GNU Linux-libre 4.9.304-gnu1
[releases.git] / drivers / xen / pci.c
1 /*
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>
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>
26
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>
32
33 static int xen_mcfg_late(void);
34 #endif
35
36 static bool __read_mostly pci_seg_supported = true;
37
38 static int xen_add_device(struct device *dev)
39 {
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;
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
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
65                 };
66                 struct physdev_pci_device_add *add = &add_ext.add;
67
68 #ifdef CONFIG_ACPI
69                 acpi_handle handle;
70 #endif
71
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;
81
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) {
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                 }
100                 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)) {
109                                         add->optarr[0] = pxm;
110                                         add->flags |= XEN_PCI_DEV_PXM;
111                                         break;
112                                 }
113                                 status = acpi_get_parent(handle, &handle);
114                         } while (ACPI_SUCCESS(status));
115                 }
116 #endif /* CONFIG_ACPI */
117
118                 r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add);
119                 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) {
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,
134                 };
135
136                 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext,
137                         &manage_pci_ext);
138         }
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,
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 = {
151                         .bus    = pci_dev->bus->number,
152                         .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
162 static int xen_remove_device(struct device *dev)
163 {
164         int r;
165         struct pci_dev *pci_dev = to_pci_dev(dev);
166
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
172                 };
173
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
182                 };
183
184                 r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_remove,
185                                           &manage_pci);
186         }
187
188         return r;
189 }
190
191 static 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:
205                 return NOTIFY_DONE;
206         }
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;
212 }
213
214 static struct notifier_block device_nb = {
215         .notifier_call = xen_pci_notifier,
216 };
217
218 static 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
226 arch_initcall(register_xen_pci_notifier);
227
228 #ifdef CONFIG_PCI_MMCONFIG
229 static int xen_mcfg_late(void)
230 {
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 }
268 #endif