acpi_memhotplug.c 15 KB

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