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 13db4728e6SMichael S. Tsirkin * License version 2 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 30db4728e6SMichael S. Tsirkin #include "hw/hw.h" 31db4728e6SMichael S. Tsirkin #include "hw/i386/pc.h" 32db4728e6SMichael S. Tsirkin #include "hw/pci/pci.h" 333e520926SDavid Hildenbrand #include "hw/pci/pci_bridge.h" 34db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h" 35db4728e6SMichael S. Tsirkin #include "sysemu/sysemu.h" 36db4728e6SMichael S. Tsirkin #include "exec/address-spaces.h" 37db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h" 38*d6454270SMarkus Armbruster #include "migration/vmstate.h" 39da34e65cSMarkus Armbruster #include "qapi/error.h" 40db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h" 41df93b194SMarkus Armbruster #include "trace.h" 42db4728e6SMichael S. Tsirkin 43e358edc8SIgor Mammedov #define ACPI_PCIHP_ADDR 0xae00 44e358edc8SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0014 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 50db4728e6SMichael S. Tsirkin 51db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind { 52db4728e6SMichael S. Tsirkin int bsel; 53db4728e6SMichael S. Tsirkin PCIBus *bus; 54db4728e6SMichael S. Tsirkin } AcpiPciHpFind; 55db4728e6SMichael S. Tsirkin 56db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus) 57db4728e6SMichael S. Tsirkin { 587c38ecd0SKirill Batuzov Error *local_err = NULL; 59c03d83d5SMarc-André Lureau uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 607c38ecd0SKirill Batuzov &local_err); 617c38ecd0SKirill Batuzov 62c03d83d5SMarc-André Lureau if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 637c38ecd0SKirill Batuzov if (local_err) { 647c38ecd0SKirill Batuzov error_free(local_err); 65db4728e6SMichael S. Tsirkin } 66db4728e6SMichael S. Tsirkin return -1; 677c38ecd0SKirill Batuzov } else { 68db4728e6SMichael S. Tsirkin return bsel; 69db4728e6SMichael S. Tsirkin } 707c38ecd0SKirill Batuzov } 71db4728e6SMichael S. Tsirkin 72ab938ae4SAnthony PERARD /* Assign BSEL property to all buses. In the future, this can be changed 73ab938ae4SAnthony PERARD * to only assign to buses that support hotplug. 74ab938ae4SAnthony PERARD */ 75ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque) 76ab938ae4SAnthony PERARD { 77ab938ae4SAnthony PERARD unsigned *bsel_alloc = opaque; 78ab938ae4SAnthony PERARD unsigned *bus_bsel; 79ab938ae4SAnthony PERARD 80ab938ae4SAnthony PERARD if (qbus_is_hotpluggable(BUS(bus))) { 81ab938ae4SAnthony PERARD bus_bsel = g_malloc(sizeof *bus_bsel); 82ab938ae4SAnthony PERARD 83ab938ae4SAnthony PERARD *bus_bsel = (*bsel_alloc)++; 84ab938ae4SAnthony PERARD object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 85ab938ae4SAnthony PERARD bus_bsel, &error_abort); 86ab938ae4SAnthony PERARD } 87ab938ae4SAnthony PERARD 88ab938ae4SAnthony PERARD return bsel_alloc; 89ab938ae4SAnthony PERARD } 90ab938ae4SAnthony PERARD 91ab938ae4SAnthony PERARD static void acpi_set_pci_info(void) 92ab938ae4SAnthony PERARD { 93ab938ae4SAnthony PERARD static bool bsel_is_set; 94ab938ae4SAnthony PERARD PCIBus *bus; 95ab938ae4SAnthony PERARD unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT; 96ab938ae4SAnthony PERARD 97ab938ae4SAnthony PERARD if (bsel_is_set) { 98ab938ae4SAnthony PERARD return; 99ab938ae4SAnthony PERARD } 100ab938ae4SAnthony PERARD bsel_is_set = true; 101ab938ae4SAnthony PERARD 102ab938ae4SAnthony PERARD bus = find_i440fx(); /* TODO: Q35 support */ 103ab938ae4SAnthony PERARD if (bus) { 104ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 105ab938ae4SAnthony PERARD pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); 106ab938ae4SAnthony PERARD } 107ab938ae4SAnthony PERARD } 108ab938ae4SAnthony PERARD 109db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 110db4728e6SMichael S. Tsirkin { 111db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 112db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 113db4728e6SMichael S. Tsirkin find->bus = bus; 114db4728e6SMichael S. Tsirkin } 115db4728e6SMichael S. Tsirkin } 116db4728e6SMichael S. Tsirkin 117db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 118db4728e6SMichael S. Tsirkin { 119db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 120db4728e6SMichael S. Tsirkin 121db4728e6SMichael S. Tsirkin if (bsel < 0) { 122db4728e6SMichael S. Tsirkin return NULL; 123db4728e6SMichael S. Tsirkin } 124db4728e6SMichael S. Tsirkin 125db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 126db4728e6SMichael S. Tsirkin 127db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 128db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 129db4728e6SMichael S. Tsirkin * TODO: really needed? 130db4728e6SMichael S. Tsirkin */ 131db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 132db4728e6SMichael S. Tsirkin find.bus = s->root; 133db4728e6SMichael S. Tsirkin } 134db4728e6SMichael S. Tsirkin return find.bus; 135db4728e6SMichael S. Tsirkin } 136db4728e6SMichael S. Tsirkin 137db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 138db4728e6SMichael S. Tsirkin { 139db4728e6SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1402897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 141db4728e6SMichael S. Tsirkin /* 142db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 143db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 144db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 145db4728e6SMichael S. Tsirkin */ 1462897ae02SIgor Mammedov return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; 147db4728e6SMichael S. Tsirkin } 148db4728e6SMichael S. Tsirkin 149db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 150db4728e6SMichael S. Tsirkin { 151c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 152db4728e6SMichael S. Tsirkin BusChild *kid, *next; 153786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 154db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 155db4728e6SMichael S. Tsirkin 15603459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 15703459ea3SMarkus Armbruster 158db4728e6SMichael S. Tsirkin if (!bus) { 159db4728e6SMichael S. Tsirkin return; 160db4728e6SMichael S. Tsirkin } 161db4728e6SMichael S. Tsirkin 162db4728e6SMichael S. Tsirkin /* Mark request as complete */ 163db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 1645a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 165db4728e6SMichael S. Tsirkin 166db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 167db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 168db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 169db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 1705a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 171c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 172c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 17307578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 174db4728e6SMichael S. Tsirkin } 175db4728e6SMichael S. Tsirkin } 176db4728e6SMichael S. Tsirkin } 177db4728e6SMichael S. Tsirkin } 178db4728e6SMichael S. Tsirkin 179db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 180db4728e6SMichael S. Tsirkin { 181db4728e6SMichael S. Tsirkin BusChild *kid, *next; 182db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 183db4728e6SMichael S. Tsirkin 184db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 185db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 186db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 187db4728e6SMichael S. Tsirkin } 188db4728e6SMichael S. Tsirkin 189db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 190db4728e6SMichael S. Tsirkin 191db4728e6SMichael S. Tsirkin if (!bus) { 192db4728e6SMichael S. Tsirkin return; 193db4728e6SMichael S. Tsirkin } 194db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 195db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 196db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 197db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 198db4728e6SMichael S. Tsirkin 199db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 200db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 201db4728e6SMichael S. Tsirkin } 202db4728e6SMichael S. Tsirkin } 203db4728e6SMichael S. Tsirkin } 204db4728e6SMichael S. Tsirkin 205db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 206db4728e6SMichael S. Tsirkin { 207db4728e6SMichael S. Tsirkin int i; 208db4728e6SMichael S. Tsirkin 209db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 210db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 211db4728e6SMichael S. Tsirkin } 212db4728e6SMichael S. Tsirkin } 213db4728e6SMichael S. Tsirkin 214db4728e6SMichael S. Tsirkin void acpi_pcihp_reset(AcpiPciHpState *s) 215db4728e6SMichael S. Tsirkin { 216ab938ae4SAnthony PERARD acpi_set_pci_info(); 217db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 218db4728e6SMichael S. Tsirkin } 219db4728e6SMichael S. Tsirkin 220ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 221ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 222ec266f40SDavid Hildenbrand { 223ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 224ec266f40SDavid Hildenbrand if (dev->hotplugged && 225ec266f40SDavid Hildenbrand acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) { 226ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 227ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 228ec266f40SDavid Hildenbrand return; 229ec266f40SDavid Hildenbrand } 230ec266f40SDavid Hildenbrand } 231ec266f40SDavid Hildenbrand 2320058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 233c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 234db4728e6SMichael S. Tsirkin { 235c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 236c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 237ec266f40SDavid Hildenbrand int bsel; 238db4728e6SMichael S. Tsirkin 239db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 240db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 241db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 242c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 2433e520926SDavid Hildenbrand /* 2443e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 2453e520926SDavid Hildenbrand * for cold plugged bridges only. 2463e520926SDavid Hildenbrand */ 2473e520926SDavid Hildenbrand if (!s->legacy_piix && 2483e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 2493e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 2503e520926SDavid Hildenbrand 25194d1cc5fSMichael Roth qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev), 2523e520926SDavid Hildenbrand &error_abort); 2533e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 2543e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 2553e520926SDavid Hildenbrand } 2563e520926SDavid Hildenbrand 257c24d5e0bSIgor Mammedov return; 258db4728e6SMichael S. Tsirkin } 259db4728e6SMichael S. Tsirkin 260ec266f40SDavid Hildenbrand bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 261ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 2628f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 2630058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 264db4728e6SMichael S. Tsirkin } 265db4728e6SMichael S. Tsirkin 2660058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 267c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 268c24d5e0bSIgor Mammedov { 26903459ea3SMarkus Armbruster trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn), 27003459ea3SMarkus Armbruster acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev)))); 27107578b0aSDavid Hildenbrand object_property_set_bool(OBJECT(dev), false, "realized", NULL); 272c97adf3cSDavid Hildenbrand } 273c97adf3cSDavid Hildenbrand 274c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 275c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 276c97adf3cSDavid Hildenbrand Error **errp) 277c97adf3cSDavid Hildenbrand { 278c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 279c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 280fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 28103459ea3SMarkus Armbruster 28203459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 28303459ea3SMarkus Armbruster 284c24d5e0bSIgor Mammedov if (bsel < 0) { 285c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 286c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 287c24d5e0bSIgor Mammedov return; 288c24d5e0bSIgor Mammedov } 289c24d5e0bSIgor Mammedov 290c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 2910058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 292db4728e6SMichael S. Tsirkin } 293db4728e6SMichael S. Tsirkin 294db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 295db4728e6SMichael S. Tsirkin { 296db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 297db4728e6SMichael S. Tsirkin uint32_t val = 0; 298db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 299db4728e6SMichael S. Tsirkin 300fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 301db4728e6SMichael S. Tsirkin return 0; 302db4728e6SMichael S. Tsirkin } 303db4728e6SMichael S. Tsirkin 304db4728e6SMichael S. Tsirkin switch (addr) { 305a7b613cfSIgor Mammedov case PCI_UP_BASE: 3065a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 30799d09dd3SIgor Mammedov if (!s->legacy_piix) { 3085a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 30999d09dd3SIgor Mammedov } 310df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 311db4728e6SMichael S. Tsirkin break; 312a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 313db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 314df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 315db4728e6SMichael S. Tsirkin break; 316a7b613cfSIgor Mammedov case PCI_EJ_BASE: 317db4728e6SMichael S. Tsirkin /* No feature defined yet */ 318df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 319db4728e6SMichael S. Tsirkin break; 320a7b613cfSIgor Mammedov case PCI_RMV_BASE: 321db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 322df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 323db4728e6SMichael S. Tsirkin break; 324a7b613cfSIgor Mammedov case PCI_SEL_BASE: 325db4728e6SMichael S. Tsirkin val = s->hotplug_select; 326df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 327db4728e6SMichael S. Tsirkin default: 328db4728e6SMichael S. Tsirkin break; 329db4728e6SMichael S. Tsirkin } 330db4728e6SMichael S. Tsirkin 331db4728e6SMichael S. Tsirkin return val; 332db4728e6SMichael S. Tsirkin } 333db4728e6SMichael S. Tsirkin 334db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 335db4728e6SMichael S. Tsirkin unsigned int size) 336db4728e6SMichael S. Tsirkin { 337db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 338db4728e6SMichael S. Tsirkin switch (addr) { 339a7b613cfSIgor Mammedov case PCI_EJ_BASE: 340db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 341db4728e6SMichael S. Tsirkin break; 342db4728e6SMichael S. Tsirkin } 343db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 344df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 345db4728e6SMichael S. Tsirkin break; 346a7b613cfSIgor Mammedov case PCI_SEL_BASE: 347f5855994SAnthony PERARD s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; 348df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 349db4728e6SMichael S. Tsirkin default: 350db4728e6SMichael S. Tsirkin break; 351db4728e6SMichael S. Tsirkin } 352db4728e6SMichael S. Tsirkin } 353db4728e6SMichael S. Tsirkin 354db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 355db4728e6SMichael S. Tsirkin .read = pci_read, 356db4728e6SMichael S. Tsirkin .write = pci_write, 357db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 358db4728e6SMichael S. Tsirkin .valid = { 359db4728e6SMichael S. Tsirkin .min_access_size = 4, 360db4728e6SMichael S. Tsirkin .max_access_size = 4, 361db4728e6SMichael S. Tsirkin }, 362db4728e6SMichael S. Tsirkin }; 363db4728e6SMichael S. Tsirkin 36478c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 36599d09dd3SIgor Mammedov MemoryRegion *address_space_io, bool bridges_enabled) 366db4728e6SMichael S. Tsirkin { 36778c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 36878c2d872SIgor Mammedov s->io_base = ACPI_PCIHP_ADDR; 369e358edc8SIgor Mammedov 370db4728e6SMichael S. Tsirkin s->root= root_bus; 37199d09dd3SIgor Mammedov s->legacy_piix = !bridges_enabled; 372e358edc8SIgor Mammedov 37378c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 37478c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 37578c2d872SIgor Mammedov memory_region_add_subregion(address_space_io, s->io_base, &s->io); 37678c2d872SIgor Mammedov 37778c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 37878c2d872SIgor Mammedov &error_abort); 37978c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 38078c2d872SIgor Mammedov &error_abort); 381db4728e6SMichael S. Tsirkin } 382db4728e6SMichael S. Tsirkin 383db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = { 384db4728e6SMichael S. Tsirkin .name = "acpi_pcihp_pci_status", 385db4728e6SMichael S. Tsirkin .version_id = 1, 386db4728e6SMichael S. Tsirkin .minimum_version_id = 1, 387db4728e6SMichael S. Tsirkin .fields = (VMStateField[]) { 388db4728e6SMichael S. Tsirkin VMSTATE_UINT32(up, AcpiPciHpPciStatus), 389db4728e6SMichael S. Tsirkin VMSTATE_UINT32(down, AcpiPciHpPciStatus), 390db4728e6SMichael S. Tsirkin VMSTATE_END_OF_LIST() 391db4728e6SMichael S. Tsirkin } 392db4728e6SMichael S. Tsirkin }; 393