Staging: comedi: range.c: properly mark up __user pointers
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / acpi / processor_driver.c
blob5675d9747e871f1007070081fbc59557576c6d65
1 /*
2 * acpi_processor.c - ACPI Processor Driver ($Revision: 71 $)
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
10 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
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.
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 #include <linux/slab.h>
50 #include <asm/io.h>
51 #include <asm/system.h>
52 #include <asm/cpu.h>
53 #include <asm/delay.h>
54 #include <asm/uaccess.h>
55 #include <asm/processor.h>
56 #include <asm/smp.h>
57 #include <asm/acpi.h>
59 #include <acpi/acpi_bus.h>
60 #include <acpi/acpi_drivers.h>
61 #include <acpi/processor.h>
63 #define PREFIX "ACPI: "
65 #define ACPI_PROCESSOR_CLASS "processor"
66 #define ACPI_PROCESSOR_DEVICE_NAME "Processor"
67 #define ACPI_PROCESSOR_FILE_INFO "info"
68 #define ACPI_PROCESSOR_FILE_THROTTLING "throttling"
69 #define ACPI_PROCESSOR_FILE_LIMIT "limit"
70 #define ACPI_PROCESSOR_NOTIFY_PERFORMANCE 0x80
71 #define ACPI_PROCESSOR_NOTIFY_POWER 0x81
72 #define ACPI_PROCESSOR_NOTIFY_THROTTLING 0x82
74 #define ACPI_PROCESSOR_LIMIT_USER 0
75 #define ACPI_PROCESSOR_LIMIT_THERMAL 1
77 #define _COMPONENT ACPI_PROCESSOR_COMPONENT
78 ACPI_MODULE_NAME("processor_driver");
80 MODULE_AUTHOR("Paul Diefenbaugh");
81 MODULE_DESCRIPTION("ACPI Processor Driver");
82 MODULE_LICENSE("GPL");
84 static int acpi_processor_add(struct acpi_device *device);
85 static int acpi_processor_remove(struct acpi_device *device, int type);
86 #ifdef CONFIG_ACPI_PROCFS
87 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file);
88 #endif
89 static void acpi_processor_notify(struct acpi_device *device, u32 event);
90 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu);
91 static int acpi_processor_handle_eject(struct acpi_processor *pr);
94 static const struct acpi_device_id processor_device_ids[] = {
95 {ACPI_PROCESSOR_OBJECT_HID, 0},
96 {"ACPI0007", 0},
97 {"", 0},
99 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
101 static struct acpi_driver acpi_processor_driver = {
102 .name = "processor",
103 .class = ACPI_PROCESSOR_CLASS,
104 .ids = processor_device_ids,
105 .ops = {
106 .add = acpi_processor_add,
107 .remove = acpi_processor_remove,
108 .suspend = acpi_processor_suspend,
109 .resume = acpi_processor_resume,
110 .notify = acpi_processor_notify,
114 #define INSTALL_NOTIFY_HANDLER 1
115 #define UNINSTALL_NOTIFY_HANDLER 2
116 #ifdef CONFIG_ACPI_PROCFS
117 static const struct file_operations acpi_processor_info_fops = {
118 .owner = THIS_MODULE,
119 .open = acpi_processor_info_open_fs,
120 .read = seq_read,
121 .llseek = seq_lseek,
122 .release = single_release,
124 #endif
126 DEFINE_PER_CPU(struct acpi_processor *, processors);
127 EXPORT_PER_CPU_SYMBOL(processors);
129 struct acpi_processor_errata errata __read_mostly;
131 /* --------------------------------------------------------------------------
132 Errata Handling
133 -------------------------------------------------------------------------- */
135 static int acpi_processor_errata_piix4(struct pci_dev *dev)
137 u8 value1 = 0;
138 u8 value2 = 0;
141 if (!dev)
142 return -EINVAL;
145 * Note that 'dev' references the PIIX4 ACPI Controller.
148 switch (dev->revision) {
149 case 0:
150 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 A-step\n"));
151 break;
152 case 1:
153 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4 B-step\n"));
154 break;
155 case 2:
156 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4E\n"));
157 break;
158 case 3:
159 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found PIIX4M\n"));
160 break;
161 default:
162 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Found unknown PIIX4\n"));
163 break;
166 switch (dev->revision) {
168 case 0: /* PIIX4 A-step */
169 case 1: /* PIIX4 B-step */
171 * See specification changes #13 ("Manual Throttle Duty Cycle")
172 * and #14 ("Enabling and Disabling Manual Throttle"), plus
173 * erratum #5 ("STPCLK# Deassertion Time") from the January
174 * 2002 PIIX4 specification update. Applies to only older
175 * PIIX4 models.
177 errata.piix4.throttle = 1;
179 case 2: /* PIIX4E */
180 case 3: /* PIIX4M */
182 * See erratum #18 ("C3 Power State/BMIDE and Type-F DMA
183 * Livelock") from the January 2002 PIIX4 specification update.
184 * Applies to all PIIX4 models.
188 * BM-IDE
189 * ------
190 * Find the PIIX4 IDE Controller and get the Bus Master IDE
191 * Status register address. We'll use this later to read
192 * each IDE controller's DMA status to make sure we catch all
193 * DMA activity.
195 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
196 PCI_DEVICE_ID_INTEL_82371AB,
197 PCI_ANY_ID, PCI_ANY_ID, NULL);
198 if (dev) {
199 errata.piix4.bmisx = pci_resource_start(dev, 4);
200 pci_dev_put(dev);
204 * Type-F DMA
205 * ----------
206 * Find the PIIX4 ISA Controller and read the Motherboard
207 * DMA controller's status to see if Type-F (Fast) DMA mode
208 * is enabled (bit 7) on either channel. Note that we'll
209 * disable C3 support if this is enabled, as some legacy
210 * devices won't operate well if fast DMA is disabled.
212 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
213 PCI_DEVICE_ID_INTEL_82371AB_0,
214 PCI_ANY_ID, PCI_ANY_ID, NULL);
215 if (dev) {
216 pci_read_config_byte(dev, 0x76, &value1);
217 pci_read_config_byte(dev, 0x77, &value2);
218 if ((value1 & 0x80) || (value2 & 0x80))
219 errata.piix4.fdma = 1;
220 pci_dev_put(dev);
223 break;
226 if (errata.piix4.bmisx)
227 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
228 "Bus master activity detection (BM-IDE) erratum enabled\n"));
229 if (errata.piix4.fdma)
230 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
231 "Type-F DMA livelock erratum (C3 disabled)\n"));
233 return 0;
236 static int acpi_processor_errata(struct acpi_processor *pr)
238 int result = 0;
239 struct pci_dev *dev = NULL;
242 if (!pr)
243 return -EINVAL;
246 * PIIX4
248 dev = pci_get_subsys(PCI_VENDOR_ID_INTEL,
249 PCI_DEVICE_ID_INTEL_82371AB_3, PCI_ANY_ID,
250 PCI_ANY_ID, NULL);
251 if (dev) {
252 result = acpi_processor_errata_piix4(dev);
253 pci_dev_put(dev);
256 return result;
259 /* --------------------------------------------------------------------------
260 FS Interface (/proc)
261 -------------------------------------------------------------------------- */
263 #ifdef CONFIG_ACPI_PROCFS
264 static struct proc_dir_entry *acpi_processor_dir = NULL;
266 static int acpi_processor_info_seq_show(struct seq_file *seq, void *offset)
268 struct acpi_processor *pr = seq->private;
271 if (!pr)
272 goto end;
274 seq_printf(seq, "processor id: %d\n"
275 "acpi id: %d\n"
276 "bus mastering control: %s\n"
277 "power management: %s\n"
278 "throttling control: %s\n"
279 "limit interface: %s\n",
280 pr->id,
281 pr->acpi_id,
282 pr->flags.bm_control ? "yes" : "no",
283 pr->flags.power ? "yes" : "no",
284 pr->flags.throttling ? "yes" : "no",
285 pr->flags.limit ? "yes" : "no");
287 end:
288 return 0;
291 static int acpi_processor_info_open_fs(struct inode *inode, struct file *file)
293 return single_open(file, acpi_processor_info_seq_show,
294 PDE(inode)->data);
297 static int __cpuinit acpi_processor_add_fs(struct acpi_device *device)
299 struct proc_dir_entry *entry = NULL;
302 if (!acpi_device_dir(device)) {
303 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
304 acpi_processor_dir);
305 if (!acpi_device_dir(device))
306 return -ENODEV;
309 /* 'info' [R] */
310 entry = proc_create_data(ACPI_PROCESSOR_FILE_INFO,
311 S_IRUGO, acpi_device_dir(device),
312 &acpi_processor_info_fops,
313 acpi_driver_data(device));
314 if (!entry)
315 return -EIO;
317 /* 'throttling' [R/W] */
318 entry = proc_create_data(ACPI_PROCESSOR_FILE_THROTTLING,
319 S_IFREG | S_IRUGO | S_IWUSR,
320 acpi_device_dir(device),
321 &acpi_processor_throttling_fops,
322 acpi_driver_data(device));
323 if (!entry)
324 return -EIO;
326 /* 'limit' [R/W] */
327 entry = proc_create_data(ACPI_PROCESSOR_FILE_LIMIT,
328 S_IFREG | S_IRUGO | S_IWUSR,
329 acpi_device_dir(device),
330 &acpi_processor_limit_fops,
331 acpi_driver_data(device));
332 if (!entry)
333 return -EIO;
334 return 0;
336 static int acpi_processor_remove_fs(struct acpi_device *device)
339 if (acpi_device_dir(device)) {
340 remove_proc_entry(ACPI_PROCESSOR_FILE_INFO,
341 acpi_device_dir(device));
342 remove_proc_entry(ACPI_PROCESSOR_FILE_THROTTLING,
343 acpi_device_dir(device));
344 remove_proc_entry(ACPI_PROCESSOR_FILE_LIMIT,
345 acpi_device_dir(device));
346 remove_proc_entry(acpi_device_bid(device), acpi_processor_dir);
347 acpi_device_dir(device) = NULL;
350 return 0;
352 #else
353 static inline int acpi_processor_add_fs(struct acpi_device *device)
355 return 0;
357 static inline int acpi_processor_remove_fs(struct acpi_device *device)
359 return 0;
361 #endif
363 /* --------------------------------------------------------------------------
364 Driver Interface
365 -------------------------------------------------------------------------- */
367 static int acpi_processor_get_info(struct acpi_device *device)
369 acpi_status status = 0;
370 union acpi_object object = { 0 };
371 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
372 struct acpi_processor *pr;
373 int cpu_index, device_declaration = 0;
374 static int cpu0_initialized;
376 pr = acpi_driver_data(device);
377 if (!pr)
378 return -EINVAL;
380 if (num_online_cpus() > 1)
381 errata.smp = TRUE;
383 acpi_processor_errata(pr);
386 * Check to see if we have bus mastering arbitration control. This
387 * is required for proper C3 usage (to maintain cache coherency).
389 if (acpi_gbl_FADT.pm2_control_block && acpi_gbl_FADT.pm2_control_length) {
390 pr->flags.bm_control = 1;
391 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
392 "Bus mastering arbitration control present\n"));
393 } else
394 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
395 "No bus mastering arbitration control\n"));
397 if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
398 /* Declared with "Processor" statement; match ProcessorID */
399 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
400 if (ACPI_FAILURE(status)) {
401 printk(KERN_ERR PREFIX "Evaluating processor object\n");
402 return -ENODEV;
406 * TBD: Synch processor ID (via LAPIC/LSAPIC structures) on SMP.
407 * >>> 'acpi_get_processor_id(acpi_id, &id)' in
408 * arch/xxx/acpi.c
410 pr->acpi_id = object.processor.proc_id;
411 } else {
413 * Declared with "Device" statement; match _UID.
414 * Note that we don't handle string _UIDs yet.
416 unsigned long long value;
417 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
418 NULL, &value);
419 if (ACPI_FAILURE(status)) {
420 printk(KERN_ERR PREFIX
421 "Evaluating processor _UID [%#x]\n", status);
422 return -ENODEV;
424 device_declaration = 1;
425 pr->acpi_id = value;
427 cpu_index = acpi_get_cpuid(pr->handle, device_declaration, pr->acpi_id);
429 /* Handle UP system running SMP kernel, with no LAPIC in MADT */
430 if (!cpu0_initialized && (cpu_index == -1) &&
431 (num_online_cpus() == 1)) {
432 cpu_index = 0;
435 cpu0_initialized = 1;
437 pr->id = cpu_index;
440 * Extra Processor objects may be enumerated on MP systems with
441 * less than the max # of CPUs. They should be ignored _iff
442 * they are physically not present.
444 if (pr->id == -1) {
445 if (ACPI_FAILURE
446 (acpi_processor_hotadd_init(pr->handle, &pr->id))) {
447 return -ENODEV;
451 * On some boxes several processors use the same processor bus id.
452 * But they are located in different scope. For example:
453 * \_SB.SCK0.CPU0
454 * \_SB.SCK1.CPU0
455 * Rename the processor device bus id. And the new bus id will be
456 * generated as the following format:
457 * CPU+CPU ID.
459 sprintf(acpi_device_bid(device), "CPU%X", pr->id);
460 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Processor [%d:%d]\n", pr->id,
461 pr->acpi_id));
463 if (!object.processor.pblk_address)
464 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "No PBLK (NULL address)\n"));
465 else if (object.processor.pblk_length != 6)
466 printk(KERN_ERR PREFIX "Invalid PBLK length [%d]\n",
467 object.processor.pblk_length);
468 else {
469 pr->throttling.address = object.processor.pblk_address;
470 pr->throttling.duty_offset = acpi_gbl_FADT.duty_offset;
471 pr->throttling.duty_width = acpi_gbl_FADT.duty_width;
473 pr->pblk = object.processor.pblk_address;
476 * We don't care about error returns - we just try to mark
477 * these reserved so that nobody else is confused into thinking
478 * that this region might be unused..
480 * (In particular, allocating the IO range for Cardbus)
482 request_region(pr->throttling.address, 6, "ACPI CPU throttle");
486 * If ACPI describes a slot number for this CPU, we can use it
487 * ensure we get the right value in the "physical id" field
488 * of /proc/cpuinfo
490 status = acpi_evaluate_object(pr->handle, "_SUN", NULL, &buffer);
491 if (ACPI_SUCCESS(status))
492 arch_fix_phys_package_id(pr->id, object.integer.value);
494 return 0;
497 static DEFINE_PER_CPU(void *, processor_device_array);
499 static void acpi_processor_notify(struct acpi_device *device, u32 event)
501 struct acpi_processor *pr = acpi_driver_data(device);
502 int saved;
504 if (!pr)
505 return;
507 switch (event) {
508 case ACPI_PROCESSOR_NOTIFY_PERFORMANCE:
509 saved = pr->performance_platform_limit;
510 acpi_processor_ppc_has_changed(pr, 1);
511 if (saved == pr->performance_platform_limit)
512 break;
513 acpi_bus_generate_proc_event(device, event,
514 pr->performance_platform_limit);
515 acpi_bus_generate_netlink_event(device->pnp.device_class,
516 dev_name(&device->dev), event,
517 pr->performance_platform_limit);
518 break;
519 case ACPI_PROCESSOR_NOTIFY_POWER:
520 acpi_processor_cst_has_changed(pr);
521 acpi_bus_generate_proc_event(device, event, 0);
522 acpi_bus_generate_netlink_event(device->pnp.device_class,
523 dev_name(&device->dev), event, 0);
524 break;
525 case ACPI_PROCESSOR_NOTIFY_THROTTLING:
526 acpi_processor_tstate_has_changed(pr);
527 acpi_bus_generate_proc_event(device, event, 0);
528 acpi_bus_generate_netlink_event(device->pnp.device_class,
529 dev_name(&device->dev), event, 0);
530 default:
531 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
532 "Unsupported event [0x%x]\n", event));
533 break;
536 return;
539 static int acpi_cpu_soft_notify(struct notifier_block *nfb,
540 unsigned long action, void *hcpu)
542 unsigned int cpu = (unsigned long)hcpu;
543 struct acpi_processor *pr = per_cpu(processors, cpu);
545 if (action == CPU_ONLINE && pr) {
546 acpi_processor_ppc_has_changed(pr, 0);
547 acpi_processor_cst_has_changed(pr);
548 acpi_processor_tstate_has_changed(pr);
550 return NOTIFY_OK;
553 static struct notifier_block acpi_cpu_notifier =
555 .notifier_call = acpi_cpu_soft_notify,
558 static int __cpuinit acpi_processor_add(struct acpi_device *device)
560 struct acpi_processor *pr = NULL;
561 int result = 0;
562 struct sys_device *sysdev;
564 pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
565 if (!pr)
566 return -ENOMEM;
568 if (!zalloc_cpumask_var(&pr->throttling.shared_cpu_map, GFP_KERNEL)) {
569 kfree(pr);
570 return -ENOMEM;
573 pr->handle = device->handle;
574 strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
575 strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
576 device->driver_data = pr;
578 result = acpi_processor_get_info(device);
579 if (result) {
580 /* Processor is physically not present */
581 return 0;
584 BUG_ON((pr->id >= nr_cpu_ids) || (pr->id < 0));
587 * Buggy BIOS check
588 * ACPI id of processors can be reported wrongly by the BIOS.
589 * Don't trust it blindly
591 if (per_cpu(processor_device_array, pr->id) != NULL &&
592 per_cpu(processor_device_array, pr->id) != device) {
593 printk(KERN_WARNING "BIOS reported wrong ACPI id "
594 "for the processor\n");
595 result = -ENODEV;
596 goto err_free_cpumask;
598 per_cpu(processor_device_array, pr->id) = device;
600 per_cpu(processors, pr->id) = pr;
602 result = acpi_processor_add_fs(device);
603 if (result)
604 goto err_free_cpumask;
606 sysdev = get_cpu_sysdev(pr->id);
607 if (sysfs_create_link(&device->dev.kobj, &sysdev->kobj, "sysdev")) {
608 result = -EFAULT;
609 goto err_remove_fs;
612 #ifdef CONFIG_CPU_FREQ
613 acpi_processor_ppc_has_changed(pr, 0);
614 #endif
615 acpi_processor_get_throttling_info(pr);
616 acpi_processor_get_limit_info(pr);
619 acpi_processor_power_init(pr, device);
621 pr->cdev = thermal_cooling_device_register("Processor", device,
622 &processor_cooling_ops);
623 if (IS_ERR(pr->cdev)) {
624 result = PTR_ERR(pr->cdev);
625 goto err_power_exit;
628 dev_dbg(&device->dev, "registered as cooling_device%d\n",
629 pr->cdev->id);
631 result = sysfs_create_link(&device->dev.kobj,
632 &pr->cdev->device.kobj,
633 "thermal_cooling");
634 if (result) {
635 printk(KERN_ERR PREFIX "Create sysfs link\n");
636 goto err_thermal_unregister;
638 result = sysfs_create_link(&pr->cdev->device.kobj,
639 &device->dev.kobj,
640 "device");
641 if (result) {
642 printk(KERN_ERR PREFIX "Create sysfs link\n");
643 goto err_remove_sysfs;
646 return 0;
648 err_remove_sysfs:
649 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
650 err_thermal_unregister:
651 thermal_cooling_device_unregister(pr->cdev);
652 err_power_exit:
653 acpi_processor_power_exit(pr, device);
654 err_remove_fs:
655 acpi_processor_remove_fs(device);
656 err_free_cpumask:
657 free_cpumask_var(pr->throttling.shared_cpu_map);
659 return result;
662 static int acpi_processor_remove(struct acpi_device *device, int type)
664 struct acpi_processor *pr = NULL;
667 if (!device || !acpi_driver_data(device))
668 return -EINVAL;
670 pr = acpi_driver_data(device);
672 if (pr->id >= nr_cpu_ids)
673 goto free;
675 if (type == ACPI_BUS_REMOVAL_EJECT) {
676 if (acpi_processor_handle_eject(pr))
677 return -EINVAL;
680 acpi_processor_power_exit(pr, device);
682 sysfs_remove_link(&device->dev.kobj, "sysdev");
684 acpi_processor_remove_fs(device);
686 if (pr->cdev) {
687 sysfs_remove_link(&device->dev.kobj, "thermal_cooling");
688 sysfs_remove_link(&pr->cdev->device.kobj, "device");
689 thermal_cooling_device_unregister(pr->cdev);
690 pr->cdev = NULL;
693 per_cpu(processors, pr->id) = NULL;
694 per_cpu(processor_device_array, pr->id) = NULL;
696 free:
697 free_cpumask_var(pr->throttling.shared_cpu_map);
698 kfree(pr);
700 return 0;
703 #ifdef CONFIG_ACPI_HOTPLUG_CPU
704 /****************************************************************************
705 * Acpi processor hotplug support *
706 ****************************************************************************/
708 static int is_processor_present(acpi_handle handle)
710 acpi_status status;
711 unsigned long long sta = 0;
714 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
716 if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
717 return 1;
720 * _STA is mandatory for a processor that supports hot plug
722 if (status == AE_NOT_FOUND)
723 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
724 "Processor does not support hot plug\n"));
725 else
726 ACPI_EXCEPTION((AE_INFO, status,
727 "Processor Device is not present"));
728 return 0;
731 static
732 int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
734 acpi_handle phandle;
735 struct acpi_device *pdev;
738 if (acpi_get_parent(handle, &phandle)) {
739 return -ENODEV;
742 if (acpi_bus_get_device(phandle, &pdev)) {
743 return -ENODEV;
746 if (acpi_bus_add(device, pdev, handle, ACPI_BUS_TYPE_PROCESSOR)) {
747 return -ENODEV;
750 return 0;
753 static void __ref acpi_processor_hotplug_notify(acpi_handle handle,
754 u32 event, void *data)
756 struct acpi_processor *pr;
757 struct acpi_device *device = NULL;
758 int result;
761 switch (event) {
762 case ACPI_NOTIFY_BUS_CHECK:
763 case ACPI_NOTIFY_DEVICE_CHECK:
764 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
765 "Processor driver received %s event\n",
766 (event == ACPI_NOTIFY_BUS_CHECK) ?
767 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
769 if (!is_processor_present(handle))
770 break;
772 if (acpi_bus_get_device(handle, &device)) {
773 result = acpi_processor_device_add(handle, &device);
774 if (result)
775 printk(KERN_ERR PREFIX
776 "Unable to add the device\n");
777 break;
779 break;
780 case ACPI_NOTIFY_EJECT_REQUEST:
781 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
782 "received ACPI_NOTIFY_EJECT_REQUEST\n"));
784 if (acpi_bus_get_device(handle, &device)) {
785 printk(KERN_ERR PREFIX
786 "Device don't exist, dropping EJECT\n");
787 break;
789 pr = acpi_driver_data(device);
790 if (!pr) {
791 printk(KERN_ERR PREFIX
792 "Driver data is NULL, dropping EJECT\n");
793 return;
795 break;
796 default:
797 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
798 "Unsupported event [0x%x]\n", event));
799 break;
802 return;
805 static acpi_status
806 processor_walk_namespace_cb(acpi_handle handle,
807 u32 lvl, void *context, void **rv)
809 acpi_status status;
810 int *action = context;
811 acpi_object_type type = 0;
813 status = acpi_get_type(handle, &type);
814 if (ACPI_FAILURE(status))
815 return (AE_OK);
817 if (type != ACPI_TYPE_PROCESSOR)
818 return (AE_OK);
820 switch (*action) {
821 case INSTALL_NOTIFY_HANDLER:
822 acpi_install_notify_handler(handle,
823 ACPI_SYSTEM_NOTIFY,
824 acpi_processor_hotplug_notify,
825 NULL);
826 break;
827 case UNINSTALL_NOTIFY_HANDLER:
828 acpi_remove_notify_handler(handle,
829 ACPI_SYSTEM_NOTIFY,
830 acpi_processor_hotplug_notify);
831 break;
832 default:
833 break;
836 return (AE_OK);
839 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
842 if (!is_processor_present(handle)) {
843 return AE_ERROR;
846 if (acpi_map_lsapic(handle, p_cpu))
847 return AE_ERROR;
849 if (arch_register_cpu(*p_cpu)) {
850 acpi_unmap_lsapic(*p_cpu);
851 return AE_ERROR;
854 return AE_OK;
857 static int acpi_processor_handle_eject(struct acpi_processor *pr)
859 if (cpu_online(pr->id))
860 cpu_down(pr->id);
862 arch_unregister_cpu(pr->id);
863 acpi_unmap_lsapic(pr->id);
864 return (0);
866 #else
867 static acpi_status acpi_processor_hotadd_init(acpi_handle handle, int *p_cpu)
869 return AE_ERROR;
871 static int acpi_processor_handle_eject(struct acpi_processor *pr)
873 return (-EINVAL);
875 #endif
877 static
878 void acpi_processor_install_hotplug_notify(void)
880 #ifdef CONFIG_ACPI_HOTPLUG_CPU
881 int action = INSTALL_NOTIFY_HANDLER;
882 acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
883 ACPI_ROOT_OBJECT,
884 ACPI_UINT32_MAX,
885 processor_walk_namespace_cb, NULL, &action, NULL);
886 #endif
887 register_hotcpu_notifier(&acpi_cpu_notifier);
890 static
891 void acpi_processor_uninstall_hotplug_notify(void)
893 #ifdef CONFIG_ACPI_HOTPLUG_CPU
894 int action = UNINSTALL_NOTIFY_HANDLER;
895 acpi_walk_namespace(ACPI_TYPE_PROCESSOR,
896 ACPI_ROOT_OBJECT,
897 ACPI_UINT32_MAX,
898 processor_walk_namespace_cb, NULL, &action, NULL);
899 #endif
900 unregister_hotcpu_notifier(&acpi_cpu_notifier);
904 * We keep the driver loaded even when ACPI is not running.
905 * This is needed for the powernow-k8 driver, that works even without
906 * ACPI, but needs symbols from this driver
909 static int __init acpi_processor_init(void)
911 int result = 0;
913 if (acpi_disabled)
914 return 0;
916 memset(&errata, 0, sizeof(errata));
918 #ifdef CONFIG_ACPI_PROCFS
919 acpi_processor_dir = proc_mkdir(ACPI_PROCESSOR_CLASS, acpi_root_dir);
920 if (!acpi_processor_dir)
921 return -ENOMEM;
922 #endif
923 result = cpuidle_register_driver(&acpi_idle_driver);
924 if (result < 0)
925 goto out_proc;
927 result = acpi_bus_register_driver(&acpi_processor_driver);
928 if (result < 0)
929 goto out_cpuidle;
931 acpi_processor_install_hotplug_notify();
933 acpi_thermal_cpufreq_init();
935 acpi_processor_ppc_init();
937 acpi_processor_throttling_init();
939 return 0;
941 out_cpuidle:
942 cpuidle_unregister_driver(&acpi_idle_driver);
944 out_proc:
945 #ifdef CONFIG_ACPI_PROCFS
946 remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
947 #endif
949 return result;
952 static void __exit acpi_processor_exit(void)
954 if (acpi_disabled)
955 return;
957 acpi_processor_ppc_exit();
959 acpi_thermal_cpufreq_exit();
961 acpi_processor_uninstall_hotplug_notify();
963 acpi_bus_unregister_driver(&acpi_processor_driver);
965 cpuidle_unregister_driver(&acpi_idle_driver);
967 #ifdef CONFIG_ACPI_PROCFS
968 remove_proc_entry(ACPI_PROCESSOR_CLASS, acpi_root_dir);
969 #endif
971 return;
974 module_init(acpi_processor_init);
975 module_exit(acpi_processor_exit);
977 EXPORT_SYMBOL(acpi_processor_set_thermal_limit);
979 MODULE_ALIAS("processor");