1db4728e6SMichael S. Tsirkin /* 2db4728e6SMichael S. Tsirkin * QEMU<->ACPI BIOS PCI hotplug interface 3db4728e6SMichael S. Tsirkin * 4db4728e6SMichael S. Tsirkin * QEMU supports PCI hotplug via ACPI. This module 5db4728e6SMichael S. Tsirkin * implements the interface between QEMU and the ACPI BIOS. 6db4728e6SMichael S. Tsirkin * Interface specification - see docs/specs/acpi_pci_hotplug.txt 7db4728e6SMichael S. Tsirkin * 8db4728e6SMichael S. Tsirkin * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com) 9db4728e6SMichael S. Tsirkin * Copyright (c) 2006 Fabrice Bellard 10db4728e6SMichael S. Tsirkin * 11db4728e6SMichael S. Tsirkin * This library is free software; you can redistribute it and/or 12db4728e6SMichael S. Tsirkin * modify it under the terms of the GNU Lesser General Public 1361f3c91aSChetan Pant * License version 2.1 as published by the Free Software Foundation. 14db4728e6SMichael S. Tsirkin * 15db4728e6SMichael S. Tsirkin * This library is distributed in the hope that it will be useful, 16db4728e6SMichael S. Tsirkin * but WITHOUT ANY WARRANTY; without even the implied warranty of 17db4728e6SMichael S. Tsirkin * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 18db4728e6SMichael S. Tsirkin * Lesser General Public License for more details. 19db4728e6SMichael S. Tsirkin * 20db4728e6SMichael S. Tsirkin * You should have received a copy of the GNU Lesser General Public 21db4728e6SMichael S. Tsirkin * License along with this library; if not, see <http://www.gnu.org/licenses/> 22db4728e6SMichael S. Tsirkin * 23db4728e6SMichael S. Tsirkin * Contributions after 2012-01-13 are licensed under the terms of the 24db4728e6SMichael S. Tsirkin * GNU GPL, version 2 or (at your option) any later version. 25db4728e6SMichael S. Tsirkin */ 26db4728e6SMichael S. Tsirkin 27b6a0aa05SPeter Maydell #include "qemu/osdep.h" 28db4728e6SMichael S. Tsirkin #include "hw/acpi/pcihp.h" 29db4728e6SMichael S. Tsirkin 300fd61a2dSPhilippe Mathieu-Daudé #include "hw/pci-host/i440fx.h" 31db4728e6SMichael S. Tsirkin #include "hw/pci/pci.h" 323e520926SDavid Hildenbrand #include "hw/pci/pci_bridge.h" 33c0e427d6SJulia Suvorova #include "hw/pci/pci_host.h" 343f3cbbb2SJulia Suvorova #include "hw/pci/pcie_port.h" 356b0969f1SIgor Mammedov #include "hw/pci-bridge/xio3130_downstream.h" 36c0e427d6SJulia Suvorova #include "hw/i386/acpi-build.h" 37db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h" 38db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h" 39d6454270SMarkus Armbruster #include "migration/vmstate.h" 40da34e65cSMarkus Armbruster #include "qapi/error.h" 41db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h" 42df93b194SMarkus Armbruster #include "trace.h" 43db4728e6SMichael S. Tsirkin 44b32bd763SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0018 45a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000 46a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004 47a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008 48a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c 49a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010 50b32bd763SIgor Mammedov #define PCI_AIDX_BASE 0x0014 51db4728e6SMichael S. Tsirkin 52db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind { 53db4728e6SMichael S. Tsirkin int bsel; 54db4728e6SMichael S. Tsirkin PCIBus *bus; 55db4728e6SMichael S. Tsirkin } AcpiPciHpFind; 56db4728e6SMichael S. Tsirkin 57db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus) 58db4728e6SMichael S. Tsirkin { 597c38ecd0SKirill Batuzov Error *local_err = NULL; 60c03d83d5SMarc-André Lureau uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 617c38ecd0SKirill Batuzov &local_err); 627c38ecd0SKirill Batuzov 63c03d83d5SMarc-André Lureau if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 647c38ecd0SKirill Batuzov if (local_err) { 657c38ecd0SKirill Batuzov error_free(local_err); 66db4728e6SMichael S. Tsirkin } 67db4728e6SMichael S. Tsirkin return -1; 687c38ecd0SKirill Batuzov } else { 69db4728e6SMichael S. Tsirkin return bsel; 70db4728e6SMichael S. Tsirkin } 717c38ecd0SKirill Batuzov } 72db4728e6SMichael S. Tsirkin 732940a4b9SIgor Mammedov typedef struct { 742940a4b9SIgor Mammedov unsigned bsel_alloc; 752940a4b9SIgor Mammedov bool has_bridge_hotplug; 762940a4b9SIgor Mammedov } BSELInfo; 772940a4b9SIgor Mammedov 782940a4b9SIgor Mammedov /* Assign BSEL property only to buses that support hotplug. */ 79ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque) 80ab938ae4SAnthony PERARD { 812940a4b9SIgor Mammedov BSELInfo *info = opaque; 82ab938ae4SAnthony PERARD unsigned *bus_bsel; 832940a4b9SIgor Mammedov DeviceState *br = bus->qbus.parent; 842940a4b9SIgor Mammedov bool is_bridge = IS_PCI_BRIDGE(br); 85ab938ae4SAnthony PERARD 862940a4b9SIgor Mammedov /* hotplugged bridges can't be described in ACPI ignore them */ 87ab938ae4SAnthony PERARD if (qbus_is_hotpluggable(BUS(bus))) { 882940a4b9SIgor Mammedov if (!is_bridge || (!br->hotplugged && info->has_bridge_hotplug)) { 89ab938ae4SAnthony PERARD bus_bsel = g_malloc(sizeof *bus_bsel); 90ab938ae4SAnthony PERARD 912940a4b9SIgor Mammedov *bus_bsel = info->bsel_alloc++; 92ab938ae4SAnthony PERARD object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 93d2623129SMarkus Armbruster bus_bsel, OBJ_PROP_FLAG_READ); 94ab938ae4SAnthony PERARD } 95ab938ae4SAnthony PERARD } 96ab938ae4SAnthony PERARD 972940a4b9SIgor Mammedov return info; 982940a4b9SIgor Mammedov } 992940a4b9SIgor Mammedov 1002940a4b9SIgor Mammedov static void acpi_set_pci_info(bool has_bridge_hotplug) 101ab938ae4SAnthony PERARD { 102ab938ae4SAnthony PERARD static bool bsel_is_set; 103c0e427d6SJulia Suvorova Object *host = acpi_get_i386_pci_host(); 104ab938ae4SAnthony PERARD PCIBus *bus; 1052940a4b9SIgor Mammedov BSELInfo info = { .bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT, 1062940a4b9SIgor Mammedov .has_bridge_hotplug = has_bridge_hotplug }; 107ab938ae4SAnthony PERARD 108ab938ae4SAnthony PERARD if (bsel_is_set) { 109ab938ae4SAnthony PERARD return; 110ab938ae4SAnthony PERARD } 111ab938ae4SAnthony PERARD bsel_is_set = true; 112ab938ae4SAnthony PERARD 113c0e427d6SJulia Suvorova if (!host) { 114c0e427d6SJulia Suvorova return; 115c0e427d6SJulia Suvorova } 116c0e427d6SJulia Suvorova 117c0e427d6SJulia Suvorova bus = PCI_HOST_BRIDGE(host)->bus; 118ab938ae4SAnthony PERARD if (bus) { 119ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 1202940a4b9SIgor Mammedov pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &info); 121ab938ae4SAnthony PERARD } 122ab938ae4SAnthony PERARD } 123ab938ae4SAnthony PERARD 124db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 125db4728e6SMichael S. Tsirkin { 126db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 127db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 128db4728e6SMichael S. Tsirkin find->bus = bus; 129db4728e6SMichael S. Tsirkin } 130db4728e6SMichael S. Tsirkin } 131db4728e6SMichael S. Tsirkin 132db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 133db4728e6SMichael S. Tsirkin { 134db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 135db4728e6SMichael S. Tsirkin 136db4728e6SMichael S. Tsirkin if (bsel < 0) { 137db4728e6SMichael S. Tsirkin return NULL; 138db4728e6SMichael S. Tsirkin } 139db4728e6SMichael S. Tsirkin 140db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 141db4728e6SMichael S. Tsirkin 142db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 143db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 144db4728e6SMichael S. Tsirkin * TODO: really needed? 145db4728e6SMichael S. Tsirkin */ 146db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 147db4728e6SMichael S. Tsirkin find.bus = s->root; 148db4728e6SMichael S. Tsirkin } 1498ad038abSAni Sinha 1508ad038abSAni Sinha /* 1518ad038abSAni Sinha * Check if find.bus is actually hotpluggable. If bsel is set to 1528ad038abSAni Sinha * NULL for example on the root bus in order to make it 1538ad038abSAni Sinha * non-hotpluggable, find.bus will match the root bus when bsel 1548ad038abSAni Sinha * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the 1558ad038abSAni Sinha * bus is not hotpluggable however, we should not select the bus. 1568ad038abSAni Sinha * Instead, we should set find.bus to NULL in that case. In the check 1578ad038abSAni Sinha * below, we generalize this case for all buses, not just the root bus. 1588ad038abSAni Sinha * The callers of this function check for a null return value and 1598ad038abSAni Sinha * handle them appropriately. 1608ad038abSAni Sinha */ 1618ad038abSAni Sinha if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { 1628ad038abSAni Sinha find.bus = NULL; 1638ad038abSAni Sinha } 164db4728e6SMichael S. Tsirkin return find.bus; 165db4728e6SMichael S. Tsirkin } 166db4728e6SMichael S. Tsirkin 167db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 168db4728e6SMichael S. Tsirkin { 1692897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 170db4728e6SMichael S. Tsirkin /* 171db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 172db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 173db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 17458660bfaSŁukasz Gieryk * 17558660bfaSŁukasz Gieryk * Don't allow hot-unplug of SR-IOV Virtual Functions, as they 17658660bfaSŁukasz Gieryk * will be removed implicitly, when Physical Function is unplugged. 177db4728e6SMichael S. Tsirkin */ 178ad494274SIgor Mammedov return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable || 17958660bfaSŁukasz Gieryk pci_is_vf(dev); 180db4728e6SMichael S. Tsirkin } 181db4728e6SMichael S. Tsirkin 182db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 183db4728e6SMichael S. Tsirkin { 184c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 185db4728e6SMichael S. Tsirkin BusChild *kid, *next; 186786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 187db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 188db4728e6SMichael S. Tsirkin 18903459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 19003459ea3SMarkus Armbruster 191a3ec4bb7SIgor Mammedov if (!bus || slot > 31) { 192db4728e6SMichael S. Tsirkin return; 193db4728e6SMichael S. Tsirkin } 194db4728e6SMichael S. Tsirkin 195db4728e6SMichael S. Tsirkin /* Mark request as complete */ 196db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 1975a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 198db4728e6SMichael S. Tsirkin 199db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 200db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 201db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 202db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 2035a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 2049323f892SLaurent Vivier /* 2059323f892SLaurent Vivier * partially_hotplugged is used by virtio-net failover: 2069323f892SLaurent Vivier * failover has asked the guest OS to unplug the device 2079323f892SLaurent Vivier * but we need to keep some references to the device 2089323f892SLaurent Vivier * to be able to plug it back in case of failure so 2099323f892SLaurent Vivier * we don't execute hotplug_handler_unplug(). 2109323f892SLaurent Vivier */ 2119323f892SLaurent Vivier if (dev->partially_hotplugged) { 2129323f892SLaurent Vivier /* 2139323f892SLaurent Vivier * pending_deleted_event is set to true when 2149323f892SLaurent Vivier * virtio-net failover asks to unplug the device, 2159323f892SLaurent Vivier * and set to false here when the operation is done 2169323f892SLaurent Vivier * This is used by the migration loop to detect the 2179323f892SLaurent Vivier * end of the operation and really start the migration. 2189323f892SLaurent Vivier */ 2199323f892SLaurent Vivier qdev->pending_deleted_event = false; 2209323f892SLaurent Vivier } else { 221c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 222c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 22307578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 224db4728e6SMichael S. Tsirkin } 225db4728e6SMichael S. Tsirkin } 226db4728e6SMichael S. Tsirkin } 227db4728e6SMichael S. Tsirkin } 2289323f892SLaurent Vivier } 229db4728e6SMichael S. Tsirkin 230db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 231db4728e6SMichael S. Tsirkin { 232db4728e6SMichael S. Tsirkin BusChild *kid, *next; 233db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 234db4728e6SMichael S. Tsirkin 235db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 236db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 237db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 238db4728e6SMichael S. Tsirkin } 239db4728e6SMichael S. Tsirkin 240db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 241db4728e6SMichael S. Tsirkin 242db4728e6SMichael S. Tsirkin if (!bus) { 243db4728e6SMichael S. Tsirkin return; 244db4728e6SMichael S. Tsirkin } 245db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 246db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 247db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 248db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 249db4728e6SMichael S. Tsirkin 250db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 251db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 252db4728e6SMichael S. Tsirkin } 253db4728e6SMichael S. Tsirkin } 254db4728e6SMichael S. Tsirkin } 255db4728e6SMichael S. Tsirkin 256db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 257db4728e6SMichael S. Tsirkin { 258db4728e6SMichael S. Tsirkin int i; 259db4728e6SMichael S. Tsirkin 260db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 261db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 262db4728e6SMichael S. Tsirkin } 263db4728e6SMichael S. Tsirkin } 264db4728e6SMichael S. Tsirkin 2656536e427SIgor Mammedov void acpi_pcihp_reset(AcpiPciHpState *s) 266db4728e6SMichael S. Tsirkin { 2676536e427SIgor Mammedov acpi_set_pci_info(s->use_acpi_hotplug_bridge); 268db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 269db4728e6SMichael S. Tsirkin } 270db4728e6SMichael S. Tsirkin 271ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 272ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 273ec266f40SDavid Hildenbrand { 274b32bd763SIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 275b32bd763SIgor Mammedov 276ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 277ec266f40SDavid Hildenbrand if (dev->hotplugged && 278028f1a88SAni Sinha acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) { 279ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 280ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 281ec266f40SDavid Hildenbrand return; 282ec266f40SDavid Hildenbrand } 283ec266f40SDavid Hildenbrand } 284ec266f40SDavid Hildenbrand 2850058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 286c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 287db4728e6SMichael S. Tsirkin { 288c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 289c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 2906b0969f1SIgor Mammedov PCIDevice *bridge; 2916b0969f1SIgor Mammedov PCIBus *bus; 292ec266f40SDavid Hildenbrand int bsel; 293db4728e6SMichael S. Tsirkin 294db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 295db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 296db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 297c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 2983e520926SDavid Hildenbrand /* 2993e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 3003e520926SDavid Hildenbrand * for cold plugged bridges only. 3013e520926SDavid Hildenbrand */ 3026536e427SIgor Mammedov if (s->use_acpi_hotplug_bridge && 3033e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 3043e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 3053e520926SDavid Hildenbrand 3069bc6bfdfSMarkus Armbruster qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); 3073e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 3083e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 3093e520926SDavid Hildenbrand } 3103e520926SDavid Hildenbrand 311c24d5e0bSIgor Mammedov return; 312db4728e6SMichael S. Tsirkin } 313db4728e6SMichael S. Tsirkin 3146b0969f1SIgor Mammedov bus = pci_get_bus(pdev); 3156b0969f1SIgor Mammedov bridge = pci_bridge_get_device(bus); 3166b0969f1SIgor Mammedov if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) || 3176b0969f1SIgor Mammedov object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) { 3186b0969f1SIgor Mammedov pcie_cap_slot_enable_power(bridge); 3196b0969f1SIgor Mammedov } 3206b0969f1SIgor Mammedov 3216b0969f1SIgor Mammedov bsel = acpi_pcihp_get_bsel(bus); 322ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 3238f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 3240058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 325db4728e6SMichael S. Tsirkin } 326db4728e6SMichael S. Tsirkin 3270058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 328c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 329c24d5e0bSIgor Mammedov { 3304fd7da4cSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 3314fd7da4cSIgor Mammedov 332028f1a88SAni Sinha trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn), 333028f1a88SAni Sinha acpi_pcihp_get_bsel(pci_get_bus(pdev))); 3344fd7da4cSIgor Mammedov 335981c3dcdSMarkus Armbruster qdev_unrealize(dev); 336c97adf3cSDavid Hildenbrand } 337c97adf3cSDavid Hildenbrand 338c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 339c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 340c97adf3cSDavid Hildenbrand Error **errp) 341c97adf3cSDavid Hildenbrand { 342c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 343c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 344fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 34503459ea3SMarkus Armbruster 34603459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 34703459ea3SMarkus Armbruster 348c24d5e0bSIgor Mammedov if (bsel < 0) { 349c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 350c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 351c24d5e0bSIgor Mammedov return; 352c24d5e0bSIgor Mammedov } 353c24d5e0bSIgor Mammedov 3549323f892SLaurent Vivier /* 3559323f892SLaurent Vivier * pending_deleted_event is used by virtio-net failover to detect the 3569323f892SLaurent Vivier * end of the unplug operation, the flag is set to false in 3579323f892SLaurent Vivier * acpi_pcihp_eject_slot() when the operation is completed. 3589323f892SLaurent Vivier */ 3599323f892SLaurent Vivier pdev->qdev.pending_deleted_event = true; 3600f689cf5SIgor Mammedov /* if unplug was requested before OSPM is initialized, 3610f689cf5SIgor Mammedov * linux kernel will clear GPE0.sts[] bits during boot, which effectively 3620f689cf5SIgor Mammedov * hides unplug event. And than followup qmp_device_del() calls remain 3630f689cf5SIgor Mammedov * blocked by above flag permanently. 3640f689cf5SIgor Mammedov * Unblock qmp_device_del() by setting expire limit, so user can 3650f689cf5SIgor Mammedov * repeat unplug request later when OSPM has been booted. 3660f689cf5SIgor Mammedov */ 3670f689cf5SIgor Mammedov pdev->qdev.pending_deleted_expires_ms = 3680f689cf5SIgor Mammedov qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); /* 1 msec */ 3690f689cf5SIgor Mammedov 370c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 3710058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 372db4728e6SMichael S. Tsirkin } 373db4728e6SMichael S. Tsirkin 374f18e29fcSIgor Mammedov bool acpi_pcihp_is_hotpluggbale_bus(AcpiPciHpState *s, BusState *bus) 375f18e29fcSIgor Mammedov { 376f18e29fcSIgor Mammedov Object *o = OBJECT(bus->parent); 377f18e29fcSIgor Mammedov 378f18e29fcSIgor Mammedov if (s->use_acpi_hotplug_bridge && 379f18e29fcSIgor Mammedov object_dynamic_cast(o, TYPE_PCI_BRIDGE)) { 380f18e29fcSIgor Mammedov if (object_dynamic_cast(o, TYPE_PCIE_SLOT) && !PCIE_SLOT(o)->hotplug) { 381f18e29fcSIgor Mammedov return false; 382f18e29fcSIgor Mammedov } 383f18e29fcSIgor Mammedov return true; 384f18e29fcSIgor Mammedov } 385f18e29fcSIgor Mammedov 386f18e29fcSIgor Mammedov if (s->use_acpi_root_pci_hotplug) { 387f18e29fcSIgor Mammedov return true; 388f18e29fcSIgor Mammedov } 389f18e29fcSIgor Mammedov return false; 390f18e29fcSIgor Mammedov } 391f18e29fcSIgor Mammedov 392db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 393db4728e6SMichael S. Tsirkin { 394db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 395db4728e6SMichael S. Tsirkin uint32_t val = 0; 396db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 397db4728e6SMichael S. Tsirkin 398fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 399db4728e6SMichael S. Tsirkin return 0; 400db4728e6SMichael S. Tsirkin } 401db4728e6SMichael S. Tsirkin 402db4728e6SMichael S. Tsirkin switch (addr) { 403a7b613cfSIgor Mammedov case PCI_UP_BASE: 4045a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 4056536e427SIgor Mammedov if (s->use_acpi_hotplug_bridge) { 4065a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 40799d09dd3SIgor Mammedov } 408df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 409db4728e6SMichael S. Tsirkin break; 410a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 411db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 412df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 413db4728e6SMichael S. Tsirkin break; 414a7b613cfSIgor Mammedov case PCI_EJ_BASE: 415df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 416db4728e6SMichael S. Tsirkin break; 417a7b613cfSIgor Mammedov case PCI_RMV_BASE: 418db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 419df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 420db4728e6SMichael S. Tsirkin break; 421a7b613cfSIgor Mammedov case PCI_SEL_BASE: 422db4728e6SMichael S. Tsirkin val = s->hotplug_select; 423df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 424b32bd763SIgor Mammedov break; 425b32bd763SIgor Mammedov case PCI_AIDX_BASE: 426b32bd763SIgor Mammedov val = s->acpi_index; 427b32bd763SIgor Mammedov s->acpi_index = 0; 428b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_read(val); 429b32bd763SIgor Mammedov break; 430db4728e6SMichael S. Tsirkin default: 431db4728e6SMichael S. Tsirkin break; 432db4728e6SMichael S. Tsirkin } 433db4728e6SMichael S. Tsirkin 434db4728e6SMichael S. Tsirkin return val; 435db4728e6SMichael S. Tsirkin } 436db4728e6SMichael S. Tsirkin 437db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 438db4728e6SMichael S. Tsirkin unsigned int size) 439db4728e6SMichael S. Tsirkin { 440b32bd763SIgor Mammedov int slot; 441b32bd763SIgor Mammedov PCIBus *bus; 442b32bd763SIgor Mammedov BusChild *kid, *next; 443db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 444b32bd763SIgor Mammedov 445b32bd763SIgor Mammedov s->acpi_index = 0; 446db4728e6SMichael S. Tsirkin switch (addr) { 447b32bd763SIgor Mammedov case PCI_AIDX_BASE: 448b32bd763SIgor Mammedov /* 449b32bd763SIgor Mammedov * fetch acpi-index for specified slot so that follow up read from 450b32bd763SIgor Mammedov * PCI_AIDX_BASE can return it to guest 451b32bd763SIgor Mammedov */ 452b32bd763SIgor Mammedov slot = ctz32(data); 453b32bd763SIgor Mammedov 454b32bd763SIgor Mammedov if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 455b32bd763SIgor Mammedov break; 456b32bd763SIgor Mammedov } 457b32bd763SIgor Mammedov 458b32bd763SIgor Mammedov bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); 4599bd6565cSMichael S. Tsirkin if (!bus) { 4609bd6565cSMichael S. Tsirkin break; 4619bd6565cSMichael S. Tsirkin } 462b32bd763SIgor Mammedov QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 463b32bd763SIgor Mammedov Object *o = OBJECT(kid->child); 464b32bd763SIgor Mammedov PCIDevice *dev = PCI_DEVICE(o); 465b32bd763SIgor Mammedov if (PCI_SLOT(dev->devfn) == slot) { 466b32bd763SIgor Mammedov s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); 467b32bd763SIgor Mammedov break; 468b32bd763SIgor Mammedov } 469b32bd763SIgor Mammedov } 470b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); 471b32bd763SIgor Mammedov break; 472a7b613cfSIgor Mammedov case PCI_EJ_BASE: 473db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 474db4728e6SMichael S. Tsirkin break; 475db4728e6SMichael S. Tsirkin } 476db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 477df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 478db4728e6SMichael S. Tsirkin break; 479a7b613cfSIgor Mammedov case PCI_SEL_BASE: 4806536e427SIgor Mammedov s->hotplug_select = s->use_acpi_hotplug_bridge ? data : 4816536e427SIgor Mammedov ACPI_PCIHP_BSEL_DEFAULT; 482df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 483db4728e6SMichael S. Tsirkin default: 484db4728e6SMichael S. Tsirkin break; 485db4728e6SMichael S. Tsirkin } 486db4728e6SMichael S. Tsirkin } 487db4728e6SMichael S. Tsirkin 488db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 489db4728e6SMichael S. Tsirkin .read = pci_read, 490db4728e6SMichael S. Tsirkin .write = pci_write, 491db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 492db4728e6SMichael S. Tsirkin .valid = { 493db4728e6SMichael S. Tsirkin .min_access_size = 4, 494db4728e6SMichael S. Tsirkin .max_access_size = 4, 495db4728e6SMichael S. Tsirkin }, 496db4728e6SMichael S. Tsirkin }; 497db4728e6SMichael S. Tsirkin 49878c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 499*305ab2b9SPhilippe Mathieu-Daudé MemoryRegion *io, uint16_t io_base) 500db4728e6SMichael S. Tsirkin { 50178c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 502caf108bcSJulia Suvorova s->io_base = io_base; 503e358edc8SIgor Mammedov 504db4728e6SMichael S. Tsirkin s->root = root_bus; 505e358edc8SIgor Mammedov 50678c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 50778c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 508*305ab2b9SPhilippe Mathieu-Daudé memory_region_add_subregion(io, s->io_base, &s->io); 50978c2d872SIgor Mammedov 51078c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 511d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 51278c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 513d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 514db4728e6SMichael S. Tsirkin } 515db4728e6SMichael S. Tsirkin 516db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = { 517db4728e6SMichael S. Tsirkin .name = "acpi_pcihp_pci_status", 518db4728e6SMichael S. Tsirkin .version_id = 1, 519db4728e6SMichael S. Tsirkin .minimum_version_id = 1, 520db4728e6SMichael S. Tsirkin .fields = (VMStateField[]) { 521db4728e6SMichael S. Tsirkin VMSTATE_UINT32(up, AcpiPciHpPciStatus), 522db4728e6SMichael S. Tsirkin VMSTATE_UINT32(down, AcpiPciHpPciStatus), 523db4728e6SMichael S. Tsirkin VMSTATE_END_OF_LIST() 524db4728e6SMichael S. Tsirkin } 525db4728e6SMichael S. Tsirkin }; 526