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
) {
44 struct physdev_pci_device_add add
= {
45 .seg
= pci_domain_nr(pci_dev
->bus
),
46 .bus
= pci_dev
->bus
->number
,
47 .devfn
= pci_dev
->devfn
54 if (pci_dev
->is_virtfn
) {
55 add
.flags
= XEN_PCI_DEV_VIRTFN
;
56 add
.physfn
.bus
= physfn
->bus
->number
;
57 add
.physfn
.devfn
= physfn
->devfn
;
60 if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
))
61 add
.flags
= XEN_PCI_DEV_EXTFN
;
64 handle
= ACPI_HANDLE(&pci_dev
->dev
);
65 if (!handle
&& pci_dev
->bus
->bridge
)
66 handle
= ACPI_HANDLE(pci_dev
->bus
->bridge
);
68 if (!handle
&& pci_dev
->is_virtfn
)
69 handle
= ACPI_HANDLE(physfn
->bus
->bridge
);
75 unsigned long long pxm
;
77 status
= acpi_evaluate_integer(handle
, "_PXM",
79 if (ACPI_SUCCESS(status
)) {
81 add
.flags
|= XEN_PCI_DEV_PXM
;
84 status
= acpi_get_parent(handle
, &handle
);
85 } while (ACPI_SUCCESS(status
));
87 #endif /* CONFIG_ACPI */
89 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add
, &add
);
92 pci_seg_supported
= false;
95 if (pci_domain_nr(pci_dev
->bus
))
98 else if (pci_dev
->is_virtfn
) {
99 struct physdev_manage_pci_ext manage_pci_ext
= {
100 .bus
= pci_dev
->bus
->number
,
101 .devfn
= pci_dev
->devfn
,
103 .physfn
.bus
= physfn
->bus
->number
,
104 .physfn
.devfn
= physfn
->devfn
,
107 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
111 else if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
)) {
112 struct physdev_manage_pci_ext manage_pci_ext
= {
113 .bus
= pci_dev
->bus
->number
,
114 .devfn
= pci_dev
->devfn
,
118 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
121 struct physdev_manage_pci manage_pci
= {
122 .bus
= pci_dev
->bus
->number
,
123 .devfn
= pci_dev
->devfn
,
126 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add
,
133 static int xen_remove_device(struct device
*dev
)
136 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
138 if (pci_seg_supported
) {
139 struct physdev_pci_device device
= {
140 .seg
= pci_domain_nr(pci_dev
->bus
),
141 .bus
= pci_dev
->bus
->number
,
142 .devfn
= pci_dev
->devfn
145 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove
,
147 } else if (pci_domain_nr(pci_dev
->bus
))
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_remove
,
162 static int xen_pci_notifier(struct notifier_block
*nb
,
163 unsigned long action
, void *data
)
165 struct device
*dev
= data
;
169 case BUS_NOTIFY_ADD_DEVICE
:
170 r
= xen_add_device(dev
);
172 case BUS_NOTIFY_DEL_DEVICE
:
173 r
= xen_remove_device(dev
);
179 dev_err(dev
, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
180 action
== BUS_NOTIFY_ADD_DEVICE
? "add" :
181 (action
== BUS_NOTIFY_DEL_DEVICE
? "delete" : "?"));
185 static struct notifier_block device_nb
= {
186 .notifier_call
= xen_pci_notifier
,
189 static int __init
register_xen_pci_notifier(void)
191 if (!xen_initial_domain())
194 return bus_register_notifier(&pci_bus_type
, &device_nb
);
197 arch_initcall(register_xen_pci_notifier
);
199 #ifdef CONFIG_PCI_MMCONFIG
200 static int __init
xen_mcfg_late(void)
202 struct pci_mmcfg_region
*cfg
;
205 if (!xen_initial_domain())
208 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
211 if (list_empty(&pci_mmcfg_list
))
214 /* Check whether they are in the right area. */
215 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
216 struct physdev_pci_mmcfg_reserved r
;
218 r
.address
= cfg
->address
;
219 r
.segment
= cfg
->segment
;
220 r
.start_bus
= cfg
->start_bus
;
221 r
.end_bus
= cfg
->end_bus
;
222 r
.flags
= XEN_PCI_MMCFG_RESERVED
;
224 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved
, &r
);
231 pr_warn("Failed to report MMCONFIG reservation"
232 " state for %s to hypervisor"
240 * Needs to be done after acpi_init which are subsys_initcall.
242 subsys_initcall_sync(xen_mcfg_late
);