acpi_memhotplug.c 14 KB

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