Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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> |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 21 | #include <linux/acpi.h> |
Ross Lagerwall | 0b97b03 | 2015-04-09 08:05:10 +0100 | [diff] [blame] | 22 | #include <linux/pci-acpi.h> |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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" |
Ben Hutchings | b7ef4a6 | 2013-12-31 20:46:27 +0100 | [diff] [blame] | 30 | #ifdef CONFIG_PCI_MMCONFIG |
Konrad Rzeszutek Wilk | 8deb3eb | 2013-10-25 16:26:02 -0400 | [diff] [blame] | 31 | #include <asm/pci_x86.h> |
Igor Druzhinin | 6fdcfc0 | 2019-09-12 19:31:51 +0100 | [diff] [blame] | 32 | |
| 33 | static int xen_mcfg_late(void); |
Ben Hutchings | b7ef4a6 | 2013-12-31 20:46:27 +0100 | [diff] [blame] | 34 | #endif |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 35 | |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 36 | static bool __read_mostly pci_seg_supported = true; |
| 37 | |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 38 | static int xen_add_device(struct device *dev) |
| 39 | { |
| 40 | int r; |
| 41 | struct pci_dev *pci_dev = to_pci_dev(dev); |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 42 | #ifdef CONFIG_PCI_IOV |
| 43 | struct pci_dev *physfn = pci_dev->physfn; |
| 44 | #endif |
Igor Druzhinin | 6fdcfc0 | 2019-09-12 19:31:51 +0100 | [diff] [blame] | 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 |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 57 | if (pci_seg_supported) { |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 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 |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 65 | }; |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 66 | struct physdev_pci_device_add *add = &add_ext.add; |
| 67 | |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 68 | #ifdef CONFIG_ACPI |
| 69 | acpi_handle handle; |
| 70 | #endif |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 71 | |
| 72 | #ifdef CONFIG_PCI_IOV |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 73 | if (pci_dev->is_virtfn) { |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 74 | add->flags = XEN_PCI_DEV_VIRTFN; |
| 75 | add->physfn.bus = physfn->bus->number; |
| 76 | add->physfn.devfn = physfn->devfn; |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 77 | } else |
| 78 | #endif |
| 79 | if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 80 | add->flags = XEN_PCI_DEV_EXTFN; |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 81 | |
| 82 | #ifdef CONFIG_ACPI |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 83 | handle = ACPI_HANDLE(&pci_dev->dev); |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 84 | #ifdef CONFIG_PCI_IOV |
| 85 | if (!handle && pci_dev->is_virtfn) |
Rafael J. Wysocki | 3a83f99 | 2013-11-14 23:17:21 +0100 | [diff] [blame] | 86 | handle = ACPI_HANDLE(physfn->bus->bridge); |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 87 | #endif |
Ross Lagerwall | 0b97b03 | 2015-04-09 08:05:10 +0100 | [diff] [blame] | 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 | } |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 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)) { |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 109 | add->optarr[0] = pxm; |
| 110 | add->flags |= XEN_PCI_DEV_PXM; |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 111 | break; |
| 112 | } |
| 113 | status = acpi_get_parent(handle, &handle); |
| 114 | } while (ACPI_SUCCESS(status)); |
| 115 | } |
| 116 | #endif /* CONFIG_ACPI */ |
| 117 | |
Boris Ostrovsky | 486edb2 | 2014-08-04 18:17:23 -0400 | [diff] [blame] | 118 | r = HYPERVISOR_physdev_op(PHYSDEVOP_pci_device_add, add); |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 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) { |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 128 | struct physdev_manage_pci_ext manage_pci_ext = { |
| 129 | .bus = pci_dev->bus->number, |
| 130 | .devfn = pci_dev->devfn, |
| 131 | .is_virtfn = 1, |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 132 | .physfn.bus = physfn->bus->number, |
| 133 | .physfn.devfn = physfn->devfn, |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 134 | }; |
| 135 | |
| 136 | r = HYPERVISOR_physdev_op(PHYSDEVOP_manage_pci_add_ext, |
| 137 | &manage_pci_ext); |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 138 | } |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 139 | #endif |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 140 | else if (pci_ari_enabled(pci_dev->bus) && PCI_SLOT(pci_dev->devfn)) { |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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 = { |
Ruslan Pisarev | 4b01098 | 2011-07-26 14:16:38 +0300 | [diff] [blame] | 151 | .bus = pci_dev->bus->number, |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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); |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 166 | |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 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 | }; |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 173 | |
Jan Beulich | 55e901f | 2011-09-22 09:17:57 +0100 | [diff] [blame] | 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 | } |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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: |
Jan Beulich | 12e13ac | 2011-08-17 09:32:32 +0100 | [diff] [blame] | 205 | return NOTIFY_DONE; |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 206 | } |
Jan Beulich | 12e13ac | 2011-08-17 09:32:32 +0100 | [diff] [blame] | 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; |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Jan Beulich | 12e13ac | 2011-08-17 09:32:32 +0100 | [diff] [blame] | 214 | static struct notifier_block device_nb = { |
Weidong Han | e28c31a | 2010-10-27 17:55:04 +0100 | [diff] [blame] | 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); |
Konrad Rzeszutek Wilk | 8deb3eb | 2013-10-25 16:26:02 -0400 | [diff] [blame] | 227 | |
| 228 | #ifdef CONFIG_PCI_MMCONFIG |
Igor Druzhinin | 6fdcfc0 | 2019-09-12 19:31:51 +0100 | [diff] [blame] | 229 | static int xen_mcfg_late(void) |
Konrad Rzeszutek Wilk | 8deb3eb | 2013-10-25 16:26:02 -0400 | [diff] [blame] | 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 | } |
Konrad Rzeszutek Wilk | 8deb3eb | 2013-10-25 16:26:02 -0400 | [diff] [blame] | 268 | #endif |