pci.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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. #include <asm/pci_x86.h>
  28. static bool __read_mostly pci_seg_supported = true;
  29. static int xen_add_device(struct device *dev)
  30. {
  31. int r;
  32. struct pci_dev *pci_dev = to_pci_dev(dev);
  33. #ifdef CONFIG_PCI_IOV
  34. struct pci_dev *physfn = pci_dev->physfn;
  35. #endif
  36. if (pci_seg_supported) {
  37. struct physdev_pci_device_add add = {
  38. .seg = pci_domain_nr(pci_dev->bus),
  39. .bus = pci_dev->bus->number,
  40. .devfn = pci_dev->devfn
  41. };
  42. #ifdef CONFIG_ACPI
  43. acpi_handle handle;
  44. #endif
  45. #ifdef CONFIG_PCI_IOV
  46. if (pci_dev->is_virtfn) {
  47. add.flags = XEN_PCI_DEV_VIRTFN;
  48. add.physfn.bus = physfn->bus->number;
  49. add.physfn.devfn = physfn->devfn;
  50. } else
  51. #endif
  52. if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
  53. add.flags = XEN_PCI_DEV_EXTFN;
  54. #ifdef CONFIG_ACPI
  55. handle = ACPI_HANDLE(&pci_dev->dev);
  56. if (!handle && pci_dev->bus->bridge)
  57. handle = ACPI_HANDLE(pci_dev->bus->bridge);
  58. #ifdef CONFIG_PCI_IOV
  59. if (!handle && pci_dev->is_virtfn)
  60. handle = ACPI_HANDLE(physfn->bus->bridge);
  61. #endif
  62. if (handle) {
  63. acpi_status status;
  64. do {
  65. unsigned long long pxm;
  66. status = acpi_evaluate_integer(handle, "_PXM",
  67. NULL, &pxm);
  68. if (ACPI_SUCCESS(status)) {
  69. add.optarr[0] = pxm;
  70. add.flags |= XEN_PCI_DEV_PXM;
  71. break;
  72. }
  73. status = acpi_get_parent(handle, &handle);
  74. } while (ACPI_SUCCESS(status));
  75. }
  76. #endif /* CONFIG_ACPI */
  77. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
  78. if (r != -ENOSYS)
  79. return r;
  80. pci_seg_supported = false;
  81. }
  82. if (pci_domain_nr(pci_dev->bus))
  83. r = -ENOSYS;
  84. #ifdef CONFIG_PCI_IOV
  85. else if (pci_dev->is_virtfn) {
  86. struct physdev_manage_pci_ext manage_pci_ext = {
  87. .bus = pci_dev->bus->number,
  88. .devfn = pci_dev->devfn,
  89. .is_virtfn = 1,
  90. .physfn.bus = physfn->bus->number,
  91. .physfn.devfn = physfn->devfn,
  92. };
  93. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  94. &manage_pci_ext);
  95. }
  96. #endif
  97. else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
  98. struct physdev_manage_pci_ext manage_pci_ext = {
  99. .bus = pci_dev->bus->number,
  100. .devfn = pci_dev->devfn,
  101. .is_extfn = 1,
  102. };
  103. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  104. &manage_pci_ext);
  105. } else {
  106. struct physdev_manage_pci manage_pci = {
  107. .bus = pci_dev->bus->number,
  108. .devfn = pci_dev->devfn,
  109. };
  110. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
  111. &manage_pci);
  112. }
  113. return r;
  114. }
  115. static int xen_remove_device(struct device *dev)
  116. {
  117. int r;
  118. struct pci_dev *pci_dev = to_pci_dev(dev);
  119. if (pci_seg_supported) {
  120. struct physdev_pci_device device = {
  121. .seg = pci_domain_nr(pci_dev->bus),
  122. .bus = pci_dev->bus->number,
  123. .devfn = pci_dev->devfn
  124. };
  125. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
  126. &device);
  127. } else if (pci_domain_nr(pci_dev->bus))
  128. r = -ENOSYS;
  129. else {
  130. struct physdev_manage_pci manage_pci = {
  131. .bus = pci_dev->bus->number,
  132. .devfn = pci_dev->devfn
  133. };
  134. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
  135. &manage_pci);
  136. }
  137. return r;
  138. }
  139. static int xen_pci_notifier(struct notifier_block *nb,
  140. unsigned long action, void *data)
  141. {
  142. struct device *dev = data;
  143. int r = 0;
  144. switch (action) {
  145. case BUS_NOTIFY_ADD_DEVICE:
  146. r = xen_add_device(dev);
  147. break;
  148. case BUS_NOTIFY_DEL_DEVICE:
  149. r = xen_remove_device(dev);
  150. break;
  151. default:
  152. return NOTIFY_DONE;
  153. }
  154. if (r)
  155. dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
  156. action == BUS_NOTIFY_ADD_DEVICE ? "add" :
  157. (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
  158. return NOTIFY_OK;
  159. }
  160. static struct notifier_block device_nb = {
  161. .notifier_call = xen_pci_notifier,
  162. };
  163. static int __init register_xen_pci_notifier(void)
  164. {
  165. if (!xen_initial_domain())
  166. return 0;
  167. return bus_register_notifier(&pci_bus_type, &device_nb);
  168. }
  169. arch_initcall(register_xen_pci_notifier);
  170. #ifdef CONFIG_PCI_MMCONFIG
  171. static int __init xen_mcfg_late(void)
  172. {
  173. struct pci_mmcfg_region *cfg;
  174. int rc;
  175. if (!xen_initial_domain())
  176. return 0;
  177. if ((pci_probe & PCI_PROBE_MMCONF) == 0)
  178. return 0;
  179. if (list_empty(&pci_mmcfg_list))
  180. return 0;
  181. /* Check whether they are in the right area. */
  182. list_for_each_entry(cfg, &pci_mmcfg_list, list) {
  183. struct physdev_pci_mmcfg_reserved r;
  184. r.address = cfg->address;
  185. r.segment = cfg->segment;
  186. r.start_bus = cfg->start_bus;
  187. r.end_bus = cfg->end_bus;
  188. r.flags = XEN_PCI_MMCFG_RESERVED;
  189. rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
  190. switch (rc) {
  191. case 0:
  192. case -ENOSYS:
  193. continue;
  194. default:
  195. pr_warn("Failed to report MMCONFIG reservation"
  196. " state for %s to hypervisor"
  197. " (%d)\n",
  198. cfg->name, rc);
  199. }
  200. }
  201. return 0;
  202. }
  203. /*
  204. * Needs to be done after acpi_init which are subsys_initcall.
  205. */
  206. subsys_initcall_sync(xen_mcfg_late);
  207. #endif