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 #define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0)
54 static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
55 module_param(max_cstate, uint, 0000);
56 static unsigned int nocst __read_mostly;
57 module_param(nocst, uint, 0000);
58 static int bm_check_disable __read_mostly;
59 module_param(bm_check_disable, uint, 0000);
61 static unsigned int latency_factor __read_mostly = 2;
62 module_param(latency_factor, uint, 0644);
64 static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
66 struct cpuidle_driver acpi_idle_driver = {
71 #ifdef CONFIG_ACPI_PROCESSOR_CSTATE
73 DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
75 static int disabled_by_idle_boot_param(void)
77 return boot_option_idle_override == IDLE_POLL ||
78 boot_option_idle_override == IDLE_HALT;
82 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
83 * For now disable this. Probably a bug somewhere else.
85 * To skip this limit, boot/load with a large max_cstate limit.
87 static int set_max_cstate(const struct dmi_system_id *id)
89 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
92 pr_notice("%s detected - limiting to C%ld max_cstate."
93 " Override with \"processor.max_cstate=%d\"\n", id->ident,
94 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
96 max_cstate = (long)id->driver_data;
101 static const struct dmi_system_id processor_power_dmi_table[] = {
102 { set_max_cstate, "Clevo 5600D", {
103 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
104 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
106 { set_max_cstate, "Pavilion zv5000", {
107 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
108 DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
110 { set_max_cstate, "Asus L8400B", {
111 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
112 DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
119 * Callers should disable interrupts before the call and enable
120 * interrupts after return.
122 static void __cpuidle acpi_safe_halt(void)
124 if (!tif_need_resched()) {
130 #ifdef ARCH_APICTIMER_STOPS_ON_C3
133 * Some BIOS implementations switch to C3 in the published C2 state.
134 * This seems to be a common problem on AMD boxen, but other vendors
135 * are affected too. We pick the most conservative approach: we assume
136 * that the local APIC stops in both C2 and C3.
138 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
139 struct acpi_processor_cx *cx)
141 struct acpi_processor_power *pwr = &pr->power;
142 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
144 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
147 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
148 type = ACPI_STATE_C1;
151 * Check, if one of the previous states already marked the lapic
154 if (pwr->timer_broadcast_on_state < state)
157 if (cx->type >= type)
158 pr->power.timer_broadcast_on_state = state;
161 static void __lapic_timer_propagate_broadcast(void *arg)
163 struct acpi_processor *pr = (struct acpi_processor *) arg;
165 if (pr->power.timer_broadcast_on_state < INT_MAX)
166 tick_broadcast_enable();
168 tick_broadcast_disable();
171 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
173 smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
177 /* Power(C) State timer broadcast control */
178 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
179 struct acpi_processor_cx *cx,
182 int state = cx - pr->power.states;
184 if (state >= pr->power.timer_broadcast_on_state) {
186 tick_broadcast_enter();
188 tick_broadcast_exit();
194 static void lapic_timer_check_state(int state, struct acpi_processor *pr,
195 struct acpi_processor_cx *cstate) { }
196 static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
197 static void lapic_timer_state_broadcast(struct acpi_processor *pr,
198 struct acpi_processor_cx *cx,
205 #if defined(CONFIG_X86)
206 static void tsc_check_state(int state)
208 switch (boot_cpu_data.x86_vendor) {
210 case X86_VENDOR_INTEL:
211 case X86_VENDOR_CENTAUR:
213 * AMD Fam10h TSC will tick in all
214 * C/P/S0/S1 states when this bit is set.
216 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
221 /* TSC could halt in idle, so notify users */
222 if (state > ACPI_STATE_C1)
223 mark_tsc_unstable("TSC halts in idle");
227 static void tsc_check_state(int state) { return; }
230 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
236 /* if info is obtained from pblk/fadt, type equals state */
237 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
238 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
240 #ifndef CONFIG_HOTPLUG_CPU
242 * Check for P_LVL2_UP flag before entering C2 and above on
245 if ((num_online_cpus() > 1) &&
246 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
250 /* determine C2 and C3 address from pblk */
251 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
252 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
254 /* determine latencies from FADT */
255 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
256 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
259 * FADT specified C2 latency must be less than or equal to
262 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
263 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
264 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
266 pr->power.states[ACPI_STATE_C2].address = 0;
270 * FADT supplied C3 latency must be less than or equal to
273 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
274 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
275 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
277 pr->power.states[ACPI_STATE_C3].address = 0;
280 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
281 "lvl2[0x%08x] lvl3[0x%08x]\n",
282 pr->power.states[ACPI_STATE_C2].address,
283 pr->power.states[ACPI_STATE_C3].address));
288 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
290 if (!pr->power.states[ACPI_STATE_C1].valid) {
291 /* set the first C-State to C1 */
292 /* all processors need to support C1 */
293 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
294 pr->power.states[ACPI_STATE_C1].valid = 1;
295 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
297 snprintf(pr->power.states[ACPI_STATE_C1].desc,
298 ACPI_CX_DESC_LEN, "ACPI HLT");
300 /* the C0 state only exists as a filler in our array */
301 pr->power.states[ACPI_STATE_C0].valid = 1;
305 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
311 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
312 union acpi_object *cst;
319 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
320 if (ACPI_FAILURE(status)) {
321 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
325 cst = buffer.pointer;
327 /* There must be at least 2 elements */
328 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
329 pr_err("not enough elements in _CST\n");
334 count = cst->package.elements[0].integer.value;
336 /* Validate number of power states. */
337 if (count < 1 || count != cst->package.count - 1) {
338 pr_err("count given by _CST is not valid\n");
343 /* Tell driver that at least _CST is supported. */
344 pr->flags.has_cst = 1;
346 for (i = 1; i <= count; i++) {
347 union acpi_object *element;
348 union acpi_object *obj;
349 struct acpi_power_register *reg;
350 struct acpi_processor_cx cx;
352 memset(&cx, 0, sizeof(cx));
354 element = &(cst->package.elements[i]);
355 if (element->type != ACPI_TYPE_PACKAGE)
358 if (element->package.count != 4)
361 obj = &(element->package.elements[0]);
363 if (obj->type != ACPI_TYPE_BUFFER)
366 reg = (struct acpi_power_register *)obj->buffer.pointer;
368 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
369 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
372 /* There should be an easy way to extract an integer... */
373 obj = &(element->package.elements[1]);
374 if (obj->type != ACPI_TYPE_INTEGER)
377 cx.type = obj->integer.value;
379 * Some buggy BIOSes won't list C1 in _CST -
380 * Let acpi_processor_get_power_info_default() handle them later
382 if (i == 1 && cx.type != ACPI_STATE_C1)
385 cx.address = reg->address;
386 cx.index = current_count + 1;
388 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
389 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
390 if (acpi_processor_ffh_cstate_probe
391 (pr->id, &cx, reg) == 0) {
392 cx.entry_method = ACPI_CSTATE_FFH;
393 } else if (cx.type == ACPI_STATE_C1) {
395 * C1 is a special case where FIXED_HARDWARE
396 * can be handled in non-MWAIT way as well.
397 * In that case, save this _CST entry info.
398 * Otherwise, ignore this info and continue.
400 cx.entry_method = ACPI_CSTATE_HALT;
401 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
405 if (cx.type == ACPI_STATE_C1 &&
406 (boot_option_idle_override == IDLE_NOMWAIT)) {
408 * In most cases the C1 space_id obtained from
409 * _CST object is FIXED_HARDWARE access mode.
410 * But when the option of idle=halt is added,
411 * the entry_method type should be changed from
412 * CSTATE_FFH to CSTATE_HALT.
413 * When the option of idle=nomwait is added,
414 * the C1 entry_method type should be
417 cx.entry_method = ACPI_CSTATE_HALT;
418 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
421 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
425 if (cx.type == ACPI_STATE_C1) {
429 obj = &(element->package.elements[2]);
430 if (obj->type != ACPI_TYPE_INTEGER)
433 cx.latency = obj->integer.value;
435 obj = &(element->package.elements[3]);
436 if (obj->type != ACPI_TYPE_INTEGER)
440 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
443 * We support total ACPI_PROCESSOR_MAX_POWER - 1
444 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
446 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
447 pr_warn("Limiting number of power states to max (%d)\n",
448 ACPI_PROCESSOR_MAX_POWER);
449 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
454 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
457 /* Validate number of power states discovered */
458 if (current_count < 2)
462 kfree(buffer.pointer);
467 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
468 struct acpi_processor_cx *cx)
470 static int bm_check_flag = -1;
471 static int bm_control_flag = -1;
478 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
479 * DMA transfers are used by any ISA device to avoid livelock.
480 * Note that we could disable Type-F DMA (as recommended by
481 * the erratum), but this is known to disrupt certain ISA
482 * devices thus we take the conservative approach.
484 else if (errata.piix4.fdma) {
485 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
486 "C3 not supported on PIIX4 with Type-F DMA\n"));
490 /* All the logic here assumes flags.bm_check is same across all CPUs */
491 if (bm_check_flag == -1) {
492 /* Determine whether bm_check is needed based on CPU */
493 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
494 bm_check_flag = pr->flags.bm_check;
495 bm_control_flag = pr->flags.bm_control;
497 pr->flags.bm_check = bm_check_flag;
498 pr->flags.bm_control = bm_control_flag;
501 if (pr->flags.bm_check) {
502 if (!pr->flags.bm_control) {
503 if (pr->flags.has_cst != 1) {
504 /* bus mastering control is necessary */
505 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
506 "C3 support requires BM control\n"));
509 /* Here we enter C3 without bus mastering */
510 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
511 "C3 support without BM control\n"));
516 * WBINVD should be set in fadt, for C3 state to be
517 * supported on when bm_check is not required.
519 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
520 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
521 "Cache invalidation should work properly"
522 " for C3 to be enabled on SMP systems\n"));
528 * Otherwise we've met all of our C3 requirements.
529 * Normalize the C3 latency to expidite policy. Enable
530 * checking of bus mastering status (bm_check) so we can
531 * use this in our C3 policy
536 * On older chipsets, BM_RLD needs to be set
537 * in order for Bus Master activity to wake the
538 * system from C3. Newer chipsets handle DMA
539 * during C3 automatically and BM_RLD is a NOP.
540 * In either case, the proper way to
541 * handle BM_RLD is to set it and leave it set.
543 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
548 static int acpi_cst_latency_cmp(const void *a, const void *b)
550 const struct acpi_processor_cx *x = a, *y = b;
552 if (!(x->valid && y->valid))
554 if (x->latency > y->latency)
556 if (x->latency < y->latency)
560 static void acpi_cst_latency_swap(void *a, void *b, int n)
562 struct acpi_processor_cx *x = a, *y = b;
565 if (!(x->valid && y->valid))
568 x->latency = y->latency;
572 static int acpi_processor_power_verify(struct acpi_processor *pr)
575 unsigned int working = 0;
576 unsigned int last_latency = 0;
577 unsigned int last_type = 0;
578 bool buggy_latency = false;
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];
597 acpi_processor_power_verify_c3(pr, cx);
602 if (cx->type >= last_type && cx->latency < last_latency)
603 buggy_latency = true;
604 last_latency = cx->latency;
605 last_type = cx->type;
607 lapic_timer_check_state(i, pr, cx);
608 tsc_check_state(cx->type);
613 pr_notice("FW issue: working around C-state latencies out of order\n");
614 sort(&pr->power.states[1], max_cstate,
615 sizeof(struct acpi_processor_cx),
616 acpi_cst_latency_cmp,
617 acpi_cst_latency_swap);
620 lapic_timer_propagate_broadcast(pr);
625 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
631 /* NOTE: the idle thread may not be running while calling
634 /* Zero initialize all the C-states info. */
635 memset(pr->power.states, 0, sizeof(pr->power.states));
637 result = acpi_processor_get_power_info_cst(pr);
638 if (result == -ENODEV)
639 result = acpi_processor_get_power_info_fadt(pr);
644 acpi_processor_get_power_info_default(pr);
646 pr->power.count = acpi_processor_power_verify(pr);
649 * if one state of type C2 or C3 is available, mark this
650 * CPU as being "idle manageable"
652 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
653 if (pr->power.states[i].valid) {
655 if (pr->power.states[i].type >= ACPI_STATE_C2)
664 * acpi_idle_bm_check - checks if bus master activity was detected
666 static int acpi_idle_bm_check(void)
670 if (bm_check_disable)
673 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
675 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
677 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
678 * the true state of bus mastering activity; forcing us to
679 * manually check the BMIDEA bit of each IDE channel.
681 else if (errata.piix4.bmisx) {
682 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
683 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
690 * acpi_idle_do_entry - enter idle state using the appropriate method
693 * Caller disables interrupt before call and enables interrupt after return.
695 static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
697 if (cx->entry_method == ACPI_CSTATE_FFH) {
698 /* Call into architectural FFH based C-state */
699 acpi_processor_ffh_cstate_enter(cx);
700 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
703 /* IO port based C-state */
705 /* Dummy wait op - must do something useless after P_LVL2 read
706 because chipsets cannot guarantee that STPCLK# signal
707 gets asserted in time to freeze execution properly. */
708 inl(acpi_gbl_FADT.xpm_timer_block.address);
713 * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
714 * @dev: the target CPU
715 * @index: the index of suggested state
717 static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
719 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
721 ACPI_FLUSH_CPU_CACHE();
725 if (cx->entry_method == ACPI_CSTATE_HALT)
727 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
729 /* See comment in acpi_idle_do_entry() */
730 inl(acpi_gbl_FADT.xpm_timer_block.address);
739 static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
741 return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
742 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
745 static int c3_cpu_count;
746 static DEFINE_RAW_SPINLOCK(c3_lock);
749 * acpi_idle_enter_bm - enters C3 with proper BM handling
750 * @pr: Target processor
751 * @cx: Target state context
752 * @timer_bc: Whether or not to change timer mode to broadcast
754 static void acpi_idle_enter_bm(struct acpi_processor *pr,
755 struct acpi_processor_cx *cx, bool timer_bc)
757 acpi_unlazy_tlb(smp_processor_id());
760 * Must be done before busmaster disable as we might need to
764 lapic_timer_state_broadcast(pr, cx, 1);
768 * bm_check implies we need ARB_DIS
769 * bm_control implies whether we can do ARB_DIS
771 * That leaves a case where bm_check is set and bm_control is
772 * not set. In that case we cannot do much, we enter C3
773 * without doing anything.
775 if (pr->flags.bm_control) {
776 raw_spin_lock(&c3_lock);
778 /* Disable bus master arbitration when all CPUs are in C3 */
779 if (c3_cpu_count == num_online_cpus())
780 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
781 raw_spin_unlock(&c3_lock);
784 acpi_idle_do_entry(cx);
786 /* Re-enable bus master arbitration */
787 if (pr->flags.bm_control) {
788 raw_spin_lock(&c3_lock);
789 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
791 raw_spin_unlock(&c3_lock);
795 lapic_timer_state_broadcast(pr, cx, 0);
798 static int acpi_idle_enter(struct cpuidle_device *dev,
799 struct cpuidle_driver *drv, int index)
801 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
802 struct acpi_processor *pr;
804 pr = __this_cpu_read(processors);
808 if (cx->type != ACPI_STATE_C1) {
809 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
810 index = ACPI_IDLE_STATE_START;
811 cx = per_cpu(acpi_cstate[index], dev->cpu);
812 } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
813 if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
814 acpi_idle_enter_bm(pr, cx, true);
816 } else if (drv->safe_state_index >= 0) {
817 index = drv->safe_state_index;
818 cx = per_cpu(acpi_cstate[index], dev->cpu);
826 lapic_timer_state_broadcast(pr, cx, 1);
828 if (cx->type == ACPI_STATE_C3)
829 ACPI_FLUSH_CPU_CACHE();
831 acpi_idle_do_entry(cx);
833 lapic_timer_state_broadcast(pr, cx, 0);
838 static void acpi_idle_enter_s2idle(struct cpuidle_device *dev,
839 struct cpuidle_driver *drv, int index)
841 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
843 if (cx->type == ACPI_STATE_C3) {
844 struct acpi_processor *pr = __this_cpu_read(processors);
849 if (pr->flags.bm_check) {
850 acpi_idle_enter_bm(pr, cx, false);
853 ACPI_FLUSH_CPU_CACHE();
856 acpi_idle_do_entry(cx);
859 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
860 struct cpuidle_device *dev)
862 int i, count = ACPI_IDLE_STATE_START;
863 struct acpi_processor_cx *cx;
868 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
869 cx = &pr->power.states[i];
874 per_cpu(acpi_cstate[count], dev->cpu) = cx;
877 if (count == CPUIDLE_STATE_MAX)
887 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
890 struct acpi_processor_cx *cx;
891 struct cpuidle_state *state;
892 struct cpuidle_driver *drv = &acpi_idle_driver;
897 if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) {
898 cpuidle_poll_state_init(drv);
904 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
905 cx = &pr->power.states[i];
910 state = &drv->states[count];
911 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
912 strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
913 state->exit_latency = cx->latency;
914 state->target_residency = cx->latency * latency_factor;
915 state->enter = acpi_idle_enter;
918 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
919 state->enter_dead = acpi_idle_play_dead;
920 drv->safe_state_index = count;
923 * Halt-induced C1 is not good for ->enter_s2idle, because it
924 * re-enables interrupts on exit. Moreover, C1 is generally not
925 * particularly interesting from the suspend-to-idle angle, so
926 * avoid C1 and the situations in which we may need to fall back
929 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
930 state->enter_s2idle = acpi_idle_enter_s2idle;
933 if (count == CPUIDLE_STATE_MAX)
937 drv->state_count = count;
945 static inline void acpi_processor_cstate_first_run_checks(void)
948 static int first_run;
952 dmi_check_system(processor_power_dmi_table);
953 max_cstate = acpi_processor_cstate_check(max_cstate);
954 if (max_cstate < ACPI_C_STATES_MAX)
955 pr_notice("ACPI: processor limited to max C-state %d\n",
959 if (acpi_gbl_FADT.cst_control && !nocst) {
960 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
961 acpi_gbl_FADT.cst_control, 8);
962 if (ACPI_FAILURE(status))
963 ACPI_EXCEPTION((AE_INFO, status,
964 "Notifying BIOS of _CST ability failed"));
969 static inline int disabled_by_idle_boot_param(void) { return 0; }
970 static inline void acpi_processor_cstate_first_run_checks(void) { }
971 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
976 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
977 struct cpuidle_device *dev)
982 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
987 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
989 struct acpi_lpi_states_array {
991 unsigned int composite_states_size;
992 struct acpi_lpi_state *entries;
993 struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
996 static int obj_get_integer(union acpi_object *obj, u32 *value)
998 if (obj->type != ACPI_TYPE_INTEGER)
1001 *value = obj->integer.value;
1005 static int acpi_processor_evaluate_lpi(acpi_handle handle,
1006 struct acpi_lpi_states_array *info)
1010 int pkg_count, state_idx = 1, loop;
1011 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1012 union acpi_object *lpi_data;
1013 struct acpi_lpi_state *lpi_state;
1015 status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
1016 if (ACPI_FAILURE(status)) {
1017 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
1021 lpi_data = buffer.pointer;
1023 /* There must be at least 4 elements = 3 elements + 1 package */
1024 if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
1025 lpi_data->package.count < 4) {
1026 pr_debug("not enough elements in _LPI\n");
1031 pkg_count = lpi_data->package.elements[2].integer.value;
1033 /* Validate number of power states. */
1034 if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
1035 pr_debug("count given by _LPI is not valid\n");
1040 lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
1046 info->size = pkg_count;
1047 info->entries = lpi_state;
1049 /* LPI States start at index 3 */
1050 for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
1051 union acpi_object *element, *pkg_elem, *obj;
1053 element = &lpi_data->package.elements[loop];
1054 if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
1057 pkg_elem = element->package.elements;
1060 if (obj->type == ACPI_TYPE_BUFFER) {
1061 struct acpi_power_register *reg;
1063 reg = (struct acpi_power_register *)obj->buffer.pointer;
1064 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
1065 reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
1068 lpi_state->address = reg->address;
1069 lpi_state->entry_method =
1070 reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
1071 ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
1072 } else if (obj->type == ACPI_TYPE_INTEGER) {
1073 lpi_state->entry_method = ACPI_CSTATE_INTEGER;
1074 lpi_state->address = obj->integer.value;
1079 /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
1082 if (obj->type == ACPI_TYPE_STRING)
1083 strlcpy(lpi_state->desc, obj->string.pointer,
1086 lpi_state->index = state_idx;
1087 if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
1088 pr_debug("No min. residency found, assuming 10 us\n");
1089 lpi_state->min_residency = 10;
1092 if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
1093 pr_debug("No wakeup residency found, assuming 10 us\n");
1094 lpi_state->wake_latency = 10;
1097 if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
1098 lpi_state->flags = 0;
1100 if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
1101 lpi_state->arch_flags = 0;
1103 if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
1104 lpi_state->res_cnt_freq = 1;
1106 if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
1107 lpi_state->enable_parent_state = 0;
1110 acpi_handle_debug(handle, "Found %d power states\n", state_idx);
1112 kfree(buffer.pointer);
1117 * flat_state_cnt - the number of composite LPI states after the process of flattening
1119 static int flat_state_cnt;
1122 * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
1124 * @local: local LPI state
1125 * @parent: parent LPI state
1126 * @result: composite LPI state
1128 static bool combine_lpi_states(struct acpi_lpi_state *local,
1129 struct acpi_lpi_state *parent,
1130 struct acpi_lpi_state *result)
1132 if (parent->entry_method == ACPI_CSTATE_INTEGER) {
1133 if (!parent->address) /* 0 means autopromotable */
1135 result->address = local->address + parent->address;
1137 result->address = parent->address;
1140 result->min_residency = max(local->min_residency, parent->min_residency);
1141 result->wake_latency = local->wake_latency + parent->wake_latency;
1142 result->enable_parent_state = parent->enable_parent_state;
1143 result->entry_method = local->entry_method;
1145 result->flags = parent->flags;
1146 result->arch_flags = parent->arch_flags;
1147 result->index = parent->index;
1149 strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
1150 strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
1151 strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
1155 #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0)
1157 static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
1158 struct acpi_lpi_state *t)
1160 curr_level->composite_states[curr_level->composite_states_size++] = t;
1163 static int flatten_lpi_states(struct acpi_processor *pr,
1164 struct acpi_lpi_states_array *curr_level,
1165 struct acpi_lpi_states_array *prev_level)
1167 int i, j, state_count = curr_level->size;
1168 struct acpi_lpi_state *p, *t = curr_level->entries;
1170 curr_level->composite_states_size = 0;
1171 for (j = 0; j < state_count; j++, t++) {
1172 struct acpi_lpi_state *flpi;
1174 if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
1177 if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
1178 pr_warn("Limiting number of LPI states to max (%d)\n",
1179 ACPI_PROCESSOR_MAX_POWER);
1180 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1184 flpi = &pr->power.lpi_states[flat_state_cnt];
1186 if (!prev_level) { /* leaf/processor node */
1187 memcpy(flpi, t, sizeof(*t));
1188 stash_composite_state(curr_level, flpi);
1193 for (i = 0; i < prev_level->composite_states_size; i++) {
1194 p = prev_level->composite_states[i];
1195 if (t->index <= p->enable_parent_state &&
1196 combine_lpi_states(p, t, flpi)) {
1197 stash_composite_state(curr_level, flpi);
1204 kfree(curr_level->entries);
1208 static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
1212 acpi_handle handle = pr->handle, pr_ahandle;
1213 struct acpi_device *d = NULL;
1214 struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
1216 if (!osc_pc_lpi_support_confirmed)
1219 if (!acpi_has_method(handle, "_LPI"))
1225 handle = pr->handle;
1226 ret = acpi_processor_evaluate_lpi(handle, prev);
1229 flatten_lpi_states(pr, prev, NULL);
1231 status = acpi_get_parent(handle, &pr_ahandle);
1232 while (ACPI_SUCCESS(status)) {
1233 acpi_bus_get_device(pr_ahandle, &d);
1234 handle = pr_ahandle;
1236 if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
1239 /* can be optional ? */
1240 if (!acpi_has_method(handle, "_LPI"))
1243 ret = acpi_processor_evaluate_lpi(handle, curr);
1247 /* flatten all the LPI states in this level of hierarchy */
1248 flatten_lpi_states(pr, curr, prev);
1250 tmp = prev, prev = curr, curr = tmp;
1252 status = acpi_get_parent(handle, &pr_ahandle);
1255 pr->power.count = flat_state_cnt;
1256 /* reset the index after flattening */
1257 for (i = 0; i < pr->power.count; i++)
1258 pr->power.lpi_states[i].index = i;
1260 /* Tell driver that _LPI is supported. */
1261 pr->flags.has_lpi = 1;
1262 pr->flags.power = 1;
1267 int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
1272 int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
1278 * acpi_idle_lpi_enter - enters an ACPI any LPI state
1279 * @dev: the target CPU
1280 * @drv: cpuidle driver containing cpuidle state info
1281 * @index: index of target state
1283 * Return: 0 for success or negative value for error
1285 static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
1286 struct cpuidle_driver *drv, int index)
1288 struct acpi_processor *pr;
1289 struct acpi_lpi_state *lpi;
1291 pr = __this_cpu_read(processors);
1296 lpi = &pr->power.lpi_states[index];
1297 if (lpi->entry_method == ACPI_CSTATE_FFH)
1298 return acpi_processor_ffh_lpi_enter(lpi);
1303 static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
1306 struct acpi_lpi_state *lpi;
1307 struct cpuidle_state *state;
1308 struct cpuidle_driver *drv = &acpi_idle_driver;
1310 if (!pr->flags.has_lpi)
1313 for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
1314 lpi = &pr->power.lpi_states[i];
1316 state = &drv->states[i];
1317 snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
1318 strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
1319 state->exit_latency = lpi->wake_latency;
1320 state->target_residency = lpi->min_residency;
1321 if (lpi->arch_flags)
1322 state->flags |= CPUIDLE_FLAG_TIMER_STOP;
1323 state->enter = acpi_idle_lpi_enter;
1324 drv->safe_state_index = i;
1327 drv->state_count = i;
1333 * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
1334 * global state data i.e. idle routines
1336 * @pr: the ACPI processor
1338 static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
1341 struct cpuidle_driver *drv = &acpi_idle_driver;
1343 if (!pr->flags.power_setup_done || !pr->flags.power)
1346 drv->safe_state_index = -1;
1347 for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
1348 drv->states[i].name[0] = '\0';
1349 drv->states[i].desc[0] = '\0';
1352 if (pr->flags.has_lpi)
1353 return acpi_processor_setup_lpi_states(pr);
1355 return acpi_processor_setup_cstates(pr);
1359 * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
1360 * device i.e. per-cpu data
1362 * @pr: the ACPI processor
1363 * @dev : the cpuidle device
1365 static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
1366 struct cpuidle_device *dev)
1368 if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
1372 if (pr->flags.has_lpi)
1373 return acpi_processor_ffh_lpi_probe(pr->id);
1375 return acpi_processor_setup_cpuidle_cx(pr, dev);
1378 static int acpi_processor_get_power_info(struct acpi_processor *pr)
1382 ret = acpi_processor_get_lpi_info(pr);
1384 ret = acpi_processor_get_cstate_info(pr);
1389 int acpi_processor_hotplug(struct acpi_processor *pr)
1392 struct cpuidle_device *dev;
1394 if (disabled_by_idle_boot_param())
1397 if (!pr->flags.power_setup_done)
1400 dev = per_cpu(acpi_cpuidle_device, pr->id);
1401 cpuidle_pause_and_lock();
1402 cpuidle_disable_device(dev);
1403 ret = acpi_processor_get_power_info(pr);
1404 if (!ret && pr->flags.power) {
1405 acpi_processor_setup_cpuidle_dev(pr, dev);
1406 ret = cpuidle_enable_device(dev);
1408 cpuidle_resume_and_unlock();
1413 int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
1416 struct acpi_processor *_pr;
1417 struct cpuidle_device *dev;
1419 if (disabled_by_idle_boot_param())
1422 if (!pr->flags.power_setup_done)
1426 * FIXME: Design the ACPI notification to make it once per
1427 * system instead of once per-cpu. This condition is a hack
1428 * to make the code that updates C-States be called once.
1431 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1433 /* Protect against cpu-hotplug */
1435 cpuidle_pause_and_lock();
1437 /* Disable all cpuidle devices */
1438 for_each_online_cpu(cpu) {
1439 _pr = per_cpu(processors, cpu);
1440 if (!_pr || !_pr->flags.power_setup_done)
1442 dev = per_cpu(acpi_cpuidle_device, cpu);
1443 cpuidle_disable_device(dev);
1446 /* Populate Updated C-state information */
1447 acpi_processor_get_power_info(pr);
1448 acpi_processor_setup_cpuidle_states(pr);
1450 /* Enable all cpuidle devices */
1451 for_each_online_cpu(cpu) {
1452 _pr = per_cpu(processors, cpu);
1453 if (!_pr || !_pr->flags.power_setup_done)
1455 acpi_processor_get_power_info(_pr);
1456 if (_pr->flags.power) {
1457 dev = per_cpu(acpi_cpuidle_device, cpu);
1458 acpi_processor_setup_cpuidle_dev(_pr, dev);
1459 cpuidle_enable_device(dev);
1462 cpuidle_resume_and_unlock();
1469 static int acpi_processor_registered;
1471 int acpi_processor_power_init(struct acpi_processor *pr)
1474 struct cpuidle_device *dev;
1476 if (disabled_by_idle_boot_param())
1479 acpi_processor_cstate_first_run_checks();
1481 if (!acpi_processor_get_power_info(pr))
1482 pr->flags.power_setup_done = 1;
1485 * Install the idle handler if processor power management is supported.
1486 * Note that we use previously set idle handler will be used on
1487 * platforms that only support C1.
1489 if (pr->flags.power) {
1490 /* Register acpi_idle_driver if not already registered */
1491 if (!acpi_processor_registered) {
1492 acpi_processor_setup_cpuidle_states(pr);
1493 retval = cpuidle_register_driver(&acpi_idle_driver);
1496 pr_debug("%s registered with cpuidle\n",
1497 acpi_idle_driver.name);
1500 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1503 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1505 acpi_processor_setup_cpuidle_dev(pr, dev);
1507 /* Register per-cpu cpuidle_device. Cpuidle driver
1508 * must already be registered before registering device
1510 retval = cpuidle_register_device(dev);
1512 if (acpi_processor_registered == 0)
1513 cpuidle_unregister_driver(&acpi_idle_driver);
1516 acpi_processor_registered++;
1521 int acpi_processor_power_exit(struct acpi_processor *pr)
1523 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1525 if (disabled_by_idle_boot_param())
1528 if (pr->flags.power) {
1529 cpuidle_unregister_device(dev);
1530 acpi_processor_registered--;
1531 if (acpi_processor_registered == 0)
1532 cpuidle_unregister_driver(&acpi_idle_driver);
1535 pr->flags.power_setup_done = 0;