Thomas Gleixner | c942fdd | 2019-05-27 08:55:06 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
| 3 | * processor_idle - idle state submodule to the ACPI processor driver |
| 4 | * |
| 5 | * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> |
| 6 | * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> |
Dominik Brodowski | c5ab81c | 2006-06-24 19:37:00 -0400 | [diff] [blame] | 7 | * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com> |
| 9 | * - Added processor hotplug support |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 10 | * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com> |
| 11 | * - Added support for C3 on SMP |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | */ |
Sudeep Holla | b6ec26f | 2015-12-02 14:10:44 +0000 | [diff] [blame] | 13 | #define pr_fmt(fmt) "ACPI: " fmt |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #include <linux/module.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 16 | #include <linux/acpi.h> |
| 17 | #include <linux/dmi.h> |
Daniel Lezcano | e2668fb | 2013-02-04 22:44:41 +0000 | [diff] [blame] | 18 | #include <linux/sched.h> /* need_resched() */ |
Thomas Gleixner | ee41eeb | 2015-04-03 02:02:00 +0200 | [diff] [blame] | 19 | #include <linux/tick.h> |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 20 | #include <linux/cpuidle.h> |
Chris Metcalf | 6727ad9 | 2016-10-07 17:02:55 -0700 | [diff] [blame] | 21 | #include <linux/cpu.h> |
Lv Zheng | 8b48463 | 2013-12-03 08:49:16 +0800 | [diff] [blame] | 22 | #include <acpi/processor.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
Thomas Gleixner | 3434933 | 2007-02-16 01:27:54 -0800 | [diff] [blame] | 24 | /* |
| 25 | * Include the apic definitions for x86 to have the APIC timer related defines |
| 26 | * available also for UP (on SMP it gets magically included via linux/smp.h). |
| 27 | * asm/acpi.h is not an option, as it would require more include magic. Also |
| 28 | * creating an empty asm-ia64/apic.h would just trade pest vs. cholera. |
| 29 | */ |
| 30 | #ifdef CONFIG_X86 |
| 31 | #include <asm/apic.h> |
| 32 | #endif |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | #define _COMPONENT ACPI_PROCESSOR_COMPONENT |
Len Brown | f52fd66 | 2007-02-12 22:42:12 -0500 | [diff] [blame] | 35 | ACPI_MODULE_NAME("processor_idle"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 37 | #define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0) |
| 38 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 39 | static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER; |
| 40 | module_param(max_cstate, uint, 0000); |
Andreas Mohr | b683505 | 2006-04-27 05:25:00 -0400 | [diff] [blame] | 41 | static unsigned int nocst __read_mostly; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 42 | module_param(nocst, uint, 0000); |
Len Brown | d3e7e99 | 2010-07-22 17:23:10 -0400 | [diff] [blame] | 43 | static int bm_check_disable __read_mostly; |
| 44 | module_param(bm_check_disable, uint, 0000); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | |
Len Brown | 25de571 | 2007-12-14 00:24:15 -0500 | [diff] [blame] | 46 | static unsigned int latency_factor __read_mostly = 2; |
Len Brown | 4963f62 | 2007-12-13 23:50:45 -0500 | [diff] [blame] | 47 | module_param(latency_factor, uint, 0644); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 49 | static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device); |
| 50 | |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 51 | struct cpuidle_driver acpi_idle_driver = { |
| 52 | .name = "acpi_idle", |
| 53 | .owner = THIS_MODULE, |
| 54 | }; |
| 55 | |
| 56 | #ifdef CONFIG_ACPI_PROCESSOR_CSTATE |
Peter Zijlstra | 2552821 | 2016-03-15 14:52:49 -0700 | [diff] [blame] | 57 | static |
| 58 | DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate); |
Daniel Lezcano | ac3ebaf | 2013-02-04 22:44:43 +0000 | [diff] [blame] | 59 | |
Thomas Renninger | d189604 | 2010-11-03 17:06:14 +0100 | [diff] [blame] | 60 | static int disabled_by_idle_boot_param(void) |
| 61 | { |
| 62 | return boot_option_idle_override == IDLE_POLL || |
Thomas Renninger | d189604 | 2010-11-03 17:06:14 +0100 | [diff] [blame] | 63 | boot_option_idle_override == IDLE_HALT; |
| 64 | } |
| 65 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | /* |
| 67 | * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3. |
| 68 | * For now disable this. Probably a bug somewhere else. |
| 69 | * |
| 70 | * To skip this limit, boot/load with a large max_cstate limit. |
| 71 | */ |
Jeff Garzik | 1855256 | 2007-10-03 15:15:40 -0400 | [diff] [blame] | 72 | static int set_max_cstate(const struct dmi_system_id *id) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | { |
| 74 | if (max_cstate > ACPI_PROCESSOR_MAX_POWER) |
| 75 | return 0; |
| 76 | |
Sudeep Holla | b6ec26f | 2015-12-02 14:10:44 +0000 | [diff] [blame] | 77 | pr_notice("%s detected - limiting to C%ld max_cstate." |
| 78 | " Override with \"processor.max_cstate=%d\"\n", id->ident, |
| 79 | (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | |
Len Brown | 3d35600 | 2005-08-03 00:22:52 -0400 | [diff] [blame] | 81 | max_cstate = (long)id->driver_data; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
Mathias Krause | b034668 | 2015-06-13 14:26:57 +0200 | [diff] [blame] | 86 | static const struct dmi_system_id processor_power_dmi_table[] = { |
Thomas Rosner | 876c184 | 2006-01-06 01:31:00 -0500 | [diff] [blame] | 87 | { set_max_cstate, "Clevo 5600D", { |
| 88 | DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"), |
| 89 | DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")}, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 90 | (void *)2}, |
Arjan van de Ven | 370d5cd | 2010-01-27 15:25:39 -0800 | [diff] [blame] | 91 | { set_max_cstate, "Pavilion zv5000", { |
| 92 | DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"), |
| 93 | DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")}, |
| 94 | (void *)1}, |
| 95 | { set_max_cstate, "Asus L8400B", { |
| 96 | DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."), |
| 97 | DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")}, |
| 98 | (void *)1}, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | {}, |
| 100 | }; |
| 101 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 102 | |
venkatesh.pallipadi@intel.com | 2e90665 | 2008-01-31 17:35:03 -0800 | [diff] [blame] | 103 | /* |
| 104 | * Callers should disable interrupts before the call and enable |
| 105 | * interrupts after return. |
| 106 | */ |
Chris Metcalf | 6727ad9 | 2016-10-07 17:02:55 -0700 | [diff] [blame] | 107 | static void __cpuidle acpi_safe_halt(void) |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 108 | { |
Peter Zijlstra | ea81174 | 2013-09-11 12:43:13 +0200 | [diff] [blame] | 109 | if (!tif_need_resched()) { |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 110 | safe_halt(); |
Venki Pallipadi | 71e93d1 | 2008-03-13 17:18:19 -0700 | [diff] [blame] | 111 | local_irq_disable(); |
| 112 | } |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 113 | } |
| 114 | |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 115 | #ifdef ARCH_APICTIMER_STOPS_ON_C3 |
| 116 | |
| 117 | /* |
| 118 | * Some BIOS implementations switch to C3 in the published C2 state. |
Linus Torvalds | 296d93c | 2007-03-23 08:03:47 -0700 | [diff] [blame] | 119 | * This seems to be a common problem on AMD boxen, but other vendors |
| 120 | * are affected too. We pick the most conservative approach: we assume |
| 121 | * that the local APIC stops in both C2 and C3. |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 122 | */ |
Len Brown | 7e275cc | 2009-05-15 02:08:44 -0400 | [diff] [blame] | 123 | static void lapic_timer_check_state(int state, struct acpi_processor *pr, |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 124 | struct acpi_processor_cx *cx) |
| 125 | { |
| 126 | struct acpi_processor_power *pwr = &pr->power; |
Thomas Gleixner | e585bef | 2007-03-23 16:08:01 +0100 | [diff] [blame] | 127 | u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2; |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 128 | |
Venkatesh Pallipadi | db954b5 | 2009-04-06 18:51:29 -0700 | [diff] [blame] | 129 | if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT)) |
| 130 | return; |
| 131 | |
Borislav Petkov | 07c94a3 | 2016-12-09 19:29:11 +0100 | [diff] [blame] | 132 | if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E)) |
Shaohua Li | 87ad57ba | 2009-05-19 16:09:42 +0800 | [diff] [blame] | 133 | type = ACPI_STATE_C1; |
| 134 | |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 135 | /* |
| 136 | * Check, if one of the previous states already marked the lapic |
| 137 | * unstable |
| 138 | */ |
| 139 | if (pwr->timer_broadcast_on_state < state) |
| 140 | return; |
| 141 | |
Thomas Gleixner | e585bef | 2007-03-23 16:08:01 +0100 | [diff] [blame] | 142 | if (cx->type >= type) |
Linus Torvalds | 296d93c | 2007-03-23 08:03:47 -0700 | [diff] [blame] | 143 | pr->power.timer_broadcast_on_state = state; |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Hidetoshi Seto | 918aae4 | 2009-12-14 17:10:06 +0900 | [diff] [blame] | 146 | static void __lapic_timer_propagate_broadcast(void *arg) |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 147 | { |
Suresh Siddha | f833bab | 2009-08-17 14:34:59 -0700 | [diff] [blame] | 148 | struct acpi_processor *pr = (struct acpi_processor *) arg; |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 149 | |
Thomas Gleixner | ee41eeb | 2015-04-03 02:02:00 +0200 | [diff] [blame] | 150 | if (pr->power.timer_broadcast_on_state < INT_MAX) |
| 151 | tick_broadcast_enable(); |
| 152 | else |
| 153 | tick_broadcast_disable(); |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 154 | } |
| 155 | |
Hidetoshi Seto | 918aae4 | 2009-12-14 17:10:06 +0900 | [diff] [blame] | 156 | static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) |
| 157 | { |
| 158 | smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast, |
| 159 | (void *)pr, 1); |
| 160 | } |
| 161 | |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 162 | /* Power(C) State timer broadcast control */ |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 163 | static bool lapic_timer_needs_broadcast(struct acpi_processor *pr, |
| 164 | struct acpi_processor_cx *cx) |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 165 | { |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 166 | return cx - pr->power.states >= pr->power.timer_broadcast_on_state; |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | #else |
| 170 | |
Len Brown | 7e275cc | 2009-05-15 02:08:44 -0400 | [diff] [blame] | 171 | static void lapic_timer_check_state(int state, struct acpi_processor *pr, |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 172 | struct acpi_processor_cx *cstate) { } |
Len Brown | 7e275cc | 2009-05-15 02:08:44 -0400 | [diff] [blame] | 173 | static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { } |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 174 | |
| 175 | static bool lapic_timer_needs_broadcast(struct acpi_processor *pr, |
| 176 | struct acpi_processor_cx *cx) |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 177 | { |
Rafael J. Wysocki | 9559212 | 2020-09-23 13:50:12 +0200 | [diff] [blame] | 178 | return false; |
Thomas Gleixner | e9e2cdb | 2007-02-16 01:28:04 -0800 | [diff] [blame] | 179 | } |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 180 | |
| 181 | #endif |
| 182 | |
John Stultz | 592913e | 2010-07-13 17:56:20 -0700 | [diff] [blame] | 183 | #if defined(CONFIG_X86) |
Len Brown | 520daf7 | 2009-05-14 17:27:38 -0400 | [diff] [blame] | 184 | static void tsc_check_state(int state) |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 185 | { |
| 186 | switch (boot_cpu_data.x86_vendor) { |
Pu Wen | 7377ed4 | 2018-09-23 17:37:05 +0800 | [diff] [blame] | 187 | case X86_VENDOR_HYGON: |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 188 | case X86_VENDOR_AMD: |
Venki Pallipadi | 40fb171 | 2008-11-17 16:11:37 -0800 | [diff] [blame] | 189 | case X86_VENDOR_INTEL: |
davidwang | fe6daab | 2018-01-22 18:14:17 +0800 | [diff] [blame] | 190 | case X86_VENDOR_CENTAUR: |
Tony W Wang-oc | 773b2f3 | 2019-06-18 08:37:14 +0000 | [diff] [blame] | 191 | case X86_VENDOR_ZHAOXIN: |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 192 | /* |
| 193 | * AMD Fam10h TSC will tick in all |
| 194 | * C/P/S0/S1 states when this bit is set. |
| 195 | */ |
Venki Pallipadi | 40fb171 | 2008-11-17 16:11:37 -0800 | [diff] [blame] | 196 | if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC)) |
Len Brown | 520daf7 | 2009-05-14 17:27:38 -0400 | [diff] [blame] | 197 | return; |
Gustavo A. R. Silva | 57d2dd4 | 2020-07-07 15:09:37 -0500 | [diff] [blame] | 198 | fallthrough; |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 199 | default: |
Len Brown | 520daf7 | 2009-05-14 17:27:38 -0400 | [diff] [blame] | 200 | /* TSC could halt in idle, so notify users */ |
| 201 | if (state > ACPI_STATE_C1) |
| 202 | mark_tsc_unstable("TSC halts in idle"); |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 203 | } |
| 204 | } |
Len Brown | 520daf7 | 2009-05-14 17:27:38 -0400 | [diff] [blame] | 205 | #else |
| 206 | static void tsc_check_state(int state) { return; } |
Andi Kleen | ddb25f9 | 2008-01-30 13:32:41 +0100 | [diff] [blame] | 207 | #endif |
| 208 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 211 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | if (!pr->pblk) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 213 | return -ENODEV; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | /* if info is obtained from pblk/fadt, type equals state */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2; |
| 217 | pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3; |
| 218 | |
Venkatesh Pallipadi | 4c03355 | 2005-09-15 12:20:00 -0400 | [diff] [blame] | 219 | #ifndef CONFIG_HOTPLUG_CPU |
| 220 | /* |
| 221 | * Check for P_LVL2_UP flag before entering C2 and above on |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 222 | * an SMP system. |
Venkatesh Pallipadi | 4c03355 | 2005-09-15 12:20:00 -0400 | [diff] [blame] | 223 | */ |
Alexey Starikovskiy | ad71860a | 2007-02-02 19:48:19 +0300 | [diff] [blame] | 224 | if ((num_online_cpus() > 1) && |
Alexey Starikovskiy | cee324b | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 225 | !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 226 | return -ENODEV; |
Venkatesh Pallipadi | 4c03355 | 2005-09-15 12:20:00 -0400 | [diff] [blame] | 227 | #endif |
| 228 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | /* determine C2 and C3 address from pblk */ |
| 230 | pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4; |
| 231 | pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5; |
| 232 | |
| 233 | /* determine latencies from FADT */ |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 234 | pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency; |
| 235 | pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 236 | |
Len Brown | 5d76b6f | 2010-01-19 22:41:14 -0500 | [diff] [blame] | 237 | /* |
| 238 | * FADT specified C2 latency must be less than or equal to |
| 239 | * 100 microseconds. |
| 240 | */ |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 241 | if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) { |
Len Brown | 5d76b6f | 2010-01-19 22:41:14 -0500 | [diff] [blame] | 242 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 243 | "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency)); |
Len Brown | 5d76b6f | 2010-01-19 22:41:14 -0500 | [diff] [blame] | 244 | /* invalidate C2 */ |
| 245 | pr->power.states[ACPI_STATE_C2].address = 0; |
| 246 | } |
| 247 | |
Len Brown | a6d72c1 | 2010-01-19 23:10:04 -0500 | [diff] [blame] | 248 | /* |
| 249 | * FADT supplied C3 latency must be less than or equal to |
| 250 | * 1000 microseconds. |
| 251 | */ |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 252 | if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) { |
Len Brown | a6d72c1 | 2010-01-19 23:10:04 -0500 | [diff] [blame] | 253 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 254 | "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency)); |
Len Brown | a6d72c1 | 2010-01-19 23:10:04 -0500 | [diff] [blame] | 255 | /* invalidate C3 */ |
| 256 | pr->power.states[ACPI_STATE_C3].address = 0; |
| 257 | } |
| 258 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 260 | "lvl2[0x%08x] lvl3[0x%08x]\n", |
| 261 | pr->power.states[ACPI_STATE_C2].address, |
| 262 | pr->power.states[ACPI_STATE_C3].address)); |
| 263 | |
Yazen Ghannam | 34a62cd | 2019-02-18 01:33:49 +0000 | [diff] [blame] | 264 | snprintf(pr->power.states[ACPI_STATE_C2].desc, |
| 265 | ACPI_CX_DESC_LEN, "ACPI P_LVL2 IOPORT 0x%x", |
| 266 | pr->power.states[ACPI_STATE_C2].address); |
| 267 | snprintf(pr->power.states[ACPI_STATE_C3].desc, |
| 268 | ACPI_CX_DESC_LEN, "ACPI P_LVL3 IOPORT 0x%x", |
| 269 | pr->power.states[ACPI_STATE_C3].address); |
| 270 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 271 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 272 | } |
| 273 | |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 274 | static int acpi_processor_get_power_info_default(struct acpi_processor *pr) |
Venkatesh Pallipadi | acf05f4 | 2005-03-31 23:23:15 -0500 | [diff] [blame] | 275 | { |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 276 | if (!pr->power.states[ACPI_STATE_C1].valid) { |
| 277 | /* set the first C-State to C1 */ |
| 278 | /* all processors need to support C1 */ |
| 279 | pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1; |
| 280 | pr->power.states[ACPI_STATE_C1].valid = 1; |
Venkatesh Pallipadi | 0fda6b4 | 2008-04-09 21:31:46 -0400 | [diff] [blame] | 281 | pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT; |
Yazen Ghannam | 248e884 | 2018-01-29 14:22:49 -0600 | [diff] [blame] | 282 | |
| 283 | snprintf(pr->power.states[ACPI_STATE_C1].desc, |
| 284 | ACPI_CX_DESC_LEN, "ACPI HLT"); |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 285 | } |
| 286 | /* the C0 state only exists as a filler in our array */ |
Venkatesh Pallipadi | acf05f4 | 2005-03-31 23:23:15 -0500 | [diff] [blame] | 287 | pr->power.states[ACPI_STATE_C0].valid = 1; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 288 | return 0; |
Venkatesh Pallipadi | acf05f4 | 2005-03-31 23:23:15 -0500 | [diff] [blame] | 289 | } |
| 290 | |
Rafael J. Wysocki | 987c785 | 2019-12-13 09:55:24 +0100 | [diff] [blame] | 291 | static int acpi_processor_get_power_info_cst(struct acpi_processor *pr) |
| 292 | { |
| 293 | int ret; |
| 294 | |
| 295 | if (nocst) |
| 296 | return -ENODEV; |
| 297 | |
| 298 | ret = acpi_processor_evaluate_cst(pr->handle, pr->id, &pr->power); |
| 299 | if (ret) |
| 300 | return ret; |
| 301 | |
Zhang Rui | 496121c | 2020-04-22 15:26:07 +0800 | [diff] [blame] | 302 | if (!pr->power.count) |
Rafael J. Wysocki | 987c785 | 2019-12-13 09:55:24 +0100 | [diff] [blame] | 303 | return -EFAULT; |
| 304 | |
| 305 | pr->flags.has_cst = 1; |
| 306 | return 0; |
| 307 | } |
| 308 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 309 | static void acpi_processor_power_verify_c3(struct acpi_processor *pr, |
| 310 | struct acpi_processor_cx *cx) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 311 | { |
Pallipadi, Venkatesh | ee1ca48 | 2009-05-21 17:09:10 -0700 | [diff] [blame] | 312 | static int bm_check_flag = -1; |
| 313 | static int bm_control_flag = -1; |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 314 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | |
| 316 | if (!cx->address) |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 317 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 318 | |
| 319 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 320 | * PIIX4 Erratum #18: We don't support C3 when Type-F (fast) |
| 321 | * DMA transfers are used by any ISA device to avoid livelock. |
| 322 | * Note that we could disable Type-F DMA (as recommended by |
| 323 | * the erratum), but this is known to disrupt certain ISA |
| 324 | * devices thus we take the conservative approach. |
| 325 | */ |
| 326 | else if (errata.piix4.fdma) { |
| 327 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 328 | "C3 not supported on PIIX4 with Type-F DMA\n")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 329 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 332 | /* All the logic here assumes flags.bm_check is same across all CPUs */ |
Pallipadi, Venkatesh | ee1ca48 | 2009-05-21 17:09:10 -0700 | [diff] [blame] | 333 | if (bm_check_flag == -1) { |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 334 | /* Determine whether bm_check is needed based on CPU */ |
| 335 | acpi_processor_power_init_bm_check(&(pr->flags), pr->id); |
| 336 | bm_check_flag = pr->flags.bm_check; |
Pallipadi, Venkatesh | ee1ca48 | 2009-05-21 17:09:10 -0700 | [diff] [blame] | 337 | bm_control_flag = pr->flags.bm_control; |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 338 | } else { |
| 339 | pr->flags.bm_check = bm_check_flag; |
Pallipadi, Venkatesh | ee1ca48 | 2009-05-21 17:09:10 -0700 | [diff] [blame] | 340 | pr->flags.bm_control = bm_control_flag; |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | if (pr->flags.bm_check) { |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 344 | if (!pr->flags.bm_control) { |
Venki Pallipadi | ed3110e | 2007-07-31 12:04:31 -0700 | [diff] [blame] | 345 | if (pr->flags.has_cst != 1) { |
| 346 | /* bus mastering control is necessary */ |
| 347 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 348 | "C3 support requires BM control\n")); |
| 349 | return; |
| 350 | } else { |
| 351 | /* Here we enter C3 without bus mastering */ |
| 352 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
| 353 | "C3 support without BM control\n")); |
| 354 | } |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 355 | } |
| 356 | } else { |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 357 | /* |
| 358 | * WBINVD should be set in fadt, for C3 state to be |
| 359 | * supported on when bm_check is not required. |
| 360 | */ |
Alexey Starikovskiy | cee324b | 2007-02-02 19:48:22 +0300 | [diff] [blame] | 361 | if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) { |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 362 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 363 | "Cache invalidation should work properly" |
| 364 | " for C3 to be enabled on SMP systems\n")); |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 365 | return; |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 366 | } |
Venkatesh Pallipadi | 02df8b9 | 2005-04-15 15:07:10 -0400 | [diff] [blame] | 367 | } |
| 368 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 369 | /* |
| 370 | * Otherwise we've met all of our C3 requirements. |
| 371 | * Normalize the C3 latency to expidite policy. Enable |
| 372 | * checking of bus mastering status (bm_check) so we can |
| 373 | * use this in our C3 policy |
| 374 | */ |
| 375 | cx->valid = 1; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 376 | |
Len Brown | 31878dd | 2009-01-28 18:28:09 -0500 | [diff] [blame] | 377 | /* |
| 378 | * On older chipsets, BM_RLD needs to be set |
| 379 | * in order for Bus Master activity to wake the |
| 380 | * system from C3. Newer chipsets handle DMA |
| 381 | * during C3 automatically and BM_RLD is a NOP. |
| 382 | * In either case, the proper way to |
| 383 | * handle BM_RLD is to set it and leave it set. |
| 384 | */ |
Bob Moore | 50ffba1 | 2009-02-23 15:02:07 +0800 | [diff] [blame] | 385 | acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 386 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 387 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 388 | } |
| 389 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 390 | static int acpi_processor_power_verify(struct acpi_processor *pr) |
| 391 | { |
| 392 | unsigned int i; |
| 393 | unsigned int working = 0; |
Venkatesh Pallipadi | 6eb0a0f | 2006-01-11 22:44:21 +0100 | [diff] [blame] | 394 | |
Thomas Gleixner | 169a0ab | 2007-02-16 01:27:55 -0800 | [diff] [blame] | 395 | pr->power.timer_broadcast_on_state = INT_MAX; |
Venkatesh Pallipadi | 6eb0a0f | 2006-01-11 22:44:21 +0100 | [diff] [blame] | 396 | |
Len Brown | a0bf284 | 2009-05-15 01:29:31 -0400 | [diff] [blame] | 397 | for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 398 | struct acpi_processor_cx *cx = &pr->power.states[i]; |
| 399 | |
| 400 | switch (cx->type) { |
| 401 | case ACPI_STATE_C1: |
| 402 | cx->valid = 1; |
| 403 | break; |
| 404 | |
| 405 | case ACPI_STATE_C2: |
Len Brown | d22edd2 | 2010-01-19 23:29:09 -0500 | [diff] [blame] | 406 | if (!cx->address) |
| 407 | break; |
Al Stone | cad1525 | 2013-12-04 12:59:11 -0700 | [diff] [blame] | 408 | cx->valid = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 409 | break; |
| 410 | |
| 411 | case ACPI_STATE_C3: |
| 412 | acpi_processor_power_verify_c3(pr, cx); |
| 413 | break; |
| 414 | } |
Len Brown | 7e275cc | 2009-05-15 02:08:44 -0400 | [diff] [blame] | 415 | if (!cx->valid) |
| 416 | continue; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 417 | |
Len Brown | 7e275cc | 2009-05-15 02:08:44 -0400 | [diff] [blame] | 418 | lapic_timer_check_state(i, pr, cx); |
| 419 | tsc_check_state(cx->type); |
| 420 | working++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | } |
| 422 | |
Hidetoshi Seto | 918aae4 | 2009-12-14 17:10:06 +0900 | [diff] [blame] | 423 | lapic_timer_propagate_broadcast(pr); |
Andi Kleen | bd66334 | 2006-03-25 16:31:07 +0100 | [diff] [blame] | 424 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 425 | return (working); |
| 426 | } |
| 427 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 428 | static int acpi_processor_get_cstate_info(struct acpi_processor *pr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 429 | { |
| 430 | unsigned int i; |
| 431 | int result; |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | |
| 434 | /* NOTE: the idle thread may not be running while calling |
| 435 | * this function */ |
| 436 | |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 437 | /* Zero initialize all the C-states info. */ |
| 438 | memset(pr->power.states, 0, sizeof(pr->power.states)); |
| 439 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 440 | result = acpi_processor_get_power_info_cst(pr); |
Venkatesh Pallipadi | 6d93c64 | 2005-09-15 12:19:00 -0400 | [diff] [blame] | 441 | if (result == -ENODEV) |
Darrick J. Wong | c5a114f | 2006-10-19 23:28:28 -0700 | [diff] [blame] | 442 | result = acpi_processor_get_power_info_fadt(pr); |
Venkatesh Pallipadi | 6d93c64 | 2005-09-15 12:19:00 -0400 | [diff] [blame] | 443 | |
Venkatesh Pallipadi | 991528d | 2006-09-25 16:28:13 -0700 | [diff] [blame] | 444 | if (result) |
| 445 | return result; |
| 446 | |
| 447 | acpi_processor_get_power_info_default(pr); |
| 448 | |
Janosch Machowinski | cf82478 | 2005-08-20 08:02:00 -0400 | [diff] [blame] | 449 | pr->power.count = acpi_processor_power_verify(pr); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 450 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 451 | /* |
| 452 | * if one state of type C2 or C3 is available, mark this |
| 453 | * CPU as being "idle manageable" |
| 454 | */ |
| 455 | for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) { |
Venkatesh Pallipadi | acf05f4 | 2005-03-31 23:23:15 -0500 | [diff] [blame] | 456 | if (pr->power.states[i].valid) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | pr->power.count = i; |
Zhang Rui | 496121c | 2020-04-22 15:26:07 +0800 | [diff] [blame] | 458 | pr->flags.power = 1; |
Venkatesh Pallipadi | acf05f4 | 2005-03-31 23:23:15 -0500 | [diff] [blame] | 459 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 460 | } |
| 461 | |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 462 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | } |
| 464 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 465 | /** |
| 466 | * acpi_idle_bm_check - checks if bus master activity was detected |
| 467 | */ |
| 468 | static int acpi_idle_bm_check(void) |
| 469 | { |
| 470 | u32 bm_status = 0; |
| 471 | |
Len Brown | d3e7e99 | 2010-07-22 17:23:10 -0400 | [diff] [blame] | 472 | if (bm_check_disable) |
| 473 | return 0; |
| 474 | |
Bob Moore | 50ffba1 | 2009-02-23 15:02:07 +0800 | [diff] [blame] | 475 | acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 476 | if (bm_status) |
Bob Moore | 50ffba1 | 2009-02-23 15:02:07 +0800 | [diff] [blame] | 477 | acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 478 | /* |
| 479 | * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect |
| 480 | * the true state of bus mastering activity; forcing us to |
| 481 | * manually check the BMIDEA bit of each IDE channel. |
| 482 | */ |
| 483 | else if (errata.piix4.bmisx) { |
| 484 | if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01) |
| 485 | || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01)) |
| 486 | bm_status = 1; |
| 487 | } |
| 488 | return bm_status; |
| 489 | } |
| 490 | |
Yin Fengwei | fa583f7 | 2019-10-24 15:04:20 +0800 | [diff] [blame] | 491 | static void wait_for_freeze(void) |
| 492 | { |
| 493 | #ifdef CONFIG_X86 |
| 494 | /* No delay is needed if we are in guest */ |
| 495 | if (boot_cpu_has(X86_FEATURE_HYPERVISOR)) |
| 496 | return; |
| 497 | #endif |
| 498 | /* Dummy wait op - must do something useless after P_LVL2 read |
| 499 | because chipsets cannot guarantee that STPCLK# signal |
| 500 | gets asserted in time to freeze execution properly. */ |
| 501 | inl(acpi_gbl_FADT.xpm_timer_block.address); |
| 502 | } |
| 503 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 504 | /** |
Rafael J. Wysocki | b00783f | 2015-02-02 23:55:08 +0100 | [diff] [blame] | 505 | * acpi_idle_do_entry - enter idle state using the appropriate method |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 506 | * @cx: cstate data |
venkatesh.pallipadi@intel.com | bc71bec | 2008-01-31 17:35:04 -0800 | [diff] [blame] | 507 | * |
| 508 | * Caller disables interrupt before call and enables interrupt after return. |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 509 | */ |
Chris Metcalf | 6727ad9 | 2016-10-07 17:02:55 -0700 | [diff] [blame] | 510 | static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 511 | { |
venkatesh.pallipadi@intel.com | bc71bec | 2008-01-31 17:35:04 -0800 | [diff] [blame] | 512 | if (cx->entry_method == ACPI_CSTATE_FFH) { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 513 | /* Call into architectural FFH based C-state */ |
| 514 | acpi_processor_ffh_cstate_enter(cx); |
venkatesh.pallipadi@intel.com | bc71bec | 2008-01-31 17:35:04 -0800 | [diff] [blame] | 515 | } else if (cx->entry_method == ACPI_CSTATE_HALT) { |
| 516 | acpi_safe_halt(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 517 | } else { |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 518 | /* IO port based C-state */ |
| 519 | inb(cx->address); |
Yin Fengwei | fa583f7 | 2019-10-24 15:04:20 +0800 | [diff] [blame] | 520 | wait_for_freeze(); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 521 | } |
| 522 | } |
| 523 | |
| 524 | /** |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 525 | * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining) |
| 526 | * @dev: the target CPU |
| 527 | * @index: the index of suggested state |
| 528 | */ |
| 529 | static int acpi_idle_play_dead(struct cpuidle_device *dev, int index) |
| 530 | { |
Alex Shi | 6240a10 | 2013-04-02 01:56:54 +0200 | [diff] [blame] | 531 | struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 532 | |
| 533 | ACPI_FLUSH_CPU_CACHE(); |
| 534 | |
| 535 | while (1) { |
| 536 | |
| 537 | if (cx->entry_method == ACPI_CSTATE_HALT) |
Luck, Tony | 54f7007 | 2012-04-03 09:37:28 -0700 | [diff] [blame] | 538 | safe_halt(); |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 539 | else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) { |
| 540 | inb(cx->address); |
Yin Fengwei | fa583f7 | 2019-10-24 15:04:20 +0800 | [diff] [blame] | 541 | wait_for_freeze(); |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 542 | } else |
| 543 | return -ENODEV; |
| 544 | } |
| 545 | |
| 546 | /* Never reached */ |
| 547 | return 0; |
| 548 | } |
| 549 | |
Rafael J. Wysocki | adcb262 | 2015-02-02 23:55:42 +0100 | [diff] [blame] | 550 | static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr) |
| 551 | { |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 552 | return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst && |
| 553 | !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED); |
Rafael J. Wysocki | adcb262 | 2015-02-02 23:55:42 +0100 | [diff] [blame] | 554 | } |
| 555 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 556 | static int c3_cpu_count; |
Thomas Gleixner | e12f65f | 2009-07-25 18:14:37 +0200 | [diff] [blame] | 557 | static DEFINE_RAW_SPINLOCK(c3_lock); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 558 | |
| 559 | /** |
| 560 | * acpi_idle_enter_bm - enters C3 with proper BM handling |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 561 | * @drv: cpuidle driver |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 562 | * @pr: Target processor |
| 563 | * @cx: Target state context |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 564 | * @index: index of target state |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 565 | */ |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 566 | static int acpi_idle_enter_bm(struct cpuidle_driver *drv, |
| 567 | struct acpi_processor *pr, |
| 568 | struct acpi_processor_cx *cx, |
| 569 | int index) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 570 | { |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 571 | static struct acpi_processor_cx safe_cx = { |
| 572 | .entry_method = ACPI_CSTATE_HALT, |
| 573 | }; |
| 574 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 575 | /* |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 576 | * disable bus master |
| 577 | * bm_check implies we need ARB_DIS |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 578 | * bm_control implies whether we can do ARB_DIS |
| 579 | * |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 580 | * That leaves a case where bm_check is set and bm_control is not set. |
| 581 | * In that case we cannot do much, we enter C3 without doing anything. |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 582 | */ |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 583 | bool dis_bm = pr->flags.bm_control; |
| 584 | |
| 585 | /* If we can skip BM, demote to a safe state. */ |
| 586 | if (!cx->bm_sts_skip && acpi_idle_bm_check()) { |
| 587 | dis_bm = false; |
| 588 | index = drv->safe_state_index; |
| 589 | if (index >= 0) { |
| 590 | cx = this_cpu_read(acpi_cstate[index]); |
| 591 | } else { |
| 592 | cx = &safe_cx; |
| 593 | index = -EBUSY; |
| 594 | } |
| 595 | } |
| 596 | |
| 597 | if (dis_bm) { |
Thomas Gleixner | e12f65f | 2009-07-25 18:14:37 +0200 | [diff] [blame] | 598 | raw_spin_lock(&c3_lock); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 599 | c3_cpu_count++; |
| 600 | /* Disable bus master arbitration when all CPUs are in C3 */ |
| 601 | if (c3_cpu_count == num_online_cpus()) |
Bob Moore | 50ffba1 | 2009-02-23 15:02:07 +0800 | [diff] [blame] | 602 | acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1); |
Thomas Gleixner | e12f65f | 2009-07-25 18:14:37 +0200 | [diff] [blame] | 603 | raw_spin_unlock(&c3_lock); |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 604 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 605 | |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 606 | rcu_idle_enter(); |
| 607 | |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 608 | acpi_idle_do_entry(cx); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 609 | |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 610 | rcu_idle_exit(); |
| 611 | |
Venkatesh Pallipadi | ddc081a | 2007-11-19 21:43:22 -0500 | [diff] [blame] | 612 | /* Re-enable bus master arbitration */ |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 613 | if (dis_bm) { |
Thomas Gleixner | e12f65f | 2009-07-25 18:14:37 +0200 | [diff] [blame] | 614 | raw_spin_lock(&c3_lock); |
Bob Moore | 50ffba1 | 2009-02-23 15:02:07 +0800 | [diff] [blame] | 615 | acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 616 | c3_cpu_count--; |
Thomas Gleixner | e12f65f | 2009-07-25 18:14:37 +0200 | [diff] [blame] | 617 | raw_spin_unlock(&c3_lock); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 618 | } |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 619 | |
| 620 | return index; |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 621 | } |
| 622 | |
| 623 | static int acpi_idle_enter(struct cpuidle_device *dev, |
| 624 | struct cpuidle_driver *drv, int index) |
| 625 | { |
| 626 | struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); |
| 627 | struct acpi_processor *pr; |
| 628 | |
| 629 | pr = __this_cpu_read(processors); |
| 630 | if (unlikely(!pr)) |
| 631 | return -EINVAL; |
| 632 | |
| 633 | if (cx->type != ACPI_STATE_C1) { |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 634 | if (cx->type == ACPI_STATE_C3 && pr->flags.bm_check) |
| 635 | return acpi_idle_enter_bm(drv, pr, cx, index); |
| 636 | |
| 637 | /* C2 to C1 demotion. */ |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 638 | if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) { |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 639 | index = ACPI_IDLE_STATE_START; |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 640 | cx = per_cpu(acpi_cstate[index], dev->cpu); |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 641 | } |
| 642 | } |
| 643 | |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 644 | if (cx->type == ACPI_STATE_C3) |
| 645 | ACPI_FLUSH_CPU_CACHE(); |
| 646 | |
| 647 | acpi_idle_do_entry(cx); |
| 648 | |
Deepthi Dharwar | e978aa7 | 2011-10-28 16:20:09 +0530 | [diff] [blame] | 649 | return index; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 650 | } |
| 651 | |
Neal Liu | efe9711 | 2020-07-27 11:25:46 +0800 | [diff] [blame] | 652 | static int acpi_idle_enter_s2idle(struct cpuidle_device *dev, |
| 653 | struct cpuidle_driver *drv, int index) |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 654 | { |
| 655 | struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu); |
| 656 | |
| 657 | if (cx->type == ACPI_STATE_C3) { |
| 658 | struct acpi_processor *pr = __this_cpu_read(processors); |
| 659 | |
| 660 | if (unlikely(!pr)) |
Neal Liu | 0a39894 | 2020-07-31 11:39:04 +0800 | [diff] [blame] | 661 | return 0; |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 662 | |
| 663 | if (pr->flags.bm_check) { |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 664 | u8 bm_sts_skip = cx->bm_sts_skip; |
| 665 | |
| 666 | /* Don't check BM_STS, do an unconditional ARB_DIS for S2IDLE */ |
| 667 | cx->bm_sts_skip = 1; |
| 668 | acpi_idle_enter_bm(drv, pr, cx, index); |
| 669 | cx->bm_sts_skip = bm_sts_skip; |
| 670 | |
Neal Liu | 0a39894 | 2020-07-31 11:39:04 +0800 | [diff] [blame] | 671 | return 0; |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 672 | } else { |
| 673 | ACPI_FLUSH_CPU_CACHE(); |
| 674 | } |
| 675 | } |
| 676 | acpi_idle_do_entry(cx); |
Neal Liu | efe9711 | 2020-07-27 11:25:46 +0800 | [diff] [blame] | 677 | |
| 678 | return 0; |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Daniel Lezcano | 6ef0f08 | 2013-02-04 22:44:42 +0000 | [diff] [blame] | 681 | static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr, |
| 682 | struct cpuidle_device *dev) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 683 | { |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 684 | int i, count = ACPI_IDLE_STATE_START; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 685 | struct acpi_processor_cx *cx; |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 686 | struct cpuidle_state *state; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 687 | |
Len Brown | 615dfd9 | 2009-04-23 23:21:29 -0400 | [diff] [blame] | 688 | if (max_cstate == 0) |
| 689 | max_cstate = 1; |
| 690 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 691 | for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { |
Peter Zijlstra | a889a23 | 2020-09-15 12:31:59 +0200 | [diff] [blame] | 692 | state = &acpi_idle_driver.states[count]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 693 | cx = &pr->power.states[i]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 694 | |
| 695 | if (!cx->valid) |
| 696 | continue; |
| 697 | |
Alex Shi | 6240a10 | 2013-04-02 01:56:54 +0200 | [diff] [blame] | 698 | per_cpu(acpi_cstate[count], dev->cpu) = cx; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 699 | |
Peter Zijlstra | a889a23 | 2020-09-15 12:31:59 +0200 | [diff] [blame] | 700 | if (lapic_timer_needs_broadcast(pr, cx)) |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 701 | state->flags |= CPUIDLE_FLAG_TIMER_STOP; |
Peter Zijlstra | a889a23 | 2020-09-15 12:31:59 +0200 | [diff] [blame] | 702 | |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 703 | if (cx->type == ACPI_STATE_C3) { |
Peter Zijlstra | a889a23 | 2020-09-15 12:31:59 +0200 | [diff] [blame] | 704 | state->flags |= CPUIDLE_FLAG_TLB_FLUSHED; |
Peter Zijlstra | 1fecfdb | 2020-09-15 12:32:01 +0200 | [diff] [blame] | 705 | if (pr->flags.bm_check) |
| 706 | state->flags |= CPUIDLE_FLAG_RCU_IDLE; |
| 707 | } |
Peter Zijlstra | aa6b43d | 2020-09-15 12:31:58 +0200 | [diff] [blame] | 708 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 709 | count++; |
| 710 | if (count == CPUIDLE_STATE_MAX) |
| 711 | break; |
| 712 | } |
| 713 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 714 | if (!count) |
| 715 | return -EINVAL; |
| 716 | |
| 717 | return 0; |
| 718 | } |
| 719 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 720 | static int acpi_processor_setup_cstates(struct acpi_processor *pr) |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 721 | { |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 722 | int i, count; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 723 | struct acpi_processor_cx *cx; |
| 724 | struct cpuidle_state *state; |
| 725 | struct cpuidle_driver *drv = &acpi_idle_driver; |
| 726 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 727 | if (max_cstate == 0) |
| 728 | max_cstate = 1; |
| 729 | |
Rafael J. Wysocki | 1b39e3f | 2017-08-29 03:14:37 +0200 | [diff] [blame] | 730 | if (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX)) { |
| 731 | cpuidle_poll_state_init(drv); |
| 732 | count = 1; |
| 733 | } else { |
| 734 | count = 0; |
| 735 | } |
| 736 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 737 | for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) { |
| 738 | cx = &pr->power.states[i]; |
| 739 | |
| 740 | if (!cx->valid) |
| 741 | continue; |
| 742 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 743 | state = &drv->states[count]; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 744 | snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i); |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 745 | strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 746 | state->exit_latency = cx->latency; |
Len Brown | 4963f62 | 2007-12-13 23:50:45 -0500 | [diff] [blame] | 747 | state->target_residency = cx->latency * latency_factor; |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 748 | state->enter = acpi_idle_enter; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 749 | |
| 750 | state->flags = 0; |
Rafael J. Wysocki | 6491bc0 | 2015-02-03 21:55:11 +0100 | [diff] [blame] | 751 | if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) { |
Boris Ostrovsky | 1a022e3 | 2012-03-13 19:55:09 +0100 | [diff] [blame] | 752 | state->enter_dead = acpi_idle_play_dead; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 753 | drv->safe_state_index = count; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 754 | } |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 755 | /* |
Rafael J. Wysocki | 28ba086 | 2017-08-10 00:14:45 +0200 | [diff] [blame] | 756 | * Halt-induced C1 is not good for ->enter_s2idle, because it |
Rafael J. Wysocki | 5f50818 | 2015-02-11 05:04:57 +0100 | [diff] [blame] | 757 | * re-enables interrupts on exit. Moreover, C1 is generally not |
| 758 | * particularly interesting from the suspend-to-idle angle, so |
| 759 | * avoid C1 and the situations in which we may need to fall back |
| 760 | * to it altogether. |
| 761 | */ |
| 762 | if (cx->type != ACPI_STATE_C1 && !acpi_idle_fallback_to_c1(pr)) |
Rafael J. Wysocki | 28ba086 | 2017-08-10 00:14:45 +0200 | [diff] [blame] | 763 | state->enter_s2idle = acpi_idle_enter_s2idle; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 764 | |
| 765 | count++; |
venkatesh.pallipadi@intel.com | 9a0b841 | 2008-01-31 17:35:06 -0800 | [diff] [blame] | 766 | if (count == CPUIDLE_STATE_MAX) |
| 767 | break; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 768 | } |
| 769 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 770 | drv->state_count = count; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 771 | |
| 772 | if (!count) |
| 773 | return -EINVAL; |
| 774 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 775 | return 0; |
| 776 | } |
| 777 | |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 778 | static inline void acpi_processor_cstate_first_run_checks(void) |
| 779 | { |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 780 | static int first_run; |
| 781 | |
| 782 | if (first_run) |
| 783 | return; |
| 784 | dmi_check_system(processor_power_dmi_table); |
| 785 | max_cstate = acpi_processor_cstate_check(max_cstate); |
| 786 | if (max_cstate < ACPI_C_STATES_MAX) |
| 787 | pr_notice("ACPI: processor limited to max C-state %d\n", |
| 788 | max_cstate); |
| 789 | first_run++; |
| 790 | |
Rafael J. Wysocki | bc94638 | 2019-12-13 09:55:14 +0100 | [diff] [blame] | 791 | if (nocst) |
| 792 | return; |
| 793 | |
| 794 | acpi_processor_claim_cst_control(); |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 795 | } |
| 796 | #else |
| 797 | |
| 798 | static inline int disabled_by_idle_boot_param(void) { return 0; } |
| 799 | static inline void acpi_processor_cstate_first_run_checks(void) { } |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 800 | static int acpi_processor_get_cstate_info(struct acpi_processor *pr) |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 801 | { |
| 802 | return -ENODEV; |
| 803 | } |
| 804 | |
| 805 | static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr, |
| 806 | struct cpuidle_device *dev) |
| 807 | { |
| 808 | return -EINVAL; |
| 809 | } |
| 810 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 811 | static int acpi_processor_setup_cstates(struct acpi_processor *pr) |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 812 | { |
| 813 | return -EINVAL; |
| 814 | } |
| 815 | |
| 816 | #endif /* CONFIG_ACPI_PROCESSOR_CSTATE */ |
| 817 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 818 | struct acpi_lpi_states_array { |
| 819 | unsigned int size; |
| 820 | unsigned int composite_states_size; |
| 821 | struct acpi_lpi_state *entries; |
| 822 | struct acpi_lpi_state *composite_states[ACPI_PROCESSOR_MAX_POWER]; |
| 823 | }; |
| 824 | |
| 825 | static int obj_get_integer(union acpi_object *obj, u32 *value) |
| 826 | { |
| 827 | if (obj->type != ACPI_TYPE_INTEGER) |
| 828 | return -EINVAL; |
| 829 | |
| 830 | *value = obj->integer.value; |
| 831 | return 0; |
| 832 | } |
| 833 | |
| 834 | static int acpi_processor_evaluate_lpi(acpi_handle handle, |
| 835 | struct acpi_lpi_states_array *info) |
| 836 | { |
| 837 | acpi_status status; |
| 838 | int ret = 0; |
| 839 | int pkg_count, state_idx = 1, loop; |
| 840 | struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL }; |
| 841 | union acpi_object *lpi_data; |
| 842 | struct acpi_lpi_state *lpi_state; |
| 843 | |
| 844 | status = acpi_evaluate_object(handle, "_LPI", NULL, &buffer); |
| 845 | if (ACPI_FAILURE(status)) { |
| 846 | ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _LPI, giving up\n")); |
| 847 | return -ENODEV; |
| 848 | } |
| 849 | |
| 850 | lpi_data = buffer.pointer; |
| 851 | |
| 852 | /* There must be at least 4 elements = 3 elements + 1 package */ |
| 853 | if (!lpi_data || lpi_data->type != ACPI_TYPE_PACKAGE || |
| 854 | lpi_data->package.count < 4) { |
| 855 | pr_debug("not enough elements in _LPI\n"); |
| 856 | ret = -ENODATA; |
| 857 | goto end; |
| 858 | } |
| 859 | |
| 860 | pkg_count = lpi_data->package.elements[2].integer.value; |
| 861 | |
| 862 | /* Validate number of power states. */ |
| 863 | if (pkg_count < 1 || pkg_count != lpi_data->package.count - 3) { |
| 864 | pr_debug("count given by _LPI is not valid\n"); |
| 865 | ret = -ENODATA; |
| 866 | goto end; |
| 867 | } |
| 868 | |
| 869 | lpi_state = kcalloc(pkg_count, sizeof(*lpi_state), GFP_KERNEL); |
| 870 | if (!lpi_state) { |
| 871 | ret = -ENOMEM; |
| 872 | goto end; |
| 873 | } |
| 874 | |
| 875 | info->size = pkg_count; |
| 876 | info->entries = lpi_state; |
| 877 | |
| 878 | /* LPI States start at index 3 */ |
| 879 | for (loop = 3; state_idx <= pkg_count; loop++, state_idx++, lpi_state++) { |
| 880 | union acpi_object *element, *pkg_elem, *obj; |
| 881 | |
| 882 | element = &lpi_data->package.elements[loop]; |
| 883 | if (element->type != ACPI_TYPE_PACKAGE || element->package.count < 7) |
| 884 | continue; |
| 885 | |
| 886 | pkg_elem = element->package.elements; |
| 887 | |
| 888 | obj = pkg_elem + 6; |
| 889 | if (obj->type == ACPI_TYPE_BUFFER) { |
| 890 | struct acpi_power_register *reg; |
| 891 | |
| 892 | reg = (struct acpi_power_register *)obj->buffer.pointer; |
| 893 | if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO && |
| 894 | reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE) |
| 895 | continue; |
| 896 | |
| 897 | lpi_state->address = reg->address; |
| 898 | lpi_state->entry_method = |
| 899 | reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE ? |
| 900 | ACPI_CSTATE_FFH : ACPI_CSTATE_SYSTEMIO; |
| 901 | } else if (obj->type == ACPI_TYPE_INTEGER) { |
| 902 | lpi_state->entry_method = ACPI_CSTATE_INTEGER; |
| 903 | lpi_state->address = obj->integer.value; |
| 904 | } else { |
| 905 | continue; |
| 906 | } |
| 907 | |
| 908 | /* elements[7,8] skipped for now i.e. Residency/Usage counter*/ |
| 909 | |
| 910 | obj = pkg_elem + 9; |
| 911 | if (obj->type == ACPI_TYPE_STRING) |
| 912 | strlcpy(lpi_state->desc, obj->string.pointer, |
| 913 | ACPI_CX_DESC_LEN); |
| 914 | |
| 915 | lpi_state->index = state_idx; |
| 916 | if (obj_get_integer(pkg_elem + 0, &lpi_state->min_residency)) { |
| 917 | pr_debug("No min. residency found, assuming 10 us\n"); |
| 918 | lpi_state->min_residency = 10; |
| 919 | } |
| 920 | |
| 921 | if (obj_get_integer(pkg_elem + 1, &lpi_state->wake_latency)) { |
| 922 | pr_debug("No wakeup residency found, assuming 10 us\n"); |
| 923 | lpi_state->wake_latency = 10; |
| 924 | } |
| 925 | |
| 926 | if (obj_get_integer(pkg_elem + 2, &lpi_state->flags)) |
| 927 | lpi_state->flags = 0; |
| 928 | |
| 929 | if (obj_get_integer(pkg_elem + 3, &lpi_state->arch_flags)) |
| 930 | lpi_state->arch_flags = 0; |
| 931 | |
| 932 | if (obj_get_integer(pkg_elem + 4, &lpi_state->res_cnt_freq)) |
| 933 | lpi_state->res_cnt_freq = 1; |
| 934 | |
| 935 | if (obj_get_integer(pkg_elem + 5, &lpi_state->enable_parent_state)) |
| 936 | lpi_state->enable_parent_state = 0; |
| 937 | } |
| 938 | |
| 939 | acpi_handle_debug(handle, "Found %d power states\n", state_idx); |
| 940 | end: |
| 941 | kfree(buffer.pointer); |
| 942 | return ret; |
| 943 | } |
| 944 | |
| 945 | /* |
| 946 | * flat_state_cnt - the number of composite LPI states after the process of flattening |
| 947 | */ |
| 948 | static int flat_state_cnt; |
| 949 | |
| 950 | /** |
| 951 | * combine_lpi_states - combine local and parent LPI states to form a composite LPI state |
| 952 | * |
| 953 | * @local: local LPI state |
| 954 | * @parent: parent LPI state |
| 955 | * @result: composite LPI state |
| 956 | */ |
| 957 | static bool combine_lpi_states(struct acpi_lpi_state *local, |
| 958 | struct acpi_lpi_state *parent, |
| 959 | struct acpi_lpi_state *result) |
| 960 | { |
| 961 | if (parent->entry_method == ACPI_CSTATE_INTEGER) { |
| 962 | if (!parent->address) /* 0 means autopromotable */ |
| 963 | return false; |
| 964 | result->address = local->address + parent->address; |
| 965 | } else { |
| 966 | result->address = parent->address; |
| 967 | } |
| 968 | |
| 969 | result->min_residency = max(local->min_residency, parent->min_residency); |
| 970 | result->wake_latency = local->wake_latency + parent->wake_latency; |
| 971 | result->enable_parent_state = parent->enable_parent_state; |
| 972 | result->entry_method = local->entry_method; |
| 973 | |
| 974 | result->flags = parent->flags; |
| 975 | result->arch_flags = parent->arch_flags; |
| 976 | result->index = parent->index; |
| 977 | |
| 978 | strlcpy(result->desc, local->desc, ACPI_CX_DESC_LEN); |
| 979 | strlcat(result->desc, "+", ACPI_CX_DESC_LEN); |
| 980 | strlcat(result->desc, parent->desc, ACPI_CX_DESC_LEN); |
| 981 | return true; |
| 982 | } |
| 983 | |
| 984 | #define ACPI_LPI_STATE_FLAGS_ENABLED BIT(0) |
| 985 | |
| 986 | static void stash_composite_state(struct acpi_lpi_states_array *curr_level, |
| 987 | struct acpi_lpi_state *t) |
| 988 | { |
| 989 | curr_level->composite_states[curr_level->composite_states_size++] = t; |
| 990 | } |
| 991 | |
| 992 | static int flatten_lpi_states(struct acpi_processor *pr, |
| 993 | struct acpi_lpi_states_array *curr_level, |
| 994 | struct acpi_lpi_states_array *prev_level) |
| 995 | { |
| 996 | int i, j, state_count = curr_level->size; |
| 997 | struct acpi_lpi_state *p, *t = curr_level->entries; |
| 998 | |
| 999 | curr_level->composite_states_size = 0; |
| 1000 | for (j = 0; j < state_count; j++, t++) { |
| 1001 | struct acpi_lpi_state *flpi; |
| 1002 | |
| 1003 | if (!(t->flags & ACPI_LPI_STATE_FLAGS_ENABLED)) |
| 1004 | continue; |
| 1005 | |
| 1006 | if (flat_state_cnt >= ACPI_PROCESSOR_MAX_POWER) { |
| 1007 | pr_warn("Limiting number of LPI states to max (%d)\n", |
| 1008 | ACPI_PROCESSOR_MAX_POWER); |
| 1009 | pr_warn("Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n"); |
| 1010 | break; |
| 1011 | } |
| 1012 | |
| 1013 | flpi = &pr->power.lpi_states[flat_state_cnt]; |
| 1014 | |
| 1015 | if (!prev_level) { /* leaf/processor node */ |
| 1016 | memcpy(flpi, t, sizeof(*t)); |
| 1017 | stash_composite_state(curr_level, flpi); |
| 1018 | flat_state_cnt++; |
| 1019 | continue; |
| 1020 | } |
| 1021 | |
| 1022 | for (i = 0; i < prev_level->composite_states_size; i++) { |
| 1023 | p = prev_level->composite_states[i]; |
| 1024 | if (t->index <= p->enable_parent_state && |
| 1025 | combine_lpi_states(p, t, flpi)) { |
| 1026 | stash_composite_state(curr_level, flpi); |
| 1027 | flat_state_cnt++; |
| 1028 | flpi++; |
| 1029 | } |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | kfree(curr_level->entries); |
| 1034 | return 0; |
| 1035 | } |
| 1036 | |
| 1037 | static int acpi_processor_get_lpi_info(struct acpi_processor *pr) |
| 1038 | { |
| 1039 | int ret, i; |
| 1040 | acpi_status status; |
| 1041 | acpi_handle handle = pr->handle, pr_ahandle; |
| 1042 | struct acpi_device *d = NULL; |
| 1043 | struct acpi_lpi_states_array info[2], *tmp, *prev, *curr; |
| 1044 | |
| 1045 | if (!osc_pc_lpi_support_confirmed) |
| 1046 | return -EOPNOTSUPP; |
| 1047 | |
| 1048 | if (!acpi_has_method(handle, "_LPI")) |
| 1049 | return -EINVAL; |
| 1050 | |
| 1051 | flat_state_cnt = 0; |
| 1052 | prev = &info[0]; |
| 1053 | curr = &info[1]; |
| 1054 | handle = pr->handle; |
| 1055 | ret = acpi_processor_evaluate_lpi(handle, prev); |
| 1056 | if (ret) |
| 1057 | return ret; |
| 1058 | flatten_lpi_states(pr, prev, NULL); |
| 1059 | |
| 1060 | status = acpi_get_parent(handle, &pr_ahandle); |
| 1061 | while (ACPI_SUCCESS(status)) { |
| 1062 | acpi_bus_get_device(pr_ahandle, &d); |
| 1063 | handle = pr_ahandle; |
| 1064 | |
| 1065 | if (strcmp(acpi_device_hid(d), ACPI_PROCESSOR_CONTAINER_HID)) |
| 1066 | break; |
| 1067 | |
| 1068 | /* can be optional ? */ |
| 1069 | if (!acpi_has_method(handle, "_LPI")) |
| 1070 | break; |
| 1071 | |
| 1072 | ret = acpi_processor_evaluate_lpi(handle, curr); |
| 1073 | if (ret) |
| 1074 | break; |
| 1075 | |
| 1076 | /* flatten all the LPI states in this level of hierarchy */ |
| 1077 | flatten_lpi_states(pr, curr, prev); |
| 1078 | |
| 1079 | tmp = prev, prev = curr, curr = tmp; |
| 1080 | |
| 1081 | status = acpi_get_parent(handle, &pr_ahandle); |
| 1082 | } |
| 1083 | |
| 1084 | pr->power.count = flat_state_cnt; |
| 1085 | /* reset the index after flattening */ |
| 1086 | for (i = 0; i < pr->power.count; i++) |
| 1087 | pr->power.lpi_states[i].index = i; |
| 1088 | |
| 1089 | /* Tell driver that _LPI is supported. */ |
| 1090 | pr->flags.has_lpi = 1; |
| 1091 | pr->flags.power = 1; |
| 1092 | |
| 1093 | return 0; |
| 1094 | } |
| 1095 | |
| 1096 | int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu) |
| 1097 | { |
| 1098 | return -ENODEV; |
| 1099 | } |
| 1100 | |
| 1101 | int __weak acpi_processor_ffh_lpi_enter(struct acpi_lpi_state *lpi) |
| 1102 | { |
| 1103 | return -ENODEV; |
| 1104 | } |
| 1105 | |
| 1106 | /** |
| 1107 | * acpi_idle_lpi_enter - enters an ACPI any LPI state |
| 1108 | * @dev: the target CPU |
| 1109 | * @drv: cpuidle driver containing cpuidle state info |
| 1110 | * @index: index of target state |
| 1111 | * |
| 1112 | * Return: 0 for success or negative value for error |
| 1113 | */ |
| 1114 | static int acpi_idle_lpi_enter(struct cpuidle_device *dev, |
| 1115 | struct cpuidle_driver *drv, int index) |
| 1116 | { |
| 1117 | struct acpi_processor *pr; |
| 1118 | struct acpi_lpi_state *lpi; |
| 1119 | |
| 1120 | pr = __this_cpu_read(processors); |
| 1121 | |
| 1122 | if (unlikely(!pr)) |
| 1123 | return -EINVAL; |
| 1124 | |
| 1125 | lpi = &pr->power.lpi_states[index]; |
| 1126 | if (lpi->entry_method == ACPI_CSTATE_FFH) |
| 1127 | return acpi_processor_ffh_lpi_enter(lpi); |
| 1128 | |
| 1129 | return -EINVAL; |
| 1130 | } |
| 1131 | |
| 1132 | static int acpi_processor_setup_lpi_states(struct acpi_processor *pr) |
| 1133 | { |
| 1134 | int i; |
| 1135 | struct acpi_lpi_state *lpi; |
| 1136 | struct cpuidle_state *state; |
| 1137 | struct cpuidle_driver *drv = &acpi_idle_driver; |
| 1138 | |
| 1139 | if (!pr->flags.has_lpi) |
| 1140 | return -EOPNOTSUPP; |
| 1141 | |
| 1142 | for (i = 0; i < pr->power.count && i < CPUIDLE_STATE_MAX; i++) { |
| 1143 | lpi = &pr->power.lpi_states[i]; |
| 1144 | |
| 1145 | state = &drv->states[i]; |
| 1146 | snprintf(state->name, CPUIDLE_NAME_LEN, "LPI-%d", i); |
| 1147 | strlcpy(state->desc, lpi->desc, CPUIDLE_DESC_LEN); |
| 1148 | state->exit_latency = lpi->wake_latency; |
| 1149 | state->target_residency = lpi->min_residency; |
| 1150 | if (lpi->arch_flags) |
| 1151 | state->flags |= CPUIDLE_FLAG_TIMER_STOP; |
| 1152 | state->enter = acpi_idle_lpi_enter; |
| 1153 | drv->safe_state_index = i; |
| 1154 | } |
| 1155 | |
| 1156 | drv->state_count = i; |
| 1157 | |
| 1158 | return 0; |
| 1159 | } |
| 1160 | |
| 1161 | /** |
| 1162 | * acpi_processor_setup_cpuidle_states- prepares and configures cpuidle |
| 1163 | * global state data i.e. idle routines |
| 1164 | * |
| 1165 | * @pr: the ACPI processor |
| 1166 | */ |
| 1167 | static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr) |
| 1168 | { |
| 1169 | int i; |
| 1170 | struct cpuidle_driver *drv = &acpi_idle_driver; |
| 1171 | |
| 1172 | if (!pr->flags.power_setup_done || !pr->flags.power) |
| 1173 | return -EINVAL; |
| 1174 | |
| 1175 | drv->safe_state_index = -1; |
Rafael J. Wysocki | dc2251b | 2017-08-23 23:19:57 +0200 | [diff] [blame] | 1176 | for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) { |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 1177 | drv->states[i].name[0] = '\0'; |
| 1178 | drv->states[i].desc[0] = '\0'; |
| 1179 | } |
| 1180 | |
| 1181 | if (pr->flags.has_lpi) |
| 1182 | return acpi_processor_setup_lpi_states(pr); |
| 1183 | |
| 1184 | return acpi_processor_setup_cstates(pr); |
| 1185 | } |
| 1186 | |
| 1187 | /** |
| 1188 | * acpi_processor_setup_cpuidle_dev - prepares and configures CPUIDLE |
| 1189 | * device i.e. per-cpu data |
| 1190 | * |
| 1191 | * @pr: the ACPI processor |
| 1192 | * @dev : the cpuidle device |
| 1193 | */ |
| 1194 | static int acpi_processor_setup_cpuidle_dev(struct acpi_processor *pr, |
| 1195 | struct cpuidle_device *dev) |
| 1196 | { |
| 1197 | if (!pr->flags.power_setup_done || !pr->flags.power || !dev) |
| 1198 | return -EINVAL; |
| 1199 | |
| 1200 | dev->cpu = pr->id; |
| 1201 | if (pr->flags.has_lpi) |
| 1202 | return acpi_processor_ffh_lpi_probe(pr->id); |
| 1203 | |
| 1204 | return acpi_processor_setup_cpuidle_cx(pr, dev); |
| 1205 | } |
| 1206 | |
| 1207 | static int acpi_processor_get_power_info(struct acpi_processor *pr) |
| 1208 | { |
| 1209 | int ret; |
| 1210 | |
| 1211 | ret = acpi_processor_get_lpi_info(pr); |
| 1212 | if (ret) |
| 1213 | ret = acpi_processor_get_cstate_info(pr); |
| 1214 | |
| 1215 | return ret; |
| 1216 | } |
| 1217 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1218 | int acpi_processor_hotplug(struct acpi_processor *pr) |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1219 | { |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 1220 | int ret = 0; |
Wei Yongjun | e8b1b59 | 2012-10-08 08:40:42 +0800 | [diff] [blame] | 1221 | struct cpuidle_device *dev; |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1222 | |
Thomas Renninger | d189604 | 2010-11-03 17:06:14 +0100 | [diff] [blame] | 1223 | if (disabled_by_idle_boot_param()) |
Venkatesh Pallipadi | 36a9135 | 2008-04-30 13:57:15 -0400 | [diff] [blame] | 1224 | return 0; |
| 1225 | |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1226 | if (!pr->flags.power_setup_done) |
| 1227 | return -ENODEV; |
| 1228 | |
Wei Yongjun | e8b1b59 | 2012-10-08 08:40:42 +0800 | [diff] [blame] | 1229 | dev = per_cpu(acpi_cpuidle_device, pr->id); |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1230 | cpuidle_pause_and_lock(); |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1231 | cpuidle_disable_device(dev); |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 1232 | ret = acpi_processor_get_power_info(pr); |
| 1233 | if (!ret && pr->flags.power) { |
| 1234 | acpi_processor_setup_cpuidle_dev(pr, dev); |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1235 | ret = cpuidle_enable_device(dev); |
Venkatesh Pallipadi | dcb84f3 | 2008-05-19 19:09:27 -0400 | [diff] [blame] | 1236 | } |
Len Brown | 4f86d3a | 2007-10-03 18:58:00 -0400 | [diff] [blame] | 1237 | cpuidle_resume_and_unlock(); |
| 1238 | |
| 1239 | return ret; |
| 1240 | } |
| 1241 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 1242 | int acpi_processor_power_state_has_changed(struct acpi_processor *pr) |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1243 | { |
| 1244 | int cpu; |
| 1245 | struct acpi_processor *_pr; |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1246 | struct cpuidle_device *dev; |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1247 | |
| 1248 | if (disabled_by_idle_boot_param()) |
| 1249 | return 0; |
| 1250 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1251 | if (!pr->flags.power_setup_done) |
| 1252 | return -ENODEV; |
| 1253 | |
| 1254 | /* |
| 1255 | * FIXME: Design the ACPI notification to make it once per |
| 1256 | * system instead of once per-cpu. This condition is a hack |
| 1257 | * to make the code that updates C-States be called once. |
| 1258 | */ |
| 1259 | |
Paul E. McKenney | 9505626 | 2012-02-28 13:27:44 -0800 | [diff] [blame] | 1260 | if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1261 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1262 | /* Protect against cpu-hotplug */ |
| 1263 | get_online_cpus(); |
Jiri Kosina | 6726655 | 2014-09-03 15:04:28 +0200 | [diff] [blame] | 1264 | cpuidle_pause_and_lock(); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1265 | |
| 1266 | /* Disable all cpuidle devices */ |
| 1267 | for_each_online_cpu(cpu) { |
| 1268 | _pr = per_cpu(processors, cpu); |
| 1269 | if (!_pr || !_pr->flags.power_setup_done) |
| 1270 | continue; |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1271 | dev = per_cpu(acpi_cpuidle_device, cpu); |
| 1272 | cpuidle_disable_device(dev); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1273 | } |
| 1274 | |
| 1275 | /* Populate Updated C-state information */ |
Thomas Schlichter | f427e5f | 2013-01-19 00:28:22 +0100 | [diff] [blame] | 1276 | acpi_processor_get_power_info(pr); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1277 | acpi_processor_setup_cpuidle_states(pr); |
| 1278 | |
| 1279 | /* Enable all cpuidle devices */ |
| 1280 | for_each_online_cpu(cpu) { |
| 1281 | _pr = per_cpu(processors, cpu); |
| 1282 | if (!_pr || !_pr->flags.power_setup_done) |
| 1283 | continue; |
| 1284 | acpi_processor_get_power_info(_pr); |
| 1285 | if (_pr->flags.power) { |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1286 | dev = per_cpu(acpi_cpuidle_device, cpu); |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 1287 | acpi_processor_setup_cpuidle_dev(_pr, dev); |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1288 | cpuidle_enable_device(dev); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1289 | } |
| 1290 | } |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1291 | cpuidle_resume_and_unlock(); |
Jiri Kosina | 6726655 | 2014-09-03 15:04:28 +0200 | [diff] [blame] | 1292 | put_online_cpus(); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1293 | } |
| 1294 | |
| 1295 | return 0; |
| 1296 | } |
| 1297 | |
| 1298 | static int acpi_processor_registered; |
| 1299 | |
Paul Gortmaker | fe7bf10 | 2013-06-19 14:30:58 -0400 | [diff] [blame] | 1300 | int acpi_processor_power_init(struct acpi_processor *pr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1301 | { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1302 | int retval; |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1303 | struct cpuidle_device *dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1304 | |
Thomas Renninger | d189604 | 2010-11-03 17:06:14 +0100 | [diff] [blame] | 1305 | if (disabled_by_idle_boot_param()) |
Venkatesh Pallipadi | 36a9135 | 2008-04-30 13:57:15 -0400 | [diff] [blame] | 1306 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1307 | |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 1308 | acpi_processor_cstate_first_run_checks(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1309 | |
Sudeep Holla | 35ae713 | 2016-07-19 18:52:53 +0100 | [diff] [blame] | 1310 | if (!acpi_processor_get_power_info(pr)) |
| 1311 | pr->flags.power_setup_done = 1; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1312 | |
| 1313 | /* |
| 1314 | * Install the idle handler if processor power management is supported. |
| 1315 | * Note that we use previously set idle handler will be used on |
| 1316 | * platforms that only support C1. |
| 1317 | */ |
Venkatesh Pallipadi | 36a9135 | 2008-04-30 13:57:15 -0400 | [diff] [blame] | 1318 | if (pr->flags.power) { |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1319 | /* Register acpi_idle_driver if not already registered */ |
| 1320 | if (!acpi_processor_registered) { |
| 1321 | acpi_processor_setup_cpuidle_states(pr); |
| 1322 | retval = cpuidle_register_driver(&acpi_idle_driver); |
| 1323 | if (retval) |
| 1324 | return retval; |
Sudeep Holla | b6ec26f | 2015-12-02 14:10:44 +0000 | [diff] [blame] | 1325 | pr_debug("%s registered with cpuidle\n", |
| 1326 | acpi_idle_driver.name); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1327 | } |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1328 | |
| 1329 | dev = kzalloc(sizeof(*dev), GFP_KERNEL); |
| 1330 | if (!dev) |
| 1331 | return -ENOMEM; |
| 1332 | per_cpu(acpi_cpuidle_device, pr->id) = dev; |
| 1333 | |
Sudeep Holla | a36a7fe | 2016-07-21 17:18:07 +0100 | [diff] [blame] | 1334 | acpi_processor_setup_cpuidle_dev(pr, dev); |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1335 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1336 | /* Register per-cpu cpuidle_device. Cpuidle driver |
| 1337 | * must already be registered before registering device |
| 1338 | */ |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1339 | retval = cpuidle_register_device(dev); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1340 | if (retval) { |
| 1341 | if (acpi_processor_registered == 0) |
| 1342 | cpuidle_unregister_driver(&acpi_idle_driver); |
| 1343 | return retval; |
| 1344 | } |
| 1345 | acpi_processor_registered++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1346 | } |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1347 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1348 | } |
| 1349 | |
Daniel Lezcano | 38a991b | 2012-09-15 22:42:54 +0200 | [diff] [blame] | 1350 | int acpi_processor_power_exit(struct acpi_processor *pr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1351 | { |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1352 | struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id); |
| 1353 | |
Thomas Renninger | d189604 | 2010-11-03 17:06:14 +0100 | [diff] [blame] | 1354 | if (disabled_by_idle_boot_param()) |
Venkatesh Pallipadi | 36a9135 | 2008-04-30 13:57:15 -0400 | [diff] [blame] | 1355 | return 0; |
| 1356 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1357 | if (pr->flags.power) { |
Daniel Lezcano | 3d339dc | 2012-09-17 23:01:56 +0200 | [diff] [blame] | 1358 | cpuidle_unregister_device(dev); |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1359 | acpi_processor_registered--; |
| 1360 | if (acpi_processor_registered == 0) |
| 1361 | cpuidle_unregister_driver(&acpi_idle_driver); |
| 1362 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1363 | |
Deepthi Dharwar | 46bcfad | 2011-10-28 16:20:42 +0530 | [diff] [blame] | 1364 | pr->flags.power_setup_done = 0; |
Patrick Mochel | d550d98 | 2006-06-27 00:41:40 -0400 | [diff] [blame] | 1365 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1366 | } |