pci.c 5.8 KB

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