msm: cpr-regulator: change sequence for handling CPR voltage over ceiling

The current CPR driver increases the UP threshold to its maximum value when
the ceiling voltage is hit, but it is not disabling the UP interrupt
because the CPR controller needs a NACK command from software if the new UP
threshold is crossed for any reason. Based on analysis of CPR hardware
behavior, disabling CPR UP interrupt when CPR hits the ceiling voltage
is not harmful even though the CPR controller waits for a response from
software indefinitely. The CPR controller restarts sampling again as soon
as software switches to new voltage corner if it had been halted due to
hitting the ceiling voltage.

Modify the cpr-regulator driver so that the CPR UP interrupt is disabled
for a given voltage corner when the ceiling voltage for that corner is
reached. Re-enable the UP interrupt for a corner if a DOWN interrupt is
ever received for the same corner.

CRs-fixed: 602095
Change-Id: I0f2c479464cc43f784e97422095df395fa0c7b47
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: add support for increasing cpr quotient value

Add support for the specification of a fixed offset value to be added to
the CPR target quotient for each voltage corner. This can be used to
increase the static voltage margin of a supply managed by a CPR controller

CRs-fixed: 605127

Change-Id: I0e8f71b35b8ea3bb1724d6e01944015d5fbcb045
Signed-off-by: Ke Liu <keliu@codeaurora.org>
Signed-off-by: Manish Kumar <manishku@codeaurora.org>
ARM: dts: msm: change CPR down threshold and quot adjustment for msm8926

The current VDD_APC CPR down thresdhold value of 1 makes the CPR controller
too sensitive to voltage ripple in the VDD_APC supply. This results in the
CPR UP and DOWN interrupts firing repeatedly in an alternating fashion.

Change the CPR down threshold to 2 and also change the CPR turbo and
nominal voltage corner target quotient adjustment values to 72 in order to
eliminate the undesirable CPR interrupt behavior.

CRs-fixed: 607472

Change-Id: I89b5c84c44c2c297f34023b177122441c65161a3
Signed-off-by: Ke Liu <keliu@codeaurora.org>
Signed-off-by: Manish Kumar <manishku@codeaurora.org>
ARM: dts: msm: optimize CPR configurations for msm8226-v1/v2 and msm8926

Move common CPR regulator configurations for msm8226-v1, msm8226-v2, and
msm8926 into msm8226-regulator.dtsi. Move msm8226-v1 specific
configurations into msm8226-v1.dtsi since most CPR configuration parameters
are common between msm8226-v2 and msm8926.

Change-Id: I043a310aa433ba8f8cf4dd17dc1ef99de1f99b89
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: modify how initial voltages and limits are specified

Currently, the initial voltage as well as the CPR ceiling and floor
voltages for each voltage corner are defined using these four device tree
properties: qcom,pvs-init-voltage, qcom,pvs-corner-ceiling-slow,
qcom,pvs-corner-ceiling-nom, and qcom,pvs-corner-ceiling-fast.
Unfortunately, since qcom,pvs-init-voltage only defines the initial
voltage for the turbo voltage corner for each PVS bin value, the initial
voltage used for the SVS and nominal corners must be calculated using a
complicated and potentially error prone algorithm using the four properties
mentioned earlier. Furthermore, the coarse slow/nom/fast binning leads to
wasted power for parts with intermediate PVS bin defined initial voltage
requirements when the CPR controller is disabled.

Therefore, replace the four properties listed above with the following
three properties: qcom,pvs-voltage-table, qcom,cpr-voltage-ceiling and
qcom,cpr-voltage-floor. The property qcom,pvs-voltage-table specifies a
mapping from a PVS bin value to the initial voltage for each voltage
corner. The properties qcom,cpr-voltage-ceiling and qcom,cpr-voltage-floor
specify the ceiling and floor for each voltage corner respectively.

Also, remove all usage of 'process' from inside of the cpr-regulator driver
since it has no meaning without the qcom,pvs-corner-ceiling-slow/nom/fast
properties.

CRs-Fixed: 606213
Change-Id: I7996074b39e1f80f6dc10e25428e09d7cb364192
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: remove register save and restore for suspend and resume

As CPR hardware register values will be retained during XO shutdown with
VDD_CX minimization, there is no need to save and restore CPR hardware
register values during suspend and resume. Also, there is a race condition
between cpr_resume and cpr_regulator_set_voltage. The
cpr_regulator_set_voltage function might be called after cpr_suspend is
invoked and before cpr_resume is called, this will enable CPR during XO
shutdown.

Hence remove register save and restore operations from cpr_suspend and
cpr_resume functions and add a flag to indicate that CPR has been
suspended.

Change-Id: Ic7c0f639e7e5cedb096d2f2eaaa3e8433ccd44c7
CRs-Fixed: 616385
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: change the sequence for handling corner switch

Clear all of the CPR control and configuration registers before switching
to a new voltage corner and then restore their values after corner
switching is complete. This sequence ensures that spurious up or down
interrupts are not received when CPR is re-enabled after corner switching
is complete.

Change-Id: I646c747c9bfa7474bd421074dc2f6e2b28ef347c
CRs-Fixed: 604782
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: Improve CPR interrupt handling

disable_irq() is not an efficient call and could cause potential deadlock.
Improve the interrupt handling by removing disable_irq and enable_irq
completely, and instead, checking the state of the interrupt source
(LOOP_EN and BUSY bits) in ISR to bail out immediately if any condition is
not met.

