acpi_memhotplug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. /*
  2. * Copyright (C) 2004 Intel Corporation <naveen.b.s@intel.com>
  3. *
  4. * All rights reserved.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or (at
  9. * your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful, but
  12. * WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
  14. * NON INFRINGEMENT. See the GNU General Public License for more
  15. * details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. *
  22. * ACPI based HotPlug driver that supports Memory Hotplug
  23. * This driver fields notifications from firmware for memory add
  24. * and remove operations and alerts the VM of the affected memory
  25. * ranges.
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/memory_hotplug.h>
  32. #include <linux/slab.h>
  33. #include <linux/acpi.h>
  34. #include <acpi/acpi_drivers.h>
  35. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  36. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  37. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  38. #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
  39. #undef PREFIX
  40. #define PREFIX "ACPI:memory_hp:"
  41. ACPI_MODULE_NAME("acpi_memhotplug");
  42. MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
  43. MODULE_DESCRIPTION("Hotplug Mem Driver");
  44. MODULE_LICENSE("GPL");
  45. /* Memory Device States */
  46. #define MEMORY_INVALID_STATE 0
  47. #define MEMORY_POWER_ON_STATE 1
  48. #define MEMORY_POWER_OFF_STATE 2
  49. static int acpi_memory_device_add(struct acpi_device *device);
  50. static int acpi_memory_device_remove(struct acpi_device *device);
  51. static const struct acpi_device_id memory_device_ids[] = {
  52. {ACPI_MEMORY_DEVICE_HID, 0},
  53. {"", 0},
  54. };
  55. MODULE_DEVICE_TABLE(acpi, memory_device_ids);
  56. static struct acpi_driver acpi_memory_device_driver = {
  57. .name = "acpi_memhotplug",
  58. .class = ACPI_MEMORY_DEVICE_CLASS,
  59. .ids = memory_device_ids,
  60. .ops = {
  61. .add = acpi_memory_device_add,
  62. .remove = acpi_memory_device_remove,
  63. },
  64. };
  65. struct acpi_memory_info {
  66. struct list_head list;
  67. u64 start_addr; /* Memory Range start physical addr */
  68. u64 length; /* Memory Range length */
  69. unsigned short caching; /* memory cache attribute */
  70. unsigned short write_protect; /* memory read/write attribute */
  71. unsigned int enabled:1;
  72. unsigned int failed:1;
  73. };
  74. struct acpi_memory_device {
  75. struct acpi_device * device;
  76. unsigned int state; /* State of the memory device */
  77. struct list_head res_list;
  78. };
  79. static acpi_status
  80. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  81. {
  82. struct acpi_memory_device *mem_device = context;
  83. struct acpi_resource_address64 address64;
  84. struct acpi_memory_info *info, *new;
  85. acpi_status status;
  86. status = acpi_resource_to_address64(resource, &address64);
  87. if (ACPI_FAILURE(status) ||
  88. (address64.resource_type != ACPI_MEMORY_RANGE))
  89. return AE_OK;
  90. list_for_each_entry(info, &mem_device->res_list, list) {
  91. /* Can we combine the resource range information? */
  92. if ((info->caching == address64.info.mem.caching) &&
  93. (info->write_protect == address64.info.mem.write_protect) &&
  94. (info->start_addr + info->length == address64.minimum)) {
  95. info->length += address64.address_length;
  96. return AE_OK;
  97. }
  98. }
  99. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  100. if (!new)
  101. return AE_ERROR;
  102. INIT_LIST_HEAD(&new->list);
  103. new->caching = address64.info.mem.caching;
  104. new->write_protect = address64.info.mem.write_protect;
  105. new->start_addr = address64.minimum;
  106. new->length = address64.address_length;
  107. list_add_tail(&new->list, &mem_device->res_list);
  108. return AE_OK;
  109. }
  110. static void
  111. acpi_memory_free_device_resources(struct acpi_memory_device *mem_device)
  112. {
  113. struct acpi_memory_info *info, *n;
  114. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  115. kfree(info);
  116. INIT_LIST_HEAD(&mem_device->res_list);
  117. }
  118. static int
  119. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  120. {
  121. acpi_status status;
  122. if (!list_empty(&mem_device->res_list))
  123. return 0;
  124. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  125. acpi_memory_get_resource, mem_device);
  126. if (ACPI_FAILURE(status)) {
  127. acpi_memory_free_device_resources(mem_device);
  128. return -EINVAL;
  129. }
  130. return 0;
  131. }
  132. static int acpi_memory_get_device(acpi_handle handle,
  133. struct acpi_memory_device **mem_device)
  134. {
  135. struct acpi_device *device = NULL;
  136. int result = 0;
  137. acpi_scan_lock_acquire();
  138. acpi_bus_get_device(handle, &device);
  139. if (device)
  140. goto end;
  141. /*
  142. * Now add the notified device. This creates the acpi_device
  143. * and invokes .add function
  144. */
  145. result = acpi_bus_scan(handle);
  146. if (result) {
  147. acpi_handle_warn(handle, "ACPI namespace scan failed\n");
  148. result = -EINVAL;
  149. goto out;
  150. }
  151. result = acpi_bus_get_device(handle, &device);
  152. if (result) {
  153. acpi_handle_warn(handle, "Missing device object\n");
  154. result = -EINVAL;
  155. goto out;
  156. }
  157. end:
  158. *mem_device = acpi_driver_data(device);
  159. if (!(*mem_device)) {
  160. dev_err(&device->dev, "driver data not found\n");
  161. result = -ENODEV;
  162. goto out;
  163. }
  164. out:
  165. acpi_scan_lock_release();
  166. return result;
  167. }
  168. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  169. {
  170. unsigned long long current_status;
  171. /* Get device present/absent information from the _STA */
  172. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  173. NULL, &current_status)))
  174. return -ENODEV;
  175. /*
  176. * Check for device status. Device should be
  177. * present/enabled/functioning.
  178. */
  179. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  180. && (current_status & ACPI_STA_DEVICE_ENABLED)
  181. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  182. return -ENODEV;
  183. return 0;
  184. }
  185. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  186. {
  187. int result, num_enabled = 0;
  188. struct acpi_memory_info *info;
  189. int node;
  190. node = acpi_get_node(mem_device->device->handle);
  191. /*
  192. * Tell the VM there is more memory here...
  193. * Note: Assume that this function returns zero on success
  194. * We don't have memory-hot-add rollback function,now.
  195. * (i.e. memory-hot-remove function)
  196. */
  197. list_for_each_entry(info, &mem_device->res_list, list) {
  198. if (info->enabled) { /* just sanity check...*/
  199. num_enabled++;
  200. continue;
  201. }
  202. /*
  203. * If the memory block size is zero, please ignore it.
  204. * Don't try to do the following memory hotplug flowchart.
  205. */
  206. if (!info->length)
  207. continue;
  208. if (node < 0)
  209. node = memory_add_physaddr_to_nid(info->start_addr);
  210. result = add_memory(node, info->start_addr, info->length);
  211. /*
  212. * If the memory block has been used by the kernel, add_memory()
  213. * returns -EEXIST. If add_memory() returns the other error, it
  214. * means that this memory block is not used by the kernel.
  215. */
  216. if (result && result != -EEXIST) {
  217. info->failed = 1;
  218. continue;
  219. }
  220. if (!result)
  221. info->enabled = 1;
  222. /*
  223. * Add num_enable even if add_memory() returns -EEXIST, so the
  224. * device is bound to this driver.
  225. */
  226. num_enabled++;
  227. }
  228. if (!num_enabled) {
  229. dev_err(&mem_device->device->dev, "add_memory failed\n");
  230. mem_device->state = MEMORY_INVALID_STATE;
  231. return -EINVAL;
  232. }
  233. /*
  234. * Sometimes the memory device will contain several memory blocks.
  235. * When one memory block is hot-added to the system memory, it will
  236. * be regarded as a success.
  237. * Otherwise if the last memory block can't be hot-added to the system
  238. * memory, it will be failure and the memory device can't be bound with
  239. * driver.
  240. */
  241. return 0;
  242. }
  243. static int acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  244. {
  245. int result = 0, nid;
  246. struct acpi_memory_info *info, *n;
  247. nid = acpi_get_node(mem_device->device->handle);
  248. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  249. if (info->failed)
  250. /* The kernel does not use this memory block */
  251. continue;
  252. if (!info->enabled)
  253. /*
  254. * The kernel uses this memory block, but it may be not
  255. * managed by us.
  256. */
  257. return -EBUSY;
  258. if (nid < 0)
  259. nid = memory_add_physaddr_to_nid(info->start_addr);
  260. result = remove_memory(nid, info->start_addr, info->length);
  261. if (result)
  262. return result;
  263. list_del(&info->list);
  264. kfree(info);
  265. }
  266. return result;
  267. }
  268. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  269. {
  270. struct acpi_memory_device *mem_device;
  271. struct acpi_device *device;
  272. struct acpi_eject_event *ej_event = NULL;
  273. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  274. acpi_status status;
  275. switch (event) {
  276. case ACPI_NOTIFY_BUS_CHECK:
  277. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  278. "\nReceived BUS CHECK notification for device\n"));
  279. /* Fall Through */
  280. case ACPI_NOTIFY_DEVICE_CHECK:
  281. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  282. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  283. "\nReceived DEVICE CHECK notification for device\n"));
  284. if (acpi_memory_get_device(handle, &mem_device)) {
  285. acpi_handle_err(handle, "Cannot find driver data\n");
  286. break;
  287. }
  288. ost_code = ACPI_OST_SC_SUCCESS;
  289. break;
  290. case ACPI_NOTIFY_EJECT_REQUEST:
  291. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  292. "\nReceived EJECT REQUEST notification for device\n"));
  293. status = AE_ERROR;
  294. acpi_scan_lock_acquire();
  295. if (acpi_bus_get_device(handle, &device)) {
  296. acpi_handle_err(handle, "Device doesn't exist\n");
  297. goto unlock;
  298. }
  299. mem_device = acpi_driver_data(device);
  300. if (!mem_device) {
  301. acpi_handle_err(handle, "Driver Data is NULL\n");
  302. goto unlock;
  303. }
  304. ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
  305. if (!ej_event) {
  306. pr_err(PREFIX "No memory, dropping EJECT\n");
  307. goto unlock;
  308. }
  309. get_device(&device->dev);
  310. ej_event->device = device;
  311. ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
  312. /* The eject is carried out asynchronously. */
  313. status = acpi_os_hotplug_execute(acpi_bus_hot_remove_device,
  314. ej_event);
  315. if (ACPI_FAILURE(status)) {
  316. put_device(&device->dev);
  317. kfree(ej_event);
  318. }
  319. unlock:
  320. acpi_scan_lock_release();
  321. if (ACPI_SUCCESS(status))
  322. return;
  323. default:
  324. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  325. "Unsupported event [0x%x]\n", event));
  326. /* non-hotplug event; possibly handled by other handler */
  327. return;
  328. }
  329. /* Inform firmware that the hotplug operation has completed */
  330. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  331. }
  332. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  333. {
  334. if (!mem_device)
  335. return;
  336. acpi_memory_free_device_resources(mem_device);
  337. kfree(mem_device);
  338. }
  339. static int acpi_memory_device_add(struct acpi_device *device)
  340. {
  341. int result;
  342. struct acpi_memory_device *mem_device = NULL;
  343. if (!device)
  344. return -EINVAL;
  345. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  346. if (!mem_device)
  347. return -ENOMEM;
  348. INIT_LIST_HEAD(&mem_device->res_list);
  349. mem_device->device = device;
  350. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  351. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  352. device->driver_data = mem_device;
  353. /* Get the range from the _CRS */
  354. result = acpi_memory_get_device_resources(mem_device);
  355. if (result) {
  356. kfree(mem_device);
  357. return result;
  358. }
  359. /* Set the device state */
  360. mem_device->state = MEMORY_POWER_ON_STATE;
  361. pr_debug("%s\n", acpi_device_name(device));
  362. if (!acpi_memory_check_device(mem_device)) {
  363. /* call add_memory func */
  364. result = acpi_memory_enable_device(mem_device);
  365. if (result) {
  366. dev_err(&device->dev,
  367. "Error in acpi_memory_enable_device\n");
  368. acpi_memory_device_free(mem_device);
  369. }
  370. }
  371. return result;
  372. }
  373. static int acpi_memory_device_remove(struct acpi_device *device)
  374. {
  375. struct acpi_memory_device *mem_device = NULL;
  376. int result;
  377. if (!device || !acpi_driver_data(device))
  378. return -EINVAL;
  379. mem_device = acpi_driver_data(device);
  380. result = acpi_memory_remove_memory(mem_device);
  381. if (result)
  382. return result;
  383. acpi_memory_device_free(mem_device);
  384. return 0;
  385. }
  386. /*
  387. * Helper function to check for memory device
  388. */
  389. static acpi_status is_memory_device(acpi_handle handle)
  390. {
  391. char *hardware_id;
  392. acpi_status status;
  393. struct acpi_device_info *info;
  394. status = acpi_get_object_info(handle, &info);
  395. if (ACPI_FAILURE(status))
  396. return status;
  397. if (!(info->valid & ACPI_VALID_HID)) {
  398. kfree(info);
  399. return AE_ERROR;
  400. }
  401. hardware_id = info->hardware_id.string;
  402. if ((hardware_id == NULL) ||
  403. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  404. status = AE_ERROR;
  405. kfree(info);
  406. return status;
  407. }
  408. static acpi_status
  409. acpi_memory_register_notify_handler(acpi_handle handle,
  410. u32 level, void *ctxt, void **retv)
  411. {
  412. acpi_status status;
  413. status = is_memory_device(handle);
  414. if (ACPI_FAILURE(status))
  415. return AE_OK; /* continue */
  416. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  417. acpi_memory_device_notify, NULL);
  418. /* continue */
  419. return AE_OK;
  420. }
  421. static acpi_status
  422. acpi_memory_deregister_notify_handler(acpi_handle handle,
  423. u32 level, void *ctxt, void **retv)
  424. {
  425. acpi_status status;
  426. status = is_memory_device(handle);
  427. if (ACPI_FAILURE(status))
  428. return AE_OK; /* continue */
  429. status = acpi_remove_notify_handler(handle,
  430. ACPI_SYSTEM_NOTIFY,
  431. acpi_memory_device_notify);
  432. return AE_OK; /* continue */
  433. }
  434. static int __init acpi_memory_device_init(void)
  435. {
  436. int result;
  437. acpi_status status;
  438. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  439. if (result < 0)
  440. return -ENODEV;
  441. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  442. ACPI_UINT32_MAX,
  443. acpi_memory_register_notify_handler, NULL,
  444. NULL, NULL);
  445. if (ACPI_FAILURE(status)) {
  446. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  447. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  448. return -ENODEV;
  449. }
  450. return 0;
  451. }
  452. static void __exit acpi_memory_device_exit(void)
  453. {
  454. acpi_status status;
  455. /*
  456. * Adding this to un-install notification handlers for all the device
  457. * handles.
  458. */
  459. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  460. ACPI_UINT32_MAX,
  461. acpi_memory_deregister_notify_handler, NULL,
  462. NULL, NULL);
  463. if (ACPI_FAILURE(status))
  464. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  465. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  466. return;
  467. }
  468. module_init(acpi_memory_device_init);
  469. module_exit(acpi_memory_device_exit);