2 * Copyright (c) 2009, Intel Corporation.
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.
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
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.
17 * Author: Weidong Han <weidong.han@intel.com>
20 #include <linux/pci.h>
21 #include <linux/acpi.h>
23 #include <xen/interface/physdev.h>
24 #include <xen/interface/xen.h>
26 #ifdef CONFIG_PARAVIRT_XEN
27 #define CONFIG_XEN_COMPAT 0x040000
28 #include <asm/xen/hypervisor.h>
29 #include <asm/xen/hypercall.h>
31 #include <asm/hypervisor.h>
33 #include "../pci/pci.h"
35 #if CONFIG_XEN_COMPAT < 0x040200
36 static bool __read_mostly pci_seg_supported = true;
38 #define pci_seg_supported true
41 static int xen_add_device(struct device *dev)
44 struct pci_dev *pci_dev = to_pci_dev(dev);
46 struct pci_dev *physfn = pci_dev->physfn;
49 if (pci_seg_supported) {
50 struct physdev_pci_device_add add = {
51 .seg = pci_domain_nr(pci_dev->bus),
52 .bus = pci_dev->bus->number,
53 .devfn = pci_dev->devfn
60 if (pci_dev->is_virtfn) {
61 add.flags = XEN_PCI_DEV_VIRTFN;
62 add.physfn.bus = physfn->bus->number;
63 add.physfn.devfn = physfn->devfn;
66 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
67 add.flags = XEN_PCI_DEV_EXTFN;
70 handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
72 handle = DEVICE_ACPI_HANDLE(pci_dev->bus->bridge);
74 if (!handle && pci_dev->is_virtfn)
75 handle = DEVICE_ACPI_HANDLE(physfn->bus->bridge);
81 unsigned long long pxm;
83 status = acpi_evaluate_integer(handle, "_PXM",
85 if (ACPI_SUCCESS(status)) {
87 add.flags |= XEN_PCI_DEV_PXM;
90 status = acpi_get_parent(handle, &handle);
91 } while (ACPI_SUCCESS(status));
93 #endif /* CONFIG_ACPI */
95 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
98 #if CONFIG_XEN_COMPAT < 0x040200
99 pci_seg_supported = false;
103 if (pci_domain_nr(pci_dev->bus))
105 #ifdef CONFIG_PCI_IOV
106 else if (pci_dev->is_virtfn) {
107 struct physdev_manage_pci_ext manage_pci_ext = {
108 .bus = pci_dev->bus->number,
109 .devfn = pci_dev->devfn,
111 .physfn.bus = physfn->bus->number,
112 .physfn.devfn = physfn->devfn,
115 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
119 else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
120 struct physdev_manage_pci_ext manage_pci_ext = {
121 .bus = pci_dev->bus->number,
122 .devfn = pci_dev->devfn,
126 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
129 struct physdev_manage_pci manage_pci = {
130 .bus = pci_dev->bus->number,
131 .devfn = pci_dev->devfn,
134 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
141 static int xen_remove_device(struct device *dev)
144 struct pci_dev *pci_dev = to_pci_dev(dev);
146 if (pci_seg_supported) {
147 struct physdev_pci_device device = {
148 .seg = pci_domain_nr(pci_dev->bus),
149 .bus = pci_dev->bus->number,
150 .devfn = pci_dev->devfn
153 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
155 } else if (pci_domain_nr(pci_dev->bus))
158 struct physdev_manage_pci manage_pci = {
159 .bus = pci_dev->bus->number,
160 .devfn = pci_dev->devfn
163 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
170 static int xen_pci_notifier(struct notifier_block *nb,
171 unsigned long action, void *data)
173 struct device *dev = data;
177 case BUS_NOTIFY_ADD_DEVICE:
178 r = xen_add_device(dev);
180 case BUS_NOTIFY_DEL_DEVICE:
181 r = xen_remove_device(dev);
187 dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
188 action == BUS_NOTIFY_ADD_DEVICE ? "add" :
189 (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
193 static struct notifier_block device_nb = {
194 .notifier_call = xen_pci_notifier,
197 static int __init register_xen_pci_notifier(void)
199 if (!xen_initial_domain())
202 return bus_register_notifier(&pci_bus_type, &device_nb);
205 arch_initcall(register_xen_pci_notifier);