pci.c 6.0 KB

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