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 #ifdef CONFIG_PCI_MMCONFIG
30 #include <asm/pci_x86.h>
33 static bool __read_mostly pci_seg_supported
= true;
35 static int xen_add_device(struct device
*dev
)
38 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
40 struct pci_dev
*physfn
= pci_dev
->physfn
;
43 if (pci_seg_supported
) {
45 struct physdev_pci_device_add add
;
48 .add
.seg
= pci_domain_nr(pci_dev
->bus
),
49 .add
.bus
= pci_dev
->bus
->number
,
50 .add
.devfn
= pci_dev
->devfn
52 struct physdev_pci_device_add
*add
= &add_ext
.add
;
59 if (pci_dev
->is_virtfn
) {
60 add
->flags
= XEN_PCI_DEV_VIRTFN
;
61 add
->physfn
.bus
= physfn
->bus
->number
;
62 add
->physfn
.devfn
= physfn
->devfn
;
65 if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
))
66 add
->flags
= XEN_PCI_DEV_EXTFN
;
69 handle
= ACPI_HANDLE(&pci_dev
->dev
);
70 if (!handle
&& pci_dev
->bus
->bridge
)
71 handle
= ACPI_HANDLE(pci_dev
->bus
->bridge
);
73 if (!handle
&& pci_dev
->is_virtfn
)
74 handle
= ACPI_HANDLE(physfn
->bus
->bridge
);
80 unsigned long long pxm
;
82 status
= acpi_evaluate_integer(handle
, "_PXM",
84 if (ACPI_SUCCESS(status
)) {
86 add
->flags
|= XEN_PCI_DEV_PXM
;
89 status
= acpi_get_parent(handle
, &handle
);
90 } while (ACPI_SUCCESS(status
));
92 #endif /* CONFIG_ACPI */
94 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add
, add
);
97 pci_seg_supported
= false;
100 if (pci_domain_nr(pci_dev
->bus
))
102 #ifdef CONFIG_PCI_IOV
103 else if (pci_dev
->is_virtfn
) {
104 struct physdev_manage_pci_ext manage_pci_ext
= {
105 .bus
= pci_dev
->bus
->number
,
106 .devfn
= pci_dev
->devfn
,
108 .physfn
.bus
= physfn
->bus
->number
,
109 .physfn
.devfn
= physfn
->devfn
,
112 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
116 else if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
)) {
117 struct physdev_manage_pci_ext manage_pci_ext
= {
118 .bus
= pci_dev
->bus
->number
,
119 .devfn
= pci_dev
->devfn
,
123 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
126 struct physdev_manage_pci manage_pci
= {
127 .bus
= pci_dev
->bus
->number
,
128 .devfn
= pci_dev
->devfn
,
131 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add
,
138 static int xen_remove_device(struct device
*dev
)
141 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
143 if (pci_seg_supported
) {
144 struct physdev_pci_device device
= {
145 .seg
= pci_domain_nr(pci_dev
->bus
),
146 .bus
= pci_dev
->bus
->number
,
147 .devfn
= pci_dev
->devfn
150 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove
,
152 } else if (pci_domain_nr(pci_dev
->bus
))
155 struct physdev_manage_pci manage_pci
= {
156 .bus
= pci_dev
->bus
->number
,
157 .devfn
= pci_dev
->devfn
160 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove
,
167 static int xen_pci_notifier(struct notifier_block
*nb
,
168 unsigned long action
, void *data
)
170 struct device
*dev
= data
;
174 case BUS_NOTIFY_ADD_DEVICE
:
175 r
= xen_add_device(dev
);
177 case BUS_NOTIFY_DEL_DEVICE
:
178 r
= xen_remove_device(dev
);
184 dev_err(dev
, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
185 action
== BUS_NOTIFY_ADD_DEVICE
? "add" :
186 (action
== BUS_NOTIFY_DEL_DEVICE
? "delete" : "?"));
190 static struct notifier_block device_nb
= {
191 .notifier_call
= xen_pci_notifier
,
194 static int __init
register_xen_pci_notifier(void)
196 if (!xen_initial_domain())
199 return bus_register_notifier(&pci_bus_type
, &device_nb
);
202 arch_initcall(register_xen_pci_notifier
);
204 #ifdef CONFIG_PCI_MMCONFIG
205 static int __init
xen_mcfg_late(void)
207 struct pci_mmcfg_region
*cfg
;
210 if (!xen_initial_domain())
213 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
216 if (list_empty(&pci_mmcfg_list
))
219 /* Check whether they are in the right area. */
220 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
221 struct physdev_pci_mmcfg_reserved r
;
223 r
.address
= cfg
->address
;
224 r
.segment
= cfg
->segment
;
225 r
.start_bus
= cfg
->start_bus
;
226 r
.end_bus
= cfg
->end_bus
;
227 r
.flags
= XEN_PCI_MMCFG_RESERVED
;
229 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved
, &r
);
236 pr_warn("Failed to report MMCONFIG reservation"
237 " state for %s to hypervisor"
245 * Needs to be done after acpi_init which are subsys_initcall.
247 subsys_initcall_sync(xen_mcfg_late
);