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:
212 * AMD Fam10h TSC will tick in all
213 * C/P/S0/S1 states when this bit is set.
215 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
220 /* TSC could halt in idle, so notify users */
221 if (state > ACPI_STATE_C1)
222 mark_tsc_unstable("TSC halts in idle");
226 static void tsc_check_state(int state) { return; }
229 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
235 /* if info is obtained from pblk/fadt, type equals state */
236 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
237 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
239 #ifndef CONFIG_HOTPLUG_CPU
241 * Check for P_LVL2_UP flag before entering C2 and above on
244 if ((num_online_cpus() > 1) &&
245 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
249 /* determine C2 and C3 address from pblk */
250 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
251 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
253 /* determine latencies from FADT */
254 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
255 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
258 * FADT specified C2 latency must be less than or equal to
261 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
262 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
263 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
265 pr->power.states[ACPI_STATE_C2].address = 0;
269 * FADT supplied C3 latency must be less than or equal to
272 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
273 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
274 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
276 pr->power.states[ACPI_STATE_C3].address = 0;
279 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
280 "lvl2[0x%08x] lvl3[0x%08x]\n",
281 pr->power.states[ACPI_STATE_C2].address,
282 pr->power.states[ACPI_STATE_C3].address));
287 static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
289 if (!pr->power.states[ACPI_STATE_C1].valid) {
290 /* set the first C-State to C1 */
291 /* all processors need to support C1 */
292 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
293 pr->power.states[ACPI_STATE_C1].valid = 1;
294 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
296 /* the C0 state only exists as a filler in our array */
297 pr->power.states[ACPI_STATE_C0].valid = 1;
301 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
307 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
308 union acpi_object *cst;
315 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
316 if (ACPI_FAILURE(status)) {
317 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
321 cst = buffer.pointer;
323 /* There must be at least 2 elements */
324 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
325 pr_err("not enough elements in _CST\n");
330 count = cst->package.elements[0].integer.value;
332 /* Validate number of power states. */
333 if (count < 1 || count != cst->package.count - 1) {
334 pr_err("count given by _CST is not valid\n");
339 /* Tell driver that at least _CST is supported. */
340 pr->flags.has_cst = 1;
342 for (i = 1; i <= count; i++) {
343 union acpi_object *element;
344 union acpi_object *obj;
345 struct acpi_power_register *reg;
346 struct acpi_processor_cx cx;
348 memset(&cx, 0, sizeof(cx));
350 element = &(cst->package.elements[i]);
351 if (element->type != ACPI_TYPE_PACKAGE)
354 if (element->package.count != 4)
357 obj = &(element->package.elements[0]);
359 if (obj->type != ACPI_TYPE_BUFFER)
362 reg = (struct acpi_power_register *)obj->buffer.pointer;
364 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
365 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
368 /* There should be an easy way to extract an integer... */
369 obj = &(element->package.elements[1]);
370 if (obj->type != ACPI_TYPE_INTEGER)
373 cx.type = obj->integer.value;
375 * Some buggy BIOSes won't list C1 in _CST -
376 * Let acpi_processor_get_power_info_default() handle them later
378 if (i == 1 && cx.type != ACPI_STATE_C1)
381 cx.address = reg->address;
382 cx.index = current_count + 1;
384 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
385 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
386 if (acpi_processor_ffh_cstate_probe
387 (pr->id, &cx, reg) == 0) {
388 cx.entry_method = ACPI_CSTATE_FFH;
389 } else if (cx.type == ACPI_STATE_C1) {
391 * C1 is a special case where FIXED_HARDWARE
392 * can be handled in non-MWAIT way as well.
393 * In that case, save this _CST entry info.
394 * Otherwise, ignore this info and continue.
396 cx.entry_method = ACPI_CSTATE_HALT;
397 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
401 if (cx.type == ACPI_STATE_C1 &&
402 (boot_option_idle_override == IDLE_NOMWAIT)) {
404 * In most cases the C1 space_id obtained from
405 * _CST object is FIXED_HARDWARE access mode.
406 * But when the option of idle=halt is added,
407 * the entry_method type should be changed from
408 * CSTATE_FFH to CSTATE_HALT.
409 * When the option of idle=nomwait is added,
410 * the C1 entry_method type should be
413 cx.entry_method = ACPI_CSTATE_HALT;
414 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
417 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
421 if (cx.type == ACPI_STATE_C1) {
425 obj = &(element->package.elements[2]);
426 if (obj->type != ACPI_TYPE_INTEGER)
429 cx.latency = obj->integer.value;
431 obj = &(element->package.elements[3]);
432 if (obj->type != ACPI_TYPE_INTEGER)
436 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
439 * We support total ACPI_PROCESSOR_MAX_POWER - 1
440 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
442 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
443 pr_warn("Limiting number of power states to max (%d)\n",
444 ACPI_PROCESSOR_MAX_POWER);
445 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
450 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
453 /* Validate number of power states discovered */
454 if (current_count < 2)
458 kfree(buffer.pointer);
463 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
464 struct acpi_processor_cx *cx)
466 static int bm_check_flag = -1;
467 static int bm_control_flag = -1;
474 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
475 * DMA transfers are used by any ISA device to avoid livelock.
476 * Note that we could disable Type-F DMA (as recommended by
477 * the erratum), but this is known to disrupt certain ISA
478 * devices thus we take the conservative approach.
480 else if (errata.piix4.fdma) {
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
482 "C3 not supported on PIIX4 with Type-F DMA\n"));
486 /* All the logic here assumes flags.bm_check is same across all CPUs */
487 if (bm_check_flag == -1) {
488 /* Determine whether bm_check is needed based on CPU */
489 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
490 bm_check_flag = pr->flags.bm_check;
491 bm_control_flag = pr->flags.bm_control;
493 pr->flags.bm_check = bm_check_flag;
494 pr->flags.bm_control = bm_control_flag;
497 if (pr->flags.bm_check) {
498 if (!pr->flags.bm_control) {
499 if (pr->flags.has_cst != 1) {
500 /* bus mastering control is necessary */
501 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
502 "C3 support requires BM control\n"));
505 /* Here we enter C3 without bus mastering */
506 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
507 "C3 support without BM control\n"));
512 * WBINVD should be set in fadt, for C3 state to be
513 * supported on when bm_check is not required.
515 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
516 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
517 "Cache invalidation should work properly"
518 " for C3 to be enabled on SMP systems\n"));
524 * Otherwise we've met all of our C3 requirements.
525 * Normalize the C3 latency to expidite policy. Enable
526 * checking of bus mastering status (bm_check) so we can
527 * use this in our C3 policy
532 * On older chipsets, BM_RLD needs to be set
533 * in order for Bus Master activity to wake the
534 * system from C3. Newer chipsets handle DMA
535 * during C3 automatically and BM_RLD is a NOP.
536 * In either case, the proper way to
537 * handle BM_RLD is to set it and leave it set.
539 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
544 static int acpi_cst_latency_cmp(const void *a, const void *b)
546 const struct acpi_processor_cx *x = a, *y = b;
548 if (!(x->valid && y->valid))
550 if (x->latency > y->latency)
552 if (x->latency < y->latency)
556 static void acpi_cst_latency_swap(void *a, void *b, int n)
558 struct acpi_processor_cx *x = a, *y = b;
561 if (!(x->valid && y->valid))
564 x->latency = y->latency;
568 static int acpi_processor_power_verify(struct acpi_processor *pr)
571 unsigned int working = 0;
572 unsigned int last_latency = 0;
573 unsigned int last_type = 0;
574 bool buggy_latency = false;
576 pr->power.timer_broadcast_on_state = INT_MAX;
578 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
579 struct acpi_processor_cx *cx = &pr->power.states[i];
593 acpi_processor_power_verify_c3(pr, cx);
598 if (cx->type >= last_type && cx->latency < last_latency)
599 buggy_latency = true;
600 last_latency = cx->latency;
601 last_type = cx->type;
603 lapic_timer_check_state(i, pr, cx);
604 tsc_check_state(cx->type);
609 pr_notice("FW issue: working around C-state latencies out of order\n");
610 sort(&pr->power.states[1], max_cstate,
611 sizeof(struct acpi_processor_cx),
612 acpi_cst_latency_cmp,
613 acpi_cst_latency_swap);
616 lapic_timer_propagate_broadcast(pr);
621 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
627 /* NOTE: the idle thread may not be running while calling
630 /* Zero initialize all the C-states info. */
631 memset(pr->power.states, 0, sizeof(pr->power.states));
633 result = acpi_processor_get_power_info_cst(pr);
634 if (result == -ENODEV)
635 result = acpi_processor_get_power_info_fadt(pr);
640 acpi_processor_get_power_info_default(pr);
642 pr->power.count = acpi_processor_power_verify(pr);
645 * if one state of type C2 or C3 is available, mark this
646 * CPU as being "idle manageable"
648 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
649 if (pr->power.states[i].valid) {
651 if (pr->power.states[i].type >= ACPI_STATE_C2)
660 * acpi_idle_bm_check - checks if bus master activity was detected
662 static int acpi_idle_bm_check(void)
666 if (bm_check_disable)
669 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
671 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
673 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
674 * the true state of bus mastering activity; forcing us to
675 * manually check the BMIDEA bit of each IDE channel.
677 else if (errata.piix4.bmisx) {
678 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
679 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
686 * acpi_idle_do_entry - enter idle state using the appropriate method
689 * Caller disables interrupt before call and enables interrupt after return.
691 static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
693 if (cx->entry_method == ACPI_CSTATE_FFH) {
694 /* Call into architectural FFH based C-state */
695 acpi_processor_ffh_cstate_enter(cx);
696 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
699 /* IO port based C-state */
701 /* Dummy wait op - must do something useless after P_LVL2 read
702 because chipsets cannot guarantee that STPCLK# signal
703 gets asserted in time to freeze execution properly. */
704 inl(acpi_gbl_FADT.xpm_timer_block.address);
709 * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
710 * @dev: the target CPU
711 * @index: the index of suggested state
713 static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
715 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
717 ACPI_FLUSH_CPU_CACHE();
721 if (cx->entry_method == ACPI_CSTATE_HALT)
723 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
725 /* See comment in acpi_idle_do_entry() */
726 inl(acpi_gbl_FADT.xpm_timer_block.address);
735 static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
737 return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
738 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
741 static int c3_cpu_count;
742 static DEFINE_RAW_SPINLOCK(c3_lock);
745 * acpi_idle_enter_bm - enters C3 with proper BM handling
746 * @pr: Target processor
747 * @cx: Target state context
748 * @timer_bc: Whether or not to change timer mode to broadcast
750 static void acpi_idle_enter_bm(struct acpi_processor *pr,
751 struct acpi_processor_cx *cx, bool timer_bc)
753 acpi_unlazy_tlb(smp_processor_id());
756 * Must be done before busmaster disable as we might need to
760 lapic_timer_state_broadcast(pr, cx, 1);
764 * bm_check implies we need ARB_DIS
765 * bm_control implies whether we can do ARB_DIS
767 * That leaves a case where bm_check is set and bm_control is
768 * not set. In that case we cannot do much, we enter C3
769 * without doing anything.
771 if (pr->flags.bm_control) {
772 raw_spin_lock(&c3_lock);
774 /* Disable bus master arbitration when all CPUs are in C3 */
775 if (c3_cpu_count == num_online_cpus())
776 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
777 raw_spin_unlock(&c3_lock);
780 acpi_idle_do_entry(cx);
782 /* Re-enable bus master arbitration */
783 if (pr->flags.bm_control) {
784 raw_spin_lock(&c3_lock);
785 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
787 raw_spin_unlock(&c3_lock);
791 lapic_timer_state_broadcast(pr, cx, 0);
794 static int acpi_idle_enter(struct cpuidle_device *dev,
795 struct cpuidle_driver *drv, int index)
797 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
798 struct acpi_processor *pr;
800 pr = __this_cpu_read(processors);
804 if (cx->type != ACPI_STATE_C1) {
805 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
806 index = ACPI_IDLE_STATE_START;
807 cx = per_cpu(acpi_cstate[index], dev->cpu);
808 } else if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) {
809 if (cx->bm_sts_skip || !acpi_idle_bm_check()) {
810 acpi_idle_enter_bm(pr, cx, true);
812 } else if (drv->safe_state_index >= 0) {
813 index = drv->safe_state_index;
814 cx = per_cpu(acpi_cstate[index], dev->cpu);
822 lapic_timer_state_broadcast(pr, cx, 1);
824 if (cx->type == ACPI_STATE_C3)
825 ACPI_FLUSH_CPU_CACHE();
827 acpi_idle_do_entry(cx);
829 lapic_timer_state_broadcast(pr, cx, 0);
834 static void acpi_idle_enter_s2idle(struct cpuidle_device *dev,
835 struct cpuidle_driver *drv, int index)
837 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
839 if (cx->type == ACPI_STATE_C3) {
840 struct acpi_processor *pr = __this_cpu_read(processors);
845 if (pr->flags.bm_check) {
846 acpi_idle_enter_bm(pr, cx, false);
849 ACPI_FLUSH_CPU_CACHE();
852 acpi_idle_do_entry(cx);
855 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
856 struct cpuidle_device *dev)
858 int i, count = ACPI_IDLE_STATE_START;
859 struct acpi_processor_cx *cx;
864 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
865 cx = &pr->power.states[i];
870 per_cpu(acpi_cstate[count], dev->cpu) = cx;
873 if (count == CPUIDLE_STATE_MAX)
883 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
886 struct acpi_processor_cx *cx;
887 struct cpuidle_state *state;
888 struct cpuidle_driver *drv = &acpi_idle_driver;
893 if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) {
894 cpuidle_poll_state_init(drv);
900 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
901 cx = &pr->power.states[i];
906 state = &drv->states[count];
907 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
908 strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
909 state->exit_latency = cx->latency;
910 state->target_residency = cx->latency * latency_factor;
911 state->enter = acpi_idle_enter;
914 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
915 state->enter_dead = acpi_idle_play_dead;
916 drv->safe_state_index = count;
919 * Halt-induced C1 is not good for ->enter_s2idle, because it
920 * re-enables interrupts on exit. Moreover, C1 is generally not
921 * particularly interesting from the suspend-to-idle angle, so
922 * avoid C1 and the situations in which we may need to fall back
925 if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr))
926 state->enter_s2idle = acpi_idle_enter_s2idle;
929 if (count == CPUIDLE_STATE_MAX)
933 drv->state_count = count;
941 static inline void acpi_processor_cstate_first_run_checks(void)
944 static int first_run;
948 dmi_check_system(processor_power_dmi_table);
949 max_cstate = acpi_processor_cstate_check(max_cstate);
950 if (max_cstate < ACPI_C_STATES_MAX)
951 pr_notice("ACPI: processor limited to max C-state %d\n",
955 if (acpi_gbl_FADT.cst_control && !nocst) {
956 status = acpi_os_write_port(acpi_gbl_FADT.smi_command,
957 acpi_gbl_FADT.cst_control, 8);
958 if (ACPI_FAILURE(status))
959 ACPI_EXCEPTION((AE_INFO, status,
960 "Notifying BIOS of _CST ability failed"));
965 static inline int disabled_by_idle_boot_param(void) { return 0; }
966 static inline void acpi_processor_cstate_first_run_checks(void) { }
967 static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
972 static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
973 struct cpuidle_device *dev)
978 static int acpi_processor_setup_cstates(struct acpi_processor *pr)
983 #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
985 struct acpi_lpi_states_array {
987 unsigned int composite_states_size;
988 struct acpi_lpi_state *entries;
989 struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER];
992 static int obj_get_integer(union acpi_object *obj, u32 *value)
994 if (obj->type != ACPI_TYPE_INTEGER)
997 *value = obj->integer.value;
1001 static int acpi_processor_evaluate_lpi(acpi_handle handle,
1002 struct acpi_lpi_states_array *info)
1006 int pkg_count, state_idx = 1, loop;
1007 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
1008 union acpi_object *lpi_data;
1009 struct acpi_lpi_state *lpi_state;
1011 status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer);
1012 if (ACPI_FAILURE(status)) {
1013 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n"));
1017 lpi_data = buffer.pointer;
1019 /* There must be at least 4 elements = 3 elements + 1 package */
1020 if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE ||
1021 lpi_data->package.count < 4) {
1022 pr_debug("not enough elements in _LPI\n");
1027 pkg_count = lpi_data->package.elements[2].integer.value;
1029 /* Validate number of power states. */
1030 if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) {
1031 pr_debug("count given by _LPI is not valid\n");
1036 lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL);
1042 info->size = pkg_count;
1043 info->entries = lpi_state;
1045 /* LPI States start at index 3 */
1046 for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) {
1047 union acpi_object *element, *pkg_elem, *obj;
1049 element = &lpi_data->package.elements[loop];
1050 if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7)
1053 pkg_elem = element->package.elements;
1056 if (obj->type == ACPI_TYPE_BUFFER) {
1057 struct acpi_power_register *reg;
1059 reg = (struct acpi_power_register *)obj->buffer.pointer;
1060 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
1061 reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE)
1064 lpi_state->address = reg->address;
1065 lpi_state->entry_method =
1066 reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ?
1067 ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO;
1068 } else if (obj->type == ACPI_TYPE_INTEGER) {
1069 lpi_state->entry_method = ACPI_CSTATE_INTEGER;
1070 lpi_state->address = obj->integer.value;
1075 /* elements[7,8] skipped for now i.e. Residency/Usage counter*/
1078 if (obj->type == ACPI_TYPE_STRING)
1079 strlcpy(lpi_state->desc, obj->string.pointer,
1082 lpi_state->index = state_idx;
1083 if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) {
1084 pr_debug("No min. residency found, assuming 10 us\n");
1085 lpi_state->min_residency = 10;
1088 if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) {
1089 pr_debug("No wakeup residency found, assuming 10 us\n");
1090 lpi_state->wake_latency = 10;
1093 if (obj_get_integer(pkg_elem + 2, &lpi_state->flags))
1094 lpi_state->flags = 0;
1096 if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags))
1097 lpi_state->arch_flags = 0;
1099 if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq))
1100 lpi_state->res_cnt_freq = 1;
1102 if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state))
1103 lpi_state->enable_parent_state = 0;
1106 acpi_handle_debug(handle, "Found %d power states\n", state_idx);
1108 kfree(buffer.pointer);
1113 * flat_state_cnt - the number of composite LPI states after the process of flattening
1115 static int flat_state_cnt;
1118 * combine_lpi_states - combine local and parent LPI states to form a composite LPI state
1120 * @local: local LPI state
1121 * @parent: parent LPI state
1122 * @result: composite LPI state
1124 static bool combine_lpi_states(struct acpi_lpi_state *local,
1125 struct acpi_lpi_state *parent,
1126 struct acpi_lpi_state *result)
1128 if (parent->entry_method == ACPI_CSTATE_INTEGER) {
1129 if (!parent->address) /* 0 means autopromotable */
1131 result->address = local->address + parent->address;
1133 result->address = parent->address;
1136 result->min_residency = max(local->min_residency, parent->min_residency);
1137 result->wake_latency = local->wake_latency + parent->wake_latency;
1138 result->enable_parent_state = parent->enable_parent_state;
1139 result->entry_method = local->entry_method;
1141 result->flags = parent->flags;
1142 result->arch_flags = parent->arch_flags;
1143 result->index = parent->index;
1145 strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN);
1146 strlcat(result->desc, "+", ACPI_CX_DESC_LEN);
1147 strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN);
1151 #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0)
1153 static void stash_composite_state(struct acpi_lpi_states_array *curr_level,
1154 struct acpi_lpi_state *t)
1156 curr_level->composite_states[curr_level->composite_states_size++] = t;
1159 static int flatten_lpi_states(struct acpi_processor *pr,
1160 struct acpi_lpi_states_array *curr_level,
1161 struct acpi_lpi_states_array *prev_level)
1163 int i, j, state_count = curr_level->size;
1164 struct acpi_lpi_state *p, *t = curr_level->entries;
1166 curr_level->composite_states_size = 0;
1167 for (j = 0; j < state_count; j++, t++) {
1168 struct acpi_lpi_state *flpi;
1170 if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED))
1173 if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) {
1174 pr_warn("Limiting number of LPI states to max (%d)\n",
1175 ACPI_PROCESSOR_MAX_POWER);
1176 pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1180 flpi = &pr->power.lpi_states[flat_state_cnt];
1182 if (!prev_level) { /* leaf/processor node */
1183 memcpy(flpi, t, sizeof(*t));
1184 stash_composite_state(curr_level, flpi);
1189 for (i = 0; i < prev_level->composite_states_size; i++) {
1190 p = prev_level->composite_states[i];
1191 if (t->index <= p->enable_parent_state &&
1192 combine_lpi_states(p, t, flpi)) {
1193 stash_composite_state(curr_level, flpi);
1200 kfree(curr_level->entries);
1204 static int acpi_processor_get_lpi_info(struct acpi_processor *pr)
1208 acpi_handle handle = pr->handle, pr_ahandle;
1209 struct acpi_device *d = NULL;
1210 struct acpi_lpi_states_array info[2], *tmp, *prev, *curr;
1212 if (!osc_pc_lpi_support_confirmed)
1215 if (!acpi_has_method(handle, "_LPI"))
1221 handle = pr->handle;
1222 ret = acpi_processor_evaluate_lpi(handle, prev);
1225 flatten_lpi_states(pr, prev, NULL);
1227 status = acpi_get_parent(handle, &pr_ahandle);
1228 while (ACPI_SUCCESS(status)) {
1229 acpi_bus_get_device(pr_ahandle, &d);
1230 handle = pr_ahandle;
1232 if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID))
1235 /* can be optional ? */
1236 if (!acpi_has_method(handle, "_LPI"))
1239 ret = acpi_processor_evaluate_lpi(handle, curr);
1243 /* flatten all the LPI states in this level of hierarchy */
1244 flatten_lpi_states(pr, curr, prev);
1246 tmp = prev, prev = curr, curr = tmp;
1248 status = acpi_get_parent(handle, &pr_ahandle);
1251 pr->power.count = flat_state_cnt;
1252 /* reset the index after flattening */
1253 for (i = 0; i < pr->power.count; i++)
1254 pr->power.lpi_states[i].index = i;
1256 /* Tell driver that _LPI is supported. */
1257 pr->flags.has_lpi = 1;
1258 pr->flags.power = 1;
1263 int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
1268 int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi)
1274 * acpi_idle_lpi_enter - enters an ACPI any LPI state
1275 * @dev: the target CPU
1276 * @drv: cpuidle driver containing cpuidle state info
1277 * @index: index of target state
1279 * Return: 0 for success or negative value for error
1281 static int acpi_idle_lpi_enter(struct cpuidle_device *dev,
1282 struct cpuidle_driver *drv, int index)
1284 struct acpi_processor *pr;
1285 struct acpi_lpi_state *lpi;
1287 pr = __this_cpu_read(processors);
1292 lpi = &pr->power.lpi_states[index];
1293 if (lpi->entry_method == ACPI_CSTATE_FFH)
1294 return acpi_processor_ffh_lpi_enter(lpi);
1299 static int acpi_processor_setup_lpi_states(struct acpi_processor *pr)
1302 struct acpi_lpi_state *lpi;
1303 struct cpuidle_state *state;
1304 struct cpuidle_driver *drv = &acpi_idle_driver;
1306 if (!pr->flags.has_lpi)
1309 for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) {
1310 lpi = &pr->power.lpi_states[i];
1312 state = &drv->states[i];
1313 snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i);
1314 strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN);
1315 state->exit_latency = lpi->wake_latency;
1316 state->target_residency = lpi->min_residency;
1317 if (lpi->arch_flags)
1318 state->flags |= CPUIDLE_FLAG_TIMER_STOP;
1319 state->enter = acpi_idle_lpi_enter;
1320 drv->safe_state_index = i;
1323 drv->state_count = i;
1329 * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle
1330 * global state data i.e. idle routines
1332 * @pr: the ACPI processor
1334 static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
1337 struct cpuidle_driver *drv = &acpi_idle_driver;
1339 if (!pr->flags.power_setup_done || !pr->flags.power)
1342 drv->safe_state_index = -1;
1343 for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
1344 drv->states[i].name[0] = '\0';
1345 drv->states[i].desc[0] = '\0';
1348 if (pr->flags.has_lpi)
1349 return acpi_processor_setup_lpi_states(pr);
1351 return acpi_processor_setup_cstates(pr);
1355 * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE
1356 * device i.e. per-cpu data
1358 * @pr: the ACPI processor
1359 * @dev : the cpuidle device
1361 static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr,
1362 struct cpuidle_device *dev)
1364 if (!pr->flags.power_setup_done || !pr->flags.power || !dev)
1368 if (pr->flags.has_lpi)
1369 return acpi_processor_ffh_lpi_probe(pr->id);
1371 return acpi_processor_setup_cpuidle_cx(pr, dev);
1374 static int acpi_processor_get_power_info(struct acpi_processor *pr)
1378 ret = acpi_processor_get_lpi_info(pr);
1380 ret = acpi_processor_get_cstate_info(pr);
1385 int acpi_processor_hotplug(struct acpi_processor *pr)
1388 struct cpuidle_device *dev;
1390 if (disabled_by_idle_boot_param())
1393 if (!pr->flags.power_setup_done)
1396 dev = per_cpu(acpi_cpuidle_device, pr->id);
1397 cpuidle_pause_and_lock();
1398 cpuidle_disable_device(dev);
1399 ret = acpi_processor_get_power_info(pr);
1400 if (!ret && pr->flags.power) {
1401 acpi_processor_setup_cpuidle_dev(pr, dev);
1402 ret = cpuidle_enable_device(dev);
1404 cpuidle_resume_and_unlock();
1409 int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
1412 struct acpi_processor *_pr;
1413 struct cpuidle_device *dev;
1415 if (disabled_by_idle_boot_param())
1418 if (!pr->flags.power_setup_done)
1422 * FIXME: Design the ACPI notification to make it once per
1423 * system instead of once per-cpu. This condition is a hack
1424 * to make the code that updates C-States be called once.
1427 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
1429 /* Protect against cpu-hotplug */
1431 cpuidle_pause_and_lock();
1433 /* Disable all cpuidle devices */
1434 for_each_online_cpu(cpu) {
1435 _pr = per_cpu(processors, cpu);
1436 if (!_pr || !_pr->flags.power_setup_done)
1438 dev = per_cpu(acpi_cpuidle_device, cpu);
1439 cpuidle_disable_device(dev);
1442 /* Populate Updated C-state information */
1443 acpi_processor_get_power_info(pr);
1444 acpi_processor_setup_cpuidle_states(pr);
1446 /* Enable all cpuidle devices */
1447 for_each_online_cpu(cpu) {
1448 _pr = per_cpu(processors, cpu);
1449 if (!_pr || !_pr->flags.power_setup_done)
1451 acpi_processor_get_power_info(_pr);
1452 if (_pr->flags.power) {
1453 dev = per_cpu(acpi_cpuidle_device, cpu);
1454 acpi_processor_setup_cpuidle_dev(_pr, dev);
1455 cpuidle_enable_device(dev);
1458 cpuidle_resume_and_unlock();
1465 static int acpi_processor_registered;
1467 int acpi_processor_power_init(struct acpi_processor *pr)
1470 struct cpuidle_device *dev;
1472 if (disabled_by_idle_boot_param())
1475 acpi_processor_cstate_first_run_checks();
1477 if (!acpi_processor_get_power_info(pr))
1478 pr->flags.power_setup_done = 1;
1481 * Install the idle handler if processor power management is supported.
1482 * Note that we use previously set idle handler will be used on
1483 * platforms that only support C1.
1485 if (pr->flags.power) {
1486 /* Register acpi_idle_driver if not already registered */
1487 if (!acpi_processor_registered) {
1488 acpi_processor_setup_cpuidle_states(pr);
1489 retval = cpuidle_register_driver(&acpi_idle_driver);
1492 pr_debug("%s registered with cpuidle\n",
1493 acpi_idle_driver.name);
1496 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1499 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1501 acpi_processor_setup_cpuidle_dev(pr, dev);
1503 /* Register per-cpu cpuidle_device. Cpuidle driver
1504 * must already be registered before registering device
1506 retval = cpuidle_register_device(dev);
1508 if (acpi_processor_registered == 0)
1509 cpuidle_unregister_driver(&acpi_idle_driver);
1512 acpi_processor_registered++;
1517 int acpi_processor_power_exit(struct acpi_processor *pr)
1519 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1521 if (disabled_by_idle_boot_param())
1524 if (pr->flags.power) {
1525 cpuidle_unregister_device(dev);
1526 acpi_processor_registered--;
1527 if (acpi_processor_registered == 0)
1528 cpuidle_unregister_driver(&acpi_idle_driver);
1531 pr->flags.power_setup_done = 0;