acpi_memhotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587
  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 <acpi/acpi_drivers.h>
  34. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  35. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  36. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  37. #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
  38. #undef PREFIX
  39. #define PREFIX "ACPI:memory_hp:"
  40. ACPI_MODULE_NAME("acpi_memhotplug");
  41. MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
  42. MODULE_DESCRIPTION("Hotplug Mem Driver");
  43. MODULE_LICENSE("GPL");
  44. /* Memory Device States */
  45. #define MEMORY_INVALID_STATE 0
  46. #define MEMORY_POWER_ON_STATE 1
  47. #define MEMORY_POWER_OFF_STATE 2
  48. static int acpi_memory_device_add(struct acpi_device *device);
  49. static int acpi_memory_device_remove(struct acpi_device *device, int type);
  50. static const struct acpi_device_id memory_device_ids[] = {
  51. {ACPI_MEMORY_DEVICE_HID, 0},
  52. {"", 0},
  53. };
  54. MODULE_DEVICE_TABLE(acpi, memory_device_ids);
  55. static struct acpi_driver acpi_memory_device_driver = {
  56. .name = "acpi_memhotplug",
  57. .class = ACPI_MEMORY_DEVICE_CLASS,
  58. .ids = memory_device_ids,
  59. .ops = {
  60. .add = acpi_memory_device_add,
  61. .remove = acpi_memory_device_remove,
  62. },
  63. };
  64. struct acpi_memory_info {
  65. struct list_head list;
  66. u64 start_addr; /* Memory Range start physical addr */
  67. u64 length; /* Memory Range length */
  68. unsigned short caching; /* memory cache attribute */
  69. unsigned short write_protect; /* memory read/write attribute */
  70. unsigned int enabled:1;
  71. unsigned int failed:1;
  72. };
  73. struct acpi_memory_device {
  74. struct acpi_device * device;
  75. unsigned int state; /* State of the memory device */
  76. struct list_head res_list;
  77. };
  78. static int acpi_hotmem_initialized;
  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
  133. acpi_memory_get_device(acpi_handle handle,
  134. struct acpi_memory_device **mem_device)
  135. {
  136. acpi_status status;
  137. acpi_handle phandle;
  138. struct acpi_device *device = NULL;
  139. struct acpi_device *pdevice = NULL;
  140. int result;
  141. if (!acpi_bus_get_device(handle, &device) && device)
  142. goto end;
  143. status = acpi_get_parent(handle, &phandle);
  144. if (ACPI_FAILURE(status)) {
  145. ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
  146. return -EINVAL;
  147. }
  148. /* Get the parent device */
  149. result = acpi_bus_get_device(phandle, &pdevice);
  150. if (result) {
  151. printk(KERN_WARNING PREFIX "Cannot get acpi bus device");
  152. return -EINVAL;
  153. }
  154. /*
  155. * Now add the notified device. This creates the acpi_device
  156. * and invokes .add function
  157. */
  158. result = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  159. if (result) {
  160. printk(KERN_WARNING PREFIX "Cannot add acpi bus");
  161. return -EINVAL;
  162. }
  163. end:
  164. *mem_device = acpi_driver_data(device);
  165. if (!(*mem_device)) {
  166. printk(KERN_ERR "\n driver data not found");
  167. return -ENODEV;
  168. }
  169. return 0;
  170. }
  171. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  172. {
  173. unsigned long long current_status;
  174. /* Get device present/absent information from the _STA */
  175. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  176. NULL, &current_status)))
  177. return -ENODEV;
  178. /*
  179. * Check for device status. Device should be
  180. * present/enabled/functioning.
  181. */
  182. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  183. && (current_status & ACPI_STA_DEVICE_ENABLED)
  184. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  185. return -ENODEV;
  186. return 0;
  187. }
  188. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  189. {
  190. int result, num_enabled = 0;
  191. struct acpi_memory_info *info;
  192. int node;
  193. /* Get the range from the _CRS */
  194. result = acpi_memory_get_device_resources(mem_device);
  195. if (result) {
  196. printk(KERN_ERR PREFIX "get_device_resources failed\n");
  197. mem_device->state = MEMORY_INVALID_STATE;
  198. return result;
  199. }
  200. node = acpi_get_node(mem_device->device->handle);
  201. /*
  202. * Tell the VM there is more memory here...
  203. * Note: Assume that this function returns zero on success
  204. * We don't have memory-hot-add rollback function,now.
  205. * (i.e. memory-hot-remove function)
  206. */
  207. list_for_each_entry(info, &mem_device->res_list, list) {
  208. if (info->enabled) { /* just sanity check...*/
  209. num_enabled++;
  210. continue;
  211. }
  212. /*
  213. * If the memory block size is zero, please ignore it.
  214. * Don't try to do the following memory hotplug flowchart.
  215. */
  216. if (!info->length)
  217. continue;
  218. if (node < 0)
  219. node = memory_add_physaddr_to_nid(info->start_addr);
  220. result = add_memory(node, info->start_addr, info->length);
  221. /*
  222. * If the memory block has been used by the kernel, add_memory()
  223. * returns -EEXIST. If add_memory() returns the other error, it
  224. * means that this memory block is not used by the kernel.
  225. */
  226. if (result && result != -EEXIST) {
  227. info->failed = 1;
  228. continue;
  229. }
  230. if (!result)
  231. info->enabled = 1;
  232. /*
  233. * Add num_enable even if add_memory() returns -EEXIST, so the
  234. * device is bound to this driver.
  235. */
  236. num_enabled++;
  237. }
  238. if (!num_enabled) {
  239. printk(KERN_ERR PREFIX "add_memory failed\n");
  240. mem_device->state = MEMORY_INVALID_STATE;
  241. return -EINVAL;
  242. }
  243. /*
  244. * Sometimes the memory device will contain several memory blocks.
  245. * When one memory block is hot-added to the system memory, it will
  246. * be regarded as a success.
  247. * Otherwise if the last memory block can't be hot-added to the system
  248. * memory, it will be failure and the memory device can't be bound with
  249. * driver.
  250. */
  251. return 0;
  252. }
  253. static int acpi_memory_remove_memory(struct acpi_memory_device *mem_device)
  254. {
  255. int result = 0;
  256. struct acpi_memory_info *info, *n;
  257. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  258. if (info->failed)
  259. /* The kernel does not use this memory block */
  260. continue;
  261. if (!info->enabled)
  262. /*
  263. * The kernel uses this memory block, but it may be not
  264. * managed by us.
  265. */
  266. return -EBUSY;
  267. result = remove_memory(info->start_addr, info->length);
  268. if (result)
  269. return result;
  270. list_del(&info->list);
  271. kfree(info);
  272. }
  273. return result;
  274. }
  275. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  276. {
  277. struct acpi_memory_device *mem_device;
  278. struct acpi_device *device;
  279. struct acpi_eject_event *ej_event = NULL;
  280. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  281. switch (event) {
  282. case ACPI_NOTIFY_BUS_CHECK:
  283. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  284. "\nReceived BUS CHECK notification for device\n"));
  285. /* Fall Through */
  286. case ACPI_NOTIFY_DEVICE_CHECK:
  287. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  289. "\nReceived DEVICE CHECK notification for device\n"));
  290. if (acpi_memory_get_device(handle, &mem_device)) {
  291. printk(KERN_ERR PREFIX "Cannot find driver data\n");
  292. break;
  293. }
  294. if (acpi_memory_check_device(mem_device))
  295. break;
  296. if (acpi_memory_enable_device(mem_device)) {
  297. printk(KERN_ERR PREFIX "Cannot enable memory device\n");
  298. break;
  299. }
  300. ost_code = ACPI_OST_SC_SUCCESS;
  301. break;
  302. case ACPI_NOTIFY_EJECT_REQUEST:
  303. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  304. "\nReceived EJECT REQUEST notification for device\n"));
  305. if (acpi_bus_get_device(handle, &device)) {
  306. printk(KERN_ERR PREFIX "Device doesn't exist\n");
  307. break;
  308. }
  309. mem_device = acpi_driver_data(device);
  310. if (!mem_device) {
  311. printk(KERN_ERR PREFIX "Driver Data is NULL\n");
  312. break;
  313. }
  314. ej_event = kmalloc(sizeof(*ej_event), GFP_KERNEL);
  315. if (!ej_event) {
  316. pr_err(PREFIX "No memory, dropping EJECT\n");
  317. break;
  318. }
  319. ej_event->handle = handle;
  320. ej_event->event = ACPI_NOTIFY_EJECT_REQUEST;
  321. acpi_os_hotplug_execute(acpi_bus_hot_remove_device,
  322. (void *)ej_event);
  323. /* eject is performed asynchronously */
  324. return;
  325. default:
  326. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  327. "Unsupported event [0x%x]\n", event));
  328. /* non-hotplug event; possibly handled by other handler */
  329. return;
  330. }
  331. /* Inform firmware that the hotplug operation has completed */
  332. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  333. return;
  334. }
  335. static void acpi_memory_device_free(struct acpi_memory_device *mem_device)
  336. {
  337. if (!mem_device)
  338. return;
  339. acpi_memory_free_device_resources(mem_device);
  340. kfree(mem_device);
  341. }
  342. static int acpi_memory_device_add(struct acpi_device *device)
  343. {
  344. int result;
  345. struct acpi_memory_device *mem_device = NULL;
  346. if (!device)
  347. return -EINVAL;
  348. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  349. if (!mem_device)
  350. return -ENOMEM;
  351. INIT_LIST_HEAD(&mem_device->res_list);
  352. mem_device->device = device;
  353. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  354. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  355. device->driver_data = mem_device;
  356. /* Get the range from the _CRS */
  357. result = acpi_memory_get_device_resources(mem_device);
  358. if (result) {
  359. kfree(mem_device);
  360. return result;
  361. }
  362. /* Set the device state */
  363. mem_device->state = MEMORY_POWER_ON_STATE;
  364. printk(KERN_DEBUG "%s \n", acpi_device_name(device));
  365. /*
  366. * Early boot code has recognized memory area by EFI/E820.
  367. * If DSDT shows these memory devices on boot, hotplug is not necessary
  368. * for them. So, it just returns until completion of this driver's
  369. * start up.
  370. */
  371. if (!acpi_hotmem_initialized)
  372. return 0;
  373. if (!acpi_memory_check_device(mem_device)) {
  374. /* call add_memory func */
  375. result = acpi_memory_enable_device(mem_device);
  376. if (result) {
  377. printk(KERN_ERR PREFIX
  378. "Error in acpi_memory_enable_device\n");
  379. acpi_memory_device_free(mem_device);
  380. }
  381. }
  382. return result;
  383. }
  384. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  385. {
  386. struct acpi_memory_device *mem_device = NULL;
  387. int result;
  388. if (!device || !acpi_driver_data(device))
  389. return -EINVAL;
  390. mem_device = acpi_driver_data(device);
  391. result = acpi_memory_remove_memory(mem_device);
  392. if (result)
  393. return result;
  394. acpi_memory_device_free(mem_device);
  395. return 0;
  396. }
  397. /*
  398. * Helper function to check for memory device
  399. */
  400. static acpi_status is_memory_device(acpi_handle handle)
  401. {
  402. char *hardware_id;
  403. acpi_status status;
  404. struct acpi_device_info *info;
  405. status = acpi_get_object_info(handle, &info);
  406. if (ACPI_FAILURE(status))
  407. return status;
  408. if (!(info->valid & ACPI_VALID_HID)) {
  409. kfree(info);
  410. return AE_ERROR;
  411. }
  412. hardware_id = info->hardware_id.string;
  413. if ((hardware_id == NULL) ||
  414. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  415. status = AE_ERROR;
  416. kfree(info);
  417. return status;
  418. }
  419. static acpi_status
  420. acpi_memory_register_notify_handler(acpi_handle handle,
  421. u32 level, void *ctxt, void **retv)
  422. {
  423. acpi_status status;
  424. status = is_memory_device(handle);
  425. if (ACPI_FAILURE(status))
  426. return AE_OK; /* continue */
  427. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  428. acpi_memory_device_notify, NULL);
  429. /* continue */
  430. return AE_OK;
  431. }
  432. static acpi_status
  433. acpi_memory_deregister_notify_handler(acpi_handle handle,
  434. u32 level, void *ctxt, void **retv)
  435. {
  436. acpi_status status;
  437. status = is_memory_device(handle);
  438. if (ACPI_FAILURE(status))
  439. return AE_OK; /* continue */
  440. status = acpi_remove_notify_handler(handle,
  441. ACPI_SYSTEM_NOTIFY,
  442. acpi_memory_device_notify);
  443. return AE_OK; /* continue */
  444. }
  445. static int __init acpi_memory_device_init(void)
  446. {
  447. int result;
  448. acpi_status status;
  449. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  450. if (result < 0)
  451. return -ENODEV;
  452. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  453. ACPI_UINT32_MAX,
  454. acpi_memory_register_notify_handler, NULL,
  455. NULL, NULL);
  456. if (ACPI_FAILURE(status)) {
  457. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  458. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  459. return -ENODEV;
  460. }
  461. acpi_hotmem_initialized = 1;
  462. return 0;
  463. }
  464. static void __exit acpi_memory_device_exit(void)
  465. {
  466. acpi_status status;
  467. /*
  468. * Adding this to un-install notification handlers for all the device
  469. * handles.
  470. */
  471. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  472. ACPI_UINT32_MAX,
  473. acpi_memory_deregister_notify_handler, NULL,
  474. NULL, NULL);
  475. if (ACPI_FAILURE(status))
  476. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  477. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  478. return;
  479. }
  480. module_init(acpi_memory_device_init);
  481. module_exit(acpi_memory_device_exit);