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 42b32bd763SIgor 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 48b32bd763SIgor 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 55*4fd7da4cSIgor Mammedov static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data) 56*4fd7da4cSIgor Mammedov { 57*4fd7da4cSIgor Mammedov return a - b; 58*4fd7da4cSIgor Mammedov } 59*4fd7da4cSIgor Mammedov 60*4fd7da4cSIgor Mammedov static GSequence *pci_acpi_index_list(void) 61*4fd7da4cSIgor Mammedov { 62*4fd7da4cSIgor Mammedov static GSequence *used_acpi_index_list; 63*4fd7da4cSIgor Mammedov 64*4fd7da4cSIgor Mammedov if (!used_acpi_index_list) { 65*4fd7da4cSIgor Mammedov used_acpi_index_list = g_sequence_new(NULL); 66*4fd7da4cSIgor Mammedov } 67*4fd7da4cSIgor Mammedov return used_acpi_index_list; 68*4fd7da4cSIgor Mammedov } 69*4fd7da4cSIgor Mammedov 70db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus) 71db4728e6SMichael S. Tsirkin { 727c38ecd0SKirill Batuzov Error *local_err = NULL; 73c03d83d5SMarc-André Lureau uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 747c38ecd0SKirill Batuzov &local_err); 757c38ecd0SKirill Batuzov 76c03d83d5SMarc-André Lureau if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 777c38ecd0SKirill Batuzov if (local_err) { 787c38ecd0SKirill Batuzov error_free(local_err); 79db4728e6SMichael S. Tsirkin } 80db4728e6SMichael S. Tsirkin return -1; 817c38ecd0SKirill Batuzov } else { 82db4728e6SMichael S. Tsirkin return bsel; 83db4728e6SMichael S. Tsirkin } 847c38ecd0SKirill Batuzov } 85db4728e6SMichael S. Tsirkin 86ab938ae4SAnthony PERARD /* Assign BSEL property to all buses. In the future, this can be changed 87ab938ae4SAnthony PERARD * to only assign to buses that support hotplug. 88ab938ae4SAnthony PERARD */ 89ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque) 90ab938ae4SAnthony PERARD { 91ab938ae4SAnthony PERARD unsigned *bsel_alloc = opaque; 92ab938ae4SAnthony PERARD unsigned *bus_bsel; 93ab938ae4SAnthony PERARD 94ab938ae4SAnthony PERARD if (qbus_is_hotpluggable(BUS(bus))) { 95ab938ae4SAnthony PERARD bus_bsel = g_malloc(sizeof *bus_bsel); 96ab938ae4SAnthony PERARD 97ab938ae4SAnthony PERARD *bus_bsel = (*bsel_alloc)++; 98ab938ae4SAnthony PERARD object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 99d2623129SMarkus Armbruster bus_bsel, OBJ_PROP_FLAG_READ); 100ab938ae4SAnthony PERARD } 101ab938ae4SAnthony PERARD 102ab938ae4SAnthony PERARD return bsel_alloc; 103ab938ae4SAnthony PERARD } 104ab938ae4SAnthony PERARD 105ab938ae4SAnthony PERARD static void acpi_set_pci_info(void) 106ab938ae4SAnthony PERARD { 107ab938ae4SAnthony PERARD static bool bsel_is_set; 108ab938ae4SAnthony PERARD PCIBus *bus; 109ab938ae4SAnthony PERARD unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT; 110ab938ae4SAnthony PERARD 111ab938ae4SAnthony PERARD if (bsel_is_set) { 112ab938ae4SAnthony PERARD return; 113ab938ae4SAnthony PERARD } 114ab938ae4SAnthony PERARD bsel_is_set = true; 115ab938ae4SAnthony PERARD 116ab938ae4SAnthony PERARD bus = find_i440fx(); /* TODO: Q35 support */ 117ab938ae4SAnthony PERARD if (bus) { 118ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 119ab938ae4SAnthony PERARD pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); 120ab938ae4SAnthony PERARD } 121ab938ae4SAnthony PERARD } 122ab938ae4SAnthony PERARD 1233d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void) 1243d7e78aaSAni Sinha { 1253d7e78aaSAni Sinha static bool root_hp_disabled; 1263d7e78aaSAni Sinha PCIBus *bus; 1273d7e78aaSAni Sinha 1283d7e78aaSAni Sinha if (root_hp_disabled) { 1293d7e78aaSAni Sinha return; 1303d7e78aaSAni Sinha } 1313d7e78aaSAni Sinha 1323d7e78aaSAni Sinha bus = find_i440fx(); 1333d7e78aaSAni Sinha if (bus) { 1343d7e78aaSAni Sinha /* setting the hotplug handler to NULL makes the bus non-hotpluggable */ 1353d7e78aaSAni Sinha qbus_set_hotplug_handler(BUS(bus), NULL); 1363d7e78aaSAni Sinha } 1373d7e78aaSAni Sinha root_hp_disabled = true; 1383d7e78aaSAni Sinha return; 1393d7e78aaSAni Sinha } 1403d7e78aaSAni Sinha 141db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 142db4728e6SMichael S. Tsirkin { 143db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 144db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 145db4728e6SMichael S. Tsirkin find->bus = bus; 146db4728e6SMichael S. Tsirkin } 147db4728e6SMichael S. Tsirkin } 148db4728e6SMichael S. Tsirkin 149db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 150db4728e6SMichael S. Tsirkin { 151db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 152db4728e6SMichael S. Tsirkin 153db4728e6SMichael S. Tsirkin if (bsel < 0) { 154db4728e6SMichael S. Tsirkin return NULL; 155db4728e6SMichael S. Tsirkin } 156db4728e6SMichael S. Tsirkin 157db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 158db4728e6SMichael S. Tsirkin 159db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 160db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 161db4728e6SMichael S. Tsirkin * TODO: really needed? 162db4728e6SMichael S. Tsirkin */ 163db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 164db4728e6SMichael S. Tsirkin find.bus = s->root; 165db4728e6SMichael S. Tsirkin } 1668ad038abSAni Sinha 1678ad038abSAni Sinha /* 1688ad038abSAni Sinha * Check if find.bus is actually hotpluggable. If bsel is set to 1698ad038abSAni Sinha * NULL for example on the root bus in order to make it 1708ad038abSAni Sinha * non-hotpluggable, find.bus will match the root bus when bsel 1718ad038abSAni Sinha * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the 1728ad038abSAni Sinha * bus is not hotpluggable however, we should not select the bus. 1738ad038abSAni Sinha * Instead, we should set find.bus to NULL in that case. In the check 1748ad038abSAni Sinha * below, we generalize this case for all buses, not just the root bus. 1758ad038abSAni Sinha * The callers of this function check for a null return value and 1768ad038abSAni Sinha * handle them appropriately. 1778ad038abSAni Sinha */ 1788ad038abSAni Sinha if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { 1798ad038abSAni Sinha find.bus = NULL; 1808ad038abSAni Sinha } 181db4728e6SMichael S. Tsirkin return find.bus; 182db4728e6SMichael S. Tsirkin } 183db4728e6SMichael S. Tsirkin 184db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 185db4728e6SMichael S. Tsirkin { 186db4728e6SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1872897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 188db4728e6SMichael S. Tsirkin /* 189db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 190db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 191db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 192db4728e6SMichael S. Tsirkin */ 1932897ae02SIgor Mammedov return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; 194db4728e6SMichael S. Tsirkin } 195db4728e6SMichael S. Tsirkin 196db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 197db4728e6SMichael S. Tsirkin { 198c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 199db4728e6SMichael S. Tsirkin BusChild *kid, *next; 200786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 201db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 202db4728e6SMichael S. Tsirkin 20303459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 20403459ea3SMarkus Armbruster 205a3ec4bb7SIgor Mammedov if (!bus || slot > 31) { 206db4728e6SMichael S. Tsirkin return; 207db4728e6SMichael S. Tsirkin } 208db4728e6SMichael S. Tsirkin 209db4728e6SMichael S. Tsirkin /* Mark request as complete */ 210db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 2115a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 212db4728e6SMichael S. Tsirkin 213db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 214db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 215db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 216db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 2175a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 218c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 219c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 22007578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 221db4728e6SMichael S. Tsirkin } 222db4728e6SMichael S. Tsirkin } 223db4728e6SMichael S. Tsirkin } 224db4728e6SMichael S. Tsirkin } 225db4728e6SMichael S. Tsirkin 226db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 227db4728e6SMichael S. Tsirkin { 228db4728e6SMichael S. Tsirkin BusChild *kid, *next; 229db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 230db4728e6SMichael S. Tsirkin 231db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 232db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 233db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 234db4728e6SMichael S. Tsirkin } 235db4728e6SMichael S. Tsirkin 236db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 237db4728e6SMichael S. Tsirkin 238db4728e6SMichael S. Tsirkin if (!bus) { 239db4728e6SMichael S. Tsirkin return; 240db4728e6SMichael S. Tsirkin } 241db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 242db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 243db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 244db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 245db4728e6SMichael S. Tsirkin 246db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 247db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 248db4728e6SMichael S. Tsirkin } 249db4728e6SMichael S. Tsirkin } 250db4728e6SMichael S. Tsirkin } 251db4728e6SMichael S. Tsirkin 252db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 253db4728e6SMichael S. Tsirkin { 254db4728e6SMichael S. Tsirkin int i; 255db4728e6SMichael S. Tsirkin 256db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 257db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 258db4728e6SMichael S. Tsirkin } 259db4728e6SMichael S. Tsirkin } 260db4728e6SMichael S. Tsirkin 2613d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off) 262db4728e6SMichael S. Tsirkin { 2633d7e78aaSAni Sinha if (acpihp_root_off) { 2643d7e78aaSAni Sinha acpi_pcihp_disable_root_bus(); 2653d7e78aaSAni Sinha } 266ab938ae4SAnthony PERARD acpi_set_pci_info(); 267db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 268db4728e6SMichael S. Tsirkin } 269db4728e6SMichael S. Tsirkin 270b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1) 271b32bd763SIgor Mammedov 272ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 273ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 274ec266f40SDavid Hildenbrand { 275b32bd763SIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 276b32bd763SIgor Mammedov 277ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 278ec266f40SDavid Hildenbrand if (dev->hotplugged && 279ec266f40SDavid Hildenbrand acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) { 280ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 281ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 282ec266f40SDavid Hildenbrand return; 283ec266f40SDavid Hildenbrand } 284b32bd763SIgor Mammedov 285b32bd763SIgor Mammedov /* 286b32bd763SIgor Mammedov * capped by systemd (see: udev-builtin-net_id.c) 287b32bd763SIgor Mammedov * as it's the only known user honor it to avoid users 288b32bd763SIgor Mammedov * misconfigure QEMU and then wonder why acpi-index doesn't work 289b32bd763SIgor Mammedov */ 290b32bd763SIgor Mammedov if (pdev->acpi_index > ONBOARD_INDEX_MAX) { 291b32bd763SIgor Mammedov error_setg(errp, "acpi-index should be less or equal to %u", 292b32bd763SIgor Mammedov ONBOARD_INDEX_MAX); 293b32bd763SIgor Mammedov return; 294b32bd763SIgor Mammedov } 295*4fd7da4cSIgor Mammedov 296*4fd7da4cSIgor Mammedov /* 297*4fd7da4cSIgor Mammedov * make sure that acpi-index is unique across all present PCI devices 298*4fd7da4cSIgor Mammedov */ 299*4fd7da4cSIgor Mammedov if (pdev->acpi_index) { 300*4fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 301*4fd7da4cSIgor Mammedov 302*4fd7da4cSIgor Mammedov if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index), 303*4fd7da4cSIgor Mammedov g_cmp_uint32, NULL)) { 304*4fd7da4cSIgor Mammedov error_setg(errp, "a PCI device with acpi-index = %" PRIu32 305*4fd7da4cSIgor Mammedov " already exist", pdev->acpi_index); 306*4fd7da4cSIgor Mammedov return; 307*4fd7da4cSIgor Mammedov } 308*4fd7da4cSIgor Mammedov g_sequence_insert_sorted(used_indexes, 309*4fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 310*4fd7da4cSIgor Mammedov g_cmp_uint32, NULL); 311*4fd7da4cSIgor Mammedov } 312ec266f40SDavid Hildenbrand } 313ec266f40SDavid Hildenbrand 3140058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 315c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 316db4728e6SMichael S. Tsirkin { 317c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 318c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 319ec266f40SDavid Hildenbrand int bsel; 320db4728e6SMichael S. Tsirkin 321db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 322db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 323db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 324c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 3253e520926SDavid Hildenbrand /* 3263e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 3273e520926SDavid Hildenbrand * for cold plugged bridges only. 3283e520926SDavid Hildenbrand */ 3293e520926SDavid Hildenbrand if (!s->legacy_piix && 3303e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 3313e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 3323e520926SDavid Hildenbrand 3339bc6bfdfSMarkus Armbruster qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); 3343e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 3353e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 3363e520926SDavid Hildenbrand } 3373e520926SDavid Hildenbrand 338c24d5e0bSIgor Mammedov return; 339db4728e6SMichael S. Tsirkin } 340db4728e6SMichael S. Tsirkin 341ec266f40SDavid Hildenbrand bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 342ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 3438f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 3440058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 345db4728e6SMichael S. Tsirkin } 346db4728e6SMichael S. Tsirkin 3470058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 348c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 349c24d5e0bSIgor Mammedov { 350*4fd7da4cSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 351*4fd7da4cSIgor Mammedov 35203459ea3SMarkus Armbruster trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn), 35303459ea3SMarkus Armbruster acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev)))); 354*4fd7da4cSIgor Mammedov 355*4fd7da4cSIgor Mammedov /* 356*4fd7da4cSIgor Mammedov * clean up acpi-index so it could reused by another device 357*4fd7da4cSIgor Mammedov */ 358*4fd7da4cSIgor Mammedov if (pdev->acpi_index) { 359*4fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 360*4fd7da4cSIgor Mammedov 361*4fd7da4cSIgor Mammedov g_sequence_remove(g_sequence_lookup(used_indexes, 362*4fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 363*4fd7da4cSIgor Mammedov g_cmp_uint32, NULL)); 364*4fd7da4cSIgor Mammedov } 365*4fd7da4cSIgor Mammedov 366981c3dcdSMarkus Armbruster qdev_unrealize(dev); 367c97adf3cSDavid Hildenbrand } 368c97adf3cSDavid Hildenbrand 369c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 370c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 371c97adf3cSDavid Hildenbrand Error **errp) 372c97adf3cSDavid Hildenbrand { 373c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 374c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 375fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 37603459ea3SMarkus Armbruster 37703459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 37803459ea3SMarkus Armbruster 379c24d5e0bSIgor Mammedov if (bsel < 0) { 380c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 381c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 382c24d5e0bSIgor Mammedov return; 383c24d5e0bSIgor Mammedov } 384c24d5e0bSIgor Mammedov 385c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 3860058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 387db4728e6SMichael S. Tsirkin } 388db4728e6SMichael S. Tsirkin 389db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 390db4728e6SMichael S. Tsirkin { 391db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 392db4728e6SMichael S. Tsirkin uint32_t val = 0; 393db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 394db4728e6SMichael S. Tsirkin 395fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 396db4728e6SMichael S. Tsirkin return 0; 397db4728e6SMichael S. Tsirkin } 398db4728e6SMichael S. Tsirkin 399db4728e6SMichael S. Tsirkin switch (addr) { 400a7b613cfSIgor Mammedov case PCI_UP_BASE: 4015a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 40299d09dd3SIgor Mammedov if (!s->legacy_piix) { 4035a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 40499d09dd3SIgor Mammedov } 405df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 406db4728e6SMichael S. Tsirkin break; 407a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 408db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 409df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 410db4728e6SMichael S. Tsirkin break; 411a7b613cfSIgor Mammedov case PCI_EJ_BASE: 412df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 413db4728e6SMichael S. Tsirkin break; 414a7b613cfSIgor Mammedov case PCI_RMV_BASE: 415db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 416df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 417db4728e6SMichael S. Tsirkin break; 418a7b613cfSIgor Mammedov case PCI_SEL_BASE: 419db4728e6SMichael S. Tsirkin val = s->hotplug_select; 420df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 421b32bd763SIgor Mammedov break; 422b32bd763SIgor Mammedov case PCI_AIDX_BASE: 423b32bd763SIgor Mammedov val = s->acpi_index; 424b32bd763SIgor Mammedov s->acpi_index = 0; 425b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_read(val); 426b32bd763SIgor Mammedov break; 427db4728e6SMichael S. Tsirkin default: 428db4728e6SMichael S. Tsirkin break; 429db4728e6SMichael S. Tsirkin } 430db4728e6SMichael S. Tsirkin 431db4728e6SMichael S. Tsirkin return val; 432db4728e6SMichael S. Tsirkin } 433db4728e6SMichael S. Tsirkin 434db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 435db4728e6SMichael S. Tsirkin unsigned int size) 436db4728e6SMichael S. Tsirkin { 437b32bd763SIgor Mammedov int slot; 438b32bd763SIgor Mammedov PCIBus *bus; 439b32bd763SIgor Mammedov BusChild *kid, *next; 440db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 441b32bd763SIgor Mammedov 442b32bd763SIgor Mammedov s->acpi_index = 0; 443db4728e6SMichael S. Tsirkin switch (addr) { 444b32bd763SIgor Mammedov case PCI_AIDX_BASE: 445b32bd763SIgor Mammedov /* 446b32bd763SIgor Mammedov * fetch acpi-index for specified slot so that follow up read from 447b32bd763SIgor Mammedov * PCI_AIDX_BASE can return it to guest 448b32bd763SIgor Mammedov */ 449b32bd763SIgor Mammedov slot = ctz32(data); 450b32bd763SIgor Mammedov 451b32bd763SIgor Mammedov if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 452b32bd763SIgor Mammedov break; 453b32bd763SIgor Mammedov } 454b32bd763SIgor Mammedov 455b32bd763SIgor Mammedov bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); 456b32bd763SIgor Mammedov QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 457b32bd763SIgor Mammedov Object *o = OBJECT(kid->child); 458b32bd763SIgor Mammedov PCIDevice *dev = PCI_DEVICE(o); 459b32bd763SIgor Mammedov if (PCI_SLOT(dev->devfn) == slot) { 460b32bd763SIgor Mammedov s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); 461b32bd763SIgor Mammedov break; 462b32bd763SIgor Mammedov } 463b32bd763SIgor Mammedov } 464b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); 465b32bd763SIgor Mammedov break; 466a7b613cfSIgor Mammedov case PCI_EJ_BASE: 467db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 468db4728e6SMichael S. Tsirkin break; 469db4728e6SMichael S. Tsirkin } 470db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 471df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 472db4728e6SMichael S. Tsirkin break; 473a7b613cfSIgor Mammedov case PCI_SEL_BASE: 474f5855994SAnthony PERARD s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; 475df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 476db4728e6SMichael S. Tsirkin default: 477db4728e6SMichael S. Tsirkin break; 478db4728e6SMichael S. Tsirkin } 479db4728e6SMichael S. Tsirkin } 480db4728e6SMichael S. Tsirkin 481db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 482db4728e6SMichael S. Tsirkin .read = pci_read, 483db4728e6SMichael S. Tsirkin .write = pci_write, 484db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 485db4728e6SMichael S. Tsirkin .valid = { 486db4728e6SMichael S. Tsirkin .min_access_size = 4, 487db4728e6SMichael S. Tsirkin .max_access_size = 4, 488db4728e6SMichael S. Tsirkin }, 489db4728e6SMichael S. Tsirkin }; 490db4728e6SMichael S. Tsirkin 49178c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 49299d09dd3SIgor Mammedov MemoryRegion *address_space_io, bool bridges_enabled) 493db4728e6SMichael S. Tsirkin { 49478c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 49578c2d872SIgor Mammedov s->io_base = ACPI_PCIHP_ADDR; 496e358edc8SIgor Mammedov 497db4728e6SMichael S. Tsirkin s->root = root_bus; 49899d09dd3SIgor Mammedov s->legacy_piix = !bridges_enabled; 499e358edc8SIgor Mammedov 50078c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 50178c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 50278c2d872SIgor Mammedov memory_region_add_subregion(address_space_io, s->io_base, &s->io); 50378c2d872SIgor Mammedov 50478c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 505d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 50678c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 507d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 508db4728e6SMichael S. Tsirkin } 509db4728e6SMichael S. Tsirkin 510b32bd763SIgor Mammedov bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id) 511b32bd763SIgor Mammedov { 512b32bd763SIgor Mammedov AcpiPciHpState *s = opaque; 513b32bd763SIgor Mammedov return s->acpi_index; 514b32bd763SIgor Mammedov } 515b32bd763SIgor Mammedov 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