msm: kgsl: Add CP_WAIT_FOR_IDLE packet before updating timestamp

HW workaround to resolve MMU pagefault caused by memory
freeing early before GPU completes it.

This is race condition where timestamp is getting updated
and the event handler runs before the GPU actually got done
with the timestamp, hence freeing the buffer early while GPU
still not completed with it.

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

Change-Id: I7db96e3cd1c4497986faa3cfc222be0af6213309
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Add cache flush for A20x

Adding extra cache flush command to make sure that CP block
gets event from RB all the time to avoid race conditions because of
clock switches. this is to improve stability on 8x25 target.

Change-Id: Ic7b56223eb0849e3ea955af3e08132431fc5318a
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Always reset global ts_cmp_enable on GPU interrupts

Always rest the ts_cmp_enable when an interrupt is received
from the GPU.  This keeps legacy code that is not using
per context timestamps correctly updated.  No effect is
seen with mainline code using per context timestamps.

CRs-fixed: 418172
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
(cherry picked from commit 95b0011ae56577e84d2912ae7d600ae62c08c2e4)

Change-Id: Id11bd4c7adfd5a396e01f508c383a62adab3d544
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Add max and min power level controls in sysfs

Add min_pwrlevel, max_pwrlevel and thermal_pwrlevel to give a
privileged user more control over which power levels are considered
during DCVS power management.  max_pwrlevel is the maximum power
level that the system can go to at any time, min_pwrlevel represents
the lowest power level.  DCVS will chose any level between these
two extremes.  thermal_pwrlevel allows a daemon to set an absolute
top for frequency to prevent thermal issues. The effective maximum
power level is considered to be the lower of thermal or max.
Also added is num_pwrlevels that shows the number of active power
levels. This corresponds to the gpu frequencies in
available_gpu_frequencies.

Change-Id: Ifc55256a6fec96ba1fbf8fd6c0fa1948173283b8
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Set requested power state to NONE after resume

When GPU resumes from suspend requested state is not cleared to NONE.
Due to this idle reporting is not done and GPU stays at MAX freq. As
a fix if the current power state is already ACTIVE and request is
made for active power state then clear the requested state immediately.

CRs-fixed: 424682
Change-Id: I7f0d7fa819308f166cbbbf30b2c20aee73644cfb
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
Revert "msm: kgsl: Always reset global ts_cmp_enable on GPU interrupts"

This reverts commit 57d99a565b21120f1d4a863057ddfb52ad458031.

Change-Id: Ib54ccc365b8bb5213040ca432dbe49602fe8fe0e
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Log retired timestamps on device wake

Knowing the retired timestamp on wake-up is useful to debug hangs that
are caused due to clock instability. When we request for clock ON, the
clock may/may-not be turned ON, but we go ahead assuming it is ON. This
can be caught by printing the timestamp on wake. The timestamp we print,
can be compared against the timestamp in postmortem dump (assuming a
hang happens).

If there is a difference, we can say that GPU has run for sometime after
wake. So, we needn't suspect clocks.

If there is no difference, we can say that GPU has hung immediately after
wake. So, we can positively suspect the clocks.

Signed-off-by: Ranjhith Kalisamy <ranjhith@codeaurora.org>
(cherry picked from commit d0348f1644a21ae73982f94c442bdf9f17584ebe)

Change-Id: Ib0f5dc37d07ee2bb6b0ed55de8230ab1c33f092e
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
(cherry picked from commit 0184185f274762462738621282dc50815313339d)

Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Add power settings to the postmortem dump

The Nap & StartStop-on-SleepWake config information will help to analyze
hangs due to incorrect power state transitions.

Change-Id: I0e2d6244236cf5e580251c170dd5d105e096c6e2
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Dump Clock registers in Postmortem Dump

Clock registers hold critical information of clock state that is
crucial for hang analysis. Dump this information as part of postmortem
dump.

Change-Id: I210bdd087e1844d1485caa1712239fdcb4f38982
Signed-off-by: Ranjhith Kalisamy <ranjhith@codeaurora.org>
(cherry picked from commit 4c76100e1789c60a74d3816c092e619659b3223e)

Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Always reset global ts_cmp_enable on GPU interrupts

Always rest the ts_cmp_enable when an interrupt is received
from the GPU.  This keeps legacy code that is not using
per context timestamps correctly updated.  No effect is
seen with mainline code using per context timestamps.

CRs-fixed: 418172
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
(cherry picked from commit 95b0011ae56577e84d2912ae7d600ae62c08c2e4)

Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
(cherry picked from commit 244b7ba49214d720d09247e67eaaaac0abe63207)

Change-Id: If1f9636d5fd617bf94b714aa2315eb65ee45fb82
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Fix conditional GPU interrupts to fire only for waiting context

