xen-acpi-cpuhotplug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2012 Intel Corporation
  4. * Author: Liu Jinsong <jinsong.liu@intel.com>
  5. * Author: Jiang Yunhong <yunhong.jiang@intel.com>
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/types.h>
  12. #include <linux/cpu.h>
  13. #include <linux/acpi.h>
  14. #include <linux/uaccess.h>
  15. #include <acpi/processor.h>
  16. #include <xen/acpi.h>
  17. #include <xen/interface/platform.h>
  18. #include <asm/xen/hypercall.h>
  19. #define PREFIX "ACPI:xen_cpu_hotplug:"
  20. #define INSTALL_NOTIFY_HANDLER 0
  21. #define UNINSTALL_NOTIFY_HANDLER 1
  22. static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr);
  23. /* --------------------------------------------------------------------------
  24. Driver Interface
  25. -------------------------------------------------------------------------- */
  26. static int xen_acpi_processor_enable(struct acpi_device *device)
  27. {
  28. acpi_status status = 0;
  29. unsigned long long value;
  30. union acpi_object object = { 0 };
  31. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  32. struct acpi_processor *pr = acpi_driver_data(device);
  33. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  34. /* Declared with "Processor" statement; match ProcessorID */
  35. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  36. if (ACPI_FAILURE(status)) {
  37. pr_err(PREFIX "Evaluating processor object\n");
  38. return -ENODEV;
  39. }
  40. pr->acpi_id = object.processor.proc_id;
  41. } else {
  42. /* Declared with "Device" statement; match _UID */
  43. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  44. NULL, &value);
  45. if (ACPI_FAILURE(status)) {
  46. pr_err(PREFIX "Evaluating processor _UID\n");
  47. return -ENODEV;
  48. }
  49. pr->acpi_id = value;
  50. }
  51. pr->id = xen_pcpu_id(pr->acpi_id);
  52. if (invalid_logical_cpuid(pr->id))
  53. /* This cpu is not presented at hypervisor, try to hotadd it */
  54. if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr))) {
  55. pr_err(PREFIX "Hotadd CPU (acpi_id = %d) failed.\n",
  56. pr->acpi_id);
  57. return -ENODEV;
  58. }
  59. return 0;
  60. }
  61. static int xen_acpi_processor_add(struct acpi_device *device)
  62. {
  63. int ret;
  64. struct acpi_processor *pr;
  65. if (!device)
  66. return -EINVAL;
  67. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  68. if (!pr)
  69. return -ENOMEM;
  70. pr->handle = device->handle;
  71. strcpy(acpi_device_name(device), ACPI_PROCESSOR_DEVICE_NAME);
  72. strcpy(acpi_device_class(device), ACPI_PROCESSOR_CLASS);
  73. device->driver_data = pr;
  74. ret = xen_acpi_processor_enable(device);
  75. if (ret)
  76. pr_err(PREFIX "Error when enabling Xen processor\n");
  77. return ret;
  78. }
  79. static int xen_acpi_processor_remove(struct acpi_device *device)
  80. {
  81. struct acpi_processor *pr;
  82. if (!device)
  83. return -EINVAL;
  84. pr = acpi_driver_data(device);
  85. if (!pr)
  86. return -EINVAL;
  87. kfree(pr);
  88. return 0;
  89. }
  90. /*--------------------------------------------------------------
  91. Acpi processor hotplug support
  92. --------------------------------------------------------------*/
  93. static int is_processor_present(acpi_handle handle)
  94. {
  95. acpi_status status;
  96. unsigned long long sta = 0;
  97. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  98. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  99. return 1;
  100. /*
  101. * _STA is mandatory for a processor that supports hot plug
  102. */
  103. if (status == AE_NOT_FOUND)
  104. pr_info(PREFIX "Processor does not support hot plug\n");
  105. else
  106. pr_info(PREFIX "Processor Device is not present");
  107. return 0;
  108. }
  109. static int xen_apic_id(acpi_handle handle)
  110. {
  111. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  112. union acpi_object *obj;
  113. struct acpi_madt_local_apic *lapic;
  114. int apic_id;
  115. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  116. return -EINVAL;
  117. if (!buffer.length || !buffer.pointer)
  118. return -EINVAL;
  119. obj = buffer.pointer;
  120. if (obj->type != ACPI_TYPE_BUFFER ||
  121. obj->buffer.length < sizeof(*lapic)) {
  122. kfree(buffer.pointer);
  123. return -EINVAL;
  124. }
  125. lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
  126. if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
  127. !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
  128. kfree(buffer.pointer);
  129. return -EINVAL;
  130. }
  131. apic_id = (uint32_t)lapic->id;
  132. kfree(buffer.pointer);
  133. buffer.length = ACPI_ALLOCATE_BUFFER;
  134. buffer.pointer = NULL;
  135. return apic_id;
  136. }
  137. static int xen_hotadd_cpu(struct acpi_processor *pr)
  138. {
  139. int cpu_id, apic_id, pxm;
  140. struct xen_platform_op op;
  141. apic_id = xen_apic_id(pr->handle);
  142. if (apic_id < 0) {
  143. pr_err(PREFIX "Failed to get apic_id for acpi_id %d\n",
  144. pr->acpi_id);
  145. return -ENODEV;
  146. }
  147. pxm = xen_acpi_get_pxm(pr->handle);
  148. if (pxm < 0) {
  149. pr_err(PREFIX "Failed to get _PXM for acpi_id %d\n",
  150. pr->acpi_id);
  151. return pxm;
  152. }
  153. op.cmd = XENPF_cpu_hotadd;
  154. op.u.cpu_add.apic_id = apic_id;
  155. op.u.cpu_add.acpi_id = pr->acpi_id;
  156. op.u.cpu_add.pxm = pxm;
  157. cpu_id = HYPERVISOR_platform_op(&op);
  158. if (cpu_id < 0)
  159. pr_err(PREFIX "Failed to hotadd CPU for acpi_id %d\n",
  160. pr->acpi_id);
  161. return cpu_id;
  162. }
  163. static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr)
  164. {
  165. if (!is_processor_present(pr->handle))
  166. return AE_ERROR;
  167. pr->id = xen_hotadd_cpu(pr);
  168. if (invalid_logical_cpuid(pr->id))
  169. return AE_ERROR;
  170. /*
  171. * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
  172. * interface after cpu hotadded.
  173. */
  174. xen_pcpu_hotplug_sync();
  175. return AE_OK;
  176. }
  177. static int acpi_processor_device_remove(struct acpi_device *device)
  178. {
  179. pr_debug(PREFIX "Xen does not support CPU hotremove\n");
  180. return -ENOSYS;
  181. }
  182. static void acpi_processor_hotplug_notify(acpi_handle handle,
  183. u32 event, void *data)
  184. {
  185. struct acpi_processor *pr;
  186. struct acpi_device *device = NULL;
  187. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  188. int result;
  189. acpi_scan_lock_acquire();
  190. switch (event) {
  191. case ACPI_NOTIFY_BUS_CHECK:
  192. case ACPI_NOTIFY_DEVICE_CHECK:
  193. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  194. "Processor driver received %s event\n",
  195. (event == ACPI_NOTIFY_BUS_CHECK) ?
  196. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  197. if (!is_processor_present(handle))
  198. break;
  199. acpi_bus_get_device(handle, &device);
  200. if (acpi_device_enumerated(device))
  201. break;
  202. result = acpi_bus_scan(handle);
  203. if (result) {
  204. pr_err(PREFIX "Unable to add the device\n");
  205. break;
  206. }
  207. device = NULL;
  208. acpi_bus_get_device(handle, &device);
  209. if (!acpi_device_enumerated(device)) {
  210. pr_err(PREFIX "Missing device object\n");
  211. break;
  212. }
  213. ost_code = ACPI_OST_SC_SUCCESS;
  214. break;
  215. case ACPI_NOTIFY_EJECT_REQUEST:
  216. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  217. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  218. if (acpi_bus_get_device(handle, &device)) {
  219. pr_err(PREFIX "Device don't exist, dropping EJECT\n");
  220. break;
  221. }
  222. pr = acpi_driver_data(device);
  223. if (!pr) {
  224. pr_err(PREFIX "Driver data is NULL, dropping EJECT\n");
  225. break;
  226. }
  227. /*
  228. * TBD: implement acpi_processor_device_remove if Xen support
  229. * CPU hotremove in the future.
  230. */
  231. acpi_processor_device_remove(device);
  232. break;
  233. default:
  234. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  235. "Unsupported event [0x%x]\n", event));
  236. /* non-hotplug event; possibly handled by other handler */
  237. goto out;
  238. }
  239. (void) acpi_evaluate_ost(handle, event, ost_code, NULL);
  240. out:
  241. acpi_scan_lock_release();
  242. }
  243. static acpi_status is_processor_device(acpi_handle handle)
  244. {
  245. struct acpi_device_info *info;
  246. char *hid;
  247. acpi_status status;
  248. status = acpi_get_object_info(handle, &info);
  249. if (ACPI_FAILURE(status))
  250. return status;
  251. if (info->type == ACPI_TYPE_PROCESSOR) {
  252. kfree(info);
  253. return AE_OK; /* found a processor object */
  254. }
  255. if (!(info->valid & ACPI_VALID_HID)) {
  256. kfree(info);
  257. return AE_ERROR;
  258. }
  259. hid = info->hardware_id.string;
  260. if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) {
  261. kfree(info);
  262. return AE_ERROR;
  263. }
  264. kfree(info);
  265. return AE_OK; /* found a processor device object */
  266. }
  267. static acpi_status
  268. processor_walk_namespace_cb(acpi_handle handle,
  269. u32 lvl, void *context, void **rv)
  270. {
  271. acpi_status status;
  272. int *action = context;
  273. status = is_processor_device(handle);
  274. if (ACPI_FAILURE(status))
  275. return AE_OK; /* not a processor; continue to walk */
  276. switch (*action) {
  277. case INSTALL_NOTIFY_HANDLER:
  278. acpi_install_notify_handler(handle,
  279. ACPI_SYSTEM_NOTIFY,
  280. acpi_processor_hotplug_notify,
  281. NULL);
  282. break;
  283. case UNINSTALL_NOTIFY_HANDLER:
  284. acpi_remove_notify_handler(handle,
  285. ACPI_SYSTEM_NOTIFY,
  286. acpi_processor_hotplug_notify);
  287. break;
  288. default:
  289. break;
  290. }
  291. /* found a processor; skip walking underneath */
  292. return AE_CTRL_DEPTH;
  293. }
  294. static
  295. void acpi_processor_install_hotplug_notify(void)
  296. {
  297. int action = INSTALL_NOTIFY_HANDLER;
  298. acpi_walk_namespace(ACPI_TYPE_ANY,
  299. ACPI_ROOT_OBJECT,
  300. ACPI_UINT32_MAX,
  301. processor_walk_namespace_cb, NULL, &action, NULL);
  302. }
  303. static
  304. void acpi_processor_uninstall_hotplug_notify(void)
  305. {
  306. int action = UNINSTALL_NOTIFY_HANDLER;
  307. acpi_walk_namespace(ACPI_TYPE_ANY,
  308. ACPI_ROOT_OBJECT,
  309. ACPI_UINT32_MAX,
  310. processor_walk_namespace_cb, NULL, &action, NULL);
  311. }
  312. static const struct acpi_device_id processor_device_ids[] = {
  313. {ACPI_PROCESSOR_OBJECT_HID, 0},
  314. {ACPI_PROCESSOR_DEVICE_HID, 0},
  315. {"", 0},
  316. };
  317. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  318. static struct acpi_driver xen_acpi_processor_driver = {
  319. .name = "processor",
  320. .class = ACPI_PROCESSOR_CLASS,
  321. .ids = processor_device_ids,
  322. .ops = {
  323. .add = xen_acpi_processor_add,
  324. .remove = xen_acpi_processor_remove,
  325. },
  326. };
  327. static int __init xen_acpi_processor_init(void)
  328. {
  329. int result = 0;
  330. if (!xen_initial_domain())
  331. return -ENODEV;
  332. /* unregister the stub which only used to reserve driver space */
  333. xen_stub_processor_exit();
  334. result = acpi_bus_register_driver(&xen_acpi_processor_driver);
  335. if (result < 0) {
  336. xen_stub_processor_init();
  337. return result;
  338. }
  339. acpi_processor_install_hotplug_notify();
  340. return 0;
  341. }
  342. static void __exit xen_acpi_processor_exit(void)
  343. {
  344. if (!xen_initial_domain())
  345. return;
  346. acpi_processor_uninstall_hotplug_notify();
  347. acpi_bus_unregister_driver(&xen_acpi_processor_driver);
  348. /*
  349. * stub reserve space again to prevent any chance of native
  350. * driver loading.
  351. */
  352. xen_stub_processor_init();
  353. return;
  354. }
  355. module_init(xen_acpi_processor_init);
  356. module_exit(xen_acpi_processor_exit);
  357. ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
  358. MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
  359. MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
  360. MODULE_LICENSE("GPL");