acpi_memhotplug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  1. /*
  2. * Copyright (C) 2004, 2013 Intel Corporation
  3. * Author: Naveen B S <naveen.b.s@intel.com>
  4. * Author: Rafael J. Wysocki <rafael.j.wysocki@intel.com>
  5. *
  6. * All rights reserved.
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or (at
  11. * your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful, but
  14. * WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  16. * NON INFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * ACPI based HotPlug driver that supports Memory Hotplug
  20. * This driver fields notifications from firmware for memory add
  21. * and remove operations and alerts the VM of the affected memory
  22. * ranges.
  23. */
  24. #include <linux/acpi.h>
  25. #include <linux/memory.h>
  26. #include <linux/memory_hotplug.h>
  27. #include "internal.h"
  28. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  29. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  30. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  31. #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
  32. #undef PREFIX
  33. #define PREFIX "ACPI:memory_hp:"
  34. ACPI_MODULE_NAME("acpi_memhotplug");
  35. static const struct acpi_device_id memory_device_ids[] = {
  36. {ACPI_MEMORY_DEVICE_HID, 0},
  37. {"", 0},
  38. };
  39. #ifdef CONFIG_ACPI_HOTPLUG_MEMORY
  40. /* Memory Device States */
  41. #define MEMORY_INVALID_STATE 0
  42. #define MEMORY_POWER_ON_STATE 1
  43. #define MEMORY_POWER_OFF_STATE 2
  44. static int acpi_memory_device_add(struct acpi_device *device,
  45. const struct acpi_device_id *not_used);
  46. static void acpi_memory_device_remove(struct acpi_device *device);
  47. static struct acpi_scan_handler memory_device_handler = {
  48. .ids = memory_device_ids,
  49. .attach = acpi_memory_device_add,
  50. .detach = acpi_memory_device_remove,
  51. .hotplug = {
  52. .enabled = true,
  53. },
  54. };
  55. struct acpi_memory_info {
  56. struct list_head list;
  57. u64 start_addr; /* Memory Range start physical addr */
  58. u64 length; /* Memory Range length */
  59. unsigned short caching; /* memory cache attribute */
  60. unsigned short write_protect; /* memory read/write attribute */
  61. unsigned int enabled:1;
  62. };
  63. struct acpi_memory_device {
  64. struct acpi_device * device;
  65. unsigned int state; /* State of the memory device */
  66. struct list_head res_list;
  67. };
  68. static acpi_status
  69. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  70. {
  71. struct acpi_memory_device *mem_device = context;
  72. struct acpi_resource_address64 address64;
  73. struct acpi_memory_info *info, *new;
  74. acpi_status status;
  75. status = acpi_resource_to_address64(resource, &address64);
  76. if (ACPI_FAILURE(status) ||
  77. (address64.resource_type != ACPI_MEMORY_RANGE))
  78. return AE_OK;
  79. list_for_each_entry(info, &mem_device->res_list, list) {
  80. /* Can we combine the resource range information? */
  81. if ((info->caching == address64.info.mem.caching) &&
  82. (info->write_protect == address64.info.mem.write_protect) &&
  83. (info->start_addr + info->length == address64.address.minimum)) {
  84. info->length += address64.address.address_length;
  85. return AE_OK;
  86. }
  87. }
  88. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  89. if (!new)
  90. return AE_ERROR;
  91. INIT_LIST_HEAD(&new->list);
  92. new->caching = address64.info.mem.caching;
  93. new->write_protect = address64.info.mem.write_protect;
  94. new->start_addr = address64.address.minimum;
  95. new->length = address64.address.address_length;
  96. list_add_tail(&new->list, &mem_device->res_list);
  97. return AE_OK;
  98. }
  99. static void
  100. acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
  101. {
  102. struct acpi_memory_info *info, *n;
  103. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  104. kfree(info);
  105. INIT_LIST_HEAD(&mem_device->res_list);
  106. }
  107. static int
  108. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  109. {
  110. acpi_status status;
  111. if (!list_empty(&mem_device->res_list))
  112. return 0;
  113. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  114. acpi_memory_get_resource, mem_device);
  115. if (ACPI_FAILURE(status)) {
  116. acpi_memory_free_device_resources(mem_device);
  117. return -EINVAL;
  118. }
  119. return 0;
  120. }
  121. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  122. {
  123. unsigned long long current_status;
  124. /* Get device present/absent information from the _STA */
  125. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle,
  126. METHOD_NAME__STA, NULL,
  127. &current_status)))
  128. return -ENODEV;
  129. /*
  130. * Check for device status. Device should be
  131. * present/enabled/functioning.
  132. */
  133. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  134. && (current_status & ACPI_STA_DEVICE_ENABLED)
  135. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  136. return -ENODEV;
  137. return 0;
  138. }
  139. static unsigned long acpi_meminfo_start_pfn(struct acpi_memory_info *info)
  140. {
  141. return PFN_DOWN(info->start_addr);
  142. }
  143. static unsigned long acpi_meminfo_end_pfn(struct acpi_memory_info *info)
  144. {
  145. return PFN_UP(info->start_addr + info->length-1);
  146. }
  147. static int acpi_bind_memblk(struct memory_block *mem, void *arg)
  148. {
  149. return acpi_bind_one(&mem->dev, arg);
  150. }
  151. static int acpi_bind_memory_blocks(struct acpi_memory_info *info,
  152. struct acpi_device *adev)
  153. {
  154. return walk_memory_range(acpi_meminfo_start_pfn(info),
  155. acpi_meminfo_end_pfn(info), adev,
  156. acpi_bind_memblk);
  157. }
  158. static int acpi_unbind_memblk(struct memory_block *mem, void *arg)
  159. {
  160. acpi_unbind_one(&mem->dev);
  161. return 0;
  162. }
  163. static void acpi_unbind_memory_blocks(struct acpi_memory_info *info)
  164. {
  165. walk_memory_range(acpi_meminfo_start_pfn(info),
  166. acpi_meminfo_end_pfn(info), NULL, acpi_unbind_memblk);
  167. }
  168. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  169. {
  170. acpi_handle handle = mem_device->device->handle;
  171. int result, num_enabled = 0;
  172. struct acpi_memory_info *info;
  173. int node;
  174. node = acpi_get_node(handle);
  175. /*
  176. * Tell the VM there is more memory here...
  177. * Note: Assume that this function returns zero on success
  178. * We don't have memory-hot-add rollback function,now.
  179. * (i.e. memory-hot-remove function)
  180. */
  181. list_for_each_entry(info, &mem_device->res_list, list) {
  182. if (info->enabled) { /* just sanity check...*/
  183. num_enabled++;
  184. continue;
  185. }
  186. /*
  187. * If the memory block size is zero, please ignore it.
  188. * Don't try to do the following memory hotplug flowchart.
  189. */
  190. if (!info->length)
  191. continue;
  192. if (node < 0)
  193. node = memory_add_physaddr_to_nid(info->start_addr);
  194. result = add_memory(node, info->start_addr, info->length);
  195. /*
  196. * If the memory block has been used by the kernel, add_memory()
  197. * returns -EEXIST. If add_memory() returns the other error, it
  198. * means that this memory block is not used by the kernel.
  199. */
  200. if (result && result != -EEXIST)
  201. continue;
  202. result = acpi_bind_memory_blocks(info, mem_device->device);
  203. if (result) {
  204. acpi_unbind_memory_blocks(info);
  205. return -ENODEV;
  206. }
  207. info->enabled = 1;
  208. /*
  209. * Add num_enable even if add_memory() returns -EEXIST, so the
  210. * device is bound to this driver.
  211. */
  212. num_enabled++;
  213. }
  214. if (!num_enabled) {
  215. dev_err(&mem_device->device->dev, "add_memory failed\n");
  216. mem_device->state = MEMORY_INVALID_STATE;
  217. return -EINVAL;
  218. }
  219. /*
  220. * Sometimes the memory device will contain several memory blocks.
  221. * When one memory block is hot-added to the system memory, it will
  222. * be regarded as a success.
  223. * Otherwise if the last memory block can't be hot-added to the system
  224. * memory, it will be failure and the memory device can't be bound with
  225. * driver.
  226. */
  227. return 0;
  228. }
  229. static void acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  230. {
  231. acpi_handle handle = mem_device->device->handle;
  232. struct acpi_memory_info *info, *n;
  233. int nid = acpi_get_node(handle);
  234. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  235. if (!info->enabled)
  236. continue;
  237. if (nid == NUMA_NO_NODE)
  238. nid = memory_add_physaddr_to_nid(info->start_addr);
  239. acpi_unbind_memory_blocks(info);
  240. remove_memory(nid, info->start_addr, info->length);
  241. list_del(&info->list);
  242. kfree(info);
  243. }
  244. }
  245. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  246. {
  247. if (!mem_device)
  248. return;
  249. acpi_memory_free_device_resources(mem_device);
  250. mem_device->device->driver_data = NULL;
  251. kfree(mem_device);
  252. }
  253. static int acpi_memory_device_add(struct acpi_device *device,
  254. const struct acpi_device_id *not_used)
  255. {
  256. struct acpi_memory_device *mem_device;
  257. int result;
  258. if (!device)
  259. return -EINVAL;
  260. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  261. if (!mem_device)
  262. return -ENOMEM;
  263. INIT_LIST_HEAD(&mem_device->res_list);
  264. mem_device->device = device;
  265. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  266. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  267. device->driver_data = mem_device;
  268. /* Get the range from the _CRS */
  269. result = acpi_memory_get_device_resources(mem_device);
  270. if (result) {
  271. device->driver_data = NULL;
  272. kfree(mem_device);
  273. return result;
  274. }
  275. /* Set the device state */
  276. mem_device->state = MEMORY_POWER_ON_STATE;
  277. result = acpi_memory_check_device(mem_device);
  278. if (result) {
  279. acpi_memory_device_free(mem_device);
  280. return 0;
  281. }
  282. result = acpi_memory_enable_device(mem_device);
  283. if (result) {
  284. dev_err(&device->dev, "acpi_memory_enable_device() error\n");
  285. acpi_memory_device_free(mem_device);
  286. return result;
  287. }
  288. dev_dbg(&device->dev, "Memory device configured by ACPI\n");
  289. return 1;
  290. }
  291. static void acpi_memory_device_remove(struct acpi_device *device)
  292. {
  293. struct acpi_memory_device *mem_device;
  294. if (!device || !acpi_driver_data(device))
  295. return;
  296. mem_device = acpi_driver_data(device);
  297. acpi_memory_remove_memory(mem_device);
  298. acpi_memory_device_free(mem_device);
  299. }
  300. static bool __initdata acpi_no_memhotplug;
  301. void __init acpi_memory_hotplug_init(void)
  302. {
  303. if (acpi_no_memhotplug) {
  304. memory_device_handler.attach = NULL;
  305. acpi_scan_add_handler(&memory_device_handler);
  306. return;
  307. }
  308. acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
  309. }
  310. static int __init disable_acpi_memory_hotplug(char *str)
  311. {
  312. acpi_no_memhotplug = true;
  313. return 1;
  314. }
  315. __setup("acpi_no_memhotplug", disable_acpi_memory_hotplug);
  316. #else
  317. static struct acpi_scan_handler memory_device_handler = {
  318. .ids = memory_device_ids,
  319. };
  320. void __init acpi_memory_hotplug_init(void)
  321. {
  322. acpi_scan_add_handler(&memory_device_handler);
  323. }
  324. #endif /* CONFIG_ACPI_HOTPLUG_MEMORY */