1 #include <linux/errno.h>
2 #include <linux/kernel.h>
5 #include <linux/slab.h>
6 #include <linux/sched.h>
7 #include <linux/module.h>
9 #include <linux/clockchips.h>
11 struct kmem_cache
*task_xstate_cachep
;
13 int arch_dup_task_struct(struct task_struct
*dst
, struct task_struct
*src
)
16 if (src
->thread
.xstate
) {
17 dst
->thread
.xstate
= kmem_cache_alloc(task_xstate_cachep
,
19 if (!dst
->thread
.xstate
)
21 WARN_ON((unsigned long)dst
->thread
.xstate
& 15);
22 memcpy(dst
->thread
.xstate
, src
->thread
.xstate
, xstate_size
);
27 void free_thread_xstate(struct task_struct
*tsk
)
29 if (tsk
->thread
.xstate
) {
30 kmem_cache_free(task_xstate_cachep
, tsk
->thread
.xstate
);
31 tsk
->thread
.xstate
= NULL
;
35 void free_thread_info(struct thread_info
*ti
)
37 free_thread_xstate(ti
->task
);
38 free_pages((unsigned long)ti
, get_order(THREAD_SIZE
));
41 void arch_task_cache_init(void)
44 kmem_cache_create("task_xstate", xstate_size
,
45 __alignof__(union thread_xstate
),
50 * Idle related variables and functions
52 unsigned long boot_option_idle_override
= 0;
53 EXPORT_SYMBOL(boot_option_idle_override
);
56 * Powermanagement idle function, if any..
58 void (*pm_idle
)(void);
59 EXPORT_SYMBOL(pm_idle
);
63 * This halt magic was a workaround for ancient floppy DMA
64 * wreckage. It should be safe to remove.
66 static int hlt_counter
;
67 void disable_hlt(void)
71 EXPORT_SYMBOL(disable_hlt
);
77 EXPORT_SYMBOL(enable_hlt
);
79 static inline int hlt_use_halt(void)
81 return (!hlt_counter
&& boot_cpu_data
.hlt_works_ok
);
84 static inline int hlt_use_halt(void)
91 * We use this if we don't have any better
94 void default_idle(void)
97 current_thread_info()->status
&= ~TS_POLLING
;
99 * TS_POLLING-cleared state must be visible before we
105 safe_halt(); /* enables interrupts racelessly */
108 current_thread_info()->status
|= TS_POLLING
;
111 /* loop is done by the caller */
115 #ifdef CONFIG_APM_MODULE
116 EXPORT_SYMBOL(default_idle
);
119 static void do_nothing(void *unused
)
124 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
125 * pm_idle and update to new pm_idle value. Required while changing pm_idle
126 * handler on SMP systems.
128 * Caller must have changed pm_idle to the new value before the call. Old
129 * pm_idle value will not be used by any CPU after the return of this function.
131 void cpu_idle_wait(void)
134 /* kick all the CPUs so that they exit out of pm_idle */
135 smp_call_function(do_nothing
, NULL
, 0, 1);
137 EXPORT_SYMBOL_GPL(cpu_idle_wait
);
140 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
141 * which can obviate IPI to trigger checking of need_resched.
142 * We execute MONITOR against need_resched and enter optimized wait state
143 * through MWAIT. Whenever someone changes need_resched, we would be woken
144 * up from MWAIT (without an IPI).
146 * New with Core Duo processors, MWAIT can take some hints based on CPU
149 void mwait_idle_with_hints(unsigned long ax
, unsigned long cx
)
151 if (!need_resched()) {
152 __monitor((void *)¤t_thread_info()->flags
, 0, 0);
159 /* Default MONITOR/MWAIT with no hints, used for default C1 state */
160 static void mwait_idle(void)
162 if (!need_resched()) {
163 __monitor((void *)¤t_thread_info()->flags
, 0, 0);
174 * On SMP it's slightly faster (but much more power-consuming!)
175 * to poll the ->work.need_resched flag instead of waiting for the
176 * cross-CPU IPI to arrive. Use this option with caution.
178 static void poll_idle(void)
185 * mwait selection logic:
187 * It depends on the CPU. For AMD CPUs that support MWAIT this is
188 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
189 * then depend on a clock divisor and current Pstate of the core. If
190 * all cores of a processor are in halt state (C1) the processor can
191 * enter the C1E (C1 enhanced) state. If mwait is used this will never
194 * idle=mwait overrides this decision and forces the usage of mwait.
197 #define MWAIT_INFO 0x05
198 #define MWAIT_ECX_EXTENDED_INFO 0x01
199 #define MWAIT_EDX_C1 0xf0
201 static int __cpuinit
mwait_usable(const struct cpuinfo_x86
*c
)
203 u32 eax
, ebx
, ecx
, edx
;
208 if (c
->cpuid_level
< MWAIT_INFO
)
211 cpuid(MWAIT_INFO
, &eax
, &ebx
, &ecx
, &edx
);
212 /* Check, whether EDX has extended info about MWAIT */
213 if (!(ecx
& MWAIT_ECX_EXTENDED_INFO
))
217 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
220 return (edx
& MWAIT_EDX_C1
);
224 * Check for AMD CPUs, which have potentially C1E support
226 static int __cpuinit
check_c1e_idle(const struct cpuinfo_x86
*c
)
228 if (c
->x86_vendor
!= X86_VENDOR_AMD
)
234 /* Family 0x0f models < rev F do not have C1E */
235 if (c
->x86
== 0x0f && c
->x86_model
< 0x40)
242 * C1E aware idle routine. We check for C1E active in the interrupt
243 * pending message MSR. If we detect C1E, then we handle it the same
244 * way as C3 power states (local apic timer and TSC stop)
246 static void c1e_idle(void)
248 static cpumask_t c1e_mask
= CPU_MASK_NONE
;
249 static int c1e_detected
;
257 rdmsr(MSR_K8_INT_PENDING_MSG
, lo
, hi
);
258 if (lo
& K8_INTP_C1E_ACTIVE_MASK
) {
260 mark_tsc_unstable("TSC halt in C1E");
261 printk(KERN_INFO
"System has C1E enabled\n");
266 int cpu
= smp_processor_id();
268 if (!cpu_isset(cpu
, c1e_mask
)) {
269 cpu_set(cpu
, c1e_mask
);
271 * Force broadcast so ACPI can not interfere. Needs
272 * to run with interrupts enabled as it uses
276 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE
,
278 printk(KERN_INFO
"Switch to broadcast mode on CPU%d\n",
282 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER
, &cpu
);
287 * The switch back from broadcast mode needs to be
288 * called with interrupts disabled.
291 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT
, &cpu
);
297 void __cpuinit
select_idle_routine(const struct cpuinfo_x86
*c
)
299 #ifdef CONFIG_X86_SMP
300 if (pm_idle
== poll_idle
&& smp_num_siblings
> 1) {
301 printk(KERN_WARNING
"WARNING: polling idle and HT enabled,"
302 " performance may degrade.\n");
308 if (cpu_has(c
, X86_FEATURE_MWAIT
) && mwait_usable(c
)) {
310 * One CPU supports mwait => All CPUs supports mwait
312 printk(KERN_INFO
"using mwait in idle threads.\n");
313 pm_idle
= mwait_idle
;
314 } else if (check_c1e_idle(c
)) {
315 printk(KERN_INFO
"using C1E aware idle routine\n");
318 pm_idle
= default_idle
;
321 static int __init
idle_setup(char *str
)
323 if (!strcmp(str
, "poll")) {
324 printk("using polling idle threads.\n");
326 } else if (!strcmp(str
, "mwait"))
331 boot_option_idle_override
= 1;
334 early_param("idle", idle_setup
);