xen-acpi-cpuhotplug.c 11 KB

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