EHCI: HSIC: Fix race between HSIC runtime resume and remote wakeup

HSIC runtime resume initiated by an interface driver and remote wakeup
gpio interrupt handler can run in parallel. If HSIC runtime resume is
triggered by runtime PM core, pm_runtime_get call will fail in wakeup
interrupt handler.  Check the return code and decrement the PM usage
counter.

(cherry picked from commit 968cf27ef604a71d98f1a7b4a5c4369892cd10dc)

Change-Id: Ic0cd744027c4db9f6c9205b3e10b973626e4c74c
CRs-Fixed: 403708
Signed-off-by: Pavankumar Kondeti <pkondeti@codeaurora.org>
sched_avg: add run queue averaging

Add code to calculate the run queue depth of a cpu and iowait
depth of the cpu.

The scheduler calls in to sched_update_nr_prod whenever there
is a runqueue change. This function maintains the runqueue average
and the iowait of that cpu in that time interval.

Whoever wants to know the runqueue average is expected to call
sched_get_nr_running_avg periodically to get the accumulated
runqueue and iowait averages for all the cpus.

Signed-off-by: Jeff Ohlstein <johlstei@codeaurora.org>
(cherry picked from commit 0299fcaaad80e2c0ac9aa583c95107f6edc27750)

Change-Id: I64dfabb1d154be5e661e1650e3a2e9afb4a0bec9
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: update the event enum in preparation for mpdecision

The event numbers used to communicate with TZ have changed. Update
them here in preparation of mpdecision.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 0db555f69e8e5b5237f2c053fffeeec3926084a1)

Change-Id: I135b7dd1a96757260b226e3bae0aca456546913a
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: mpdecision: Add mpdecision driver code

This driver is mainly responsible to convey the runqueue average and
iowait average values to the TZ periodically. The TZ returns the mask
of the cpu's that should be online and the slack timer.

The mpdecision code brings cpus up or down to match the online mask
from TZ. Also starts the slack timer as specified by TZ.

Upon expiry of the slack timer mpdecision calls TZ to get a new
mask and a new slack timer.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit a599de41a47b0b1a9f81a77227e16aff5a16a072)

Change-Id: I8a7c3c24fd1efda494573742b034ecac77e2cacd
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: 8064: add board file structures for mpdecision and msm governor

Add device structures in the board file for mpdecision and the msm
governor. This essentially probes mpdecision driver and the msm
governor driver. When enabled mpdecision communicates the run queue
average with TZ and obtains back a mask of which cpus should be
online. The msm governor communicates the frequencies of the cpus
with TZ and obtains newer frequency for that cpu.

Both combined they govern which cpus stay online and how fast they
run.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit fb4a2f8e62af9cfa0852b16b2370f0d3df324e59)

Change-Id: I26c858e8872e685d4f2f699f210ebd65b7d1ac16
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: rearrange platform data

This change
-removes the use of group id and instead introduces core type
-rearranges platform data, adds energy curve coefficients and power
 parameters
-allow for the energy params to be -ve numbers

The change also mandates updates to the msm8974-gpu.dtsi and the
associated binding documentation.

Also take this opportunity to remove devices for unsupported platform
- 8930 and 8960

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 4445166ad16be0c45b077bfb10487de355ed2e05)

Change-Id: Ib4b6a62bb2aee548a81dc669552c2519fff37348
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: Add thermal interfaces

The algorithm needs thermal inputs for all the cores. Create members in
the internal core_info strucutre and platform data/device tree to pass
in the sensors they use.

Update the dcvs code to notify the temperature to TZ.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit fc7dca4c325725492af997fa282e07b9d03154d1)

Change-Id: If57cbaabd1f3ce31ab11cd260d0c51134577c371
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
cpufreq: msm: remove gov_mutex from stop

The stop could end up calling set_frequency callback. The set_frequency
callback takes the same gov_mutex lock and we end up in a deadlock.

Fix this by removing the locks around stop.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 5106c477a747279dbf045eed5a6ab4f68ac3501c)

Change-Id: I4fbfa8aed8294cdda2c7253ef9cd1da05b852c60
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
cpufreq: msm: fix race in cpufreq

__cpufreq_driver_target expects to be called with the rw semaphore
held. We are not doing this when the frequency is asked to be changed
by dcvs.

Use cpufreq_driver_target variant of that function which gets the
rw semaphore before setting the frequency.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 54fc08ea6d33c6c561167f369cba060fe33755c9)

Change-Id: I1a5d8c3aaf508904c86f46827f51c975b841e997
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: hide -13 errors when communicating with the secure side

-13 errors from the secure side are benign, dont print an error when
that happens.

Signed-off-by: Jeff Ohlstein <johlstei@codeaurora.org>
(cherry picked from commit 3cb914d56d65b9ad1dea8d186f74d3a1bc760769)

Change-Id: I4e3df19f01effe86a0e404d75345d84216b07c67
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: prevent thread creations upon cpu hotplug insertions

Numerous threads were seen created and left idle as cpu's were
hotplugged in and out. Avoid creating new threads unnecessarily.

