vserver 1.9.3
[linux-2.6.git] / drivers / acpi / processor.c
1 /*
2  * acpi_processor.c - ACPI Processor Driver ($Revision: 71 $)
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       Dominik Brodowski <linux@brodo.de>
7  *
8  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
9  *
10  *  This program is free software; you can redistribute it and/or modify
11  *  it under the terms of the GNU General Public License as published by
12  *  the Free Software Foundation; either version 2 of the License, or (at
13  *  your option) any later version.
14  *
15  *  This program is distributed in the hope that it will be useful, but
16  *  WITHOUT ANY WARRANTY; without even the implied warranty of
17  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  *  General Public License for more details.
19  *
20  *  You should have received a copy of the GNU General Public License along
21  *  with this program; if not, write to the Free Software Foundation, Inc.,
22  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
23  *
24  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25  *  TBD:
26  *      1. Make # power states dynamic.
27  *      2. Support duty_cycle values that span bit 4.
28  *      3. Optimize by having scheduler determine business instead of
29  *         having us try to calculate it here.
30  *      4. Need C1 timing -- must modify kernel (IRQ handler) to get this.
31  */
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/types.h>
37 #include <linux/pci.h>
38 #include <linux/pm.h>
39 #include <linux/cpufreq.h>
40 #include <linux/proc_fs.h>
41 #include <linux/seq_file.h>
42
43 #include <asm/io.h>
44 #include <asm/system.h>
45 #include <asm/delay.h>
46 #include <asm/uaccess.h>
47 #include <asm/processor.h>
48 #include <asm/smp.h>
49 #include <asm/acpi.h>
50
51 #include <acpi/acpi_bus.h>
52 #include <acpi/acpi_drivers.h>
53 #include <acpi/processor.h>
54
55
56 #define ACPI_PROCESSOR_COMPONENT        0x01000000
57 #define ACPI_PROCESSOR_CLASS            "processor"
58 #define ACPI_PROCESSOR_DRIVER_NAME      "ACPI Processor Driver"
59 #define ACPI_PROCESSOR_DEVICE_NAME      "Processor"
60 #define ACPI_PROCESSOR_FILE_INFO        "info"
61 #define ACPI_PROCESSOR_FILE_POWER       "power"
62 #define ACPI_PROCESSOR_FILE_THROTTLING  "throttling"
63 #define ACPI_PROCESSOR_FILE_LIMIT       "limit"
64 #define ACPI_PROCESSOR_FILE_PERFORMANCE "performance"
65 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
66 #define ACPI_PROCESSOR_NOTIFY_POWER     0x81
67
68 #define US_TO_PM_TIMER_TICKS(t)         ((t * (PM_TIMER_FREQUENCY/1000)) / 1000)
69 #define C2_OVERHEAD                     4       /* 1us (3.579 ticks per us) */
70 #define C3_OVERHEAD                     4       /* 1us (3.579 ticks per us) */
71
72
73 #define ACPI_PROCESSOR_LIMIT_USER       0
74 #define ACPI_PROCESSOR_LIMIT_THERMAL    1
75
76 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
77 ACPI_MODULE_NAME                ("acpi_processor")
78
79 MODULE_AUTHOR("Paul Diefenbaugh");
80 MODULE_DESCRIPTION(ACPI_PROCESSOR_DRIVER_NAME);
81 MODULE_LICENSE("GPL");
82
83
84 static int acpi_processor_add (struct acpi_device *device);
85 static int acpi_processor_remove (struct acpi_device *device, int type);
86 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file);
87 static int acpi_processor_throttling_open_fs(struct inode *inode, struct file *file);
88 static int acpi_processor_power_open_fs(struct inode *inode, struct file *file);
89 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file);
90 static int acpi_processor_get_limit_info(struct acpi_processor *pr);
91
92 static struct acpi_driver acpi_processor_driver = {
93         .name =         ACPI_PROCESSOR_DRIVER_NAME,
94         .class =        ACPI_PROCESSOR_CLASS,
95         .ids =          ACPI_PROCESSOR_HID,
96         .ops =          {
97                                 .add =          acpi_processor_add,
98                                 .remove =       acpi_processor_remove,
99                         },
100 };
101
102
103 struct acpi_processor_errata {
104         u8                      smp;
105         struct {
106                 u8                      throttle:1;
107                 u8                      fdma:1;
108                 u8                      reserved:6;
109                 u32                     bmisx;
110         }                       piix4;
111 };
112
113 static struct file_operations acpi_processor_info_fops = {
114         .open           = acpi_processor_info_open_fs,
115         .read           = seq_read,
116         .llseek         = seq_lseek,
117         .release        = single_release,
118 };
119
120 static struct file_operations acpi_processor_power_fops = {
121         .open           = acpi_processor_power_open_fs,
122         .read           = seq_read,
123         .llseek         = seq_lseek,
124         .release        = single_release,
125 };
126
127 static struct file_operations acpi_processor_throttling_fops = {
128         .open           = acpi_processor_throttling_open_fs,
129         .read           = seq_read,
130         .llseek         = seq_lseek,
131         .release        = single_release,
132 };
133
134 static struct file_operations acpi_processor_limit_fops = {
135         .open           = acpi_processor_limit_open_fs,
136         .read           = seq_read,
137         .llseek         = seq_lseek,
138         .release        = single_release,
139 };
140
141 static struct acpi_processor    *processors[NR_CPUS];
142 static struct acpi_processor_errata errata;
143 static void (*pm_idle_save)(void);
144
145
146 /* --------------------------------------------------------------------------
147                                 Errata Handling
148    -------------------------------------------------------------------------- */
149
150 int
151 acpi_processor_errata_piix4 (
152         struct pci_dev          *dev)
153 {
154         u8                      rev = 0;
155         u8                      value1 = 0;
156         u8                      value2 = 0;
157
158         ACPI_FUNCTION_TRACE("acpi_processor_errata_piix4");
159
160         if (!dev)
161                 return_VALUE(-EINVAL);
162
163         /*
164          * Note that 'dev' references the PIIX4 ACPI Controller.
165          */
166
167         pci_read_config_byte(dev, PCI_REVISION_ID, &rev);
168
169         switch (rev) {
170         case 0:
171                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
172                 break;
173         case 1:
174                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
175                 break;
176         case 2:
177                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
178                 break;
179         case 3:
180                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
181                 break;
182         default:
183                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
184                 break;
185         }
186
187         switch (rev) {
188
189         case 0:         /* PIIX4 A-step */
190         case 1:         /* PIIX4 B-step */
191                 /*
192                  * See specification changes #13 ("Manual Throttle Duty Cycle")
193                  * and #14 ("Enabling and Disabling Manual Throttle"), plus
194                  * erratum #5 ("STPCLK# Deassertion Time") from the January 
195                  * 2002 PIIX4 specification update.  Applies to only older 
196                  * PIIX4 models.
197                  */
198                 errata.piix4.throttle = 1;
199
200         case 2:         /* PIIX4E */
201         case 3:         /* PIIX4M */
202                 /*
203                  * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA 
204                  * Livelock") from the January 2002 PIIX4 specification update.
205                  * Applies to all PIIX4 models.
206                  */
207
208                 /* 
209                  * BM-IDE
210                  * ------
211                  * Find the PIIX4 IDE Controller and get the Bus Master IDE 
212                  * Status register address.  We'll use this later to read 
213                  * each IDE controller's DMA status to make sure we catch all
214                  * DMA activity.
215                  */
216                 dev = pci_find_subsys(PCI_VENDOR_ID_INTEL,
217                            PCI_DEVICE_ID_INTEL_82371AB, 
218                            PCI_ANY_ID, PCI_ANY_ID, NULL);
219                 if (dev)
220                         errata.piix4.bmisx = pci_resource_start(dev, 4);
221
222                 /* 
223                  * Type-F DMA
224                  * ----------
225                  * Find the PIIX4 ISA Controller and read the Motherboard
226                  * DMA controller's status to see if Type-F (Fast) DMA mode
227                  * is enabled (bit 7) on either channel.  Note that we'll 
228                  * disable C3 support if this is enabled, as some legacy 
229                  * devices won't operate well if fast DMA is disabled.
230                  */
231                 dev = pci_find_subsys(PCI_VENDOR_ID_INTEL, 
232                         PCI_DEVICE_ID_INTEL_82371AB_0, 
233                         PCI_ANY_ID, PCI_ANY_ID, NULL);
234                 if (dev) {
235                         pci_read_config_byte(dev, 0x76, &value1);
236                         pci_read_config_byte(dev, 0x77, &value2);
237                         if ((value1 & 0x80) || (value2 & 0x80))
238                                 errata.piix4.fdma = 1;
239                 }
240
241                 break;
242         }
243
244         if (errata.piix4.bmisx)
245                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
246                         "Bus master activity detection (BM-IDE) erratum enabled\n"));
247         if (errata.piix4.fdma)
248                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
249                         "Type-F DMA livelock erratum (C3 disabled)\n"));
250
251         return_VALUE(0);
252 }
253
254
255 int
256 acpi_processor_errata (
257         struct acpi_processor   *pr)
258 {
259         int                     result = 0;
260         struct pci_dev          *dev = NULL;
261
262         ACPI_FUNCTION_TRACE("acpi_processor_errata");
263
264         if (!pr)
265                 return_VALUE(-EINVAL);
266
267         /*
268          * PIIX4
269          */
270         dev = pci_find_subsys(PCI_VENDOR_ID_INTEL, 
271                 PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID, PCI_ANY_ID, NULL);
272         if (dev)
273                 result = acpi_processor_errata_piix4(dev);
274
275         return_VALUE(result);
276 }
277
278
279 /* --------------------------------------------------------------------------
280                                 Power Management
281    -------------------------------------------------------------------------- */
282
283 static inline u32
284 ticks_elapsed (
285         u32                     t1,
286         u32                     t2)
287 {
288         if (t2 >= t1)
289                 return (t2 - t1);
290         else if (!acpi_fadt.tmr_val_ext)
291                 return (((0x00FFFFFF - t1) + t2) & 0x00FFFFFF);
292         else
293                 return ((0xFFFFFFFF - t1) + t2);
294 }
295
296
297 static void
298 acpi_processor_power_activate (
299         struct acpi_processor   *pr,
300         int                     state)
301 {
302         if (!pr)
303                 return;
304
305         pr->power.states[pr->power.state].promotion.count = 0;
306         pr->power.states[pr->power.state].demotion.count = 0;
307
308         /* Cleanup from old state. */
309         switch (pr->power.state) {
310         case ACPI_STATE_C3:
311                 /* Disable bus master reload */
312                 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 0, ACPI_MTX_DO_NOT_LOCK);
313                 break;
314         }
315
316         /* Prepare to use new state. */
317         switch (state) {
318         case ACPI_STATE_C3:
319                 /* Enable bus master reload */
320                 acpi_set_register(ACPI_BITREG_BUS_MASTER_RLD, 1, ACPI_MTX_DO_NOT_LOCK);
321                 break;
322         }
323
324         pr->power.state = state;
325
326         return;
327 }
328
329
330 static void
331 acpi_processor_idle (void)
332 {
333         struct acpi_processor   *pr = NULL;
334         struct acpi_processor_cx *cx = NULL;
335         int                     next_state = 0;
336         int                     sleep_ticks = 0;
337         u32                     t1, t2 = 0;
338
339         pr = processors[smp_processor_id()];
340         if (!pr)
341                 return;
342
343         /*
344          * Interrupts must be disabled during bus mastering calculations and
345          * for C2/C3 transitions.
346          */
347         local_irq_disable();
348
349         cx = &(pr->power.states[pr->power.state]);
350
351         /*
352          * Check BM Activity
353          * -----------------
354          * Check for bus mastering activity (if required), record, and check
355          * for demotion.
356          */
357         if (pr->flags.bm_check) {
358                 u32             bm_status = 0;
359
360                 pr->power.bm_activity <<= 1;
361
362                 acpi_get_register(ACPI_BITREG_BUS_MASTER_STATUS, 
363                         &bm_status, ACPI_MTX_DO_NOT_LOCK);
364                 if (bm_status) {
365                         pr->power.bm_activity++;
366                         acpi_set_register(ACPI_BITREG_BUS_MASTER_STATUS,
367                                 1, ACPI_MTX_DO_NOT_LOCK);
368                 }
369                 /*
370                  * PIIX4 Erratum #18: Note that BM_STS doesn't always reflect
371                  * the true state of bus mastering activity; forcing us to 
372                  * manually check the BMIDEA bit of each IDE channel.
373                  */
374                 else if (errata.piix4.bmisx) {
375                         if ((inb_p(errata.piix4.bmisx + 0x02) & 0x01) 
376                                 || (inb_p(errata.piix4.bmisx + 0x0A) & 0x01))
377                                 pr->power.bm_activity++;
378                 }
379                 /*
380                  * Apply bus mastering demotion policy.  Automatically demote
381                  * to avoid a faulty transition.  Note that the processor 
382                  * won't enter a low-power state during this call (to this 
383                  * funciton) but should upon the next.
384                  *
385                  * TBD: A better policy might be to fallback to the demotion 
386                  *      state (use it for this quantum only) istead of 
387                  *      demoting -- and rely on duration as our sole demotion
388                  *      qualification.  This may, however, introduce DMA 
389                  *      issues (e.g. floppy DMA transfer overrun/underrun).
390                  */
391                 if (pr->power.bm_activity & cx->demotion.threshold.bm) {
392                         local_irq_enable();
393                         next_state = cx->demotion.state;
394                         goto end;
395                 }
396         }
397
398         cx->usage++;
399
400         /*
401          * Sleep:
402          * ------
403          * Invoke the current Cx state to put the processor to sleep.
404          */
405         switch (pr->power.state) {
406
407         case ACPI_STATE_C1:
408                 /* Invoke C1. */
409                 safe_halt();
410                 /*
411                  * TBD: Can't get time duration while in C1, as resumes
412                  *      go to an ISR rather than here.  Need to instrument
413                  *      base interrupt handler.
414                  */
415                 sleep_ticks = 0xFFFFFFFF;
416                 break;
417
418         case ACPI_STATE_C2:
419                 /* Get start time (ticks) */
420                 t1 = inl(acpi_fadt.xpm_tmr_blk.address);
421                 /* Invoke C2 */
422                 inb(pr->power.states[ACPI_STATE_C2].address);
423                 /* Dummy op - must do something useless after P_LVL2 read */
424                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
425                 /* Get end time (ticks) */
426                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
427                 /* Re-enable interrupts */
428                 local_irq_enable();
429                 /* Compute time (ticks) that we were actually asleep */
430                 sleep_ticks = ticks_elapsed(t1, t2) - cx->latency_ticks - C2_OVERHEAD;
431                 break;
432
433         case ACPI_STATE_C3:
434                 /* Disable bus master arbitration */
435                 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 1, ACPI_MTX_DO_NOT_LOCK);
436                 /* Get start time (ticks) */
437                 t1 = inl(acpi_fadt.xpm_tmr_blk.address);
438                 /* Invoke C3 */
439                 inb(pr->power.states[ACPI_STATE_C3].address);
440                 /* Dummy op - must do something useless after P_LVL3 read */
441                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
442                 /* Get end time (ticks) */
443                 t2 = inl(acpi_fadt.xpm_tmr_blk.address);
444                 /* Enable bus master arbitration */
445                 acpi_set_register(ACPI_BITREG_ARB_DISABLE, 0, ACPI_MTX_DO_NOT_LOCK);
446                 /* Re-enable interrupts */
447                 local_irq_enable();
448                 /* Compute time (ticks) that we were actually asleep */
449                 sleep_ticks = ticks_elapsed(t1, t2) - cx->latency_ticks - C3_OVERHEAD;
450                 break;
451
452         default:
453                 local_irq_enable();
454                 return;
455         }
456
457         next_state = pr->power.state;
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          */
466         if (cx->promotion.state) {
467                 if (sleep_ticks > cx->promotion.threshold.ticks) {
468                         cx->promotion.count++;
469                         cx->demotion.count = 0;
470                         if (cx->promotion.count >= cx->promotion.threshold.count) {
471                                 if (pr->flags.bm_check) {
472                                         if (!(pr->power.bm_activity & cx->promotion.threshold.bm)) {
473                                                 next_state = cx->promotion.state;
474                                                 goto end;
475                                         }
476                                 }
477                                 else {
478                                         next_state = cx->promotion.state;
479                                         goto end;
480                                 }
481                         }
482                 }
483         }
484
485         /*
486          * Demotion?
487          * ---------
488          * Track the number of shorts (time asleep is less than time threshold)
489          * and demote when the usage threshold is reached.
490          */
491         if (cx->demotion.state) {
492                 if (sleep_ticks < cx->demotion.threshold.ticks) {
493                         cx->demotion.count++;
494                         cx->promotion.count = 0;
495                         if (cx->demotion.count >= cx->demotion.threshold.count) {
496                                 next_state = cx->demotion.state;
497                                 goto end;
498                         }
499                 }
500         }
501
502 end:
503         /*
504          * New Cx State?
505          * -------------
506          * If we're going to start using a new Cx state we must clean up
507          * from the previous and prepare to use the new.
508          */
509         if (next_state != pr->power.state)
510                 acpi_processor_power_activate(pr, next_state);
511
512         return;
513 }
514
515
516 static int
517 acpi_processor_set_power_policy (
518         struct acpi_processor   *pr)
519 {
520         ACPI_FUNCTION_TRACE("acpi_processor_set_power_policy");
521
522         /*
523          * This function sets the default Cx state policy (OS idle handler).
524          * Our scheme is to promote quickly to C2 but more conservatively
525          * to C3.  We're favoring C2  for its characteristics of low latency
526          * (quick response), good power savings, and ability to allow bus
527          * mastering activity.  Note that the Cx state policy is completely
528          * customizable and can be altered dynamically.
529          */
530
531         if (!pr)
532                 return_VALUE(-EINVAL);
533
534         /*
535          * C0/C1
536          * -----
537          */
538         pr->power.state = ACPI_STATE_C1;
539         pr->power.default_state = ACPI_STATE_C1;
540
541         /*
542          * C1/C2
543          * -----
544          * Set the default C1 promotion and C2 demotion policies, where we
545          * promote from C1 to C2 after several (10) successive C1 transitions,
546          * as we cannot (currently) measure the time spent in C1. Demote from
547          * C2 to C1 anytime we experience a 'short' (time spent in C2 is less
548          * than the C2 transtion latency).  Note the simplifying assumption 
549          * that the 'cost' of a transition is amortized when we sleep for at
550          * least as long as the transition's latency (thus the total transition
551          * time is two times the latency).
552          *
553          * TBD: Measure C1 sleep times by instrumenting the core IRQ handler.
554          * TBD: Demote to default C-State after long periods of activity.
555          * TBD: Investigate policy's use of CPU utilization -vs- sleep duration.
556          */
557         if (pr->power.states[ACPI_STATE_C2].valid) {
558                 pr->power.states[ACPI_STATE_C1].promotion.threshold.count = 10;
559                 pr->power.states[ACPI_STATE_C1].promotion.threshold.ticks =
560                         pr->power.states[ACPI_STATE_C2].latency_ticks;
561                 pr->power.states[ACPI_STATE_C1].promotion.state = ACPI_STATE_C2;
562
563                 pr->power.states[ACPI_STATE_C2].demotion.threshold.count = 1;
564                 pr->power.states[ACPI_STATE_C2].demotion.threshold.ticks =
565                         pr->power.states[ACPI_STATE_C2].latency_ticks;
566                 pr->power.states[ACPI_STATE_C2].demotion.state = ACPI_STATE_C1;
567         }
568
569         /*
570          * C2/C3
571          * -----
572          * Set default C2 promotion and C3 demotion policies, where we promote
573          * from C2 to C3 after several (4) cycles of no bus mastering activity
574          * while maintaining sleep time criteria.  Demote immediately on a
575          * short or whenever bus mastering activity occurs.
576          */
577         if ((pr->power.states[ACPI_STATE_C2].valid) &&
578                 (pr->power.states[ACPI_STATE_C3].valid)) {
579                 pr->power.states[ACPI_STATE_C2].promotion.threshold.count = 4;
580                 pr->power.states[ACPI_STATE_C2].promotion.threshold.ticks =
581                         pr->power.states[ACPI_STATE_C3].latency_ticks;
582                 pr->power.states[ACPI_STATE_C2].promotion.threshold.bm = 0x0F;
583                 pr->power.states[ACPI_STATE_C2].promotion.state = ACPI_STATE_C3;
584
585                 pr->power.states[ACPI_STATE_C3].demotion.threshold.count = 1;
586                 pr->power.states[ACPI_STATE_C3].demotion.threshold.ticks =
587                         pr->power.states[ACPI_STATE_C3].latency_ticks;
588                 pr->power.states[ACPI_STATE_C3].demotion.threshold.bm = 0x0F;
589                 pr->power.states[ACPI_STATE_C3].demotion.state = ACPI_STATE_C2;
590         }
591
592         return_VALUE(0);
593 }
594
595
596 int
597 acpi_processor_get_power_info (
598         struct acpi_processor   *pr)
599 {
600         int                     result = 0;
601
602         ACPI_FUNCTION_TRACE("acpi_processor_get_power_info");
603
604         if (!pr)
605                 return_VALUE(-EINVAL);
606
607         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
608                 "lvl2[0x%08x] lvl3[0x%08x]\n",
609                 pr->power.states[ACPI_STATE_C2].address,
610                 pr->power.states[ACPI_STATE_C3].address));
611
612         /* TBD: Support ACPI 2.0 objects */
613
614         /*
615          * C0
616          * --
617          * This state exists only as filler in our array.
618          */
619         pr->power.states[ACPI_STATE_C0].valid = 1;
620
621         /*
622          * C1
623          * --
624          * ACPI requires C1 support for all processors.
625          *
626          * TBD: What about PROC_C1?
627          */
628         pr->power.states[ACPI_STATE_C1].valid = 1;
629
630         /*
631          * C2
632          * --
633          * We're (currently) only supporting C2 on UP systems.
634          *
635          * TBD: Support for C2 on MP (P_LVL2_UP).
636          */
637         if (pr->power.states[ACPI_STATE_C2].address) {
638
639                 pr->power.states[ACPI_STATE_C2].latency = acpi_fadt.plvl2_lat;
640
641                 /*
642                  * C2 latency must be less than or equal to 100 microseconds.
643                  */
644                 if (acpi_fadt.plvl2_lat > ACPI_PROCESSOR_MAX_C2_LATENCY)
645                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
646                                 "C2 latency too large [%d]\n",
647                                 acpi_fadt.plvl2_lat));
648                 /*
649                  * Only support C2 on UP systems (see TBD above).
650                  */
651                 else if (errata.smp)
652                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
653                                 "C2 not supported in SMP mode\n"));
654                 /*
655                  * Otherwise we've met all of our C2 requirements.
656                  * Normalize the C2 latency to expidite policy.
657                  */
658                 else {
659                         pr->power.states[ACPI_STATE_C2].valid = 1;
660                         pr->power.states[ACPI_STATE_C2].latency_ticks = 
661                                 US_TO_PM_TIMER_TICKS(acpi_fadt.plvl2_lat);
662                 }
663         }
664
665         /*
666          * C3
667          * --
668          * TBD: Investigate use of WBINVD on UP/SMP system in absence of
669          *      bm_control.
670          */
671         if (pr->power.states[ACPI_STATE_C3].address) {
672
673                 pr->power.states[ACPI_STATE_C3].latency = acpi_fadt.plvl3_lat;
674
675                 /*
676                  * C3 latency must be less than or equal to 1000 microseconds.
677                  */
678                 if (acpi_fadt.plvl3_lat > ACPI_PROCESSOR_MAX_C3_LATENCY)
679                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
680                                 "C3 latency too large [%d]\n", 
681                                 acpi_fadt.plvl3_lat));
682                 /*
683                  * Only support C3 when bus mastering arbitration control
684                  * is present (able to disable bus mastering to maintain
685                  * cache coherency while in C3).
686                  */
687                 else if (!pr->flags.bm_control)
688                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
689                                 "C3 support requires bus mastering control\n"));
690                 /*
691                  * Only support C3 on UP systems, as bm_control is only viable
692                  * on a UP system and flushing caches (e.g. WBINVD) is simply 
693                  * too costly (at this time).
694                  */
695                 else if (errata.smp)
696                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
697                                 "C3 not supported in SMP mode\n"));
698                 /*
699                  * PIIX4 Erratum #18: We don't support C3 when Type-F (fast) 
700                  * DMA transfers are used by any ISA device to avoid livelock.
701                  * Note that we could disable Type-F DMA (as recommended by
702                  * the erratum), but this is known to disrupt certain ISA 
703                  * devices thus we take the conservative approach.
704                  */
705                 else if (errata.piix4.fdma) {
706                         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
707                                 "C3 not supported on PIIX4 with Type-F DMA\n"));
708                 }
709                 /*
710                  * Otherwise we've met all of our C3 requirements.  
711                  * Normalize the C2 latency to expidite policy.  Enable
712                  * checking of bus mastering status (bm_check) so we can 
713                  * use this in our C3 policy.
714                  */
715                 else {
716                         pr->power.states[ACPI_STATE_C3].valid = 1;
717                         pr->power.states[ACPI_STATE_C3].latency_ticks = 
718                                 US_TO_PM_TIMER_TICKS(acpi_fadt.plvl3_lat);
719                         pr->flags.bm_check = 1;
720                 }
721         }
722
723         /*
724          * Set Default Policy
725          * ------------------
726          * Now that we know which state are supported, set the default
727          * policy.  Note that this policy can be changed dynamically
728          * (e.g. encourage deeper sleeps to conserve battery life when
729          * not on AC).
730          */
731         result = acpi_processor_set_power_policy(pr);
732         if (result)
733                 return_VALUE(result);
734
735         /*
736          * If this processor supports C2 or C3 we denote it as being 'power
737          * manageable'.  Note that there's really no policy involved for
738          * when only C1 is supported.
739          */
740         if (pr->power.states[ACPI_STATE_C2].valid 
741                 || pr->power.states[ACPI_STATE_C3].valid)
742                 pr->flags.power = 1;
743
744         return_VALUE(0);
745 }
746
747
748 /* --------------------------------------------------------------------------
749                               Performance Management
750    -------------------------------------------------------------------------- */
751 #ifdef CONFIG_CPU_FREQ
752
753 static DECLARE_MUTEX(performance_sem);
754
755 /*
756  * _PPC support is implemented as a CPUfreq policy notifier: 
757  * This means each time a CPUfreq driver registered also with
758  * the ACPI core is asked to change the speed policy, the maximum
759  * value is adjusted so that it is within the platform limit.
760  * 
761  * Also, when a new platform limit value is detected, the CPUfreq
762  * policy is adjusted accordingly.
763  */
764
765 static int acpi_processor_ppc_is_init = 0;
766
767 static int acpi_processor_ppc_notifier(struct notifier_block *nb, 
768         unsigned long event,
769         void *data)
770 {
771         struct cpufreq_policy *policy = data;
772         struct acpi_processor *pr;
773         unsigned int ppc = 0;
774
775         down(&performance_sem);
776
777         if (event != CPUFREQ_INCOMPATIBLE)
778                 goto out;
779
780         pr = processors[policy->cpu];
781         if (!pr || !pr->performance)
782                 goto out;
783
784         ppc = (unsigned int) pr->performance_platform_limit;
785         if (!ppc)
786                 goto out;
787
788         if (ppc > pr->performance->state_count)
789                 goto out;
790
791         cpufreq_verify_within_limits(policy, 0, 
792                 pr->performance->states[ppc].core_frequency * 1000);
793
794  out:
795         up(&performance_sem);
796
797         return 0;
798 }
799
800
801 static struct notifier_block acpi_ppc_notifier_block = {
802         .notifier_call = acpi_processor_ppc_notifier,
803 };
804
805
806 static int
807 acpi_processor_get_platform_limit (
808         struct acpi_processor*  pr)
809 {
810         acpi_status             status = 0;
811         unsigned long           ppc = 0;
812
813         ACPI_FUNCTION_TRACE("acpi_processor_get_platform_limit");
814
815         if (!pr)
816                 return_VALUE(-EINVAL);
817
818         /*
819          * _PPC indicates the maximum state currently supported by the platform
820          * (e.g. 0 = states 0..n; 1 = states 1..n; etc.
821          */
822         status = acpi_evaluate_integer(pr->handle, "_PPC", NULL, &ppc);
823         if(ACPI_FAILURE(status) && status != AE_NOT_FOUND) {
824                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PPC\n"));
825                 return_VALUE(-ENODEV);
826         }
827
828         pr->performance_platform_limit = (int) ppc;
829         
830         return_VALUE(0);
831 }
832
833
834 static int acpi_processor_ppc_has_changed(
835         struct acpi_processor *pr)
836 {
837         int ret = acpi_processor_get_platform_limit(pr);
838         if (ret < 0)
839                 return (ret);
840         else
841                 return cpufreq_update_policy(pr->id);
842 }
843
844
845 static void acpi_processor_ppc_init(void) {
846         if (!cpufreq_register_notifier(&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER))
847                 acpi_processor_ppc_is_init = 1;
848         else
849                 printk(KERN_DEBUG "Warning: Processor Platform Limit not supported.\n");
850 }
851
852
853 static void acpi_processor_ppc_exit(void) {
854         if (acpi_processor_ppc_is_init)
855                 cpufreq_unregister_notifier(&acpi_ppc_notifier_block, CPUFREQ_POLICY_NOTIFIER);
856
857         acpi_processor_ppc_is_init = 0;
858 }
859
860 /*
861  * when registering a cpufreq driver with this ACPI processor driver, the
862  * _PCT and _PSS structures are read out and written into struct
863  * acpi_processor_performance.
864  */
865 static int acpi_processor_set_pdc (struct acpi_processor *pr)
866 {
867         acpi_status             status = AE_OK;
868         u32                     arg0_buf[3];
869         union acpi_object       arg0 = {ACPI_TYPE_BUFFER};
870         struct acpi_object_list no_object = {1, &arg0};
871         struct acpi_object_list *pdc;
872
873         ACPI_FUNCTION_TRACE("acpi_processor_set_pdc");
874         
875         arg0.buffer.length = 12;
876         arg0.buffer.pointer = (u8 *) arg0_buf;
877         arg0_buf[0] = ACPI_PDC_REVISION_ID;
878         arg0_buf[1] = 0;
879         arg0_buf[2] = 0;
880
881         pdc = (pr->performance->pdc) ? pr->performance->pdc : &no_object;
882
883         status = acpi_evaluate_object(pr->handle, "_PDC", pdc, NULL);
884
885         if ((ACPI_FAILURE(status)) && (pr->performance->pdc))
886                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Error evaluating _PDC, using legacy perf. control...\n"));
887
888         return_VALUE(status);
889 }
890
891
892 static int 
893 acpi_processor_get_performance_control (
894         struct acpi_processor *pr)
895 {
896         int                     result = 0;
897         acpi_status             status = 0;
898         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
899         union acpi_object       *pct = NULL;
900         union acpi_object       obj = {0};
901
902         ACPI_FUNCTION_TRACE("acpi_processor_get_performance_control");
903
904         status = acpi_evaluate_object(pr->handle, "_PCT", NULL, &buffer);
905         if(ACPI_FAILURE(status)) {
906                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PCT\n"));
907                 return_VALUE(-ENODEV);
908         }
909
910         pct = (union acpi_object *) buffer.pointer;
911         if (!pct || (pct->type != ACPI_TYPE_PACKAGE) 
912                 || (pct->package.count != 2)) {
913                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PCT data\n"));
914                 result = -EFAULT;
915                 goto end;
916         }
917
918         /*
919          * control_register
920          */
921
922         obj = pct->package.elements[0];
923
924         if ((obj.type != ACPI_TYPE_BUFFER) 
925                 || (obj.buffer.length < sizeof(struct acpi_pct_register)) 
926                 || (obj.buffer.pointer == NULL)) {
927                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
928                         "Invalid _PCT data (control_register)\n"));
929                 result = -EFAULT;
930                 goto end;
931         }
932         memcpy(&pr->performance->control_register, obj.buffer.pointer, sizeof(struct acpi_pct_register));
933
934
935         /*
936          * status_register
937          */
938
939         obj = pct->package.elements[1];
940
941         if ((obj.type != ACPI_TYPE_BUFFER) 
942                 || (obj.buffer.length < sizeof(struct acpi_pct_register)) 
943                 || (obj.buffer.pointer == NULL)) {
944                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
945                         "Invalid _PCT data (status_register)\n"));
946                 result = -EFAULT;
947                 goto end;
948         }
949
950         memcpy(&pr->performance->status_register, obj.buffer.pointer, sizeof(struct acpi_pct_register));
951
952 end:
953         acpi_os_free(buffer.pointer);
954
955         return_VALUE(result);
956 }
957
958
959 static int 
960 acpi_processor_get_performance_states (
961         struct acpi_processor   *pr)
962 {
963         int                     result = 0;
964         acpi_status             status = AE_OK;
965         struct acpi_buffer      buffer = {ACPI_ALLOCATE_BUFFER, NULL};
966         struct acpi_buffer      format = {sizeof("NNNNNN"), "NNNNNN"};
967         struct acpi_buffer      state = {0, NULL};
968         union acpi_object       *pss = NULL;
969         int                     i = 0;
970
971         ACPI_FUNCTION_TRACE("acpi_processor_get_performance_states");
972
973         status = acpi_evaluate_object(pr->handle, "_PSS", NULL, &buffer);
974         if(ACPI_FAILURE(status)) {
975                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _PSS\n"));
976                 return_VALUE(-ENODEV);
977         }
978
979         pss = (union acpi_object *) buffer.pointer;
980         if (!pss || (pss->type != ACPI_TYPE_PACKAGE)) {
981                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data\n"));
982                 result = -EFAULT;
983                 goto end;
984         }
985
986         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d performance states\n", 
987                 pss->package.count));
988
989         pr->performance->state_count = pss->package.count;
990         pr->performance->states = kmalloc(sizeof(struct acpi_processor_px) * pss->package.count, GFP_KERNEL);
991         if (!pr->performance->states) {
992                 result = -ENOMEM;
993                 goto end;
994         }
995
996         for (i = 0; i < pr->performance->state_count; i++) {
997
998                 struct acpi_processor_px *px = &(pr->performance->states[i]);
999
1000                 state.length = sizeof(struct acpi_processor_px);
1001                 state.pointer = px;
1002
1003                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Extracting state %d\n", i));
1004
1005                 status = acpi_extract_package(&(pss->package.elements[i]), 
1006                         &format, &state);
1007                 if (ACPI_FAILURE(status)) {
1008                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data\n"));
1009                         result = -EFAULT;
1010                         kfree(pr->performance->states);
1011                         goto end;
1012                 }
1013
1014                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1015                         "State [%d]: core_frequency[%d] power[%d] transition_latency[%d] bus_master_latency[%d] control[0x%x] status[0x%x]\n",
1016                         i, 
1017                         (u32) px->core_frequency, 
1018                         (u32) px->power, 
1019                         (u32) px->transition_latency, 
1020                         (u32) px->bus_master_latency,
1021                         (u32) px->control, 
1022                         (u32) px->status));
1023
1024                 if (!px->core_frequency) {
1025                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid _PSS data: freq is zero\n"));
1026                         result = -EFAULT;
1027                         kfree(pr->performance->states);
1028                         goto end;
1029                 }
1030         }
1031
1032 end:
1033         acpi_os_free(buffer.pointer);
1034
1035         return_VALUE(result);
1036 }
1037
1038
1039 static int
1040 acpi_processor_get_performance_info (
1041         struct acpi_processor   *pr)
1042 {
1043         int                     result = 0;
1044         acpi_status             status = AE_OK;
1045         acpi_handle             handle = NULL;
1046
1047         ACPI_FUNCTION_TRACE("acpi_processor_get_performance_info");
1048
1049         if (!pr || !pr->performance || !pr->handle)
1050                 return_VALUE(-EINVAL);
1051
1052         acpi_processor_set_pdc(pr);
1053
1054         status = acpi_get_handle(pr->handle, "_PCT", &handle);
1055         if (ACPI_FAILURE(status)) {
1056                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1057                         "ACPI-based processor performance control unavailable\n"));
1058                 return_VALUE(-ENODEV);
1059         }
1060
1061         result = acpi_processor_get_performance_control(pr);
1062         if (result)
1063                 return_VALUE(result);
1064
1065         result = acpi_processor_get_performance_states(pr);
1066         if (result)
1067                 return_VALUE(result);
1068
1069         result = acpi_processor_get_platform_limit(pr);
1070         if (result)
1071                 return_VALUE(result);
1072
1073         return_VALUE(0);
1074 }
1075
1076
1077 #ifdef CONFIG_X86_ACPI_CPUFREQ_PROC_INTF
1078 /* /proc/acpi/processor/../performance interface (DEPRECATED) */
1079
1080 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file);
1081 static struct file_operations acpi_processor_perf_fops = {
1082         .open           = acpi_processor_perf_open_fs,
1083         .read           = seq_read,
1084         .llseek         = seq_lseek,
1085         .release        = single_release,
1086 };
1087
1088 static int acpi_processor_perf_seq_show(struct seq_file *seq, void *offset)
1089 {
1090         struct acpi_processor   *pr = (struct acpi_processor *)seq->private;
1091         int                     i = 0;
1092
1093         ACPI_FUNCTION_TRACE("acpi_processor_perf_seq_show");
1094
1095         if (!pr)
1096                 goto end;
1097
1098         if (!pr->performance) {
1099                 seq_puts(seq, "<not supported>\n");
1100                 goto end;
1101         }
1102
1103         seq_printf(seq, "state count:             %d\n"
1104                         "active state:            P%d\n",
1105                         pr->performance->state_count,
1106                         pr->performance->state);
1107
1108         seq_puts(seq, "states:\n");
1109         for (i = 0; i < pr->performance->state_count; i++)
1110                 seq_printf(seq, "   %cP%d:                  %d MHz, %d mW, %d uS\n",
1111                         (i == pr->performance->state?'*':' '), i,
1112                         (u32) pr->performance->states[i].core_frequency,
1113                         (u32) pr->performance->states[i].power,
1114                         (u32) pr->performance->states[i].transition_latency);
1115
1116 end:
1117         return 0;
1118 }
1119
1120 static int acpi_processor_perf_open_fs(struct inode *inode, struct file *file)
1121 {
1122         return single_open(file, acpi_processor_perf_seq_show,
1123                                                 PDE(inode)->data);
1124 }
1125
1126 static ssize_t
1127 acpi_processor_write_performance (
1128         struct file             *file,
1129         const char              __user *buffer,
1130         size_t                  count,
1131         loff_t                  *data)
1132 {
1133         int                     result = 0;
1134         struct seq_file         *m = (struct seq_file *) file->private_data;
1135         struct acpi_processor   *pr = (struct acpi_processor *) m->private;
1136         struct acpi_processor_performance *perf;
1137         char                    state_string[12] = {'\0'};
1138         unsigned int            new_state = 0;
1139         struct cpufreq_policy   policy;
1140
1141         ACPI_FUNCTION_TRACE("acpi_processor_write_performance");
1142
1143         if (!pr || (count > sizeof(state_string) - 1))
1144                 return_VALUE(-EINVAL);
1145
1146         perf = pr->performance;
1147         if (!perf)
1148                 return_VALUE(-EINVAL);
1149         
1150         if (copy_from_user(state_string, buffer, count))
1151                 return_VALUE(-EFAULT);
1152         
1153         state_string[count] = '\0';
1154         new_state = simple_strtoul(state_string, NULL, 0);
1155
1156         if (new_state >= perf->state_count)
1157                 return_VALUE(-EINVAL);
1158
1159         cpufreq_get_policy(&policy, pr->id);
1160
1161         policy.cpu = pr->id;
1162         policy.min = perf->states[new_state].core_frequency * 1000;
1163         policy.max = perf->states[new_state].core_frequency * 1000;
1164
1165         result = cpufreq_set_policy(&policy);
1166         if (result)
1167                 return_VALUE(result);
1168
1169         return_VALUE(count);
1170 }
1171
1172 static void
1173 acpi_cpufreq_add_file (
1174         struct acpi_processor *pr)
1175 {
1176         struct proc_dir_entry   *entry = NULL;
1177         struct acpi_device      *device = NULL;
1178
1179         ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
1180
1181         if (acpi_bus_get_device(pr->handle, &device))
1182                 return_VOID;
1183
1184         /* add file 'performance' [R/W] */
1185         entry = create_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
1186                   S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
1187         if (!entry)
1188                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1189                         "Unable to create '%s' fs entry\n",
1190                         ACPI_PROCESSOR_FILE_PERFORMANCE));
1191         else {
1192                 entry->proc_fops = &acpi_processor_perf_fops;
1193                 entry->proc_fops->write = acpi_processor_write_performance;
1194                 entry->data = acpi_driver_data(device);
1195                 entry->owner = THIS_MODULE;
1196         }
1197         return_VOID;
1198 }
1199
1200 static void
1201 acpi_cpufreq_remove_file (
1202         struct acpi_processor *pr)
1203 {
1204         struct acpi_device      *device = NULL;
1205
1206         ACPI_FUNCTION_TRACE("acpi_cpufreq_addfile");
1207
1208         if (acpi_bus_get_device(pr->handle, &device))
1209                 return_VOID;
1210
1211         /* remove file 'performance' */
1212         remove_proc_entry(ACPI_PROCESSOR_FILE_PERFORMANCE,
1213                   acpi_device_dir(device));
1214
1215         return_VOID;
1216 }
1217
1218 #else
1219 static void acpi_cpufreq_add_file (struct acpi_processor *pr) { return; }
1220 static void acpi_cpufreq_remove_file (struct acpi_processor *pr) { return; }
1221 #endif /* CONFIG_X86_ACPI_CPUFREQ_PROC_INTF */
1222
1223
1224 int 
1225 acpi_processor_register_performance (
1226         struct acpi_processor_performance * performance,
1227         unsigned int cpu)
1228 {
1229         struct acpi_processor *pr;
1230
1231         ACPI_FUNCTION_TRACE("acpi_processor_register_performance");
1232
1233         if (!acpi_processor_ppc_is_init)
1234                 return_VALUE(-EINVAL);
1235
1236         down(&performance_sem);
1237
1238         pr = processors[cpu];
1239         if (!pr) {
1240                 up(&performance_sem);
1241                 return_VALUE(-ENODEV);
1242         }
1243
1244         if (pr->performance) {
1245                 up(&performance_sem);
1246                 return_VALUE(-EBUSY);
1247         }
1248
1249         pr->performance = performance;
1250
1251         if (acpi_processor_get_performance_info(pr)) {
1252                 pr->performance = NULL;
1253                 up(&performance_sem);
1254                 return_VALUE(-EIO);
1255         }
1256
1257         acpi_cpufreq_add_file(pr);
1258
1259         up(&performance_sem);
1260         return_VALUE(0);
1261 }
1262 EXPORT_SYMBOL(acpi_processor_register_performance);
1263
1264
1265 void 
1266 acpi_processor_unregister_performance (
1267         struct acpi_processor_performance * performance,
1268         unsigned int cpu)
1269 {
1270         struct acpi_processor *pr;
1271
1272         ACPI_FUNCTION_TRACE("acpi_processor_unregister_performance");
1273
1274         if (!acpi_processor_ppc_is_init)
1275                 return_VOID;
1276
1277         down(&performance_sem);
1278
1279         pr = processors[cpu];
1280         if (!pr) {
1281                 up(&performance_sem);
1282                 return_VOID;
1283         }
1284
1285         kfree(pr->performance->states);
1286         pr->performance = NULL;
1287
1288         acpi_cpufreq_remove_file(pr);
1289
1290         up(&performance_sem);
1291
1292         return_VOID;
1293 }
1294 EXPORT_SYMBOL(acpi_processor_unregister_performance);
1295
1296
1297 /* for the rest of it, check arch/i386/kernel/cpu/cpufreq/acpi.c */
1298
1299 #else  /* !CONFIG_CPU_FREQ */
1300
1301 static void acpi_processor_ppc_init(void) { return; }
1302 static void acpi_processor_ppc_exit(void) { return; }
1303
1304 static int acpi_processor_ppc_has_changed(struct acpi_processor *pr) {
1305         static unsigned int printout = 1;
1306         if (printout) {
1307                 printk(KERN_WARNING "Warning: Processor Platform Limit event detected, but not handled.\n");
1308                 printk(KERN_WARNING "Consider compiling CPUfreq support into your kernel.\n");
1309                 printout = 0;
1310         }
1311         return 0;
1312 }
1313
1314 #endif /* CONFIG_CPU_FREQ */
1315
1316 /* --------------------------------------------------------------------------
1317                               Throttling Control
1318    -------------------------------------------------------------------------- */
1319
1320 static int
1321 acpi_processor_get_throttling (
1322         struct acpi_processor   *pr)
1323 {
1324         int                     state = 0;
1325         u32                     value = 0;
1326         u32                     duty_mask = 0;
1327         u32                     duty_value = 0;
1328
1329         ACPI_FUNCTION_TRACE("acpi_processor_get_throttling");
1330
1331         if (!pr)
1332                 return_VALUE(-EINVAL);
1333
1334         if (!pr->flags.throttling)
1335                 return_VALUE(-ENODEV);
1336
1337         pr->throttling.state = 0;
1338
1339         local_irq_disable();
1340
1341         duty_mask = pr->throttling.state_count - 1;
1342
1343         duty_mask <<= pr->throttling.duty_offset;
1344
1345         value = inl(pr->throttling.address);
1346
1347         /*
1348          * Compute the current throttling state when throttling is enabled
1349          * (bit 4 is on).
1350          */
1351         if (value & 0x10) {
1352                 duty_value = value & duty_mask;
1353                 duty_value >>= pr->throttling.duty_offset;
1354
1355                 if (duty_value)
1356                         state = pr->throttling.state_count-duty_value;
1357         }
1358
1359         pr->throttling.state = state;
1360
1361         local_irq_enable();
1362
1363         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1364                 "Throttling state is T%d (%d%% throttling applied)\n",
1365                 state, pr->throttling.states[state].performance));
1366
1367         return_VALUE(0);
1368 }
1369
1370
1371 static int
1372 acpi_processor_set_throttling (
1373         struct acpi_processor   *pr,
1374         int                     state)
1375 {
1376         u32                     value = 0;
1377         u32                     duty_mask = 0;
1378         u32                     duty_value = 0;
1379
1380         ACPI_FUNCTION_TRACE("acpi_processor_set_throttling");
1381
1382         if (!pr)
1383                 return_VALUE(-EINVAL);
1384
1385         if ((state < 0) || (state > (pr->throttling.state_count - 1)))
1386                 return_VALUE(-EINVAL);
1387
1388         if (!pr->flags.throttling)
1389                 return_VALUE(-ENODEV);
1390
1391         if (state == pr->throttling.state)
1392                 return_VALUE(0);
1393
1394         local_irq_disable();
1395
1396         /*
1397          * Calculate the duty_value and duty_mask.
1398          */
1399         if (state) {
1400                 duty_value = pr->throttling.state_count - state;
1401
1402                 duty_value <<= pr->throttling.duty_offset;
1403
1404                 /* Used to clear all duty_value bits */
1405                 duty_mask = pr->throttling.state_count - 1;
1406
1407                 duty_mask <<= acpi_fadt.duty_offset;
1408                 duty_mask = ~duty_mask;
1409         }
1410
1411         /*
1412          * Disable throttling by writing a 0 to bit 4.  Note that we must
1413          * turn it off before you can change the duty_value.
1414          */
1415         value = inl(pr->throttling.address);
1416         if (value & 0x10) {
1417                 value &= 0xFFFFFFEF;
1418                 outl(value, pr->throttling.address);
1419         }
1420
1421         /*
1422          * Write the new duty_value and then enable throttling.  Note
1423          * that a state value of 0 leaves throttling disabled.
1424          */
1425         if (state) {
1426                 value &= duty_mask;
1427                 value |= duty_value;
1428                 outl(value, pr->throttling.address);
1429
1430                 value |= 0x00000010;
1431                 outl(value, pr->throttling.address);
1432         }
1433
1434         pr->throttling.state = state;
1435
1436         local_irq_enable();
1437
1438         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1439                 "Throttling state set to T%d (%d%%)\n", state, 
1440                 (pr->throttling.states[state].performance?pr->throttling.states[state].performance/10:0)));
1441
1442         return_VALUE(0);
1443 }
1444
1445
1446 static int
1447 acpi_processor_get_throttling_info (
1448         struct acpi_processor   *pr)
1449 {
1450         int                     result = 0;
1451         int                     step = 0;
1452         int                     i = 0;
1453
1454         ACPI_FUNCTION_TRACE("acpi_processor_get_throttling_info");
1455
1456         ACPI_DEBUG_PRINT((ACPI_DB_INFO,
1457                 "pblk_address[0x%08x] duty_offset[%d] duty_width[%d]\n",
1458                 pr->throttling.address,
1459                 pr->throttling.duty_offset,
1460                 pr->throttling.duty_width));
1461
1462         if (!pr)
1463                 return_VALUE(-EINVAL);
1464
1465         /* TBD: Support ACPI 2.0 objects */
1466
1467         if (!pr->throttling.address) {
1468                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling register\n"));
1469                 return_VALUE(0);
1470         }
1471         else if (!pr->throttling.duty_width) {
1472                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No throttling states\n"));
1473                 return_VALUE(0);
1474         }
1475         /* TBD: Support duty_cycle values that span bit 4. */
1476         else if ((pr->throttling.duty_offset
1477                 + pr->throttling.duty_width) > 4) {
1478                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "duty_cycle spans bit 4\n"));
1479                 return_VALUE(0);
1480         }
1481
1482         /*
1483          * PIIX4 Errata: We don't support throttling on the original PIIX4.
1484          * This shouldn't be an issue as few (if any) mobile systems ever
1485          * used this part.
1486          */
1487         if (errata.piix4.throttle) {
1488                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1489                         "Throttling not supported on PIIX4 A- or B-step\n"));
1490                 return_VALUE(0);
1491         }
1492
1493         pr->throttling.state_count = 1 << acpi_fadt.duty_width;
1494
1495         /*
1496          * Compute state values. Note that throttling displays a linear power/
1497          * performance relationship (at 50% performance the CPU will consume
1498          * 50% power).  Values are in 1/10th of a percent to preserve accuracy.
1499          */
1500
1501         step = (1000 / pr->throttling.state_count);
1502
1503         for (i=0; i<pr->throttling.state_count; i++) {
1504                 pr->throttling.states[i].performance = step * i;
1505                 pr->throttling.states[i].power = step * i;
1506         }
1507
1508         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found %d throttling states\n", 
1509                 pr->throttling.state_count));
1510
1511         pr->flags.throttling = 1;
1512
1513         /*
1514          * Disable throttling (if enabled).  We'll let subsequent policy (e.g. 
1515          * thermal) decide to lower performance if it so chooses, but for now 
1516          * we'll crank up the speed.
1517          */
1518
1519         result = acpi_processor_get_throttling(pr);
1520         if (result)
1521                 goto end;
1522
1523         if (pr->throttling.state) {
1524                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Disabling throttling (was T%d)\n", 
1525                         pr->throttling.state));
1526                 result = acpi_processor_set_throttling(pr, 0);
1527                 if (result)
1528                         goto end;
1529         }
1530
1531 end:
1532         if (result)
1533                 pr->flags.throttling = 0;
1534
1535         return_VALUE(result);
1536 }
1537
1538
1539 /* --------------------------------------------------------------------------
1540                                  Limit Interface
1541    -------------------------------------------------------------------------- */
1542
1543 static int
1544 acpi_processor_apply_limit (
1545         struct acpi_processor*  pr)
1546 {
1547         int                     result = 0;
1548         u16                     px = 0;
1549         u16                     tx = 0;
1550
1551         ACPI_FUNCTION_TRACE("acpi_processor_apply_limit");
1552
1553         if (!pr)
1554                 return_VALUE(-EINVAL);
1555
1556         if (!pr->flags.limit)
1557                 return_VALUE(-ENODEV);
1558
1559         if (pr->flags.throttling) {
1560                 if (pr->limit.user.tx > tx)
1561                         tx = pr->limit.user.tx;
1562                 if (pr->limit.thermal.tx > tx)
1563                         tx = pr->limit.thermal.tx;
1564
1565                 result = acpi_processor_set_throttling(pr, tx);
1566                 if (result)
1567                         goto end;
1568         }
1569
1570         pr->limit.state.px = px;
1571         pr->limit.state.tx = tx;
1572
1573         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d] limit set to (P%d:T%d)\n",
1574                 pr->id,
1575                 pr->limit.state.px,
1576                 pr->limit.state.tx));
1577
1578 end:
1579         if (result)
1580                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Unable to set limit\n"));
1581
1582         return_VALUE(result);
1583 }
1584
1585
1586 #ifdef CONFIG_CPU_FREQ
1587
1588 /* If a passive cooling situation is detected, primarily CPUfreq is used, as it
1589  * offers (in most cases) voltage scaling in addition to frequency scaling, and
1590  * thus a cubic (instead of linear) reduction of energy. Also, we allow for
1591  * _any_ cpufreq driver and not only the acpi-cpufreq driver.
1592  */
1593
1594 static unsigned int cpufreq_thermal_reduction_pctg[NR_CPUS];
1595 static unsigned int acpi_thermal_cpufreq_is_init = 0;
1596
1597
1598 static int cpu_has_cpufreq(unsigned int cpu)
1599 {
1600         struct cpufreq_policy policy;
1601         if (!acpi_thermal_cpufreq_is_init)
1602                 return -ENODEV;
1603         if (!cpufreq_get_policy(&policy, cpu))
1604                 return -ENODEV;
1605         return 0;
1606 }
1607
1608
1609 static int acpi_thermal_cpufreq_increase(unsigned int cpu)
1610 {
1611         if (!cpu_has_cpufreq(cpu))
1612                 return -ENODEV;
1613
1614         if (cpufreq_thermal_reduction_pctg[cpu] < 60) {
1615                 cpufreq_thermal_reduction_pctg[cpu] += 20;
1616                 cpufreq_update_policy(cpu);
1617                 return 0;
1618         }
1619
1620         return -ERANGE;
1621 }
1622
1623
1624 static int acpi_thermal_cpufreq_decrease(unsigned int cpu)
1625 {
1626         if (!cpu_has_cpufreq(cpu))
1627                 return -ENODEV;
1628
1629         if (cpufreq_thermal_reduction_pctg[cpu] >= 20) {
1630                 cpufreq_thermal_reduction_pctg[cpu] -= 20;
1631                 cpufreq_update_policy(cpu);
1632                 return 0;
1633         }
1634
1635         return -ERANGE;
1636 }
1637
1638
1639 static int acpi_thermal_cpufreq_notifier(
1640         struct notifier_block *nb,
1641         unsigned long event,
1642         void *data)
1643 {
1644         struct cpufreq_policy *policy = data;
1645         unsigned long max_freq = 0;
1646
1647         if (event != CPUFREQ_ADJUST)
1648                 goto out;
1649
1650         max_freq = (policy->cpuinfo.max_freq * (100 - cpufreq_thermal_reduction_pctg[policy->cpu])) / 100;
1651
1652         cpufreq_verify_within_limits(policy, 0, max_freq);
1653
1654  out:
1655         return 0;
1656 }
1657
1658
1659 static struct notifier_block acpi_thermal_cpufreq_notifier_block = {
1660         .notifier_call = acpi_thermal_cpufreq_notifier,
1661 };
1662
1663
1664 static void acpi_thermal_cpufreq_init(void) {
1665         int i;
1666
1667         for (i=0; i<NR_CPUS; i++)
1668                 cpufreq_thermal_reduction_pctg[i] = 0;
1669
1670         i = cpufreq_register_notifier(&acpi_thermal_cpufreq_notifier_block, CPUFREQ_POLICY_NOTIFIER);
1671         if (!i)
1672                 acpi_thermal_cpufreq_is_init = 1;
1673 }
1674
1675 static void acpi_thermal_cpufreq_exit(void) {
1676         if (acpi_thermal_cpufreq_is_init)
1677                 cpufreq_unregister_notifier(&acpi_thermal_cpufreq_notifier_block, CPUFREQ_POLICY_NOTIFIER);
1678
1679         acpi_thermal_cpufreq_is_init = 0;
1680 }
1681
1682 #else /* ! CONFIG_CPU_FREQ */
1683
1684 static void acpi_thermal_cpufreq_init(void) { return; }
1685 static void acpi_thermal_cpufreq_exit(void) { return; }
1686 static int acpi_thermal_cpufreq_increase(unsigned int cpu) { return -ENODEV; }
1687 static int acpi_thermal_cpufreq_decrease(unsigned int cpu) { return -ENODEV; }
1688
1689
1690 #endif
1691
1692
1693 int
1694 acpi_processor_set_thermal_limit (
1695         acpi_handle             handle,
1696         int                     type)
1697 {
1698         int                     result = 0;
1699         struct acpi_processor   *pr = NULL;
1700         struct acpi_device      *device = NULL;
1701         int                     tx = 0;
1702
1703         ACPI_FUNCTION_TRACE("acpi_processor_set_thermal_limit");
1704
1705         if ((type < ACPI_PROCESSOR_LIMIT_NONE) 
1706                 || (type > ACPI_PROCESSOR_LIMIT_DECREMENT))
1707                 return_VALUE(-EINVAL);
1708
1709         result = acpi_bus_get_device(handle, &device);
1710         if (result)
1711                 return_VALUE(result);
1712
1713         pr = (struct acpi_processor *) acpi_driver_data(device);
1714         if (!pr)
1715                 return_VALUE(-ENODEV);
1716
1717         /* Thermal limits are always relative to the current Px/Tx state. */
1718         if (pr->flags.throttling)
1719                 pr->limit.thermal.tx = pr->throttling.state;
1720
1721         /*
1722          * Our default policy is to only use throttling at the lowest
1723          * performance state.
1724          */
1725
1726         tx = pr->limit.thermal.tx;
1727
1728         switch (type) {
1729
1730         case ACPI_PROCESSOR_LIMIT_NONE:
1731                 do {
1732                         result = acpi_thermal_cpufreq_decrease(pr->id);
1733                 } while (!result);
1734                 tx = 0;
1735                 break;
1736
1737         case ACPI_PROCESSOR_LIMIT_INCREMENT:
1738                 /* if going up: P-states first, T-states later */
1739
1740                 result = acpi_thermal_cpufreq_increase(pr->id);
1741                 if (!result)
1742                         goto end;
1743                 else if (result == -ERANGE)
1744                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1745                                         "At maximum performance state\n"));
1746
1747                 if (pr->flags.throttling) {
1748                         if (tx == (pr->throttling.state_count - 1))
1749                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1750                                         "At maximum throttling state\n"));
1751                         else
1752                                 tx++;
1753                 }
1754                 break;
1755
1756         case ACPI_PROCESSOR_LIMIT_DECREMENT:
1757                 /* if going down: T-states first, P-states later */
1758
1759                 if (pr->flags.throttling) {
1760                         if (tx == 0)
1761                                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1762                                         "At minimum throttling state\n"));
1763                         else {
1764                                 tx--;
1765                                 goto end;
1766                         }
1767                 }
1768
1769                 result = acpi_thermal_cpufreq_decrease(pr->id);
1770                 if (result == -ERANGE)
1771                         ACPI_DEBUG_PRINT((ACPI_DB_INFO, 
1772                                         "At minimum performance state\n"));
1773
1774                 break;
1775         }
1776
1777 end:
1778         if (pr->flags.throttling) {
1779                 pr->limit.thermal.px = 0;
1780                 pr->limit.thermal.tx = tx;
1781
1782                 result = acpi_processor_apply_limit(pr);
1783                 if (result)
1784                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
1785                                           "Unable to set thermal limit\n"));
1786
1787                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Thermal limit now (P%d:T%d)\n",
1788                                   pr->limit.thermal.px,
1789                                   pr->limit.thermal.tx));
1790         } else
1791                 result = 0;
1792
1793         return_VALUE(result);
1794 }
1795
1796
1797 static int
1798 acpi_processor_get_limit_info (
1799         struct acpi_processor   *pr)
1800 {
1801         ACPI_FUNCTION_TRACE("acpi_processor_get_limit_info");
1802
1803         if (!pr)
1804                 return_VALUE(-EINVAL);
1805
1806         if (pr->flags.throttling)
1807                 pr->flags.limit = 1;
1808
1809         return_VALUE(0);
1810 }
1811
1812
1813 /* --------------------------------------------------------------------------
1814                               FS Interface (/proc)
1815    -------------------------------------------------------------------------- */
1816
1817 struct proc_dir_entry           *acpi_processor_dir = NULL;
1818
1819 static int acpi_processor_info_seq_show(struct seq_file *seq, void *offset)
1820 {
1821         struct acpi_processor   *pr = (struct acpi_processor *)seq->private;
1822
1823         ACPI_FUNCTION_TRACE("acpi_processor_info_seq_show");
1824
1825         if (!pr)
1826                 goto end;
1827
1828         seq_printf(seq, "processor id:            %d\n"
1829                         "acpi id:                 %d\n"
1830                         "bus mastering control:   %s\n"
1831                         "power management:        %s\n"
1832                         "throttling control:      %s\n"
1833                         "limit interface:         %s\n",
1834                         pr->id,
1835                         pr->acpi_id,
1836                         pr->flags.bm_control ? "yes" : "no",
1837                         pr->flags.power ? "yes" : "no",
1838                         pr->flags.throttling ? "yes" : "no",
1839                         pr->flags.limit ? "yes" : "no");
1840
1841 end:
1842         return 0;
1843 }
1844
1845 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file)
1846 {
1847         return single_open(file, acpi_processor_info_seq_show,
1848                                                 PDE(inode)->data);
1849 }
1850
1851 static int acpi_processor_power_seq_show(struct seq_file *seq, void *offset)
1852 {
1853         struct acpi_processor   *pr = (struct acpi_processor *)seq->private;
1854         int                     i = 0;
1855
1856         ACPI_FUNCTION_TRACE("acpi_processor_power_seq_show");
1857
1858         if (!pr)
1859                 goto end;
1860
1861         seq_printf(seq, "active state:            C%d\n"
1862                         "default state:           C%d\n"
1863                         "bus master activity:     %08x\n",
1864                         pr->power.state,
1865                         pr->power.default_state,
1866                         pr->power.bm_activity);
1867
1868         seq_puts(seq, "states:\n");
1869
1870         for (i = 1; i < ACPI_C_STATE_COUNT; i++) {
1871                 seq_printf(seq, "   %cC%d:                  ", 
1872                         (i == pr->power.state?'*':' '), i);
1873
1874                 if (!pr->power.states[i].valid) {
1875                         seq_puts(seq, "<not supported>\n");
1876                         continue;
1877                 }
1878
1879                 if (pr->power.states[i].promotion.state)
1880                         seq_printf(seq, "promotion[C%d] ",
1881                                 pr->power.states[i].promotion.state);
1882                 else
1883                         seq_puts(seq, "promotion[--] ");
1884
1885                 if (pr->power.states[i].demotion.state)
1886                         seq_printf(seq, "demotion[C%d] ",
1887                                 pr->power.states[i].demotion.state);
1888                 else
1889                         seq_puts(seq, "demotion[--] ");
1890
1891                 seq_printf(seq, "latency[%03d] usage[%08d]\n",
1892                         pr->power.states[i].latency,
1893                         pr->power.states[i].usage);
1894         }
1895
1896 end:
1897         return 0;
1898 }
1899
1900 static int acpi_processor_power_open_fs(struct inode *inode, struct file *file)
1901 {
1902         return single_open(file, acpi_processor_power_seq_show,
1903                                                 PDE(inode)->data);
1904 }
1905
1906 static int acpi_processor_throttling_seq_show(struct seq_file *seq, void *offset)
1907 {
1908         struct acpi_processor   *pr = (struct acpi_processor *)seq->private;
1909         int                     i = 0;
1910         int                     result = 0;
1911
1912         ACPI_FUNCTION_TRACE("acpi_processor_throttling_seq_show");
1913
1914         if (!pr)
1915                 goto end;
1916
1917         if (!(pr->throttling.state_count > 0)) {
1918                 seq_puts(seq, "<not supported>\n");
1919                 goto end;
1920         }
1921
1922         result = acpi_processor_get_throttling(pr);
1923
1924         if (result) {
1925                 seq_puts(seq, "Could not determine current throttling state.\n");
1926                 goto end;
1927         }
1928
1929         seq_printf(seq, "state count:             %d\n"
1930                         "active state:            T%d\n",
1931                         pr->throttling.state_count,
1932                         pr->throttling.state);
1933
1934         seq_puts(seq, "states:\n");
1935         for (i = 0; i < pr->throttling.state_count; i++)
1936                 seq_printf(seq, "   %cT%d:                  %02d%%\n",
1937                         (i == pr->throttling.state?'*':' '), i,
1938                         (pr->throttling.states[i].performance?pr->throttling.states[i].performance/10:0));
1939
1940 end:
1941         return 0;
1942 }
1943
1944 static int acpi_processor_throttling_open_fs(struct inode *inode, struct file *file)
1945 {
1946         return single_open(file, acpi_processor_throttling_seq_show,
1947                                                 PDE(inode)->data);
1948 }
1949
1950 static ssize_t
1951 acpi_processor_write_throttling (
1952         struct file             *file,
1953         const char              __user *buffer,
1954         size_t                  count,
1955         loff_t                  *data)
1956 {
1957         int                     result = 0;
1958         struct seq_file         *m = (struct seq_file *)file->private_data;
1959         struct acpi_processor   *pr = (struct acpi_processor *)m->private;
1960         char                    state_string[12] = {'\0'};
1961
1962         ACPI_FUNCTION_TRACE("acpi_processor_write_throttling");
1963
1964         if (!pr || (count > sizeof(state_string) - 1))
1965                 return_VALUE(-EINVAL);
1966         
1967         if (copy_from_user(state_string, buffer, count))
1968                 return_VALUE(-EFAULT);
1969         
1970         state_string[count] = '\0';
1971         
1972         result = acpi_processor_set_throttling(pr, 
1973                 simple_strtoul(state_string, NULL, 0));
1974         if (result)
1975                 return_VALUE(result);
1976
1977         return_VALUE(count);
1978 }
1979
1980 static int acpi_processor_limit_seq_show(struct seq_file *seq, void *offset)
1981 {
1982         struct acpi_processor   *pr = (struct acpi_processor *)seq->private;
1983
1984         ACPI_FUNCTION_TRACE("acpi_processor_limit_seq_show");
1985
1986         if (!pr)
1987                 goto end;
1988
1989         if (!pr->flags.limit) {
1990                 seq_puts(seq, "<not supported>\n");
1991                 goto end;
1992         }
1993
1994         seq_printf(seq, "active limit:            P%d:T%d\n"
1995                         "user limit:              P%d:T%d\n"
1996                         "thermal limit:           P%d:T%d\n",
1997                         pr->limit.state.px, pr->limit.state.tx,
1998                         pr->limit.user.px, pr->limit.user.tx,
1999                         pr->limit.thermal.px, pr->limit.thermal.tx);
2000
2001 end:
2002         return 0;
2003 }
2004
2005 static int acpi_processor_limit_open_fs(struct inode *inode, struct file *file)
2006 {
2007         return single_open(file, acpi_processor_limit_seq_show,
2008                                                 PDE(inode)->data);
2009 }
2010
2011 static ssize_t
2012 acpi_processor_write_limit (
2013         struct file             *file,
2014         const char              __user *buffer,
2015         size_t                  count,
2016         loff_t                  *data)
2017 {
2018         int                     result = 0;
2019         struct seq_file         *m = (struct seq_file *)file->private_data;
2020         struct acpi_processor   *pr = (struct acpi_processor *)m->private;
2021         char                    limit_string[25] = {'\0'};
2022         int                     px = 0;
2023         int                     tx = 0;
2024
2025         ACPI_FUNCTION_TRACE("acpi_processor_write_limit");
2026
2027         if (!pr || (count > sizeof(limit_string) - 1)) {
2028                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid argument\n"));
2029                 return_VALUE(-EINVAL);
2030         }
2031         
2032         if (copy_from_user(limit_string, buffer, count)) {
2033                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data\n"));
2034                 return_VALUE(-EFAULT);
2035         }
2036         
2037         limit_string[count] = '\0';
2038
2039         if (sscanf(limit_string, "%d:%d", &px, &tx) != 2) {
2040                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid data format\n"));
2041                 return_VALUE(-EINVAL);
2042         }
2043
2044         if (pr->flags.throttling) {
2045                 if ((tx < 0) || (tx > (pr->throttling.state_count - 1))) {
2046                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid tx\n"));
2047                         return_VALUE(-EINVAL);
2048                 }
2049                 pr->limit.user.tx = tx;
2050         }
2051
2052         result = acpi_processor_apply_limit(pr);
2053
2054         return_VALUE(count);
2055 }
2056
2057
2058 static int
2059 acpi_processor_add_fs (
2060         struct acpi_device      *device)
2061 {
2062         struct proc_dir_entry   *entry = NULL;
2063
2064         ACPI_FUNCTION_TRACE("acpi_processor_add_fs");
2065
2066         if (!acpi_device_dir(device)) {
2067                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
2068                         acpi_processor_dir);
2069                 if (!acpi_device_dir(device))
2070                         return_VALUE(-ENODEV);
2071         }
2072         acpi_device_dir(device)->owner = THIS_MODULE;
2073
2074         /* 'info' [R] */
2075         entry = create_proc_entry(ACPI_PROCESSOR_FILE_INFO,
2076                 S_IRUGO, acpi_device_dir(device));
2077         if (!entry)
2078                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2079                         "Unable to create '%s' fs entry\n",
2080                         ACPI_PROCESSOR_FILE_INFO));
2081         else {
2082                 entry->proc_fops = &acpi_processor_info_fops;
2083                 entry->data = acpi_driver_data(device);
2084                 entry->owner = THIS_MODULE;
2085         }
2086
2087         /* 'power' [R] */
2088         entry = create_proc_entry(ACPI_PROCESSOR_FILE_POWER,
2089                 S_IRUGO, acpi_device_dir(device));
2090         if (!entry)
2091                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2092                         "Unable to create '%s' fs entry\n",
2093                         ACPI_PROCESSOR_FILE_POWER));
2094         else {
2095                 entry->proc_fops = &acpi_processor_power_fops;
2096                 entry->data = acpi_driver_data(device);
2097                 entry->owner = THIS_MODULE;
2098         }
2099
2100         /* 'throttling' [R/W] */
2101         entry = create_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
2102                 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
2103         if (!entry)
2104                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2105                         "Unable to create '%s' fs entry\n",
2106                         ACPI_PROCESSOR_FILE_THROTTLING));
2107         else {
2108                 entry->proc_fops = &acpi_processor_throttling_fops;
2109                 entry->proc_fops->write = acpi_processor_write_throttling;
2110                 entry->data = acpi_driver_data(device);
2111                 entry->owner = THIS_MODULE;
2112         }
2113
2114         /* 'limit' [R/W] */
2115         entry = create_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,
2116                 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
2117         if (!entry)
2118                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2119                         "Unable to create '%s' fs entry\n",
2120                         ACPI_PROCESSOR_FILE_LIMIT));
2121         else {
2122                 entry->proc_fops = &acpi_processor_limit_fops;
2123                 entry->proc_fops->write = acpi_processor_write_limit;
2124                 entry->data = acpi_driver_data(device);
2125                 entry->owner = THIS_MODULE;
2126         }
2127
2128         return_VALUE(0);
2129 }
2130
2131
2132 static int
2133 acpi_processor_remove_fs (
2134         struct acpi_device      *device)
2135 {
2136         ACPI_FUNCTION_TRACE("acpi_processor_remove_fs");
2137
2138         if (acpi_device_dir(device)) {
2139                 remove_proc_entry(ACPI_PROCESSOR_FILE_INFO,acpi_device_dir(device));
2140                 remove_proc_entry(ACPI_PROCESSOR_FILE_POWER,acpi_device_dir(device));
2141                 remove_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
2142                         acpi_device_dir(device));
2143                 remove_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,acpi_device_dir(device));
2144                 remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
2145                 acpi_device_dir(device) = NULL;
2146         }
2147
2148         return_VALUE(0);
2149 }
2150
2151 /* Use the acpiid in MADT to map cpus in case of SMP */
2152 #ifndef CONFIG_SMP
2153 #define convert_acpiid_to_cpu(acpi_id) (0xff)
2154 #else
2155
2156 #ifdef CONFIG_IA64
2157 #define arch_acpiid_to_apicid   ia64_acpiid_to_sapicid
2158 #define arch_cpu_to_apicid      ia64_cpu_to_sapicid
2159 #define ARCH_BAD_APICID         (0xffff)
2160 #else
2161 #define arch_acpiid_to_apicid   x86_acpiid_to_apicid
2162 #define arch_cpu_to_apicid      x86_cpu_to_apicid
2163 #define ARCH_BAD_APICID         (0xff)
2164 #endif
2165
2166 static u8 convert_acpiid_to_cpu(u8 acpi_id)
2167 {
2168         u16 apic_id;
2169         int i;
2170         
2171         apic_id = arch_acpiid_to_apicid[acpi_id];
2172         if (apic_id == ARCH_BAD_APICID)
2173                 return -1;
2174
2175         for (i = 0; i < NR_CPUS; i++) {
2176                 if (arch_cpu_to_apicid[i] == apic_id)
2177                         return i;
2178         }
2179         return -1;
2180 }
2181 #endif
2182
2183 /* --------------------------------------------------------------------------
2184                                  Driver Interface
2185    -------------------------------------------------------------------------- */
2186
2187 static int
2188 acpi_processor_get_info (
2189         struct acpi_processor   *pr)
2190 {
2191         acpi_status             status = 0;
2192         union acpi_object       object = {0};
2193         struct acpi_buffer      buffer = {sizeof(union acpi_object), &object};
2194         u8                      cpu_index;
2195         static int              cpu0_initialized;
2196
2197         ACPI_FUNCTION_TRACE("acpi_processor_get_info");
2198
2199         if (!pr)
2200                 return_VALUE(-EINVAL);
2201
2202         if (num_online_cpus() > 1)
2203                 errata.smp = TRUE;
2204
2205         acpi_processor_errata(pr);
2206
2207         /*
2208          * Check to see if we have bus mastering arbitration control.  This
2209          * is required for proper C3 usage (to maintain cache coherency).
2210          */
2211         if (acpi_fadt.V1_pm2_cnt_blk && acpi_fadt.pm2_cnt_len) {
2212                 pr->flags.bm_control = 1;
2213                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2214                         "Bus mastering arbitration control present\n"));
2215         }
2216         else
2217                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2218                         "No bus mastering arbitration control\n"));
2219
2220         /*
2221          * Evalute the processor object.  Note that it is common on SMP to
2222          * have the first (boot) processor with a valid PBLK address while
2223          * all others have a NULL address.
2224          */
2225         status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
2226         if (ACPI_FAILURE(status)) {
2227                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2228                         "Error evaluating processor object\n"));
2229                 return_VALUE(-ENODEV);
2230         }
2231
2232         /*
2233          * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
2234          *      >>> 'acpi_get_processor_id(acpi_id, &id)' in arch/xxx/acpi.c
2235          */
2236         pr->acpi_id = object.processor.proc_id;
2237
2238         cpu_index = convert_acpiid_to_cpu(pr->acpi_id);
2239
2240         if ( !cpu0_initialized && (cpu_index == 0xff)) {
2241                 /* Handle UP system running SMP kernel, with no LAPIC in MADT */
2242                 cpu_index = 0;
2243         } else if (cpu_index > num_online_cpus()) {
2244                 /*
2245                  *  Extra Processor objects may be enumerated on MP systems with
2246                  *  less than the max # of CPUs. They should be ignored.
2247                  */
2248                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
2249                         "Error getting cpuindex for acpiid 0x%x\n",
2250                         pr->acpi_id));
2251                 return_VALUE(-ENODEV);
2252         }
2253         cpu0_initialized = 1;
2254
2255         pr->id = cpu_index;
2256
2257         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id, 
2258                 pr->acpi_id));
2259
2260         if (!object.processor.pblk_address)
2261                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
2262         else if (object.processor.pblk_length != 6)
2263                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Invalid PBLK length [%d]\n",
2264                         object.processor.pblk_length));
2265         else {
2266                 pr->throttling.address = object.processor.pblk_address;
2267                 pr->throttling.duty_offset = acpi_fadt.duty_offset;
2268                 pr->throttling.duty_width = acpi_fadt.duty_width;
2269                 pr->power.states[ACPI_STATE_C2].address =
2270                         object.processor.pblk_address + 4;
2271                 pr->power.states[ACPI_STATE_C3].address =
2272                         object.processor.pblk_address + 5;
2273
2274                 /*
2275                  * We don't care about error returns - we just try to mark
2276                  * these reserved so that nobody else is confused into thinking
2277                  * that this region might be unused..
2278                  *
2279                  * (In particular, allocating the IO range for Cardbus)
2280                  */
2281                 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
2282         }
2283
2284         acpi_processor_get_power_info(pr);
2285 #ifdef CONFIG_CPU_FREQ
2286         acpi_processor_ppc_has_changed(pr);
2287 #endif
2288         acpi_processor_get_throttling_info(pr);
2289         acpi_processor_get_limit_info(pr);
2290
2291         return_VALUE(0);
2292 }
2293
2294
2295 static void
2296 acpi_processor_notify (
2297         acpi_handle             handle,
2298         u32                     event,
2299         void                    *data)
2300 {
2301         struct acpi_processor   *pr = (struct acpi_processor *) data;
2302         struct acpi_device      *device = NULL;
2303
2304         ACPI_FUNCTION_TRACE("acpi_processor_notify");
2305
2306         if (!pr)
2307                 return_VOID;
2308
2309         if (acpi_bus_get_device(pr->handle, &device))
2310                 return_VOID;
2311
2312         switch (event) {
2313         case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
2314                 acpi_processor_ppc_has_changed(pr);
2315                 acpi_bus_generate_event(device, event, 
2316                         pr->performance_platform_limit);
2317                 break;
2318         case ACPI_PROCESSOR_NOTIFY_POWER:
2319                 /* TBD */
2320                 acpi_bus_generate_event(device, event, 0);
2321                 break;
2322         default:
2323                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
2324                         "Unsupported event [0x%x]\n", event));
2325                 break;
2326         }
2327
2328         return_VOID;
2329 }
2330
2331
2332 static int
2333 acpi_processor_add (
2334         struct acpi_device      *device)
2335 {
2336         int                     result = 0;
2337         acpi_status             status = AE_OK;
2338         struct acpi_processor   *pr = NULL;
2339         u32                     i = 0;
2340
2341         ACPI_FUNCTION_TRACE("acpi_processor_add");
2342
2343         if (!device)
2344                 return_VALUE(-EINVAL);
2345
2346         pr = kmalloc(sizeof(struct acpi_processor), GFP_KERNEL);
2347         if (!pr)
2348                 return_VALUE(-ENOMEM);
2349         memset(pr, 0, sizeof(struct acpi_processor));
2350
2351         pr->handle = device->handle;
2352         strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
2353         strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
2354         acpi_driver_data(device) = pr;
2355
2356         result = acpi_processor_get_info(pr);
2357         if (result)
2358                 goto end;
2359
2360         result = acpi_processor_add_fs(device);
2361         if (result)
2362                 goto end;
2363
2364         status = acpi_install_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY, 
2365                 acpi_processor_notify, pr);
2366         if (ACPI_FAILURE(status)) {
2367                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
2368                         "Error installing notify handler\n"));
2369                 result = -ENODEV;
2370                 goto end;
2371         }
2372
2373         processors[pr->id] = pr;
2374
2375         /*
2376          * Install the idle handler if processor power management is supported.
2377          * Note that the default idle handler (default_idle) will be used on 
2378          * platforms that only support C1.
2379          */
2380         if ((pr->id == 0) && (pr->flags.power)) {
2381                 pm_idle_save = pm_idle;
2382                 pm_idle = acpi_processor_idle;
2383         }
2384         
2385         printk(KERN_INFO PREFIX "%s [%s] (supports",
2386                 acpi_device_name(device), acpi_device_bid(device));
2387         for (i=1; i<ACPI_C_STATE_COUNT; i++)
2388                 if (pr->power.states[i].valid)
2389                         printk(" C%d", i);
2390         if (pr->flags.throttling)
2391                 printk(", %d throttling states", pr->throttling.state_count);
2392         printk(")\n");
2393
2394 end:
2395         if (result) {
2396                 acpi_processor_remove_fs(device);
2397                 kfree(pr);
2398         }
2399
2400         return_VALUE(result);
2401 }
2402
2403
2404 static int
2405 acpi_processor_remove (
2406         struct acpi_device      *device,
2407         int                     type)
2408 {
2409         acpi_status             status = AE_OK;
2410         struct acpi_processor   *pr = NULL;
2411
2412         ACPI_FUNCTION_TRACE("acpi_processor_remove");
2413
2414         if (!device || !acpi_driver_data(device))
2415                 return_VALUE(-EINVAL);
2416
2417         pr = (struct acpi_processor *) acpi_driver_data(device);
2418
2419         /* Unregister the idle handler when processor #0 is removed. */
2420         if (pr->id == 0) {
2421                 pm_idle = pm_idle_save;
2422                 /*
2423                  * We are about to unload the current idle thread pm callback
2424                  * (pm_idle), Wait for all processors to update cached/local
2425                  * copies of pm_idle before proceeding.
2426                  */
2427                 synchronize_kernel();
2428         }
2429
2430         status = acpi_remove_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY, 
2431                 acpi_processor_notify);
2432         if (ACPI_FAILURE(status)) {
2433                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, 
2434                         "Error removing notify handler\n"));
2435         }
2436
2437         acpi_processor_remove_fs(device);
2438
2439         processors[pr->id] = NULL;
2440
2441         kfree(pr);
2442
2443         return_VALUE(0);
2444 }
2445
2446
2447 static int __init
2448 acpi_processor_init (void)
2449 {
2450         int                     result = 0;
2451
2452         ACPI_FUNCTION_TRACE("acpi_processor_init");
2453
2454         memset(&processors, 0, sizeof(processors));
2455         memset(&errata, 0, sizeof(errata));
2456
2457         acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2458         if (!acpi_processor_dir)
2459                 return_VALUE(-ENODEV);
2460         acpi_processor_dir->owner = THIS_MODULE;
2461
2462         result = acpi_bus_register_driver(&acpi_processor_driver);
2463         if (result < 0) {
2464                 remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2465                 return_VALUE(-ENODEV);
2466         }
2467
2468         acpi_thermal_cpufreq_init();
2469
2470         acpi_processor_ppc_init();
2471
2472         return_VALUE(0);
2473 }
2474
2475
2476 static void __exit
2477 acpi_processor_exit (void)
2478 {
2479         ACPI_FUNCTION_TRACE("acpi_processor_exit");
2480
2481         acpi_processor_ppc_exit();
2482
2483         acpi_thermal_cpufreq_exit();
2484
2485         acpi_bus_unregister_driver(&acpi_processor_driver);
2486
2487         remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
2488
2489         return_VOID;
2490 }
2491
2492
2493 module_init(acpi_processor_init);
2494 module_exit(acpi_processor_exit);
2495
2496 EXPORT_SYMBOL(acpi_processor_set_thermal_limit);