Merge branch 'sysfs' into release
[safe/jmp/linux-2.6] / drivers / acpi / processor_core.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  *  Copyright (C) 2004  Anil S Keshavamurthy <anil.s.keshavamurthy@intel.com>
8  *                      - Added processor hotplug support
9  *
10  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11  *
12  *  This program is free software; you can redistribute it and/or modify
13  *  it under the terms of the GNU General Public License as published by
14  *  the Free Software Foundation; either version 2 of the License, or (at
15  *  your option) any later version.
16  *
17  *  This program is distributed in the hope that it will be useful, but
18  *  WITHOUT ANY WARRANTY; without even the implied warranty of
19  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
20  *  General Public License for more details.
21  *
22  *  You should have received a copy of the GNU General Public License along
23  *  with this program; if not, write to the Free Software Foundation, Inc.,
24  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25  *
26  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27  *  TBD:
28  *      1. Make # power states dynamic.
29  *      2. Support duty_cycle values that span bit 4.
30  *      3. Optimize by having scheduler determine business instead of
31  *         having us try to calculate it here.
32  *      4. Need C1 timing -- must modify kernel (IRQ handler) to get this.
33  */
34
35 #include <linux/kernel.h>
36 #include <linux/module.h>
37 #include <linux/init.h>
38 #include <linux/types.h>
39 #include <linux/pci.h>
40 #include <linux/pm.h>
41 #include <linux/cpufreq.h>
42 #include <linux/cpu.h>
43 #include <linux/proc_fs.h>
44 #include <linux/seq_file.h>
45 #include <linux/dmi.h>
46 #include <linux/moduleparam.h>
47 #include <linux/cpuidle.h>
48
49 #include <asm/io.h>
50 #include <asm/system.h>
51 #include <asm/cpu.h>
52 #include <asm/delay.h>
53 #include <asm/uaccess.h>
54 #include <asm/processor.h>
55 #include <asm/smp.h>
56 #include <asm/acpi.h>
57
58 #include <acpi/acpi_bus.h>
59 #include <acpi/acpi_drivers.h>
60 #include <acpi/processor.h>
61
62 #define ACPI_PROCESSOR_CLASS            "processor"
63 #define ACPI_PROCESSOR_DEVICE_NAME      "Processor"
64 #define ACPI_PROCESSOR_FILE_INFO        "info"
65 #define ACPI_PROCESSOR_FILE_THROTTLING  "throttling"
66 #define ACPI_PROCESSOR_FILE_LIMIT       "limit"
67 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
68 #define ACPI_PROCESSOR_NOTIFY_POWER     0x81
69 #define ACPI_PROCESSOR_NOTIFY_THROTTLING        0x82
70
71 #define ACPI_PROCESSOR_LIMIT_USER       0
72 #define ACPI_PROCESSOR_LIMIT_THERMAL    1
73
74 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
75 ACPI_MODULE_NAME("processor_core");
76
77 MODULE_AUTHOR("Paul Diefenbaugh");
78 MODULE_DESCRIPTION("ACPI Processor Driver");
79 MODULE_LICENSE("GPL");
80
81 static int acpi_processor_add(struct acpi_device *device);
82 static int acpi_processor_start(struct acpi_device *device);
83 static int acpi_processor_remove(struct acpi_device *device, int type);
84 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file);
85 static void acpi_processor_notify(acpi_handle handle, u32 event, void *data);
86 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu);
87 static int acpi_processor_handle_eject(struct acpi_processor *pr);
88
89
90 static const struct acpi_device_id processor_device_ids[] = {
91         {ACPI_PROCESSOR_HID, 0},
92         {"", 0},
93 };
94 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
95
96 static struct acpi_driver acpi_processor_driver = {
97         .name = "processor",
98         .class = ACPI_PROCESSOR_CLASS,
99         .ids = processor_device_ids,
100         .ops = {
101                 .add = acpi_processor_add,
102                 .remove = acpi_processor_remove,
103                 .start = acpi_processor_start,
104                 .suspend = acpi_processor_suspend,
105                 .resume = acpi_processor_resume,
106                 },
107 };
108
109 #define INSTALL_NOTIFY_HANDLER          1
110 #define UNINSTALL_NOTIFY_HANDLER        2
111
112 static const struct file_operations acpi_processor_info_fops = {
113         .owner = THIS_MODULE,
114         .open = acpi_processor_info_open_fs,
115         .read = seq_read,
116         .llseek = seq_lseek,
117         .release = single_release,
118 };
119
120 DEFINE_PER_CPU(struct acpi_processor *, processors);
121 struct acpi_processor_errata errata __read_mostly;
122 static int set_no_mwait(const struct dmi_system_id *id)
123 {
124         printk(KERN_NOTICE PREFIX "%s detected - "
125                 "disabling mwait for CPU C-states\n", id->ident);
126         idle_nomwait = 1;
127         return 0;
128 }
129
130 static struct dmi_system_id __cpuinitdata processor_idle_dmi_table[] = {
131         {
132         set_no_mwait, "IFL91 board", {
133         DMI_MATCH(DMI_BIOS_VENDOR, "COMPAL"),
134         DMI_MATCH(DMI_SYS_VENDOR, "ZEPTO"),
135         DMI_MATCH(DMI_PRODUCT_VERSION, "3215W"),
136         DMI_MATCH(DMI_BOARD_NAME, "IFL91") }, NULL},
137         {
138         set_no_mwait, "Extensa 5220", {
139         DMI_MATCH(DMI_BIOS_VENDOR, "Phoenix Technologies LTD"),
140         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
141         DMI_MATCH(DMI_PRODUCT_VERSION, "0100"),
142         DMI_MATCH(DMI_BOARD_NAME, "Columbia") }, NULL},
143         {},
144 };
145
146 /* --------------------------------------------------------------------------
147                                 Errata Handling
148    -------------------------------------------------------------------------- */
149
150 static int acpi_processor_errata_piix4(struct pci_dev *dev)
151 {
152         u8 value1 = 0;
153         u8 value2 = 0;
154
155
156         if (!dev)
157                 return -EINVAL;
158
159         /*
160          * Note that 'dev' references the PIIX4 ACPI Controller.
161          */
162
163         switch (dev->revision) {
164         case 0:
165                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
166                 break;
167         case 1:
168                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
169                 break;
170         case 2:
171                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
172                 break;
173         case 3:
174                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
175                 break;
176         default:
177                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
178                 break;
179         }
180
181         switch (dev->revision) {
182
183         case 0:         /* PIIX4 A-step */
184         case 1:         /* PIIX4 B-step */
185                 /*
186                  * See specification changes #13 ("Manual Throttle Duty Cycle")
187                  * and #14 ("Enabling and Disabling Manual Throttle"), plus
188                  * erratum #5 ("STPCLK# Deassertion Time") from the January
189                  * 2002 PIIX4 specification update.  Applies to only older
190                  * PIIX4 models.
191                  */
192                 errata.piix4.throttle = 1;
193
194         case 2:         /* PIIX4E */
195         case 3:         /* PIIX4M */
196                 /*
197                  * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
198                  * Livelock") from the January 2002 PIIX4 specification update.
199                  * Applies to all PIIX4 models.
200                  */
201
202                 /*
203                  * BM-IDE
204                  * ------
205                  * Find the PIIX4 IDE Controller and get the Bus Master IDE
206                  * Status register address.  We'll use this later to read
207                  * each IDE controller's DMA status to make sure we catch all
208                  * DMA activity.
209                  */
210                 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
211                                      PCI_DEVICE_ID_INTEL_82371AB,
212                                      PCI_ANY_ID, PCI_ANY_ID, NULL);
213                 if (dev) {
214                         errata.piix4.bmisx = pci_resource_start(dev, 4);
215                         pci_dev_put(dev);
216                 }
217
218                 /*
219                  * Type-F DMA
220                  * ----------
221                  * Find the PIIX4 ISA Controller and read the Motherboard
222                  * DMA controller's status to see if Type-F (Fast) DMA mode
223                  * is enabled (bit 7) on either channel.  Note that we'll
224                  * disable C3 support if this is enabled, as some legacy
225                  * devices won't operate well if fast DMA is disabled.
226                  */
227                 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
228                                      PCI_DEVICE_ID_INTEL_82371AB_0,
229                                      PCI_ANY_ID, PCI_ANY_ID, NULL);
230                 if (dev) {
231                         pci_read_config_byte(dev, 0x76, &value1);
232                         pci_read_config_byte(dev, 0x77, &value2);
233                         if ((value1 & 0x80) || (value2 & 0x80))
234                                 errata.piix4.fdma = 1;
235                         pci_dev_put(dev);
236                 }
237
238                 break;
239         }
240
241         if (errata.piix4.bmisx)
242                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
243                                   "Bus master activity detection (BM-IDE) erratum enabled\n"));
244         if (errata.piix4.fdma)
245                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
246                                   "Type-F DMA livelock erratum (C3 disabled)\n"));
247
248         return 0;
249 }
250
251 static int acpi_processor_errata(struct acpi_processor *pr)
252 {
253         int result = 0;
254         struct pci_dev *dev = NULL;
255
256
257         if (!pr)
258                 return -EINVAL;
259
260         /*
261          * PIIX4
262          */
263         dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
264                              PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
265                              PCI_ANY_ID, NULL);
266         if (dev) {
267                 result = acpi_processor_errata_piix4(dev);
268                 pci_dev_put(dev);
269         }
270
271         return result;
272 }
273
274 /* --------------------------------------------------------------------------
275                               Common ACPI processor functions
276    -------------------------------------------------------------------------- */
277
278 /*
279  * _PDC is required for a BIOS-OS handshake for most of the newer
280  * ACPI processor features.
281  */
282 static int acpi_processor_set_pdc(struct acpi_processor *pr)
283 {
284         struct acpi_object_list *pdc_in = pr->pdc;
285         acpi_status status = AE_OK;
286
287
288         if (!pdc_in)
289                 return status;
290         if (idle_nomwait) {
291                 /*
292                  * If mwait is disabled for CPU C-states, the C2C3_FFH access
293                  * mode will be disabled in the parameter of _PDC object.
294                  * Of course C1_FFH access mode will also be disabled.
295                  */
296                 union acpi_object *obj;
297                 u32 *buffer = NULL;
298
299                 obj = pdc_in->pointer;
300                 buffer = (u32 *)(obj->buffer.pointer);
301                 buffer[2] &= ~(ACPI_PDC_C_C2C3_FFH | ACPI_PDC_C_C1_FFH);
302
303         }
304         status = acpi_evaluate_object(pr->handle, "_PDC", pdc_in, NULL);
305
306         if (ACPI_FAILURE(status))
307                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
308                     "Could not evaluate _PDC, using legacy perf. control...\n"));
309
310         return status;
311 }
312
313 /* --------------------------------------------------------------------------
314                               FS Interface (/proc)
315    -------------------------------------------------------------------------- */
316
317 static struct proc_dir_entry *acpi_processor_dir = NULL;
318
319 static int acpi_processor_info_seq_show(struct seq_file *seq, void *offset)
320 {
321         struct acpi_processor *pr = seq->private;
322
323
324         if (!pr)
325                 goto end;
326
327         seq_printf(seq, "processor id:            %d\n"
328                    "acpi id:                 %d\n"
329                    "bus mastering control:   %s\n"
330                    "power management:        %s\n"
331                    "throttling control:      %s\n"
332                    "limit interface:         %s\n",
333                    pr->id,
334                    pr->acpi_id,
335                    pr->flags.bm_control ? "yes" : "no",
336                    pr->flags.power ? "yes" : "no",
337                    pr->flags.throttling ? "yes" : "no",
338                    pr->flags.limit ? "yes" : "no");
339
340       end:
341         return 0;
342 }
343
344 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file)
345 {
346         return single_open(file, acpi_processor_info_seq_show,
347                            PDE(inode)->data);
348 }
349
350 static int acpi_processor_add_fs(struct acpi_device *device)
351 {
352         struct proc_dir_entry *entry = NULL;
353
354
355         if (!acpi_device_dir(device)) {
356                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
357                                                      acpi_processor_dir);
358                 if (!acpi_device_dir(device))
359                         return -ENODEV;
360         }
361         acpi_device_dir(device)->owner = THIS_MODULE;
362
363         /* 'info' [R] */
364         entry = proc_create_data(ACPI_PROCESSOR_FILE_INFO,
365                                  S_IRUGO, acpi_device_dir(device),
366                                  &acpi_processor_info_fops,
367                                  acpi_driver_data(device));
368         if (!entry)
369                 return -EIO;
370
371         /* 'throttling' [R/W] */
372         entry = proc_create_data(ACPI_PROCESSOR_FILE_THROTTLING,
373                                  S_IFREG | S_IRUGO | S_IWUSR,
374                                  acpi_device_dir(device),
375                                  &acpi_processor_throttling_fops,
376                                  acpi_driver_data(device));
377         if (!entry)
378                 return -EIO;
379
380         /* 'limit' [R/W] */
381         entry = proc_create_data(ACPI_PROCESSOR_FILE_LIMIT,
382                                  S_IFREG | S_IRUGO | S_IWUSR,
383                                  acpi_device_dir(device),
384                                  &acpi_processor_limit_fops,
385                                  acpi_driver_data(device));
386         if (!entry)
387                 return -EIO;
388         return 0;
389 }
390
391 static int acpi_processor_remove_fs(struct acpi_device *device)
392 {
393
394         if (acpi_device_dir(device)) {
395                 remove_proc_entry(ACPI_PROCESSOR_FILE_INFO,
396                                   acpi_device_dir(device));
397                 remove_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
398                                   acpi_device_dir(device));
399                 remove_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,
400                                   acpi_device_dir(device));
401                 remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
402                 acpi_device_dir(device) = NULL;
403         }
404
405         return 0;
406 }
407
408 /* Use the acpiid in MADT to map cpus in case of SMP */
409
410 #ifndef CONFIG_SMP
411 static int get_cpu_id(acpi_handle handle, u32 acpi_id) {return -1;}
412 #else
413
414 static struct acpi_table_madt *madt;
415
416 static int map_lapic_id(struct acpi_subtable_header *entry,
417                  u32 acpi_id, int *apic_id)
418 {
419         struct acpi_madt_local_apic *lapic =
420                 (struct acpi_madt_local_apic *)entry;
421         if ((lapic->lapic_flags & ACPI_MADT_ENABLED) &&
422             lapic->processor_id == acpi_id) {
423                 *apic_id = lapic->id;
424                 return 1;
425         }
426         return 0;
427 }
428
429 static int map_lsapic_id(struct acpi_subtable_header *entry,
430                   u32 acpi_id, int *apic_id)
431 {
432         struct acpi_madt_local_sapic *lsapic =
433                 (struct acpi_madt_local_sapic *)entry;
434         /* Only check enabled APICs*/
435         if (lsapic->lapic_flags & ACPI_MADT_ENABLED) {
436                 /* First check against id */
437                 if (lsapic->processor_id == acpi_id) {
438                         *apic_id = (lsapic->id << 8) | lsapic->eid;
439                         return 1;
440                 /* Check against optional uid */
441                 } else if (entry->length >= 16 &&
442                         lsapic->uid == acpi_id) {
443                         *apic_id = lsapic->uid;
444                         return 1;
445                 }
446         }
447         return 0;
448 }
449
450 static int map_madt_entry(u32 acpi_id)
451 {
452         unsigned long madt_end, entry;
453         int apic_id = -1;
454
455         if (!madt)
456                 return apic_id;
457
458         entry = (unsigned long)madt;
459         madt_end = entry + madt->header.length;
460
461         /* Parse all entries looking for a match. */
462
463         entry += sizeof(struct acpi_table_madt);
464         while (entry + sizeof(struct acpi_subtable_header) < madt_end) {
465                 struct acpi_subtable_header *header =
466                         (struct acpi_subtable_header *)entry;
467                 if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
468                         if (map_lapic_id(header, acpi_id, &apic_id))
469                                 break;
470                 } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
471                         if (map_lsapic_id(header, acpi_id, &apic_id))
472                                 break;
473                 }
474                 entry += header->length;
475         }
476         return apic_id;
477 }
478
479 static int map_mat_entry(acpi_handle handle, u32 acpi_id)
480 {
481         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
482         union acpi_object *obj;
483         struct acpi_subtable_header *header;
484         int apic_id = -1;
485
486         if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
487                 goto exit;
488
489         if (!buffer.length || !buffer.pointer)
490                 goto exit;
491
492         obj = buffer.pointer;
493         if (obj->type != ACPI_TYPE_BUFFER ||
494             obj->buffer.length < sizeof(struct acpi_subtable_header)) {
495                 goto exit;
496         }
497
498         header = (struct acpi_subtable_header *)obj->buffer.pointer;
499         if (header->type == ACPI_MADT_TYPE_LOCAL_APIC) {
500                 map_lapic_id(header, acpi_id, &apic_id);
501         } else if (header->type == ACPI_MADT_TYPE_LOCAL_SAPIC) {
502                 map_lsapic_id(header, acpi_id, &apic_id);
503         }
504
505 exit:
506         if (buffer.pointer)
507                 kfree(buffer.pointer);
508         return apic_id;
509 }
510
511 static int get_cpu_id(acpi_handle handle, u32 acpi_id)
512 {
513         int i;
514         int apic_id = -1;
515
516         apic_id = map_mat_entry(handle, acpi_id);
517         if (apic_id == -1)
518                 apic_id = map_madt_entry(acpi_id);
519         if (apic_id == -1)
520                 return apic_id;
521
522         for_each_possible_cpu(i) {
523                 if (cpu_physical_id(i) == apic_id)
524                         return i;
525         }
526         return -1;
527 }
528 #endif
529
530 /* --------------------------------------------------------------------------
531                                  Driver Interface
532    -------------------------------------------------------------------------- */
533
534 static int acpi_processor_get_info(struct acpi_processor *pr, unsigned has_uid)
535 {
536         acpi_status status = 0;
537         union acpi_object object = { 0 };
538         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
539         int cpu_index;
540         static int cpu0_initialized;
541
542
543         if (!pr)
544                 return -EINVAL;
545
546         if (num_online_cpus() > 1)
547                 errata.smp = TRUE;
548
549         acpi_processor_errata(pr);
550
551         /*
552          * Check to see if we have bus mastering arbitration control.  This
553          * is required for proper C3 usage (to maintain cache coherency).
554          */
555         if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
556                 pr->flags.bm_control = 1;
557                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
558                                   "Bus mastering arbitration control present\n"));
559         } else
560                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
561                                   "No bus mastering arbitration control\n"));
562
563         /* Check if it is a Device with HID and UID */
564         if (has_uid) {
565                 unsigned long long value;
566                 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
567                                                 NULL, &value);
568                 if (ACPI_FAILURE(status)) {
569                         printk(KERN_ERR PREFIX "Evaluating processor _UID\n");
570                         return -ENODEV;
571                 }
572                 pr->acpi_id = value;
573         } else {
574                 /*
575                 * Evalute the processor object.  Note that it is common on SMP to
576                 * have the first (boot) processor with a valid PBLK address while
577                 * all others have a NULL address.
578                 */
579                 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
580                 if (ACPI_FAILURE(status)) {
581                         printk(KERN_ERR PREFIX "Evaluating processor object\n");
582                         return -ENODEV;
583                 }
584
585                 /*
586                 * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
587                 *      >>> 'acpi_get_processor_id(acpi_id, &id)' in arch/xxx/acpi.c
588                 */
589                 pr->acpi_id = object.processor.proc_id;
590         }
591         cpu_index = get_cpu_id(pr->handle, pr->acpi_id);
592
593         /* Handle UP system running SMP kernel, with no LAPIC in MADT */
594         if (!cpu0_initialized && (cpu_index == -1) &&
595             (num_online_cpus() == 1)) {
596                 cpu_index = 0;
597         }
598
599         cpu0_initialized = 1;
600
601         pr->id = cpu_index;
602
603         /*
604          *  Extra Processor objects may be enumerated on MP systems with
605          *  less than the max # of CPUs. They should be ignored _iff
606          *  they are physically not present.
607          */
608         if (pr->id == -1) {
609                 if (ACPI_FAILURE
610                     (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
611                         return -ENODEV;
612                 }
613         }
614
615         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
616                           pr->acpi_id));
617
618         if (!object.processor.pblk_address)
619                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
620         else if (object.processor.pblk_length != 6)
621                 printk(KERN_ERR PREFIX "Invalid PBLK length [%d]\n",
622                             object.processor.pblk_length);
623         else {
624                 pr->throttling.address = object.processor.pblk_address;
625                 pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
626                 pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
627
628                 pr->pblk = object.processor.pblk_address;
629
630                 /*
631                  * We don't care about error returns - we just try to mark
632                  * these reserved so that nobody else is confused into thinking
633                  * that this region might be unused..
634                  *
635                  * (In particular, allocating the IO range for Cardbus)
636                  */
637                 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
638         }
639
640         /*
641          * If ACPI describes a slot number for this CPU, we can use it
642          * ensure we get the right value in the "physical id" field
643          * of /proc/cpuinfo
644          */
645         status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
646         if (ACPI_SUCCESS(status))
647                 arch_fix_phys_package_id(pr->id, object.integer.value);
648
649         return 0;
650 }
651
652 static DEFINE_PER_CPU(void *, processor_device_array);
653
654 static int __cpuinit acpi_processor_start(struct acpi_device *device)
655 {
656         int result = 0;
657         acpi_status status = AE_OK;
658         struct acpi_processor *pr;
659         struct sys_device *sysdev;
660
661         pr = acpi_driver_data(device);
662
663         result = acpi_processor_get_info(pr, device->flags.unique_id);
664         if (result) {
665                 /* Processor is physically not present */
666                 return 0;
667         }
668
669         BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
670
671         /*
672          * Buggy BIOS check
673          * ACPI id of processors can be reported wrongly by the BIOS.
674          * Don't trust it blindly
675          */
676         if (per_cpu(processor_device_array, pr->id) != NULL &&
677             per_cpu(processor_device_array, pr->id) != device) {
678                 printk(KERN_WARNING "BIOS reported wrong ACPI id "
679                         "for the processor\n");
680                 return -ENODEV;
681         }
682         per_cpu(processor_device_array, pr->id) = device;
683
684         per_cpu(processors, pr->id) = pr;
685
686         result = acpi_processor_add_fs(device);
687         if (result)
688                 goto end;
689
690         sysdev = get_cpu_sysdev(pr->id);
691         if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev"))
692                 return -EFAULT;
693
694         status = acpi_install_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
695                                              acpi_processor_notify, pr);
696
697         /* _PDC call should be done before doing anything else (if reqd.). */
698         arch_acpi_processor_init_pdc(pr);
699         acpi_processor_set_pdc(pr);
700 #ifdef CONFIG_CPU_FREQ
701         acpi_processor_ppc_has_changed(pr);
702 #endif
703         acpi_processor_get_throttling_info(pr);
704         acpi_processor_get_limit_info(pr);
705
706
707         acpi_processor_power_init(pr, device);
708
709         pr->cdev = thermal_cooling_device_register("Processor", device,
710                                                 &processor_cooling_ops);
711         if (IS_ERR(pr->cdev)) {
712                 result = PTR_ERR(pr->cdev);
713                 goto end;
714         }
715
716         dev_info(&device->dev, "registered as cooling_device%d\n",
717                  pr->cdev->id);
718
719         result = sysfs_create_link(&device->dev.kobj,
720                                    &pr->cdev->device.kobj,
721                                    "thermal_cooling");
722         if (result)
723                 printk(KERN_ERR PREFIX "Create sysfs link\n");
724         result = sysfs_create_link(&pr->cdev->device.kobj,
725                                    &device->dev.kobj,
726                                    "device");
727         if (result)
728                 printk(KERN_ERR PREFIX "Create sysfs link\n");
729
730         if (pr->flags.throttling) {
731                 printk(KERN_INFO PREFIX "%s [%s] (supports",
732                        acpi_device_name(device), acpi_device_bid(device));
733                 printk(" %d throttling states", pr->throttling.state_count);
734                 printk(")\n");
735         }
736
737       end:
738
739         return result;
740 }
741
742 static void acpi_processor_notify(acpi_handle handle, u32 event, void *data)
743 {
744         struct acpi_processor *pr = data;
745         struct acpi_device *device = NULL;
746         int saved;
747
748         if (!pr)
749                 return;
750
751         if (acpi_bus_get_device(pr->handle, &device))
752                 return;
753
754         switch (event) {
755         case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
756                 saved = pr->performance_platform_limit;
757                 acpi_processor_ppc_has_changed(pr);
758                 if (saved == pr->performance_platform_limit)
759                         break;
760                 acpi_bus_generate_proc_event(device, event,
761                                         pr->performance_platform_limit);
762                 acpi_bus_generate_netlink_event(device->pnp.device_class,
763                                                   dev_name(&device->dev), event,
764                                                   pr->performance_platform_limit);
765                 break;
766         case ACPI_PROCESSOR_NOTIFY_POWER:
767                 acpi_processor_cst_has_changed(pr);
768                 acpi_bus_generate_proc_event(device, event, 0);
769                 acpi_bus_generate_netlink_event(device->pnp.device_class,
770                                                   dev_name(&device->dev), event, 0);
771                 break;
772         case ACPI_PROCESSOR_NOTIFY_THROTTLING:
773                 acpi_processor_tstate_has_changed(pr);
774                 acpi_bus_generate_proc_event(device, event, 0);
775                 acpi_bus_generate_netlink_event(device->pnp.device_class,
776                                                   dev_name(&device->dev), event, 0);
777         default:
778                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
779                                   "Unsupported event [0x%x]\n", event));
780                 break;
781         }
782
783         return;
784 }
785
786 static int acpi_cpu_soft_notify(struct notifier_block *nfb,
787                 unsigned long action, void *hcpu)
788 {
789         unsigned int cpu = (unsigned long)hcpu;
790         struct acpi_processor *pr = per_cpu(processors, cpu);
791
792         if (action == CPU_ONLINE && pr) {
793                 acpi_processor_ppc_has_changed(pr);
794                 acpi_processor_cst_has_changed(pr);
795                 acpi_processor_tstate_has_changed(pr);
796         }
797         return NOTIFY_OK;
798 }
799
800 static struct notifier_block acpi_cpu_notifier =
801 {
802             .notifier_call = acpi_cpu_soft_notify,
803 };
804
805 static int acpi_processor_add(struct acpi_device *device)
806 {
807         struct acpi_processor *pr = NULL;
808
809
810         if (!device)
811                 return -EINVAL;
812
813         pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
814         if (!pr)
815                 return -ENOMEM;
816
817         pr->handle = device->handle;
818         strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
819         strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
820         device->driver_data = pr;
821
822         return 0;
823 }
824
825 static int acpi_processor_remove(struct acpi_device *device, int type)
826 {
827         acpi_status status = AE_OK;
828         struct acpi_processor *pr = NULL;
829
830
831         if (!device || !acpi_driver_data(device))
832                 return -EINVAL;
833
834         pr = acpi_driver_data(device);
835
836         if (pr->id >= nr_cpu_ids) {
837                 kfree(pr);
838                 return 0;
839         }
840
841         if (type == ACPI_BUS_REMOVAL_EJECT) {
842                 if (acpi_processor_handle_eject(pr))
843                         return -EINVAL;
844         }
845
846         acpi_processor_power_exit(pr, device);
847
848         status = acpi_remove_notify_handler(pr->handle, ACPI_DEVICE_NOTIFY,
849                                             acpi_processor_notify);
850
851         sysfs_remove_link(&device->dev.kobj, "sysdev");
852
853         acpi_processor_remove_fs(device);
854
855         if (pr->cdev) {
856                 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
857                 sysfs_remove_link(&pr->cdev->device.kobj, "device");
858                 thermal_cooling_device_unregister(pr->cdev);
859                 pr->cdev = NULL;
860         }
861
862         per_cpu(processors, pr->id) = NULL;
863         per_cpu(processor_device_array, pr->id) = NULL;
864         kfree(pr);
865
866         return 0;
867 }
868
869 #ifdef CONFIG_ACPI_HOTPLUG_CPU
870 /****************************************************************************
871  *      Acpi processor hotplug support                                      *
872  ****************************************************************************/
873
874 static int is_processor_present(acpi_handle handle)
875 {
876         acpi_status status;
877         unsigned long long sta = 0;
878
879
880         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
881
882         if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
883                 return 1;
884
885         /*
886          * _STA is mandatory for a processor that supports hot plug
887          */
888         if (status == AE_NOT_FOUND)
889                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
890                                 "Processor does not support hot plug\n"));
891         else
892                 ACPI_EXCEPTION((AE_INFO, status,
893                                 "Processor Device is not present"));
894         return 0;
895 }
896
897 static
898 int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
899 {
900         acpi_handle phandle;
901         struct acpi_device *pdev;
902         struct acpi_processor *pr;
903
904
905         if (acpi_get_parent(handle, &phandle)) {
906                 return -ENODEV;
907         }
908
909         if (acpi_bus_get_device(phandle, &pdev)) {
910                 return -ENODEV;
911         }
912
913         if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
914                 return -ENODEV;
915         }
916
917         acpi_bus_start(*device);
918
919         pr = acpi_driver_data(*device);
920         if (!pr)
921                 return -ENODEV;
922
923         if ((pr->id >= 0) && (pr->id < nr_cpu_ids)) {
924                 kobject_uevent(&(*device)->dev.kobj, KOBJ_ONLINE);
925         }
926         return 0;
927 }
928
929 static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
930                                                 u32 event, void *data)
931 {
932         struct acpi_processor *pr;
933         struct acpi_device *device = NULL;
934         int result;
935
936
937         switch (event) {
938         case ACPI_NOTIFY_BUS_CHECK:
939         case ACPI_NOTIFY_DEVICE_CHECK:
940                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
941                 "Processor driver received %s event\n",
942                        (event == ACPI_NOTIFY_BUS_CHECK) ?
943                        "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
944
945                 if (!is_processor_present(handle))
946                         break;
947
948                 if (acpi_bus_get_device(handle, &device)) {
949                         result = acpi_processor_device_add(handle, &device);
950                         if (result)
951                                 printk(KERN_ERR PREFIX
952                                             "Unable to add the device\n");
953                         break;
954                 }
955
956                 pr = acpi_driver_data(device);
957                 if (!pr) {
958                         printk(KERN_ERR PREFIX "Driver data is NULL\n");
959                         break;
960                 }
961
962                 if (pr->id >= 0 && (pr->id < nr_cpu_ids)) {
963                         kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
964                         break;
965                 }
966
967                 result = acpi_processor_start(device);
968                 if ((!result) && ((pr->id >= 0) && (pr->id < nr_cpu_ids))) {
969                         kobject_uevent(&device->dev.kobj, KOBJ_ONLINE);
970                 } else {
971                         printk(KERN_ERR PREFIX "Device [%s] failed to start\n",
972                                     acpi_device_bid(device));
973                 }
974                 break;
975         case ACPI_NOTIFY_EJECT_REQUEST:
976                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
977                                   "received ACPI_NOTIFY_EJECT_REQUEST\n"));
978
979                 if (acpi_bus_get_device(handle, &device)) {
980                         printk(KERN_ERR PREFIX
981                                     "Device don't exist, dropping EJECT\n");
982                         break;
983                 }
984                 pr = acpi_driver_data(device);
985                 if (!pr) {
986                         printk(KERN_ERR PREFIX
987                                     "Driver data is NULL, dropping EJECT\n");
988                         return;
989                 }
990
991                 if ((pr->id < nr_cpu_ids) && (cpu_present(pr->id)))
992                         kobject_uevent(&device->dev.kobj, KOBJ_OFFLINE);
993                 break;
994         default:
995                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
996                                   "Unsupported event [0x%x]\n", event));
997                 break;
998         }
999
1000         return;
1001 }
1002
1003 static acpi_status
1004 processor_walk_namespace_cb(acpi_handle handle,
1005                             u32 lvl, void *context, void **rv)
1006 {
1007         acpi_status status;
1008         int *action = context;
1009         acpi_object_type type = 0;
1010
1011         status = acpi_get_type(handle, &type);
1012         if (ACPI_FAILURE(status))
1013                 return (AE_OK);
1014
1015         if (type != ACPI_TYPE_PROCESSOR)
1016                 return (AE_OK);
1017
1018         switch (*action) {
1019         case INSTALL_NOTIFY_HANDLER:
1020                 acpi_install_notify_handler(handle,
1021                                             ACPI_SYSTEM_NOTIFY,
1022                                             acpi_processor_hotplug_notify,
1023                                             NULL);
1024                 break;
1025         case UNINSTALL_NOTIFY_HANDLER:
1026                 acpi_remove_notify_handler(handle,
1027                                            ACPI_SYSTEM_NOTIFY,
1028                                            acpi_processor_hotplug_notify);
1029                 break;
1030         default:
1031                 break;
1032         }
1033
1034         return (AE_OK);
1035 }
1036
1037 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
1038 {
1039
1040         if (!is_processor_present(handle)) {
1041                 return AE_ERROR;
1042         }
1043
1044         if (acpi_map_lsapic(handle, p_cpu))
1045                 return AE_ERROR;
1046
1047         if (arch_register_cpu(*p_cpu)) {
1048                 acpi_unmap_lsapic(*p_cpu);
1049                 return AE_ERROR;
1050         }
1051
1052         return AE_OK;
1053 }
1054
1055 static int acpi_processor_handle_eject(struct acpi_processor *pr)
1056 {
1057         if (cpu_online(pr->id))
1058                 cpu_down(pr->id);
1059
1060         arch_unregister_cpu(pr->id);
1061         acpi_unmap_lsapic(pr->id);
1062         return (0);
1063 }
1064 #else
1065 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
1066 {
1067         return AE_ERROR;
1068 }
1069 static int acpi_processor_handle_eject(struct acpi_processor *pr)
1070 {
1071         return (-EINVAL);
1072 }
1073 #endif
1074
1075 static
1076 void acpi_processor_install_hotplug_notify(void)
1077 {
1078 #ifdef CONFIG_ACPI_HOTPLUG_CPU
1079         int action = INSTALL_NOTIFY_HANDLER;
1080         acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
1081                             ACPI_ROOT_OBJECT,
1082                             ACPI_UINT32_MAX,
1083                             processor_walk_namespace_cb, &action, NULL);
1084 #endif
1085         register_hotcpu_notifier(&acpi_cpu_notifier);
1086 }
1087
1088 static
1089 void acpi_processor_uninstall_hotplug_notify(void)
1090 {
1091 #ifdef CONFIG_ACPI_HOTPLUG_CPU
1092         int action = UNINSTALL_NOTIFY_HANDLER;
1093         acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
1094                             ACPI_ROOT_OBJECT,
1095                             ACPI_UINT32_MAX,
1096                             processor_walk_namespace_cb, &action, NULL);
1097 #endif
1098         unregister_hotcpu_notifier(&acpi_cpu_notifier);
1099 }
1100
1101 /*
1102  * We keep the driver loaded even when ACPI is not running.
1103  * This is needed for the powernow-k8 driver, that works even without
1104  * ACPI, but needs symbols from this driver
1105  */
1106
1107 static int __init acpi_processor_init(void)
1108 {
1109         int result = 0;
1110
1111         memset(&errata, 0, sizeof(errata));
1112
1113 #ifdef CONFIG_SMP
1114         if (ACPI_FAILURE(acpi_get_table(ACPI_SIG_MADT, 0,
1115                                 (struct acpi_table_header **)&madt)))
1116                 madt = NULL;
1117 #endif
1118
1119         acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
1120         if (!acpi_processor_dir)
1121                 return -ENOMEM;
1122         acpi_processor_dir->owner = THIS_MODULE;
1123
1124         /*
1125          * Check whether the system is DMI table. If yes, OSPM
1126          * should not use mwait for CPU-states.
1127          */
1128         dmi_check_system(processor_idle_dmi_table);
1129         result = cpuidle_register_driver(&acpi_idle_driver);
1130         if (result < 0)
1131                 goto out_proc;
1132
1133         result = acpi_bus_register_driver(&acpi_processor_driver);
1134         if (result < 0)
1135                 goto out_cpuidle;
1136
1137         acpi_processor_install_hotplug_notify();
1138
1139         acpi_thermal_cpufreq_init();
1140
1141         acpi_processor_ppc_init();
1142
1143         acpi_processor_throttling_init();
1144
1145         return 0;
1146
1147 out_cpuidle:
1148         cpuidle_unregister_driver(&acpi_idle_driver);
1149
1150 out_proc:
1151         remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
1152
1153         return result;
1154 }
1155
1156 static void __exit acpi_processor_exit(void)
1157 {
1158         acpi_processor_ppc_exit();
1159
1160         acpi_thermal_cpufreq_exit();
1161
1162         acpi_processor_uninstall_hotplug_notify();
1163
1164         acpi_bus_unregister_driver(&acpi_processor_driver);
1165
1166         cpuidle_unregister_driver(&acpi_idle_driver);
1167
1168         remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
1169
1170         return;
1171 }
1172
1173 module_init(acpi_processor_init);
1174 module_exit(acpi_processor_exit);
1175
1176 EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
1177
1178 MODULE_ALIAS("processor");