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 #include <asm/xen/hypervisor.h>
27 #include <asm/xen/hypercall.h>
28 #include "../pci/pci.h"
29 #include <asm/pci_x86.h>
31 static bool __read_mostly pci_seg_supported = true;
33 static int xen_add_device(struct device *dev)
36 struct pci_dev *pci_dev = to_pci_dev(dev);
38 struct pci_dev *physfn = pci_dev->physfn;
41 if (pci_seg_supported) {
42 struct physdev_pci_device_add add = {
43 .seg = pci_domain_nr(pci_dev->bus),
44 .bus = pci_dev->bus->number,
45 .devfn = pci_dev->devfn
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;
58 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
59 add.flags = XEN_PCI_DEV_EXTFN;
62 handle = DEVICE_ACPI_HANDLE(&pci_dev->dev);
63 if (!handle && pci_dev->bus->bridge)
64 handle = DEVICE_ACPI_HANDLE(pci_dev->bus->bridge);
66 if (!handle && pci_dev->is_virtfn)
67 handle = DEVICE_ACPI_HANDLE(physfn->bus->bridge);
73 unsigned long long pxm;
75 status = acpi_evaluate_integer(handle, "_PXM",
77 if (ACPI_SUCCESS(status)) {
79 add.flags |= XEN_PCI_DEV_PXM;
82 status = acpi_get_parent(handle, &handle);
83 } while (ACPI_SUCCESS(status));
85 #endif /* CONFIG_ACPI */
87 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, &add);
90 pci_seg_supported = false;
93 if (pci_domain_nr(pci_dev->bus))
96 else if (pci_dev->is_virtfn) {
97 struct physdev_manage_pci_ext manage_pci_ext = {
98 .bus = pci_dev->bus->number,
99 .devfn = pci_dev->devfn,
101 .physfn.bus = physfn->bus->number,
102 .physfn.devfn = physfn->devfn,
105 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
109 else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
110 struct physdev_manage_pci_ext manage_pci_ext = {
111 .bus = pci_dev->bus->number,
112 .devfn = pci_dev->devfn,
116 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
119 struct physdev_manage_pci manage_pci = {
120 .bus = pci_dev->bus->number,
121 .devfn = pci_dev->devfn,
124 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
131 static int xen_remove_device(struct device *dev)
134 struct pci_dev *pci_dev = to_pci_dev(dev);
136 if (pci_seg_supported) {
137 struct physdev_pci_device device = {
138 .seg = pci_domain_nr(pci_dev->bus),
139 .bus = pci_dev->bus->number,
140 .devfn = pci_dev->devfn
143 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
145 } else if (pci_domain_nr(pci_dev->bus))
148 struct physdev_manage_pci manage_pci = {
149 .bus = pci_dev->bus->number,
150 .devfn = pci_dev->devfn
153 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
160 static int xen_pci_notifier(struct notifier_block *nb,
161 unsigned long action, void *data)
163 struct device *dev = data;
167 case BUS_NOTIFY_ADD_DEVICE:
168 r = xen_add_device(dev);
170 case BUS_NOTIFY_DEL_DEVICE:
171 r = xen_remove_device(dev);
177 dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
178 action == BUS_NOTIFY_ADD_DEVICE ? "add" :
179 (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
183 static struct notifier_block device_nb = {
184 .notifier_call = xen_pci_notifier,
187 static int __init register_xen_pci_notifier(void)
189 if (!xen_initial_domain())
192 return bus_register_notifier(&pci_bus_type, &device_nb);
195 arch_initcall(register_xen_pci_notifier);
197 #ifdef CONFIG_PCI_MMCONFIG
198 static int __init xen_mcfg_late(void)
200 struct pci_mmcfg_region *cfg;
203 if (!xen_initial_domain())
206 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
209 if (list_empty(&pci_mmcfg_list))
212 /* Check whether they are in the right area. */
213 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
214 struct physdev_pci_mmcfg_reserved r;
216 r.address = cfg->address;
217 r.segment = cfg->segment;
218 r.start_bus = cfg->start_bus;
219 r.end_bus = cfg->end_bus;
220 r.flags = XEN_PCI_MMCFG_RESERVED;
222 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
229 pr_warn("Failed to report MMCONFIG reservation"
230 " state for %s to hypervisor"
238 * Needs to be done after acpi_init which are subsys_initcall.
240 subsys_initcall_sync(xen_mcfg_late);