pci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (c) 2009, Intel Corporation.
  4. *
  5. * Author: Weidong Han <weidong.han@intel.com>
  6. */
  7. #include <linux/pci.h>
  8. #include <linux/acpi.h>
  9. #include <linux/pci-acpi.h>
  10. #include <xen/xen.h>
  11. #include <xen/interface/physdev.h>
  12. #include <xen/interface/xen.h>
  13. #include <asm/xen/hypervisor.h>
  14. #include <asm/xen/hypercall.h>
  15. #include "../pci/pci.h"
  16. #ifdef CONFIG_PCI_MMCONFIG
  17. #include <asm/pci_x86.h>
  18. static int xen_mcfg_late(void);
  19. #endif
  20. static bool __read_mostly pci_seg_supported = true;
  21. static int xen_add_device(struct device *dev)
  22. {
  23. int r;
  24. struct pci_dev *pci_dev = to_pci_dev(dev);
  25. #ifdef CONFIG_PCI_IOV
  26. struct pci_dev *physfn = pci_dev->physfn;
  27. #endif
  28. #ifdef CONFIG_PCI_MMCONFIG
  29. static bool pci_mcfg_reserved = false;
  30. /*
  31. * Reserve MCFG areas in Xen on first invocation due to this being
  32. * potentially called from inside of acpi_init immediately after
  33. * MCFG table has been finally parsed.
  34. */
  35. if (!pci_mcfg_reserved) {
  36. xen_mcfg_late();
  37. pci_mcfg_reserved = true;
  38. }
  39. #endif
  40. if (pci_seg_supported) {
  41. struct {
  42. struct physdev_pci_device_add add;
  43. uint32_t pxm;
  44. } add_ext = {
  45. .add.seg = pci_domain_nr(pci_dev->bus),
  46. .add.bus = pci_dev->bus->number,
  47. .add.devfn = pci_dev->devfn
  48. };
  49. struct physdev_pci_device_add *add = &add_ext.add;
  50. #ifdef CONFIG_ACPI
  51. acpi_handle handle;
  52. #endif
  53. #ifdef CONFIG_PCI_IOV
  54. if (pci_dev->is_virtfn) {
  55. add->flags = XEN_PCI_DEV_VIRTFN;
  56. add->physfn.bus = physfn->bus->number;
  57. add->physfn.devfn = physfn->devfn;
  58. } else
  59. #endif
  60. if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
  61. add->flags = XEN_PCI_DEV_EXTFN;
  62. #ifdef CONFIG_ACPI
  63. handle = ACPI_HANDLE(&pci_dev->dev);
  64. #ifdef CONFIG_PCI_IOV
  65. if (!handle && pci_dev->is_virtfn)
  66. handle = ACPI_HANDLE(physfn->bus->bridge);
  67. #endif
  68. if (!handle) {
  69. /*
  70. * This device was not listed in the ACPI name space at
  71. * all. Try to get acpi handle of parent pci bus.
  72. */
  73. struct pci_bus *pbus;
  74. for (pbus = pci_dev->bus; pbus; pbus = pbus->parent) {
  75. handle = acpi_pci_get_bridge_handle(pbus);
  76. if (handle)
  77. break;
  78. }
  79. }
  80. if (handle) {
  81. acpi_status status;
  82. do {
  83. unsigned long long pxm;
  84. status = acpi_evaluate_integer(handle, "_PXM",
  85. NULL, &pxm);
  86. if (ACPI_SUCCESS(status)) {
  87. add->optarr[0] = pxm;
  88. add->flags |= XEN_PCI_DEV_PXM;
  89. break;
  90. }
  91. status = acpi_get_parent(handle, &handle);
  92. } while (ACPI_SUCCESS(status));
  93. }
  94. #endif /* CONFIG_ACPI */
  95. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add);
  96. if (r != -ENOSYS)
  97. return r;
  98. pci_seg_supported = false;
  99. }
  100. if (pci_domain_nr(pci_dev->bus))
  101. r = -ENOSYS;
  102. #ifdef CONFIG_PCI_IOV
  103. else if (pci_dev->is_virtfn) {
  104. struct physdev_manage_pci_ext manage_pci_ext = {
  105. .bus = pci_dev->bus->number,
  106. .devfn = pci_dev->devfn,
  107. .is_virtfn = 1,
  108. .physfn.bus = physfn->bus->number,
  109. .physfn.devfn = physfn->devfn,
  110. };
  111. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  112. &manage_pci_ext);
  113. }
  114. #endif
  115. else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
  116. struct physdev_manage_pci_ext manage_pci_ext = {
  117. .bus = pci_dev->bus->number,
  118. .devfn = pci_dev->devfn,
  119. .is_extfn = 1,
  120. };
  121. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  122. &manage_pci_ext);
  123. } else {
  124. struct physdev_manage_pci manage_pci = {
  125. .bus = pci_dev->bus->number,
  126. .devfn = pci_dev->devfn,
  127. };
  128. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
  129. &manage_pci);
  130. }
  131. return r;
  132. }
  133. static int xen_remove_device(struct device *dev)
  134. {
  135. int r;
  136. struct pci_dev *pci_dev = to_pci_dev(dev);
  137. if (pci_seg_supported) {
  138. struct physdev_pci_device device = {
  139. .seg = pci_domain_nr(pci_dev->bus),
  140. .bus = pci_dev->bus->number,
  141. .devfn = pci_dev->devfn
  142. };
  143. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
  144. &device);
  145. } else if (pci_domain_nr(pci_dev->bus))
  146. r = -ENOSYS;
  147. else {
  148. struct physdev_manage_pci manage_pci = {
  149. .bus = pci_dev->bus->number,
  150. .devfn = pci_dev->devfn
  151. };
  152. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
  153. &manage_pci);
  154. }
  155. return r;
  156. }
  157. static int xen_pci_notifier(struct notifier_block *nb,
  158. unsigned long action, void *data)
  159. {
  160. struct device *dev = data;
  161. int r = 0;
  162. switch (action) {
  163. case BUS_NOTIFY_ADD_DEVICE:
  164. r = xen_add_device(dev);
  165. break;
  166. case BUS_NOTIFY_DEL_DEVICE:
  167. r = xen_remove_device(dev);
  168. break;
  169. default:
  170. return NOTIFY_DONE;
  171. }
  172. if (r)
  173. dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
  174. action == BUS_NOTIFY_ADD_DEVICE ? "add" :
  175. (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
  176. return NOTIFY_OK;
  177. }
  178. static struct notifier_block device_nb = {
  179. .notifier_call = xen_pci_notifier,
  180. };
  181. static int __init register_xen_pci_notifier(void)
  182. {
  183. if (!xen_initial_domain())
  184. return 0;
  185. return bus_register_notifier(&pci_bus_type, &device_nb);
  186. }
  187. arch_initcall(register_xen_pci_notifier);
  188. #ifdef CONFIG_PCI_MMCONFIG
  189. static int xen_mcfg_late(void)
  190. {
  191. struct pci_mmcfg_region *cfg;
  192. int rc;
  193. if (!xen_initial_domain())
  194. return 0;
  195. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  196. return 0;
  197. if (list_empty(&pci_mmcfg_list))
  198. return 0;
  199. /* Check whether they are in the right area. */
  200. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  201. struct physdev_pci_mmcfg_reserved r;
  202. r.address = cfg->address;
  203. r.segment = cfg->segment;
  204. r.start_bus = cfg->start_bus;
  205. r.end_bus = cfg->end_bus;
  206. r.flags = XEN_PCI_MMCFG_RESERVED;
  207. rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
  208. switch (rc) {
  209. case 0:
  210. case -ENOSYS:
  211. continue;
  212. default:
  213. pr_warn("Failed to report MMCONFIG reservation"
  214. " state for %s to hypervisor"
  215. " (%d)\n",
  216. cfg->name, rc);
  217. }
  218. }
  219. return 0;
  220. }
  221. #endif