msm: kgsl: Limit buffer dumping in the snapshot to the current context

In the vast majority of cases the only interesting indirect buffers (IBs)
 to dump in a shapshot are the ones that are in the current context
because they are the ones that probably contributed to the hang. Limit
the IBs that are dumped to the range between the start of context and
the RB read pointer. If the start of context can't be found, then dump
from the start of the ringbuffer dump to read pointer. Also, since
buffers can be reused (though typically not in the same context), use the
most recent parsed size when dumping the IB to make sure we get the right
buffer contents for the most recent usage of said buffer.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit e9e91bf3e498aeb58bc4e890c11e4f03e8ff8185)

Change-Id: I169ee434f8c234966e0d26edf5c29275bab91c74
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: Identify all variations of indirect buffer opcodes

When searching for indirect buffers to dump in the snapshot, look for
all of the variations for the indirect buffer opcode, not just the
most commonly used one.  This picks up many IB2 buffers that were
previously left behind.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit 2832867ed26b8f673610cb9017ff71dce7f0c5ee)

Conflicts:

	drivers/gpu/msm/adreno_pm4types.h

Change-Id: Id8e2fbbb15780e41f8465e8573cc7b761f17c334
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: allocate the snapshot buffer from kmalloc

Having the snapshot buffer physically contiguous will make
it easier to recover from a ram dump in case the system
crashes after a hang. Also log the buffer address when
the snapshot is created so we know where to look for it.

Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 9d15ae432b2f47182dcb3f191089cf892630e406)

Change-Id: I74dbc3f40461e079506a4d4f7e56740c75454713
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: fix size checking in adreno_find_region

This function is supposed to return the memdesc that
contains the range gpuaddr to gpuaddr + size. One of the
lookups was using sizeof(unsigned int) instead of size,
which could cause false positive results from this function
and possibly kernel panics in the snapshot or postmortem
code, which rely on it to do bounds checking for them.

Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 1ab51045b57edc5af3079599cfc9b843b03843f9)

Change-Id: I818c603f5b301865a87ec6bd84e6a6922c7c3234
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: handle all indirect buffer types in postmortem

Postmortem dump was not parsing CP_INDIRECT_BUFFER_PFE commands.
Snapshot was recently fixed to handle this, and this change
extends support to postmortem dump.

Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 6ca1d15190f730e9c0eca510101b24c39297b736)

Change-Id: If30279161881afabb944531963daf3b5adfb1788
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: Poke regularly in adreno_idle

Poking once during adreno_idle is not enough; a GPU hang may still happen.
Seen on 7x27A. Write a few times during the wait timeout, to ensure that
the WPTR is updated properly.

CRs-fixed: 340123
Signed-off-by: Lynus Vaz <lvaz@codeaurora.org>
(cherry picked from commit 284d10404f81d05a59655dd03b72c80fae112b9d)

Change-Id: I7281fc1f02f38380a3f3e8ad16a7298f5bf4c1de
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
msm: kgsl: Add a new property to IOCTL_KGSL_DEVICE_GETPROPERTY

Return the reset status of the GPU unit when
IOCTL_KGSL_DEVICE_GETPROPERTY is called with
type KGSL_PROP_GPU_RESET_STAT

(cherry picked from commit 2dfe5dd0c53c8f9abc83bc6a9671d499c8135f68)

Change-Id: I6109b2d161e1c5a82363b582e2c1f106e2753742
Signed-off-by: Sudhir Sharma <sudsha@codeaurora.org>
msm: kgsl: Make sure kmemleak tool does not report incorrect mem leak.

Certain memory allocations are not properly tracked by kmemleak tool,
which makes it to incorrectly detect memory leak. Notify the tool by using
kmemleak_not_leak() to ignore the memory allocation so that incorrect leaks
report are avoided.

CRs-Fixed: 334294
Signed-off-by: Anshuman Dani <adani@codeaurora.org>
(cherry picked from commit eecd52061a50325db02d2396bfd455b306a04f34)

Change-Id: If2c955f95c650a73a04aca01977dc7ac13319878
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: kgsl: Do not BUG in GPU recovery if a valid context can't be found

Don't BUG() if we can't find a valid context to recover to; just fail and
go into a zombie state. This usually happens after a first hang goes bad
and the system keeps hanging while trying to find its way. The problem with
a BUG() in this case is that the system goes down and we can't recover the
snapshot or other debug information.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit a400d8da2d5464a47b3fe19b96a3bb04af82114e)

Change-Id: I68581017f6e78a866a6d91033f980ecd06d96360
Signed-off-by: Gopal G Goberu <ggober@codeaurora.org>
msm: kgsl: set the dma_address field of scatterlists

Ion carveout and content protect heap buffers do not
have a struct page associated with them. Thus
sg_phys() will not work reliably on these buffers.
Set the dma_address field on physically contiguous
buffers.  When mapping a scatterlist to the gpummu
use sg_dma_address() first and if it returns 0
then use sg_phys().

Change-Id: Ie5f19986446be4383dfbfffa2534136b592e8e46
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 582fe31d2719b45fd0accf85bf2351f1a15b97b7)
msm: kgsl: Allocate physical pages instead of using vmalloc

Replace vmalloc allocation with physical page allocation. For most
allocations we do not need a kernel virual address. vmalloc uses up
the kernel virtual address space. By replacing vmalloc with physical
page alloction and mapping that allocation to kernel space only
when it is required prevents the kgsl driver from using unnecessary
vmalloc virtual space.

