xen-acpi-cpuhotplug.c 11 KB

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