acpi_memhotplug.c 16 KB

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