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>
13 #include <xen/interface/physdev.h>
14 #include <xen/interface/xen.h>
16 #include <asm/xen/hypervisor.h>
17 #include <asm/xen/hypercall.h>
18 #include "../pci/pci.h"
19 #ifdef CONFIG_PCI_MMCONFIG
20 #include <asm/pci_x86.h>
22 static int xen_mcfg_late(void);
25 static bool __read_mostly pci_seg_supported = true;
27 static int xen_add_device(struct device *dev)
30 struct pci_dev *pci_dev = to_pci_dev(dev);
32 struct pci_dev *physfn = pci_dev->physfn;
34 #ifdef CONFIG_PCI_MMCONFIG
35 static bool pci_mcfg_reserved = false;
37 * Reserve MCFG areas in Xen on first invocation due to this being
38 * potentially called from inside of acpi_init immediately after
39 * MCFG table has been finally parsed.
41 if (!pci_mcfg_reserved) {
43 pci_mcfg_reserved = true;
46 if (pci_seg_supported) {
48 struct physdev_pci_device_add add;
51 .add.seg = pci_domain_nr(pci_dev->bus),
52 .add.bus = pci_dev->bus->number,
53 .add.devfn = pci_dev->devfn
55 struct physdev_pci_device_add *add = &add_ext.add;
62 if (pci_dev->is_virtfn) {
63 add->flags = XEN_PCI_DEV_VIRTFN;
64 add->physfn.bus = physfn->bus->number;
65 add->physfn.devfn = physfn->devfn;
68 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
69 add->flags = XEN_PCI_DEV_EXTFN;
72 handle = ACPI_HANDLE(&pci_dev->dev);
74 if (!handle && pci_dev->is_virtfn)
75 handle = ACPI_HANDLE(physfn->bus->bridge);
79 * This device was not listed in the ACPI name space at
80 * all. Try to get acpi handle of parent pci bus.
83 for (pbus = pci_dev->bus; pbus; pbus = pbus->parent) {
84 handle = acpi_pci_get_bridge_handle(pbus);
93 unsigned long long pxm;
95 status = acpi_evaluate_integer(handle, "_PXM",
97 if (ACPI_SUCCESS(status)) {
99 add->flags |= XEN_PCI_DEV_PXM;
102 status = acpi_get_parent(handle, &handle);
103 } while (ACPI_SUCCESS(status));
105 #endif /* CONFIG_ACPI */
107 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add);
110 pci_seg_supported = false;
113 if (pci_domain_nr(pci_dev->bus))
115 #ifdef CONFIG_PCI_IOV
116 else if (pci_dev->is_virtfn) {
117 struct physdev_manage_pci_ext manage_pci_ext = {
118 .bus = pci_dev->bus->number,
119 .devfn = pci_dev->devfn,
121 .physfn.bus = physfn->bus->number,
122 .physfn.devfn = physfn->devfn,
125 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
129 else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
130 struct physdev_manage_pci_ext manage_pci_ext = {
131 .bus = pci_dev->bus->number,
132 .devfn = pci_dev->devfn,
136 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
139 struct physdev_manage_pci manage_pci = {
140 .bus = pci_dev->bus->number,
141 .devfn = pci_dev->devfn,
144 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
151 static int xen_remove_device(struct device *dev)
154 struct pci_dev *pci_dev = to_pci_dev(dev);
156 if (pci_seg_supported) {
157 struct physdev_pci_device device = {
158 .seg = pci_domain_nr(pci_dev->bus),
159 .bus = pci_dev->bus->number,
160 .devfn = pci_dev->devfn
163 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_remove,
165 } else if (pci_domain_nr(pci_dev->bus))
168 struct physdev_manage_pci manage_pci = {
169 .bus = pci_dev->bus->number,
170 .devfn = pci_dev->devfn
173 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
180 static int xen_pci_notifier(struct notifier_block *nb,
181 unsigned long action, void *data)
183 struct device *dev = data;
187 case BUS_NOTIFY_ADD_DEVICE:
188 r = xen_add_device(dev);
190 case BUS_NOTIFY_DEL_DEVICE:
191 r = xen_remove_device(dev);
197 dev_err(dev, "Failed to %s - passthrough or MSI/MSI-X might fail!\n",
198 action == BUS_NOTIFY_ADD_DEVICE ? "add" :
199 (action == BUS_NOTIFY_DEL_DEVICE ? "delete" : "?"));
203 static struct notifier_block device_nb = {
204 .notifier_call = xen_pci_notifier,
207 static int __init register_xen_pci_notifier(void)
209 if (!xen_initial_domain())
212 return bus_register_notifier(&pci_bus_type, &device_nb);
215 arch_initcall(register_xen_pci_notifier);
217 #ifdef CONFIG_PCI_MMCONFIG
218 static int xen_mcfg_late(void)
220 struct pci_mmcfg_region *cfg;
223 if (!xen_initial_domain())
226 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
229 if (list_empty(&pci_mmcfg_list))
232 /* Check whether they are in the right area. */
233 list_for_each_entry(cfg, &pci_mmcfg_list, list) {
234 struct physdev_pci_mmcfg_reserved r;
236 r.address = cfg->address;
237 r.segment = cfg->segment;
238 r.start_bus = cfg->start_bus;
239 r.end_bus = cfg->end_bus;
240 r.flags = XEN_PCI_MMCFG_RESERVED;
242 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
249 pr_warn("Failed to report MMCONFIG reservation"
250 " state for %s to hypervisor"
259 #ifdef CONFIG_XEN_DOM0
260 struct xen_device_domain_owner {
263 struct list_head list;
266 static DEFINE_SPINLOCK(dev_domain_list_spinlock);
267 static LIST_HEAD(dev_domain_list);
269 static struct xen_device_domain_owner *find_device(struct pci_dev *dev)
271 struct xen_device_domain_owner *owner;
273 list_for_each_entry(owner, &dev_domain_list, list) {
274 if (owner->dev == dev)
280 int xen_find_device_domain_owner(struct pci_dev *dev)
282 struct xen_device_domain_owner *owner;
283 int domain = -ENODEV;
285 spin_lock(&dev_domain_list_spinlock);
286 owner = find_device(dev);
288 domain = owner->domain;
289 spin_unlock(&dev_domain_list_spinlock);
292 EXPORT_SYMBOL_GPL(xen_find_device_domain_owner);
294 int xen_register_device_domain_owner(struct pci_dev *dev, uint16_t domain)
296 struct xen_device_domain_owner *owner;
298 owner = kzalloc(sizeof(struct xen_device_domain_owner), GFP_KERNEL);
302 spin_lock(&dev_domain_list_spinlock);
303 if (find_device(dev)) {
304 spin_unlock(&dev_domain_list_spinlock);
308 owner->domain = domain;
310 list_add_tail(&owner->list, &dev_domain_list);
311 spin_unlock(&dev_domain_list_spinlock);
314 EXPORT_SYMBOL_GPL(xen_register_device_domain_owner);
316 int xen_unregister_device_domain_owner(struct pci_dev *dev)
318 struct xen_device_domain_owner *owner;
320 spin_lock(&dev_domain_list_spinlock);
321 owner = find_device(dev);
323 spin_unlock(&dev_domain_list_spinlock);
326 list_del(&owner->list);
327 spin_unlock(&dev_domain_list_spinlock);
331 EXPORT_SYMBOL_GPL(xen_unregister_device_domain_owner);