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 "hw/pci/pci_bus.h" 35d6454270SMarkus Armbruster #include "migration/vmstate.h" 36da34e65cSMarkus Armbruster #include "qapi/error.h" 37db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h" 38df93b194SMarkus Armbruster #include "trace.h" 39db4728e6SMichael S. Tsirkin 40b32bd763SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0018 41a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000 42a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004 43a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008 44a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c 45a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010 46b32bd763SIgor Mammedov #define PCI_AIDX_BASE 0x0014 47db4728e6SMichael S. Tsirkin 48db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind { 49db4728e6SMichael S. Tsirkin int bsel; 50db4728e6SMichael S. Tsirkin PCIBus *bus; 51db4728e6SMichael S. Tsirkin } AcpiPciHpFind; 52db4728e6SMichael S. Tsirkin 534fd7da4cSIgor Mammedov static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data) 544fd7da4cSIgor Mammedov { 554fd7da4cSIgor Mammedov return a - b; 564fd7da4cSIgor Mammedov } 574fd7da4cSIgor Mammedov 584fd7da4cSIgor Mammedov static GSequence *pci_acpi_index_list(void) 594fd7da4cSIgor Mammedov { 604fd7da4cSIgor Mammedov static GSequence *used_acpi_index_list; 614fd7da4cSIgor Mammedov 624fd7da4cSIgor Mammedov if (!used_acpi_index_list) { 634fd7da4cSIgor Mammedov used_acpi_index_list = g_sequence_new(NULL); 644fd7da4cSIgor Mammedov } 654fd7da4cSIgor Mammedov return used_acpi_index_list; 664fd7da4cSIgor Mammedov } 674fd7da4cSIgor Mammedov 68db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus) 69db4728e6SMichael S. Tsirkin { 707c38ecd0SKirill Batuzov Error *local_err = NULL; 71c03d83d5SMarc-André Lureau uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 727c38ecd0SKirill Batuzov &local_err); 737c38ecd0SKirill Batuzov 74c03d83d5SMarc-André Lureau if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 757c38ecd0SKirill Batuzov if (local_err) { 767c38ecd0SKirill Batuzov error_free(local_err); 77db4728e6SMichael S. Tsirkin } 78db4728e6SMichael S. Tsirkin return -1; 797c38ecd0SKirill Batuzov } else { 80db4728e6SMichael S. Tsirkin return bsel; 81db4728e6SMichael S. Tsirkin } 827c38ecd0SKirill Batuzov } 83db4728e6SMichael S. Tsirkin 84ab938ae4SAnthony PERARD /* Assign BSEL property to all buses. In the future, this can be changed 85ab938ae4SAnthony PERARD * to only assign to buses that support hotplug. 86ab938ae4SAnthony PERARD */ 87ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque) 88ab938ae4SAnthony PERARD { 89ab938ae4SAnthony PERARD unsigned *bsel_alloc = opaque; 90ab938ae4SAnthony PERARD unsigned *bus_bsel; 91ab938ae4SAnthony PERARD 92ab938ae4SAnthony PERARD if (qbus_is_hotpluggable(BUS(bus))) { 93ab938ae4SAnthony PERARD bus_bsel = g_malloc(sizeof *bus_bsel); 94ab938ae4SAnthony PERARD 95ab938ae4SAnthony PERARD *bus_bsel = (*bsel_alloc)++; 96ab938ae4SAnthony PERARD object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, 97d2623129SMarkus Armbruster bus_bsel, OBJ_PROP_FLAG_READ); 98ab938ae4SAnthony PERARD } 99ab938ae4SAnthony PERARD 100ab938ae4SAnthony PERARD return bsel_alloc; 101ab938ae4SAnthony PERARD } 102ab938ae4SAnthony PERARD 103ab938ae4SAnthony PERARD static void acpi_set_pci_info(void) 104ab938ae4SAnthony PERARD { 105ab938ae4SAnthony PERARD static bool bsel_is_set; 106ab938ae4SAnthony PERARD PCIBus *bus; 107ab938ae4SAnthony PERARD unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT; 108ab938ae4SAnthony PERARD 109ab938ae4SAnthony PERARD if (bsel_is_set) { 110ab938ae4SAnthony PERARD return; 111ab938ae4SAnthony PERARD } 112ab938ae4SAnthony PERARD bsel_is_set = true; 113ab938ae4SAnthony PERARD 114ab938ae4SAnthony PERARD bus = find_i440fx(); /* TODO: Q35 support */ 115ab938ae4SAnthony PERARD if (bus) { 116ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 117ab938ae4SAnthony PERARD pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); 118ab938ae4SAnthony PERARD } 119ab938ae4SAnthony PERARD } 120ab938ae4SAnthony PERARD 1213d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void) 1223d7e78aaSAni Sinha { 1233d7e78aaSAni Sinha static bool root_hp_disabled; 1243d7e78aaSAni Sinha PCIBus *bus; 1253d7e78aaSAni Sinha 1263d7e78aaSAni Sinha if (root_hp_disabled) { 1273d7e78aaSAni Sinha return; 1283d7e78aaSAni Sinha } 1293d7e78aaSAni Sinha 1303d7e78aaSAni Sinha bus = find_i440fx(); 1313d7e78aaSAni Sinha if (bus) { 1323d7e78aaSAni Sinha /* setting the hotplug handler to NULL makes the bus non-hotpluggable */ 1333d7e78aaSAni Sinha qbus_set_hotplug_handler(BUS(bus), NULL); 1343d7e78aaSAni Sinha } 1353d7e78aaSAni Sinha root_hp_disabled = true; 1363d7e78aaSAni Sinha return; 1373d7e78aaSAni Sinha } 1383d7e78aaSAni Sinha 139db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 140db4728e6SMichael S. Tsirkin { 141db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 142db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 143db4728e6SMichael S. Tsirkin find->bus = bus; 144db4728e6SMichael S. Tsirkin } 145db4728e6SMichael S. Tsirkin } 146db4728e6SMichael S. Tsirkin 147db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 148db4728e6SMichael S. Tsirkin { 149db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 150db4728e6SMichael S. Tsirkin 151db4728e6SMichael S. Tsirkin if (bsel < 0) { 152db4728e6SMichael S. Tsirkin return NULL; 153db4728e6SMichael S. Tsirkin } 154db4728e6SMichael S. Tsirkin 155db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 156db4728e6SMichael S. Tsirkin 157db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 158db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 159db4728e6SMichael S. Tsirkin * TODO: really needed? 160db4728e6SMichael S. Tsirkin */ 161db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 162db4728e6SMichael S. Tsirkin find.bus = s->root; 163db4728e6SMichael S. Tsirkin } 1648ad038abSAni Sinha 1658ad038abSAni Sinha /* 1668ad038abSAni Sinha * Check if find.bus is actually hotpluggable. If bsel is set to 1678ad038abSAni Sinha * NULL for example on the root bus in order to make it 1688ad038abSAni Sinha * non-hotpluggable, find.bus will match the root bus when bsel 1698ad038abSAni Sinha * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the 1708ad038abSAni Sinha * bus is not hotpluggable however, we should not select the bus. 1718ad038abSAni Sinha * Instead, we should set find.bus to NULL in that case. In the check 1728ad038abSAni Sinha * below, we generalize this case for all buses, not just the root bus. 1738ad038abSAni Sinha * The callers of this function check for a null return value and 1748ad038abSAni Sinha * handle them appropriately. 1758ad038abSAni Sinha */ 1768ad038abSAni Sinha if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { 1778ad038abSAni Sinha find.bus = NULL; 1788ad038abSAni Sinha } 179db4728e6SMichael S. Tsirkin return find.bus; 180db4728e6SMichael S. Tsirkin } 181db4728e6SMichael S. Tsirkin 182db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 183db4728e6SMichael S. Tsirkin { 184db4728e6SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1852897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 186db4728e6SMichael S. Tsirkin /* 187db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 188db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 189db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 190db4728e6SMichael S. Tsirkin */ 1912897ae02SIgor Mammedov return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; 192db4728e6SMichael S. Tsirkin } 193db4728e6SMichael S. Tsirkin 194db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 195db4728e6SMichael S. Tsirkin { 196c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 197db4728e6SMichael S. Tsirkin BusChild *kid, *next; 198786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 199db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 200db4728e6SMichael S. Tsirkin 20103459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 20203459ea3SMarkus Armbruster 203a3ec4bb7SIgor Mammedov if (!bus || slot > 31) { 204db4728e6SMichael S. Tsirkin return; 205db4728e6SMichael S. Tsirkin } 206db4728e6SMichael S. Tsirkin 207db4728e6SMichael S. Tsirkin /* Mark request as complete */ 208db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 2095a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 210db4728e6SMichael S. Tsirkin 211db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 212db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 213db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 214db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 2155a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 216c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 217c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 21807578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 219db4728e6SMichael S. Tsirkin } 220db4728e6SMichael S. Tsirkin } 221db4728e6SMichael S. Tsirkin } 222db4728e6SMichael S. Tsirkin } 223db4728e6SMichael S. Tsirkin 224db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 225db4728e6SMichael S. Tsirkin { 226db4728e6SMichael S. Tsirkin BusChild *kid, *next; 227db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 228db4728e6SMichael S. Tsirkin 229db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 230db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 231db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 232db4728e6SMichael S. Tsirkin } 233db4728e6SMichael S. Tsirkin 234db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 235db4728e6SMichael S. Tsirkin 236db4728e6SMichael S. Tsirkin if (!bus) { 237db4728e6SMichael S. Tsirkin return; 238db4728e6SMichael S. Tsirkin } 239db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 240db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 241db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 242db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 243db4728e6SMichael S. Tsirkin 244db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 245db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 246db4728e6SMichael S. Tsirkin } 247db4728e6SMichael S. Tsirkin } 248db4728e6SMichael S. Tsirkin } 249db4728e6SMichael S. Tsirkin 250db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 251db4728e6SMichael S. Tsirkin { 252db4728e6SMichael S. Tsirkin int i; 253db4728e6SMichael S. Tsirkin 254db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 255db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 256db4728e6SMichael S. Tsirkin } 257db4728e6SMichael S. Tsirkin } 258db4728e6SMichael S. Tsirkin 2593d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off) 260db4728e6SMichael S. Tsirkin { 2613d7e78aaSAni Sinha if (acpihp_root_off) { 2623d7e78aaSAni Sinha acpi_pcihp_disable_root_bus(); 2633d7e78aaSAni Sinha } 264ab938ae4SAnthony PERARD acpi_set_pci_info(); 265db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 266db4728e6SMichael S. Tsirkin } 267db4728e6SMichael S. Tsirkin 268b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1) 269b32bd763SIgor Mammedov 270ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 271ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 272ec266f40SDavid Hildenbrand { 273b32bd763SIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 274b32bd763SIgor Mammedov 275ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 276ec266f40SDavid Hildenbrand if (dev->hotplugged && 277ec266f40SDavid Hildenbrand acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) { 278ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 279ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 280ec266f40SDavid Hildenbrand return; 281ec266f40SDavid Hildenbrand } 282b32bd763SIgor Mammedov 283b32bd763SIgor Mammedov /* 284b32bd763SIgor Mammedov * capped by systemd (see: udev-builtin-net_id.c) 285b32bd763SIgor Mammedov * as it's the only known user honor it to avoid users 286b32bd763SIgor Mammedov * misconfigure QEMU and then wonder why acpi-index doesn't work 287b32bd763SIgor Mammedov */ 288b32bd763SIgor Mammedov if (pdev->acpi_index > ONBOARD_INDEX_MAX) { 289b32bd763SIgor Mammedov error_setg(errp, "acpi-index should be less or equal to %u", 290b32bd763SIgor Mammedov ONBOARD_INDEX_MAX); 291b32bd763SIgor Mammedov return; 292b32bd763SIgor Mammedov } 2934fd7da4cSIgor Mammedov 2944fd7da4cSIgor Mammedov /* 2954fd7da4cSIgor Mammedov * make sure that acpi-index is unique across all present PCI devices 2964fd7da4cSIgor Mammedov */ 2974fd7da4cSIgor Mammedov if (pdev->acpi_index) { 2984fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 2994fd7da4cSIgor Mammedov 3004fd7da4cSIgor Mammedov if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index), 3014fd7da4cSIgor Mammedov g_cmp_uint32, NULL)) { 3024fd7da4cSIgor Mammedov error_setg(errp, "a PCI device with acpi-index = %" PRIu32 3034fd7da4cSIgor Mammedov " already exist", pdev->acpi_index); 3044fd7da4cSIgor Mammedov return; 3054fd7da4cSIgor Mammedov } 3064fd7da4cSIgor Mammedov g_sequence_insert_sorted(used_indexes, 3074fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 3084fd7da4cSIgor Mammedov g_cmp_uint32, NULL); 3094fd7da4cSIgor Mammedov } 310ec266f40SDavid Hildenbrand } 311ec266f40SDavid Hildenbrand 3120058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 313c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 314db4728e6SMichael S. Tsirkin { 315c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 316c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 317ec266f40SDavid Hildenbrand int bsel; 318db4728e6SMichael S. Tsirkin 319db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 320db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 321db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 322c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 3233e520926SDavid Hildenbrand /* 3243e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 3253e520926SDavid Hildenbrand * for cold plugged bridges only. 3263e520926SDavid Hildenbrand */ 3273e520926SDavid Hildenbrand if (!s->legacy_piix && 3283e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 3293e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 3303e520926SDavid Hildenbrand 3319bc6bfdfSMarkus Armbruster qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); 3323e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 3333e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 3343e520926SDavid Hildenbrand } 3353e520926SDavid Hildenbrand 336c24d5e0bSIgor Mammedov return; 337db4728e6SMichael S. Tsirkin } 338db4728e6SMichael S. Tsirkin 339ec266f40SDavid Hildenbrand bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 340ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 3418f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 3420058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 343db4728e6SMichael S. Tsirkin } 344db4728e6SMichael S. Tsirkin 3450058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 346c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 347c24d5e0bSIgor Mammedov { 3484fd7da4cSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 3494fd7da4cSIgor Mammedov 35003459ea3SMarkus Armbruster trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn), 35103459ea3SMarkus Armbruster acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev)))); 3524fd7da4cSIgor Mammedov 3534fd7da4cSIgor Mammedov /* 3544fd7da4cSIgor Mammedov * clean up acpi-index so it could reused by another device 3554fd7da4cSIgor Mammedov */ 3564fd7da4cSIgor Mammedov if (pdev->acpi_index) { 3574fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 3584fd7da4cSIgor Mammedov 3594fd7da4cSIgor Mammedov g_sequence_remove(g_sequence_lookup(used_indexes, 3604fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 3614fd7da4cSIgor Mammedov g_cmp_uint32, NULL)); 3624fd7da4cSIgor Mammedov } 3634fd7da4cSIgor Mammedov 364981c3dcdSMarkus Armbruster qdev_unrealize(dev); 365c97adf3cSDavid Hildenbrand } 366c97adf3cSDavid Hildenbrand 367c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 368c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 369c97adf3cSDavid Hildenbrand Error **errp) 370c97adf3cSDavid Hildenbrand { 371c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 372c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 373fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 37403459ea3SMarkus Armbruster 37503459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 37603459ea3SMarkus Armbruster 377c24d5e0bSIgor Mammedov if (bsel < 0) { 378c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 379c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 380c24d5e0bSIgor Mammedov return; 381c24d5e0bSIgor Mammedov } 382c24d5e0bSIgor Mammedov 383c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 3840058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 385db4728e6SMichael S. Tsirkin } 386db4728e6SMichael S. Tsirkin 387db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 388db4728e6SMichael S. Tsirkin { 389db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 390db4728e6SMichael S. Tsirkin uint32_t val = 0; 391db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 392db4728e6SMichael S. Tsirkin 393fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 394db4728e6SMichael S. Tsirkin return 0; 395db4728e6SMichael S. Tsirkin } 396db4728e6SMichael S. Tsirkin 397db4728e6SMichael S. Tsirkin switch (addr) { 398a7b613cfSIgor Mammedov case PCI_UP_BASE: 3995a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 40099d09dd3SIgor Mammedov if (!s->legacy_piix) { 4015a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 40299d09dd3SIgor Mammedov } 403df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 404db4728e6SMichael S. Tsirkin break; 405a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 406db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 407df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 408db4728e6SMichael S. Tsirkin break; 409a7b613cfSIgor Mammedov case PCI_EJ_BASE: 410df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 411db4728e6SMichael S. Tsirkin break; 412a7b613cfSIgor Mammedov case PCI_RMV_BASE: 413db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 414df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 415db4728e6SMichael S. Tsirkin break; 416a7b613cfSIgor Mammedov case PCI_SEL_BASE: 417db4728e6SMichael S. Tsirkin val = s->hotplug_select; 418df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 419b32bd763SIgor Mammedov break; 420b32bd763SIgor Mammedov case PCI_AIDX_BASE: 421b32bd763SIgor Mammedov val = s->acpi_index; 422b32bd763SIgor Mammedov s->acpi_index = 0; 423b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_read(val); 424b32bd763SIgor Mammedov break; 425db4728e6SMichael S. Tsirkin default: 426db4728e6SMichael S. Tsirkin break; 427db4728e6SMichael S. Tsirkin } 428db4728e6SMichael S. Tsirkin 429db4728e6SMichael S. Tsirkin return val; 430db4728e6SMichael S. Tsirkin } 431db4728e6SMichael S. Tsirkin 432db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 433db4728e6SMichael S. Tsirkin unsigned int size) 434db4728e6SMichael S. Tsirkin { 435b32bd763SIgor Mammedov int slot; 436b32bd763SIgor Mammedov PCIBus *bus; 437b32bd763SIgor Mammedov BusChild *kid, *next; 438db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 439b32bd763SIgor Mammedov 440b32bd763SIgor Mammedov s->acpi_index = 0; 441db4728e6SMichael S. Tsirkin switch (addr) { 442b32bd763SIgor Mammedov case PCI_AIDX_BASE: 443b32bd763SIgor Mammedov /* 444b32bd763SIgor Mammedov * fetch acpi-index for specified slot so that follow up read from 445b32bd763SIgor Mammedov * PCI_AIDX_BASE can return it to guest 446b32bd763SIgor Mammedov */ 447b32bd763SIgor Mammedov slot = ctz32(data); 448b32bd763SIgor Mammedov 449b32bd763SIgor Mammedov if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 450b32bd763SIgor Mammedov break; 451b32bd763SIgor Mammedov } 452b32bd763SIgor Mammedov 453b32bd763SIgor Mammedov bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); 454b32bd763SIgor Mammedov QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 455b32bd763SIgor Mammedov Object *o = OBJECT(kid->child); 456b32bd763SIgor Mammedov PCIDevice *dev = PCI_DEVICE(o); 457b32bd763SIgor Mammedov if (PCI_SLOT(dev->devfn) == slot) { 458b32bd763SIgor Mammedov s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); 459b32bd763SIgor Mammedov break; 460b32bd763SIgor Mammedov } 461b32bd763SIgor Mammedov } 462b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); 463b32bd763SIgor Mammedov break; 464a7b613cfSIgor Mammedov case PCI_EJ_BASE: 465db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 466db4728e6SMichael S. Tsirkin break; 467db4728e6SMichael S. Tsirkin } 468db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 469df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 470db4728e6SMichael S. Tsirkin break; 471a7b613cfSIgor Mammedov case PCI_SEL_BASE: 472f5855994SAnthony PERARD s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; 473df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 474db4728e6SMichael S. Tsirkin default: 475db4728e6SMichael S. Tsirkin break; 476db4728e6SMichael S. Tsirkin } 477db4728e6SMichael S. Tsirkin } 478db4728e6SMichael S. Tsirkin 479db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 480db4728e6SMichael S. Tsirkin .read = pci_read, 481db4728e6SMichael S. Tsirkin .write = pci_write, 482db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 483db4728e6SMichael S. Tsirkin .valid = { 484db4728e6SMichael S. Tsirkin .min_access_size = 4, 485db4728e6SMichael S. Tsirkin .max_access_size = 4, 486db4728e6SMichael S. Tsirkin }, 487db4728e6SMichael S. Tsirkin }; 488db4728e6SMichael S. Tsirkin 48978c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 490*caf108bcSJulia Suvorova MemoryRegion *address_space_io, bool bridges_enabled, 491*caf108bcSJulia Suvorova uint16_t io_base) 492db4728e6SMichael S. Tsirkin { 49378c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 494*caf108bcSJulia Suvorova s->io_base = io_base; 495e358edc8SIgor Mammedov 496db4728e6SMichael S. Tsirkin s->root = root_bus; 49799d09dd3SIgor Mammedov s->legacy_piix = !bridges_enabled; 498e358edc8SIgor Mammedov 49978c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 50078c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 50178c2d872SIgor Mammedov memory_region_add_subregion(address_space_io, s->io_base, &s->io); 50278c2d872SIgor Mammedov 50378c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 504d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 50578c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 506d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 507db4728e6SMichael S. Tsirkin } 508db4728e6SMichael S. Tsirkin 509b32bd763SIgor Mammedov bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id) 510b32bd763SIgor Mammedov { 511b32bd763SIgor Mammedov AcpiPciHpState *s = opaque; 512b32bd763SIgor Mammedov return s->acpi_index; 513b32bd763SIgor Mammedov } 514b32bd763SIgor Mammedov 515db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = { 516db4728e6SMichael S. Tsirkin .name = "acpi_pcihp_pci_status", 517db4728e6SMichael S. Tsirkin .version_id = 1, 518db4728e6SMichael S. Tsirkin .minimum_version_id = 1, 519db4728e6SMichael S. Tsirkin .fields = (VMStateField[]) { 520db4728e6SMichael S. Tsirkin VMSTATE_UINT32(up, AcpiPciHpPciStatus), 521db4728e6SMichael S. Tsirkin VMSTATE_UINT32(down, AcpiPciHpPciStatus), 522db4728e6SMichael S. Tsirkin VMSTATE_END_OF_LIST() 523db4728e6SMichael S. Tsirkin } 524db4728e6SMichael S. Tsirkin }; 525