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