Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / drivers / acpi / processor_idle.c
blobe3d9de09c4522c3196dd5b5c25d54e262e1a3804
1 /*
2 * processor_idle - idle state submodule to the ACPI processor driver
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6 * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
7 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
9 * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10 * - Added support for C3 on SMP
12 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
28 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/init.h>
34 #include <linux/cpufreq.h>
35 #include <linux/proc_fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/acpi.h>
38 #include <linux/dmi.h>
39 #include <linux/moduleparam.h>
40 #include <linux/sched.h> /* need_resched() */
41 #include <linux/pm_qos_params.h>
42 #include <linux/clockchips.h>
43 #include <linux/cpuidle.h>
46 * Include the apic definitions for x86 to have the APIC timer related defines
47 * available also for UP (on SMP it gets magically included via linux/smp.h).
48 * asm/acpi.h is not an option, as it would require more include magic. Also
49 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
51 #ifdef CONFIG_X86
52 #include <asm/apic.h>
53 #endif
55 #include <asm/io.h>
56 #include <asm/uaccess.h>
58 #include <acpi/acpi_bus.h>
59 #include <acpi/processor.h>
61 #define ACPI_PROCESSOR_COMPONENT 0x01000000
62 #define ACPI_PROCESSOR_CLASS "processor"
63 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
64 ACPI_MODULE_NAME("processor_idle");
65 #define ACPI_PROCESSOR_FILE_POWER "power"
66 #define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
67 #define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
68 #ifndef CONFIG_CPU_IDLE
69 #define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
70 #define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
71 static void (*pm_idle_save) (void) __read_mostly;
72 #else
73 #define C2_OVERHEAD 1 /* 1us */
74 #define C3_OVERHEAD 1 /* 1us */
75 #endif
76 #define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
78 static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
79 #ifdef CONFIG_CPU_IDLE
80 module_param(max_cstate, uint, 0000);
81 #else
82 module_param(max_cstate, uint, 0644);
83 #endif
84 static unsigned int nocst __read_mostly;
85 module_param(nocst, uint, 0000);
87 #ifndef CONFIG_CPU_IDLE
89 * bm_history -- bit-mask with a bit per jiffy of bus-master activity
90 * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
91 * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
92 * 100 HZ: 0x0000000F: 4 jiffies = 40ms
93 * reduce history for more aggressive entry into C3
95 static unsigned int bm_history __read_mostly =
96 (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
97 module_param(bm_history, uint, 0644);
99 static int acpi_processor_set_power_policy(struct acpi_processor *pr);
101 #else /* CONFIG_CPU_IDLE */
102 static unsigned int latency_factor __read_mostly = 2;
103 module_param(latency_factor, uint, 0644);
104 #endif
107 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
108 * For now disable this. Probably a bug somewhere else.
110 * To skip this limit, boot/load with a large max_cstate limit.
112 static int set_max_cstate(const struct dmi_system_id *id)
114 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
115 return 0;
117 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
118 " Override with \"processor.max_cstate=%d\"\n", id->ident,
119 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
121 max_cstate = (long)id->driver_data;
123 return 0;
126 /* Actually this shouldn't be __cpuinitdata, would be better to fix the
127 callers to only run once -AK */
128 static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
129 { set_max_cstate, "IBM ThinkPad R40e", {
130 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
131 DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
132 { set_max_cstate, "IBM ThinkPad R40e", {
133 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
134 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
135 { set_max_cstate, "IBM ThinkPad R40e", {
136 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
137 DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
138 { set_max_cstate, "IBM ThinkPad R40e", {
139 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
140 DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
141 { set_max_cstate, "IBM ThinkPad R40e", {
142 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
143 DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
144 { set_max_cstate, "IBM ThinkPad R40e", {
145 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
146 DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
147 { set_max_cstate, "IBM ThinkPad R40e", {
148 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
149 DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
150 { set_max_cstate, "IBM ThinkPad R40e", {
151 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
152 DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
153 { set_max_cstate, "IBM ThinkPad R40e", {
154 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
155 DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
156 { set_max_cstate, "IBM ThinkPad R40e", {
157 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
158 DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
159 { set_max_cstate, "IBM ThinkPad R40e", {
160 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
161 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
162 { set_max_cstate, "IBM ThinkPad R40e", {
163 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
164 DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
165 { set_max_cstate, "IBM ThinkPad R40e", {
166 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
167 DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
168 { set_max_cstate, "IBM ThinkPad R40e", {
169 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
170 DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
171 { set_max_cstate, "IBM ThinkPad R40e", {
172 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
173 DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
174 { set_max_cstate, "IBM ThinkPad R40e", {
175 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
176 DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
177 { set_max_cstate, "Medion 41700", {
178 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
179 DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
180 { set_max_cstate, "Clevo 5600D", {
181 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
182 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
183 (void *)2},
187 static inline u32 ticks_elapsed(u32 t1, u32 t2)
189 if (t2 >= t1)
190 return (t2 - t1);
191 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
192 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
193 else
194 return ((0xFFFFFFFF - t1) + t2);
197 static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
199 if (t2 >= t1)
200 return PM_TIMER_TICKS_TO_US(t2 - t1);
201 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
202 return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
203 else
204 return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
208 * Callers should disable interrupts before the call and enable
209 * interrupts after return.
211 static void acpi_safe_halt(void)
213 current_thread_info()->status &= ~TS_POLLING;
215 * TS_POLLING-cleared state must be visible before we
216 * test NEED_RESCHED:
218 smp_mb();
219 <<<<<<< HEAD:drivers/acpi/processor_idle.c
220 if (!need_resched())
221 =======
222 if (!need_resched()) {
223 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
224 safe_halt();
225 <<<<<<< HEAD:drivers/acpi/processor_idle.c
226 =======
227 local_irq_disable();
229 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
230 current_thread_info()->status |= TS_POLLING;
233 #ifndef CONFIG_CPU_IDLE
235 static void
236 acpi_processor_power_activate(struct acpi_processor *pr,
237 struct acpi_processor_cx *new)
239 struct acpi_processor_cx *old;
241 if (!pr || !new)
242 return;
244 old = pr->power.state;
246 if (old)
247 old->promotion.count = 0;
248 new->demotion.count = 0;
250 /* Cleanup from old state. */
251 if (old) {
252 switch (old->type) {
253 case ACPI_STATE_C3:
254 /* Disable bus master reload */
255 if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
256 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
257 break;
261 /* Prepare to use new state. */
262 switch (new->type) {
263 case ACPI_STATE_C3:
264 /* Enable bus master reload */
265 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
266 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
267 break;
270 pr->power.state = new;
272 return;
275 static atomic_t c3_cpu_count;
277 /* Common C-state entry for C2, C3, .. */
278 static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
280 if (cstate->entry_method == ACPI_CSTATE_FFH) {
281 /* Call into architectural FFH based C-state */
282 acpi_processor_ffh_cstate_enter(cstate);
283 } else {
284 int unused;
285 /* IO port based C-state */
286 inb(cstate->address);
287 /* Dummy wait op - must do something useless after P_LVL2 read
288 because chipsets cannot guarantee that STPCLK# signal
289 gets asserted in time to freeze execution properly. */
290 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
293 #endif /* !CONFIG_CPU_IDLE */
295 #ifdef ARCH_APICTIMER_STOPS_ON_C3
298 * Some BIOS implementations switch to C3 in the published C2 state.
299 * This seems to be a common problem on AMD boxen, but other vendors
300 * are affected too. We pick the most conservative approach: we assume
301 * that the local APIC stops in both C2 and C3.
303 static void acpi_timer_check_state(int state, struct acpi_processor *pr,
304 struct acpi_processor_cx *cx)
306 struct acpi_processor_power *pwr = &pr->power;
307 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
310 * Check, if one of the previous states already marked the lapic
311 * unstable
313 if (pwr->timer_broadcast_on_state < state)
314 return;
316 if (cx->type >= type)
317 pr->power.timer_broadcast_on_state = state;
320 static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
322 unsigned long reason;
324 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
325 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
327 clockevents_notify(reason, &pr->id);
330 /* Power(C) State timer broadcast control */
331 static void acpi_state_timer_broadcast(struct acpi_processor *pr,
332 struct acpi_processor_cx *cx,
333 int broadcast)
335 int state = cx - pr->power.states;
337 if (state >= pr->power.timer_broadcast_on_state) {
338 unsigned long reason;
340 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
341 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
342 clockevents_notify(reason, &pr->id);
346 #else
348 static void acpi_timer_check_state(int state, struct acpi_processor *pr,
349 struct acpi_processor_cx *cstate) { }
350 static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
351 static void acpi_state_timer_broadcast(struct acpi_processor *pr,
352 struct acpi_processor_cx *cx,
353 int broadcast)
357 #endif
360 * Suspend / resume control
362 static int acpi_idle_suspend;
364 int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
366 acpi_idle_suspend = 1;
367 return 0;
370 int acpi_processor_resume(struct acpi_device * device)
372 acpi_idle_suspend = 0;
373 return 0;
376 <<<<<<< HEAD:drivers/acpi/processor_idle.c
377 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
378 =======
379 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
380 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
381 static int tsc_halts_in_c(int state)
383 switch (boot_cpu_data.x86_vendor) {
384 case X86_VENDOR_AMD:
386 * AMD Fam10h TSC will tick in all
387 * C/P/S0/S1 states when this bit is set.
389 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
390 return 0;
391 /*FALL THROUGH*/
392 case X86_VENDOR_INTEL:
393 /* Several cases known where TSC halts in C2 too */
394 default:
395 return state > ACPI_STATE_C1;
398 #endif
400 #ifndef CONFIG_CPU_IDLE
401 static void acpi_processor_idle(void)
403 struct acpi_processor *pr = NULL;
404 struct acpi_processor_cx *cx = NULL;
405 struct acpi_processor_cx *next_state = NULL;
406 int sleep_ticks = 0;
407 u32 t1, t2 = 0;
410 * Interrupts must be disabled during bus mastering calculations and
411 * for C2/C3 transitions.
413 local_irq_disable();
415 pr = processors[smp_processor_id()];
416 if (!pr) {
417 local_irq_enable();
418 return;
422 * Check whether we truly need to go idle, or should
423 * reschedule:
425 if (unlikely(need_resched())) {
426 local_irq_enable();
427 return;
430 cx = pr->power.state;
431 if (!cx || acpi_idle_suspend) {
432 if (pm_idle_save)
433 pm_idle_save();
434 else
435 acpi_safe_halt();
437 <<<<<<< HEAD:drivers/acpi/processor_idle.c
438 local_irq_enable();
439 =======
440 if (irqs_disabled())
441 local_irq_enable();
443 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
444 return;
448 * Check BM Activity
449 * -----------------
450 * Check for bus mastering activity (if required), record, and check
451 * for demotion.
453 if (pr->flags.bm_check) {
454 u32 bm_status = 0;
455 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
457 if (diff > 31)
458 diff = 31;
460 pr->power.bm_activity <<= diff;
462 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
463 if (bm_status) {
464 pr->power.bm_activity |= 0x1;
465 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
468 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
469 * the true state of bus mastering activity; forcing us to
470 * manually check the BMIDEA bit of each IDE channel.
472 else if (errata.piix4.bmisx) {
473 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
474 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
475 pr->power.bm_activity |= 0x1;
478 pr->power.bm_check_timestamp = jiffies;
481 * If bus mastering is or was active this jiffy, demote
482 * to avoid a faulty transition. Note that the processor
483 * won't enter a low-power state during this call (to this
484 * function) but should upon the next.
486 * TBD: A better policy might be to fallback to the demotion
487 * state (use it for this quantum only) istead of
488 * demoting -- and rely on duration as our sole demotion
489 * qualification. This may, however, introduce DMA
490 * issues (e.g. floppy DMA transfer overrun/underrun).
492 if ((pr->power.bm_activity & 0x1) &&
493 cx->demotion.threshold.bm) {
494 local_irq_enable();
495 next_state = cx->demotion.state;
496 goto end;
500 #ifdef CONFIG_HOTPLUG_CPU
502 * Check for P_LVL2_UP flag before entering C2 and above on
503 * an SMP system. We do it here instead of doing it at _CST/P_LVL
504 * detection phase, to work cleanly with logical CPU hotplug.
506 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
507 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
508 cx = &pr->power.states[ACPI_STATE_C1];
509 #endif
512 * Sleep:
513 * ------
514 * Invoke the current Cx state to put the processor to sleep.
516 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
517 current_thread_info()->status &= ~TS_POLLING;
519 * TS_POLLING-cleared state must be visible before we
520 * test NEED_RESCHED:
522 smp_mb();
523 if (need_resched()) {
524 current_thread_info()->status |= TS_POLLING;
525 local_irq_enable();
526 return;
530 switch (cx->type) {
532 case ACPI_STATE_C1:
534 * Invoke C1.
535 * Use the appropriate idle routine, the one that would
536 * be used without acpi C-states.
538 if (pm_idle_save)
539 pm_idle_save();
540 else
541 acpi_safe_halt();
544 * TBD: Can't get time duration while in C1, as resumes
545 * go to an ISR rather than here. Need to instrument
546 * base interrupt handler.
548 * Note: the TSC better not stop in C1, sched_clock() will
549 * skew otherwise.
551 sleep_ticks = 0xFFFFFFFF;
552 <<<<<<< HEAD:drivers/acpi/processor_idle.c
553 local_irq_enable();
554 =======
555 if (irqs_disabled())
556 local_irq_enable();
558 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
559 break;
561 case ACPI_STATE_C2:
562 /* Get start time (ticks) */
563 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
564 /* Tell the scheduler that we are going deep-idle: */
565 sched_clock_idle_sleep_event();
566 /* Invoke C2 */
567 acpi_state_timer_broadcast(pr, cx, 1);
568 acpi_cstate_enter(cx);
569 /* Get end time (ticks) */
570 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
572 <<<<<<< HEAD:drivers/acpi/processor_idle.c
573 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
574 =======
575 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
576 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
577 /* TSC halts in C2, so notify users */
578 if (tsc_halts_in_c(ACPI_STATE_C2))
579 mark_tsc_unstable("possible TSC halt in C2");
580 #endif
581 /* Compute time (ticks) that we were actually asleep */
582 sleep_ticks = ticks_elapsed(t1, t2);
584 /* Tell the scheduler how much we idled: */
585 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
587 /* Re-enable interrupts */
588 local_irq_enable();
589 /* Do not account our idle-switching overhead: */
590 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
592 current_thread_info()->status |= TS_POLLING;
593 acpi_state_timer_broadcast(pr, cx, 0);
594 break;
596 case ACPI_STATE_C3:
597 acpi_unlazy_tlb(smp_processor_id());
599 * Must be done before busmaster disable as we might
600 * need to access HPET !
602 acpi_state_timer_broadcast(pr, cx, 1);
604 * disable bus master
605 * bm_check implies we need ARB_DIS
606 * !bm_check implies we need cache flush
607 * bm_control implies whether we can do ARB_DIS
609 * That leaves a case where bm_check is set and bm_control is
610 * not set. In that case we cannot do much, we enter C3
611 * without doing anything.
613 if (pr->flags.bm_check && pr->flags.bm_control) {
614 if (atomic_inc_return(&c3_cpu_count) ==
615 num_online_cpus()) {
617 * All CPUs are trying to go to C3
618 * Disable bus master arbitration
620 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
622 } else if (!pr->flags.bm_check) {
623 /* SMP with no shared cache... Invalidate cache */
624 ACPI_FLUSH_CPU_CACHE();
627 /* Get start time (ticks) */
628 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
629 /* Invoke C3 */
630 /* Tell the scheduler that we are going deep-idle: */
631 sched_clock_idle_sleep_event();
632 acpi_cstate_enter(cx);
633 /* Get end time (ticks) */
634 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
635 if (pr->flags.bm_check && pr->flags.bm_control) {
636 /* Enable bus master arbitration */
637 atomic_dec(&c3_cpu_count);
638 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
641 <<<<<<< HEAD:drivers/acpi/processor_idle.c
642 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
643 =======
644 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
645 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
646 /* TSC halts in C3, so notify users */
647 if (tsc_halts_in_c(ACPI_STATE_C3))
648 mark_tsc_unstable("TSC halts in C3");
649 #endif
650 /* Compute time (ticks) that we were actually asleep */
651 sleep_ticks = ticks_elapsed(t1, t2);
652 /* Tell the scheduler how much we idled: */
653 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
655 /* Re-enable interrupts */
656 local_irq_enable();
657 /* Do not account our idle-switching overhead: */
658 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
660 current_thread_info()->status |= TS_POLLING;
661 acpi_state_timer_broadcast(pr, cx, 0);
662 break;
664 default:
665 local_irq_enable();
666 return;
668 cx->usage++;
669 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
670 cx->time += sleep_ticks;
672 next_state = pr->power.state;
674 #ifdef CONFIG_HOTPLUG_CPU
675 /* Don't do promotion/demotion */
676 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
677 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
678 next_state = cx;
679 goto end;
681 #endif
684 * Promotion?
685 * ----------
686 * Track the number of longs (time asleep is greater than threshold)
687 * and promote when the count threshold is reached. Note that bus
688 * mastering activity may prevent promotions.
689 * Do not promote above max_cstate.
691 if (cx->promotion.state &&
692 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
693 if (sleep_ticks > cx->promotion.threshold.ticks &&
694 cx->promotion.state->latency <=
695 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
696 cx->promotion.count++;
697 cx->demotion.count = 0;
698 if (cx->promotion.count >=
699 cx->promotion.threshold.count) {
700 if (pr->flags.bm_check) {
701 if (!
702 (pr->power.bm_activity & cx->
703 promotion.threshold.bm)) {
704 next_state =
705 cx->promotion.state;
706 goto end;
708 } else {
709 next_state = cx->promotion.state;
710 goto end;
717 * Demotion?
718 * ---------
719 * Track the number of shorts (time asleep is less than time threshold)
720 * and demote when the usage threshold is reached.
722 if (cx->demotion.state) {
723 if (sleep_ticks < cx->demotion.threshold.ticks) {
724 cx->demotion.count++;
725 cx->promotion.count = 0;
726 if (cx->demotion.count >= cx->demotion.threshold.count) {
727 next_state = cx->demotion.state;
728 goto end;
733 end:
735 * Demote if current state exceeds max_cstate
736 * or if the latency of the current state is unacceptable
738 if ((pr->power.state - pr->power.states) > max_cstate ||
739 pr->power.state->latency >
740 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
741 if (cx->demotion.state)
742 next_state = cx->demotion.state;
746 * New Cx State?
747 * -------------
748 * If we're going to start using a new Cx state we must clean up
749 * from the previous and prepare to use the new.
751 if (next_state != pr->power.state)
752 acpi_processor_power_activate(pr, next_state);
755 static int acpi_processor_set_power_policy(struct acpi_processor *pr)
757 unsigned int i;
758 unsigned int state_is_set = 0;
759 struct acpi_processor_cx *lower = NULL;
760 struct acpi_processor_cx *higher = NULL;
761 struct acpi_processor_cx *cx;
764 if (!pr)
765 return -EINVAL;
768 * This function sets the default Cx state policy (OS idle handler).
769 * Our scheme is to promote quickly to C2 but more conservatively
770 * to C3. We're favoring C2 for its characteristics of low latency
771 * (quick response), good power savings, and ability to allow bus
772 * mastering activity. Note that the Cx state policy is completely
773 * customizable and can be altered dynamically.
776 /* startup state */
777 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
778 cx = &pr->power.states[i];
779 if (!cx->valid)
780 continue;
782 if (!state_is_set)
783 pr->power.state = cx;
784 state_is_set++;
785 break;
788 if (!state_is_set)
789 return -ENODEV;
791 /* demotion */
792 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
793 cx = &pr->power.states[i];
794 if (!cx->valid)
795 continue;
797 if (lower) {
798 cx->demotion.state = lower;
799 cx->demotion.threshold.ticks = cx->latency_ticks;
800 cx->demotion.threshold.count = 1;
801 if (cx->type == ACPI_STATE_C3)
802 cx->demotion.threshold.bm = bm_history;
805 lower = cx;
808 /* promotion */
809 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
810 cx = &pr->power.states[i];
811 if (!cx->valid)
812 continue;
814 if (higher) {
815 cx->promotion.state = higher;
816 cx->promotion.threshold.ticks = cx->latency_ticks;
817 if (cx->type >= ACPI_STATE_C2)
818 cx->promotion.threshold.count = 4;
819 else
820 cx->promotion.threshold.count = 10;
821 if (higher->type == ACPI_STATE_C3)
822 cx->promotion.threshold.bm = bm_history;
825 higher = cx;
828 return 0;
830 #endif /* !CONFIG_CPU_IDLE */
832 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
835 if (!pr)
836 return -EINVAL;
838 if (!pr->pblk)
839 return -ENODEV;
841 /* if info is obtained from pblk/fadt, type equals state */
842 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
843 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
845 #ifndef CONFIG_HOTPLUG_CPU
847 * Check for P_LVL2_UP flag before entering C2 and above on
848 * an SMP system.
850 if ((num_online_cpus() > 1) &&
851 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
852 return -ENODEV;
853 #endif
855 /* determine C2 and C3 address from pblk */
856 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
857 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
859 /* determine latencies from FADT */
860 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
861 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
863 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
864 "lvl2[0x%08x] lvl3[0x%08x]\n",
865 pr->power.states[ACPI_STATE_C2].address,
866 pr->power.states[ACPI_STATE_C3].address));
868 return 0;
871 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
873 if (!pr->power.states[ACPI_STATE_C1].valid) {
874 /* set the first C-State to C1 */
875 /* all processors need to support C1 */
876 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
877 pr->power.states[ACPI_STATE_C1].valid = 1;
879 /* the C0 state only exists as a filler in our array */
880 pr->power.states[ACPI_STATE_C0].valid = 1;
881 return 0;
884 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
886 acpi_status status = 0;
887 acpi_integer count;
888 int current_count;
889 int i;
890 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
891 union acpi_object *cst;
894 if (nocst)
895 return -ENODEV;
897 current_count = 0;
899 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
900 if (ACPI_FAILURE(status)) {
901 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
902 return -ENODEV;
905 cst = buffer.pointer;
907 /* There must be at least 2 elements */
908 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
909 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
910 status = -EFAULT;
911 goto end;
914 count = cst->package.elements[0].integer.value;
916 /* Validate number of power states. */
917 if (count < 1 || count != cst->package.count - 1) {
918 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
919 status = -EFAULT;
920 goto end;
923 /* Tell driver that at least _CST is supported. */
924 pr->flags.has_cst = 1;
926 for (i = 1; i <= count; i++) {
927 union acpi_object *element;
928 union acpi_object *obj;
929 struct acpi_power_register *reg;
930 struct acpi_processor_cx cx;
932 memset(&cx, 0, sizeof(cx));
934 element = &(cst->package.elements[i]);
935 if (element->type != ACPI_TYPE_PACKAGE)
936 continue;
938 if (element->package.count != 4)
939 continue;
941 obj = &(element->package.elements[0]);
943 if (obj->type != ACPI_TYPE_BUFFER)
944 continue;
946 reg = (struct acpi_power_register *)obj->buffer.pointer;
948 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
949 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
950 continue;
952 /* There should be an easy way to extract an integer... */
953 obj = &(element->package.elements[1]);
954 if (obj->type != ACPI_TYPE_INTEGER)
955 continue;
957 cx.type = obj->integer.value;
959 * Some buggy BIOSes won't list C1 in _CST -
960 * Let acpi_processor_get_power_info_default() handle them later
962 if (i == 1 && cx.type != ACPI_STATE_C1)
963 current_count++;
965 cx.address = reg->address;
966 cx.index = current_count + 1;
968 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
969 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
970 if (acpi_processor_ffh_cstate_probe
971 (pr->id, &cx, reg) == 0) {
972 cx.entry_method = ACPI_CSTATE_FFH;
973 } else if (cx.type == ACPI_STATE_C1) {
975 * C1 is a special case where FIXED_HARDWARE
976 * can be handled in non-MWAIT way as well.
977 * In that case, save this _CST entry info.
978 * Otherwise, ignore this info and continue.
980 cx.entry_method = ACPI_CSTATE_HALT;
981 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
982 } else {
983 continue;
985 } else {
986 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
987 cx.address);
991 obj = &(element->package.elements[2]);
992 if (obj->type != ACPI_TYPE_INTEGER)
993 continue;
995 cx.latency = obj->integer.value;
997 obj = &(element->package.elements[3]);
998 if (obj->type != ACPI_TYPE_INTEGER)
999 continue;
1001 cx.power = obj->integer.value;
1003 current_count++;
1004 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
1007 * We support total ACPI_PROCESSOR_MAX_POWER - 1
1008 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
1010 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1011 printk(KERN_WARNING
1012 "Limiting number of power states to max (%d)\n",
1013 ACPI_PROCESSOR_MAX_POWER);
1014 printk(KERN_WARNING
1015 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1016 break;
1020 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
1021 current_count));
1023 /* Validate number of power states discovered */
1024 if (current_count < 2)
1025 status = -EFAULT;
1027 end:
1028 kfree(buffer.pointer);
1030 return status;
1033 static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1036 if (!cx->address)
1037 return;
1040 * C2 latency must be less than or equal to 100
1041 * microseconds.
1043 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1044 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1045 "latency too large [%d]\n", cx->latency));
1046 return;
1050 * Otherwise we've met all of our C2 requirements.
1051 * Normalize the C2 latency to expidite policy
1053 cx->valid = 1;
1055 #ifndef CONFIG_CPU_IDLE
1056 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
1057 #else
1058 cx->latency_ticks = cx->latency;
1059 #endif
1061 return;
1064 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1065 struct acpi_processor_cx *cx)
1067 static int bm_check_flag;
1070 if (!cx->address)
1071 return;
1074 * C3 latency must be less than or equal to 1000
1075 * microseconds.
1077 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1078 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1079 "latency too large [%d]\n", cx->latency));
1080 return;
1084 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1085 * DMA transfers are used by any ISA device to avoid livelock.
1086 * Note that we could disable Type-F DMA (as recommended by
1087 * the erratum), but this is known to disrupt certain ISA
1088 * devices thus we take the conservative approach.
1090 else if (errata.piix4.fdma) {
1091 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1092 "C3 not supported on PIIX4 with Type-F DMA\n"));
1093 return;
1096 /* All the logic here assumes flags.bm_check is same across all CPUs */
1097 if (!bm_check_flag) {
1098 /* Determine whether bm_check is needed based on CPU */
1099 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1100 bm_check_flag = pr->flags.bm_check;
1101 } else {
1102 pr->flags.bm_check = bm_check_flag;
1105 if (pr->flags.bm_check) {
1106 if (!pr->flags.bm_control) {
1107 if (pr->flags.has_cst != 1) {
1108 /* bus mastering control is necessary */
1109 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1110 "C3 support requires BM control\n"));
1111 return;
1112 } else {
1113 /* Here we enter C3 without bus mastering */
1114 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1115 "C3 support without BM control\n"));
1118 } else {
1120 * WBINVD should be set in fadt, for C3 state to be
1121 * supported on when bm_check is not required.
1123 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
1124 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1125 "Cache invalidation should work properly"
1126 " for C3 to be enabled on SMP systems\n"));
1127 return;
1129 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1133 * Otherwise we've met all of our C3 requirements.
1134 * Normalize the C3 latency to expidite policy. Enable
1135 * checking of bus mastering status (bm_check) so we can
1136 * use this in our C3 policy
1138 cx->valid = 1;
1140 #ifndef CONFIG_CPU_IDLE
1141 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
1142 #else
1143 cx->latency_ticks = cx->latency;
1144 #endif
1146 return;
1149 static int acpi_processor_power_verify(struct acpi_processor *pr)
1151 unsigned int i;
1152 unsigned int working = 0;
1154 pr->power.timer_broadcast_on_state = INT_MAX;
1156 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
1157 struct acpi_processor_cx *cx = &pr->power.states[i];
1159 switch (cx->type) {
1160 case ACPI_STATE_C1:
1161 cx->valid = 1;
1162 break;
1164 case ACPI_STATE_C2:
1165 acpi_processor_power_verify_c2(cx);
1166 if (cx->valid)
1167 acpi_timer_check_state(i, pr, cx);
1168 break;
1170 case ACPI_STATE_C3:
1171 acpi_processor_power_verify_c3(pr, cx);
1172 if (cx->valid)
1173 acpi_timer_check_state(i, pr, cx);
1174 break;
1177 if (cx->valid)
1178 working++;
1181 acpi_propagate_timer_broadcast(pr);
1183 return (working);
1186 static int acpi_processor_get_power_info(struct acpi_processor *pr)
1188 unsigned int i;
1189 int result;
1192 /* NOTE: the idle thread may not be running while calling
1193 * this function */
1195 /* Zero initialize all the C-states info. */
1196 memset(pr->power.states, 0, sizeof(pr->power.states));
1198 result = acpi_processor_get_power_info_cst(pr);
1199 if (result == -ENODEV)
1200 result = acpi_processor_get_power_info_fadt(pr);
1202 if (result)
1203 return result;
1205 acpi_processor_get_power_info_default(pr);
1207 pr->power.count = acpi_processor_power_verify(pr);
1209 #ifndef CONFIG_CPU_IDLE
1211 * Set Default Policy
1212 * ------------------
1213 * Now that we know which states are supported, set the default
1214 * policy. Note that this policy can be changed dynamically
1215 * (e.g. encourage deeper sleeps to conserve battery life when
1216 * not on AC).
1218 result = acpi_processor_set_power_policy(pr);
1219 if (result)
1220 return result;
1221 #endif
1224 * if one state of type C2 or C3 is available, mark this
1225 * CPU as being "idle manageable"
1227 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
1228 if (pr->power.states[i].valid) {
1229 pr->power.count = i;
1230 if (pr->power.states[i].type >= ACPI_STATE_C2)
1231 pr->flags.power = 1;
1235 return 0;
1238 static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1240 struct acpi_processor *pr = seq->private;
1241 unsigned int i;
1244 if (!pr)
1245 goto end;
1247 seq_printf(seq, "active state: C%zd\n"
1248 "max_cstate: C%d\n"
1249 "bus master activity: %08x\n"
1250 "maximum allowed latency: %d usec\n",
1251 pr->power.state ? pr->power.state - pr->power.states : 0,
1252 max_cstate, (unsigned)pr->power.bm_activity,
1253 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
1255 seq_puts(seq, "states:\n");
1257 for (i = 1; i <= pr->power.count; i++) {
1258 seq_printf(seq, " %cC%d: ",
1259 (&pr->power.states[i] ==
1260 pr->power.state ? '*' : ' '), i);
1262 if (!pr->power.states[i].valid) {
1263 seq_puts(seq, "<not supported>\n");
1264 continue;
1267 switch (pr->power.states[i].type) {
1268 case ACPI_STATE_C1:
1269 seq_printf(seq, "type[C1] ");
1270 break;
1271 case ACPI_STATE_C2:
1272 seq_printf(seq, "type[C2] ");
1273 break;
1274 case ACPI_STATE_C3:
1275 seq_printf(seq, "type[C3] ");
1276 break;
1277 default:
1278 seq_printf(seq, "type[--] ");
1279 break;
1282 if (pr->power.states[i].promotion.state)
1283 seq_printf(seq, "promotion[C%zd] ",
1284 (pr->power.states[i].promotion.state -
1285 pr->power.states));
1286 else
1287 seq_puts(seq, "promotion[--] ");
1289 if (pr->power.states[i].demotion.state)
1290 seq_printf(seq, "demotion[C%zd] ",
1291 (pr->power.states[i].demotion.state -
1292 pr->power.states));
1293 else
1294 seq_puts(seq, "demotion[--] ");
1296 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
1297 pr->power.states[i].latency,
1298 pr->power.states[i].usage,
1299 (unsigned long long)pr->power.states[i].time);
1302 end:
1303 return 0;
1306 static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1308 return single_open(file, acpi_processor_power_seq_show,
1309 PDE(inode)->data);
1312 static const struct file_operations acpi_processor_power_fops = {
1313 .open = acpi_processor_power_open_fs,
1314 .read = seq_read,
1315 .llseek = seq_lseek,
1316 .release = single_release,
1319 #ifndef CONFIG_CPU_IDLE
1321 int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1323 int result = 0;
1326 if (!pr)
1327 return -EINVAL;
1329 if (nocst) {
1330 return -ENODEV;
1333 if (!pr->flags.power_setup_done)
1334 return -ENODEV;
1336 /* Fall back to the default idle loop */
1337 pm_idle = pm_idle_save;
1338 synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
1340 pr->flags.power = 0;
1341 result = acpi_processor_get_power_info(pr);
1342 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1343 pm_idle = acpi_processor_idle;
1345 return result;
1348 #ifdef CONFIG_SMP
1349 static void smp_callback(void *v)
1351 /* we already woke the CPU up, nothing more to do */
1355 * This function gets called when a part of the kernel has a new latency
1356 * requirement. This means we need to get all processors out of their C-state,
1357 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1358 * wakes them all right up.
1360 static int acpi_processor_latency_notify(struct notifier_block *b,
1361 unsigned long l, void *v)
1363 smp_call_function(smp_callback, NULL, 0, 1);
1364 return NOTIFY_OK;
1367 static struct notifier_block acpi_processor_latency_notifier = {
1368 .notifier_call = acpi_processor_latency_notify,
1371 #endif
1373 #else /* CONFIG_CPU_IDLE */
1376 * acpi_idle_bm_check - checks if bus master activity was detected
1378 static int acpi_idle_bm_check(void)
1380 u32 bm_status = 0;
1382 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1383 if (bm_status)
1384 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1386 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1387 * the true state of bus mastering activity; forcing us to
1388 * manually check the BMIDEA bit of each IDE channel.
1390 else if (errata.piix4.bmisx) {
1391 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1392 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1393 bm_status = 1;
1395 return bm_status;
1399 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1400 * @pr: the processor
1401 * @target: the new target state
1403 static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1404 struct acpi_processor_cx *target)
1406 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1407 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1408 pr->flags.bm_rld_set = 0;
1411 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1412 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1413 pr->flags.bm_rld_set = 1;
1418 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1419 * @cx: cstate data
1421 * Caller disables interrupt before call and enables interrupt after return.
1423 static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1425 if (cx->entry_method == ACPI_CSTATE_FFH) {
1426 /* Call into architectural FFH based C-state */
1427 acpi_processor_ffh_cstate_enter(cx);
1428 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1429 acpi_safe_halt();
1430 } else {
1431 int unused;
1432 /* IO port based C-state */
1433 inb(cx->address);
1434 /* Dummy wait op - must do something useless after P_LVL2 read
1435 because chipsets cannot guarantee that STPCLK# signal
1436 gets asserted in time to freeze execution properly. */
1437 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1442 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1443 * @dev: the target CPU
1444 * @state: the state data
1446 * This is equivalent to the HALT instruction.
1448 static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1449 struct cpuidle_state *state)
1451 u32 t1, t2;
1452 struct acpi_processor *pr;
1453 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1455 pr = processors[smp_processor_id()];
1457 if (unlikely(!pr))
1458 return 0;
1460 local_irq_disable();
1462 /* Do not access any ACPI IO ports in suspend path */
1463 if (acpi_idle_suspend) {
1464 acpi_safe_halt();
1465 local_irq_enable();
1466 return 0;
1469 if (pr->flags.bm_check)
1470 acpi_idle_update_bm_rld(pr, cx);
1472 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1473 acpi_idle_do_entry(cx);
1474 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1476 local_irq_enable();
1477 cx->usage++;
1479 return ticks_elapsed_in_us(t1, t2);
1483 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1484 * @dev: the target CPU
1485 * @state: the state data
1487 static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1488 struct cpuidle_state *state)
1490 struct acpi_processor *pr;
1491 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1492 u32 t1, t2;
1493 int sleep_ticks = 0;
1495 pr = processors[smp_processor_id()];
1497 if (unlikely(!pr))
1498 return 0;
1500 if (acpi_idle_suspend)
1501 return(acpi_idle_enter_c1(dev, state));
1503 local_irq_disable();
1504 current_thread_info()->status &= ~TS_POLLING;
1506 * TS_POLLING-cleared state must be visible before we test
1507 * NEED_RESCHED:
1509 smp_mb();
1511 if (unlikely(need_resched())) {
1512 current_thread_info()->status |= TS_POLLING;
1513 local_irq_enable();
1514 return 0;
1517 acpi_unlazy_tlb(smp_processor_id());
1519 * Must be done before busmaster disable as we might need to
1520 * access HPET !
1522 acpi_state_timer_broadcast(pr, cx, 1);
1524 if (pr->flags.bm_check)
1525 acpi_idle_update_bm_rld(pr, cx);
1527 if (cx->type == ACPI_STATE_C3)
1528 ACPI_FLUSH_CPU_CACHE();
1530 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1531 /* Tell the scheduler that we are going deep-idle: */
1532 sched_clock_idle_sleep_event();
1533 acpi_idle_do_entry(cx);
1534 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1536 <<<<<<< HEAD:drivers/acpi/processor_idle.c
1537 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
1538 =======
1539 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
1540 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
1541 /* TSC could halt in idle, so notify users */
1542 if (tsc_halts_in_c(cx->type))
1543 mark_tsc_unstable("TSC halts in idle");;
1544 #endif
1545 sleep_ticks = ticks_elapsed(t1, t2);
1547 /* Tell the scheduler how much we idled: */
1548 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
1550 local_irq_enable();
1551 current_thread_info()->status |= TS_POLLING;
1553 cx->usage++;
1555 acpi_state_timer_broadcast(pr, cx, 0);
1556 cx->time += sleep_ticks;
1557 return ticks_elapsed_in_us(t1, t2);
1560 static int c3_cpu_count;
1561 static DEFINE_SPINLOCK(c3_lock);
1564 * acpi_idle_enter_bm - enters C3 with proper BM handling
1565 * @dev: the target CPU
1566 * @state: the state data
1568 * If BM is detected, the deepest non-C3 idle state is entered instead.
1570 static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1571 struct cpuidle_state *state)
1573 struct acpi_processor *pr;
1574 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1575 u32 t1, t2;
1576 int sleep_ticks = 0;
1578 pr = processors[smp_processor_id()];
1580 if (unlikely(!pr))
1581 return 0;
1583 if (acpi_idle_suspend)
1584 return(acpi_idle_enter_c1(dev, state));
1586 if (acpi_idle_bm_check()) {
1587 if (dev->safe_state) {
1588 return dev->safe_state->enter(dev, dev->safe_state);
1589 } else {
1590 local_irq_disable();
1591 acpi_safe_halt();
1592 local_irq_enable();
1593 return 0;
1597 local_irq_disable();
1598 current_thread_info()->status &= ~TS_POLLING;
1600 * TS_POLLING-cleared state must be visible before we test
1601 * NEED_RESCHED:
1603 smp_mb();
1605 if (unlikely(need_resched())) {
1606 current_thread_info()->status |= TS_POLLING;
1607 local_irq_enable();
1608 return 0;
1611 /* Tell the scheduler that we are going deep-idle: */
1612 sched_clock_idle_sleep_event();
1614 * Must be done before busmaster disable as we might need to
1615 * access HPET !
1617 acpi_state_timer_broadcast(pr, cx, 1);
1619 acpi_idle_update_bm_rld(pr, cx);
1622 * disable bus master
1623 * bm_check implies we need ARB_DIS
1624 * !bm_check implies we need cache flush
1625 * bm_control implies whether we can do ARB_DIS
1627 * That leaves a case where bm_check is set and bm_control is
1628 * not set. In that case we cannot do much, we enter C3
1629 * without doing anything.
1631 if (pr->flags.bm_check && pr->flags.bm_control) {
1632 spin_lock(&c3_lock);
1633 c3_cpu_count++;
1634 /* Disable bus master arbitration when all CPUs are in C3 */
1635 if (c3_cpu_count == num_online_cpus())
1636 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1637 spin_unlock(&c3_lock);
1638 } else if (!pr->flags.bm_check) {
1639 ACPI_FLUSH_CPU_CACHE();
1642 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1643 acpi_idle_do_entry(cx);
1644 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1646 /* Re-enable bus master arbitration */
1647 if (pr->flags.bm_check && pr->flags.bm_control) {
1648 spin_lock(&c3_lock);
1649 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
1650 c3_cpu_count--;
1651 spin_unlock(&c3_lock);
1654 <<<<<<< HEAD:drivers/acpi/processor_idle.c
1655 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
1656 =======
1657 #if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
1658 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:drivers/acpi/processor_idle.c
1659 /* TSC could halt in idle, so notify users */
1660 if (tsc_halts_in_c(ACPI_STATE_C3))
1661 mark_tsc_unstable("TSC halts in idle");
1662 #endif
1663 sleep_ticks = ticks_elapsed(t1, t2);
1664 /* Tell the scheduler how much we idled: */
1665 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
1667 local_irq_enable();
1668 current_thread_info()->status |= TS_POLLING;
1670 cx->usage++;
1672 acpi_state_timer_broadcast(pr, cx, 0);
1673 cx->time += sleep_ticks;
1674 return ticks_elapsed_in_us(t1, t2);
1677 struct cpuidle_driver acpi_idle_driver = {
1678 .name = "acpi_idle",
1679 .owner = THIS_MODULE,
1683 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1684 * @pr: the ACPI processor
1686 static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1688 int i, count = CPUIDLE_DRIVER_STATE_START;
1689 struct acpi_processor_cx *cx;
1690 struct cpuidle_state *state;
1691 struct cpuidle_device *dev = &pr->power.dev;
1693 if (!pr->flags.power_setup_done)
1694 return -EINVAL;
1696 if (pr->flags.power == 0) {
1697 return -EINVAL;
1700 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1701 dev->states[i].name[0] = '\0';
1702 dev->states[i].desc[0] = '\0';
1705 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1706 cx = &pr->power.states[i];
1707 state = &dev->states[count];
1709 if (!cx->valid)
1710 continue;
1712 #ifdef CONFIG_HOTPLUG_CPU
1713 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1714 !pr->flags.has_cst &&
1715 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1716 continue;
1717 #endif
1718 cpuidle_set_statedata(state, cx);
1720 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
1721 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
1722 state->exit_latency = cx->latency;
1723 state->target_residency = cx->latency * latency_factor;
1724 state->power_usage = cx->power;
1726 state->flags = 0;
1727 switch (cx->type) {
1728 case ACPI_STATE_C1:
1729 state->flags |= CPUIDLE_FLAG_SHALLOW;
1730 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1731 state->enter = acpi_idle_enter_c1;
1732 dev->safe_state = state;
1733 break;
1735 case ACPI_STATE_C2:
1736 state->flags |= CPUIDLE_FLAG_BALANCED;
1737 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1738 state->enter = acpi_idle_enter_simple;
1739 dev->safe_state = state;
1740 break;
1742 case ACPI_STATE_C3:
1743 state->flags |= CPUIDLE_FLAG_DEEP;
1744 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1745 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1746 state->enter = pr->flags.bm_check ?
1747 acpi_idle_enter_bm :
1748 acpi_idle_enter_simple;
1749 break;
1752 count++;
1753 if (count == CPUIDLE_STATE_MAX)
1754 break;
1757 dev->state_count = count;
1759 if (!count)
1760 return -EINVAL;
1762 return 0;
1765 int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1767 int ret;
1769 if (!pr)
1770 return -EINVAL;
1772 if (nocst) {
1773 return -ENODEV;
1776 if (!pr->flags.power_setup_done)
1777 return -ENODEV;
1779 cpuidle_pause_and_lock();
1780 cpuidle_disable_device(&pr->power.dev);
1781 acpi_processor_get_power_info(pr);
1782 acpi_processor_setup_cpuidle(pr);
1783 ret = cpuidle_enable_device(&pr->power.dev);
1784 cpuidle_resume_and_unlock();
1786 return ret;
1789 #endif /* CONFIG_CPU_IDLE */
1791 int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
1792 struct acpi_device *device)
1794 acpi_status status = 0;
1795 static int first_run;
1796 struct proc_dir_entry *entry = NULL;
1797 unsigned int i;
1800 if (!first_run) {
1801 dmi_check_system(processor_power_dmi_table);
1802 max_cstate = acpi_processor_cstate_check(max_cstate);
1803 if (max_cstate < ACPI_C_STATES_MAX)
1804 printk(KERN_NOTICE
1805 "ACPI: processor limited to max C-state %d\n",
1806 max_cstate);
1807 first_run++;
1808 #if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1809 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1810 &acpi_processor_latency_notifier);
1811 #endif
1814 if (!pr)
1815 return -EINVAL;
1817 if (acpi_gbl_FADT.cst_control && !nocst) {
1818 status =
1819 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
1820 if (ACPI_FAILURE(status)) {
1821 ACPI_EXCEPTION((AE_INFO, status,
1822 "Notifying BIOS of _CST ability failed"));
1826 acpi_processor_get_power_info(pr);
1827 pr->flags.power_setup_done = 1;
1830 * Install the idle handler if processor power management is supported.
1831 * Note that we use previously set idle handler will be used on
1832 * platforms that only support C1.
1834 if ((pr->flags.power) && (!boot_option_idle_override)) {
1835 #ifdef CONFIG_CPU_IDLE
1836 acpi_processor_setup_cpuidle(pr);
1837 pr->power.dev.cpu = pr->id;
1838 if (cpuidle_register_device(&pr->power.dev))
1839 return -EIO;
1840 #endif
1842 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1843 for (i = 1; i <= pr->power.count; i++)
1844 if (pr->power.states[i].valid)
1845 printk(" C%d[C%d]", i,
1846 pr->power.states[i].type);
1847 printk(")\n");
1849 #ifndef CONFIG_CPU_IDLE
1850 if (pr->id == 0) {
1851 pm_idle_save = pm_idle;
1852 pm_idle = acpi_processor_idle;
1854 #endif
1857 /* 'power' [R] */
1858 entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1859 S_IRUGO, acpi_device_dir(device));
1860 if (!entry)
1861 return -EIO;
1862 else {
1863 entry->proc_fops = &acpi_processor_power_fops;
1864 entry->data = acpi_driver_data(device);
1865 entry->owner = THIS_MODULE;
1868 return 0;
1871 int acpi_processor_power_exit(struct acpi_processor *pr,
1872 struct acpi_device *device)
1874 #ifdef CONFIG_CPU_IDLE
1875 if ((pr->flags.power) && (!boot_option_idle_override))
1876 cpuidle_unregister_device(&pr->power.dev);
1877 #endif
1878 pr->flags.power_setup_done = 0;
1880 if (acpi_device_dir(device))
1881 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1882 acpi_device_dir(device));
1884 #ifndef CONFIG_CPU_IDLE
1886 /* Unregister the idle handler when processor #0 is removed. */
1887 if (pr->id == 0) {
1888 pm_idle = pm_idle_save;
1891 * We are about to unload the current idle thread pm callback
1892 * (pm_idle), Wait for all processors to update cached/local
1893 * copies of pm_idle before proceeding.
1895 cpu_idle_wait();
1896 #ifdef CONFIG_SMP
1897 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1898 &acpi_processor_latency_notifier);
1899 #endif
1901 #endif
1903 return 0;