acpi_memhotplug.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (C) 2004, 2013 Intel Corporation
  4. * Author: Naveen B S <naveen.b.s@intel.com>
  5. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  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_obj(struct acpi_memory_info);
  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. mhp_flags |= MHP_MEMMAP_ON_MEMORY;
  180. result = __add_memory(mgid, info->start_addr, info->length,
  181. mhp_flags);
  182. /*
  183. * If the memory block has been used by the kernel, add_memory()
  184. * returns -EEXIST. If add_memory() returns the other error, it
  185. * means that this memory block is not used by the kernel.
  186. */
  187. if (result && result != -EEXIST)
  188. continue;
  189. result = acpi_bind_memory_blocks(info, mem_device->device);
  190. if (result) {
  191. acpi_unbind_memory_blocks(info);
  192. return -ENODEV;
  193. }
  194. info->enabled = 1;
  195. /*
  196. * Add num_enable even if add_memory() returns -EEXIST, so the
  197. * device is bound to this driver.
  198. */
  199. num_enabled++;
  200. }
  201. if (!num_enabled) {
  202. dev_err(&mem_device->device->dev, "add_memory failed\n");
  203. return -EINVAL;
  204. }
  205. /*
  206. * Sometimes the memory device will contain several memory blocks.
  207. * When one memory block is hot-added to the system memory, it will
  208. * be regarded as a success.
  209. * Otherwise if the last memory block can't be hot-added to the system
  210. * memory, it will be failure and the memory device can't be bound with
  211. * driver.
  212. */
  213. return 0;
  214. }
  215. static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  216. {
  217. struct acpi_memory_info *info, *n;
  218. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  219. if (!info->enabled)
  220. continue;
  221. acpi_unbind_memory_blocks(info);
  222. __remove_memory(info->start_addr, info->length);
  223. list_del(&info->list);
  224. kfree(info);
  225. }
  226. }
  227. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  228. {
  229. if (!mem_device)
  230. return;
  231. /* In case we succeeded adding *some* memory, unregistering fails. */
  232. if (mem_device->mgid >= 0)
  233. memory_group_unregister(mem_device->mgid);
  234. acpi_memory_free_device_resources(mem_device);
  235. mem_device->device->driver_data = NULL;
  236. kfree(mem_device);
  237. }
  238. static int acpi_memory_device_add(struct acpi_device *device,
  239. const struct acpi_device_id *not_used)
  240. {
  241. struct acpi_memory_device *mem_device;
  242. int result;
  243. if (!device)
  244. return -EINVAL;
  245. mem_device = kzalloc_obj(struct acpi_memory_device);
  246. if (!mem_device)
  247. return -ENOMEM;
  248. INIT_LIST_HEAD(&mem_device->res_list);
  249. mem_device->device = device;
  250. mem_device->mgid = -1;
  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. device->driver_data = NULL;
  258. kfree(mem_device);
  259. return result;
  260. }
  261. result = acpi_memory_check_device(mem_device);
  262. if (result) {
  263. acpi_memory_device_free(mem_device);
  264. return 0;
  265. }
  266. result = acpi_memory_enable_device(mem_device);
  267. if (result) {
  268. dev_err(&device->dev, "acpi_memory_enable_device() error\n");
  269. acpi_memory_device_free(mem_device);
  270. return result;
  271. }
  272. dev_dbg(&device->dev, "Memory device configured by ACPI\n");
  273. return 1;
  274. }
  275. static void acpi_memory_device_remove(struct acpi_device *device)
  276. {
  277. struct acpi_memory_device *mem_device;
  278. if (!device || !acpi_driver_data(device))
  279. return;
  280. mem_device = acpi_driver_data(device);
  281. acpi_memory_remove_memory(mem_device);
  282. acpi_memory_device_free(mem_device);
  283. }
  284. static bool __initdata acpi_no_memhotplug;
  285. void __init acpi_memory_hotplug_init(void)
  286. {
  287. if (acpi_no_memhotplug) {
  288. memory_device_handler.attach = NULL;
  289. acpi_scan_add_handler(&memory_device_handler);
  290. return;
  291. }
  292. acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
  293. }
  294. static int __init disable_acpi_memory_hotplug(char *str)
  295. {
  296. acpi_no_memhotplug = true;
  297. return 1;
  298. }
  299. __setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
  300. #else
  301. static struct acpi_scan_handler memory_device_handler = {
  302. .ids = memory_device_ids,
  303. };
  304. void __init acpi_memory_hotplug_init(void)
  305. {
  306. acpi_scan_add_handler(&memory_device_handler);
  307. }
  308. #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */