msm: kgsl: Making several static functions non-static

Change several static functions to non-static. Also add a kgsl
ioctl helper function.

Change-Id: I984b076cc593a08b49e5a010d02187b9b217dfdb
Signed-off-by: Shrenuj Bansal <shrenujb@codeaurora.org>

msm: kgsl: Remove non-context timestamp APIs

We no longer intentionally expose global timestamps to the user so
there is no reason to support APIs that excusively use global
timestamps.

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

msm: kgsl: Remove GPUMMU

Remove GPUMMU code because we dont have the hardware on
current targets.

Change-Id: I045111111f3eaa258c808936b6250703c9c4eb62
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>

msm: kgsl: allow creation of events from the future

For contexts created with the KGSL_CONTEXT_USER_GENERATED_TS,
allow events to be created for timestamps that have not
been issued yet. Presumably these contexts know what they
are doing.

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

msm: kgsl: Make a helper macro for getting the context id

Use a bit of shorthand to get a context id (or the global ID if no
context exists).

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

msm: kgsl: Flush the UCHE when context changes

Add commands to flush the GPU UCHE when a new context submits
commands to GPU. The ensures that the new context does not use
stale data present in UCHE.

Change-Id: I123a323be5f3fb9d1f9f96fed5bb68b8d0d27d76
CRs-Fixed: 607976
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>

msm: kgsl: Refactor the GPU event engine

The old GPU event model was pretty heavy weight. It walked a lot
of issues while holding the mutex and was generally not very
flexible or scalable.

The new design introduces event groups. Event groups have a list of
common events with a local lock.  Each context has a default event
group and the device adds a few more for global events and IOMMU events.
Each event group is processed when timestamps expire and the expired
events are scheduled on a workqueue. We never need to take the device
mutex in any of the event code and we have the flexibility to deal with
both global and per-context timestamps in a reasonably generic manner.

Speed is a premimum at every step of the process, so we use RCUs for
the global group list, a new kmem cache for the event structs and
generally go out of our way to avoid blocking in rendering threads
even a little bit.

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

msm: kgsl: Use temp unsigned long variable to prevent truncation

Casting the output of adreno_wait_event_interruptible_timeout() to
int can cause problems on 64bit since msecs_to_jiffies() returns an
unsigned long. This results in ret having negative values which are
not appropriately handled. Instead, use a temp variable to get the
returned value and translate that into a return value.

Change-Id: I5d1d2b40b4b832bdb018560630ab77dce98ea866
Signed-off-by: Shrenuj Bansal <shrenujb@codeaurora.org>

msm: kgsl: Fix miscellaneous compilation errors

Do not include ocmem header file for non-ocmem targets.
Use correct print type for size_t.

Change-Id: Ica17bdf908b8e9b2849f95f45746d5c877ed4cfc
Signed-off-by: Pu Chen <puchen@codeaurora.org>

msm: kgsl: Add a generic IB parser

Add generic IB parser to parse adreno IB's and find the objects
that are required to execute the IB. This generic parser can
be used from snapshot and other places like CFF to get the
list of objects that are required for an IB to execute
Use the generic parser from snapshot to find the objects in IB's that
need to be dumped during snapshot and implement IB object list in
snapshot so that the amount of time to take snapshot does not increase.

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

msm: kgsl: Save snapshot IB data in queued work item

Queue a work item after taking snapshot. The work item saves
the data placed in frozen objects into a separate allocated memory.
The data in frozen objects can change after snapshot is taken and
before the snapshot is dumped or pulled from the system. This work
item in between helps in getting the actual data in the objects by
saving the data in memory. Data can change in the time the work item
executes and snapshot is taken but this time gap is much smaller
compared to snapshot being pulled and snapshot being taken so it
avoids incorrect data in most known cases.

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

msm: kgsl: Parse set draw state packet found in IB for A4XX

Add parsing of set draw state packet when searching for memory
objects in IB. This is a new PM4 packet type introduced for
A4XX. The packet declares a list of command streams that can
contain pointers to other objects like vbos, textures, etc.
When parsing a set draw state packet all the objects in the
command stream needs to be tracked including the command stream
itself.

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

msm: kgsl: Restructure type0 packet parsing

Make parsing of type0 packet more generic by creating a
common array of registers that have addresses which need
to be tracked. This makes it easier to add parsing of type0
packet for a new gpu chip. New chips will have new registers
that need to be tracked which can be added to the existing
list and this makes the code more generic.

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

msm: kgsl: Process null flag and assign new ID for set draw state

If no flags are set in a set draw state packet then that is
an indication to load the state in the packet for the first
time. This situation was not handled corectly while parsing
the set draw state packet, fix this. Also, when saving set
draw commands save it with a new ID defined specifically for
this packet sequence.

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

