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 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #define pr_fmt(fmt) "ACPI: " fmt
28 #include <linux/module.h>
29 #include <linux/acpi.h>
30 #include <linux/dmi.h>
31 #include <linux/sched.h> /* need_resched() */
32 #include <linux/sort.h>
33 #include <linux/tick.h>
34 #include <linux/cpuidle.h>
35 #include <linux/cpu.h>
36 #include <acpi/processor.h>
39 * Include the apic definitions for x86 to have the APIC timer related defines
40 * available also for UP (on SMP it gets magically included via linux/smp.h).
41 * asm/acpi.h is not an option, as it would require more include magic. Also
42 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
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 struct cpuidle_driver acpi_idle_driver = {
69 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
71 DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
73 static int disabled_by_idle_boot_param(void)
75 return boot_option_idle_override == IDLE_POLL ||
76 boot_option_idle_override == IDLE_HALT;
80 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
81 * For now disable this. Probably a bug somewhere else.
83 * To skip this limit, boot/load with a large max_cstate limit.
85 static int set_max_cstate(const struct dmi_system_id *id)
87 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
90 pr_notice("%s detected - limiting to C%ld max_cstate."
91 " Override with \"processor.max_cstate=%d\"\n", id->ident,
92 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
94 max_cstate = (long)id->driver_data;
99 static const struct dmi_system_id processor_power_dmi_table[] = {
100 { set_max_cstate, "Clevo 5600D", {
101 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
102 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
104 { set_max_cstate, "Pavilion zv5000", {
105 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
106 DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
108 { set_max_cstate, "Asus L8400B", {
109 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
110 DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
117 * Callers should disable interrupts before the call and enable
118 * interrupts after return.
120 static void __cpuidle acpi_safe_halt(void)
122 if (!tif_need_resched()) {
128 #ifdef ARCH_APICTIMER_STOPS_ON_C3
131 * Some BIOS implementations switch to C3 in the published C2 state.
132 * This seems to be a common problem on AMD boxen, but other vendors
133 * are affected too. We pick the most conservative approach: we assume
134 * that the local APIC stops in both C2 and C3.
136 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
137 struct acpi_processor_cx *cx)
139 struct acpi_processor_power *pwr = &pr->power;
140 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
142 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
145 if (amd_e400_c1e_detected)
146 type = ACPI_STATE_C1;
149 * Check, if one of the previous states already marked the lapic
152 if (pwr->timer_broadcast_on_state < state)
155 if (cx->type >= type)
156 pr->power.timer_broadcast_on_state = state;
159 static void __lapic_timer_propagate_broadcast(void *arg)
161 struct acpi_processor *pr = (struct acpi_processor *) arg;
163 if (pr->power.timer_broadcast_on_state < INT_MAX)
164 tick_broadcast_enable();
166 tick_broadcast_disable();
169 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
171 smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
175 /* Power(C) State timer broadcast control */
176 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
177 struct acpi_processor_cx *cx,
180 int state = cx - pr->power.states;
182 if (state >= pr->power.timer_broadcast_on_state) {
184 tick_broadcast_enter();
186 tick_broadcast_exit();
192 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
193 struct acpi_processor_cx *cstate) { }
194 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
195 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
196 struct acpi_processor_cx *cx,
203 #if defined(CONFIG_X86)
204 static void tsc_check_state(int state)
206 switch (boot_cpu_data.x86_vendor) {
208 case X86_VENDOR_INTEL:
210 * AMD Fam10h TSC will tick in all
211 * C/P/S0/S1 states when this bit is set.
213 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
218 /* TSC could halt in idle, so notify users */
219 if (state > ACPI_STATE_C1)
220 mark_tsc_unstable("TSC halts in idle");
224 static void tsc_check_state(int state) { return; }
227 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
233 /* if info is obtained from pblk/fadt, type equals state */
234 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
235 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
237 #ifndef CONFIG_HOTPLUG_CPU
239 * Check for P_LVL2_UP flag before entering C2 and above on
242 if ((num_online_cpus() > 1) &&
243 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
247 /* determine C2 and C3 address from pblk */
248 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
249 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
251 /* determine latencies from FADT */
252 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
253 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
256 * FADT specified C2 latency must be less than or equal to
259 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
260 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
261 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
263 pr->power.states[ACPI_STATE_C2].address = 0;
267 * FADT supplied C3 latency must be less than or equal to
270 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
272 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
274 pr->power.states[ACPI_STATE_C3].address = 0;
277 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
278 "lvl2[0x%08x] lvl3[0x%08x]\n",
279 pr->power.states[ACPI_STATE_C2].address,
280 pr->power.states[ACPI_STATE_C3].address));
285 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
287 if (!pr->power.states[ACPI_STATE_C1].valid) {
288 /* set the first C-State to C1 */
289 /* all processors need to support C1 */
290 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
291 pr->power.states[ACPI_STATE_C1].valid = 1;
292 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
294 /* the C0 state only exists as a filler in our array */
295 pr->power.states[ACPI_STATE_C0].valid = 1;
299 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
305 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
306 union acpi_object *cst;
313 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
314 if (ACPI_FAILURE(status)) {
315 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
319 cst = buffer.pointer;
321 /* There must be at least 2 elements */
322 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
323 pr_err("not enough elements in _CST\n");
328 count = cst->package.elements[0].integer.value;
330 /* Validate number of power states. */
331 if (count < 1 || count != cst->package.count - 1) {
332 pr_err("count given by _CST is not valid\n");
337 /* Tell driver that at least _CST is supported. */
338 pr->flags.has_cst = 1;
340 for (i = 1; i <= count; i++) {
341 union acpi_object *element;
342 union acpi_object *obj;
343 struct acpi_power_register *reg;
344 struct acpi_processor_cx cx;
346 memset(&cx, 0, sizeof(cx));
348 element = &(cst->package.elements[i]);
349 if (element->type != ACPI_TYPE_PACKAGE)
352 if (element->package.count != 4)
355 obj = &(element->package.elements[0]);
357 if (obj->type != ACPI_TYPE_BUFFER)
360 reg = (struct acpi_power_register *)obj->buffer.pointer;
362 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
363 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
366 /* There should be an easy way to extract an integer... */
367 obj = &(element->package.elements[1]);
368 if (obj->type != ACPI_TYPE_INTEGER)
371 cx.type = obj->integer.value;
373 * Some buggy BIOSes won't list C1 in _CST -
374 * Let acpi_processor_get_power_info_default() handle them later
376 if (i == 1 && cx.type != ACPI_STATE_C1)
379 cx.address = reg->address;
380 cx.index = current_count + 1;
382 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
383 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
384 if (acpi_processor_ffh_cstate_probe
385 (pr->id, &cx, reg) == 0) {
386 cx.entry_method = ACPI_CSTATE_FFH;
387 } else if (cx.type == ACPI_STATE_C1) {
389 * C1 is a special case where FIXED_HARDWARE
390 * can be handled in non-MWAIT way as well.
391 * In that case, save this _CST entry info.
392 * Otherwise, ignore this info and continue.
394 cx.entry_method = ACPI_CSTATE_HALT;
395 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
399 if (cx.type == ACPI_STATE_C1 &&
400 (boot_option_idle_override == IDLE_NOMWAIT)) {
402 * In most cases the C1 space_id obtained from
403 * _CST object is FIXED_HARDWARE access mode.
404 * But when the option of idle=halt is added,
405 * the entry_method type should be changed from
406 * CSTATE_FFH to CSTATE_HALT.
407 * When the option of idle=nomwait is added,
408 * the C1 entry_method type should be
411 cx.entry_method = ACPI_CSTATE_HALT;
412 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
415 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
419 if (cx.type == ACPI_STATE_C1) {
423 obj = &(element->package.elements[2]);
424 if (obj->type != ACPI_TYPE_INTEGER)
427 cx.latency = obj->integer.value;
429 obj = &(element->package.elements[3]);
430 if (obj->type != ACPI_TYPE_INTEGER)
434 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
437 * We support total ACPI_PROCESSOR_MAX_POWER - 1
438 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
440 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
441 pr_warn("Limiting number of power states to max (%d)\n",
442 ACPI_PROCESSOR_MAX_POWER);
443 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
448 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
451 /* Validate number of power states discovered */
452 if (current_count < 2)
456 kfree(buffer.pointer);
461 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
462 struct acpi_processor_cx *cx)
464 static int bm_check_flag = -1;
465 static int bm_control_flag = -1;
472 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
473 * DMA transfers are used by any ISA device to avoid livelock.
474 * Note that we could disable Type-F DMA (as recommended by
475 * the erratum), but this is known to disrupt certain ISA
476 * devices thus we take the conservative approach.
478 else if (errata.piix4.fdma) {
479 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
480 "C3 not supported on PIIX4 with Type-F DMA\n"));
484 /* All the logic here assumes flags.bm_check is same across all CPUs */
485 if (bm_check_flag == -1) {
486 /* Determine whether bm_check is needed based on CPU */
487 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
488 bm_check_flag = pr->flags.bm_check;
489 bm_control_flag = pr->flags.bm_control;
491 pr->flags.bm_check = bm_check_flag;
492 pr->flags.bm_control = bm_control_flag;
495 if (pr->flags.bm_check) {
496 if (!pr->flags.bm_control) {
497 if (pr->flags.has_cst != 1) {
498 /* bus mastering control is necessary */
499 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
500 "C3 support requires BM control\n"));
503 /* Here we enter C3 without bus mastering */
504 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
505 "C3 support without BM control\n"));
510 * WBINVD should be set in fadt, for C3 state to be
511 * supported on when bm_check is not required.
513 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
514 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
515 "Cache invalidation should work properly"
516 " for C3 to be enabled on SMP systems\n"));
522 * Otherwise we've met all of our C3 requirements.
523 * Normalize the C3 latency to expidite policy. Enable
524 * checking of bus mastering status (bm_check) so we can
525 * use this in our C3 policy
530 * On older chipsets, BM_RLD needs to be set
531 * in order for Bus Master activity to wake the
532 * system from C3. Newer chipsets handle DMA
533 * during C3 automatically and BM_RLD is a NOP.
534 * In either case, the proper way to
535 * handle BM_RLD is to set it and leave it set.
537 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
542 static int acpi_cst_latency_cmp(const void *a, const void *b)
544 const struct acpi_processor_cx *x = a, *y = b;
546 if (!(x->valid && y->valid))
548 if (x->latency > y->latency)
550 if (x->latency < y->latency)
554 static void acpi_cst_latency_swap(void *a, void *b, int n)
556 struct acpi_processor_cx *x = a, *y = b;
559 if (!(x->valid && y->valid))
562 x->latency = y->latency;
566 static int acpi_processor_power_verify(struct acpi_processor *pr)
569 unsigned int working = 0;
570 unsigned int last_latency = 0;
571 unsigned int last_type = 0;
572 bool buggy_latency = false;
574 pr->power.timer_broadcast_on_state = INT_MAX;
576 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
577 struct acpi_processor_cx *cx = &pr->power.states[i];
591 acpi_processor_power_verify_c3(pr, cx);
596 if (cx->type >= last_type && cx->latency < last_latency)
597 buggy_latency = true;
598 last_latency = cx->latency;
599 last_type = cx->type;
601 lapic_timer_check_state(i, pr, cx);
602 tsc_check_state(cx->type);
607 pr_notice("FW issue: working around C-state latencies out of order\n");
608 sort(&pr->power.states[1], max_cstate,
609 sizeof(struct acpi_processor_cx),
610 acpi_cst_latency_cmp,
611 acpi_cst_latency_swap);
614 lapic_timer_propagate_broadcast(pr);
619 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
625 /* NOTE: the idle thread may not be running while calling
628 /* Zero initialize all the C-states info. */
629 memset(pr->power.states, 0, sizeof(pr->power.states));
631 result = acpi_processor_get_power_info_cst(pr);
632 if (result == -ENODEV)
633 result = acpi_processor_get_power_info_fadt(pr);
638 acpi_processor_get_power_info_default(pr);
640 pr->power.count = acpi_processor_power_verify(pr);
643 * if one state of type C2 or C3 is available, mark this
644 * CPU as being "idle manageable"
646 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
647 if (pr->power.states[i].valid) {
649 if (pr->power.states[i].type >= ACPI_STATE_C2)
658 * acpi_idle_bm_check - checks if bus master activity was detected
660 static int acpi_idle_bm_check(void)
664 if (bm_check_disable)
667 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
669 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
671 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
672 * the true state of bus mastering activity; forcing us to
673 * manually check the BMIDEA bit of each IDE channel.
675 else if (errata.piix4.bmisx) {
676 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
677 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
684 * acpi_idle_do_entry - enter idle state using the appropriate method
687 * Caller disables interrupt before call and enables interrupt after return.
689 static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
691 if (cx->entry_method == ACPI_CSTATE_FFH) {
692 /* Call into architectural FFH based C-state */
693 acpi_processor_ffh_cstate_enter(cx);
694 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
697 /* IO port based C-state */
699 /* Dummy wait op - must do something useless after P_LVL2 read
700 because chipsets cannot guarantee that STPCLK# signal
701 gets asserted in time to freeze execution properly. */
702 inl(acpi_gbl_FADT.xpm_timer_block.address);
707 * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
708 * @dev: the target CPU
709 * @index: the index of suggested state
711 static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
713 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
715 ACPI_FLUSH_CPU_CACHE();
719 if (cx->entry_method == ACPI_CSTATE_HALT)
721 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
723 /* See comment in acpi_idle_do_entry() */
724 inl(acpi_gbl_FADT.xpm_timer_block.address);
733 static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
735 return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
736 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
739 static int c3_cpu_count;
740 static DEFINE_RAW_SPINLOCK(c3_lock);
743 * acpi_idle_enter_bm - enters C3 with proper BM handling
744 * @pr: Target processor
745 * @cx: Target state context
746 * @timer_bc: Whether or not to change timer mode to broadcast
748 static void acpi_idle_enter_bm(struct acpi_processor *pr,
749 struct acpi_processor_cx *cx, bool timer_bc)
751 acpi_unlazy_tlb(smp_processor_id());
754 * Must be done before busmaster disable as we might need to
758 lapic_timer_state_broadcast(pr, cx, 1);
762 * bm_check implies we need ARB_DIS
763 * bm_control implies whether we can do ARB_DIS
765 * That leaves a case where bm_check is set and bm_control is
766 * not set. In that case we cannot do much, we enter C3
767 * without doing anything.
769 if (pr->flags.bm_control) {
770 raw_spin_lock(&c3_lock);
772 /* Disable bus master arbitration when all CPUs are in C3 */
773 if (c3_cpu_count == num_online_cpus())
774 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
775 raw_spin_unlock(&c3_lock);
778 acpi_idle_do_entry(cx);
780 /* Re-enable bus master arbitration */
781 if (pr->flags.bm_control) {
782 raw_spin_lock(&c3_lock);
783 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
785 raw_spin_unlock(&c3_lock);
789 lapic_timer_state_broadcast(pr, cx, 0);
792 static int acpi_idle_enter(struct cpuidle_device *dev,
793 struct cpuidle_driver *drv, int index)
795 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
796 struct acpi_processor *pr;
798 pr = __this_cpu_read(processors);
802 if (cx->type != ACPI_STATE_C1) {
803 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
804 index = CPUIDLE_DRIVER_STATE_START;
805 cx = per_cpu(acpi_cstate[index], dev->cpu);
806 } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
807 if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
808 acpi_idle_enter_bm(pr, cx, true);
810 } else if (drv->safe_state_index >= 0) {
811 index = drv->safe_state_index;
812 cx = per_cpu(acpi_cstate[index], dev->cpu);
820 lapic_timer_state_broadcast(pr, cx, 1);
822 if (cx->type == ACPI_STATE_C3)
823 ACPI_FLUSH_CPU_CACHE();
825 acpi_idle_do_entry(cx);
827 lapic_timer_state_broadcast(pr, cx, 0);
832 static void acpi_idle_enter_freeze(struct cpuidle_device *dev,
833 struct cpuidle_driver *drv, int index)
835 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
837 if (cx->type == ACPI_STATE_C3) {
838 struct acpi_processor *pr = __this_cpu_read(processors);
843 if (pr->flags.bm_check) {
844 acpi_idle_enter_bm(pr, cx, false);
847 ACPI_FLUSH_CPU_CACHE();
850 acpi_idle_do_entry(cx);
853 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
854 struct cpuidle_device *dev)
856 int i, count = CPUIDLE_DRIVER_STATE_START;
857 struct acpi_processor_cx *cx;
862 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
863 cx = &pr->power.states[i];
868 per_cpu(acpi_cstate[count], dev->cpu) = cx;
871 if (count == CPUIDLE_STATE_MAX)
881 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
883 int i, count = CPUIDLE_DRIVER_STATE_START;
884 struct acpi_processor_cx *cx;
885 struct cpuidle_state *state;
886 struct cpuidle_driver *drv = &acpi_idle_driver;
891 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
892 cx = &pr->power.states[i];
897 state = &drv->states[count];
898 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
899 strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
900 state->exit_latency = cx->latency;
901 state->target_residency = cx->latency * latency_factor;
902 state->enter = acpi_idle_enter;
905 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
906 state->enter_dead = acpi_idle_play_dead;
907 drv->safe_state_index = count;
910 * Halt-induced C1 is not good for ->enter_freeze, because it
911 * re-enables interrupts on exit. Moreover, C1 is generally not
912 * particularly interesting from the suspend-to-idle angle, so
913 * avoid C1 and the situations in which we may need to fall back
916 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
917 state->enter_freeze = acpi_idle_enter_freeze;
920 if (count == CPUIDLE_STATE_MAX)
924 drv->state_count = count;
932 static inline void acpi_processor_cstate_first_run_checks(void)
935 static int first_run;
939 dmi_check_system(processor_power_dmi_table);
940 max_cstate = acpi_processor_cstate_check(max_cstate);
941 if (max_cstate < ACPI_C_STATES_MAX)
942 pr_notice("ACPI: processor limited to max C-state %d\n",
946 if (acpi_gbl_FADT.cst_control && !nocst) {
947 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
948 acpi_gbl_FADT.cst_control, 8);
949 if (ACPI_FAILURE(status))
950 ACPI_EXCEPTION((AE_INFO, status,
951 "Notifying BIOS of _CST ability failed"));
956 static inline int disabled_by_idle_boot_param(void) { return 0; }
957 static inline void acpi_processor_cstate_first_run_checks(void) { }
958 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
963 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
964 struct cpuidle_device *dev)
969 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
974 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
976 struct acpi_lpi_states_array {
978 unsigned int composite_states_size;
979 struct acpi_lpi_state *entries;
980 struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
983 static int obj_get_integer(union acpi_object *obj, u32 *value)
985 if (obj->type != ACPI_TYPE_INTEGER)
988 *value = obj->integer.value;
992 static int acpi_processor_evaluate_lpi(acpi_handle handle,
993 struct acpi_lpi_states_array *info)
997 int pkg_count, state_idx = 1, loop;
998 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
999 union acpi_object *lpi_data;
1000 struct acpi_lpi_state *lpi_state;
1002 status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
1003 if (ACPI_FAILURE(status)) {
1004 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
1008 lpi_data = buffer.pointer;
1010 /* There must be at least 4 elements = 3 elements + 1 package */
1011 if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
1012 lpi_data->package.count < 4) {
1013 pr_debug("not enough elements in _LPI\n");
1018 pkg_count = lpi_data->package.elements[2].integer.value;
1020 /* Validate number of power states. */
1021 if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
1022 pr_debug("count given by _LPI is not valid\n");
1027 lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
1033 info->size = pkg_count;
1034 info->entries = lpi_state;
1036 /* LPI States start at index 3 */
1037 for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
1038 union acpi_object *element, *pkg_elem, *obj;
1040 element = &lpi_data->package.elements[loop];
1041 if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
1044 pkg_elem = element->package.elements;
1047 if (obj->type == ACPI_TYPE_BUFFER) {
1048 struct acpi_power_register *reg;
1050 reg = (struct acpi_power_register *)obj->buffer.pointer;
1051 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
1052 reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
1055 lpi_state->address = reg->address;
1056 lpi_state->entry_method =
1057 reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
1058 ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
1059 } else if (obj->type == ACPI_TYPE_INTEGER) {
1060 lpi_state->entry_method = ACPI_CSTATE_INTEGER;
1061 lpi_state->address = obj->integer.value;
1066 /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
1069 if (obj->type == ACPI_TYPE_STRING)
1070 strlcpy(lpi_state->desc, obj->string.pointer,
1073 lpi_state->index = state_idx;
1074 if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
1075 pr_debug("No min. residency found, assuming 10 us\n");
1076 lpi_state->min_residency = 10;
1079 if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
1080 pr_debug("No wakeup residency found, assuming 10 us\n");
1081 lpi_state->wake_latency = 10;
1084 if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
1085 lpi_state->flags = 0;
1087 if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
1088 lpi_state->arch_flags = 0;
1090 if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
1091 lpi_state->res_cnt_freq = 1;
1093 if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
1094 lpi_state->enable_parent_state = 0;
1097 acpi_handle_debug(handle, "Found %d power states\n", state_idx);
1099 kfree(buffer.pointer);
1104 * flat_state_cnt - the number of composite LPI states after the process of flattening
1106 static int flat_state_cnt;
1109 * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
1111 * @local: local LPI state
1112 * @parent: parent LPI state
1113 * @result: composite LPI state
1115 static bool combine_lpi_states(struct acpi_lpi_state *local,
1116 struct acpi_lpi_state *parent,
1117 struct acpi_lpi_state *result)
1119 if (parent->entry_method == ACPI_CSTATE_INTEGER) {
1120 if (!parent->address) /* 0 means autopromotable */
1122 result->address = local->address + parent->address;
1124 result->address = parent->address;
1127 result->min_residency = max(local->min_residency, parent->min_residency);
1128 result->wake_latency = local->wake_latency + parent->wake_latency;
1129 result->enable_parent_state = parent->enable_parent_state;
1130 result->entry_method = local->entry_method;
1132 result->flags = parent->flags;
1133 result->arch_flags = parent->arch_flags;
1134 result->index = parent->index;
1136 strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
1137 strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
1138 strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
1142 #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0)
1144 static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
1145 struct acpi_lpi_state *t)
1147 curr_level->composite_states[curr_level->composite_states_size++] = t;
1150 static int flatten_lpi_states(struct acpi_processor *pr,
1151 struct acpi_lpi_states_array *curr_level,
1152 struct acpi_lpi_states_array *prev_level)
1154 int i, j, state_count = curr_level->size;
1155 struct acpi_lpi_state *p, *t = curr_level->entries;
1157 curr_level->composite_states_size = 0;
1158 for (j = 0; j < state_count; j++, t++) {
1159 struct acpi_lpi_state *flpi;
1161 if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
1164 if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
1165 pr_warn("Limiting number of LPI states to max (%d)\n",
1166 ACPI_PROCESSOR_MAX_POWER);
1167 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1171 flpi = &pr->power.lpi_states[flat_state_cnt];
1173 if (!prev_level) { /* leaf/processor node */
1174 memcpy(flpi, t, sizeof(*t));
1175 stash_composite_state(curr_level, flpi);
1180 for (i = 0; i < prev_level->composite_states_size; i++) {
1181 p = prev_level->composite_states[i];
1182 if (t->index <= p->enable_parent_state &&
1183 combine_lpi_states(p, t, flpi)) {
1184 stash_composite_state(curr_level, flpi);
1191 kfree(curr_level->entries);
1195 static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
1199 acpi_handle handle = pr->handle, pr_ahandle;
1200 struct acpi_device *d = NULL;
1201 struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
1203 if (!osc_pc_lpi_support_confirmed)
1206 if (!acpi_has_method(handle, "_LPI"))
1212 handle = pr->handle;
1213 ret = acpi_processor_evaluate_lpi(handle, prev);
1216 flatten_lpi_states(pr, prev, NULL);
1218 status = acpi_get_parent(handle, &pr_ahandle);
1219 while (ACPI_SUCCESS(status)) {
1220 acpi_bus_get_device(pr_ahandle, &d);
1221 handle = pr_ahandle;
1223 if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
1226 /* can be optional ? */
1227 if (!acpi_has_method(handle, "_LPI"))
1230 ret = acpi_processor_evaluate_lpi(handle, curr);
1234 /* flatten all the LPI states in this level of hierarchy */
1235 flatten_lpi_states(pr, curr, prev);
1237 tmp = prev, prev = curr, curr = tmp;
1239 status = acpi_get_parent(handle, &pr_ahandle);
1242 pr->power.count = flat_state_cnt;
1243 /* reset the index after flattening */
1244 for (i = 0; i < pr->power.count; i++)
1245 pr->power.lpi_states[i].index = i;
1247 /* Tell driver that _LPI is supported. */
1248 pr->flags.has_lpi = 1;
1249 pr->flags.power = 1;
1254 int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
1259 int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
1265 * acpi_idle_lpi_enter - enters an ACPI any LPI state
1266 * @dev: the target CPU
1267 * @drv: cpuidle driver containing cpuidle state info
1268 * @index: index of target state
1270 * Return: 0 for success or negative value for error
1272 static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
1273 struct cpuidle_driver *drv, int index)
1275 struct acpi_processor *pr;
1276 struct acpi_lpi_state *lpi;
1278 pr = __this_cpu_read(processors);
1283 lpi = &pr->power.lpi_states[index];
1284 if (lpi->entry_method == ACPI_CSTATE_FFH)
1285 return acpi_processor_ffh_lpi_enter(lpi);
1290 static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
1293 struct acpi_lpi_state *lpi;
1294 struct cpuidle_state *state;
1295 struct cpuidle_driver *drv = &acpi_idle_driver;
1297 if (!pr->flags.has_lpi)
1300 for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
1301 lpi = &pr->power.lpi_states[i];
1303 state = &drv->states[i];
1304 snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
1305 strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
1306 state->exit_latency = lpi->wake_latency;
1307 state->target_residency = lpi->min_residency;
1308 if (lpi->arch_flags)
1309 state->flags |= CPUIDLE_FLAG_TIMER_STOP;
1310 state->enter = acpi_idle_lpi_enter;
1311 drv->safe_state_index = i;
1314 drv->state_count = i;
1320 * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
1321 * global state data i.e. idle routines
1323 * @pr: the ACPI processor
1325 static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
1328 struct cpuidle_driver *drv = &acpi_idle_driver;
1330 if (!pr->flags.power_setup_done || !pr->flags.power)
1333 drv->safe_state_index = -1;
1334 for (i = CPUIDLE_DRIVER_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
1335 drv->states[i].name[0] = '\0';
1336 drv->states[i].desc[0] = '\0';
1339 if (pr->flags.has_lpi)
1340 return acpi_processor_setup_lpi_states(pr);
1342 return acpi_processor_setup_cstates(pr);
1346 * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
1347 * device i.e. per-cpu data
1349 * @pr: the ACPI processor
1350 * @dev : the cpuidle device
1352 static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
1353 struct cpuidle_device *dev)
1355 if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
1359 if (pr->flags.has_lpi)
1360 return acpi_processor_ffh_lpi_probe(pr->id);
1362 return acpi_processor_setup_cpuidle_cx(pr, dev);
1365 static int acpi_processor_get_power_info(struct acpi_processor *pr)
1369 ret = acpi_processor_get_lpi_info(pr);
1371 ret = acpi_processor_get_cstate_info(pr);
1376 int acpi_processor_hotplug(struct acpi_processor *pr)
1379 struct cpuidle_device *dev;
1381 if (disabled_by_idle_boot_param())
1384 if (!pr->flags.power_setup_done)
1387 dev = per_cpu(acpi_cpuidle_device, pr->id);
1388 cpuidle_pause_and_lock();
1389 cpuidle_disable_device(dev);
1390 ret = acpi_processor_get_power_info(pr);
1391 if (!ret && pr->flags.power) {
1392 acpi_processor_setup_cpuidle_dev(pr, dev);
1393 ret = cpuidle_enable_device(dev);
1395 cpuidle_resume_and_unlock();
1400 int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
1403 struct acpi_processor *_pr;
1404 struct cpuidle_device *dev;
1406 if (disabled_by_idle_boot_param())
1409 if (!pr->flags.power_setup_done)
1413 * FIXME: Design the ACPI notification to make it once per
1414 * system instead of once per-cpu. This condition is a hack
1415 * to make the code that updates C-States be called once.
1418 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1420 /* Protect against cpu-hotplug */
1422 cpuidle_pause_and_lock();
1424 /* Disable all cpuidle devices */
1425 for_each_online_cpu(cpu) {
1426 _pr = per_cpu(processors, cpu);
1427 if (!_pr || !_pr->flags.power_setup_done)
1429 dev = per_cpu(acpi_cpuidle_device, cpu);
1430 cpuidle_disable_device(dev);
1433 /* Populate Updated C-state information */
1434 acpi_processor_get_power_info(pr);
1435 acpi_processor_setup_cpuidle_states(pr);
1437 /* Enable all cpuidle devices */
1438 for_each_online_cpu(cpu) {
1439 _pr = per_cpu(processors, cpu);
1440 if (!_pr || !_pr->flags.power_setup_done)
1442 acpi_processor_get_power_info(_pr);
1443 if (_pr->flags.power) {
1444 dev = per_cpu(acpi_cpuidle_device, cpu);
1445 acpi_processor_setup_cpuidle_dev(_pr, dev);
1446 cpuidle_enable_device(dev);
1449 cpuidle_resume_and_unlock();
1456 static int acpi_processor_registered;
1458 int acpi_processor_power_init(struct acpi_processor *pr)
1461 struct cpuidle_device *dev;
1463 if (disabled_by_idle_boot_param())
1466 acpi_processor_cstate_first_run_checks();
1468 if (!acpi_processor_get_power_info(pr))
1469 pr->flags.power_setup_done = 1;
1472 * Install the idle handler if processor power management is supported.
1473 * Note that we use previously set idle handler will be used on
1474 * platforms that only support C1.
1476 if (pr->flags.power) {
1477 /* Register acpi_idle_driver if not already registered */
1478 if (!acpi_processor_registered) {
1479 acpi_processor_setup_cpuidle_states(pr);
1480 retval = cpuidle_register_driver(&acpi_idle_driver);
1483 pr_debug("%s registered with cpuidle\n",
1484 acpi_idle_driver.name);
1487 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1490 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1492 acpi_processor_setup_cpuidle_dev(pr, dev);
1494 /* Register per-cpu cpuidle_device. Cpuidle driver
1495 * must already be registered before registering device
1497 retval = cpuidle_register_device(dev);
1499 if (acpi_processor_registered == 0)
1500 cpuidle_unregister_driver(&acpi_idle_driver);
1503 acpi_processor_registered++;
1508 int acpi_processor_power_exit(struct acpi_processor *pr)
1510 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1512 if (disabled_by_idle_boot_param())
1515 if (pr->flags.power) {
1516 cpuidle_unregister_device(dev);
1517 acpi_processor_registered--;
1518 if (acpi_processor_registered == 0)
1519 cpuidle_unregister_driver(&acpi_idle_driver);
1522 pr->flags.power_setup_done = 0;