blob: d93e400940a31b28aa90139e6958cae49abdd703 [file] [log] [blame]
Thomas Gleixnerc942fdd2019-05-27 08:55:06 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
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 Brodowskic5ab81c2006-06-24 19:37:00 -04007 * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
9 * - Added processor hotplug support
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -040010 * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
11 * - Added support for C3 on SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 */
Sudeep Hollab6ec26f2015-12-02 14:10:44 +000013#define pr_fmt(fmt) "ACPI: " fmt
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070016#include <linux/acpi.h>
17#include <linux/dmi.h>
Daniel Lezcanoe2668fb2013-02-04 22:44:41 +000018#include <linux/sched.h> /* need_resched() */
Thomas Gleixneree41eeb2015-04-03 02:02:00 +020019#include <linux/tick.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040020#include <linux/cpuidle.h>
Chris Metcalf6727ad92016-10-07 17:02:55 -070021#include <linux/cpu.h>
Lv Zheng8b484632013-12-03 08:49:16 +080022#include <acpi/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
Thomas Gleixner34349332007-02-16 01:27:54 -080024/*
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 Torvalds1da177e2005-04-16 15:20:36 -070034#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050035ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Rafael J. Wysockidc2251b2017-08-23 23:19:57 +020037#define ACPI_IDLE_STATE_START (IS_ENABLED(CONFIG_ARCH_HAS_CPU_RELAX) ? 1 : 0)
38
Len Brown4f86d3a2007-10-03 18:58:00 -040039static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
40module_param(max_cstate, uint, 0000);
Andreas Mohrb6835052006-04-27 05:25:00 -040041static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070042module_param(nocst, uint, 0000);
Len Brownd3e7e992010-07-22 17:23:10 -040043static int bm_check_disable __read_mostly;
44module_param(bm_check_disable, uint, 0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Len Brown25de5712007-12-14 00:24:15 -050046static unsigned int latency_factor __read_mostly = 2;
Len Brown4963f622007-12-13 23:50:45 -050047module_param(latency_factor, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
Daniel Lezcano3d339dc2012-09-17 23:01:56 +020049static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
50
Sudeep Holla35ae7132016-07-19 18:52:53 +010051struct cpuidle_driver acpi_idle_driver = {
52 .name = "acpi_idle",
53 .owner = THIS_MODULE,
54};
55
56#ifdef CONFIG_ACPI_PROCESSOR_CSTATE
Peter Zijlstra25528212016-03-15 14:52:49 -070057static
58DEFINE_PER_CPU(struct acpi_processor_cx * [CPUIDLE_STATE_MAX], acpi_cstate);
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +000059
Thomas Renningerd1896042010-11-03 17:06:14 +010060static int disabled_by_idle_boot_param(void)
61{
62 return boot_option_idle_override == IDLE_POLL ||
Thomas Renningerd1896042010-11-03 17:06:14 +010063 boot_option_idle_override == IDLE_HALT;
64}
65
Linus Torvalds1da177e2005-04-16 15:20:36 -070066/*
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 Garzik18552562007-10-03 15:15:40 -040072static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
75 return 0;
76
Sudeep Hollab6ec26f2015-12-02 14:10:44 +000077 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 Torvalds1da177e2005-04-16 15:20:36 -070080
Len Brown3d356002005-08-03 00:22:52 -040081 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 return 0;
84}
85
Mathias Krauseb0346682015-06-13 14:26:57 +020086static const struct dmi_system_id processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -050087 { 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 Brown4be44fc2005-08-05 00:44:28 -040090 (void *)2},
Arjan van de Ven370d5cd2010-01-27 15:25:39 -080091 { 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 Torvalds1da177e2005-04-16 15:20:36 -070099 {},
100};
101
Len Brown4f86d3a2007-10-03 18:58:00 -0400102
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -0800103/*
104 * Callers should disable interrupts before the call and enable
105 * interrupts after return.
106 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700107static void __cpuidle acpi_safe_halt(void)
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500108{
Peter Zijlstraea811742013-09-11 12:43:13 +0200109 if (!tif_need_resched()) {
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500110 safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700111 local_irq_disable();
112 }
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500113}
114
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800115#ifdef ARCH_APICTIMER_STOPS_ON_C3
116
117/*
118 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700119 * 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 Gleixner169a0ab2007-02-16 01:27:55 -0800122 */
Len Brown7e275cc2009-05-15 02:08:44 -0400123static void lapic_timer_check_state(int state, struct acpi_processor *pr,
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800124 struct acpi_processor_cx *cx)
125{
126 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100127 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800128
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700129 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
130 return;
131
Borislav Petkov07c94a32016-12-09 19:29:11 +0100132 if (boot_cpu_has_bug(X86_BUG_AMD_APIC_C1E))
Shaohua Li87ad57ba2009-05-19 16:09:42 +0800133 type = ACPI_STATE_C1;
134
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800135 /*
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 Gleixnere585bef2007-03-23 16:08:01 +0100142 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700143 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800144}
145
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900146static void __lapic_timer_propagate_broadcast(void *arg)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800147{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700148 struct acpi_processor *pr = (struct acpi_processor *) arg;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800149
Thomas Gleixneree41eeb2015-04-03 02:02:00 +0200150 if (pr->power.timer_broadcast_on_state < INT_MAX)
151 tick_broadcast_enable();
152 else
153 tick_broadcast_disable();
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800154}
155
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900156static 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 Gleixnere9e2cdb2007-02-16 01:28:04 -0800162/* Power(C) State timer broadcast control */
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200163static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
164 struct acpi_processor_cx *cx)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800165{
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200166 return cx - pr->power.states >= pr->power.timer_broadcast_on_state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800167}
168
169#else
170
Len Brown7e275cc2009-05-15 02:08:44 -0400171static void lapic_timer_check_state(int state, struct acpi_processor *pr,
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800172 struct acpi_processor_cx *cstate) { }
Len Brown7e275cc2009-05-15 02:08:44 -0400173static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200174
175static bool lapic_timer_needs_broadcast(struct acpi_processor *pr,
176 struct acpi_processor_cx *cx)
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800177{
Rafael J. Wysocki95592122020-09-23 13:50:12 +0200178 return false;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800179}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800180
181#endif
182
John Stultz592913e2010-07-13 17:56:20 -0700183#if defined(CONFIG_X86)
Len Brown520daf72009-05-14 17:27:38 -0400184static void tsc_check_state(int state)
Andi Kleenddb25f92008-01-30 13:32:41 +0100185{
186 switch (boot_cpu_data.x86_vendor) {
Pu Wen7377ed42018-09-23 17:37:05 +0800187 case X86_VENDOR_HYGON:
Andi Kleenddb25f92008-01-30 13:32:41 +0100188 case X86_VENDOR_AMD:
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800189 case X86_VENDOR_INTEL:
davidwangfe6daab2018-01-22 18:14:17 +0800190 case X86_VENDOR_CENTAUR:
Tony W Wang-oc773b2f32019-06-18 08:37:14 +0000191 case X86_VENDOR_ZHAOXIN:
Andi Kleenddb25f92008-01-30 13:32:41 +0100192 /*
193 * AMD Fam10h TSC will tick in all
194 * C/P/S0/S1 states when this bit is set.
195 */
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800196 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Len Brown520daf72009-05-14 17:27:38 -0400197 return;
Gustavo A. R. Silva57d2dd42020-07-07 15:09:37 -0500198 fallthrough;
Andi Kleenddb25f92008-01-30 13:32:41 +0100199 default:
Len Brown520daf72009-05-14 17:27:38 -0400200 /* TSC could halt in idle, so notify users */
201 if (state > ACPI_STATE_C1)
202 mark_tsc_unstable("TSC halts in idle");
Andi Kleenddb25f92008-01-30 13:32:41 +0100203 }
204}
Len Brown520daf72009-05-14 17:27:38 -0400205#else
206static void tsc_check_state(int state) { return; }
Andi Kleenddb25f92008-01-30 13:32:41 +0100207#endif
208
Len Brown4be44fc2005-08-05 00:44:28 -0400209static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400213 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
217 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
218
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400219#ifndef CONFIG_HOTPLUG_CPU
220 /*
221 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400222 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400223 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300224 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300225 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400226 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400227#endif
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 /* 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 Mooreba494be2012-07-12 09:40:10 +0800234 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 Torvalds1da177e2005-04-16 15:20:36 -0700236
Len Brown5d76b6f2010-01-19 22:41:14 -0500237 /*
238 * FADT specified C2 latency must be less than or equal to
239 * 100 microseconds.
240 */
Bob Mooreba494be2012-07-12 09:40:10 +0800241 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
Len Brown5d76b6f2010-01-19 22:41:14 -0500242 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Mooreba494be2012-07-12 09:40:10 +0800243 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
Len Brown5d76b6f2010-01-19 22:41:14 -0500244 /* invalidate C2 */
245 pr->power.states[ACPI_STATE_C2].address = 0;
246 }
247
Len Browna6d72c12010-01-19 23:10:04 -0500248 /*
249 * FADT supplied C3 latency must be less than or equal to
250 * 1000 microseconds.
251 */
Bob Mooreba494be2012-07-12 09:40:10 +0800252 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
Len Browna6d72c12010-01-19 23:10:04 -0500253 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Mooreba494be2012-07-12 09:40:10 +0800254 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
Len Browna6d72c12010-01-19 23:10:04 -0500255 /* invalidate C3 */
256 pr->power.states[ACPI_STATE_C3].address = 0;
257 }
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 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 Ghannam34a62cd2019-02-18 01:33:49 +0000264 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 Mocheld550d982006-06-27 00:41:40 -0400271 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272}
273
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700274static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500275{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700276 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 Pallipadi0fda6b42008-04-09 21:31:46 -0400281 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Yazen Ghannam248e8842018-01-29 14:22:49 -0600282
283 snprintf(pr->power.states[ACPI_STATE_C1].desc,
284 ACPI_CX_DESC_LEN, "ACPI HLT");
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700285 }
286 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500287 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400288 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500289}
290
Rafael J. Wysocki987c7852019-12-13 09:55:24 +0100291static 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 Rui496121c2020-04-22 15:26:07 +0800302 if (!pr->power.count)
Rafael J. Wysocki987c7852019-12-13 09:55:24 +0100303 return -EFAULT;
304
305 pr->flags.has_cst = 1;
306 return 0;
307}
308
Len Brown4be44fc2005-08-05 00:44:28 -0400309static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
310 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311{
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700312 static int bm_check_flag = -1;
313 static int bm_control_flag = -1;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315
316 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -0400317 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
319 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * 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 Brown4be44fc2005-08-05 00:44:28 -0400328 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400329 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 }
331
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400332 /* All the logic here assumes flags.bm_check is same across all CPUs */
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700333 if (bm_check_flag == -1) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400334 /* 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, Venkateshee1ca482009-05-21 17:09:10 -0700337 bm_control_flag = pr->flags.bm_control;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400338 } else {
339 pr->flags.bm_check = bm_check_flag;
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700340 pr->flags.bm_control = bm_control_flag;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400341 }
342
343 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400344 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -0700345 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 Pallipadi02df8b92005-04-15 15:07:10 -0400355 }
356 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400357 /*
358 * WBINVD should be set in fadt, for C3 state to be
359 * supported on when bm_check is not required.
360 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300361 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400362 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400363 "Cache invalidation should work properly"
364 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400365 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400366 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400367 }
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 /*
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 Brown4f86d3a2007-10-03 18:58:00 -0400376
Len Brown31878dd2009-01-28 18:28:09 -0500377 /*
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 Moore50ffba12009-02-23 15:02:07 +0800385 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Patrick Mocheld550d982006-06-27 00:41:40 -0400387 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390static int acpi_processor_power_verify(struct acpi_processor *pr)
391{
392 unsigned int i;
393 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100394
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800395 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100396
Len Browna0bf2842009-05-15 01:29:31 -0400397 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 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 Brownd22edd22010-01-19 23:29:09 -0500406 if (!cx->address)
407 break;
Al Stonecad15252013-12-04 12:59:11 -0700408 cx->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 break;
410
411 case ACPI_STATE_C3:
412 acpi_processor_power_verify_c3(pr, cx);
413 break;
414 }
Len Brown7e275cc2009-05-15 02:08:44 -0400415 if (!cx->valid)
416 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417
Len Brown7e275cc2009-05-15 02:08:44 -0400418 lapic_timer_check_state(i, pr, cx);
419 tsc_check_state(cx->type);
420 working++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900423 lapic_timer_propagate_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +0100424
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 return (working);
426}
427
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100428static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429{
430 unsigned int i;
431 int result;
432
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
434 /* NOTE: the idle thread may not be running while calling
435 * this function */
436
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700437 /* Zero initialize all the C-states info. */
438 memset(pr->power.states, 0, sizeof(pr->power.states));
439
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400441 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -0700442 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400443
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700444 if (result)
445 return result;
446
447 acpi_processor_get_power_info_default(pr);
448
Janosch Machowinskicf824782005-08-20 08:02:00 -0400449 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 /*
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 Pallipadiacf05f42005-03-31 23:23:15 -0500456 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 pr->power.count = i;
Zhang Rui496121c2020-04-22 15:26:07 +0800458 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 }
461
Patrick Mocheld550d982006-06-27 00:41:40 -0400462 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463}
464
Len Brown4f86d3a2007-10-03 18:58:00 -0400465/**
466 * acpi_idle_bm_check - checks if bus master activity was detected
467 */
468static int acpi_idle_bm_check(void)
469{
470 u32 bm_status = 0;
471
Len Brownd3e7e992010-07-22 17:23:10 -0400472 if (bm_check_disable)
473 return 0;
474
Bob Moore50ffba12009-02-23 15:02:07 +0800475 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Len Brown4f86d3a2007-10-03 18:58:00 -0400476 if (bm_status)
Bob Moore50ffba12009-02-23 15:02:07 +0800477 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400478 /*
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 Fengweifa583f72019-10-24 15:04:20 +0800491static 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 Brown4f86d3a2007-10-03 18:58:00 -0400504/**
Rafael J. Wysockib00783f2015-02-02 23:55:08 +0100505 * acpi_idle_do_entry - enter idle state using the appropriate method
Len Brown4f86d3a2007-10-03 18:58:00 -0400506 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800507 *
508 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -0400509 */
Chris Metcalf6727ad92016-10-07 17:02:55 -0700510static void __cpuidle acpi_idle_do_entry(struct acpi_processor_cx *cx)
Len Brown4f86d3a2007-10-03 18:58:00 -0400511{
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800512 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400513 /* Call into architectural FFH based C-state */
514 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800515 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
516 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -0400517 } else {
Len Brown4f86d3a2007-10-03 18:58:00 -0400518 /* IO port based C-state */
519 inb(cx->address);
Yin Fengweifa583f72019-10-24 15:04:20 +0800520 wait_for_freeze();
Len Brown4f86d3a2007-10-03 18:58:00 -0400521 }
522}
523
524/**
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100525 * 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 */
529static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
530{
Alex Shi6240a102013-04-02 01:56:54 +0200531 struct acpi_processor_cx *cx = per_cpu(acpi_cstate[index], dev->cpu);
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100532
533 ACPI_FLUSH_CPU_CACHE();
534
535 while (1) {
536
537 if (cx->entry_method == ACPI_CSTATE_HALT)
Luck, Tony54f70072012-04-03 09:37:28 -0700538 safe_halt();
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100539 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
540 inb(cx->address);
Yin Fengweifa583f72019-10-24 15:04:20 +0800541 wait_for_freeze();
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100542 } else
543 return -ENODEV;
544 }
545
546 /* Never reached */
547 return 0;
548}
549
Rafael J. Wysockiadcb2622015-02-02 23:55:42 +0100550static bool acpi_idle_fallback_to_c1(struct acpi_processor *pr)
551{
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100552 return IS_ENABLED(CONFIG_HOTPLUG_CPU) && !pr->flags.has_cst &&
553 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED);
Rafael J. Wysockiadcb2622015-02-02 23:55:42 +0100554}
555
Len Brown4f86d3a2007-10-03 18:58:00 -0400556static int c3_cpu_count;
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200557static DEFINE_RAW_SPINLOCK(c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400558
559/**
560 * acpi_idle_enter_bm - enters C3 with proper BM handling
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200561 * @drv: cpuidle driver
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100562 * @pr: Target processor
563 * @cx: Target state context
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200564 * @index: index of target state
Len Brown4f86d3a2007-10-03 18:58:00 -0400565 */
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200566static int acpi_idle_enter_bm(struct cpuidle_driver *drv,
567 struct acpi_processor *pr,
568 struct acpi_processor_cx *cx,
569 int index)
Len Brown4f86d3a2007-10-03 18:58:00 -0400570{
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200571 static struct acpi_processor_cx safe_cx = {
572 .entry_method = ACPI_CSTATE_HALT,
573 };
574
Len Brown4f86d3a2007-10-03 18:58:00 -0400575 /*
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500576 * disable bus master
577 * bm_check implies we need ARB_DIS
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500578 * bm_control implies whether we can do ARB_DIS
579 *
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200580 * 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 Pallipadiddc081a2007-11-19 21:43:22 -0500582 */
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200583 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 Gleixnere12f65f2009-07-25 18:14:37 +0200598 raw_spin_lock(&c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400599 c3_cpu_count++;
600 /* Disable bus master arbitration when all CPUs are in C3 */
601 if (c3_cpu_count == num_online_cpus())
Bob Moore50ffba12009-02-23 15:02:07 +0800602 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200603 raw_spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500604 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400605
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200606 rcu_idle_enter();
607
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500608 acpi_idle_do_entry(cx);
Len Brown4f86d3a2007-10-03 18:58:00 -0400609
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200610 rcu_idle_exit();
611
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500612 /* Re-enable bus master arbitration */
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200613 if (dis_bm) {
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200614 raw_spin_lock(&c3_lock);
Bob Moore50ffba12009-02-23 15:02:07 +0800615 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -0400616 c3_cpu_count--;
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200617 raw_spin_unlock(&c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400618 }
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200619
620 return index;
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100621}
622
623static 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 Zijlstra1fecfdb2020-09-15 12:32:01 +0200634 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. Wysocki5f508182015-02-11 05:04:57 +0100638 if (acpi_idle_fallback_to_c1(pr) && num_online_cpus() > 1) {
Rafael J. Wysockidc2251b2017-08-23 23:19:57 +0200639 index = ACPI_IDLE_STATE_START;
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100640 cx = per_cpu(acpi_cstate[index], dev->cpu);
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100641 }
642 }
643
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100644 if (cx->type == ACPI_STATE_C3)
645 ACPI_FLUSH_CPU_CACHE();
646
647 acpi_idle_do_entry(cx);
648
Deepthi Dharware978aa72011-10-28 16:20:09 +0530649 return index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400650}
651
Neal Liuefe97112020-07-27 11:25:46 +0800652static int acpi_idle_enter_s2idle(struct cpuidle_device *dev,
653 struct cpuidle_driver *drv, int index)
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100654{
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 Liu0a398942020-07-31 11:39:04 +0800661 return 0;
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100662
663 if (pr->flags.bm_check) {
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200664 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 Liu0a398942020-07-31 11:39:04 +0800671 return 0;
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100672 } else {
673 ACPI_FLUSH_CPU_CACHE();
674 }
675 }
676 acpi_idle_do_entry(cx);
Neal Liuefe97112020-07-27 11:25:46 +0800677
678 return 0;
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100679}
680
Daniel Lezcano6ef0f082013-02-04 22:44:42 +0000681static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
682 struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400683{
Rafael J. Wysockidc2251b2017-08-23 23:19:57 +0200684 int i, count = ACPI_IDLE_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -0400685 struct acpi_processor_cx *cx;
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200686 struct cpuidle_state *state;
Len Brown4f86d3a2007-10-03 18:58:00 -0400687
Len Brown615dfd92009-04-23 23:21:29 -0400688 if (max_cstate == 0)
689 max_cstate = 1;
690
Len Brown4f86d3a2007-10-03 18:58:00 -0400691 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
Peter Zijlstraa889a232020-09-15 12:31:59 +0200692 state = &acpi_idle_driver.states[count];
Len Brown4f86d3a2007-10-03 18:58:00 -0400693 cx = &pr->power.states[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400694
695 if (!cx->valid)
696 continue;
697
Alex Shi6240a102013-04-02 01:56:54 +0200698 per_cpu(acpi_cstate[count], dev->cpu) = cx;
Len Brown4f86d3a2007-10-03 18:58:00 -0400699
Peter Zijlstraa889a232020-09-15 12:31:59 +0200700 if (lapic_timer_needs_broadcast(pr, cx))
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200701 state->flags |= CPUIDLE_FLAG_TIMER_STOP;
Peter Zijlstraa889a232020-09-15 12:31:59 +0200702
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200703 if (cx->type == ACPI_STATE_C3) {
Peter Zijlstraa889a232020-09-15 12:31:59 +0200704 state->flags |= CPUIDLE_FLAG_TLB_FLUSHED;
Peter Zijlstra1fecfdb2020-09-15 12:32:01 +0200705 if (pr->flags.bm_check)
706 state->flags |= CPUIDLE_FLAG_RCU_IDLE;
707 }
Peter Zijlstraaa6b43d2020-09-15 12:31:58 +0200708
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530709 count++;
710 if (count == CPUIDLE_STATE_MAX)
711 break;
712 }
713
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530714 if (!count)
715 return -EINVAL;
716
717 return 0;
718}
719
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100720static int acpi_processor_setup_cstates(struct acpi_processor *pr)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530721{
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +0200722 int i, count;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530723 struct acpi_processor_cx *cx;
724 struct cpuidle_state *state;
725 struct cpuidle_driver *drv = &acpi_idle_driver;
726
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530727 if (max_cstate == 0)
728 max_cstate = 1;
729
Rafael J. Wysocki1b39e3f2017-08-29 03:14:37 +0200730 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 Dharwar46bcfad2011-10-28 16:20:42 +0530737 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 Dharwar46bcfad2011-10-28 16:20:42 +0530743 state = &drv->states[count];
Len Brown4f86d3a2007-10-03 18:58:00 -0400744 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100745 strlcpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -0400746 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -0500747 state->target_residency = cx->latency * latency_factor;
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100748 state->enter = acpi_idle_enter;
Len Brown4f86d3a2007-10-03 18:58:00 -0400749
750 state->flags = 0;
Rafael J. Wysocki6491bc02015-02-03 21:55:11 +0100751 if (cx->type == ACPI_STATE_C1 || cx->type == ACPI_STATE_C2) {
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100752 state->enter_dead = acpi_idle_play_dead;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530753 drv->safe_state_index = count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400754 }
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100755 /*
Rafael J. Wysocki28ba0862017-08-10 00:14:45 +0200756 * Halt-induced C1 is not good for ->enter_s2idle, because it
Rafael J. Wysocki5f508182015-02-11 05:04:57 +0100757 * 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. Wysocki28ba0862017-08-10 00:14:45 +0200763 state->enter_s2idle = acpi_idle_enter_s2idle;
Len Brown4f86d3a2007-10-03 18:58:00 -0400764
765 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800766 if (count == CPUIDLE_STATE_MAX)
767 break;
Len Brown4f86d3a2007-10-03 18:58:00 -0400768 }
769
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530770 drv->state_count = count;
Len Brown4f86d3a2007-10-03 18:58:00 -0400771
772 if (!count)
773 return -EINVAL;
774
Len Brown4f86d3a2007-10-03 18:58:00 -0400775 return 0;
776}
777
Sudeep Holla35ae7132016-07-19 18:52:53 +0100778static inline void acpi_processor_cstate_first_run_checks(void)
779{
Sudeep Holla35ae7132016-07-19 18:52:53 +0100780 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. Wysockibc946382019-12-13 09:55:14 +0100791 if (nocst)
792 return;
793
794 acpi_processor_claim_cst_control();
Sudeep Holla35ae7132016-07-19 18:52:53 +0100795}
796#else
797
798static inline int disabled_by_idle_boot_param(void) { return 0; }
799static inline void acpi_processor_cstate_first_run_checks(void) { }
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100800static int acpi_processor_get_cstate_info(struct acpi_processor *pr)
Sudeep Holla35ae7132016-07-19 18:52:53 +0100801{
802 return -ENODEV;
803}
804
805static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
806 struct cpuidle_device *dev)
807{
808 return -EINVAL;
809}
810
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100811static int acpi_processor_setup_cstates(struct acpi_processor *pr)
Sudeep Holla35ae7132016-07-19 18:52:53 +0100812{
813 return -EINVAL;
814}
815
816#endif /* CONFIG_ACPI_PROCESSOR_CSTATE */
817
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +0100818struct 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
825static 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
834static 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);
940end:
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 */
948static 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 */
957static 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
986static 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
992static 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
1037static 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
1096int __weak acpi_processor_ffh_lpi_probe(unsigned int cpu)
1097{
1098 return -ENODEV;
1099}
1100
1101int __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 */
1114static 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
1132static 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 */
1167static 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. Wysockidc2251b2017-08-23 23:19:57 +02001176 for (i = ACPI_IDLE_STATE_START; i < CPUIDLE_STATE_MAX; i++) {
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +01001177 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 */
1194static 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
1207static 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 Dharwar46bcfad2011-10-28 16:20:42 +05301218int acpi_processor_hotplug(struct acpi_processor *pr)
Len Brown4f86d3a2007-10-03 18:58:00 -04001219{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001220 int ret = 0;
Wei Yongjune8b1b592012-10-08 08:40:42 +08001221 struct cpuidle_device *dev;
Len Brown4f86d3a2007-10-03 18:58:00 -04001222
Thomas Renningerd1896042010-11-03 17:06:14 +01001223 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001224 return 0;
1225
Len Brown4f86d3a2007-10-03 18:58:00 -04001226 if (!pr->flags.power_setup_done)
1227 return -ENODEV;
1228
Wei Yongjune8b1b592012-10-08 08:40:42 +08001229 dev = per_cpu(acpi_cpuidle_device, pr->id);
Len Brown4f86d3a2007-10-03 18:58:00 -04001230 cpuidle_pause_and_lock();
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001231 cpuidle_disable_device(dev);
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +01001232 ret = acpi_processor_get_power_info(pr);
1233 if (!ret && pr->flags.power) {
1234 acpi_processor_setup_cpuidle_dev(pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001235 ret = cpuidle_enable_device(dev);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001236 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001237 cpuidle_resume_and_unlock();
1238
1239 return ret;
1240}
1241
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +01001242int acpi_processor_power_state_has_changed(struct acpi_processor *pr)
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301243{
1244 int cpu;
1245 struct acpi_processor *_pr;
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001246 struct cpuidle_device *dev;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301247
1248 if (disabled_by_idle_boot_param())
1249 return 0;
1250
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301251 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. McKenney95056262012-02-28 13:27:44 -08001260 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301261
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301262 /* Protect against cpu-hotplug */
1263 get_online_cpus();
Jiri Kosina67266552014-09-03 15:04:28 +02001264 cpuidle_pause_and_lock();
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301265
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 Lezcano3d339dc2012-09-17 23:01:56 +02001271 dev = per_cpu(acpi_cpuidle_device, cpu);
1272 cpuidle_disable_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301273 }
1274
1275 /* Populate Updated C-state information */
Thomas Schlichterf427e5f2013-01-19 00:28:22 +01001276 acpi_processor_get_power_info(pr);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301277 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 Lezcano3d339dc2012-09-17 23:01:56 +02001286 dev = per_cpu(acpi_cpuidle_device, cpu);
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +01001287 acpi_processor_setup_cpuidle_dev(_pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001288 cpuidle_enable_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301289 }
1290 }
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301291 cpuidle_resume_and_unlock();
Jiri Kosina67266552014-09-03 15:04:28 +02001292 put_online_cpus();
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301293 }
1294
1295 return 0;
1296}
1297
1298static int acpi_processor_registered;
1299
Paul Gortmakerfe7bf102013-06-19 14:30:58 -04001300int acpi_processor_power_init(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301{
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301302 int retval;
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001303 struct cpuidle_device *dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Thomas Renningerd1896042010-11-03 17:06:14 +01001305 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001306 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307
Sudeep Holla35ae7132016-07-19 18:52:53 +01001308 acpi_processor_cstate_first_run_checks();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309
Sudeep Holla35ae7132016-07-19 18:52:53 +01001310 if (!acpi_processor_get_power_info(pr))
1311 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312
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 Pallipadi36a91352008-04-30 13:57:15 -04001318 if (pr->flags.power) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301319 /* 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 Hollab6ec26f2015-12-02 14:10:44 +00001325 pr_debug("%s registered with cpuidle\n",
1326 acpi_idle_driver.name);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301327 }
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001328
1329 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1330 if (!dev)
1331 return -ENOMEM;
1332 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1333
Sudeep Hollaa36a7fe2016-07-21 17:18:07 +01001334 acpi_processor_setup_cpuidle_dev(pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001335
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301336 /* Register per-cpu cpuidle_device. Cpuidle driver
1337 * must already be registered before registering device
1338 */
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001339 retval = cpuidle_register_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301340 if (retval) {
1341 if (acpi_processor_registered == 0)
1342 cpuidle_unregister_driver(&acpi_idle_driver);
1343 return retval;
1344 }
1345 acpi_processor_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001347 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001348}
1349
Daniel Lezcano38a991b2012-09-15 22:42:54 +02001350int acpi_processor_power_exit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351{
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001352 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1353
Thomas Renningerd1896042010-11-03 17:06:14 +01001354 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001355 return 0;
1356
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301357 if (pr->flags.power) {
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001358 cpuidle_unregister_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301359 acpi_processor_registered--;
1360 if (acpi_processor_registered == 0)
1361 cpuidle_unregister_driver(&acpi_idle_driver);
1362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301364 pr->flags.power_setup_done = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -04001365 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366}