cpuquiet: Update averaging of nr_runnables

Doing a Exponential moving average per nr_running++/-- does not
guarantee a fixed sample rate which induces errors if there are lots of
threads being enqueued/dequeued from the rq (Linpack mt). Instead of
keeping track of the avg, the scheduler now keeps track of the integral
of nr_running and allows the readers to perform filtering on top.

Implemented a proper exponential moving average for the runnables
governor and a straight 100ms average for the balanced governor. Tweaked
the thresholds for the runnables governor to minimize latency. Also,
decreased sample_rate for the runnables governor to the absolute minimum
of 10msecs.

Updated to K3.4

Change-Id: Ia25bf8baf2a1a015ba188b2c06e551e89b16c5f8
Signed-off-by: Sai Charan Gurrappadi <sgurrappadi@nvidia.com>
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/131147
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>

Conflicts:
	drivers/cpuquiet/governors/balanced.c
	drivers/cpuquiet/governors/runnable_threads.c
cpuquiet: balanced governor requires cpufreq

The balanced governor relies on cpufreq for some of its decisions. Fail
initialization if there are no cpufreq OPP tables available.

Change-Id: I5d64215d56f50d5df52c1433208b1eb40440c812
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/131437
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>

Rebase-Id: R03bb471218a088a816a67232861759cba2b91f38

Conflicts:
	drivers/cpuquiet/governors/balanced.c
cpuquiet: initialize runnable threads first as the default governor

Add the necessary code to initialize the runnable threads governor first when
selected as the default governor.

Change-Id: Iebbf304f3934b429d8a217011b3cb45ecddfd558
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/131438
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>

Rebase-Id: Rb5009930e554648a17d78edf9d8bdd17199c9c6a
cpuquiet: Update runnable gov param 20/100ms (1.25/2.25/2.5 +.5)

Sampling interval: 20ms
Window size: 100ms

Thresholds: 1.25/2.25/2.5 thread
Hysteresis: .5 thread

Bug 1050445

Change-Id: I5138bed06e6f3548ec440d2b9ee17460fc37adfb
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/163105
Reviewed-by: Automatic_Commit_Validation_User
GVS: Gerrit_Virtual_Submit
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
cpuquiet: Make stats optional

Make the per CPU statistics a configureable option.

bug 1049625

Change-Id: I28b248bbbad89a19e8d8d69400733dfd4a2cf052
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/168749
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>

Conflicts:
	drivers/cpuquiet/driver.c
cpuquiet: Fix race in runnable governor

Fixed a race where the cpuquiet driver would call device_free() before
the governor was started, which would lead to a kernel panic because
runnables_timer was not initialized.

Introduced RUNNING state, so states are:
- DISABLED: truly disabled
- IDLE: enabled, but not polling upon request from the driver
- RUNNING: actively polling

Bug 1189042

Change-Id: I45b9ce40e61e1cfddde74ff7b2691722204045bb
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/168803
Reviewed-by: Sang-Hun Lee <sanlee@nvidia.com>
Tested-by: Sang-Hun Lee <sanlee@nvidia.com>
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
cpuquiet: Fix warning about unused variable

drivers/cpuquiet/governors/runnable_threads.c: In function 'runnables_work_func':
drivers/cpuquiet/governors/runnable_threads.c:184:14: warning: unused variable 'state' [-Wunused-variable]

Change-Id: I26c2288876043cf3dd21e1b57317a1f5b2de5a9b
Signed-off-by: Sami Liedes <sliedes@nvidia.com>
Reviewed-on: http://git-master/r/197469
Reviewed-by: Automatic_Commit_Validation_User
GVS: Gerrit_Virtual_Submit
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>
cpuquiet: Remove synchronization from runnables_work_func()

runnables_stop() can deadlock when cancel_work_sync() waits for the
work function to end and the work function blocks on the same lock
held by runnables_stop().

Removing the locks from runnables_work_func() fixes this. This should
be safe because runnables_lock protects runnables_state and
runnables_work_func() only reads runnables_state. Also, the functions
that change state to DISABLED do a cancel_work_sync() to guarantee the
work function stopped running.

Bug 1215668

Change-Id: I70617b3b0fc81db8555869e67e3b11652af8d94c
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/193881
Reviewed-by: Mandar Padmawar <mpadmawar@nvidia.com>
Tested-by: Mandar Padmawar <mpadmawar@nvidia.com>
ARM: tegra: cpuquiet: Fix cpuquiet notifiers

The notifiers now properly fire on every cluster switch

Change-Id: I381301cf62f25b49532326cc7759696c7f6797b7
Signed-off-by: Sai Charan Gurrappadi <sgurrappadi@nvidia.com>
Reviewed-on: http://git-master/r/118376
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>

Rebase-Id: Rd93d027e02b4f1122becef42916b28e652c76980

Conflicts:
	arch/arm/mach-tegra/cpuquiet.c
ARM: tegra: Ignore PmQos when disabled

The cpu hotplug drivers should only bother updating state or switching
to the G cluster if the driver is not disabled

Bug 1032096

Change-Id: Ice938b094bbf48fef47afa3915b1a48362fb256d
Signed-off-by: Sai Charan Gurrappadi <sgurrappadi@nvidia.com>
Reviewed-on: http://git-master/r/122859
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Aleksandr Frid <afrid@nvidia.com>
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>

Rebase-Id: R8840db867a4b54be73d296dd26c9935f4112aeee
cpuquiet: Fix cpuquiet for k3.4

Change-Id: I75d8dba6c9c4c43152c819e08beeccb1e3c07c0c
Signed-off-by: Sai Charan Gurrappadi <sgurrappadi@nvidia.com>
Reviewed-on: http://git-master/r/123850
Reviewed-by: Varun Colbert <vcolbert@nvidia.com>
Tested-by: Varun Colbert <vcolbert@nvidia.com>

