acpi_memhotplug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563
  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. ACPI_MODULE_NAME("acpi_memhotplug");
  38. MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
  39. MODULE_DESCRIPTION("Hotplug Mem Driver");
  40. MODULE_LICENSE("GPL");
  41. /* Memory Device States */
  42. #define MEMORY_INVALID_STATE 0
  43. #define MEMORY_POWER_ON_STATE 1
  44. #define MEMORY_POWER_OFF_STATE 2
  45. static int acpi_memory_device_add(struct acpi_device *device);
  46. static int acpi_memory_device_remove(struct acpi_device *device, int type);
  47. static const struct acpi_device_id memory_device_ids[] = {
  48. {ACPI_MEMORY_DEVICE_HID, 0},
  49. {"", 0},
  50. };
  51. MODULE_DEVICE_TABLE(acpi, memory_device_ids);
  52. static struct acpi_driver acpi_memory_device_driver = {
  53. .name = "acpi_memhotplug",
  54. .class = ACPI_MEMORY_DEVICE_CLASS,
  55. .ids = memory_device_ids,
  56. .ops = {
  57. .add = acpi_memory_device_add,
  58. .remove = acpi_memory_device_remove,
  59. },
  60. };
  61. struct acpi_memory_info {
  62. struct list_head list;
  63. u64 start_addr; /* Memory Range start physical addr */
  64. u64 length; /* Memory Range length */
  65. unsigned short caching; /* memory cache attribute */
  66. unsigned short write_protect; /* memory read/write attribute */
  67. unsigned int enabled:1;
  68. };
  69. struct acpi_memory_device {
  70. struct acpi_device * device;
  71. unsigned int state; /* State of the memory device */
  72. struct list_head res_list;
  73. };
  74. static int acpi_hotmem_initialized;
  75. static acpi_status
  76. acpi_memory_get_resource(struct acpi_resource *resource, void *context)
  77. {
  78. struct acpi_memory_device *mem_device = context;
  79. struct acpi_resource_address64 address64;
  80. struct acpi_memory_info *info, *new;
  81. acpi_status status;
  82. status = acpi_resource_to_address64(resource, &address64);
  83. if (ACPI_FAILURE(status) ||
  84. (address64.resource_type != ACPI_MEMORY_RANGE))
  85. return AE_OK;
  86. list_for_each_entry(info, &mem_device->res_list, list) {
  87. /* Can we combine the resource range information? */
  88. if ((info->caching == address64.info.mem.caching) &&
  89. (info->write_protect == address64.info.mem.write_protect) &&
  90. (info->start_addr + info->length == address64.minimum)) {
  91. info->length += address64.address_length;
  92. return AE_OK;
  93. }
  94. }
  95. new = kzalloc(sizeof(struct acpi_memory_info), GFP_KERNEL);
  96. if (!new)
  97. return AE_ERROR;
  98. INIT_LIST_HEAD(&new->list);
  99. new->caching = address64.info.mem.caching;
  100. new->write_protect = address64.info.mem.write_protect;
  101. new->start_addr = address64.minimum;
  102. new->length = address64.address_length;
  103. list_add_tail(&new->list, &mem_device->res_list);
  104. return AE_OK;
  105. }
  106. static int
  107. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  108. {
  109. acpi_status status;
  110. struct acpi_memory_info *info, *n;
  111. if (!list_empty(&mem_device->res_list))
  112. return 0;
  113. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  114. acpi_memory_get_resource, mem_device);
  115. if (ACPI_FAILURE(status)) {
  116. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  117. kfree(info);
  118. INIT_LIST_HEAD(&mem_device->res_list);
  119. return -EINVAL;
  120. }
  121. return 0;
  122. }
  123. static int
  124. acpi_memory_get_device(acpi_handle handle,
  125. struct acpi_memory_device **mem_device)
  126. {
  127. acpi_status status;
  128. acpi_handle phandle;
  129. struct acpi_device *device = NULL;
  130. struct acpi_device *pdevice = NULL;
  131. if (!acpi_bus_get_device(handle, &device) && device)
  132. goto end;
  133. status = acpi_get_parent(handle, &phandle);
  134. if (ACPI_FAILURE(status)) {
  135. ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
  136. return -EINVAL;
  137. }
  138. /* Get the parent device */
  139. status = acpi_bus_get_device(phandle, &pdevice);
  140. if (ACPI_FAILURE(status)) {
  141. ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
  142. return -EINVAL;
  143. }
  144. /*
  145. * Now add the notified device. This creates the acpi_device
  146. * and invokes .add function
  147. */
  148. status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  149. if (ACPI_FAILURE(status)) {
  150. ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
  151. return -EINVAL;
  152. }
  153. end:
  154. *mem_device = acpi_driver_data(device);
  155. if (!(*mem_device)) {
  156. printk(KERN_ERR "\n driver data not found");
  157. return -ENODEV;
  158. }
  159. return 0;
  160. }
  161. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  162. {
  163. unsigned long long current_status;
  164. /* Get device present/absent information from the _STA */
  165. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  166. NULL, &current_status)))
  167. return -ENODEV;
  168. /*
  169. * Check for device status. Device should be
  170. * present/enabled/functioning.
  171. */
  172. if (!((current_status & ACPI_STA_DEVICE_PRESENT)
  173. && (current_status & ACPI_STA_DEVICE_ENABLED)
  174. && (current_status & ACPI_STA_DEVICE_FUNCTIONING)))
  175. return -ENODEV;
  176. return 0;
  177. }
  178. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  179. {
  180. int result, num_enabled = 0;
  181. struct acpi_memory_info *info;
  182. int node;
  183. /* Get the range from the _CRS */
  184. result = acpi_memory_get_device_resources(mem_device);
  185. if (result) {
  186. printk(KERN_ERR PREFIX "get_device_resources failed\n");
  187. mem_device->state = MEMORY_INVALID_STATE;
  188. return result;
  189. }
  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. if (node < 0)
  203. node = memory_add_physaddr_to_nid(info->start_addr);
  204. result = add_memory(node, info->start_addr, info->length);
  205. if (result)
  206. continue;
  207. info->enabled = 1;
  208. num_enabled++;
  209. }
  210. if (!num_enabled) {
  211. printk(KERN_ERR PREFIX "add_memory failed\n");
  212. mem_device->state = MEMORY_INVALID_STATE;
  213. return -EINVAL;
  214. }
  215. return result;
  216. }
  217. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  218. {
  219. acpi_status status;
  220. struct acpi_object_list arg_list;
  221. union acpi_object arg;
  222. unsigned long long current_status;
  223. /* Issue the _EJ0 command */
  224. arg_list.count = 1;
  225. arg_list.pointer = &arg;
  226. arg.type = ACPI_TYPE_INTEGER;
  227. arg.integer.value = 1;
  228. status = acpi_evaluate_object(mem_device->device->handle,
  229. "_EJ0", &arg_list, NULL);
  230. /* Return on _EJ0 failure */
  231. if (ACPI_FAILURE(status)) {
  232. ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
  233. return -ENODEV;
  234. }
  235. /* Evalute _STA to check if the device is disabled */
  236. status = acpi_evaluate_integer(mem_device->device->handle, "_STA",
  237. NULL, &current_status);
  238. if (ACPI_FAILURE(status))
  239. return -ENODEV;
  240. /* Check for device status. Device should be disabled */
  241. if (current_status & ACPI_STA_DEVICE_ENABLED)
  242. return -EINVAL;
  243. return 0;
  244. }
  245. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  246. {
  247. int result;
  248. struct acpi_memory_info *info, *n;
  249. /*
  250. * Ask the VM to offline this memory range.
  251. * Note: Assume that this function returns zero on success
  252. */
  253. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  254. if (info->enabled) {
  255. result = remove_memory(info->start_addr, info->length);
  256. if (result)
  257. return result;
  258. }
  259. kfree(info);
  260. }
  261. /* Power-off and eject the device */
  262. result = acpi_memory_powerdown_device(mem_device);
  263. if (result) {
  264. /* Set the status of the device to invalid */
  265. mem_device->state = MEMORY_INVALID_STATE;
  266. return result;
  267. }
  268. mem_device->state = MEMORY_POWER_OFF_STATE;
  269. return result;
  270. }
  271. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  272. {
  273. struct acpi_memory_device *mem_device;
  274. struct acpi_device *device;
  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. printk(KERN_ERR PREFIX "Cannot find driver data\n");
  286. return;
  287. }
  288. if (!acpi_memory_check_device(mem_device)) {
  289. if (acpi_memory_enable_device(mem_device))
  290. printk(KERN_ERR PREFIX
  291. "Cannot enable memory device\n");
  292. }
  293. break;
  294. case ACPI_NOTIFY_EJECT_REQUEST:
  295. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  296. "\nReceived EJECT REQUEST notification for device\n"));
  297. if (acpi_bus_get_device(handle, &device)) {
  298. printk(KERN_ERR PREFIX "Device doesn't exist\n");
  299. break;
  300. }
  301. mem_device = acpi_driver_data(device);
  302. if (!mem_device) {
  303. printk(KERN_ERR PREFIX "Driver Data is NULL\n");
  304. break;
  305. }
  306. /*
  307. * Currently disabling memory device from kernel mode
  308. * TBD: Can also be disabled from user mode scripts
  309. * TBD: Can also be disabled by Callback registration
  310. * with generic sysfs driver
  311. */
  312. if (acpi_memory_disable_device(mem_device))
  313. printk(KERN_ERR PREFIX
  314. "Disable memory device\n");
  315. /*
  316. * TBD: Invoke acpi_bus_remove to cleanup data structures
  317. */
  318. break;
  319. default:
  320. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  321. "Unsupported event [0x%x]\n", event));
  322. break;
  323. }
  324. return;
  325. }
  326. static int acpi_memory_device_add(struct acpi_device *device)
  327. {
  328. int result;
  329. struct acpi_memory_device *mem_device = NULL;
  330. if (!device)
  331. return -EINVAL;
  332. mem_device = kzalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  333. if (!mem_device)
  334. return -ENOMEM;
  335. INIT_LIST_HEAD(&mem_device->res_list);
  336. mem_device->device = device;
  337. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  338. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  339. device->driver_data = mem_device;
  340. /* Get the range from the _CRS */
  341. result = acpi_memory_get_device_resources(mem_device);
  342. if (result) {
  343. kfree(mem_device);
  344. return result;
  345. }
  346. /* Set the device state */
  347. mem_device->state = MEMORY_POWER_ON_STATE;
  348. printk(KERN_DEBUG "%s \n", acpi_device_name(device));
  349. /*
  350. * Early boot code has recognized memory area by EFI/E820.
  351. * If DSDT shows these memory devices on boot, hotplug is not necessary
  352. * for them. So, it just returns until completion of this driver's
  353. * start up.
  354. */
  355. if (!acpi_hotmem_initialized)
  356. return 0;
  357. if (!acpi_memory_check_device(mem_device)) {
  358. /* call add_memory func */
  359. result = acpi_memory_enable_device(mem_device);
  360. if (result)
  361. printk(KERN_ERR PREFIX
  362. "Error in acpi_memory_enable_device\n");
  363. }
  364. return result;
  365. }
  366. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  367. {
  368. struct acpi_memory_device *mem_device = NULL;
  369. if (!device || !acpi_driver_data(device))
  370. return -EINVAL;
  371. mem_device = acpi_driver_data(device);
  372. kfree(mem_device);
  373. return 0;
  374. }
  375. /*
  376. * Helper function to check for memory device
  377. */
  378. static acpi_status is_memory_device(acpi_handle handle)
  379. {
  380. char *hardware_id;
  381. acpi_status status;
  382. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  383. struct acpi_device_info *info;
  384. status = acpi_get_object_info(handle, &buffer);
  385. if (ACPI_FAILURE(status))
  386. return status;
  387. info = buffer.pointer;
  388. if (!(info->valid & ACPI_VALID_HID)) {
  389. kfree(buffer.pointer);
  390. return AE_ERROR;
  391. }
  392. hardware_id = info->hardware_id.value;
  393. if ((hardware_id == NULL) ||
  394. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  395. status = AE_ERROR;
  396. kfree(buffer.pointer);
  397. return status;
  398. }
  399. static acpi_status
  400. acpi_memory_register_notify_handler(acpi_handle handle,
  401. u32 level, void *ctxt, void **retv)
  402. {
  403. acpi_status status;
  404. status = is_memory_device(handle);
  405. if (ACPI_FAILURE(status))
  406. return AE_OK; /* continue */
  407. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  408. acpi_memory_device_notify, NULL);
  409. /* continue */
  410. return AE_OK;
  411. }
  412. static acpi_status
  413. acpi_memory_deregister_notify_handler(acpi_handle handle,
  414. u32 level, void *ctxt, void **retv)
  415. {
  416. acpi_status status;
  417. status = is_memory_device(handle);
  418. if (ACPI_FAILURE(status))
  419. return AE_OK; /* continue */
  420. status = acpi_remove_notify_handler(handle,
  421. ACPI_SYSTEM_NOTIFY,
  422. acpi_memory_device_notify);
  423. return AE_OK; /* continue */
  424. }
  425. static int __init acpi_memory_device_init(void)
  426. {
  427. int result;
  428. acpi_status status;
  429. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  430. if (result < 0)
  431. return -ENODEV;
  432. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  433. ACPI_UINT32_MAX,
  434. acpi_memory_register_notify_handler,
  435. NULL, NULL);
  436. if (ACPI_FAILURE(status)) {
  437. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  438. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  439. return -ENODEV;
  440. }
  441. acpi_hotmem_initialized = 1;
  442. return 0;
  443. }
  444. static void __exit acpi_memory_device_exit(void)
  445. {
  446. acpi_status status;
  447. /*
  448. * Adding this to un-install notification handlers for all the device
  449. * handles.
  450. */
  451. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  452. ACPI_UINT32_MAX,
  453. acpi_memory_deregister_notify_handler,
  454. NULL, NULL);
  455. if (ACPI_FAILURE(status))
  456. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  457. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  458. return;
  459. }
  460. module_init(acpi_memory_device_init);
  461. module_exit(acpi_memory_device_exit);