Conditional interrupts were not getting fired at the correct time when
using per context timestamps.  When a context was waiting on a timestamp,
the interrupt was being fired on the global timestamp rather than the per
context timestamp.  Now the interrupt is fired on the per context
timestamp.

Change-Id: Ib683c846f0639a1e44f5b39bb81ca2751b306ea1
CRs-Fixed: 340620
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>

Conflicts:

	drivers/gpu/msm/adreno_ringbuffer.c

Signed-off-by: Rajesh Kemisetti <rajeshk@codeaurora.org>
msm: kgsl: Make the GPU device aware of the next pending event

The adreno core needs to know what the next event pending for
any given context is so it can mark the interupt to be fired.
If this isn't done then some timestamps that don't have a
matching waittimestamp call won't fire an interrupt. This is
dangerous on the last interrupt/event before a context goes
away.

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

Conflicts:

	drivers/gpu/msm/adreno.c
	drivers/gpu/msm/kgsl_device.h

Signed-off-by: Rajesh Kemisetti <rajeshk@codeaurora.org>
msm: kgsl: Set a useful pm_qos default value

Also allow the value to be updated via sysfs.

CRs-Fixed: 434556
Change-Id: Ia69cafd44f48895b9d71e184efdb3cd253cec75b
Signed-off-by: Lucille Sylvester <lsylvest@codeaurora.org>

Conflicts:

	drivers/gpu/msm/kgsl_pwrctrl.c

Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Restore the value of RB_BC_CONTROL on context switch

On 8x25Q, when GPU footswitch is turned off during TCXO shutdown, the
RB_BC_CONTROL register value is lost which causes GPU hang after coming
out of TCXO shutdown. Now the value of RB_BC_CONTROL is preserved properly
during context save and restore.

Change-Id: I20fe3cb2565e306a589765460339990ec149fd01
CRs-fixed: 441134
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Calculate busy interval considering GPU freq as float

GPU freq could be non-integral value and treating it as an integer
to calculate GPU busy insterval leads to higher busy interval
and more often we see negative idle stats. This change caculates
busy interval by treating gpu freq to the correct precision.

Change-Id: Ic54130803d7a0436b4ba31b09915eb7165d16590
CRs-fixed: 454972
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Start Busy Timer before resetting the busy counter

When GPU is 100% busy, starting the busy counter before the begining
of next busy start makes the bus interval exceed total time. Another
possibility is that we started the busy_start_time for the next sample
but since we reset the perfmon control rtegister after sometime, when
we stop the busy_start_time for the current submission, the GPU busy
ticks is still. These two conditions could result in negative idle
stats and cause Busy timer expiry.

The change ensures that we start busy timer first and then reset the
perfmon control registers, so that total time is not less than busy
interval. Also add some delta of 100 microsec to complensate for the
delay in starting the two clocks.

Change-Id: Id7f4cb20916b39740ae88849bf56857b821ec12d
CRs-fixed: 460011
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Don't change the sglen of the memdesc if allocation fails

If the page_alloc loop fails, we changed the length of the scatterlist
for the memdesc to the last good page so we could have an efficient
loop through kgsl_sharedmem_free.  Unfortunately, we were also using
the sglen to decide between using kmem or vmalloc memory for the sg
list in the first place. There is a chance that one could start out
with a large sg list from vmalloc, but if it failed early, then we
could truncate the size and attempt to kfree it.

Zero the stlist at init time and use a length == 0 as an indicator
that the list might be truncated.

Change-Id: Ic0dedbadc0007b5e3c338151517c95999ed523a3
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Properly record the size of the sglist in the memdesc

It was possible to fail out of kgsl_sharedmem_alloc after a sglist was
allocated but before we saved the size of the allocation which caused
problems when the sglist was freed during cleanup.

CRs-fixed: 464289
Change-Id: I597c671b7a69d2b7930d426884cdfb6799714763
Signed-off-by: Vinay Roy <vroy@codeaurora.org>
msm: kgsl: Set sysfs max clock for devices with no power scale policy

When the sysfs entry for max clock is updated, set it immediately for the
devices that has no power scale policy. This prevents the device from
running with undesired clock.

(cherry picked from commit 6ca8011b4feb30c29e8e6fb5b3571fd2314879e0)

Change-Id: I3a4d689d90036edc243fa97562a79394ace94c2a
CRs-fixed: 408963
Signed-off-by: Ranjhith Kalisamy <ranjhith@codeaurora.org>
msm: kgsl: Hold the pm_qos vote at all times when ACTIVE

The GPU needs access to the L2 cache while running.  If the
CPU is not also active using the cache can lead to stability
issues.  Keep the CPU out of idle power collapse at all times
while the GPU is running.

CRs-Fixed: 397489

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

Conflicts:
	drivers/gpu/msm/kgsl_pwrctrl.c