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" 33*c0e427d6SJulia Suvorova #include "hw/pci/pci_host.h" 34*c0e427d6SJulia Suvorova #include "hw/i386/acpi-build.h" 35db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h" 36db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h" 37d6454270SMarkus Armbruster #include "migration/vmstate.h" 38da34e65cSMarkus Armbruster #include "qapi/error.h" 39db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h" 40df93b194SMarkus Armbruster #include "trace.h" 41db4728e6SMichael S. Tsirkin 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 554fd7da4cSIgor Mammedov static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data) 564fd7da4cSIgor Mammedov { 574fd7da4cSIgor Mammedov return a - b; 584fd7da4cSIgor Mammedov } 594fd7da4cSIgor Mammedov 604fd7da4cSIgor Mammedov static GSequence *pci_acpi_index_list(void) 614fd7da4cSIgor Mammedov { 624fd7da4cSIgor Mammedov static GSequence *used_acpi_index_list; 634fd7da4cSIgor Mammedov 644fd7da4cSIgor Mammedov if (!used_acpi_index_list) { 654fd7da4cSIgor Mammedov used_acpi_index_list = g_sequence_new(NULL); 664fd7da4cSIgor Mammedov } 674fd7da4cSIgor Mammedov return used_acpi_index_list; 684fd7da4cSIgor Mammedov } 694fd7da4cSIgor 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; 108*c0e427d6SJulia Suvorova Object *host = acpi_get_i386_pci_host(); 109ab938ae4SAnthony PERARD PCIBus *bus; 110ab938ae4SAnthony PERARD unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT; 111ab938ae4SAnthony PERARD 112ab938ae4SAnthony PERARD if (bsel_is_set) { 113ab938ae4SAnthony PERARD return; 114ab938ae4SAnthony PERARD } 115ab938ae4SAnthony PERARD bsel_is_set = true; 116ab938ae4SAnthony PERARD 117*c0e427d6SJulia Suvorova if (!host) { 118*c0e427d6SJulia Suvorova return; 119*c0e427d6SJulia Suvorova } 120*c0e427d6SJulia Suvorova 121*c0e427d6SJulia Suvorova bus = PCI_HOST_BRIDGE(host)->bus; 122ab938ae4SAnthony PERARD if (bus) { 123ab938ae4SAnthony PERARD /* Scan all PCI buses. Set property to enable acpi based hotplug. */ 124ab938ae4SAnthony PERARD pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc); 125ab938ae4SAnthony PERARD } 126ab938ae4SAnthony PERARD } 127ab938ae4SAnthony PERARD 1283d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void) 1293d7e78aaSAni Sinha { 1303d7e78aaSAni Sinha static bool root_hp_disabled; 131*c0e427d6SJulia Suvorova Object *host = acpi_get_i386_pci_host(); 1323d7e78aaSAni Sinha PCIBus *bus; 1333d7e78aaSAni Sinha 1343d7e78aaSAni Sinha if (root_hp_disabled) { 1353d7e78aaSAni Sinha return; 1363d7e78aaSAni Sinha } 1373d7e78aaSAni Sinha 138*c0e427d6SJulia Suvorova bus = PCI_HOST_BRIDGE(host)->bus; 1393d7e78aaSAni Sinha if (bus) { 1403d7e78aaSAni Sinha /* setting the hotplug handler to NULL makes the bus non-hotpluggable */ 1413d7e78aaSAni Sinha qbus_set_hotplug_handler(BUS(bus), NULL); 1423d7e78aaSAni Sinha } 1433d7e78aaSAni Sinha root_hp_disabled = true; 1443d7e78aaSAni Sinha return; 1453d7e78aaSAni Sinha } 1463d7e78aaSAni Sinha 147db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque) 148db4728e6SMichael S. Tsirkin { 149db4728e6SMichael S. Tsirkin AcpiPciHpFind *find = opaque; 150db4728e6SMichael S. Tsirkin if (find->bsel == acpi_pcihp_get_bsel(bus)) { 151db4728e6SMichael S. Tsirkin find->bus = bus; 152db4728e6SMichael S. Tsirkin } 153db4728e6SMichael S. Tsirkin } 154db4728e6SMichael S. Tsirkin 155db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel) 156db4728e6SMichael S. Tsirkin { 157db4728e6SMichael S. Tsirkin AcpiPciHpFind find = { .bsel = bsel, .bus = NULL }; 158db4728e6SMichael S. Tsirkin 159db4728e6SMichael S. Tsirkin if (bsel < 0) { 160db4728e6SMichael S. Tsirkin return NULL; 161db4728e6SMichael S. Tsirkin } 162db4728e6SMichael S. Tsirkin 163db4728e6SMichael S. Tsirkin pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find); 164db4728e6SMichael S. Tsirkin 165db4728e6SMichael S. Tsirkin /* Make bsel 0 eject root bus if bsel property is not set, 166db4728e6SMichael S. Tsirkin * for compatibility with non acpi setups. 167db4728e6SMichael S. Tsirkin * TODO: really needed? 168db4728e6SMichael S. Tsirkin */ 169db4728e6SMichael S. Tsirkin if (!bsel && !find.bus) { 170db4728e6SMichael S. Tsirkin find.bus = s->root; 171db4728e6SMichael S. Tsirkin } 1728ad038abSAni Sinha 1738ad038abSAni Sinha /* 1748ad038abSAni Sinha * Check if find.bus is actually hotpluggable. If bsel is set to 1758ad038abSAni Sinha * NULL for example on the root bus in order to make it 1768ad038abSAni Sinha * non-hotpluggable, find.bus will match the root bus when bsel 1778ad038abSAni Sinha * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the 1788ad038abSAni Sinha * bus is not hotpluggable however, we should not select the bus. 1798ad038abSAni Sinha * Instead, we should set find.bus to NULL in that case. In the check 1808ad038abSAni Sinha * below, we generalize this case for all buses, not just the root bus. 1818ad038abSAni Sinha * The callers of this function check for a null return value and 1828ad038abSAni Sinha * handle them appropriately. 1838ad038abSAni Sinha */ 1848ad038abSAni Sinha if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) { 1858ad038abSAni Sinha find.bus = NULL; 1868ad038abSAni Sinha } 187db4728e6SMichael S. Tsirkin return find.bus; 188db4728e6SMichael S. Tsirkin } 189db4728e6SMichael S. Tsirkin 190db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev) 191db4728e6SMichael S. Tsirkin { 192db4728e6SMichael S. Tsirkin PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev); 1932897ae02SIgor Mammedov DeviceClass *dc = DEVICE_GET_CLASS(dev); 194db4728e6SMichael S. Tsirkin /* 195db4728e6SMichael S. Tsirkin * ACPI doesn't allow hotplug of bridge devices. Don't allow 196db4728e6SMichael S. Tsirkin * hot-unplug of bridge devices unless they were added by hotplug 197db4728e6SMichael S. Tsirkin * (and so, not described by acpi). 198db4728e6SMichael S. Tsirkin */ 1992897ae02SIgor Mammedov return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable; 200db4728e6SMichael S. Tsirkin } 201db4728e6SMichael S. Tsirkin 202db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots) 203db4728e6SMichael S. Tsirkin { 204c97adf3cSDavid Hildenbrand HotplugHandler *hotplug_ctrl; 205db4728e6SMichael S. Tsirkin BusChild *kid, *next; 206786a4ea8SStefan Hajnoczi int slot = ctz32(slots); 207db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 208db4728e6SMichael S. Tsirkin 20903459ea3SMarkus Armbruster trace_acpi_pci_eject_slot(bsel, slot); 21003459ea3SMarkus Armbruster 211a3ec4bb7SIgor Mammedov if (!bus || slot > 31) { 212db4728e6SMichael S. Tsirkin return; 213db4728e6SMichael S. Tsirkin } 214db4728e6SMichael S. Tsirkin 215db4728e6SMichael S. Tsirkin /* Mark request as complete */ 216db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot); 2175a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot); 218db4728e6SMichael S. Tsirkin 219db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 220db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 221db4728e6SMichael S. Tsirkin PCIDevice *dev = PCI_DEVICE(qdev); 222db4728e6SMichael S. Tsirkin if (PCI_SLOT(dev->devfn) == slot) { 2235a2223caSMichael S. Tsirkin if (!acpi_pcihp_pc_no_hotplug(s, dev)) { 224c97adf3cSDavid Hildenbrand hotplug_ctrl = qdev_get_hotplug_handler(qdev); 225c97adf3cSDavid Hildenbrand hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort); 22607578b0aSDavid Hildenbrand object_unparent(OBJECT(qdev)); 227db4728e6SMichael S. Tsirkin } 228db4728e6SMichael S. Tsirkin } 229db4728e6SMichael S. Tsirkin } 230db4728e6SMichael S. Tsirkin } 231db4728e6SMichael S. Tsirkin 232db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel) 233db4728e6SMichael S. Tsirkin { 234db4728e6SMichael S. Tsirkin BusChild *kid, *next; 235db4728e6SMichael S. Tsirkin PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel); 236db4728e6SMichael S. Tsirkin 237db4728e6SMichael S. Tsirkin /* Execute any pending removes during reset */ 238db4728e6SMichael S. Tsirkin while (s->acpi_pcihp_pci_status[bsel].down) { 239db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down); 240db4728e6SMichael S. Tsirkin } 241db4728e6SMichael S. Tsirkin 242db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0; 243db4728e6SMichael S. Tsirkin 244db4728e6SMichael S. Tsirkin if (!bus) { 245db4728e6SMichael S. Tsirkin return; 246db4728e6SMichael S. Tsirkin } 247db4728e6SMichael S. Tsirkin QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 248db4728e6SMichael S. Tsirkin DeviceState *qdev = kid->child; 249db4728e6SMichael S. Tsirkin PCIDevice *pdev = PCI_DEVICE(qdev); 250db4728e6SMichael S. Tsirkin int slot = PCI_SLOT(pdev->devfn); 251db4728e6SMichael S. Tsirkin 252db4728e6SMichael S. Tsirkin if (acpi_pcihp_pc_no_hotplug(s, pdev)) { 253db4728e6SMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot); 254db4728e6SMichael S. Tsirkin } 255db4728e6SMichael S. Tsirkin } 256db4728e6SMichael S. Tsirkin } 257db4728e6SMichael S. Tsirkin 258db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s) 259db4728e6SMichael S. Tsirkin { 260db4728e6SMichael S. Tsirkin int i; 261db4728e6SMichael S. Tsirkin 262db4728e6SMichael S. Tsirkin for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) { 263db4728e6SMichael S. Tsirkin acpi_pcihp_update_hotplug_bus(s, i); 264db4728e6SMichael S. Tsirkin } 265db4728e6SMichael S. Tsirkin } 266db4728e6SMichael S. Tsirkin 2673d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off) 268db4728e6SMichael S. Tsirkin { 2693d7e78aaSAni Sinha if (acpihp_root_off) { 2703d7e78aaSAni Sinha acpi_pcihp_disable_root_bus(); 2713d7e78aaSAni Sinha } 272ab938ae4SAnthony PERARD acpi_set_pci_info(); 273db4728e6SMichael S. Tsirkin acpi_pcihp_update(s); 274db4728e6SMichael S. Tsirkin } 275db4728e6SMichael S. Tsirkin 276b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1) 277b32bd763SIgor Mammedov 278ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev, 279ec266f40SDavid Hildenbrand DeviceState *dev, Error **errp) 280ec266f40SDavid Hildenbrand { 281b32bd763SIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 282b32bd763SIgor Mammedov 283ec266f40SDavid Hildenbrand /* Only hotplugged devices need the hotplug capability. */ 284ec266f40SDavid Hildenbrand if (dev->hotplugged && 285ec266f40SDavid Hildenbrand acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) { 286ec266f40SDavid Hildenbrand error_setg(errp, "Unsupported bus. Bus doesn't have property '" 287ec266f40SDavid Hildenbrand ACPI_PCIHP_PROP_BSEL "' set"); 288ec266f40SDavid Hildenbrand return; 289ec266f40SDavid Hildenbrand } 290b32bd763SIgor Mammedov 291b32bd763SIgor Mammedov /* 292b32bd763SIgor Mammedov * capped by systemd (see: udev-builtin-net_id.c) 293b32bd763SIgor Mammedov * as it's the only known user honor it to avoid users 294b32bd763SIgor Mammedov * misconfigure QEMU and then wonder why acpi-index doesn't work 295b32bd763SIgor Mammedov */ 296b32bd763SIgor Mammedov if (pdev->acpi_index > ONBOARD_INDEX_MAX) { 297b32bd763SIgor Mammedov error_setg(errp, "acpi-index should be less or equal to %u", 298b32bd763SIgor Mammedov ONBOARD_INDEX_MAX); 299b32bd763SIgor Mammedov return; 300b32bd763SIgor Mammedov } 3014fd7da4cSIgor Mammedov 3024fd7da4cSIgor Mammedov /* 3034fd7da4cSIgor Mammedov * make sure that acpi-index is unique across all present PCI devices 3044fd7da4cSIgor Mammedov */ 3054fd7da4cSIgor Mammedov if (pdev->acpi_index) { 3064fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 3074fd7da4cSIgor Mammedov 3084fd7da4cSIgor Mammedov if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index), 3094fd7da4cSIgor Mammedov g_cmp_uint32, NULL)) { 3104fd7da4cSIgor Mammedov error_setg(errp, "a PCI device with acpi-index = %" PRIu32 3114fd7da4cSIgor Mammedov " already exist", pdev->acpi_index); 3124fd7da4cSIgor Mammedov return; 3134fd7da4cSIgor Mammedov } 3144fd7da4cSIgor Mammedov g_sequence_insert_sorted(used_indexes, 3154fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 3164fd7da4cSIgor Mammedov g_cmp_uint32, NULL); 3174fd7da4cSIgor Mammedov } 318ec266f40SDavid Hildenbrand } 319ec266f40SDavid Hildenbrand 3200058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 321c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 322db4728e6SMichael S. Tsirkin { 323c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 324c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 325ec266f40SDavid Hildenbrand int bsel; 326db4728e6SMichael S. Tsirkin 327db4728e6SMichael S. Tsirkin /* Don't send event when device is enabled during qemu machine creation: 328db4728e6SMichael S. Tsirkin * it is present on boot, no hotplug event is necessary. We do send an 329db4728e6SMichael S. Tsirkin * event when the device is disabled later. */ 330c24d5e0bSIgor Mammedov if (!dev->hotplugged) { 3313e520926SDavid Hildenbrand /* 3323e520926SDavid Hildenbrand * Overwrite the default hotplug handler with the ACPI PCI one 3333e520926SDavid Hildenbrand * for cold plugged bridges only. 3343e520926SDavid Hildenbrand */ 3353e520926SDavid Hildenbrand if (!s->legacy_piix && 3363e520926SDavid Hildenbrand object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) { 3373e520926SDavid Hildenbrand PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev)); 3383e520926SDavid Hildenbrand 3399bc6bfdfSMarkus Armbruster qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev)); 3403e520926SDavid Hildenbrand /* We don't have to overwrite any other hotplug handler yet */ 3413e520926SDavid Hildenbrand assert(QLIST_EMPTY(&sec->child)); 3423e520926SDavid Hildenbrand } 3433e520926SDavid Hildenbrand 344c24d5e0bSIgor Mammedov return; 345db4728e6SMichael S. Tsirkin } 346db4728e6SMichael S. Tsirkin 347ec266f40SDavid Hildenbrand bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 348ec266f40SDavid Hildenbrand g_assert(bsel >= 0); 3498f5001f9SIgor Mammedov s->acpi_pcihp_pci_status[bsel].up |= (1U << slot); 3500058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 351db4728e6SMichael S. Tsirkin } 352db4728e6SMichael S. Tsirkin 3530058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s, 354c24d5e0bSIgor Mammedov DeviceState *dev, Error **errp) 355c24d5e0bSIgor Mammedov { 3564fd7da4cSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 3574fd7da4cSIgor Mammedov 35803459ea3SMarkus Armbruster trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn), 35903459ea3SMarkus Armbruster acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev)))); 3604fd7da4cSIgor Mammedov 3614fd7da4cSIgor Mammedov /* 3624fd7da4cSIgor Mammedov * clean up acpi-index so it could reused by another device 3634fd7da4cSIgor Mammedov */ 3644fd7da4cSIgor Mammedov if (pdev->acpi_index) { 3654fd7da4cSIgor Mammedov GSequence *used_indexes = pci_acpi_index_list(); 3664fd7da4cSIgor Mammedov 3674fd7da4cSIgor Mammedov g_sequence_remove(g_sequence_lookup(used_indexes, 3684fd7da4cSIgor Mammedov GINT_TO_POINTER(pdev->acpi_index), 3694fd7da4cSIgor Mammedov g_cmp_uint32, NULL)); 3704fd7da4cSIgor Mammedov } 3714fd7da4cSIgor Mammedov 372981c3dcdSMarkus Armbruster qdev_unrealize(dev); 373c97adf3cSDavid Hildenbrand } 374c97adf3cSDavid Hildenbrand 375c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev, 376c97adf3cSDavid Hildenbrand AcpiPciHpState *s, DeviceState *dev, 377c97adf3cSDavid Hildenbrand Error **errp) 378c97adf3cSDavid Hildenbrand { 379c24d5e0bSIgor Mammedov PCIDevice *pdev = PCI_DEVICE(dev); 380c24d5e0bSIgor Mammedov int slot = PCI_SLOT(pdev->devfn); 381fd56e061SDavid Gibson int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev)); 38203459ea3SMarkus Armbruster 38303459ea3SMarkus Armbruster trace_acpi_pci_unplug_request(bsel, slot); 38403459ea3SMarkus Armbruster 385c24d5e0bSIgor Mammedov if (bsel < 0) { 386c24d5e0bSIgor Mammedov error_setg(errp, "Unsupported bus. Bus doesn't have property '" 387c24d5e0bSIgor Mammedov ACPI_PCIHP_PROP_BSEL "' set"); 388c24d5e0bSIgor Mammedov return; 389c24d5e0bSIgor Mammedov } 390c24d5e0bSIgor Mammedov 391c24d5e0bSIgor Mammedov s->acpi_pcihp_pci_status[bsel].down |= (1U << slot); 3920058c082SIgor Mammedov acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS); 393db4728e6SMichael S. Tsirkin } 394db4728e6SMichael S. Tsirkin 395db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size) 396db4728e6SMichael S. Tsirkin { 397db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 398db4728e6SMichael S. Tsirkin uint32_t val = 0; 399db4728e6SMichael S. Tsirkin int bsel = s->hotplug_select; 400db4728e6SMichael S. Tsirkin 401fa365d7cSGonglei if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 402db4728e6SMichael S. Tsirkin return 0; 403db4728e6SMichael S. Tsirkin } 404db4728e6SMichael S. Tsirkin 405db4728e6SMichael S. Tsirkin switch (addr) { 406a7b613cfSIgor Mammedov case PCI_UP_BASE: 4075a2223caSMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].up; 40899d09dd3SIgor Mammedov if (!s->legacy_piix) { 4095a2223caSMichael S. Tsirkin s->acpi_pcihp_pci_status[bsel].up = 0; 41099d09dd3SIgor Mammedov } 411df93b194SMarkus Armbruster trace_acpi_pci_up_read(val); 412db4728e6SMichael S. Tsirkin break; 413a7b613cfSIgor Mammedov case PCI_DOWN_BASE: 414db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].down; 415df93b194SMarkus Armbruster trace_acpi_pci_down_read(val); 416db4728e6SMichael S. Tsirkin break; 417a7b613cfSIgor Mammedov case PCI_EJ_BASE: 418df93b194SMarkus Armbruster trace_acpi_pci_features_read(val); 419db4728e6SMichael S. Tsirkin break; 420a7b613cfSIgor Mammedov case PCI_RMV_BASE: 421db4728e6SMichael S. Tsirkin val = s->acpi_pcihp_pci_status[bsel].hotplug_enable; 422df93b194SMarkus Armbruster trace_acpi_pci_rmv_read(val); 423db4728e6SMichael S. Tsirkin break; 424a7b613cfSIgor Mammedov case PCI_SEL_BASE: 425db4728e6SMichael S. Tsirkin val = s->hotplug_select; 426df93b194SMarkus Armbruster trace_acpi_pci_sel_read(val); 427b32bd763SIgor Mammedov break; 428b32bd763SIgor Mammedov case PCI_AIDX_BASE: 429b32bd763SIgor Mammedov val = s->acpi_index; 430b32bd763SIgor Mammedov s->acpi_index = 0; 431b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_read(val); 432b32bd763SIgor Mammedov break; 433db4728e6SMichael S. Tsirkin default: 434db4728e6SMichael S. Tsirkin break; 435db4728e6SMichael S. Tsirkin } 436db4728e6SMichael S. Tsirkin 437db4728e6SMichael S. Tsirkin return val; 438db4728e6SMichael S. Tsirkin } 439db4728e6SMichael S. Tsirkin 440db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data, 441db4728e6SMichael S. Tsirkin unsigned int size) 442db4728e6SMichael S. Tsirkin { 443b32bd763SIgor Mammedov int slot; 444b32bd763SIgor Mammedov PCIBus *bus; 445b32bd763SIgor Mammedov BusChild *kid, *next; 446db4728e6SMichael S. Tsirkin AcpiPciHpState *s = opaque; 447b32bd763SIgor Mammedov 448b32bd763SIgor Mammedov s->acpi_index = 0; 449db4728e6SMichael S. Tsirkin switch (addr) { 450b32bd763SIgor Mammedov case PCI_AIDX_BASE: 451b32bd763SIgor Mammedov /* 452b32bd763SIgor Mammedov * fetch acpi-index for specified slot so that follow up read from 453b32bd763SIgor Mammedov * PCI_AIDX_BASE can return it to guest 454b32bd763SIgor Mammedov */ 455b32bd763SIgor Mammedov slot = ctz32(data); 456b32bd763SIgor Mammedov 457b32bd763SIgor Mammedov if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 458b32bd763SIgor Mammedov break; 459b32bd763SIgor Mammedov } 460b32bd763SIgor Mammedov 461b32bd763SIgor Mammedov bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select); 462b32bd763SIgor Mammedov QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) { 463b32bd763SIgor Mammedov Object *o = OBJECT(kid->child); 464b32bd763SIgor Mammedov PCIDevice *dev = PCI_DEVICE(o); 465b32bd763SIgor Mammedov if (PCI_SLOT(dev->devfn) == slot) { 466b32bd763SIgor Mammedov s->acpi_index = object_property_get_uint(o, "acpi-index", NULL); 467b32bd763SIgor Mammedov break; 468b32bd763SIgor Mammedov } 469b32bd763SIgor Mammedov } 470b32bd763SIgor Mammedov trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index); 471b32bd763SIgor Mammedov break; 472a7b613cfSIgor Mammedov case PCI_EJ_BASE: 473db4728e6SMichael S. Tsirkin if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) { 474db4728e6SMichael S. Tsirkin break; 475db4728e6SMichael S. Tsirkin } 476db4728e6SMichael S. Tsirkin acpi_pcihp_eject_slot(s, s->hotplug_select, data); 477df93b194SMarkus Armbruster trace_acpi_pci_ej_write(addr, data); 478db4728e6SMichael S. Tsirkin break; 479a7b613cfSIgor Mammedov case PCI_SEL_BASE: 480f5855994SAnthony PERARD s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data; 481df93b194SMarkus Armbruster trace_acpi_pci_sel_write(addr, data); 482db4728e6SMichael S. Tsirkin default: 483db4728e6SMichael S. Tsirkin break; 484db4728e6SMichael S. Tsirkin } 485db4728e6SMichael S. Tsirkin } 486db4728e6SMichael S. Tsirkin 487db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = { 488db4728e6SMichael S. Tsirkin .read = pci_read, 489db4728e6SMichael S. Tsirkin .write = pci_write, 490db4728e6SMichael S. Tsirkin .endianness = DEVICE_LITTLE_ENDIAN, 491db4728e6SMichael S. Tsirkin .valid = { 492db4728e6SMichael S. Tsirkin .min_access_size = 4, 493db4728e6SMichael S. Tsirkin .max_access_size = 4, 494db4728e6SMichael S. Tsirkin }, 495db4728e6SMichael S. Tsirkin }; 496db4728e6SMichael S. Tsirkin 49778c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus, 498caf108bcSJulia Suvorova MemoryRegion *address_space_io, bool bridges_enabled, 499caf108bcSJulia Suvorova uint16_t io_base) 500db4728e6SMichael S. Tsirkin { 50178c2d872SIgor Mammedov s->io_len = ACPI_PCIHP_SIZE; 502caf108bcSJulia Suvorova s->io_base = io_base; 503e358edc8SIgor Mammedov 504db4728e6SMichael S. Tsirkin s->root = root_bus; 50599d09dd3SIgor Mammedov s->legacy_piix = !bridges_enabled; 506e358edc8SIgor Mammedov 50778c2d872SIgor Mammedov memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s, 50878c2d872SIgor Mammedov "acpi-pci-hotplug", s->io_len); 50978c2d872SIgor Mammedov memory_region_add_subregion(address_space_io, s->io_base, &s->io); 51078c2d872SIgor Mammedov 51178c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base, 512d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 51378c2d872SIgor Mammedov object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len, 514d2623129SMarkus Armbruster OBJ_PROP_FLAG_READ); 515db4728e6SMichael S. Tsirkin } 516db4728e6SMichael S. Tsirkin 517b32bd763SIgor Mammedov bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id) 518b32bd763SIgor Mammedov { 519b32bd763SIgor Mammedov AcpiPciHpState *s = opaque; 520b32bd763SIgor Mammedov return s->acpi_index; 521b32bd763SIgor Mammedov } 522b32bd763SIgor Mammedov 523db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = { 524db4728e6SMichael S. Tsirkin .name = "acpi_pcihp_pci_status", 525db4728e6SMichael S. Tsirkin .version_id = 1, 526db4728e6SMichael S. Tsirkin .minimum_version_id = 1, 527db4728e6SMichael S. Tsirkin .fields = (VMStateField[]) { 528db4728e6SMichael S. Tsirkin VMSTATE_UINT32(up, AcpiPciHpPciStatus), 529db4728e6SMichael S. Tsirkin VMSTATE_UINT32(down, AcpiPciHpPciStatus), 530db4728e6SMichael S. Tsirkin VMSTATE_END_OF_LIST() 531db4728e6SMichael S. Tsirkin } 532db4728e6SMichael S. Tsirkin }; 533