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