acpi_memhotplug.c 14 KB

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