xen-acpi-cpuhotplug.c 11 KB

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