msm: kgsl: Remove sscanf() from kgsl_pwrctrl.c

Replace sscanf() in kgsl_pwrctrl.c with a generic function to safely
convert 32 bit unsigned ints.

CRs-fixed: 563585
Change-Id: Ic0dedbad10e2264ec8fccab622d094d123b49b77
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: don't use sscanf()

Friends do not let friends use sscanf().

CRs-Fixed: 563605
Change-Id: Ic0dedbad56af5bbee4fc833701d8bc93b118efa9
Signed-off-by: Jordan Crouse <jcrouse@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: 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.

CRs-Fixed: 607976
Change-Id: I123a323be5f3fb9d1f9f96fed5bb68b8d0d27d76
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
Signed-off-by: Hareesh Gundu <hareeshg@codeaurora.org>
msm: kgsl: Fix memleak in IOMMU clock disable

IOMMU clock disable on timestamp allocates memory and passes
the pointer to event created, this memory should be freed
when the event fires.

Change-Id: Ia714550ce8a05958fb6668293cd0400a0f69d4af
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: Add new API's to track device mutex owner

Add new API's kgsl_mutex_lock/unlock to track device mutex owner.
Mutex owner is an atomic variable and all operations set/clear on
it are also atomic to avoid any race conditions.

If a thread tries to acquire the mutex using new API, try to acquire
the mutex only if mutex owner is not same as current thread. This is
to prevent deadlocks which can happen if a thread tries to acquire
mutex with mutex held.

Change-Id: I3da4d0427ede5458f6a663043c57a4799b72d8fd
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Flush TLB when mapping to IOMMU pagetable

Flush TLB when mapping to IOMMU pagetable, this is a requirement
for current IOMMU hardware.

Change-Id: I82e766f4869c737c1f5b73934eacaada40d46e10
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: set the profile max_state correctly

The max_state should be set to the number of valid powerlevels.

Change-Id: I232e9abb2328681f3c0ebff4cfbc820279371532
Signed-off-by: Suman Tatiraju <sumant@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: Change call timing to alleviate mutex contention

Finish the state transition in the _idle_check function before
calling to update devfreq.  With this reordering the devfreq
notifier won't be stalled waiting for the GPU's device mutex.

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

msm: kgsl: Remove the frequency stepping work-around

In older hardware variants while running at GPU frequency x,
there was a .5x-2x limitation on changing it while the GPU was
active.  Therefore all frequency changes were stepped to avoid
stability issues.  This limitation is no longer in effect, so
remove the work-around.

Change-Id: I9716ba4cb40148d4db5ac1478b512a93707d88a1
CRs-fixed: 598694
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>

msm: kgsl: Consolidate sysfs store functions

Many of our sysfs _store hooks grok unsigned ints.  Use a common
function to replace the various sscanf() and strtoul() calls
scattered throughout the code.

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

msm: kgsl: Avoid mixing signed and unsigned ints in kgsl_sysfs_store

kstrtou32 returns negative on error or 0 on success.  kgsl_sysfs_store
then proceeded to either return the negative value or the unsigned int
'count'.  Furthermore it compared the return value of kgsl_sysfs_store
against an unsigned int so streams were crossed everywhere.

Remove the uneeded count paramater, return 0 or negative from
kgsl_sysfs_store and handle it appropriately.

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

msm: kgsl: Make sure the bus is on to consider power enabled

In theory only reg reads/writes should be using this check, and
they don't need the bus.  But just in case, make sure it is on
before allowing any activity.

Change-Id: I660fb947ad16e2872cfb95a9d99200a197b37f31
Signed-off-by: Lucille Sylvester <lsylvest@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: Rework the gpubusy and gputop sysfs variables

Remove gputop as the devfreq sysfs files make it redundant.
Update gpubusy to reflect the busy counter used by GPU DCVS
rather than the SW controled clock gating.  Users expect gpubusy
to reflect the GPU's running frequency.

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

msm: kgsl: Fixing min_pwrlevel kgsl sysfs element

The change 30a2b23 modified kgsl_sysfs_store function calls
everywhere except in the kgsl_pwrctrl_min_pwrlevel_store()
function. As a result min_pwrlevel sysfs item behaved incorrectly.

Correct the return code check for kgsl_sysfs_store call in
kgsl_pwrctrl_min_pwrlevel_store().

Change-Id: If2b0cc9b26625c1a326b6644ab183a36868666f9
Signed-off-by: Vladimir Razgulin <vrazguli@codeaurora.org>
msm: kgsl: Enhance GFT to avoid hang->recover->hang cycle

If GFT recovered more than X times in Y ms invalidate the context
and do not attempt recovery. this is to prevent the case where GFT keeps
recovering continuously.

