acpi_memhotplug.c 14 KB

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