pci.c 6.2 KB

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