x86: make poll_idle behave more like the other idle methods
[deliverable/linux.git] / arch / x86 / kernel / process.c
1 #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>
7 #include <linux/module.h>
8 #include <linux/pm.h>
9 #include <linux/clockchips.h>
10 #include <asm/system.h>
11
12 unsigned long idle_halt;
13 EXPORT_SYMBOL(idle_halt);
14 unsigned long idle_nomwait;
15 EXPORT_SYMBOL(idle_nomwait);
16
17 struct kmem_cache *task_xstate_cachep;
18 static int force_mwait __cpuinitdata;
19
20 int arch_dup_task_struct(struct task_struct *dst, struct task_struct *src)
21 {
22 *dst = *src;
23 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 }
31 return 0;
32 }
33
34 void 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
42 void free_thread_info(struct thread_info *ti)
43 {
44 free_thread_xstate(ti->task);
45 free_pages((unsigned long)ti, get_order(THREAD_SIZE));
46 }
47
48 void 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 }
55
56 /*
57 * Idle related variables and functions
58 */
59 unsigned long boot_option_idle_override = 0;
60 EXPORT_SYMBOL(boot_option_idle_override);
61
62 /*
63 * Powermanagement idle function, if any..
64 */
65 void (*pm_idle)(void);
66 EXPORT_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 */
73 static int hlt_counter;
74 void disable_hlt(void)
75 {
76 hlt_counter++;
77 }
78 EXPORT_SYMBOL(disable_hlt);
79
80 void enable_hlt(void)
81 {
82 hlt_counter--;
83 }
84 EXPORT_SYMBOL(enable_hlt);
85
86 static inline int hlt_use_halt(void)
87 {
88 return (!hlt_counter && boot_cpu_data.hlt_works_ok);
89 }
90 #else
91 static 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 */
101 void default_idle(void)
102 {
103 if (hlt_use_halt()) {
104 current_thread_info()->status &= ~TS_POLLING;
105 /*
106 * TS_POLLING-cleared state must be visible before we
107 * test NEED_RESCHED:
108 */
109 smp_mb();
110
111 if (!need_resched())
112 safe_halt(); /* enables interrupts racelessly */
113 else
114 local_irq_enable();
115 current_thread_info()->status |= TS_POLLING;
116 } else {
117 local_irq_enable();
118 /* loop is done by the caller */
119 cpu_relax();
120 }
121 }
122 #ifdef CONFIG_APM_MODULE
123 EXPORT_SYMBOL(default_idle);
124 #endif
125
126 static void do_nothing(void *unused)
127 {
128 }
129
130 /*
131 * cpu_idle_wait - Used to ensure that all the CPUs discard old value of
132 * pm_idle and update to new pm_idle value. Required while changing pm_idle
133 * handler on SMP systems.
134 *
135 * Caller must have changed pm_idle to the new value before the call. Old
136 * pm_idle value will not be used by any CPU after the return of this function.
137 */
138 void cpu_idle_wait(void)
139 {
140 smp_mb();
141 /* kick all the CPUs so that they exit out of pm_idle */
142 smp_call_function(do_nothing, NULL, 1);
143 }
144 EXPORT_SYMBOL_GPL(cpu_idle_wait);
145
146 /*
147 * This uses new MONITOR/MWAIT instructions on P4 processors with PNI,
148 * which can obviate IPI to trigger checking of need_resched.
149 * We execute MONITOR against need_resched and enter optimized wait state
150 * through MWAIT. Whenever someone changes need_resched, we would be woken
151 * up from MWAIT (without an IPI).
152 *
153 * New with Core Duo processors, MWAIT can take some hints based on CPU
154 * capability.
155 */
156 void mwait_idle_with_hints(unsigned long ax, unsigned long cx)
157 {
158 if (!need_resched()) {
159 __monitor((void *)&current_thread_info()->flags, 0, 0);
160 smp_mb();
161 if (!need_resched())
162 __mwait(ax, cx);
163 }
164 }
165
166 /* Default MONITOR/MWAIT with no hints, used for default C1 state */
167 static void mwait_idle(void)
168 {
169 if (!need_resched()) {
170 __monitor((void *)&current_thread_info()->flags, 0, 0);
171 smp_mb();
172 if (!need_resched())
173 __sti_mwait(0, 0);
174 else
175 local_irq_enable();
176 } else
177 local_irq_enable();
178 }
179
180 /*
181 * On SMP it's slightly faster (but much more power-consuming!)
182 * to poll the ->work.need_resched flag instead of waiting for the
183 * cross-CPU IPI to arrive. Use this option with caution.
184 */
185 static void poll_idle(void)
186 {
187 local_irq_enable();
188 while (!need_resched())
189 cpu_relax();
190 }
191
192 /*
193 * mwait selection logic:
194 *
195 * It depends on the CPU. For AMD CPUs that support MWAIT this is
196 * wrong. Family 0x10 and 0x11 CPUs will enter C1 on HLT. Powersavings
197 * then depend on a clock divisor and current Pstate of the core. If
198 * all cores of a processor are in halt state (C1) the processor can
199 * enter the C1E (C1 enhanced) state. If mwait is used this will never
200 * happen.
201 *
202 * idle=mwait overrides this decision and forces the usage of mwait.
203 */
204 static int __cpuinitdata force_mwait;
205
206 #define MWAIT_INFO 0x05
207 #define MWAIT_ECX_EXTENDED_INFO 0x01
208 #define MWAIT_EDX_C1 0xf0
209
210 static int __cpuinit mwait_usable(const struct cpuinfo_x86 *c)
211 {
212 u32 eax, ebx, ecx, edx;
213
214 if (force_mwait)
215 return 1;
216
217 if (c->cpuid_level < MWAIT_INFO)
218 return 0;
219
220 cpuid(MWAIT_INFO, &eax, &ebx, &ecx, &edx);
221 /* Check, whether EDX has extended info about MWAIT */
222 if (!(ecx & MWAIT_ECX_EXTENDED_INFO))
223 return 1;
224
225 /*
226 * edx enumeratios MONITOR/MWAIT extensions. Check, whether
227 * C1 supports MWAIT
228 */
229 return (edx & MWAIT_EDX_C1);
230 }
231
232 /*
233 * Check for AMD CPUs, which have potentially C1E support
234 */
235 static int __cpuinit check_c1e_idle(const struct cpuinfo_x86 *c)
236 {
237 if (c->x86_vendor != X86_VENDOR_AMD)
238 return 0;
239
240 if (c->x86 < 0x0F)
241 return 0;
242
243 /* Family 0x0f models < rev F do not have C1E */
244 if (c->x86 == 0x0f && c->x86_model < 0x40)
245 return 0;
246
247 return 1;
248 }
249
250 /*
251 * C1E aware idle routine. We check for C1E active in the interrupt
252 * pending message MSR. If we detect C1E, then we handle it the same
253 * way as C3 power states (local apic timer and TSC stop)
254 */
255 static void c1e_idle(void)
256 {
257 static cpumask_t c1e_mask = CPU_MASK_NONE;
258 static int c1e_detected;
259
260 if (need_resched())
261 return;
262
263 if (!c1e_detected) {
264 u32 lo, hi;
265
266 rdmsr(MSR_K8_INT_PENDING_MSG, lo, hi);
267 if (lo & K8_INTP_C1E_ACTIVE_MASK) {
268 c1e_detected = 1;
269 mark_tsc_unstable("TSC halt in C1E");
270 printk(KERN_INFO "System has C1E enabled\n");
271 }
272 }
273
274 if (c1e_detected) {
275 int cpu = smp_processor_id();
276
277 if (!cpu_isset(cpu, c1e_mask)) {
278 cpu_set(cpu, c1e_mask);
279 /*
280 * Force broadcast so ACPI can not interfere. Needs
281 * to run with interrupts enabled as it uses
282 * smp_function_call.
283 */
284 local_irq_enable();
285 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_FORCE,
286 &cpu);
287 printk(KERN_INFO "Switch to broadcast mode on CPU%d\n",
288 cpu);
289 local_irq_disable();
290 }
291 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_ENTER, &cpu);
292
293 default_idle();
294
295 /*
296 * The switch back from broadcast mode needs to be
297 * called with interrupts disabled.
298 */
299 local_irq_disable();
300 clockevents_notify(CLOCK_EVT_NOTIFY_BROADCAST_EXIT, &cpu);
301 local_irq_enable();
302 } else
303 default_idle();
304 }
305
306 void __cpuinit select_idle_routine(const struct cpuinfo_x86 *c)
307 {
308 #ifdef CONFIG_X86_SMP
309 if (pm_idle == poll_idle && smp_num_siblings > 1) {
310 printk(KERN_WARNING "WARNING: polling idle and HT enabled,"
311 " performance may degrade.\n");
312 }
313 #endif
314 if (pm_idle)
315 return;
316
317 if (cpu_has(c, X86_FEATURE_MWAIT) && mwait_usable(c)) {
318 /*
319 * One CPU supports mwait => All CPUs supports mwait
320 */
321 printk(KERN_INFO "using mwait in idle threads.\n");
322 pm_idle = mwait_idle;
323 } else if (check_c1e_idle(c)) {
324 printk(KERN_INFO "using C1E aware idle routine\n");
325 pm_idle = c1e_idle;
326 } else
327 pm_idle = default_idle;
328 }
329
330 static int __init idle_setup(char *str)
331 {
332 if (!str)
333 return -EINVAL;
334
335 if (!strcmp(str, "poll")) {
336 printk("using polling idle threads.\n");
337 pm_idle = poll_idle;
338 } else if (!strcmp(str, "mwait"))
339 force_mwait = 1;
340 else if (!strcmp(str, "halt")) {
341 /*
342 * When the boot option of idle=halt is added, halt is
343 * forced to be used for CPU idle. In such case CPU C2/C3
344 * won't be used again.
345 * To continue to load the CPU idle driver, don't touch
346 * the boot_option_idle_override.
347 */
348 pm_idle = default_idle;
349 idle_halt = 1;
350 return 0;
351 } else if (!strcmp(str, "nomwait")) {
352 /*
353 * If the boot option of "idle=nomwait" is added,
354 * it means that mwait will be disabled for CPU C2/C3
355 * states. In such case it won't touch the variable
356 * of boot_option_idle_override.
357 */
358 idle_nomwait = 1;
359 return 0;
360 } else
361 return -1;
362
363 boot_option_idle_override = 1;
364 return 0;
365 }
366 early_param("idle", idle_setup);
367
This page took 0.042185 seconds and 5 git commands to generate.