xen-acpi-memhotplug.c 11 KB

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