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" 33db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h" 34db4728e6SMichael S. Tsirkin #include "exec/address-spaces.h" 35db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h" 36d6454270SMarkus Armbruster #include "migration/vmstate.h" 37da34e65cSMarkus Armbruster #include "qapi/error.h" 38db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h" 39df93b194SMarkus Armbruster #include "trace.h" 40db4728e6SMichael S. Tsirkin 41e358edc8SIgor Mammedov #define ACPI_PCIHP_ADDR 0xae00 42*b32bd763SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0018 43a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000 44a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004 45a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008 46a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c 47a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010 48*b32bd763SIgor Mammedov #define PCI_AIDX_BASE 0x0014 49db4728e6SMichael S. Tsirkin 50db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind { 51db4728e6SMichael S. Tsirkin int bsel; 52db4728e6SMichael S. Tsirkin PCIBus *bus; 53db4728e6SMichael S. Tsirkin } AcpiPciHpFind; 54db4728e6SMichael S. Tsirkin 55db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus) 56db4728e6SMichael S. Tsirkin { 577c38ecd0SKirill Batuzov Error *local_err = NULL; 58c03d83d5SMarc-André Lureau uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 597c38ecd0SKirill Batuzov &local_err); 607c38ecd0SKirill Batuzov 61c03d83d5SMarc-André Lureau if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 627c38ecd0SKirill Batuzov if (local_err) { 637c38ecd0SKirill Batuzov error_free(local_err); 64db4728e6SMichael S. Tsirkin } 65db4728e6SMichael S. Tsirkin return -1; 667c38ecd0SKirill Batuzov } else { 67db4728e6SMichael S. Tsirkin return bsel; 68db4728e6SMichael S. Tsirkin } 697c38ecd0SKirill Batuzov } 70db4728e6SMichael S. Tsirkin 71ab938ae4SAnthony PERARD /* Assign BSEL property to all buses. In the future, this can be changed 72ab938ae4SAnthony PERARD * to only assign to buses that support hotplug. 73ab938ae4SAnthony PERARD */ 74ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque) 75ab938ae4SAnthony PERARD { 76ab938ae4SAnthony PERARD unsigned *bsel_alloc = opaque; 77ab938ae4SAnthony PERARD unsigned *bus_bsel; 78ab938ae4SAnthony PERARD 79ab938ae4SAnthony PERARD if (qbus_is_hotpluggable(BUS(bus))) { 80ab938ae4SAnthony PERARD bus_bsel = g_malloc(sizeof *bus_bsel); 81ab938ae4SAnthony PERARD 82ab938ae4SAnthony PERARD *bus_bsel = (*bsel_alloc)++; 83ab938ae4SAnthony PERARD object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 84d2623129SMarkus Armbruster bus_bsel, OBJ_PROP_FLAG_READ); 85ab938ae4SAnthony PERARD } 86ab938ae4SAnthony PERARD 87ab938ae4SAnthony PERARD return bsel_alloc; 88ab938ae4SAnthony PERARD } 89ab938ae4SAnthony PERARD 90ab938ae4SAnthony PERARD static void acpi_set_pci_info(void) 91ab938ae4SAnthony PERARD { 92ab938ae4SAnthony PERARD static bool bsel_is_set; 93ab938ae4SAnthony PERARD PCIBus *bus; 94ab938ae4SAnthony PERARD unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT; 95ab938ae4SAnthony PERARD 96ab938ae4SAnthony PERARD if (bsel_is_set) { 97ab938ae4SAnthony PERARD return; 98ab938ae4SAnthony PERARD } 99ab938ae4SAnthony PERARD bsel_is_set = true; 100ab938ae4SAnthony PERARD 101ab938ae4SAnthony PERARD bus = find_i440fx(); /* TODO: Q35 support */ 102ab938ae4SAnthony PERARD if (bus) { 103ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 104ab938ae4SAnthony PERARD pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); 105ab938ae4SAnthony PERARD } 106ab938ae4SAnthony PERARD } 107ab938ae4SAnthony PERARD 1083d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void) 1093d7e78aaSAni Sinha { 1103d7e78aaSAni Sinha static bool root_hp_disabled; 1113d7e78aaSAni Sinha PCIBus *bus; 1123d7e78aaSAni Sinha 1133d7e78aaSAni Sinha if (root_hp_disabled) { 1143d7e78aaSAni Sinha return; 1153d7e78aaSAni Sinha } 1163d7e78aaSAni Sinha 1173d7e78aaSAni Sinha bus = find_i440fx(); 1183d7e78aaSAni Sinha if (bus) { 1193d7e78aaSAni Sinha /* setting the hotplug handler to NULL makes the bus non-hotpluggable */ 1203d7e78aaSAni Sinha qbus_set_hotplug_handler(BUS(bus), NULL); 1213d7e78aaSAni Sinha } 1223d7e78aaSAni Sinha root_hp_disabled = true; 1233d7e78aaSAni Sinha return; 1243d7e78aaSAni Sinha } 1253d7e78aaSAni Sinha 126db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 127db4728e6SMichael S. Tsirkin { 128db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 129db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 130db4728e6SMichael S. Tsirkin find->bus = bus; 131db4728e6SMichael S. Tsirkin } 132db4728e6SMichael S. Tsirkin } 133db4728e6SMichael S. Tsirkin 134db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 135db4728e6SMichael S. Tsirkin { 136db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 137db4728e6SMichael S. Tsirkin 138db4728e6SMichael S. Tsirkin if (bsel < 0) { 139db4728e6SMichael S. Tsirkin return NULL; 140db4728e6SMichael S. Tsirkin } 141db4728e6SMichael S. Tsirkin 142db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 143db4728e6SMichael S. Tsirkin 144db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 145db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 146db4728e6SMichael S. Tsirkin * TODO: really needed? 147db4728e6SMichael S. Tsirkin */ 148db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 149db4728e6SMichael S. Tsirkin find.bus = s->root; 150db4728e6SMichael S. Tsirkin } 1518ad038abSAni Sinha 1528ad038abSAni Sinha /* 1538ad038abSAni Sinha * Check if find.bus is actually hotpluggable. If bsel is set to 1548ad038abSAni Sinha * NULL for example on the root bus in order to make it 1558ad038abSAni Sinha * non-hotpluggable, find.bus will match the root bus when bsel 1568ad038abSAni Sinha * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the 1578ad038abSAni Sinha * bus is not hotpluggable however, we should not select the bus. 1588ad038abSAni Sinha * Instead, we should set find.bus to NULL in that case. In the check 1598ad038abSAni Sinha * below, we generalize this case for all buses, not just the root bus. 1608ad038abSAni Sinha * The callers of this function check for a null return value and 1618ad038abSAni Sinha * handle them appropriately. 1628ad038abSAni Sinha */ 1638ad038abSAni Sinha if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { 1648ad038abSAni Sinha find.bus = NULL; 1658ad038abSAni Sinha } 166db4728e6SMichael S. Tsirkin return find.bus; 167db4728e6SMichael S. Tsirkin } 168db4728e6SMichael S. Tsirkin 169db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 170db4728e6SMichael S. Tsirkin { 171db4728e6SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1722897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 173db4728e6SMichael S. Tsirkin /* 174db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 175db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 176db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 177db4728e6SMichael S. Tsirkin */ 1782897ae02SIgor Mammedov return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; 179db4728e6SMichael S. Tsirkin } 180db4728e6SMichael S. Tsirkin 181db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 182db4728e6SMichael S. Tsirkin { 183c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 184db4728e6SMichael S. Tsirkin BusChild *kid, *next; 185786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 186db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 187db4728e6SMichael S. Tsirkin 18803459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 18903459ea3SMarkus Armbruster 190a3ec4bb7SIgor Mammedov if (!bus || slot > 31) { 191db4728e6SMichael S. Tsirkin return; 192db4728e6SMichael S. Tsirkin } 193db4728e6SMichael S. Tsirkin 194db4728e6SMichael S. Tsirkin /* Mark request as complete */ 195db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 1965a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 197db4728e6SMichael S. Tsirkin 198db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 199db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 200db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 201db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 2025a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 203c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 204c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 20507578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 206db4728e6SMichael S. Tsirkin } 207db4728e6SMichael S. Tsirkin } 208db4728e6SMichael S. Tsirkin } 209db4728e6SMichael S. Tsirkin } 210db4728e6SMichael S. Tsirkin 211db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 212db4728e6SMichael S. Tsirkin { 213db4728e6SMichael S. Tsirkin BusChild *kid, *next; 214db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 215db4728e6SMichael S. Tsirkin 216db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 217db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 218db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 219db4728e6SMichael S. Tsirkin } 220db4728e6SMichael S. Tsirkin 221db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 222db4728e6SMichael S. Tsirkin 223db4728e6SMichael S. Tsirkin if (!bus) { 224db4728e6SMichael S. Tsirkin return; 225db4728e6SMichael S. Tsirkin } 226db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 227db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 228db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 229db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 230db4728e6SMichael S. Tsirkin 231db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 232db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 233db4728e6SMichael S. Tsirkin } 234db4728e6SMichael S. Tsirkin } 235db4728e6SMichael S. Tsirkin } 236db4728e6SMichael S. Tsirkin 237db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 238db4728e6SMichael S. Tsirkin { 239db4728e6SMichael S. Tsirkin int i; 240db4728e6SMichael S. Tsirkin 241db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 242db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 243db4728e6SMichael S. Tsirkin } 244db4728e6SMichael S. Tsirkin } 245db4728e6SMichael S. Tsirkin 2463d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off) 247db4728e6SMichael S. Tsirkin { 2483d7e78aaSAni Sinha if (acpihp_root_off) { 2493d7e78aaSAni Sinha acpi_pcihp_disable_root_bus(); 2503d7e78aaSAni Sinha } 251ab938ae4SAnthony PERARD acpi_set_pci_info(); 252db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 253db4728e6SMichael S. Tsirkin } 254db4728e6SMichael S. Tsirkin 255*b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1) 256*b32bd763SIgor Mammedov 257ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 258ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 259ec266f40SDavid Hildenbrand { 260*b32bd763SIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 261*b32bd763SIgor Mammedov 262ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 263ec266f40SDavid Hildenbrand if (dev->hotplugged && 264ec266f40SDavid Hildenbrand acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) { 265ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 266ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 267ec266f40SDavid Hildenbrand return; 268ec266f40SDavid Hildenbrand } 269*b32bd763SIgor Mammedov 270*b32bd763SIgor Mammedov /* 271*b32bd763SIgor Mammedov * capped by systemd (see: udev-builtin-net_id.c) 272*b32bd763SIgor Mammedov * as it's the only known user honor it to avoid users 273*b32bd763SIgor Mammedov * misconfigure QEMU and then wonder why acpi-index doesn't work 274*b32bd763SIgor Mammedov */ 275*b32bd763SIgor Mammedov if (pdev->acpi_index > ONBOARD_INDEX_MAX) { 276*b32bd763SIgor Mammedov error_setg(errp, "acpi-index should be less or equal to %u", 277*b32bd763SIgor Mammedov ONBOARD_INDEX_MAX); 278*b32bd763SIgor Mammedov return; 279*b32bd763SIgor Mammedov } 280ec266f40SDavid Hildenbrand } 281ec266f40SDavid Hildenbrand 2820058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 283c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 284db4728e6SMichael S. Tsirkin { 285c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 286c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 287ec266f40SDavid Hildenbrand int bsel; 288db4728e6SMichael S. Tsirkin 289db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 290db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 291db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 292c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 2933e520926SDavid Hildenbrand /* 2943e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 2953e520926SDavid Hildenbrand * for cold plugged bridges only. 2963e520926SDavid Hildenbrand */ 2973e520926SDavid Hildenbrand if (!s->legacy_piix && 2983e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 2993e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 3003e520926SDavid Hildenbrand 3019bc6bfdfSMarkus Armbruster qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); 3023e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 3033e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 3043e520926SDavid Hildenbrand } 3053e520926SDavid Hildenbrand 306c24d5e0bSIgor Mammedov return; 307db4728e6SMichael S. Tsirkin } 308db4728e6SMichael S. Tsirkin 309ec266f40SDavid Hildenbrand bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 310ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 3118f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 3120058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 313db4728e6SMichael S. Tsirkin } 314db4728e6SMichael S. Tsirkin 3150058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 316c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 317c24d5e0bSIgor Mammedov { 31803459ea3SMarkus Armbruster trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn), 31903459ea3SMarkus Armbruster acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev)))); 320981c3dcdSMarkus Armbruster qdev_unrealize(dev); 321c97adf3cSDavid Hildenbrand } 322c97adf3cSDavid Hildenbrand 323c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 324c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 325c97adf3cSDavid Hildenbrand Error **errp) 326c97adf3cSDavid Hildenbrand { 327c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 328c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 329fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 33003459ea3SMarkus Armbruster 33103459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 33203459ea3SMarkus Armbruster 333c24d5e0bSIgor Mammedov if (bsel < 0) { 334c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 335c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 336c24d5e0bSIgor Mammedov return; 337c24d5e0bSIgor Mammedov } 338c24d5e0bSIgor Mammedov 339c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 3400058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 341db4728e6SMichael S. Tsirkin } 342db4728e6SMichael S. Tsirkin 343db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 344db4728e6SMichael S. Tsirkin { 345db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 346db4728e6SMichael S. Tsirkin uint32_t val = 0; 347db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 348db4728e6SMichael S. Tsirkin 349fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 350db4728e6SMichael S. Tsirkin return 0; 351db4728e6SMichael S. Tsirkin } 352db4728e6SMichael S. Tsirkin 353db4728e6SMichael S. Tsirkin switch (addr) { 354a7b613cfSIgor Mammedov case PCI_UP_BASE: 3555a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 35699d09dd3SIgor Mammedov if (!s->legacy_piix) { 3575a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 35899d09dd3SIgor Mammedov } 359df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 360db4728e6SMichael S. Tsirkin break; 361a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 362db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 363df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 364db4728e6SMichael S. Tsirkin break; 365a7b613cfSIgor Mammedov case PCI_EJ_BASE: 366df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 367db4728e6SMichael S. Tsirkin break; 368a7b613cfSIgor Mammedov case PCI_RMV_BASE: 369db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 370df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 371db4728e6SMichael S. Tsirkin break; 372a7b613cfSIgor Mammedov case PCI_SEL_BASE: 373db4728e6SMichael S. Tsirkin val = s->hotplug_select; 374df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 375*b32bd763SIgor Mammedov break; 376*b32bd763SIgor Mammedov case PCI_AIDX_BASE: 377*b32bd763SIgor Mammedov val = s->acpi_index; 378*b32bd763SIgor Mammedov s->acpi_index = 0; 379*b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_read(val); 380*b32bd763SIgor Mammedov break; 381db4728e6SMichael S. Tsirkin default: 382db4728e6SMichael S. Tsirkin break; 383db4728e6SMichael S. Tsirkin } 384db4728e6SMichael S. Tsirkin 385db4728e6SMichael S. Tsirkin return val; 386db4728e6SMichael S. Tsirkin } 387db4728e6SMichael S. Tsirkin 388db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 389db4728e6SMichael S. Tsirkin unsigned int size) 390db4728e6SMichael S. Tsirkin { 391*b32bd763SIgor Mammedov int slot; 392*b32bd763SIgor Mammedov PCIBus *bus; 393*b32bd763SIgor Mammedov BusChild *kid, *next; 394db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 395*b32bd763SIgor Mammedov 396*b32bd763SIgor Mammedov s->acpi_index = 0; 397db4728e6SMichael S. Tsirkin switch (addr) { 398*b32bd763SIgor Mammedov case PCI_AIDX_BASE: 399*b32bd763SIgor Mammedov /* 400*b32bd763SIgor Mammedov * fetch acpi-index for specified slot so that follow up read from 401*b32bd763SIgor Mammedov * PCI_AIDX_BASE can return it to guest 402*b32bd763SIgor Mammedov */ 403*b32bd763SIgor Mammedov slot = ctz32(data); 404*b32bd763SIgor Mammedov 405*b32bd763SIgor Mammedov if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 406*b32bd763SIgor Mammedov break; 407*b32bd763SIgor Mammedov } 408*b32bd763SIgor Mammedov 409*b32bd763SIgor Mammedov bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); 410*b32bd763SIgor Mammedov QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 411*b32bd763SIgor Mammedov Object *o = OBJECT(kid->child); 412*b32bd763SIgor Mammedov PCIDevice *dev = PCI_DEVICE(o); 413*b32bd763SIgor Mammedov if (PCI_SLOT(dev->devfn) == slot) { 414*b32bd763SIgor Mammedov s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); 415*b32bd763SIgor Mammedov break; 416*b32bd763SIgor Mammedov } 417*b32bd763SIgor Mammedov } 418*b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); 419*b32bd763SIgor Mammedov break; 420a7b613cfSIgor Mammedov case PCI_EJ_BASE: 421db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 422db4728e6SMichael S. Tsirkin break; 423db4728e6SMichael S. Tsirkin } 424db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 425df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 426db4728e6SMichael S. Tsirkin break; 427a7b613cfSIgor Mammedov case PCI_SEL_BASE: 428f5855994SAnthony PERARD s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; 429df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 430db4728e6SMichael S. Tsirkin default: 431db4728e6SMichael S. Tsirkin break; 432db4728e6SMichael S. Tsirkin } 433db4728e6SMichael S. Tsirkin } 434db4728e6SMichael S. Tsirkin 435db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 436db4728e6SMichael S. Tsirkin .read = pci_read, 437db4728e6SMichael S. Tsirkin .write = pci_write, 438db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 439db4728e6SMichael S. Tsirkin .valid = { 440db4728e6SMichael S. Tsirkin .min_access_size = 4, 441db4728e6SMichael S. Tsirkin .max_access_size = 4, 442db4728e6SMichael S. Tsirkin }, 443db4728e6SMichael S. Tsirkin }; 444db4728e6SMichael S. Tsirkin 44578c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 44699d09dd3SIgor Mammedov MemoryRegion *address_space_io, bool bridges_enabled) 447db4728e6SMichael S. Tsirkin { 44878c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 44978c2d872SIgor Mammedov s->io_base = ACPI_PCIHP_ADDR; 450e358edc8SIgor Mammedov 451db4728e6SMichael S. Tsirkin s->root = root_bus; 45299d09dd3SIgor Mammedov s->legacy_piix = !bridges_enabled; 453e358edc8SIgor Mammedov 45478c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 45578c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 45678c2d872SIgor Mammedov memory_region_add_subregion(address_space_io, s->io_base, &s->io); 45778c2d872SIgor Mammedov 45878c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 459d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 46078c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 461d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 462db4728e6SMichael S. Tsirkin } 463db4728e6SMichael S. Tsirkin 464*b32bd763SIgor Mammedov bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id) 465*b32bd763SIgor Mammedov { 466*b32bd763SIgor Mammedov AcpiPciHpState *s = opaque; 467*b32bd763SIgor Mammedov return s->acpi_index; 468*b32bd763SIgor Mammedov } 469*b32bd763SIgor Mammedov 470db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = { 471db4728e6SMichael S. Tsirkin .name = "acpi_pcihp_pci_status", 472db4728e6SMichael S. Tsirkin .version_id = 1, 473db4728e6SMichael S. Tsirkin .minimum_version_id = 1, 474db4728e6SMichael S. Tsirkin .fields = (VMStateField[]) { 475db4728e6SMichael S. Tsirkin VMSTATE_UINT32(up, AcpiPciHpPciStatus), 476db4728e6SMichael S. Tsirkin VMSTATE_UINT32(down, AcpiPciHpPciStatus), 477db4728e6SMichael S. Tsirkin VMSTATE_END_OF_LIST() 478db4728e6SMichael S. Tsirkin } 479db4728e6SMichael S. Tsirkin }; 480