acpi_memhotplug.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2004, 2013 Intel Corporation
  4. * Author: Naveen B S <[email protected]>
  5. * Author: Rafael J. Wysocki <[email protected]>
  6. *
  7. * All rights reserved.
  8. *
  9. * ACPI based HotPlug driver that supports Memory Hotplug
  10. * This driver fields notifications from firmware for memory add
  11. * and remove operations and alerts the VM of the affected memory
  12. * ranges.
  13. */
  14. #include <linux/acpi.h>
  15. #include <linux/memory.h>
  16. #include <linux/memory_hotplug.h>
  17. #include "internal.h"
  18. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  19. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  20. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  21. static const struct acpi_device_id memory_device_ids[] = {
  22. {ACPI_MEMORY_DEVICE_HID, 0},
  23. {"", 0},
  24. };
  25. #ifdef CONFIG_ACPI_HOTPLUG_MEMORY
  26. static int acpi_memory_device_add(struct acpi_device *device,
  27. const struct acpi_device_id *not_used);
  28. static void acpi_memory_device_remove(struct acpi_device *device);
  29. static struct acpi_scan_handler memory_device_handler = {
  30. .ids = memory_device_ids,
  31. .attach = acpi_memory_device_add,
  32. .detach = acpi_memory_device_remove,
  33. .hotplug = {
  34. .enabled = true,
  35. },
  36. };
  37. struct acpi_memory_info {
  38. struct list_head list;
  39. u64 start_addr; /* Memory Range start physical addr */
  40. u64 length; /* Memory Range length */
  41. unsigned short caching; /* memory cache attribute */
  42. unsigned short write_protect; /* memory read/write attribute */
  43. unsigned int enabled:1;
  44. };
  45. struct acpi_memory_device {
  46. struct acpi_device *device;
  47. struct list_head res_list;
  48. int mgid;
  49. };
  50. static acpi_status
  51. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  52. {
  53. struct acpi_memory_device *mem_device = context;
  54. struct acpi_resource_address64 address64;
  55. struct acpi_memory_info *info, *new;
  56. acpi_status status;
  57. status = acpi_resource_to_address64(resource, &address64);
  58. if (ACPI_FAILURE(status) ||
  59. (address64.resource_type != ACPI_MEMORY_RANGE))
  60. return AE_OK;
  61. list_for_each_entry(info, &mem_device->res_list, list) {
  62. /* Can we combine the resource range information? */
  63. if ((info->caching == address64.info.mem.caching) &&
  64. (info->write_protect == address64.info.mem.write_protect) &&
  65. (info->start_addr + info->length == address64.address.minimum)) {
  66. info->length += address64.address.address_length;
  67. return AE_OK;
  68. }
  69. }
  70. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  71. if (!new)
  72. return AE_ERROR;
  73. INIT_LIST_HEAD(&new->list);
  74. new->caching = address64.info.mem.caching;
  75. new->write_protect = address64.info.mem.write_protect;
  76. new->start_addr = address64.address.minimum;
  77. new->length = address64.address.address_length;
  78. list_add_tail(&new->list, &mem_device->res_list);
  79. return AE_OK;
  80. }
  81. static void
  82. acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
  83. {
  84. struct acpi_memory_info *info, *n;
  85. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  86. kfree(info);
  87. INIT_LIST_HEAD(&mem_device->res_list);
  88. }
  89. static int
  90. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  91. {
  92. acpi_status status;
  93. if (!list_empty(&mem_device->res_list))
  94. return 0;
  95. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  96. acpi_memory_get_resource, mem_device);
  97. if (ACPI_FAILURE(status)) {
  98. acpi_memory_free_device_resources(mem_device);
  99. return -EINVAL;
  100. }
  101. return 0;
  102. }
  103. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  104. {
  105. unsigned long long current_status;
  106. /* Get device present/absent information from the _STA */
  107. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
  108. METHOD_NAME__STA, NULL,
  109. &current_status)))
  110. return -ENODEV;
  111. /*
  112. * Check for device status. Device should be
  113. * present/enabled/functioning.
  114. */
  115. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  116. && (current_status & ACPI_STA_DEVICE_ENABLED)
  117. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  118. return -ENODEV;
  119. return 0;
  120. }
  121. static int acpi_bind_memblk(struct memory_block *mem, void *arg)
  122. {
  123. return acpi_bind_one(&mem->dev, arg);
  124. }
  125. static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
  126. struct acpi_device *adev)
  127. {
  128. return walk_memory_blocks(info->start_addr, info->length, adev,
  129. acpi_bind_memblk);
  130. }
  131. static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
  132. {
  133. acpi_unbind_one(&mem->dev);
  134. return 0;
  135. }
  136. static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
  137. {
  138. walk_memory_blocks(info->start_addr, info->length, NULL,
  139. acpi_unbind_memblk);
  140. }
  141. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  142. {
  143. acpi_handle handle = mem_device->device->handle;
  144. mhp_t mhp_flags = MHP_NID_IS_MGID;
  145. int result, num_enabled = 0;
  146. struct acpi_memory_info *info;
  147. u64 total_length = 0;
  148. int node, mgid;
  149. node = acpi_get_node(handle);
  150. list_for_each_entry(info, &mem_device->res_list, list) {
  151. if (!info->length)
  152. continue;
  153. /* We want a single node for the whole memory group */
  154. if (node < 0)
  155. node = memory_add_physaddr_to_nid(info->start_addr);
  156. total_length += info->length;
  157. }
  158. if (!total_length) {
  159. dev_err(&mem_device->device->dev, "device is empty\n");
  160. return -EINVAL;
  161. }
  162. mgid = memory_group_register_static(node, PFN_UP(total_length));
  163. if (mgid < 0)
  164. return mgid;
  165. mem_device->mgid = mgid;
  166. /*
  167. * Tell the VM there is more memory here...
  168. * Note: Assume that this function returns zero on success
  169. * We don't have memory-hot-add rollback function,now.
  170. * (i.e. memory-hot-remove function)
  171. */
  172. list_for_each_entry(info, &mem_device->res_list, list) {
  173. /*
  174. * If the memory block size is zero, please ignore it.
  175. * Don't try to do the following memory hotplug flowchart.
  176. */
  177. if (!info->length)
  178. continue;
  179. if (mhp_supports_memmap_on_memory(info->length))
  180. mhp_flags |= MHP_MEMMAP_ON_MEMORY;
  181. result = __add_memory(mgid, info->start_addr, info->length,
  182. mhp_flags);
  183. /*
  184. * If the memory block has been used by the kernel, add_memory()
  185. * returns -EEXIST. If add_memory() returns the other error, it
  186. * means that this memory block is not used by the kernel.
  187. */
  188. if (result && result != -EEXIST)
  189. continue;
  190. result = acpi_bind_memory_blocks(info, mem_device->device);
  191. if (result) {
  192. acpi_unbind_memory_blocks(info);
  193. return -ENODEV;
  194. }
  195. info->enabled = 1;
  196. /*
  197. * Add num_enable even if add_memory() returns -EEXIST, so the
  198. * device is bound to this driver.
  199. */
  200. num_enabled++;
  201. }
  202. if (!num_enabled) {
  203. dev_err(&mem_device->device->dev, "add_memory failed\n");
  204. return -EINVAL;
  205. }
  206. /*
  207. * Sometimes the memory device will contain several memory blocks.
  208. * When one memory block is hot-added to the system memory, it will
  209. * be regarded as a success.
  210. * Otherwise if the last memory block can't be hot-added to the system
  211. * memory, it will be failure and the memory device can't be bound with
  212. * driver.
  213. */
  214. return 0;
  215. }
  216. static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  217. {
  218. struct acpi_memory_info *info, *n;
  219. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  220. if (!info->enabled)
  221. continue;
  222. acpi_unbind_memory_blocks(info);
  223. __remove_memory(info->start_addr, info->length);
  224. list_del(&info->list);
  225. kfree(info);
  226. }
  227. }
  228. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  229. {
  230. if (!mem_device)
  231. return;
  232. /* In case we succeeded adding *some* memory, unregistering fails. */
  233. if (mem_device->mgid >= 0)
  234. memory_group_unregister(mem_device->mgid);
  235. acpi_memory_free_device_resources(mem_device);
  236. mem_device->device->driver_data = NULL;
  237. kfree(mem_device);
  238. }
  239. static int acpi_memory_device_add(struct acpi_device *device,
  240. const struct acpi_device_id *not_used)
  241. {
  242. struct acpi_memory_device *mem_device;
  243. int result;
  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. mem_device->mgid = -1;
  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. device->driver_data = NULL;
  259. kfree(mem_device);
  260. return result;
  261. }
  262. result = acpi_memory_check_device(mem_device);
  263. if (result) {
  264. acpi_memory_device_free(mem_device);
  265. return 0;
  266. }
  267. result = acpi_memory_enable_device(mem_device);
  268. if (result) {
  269. dev_err(&device->dev, "acpi_memory_enable_device() error\n");
  270. acpi_memory_device_free(mem_device);
  271. return result;
  272. }
  273. dev_dbg(&device->dev, "Memory device configured by ACPI\n");
  274. return 1;
  275. }
  276. static void acpi_memory_device_remove(struct acpi_device *device)
  277. {
  278. struct acpi_memory_device *mem_device;
  279. if (!device || !acpi_driver_data(device))
  280. return;
  281. mem_device = acpi_driver_data(device);
  282. acpi_memory_remove_memory(mem_device);
  283. acpi_memory_device_free(mem_device);
  284. }
  285. static bool __initdata acpi_no_memhotplug;
  286. void __init acpi_memory_hotplug_init(void)
  287. {
  288. if (acpi_no_memhotplug) {
  289. memory_device_handler.attach = NULL;
  290. acpi_scan_add_handler(&memory_device_handler);
  291. return;
  292. }
  293. acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
  294. }
  295. static int __init disable_acpi_memory_hotplug(char *str)
  296. {
  297. acpi_no_memhotplug = true;
  298. return 1;
  299. }
  300. __setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
  301. #else
  302. static struct acpi_scan_handler memory_device_handler = {
  303. .ids = memory_device_ids,
  304. };
  305. void __init acpi_memory_hotplug_init(void)
  306. {
  307. acpi_scan_add_handler(&memory_device_handler);
  308. }
  309. #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */