xen-acpi-memhotplug.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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/acpi.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.address.minimum)) {
  100. info->length += address64.address.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.address.minimum;
  111. new->length = address64.address.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 acpi_memory_get_device(acpi_handle handle,
  133. struct acpi_memory_device **mem_device)
  134. {
  135. struct acpi_device *device = NULL;
  136. int result = 0;
  137. acpi_scan_lock_acquire();
  138. acpi_bus_get_device(handle, &device);
  139. if (acpi_device_enumerated(device))
  140. goto end;
  141. /*
  142. * Now add the notified device. This creates the acpi_device
  143. * and invokes .add function
  144. */
  145. result = acpi_bus_scan(handle);
  146. if (result) {
  147. pr_warn(PREFIX "ACPI namespace scan failed\n");
  148. result = -EINVAL;
  149. goto out;
  150. }
  151. device = NULL;
  152. acpi_bus_get_device(handle, &device);
  153. if (!acpi_device_enumerated(device)) {
  154. pr_warn(PREFIX "Missing device object\n");
  155. result = -EINVAL;
  156. goto out;
  157. }
  158. end:
  159. *mem_device = acpi_driver_data(device);
  160. if (!(*mem_device)) {
  161. pr_err(PREFIX "driver data not found\n");
  162. result = -ENODEV;
  163. goto out;
  164. }
  165. out:
  166. acpi_scan_lock_release();
  167. return result;
  168. }
  169. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  170. {
  171. unsigned long long current_status;
  172. /* Get device present/absent information from the _STA */
  173. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
  174. "_STA", NULL, &current_status)))
  175. return -ENODEV;
  176. /*
  177. * Check for device status. Device should be
  178. * present/enabled/functioning.
  179. */
  180. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  181. && (current_status & ACPI_STA_DEVICE_ENABLED)
  182. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  183. return -ENODEV;
  184. return 0;
  185. }
  186. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  187. {
  188. pr_debug(PREFIX "Xen does not support memory hotremove\n");
  189. return -ENOSYS;
  190. }
  191. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  192. {
  193. struct acpi_memory_device *mem_device;
  194. struct acpi_device *device;
  195. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  196. switch (event) {
  197. case ACPI_NOTIFY_BUS_CHECK:
  198. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  199. "\nReceived BUS CHECK notification for device\n"));
  200. /* Fall Through */
  201. case ACPI_NOTIFY_DEVICE_CHECK:
  202. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  203. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  204. "\nReceived DEVICE CHECK notification for device\n"));
  205. if (acpi_memory_get_device(handle, &mem_device)) {
  206. pr_err(PREFIX "Cannot find driver data\n");
  207. break;
  208. }
  209. ost_code = ACPI_OST_SC_SUCCESS;
  210. break;
  211. case ACPI_NOTIFY_EJECT_REQUEST:
  212. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  213. "\nReceived EJECT REQUEST notification for device\n"));
  214. acpi_scan_lock_acquire();
  215. if (acpi_bus_get_device(handle, &device)) {
  216. acpi_scan_lock_release();
  217. pr_err(PREFIX "Device doesn't exist\n");
  218. break;
  219. }
  220. mem_device = acpi_driver_data(device);
  221. if (!mem_device) {
  222. acpi_scan_lock_release();
  223. pr_err(PREFIX "Driver Data is NULL\n");
  224. break;
  225. }
  226. /*
  227. * TBD: implement acpi_memory_disable_device and invoke
  228. * acpi_bus_remove if Xen support hotremove in the future
  229. */
  230. acpi_memory_disable_device(mem_device);
  231. acpi_scan_lock_release();
  232. break;
  233. default:
  234. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  235. "Unsupported event [0x%x]\n", event));
  236. /* non-hotplug event; possibly handled by other handler */
  237. return;
  238. }
  239. (void) acpi_evaluate_ost(handle, event, ost_code, NULL);
  240. return;
  241. }
  242. static int xen_acpi_memory_device_add(struct acpi_device *device)
  243. {
  244. int result;
  245. struct acpi_memory_device *mem_device = NULL;
  246. if (!device)
  247. return -EINVAL;
  248. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  249. if (!mem_device)
  250. return -ENOMEM;
  251. INIT_LIST_HEAD(&mem_device->res_list);
  252. mem_device->device = device;
  253. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  254. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  255. device->driver_data = mem_device;
  256. /* Get the range from the _CRS */
  257. result = acpi_memory_get_device_resources(mem_device);
  258. if (result) {
  259. kfree(mem_device);
  260. return result;
  261. }
  262. /*
  263. * For booting existed memory devices, early boot code has recognized
  264. * memory area by EFI/E820. If DSDT shows these memory devices on boot,
  265. * hotplug is not necessary for them.
  266. * For hot-added memory devices during runtime, it need hypercall to
  267. * Xen hypervisor to add memory.
  268. */
  269. if (!acpi_hotmem_initialized)
  270. return 0;
  271. if (!acpi_memory_check_device(mem_device))
  272. result = xen_acpi_memory_enable_device(mem_device);
  273. return result;
  274. }
  275. static int xen_acpi_memory_device_remove(struct acpi_device *device)
  276. {
  277. struct acpi_memory_device *mem_device = NULL;
  278. if (!device || !acpi_driver_data(device))
  279. return -EINVAL;
  280. mem_device = acpi_driver_data(device);
  281. kfree(mem_device);
  282. return 0;
  283. }
  284. /*
  285. * Helper function to check for memory device
  286. */
  287. static acpi_status is_memory_device(acpi_handle handle)
  288. {
  289. char *hardware_id;
  290. acpi_status status;
  291. struct acpi_device_info *info;
  292. status = acpi_get_object_info(handle, &info);
  293. if (ACPI_FAILURE(status))
  294. return status;
  295. if (!(info->valid & ACPI_VALID_HID)) {
  296. kfree(info);
  297. return AE_ERROR;
  298. }
  299. hardware_id = info->hardware_id.string;
  300. if ((hardware_id == NULL) ||
  301. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  302. status = AE_ERROR;
  303. kfree(info);
  304. return status;
  305. }
  306. static acpi_status
  307. acpi_memory_register_notify_handler(acpi_handle handle,
  308. u32 level, void *ctxt, void **retv)
  309. {
  310. acpi_status status;
  311. status = is_memory_device(handle);
  312. if (ACPI_FAILURE(status))
  313. return AE_OK; /* continue */
  314. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  315. acpi_memory_device_notify, NULL);
  316. /* continue */
  317. return AE_OK;
  318. }
  319. static acpi_status
  320. acpi_memory_deregister_notify_handler(acpi_handle handle,
  321. u32 level, void *ctxt, void **retv)
  322. {
  323. acpi_status status;
  324. status = is_memory_device(handle);
  325. if (ACPI_FAILURE(status))
  326. return AE_OK; /* continue */
  327. status = acpi_remove_notify_handler(handle,
  328. ACPI_SYSTEM_NOTIFY,
  329. acpi_memory_device_notify);
  330. return AE_OK; /* continue */
  331. }
  332. static const struct acpi_device_id memory_device_ids[] = {
  333. {ACPI_MEMORY_DEVICE_HID, 0},
  334. {"", 0},
  335. };
  336. MODULE_DEVICE_TABLE(acpi, memory_device_ids);
  337. static struct acpi_driver xen_acpi_memory_device_driver = {
  338. .name = "acpi_memhotplug",
  339. .class = ACPI_MEMORY_DEVICE_CLASS,
  340. .ids = memory_device_ids,
  341. .ops = {
  342. .add = xen_acpi_memory_device_add,
  343. .remove = xen_acpi_memory_device_remove,
  344. },
  345. };
  346. static int __init xen_acpi_memory_device_init(void)
  347. {
  348. int result;
  349. acpi_status status;
  350. if (!xen_initial_domain())
  351. return -ENODEV;
  352. /* unregister the stub which only used to reserve driver space */
  353. xen_stub_memory_device_exit();
  354. result = acpi_bus_register_driver(&xen_acpi_memory_device_driver);
  355. if (result < 0) {
  356. xen_stub_memory_device_init();
  357. return -ENODEV;
  358. }
  359. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  360. ACPI_UINT32_MAX,
  361. acpi_memory_register_notify_handler,
  362. NULL, NULL, NULL);
  363. if (ACPI_FAILURE(status)) {
  364. pr_warn(PREFIX "walk_namespace failed\n");
  365. acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
  366. xen_stub_memory_device_init();
  367. return -ENODEV;
  368. }
  369. acpi_hotmem_initialized = true;
  370. return 0;
  371. }
  372. static void __exit xen_acpi_memory_device_exit(void)
  373. {
  374. acpi_status status;
  375. if (!xen_initial_domain())
  376. return;
  377. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  378. ACPI_UINT32_MAX,
  379. acpi_memory_deregister_notify_handler,
  380. NULL, NULL, NULL);
  381. if (ACPI_FAILURE(status))
  382. pr_warn(PREFIX "walk_namespace failed\n");
  383. acpi_bus_unregister_driver(&xen_acpi_memory_device_driver);
  384. /*
  385. * stub reserve space again to prevent any chance of native
  386. * driver loading.
  387. */
  388. xen_stub_memory_device_init();
  389. return;
  390. }
  391. module_init(xen_acpi_memory_device_init);
  392. module_exit(xen_acpi_memory_device_exit);
  393. ACPI_MODULE_NAME("xen-acpi-memhotplug");
  394. MODULE_AUTHOR("Liu Jinsong <jinsong.liu@intel.com>");
  395. MODULE_DESCRIPTION("Xen Hotplug Mem Driver");
  396. MODULE_LICENSE("GPL");