ARM: msm: only create rpcrouter pdev once

Change-Id: I1e3bebe50c93eb4c4874d35d1c87ebafa4f88af5
Signed-off-by: Dima Zavin <dima@android.com> (cherry picked from commit ed0708d)
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: 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
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: Use the correct units for ADRENO_IDLE_TIMEOUT

In a few spots the ADRENO_IDLE_TIMEOUT value (measured in milliseconds)
was used directly for jiffy calculations resulting in timeouts that were
1000/HZ times longer than they should have been.  Use msecs_to_jiffies
for these situations.

CRs-Fixed: 448226
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
(cherry picked from commit 0bcdb735fb0ffa524638ab75acf76d3ddfe38616)

Change-Id: Ic2cd7eaf949d5c69c803fd109f206629de56ef9f
Signed-off-by: Srivalli Oguri <oguri@codeaurora.org>
msm: kgsl: Turn off the CP_DEBUG dynamic clock

The CP dynamic clock seems to be glitchy when the CP clocks are turned
back on after a power event. Turn off said dynamic clock control at
init time. The impact of leaving the dynamic clock control off is
negligible since the CP clock is only on when the CP is actually in
use.

CRs-fixed: 402119
CRs-fixed: 409253
CRs-fixed: 413224
Change-Id: Ic0dedbad783f8b911d9b57d1602d9b3976af1b3b
Signed-off-by: Jordan Crouse <jcrouse@codeaurora.org>
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: I6105250faff58da588e4bb6b3f4101e49e5c4758
Signed-off-by: Gopal Goberu <ggober@codeaurora.org>
msm: kgsl: Always submit an interrupt request to the GPU

A context does not have to be present to send an internal dummy
command to the GPU.  Remove BUG() in this case and instead send
a NULL context which will still cause the interrupt to get submitted
to the GPU.

CRs-fixed: 392688
Change-Id: I66b6eebe899ab09e6615d08faddea7ea73de5c6d
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: Check for valid pointer before dereferencing

Check to make sure that there is a valid context pointer before
dereferencing it when adding commands to the ringbuffer.

Change-Id: Icf9824de8f62a28cdeeef4378246d9fb334d39fa
Signed-off-by: Carter Cooper <ccooper@codeaurora.org>
msm: kgsl: use kgsl_mmu_device_setstate() if the GPU is already idle

Use the default setstate function, which directly reprograms the IOMMU,
to change IOMMU pagetables or flush the TLB if the GPU is already idle.
This condition often occurs when the GPU is being powered down. In this
case it is desirable to avoid the overhead of issuing commands, waiting
for idle and firing events that results from using the GPU
command stream to reprogram the IOMMU.

Change-Id: I633002ac49c8fe58df3f1f6a1fd1ddf705fc1733
Signed-off-by: Jeremy Gebben <jgebben@codeaurora.org>
msm: kgsl: Remove extra interrupts when setting MMU state

The interrupts added to the ringbuffer on PTFLUSH and TLBUPDATE
were causing a major increase in the number of interrupts from the GPU.
This was leading to increase in power and loss of performance. Add a check
to turn off IOMMU clocks when going to SLEEP.

Change-Id: I41617dd3b7b3f7d9622523f2a1407b912dbd989e
Signed-off-by: Shubhraprakash Das <sadas@codeaurora.org>
msm: remove fmem support

This functionality is deprecated and no longer supported.
kernel:sched: LOAD_FREQ (4*HZ+61) avoids loadavg Moire

http://ripke.com/loadavg/moire

LOAD_FREQ is (5*HZ+1) to avoid high load average when idle.

Tests suggest (4*HZ+61) for a better distribution.

With some seconds based load (like SSL heartbeats)
and LOAD_FREQ at (5*HZ+1) we see Moire patterns like inverse sawtooth,
since 2 or 3 probes hit the jobs (load increases quickly),
followed by several probes missing it.