CRs-Fixed: 630182
Change-Id: Ieedadf4f0db4febc41ab85f383af683bdaf34615
Signed-off-by: Tirupathi Reddy <tirupath@codeaurora.org>
Signed-off-by: Ke Liu <keliu@codeaurora.org>
msm: cpr-regulator: calculate quotient adjustment dynamically

Replace the fixed quotient adjustment algorithm with a new algorithm
that calculates quotient adjustments dynamically.

Calculate the quotient scaling factor according to fused target
quotients and application processor clock frequencies. A different
quotient adjustment value is applied to each voltage corner.

The formula for calculating the quotient adjustment value for each
voltage corner is:
quot_adjust = (freq_turbo_MHZ - freq_corner_MHZ) * scaling / 1000
where the "scaling" is determined using this equation:
scaling = min(max_scaling_factor,
1000 * (QUOT(fused_turbo) - QUOT(fused_norm)) /
(Freq_turbo_MHZ - Freq_normal_MHZ)).

In msm8x26 cpr-regulator device tree node, add a corner to frequency
mapping table, a speedbin + PVS version to max SVS/normal/turbo fuse
corner mapping table, a maximum quotient adjustment scaling factor,and
PVS version eFuse information. With the frequency to corner mapping
table specified in msm8x26 clock-a7 device tree node, this ensures that
1.6G/1.8G msm8226v2 and msm8926 parts receive power saving when running
at turbo frequencies below the maximum possible turbo frequency.

CRs-Fixed: 597721
Change-Id: Ideecccc2f81762bed3668cfdf27d942c00df76ea
Signed-off-by: Wu Fenglin <fenglinw@codeaurora.org>
Signed-off-by: Ke Liu <keliu@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: Change the pagefault policy immediately

When changing the pagefault policy the MMU setting for the pagefault
policy was not changed, add code to do that.

Conflicts:
	drivers/gpu/msm/adreno.c

Change-Id: I3b185a7f6bdda42856c777c60ae82c32b9c52eb9
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
Signed-off-by: Hareesh Gundu <hareeshg@codeaurora.org>
msm: kgsl: protect syncpoint events with a reference count

Replace the event spinlock with krefs to avoid the possiblity
of calling a sleeping function while holding the spinlock and
to generally make the whole cmdbatch cancel loop a lot more
stable.

Change-Id: I08ba54fa4c5a11c05e3503dfc6a4ee040733e5a6
Signed-off-by: Jeff Boody <jboody@codeaurora.org>
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
msm: kgsl: Don't block on detach if the context is invalid

An invalidated context means that something pretty nasty happened and
likely there was an unrecovered GPU fault along the way. If this is
the case then the GPU is likely not processing commands for the context
anymore. Don't block for the last retired global if the context was
invalidated.

Change-Id: Ic0dedbad22a22b704baf14557d7e18cc4cf1044a
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
gpu: ion: fix use-after-free in ion_heap_freelist_drain

The `buffer' variable is being used after being freed. Fix this.

Change-Id: Iea3471fa7dc7535bbf0620c1639fea2008d7cf19
Signed-off-by: Mitchel Humpherys <mitchelh@codeaurora.org>
msm: kgsl: Treat 0x0 as valid GPU pagetable physical address

Iommu driver could be allocating a valid GPU pagetable at
physical address 0x0, remove assumption from GPU kernel driver
that 0x0 is invalid pagetable address.

Change-Id: I213c0fbc1d90a13bef83c7d7589ad9e5729771fd
Signed-off-by: Tarun Karra <tkarra@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: Do not detect faults when GPU core is idle

Do not detect GPU faults when GPU hardware status is idle,
this leads to incorrect fault detection.

Conflicts:
	drivers/gpu/msm/adreno.h

Change-Id: I7a9f8c49eb819241698f99fd50aaed400aadd4f4
Signed-off-by: Tarun Karra <tkarra@codeaurora.org>
Signed-off-by: Hareesh Gundu <hareeshg@codeaurora.org>
ion: invalidate buffers before zero'ing

Currently, we invalidate the cache for buffers being zero'd *after* we
zero them. However, this is racey since a dirty cache line from an
earlier mapping could be written out to memory between the zero'ing and
the invalidation. Fix this by invalidating the cache *before* zero'ing
the buffer.

Before this change, I was seeing corruption with:

    $ cat <<EOF | ./memory_prof -e -i -
    simple_alloc,1,ION_SYSTEM_HEAP_ID,ION_FLAG_CACHED,0x100000,1MB
    simple_basic_sanity,1
    simple_profile,1
    simple_free,1
    EOF

After this change, no corruption.

CRs-Fixed: 585524
Change-Id: I1f2603604efc9be4ebf9112b06728e0aeca60ea0
Signed-off-by: Mitchel Humpherys <mitchelh@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>
cpufreq: cpu-boost: Fix queue_delayed_work_on() race with hotplug

Calling queue_delayed_work_on() on a CPU that's in the process of getting
hotplugged out can result in that CPU infinitely looping in
msm_pm_wait_cpu_shutdown(). If queue_delayed_work_on() is called after the
CPU is hotplugged out, it could wake up the CPU without going through the
hotplug path and cause instability. To avoid this, make sure the CPU is and
stays online while queuing a work on it.

Change-Id: I1b4aae3db803e476b1a7676d08f495c1f38bb154
Signed-off-by: Saravana Kannan <skannan@codeaurora.org>