acpi_memhotplug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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. ACPI_FUNCTION_TRACE("acpi_memory_get_device_resources");
  78. /* Get the range from the _CRS */
  79. status = acpi_get_current_resources(mem_device->handle, &buffer);
  80. if (ACPI_FAILURE(status))
  81. return_VALUE(-EINVAL);
  82. resource = (struct acpi_resource *)buffer.pointer;
  83. status = acpi_resource_to_address64(resource, &address64);
  84. if (ACPI_SUCCESS(status)) {
  85. if (address64.resource_type == ACPI_MEMORY_RANGE) {
  86. /* Populate the structure */
  87. mem_device->caching = address64.info.mem.caching;
  88. mem_device->write_protect =
  89. address64.info.mem.write_protect;
  90. mem_device->start_addr = address64.minimum;
  91. mem_device->length = address64.address_length;
  92. }
  93. }
  94. acpi_os_free(buffer.pointer);
  95. return_VALUE(0);
  96. }
  97. static int
  98. acpi_memory_get_device(acpi_handle handle,
  99. struct acpi_memory_device **mem_device)
  100. {
  101. acpi_status status;
  102. acpi_handle phandle;
  103. struct acpi_device *device = NULL;
  104. struct acpi_device *pdevice = NULL;
  105. ACPI_FUNCTION_TRACE("acpi_memory_get_device");
  106. if (!acpi_bus_get_device(handle, &device) && device)
  107. goto end;
  108. status = acpi_get_parent(handle, &phandle);
  109. if (ACPI_FAILURE(status)) {
  110. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_get_parent\n"));
  111. return_VALUE(-EINVAL);
  112. }
  113. /* Get the parent device */
  114. status = acpi_bus_get_device(phandle, &pdevice);
  115. if (ACPI_FAILURE(status)) {
  116. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  117. "Error in acpi_bus_get_device\n"));
  118. return_VALUE(-EINVAL);
  119. }
  120. /*
  121. * Now add the notified device. This creates the acpi_device
  122. * and invokes .add function
  123. */
  124. status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  125. if (ACPI_FAILURE(status)) {
  126. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error in acpi_bus_add\n"));
  127. return_VALUE(-EINVAL);
  128. }
  129. end:
  130. *mem_device = acpi_driver_data(device);
  131. if (!(*mem_device)) {
  132. printk(KERN_ERR "\n driver data not found");
  133. return_VALUE(-ENODEV);
  134. }
  135. return_VALUE(0);
  136. }
  137. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  138. {
  139. unsigned long current_status;
  140. ACPI_FUNCTION_TRACE("acpi_memory_check_device");
  141. /* Get device present/absent information from the _STA */
  142. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->handle, "_STA",
  143. NULL, &current_status)))
  144. return_VALUE(-ENODEV);
  145. /*
  146. * Check for device status. Device should be
  147. * present/enabled/functioning.
  148. */
  149. if (!((current_status & ACPI_MEMORY_STA_PRESENT)
  150. && (current_status & ACPI_MEMORY_STA_ENABLED)
  151. && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
  152. return_VALUE(-ENODEV);
  153. return_VALUE(0);
  154. }
  155. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  156. {
  157. int result;
  158. ACPI_FUNCTION_TRACE("acpi_memory_enable_device");
  159. /* Get the range from the _CRS */
  160. result = acpi_memory_get_device_resources(mem_device);
  161. if (result) {
  162. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  163. "\nget_device_resources failed\n"));
  164. mem_device->state = MEMORY_INVALID_STATE;
  165. return result;
  166. }
  167. /*
  168. * Tell the VM there is more memory here...
  169. * Note: Assume that this function returns zero on success
  170. */
  171. result = add_memory(mem_device->start_addr, mem_device->length);
  172. if (result) {
  173. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "\nadd_memory failed\n"));
  174. mem_device->state = MEMORY_INVALID_STATE;
  175. return result;
  176. }
  177. return result;
  178. }
  179. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  180. {
  181. acpi_status status;
  182. struct acpi_object_list arg_list;
  183. union acpi_object arg;
  184. unsigned long current_status;
  185. ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device");
  186. /* Issue the _EJ0 command */
  187. arg_list.count = 1;
  188. arg_list.pointer = &arg;
  189. arg.type = ACPI_TYPE_INTEGER;
  190. arg.integer.value = 1;
  191. status = acpi_evaluate_object(mem_device->handle,
  192. "_EJ0", &arg_list, NULL);
  193. /* Return on _EJ0 failure */
  194. if (ACPI_FAILURE(status)) {
  195. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_EJ0 failed.\n"));
  196. return_VALUE(-ENODEV);
  197. }
  198. /* Evalute _STA to check if the device is disabled */
  199. status = acpi_evaluate_integer(mem_device->handle, "_STA",
  200. NULL, &current_status);
  201. if (ACPI_FAILURE(status))
  202. return_VALUE(-ENODEV);
  203. /* Check for device status. Device should be disabled */
  204. if (current_status & ACPI_MEMORY_STA_ENABLED)
  205. return_VALUE(-EINVAL);
  206. return_VALUE(0);
  207. }
  208. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  209. {
  210. int result;
  211. u64 start = mem_device->start_addr;
  212. u64 len = mem_device->length;
  213. ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
  214. /*
  215. * Ask the VM to offline this memory range.
  216. * Note: Assume that this function returns zero on success
  217. */
  218. result = remove_memory(start, len);
  219. if (result) {
  220. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n"));
  221. return_VALUE(result);
  222. }
  223. /* Power-off and eject the device */
  224. result = acpi_memory_powerdown_device(mem_device);
  225. if (result) {
  226. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  227. "Device Power Down failed.\n"));
  228. /* Set the status of the device to invalid */
  229. mem_device->state = MEMORY_INVALID_STATE;
  230. return result;
  231. }
  232. mem_device->state = MEMORY_POWER_OFF_STATE;
  233. return result;
  234. }
  235. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  236. {
  237. struct acpi_memory_device *mem_device;
  238. struct acpi_device *device;
  239. ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
  240. switch (event) {
  241. case ACPI_NOTIFY_BUS_CHECK:
  242. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  243. "\nReceived BUS CHECK notification for device\n"));
  244. /* Fall Through */
  245. case ACPI_NOTIFY_DEVICE_CHECK:
  246. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  247. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  248. "\nReceived DEVICE CHECK notification for device\n"));
  249. if (acpi_memory_get_device(handle, &mem_device)) {
  250. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  251. "Error in finding driver data\n"));
  252. return_VOID;
  253. }
  254. if (!acpi_memory_check_device(mem_device)) {
  255. if (acpi_memory_enable_device(mem_device))
  256. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  257. "Error in acpi_memory_enable_device\n"));
  258. }
  259. break;
  260. case ACPI_NOTIFY_EJECT_REQUEST:
  261. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  262. "\nReceived EJECT REQUEST notification for device\n"));
  263. if (acpi_bus_get_device(handle, &device)) {
  264. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  265. "Device doesn't exist\n"));
  266. break;
  267. }
  268. mem_device = acpi_driver_data(device);
  269. if (!mem_device) {
  270. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  271. "Driver Data is NULL\n"));
  272. break;
  273. }
  274. /*
  275. * Currently disabling memory device from kernel mode
  276. * TBD: Can also be disabled from user mode scripts
  277. * TBD: Can also be disabled by Callback registration
  278. * with generic sysfs driver
  279. */
  280. if (acpi_memory_disable_device(mem_device))
  281. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  282. "Error in acpi_memory_disable_device\n"));
  283. /*
  284. * TBD: Invoke acpi_bus_remove to cleanup data structures
  285. */
  286. break;
  287. default:
  288. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  289. "Unsupported event [0x%x]\n", event));
  290. break;
  291. }
  292. return_VOID;
  293. }
  294. static int acpi_memory_device_add(struct acpi_device *device)
  295. {
  296. int result;
  297. struct acpi_memory_device *mem_device = NULL;
  298. ACPI_FUNCTION_TRACE("acpi_memory_device_add");
  299. if (!device)
  300. return_VALUE(-EINVAL);
  301. mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  302. if (!mem_device)
  303. return_VALUE(-ENOMEM);
  304. memset(mem_device, 0, sizeof(struct acpi_memory_device));
  305. mem_device->handle = device->handle;
  306. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  307. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  308. acpi_driver_data(device) = mem_device;
  309. /* Get the range from the _CRS */
  310. result = acpi_memory_get_device_resources(mem_device);
  311. if (result) {
  312. kfree(mem_device);
  313. return_VALUE(result);
  314. }
  315. /* Set the device state */
  316. mem_device->state = MEMORY_POWER_ON_STATE;
  317. printk(KERN_INFO "%s \n", acpi_device_name(device));
  318. return_VALUE(result);
  319. }
  320. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  321. {
  322. struct acpi_memory_device *mem_device = NULL;
  323. ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
  324. if (!device || !acpi_driver_data(device))
  325. return_VALUE(-EINVAL);
  326. mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
  327. kfree(mem_device);
  328. return_VALUE(0);
  329. }
  330. /*
  331. * Helper function to check for memory device
  332. */
  333. static acpi_status is_memory_device(acpi_handle handle)
  334. {
  335. char *hardware_id;
  336. acpi_status status;
  337. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  338. struct acpi_device_info *info;
  339. ACPI_FUNCTION_TRACE("is_memory_device");
  340. status = acpi_get_object_info(handle, &buffer);
  341. if (ACPI_FAILURE(status))
  342. return_ACPI_STATUS(AE_ERROR);
  343. info = buffer.pointer;
  344. if (!(info->valid & ACPI_VALID_HID)) {
  345. acpi_os_free(buffer.pointer);
  346. return_ACPI_STATUS(AE_ERROR);
  347. }
  348. hardware_id = info->hardware_id.value;
  349. if ((hardware_id == NULL) ||
  350. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  351. status = AE_ERROR;
  352. acpi_os_free(buffer.pointer);
  353. return_ACPI_STATUS(status);
  354. }
  355. static acpi_status
  356. acpi_memory_register_notify_handler(acpi_handle handle,
  357. u32 level, void *ctxt, void **retv)
  358. {
  359. acpi_status status;
  360. ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
  361. status = is_memory_device(handle);
  362. if (ACPI_FAILURE(status))
  363. return_ACPI_STATUS(AE_OK); /* continue */
  364. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  365. acpi_memory_device_notify, NULL);
  366. if (ACPI_FAILURE(status)) {
  367. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  368. "Error installing notify handler\n"));
  369. return_ACPI_STATUS(AE_OK); /* continue */
  370. }
  371. return_ACPI_STATUS(status);
  372. }
  373. static acpi_status
  374. acpi_memory_deregister_notify_handler(acpi_handle handle,
  375. u32 level, void *ctxt, void **retv)
  376. {
  377. acpi_status status;
  378. ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
  379. status = is_memory_device(handle);
  380. if (ACPI_FAILURE(status))
  381. return_ACPI_STATUS(AE_OK); /* continue */
  382. status = acpi_remove_notify_handler(handle,
  383. ACPI_SYSTEM_NOTIFY,
  384. acpi_memory_device_notify);
  385. if (ACPI_FAILURE(status)) {
  386. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  387. "Error removing notify handler\n"));
  388. return_ACPI_STATUS(AE_OK); /* continue */
  389. }
  390. return_ACPI_STATUS(status);
  391. }
  392. static int __init acpi_memory_device_init(void)
  393. {
  394. int result;
  395. acpi_status status;
  396. ACPI_FUNCTION_TRACE("acpi_memory_device_init");
  397. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  398. if (result < 0)
  399. return_VALUE(-ENODEV);
  400. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  401. ACPI_UINT32_MAX,
  402. acpi_memory_register_notify_handler,
  403. NULL, NULL);
  404. if (ACPI_FAILURE(status)) {
  405. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
  406. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  407. return_VALUE(-ENODEV);
  408. }
  409. return_VALUE(0);
  410. }
  411. static void __exit acpi_memory_device_exit(void)
  412. {
  413. acpi_status status;
  414. ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
  415. /*
  416. * Adding this to un-install notification handlers for all the device
  417. * handles.
  418. */
  419. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  420. ACPI_UINT32_MAX,
  421. acpi_memory_deregister_notify_handler,
  422. NULL, NULL);
  423. if (ACPI_FAILURE(status))
  424. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
  425. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  426. return_VOID;
  427. }
  428. module_init(acpi_memory_device_init);
  429. module_exit(acpi_memory_device_exit);