xen-acpi-memhotplug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483
  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/acpi.h>
  22. #include <acpi/acpi_drivers.h>
  23. #include <xen/acpi.h>
  24. #include <xen/interface/platform.h>
  25. #include <asm/xen/hypercall.h>
  26. #define PREFIX "ACPI:xen_memory_hotplug:"
  27. struct acpi_memory_info {
  28. struct list_head list;
  29. u64 start_addr; /* Memory Range start physical addr */
  30. u64 length; /* Memory Range length */
  31. unsigned short caching; /* memory cache attribute */
  32. unsigned short write_protect; /* memory read/write attribute */
  33. /* copied from buffer getting from _CRS */
  34. unsigned int enabled:1;
  35. };
  36. struct acpi_memory_device {
  37. struct acpi_device *device;
  38. struct list_head res_list;
  39. };
  40. static bool acpi_hotmem_initialized __read_mostly;
  41. static int xen_hotadd_memory(int pxm, struct acpi_memory_info *info)
  42. {
  43. int rc;
  44. struct xen_platform_op op;
  45. op.cmd = XENPF_mem_hotadd;
  46. op.u.mem_add.spfn = info->start_addr >> PAGE_SHIFT;
  47. op.u.mem_add.epfn = (info->start_addr + info->length) >> PAGE_SHIFT;
  48. op.u.mem_add.pxm = pxm;
  49. rc = HYPERVISOR_dom0_op(&op);
  50. if (rc)
  51. pr_err(PREFIX "Xen Hotplug Memory Add failed on "
  52. "0x%lx -> 0x%lx, _PXM: %d, error: %d\n",
  53. (unsigned long)info->start_addr,
  54. (unsigned long)(info->start_addr + info->length),
  55. pxm, rc);
  56. return rc;
  57. }
  58. static int xen_acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  59. {
  60. int pxm, result;
  61. int num_enabled = 0;
  62. struct acpi_memory_info *info;
  63. if (!mem_device)
  64. return -EINVAL;
  65. pxm = xen_acpi_get_pxm(mem_device->device->handle);
  66. if (pxm < 0)
  67. return pxm;
  68. list_for_each_entry(info, &mem_device->res_list, list) {
  69. if (info->enabled) { /* just sanity check...*/
  70. num_enabled++;
  71. continue;
  72. }
  73. if (!info->length)
  74. continue;
  75. result = xen_hotadd_memory(pxm, info);
  76. if (result)
  77. continue;
  78. info->enabled = 1;
  79. num_enabled++;
  80. }
  81. if (!num_enabled)
  82. return -ENODEV;
  83. return 0;
  84. }
  85. static acpi_status
  86. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  87. {
  88. struct acpi_memory_device *mem_device = context;
  89. struct acpi_resource_address64 address64;
  90. struct acpi_memory_info *info, *new;
  91. acpi_status status;
  92. status = acpi_resource_to_address64(resource, &address64);
  93. if (ACPI_FAILURE(status) ||
  94. (address64.resource_type != ACPI_MEMORY_RANGE))
  95. return AE_OK;
  96. list_for_each_entry(info, &mem_device->res_list, list) {
  97. if ((info->caching == address64.info.mem.caching) &&
  98. (info->write_protect == address64.info.mem.write_protect) &&
  99. (info->start_addr + info->length == address64.minimum)) {
  100. info->length += address64.address_length;
  101. return AE_OK;
  102. }
  103. }
  104. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  105. if (!new)
  106. return AE_ERROR;
  107. INIT_LIST_HEAD(&new->list);
  108. new->caching = address64.info.mem.caching;
  109. new->write_protect = address64.info.mem.write_protect;
  110. new->start_addr = address64.minimum;
  111. new->length = address64.address_length;
  112. list_add_tail(&new->list, &mem_device->res_list);
  113. return AE_OK;
  114. }
  115. static int
  116. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  117. {
  118. acpi_status status;
  119. struct acpi_memory_info *info, *n;
  120. if (!list_empty(&mem_device->res_list))
  121. return 0;
  122. status = acpi_walk_resources(mem_device->device->handle,
  123. METHOD_NAME__CRS, acpi_memory_get_resource, mem_device);
  124. if (ACPI_FAILURE(status)) {
  125. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  126. kfree(info);
  127. INIT_LIST_HEAD(&mem_device->res_list);
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static int
  133. acpi_memory_get_device(acpi_handle handle,
  134. struct acpi_memory_device **mem_device)
  135. {
  136. acpi_status status;
  137. acpi_handle phandle;
  138. struct acpi_device *device = NULL;
  139. struct acpi_device *pdevice = NULL;
  140. int result;
  141. if (!acpi_bus_get_device(handle, &device) && device)
  142. goto end;
  143. status = acpi_get_parent(handle, &phandle);
  144. if (ACPI_FAILURE(status)) {
  145. pr_warn(PREFIX "Cannot find acpi parent\n");
  146. return -EINVAL;
  147. }
  148. /* Get the parent device */
  149. result = acpi_bus_get_device(phandle, &pdevice);
  150. if (result) {
  151. pr_warn(PREFIX "Cannot get acpi bus device\n");
  152. return -EINVAL;
  153. }
  154. /*
  155. * Now add the notified device. This creates the acpi_device
  156. * and invokes .add function
  157. */
  158. result = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  159. if (result) {
  160. pr_warn(PREFIX "Cannot add acpi bus\n");
  161. return -EINVAL;
  162. }
  163. end:
  164. *mem_device = acpi_driver_data(device);
  165. if (!(*mem_device)) {
  166. pr_err(PREFIX "Driver data not found\n");
  167. return -ENODEV;
  168. }
  169. return 0;
  170. }
  171. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  172. {
  173. unsigned long long current_status;
  174. /* Get device present/absent information from the _STA */
  175. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
  176. "_STA", NULL, &current_status)))
  177. return -ENODEV;
  178. /*
  179. * Check for device status. Device should be
  180. * present/enabled/functioning.
  181. */
  182. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  183. && (current_status & ACPI_STA_DEVICE_ENABLED)
  184. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  185. return -ENODEV;
  186. return 0;
  187. }
  188. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  189. {
  190. pr_debug(PREFIX "Xen does not support memory hotremove\n");
  191. return -ENOSYS;
  192. }
  193. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  194. {
  195. struct acpi_memory_device *mem_device;
  196. struct acpi_device *device;
  197. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  198. switch (event) {
  199. case ACPI_NOTIFY_BUS_CHECK:
  200. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  201. "\nReceived BUS CHECK notification for device\n"));
  202. /* Fall Through */
  203. case ACPI_NOTIFY_DEVICE_CHECK:
  204. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  205. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  206. "\nReceived DEVICE CHECK notification for device\n"));
  207. if (acpi_memory_get_device(handle, &mem_device)) {
  208. pr_err(PREFIX "Cannot find driver data\n");
  209. break;
  210. }
  211. ost_code = ACPI_OST_SC_SUCCESS;
  212. break;
  213. case ACPI_NOTIFY_EJECT_REQUEST:
  214. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  215. "\nReceived EJECT REQUEST notification for device\n"));
  216. if (acpi_bus_get_device(handle, &device)) {
  217. pr_err(PREFIX "Device doesn't exist\n");
  218. break;
  219. }
  220. mem_device = acpi_driver_data(device);
  221. if (!mem_device) {
  222. pr_err(PREFIX "Driver Data is NULL\n");
  223. break;
  224. }
  225. /*
  226. * TBD: implement acpi_memory_disable_device and invoke
  227. * acpi_bus_remove if Xen support hotremove in the future
  228. */
  229. acpi_memory_disable_device(mem_device);
  230. break;
  231. default:
  232. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  233. "Unsupported event [0x%x]\n", event));
  234. /* non-hotplug event; possibly handled by other handler */
  235. return;
  236. }
  237. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  238. return;
  239. }
  240. static int xen_acpi_memory_device_add(struct acpi_device *device)
  241. {
  242. int result;
  243. struct acpi_memory_device *mem_device = NULL;
  244. if (!device)
  245. return -EINVAL;
  246. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  247. if (!mem_device)
  248. return -ENOMEM;
  249. INIT_LIST_HEAD(&mem_device->res_list);
  250. mem_device->device = device;
  251. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  252. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  253. device->driver_data = mem_device;
  254. /* Get the range from the _CRS */
  255. result = acpi_memory_get_device_resources(mem_device);
  256. if (result) {
  257. kfree(mem_device);
  258. return result;
  259. }
  260. /*
  261. * For booting existed memory devices, early boot code has recognized
  262. * memory area by EFI/E820. If DSDT shows these memory devices on boot,
  263. * hotplug is not necessary for them.
  264. * For hot-added memory devices during runtime, it need hypercall to
  265. * Xen hypervisor to add memory.
  266. */
  267. if (!acpi_hotmem_initialized)
  268. return 0;
  269. if (!acpi_memory_check_device(mem_device))
  270. result = xen_acpi_memory_enable_device(mem_device);
  271. return result;
  272. }
  273. static int xen_acpi_memory_device_remove(struct acpi_device *device, int type)
  274. {
  275. struct acpi_memory_device *mem_device = NULL;
  276. if (!device || !acpi_driver_data(device))
  277. return -EINVAL;
  278. mem_device = acpi_driver_data(device);
  279. kfree(mem_device);
  280. return 0;
  281. }
  282. /*
  283. * Helper function to check for memory device
  284. */
  285. static acpi_status is_memory_device(acpi_handle handle)
  286. {
  287. char *hardware_id;
  288. acpi_status status;
  289. struct acpi_device_info *info;
  290. status = acpi_get_object_info(handle, &info);
  291. if (ACPI_FAILURE(status))
  292. return status;
  293. if (!(info->valid & ACPI_VALID_HID)) {
  294. kfree(info);
  295. return AE_ERROR;
  296. }
  297. hardware_id = info->hardware_id.string;
  298. if ((hardware_id == NULL) ||
  299. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  300. status = AE_ERROR;
  301. kfree(info);
  302. return status;
  303. }
  304. static acpi_status
  305. acpi_memory_register_notify_handler(acpi_handle handle,
  306. u32 level, void *ctxt, void **retv)
  307. {
  308. acpi_status status;
  309. status = is_memory_device(handle);
  310. if (ACPI_FAILURE(status))
  311. return AE_OK; /* continue */
  312. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  313. acpi_memory_device_notify, NULL);
  314. /* continue */
  315. return AE_OK;
  316. }
  317. static acpi_status
  318. acpi_memory_deregister_notify_handler(acpi_handle handle,
  319. u32 level, void *ctxt, void **retv)
  320. {
  321. acpi_status status;
  322. status = is_memory_device(handle);
  323. if (ACPI_FAILURE(status))
  324. return AE_OK; /* continue */
  325. status = acpi_remove_notify_handler(handle,
  326. ACPI_SYSTEM_NOTIFY,
  327. acpi_memory_device_notify);
  328. return AE_OK; /* continue */
  329. }
  330. static const struct acpi_device_id memory_device_ids[] = {
  331. {ACPI_MEMORY_DEVICE_HID, 0},
  332. {"", 0},
  333. };
  334. MODULE_DEVICE_TABLE(acpi, memory_device_ids);
  335. static struct acpi_driver xen_acpi_memory_device_driver = {
  336. .name = "acpi_memhotplug",
  337. .class = ACPI_MEMORY_DEVICE_CLASS,
  338. .ids = memory_device_ids,
  339. .ops = {
  340. .add = xen_acpi_memory_device_add,
  341. .remove = xen_acpi_memory_device_remove,
  342. },
  343. };
  344. static int __init xen_acpi_memory_device_init(void)
  345. {
  346. int result;
  347. acpi_status status;
  348. if (!xen_initial_domain())
  349. return -ENODEV;
  350. /* unregister the stub which only used to reserve driver space */
  351. xen_stub_memory_device_exit();
  352. result = acpi_bus_register_driver(&xen_acpi_memory_device_driver);
  353. if (result < 0) {
  354. xen_stub_memory_device_init();
  355. return -ENODEV;
  356. }
  357. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  358. ACPI_UINT32_MAX,
  359. acpi_memory_register_notify_handler,
  360. NULL, NULL, NULL);
  361. if (ACPI_FAILURE(status)) {
  362. pr_warn(PREFIX "walk_namespace failed\n");
  363. acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
  364. xen_stub_memory_device_init();
  365. return -ENODEV;
  366. }
  367. acpi_hotmem_initialized = true;
  368. return 0;
  369. }
  370. static void __exit xen_acpi_memory_device_exit(void)
  371. {
  372. acpi_status status;
  373. if (!xen_initial_domain())
  374. return;
  375. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  376. ACPI_UINT32_MAX,
  377. acpi_memory_deregister_notify_handler,
  378. NULL, NULL, NULL);
  379. if (ACPI_FAILURE(status))
  380. pr_warn(PREFIX "walk_namespace failed\n");
  381. acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
  382. /*
  383. * stub reserve space again to prevent any chance of native
  384. * driver loading.
  385. */
  386. xen_stub_memory_device_init();
  387. return;
  388. }
  389. module_init(xen_acpi_memory_device_init);
  390. module_exit(xen_acpi_memory_device_exit);
  391. ACPI_MODULE_NAME("xen-acpi-memhotplug");
  392. MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
  393. MODULE_DESCRIPTION("Xen Hotplug Mem Driver");
  394. MODULE_LICENSE("GPL");