msm: kgsl: Return correct timestamp for consumed timestamp reads

adreno_readtimestamp() would return a register value when asked to
get the KGSL_TIMESTAMP_CONSUMED value.  With per-context timestamps
this no longer holds valid since the value in the register could
correspond to a different contexts timestamp.  Return the start of
packet timestamp for the specified context now that the start of
timestamp values are correctly submitted.

CRs-fixed: 382888
Change-Id: Iad56d07eb4a2973a5906d989c96dd117ff902cea
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: Verify the user doesn't accidentally submit a zero length IB

Indirect buffers to be executed will not be (and cannot be) zero length.
Check and reject.

Change-Id: Ic0dedbadea429c4f7bd386c5e64603b51ea8af61
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Add a ftrace event for register writes

It is interesting to know when and how we program the hardware so
add a tracepoint to monitor register writes.  Register reads are
not traced since they are done much more frequently and are somewhat
less interesting from a debug perspective.

Change-Id: Ic0dedbad8ee85f154b9ba8731fe2c14603351243
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Track GPU device resets

Keep track of the number of times the GPU power rails have been
cycled and provide said number to the user in the device sysfs
directory.

Change-Id: Ic0dedbad4489524a98e52b9993131257068ae2ef
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Remove an uneeded log message that pre-dates tracing

Before we started using tracepoints this log message was used for
debugging the critical loop. Now that we are smarter remove the
unneeded log message.

Change-Id: Ic0dedbad57d97294cf2cb3865cf25f698aaea778
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Use ERR_PTR to return errors from kgsl_create_context()

Normally kgsl_create_context() returns a pointer to a new context.
In case of errors it just returns NULL pointer, and that doesn't allow
to propagate the right error code back to the user via
kgsl_ioctl_drawctxt_create()
This fix modifies kgsl_create_context to use ERR_PTR macro for passing
error codes to its caller.

Change-Id: I447c1765828912b0994bcae67a73864e62eef9b6
Signed-off-by: Vladimir Razgulin <vrazguli@codeaurora.org>
msm: kgsl: Add a type field to the adreno draw context flags

Allow the user space to pass in a type field to indicate the
type of upper level library that owns the context. The type
field is added to all the appropriate ftrace output for easier
debugging.

Change-Id: Ic0dedbadd42fc5ccfffd89738affd4794a6ab85e
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Fix context reference counting

Get rid of kgsl_find_context. Use instead kgsl_context_get that does
correct RCU read locking around the itr_find and increases the
reference count on the context before returning it.  This eliminates
the chance that a context will be destroyed while somebody is still
using it.  Of course increased use of kgsl_context_get is accompanied
by kgsl_context_put in all the right places.

Change-Id: Ic0dedbad73d497fd9b451aefad8e5b28d33b829d
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Move timestamps inside the context structure

Store the timestamps inside the context rather than in a
list stored in the ringbuffer.  This allows for easier
maintanability as well as keeping all context data
centralized.

Change-Id: I0467d07be6c8bb9f062a81a40629c0288be7e868
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: Allow tasks to signal pending events

Add a type parameter to event callbacks to give tasks a way to
communicate error conditions to the callback. Current type values
are KGSL_EVENT_TIMESTAMP_RETIRED for normal timestamp expiry and
KGSL_EVENT_CANCELLED if the event is cancelled prematurely.

Change-Id: Ic0dedbad9907cd50642a604a3af562e01a4b4a3b
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Add memfree_history debug feature

A kgsl user can free up an allocation of GPU memory with
kgsl_ioctl_sharedmem_free() function, but the GPU address
might still be in use resulting in a GPU page fault.
A bug of this kind is hard to find because the driver keeps
no trace of kgsl_ioctl_sharedmem_free calls.
This change implements a "memfree history" debug feture that
allows to see an information about a last couple of hundred
memory free calls in kernel/debug/kgsl/kgsl-3d0/memfree_history
file.

Change-Id: I12e0e3702db56d99d5de644739b364dea4cc37b1
Signed-off-by: Vladimir Razgulin <vrazguli@codeaurora.org>
msm: kgsl: Update required timestamps during command submission

Update all the required timestamps when submitting commands to the
GPU ringbuffer for execution. The global timestamp was not being
updated in all the required cases. Now the global timestamp and
per context timestamp are updated if following conditions are met:
	1. A valid non global context is passed
	2. The context passed supports per context timestamps
	3. The command submission is not from kgsl
If 3 conditions above are not met then only the global timestamp is
updated.

Change-Id: Ib4fe6fbd2ac57fbc5306377f09072f9e4c09d442
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Lock ringbuffer translation in TLB

Lock ringbuffer virtual address translation in TLB of IOMMU. This
is required to prevent a hardware table walk from happening when
GPU executes commands that program the IOMMU registers. If a hardware
table walk happens when GPU is programming IOMMU register then it
can cause a deadlock on the bus.

Change-Id: I99428be7879a5210ba816f5f96864f94714649d7
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Add a barrier after writing to V2PUR register

Add a memory barrier after writing to V2PUR register of IOMMU. Without
this barrier the CPU core can stall because of multiple outstanding IOMMU
register transactions happening in parallel. This barrier synchronizes
the multiple IOMMU register transactions and prevents the CPU from
stalling.

Change-Id: I1d715e9fb8287b1f0b009b39b11b5469ab8861c8
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Print additional registers on IOMMU pagefault

Print more IOMMU registers when a IOMMU pagefault happens which
report whether the pagefault is a read or write fault.

Change-Id: I27acafa9dcfd0c7de9056151ed1baef7dd2346df
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: kgsl: Always fire an interupt if requested

Even if the event code specifies a dummy interupt it might still
get wrapped with the conditional check.  Make sure that all forced
interrupts from the event code get fired.

Change-Id: Ic0dedbadc4599efcbca65ad93cc27943bad2ca79
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Add device init function

Some device specific parameters need to be setup only once during
device initialization. Create an init function for this purpose
rather than re-doing this init everytime the device is started.

Change-Id: I45c7fcda8d61fd2b212044c9167b64f793eedcda
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: use kgsl_mmu_device_setstate() if the GPU is already idle

Use the default setstate function, which directly reprograms the IOMMU,
to change IOMMU pagetables or flush the TLB if the GPU is already idle.
This condition often occurs when the GPU is being powered down. In this
case it is desirable to avoid the overhead of issuing commands, waiting
for idle and firing events that results from using the GPU
command stream to reprogram the IOMMU.

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