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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
20 #include <linux/kernel.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/types.h>
24 #include <linux/cpu.h>
25 #include <linux/acpi.h>
26 #include <linux/uaccess.h>
27 #include <acpi/processor.h>
29 #include <xen/interface/platform.h>
30 #include <asm/xen/hypercall.h>
32 #define PREFIX "ACPI:xen_cpu_hotplug:"
34 #define INSTALL_NOTIFY_HANDLER 0
35 #define UNINSTALL_NOTIFY_HANDLER 1
37 static acpi_status
xen_acpi_cpu_hotadd(struct acpi_processor
*pr
);
39 /* --------------------------------------------------------------------------
41 -------------------------------------------------------------------------- */
43 static int xen_acpi_processor_enable(struct acpi_device
*device
)
45 acpi_status status
= 0;
46 unsigned long long value
;
47 union acpi_object object
= { 0 };
48 struct acpi_buffer buffer
= { sizeof(union acpi_object
), &object
};
49 struct acpi_processor
*pr
;
51 pr
= acpi_driver_data(device
);
53 pr_err(PREFIX
"Cannot find driver data\n");
57 if (!strcmp(acpi_device_hid(device
), ACPI_PROCESSOR_OBJECT_HID
)) {
58 /* Declared with "Processor" statement; match ProcessorID */
59 status
= acpi_evaluate_object(pr
->handle
, NULL
, NULL
, &buffer
);
60 if (ACPI_FAILURE(status
)) {
61 pr_err(PREFIX
"Evaluating processor object\n");
65 pr
->acpi_id
= object
.processor
.proc_id
;
67 /* Declared with "Device" statement; match _UID */
68 status
= acpi_evaluate_integer(pr
->handle
, METHOD_NAME__UID
,
70 if (ACPI_FAILURE(status
)) {
71 pr_err(PREFIX
"Evaluating processor _UID\n");
78 pr
->id
= xen_pcpu_id(pr
->acpi_id
);
81 /* This cpu is not presented at hypervisor, try to hotadd it */
82 if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr
))) {
83 pr_err(PREFIX
"Hotadd CPU (acpi_id = %d) failed.\n",
91 static int xen_acpi_processor_add(struct acpi_device
*device
)
94 struct acpi_processor
*pr
;
99 pr
= kzalloc(sizeof(struct acpi_processor
), GFP_KERNEL
);
103 pr
->handle
= device
->handle
;
104 strcpy(acpi_device_name(device
), ACPI_PROCESSOR_DEVICE_NAME
);
105 strcpy(acpi_device_class(device
), ACPI_PROCESSOR_CLASS
);
106 device
->driver_data
= pr
;
108 ret
= xen_acpi_processor_enable(device
);
110 pr_err(PREFIX
"Error when enabling Xen processor\n");
115 static int xen_acpi_processor_remove(struct acpi_device
*device
)
117 struct acpi_processor
*pr
;
122 pr
= acpi_driver_data(device
);
130 /*--------------------------------------------------------------
131 Acpi processor hotplug support
132 --------------------------------------------------------------*/
134 static int is_processor_present(acpi_handle handle
)
137 unsigned long long sta
= 0;
140 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
142 if (ACPI_SUCCESS(status
) && (sta
& ACPI_STA_DEVICE_PRESENT
))
146 * _STA is mandatory for a processor that supports hot plug
148 if (status
== AE_NOT_FOUND
)
149 pr_info(PREFIX
"Processor does not support hot plug\n");
151 pr_info(PREFIX
"Processor Device is not present");
155 static int xen_apic_id(acpi_handle handle
)
157 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
158 union acpi_object
*obj
;
159 struct acpi_madt_local_apic
*lapic
;
162 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
)))
165 if (!buffer
.length
|| !buffer
.pointer
)
168 obj
= buffer
.pointer
;
169 if (obj
->type
!= ACPI_TYPE_BUFFER
||
170 obj
->buffer
.length
< sizeof(*lapic
)) {
171 kfree(buffer
.pointer
);
175 lapic
= (struct acpi_madt_local_apic
*)obj
->buffer
.pointer
;
177 if (lapic
->header
.type
!= ACPI_MADT_TYPE_LOCAL_APIC
||
178 !(lapic
->lapic_flags
& ACPI_MADT_ENABLED
)) {
179 kfree(buffer
.pointer
);
183 apic_id
= (uint32_t)lapic
->id
;
184 kfree(buffer
.pointer
);
185 buffer
.length
= ACPI_ALLOCATE_BUFFER
;
186 buffer
.pointer
= NULL
;
191 static int xen_hotadd_cpu(struct acpi_processor
*pr
)
193 int cpu_id
, apic_id
, pxm
;
194 struct xen_platform_op op
;
196 apic_id
= xen_apic_id(pr
->handle
);
198 pr_err(PREFIX
"Failed to get apic_id for acpi_id %d\n",
203 pxm
= xen_acpi_get_pxm(pr
->handle
);
205 pr_err(PREFIX
"Failed to get _PXM for acpi_id %d\n",
210 op
.cmd
= XENPF_cpu_hotadd
;
211 op
.u
.cpu_add
.apic_id
= apic_id
;
212 op
.u
.cpu_add
.acpi_id
= pr
->acpi_id
;
213 op
.u
.cpu_add
.pxm
= pxm
;
215 cpu_id
= HYPERVISOR_dom0_op(&op
);
217 pr_err(PREFIX
"Failed to hotadd CPU for acpi_id %d\n",
223 static acpi_status
xen_acpi_cpu_hotadd(struct acpi_processor
*pr
)
225 if (!is_processor_present(pr
->handle
))
228 pr
->id
= xen_hotadd_cpu(pr
);
233 * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
234 * interface after cpu hotadded.
236 xen_pcpu_hotplug_sync();
241 static int acpi_processor_device_remove(struct acpi_device
*device
)
243 pr_debug(PREFIX
"Xen does not support CPU hotremove\n");
248 static void acpi_processor_hotplug_notify(acpi_handle handle
,
249 u32 event
, void *data
)
251 struct acpi_processor
*pr
;
252 struct acpi_device
*device
= NULL
;
253 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
; /* default */
256 acpi_scan_lock_acquire();
259 case ACPI_NOTIFY_BUS_CHECK
:
260 case ACPI_NOTIFY_DEVICE_CHECK
:
261 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
262 "Processor driver received %s event\n",
263 (event
== ACPI_NOTIFY_BUS_CHECK
) ?
264 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
266 if (!is_processor_present(handle
))
269 acpi_bus_get_device(handle
, &device
);
270 if (acpi_device_enumerated(device
))
273 result
= acpi_bus_scan(handle
);
275 pr_err(PREFIX
"Unable to add the device\n");
279 acpi_bus_get_device(handle
, &device
);
280 if (!acpi_device_enumerated(device
)) {
281 pr_err(PREFIX
"Missing device object\n");
284 ost_code
= ACPI_OST_SC_SUCCESS
;
287 case ACPI_NOTIFY_EJECT_REQUEST
:
288 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
289 "received ACPI_NOTIFY_EJECT_REQUEST\n"));
291 if (acpi_bus_get_device(handle
, &device
)) {
292 pr_err(PREFIX
"Device don't exist, dropping EJECT\n");
295 pr
= acpi_driver_data(device
);
297 pr_err(PREFIX
"Driver data is NULL, dropping EJECT\n");
302 * TBD: implement acpi_processor_device_remove if Xen support
303 * CPU hotremove in the future.
305 acpi_processor_device_remove(device
);
309 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
310 "Unsupported event [0x%x]\n", event
));
312 /* non-hotplug event; possibly handled by other handler */
316 (void) acpi_evaluate_hotplug_ost(handle
, event
, ost_code
, NULL
);
319 acpi_scan_lock_release();
322 static acpi_status
is_processor_device(acpi_handle handle
)
324 struct acpi_device_info
*info
;
328 status
= acpi_get_object_info(handle
, &info
);
329 if (ACPI_FAILURE(status
))
332 if (info
->type
== ACPI_TYPE_PROCESSOR
) {
334 return AE_OK
; /* found a processor object */
337 if (!(info
->valid
& ACPI_VALID_HID
)) {
342 hid
= info
->hardware_id
.string
;
343 if ((hid
== NULL
) || strcmp(hid
, ACPI_PROCESSOR_DEVICE_HID
)) {
349 return AE_OK
; /* found a processor device object */
353 processor_walk_namespace_cb(acpi_handle handle
,
354 u32 lvl
, void *context
, void **rv
)
357 int *action
= context
;
359 status
= is_processor_device(handle
);
360 if (ACPI_FAILURE(status
))
361 return AE_OK
; /* not a processor; continue to walk */
364 case INSTALL_NOTIFY_HANDLER
:
365 acpi_install_notify_handler(handle
,
367 acpi_processor_hotplug_notify
,
370 case UNINSTALL_NOTIFY_HANDLER
:
371 acpi_remove_notify_handler(handle
,
373 acpi_processor_hotplug_notify
);
379 /* found a processor; skip walking underneath */
380 return AE_CTRL_DEPTH
;
384 void acpi_processor_install_hotplug_notify(void)
386 int action
= INSTALL_NOTIFY_HANDLER
;
387 acpi_walk_namespace(ACPI_TYPE_ANY
,
390 processor_walk_namespace_cb
, NULL
, &action
, NULL
);
394 void acpi_processor_uninstall_hotplug_notify(void)
396 int action
= UNINSTALL_NOTIFY_HANDLER
;
397 acpi_walk_namespace(ACPI_TYPE_ANY
,
400 processor_walk_namespace_cb
, NULL
, &action
, NULL
);
403 static const struct acpi_device_id processor_device_ids
[] = {
404 {ACPI_PROCESSOR_OBJECT_HID
, 0},
405 {ACPI_PROCESSOR_DEVICE_HID
, 0},
408 MODULE_DEVICE_TABLE(acpi
, processor_device_ids
);
410 static struct acpi_driver xen_acpi_processor_driver
= {
412 .class = ACPI_PROCESSOR_CLASS
,
413 .ids
= processor_device_ids
,
415 .add
= xen_acpi_processor_add
,
416 .remove
= xen_acpi_processor_remove
,
420 static int __init
xen_acpi_processor_init(void)
424 if (!xen_initial_domain())
427 /* unregister the stub which only used to reserve driver space */
428 xen_stub_processor_exit();
430 result
= acpi_bus_register_driver(&xen_acpi_processor_driver
);
432 xen_stub_processor_init();
436 acpi_processor_install_hotplug_notify();
440 static void __exit
xen_acpi_processor_exit(void)
442 if (!xen_initial_domain())
445 acpi_processor_uninstall_hotplug_notify();
447 acpi_bus_unregister_driver(&xen_acpi_processor_driver
);
450 * stub reserve space again to prevent any chance of native
453 xen_stub_processor_init();
457 module_init(xen_acpi_processor_init
);
458 module_exit(xen_acpi_processor_exit
);
459 ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
460 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
461 MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
462 MODULE_LICENSE("GPL");