1 #include <linux/errno.h>
2 #include <linux/kernel.h>
5 #include <linux/prctl.h>
6 #include <linux/slab.h>
7 #include <linux/sched.h>
8 #include <linux/module.h>
10 #include <linux/clockchips.h>
11 #include <trace/power.h>
12 #include <asm/system.h>
15 #include <asm/uaccess.h>
18 unsigned long idle_halt;
19 EXPORT_SYMBOL(idle_halt);
20 unsigned long idle_nomwait;
21 EXPORT_SYMBOL(idle_nomwait);
23 struct kmem_cache *task_xstate_cachep;
25 DEFINE_TRACE(power_start);
26 DEFINE_TRACE(power_end);
28 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
31 if (src->thread.xstate) {
32 dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
34 if (!dst->thread.xstate)
36 WARN_ON((unsigned long)dst->thread.xstate & 15);
37 memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
42 void free_thread_xstate(struct task_struct *tsk)
44 if (tsk->thread.xstate) {
45 kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
46 tsk->thread.xstate = NULL;
50 void free_thread_info(struct thread_info *ti)
52 free_thread_xstate(ti->task);
53 free_pages((unsigned long)ti, get_order(THREAD_SIZE));
56 void arch_task_cache_init(void)
59 kmem_cache_create("task_xstate", xstate_size,
60 __alignof__(union thread_xstate),
65 * Free current thread data structures etc..
67 void exit_thread(void)
69 struct task_struct *me = current;
70 struct thread_struct *t = &me->thread;
72 if (me->thread.io_bitmap_ptr) {
73 struct tss_struct *tss = &per_cpu(init_tss, get_cpu());
75 kfree(t->io_bitmap_ptr);
76 t->io_bitmap_ptr = NULL;
77 clear_thread_flag(TIF_IO_BITMAP);
79 * Careful, clear this in the TSS too:
81 memset(tss->io_bitmap, 0xff, t->io_bitmap_max);
86 ds_exit_thread(current);
89 void flush_thread(void)
91 struct task_struct *tsk = current;
94 if (test_tsk_thread_flag(tsk, TIF_ABI_PENDING)) {
95 clear_tsk_thread_flag(tsk, TIF_ABI_PENDING);
96 if (test_tsk_thread_flag(tsk, TIF_IA32)) {
97 clear_tsk_thread_flag(tsk, TIF_IA32);
99 set_tsk_thread_flag(tsk, TIF_IA32);
100 current_thread_info()->status |= TS_COMPAT;
105 clear_tsk_thread_flag(tsk, TIF_DEBUG);
107 tsk->thread.debugreg0 = 0;
108 tsk->thread.debugreg1 = 0;
109 tsk->thread.debugreg2 = 0;
110 tsk->thread.debugreg3 = 0;
111 tsk->thread.debugreg6 = 0;
112 tsk->thread.debugreg7 = 0;
113 memset(tsk->thread.tls_array, 0, sizeof(tsk->thread.tls_array));
115 * Forget coprocessor state..
117 tsk->fpu_counter = 0;
122 static void hard_disable_TSC(void)
124 write_cr4(read_cr4() | X86_CR4_TSD);
127 void disable_TSC(void)
130 if (!test_and_set_thread_flag(TIF_NOTSC))
132 * Must flip the CPU state synchronously with
133 * TIF_NOTSC in the current running context.
139 static void hard_enable_TSC(void)
141 write_cr4(read_cr4() & ~X86_CR4_TSD);
144 static void enable_TSC(void)
147 if (test_and_clear_thread_flag(TIF_NOTSC))
149 * Must flip the CPU state synchronously with
150 * TIF_NOTSC in the current running context.
156 int get_tsc_mode(unsigned long adr)
160 if (test_thread_flag(TIF_NOTSC))
161 val = PR_TSC_SIGSEGV;
165 return put_user(val, (unsigned int __user *)adr);
168 int set_tsc_mode(unsigned int val)
170 if (val == PR_TSC_SIGSEGV)
172 else if (val == PR_TSC_ENABLE)
180 void __switch_to_xtra(struct task_struct *prev_p, struct task_struct *next_p,
181 struct tss_struct *tss)
183 struct thread_struct *prev, *next;
185 prev = &prev_p->thread;
186 next = &next_p->thread;
188 if (test_tsk_thread_flag(next_p, TIF_DS_AREA_MSR) ||
189 test_tsk_thread_flag(prev_p, TIF_DS_AREA_MSR))
190 ds_switch_to(prev_p, next_p);
191 else if (next->debugctlmsr != prev->debugctlmsr)
192 update_debugctlmsr(next->debugctlmsr);
194 if (test_tsk_thread_flag(next_p, TIF_DEBUG)) {
195 set_debugreg(next->debugreg0, 0);
196 set_debugreg(next->debugreg1, 1);
197 set_debugreg(next->debugreg2, 2);
198 set_debugreg(next->debugreg3, 3);
200 set_debugreg(next->debugreg6, 6);
201 set_debugreg(next->debugreg7, 7);
204 if (test_tsk_thread_flag(prev_p, TIF_NOTSC) ^
205 test_tsk_thread_flag(next_p, TIF_NOTSC)) {
206 /* prev and next are different */
207 if (test_tsk_thread_flag(next_p, TIF_NOTSC))
213 if (test_tsk_thread_flag(next_p, TIF_IO_BITMAP)) {
215 * Copy the relevant range of the IO bitmap.
216 * Normally this is 128 bytes or less:
218 memcpy(tss->io_bitmap, next->io_bitmap_ptr,
219 max(prev->io_bitmap_max, next->io_bitmap_max));
220 } else if (test_tsk_thread_flag(prev_p, TIF_IO_BITMAP)) {
222 * Clear any possible leftover bits:
224 memset(tss->io_bitmap, 0xff, prev->io_bitmap_max);
228 int sys_fork(struct pt_regs *regs)
230 return do_fork(SIGCHLD, regs->sp, regs, 0, NULL, NULL);
234 * This is trivial, and on the face of it looks like it
235 * could equally well be done in user mode.
237 * Not so, for quite unobvious reasons - register pressure.
238 * In user mode vfork() cannot have a stack frame, and if
239 * done by calling the "clone()" system call directly, you
240 * do not have enough call-clobbered registers to hold all
241 * the information you need.
243 int sys_vfork(struct pt_regs *regs)
245 return do_fork(CLONE_VFORK | CLONE_VM | SIGCHLD, regs->sp, regs, 0,
251 * Idle related variables and functions
253 unsigned long boot_option_idle_override = 0;
254 EXPORT_SYMBOL(boot_option_idle_override);
257 * Powermanagement idle function, if any..
259 void (*pm_idle)(void);
260 EXPORT_SYMBOL(pm_idle);
264 * This halt magic was a workaround for ancient floppy DMA
265 * wreckage. It should be safe to remove.
267 static int hlt_counter;
268 void disable_hlt(void)
272 EXPORT_SYMBOL(disable_hlt);
274 void enable_hlt(void)
278 EXPORT_SYMBOL(enable_hlt);
280 static inline int hlt_use_halt(void)
282 return (!hlt_counter && boot_cpu_data.hlt_works_ok);
285 static inline int hlt_use_halt(void)
292 * We use this if we don't have any better
295 void default_idle(void)
297 if (hlt_use_halt()) {
298 struct power_trace it;
300 trace_power_start(&it, POWER_CSTATE, 1);
301 current_thread_info()->status &= ~TS_POLLING;
303 * TS_POLLING-cleared state must be visible before we
309 safe_halt(); /* enables interrupts racelessly */
312 current_thread_info()->status |= TS_POLLING;
313 trace_power_end(&it);
316 /* loop is done by the caller */
320 #ifdef CONFIG_APM_MODULE
321 EXPORT_SYMBOL(default_idle);
324 void stop_this_cpu(void *dummy)
330 cpu_clear(smp_processor_id(), cpu_online_map);
331 disable_local_APIC();
334 if (hlt_works(smp_processor_id()))
339 static void do_nothing(void *unused)
344 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
345 * pm_idle and update to new pm_idle value. Required while changing pm_idle
346 * handler on SMP systems.
348 * Caller must have changed pm_idle to the new value before the call. Old
349 * pm_idle value will not be used by any CPU after the return of this function.
351 void cpu_idle_wait(void)
354 /* kick all the CPUs so that they exit out of pm_idle */
355 smp_call_function(do_nothing, NULL, 1);
357 EXPORT_SYMBOL_GPL(cpu_idle_wait);
360 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
361 * which can obviate IPI to trigger checking of need_resched.
362 * We execute MONITOR against need_resched and enter optimized wait state
363 * through MWAIT. Whenever someone changes need_resched, we would be woken
364 * up from MWAIT (without an IPI).
366 * New with Core Duo processors, MWAIT can take some hints based on CPU
369 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
371 struct power_trace it;
373 trace_power_start(&it, POWER_CSTATE, (ax>>4)+1);
374 if (!need_resched()) {
375 if (cpu_has(¤t_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
376 clflush((void *)¤t_thread_info()->flags);
378 __monitor((void *)¤t_thread_info()->flags, 0, 0);
383 trace_power_end(&it);
386 /* Default MONITOR/MWAIT with no hints, used for default C1 state */
387 static void mwait_idle(void)
389 struct power_trace it;
390 if (!need_resched()) {
391 trace_power_start(&it, POWER_CSTATE, 1);
392 if (cpu_has(¤t_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
393 clflush((void *)¤t_thread_info()->flags);
395 __monitor((void *)¤t_thread_info()->flags, 0, 0);
401 trace_power_end(&it);
407 * On SMP it's slightly faster (but much more power-consuming!)
408 * to poll the ->work.need_resched flag instead of waiting for the
409 * cross-CPU IPI to arrive. Use this option with caution.
411 static void poll_idle(void)
413 struct power_trace it;
415 trace_power_start(&it, POWER_CSTATE, 0);
417 while (!need_resched())
419 trace_power_end(&it);
423 * mwait selection logic:
425 * It depends on the CPU. For AMD CPUs that support MWAIT this is
426 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
427 * then depend on a clock divisor and current Pstate of the core. If
428 * all cores of a processor are in halt state (C1) the processor can
429 * enter the C1E (C1 enhanced) state. If mwait is used this will never
432 * idle=mwait overrides this decision and forces the usage of mwait.
434 static int __cpuinitdata force_mwait;
436 #define MWAIT_INFO 0x05
437 #define MWAIT_ECX_EXTENDED_INFO 0x01
438 #define MWAIT_EDX_C1 0xf0
440 static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
442 u32 eax, ebx, ecx, edx;
447 if (c->cpuid_level < MWAIT_INFO)
450 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
451 /* Check, whether EDX has extended info about MWAIT */
452 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
456 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
459 return (edx & MWAIT_EDX_C1);
463 * Check for AMD CPUs, which have potentially C1E support
465 static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
467 if (c->x86_vendor != X86_VENDOR_AMD)
473 /* Family 0x0f models < rev F do not have C1E */
474 if (c->x86 == 0x0f && c->x86_model < 0x40)
480 static cpumask_t c1e_mask = CPU_MASK_NONE;
481 static int c1e_detected;
483 void c1e_remove_cpu(int cpu)
485 cpu_clear(cpu, c1e_mask);
489 * C1E aware idle routine. We check for C1E active in the interrupt
490 * pending message MSR. If we detect C1E, then we handle it the same
491 * way as C3 power states (local apic timer and TSC stop)
493 static void c1e_idle(void)
501 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
502 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
504 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
505 mark_tsc_unstable("TSC halt in AMD C1E");
506 printk(KERN_INFO "System has AMD C1E enabled\n");
507 set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
512 int cpu = smp_processor_id();
514 if (!cpu_isset(cpu, c1e_mask)) {
515 cpu_set(cpu, c1e_mask);
517 * Force broadcast so ACPI can not interfere. Needs
518 * to run with interrupts enabled as it uses
522 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
524 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
528 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
533 * The switch back from broadcast mode needs to be
534 * called with interrupts disabled.
537 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
543 void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
546 if (pm_idle == poll_idle && smp_num_siblings > 1) {
547 printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
548 " performance may degrade.\n");
554 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
556 * One CPU supports mwait => All CPUs supports mwait
558 printk(KERN_INFO "using mwait in idle threads.\n");
559 pm_idle = mwait_idle;
560 } else if (check_c1e_idle(c)) {
561 printk(KERN_INFO "using C1E aware idle routine\n");
564 pm_idle = default_idle;
567 static int __init idle_setup(char *str)
572 if (!strcmp(str, "poll")) {
573 printk("using polling idle threads.\n");
575 } else if (!strcmp(str, "mwait"))
577 else if (!strcmp(str, "halt")) {
579 * When the boot option of idle=halt is added, halt is
580 * forced to be used for CPU idle. In such case CPU C2/C3
581 * won't be used again.
582 * To continue to load the CPU idle driver, don't touch
583 * the boot_option_idle_override.
585 pm_idle = default_idle;
588 } else if (!strcmp(str, "nomwait")) {
590 * If the boot option of "idle=nomwait" is added,
591 * it means that mwait will be disabled for CPU C2/C3
592 * states. In such case it won't touch the variable
593 * of boot_option_idle_override.
600 boot_option_idle_override = 1;
603 early_param("idle", idle_setup);