Rebase-Id: R06aaf7d4745c3b989c1329115df3dd804b0b35a9
cpuquiet: Add notifiers for the runnables governor

The governor stops sampling whenever the device is busy and starts its
sampling loop when the device is free.

Change-Id: Ifdae4a84e56c2734be48e49f6112b537602a02da
Signed-off-by: Sai Charan Gurrappadi <sgurrappadi@nvidia.com>
Reviewed-on: http://git-master/r/124680
Reviewed-by: Varun Colbert <vcolbert@nvidia.com>
Tested-by: Varun Colbert <vcolbert@nvidia.com>

Rebase-Id: R5caf7ff378d963d090369e0c0b65f8328234de3e
ARM: tegra: cpuquiet: Bring back hotplug stats support

Hotplug stats are needed for dfs_log and Power Signature. Cpuquiet
provides similar stats in the core layer, but cannot provide stats for
LP cpu, since this is only visible at the driver level.

Bug 1045785

Change-Id: I6c5c26912d1a26637e81c73741637fbd2bee6157
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/131210
Reviewed-by: Automatic_Commit_Validation_User
Tested-by: Shaoming Feng <shaomingf@nvidia.com>
Reviewed-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
GVS: Gerrit_Virtual_Submit

Rebase-Id: R8e578267939d0bcde96d4851fb46b44da78baf28
ARM: tegra: cpuquiet: add min/max_cpus debugfs nodes back

Bug 1047573

Change-Id: I7209ca87075b3d8a1d53141c48fe8fbdd86c4728
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/131606
Reviewed-by: Automatic_Commit_Validation_User
Tested-by: Ilan Aelion <iaelion@nvidia.com>
GVS: Gerrit_Virtual_Submit
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>

Rebase-Id: R81f2ceacae7865e1941c3eb7fb3950a9a0195f5c
ARM: tegra: cpuquiet: Bring back hotplug stats support

Hotplug stats are needed for dfs_log and Power Signature. Cpuquiet
provides similar stats in the core layer, but cannot provide stats for
LP cpu, since this is only visible at the driver level.

Bug 1045785

(cherry picked from commit ec6f32c8c39b574a6d01596a8d31060e47d20905)
Reviewed-on: http://git-master/r/134271

Change-Id: I808c5b9597d32d7297257d4cf8767aa0f9ab0473
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Signed-off-by: Deepak Nibade <dnibade@nvidia.com>
Reviewed-on: http://git-master/r/143137
Reviewed-by: Rohan Somvanshi <rsomvanshi@nvidia.com>
Tested-by: Rohan Somvanshi <rsomvanshi@nvidia.com>

Rebase-Id: R6dc5d809303f53eb71031c8c947c754426db4889
ARM: tegra: refactor cpuquiet driver

Refactor the cpuquiet driver to use 1 workqueue which handles the
clusterswitch and cpu online/offline operations.

Functional changes compared to the previous driver:

+ changing no_lp also triggers a clusterswitch if the current cluster is the
  LP cluster.

+ removing all QoS constraints will return to the configuration as defined by
  the governor.

+ the minimum CPUs online QoS constraint will always prevail above the maximum
  CPUs online constraint. If both constraints are set, the maximum CPUs online
  constraint will be clamped to the minimum constraint.

bug 1047545
bug 1055406

Change-Id: Ib52de2578254c282f23f972484a6532d24fdc0e9
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/134391
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
Reviewed-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>
ARM: tegra: cpuquiet: Fix compiler warnings

Bug 1055406

Change-Id: I5033cc19e170d932c3697821a0f3273957789995
Signed-off-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-on: http://git-master/r/164384
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Krishna Reddy <vdumpa@nvidia.com>
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
Tested-by: Diwakar Tundlam <dtundlam@nvidia.com>
Reviewed-by: Peter De Schrijver <pdeschrijver@nvidia.com>
ARM: tegra: avoid race condition between cpufreq and clusterswitch

Avoid a race condition between cpufreq and clusterswitch and also make writes
to no_lp and enable synchronous operations.

bug 1178947

Change-Id: Ib9608f8a0a22be84d3c0916babb7e43c1f6df2e1
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/165751
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
Reviewed-by: Juha Tukkinen <jtukkinen@nvidia.com>
ARM: tegra: Rework timer for clusterswitching

This patch introduces a separate timer for clusterswitching. The timer will
queue the usual workitem on expiry. This allows all other operations to
happen immediately without having to cancel a delayed workitem. It also allows
the timer itself to be canceled when the conditions for a clusterswitch are
no longer fulfilled.

bug 1178947

Change-Id: Ieb63baf5a38ebcca29ad938365e46530f755a105
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/170533
Reviewed-by: Automatic_Commit_Validation_User
GVS: Gerrit_Virtual_Submit
Tested-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-by: Peter Boonstoppel <pboonstoppel@nvidia.com>
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>
ARM: tegra: only do clusterswitching in 1 CPU mode

Only consider clusterswitching when there is a single CPU online.
This is done by ignoring frequency changes when there are more cores
online and registering a notifier to immediately switch to the slow
cluster if there is only 1 CPU online and the frequency is below
idle_bottom_freq.

bug 1178947
bug 1202877

Change-Id: I1244f866de1d32c268d36c835cd90d9c6cba1481
Signed-off-by: Peter De Schrijver <pdeschrijver@nvidia.com>
Reviewed-on: http://git-master/r/172346
Reviewed-by: Automatic_Commit_Validation_User
Reviewed-by: Diwakar Tundlam <dtundlam@nvidia.com>