Change-Id: I9326a4cf0c8d1b6b92497ae5cd086e4b8bb7e89f
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Skip the entire command during fault recovery

During fault recovery the algorithm may choose to skip the faulting
command. To skip just one indirect buffer we have to rely on
CP_IB1_BASE but depending on where the fault happened the base could
already be pointing to the next buffer. This causes the code to skip
over the wrong buffer. There is no advantage to skipping just one
buffer vs the entire submission.

Change-Id: I28dc999b2156d05f7b5084df6bdd95c5b9308579
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
msm: kgsl: Mark the IOMMU setstate memory as read only

Mark the IOMMU setstate memory as read only in the pagetable.

Change-Id: Ic0dedbadb19e499c749cd744c3e89be3bcb4c2a2
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Protect CP_STATE_DEBUG_INDEX

Put CP_STATE_DEBUG_INDEX and CP_STATE_DEBUG_DATA under protection
to keep it from being written from an IB1. Doing so however opens
up a subtle "feature" in the microcode: memory read opcodes turn off
protected mode in the microcode to do the read and then turns it
back on regardless of the initial state. This is a problem if the
memory read happens while protected mode is turned off and then we
try to access a protected register which then complains and goes boom.

To account for this irregularity explicitly turn back off protected
mode in all the places where we know this will be a problem.

Change-Id: Ic0dedbad1397ca9b80132241ac006560a615e042
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Fix nice level for higher priority GPU start thread

Before, we were actually setting the lowest possible priority.

Change-Id: I3a52de949eee0faea8061dd91ed303fa8231f0d4
msm: kgsl: Reset the bus_mod value when power is re-enabled

kgsl_pwrctrl_pwrlevel_change() resets the bus_mod level when the
power level is changed but the power level doesn't get changed
on re-init if constraints are enabled with the net effect of
turning on the bus at a lower level than we would expect.  Reset
bus_mod in  kgsl_pwrctrl_enable() to make sure it always comes
up at the expected level.

Change-Id: Ic0dedbad303cc938d91583b3e3309d232590b975
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Update calculation for graphics IOMMU context IDs

Calculate graphics IOMMU context IDs using the 'label' property
of the device tree node of that context. Context IDs are independent
of the register addresses of the SMMU device and its contexts, which
change for every target.

Change-Id: I16825d7248081997a442d4fb59ca5b04b88e33fd
Signed-off-by: Lokesh Batra <lbatra@codeaurora.org>
Signed-off-by: Lynus Vaz <lvaz@codeaurora.org>
msm: kgsl: Add a flag to mark user mapped GPU buffers

Add a flag in /sys/kernel/debug/kgsl/kgsl-3do/proc/<pid>/mem
file to indicate for each GPU buffer if it is mapped to userspace
in <pid> or not.

CRs-fixed: 634962

Change-Id: I8abda74ef5656aca6b1c0315af8deb77460fa5a9
Signed-off-by: Harsh Vardhan Dwivedi <hdwivedi@codeaurora.org>
Signed-off-by: Prabhat Awasthi <pawasthi@codeaurora.org>
msm: kgsl: Return HI offset from kgsl_perfcounter_get

Previously, kgsl_perfcounter_get only returned the offset
of the LO perf counter register. This assumed that the HI
register would always be adjacent to the LO register. With
VBIF 2.0, this assumption has now been broken, so both the
HI and LO register offsets must be returned.

Conflicts:

        drivers/gpu/msm/a4xx_reg.h
        drivers/gpu/msm/adreno_a3xx.c
        drivers/gpu/msm/adreno_a4xx.c
        include/uapi/linux/msm_kgsl.h

CRs-fixed: 578771
Change-Id: Ie74da5d797e58a143b89a61aba7ebaf1ed42ed5e
Signed-off-by: Kevin Matlage <kmatlage@codeaurora.org>
msm: kgsl: Add a timer to detect possible syncpoint deadlocks

Use timers to detect when a syncpoint stays active for too long. This
won't avoid deadlocks but it sure will make it easier to debug them.

Conflicts:
        drivers/gpu/msm/kgsl_device.h

Change-Id: Ic0dedbadde07abcc1d39563c3b141591cbf2f634
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Avoid a deadlock with the cmdbatch timer

Don't take the cmdbatch lock while deleting the cmdbatch timer.
This avoids a deadlock where del_timer_sync() waits for the timer
function which needs to take the lock.

CRs-Fixed: 644944
Change-Id: Iaeb5a26b67e96e8e748fb9ff8c26fa8b0f2c3827
Signed-off-by: Lynus Vaz <lvaz@codeaurora.org>