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>
22 #include <linux/pci-acpi.h>
24 #include <xen/interface/physdev.h>
25 #include <xen/interface/xen.h>
27 #include <asm/xen/hypervisor.h>
28 #include <asm/xen/hypercall.h>
29 #include "../pci/pci.h"
30 #ifdef CONFIG_PCI_MMCONFIG
31 #include <asm/pci_x86.h>
34 static bool __read_mostly pci_seg_supported = true;
36 static int xen_add_device(struct device *dev)
39 struct pci_dev *pci_dev = to_pci_dev(dev);
41 struct pci_dev *physfn = pci_dev->physfn;
44 if (pci_seg_supported) {
46 struct physdev_pci_device_add add;
49 .add.seg = pci_domain_nr(pci_dev->bus),
50 .add.bus = pci_dev->bus->number,
51 .add.devfn = pci_dev->devfn
53 struct physdev_pci_device_add *add = &add_ext.add;
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 = ACPI_HANDLE(&pci_dev->dev);
72 if (!handle && pci_dev->is_virtfn)
73 handle = ACPI_HANDLE(physfn->bus->bridge);
77 * This device was not listed in the ACPI name space at
78 * all. Try to get acpi handle of parent pci bus.
81 for (pbus = pci_dev->bus; pbus; pbus = pbus->parent) {
82 handle = acpi_pci_get_bridge_handle(pbus);
91 unsigned long long pxm;
93 status = acpi_evaluate_integer(handle, "_PXM",
95 if (ACPI_SUCCESS(status)) {
97 add->flags |= XEN_PCI_DEV_PXM;
100 status = acpi_get_parent(handle, &handle);
101 } while (ACPI_SUCCESS(status));
103 #endif /* CONFIG_ACPI */
105 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add);
108 pci_seg_supported = false;
111 if (pci_domain_nr(pci_dev->bus))
113 #ifdef CONFIG_PCI_IOV
114 else if (pci_dev->is_virtfn) {
115 struct physdev_manage_pci_ext manage_pci_ext = {
116 .bus = pci_dev->bus->number,
117 .devfn = pci_dev->devfn,
119 .physfn.bus = physfn->bus->number,
120 .physfn.devfn = physfn->devfn,
123 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
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,
134 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
137 struct physdev_manage_pci manage_pci = {
138 .bus = pci_dev->bus->number,
139 .devfn = pci_dev->devfn,
142 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
149 static int xen_remove_device(struct device *dev)
152 struct pci_dev *pci_dev = to_pci_dev(dev);
154 if (pci_seg_supported) {
155 struct physdev_pci_device device = {
156 .seg = pci_domain_nr(pci_dev->bus),
157 .bus = pci_dev->bus->number,
158 .devfn = pci_dev->devfn
161 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
163 } else if (pci_domain_nr(pci_dev->bus))
166 struct physdev_manage_pci manage_pci = {
167 .bus = pci_dev->bus->number,
168 .devfn = pci_dev->devfn
171 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
178 static int xen_pci_notifier(struct notifier_block *nb,
179 unsigned long action, void *data)
181 struct device *dev = data;
185 case BUS_NOTIFY_ADD_DEVICE:
186 r = xen_add_device(dev);
188 case BUS_NOTIFY_DEL_DEVICE:
189 r = xen_remove_device(dev);
195 dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
196 action == BUS_NOTIFY_ADD_DEVICE ? "add" :
197 (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
201 static struct notifier_block device_nb = {
202 .notifier_call = xen_pci_notifier,
205 static int __init register_xen_pci_notifier(void)
207 if (!xen_initial_domain())
210 return bus_register_notifier(&pci_bus_type, &device_nb);
213 arch_initcall(register_xen_pci_notifier);
215 #ifdef CONFIG_PCI_MMCONFIG
216 static int __init xen_mcfg_late(void)
218 struct pci_mmcfg_region *cfg;
221 if (!xen_initial_domain())
224 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
227 if (list_empty(&pci_mmcfg_list))
230 /* Check whether they are in the right area. */
231 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
232 struct physdev_pci_mmcfg_reserved r;
234 r.address = cfg->address;
235 r.segment = cfg->segment;
236 r.start_bus = cfg->start_bus;
237 r.end_bus = cfg->end_bus;
238 r.flags = XEN_PCI_MMCFG_RESERVED;
240 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
247 pr_warn("Failed to report MMCONFIG reservation"
248 " state for %s to hypervisor"
256 * Needs to be done after acpi_init which are subsys_initcall.
258 subsys_initcall_sync(xen_mcfg_late);