acpi_memhotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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. };
  72. struct acpi_memory_device {
  73. struct acpi_device * device;
  74. unsigned int state; /* State of the memory device */
  75. struct list_head res_list;
  76. };
  77. static int acpi_hotmem_initialized;
  78. static acpi_status
  79. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  80. {
  81. struct acpi_memory_device *mem_device = context;
  82. struct acpi_resource_address64 address64;
  83. struct acpi_memory_info *info, *new;
  84. acpi_status status;
  85. status = acpi_resource_to_address64(resource, &address64);
  86. if (ACPI_FAILURE(status) ||
  87. (address64.resource_type != ACPI_MEMORY_RANGE))
  88. return AE_OK;
  89. list_for_each_entry(info, &mem_device->res_list, list) {
  90. /* Can we combine the resource range information? */
  91. if ((info->caching == address64.info.mem.caching) &&
  92. (info->write_protect == address64.info.mem.write_protect) &&
  93. (info->start_addr + info->length == address64.minimum)) {
  94. info->length += address64.address_length;
  95. return AE_OK;
  96. }
  97. }
  98. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  99. if (!new)
  100. return AE_ERROR;
  101. INIT_LIST_HEAD(&new->list);
  102. new->caching = address64.info.mem.caching;
  103. new->write_protect = address64.info.mem.write_protect;
  104. new->start_addr = address64.minimum;
  105. new->length = address64.address_length;
  106. list_add_tail(&new->list, &mem_device->res_list);
  107. return AE_OK;
  108. }
  109. static int
  110. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  111. {
  112. acpi_status status;
  113. struct acpi_memory_info *info, *n;
  114. if (!list_empty(&mem_device->res_list))
  115. return 0;
  116. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  117. acpi_memory_get_resource, mem_device);
  118. if (ACPI_FAILURE(status)) {
  119. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  120. kfree(info);
  121. INIT_LIST_HEAD(&mem_device->res_list);
  122. return -EINVAL;
  123. }
  124. return 0;
  125. }
  126. static int
  127. acpi_memory_get_device(acpi_handle handle,
  128. struct acpi_memory_device **mem_device)
  129. {
  130. acpi_status status;
  131. acpi_handle phandle;
  132. struct acpi_device *device = NULL;
  133. struct acpi_device *pdevice = NULL;
  134. int result;
  135. if (!acpi_bus_get_device(handle, &device) && device)
  136. goto end;
  137. status = acpi_get_parent(handle, &phandle);
  138. if (ACPI_FAILURE(status)) {
  139. ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
  140. return -EINVAL;
  141. }
  142. /* Get the parent device */
  143. result = acpi_bus_get_device(phandle, &pdevice);
  144. if (result) {
  145. printk(KERN_WARNING PREFIX "Cannot get acpi bus device");
  146. return -EINVAL;
  147. }
  148. /*
  149. * Now add the notified device. This creates the acpi_device
  150. * and invokes .add function
  151. */
  152. result = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  153. if (result) {
  154. printk(KERN_WARNING PREFIX "Cannot add acpi bus");
  155. return -EINVAL;
  156. }
  157. end:
  158. *mem_device = acpi_driver_data(device);
  159. if (!(*mem_device)) {
  160. printk(KERN_ERR "\n driver data not found");
  161. return -ENODEV;
  162. }
  163. return 0;
  164. }
  165. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  166. {
  167. unsigned long long current_status;
  168. /* Get device present/absent information from the _STA */
  169. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  170. NULL, &current_status)))
  171. return -ENODEV;
  172. /*
  173. * Check for device status. Device should be
  174. * present/enabled/functioning.
  175. */
  176. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  177. && (current_status & ACPI_STA_DEVICE_ENABLED)
  178. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  179. return -ENODEV;
  180. return 0;
  181. }
  182. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  183. {
  184. int result, num_enabled = 0;
  185. struct acpi_memory_info *info;
  186. int node;
  187. /* Get the range from the _CRS */
  188. result = acpi_memory_get_device_resources(mem_device);
  189. if (result) {
  190. printk(KERN_ERR PREFIX "get_device_resources failed\n");
  191. mem_device->state = MEMORY_INVALID_STATE;
  192. return result;
  193. }
  194. node = acpi_get_node(mem_device->device->handle);
  195. /*
  196. * Tell the VM there is more memory here...
  197. * Note: Assume that this function returns zero on success
  198. * We don't have memory-hot-add rollback function,now.
  199. * (i.e. memory-hot-remove function)
  200. */
  201. list_for_each_entry(info, &mem_device->res_list, list) {
  202. if (info->enabled) { /* just sanity check...*/
  203. num_enabled++;
  204. continue;
  205. }
  206. /*
  207. * If the memory block size is zero, please ignore it.
  208. * Don't try to do the following memory hotplug flowchart.
  209. */
  210. if (!info->length)
  211. continue;
  212. if (node < 0)
  213. node = memory_add_physaddr_to_nid(info->start_addr);
  214. result = add_memory(node, info->start_addr, info->length);
  215. if (result)
  216. continue;
  217. info->enabled = 1;
  218. num_enabled++;
  219. }
  220. if (!num_enabled) {
  221. printk(KERN_ERR PREFIX "add_memory failed\n");
  222. mem_device->state = MEMORY_INVALID_STATE;
  223. return -EINVAL;
  224. }
  225. /*
  226. * Sometimes the memory device will contain several memory blocks.
  227. * When one memory block is hot-added to the system memory, it will
  228. * be regarded as a success.
  229. * Otherwise if the last memory block can't be hot-added to the system
  230. * memory, it will be failure and the memory device can't be bound with
  231. * driver.
  232. */
  233. return 0;
  234. }
  235. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  236. {
  237. acpi_status status;
  238. struct acpi_object_list arg_list;
  239. union acpi_object arg;
  240. unsigned long long current_status;
  241. /* Issue the _EJ0 command */
  242. arg_list.count = 1;
  243. arg_list.pointer = &arg;
  244. arg.type = ACPI_TYPE_INTEGER;
  245. arg.integer.value = 1;
  246. status = acpi_evaluate_object(mem_device->device->handle,
  247. "_EJ0", &arg_list, NULL);
  248. /* Return on _EJ0 failure */
  249. if (ACPI_FAILURE(status)) {
  250. ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
  251. return -ENODEV;
  252. }
  253. /* Evalute _STA to check if the device is disabled */
  254. status = acpi_evaluate_integer(mem_device->device->handle, "_STA",
  255. NULL, &current_status);
  256. if (ACPI_FAILURE(status))
  257. return -ENODEV;
  258. /* Check for device status. Device should be disabled */
  259. if (current_status & ACPI_STA_DEVICE_ENABLED)
  260. return -EINVAL;
  261. return 0;
  262. }
  263. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  264. {
  265. int result;
  266. struct acpi_memory_info *info, *n;
  267. /*
  268. * Ask the VM to offline this memory range.
  269. * Note: Assume that this function returns zero on success
  270. */
  271. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  272. if (info->enabled) {
  273. result = remove_memory(info->start_addr, info->length);
  274. if (result)
  275. return result;
  276. }
  277. kfree(info);
  278. }
  279. /* Power-off and eject the device */
  280. result = acpi_memory_powerdown_device(mem_device);
  281. if (result) {
  282. /* Set the status of the device to invalid */
  283. mem_device->state = MEMORY_INVALID_STATE;
  284. return result;
  285. }
  286. mem_device->state = MEMORY_POWER_OFF_STATE;
  287. return result;
  288. }
  289. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  290. {
  291. struct acpi_memory_device *mem_device;
  292. struct acpi_device *device;
  293. u32 ost_code = ACPI_OST_SC_NON_SPECIFIC_FAILURE; /* default */
  294. switch (event) {
  295. case ACPI_NOTIFY_BUS_CHECK:
  296. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  297. "\nReceived BUS CHECK notification for device\n"));
  298. /* Fall Through */
  299. case ACPI_NOTIFY_DEVICE_CHECK:
  300. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  301. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  302. "\nReceived DEVICE CHECK notification for device\n"));
  303. if (acpi_memory_get_device(handle, &mem_device)) {
  304. printk(KERN_ERR PREFIX "Cannot find driver data\n");
  305. break;
  306. }
  307. if (acpi_memory_check_device(mem_device))
  308. break;
  309. if (acpi_memory_enable_device(mem_device)) {
  310. printk(KERN_ERR PREFIX "Cannot enable memory device\n");
  311. break;
  312. }
  313. ost_code = ACPI_OST_SC_SUCCESS;
  314. break;
  315. case ACPI_NOTIFY_EJECT_REQUEST:
  316. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  317. "\nReceived EJECT REQUEST notification for device\n"));
  318. if (acpi_bus_get_device(handle, &device)) {
  319. printk(KERN_ERR PREFIX "Device doesn't exist\n");
  320. break;
  321. }
  322. mem_device = acpi_driver_data(device);
  323. if (!mem_device) {
  324. printk(KERN_ERR PREFIX "Driver Data is NULL\n");
  325. break;
  326. }
  327. /*
  328. * Currently disabling memory device from kernel mode
  329. * TBD: Can also be disabled from user mode scripts
  330. * TBD: Can also be disabled by Callback registration
  331. * with generic sysfs driver
  332. */
  333. if (acpi_memory_disable_device(mem_device)) {
  334. printk(KERN_ERR PREFIX "Disable memory device\n");
  335. /*
  336. * If _EJ0 was called but failed, _OST is not
  337. * necessary.
  338. */
  339. if (mem_device->state == MEMORY_INVALID_STATE)
  340. return;
  341. break;
  342. }
  343. /*
  344. * TBD: Invoke acpi_bus_remove to cleanup data structures
  345. */
  346. /* _EJ0 succeeded; _OST is not necessary */
  347. return;
  348. default:
  349. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  350. "Unsupported event [0x%x]\n", event));
  351. /* non-hotplug event; possibly handled by other handler */
  352. return;
  353. }
  354. /* Inform firmware that the hotplug operation has completed */
  355. (void) acpi_evaluate_hotplug_ost(handle, event, ost_code, NULL);
  356. return;
  357. }
  358. static int acpi_memory_device_add(struct acpi_device *device)
  359. {
  360. int result;
  361. struct acpi_memory_device *mem_device = NULL;
  362. if (!device)
  363. return -EINVAL;
  364. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  365. if (!mem_device)
  366. return -ENOMEM;
  367. INIT_LIST_HEAD(&mem_device->res_list);
  368. mem_device->device = device;
  369. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  370. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  371. device->driver_data = mem_device;
  372. /* Get the range from the _CRS */
  373. result = acpi_memory_get_device_resources(mem_device);
  374. if (result) {
  375. kfree(mem_device);
  376. return result;
  377. }
  378. /* Set the device state */
  379. mem_device->state = MEMORY_POWER_ON_STATE;
  380. printk(KERN_DEBUG "%s \n", acpi_device_name(device));
  381. /*
  382. * Early boot code has recognized memory area by EFI/E820.
  383. * If DSDT shows these memory devices on boot, hotplug is not necessary
  384. * for them. So, it just returns until completion of this driver's
  385. * start up.
  386. */
  387. if (!acpi_hotmem_initialized)
  388. return 0;
  389. if (!acpi_memory_check_device(mem_device)) {
  390. /* call add_memory func */
  391. result = acpi_memory_enable_device(mem_device);
  392. if (result)
  393. printk(KERN_ERR PREFIX
  394. "Error in acpi_memory_enable_device\n");
  395. }
  396. return result;
  397. }
  398. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  399. {
  400. struct acpi_memory_device *mem_device = NULL;
  401. if (!device || !acpi_driver_data(device))
  402. return -EINVAL;
  403. mem_device = acpi_driver_data(device);
  404. kfree(mem_device);
  405. return 0;
  406. }
  407. /*
  408. * Helper function to check for memory device
  409. */
  410. static acpi_status is_memory_device(acpi_handle handle)
  411. {
  412. char *hardware_id;
  413. acpi_status status;
  414. struct acpi_device_info *info;
  415. status = acpi_get_object_info(handle, &info);
  416. if (ACPI_FAILURE(status))
  417. return status;
  418. if (!(info->valid & ACPI_VALID_HID)) {
  419. kfree(info);
  420. return AE_ERROR;
  421. }
  422. hardware_id = info->hardware_id.string;
  423. if ((hardware_id == NULL) ||
  424. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  425. status = AE_ERROR;
  426. kfree(info);
  427. return status;
  428. }
  429. static acpi_status
  430. acpi_memory_register_notify_handler(acpi_handle handle,
  431. u32 level, void *ctxt, void **retv)
  432. {
  433. acpi_status status;
  434. status = is_memory_device(handle);
  435. if (ACPI_FAILURE(status))
  436. return AE_OK; /* continue */
  437. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  438. acpi_memory_device_notify, NULL);
  439. /* continue */
  440. return AE_OK;
  441. }
  442. static acpi_status
  443. acpi_memory_deregister_notify_handler(acpi_handle handle,
  444. u32 level, void *ctxt, void **retv)
  445. {
  446. acpi_status status;
  447. status = is_memory_device(handle);
  448. if (ACPI_FAILURE(status))
  449. return AE_OK; /* continue */
  450. status = acpi_remove_notify_handler(handle,
  451. ACPI_SYSTEM_NOTIFY,
  452. acpi_memory_device_notify);
  453. return AE_OK; /* continue */
  454. }
  455. static int __init acpi_memory_device_init(void)
  456. {
  457. int result;
  458. acpi_status status;
  459. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  460. if (result < 0)
  461. return -ENODEV;
  462. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  463. ACPI_UINT32_MAX,
  464. acpi_memory_register_notify_handler, NULL,
  465. NULL, NULL);
  466. if (ACPI_FAILURE(status)) {
  467. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  468. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  469. return -ENODEV;
  470. }
  471. acpi_hotmem_initialized = 1;
  472. return 0;
  473. }
  474. static void __exit acpi_memory_device_exit(void)
  475. {
  476. acpi_status status;
  477. /*
  478. * Adding this to un-install notification handlers for all the device
  479. * handles.
  480. */
  481. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  482. ACPI_UINT32_MAX,
  483. acpi_memory_deregister_notify_handler, NULL,
  484. NULL, NULL);
  485. if (ACPI_FAILURE(status))
  486. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  487. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  488. return;
  489. }
  490. module_init(acpi_memory_device_init);
  491. module_exit(acpi_memory_device_exit);