blob: c27af49a4ede1b525daece26f937b8bfaa6560b5 [file] [log] [blame]
Suresh Siddha61c46282008-03-10 15:28:04 -07001#include <linux/errno.h>
2#include <linux/kernel.h>
3#include <linux/mm.h>
4#include <linux/smp.h>
5#include <linux/slab.h>
6#include <linux/sched.h>
Peter Zijlstra7f424a82008-04-25 17:39:01 +02007#include <linux/module.h>
8#include <linux/pm.h>
Thomas Gleixneraa276e12008-06-09 19:15:00 +02009#include <linux/clockchips.h>
Arjan van de Venf3f47a62008-11-23 16:49:58 -080010#include <linux/ftrace.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080011#include <asm/system.h>
12
13unsigned long idle_halt;
14EXPORT_SYMBOL(idle_halt);
Zhao Yakuida5e09a2008-06-24 18:01:09 +080015unsigned long idle_nomwait;
16EXPORT_SYMBOL(idle_nomwait);
Suresh Siddha61c46282008-03-10 15:28:04 -070017
Suresh Siddhaaa283f42008-03-10 15:28:05 -070018struct kmem_cache *task_xstate_cachep;
Suresh Siddha61c46282008-03-10 15:28:04 -070019
20int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
21{
22 *dst = *src;
Suresh Siddhaaa283f42008-03-10 15:28:05 -070023 if (src->thread.xstate) {
24 dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
25 GFP_KERNEL);
26 if (!dst->thread.xstate)
27 return -ENOMEM;
28 WARN_ON((unsigned long)dst->thread.xstate & 15);
29 memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
30 }
Suresh Siddha61c46282008-03-10 15:28:04 -070031 return 0;
32}
33
Suresh Siddhaaa283f42008-03-10 15:28:05 -070034void free_thread_xstate(struct task_struct *tsk)
35{
36 if (tsk->thread.xstate) {
37 kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
38 tsk->thread.xstate = NULL;
39 }
40}
41
Suresh Siddha61c46282008-03-10 15:28:04 -070042void free_thread_info(struct thread_info *ti)
43{
Suresh Siddhaaa283f42008-03-10 15:28:05 -070044 free_thread_xstate(ti->task);
Suresh Siddha1679f272008-04-16 10:27:53 +020045 free_pages((unsigned long)ti, get_order(THREAD_SIZE));
Suresh Siddha61c46282008-03-10 15:28:04 -070046}
47
48void arch_task_cache_init(void)
49{
50 task_xstate_cachep =
51 kmem_cache_create("task_xstate", xstate_size,
52 __alignof__(union thread_xstate),
53 SLAB_PANIC, NULL);
54}
Peter Zijlstra7f424a82008-04-25 17:39:01 +020055
Thomas Gleixner00dba562008-06-09 18:35:28 +020056/*
57 * Idle related variables and functions
58 */
59unsigned long boot_option_idle_override = 0;
60EXPORT_SYMBOL(boot_option_idle_override);
61
62/*
63 * Powermanagement idle function, if any..
64 */
65void (*pm_idle)(void);
66EXPORT_SYMBOL(pm_idle);
67
68#ifdef CONFIG_X86_32
69/*
70 * This halt magic was a workaround for ancient floppy DMA
71 * wreckage. It should be safe to remove.
72 */
73static int hlt_counter;
74void disable_hlt(void)
75{
76 hlt_counter++;
77}
78EXPORT_SYMBOL(disable_hlt);
79
80void enable_hlt(void)
81{
82 hlt_counter--;
83}
84EXPORT_SYMBOL(enable_hlt);
85
86static inline int hlt_use_halt(void)
87{
88 return (!hlt_counter && boot_cpu_data.hlt_works_ok);
89}
90#else
91static inline int hlt_use_halt(void)
92{
93 return 1;
94}
95#endif
96
97/*
98 * We use this if we don't have any better
99 * idle routine..
100 */
101void default_idle(void)
102{
103 if (hlt_use_halt()) {
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800104 struct power_trace it;
105
106 trace_power_start(&it, POWER_CSTATE, 1);
Thomas Gleixner00dba562008-06-09 18:35:28 +0200107 current_thread_info()->status &= ~TS_POLLING;
108 /*
109 * TS_POLLING-cleared state must be visible before we
110 * test NEED_RESCHED:
111 */
112 smp_mb();
113
114 if (!need_resched())
115 safe_halt(); /* enables interrupts racelessly */
116 else
117 local_irq_enable();
118 current_thread_info()->status |= TS_POLLING;
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800119 trace_power_end(&it);
Thomas Gleixner00dba562008-06-09 18:35:28 +0200120 } else {
121 local_irq_enable();
122 /* loop is done by the caller */
123 cpu_relax();
124 }
125}
126#ifdef CONFIG_APM_MODULE
127EXPORT_SYMBOL(default_idle);
128#endif
129
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200130static void do_nothing(void *unused)
131{
132}
133
134/*
135 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
136 * pm_idle and update to new pm_idle value. Required while changing pm_idle
137 * handler on SMP systems.
138 *
139 * Caller must have changed pm_idle to the new value before the call. Old
140 * pm_idle value will not be used by any CPU after the return of this function.
141 */
142void cpu_idle_wait(void)
143{
144 smp_mb();
145 /* kick all the CPUs so that they exit out of pm_idle */
Ingo Molnar127a2372008-06-27 11:48:22 +0200146 smp_call_function(do_nothing, NULL, 1);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200147}
148EXPORT_SYMBOL_GPL(cpu_idle_wait);
149
150/*
151 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
152 * which can obviate IPI to trigger checking of need_resched.
153 * We execute MONITOR against need_resched and enter optimized wait state
154 * through MWAIT. Whenever someone changes need_resched, we would be woken
155 * up from MWAIT (without an IPI).
156 *
157 * New with Core Duo processors, MWAIT can take some hints based on CPU
158 * capability.
159 */
160void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
161{
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800162 struct power_trace it;
163
164 trace_power_start(&it, POWER_CSTATE, (ax>>4)+1);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200165 if (!need_resched()) {
166 __monitor((void *)&current_thread_info()->flags, 0, 0);
167 smp_mb();
168 if (!need_resched())
169 __mwait(ax, cx);
170 }
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800171 trace_power_end(&it);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200172}
173
174/* Default MONITOR/MWAIT with no hints, used for default C1 state */
175static void mwait_idle(void)
176{
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800177 struct power_trace it;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200178 if (!need_resched()) {
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800179 trace_power_start(&it, POWER_CSTATE, 1);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200180 __monitor((void *)&current_thread_info()->flags, 0, 0);
181 smp_mb();
182 if (!need_resched())
183 __sti_mwait(0, 0);
184 else
185 local_irq_enable();
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800186 trace_power_end(&it);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200187 } else
188 local_irq_enable();
189}
190
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200191/*
192 * On SMP it's slightly faster (but much more power-consuming!)
193 * to poll the ->work.need_resched flag instead of waiting for the
194 * cross-CPU IPI to arrive. Use this option with caution.
195 */
196static void poll_idle(void)
197{
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800198 struct power_trace it;
199
200 trace_power_start(&it, POWER_CSTATE, 0);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200201 local_irq_enable();
Joe Korty2c7e9fd2008-08-27 10:35:06 -0400202 while (!need_resched())
203 cpu_relax();
Arjan van de Venf3f47a62008-11-23 16:49:58 -0800204 trace_power_end(&it);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200205}
206
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200207/*
208 * mwait selection logic:
209 *
210 * It depends on the CPU. For AMD CPUs that support MWAIT this is
211 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
212 * then depend on a clock divisor and current Pstate of the core. If
213 * all cores of a processor are in halt state (C1) the processor can
214 * enter the C1E (C1 enhanced) state. If mwait is used this will never
215 * happen.
216 *
217 * idle=mwait overrides this decision and forces the usage of mwait.
218 */
Jan Beulich08ad8af2008-07-18 13:45:20 +0100219static int __cpuinitdata force_mwait;
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200220
221#define MWAIT_INFO 0x05
222#define MWAIT_ECX_EXTENDED_INFO 0x01
223#define MWAIT_EDX_C1 0xf0
224
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200225static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
226{
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200227 u32 eax, ebx, ecx, edx;
228
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200229 if (force_mwait)
230 return 1;
231
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200232 if (c->cpuid_level < MWAIT_INFO)
233 return 0;
234
235 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
236 /* Check, whether EDX has extended info about MWAIT */
237 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
238 return 1;
239
240 /*
241 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
242 * C1 supports MWAIT
243 */
244 return (edx & MWAIT_EDX_C1);
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200245}
246
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200247/*
248 * Check for AMD CPUs, which have potentially C1E support
249 */
250static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
251{
252 if (c->x86_vendor != X86_VENDOR_AMD)
253 return 0;
254
255 if (c->x86 < 0x0F)
256 return 0;
257
258 /* Family 0x0f models < rev F do not have C1E */
259 if (c->x86 == 0x0f && c->x86_model < 0x40)
260 return 0;
261
262 return 1;
263}
264
Thomas Gleixner4faac972008-09-22 18:54:29 +0200265static cpumask_t c1e_mask = CPU_MASK_NONE;
266static int c1e_detected;
267
268void c1e_remove_cpu(int cpu)
269{
270 cpu_clear(cpu, c1e_mask);
271}
272
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200273/*
274 * C1E aware idle routine. We check for C1E active in the interrupt
275 * pending message MSR. If we detect C1E, then we handle it the same
276 * way as C3 power states (local apic timer and TSC stop)
277 */
278static void c1e_idle(void)
279{
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200280 if (need_resched())
281 return;
282
283 if (!c1e_detected) {
284 u32 lo, hi;
285
286 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
287 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
288 c1e_detected = 1;
Andreas Herrmann09bfeea2008-09-18 21:12:10 +0200289 if (!boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
290 mark_tsc_unstable("TSC halt in AMD C1E");
291 printk(KERN_INFO "System has AMD C1E enabled\n");
Thomas Gleixnera8d68292008-09-22 19:02:25 +0200292 set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200293 }
294 }
295
296 if (c1e_detected) {
297 int cpu = smp_processor_id();
298
299 if (!cpu_isset(cpu, c1e_mask)) {
300 cpu_set(cpu, c1e_mask);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200301 /*
302 * Force broadcast so ACPI can not interfere. Needs
303 * to run with interrupts enabled as it uses
304 * smp_function_call.
305 */
306 local_irq_enable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200307 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
308 &cpu);
309 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
310 cpu);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200311 local_irq_disable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200312 }
313 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200314
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200315 default_idle();
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200316
317 /*
318 * The switch back from broadcast mode needs to be
319 * called with interrupts disabled.
320 */
321 local_irq_disable();
322 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
323 local_irq_enable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200324 } else
325 default_idle();
326}
327
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200328void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
329{
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200330#ifdef CONFIG_X86_SMP
331 if (pm_idle == poll_idle && smp_num_siblings > 1) {
332 printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
333 " performance may degrade.\n");
334 }
335#endif
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200336 if (pm_idle)
337 return;
338
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200339 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200340 /*
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200341 * One CPU supports mwait => All CPUs supports mwait
342 */
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200343 printk(KERN_INFO "using mwait in idle threads.\n");
344 pm_idle = mwait_idle;
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200345 } else if (check_c1e_idle(c)) {
346 printk(KERN_INFO "using C1E aware idle routine\n");
347 pm_idle = c1e_idle;
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200348 } else
349 pm_idle = default_idle;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200350}
351
352static int __init idle_setup(char *str)
353{
Cyrill Gorcunovab6bc3e2008-07-05 15:53:36 +0400354 if (!str)
355 return -EINVAL;
356
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200357 if (!strcmp(str, "poll")) {
358 printk("using polling idle threads.\n");
359 pm_idle = poll_idle;
360 } else if (!strcmp(str, "mwait"))
361 force_mwait = 1;
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800362 else if (!strcmp(str, "halt")) {
363 /*
364 * When the boot option of idle=halt is added, halt is
365 * forced to be used for CPU idle. In such case CPU C2/C3
366 * won't be used again.
367 * To continue to load the CPU idle driver, don't touch
368 * the boot_option_idle_override.
369 */
370 pm_idle = default_idle;
371 idle_halt = 1;
372 return 0;
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800373 } else if (!strcmp(str, "nomwait")) {
374 /*
375 * If the boot option of "idle=nomwait" is added,
376 * it means that mwait will be disabled for CPU C2/C3
377 * states. In such case it won't touch the variable
378 * of boot_option_idle_override.
379 */
380 idle_nomwait = 1;
381 return 0;
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800382 } else
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200383 return -1;
384
385 boot_option_idle_override = 1;
386 return 0;
387}
388early_param("idle", idle_setup);
389