blob: 26ade1f3f5cd4f67cbd158304d42d8dd5ce95425 [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() */
Arjan van de Ven5c875792006-09-30 23:27:17 -070041#include <linux/latency.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>
60
61#define ACPI_PROCESSOR_COMPONENT 0x01000000
62#define ACPI_PROCESSOR_CLASS "processor"
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#define _COMPONENT ACPI_PROCESSOR_COMPONENT
Len Brownf52fd662007-02-12 22:42:12 -050064ACPI_MODULE_NAME("processor_idle");
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#define ACPI_PROCESSOR_FILE_POWER "power"
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#define US_TO_PM_TIMER_TICKS(t) ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
Ingo Molnar2aa44d02007-08-23 15:18:02 +020067#define PM_TIMER_TICK_NS (1000000000ULL/PM_TIMER_FREQUENCY)
Len Brown4f86d3a2007-10-03 18:58:00 -040068#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#define C2_OVERHEAD 4 /* 1us (3.579 ticks per us) */
70#define C3_OVERHEAD 4 /* 1us (3.579 ticks per us) */
Andreas Mohrb6835052006-04-27 05:25:00 -040071static void (*pm_idle_save) (void) __read_mostly;
Len Brown4f86d3a2007-10-03 18:58:00 -040072#else
73#define C2_OVERHEAD 1 /* 1us */
74#define C3_OVERHEAD 1 /* 1us */
75#endif
76#define PM_TIMER_TICKS_TO_US(p) (((p) * 1000)/(PM_TIMER_FREQUENCY/1000))
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
Len Brown4f86d3a2007-10-03 18:58:00 -040078static unsigned int max_cstate __read_mostly = ACPI_PROCESSOR_MAX_POWER;
79module_param(max_cstate, uint, 0000);
Andreas Mohrb6835052006-04-27 05:25:00 -040080static unsigned int nocst __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081module_param(nocst, uint, 0000);
82
Len Brown4f86d3a2007-10-03 18:58:00 -040083#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * bm_history -- bit-mask with a bit per jiffy of bus-master activity
86 * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
87 * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
88 * 100 HZ: 0x0000000F: 4 jiffies = 40ms
89 * reduce history for more aggressive entry into C3
90 */
Andreas Mohrb6835052006-04-27 05:25:00 -040091static unsigned int bm_history __read_mostly =
Len Brown4be44fc2005-08-05 00:44:28 -040092 (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
Linus Torvalds1da177e2005-04-16 15:20:36 -070093module_param(bm_history, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -040094
95static int acpi_processor_set_power_policy(struct acpi_processor *pr);
96
Len Brown4963f622007-12-13 23:50:45 -050097#else /* CONFIG_CPU_IDLE */
98static unsigned int latency_factor __read_mostly = 6;
99module_param(latency_factor, uint, 0644);
Len Brown4f86d3a2007-10-03 18:58:00 -0400100#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102/*
103 * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
104 * For now disable this. Probably a bug somewhere else.
105 *
106 * To skip this limit, boot/load with a large max_cstate limit.
107 */
Jeff Garzik18552562007-10-03 15:15:40 -0400108static int set_max_cstate(const struct dmi_system_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
111 return 0;
112
Len Brown3d356002005-08-03 00:22:52 -0400113 printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
Len Brown4be44fc2005-08-05 00:44:28 -0400114 " Override with \"processor.max_cstate=%d\"\n", id->ident,
115 (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
Len Brown3d356002005-08-03 00:22:52 -0400117 max_cstate = (long)id->driver_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
119 return 0;
120}
121
Ashok Raj7ded5682006-02-03 21:51:23 +0100122/* Actually this shouldn't be __cpuinitdata, would be better to fix the
123 callers to only run once -AK */
124static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
Thomas Rosner876c1842006-01-06 01:31:00 -0500125 { set_max_cstate, "IBM ThinkPad R40e", {
126 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Bartlomiej Swierczf8313352006-05-29 07:16:00 -0400127 DMI_MATCH(DMI_BIOS_VERSION,"1SET70WW")}, (void *)1},
128 { set_max_cstate, "IBM ThinkPad R40e", {
129 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
Thomas Rosner876c1842006-01-06 01:31:00 -0500130 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
131 { set_max_cstate, "IBM ThinkPad R40e", {
132 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
133 DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
134 { set_max_cstate, "IBM ThinkPad R40e", {
135 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
136 DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
137 { set_max_cstate, "IBM ThinkPad R40e", {
138 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
139 DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
140 { set_max_cstate, "IBM ThinkPad R40e", {
141 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
142 DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
143 { set_max_cstate, "IBM ThinkPad R40e", {
144 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
145 DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
146 { set_max_cstate, "IBM ThinkPad R40e", {
147 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
148 DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
149 { set_max_cstate, "IBM ThinkPad R40e", {
150 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
151 DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
152 { set_max_cstate, "IBM ThinkPad R40e", {
153 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
154 DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
155 { set_max_cstate, "IBM ThinkPad R40e", {
156 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
157 DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
158 { set_max_cstate, "IBM ThinkPad R40e", {
159 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
160 DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
161 { set_max_cstate, "IBM ThinkPad R40e", {
162 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
163 DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
164 { set_max_cstate, "IBM ThinkPad R40e", {
165 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
166 DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
167 { set_max_cstate, "IBM ThinkPad R40e", {
168 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
169 DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
170 { set_max_cstate, "IBM ThinkPad R40e", {
171 DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
172 DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
173 { set_max_cstate, "Medion 41700", {
174 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
175 DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
176 { set_max_cstate, "Clevo 5600D", {
177 DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
178 DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
Len Brown4be44fc2005-08-05 00:44:28 -0400179 (void *)2},
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 {},
181};
182
Len Brown4be44fc2005-08-05 00:44:28 -0400183static inline u32 ticks_elapsed(u32 t1, u32 t2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184{
185 if (t2 >= t1)
186 return (t2 - t1);
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300187 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
189 else
190 return ((0xFFFFFFFF - t1) + t2);
191}
192
Len Brown4f86d3a2007-10-03 18:58:00 -0400193static inline u32 ticks_elapsed_in_us(u32 t1, u32 t2)
194{
195 if (t2 >= t1)
196 return PM_TIMER_TICKS_TO_US(t2 - t1);
197 else if (!(acpi_gbl_FADT.flags & ACPI_FADT_32BIT_TIMER))
198 return PM_TIMER_TICKS_TO_US(((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
199 else
200 return PM_TIMER_TICKS_TO_US((0xFFFFFFFF - t1) + t2);
201}
202
203#ifndef CONFIG_CPU_IDLE
204
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205static void
Len Brown4be44fc2005-08-05 00:44:28 -0400206acpi_processor_power_activate(struct acpi_processor *pr,
207 struct acpi_processor_cx *new)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Len Brown4be44fc2005-08-05 00:44:28 -0400209 struct acpi_processor_cx *old;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 if (!pr || !new)
212 return;
213
214 old = pr->power.state;
215
216 if (old)
217 old->promotion.count = 0;
Len Brown4be44fc2005-08-05 00:44:28 -0400218 new->demotion.count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Cleanup from old state. */
221 if (old) {
222 switch (old->type) {
223 case ACPI_STATE_C3:
224 /* Disable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400225 if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300226 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 break;
228 }
229 }
230
231 /* Prepare to use new state. */
232 switch (new->type) {
233 case ACPI_STATE_C3:
234 /* Enable bus master reload */
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400235 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
Bob Moored8c71b62007-02-02 19:48:21 +0300236 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 break;
238 }
239
240 pr->power.state = new;
241
242 return;
243}
244
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800245static void acpi_safe_halt(void)
246{
Andi Kleen495ab9c2006-06-26 13:59:11 +0200247 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800248 /*
249 * TS_POLLING-cleared state must be visible before we
250 * test NEED_RESCHED:
251 */
252 smp_mb();
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800253 if (!need_resched())
254 safe_halt();
Andi Kleen495ab9c2006-06-26 13:59:11 +0200255 current_thread_info()->status |= TS_POLLING;
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800256}
257
Len Brown4be44fc2005-08-05 00:44:28 -0400258static atomic_t c3_cpu_count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700260/* Common C-state entry for C2, C3, .. */
261static void acpi_cstate_enter(struct acpi_processor_cx *cstate)
262{
263 if (cstate->space_id == ACPI_CSTATE_FFH) {
264 /* Call into architectural FFH based C-state */
265 acpi_processor_ffh_cstate_enter(cstate);
266 } else {
267 int unused;
268 /* IO port based C-state */
269 inb(cstate->address);
270 /* Dummy wait op - must do something useless after P_LVL2 read
271 because chipsets cannot guarantee that STPCLK# signal
272 gets asserted in time to freeze execution properly. */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300273 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700274 }
275}
Len Brown4f86d3a2007-10-03 18:58:00 -0400276#endif /* !CONFIG_CPU_IDLE */
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700277
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800278#ifdef ARCH_APICTIMER_STOPS_ON_C3
279
280/*
281 * Some BIOS implementations switch to C3 in the published C2 state.
Linus Torvalds296d93c2007-03-23 08:03:47 -0700282 * This seems to be a common problem on AMD boxen, but other vendors
283 * are affected too. We pick the most conservative approach: we assume
284 * that the local APIC stops in both C2 and C3.
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800285 */
286static void acpi_timer_check_state(int state, struct acpi_processor *pr,
287 struct acpi_processor_cx *cx)
288{
289 struct acpi_processor_power *pwr = &pr->power;
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100290 u8 type = local_apic_timer_c2_ok ? ACPI_STATE_C3 : ACPI_STATE_C2;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800291
292 /*
293 * Check, if one of the previous states already marked the lapic
294 * unstable
295 */
296 if (pwr->timer_broadcast_on_state < state)
297 return;
298
Thomas Gleixnere585bef2007-03-23 16:08:01 +0100299 if (cx->type >= type)
Linus Torvalds296d93c2007-03-23 08:03:47 -0700300 pr->power.timer_broadcast_on_state = state;
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800301}
302
303static void acpi_propagate_timer_broadcast(struct acpi_processor *pr)
304{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800305 unsigned long reason;
306
307 reason = pr->power.timer_broadcast_on_state < INT_MAX ?
308 CLOCK_EVT_NOTIFY_BROADCAST_ON : CLOCK_EVT_NOTIFY_BROADCAST_OFF;
309
310 clockevents_notify(reason, &pr->id);
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800311}
312
313/* Power(C) State timer broadcast control */
314static void acpi_state_timer_broadcast(struct acpi_processor *pr,
315 struct acpi_processor_cx *cx,
316 int broadcast)
317{
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800318 int state = cx - pr->power.states;
319
320 if (state >= pr->power.timer_broadcast_on_state) {
321 unsigned long reason;
322
323 reason = broadcast ? CLOCK_EVT_NOTIFY_BROADCAST_ENTER :
324 CLOCK_EVT_NOTIFY_BROADCAST_EXIT;
325 clockevents_notify(reason, &pr->id);
326 }
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800327}
328
329#else
330
331static void acpi_timer_check_state(int state, struct acpi_processor *pr,
332 struct acpi_processor_cx *cstate) { }
333static void acpi_propagate_timer_broadcast(struct acpi_processor *pr) { }
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800334static void acpi_state_timer_broadcast(struct acpi_processor *pr,
335 struct acpi_processor_cx *cx,
336 int broadcast)
337{
338}
Thomas Gleixner169a0ab2007-02-16 01:27:55 -0800339
340#endif
341
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000342/*
343 * Suspend / resume control
344 */
345static int acpi_idle_suspend;
346
347int acpi_processor_suspend(struct acpi_device * device, pm_message_t state)
348{
349 acpi_idle_suspend = 1;
350 return 0;
351}
352
353int acpi_processor_resume(struct acpi_device * device)
354{
355 acpi_idle_suspend = 0;
356 return 0;
357}
358
Len Brown4f86d3a2007-10-03 18:58:00 -0400359#ifndef CONFIG_CPU_IDLE
Len Brown4be44fc2005-08-05 00:44:28 -0400360static void acpi_processor_idle(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361{
Len Brown4be44fc2005-08-05 00:44:28 -0400362 struct acpi_processor *pr = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 struct acpi_processor_cx *cx = NULL;
364 struct acpi_processor_cx *next_state = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400365 int sleep_ticks = 0;
366 u32 t1, t2 = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /*
369 * Interrupts must be disabled during bus mastering calculations and
370 * for C2/C3 transitions.
371 */
372 local_irq_disable();
373
Venkatesh Pallipadid5a3d322007-06-15 19:36:00 -0400374 pr = processors[smp_processor_id()];
375 if (!pr) {
376 local_irq_enable();
377 return;
378 }
379
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 /*
381 * Check whether we truly need to go idle, or should
382 * reschedule:
383 */
384 if (unlikely(need_resched())) {
385 local_irq_enable();
386 return;
387 }
388
389 cx = pr->power.state;
Thomas Gleixnerb04e7bd2007-09-22 22:29:05 +0000390 if (!cx || acpi_idle_suspend) {
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800391 if (pm_idle_save)
392 pm_idle_save();
393 else
394 acpi_safe_halt();
395 return;
396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397
398 /*
399 * Check BM Activity
400 * -----------------
401 * Check for bus mastering activity (if required), record, and check
402 * for demotion.
403 */
404 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400405 u32 bm_status = 0;
406 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400408 if (diff > 31)
409 diff = 31;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400411 pr->power.bm_activity <<= diff;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
Bob Moored8c71b62007-02-02 19:48:21 +0300413 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (bm_status) {
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400415 pr->power.bm_activity |= 0x1;
Bob Moored8c71b62007-02-02 19:48:21 +0300416 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418 /*
419 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
420 * the true state of bus mastering activity; forcing us to
421 * manually check the BMIDEA bit of each IDE channel.
422 */
423 else if (errata.piix4.bmisx) {
424 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
Len Brown4be44fc2005-08-05 00:44:28 -0400425 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
Dominik Brodowskic5ab81c2006-06-24 19:37:00 -0400426 pr->power.bm_activity |= 0x1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 }
428
429 pr->power.bm_check_timestamp = jiffies;
430
431 /*
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400432 * If bus mastering is or was active this jiffy, demote
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 * to avoid a faulty transition. Note that the processor
434 * won't enter a low-power state during this call (to this
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400435 * function) but should upon the next.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 *
437 * TBD: A better policy might be to fallback to the demotion
438 * state (use it for this quantum only) istead of
439 * demoting -- and rely on duration as our sole demotion
440 * qualification. This may, however, introduce DMA
441 * issues (e.g. floppy DMA transfer overrun/underrun).
442 */
Dominik Brodowskic4a001b2006-06-24 19:37:00 -0400443 if ((pr->power.bm_activity & 0x1) &&
444 cx->demotion.threshold.bm) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 local_irq_enable();
446 next_state = cx->demotion.state;
447 goto end;
448 }
449 }
450
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400451#ifdef CONFIG_HOTPLUG_CPU
452 /*
453 * Check for P_LVL2_UP flag before entering C2 and above on
454 * an SMP system. We do it here instead of doing it at _CST/P_LVL
455 * detection phase, to work cleanly with logical CPU hotplug.
456 */
Len Brown4f86d3a2007-10-03 18:58:00 -0400457 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300458 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
David Shaohua Li1e483962005-12-01 17:00:00 -0500459 cx = &pr->power.states[ACPI_STATE_C1];
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400460#endif
David Shaohua Li1e483962005-12-01 17:00:00 -0500461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 /*
463 * Sleep:
464 * ------
465 * Invoke the current Cx state to put the processor to sleep.
466 */
Nick Piggin2a298a32005-12-02 12:44:19 +1100467 if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200468 current_thread_info()->status &= ~TS_POLLING;
Ingo Molnar0888f062006-12-22 01:11:56 -0800469 /*
470 * TS_POLLING-cleared state must be visible before we
471 * test NEED_RESCHED:
472 */
473 smp_mb();
Nick Piggin2a298a32005-12-02 12:44:19 +1100474 if (need_resched()) {
Andi Kleen495ab9c2006-06-26 13:59:11 +0200475 current_thread_info()->status |= TS_POLLING;
Linus Torvaldsaf2eb172005-12-02 23:09:06 -0800476 local_irq_enable();
Nick Piggin2a298a32005-12-02 12:44:19 +1100477 return;
478 }
479 }
480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 switch (cx->type) {
482
483 case ACPI_STATE_C1:
484 /*
485 * Invoke C1.
486 * Use the appropriate idle routine, the one that would
487 * be used without acpi C-states.
488 */
489 if (pm_idle_save)
490 pm_idle_save();
491 else
Nick Piggin64c7c8f2005-11-08 21:39:04 -0800492 acpi_safe_halt();
493
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 /*
Len Brown4be44fc2005-08-05 00:44:28 -0400495 * TBD: Can't get time duration while in C1, as resumes
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 * go to an ISR rather than here. Need to instrument
497 * base interrupt handler.
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200498 *
499 * Note: the TSC better not stop in C1, sched_clock() will
500 * skew otherwise.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 */
502 sleep_ticks = 0xFFFFFFFF;
503 break;
504
505 case ACPI_STATE_C2:
506 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300507 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200508 /* Tell the scheduler that we are going deep-idle: */
509 sched_clock_idle_sleep_event();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /* Invoke C2 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800511 acpi_state_timer_broadcast(pr, cx, 1);
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700512 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300514 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
john stultz539eb112006-06-26 00:25:10 -0700515
Tony Luck0aa366f2007-07-20 11:22:30 -0700516#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
john stultz539eb112006-06-26 00:25:10 -0700517 /* TSC halts in C2, so notify users */
john stultz5a90cf22007-05-02 19:27:08 +0200518 mark_tsc_unstable("possible TSC halt in C2");
john stultz539eb112006-06-26 00:25:10 -0700519#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200520 /* Compute time (ticks) that we were actually asleep */
521 sleep_ticks = ticks_elapsed(t1, t2);
522
523 /* Tell the scheduler how much we idled: */
524 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
525
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 /* Re-enable interrupts */
527 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200528 /* Do not account our idle-switching overhead: */
529 sleep_ticks -= cx->latency_ticks + C2_OVERHEAD;
530
Andi Kleen495ab9c2006-06-26 13:59:11 +0200531 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800532 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 break;
534
535 case ACPI_STATE_C3:
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400536 /*
537 * disable bus master
538 * bm_check implies we need ARB_DIS
539 * !bm_check implies we need cache flush
540 * bm_control implies whether we can do ARB_DIS
541 *
542 * That leaves a case where bm_check is set and bm_control is
543 * not set. In that case we cannot do much, we enter C3
544 * without doing anything.
545 */
546 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400547 if (atomic_inc_return(&c3_cpu_count) ==
Len Brown4be44fc2005-08-05 00:44:28 -0400548 num_online_cpus()) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400549 /*
550 * All CPUs are trying to go to C3
551 * Disable bus master arbitration
552 */
Bob Moored8c71b62007-02-02 19:48:21 +0300553 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400554 }
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400555 } else if (!pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400556 /* SMP with no shared cache... Invalidate cache */
557 ACPI_FLUSH_CPU_CACHE();
558 }
Len Brown4be44fc2005-08-05 00:44:28 -0400559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 /* Get start time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300561 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 /* Invoke C3 */
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800563 acpi_state_timer_broadcast(pr, cx, 1);
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200564 /* Tell the scheduler that we are going deep-idle: */
565 sched_clock_idle_sleep_event();
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700566 acpi_cstate_enter(cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 /* Get end time (ticks) */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300568 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
Venkatesh Pallipadi18eab852007-06-15 19:37:00 -0400569 if (pr->flags.bm_check && pr->flags.bm_control) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400570 /* Enable bus master arbitration */
571 atomic_dec(&c3_cpu_count);
Bob Moored8c71b62007-02-02 19:48:21 +0300572 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400573 }
574
Tony Luck0aa366f2007-07-20 11:22:30 -0700575#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
john stultz539eb112006-06-26 00:25:10 -0700576 /* TSC halts in C3, so notify users */
john stultz5a90cf22007-05-02 19:27:08 +0200577 mark_tsc_unstable("TSC halts in C3");
john stultz539eb112006-06-26 00:25:10 -0700578#endif
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200579 /* Compute time (ticks) that we were actually asleep */
580 sleep_ticks = ticks_elapsed(t1, t2);
581 /* Tell the scheduler how much we idled: */
582 sched_clock_idle_wakeup_event(sleep_ticks*PM_TIMER_TICK_NS);
583
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 /* Re-enable interrupts */
585 local_irq_enable();
Ingo Molnar2aa44d02007-08-23 15:18:02 +0200586 /* Do not account our idle-switching overhead: */
587 sleep_ticks -= cx->latency_ticks + C3_OVERHEAD;
588
Andi Kleen495ab9c2006-06-26 13:59:11 +0200589 current_thread_info()->status |= TS_POLLING;
Thomas Gleixnere9e2cdb2007-02-16 01:28:04 -0800590 acpi_state_timer_broadcast(pr, cx, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 break;
592
593 default:
594 local_irq_enable();
595 return;
596 }
Dominik Brodowskia3c65982006-06-24 19:37:00 -0400597 cx->usage++;
598 if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
599 cx->time += sleep_ticks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600
601 next_state = pr->power.state;
602
David Shaohua Li1e483962005-12-01 17:00:00 -0500603#ifdef CONFIG_HOTPLUG_CPU
604 /* Don't do promotion/demotion */
605 if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300606 !pr->flags.has_cst && !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED)) {
David Shaohua Li1e483962005-12-01 17:00:00 -0500607 next_state = cx;
608 goto end;
609 }
610#endif
611
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 /*
613 * Promotion?
614 * ----------
615 * Track the number of longs (time asleep is greater than threshold)
616 * and promote when the count threshold is reached. Note that bus
617 * mastering activity may prevent promotions.
618 * Do not promote above max_cstate.
619 */
620 if (cx->promotion.state &&
621 ((cx->promotion.state - pr->power.states) <= max_cstate)) {
Arjan van de Ven5c875792006-09-30 23:27:17 -0700622 if (sleep_ticks > cx->promotion.threshold.ticks &&
623 cx->promotion.state->latency <= system_latency_constraint()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 cx->promotion.count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400625 cx->demotion.count = 0;
626 if (cx->promotion.count >=
627 cx->promotion.threshold.count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 if (pr->flags.bm_check) {
Len Brown4be44fc2005-08-05 00:44:28 -0400629 if (!
630 (pr->power.bm_activity & cx->
631 promotion.threshold.bm)) {
632 next_state =
633 cx->promotion.state;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 goto end;
635 }
Len Brown4be44fc2005-08-05 00:44:28 -0400636 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 next_state = cx->promotion.state;
638 goto end;
639 }
640 }
641 }
642 }
643
644 /*
645 * Demotion?
646 * ---------
647 * Track the number of shorts (time asleep is less than time threshold)
648 * and demote when the usage threshold is reached.
649 */
650 if (cx->demotion.state) {
651 if (sleep_ticks < cx->demotion.threshold.ticks) {
652 cx->demotion.count++;
653 cx->promotion.count = 0;
654 if (cx->demotion.count >= cx->demotion.threshold.count) {
655 next_state = cx->demotion.state;
656 goto end;
657 }
658 }
659 }
660
Len Brown4be44fc2005-08-05 00:44:28 -0400661 end:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662 /*
663 * Demote if current state exceeds max_cstate
Arjan van de Ven5c875792006-09-30 23:27:17 -0700664 * or if the latency of the current state is unacceptable
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 */
Arjan van de Ven5c875792006-09-30 23:27:17 -0700666 if ((pr->power.state - pr->power.states) > max_cstate ||
667 pr->power.state->latency > system_latency_constraint()) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (cx->demotion.state)
669 next_state = cx->demotion.state;
670 }
671
672 /*
673 * New Cx State?
674 * -------------
675 * If we're going to start using a new Cx state we must clean up
676 * from the previous and prepare to use the new.
677 */
678 if (next_state != pr->power.state)
679 acpi_processor_power_activate(pr, next_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680}
681
Len Brown4be44fc2005-08-05 00:44:28 -0400682static int acpi_processor_set_power_policy(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683{
684 unsigned int i;
685 unsigned int state_is_set = 0;
686 struct acpi_processor_cx *lower = NULL;
687 struct acpi_processor_cx *higher = NULL;
688 struct acpi_processor_cx *cx;
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
691 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400692 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693
694 /*
695 * This function sets the default Cx state policy (OS idle handler).
696 * Our scheme is to promote quickly to C2 but more conservatively
697 * to C3. We're favoring C2 for its characteristics of low latency
698 * (quick response), good power savings, and ability to allow bus
699 * mastering activity. Note that the Cx state policy is completely
700 * customizable and can be altered dynamically.
701 */
702
703 /* startup state */
Len Brown4be44fc2005-08-05 00:44:28 -0400704 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 cx = &pr->power.states[i];
706 if (!cx->valid)
707 continue;
708
709 if (!state_is_set)
710 pr->power.state = cx;
711 state_is_set++;
712 break;
Len Brown4be44fc2005-08-05 00:44:28 -0400713 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 if (!state_is_set)
Patrick Mocheld550d982006-06-27 00:41:40 -0400716 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717
718 /* demotion */
Len Brown4be44fc2005-08-05 00:44:28 -0400719 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 cx = &pr->power.states[i];
721 if (!cx->valid)
722 continue;
723
724 if (lower) {
725 cx->demotion.state = lower;
726 cx->demotion.threshold.ticks = cx->latency_ticks;
727 cx->demotion.threshold.count = 1;
728 if (cx->type == ACPI_STATE_C3)
729 cx->demotion.threshold.bm = bm_history;
730 }
731
732 lower = cx;
733 }
734
735 /* promotion */
736 for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
737 cx = &pr->power.states[i];
738 if (!cx->valid)
739 continue;
740
741 if (higher) {
Len Brown4be44fc2005-08-05 00:44:28 -0400742 cx->promotion.state = higher;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 cx->promotion.threshold.ticks = cx->latency_ticks;
744 if (cx->type >= ACPI_STATE_C2)
745 cx->promotion.threshold.count = 4;
746 else
747 cx->promotion.threshold.count = 10;
748 if (higher->type == ACPI_STATE_C3)
749 cx->promotion.threshold.bm = bm_history;
750 }
751
752 higher = cx;
753 }
754
Patrick Mocheld550d982006-06-27 00:41:40 -0400755 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
Len Brown4f86d3a2007-10-03 18:58:00 -0400757#endif /* !CONFIG_CPU_IDLE */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758
Len Brown4be44fc2005-08-05 00:44:28 -0400759static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761
762 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -0400763 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 if (!pr->pblk)
Patrick Mocheld550d982006-06-27 00:41:40 -0400766 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 /* if info is obtained from pblk/fadt, type equals state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
770 pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
771
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400772#ifndef CONFIG_HOTPLUG_CPU
773 /*
774 * Check for P_LVL2_UP flag before entering C2 and above on
Len Brown4f86d3a2007-10-03 18:58:00 -0400775 * an SMP system.
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400776 */
Alexey Starikovskiyad71860a2007-02-02 19:48:19 +0300777 if ((num_online_cpus() > 1) &&
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300778 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
Patrick Mocheld550d982006-06-27 00:41:40 -0400779 return -ENODEV;
Venkatesh Pallipadi4c033552005-09-15 12:20:00 -0400780#endif
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 /* determine C2 and C3 address from pblk */
783 pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
784 pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
785
786 /* determine latencies from FADT */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +0300787 pr->power.states[ACPI_STATE_C2].latency = acpi_gbl_FADT.C2latency;
788 pr->power.states[ACPI_STATE_C3].latency = acpi_gbl_FADT.C3latency;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
791 "lvl2[0x%08x] lvl3[0x%08x]\n",
792 pr->power.states[ACPI_STATE_C2].address,
793 pr->power.states[ACPI_STATE_C3].address));
794
Patrick Mocheld550d982006-06-27 00:41:40 -0400795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796}
797
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700798static int acpi_processor_get_power_info_default(struct acpi_processor *pr)
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500799{
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700800 if (!pr->power.states[ACPI_STATE_C1].valid) {
801 /* set the first C-State to C1 */
802 /* all processors need to support C1 */
803 pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
804 pr->power.states[ACPI_STATE_C1].valid = 1;
805 }
806 /* the C0 state only exists as a filler in our array */
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500807 pr->power.states[ACPI_STATE_C0].valid = 1;
Patrick Mocheld550d982006-06-27 00:41:40 -0400808 return 0;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -0500809}
810
Len Brown4be44fc2005-08-05 00:44:28 -0400811static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
Len Brown4be44fc2005-08-05 00:44:28 -0400813 acpi_status status = 0;
814 acpi_integer count;
Janosch Machowinskicf824782005-08-20 08:02:00 -0400815 int current_count;
Len Brown4be44fc2005-08-05 00:44:28 -0400816 int i;
817 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
818 union acpi_object *cst;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821 if (nocst)
Patrick Mocheld550d982006-06-27 00:41:40 -0400822 return -ENODEV;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700824 current_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
827 if (ACPI_FAILURE(status)) {
828 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -0400829 return -ENODEV;
Len Brown4be44fc2005-08-05 00:44:28 -0400830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200832 cst = buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 /* There must be at least 2 elements */
835 if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
Len Brown64684632006-06-26 23:41:38 -0400836 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837 status = -EFAULT;
838 goto end;
839 }
840
841 count = cst->package.elements[0].integer.value;
842
843 /* Validate number of power states. */
844 if (count < 1 || count != cst->package.count - 1) {
Len Brown64684632006-06-26 23:41:38 -0400845 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700846 status = -EFAULT;
847 goto end;
848 }
849
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 /* Tell driver that at least _CST is supported. */
851 pr->flags.has_cst = 1;
852
853 for (i = 1; i <= count; i++) {
854 union acpi_object *element;
855 union acpi_object *obj;
856 struct acpi_power_register *reg;
857 struct acpi_processor_cx cx;
858
859 memset(&cx, 0, sizeof(cx));
860
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200861 element = &(cst->package.elements[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (element->type != ACPI_TYPE_PACKAGE)
863 continue;
864
865 if (element->package.count != 4)
866 continue;
867
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200868 obj = &(element->package.elements[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 if (obj->type != ACPI_TYPE_BUFFER)
871 continue;
872
Len Brown4be44fc2005-08-05 00:44:28 -0400873 reg = (struct acpi_power_register *)obj->buffer.pointer;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
Len Brown4be44fc2005-08-05 00:44:28 -0400876 (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877 continue;
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 /* There should be an easy way to extract an integer... */
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200880 obj = &(element->package.elements[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 if (obj->type != ACPI_TYPE_INTEGER)
882 continue;
883
884 cx.type = obj->integer.value;
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700885 /*
886 * Some buggy BIOSes won't list C1 in _CST -
887 * Let acpi_processor_get_power_info_default() handle them later
888 */
889 if (i == 1 && cx.type != ACPI_STATE_C1)
890 current_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700891
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700892 cx.address = reg->address;
893 cx.index = current_count + 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700894
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -0700895 cx.space_id = ACPI_CSTATE_SYSTEMIO;
896 if (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) {
897 if (acpi_processor_ffh_cstate_probe
898 (pr->id, &cx, reg) == 0) {
899 cx.space_id = ACPI_CSTATE_FFH;
900 } else if (cx.type != ACPI_STATE_C1) {
901 /*
902 * C1 is a special case where FIXED_HARDWARE
903 * can be handled in non-MWAIT way as well.
904 * In that case, save this _CST entry info.
905 * That is, we retain space_id of SYSTEM_IO for
906 * halt based C1.
907 * Otherwise, ignore this info and continue.
908 */
909 continue;
910 }
911 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200913 obj = &(element->package.elements[2]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914 if (obj->type != ACPI_TYPE_INTEGER)
915 continue;
916
917 cx.latency = obj->integer.value;
918
Jan Engelhardt50dd0962006-10-01 00:28:50 +0200919 obj = &(element->package.elements[3]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 if (obj->type != ACPI_TYPE_INTEGER)
921 continue;
922
923 cx.power = obj->integer.value;
924
Janosch Machowinskicf824782005-08-20 08:02:00 -0400925 current_count++;
926 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
927
928 /*
929 * We support total ACPI_PROCESSOR_MAX_POWER - 1
930 * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
931 */
932 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
933 printk(KERN_WARNING
934 "Limiting number of power states to max (%d)\n",
935 ACPI_PROCESSOR_MAX_POWER);
936 printk(KERN_WARNING
937 "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
938 break;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 }
941
Len Brown4be44fc2005-08-05 00:44:28 -0400942 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
Janosch Machowinskicf824782005-08-20 08:02:00 -0400943 current_count));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944
945 /* Validate number of power states discovered */
Janosch Machowinskicf824782005-08-20 08:02:00 -0400946 if (current_count < 2)
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -0400947 status = -EFAULT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948
Len Brown4be44fc2005-08-05 00:44:28 -0400949 end:
Len Brown02438d82006-06-30 03:19:10 -0400950 kfree(buffer.pointer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951
Patrick Mocheld550d982006-06-27 00:41:40 -0400952 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953}
954
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
956{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
958 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -0400959 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960
961 /*
962 * C2 latency must be less than or equal to 100
963 * microseconds.
964 */
965 else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
966 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -0400967 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -0400968 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700969 }
970
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 /*
972 * Otherwise we've met all of our C2 requirements.
973 * Normalize the C2 latency to expidite policy
974 */
975 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -0400976
977#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -0400979#else
980 cx->latency_ticks = cx->latency;
981#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Patrick Mocheld550d982006-06-27 00:41:40 -0400983 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
Len Brown4be44fc2005-08-05 00:44:28 -0400986static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
987 struct acpi_processor_cx *cx)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988{
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -0400989 static int bm_check_flag;
990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992 if (!cx->address)
Patrick Mocheld550d982006-06-27 00:41:40 -0400993 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
995 /*
996 * C3 latency must be less than or equal to 1000
997 * microseconds.
998 */
999 else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
1000 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001001 "latency too large [%d]\n", cx->latency));
Patrick Mocheld550d982006-06-27 00:41:40 -04001002 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 }
1004
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 /*
1006 * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
1007 * DMA transfers are used by any ISA device to avoid livelock.
1008 * Note that we could disable Type-F DMA (as recommended by
1009 * the erratum), but this is known to disrupt certain ISA
1010 * devices thus we take the conservative approach.
1011 */
1012 else if (errata.piix4.fdma) {
1013 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001014 "C3 not supported on PIIX4 with Type-F DMA\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001015 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016 }
1017
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001018 /* All the logic here assumes flags.bm_check is same across all CPUs */
1019 if (!bm_check_flag) {
1020 /* Determine whether bm_check is needed based on CPU */
1021 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
1022 bm_check_flag = pr->flags.bm_check;
1023 } else {
1024 pr->flags.bm_check = bm_check_flag;
1025 }
1026
1027 if (pr->flags.bm_check) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001028 if (!pr->flags.bm_control) {
Venki Pallipadied3110e2007-07-31 12:04:31 -07001029 if (pr->flags.has_cst != 1) {
1030 /* bus mastering control is necessary */
1031 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1032 "C3 support requires BM control\n"));
1033 return;
1034 } else {
1035 /* Here we enter C3 without bus mastering */
1036 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1037 "C3 support without BM control\n"));
1038 }
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001039 }
1040 } else {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001041 /*
1042 * WBINVD should be set in fadt, for C3 state to be
1043 * supported on when bm_check is not required.
1044 */
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001045 if (!(acpi_gbl_FADT.flags & ACPI_FADT_WBINVD)) {
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001046 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
Len Brown4be44fc2005-08-05 00:44:28 -04001047 "Cache invalidation should work properly"
1048 " for C3 to be enabled on SMP systems\n"));
Patrick Mocheld550d982006-06-27 00:41:40 -04001049 return;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001050 }
Bob Moored8c71b62007-02-02 19:48:21 +03001051 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001052 }
1053
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 /*
1055 * Otherwise we've met all of our C3 requirements.
1056 * Normalize the C3 latency to expidite policy. Enable
1057 * checking of bus mastering status (bm_check) so we can
1058 * use this in our C3 policy
1059 */
1060 cx->valid = 1;
Len Brown4f86d3a2007-10-03 18:58:00 -04001061
1062#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001063 cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
Len Brown4f86d3a2007-10-03 18:58:00 -04001064#else
1065 cx->latency_ticks = cx->latency;
1066#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Patrick Mocheld550d982006-06-27 00:41:40 -04001068 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069}
1070
Linus Torvalds1da177e2005-04-16 15:20:36 -07001071static int acpi_processor_power_verify(struct acpi_processor *pr)
1072{
1073 unsigned int i;
1074 unsigned int working = 0;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001075
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001076 pr->power.timer_broadcast_on_state = INT_MAX;
Venkatesh Pallipadi6eb0a0f2006-01-11 22:44:21 +01001077
Len Brown4be44fc2005-08-05 00:44:28 -04001078 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079 struct acpi_processor_cx *cx = &pr->power.states[i];
1080
1081 switch (cx->type) {
1082 case ACPI_STATE_C1:
1083 cx->valid = 1;
1084 break;
1085
1086 case ACPI_STATE_C2:
1087 acpi_processor_power_verify_c2(cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001088 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001089 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 break;
1091
1092 case ACPI_STATE_C3:
1093 acpi_processor_power_verify_c3(pr, cx);
Linus Torvalds296d93c2007-03-23 08:03:47 -07001094 if (cx->valid)
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001095 acpi_timer_check_state(i, pr, cx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 break;
1097 }
1098
1099 if (cx->valid)
1100 working++;
1101 }
1102
Thomas Gleixner169a0ab2007-02-16 01:27:55 -08001103 acpi_propagate_timer_broadcast(pr);
Andi Kleenbd663342006-03-25 16:31:07 +01001104
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 return (working);
1106}
1107
Len Brown4be44fc2005-08-05 00:44:28 -04001108static int acpi_processor_get_power_info(struct acpi_processor *pr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001109{
1110 unsigned int i;
1111 int result;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
1114 /* NOTE: the idle thread may not be running while calling
1115 * this function */
1116
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001117 /* Zero initialize all the C-states info. */
1118 memset(pr->power.states, 0, sizeof(pr->power.states));
1119
Linus Torvalds1da177e2005-04-16 15:20:36 -07001120 result = acpi_processor_get_power_info_cst(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001121 if (result == -ENODEV)
Darrick J. Wongc5a114f2006-10-19 23:28:28 -07001122 result = acpi_processor_get_power_info_fadt(pr);
Venkatesh Pallipadi6d93c642005-09-15 12:19:00 -04001123
Venkatesh Pallipadi991528d2006-09-25 16:28:13 -07001124 if (result)
1125 return result;
1126
1127 acpi_processor_get_power_info_default(pr);
1128
Janosch Machowinskicf824782005-08-20 08:02:00 -04001129 pr->power.count = acpi_processor_power_verify(pr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130
Len Brown4f86d3a2007-10-03 18:58:00 -04001131#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 /*
1133 * Set Default Policy
1134 * ------------------
1135 * Now that we know which states are supported, set the default
1136 * policy. Note that this policy can be changed dynamically
1137 * (e.g. encourage deeper sleeps to conserve battery life when
1138 * not on AC).
1139 */
1140 result = acpi_processor_set_power_policy(pr);
1141 if (result)
Patrick Mocheld550d982006-06-27 00:41:40 -04001142 return result;
Len Brown4f86d3a2007-10-03 18:58:00 -04001143#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144
1145 /*
1146 * if one state of type C2 or C3 is available, mark this
1147 * CPU as being "idle manageable"
1148 */
1149 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001150 if (pr->power.states[i].valid) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 pr->power.count = i;
Linus Torvalds2203d6e2005-11-18 07:29:51 -08001152 if (pr->power.states[i].type >= ACPI_STATE_C2)
1153 pr->flags.power = 1;
Venkatesh Pallipadiacf05f42005-03-31 23:23:15 -05001154 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
1156
Patrick Mocheld550d982006-06-27 00:41:40 -04001157 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158}
1159
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1161{
Jan Engelhardt50dd0962006-10-01 00:28:50 +02001162 struct acpi_processor *pr = seq->private;
Len Brown4be44fc2005-08-05 00:44:28 -04001163 unsigned int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
1166 if (!pr)
1167 goto end;
1168
1169 seq_printf(seq, "active state: C%zd\n"
Len Brown4be44fc2005-08-05 00:44:28 -04001170 "max_cstate: C%d\n"
Arjan van de Ven5c875792006-09-30 23:27:17 -07001171 "bus master activity: %08x\n"
1172 "maximum allowed latency: %d usec\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001173 pr->power.state ? pr->power.state - pr->power.states : 0,
Arjan van de Ven5c875792006-09-30 23:27:17 -07001174 max_cstate, (unsigned)pr->power.bm_activity,
1175 system_latency_constraint());
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177 seq_puts(seq, "states:\n");
1178
1179 for (i = 1; i <= pr->power.count; i++) {
1180 seq_printf(seq, " %cC%d: ",
Len Brown4be44fc2005-08-05 00:44:28 -04001181 (&pr->power.states[i] ==
1182 pr->power.state ? '*' : ' '), i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
1184 if (!pr->power.states[i].valid) {
1185 seq_puts(seq, "<not supported>\n");
1186 continue;
1187 }
1188
1189 switch (pr->power.states[i].type) {
1190 case ACPI_STATE_C1:
1191 seq_printf(seq, "type[C1] ");
1192 break;
1193 case ACPI_STATE_C2:
1194 seq_printf(seq, "type[C2] ");
1195 break;
1196 case ACPI_STATE_C3:
1197 seq_printf(seq, "type[C3] ");
1198 break;
1199 default:
1200 seq_printf(seq, "type[--] ");
1201 break;
1202 }
1203
1204 if (pr->power.states[i].promotion.state)
1205 seq_printf(seq, "promotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001206 (pr->power.states[i].promotion.state -
1207 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 else
1209 seq_puts(seq, "promotion[--] ");
1210
1211 if (pr->power.states[i].demotion.state)
1212 seq_printf(seq, "demotion[C%zd] ",
Len Brown4be44fc2005-08-05 00:44:28 -04001213 (pr->power.states[i].demotion.state -
1214 pr->power.states));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 else
1216 seq_puts(seq, "demotion[--] ");
1217
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001218 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
Len Brown4be44fc2005-08-05 00:44:28 -04001219 pr->power.states[i].latency,
Dominik Brodowskia3c65982006-06-24 19:37:00 -04001220 pr->power.states[i].usage,
Alexey Starikovskiyb0b7eaa2007-01-25 22:39:44 -05001221 (unsigned long long)pr->power.states[i].time);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 }
1223
Len Brown4be44fc2005-08-05 00:44:28 -04001224 end:
Patrick Mocheld550d982006-06-27 00:41:40 -04001225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001226}
1227
1228static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1229{
1230 return single_open(file, acpi_processor_power_seq_show,
Len Brown4be44fc2005-08-05 00:44:28 -04001231 PDE(inode)->data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232}
1233
Arjan van de Vend7508032006-07-04 13:06:00 -04001234static const struct file_operations acpi_processor_power_fops = {
Len Brown4be44fc2005-08-05 00:44:28 -04001235 .open = acpi_processor_power_open_fs,
1236 .read = seq_read,
1237 .llseek = seq_lseek,
1238 .release = single_release,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239};
1240
Len Brown4f86d3a2007-10-03 18:58:00 -04001241#ifndef CONFIG_CPU_IDLE
1242
1243int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1244{
1245 int result = 0;
1246
1247
1248 if (!pr)
1249 return -EINVAL;
1250
1251 if (nocst) {
1252 return -ENODEV;
1253 }
1254
1255 if (!pr->flags.power_setup_done)
1256 return -ENODEV;
1257
1258 /* Fall back to the default idle loop */
1259 pm_idle = pm_idle_save;
1260 synchronize_sched(); /* Relies on interrupts forcing exit from idle. */
1261
1262 pr->flags.power = 0;
1263 result = acpi_processor_get_power_info(pr);
1264 if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
1265 pm_idle = acpi_processor_idle;
1266
1267 return result;
1268}
1269
Andrew Morton1fec74a2006-10-17 00:09:58 -07001270#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001271static void smp_callback(void *v)
1272{
1273 /* we already woke the CPU up, nothing more to do */
1274}
1275
1276/*
1277 * This function gets called when a part of the kernel has a new latency
1278 * requirement. This means we need to get all processors out of their C-state,
1279 * and then recalculate a new suitable C-state. Just do a cross-cpu IPI; that
1280 * wakes them all right up.
1281 */
1282static int acpi_processor_latency_notify(struct notifier_block *b,
1283 unsigned long l, void *v)
1284{
1285 smp_call_function(smp_callback, NULL, 0, 1);
1286 return NOTIFY_OK;
1287}
1288
1289static struct notifier_block acpi_processor_latency_notifier = {
1290 .notifier_call = acpi_processor_latency_notify,
1291};
Len Brown4f86d3a2007-10-03 18:58:00 -04001292
Andrew Morton1fec74a2006-10-17 00:09:58 -07001293#endif
Arjan van de Ven5c875792006-09-30 23:27:17 -07001294
Len Brown4f86d3a2007-10-03 18:58:00 -04001295#else /* CONFIG_CPU_IDLE */
1296
1297/**
1298 * acpi_idle_bm_check - checks if bus master activity was detected
1299 */
1300static int acpi_idle_bm_check(void)
1301{
1302 u32 bm_status = 0;
1303
1304 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, &bm_status);
1305 if (bm_status)
1306 acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS, 1);
1307 /*
1308 * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
1309 * the true state of bus mastering activity; forcing us to
1310 * manually check the BMIDEA bit of each IDE channel.
1311 */
1312 else if (errata.piix4.bmisx) {
1313 if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
1314 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
1315 bm_status = 1;
1316 }
1317 return bm_status;
1318}
1319
1320/**
1321 * acpi_idle_update_bm_rld - updates the BM_RLD bit depending on target state
1322 * @pr: the processor
1323 * @target: the new target state
1324 */
1325static inline void acpi_idle_update_bm_rld(struct acpi_processor *pr,
1326 struct acpi_processor_cx *target)
1327{
1328 if (pr->flags.bm_rld_set && target->type != ACPI_STATE_C3) {
1329 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0);
1330 pr->flags.bm_rld_set = 0;
1331 }
1332
1333 if (!pr->flags.bm_rld_set && target->type == ACPI_STATE_C3) {
1334 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1);
1335 pr->flags.bm_rld_set = 1;
1336 }
1337}
1338
1339/**
1340 * acpi_idle_do_entry - a helper function that does C2 and C3 type entry
1341 * @cx: cstate data
1342 */
1343static inline void acpi_idle_do_entry(struct acpi_processor_cx *cx)
1344{
1345 if (cx->space_id == ACPI_CSTATE_FFH) {
1346 /* Call into architectural FFH based C-state */
1347 acpi_processor_ffh_cstate_enter(cx);
1348 } else {
1349 int unused;
1350 /* IO port based C-state */
1351 inb(cx->address);
1352 /* Dummy wait op - must do something useless after P_LVL2 read
1353 because chipsets cannot guarantee that STPCLK# signal
1354 gets asserted in time to freeze execution properly. */
1355 unused = inl(acpi_gbl_FADT.xpm_timer_block.address);
1356 }
1357}
1358
1359/**
1360 * acpi_idle_enter_c1 - enters an ACPI C1 state-type
1361 * @dev: the target CPU
1362 * @state: the state data
1363 *
1364 * This is equivalent to the HALT instruction.
1365 */
1366static int acpi_idle_enter_c1(struct cpuidle_device *dev,
1367 struct cpuidle_state *state)
1368{
1369 struct acpi_processor *pr;
1370 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1371 pr = processors[smp_processor_id()];
1372
1373 if (unlikely(!pr))
1374 return 0;
1375
1376 if (pr->flags.bm_check)
1377 acpi_idle_update_bm_rld(pr, cx);
1378
1379 current_thread_info()->status &= ~TS_POLLING;
1380 /*
1381 * TS_POLLING-cleared state must be visible before we test
1382 * NEED_RESCHED:
1383 */
1384 smp_mb();
1385 if (!need_resched())
1386 safe_halt();
1387 current_thread_info()->status |= TS_POLLING;
1388
1389 cx->usage++;
1390
1391 return 0;
1392}
1393
1394/**
1395 * acpi_idle_enter_simple - enters an ACPI state without BM handling
1396 * @dev: the target CPU
1397 * @state: the state data
1398 */
1399static int acpi_idle_enter_simple(struct cpuidle_device *dev,
1400 struct cpuidle_state *state)
1401{
1402 struct acpi_processor *pr;
1403 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1404 u32 t1, t2;
1405 pr = processors[smp_processor_id()];
1406
1407 if (unlikely(!pr))
1408 return 0;
1409
Len Browne1964412007-10-04 01:23:47 -04001410 if (acpi_idle_suspend)
1411 return(acpi_idle_enter_c1(dev, state));
1412
Len Brown4f86d3a2007-10-03 18:58:00 -04001413 if (pr->flags.bm_check)
1414 acpi_idle_update_bm_rld(pr, cx);
1415
1416 local_irq_disable();
1417 current_thread_info()->status &= ~TS_POLLING;
1418 /*
1419 * TS_POLLING-cleared state must be visible before we test
1420 * NEED_RESCHED:
1421 */
1422 smp_mb();
1423
1424 if (unlikely(need_resched())) {
1425 current_thread_info()->status |= TS_POLLING;
1426 local_irq_enable();
1427 return 0;
1428 }
1429
1430 if (cx->type == ACPI_STATE_C3)
1431 ACPI_FLUSH_CPU_CACHE();
1432
1433 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1434 acpi_state_timer_broadcast(pr, cx, 1);
1435 acpi_idle_do_entry(cx);
1436 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1437
1438#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
1439 /* TSC could halt in idle, so notify users */
1440 mark_tsc_unstable("TSC halts in idle");;
1441#endif
1442
1443 local_irq_enable();
1444 current_thread_info()->status |= TS_POLLING;
1445
1446 cx->usage++;
1447
1448 acpi_state_timer_broadcast(pr, cx, 0);
1449 cx->time += ticks_elapsed(t1, t2);
1450 return ticks_elapsed_in_us(t1, t2);
1451}
1452
1453static int c3_cpu_count;
1454static DEFINE_SPINLOCK(c3_lock);
1455
1456/**
1457 * acpi_idle_enter_bm - enters C3 with proper BM handling
1458 * @dev: the target CPU
1459 * @state: the state data
1460 *
1461 * If BM is detected, the deepest non-C3 idle state is entered instead.
1462 */
1463static int acpi_idle_enter_bm(struct cpuidle_device *dev,
1464 struct cpuidle_state *state)
1465{
1466 struct acpi_processor *pr;
1467 struct acpi_processor_cx *cx = cpuidle_get_statedata(state);
1468 u32 t1, t2;
1469 pr = processors[smp_processor_id()];
1470
1471 if (unlikely(!pr))
1472 return 0;
1473
Len Browne1964412007-10-04 01:23:47 -04001474 if (acpi_idle_suspend)
1475 return(acpi_idle_enter_c1(dev, state));
1476
Len Brown4f86d3a2007-10-03 18:58:00 -04001477 local_irq_disable();
1478 current_thread_info()->status &= ~TS_POLLING;
1479 /*
1480 * TS_POLLING-cleared state must be visible before we test
1481 * NEED_RESCHED:
1482 */
1483 smp_mb();
1484
1485 if (unlikely(need_resched())) {
1486 current_thread_info()->status |= TS_POLLING;
1487 local_irq_enable();
1488 return 0;
1489 }
1490
1491 /*
1492 * Must be done before busmaster disable as we might need to
1493 * access HPET !
1494 */
1495 acpi_state_timer_broadcast(pr, cx, 1);
1496
1497 if (acpi_idle_bm_check()) {
1498 cx = pr->power.bm_state;
1499
1500 acpi_idle_update_bm_rld(pr, cx);
1501
1502 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1503 acpi_idle_do_entry(cx);
1504 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1505 } else {
1506 acpi_idle_update_bm_rld(pr, cx);
1507
1508 spin_lock(&c3_lock);
1509 c3_cpu_count++;
1510 /* Disable bus master arbitration when all CPUs are in C3 */
1511 if (c3_cpu_count == num_online_cpus())
1512 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1);
1513 spin_unlock(&c3_lock);
1514
1515 t1 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1516 acpi_idle_do_entry(cx);
1517 t2 = inl(acpi_gbl_FADT.xpm_timer_block.address);
1518
1519 spin_lock(&c3_lock);
1520 /* Re-enable bus master arbitration */
1521 if (c3_cpu_count == num_online_cpus())
1522 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0);
1523 c3_cpu_count--;
1524 spin_unlock(&c3_lock);
1525 }
1526
1527#if defined (CONFIG_GENERIC_TIME) && defined (CONFIG_X86_TSC)
1528 /* TSC could halt in idle, so notify users */
1529 mark_tsc_unstable("TSC halts in idle");
1530#endif
1531
1532 local_irq_enable();
1533 current_thread_info()->status |= TS_POLLING;
1534
1535 cx->usage++;
1536
1537 acpi_state_timer_broadcast(pr, cx, 0);
1538 cx->time += ticks_elapsed(t1, t2);
1539 return ticks_elapsed_in_us(t1, t2);
1540}
1541
1542struct cpuidle_driver acpi_idle_driver = {
1543 .name = "acpi_idle",
1544 .owner = THIS_MODULE,
1545};
1546
1547/**
1548 * acpi_processor_setup_cpuidle - prepares and configures CPUIDLE
1549 * @pr: the ACPI processor
1550 */
1551static int acpi_processor_setup_cpuidle(struct acpi_processor *pr)
1552{
1553 int i, count = 0;
1554 struct acpi_processor_cx *cx;
1555 struct cpuidle_state *state;
1556 struct cpuidle_device *dev = &pr->power.dev;
1557
1558 if (!pr->flags.power_setup_done)
1559 return -EINVAL;
1560
1561 if (pr->flags.power == 0) {
1562 return -EINVAL;
1563 }
1564
1565 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++) {
1566 cx = &pr->power.states[i];
1567 state = &dev->states[count];
1568
1569 if (!cx->valid)
1570 continue;
1571
1572#ifdef CONFIG_HOTPLUG_CPU
1573 if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) &&
1574 !pr->flags.has_cst &&
1575 !(acpi_gbl_FADT.flags & ACPI_FADT_C2_MP_SUPPORTED))
1576 continue;
1577#endif
1578 cpuidle_set_statedata(state, cx);
1579
1580 snprintf(state->name, CPUIDLE_NAME_LEN, "C%d", i);
1581 state->exit_latency = cx->latency;
Len Brown4963f622007-12-13 23:50:45 -05001582 state->target_residency = cx->latency * latency_factor;
Len Brown4f86d3a2007-10-03 18:58:00 -04001583 state->power_usage = cx->power;
1584
1585 state->flags = 0;
1586 switch (cx->type) {
1587 case ACPI_STATE_C1:
1588 state->flags |= CPUIDLE_FLAG_SHALLOW;
1589 state->enter = acpi_idle_enter_c1;
1590 break;
1591
1592 case ACPI_STATE_C2:
1593 state->flags |= CPUIDLE_FLAG_BALANCED;
1594 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1595 state->enter = acpi_idle_enter_simple;
1596 break;
1597
1598 case ACPI_STATE_C3:
1599 state->flags |= CPUIDLE_FLAG_DEEP;
1600 state->flags |= CPUIDLE_FLAG_TIME_VALID;
1601 state->flags |= CPUIDLE_FLAG_CHECK_BM;
1602 state->enter = pr->flags.bm_check ?
1603 acpi_idle_enter_bm :
1604 acpi_idle_enter_simple;
1605 break;
1606 }
1607
1608 count++;
1609 }
1610
1611 dev->state_count = count;
1612
1613 if (!count)
1614 return -EINVAL;
1615
1616 /* find the deepest state that can handle active BM */
1617 if (pr->flags.bm_check) {
1618 for (i = 1; i < ACPI_PROCESSOR_MAX_POWER && i <= max_cstate; i++)
1619 if (pr->power.states[i].type == ACPI_STATE_C3)
1620 break;
1621 pr->power.bm_state = &pr->power.states[i-1];
1622 }
1623
1624 return 0;
1625}
1626
1627int acpi_processor_cst_has_changed(struct acpi_processor *pr)
1628{
1629 int ret;
1630
1631 if (!pr)
1632 return -EINVAL;
1633
1634 if (nocst) {
1635 return -ENODEV;
1636 }
1637
1638 if (!pr->flags.power_setup_done)
1639 return -ENODEV;
1640
1641 cpuidle_pause_and_lock();
1642 cpuidle_disable_device(&pr->power.dev);
1643 acpi_processor_get_power_info(pr);
1644 acpi_processor_setup_cpuidle(pr);
1645 ret = cpuidle_enable_device(&pr->power.dev);
1646 cpuidle_resume_and_unlock();
1647
1648 return ret;
1649}
1650
1651#endif /* CONFIG_CPU_IDLE */
1652
Pierre Ossman7af8b662006-10-10 14:20:31 -07001653int __cpuinit acpi_processor_power_init(struct acpi_processor *pr,
Len Brown4be44fc2005-08-05 00:44:28 -04001654 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655{
Len Brown4be44fc2005-08-05 00:44:28 -04001656 acpi_status status = 0;
Andreas Mohrb6835052006-04-27 05:25:00 -04001657 static int first_run;
Len Brown4be44fc2005-08-05 00:44:28 -04001658 struct proc_dir_entry *entry = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 unsigned int i;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
1662 if (!first_run) {
1663 dmi_check_system(processor_power_dmi_table);
1664 if (max_cstate < ACPI_C_STATES_MAX)
Len Brown4be44fc2005-08-05 00:44:28 -04001665 printk(KERN_NOTICE
1666 "ACPI: processor limited to max C-state %d\n",
1667 max_cstate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668 first_run++;
Len Brown4f86d3a2007-10-03 18:58:00 -04001669#if !defined (CONFIG_CPU_IDLE) && defined (CONFIG_SMP)
Arjan van de Ven5c875792006-09-30 23:27:17 -07001670 register_latency_notifier(&acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001671#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 }
1673
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001674 if (!pr)
Patrick Mocheld550d982006-06-27 00:41:40 -04001675 return -EINVAL;
Venkatesh Pallipadi02df8b92005-04-15 15:07:10 -04001676
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001677 if (acpi_gbl_FADT.cst_control && !nocst) {
Len Brown4be44fc2005-08-05 00:44:28 -04001678 status =
Alexey Starikovskiycee324b2007-02-02 19:48:22 +03001679 acpi_os_write_port(acpi_gbl_FADT.smi_command, acpi_gbl_FADT.cst_control, 8);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 if (ACPI_FAILURE(status)) {
Thomas Renningera6fc6722006-06-26 23:58:43 -04001681 ACPI_EXCEPTION((AE_INFO, status,
1682 "Notifying BIOS of _CST ability failed"));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
1684 }
1685
Linus Torvalds1da177e2005-04-16 15:20:36 -07001686 acpi_processor_get_power_info(pr);
Len Brown4f86d3a2007-10-03 18:58:00 -04001687 pr->flags.power_setup_done = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
1689 /*
1690 * Install the idle handler if processor power management is supported.
1691 * Note that we use previously set idle handler will be used on
1692 * platforms that only support C1.
1693 */
1694 if ((pr->flags.power) && (!boot_option_idle_override)) {
Len Brown4f86d3a2007-10-03 18:58:00 -04001695#ifdef CONFIG_CPU_IDLE
1696 acpi_processor_setup_cpuidle(pr);
1697 pr->power.dev.cpu = pr->id;
1698 if (cpuidle_register_device(&pr->power.dev))
1699 return -EIO;
1700#endif
1701
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1703 for (i = 1; i <= pr->power.count; i++)
1704 if (pr->power.states[i].valid)
Len Brown4be44fc2005-08-05 00:44:28 -04001705 printk(" C%d[C%d]", i,
1706 pr->power.states[i].type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 printk(")\n");
1708
Len Brown4f86d3a2007-10-03 18:58:00 -04001709#ifndef CONFIG_CPU_IDLE
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 if (pr->id == 0) {
1711 pm_idle_save = pm_idle;
1712 pm_idle = acpi_processor_idle;
1713 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001714#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 }
1716
1717 /* 'power' [R] */
1718 entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
Len Brown4be44fc2005-08-05 00:44:28 -04001719 S_IRUGO, acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 if (!entry)
Thomas Renningera6fc6722006-06-26 23:58:43 -04001721 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001722 else {
1723 entry->proc_fops = &acpi_processor_power_fops;
1724 entry->data = acpi_driver_data(device);
1725 entry->owner = THIS_MODULE;
1726 }
1727
Patrick Mocheld550d982006-06-27 00:41:40 -04001728 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729}
1730
Len Brown4be44fc2005-08-05 00:44:28 -04001731int acpi_processor_power_exit(struct acpi_processor *pr,
1732 struct acpi_device *device)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733{
Len Brown4f86d3a2007-10-03 18:58:00 -04001734#ifdef CONFIG_CPU_IDLE
1735 if ((pr->flags.power) && (!boot_option_idle_override))
1736 cpuidle_unregister_device(&pr->power.dev);
1737#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738 pr->flags.power_setup_done = 0;
1739
1740 if (acpi_device_dir(device))
Len Brown4be44fc2005-08-05 00:44:28 -04001741 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1742 acpi_device_dir(device));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743
Len Brown4f86d3a2007-10-03 18:58:00 -04001744#ifndef CONFIG_CPU_IDLE
1745
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746 /* Unregister the idle handler when processor #0 is removed. */
1747 if (pr->id == 0) {
1748 pm_idle = pm_idle_save;
1749
1750 /*
1751 * We are about to unload the current idle thread pm callback
1752 * (pm_idle), Wait for all processors to update cached/local
1753 * copies of pm_idle before proceeding.
1754 */
1755 cpu_idle_wait();
Andrew Morton1fec74a2006-10-17 00:09:58 -07001756#ifdef CONFIG_SMP
Arjan van de Ven5c875792006-09-30 23:27:17 -07001757 unregister_latency_notifier(&acpi_processor_latency_notifier);
Andrew Morton1fec74a2006-10-17 00:09:58 -07001758#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 }
Len Brown4f86d3a2007-10-03 18:58:00 -04001760#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Patrick Mocheld550d982006-06-27 00:41:40 -04001762 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763}