ioapic.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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) == 0)
  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;
  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. res = &dev->resource[0];
  121. ioapic->pdev = dev;
  122. } else {
  123. pci_dev_put(dev);
  124. dev = NULL;
  125. res = &ioapic->res;
  126. acpi_walk_resources(handle, METHOD_NAME__CRS, setup_res, res);
  127. if (res->flags == 0) {
  128. acpi_handle_warn(handle, "failed to get resource\n");
  129. goto exit_free;
  130. } else if (request_resource(&iomem_resource, res)) {
  131. acpi_handle_warn(handle, "failed to insert resource\n");
  132. goto exit_free;
  133. }
  134. }
  135. if (acpi_register_ioapic(handle, res->start, (u32)gsi_base)) {
  136. acpi_handle_warn(handle, "failed to register IOAPIC\n");
  137. goto exit_release;
  138. }
  139. done:
  140. list_add(&ioapic->list, &ioapic_list);
  141. mutex_unlock(&ioapic_list_lock);
  142. if (dev)
  143. dev_info(&dev->dev, "%s at %pR, GSI %u\n",
  144. type, res, (u32)gsi_base);
  145. else
  146. acpi_handle_info(handle, "%s at %pR, GSI %u\n",
  147. type, res, (u32)gsi_base);
  148. return AE_OK;
  149. exit_release:
  150. if (dev)
  151. pci_release_region(dev, 0);
  152. else
  153. release_resource(res);
  154. exit_disable:
  155. if (dev)
  156. pci_disable_device(dev);
  157. exit_put:
  158. pci_dev_put(dev);
  159. exit_free:
  160. kfree(ioapic);
  161. exit:
  162. mutex_unlock(&ioapic_list_lock);
  163. *(acpi_status *)rv = AE_ERROR;
  164. return AE_OK;
  165. }
  166. int acpi_ioapic_add(struct acpi_pci_root *root)
  167. {
  168. acpi_status status, retval = AE_OK;
  169. status = acpi_walk_namespace(ACPI_TYPE_DEVICE, root->device->handle,
  170. UINT_MAX, handle_ioapic_add, NULL,
  171. root->device->handle, (void **)&retval);
  172. return ACPI_SUCCESS(status) && ACPI_SUCCESS(retval) ? 0 : -ENODEV;
  173. }
  174. int acpi_ioapic_remove(struct acpi_pci_root *root)
  175. {
  176. int retval = 0;
  177. struct acpi_pci_ioapic *ioapic, *tmp;
  178. mutex_lock(&ioapic_list_lock);
  179. list_for_each_entry_safe(ioapic, tmp, &ioapic_list, list) {
  180. if (root->device->handle != ioapic->root_handle)
  181. continue;
  182. if (acpi_unregister_ioapic(ioapic->handle, ioapic->gsi_base))
  183. retval = -EBUSY;
  184. if (ioapic->pdev) {
  185. pci_release_region(ioapic->pdev, 0);
  186. pci_disable_device(ioapic->pdev);
  187. pci_dev_put(ioapic->pdev);
  188. } else if (ioapic->res.flags && ioapic->res.parent) {
  189. release_resource(&ioapic->res);
  190. }
  191. list_del(&ioapic->list);
  192. kfree(ioapic);
  193. }
  194. mutex_unlock(&ioapic_list_lock);
  195. return retval;
  196. }