msm: kgsl: Add new packet types for A4XX while parsing IB

Add parsing of new draw call packets types when parsing an IB. These
new packet types have been introduced with new chip A4XX.

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

msm: kgsl: Fix overlap checking of address range

While checking if an address range can be merged or is contained within
a different address range also check if the type of the ranges are same.
If they are not same then do not merge them. Also, when checking for
overlap condition reduce the size of range by 1 since the last byte
till the size of the range is not a part of the range.

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

msm: kgsl: Fix skipping of certain data objects in snapshot

Few data objects were not saved in snapshot because the condition
checked to save the object was incorrect.

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

msm: kgsl: Track additional memory for A4XX in cp packet parser

In A4XX UCHE_INVALIDATE0 and UCHE_INVALIDATE1 have memory address
programmed in them, track them in the cp parser.

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

msm: kgsl: Add kgsl_malloc/kgsl_free helpers

Instead of constantly trying to reinvent the wheel when doing
kernel allocations of indeterminate size create a alloc/free
pair of functions that can do the magic for us. Replace all of
the existing bits and pieces with the new function calls.

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

msm: kgsl: Remove some uneeded MMU infrastructure

Pick up some bits and pieces left over from the removal of GPUMMU.

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

msm: kgsl: Remove unused legacy IB timeout variables

Remove unused variables from legacy IB timeout detection.

Change-Id: I4df94ee43072ccebff4045c5950edbc406de38a8
Signed-off-by: Hareesh Gundu <hareeshg@codeaurora.org>

msm: kgsl: Use sg->length instead of sg_dma_len API to get sg size

The CONFIG_NEED_SG_DMA_LENGTH option is set for the arm64 kernel
config. This results in the sg_dma_len API to select sg->dma_length
for the sg len which is never set and is always 0. Therefore,
change to sg->length which is what we set and use in our code.

Change-Id: I11aabf4c56bf05ba4e1c5f582b739f3b3503ec63
Signed-off-by: Shrenuj Bansal <shrenujb@codeaurora.org>

msm: kgsl: Remove unused sharedmem functions

No sense in keeping these sharedmem functions around and have to
try and maintain them.

Change-Id: I14b8584c575ed1f935251c54610c572cb51279d4
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>

msm: kgsl: Fix spinlock recursion in destroy pagetable

pagetable list is protected by ptlock. Few functions
while iterating over a pagetable list takes same ptlock,
kgsl_destroy_pagetable() also need same lock. This will
cause spinlock recursion if kgsl_destroy_pagetable()
called while iterating over a list. Created two versions
of same function one is with lock and other is without
lock.

CRs-Fixed: 621172
Change-Id: I61440f99022fce8629a57bb5661e2eef9613187b
Signed-off-by: Prakash Kamliya <pkamliya@codeaurora.org>

msm: kgsl: set usermem type in the memdesc flags

It is sometimes helpful for userspace to know if
a buffer is allocated memory or mapped in from
another driver. Add  KGSL_MEMFLAGS_USERMEM_ values
so this information can be stored into the flags,
which are visible to userspace after the allocation
and map ioctls return, or via IOCTL_KGSL_GPUMEM_GET_INFO.

Because enum kgsl_user_mem_type only accounted for
mapped memory types, the flags value adds 1 to each
enum so that 0 can mean allocated (not mapped)
memory.

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

msm: kgsl: Remove A3XX legacy context save/restore

The A3XX context save/restore code in the kernel is no longer needed by the
UMD.  Remove it and all other vestiges of context save/restore.
And there was much rejoicing.

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

msm: kgsl: Allow users to set context priority at creation

Allow context priority to be passed in as a flag when creating
contexts. Priority is set only at context creation and cannot be
changed. Priority is used as a hint to the dispatcher when sending
commands to the GPU hardware.

Priorities are only hints. If the driver wishes, it can reject the
priority passed in. If the driver rejects the priority (if there
are too many high priorities for example), then the new context will
be created at the default priority level.

Change-Id: I12253275b7045048d59d326fbecc6446714f33a9
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>

msm: kgsl: Support the KGSL_CONTEXT_CTX_SWITCH flag

Sometimes the user driver has reason to force the command preamble
to run on every submit and not just when the context is switched.
Support KGSL_CONTEXT_CTX_SWITCH for both an entire context or
just a single submission.

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

msm: kgsl: hold a reference on pages found with memdesc_sg_virt()

The GPU could write to pages gathered with this function, so
references should be held while they are in use.
Since the function was basically a broken version of
get_user_pages(), just call that instead.

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

msm: kgsl: Remove unused variable

pwrscale_kobj should have been removed in the devfreq
transition.  Clean it up now.

