• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /netgear-R7000-V1.0.7.12_1.2.5/components/opensource/linux/linux-2.6.36/drivers/cpuidle/
1/*
2 * cpuidle.c - core cpuidle infrastructure
3 *
4 * (C) 2006-2007 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
5 *               Shaohua Li <shaohua.li@intel.com>
6 *               Adam Belay <abelay@novell.com>
7 *
8 * This code is licenced under the GPL.
9 */
10
11#include <linux/kernel.h>
12#include <linux/mutex.h>
13#include <linux/sched.h>
14#include <linux/notifier.h>
15#include <linux/pm_qos_params.h>
16#include <linux/cpu.h>
17#include <linux/cpuidle.h>
18#include <linux/ktime.h>
19#include <linux/hrtimer.h>
20#include <trace/events/power.h>
21
22#include "cpuidle.h"
23
24DEFINE_PER_CPU(struct cpuidle_device *, cpuidle_devices);
25
26DEFINE_MUTEX(cpuidle_lock);
27LIST_HEAD(cpuidle_detected_devices);
28static void (*pm_idle_old)(void);
29
30static int enabled_devices;
31
32#if defined(CONFIG_ARCH_HAS_CPU_IDLE_WAIT)
33static void cpuidle_kick_cpus(void)
34{
35	cpu_idle_wait();
36}
37#elif defined(CONFIG_SMP)
38# error "Arch needs cpu_idle_wait() equivalent here"
39#else /* !CONFIG_ARCH_HAS_CPU_IDLE_WAIT && !CONFIG_SMP */
40static void cpuidle_kick_cpus(void) {}
41#endif
42
43static int __cpuidle_register_device(struct cpuidle_device *dev);
44
45/**
46 * cpuidle_idle_call - the main idle loop
47 *
48 * NOTE: no locks or semaphores should be used here
49 */
50static void cpuidle_idle_call(void)
51{
52	struct cpuidle_device *dev = __get_cpu_var(cpuidle_devices);
53	struct cpuidle_state *target_state;
54	int next_state;
55
56	/* check if the device is ready */
57	if (!dev || !dev->enabled) {
58		if (pm_idle_old)
59			pm_idle_old();
60		else
61#if defined(CONFIG_ARCH_HAS_DEFAULT_IDLE)
62			default_idle();
63#else
64			local_irq_enable();
65#endif
66		return;
67	}
68
69
70	/*
71	 * Call the device's prepare function before calling the
72	 * governor's select function.  ->prepare gives the device's
73	 * cpuidle driver a chance to update any dynamic information
74	 * of its cpuidle states for the current idle period, e.g.
75	 * state availability, latencies, residencies, etc.
76	 */
77	if (dev->prepare)
78		dev->prepare(dev);
79
80	/* ask the governor for the next state */
81	next_state = cpuidle_curr_governor->select(dev);
82	if (need_resched()) {
83		local_irq_enable();
84		return;
85	}
86
87	target_state = &dev->states[next_state];
88
89	/* enter the state and update stats */
90	dev->last_state = target_state;
91	dev->last_residency = target_state->enter(dev, target_state);
92	if (dev->last_state)
93		target_state = dev->last_state;
94
95	target_state->time += (unsigned long long)dev->last_residency;
96	target_state->usage++;
97
98	/* give the governor an opportunity to reflect on the outcome */
99	if (cpuidle_curr_governor->reflect)
100		cpuidle_curr_governor->reflect(dev);
101	trace_power_end(smp_processor_id());
102}
103
104/**
105 * cpuidle_install_idle_handler - installs the cpuidle idle loop handler
106 */
107void cpuidle_install_idle_handler(void)
108{
109	if (enabled_devices && (pm_idle != cpuidle_idle_call)) {
110		/* Make sure all changes finished before we switch to new idle */
111		smp_wmb();
112		pm_idle = cpuidle_idle_call;
113	}
114}
115
116/**
117 * cpuidle_uninstall_idle_handler - uninstalls the cpuidle idle loop handler
118 */
119void cpuidle_uninstall_idle_handler(void)
120{
121	if (enabled_devices && pm_idle_old && (pm_idle != pm_idle_old)) {
122		pm_idle = pm_idle_old;
123		cpuidle_kick_cpus();
124	}
125}
126
127/**
128 * cpuidle_pause_and_lock - temporarily disables CPUIDLE
129 */
130void cpuidle_pause_and_lock(void)
131{
132	mutex_lock(&cpuidle_lock);
133	cpuidle_uninstall_idle_handler();
134}
135
136EXPORT_SYMBOL_GPL(cpuidle_pause_and_lock);
137
138/**
139 * cpuidle_resume_and_unlock - resumes CPUIDLE operation
140 */
141void cpuidle_resume_and_unlock(void)
142{
143	cpuidle_install_idle_handler();
144	mutex_unlock(&cpuidle_lock);
145}
146
147EXPORT_SYMBOL_GPL(cpuidle_resume_and_unlock);
148
149#ifdef CONFIG_ARCH_HAS_CPU_RELAX
150static int poll_idle(struct cpuidle_device *dev, struct cpuidle_state *st)
151{
152	ktime_t	t1, t2;
153	s64 diff;
154	int ret;
155
156	t1 = ktime_get();
157	local_irq_enable();
158	while (!need_resched())
159		cpu_relax();
160
161	t2 = ktime_get();
162	diff = ktime_to_us(ktime_sub(t2, t1));
163	if (diff > INT_MAX)
164		diff = INT_MAX;
165
166	ret = (int) diff;
167	return ret;
168}
169
170static void poll_idle_init(struct cpuidle_device *dev)
171{
172	struct cpuidle_state *state = &dev->states[0];
173
174	cpuidle_set_statedata(state, NULL);
175
176	snprintf(state->name, CPUIDLE_NAME_LEN, "C0");
177	snprintf(state->desc, CPUIDLE_DESC_LEN, "CPUIDLE CORE POLL IDLE");
178	state->exit_latency = 0;
179	state->target_residency = 0;
180	state->power_usage = -1;
181	state->flags = CPUIDLE_FLAG_POLL;
182	state->enter = poll_idle;
183}
184#else
185static void poll_idle_init(struct cpuidle_device *dev) {}
186#endif /* CONFIG_ARCH_HAS_CPU_RELAX */
187
188/**
189 * cpuidle_enable_device - enables idle PM for a CPU
190 * @dev: the CPU
191 *
192 * This function must be called between cpuidle_pause_and_lock and
193 * cpuidle_resume_and_unlock when used externally.
194 */
195int cpuidle_enable_device(struct cpuidle_device *dev)
196{
197	int ret, i;
198
199	if (dev->enabled)
200		return 0;
201	if (!cpuidle_get_driver() || !cpuidle_curr_governor)
202		return -EIO;
203	if (!dev->state_count)
204		return -EINVAL;
205
206	if (dev->registered == 0) {
207		ret = __cpuidle_register_device(dev);
208		if (ret)
209			return ret;
210	}
211
212	poll_idle_init(dev);
213
214	if ((ret = cpuidle_add_state_sysfs(dev)))
215		return ret;
216
217	if (cpuidle_curr_governor->enable &&
218	    (ret = cpuidle_curr_governor->enable(dev)))
219		goto fail_sysfs;
220
221	for (i = 0; i < dev->state_count; i++) {
222		dev->states[i].usage = 0;
223		dev->states[i].time = 0;
224	}
225	dev->last_residency = 0;
226	dev->last_state = NULL;
227
228	smp_wmb();
229
230	dev->enabled = 1;
231
232	enabled_devices++;
233	return 0;
234
235fail_sysfs:
236	cpuidle_remove_state_sysfs(dev);
237
238	return ret;
239}
240
241EXPORT_SYMBOL_GPL(cpuidle_enable_device);
242
243/**
244 * cpuidle_disable_device - disables idle PM for a CPU
245 * @dev: the CPU
246 *
247 * This function must be called between cpuidle_pause_and_lock and
248 * cpuidle_resume_and_unlock when used externally.
249 */
250void cpuidle_disable_device(struct cpuidle_device *dev)
251{
252	if (!dev->enabled)
253		return;
254	if (!cpuidle_get_driver() || !cpuidle_curr_governor)
255		return;
256
257	dev->enabled = 0;
258
259	if (cpuidle_curr_governor->disable)
260		cpuidle_curr_governor->disable(dev);
261
262	cpuidle_remove_state_sysfs(dev);
263	enabled_devices--;
264}
265
266EXPORT_SYMBOL_GPL(cpuidle_disable_device);
267
268/**
269 * __cpuidle_register_device - internal register function called before register
270 * and enable routines
271 * @dev: the cpu
272 *
273 * cpuidle_lock mutex must be held before this is called
274 */
275static int __cpuidle_register_device(struct cpuidle_device *dev)
276{
277	int ret;
278	struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
279	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
280
281	if (!sys_dev)
282		return -EINVAL;
283	if (!try_module_get(cpuidle_driver->owner))
284		return -EINVAL;
285
286	init_completion(&dev->kobj_unregister);
287
288	/*
289	 * cpuidle driver should set the dev->power_specified bit
290	 * before registering the device if the driver provides
291	 * power_usage numbers.
292	 *
293	 * For those devices whose ->power_specified is not set,
294	 * we fill in power_usage with decreasing values as the
295	 * cpuidle code has an implicit assumption that state Cn
296	 * uses less power than C(n-1).
297	 *
298	 * With CONFIG_ARCH_HAS_CPU_RELAX, C0 is already assigned
299	 * an power value of -1.  So we use -2, -3, etc, for other
300	 * c-states.
301	 */
302	if (!dev->power_specified) {
303		int i;
304		for (i = CPUIDLE_DRIVER_STATE_START; i < dev->state_count; i++)
305			dev->states[i].power_usage = -1 - i;
306	}
307
308	per_cpu(cpuidle_devices, dev->cpu) = dev;
309	list_add(&dev->device_list, &cpuidle_detected_devices);
310	if ((ret = cpuidle_add_sysfs(sys_dev))) {
311		module_put(cpuidle_driver->owner);
312		return ret;
313	}
314
315	dev->registered = 1;
316	return 0;
317}
318
319/**
320 * cpuidle_register_device - registers a CPU's idle PM feature
321 * @dev: the cpu
322 */
323int cpuidle_register_device(struct cpuidle_device *dev)
324{
325	int ret;
326
327	mutex_lock(&cpuidle_lock);
328
329	if ((ret = __cpuidle_register_device(dev))) {
330		mutex_unlock(&cpuidle_lock);
331		return ret;
332	}
333
334	cpuidle_enable_device(dev);
335	cpuidle_install_idle_handler();
336
337	mutex_unlock(&cpuidle_lock);
338
339	return 0;
340
341}
342
343EXPORT_SYMBOL_GPL(cpuidle_register_device);
344
345/**
346 * cpuidle_unregister_device - unregisters a CPU's idle PM feature
347 * @dev: the cpu
348 */
349void cpuidle_unregister_device(struct cpuidle_device *dev)
350{
351	struct sys_device *sys_dev = get_cpu_sysdev((unsigned long)dev->cpu);
352	struct cpuidle_driver *cpuidle_driver = cpuidle_get_driver();
353
354	if (dev->registered == 0)
355		return;
356
357	cpuidle_pause_and_lock();
358
359	cpuidle_disable_device(dev);
360
361	cpuidle_remove_sysfs(sys_dev);
362	list_del(&dev->device_list);
363	wait_for_completion(&dev->kobj_unregister);
364	per_cpu(cpuidle_devices, dev->cpu) = NULL;
365
366	cpuidle_resume_and_unlock();
367
368	module_put(cpuidle_driver->owner);
369}
370
371EXPORT_SYMBOL_GPL(cpuidle_unregister_device);
372
373#ifdef CONFIG_SMP
374
375static void smp_callback(void *v)
376{
377	/* we already woke the CPU up, nothing more to do */
378}
379
380/*
381 * This function gets called when a part of the kernel has a new latency
382 * requirement.  This means we need to get all processors out of their C-state,
383 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
384 * wakes them all right up.
385 */
386static int cpuidle_latency_notify(struct notifier_block *b,
387		unsigned long l, void *v)
388{
389	smp_call_function(smp_callback, NULL, 1);
390	return NOTIFY_OK;
391}
392
393static struct notifier_block cpuidle_latency_notifier = {
394	.notifier_call = cpuidle_latency_notify,
395};
396
397static inline void latency_notifier_init(struct notifier_block *n)
398{
399	pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY, n);
400}
401
402#else /* CONFIG_SMP */
403
404#define latency_notifier_init(x) do { } while (0)
405
406#endif /* CONFIG_SMP */
407
408/**
409 * cpuidle_init - core initializer
410 */
411static int __init cpuidle_init(void)
412{
413	int ret;
414
415	pm_idle_old = pm_idle;
416
417	ret = cpuidle_add_class_sysfs(&cpu_sysdev_class);
418	if (ret)
419		return ret;
420
421	latency_notifier_init(&cpuidle_latency_notifier);
422
423	return 0;
424}
425
426core_initcall(cpuidle_init);
427