Linux 4.19.133
[linux/fpc-iii.git] / drivers / xen / pci.c
blobdb58aaa4dc5983ac41b2c6c228440749790f7412
1 /*
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
11 * more details.
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>
23 #include <xen/xen.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);
34 #endif
36 static bool __read_mostly pci_seg_supported = true;
38 static int xen_add_device(struct device *dev)
40 int r;
41 struct pci_dev *pci_dev = to_pci_dev(dev);
42 #ifdef CONFIG_PCI_IOV
43 struct pci_dev *physfn = pci_dev->physfn;
44 #endif
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) {
53 xen_mcfg_late();
54 pci_mcfg_reserved = true;
56 #endif
57 if (pci_seg_supported) {
58 struct {
59 struct physdev_pci_device_add add;
60 uint32_t pxm;
61 } add_ext = {
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;
68 #ifdef CONFIG_ACPI
69 acpi_handle handle;
70 #endif
72 #ifdef CONFIG_PCI_IOV
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;
77 } else
78 #endif
79 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
80 add->flags = XEN_PCI_DEV_EXTFN;
82 #ifdef CONFIG_ACPI
83 handle = ACPI_HANDLE(&pci_dev->dev);
84 #ifdef CONFIG_PCI_IOV
85 if (!handle && pci_dev->is_virtfn)
86 handle = ACPI_HANDLE(physfn->bus->bridge);
87 #endif
88 if (!handle) {
90 * This device was not listed in the ACPI name space at
91 * all. Try to get acpi handle of parent pci bus.
93 struct pci_bus *pbus;
94 for (pbus = pci_dev->bus; pbus; pbus = pbus->parent) {
95 handle = acpi_pci_get_bridge_handle(pbus);
96 if (handle)
97 break;
100 if (handle) {
101 acpi_status status;
103 do {
104 unsigned long long pxm;
106 status = acpi_evaluate_integer(handle, "_PXM",
107 NULL, &pxm);
108 if (ACPI_SUCCESS(status)) {
109 add->optarr[0] = pxm;
110 add->flags |= XEN_PCI_DEV_PXM;
111 break;
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);
119 if (r != -ENOSYS)
120 return r;
121 pci_seg_supported = false;
124 if (pci_domain_nr(pci_dev->bus))
125 r = -ENOSYS;
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,
131 .is_virtfn = 1,
132 .physfn.bus = physfn->bus->number,
133 .physfn.devfn = physfn->devfn,
136 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
137 &manage_pci_ext);
139 #endif
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,
144 .is_extfn = 1,
147 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
148 &manage_pci_ext);
149 } else {
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,
156 &manage_pci);
159 return r;
162 static int xen_remove_device(struct device *dev)
164 int r;
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,
175 &device);
176 } else if (pci_domain_nr(pci_dev->bus))
177 r = -ENOSYS;
178 else {
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,
185 &manage_pci);
188 return r;
191 static int xen_pci_notifier(struct notifier_block *nb,
192 unsigned long action, void *data)
194 struct device *dev = data;
195 int r = 0;
197 switch (action) {
198 case BUS_NOTIFY_ADD_DEVICE:
199 r = xen_add_device(dev);
200 break;
201 case BUS_NOTIFY_DEL_DEVICE:
202 r = xen_remove_device(dev);
203 break;
204 default:
205 return NOTIFY_DONE;
207 if (r)
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" : "?"));
211 return NOTIFY_OK;
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())
221 return 0;
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;
232 int rc;
234 if (!xen_initial_domain())
235 return 0;
237 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
238 return 0;
240 if (list_empty(&pci_mmcfg_list))
241 return 0;
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);
254 switch (rc) {
255 case 0:
256 case -ENOSYS:
257 continue;
259 default:
260 pr_warn("Failed to report MMCONFIG reservation"
261 " state for %s to hypervisor"
262 " (%d)\n",
263 cfg->name, rc);
266 return 0;
268 #endif