Change-Id: I2f464b8defd2dda89ffbac571efa2299749dd162
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>

msm: kgsl: Consolidate and correctly update reading pwrstats

Only read the various power counters before turning the clocks
off or when reporting to devfreq.  Reading them at other times
is redundant.  Make sure to accumulate the same set of counters
at both points.

Change-Id: I0e4e988d25a18fcede0f0fb022cb7783f5354cd2
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>

msm: kgsl: Protect fault detect start with active count

Get the active count when enabling GFT from sysfs so the performance
counters can be properly programmed.

CRs-Fixed: 622252
Change-Id: Ic0dedbadfe56f8c66aca15fb521494a2fc00332b
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>

msm: kgsl: Remove redundant logging on state change

This code would cause issues in the SLEEP state transition.
As the information it provides can be deduced other ways
remove the logging.

Change-Id: Ia6c7c1efa17c44340454cfcc117f98c3c2dd9fda
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>

msm: kgsl: restore usage flags on imported buffers

Commit 20ef1f47dd6754f8b49850591c5d6aa630248ca0 (msm: kgsl: set usermem
type in the memdesc flags) inadvertently prevented the KGSL_MEMTYPE_MASK
bits from being set by userspace on mapped buffers. Put them back.

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

gpu: msm: Remove a4xx work

Change-Id: Ie8589d6862552a4d917bdead6c43c8bd6fb3f08f

msm: kgsl: Keep track of kernel space mappings to memory

Add a counter to count number of times a memory descriptor is
mapped in the kernel and unmap the kernel mapping only when the
count reaches 0. There can be multiple threads using the kernel
mapping at the same time hence a counter is used to make sure
that we do not unmap the mapping before the last thread has
finished using it.

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

gpu: msm: Remove long-deprecated genlock code

Change-Id: Ibb9c1d3418d05d6552e30dfecc0e177c7eab33a6

gpu: msm: Cleanup various crufty things

Change-Id: I77e5bf0ff49494ae4efa00dd4edf544b99ebc01c

msm: kgsl: Use the correct method for exporting trace event functions

Use EXPORT_TRACEPOINT_SYMBOL instead of my lazy "trace" wrappers for
trace_kgsl_regwrite and trace_kgsl_issueibcmds thus saving lots of
code and a shred or two of dignity along the way.

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

msm: kgsl: Mark local functions as static and remove unused code

Mark a bunch of functions and structures as static and remove unused
functions.

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

msm: kgsl: Add VBIF register programming for A4XX

Add VBIF register programming for a420 core during start up of
this device.

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

msm: kgsl: Save and restore coresight debug registers on power collapse

Add shadow registers for the coresight debug registers and save and
restore the values when the GPU goes into power collapse. Reset the
registers to the default initial values when coresight is enabled
and write 0 when it is disabled. If the GPU is power collapsed,
don't wake it up to read and write individual registers while coresight
is enabled.

CRs-fixed: 552580
Change-Id: Ic0dedbad02deac88c50371956c993662f5d34a61
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>

msm: kgsl: When switching pagetable use current context ID

When we switch pagetables then the pagetable switch commands
should be executed on behalf of the current active context and
not the context to which we are switching. This is because
until the pagetable switch occurs the GPU will be using the
pagetable of the active context.

CRs-fixed: 574989
Change-Id: I9ca65ea399e39a158dc72adc2c7b661f62f18c93
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>

msm: kgsl: irqctrl() didn't need to be a KGSL level function

irqctrl is only useful at the adreno level so remove it from
the kgsl function jump table.

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

msm: kgsl: allow dma-bufs to be mapped by user address

Allow a userspace pointer to a dma-buf to be mapped by
IOCTL_KGSL_MAP_USER_MEM with KGSL_USER_MEM_TYPE_ADDR,
with the correct dma-buf handling.

CRs-Fixed: 607830
Change-Id: I037ed070b96592040328014c9adbd8a5b4d4191f
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>

msm: kgsl: hold a process_private refcount in process_mem_print

There is a possible race condition where the process can be going
away while its debugfs 'mem' file is being read, which could cause
memory corruption.

CRs-Fixed: 627780
Change-Id: I697486faeb3f186fd1220d0acc1e449a4f7b77b0
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>

msm: kgsl: Check the validity of gpuaddr

In kgsl_mmu_gpuaddr_in_range(), actually check if the gpuaddr is
within the requisite bounds instead of directly returning success.
In addition, change check for the special SVM case to check
specifically if cpu map is enabled.

Change-Id: Ib3a313231bcc191fefffd487d473ffc3e9fa53d8
Signed-off-by: Shrenuj Bansal <shrenujb@codeaurora.org>