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>
33 static int xen_mcfg_late(void);
36 static bool __read_mostly pci_seg_supported
= true;
38 static int xen_add_device(struct device
*dev
)
41 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
43 struct pci_dev
*physfn
= pci_dev
->physfn
;
45 #ifdef CONFIG_PCI_MMCONFIG
46 static bool pci_mcfg_reserved
= false;
48 * Reserve MCFG areas in Xen on first invocation due to this being
49 * potentially called from inside of acpi_init immediately after
50 * MCFG table has been finally parsed.
52 if (!pci_mcfg_reserved
) {
54 pci_mcfg_reserved
= true;
57 if (pci_seg_supported
) {
59 struct physdev_pci_device_add add
;
62 .add
.seg
= pci_domain_nr(pci_dev
->bus
),
63 .add
.bus
= pci_dev
->bus
->number
,
64 .add
.devfn
= pci_dev
->devfn
66 struct physdev_pci_device_add
*add
= &add_ext
.add
;
73 if (pci_dev
->is_virtfn
) {
74 add
->flags
= XEN_PCI_DEV_VIRTFN
;
75 add
->physfn
.bus
= physfn
->bus
->number
;
76 add
->physfn
.devfn
= physfn
->devfn
;
79 if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
))
80 add
->flags
= XEN_PCI_DEV_EXTFN
;
83 handle
= ACPI_HANDLE(&pci_dev
->dev
);
85 if (!handle
&& pci_dev
->is_virtfn
)
86 handle
= ACPI_HANDLE(physfn
->bus
->bridge
);
90 * This device was not listed in the ACPI name space at
91 * all. Try to get acpi handle of parent pci bus.
94 for (pbus
= pci_dev
->bus
; pbus
; pbus
= pbus
->parent
) {
95 handle
= acpi_pci_get_bridge_handle(pbus
);
104 unsigned long long pxm
;
106 status
= acpi_evaluate_integer(handle
, "_PXM",
108 if (ACPI_SUCCESS(status
)) {
109 add
->optarr
[0] = pxm
;
110 add
->flags
|= XEN_PCI_DEV_PXM
;
113 status
= acpi_get_parent(handle
, &handle
);
114 } while (ACPI_SUCCESS(status
));
116 #endif /* CONFIG_ACPI */
118 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add
, add
);
121 pci_seg_supported
= false;
124 if (pci_domain_nr(pci_dev
->bus
))
126 #ifdef CONFIG_PCI_IOV
127 else if (pci_dev
->is_virtfn
) {
128 struct physdev_manage_pci_ext manage_pci_ext
= {
129 .bus
= pci_dev
->bus
->number
,
130 .devfn
= pci_dev
->devfn
,
132 .physfn
.bus
= physfn
->bus
->number
,
133 .physfn
.devfn
= physfn
->devfn
,
136 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
140 else if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
)) {
141 struct physdev_manage_pci_ext manage_pci_ext
= {
142 .bus
= pci_dev
->bus
->number
,
143 .devfn
= pci_dev
->devfn
,
147 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
150 struct physdev_manage_pci manage_pci
= {
151 .bus
= pci_dev
->bus
->number
,
152 .devfn
= pci_dev
->devfn
,
155 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add
,
162 static int xen_remove_device(struct device
*dev
)
165 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
167 if (pci_seg_supported
) {
168 struct physdev_pci_device device
= {
169 .seg
= pci_domain_nr(pci_dev
->bus
),
170 .bus
= pci_dev
->bus
->number
,
171 .devfn
= pci_dev
->devfn
174 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove
,
176 } else if (pci_domain_nr(pci_dev
->bus
))
179 struct physdev_manage_pci manage_pci
= {
180 .bus
= pci_dev
->bus
->number
,
181 .devfn
= pci_dev
->devfn
184 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove
,
191 static int xen_pci_notifier(struct notifier_block
*nb
,
192 unsigned long action
, void *data
)
194 struct device
*dev
= data
;
198 case BUS_NOTIFY_ADD_DEVICE
:
199 r
= xen_add_device(dev
);
201 case BUS_NOTIFY_DEL_DEVICE
:
202 r
= xen_remove_device(dev
);
208 dev_err(dev
, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
209 action
== BUS_NOTIFY_ADD_DEVICE
? "add" :
210 (action
== BUS_NOTIFY_DEL_DEVICE
? "delete" : "?"));
214 static struct notifier_block device_nb
= {
215 .notifier_call
= xen_pci_notifier
,
218 static int __init
register_xen_pci_notifier(void)
220 if (!xen_initial_domain())
223 return bus_register_notifier(&pci_bus_type
, &device_nb
);
226 arch_initcall(register_xen_pci_notifier
);
228 #ifdef CONFIG_PCI_MMCONFIG
229 static int xen_mcfg_late(void)
231 struct pci_mmcfg_region
*cfg
;
234 if (!xen_initial_domain())
237 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
240 if (list_empty(&pci_mmcfg_list
))
243 /* Check whether they are in the right area. */
244 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
245 struct physdev_pci_mmcfg_reserved r
;
247 r
.address
= cfg
->address
;
248 r
.segment
= cfg
->segment
;
249 r
.start_bus
= cfg
->start_bus
;
250 r
.end_bus
= cfg
->end_bus
;
251 r
.flags
= XEN_PCI_MMCFG_RESERVED
;
253 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved
, &r
);
260 pr_warn("Failed to report MMCONFIG reservation"
261 " state for %s to hypervisor"