acpi_memhotplug.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. * You should have received a copy of the GNU General Public License
  20. * along with this program; if not, write to the Free Software
  21. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  22. *
  23. *
  24. * ACPI based HotPlug driver that supports Memory Hotplug
  25. * This driver fields notifications from firmware for memory add
  26. * and remove operations and alerts the VM of the affected memory
  27. * ranges.
  28. */
  29. #include <linux/acpi.h>
  30. #include <linux/memory_hotplug.h>
  31. #include "internal.h"
  32. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  33. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  34. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  35. #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
  36. #undef PREFIX
  37. #define PREFIX "ACPI:memory_hp:"
  38. ACPI_MODULE_NAME("acpi_memhotplug");
  39. /* Memory Device States */
  40. #define MEMORY_INVALID_STATE 0
  41. #define MEMORY_POWER_ON_STATE 1
  42. #define MEMORY_POWER_OFF_STATE 2
  43. static int acpi_memory_device_add(struct acpi_device *device,
  44. const struct acpi_device_id *not_used);
  45. static void acpi_memory_device_remove(struct acpi_device *device);
  46. static const struct acpi_device_id memory_device_ids[] = {
  47. {ACPI_MEMORY_DEVICE_HID, 0},
  48. {"", 0},
  49. };
  50. static struct acpi_scan_handler memory_device_handler = {
  51. .ids = memory_device_ids,
  52. .attach = acpi_memory_device_add,
  53. .detach = acpi_memory_device_remove,
  54. .hotplug = {
  55. .enabled = true,
  56. },
  57. };
  58. struct acpi_memory_info {
  59. struct list_head list;
  60. u64 start_addr; /* Memory Range start physical addr */
  61. u64 length; /* Memory Range length */
  62. unsigned short caching; /* memory cache attribute */
  63. unsigned short write_protect; /* memory read/write attribute */
  64. unsigned int enabled:1;
  65. unsigned int failed:1;
  66. };
  67. struct acpi_memory_device {
  68. struct acpi_device * device;
  69. unsigned int state; /* State of the memory device */
  70. struct list_head res_list;
  71. };
  72. static acpi_status
  73. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  74. {
  75. struct acpi_memory_device *mem_device = context;
  76. struct acpi_resource_address64 address64;
  77. struct acpi_memory_info *info, *new;
  78. acpi_status status;
  79. status = acpi_resource_to_address64(resource, &address64);
  80. if (ACPI_FAILURE(status) ||
  81. (address64.resource_type != ACPI_MEMORY_RANGE))
  82. return AE_OK;
  83. list_for_each_entry(info, &mem_device->res_list, list) {
  84. /* Can we combine the resource range information? */
  85. if ((info->caching == address64.info.mem.caching) &&
  86. (info->write_protect == address64.info.mem.write_protect) &&
  87. (info->start_addr + info->length == address64.minimum)) {
  88. info->length += address64.address_length;
  89. return AE_OK;
  90. }
  91. }
  92. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  93. if (!new)
  94. return AE_ERROR;
  95. INIT_LIST_HEAD(&new->list);
  96. new->caching = address64.info.mem.caching;
  97. new->write_protect = address64.info.mem.write_protect;
  98. new->start_addr = address64.minimum;
  99. new->length = address64.address_length;
  100. list_add_tail(&new->list, &mem_device->res_list);
  101. return AE_OK;
  102. }
  103. static void
  104. acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
  105. {
  106. struct acpi_memory_info *info, *n;
  107. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  108. kfree(info);
  109. INIT_LIST_HEAD(&mem_device->res_list);
  110. }
  111. static int
  112. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  113. {
  114. acpi_status status;
  115. if (!list_empty(&mem_device->res_list))
  116. return 0;
  117. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  118. acpi_memory_get_resource, mem_device);
  119. if (ACPI_FAILURE(status)) {
  120. acpi_memory_free_device_resources(mem_device);
  121. return -EINVAL;
  122. }
  123. return 0;
  124. }
  125. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  126. {
  127. unsigned long long current_status;
  128. /* Get device present/absent information from the _STA */
  129. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  130. NULL, &current_status)))
  131. return -ENODEV;
  132. /*
  133. * Check for device status. Device should be
  134. * present/enabled/functioning.
  135. */
  136. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  137. && (current_status & ACPI_STA_DEVICE_ENABLED)
  138. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  139. return -ENODEV;
  140. return 0;
  141. }
  142. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  143. {
  144. int result, num_enabled = 0;
  145. struct acpi_memory_info *info;
  146. int node;
  147. node = acpi_get_node(mem_device->device->handle);
  148. /*
  149. * Tell the VM there is more memory here...
  150. * Note: Assume that this function returns zero on success
  151. * We don't have memory-hot-add rollback function,now.
  152. * (i.e. memory-hot-remove function)
  153. */
  154. list_for_each_entry(info, &mem_device->res_list, list) {
  155. if (info->enabled) { /* just sanity check...*/
  156. num_enabled++;
  157. continue;
  158. }
  159. /*
  160. * If the memory block size is zero, please ignore it.
  161. * Don't try to do the following memory hotplug flowchart.
  162. */
  163. if (!info->length)
  164. continue;
  165. if (node < 0)
  166. node = memory_add_physaddr_to_nid(info->start_addr);
  167. result = add_memory(node, info->start_addr, info->length);
  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. info->failed = 1;
  175. continue;
  176. }
  177. if (!result)
  178. info->enabled = 1;
  179. /*
  180. * Add num_enable even if add_memory() returns -EEXIST, so the
  181. * device is bound to this driver.
  182. */
  183. num_enabled++;
  184. }
  185. if (!num_enabled) {
  186. dev_err(&mem_device->device->dev, "add_memory failed\n");
  187. mem_device->state = MEMORY_INVALID_STATE;
  188. return -EINVAL;
  189. }
  190. /*
  191. * Sometimes the memory device will contain several memory blocks.
  192. * When one memory block is hot-added to the system memory, it will
  193. * be regarded as a success.
  194. * Otherwise if the last memory block can't be hot-added to the system
  195. * memory, it will be failure and the memory device can't be bound with
  196. * driver.
  197. */
  198. return 0;
  199. }
  200. static int acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  201. {
  202. int result = 0, nid;
  203. struct acpi_memory_info *info, *n;
  204. nid = acpi_get_node(mem_device->device->handle);
  205. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  206. if (info->failed)
  207. /* The kernel does not use this memory block */
  208. continue;
  209. if (!info->enabled)
  210. /*
  211. * The kernel uses this memory block, but it may be not
  212. * managed by us.
  213. */
  214. return -EBUSY;
  215. if (nid < 0)
  216. nid = memory_add_physaddr_to_nid(info->start_addr);
  217. result = remove_memory(nid, info->start_addr, info->length);
  218. if (result)
  219. return result;
  220. list_del(&info->list);
  221. kfree(info);
  222. }
  223. return result;
  224. }
  225. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  226. {
  227. if (!mem_device)
  228. return;
  229. acpi_memory_free_device_resources(mem_device);
  230. mem_device->device->driver_data = NULL;
  231. kfree(mem_device);
  232. }
  233. static int acpi_memory_device_add(struct acpi_device *device,
  234. const struct acpi_device_id *not_used)
  235. {
  236. struct acpi_memory_device *mem_device;
  237. int result;
  238. if (!device)
  239. return -EINVAL;
  240. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  241. if (!mem_device)
  242. return -ENOMEM;
  243. INIT_LIST_HEAD(&mem_device->res_list);
  244. mem_device->device = device;
  245. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  246. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  247. device->driver_data = mem_device;
  248. /* Get the range from the _CRS */
  249. result = acpi_memory_get_device_resources(mem_device);
  250. if (result) {
  251. kfree(mem_device);
  252. return result;
  253. }
  254. /* Set the device state */
  255. mem_device->state = MEMORY_POWER_ON_STATE;
  256. result = acpi_memory_check_device(mem_device);
  257. if (result) {
  258. acpi_memory_device_free(mem_device);
  259. return 0;
  260. }
  261. result = acpi_memory_enable_device(mem_device);
  262. if (result) {
  263. dev_err(&device->dev, "acpi_memory_enable_device() error\n");
  264. acpi_memory_device_free(mem_device);
  265. return -ENODEV;
  266. }
  267. dev_dbg(&device->dev, "Memory device configured by ACPI\n");
  268. return 1;
  269. }
  270. static void acpi_memory_device_remove(struct acpi_device *device)
  271. {
  272. struct acpi_memory_device *mem_device;
  273. if (!device || !acpi_driver_data(device))
  274. return;
  275. mem_device = acpi_driver_data(device);
  276. acpi_memory_remove_memory(mem_device);
  277. acpi_memory_device_free(mem_device);
  278. }
  279. void __init acpi_memory_hotplug_init(void)
  280. {
  281. acpi_scan_add_handler_with_hotplug(&memory_device_handler, "memory");
  282. }