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. /*
  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) == 0)
  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;
  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. res = &dev->resource[0];
  126. ioapic->pdev = dev;
  127. } else {
  128. pci_dev_put(dev);
  129. dev = NULL;
  130. res = &ioapic->res;
  131. acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, res);
  132. if (res->flags == 0) {
  133. acpi_handle_warn(handle, "failed to get resource\n");
  134. goto exit_free;
  135. } else if (request_resource(&iomem_resource, res)) {
  136. acpi_handle_warn(handle, "failed to insert resource\n");
  137. goto exit_free;
  138. }
  139. }
  140. if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
  141. acpi_handle_warn(handle, "failed to register IOAPIC\n");
  142. goto exit_release;
  143. }
  144. done:
  145. list_add(&ioapic->list, &ioapic_list);
  146. mutex_unlock(&ioapic_list_lock);
  147. if (dev)
  148. dev_info(&dev->dev, "%s at %pR, GSI %u\n",
  149. type, res, (u32)gsi_base);
  150. else
  151. acpi_handle_info(handle, "%s at %pR, GSI %u\n",
  152. type, res, (u32)gsi_base);
  153. return AE_OK;
  154. exit_release:
  155. if (dev)
  156. pci_release_region(dev, 0);
  157. else
  158. release_resource(res);
  159. exit_disable:
  160. if (dev)
  161. pci_disable_device(dev);
  162. exit_put:
  163. pci_dev_put(dev);
  164. exit_free:
  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(struct acpi_pci_root *root)
  172. {
  173. acpi_status status, retval = AE_OK;
  174. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root->device->handle,
  175. UINT_MAX, handle_ioapic_add, NULL,
  176. root->device->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. } else if (ioapic->res.flags && ioapic->res.parent) {
  194. release_resource(&ioapic->res);
  195. }
  196. list_del(&ioapic->list);
  197. kfree(ioapic);
  198. }
  199. mutex_unlock(&ioapic_list_lock);
  200. return retval;
  201. }