acpi_memhotplug.c 14 KB

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