ioapic.c 5.9 KB

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