blob: 7eab733ae96e85fd06663ec5a086ea9609d642ea [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>
Russell Kingba84be22009-01-06 14:41:07 -080044#include <linux/irqflags.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
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050065ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define ACPI_PROCESSOR_FILE_POWER "power"
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
Ingo Molnar2aa44d02007-08-23 15:18:02 +020068#define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
Len Brown4f86d3a2007-10-03 18:58:00 -040069#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070070#define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
71#define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
Andreas Mohrb6835052006-04-27 05:25:00 -040072static void (*pm_idle_save) (void) __read_mostly;
Len Brown4f86d3a2007-10-03 18:58:00 -040073#else
74#define C2_OVERHEAD 1 /* 1us */
75#define C3_OVERHEAD 1 /* 1us */
76#endif
77#define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
Len Brown4f86d3a2007-10-03 18:58:00 -040079static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050080#ifdef CONFIG_CPU_IDLE
Len Brown4f86d3a2007-10-03 18:58:00 -040081module_param(max_cstate, uint, 0000);
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050082#else
83module_param(max_cstate, uint, 0644);
84#endif
Andreas Mohrb6835052006-04-27 05:25:00 -040085static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086module_param(nocst, uint, 0000);
87
Len Brown4f86d3a2007-10-03 18:58:00 -040088#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*
90 * bm_history -- bit-mask with a bit per jiffy of bus-master activity
91 * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
92 * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
93 * 100 HZ: 0x0000000F: 4 jiffies = 40ms
94 * reduce history for more aggressive entry into C3
95 */
Andreas Mohrb6835052006-04-27 05:25:00 -040096static unsigned int bm_history __read_mostly =
Len Brown4be44fc2005-08-05 00:44:28 -040097 (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070098module_param(bm_history, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -040099
100static int acpi_processor_set_power_policy(struct acpi_processor *pr);
101
Len Brown4963f622007-12-13 23:50:45 -0500102#else /* CONFIG_CPU_IDLE */
Len Brown25de5712007-12-14 00:24:15 -0500103static unsigned int latency_factor __read_mostly = 2;
Len Brown4963f622007-12-13 23:50:45 -0500104module_param(latency_factor, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -0400105#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106
107/*
108 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
109 * For now disable this. Probably a bug somewhere else.
110 *
111 * To skip this limit, boot/load with a large max_cstate limit.
112 */
Jeff Garzik18552562007-10-03 15:15:40 -0400113static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114{
115 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
116 return 0;
117
Len Brown3d356002005-08-03 00:22:52 -0400118 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
Len Brown4be44fc2005-08-05 00:44:28 -0400119 " Override with \"processor.max_cstate=%d\"\n", id->ident,
120 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Len Brown3d356002005-08-03 00:22:52 -0400122 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123
124 return 0;
125}
126
Ashok Raj7ded5682006-02-03 21:51:23 +0100127/* Actually this shouldn't be __cpuinitdata, would be better to fix the
128 callers to only run once -AK */
129static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -0500130 { set_max_cstate, "IBM ThinkPad R40e", {
131 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Bartlomiej Swierczf8313352006-05-29 07:16:00 -0400132 DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
133 { set_max_cstate, "IBM ThinkPad R40e", {
134 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Thomas Rosner876c1842006-01-06 01:31:00 -0500135 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
136 { set_max_cstate, "IBM ThinkPad R40e", {
137 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
138 DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
139 { set_max_cstate, "IBM ThinkPad R40e", {
140 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
141 DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
142 { set_max_cstate, "IBM ThinkPad R40e", {
143 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
144 DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
145 { set_max_cstate, "IBM ThinkPad R40e", {
146 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
147 DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
148 { set_max_cstate, "IBM ThinkPad R40e", {
149 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
150 DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
151 { set_max_cstate, "IBM ThinkPad R40e", {
152 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
153 DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
154 { set_max_cstate, "IBM ThinkPad R40e", {
155 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
156 DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
157 { set_max_cstate, "IBM ThinkPad R40e", {
158 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
159 DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
160 { set_max_cstate, "IBM ThinkPad R40e", {
161 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
162 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
163 { set_max_cstate, "IBM ThinkPad R40e", {
164 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
165 DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
166 { set_max_cstate, "IBM ThinkPad R40e", {
167 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
168 DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
169 { set_max_cstate, "IBM ThinkPad R40e", {
170 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
171 DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
172 { set_max_cstate, "IBM ThinkPad R40e", {
173 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
174 DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
175 { set_max_cstate, "IBM ThinkPad R40e", {
176 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
177 DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
178 { set_max_cstate, "Medion 41700", {
179 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
180 DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
181 { set_max_cstate, "Clevo 5600D", {
182 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
183 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
Len Brown4be44fc2005-08-05 00:44:28 -0400184 (void *)2},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185 {},
186};
187
Len Brown4be44fc2005-08-05 00:44:28 -0400188static inline u32 ticks_elapsed(u32 t1, u32 t2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
190 if (t2 >= t1)
191 return (t2 - t1);
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300192 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
194 else
195 return ((0xFFFFFFFF - t1) + t2);
196}
197
Len Brown4f86d3a2007-10-03 18:58:00 -0400198static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
199{
200 if (t2 >= t1)
201 return PM_TIMER_TICKS_TO_US(t2 - t1);
202 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
203 return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
204 else
205 return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
206}
207
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -0800208/*
209 * Callers should disable interrupts before the call and enable
210 * interrupts after return.
211 */
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500212static void acpi_safe_halt(void)
213{
214 current_thread_info()->status &= ~TS_POLLING;
215 /*
216 * TS_POLLING-cleared state must be visible before we
217 * test NEED_RESCHED:
218 */
219 smp_mb();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700220 if (!need_resched()) {
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500221 safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700222 local_irq_disable();
223 }
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500224 current_thread_info()->status |= TS_POLLING;
225}
226
Len Brown4f86d3a2007-10-03 18:58:00 -0400227#ifndef CONFIG_CPU_IDLE
228
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229static void
Len Brown4be44fc2005-08-05 00:44:28 -0400230acpi_processor_power_activate(struct acpi_processor *pr,
231 struct acpi_processor_cx *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Len Brown4be44fc2005-08-05 00:44:28 -0400233 struct acpi_processor_cx *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234
235 if (!pr || !new)
236 return;
237
238 old = pr->power.state;
239
240 if (old)
241 old->promotion.count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400242 new->demotion.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 pr->power.state = new;
245
246 return;
247}
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249static atomic_t c3_cpu_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700251/* Common C-state entry for C2, C3, .. */
252static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
253{
Steven Rostedtdcf30992008-07-25 18:00:42 -0400254 /* Don't trace irqs off for idle */
255 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800256 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700257 /* Call into architectural FFH based C-state */
258 acpi_processor_ffh_cstate_enter(cstate);
259 } else {
260 int unused;
261 /* IO port based C-state */
262 inb(cstate->address);
263 /* Dummy wait op - must do something useless after P_LVL2 read
264 because chipsets cannot guarantee that STPCLK# signal
265 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300266 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700267 }
Steven Rostedtdcf30992008-07-25 18:00:42 -0400268 start_critical_timings();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700269}
Len Brown4f86d3a2007-10-03 18:58:00 -0400270#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700271
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800272#ifdef ARCH_APICTIMER_STOPS_ON_C3
273
274/*
275 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700276 * This seems to be a common problem on AMD boxen, but other vendors
277 * are affected too. We pick the most conservative approach: we assume
278 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800279 */
280static void acpi_timer_check_state(int state, struct acpi_processor *pr,
281 struct acpi_processor_cx *cx)
282{
283 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100284 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800285
286 /*
287 * Check, if one of the previous states already marked the lapic
288 * unstable
289 */
290 if (pwr->timer_broadcast_on_state < state)
291 return;
292
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100293 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700294 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800295}
296
297static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
298{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800299 unsigned long reason;
300
301 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
302 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
303
304 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800305}
306
307/* Power(C) State timer broadcast control */
308static void acpi_state_timer_broadcast(struct acpi_processor *pr,
309 struct acpi_processor_cx *cx,
310 int broadcast)
311{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800312 int state = cx - pr->power.states;
313
314 if (state >= pr->power.timer_broadcast_on_state) {
315 unsigned long reason;
316
317 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
318 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
319 clockevents_notify(reason, &pr->id);
320 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800321}
322
323#else
324
325static void acpi_timer_check_state(int state, struct acpi_processor *pr,
326 struct acpi_processor_cx *cstate) { }
327static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800328static void acpi_state_timer_broadcast(struct acpi_processor *pr,
329 struct acpi_processor_cx *cx,
330 int broadcast)
331{
332}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800333
334#endif
335
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000336/*
337 * Suspend / resume control
338 */
339static int acpi_idle_suspend;
340
341int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
342{
343 acpi_idle_suspend = 1;
344 return 0;
345}
346
347int acpi_processor_resume(struct acpi_device * device)
348{
349 acpi_idle_suspend = 0;
350 return 0;
351}
352
Pavel Machek61331162008-02-19 11:00:29 +0100353#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100354static int tsc_halts_in_c(int state)
355{
356 switch (boot_cpu_data.x86_vendor) {
357 case X86_VENDOR_AMD:
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800358 case X86_VENDOR_INTEL:
Andi Kleenddb25f92008-01-30 13:32:41 +0100359 /*
360 * AMD Fam10h TSC will tick in all
361 * C/P/S0/S1 states when this bit is set.
362 */
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800363 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Andi Kleenddb25f92008-01-30 13:32:41 +0100364 return 0;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800365
Andi Kleenddb25f92008-01-30 13:32:41 +0100366 /*FALL THROUGH*/
Andi Kleenddb25f92008-01-30 13:32:41 +0100367 default:
368 return state > ACPI_STATE_C1;
369 }
370}
371#endif
372
Len Brown4f86d3a2007-10-03 18:58:00 -0400373#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400374static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Len Brown4be44fc2005-08-05 00:44:28 -0400376 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377 struct acpi_processor_cx *cx = NULL;
378 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400379 int sleep_ticks = 0;
380 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 /*
383 * Interrupts must be disabled during bus mastering calculations and
384 * for C2/C3 transitions.
385 */
386 local_irq_disable();
387
Mike Travis706546d2008-06-09 16:22:23 -0700388 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400389 if (!pr) {
390 local_irq_enable();
391 return;
392 }
393
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 /*
395 * Check whether we truly need to go idle, or should
396 * reschedule:
397 */
398 if (unlikely(need_resched())) {
399 local_irq_enable();
400 return;
401 }
402
403 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000404 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200405 if (pm_idle_save) {
406 pm_idle_save(); /* enables IRQs */
407 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800408 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700409 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200410 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700411
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800412 return;
413 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414
415 /*
416 * Check BM Activity
417 * -----------------
418 * Check for bus mastering activity (if required), record, and check
419 * for demotion.
420 */
421 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400422 u32 bm_status = 0;
423 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400425 if (diff > 31)
426 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400428 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429
Len Browna2b7b012009-01-28 12:47:15 -0500430 acpi_get_register_unlocked(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400432 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300433 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 }
435 /*
436 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
437 * the true state of bus mastering activity; forcing us to
438 * manually check the BMIDEA bit of each IDE channel.
439 */
440 else if (errata.piix4.bmisx) {
441 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400442 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400443 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 }
445
446 pr->power.bm_check_timestamp = jiffies;
447
448 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400449 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 * to avoid a faulty transition. Note that the processor
451 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400452 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 *
454 * TBD: A better policy might be to fallback to the demotion
455 * state (use it for this quantum only) istead of
456 * demoting -- and rely on duration as our sole demotion
457 * qualification. This may, however, introduce DMA
458 * issues (e.g. floppy DMA transfer overrun/underrun).
459 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400460 if ((pr->power.bm_activity & 0x1) &&
461 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 local_irq_enable();
463 next_state = cx->demotion.state;
464 goto end;
465 }
466 }
467
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400468#ifdef CONFIG_HOTPLUG_CPU
469 /*
470 * Check for P_LVL2_UP flag before entering C2 and above on
471 * an SMP system. We do it here instead of doing it at _CST/P_LVL
472 * detection phase, to work cleanly with logical CPU hotplug.
473 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400474 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300475 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500476 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400477#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500478
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 /*
480 * Sleep:
481 * ------
482 * Invoke the current Cx state to put the processor to sleep.
483 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100484 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200485 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800486 /*
487 * TS_POLLING-cleared state must be visible before we
488 * test NEED_RESCHED:
489 */
490 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100491 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200492 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800493 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100494 return;
495 }
496 }
497
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 switch (cx->type) {
499
500 case ACPI_STATE_C1:
501 /*
502 * Invoke C1.
503 * Use the appropriate idle routine, the one that would
504 * be used without acpi C-states.
505 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200506 if (pm_idle_save) {
507 pm_idle_save(); /* enables IRQs */
508 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800509 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200510 local_irq_enable();
511 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800512
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400514 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * go to an ISR rather than here. Need to instrument
516 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200517 *
518 * Note: the TSC better not stop in C1, sched_clock() will
519 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 */
521 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 break;
524
525 case ACPI_STATE_C2:
526 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300527 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200528 /* Tell the scheduler that we are going deep-idle: */
529 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800531 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700532 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300534 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700535
Pavel Machek61331162008-02-19 11:00:29 +0100536#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700537 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100538 if (tsc_halts_in_c(ACPI_STATE_C2))
539 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700540#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200541 /* Compute time (ticks) that we were actually asleep */
542 sleep_ticks = ticks_elapsed(t1, t2);
543
544 /* Tell the scheduler how much we idled: */
545 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 /* Re-enable interrupts */
548 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200549 /* Do not account our idle-switching overhead: */
550 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
551
Andi Kleen495ab9c2006-06-26 13:59:11 +0200552 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800553 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 break;
555
556 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100557 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400558 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100559 * Must be done before busmaster disable as we might
560 * need to access HPET !
561 */
562 acpi_state_timer_broadcast(pr, cx, 1);
563 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400564 * disable bus master
565 * bm_check implies we need ARB_DIS
566 * !bm_check implies we need cache flush
567 * bm_control implies whether we can do ARB_DIS
568 *
569 * That leaves a case where bm_check is set and bm_control is
570 * not set. In that case we cannot do much, we enter C3
571 * without doing anything.
572 */
573 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400574 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400575 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400576 /*
577 * All CPUs are trying to go to C3
578 * Disable bus master arbitration
579 */
Bob Moored8c71b62007-02-02 19:48:21 +0300580 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400581 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400582 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400583 /* SMP with no shared cache... Invalidate cache */
584 ACPI_FLUSH_CPU_CACHE();
585 }
Len Brown4be44fc2005-08-05 00:44:28 -0400586
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300588 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200590 /* Tell the scheduler that we are going deep-idle: */
591 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700592 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300594 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400595 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400596 /* Enable bus master arbitration */
597 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300598 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400599 }
600
Pavel Machek61331162008-02-19 11:00:29 +0100601#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700602 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100603 if (tsc_halts_in_c(ACPI_STATE_C3))
604 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700605#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200606 /* Compute time (ticks) that we were actually asleep */
607 sleep_ticks = ticks_elapsed(t1, t2);
608 /* Tell the scheduler how much we idled: */
609 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Re-enable interrupts */
612 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200613 /* Do not account our idle-switching overhead: */
614 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
615
Andi Kleen495ab9c2006-06-26 13:59:11 +0200616 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800617 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 break;
619
620 default:
621 local_irq_enable();
622 return;
623 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400624 cx->usage++;
625 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
626 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627
628 next_state = pr->power.state;
629
David Shaohua Li1e483962005-12-01 17:00:00 -0500630#ifdef CONFIG_HOTPLUG_CPU
631 /* Don't do promotion/demotion */
632 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300633 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500634 next_state = cx;
635 goto end;
636 }
637#endif
638
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 /*
640 * Promotion?
641 * ----------
642 * Track the number of longs (time asleep is greater than threshold)
643 * and promote when the count threshold is reached. Note that bus
644 * mastering activity may prevent promotions.
645 * Do not promote above max_cstate.
646 */
647 if (cx->promotion.state &&
648 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700649 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800650 cx->promotion.state->latency <=
651 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400653 cx->demotion.count = 0;
654 if (cx->promotion.count >=
655 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400657 if (!
658 (pr->power.bm_activity & cx->
659 promotion.threshold.bm)) {
660 next_state =
661 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 goto end;
663 }
Len Brown4be44fc2005-08-05 00:44:28 -0400664 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 next_state = cx->promotion.state;
666 goto end;
667 }
668 }
669 }
670 }
671
672 /*
673 * Demotion?
674 * ---------
675 * Track the number of shorts (time asleep is less than time threshold)
676 * and demote when the usage threshold is reached.
677 */
678 if (cx->demotion.state) {
679 if (sleep_ticks < cx->demotion.threshold.ticks) {
680 cx->demotion.count++;
681 cx->promotion.count = 0;
682 if (cx->demotion.count >= cx->demotion.threshold.count) {
683 next_state = cx->demotion.state;
684 goto end;
685 }
686 }
687 }
688
Len Brown4be44fc2005-08-05 00:44:28 -0400689 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 /*
691 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700692 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700694 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800695 pr->power.state->latency >
696 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (cx->demotion.state)
698 next_state = cx->demotion.state;
699 }
700
701 /*
702 * New Cx State?
703 * -------------
704 * If we're going to start using a new Cx state we must clean up
705 * from the previous and prepare to use the new.
706 */
707 if (next_state != pr->power.state)
708 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709}
710
Len Brown4be44fc2005-08-05 00:44:28 -0400711static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712{
713 unsigned int i;
714 unsigned int state_is_set = 0;
715 struct acpi_processor_cx *lower = NULL;
716 struct acpi_processor_cx *higher = NULL;
717 struct acpi_processor_cx *cx;
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400721 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
723 /*
724 * This function sets the default Cx state policy (OS idle handler).
725 * Our scheme is to promote quickly to C2 but more conservatively
726 * to C3. We're favoring C2 for its characteristics of low latency
727 * (quick response), good power savings, and ability to allow bus
728 * mastering activity. Note that the Cx state policy is completely
729 * customizable and can be altered dynamically.
730 */
731
732 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400733 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 cx = &pr->power.states[i];
735 if (!cx->valid)
736 continue;
737
738 if (!state_is_set)
739 pr->power.state = cx;
740 state_is_set++;
741 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400742 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400748 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 cx = &pr->power.states[i];
750 if (!cx->valid)
751 continue;
752
753 if (lower) {
754 cx->demotion.state = lower;
755 cx->demotion.threshold.ticks = cx->latency_ticks;
756 cx->demotion.threshold.count = 1;
757 if (cx->type == ACPI_STATE_C3)
758 cx->demotion.threshold.bm = bm_history;
759 }
760
761 lower = cx;
762 }
763
764 /* promotion */
765 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
766 cx = &pr->power.states[i];
767 if (!cx->valid)
768 continue;
769
770 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400771 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 cx->promotion.threshold.ticks = cx->latency_ticks;
773 if (cx->type >= ACPI_STATE_C2)
774 cx->promotion.threshold.count = 4;
775 else
776 cx->promotion.threshold.count = 10;
777 if (higher->type == ACPI_STATE_C3)
778 cx->promotion.threshold.bm = bm_history;
779 }
780
781 higher = cx;
782 }
783
Patrick Mocheld550d982006-06-27 00:41:40 -0400784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
Len Brown4f86d3a2007-10-03 18:58:00 -0400786#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787
Len Brown4be44fc2005-08-05 00:44:28 -0400788static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
791 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400792 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
799 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
800
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400801#ifndef CONFIG_HOTPLUG_CPU
802 /*
803 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400804 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400805 */
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300806 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300807 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400809#endif
810
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811 /* determine C2 and C3 address from pblk */
812 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
813 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
814
815 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300816 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
817 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818
819 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
820 "lvl2[0x%08x] lvl3[0x%08x]\n",
821 pr->power.states[ACPI_STATE_C2].address,
822 pr->power.states[ACPI_STATE_C3].address));
823
Patrick Mocheld550d982006-06-27 00:41:40 -0400824 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
826
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700827static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500828{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700829 if (!pr->power.states[ACPI_STATE_C1].valid) {
830 /* set the first C-State to C1 */
831 /* all processors need to support C1 */
832 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
833 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400834 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700835 }
836 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500837 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400838 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500839}
840
Len Brown4be44fc2005-08-05 00:44:28 -0400841static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842{
Len Brown4be44fc2005-08-05 00:44:28 -0400843 acpi_status status = 0;
844 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400845 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400846 int i;
847 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
848 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400852 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700854 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
857 if (ACPI_FAILURE(status)) {
858 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400859 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400860 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200862 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863
864 /* There must be at least 2 elements */
865 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400866 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 status = -EFAULT;
868 goto end;
869 }
870
871 count = cst->package.elements[0].integer.value;
872
873 /* Validate number of power states. */
874 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400875 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876 status = -EFAULT;
877 goto end;
878 }
879
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 /* Tell driver that at least _CST is supported. */
881 pr->flags.has_cst = 1;
882
883 for (i = 1; i <= count; i++) {
884 union acpi_object *element;
885 union acpi_object *obj;
886 struct acpi_power_register *reg;
887 struct acpi_processor_cx cx;
888
889 memset(&cx, 0, sizeof(cx));
890
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200891 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 if (element->type != ACPI_TYPE_PACKAGE)
893 continue;
894
895 if (element->package.count != 4)
896 continue;
897
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200898 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
900 if (obj->type != ACPI_TYPE_BUFFER)
901 continue;
902
Len Brown4be44fc2005-08-05 00:44:28 -0400903 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904
905 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400906 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700907 continue;
908
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200910 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700911 if (obj->type != ACPI_TYPE_INTEGER)
912 continue;
913
914 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700915 /*
916 * Some buggy BIOSes won't list C1 in _CST -
917 * Let acpi_processor_get_power_info_default() handle them later
918 */
919 if (i == 1 && cx.type != ACPI_STATE_C1)
920 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700922 cx.address = reg->address;
923 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800925 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700926 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
927 if (acpi_processor_ffh_cstate_probe
928 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800929 cx.entry_method = ACPI_CSTATE_FFH;
930 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700931 /*
932 * C1 is a special case where FIXED_HARDWARE
933 * can be handled in non-MWAIT way as well.
934 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700935 * Otherwise, ignore this info and continue.
936 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800937 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800938 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800939 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700940 continue;
941 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800942 if (cx.type == ACPI_STATE_C1 &&
943 (idle_halt || idle_nomwait)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800944 /*
945 * In most cases the C1 space_id obtained from
946 * _CST object is FIXED_HARDWARE access mode.
947 * But when the option of idle=halt is added,
948 * the entry_method type should be changed from
949 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800950 * When the option of idle=nomwait is added,
951 * the C1 entry_method type should be
952 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800953 */
954 cx.entry_method = ACPI_CSTATE_HALT;
955 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
956 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800957 } else {
958 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
959 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700960 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400962 if (cx.type == ACPI_STATE_C1) {
963 cx.valid = 1;
964 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800965
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200966 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 if (obj->type != ACPI_TYPE_INTEGER)
968 continue;
969
970 cx.latency = obj->integer.value;
971
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200972 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973 if (obj->type != ACPI_TYPE_INTEGER)
974 continue;
975
976 cx.power = obj->integer.value;
977
Janosch Machowinskicf824782005-08-20 08:02:00 -0400978 current_count++;
979 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
980
981 /*
982 * We support total ACPI_PROCESSOR_MAX_POWER - 1
983 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
984 */
985 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
986 printk(KERN_WARNING
987 "Limiting number of power states to max (%d)\n",
988 ACPI_PROCESSOR_MAX_POWER);
989 printk(KERN_WARNING
990 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
991 break;
992 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 }
994
Len Brown4be44fc2005-08-05 00:44:28 -0400995 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -0400996 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
998 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -0400999 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001000 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001001
Len Brown4be44fc2005-08-05 00:44:28 -04001002 end:
Len Brown02438d82006-06-30 03:19:10 -04001003 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001004
Patrick Mocheld550d982006-06-27 00:41:40 -04001005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006}
1007
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1009{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010
1011 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 /*
1015 * C2 latency must be less than or equal to 100
1016 * microseconds.
1017 */
1018 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1019 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001020 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001021 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 }
1023
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024 /*
1025 * Otherwise we've met all of our C2 requirements.
1026 * Normalize the C2 latency to expidite policy
1027 */
1028 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001029
1030#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001032#else
1033 cx->latency_ticks = cx->latency;
1034#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037}
1038
Len Brown4be44fc2005-08-05 00:44:28 -04001039static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1040 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001042 static int bm_check_flag;
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001046 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047
1048 /*
1049 * C3 latency must be less than or equal to 1000
1050 * microseconds.
1051 */
1052 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1053 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001054 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001055 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 }
1057
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 /*
1059 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1060 * DMA transfers are used by any ISA device to avoid livelock.
1061 * Note that we could disable Type-F DMA (as recommended by
1062 * the erratum), but this is known to disrupt certain ISA
1063 * devices thus we take the conservative approach.
1064 */
1065 else if (errata.piix4.fdma) {
1066 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001067 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 }
1070
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001071 /* All the logic here assumes flags.bm_check is same across all CPUs */
1072 if (!bm_check_flag) {
1073 /* Determine whether bm_check is needed based on CPU */
1074 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1075 bm_check_flag = pr->flags.bm_check;
1076 } else {
1077 pr->flags.bm_check = bm_check_flag;
1078 }
1079
1080 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001081 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001082 if (pr->flags.has_cst != 1) {
1083 /* bus mastering control is necessary */
1084 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1085 "C3 support requires BM control\n"));
1086 return;
1087 } else {
1088 /* Here we enter C3 without bus mastering */
1089 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1090 "C3 support without BM control\n"));
1091 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001092 }
1093 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001094 /*
1095 * WBINVD should be set in fadt, for C3 state to be
1096 * supported on when bm_check is not required.
1097 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001098 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001099 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001100 "Cache invalidation should work properly"
1101 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001102 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001103 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001104 }
1105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * Otherwise we've met all of our C3 requirements.
1108 * Normalize the C3 latency to expidite policy. Enable
1109 * checking of bus mastering status (bm_check) so we can
1110 * use this in our C3 policy
1111 */
1112 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001113
1114#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001116#else
1117 cx->latency_ticks = cx->latency;
1118#endif
Len Brown31878dd2009-01-28 18:28:09 -05001119 /*
1120 * On older chipsets, BM_RLD needs to be set
1121 * in order for Bus Master activity to wake the
1122 * system from C3. Newer chipsets handle DMA
1123 * during C3 automatically and BM_RLD is a NOP.
1124 * In either case, the proper way to
1125 * handle BM_RLD is to set it and leave it set.
1126 */
1127 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001128
Patrick Mocheld550d982006-06-27 00:41:40 -04001129 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130}
1131
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132static int acpi_processor_power_verify(struct acpi_processor *pr)
1133{
1134 unsigned int i;
1135 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001136
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001137 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001138
Len Brown4be44fc2005-08-05 00:44:28 -04001139 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 struct acpi_processor_cx *cx = &pr->power.states[i];
1141
1142 switch (cx->type) {
1143 case ACPI_STATE_C1:
1144 cx->valid = 1;
1145 break;
1146
1147 case ACPI_STATE_C2:
1148 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001149 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001150 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 break;
1152
1153 case ACPI_STATE_C3:
1154 acpi_processor_power_verify_c3(pr, 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
1160 if (cx->valid)
1161 working++;
1162 }
1163
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001164 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001165
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 return (working);
1167}
1168
Len Brown4be44fc2005-08-05 00:44:28 -04001169static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170{
1171 unsigned int i;
1172 int result;
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174
1175 /* NOTE: the idle thread may not be running while calling
1176 * this function */
1177
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001178 /* Zero initialize all the C-states info. */
1179 memset(pr->power.states, 0, sizeof(pr->power.states));
1180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001182 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001183 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001184
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001185 if (result)
1186 return result;
1187
1188 acpi_processor_get_power_info_default(pr);
1189
Janosch Machowinskicf824782005-08-20 08:02:00 -04001190 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001191
Len Brown4f86d3a2007-10-03 18:58:00 -04001192#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 /*
1194 * Set Default Policy
1195 * ------------------
1196 * Now that we know which states are supported, set the default
1197 * policy. Note that this policy can be changed dynamically
1198 * (e.g. encourage deeper sleeps to conserve battery life when
1199 * not on AC).
1200 */
1201 result = acpi_processor_set_power_policy(pr);
1202 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001203 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001204#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205
1206 /*
1207 * if one state of type C2 or C3 is available, mark this
1208 * CPU as being "idle manageable"
1209 */
1210 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001211 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001213 if (pr->power.states[i].type >= ACPI_STATE_C2)
1214 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001215 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 }
1217
Patrick Mocheld550d982006-06-27 00:41:40 -04001218 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001219}
1220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1222{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001223 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001224 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226
1227 if (!pr)
1228 goto end;
1229
1230 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001231 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001232 "bus master activity: %08x\n"
1233 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001234 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001235 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001236 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
1238 seq_puts(seq, "states:\n");
1239
1240 for (i = 1; i <= pr->power.count; i++) {
1241 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001242 (&pr->power.states[i] ==
1243 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245 if (!pr->power.states[i].valid) {
1246 seq_puts(seq, "<not supported>\n");
1247 continue;
1248 }
1249
1250 switch (pr->power.states[i].type) {
1251 case ACPI_STATE_C1:
1252 seq_printf(seq, "type[C1] ");
1253 break;
1254 case ACPI_STATE_C2:
1255 seq_printf(seq, "type[C2] ");
1256 break;
1257 case ACPI_STATE_C3:
1258 seq_printf(seq, "type[C3] ");
1259 break;
1260 default:
1261 seq_printf(seq, "type[--] ");
1262 break;
1263 }
1264
1265 if (pr->power.states[i].promotion.state)
1266 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001267 (pr->power.states[i].promotion.state -
1268 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269 else
1270 seq_puts(seq, "promotion[--] ");
1271
1272 if (pr->power.states[i].demotion.state)
1273 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001274 (pr->power.states[i].demotion.state -
1275 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 else
1277 seq_puts(seq, "demotion[--] ");
1278
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001279 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001280 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001281 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001282 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283 }
1284
Len Brown4be44fc2005-08-05 00:44:28 -04001285 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001286 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1290{
1291 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001292 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293}
1294
Arjan van de Vend7508032006-07-04 13:06:00 -04001295static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001296 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001297 .open = acpi_processor_power_open_fs,
1298 .read = seq_read,
1299 .llseek = seq_lseek,
1300 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301};
1302
Len Brown4f86d3a2007-10-03 18:58:00 -04001303#ifndef CONFIG_CPU_IDLE
1304
1305int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1306{
1307 int result = 0;
1308
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001309 if (boot_option_idle_override)
1310 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001311
1312 if (!pr)
1313 return -EINVAL;
1314
1315 if (nocst) {
1316 return -ENODEV;
1317 }
1318
1319 if (!pr->flags.power_setup_done)
1320 return -ENODEV;
1321
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001322 /*
1323 * Fall back to the default idle loop, when pm_idle_save had
1324 * been initialized.
1325 */
1326 if (pm_idle_save) {
1327 pm_idle = pm_idle_save;
1328 /* Relies on interrupts forcing exit from idle. */
1329 synchronize_sched();
1330 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001331
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
Len Browna2b7b012009-01-28 12:47:15 -05001374 acpi_get_register_unlocked(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Len Brown4f86d3a2007-10-03 18:58:00 -04001375 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/**
Len Brown4f86d3a2007-10-03 18:58:00 -04001391 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1392 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001393 *
1394 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001395 */
1396static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1397{
Steven Rostedtdcf30992008-07-25 18:00:42 -04001398 /* Don't trace irqs off for idle */
1399 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001400 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001401 /* Call into architectural FFH based C-state */
1402 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001403 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1404 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001405 } else {
1406 int unused;
1407 /* IO port based C-state */
1408 inb(cx->address);
1409 /* Dummy wait op - must do something useless after P_LVL2 read
1410 because chipsets cannot guarantee that STPCLK# signal
1411 gets asserted in time to freeze execution properly. */
1412 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1413 }
Steven Rostedtdcf30992008-07-25 18:00:42 -04001414 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -04001415}
1416
1417/**
1418 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1419 * @dev: the target CPU
1420 * @state: the state data
1421 *
1422 * This is equivalent to the HALT instruction.
1423 */
1424static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1425 struct cpuidle_state *state)
1426{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001427 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001428 struct acpi_processor *pr;
1429 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001430
Mike Travis706546d2008-06-09 16:22:23 -07001431 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001432
1433 if (unlikely(!pr))
1434 return 0;
1435
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001436 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001437
1438 /* Do not access any ACPI IO ports in suspend path */
1439 if (acpi_idle_suspend) {
1440 acpi_safe_halt();
1441 local_irq_enable();
1442 return 0;
1443 }
1444
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001445 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001446 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001447 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001448
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001449 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001450 cx->usage++;
1451
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001452 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001453}
1454
1455/**
1456 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1457 * @dev: the target CPU
1458 * @state: the state data
1459 */
1460static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1461 struct cpuidle_state *state)
1462{
1463 struct acpi_processor *pr;
1464 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1465 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001466 int sleep_ticks = 0;
1467
Mike Travis706546d2008-06-09 16:22:23 -07001468 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001469
1470 if (unlikely(!pr))
1471 return 0;
1472
Len Browne1964412007-10-04 01:23:47 -04001473 if (acpi_idle_suspend)
1474 return(acpi_idle_enter_c1(dev, state));
1475
Len Brown4f86d3a2007-10-03 18:58:00 -04001476 local_irq_disable();
1477 current_thread_info()->status &= ~TS_POLLING;
1478 /*
1479 * TS_POLLING-cleared state must be visible before we test
1480 * NEED_RESCHED:
1481 */
1482 smp_mb();
1483
1484 if (unlikely(need_resched())) {
1485 current_thread_info()->status |= TS_POLLING;
1486 local_irq_enable();
1487 return 0;
1488 }
1489
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001490 /*
1491 * Must be done before busmaster disable as we might need to
1492 * access HPET !
1493 */
1494 acpi_state_timer_broadcast(pr, cx, 1);
1495
Len Brown4f86d3a2007-10-03 18:58:00 -04001496 if (cx->type == ACPI_STATE_C3)
1497 ACPI_FLUSH_CPU_CACHE();
1498
1499 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001500 /* Tell the scheduler that we are going deep-idle: */
1501 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001502 acpi_idle_do_entry(cx);
1503 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1504
Pavel Machek61331162008-02-19 11:00:29 +01001505#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001506 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001507 if (tsc_halts_in_c(cx->type))
1508 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001509#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001510 sleep_ticks = ticks_elapsed(t1, t2);
1511
1512 /* Tell the scheduler how much we idled: */
1513 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001514
1515 local_irq_enable();
1516 current_thread_info()->status |= TS_POLLING;
1517
1518 cx->usage++;
1519
1520 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001521 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001522 return ticks_elapsed_in_us(t1, t2);
1523}
1524
1525static int c3_cpu_count;
1526static DEFINE_SPINLOCK(c3_lock);
1527
1528/**
1529 * acpi_idle_enter_bm - enters C3 with proper BM handling
1530 * @dev: the target CPU
1531 * @state: the state data
1532 *
1533 * If BM is detected, the deepest non-C3 idle state is entered instead.
1534 */
1535static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1536 struct cpuidle_state *state)
1537{
1538 struct acpi_processor *pr;
1539 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1540 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001541 int sleep_ticks = 0;
1542
Mike Travis706546d2008-06-09 16:22:23 -07001543 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001544
1545 if (unlikely(!pr))
1546 return 0;
1547
Len Browne1964412007-10-04 01:23:47 -04001548 if (acpi_idle_suspend)
1549 return(acpi_idle_enter_c1(dev, state));
1550
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001551 if (acpi_idle_bm_check()) {
1552 if (dev->safe_state) {
Venkatesh Pallipadiaddbad42008-09-29 15:24:28 -07001553 dev->last_state = dev->safe_state;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001554 return dev->safe_state->enter(dev, dev->safe_state);
1555 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001556 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001557 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001558 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001559 return 0;
1560 }
1561 }
1562
Len Brown4f86d3a2007-10-03 18:58:00 -04001563 local_irq_disable();
1564 current_thread_info()->status &= ~TS_POLLING;
1565 /*
1566 * TS_POLLING-cleared state must be visible before we test
1567 * NEED_RESCHED:
1568 */
1569 smp_mb();
1570
1571 if (unlikely(need_resched())) {
1572 current_thread_info()->status |= TS_POLLING;
1573 local_irq_enable();
1574 return 0;
1575 }
1576
Venki Pallipadi996520c2008-03-24 14:24:10 -07001577 acpi_unlazy_tlb(smp_processor_id());
1578
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001579 /* Tell the scheduler that we are going deep-idle: */
1580 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001581 /*
1582 * Must be done before busmaster disable as we might need to
1583 * access HPET !
1584 */
1585 acpi_state_timer_broadcast(pr, cx, 1);
1586
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001587 /*
1588 * disable bus master
1589 * bm_check implies we need ARB_DIS
1590 * !bm_check implies we need cache flush
1591 * bm_control implies whether we can do ARB_DIS
1592 *
1593 * That leaves a case where bm_check is set and bm_control is
1594 * not set. In that case we cannot do much, we enter C3
1595 * without doing anything.
1596 */
1597 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001598 spin_lock(&c3_lock);
1599 c3_cpu_count++;
1600 /* Disable bus master arbitration when all CPUs are in C3 */
1601 if (c3_cpu_count == num_online_cpus())
1602 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1603 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001604 } else if (!pr->flags.bm_check) {
1605 ACPI_FLUSH_CPU_CACHE();
1606 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001607
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001608 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1609 acpi_idle_do_entry(cx);
1610 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001611
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001612 /* Re-enable bus master arbitration */
1613 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001614 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001615 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001616 c3_cpu_count--;
1617 spin_unlock(&c3_lock);
1618 }
1619
Pavel Machek61331162008-02-19 11:00:29 +01001620#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001621 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001622 if (tsc_halts_in_c(ACPI_STATE_C3))
1623 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001624#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001625 sleep_ticks = ticks_elapsed(t1, t2);
1626 /* Tell the scheduler how much we idled: */
1627 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001628
1629 local_irq_enable();
1630 current_thread_info()->status |= TS_POLLING;
1631
1632 cx->usage++;
1633
1634 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001635 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001636 return ticks_elapsed_in_us(t1, t2);
1637}
1638
1639struct cpuidle_driver acpi_idle_driver = {
1640 .name = "acpi_idle",
1641 .owner = THIS_MODULE,
1642};
1643
1644/**
1645 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1646 * @pr: the ACPI processor
1647 */
1648static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1649{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001650 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001651 struct acpi_processor_cx *cx;
1652 struct cpuidle_state *state;
1653 struct cpuidle_device *dev = &pr->power.dev;
1654
1655 if (!pr->flags.power_setup_done)
1656 return -EINVAL;
1657
1658 if (pr->flags.power == 0) {
1659 return -EINVAL;
1660 }
1661
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001662 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001663 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1664 dev->states[i].name[0] = '\0';
1665 dev->states[i].desc[0] = '\0';
1666 }
1667
Len Brown4f86d3a2007-10-03 18:58:00 -04001668 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1669 cx = &pr->power.states[i];
1670 state = &dev->states[count];
1671
1672 if (!cx->valid)
1673 continue;
1674
1675#ifdef CONFIG_HOTPLUG_CPU
1676 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1677 !pr->flags.has_cst &&
1678 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1679 continue;
1680#endif
1681 cpuidle_set_statedata(state, cx);
1682
1683 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001684 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001685 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001686 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001687 state->power_usage = cx->power;
1688
1689 state->flags = 0;
1690 switch (cx->type) {
1691 case ACPI_STATE_C1:
1692 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001693 if (cx->entry_method == ACPI_CSTATE_FFH)
1694 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1695
Len Brown4f86d3a2007-10-03 18:58:00 -04001696 state->enter = acpi_idle_enter_c1;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001697 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001698 break;
1699
1700 case ACPI_STATE_C2:
1701 state->flags |= CPUIDLE_FLAG_BALANCED;
1702 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1703 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001704 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001705 break;
1706
1707 case ACPI_STATE_C3:
1708 state->flags |= CPUIDLE_FLAG_DEEP;
1709 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1710 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1711 state->enter = pr->flags.bm_check ?
1712 acpi_idle_enter_bm :
1713 acpi_idle_enter_simple;
1714 break;
1715 }
1716
1717 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001718 if (count == CPUIDLE_STATE_MAX)
1719 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001720 }
1721
1722 dev->state_count = count;
1723
1724 if (!count)
1725 return -EINVAL;
1726
Len Brown4f86d3a2007-10-03 18:58:00 -04001727 return 0;
1728}
1729
1730int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1731{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001732 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001733
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001734 if (boot_option_idle_override)
1735 return 0;
1736
Len Brown4f86d3a2007-10-03 18:58:00 -04001737 if (!pr)
1738 return -EINVAL;
1739
1740 if (nocst) {
1741 return -ENODEV;
1742 }
1743
1744 if (!pr->flags.power_setup_done)
1745 return -ENODEV;
1746
1747 cpuidle_pause_and_lock();
1748 cpuidle_disable_device(&pr->power.dev);
1749 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001750 if (pr->flags.power) {
1751 acpi_processor_setup_cpuidle(pr);
1752 ret = cpuidle_enable_device(&pr->power.dev);
1753 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001754 cpuidle_resume_and_unlock();
1755
1756 return ret;
1757}
1758
1759#endif /* CONFIG_CPU_IDLE */
1760
Pierre Ossman7af8b662006-10-10 14:20:31 -07001761int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001762 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763{
Len Brown4be44fc2005-08-05 00:44:28 -04001764 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001765 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001766 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 unsigned int i;
1768
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001769 if (boot_option_idle_override)
1770 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001773 if (idle_halt) {
1774 /*
1775 * When the boot option of "idle=halt" is added, halt
1776 * is used for CPU IDLE.
1777 * In such case C2/C3 is meaningless. So the max_cstate
1778 * is set to one.
1779 */
1780 max_cstate = 1;
1781 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001783 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001784 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001785 printk(KERN_NOTICE
1786 "ACPI: processor limited to max C-state %d\n",
1787 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001789#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1790 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1791 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001792#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 }
1794
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001795 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001796 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001797
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001798 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001799 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001800 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001802 ACPI_EXCEPTION((AE_INFO, status,
1803 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 }
1805 }
1806
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001808 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
1810 /*
1811 * Install the idle handler if processor power management is supported.
1812 * Note that we use previously set idle handler will be used on
1813 * platforms that only support C1.
1814 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001815 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001816#ifdef CONFIG_CPU_IDLE
1817 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001818 if (cpuidle_register_device(&pr->power.dev))
1819 return -EIO;
1820#endif
1821
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1823 for (i = 1; i <= pr->power.count; i++)
1824 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001825 printk(" C%d[C%d]", i,
1826 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 printk(")\n");
1828
Len Brown4f86d3a2007-10-03 18:58:00 -04001829#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (pr->id == 0) {
1831 pm_idle_save = pm_idle;
1832 pm_idle = acpi_processor_idle;
1833 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001834#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 }
1836
1837 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001838 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1839 S_IRUGO, acpi_device_dir(device),
1840 &acpi_processor_power_fops,
1841 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001842 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001843 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
Len Brown4be44fc2005-08-05 00:44:28 -04001847int acpi_processor_power_exit(struct acpi_processor *pr,
1848 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001850 if (boot_option_idle_override)
1851 return 0;
1852
Len Brown4f86d3a2007-10-03 18:58:00 -04001853#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001854 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001855#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856 pr->flags.power_setup_done = 0;
1857
1858 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001859 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1860 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Len Brown4f86d3a2007-10-03 18:58:00 -04001862#ifndef CONFIG_CPU_IDLE
1863
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864 /* Unregister the idle handler when processor #0 is removed. */
1865 if (pr->id == 0) {
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001866 if (pm_idle_save)
1867 pm_idle = pm_idle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869 /*
1870 * We are about to unload the current idle thread pm callback
1871 * (pm_idle), Wait for all processors to update cached/local
1872 * copies of pm_idle before proceeding.
1873 */
1874 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001875#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001876 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1877 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001878#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881
Patrick Mocheld550d982006-06-27 00:41:40 -04001882 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001883}