blob: db58aaa4dc5983ac41b2c6c228440749790f7412 [file] [log] [blame]
Weidong Hane28c31a2010-10-27 17:55:04 +01001/*
2 * Copyright (c) 2009, Intel Corporation.
3 *
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.
7 *
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.
12 *
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.
16 *
17 * Author: Weidong Han <weidong.han@intel.com>
18 */
19
20#include <linux/pci.h>
Jan Beulich55e901f2011-09-22 09:17:57 +010021#include <linux/acpi.h>
Ross Lagerwall0b97b032015-04-09 08:05:10 +010022#include <linux/pci-acpi.h>
Weidong Hane28c31a2010-10-27 17:55:04 +010023#include <xen/xen.h>
24#include <xen/interface/physdev.h>
25#include <xen/interface/xen.h>
26
27#include <asm/xen/hypervisor.h>
28#include <asm/xen/hypercall.h>
29#include "../pci/pci.h"
Ben Hutchingsb7ef4a62013-12-31 20:46:27 +010030#ifdef CONFIG_PCI_MMCONFIG
Konrad Rzeszutek Wilk8deb3eb2013-10-25 16:26:02 -040031#include <asm/pci_x86.h>
Igor Druzhinin6fdcfc02019-09-12 19:31:51 +010032
33static int xen_mcfg_late(void);
Ben Hutchingsb7ef4a62013-12-31 20:46:27 +010034#endif
Weidong Hane28c31a2010-10-27 17:55:04 +010035
Jan Beulich55e901f2011-09-22 09:17:57 +010036static bool __read_mostly pci_seg_supported = true;
37
Weidong Hane28c31a2010-10-27 17:55:04 +010038static int xen_add_device(struct device *dev)
39{
40 int r;
41 struct pci_dev *pci_dev = to_pci_dev(dev);
Jan Beulich55e901f2011-09-22 09:17:57 +010042#ifdef CONFIG_PCI_IOV
43 struct pci_dev *physfn = pci_dev->physfn;
44#endif
Igor Druzhinin6fdcfc02019-09-12 19:31:51 +010045#ifdef CONFIG_PCI_MMCONFIG
46 static bool pci_mcfg_reserved = false;
47 /*
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.
51 */
52 if (!pci_mcfg_reserved) {
53 xen_mcfg_late();
54 pci_mcfg_reserved = true;
55 }
56#endif
Jan Beulich55e901f2011-09-22 09:17:57 +010057 if (pci_seg_supported) {
Boris Ostrovsky486edb22014-08-04 18:17:23 -040058 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
Jan Beulich55e901f2011-09-22 09:17:57 +010065 };
Boris Ostrovsky486edb22014-08-04 18:17:23 -040066 struct physdev_pci_device_add *add = &add_ext.add;
67
Jan Beulich55e901f2011-09-22 09:17:57 +010068#ifdef CONFIG_ACPI
69 acpi_handle handle;
70#endif
Weidong Hane28c31a2010-10-27 17:55:04 +010071
72#ifdef CONFIG_PCI_IOV
Jan Beulich55e901f2011-09-22 09:17:57 +010073 if (pci_dev->is_virtfn) {
Boris Ostrovsky486edb22014-08-04 18:17:23 -040074 add->flags = XEN_PCI_DEV_VIRTFN;
75 add->physfn.bus = physfn->bus->number;
76 add->physfn.devfn = physfn->devfn;
Jan Beulich55e901f2011-09-22 09:17:57 +010077 } else
78#endif
79 if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn))
Boris Ostrovsky486edb22014-08-04 18:17:23 -040080 add->flags = XEN_PCI_DEV_EXTFN;
Jan Beulich55e901f2011-09-22 09:17:57 +010081
82#ifdef CONFIG_ACPI
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +010083 handle = ACPI_HANDLE(&pci_dev->dev);
Jan Beulich55e901f2011-09-22 09:17:57 +010084#ifdef CONFIG_PCI_IOV
85 if (!handle && pci_dev->is_virtfn)
Rafael J. Wysocki3a83f992013-11-14 23:17:21 +010086 handle = ACPI_HANDLE(physfn->bus->bridge);
Jan Beulich55e901f2011-09-22 09:17:57 +010087#endif
Ross Lagerwall0b97b032015-04-09 08:05:10 +010088 if (!handle) {
89 /*
90 * This device was not listed in the ACPI name space at
91 * all. Try to get acpi handle of parent pci bus.
92 */
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;
98 }
99 }
Jan Beulich55e901f2011-09-22 09:17:57 +0100100 if (handle) {
101 acpi_status status;
102
103 do {
104 unsigned long long pxm;
105
106 status = acpi_evaluate_integer(handle, "_PXM",
107 NULL, &pxm);
108 if (ACPI_SUCCESS(status)) {
Boris Ostrovsky486edb22014-08-04 18:17:23 -0400109 add->optarr[0] = pxm;
110 add->flags |= XEN_PCI_DEV_PXM;
Jan Beulich55e901f2011-09-22 09:17:57 +0100111 break;
112 }
113 status = acpi_get_parent(handle, &handle);
114 } while (ACPI_SUCCESS(status));
115 }
116#endif /* CONFIG_ACPI */
117
Boris Ostrovsky486edb22014-08-04 18:17:23 -0400118 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add);
Jan Beulich55e901f2011-09-22 09:17:57 +0100119 if (r != -ENOSYS)
120 return r;
121 pci_seg_supported = false;
122 }
123
124 if (pci_domain_nr(pci_dev->bus))
125 r = -ENOSYS;
126#ifdef CONFIG_PCI_IOV
127 else if (pci_dev->is_virtfn) {
Weidong Hane28c31a2010-10-27 17:55:04 +0100128 struct physdev_manage_pci_ext manage_pci_ext = {
129 .bus = pci_dev->bus->number,
130 .devfn = pci_dev->devfn,
131 .is_virtfn = 1,
Jan Beulich55e901f2011-09-22 09:17:57 +0100132 .physfn.bus = physfn->bus->number,
133 .physfn.devfn = physfn->devfn,
Weidong Hane28c31a2010-10-27 17:55:04 +0100134 };
135
136 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
137 &manage_pci_ext);
Jan Beulich55e901f2011-09-22 09:17:57 +0100138 }
Weidong Hane28c31a2010-10-27 17:55:04 +0100139#endif
Jan Beulich55e901f2011-09-22 09:17:57 +0100140 else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) {
Weidong Hane28c31a2010-10-27 17:55:04 +0100141 struct physdev_manage_pci_ext manage_pci_ext = {
142 .bus = pci_dev->bus->number,
143 .devfn = pci_dev->devfn,
144 .is_extfn = 1,
145 };
146
147 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
148 &manage_pci_ext);
149 } else {
150 struct physdev_manage_pci manage_pci = {
Ruslan Pisarev4b010982011-07-26 14:16:38 +0300151 .bus = pci_dev->bus->number,
Weidong Hane28c31a2010-10-27 17:55:04 +0100152 .devfn = pci_dev->devfn,
153 };
154
155 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add,
156 &manage_pci);
157 }
158
159 return r;
160}
161
162static int xen_remove_device(struct device *dev)
163{
164 int r;
165 struct pci_dev *pci_dev = to_pci_dev(dev);
Weidong Hane28c31a2010-10-27 17:55:04 +0100166
Jan Beulich55e901f2011-09-22 09:17:57 +0100167 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
172 };
Weidong Hane28c31a2010-10-27 17:55:04 +0100173
Jan Beulich55e901f2011-09-22 09:17:57 +0100174 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
182 };
183
184 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
185 &manage_pci);
186 }
Weidong Hane28c31a2010-10-27 17:55:04 +0100187
188 return r;
189}
190
191static int xen_pci_notifier(struct notifier_block *nb,
192 unsigned long action, void *data)
193{
194 struct device *dev = data;
195 int r = 0;
196
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:
Jan Beulich12e13ac2011-08-17 09:32:32 +0100205 return NOTIFY_DONE;
Weidong Hane28c31a2010-10-27 17:55:04 +0100206 }
Jan Beulich12e13ac2011-08-17 09:32:32 +0100207 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;
Weidong Hane28c31a2010-10-27 17:55:04 +0100212}
213
Jan Beulich12e13ac2011-08-17 09:32:32 +0100214static struct notifier_block device_nb = {
Weidong Hane28c31a2010-10-27 17:55:04 +0100215 .notifier_call = xen_pci_notifier,
216};
217
218static int __init register_xen_pci_notifier(void)
219{
220 if (!xen_initial_domain())
221 return 0;
222
223 return bus_register_notifier(&pci_bus_type, &device_nb);
224}
225
226arch_initcall(register_xen_pci_notifier);
Konrad Rzeszutek Wilk8deb3eb2013-10-25 16:26:02 -0400227
228#ifdef CONFIG_PCI_MMCONFIG
Igor Druzhinin6fdcfc02019-09-12 19:31:51 +0100229static int xen_mcfg_late(void)
Konrad Rzeszutek Wilk8deb3eb2013-10-25 16:26:02 -0400230{
231 struct pci_mmcfg_region *cfg;
232 int rc;
233
234 if (!xen_initial_domain())
235 return 0;
236
237 if ((pci_probe & PCI_PROBE_MMCONF) == 0)
238 return 0;
239
240 if (list_empty(&pci_mmcfg_list))
241 return 0;
242
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;
246
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;
252
253 rc = HYPERVISOR_physdev_op(PHYSDEVOP_pci_mmcfg_reserved, &r);
254 switch (rc) {
255 case 0:
256 case -ENOSYS:
257 continue;
258
259 default:
260 pr_warn("Failed to report MMCONFIG reservation"
261 " state for %s to hypervisor"
262 " (%d)\n",
263 cfg->name, rc);
264 }
265 }
266 return 0;
267}
Konrad Rzeszutek Wilk8deb3eb2013-10-25 16:26:02 -0400268#endif