blob: c38d84e01022d31b36b44e0fdae4c4536f19e3d5 [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>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -08005#include <linux/prctl.h>
Suresh Siddha61c46282008-03-10 15:28:04 -07006#include <linux/slab.h>
7#include <linux/sched.h>
Peter Zijlstra7f424a82008-04-25 17:39:01 +02008#include <linux/module.h>
9#include <linux/pm.h>
Thomas Gleixneraa276e12008-06-09 19:15:00 +020010#include <linux/clockchips.h>
Amerigo Wang9d62dcd2009-05-11 22:05:28 -040011#include <linux/random.h>
Avi Kivity7c68af62009-09-19 09:40:22 +030012#include <linux/user-return-notifier.h>
Andy Isaacson814e2c82009-12-08 00:29:42 -080013#include <linux/dmi.h>
14#include <linux/utsname.h>
Arjan van de Ven61613522009-09-17 16:11:28 +020015#include <trace/events/power.h>
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +020016#include <linux/hw_breakpoint.h>
Borislav Petkov93789b32011-01-20 15:42:52 +010017#include <asm/cpu.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080018#include <asm/system.h>
Ivan Vecerad3ec5ca2008-11-11 14:33:44 +010019#include <asm/apic.h>
Jaswinder Singh Rajput2c1b2842009-04-11 00:03:10 +053020#include <asm/syscalls.h>
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080021#include <asm/idle.h>
22#include <asm/uaccess.h>
23#include <asm/i387.h>
Linus Torvalds1361b832012-02-21 13:19:22 -080024#include <asm/fpu-internal.h>
K.Prasad66cb5912009-06-01 23:44:55 +053025#include <asm/debugreg.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080026
Suresh Siddhaaa283f42008-03-10 15:28:05 -070027struct kmem_cache *task_xstate_cachep;
Sheng Yang5ee481d2010-05-17 17:22:23 +080028EXPORT_SYMBOL_GPL(task_xstate_cachep);
Suresh Siddha61c46282008-03-10 15:28:04 -070029
30int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
31{
Avi Kivity86603282010-05-06 11:45:46 +030032 int ret;
33
Suresh Siddha61c46282008-03-10 15:28:04 -070034 *dst = *src;
Avi Kivity86603282010-05-06 11:45:46 +030035 if (fpu_allocated(&src->thread.fpu)) {
36 memset(&dst->thread.fpu, 0, sizeof(dst->thread.fpu));
37 ret = fpu_alloc(&dst->thread.fpu);
38 if (ret)
39 return ret;
40 fpu_copy(&dst->thread.fpu, &src->thread.fpu);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070041 }
Suresh Siddha61c46282008-03-10 15:28:04 -070042 return 0;
43}
44
Suresh Siddhaaa283f42008-03-10 15:28:05 -070045void free_thread_xstate(struct task_struct *tsk)
46{
Avi Kivity86603282010-05-06 11:45:46 +030047 fpu_free(&tsk->thread.fpu);
Suresh Siddhaaa283f42008-03-10 15:28:05 -070048}
49
Suresh Siddha61c46282008-03-10 15:28:04 -070050void free_thread_info(struct thread_info *ti)
51{
Suresh Siddhaaa283f42008-03-10 15:28:05 -070052 free_thread_xstate(ti->task);
Zhao Jinc812d8f2011-08-20 21:24:57 +080053 free_pages((unsigned long)ti, THREAD_ORDER);
Suresh Siddha61c46282008-03-10 15:28:04 -070054}
55
56void arch_task_cache_init(void)
57{
58 task_xstate_cachep =
59 kmem_cache_create("task_xstate", xstate_size,
60 __alignof__(union thread_xstate),
Vegard Nossum2dff4402008-05-31 15:56:17 +020061 SLAB_PANIC | SLAB_NOTRACK, NULL);
Suresh Siddha61c46282008-03-10 15:28:04 -070062}
Peter Zijlstra7f424a82008-04-25 17:39:01 +020063
Thomas Gleixner00dba562008-06-09 18:35:28 +020064/*
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080065 * Free current thread data structures etc..
66 */
67void exit_thread(void)
68{
69 struct task_struct *me = current;
70 struct thread_struct *t = &me->thread;
Thomas Gleixner250981e2009-03-16 13:07:21 +010071 unsigned long *bp = t->io_bitmap_ptr;
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080072
Thomas Gleixner250981e2009-03-16 13:07:21 +010073 if (bp) {
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080074 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
75
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080076 t->io_bitmap_ptr = NULL;
77 clear_thread_flag(TIF_IO_BITMAP);
78 /*
79 * Careful, clear this in the TSS too:
80 */
81 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
82 t->io_bitmap_max = 0;
83 put_cpu();
Thomas Gleixner250981e2009-03-16 13:07:21 +010084 kfree(bp);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080085 }
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -080086}
87
Brian Gerst3bef4442010-01-13 10:45:55 -050088void show_regs(struct pt_regs *regs)
89{
90 show_registers(regs);
Namhyung Kime8e999c2011-03-18 11:40:06 +090091 show_trace(NULL, regs, (unsigned long *)kernel_stack_pointer(regs), 0);
Brian Gerst3bef4442010-01-13 10:45:55 -050092}
93
Andy Isaacson814e2c82009-12-08 00:29:42 -080094void show_regs_common(void)
95{
Naga Chumbalkar84e383b2011-02-14 22:47:17 +000096 const char *vendor, *product, *board;
Andy Isaacson814e2c82009-12-08 00:29:42 -080097
Naga Chumbalkar84e383b2011-02-14 22:47:17 +000098 vendor = dmi_get_system_info(DMI_SYS_VENDOR);
99 if (!vendor)
100 vendor = "";
Andy Isaacsona1884b82009-12-08 00:30:21 -0800101 product = dmi_get_system_info(DMI_PRODUCT_NAME);
102 if (!product)
103 product = "";
Andy Isaacson814e2c82009-12-08 00:29:42 -0800104
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000105 /* Board Name is optional */
106 board = dmi_get_system_info(DMI_BOARD_NAME);
107
Pekka Enbergd015a092009-12-28 10:26:59 +0200108 printk(KERN_CONT "\n");
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000109 printk(KERN_DEFAULT "Pid: %d, comm: %.20s %s %s %.*s",
Andy Isaacson814e2c82009-12-08 00:29:42 -0800110 current->pid, current->comm, print_tainted(),
111 init_utsname()->release,
112 (int)strcspn(init_utsname()->version, " "),
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000113 init_utsname()->version);
Jan Beulichfd8fa4d32011-02-17 15:56:58 +0000114 printk(KERN_CONT " %s %s", vendor, product);
115 if (board)
116 printk(KERN_CONT "/%s", board);
Naga Chumbalkar84e383b2011-02-14 22:47:17 +0000117 printk(KERN_CONT "\n");
Andy Isaacson814e2c82009-12-08 00:29:42 -0800118}
119
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800120void flush_thread(void)
121{
122 struct task_struct *tsk = current;
123
Frederic Weisbecker24f1e32c2009-09-09 19:22:48 +0200124 flush_ptrace_hw_breakpoint(tsk);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800125 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
126 /*
127 * Forget coprocessor state..
128 */
129 tsk->fpu_counter = 0;
130 clear_fpu(tsk);
131 clear_used_math();
132}
133
134static void hard_disable_TSC(void)
135{
136 write_cr4(read_cr4() | X86_CR4_TSD);
137}
138
139void disable_TSC(void)
140{
141 preempt_disable();
142 if (!test_and_set_thread_flag(TIF_NOTSC))
143 /*
144 * Must flip the CPU state synchronously with
145 * TIF_NOTSC in the current running context.
146 */
147 hard_disable_TSC();
148 preempt_enable();
149}
150
151static void hard_enable_TSC(void)
152{
153 write_cr4(read_cr4() & ~X86_CR4_TSD);
154}
155
156static void enable_TSC(void)
157{
158 preempt_disable();
159 if (test_and_clear_thread_flag(TIF_NOTSC))
160 /*
161 * Must flip the CPU state synchronously with
162 * TIF_NOTSC in the current running context.
163 */
164 hard_enable_TSC();
165 preempt_enable();
166}
167
168int get_tsc_mode(unsigned long adr)
169{
170 unsigned int val;
171
172 if (test_thread_flag(TIF_NOTSC))
173 val = PR_TSC_SIGSEGV;
174 else
175 val = PR_TSC_ENABLE;
176
177 return put_user(val, (unsigned int __user *)adr);
178}
179
180int set_tsc_mode(unsigned int val)
181{
182 if (val == PR_TSC_SIGSEGV)
183 disable_TSC();
184 else if (val == PR_TSC_ENABLE)
185 enable_TSC();
186 else
187 return -EINVAL;
188
189 return 0;
190}
191
192void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
193 struct tss_struct *tss)
194{
195 struct thread_struct *prev, *next;
196
197 prev = &prev_p->thread;
198 next = &next_p->thread;
199
Peter Zijlstraea8e61b2010-03-25 14:51:51 +0100200 if (test_tsk_thread_flag(prev_p, TIF_BLOCKSTEP) ^
201 test_tsk_thread_flag(next_p, TIF_BLOCKSTEP)) {
202 unsigned long debugctl = get_debugctlmsr();
203
204 debugctl &= ~DEBUGCTLMSR_BTF;
205 if (test_tsk_thread_flag(next_p, TIF_BLOCKSTEP))
206 debugctl |= DEBUGCTLMSR_BTF;
207
208 update_debugctlmsr(debugctl);
209 }
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800210
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800211 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
212 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
213 /* prev and next are different */
214 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
215 hard_disable_TSC();
216 else
217 hard_enable_TSC();
218 }
219
220 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
221 /*
222 * Copy the relevant range of the IO bitmap.
223 * Normally this is 128 bytes or less:
224 */
225 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
226 max(prev->io_bitmap_max, next->io_bitmap_max));
227 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
228 /*
229 * Clear any possible leftover bits:
230 */
231 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
232 }
Avi Kivity7c68af62009-09-19 09:40:22 +0300233 propagate_user_return_notify(prev_p, next_p);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800234}
235
236int sys_fork(struct pt_regs *regs)
237{
238 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
239}
240
241/*
242 * This is trivial, and on the face of it looks like it
243 * could equally well be done in user mode.
244 *
245 * Not so, for quite unobvious reasons - register pressure.
246 * In user mode vfork() cannot have a stack frame, and if
247 * done by calling the "clone()" system call directly, you
248 * do not have enough call-clobbered registers to hold all
249 * the information you need.
250 */
251int sys_vfork(struct pt_regs *regs)
252{
253 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
254 NULL, NULL);
255}
256
Brian Gerstf839bbc2009-12-09 19:01:56 -0500257long
258sys_clone(unsigned long clone_flags, unsigned long newsp,
259 void __user *parent_tid, void __user *child_tid, struct pt_regs *regs)
260{
261 if (!newsp)
262 newsp = regs->sp;
263 return do_fork(clone_flags, newsp, regs, 0, parent_tid, child_tid);
264}
265
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500266/*
267 * This gets run with %si containing the
268 * function to call, and %di containing
269 * the "args".
270 */
271extern void kernel_thread_helper(void);
272
273/*
274 * Create a kernel thread
275 */
276int kernel_thread(int (*fn)(void *), void *arg, unsigned long flags)
277{
278 struct pt_regs regs;
279
280 memset(&regs, 0, sizeof(regs));
281
282 regs.si = (unsigned long) fn;
283 regs.di = (unsigned long) arg;
284
285#ifdef CONFIG_X86_32
286 regs.ds = __USER_DS;
287 regs.es = __USER_DS;
288 regs.fs = __KERNEL_PERCPU;
289 regs.gs = __KERNEL_STACK_CANARY;
Cyrill Gorcunov864a0922010-01-13 10:16:07 +0000290#else
291 regs.ss = __KERNEL_DS;
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500292#endif
293
294 regs.orig_ax = -1;
295 regs.ip = (unsigned long) kernel_thread_helper;
296 regs.cs = __KERNEL_CS | get_kernel_rpl();
Seiichi Ikarashi1cf83432011-12-06 17:58:14 +0900297 regs.flags = X86_EFLAGS_IF | X86_EFLAGS_BIT1;
Brian Gerstdf59e7b2009-12-09 12:34:44 -0500298
299 /* Ok, create the new process.. */
300 return do_fork(flags | CLONE_VM | CLONE_UNTRACED, 0, &regs, 0, NULL, NULL);
301}
302EXPORT_SYMBOL(kernel_thread);
Jeremy Fitzhardinge389d1fb2009-02-27 13:25:28 -0800303
304/*
Brian Gerst11cf88b2009-12-09 19:01:53 -0500305 * sys_execve() executes a new program.
306 */
David Howellsd7627462010-08-17 23:52:56 +0100307long sys_execve(const char __user *name,
308 const char __user *const __user *argv,
309 const char __user *const __user *envp, struct pt_regs *regs)
Brian Gerst11cf88b2009-12-09 19:01:53 -0500310{
311 long error;
312 char *filename;
313
314 filename = getname(name);
315 error = PTR_ERR(filename);
316 if (IS_ERR(filename))
317 return error;
318 error = do_execve(filename, argv, envp, regs);
319
320#ifdef CONFIG_X86_32
321 if (error == 0) {
322 /* Make sure we don't return using sysenter.. */
323 set_thread_flag(TIF_IRET);
324 }
325#endif
326
327 putname(filename);
328 return error;
329}
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200330
331/*
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200332 * Idle related variables and functions
333 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100334unsigned long boot_option_idle_override = IDLE_NO_OVERRIDE;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200335EXPORT_SYMBOL(boot_option_idle_override);
336
337/*
338 * Powermanagement idle function, if any..
339 */
340void (*pm_idle)(void);
Andy Whitcroft60b8b1d2011-06-14 12:45:10 -0700341#ifdef CONFIG_APM_MODULE
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200342EXPORT_SYMBOL(pm_idle);
Len Brown06ae40c2011-04-01 15:28:09 -0400343#endif
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200344
345#ifdef CONFIG_X86_32
346/*
347 * This halt magic was a workaround for ancient floppy DMA
348 * wreckage. It should be safe to remove.
349 */
350static int hlt_counter;
351void disable_hlt(void)
352{
353 hlt_counter++;
354}
355EXPORT_SYMBOL(disable_hlt);
356
357void enable_hlt(void)
358{
359 hlt_counter--;
360}
361EXPORT_SYMBOL(enable_hlt);
362
363static inline int hlt_use_halt(void)
364{
365 return (!hlt_counter && boot_cpu_data.hlt_works_ok);
366}
367#else
368static inline int hlt_use_halt(void)
369{
370 return 1;
371}
372#endif
373
374/*
375 * We use this if we don't have any better
376 * idle routine..
377 */
378void default_idle(void)
379{
380 if (hlt_use_halt()) {
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200381 trace_power_start(POWER_CSTATE, 1, smp_processor_id());
Thomas Renninger25e41932011-01-03 17:50:44 +0100382 trace_cpu_idle(1, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200383 current_thread_info()->status &= ~TS_POLLING;
384 /*
385 * TS_POLLING-cleared state must be visible before we
386 * test NEED_RESCHED:
387 */
388 smp_mb();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200389
390 if (!need_resched())
391 safe_halt(); /* enables interrupts racelessly */
392 else
393 local_irq_enable();
394 current_thread_info()->status |= TS_POLLING;
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100395 trace_power_end(smp_processor_id());
396 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200397 } else {
398 local_irq_enable();
399 /* loop is done by the caller */
400 cpu_relax();
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200401 }
402}
Andy Whitcroft60b8b1d2011-06-14 12:45:10 -0700403#ifdef CONFIG_APM_MODULE
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200404EXPORT_SYMBOL(default_idle);
405#endif
406
Konrad Rzeszutek Wilke5fd47b2011-11-21 18:02:02 -0500407bool set_pm_idle_to_default(void)
408{
409 bool ret = !!pm_idle;
410
411 pm_idle = default_idle;
412
413 return ret;
414}
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200415void stop_this_cpu(void *dummy)
416{
417 local_irq_disable();
418 /*
419 * Remove this CPU:
420 */
Rusty Russell4f062892009-03-13 14:49:54 +1030421 set_cpu_online(smp_processor_id(), false);
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200422 disable_local_APIC();
423
424 for (;;) {
425 if (hlt_works(smp_processor_id()))
426 halt();
427 }
428}
429
430static void do_nothing(void *unused)
431{
432}
433
434/*
435 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
436 * pm_idle and update to new pm_idle value. Required while changing pm_idle
437 * handler on SMP systems.
438 *
439 * Caller must have changed pm_idle to the new value before the call. Old
440 * pm_idle value will not be used by any CPU after the return of this function.
441 */
442void cpu_idle_wait(void)
443{
444 smp_mb();
445 /* kick all the CPUs so that they exit out of pm_idle */
446 smp_call_function(do_nothing, NULL, 1);
447}
448EXPORT_SYMBOL_GPL(cpu_idle_wait);
449
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200450/* Default MONITOR/MWAIT with no hints, used for default C1 state */
451static void mwait_idle(void)
452{
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200453 if (!need_resched()) {
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200454 trace_power_start(POWER_CSTATE, 1, smp_processor_id());
Thomas Renninger25e41932011-01-03 17:50:44 +0100455 trace_cpu_idle(1, smp_processor_id());
Christoph Lameter349c0042011-03-12 12:50:10 +0100456 if (this_cpu_has(X86_FEATURE_CLFLUSH_MONITOR))
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200457 clflush((void *)&current_thread_info()->flags);
458
459 __monitor((void *)&current_thread_info()->flags, 0, 0);
460 smp_mb();
461 if (!need_resched())
462 __sti_mwait(0, 0);
463 else
464 local_irq_enable();
Thomas Renningerf77cfe42011-01-07 11:29:44 +0100465 trace_power_end(smp_processor_id());
466 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200467 } else
468 local_irq_enable();
469}
470
471/*
472 * On SMP it's slightly faster (but much more power-consuming!)
473 * to poll the ->work.need_resched flag instead of waiting for the
474 * cross-CPU IPI to arrive. Use this option with caution.
475 */
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200476static void poll_idle(void)
477{
Thomas Renninger6f4f2722010-04-20 13:17:36 +0200478 trace_power_start(POWER_CSTATE, 0, smp_processor_id());
Thomas Renninger25e41932011-01-03 17:50:44 +0100479 trace_cpu_idle(0, smp_processor_id());
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200480 local_irq_enable();
481 while (!need_resched())
482 cpu_relax();
Thomas Renninger25e41932011-01-03 17:50:44 +0100483 trace_power_end(smp_processor_id());
484 trace_cpu_idle(PWR_EVENT_EXIT, smp_processor_id());
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200485}
486
487/*
488 * mwait selection logic:
489 *
490 * It depends on the CPU. For AMD CPUs that support MWAIT this is
491 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
492 * then depend on a clock divisor and current Pstate of the core. If
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200493 * all cores of a processor are in halt state (C1) the processor can
494 * enter the C1E (C1 enhanced) state. If mwait is used this will never
495 * happen.
496 *
497 * idle=mwait overrides this decision and forces the usage of mwait.
498 */
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200499
500#define MWAIT_INFO 0x05
501#define MWAIT_ECX_EXTENDED_INFO 0x01
502#define MWAIT_EDX_C1 0xf0
503
Borislav Petkov1c9d16e2011-02-11 18:17:54 +0100504int mwait_usable(const struct cpuinfo_x86 *c)
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200505{
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200506 u32 eax, ebx, ecx, edx;
507
Thomas Renningerd1896042010-11-03 17:06:14 +0100508 if (boot_option_idle_override == IDLE_FORCE_MWAIT)
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200509 return 1;
510
Thomas Gleixner09fd4b42008-06-09 18:04:27 +0200511 if (c->cpuid_level < MWAIT_INFO)
512 return 0;
513
514 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
515 /* Check, whether EDX has extended info about MWAIT */
516 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
517 return 1;
518
519 /*
520 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
521 * C1 supports MWAIT
522 */
523 return (edx & MWAIT_EDX_C1);
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200524}
525
Len Brown02c68a02011-04-01 16:59:53 -0400526bool amd_e400_c1e_detected;
527EXPORT_SYMBOL(amd_e400_c1e_detected);
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200528
Len Brown02c68a02011-04-01 16:59:53 -0400529static cpumask_var_t amd_e400_c1e_mask;
Thomas Gleixner4faac972008-09-22 18:54:29 +0200530
Len Brown02c68a02011-04-01 16:59:53 -0400531void amd_e400_remove_cpu(int cpu)
Thomas Gleixner4faac972008-09-22 18:54:29 +0200532{
Len Brown02c68a02011-04-01 16:59:53 -0400533 if (amd_e400_c1e_mask != NULL)
534 cpumask_clear_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner4faac972008-09-22 18:54:29 +0200535}
536
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200537/*
Len Brown02c68a02011-04-01 16:59:53 -0400538 * AMD Erratum 400 aware idle routine. We check for C1E active in the interrupt
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200539 * pending message MSR. If we detect C1E, then we handle it the same
540 * way as C3 power states (local apic timer and TSC stop)
541 */
Len Brown02c68a02011-04-01 16:59:53 -0400542static void amd_e400_idle(void)
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200543{
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200544 if (need_resched())
545 return;
546
Len Brown02c68a02011-04-01 16:59:53 -0400547 if (!amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200548 u32 lo, hi;
549
550 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
Michal Schmidte8c534e2010-07-27 18:53:35 +0200551
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200552 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
Len Brown02c68a02011-04-01 16:59:53 -0400553 amd_e400_c1e_detected = true;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800554 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Andreas Herrmann09bfeea2008-09-18 21:12:10 +0200555 mark_tsc_unstable("TSC halt in AMD C1E");
556 printk(KERN_INFO "System has AMD C1E enabled\n");
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200557 }
558 }
559
Len Brown02c68a02011-04-01 16:59:53 -0400560 if (amd_e400_c1e_detected) {
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200561 int cpu = smp_processor_id();
562
Len Brown02c68a02011-04-01 16:59:53 -0400563 if (!cpumask_test_cpu(cpu, amd_e400_c1e_mask)) {
564 cpumask_set_cpu(cpu, amd_e400_c1e_mask);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200565 /*
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700566 * Force broadcast so ACPI can not interfere.
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200567 */
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200568 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
569 &cpu);
570 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
571 cpu);
572 }
573 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200574
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200575 default_idle();
Thomas Gleixner0beefa22008-06-17 09:12:03 +0200576
577 /*
578 * The switch back from broadcast mode needs to be
579 * called with interrupts disabled.
580 */
581 local_irq_disable();
582 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
583 local_irq_enable();
Thomas Gleixneraa276e12008-06-09 19:15:00 +0200584 } else
585 default_idle();
586}
587
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200588void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
589{
Ingo Molnar3e5095d2009-01-27 17:07:08 +0100590#ifdef CONFIG_SMP
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200591 if (pm_idle == poll_idle && smp_num_siblings > 1) {
Mike Travisd6dd6922010-03-05 13:10:38 -0600592 printk_once(KERN_WARNING "WARNING: polling idle and HT enabled,"
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200593 " performance may degrade.\n");
594 }
595#endif
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200596 if (pm_idle)
597 return;
598
Thomas Gleixnere9623b32008-05-16 22:55:26 +0200599 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200600 /*
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200601 * One CPU supports mwait => All CPUs supports mwait
602 */
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200603 printk(KERN_INFO "using mwait in idle threads.\n");
604 pm_idle = mwait_idle;
Hans Rosenfeld9d8888c2010-07-28 19:09:31 +0200605 } else if (cpu_has_amd_erratum(amd_erratum_400)) {
606 /* E400: APIC timer interrupt does not wake up CPU from C1e */
Len Brown02c68a02011-04-01 16:59:53 -0400607 printk(KERN_INFO "using AMD E400 aware idle routine\n");
608 pm_idle = amd_e400_idle;
Thomas Gleixner6ddd2a22008-06-09 16:59:53 +0200609 } else
610 pm_idle = default_idle;
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200611}
612
Len Brown02c68a02011-04-01 16:59:53 -0400613void __init init_amd_e400_c1e_mask(void)
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030614{
Len Brown02c68a02011-04-01 16:59:53 -0400615 /* If we're using amd_e400_idle, we need to allocate amd_e400_c1e_mask. */
616 if (pm_idle == amd_e400_idle)
617 zalloc_cpumask_var(&amd_e400_c1e_mask, GFP_KERNEL);
Rusty Russell30e1e6d2009-03-17 14:50:34 +1030618}
619
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200620static int __init idle_setup(char *str)
621{
Cyrill Gorcunovab6bc3e2008-07-05 15:53:36 +0400622 if (!str)
623 return -EINVAL;
624
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200625 if (!strcmp(str, "poll")) {
626 printk("using polling idle threads.\n");
627 pm_idle = poll_idle;
Thomas Renningerd1896042010-11-03 17:06:14 +0100628 boot_option_idle_override = IDLE_POLL;
629 } else if (!strcmp(str, "mwait")) {
630 boot_option_idle_override = IDLE_FORCE_MWAIT;
Linus Torvaldsaf0d6a02011-06-01 02:07:22 +0900631 WARN_ONCE(1, "\"idle=mwait\" will be removed in 2012\n");
Thomas Renningerd1896042010-11-03 17:06:14 +0100632 } else if (!strcmp(str, "halt")) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800633 /*
634 * When the boot option of idle=halt is added, halt is
635 * forced to be used for CPU idle. In such case CPU C2/C3
636 * won't be used again.
637 * To continue to load the CPU idle driver, don't touch
638 * the boot_option_idle_override.
639 */
640 pm_idle = default_idle;
Thomas Renningerd1896042010-11-03 17:06:14 +0100641 boot_option_idle_override = IDLE_HALT;
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800642 } else if (!strcmp(str, "nomwait")) {
643 /*
644 * If the boot option of "idle=nomwait" is added,
645 * it means that mwait will be disabled for CPU C2/C3
646 * states. In such case it won't touch the variable
647 * of boot_option_idle_override.
648 */
Thomas Renningerd1896042010-11-03 17:06:14 +0100649 boot_option_idle_override = IDLE_NOMWAIT;
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800650 } else
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200651 return -1;
652
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200653 return 0;
654}
655early_param("idle", idle_setup);
656
Amerigo Wang9d62dcd2009-05-11 22:05:28 -0400657unsigned long arch_align_stack(unsigned long sp)
658{
659 if (!(current->personality & ADDR_NO_RANDOMIZE) && randomize_va_space)
660 sp -= get_random_int() % 8192;
661 return sp & ~0xf;
662}
663
664unsigned long arch_randomize_brk(struct mm_struct *mm)
665{
666 unsigned long range_end = mm->brk + 0x02000000;
667 return randomize_range(mm->brk, range_end, 0) ? : mm->brk;
668}
669