acpi_memhotplug.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  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 int acpi_memory_device_start(struct acpi_device *device);
  54. static struct acpi_driver acpi_memory_device_driver = {
  55. .name = ACPI_MEMORY_DEVICE_DRIVER_NAME,
  56. .class = ACPI_MEMORY_DEVICE_CLASS,
  57. .ids = ACPI_MEMORY_DEVICE_HID,
  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 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. status = acpi_walk_resources(mem_device->device->handle, METHOD_NAME__CRS,
  114. acpi_memory_get_resource, mem_device);
  115. if (ACPI_FAILURE(status)) {
  116. list_for_each_entry_safe(info, n, &mem_device->res_list, list)
  117. kfree(info);
  118. return -EINVAL;
  119. }
  120. return 0;
  121. }
  122. static int
  123. acpi_memory_get_device(acpi_handle handle,
  124. struct acpi_memory_device **mem_device)
  125. {
  126. acpi_status status;
  127. acpi_handle phandle;
  128. struct acpi_device *device = NULL;
  129. struct acpi_device *pdevice = NULL;
  130. if (!acpi_bus_get_device(handle, &device) && device)
  131. goto end;
  132. status = acpi_get_parent(handle, &phandle);
  133. if (ACPI_FAILURE(status)) {
  134. ACPI_EXCEPTION((AE_INFO, status, "Cannot find acpi parent"));
  135. return -EINVAL;
  136. }
  137. /* Get the parent device */
  138. status = acpi_bus_get_device(phandle, &pdevice);
  139. if (ACPI_FAILURE(status)) {
  140. ACPI_EXCEPTION((AE_INFO, status, "Cannot get acpi bus device"));
  141. return -EINVAL;
  142. }
  143. /*
  144. * Now add the notified device. This creates the acpi_device
  145. * and invokes .add function
  146. */
  147. status = acpi_bus_add(&device, pdevice, handle, ACPI_BUS_TYPE_DEVICE);
  148. if (ACPI_FAILURE(status)) {
  149. ACPI_EXCEPTION((AE_INFO, status, "Cannot add acpi bus"));
  150. return -EINVAL;
  151. }
  152. end:
  153. *mem_device = acpi_driver_data(device);
  154. if (!(*mem_device)) {
  155. printk(KERN_ERR "\n driver data not found");
  156. return -ENODEV;
  157. }
  158. return 0;
  159. }
  160. static int acpi_memory_check_device(struct acpi_memory_device *mem_device)
  161. {
  162. unsigned long current_status;
  163. /* Get device present/absent information from the _STA */
  164. if (ACPI_FAILURE(acpi_evaluate_integer(mem_device->device->handle, "_STA",
  165. NULL, &current_status)))
  166. return -ENODEV;
  167. /*
  168. * Check for device status. Device should be
  169. * present/enabled/functioning.
  170. */
  171. if (!((current_status & ACPI_MEMORY_STA_PRESENT)
  172. && (current_status & ACPI_MEMORY_STA_ENABLED)
  173. && (current_status & ACPI_MEMORY_STA_FUNCTIONAL)))
  174. return -ENODEV;
  175. return 0;
  176. }
  177. static int acpi_memory_enable_device(struct acpi_memory_device *mem_device)
  178. {
  179. int result, num_enabled = 0;
  180. struct acpi_memory_info *info;
  181. int node;
  182. /* Get the range from the _CRS */
  183. result = acpi_memory_get_device_resources(mem_device);
  184. if (result) {
  185. printk(KERN_ERR PREFIX "get_device_resources failed\n");
  186. mem_device->state = MEMORY_INVALID_STATE;
  187. return result;
  188. }
  189. node = acpi_get_node(mem_device->device->handle);
  190. /*
  191. * Tell the VM there is more memory here...
  192. * Note: Assume that this function returns zero on success
  193. * We don't have memory-hot-add rollback function,now.
  194. * (i.e. memory-hot-remove function)
  195. */
  196. list_for_each_entry(info, &mem_device->res_list, list) {
  197. u64 start_pfn, end_pfn;
  198. start_pfn = info->start_addr >> PAGE_SHIFT;
  199. end_pfn = (info->start_addr + info->length - 1) >> PAGE_SHIFT;
  200. if (pfn_valid(start_pfn) || pfn_valid(end_pfn)) {
  201. /* already enabled. try next area */
  202. num_enabled++;
  203. continue;
  204. }
  205. result = add_memory(node, info->start_addr, info->length);
  206. if (result)
  207. continue;
  208. info->enabled = 1;
  209. num_enabled++;
  210. }
  211. if (!num_enabled) {
  212. printk(KERN_ERR PREFIX "add_memory failed\n");
  213. mem_device->state = MEMORY_INVALID_STATE;
  214. return -EINVAL;
  215. }
  216. return result;
  217. }
  218. static int acpi_memory_powerdown_device(struct acpi_memory_device *mem_device)
  219. {
  220. acpi_status status;
  221. struct acpi_object_list arg_list;
  222. union acpi_object arg;
  223. unsigned long current_status;
  224. /* Issue the _EJ0 command */
  225. arg_list.count = 1;
  226. arg_list.pointer = &arg;
  227. arg.type = ACPI_TYPE_INTEGER;
  228. arg.integer.value = 1;
  229. status = acpi_evaluate_object(mem_device->device->handle,
  230. "_EJ0", &arg_list, NULL);
  231. /* Return on _EJ0 failure */
  232. if (ACPI_FAILURE(status)) {
  233. ACPI_EXCEPTION((AE_INFO, status, "_EJ0 failed"));
  234. return -ENODEV;
  235. }
  236. /* Evalute _STA to check if the device is disabled */
  237. status = acpi_evaluate_integer(mem_device->device->handle, "_STA",
  238. NULL, &current_status);
  239. if (ACPI_FAILURE(status))
  240. return -ENODEV;
  241. /* Check for device status. Device should be disabled */
  242. if (current_status & ACPI_MEMORY_STA_ENABLED)
  243. return -EINVAL;
  244. return 0;
  245. }
  246. static int acpi_memory_disable_device(struct acpi_memory_device *mem_device)
  247. {
  248. int result;
  249. struct acpi_memory_info *info, *n;
  250. /*
  251. * Ask the VM to offline this memory range.
  252. * Note: Assume that this function returns zero on success
  253. */
  254. list_for_each_entry_safe(info, n, &mem_device->res_list, list) {
  255. if (info->enabled) {
  256. result = remove_memory(info->start_addr, info->length);
  257. if (result)
  258. return result;
  259. }
  260. kfree(info);
  261. }
  262. /* Power-off and eject the device */
  263. result = acpi_memory_powerdown_device(mem_device);
  264. if (result) {
  265. /* Set the status of the device to invalid */
  266. mem_device->state = MEMORY_INVALID_STATE;
  267. return result;
  268. }
  269. mem_device->state = MEMORY_POWER_OFF_STATE;
  270. return result;
  271. }
  272. static void acpi_memory_device_notify(acpi_handle handle, u32 event, void *data)
  273. {
  274. struct acpi_memory_device *mem_device;
  275. struct acpi_device *device;
  276. switch (event) {
  277. case ACPI_NOTIFY_BUS_CHECK:
  278. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  279. "\nReceived BUS CHECK notification for device\n"));
  280. /* Fall Through */
  281. case ACPI_NOTIFY_DEVICE_CHECK:
  282. if (event == ACPI_NOTIFY_DEVICE_CHECK)
  283. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  284. "\nReceived DEVICE CHECK notification for device\n"));
  285. if (acpi_memory_get_device(handle, &mem_device)) {
  286. printk(KERN_ERR PREFIX "Cannot find driver data\n");
  287. return;
  288. }
  289. if (!acpi_memory_check_device(mem_device)) {
  290. if (acpi_memory_enable_device(mem_device))
  291. printk(KERN_ERR PREFIX
  292. "Cannot enable memory device\n");
  293. }
  294. break;
  295. case ACPI_NOTIFY_EJECT_REQUEST:
  296. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  297. "\nReceived EJECT REQUEST notification for device\n"));
  298. if (acpi_bus_get_device(handle, &device)) {
  299. printk(KERN_ERR PREFIX "Device doesn't exist\n");
  300. break;
  301. }
  302. mem_device = acpi_driver_data(device);
  303. if (!mem_device) {
  304. printk(KERN_ERR PREFIX "Driver Data is NULL\n");
  305. break;
  306. }
  307. /*
  308. * Currently disabling memory device from kernel mode
  309. * TBD: Can also be disabled from user mode scripts
  310. * TBD: Can also be disabled by Callback registration
  311. * with generic sysfs driver
  312. */
  313. if (acpi_memory_disable_device(mem_device))
  314. printk(KERN_ERR PREFIX
  315. "Disable memory device\n");
  316. /*
  317. * TBD: Invoke acpi_bus_remove to cleanup data structures
  318. */
  319. break;
  320. default:
  321. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  322. "Unsupported event [0x%x]\n", event));
  323. break;
  324. }
  325. return;
  326. }
  327. static int acpi_memory_device_add(struct acpi_device *device)
  328. {
  329. int result;
  330. struct acpi_memory_device *mem_device = NULL;
  331. if (!device)
  332. return -EINVAL;
  333. mem_device = kmalloc(sizeof(struct acpi_memory_device), GFP_KERNEL);
  334. if (!mem_device)
  335. return -ENOMEM;
  336. memset(mem_device, 0, sizeof(struct acpi_memory_device));
  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. acpi_driver_data(device) = 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_INFO "%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 = (struct acpi_memory_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. mem_device = acpi_driver_data(device);
  367. if (!acpi_memory_check_device(mem_device)) {
  368. /* call add_memory func */
  369. result = acpi_memory_enable_device(mem_device);
  370. if (result)
  371. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  372. "Error in acpi_memory_enable_device\n"));
  373. }
  374. return result;
  375. }
  376. /*
  377. * Helper function to check for memory device
  378. */
  379. static acpi_status is_memory_device(acpi_handle handle)
  380. {
  381. char *hardware_id;
  382. acpi_status status;
  383. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  384. struct acpi_device_info *info;
  385. status = acpi_get_object_info(handle, &buffer);
  386. if (ACPI_FAILURE(status))
  387. return status;
  388. info = buffer.pointer;
  389. if (!(info->valid & ACPI_VALID_HID)) {
  390. kfree(buffer.pointer);
  391. return AE_ERROR;
  392. }
  393. hardware_id = info->hardware_id.value;
  394. if ((hardware_id == NULL) ||
  395. (strcmp(hardware_id, ACPI_MEMORY_DEVICE_HID)))
  396. status = AE_ERROR;
  397. kfree(buffer.pointer);
  398. return status;
  399. }
  400. static acpi_status
  401. acpi_memory_register_notify_handler(acpi_handle handle,
  402. u32 level, void *ctxt, void **retv)
  403. {
  404. acpi_status status;
  405. status = is_memory_device(handle);
  406. if (ACPI_FAILURE(status)){
  407. ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
  408. return AE_OK; /* continue */
  409. }
  410. status = acpi_install_notify_handler(handle, ACPI_SYSTEM_NOTIFY,
  411. acpi_memory_device_notify, NULL);
  412. /* continue */
  413. return AE_OK;
  414. }
  415. static acpi_status
  416. acpi_memory_deregister_notify_handler(acpi_handle handle,
  417. u32 level, void *ctxt, void **retv)
  418. {
  419. acpi_status status;
  420. status = is_memory_device(handle);
  421. if (ACPI_FAILURE(status)){
  422. ACPI_EXCEPTION((AE_INFO, status, "handle is no memory device"));
  423. return AE_OK; /* continue */
  424. }
  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. return 0;
  447. }
  448. static void __exit acpi_memory_device_exit(void)
  449. {
  450. acpi_status status;
  451. /*
  452. * Adding this to un-install notification handlers for all the device
  453. * handles.
  454. */
  455. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, ACPI_ROOT_OBJECT,
  456. ACPI_UINT32_MAX,
  457. acpi_memory_deregister_notify_handler,
  458. NULL, NULL);
  459. if (ACPI_FAILURE(status))
  460. ACPI_EXCEPTION((AE_INFO, status, "walk_namespace failed"));
  461. acpi_bus_unregister_driver(&acpi_memory_device_driver);
  462. return;
  463. }
  464. module_init(acpi_memory_device_init);
  465. module_exit(acpi_memory_device_exit);