acpi_memhotplug.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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 firmare 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_COMPONENT 0x08000000UL
  34. #define ACPI_MEMORY_DEVICE_CLASS "memory"
  35. #define ACPI_MEMORY_DEVICE_HID "PNP0C80"
  36. #define ACPI_MEMORY_DEVICE_DRIVER_NAME "Hotplug Mem Driver"
  37. #define ACPI_MEMORY_DEVICE_NAME "Hotplug Mem Device"
  38. #define _COMPONENT ACPI_MEMORY_DEVICE_COMPONENT
  39. ACPI_MODULE_NAME("acpi_memory")
  40. MODULE_AUTHOR("Naveen B S <naveen.b.s@intel.com>");
  41. MODULE_DESCRIPTION(ACPI_MEMORY_DEVICE_DRIVER_NAME);
  42. MODULE_LICENSE("GPL");
  43. /* ACPI _STA method values */
  44. #define ACPI_MEMORY_STA_PRESENT (0x00000001UL)
  45. #define ACPI_MEMORY_STA_ENABLED (0x00000002UL)
  46. #define ACPI_MEMORY_STA_FUNCTIONAL (0x00000008UL)
  47. /* Memory Device States */
  48. #define MEMORY_INVALID_STATE 0
  49. #define MEMORY_POWER_ON_STATE 1
  50. #define MEMORY_POWER_OFF_STATE 2
  51. static int acpi_memory_device_add(struct acpi_device *device);
  52. static int acpi_memory_device_remove(struct acpi_device *device, int type);
  53. static struct acpi_driver acpi_memory_device_driver = {
  54. .name = ACPI_MEMORY_DEVICE_DRIVER_NAME,
  55. .class = ACPI_MEMORY_DEVICE_CLASS,
  56. .ids = ACPI_MEMORY_DEVICE_HID,
  57. .ops = {
  58. .add = acpi_memory_device_add,
  59. .remove = acpi_memory_device_remove,
  60. },
  61. };
  62. struct acpi_memory_device {
  63. acpi_handle handle;
  64. unsigned int state; /* State of the memory device */
  65. unsigned short caching; /* memory cache attribute */
  66. unsigned short write_protect; /* memory read/write attribute */
  67. u64 start_addr; /* Memory Range start physical addr */
  68. u64 length; /* Memory Range length */
  69. };
  70. static int
  71. acpi_memory_get_device_resources(struct acpi_memory_device *mem_device)
  72. {
  73. acpi_status status;
  74. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  75. struct acpi_resource *resource = NULL;
  76. struct acpi_resource_address64 address64;
  77. /* Get the range from the _CRS */
  78. status = acpi_get_current_resources(mem_device->handle, &buffer);
  79. if (ACPI_FAILURE(status))
  80. return -EINVAL;
  81. resource = (struct acpi_resource *)buffer.pointer;
  82. status = acpi_resource_to_address64(resource, &address64);
  83. if (ACPI_SUCCESS(status)) {
  84. if (address64.resource_type == ACPI_MEMORY_RANGE) {
  85. /* Populate the structure */
  86. mem_device->caching = address64.info.mem.caching;
  87. mem_device->write_protect =
  88. address64.info.mem.write_protect;
  89. mem_device->start_addr = address64.minimum;
  90. mem_device->length = address64.address_length;
  91. }
  92. }
  93. acpi_os_free(buffer.pointer);
  94. return 0;
  95. }
  96. static int
  97. acpi_memory_get_device(acpi_handle handle,
  98. struct acpi_memory_device **mem_device)
  99. {
  100. acpi_status status;
  101. acpi_handle phandle;
  102. struct acpi_device *device = NULL;
  103. struct acpi_device *pdevice = NULL;
  104. if (!acpi_bus_get_device(handle, &device) && device)
  105. goto end;
  106. status = acpi_get_parent(handle, &phandle);
  107. if (ACPI_FAILURE(status)) {
  108. ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
  109. return -EINVAL;
  110. }
  111. /* Get the parent device */
  112. status = acpi_bus_get_device(phandle, &pdevice);
  113. if (ACPI_FAILURE(status)) {
  114. ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
  115. return -EINVAL;
  116. }
  117. /*
  118. * Now add the notified device. This creates the acpi_device
  119. * and invokes .add function
  120. */
  121. status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  122. if (ACPI_FAILURE(status)) {
  123. ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
  124. return -EINVAL;
  125. }
  126. end:
  127. *mem_device = acpi_driver_data(device);
  128. if (!(*mem_device)) {
  129. printk(KERN_ERR "\n driver data not found");
  130. return -ENODEV;
  131. }
  132. return 0;
  133. }
  134. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  135. {
  136. unsigned long current_status;
  137. /* Get device present/absent information from the _STA */
  138. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
  139. NULL, &current_status)))
  140. return -ENODEV;
  141. /*
  142. * Check for device status. Device should be
  143. * present/enabled/functioning.
  144. */
  145. if (!((current_status & ACPI_MEMORY_STA_PRESENT)
  146. && (current_status & ACPI_MEMORY_STA_ENABLED)
  147. && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
  148. return -ENODEV;
  149. return 0;
  150. }
  151. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  152. {
  153. int result;
  154. /* Get the range from the _CRS */
  155. result = acpi_memory_get_device_resources(mem_device);
  156. if (result) {
  157. printk(KERN_ERR PREFIX "get_device_resources failed\n");
  158. mem_device->state = MEMORY_INVALID_STATE;
  159. return result;
  160. }
  161. /*
  162. * Tell the VM there is more memory here...
  163. * Note: Assume that this function returns zero on success
  164. */
  165. result = add_memory(mem_device->start_addr, mem_device->length);
  166. if (result) {
  167. printk(KERN_ERR PREFIX "add_memory failed\n");
  168. mem_device->state = MEMORY_INVALID_STATE;
  169. return result;
  170. }
  171. return result;
  172. }
  173. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  174. {
  175. acpi_status status;
  176. struct acpi_object_list arg_list;
  177. union acpi_object arg;
  178. unsigned long current_status;
  179. /* Issue the _EJ0 command */
  180. arg_list.count = 1;
  181. arg_list.pointer = &arg;
  182. arg.type = ACPI_TYPE_INTEGER;
  183. arg.integer.value = 1;
  184. status = acpi_evaluate_object(mem_device->handle,
  185. "_EJ0", &arg_list, NULL);
  186. /* Return on _EJ0 failure */
  187. if (ACPI_FAILURE(status)) {
  188. ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
  189. return -ENODEV;
  190. }
  191. /* Evalute _STA to check if the device is disabled */
  192. status = acpi_evaluate_integer(mem_device->handle, "_STA",
  193. NULL, &current_status);
  194. if (ACPI_FAILURE(status))
  195. return -ENODEV;
  196. /* Check for device status. Device should be disabled */
  197. if (current_status & ACPI_MEMORY_STA_ENABLED)
  198. return -EINVAL;
  199. return 0;
  200. }
  201. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  202. {
  203. int result;
  204. u64 start = mem_device->start_addr;
  205. u64 len = mem_device->length;
  206. /*
  207. * Ask the VM to offline this memory range.
  208. * Note: Assume that this function returns zero on success
  209. */
  210. result = remove_memory(start, len);
  211. if (result)
  212. return result;
  213. /* Power-off and eject the device */
  214. result = acpi_memory_powerdown_device(mem_device);
  215. if (result) {
  216. /* Set the status of the device to invalid */
  217. mem_device->state = MEMORY_INVALID_STATE;
  218. return result;
  219. }
  220. mem_device->state = MEMORY_POWER_OFF_STATE;
  221. return result;
  222. }
  223. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  224. {
  225. struct acpi_memory_device *mem_device;
  226. struct acpi_device *device;
  227. switch (event) {
  228. case ACPI_NOTIFY_BUS_CHECK:
  229. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  230. "\nReceived BUS CHECK notification for device\n"));
  231. /* Fall Through */
  232. case ACPI_NOTIFY_DEVICE_CHECK:
  233. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  234. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  235. "\nReceived DEVICE CHECK notification for device\n"));
  236. if (acpi_memory_get_device(handle, &mem_device)) {
  237. printk(KERN_ERR PREFIX "Cannot find driver data\n");
  238. return;
  239. }
  240. if (!acpi_memory_check_device(mem_device)) {
  241. if (acpi_memory_enable_device(mem_device))
  242. printk(KERN_ERR PREFIX
  243. "Cannot enable memory device\n");
  244. }
  245. break;
  246. case ACPI_NOTIFY_EJECT_REQUEST:
  247. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  248. "\nReceived EJECT REQUEST notification for device\n"));
  249. if (acpi_bus_get_device(handle, &device)) {
  250. printk(KERN_ERR PREFIX "Device doesn't exist\n");
  251. break;
  252. }
  253. mem_device = acpi_driver_data(device);
  254. if (!mem_device) {
  255. printk(KERN_ERR PREFIX "Driver Data is NULL\n");
  256. break;
  257. }
  258. /*
  259. * Currently disabling memory device from kernel mode
  260. * TBD: Can also be disabled from user mode scripts
  261. * TBD: Can also be disabled by Callback registration
  262. * with generic sysfs driver
  263. */
  264. if (acpi_memory_disable_device(mem_device))
  265. printk(KERN_ERR PREFIX
  266. "Disable memory device\n");
  267. /*
  268. * TBD: Invoke acpi_bus_remove to cleanup data structures
  269. */
  270. break;
  271. default:
  272. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  273. "Unsupported event [0x%x]\n", event));
  274. break;
  275. }
  276. return;
  277. }
  278. static int acpi_memory_device_add(struct acpi_device *device)
  279. {
  280. int result;
  281. struct acpi_memory_device *mem_device = NULL;
  282. if (!device)
  283. return -EINVAL;
  284. mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  285. if (!mem_device)
  286. return -ENOMEM;
  287. memset(mem_device, 0, sizeof(struct acpi_memory_device));
  288. mem_device->handle = device->handle;
  289. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  290. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  291. acpi_driver_data(device) = mem_device;
  292. /* Get the range from the _CRS */
  293. result = acpi_memory_get_device_resources(mem_device);
  294. if (result) {
  295. kfree(mem_device);
  296. return result;
  297. }
  298. /* Set the device state */
  299. mem_device->state = MEMORY_POWER_ON_STATE;
  300. printk(KERN_INFO "%s \n", acpi_device_name(device));
  301. return result;
  302. }
  303. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  304. {
  305. struct acpi_memory_device *mem_device = NULL;
  306. if (!device || !acpi_driver_data(device))
  307. return -EINVAL;
  308. mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
  309. kfree(mem_device);
  310. return 0;
  311. }
  312. /*
  313. * Helper function to check for memory device
  314. */
  315. static acpi_status is_memory_device(acpi_handle handle)
  316. {
  317. char *hardware_id;
  318. acpi_status status;
  319. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  320. struct acpi_device_info *info;
  321. status = acpi_get_object_info(handle, &buffer);
  322. if (ACPI_FAILURE(status))
  323. return status;
  324. info = buffer.pointer;
  325. if (!(info->valid & ACPI_VALID_HID)) {
  326. acpi_os_free(buffer.pointer);
  327. return AE_ERROR;
  328. }
  329. hardware_id = info->hardware_id.value;
  330. if ((hardware_id == NULL) ||
  331. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  332. status = AE_ERROR;
  333. acpi_os_free(buffer.pointer);
  334. return status;
  335. }
  336. static acpi_status
  337. acpi_memory_register_notify_handler(acpi_handle handle,
  338. u32 level, void *ctxt, void **retv)
  339. {
  340. acpi_status status;
  341. status = is_memory_device(handle);
  342. if (ACPI_FAILURE(status)){
  343. ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
  344. return AE_OK; /* continue */
  345. }
  346. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  347. acpi_memory_device_notify, NULL);
  348. /* continue */
  349. return AE_OK;
  350. }
  351. static acpi_status
  352. acpi_memory_deregister_notify_handler(acpi_handle handle,
  353. u32 level, void *ctxt, void **retv)
  354. {
  355. acpi_status status;
  356. status = is_memory_device(handle);
  357. if (ACPI_FAILURE(status)){
  358. ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
  359. return AE_OK; /* continue */
  360. }
  361. status = acpi_remove_notify_handler(handle,
  362. ACPI_SYSTEM_NOTIFY,
  363. acpi_memory_device_notify);
  364. return AE_OK; /* continue */
  365. }
  366. static int __init acpi_memory_device_init(void)
  367. {
  368. int result;
  369. acpi_status status;
  370. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  371. if (result < 0)
  372. return -ENODEV;
  373. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  374. ACPI_UINT32_MAX,
  375. acpi_memory_register_notify_handler,
  376. NULL, NULL);
  377. if (ACPI_FAILURE(status)) {
  378. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  379. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  380. return -ENODEV;
  381. }
  382. return 0;
  383. }
  384. static void __exit acpi_memory_device_exit(void)
  385. {
  386. acpi_status status;
  387. /*
  388. * Adding this to un-install notification handlers for all the device
  389. * handles.
  390. */
  391. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  392. ACPI_UINT32_MAX,
  393. acpi_memory_deregister_notify_handler,
  394. NULL, NULL);
  395. if (ACPI_FAILURE(status))
  396. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  397. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  398. return;
  399. }
  400. module_init(acpi_memory_device_init);
  401. module_exit(acpi_memory_device_exit);