A 4.61 sec interval gives optimal distribution over when within a
second a probe is taken, as .61 is close to golden ratio phi 1.618...
(test in http://ripke.com/goldenratio.c).

12*4.61 = 55.32 secs is still close to a minute,
and 13*4.61=59.93 is even closer than the current 12*5.01=60.12
(with exponents EXP_x adjusted to a ratio of 13 instead of 12).
smp: Add generic smpboot facility

Thomas Gleixner's generic-idle patches:

smp: Add task_struct argument to __cpu_up()
	Preparatory patch to make the idle thread allocation for secondary
	cpus generic.

smp: Add generic smpboot facility
	Start a new file, which will hold SMP and CPU hotplug related generic
	infrastructure.

smp: Provide generic idle thread allocation
	All SMP architectures have magic to fork the idle task and to store it
	for reusage when cpu hotplug is enabled. Provide a generic
	infrastructure for it.
	Create/reinit the idle thread for the cpu which is brought up in the
	generic code and hand the thread pointer to the architecture code via
	__cpu_up().
	Note, that fork_idle() is called via a workqueue, because this
	guarantees that the idle thread does not get a reference to a user
	space VM. This can happen when the boot process did not bring up all
	possible cpus and a later cpu_up() is initiated via the sysfs
	interface. In that case fork_idle() would be called in the context of
	the user space task and take a reference on the user space VM.

arm: Use generic idle thread allocation

smp, idle: Allocate idle thread for each possible cpu during boot
	percpu areas are already allocated during boot for each possible cpu.
	percpu idle threads can be considered as an extension of the percpu areas,
	and allocate them for each possible cpu during boot.

	This will eliminate the need for workqueue based idle thread allocation.
	In future we can move the idle thread area into the percpu area too.

	[ tglx: Moved the loop into smpboot.c and added an error check when
	the init code failed to allocate an idle thread for a cpu which
	should be onlined ]

smpboot, idle: Optimize calls to smp_processor_id() in idle_threads_init()
	While trying to initialize idle threads for all cpus, idle_threads_init()
	calls smp_processor_id() in a loop, which is unnecessary. The intent
	is to initialize idle threads for all non-boot cpus. So just use a variable
	to note the boot cpu and use it in the loop.

kthread: Implement park/unpark facility
	To avoid the full teardown/setup of per cpu kthreads in the case of
	cpu hot(un)plug, provide a facility which allows to put the kthread
	into a park position and unpark it when the cpu comes online again.

smpboot: Provide infrastructure for percpu hotplug threads
	Provide a generic interface for setting up and tearing down percpu
	threads.

	On registration the threads for already online cpus are created and
	started. On deregistration (modules) the threads are stoppped.

	During hotplug operations the threads are created, started, parked and
	unparked. The datastructure for registration provides a pointer to
	percpu storage space and optional setup, cleanup, park, unpark
	functions. These functions are called when the thread state changes.

	Each implementation has to provide a function which is queried and
	returns whether the thread should run and the thread function itself.

	The core code handles all state transitions and avoids duplicated code
	in the call sites.

	[ paulmck: Preemption leak fix ]

hotplug: Fix UP bug in smpboot hotplug code
	Because kernel subsystems need their per-CPU kthreads on UP systems as
	well as on SMP systems, the smpboot hotplug kthread functions must be
	provided in UP builds as well as in SMP builds.  This commit therefore
	adds smpboot.c to UP builds and excludes irrelevant code via #ifdef.

softirq: Use hotplug thread infrastructure

watchdog: Use hotplug thread infrastructure

rcu: Use smp_hotplug_thread facility for RCUs per-CPU kthread
	Bring RCU into the new-age CPU-hotplug fold by modifying RCU's per-CPU
	kthread code to use the new smp_hotplug_thread facility.

	[ tglx: Adapted it to use callbacks and to the simplified rcu yield ]

smpboot: Allow selfparking per cpu threads
	The stop machine threads are still killed when a cpu goes offline. The
	reason is that the thread is used to bring the cpu down, so it can't
	be parked along with the other per cpu threads.

	Allow a per cpu thread to be excluded from automatic parking, so it
	can park itself once it's done

	Add a create callback function as well.

kthread: Prevent unpark race which puts threads on the wrong cpu
	The smpboot threads rely on the park/unpark mechanism which binds per
	cpu threads on a particular core. Though the functionality is racy:

	CPU0	       	 	CPU1  	     	    CPU2
	unpark(T)				    wake_up_process(T)
	  clear(SHOULD_PARK)	T runs
				leave parkme() due to !SHOULD_PARK
	  bind_to(CPU2)		BUG_ON(wrong CPU)

	We cannot let the tasks move themself to the target CPU as one of
	those tasks is actually the migration thread itself, which requires
	that it starts running on the target cpu right away.

	The solution to this problem is to prevent wakeups in park mode which
	are not from unpark(). That way we can guarantee that the association
	of the task to the target cpu is working correctly.

	Add a new task state (TASK_PARKED) which prevents other wakeups and
	use this state explicitly for the unpark wakeup.

	Peter noticed: Also, since the task state is visible to userspace and
	all the parked tasks are still in the PID space, its a good hint in ps
	and friends that these tasks aren't really there for the moment.

	The migration thread has another related issue.

	CPU0	      	     	 CPU1
	Bring up CPU2
	create_thread(T)
	park(T)
	 wait_for_completion()
				 parkme()
				 complete()
	sched_set_stop_task()
				 schedule(TASK_PARKED)

	The sched_set_stop_task() call is issued while the task is on the
	runqueue of CPU1 and that confuses the hell out of the stop_task class
	on that cpu. So we need the same synchronizaion before
	sched_set_stop_task().

kthread_worker: reorganize to prepare for flush_kthread_work() reimplementation
	Make the following two non-functional changes.

	* Separate out insert_kthread_work() from queue_kthread_work().

	* Relocate struct kthread_flush_work and kthread_flush_work_fn()
	  definitions above flush_kthread_work().

	v2: Added lockdep_assert_held() in insert_kthread_work() as suggested
	    by Andy Walls.

	Signed-off-by: Tejun Heo <tj@kernel.org>
	Acked-by: Andy Walls <awalls@md.metrocast.net>

kthread_worker: reimplement flush_kthread_work() to allow freeing the work item being executed
	kthread_worker provides minimalistic workqueue-like interface for
	users which need a dedicated worker thread (e.g. for realtime
	priority).  It has basic queue, flush_work, flush_worker operations
	which mostly match the workqueue counterparts; however, due to the way
	flush_work() is implemented, it has a noticeable difference of not
	allowing work items to be freed while being executed.

	While the current users of kthread_worker are okay with the current
	behavior, the restriction does impede some valid use cases.  Also,
	removing this difference isn't difficult and actually makes the code
	easier to understand.

	This patch reimplements flush_kthread_work() such that it uses a
	flush_work item instead of queue/done sequence numbers.

stop_machine: Store task reference in a separate per cpu variable
	To allow the stopper thread being managed by the smpboot thread
	infrastructure separate out the task storage from the stopper data
	structure.

stop_machine: Use smpboot threads
	Use the smpboot thread infrastructure. Mark the stopper thread
	selfparking and park it after it has finished the take_cpu_down()
	work.

stop_machine: Mark per cpu stopper enabled early
	commit 14e568e78 (stop_machine: Use smpboot threads) introduced the
	following regression:

	Before this commit the stopper enabled bit was set in the online
	notifier.

	CPU0				CPU1
	cpu_up
					cpu online
	hotplug_notifier(ONLINE)
	  stopper(CPU1)->enabled = true;
	...
	stop_machine()

	The conversion to smpboot threads moved the enablement to the wakeup
	path of the parked thread. The majority of users seem to have the
	following working order:

	CPU0				CPU1
	cpu_up
					cpu online
	unpark_threads()
	  wakeup(stopper[CPU1])
	....
					stopper thread runs
					  stopper(CPU1)->enabled = true;
	stop_machine()

	But Konrad and Sander have observed:

	CPU0				CPU1
	cpu_up
					cpu online
	unpark_threads()
	  wakeup(stopper[CPU1])
	....
	stop_machine()
					stopper thread runs
					  stopper(CPU1)->enabled = true;

	Now the stop machinery kicks CPU0 into the stop loop, where it gets
	stuck forever because the queue code saw stopper(CPU1)->enabled ==
	false, so CPU0 waits for CPU1 to enter stomp_machine, but the CPU1
	stopper work got discarded due to enabled == false.

	Add a pre_unpark function to the smpboot thread descriptor and call it
	before waking the thread.

	This fixes the problem at hand, but the stop_machine code should be
	more robust. The stopper->enabled flag smells fishy at best.

	Thanks to Konrad for going through a loop of debug patches and
	providing the information to decode this issue.

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
mach-msm: don't enforce compiling run_queue_stats module

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
mach-msm: Introduce Dynamic hotplug driver for MSM

Introduce a dynamic hotplug driver to automatically online/offline
CPUs proportional to load.

Signed-off-by: Stratos Karafotis <stratosk@semaphore.gr>
cpufreq: update policy->util directly

Signed-off-by: moddingg33k <moddingg33k@t-online.de>
dyn_hotplug: relax conditions for hotplug

- reduce hot-un-plug lock to 3 sec
- increase hot-plug lock to 1.5 sec
- increase up_threshold to 80

- consider max_load of busiest CPU instead of avg_load

- reset 'up_timer' if cpu0_freq below cpu0_max_freq
  AND 'load' below 'up_threshold'

Signed-off-by: moddingg33k <moddingg33k@t-online.de>