blob: c75c7ace8c1322c767a71e6e096ae3e4302ac694 [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
31#include <linux/kernel.h>
32#include <linux/module.h>
33#include <linux/init.h>
34#include <linux/cpufreq.h>
35#include <linux/proc_fs.h>
36#include <linux/seq_file.h>
37#include <linux/acpi.h>
38#include <linux/dmi.h>
39#include <linux/moduleparam.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080040#include <linux/sched.h> /* need_resched() */
Mark Grossf011e2e2008-02-04 22:30:09 -080041#include <linux/pm_qos_params.h>
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -080042#include <linux/clockchips.h>
Len Brown4f86d3a2007-10-03 18:58:00 -040043#include <linux/cpuidle.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080044#include <linux/cpuidle.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
Thomas Gleixner34349332007-02-16 01:27:54 -080046/*
47 * Include the apic definitions for x86 to have the APIC timer related defines
48 * available also for UP (on SMP it gets magically included via linux/smp.h).
49 * asm/acpi.h is not an option, as it would require more include magic. Also
50 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
51 */
52#ifdef CONFIG_X86
53#include <asm/apic.h>
54#endif
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056#include <asm/io.h>
57#include <asm/uaccess.h>
58
59#include <acpi/acpi_bus.h>
60#include <acpi/processor.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080061#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
63#define ACPI_PROCESSOR_COMPONENT 0x01000000
64#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050066ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define ACPI_PROCESSOR_FILE_POWER "power"
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
Ingo Molnar2aa44d02007-08-23 15:18:02 +020069#define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
Len Brown4f86d3a2007-10-03 18:58:00 -040070#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
72#define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
Andreas Mohrb6835052006-04-27 05:25:00 -040073static void (*pm_idle_save) (void) __read_mostly;
Len Brown4f86d3a2007-10-03 18:58:00 -040074#else
75#define C2_OVERHEAD 1 /* 1us */
76#define C3_OVERHEAD 1 /* 1us */
77#endif
78#define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
Len Brown4f86d3a2007-10-03 18:58:00 -040080static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050081#ifdef CONFIG_CPU_IDLE
Len Brown4f86d3a2007-10-03 18:58:00 -040082module_param(max_cstate, uint, 0000);
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050083#else
84module_param(max_cstate, uint, 0644);
85#endif
Andreas Mohrb6835052006-04-27 05:25:00 -040086static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087module_param(nocst, uint, 0000);
88
Len Brown4f86d3a2007-10-03 18:58:00 -040089#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*
91 * bm_history -- bit-mask with a bit per jiffy of bus-master activity
92 * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
93 * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
94 * 100 HZ: 0x0000000F: 4 jiffies = 40ms
95 * reduce history for more aggressive entry into C3
96 */
Andreas Mohrb6835052006-04-27 05:25:00 -040097static unsigned int bm_history __read_mostly =
Len Brown4be44fc2005-08-05 00:44:28 -040098 (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070099module_param(bm_history, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -0400100
101static int acpi_processor_set_power_policy(struct acpi_processor *pr);
102
Len Brown4963f622007-12-13 23:50:45 -0500103#else /* CONFIG_CPU_IDLE */
Len Brown25de5712007-12-14 00:24:15 -0500104static unsigned int latency_factor __read_mostly = 2;
Len Brown4963f622007-12-13 23:50:45 -0500105module_param(latency_factor, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -0400106#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108/*
109 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
110 * For now disable this. Probably a bug somewhere else.
111 *
112 * To skip this limit, boot/load with a large max_cstate limit.
113 */
Jeff Garzik18552562007-10-03 15:15:40 -0400114static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
117 return 0;
118
Len Brown3d356002005-08-03 00:22:52 -0400119 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
Len Brown4be44fc2005-08-05 00:44:28 -0400120 " Override with \"processor.max_cstate=%d\"\n", id->ident,
121 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Len Brown3d356002005-08-03 00:22:52 -0400123 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
125 return 0;
126}
127
Ashok Raj7ded5682006-02-03 21:51:23 +0100128/* Actually this shouldn't be __cpuinitdata, would be better to fix the
129 callers to only run once -AK */
130static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -0500131 { set_max_cstate, "IBM ThinkPad R40e", {
132 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Bartlomiej Swierczf8313352006-05-29 07:16:00 -0400133 DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
134 { set_max_cstate, "IBM ThinkPad R40e", {
135 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Thomas Rosner876c1842006-01-06 01:31:00 -0500136 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
137 { set_max_cstate, "IBM ThinkPad R40e", {
138 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
139 DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
140 { set_max_cstate, "IBM ThinkPad R40e", {
141 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
142 DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
143 { set_max_cstate, "IBM ThinkPad R40e", {
144 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
145 DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
146 { set_max_cstate, "IBM ThinkPad R40e", {
147 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
148 DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
149 { set_max_cstate, "IBM ThinkPad R40e", {
150 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
151 DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
152 { set_max_cstate, "IBM ThinkPad R40e", {
153 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
154 DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
155 { set_max_cstate, "IBM ThinkPad R40e", {
156 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
157 DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
158 { set_max_cstate, "IBM ThinkPad R40e", {
159 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
160 DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
161 { set_max_cstate, "IBM ThinkPad R40e", {
162 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
163 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
164 { set_max_cstate, "IBM ThinkPad R40e", {
165 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
166 DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
167 { set_max_cstate, "IBM ThinkPad R40e", {
168 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
169 DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
170 { set_max_cstate, "IBM ThinkPad R40e", {
171 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
172 DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
173 { set_max_cstate, "IBM ThinkPad R40e", {
174 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
175 DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
176 { set_max_cstate, "IBM ThinkPad R40e", {
177 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
178 DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
179 { set_max_cstate, "Medion 41700", {
180 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
181 DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
182 { set_max_cstate, "Clevo 5600D", {
183 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
184 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
Len Brown4be44fc2005-08-05 00:44:28 -0400185 (void *)2},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 {},
187};
188
Len Brown4be44fc2005-08-05 00:44:28 -0400189static inline u32 ticks_elapsed(u32 t1, u32 t2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
191 if (t2 >= t1)
192 return (t2 - t1);
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300193 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
195 else
196 return ((0xFFFFFFFF - t1) + t2);
197}
198
Len Brown4f86d3a2007-10-03 18:58:00 -0400199static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
200{
201 if (t2 >= t1)
202 return PM_TIMER_TICKS_TO_US(t2 - t1);
203 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
204 return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
205 else
206 return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
207}
208
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -0800209/*
210 * Callers should disable interrupts before the call and enable
211 * interrupts after return.
212 */
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500213static void acpi_safe_halt(void)
214{
215 current_thread_info()->status &= ~TS_POLLING;
216 /*
217 * TS_POLLING-cleared state must be visible before we
218 * test NEED_RESCHED:
219 */
220 smp_mb();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700221 if (!need_resched()) {
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500222 safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700223 local_irq_disable();
224 }
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500225 current_thread_info()->status |= TS_POLLING;
226}
227
Len Brown4f86d3a2007-10-03 18:58:00 -0400228#ifndef CONFIG_CPU_IDLE
229
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230static void
Len Brown4be44fc2005-08-05 00:44:28 -0400231acpi_processor_power_activate(struct acpi_processor *pr,
232 struct acpi_processor_cx *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Len Brown4be44fc2005-08-05 00:44:28 -0400234 struct acpi_processor_cx *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 if (!pr || !new)
237 return;
238
239 old = pr->power.state;
240
241 if (old)
242 old->promotion.count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400243 new->demotion.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244
245 /* Cleanup from old state. */
246 if (old) {
247 switch (old->type) {
248 case ACPI_STATE_C3:
249 /* Disable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400250 if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300251 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 break;
253 }
254 }
255
256 /* Prepare to use new state. */
257 switch (new->type) {
258 case ACPI_STATE_C3:
259 /* Enable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400260 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300261 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 break;
263 }
264
265 pr->power.state = new;
266
267 return;
268}
269
Len Brown4be44fc2005-08-05 00:44:28 -0400270static atomic_t c3_cpu_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700272/* Common C-state entry for C2, C3, .. */
273static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
274{
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800275 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700276 /* Call into architectural FFH based C-state */
277 acpi_processor_ffh_cstate_enter(cstate);
278 } else {
279 int unused;
280 /* IO port based C-state */
281 inb(cstate->address);
282 /* Dummy wait op - must do something useless after P_LVL2 read
283 because chipsets cannot guarantee that STPCLK# signal
284 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300285 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700286 }
287}
Len Brown4f86d3a2007-10-03 18:58:00 -0400288#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700289
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800290#ifdef ARCH_APICTIMER_STOPS_ON_C3
291
292/*
293 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700294 * This seems to be a common problem on AMD boxen, but other vendors
295 * are affected too. We pick the most conservative approach: we assume
296 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800297 */
298static void acpi_timer_check_state(int state, struct acpi_processor *pr,
299 struct acpi_processor_cx *cx)
300{
301 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100302 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800303
304 /*
305 * Check, if one of the previous states already marked the lapic
306 * unstable
307 */
308 if (pwr->timer_broadcast_on_state < state)
309 return;
310
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100311 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700312 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800313}
314
315static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
316{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800317 unsigned long reason;
318
319 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
320 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
321
322 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800323}
324
325/* Power(C) State timer broadcast control */
326static void acpi_state_timer_broadcast(struct acpi_processor *pr,
327 struct acpi_processor_cx *cx,
328 int broadcast)
329{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800330 int state = cx - pr->power.states;
331
332 if (state >= pr->power.timer_broadcast_on_state) {
333 unsigned long reason;
334
335 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
336 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
337 clockevents_notify(reason, &pr->id);
338 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800339}
340
341#else
342
343static void acpi_timer_check_state(int state, struct acpi_processor *pr,
344 struct acpi_processor_cx *cstate) { }
345static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800346static void acpi_state_timer_broadcast(struct acpi_processor *pr,
347 struct acpi_processor_cx *cx,
348 int broadcast)
349{
350}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800351
352#endif
353
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000354/*
355 * Suspend / resume control
356 */
357static int acpi_idle_suspend;
358
359int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
360{
361 acpi_idle_suspend = 1;
362 return 0;
363}
364
365int acpi_processor_resume(struct acpi_device * device)
366{
367 acpi_idle_suspend = 0;
368 return 0;
369}
370
Pavel Machek61331162008-02-19 11:00:29 +0100371#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100372static int tsc_halts_in_c(int state)
373{
374 switch (boot_cpu_data.x86_vendor) {
375 case X86_VENDOR_AMD:
376 /*
377 * AMD Fam10h TSC will tick in all
378 * C/P/S0/S1 states when this bit is set.
379 */
380 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
381 return 0;
382 /*FALL THROUGH*/
383 case X86_VENDOR_INTEL:
384 /* Several cases known where TSC halts in C2 too */
385 default:
386 return state > ACPI_STATE_C1;
387 }
388}
389#endif
390
Len Brown4f86d3a2007-10-03 18:58:00 -0400391#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400392static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393{
Len Brown4be44fc2005-08-05 00:44:28 -0400394 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 struct acpi_processor_cx *cx = NULL;
396 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400397 int sleep_ticks = 0;
398 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 /*
401 * Interrupts must be disabled during bus mastering calculations and
402 * for C2/C3 transitions.
403 */
404 local_irq_disable();
405
Mike Travis706546d2008-06-09 16:22:23 -0700406 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400407 if (!pr) {
408 local_irq_enable();
409 return;
410 }
411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /*
413 * Check whether we truly need to go idle, or should
414 * reschedule:
415 */
416 if (unlikely(need_resched())) {
417 local_irq_enable();
418 return;
419 }
420
421 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000422 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200423 if (pm_idle_save) {
424 pm_idle_save(); /* enables IRQs */
425 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800426 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700427 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200428 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700429
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800430 return;
431 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
433 /*
434 * Check BM Activity
435 * -----------------
436 * Check for bus mastering activity (if required), record, and check
437 * for demotion.
438 */
439 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400440 u32 bm_status = 0;
441 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400443 if (diff > 31)
444 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400446 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Bob Moored8c71b62007-02-02 19:48:21 +0300448 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400450 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300451 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 }
453 /*
454 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
455 * the true state of bus mastering activity; forcing us to
456 * manually check the BMIDEA bit of each IDE channel.
457 */
458 else if (errata.piix4.bmisx) {
459 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400460 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400461 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 pr->power.bm_check_timestamp = jiffies;
465
466 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400467 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 * to avoid a faulty transition. Note that the processor
469 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400470 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 *
472 * TBD: A better policy might be to fallback to the demotion
473 * state (use it for this quantum only) istead of
474 * demoting -- and rely on duration as our sole demotion
475 * qualification. This may, however, introduce DMA
476 * issues (e.g. floppy DMA transfer overrun/underrun).
477 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400478 if ((pr->power.bm_activity & 0x1) &&
479 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 local_irq_enable();
481 next_state = cx->demotion.state;
482 goto end;
483 }
484 }
485
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400486#ifdef CONFIG_HOTPLUG_CPU
487 /*
488 * Check for P_LVL2_UP flag before entering C2 and above on
489 * an SMP system. We do it here instead of doing it at _CST/P_LVL
490 * detection phase, to work cleanly with logical CPU hotplug.
491 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400492 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300493 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500494 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400495#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 /*
498 * Sleep:
499 * ------
500 * Invoke the current Cx state to put the processor to sleep.
501 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100502 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200503 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800504 /*
505 * TS_POLLING-cleared state must be visible before we
506 * test NEED_RESCHED:
507 */
508 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100509 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200510 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800511 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100512 return;
513 }
514 }
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 switch (cx->type) {
517
518 case ACPI_STATE_C1:
519 /*
520 * Invoke C1.
521 * Use the appropriate idle routine, the one that would
522 * be used without acpi C-states.
523 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200524 if (pm_idle_save) {
525 pm_idle_save(); /* enables IRQs */
526 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800527 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200528 local_irq_enable();
529 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800530
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400532 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * go to an ISR rather than here. Need to instrument
534 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200535 *
536 * Note: the TSC better not stop in C1, sched_clock() will
537 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 */
539 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700540
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 break;
542
543 case ACPI_STATE_C2:
544 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300545 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200546 /* Tell the scheduler that we are going deep-idle: */
547 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800549 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700550 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300552 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700553
Pavel Machek61331162008-02-19 11:00:29 +0100554#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700555 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100556 if (tsc_halts_in_c(ACPI_STATE_C2))
557 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700558#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200559 /* Compute time (ticks) that we were actually asleep */
560 sleep_ticks = ticks_elapsed(t1, t2);
561
562 /* Tell the scheduler how much we idled: */
563 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
564
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 /* Re-enable interrupts */
566 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200567 /* Do not account our idle-switching overhead: */
568 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
569
Andi Kleen495ab9c2006-06-26 13:59:11 +0200570 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800571 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 break;
573
574 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100575 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400576 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100577 * Must be done before busmaster disable as we might
578 * need to access HPET !
579 */
580 acpi_state_timer_broadcast(pr, cx, 1);
581 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400582 * disable bus master
583 * bm_check implies we need ARB_DIS
584 * !bm_check implies we need cache flush
585 * bm_control implies whether we can do ARB_DIS
586 *
587 * That leaves a case where bm_check is set and bm_control is
588 * not set. In that case we cannot do much, we enter C3
589 * without doing anything.
590 */
591 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400592 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400593 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400594 /*
595 * All CPUs are trying to go to C3
596 * Disable bus master arbitration
597 */
Bob Moored8c71b62007-02-02 19:48:21 +0300598 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400599 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400600 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400601 /* SMP with no shared cache... Invalidate cache */
602 ACPI_FLUSH_CPU_CACHE();
603 }
Len Brown4be44fc2005-08-05 00:44:28 -0400604
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300606 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200608 /* Tell the scheduler that we are going deep-idle: */
609 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700610 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300612 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400613 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400614 /* Enable bus master arbitration */
615 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300616 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400617 }
618
Pavel Machek61331162008-02-19 11:00:29 +0100619#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700620 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100621 if (tsc_halts_in_c(ACPI_STATE_C3))
622 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700623#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200624 /* Compute time (ticks) that we were actually asleep */
625 sleep_ticks = ticks_elapsed(t1, t2);
626 /* Tell the scheduler how much we idled: */
627 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
628
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 /* Re-enable interrupts */
630 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200631 /* Do not account our idle-switching overhead: */
632 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
633
Andi Kleen495ab9c2006-06-26 13:59:11 +0200634 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800635 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 break;
637
638 default:
639 local_irq_enable();
640 return;
641 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400642 cx->usage++;
643 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
644 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
646 next_state = pr->power.state;
647
David Shaohua Li1e483962005-12-01 17:00:00 -0500648#ifdef CONFIG_HOTPLUG_CPU
649 /* Don't do promotion/demotion */
650 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300651 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500652 next_state = cx;
653 goto end;
654 }
655#endif
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * Promotion?
659 * ----------
660 * Track the number of longs (time asleep is greater than threshold)
661 * and promote when the count threshold is reached. Note that bus
662 * mastering activity may prevent promotions.
663 * Do not promote above max_cstate.
664 */
665 if (cx->promotion.state &&
666 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700667 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800668 cx->promotion.state->latency <=
669 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400671 cx->demotion.count = 0;
672 if (cx->promotion.count >=
673 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400675 if (!
676 (pr->power.bm_activity & cx->
677 promotion.threshold.bm)) {
678 next_state =
679 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 goto end;
681 }
Len Brown4be44fc2005-08-05 00:44:28 -0400682 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 next_state = cx->promotion.state;
684 goto end;
685 }
686 }
687 }
688 }
689
690 /*
691 * Demotion?
692 * ---------
693 * Track the number of shorts (time asleep is less than time threshold)
694 * and demote when the usage threshold is reached.
695 */
696 if (cx->demotion.state) {
697 if (sleep_ticks < cx->demotion.threshold.ticks) {
698 cx->demotion.count++;
699 cx->promotion.count = 0;
700 if (cx->demotion.count >= cx->demotion.threshold.count) {
701 next_state = cx->demotion.state;
702 goto end;
703 }
704 }
705 }
706
Len Brown4be44fc2005-08-05 00:44:28 -0400707 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708 /*
709 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700710 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700712 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800713 pr->power.state->latency >
714 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 if (cx->demotion.state)
716 next_state = cx->demotion.state;
717 }
718
719 /*
720 * New Cx State?
721 * -------------
722 * If we're going to start using a new Cx state we must clean up
723 * from the previous and prepare to use the new.
724 */
725 if (next_state != pr->power.state)
726 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
Len Brown4be44fc2005-08-05 00:44:28 -0400729static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730{
731 unsigned int i;
732 unsigned int state_is_set = 0;
733 struct acpi_processor_cx *lower = NULL;
734 struct acpi_processor_cx *higher = NULL;
735 struct acpi_processor_cx *cx;
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400739 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 /*
742 * This function sets the default Cx state policy (OS idle handler).
743 * Our scheme is to promote quickly to C2 but more conservatively
744 * to C3. We're favoring C2 for its characteristics of low latency
745 * (quick response), good power savings, and ability to allow bus
746 * mastering activity. Note that the Cx state policy is completely
747 * customizable and can be altered dynamically.
748 */
749
750 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400751 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 cx = &pr->power.states[i];
753 if (!cx->valid)
754 continue;
755
756 if (!state_is_set)
757 pr->power.state = cx;
758 state_is_set++;
759 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400760 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400766 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 cx = &pr->power.states[i];
768 if (!cx->valid)
769 continue;
770
771 if (lower) {
772 cx->demotion.state = lower;
773 cx->demotion.threshold.ticks = cx->latency_ticks;
774 cx->demotion.threshold.count = 1;
775 if (cx->type == ACPI_STATE_C3)
776 cx->demotion.threshold.bm = bm_history;
777 }
778
779 lower = cx;
780 }
781
782 /* promotion */
783 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
784 cx = &pr->power.states[i];
785 if (!cx->valid)
786 continue;
787
788 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400789 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 cx->promotion.threshold.ticks = cx->latency_ticks;
791 if (cx->type >= ACPI_STATE_C2)
792 cx->promotion.threshold.count = 4;
793 else
794 cx->promotion.threshold.count = 10;
795 if (higher->type == ACPI_STATE_C3)
796 cx->promotion.threshold.bm = bm_history;
797 }
798
799 higher = cx;
800 }
801
Patrick Mocheld550d982006-06-27 00:41:40 -0400802 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
Len Brown4f86d3a2007-10-03 18:58:00 -0400804#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Len Brown4be44fc2005-08-05 00:44:28 -0400806static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
809 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400810 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
817 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
818
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400819#ifndef CONFIG_HOTPLUG_CPU
820 /*
821 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400822 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400823 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300824 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300825 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400826 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400827#endif
828
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 /* determine C2 and C3 address from pblk */
830 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
831 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
832
833 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300834 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
835 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
838 "lvl2[0x%08x] lvl3[0x%08x]\n",
839 pr->power.states[ACPI_STATE_C2].address,
840 pr->power.states[ACPI_STATE_C3].address));
841
Patrick Mocheld550d982006-06-27 00:41:40 -0400842 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843}
844
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700845static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500846{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700847 if (!pr->power.states[ACPI_STATE_C1].valid) {
848 /* set the first C-State to C1 */
849 /* all processors need to support C1 */
850 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
851 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400852 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700853 }
854 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500855 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400856 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500857}
858
Len Brown4be44fc2005-08-05 00:44:28 -0400859static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860{
Len Brown4be44fc2005-08-05 00:44:28 -0400861 acpi_status status = 0;
862 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400863 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400864 int i;
865 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
866 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400870 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700872 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
874 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
875 if (ACPI_FAILURE(status)) {
876 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400877 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400878 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200880 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
882 /* There must be at least 2 elements */
883 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400884 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 status = -EFAULT;
886 goto end;
887 }
888
889 count = cst->package.elements[0].integer.value;
890
891 /* Validate number of power states. */
892 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400893 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894 status = -EFAULT;
895 goto end;
896 }
897
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 /* Tell driver that at least _CST is supported. */
899 pr->flags.has_cst = 1;
900
901 for (i = 1; i <= count; i++) {
902 union acpi_object *element;
903 union acpi_object *obj;
904 struct acpi_power_register *reg;
905 struct acpi_processor_cx cx;
906
907 memset(&cx, 0, sizeof(cx));
908
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200909 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910 if (element->type != ACPI_TYPE_PACKAGE)
911 continue;
912
913 if (element->package.count != 4)
914 continue;
915
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200916 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917
918 if (obj->type != ACPI_TYPE_BUFFER)
919 continue;
920
Len Brown4be44fc2005-08-05 00:44:28 -0400921 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400924 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 continue;
926
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200928 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (obj->type != ACPI_TYPE_INTEGER)
930 continue;
931
932 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700933 /*
934 * Some buggy BIOSes won't list C1 in _CST -
935 * Let acpi_processor_get_power_info_default() handle them later
936 */
937 if (i == 1 && cx.type != ACPI_STATE_C1)
938 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700940 cx.address = reg->address;
941 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800943 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700944 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
945 if (acpi_processor_ffh_cstate_probe
946 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800947 cx.entry_method = ACPI_CSTATE_FFH;
948 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700949 /*
950 * C1 is a special case where FIXED_HARDWARE
951 * can be handled in non-MWAIT way as well.
952 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700953 * Otherwise, ignore this info and continue.
954 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800955 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800956 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800957 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700958 continue;
959 }
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800960 if (cx.type == ACPI_STATE_C1 && idle_halt) {
961 /*
962 * In most cases the C1 space_id obtained from
963 * _CST object is FIXED_HARDWARE access mode.
964 * But when the option of idle=halt is added,
965 * the entry_method type should be changed from
966 * CSTATE_FFH to CSTATE_HALT.
967 */
968 cx.entry_method = ACPI_CSTATE_HALT;
969 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
970 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800971 } else {
972 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
973 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700974 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400976 if (cx.type == ACPI_STATE_C1) {
977 cx.valid = 1;
978 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800979
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200980 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 if (obj->type != ACPI_TYPE_INTEGER)
982 continue;
983
984 cx.latency = obj->integer.value;
985
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200986 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (obj->type != ACPI_TYPE_INTEGER)
988 continue;
989
990 cx.power = obj->integer.value;
991
Janosch Machowinskicf824782005-08-20 08:02:00 -0400992 current_count++;
993 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
994
995 /*
996 * We support total ACPI_PROCESSOR_MAX_POWER - 1
997 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
998 */
999 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1000 printk(KERN_WARNING
1001 "Limiting number of power states to max (%d)\n",
1002 ACPI_PROCESSOR_MAX_POWER);
1003 printk(KERN_WARNING
1004 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1005 break;
1006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 }
1008
Len Brown4be44fc2005-08-05 00:44:28 -04001009 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -04001010 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
1012 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -04001013 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001014 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015
Len Brown4be44fc2005-08-05 00:44:28 -04001016 end:
Len Brown02438d82006-06-30 03:19:10 -04001017 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
Patrick Mocheld550d982006-06-27 00:41:40 -04001019 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020}
1021
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1023{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
1025 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
1028 /*
1029 * C2 latency must be less than or equal to 100
1030 * microseconds.
1031 */
1032 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1033 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001034 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 }
1037
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 /*
1039 * Otherwise we've met all of our C2 requirements.
1040 * Normalize the C2 latency to expidite policy
1041 */
1042 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001043
1044#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001046#else
1047 cx->latency_ticks = cx->latency;
1048#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049
Patrick Mocheld550d982006-06-27 00:41:40 -04001050 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051}
1052
Len Brown4be44fc2005-08-05 00:44:28 -04001053static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1054 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001056 static int bm_check_flag;
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
1059 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061
1062 /*
1063 * C3 latency must be less than or equal to 1000
1064 * microseconds.
1065 */
1066 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1067 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001068 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070 }
1071
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 /*
1073 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1074 * DMA transfers are used by any ISA device to avoid livelock.
1075 * Note that we could disable Type-F DMA (as recommended by
1076 * the erratum), but this is known to disrupt certain ISA
1077 * devices thus we take the conservative approach.
1078 */
1079 else if (errata.piix4.fdma) {
1080 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001081 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001082 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083 }
1084
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001085 /* All the logic here assumes flags.bm_check is same across all CPUs */
1086 if (!bm_check_flag) {
1087 /* Determine whether bm_check is needed based on CPU */
1088 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1089 bm_check_flag = pr->flags.bm_check;
1090 } else {
1091 pr->flags.bm_check = bm_check_flag;
1092 }
1093
1094 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001095 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001096 if (pr->flags.has_cst != 1) {
1097 /* bus mastering control is necessary */
1098 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1099 "C3 support requires BM control\n"));
1100 return;
1101 } else {
1102 /* Here we enter C3 without bus mastering */
1103 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1104 "C3 support without BM control\n"));
1105 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001106 }
1107 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001108 /*
1109 * WBINVD should be set in fadt, for C3 state to be
1110 * supported on when bm_check is not required.
1111 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001112 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001113 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001114 "Cache invalidation should work properly"
1115 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001116 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001117 }
Bob Moored8c71b62007-02-02 19:48:21 +03001118 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001119 }
1120
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121 /*
1122 * Otherwise we've met all of our C3 requirements.
1123 * Normalize the C3 latency to expidite policy. Enable
1124 * checking of bus mastering status (bm_check) so we can
1125 * use this in our C3 policy
1126 */
1127 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001128
1129#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001131#else
1132 cx->latency_ticks = cx->latency;
1133#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
Patrick Mocheld550d982006-06-27 00:41:40 -04001135 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static int acpi_processor_power_verify(struct acpi_processor *pr)
1139{
1140 unsigned int i;
1141 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001142
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001143 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001144
Len Brown4be44fc2005-08-05 00:44:28 -04001145 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 struct acpi_processor_cx *cx = &pr->power.states[i];
1147
1148 switch (cx->type) {
1149 case ACPI_STATE_C1:
1150 cx->valid = 1;
1151 break;
1152
1153 case ACPI_STATE_C2:
1154 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001155 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001156 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 break;
1158
1159 case ACPI_STATE_C3:
1160 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001161 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001162 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164 }
1165
1166 if (cx->valid)
1167 working++;
1168 }
1169
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001170 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 return (working);
1173}
1174
Len Brown4be44fc2005-08-05 00:44:28 -04001175static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
1177 unsigned int i;
1178 int result;
1179
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180
1181 /* NOTE: the idle thread may not be running while calling
1182 * this function */
1183
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001184 /* Zero initialize all the C-states info. */
1185 memset(pr->power.states, 0, sizeof(pr->power.states));
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001188 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001189 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001190
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001191 if (result)
1192 return result;
1193
1194 acpi_processor_get_power_info_default(pr);
1195
Janosch Machowinskicf824782005-08-20 08:02:00 -04001196 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197
Len Brown4f86d3a2007-10-03 18:58:00 -04001198#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 /*
1200 * Set Default Policy
1201 * ------------------
1202 * Now that we know which states are supported, set the default
1203 * policy. Note that this policy can be changed dynamically
1204 * (e.g. encourage deeper sleeps to conserve battery life when
1205 * not on AC).
1206 */
1207 result = acpi_processor_set_power_policy(pr);
1208 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001209 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001210#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211
1212 /*
1213 * if one state of type C2 or C3 is available, mark this
1214 * CPU as being "idle manageable"
1215 */
1216 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001217 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001219 if (pr->power.states[i].type >= ACPI_STATE_C2)
1220 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001221 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
Patrick Mocheld550d982006-06-27 00:41:40 -04001224 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225}
1226
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1228{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001229 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001230 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232
1233 if (!pr)
1234 goto end;
1235
1236 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001237 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001238 "bus master activity: %08x\n"
1239 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001240 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001241 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001242 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244 seq_puts(seq, "states:\n");
1245
1246 for (i = 1; i <= pr->power.count; i++) {
1247 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001248 (&pr->power.states[i] ==
1249 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 if (!pr->power.states[i].valid) {
1252 seq_puts(seq, "<not supported>\n");
1253 continue;
1254 }
1255
1256 switch (pr->power.states[i].type) {
1257 case ACPI_STATE_C1:
1258 seq_printf(seq, "type[C1] ");
1259 break;
1260 case ACPI_STATE_C2:
1261 seq_printf(seq, "type[C2] ");
1262 break;
1263 case ACPI_STATE_C3:
1264 seq_printf(seq, "type[C3] ");
1265 break;
1266 default:
1267 seq_printf(seq, "type[--] ");
1268 break;
1269 }
1270
1271 if (pr->power.states[i].promotion.state)
1272 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001273 (pr->power.states[i].promotion.state -
1274 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 else
1276 seq_puts(seq, "promotion[--] ");
1277
1278 if (pr->power.states[i].demotion.state)
1279 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001280 (pr->power.states[i].demotion.state -
1281 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 else
1283 seq_puts(seq, "demotion[--] ");
1284
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001285 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001286 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001287 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001288 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 }
1290
Len Brown4be44fc2005-08-05 00:44:28 -04001291 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001292 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
1295static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1296{
1297 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001298 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
Arjan van de Vend7508032006-07-04 13:06:00 -04001301static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001302 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001303 .open = acpi_processor_power_open_fs,
1304 .read = seq_read,
1305 .llseek = seq_lseek,
1306 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307};
1308
Len Brown4f86d3a2007-10-03 18:58:00 -04001309#ifndef CONFIG_CPU_IDLE
1310
1311int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1312{
1313 int result = 0;
1314
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001315 if (boot_option_idle_override)
1316 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001317
1318 if (!pr)
1319 return -EINVAL;
1320
1321 if (nocst) {
1322 return -ENODEV;
1323 }
1324
1325 if (!pr->flags.power_setup_done)
1326 return -ENODEV;
1327
1328 /* Fall back to the default idle loop */
1329 pm_idle = pm_idle_save;
1330 synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
1331
1332 pr->flags.power = 0;
1333 result = acpi_processor_get_power_info(pr);
1334 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1335 pm_idle = acpi_processor_idle;
1336
1337 return result;
1338}
1339
Andrew Morton1fec74a2006-10-17 00:09:58 -07001340#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001341static void smp_callback(void *v)
1342{
1343 /* we already woke the CPU up, nothing more to do */
1344}
1345
1346/*
1347 * This function gets called when a part of the kernel has a new latency
1348 * requirement. This means we need to get all processors out of their C-state,
1349 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1350 * wakes them all right up.
1351 */
1352static int acpi_processor_latency_notify(struct notifier_block *b,
1353 unsigned long l, void *v)
1354{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001355 smp_call_function(smp_callback, NULL, 1);
Arjan van de Ven5c875792006-09-30 23:27:17 -07001356 return NOTIFY_OK;
1357}
1358
1359static struct notifier_block acpi_processor_latency_notifier = {
1360 .notifier_call = acpi_processor_latency_notify,
1361};
Len Brown4f86d3a2007-10-03 18:58:00 -04001362
Andrew Morton1fec74a2006-10-17 00:09:58 -07001363#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001364
Len Brown4f86d3a2007-10-03 18:58:00 -04001365#else /* CONFIG_CPU_IDLE */
1366
1367/**
1368 * acpi_idle_bm_check - checks if bus master activity was detected
1369 */
1370static int acpi_idle_bm_check(void)
1371{
1372 u32 bm_status = 0;
1373
1374 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1375 if (bm_status)
1376 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1377 /*
1378 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1379 * the true state of bus mastering activity; forcing us to
1380 * manually check the BMIDEA bit of each IDE channel.
1381 */
1382 else if (errata.piix4.bmisx) {
1383 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1384 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1385 bm_status = 1;
1386 }
1387 return bm_status;
1388}
1389
1390/**
1391 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1392 * @pr: the processor
1393 * @target: the new target state
1394 */
1395static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1396 struct acpi_processor_cx *target)
1397{
1398 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1399 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1400 pr->flags.bm_rld_set = 0;
1401 }
1402
1403 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1404 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1405 pr->flags.bm_rld_set = 1;
1406 }
1407}
1408
1409/**
1410 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1411 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001412 *
1413 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001414 */
1415static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1416{
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001417 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001418 /* Call into architectural FFH based C-state */
1419 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001420 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1421 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001422 } else {
1423 int unused;
1424 /* IO port based C-state */
1425 inb(cx->address);
1426 /* Dummy wait op - must do something useless after P_LVL2 read
1427 because chipsets cannot guarantee that STPCLK# signal
1428 gets asserted in time to freeze execution properly. */
1429 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1430 }
1431}
1432
1433/**
1434 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1435 * @dev: the target CPU
1436 * @state: the state data
1437 *
1438 * This is equivalent to the HALT instruction.
1439 */
1440static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1441 struct cpuidle_state *state)
1442{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001443 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001444 struct acpi_processor *pr;
1445 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001446
Mike Travis706546d2008-06-09 16:22:23 -07001447 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001448
1449 if (unlikely(!pr))
1450 return 0;
1451
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001452 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001453
1454 /* Do not access any ACPI IO ports in suspend path */
1455 if (acpi_idle_suspend) {
1456 acpi_safe_halt();
1457 local_irq_enable();
1458 return 0;
1459 }
1460
Len Brown4f86d3a2007-10-03 18:58:00 -04001461 if (pr->flags.bm_check)
1462 acpi_idle_update_bm_rld(pr, cx);
1463
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001464 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001465 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001466 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001467
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001468 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001469 cx->usage++;
1470
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001471 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001472}
1473
1474/**
1475 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1476 * @dev: the target CPU
1477 * @state: the state data
1478 */
1479static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1480 struct cpuidle_state *state)
1481{
1482 struct acpi_processor *pr;
1483 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1484 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001485 int sleep_ticks = 0;
1486
Mike Travis706546d2008-06-09 16:22:23 -07001487 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001488
1489 if (unlikely(!pr))
1490 return 0;
1491
Len Browne1964412007-10-04 01:23:47 -04001492 if (acpi_idle_suspend)
1493 return(acpi_idle_enter_c1(dev, state));
1494
Len Brown4f86d3a2007-10-03 18:58:00 -04001495 local_irq_disable();
1496 current_thread_info()->status &= ~TS_POLLING;
1497 /*
1498 * TS_POLLING-cleared state must be visible before we test
1499 * NEED_RESCHED:
1500 */
1501 smp_mb();
1502
1503 if (unlikely(need_resched())) {
1504 current_thread_info()->status |= TS_POLLING;
1505 local_irq_enable();
1506 return 0;
1507 }
1508
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001509 /*
1510 * Must be done before busmaster disable as we might need to
1511 * access HPET !
1512 */
1513 acpi_state_timer_broadcast(pr, cx, 1);
1514
1515 if (pr->flags.bm_check)
1516 acpi_idle_update_bm_rld(pr, cx);
1517
Len Brown4f86d3a2007-10-03 18:58:00 -04001518 if (cx->type == ACPI_STATE_C3)
1519 ACPI_FLUSH_CPU_CACHE();
1520
1521 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001522 /* Tell the scheduler that we are going deep-idle: */
1523 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001524 acpi_idle_do_entry(cx);
1525 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1526
Pavel Machek61331162008-02-19 11:00:29 +01001527#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001528 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001529 if (tsc_halts_in_c(cx->type))
1530 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001531#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001532 sleep_ticks = ticks_elapsed(t1, t2);
1533
1534 /* Tell the scheduler how much we idled: */
1535 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001536
1537 local_irq_enable();
1538 current_thread_info()->status |= TS_POLLING;
1539
1540 cx->usage++;
1541
1542 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001543 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001544 return ticks_elapsed_in_us(t1, t2);
1545}
1546
1547static int c3_cpu_count;
1548static DEFINE_SPINLOCK(c3_lock);
1549
1550/**
1551 * acpi_idle_enter_bm - enters C3 with proper BM handling
1552 * @dev: the target CPU
1553 * @state: the state data
1554 *
1555 * If BM is detected, the deepest non-C3 idle state is entered instead.
1556 */
1557static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1558 struct cpuidle_state *state)
1559{
1560 struct acpi_processor *pr;
1561 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1562 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001563 int sleep_ticks = 0;
1564
Mike Travis706546d2008-06-09 16:22:23 -07001565 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001566
1567 if (unlikely(!pr))
1568 return 0;
1569
Len Browne1964412007-10-04 01:23:47 -04001570 if (acpi_idle_suspend)
1571 return(acpi_idle_enter_c1(dev, state));
1572
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001573 if (acpi_idle_bm_check()) {
1574 if (dev->safe_state) {
1575 return dev->safe_state->enter(dev, dev->safe_state);
1576 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001577 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001578 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001579 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001580 return 0;
1581 }
1582 }
1583
Len Brown4f86d3a2007-10-03 18:58:00 -04001584 local_irq_disable();
1585 current_thread_info()->status &= ~TS_POLLING;
1586 /*
1587 * TS_POLLING-cleared state must be visible before we test
1588 * NEED_RESCHED:
1589 */
1590 smp_mb();
1591
1592 if (unlikely(need_resched())) {
1593 current_thread_info()->status |= TS_POLLING;
1594 local_irq_enable();
1595 return 0;
1596 }
1597
Venki Pallipadi996520c2008-03-24 14:24:10 -07001598 acpi_unlazy_tlb(smp_processor_id());
1599
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001600 /* Tell the scheduler that we are going deep-idle: */
1601 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001602 /*
1603 * Must be done before busmaster disable as we might need to
1604 * access HPET !
1605 */
1606 acpi_state_timer_broadcast(pr, cx, 1);
1607
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001608 acpi_idle_update_bm_rld(pr, cx);
Len Brown4f86d3a2007-10-03 18:58:00 -04001609
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001610 /*
1611 * disable bus master
1612 * bm_check implies we need ARB_DIS
1613 * !bm_check implies we need cache flush
1614 * bm_control implies whether we can do ARB_DIS
1615 *
1616 * That leaves a case where bm_check is set and bm_control is
1617 * not set. In that case we cannot do much, we enter C3
1618 * without doing anything.
1619 */
1620 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001621 spin_lock(&c3_lock);
1622 c3_cpu_count++;
1623 /* Disable bus master arbitration when all CPUs are in C3 */
1624 if (c3_cpu_count == num_online_cpus())
1625 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1626 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001627 } else if (!pr->flags.bm_check) {
1628 ACPI_FLUSH_CPU_CACHE();
1629 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001630
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001631 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1632 acpi_idle_do_entry(cx);
1633 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001634
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001635 /* Re-enable bus master arbitration */
1636 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001637 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001638 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001639 c3_cpu_count--;
1640 spin_unlock(&c3_lock);
1641 }
1642
Pavel Machek61331162008-02-19 11:00:29 +01001643#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001644 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001645 if (tsc_halts_in_c(ACPI_STATE_C3))
1646 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001647#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001648 sleep_ticks = ticks_elapsed(t1, t2);
1649 /* Tell the scheduler how much we idled: */
1650 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001651
1652 local_irq_enable();
1653 current_thread_info()->status |= TS_POLLING;
1654
1655 cx->usage++;
1656
1657 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001658 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001659 return ticks_elapsed_in_us(t1, t2);
1660}
1661
1662struct cpuidle_driver acpi_idle_driver = {
1663 .name = "acpi_idle",
1664 .owner = THIS_MODULE,
1665};
1666
1667/**
1668 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1669 * @pr: the ACPI processor
1670 */
1671static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1672{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001673 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001674 struct acpi_processor_cx *cx;
1675 struct cpuidle_state *state;
1676 struct cpuidle_device *dev = &pr->power.dev;
1677
1678 if (!pr->flags.power_setup_done)
1679 return -EINVAL;
1680
1681 if (pr->flags.power == 0) {
1682 return -EINVAL;
1683 }
1684
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001685 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001686 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1687 dev->states[i].name[0] = '\0';
1688 dev->states[i].desc[0] = '\0';
1689 }
1690
Len Brown4f86d3a2007-10-03 18:58:00 -04001691 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1692 cx = &pr->power.states[i];
1693 state = &dev->states[count];
1694
1695 if (!cx->valid)
1696 continue;
1697
1698#ifdef CONFIG_HOTPLUG_CPU
1699 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1700 !pr->flags.has_cst &&
1701 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1702 continue;
1703#endif
1704 cpuidle_set_statedata(state, cx);
1705
1706 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001707 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001708 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001709 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001710 state->power_usage = cx->power;
1711
1712 state->flags = 0;
1713 switch (cx->type) {
1714 case ACPI_STATE_C1:
1715 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001716 if (cx->entry_method == ACPI_CSTATE_FFH)
1717 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1718
Len Brown4f86d3a2007-10-03 18:58:00 -04001719 state->enter = acpi_idle_enter_c1;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001720 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001721 break;
1722
1723 case ACPI_STATE_C2:
1724 state->flags |= CPUIDLE_FLAG_BALANCED;
1725 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1726 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001727 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001728 break;
1729
1730 case ACPI_STATE_C3:
1731 state->flags |= CPUIDLE_FLAG_DEEP;
1732 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1733 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1734 state->enter = pr->flags.bm_check ?
1735 acpi_idle_enter_bm :
1736 acpi_idle_enter_simple;
1737 break;
1738 }
1739
1740 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001741 if (count == CPUIDLE_STATE_MAX)
1742 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001743 }
1744
1745 dev->state_count = count;
1746
1747 if (!count)
1748 return -EINVAL;
1749
Len Brown4f86d3a2007-10-03 18:58:00 -04001750 return 0;
1751}
1752
1753int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1754{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001755 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001756
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001757 if (boot_option_idle_override)
1758 return 0;
1759
Len Brown4f86d3a2007-10-03 18:58:00 -04001760 if (!pr)
1761 return -EINVAL;
1762
1763 if (nocst) {
1764 return -ENODEV;
1765 }
1766
1767 if (!pr->flags.power_setup_done)
1768 return -ENODEV;
1769
1770 cpuidle_pause_and_lock();
1771 cpuidle_disable_device(&pr->power.dev);
1772 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001773 if (pr->flags.power) {
1774 acpi_processor_setup_cpuidle(pr);
1775 ret = cpuidle_enable_device(&pr->power.dev);
1776 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001777 cpuidle_resume_and_unlock();
1778
1779 return ret;
1780}
1781
1782#endif /* CONFIG_CPU_IDLE */
1783
Pierre Ossman7af8b662006-10-10 14:20:31 -07001784int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001785 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786{
Len Brown4be44fc2005-08-05 00:44:28 -04001787 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001788 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001789 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 unsigned int i;
1791
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001792 if (boot_option_idle_override)
1793 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794
1795 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001796 if (idle_halt) {
1797 /*
1798 * When the boot option of "idle=halt" is added, halt
1799 * is used for CPU IDLE.
1800 * In such case C2/C3 is meaningless. So the max_cstate
1801 * is set to one.
1802 */
1803 max_cstate = 1;
1804 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001806 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001808 printk(KERN_NOTICE
1809 "ACPI: processor limited to max C-state %d\n",
1810 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001812#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1813 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1814 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001815#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
1817
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001818 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001819 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001820
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001821 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001822 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001823 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001825 ACPI_EXCEPTION((AE_INFO, status,
1826 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 }
1828 }
1829
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001831 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
1833 /*
1834 * Install the idle handler if processor power management is supported.
1835 * Note that we use previously set idle handler will be used on
1836 * platforms that only support C1.
1837 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001838 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001839#ifdef CONFIG_CPU_IDLE
1840 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001841 if (cpuidle_register_device(&pr->power.dev))
1842 return -EIO;
1843#endif
1844
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1846 for (i = 1; i <= pr->power.count; i++)
1847 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001848 printk(" C%d[C%d]", i,
1849 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 printk(")\n");
1851
Len Brown4f86d3a2007-10-03 18:58:00 -04001852#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 if (pr->id == 0) {
1854 pm_idle_save = pm_idle;
1855 pm_idle = acpi_processor_idle;
1856 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001857#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 }
1859
1860 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001861 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1862 S_IRUGO, acpi_device_dir(device),
1863 &acpi_processor_power_fops,
1864 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001866 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001867 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868}
1869
Len Brown4be44fc2005-08-05 00:44:28 -04001870int acpi_processor_power_exit(struct acpi_processor *pr,
1871 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001873 if (boot_option_idle_override)
1874 return 0;
1875
Len Brown4f86d3a2007-10-03 18:58:00 -04001876#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001877 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 pr->flags.power_setup_done = 0;
1880
1881 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001882 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1883 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884
Len Brown4f86d3a2007-10-03 18:58:00 -04001885#ifndef CONFIG_CPU_IDLE
1886
Linus Torvalds1da177e2005-04-16 15:20:36 -07001887 /* Unregister the idle handler when processor #0 is removed. */
1888 if (pr->id == 0) {
1889 pm_idle = pm_idle_save;
1890
1891 /*
1892 * We are about to unload the current idle thread pm callback
1893 * (pm_idle), Wait for all processors to update cached/local
1894 * copies of pm_idle before proceeding.
1895 */
1896 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001897#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001898 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1899 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001900#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001901 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
Patrick Mocheld550d982006-06-27 00:41:40 -04001904 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001905}