Merge git://git.kernel.org/pub/scm/linux/kernel/git/davem/sparc-2.6
[pandora-kernel.git] / drivers / acpi / processor_driver.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 #ifdef CONFIG_ACPI_PROCFS
44 #include <linux/proc_fs.h>
45 #include <linux/seq_file.h>
46 #endif
47 #include <linux/dmi.h>
48 #include <linux/moduleparam.h>
49 #include <linux/cpuidle.h>
50 #include <linux/slab.h>
51
52 #include <asm/io.h>
53 #include <asm/system.h>
54 #include <asm/cpu.h>
55 #include <asm/delay.h>
56 #include <asm/uaccess.h>
57 #include <asm/processor.h>
58 #include <asm/smp.h>
59 #include <asm/acpi.h>
60
61 #include <acpi/acpi_bus.h>
62 #include <acpi/acpi_drivers.h>
63 #include <acpi/processor.h>
64
65 #define PREFIX "ACPI: "
66
67 #define ACPI_PROCESSOR_CLASS            "processor"
68 #define ACPI_PROCESSOR_DEVICE_NAME      "Processor"
69 #define ACPI_PROCESSOR_FILE_INFO        "info"
70 #define ACPI_PROCESSOR_FILE_THROTTLING  "throttling"
71 #define ACPI_PROCESSOR_FILE_LIMIT       "limit"
72 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
73 #define ACPI_PROCESSOR_NOTIFY_POWER     0x81
74 #define ACPI_PROCESSOR_NOTIFY_THROTTLING        0x82
75
76 #define ACPI_PROCESSOR_LIMIT_USER       0
77 #define ACPI_PROCESSOR_LIMIT_THERMAL    1
78
79 #define _COMPONENT              ACPI_PROCESSOR_COMPONENT
80 ACPI_MODULE_NAME("processor_driver");
81
82 MODULE_AUTHOR("Paul Diefenbaugh");
83 MODULE_DESCRIPTION("ACPI Processor Driver");
84 MODULE_LICENSE("GPL");
85
86 static int acpi_processor_add(struct acpi_device *device);
87 static int acpi_processor_remove(struct acpi_device *device, int type);
88 static void acpi_processor_notify(struct acpi_device *device, u32 event);
89 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu);
90 static int acpi_processor_handle_eject(struct acpi_processor *pr);
91
92
93 static const struct acpi_device_id processor_device_ids[] = {
94         {ACPI_PROCESSOR_OBJECT_HID, 0},
95         {"ACPI0007", 0},
96         {"", 0},
97 };
98 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
99
100 static struct acpi_driver acpi_processor_driver = {
101         .name = "processor",
102         .class = ACPI_PROCESSOR_CLASS,
103         .ids = processor_device_ids,
104         .ops = {
105                 .add = acpi_processor_add,
106                 .remove = acpi_processor_remove,
107                 .suspend = acpi_processor_suspend,
108                 .resume = acpi_processor_resume,
109                 .notify = acpi_processor_notify,
110                 },
111 };
112
113 #define INSTALL_NOTIFY_HANDLER          1
114 #define UNINSTALL_NOTIFY_HANDLER        2
115
116 DEFINE_PER_CPU(struct acpi_processor *, processors);
117 EXPORT_PER_CPU_SYMBOL(processors);
118
119 struct acpi_processor_errata errata __read_mostly;
120
121 /* --------------------------------------------------------------------------
122                                 Errata Handling
123    -------------------------------------------------------------------------- */
124
125 static int acpi_processor_errata_piix4(struct pci_dev *dev)
126 {
127         u8 value1 = 0;
128         u8 value2 = 0;
129
130
131         if (!dev)
132                 return -EINVAL;
133
134         /*
135          * Note that 'dev' references the PIIX4 ACPI Controller.
136          */
137
138         switch (dev->revision) {
139         case 0:
140                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
141                 break;
142         case 1:
143                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
144                 break;
145         case 2:
146                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
147                 break;
148         case 3:
149                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
150                 break;
151         default:
152                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
153                 break;
154         }
155
156         switch (dev->revision) {
157
158         case 0:         /* PIIX4 A-step */
159         case 1:         /* PIIX4 B-step */
160                 /*
161                  * See specification changes #13 ("Manual Throttle Duty Cycle")
162                  * and #14 ("Enabling and Disabling Manual Throttle"), plus
163                  * erratum #5 ("STPCLK# Deassertion Time") from the January
164                  * 2002 PIIX4 specification update.  Applies to only older
165                  * PIIX4 models.
166                  */
167                 errata.piix4.throttle = 1;
168
169         case 2:         /* PIIX4E */
170         case 3:         /* PIIX4M */
171                 /*
172                  * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
173                  * Livelock") from the January 2002 PIIX4 specification update.
174                  * Applies to all PIIX4 models.
175                  */
176
177                 /*
178                  * BM-IDE
179                  * ------
180                  * Find the PIIX4 IDE Controller and get the Bus Master IDE
181                  * Status register address.  We'll use this later to read
182                  * each IDE controller's DMA status to make sure we catch all
183                  * DMA activity.
184                  */
185                 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
186                                      PCI_DEVICE_ID_INTEL_82371AB,
187                                      PCI_ANY_ID, PCI_ANY_ID, NULL);
188                 if (dev) {
189                         errata.piix4.bmisx = pci_resource_start(dev, 4);
190                         pci_dev_put(dev);
191                 }
192
193                 /*
194                  * Type-F DMA
195                  * ----------
196                  * Find the PIIX4 ISA Controller and read the Motherboard
197                  * DMA controller's status to see if Type-F (Fast) DMA mode
198                  * is enabled (bit 7) on either channel.  Note that we'll
199                  * disable C3 support if this is enabled, as some legacy
200                  * devices won't operate well if fast DMA is disabled.
201                  */
202                 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
203                                      PCI_DEVICE_ID_INTEL_82371AB_0,
204                                      PCI_ANY_ID, PCI_ANY_ID, NULL);
205                 if (dev) {
206                         pci_read_config_byte(dev, 0x76, &value1);
207                         pci_read_config_byte(dev, 0x77, &value2);
208                         if ((value1 & 0x80) || (value2 & 0x80))
209                                 errata.piix4.fdma = 1;
210                         pci_dev_put(dev);
211                 }
212
213                 break;
214         }
215
216         if (errata.piix4.bmisx)
217                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
218                                   "Bus master activity detection (BM-IDE) erratum enabled\n"));
219         if (errata.piix4.fdma)
220                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
221                                   "Type-F DMA livelock erratum (C3 disabled)\n"));
222
223         return 0;
224 }
225
226 static int acpi_processor_errata(struct acpi_processor *pr)
227 {
228         int result = 0;
229         struct pci_dev *dev = NULL;
230
231
232         if (!pr)
233                 return -EINVAL;
234
235         /*
236          * PIIX4
237          */
238         dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
239                              PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
240                              PCI_ANY_ID, NULL);
241         if (dev) {
242                 result = acpi_processor_errata_piix4(dev);
243                 pci_dev_put(dev);
244         }
245
246         return result;
247 }
248
249 #ifdef CONFIG_ACPI_PROCFS
250 static struct proc_dir_entry *acpi_processor_dir = NULL;
251
252 static int __cpuinit acpi_processor_add_fs(struct acpi_device *device)
253 {
254         struct proc_dir_entry *entry = NULL;
255
256
257         if (!acpi_device_dir(device)) {
258                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
259                                                      acpi_processor_dir);
260                 if (!acpi_device_dir(device))
261                         return -ENODEV;
262         }
263
264         /* 'throttling' [R/W] */
265         entry = proc_create_data(ACPI_PROCESSOR_FILE_THROTTLING,
266                                  S_IFREG | S_IRUGO | S_IWUSR,
267                                  acpi_device_dir(device),
268                                  &acpi_processor_throttling_fops,
269                                  acpi_driver_data(device));
270         if (!entry)
271                 return -EIO;
272         return 0;
273 }
274 static int acpi_processor_remove_fs(struct acpi_device *device)
275 {
276
277         if (acpi_device_dir(device)) {
278                 remove_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
279                                   acpi_device_dir(device));
280                 remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
281                 acpi_device_dir(device) = NULL;
282         }
283
284         return 0;
285 }
286 #else
287 static inline int acpi_processor_add_fs(struct acpi_device *device)
288 {
289         return 0;
290 }
291 static inline int acpi_processor_remove_fs(struct acpi_device *device)
292 {
293         return 0;
294 }
295 #endif
296 /* --------------------------------------------------------------------------
297                                  Driver Interface
298    -------------------------------------------------------------------------- */
299
300 static int acpi_processor_get_info(struct acpi_device *device)
301 {
302         acpi_status status = 0;
303         union acpi_object object = { 0 };
304         struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
305         struct acpi_processor *pr;
306         int cpu_index, device_declaration = 0;
307         static int cpu0_initialized;
308
309         pr = acpi_driver_data(device);
310         if (!pr)
311                 return -EINVAL;
312
313         if (num_online_cpus() > 1)
314                 errata.smp = TRUE;
315
316         acpi_processor_errata(pr);
317
318         /*
319          * Check to see if we have bus mastering arbitration control.  This
320          * is required for proper C3 usage (to maintain cache coherency).
321          */
322         if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
323                 pr->flags.bm_control = 1;
324                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
325                                   "Bus mastering arbitration control present\n"));
326         } else
327                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
328                                   "No bus mastering arbitration control\n"));
329
330         if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
331                 /* Declared with "Processor" statement; match ProcessorID */
332                 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
333                 if (ACPI_FAILURE(status)) {
334                         printk(KERN_ERR PREFIX "Evaluating processor object\n");
335                         return -ENODEV;
336                 }
337
338                 /*
339                  * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
340                  *      >>> 'acpi_get_processor_id(acpi_id, &id)' in
341                  *      arch/xxx/acpi.c
342                  */
343                 pr->acpi_id = object.processor.proc_id;
344         } else {
345                 /*
346                  * Declared with "Device" statement; match _UID.
347                  * Note that we don't handle string _UIDs yet.
348                  */
349                 unsigned long long value;
350                 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
351                                                 NULL, &value);
352                 if (ACPI_FAILURE(status)) {
353                         printk(KERN_ERR PREFIX
354                             "Evaluating processor _UID [%#x]\n", status);
355                         return -ENODEV;
356                 }
357                 device_declaration = 1;
358                 pr->acpi_id = value;
359         }
360         cpu_index = acpi_get_cpuid(pr->handle, device_declaration, pr->acpi_id);
361
362         /* Handle UP system running SMP kernel, with no LAPIC in MADT */
363         if (!cpu0_initialized && (cpu_index == -1) &&
364             (num_online_cpus() == 1)) {
365                 cpu_index = 0;
366         }
367
368         cpu0_initialized = 1;
369
370         pr->id = cpu_index;
371
372         /*
373          *  Extra Processor objects may be enumerated on MP systems with
374          *  less than the max # of CPUs. They should be ignored _iff
375          *  they are physically not present.
376          */
377         if (pr->id == -1) {
378                 if (ACPI_FAILURE
379                     (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
380                         return -ENODEV;
381                 }
382         }
383         /*
384          * On some boxes several processors use the same processor bus id.
385          * But they are located in different scope. For example:
386          * \_SB.SCK0.CPU0
387          * \_SB.SCK1.CPU0
388          * Rename the processor device bus id. And the new bus id will be
389          * generated as the following format:
390          * CPU+CPU ID.
391          */
392         sprintf(acpi_device_bid(device), "CPU%X", pr->id);
393         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
394                           pr->acpi_id));
395
396         if (!object.processor.pblk_address)
397                 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
398         else if (object.processor.pblk_length != 6)
399                 printk(KERN_ERR PREFIX "Invalid PBLK length [%d]\n",
400                             object.processor.pblk_length);
401         else {
402                 pr->throttling.address = object.processor.pblk_address;
403                 pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
404                 pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
405
406                 pr->pblk = object.processor.pblk_address;
407
408                 /*
409                  * We don't care about error returns - we just try to mark
410                  * these reserved so that nobody else is confused into thinking
411                  * that this region might be unused..
412                  *
413                  * (In particular, allocating the IO range for Cardbus)
414                  */
415                 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
416         }
417
418         /*
419          * If ACPI describes a slot number for this CPU, we can use it
420          * ensure we get the right value in the "physical id" field
421          * of /proc/cpuinfo
422          */
423         status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
424         if (ACPI_SUCCESS(status))
425                 arch_fix_phys_package_id(pr->id, object.integer.value);
426
427         return 0;
428 }
429
430 static DEFINE_PER_CPU(void *, processor_device_array);
431
432 static void acpi_processor_notify(struct acpi_device *device, u32 event)
433 {
434         struct acpi_processor *pr = acpi_driver_data(device);
435         int saved;
436
437         if (!pr)
438                 return;
439
440         switch (event) {
441         case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
442                 saved = pr->performance_platform_limit;
443                 acpi_processor_ppc_has_changed(pr, 1);
444                 if (saved == pr->performance_platform_limit)
445                         break;
446                 acpi_bus_generate_proc_event(device, event,
447                                         pr->performance_platform_limit);
448                 acpi_bus_generate_netlink_event(device->pnp.device_class,
449                                                   dev_name(&device->dev), event,
450                                                   pr->performance_platform_limit);
451                 break;
452         case ACPI_PROCESSOR_NOTIFY_POWER:
453                 acpi_processor_cst_has_changed(pr);
454                 acpi_bus_generate_proc_event(device, event, 0);
455                 acpi_bus_generate_netlink_event(device->pnp.device_class,
456                                                   dev_name(&device->dev), event, 0);
457                 break;
458         case ACPI_PROCESSOR_NOTIFY_THROTTLING:
459                 acpi_processor_tstate_has_changed(pr);
460                 acpi_bus_generate_proc_event(device, event, 0);
461                 acpi_bus_generate_netlink_event(device->pnp.device_class,
462                                                   dev_name(&device->dev), event, 0);
463         default:
464                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
465                                   "Unsupported event [0x%x]\n", event));
466                 break;
467         }
468
469         return;
470 }
471
472 static int acpi_cpu_soft_notify(struct notifier_block *nfb,
473                 unsigned long action, void *hcpu)
474 {
475         unsigned int cpu = (unsigned long)hcpu;
476         struct acpi_processor *pr = per_cpu(processors, cpu);
477
478         if (action == CPU_ONLINE && pr) {
479                 acpi_processor_ppc_has_changed(pr, 0);
480                 acpi_processor_cst_has_changed(pr);
481                 acpi_processor_tstate_has_changed(pr);
482         }
483         return NOTIFY_OK;
484 }
485
486 static struct notifier_block acpi_cpu_notifier =
487 {
488             .notifier_call = acpi_cpu_soft_notify,
489 };
490
491 static int __cpuinit acpi_processor_add(struct acpi_device *device)
492 {
493         struct acpi_processor *pr = NULL;
494         int result = 0;
495         struct sys_device *sysdev;
496
497         pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
498         if (!pr)
499                 return -ENOMEM;
500
501         if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
502                 kfree(pr);
503                 return -ENOMEM;
504         }
505
506         pr->handle = device->handle;
507         strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
508         strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
509         device->driver_data = pr;
510
511         result = acpi_processor_get_info(device);
512         if (result) {
513                 /* Processor is physically not present */
514                 return 0;
515         }
516
517 #ifdef CONFIG_SMP
518         if (pr->id >= setup_max_cpus && pr->id != 0)
519                 return 0;
520 #endif
521
522         BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
523
524         /*
525          * Buggy BIOS check
526          * ACPI id of processors can be reported wrongly by the BIOS.
527          * Don't trust it blindly
528          */
529         if (per_cpu(processor_device_array, pr->id) != NULL &&
530             per_cpu(processor_device_array, pr->id) != device) {
531                 printk(KERN_WARNING "BIOS reported wrong ACPI id "
532                         "for the processor\n");
533                 result = -ENODEV;
534                 goto err_free_cpumask;
535         }
536         per_cpu(processor_device_array, pr->id) = device;
537
538         per_cpu(processors, pr->id) = pr;
539
540         result = acpi_processor_add_fs(device);
541         if (result)
542                 goto err_free_cpumask;
543
544         sysdev = get_cpu_sysdev(pr->id);
545         if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev")) {
546                 result = -EFAULT;
547                 goto err_remove_fs;
548         }
549
550 #ifdef CONFIG_CPU_FREQ
551         acpi_processor_ppc_has_changed(pr, 0);
552 #endif
553         acpi_processor_get_throttling_info(pr);
554         acpi_processor_get_limit_info(pr);
555
556
557         if (cpuidle_get_driver() == &acpi_idle_driver)
558                 acpi_processor_power_init(pr, device);
559
560         pr->cdev = thermal_cooling_device_register("Processor", device,
561                                                 &processor_cooling_ops);
562         if (IS_ERR(pr->cdev)) {
563                 result = PTR_ERR(pr->cdev);
564                 goto err_power_exit;
565         }
566
567         dev_dbg(&device->dev, "registered as cooling_device%d\n",
568                  pr->cdev->id);
569
570         result = sysfs_create_link(&device->dev.kobj,
571                                    &pr->cdev->device.kobj,
572                                    "thermal_cooling");
573         if (result) {
574                 printk(KERN_ERR PREFIX "Create sysfs link\n");
575                 goto err_thermal_unregister;
576         }
577         result = sysfs_create_link(&pr->cdev->device.kobj,
578                                    &device->dev.kobj,
579                                    "device");
580         if (result) {
581                 printk(KERN_ERR PREFIX "Create sysfs link\n");
582                 goto err_remove_sysfs;
583         }
584
585         return 0;
586
587 err_remove_sysfs:
588         sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
589 err_thermal_unregister:
590         thermal_cooling_device_unregister(pr->cdev);
591 err_power_exit:
592         acpi_processor_power_exit(pr, device);
593 err_remove_fs:
594         acpi_processor_remove_fs(device);
595 err_free_cpumask:
596         free_cpumask_var(pr->throttling.shared_cpu_map);
597
598         return result;
599 }
600
601 static int acpi_processor_remove(struct acpi_device *device, int type)
602 {
603         struct acpi_processor *pr = NULL;
604
605
606         if (!device || !acpi_driver_data(device))
607                 return -EINVAL;
608
609         pr = acpi_driver_data(device);
610
611         if (pr->id >= nr_cpu_ids)
612                 goto free;
613
614         if (type == ACPI_BUS_REMOVAL_EJECT) {
615                 if (acpi_processor_handle_eject(pr))
616                         return -EINVAL;
617         }
618
619         acpi_processor_power_exit(pr, device);
620
621         sysfs_remove_link(&device->dev.kobj, "sysdev");
622
623         acpi_processor_remove_fs(device);
624
625         if (pr->cdev) {
626                 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
627                 sysfs_remove_link(&pr->cdev->device.kobj, "device");
628                 thermal_cooling_device_unregister(pr->cdev);
629                 pr->cdev = NULL;
630         }
631
632         per_cpu(processors, pr->id) = NULL;
633         per_cpu(processor_device_array, pr->id) = NULL;
634
635 free:
636         free_cpumask_var(pr->throttling.shared_cpu_map);
637         kfree(pr);
638
639         return 0;
640 }
641
642 #ifdef CONFIG_ACPI_HOTPLUG_CPU
643 /****************************************************************************
644  *      Acpi processor hotplug support                                      *
645  ****************************************************************************/
646
647 static int is_processor_present(acpi_handle handle)
648 {
649         acpi_status status;
650         unsigned long long sta = 0;
651
652
653         status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
654
655         if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
656                 return 1;
657
658         /*
659          * _STA is mandatory for a processor that supports hot plug
660          */
661         if (status == AE_NOT_FOUND)
662                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
663                                 "Processor does not support hot plug\n"));
664         else
665                 ACPI_EXCEPTION((AE_INFO, status,
666                                 "Processor Device is not present"));
667         return 0;
668 }
669
670 static
671 int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
672 {
673         acpi_handle phandle;
674         struct acpi_device *pdev;
675
676
677         if (acpi_get_parent(handle, &phandle)) {
678                 return -ENODEV;
679         }
680
681         if (acpi_bus_get_device(phandle, &pdev)) {
682                 return -ENODEV;
683         }
684
685         if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
686                 return -ENODEV;
687         }
688
689         return 0;
690 }
691
692 static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
693                                                 u32 event, void *data)
694 {
695         struct acpi_processor *pr;
696         struct acpi_device *device = NULL;
697         int result;
698
699
700         switch (event) {
701         case ACPI_NOTIFY_BUS_CHECK:
702         case ACPI_NOTIFY_DEVICE_CHECK:
703                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
704                 "Processor driver received %s event\n",
705                        (event == ACPI_NOTIFY_BUS_CHECK) ?
706                        "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
707
708                 if (!is_processor_present(handle))
709                         break;
710
711                 if (acpi_bus_get_device(handle, &device)) {
712                         result = acpi_processor_device_add(handle, &device);
713                         if (result)
714                                 printk(KERN_ERR PREFIX
715                                             "Unable to add the device\n");
716                         break;
717                 }
718                 break;
719         case ACPI_NOTIFY_EJECT_REQUEST:
720                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
721                                   "received ACPI_NOTIFY_EJECT_REQUEST\n"));
722
723                 if (acpi_bus_get_device(handle, &device)) {
724                         printk(KERN_ERR PREFIX
725                                     "Device don't exist, dropping EJECT\n");
726                         break;
727                 }
728                 pr = acpi_driver_data(device);
729                 if (!pr) {
730                         printk(KERN_ERR PREFIX
731                                     "Driver data is NULL, dropping EJECT\n");
732                         return;
733                 }
734                 break;
735         default:
736                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
737                                   "Unsupported event [0x%x]\n", event));
738                 break;
739         }
740
741         return;
742 }
743
744 static acpi_status
745 processor_walk_namespace_cb(acpi_handle handle,
746                             u32 lvl, void *context, void **rv)
747 {
748         acpi_status status;
749         int *action = context;
750         acpi_object_type type = 0;
751
752         status = acpi_get_type(handle, &type);
753         if (ACPI_FAILURE(status))
754                 return (AE_OK);
755
756         if (type != ACPI_TYPE_PROCESSOR)
757                 return (AE_OK);
758
759         switch (*action) {
760         case INSTALL_NOTIFY_HANDLER:
761                 acpi_install_notify_handler(handle,
762                                             ACPI_SYSTEM_NOTIFY,
763                                             acpi_processor_hotplug_notify,
764                                             NULL);
765                 break;
766         case UNINSTALL_NOTIFY_HANDLER:
767                 acpi_remove_notify_handler(handle,
768                                            ACPI_SYSTEM_NOTIFY,
769                                            acpi_processor_hotplug_notify);
770                 break;
771         default:
772                 break;
773         }
774
775         return (AE_OK);
776 }
777
778 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
779 {
780
781         if (!is_processor_present(handle)) {
782                 return AE_ERROR;
783         }
784
785         if (acpi_map_lsapic(handle, p_cpu))
786                 return AE_ERROR;
787
788         if (arch_register_cpu(*p_cpu)) {
789                 acpi_unmap_lsapic(*p_cpu);
790                 return AE_ERROR;
791         }
792
793         return AE_OK;
794 }
795
796 static int acpi_processor_handle_eject(struct acpi_processor *pr)
797 {
798         if (cpu_online(pr->id))
799                 cpu_down(pr->id);
800
801         arch_unregister_cpu(pr->id);
802         acpi_unmap_lsapic(pr->id);
803         return (0);
804 }
805 #else
806 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
807 {
808         return AE_ERROR;
809 }
810 static int acpi_processor_handle_eject(struct acpi_processor *pr)
811 {
812         return (-EINVAL);
813 }
814 #endif
815
816 static
817 void acpi_processor_install_hotplug_notify(void)
818 {
819 #ifdef CONFIG_ACPI_HOTPLUG_CPU
820         int action = INSTALL_NOTIFY_HANDLER;
821         acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
822                             ACPI_ROOT_OBJECT,
823                             ACPI_UINT32_MAX,
824                             processor_walk_namespace_cb, NULL, &action, NULL);
825 #endif
826         register_hotcpu_notifier(&acpi_cpu_notifier);
827 }
828
829 static
830 void acpi_processor_uninstall_hotplug_notify(void)
831 {
832 #ifdef CONFIG_ACPI_HOTPLUG_CPU
833         int action = UNINSTALL_NOTIFY_HANDLER;
834         acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
835                             ACPI_ROOT_OBJECT,
836                             ACPI_UINT32_MAX,
837                             processor_walk_namespace_cb, NULL, &action, NULL);
838 #endif
839         unregister_hotcpu_notifier(&acpi_cpu_notifier);
840 }
841
842 /*
843  * We keep the driver loaded even when ACPI is not running.
844  * This is needed for the powernow-k8 driver, that works even without
845  * ACPI, but needs symbols from this driver
846  */
847
848 static int __init acpi_processor_init(void)
849 {
850         int result = 0;
851
852         if (acpi_disabled)
853                 return 0;
854
855         memset(&errata, 0, sizeof(errata));
856
857 #ifdef CONFIG_ACPI_PROCFS
858         acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
859         if (!acpi_processor_dir)
860                 return -ENOMEM;
861 #endif
862
863         if (!cpuidle_register_driver(&acpi_idle_driver)) {
864                 printk(KERN_DEBUG "ACPI: %s registered with cpuidle\n",
865                         acpi_idle_driver.name);
866         } else {
867                 printk(KERN_DEBUG "ACPI: acpi_idle yielding to %s\n",
868                         cpuidle_get_driver()->name);
869         }
870
871         result = acpi_bus_register_driver(&acpi_processor_driver);
872         if (result < 0)
873                 goto out_cpuidle;
874
875         acpi_processor_install_hotplug_notify();
876
877         acpi_thermal_cpufreq_init();
878
879         acpi_processor_ppc_init();
880
881         acpi_processor_throttling_init();
882
883         return 0;
884
885 out_cpuidle:
886         cpuidle_unregister_driver(&acpi_idle_driver);
887
888 #ifdef CONFIG_ACPI_PROCFS
889         remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
890 #endif
891
892         return result;
893 }
894
895 static void __exit acpi_processor_exit(void)
896 {
897         if (acpi_disabled)
898                 return;
899
900         acpi_processor_ppc_exit();
901
902         acpi_thermal_cpufreq_exit();
903
904         acpi_processor_uninstall_hotplug_notify();
905
906         acpi_bus_unregister_driver(&acpi_processor_driver);
907
908         cpuidle_unregister_driver(&acpi_idle_driver);
909
910 #ifdef CONFIG_ACPI_PROCFS
911         remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
912 #endif
913
914         return;
915 }
916
917 module_init(acpi_processor_init);
918 module_exit(acpi_processor_exit);
919
920 MODULE_ALIAS("processor");