acpi_memhotplug.c 15 KB

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