ioapic.c 5.9 KB

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