acpi_memhotplug.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524
  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 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->caching =
  88. address64.info.mem.caching;
  89. mem_device->write_protect =
  90. address64.info.mem.write_protect;
  91. mem_device->start_addr = address64.minimum;
  92. mem_device->end_addr = address64.maximum;
  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. if (result) {
  175. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "\nadd_memory failed\n"));
  176. mem_device->state = MEMORY_INVALID_STATE;
  177. return result;
  178. }
  179. return result;
  180. }
  181. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  182. {
  183. acpi_status status;
  184. struct acpi_object_list arg_list;
  185. union acpi_object arg;
  186. unsigned long current_status;
  187. ACPI_FUNCTION_TRACE("acpi_memory_powerdown_device");
  188. /* Issue the _EJ0 command */
  189. arg_list.count = 1;
  190. arg_list.pointer = &arg;
  191. arg.type = ACPI_TYPE_INTEGER;
  192. arg.integer.value = 1;
  193. status = acpi_evaluate_object(mem_device->handle,
  194. "_EJ0", &arg_list, NULL);
  195. /* Return on _EJ0 failure */
  196. if (ACPI_FAILURE(status)) {
  197. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "_EJ0 failed.\n"));
  198. return_VALUE(-ENODEV);
  199. }
  200. /* Evalute _STA to check if the device is disabled */
  201. status = acpi_evaluate_integer(mem_device->handle, "_STA",
  202. NULL, &current_status);
  203. if (ACPI_FAILURE(status))
  204. return_VALUE(-ENODEV);
  205. /* Check for device status. Device should be disabled */
  206. if (current_status & ACPI_MEMORY_STA_ENABLED)
  207. return_VALUE(-EINVAL);
  208. return_VALUE(0);
  209. }
  210. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  211. {
  212. int result;
  213. u64 start = mem_device->start_addr;
  214. u64 len = mem_device->end_addr - start + 1;
  215. ACPI_FUNCTION_TRACE("acpi_memory_disable_device");
  216. /*
  217. * Ask the VM to offline this memory range.
  218. * Note: Assume that this function returns zero on success
  219. */
  220. result = remove_memory(start, len);
  221. if (result) {
  222. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Hot-Remove failed.\n"));
  223. return_VALUE(result);
  224. }
  225. /* Power-off and eject the device */
  226. result = acpi_memory_powerdown_device(mem_device);
  227. if (result) {
  228. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  229. "Device Power Down failed.\n"));
  230. /* Set the status of the device to invalid */
  231. mem_device->state = MEMORY_INVALID_STATE;
  232. return result;
  233. }
  234. mem_device->state = MEMORY_POWER_OFF_STATE;
  235. return result;
  236. }
  237. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  238. {
  239. struct acpi_memory_device *mem_device;
  240. struct acpi_device *device;
  241. ACPI_FUNCTION_TRACE("acpi_memory_device_notify");
  242. switch (event) {
  243. case ACPI_NOTIFY_BUS_CHECK:
  244. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  245. "\nReceived BUS CHECK notification for device\n"));
  246. /* Fall Through */
  247. case ACPI_NOTIFY_DEVICE_CHECK:
  248. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  249. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  250. "\nReceived DEVICE CHECK notification for device\n"));
  251. if (acpi_memory_get_device(handle, &mem_device)) {
  252. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  253. "Error in finding driver data\n"));
  254. return_VOID;
  255. }
  256. if (!acpi_memory_check_device(mem_device)) {
  257. if (acpi_memory_enable_device(mem_device))
  258. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  259. "Error in acpi_memory_enable_device\n"));
  260. }
  261. break;
  262. case ACPI_NOTIFY_EJECT_REQUEST:
  263. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  264. "\nReceived EJECT REQUEST notification for device\n"));
  265. if (acpi_bus_get_device(handle, &device)) {
  266. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  267. "Device doesn't exist\n"));
  268. break;
  269. }
  270. mem_device = acpi_driver_data(device);
  271. if (!mem_device) {
  272. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  273. "Driver Data is NULL\n"));
  274. break;
  275. }
  276. /*
  277. * Currently disabling memory device from kernel mode
  278. * TBD: Can also be disabled from user mode scripts
  279. * TBD: Can also be disabled by Callback registration
  280. * with generic sysfs driver
  281. */
  282. if (acpi_memory_disable_device(mem_device))
  283. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  284. "Error in acpi_memory_disable_device\n"));
  285. /*
  286. * TBD: Invoke acpi_bus_remove to cleanup data structures
  287. */
  288. break;
  289. default:
  290. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  291. "Unsupported event [0x%x]\n", event));
  292. break;
  293. }
  294. return_VOID;
  295. }
  296. static int acpi_memory_device_add(struct acpi_device *device)
  297. {
  298. int result;
  299. struct acpi_memory_device *mem_device = NULL;
  300. ACPI_FUNCTION_TRACE("acpi_memory_device_add");
  301. if (!device)
  302. return_VALUE(-EINVAL);
  303. mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  304. if (!mem_device)
  305. return_VALUE(-ENOMEM);
  306. memset(mem_device, 0, sizeof(struct acpi_memory_device));
  307. mem_device->handle = device->handle;
  308. sprintf(acpi_device_name(device), "%s", ACPI_MEMORY_DEVICE_NAME);
  309. sprintf(acpi_device_class(device), "%s", ACPI_MEMORY_DEVICE_CLASS);
  310. acpi_driver_data(device) = mem_device;
  311. /* Get the range from the _CRS */
  312. result = acpi_memory_get_device_resources(mem_device);
  313. if (result) {
  314. kfree(mem_device);
  315. return_VALUE(result);
  316. }
  317. /* Set the device state */
  318. mem_device->state = MEMORY_POWER_ON_STATE;
  319. printk(KERN_INFO "%s \n", acpi_device_name(device));
  320. return_VALUE(result);
  321. }
  322. static int acpi_memory_device_remove(struct acpi_device *device, int type)
  323. {
  324. struct acpi_memory_device *mem_device = NULL;
  325. ACPI_FUNCTION_TRACE("acpi_memory_device_remove");
  326. if (!device || !acpi_driver_data(device))
  327. return_VALUE(-EINVAL);
  328. mem_device = (struct acpi_memory_device *)acpi_driver_data(device);
  329. kfree(mem_device);
  330. return_VALUE(0);
  331. }
  332. /*
  333. * Helper function to check for memory device
  334. */
  335. static acpi_status is_memory_device(acpi_handle handle)
  336. {
  337. char *hardware_id;
  338. acpi_status status;
  339. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  340. struct acpi_device_info *info;
  341. ACPI_FUNCTION_TRACE("is_memory_device");
  342. status = acpi_get_object_info(handle, &buffer);
  343. if (ACPI_FAILURE(status))
  344. return_ACPI_STATUS(AE_ERROR);
  345. info = buffer.pointer;
  346. if (!(info->valid & ACPI_VALID_HID)) {
  347. acpi_os_free(buffer.pointer);
  348. return_ACPI_STATUS(AE_ERROR);
  349. }
  350. hardware_id = info->hardware_id.value;
  351. if ((hardware_id == NULL) ||
  352. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  353. status = AE_ERROR;
  354. acpi_os_free(buffer.pointer);
  355. return_ACPI_STATUS(status);
  356. }
  357. static acpi_status
  358. acpi_memory_register_notify_handler(acpi_handle handle,
  359. u32 level, void *ctxt, void **retv)
  360. {
  361. acpi_status status;
  362. ACPI_FUNCTION_TRACE("acpi_memory_register_notify_handler");
  363. status = is_memory_device(handle);
  364. if (ACPI_FAILURE(status))
  365. return_ACPI_STATUS(AE_OK); /* continue */
  366. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  367. acpi_memory_device_notify, NULL);
  368. if (ACPI_FAILURE(status)) {
  369. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  370. "Error installing notify handler\n"));
  371. return_ACPI_STATUS(AE_OK); /* continue */
  372. }
  373. return_ACPI_STATUS(status);
  374. }
  375. static acpi_status
  376. acpi_memory_deregister_notify_handler(acpi_handle handle,
  377. u32 level, void *ctxt, void **retv)
  378. {
  379. acpi_status status;
  380. ACPI_FUNCTION_TRACE("acpi_memory_deregister_notify_handler");
  381. status = is_memory_device(handle);
  382. if (ACPI_FAILURE(status))
  383. return_ACPI_STATUS(AE_OK); /* continue */
  384. status = acpi_remove_notify_handler(handle,
  385. ACPI_SYSTEM_NOTIFY,
  386. acpi_memory_device_notify);
  387. if (ACPI_FAILURE(status)) {
  388. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  389. "Error removing notify handler\n"));
  390. return_ACPI_STATUS(AE_OK); /* continue */
  391. }
  392. return_ACPI_STATUS(status);
  393. }
  394. static int __init acpi_memory_device_init(void)
  395. {
  396. int result;
  397. acpi_status status;
  398. ACPI_FUNCTION_TRACE("acpi_memory_device_init");
  399. result = acpi_bus_register_driver(&acpi_memory_device_driver);
  400. if (result < 0)
  401. return_VALUE(-ENODEV);
  402. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  403. ACPI_UINT32_MAX,
  404. acpi_memory_register_notify_handler,
  405. NULL, NULL);
  406. if (ACPI_FAILURE(status)) {
  407. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
  408. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  409. return_VALUE(-ENODEV);
  410. }
  411. return_VALUE(0);
  412. }
  413. static void __exit acpi_memory_device_exit(void)
  414. {
  415. acpi_status status;
  416. ACPI_FUNCTION_TRACE("acpi_memory_device_exit");
  417. /*
  418. * Adding this to un-install notification handlers for all the device
  419. * handles.
  420. */
  421. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  422. ACPI_UINT32_MAX,
  423. acpi_memory_deregister_notify_handler,
  424. NULL, NULL);
  425. if (ACPI_FAILURE(status))
  426. ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "walk_namespace failed\n"));
  427. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  428. return_VOID;
  429. }
  430. module_init(acpi_memory_device_init);
  431. module_exit(acpi_memory_device_exit);