blob: 7acb23f830ceba79e0b490da2af74d69fcbfef8a [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
244 /* Cleanup from old state. */
245 if (old) {
246 switch (old->type) {
247 case ACPI_STATE_C3:
248 /* Disable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400249 if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300250 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 break;
252 }
253 }
254
255 /* Prepare to use new state. */
256 switch (new->type) {
257 case ACPI_STATE_C3:
258 /* Enable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400259 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300260 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 break;
262 }
263
264 pr->power.state = new;
265
266 return;
267}
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269static atomic_t c3_cpu_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700271/* Common C-state entry for C2, C3, .. */
272static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
273{
Ingo Molnar01b28382008-12-11 13:45:51 +0100274 u64 perf_flags;
Thomas Gleixner4ac13292008-12-09 21:43:39 +0100275
Steven Rostedtdcf30992008-07-25 18:00:42 -0400276 /* Don't trace irqs off for idle */
277 stop_critical_timings();
Ingo Molnar01b28382008-12-11 13:45:51 +0100278 perf_flags = hw_perf_save_disable();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800279 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700280 /* Call into architectural FFH based C-state */
281 acpi_processor_ffh_cstate_enter(cstate);
282 } else {
283 int unused;
284 /* IO port based C-state */
285 inb(cstate->address);
286 /* Dummy wait op - must do something useless after P_LVL2 read
287 because chipsets cannot guarantee that STPCLK# signal
288 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300289 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700290 }
Ingo Molnar01b28382008-12-11 13:45:51 +0100291 hw_perf_restore(perf_flags);
Steven Rostedtdcf30992008-07-25 18:00:42 -0400292 start_critical_timings();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700293}
Len Brown4f86d3a2007-10-03 18:58:00 -0400294#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700295
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800296#ifdef ARCH_APICTIMER_STOPS_ON_C3
297
298/*
299 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700300 * This seems to be a common problem on AMD boxen, but other vendors
301 * are affected too. We pick the most conservative approach: we assume
302 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800303 */
304static void acpi_timer_check_state(int state, struct acpi_processor *pr,
305 struct acpi_processor_cx *cx)
306{
307 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100308 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800309
310 /*
311 * Check, if one of the previous states already marked the lapic
312 * unstable
313 */
314 if (pwr->timer_broadcast_on_state < state)
315 return;
316
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100317 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700318 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800319}
320
321static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
322{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800323 unsigned long reason;
324
325 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
326 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
327
328 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800329}
330
331/* Power(C) State timer broadcast control */
332static void acpi_state_timer_broadcast(struct acpi_processor *pr,
333 struct acpi_processor_cx *cx,
334 int broadcast)
335{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800336 int state = cx - pr->power.states;
337
338 if (state >= pr->power.timer_broadcast_on_state) {
339 unsigned long reason;
340
341 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
342 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
343 clockevents_notify(reason, &pr->id);
344 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800345}
346
347#else
348
349static void acpi_timer_check_state(int state, struct acpi_processor *pr,
350 struct acpi_processor_cx *cstate) { }
351static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800352static void acpi_state_timer_broadcast(struct acpi_processor *pr,
353 struct acpi_processor_cx *cx,
354 int broadcast)
355{
356}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800357
358#endif
359
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000360/*
361 * Suspend / resume control
362 */
363static int acpi_idle_suspend;
364
365int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
366{
367 acpi_idle_suspend = 1;
368 return 0;
369}
370
371int acpi_processor_resume(struct acpi_device * device)
372{
373 acpi_idle_suspend = 0;
374 return 0;
375}
376
Pavel Machek61331162008-02-19 11:00:29 +0100377#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100378static int tsc_halts_in_c(int state)
379{
380 switch (boot_cpu_data.x86_vendor) {
381 case X86_VENDOR_AMD:
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800382 case X86_VENDOR_INTEL:
Andi Kleenddb25f92008-01-30 13:32:41 +0100383 /*
384 * AMD Fam10h TSC will tick in all
385 * C/P/S0/S1 states when this bit is set.
386 */
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800387 if (boot_cpu_has(X86_FEATURE_NONSTOP_TSC))
Andi Kleenddb25f92008-01-30 13:32:41 +0100388 return 0;
Venki Pallipadi40fb1712008-11-17 16:11:37 -0800389
Andi Kleenddb25f92008-01-30 13:32:41 +0100390 /*FALL THROUGH*/
Andi Kleenddb25f92008-01-30 13:32:41 +0100391 default:
392 return state > ACPI_STATE_C1;
393 }
394}
395#endif
396
Len Brown4f86d3a2007-10-03 18:58:00 -0400397#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400398static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399{
Len Brown4be44fc2005-08-05 00:44:28 -0400400 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 struct acpi_processor_cx *cx = NULL;
402 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400403 int sleep_ticks = 0;
404 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Interrupts must be disabled during bus mastering calculations and
408 * for C2/C3 transitions.
409 */
410 local_irq_disable();
411
Mike Travis706546d2008-06-09 16:22:23 -0700412 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400413 if (!pr) {
414 local_irq_enable();
415 return;
416 }
417
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 /*
419 * Check whether we truly need to go idle, or should
420 * reschedule:
421 */
422 if (unlikely(need_resched())) {
423 local_irq_enable();
424 return;
425 }
426
427 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000428 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200429 if (pm_idle_save) {
430 pm_idle_save(); /* enables IRQs */
431 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800432 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700433 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200434 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700435
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800436 return;
437 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 /*
440 * Check BM Activity
441 * -----------------
442 * Check for bus mastering activity (if required), record, and check
443 * for demotion.
444 */
445 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400446 u32 bm_status = 0;
447 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400449 if (diff > 31)
450 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400452 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453
Bob Moored8c71b62007-02-02 19:48:21 +0300454 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400456 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300457 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459 /*
460 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
461 * the true state of bus mastering activity; forcing us to
462 * manually check the BMIDEA bit of each IDE channel.
463 */
464 else if (errata.piix4.bmisx) {
465 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400466 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400467 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 }
469
470 pr->power.bm_check_timestamp = jiffies;
471
472 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400473 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 * to avoid a faulty transition. Note that the processor
475 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400476 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 *
478 * TBD: A better policy might be to fallback to the demotion
479 * state (use it for this quantum only) istead of
480 * demoting -- and rely on duration as our sole demotion
481 * qualification. This may, however, introduce DMA
482 * issues (e.g. floppy DMA transfer overrun/underrun).
483 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400484 if ((pr->power.bm_activity & 0x1) &&
485 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 local_irq_enable();
487 next_state = cx->demotion.state;
488 goto end;
489 }
490 }
491
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400492#ifdef CONFIG_HOTPLUG_CPU
493 /*
494 * Check for P_LVL2_UP flag before entering C2 and above on
495 * an SMP system. We do it here instead of doing it at _CST/P_LVL
496 * detection phase, to work cleanly with logical CPU hotplug.
497 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400498 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300499 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500500 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400501#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /*
504 * Sleep:
505 * ------
506 * Invoke the current Cx state to put the processor to sleep.
507 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100508 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200509 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800510 /*
511 * TS_POLLING-cleared state must be visible before we
512 * test NEED_RESCHED:
513 */
514 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100515 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200516 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800517 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100518 return;
519 }
520 }
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 switch (cx->type) {
523
524 case ACPI_STATE_C1:
525 /*
526 * Invoke C1.
527 * Use the appropriate idle routine, the one that would
528 * be used without acpi C-states.
529 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200530 if (pm_idle_save) {
531 pm_idle_save(); /* enables IRQs */
532 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800533 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200534 local_irq_enable();
535 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400538 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 * go to an ISR rather than here. Need to instrument
540 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200541 *
542 * Note: the TSC better not stop in C1, sched_clock() will
543 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 */
545 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 break;
548
549 case ACPI_STATE_C2:
550 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300551 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200552 /* Tell the scheduler that we are going deep-idle: */
553 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800555 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700556 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300558 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700559
Pavel Machek61331162008-02-19 11:00:29 +0100560#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700561 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100562 if (tsc_halts_in_c(ACPI_STATE_C2))
563 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700564#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200565 /* Compute time (ticks) that we were actually asleep */
566 sleep_ticks = ticks_elapsed(t1, t2);
567
568 /* Tell the scheduler how much we idled: */
569 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
570
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 /* Re-enable interrupts */
572 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200573 /* Do not account our idle-switching overhead: */
574 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
575
Andi Kleen495ab9c2006-06-26 13:59:11 +0200576 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800577 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 break;
579
580 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100581 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400582 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100583 * Must be done before busmaster disable as we might
584 * need to access HPET !
585 */
586 acpi_state_timer_broadcast(pr, cx, 1);
587 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400588 * disable bus master
589 * bm_check implies we need ARB_DIS
590 * !bm_check implies we need cache flush
591 * bm_control implies whether we can do ARB_DIS
592 *
593 * That leaves a case where bm_check is set and bm_control is
594 * not set. In that case we cannot do much, we enter C3
595 * without doing anything.
596 */
597 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400598 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400599 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400600 /*
601 * All CPUs are trying to go to C3
602 * Disable bus master arbitration
603 */
Bob Moored8c71b62007-02-02 19:48:21 +0300604 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400605 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400606 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400607 /* SMP with no shared cache... Invalidate cache */
608 ACPI_FLUSH_CPU_CACHE();
609 }
Len Brown4be44fc2005-08-05 00:44:28 -0400610
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300612 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200614 /* Tell the scheduler that we are going deep-idle: */
615 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700616 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300618 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400619 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400620 /* Enable bus master arbitration */
621 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300622 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400623 }
624
Pavel Machek61331162008-02-19 11:00:29 +0100625#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700626 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100627 if (tsc_halts_in_c(ACPI_STATE_C3))
628 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700629#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200630 /* Compute time (ticks) that we were actually asleep */
631 sleep_ticks = ticks_elapsed(t1, t2);
632 /* Tell the scheduler how much we idled: */
633 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
634
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 /* Re-enable interrupts */
636 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200637 /* Do not account our idle-switching overhead: */
638 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
639
Andi Kleen495ab9c2006-06-26 13:59:11 +0200640 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800641 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 break;
643
644 default:
645 local_irq_enable();
646 return;
647 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400648 cx->usage++;
649 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
650 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651
652 next_state = pr->power.state;
653
David Shaohua Li1e483962005-12-01 17:00:00 -0500654#ifdef CONFIG_HOTPLUG_CPU
655 /* Don't do promotion/demotion */
656 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300657 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500658 next_state = cx;
659 goto end;
660 }
661#endif
662
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 /*
664 * Promotion?
665 * ----------
666 * Track the number of longs (time asleep is greater than threshold)
667 * and promote when the count threshold is reached. Note that bus
668 * mastering activity may prevent promotions.
669 * Do not promote above max_cstate.
670 */
671 if (cx->promotion.state &&
672 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700673 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800674 cx->promotion.state->latency <=
675 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400677 cx->demotion.count = 0;
678 if (cx->promotion.count >=
679 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400681 if (!
682 (pr->power.bm_activity & cx->
683 promotion.threshold.bm)) {
684 next_state =
685 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 goto end;
687 }
Len Brown4be44fc2005-08-05 00:44:28 -0400688 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 next_state = cx->promotion.state;
690 goto end;
691 }
692 }
693 }
694 }
695
696 /*
697 * Demotion?
698 * ---------
699 * Track the number of shorts (time asleep is less than time threshold)
700 * and demote when the usage threshold is reached.
701 */
702 if (cx->demotion.state) {
703 if (sleep_ticks < cx->demotion.threshold.ticks) {
704 cx->demotion.count++;
705 cx->promotion.count = 0;
706 if (cx->demotion.count >= cx->demotion.threshold.count) {
707 next_state = cx->demotion.state;
708 goto end;
709 }
710 }
711 }
712
Len Brown4be44fc2005-08-05 00:44:28 -0400713 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 /*
715 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700716 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700718 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800719 pr->power.state->latency >
720 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 if (cx->demotion.state)
722 next_state = cx->demotion.state;
723 }
724
725 /*
726 * New Cx State?
727 * -------------
728 * If we're going to start using a new Cx state we must clean up
729 * from the previous and prepare to use the new.
730 */
731 if (next_state != pr->power.state)
732 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
Len Brown4be44fc2005-08-05 00:44:28 -0400735static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736{
737 unsigned int i;
738 unsigned int state_is_set = 0;
739 struct acpi_processor_cx *lower = NULL;
740 struct acpi_processor_cx *higher = NULL;
741 struct acpi_processor_cx *cx;
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743
744 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400745 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /*
748 * This function sets the default Cx state policy (OS idle handler).
749 * Our scheme is to promote quickly to C2 but more conservatively
750 * to C3. We're favoring C2 for its characteristics of low latency
751 * (quick response), good power savings, and ability to allow bus
752 * mastering activity. Note that the Cx state policy is completely
753 * customizable and can be altered dynamically.
754 */
755
756 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400757 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 cx = &pr->power.states[i];
759 if (!cx->valid)
760 continue;
761
762 if (!state_is_set)
763 pr->power.state = cx;
764 state_is_set++;
765 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400769 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400772 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 cx = &pr->power.states[i];
774 if (!cx->valid)
775 continue;
776
777 if (lower) {
778 cx->demotion.state = lower;
779 cx->demotion.threshold.ticks = cx->latency_ticks;
780 cx->demotion.threshold.count = 1;
781 if (cx->type == ACPI_STATE_C3)
782 cx->demotion.threshold.bm = bm_history;
783 }
784
785 lower = cx;
786 }
787
788 /* promotion */
789 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
790 cx = &pr->power.states[i];
791 if (!cx->valid)
792 continue;
793
794 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400795 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 cx->promotion.threshold.ticks = cx->latency_ticks;
797 if (cx->type >= ACPI_STATE_C2)
798 cx->promotion.threshold.count = 4;
799 else
800 cx->promotion.threshold.count = 10;
801 if (higher->type == ACPI_STATE_C3)
802 cx->promotion.threshold.bm = bm_history;
803 }
804
805 higher = cx;
806 }
807
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809}
Len Brown4f86d3a2007-10-03 18:58:00 -0400810#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
Len Brown4be44fc2005-08-05 00:44:28 -0400812static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814
815 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400816 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
818 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400819 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
823 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
824
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400825#ifndef CONFIG_HOTPLUG_CPU
826 /*
827 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400828 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400829 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300830 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300831 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400832 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400833#endif
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 /* determine C2 and C3 address from pblk */
836 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
837 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
838
839 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300840 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
841 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
843 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
844 "lvl2[0x%08x] lvl3[0x%08x]\n",
845 pr->power.states[ACPI_STATE_C2].address,
846 pr->power.states[ACPI_STATE_C3].address));
847
Patrick Mocheld550d982006-06-27 00:41:40 -0400848 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849}
850
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700851static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500852{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700853 if (!pr->power.states[ACPI_STATE_C1].valid) {
854 /* set the first C-State to C1 */
855 /* all processors need to support C1 */
856 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
857 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400858 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700859 }
860 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500861 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400862 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500863}
864
Len Brown4be44fc2005-08-05 00:44:28 -0400865static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Len Brown4be44fc2005-08-05 00:44:28 -0400867 acpi_status status = 0;
868 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400869 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400870 int i;
871 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
872 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400876 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700878 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
880 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
881 if (ACPI_FAILURE(status)) {
882 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400883 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400884 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200886 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
888 /* There must be at least 2 elements */
889 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400890 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891 status = -EFAULT;
892 goto end;
893 }
894
895 count = cst->package.elements[0].integer.value;
896
897 /* Validate number of power states. */
898 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400899 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 status = -EFAULT;
901 goto end;
902 }
903
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 /* Tell driver that at least _CST is supported. */
905 pr->flags.has_cst = 1;
906
907 for (i = 1; i <= count; i++) {
908 union acpi_object *element;
909 union acpi_object *obj;
910 struct acpi_power_register *reg;
911 struct acpi_processor_cx cx;
912
913 memset(&cx, 0, sizeof(cx));
914
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200915 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (element->type != ACPI_TYPE_PACKAGE)
917 continue;
918
919 if (element->package.count != 4)
920 continue;
921
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200922 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923
924 if (obj->type != ACPI_TYPE_BUFFER)
925 continue;
926
Len Brown4be44fc2005-08-05 00:44:28 -0400927 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700928
929 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400930 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 continue;
932
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200934 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 if (obj->type != ACPI_TYPE_INTEGER)
936 continue;
937
938 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700939 /*
940 * Some buggy BIOSes won't list C1 in _CST -
941 * Let acpi_processor_get_power_info_default() handle them later
942 */
943 if (i == 1 && cx.type != ACPI_STATE_C1)
944 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700946 cx.address = reg->address;
947 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800949 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700950 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
951 if (acpi_processor_ffh_cstate_probe
952 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800953 cx.entry_method = ACPI_CSTATE_FFH;
954 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700955 /*
956 * C1 is a special case where FIXED_HARDWARE
957 * can be handled in non-MWAIT way as well.
958 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700959 * Otherwise, ignore this info and continue.
960 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800961 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800962 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800963 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700964 continue;
965 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800966 if (cx.type == ACPI_STATE_C1 &&
967 (idle_halt || idle_nomwait)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800968 /*
969 * In most cases the C1 space_id obtained from
970 * _CST object is FIXED_HARDWARE access mode.
971 * But when the option of idle=halt is added,
972 * the entry_method type should be changed from
973 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800974 * When the option of idle=nomwait is added,
975 * the C1 entry_method type should be
976 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800977 */
978 cx.entry_method = ACPI_CSTATE_HALT;
979 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
980 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800981 } else {
982 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
983 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700984 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400986 if (cx.type == ACPI_STATE_C1) {
987 cx.valid = 1;
988 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800989
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200990 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 if (obj->type != ACPI_TYPE_INTEGER)
992 continue;
993
994 cx.latency = obj->integer.value;
995
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200996 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 if (obj->type != ACPI_TYPE_INTEGER)
998 continue;
999
1000 cx.power = obj->integer.value;
1001
Janosch Machowinskicf824782005-08-20 08:02:00 -04001002 current_count++;
1003 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
1004
1005 /*
1006 * We support total ACPI_PROCESSOR_MAX_POWER - 1
1007 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
1008 */
1009 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1010 printk(KERN_WARNING
1011 "Limiting number of power states to max (%d)\n",
1012 ACPI_PROCESSOR_MAX_POWER);
1013 printk(KERN_WARNING
1014 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1015 break;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 }
1018
Len Brown4be44fc2005-08-05 00:44:28 -04001019 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -04001020 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
1022 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -04001023 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001024 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025
Len Brown4be44fc2005-08-05 00:44:28 -04001026 end:
Len Brown02438d82006-06-30 03:19:10 -04001027 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028
Patrick Mocheld550d982006-06-27 00:41:40 -04001029 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030}
1031
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1033{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034
1035 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001036 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
1038 /*
1039 * C2 latency must be less than or equal to 100
1040 * microseconds.
1041 */
1042 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1043 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001044 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001045 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 }
1047
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 /*
1049 * Otherwise we've met all of our C2 requirements.
1050 * Normalize the C2 latency to expidite policy
1051 */
1052 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001053
1054#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001056#else
1057 cx->latency_ticks = cx->latency;
1058#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059
Patrick Mocheld550d982006-06-27 00:41:40 -04001060 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061}
1062
Len Brown4be44fc2005-08-05 00:44:28 -04001063static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1064 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001066 static int bm_check_flag;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068
1069 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001070 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071
1072 /*
1073 * C3 latency must be less than or equal to 1000
1074 * microseconds.
1075 */
1076 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1077 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001078 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001079 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 }
1081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082 /*
1083 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1084 * DMA transfers are used by any ISA device to avoid livelock.
1085 * Note that we could disable Type-F DMA (as recommended by
1086 * the erratum), but this is known to disrupt certain ISA
1087 * devices thus we take the conservative approach.
1088 */
1089 else if (errata.piix4.fdma) {
1090 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001091 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001092 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 }
1094
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001095 /* All the logic here assumes flags.bm_check is same across all CPUs */
1096 if (!bm_check_flag) {
1097 /* Determine whether bm_check is needed based on CPU */
1098 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1099 bm_check_flag = pr->flags.bm_check;
1100 } else {
1101 pr->flags.bm_check = bm_check_flag;
1102 }
1103
1104 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001105 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001106 if (pr->flags.has_cst != 1) {
1107 /* bus mastering control is necessary */
1108 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1109 "C3 support requires BM control\n"));
1110 return;
1111 } else {
1112 /* Here we enter C3 without bus mastering */
1113 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1114 "C3 support without BM control\n"));
1115 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001116 }
1117 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001118 /*
1119 * WBINVD should be set in fadt, for C3 state to be
1120 * supported on when bm_check is not required.
1121 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001122 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001123 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001124 "Cache invalidation should work properly"
1125 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001126 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001127 }
Bob Moored8c71b62007-02-02 19:48:21 +03001128 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001129 }
1130
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 /*
1132 * Otherwise we've met all of our C3 requirements.
1133 * Normalize the C3 latency to expidite policy. Enable
1134 * checking of bus mastering status (bm_check) so we can
1135 * use this in our C3 policy
1136 */
1137 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001138
1139#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001141#else
1142 cx->latency_ticks = cx->latency;
1143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
Patrick Mocheld550d982006-06-27 00:41:40 -04001145 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146}
1147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148static int acpi_processor_power_verify(struct acpi_processor *pr)
1149{
1150 unsigned int i;
1151 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001152
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001153 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001154
Len Brown4be44fc2005-08-05 00:44:28 -04001155 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156 struct acpi_processor_cx *cx = &pr->power.states[i];
1157
1158 switch (cx->type) {
1159 case ACPI_STATE_C1:
1160 cx->valid = 1;
1161 break;
1162
1163 case ACPI_STATE_C2:
1164 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001165 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001166 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167 break;
1168
1169 case ACPI_STATE_C3:
1170 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001171 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001172 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 break;
1174 }
1175
1176 if (cx->valid)
1177 working++;
1178 }
1179
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001180 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001181
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182 return (working);
1183}
1184
Len Brown4be44fc2005-08-05 00:44:28 -04001185static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186{
1187 unsigned int i;
1188 int result;
1189
Linus Torvalds1da177e2005-04-16 15:20:36 -07001190
1191 /* NOTE: the idle thread may not be running while calling
1192 * this function */
1193
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001194 /* Zero initialize all the C-states info. */
1195 memset(pr->power.states, 0, sizeof(pr->power.states));
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001198 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001199 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001200
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001201 if (result)
1202 return result;
1203
1204 acpi_processor_get_power_info_default(pr);
1205
Janosch Machowinskicf824782005-08-20 08:02:00 -04001206 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Len Brown4f86d3a2007-10-03 18:58:00 -04001208#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 /*
1210 * Set Default Policy
1211 * ------------------
1212 * Now that we know which states are supported, set the default
1213 * policy. Note that this policy can be changed dynamically
1214 * (e.g. encourage deeper sleeps to conserve battery life when
1215 * not on AC).
1216 */
1217 result = acpi_processor_set_power_policy(pr);
1218 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001219 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001220#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222 /*
1223 * if one state of type C2 or C3 is available, mark this
1224 * CPU as being "idle manageable"
1225 */
1226 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001227 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001229 if (pr->power.states[i].type >= ACPI_STATE_C2)
1230 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001231 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232 }
1233
Patrick Mocheld550d982006-06-27 00:41:40 -04001234 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235}
1236
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1238{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001239 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001240 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242
1243 if (!pr)
1244 goto end;
1245
1246 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001247 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001248 "bus master activity: %08x\n"
1249 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001250 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001251 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001252 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 seq_puts(seq, "states:\n");
1255
1256 for (i = 1; i <= pr->power.count; i++) {
1257 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001258 (&pr->power.states[i] ==
1259 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 if (!pr->power.states[i].valid) {
1262 seq_puts(seq, "<not supported>\n");
1263 continue;
1264 }
1265
1266 switch (pr->power.states[i].type) {
1267 case ACPI_STATE_C1:
1268 seq_printf(seq, "type[C1] ");
1269 break;
1270 case ACPI_STATE_C2:
1271 seq_printf(seq, "type[C2] ");
1272 break;
1273 case ACPI_STATE_C3:
1274 seq_printf(seq, "type[C3] ");
1275 break;
1276 default:
1277 seq_printf(seq, "type[--] ");
1278 break;
1279 }
1280
1281 if (pr->power.states[i].promotion.state)
1282 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001283 (pr->power.states[i].promotion.state -
1284 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 else
1286 seq_puts(seq, "promotion[--] ");
1287
1288 if (pr->power.states[i].demotion.state)
1289 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001290 (pr->power.states[i].demotion.state -
1291 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 else
1293 seq_puts(seq, "demotion[--] ");
1294
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001295 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001296 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001297 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001298 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299 }
1300
Len Brown4be44fc2005-08-05 00:44:28 -04001301 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001302 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001303}
1304
1305static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1306{
1307 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001308 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309}
1310
Arjan van de Vend7508032006-07-04 13:06:00 -04001311static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001312 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001313 .open = acpi_processor_power_open_fs,
1314 .read = seq_read,
1315 .llseek = seq_lseek,
1316 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001317};
1318
Len Brown4f86d3a2007-10-03 18:58:00 -04001319#ifndef CONFIG_CPU_IDLE
1320
1321int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1322{
1323 int result = 0;
1324
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001325 if (boot_option_idle_override)
1326 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001327
1328 if (!pr)
1329 return -EINVAL;
1330
1331 if (nocst) {
1332 return -ENODEV;
1333 }
1334
1335 if (!pr->flags.power_setup_done)
1336 return -ENODEV;
1337
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001338 /*
1339 * Fall back to the default idle loop, when pm_idle_save had
1340 * been initialized.
1341 */
1342 if (pm_idle_save) {
1343 pm_idle = pm_idle_save;
1344 /* Relies on interrupts forcing exit from idle. */
1345 synchronize_sched();
1346 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001347
1348 pr->flags.power = 0;
1349 result = acpi_processor_get_power_info(pr);
1350 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1351 pm_idle = acpi_processor_idle;
1352
1353 return result;
1354}
1355
Andrew Morton1fec74a2006-10-17 00:09:58 -07001356#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001357static void smp_callback(void *v)
1358{
1359 /* we already woke the CPU up, nothing more to do */
1360}
1361
1362/*
1363 * This function gets called when a part of the kernel has a new latency
1364 * requirement. This means we need to get all processors out of their C-state,
1365 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1366 * wakes them all right up.
1367 */
1368static int acpi_processor_latency_notify(struct notifier_block *b,
1369 unsigned long l, void *v)
1370{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001371 smp_call_function(smp_callback, NULL, 1);
Arjan van de Ven5c875792006-09-30 23:27:17 -07001372 return NOTIFY_OK;
1373}
1374
1375static struct notifier_block acpi_processor_latency_notifier = {
1376 .notifier_call = acpi_processor_latency_notify,
1377};
Len Brown4f86d3a2007-10-03 18:58:00 -04001378
Andrew Morton1fec74a2006-10-17 00:09:58 -07001379#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001380
Len Brown4f86d3a2007-10-03 18:58:00 -04001381#else /* CONFIG_CPU_IDLE */
1382
1383/**
1384 * acpi_idle_bm_check - checks if bus master activity was detected
1385 */
1386static int acpi_idle_bm_check(void)
1387{
1388 u32 bm_status = 0;
1389
1390 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1391 if (bm_status)
1392 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1393 /*
1394 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1395 * the true state of bus mastering activity; forcing us to
1396 * manually check the BMIDEA bit of each IDE channel.
1397 */
1398 else if (errata.piix4.bmisx) {
1399 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1400 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1401 bm_status = 1;
1402 }
1403 return bm_status;
1404}
1405
1406/**
1407 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1408 * @pr: the processor
1409 * @target: the new target state
1410 */
1411static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1412 struct acpi_processor_cx *target)
1413{
1414 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1415 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1416 pr->flags.bm_rld_set = 0;
1417 }
1418
1419 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1420 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1421 pr->flags.bm_rld_set = 1;
1422 }
1423}
1424
1425/**
1426 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1427 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001428 *
1429 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001430 */
1431static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1432{
Thomas Gleixner4ac13292008-12-09 21:43:39 +01001433 u64 pctrl;
1434
Steven Rostedtdcf30992008-07-25 18:00:42 -04001435 /* Don't trace irqs off for idle */
1436 stop_critical_timings();
Ingo Molnar01b28382008-12-11 13:45:51 +01001437 pctrl = hw_perf_save_disable();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001438 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001439 /* Call into architectural FFH based C-state */
1440 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001441 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1442 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001443 } else {
1444 int unused;
1445 /* IO port based C-state */
1446 inb(cx->address);
1447 /* Dummy wait op - must do something useless after P_LVL2 read
1448 because chipsets cannot guarantee that STPCLK# signal
1449 gets asserted in time to freeze execution properly. */
1450 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1451 }
Ingo Molnar01b28382008-12-11 13:45:51 +01001452 hw_perf_restore(pctrl);
Steven Rostedtdcf30992008-07-25 18:00:42 -04001453 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -04001454}
1455
1456/**
1457 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1458 * @dev: the target CPU
1459 * @state: the state data
1460 *
1461 * This is equivalent to the HALT instruction.
1462 */
1463static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1464 struct cpuidle_state *state)
1465{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001466 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001467 struct acpi_processor *pr;
1468 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001469
Mike Travis706546d2008-06-09 16:22:23 -07001470 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001471
1472 if (unlikely(!pr))
1473 return 0;
1474
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001475 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001476
1477 /* Do not access any ACPI IO ports in suspend path */
1478 if (acpi_idle_suspend) {
1479 acpi_safe_halt();
1480 local_irq_enable();
1481 return 0;
1482 }
1483
Len Brown4f86d3a2007-10-03 18:58:00 -04001484 if (pr->flags.bm_check)
1485 acpi_idle_update_bm_rld(pr, cx);
1486
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001487 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001488 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001489 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001490
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001491 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001492 cx->usage++;
1493
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001494 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001495}
1496
1497/**
1498 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1499 * @dev: the target CPU
1500 * @state: the state data
1501 */
1502static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1503 struct cpuidle_state *state)
1504{
1505 struct acpi_processor *pr;
1506 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1507 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001508 int sleep_ticks = 0;
1509
Mike Travis706546d2008-06-09 16:22:23 -07001510 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001511
1512 if (unlikely(!pr))
1513 return 0;
1514
Len Browne1964412007-10-04 01:23:47 -04001515 if (acpi_idle_suspend)
1516 return(acpi_idle_enter_c1(dev, state));
1517
Len Brown4f86d3a2007-10-03 18:58:00 -04001518 local_irq_disable();
1519 current_thread_info()->status &= ~TS_POLLING;
1520 /*
1521 * TS_POLLING-cleared state must be visible before we test
1522 * NEED_RESCHED:
1523 */
1524 smp_mb();
1525
1526 if (unlikely(need_resched())) {
1527 current_thread_info()->status |= TS_POLLING;
1528 local_irq_enable();
1529 return 0;
1530 }
1531
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001532 /*
1533 * Must be done before busmaster disable as we might need to
1534 * access HPET !
1535 */
1536 acpi_state_timer_broadcast(pr, cx, 1);
1537
1538 if (pr->flags.bm_check)
1539 acpi_idle_update_bm_rld(pr, cx);
1540
Len Brown4f86d3a2007-10-03 18:58:00 -04001541 if (cx->type == ACPI_STATE_C3)
1542 ACPI_FLUSH_CPU_CACHE();
1543
1544 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001545 /* Tell the scheduler that we are going deep-idle: */
1546 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001547 acpi_idle_do_entry(cx);
1548 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1549
Pavel Machek61331162008-02-19 11:00:29 +01001550#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001551 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001552 if (tsc_halts_in_c(cx->type))
1553 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001554#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001555 sleep_ticks = ticks_elapsed(t1, t2);
1556
1557 /* Tell the scheduler how much we idled: */
1558 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001559
1560 local_irq_enable();
1561 current_thread_info()->status |= TS_POLLING;
1562
1563 cx->usage++;
1564
1565 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001566 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001567 return ticks_elapsed_in_us(t1, t2);
1568}
1569
1570static int c3_cpu_count;
1571static DEFINE_SPINLOCK(c3_lock);
1572
1573/**
1574 * acpi_idle_enter_bm - enters C3 with proper BM handling
1575 * @dev: the target CPU
1576 * @state: the state data
1577 *
1578 * If BM is detected, the deepest non-C3 idle state is entered instead.
1579 */
1580static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1581 struct cpuidle_state *state)
1582{
1583 struct acpi_processor *pr;
1584 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1585 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001586 int sleep_ticks = 0;
1587
Mike Travis706546d2008-06-09 16:22:23 -07001588 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001589
1590 if (unlikely(!pr))
1591 return 0;
1592
Len Browne1964412007-10-04 01:23:47 -04001593 if (acpi_idle_suspend)
1594 return(acpi_idle_enter_c1(dev, state));
1595
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001596 if (acpi_idle_bm_check()) {
1597 if (dev->safe_state) {
Venkatesh Pallipadiaddbad42008-09-29 15:24:28 -07001598 dev->last_state = dev->safe_state;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001599 return dev->safe_state->enter(dev, dev->safe_state);
1600 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001601 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001602 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001603 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001604 return 0;
1605 }
1606 }
1607
Len Brown4f86d3a2007-10-03 18:58:00 -04001608 local_irq_disable();
1609 current_thread_info()->status &= ~TS_POLLING;
1610 /*
1611 * TS_POLLING-cleared state must be visible before we test
1612 * NEED_RESCHED:
1613 */
1614 smp_mb();
1615
1616 if (unlikely(need_resched())) {
1617 current_thread_info()->status |= TS_POLLING;
1618 local_irq_enable();
1619 return 0;
1620 }
1621
Venki Pallipadi996520c2008-03-24 14:24:10 -07001622 acpi_unlazy_tlb(smp_processor_id());
1623
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001624 /* Tell the scheduler that we are going deep-idle: */
1625 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001626 /*
1627 * Must be done before busmaster disable as we might need to
1628 * access HPET !
1629 */
1630 acpi_state_timer_broadcast(pr, cx, 1);
1631
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001632 acpi_idle_update_bm_rld(pr, cx);
Len Brown4f86d3a2007-10-03 18:58:00 -04001633
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001634 /*
1635 * disable bus master
1636 * bm_check implies we need ARB_DIS
1637 * !bm_check implies we need cache flush
1638 * bm_control implies whether we can do ARB_DIS
1639 *
1640 * That leaves a case where bm_check is set and bm_control is
1641 * not set. In that case we cannot do much, we enter C3
1642 * without doing anything.
1643 */
1644 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001645 spin_lock(&c3_lock);
1646 c3_cpu_count++;
1647 /* Disable bus master arbitration when all CPUs are in C3 */
1648 if (c3_cpu_count == num_online_cpus())
1649 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1650 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001651 } else if (!pr->flags.bm_check) {
1652 ACPI_FLUSH_CPU_CACHE();
1653 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001654
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001655 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1656 acpi_idle_do_entry(cx);
1657 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001658
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001659 /* Re-enable bus master arbitration */
1660 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001661 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001662 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001663 c3_cpu_count--;
1664 spin_unlock(&c3_lock);
1665 }
1666
Pavel Machek61331162008-02-19 11:00:29 +01001667#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001668 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001669 if (tsc_halts_in_c(ACPI_STATE_C3))
1670 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001671#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001672 sleep_ticks = ticks_elapsed(t1, t2);
1673 /* Tell the scheduler how much we idled: */
1674 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001675
1676 local_irq_enable();
1677 current_thread_info()->status |= TS_POLLING;
1678
1679 cx->usage++;
1680
1681 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001682 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001683 return ticks_elapsed_in_us(t1, t2);
1684}
1685
1686struct cpuidle_driver acpi_idle_driver = {
1687 .name = "acpi_idle",
1688 .owner = THIS_MODULE,
1689};
1690
1691/**
1692 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1693 * @pr: the ACPI processor
1694 */
1695static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1696{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001697 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001698 struct acpi_processor_cx *cx;
1699 struct cpuidle_state *state;
1700 struct cpuidle_device *dev = &pr->power.dev;
1701
1702 if (!pr->flags.power_setup_done)
1703 return -EINVAL;
1704
1705 if (pr->flags.power == 0) {
1706 return -EINVAL;
1707 }
1708
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001709 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001710 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1711 dev->states[i].name[0] = '\0';
1712 dev->states[i].desc[0] = '\0';
1713 }
1714
Len Brown4f86d3a2007-10-03 18:58:00 -04001715 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1716 cx = &pr->power.states[i];
1717 state = &dev->states[count];
1718
1719 if (!cx->valid)
1720 continue;
1721
1722#ifdef CONFIG_HOTPLUG_CPU
1723 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1724 !pr->flags.has_cst &&
1725 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1726 continue;
1727#endif
1728 cpuidle_set_statedata(state, cx);
1729
1730 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001731 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001732 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001733 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001734 state->power_usage = cx->power;
1735
1736 state->flags = 0;
1737 switch (cx->type) {
1738 case ACPI_STATE_C1:
1739 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001740 if (cx->entry_method == ACPI_CSTATE_FFH)
1741 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1742
Len Brown4f86d3a2007-10-03 18:58:00 -04001743 state->enter = acpi_idle_enter_c1;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001744 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001745 break;
1746
1747 case ACPI_STATE_C2:
1748 state->flags |= CPUIDLE_FLAG_BALANCED;
1749 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1750 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001751 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001752 break;
1753
1754 case ACPI_STATE_C3:
1755 state->flags |= CPUIDLE_FLAG_DEEP;
1756 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1757 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1758 state->enter = pr->flags.bm_check ?
1759 acpi_idle_enter_bm :
1760 acpi_idle_enter_simple;
1761 break;
1762 }
1763
1764 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001765 if (count == CPUIDLE_STATE_MAX)
1766 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001767 }
1768
1769 dev->state_count = count;
1770
1771 if (!count)
1772 return -EINVAL;
1773
Len Brown4f86d3a2007-10-03 18:58:00 -04001774 return 0;
1775}
1776
1777int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1778{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001779 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001780
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001781 if (boot_option_idle_override)
1782 return 0;
1783
Len Brown4f86d3a2007-10-03 18:58:00 -04001784 if (!pr)
1785 return -EINVAL;
1786
1787 if (nocst) {
1788 return -ENODEV;
1789 }
1790
1791 if (!pr->flags.power_setup_done)
1792 return -ENODEV;
1793
1794 cpuidle_pause_and_lock();
1795 cpuidle_disable_device(&pr->power.dev);
1796 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001797 if (pr->flags.power) {
1798 acpi_processor_setup_cpuidle(pr);
1799 ret = cpuidle_enable_device(&pr->power.dev);
1800 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001801 cpuidle_resume_and_unlock();
1802
1803 return ret;
1804}
1805
1806#endif /* CONFIG_CPU_IDLE */
1807
Pierre Ossman7af8b662006-10-10 14:20:31 -07001808int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001809 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
Len Brown4be44fc2005-08-05 00:44:28 -04001811 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001812 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001813 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814 unsigned int i;
1815
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001816 if (boot_option_idle_override)
1817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001820 if (idle_halt) {
1821 /*
1822 * When the boot option of "idle=halt" is added, halt
1823 * is used for CPU IDLE.
1824 * In such case C2/C3 is meaningless. So the max_cstate
1825 * is set to one.
1826 */
1827 max_cstate = 1;
1828 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001829 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001830 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001832 printk(KERN_NOTICE
1833 "ACPI: processor limited to max C-state %d\n",
1834 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001836#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1837 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1838 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001839#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
1841
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001842 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001843 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001844
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001845 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001846 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001847 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001849 ACPI_EXCEPTION((AE_INFO, status,
1850 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851 }
1852 }
1853
Linus Torvalds1da177e2005-04-16 15:20:36 -07001854 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001855 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001856
1857 /*
1858 * Install the idle handler if processor power management is supported.
1859 * Note that we use previously set idle handler will be used on
1860 * platforms that only support C1.
1861 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001862 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001863#ifdef CONFIG_CPU_IDLE
1864 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001865 if (cpuidle_register_device(&pr->power.dev))
1866 return -EIO;
1867#endif
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1870 for (i = 1; i <= pr->power.count; i++)
1871 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001872 printk(" C%d[C%d]", i,
1873 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 printk(")\n");
1875
Len Brown4f86d3a2007-10-03 18:58:00 -04001876#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 if (pr->id == 0) {
1878 pm_idle_save = pm_idle;
1879 pm_idle = acpi_processor_idle;
1880 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001881#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 }
1883
1884 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001885 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1886 S_IRUGO, acpi_device_dir(device),
1887 &acpi_processor_power_fops,
1888 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001890 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892}
1893
Len Brown4be44fc2005-08-05 00:44:28 -04001894int acpi_processor_power_exit(struct acpi_processor *pr,
1895 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001897 if (boot_option_idle_override)
1898 return 0;
1899
Len Brown4f86d3a2007-10-03 18:58:00 -04001900#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001901 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001902#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 pr->flags.power_setup_done = 0;
1904
1905 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001906 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1907 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908
Len Brown4f86d3a2007-10-03 18:58:00 -04001909#ifndef CONFIG_CPU_IDLE
1910
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911 /* Unregister the idle handler when processor #0 is removed. */
1912 if (pr->id == 0) {
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001913 if (pm_idle_save)
1914 pm_idle = pm_idle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915
1916 /*
1917 * We are about to unload the current idle thread pm callback
1918 * (pm_idle), Wait for all processors to update cached/local
1919 * copies of pm_idle before proceeding.
1920 */
1921 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001922#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001923 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1924 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001925#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001927#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928
Patrick Mocheld550d982006-06-27 00:41:40 -04001929 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930}