1 #include <linux/errno.h>
2 #include <linux/kernel.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 unsigned long idle_halt;
16 EXPORT_SYMBOL(idle_halt);
17 unsigned long idle_nomwait;
18 EXPORT_SYMBOL(idle_nomwait);
20 struct kmem_cache *task_xstate_cachep;
22 DEFINE_TRACE(power_start);
23 DEFINE_TRACE(power_end);
25 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
28 if (src->thread.xstate) {
29 dst->thread.xstate = kmem_cache_alloc(task_xstate_cachep,
31 if (!dst->thread.xstate)
33 WARN_ON((unsigned long)dst->thread.xstate & 15);
34 memcpy(dst->thread.xstate, src->thread.xstate, xstate_size);
39 void free_thread_xstate(struct task_struct *tsk)
41 if (tsk->thread.xstate) {
42 kmem_cache_free(task_xstate_cachep, tsk->thread.xstate);
43 tsk->thread.xstate = NULL;
47 void free_thread_info(struct thread_info *ti)
49 free_thread_xstate(ti->task);
50 free_pages((unsigned long)ti, get_order(THREAD_SIZE));
53 void arch_task_cache_init(void)
56 kmem_cache_create("task_xstate", xstate_size,
57 __alignof__(union thread_xstate),
62 * Idle related variables and functions
64 unsigned long boot_option_idle_override = 0;
65 EXPORT_SYMBOL(boot_option_idle_override);
68 * Powermanagement idle function, if any..
70 void (*pm_idle)(void);
71 EXPORT_SYMBOL(pm_idle);
75 * This halt magic was a workaround for ancient floppy DMA
76 * wreckage. It should be safe to remove.
78 static int hlt_counter;
79 void disable_hlt(void)
83 EXPORT_SYMBOL(disable_hlt);
89 EXPORT_SYMBOL(enable_hlt);
91 static inline int hlt_use_halt(void)
93 return (!hlt_counter && boot_cpu_data.hlt_works_ok);
96 static inline int hlt_use_halt(void)
103 * We use this if we don't have any better
106 void default_idle(void)
108 if (hlt_use_halt()) {
109 struct power_trace it;
111 trace_power_start(&it, POWER_CSTATE, 1);
112 current_thread_info()->status &= ~TS_POLLING;
114 * TS_POLLING-cleared state must be visible before we
120 safe_halt(); /* enables interrupts racelessly */
123 current_thread_info()->status |= TS_POLLING;
124 trace_power_end(&it);
127 /* loop is done by the caller */
131 #ifdef CONFIG_APM_MODULE
132 EXPORT_SYMBOL(default_idle);
135 void stop_this_cpu(void *dummy)
141 cpu_clear(smp_processor_id(), cpu_online_map);
142 disable_local_APIC();
145 if (hlt_works(smp_processor_id()))
150 static void do_nothing(void *unused)
155 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
156 * pm_idle and update to new pm_idle value. Required while changing pm_idle
157 * handler on SMP systems.
159 * Caller must have changed pm_idle to the new value before the call. Old
160 * pm_idle value will not be used by any CPU after the return of this function.
162 void cpu_idle_wait(void)
165 /* kick all the CPUs so that they exit out of pm_idle */
166 smp_call_function(do_nothing, NULL, 1);
168 EXPORT_SYMBOL_GPL(cpu_idle_wait);
171 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
172 * which can obviate IPI to trigger checking of need_resched.
173 * We execute MONITOR against need_resched and enter optimized wait state
174 * through MWAIT. Whenever someone changes need_resched, we would be woken
175 * up from MWAIT (without an IPI).
177 * New with Core Duo processors, MWAIT can take some hints based on CPU
180 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
182 struct power_trace it;
184 trace_power_start(&it, POWER_CSTATE, (ax>>4)+1);
185 if (!need_resched()) {
186 if (cpu_has(¤t_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
187 clflush((void *)¤t_thread_info()->flags);
189 __monitor((void *)¤t_thread_info()->flags, 0, 0);
194 trace_power_end(&it);
197 /* Default MONITOR/MWAIT with no hints, used for default C1 state */
198 static void mwait_idle(void)
200 struct power_trace it;
201 if (!need_resched()) {
202 trace_power_start(&it, POWER_CSTATE, 1);
203 if (cpu_has(¤t_cpu_data, X86_FEATURE_CLFLUSH_MONITOR))
204 clflush((void *)¤t_thread_info()->flags);
206 __monitor((void *)¤t_thread_info()->flags, 0, 0);
212 trace_power_end(&it);
218 * On SMP it's slightly faster (but much more power-consuming!)
219 * to poll the ->work.need_resched flag instead of waiting for the
220 * cross-CPU IPI to arrive. Use this option with caution.
222 static void poll_idle(void)
224 struct power_trace it;
226 trace_power_start(&it, POWER_CSTATE, 0);
228 while (!need_resched())
230 trace_power_end(&it);
234 * mwait selection logic:
236 * It depends on the CPU. For AMD CPUs that support MWAIT this is
237 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
238 * then depend on a clock divisor and current Pstate of the core. If
239 * all cores of a processor are in halt state (C1) the processor can
240 * enter the C1E (C1 enhanced) state. If mwait is used this will never
243 * idle=mwait overrides this decision and forces the usage of mwait.
245 static int __cpuinitdata force_mwait;
247 #define MWAIT_INFO 0x05
248 #define MWAIT_ECX_EXTENDED_INFO 0x01
249 #define MWAIT_EDX_C1 0xf0
251 static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
253 u32 eax, ebx, ecx, edx;
258 if (c->cpuid_level < MWAIT_INFO)
261 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
262 /* Check, whether EDX has extended info about MWAIT */
263 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
267 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
270 return (edx & MWAIT_EDX_C1);
274 * Check for AMD CPUs, which have potentially C1E support
276 static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
278 if (c->x86_vendor != X86_VENDOR_AMD)
284 /* Family 0x0f models < rev F do not have C1E */
285 if (c->x86 == 0x0f && c->x86_model < 0x40)
291 static cpumask_t c1e_mask = CPU_MASK_NONE;
292 static int c1e_detected;
294 void c1e_remove_cpu(int cpu)
296 cpu_clear(cpu, c1e_mask);
300 * C1E aware idle routine. We check for C1E active in the interrupt
301 * pending message MSR. If we detect C1E, then we handle it the same
302 * way as C3 power states (local apic timer and TSC stop)
304 static void c1e_idle(void)
312 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
313 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
315 if (!boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
316 mark_tsc_unstable("TSC halt in AMD C1E");
317 printk(KERN_INFO "System has AMD C1E enabled\n");
318 set_cpu_cap(&boot_cpu_data, X86_FEATURE_AMDC1E);
323 int cpu = smp_processor_id();
325 if (!cpu_isset(cpu, c1e_mask)) {
326 cpu_set(cpu, c1e_mask);
328 * Force broadcast so ACPI can not interfere. Needs
329 * to run with interrupts enabled as it uses
333 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
335 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
339 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
344 * The switch back from broadcast mode needs to be
345 * called with interrupts disabled.
348 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
354 void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
356 #ifdef CONFIG_X86_SMP
357 if (pm_idle == poll_idle && smp_num_siblings > 1) {
358 printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
359 " performance may degrade.\n");
365 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
367 * One CPU supports mwait => All CPUs supports mwait
369 printk(KERN_INFO "using mwait in idle threads.\n");
370 pm_idle = mwait_idle;
371 } else if (check_c1e_idle(c)) {
372 printk(KERN_INFO "using C1E aware idle routine\n");
375 pm_idle = default_idle;
378 static int __init idle_setup(char *str)
383 if (!strcmp(str, "poll")) {
384 printk("using polling idle threads.\n");
386 } else if (!strcmp(str, "mwait"))
388 else if (!strcmp(str, "halt")) {
390 * When the boot option of idle=halt is added, halt is
391 * forced to be used for CPU idle. In such case CPU C2/C3
392 * won't be used again.
393 * To continue to load the CPU idle driver, don't touch
394 * the boot_option_idle_override.
396 pm_idle = default_idle;
399 } else if (!strcmp(str, "nomwait")) {
401 * If the boot option of "idle=nomwait" is added,
402 * it means that mwait will be disabled for CPU C2/C3
403 * states. In such case it won't touch the variable
404 * of boot_option_idle_override.
411 boot_option_idle_override = 1;
414 early_param("idle", idle_setup);