CRs-Fixed: 346921

Signed-off-by: Harsh Vardhan Dwivedi <hdwivedi@codeaurora.org>
(cherry picked from commit 8cb835b656ae2c3faa79af4f374563d344b46d4a)

Conflicts:

	drivers/gpu/msm/kgsl_sharedmem.c

Change-Id: I88e1eb282cc4822970811728d00b1096188f64e9
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
(cherry picked from commit d5fa912f599a71223496d2ff97ecd5040b95baaf)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: increase valid timestamp range

The existing timestamp_cmp function returns a different
result depending on the order of the input parameters due to
having an asymetric valid window. When no rollover is
detected the window is 2^31 but when a rollover is detected
the window is 25000. This change makes the rollover window
symmetric at 2^31.

CRs-fixed: 347631
Signed-off-by: Jeff Boody <jboody@codeaurora.org>
(cherry picked from commit beb4a64b08ac49d4e01f4dd0c911c14ed6939f6d)

Conflicts:

	drivers/gpu/msm/kgsl.h

Change-Id: I22dcc773438df32f105dc8bd069051c48fde5018
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
(cherry picked from commit 267762656c2fe837749eec87cd1593e21b9077b6)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: flush outer cache for alloc_page() pages

The outer cache needs to be flushed for these pages
after they are allocated so that the GPU and CPU
have a consistent view of them.

CRs-Fixed: 346921
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 7018a21bf2b52f1296557d1f6ec9ed64e9169de7)

Change-Id: Id5c11ab8d856fc270330fd3258baa51b8efca192
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
(cherry picked from commit a240fca76b5562d3ca53677151213286306bbc96)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: call sysfs_notify when a hang snapshot is made

/sys/class/kgsl/kgsl-3d0/snapshot/timestamp will have sysfs_notify()
called when a hang happens, so that a userspace process can
poll(2) on this file to detect a hang.

CRs-Fixed: 354385
Change-Id: I0a3c8fcbe3fb09256bcd12f6e63c107536734485
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
(cherry picked from commit 90812c8eac42b5ce7bb48e35897537021e01e6de)
(cherry picked from commit 15456a25c55409ee3032c4e083d232a3d3aedf6e)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: release the wakelock when going to suspend

This change is needed only if slumber is disabled
CRs-Fixed: 355216
Signed-off-by: Suman Tatiraju <sumant@codeaurora.org>
(cherry picked from commit 48e727689b8fabbb44f20e86f1ad6a04b8111e0a)

Change-Id: I5183e9e409f494757e44a919b87d3b078ac9ea7d
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: disable early suspend and late resume for 2d cores.

The powerrail off during slumber is causing a GPU hang

CRs-Fixed: 355216
Signed-off-by: Suman Tatiraju <sumant@codeaurora.org>
(cherry picked from commit 2bd5c33b0c7a47fffc089ced6d14efc0369a5bdc)

Change-Id: I6a1d933abaca690124170dbda2a3ccc0cb1879b3
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: fix cleanup code in kgsl_ioctl_map_user_mem()

ion handles need to be handled differently than pmem or
ashmem handles.

CRs-Fixed: 359268
Change-Id: I20637644f247859245fbba80e5bc6b50fd896403
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: convert timestamp_cmp logic to use unsigned logic

The meaning of timestamps becomes difficult to understand when
converting between unsigned and signed values. Values that
were seperated by 0x80000000 also returned inconsistent
results.

CRs-Fixed: 355752

Conflicts:

	drivers/gpu/msm/kgsl.h

Change-Id: I5d5d1ad64ef120da8769d72ac69dde37f8f5c6ae
Signed-off-by: Jeff Boody <jboody@codeaurora.org>
Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: Store process mem entries in a rbtree

The list of memory objects attached to a process gets searched quite
a lot during normal operation of the driver.  For processes with a
lot of memory allocations, the linear search through the list is O(N)
and uses a lot more CPU during critical loops than it should. Change
the mem entry list to a rbtree for faster search speeds.

Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>

Conflicts:
	drivers/gpu/msm/kgsl.c
(cherry picked from commit c9559e461e3df1b828218421430a38c9d7a38ca4)

Change-Id: Ie1639fe75718038c8149e519952f7085dede4c0a
Signed-off-by: Jeremy Gebben <jgebben@quicinc.com>
(cherry picked from commit 33bceb148eaadf18ea91378dd9b9ac5861b36a09)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>
msm: kgsl: rework ib checking

Separate ib parse checking from cffdump as it is useful
in other situations. This is controlled by a new debugfs
file, ib_check. All ib checking is off (0) by default,
because parsing and mem_entry lookup can have a performance
impact on some benchmarks. Level 1 checking verifies the
IB1's. Level 2 checking also verifies the IB2.

Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>

Conflicts:
	drivers/gpu/msm/adreno.h
	drivers/gpu/msm/adreno_pm4types.h
	drivers/gpu/msm/adreno_ringbuffer.c
(cherry-picked from commit d0ab6adeeae7dd039069fd93e6649777aff23668)

Change-Id: If7c93596d0228792002868a56cdf61dc9d34dd5c
Signed-off-by: Jeremy Gebben <jgebben@quicinc.com>
(cherry picked from commit f6924037a5d56f2934ef7d2b2cafb5ab3e49c94e)

Signed-off-by: Zhoulu Luo <zluo@codeaurora.org>