1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (c) 2009, Intel Corporation.
5 * Author: Weidong Han <weidong.han@intel.com>
9 #include <linux/acpi.h>
10 #include <linux/pci-acpi.h>
12 #include <xen/interface/physdev.h>
13 #include <xen/interface/xen.h>
15 #include <asm/xen/hypervisor.h>
16 #include <asm/xen/hypercall.h>
17 #include "../pci/pci.h"
18 #ifdef CONFIG_PCI_MMCONFIG
19 #include <asm/pci_x86.h>
21 static int xen_mcfg_late(void);
24 static bool __read_mostly pci_seg_supported
= true;
26 static int xen_add_device(struct device
*dev
)
29 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
31 struct pci_dev
*physfn
= pci_dev
->physfn
;
33 #ifdef CONFIG_PCI_MMCONFIG
34 static bool pci_mcfg_reserved
= false;
36 * Reserve MCFG areas in Xen on first invocation due to this being
37 * potentially called from inside of acpi_init immediately after
38 * MCFG table has been finally parsed.
40 if (!pci_mcfg_reserved
) {
42 pci_mcfg_reserved
= true;
45 if (pci_seg_supported
) {
47 struct physdev_pci_device_add add
;
50 .add
.seg
= pci_domain_nr(pci_dev
->bus
),
51 .add
.bus
= pci_dev
->bus
->number
,
52 .add
.devfn
= pci_dev
->devfn
54 struct physdev_pci_device_add
*add
= &add_ext
.add
;
61 if (pci_dev
->is_virtfn
) {
62 add
->flags
= XEN_PCI_DEV_VIRTFN
;
63 add
->physfn
.bus
= physfn
->bus
->number
;
64 add
->physfn
.devfn
= physfn
->devfn
;
67 if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
))
68 add
->flags
= XEN_PCI_DEV_EXTFN
;
71 handle
= ACPI_HANDLE(&pci_dev
->dev
);
73 if (!handle
&& pci_dev
->is_virtfn
)
74 handle
= ACPI_HANDLE(physfn
->bus
->bridge
);
78 * This device was not listed in the ACPI name space at
79 * all. Try to get acpi handle of parent pci bus.
82 for (pbus
= pci_dev
->bus
; pbus
; pbus
= pbus
->parent
) {
83 handle
= acpi_pci_get_bridge_handle(pbus
);
92 unsigned long long pxm
;
94 status
= acpi_evaluate_integer(handle
, "_PXM",
96 if (ACPI_SUCCESS(status
)) {
98 add
->flags
|= XEN_PCI_DEV_PXM
;
101 status
= acpi_get_parent(handle
, &handle
);
102 } while (ACPI_SUCCESS(status
));
104 #endif /* CONFIG_ACPI */
106 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add
, add
);
109 pci_seg_supported
= false;
112 if (pci_domain_nr(pci_dev
->bus
))
114 #ifdef CONFIG_PCI_IOV
115 else if (pci_dev
->is_virtfn
) {
116 struct physdev_manage_pci_ext manage_pci_ext
= {
117 .bus
= pci_dev
->bus
->number
,
118 .devfn
= pci_dev
->devfn
,
120 .physfn
.bus
= physfn
->bus
->number
,
121 .physfn
.devfn
= physfn
->devfn
,
124 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
128 else if (pci_ari_enabled(pci_dev
->bus
) && PCI_SLOT(pci_dev
->devfn
)) {
129 struct physdev_manage_pci_ext manage_pci_ext
= {
130 .bus
= pci_dev
->bus
->number
,
131 .devfn
= pci_dev
->devfn
,
135 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext
,
138 struct physdev_manage_pci manage_pci
= {
139 .bus
= pci_dev
->bus
->number
,
140 .devfn
= pci_dev
->devfn
,
143 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add
,
150 static int xen_remove_device(struct device
*dev
)
153 struct pci_dev
*pci_dev
= to_pci_dev(dev
);
155 if (pci_seg_supported
) {
156 struct physdev_pci_device device
= {
157 .seg
= pci_domain_nr(pci_dev
->bus
),
158 .bus
= pci_dev
->bus
->number
,
159 .devfn
= pci_dev
->devfn
162 r
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove
,
164 } else if (pci_domain_nr(pci_dev
->bus
))
167 struct physdev_manage_pci manage_pci
= {
168 .bus
= pci_dev
->bus
->number
,
169 .devfn
= pci_dev
->devfn
172 r
= HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove
,
179 static int xen_pci_notifier(struct notifier_block
*nb
,
180 unsigned long action
, void *data
)
182 struct device
*dev
= data
;
186 case BUS_NOTIFY_ADD_DEVICE
:
187 r
= xen_add_device(dev
);
189 case BUS_NOTIFY_DEL_DEVICE
:
190 r
= xen_remove_device(dev
);
196 dev_err(dev
, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
197 action
== BUS_NOTIFY_ADD_DEVICE
? "add" :
198 (action
== BUS_NOTIFY_DEL_DEVICE
? "delete" : "?"));
202 static struct notifier_block device_nb
= {
203 .notifier_call
= xen_pci_notifier
,
206 static int __init
register_xen_pci_notifier(void)
208 if (!xen_initial_domain())
211 return bus_register_notifier(&pci_bus_type
, &device_nb
);
214 arch_initcall(register_xen_pci_notifier
);
216 #ifdef CONFIG_PCI_MMCONFIG
217 static int xen_mcfg_late(void)
219 struct pci_mmcfg_region
*cfg
;
222 if (!xen_initial_domain())
225 if ((pci_probe
& PCI_PROBE_MMCONF
) == 0)
228 if (list_empty(&pci_mmcfg_list
))
231 /* Check whether they are in the right area. */
232 list_for_each_entry(cfg
, &pci_mmcfg_list
, list
) {
233 struct physdev_pci_mmcfg_reserved r
;
235 r
.address
= cfg
->address
;
236 r
.segment
= cfg
->segment
;
237 r
.start_bus
= cfg
->start_bus
;
238 r
.end_bus
= cfg
->end_bus
;
239 r
.flags
= XEN_PCI_MMCFG_RESERVED
;
241 rc
= HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved
, &r
);
248 pr_warn("Failed to report MMCONFIG reservation"
249 " state for %s to hypervisor"