s390/ptrace: get rid of long longs in psw_bits
[linux/fpc-iii.git] / drivers / acpi / processor_idle.c
blob175c86bee3a95b47934d094a491b842973e2e090
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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27 #include <linux/module.h>
28 #include <linux/acpi.h>
29 #include <linux/dmi.h>
30 #include <linux/sched.h> /* need_resched() */
31 #include <linux/tick.h>
32 #include <linux/cpuidle.h>
33 #include <linux/syscore_ops.h>
34 #include <acpi/processor.h>
37 * Include the apic definitions for x86 to have the APIC timer related defines
38 * available also for UP (on SMP it gets magically included via linux/smp.h).
39 * asm/acpi.h is not an option, as it would require more include magic. Also
40 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
42 #ifdef CONFIG_X86
43 #include <asm/apic.h>
44 #endif
46 #define PREFIX "ACPI: "
48 #define ACPI_PROCESSOR_CLASS "processor"
49 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
50 ACPI_MODULE_NAME("processor_idle");
52 static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
53 module_param(max_cstate, uint, 0000);
54 static unsigned int nocst __read_mostly;
55 module_param(nocst, uint, 0000);
56 static int bm_check_disable __read_mostly;
57 module_param(bm_check_disable, uint, 0000);
59 static unsigned int latency_factor __read_mostly = 2;
60 module_param(latency_factor, uint, 0644);
62 static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
64 static DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX],
65 acpi_cstate);
67 static int disabled_by_idle_boot_param(void)
69 return boot_option_idle_override == IDLE_POLL ||
70 boot_option_idle_override == IDLE_HALT;
74 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
75 * For now disable this. Probably a bug somewhere else.
77 * To skip this limit, boot/load with a large max_cstate limit.
79 static int set_max_cstate(const struct dmi_system_id *id)
81 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
82 return 0;
84 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
85 " Override with \"processor.max_cstate=%d\"\n", id->ident,
86 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
88 max_cstate = (long)id->driver_data;
90 return 0;
93 static const struct dmi_system_id processor_power_dmi_table[] = {
94 { set_max_cstate, "Clevo 5600D", {
95 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
96 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
97 (void *)2},
98 { set_max_cstate, "Pavilion zv5000", {
99 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
100 DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
101 (void *)1},
102 { set_max_cstate, "Asus L8400B", {
103 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
104 DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
105 (void *)1},
111 * Callers should disable interrupts before the call and enable
112 * interrupts after return.
114 static void acpi_safe_halt(void)
116 if (!tif_need_resched()) {
117 safe_halt();
118 local_irq_disable();
122 #ifdef ARCH_APICTIMER_STOPS_ON_C3
125 * Some BIOS implementations switch to C3 in the published C2 state.
126 * This seems to be a common problem on AMD boxen, but other vendors
127 * are affected too. We pick the most conservative approach: we assume
128 * that the local APIC stops in both C2 and C3.
130 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
131 struct acpi_processor_cx *cx)
133 struct acpi_processor_power *pwr = &pr->power;
134 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
136 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
137 return;
139 if (amd_e400_c1e_detected)
140 type = ACPI_STATE_C1;
143 * Check, if one of the previous states already marked the lapic
144 * unstable
146 if (pwr->timer_broadcast_on_state < state)
147 return;
149 if (cx->type >= type)
150 pr->power.timer_broadcast_on_state = state;
153 static void __lapic_timer_propagate_broadcast(void *arg)
155 struct acpi_processor *pr = (struct acpi_processor *) arg;
157 if (pr->power.timer_broadcast_on_state < INT_MAX)
158 tick_broadcast_enable();
159 else
160 tick_broadcast_disable();
163 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
165 smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
166 (void *)pr, 1);
169 /* Power(C) State timer broadcast control */
170 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
171 struct acpi_processor_cx *cx,
172 int broadcast)
174 int state = cx - pr->power.states;
176 if (state >= pr->power.timer_broadcast_on_state) {
177 if (broadcast)
178 tick_broadcast_enter();
179 else
180 tick_broadcast_exit();
184 #else
186 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
187 struct acpi_processor_cx *cstate) { }
188 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
189 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
190 struct acpi_processor_cx *cx,
191 int broadcast)
195 #endif
197 #ifdef CONFIG_PM_SLEEP
198 static u32 saved_bm_rld;
200 static int acpi_processor_suspend(void)
202 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
203 return 0;
206 static void acpi_processor_resume(void)
208 u32 resumed_bm_rld = 0;
210 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
211 if (resumed_bm_rld == saved_bm_rld)
212 return;
214 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
217 static struct syscore_ops acpi_processor_syscore_ops = {
218 .suspend = acpi_processor_suspend,
219 .resume = acpi_processor_resume,
222 void acpi_processor_syscore_init(void)
224 register_syscore_ops(&acpi_processor_syscore_ops);
227 void acpi_processor_syscore_exit(void)
229 unregister_syscore_ops(&acpi_processor_syscore_ops);
231 #endif /* CONFIG_PM_SLEEP */
233 #if defined(CONFIG_X86)
234 static void tsc_check_state(int state)
236 switch (boot_cpu_data.x86_vendor) {
237 case X86_VENDOR_AMD:
238 case X86_VENDOR_INTEL:
240 * AMD Fam10h TSC will tick in all
241 * C/P/S0/S1 states when this bit is set.
243 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
244 return;
246 /*FALL THROUGH*/
247 default:
248 /* TSC could halt in idle, so notify users */
249 if (state > ACPI_STATE_C1)
250 mark_tsc_unstable("TSC halts in idle");
253 #else
254 static void tsc_check_state(int state) { return; }
255 #endif
257 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
260 if (!pr->pblk)
261 return -ENODEV;
263 /* if info is obtained from pblk/fadt, type equals state */
264 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
265 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
267 #ifndef CONFIG_HOTPLUG_CPU
269 * Check for P_LVL2_UP flag before entering C2 and above on
270 * an SMP system.
272 if ((num_online_cpus() > 1) &&
273 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
274 return -ENODEV;
275 #endif
277 /* determine C2 and C3 address from pblk */
278 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
279 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
281 /* determine latencies from FADT */
282 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
283 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
286 * FADT specified C2 latency must be less than or equal to
287 * 100 microseconds.
289 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
290 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
291 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
292 /* invalidate C2 */
293 pr->power.states[ACPI_STATE_C2].address = 0;
297 * FADT supplied C3 latency must be less than or equal to
298 * 1000 microseconds.
300 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
301 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
302 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
303 /* invalidate C3 */
304 pr->power.states[ACPI_STATE_C3].address = 0;
307 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308 "lvl2[0x%08x] lvl3[0x%08x]\n",
309 pr->power.states[ACPI_STATE_C2].address,
310 pr->power.states[ACPI_STATE_C3].address));
312 return 0;
315 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
317 if (!pr->power.states[ACPI_STATE_C1].valid) {
318 /* set the first C-State to C1 */
319 /* all processors need to support C1 */
320 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
321 pr->power.states[ACPI_STATE_C1].valid = 1;
322 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
324 /* the C0 state only exists as a filler in our array */
325 pr->power.states[ACPI_STATE_C0].valid = 1;
326 return 0;
329 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
331 acpi_status status;
332 u64 count;
333 int current_count;
334 int i, ret = 0;
335 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
336 union acpi_object *cst;
339 if (nocst)
340 return -ENODEV;
342 current_count = 0;
344 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
345 if (ACPI_FAILURE(status)) {
346 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
347 return -ENODEV;
350 cst = buffer.pointer;
352 /* There must be at least 2 elements */
353 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
354 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
355 ret = -EFAULT;
356 goto end;
359 count = cst->package.elements[0].integer.value;
361 /* Validate number of power states. */
362 if (count < 1 || count != cst->package.count - 1) {
363 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
364 ret = -EFAULT;
365 goto end;
368 /* Tell driver that at least _CST is supported. */
369 pr->flags.has_cst = 1;
371 for (i = 1; i <= count; i++) {
372 union acpi_object *element;
373 union acpi_object *obj;
374 struct acpi_power_register *reg;
375 struct acpi_processor_cx cx;
377 memset(&cx, 0, sizeof(cx));
379 element = &(cst->package.elements[i]);
380 if (element->type != ACPI_TYPE_PACKAGE)
381 continue;
383 if (element->package.count != 4)
384 continue;
386 obj = &(element->package.elements[0]);
388 if (obj->type != ACPI_TYPE_BUFFER)
389 continue;
391 reg = (struct acpi_power_register *)obj->buffer.pointer;
393 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
394 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
395 continue;
397 /* There should be an easy way to extract an integer... */
398 obj = &(element->package.elements[1]);
399 if (obj->type != ACPI_TYPE_INTEGER)
400 continue;
402 cx.type = obj->integer.value;
404 * Some buggy BIOSes won't list C1 in _CST -
405 * Let acpi_processor_get_power_info_default() handle them later
407 if (i == 1 && cx.type != ACPI_STATE_C1)
408 current_count++;
410 cx.address = reg->address;
411 cx.index = current_count + 1;
413 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
414 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
415 if (acpi_processor_ffh_cstate_probe
416 (pr->id, &cx, reg) == 0) {
417 cx.entry_method = ACPI_CSTATE_FFH;
418 } else if (cx.type == ACPI_STATE_C1) {
420 * C1 is a special case where FIXED_HARDWARE
421 * can be handled in non-MWAIT way as well.
422 * In that case, save this _CST entry info.
423 * Otherwise, ignore this info and continue.
425 cx.entry_method = ACPI_CSTATE_HALT;
426 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
427 } else {
428 continue;
430 if (cx.type == ACPI_STATE_C1 &&
431 (boot_option_idle_override == IDLE_NOMWAIT)) {
433 * In most cases the C1 space_id obtained from
434 * _CST object is FIXED_HARDWARE access mode.
435 * But when the option of idle=halt is added,
436 * the entry_method type should be changed from
437 * CSTATE_FFH to CSTATE_HALT.
438 * When the option of idle=nomwait is added,
439 * the C1 entry_method type should be
440 * CSTATE_HALT.
442 cx.entry_method = ACPI_CSTATE_HALT;
443 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
445 } else {
446 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
447 cx.address);
450 if (cx.type == ACPI_STATE_C1) {
451 cx.valid = 1;
454 obj = &(element->package.elements[2]);
455 if (obj->type != ACPI_TYPE_INTEGER)
456 continue;
458 cx.latency = obj->integer.value;
460 obj = &(element->package.elements[3]);
461 if (obj->type != ACPI_TYPE_INTEGER)
462 continue;
464 current_count++;
465 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
468 * We support total ACPI_PROCESSOR_MAX_POWER - 1
469 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
471 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
472 printk(KERN_WARNING
473 "Limiting number of power states to max (%d)\n",
474 ACPI_PROCESSOR_MAX_POWER);
475 printk(KERN_WARNING
476 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
477 break;
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
482 current_count));
484 /* Validate number of power states discovered */
485 if (current_count < 2)
486 ret = -EFAULT;
488 end:
489 kfree(buffer.pointer);
491 return ret;
494 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
495 struct acpi_processor_cx *cx)
497 static int bm_check_flag = -1;
498 static int bm_control_flag = -1;
501 if (!cx->address)
502 return;
505 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
506 * DMA transfers are used by any ISA device to avoid livelock.
507 * Note that we could disable Type-F DMA (as recommended by
508 * the erratum), but this is known to disrupt certain ISA
509 * devices thus we take the conservative approach.
511 else if (errata.piix4.fdma) {
512 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
513 "C3 not supported on PIIX4 with Type-F DMA\n"));
514 return;
517 /* All the logic here assumes flags.bm_check is same across all CPUs */
518 if (bm_check_flag == -1) {
519 /* Determine whether bm_check is needed based on CPU */
520 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
521 bm_check_flag = pr->flags.bm_check;
522 bm_control_flag = pr->flags.bm_control;
523 } else {
524 pr->flags.bm_check = bm_check_flag;
525 pr->flags.bm_control = bm_control_flag;
528 if (pr->flags.bm_check) {
529 if (!pr->flags.bm_control) {
530 if (pr->flags.has_cst != 1) {
531 /* bus mastering control is necessary */
532 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
533 "C3 support requires BM control\n"));
534 return;
535 } else {
536 /* Here we enter C3 without bus mastering */
537 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
538 "C3 support without BM control\n"));
541 } else {
543 * WBINVD should be set in fadt, for C3 state to be
544 * supported on when bm_check is not required.
546 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "Cache invalidation should work properly"
549 " for C3 to be enabled on SMP systems\n"));
550 return;
555 * Otherwise we've met all of our C3 requirements.
556 * Normalize the C3 latency to expidite policy. Enable
557 * checking of bus mastering status (bm_check) so we can
558 * use this in our C3 policy
560 cx->valid = 1;
563 * On older chipsets, BM_RLD needs to be set
564 * in order for Bus Master activity to wake the
565 * system from C3. Newer chipsets handle DMA
566 * during C3 automatically and BM_RLD is a NOP.
567 * In either case, the proper way to
568 * handle BM_RLD is to set it and leave it set.
570 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
572 return;
575 static int acpi_processor_power_verify(struct acpi_processor *pr)
577 unsigned int i;
578 unsigned int working = 0;
580 pr->power.timer_broadcast_on_state = INT_MAX;
582 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
583 struct acpi_processor_cx *cx = &pr->power.states[i];
585 switch (cx->type) {
586 case ACPI_STATE_C1:
587 cx->valid = 1;
588 break;
590 case ACPI_STATE_C2:
591 if (!cx->address)
592 break;
593 cx->valid = 1;
594 break;
596 case ACPI_STATE_C3:
597 acpi_processor_power_verify_c3(pr, cx);
598 break;
600 if (!cx->valid)
601 continue;
603 lapic_timer_check_state(i, pr, cx);
604 tsc_check_state(cx->type);
605 working++;
608 lapic_timer_propagate_broadcast(pr);
610 return (working);
613 static int acpi_processor_get_power_info(struct acpi_processor *pr)
615 unsigned int i;
616 int result;
619 /* NOTE: the idle thread may not be running while calling
620 * this function */
622 /* Zero initialize all the C-states info. */
623 memset(pr->power.states, 0, sizeof(pr->power.states));
625 result = acpi_processor_get_power_info_cst(pr);
626 if (result == -ENODEV)
627 result = acpi_processor_get_power_info_fadt(pr);
629 if (result)
630 return result;
632 acpi_processor_get_power_info_default(pr);
634 pr->power.count = acpi_processor_power_verify(pr);
637 * if one state of type C2 or C3 is available, mark this
638 * CPU as being "idle manageable"
640 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
641 if (pr->power.states[i].valid) {
642 pr->power.count = i;
643 if (pr->power.states[i].type >= ACPI_STATE_C2)
644 pr->flags.power = 1;
648 return 0;
652 * acpi_idle_bm_check - checks if bus master activity was detected
654 static int acpi_idle_bm_check(void)
656 u32 bm_status = 0;
658 if (bm_check_disable)
659 return 0;
661 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
662 if (bm_status)
663 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
665 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
666 * the true state of bus mastering activity; forcing us to
667 * manually check the BMIDEA bit of each IDE channel.
669 else if (errata.piix4.bmisx) {
670 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
671 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
672 bm_status = 1;
674 return bm_status;
678 * acpi_idle_do_entry - enter idle state using the appropriate method
679 * @cx: cstate data
681 * Caller disables interrupt before call and enables interrupt after return.
683 static void acpi_idle_do_entry(struct acpi_processor_cx *cx)
685 if (cx->entry_method == ACPI_CSTATE_FFH) {
686 /* Call into architectural FFH based C-state */
687 acpi_processor_ffh_cstate_enter(cx);
688 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
689 acpi_safe_halt();
690 } else {
691 /* IO port based C-state */
692 inb(cx->address);
693 /* Dummy wait op - must do something useless after P_LVL2 read
694 because chipsets cannot guarantee that STPCLK# signal
695 gets asserted in time to freeze execution properly. */
696 inl(acpi_gbl_FADT.xpm_timer_block.address);
701 * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
702 * @dev: the target CPU
703 * @index: the index of suggested state
705 static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
707 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
709 ACPI_FLUSH_CPU_CACHE();
711 while (1) {
713 if (cx->entry_method == ACPI_CSTATE_HALT)
714 safe_halt();
715 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
716 inb(cx->address);
717 /* See comment in acpi_idle_do_entry() */
718 inl(acpi_gbl_FADT.xpm_timer_block.address);
719 } else
720 return -ENODEV;
723 /* Never reached */
724 return 0;
727 static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
729 return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
730 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
733 static int c3_cpu_count;
734 static DEFINE_RAW_SPINLOCK(c3_lock);
737 * acpi_idle_enter_bm - enters C3 with proper BM handling
738 * @pr: Target processor
739 * @cx: Target state context
740 * @timer_bc: Whether or not to change timer mode to broadcast
742 static void acpi_idle_enter_bm(struct acpi_processor *pr,
743 struct acpi_processor_cx *cx, bool timer_bc)
745 acpi_unlazy_tlb(smp_processor_id());
748 * Must be done before busmaster disable as we might need to
749 * access HPET !
751 if (timer_bc)
752 lapic_timer_state_broadcast(pr, cx, 1);
755 * disable bus master
756 * bm_check implies we need ARB_DIS
757 * bm_control implies whether we can do ARB_DIS
759 * That leaves a case where bm_check is set and bm_control is
760 * not set. In that case we cannot do much, we enter C3
761 * without doing anything.
763 if (pr->flags.bm_control) {
764 raw_spin_lock(&c3_lock);
765 c3_cpu_count++;
766 /* Disable bus master arbitration when all CPUs are in C3 */
767 if (c3_cpu_count == num_online_cpus())
768 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
769 raw_spin_unlock(&c3_lock);
772 acpi_idle_do_entry(cx);
774 /* Re-enable bus master arbitration */
775 if (pr->flags.bm_control) {
776 raw_spin_lock(&c3_lock);
777 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
778 c3_cpu_count--;
779 raw_spin_unlock(&c3_lock);
782 if (timer_bc)
783 lapic_timer_state_broadcast(pr, cx, 0);
786 static int acpi_idle_enter(struct cpuidle_device *dev,
787 struct cpuidle_driver *drv, int index)
789 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
790 struct acpi_processor *pr;
792 pr = __this_cpu_read(processors);
793 if (unlikely(!pr))
794 return -EINVAL;
796 if (cx->type != ACPI_STATE_C1) {
797 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
798 index = CPUIDLE_DRIVER_STATE_START;
799 cx = per_cpu(acpi_cstate[index], dev->cpu);
800 } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
801 if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
802 acpi_idle_enter_bm(pr, cx, true);
803 return index;
804 } else if (drv->safe_state_index >= 0) {
805 index = drv->safe_state_index;
806 cx = per_cpu(acpi_cstate[index], dev->cpu);
807 } else {
808 acpi_safe_halt();
809 return -EBUSY;
814 lapic_timer_state_broadcast(pr, cx, 1);
816 if (cx->type == ACPI_STATE_C3)
817 ACPI_FLUSH_CPU_CACHE();
819 acpi_idle_do_entry(cx);
821 lapic_timer_state_broadcast(pr, cx, 0);
823 return index;
826 static void acpi_idle_enter_freeze(struct cpuidle_device *dev,
827 struct cpuidle_driver *drv, int index)
829 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
831 if (cx->type == ACPI_STATE_C3) {
832 struct acpi_processor *pr = __this_cpu_read(processors);
834 if (unlikely(!pr))
835 return;
837 if (pr->flags.bm_check) {
838 acpi_idle_enter_bm(pr, cx, false);
839 return;
840 } else {
841 ACPI_FLUSH_CPU_CACHE();
844 acpi_idle_do_entry(cx);
847 struct cpuidle_driver acpi_idle_driver = {
848 .name = "acpi_idle",
849 .owner = THIS_MODULE,
853 * acpi_processor_setup_cpuidle_cx - prepares and configures CPUIDLE
854 * device i.e. per-cpu data
856 * @pr: the ACPI processor
857 * @dev : the cpuidle device
859 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
860 struct cpuidle_device *dev)
862 int i, count = CPUIDLE_DRIVER_STATE_START;
863 struct acpi_processor_cx *cx;
865 if (!pr->flags.power_setup_done)
866 return -EINVAL;
868 if (pr->flags.power == 0) {
869 return -EINVAL;
872 if (!dev)
873 return -EINVAL;
875 dev->cpu = pr->id;
877 if (max_cstate == 0)
878 max_cstate = 1;
880 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
881 cx = &pr->power.states[i];
883 if (!cx->valid)
884 continue;
886 per_cpu(acpi_cstate[count], dev->cpu) = cx;
888 count++;
889 if (count == CPUIDLE_STATE_MAX)
890 break;
893 if (!count)
894 return -EINVAL;
896 return 0;
900 * acpi_processor_setup_cpuidle states- prepares and configures cpuidle
901 * global state data i.e. idle routines
903 * @pr: the ACPI processor
905 static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
907 int i, count = CPUIDLE_DRIVER_STATE_START;
908 struct acpi_processor_cx *cx;
909 struct cpuidle_state *state;
910 struct cpuidle_driver *drv = &acpi_idle_driver;
912 if (!pr->flags.power_setup_done)
913 return -EINVAL;
915 if (pr->flags.power == 0)
916 return -EINVAL;
918 drv->safe_state_index = -1;
919 for (i = CPUIDLE_DRIVER_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
920 drv->states[i].name[0] = '\0';
921 drv->states[i].desc[0] = '\0';
924 if (max_cstate == 0)
925 max_cstate = 1;
927 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
928 cx = &pr->power.states[i];
930 if (!cx->valid)
931 continue;
933 state = &drv->states[count];
934 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
935 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
936 state->exit_latency = cx->latency;
937 state->target_residency = cx->latency * latency_factor;
938 state->enter = acpi_idle_enter;
940 state->flags = 0;
941 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
942 state->enter_dead = acpi_idle_play_dead;
943 drv->safe_state_index = count;
946 * Halt-induced C1 is not good for ->enter_freeze, because it
947 * re-enables interrupts on exit. Moreover, C1 is generally not
948 * particularly interesting from the suspend-to-idle angle, so
949 * avoid C1 and the situations in which we may need to fall back
950 * to it altogether.
952 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
953 state->enter_freeze = acpi_idle_enter_freeze;
955 count++;
956 if (count == CPUIDLE_STATE_MAX)
957 break;
960 drv->state_count = count;
962 if (!count)
963 return -EINVAL;
965 return 0;
968 int acpi_processor_hotplug(struct acpi_processor *pr)
970 int ret = 0;
971 struct cpuidle_device *dev;
973 if (disabled_by_idle_boot_param())
974 return 0;
976 if (nocst)
977 return -ENODEV;
979 if (!pr->flags.power_setup_done)
980 return -ENODEV;
982 dev = per_cpu(acpi_cpuidle_device, pr->id);
983 cpuidle_pause_and_lock();
984 cpuidle_disable_device(dev);
985 acpi_processor_get_power_info(pr);
986 if (pr->flags.power) {
987 acpi_processor_setup_cpuidle_cx(pr, dev);
988 ret = cpuidle_enable_device(dev);
990 cpuidle_resume_and_unlock();
992 return ret;
995 int acpi_processor_cst_has_changed(struct acpi_processor *pr)
997 int cpu;
998 struct acpi_processor *_pr;
999 struct cpuidle_device *dev;
1001 if (disabled_by_idle_boot_param())
1002 return 0;
1004 if (nocst)
1005 return -ENODEV;
1007 if (!pr->flags.power_setup_done)
1008 return -ENODEV;
1011 * FIXME: Design the ACPI notification to make it once per
1012 * system instead of once per-cpu. This condition is a hack
1013 * to make the code that updates C-States be called once.
1016 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1018 /* Protect against cpu-hotplug */
1019 get_online_cpus();
1020 cpuidle_pause_and_lock();
1022 /* Disable all cpuidle devices */
1023 for_each_online_cpu(cpu) {
1024 _pr = per_cpu(processors, cpu);
1025 if (!_pr || !_pr->flags.power_setup_done)
1026 continue;
1027 dev = per_cpu(acpi_cpuidle_device, cpu);
1028 cpuidle_disable_device(dev);
1031 /* Populate Updated C-state information */
1032 acpi_processor_get_power_info(pr);
1033 acpi_processor_setup_cpuidle_states(pr);
1035 /* Enable all cpuidle devices */
1036 for_each_online_cpu(cpu) {
1037 _pr = per_cpu(processors, cpu);
1038 if (!_pr || !_pr->flags.power_setup_done)
1039 continue;
1040 acpi_processor_get_power_info(_pr);
1041 if (_pr->flags.power) {
1042 dev = per_cpu(acpi_cpuidle_device, cpu);
1043 acpi_processor_setup_cpuidle_cx(_pr, dev);
1044 cpuidle_enable_device(dev);
1047 cpuidle_resume_and_unlock();
1048 put_online_cpus();
1051 return 0;
1054 static int acpi_processor_registered;
1056 int acpi_processor_power_init(struct acpi_processor *pr)
1058 acpi_status status;
1059 int retval;
1060 struct cpuidle_device *dev;
1061 static int first_run;
1063 if (disabled_by_idle_boot_param())
1064 return 0;
1066 if (!first_run) {
1067 dmi_check_system(processor_power_dmi_table);
1068 max_cstate = acpi_processor_cstate_check(max_cstate);
1069 if (max_cstate < ACPI_C_STATES_MAX)
1070 printk(KERN_NOTICE
1071 "ACPI: processor limited to max C-state %d\n",
1072 max_cstate);
1073 first_run++;
1076 if (acpi_gbl_FADT.cst_control && !nocst) {
1077 status =
1078 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
1079 if (ACPI_FAILURE(status)) {
1080 ACPI_EXCEPTION((AE_INFO, status,
1081 "Notifying BIOS of _CST ability failed"));
1085 acpi_processor_get_power_info(pr);
1086 pr->flags.power_setup_done = 1;
1089 * Install the idle handler if processor power management is supported.
1090 * Note that we use previously set idle handler will be used on
1091 * platforms that only support C1.
1093 if (pr->flags.power) {
1094 /* Register acpi_idle_driver if not already registered */
1095 if (!acpi_processor_registered) {
1096 acpi_processor_setup_cpuidle_states(pr);
1097 retval = cpuidle_register_driver(&acpi_idle_driver);
1098 if (retval)
1099 return retval;
1100 printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
1101 acpi_idle_driver.name);
1104 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1105 if (!dev)
1106 return -ENOMEM;
1107 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1109 acpi_processor_setup_cpuidle_cx(pr, dev);
1111 /* Register per-cpu cpuidle_device. Cpuidle driver
1112 * must already be registered before registering device
1114 retval = cpuidle_register_device(dev);
1115 if (retval) {
1116 if (acpi_processor_registered == 0)
1117 cpuidle_unregister_driver(&acpi_idle_driver);
1118 return retval;
1120 acpi_processor_registered++;
1122 return 0;
1125 int acpi_processor_power_exit(struct acpi_processor *pr)
1127 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1129 if (disabled_by_idle_boot_param())
1130 return 0;
1132 if (pr->flags.power) {
1133 cpuidle_unregister_device(dev);
1134 acpi_processor_registered--;
1135 if (acpi_processor_registered == 0)
1136 cpuidle_unregister_driver(&acpi_idle_driver);
1139 pr->flags.power_setup_done = 0;
1140 return 0;