acpi_memhotplug.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. };
  49. static acpi_status
  50. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  51. {
  52. struct acpi_memory_device *mem_device = context;
  53. struct acpi_resource_address64 address64;
  54. struct acpi_memory_info *info, *new;
  55. acpi_status status;
  56. status = acpi_resource_to_address64(resource, &address64);
  57. if (ACPI_FAILURE(status) ||
  58. (address64.resource_type != ACPI_MEMORY_RANGE))
  59. return AE_OK;
  60. list_for_each_entry(info, &mem_device->res_list, list) {
  61. /* Can we combine the resource range information? */
  62. if ((info->caching == address64.info.mem.caching) &&
  63. (info->write_protect == address64.info.mem.write_protect) &&
  64. (info->start_addr + info->length == address64.address.minimum)) {
  65. info->length += address64.address.address_length;
  66. return AE_OK;
  67. }
  68. }
  69. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  70. if (!new)
  71. return AE_ERROR;
  72. INIT_LIST_HEAD(&new->list);
  73. new->caching = address64.info.mem.caching;
  74. new->write_protect = address64.info.mem.write_protect;
  75. new->start_addr = address64.address.minimum;
  76. new->length = address64.address.address_length;
  77. list_add_tail(&new->list, &mem_device->res_list);
  78. return AE_OK;
  79. }
  80. static void
  81. acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
  82. {
  83. struct acpi_memory_info *info, *n;
  84. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  85. kfree(info);
  86. INIT_LIST_HEAD(&mem_device->res_list);
  87. }
  88. static int
  89. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  90. {
  91. acpi_status status;
  92. if (!list_empty(&mem_device->res_list))
  93. return 0;
  94. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  95. acpi_memory_get_resource, mem_device);
  96. if (ACPI_FAILURE(status)) {
  97. acpi_memory_free_device_resources(mem_device);
  98. return -EINVAL;
  99. }
  100. return 0;
  101. }
  102. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  103. {
  104. unsigned long long current_status;
  105. /* Get device present/absent information from the _STA */
  106. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
  107. METHOD_NAME__STA, NULL,
  108. &current_status)))
  109. return -ENODEV;
  110. /*
  111. * Check for device status. Device should be
  112. * present/enabled/functioning.
  113. */
  114. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  115. && (current_status & ACPI_STA_DEVICE_ENABLED)
  116. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  117. return -ENODEV;
  118. return 0;
  119. }
  120. static int acpi_bind_memblk(struct memory_block *mem, void *arg)
  121. {
  122. return acpi_bind_one(&mem->dev, arg);
  123. }
  124. static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
  125. struct acpi_device *adev)
  126. {
  127. return walk_memory_blocks(info->start_addr, info->length, adev,
  128. acpi_bind_memblk);
  129. }
  130. static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
  131. {
  132. acpi_unbind_one(&mem->dev);
  133. return 0;
  134. }
  135. static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
  136. {
  137. walk_memory_blocks(info->start_addr, info->length, NULL,
  138. acpi_unbind_memblk);
  139. }
  140. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  141. {
  142. acpi_handle handle = mem_device->device->handle;
  143. int result, num_enabled = 0;
  144. struct acpi_memory_info *info;
  145. int node;
  146. node = acpi_get_node(handle);
  147. /*
  148. * Tell the VM there is more memory here...
  149. * Note: Assume that this function returns zero on success
  150. * We don't have memory-hot-add rollback function,now.
  151. * (i.e. memory-hot-remove function)
  152. */
  153. list_for_each_entry(info, &mem_device->res_list, list) {
  154. if (info->enabled) { /* just sanity check...*/
  155. num_enabled++;
  156. continue;
  157. }
  158. /*
  159. * If the memory block size is zero, please ignore it.
  160. * Don't try to do the following memory hotplug flowchart.
  161. */
  162. if (!info->length)
  163. continue;
  164. if (node < 0)
  165. node = memory_add_physaddr_to_nid(info->start_addr);
  166. result = __add_memory(node, info->start_addr, info->length,
  167. MHP_NONE);
  168. /*
  169. * If the memory block has been used by the kernel, add_memory()
  170. * returns -EEXIST. If add_memory() returns the other error, it
  171. * means that this memory block is not used by the kernel.
  172. */
  173. if (result && result != -EEXIST)
  174. continue;
  175. result = acpi_bind_memory_blocks(info, mem_device->device);
  176. if (result) {
  177. acpi_unbind_memory_blocks(info);
  178. return -ENODEV;
  179. }
  180. info->enabled = 1;
  181. /*
  182. * Add num_enable even if add_memory() returns -EEXIST, so the
  183. * device is bound to this driver.
  184. */
  185. num_enabled++;
  186. }
  187. if (!num_enabled) {
  188. dev_err(&mem_device->device->dev, "add_memory failed\n");
  189. return -EINVAL;
  190. }
  191. /*
  192. * Sometimes the memory device will contain several memory blocks.
  193. * When one memory block is hot-added to the system memory, it will
  194. * be regarded as a success.
  195. * Otherwise if the last memory block can't be hot-added to the system
  196. * memory, it will be failure and the memory device can't be bound with
  197. * driver.
  198. */
  199. return 0;
  200. }
  201. static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  202. {
  203. acpi_handle handle = mem_device->device->handle;
  204. struct acpi_memory_info *info, *n;
  205. int nid = acpi_get_node(handle);
  206. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  207. if (!info->enabled)
  208. continue;
  209. if (nid == NUMA_NO_NODE)
  210. nid = memory_add_physaddr_to_nid(info->start_addr);
  211. acpi_unbind_memory_blocks(info);
  212. __remove_memory(nid, info->start_addr, info->length);
  213. list_del(&info->list);
  214. kfree(info);
  215. }
  216. }
  217. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  218. {
  219. if (!mem_device)
  220. return;
  221. acpi_memory_free_device_resources(mem_device);
  222. mem_device->device->driver_data = NULL;
  223. kfree(mem_device);
  224. }
  225. static int acpi_memory_device_add(struct acpi_device *device,
  226. const struct acpi_device_id *not_used)
  227. {
  228. struct acpi_memory_device *mem_device;
  229. int result;
  230. if (!device)
  231. return -EINVAL;
  232. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  233. if (!mem_device)
  234. return -ENOMEM;
  235. INIT_LIST_HEAD(&mem_device->res_list);
  236. mem_device->device = device;
  237. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  238. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  239. device->driver_data = mem_device;
  240. /* Get the range from the _CRS */
  241. result = acpi_memory_get_device_resources(mem_device);
  242. if (result) {
  243. device->driver_data = NULL;
  244. kfree(mem_device);
  245. return result;
  246. }
  247. result = acpi_memory_check_device(mem_device);
  248. if (result) {
  249. acpi_memory_device_free(mem_device);
  250. return 0;
  251. }
  252. result = acpi_memory_enable_device(mem_device);
  253. if (result) {
  254. dev_err(&device->dev, "acpi_memory_enable_device() error\n");
  255. acpi_memory_device_free(mem_device);
  256. return result;
  257. }
  258. dev_dbg(&device->dev, "Memory device configured by ACPI\n");
  259. return 1;
  260. }
  261. static void acpi_memory_device_remove(struct acpi_device *device)
  262. {
  263. struct acpi_memory_device *mem_device;
  264. if (!device || !acpi_driver_data(device))
  265. return;
  266. mem_device = acpi_driver_data(device);
  267. acpi_memory_remove_memory(mem_device);
  268. acpi_memory_device_free(mem_device);
  269. }
  270. static bool __initdata acpi_no_memhotplug;
  271. void __init acpi_memory_hotplug_init(void)
  272. {
  273. if (acpi_no_memhotplug) {
  274. memory_device_handler.attach = NULL;
  275. acpi_scan_add_handler(&memory_device_handler);
  276. return;
  277. }
  278. acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
  279. }
  280. static int __init disable_acpi_memory_hotplug(char *str)
  281. {
  282. acpi_no_memhotplug = true;
  283. return 1;
  284. }
  285. __setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
  286. #else
  287. static struct acpi_scan_handler memory_device_handler = {
  288. .ids = memory_device_ids,
  289. };
  290. void __init acpi_memory_hotplug_init(void)
  291. {
  292. acpi_scan_add_handler(&memory_device_handler);
  293. }
  294. #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */