1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2012 Intel Corporation
4 * Author: Liu Jinsong <jinsong.liu@intel.com>
5 * Author: Jiang Yunhong <yunhong.jiang@intel.com>
8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
10 #include <linux/kernel.h>
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/types.h>
14 #include <linux/cpu.h>
15 #include <linux/acpi.h>
16 #include <linux/uaccess.h>
17 #include <acpi/processor.h>
19 #include <xen/interface/platform.h>
20 #include <asm/xen/hypercall.h>
22 #define PREFIX "ACPI:xen_cpu_hotplug:"
24 #define INSTALL_NOTIFY_HANDLER 0
25 #define UNINSTALL_NOTIFY_HANDLER 1
27 static acpi_status
xen_acpi_cpu_hotadd(struct acpi_processor
*pr
);
29 /* --------------------------------------------------------------------------
31 -------------------------------------------------------------------------- */
33 static int xen_acpi_processor_enable(struct acpi_device
*device
)
35 acpi_status status
= 0;
36 unsigned long long value
;
37 union acpi_object object
= { 0 };
38 struct acpi_buffer buffer
= { sizeof(union acpi_object
), &object
};
39 struct acpi_processor
*pr
= acpi_driver_data(device
);
41 if (!strcmp(acpi_device_hid(device
), ACPI_PROCESSOR_OBJECT_HID
)) {
42 /* Declared with "Processor" statement; match ProcessorID */
43 status
= acpi_evaluate_object(pr
->handle
, NULL
, NULL
, &buffer
);
44 if (ACPI_FAILURE(status
)) {
45 pr_err(PREFIX
"Evaluating processor object\n");
49 pr
->acpi_id
= object
.processor
.proc_id
;
51 /* Declared with "Device" statement; match _UID */
52 status
= acpi_evaluate_integer(pr
->handle
, METHOD_NAME__UID
,
54 if (ACPI_FAILURE(status
)) {
55 pr_err(PREFIX
"Evaluating processor _UID\n");
62 pr
->id
= xen_pcpu_id(pr
->acpi_id
);
64 if (invalid_logical_cpuid(pr
->id
))
65 /* This cpu is not presented at hypervisor, try to hotadd it */
66 if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr
))) {
67 pr_err(PREFIX
"Hotadd CPU (acpi_id = %d) failed.\n",
75 static int xen_acpi_processor_add(struct acpi_device
*device
)
78 struct acpi_processor
*pr
;
83 pr
= kzalloc(sizeof(struct acpi_processor
), GFP_KERNEL
);
87 pr
->handle
= device
->handle
;
88 strcpy(acpi_device_name(device
), ACPI_PROCESSOR_DEVICE_NAME
);
89 strcpy(acpi_device_class(device
), ACPI_PROCESSOR_CLASS
);
90 device
->driver_data
= pr
;
92 ret
= xen_acpi_processor_enable(device
);
94 pr_err(PREFIX
"Error when enabling Xen processor\n");
99 static int xen_acpi_processor_remove(struct acpi_device
*device
)
101 struct acpi_processor
*pr
;
106 pr
= acpi_driver_data(device
);
114 /*--------------------------------------------------------------
115 Acpi processor hotplug support
116 --------------------------------------------------------------*/
118 static int is_processor_present(acpi_handle handle
)
121 unsigned long long sta
= 0;
124 status
= acpi_evaluate_integer(handle
, "_STA", NULL
, &sta
);
126 if (ACPI_SUCCESS(status
) && (sta
& ACPI_STA_DEVICE_PRESENT
))
130 * _STA is mandatory for a processor that supports hot plug
132 if (status
== AE_NOT_FOUND
)
133 pr_info(PREFIX
"Processor does not support hot plug\n");
135 pr_info(PREFIX
"Processor Device is not present");
139 static int xen_apic_id(acpi_handle handle
)
141 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
142 union acpi_object
*obj
;
143 struct acpi_madt_local_apic
*lapic
;
146 if (ACPI_FAILURE(acpi_evaluate_object(handle
, "_MAT", NULL
, &buffer
)))
149 if (!buffer
.length
|| !buffer
.pointer
)
152 obj
= buffer
.pointer
;
153 if (obj
->type
!= ACPI_TYPE_BUFFER
||
154 obj
->buffer
.length
< sizeof(*lapic
)) {
155 kfree(buffer
.pointer
);
159 lapic
= (struct acpi_madt_local_apic
*)obj
->buffer
.pointer
;
161 if (lapic
->header
.type
!= ACPI_MADT_TYPE_LOCAL_APIC
||
162 !(lapic
->lapic_flags
& ACPI_MADT_ENABLED
)) {
163 kfree(buffer
.pointer
);
167 apic_id
= (uint32_t)lapic
->id
;
168 kfree(buffer
.pointer
);
169 buffer
.length
= ACPI_ALLOCATE_BUFFER
;
170 buffer
.pointer
= NULL
;
175 static int xen_hotadd_cpu(struct acpi_processor
*pr
)
177 int cpu_id
, apic_id
, pxm
;
178 struct xen_platform_op op
;
180 apic_id
= xen_apic_id(pr
->handle
);
182 pr_err(PREFIX
"Failed to get apic_id for acpi_id %d\n",
187 pxm
= xen_acpi_get_pxm(pr
->handle
);
189 pr_err(PREFIX
"Failed to get _PXM for acpi_id %d\n",
194 op
.cmd
= XENPF_cpu_hotadd
;
195 op
.u
.cpu_add
.apic_id
= apic_id
;
196 op
.u
.cpu_add
.acpi_id
= pr
->acpi_id
;
197 op
.u
.cpu_add
.pxm
= pxm
;
199 cpu_id
= HYPERVISOR_platform_op(&op
);
201 pr_err(PREFIX
"Failed to hotadd CPU for acpi_id %d\n",
207 static acpi_status
xen_acpi_cpu_hotadd(struct acpi_processor
*pr
)
209 if (!is_processor_present(pr
->handle
))
212 pr
->id
= xen_hotadd_cpu(pr
);
213 if (invalid_logical_cpuid(pr
->id
))
217 * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
218 * interface after cpu hotadded.
220 xen_pcpu_hotplug_sync();
225 static int acpi_processor_device_remove(struct acpi_device
*device
)
227 pr_debug(PREFIX
"Xen does not support CPU hotremove\n");
232 static void acpi_processor_hotplug_notify(acpi_handle handle
,
233 u32 event
, void *data
)
235 struct acpi_processor
*pr
;
236 struct acpi_device
*device
= NULL
;
237 u32 ost_code
= ACPI_OST_SC_NON_SPECIFIC_FAILURE
; /* default */
240 acpi_scan_lock_acquire();
243 case ACPI_NOTIFY_BUS_CHECK
:
244 case ACPI_NOTIFY_DEVICE_CHECK
:
245 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
246 "Processor driver received %s event\n",
247 (event
== ACPI_NOTIFY_BUS_CHECK
) ?
248 "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
250 if (!is_processor_present(handle
))
253 acpi_bus_get_device(handle
, &device
);
254 if (acpi_device_enumerated(device
))
257 result
= acpi_bus_scan(handle
);
259 pr_err(PREFIX
"Unable to add the device\n");
263 acpi_bus_get_device(handle
, &device
);
264 if (!acpi_device_enumerated(device
)) {
265 pr_err(PREFIX
"Missing device object\n");
268 ost_code
= ACPI_OST_SC_SUCCESS
;
271 case ACPI_NOTIFY_EJECT_REQUEST
:
272 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
273 "received ACPI_NOTIFY_EJECT_REQUEST\n"));
275 if (acpi_bus_get_device(handle
, &device
)) {
276 pr_err(PREFIX
"Device don't exist, dropping EJECT\n");
279 pr
= acpi_driver_data(device
);
281 pr_err(PREFIX
"Driver data is NULL, dropping EJECT\n");
286 * TBD: implement acpi_processor_device_remove if Xen support
287 * CPU hotremove in the future.
289 acpi_processor_device_remove(device
);
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
294 "Unsupported event [0x%x]\n", event
));
296 /* non-hotplug event; possibly handled by other handler */
300 (void) acpi_evaluate_ost(handle
, event
, ost_code
, NULL
);
303 acpi_scan_lock_release();
306 static acpi_status
is_processor_device(acpi_handle handle
)
308 struct acpi_device_info
*info
;
312 status
= acpi_get_object_info(handle
, &info
);
313 if (ACPI_FAILURE(status
))
316 if (info
->type
== ACPI_TYPE_PROCESSOR
) {
318 return AE_OK
; /* found a processor object */
321 if (!(info
->valid
& ACPI_VALID_HID
)) {
326 hid
= info
->hardware_id
.string
;
327 if ((hid
== NULL
) || strcmp(hid
, ACPI_PROCESSOR_DEVICE_HID
)) {
333 return AE_OK
; /* found a processor device object */
337 processor_walk_namespace_cb(acpi_handle handle
,
338 u32 lvl
, void *context
, void **rv
)
341 int *action
= context
;
343 status
= is_processor_device(handle
);
344 if (ACPI_FAILURE(status
))
345 return AE_OK
; /* not a processor; continue to walk */
348 case INSTALL_NOTIFY_HANDLER
:
349 acpi_install_notify_handler(handle
,
351 acpi_processor_hotplug_notify
,
354 case UNINSTALL_NOTIFY_HANDLER
:
355 acpi_remove_notify_handler(handle
,
357 acpi_processor_hotplug_notify
);
363 /* found a processor; skip walking underneath */
364 return AE_CTRL_DEPTH
;
368 void acpi_processor_install_hotplug_notify(void)
370 int action
= INSTALL_NOTIFY_HANDLER
;
371 acpi_walk_namespace(ACPI_TYPE_ANY
,
374 processor_walk_namespace_cb
, NULL
, &action
, NULL
);
378 void acpi_processor_uninstall_hotplug_notify(void)
380 int action
= UNINSTALL_NOTIFY_HANDLER
;
381 acpi_walk_namespace(ACPI_TYPE_ANY
,
384 processor_walk_namespace_cb
, NULL
, &action
, NULL
);
387 static const struct acpi_device_id processor_device_ids
[] = {
388 {ACPI_PROCESSOR_OBJECT_HID
, 0},
389 {ACPI_PROCESSOR_DEVICE_HID
, 0},
392 MODULE_DEVICE_TABLE(acpi
, processor_device_ids
);
394 static struct acpi_driver xen_acpi_processor_driver
= {
396 .class = ACPI_PROCESSOR_CLASS
,
397 .ids
= processor_device_ids
,
399 .add
= xen_acpi_processor_add
,
400 .remove
= xen_acpi_processor_remove
,
404 static int __init
xen_acpi_processor_init(void)
408 if (!xen_initial_domain())
411 /* unregister the stub which only used to reserve driver space */
412 xen_stub_processor_exit();
414 result
= acpi_bus_register_driver(&xen_acpi_processor_driver
);
416 xen_stub_processor_init();
420 acpi_processor_install_hotplug_notify();
424 static void __exit
xen_acpi_processor_exit(void)
426 if (!xen_initial_domain())
429 acpi_processor_uninstall_hotplug_notify();
431 acpi_bus_unregister_driver(&xen_acpi_processor_driver
);
434 * stub reserve space again to prevent any chance of native
437 xen_stub_processor_init();
441 module_init(xen_acpi_processor_init
);
442 module_exit(xen_acpi_processor_exit
);
443 ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
444 MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
445 MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
446 MODULE_LICENSE("GPL");