Instead create just one thread when a core is registered and use
it later.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit e039aac0c0be7c0d914055224103e8f41821320d)

Change-Id: I9d6105f4a14fadb32b3cae5e9a8b46065ab30e81
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: get rid of the pending freq array

Only one pending frequency should be set, there is no need to
maintain an array of intermediate requests.

Cleanup the code and remove new_freq[] array and the index to
it. Introduce pending_freq a single entity that carries the
latest frequency request. Use the core's cpu_lock to protect
it.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit bea1b7628356bd953a88596c556373a6ec355f94)

Change-Id: I0e3412c8aaccc4d90160662e3194830afbecd3e8
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: use wait_q

It is cleaner to use wait_q mechanism instead of waking up the thread
and managing the scheduling flag ourselves.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 33bf417160e9340f2dd5f9f275e7e6021987bfe1)

Change-Id: I3977cf17b1da744a0fea6b79ec94e86b4e242248
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: update dcvs if the governor limits change

We had seen issues where dcvs goes out of sync with the actual
freq the cpu is running at. The root cause was if the userspace
changes the limits on the governor, the governor ends up changing
the frequency without notifying dcvs.

Provide an api for the governor to call dcvs when a frequency change
happens.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 68c970e38fcb700d851301dbacc56da9387294c6)

Change-Id: Ib55f1f2061c385bb68281263a2bfcc9c20e4cf39
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: provide frequency set/get callbacks at registration

There is no need to register a separate structure for setting and
getting frequency.

Simply pass function pointers to set and get callbacks when a
core is registered.

While at it rename the msm_dcvs_freq_sink_register/unregister to
msm_dcvs_freq_sink_start/stop to better reflect that those
apis are meant to do.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 69134111c2a24002d1995a05c99f84403ac6a7e8)

Change-Id: I1c28320dab8a64bffadc6ecad6c3ff40aeb5782f
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: remove idle notification registration

Register the idle enable callback along with the core. The code
becomes cleaner and easy to update.

Importantly, the msm_dcvs_idle driver becomes useless. Remove it
and instead let the msm governor handle idle enabling and disabling.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit c1ed66c9035b4fbf240e46837d86a9a6442531f1)

Change-Id: I9bd72dc6ee6d08e800ff2b403697dd8673219289
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: split get_core in add/get_core

There is currently only one function to add and get a core. Clean this
up by splitting that in two functions one for adding core and the other
for getting one.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit fbd7ea63accf3624f2c80d4a638495817b634d3a)

Change-Id: I45962fe0ed879bcf8f51f55cbbcd1b2c7ad46e25
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: remove core name

Currently core_name is used to identify which core the dcvs operates on.
Instead use a type and the type num while registration with dcvs and
return an id (dcvs_core_id) upon successfull registration.

The dcvs_core_id is used by the clients of msm_dcvs to call upon its
apis viz. freq_start, freq_stop, msm_dcvs_idle etc.

The dcvs inturn uses the type num passed in at registration time to
invoke apis on the clients viz. set_freq, get_freq, idle_enable.

This further cleans up the internal dcvs add_core and get_core
implementation. One need not pass around the core_name and use the type
instead.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 1bbc0321a6871e018c17da6f244b9df442faead6)

Change-Id: I143b1290ad1d88c048564474b35f030cb2ff92bc
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: cleanup debug prints

The debug print mechanism is not much useful. Remove that code.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit 0e1679b1ed8419f44c8976853f0070517f84cb14)

Change-Id: Ie039b95cb5b03f9a66dae09ca4c0190e0950d9fe
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>
msm: dcvs: functions to start/stop/restart hrtimer

The slack timer is supposed to be activated only when the core
is out of idle and that the governor for that core has started.
If any of the conditions is not met one should not activate
the slack timer.

To implement such a scheme, create two variables idle_entered,
and pending freq protected by their respective spinlocks
idle_state change_lock and pending_freq_lock. Whenever the
idle is entered or exited stop/start_slack_timer is called
which grabs the appropriate spinlocks and udpates the idle_entered
variable and starts and stops the hrtimer.

Moreover there are situations when a activated timer needs to
be restarted. This happens when a frequency is updated and a new
slack timer is received. We provide restart_slack_timer() callback
which cancels the hrtimer and restarts it only if the conditions
mentioned above are met.

There is also a situation when one has to forcefully cancel the timer
when the governor stops. Provide an api for that too. Note that one
cannot use stop_slack_timer becuase stop_slack_timer manipulates
idle_entered flag.

Also on related note, cleanup the code that handles pending_freq.
We get a new freq to set for a core when
- the slack timer expires
- when the governor starts when the cpu is brought back online
- when the cpu comes out of idle

Use pending_freq_lock to serialize updates to pending_freq.

Signed-off-by: Abhijeet Dharmapurikar <adharmap@codeaurora.org>
(cherry picked from commit d6911b9e6d9cf51a8a7acc7363b424e325658e1f)

Change-Id: I1a541b9c9ecb857b3e2e038bedf722d19dd410dd
Signed-off-by: Ram Kumar Chakravarthy Chebathini <rcheba@codeaurora.org>