blob: 81b40ed5379e76edcd02553aaa4881f1107a1422 [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
62#define ACPI_PROCESSOR_COMPONENT 0x01000000
63#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{
Steven Rostedtdcf309972008-07-25 18:00:42 -0400274 /* Don't trace irqs off for idle */
275 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800276 if (cstate->entry_method == ACPI_CSTATE_FFH) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700277 /* Call into architectural FFH based C-state */
278 acpi_processor_ffh_cstate_enter(cstate);
279 } else {
280 int unused;
281 /* IO port based C-state */
282 inb(cstate->address);
283 /* Dummy wait op - must do something useless after P_LVL2 read
284 because chipsets cannot guarantee that STPCLK# signal
285 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300286 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700287 }
Steven Rostedtdcf309972008-07-25 18:00:42 -0400288 start_critical_timings();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700289}
Len Brown4f86d3a2007-10-03 18:58:00 -0400290#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700291
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800292#ifdef ARCH_APICTIMER_STOPS_ON_C3
293
294/*
295 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700296 * This seems to be a common problem on AMD boxen, but other vendors
297 * are affected too. We pick the most conservative approach: we assume
298 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800299 */
300static void acpi_timer_check_state(int state, struct acpi_processor *pr,
301 struct acpi_processor_cx *cx)
302{
303 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100304 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800305
306 /*
307 * Check, if one of the previous states already marked the lapic
308 * unstable
309 */
310 if (pwr->timer_broadcast_on_state < state)
311 return;
312
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100313 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700314 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800315}
316
317static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
318{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800319 unsigned long reason;
320
321 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
322 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
323
324 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800325}
326
327/* Power(C) State timer broadcast control */
328static void acpi_state_timer_broadcast(struct acpi_processor *pr,
329 struct acpi_processor_cx *cx,
330 int broadcast)
331{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800332 int state = cx - pr->power.states;
333
334 if (state >= pr->power.timer_broadcast_on_state) {
335 unsigned long reason;
336
337 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
338 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
339 clockevents_notify(reason, &pr->id);
340 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800341}
342
343#else
344
345static void acpi_timer_check_state(int state, struct acpi_processor *pr,
346 struct acpi_processor_cx *cstate) { }
347static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800348static void acpi_state_timer_broadcast(struct acpi_processor *pr,
349 struct acpi_processor_cx *cx,
350 int broadcast)
351{
352}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800353
354#endif
355
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000356/*
357 * Suspend / resume control
358 */
359static int acpi_idle_suspend;
360
361int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
362{
363 acpi_idle_suspend = 1;
364 return 0;
365}
366
367int acpi_processor_resume(struct acpi_device * device)
368{
369 acpi_idle_suspend = 0;
370 return 0;
371}
372
Pavel Machek61331162008-02-19 11:00:29 +0100373#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Andi Kleenddb25f92008-01-30 13:32:41 +0100374static int tsc_halts_in_c(int state)
375{
376 switch (boot_cpu_data.x86_vendor) {
377 case X86_VENDOR_AMD:
378 /*
379 * AMD Fam10h TSC will tick in all
380 * C/P/S0/S1 states when this bit is set.
381 */
382 if (boot_cpu_has(X86_FEATURE_CONSTANT_TSC))
383 return 0;
384 /*FALL THROUGH*/
385 case X86_VENDOR_INTEL:
386 /* Several cases known where TSC halts in C2 too */
387 default:
388 return state > ACPI_STATE_C1;
389 }
390}
391#endif
392
Len Brown4f86d3a2007-10-03 18:58:00 -0400393#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400394static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395{
Len Brown4be44fc2005-08-05 00:44:28 -0400396 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 struct acpi_processor_cx *cx = NULL;
398 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400399 int sleep_ticks = 0;
400 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 /*
403 * Interrupts must be disabled during bus mastering calculations and
404 * for C2/C3 transitions.
405 */
406 local_irq_disable();
407
Mike Travis706546d2008-06-09 16:22:23 -0700408 pr = __get_cpu_var(processors);
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400409 if (!pr) {
410 local_irq_enable();
411 return;
412 }
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 /*
415 * Check whether we truly need to go idle, or should
416 * reschedule:
417 */
418 if (unlikely(need_resched())) {
419 local_irq_enable();
420 return;
421 }
422
423 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000424 if (!cx || acpi_idle_suspend) {
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200425 if (pm_idle_save) {
426 pm_idle_save(); /* enables IRQs */
427 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800428 acpi_safe_halt();
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700429 local_irq_enable();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200430 }
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700431
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800432 return;
433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434
435 /*
436 * Check BM Activity
437 * -----------------
438 * Check for bus mastering activity (if required), record, and check
439 * for demotion.
440 */
441 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400442 u32 bm_status = 0;
443 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400445 if (diff > 31)
446 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400448 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449
Bob Moored8c71b62007-02-02 19:48:21 +0300450 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400452 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300453 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455 /*
456 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
457 * the true state of bus mastering activity; forcing us to
458 * manually check the BMIDEA bit of each IDE channel.
459 */
460 else if (errata.piix4.bmisx) {
461 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400462 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400463 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 }
465
466 pr->power.bm_check_timestamp = jiffies;
467
468 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400469 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 * to avoid a faulty transition. Note that the processor
471 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400472 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 *
474 * TBD: A better policy might be to fallback to the demotion
475 * state (use it for this quantum only) istead of
476 * demoting -- and rely on duration as our sole demotion
477 * qualification. This may, however, introduce DMA
478 * issues (e.g. floppy DMA transfer overrun/underrun).
479 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400480 if ((pr->power.bm_activity & 0x1) &&
481 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 local_irq_enable();
483 next_state = cx->demotion.state;
484 goto end;
485 }
486 }
487
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400488#ifdef CONFIG_HOTPLUG_CPU
489 /*
490 * Check for P_LVL2_UP flag before entering C2 and above on
491 * an SMP system. We do it here instead of doing it at _CST/P_LVL
492 * detection phase, to work cleanly with logical CPU hotplug.
493 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400494 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300495 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500496 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400497#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500498
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499 /*
500 * Sleep:
501 * ------
502 * Invoke the current Cx state to put the processor to sleep.
503 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100504 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200505 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800506 /*
507 * TS_POLLING-cleared state must be visible before we
508 * test NEED_RESCHED:
509 */
510 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100511 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200512 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800513 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100514 return;
515 }
516 }
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518 switch (cx->type) {
519
520 case ACPI_STATE_C1:
521 /*
522 * Invoke C1.
523 * Use the appropriate idle routine, the one that would
524 * be used without acpi C-states.
525 */
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200526 if (pm_idle_save) {
527 pm_idle_save(); /* enables IRQs */
528 } else {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800529 acpi_safe_halt();
Peter Zijlstra7f424a82008-04-25 17:39:01 +0200530 local_irq_enable();
531 }
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800532
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400534 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 * go to an ISR rather than here. Need to instrument
536 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200537 *
538 * Note: the TSC better not stop in C1, sched_clock() will
539 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 */
541 sleep_ticks = 0xFFFFFFFF;
Venki Pallipadi71e93d12008-03-13 17:18:19 -0700542
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 break;
544
545 case ACPI_STATE_C2:
546 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300547 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200548 /* Tell the scheduler that we are going deep-idle: */
549 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800551 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700552 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300554 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700555
Pavel Machek61331162008-02-19 11:00:29 +0100556#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700557 /* TSC halts in C2, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100558 if (tsc_halts_in_c(ACPI_STATE_C2))
559 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700560#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200561 /* Compute time (ticks) that we were actually asleep */
562 sleep_ticks = ticks_elapsed(t1, t2);
563
564 /* Tell the scheduler how much we idled: */
565 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
566
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Re-enable interrupts */
568 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200569 /* Do not account our idle-switching overhead: */
570 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
571
Andi Kleen495ab9c2006-06-26 13:59:11 +0200572 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800573 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 break;
575
576 case ACPI_STATE_C3:
Venki Pallipadibde6f5f2008-01-30 13:32:01 +0100577 acpi_unlazy_tlb(smp_processor_id());
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400578 /*
Thomas Gleixnere17bcb42007-12-07 19:16:17 +0100579 * Must be done before busmaster disable as we might
580 * need to access HPET !
581 */
582 acpi_state_timer_broadcast(pr, cx, 1);
583 /*
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400584 * disable bus master
585 * bm_check implies we need ARB_DIS
586 * !bm_check implies we need cache flush
587 * bm_control implies whether we can do ARB_DIS
588 *
589 * That leaves a case where bm_check is set and bm_control is
590 * not set. In that case we cannot do much, we enter C3
591 * without doing anything.
592 */
593 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400594 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400595 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400596 /*
597 * All CPUs are trying to go to C3
598 * Disable bus master arbitration
599 */
Bob Moored8c71b62007-02-02 19:48:21 +0300600 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400601 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400602 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400603 /* SMP with no shared cache... Invalidate cache */
604 ACPI_FLUSH_CPU_CACHE();
605 }
Len Brown4be44fc2005-08-05 00:44:28 -0400606
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300608 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609 /* Invoke C3 */
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200610 /* Tell the scheduler that we are going deep-idle: */
611 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700612 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300614 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400615 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400616 /* Enable bus master arbitration */
617 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300618 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400619 }
620
Pavel Machek61331162008-02-19 11:00:29 +0100621#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
john stultz539eb112006-06-26 00:25:10 -0700622 /* TSC halts in C3, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +0100623 if (tsc_halts_in_c(ACPI_STATE_C3))
624 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700625#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200626 /* Compute time (ticks) that we were actually asleep */
627 sleep_ticks = ticks_elapsed(t1, t2);
628 /* Tell the scheduler how much we idled: */
629 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 /* Re-enable interrupts */
632 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200633 /* Do not account our idle-switching overhead: */
634 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
635
Andi Kleen495ab9c2006-06-26 13:59:11 +0200636 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800637 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 break;
639
640 default:
641 local_irq_enable();
642 return;
643 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400644 cx->usage++;
645 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
646 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647
648 next_state = pr->power.state;
649
David Shaohua Li1e483962005-12-01 17:00:00 -0500650#ifdef CONFIG_HOTPLUG_CPU
651 /* Don't do promotion/demotion */
652 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300653 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500654 next_state = cx;
655 goto end;
656 }
657#endif
658
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 /*
660 * Promotion?
661 * ----------
662 * Track the number of longs (time asleep is greater than threshold)
663 * and promote when the count threshold is reached. Note that bus
664 * mastering activity may prevent promotions.
665 * Do not promote above max_cstate.
666 */
667 if (cx->promotion.state &&
668 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700669 if (sleep_ticks > cx->promotion.threshold.ticks &&
Mark Grossf011e2e2008-02-04 22:30:09 -0800670 cx->promotion.state->latency <=
671 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400673 cx->demotion.count = 0;
674 if (cx->promotion.count >=
675 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400677 if (!
678 (pr->power.bm_activity & cx->
679 promotion.threshold.bm)) {
680 next_state =
681 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 goto end;
683 }
Len Brown4be44fc2005-08-05 00:44:28 -0400684 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 next_state = cx->promotion.state;
686 goto end;
687 }
688 }
689 }
690 }
691
692 /*
693 * Demotion?
694 * ---------
695 * Track the number of shorts (time asleep is less than time threshold)
696 * and demote when the usage threshold is reached.
697 */
698 if (cx->demotion.state) {
699 if (sleep_ticks < cx->demotion.threshold.ticks) {
700 cx->demotion.count++;
701 cx->promotion.count = 0;
702 if (cx->demotion.count >= cx->demotion.threshold.count) {
703 next_state = cx->demotion.state;
704 goto end;
705 }
706 }
707 }
708
Len Brown4be44fc2005-08-05 00:44:28 -0400709 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 /*
711 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700712 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700714 if ((pr->power.state - pr->power.states) > max_cstate ||
Mark Grossf011e2e2008-02-04 22:30:09 -0800715 pr->power.state->latency >
716 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 if (cx->demotion.state)
718 next_state = cx->demotion.state;
719 }
720
721 /*
722 * New Cx State?
723 * -------------
724 * If we're going to start using a new Cx state we must clean up
725 * from the previous and prepare to use the new.
726 */
727 if (next_state != pr->power.state)
728 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
730
Len Brown4be44fc2005-08-05 00:44:28 -0400731static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732{
733 unsigned int i;
734 unsigned int state_is_set = 0;
735 struct acpi_processor_cx *lower = NULL;
736 struct acpi_processor_cx *higher = NULL;
737 struct acpi_processor_cx *cx;
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400741 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742
743 /*
744 * This function sets the default Cx state policy (OS idle handler).
745 * Our scheme is to promote quickly to C2 but more conservatively
746 * to C3. We're favoring C2 for its characteristics of low latency
747 * (quick response), good power savings, and ability to allow bus
748 * mastering activity. Note that the Cx state policy is completely
749 * customizable and can be altered dynamically.
750 */
751
752 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400753 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 cx = &pr->power.states[i];
755 if (!cx->valid)
756 continue;
757
758 if (!state_is_set)
759 pr->power.state = cx;
760 state_is_set++;
761 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400762 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
764 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400765 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766
767 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400768 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 cx = &pr->power.states[i];
770 if (!cx->valid)
771 continue;
772
773 if (lower) {
774 cx->demotion.state = lower;
775 cx->demotion.threshold.ticks = cx->latency_ticks;
776 cx->demotion.threshold.count = 1;
777 if (cx->type == ACPI_STATE_C3)
778 cx->demotion.threshold.bm = bm_history;
779 }
780
781 lower = cx;
782 }
783
784 /* promotion */
785 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
786 cx = &pr->power.states[i];
787 if (!cx->valid)
788 continue;
789
790 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400791 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 cx->promotion.threshold.ticks = cx->latency_ticks;
793 if (cx->type >= ACPI_STATE_C2)
794 cx->promotion.threshold.count = 4;
795 else
796 cx->promotion.threshold.count = 10;
797 if (higher->type == ACPI_STATE_C3)
798 cx->promotion.threshold.bm = bm_history;
799 }
800
801 higher = cx;
802 }
803
Patrick Mocheld550d982006-06-27 00:41:40 -0400804 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
Len Brown4f86d3a2007-10-03 18:58:00 -0400806#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
Len Brown4be44fc2005-08-05 00:44:28 -0400808static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400812 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
814 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400815 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
819 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
820
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400821#ifndef CONFIG_HOTPLUG_CPU
822 /*
823 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400824 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400825 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300826 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300827 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400828 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400829#endif
830
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 /* determine C2 and C3 address from pblk */
832 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
833 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
834
835 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300836 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
837 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838
839 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
840 "lvl2[0x%08x] lvl3[0x%08x]\n",
841 pr->power.states[ACPI_STATE_C2].address,
842 pr->power.states[ACPI_STATE_C3].address));
843
Patrick Mocheld550d982006-06-27 00:41:40 -0400844 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845}
846
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700847static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500848{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700849 if (!pr->power.states[ACPI_STATE_C1].valid) {
850 /* set the first C-State to C1 */
851 /* all processors need to support C1 */
852 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
853 pr->power.states[ACPI_STATE_C1].valid = 1;
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400854 pr->power.states[ACPI_STATE_C1].entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700855 }
856 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500857 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400858 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500859}
860
Len Brown4be44fc2005-08-05 00:44:28 -0400861static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862{
Len Brown4be44fc2005-08-05 00:44:28 -0400863 acpi_status status = 0;
864 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400865 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400866 int i;
867 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
868 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400872 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700874 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875
876 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
877 if (ACPI_FAILURE(status)) {
878 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400879 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400880 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200882 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883
884 /* There must be at least 2 elements */
885 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400886 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 status = -EFAULT;
888 goto end;
889 }
890
891 count = cst->package.elements[0].integer.value;
892
893 /* Validate number of power states. */
894 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400895 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 status = -EFAULT;
897 goto end;
898 }
899
Linus Torvalds1da177e2005-04-16 15:20:36 -0700900 /* Tell driver that at least _CST is supported. */
901 pr->flags.has_cst = 1;
902
903 for (i = 1; i <= count; i++) {
904 union acpi_object *element;
905 union acpi_object *obj;
906 struct acpi_power_register *reg;
907 struct acpi_processor_cx cx;
908
909 memset(&cx, 0, sizeof(cx));
910
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200911 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912 if (element->type != ACPI_TYPE_PACKAGE)
913 continue;
914
915 if (element->package.count != 4)
916 continue;
917
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200918 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919
920 if (obj->type != ACPI_TYPE_BUFFER)
921 continue;
922
Len Brown4be44fc2005-08-05 00:44:28 -0400923 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924
925 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400926 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927 continue;
928
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200930 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (obj->type != ACPI_TYPE_INTEGER)
932 continue;
933
934 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700935 /*
936 * Some buggy BIOSes won't list C1 in _CST -
937 * Let acpi_processor_get_power_info_default() handle them later
938 */
939 if (i == 1 && cx.type != ACPI_STATE_C1)
940 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700942 cx.address = reg->address;
943 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800945 cx.entry_method = ACPI_CSTATE_SYSTEMIO;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700946 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
947 if (acpi_processor_ffh_cstate_probe
948 (pr->id, &cx, reg) == 0) {
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800949 cx.entry_method = ACPI_CSTATE_FFH;
950 } else if (cx.type == ACPI_STATE_C1) {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700951 /*
952 * C1 is a special case where FIXED_HARDWARE
953 * can be handled in non-MWAIT way as well.
954 * In that case, save this _CST entry info.
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700955 * Otherwise, ignore this info and continue.
956 */
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800957 cx.entry_method = ACPI_CSTATE_HALT;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800958 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -0800959 } else {
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700960 continue;
961 }
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800962 if (cx.type == ACPI_STATE_C1 &&
963 (idle_halt || idle_nomwait)) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800964 /*
965 * In most cases the C1 space_id obtained from
966 * _CST object is FIXED_HARDWARE access mode.
967 * But when the option of idle=halt is added,
968 * the entry_method type should be changed from
969 * CSTATE_FFH to CSTATE_HALT.
Zhao Yakuida5e09a2008-06-24 18:01:09 +0800970 * When the option of idle=nomwait is added,
971 * the C1 entry_method type should be
972 * CSTATE_HALT.
Zhao Yakuic1e3b372008-06-24 17:58:53 +0800973 */
974 cx.entry_method = ACPI_CSTATE_HALT;
975 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI HLT");
976 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800977 } else {
978 snprintf(cx.desc, ACPI_CX_DESC_LEN, "ACPI IOPORT 0x%x",
979 cx.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700980 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981
Venkatesh Pallipadi0fda6b42008-04-09 21:31:46 -0400982 if (cx.type == ACPI_STATE_C1) {
983 cx.valid = 1;
984 }
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -0800985
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200986 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 if (obj->type != ACPI_TYPE_INTEGER)
988 continue;
989
990 cx.latency = obj->integer.value;
991
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200992 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993 if (obj->type != ACPI_TYPE_INTEGER)
994 continue;
995
996 cx.power = obj->integer.value;
997
Janosch Machowinskicf824782005-08-20 08:02:00 -0400998 current_count++;
999 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
1000
1001 /*
1002 * We support total ACPI_PROCESSOR_MAX_POWER - 1
1003 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
1004 */
1005 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
1006 printk(KERN_WARNING
1007 "Limiting number of power states to max (%d)\n",
1008 ACPI_PROCESSOR_MAX_POWER);
1009 printk(KERN_WARNING
1010 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
1011 break;
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 }
1014
Len Brown4be44fc2005-08-05 00:44:28 -04001015 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -04001016 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
1018 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -04001019 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001020 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021
Len Brown4be44fc2005-08-05 00:44:28 -04001022 end:
Len Brown02438d82006-06-30 03:19:10 -04001023 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001024
Patrick Mocheld550d982006-06-27 00:41:40 -04001025 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026}
1027
Linus Torvalds1da177e2005-04-16 15:20:36 -07001028static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
1029{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030
1031 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001032 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
1034 /*
1035 * C2 latency must be less than or equal to 100
1036 * microseconds.
1037 */
1038 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
1039 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001040 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001041 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 }
1043
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 /*
1045 * Otherwise we've met all of our C2 requirements.
1046 * Normalize the C2 latency to expidite policy
1047 */
1048 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001049
1050#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001052#else
1053 cx->latency_ticks = cx->latency;
1054#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055
Patrick Mocheld550d982006-06-27 00:41:40 -04001056 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057}
1058
Len Brown4be44fc2005-08-05 00:44:28 -04001059static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
1060 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001062 static int bm_check_flag;
1063
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
1065 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -04001066 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
1068 /*
1069 * C3 latency must be less than or equal to 1000
1070 * microseconds.
1071 */
1072 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1073 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001074 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001075 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 }
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /*
1079 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1080 * DMA transfers are used by any ISA device to avoid livelock.
1081 * Note that we could disable Type-F DMA (as recommended by
1082 * the erratum), but this is known to disrupt certain ISA
1083 * devices thus we take the conservative approach.
1084 */
1085 else if (errata.piix4.fdma) {
1086 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001087 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001088 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 }
1090
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001091 /* All the logic here assumes flags.bm_check is same across all CPUs */
1092 if (!bm_check_flag) {
1093 /* Determine whether bm_check is needed based on CPU */
1094 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1095 bm_check_flag = pr->flags.bm_check;
1096 } else {
1097 pr->flags.bm_check = bm_check_flag;
1098 }
1099
1100 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001101 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001102 if (pr->flags.has_cst != 1) {
1103 /* bus mastering control is necessary */
1104 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1105 "C3 support requires BM control\n"));
1106 return;
1107 } else {
1108 /* Here we enter C3 without bus mastering */
1109 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1110 "C3 support without BM control\n"));
1111 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001112 }
1113 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001114 /*
1115 * WBINVD should be set in fadt, for C3 state to be
1116 * supported on when bm_check is not required.
1117 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001118 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001119 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001120 "Cache invalidation should work properly"
1121 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001122 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001123 }
Bob Moored8c71b62007-02-02 19:48:21 +03001124 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001125 }
1126
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 /*
1128 * Otherwise we've met all of our C3 requirements.
1129 * Normalize the C3 latency to expidite policy. Enable
1130 * checking of bus mastering status (bm_check) so we can
1131 * use this in our C3 policy
1132 */
1133 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001134
1135#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001137#else
1138 cx->latency_ticks = cx->latency;
1139#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140
Patrick Mocheld550d982006-06-27 00:41:40 -04001141 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142}
1143
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144static int acpi_processor_power_verify(struct acpi_processor *pr)
1145{
1146 unsigned int i;
1147 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001148
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001149 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001150
Len Brown4be44fc2005-08-05 00:44:28 -04001151 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152 struct acpi_processor_cx *cx = &pr->power.states[i];
1153
1154 switch (cx->type) {
1155 case ACPI_STATE_C1:
1156 cx->valid = 1;
1157 break;
1158
1159 case ACPI_STATE_C2:
1160 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001161 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001162 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 break;
1164
1165 case ACPI_STATE_C3:
1166 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001167 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001168 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 break;
1170 }
1171
1172 if (cx->valid)
1173 working++;
1174 }
1175
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001176 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001177
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 return (working);
1179}
1180
Len Brown4be44fc2005-08-05 00:44:28 -04001181static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182{
1183 unsigned int i;
1184 int result;
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186
1187 /* NOTE: the idle thread may not be running while calling
1188 * this function */
1189
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001190 /* Zero initialize all the C-states info. */
1191 memset(pr->power.states, 0, sizeof(pr->power.states));
1192
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001194 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001195 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001196
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001197 if (result)
1198 return result;
1199
1200 acpi_processor_get_power_info_default(pr);
1201
Janosch Machowinskicf824782005-08-20 08:02:00 -04001202 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203
Len Brown4f86d3a2007-10-03 18:58:00 -04001204#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 /*
1206 * Set Default Policy
1207 * ------------------
1208 * Now that we know which states are supported, set the default
1209 * policy. Note that this policy can be changed dynamically
1210 * (e.g. encourage deeper sleeps to conserve battery life when
1211 * not on AC).
1212 */
1213 result = acpi_processor_set_power_policy(pr);
1214 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001215 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001216#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217
1218 /*
1219 * if one state of type C2 or C3 is available, mark this
1220 * CPU as being "idle manageable"
1221 */
1222 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001223 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001224 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001225 if (pr->power.states[i].type >= ACPI_STATE_C2)
1226 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001227 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001228 }
1229
Patrick Mocheld550d982006-06-27 00:41:40 -04001230 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231}
1232
Linus Torvalds1da177e2005-04-16 15:20:36 -07001233static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1234{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001235 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001236 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238
1239 if (!pr)
1240 goto end;
1241
1242 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001243 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001244 "bus master activity: %08x\n"
1245 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001246 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001247 max_cstate, (unsigned)pr->power.bm_activity,
Mark Grossf011e2e2008-02-04 22:30:09 -08001248 pm_qos_requirement(PM_QOS_CPU_DMA_LATENCY));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249
1250 seq_puts(seq, "states:\n");
1251
1252 for (i = 1; i <= pr->power.count; i++) {
1253 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001254 (&pr->power.states[i] ==
1255 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001256
1257 if (!pr->power.states[i].valid) {
1258 seq_puts(seq, "<not supported>\n");
1259 continue;
1260 }
1261
1262 switch (pr->power.states[i].type) {
1263 case ACPI_STATE_C1:
1264 seq_printf(seq, "type[C1] ");
1265 break;
1266 case ACPI_STATE_C2:
1267 seq_printf(seq, "type[C2] ");
1268 break;
1269 case ACPI_STATE_C3:
1270 seq_printf(seq, "type[C3] ");
1271 break;
1272 default:
1273 seq_printf(seq, "type[--] ");
1274 break;
1275 }
1276
1277 if (pr->power.states[i].promotion.state)
1278 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001279 (pr->power.states[i].promotion.state -
1280 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281 else
1282 seq_puts(seq, "promotion[--] ");
1283
1284 if (pr->power.states[i].demotion.state)
1285 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001286 (pr->power.states[i].demotion.state -
1287 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288 else
1289 seq_puts(seq, "demotion[--] ");
1290
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001291 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001292 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001293 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001294 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 }
1296
Len Brown4be44fc2005-08-05 00:44:28 -04001297 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001298 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299}
1300
1301static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1302{
1303 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001304 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001305}
1306
Arjan van de Vend7508032006-07-04 13:06:00 -04001307static const struct file_operations acpi_processor_power_fops = {
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001308 .owner = THIS_MODULE,
Len Brown4be44fc2005-08-05 00:44:28 -04001309 .open = acpi_processor_power_open_fs,
1310 .read = seq_read,
1311 .llseek = seq_lseek,
1312 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313};
1314
Len Brown4f86d3a2007-10-03 18:58:00 -04001315#ifndef CONFIG_CPU_IDLE
1316
1317int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1318{
1319 int result = 0;
1320
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001321 if (boot_option_idle_override)
1322 return 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001323
1324 if (!pr)
1325 return -EINVAL;
1326
1327 if (nocst) {
1328 return -ENODEV;
1329 }
1330
1331 if (!pr->flags.power_setup_done)
1332 return -ENODEV;
1333
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001334 /*
1335 * Fall back to the default idle loop, when pm_idle_save had
1336 * been initialized.
1337 */
1338 if (pm_idle_save) {
1339 pm_idle = pm_idle_save;
1340 /* Relies on interrupts forcing exit from idle. */
1341 synchronize_sched();
1342 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001343
1344 pr->flags.power = 0;
1345 result = acpi_processor_get_power_info(pr);
1346 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1347 pm_idle = acpi_processor_idle;
1348
1349 return result;
1350}
1351
Andrew Morton1fec74a2006-10-17 00:09:58 -07001352#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001353static void smp_callback(void *v)
1354{
1355 /* we already woke the CPU up, nothing more to do */
1356}
1357
1358/*
1359 * This function gets called when a part of the kernel has a new latency
1360 * requirement. This means we need to get all processors out of their C-state,
1361 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1362 * wakes them all right up.
1363 */
1364static int acpi_processor_latency_notify(struct notifier_block *b,
1365 unsigned long l, void *v)
1366{
Jens Axboe8691e5a2008-06-06 11:18:06 +02001367 smp_call_function(smp_callback, NULL, 1);
Arjan van de Ven5c875792006-09-30 23:27:17 -07001368 return NOTIFY_OK;
1369}
1370
1371static struct notifier_block acpi_processor_latency_notifier = {
1372 .notifier_call = acpi_processor_latency_notify,
1373};
Len Brown4f86d3a2007-10-03 18:58:00 -04001374
Andrew Morton1fec74a2006-10-17 00:09:58 -07001375#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001376
Len Brown4f86d3a2007-10-03 18:58:00 -04001377#else /* CONFIG_CPU_IDLE */
1378
1379/**
1380 * acpi_idle_bm_check - checks if bus master activity was detected
1381 */
1382static int acpi_idle_bm_check(void)
1383{
1384 u32 bm_status = 0;
1385
1386 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1387 if (bm_status)
1388 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1389 /*
1390 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1391 * the true state of bus mastering activity; forcing us to
1392 * manually check the BMIDEA bit of each IDE channel.
1393 */
1394 else if (errata.piix4.bmisx) {
1395 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1396 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1397 bm_status = 1;
1398 }
1399 return bm_status;
1400}
1401
1402/**
1403 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1404 * @pr: the processor
1405 * @target: the new target state
1406 */
1407static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1408 struct acpi_processor_cx *target)
1409{
1410 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1411 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1412 pr->flags.bm_rld_set = 0;
1413 }
1414
1415 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1416 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1417 pr->flags.bm_rld_set = 1;
1418 }
1419}
1420
1421/**
1422 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1423 * @cx: cstate data
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001424 *
1425 * Caller disables interrupt before call and enables interrupt after return.
Len Brown4f86d3a2007-10-03 18:58:00 -04001426 */
1427static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1428{
Steven Rostedtdcf309972008-07-25 18:00:42 -04001429 /* Don't trace irqs off for idle */
1430 stop_critical_timings();
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001431 if (cx->entry_method == ACPI_CSTATE_FFH) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001432 /* Call into architectural FFH based C-state */
1433 acpi_processor_ffh_cstate_enter(cx);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001434 } else if (cx->entry_method == ACPI_CSTATE_HALT) {
1435 acpi_safe_halt();
Len Brown4f86d3a2007-10-03 18:58:00 -04001436 } else {
1437 int unused;
1438 /* IO port based C-state */
1439 inb(cx->address);
1440 /* Dummy wait op - must do something useless after P_LVL2 read
1441 because chipsets cannot guarantee that STPCLK# signal
1442 gets asserted in time to freeze execution properly. */
1443 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1444 }
Steven Rostedtdcf309972008-07-25 18:00:42 -04001445 start_critical_timings();
Len Brown4f86d3a2007-10-03 18:58:00 -04001446}
1447
1448/**
1449 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1450 * @dev: the target CPU
1451 * @state: the state data
1452 *
1453 * This is equivalent to the HALT instruction.
1454 */
1455static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1456 struct cpuidle_state *state)
1457{
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001458 u32 t1, t2;
Len Brown4f86d3a2007-10-03 18:58:00 -04001459 struct acpi_processor *pr;
1460 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001461
Mike Travis706546d2008-06-09 16:22:23 -07001462 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001463
1464 if (unlikely(!pr))
1465 return 0;
1466
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001467 local_irq_disable();
Venkatesh Pallipadib077fba2008-02-11 15:20:27 -08001468
1469 /* Do not access any ACPI IO ports in suspend path */
1470 if (acpi_idle_suspend) {
1471 acpi_safe_halt();
1472 local_irq_enable();
1473 return 0;
1474 }
1475
Len Brown4f86d3a2007-10-03 18:58:00 -04001476 if (pr->flags.bm_check)
1477 acpi_idle_update_bm_rld(pr, cx);
1478
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001479 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
venkatesh.pallipadi@intel.combc71bec2008-01-31 17:35:04 -08001480 acpi_idle_do_entry(cx);
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001481 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001482
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001483 local_irq_enable();
Len Brown4f86d3a2007-10-03 18:58:00 -04001484 cx->usage++;
1485
venkatesh.pallipadi@intel.com9b12e182008-01-31 17:35:05 -08001486 return ticks_elapsed_in_us(t1, t2);
Len Brown4f86d3a2007-10-03 18:58:00 -04001487}
1488
1489/**
1490 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1491 * @dev: the target CPU
1492 * @state: the state data
1493 */
1494static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1495 struct cpuidle_state *state)
1496{
1497 struct acpi_processor *pr;
1498 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1499 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001500 int sleep_ticks = 0;
1501
Mike Travis706546d2008-06-09 16:22:23 -07001502 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001503
1504 if (unlikely(!pr))
1505 return 0;
1506
Len Browne1964412007-10-04 01:23:47 -04001507 if (acpi_idle_suspend)
1508 return(acpi_idle_enter_c1(dev, state));
1509
Len Brown4f86d3a2007-10-03 18:58:00 -04001510 local_irq_disable();
1511 current_thread_info()->status &= ~TS_POLLING;
1512 /*
1513 * TS_POLLING-cleared state must be visible before we test
1514 * NEED_RESCHED:
1515 */
1516 smp_mb();
1517
1518 if (unlikely(need_resched())) {
1519 current_thread_info()->status |= TS_POLLING;
1520 local_irq_enable();
1521 return 0;
1522 }
1523
Thomas Gleixnere17bcb42007-12-07 19:16:17 +01001524 /*
1525 * Must be done before busmaster disable as we might need to
1526 * access HPET !
1527 */
1528 acpi_state_timer_broadcast(pr, cx, 1);
1529
1530 if (pr->flags.bm_check)
1531 acpi_idle_update_bm_rld(pr, cx);
1532
Len Brown4f86d3a2007-10-03 18:58:00 -04001533 if (cx->type == ACPI_STATE_C3)
1534 ACPI_FLUSH_CPU_CACHE();
1535
1536 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001537 /* Tell the scheduler that we are going deep-idle: */
1538 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001539 acpi_idle_do_entry(cx);
1540 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1541
Pavel Machek61331162008-02-19 11:00:29 +01001542#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001543 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001544 if (tsc_halts_in_c(cx->type))
1545 mark_tsc_unstable("TSC halts in idle");;
Len Brown4f86d3a2007-10-03 18:58:00 -04001546#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001547 sleep_ticks = ticks_elapsed(t1, t2);
1548
1549 /* Tell the scheduler how much we idled: */
1550 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001551
1552 local_irq_enable();
1553 current_thread_info()->status |= TS_POLLING;
1554
1555 cx->usage++;
1556
1557 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001558 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001559 return ticks_elapsed_in_us(t1, t2);
1560}
1561
1562static int c3_cpu_count;
1563static DEFINE_SPINLOCK(c3_lock);
1564
1565/**
1566 * acpi_idle_enter_bm - enters C3 with proper BM handling
1567 * @dev: the target CPU
1568 * @state: the state data
1569 *
1570 * If BM is detected, the deepest non-C3 idle state is entered instead.
1571 */
1572static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1573 struct cpuidle_state *state)
1574{
1575 struct acpi_processor *pr;
1576 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1577 u32 t1, t2;
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001578 int sleep_ticks = 0;
1579
Mike Travis706546d2008-06-09 16:22:23 -07001580 pr = __get_cpu_var(processors);
Len Brown4f86d3a2007-10-03 18:58:00 -04001581
1582 if (unlikely(!pr))
1583 return 0;
1584
Len Browne1964412007-10-04 01:23:47 -04001585 if (acpi_idle_suspend)
1586 return(acpi_idle_enter_c1(dev, state));
1587
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001588 if (acpi_idle_bm_check()) {
1589 if (dev->safe_state) {
Venkatesh Pallipadiaddbad42008-09-29 15:24:28 -07001590 dev->last_state = dev->safe_state;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001591 return dev->safe_state->enter(dev, dev->safe_state);
1592 } else {
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001593 local_irq_disable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001594 acpi_safe_halt();
venkatesh.pallipadi@intel.com2e906652008-01-31 17:35:03 -08001595 local_irq_enable();
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001596 return 0;
1597 }
1598 }
1599
Len Brown4f86d3a2007-10-03 18:58:00 -04001600 local_irq_disable();
1601 current_thread_info()->status &= ~TS_POLLING;
1602 /*
1603 * TS_POLLING-cleared state must be visible before we test
1604 * NEED_RESCHED:
1605 */
1606 smp_mb();
1607
1608 if (unlikely(need_resched())) {
1609 current_thread_info()->status |= TS_POLLING;
1610 local_irq_enable();
1611 return 0;
1612 }
1613
Venki Pallipadi996520c2008-03-24 14:24:10 -07001614 acpi_unlazy_tlb(smp_processor_id());
1615
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001616 /* Tell the scheduler that we are going deep-idle: */
1617 sched_clock_idle_sleep_event();
Len Brown4f86d3a2007-10-03 18:58:00 -04001618 /*
1619 * Must be done before busmaster disable as we might need to
1620 * access HPET !
1621 */
1622 acpi_state_timer_broadcast(pr, cx, 1);
1623
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001624 acpi_idle_update_bm_rld(pr, cx);
Len Brown4f86d3a2007-10-03 18:58:00 -04001625
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001626 /*
1627 * disable bus master
1628 * bm_check implies we need ARB_DIS
1629 * !bm_check implies we need cache flush
1630 * bm_control implies whether we can do ARB_DIS
1631 *
1632 * That leaves a case where bm_check is set and bm_control is
1633 * not set. In that case we cannot do much, we enter C3
1634 * without doing anything.
1635 */
1636 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001637 spin_lock(&c3_lock);
1638 c3_cpu_count++;
1639 /* Disable bus master arbitration when all CPUs are in C3 */
1640 if (c3_cpu_count == num_online_cpus())
1641 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1642 spin_unlock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001643 } else if (!pr->flags.bm_check) {
1644 ACPI_FLUSH_CPU_CACHE();
1645 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001646
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001647 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1648 acpi_idle_do_entry(cx);
1649 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Len Brown4f86d3a2007-10-03 18:58:00 -04001650
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001651 /* Re-enable bus master arbitration */
1652 if (pr->flags.bm_check && pr->flags.bm_control) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001653 spin_lock(&c3_lock);
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001654 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Len Brown4f86d3a2007-10-03 18:58:00 -04001655 c3_cpu_count--;
1656 spin_unlock(&c3_lock);
1657 }
1658
Pavel Machek61331162008-02-19 11:00:29 +01001659#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86)
Len Brown4f86d3a2007-10-03 18:58:00 -04001660 /* TSC could halt in idle, so notify users */
Andi Kleenddb25f92008-01-30 13:32:41 +01001661 if (tsc_halts_in_c(ACPI_STATE_C3))
1662 mark_tsc_unstable("TSC halts in idle");
Len Brown4f86d3a2007-10-03 18:58:00 -04001663#endif
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001664 sleep_ticks = ticks_elapsed(t1, t2);
1665 /* Tell the scheduler how much we idled: */
1666 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
Len Brown4f86d3a2007-10-03 18:58:00 -04001667
1668 local_irq_enable();
1669 current_thread_info()->status |= TS_POLLING;
1670
1671 cx->usage++;
1672
1673 acpi_state_timer_broadcast(pr, cx, 0);
Venkatesh Pallipadi50629112007-11-19 19:49:00 -05001674 cx->time += sleep_ticks;
Len Brown4f86d3a2007-10-03 18:58:00 -04001675 return ticks_elapsed_in_us(t1, t2);
1676}
1677
1678struct cpuidle_driver acpi_idle_driver = {
1679 .name = "acpi_idle",
1680 .owner = THIS_MODULE,
1681};
1682
1683/**
1684 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1685 * @pr: the ACPI processor
1686 */
1687static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1688{
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001689 int i, count = CPUIDLE_DRIVER_STATE_START;
Len Brown4f86d3a2007-10-03 18:58:00 -04001690 struct acpi_processor_cx *cx;
1691 struct cpuidle_state *state;
1692 struct cpuidle_device *dev = &pr->power.dev;
1693
1694 if (!pr->flags.power_setup_done)
1695 return -EINVAL;
1696
1697 if (pr->flags.power == 0) {
1698 return -EINVAL;
1699 }
1700
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001701 dev->cpu = pr->id;
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001702 for (i = 0; i < CPUIDLE_STATE_MAX; i++) {
1703 dev->states[i].name[0] = '\0';
1704 dev->states[i].desc[0] = '\0';
1705 }
1706
Len Brown4f86d3a2007-10-03 18:58:00 -04001707 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1708 cx = &pr->power.states[i];
1709 state = &dev->states[count];
1710
1711 if (!cx->valid)
1712 continue;
1713
1714#ifdef CONFIG_HOTPLUG_CPU
1715 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1716 !pr->flags.has_cst &&
1717 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1718 continue;
1719#endif
1720 cpuidle_set_statedata(state, cx);
1721
1722 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
Venkatesh Pallipadi4fcb2fc2008-02-11 17:46:31 -08001723 strncpy(state->desc, cx->desc, CPUIDLE_DESC_LEN);
Len Brown4f86d3a2007-10-03 18:58:00 -04001724 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001725 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001726 state->power_usage = cx->power;
1727
1728 state->flags = 0;
1729 switch (cx->type) {
1730 case ACPI_STATE_C1:
1731 state->flags |= CPUIDLE_FLAG_SHALLOW;
Venki Pallipadi8e92b662008-02-29 10:24:32 -08001732 if (cx->entry_method == ACPI_CSTATE_FFH)
1733 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1734
Len Brown4f86d3a2007-10-03 18:58:00 -04001735 state->enter = acpi_idle_enter_c1;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001736 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001737 break;
1738
1739 case ACPI_STATE_C2:
1740 state->flags |= CPUIDLE_FLAG_BALANCED;
1741 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1742 state->enter = acpi_idle_enter_simple;
Venkatesh Pallipadiddc081a2007-11-19 21:43:22 -05001743 dev->safe_state = state;
Len Brown4f86d3a2007-10-03 18:58:00 -04001744 break;
1745
1746 case ACPI_STATE_C3:
1747 state->flags |= CPUIDLE_FLAG_DEEP;
1748 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1749 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1750 state->enter = pr->flags.bm_check ?
1751 acpi_idle_enter_bm :
1752 acpi_idle_enter_simple;
1753 break;
1754 }
1755
1756 count++;
venkatesh.pallipadi@intel.com9a0b8412008-01-31 17:35:06 -08001757 if (count == CPUIDLE_STATE_MAX)
1758 break;
Len Brown4f86d3a2007-10-03 18:58:00 -04001759 }
1760
1761 dev->state_count = count;
1762
1763 if (!count)
1764 return -EINVAL;
1765
Len Brown4f86d3a2007-10-03 18:58:00 -04001766 return 0;
1767}
1768
1769int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1770{
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001771 int ret = 0;
Len Brown4f86d3a2007-10-03 18:58:00 -04001772
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001773 if (boot_option_idle_override)
1774 return 0;
1775
Len Brown4f86d3a2007-10-03 18:58:00 -04001776 if (!pr)
1777 return -EINVAL;
1778
1779 if (nocst) {
1780 return -ENODEV;
1781 }
1782
1783 if (!pr->flags.power_setup_done)
1784 return -ENODEV;
1785
1786 cpuidle_pause_and_lock();
1787 cpuidle_disable_device(&pr->power.dev);
1788 acpi_processor_get_power_info(pr);
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001789 if (pr->flags.power) {
1790 acpi_processor_setup_cpuidle(pr);
1791 ret = cpuidle_enable_device(&pr->power.dev);
1792 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001793 cpuidle_resume_and_unlock();
1794
1795 return ret;
1796}
1797
1798#endif /* CONFIG_CPU_IDLE */
1799
Pierre Ossman7af8b662006-10-10 14:20:31 -07001800int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001801 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001802{
Len Brown4be44fc2005-08-05 00:44:28 -04001803 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001804 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001805 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 unsigned int i;
1807
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001808 if (boot_option_idle_override)
1809 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 if (!first_run) {
Zhao Yakuic1e3b372008-06-24 17:58:53 +08001812 if (idle_halt) {
1813 /*
1814 * When the boot option of "idle=halt" is added, halt
1815 * is used for CPU IDLE.
1816 * In such case C2/C3 is meaningless. So the max_cstate
1817 * is set to one.
1818 */
1819 max_cstate = 1;
1820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001821 dmi_check_system(processor_power_dmi_table);
Alexey Starikovskiyc1c30632007-11-26 20:42:19 +01001822 max_cstate = acpi_processor_cstate_check(max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001824 printk(KERN_NOTICE
1825 "ACPI: processor limited to max C-state %d\n",
1826 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827 first_run++;
Mark Grossf011e2e2008-02-04 22:30:09 -08001828#if !defined(CONFIG_CPU_IDLE) && defined(CONFIG_SMP)
1829 pm_qos_add_notifier(PM_QOS_CPU_DMA_LATENCY,
1830 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001831#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832 }
1833
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001834 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001835 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001836
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001837 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001838 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001839 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001841 ACPI_EXCEPTION((AE_INFO, status,
1842 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843 }
1844 }
1845
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001847 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848
1849 /*
1850 * Install the idle handler if processor power management is supported.
1851 * Note that we use previously set idle handler will be used on
1852 * platforms that only support C1.
1853 */
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001854 if (pr->flags.power) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001855#ifdef CONFIG_CPU_IDLE
1856 acpi_processor_setup_cpuidle(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001857 if (cpuidle_register_device(&pr->power.dev))
1858 return -EIO;
1859#endif
1860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1862 for (i = 1; i <= pr->power.count; i++)
1863 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001864 printk(" C%d[C%d]", i,
1865 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 printk(")\n");
1867
Len Brown4f86d3a2007-10-03 18:58:00 -04001868#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 if (pr->id == 0) {
1870 pm_idle_save = pm_idle;
1871 pm_idle = acpi_processor_idle;
1872 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001873#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 }
1875
1876 /* 'power' [R] */
Denis V. Lunevcf7acfa2008-04-29 01:02:27 -07001877 entry = proc_create_data(ACPI_PROCESSOR_FILE_POWER,
1878 S_IRUGO, acpi_device_dir(device),
1879 &acpi_processor_power_fops,
1880 acpi_driver_data(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001882 return -EIO;
Patrick Mocheld550d982006-06-27 00:41:40 -04001883 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001884}
1885
Len Brown4be44fc2005-08-05 00:44:28 -04001886int acpi_processor_power_exit(struct acpi_processor *pr,
1887 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001888{
Venkatesh Pallipadi36a91352008-04-30 13:57:15 -04001889 if (boot_option_idle_override)
1890 return 0;
1891
Len Brown4f86d3a2007-10-03 18:58:00 -04001892#ifdef CONFIG_CPU_IDLE
Venkatesh Pallipadidcb84f32008-05-19 19:09:27 -04001893 cpuidle_unregister_device(&pr->power.dev);
Len Brown4f86d3a2007-10-03 18:58:00 -04001894#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 pr->flags.power_setup_done = 0;
1896
1897 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001898 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1899 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900
Len Brown4f86d3a2007-10-03 18:58:00 -04001901#ifndef CONFIG_CPU_IDLE
1902
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903 /* Unregister the idle handler when processor #0 is removed. */
1904 if (pr->id == 0) {
Thomas Gleixnerb032bf70d2008-07-27 23:47:12 +02001905 if (pm_idle_save)
1906 pm_idle = pm_idle_save;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907
1908 /*
1909 * We are about to unload the current idle thread pm callback
1910 * (pm_idle), Wait for all processors to update cached/local
1911 * copies of pm_idle before proceeding.
1912 */
1913 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001914#ifdef CONFIG_SMP
Mark Grossf011e2e2008-02-04 22:30:09 -08001915 pm_qos_remove_notifier(PM_QOS_CPU_DMA_LATENCY,
1916 &acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001917#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001918 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001919#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920
Patrick Mocheld550d982006-06-27 00:41:40 -04001921 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922}