ioapic.c 5.6 KB

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