pci.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. static bool __read_mostly pci_seg_supported = true;
  28. static int xen_add_device(struct device *dev)
  29. {
  30. int r;
  31. struct pci_dev *pci_dev = to_pci_dev(dev);
  32. #ifdef CONFIG_PCI_IOV
  33. struct pci_dev *physfn = pci_dev->physfn;
  34. #endif
  35. if (pci_seg_supported) {
  36. struct physdev_pci_device_add add = {
  37. .seg = pci_domain_nr(pci_dev->bus),
  38. .bus = pci_dev->bus->number,
  39. .devfn = pci_dev->devfn
  40. };
  41. #ifdef CONFIG_ACPI
  42. acpi_handle handle;
  43. #endif
  44. #ifdef CONFIG_PCI_IOV
  45. if (pci_dev->is_virtfn) {
  46. add.flags = XEN_PCI_DEV_VIRTFN;
  47. add.physfn.bus = physfn->bus->number;
  48. add.physfn.devfn = physfn->devfn;
  49. } else
  50. #endif
  51. if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
  52. add.flags = XEN_PCI_DEV_EXTFN;
  53. #ifdef CONFIG_ACPI
  54. handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
  55. if (!handle)
  56. handle = DEVICE_ACPI_HANDLE(pci_dev->bus->bridge);
  57. #ifdef CONFIG_PCI_IOV
  58. if (!handle && pci_dev->is_virtfn)
  59. handle = DEVICE_ACPI_HANDLE(physfn->bus->bridge);
  60. #endif
  61. if (handle) {
  62. acpi_status status;
  63. do {
  64. unsigned long long pxm;
  65. status = acpi_evaluate_integer(handle, "_PXM",
  66. NULL, &pxm);
  67. if (ACPI_SUCCESS(status)) {
  68. add.optarr[0] = pxm;
  69. add.flags |= XEN_PCI_DEV_PXM;
  70. break;
  71. }
  72. status = acpi_get_parent(handle, &handle);
  73. } while (ACPI_SUCCESS(status));
  74. }
  75. #endif /* CONFIG_ACPI */
  76. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
  77. if (r != -ENOSYS)
  78. return r;
  79. pci_seg_supported = false;
  80. }
  81. if (pci_domain_nr(pci_dev->bus))
  82. r = -ENOSYS;
  83. #ifdef CONFIG_PCI_IOV
  84. else if (pci_dev->is_virtfn) {
  85. struct physdev_manage_pci_ext manage_pci_ext = {
  86. .bus = pci_dev->bus->number,
  87. .devfn = pci_dev->devfn,
  88. .is_virtfn = 1,
  89. .physfn.bus = physfn->bus->number,
  90. .physfn.devfn = physfn->devfn,
  91. };
  92. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  93. &manage_pci_ext);
  94. }
  95. #endif
  96. else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
  97. struct physdev_manage_pci_ext manage_pci_ext = {
  98. .bus = pci_dev->bus->number,
  99. .devfn = pci_dev->devfn,
  100. .is_extfn = 1,
  101. };
  102. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
  103. &manage_pci_ext);
  104. } else {
  105. struct physdev_manage_pci manage_pci = {
  106. .bus = pci_dev->bus->number,
  107. .devfn = pci_dev->devfn,
  108. };
  109. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
  110. &manage_pci);
  111. }
  112. return r;
  113. }
  114. static int xen_remove_device(struct device *dev)
  115. {
  116. int r;
  117. struct pci_dev *pci_dev = to_pci_dev(dev);
  118. if (pci_seg_supported) {
  119. struct physdev_pci_device device = {
  120. .seg = pci_domain_nr(pci_dev->bus),
  121. .bus = pci_dev->bus->number,
  122. .devfn = pci_dev->devfn
  123. };
  124. r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
  125. &device);
  126. } else if (pci_domain_nr(pci_dev->bus))
  127. r = -ENOSYS;
  128. else {
  129. struct physdev_manage_pci manage_pci = {
  130. .bus = pci_dev->bus->number,
  131. .devfn = pci_dev->devfn
  132. };
  133. r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
  134. &manage_pci);
  135. }
  136. return r;
  137. }
  138. static int xen_pci_notifier(struct notifier_block *nb,
  139. unsigned long action, void *data)
  140. {
  141. struct device *dev = data;
  142. int r = 0;
  143. switch (action) {
  144. case BUS_NOTIFY_ADD_DEVICE:
  145. r = xen_add_device(dev);
  146. break;
  147. case BUS_NOTIFY_DEL_DEVICE:
  148. r = xen_remove_device(dev);
  149. break;
  150. default:
  151. return NOTIFY_DONE;
  152. }
  153. if (r)
  154. dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
  155. action == BUS_NOTIFY_ADD_DEVICE ? "add" :
  156. (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
  157. return NOTIFY_OK;
  158. }
  159. static struct notifier_block device_nb = {
  160. .notifier_call = xen_pci_notifier,
  161. };
  162. static int __init register_xen_pci_notifier(void)
  163. {
  164. if (!xen_initial_domain())
  165. return 0;
  166. return bus_register_notifier(&pci_bus_type, &device_nb);
  167. }
  168. arch_initcall(register_xen_pci_notifier);