blob: fc95308e9a114896289922869ff068f486fdf788 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * processor_idle - idle state submodule to the ACPI processor driver
3 *
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -04006 * Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * Copyright (C) 2004 Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8 * - Added processor hotplug support
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04009 * Copyright (C) 2005 Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10 * - Added support for C3 on SMP
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
12 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or (at
17 * your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22 * General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
27 *
28 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
29 */
30
Linus Torvalds1da177e2005-04-16 15:20:36 -070031#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032#include <linux/acpi.h>
33#include <linux/dmi.h>
Daniel Lezcanoe2668fb2013-02-04 22:44:41 +000034#include <linux/sched.h> /* need_resched() */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080035#include <linux/clockchips.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040036#include <linux/cpuidle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Thomas Gleixner34349332007-02-16 01:27:54 -080038/*
39 * Include the apic definitions for x86 to have the APIC timer related defines
40 * available also for UP (on SMP it gets magically included via linux/smp.h).
41 * asm/acpi.h is not an option, as it would require more include magic. Also
42 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
43 */
44#ifdef CONFIG_X86
45#include <asm/apic.h>
46#endif
47
Linus Torvalds1da177e2005-04-16 15:20:36 -070048#include <acpi/acpi_bus.h>
49#include <acpi/processor.h>
50
Len Browna192a952009-07-28 16:45:54 -040051#define PREFIX "ACPI: "
52
Linus Torvalds1da177e2005-04-16 15:20:36 -070053#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070054#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050055ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Len Brown4f86d3a2007-10-03 18:58:00 -040057static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
58module_param(max_cstate, uint, 0000);
Andreas Mohrb6835052006-04-27 05:25:00 -040059static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070060module_param(nocst, uint, 0000);
Len Brownd3e7e992010-07-22 17:23:10 -040061static int bm_check_disable __read_mostly;
62module_param(bm_check_disable, uint, 0000);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Len Brown25de5712007-12-14 00:24:15 -050064static unsigned int latency_factor __read_mostly = 2;
Len Brown4963f622007-12-13 23:50:45 -050065module_param(latency_factor, uint, 0644);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
Daniel Lezcano3d339dc2012-09-17 23:01:56 +020067static DEFINE_PER_CPU(struct cpuidle_device *, acpi_cpuidle_device);
68
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +000069static struct acpi_processor_cx *acpi_cstate[CPUIDLE_STATE_MAX];
70
Thomas Renningerd1896042010-11-03 17:06:14 +010071static int disabled_by_idle_boot_param(void)
72{
73 return boot_option_idle_override == IDLE_POLL ||
Thomas Renningerd1896042010-11-03 17:06:14 +010074 boot_option_idle_override == IDLE_HALT;
75}
76
Linus Torvalds1da177e2005-04-16 15:20:36 -070077/*
78 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
79 * For now disable this. Probably a bug somewhere else.
80 *
81 * To skip this limit, boot/load with a large max_cstate limit.
82 */
Jeff Garzik18552562007-10-03 15:15:40 -040083static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
86 return 0;
87
Len Brown3d356002005-08-03 00:22:52 -040088 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
Len Brown4be44fc2005-08-05 00:44:28 -040089 " Override with \"processor.max_cstate=%d\"\n", id->ident,
90 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
Len Brown3d356002005-08-03 00:22:52 -040092 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -070093
94 return 0;
95}
96
Ashok Raj7ded5682006-02-03 21:51:23 +010097/* Actually this shouldn't be __cpuinitdata, would be better to fix the
98 callers to only run once -AK */
99static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -0500100 { set_max_cstate, "Clevo 5600D", {
101 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
102 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
Len Brown4be44fc2005-08-05 00:44:28 -0400103 (void *)2},
Arjan van de Ven370d5cd2010-01-27 15:25:39 -0800104 { set_max_cstate, "Pavilion zv5000", {
105 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
106 DMI_MATCH(DMI_PRODUCT_NAME,"Pavilion zv5000 (DS502A#ABA)")},
107 (void *)1},
108 { set_max_cstate, "Asus L8400B", {
109 DMI_MATCH(DMI_SYS_VENDOR, "ASUSTeK Computer Inc."),
110 DMI_MATCH(DMI_PRODUCT_NAME,"L8400B series Notebook PC")},
111 (void *)1},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 {},
113};
114
Len Brown4f86d3a2007-10-03 18:58:00 -0400115
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -0800116/*
117 * Callers should disable interrupts before the call and enable
118 * interrupts after return.
119 */
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500120static void acpi_safe_halt(void)
121{
122 current_thread_info()->status &= ~TS_POLLING;
123 /*
124 * TS_POLLING-cleared state must be visible before we
125 * test NEED_RESCHED:
126 */
127 smp_mb();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700128 if (!need_resched()) {
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500129 safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700130 local_irq_disable();
131 }
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500132 current_thread_info()->status |= TS_POLLING;
133}
134
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800135#ifdef ARCH_APICTIMER_STOPS_ON_C3
136
137/*
138 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700139 * This seems to be a common problem on AMD boxen, but other vendors
140 * are affected too. We pick the most conservative approach: we assume
141 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800142 */
Len Brown7e275cc2009-05-15 02:08:44 -0400143static void lapic_timer_check_state(int state, struct acpi_processor *pr,
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800144 struct acpi_processor_cx *cx)
145{
146 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100147 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800148
Venkatesh Pallipadidb954b52009-04-06 18:51:29 -0700149 if (cpu_has(&cpu_data(pr->id), X86_FEATURE_ARAT))
150 return;
151
Len Brown02c68a02011-04-01 16:59:53 -0400152 if (amd_e400_c1e_detected)
Shaohua Li87ad57ba2009-05-19 16:09:42 +0800153 type = ACPI_STATE_C1;
154
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800155 /*
156 * Check, if one of the previous states already marked the lapic
157 * unstable
158 */
159 if (pwr->timer_broadcast_on_state < state)
160 return;
161
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100162 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700163 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800164}
165
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900166static void __lapic_timer_propagate_broadcast(void *arg)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800167{
Suresh Siddhaf833bab2009-08-17 14:34:59 -0700168 struct acpi_processor *pr = (struct acpi_processor *) arg;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800169 unsigned long reason;
170
171 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
172 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
173
174 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800175}
176
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900177static void lapic_timer_propagate_broadcast(struct acpi_processor *pr)
178{
179 smp_call_function_single(pr->id, __lapic_timer_propagate_broadcast,
180 (void *)pr, 1);
181}
182
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800183/* Power(C) State timer broadcast control */
Len Brown7e275cc2009-05-15 02:08:44 -0400184static void lapic_timer_state_broadcast(struct acpi_processor *pr,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800185 struct acpi_processor_cx *cx,
186 int broadcast)
187{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800188 int state = cx - pr->power.states;
189
190 if (state >= pr->power.timer_broadcast_on_state) {
191 unsigned long reason;
192
193 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
194 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
195 clockevents_notify(reason, &pr->id);
196 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800197}
198
199#else
200
Len Brown7e275cc2009-05-15 02:08:44 -0400201static void lapic_timer_check_state(int state, struct acpi_processor *pr,
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800202 struct acpi_processor_cx *cstate) { }
Len Brown7e275cc2009-05-15 02:08:44 -0400203static void lapic_timer_propagate_broadcast(struct acpi_processor *pr) { }
204static void lapic_timer_state_broadcast(struct acpi_processor *pr,
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800205 struct acpi_processor_cx *cx,
206 int broadcast)
207{
208}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800209
210#endif
211
Len Brown815ab0f2009-05-07 22:19:45 -0400212static u32 saved_bm_rld;
213
214static void acpi_idle_bm_rld_save(void)
215{
216 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &saved_bm_rld);
217}
218static void acpi_idle_bm_rld_restore(void)
219{
220 u32 resumed_bm_rld;
221
222 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_RLD, &resumed_bm_rld);
223
224 if (resumed_bm_rld != saved_bm_rld)
225 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, saved_bm_rld);
226}
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000227
Rafael J. Wysockie8110b62012-06-27 23:26:26 +0200228int acpi_processor_suspend(struct device *dev)
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000229{
Len Brown815ab0f2009-05-07 22:19:45 -0400230 acpi_idle_bm_rld_save();
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000231 return 0;
232}
233
Rafael J. Wysockie8110b62012-06-27 23:26:26 +0200234int acpi_processor_resume(struct device *dev)
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000235{
Len Brown815ab0f2009-05-07 22:19:45 -0400236 acpi_idle_bm_rld_restore();
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000237 return 0;
238}
239
John Stultz592913e2010-07-13 17:56:20 -0700240#if defined(CONFIG_X86)
Len Brown520daf72009-05-14 17:27:38 -0400241static void tsc_check_state(int state)
Andi Kleenddb25f92008-01-30 13:32:41 +0100242{
243 switch (boot_cpu_data.x86_vendor) {
244 case X86_VENDOR_AMD:
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800245 case X86_VENDOR_INTEL:
Andi Kleenddb25f92008-01-30 13:32:41 +0100246 /*
247 * AMD Fam10h TSC will tick in all
248 * C/P/S0/S1 states when this bit is set.
249 */
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800250 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Len Brown520daf72009-05-14 17:27:38 -0400251 return;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800252
Andi Kleenddb25f92008-01-30 13:32:41 +0100253 /*FALL THROUGH*/
Andi Kleenddb25f92008-01-30 13:32:41 +0100254 default:
Len Brown520daf72009-05-14 17:27:38 -0400255 /* TSC could halt in idle, so notify users */
256 if (state > ACPI_STATE_C1)
257 mark_tsc_unstable("TSC halts in idle");
Andi Kleenddb25f92008-01-30 13:32:41 +0100258 }
259}
Len Brown520daf72009-05-14 17:27:38 -0400260#else
261static void tsc_check_state(int state) { return; }
Andi Kleenddb25f92008-01-30 13:32:41 +0100262#endif
263
Len Brown4be44fc2005-08-05 00:44:28 -0400264static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
267 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400268 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
270 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400271 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
275 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
276
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400277#ifndef CONFIG_HOTPLUG_CPU
278 /*
279 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400280 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400281 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300282 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300283 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400284 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400285#endif
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 /* determine C2 and C3 address from pblk */
288 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
289 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
290
291 /* determine latencies from FADT */
Bob Mooreba494be2012-07-12 09:40:10 +0800292 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.c2_latency;
293 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.c3_latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294
Len Brown5d76b6f2010-01-19 22:41:14 -0500295 /*
296 * FADT specified C2 latency must be less than or equal to
297 * 100 microseconds.
298 */
Bob Mooreba494be2012-07-12 09:40:10 +0800299 if (acpi_gbl_FADT.c2_latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
Len Brown5d76b6f2010-01-19 22:41:14 -0500300 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Mooreba494be2012-07-12 09:40:10 +0800301 "C2 latency too large [%d]\n", acpi_gbl_FADT.c2_latency));
Len Brown5d76b6f2010-01-19 22:41:14 -0500302 /* invalidate C2 */
303 pr->power.states[ACPI_STATE_C2].address = 0;
304 }
305
Len Browna6d72c12010-01-19 23:10:04 -0500306 /*
307 * FADT supplied C3 latency must be less than or equal to
308 * 1000 microseconds.
309 */
Bob Mooreba494be2012-07-12 09:40:10 +0800310 if (acpi_gbl_FADT.c3_latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
Len Browna6d72c12010-01-19 23:10:04 -0500311 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Bob Mooreba494be2012-07-12 09:40:10 +0800312 "C3 latency too large [%d]\n", acpi_gbl_FADT.c3_latency));
Len Browna6d72c12010-01-19 23:10:04 -0500313 /* invalidate C3 */
314 pr->power.states[ACPI_STATE_C3].address = 0;
315 }
316
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
318 "lvl2[0x%08x] lvl3[0x%08x]\n",
319 pr->power.states[ACPI_STATE_C2].address,
320 pr->power.states[ACPI_STATE_C3].address));
321
Patrick Mocheld550d982006-06-27 00:41:40 -0400322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700325static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500326{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700327 if (!pr->power.states[ACPI_STATE_C1].valid) {
328 /* set the first C-State to C1 */
329 /* all processors need to support C1 */
330 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
331 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400332 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700333 }
334 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500335 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400336 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500337}
338
Len Brown4be44fc2005-08-05 00:44:28 -0400339static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340{
Len Brown4be44fc2005-08-05 00:44:28 -0400341 acpi_status status = 0;
Lin Ming439913f2010-01-28 10:53:19 +0800342 u64 count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400343 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400344 int i;
345 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
346 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400350 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700352 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353
354 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
355 if (ACPI_FAILURE(status)) {
356 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400357 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400358 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200360 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 /* There must be at least 2 elements */
363 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400364 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 status = -EFAULT;
366 goto end;
367 }
368
369 count = cst->package.elements[0].integer.value;
370
371 /* Validate number of power states. */
372 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400373 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 status = -EFAULT;
375 goto end;
376 }
377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Tell driver that at least _CST is supported. */
379 pr->flags.has_cst = 1;
380
381 for (i = 1; i <= count; i++) {
382 union acpi_object *element;
383 union acpi_object *obj;
384 struct acpi_power_register *reg;
385 struct acpi_processor_cx cx;
386
387 memset(&cx, 0, sizeof(cx));
388
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200389 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 if (element->type != ACPI_TYPE_PACKAGE)
391 continue;
392
393 if (element->package.count != 4)
394 continue;
395
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200396 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 if (obj->type != ACPI_TYPE_BUFFER)
399 continue;
400
Len Brown4be44fc2005-08-05 00:44:28 -0400401 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
403 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400404 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 continue;
406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200408 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 if (obj->type != ACPI_TYPE_INTEGER)
410 continue;
411
412 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700413 /*
414 * Some buggy BIOSes won't list C1 in _CST -
415 * Let acpi_processor_get_power_info_default() handle them later
416 */
417 if (i == 1 && cx.type != ACPI_STATE_C1)
418 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700420 cx.address = reg->address;
421 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800423 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700424 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
425 if (acpi_processor_ffh_cstate_probe
426 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800427 cx.entry_method = ACPI_CSTATE_FFH;
428 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700429 /*
430 * C1 is a special case where FIXED_HARDWARE
431 * can be handled in non-MWAIT way as well.
432 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700433 * Otherwise, ignore this info and continue.
434 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800435 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800436 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800437 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700438 continue;
439 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800440 if (cx.type == ACPI_STATE_C1 &&
Thomas Renningerd1896042010-11-03 17:06:14 +0100441 (boot_option_idle_override == IDLE_NOMWAIT)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800442 /*
443 * In most cases the C1 space_id obtained from
444 * _CST object is FIXED_HARDWARE access mode.
445 * But when the option of idle=halt is added,
446 * the entry_method type should be changed from
447 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800448 * When the option of idle=nomwait is added,
449 * the C1 entry_method type should be
450 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800451 */
452 cx.entry_method = ACPI_CSTATE_HALT;
453 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
454 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800455 } else {
456 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
457 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400460 if (cx.type == ACPI_STATE_C1) {
461 cx.valid = 1;
462 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800463
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200464 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (obj->type != ACPI_TYPE_INTEGER)
466 continue;
467
468 cx.latency = obj->integer.value;
469
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200470 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 if (obj->type != ACPI_TYPE_INTEGER)
472 continue;
473
Janosch Machowinskicf824782005-08-20 08:02:00 -0400474 current_count++;
475 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
476
477 /*
478 * We support total ACPI_PROCESSOR_MAX_POWER - 1
479 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
480 */
481 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
482 printk(KERN_WARNING
483 "Limiting number of power states to max (%d)\n",
484 ACPI_PROCESSOR_MAX_POWER);
485 printk(KERN_WARNING
486 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
487 break;
488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700489 }
490
Len Brown4be44fc2005-08-05 00:44:28 -0400491 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -0400492 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -0400495 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400496 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497
Len Brown4be44fc2005-08-05 00:44:28 -0400498 end:
Len Brown02438d82006-06-30 03:19:10 -0400499 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Patrick Mocheld550d982006-06-27 00:41:40 -0400501 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502}
503
Len Brown4be44fc2005-08-05 00:44:28 -0400504static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
505 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506{
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700507 static int bm_check_flag = -1;
508 static int bm_control_flag = -1;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
511 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -0400512 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
514 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
516 * DMA transfers are used by any ISA device to avoid livelock.
517 * Note that we could disable Type-F DMA (as recommended by
518 * the erratum), but this is known to disrupt certain ISA
519 * devices thus we take the conservative approach.
520 */
521 else if (errata.piix4.fdma) {
522 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400523 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400524 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400527 /* All the logic here assumes flags.bm_check is same across all CPUs */
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700528 if (bm_check_flag == -1) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400529 /* Determine whether bm_check is needed based on CPU */
530 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
531 bm_check_flag = pr->flags.bm_check;
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700532 bm_control_flag = pr->flags.bm_control;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400533 } else {
534 pr->flags.bm_check = bm_check_flag;
Pallipadi, Venkateshee1ca482009-05-21 17:09:10 -0700535 pr->flags.bm_control = bm_control_flag;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400536 }
537
538 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400539 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -0700540 if (pr->flags.has_cst != 1) {
541 /* bus mastering control is necessary */
542 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
543 "C3 support requires BM control\n"));
544 return;
545 } else {
546 /* Here we enter C3 without bus mastering */
547 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
548 "C3 support without BM control\n"));
549 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400550 }
551 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400552 /*
553 * WBINVD should be set in fadt, for C3 state to be
554 * supported on when bm_check is not required.
555 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300556 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400557 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400558 "Cache invalidation should work properly"
559 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400560 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400561 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400562 }
563
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 /*
565 * Otherwise we've met all of our C3 requirements.
566 * Normalize the C3 latency to expidite policy. Enable
567 * checking of bus mastering status (bm_check) so we can
568 * use this in our C3 policy
569 */
570 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400571
Len Brown31878dd2009-01-28 18:28:09 -0500572 /*
573 * On older chipsets, BM_RLD needs to be set
574 * in order for Bus Master activity to wake the
575 * system from C3. Newer chipsets handle DMA
576 * during C3 automatically and BM_RLD is a NOP.
577 * In either case, the proper way to
578 * handle BM_RLD is to set it and leave it set.
579 */
Bob Moore50ffba12009-02-23 15:02:07 +0800580 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581
Patrick Mocheld550d982006-06-27 00:41:40 -0400582 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583}
584
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585static int acpi_processor_power_verify(struct acpi_processor *pr)
586{
587 unsigned int i;
588 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100589
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800590 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +0100591
Len Browna0bf2842009-05-15 01:29:31 -0400592 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 struct acpi_processor_cx *cx = &pr->power.states[i];
594
595 switch (cx->type) {
596 case ACPI_STATE_C1:
597 cx->valid = 1;
598 break;
599
600 case ACPI_STATE_C2:
Len Brownd22edd22010-01-19 23:29:09 -0500601 if (!cx->address)
602 break;
603 cx->valid = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604 break;
605
606 case ACPI_STATE_C3:
607 acpi_processor_power_verify_c3(pr, cx);
608 break;
609 }
Len Brown7e275cc2009-05-15 02:08:44 -0400610 if (!cx->valid)
611 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612
Len Brown7e275cc2009-05-15 02:08:44 -0400613 lapic_timer_check_state(i, pr, cx);
614 tsc_check_state(cx->type);
615 working++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 }
617
Hidetoshi Seto918aae42009-12-14 17:10:06 +0900618 lapic_timer_propagate_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +0100619
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 return (working);
621}
622
Len Brown4be44fc2005-08-05 00:44:28 -0400623static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624{
625 unsigned int i;
626 int result;
627
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
629 /* NOTE: the idle thread may not be running while calling
630 * this function */
631
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700632 /* Zero initialize all the C-states info. */
633 memset(pr->power.states, 0, sizeof(pr->power.states));
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400636 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -0700637 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400638
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700639 if (result)
640 return result;
641
642 acpi_processor_get_power_info_default(pr);
643
Janosch Machowinskicf824782005-08-20 08:02:00 -0400644 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 /*
647 * if one state of type C2 or C3 is available, mark this
648 * CPU as being "idle manageable"
649 */
650 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500651 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -0800653 if (pr->power.states[i].type >= ACPI_STATE_C2)
654 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 }
657
Patrick Mocheld550d982006-06-27 00:41:40 -0400658 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
Len Brown4f86d3a2007-10-03 18:58:00 -0400661/**
662 * acpi_idle_bm_check - checks if bus master activity was detected
663 */
664static int acpi_idle_bm_check(void)
665{
666 u32 bm_status = 0;
667
Len Brownd3e7e992010-07-22 17:23:10 -0400668 if (bm_check_disable)
669 return 0;
670
Bob Moore50ffba12009-02-23 15:02:07 +0800671 acpi_read_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Len Brown4f86d3a2007-10-03 18:58:00 -0400672 if (bm_status)
Bob Moore50ffba12009-02-23 15:02:07 +0800673 acpi_write_bit_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400674 /*
675 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
676 * the true state of bus mastering activity; forcing us to
677 * manually check the BMIDEA bit of each IDE channel.
678 */
679 else if (errata.piix4.bmisx) {
680 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
681 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
682 bm_status = 1;
683 }
684 return bm_status;
685}
686
687/**
Len Brown4f86d3a2007-10-03 18:58:00 -0400688 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
689 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800690 *
691 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -0400692 */
693static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
694{
Steven Rostedtdcf30992008-07-25 18:00:42 -0400695 /* Don't trace irqs off for idle */
696 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800697 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -0400698 /* Call into architectural FFH based C-state */
699 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800700 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
701 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -0400702 } else {
Len Brown4f86d3a2007-10-03 18:58:00 -0400703 /* IO port based C-state */
704 inb(cx->address);
705 /* Dummy wait op - must do something useless after P_LVL2 read
706 because chipsets cannot guarantee that STPCLK# signal
707 gets asserted in time to freeze execution properly. */
Andi Kleencfa806f2010-07-20 15:18:36 -0700708 inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -0400709 }
Steven Rostedtdcf30992008-07-25 18:00:42 -0400710 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -0400711}
712
713/**
714 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
715 * @dev: the target CPU
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530716 * @drv: cpuidle driver containing cpuidle state info
Deepthi Dharware978aa72011-10-28 16:20:09 +0530717 * @index: index of target state
Len Brown4f86d3a2007-10-03 18:58:00 -0400718 *
719 * This is equivalent to the HALT instruction.
720 */
721static int acpi_idle_enter_c1(struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530722 struct cpuidle_driver *drv, int index)
Len Brown4f86d3a2007-10-03 18:58:00 -0400723{
724 struct acpi_processor *pr;
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +0000725 struct acpi_processor_cx *cx = acpi_cstate[index];
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -0800726
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600727 pr = __this_cpu_read(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -0400728
729 if (unlikely(!pr))
Deepthi Dharware978aa72011-10-28 16:20:09 +0530730 return -EINVAL;
Len Brown4f86d3a2007-10-03 18:58:00 -0400731
Len Brown7e275cc2009-05-15 02:08:44 -0400732 lapic_timer_state_broadcast(pr, cx, 1);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800733 acpi_idle_do_entry(cx);
Len Brown4f86d3a2007-10-03 18:58:00 -0400734
Len Brown7e275cc2009-05-15 02:08:44 -0400735 lapic_timer_state_broadcast(pr, cx, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -0400736
Deepthi Dharware978aa72011-10-28 16:20:09 +0530737 return index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400738}
739
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100740
741/**
742 * acpi_idle_play_dead - enters an ACPI state for long-term idle (i.e. off-lining)
743 * @dev: the target CPU
744 * @index: the index of suggested state
745 */
746static int acpi_idle_play_dead(struct cpuidle_device *dev, int index)
747{
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +0000748 struct acpi_processor_cx *cx = acpi_cstate[index];
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100749
750 ACPI_FLUSH_CPU_CACHE();
751
752 while (1) {
753
754 if (cx->entry_method == ACPI_CSTATE_HALT)
Luck, Tony54f70072012-04-03 09:37:28 -0700755 safe_halt();
Boris Ostrovsky1a022e32012-03-13 19:55:09 +0100756 else if (cx->entry_method == ACPI_CSTATE_SYSTEMIO) {
757 inb(cx->address);
758 /* See comment in acpi_idle_do_entry() */
759 inl(acpi_gbl_FADT.xpm_timer_block.address);
760 } else
761 return -ENODEV;
762 }
763
764 /* Never reached */
765 return 0;
766}
767
Len Brown4f86d3a2007-10-03 18:58:00 -0400768/**
769 * acpi_idle_enter_simple - enters an ACPI state without BM handling
770 * @dev: the target CPU
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530771 * @drv: cpuidle driver with cpuidle state information
Deepthi Dharware978aa72011-10-28 16:20:09 +0530772 * @index: the index of suggested state
Len Brown4f86d3a2007-10-03 18:58:00 -0400773 */
774static int acpi_idle_enter_simple(struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530775 struct cpuidle_driver *drv, int index)
Len Brown4f86d3a2007-10-03 18:58:00 -0400776{
777 struct acpi_processor *pr;
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +0000778 struct acpi_processor_cx *cx = acpi_cstate[index];
Venkatesh Pallipadi50629112007-11-19 19:49:00 -0500779
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600780 pr = __this_cpu_read(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -0400781
782 if (unlikely(!pr))
Deepthi Dharware978aa72011-10-28 16:20:09 +0530783 return -EINVAL;
Len Browne1964412007-10-04 01:23:47 -0400784
Pallipadi, Venkateshd306ebc2010-02-10 10:35:31 -0800785 if (cx->entry_method != ACPI_CSTATE_FFH) {
786 current_thread_info()->status &= ~TS_POLLING;
787 /*
788 * TS_POLLING-cleared state must be visible before we test
789 * NEED_RESCHED:
790 */
791 smp_mb();
Len Brown4f86d3a2007-10-03 18:58:00 -0400792
Len Brown02cf4f92010-05-24 14:27:44 -0400793 if (unlikely(need_resched())) {
794 current_thread_info()->status |= TS_POLLING;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530795 return -EINVAL;
Len Brown02cf4f92010-05-24 14:27:44 -0400796 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400797 }
798
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100799 /*
800 * Must be done before busmaster disable as we might need to
801 * access HPET !
802 */
Len Brown7e275cc2009-05-15 02:08:44 -0400803 lapic_timer_state_broadcast(pr, cx, 1);
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100804
Len Brown4f86d3a2007-10-03 18:58:00 -0400805 if (cx->type == ACPI_STATE_C3)
806 ACPI_FLUSH_CPU_CACHE();
807
Venkatesh Pallipadi50629112007-11-19 19:49:00 -0500808 /* Tell the scheduler that we are going deep-idle: */
809 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -0400810 acpi_idle_do_entry(cx);
Len Brown4f86d3a2007-10-03 18:58:00 -0400811
Julius Wernera474a512012-11-27 14:17:58 +0100812 sched_clock_idle_wakeup_event(0);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530813
Len Brown02cf4f92010-05-24 14:27:44 -0400814 if (cx->entry_method != ACPI_CSTATE_FFH)
815 current_thread_info()->status |= TS_POLLING;
Len Brown4f86d3a2007-10-03 18:58:00 -0400816
Len Brown7e275cc2009-05-15 02:08:44 -0400817 lapic_timer_state_broadcast(pr, cx, 0);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530818 return index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400819}
820
821static int c3_cpu_count;
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200822static DEFINE_RAW_SPINLOCK(c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400823
824/**
825 * acpi_idle_enter_bm - enters C3 with proper BM handling
826 * @dev: the target CPU
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530827 * @drv: cpuidle driver containing state data
Deepthi Dharware978aa72011-10-28 16:20:09 +0530828 * @index: the index of suggested state
Len Brown4f86d3a2007-10-03 18:58:00 -0400829 *
830 * If BM is detected, the deepest non-C3 idle state is entered instead.
831 */
832static int acpi_idle_enter_bm(struct cpuidle_device *dev,
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530833 struct cpuidle_driver *drv, int index)
Len Brown4f86d3a2007-10-03 18:58:00 -0400834{
835 struct acpi_processor *pr;
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +0000836 struct acpi_processor_cx *cx = acpi_cstate[index];
Venkatesh Pallipadi50629112007-11-19 19:49:00 -0500837
Christoph Lameter4a6f4fe2010-12-06 11:16:24 -0600838 pr = __this_cpu_read(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -0400839
840 if (unlikely(!pr))
Deepthi Dharware978aa72011-10-28 16:20:09 +0530841 return -EINVAL;
Len Brown4f86d3a2007-10-03 18:58:00 -0400842
Len Brown718be4a2010-07-22 16:54:27 -0400843 if (!cx->bm_sts_skip && acpi_idle_bm_check()) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530844 if (drv->safe_state_index >= 0) {
845 return drv->states[drv->safe_state_index].enter(dev,
846 drv, drv->safe_state_index);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500847 } else {
Preeti U Murthy8651f972012-07-09 10:12:56 +0200848 acpi_safe_halt();
Deepthi Dharwar75cc5232012-06-25 23:59:54 +0200849 return -EBUSY;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500850 }
851 }
852
Pallipadi, Venkateshd306ebc2010-02-10 10:35:31 -0800853 if (cx->entry_method != ACPI_CSTATE_FFH) {
854 current_thread_info()->status &= ~TS_POLLING;
855 /*
856 * TS_POLLING-cleared state must be visible before we test
857 * NEED_RESCHED:
858 */
859 smp_mb();
Len Brown4f86d3a2007-10-03 18:58:00 -0400860
Len Brown02cf4f92010-05-24 14:27:44 -0400861 if (unlikely(need_resched())) {
862 current_thread_info()->status |= TS_POLLING;
Deepthi Dharware978aa72011-10-28 16:20:09 +0530863 return -EINVAL;
Len Brown02cf4f92010-05-24 14:27:44 -0400864 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400865 }
866
Venki Pallipadi996520c2008-03-24 14:24:10 -0700867 acpi_unlazy_tlb(smp_processor_id());
868
Venkatesh Pallipadi50629112007-11-19 19:49:00 -0500869 /* Tell the scheduler that we are going deep-idle: */
870 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -0400871 /*
872 * Must be done before busmaster disable as we might need to
873 * access HPET !
874 */
Len Brown7e275cc2009-05-15 02:08:44 -0400875 lapic_timer_state_broadcast(pr, cx, 1);
Len Brown4f86d3a2007-10-03 18:58:00 -0400876
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500877 /*
878 * disable bus master
879 * bm_check implies we need ARB_DIS
880 * !bm_check implies we need cache flush
881 * bm_control implies whether we can do ARB_DIS
882 *
883 * That leaves a case where bm_check is set and bm_control is
884 * not set. In that case we cannot do much, we enter C3
885 * without doing anything.
886 */
887 if (pr->flags.bm_check && pr->flags.bm_control) {
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200888 raw_spin_lock(&c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400889 c3_cpu_count++;
890 /* Disable bus master arbitration when all CPUs are in C3 */
891 if (c3_cpu_count == num_online_cpus())
Bob Moore50ffba12009-02-23 15:02:07 +0800892 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 1);
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200893 raw_spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500894 } else if (!pr->flags.bm_check) {
895 ACPI_FLUSH_CPU_CACHE();
896 }
Len Brown4f86d3a2007-10-03 18:58:00 -0400897
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500898 acpi_idle_do_entry(cx);
Len Brown4f86d3a2007-10-03 18:58:00 -0400899
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500900 /* Re-enable bus master arbitration */
901 if (pr->flags.bm_check && pr->flags.bm_control) {
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200902 raw_spin_lock(&c3_lock);
Bob Moore50ffba12009-02-23 15:02:07 +0800903 acpi_write_bit_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -0400904 c3_cpu_count--;
Thomas Gleixnere12f65f2009-07-25 18:14:37 +0200905 raw_spin_unlock(&c3_lock);
Len Brown4f86d3a2007-10-03 18:58:00 -0400906 }
907
Julius Wernera474a512012-11-27 14:17:58 +0100908 sched_clock_idle_wakeup_event(0);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530909
Len Brown02cf4f92010-05-24 14:27:44 -0400910 if (cx->entry_method != ACPI_CSTATE_FFH)
911 current_thread_info()->status |= TS_POLLING;
Len Brown4f86d3a2007-10-03 18:58:00 -0400912
Len Brown7e275cc2009-05-15 02:08:44 -0400913 lapic_timer_state_broadcast(pr, cx, 0);
Deepthi Dharware978aa72011-10-28 16:20:09 +0530914 return index;
Len Brown4f86d3a2007-10-03 18:58:00 -0400915}
916
917struct cpuidle_driver acpi_idle_driver = {
918 .name = "acpi_idle",
919 .owner = THIS_MODULE,
Julius Wernera474a512012-11-27 14:17:58 +0100920 .en_core_tk_irqen = 1,
Len Brown4f86d3a2007-10-03 18:58:00 -0400921};
922
923/**
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530924 * acpi_processor_setup_cpuidle_cx - prepares and configures CPUIDLE
925 * device i.e. per-cpu data
926 *
Len Brown4f86d3a2007-10-03 18:58:00 -0400927 * @pr: the ACPI processor
Daniel Lezcano6ef0f082013-02-04 22:44:42 +0000928 * @dev : the cpuidle device
Len Brown4f86d3a2007-10-03 18:58:00 -0400929 */
Daniel Lezcano6ef0f082013-02-04 22:44:42 +0000930static int acpi_processor_setup_cpuidle_cx(struct acpi_processor *pr,
931 struct cpuidle_device *dev)
Len Brown4f86d3a2007-10-03 18:58:00 -0400932{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -0800933 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -0400934 struct acpi_processor_cx *cx;
Len Brown4f86d3a2007-10-03 18:58:00 -0400935
936 if (!pr->flags.power_setup_done)
937 return -EINVAL;
938
939 if (pr->flags.power == 0) {
940 return -EINVAL;
941 }
942
Konrad Rzeszutek Wilkb88a6342013-01-16 23:40:07 +0100943 if (!dev)
944 return -EINVAL;
945
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -0400946 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800947
Len Brown615dfd92009-04-23 23:21:29 -0400948 if (max_cstate == 0)
949 max_cstate = 1;
950
Len Brown4f86d3a2007-10-03 18:58:00 -0400951 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
952 cx = &pr->power.states[i];
Len Brown4f86d3a2007-10-03 18:58:00 -0400953
954 if (!cx->valid)
955 continue;
956
957#ifdef CONFIG_HOTPLUG_CPU
958 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
959 !pr->flags.has_cst &&
960 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
961 continue;
962#endif
Daniel Lezcanoac3ebaf2013-02-04 22:44:43 +0000963 acpi_cstate[count] = cx;
Len Brown4f86d3a2007-10-03 18:58:00 -0400964
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +0530965 count++;
966 if (count == CPUIDLE_STATE_MAX)
967 break;
968 }
969
970 dev->state_count = count;
971
972 if (!count)
973 return -EINVAL;
974
975 return 0;
976}
977
978/**
979 * acpi_processor_setup_cpuidle states- prepares and configures cpuidle
980 * global state data i.e. idle routines
981 *
982 * @pr: the ACPI processor
983 */
984static int acpi_processor_setup_cpuidle_states(struct acpi_processor *pr)
985{
986 int i, count = CPUIDLE_DRIVER_STATE_START;
987 struct acpi_processor_cx *cx;
988 struct cpuidle_state *state;
989 struct cpuidle_driver *drv = &acpi_idle_driver;
990
991 if (!pr->flags.power_setup_done)
992 return -EINVAL;
993
994 if (pr->flags.power == 0)
995 return -EINVAL;
996
997 drv->safe_state_index = -1;
998 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
999 drv->states[i].name[0] = '\0';
1000 drv->states[i].desc[0] = '\0';
1001 }
1002
1003 if (max_cstate == 0)
1004 max_cstate = 1;
1005
1006 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1007 cx = &pr->power.states[i];
1008
1009 if (!cx->valid)
1010 continue;
1011
1012#ifdef CONFIG_HOTPLUG_CPU
1013 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1014 !pr->flags.has_cst &&
1015 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1016 continue;
1017#endif
1018
1019 state = &drv->states[count];
Len Brown4f86d3a2007-10-03 18:58:00 -04001020 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001021 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001022 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001023 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001024
1025 state->flags = 0;
1026 switch (cx->type) {
1027 case ACPI_STATE_C1:
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001028 if (cx->entry_method == ACPI_CSTATE_FFH)
1029 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1030
Len Brown4f86d3a2007-10-03 18:58:00 -04001031 state->enter = acpi_idle_enter_c1;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +01001032 state->enter_dead = acpi_idle_play_dead;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301033 drv->safe_state_index = count;
Len Brown4f86d3a2007-10-03 18:58:00 -04001034 break;
1035
1036 case ACPI_STATE_C2:
Len Brown4f86d3a2007-10-03 18:58:00 -04001037 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1038 state->enter = acpi_idle_enter_simple;
Boris Ostrovsky1a022e32012-03-13 19:55:09 +01001039 state->enter_dead = acpi_idle_play_dead;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301040 drv->safe_state_index = count;
Len Brown4f86d3a2007-10-03 18:58:00 -04001041 break;
1042
1043 case ACPI_STATE_C3:
Len Brown4f86d3a2007-10-03 18:58:00 -04001044 state->flags |= CPUIDLE_FLAG_TIME_VALID;
Len Brown4f86d3a2007-10-03 18:58:00 -04001045 state->enter = pr->flags.bm_check ?
1046 acpi_idle_enter_bm :
1047 acpi_idle_enter_simple;
1048 break;
1049 }
1050
1051 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001052 if (count == CPUIDLE_STATE_MAX)
1053 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001054 }
1055
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301056 drv->state_count = count;
Len Brown4f86d3a2007-10-03 18:58:00 -04001057
1058 if (!count)
1059 return -EINVAL;
1060
Len Brown4f86d3a2007-10-03 18:58:00 -04001061 return 0;
1062}
1063
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301064int acpi_processor_hotplug(struct acpi_processor *pr)
Len Brown4f86d3a2007-10-03 18:58:00 -04001065{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001066 int ret = 0;
Wei Yongjune8b1b592012-10-08 08:40:42 +08001067 struct cpuidle_device *dev;
Len Brown4f86d3a2007-10-03 18:58:00 -04001068
Thomas Renningerd1896042010-11-03 17:06:14 +01001069 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001070 return 0;
1071
Len Brown4f86d3a2007-10-03 18:58:00 -04001072 if (!pr)
1073 return -EINVAL;
1074
1075 if (nocst) {
1076 return -ENODEV;
1077 }
1078
1079 if (!pr->flags.power_setup_done)
1080 return -ENODEV;
1081
Wei Yongjune8b1b592012-10-08 08:40:42 +08001082 dev = per_cpu(acpi_cpuidle_device, pr->id);
Len Brown4f86d3a2007-10-03 18:58:00 -04001083 cpuidle_pause_and_lock();
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001084 cpuidle_disable_device(dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001085 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001086 if (pr->flags.power) {
Daniel Lezcano6ef0f082013-02-04 22:44:42 +00001087 acpi_processor_setup_cpuidle_cx(pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001088 ret = cpuidle_enable_device(dev);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001089 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001090 cpuidle_resume_and_unlock();
1091
1092 return ret;
1093}
1094
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301095int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1096{
1097 int cpu;
1098 struct acpi_processor *_pr;
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001099 struct cpuidle_device *dev;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301100
1101 if (disabled_by_idle_boot_param())
1102 return 0;
1103
1104 if (!pr)
1105 return -EINVAL;
1106
1107 if (nocst)
1108 return -ENODEV;
1109
1110 if (!pr->flags.power_setup_done)
1111 return -ENODEV;
1112
1113 /*
1114 * FIXME: Design the ACPI notification to make it once per
1115 * system instead of once per-cpu. This condition is a hack
1116 * to make the code that updates C-States be called once.
1117 */
1118
Paul E. McKenney95056262012-02-28 13:27:44 -08001119 if (pr->id == 0 && cpuidle_get_driver() == &acpi_idle_driver) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301120
1121 cpuidle_pause_and_lock();
1122 /* Protect against cpu-hotplug */
1123 get_online_cpus();
1124
1125 /* Disable all cpuidle devices */
1126 for_each_online_cpu(cpu) {
1127 _pr = per_cpu(processors, cpu);
1128 if (!_pr || !_pr->flags.power_setup_done)
1129 continue;
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001130 dev = per_cpu(acpi_cpuidle_device, cpu);
1131 cpuidle_disable_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301132 }
1133
1134 /* Populate Updated C-state information */
Thomas Schlichterf427e5f2013-01-19 00:28:22 +01001135 acpi_processor_get_power_info(pr);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301136 acpi_processor_setup_cpuidle_states(pr);
1137
1138 /* Enable all cpuidle devices */
1139 for_each_online_cpu(cpu) {
1140 _pr = per_cpu(processors, cpu);
1141 if (!_pr || !_pr->flags.power_setup_done)
1142 continue;
1143 acpi_processor_get_power_info(_pr);
1144 if (_pr->flags.power) {
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001145 dev = per_cpu(acpi_cpuidle_device, cpu);
Daniel Lezcano6ef0f082013-02-04 22:44:42 +00001146 acpi_processor_setup_cpuidle_cx(_pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001147 cpuidle_enable_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301148 }
1149 }
1150 put_online_cpus();
1151 cpuidle_resume_and_unlock();
1152 }
1153
1154 return 0;
1155}
1156
1157static int acpi_processor_registered;
1158
Daniel Lezcano38a991b2012-09-15 22:42:54 +02001159int __cpuinit acpi_processor_power_init(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160{
Len Brown4be44fc2005-08-05 00:44:28 -04001161 acpi_status status = 0;
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301162 int retval;
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001163 struct cpuidle_device *dev;
Andreas Mohrb6835052006-04-27 05:25:00 -04001164 static int first_run;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Thomas Renningerd1896042010-11-03 17:06:14 +01001166 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001167 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
1169 if (!first_run) {
1170 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001171 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001173 printk(KERN_NOTICE
1174 "ACPI: processor limited to max C-state %d\n",
1175 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 first_run++;
1177 }
1178
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001179 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001180 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001181
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001182 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001183 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001184 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001186 ACPI_EXCEPTION((AE_INFO, status,
1187 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 }
1189 }
1190
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001192 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194 /*
1195 * Install the idle handler if processor power management is supported.
1196 * Note that we use previously set idle handler will be used on
1197 * platforms that only support C1.
1198 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001199 if (pr->flags.power) {
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301200 /* Register acpi_idle_driver if not already registered */
1201 if (!acpi_processor_registered) {
1202 acpi_processor_setup_cpuidle_states(pr);
1203 retval = cpuidle_register_driver(&acpi_idle_driver);
1204 if (retval)
1205 return retval;
1206 printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
1207 acpi_idle_driver.name);
1208 }
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001209
1210 dev = kzalloc(sizeof(*dev), GFP_KERNEL);
1211 if (!dev)
1212 return -ENOMEM;
1213 per_cpu(acpi_cpuidle_device, pr->id) = dev;
1214
Daniel Lezcano6ef0f082013-02-04 22:44:42 +00001215 acpi_processor_setup_cpuidle_cx(pr, dev);
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001216
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301217 /* Register per-cpu cpuidle_device. Cpuidle driver
1218 * must already be registered before registering device
1219 */
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001220 retval = cpuidle_register_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301221 if (retval) {
1222 if (acpi_processor_registered == 0)
1223 cpuidle_unregister_driver(&acpi_idle_driver);
1224 return retval;
1225 }
1226 acpi_processor_registered++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 }
Patrick Mocheld550d982006-06-27 00:41:40 -04001228 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229}
1230
Daniel Lezcano38a991b2012-09-15 22:42:54 +02001231int acpi_processor_power_exit(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232{
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001233 struct cpuidle_device *dev = per_cpu(acpi_cpuidle_device, pr->id);
1234
Thomas Renningerd1896042010-11-03 17:06:14 +01001235 if (disabled_by_idle_boot_param())
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001236 return 0;
1237
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301238 if (pr->flags.power) {
Daniel Lezcano3d339dc2012-09-17 23:01:56 +02001239 cpuidle_unregister_device(dev);
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301240 acpi_processor_registered--;
1241 if (acpi_processor_registered == 0)
1242 cpuidle_unregister_driver(&acpi_idle_driver);
1243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
Deepthi Dharwar46bcfad2011-10-28 16:20:42 +05301245 pr->flags.power_setup_done = 0;
Patrick Mocheld550d982006-06-27 00:41:40 -04001246 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001247}