blob: a3e66a33b7a2ecf4cb87c47fc74078f4236eac14 [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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
Thomas Gleixner34349332007-02-16 01:27:54 -080045/*
46 * Include the apic definitions for x86 to have the APIC timer related defines
47 * available also for UP (on SMP it gets magically included via linux/smp.h).
48 * asm/acpi.h is not an option, as it would require more include magic. Also
49 * creating an empty asm-ia64/apic.h would just trade pest vs. cholera.
50 */
51#ifdef CONFIG_X86
52#include <asm/apic.h>
53#endif
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055#include <asm/io.h>
56#include <asm/uaccess.h>
57
58#include <acpi/acpi_bus.h>
59#include <acpi/processor.h>
Zhao Yakuic1e3b372008-06-24 17:58:53 +080060#include <asm/processor.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070061
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050064ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define ACPI_PROCESSOR_FILE_POWER "power"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
Ingo Molnar2aa44d02007-08-23 15:18:02 +020067#define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
Len Brown4f86d3a2007-10-03 18:58:00 -040068#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
70#define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
Andreas Mohrb6835052006-04-27 05:25:00 -040071static void (*pm_idle_save) (void) __read_mostly;
Len Brown4f86d3a2007-10-03 18:58:00 -040072#else
73#define C2_OVERHEAD 1 /* 1us */
74#define C3_OVERHEAD 1 /* 1us */
75#endif
76#define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Len Brown4f86d3a2007-10-03 18:58:00 -040078static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050079#ifdef CONFIG_CPU_IDLE
Len Brown4f86d3a2007-10-03 18:58:00 -040080module_param(max_cstate, uint, 0000);
Venki Pallipadi5b3f0e62008-01-07 17:50:10 -050081#else
82module_param(max_cstate, uint, 0644);
83#endif
Andreas Mohrb6835052006-04-27 05:25:00 -040084static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085module_param(nocst, uint, 0000);
86
Len Brown4f86d3a2007-10-03 18:58:00 -040087#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070088/*
89 * bm_history -- bit-mask with a bit per jiffy of bus-master activity
90 * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
91 * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
92 * 100 HZ: 0x0000000F: 4 jiffies = 40ms
93 * reduce history for more aggressive entry into C3
94 */
Andreas Mohrb6835052006-04-27 05:25:00 -040095static unsigned int bm_history __read_mostly =
Len Brown4be44fc2005-08-05 00:44:28 -040096 (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070097module_param(bm_history, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -040098
99static int acpi_processor_set_power_policy(struct acpi_processor *pr);
100
Len Brown4963f622007-12-13 23:50:45 -0500101#else /* CONFIG_CPU_IDLE */
Len Brown25de5712007-12-14 00:24:15 -0500102static unsigned int latency_factor __read_mostly = 2;
Len Brown4963f622007-12-13 23:50:45 -0500103module_param(latency_factor, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -0400104#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/*
107 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
108 * For now disable this. Probably a bug somewhere else.
109 *
110 * To skip this limit, boot/load with a large max_cstate limit.
111 */
Jeff Garzik18552562007-10-03 15:15:40 -0400112static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
114 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
115 return 0;
116
Len Brown3d356002005-08-03 00:22:52 -0400117 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
Len Brown4be44fc2005-08-05 00:44:28 -0400118 " Override with \"processor.max_cstate=%d\"\n", id->ident,
119 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Len Brown3d356002005-08-03 00:22:52 -0400121 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
123 return 0;
124}
125
Ashok Raj7ded5682006-02-03 21:51:23 +0100126/* Actually this shouldn't be __cpuinitdata, would be better to fix the
127 callers to only run once -AK */
128static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -0500129 { set_max_cstate, "IBM ThinkPad R40e", {
130 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Bartlomiej Swierczf8313352006-05-29 07:16:00 -0400131 DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
132 { set_max_cstate, "IBM ThinkPad R40e", {
133 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Thomas Rosner876c1842006-01-06 01:31:00 -0500134 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
135 { set_max_cstate, "IBM ThinkPad R40e", {
136 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
137 DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
138 { set_max_cstate, "IBM ThinkPad R40e", {
139 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
140 DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
141 { set_max_cstate, "IBM ThinkPad R40e", {
142 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
143 DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
144 { set_max_cstate, "IBM ThinkPad R40e", {
145 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
146 DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
147 { set_max_cstate, "IBM ThinkPad R40e", {
148 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
149 DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
150 { set_max_cstate, "IBM ThinkPad R40e", {
151 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
152 DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
153 { set_max_cstate, "IBM ThinkPad R40e", {
154 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
155 DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
156 { set_max_cstate, "IBM ThinkPad R40e", {
157 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
158 DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
159 { set_max_cstate, "IBM ThinkPad R40e", {
160 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
161 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
162 { set_max_cstate, "IBM ThinkPad R40e", {
163 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
164 DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
165 { set_max_cstate, "IBM ThinkPad R40e", {
166 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
167 DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
168 { set_max_cstate, "IBM ThinkPad R40e", {
169 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
170 DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
171 { set_max_cstate, "IBM ThinkPad R40e", {
172 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
173 DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
174 { set_max_cstate, "IBM ThinkPad R40e", {
175 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
176 DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
177 { set_max_cstate, "Medion 41700", {
178 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
179 DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
180 { set_max_cstate, "Clevo 5600D", {
181 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
182 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
Len Brown4be44fc2005-08-05 00:44:28 -0400183 (void *)2},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 {},
185};
186
Len Brown4be44fc2005-08-05 00:44:28 -0400187static inline u32 ticks_elapsed(u32 t1, u32 t2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
189 if (t2 >= t1)
190 return (t2 - t1);
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300191 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
193 else
194 return ((0xFFFFFFFF - t1) + t2);
195}
196
Len Brown4f86d3a2007-10-03 18:58:00 -0400197static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
198{
199 if (t2 >= t1)
200 return PM_TIMER_TICKS_TO_US(t2 - t1);
201 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
202 return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
203 else
204 return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
205}
206
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -0800207/*
208 * Callers should disable interrupts before the call and enable
209 * interrupts after return.
210 */
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500211static void acpi_safe_halt(void)
212{
213 current_thread_info()->status &= ~TS_POLLING;
214 /*
215 * TS_POLLING-cleared state must be visible before we
216 * test NEED_RESCHED:
217 */
218 smp_mb();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700219 if (!need_resched()) {
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500220 safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700221 local_irq_disable();
222 }
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -0500223 current_thread_info()->status |= TS_POLLING;
224}
225
Len Brown4f86d3a2007-10-03 18:58:00 -0400226#ifndef CONFIG_CPU_IDLE
227
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228static void
Len Brown4be44fc2005-08-05 00:44:28 -0400229acpi_processor_power_activate(struct acpi_processor *pr,
230 struct acpi_processor_cx *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231{
Len Brown4be44fc2005-08-05 00:44:28 -0400232 struct acpi_processor_cx *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233
234 if (!pr || !new)
235 return;
236
237 old = pr->power.state;
238
239 if (old)
240 old->promotion.count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400241 new->demotion.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /* Cleanup from old state. */
244 if (old) {
245 switch (old->type) {
246 case ACPI_STATE_C3:
247 /* Disable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400248 if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300249 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 break;
251 }
252 }
253
254 /* Prepare to use new state. */
255 switch (new->type) {
256 case ACPI_STATE_C3:
257 /* Enable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400258 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300259 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 break;
261 }
262
263 pr->power.state = new;
264
265 return;
266}
267
Len Brown4be44fc2005-08-05 00:44:28 -0400268static atomic_t c3_cpu_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700270/* Common C-state entry for C2, C3, .. */
271static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
272{
Ingo Molnar01b28382008-12-11 13:45:51 +0100273 u64 perf_flags;
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100274
Steven Rostedtdcf30992008-07-25 18:00:42 -0400275 /* Don't trace irqs off for idle */
276 stop_critical_timings();
Ingo Molnar01b28382008-12-11 13:45:51 +0100277 perf_flags = hw_perf_save_disable();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800278 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700279 /* Call into architectural FFH based C-state */
280 acpi_processor_ffh_cstate_enter(cstate);
281 } else {
282 int unused;
283 /* IO port based C-state */
284 inb(cstate->address);
285 /* Dummy wait op - must do something useless after P_LVL2 read
286 because chipsets cannot guarantee that STPCLK# signal
287 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300288 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700289 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100290 hw_perf_restore(perf_flags);
Steven Rostedtdcf30992008-07-25 18:00:42 -0400291 start_critical_timings();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700292}
Len Brown4f86d3a2007-10-03 18:58:00 -0400293#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700294
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800295#ifdef ARCH_APICTIMER_STOPS_ON_C3
296
297/*
298 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700299 * This seems to be a common problem on AMD boxen, but other vendors
300 * are affected too. We pick the most conservative approach: we assume
301 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800302 */
303static void acpi_timer_check_state(int state, struct acpi_processor *pr,
304 struct acpi_processor_cx *cx)
305{
306 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100307 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800308
309 /*
310 * Check, if one of the previous states already marked the lapic
311 * unstable
312 */
313 if (pwr->timer_broadcast_on_state < state)
314 return;
315
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100316 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700317 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800318}
319
320static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
321{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800322 unsigned long reason;
323
324 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
325 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
326
327 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800328}
329
330/* Power(C) State timer broadcast control */
331static void acpi_state_timer_broadcast(struct acpi_processor *pr,
332 struct acpi_processor_cx *cx,
333 int broadcast)
334{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800335 int state = cx - pr->power.states;
336
337 if (state >= pr->power.timer_broadcast_on_state) {
338 unsigned long reason;
339
340 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
341 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
342 clockevents_notify(reason, &pr->id);
343 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800344}
345
346#else
347
348static void acpi_timer_check_state(int state, struct acpi_processor *pr,
349 struct acpi_processor_cx *cstate) { }
350static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800351static void acpi_state_timer_broadcast(struct acpi_processor *pr,
352 struct acpi_processor_cx *cx,
353 int broadcast)
354{
355}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800356
357#endif
358
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000359/*
360 * Suspend / resume control
361 */
362static int acpi_idle_suspend;
363
364int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
365{
366 acpi_idle_suspend = 1;
367 return 0;
368}
369
370int acpi_processor_resume(struct acpi_device * device)
371{
372 acpi_idle_suspend = 0;
373 return 0;
374}
375
Pavel Machek61331162008-02-19 11:00:29 +0100376#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100377static int tsc_halts_in_c(int state)
378{
379 switch (boot_cpu_data.x86_vendor) {
380 case X86_VENDOR_AMD:
381 /*
382 * AMD Fam10h TSC will tick in all
383 * C/P/S0/S1 states when this bit is set.
384 */
385 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
386 return 0;
387 /*FALL THROUGH*/
388 case X86_VENDOR_INTEL:
389 /* Several cases known where TSC halts in C2 too */
390 default:
391 return state > ACPI_STATE_C1;
392 }
393}
394#endif
395
Len Brown4f86d3a2007-10-03 18:58:00 -0400396#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400397static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
Len Brown4be44fc2005-08-05 00:44:28 -0400399 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct acpi_processor_cx *cx = NULL;
401 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400402 int sleep_ticks = 0;
403 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Interrupts must be disabled during bus mastering calculations and
407 * for C2/C3 transitions.
408 */
409 local_irq_disable();
410
Mike Travis706546d2008-06-09 16:22:23 -0700411 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400412 if (!pr) {
413 local_irq_enable();
414 return;
415 }
416
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 /*
418 * Check whether we truly need to go idle, or should
419 * reschedule:
420 */
421 if (unlikely(need_resched())) {
422 local_irq_enable();
423 return;
424 }
425
426 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000427 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200428 if (pm_idle_save) {
429 pm_idle_save(); /* enables IRQs */
430 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800431 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700432 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200433 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700434
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800435 return;
436 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437
438 /*
439 * Check BM Activity
440 * -----------------
441 * Check for bus mastering activity (if required), record, and check
442 * for demotion.
443 */
444 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400445 u32 bm_status = 0;
446 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400448 if (diff > 31)
449 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400451 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452
Bob Moored8c71b62007-02-02 19:48:21 +0300453 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400455 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300456 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458 /*
459 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
460 * the true state of bus mastering activity; forcing us to
461 * manually check the BMIDEA bit of each IDE channel.
462 */
463 else if (errata.piix4.bmisx) {
464 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400465 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400466 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 pr->power.bm_check_timestamp = jiffies;
470
471 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400472 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 * to avoid a faulty transition. Note that the processor
474 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400475 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476 *
477 * TBD: A better policy might be to fallback to the demotion
478 * state (use it for this quantum only) istead of
479 * demoting -- and rely on duration as our sole demotion
480 * qualification. This may, however, introduce DMA
481 * issues (e.g. floppy DMA transfer overrun/underrun).
482 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400483 if ((pr->power.bm_activity & 0x1) &&
484 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485 local_irq_enable();
486 next_state = cx->demotion.state;
487 goto end;
488 }
489 }
490
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400491#ifdef CONFIG_HOTPLUG_CPU
492 /*
493 * Check for P_LVL2_UP flag before entering C2 and above on
494 * an SMP system. We do it here instead of doing it at _CST/P_LVL
495 * detection phase, to work cleanly with logical CPU hotplug.
496 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400497 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300498 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500499 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400500#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 /*
503 * Sleep:
504 * ------
505 * Invoke the current Cx state to put the processor to sleep.
506 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100507 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200508 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800509 /*
510 * TS_POLLING-cleared state must be visible before we
511 * test NEED_RESCHED:
512 */
513 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100514 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200515 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800516 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100517 return;
518 }
519 }
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 switch (cx->type) {
522
523 case ACPI_STATE_C1:
524 /*
525 * Invoke C1.
526 * Use the appropriate idle routine, the one that would
527 * be used without acpi C-states.
528 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200529 if (pm_idle_save) {
530 pm_idle_save(); /* enables IRQs */
531 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800532 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200533 local_irq_enable();
534 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800535
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400537 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 * go to an ISR rather than here. Need to instrument
539 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200540 *
541 * Note: the TSC better not stop in C1, sched_clock() will
542 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 */
544 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700545
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 break;
547
548 case ACPI_STATE_C2:
549 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300550 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200551 /* Tell the scheduler that we are going deep-idle: */
552 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800554 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700555 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300557 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700558
Pavel Machek61331162008-02-19 11:00:29 +0100559#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700560 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100561 if (tsc_halts_in_c(ACPI_STATE_C2))
562 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700563#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200564 /* Compute time (ticks) that we were actually asleep */
565 sleep_ticks = ticks_elapsed(t1, t2);
566
567 /* Tell the scheduler how much we idled: */
568 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
569
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 /* Re-enable interrupts */
571 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200572 /* Do not account our idle-switching overhead: */
573 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
574
Andi Kleen495ab9c2006-06-26 13:59:11 +0200575 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800576 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 break;
578
579 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100580 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400581 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100582 * Must be done before busmaster disable as we might
583 * need to access HPET !
584 */
585 acpi_state_timer_broadcast(pr, cx, 1);
586 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400587 * disable bus master
588 * bm_check implies we need ARB_DIS
589 * !bm_check implies we need cache flush
590 * bm_control implies whether we can do ARB_DIS
591 *
592 * That leaves a case where bm_check is set and bm_control is
593 * not set. In that case we cannot do much, we enter C3
594 * without doing anything.
595 */
596 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400597 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400598 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400599 /*
600 * All CPUs are trying to go to C3
601 * Disable bus master arbitration
602 */
Bob Moored8c71b62007-02-02 19:48:21 +0300603 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400604 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400605 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400606 /* SMP with no shared cache... Invalidate cache */
607 ACPI_FLUSH_CPU_CACHE();
608 }
Len Brown4be44fc2005-08-05 00:44:28 -0400609
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300611 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200613 /* Tell the scheduler that we are going deep-idle: */
614 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700615 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300617 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400618 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400619 /* Enable bus master arbitration */
620 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300621 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400622 }
623
Pavel Machek61331162008-02-19 11:00:29 +0100624#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700625 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100626 if (tsc_halts_in_c(ACPI_STATE_C3))
627 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700628#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200629 /* Compute time (ticks) that we were actually asleep */
630 sleep_ticks = ticks_elapsed(t1, t2);
631 /* Tell the scheduler how much we idled: */
632 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
633
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 /* Re-enable interrupts */
635 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200636 /* Do not account our idle-switching overhead: */
637 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
638
Andi Kleen495ab9c2006-06-26 13:59:11 +0200639 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800640 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 break;
642
643 default:
644 local_irq_enable();
645 return;
646 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400647 cx->usage++;
648 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
649 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
651 next_state = pr->power.state;
652
David Shaohua Li1e483962005-12-01 17:00:00 -0500653#ifdef CONFIG_HOTPLUG_CPU
654 /* Don't do promotion/demotion */
655 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300656 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500657 next_state = cx;
658 goto end;
659 }
660#endif
661
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Promotion?
664 * ----------
665 * Track the number of longs (time asleep is greater than threshold)
666 * and promote when the count threshold is reached. Note that bus
667 * mastering activity may prevent promotions.
668 * Do not promote above max_cstate.
669 */
670 if (cx->promotion.state &&
671 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700672 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800673 cx->promotion.state->latency <=
674 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400676 cx->demotion.count = 0;
677 if (cx->promotion.count >=
678 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400680 if (!
681 (pr->power.bm_activity & cx->
682 promotion.threshold.bm)) {
683 next_state =
684 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 goto end;
686 }
Len Brown4be44fc2005-08-05 00:44:28 -0400687 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 next_state = cx->promotion.state;
689 goto end;
690 }
691 }
692 }
693 }
694
695 /*
696 * Demotion?
697 * ---------
698 * Track the number of shorts (time asleep is less than time threshold)
699 * and demote when the usage threshold is reached.
700 */
701 if (cx->demotion.state) {
702 if (sleep_ticks < cx->demotion.threshold.ticks) {
703 cx->demotion.count++;
704 cx->promotion.count = 0;
705 if (cx->demotion.count >= cx->demotion.threshold.count) {
706 next_state = cx->demotion.state;
707 goto end;
708 }
709 }
710 }
711
Len Brown4be44fc2005-08-05 00:44:28 -0400712 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 /*
714 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700715 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700717 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800718 pr->power.state->latency >
719 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 if (cx->demotion.state)
721 next_state = cx->demotion.state;
722 }
723
724 /*
725 * New Cx State?
726 * -------------
727 * If we're going to start using a new Cx state we must clean up
728 * from the previous and prepare to use the new.
729 */
730 if (next_state != pr->power.state)
731 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732}
733
Len Brown4be44fc2005-08-05 00:44:28 -0400734static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
736 unsigned int i;
737 unsigned int state_is_set = 0;
738 struct acpi_processor_cx *lower = NULL;
739 struct acpi_processor_cx *higher = NULL;
740 struct acpi_processor_cx *cx;
741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400744 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
746 /*
747 * This function sets the default Cx state policy (OS idle handler).
748 * Our scheme is to promote quickly to C2 but more conservatively
749 * to C3. We're favoring C2 for its characteristics of low latency
750 * (quick response), good power savings, and ability to allow bus
751 * mastering activity. Note that the Cx state policy is completely
752 * customizable and can be altered dynamically.
753 */
754
755 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400756 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 cx = &pr->power.states[i];
758 if (!cx->valid)
759 continue;
760
761 if (!state_is_set)
762 pr->power.state = cx;
763 state_is_set++;
764 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400768 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400771 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 cx = &pr->power.states[i];
773 if (!cx->valid)
774 continue;
775
776 if (lower) {
777 cx->demotion.state = lower;
778 cx->demotion.threshold.ticks = cx->latency_ticks;
779 cx->demotion.threshold.count = 1;
780 if (cx->type == ACPI_STATE_C3)
781 cx->demotion.threshold.bm = bm_history;
782 }
783
784 lower = cx;
785 }
786
787 /* promotion */
788 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
789 cx = &pr->power.states[i];
790 if (!cx->valid)
791 continue;
792
793 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400794 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 cx->promotion.threshold.ticks = cx->latency_ticks;
796 if (cx->type >= ACPI_STATE_C2)
797 cx->promotion.threshold.count = 4;
798 else
799 cx->promotion.threshold.count = 10;
800 if (higher->type == ACPI_STATE_C3)
801 cx->promotion.threshold.bm = bm_history;
802 }
803
804 higher = cx;
805 }
806
Patrick Mocheld550d982006-06-27 00:41:40 -0400807 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808}
Len Brown4f86d3a2007-10-03 18:58:00 -0400809#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Len Brown4be44fc2005-08-05 00:44:28 -0400811static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400818 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
822 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
823
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400824#ifndef CONFIG_HOTPLUG_CPU
825 /*
826 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400827 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400828 */
Alexey Starikovskiyad718602007-02-02 19:48:19 +0300829 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300830 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400831 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400832#endif
833
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 /* determine C2 and C3 address from pblk */
835 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
836 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
837
838 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300839 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
840 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841
842 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
843 "lvl2[0x%08x] lvl3[0x%08x]\n",
844 pr->power.states[ACPI_STATE_C2].address,
845 pr->power.states[ACPI_STATE_C3].address));
846
Patrick Mocheld550d982006-06-27 00:41:40 -0400847 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
849
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700850static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500851{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700852 if (!pr->power.states[ACPI_STATE_C1].valid) {
853 /* set the first C-State to C1 */
854 /* all processors need to support C1 */
855 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
856 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400857 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700858 }
859 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500860 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400861 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500862}
863
Len Brown4be44fc2005-08-05 00:44:28 -0400864static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865{
Len Brown4be44fc2005-08-05 00:44:28 -0400866 acpi_status status = 0;
867 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400868 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400869 int i;
870 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
871 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400875 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700877 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878
879 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
880 if (ACPI_FAILURE(status)) {
881 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400882 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400883 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700884
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200885 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700886
887 /* There must be at least 2 elements */
888 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400889 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 status = -EFAULT;
891 goto end;
892 }
893
894 count = cst->package.elements[0].integer.value;
895
896 /* Validate number of power states. */
897 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400898 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 status = -EFAULT;
900 goto end;
901 }
902
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 /* Tell driver that at least _CST is supported. */
904 pr->flags.has_cst = 1;
905
906 for (i = 1; i <= count; i++) {
907 union acpi_object *element;
908 union acpi_object *obj;
909 struct acpi_power_register *reg;
910 struct acpi_processor_cx cx;
911
912 memset(&cx, 0, sizeof(cx));
913
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200914 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (element->type != ACPI_TYPE_PACKAGE)
916 continue;
917
918 if (element->package.count != 4)
919 continue;
920
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200921 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922
923 if (obj->type != ACPI_TYPE_BUFFER)
924 continue;
925
Len Brown4be44fc2005-08-05 00:44:28 -0400926 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400929 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 continue;
931
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200933 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 if (obj->type != ACPI_TYPE_INTEGER)
935 continue;
936
937 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700938 /*
939 * Some buggy BIOSes won't list C1 in _CST -
940 * Let acpi_processor_get_power_info_default() handle them later
941 */
942 if (i == 1 && cx.type != ACPI_STATE_C1)
943 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700945 cx.address = reg->address;
946 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800948 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700949 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
950 if (acpi_processor_ffh_cstate_probe
951 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800952 cx.entry_method = ACPI_CSTATE_FFH;
953 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700954 /*
955 * C1 is a special case where FIXED_HARDWARE
956 * can be handled in non-MWAIT way as well.
957 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700958 * Otherwise, ignore this info and continue.
959 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800960 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800961 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800962 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700963 continue;
964 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800965 if (cx.type == ACPI_STATE_C1 &&
966 (idle_halt || idle_nomwait)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800967 /*
968 * In most cases the C1 space_id obtained from
969 * _CST object is FIXED_HARDWARE access mode.
970 * But when the option of idle=halt is added,
971 * the entry_method type should be changed from
972 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800973 * When the option of idle=nomwait is added,
974 * the C1 entry_method type should be
975 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800976 */
977 cx.entry_method = ACPI_CSTATE_HALT;
978 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
979 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800980 } else {
981 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
982 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700983 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400985 if (cx.type == ACPI_STATE_C1) {
986 cx.valid = 1;
987 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800988
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200989 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 if (obj->type != ACPI_TYPE_INTEGER)
991 continue;
992
993 cx.latency = obj->integer.value;
994
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200995 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996 if (obj->type != ACPI_TYPE_INTEGER)
997 continue;
998
999 cx.power = obj->integer.value;
1000
Janosch Machowinskicf824782005-08-20 08:02:00 -04001001 current_count++;
1002 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
1003
1004 /*
1005 * We support total ACPI_PROCESSOR_MAX_POWER - 1
1006 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
1007 */
1008 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1009 printk(KERN_WARNING
1010 "Limiting number of power states to max (%d)\n",
1011 ACPI_PROCESSOR_MAX_POWER);
1012 printk(KERN_WARNING
1013 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1014 break;
1015 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Len Brown4be44fc2005-08-05 00:44:28 -04001018 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -04001019 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020
1021 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -04001022 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001023 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Len Brown4be44fc2005-08-05 00:44:28 -04001025 end:
Len Brown02438d82006-06-30 03:19:10 -04001026 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027
Patrick Mocheld550d982006-06-27 00:41:40 -04001028 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029}
1030
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1032{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001035 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036
1037 /*
1038 * C2 latency must be less than or equal to 100
1039 * microseconds.
1040 */
1041 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1042 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001043 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001044 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 }
1046
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 /*
1048 * Otherwise we've met all of our C2 requirements.
1049 * Normalize the C2 latency to expidite policy
1050 */
1051 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001052
1053#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001055#else
1056 cx->latency_ticks = cx->latency;
1057#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Patrick Mocheld550d982006-06-27 00:41:40 -04001059 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060}
1061
Len Brown4be44fc2005-08-05 00:44:28 -04001062static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1063 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001065 static int bm_check_flag;
1066
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001069 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001070
1071 /*
1072 * C3 latency must be less than or equal to 1000
1073 * microseconds.
1074 */
1075 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1076 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001077 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001078 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 }
1080
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 /*
1082 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1083 * DMA transfers are used by any ISA device to avoid livelock.
1084 * Note that we could disable Type-F DMA (as recommended by
1085 * the erratum), but this is known to disrupt certain ISA
1086 * devices thus we take the conservative approach.
1087 */
1088 else if (errata.piix4.fdma) {
1089 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001090 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001091 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001092 }
1093
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001094 /* All the logic here assumes flags.bm_check is same across all CPUs */
1095 if (!bm_check_flag) {
1096 /* Determine whether bm_check is needed based on CPU */
1097 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1098 bm_check_flag = pr->flags.bm_check;
1099 } else {
1100 pr->flags.bm_check = bm_check_flag;
1101 }
1102
1103 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001104 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001105 if (pr->flags.has_cst != 1) {
1106 /* bus mastering control is necessary */
1107 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1108 "C3 support requires BM control\n"));
1109 return;
1110 } else {
1111 /* Here we enter C3 without bus mastering */
1112 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1113 "C3 support without BM control\n"));
1114 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001115 }
1116 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001117 /*
1118 * WBINVD should be set in fadt, for C3 state to be
1119 * supported on when bm_check is not required.
1120 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001121 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001122 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001123 "Cache invalidation should work properly"
1124 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001125 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001126 }
Bob Moored8c71b62007-02-02 19:48:21 +03001127 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001128 }
1129
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 /*
1131 * Otherwise we've met all of our C3 requirements.
1132 * Normalize the C3 latency to expidite policy. Enable
1133 * checking of bus mastering status (bm_check) so we can
1134 * use this in our C3 policy
1135 */
1136 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001137
1138#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001140#else
1141 cx->latency_ticks = cx->latency;
1142#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143
Patrick Mocheld550d982006-06-27 00:41:40 -04001144 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
1146
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147static int acpi_processor_power_verify(struct acpi_processor *pr)
1148{
1149 unsigned int i;
1150 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001151
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001152 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001153
Len Brown4be44fc2005-08-05 00:44:28 -04001154 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 struct acpi_processor_cx *cx = &pr->power.states[i];
1156
1157 switch (cx->type) {
1158 case ACPI_STATE_C1:
1159 cx->valid = 1;
1160 break;
1161
1162 case ACPI_STATE_C2:
1163 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001164 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001165 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 break;
1167
1168 case ACPI_STATE_C3:
1169 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001170 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001171 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 break;
1173 }
1174
1175 if (cx->valid)
1176 working++;
1177 }
1178
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001179 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001180
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 return (working);
1182}
1183
Len Brown4be44fc2005-08-05 00:44:28 -04001184static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185{
1186 unsigned int i;
1187 int result;
1188
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 /* NOTE: the idle thread may not be running while calling
1191 * this function */
1192
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001193 /* Zero initialize all the C-states info. */
1194 memset(pr->power.states, 0, sizeof(pr->power.states));
1195
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001197 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001198 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001199
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001200 if (result)
1201 return result;
1202
1203 acpi_processor_get_power_info_default(pr);
1204
Janosch Machowinskicf824782005-08-20 08:02:00 -04001205 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206
Len Brown4f86d3a2007-10-03 18:58:00 -04001207#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 /*
1209 * Set Default Policy
1210 * ------------------
1211 * Now that we know which states are supported, set the default
1212 * policy. Note that this policy can be changed dynamically
1213 * (e.g. encourage deeper sleeps to conserve battery life when
1214 * not on AC).
1215 */
1216 result = acpi_processor_set_power_policy(pr);
1217 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001218 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001219#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
1221 /*
1222 * if one state of type C2 or C3 is available, mark this
1223 * CPU as being "idle manageable"
1224 */
1225 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001226 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001227 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001228 if (pr->power.states[i].type >= ACPI_STATE_C2)
1229 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001230 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 }
1232
Patrick Mocheld550d982006-06-27 00:41:40 -04001233 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234}
1235
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1237{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001238 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001239 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
1242 if (!pr)
1243 goto end;
1244
1245 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001246 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001247 "bus master activity: %08x\n"
1248 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001249 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001250 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001251 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252
1253 seq_puts(seq, "states:\n");
1254
1255 for (i = 1; i <= pr->power.count; i++) {
1256 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001257 (&pr->power.states[i] ==
1258 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259
1260 if (!pr->power.states[i].valid) {
1261 seq_puts(seq, "<not supported>\n");
1262 continue;
1263 }
1264
1265 switch (pr->power.states[i].type) {
1266 case ACPI_STATE_C1:
1267 seq_printf(seq, "type[C1] ");
1268 break;
1269 case ACPI_STATE_C2:
1270 seq_printf(seq, "type[C2] ");
1271 break;
1272 case ACPI_STATE_C3:
1273 seq_printf(seq, "type[C3] ");
1274 break;
1275 default:
1276 seq_printf(seq, "type[--] ");
1277 break;
1278 }
1279
1280 if (pr->power.states[i].promotion.state)
1281 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001282 (pr->power.states[i].promotion.state -
1283 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 else
1285 seq_puts(seq, "promotion[--] ");
1286
1287 if (pr->power.states[i].demotion.state)
1288 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001289 (pr->power.states[i].demotion.state -
1290 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001291 else
1292 seq_puts(seq, "demotion[--] ");
1293
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001294 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001295 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001296 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001297 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 }
1299
Len Brown4be44fc2005-08-05 00:44:28 -04001300 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001301 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302}
1303
1304static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1305{
1306 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001307 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308}
1309
Arjan van de Vend7508032006-07-04 13:06:00 -04001310static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001311 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001312 .open = acpi_processor_power_open_fs,
1313 .read = seq_read,
1314 .llseek = seq_lseek,
1315 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316};
1317
Len Brown4f86d3a2007-10-03 18:58:00 -04001318#ifndef CONFIG_CPU_IDLE
1319
1320int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1321{
1322 int result = 0;
1323
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001324 if (boot_option_idle_override)
1325 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001326
1327 if (!pr)
1328 return -EINVAL;
1329
1330 if (nocst) {
1331 return -ENODEV;
1332 }
1333
1334 if (!pr->flags.power_setup_done)
1335 return -ENODEV;
1336
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001337 /*
1338 * Fall back to the default idle loop, when pm_idle_save had
1339 * been initialized.
1340 */
1341 if (pm_idle_save) {
1342 pm_idle = pm_idle_save;
1343 /* Relies on interrupts forcing exit from idle. */
1344 synchronize_sched();
1345 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001346
1347 pr->flags.power = 0;
1348 result = acpi_processor_get_power_info(pr);
1349 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1350 pm_idle = acpi_processor_idle;
1351
1352 return result;
1353}
1354
Andrew Morton1fec74a2006-10-17 00:09:58 -07001355#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001356static void smp_callback(void *v)
1357{
1358 /* we already woke the CPU up, nothing more to do */
1359}
1360
1361/*
1362 * This function gets called when a part of the kernel has a new latency
1363 * requirement. This means we need to get all processors out of their C-state,
1364 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1365 * wakes them all right up.
1366 */
1367static int acpi_processor_latency_notify(struct notifier_block *b,
1368 unsigned long l, void *v)
1369{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001370 smp_call_function(smp_callback, NULL, 1);
Arjan van de Ven5c875792006-09-30 23:27:17 -07001371 return NOTIFY_OK;
1372}
1373
1374static struct notifier_block acpi_processor_latency_notifier = {
1375 .notifier_call = acpi_processor_latency_notify,
1376};
Len Brown4f86d3a2007-10-03 18:58:00 -04001377
Andrew Morton1fec74a2006-10-17 00:09:58 -07001378#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001379
Len Brown4f86d3a2007-10-03 18:58:00 -04001380#else /* CONFIG_CPU_IDLE */
1381
1382/**
1383 * acpi_idle_bm_check - checks if bus master activity was detected
1384 */
1385static int acpi_idle_bm_check(void)
1386{
1387 u32 bm_status = 0;
1388
1389 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1390 if (bm_status)
1391 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1392 /*
1393 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1394 * the true state of bus mastering activity; forcing us to
1395 * manually check the BMIDEA bit of each IDE channel.
1396 */
1397 else if (errata.piix4.bmisx) {
1398 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1399 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1400 bm_status = 1;
1401 }
1402 return bm_status;
1403}
1404
1405/**
1406 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1407 * @pr: the processor
1408 * @target: the new target state
1409 */
1410static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1411 struct acpi_processor_cx *target)
1412{
1413 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1414 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1415 pr->flags.bm_rld_set = 0;
1416 }
1417
1418 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1419 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1420 pr->flags.bm_rld_set = 1;
1421 }
1422}
1423
1424/**
1425 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1426 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001427 *
1428 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001429 */
1430static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1431{
Thomas Gleixner4ac13292008-12-09 21:43:39 +01001432 u64 pctrl;
1433
Steven Rostedtdcf30992008-07-25 18:00:42 -04001434 /* Don't trace irqs off for idle */
1435 stop_critical_timings();
Ingo Molnar01b28382008-12-11 13:45:51 +01001436 pctrl = hw_perf_save_disable();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001437 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001438 /* Call into architectural FFH based C-state */
1439 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001440 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1441 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001442 } else {
1443 int unused;
1444 /* IO port based C-state */
1445 inb(cx->address);
1446 /* Dummy wait op - must do something useless after P_LVL2 read
1447 because chipsets cannot guarantee that STPCLK# signal
1448 gets asserted in time to freeze execution properly. */
1449 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1450 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001451 hw_perf_restore(pctrl);
Steven Rostedtdcf30992008-07-25 18:00:42 -04001452 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -04001453}
1454
1455/**
1456 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1457 * @dev: the target CPU
1458 * @state: the state data
1459 *
1460 * This is equivalent to the HALT instruction.
1461 */
1462static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1463 struct cpuidle_state *state)
1464{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001465 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001466 struct acpi_processor *pr;
1467 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001468
Mike Travis706546d2008-06-09 16:22:23 -07001469 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001470
1471 if (unlikely(!pr))
1472 return 0;
1473
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001474 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001475
1476 /* Do not access any ACPI IO ports in suspend path */
1477 if (acpi_idle_suspend) {
1478 acpi_safe_halt();
1479 local_irq_enable();
1480 return 0;
1481 }
1482
Len Brown4f86d3a2007-10-03 18:58:00 -04001483 if (pr->flags.bm_check)
1484 acpi_idle_update_bm_rld(pr, cx);
1485
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001486 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001487 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001488 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001489
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001490 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001491 cx->usage++;
1492
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001493 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001494}
1495
1496/**
1497 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1498 * @dev: the target CPU
1499 * @state: the state data
1500 */
1501static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1502 struct cpuidle_state *state)
1503{
1504 struct acpi_processor *pr;
1505 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1506 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001507 int sleep_ticks = 0;
1508
Mike Travis706546d2008-06-09 16:22:23 -07001509 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001510
1511 if (unlikely(!pr))
1512 return 0;
1513
Len Browne1964412007-10-04 01:23:47 -04001514 if (acpi_idle_suspend)
1515 return(acpi_idle_enter_c1(dev, state));
1516
Len Brown4f86d3a2007-10-03 18:58:00 -04001517 local_irq_disable();
1518 current_thread_info()->status &= ~TS_POLLING;
1519 /*
1520 * TS_POLLING-cleared state must be visible before we test
1521 * NEED_RESCHED:
1522 */
1523 smp_mb();
1524
1525 if (unlikely(need_resched())) {
1526 current_thread_info()->status |= TS_POLLING;
1527 local_irq_enable();
1528 return 0;
1529 }
1530
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001531 /*
1532 * Must be done before busmaster disable as we might need to
1533 * access HPET !
1534 */
1535 acpi_state_timer_broadcast(pr, cx, 1);
1536
1537 if (pr->flags.bm_check)
1538 acpi_idle_update_bm_rld(pr, cx);
1539
Len Brown4f86d3a2007-10-03 18:58:00 -04001540 if (cx->type == ACPI_STATE_C3)
1541 ACPI_FLUSH_CPU_CACHE();
1542
1543 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001544 /* Tell the scheduler that we are going deep-idle: */
1545 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001546 acpi_idle_do_entry(cx);
1547 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1548
Pavel Machek61331162008-02-19 11:00:29 +01001549#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001550 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001551 if (tsc_halts_in_c(cx->type))
1552 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001553#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001554 sleep_ticks = ticks_elapsed(t1, t2);
1555
1556 /* Tell the scheduler how much we idled: */
1557 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001558
1559 local_irq_enable();
1560 current_thread_info()->status |= TS_POLLING;
1561
1562 cx->usage++;
1563
1564 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001565 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001566 return ticks_elapsed_in_us(t1, t2);
1567}
1568
1569static int c3_cpu_count;
1570static DEFINE_SPINLOCK(c3_lock);
1571
1572/**
1573 * acpi_idle_enter_bm - enters C3 with proper BM handling
1574 * @dev: the target CPU
1575 * @state: the state data
1576 *
1577 * If BM is detected, the deepest non-C3 idle state is entered instead.
1578 */
1579static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1580 struct cpuidle_state *state)
1581{
1582 struct acpi_processor *pr;
1583 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1584 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001585 int sleep_ticks = 0;
1586
Mike Travis706546d2008-06-09 16:22:23 -07001587 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001588
1589 if (unlikely(!pr))
1590 return 0;
1591
Len Browne1964412007-10-04 01:23:47 -04001592 if (acpi_idle_suspend)
1593 return(acpi_idle_enter_c1(dev, state));
1594
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001595 if (acpi_idle_bm_check()) {
1596 if (dev->safe_state) {
Venkatesh Pallipadiaddbad42008-09-29 15:24:28 -07001597 dev->last_state = dev->safe_state;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001598 return dev->safe_state->enter(dev, dev->safe_state);
1599 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001600 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001601 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001602 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001603 return 0;
1604 }
1605 }
1606
Len Brown4f86d3a2007-10-03 18:58:00 -04001607 local_irq_disable();
1608 current_thread_info()->status &= ~TS_POLLING;
1609 /*
1610 * TS_POLLING-cleared state must be visible before we test
1611 * NEED_RESCHED:
1612 */
1613 smp_mb();
1614
1615 if (unlikely(need_resched())) {
1616 current_thread_info()->status |= TS_POLLING;
1617 local_irq_enable();
1618 return 0;
1619 }
1620
Venki Pallipadi996520c2008-03-24 14:24:10 -07001621 acpi_unlazy_tlb(smp_processor_id());
1622
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001623 /* Tell the scheduler that we are going deep-idle: */
1624 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001625 /*
1626 * Must be done before busmaster disable as we might need to
1627 * access HPET !
1628 */
1629 acpi_state_timer_broadcast(pr, cx, 1);
1630
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001631 acpi_idle_update_bm_rld(pr, cx);
Len Brown4f86d3a2007-10-03 18:58:00 -04001632
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001633 /*
1634 * disable bus master
1635 * bm_check implies we need ARB_DIS
1636 * !bm_check implies we need cache flush
1637 * bm_control implies whether we can do ARB_DIS
1638 *
1639 * That leaves a case where bm_check is set and bm_control is
1640 * not set. In that case we cannot do much, we enter C3
1641 * without doing anything.
1642 */
1643 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001644 spin_lock(&c3_lock);
1645 c3_cpu_count++;
1646 /* Disable bus master arbitration when all CPUs are in C3 */
1647 if (c3_cpu_count == num_online_cpus())
1648 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1649 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001650 } else if (!pr->flags.bm_check) {
1651 ACPI_FLUSH_CPU_CACHE();
1652 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001653
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001654 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1655 acpi_idle_do_entry(cx);
1656 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001657
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001658 /* Re-enable bus master arbitration */
1659 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001660 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001661 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001662 c3_cpu_count--;
1663 spin_unlock(&c3_lock);
1664 }
1665
Pavel Machek61331162008-02-19 11:00:29 +01001666#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001667 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001668 if (tsc_halts_in_c(ACPI_STATE_C3))
1669 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001670#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001671 sleep_ticks = ticks_elapsed(t1, t2);
1672 /* Tell the scheduler how much we idled: */
1673 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001674
1675 local_irq_enable();
1676 current_thread_info()->status |= TS_POLLING;
1677
1678 cx->usage++;
1679
1680 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001681 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001682 return ticks_elapsed_in_us(t1, t2);
1683}
1684
1685struct cpuidle_driver acpi_idle_driver = {
1686 .name = "acpi_idle",
1687 .owner = THIS_MODULE,
1688};
1689
1690/**
1691 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1692 * @pr: the ACPI processor
1693 */
1694static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1695{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001696 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001697 struct acpi_processor_cx *cx;
1698 struct cpuidle_state *state;
1699 struct cpuidle_device *dev = &pr->power.dev;
1700
1701 if (!pr->flags.power_setup_done)
1702 return -EINVAL;
1703
1704 if (pr->flags.power == 0) {
1705 return -EINVAL;
1706 }
1707
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001708 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001709 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1710 dev->states[i].name[0] = '\0';
1711 dev->states[i].desc[0] = '\0';
1712 }
1713
Len Brown4f86d3a2007-10-03 18:58:00 -04001714 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1715 cx = &pr->power.states[i];
1716 state = &dev->states[count];
1717
1718 if (!cx->valid)
1719 continue;
1720
1721#ifdef CONFIG_HOTPLUG_CPU
1722 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1723 !pr->flags.has_cst &&
1724 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1725 continue;
1726#endif
1727 cpuidle_set_statedata(state, cx);
1728
1729 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001730 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001731 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001732 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001733 state->power_usage = cx->power;
1734
1735 state->flags = 0;
1736 switch (cx->type) {
1737 case ACPI_STATE_C1:
1738 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001739 if (cx->entry_method == ACPI_CSTATE_FFH)
1740 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1741
Len Brown4f86d3a2007-10-03 18:58:00 -04001742 state->enter = acpi_idle_enter_c1;
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_C2:
1747 state->flags |= CPUIDLE_FLAG_BALANCED;
1748 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1749 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001750 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001751 break;
1752
1753 case ACPI_STATE_C3:
1754 state->flags |= CPUIDLE_FLAG_DEEP;
1755 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1756 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1757 state->enter = pr->flags.bm_check ?
1758 acpi_idle_enter_bm :
1759 acpi_idle_enter_simple;
1760 break;
1761 }
1762
1763 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001764 if (count == CPUIDLE_STATE_MAX)
1765 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001766 }
1767
1768 dev->state_count = count;
1769
1770 if (!count)
1771 return -EINVAL;
1772
Len Brown4f86d3a2007-10-03 18:58:00 -04001773 return 0;
1774}
1775
1776int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1777{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001778 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001779
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001780 if (boot_option_idle_override)
1781 return 0;
1782
Len Brown4f86d3a2007-10-03 18:58:00 -04001783 if (!pr)
1784 return -EINVAL;
1785
1786 if (nocst) {
1787 return -ENODEV;
1788 }
1789
1790 if (!pr->flags.power_setup_done)
1791 return -ENODEV;
1792
1793 cpuidle_pause_and_lock();
1794 cpuidle_disable_device(&pr->power.dev);
1795 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001796 if (pr->flags.power) {
1797 acpi_processor_setup_cpuidle(pr);
1798 ret = cpuidle_enable_device(&pr->power.dev);
1799 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001800 cpuidle_resume_and_unlock();
1801
1802 return ret;
1803}
1804
1805#endif /* CONFIG_CPU_IDLE */
1806
Pierre Ossman7af8b662006-10-10 14:20:31 -07001807int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001808 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809{
Len Brown4be44fc2005-08-05 00:44:28 -04001810 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001811 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001812 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001813 unsigned int i;
1814
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001815 if (boot_option_idle_override)
1816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817
1818 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001819 if (idle_halt) {
1820 /*
1821 * When the boot option of "idle=halt" is added, halt
1822 * is used for CPU IDLE.
1823 * In such case C2/C3 is meaningless. So the max_cstate
1824 * is set to one.
1825 */
1826 max_cstate = 1;
1827 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001828 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001829 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001831 printk(KERN_NOTICE
1832 "ACPI: processor limited to max C-state %d\n",
1833 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001835#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1836 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1837 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001838#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 }
1840
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001841 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001842 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001843
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001844 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001845 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001846 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001847 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001848 ACPI_EXCEPTION((AE_INFO, status,
1849 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 }
1851 }
1852
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001854 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855
1856 /*
1857 * Install the idle handler if processor power management is supported.
1858 * Note that we use previously set idle handler will be used on
1859 * platforms that only support C1.
1860 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001861 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001862#ifdef CONFIG_CPU_IDLE
1863 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001864 if (cpuidle_register_device(&pr->power.dev))
1865 return -EIO;
1866#endif
1867
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1869 for (i = 1; i <= pr->power.count; i++)
1870 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001871 printk(" C%d[C%d]", i,
1872 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 printk(")\n");
1874
Len Brown4f86d3a2007-10-03 18:58:00 -04001875#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876 if (pr->id == 0) {
1877 pm_idle_save = pm_idle;
1878 pm_idle = acpi_processor_idle;
1879 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001880#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 }
1882
1883 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001884 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1885 S_IRUGO, acpi_device_dir(device),
1886 &acpi_processor_power_fops,
1887 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001889 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001890 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891}
1892
Len Brown4be44fc2005-08-05 00:44:28 -04001893int acpi_processor_power_exit(struct acpi_processor *pr,
1894 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001896 if (boot_option_idle_override)
1897 return 0;
1898
Len Brown4f86d3a2007-10-03 18:58:00 -04001899#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001900 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001901#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 pr->flags.power_setup_done = 0;
1903
1904 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001905 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1906 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
Len Brown4f86d3a2007-10-03 18:58:00 -04001908#ifndef CONFIG_CPU_IDLE
1909
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 /* Unregister the idle handler when processor #0 is removed. */
1911 if (pr->id == 0) {
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001912 if (pm_idle_save)
1913 pm_idle = pm_idle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914
1915 /*
1916 * We are about to unload the current idle thread pm callback
1917 * (pm_idle), Wait for all processors to update cached/local
1918 * copies of pm_idle before proceeding.
1919 */
1920 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001921#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001922 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1923 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001924#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001926#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Patrick Mocheld550d982006-06-27 00:41:40 -04001928 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929}