Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 1 | /* |
| 2 | * QEMU<->ACPI BIOS PCI hotplug interface |
| 3 | * |
| 4 | * QEMU supports PCI hotplug via ACPI. This module |
| 5 | * implements the interface between QEMU and the ACPI BIOS. |
| 6 | * Interface specification - see docs/specs/acpi_pci_hotplug.txt |
| 7 | * |
| 8 | * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com) |
| 9 | * Copyright (c) 2006 Fabrice Bellard |
| 10 | * |
| 11 | * This library is free software; you can redistribute it and/or |
| 12 | * modify it under the terms of the GNU Lesser General Public |
Chetan Pant | 61f3c91 | 2020-10-23 12:44:24 +0000 | [diff] [blame] | 13 | * License version 2.1 as published by the Free Software Foundation. |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 14 | * |
| 15 | * This library is distributed in the hope that it will be useful, |
| 16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 | * Lesser General Public License for more details. |
| 19 | * |
| 20 | * You should have received a copy of the GNU Lesser General Public |
| 21 | * License along with this library; if not, see <http://www.gnu.org/licenses/> |
| 22 | * |
| 23 | * Contributions after 2012-01-13 are licensed under the terms of the |
| 24 | * GNU GPL, version 2 or (at your option) any later version. |
| 25 | */ |
| 26 | |
Peter Maydell | b6a0aa0 | 2016-01-26 18:17:03 +0000 | [diff] [blame] | 27 | #include "qemu/osdep.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 28 | #include "hw/acpi/pcihp.h" |
| 29 | |
Philippe Mathieu-Daudé | 0fd61a2 | 2019-02-02 20:57:47 +0100 | [diff] [blame] | 30 | #include "hw/pci-host/i440fx.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 31 | #include "hw/pci/pci.h" |
David Hildenbrand | 3e52092 | 2018-12-12 10:16:18 +0100 | [diff] [blame] | 32 | #include "hw/pci/pci_bridge.h" |
Julia Suvorova | c0e427d | 2021-07-13 02:42:01 +0200 | [diff] [blame] | 33 | #include "hw/pci/pci_host.h" |
Julia Suvorova | 3f3cbbb | 2021-07-13 02:42:02 +0200 | [diff] [blame] | 34 | #include "hw/pci/pcie_port.h" |
Igor Mammedov | 6b0969f | 2022-03-01 10:11:59 -0500 | [diff] [blame] | 35 | #include "hw/pci-bridge/xio3130_downstream.h" |
Julia Suvorova | c0e427d | 2021-07-13 02:42:01 +0200 | [diff] [blame] | 36 | #include "hw/i386/acpi-build.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 37 | #include "hw/acpi/acpi.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 38 | #include "hw/pci/pci_bus.h" |
Markus Armbruster | d645427 | 2019-08-12 07:23:45 +0200 | [diff] [blame] | 39 | #include "migration/vmstate.h" |
Markus Armbruster | da34e65 | 2016-03-14 09:01:28 +0100 | [diff] [blame] | 40 | #include "qapi/error.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 41 | #include "qom/qom-qobject.h" |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 42 | #include "trace.h" |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 43 | |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 44 | #define ACPI_PCIHP_SIZE 0x0018 |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 45 | #define PCI_UP_BASE 0x0000 |
| 46 | #define PCI_DOWN_BASE 0x0004 |
| 47 | #define PCI_EJ_BASE 0x0008 |
| 48 | #define PCI_RMV_BASE 0x000c |
| 49 | #define PCI_SEL_BASE 0x0010 |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 50 | #define PCI_AIDX_BASE 0x0014 |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 51 | |
| 52 | typedef struct AcpiPciHpFind { |
| 53 | int bsel; |
| 54 | PCIBus *bus; |
| 55 | } AcpiPciHpFind; |
| 56 | |
| 57 | static int acpi_pcihp_get_bsel(PCIBus *bus) |
| 58 | { |
Kirill Batuzov | 7c38ecd | 2014-04-24 18:15:56 +0400 | [diff] [blame] | 59 | Error *local_err = NULL; |
Marc-André Lureau | c03d83d | 2017-06-07 20:36:15 +0400 | [diff] [blame] | 60 | uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, |
| 61 | &local_err); |
Kirill Batuzov | 7c38ecd | 2014-04-24 18:15:56 +0400 | [diff] [blame] | 62 | |
Marc-André Lureau | c03d83d | 2017-06-07 20:36:15 +0400 | [diff] [blame] | 63 | if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
Kirill Batuzov | 7c38ecd | 2014-04-24 18:15:56 +0400 | [diff] [blame] | 64 | if (local_err) { |
| 65 | error_free(local_err); |
| 66 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 67 | return -1; |
Kirill Batuzov | 7c38ecd | 2014-04-24 18:15:56 +0400 | [diff] [blame] | 68 | } else { |
| 69 | return bsel; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 70 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 71 | } |
| 72 | |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 73 | typedef struct { |
| 74 | unsigned bsel_alloc; |
| 75 | bool has_bridge_hotplug; |
| 76 | } BSELInfo; |
| 77 | |
| 78 | /* Assign BSEL property only to buses that support hotplug. */ |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 79 | static void *acpi_set_bsel(PCIBus *bus, void *opaque) |
| 80 | { |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 81 | BSELInfo *info = opaque; |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 82 | unsigned *bus_bsel; |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 83 | DeviceState *br = bus->qbus.parent; |
| 84 | bool is_bridge = IS_PCI_BRIDGE(br); |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 85 | |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 86 | /* hotplugged bridges can't be described in ACPI ignore them */ |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 87 | if (qbus_is_hotpluggable(BUS(bus))) { |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 88 | if (!is_bridge || (!br->hotplugged && info->has_bridge_hotplug)) { |
| 89 | bus_bsel = g_malloc(sizeof *bus_bsel); |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 90 | |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 91 | *bus_bsel = info->bsel_alloc++; |
| 92 | object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, |
| 93 | bus_bsel, OBJ_PROP_FLAG_READ); |
| 94 | } |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 95 | } |
| 96 | |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 97 | return info; |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 98 | } |
| 99 | |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 100 | static void acpi_set_pci_info(bool has_bridge_hotplug) |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 101 | { |
| 102 | static bool bsel_is_set; |
Julia Suvorova | c0e427d | 2021-07-13 02:42:01 +0200 | [diff] [blame] | 103 | Object *host = acpi_get_i386_pci_host(); |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 104 | PCIBus *bus; |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 105 | BSELInfo info = { .bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT, |
| 106 | .has_bridge_hotplug = has_bridge_hotplug }; |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 107 | |
| 108 | if (bsel_is_set) { |
| 109 | return; |
| 110 | } |
| 111 | bsel_is_set = true; |
| 112 | |
Julia Suvorova | c0e427d | 2021-07-13 02:42:01 +0200 | [diff] [blame] | 113 | if (!host) { |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | bus = PCI_HOST_BRIDGE(host)->bus; |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 118 | if (bus) { |
| 119 | /* Scan all PCI buses. Set property to enable acpi based hotplug. */ |
Igor Mammedov | 2940a4b | 2023-01-12 15:02:43 +0100 | [diff] [blame] | 120 | pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &info); |
Anthony PERARD | ab938ae | 2017-09-06 14:40:32 +0100 | [diff] [blame] | 121 | } |
| 122 | } |
| 123 | |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 124 | static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) |
| 125 | { |
| 126 | AcpiPciHpFind *find = opaque; |
| 127 | if (find->bsel == acpi_pcihp_get_bsel(bus)) { |
| 128 | find->bus = bus; |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) |
| 133 | { |
| 134 | AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; |
| 135 | |
| 136 | if (bsel < 0) { |
| 137 | return NULL; |
| 138 | } |
| 139 | |
| 140 | pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); |
| 141 | |
| 142 | /* Make bsel 0 eject root bus if bsel property is not set, |
| 143 | * for compatibility with non acpi setups. |
| 144 | * TODO: really needed? |
| 145 | */ |
| 146 | if (!bsel && !find.bus) { |
| 147 | find.bus = s->root; |
| 148 | } |
Ani Sinha | 8ad038a | 2020-09-18 14:11:02 +0530 | [diff] [blame] | 149 | |
| 150 | /* |
| 151 | * Check if find.bus is actually hotpluggable. If bsel is set to |
| 152 | * NULL for example on the root bus in order to make it |
| 153 | * non-hotpluggable, find.bus will match the root bus when bsel |
| 154 | * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the |
| 155 | * bus is not hotpluggable however, we should not select the bus. |
| 156 | * Instead, we should set find.bus to NULL in that case. In the check |
| 157 | * below, we generalize this case for all buses, not just the root bus. |
| 158 | * The callers of this function check for a null return value and |
| 159 | * handle them appropriately. |
| 160 | */ |
| 161 | if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { |
| 162 | find.bus = NULL; |
| 163 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 164 | return find.bus; |
| 165 | } |
| 166 | |
| 167 | static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) |
| 168 | { |
Igor Mammedov | 2897ae0 | 2014-02-05 16:36:48 +0100 | [diff] [blame] | 169 | DeviceClass *dc = DEVICE_GET_CLASS(dev); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 170 | /* |
| 171 | * ACPI doesn't allow hotplug of bridge devices. Don't allow |
| 172 | * hot-unplug of bridge devices unless they were added by hotplug |
| 173 | * (and so, not described by acpi). |
Łukasz Gieryk | 58660bf | 2022-05-09 16:16:20 +0200 | [diff] [blame] | 174 | * |
| 175 | * Don't allow hot-unplug of SR-IOV Virtual Functions, as they |
| 176 | * will be removed implicitly, when Physical Function is unplugged. |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 177 | */ |
Igor Mammedov | ad49427 | 2022-11-29 11:13:41 +0100 | [diff] [blame] | 178 | return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable || |
Łukasz Gieryk | 58660bf | 2022-05-09 16:16:20 +0200 | [diff] [blame] | 179 | pci_is_vf(dev); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) |
| 183 | { |
David Hildenbrand | c97adf3 | 2018-12-12 10:16:19 +0100 | [diff] [blame] | 184 | HotplugHandler *hotplug_ctrl; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 185 | BusChild *kid, *next; |
Stefan Hajnoczi | 786a4ea | 2015-03-23 15:29:26 +0000 | [diff] [blame] | 186 | int slot = ctz32(slots); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 187 | PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); |
| 188 | |
Markus Armbruster | 03459ea | 2019-04-02 18:19:00 +0200 | [diff] [blame] | 189 | trace_acpi_pci_eject_slot(bsel, slot); |
| 190 | |
Igor Mammedov | a3ec4bb | 2020-03-26 09:56:24 -0400 | [diff] [blame] | 191 | if (!bus || slot > 31) { |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 192 | return; |
| 193 | } |
| 194 | |
| 195 | /* Mark request as complete */ |
| 196 | s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); |
Michael S. Tsirkin | 5a2223c | 2014-01-26 12:31:27 +0200 | [diff] [blame] | 197 | s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 198 | |
| 199 | QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { |
| 200 | DeviceState *qdev = kid->child; |
| 201 | PCIDevice *dev = PCI_DEVICE(qdev); |
| 202 | if (PCI_SLOT(dev->devfn) == slot) { |
Michael S. Tsirkin | 5a2223c | 2014-01-26 12:31:27 +0200 | [diff] [blame] | 203 | if (!acpi_pcihp_pc_no_hotplug(s, dev)) { |
Laurent Vivier | 9323f89 | 2021-11-18 14:32:23 +0100 | [diff] [blame] | 204 | /* |
| 205 | * partially_hotplugged is used by virtio-net failover: |
| 206 | * failover has asked the guest OS to unplug the device |
| 207 | * but we need to keep some references to the device |
| 208 | * to be able to plug it back in case of failure so |
| 209 | * we don't execute hotplug_handler_unplug(). |
| 210 | */ |
| 211 | if (dev->partially_hotplugged) { |
| 212 | /* |
| 213 | * pending_deleted_event is set to true when |
| 214 | * virtio-net failover asks to unplug the device, |
| 215 | * and set to false here when the operation is done |
| 216 | * This is used by the migration loop to detect the |
| 217 | * end of the operation and really start the migration. |
| 218 | */ |
| 219 | qdev->pending_deleted_event = false; |
| 220 | } else { |
| 221 | hotplug_ctrl = qdev_get_hotplug_handler(qdev); |
| 222 | hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); |
| 223 | object_unparent(OBJECT(qdev)); |
| 224 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) |
| 231 | { |
| 232 | BusChild *kid, *next; |
| 233 | PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); |
| 234 | |
| 235 | /* Execute any pending removes during reset */ |
| 236 | while (s->acpi_pcihp_pci_status[bsel].down) { |
| 237 | acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); |
| 238 | } |
| 239 | |
| 240 | s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 241 | |
| 242 | if (!bus) { |
| 243 | return; |
| 244 | } |
| 245 | QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { |
| 246 | DeviceState *qdev = kid->child; |
| 247 | PCIDevice *pdev = PCI_DEVICE(qdev); |
| 248 | int slot = PCI_SLOT(pdev->devfn); |
| 249 | |
| 250 | if (acpi_pcihp_pc_no_hotplug(s, pdev)) { |
| 251 | s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); |
| 252 | } |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
| 256 | static void acpi_pcihp_update(AcpiPciHpState *s) |
| 257 | { |
| 258 | int i; |
| 259 | |
| 260 | for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { |
| 261 | acpi_pcihp_update_hotplug_bus(s, i); |
| 262 | } |
| 263 | } |
| 264 | |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 265 | void acpi_pcihp_reset(AcpiPciHpState *s) |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 266 | { |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 267 | acpi_set_pci_info(s->use_acpi_hotplug_bridge); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 268 | acpi_pcihp_update(s); |
| 269 | } |
| 270 | |
David Hildenbrand | ec266f4 | 2018-12-12 10:16:17 +0100 | [diff] [blame] | 271 | void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, |
| 272 | DeviceState *dev, Error **errp) |
| 273 | { |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 274 | PCIDevice *pdev = PCI_DEVICE(dev); |
| 275 | |
David Hildenbrand | ec266f4 | 2018-12-12 10:16:17 +0100 | [diff] [blame] | 276 | /* Only hotplugged devices need the hotplug capability. */ |
| 277 | if (dev->hotplugged && |
Ani Sinha | 028f1a8 | 2021-08-25 08:49:46 +0530 | [diff] [blame] | 278 | acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) { |
David Hildenbrand | ec266f4 | 2018-12-12 10:16:17 +0100 | [diff] [blame] | 279 | error_setg(errp, "Unsupported bus. Bus doesn't have property '" |
| 280 | ACPI_PCIHP_PROP_BSEL "' set"); |
| 281 | return; |
| 282 | } |
| 283 | } |
| 284 | |
Igor Mammedov | 0058c08 | 2016-05-31 12:01:17 +0200 | [diff] [blame] | 285 | void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 286 | DeviceState *dev, Error **errp) |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 287 | { |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 288 | PCIDevice *pdev = PCI_DEVICE(dev); |
| 289 | int slot = PCI_SLOT(pdev->devfn); |
Igor Mammedov | 6b0969f | 2022-03-01 10:11:59 -0500 | [diff] [blame] | 290 | PCIDevice *bridge; |
| 291 | PCIBus *bus; |
David Hildenbrand | ec266f4 | 2018-12-12 10:16:17 +0100 | [diff] [blame] | 292 | int bsel; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 293 | |
| 294 | /* Don't send event when device is enabled during qemu machine creation: |
| 295 | * it is present on boot, no hotplug event is necessary. We do send an |
| 296 | * event when the device is disabled later. */ |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 297 | if (!dev->hotplugged) { |
David Hildenbrand | 3e52092 | 2018-12-12 10:16:18 +0100 | [diff] [blame] | 298 | /* |
| 299 | * Overwrite the default hotplug handler with the ACPI PCI one |
| 300 | * for cold plugged bridges only. |
| 301 | */ |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 302 | if (s->use_acpi_hotplug_bridge && |
David Hildenbrand | 3e52092 | 2018-12-12 10:16:18 +0100 | [diff] [blame] | 303 | object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { |
| 304 | PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); |
| 305 | |
Markus Armbruster | 9bc6bfd | 2020-06-30 11:03:39 +0200 | [diff] [blame] | 306 | qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); |
David Hildenbrand | 3e52092 | 2018-12-12 10:16:18 +0100 | [diff] [blame] | 307 | /* We don't have to overwrite any other hotplug handler yet */ |
| 308 | assert(QLIST_EMPTY(&sec->child)); |
| 309 | } |
| 310 | |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 311 | return; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 312 | } |
| 313 | |
Igor Mammedov | 6b0969f | 2022-03-01 10:11:59 -0500 | [diff] [blame] | 314 | bus = pci_get_bus(pdev); |
| 315 | bridge = pci_bridge_get_device(bus); |
| 316 | if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) || |
| 317 | object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) { |
| 318 | pcie_cap_slot_enable_power(bridge); |
| 319 | } |
| 320 | |
| 321 | bsel = acpi_pcihp_get_bsel(bus); |
David Hildenbrand | ec266f4 | 2018-12-12 10:16:17 +0100 | [diff] [blame] | 322 | g_assert(bsel >= 0); |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 323 | s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); |
Igor Mammedov | 0058c08 | 2016-05-31 12:01:17 +0200 | [diff] [blame] | 324 | acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 325 | } |
| 326 | |
Igor Mammedov | 0058c08 | 2016-05-31 12:01:17 +0200 | [diff] [blame] | 327 | void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 328 | DeviceState *dev, Error **errp) |
| 329 | { |
Igor Mammedov | 4fd7da4 | 2021-03-15 14:00:59 -0400 | [diff] [blame] | 330 | PCIDevice *pdev = PCI_DEVICE(dev); |
| 331 | |
Ani Sinha | 028f1a8 | 2021-08-25 08:49:46 +0530 | [diff] [blame] | 332 | trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn), |
| 333 | acpi_pcihp_get_bsel(pci_get_bus(pdev))); |
Igor Mammedov | 4fd7da4 | 2021-03-15 14:00:59 -0400 | [diff] [blame] | 334 | |
Markus Armbruster | 981c3dc | 2020-06-10 07:31:56 +0200 | [diff] [blame] | 335 | qdev_unrealize(dev); |
David Hildenbrand | c97adf3 | 2018-12-12 10:16:19 +0100 | [diff] [blame] | 336 | } |
| 337 | |
| 338 | void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, |
| 339 | AcpiPciHpState *s, DeviceState *dev, |
| 340 | Error **errp) |
| 341 | { |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 342 | PCIDevice *pdev = PCI_DEVICE(dev); |
| 343 | int slot = PCI_SLOT(pdev->devfn); |
David Gibson | fd56e06 | 2017-11-29 19:46:27 +1100 | [diff] [blame] | 344 | int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); |
Markus Armbruster | 03459ea | 2019-04-02 18:19:00 +0200 | [diff] [blame] | 345 | |
| 346 | trace_acpi_pci_unplug_request(bsel, slot); |
| 347 | |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 348 | if (bsel < 0) { |
| 349 | error_setg(errp, "Unsupported bus. Bus doesn't have property '" |
| 350 | ACPI_PCIHP_PROP_BSEL "' set"); |
| 351 | return; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 352 | } |
| 353 | |
Laurent Vivier | 9323f89 | 2021-11-18 14:32:23 +0100 | [diff] [blame] | 354 | /* |
| 355 | * pending_deleted_event is used by virtio-net failover to detect the |
| 356 | * end of the unplug operation, the flag is set to false in |
| 357 | * acpi_pcihp_eject_slot() when the operation is completed. |
| 358 | */ |
| 359 | pdev->qdev.pending_deleted_event = true; |
Igor Mammedov | 0f689cf | 2023-04-18 11:04:49 +0200 | [diff] [blame] | 360 | /* if unplug was requested before OSPM is initialized, |
| 361 | * linux kernel will clear GPE0.sts[] bits during boot, which effectively |
| 362 | * hides unplug event. And than followup qmp_device_del() calls remain |
| 363 | * blocked by above flag permanently. |
| 364 | * Unblock qmp_device_del() by setting expire limit, so user can |
| 365 | * repeat unplug request later when OSPM has been booted. |
| 366 | */ |
| 367 | pdev->qdev.pending_deleted_expires_ms = |
| 368 | qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); /* 1 msec */ |
| 369 | |
Igor Mammedov | c24d5e0 | 2014-02-05 16:36:49 +0100 | [diff] [blame] | 370 | s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); |
Igor Mammedov | 0058c08 | 2016-05-31 12:01:17 +0200 | [diff] [blame] | 371 | acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 372 | } |
| 373 | |
Igor Mammedov | f18e29f | 2023-03-02 17:15:43 +0100 | [diff] [blame] | 374 | bool acpi_pcihp_is_hotpluggbale_bus(AcpiPciHpState *s, BusState *bus) |
| 375 | { |
| 376 | Object *o = OBJECT(bus->parent); |
| 377 | |
| 378 | if (s->use_acpi_hotplug_bridge && |
| 379 | object_dynamic_cast(o, TYPE_PCI_BRIDGE)) { |
| 380 | if (object_dynamic_cast(o, TYPE_PCIE_SLOT) && !PCIE_SLOT(o)->hotplug) { |
| 381 | return false; |
| 382 | } |
| 383 | return true; |
| 384 | } |
| 385 | |
| 386 | if (s->use_acpi_root_pci_hotplug) { |
| 387 | return true; |
| 388 | } |
| 389 | return false; |
| 390 | } |
| 391 | |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 392 | static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) |
| 393 | { |
| 394 | AcpiPciHpState *s = opaque; |
| 395 | uint32_t val = 0; |
| 396 | int bsel = s->hotplug_select; |
| 397 | |
Gonglei | fa365d7 | 2014-08-20 13:52:30 +0800 | [diff] [blame] | 398 | if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 399 | return 0; |
| 400 | } |
| 401 | |
| 402 | switch (addr) { |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 403 | case PCI_UP_BASE: |
Michael S. Tsirkin | 5a2223c | 2014-01-26 12:31:27 +0200 | [diff] [blame] | 404 | val = s->acpi_pcihp_pci_status[bsel].up; |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 405 | if (s->use_acpi_hotplug_bridge) { |
Igor Mammedov | 99d09dd | 2014-02-03 11:44:59 +0100 | [diff] [blame] | 406 | s->acpi_pcihp_pci_status[bsel].up = 0; |
| 407 | } |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 408 | trace_acpi_pci_up_read(val); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 409 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 410 | case PCI_DOWN_BASE: |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 411 | val = s->acpi_pcihp_pci_status[bsel].down; |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 412 | trace_acpi_pci_down_read(val); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 413 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 414 | case PCI_EJ_BASE: |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 415 | trace_acpi_pci_features_read(val); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 416 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 417 | case PCI_RMV_BASE: |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 418 | val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 419 | trace_acpi_pci_rmv_read(val); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 420 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 421 | case PCI_SEL_BASE: |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 422 | val = s->hotplug_select; |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 423 | trace_acpi_pci_sel_read(val); |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 424 | break; |
| 425 | case PCI_AIDX_BASE: |
| 426 | val = s->acpi_index; |
| 427 | s->acpi_index = 0; |
| 428 | trace_acpi_pci_acpi_index_read(val); |
| 429 | break; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 430 | default: |
| 431 | break; |
| 432 | } |
| 433 | |
| 434 | return val; |
| 435 | } |
| 436 | |
| 437 | static void pci_write(void *opaque, hwaddr addr, uint64_t data, |
| 438 | unsigned int size) |
| 439 | { |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 440 | int slot; |
| 441 | PCIBus *bus; |
| 442 | BusChild *kid, *next; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 443 | AcpiPciHpState *s = opaque; |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 444 | |
| 445 | s->acpi_index = 0; |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 446 | switch (addr) { |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 447 | case PCI_AIDX_BASE: |
| 448 | /* |
| 449 | * fetch acpi-index for specified slot so that follow up read from |
| 450 | * PCI_AIDX_BASE can return it to guest |
| 451 | */ |
| 452 | slot = ctz32(data); |
| 453 | |
| 454 | if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
| 455 | break; |
| 456 | } |
| 457 | |
| 458 | bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); |
Michael S. Tsirkin | 9bd6565 | 2021-12-21 09:45:44 -0500 | [diff] [blame] | 459 | if (!bus) { |
| 460 | break; |
| 461 | } |
Igor Mammedov | b32bd76 | 2021-03-15 14:00:58 -0400 | [diff] [blame] | 462 | QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { |
| 463 | Object *o = OBJECT(kid->child); |
| 464 | PCIDevice *dev = PCI_DEVICE(o); |
| 465 | if (PCI_SLOT(dev->devfn) == slot) { |
| 466 | s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); |
| 471 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 472 | case PCI_EJ_BASE: |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 473 | if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { |
| 474 | break; |
| 475 | } |
| 476 | acpi_pcihp_eject_slot(s, s->hotplug_select, data); |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 477 | trace_acpi_pci_ej_write(addr, data); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 478 | break; |
Igor Mammedov | a7b613c | 2014-02-03 11:44:58 +0100 | [diff] [blame] | 479 | case PCI_SEL_BASE: |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 480 | s->hotplug_select = s->use_acpi_hotplug_bridge ? data : |
| 481 | ACPI_PCIHP_BSEL_DEFAULT; |
Markus Armbruster | df93b19 | 2019-04-02 18:18:59 +0200 | [diff] [blame] | 482 | trace_acpi_pci_sel_write(addr, data); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 483 | default: |
| 484 | break; |
| 485 | } |
| 486 | } |
| 487 | |
| 488 | static const MemoryRegionOps acpi_pcihp_io_ops = { |
| 489 | .read = pci_read, |
| 490 | .write = pci_write, |
| 491 | .endianness = DEVICE_LITTLE_ENDIAN, |
| 492 | .valid = { |
| 493 | .min_access_size = 4, |
| 494 | .max_access_size = 4, |
| 495 | }, |
| 496 | }; |
| 497 | |
Igor Mammedov | 78c2d87 | 2015-02-18 19:14:49 +0000 | [diff] [blame] | 498 | void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, |
Igor Mammedov | 6536e42 | 2023-03-02 17:15:42 +0100 | [diff] [blame] | 499 | MemoryRegion *address_space_io, |
Julia Suvorova | caf108b | 2021-07-13 02:42:00 +0200 | [diff] [blame] | 500 | uint16_t io_base) |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 501 | { |
Igor Mammedov | 78c2d87 | 2015-02-18 19:14:49 +0000 | [diff] [blame] | 502 | s->io_len = ACPI_PCIHP_SIZE; |
Julia Suvorova | caf108b | 2021-07-13 02:42:00 +0200 | [diff] [blame] | 503 | s->io_base = io_base; |
Igor Mammedov | e358edc | 2014-02-03 11:45:01 +0100 | [diff] [blame] | 504 | |
Xinhao Zhang | 510feed | 2020-11-03 18:26:34 +0800 | [diff] [blame] | 505 | s->root = root_bus; |
Igor Mammedov | e358edc | 2014-02-03 11:45:01 +0100 | [diff] [blame] | 506 | |
Igor Mammedov | 78c2d87 | 2015-02-18 19:14:49 +0000 | [diff] [blame] | 507 | memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, |
| 508 | "acpi-pci-hotplug", s->io_len); |
| 509 | memory_region_add_subregion(address_space_io, s->io_base, &s->io); |
| 510 | |
| 511 | object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 512 | OBJ_PROP_FLAG_READ); |
Igor Mammedov | 78c2d87 | 2015-02-18 19:14:49 +0000 | [diff] [blame] | 513 | object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, |
Markus Armbruster | d262312 | 2020-05-05 17:29:22 +0200 | [diff] [blame] | 514 | OBJ_PROP_FLAG_READ); |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | const VMStateDescription vmstate_acpi_pcihp_pci_status = { |
| 518 | .name = "acpi_pcihp_pci_status", |
| 519 | .version_id = 1, |
| 520 | .minimum_version_id = 1, |
Juan Quintela | d49805a | 2014-04-16 15:32:32 +0200 | [diff] [blame] | 521 | .fields = (VMStateField[]) { |
Michael S. Tsirkin | db4728e | 2013-10-14 18:01:11 +0300 | [diff] [blame] | 522 | VMSTATE_UINT32(up, AcpiPciHpPciStatus), |
| 523 | VMSTATE_UINT32(down, AcpiPciHpPciStatus), |
| 524 | VMSTATE_END_OF_LIST() |
| 525 | } |
| 526 | }; |