xen-acpi-cpuhotplug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463
  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/acpi_bus.h>
  26. #include <acpi/acpi_drivers.h>
  27. #include <acpi/processor.h>
  28. #include <xen/acpi.h>
  29. #include <xen/interface/platform.h>
  30. #include <asm/xen/hypercall.h>
  31. #define PREFIX "ACPI:xen_cpu_hotplug:"
  32. #define INSTALL_NOTIFY_HANDLER 0
  33. #define UNINSTALL_NOTIFY_HANDLER 1
  34. static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr);
  35. /* --------------------------------------------------------------------------
  36. Driver Interface
  37. -------------------------------------------------------------------------- */
  38. static int xen_acpi_processor_enable(struct acpi_device *device)
  39. {
  40. acpi_status status = 0;
  41. unsigned long long value;
  42. union acpi_object object = { 0 };
  43. struct acpi_buffer buffer = { sizeof(union acpi_object), &object };
  44. struct acpi_processor *pr;
  45. pr = acpi_driver_data(device);
  46. if (!pr) {
  47. pr_err(PREFIX "Cannot find driver data\n");
  48. return -EINVAL;
  49. }
  50. if (!strcmp(acpi_device_hid(device), ACPI_PROCESSOR_OBJECT_HID)) {
  51. /* Declared with "Processor" statement; match ProcessorID */
  52. status = acpi_evaluate_object(pr->handle, NULL, NULL, &buffer);
  53. if (ACPI_FAILURE(status)) {
  54. pr_err(PREFIX "Evaluating processor object\n");
  55. return -ENODEV;
  56. }
  57. pr->acpi_id = object.processor.proc_id;
  58. } else {
  59. /* Declared with "Device" statement; match _UID */
  60. status = acpi_evaluate_integer(pr->handle, METHOD_NAME__UID,
  61. NULL, &value);
  62. if (ACPI_FAILURE(status)) {
  63. pr_err(PREFIX "Evaluating processor _UID\n");
  64. return -ENODEV;
  65. }
  66. pr->acpi_id = value;
  67. }
  68. pr->id = xen_pcpu_id(pr->acpi_id);
  69. if ((int)pr->id < 0)
  70. /* This cpu is not presented at hypervisor, try to hotadd it */
  71. if (ACPI_FAILURE(xen_acpi_cpu_hotadd(pr))) {
  72. pr_err(PREFIX "Hotadd CPU (acpi_id = %d) failed.\n",
  73. pr->acpi_id);
  74. return -ENODEV;
  75. }
  76. return 0;
  77. }
  78. static int xen_acpi_processor_add(struct acpi_device *device)
  79. {
  80. int ret;
  81. struct acpi_processor *pr;
  82. if (!device)
  83. return -EINVAL;
  84. pr = kzalloc(sizeof(struct acpi_processor), GFP_KERNEL);
  85. if (!pr)
  86. return -ENOMEM;
  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;
  91. ret = xen_acpi_processor_enable(device);
  92. if (ret)
  93. pr_err(PREFIX "Error when enabling Xen processor\n");
  94. return ret;
  95. }
  96. static int xen_acpi_processor_remove(struct acpi_device *device)
  97. {
  98. struct acpi_processor *pr;
  99. if (!device)
  100. return -EINVAL;
  101. pr = acpi_driver_data(device);
  102. if (!pr)
  103. return -EINVAL;
  104. kfree(pr);
  105. return 0;
  106. }
  107. /*--------------------------------------------------------------
  108. Acpi processor hotplug support
  109. --------------------------------------------------------------*/
  110. static int is_processor_present(acpi_handle handle)
  111. {
  112. acpi_status status;
  113. unsigned long long sta = 0;
  114. status = acpi_evaluate_integer(handle, "_STA", NULL, &sta);
  115. if (ACPI_SUCCESS(status) && (sta & ACPI_STA_DEVICE_PRESENT))
  116. return 1;
  117. /*
  118. * _STA is mandatory for a processor that supports hot plug
  119. */
  120. if (status == AE_NOT_FOUND)
  121. pr_info(PREFIX "Processor does not support hot plug\n");
  122. else
  123. pr_info(PREFIX "Processor Device is not present");
  124. return 0;
  125. }
  126. static int xen_apic_id(acpi_handle handle)
  127. {
  128. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  129. union acpi_object *obj;
  130. struct acpi_madt_local_apic *lapic;
  131. int apic_id;
  132. if (ACPI_FAILURE(acpi_evaluate_object(handle, "_MAT", NULL, &buffer)))
  133. return -EINVAL;
  134. if (!buffer.length || !buffer.pointer)
  135. return -EINVAL;
  136. obj = buffer.pointer;
  137. if (obj->type != ACPI_TYPE_BUFFER ||
  138. obj->buffer.length < sizeof(*lapic)) {
  139. kfree(buffer.pointer);
  140. return -EINVAL;
  141. }
  142. lapic = (struct acpi_madt_local_apic *)obj->buffer.pointer;
  143. if (lapic->header.type != ACPI_MADT_TYPE_LOCAL_APIC ||
  144. !(lapic->lapic_flags & ACPI_MADT_ENABLED)) {
  145. kfree(buffer.pointer);
  146. return -EINVAL;
  147. }
  148. apic_id = (uint32_t)lapic->id;
  149. kfree(buffer.pointer);
  150. buffer.length = ACPI_ALLOCATE_BUFFER;
  151. buffer.pointer = NULL;
  152. return apic_id;
  153. }
  154. static int xen_hotadd_cpu(struct acpi_processor *pr)
  155. {
  156. int cpu_id, apic_id, pxm;
  157. struct xen_platform_op op;
  158. apic_id = xen_apic_id(pr->handle);
  159. if (apic_id < 0) {
  160. pr_err(PREFIX "Failed to get apic_id for acpi_id %d\n",
  161. pr->acpi_id);
  162. return -ENODEV;
  163. }
  164. pxm = xen_acpi_get_pxm(pr->handle);
  165. if (pxm < 0) {
  166. pr_err(PREFIX "Failed to get _PXM for acpi_id %d\n",
  167. pr->acpi_id);
  168. return pxm;
  169. }
  170. op.cmd = XENPF_cpu_hotadd;
  171. op.u.cpu_add.apic_id = apic_id;
  172. op.u.cpu_add.acpi_id = pr->acpi_id;
  173. op.u.cpu_add.pxm = pxm;
  174. cpu_id = HYPERVISOR_dom0_op(&op);
  175. if (cpu_id < 0)
  176. pr_err(PREFIX "Failed to hotadd CPU for acpi_id %d\n",
  177. pr->acpi_id);
  178. return cpu_id;
  179. }
  180. static acpi_status xen_acpi_cpu_hotadd(struct acpi_processor *pr)
  181. {
  182. if (!is_processor_present(pr->handle))
  183. return AE_ERROR;
  184. pr->id = xen_hotadd_cpu(pr);
  185. if ((int)pr->id < 0)
  186. return AE_ERROR;
  187. /*
  188. * Sync with Xen hypervisor, providing new /sys/.../xen_cpuX
  189. * interface after cpu hotadded.
  190. */
  191. xen_pcpu_hotplug_sync();
  192. return AE_OK;
  193. }
  194. static int acpi_processor_device_remove(struct acpi_device *device)
  195. {
  196. pr_debug(PREFIX "Xen does not support CPU hotremove\n");
  197. return -ENOSYS;
  198. }
  199. static void acpi_processor_hotplug_notify(acpi_handle handle,
  200. u32 event, void *data)
  201. {
  202. struct acpi_processor *pr;
  203. struct acpi_device *device = NULL;
  204. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  205. int result;
  206. acpi_scan_lock_acquire();
  207. switch (event) {
  208. case ACPI_NOTIFY_BUS_CHECK:
  209. case ACPI_NOTIFY_DEVICE_CHECK:
  210. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  211. "Processor driver received %s event\n",
  212. (event == ACPI_NOTIFY_BUS_CHECK) ?
  213. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  214. if (!is_processor_present(handle))
  215. break;
  216. if (!acpi_bus_get_device(handle, &device))
  217. break;
  218. result = acpi_bus_scan(handle);
  219. if (result) {
  220. pr_err(PREFIX "Unable to add the device\n");
  221. break;
  222. }
  223. result = acpi_bus_get_device(handle, &device);
  224. if (result) {
  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_hotplug_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");