msm: kgsl: Use the right set of flags for issuing user commands

KGSL_CMD_FLAGS_EOF is both a valid command flag from the user and
a valid internal flag for the ringbuffer submit code.  This is
the only flag that can do this - the other internal flags either
have different meanings or aren't valid user flags.  Use an internal
register to store the command flags in adreno_ringbuffer_issueibcmds
so we don't accidently end up sending in the wrong control flags to the
ringbuffer code.

CRs-fixed: 546449
Change-Id: Ic0dedbadb9245002a60aa54cb51d35d98b9cb531
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Don't use uninitialized values in hang detection

Under some circumstances some of the registers used for hang detection
are not defined (usually because the hardware doesn't support them).
The hang detect code properly skips those registers when reading the
hardware but the corresponding offsets in the destination array are
still compared to detect a hang.  Since the array was defined on the
stack and those particular members were never read from the hardware
the values could be non zero a real GPU hang could be missed.

Skip the undefined hang detect registers in all three for() loops
where the list of hang detect registers are used.

CRs-fixed: 540224
Change-Id: Ic0dedbad6e654e3a4e6db5468eac698e91e7f32b
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Change the order of GPU global mappings

Move frequently accessed memories like read write pointers,
memstore and mmu setstate memory together. This helps
TLB prefetcher to always keep these entries in TLB.
If they are far apart they are going in and out of TLB
and causing XPU violations.

CRs-Fixed: 548223
Change-Id: I4aa72481ebab839745e4ad9b4dc9912925100dad
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
iommu: msm: Refactor remote spinlock defconfig

Refactor the remote spinlock defconfig to allow it to be used with
subsystems other than GPU. This will enhance the usability of this
feature and minimize the chance of errors when enabling this feature.

CRs-fixed: 517873
Change-Id: I609cd77ece4e8d2e15e1a24d87a23be05a61b60f
Signed-off-by: Olav Haugan <ohaugan@codeaurora.org>
iommu: msm: Refactor use of remote spinlock

The remote spinlock was originally meant to synchronize access between
GPU and CPU accessing SMMU. However, there are more use cases that
this remote spinlock serves. This change decouples the spinlock from
GPU and adds support for taking the spinlock only for SMMUs that are
specified in the device tree.

CRs-fixed: 517873
Change-Id: Ic50992d0d1a102fbd05855e09e254e627f99ec33
Signed-off-by: Olav Haugan <ohaugan@codeaurora.org>
msm: kgsl: Enable the shader corruption workaround for all A3XX targets

The power-on shader corruption workaround is required for all A3XX
targets.

CRs-fixed: 556872
Change-Id: Ic0dedbad5ef5a1bfec7cba148a652e1766517e57
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Change an overflow check to be "smart" compiler proof

In its infinite wisdom, GCC will optimize out this overflow check:

   if ((gpuaddr + size) < gpuaddr)

Which might totally make sense for the compiler but it doesn't do us
a bit of good. Replace the existing overflow check with something
that GCC can't ignore.

CRs-fixed: 544025
Change-Id: Ic0dedbad921545a01d5fe80a20c3f2f4f86ef641
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Return NULL when done parsing the profiler assignments string

The assignment string parser has a corner condition wherein the user
can craft a malicious string that contains a \0 and force a buffer
overrun.  Avoid this by always returning NULL when the parser encounters
a \0.

CRs-fixed: 547601
Change-Id: Ic0dedbad946e597a5a245df41239b89f114ba9ee
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Fix overflow check in mmap_range_valid

A eager compiler might optimize out the overflow check in
mmap_range_valid.  Add a check against ULONG_MAX to ensure
that the check is performed.

CRs-fixed: 546373
Change-Id: Ic0dedbad478cc3bd65a00a7b31d2039095c005d6
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Try to run soft reset on all targets that support it

Run soft reset on all targets that define a soft_reset function hook.
If the target defines jump table offsets then we can run through a
faster reset sequence, otherwise default to the slower full reset
path.  In any event, both these options are much faster than the
hard reset path that toggles the regulators.

CRs-Fixed: 519855
Change-Id: I0ff1ea61dbe46afe08e04b2292949cf631514d99
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Get reference to process private when attaching mem entry

When a memory entry is attached with to a process private list then
increment the refcount of the process private structure so that the
structure is valid as long as the entry is around. The context
structure is also handled in similar manner. Also, the refcount to
the entry needs to be decremented when the handle that created the
entry goes away and not when the process private structure is
destroyed.

Conflicts:
        drivers/gpu/msm/kgsl.c

Change-Id: I225698ad4081947a93eb553104e5259bbf31f293
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
Signed-off-by: Shrenuj Bansal <shrenujb@codeaurora.org>
msm: kgsl: Don't resume MMU before detaching domain

In cases of page fault, resuming the MMU before detaching
the domain locks up the MMU and causes erroneous behavior.
To avoid this, only resume the MMU after detaching the domain.
Additionally, do not attempt soft reset in cases of page faults.

CRs-fixed: 550840
Change-Id: I0c8ea79d802d8b8d29e03af6f3153ee6c3401400
Signed-off-by: Harsh Vardhan Dwivedi <hdwivedi@codeaurora.org>
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Add TSE performance counter to software hang detection

Our current hang detection logic covers rendering pass by checking for
shader performance counters but it does not cover the case where there
is long binning or resolve pass. For long binning pass shader is inactive
making our current hang detection logic faulty. Checking for TSE number of
input primitives covers binning and resolve passes and makes our hang
detection logic robust.

CRs-fixed: 521284
Change-Id: Ifdec4b53685903456feb367f64380119b1485408
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Add dispatcher and server-side-sync support

Add dispatcher and server-side-sync support from the msm-3.10 branch
featuring these commits:

	msm: kgsl: Add a command dispatcher to manage the ringbuffer
	msm: kgsl: Implement KGSL fault tolerance policy in the dispatcher
	msm: kgsl: Don't process events if the timestamp hasn't changed
	msm: kgsl: Add a new command submission API
	msm: kgsl: add kgsl_sync_fence_waiter for server side sync
	msm: kgsl: Add support for KGSL_CMD_SYNCPOINT_TYPE_FENCE
	msm: kgsl: Add the event callback symbol to GPU event tracepoints
	msm: kgsl: Add a ftrace event for adreno context switches
	msm: kgsl: Try to run soft reset on all targets that support it
	msm: kgsl: Check for idle in the fault detection timer
	msm: kgsl: Improve the dispatcher issuecmds loop
	kref: Implement kref_get_unless_zero v3
	msm: kgsl: fix a race condition when calling kref_put
	msm: kgsl: Check the return value of _kgsl_context_get()
	msm: kgsl: Hold the plist spinlock to protect nodes
	msm: kgsl: Hold a reference to process private when creating context
	msm: kgsl: Do not delete command batch unless it's retired
	msm: kgsl: Check whether context is detached before updating timestamp
	msm: kgsl: Free the cmdbatch when it fails to be requeued
	msm: kgsl: Clear dispatcher fault bit if inflight commands are 0
	msm: kgsl: Do not register additional IOMMU faults
	msm: kgsl: Do not overwrite dispatcher fault
	msm: kgsl: Define new fault type for IOMMU fault
	msm: kgsl: On IOMMU fault force a hard reset
	msm: kgsl: Max the expired timestamp of invalidated context
	msm: kgsl: Do not allow events with bogus timestamp
	msm: kgsl: Immediately set the fault bit on a pagefault
	msm: kgsl: Set FSR to 0 on IOMMU pagefault
	msm: kgsl: Do not call dispatcher fault handler if already called
	msm: kgsl: Only skip IB for faulting context
	msm: kgsl: Retry hard reset more than once on failure
	msm: kgsl: Wake up threads after context detach
	msm: kgsl: Don't pause the dispatcher in adreno_dispatcher_stop
	msm: kgsl: Fix draw context fair scheduling in the dispatcher
	msm: kgsl: Return -EPROTO in certain functions if the GPU has faulted
	msm: kgsl: GPU pagefault policy change to not stall on pagefault
	msm: kgsl: A few quick cleanups to improve stability
	msm: kgsl: Return -ENOTTY for legacy waittimestamp calls
	msm: kgsl Check NULL return from kgsl_get_device_from_dev
	msm: kgsl: use kgsl_context_detached() in adreno_dispatcher_queue_cmd()
	msm: kgsl: remove KGSL_STATE_DUMP_AND_FT
	msm: kgsl: Re-add the irq_last functionality
	msm: kgsl: Allow sync points to be created on the submitter's context
	msm: kgsl: Don't repeatedly call adreno_stop during reset
	msm: kgsl:  Add an helper function to clear the GPU fault after reset

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

Conflicts:
	drivers/gpu/msm/adreno.c
	drivers/gpu/msm/adreno.h
	drivers/gpu/msm/adreno_postmortem.c
	drivers/gpu/msm/kgsl_log.h
	include/linux/kref.h
msm: kgsl: Store process private pointer in context

Store the process private pointer in the context. Earlier the process
private pointer was referenced through the dev_priv pointer in the
context, but the dev_priv pointer can be destroyed before the context
process private so store this pointer locally.

Change-Id: Ic07680b79db55d6306306bd61bda5a1288813914
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Restructure performance counters

Restructure the performance counter mechanism by combining the two
structures declared for it. The 2 structures can only refer to a
single counter hence it is easier to manage by having them
under a single structure.

Change-Id: I19d13cf5aa619b85a332b383b464c2af65ad38c9
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Fix reading of special case counters

VBIF, VBIF power and power performance counters are special case
counters that are stopped by using a different set of registers.
Handle the case to read these counters in separate functions.

Change-Id: Ia89ba44d1031496ac7742b435c28ea2f3b2177fa
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Fix page align allocation overflow

KGSL does alignments for all user memory requests. If the
request is large enough the alignment operation will overflow
resulting in a valid request of size 0. It's just a matter of
time until the kernel or user tries to then do something useful
with this buffer resulting in a hang. Perform a sanity check
after doing the alignments to ensure the overflow did not occur.

Change-Id: I68f0f2c7dfdfb117e6d1320ec594cbdf75407a3e
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: Run the idle timer while the GPU is not power collapsed

If the GPU is not in its lowest permitted power collapse state,
make sure there is a timer running to get the GPU to that state.

Change-Id: If57aaaf02087036bc4e24725c0cd3de5bc1f1034
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>
msm: kgsl: Call adreno_init only once on startup

The adreno_init function initializes things that need to be initialized
only once on the first start up. After that until the device is removed
the adreno_init function does not need to called again. Make changes
to execute this function just once until the device is removed.

Change-Id: I1bd18dd9ec70146dc74da1ee9ad1186576344309
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>