ACPI: C-States: bm_activity improvements
[linux-2.6] / drivers / acpi / processor_idle.c
1 /*
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>
6  *  Copyright (C) 2004, 2005 Dominik Brodowski <linux@brodo.de>
7  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8  *                      - Added processor hotplug support
9  *  Copyright (C) 2005  Venkatesh Pallipadi <venkatesh.pallipadi@intel.com>
10  *                      - Added support for C3 on SMP
11  *
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>
40 #include <linux/sched.h>        /* need_resched() */
41
42 #include <asm/io.h>
43 #include <asm/uaccess.h>
44
45 #include <acpi/acpi_bus.h>
46 #include <acpi/processor.h>
47
48 #define ACPI_PROCESSOR_COMPONENT        0x01000000
49 #define ACPI_PROCESSOR_CLASS            "processor"
50 #define ACPI_PROCESSOR_DRIVER_NAME      "ACPI Processor Driver"
51 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
52 ACPI_MODULE_NAME("acpi_processor")
53 #define ACPI_PROCESSOR_FILE_POWER       "power"
54 #define US_TO_PM_TIMER_TICKS(t)         ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
55 #define C2_OVERHEAD                     4       /* 1us (3.579 ticks per us) */
56 #define C3_OVERHEAD                     4       /* 1us (3.579 ticks per us) */
57 static void (*pm_idle_save) (void) __read_mostly;
58 module_param(max_cstate, uint, 0644);
59
60 static unsigned int nocst __read_mostly;
61 module_param(nocst, uint, 0000);
62
63 /*
64  * bm_history -- bit-mask with a bit per jiffy of bus-master activity
65  * 1000 HZ: 0xFFFFFFFF: 32 jiffies = 32ms
66  * 800 HZ: 0xFFFFFFFF: 32 jiffies = 40ms
67  * 100 HZ: 0x0000000F: 4 jiffies = 40ms
68  * reduce history for more aggressive entry into C3
69  */
70 static unsigned int bm_history __read_mostly =
71     (HZ >= 800 ? 0xFFFFFFFF : ((1U << (HZ / 25)) - 1));
72 module_param(bm_history, uint, 0644);
73 /* --------------------------------------------------------------------------
74                                 Power Management
75    -------------------------------------------------------------------------- */
76
77 /*
78  * IBM ThinkPad R40e crashes mysteriously when going into C2 or C3.
79  * For now disable this. Probably a bug somewhere else.
80  *
81  * To skip this limit, boot/load with a large max_cstate limit.
82  */
83 static int set_max_cstate(struct dmi_system_id *id)
84 {
85         if (max_cstate > ACPI_PROCESSOR_MAX_POWER)
86                 return 0;
87
88         printk(KERN_NOTICE PREFIX "%s detected - limiting to C%ld max_cstate."
89                " Override with \"processor.max_cstate=%d\"\n", id->ident,
90                (long)id->driver_data, ACPI_PROCESSOR_MAX_POWER + 1);
91
92         max_cstate = (long)id->driver_data;
93
94         return 0;
95 }
96
97 /* Actually this shouldn't be __cpuinitdata, would be better to fix the
98    callers to only run once -AK */
99 static struct dmi_system_id __cpuinitdata processor_power_dmi_table[] = {
100         { set_max_cstate, "IBM ThinkPad R40e", {
101           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
102           DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW")}, (void *)1},
103         { set_max_cstate, "IBM ThinkPad R40e", {
104           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
105           DMI_MATCH(DMI_BIOS_VERSION,"1SET43WW") }, (void*)1},
106         { set_max_cstate, "IBM ThinkPad R40e", {
107           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
108           DMI_MATCH(DMI_BIOS_VERSION,"1SET45WW") }, (void*)1},
109         { set_max_cstate, "IBM ThinkPad R40e", {
110           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
111           DMI_MATCH(DMI_BIOS_VERSION,"1SET47WW") }, (void*)1},
112         { set_max_cstate, "IBM ThinkPad R40e", {
113           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
114           DMI_MATCH(DMI_BIOS_VERSION,"1SET50WW") }, (void*)1},
115         { set_max_cstate, "IBM ThinkPad R40e", {
116           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
117           DMI_MATCH(DMI_BIOS_VERSION,"1SET52WW") }, (void*)1},
118         { set_max_cstate, "IBM ThinkPad R40e", {
119           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
120           DMI_MATCH(DMI_BIOS_VERSION,"1SET55WW") }, (void*)1},
121         { set_max_cstate, "IBM ThinkPad R40e", {
122           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
123           DMI_MATCH(DMI_BIOS_VERSION,"1SET56WW") }, (void*)1},
124         { set_max_cstate, "IBM ThinkPad R40e", {
125           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
126           DMI_MATCH(DMI_BIOS_VERSION,"1SET59WW") }, (void*)1},
127         { set_max_cstate, "IBM ThinkPad R40e", {
128           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
129           DMI_MATCH(DMI_BIOS_VERSION,"1SET60WW") }, (void*)1},
130         { set_max_cstate, "IBM ThinkPad R40e", {
131           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
132           DMI_MATCH(DMI_BIOS_VERSION,"1SET61WW") }, (void*)1},
133         { set_max_cstate, "IBM ThinkPad R40e", {
134           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
135           DMI_MATCH(DMI_BIOS_VERSION,"1SET62WW") }, (void*)1},
136         { set_max_cstate, "IBM ThinkPad R40e", {
137           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
138           DMI_MATCH(DMI_BIOS_VERSION,"1SET64WW") }, (void*)1},
139         { set_max_cstate, "IBM ThinkPad R40e", {
140           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
141           DMI_MATCH(DMI_BIOS_VERSION,"1SET65WW") }, (void*)1},
142         { set_max_cstate, "IBM ThinkPad R40e", {
143           DMI_MATCH(DMI_BIOS_VENDOR,"IBM"),
144           DMI_MATCH(DMI_BIOS_VERSION,"1SET68WW") }, (void*)1},
145         { set_max_cstate, "Medion 41700", {
146           DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
147           DMI_MATCH(DMI_BIOS_VERSION,"R01-A1J")}, (void *)1},
148         { set_max_cstate, "Clevo 5600D", {
149           DMI_MATCH(DMI_BIOS_VENDOR,"Phoenix Technologies LTD"),
150           DMI_MATCH(DMI_BIOS_VERSION,"SHE845M0.86C.0013.D.0302131307")},
151          (void *)2},
152         {},
153 };
154
155 static inline u32 ticks_elapsed(u32 t1, u32 t2)
156 {
157         if (t2 >= t1)
158                 return (t2 - t1);
159         else if (!acpi_fadt.tmr_val_ext)
160                 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
161         else
162                 return ((0xFFFFFFFF - t1) + t2);
163 }
164
165 static void
166 acpi_processor_power_activate(struct acpi_processor *pr,
167                               struct acpi_processor_cx *new)
168 {
169         struct acpi_processor_cx *old;
170
171         if (!pr || !new)
172                 return;
173
174         old = pr->power.state;
175
176         if (old)
177                 old->promotion.count = 0;
178         new->demotion.count = 0;
179
180         /* Cleanup from old state. */
181         if (old) {
182                 switch (old->type) {
183                 case ACPI_STATE_C3:
184                         /* Disable bus master reload */
185                         if (new->type != ACPI_STATE_C3 && pr->flags.bm_check)
186                                 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0,
187                                                   ACPI_MTX_DO_NOT_LOCK);
188                         break;
189                 }
190         }
191
192         /* Prepare to use new state. */
193         switch (new->type) {
194         case ACPI_STATE_C3:
195                 /* Enable bus master reload */
196                 if (old->type != ACPI_STATE_C3 && pr->flags.bm_check)
197                         acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1,
198                                           ACPI_MTX_DO_NOT_LOCK);
199                 break;
200         }
201
202         pr->power.state = new;
203
204         return;
205 }
206
207 static void acpi_safe_halt(void)
208 {
209         current_thread_info()->status &= ~TS_POLLING;
210         smp_mb__after_clear_bit();
211         if (!need_resched())
212                 safe_halt();
213         current_thread_info()->status |= TS_POLLING;
214 }
215
216 static atomic_t c3_cpu_count;
217
218 static void acpi_processor_idle(void)
219 {
220         struct acpi_processor *pr = NULL;
221         struct acpi_processor_cx *cx = NULL;
222         struct acpi_processor_cx *next_state = NULL;
223         int sleep_ticks = 0;
224         u32 t1, t2 = 0;
225
226         pr = processors[smp_processor_id()];
227         if (!pr)
228                 return;
229
230         /*
231          * Interrupts must be disabled during bus mastering calculations and
232          * for C2/C3 transitions.
233          */
234         local_irq_disable();
235
236         /*
237          * Check whether we truly need to go idle, or should
238          * reschedule:
239          */
240         if (unlikely(need_resched())) {
241                 local_irq_enable();
242                 return;
243         }
244
245         cx = pr->power.state;
246         if (!cx) {
247                 if (pm_idle_save)
248                         pm_idle_save();
249                 else
250                         acpi_safe_halt();
251                 return;
252         }
253
254         /*
255          * Check BM Activity
256          * -----------------
257          * Check for bus mastering activity (if required), record, and check
258          * for demotion.
259          */
260         if (pr->flags.bm_check) {
261                 u32 bm_status = 0;
262                 unsigned long diff = jiffies - pr->power.bm_check_timestamp;
263
264                 if (diff > 31)
265                         diff = 31;
266
267                 pr->power.bm_activity <<= diff;
268
269                 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS,
270                                   &bm_status, ACPI_MTX_DO_NOT_LOCK);
271                 if (bm_status) {
272                         pr->power.bm_activity |= 0x1;
273                         acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS,
274                                           1, ACPI_MTX_DO_NOT_LOCK);
275                 }
276                 /*
277                  * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
278                  * the true state of bus mastering activity; forcing us to
279                  * manually check the BMIDEA bit of each IDE channel.
280                  */
281                 else if (errata.piix4.bmisx) {
282                         if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01)
283                             || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
284                                 pr->power.bm_activity |= 0x1;
285                 }
286
287                 pr->power.bm_check_timestamp = jiffies;
288
289                 /*
290                  * Apply bus mastering demotion policy.  Automatically demote
291                  * to avoid a faulty transition.  Note that the processor
292                  * won't enter a low-power state during this call (to this
293                  * funciton) but should upon the next.
294                  *
295                  * TBD: A better policy might be to fallback to the demotion
296                  *      state (use it for this quantum only) istead of
297                  *      demoting -- and rely on duration as our sole demotion
298                  *      qualification.  This may, however, introduce DMA
299                  *      issues (e.g. floppy DMA transfer overrun/underrun).
300                  */
301                 if (pr->power.bm_activity & cx->demotion.threshold.bm) {
302                         local_irq_enable();
303                         next_state = cx->demotion.state;
304                         goto end;
305                 }
306         }
307
308 #ifdef CONFIG_HOTPLUG_CPU
309         /*
310          * Check for P_LVL2_UP flag before entering C2 and above on
311          * an SMP system. We do it here instead of doing it at _CST/P_LVL
312          * detection phase, to work cleanly with logical CPU hotplug.
313          */
314         if ((cx->type != ACPI_STATE_C1) && (num_online_cpus() > 1) && 
315             !pr->flags.has_cst && !acpi_fadt.plvl2_up)
316                 cx = &pr->power.states[ACPI_STATE_C1];
317 #endif
318
319         /*
320          * Sleep:
321          * ------
322          * Invoke the current Cx state to put the processor to sleep.
323          */
324         if (cx->type == ACPI_STATE_C2 || cx->type == ACPI_STATE_C3) {
325                 current_thread_info()->status &= ~TS_POLLING;
326                 smp_mb__after_clear_bit();
327                 if (need_resched()) {
328                         current_thread_info()->status |= TS_POLLING;
329                         local_irq_enable();
330                         return;
331                 }
332         }
333
334         switch (cx->type) {
335
336         case ACPI_STATE_C1:
337                 /*
338                  * Invoke C1.
339                  * Use the appropriate idle routine, the one that would
340                  * be used without acpi C-states.
341                  */
342                 if (pm_idle_save)
343                         pm_idle_save();
344                 else
345                         acpi_safe_halt();
346
347                 /*
348                  * TBD: Can't get time duration while in C1, as resumes
349                  *      go to an ISR rather than here.  Need to instrument
350                  *      base interrupt handler.
351                  */
352                 sleep_ticks = 0xFFFFFFFF;
353                 break;
354
355         case ACPI_STATE_C2:
356                 /* Get start time (ticks) */
357                 t1 = inl(acpi_fadt.xpm_tmr_blk.address);
358                 /* Invoke C2 */
359                 inb(cx->address);
360                 /* Dummy op - must do something useless after P_LVL2 read */
361                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
362                 /* Get end time (ticks) */
363                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
364
365 #ifdef CONFIG_GENERIC_TIME
366                 /* TSC halts in C2, so notify users */
367                 mark_tsc_unstable();
368 #endif
369                 /* Re-enable interrupts */
370                 local_irq_enable();
371                 current_thread_info()->status |= TS_POLLING;
372                 /* Compute time (ticks) that we were actually asleep */
373                 sleep_ticks =
374                     ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
375                 break;
376
377         case ACPI_STATE_C3:
378
379                 if (pr->flags.bm_check) {
380                         if (atomic_inc_return(&c3_cpu_count) ==
381                             num_online_cpus()) {
382                                 /*
383                                  * All CPUs are trying to go to C3
384                                  * Disable bus master arbitration
385                                  */
386                                 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1,
387                                                   ACPI_MTX_DO_NOT_LOCK);
388                         }
389                 } else {
390                         /* SMP with no shared cache... Invalidate cache  */
391                         ACPI_FLUSH_CPU_CACHE();
392                 }
393
394                 /* Get start time (ticks) */
395                 t1 = inl(acpi_fadt.xpm_tmr_blk.address);
396                 /* Invoke C3 */
397                 inb(cx->address);
398                 /* Dummy op - must do something useless after P_LVL3 read */
399                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
400                 /* Get end time (ticks) */
401                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
402                 if (pr->flags.bm_check) {
403                         /* Enable bus master arbitration */
404                         atomic_dec(&c3_cpu_count);
405                         acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0,
406                                           ACPI_MTX_DO_NOT_LOCK);
407                 }
408
409 #ifdef CONFIG_GENERIC_TIME
410                 /* TSC halts in C3, so notify users */
411                 mark_tsc_unstable();
412 #endif
413                 /* Re-enable interrupts */
414                 local_irq_enable();
415                 current_thread_info()->status |= TS_POLLING;
416                 /* Compute time (ticks) that we were actually asleep */
417                 sleep_ticks =
418                     ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
419                 break;
420
421         default:
422                 local_irq_enable();
423                 return;
424         }
425         cx->usage++;
426         if ((cx->type != ACPI_STATE_C1) && (sleep_ticks > 0))
427                 cx->time += sleep_ticks;
428
429         next_state = pr->power.state;
430
431 #ifdef CONFIG_HOTPLUG_CPU
432         /* Don't do promotion/demotion */
433         if ((cx->type == ACPI_STATE_C1) && (num_online_cpus() > 1) &&
434             !pr->flags.has_cst && !acpi_fadt.plvl2_up) {
435                 next_state = cx;
436                 goto end;
437         }
438 #endif
439
440         /*
441          * Promotion?
442          * ----------
443          * Track the number of longs (time asleep is greater than threshold)
444          * and promote when the count threshold is reached.  Note that bus
445          * mastering activity may prevent promotions.
446          * Do not promote above max_cstate.
447          */
448         if (cx->promotion.state &&
449             ((cx->promotion.state - pr->power.states) <= max_cstate)) {
450                 if (sleep_ticks > cx->promotion.threshold.ticks) {
451                         cx->promotion.count++;
452                         cx->demotion.count = 0;
453                         if (cx->promotion.count >=
454                             cx->promotion.threshold.count) {
455                                 if (pr->flags.bm_check) {
456                                         if (!
457                                             (pr->power.bm_activity & cx->
458                                              promotion.threshold.bm)) {
459                                                 next_state =
460                                                     cx->promotion.state;
461                                                 goto end;
462                                         }
463                                 } else {
464                                         next_state = cx->promotion.state;
465                                         goto end;
466                                 }
467                         }
468                 }
469         }
470
471         /*
472          * Demotion?
473          * ---------
474          * Track the number of shorts (time asleep is less than time threshold)
475          * and demote when the usage threshold is reached.
476          */
477         if (cx->demotion.state) {
478                 if (sleep_ticks < cx->demotion.threshold.ticks) {
479                         cx->demotion.count++;
480                         cx->promotion.count = 0;
481                         if (cx->demotion.count >= cx->demotion.threshold.count) {
482                                 next_state = cx->demotion.state;
483                                 goto end;
484                         }
485                 }
486         }
487
488       end:
489         /*
490          * Demote if current state exceeds max_cstate
491          */
492         if ((pr->power.state - pr->power.states) > max_cstate) {
493                 if (cx->demotion.state)
494                         next_state = cx->demotion.state;
495         }
496
497         /*
498          * New Cx State?
499          * -------------
500          * If we're going to start using a new Cx state we must clean up
501          * from the previous and prepare to use the new.
502          */
503         if (next_state != pr->power.state)
504                 acpi_processor_power_activate(pr, next_state);
505 }
506
507 static int acpi_processor_set_power_policy(struct acpi_processor *pr)
508 {
509         unsigned int i;
510         unsigned int state_is_set = 0;
511         struct acpi_processor_cx *lower = NULL;
512         struct acpi_processor_cx *higher = NULL;
513         struct acpi_processor_cx *cx;
514
515
516         if (!pr)
517                 return -EINVAL;
518
519         /*
520          * This function sets the default Cx state policy (OS idle handler).
521          * Our scheme is to promote quickly to C2 but more conservatively
522          * to C3.  We're favoring C2  for its characteristics of low latency
523          * (quick response), good power savings, and ability to allow bus
524          * mastering activity.  Note that the Cx state policy is completely
525          * customizable and can be altered dynamically.
526          */
527
528         /* startup state */
529         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
530                 cx = &pr->power.states[i];
531                 if (!cx->valid)
532                         continue;
533
534                 if (!state_is_set)
535                         pr->power.state = cx;
536                 state_is_set++;
537                 break;
538         }
539
540         if (!state_is_set)
541                 return -ENODEV;
542
543         /* demotion */
544         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
545                 cx = &pr->power.states[i];
546                 if (!cx->valid)
547                         continue;
548
549                 if (lower) {
550                         cx->demotion.state = lower;
551                         cx->demotion.threshold.ticks = cx->latency_ticks;
552                         cx->demotion.threshold.count = 1;
553                         if (cx->type == ACPI_STATE_C3)
554                                 cx->demotion.threshold.bm = bm_history;
555                 }
556
557                 lower = cx;
558         }
559
560         /* promotion */
561         for (i = (ACPI_PROCESSOR_MAX_POWER - 1); i > 0; i--) {
562                 cx = &pr->power.states[i];
563                 if (!cx->valid)
564                         continue;
565
566                 if (higher) {
567                         cx->promotion.state = higher;
568                         cx->promotion.threshold.ticks = cx->latency_ticks;
569                         if (cx->type >= ACPI_STATE_C2)
570                                 cx->promotion.threshold.count = 4;
571                         else
572                                 cx->promotion.threshold.count = 10;
573                         if (higher->type == ACPI_STATE_C3)
574                                 cx->promotion.threshold.bm = bm_history;
575                 }
576
577                 higher = cx;
578         }
579
580         return 0;
581 }
582
583 static int acpi_processor_get_power_info_fadt(struct acpi_processor *pr)
584 {
585
586         if (!pr)
587                 return -EINVAL;
588
589         if (!pr->pblk)
590                 return -ENODEV;
591
592         /* if info is obtained from pblk/fadt, type equals state */
593         pr->power.states[ACPI_STATE_C2].type = ACPI_STATE_C2;
594         pr->power.states[ACPI_STATE_C3].type = ACPI_STATE_C3;
595
596 #ifndef CONFIG_HOTPLUG_CPU
597         /*
598          * Check for P_LVL2_UP flag before entering C2 and above on
599          * an SMP system. 
600          */
601         if ((num_online_cpus() > 1) && !acpi_fadt.plvl2_up)
602                 return -ENODEV;
603 #endif
604
605         /* determine C2 and C3 address from pblk */
606         pr->power.states[ACPI_STATE_C2].address = pr->pblk + 4;
607         pr->power.states[ACPI_STATE_C3].address = pr->pblk + 5;
608
609         /* determine latencies from FADT */
610         pr->power.states[ACPI_STATE_C2].latency = acpi_fadt.plvl2_lat;
611         pr->power.states[ACPI_STATE_C3].latency = acpi_fadt.plvl3_lat;
612
613         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
614                           "lvl2[0x%08x] lvl3[0x%08x]\n",
615                           pr->power.states[ACPI_STATE_C2].address,
616                           pr->power.states[ACPI_STATE_C3].address));
617
618         return 0;
619 }
620
621 static int acpi_processor_get_power_info_default_c1(struct acpi_processor *pr)
622 {
623
624         /* Zero initialize all the C-states info. */
625         memset(pr->power.states, 0, sizeof(pr->power.states));
626
627         /* set the first C-State to C1 */
628         pr->power.states[ACPI_STATE_C1].type = ACPI_STATE_C1;
629
630         /* the C0 state only exists as a filler in our array,
631          * and all processors need to support C1 */
632         pr->power.states[ACPI_STATE_C0].valid = 1;
633         pr->power.states[ACPI_STATE_C1].valid = 1;
634
635         return 0;
636 }
637
638 static int acpi_processor_get_power_info_cst(struct acpi_processor *pr)
639 {
640         acpi_status status = 0;
641         acpi_integer count;
642         int current_count;
643         int i;
644         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
645         union acpi_object *cst;
646
647
648         if (nocst)
649                 return -ENODEV;
650
651         current_count = 1;
652
653         /* Zero initialize C2 onwards and prepare for fresh CST lookup */
654         for (i = 2; i < ACPI_PROCESSOR_MAX_POWER; i++)
655                 memset(&(pr->power.states[i]), 0, 
656                                 sizeof(struct acpi_processor_cx));
657
658         status = acpi_evaluate_object(pr->handle, "_CST", NULL, &buffer);
659         if (ACPI_FAILURE(status)) {
660                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No _CST, giving up\n"));
661                 return -ENODEV;
662         }
663
664         cst = (union acpi_object *)buffer.pointer;
665
666         /* There must be at least 2 elements */
667         if (!cst || (cst->type != ACPI_TYPE_PACKAGE) || cst->package.count < 2) {
668                 printk(KERN_ERR PREFIX "not enough elements in _CST\n");
669                 status = -EFAULT;
670                 goto end;
671         }
672
673         count = cst->package.elements[0].integer.value;
674
675         /* Validate number of power states. */
676         if (count < 1 || count != cst->package.count - 1) {
677                 printk(KERN_ERR PREFIX "count given by _CST is not valid\n");
678                 status = -EFAULT;
679                 goto end;
680         }
681
682         /* Tell driver that at least _CST is supported. */
683         pr->flags.has_cst = 1;
684
685         for (i = 1; i <= count; i++) {
686                 union acpi_object *element;
687                 union acpi_object *obj;
688                 struct acpi_power_register *reg;
689                 struct acpi_processor_cx cx;
690
691                 memset(&cx, 0, sizeof(cx));
692
693                 element = (union acpi_object *)&(cst->package.elements[i]);
694                 if (element->type != ACPI_TYPE_PACKAGE)
695                         continue;
696
697                 if (element->package.count != 4)
698                         continue;
699
700                 obj = (union acpi_object *)&(element->package.elements[0]);
701
702                 if (obj->type != ACPI_TYPE_BUFFER)
703                         continue;
704
705                 reg = (struct acpi_power_register *)obj->buffer.pointer;
706
707                 if (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO &&
708                     (reg->space_id != ACPI_ADR_SPACE_FIXED_HARDWARE))
709                         continue;
710
711                 cx.address = (reg->space_id == ACPI_ADR_SPACE_FIXED_HARDWARE) ?
712                     0 : reg->address;
713
714                 /* There should be an easy way to extract an integer... */
715                 obj = (union acpi_object *)&(element->package.elements[1]);
716                 if (obj->type != ACPI_TYPE_INTEGER)
717                         continue;
718
719                 cx.type = obj->integer.value;
720
721                 if ((cx.type != ACPI_STATE_C1) &&
722                     (reg->space_id != ACPI_ADR_SPACE_SYSTEM_IO))
723                         continue;
724
725                 if ((cx.type < ACPI_STATE_C2) || (cx.type > ACPI_STATE_C3))
726                         continue;
727
728                 obj = (union acpi_object *)&(element->package.elements[2]);
729                 if (obj->type != ACPI_TYPE_INTEGER)
730                         continue;
731
732                 cx.latency = obj->integer.value;
733
734                 obj = (union acpi_object *)&(element->package.elements[3]);
735                 if (obj->type != ACPI_TYPE_INTEGER)
736                         continue;
737
738                 cx.power = obj->integer.value;
739
740                 current_count++;
741                 memcpy(&(pr->power.states[current_count]), &cx, sizeof(cx));
742
743                 /*
744                  * We support total ACPI_PROCESSOR_MAX_POWER - 1
745                  * (From 1 through ACPI_PROCESSOR_MAX_POWER - 1)
746                  */
747                 if (current_count >= (ACPI_PROCESSOR_MAX_POWER - 1)) {
748                         printk(KERN_WARNING
749                                "Limiting number of power states to max (%d)\n",
750                                ACPI_PROCESSOR_MAX_POWER);
751                         printk(KERN_WARNING
752                                "Please increase ACPI_PROCESSOR_MAX_POWER if needed.\n");
753                         break;
754                 }
755         }
756
757         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d power states\n",
758                           current_count));
759
760         /* Validate number of power states discovered */
761         if (current_count < 2)
762                 status = -EFAULT;
763
764       end:
765         acpi_os_free(buffer.pointer);
766
767         return status;
768 }
769
770 static void acpi_processor_power_verify_c2(struct acpi_processor_cx *cx)
771 {
772
773         if (!cx->address)
774                 return;
775
776         /*
777          * C2 latency must be less than or equal to 100
778          * microseconds.
779          */
780         else if (cx->latency > ACPI_PROCESSOR_MAX_C2_LATENCY) {
781                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
782                                   "latency too large [%d]\n", cx->latency));
783                 return;
784         }
785
786         /*
787          * Otherwise we've met all of our C2 requirements.
788          * Normalize the C2 latency to expidite policy
789          */
790         cx->valid = 1;
791         cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
792
793         return;
794 }
795
796 static void acpi_processor_power_verify_c3(struct acpi_processor *pr,
797                                            struct acpi_processor_cx *cx)
798 {
799         static int bm_check_flag;
800
801
802         if (!cx->address)
803                 return;
804
805         /*
806          * C3 latency must be less than or equal to 1000
807          * microseconds.
808          */
809         else if (cx->latency > ACPI_PROCESSOR_MAX_C3_LATENCY) {
810                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
811                                   "latency too large [%d]\n", cx->latency));
812                 return;
813         }
814
815         /*
816          * PIIX4 Erratum #18: We don't support C3 when Type-F (fast)
817          * DMA transfers are used by any ISA device to avoid livelock.
818          * Note that we could disable Type-F DMA (as recommended by
819          * the erratum), but this is known to disrupt certain ISA
820          * devices thus we take the conservative approach.
821          */
822         else if (errata.piix4.fdma) {
823                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
824                                   "C3 not supported on PIIX4 with Type-F DMA\n"));
825                 return;
826         }
827
828         /* All the logic here assumes flags.bm_check is same across all CPUs */
829         if (!bm_check_flag) {
830                 /* Determine whether bm_check is needed based on CPU  */
831                 acpi_processor_power_init_bm_check(&(pr->flags), pr->id);
832                 bm_check_flag = pr->flags.bm_check;
833         } else {
834                 pr->flags.bm_check = bm_check_flag;
835         }
836
837         if (pr->flags.bm_check) {
838                 /* bus mastering control is necessary */
839                 if (!pr->flags.bm_control) {
840                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
841                                           "C3 support requires bus mastering control\n"));
842                         return;
843                 }
844         } else {
845                 /*
846                  * WBINVD should be set in fadt, for C3 state to be
847                  * supported on when bm_check is not required.
848                  */
849                 if (acpi_fadt.wb_invd != 1) {
850                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
851                                           "Cache invalidation should work properly"
852                                           " for C3 to be enabled on SMP systems\n"));
853                         return;
854                 }
855                 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD,
856                                   0, ACPI_MTX_DO_NOT_LOCK);
857         }
858
859         /*
860          * Otherwise we've met all of our C3 requirements.
861          * Normalize the C3 latency to expidite policy.  Enable
862          * checking of bus mastering status (bm_check) so we can
863          * use this in our C3 policy
864          */
865         cx->valid = 1;
866         cx->latency_ticks = US_TO_PM_TIMER_TICKS(cx->latency);
867
868         return;
869 }
870
871 static int acpi_processor_power_verify(struct acpi_processor *pr)
872 {
873         unsigned int i;
874         unsigned int working = 0;
875
876 #ifdef ARCH_APICTIMER_STOPS_ON_C3
877         int timer_broadcast = 0;
878         cpumask_t mask = cpumask_of_cpu(pr->id);
879         on_each_cpu(switch_ipi_to_APIC_timer, &mask, 1, 1);
880 #endif
881
882         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
883                 struct acpi_processor_cx *cx = &pr->power.states[i];
884
885                 switch (cx->type) {
886                 case ACPI_STATE_C1:
887                         cx->valid = 1;
888                         break;
889
890                 case ACPI_STATE_C2:
891                         acpi_processor_power_verify_c2(cx);
892 #ifdef ARCH_APICTIMER_STOPS_ON_C3
893                         /* Some AMD systems fake C3 as C2, but still
894                            have timer troubles */
895                         if (cx->valid && 
896                                 boot_cpu_data.x86_vendor == X86_VENDOR_AMD)
897                                 timer_broadcast++;
898 #endif
899                         break;
900
901                 case ACPI_STATE_C3:
902                         acpi_processor_power_verify_c3(pr, cx);
903 #ifdef ARCH_APICTIMER_STOPS_ON_C3
904                         if (cx->valid)
905                                 timer_broadcast++;
906 #endif
907                         break;
908                 }
909
910                 if (cx->valid)
911                         working++;
912         }
913
914 #ifdef ARCH_APICTIMER_STOPS_ON_C3
915         if (timer_broadcast)
916                 on_each_cpu(switch_APIC_timer_to_ipi, &mask, 1, 1);
917 #endif
918
919         return (working);
920 }
921
922 static int acpi_processor_get_power_info(struct acpi_processor *pr)
923 {
924         unsigned int i;
925         int result;
926
927
928         /* NOTE: the idle thread may not be running while calling
929          * this function */
930
931         /* Adding C1 state */
932         acpi_processor_get_power_info_default_c1(pr);
933         result = acpi_processor_get_power_info_cst(pr);
934         if (result == -ENODEV)
935                 acpi_processor_get_power_info_fadt(pr);
936
937         pr->power.count = acpi_processor_power_verify(pr);
938
939         /*
940          * Set Default Policy
941          * ------------------
942          * Now that we know which states are supported, set the default
943          * policy.  Note that this policy can be changed dynamically
944          * (e.g. encourage deeper sleeps to conserve battery life when
945          * not on AC).
946          */
947         result = acpi_processor_set_power_policy(pr);
948         if (result)
949                 return result;
950
951         /*
952          * if one state of type C2 or C3 is available, mark this
953          * CPU as being "idle manageable"
954          */
955         for (i = 1; i < ACPI_PROCESSOR_MAX_POWER; i++) {
956                 if (pr->power.states[i].valid) {
957                         pr->power.count = i;
958                         if (pr->power.states[i].type >= ACPI_STATE_C2)
959                                 pr->flags.power = 1;
960                 }
961         }
962
963         return 0;
964 }
965
966 int acpi_processor_cst_has_changed(struct acpi_processor *pr)
967 {
968         int result = 0;
969
970
971         if (!pr)
972                 return -EINVAL;
973
974         if (nocst) {
975                 return -ENODEV;
976         }
977
978         if (!pr->flags.power_setup_done)
979                 return -ENODEV;
980
981         /* Fall back to the default idle loop */
982         pm_idle = pm_idle_save;
983         synchronize_sched();    /* Relies on interrupts forcing exit from idle. */
984
985         pr->flags.power = 0;
986         result = acpi_processor_get_power_info(pr);
987         if ((pr->flags.power == 1) && (pr->flags.power_setup_done))
988                 pm_idle = acpi_processor_idle;
989
990         return result;
991 }
992
993 /* proc interface */
994
995 static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
996 {
997         struct acpi_processor *pr = (struct acpi_processor *)seq->private;
998         unsigned int i;
999
1000
1001         if (!pr)
1002                 goto end;
1003
1004         seq_printf(seq, "active state:            C%zd\n"
1005                    "max_cstate:              C%d\n"
1006                    "bus master activity:     %08x\n",
1007                    pr->power.state ? pr->power.state - pr->power.states : 0,
1008                    max_cstate, (unsigned)pr->power.bm_activity);
1009
1010         seq_puts(seq, "states:\n");
1011
1012         for (i = 1; i <= pr->power.count; i++) {
1013                 seq_printf(seq, "   %cC%d:                  ",
1014                            (&pr->power.states[i] ==
1015                             pr->power.state ? '*' : ' '), i);
1016
1017                 if (!pr->power.states[i].valid) {
1018                         seq_puts(seq, "<not supported>\n");
1019                         continue;
1020                 }
1021
1022                 switch (pr->power.states[i].type) {
1023                 case ACPI_STATE_C1:
1024                         seq_printf(seq, "type[C1] ");
1025                         break;
1026                 case ACPI_STATE_C2:
1027                         seq_printf(seq, "type[C2] ");
1028                         break;
1029                 case ACPI_STATE_C3:
1030                         seq_printf(seq, "type[C3] ");
1031                         break;
1032                 default:
1033                         seq_printf(seq, "type[--] ");
1034                         break;
1035                 }
1036
1037                 if (pr->power.states[i].promotion.state)
1038                         seq_printf(seq, "promotion[C%zd] ",
1039                                    (pr->power.states[i].promotion.state -
1040                                     pr->power.states));
1041                 else
1042                         seq_puts(seq, "promotion[--] ");
1043
1044                 if (pr->power.states[i].demotion.state)
1045                         seq_printf(seq, "demotion[C%zd] ",
1046                                    (pr->power.states[i].demotion.state -
1047                                     pr->power.states));
1048                 else
1049                         seq_puts(seq, "demotion[--] ");
1050
1051                 seq_printf(seq, "latency[%03d] usage[%08d] duration[%020llu]\n",
1052                            pr->power.states[i].latency,
1053                            pr->power.states[i].usage,
1054                            pr->power.states[i].time);
1055         }
1056
1057       end:
1058         return 0;
1059 }
1060
1061 static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1062 {
1063         return single_open(file, acpi_processor_power_seq_show,
1064                            PDE(inode)->data);
1065 }
1066
1067 static struct file_operations acpi_processor_power_fops = {
1068         .open = acpi_processor_power_open_fs,
1069         .read = seq_read,
1070         .llseek = seq_lseek,
1071         .release = single_release,
1072 };
1073
1074 int acpi_processor_power_init(struct acpi_processor *pr,
1075                               struct acpi_device *device)
1076 {
1077         acpi_status status = 0;
1078         static int first_run;
1079         struct proc_dir_entry *entry = NULL;
1080         unsigned int i;
1081
1082
1083         if (!first_run) {
1084                 dmi_check_system(processor_power_dmi_table);
1085                 if (max_cstate < ACPI_C_STATES_MAX)
1086                         printk(KERN_NOTICE
1087                                "ACPI: processor limited to max C-state %d\n",
1088                                max_cstate);
1089                 first_run++;
1090         }
1091
1092         if (!pr)
1093                 return -EINVAL;
1094
1095         if (acpi_fadt.cst_cnt && !nocst) {
1096                 status =
1097                     acpi_os_write_port(acpi_fadt.smi_cmd, acpi_fadt.cst_cnt, 8);
1098                 if (ACPI_FAILURE(status)) {
1099                         ACPI_EXCEPTION((AE_INFO, status,
1100                                         "Notifying BIOS of _CST ability failed"));
1101                 }
1102         }
1103
1104         acpi_processor_get_power_info(pr);
1105
1106         /*
1107          * Install the idle handler if processor power management is supported.
1108          * Note that we use previously set idle handler will be used on
1109          * platforms that only support C1.
1110          */
1111         if ((pr->flags.power) && (!boot_option_idle_override)) {
1112                 printk(KERN_INFO PREFIX "CPU%d (power states:", pr->id);
1113                 for (i = 1; i <= pr->power.count; i++)
1114                         if (pr->power.states[i].valid)
1115                                 printk(" C%d[C%d]", i,
1116                                        pr->power.states[i].type);
1117                 printk(")\n");
1118
1119                 if (pr->id == 0) {
1120                         pm_idle_save = pm_idle;
1121                         pm_idle = acpi_processor_idle;
1122                 }
1123         }
1124
1125         /* 'power' [R] */
1126         entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1127                                   S_IRUGO, acpi_device_dir(device));
1128         if (!entry)
1129                 return -EIO;
1130         else {
1131                 entry->proc_fops = &acpi_processor_power_fops;
1132                 entry->data = acpi_driver_data(device);
1133                 entry->owner = THIS_MODULE;
1134         }
1135
1136         pr->flags.power_setup_done = 1;
1137
1138         return 0;
1139 }
1140
1141 int acpi_processor_power_exit(struct acpi_processor *pr,
1142                               struct acpi_device *device)
1143 {
1144
1145         pr->flags.power_setup_done = 0;
1146
1147         if (acpi_device_dir(device))
1148                 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,
1149                                   acpi_device_dir(device));
1150
1151         /* Unregister the idle handler when processor #0 is removed. */
1152         if (pr->id == 0) {
1153                 pm_idle = pm_idle_save;
1154
1155                 /*
1156                  * We are about to unload the current idle thread pm callback
1157                  * (pm_idle), Wait for all processors to update cached/local
1158                  * copies of pm_idle before proceeding.
1159                  */
1160                 cpu_idle_wait();
1161         }
1162
1163         return 0;
1164 }