ioapic.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*
  2. * IOAPIC/IOxAPIC/IOSAPIC driver
  3. *
  4. * Copyright (C) 2009 Fujitsu Limited.
  5. * (c) Copyright 2009 Hewlett-Packard Development Company, L.P.
  6. *
  7. * Copyright (C) 2014 Intel Corporation
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 as
  11. * published by the Free Software Foundation.
  12. *
  13. * Based on original drivers/pci/ioapic.c
  14. * Yinghai Lu <yinghai@kernel.org>
  15. * Jiang Liu <jiang.liu@intel.com>
  16. */
  17. /*
  18. * This driver manages I/O APICs added by hotplug after boot.
  19. * We try to claim all I/O APIC devices, but those present at boot were
  20. * registered when we parsed the ACPI MADT.
  21. */
  22. #define pr_fmt(fmt) "ACPI : IOAPIC: " fmt
  23. #include <linux/slab.h>
  24. #include <linux/acpi.h>
  25. #include <linux/pci.h>
  26. #include <acpi/acpi.h>
  27. struct acpi_pci_ioapic {
  28. acpi_handle root_handle;
  29. acpi_handle handle;
  30. u32 gsi_base;
  31. struct resource res;
  32. struct pci_dev *pdev;
  33. struct list_head list;
  34. };
  35. static LIST_HEAD(ioapic_list);
  36. static DEFINE_MUTEX(ioapic_list_lock);
  37. static acpi_status setup_res(struct acpi_resource *acpi_res, void *data)
  38. {
  39. struct resource *res = data;
  40. struct resource_win win;
  41. /*
  42. * We might assign this to 'res' later, make sure all pointers are
  43. * cleared before the resource is added to the global list
  44. */
  45. memset(&win, 0, sizeof(win));
  46. res->flags = 0;
  47. if (acpi_dev_filter_resource_type(acpi_res, IORESOURCE_MEM))
  48. return AE_OK;
  49. if (!acpi_dev_resource_memory(acpi_res, res)) {
  50. if (acpi_dev_resource_address_space(acpi_res, &win) ||
  51. acpi_dev_resource_ext_address_space(acpi_res, &win))
  52. *res = win.res;
  53. }
  54. if ((res->flags & IORESOURCE_PREFETCH) ||
  55. (res->flags & IORESOURCE_DISABLED))
  56. res->flags = 0;
  57. return AE_CTRL_TERMINATE;
  58. }
  59. static bool acpi_is_ioapic(acpi_handle handle, char **type)
  60. {
  61. acpi_status status;
  62. struct acpi_device_info *info;
  63. char *hid = NULL;
  64. bool match = false;
  65. if (!acpi_has_method(handle, "_GSB"))
  66. return false;
  67. status = acpi_get_object_info(handle, &info);
  68. if (ACPI_SUCCESS(status)) {
  69. if (info->valid & ACPI_VALID_HID)
  70. hid = info->hardware_id.string;
  71. if (hid) {
  72. if (strcmp(hid, "ACPI0009") == 0) {
  73. *type = "IOxAPIC";
  74. match = true;
  75. } else if (strcmp(hid, "ACPI000A") == 0) {
  76. *type = "IOAPIC";
  77. match = true;
  78. }
  79. }
  80. kfree(info);
  81. }
  82. return match;
  83. }
  84. static acpi_status handle_ioapic_add(acpi_handle handle, u32 lvl,
  85. void *context, void **rv)
  86. {
  87. acpi_status status;
  88. unsigned long long gsi_base;
  89. struct acpi_pci_ioapic *ioapic;
  90. struct pci_dev *dev = NULL;
  91. struct resource *res = NULL, *pci_res = NULL, *crs_res;
  92. char *type = NULL;
  93. if (!acpi_is_ioapic(handle, &type))
  94. return AE_OK;
  95. mutex_lock(&ioapic_list_lock);
  96. list_for_each_entry(ioapic, &ioapic_list, list)
  97. if (ioapic->handle == handle) {
  98. mutex_unlock(&ioapic_list_lock);
  99. return AE_OK;
  100. }
  101. status = acpi_evaluate_integer(handle, "_GSB", NULL, &gsi_base);
  102. if (ACPI_FAILURE(status)) {
  103. acpi_handle_warn(handle, "failed to evaluate _GSB method\n");
  104. goto exit;
  105. }
  106. ioapic = kzalloc(sizeof(*ioapic), GFP_KERNEL);
  107. if (!ioapic) {
  108. pr_err("cannot allocate memory for new IOAPIC\n");
  109. goto exit;
  110. } else {
  111. ioapic->root_handle = (acpi_handle)context;
  112. ioapic->handle = handle;
  113. ioapic->gsi_base = (u32)gsi_base;
  114. INIT_LIST_HEAD(&ioapic->list);
  115. }
  116. if (acpi_ioapic_registered(handle, (u32)gsi_base))
  117. goto done;
  118. dev = acpi_get_pci_dev(handle);
  119. if (dev && pci_resource_len(dev, 0)) {
  120. if (pci_enable_device(dev) < 0)
  121. goto exit_put;
  122. pci_set_master(dev);
  123. if (pci_request_region(dev, 0, type))
  124. goto exit_disable;
  125. pci_res = &dev->resource[0];
  126. ioapic->pdev = dev;
  127. } else {
  128. pci_dev_put(dev);
  129. dev = NULL;
  130. }
  131. crs_res = &ioapic->res;
  132. acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, crs_res);
  133. crs_res->name = type;
  134. crs_res->flags |= IORESOURCE_BUSY;
  135. if (crs_res->flags == 0) {
  136. acpi_handle_warn(handle, "failed to get resource\n");
  137. goto exit_release;
  138. } else if (insert_resource(&iomem_resource, crs_res)) {
  139. acpi_handle_warn(handle, "failed to insert resource\n");
  140. goto exit_release;
  141. }
  142. /* try pci resource first, then "_CRS" resource */
  143. res = pci_res;
  144. if (!res || !res->flags)
  145. res = crs_res;
  146. if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
  147. acpi_handle_warn(handle, "failed to register IOAPIC\n");
  148. goto exit_release;
  149. }
  150. done:
  151. list_add(&ioapic->list, &ioapic_list);
  152. mutex_unlock(&ioapic_list_lock);
  153. if (dev)
  154. dev_info(&dev->dev, "%s at %pR, GSI %u\n",
  155. type, res, (u32)gsi_base);
  156. else
  157. acpi_handle_info(handle, "%s at %pR, GSI %u\n",
  158. type, res, (u32)gsi_base);
  159. return AE_OK;
  160. exit_release:
  161. if (dev)
  162. pci_release_region(dev, 0);
  163. if (ioapic->res.flags && ioapic->res.parent)
  164. release_resource(&ioapic->res);
  165. exit_disable:
  166. if (dev)
  167. pci_disable_device(dev);
  168. exit_put:
  169. pci_dev_put(dev);
  170. kfree(ioapic);
  171. exit:
  172. mutex_unlock(&ioapic_list_lock);
  173. *(acpi_status *)rv = AE_ERROR;
  174. return AE_OK;
  175. }
  176. int acpi_ioapic_add(acpi_handle root_handle)
  177. {
  178. acpi_status status, retval = AE_OK;
  179. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root_handle,
  180. UINT_MAX, handle_ioapic_add, NULL,
  181. root_handle, (void **)&retval);
  182. return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
  183. }
  184. int acpi_ioapic_remove(struct acpi_pci_root *root)
  185. {
  186. int retval = 0;
  187. struct acpi_pci_ioapic *ioapic, *tmp;
  188. mutex_lock(&ioapic_list_lock);
  189. list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
  190. if (root->device->handle != ioapic->root_handle)
  191. continue;
  192. if (acpi_unregister_ioapic(ioapic->handle, ioapic->gsi_base))
  193. retval = -EBUSY;
  194. if (ioapic->pdev) {
  195. pci_release_region(ioapic->pdev, 0);
  196. pci_disable_device(ioapic->pdev);
  197. pci_dev_put(ioapic->pdev);
  198. }
  199. if (ioapic->res.flags && ioapic->res.parent)
  200. release_resource(&ioapic->res);
  201. list_del(&ioapic->list);
  202. kfree(ioapic);
  203. }
  204. mutex_unlock(&ioapic_list_lock);
  205. return retval;
  206. }