xen-acpi-cpuhotplug.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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 int acpi_processor_device_remove(struct acpi_device *device)
  194. {
  195. pr_debug(PREFIX "Xen does not support CPU hotremove\n");
  196. return -ENOSYS;
  197. }
  198. static void acpi_processor_hotplug_notify(acpi_handle handle,
  199. u32 event, void *data)
  200. {
  201. struct acpi_processor *pr;
  202. struct acpi_device *device = NULL;
  203. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  204. int result;
  205. acpi_scan_lock_acquire();
  206. switch (event) {
  207. case ACPI_NOTIFY_BUS_CHECK:
  208. case ACPI_NOTIFY_DEVICE_CHECK:
  209. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  210. "Processor driver received %s event\n",
  211. (event == ACPI_NOTIFY_BUS_CHECK) ?
  212. "ACPI_NOTIFY_BUS_CHECK" : "ACPI_NOTIFY_DEVICE_CHECK"));
  213. if (!is_processor_present(handle))
  214. break;
  215. if (!acpi_bus_get_device(handle, &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. result = acpi_bus_get_device(handle, &device);
  223. if (result) {
  224. pr_err(PREFIX "Missing device object\n");
  225. break;
  226. }
  227. ost_code = ACPI_OST_SC_SUCCESS;
  228. break;
  229. case ACPI_NOTIFY_EJECT_REQUEST:
  230. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  231. "received ACPI_NOTIFY_EJECT_REQUEST\n"));
  232. if (acpi_bus_get_device(handle, &device)) {
  233. pr_err(PREFIX "Device don't exist, dropping EJECT\n");
  234. break;
  235. }
  236. pr = acpi_driver_data(device);
  237. if (!pr) {
  238. pr_err(PREFIX "Driver data is NULL, dropping EJECT\n");
  239. break;
  240. }
  241. /*
  242. * TBD: implement acpi_processor_device_remove if Xen support
  243. * CPU hotremove in the future.
  244. */
  245. acpi_processor_device_remove(device);
  246. break;
  247. default:
  248. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  249. "Unsupported event [0x%x]\n", event));
  250. /* non-hotplug event; possibly handled by other handler */
  251. goto out;
  252. }
  253. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  254. out:
  255. acpi_scan_lock_release();
  256. }
  257. static acpi_status is_processor_device(acpi_handle handle)
  258. {
  259. struct acpi_device_info *info;
  260. char *hid;
  261. acpi_status status;
  262. status = acpi_get_object_info(handle, &info);
  263. if (ACPI_FAILURE(status))
  264. return status;
  265. if (info->type == ACPI_TYPE_PROCESSOR) {
  266. kfree(info);
  267. return AE_OK; /* found a processor object */
  268. }
  269. if (!(info->valid & ACPI_VALID_HID)) {
  270. kfree(info);
  271. return AE_ERROR;
  272. }
  273. hid = info->hardware_id.string;
  274. if ((hid == NULL) || strcmp(hid, ACPI_PROCESSOR_DEVICE_HID)) {
  275. kfree(info);
  276. return AE_ERROR;
  277. }
  278. kfree(info);
  279. return AE_OK; /* found a processor device object */
  280. }
  281. static acpi_status
  282. processor_walk_namespace_cb(acpi_handle handle,
  283. u32 lvl, void *context, void **rv)
  284. {
  285. acpi_status status;
  286. int *action = context;
  287. status = is_processor_device(handle);
  288. if (ACPI_FAILURE(status))
  289. return AE_OK; /* not a processor; continue to walk */
  290. switch (*action) {
  291. case INSTALL_NOTIFY_HANDLER:
  292. acpi_install_notify_handler(handle,
  293. ACPI_SYSTEM_NOTIFY,
  294. acpi_processor_hotplug_notify,
  295. NULL);
  296. break;
  297. case UNINSTALL_NOTIFY_HANDLER:
  298. acpi_remove_notify_handler(handle,
  299. ACPI_SYSTEM_NOTIFY,
  300. acpi_processor_hotplug_notify);
  301. break;
  302. default:
  303. break;
  304. }
  305. /* found a processor; skip walking underneath */
  306. return AE_CTRL_DEPTH;
  307. }
  308. static
  309. void acpi_processor_install_hotplug_notify(void)
  310. {
  311. int action = INSTALL_NOTIFY_HANDLER;
  312. acpi_walk_namespace(ACPI_TYPE_ANY,
  313. ACPI_ROOT_OBJECT,
  314. ACPI_UINT32_MAX,
  315. processor_walk_namespace_cb, NULL, &action, NULL);
  316. }
  317. static
  318. void acpi_processor_uninstall_hotplug_notify(void)
  319. {
  320. int action = UNINSTALL_NOTIFY_HANDLER;
  321. acpi_walk_namespace(ACPI_TYPE_ANY,
  322. ACPI_ROOT_OBJECT,
  323. ACPI_UINT32_MAX,
  324. processor_walk_namespace_cb, NULL, &action, NULL);
  325. }
  326. static const struct acpi_device_id processor_device_ids[] = {
  327. {ACPI_PROCESSOR_OBJECT_HID, 0},
  328. {ACPI_PROCESSOR_DEVICE_HID, 0},
  329. {"", 0},
  330. };
  331. MODULE_DEVICE_TABLE(acpi, processor_device_ids);
  332. static struct acpi_driver xen_acpi_processor_driver = {
  333. .name = "processor",
  334. .class = ACPI_PROCESSOR_CLASS,
  335. .ids = processor_device_ids,
  336. .ops = {
  337. .add = xen_acpi_processor_add,
  338. .remove = xen_acpi_processor_remove,
  339. },
  340. };
  341. static int __init xen_acpi_processor_init(void)
  342. {
  343. int result = 0;
  344. if (!xen_initial_domain())
  345. return -ENODEV;
  346. /* unregister the stub which only used to reserve driver space */
  347. xen_stub_processor_exit();
  348. result = acpi_bus_register_driver(&xen_acpi_processor_driver);
  349. if (result < 0) {
  350. xen_stub_processor_init();
  351. return result;
  352. }
  353. acpi_processor_install_hotplug_notify();
  354. return 0;
  355. }
  356. static void __exit xen_acpi_processor_exit(void)
  357. {
  358. if (!xen_initial_domain())
  359. return;
  360. acpi_processor_uninstall_hotplug_notify();
  361. acpi_bus_unregister_driver(&xen_acpi_processor_driver);
  362. /*
  363. * stub reserve space again to prevent any chance of native
  364. * driver loading.
  365. */
  366. xen_stub_processor_init();
  367. return;
  368. }
  369. module_init(xen_acpi_processor_init);
  370. module_exit(xen_acpi_processor_exit);
  371. ACPI_MODULE_NAME("xen-acpi-cpuhotplug");
  372. MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
  373. MODULE_DESCRIPTION("Xen Hotplug CPU Driver");
  374. MODULE_LICENSE("GPL");