blob: 283c08f5f4d4390bfd12a7bfd820787a1344dbde [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{
Steven Rostedtdcf30992008-07-25 18:00:42 -0400275 /* Don't trace irqs off for idle */
276 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800277 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700278 /* Call into architectural FFH based C-state */
279 acpi_processor_ffh_cstate_enter(cstate);
280 } else {
281 int unused;
282 /* IO port based C-state */
283 inb(cstate->address);
284 /* Dummy wait op - must do something useless after P_LVL2 read
285 because chipsets cannot guarantee that STPCLK# signal
286 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300287 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700288 }
Steven Rostedtdcf30992008-07-25 18:00:42 -0400289 start_critical_timings();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700290}
Len Brown4f86d3a2007-10-03 18:58:00 -0400291#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700292
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800293#ifdef ARCH_APICTIMER_STOPS_ON_C3
294
295/*
296 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700297 * This seems to be a common problem on AMD boxen, but other vendors
298 * are affected too. We pick the most conservative approach: we assume
299 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800300 */
301static void acpi_timer_check_state(int state, struct acpi_processor *pr,
302 struct acpi_processor_cx *cx)
303{
304 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100305 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800306
307 /*
308 * Check, if one of the previous states already marked the lapic
309 * unstable
310 */
311 if (pwr->timer_broadcast_on_state < state)
312 return;
313
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100314 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700315 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800316}
317
318static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
319{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800320 unsigned long reason;
321
322 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
323 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
324
325 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800326}
327
328/* Power(C) State timer broadcast control */
329static void acpi_state_timer_broadcast(struct acpi_processor *pr,
330 struct acpi_processor_cx *cx,
331 int broadcast)
332{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800333 int state = cx - pr->power.states;
334
335 if (state >= pr->power.timer_broadcast_on_state) {
336 unsigned long reason;
337
338 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
339 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
340 clockevents_notify(reason, &pr->id);
341 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800342}
343
344#else
345
346static void acpi_timer_check_state(int state, struct acpi_processor *pr,
347 struct acpi_processor_cx *cstate) { }
348static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800349static void acpi_state_timer_broadcast(struct acpi_processor *pr,
350 struct acpi_processor_cx *cx,
351 int broadcast)
352{
353}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800354
355#endif
356
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000357/*
358 * Suspend / resume control
359 */
360static int acpi_idle_suspend;
361
362int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
363{
364 acpi_idle_suspend = 1;
365 return 0;
366}
367
368int acpi_processor_resume(struct acpi_device * device)
369{
370 acpi_idle_suspend = 0;
371 return 0;
372}
373
Pavel Machek61331162008-02-19 11:00:29 +0100374#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100375static int tsc_halts_in_c(int state)
376{
377 switch (boot_cpu_data.x86_vendor) {
378 case X86_VENDOR_AMD:
379 /*
380 * AMD Fam10h TSC will tick in all
381 * C/P/S0/S1 states when this bit is set.
382 */
383 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
384 return 0;
385 /*FALL THROUGH*/
386 case X86_VENDOR_INTEL:
387 /* Several cases known where TSC halts in C2 too */
388 default:
389 return state > ACPI_STATE_C1;
390 }
391}
392#endif
393
Len Brown4f86d3a2007-10-03 18:58:00 -0400394#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400395static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396{
Len Brown4be44fc2005-08-05 00:44:28 -0400397 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398 struct acpi_processor_cx *cx = NULL;
399 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400400 int sleep_ticks = 0;
401 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 /*
404 * Interrupts must be disabled during bus mastering calculations and
405 * for C2/C3 transitions.
406 */
407 local_irq_disable();
408
Mike Travis706546d2008-06-09 16:22:23 -0700409 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400410 if (!pr) {
411 local_irq_enable();
412 return;
413 }
414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /*
416 * Check whether we truly need to go idle, or should
417 * reschedule:
418 */
419 if (unlikely(need_resched())) {
420 local_irq_enable();
421 return;
422 }
423
424 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000425 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200426 if (pm_idle_save) {
427 pm_idle_save(); /* enables IRQs */
428 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800429 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700430 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200431 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700432
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800433 return;
434 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
436 /*
437 * Check BM Activity
438 * -----------------
439 * Check for bus mastering activity (if required), record, and check
440 * for demotion.
441 */
442 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400443 u32 bm_status = 0;
444 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400446 if (diff > 31)
447 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400449 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Bob Moored8c71b62007-02-02 19:48:21 +0300451 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400453 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300454 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 }
456 /*
457 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
458 * the true state of bus mastering activity; forcing us to
459 * manually check the BMIDEA bit of each IDE channel.
460 */
461 else if (errata.piix4.bmisx) {
462 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400463 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400464 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 }
466
467 pr->power.bm_check_timestamp = jiffies;
468
469 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400470 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 * to avoid a faulty transition. Note that the processor
472 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400473 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 *
475 * TBD: A better policy might be to fallback to the demotion
476 * state (use it for this quantum only) istead of
477 * demoting -- and rely on duration as our sole demotion
478 * qualification. This may, however, introduce DMA
479 * issues (e.g. floppy DMA transfer overrun/underrun).
480 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400481 if ((pr->power.bm_activity & 0x1) &&
482 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 local_irq_enable();
484 next_state = cx->demotion.state;
485 goto end;
486 }
487 }
488
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400489#ifdef CONFIG_HOTPLUG_CPU
490 /*
491 * Check for P_LVL2_UP flag before entering C2 and above on
492 * an SMP system. We do it here instead of doing it at _CST/P_LVL
493 * detection phase, to work cleanly with logical CPU hotplug.
494 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400495 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300496 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500497 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400498#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500499
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * Sleep:
502 * ------
503 * Invoke the current Cx state to put the processor to sleep.
504 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100505 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200506 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800507 /*
508 * TS_POLLING-cleared state must be visible before we
509 * test NEED_RESCHED:
510 */
511 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100512 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200513 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800514 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100515 return;
516 }
517 }
518
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 switch (cx->type) {
520
521 case ACPI_STATE_C1:
522 /*
523 * Invoke C1.
524 * Use the appropriate idle routine, the one that would
525 * be used without acpi C-states.
526 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200527 if (pm_idle_save) {
528 pm_idle_save(); /* enables IRQs */
529 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800530 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200531 local_irq_enable();
532 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800533
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400535 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 * go to an ISR rather than here. Need to instrument
537 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200538 *
539 * Note: the TSC better not stop in C1, sched_clock() will
540 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 */
542 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700543
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 break;
545
546 case ACPI_STATE_C2:
547 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300548 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200549 /* Tell the scheduler that we are going deep-idle: */
550 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800552 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700553 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300555 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700556
Pavel Machek61331162008-02-19 11:00:29 +0100557#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700558 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100559 if (tsc_halts_in_c(ACPI_STATE_C2))
560 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700561#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200562 /* Compute time (ticks) that we were actually asleep */
563 sleep_ticks = ticks_elapsed(t1, t2);
564
565 /* Tell the scheduler how much we idled: */
566 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /* Re-enable interrupts */
569 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200570 /* Do not account our idle-switching overhead: */
571 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
572
Andi Kleen495ab9c2006-06-26 13:59:11 +0200573 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800574 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 break;
576
577 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100578 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400579 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100580 * Must be done before busmaster disable as we might
581 * need to access HPET !
582 */
583 acpi_state_timer_broadcast(pr, cx, 1);
584 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400585 * disable bus master
586 * bm_check implies we need ARB_DIS
587 * !bm_check implies we need cache flush
588 * bm_control implies whether we can do ARB_DIS
589 *
590 * That leaves a case where bm_check is set and bm_control is
591 * not set. In that case we cannot do much, we enter C3
592 * without doing anything.
593 */
594 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400595 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400596 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400597 /*
598 * All CPUs are trying to go to C3
599 * Disable bus master arbitration
600 */
Bob Moored8c71b62007-02-02 19:48:21 +0300601 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400602 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400603 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400604 /* SMP with no shared cache... Invalidate cache */
605 ACPI_FLUSH_CPU_CACHE();
606 }
Len Brown4be44fc2005-08-05 00:44:28 -0400607
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300609 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200611 /* Tell the scheduler that we are going deep-idle: */
612 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700613 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300615 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400616 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400617 /* Enable bus master arbitration */
618 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300619 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400620 }
621
Pavel Machek61331162008-02-19 11:00:29 +0100622#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700623 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100624 if (tsc_halts_in_c(ACPI_STATE_C3))
625 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700626#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200627 /* Compute time (ticks) that we were actually asleep */
628 sleep_ticks = ticks_elapsed(t1, t2);
629 /* Tell the scheduler how much we idled: */
630 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /* Re-enable interrupts */
633 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200634 /* Do not account our idle-switching overhead: */
635 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
636
Andi Kleen495ab9c2006-06-26 13:59:11 +0200637 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800638 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 break;
640
641 default:
642 local_irq_enable();
643 return;
644 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400645 cx->usage++;
646 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
647 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 next_state = pr->power.state;
650
David Shaohua Li1e483962005-12-01 17:00:00 -0500651#ifdef CONFIG_HOTPLUG_CPU
652 /* Don't do promotion/demotion */
653 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300654 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500655 next_state = cx;
656 goto end;
657 }
658#endif
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 /*
661 * Promotion?
662 * ----------
663 * Track the number of longs (time asleep is greater than threshold)
664 * and promote when the count threshold is reached. Note that bus
665 * mastering activity may prevent promotions.
666 * Do not promote above max_cstate.
667 */
668 if (cx->promotion.state &&
669 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700670 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800671 cx->promotion.state->latency <=
672 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400674 cx->demotion.count = 0;
675 if (cx->promotion.count >=
676 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400678 if (!
679 (pr->power.bm_activity & cx->
680 promotion.threshold.bm)) {
681 next_state =
682 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 goto end;
684 }
Len Brown4be44fc2005-08-05 00:44:28 -0400685 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 next_state = cx->promotion.state;
687 goto end;
688 }
689 }
690 }
691 }
692
693 /*
694 * Demotion?
695 * ---------
696 * Track the number of shorts (time asleep is less than time threshold)
697 * and demote when the usage threshold is reached.
698 */
699 if (cx->demotion.state) {
700 if (sleep_ticks < cx->demotion.threshold.ticks) {
701 cx->demotion.count++;
702 cx->promotion.count = 0;
703 if (cx->demotion.count >= cx->demotion.threshold.count) {
704 next_state = cx->demotion.state;
705 goto end;
706 }
707 }
708 }
709
Len Brown4be44fc2005-08-05 00:44:28 -0400710 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /*
712 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700713 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700715 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800716 pr->power.state->latency >
717 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 if (cx->demotion.state)
719 next_state = cx->demotion.state;
720 }
721
722 /*
723 * New Cx State?
724 * -------------
725 * If we're going to start using a new Cx state we must clean up
726 * from the previous and prepare to use the new.
727 */
728 if (next_state != pr->power.state)
729 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730}
731
Len Brown4be44fc2005-08-05 00:44:28 -0400732static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733{
734 unsigned int i;
735 unsigned int state_is_set = 0;
736 struct acpi_processor_cx *lower = NULL;
737 struct acpi_processor_cx *higher = NULL;
738 struct acpi_processor_cx *cx;
739
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740
741 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400742 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 /*
745 * This function sets the default Cx state policy (OS idle handler).
746 * Our scheme is to promote quickly to C2 but more conservatively
747 * to C3. We're favoring C2 for its characteristics of low latency
748 * (quick response), good power savings, and ability to allow bus
749 * mastering activity. Note that the Cx state policy is completely
750 * customizable and can be altered dynamically.
751 */
752
753 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400754 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 cx = &pr->power.states[i];
756 if (!cx->valid)
757 continue;
758
759 if (!state_is_set)
760 pr->power.state = cx;
761 state_is_set++;
762 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400769 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 cx = &pr->power.states[i];
771 if (!cx->valid)
772 continue;
773
774 if (lower) {
775 cx->demotion.state = lower;
776 cx->demotion.threshold.ticks = cx->latency_ticks;
777 cx->demotion.threshold.count = 1;
778 if (cx->type == ACPI_STATE_C3)
779 cx->demotion.threshold.bm = bm_history;
780 }
781
782 lower = cx;
783 }
784
785 /* promotion */
786 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
787 cx = &pr->power.states[i];
788 if (!cx->valid)
789 continue;
790
791 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400792 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 cx->promotion.threshold.ticks = cx->latency_ticks;
794 if (cx->type >= ACPI_STATE_C2)
795 cx->promotion.threshold.count = 4;
796 else
797 cx->promotion.threshold.count = 10;
798 if (higher->type == ACPI_STATE_C3)
799 cx->promotion.threshold.bm = bm_history;
800 }
801
802 higher = cx;
803 }
804
Patrick Mocheld550d982006-06-27 00:41:40 -0400805 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806}
Len Brown4f86d3a2007-10-03 18:58:00 -0400807#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808
Len Brown4be44fc2005-08-05 00:44:28 -0400809static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400813 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400816 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
820 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
821
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400822#ifndef CONFIG_HOTPLUG_CPU
823 /*
824 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400825 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400826 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300827 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300828 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400829 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400830#endif
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 /* determine C2 and C3 address from pblk */
833 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
834 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
835
836 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300837 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
838 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839
840 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
841 "lvl2[0x%08x] lvl3[0x%08x]\n",
842 pr->power.states[ACPI_STATE_C2].address,
843 pr->power.states[ACPI_STATE_C3].address));
844
Patrick Mocheld550d982006-06-27 00:41:40 -0400845 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846}
847
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700848static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500849{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700850 if (!pr->power.states[ACPI_STATE_C1].valid) {
851 /* set the first C-State to C1 */
852 /* all processors need to support C1 */
853 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
854 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400855 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700856 }
857 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500858 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400859 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500860}
861
Len Brown4be44fc2005-08-05 00:44:28 -0400862static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863{
Len Brown4be44fc2005-08-05 00:44:28 -0400864 acpi_status status = 0;
865 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400866 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400867 int i;
868 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
869 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400873 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700875 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
878 if (ACPI_FAILURE(status)) {
879 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400880 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400881 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200883 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
885 /* There must be at least 2 elements */
886 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400887 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700888 status = -EFAULT;
889 goto end;
890 }
891
892 count = cst->package.elements[0].integer.value;
893
894 /* Validate number of power states. */
895 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400896 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 status = -EFAULT;
898 goto end;
899 }
900
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 /* Tell driver that at least _CST is supported. */
902 pr->flags.has_cst = 1;
903
904 for (i = 1; i <= count; i++) {
905 union acpi_object *element;
906 union acpi_object *obj;
907 struct acpi_power_register *reg;
908 struct acpi_processor_cx cx;
909
910 memset(&cx, 0, sizeof(cx));
911
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200912 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913 if (element->type != ACPI_TYPE_PACKAGE)
914 continue;
915
916 if (element->package.count != 4)
917 continue;
918
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200919 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920
921 if (obj->type != ACPI_TYPE_BUFFER)
922 continue;
923
Len Brown4be44fc2005-08-05 00:44:28 -0400924 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925
926 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400927 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928 continue;
929
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200931 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 if (obj->type != ACPI_TYPE_INTEGER)
933 continue;
934
935 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700936 /*
937 * Some buggy BIOSes won't list C1 in _CST -
938 * Let acpi_processor_get_power_info_default() handle them later
939 */
940 if (i == 1 && cx.type != ACPI_STATE_C1)
941 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700943 cx.address = reg->address;
944 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800946 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700947 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
948 if (acpi_processor_ffh_cstate_probe
949 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800950 cx.entry_method = ACPI_CSTATE_FFH;
951 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700952 /*
953 * C1 is a special case where FIXED_HARDWARE
954 * can be handled in non-MWAIT way as well.
955 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700956 * Otherwise, ignore this info and continue.
957 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800958 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800959 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800960 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700961 continue;
962 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800963 if (cx.type == ACPI_STATE_C1 &&
964 (idle_halt || idle_nomwait)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800965 /*
966 * In most cases the C1 space_id obtained from
967 * _CST object is FIXED_HARDWARE access mode.
968 * But when the option of idle=halt is added,
969 * the entry_method type should be changed from
970 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800971 * When the option of idle=nomwait is added,
972 * the C1 entry_method type should be
973 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800974 */
975 cx.entry_method = ACPI_CSTATE_HALT;
976 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
977 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800978 } else {
979 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
980 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400983 if (cx.type == ACPI_STATE_C1) {
984 cx.valid = 1;
985 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800986
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200987 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 if (obj->type != ACPI_TYPE_INTEGER)
989 continue;
990
991 cx.latency = obj->integer.value;
992
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200993 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 if (obj->type != ACPI_TYPE_INTEGER)
995 continue;
996
997 cx.power = obj->integer.value;
998
Janosch Machowinskicf824782005-08-20 08:02:00 -0400999 current_count++;
1000 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
1001
1002 /*
1003 * We support total ACPI_PROCESSOR_MAX_POWER - 1
1004 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
1005 */
1006 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1007 printk(KERN_WARNING
1008 "Limiting number of power states to max (%d)\n",
1009 ACPI_PROCESSOR_MAX_POWER);
1010 printk(KERN_WARNING
1011 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1012 break;
1013 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001014 }
1015
Len Brown4be44fc2005-08-05 00:44:28 -04001016 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -04001017 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018
1019 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -04001020 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001021 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022
Len Brown4be44fc2005-08-05 00:44:28 -04001023 end:
Len Brown02438d82006-06-30 03:19:10 -04001024 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Patrick Mocheld550d982006-06-27 00:41:40 -04001026 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1030{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001033 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 /*
1036 * C2 latency must be less than or equal to 100
1037 * microseconds.
1038 */
1039 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1040 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001041 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001042 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 }
1044
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /*
1046 * Otherwise we've met all of our C2 requirements.
1047 * Normalize the C2 latency to expidite policy
1048 */
1049 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001050
1051#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001053#else
1054 cx->latency_ticks = cx->latency;
1055#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Patrick Mocheld550d982006-06-27 00:41:40 -04001057 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058}
1059
Len Brown4be44fc2005-08-05 00:44:28 -04001060static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1061 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001063 static int bm_check_flag;
1064
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065
1066 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001067 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 /*
1070 * C3 latency must be less than or equal to 1000
1071 * microseconds.
1072 */
1073 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1074 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001075 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001076 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001077 }
1078
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 /*
1080 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1081 * DMA transfers are used by any ISA device to avoid livelock.
1082 * Note that we could disable Type-F DMA (as recommended by
1083 * the erratum), but this is known to disrupt certain ISA
1084 * devices thus we take the conservative approach.
1085 */
1086 else if (errata.piix4.fdma) {
1087 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001088 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001089 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 }
1091
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001092 /* All the logic here assumes flags.bm_check is same across all CPUs */
1093 if (!bm_check_flag) {
1094 /* Determine whether bm_check is needed based on CPU */
1095 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1096 bm_check_flag = pr->flags.bm_check;
1097 } else {
1098 pr->flags.bm_check = bm_check_flag;
1099 }
1100
1101 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001102 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001103 if (pr->flags.has_cst != 1) {
1104 /* bus mastering control is necessary */
1105 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1106 "C3 support requires BM control\n"));
1107 return;
1108 } else {
1109 /* Here we enter C3 without bus mastering */
1110 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1111 "C3 support without BM control\n"));
1112 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001113 }
1114 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001115 /*
1116 * WBINVD should be set in fadt, for C3 state to be
1117 * supported on when bm_check is not required.
1118 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001119 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001120 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001121 "Cache invalidation should work properly"
1122 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001123 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001124 }
Bob Moored8c71b62007-02-02 19:48:21 +03001125 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001126 }
1127
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128 /*
1129 * Otherwise we've met all of our C3 requirements.
1130 * Normalize the C3 latency to expidite policy. Enable
1131 * checking of bus mastering status (bm_check) so we can
1132 * use this in our C3 policy
1133 */
1134 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001135
1136#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001137 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001138#else
1139 cx->latency_ticks = cx->latency;
1140#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143}
1144
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145static int acpi_processor_power_verify(struct acpi_processor *pr)
1146{
1147 unsigned int i;
1148 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001149
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001150 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001151
Len Brown4be44fc2005-08-05 00:44:28 -04001152 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001153 struct acpi_processor_cx *cx = &pr->power.states[i];
1154
1155 switch (cx->type) {
1156 case ACPI_STATE_C1:
1157 cx->valid = 1;
1158 break;
1159
1160 case ACPI_STATE_C2:
1161 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001162 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001163 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 break;
1165
1166 case ACPI_STATE_C3:
1167 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001168 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001169 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 break;
1171 }
1172
1173 if (cx->valid)
1174 working++;
1175 }
1176
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001177 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001178
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179 return (working);
1180}
1181
Len Brown4be44fc2005-08-05 00:44:28 -04001182static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183{
1184 unsigned int i;
1185 int result;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188 /* NOTE: the idle thread may not be running while calling
1189 * this function */
1190
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001191 /* Zero initialize all the C-states info. */
1192 memset(pr->power.states, 0, sizeof(pr->power.states));
1193
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001195 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001196 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001197
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001198 if (result)
1199 return result;
1200
1201 acpi_processor_get_power_info_default(pr);
1202
Janosch Machowinskicf824782005-08-20 08:02:00 -04001203 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204
Len Brown4f86d3a2007-10-03 18:58:00 -04001205#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 /*
1207 * Set Default Policy
1208 * ------------------
1209 * Now that we know which states are supported, set the default
1210 * policy. Note that this policy can be changed dynamically
1211 * (e.g. encourage deeper sleeps to conserve battery life when
1212 * not on AC).
1213 */
1214 result = acpi_processor_set_power_policy(pr);
1215 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001216 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001217#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218
1219 /*
1220 * if one state of type C2 or C3 is available, mark this
1221 * CPU as being "idle manageable"
1222 */
1223 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001224 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001226 if (pr->power.states[i].type >= ACPI_STATE_C2)
1227 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001229 }
1230
Patrick Mocheld550d982006-06-27 00:41:40 -04001231 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1235{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001236 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001237 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239
1240 if (!pr)
1241 goto end;
1242
1243 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001244 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001245 "bus master activity: %08x\n"
1246 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001247 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001248 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001249 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001250
1251 seq_puts(seq, "states:\n");
1252
1253 for (i = 1; i <= pr->power.count; i++) {
1254 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001255 (&pr->power.states[i] ==
1256 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257
1258 if (!pr->power.states[i].valid) {
1259 seq_puts(seq, "<not supported>\n");
1260 continue;
1261 }
1262
1263 switch (pr->power.states[i].type) {
1264 case ACPI_STATE_C1:
1265 seq_printf(seq, "type[C1] ");
1266 break;
1267 case ACPI_STATE_C2:
1268 seq_printf(seq, "type[C2] ");
1269 break;
1270 case ACPI_STATE_C3:
1271 seq_printf(seq, "type[C3] ");
1272 break;
1273 default:
1274 seq_printf(seq, "type[--] ");
1275 break;
1276 }
1277
1278 if (pr->power.states[i].promotion.state)
1279 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001280 (pr->power.states[i].promotion.state -
1281 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 else
1283 seq_puts(seq, "promotion[--] ");
1284
1285 if (pr->power.states[i].demotion.state)
1286 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001287 (pr->power.states[i].demotion.state -
1288 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289 else
1290 seq_puts(seq, "demotion[--] ");
1291
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001292 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001293 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001294 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001295 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296 }
1297
Len Brown4be44fc2005-08-05 00:44:28 -04001298 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001299 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300}
1301
1302static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1303{
1304 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001305 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306}
1307
Arjan van de Vend7508032006-07-04 13:06:00 -04001308static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001309 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001310 .open = acpi_processor_power_open_fs,
1311 .read = seq_read,
1312 .llseek = seq_lseek,
1313 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314};
1315
Len Brown4f86d3a2007-10-03 18:58:00 -04001316#ifndef CONFIG_CPU_IDLE
1317
1318int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1319{
1320 int result = 0;
1321
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001322 if (boot_option_idle_override)
1323 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001324
1325 if (!pr)
1326 return -EINVAL;
1327
1328 if (nocst) {
1329 return -ENODEV;
1330 }
1331
1332 if (!pr->flags.power_setup_done)
1333 return -ENODEV;
1334
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001335 /*
1336 * Fall back to the default idle loop, when pm_idle_save had
1337 * been initialized.
1338 */
1339 if (pm_idle_save) {
1340 pm_idle = pm_idle_save;
1341 /* Relies on interrupts forcing exit from idle. */
1342 synchronize_sched();
1343 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001344
1345 pr->flags.power = 0;
1346 result = acpi_processor_get_power_info(pr);
1347 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1348 pm_idle = acpi_processor_idle;
1349
1350 return result;
1351}
1352
Andrew Morton1fec74a2006-10-17 00:09:58 -07001353#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001354static void smp_callback(void *v)
1355{
1356 /* we already woke the CPU up, nothing more to do */
1357}
1358
1359/*
1360 * This function gets called when a part of the kernel has a new latency
1361 * requirement. This means we need to get all processors out of their C-state,
1362 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1363 * wakes them all right up.
1364 */
1365static int acpi_processor_latency_notify(struct notifier_block *b,
1366 unsigned long l, void *v)
1367{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001368 smp_call_function(smp_callback, NULL, 1);
Arjan van de Ven5c875792006-09-30 23:27:17 -07001369 return NOTIFY_OK;
1370}
1371
1372static struct notifier_block acpi_processor_latency_notifier = {
1373 .notifier_call = acpi_processor_latency_notify,
1374};
Len Brown4f86d3a2007-10-03 18:58:00 -04001375
Andrew Morton1fec74a2006-10-17 00:09:58 -07001376#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001377
Len Brown4f86d3a2007-10-03 18:58:00 -04001378#else /* CONFIG_CPU_IDLE */
1379
1380/**
1381 * acpi_idle_bm_check - checks if bus master activity was detected
1382 */
1383static int acpi_idle_bm_check(void)
1384{
1385 u32 bm_status = 0;
1386
1387 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1388 if (bm_status)
1389 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1390 /*
1391 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1392 * the true state of bus mastering activity; forcing us to
1393 * manually check the BMIDEA bit of each IDE channel.
1394 */
1395 else if (errata.piix4.bmisx) {
1396 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1397 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1398 bm_status = 1;
1399 }
1400 return bm_status;
1401}
1402
1403/**
1404 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1405 * @pr: the processor
1406 * @target: the new target state
1407 */
1408static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1409 struct acpi_processor_cx *target)
1410{
1411 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1412 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1413 pr->flags.bm_rld_set = 0;
1414 }
1415
1416 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1417 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1418 pr->flags.bm_rld_set = 1;
1419 }
1420}
1421
1422/**
1423 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1424 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001425 *
1426 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001427 */
1428static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1429{
Steven Rostedtdcf30992008-07-25 18:00:42 -04001430 /* Don't trace irqs off for idle */
1431 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001432 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001433 /* Call into architectural FFH based C-state */
1434 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001435 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1436 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001437 } else {
1438 int unused;
1439 /* IO port based C-state */
1440 inb(cx->address);
1441 /* Dummy wait op - must do something useless after P_LVL2 read
1442 because chipsets cannot guarantee that STPCLK# signal
1443 gets asserted in time to freeze execution properly. */
1444 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1445 }
Steven Rostedtdcf30992008-07-25 18:00:42 -04001446 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -04001447}
1448
1449/**
1450 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1451 * @dev: the target CPU
1452 * @state: the state data
1453 *
1454 * This is equivalent to the HALT instruction.
1455 */
1456static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1457 struct cpuidle_state *state)
1458{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001459 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001460 struct acpi_processor *pr;
1461 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001462
Mike Travis706546d2008-06-09 16:22:23 -07001463 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001464
1465 if (unlikely(!pr))
1466 return 0;
1467
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001468 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001469
1470 /* Do not access any ACPI IO ports in suspend path */
1471 if (acpi_idle_suspend) {
1472 acpi_safe_halt();
1473 local_irq_enable();
1474 return 0;
1475 }
1476
Len Brown4f86d3a2007-10-03 18:58:00 -04001477 if (pr->flags.bm_check)
1478 acpi_idle_update_bm_rld(pr, cx);
1479
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001480 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001481 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001482 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001483
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001484 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001485 cx->usage++;
1486
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001487 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001488}
1489
1490/**
1491 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1492 * @dev: the target CPU
1493 * @state: the state data
1494 */
1495static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1496 struct cpuidle_state *state)
1497{
1498 struct acpi_processor *pr;
1499 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1500 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001501 int sleep_ticks = 0;
1502
Mike Travis706546d2008-06-09 16:22:23 -07001503 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001504
1505 if (unlikely(!pr))
1506 return 0;
1507
Len Browne1964412007-10-04 01:23:47 -04001508 if (acpi_idle_suspend)
1509 return(acpi_idle_enter_c1(dev, state));
1510
Len Brown4f86d3a2007-10-03 18:58:00 -04001511 local_irq_disable();
1512 current_thread_info()->status &= ~TS_POLLING;
1513 /*
1514 * TS_POLLING-cleared state must be visible before we test
1515 * NEED_RESCHED:
1516 */
1517 smp_mb();
1518
1519 if (unlikely(need_resched())) {
1520 current_thread_info()->status |= TS_POLLING;
1521 local_irq_enable();
1522 return 0;
1523 }
1524
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001525 /*
1526 * Must be done before busmaster disable as we might need to
1527 * access HPET !
1528 */
1529 acpi_state_timer_broadcast(pr, cx, 1);
1530
1531 if (pr->flags.bm_check)
1532 acpi_idle_update_bm_rld(pr, cx);
1533
Len Brown4f86d3a2007-10-03 18:58:00 -04001534 if (cx->type == ACPI_STATE_C3)
1535 ACPI_FLUSH_CPU_CACHE();
1536
1537 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001538 /* Tell the scheduler that we are going deep-idle: */
1539 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001540 acpi_idle_do_entry(cx);
1541 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1542
Pavel Machek61331162008-02-19 11:00:29 +01001543#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001544 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001545 if (tsc_halts_in_c(cx->type))
1546 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001547#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001548 sleep_ticks = ticks_elapsed(t1, t2);
1549
1550 /* Tell the scheduler how much we idled: */
1551 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001552
1553 local_irq_enable();
1554 current_thread_info()->status |= TS_POLLING;
1555
1556 cx->usage++;
1557
1558 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001559 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001560 return ticks_elapsed_in_us(t1, t2);
1561}
1562
1563static int c3_cpu_count;
1564static DEFINE_SPINLOCK(c3_lock);
1565
1566/**
1567 * acpi_idle_enter_bm - enters C3 with proper BM handling
1568 * @dev: the target CPU
1569 * @state: the state data
1570 *
1571 * If BM is detected, the deepest non-C3 idle state is entered instead.
1572 */
1573static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1574 struct cpuidle_state *state)
1575{
1576 struct acpi_processor *pr;
1577 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1578 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001579 int sleep_ticks = 0;
1580
Mike Travis706546d2008-06-09 16:22:23 -07001581 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001582
1583 if (unlikely(!pr))
1584 return 0;
1585
Len Browne1964412007-10-04 01:23:47 -04001586 if (acpi_idle_suspend)
1587 return(acpi_idle_enter_c1(dev, state));
1588
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001589 if (acpi_idle_bm_check()) {
1590 if (dev->safe_state) {
1591 return dev->safe_state->enter(dev, dev->safe_state);
1592 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001593 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001594 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001595 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001596 return 0;
1597 }
1598 }
1599
Len Brown4f86d3a2007-10-03 18:58:00 -04001600 local_irq_disable();
1601 current_thread_info()->status &= ~TS_POLLING;
1602 /*
1603 * TS_POLLING-cleared state must be visible before we test
1604 * NEED_RESCHED:
1605 */
1606 smp_mb();
1607
1608 if (unlikely(need_resched())) {
1609 current_thread_info()->status |= TS_POLLING;
1610 local_irq_enable();
1611 return 0;
1612 }
1613
Venki Pallipadi996520c2008-03-24 14:24:10 -07001614 acpi_unlazy_tlb(smp_processor_id());
1615
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001616 /* Tell the scheduler that we are going deep-idle: */
1617 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001618 /*
1619 * Must be done before busmaster disable as we might need to
1620 * access HPET !
1621 */
1622 acpi_state_timer_broadcast(pr, cx, 1);
1623
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001624 acpi_idle_update_bm_rld(pr, cx);
Len Brown4f86d3a2007-10-03 18:58:00 -04001625
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001626 /*
1627 * disable bus master
1628 * bm_check implies we need ARB_DIS
1629 * !bm_check implies we need cache flush
1630 * bm_control implies whether we can do ARB_DIS
1631 *
1632 * That leaves a case where bm_check is set and bm_control is
1633 * not set. In that case we cannot do much, we enter C3
1634 * without doing anything.
1635 */
1636 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001637 spin_lock(&c3_lock);
1638 c3_cpu_count++;
1639 /* Disable bus master arbitration when all CPUs are in C3 */
1640 if (c3_cpu_count == num_online_cpus())
1641 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1642 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001643 } else if (!pr->flags.bm_check) {
1644 ACPI_FLUSH_CPU_CACHE();
1645 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001646
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001647 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1648 acpi_idle_do_entry(cx);
1649 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001650
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001651 /* Re-enable bus master arbitration */
1652 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001653 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001654 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001655 c3_cpu_count--;
1656 spin_unlock(&c3_lock);
1657 }
1658
Pavel Machek61331162008-02-19 11:00:29 +01001659#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001660 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001661 if (tsc_halts_in_c(ACPI_STATE_C3))
1662 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001663#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001664 sleep_ticks = ticks_elapsed(t1, t2);
1665 /* Tell the scheduler how much we idled: */
1666 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001667
1668 local_irq_enable();
1669 current_thread_info()->status |= TS_POLLING;
1670
1671 cx->usage++;
1672
1673 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001674 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001675 return ticks_elapsed_in_us(t1, t2);
1676}
1677
1678struct cpuidle_driver acpi_idle_driver = {
1679 .name = "acpi_idle",
1680 .owner = THIS_MODULE,
1681};
1682
1683/**
1684 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1685 * @pr: the ACPI processor
1686 */
1687static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1688{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001689 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001690 struct acpi_processor_cx *cx;
1691 struct cpuidle_state *state;
1692 struct cpuidle_device *dev = &pr->power.dev;
1693
1694 if (!pr->flags.power_setup_done)
1695 return -EINVAL;
1696
1697 if (pr->flags.power == 0) {
1698 return -EINVAL;
1699 }
1700
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001701 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001702 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1703 dev->states[i].name[0] = '\0';
1704 dev->states[i].desc[0] = '\0';
1705 }
1706
Len Brown4f86d3a2007-10-03 18:58:00 -04001707 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1708 cx = &pr->power.states[i];
1709 state = &dev->states[count];
1710
1711 if (!cx->valid)
1712 continue;
1713
1714#ifdef CONFIG_HOTPLUG_CPU
1715 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1716 !pr->flags.has_cst &&
1717 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1718 continue;
1719#endif
1720 cpuidle_set_statedata(state, cx);
1721
1722 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001723 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001724 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001725 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001726 state->power_usage = cx->power;
1727
1728 state->flags = 0;
1729 switch (cx->type) {
1730 case ACPI_STATE_C1:
1731 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001732 if (cx->entry_method == ACPI_CSTATE_FFH)
1733 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1734
Len Brown4f86d3a2007-10-03 18:58:00 -04001735 state->enter = acpi_idle_enter_c1;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001736 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001737 break;
1738
1739 case ACPI_STATE_C2:
1740 state->flags |= CPUIDLE_FLAG_BALANCED;
1741 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1742 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001743 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001744 break;
1745
1746 case ACPI_STATE_C3:
1747 state->flags |= CPUIDLE_FLAG_DEEP;
1748 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1749 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1750 state->enter = pr->flags.bm_check ?
1751 acpi_idle_enter_bm :
1752 acpi_idle_enter_simple;
1753 break;
1754 }
1755
1756 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001757 if (count == CPUIDLE_STATE_MAX)
1758 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001759 }
1760
1761 dev->state_count = count;
1762
1763 if (!count)
1764 return -EINVAL;
1765
Len Brown4f86d3a2007-10-03 18:58:00 -04001766 return 0;
1767}
1768
1769int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1770{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001771 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001772
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001773 if (boot_option_idle_override)
1774 return 0;
1775
Len Brown4f86d3a2007-10-03 18:58:00 -04001776 if (!pr)
1777 return -EINVAL;
1778
1779 if (nocst) {
1780 return -ENODEV;
1781 }
1782
1783 if (!pr->flags.power_setup_done)
1784 return -ENODEV;
1785
1786 cpuidle_pause_and_lock();
1787 cpuidle_disable_device(&pr->power.dev);
1788 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001789 if (pr->flags.power) {
1790 acpi_processor_setup_cpuidle(pr);
1791 ret = cpuidle_enable_device(&pr->power.dev);
1792 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001793 cpuidle_resume_and_unlock();
1794
1795 return ret;
1796}
1797
1798#endif /* CONFIG_CPU_IDLE */
1799
Pierre Ossman7af8b662006-10-10 14:20:31 -07001800int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001801 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Len Brown4be44fc2005-08-05 00:44:28 -04001803 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001804 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001805 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 unsigned int i;
1807
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001808 if (boot_option_idle_override)
1809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001812 if (idle_halt) {
1813 /*
1814 * When the boot option of "idle=halt" is added, halt
1815 * is used for CPU IDLE.
1816 * In such case C2/C3 is meaningless. So the max_cstate
1817 * is set to one.
1818 */
1819 max_cstate = 1;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001822 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001824 printk(KERN_NOTICE
1825 "ACPI: processor limited to max C-state %d\n",
1826 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001828#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1829 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1830 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001834 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001835 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001836
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001837 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001838 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001839 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001841 ACPI_EXCEPTION((AE_INFO, status,
1842 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 }
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001847 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /*
1850 * Install the idle handler if processor power management is supported.
1851 * Note that we use previously set idle handler will be used on
1852 * platforms that only support C1.
1853 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001854 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001855#ifdef CONFIG_CPU_IDLE
1856 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001857 if (cpuidle_register_device(&pr->power.dev))
1858 return -EIO;
1859#endif
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1862 for (i = 1; i <= pr->power.count; i++)
1863 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001864 printk(" C%d[C%d]", i,
1865 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 printk(")\n");
1867
Len Brown4f86d3a2007-10-03 18:58:00 -04001868#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (pr->id == 0) {
1870 pm_idle_save = pm_idle;
1871 pm_idle = acpi_processor_idle;
1872 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001877 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1878 S_IRUGO, acpi_device_dir(device),
1879 &acpi_processor_power_fops,
1880 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001882 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Len Brown4be44fc2005-08-05 00:44:28 -04001886int acpi_processor_power_exit(struct acpi_processor *pr,
1887 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001889 if (boot_option_idle_override)
1890 return 0;
1891
Len Brown4f86d3a2007-10-03 18:58:00 -04001892#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001893 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 pr->flags.power_setup_done = 0;
1896
1897 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001898 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1899 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Len Brown4f86d3a2007-10-03 18:58:00 -04001901#ifndef CONFIG_CPU_IDLE
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* Unregister the idle handler when processor #0 is removed. */
1904 if (pr->id == 0) {
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001905 if (pm_idle_save)
1906 pm_idle = pm_idle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /*
1909 * We are about to unload the current idle thread pm callback
1910 * (pm_idle), Wait for all processors to update cached/local
1911 * copies of pm_idle before proceeding.
1912 */
1913 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001914#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001915 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1916 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001917#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001919#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Patrick Mocheld550d982006-06-27 00:41:40 -04001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}