2 * Copyright (C) 2012 Intel Corporation
3 * Author: Liu Jinsong <jinsong.liu@intel.com>
4 * Author: Jiang Yunhong <yunhong.jiang@intel.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or (at
9 * your option) any later version.
11 * This program is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
14 * NON INFRINGEMENT. See the GNU General Public License for more
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/types.h>
22 #include <linux/cpu.h>
23 #include <linux/acpi.h>
24 #include <linux/uaccess.h>
25 #include <acpi/acpi_bus.h>
26 #include <acpi/acpi_drivers.h>
27 #include <acpi/processor.h>
30 #include <xen/interface/platform.h>
31 #include <asm/xen/hypercall.h>
33 #define PREFIX "ACPI:xen_cpu_hotplug:"
35 #define INSTALL_NOTIFY_HANDLER 0
36 #define UNINSTALL_NOTIFY_HANDLER 1
38 static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr);
40 /* --------------------------------------------------------------------------
42 -------------------------------------------------------------------------- */
44 static int xen_acpi_processor_enable(struct acpi_device *device)
46 acpi_status status = 0;
47 unsigned long long value;
48 union acpi_object object = { 0 };
49 struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
50 struct acpi_processor *pr;
52 pr = acpi_driver_data(device);
54 pr_err(PREFIX "Cannot find driver data\n");
58 if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
59 /* Declared with "Processor" statement; match ProcessorID */
60 status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
61 if (ACPI_FAILURE(status)) {
62 pr_err(PREFIX "Evaluating processor object\n");
66 pr->acpi_id = object.processor.proc_id;
68 /* Declared with "Device" statement; match _UID */
69 status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
71 if (ACPI_FAILURE(status)) {
72 pr_err(PREFIX "Evaluating processor _UID\n");
79 pr->id = xen_pcpu_id(pr->acpi_id);
82 /* This cpu is not presented at hypervisor, try to hotadd it */
83 if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr))) {
84 pr_err(PREFIX "Hotadd CPU (acpi_id = %d) failed.\n",
92 static int __cpuinit xen_acpi_processor_add(struct acpi_device *device)
95 struct acpi_processor *pr;
100 pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
104 pr->handle = device->handle;
105 strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
106 strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
107 device->driver_data = pr;
109 ret = xen_acpi_processor_enable(device);
111 pr_err(PREFIX "Error when enabling Xen processor\n");
116 static int xen_acpi_processor_remove(struct acpi_device *device)
118 struct acpi_processor *pr;
123 pr = acpi_driver_data(device);
131 /*--------------------------------------------------------------
132 Acpi processor hotplug support
133 --------------------------------------------------------------*/
135 static int is_processor_present(acpi_handle handle)
138 unsigned long long sta = 0;
141 status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
143 if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
147 * _STA is mandatory for a processor that supports hot plug
149 if (status == AE_NOT_FOUND)
150 pr_info(PREFIX "Processor does not support hot plug\n");
152 pr_info(PREFIX "Processor Device is not present");
156 static int xen_apic_id(acpi_handle handle)
158 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
159 union acpi_object *obj;
160 struct acpi_madt_local_apic *lapic;
163 if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
166 if (!buffer.length || !buffer.pointer)
169 obj = buffer.pointer;
170 if (obj->type != ACPI_TYPE_BUFFER ||
171 obj->buffer.length < sizeof(*lapic)) {
172 kfree(buffer.pointer);
176 lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
178 if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
179 !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
180 kfree(buffer.pointer);
184 apic_id = (uint32_t)lapic->id;
185 kfree(buffer.pointer);
186 buffer.length = ACPI_ALLOCATE_BUFFER;
187 buffer.pointer = NULL;
192 static int xen_hotadd_cpu(struct acpi_processor *pr)
194 int cpu_id, apic_id, pxm;
195 struct xen_platform_op op;
197 apic_id = xen_apic_id(pr->handle);
199 pr_err(PREFIX "Failed to get apic_id for acpi_id %d\n",
204 pxm = xen_acpi_get_pxm(pr->handle);
206 pr_err(PREFIX "Failed to get _PXM for acpi_id %d\n",
211 op.cmd = XENPF_cpu_hotadd;
212 op.u.cpu_add.apic_id = apic_id;
213 op.u.cpu_add.acpi_id = pr->acpi_id;
214 op.u.cpu_add.pxm = pxm;
216 cpu_id = HYPERVISOR_dom0_op(&op);
218 pr_err(PREFIX "Failed to hotadd CPU for acpi_id %d\n",
224 static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr)
226 if (!is_processor_present(pr->handle))
229 pr->id = xen_hotadd_cpu(pr);
234 * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
235 * interface after cpu hotadded.
237 xen_pcpu_hotplug_sync();
243 int acpi_processor_device_add(acpi_handle handle, struct acpi_device **device)
246 struct acpi_device *pdev;
248 if (acpi_get_parent(handle, &phandle))
251 if (acpi_bus_get_device(phandle, &pdev))
254 if (acpi_bus_scan(handle))
260 static int acpi_processor_device_remove(struct acpi_device *device)
262 pr_debug(PREFIX "Xen does not support CPU hotremove\n");
267 static void acpi_processor_hotplug_notify(acpi_handle handle,
268 u32 event, void *data)
270 struct acpi_processor *pr;
271 struct acpi_device *device = NULL;
272 u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
276 case ACPI_NOTIFY_BUS_CHECK:
277 case ACPI_NOTIFY_DEVICE_CHECK:
278 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
279 "Processor driver received %s event\n",
280 (event == ACPI_NOTIFY_BUS_CHECK) ?
281 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
283 if (!is_processor_present(handle))
286 if (!acpi_bus_get_device(handle, &device))
289 result = acpi_processor_device_add(handle, &device);
291 pr_err(PREFIX "Unable to add the device\n");
295 ost_code = ACPI_OST_SC_SUCCESS;
298 case ACPI_NOTIFY_EJECT_REQUEST:
299 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
300 "received ACPI_NOTIFY_EJECT_REQUEST\n"));
302 if (acpi_bus_get_device(handle, &device)) {
303 pr_err(PREFIX "Device don't exist, dropping EJECT\n");
306 pr = acpi_driver_data(device);
308 pr_err(PREFIX "Driver data is NULL, dropping EJECT\n");
313 * TBD: implement acpi_processor_device_remove if Xen support
314 * CPU hotremove in the future.
316 acpi_processor_device_remove(device);
320 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
321 "Unsupported event [0x%x]\n", event));
323 /* non-hotplug event; possibly handled by other handler */
327 (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
331 static acpi_status is_processor_device(acpi_handle handle)
333 struct acpi_device_info *info;
337 status = acpi_get_object_info(handle, &info);
338 if (ACPI_FAILURE(status))
341 if (info->type == ACPI_TYPE_PROCESSOR) {
343 return AE_OK; /* found a processor object */
346 if (!(info->valid & ACPI_VALID_HID)) {
351 hid = info->hardware_id.string;
352 if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) {
358 return AE_OK; /* found a processor device object */
362 processor_walk_namespace_cb(acpi_handle handle,
363 u32 lvl, void *context, void **rv)
366 int *action = context;
368 status = is_processor_device(handle);
369 if (ACPI_FAILURE(status))
370 return AE_OK; /* not a processor; continue to walk */
373 case INSTALL_NOTIFY_HANDLER:
374 acpi_install_notify_handler(handle,
376 acpi_processor_hotplug_notify,
379 case UNINSTALL_NOTIFY_HANDLER:
380 acpi_remove_notify_handler(handle,
382 acpi_processor_hotplug_notify);
388 /* found a processor; skip walking underneath */
389 return AE_CTRL_DEPTH;
393 void acpi_processor_install_hotplug_notify(void)
395 int action = INSTALL_NOTIFY_HANDLER;
396 acpi_walk_namespace(ACPI_TYPE_ANY,
399 processor_walk_namespace_cb, NULL, &action, NULL);
403 void acpi_processor_uninstall_hotplug_notify(void)
405 int action = UNINSTALL_NOTIFY_HANDLER;
406 acpi_walk_namespace(ACPI_TYPE_ANY,
409 processor_walk_namespace_cb, NULL, &action, NULL);
412 static const struct acpi_device_id processor_device_ids[] = {
413 {ACPI_PROCESSOR_OBJECT_HID, 0},
414 {ACPI_PROCESSOR_DEVICE_HID, 0},
417 MODULE_DEVICE_TABLE(acpi, processor_device_ids);
419 static struct acpi_driver xen_acpi_processor_driver = {
421 .class = ACPI_PROCESSOR_CLASS,
422 .ids = processor_device_ids,
424 .add = xen_acpi_processor_add,
425 .remove = xen_acpi_processor_remove,
429 static int __init xen_acpi_processor_init(void)
433 if (!xen_initial_domain())
436 /* unregister the stub which only used to reserve driver space */
437 xen_stub_processor_exit();
439 result = acpi_bus_register_driver(&xen_acpi_processor_driver);
441 xen_stub_processor_init();
445 acpi_processor_install_hotplug_notify();
449 static void __exit xen_acpi_processor_exit(void)
451 if (!xen_initial_domain())
454 acpi_processor_uninstall_hotplug_notify();
456 acpi_bus_unregister_driver(&xen_acpi_processor_driver);
459 * stub reserve space again to prevent any chance of native
462 xen_stub_processor_init();
466 module_init(xen_acpi_processor_init);
467 module_exit(xen_acpi_processor_exit);
468 ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
469 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
470 MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
471 MODULE_LICENSE("GPL");