pci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /*
  2. * Copyright (c) 2009, Intel Corporation.
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms and conditions of the GNU General Public License,
  6. * version 2, as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
  15. * Place - Suite 330, Boston, MA 02111-1307 USA.
  16. *
  17. * Author: Weidong Han <weidong.han@intel.com>
  18. */
  19. #include <linux/pci.h>
  20. #include <linux/acpi.h>
  21. #include <xen/xen.h>
  22. #include <xen/interface/physdev.h>
  23. #include <xen/interface/xen.h>
  24. #include <asm/xen/hypervisor.h>
  25. #include <asm/xen/hypercall.h>
  26. #include "../pci/pci.h"
  27. #ifdef CONFIG_PCI_MMCONFIG
  28. #include <asm/pci_x86.h>
  29. #endif
  30. static bool __read_mostly pci_seg_supported = true;
  31. static int xen_add_device(struct device *dev)
  32. {
  33. int r;
  34. struct pci_dev *pci_dev = to_pci_dev(dev);
  35. #ifdef CONFIG_PCI_IOV
  36. struct pci_dev *physfn = pci_dev->physfn;
  37. #endif
  38. if (pci_seg_supported) {
  39. struct physdev_pci_device_add add = {
  40. .seg = pci_domain_nr(pci_dev->bus),
  41. .bus = pci_dev->bus->number,
  42. .devfn = pci_dev->devfn
  43. };
  44. #ifdef CONFIG_ACPI
  45. acpi_handle handle;
  46. #endif
  47. #ifdef CONFIG_PCI_IOV
  48. if (pci_dev->is_virtfn) {
  49. add.flags = XEN_PCI_DEV_VIRTFN;
  50. add.physfn.bus = physfn->bus->number;
  51. add.physfn.devfn = physfn->devfn;
  52. } else
  53. #endif
  54. if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
  55. add.flags = XEN_PCI_DEV_EXTFN;
  56. #ifdef CONFIG_ACPI
  57. handle = ACPI_HANDLE(&pci_dev->dev);
  58. if (!handle && pci_dev->bus->bridge)
  59. handle = ACPI_HANDLE(pci_dev->bus->bridge);
  60. #ifdef CONFIG_PCI_IOV
  61. if (!handle && pci_dev->is_virtfn)
  62. handle = ACPI_HANDLE(physfn->bus->bridge);
  63. #endif
  64. if (handle) {
  65. acpi_status status;
  66. do {
  67. unsigned long long pxm;
  68. status = acpi_evaluate_integer(handle, "_PXM",
  69. NULL, &pxm);
  70. if (ACPI_SUCCESS(status)) {
  71. add.optarr[0] = pxm;
  72. add.flags |= XEN_PCI_DEV_PXM;
  73. break;
  74. }
  75. status = acpi_get_parent(handle, &handle);
  76. } while (ACPI_SUCCESS(status));
  77. }
  78. #endif /* CONFIG_ACPI */
  79. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
  80. if (r != -ENOSYS)
  81. return r;
  82. pci_seg_supported = false;
  83. }
  84. if (pci_domain_nr(pci_dev->bus))
  85. r = -ENOSYS;
  86. #ifdef CONFIG_PCI_IOV
  87. else if (pci_dev->is_virtfn) {
  88. struct physdev_manage_pci_ext manage_pci_ext = {
  89. .bus = pci_dev->bus->number,
  90. .devfn = pci_dev->devfn,
  91. .is_virtfn = 1,
  92. .physfn.bus = physfn->bus->number,
  93. .physfn.devfn = physfn->devfn,
  94. };
  95. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  96. &manage_pci_ext);
  97. }
  98. #endif
  99. else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
  100. struct physdev_manage_pci_ext manage_pci_ext = {
  101. .bus = pci_dev->bus->number,
  102. .devfn = pci_dev->devfn,
  103. .is_extfn = 1,
  104. };
  105. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  106. &manage_pci_ext);
  107. } else {
  108. struct physdev_manage_pci manage_pci = {
  109. .bus = pci_dev->bus->number,
  110. .devfn = pci_dev->devfn,
  111. };
  112. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
  113. &manage_pci);
  114. }
  115. return r;
  116. }
  117. static int xen_remove_device(struct device *dev)
  118. {
  119. int r;
  120. struct pci_dev *pci_dev = to_pci_dev(dev);
  121. if (pci_seg_supported) {
  122. struct physdev_pci_device device = {
  123. .seg = pci_domain_nr(pci_dev->bus),
  124. .bus = pci_dev->bus->number,
  125. .devfn = pci_dev->devfn
  126. };
  127. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
  128. &device);
  129. } else if (pci_domain_nr(pci_dev->bus))
  130. r = -ENOSYS;
  131. else {
  132. struct physdev_manage_pci manage_pci = {
  133. .bus = pci_dev->bus->number,
  134. .devfn = pci_dev->devfn
  135. };
  136. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
  137. &manage_pci);
  138. }
  139. return r;
  140. }
  141. static int xen_pci_notifier(struct notifier_block *nb,
  142. unsigned long action, void *data)
  143. {
  144. struct device *dev = data;
  145. int r = 0;
  146. switch (action) {
  147. case BUS_NOTIFY_ADD_DEVICE:
  148. r = xen_add_device(dev);
  149. break;
  150. case BUS_NOTIFY_DEL_DEVICE:
  151. r = xen_remove_device(dev);
  152. break;
  153. default:
  154. return NOTIFY_DONE;
  155. }
  156. if (r)
  157. dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
  158. action == BUS_NOTIFY_ADD_DEVICE ? "add" :
  159. (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
  160. return NOTIFY_OK;
  161. }
  162. static struct notifier_block device_nb = {
  163. .notifier_call = xen_pci_notifier,
  164. };
  165. static int __init register_xen_pci_notifier(void)
  166. {
  167. if (!xen_initial_domain())
  168. return 0;
  169. return bus_register_notifier(&pci_bus_type, &device_nb);
  170. }
  171. arch_initcall(register_xen_pci_notifier);
  172. #ifdef CONFIG_PCI_MMCONFIG
  173. static int __init xen_mcfg_late(void)
  174. {
  175. struct pci_mmcfg_region *cfg;
  176. int rc;
  177. if (!xen_initial_domain())
  178. return 0;
  179. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  180. return 0;
  181. if (list_empty(&pci_mmcfg_list))
  182. return 0;
  183. /* Check whether they are in the right area. */
  184. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  185. struct physdev_pci_mmcfg_reserved r;
  186. r.address = cfg->address;
  187. r.segment = cfg->segment;
  188. r.start_bus = cfg->start_bus;
  189. r.end_bus = cfg->end_bus;
  190. r.flags = XEN_PCI_MMCFG_RESERVED;
  191. rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
  192. switch (rc) {
  193. case 0:
  194. case -ENOSYS:
  195. continue;
  196. default:
  197. pr_warn("Failed to report MMCONFIG reservation"
  198. " state for %s to hypervisor"
  199. " (%d)\n",
  200. cfg->name, rc);
  201. }
  202. }
  203. return 0;
  204. }
  205. /*
  206. * Needs to be done after acpi_init which are subsys_initcall.
  207. */
  208. subsys_initcall_sync(xen_mcfg_late);
  209. #endif