xref: /openbmc/qemu/hw/acpi/pcihp.c (revision ad4942746cb428f649f854e1d13622a745f603a5)
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"
33c0e427d6SJulia Suvorova #include "hw/pci/pci_host.h"
343f3cbbb2SJulia Suvorova #include "hw/pci/pcie_port.h"
356b0969f1SIgor Mammedov #include "hw/pci-bridge/xio3130_downstream.h"
36c0e427d6SJulia Suvorova #include "hw/i386/acpi-build.h"
37db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h"
38db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h"
39d6454270SMarkus Armbruster #include "migration/vmstate.h"
40da34e65cSMarkus Armbruster #include "qapi/error.h"
41db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h"
42df93b194SMarkus Armbruster #include "trace.h"
43db4728e6SMichael S. Tsirkin 
44b32bd763SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0018
45a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000
46a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004
47a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008
48a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c
49a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010
50b32bd763SIgor Mammedov #define PCI_AIDX_BASE 0x0014
51db4728e6SMichael S. Tsirkin 
52db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind {
53db4728e6SMichael S. Tsirkin     int bsel;
54db4728e6SMichael S. Tsirkin     PCIBus *bus;
55db4728e6SMichael S. Tsirkin } AcpiPciHpFind;
56db4728e6SMichael S. Tsirkin 
574fd7da4cSIgor Mammedov static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data)
584fd7da4cSIgor Mammedov {
594fd7da4cSIgor Mammedov     return a - b;
604fd7da4cSIgor Mammedov }
614fd7da4cSIgor Mammedov 
624fd7da4cSIgor Mammedov static GSequence *pci_acpi_index_list(void)
634fd7da4cSIgor Mammedov {
644fd7da4cSIgor Mammedov     static GSequence *used_acpi_index_list;
654fd7da4cSIgor Mammedov 
664fd7da4cSIgor Mammedov     if (!used_acpi_index_list) {
674fd7da4cSIgor Mammedov         used_acpi_index_list = g_sequence_new(NULL);
684fd7da4cSIgor Mammedov     }
694fd7da4cSIgor Mammedov     return used_acpi_index_list;
704fd7da4cSIgor Mammedov }
714fd7da4cSIgor Mammedov 
72db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus)
73db4728e6SMichael S. Tsirkin {
747c38ecd0SKirill Batuzov     Error *local_err = NULL;
75c03d83d5SMarc-André Lureau     uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
767c38ecd0SKirill Batuzov                                              &local_err);
777c38ecd0SKirill Batuzov 
78c03d83d5SMarc-André Lureau     if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
797c38ecd0SKirill Batuzov         if (local_err) {
807c38ecd0SKirill Batuzov             error_free(local_err);
81db4728e6SMichael S. Tsirkin         }
82db4728e6SMichael S. Tsirkin         return -1;
837c38ecd0SKirill Batuzov     } else {
84db4728e6SMichael S. Tsirkin         return bsel;
85db4728e6SMichael S. Tsirkin     }
867c38ecd0SKirill Batuzov }
87db4728e6SMichael S. Tsirkin 
88ab938ae4SAnthony PERARD /* Assign BSEL property to all buses.  In the future, this can be changed
89ab938ae4SAnthony PERARD  * to only assign to buses that support hotplug.
90ab938ae4SAnthony PERARD  */
91ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque)
92ab938ae4SAnthony PERARD {
93ab938ae4SAnthony PERARD     unsigned *bsel_alloc = opaque;
94ab938ae4SAnthony PERARD     unsigned *bus_bsel;
95ab938ae4SAnthony PERARD 
96ab938ae4SAnthony PERARD     if (qbus_is_hotpluggable(BUS(bus))) {
97ab938ae4SAnthony PERARD         bus_bsel = g_malloc(sizeof *bus_bsel);
98ab938ae4SAnthony PERARD 
99ab938ae4SAnthony PERARD         *bus_bsel = (*bsel_alloc)++;
100ab938ae4SAnthony PERARD         object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
101d2623129SMarkus Armbruster                                        bus_bsel, OBJ_PROP_FLAG_READ);
102ab938ae4SAnthony PERARD     }
103ab938ae4SAnthony PERARD 
104ab938ae4SAnthony PERARD     return bsel_alloc;
105ab938ae4SAnthony PERARD }
106ab938ae4SAnthony PERARD 
107ab938ae4SAnthony PERARD static void acpi_set_pci_info(void)
108ab938ae4SAnthony PERARD {
109ab938ae4SAnthony PERARD     static bool bsel_is_set;
110c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
111ab938ae4SAnthony PERARD     PCIBus *bus;
112ab938ae4SAnthony PERARD     unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
113ab938ae4SAnthony PERARD 
114ab938ae4SAnthony PERARD     if (bsel_is_set) {
115ab938ae4SAnthony PERARD         return;
116ab938ae4SAnthony PERARD     }
117ab938ae4SAnthony PERARD     bsel_is_set = true;
118ab938ae4SAnthony PERARD 
119c0e427d6SJulia Suvorova     if (!host) {
120c0e427d6SJulia Suvorova         return;
121c0e427d6SJulia Suvorova     }
122c0e427d6SJulia Suvorova 
123c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
124ab938ae4SAnthony PERARD     if (bus) {
125ab938ae4SAnthony PERARD         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
126ab938ae4SAnthony PERARD         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
127ab938ae4SAnthony PERARD     }
128ab938ae4SAnthony PERARD }
129ab938ae4SAnthony PERARD 
1303d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void)
1313d7e78aaSAni Sinha {
132c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
1333d7e78aaSAni Sinha     PCIBus *bus;
1343d7e78aaSAni Sinha 
135c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
13678480268SAni Sinha     if (bus && qbus_is_hotpluggable(BUS(bus))) {
1373d7e78aaSAni Sinha         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
1383d7e78aaSAni Sinha         qbus_set_hotplug_handler(BUS(bus), NULL);
1393d7e78aaSAni Sinha     }
14078480268SAni Sinha 
1413d7e78aaSAni Sinha     return;
1423d7e78aaSAni Sinha }
1433d7e78aaSAni Sinha 
144db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
145db4728e6SMichael S. Tsirkin {
146db4728e6SMichael S. Tsirkin     AcpiPciHpFind *find = opaque;
147db4728e6SMichael S. Tsirkin     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
148db4728e6SMichael S. Tsirkin         find->bus = bus;
149db4728e6SMichael S. Tsirkin     }
150db4728e6SMichael S. Tsirkin }
151db4728e6SMichael S. Tsirkin 
152db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
153db4728e6SMichael S. Tsirkin {
154db4728e6SMichael S. Tsirkin     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
155db4728e6SMichael S. Tsirkin 
156db4728e6SMichael S. Tsirkin     if (bsel < 0) {
157db4728e6SMichael S. Tsirkin         return NULL;
158db4728e6SMichael S. Tsirkin     }
159db4728e6SMichael S. Tsirkin 
160db4728e6SMichael S. Tsirkin     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
161db4728e6SMichael S. Tsirkin 
162db4728e6SMichael S. Tsirkin     /* Make bsel 0 eject root bus if bsel property is not set,
163db4728e6SMichael S. Tsirkin      * for compatibility with non acpi setups.
164db4728e6SMichael S. Tsirkin      * TODO: really needed?
165db4728e6SMichael S. Tsirkin      */
166db4728e6SMichael S. Tsirkin     if (!bsel && !find.bus) {
167db4728e6SMichael S. Tsirkin         find.bus = s->root;
168db4728e6SMichael S. Tsirkin     }
1698ad038abSAni Sinha 
1708ad038abSAni Sinha     /*
1718ad038abSAni Sinha      * Check if find.bus is actually hotpluggable. If bsel is set to
1728ad038abSAni Sinha      * NULL for example on the root bus in order to make it
1738ad038abSAni Sinha      * non-hotpluggable, find.bus will match the root bus when bsel
1748ad038abSAni Sinha      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
1758ad038abSAni Sinha      * bus is not hotpluggable however, we should not select the bus.
1768ad038abSAni Sinha      * Instead, we should set find.bus to NULL in that case. In the check
1778ad038abSAni Sinha      * below, we generalize this case for all buses, not just the root bus.
1788ad038abSAni Sinha      * The callers of this function check for a null return value and
1798ad038abSAni Sinha      * handle them appropriately.
1808ad038abSAni Sinha      */
1818ad038abSAni Sinha     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
1828ad038abSAni Sinha         find.bus = NULL;
1838ad038abSAni Sinha     }
184db4728e6SMichael S. Tsirkin     return find.bus;
185db4728e6SMichael S. Tsirkin }
186db4728e6SMichael S. Tsirkin 
187db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
188db4728e6SMichael S. Tsirkin {
1892897ae02SIgor Mammedov     DeviceClass *dc = DEVICE_GET_CLASS(dev);
190db4728e6SMichael S. Tsirkin     /*
191db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
192db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
193db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
19458660bfaSŁukasz Gieryk      *
19558660bfaSŁukasz Gieryk      * Don't allow hot-unplug of SR-IOV Virtual Functions, as they
19658660bfaSŁukasz Gieryk      * will be removed implicitly, when Physical Function is unplugged.
197db4728e6SMichael S. Tsirkin      */
198*ad494274SIgor Mammedov     return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable ||
19958660bfaSŁukasz Gieryk            pci_is_vf(dev);
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)) {
2249323f892SLaurent Vivier                 /*
2259323f892SLaurent Vivier                  * partially_hotplugged is used by virtio-net failover:
2269323f892SLaurent Vivier                  * failover has asked the guest OS to unplug the device
2279323f892SLaurent Vivier                  * but we need to keep some references to the device
2289323f892SLaurent Vivier                  * to be able to plug it back in case of failure so
2299323f892SLaurent Vivier                  * we don't execute hotplug_handler_unplug().
2309323f892SLaurent Vivier                  */
2319323f892SLaurent Vivier                 if (dev->partially_hotplugged) {
2329323f892SLaurent Vivier                     /*
2339323f892SLaurent Vivier                      * pending_deleted_event is set to true when
2349323f892SLaurent Vivier                      * virtio-net failover asks to unplug the device,
2359323f892SLaurent Vivier                      * and set to false here when the operation is done
2369323f892SLaurent Vivier                      * This is used by the migration loop to detect the
2379323f892SLaurent Vivier                      * end of the operation and really start the migration.
2389323f892SLaurent Vivier                      */
2399323f892SLaurent Vivier                     qdev->pending_deleted_event = false;
2409323f892SLaurent Vivier                 } else {
241c97adf3cSDavid Hildenbrand                     hotplug_ctrl = qdev_get_hotplug_handler(qdev);
242c97adf3cSDavid Hildenbrand                     hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
24307578b0aSDavid Hildenbrand                     object_unparent(OBJECT(qdev));
244db4728e6SMichael S. Tsirkin                 }
245db4728e6SMichael S. Tsirkin             }
246db4728e6SMichael S. Tsirkin         }
247db4728e6SMichael S. Tsirkin     }
2489323f892SLaurent Vivier }
249db4728e6SMichael S. Tsirkin 
250db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
251db4728e6SMichael S. Tsirkin {
252db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
253db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
254db4728e6SMichael S. Tsirkin 
255db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
256db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
257db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
258db4728e6SMichael S. Tsirkin     }
259db4728e6SMichael S. Tsirkin 
260db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
261db4728e6SMichael S. Tsirkin 
262db4728e6SMichael S. Tsirkin     if (!bus) {
263db4728e6SMichael S. Tsirkin         return;
264db4728e6SMichael S. Tsirkin     }
265db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
266db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
267db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
268db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
269db4728e6SMichael S. Tsirkin 
270db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
271db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
272db4728e6SMichael S. Tsirkin         }
273db4728e6SMichael S. Tsirkin     }
274db4728e6SMichael S. Tsirkin }
275db4728e6SMichael S. Tsirkin 
276db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
277db4728e6SMichael S. Tsirkin {
278db4728e6SMichael S. Tsirkin     int i;
279db4728e6SMichael S. Tsirkin 
280db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
281db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
282db4728e6SMichael S. Tsirkin     }
283db4728e6SMichael S. Tsirkin }
284db4728e6SMichael S. Tsirkin 
2853d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
286db4728e6SMichael S. Tsirkin {
2873d7e78aaSAni Sinha     if (acpihp_root_off) {
2883d7e78aaSAni Sinha         acpi_pcihp_disable_root_bus();
2893d7e78aaSAni Sinha     }
290ab938ae4SAnthony PERARD     acpi_set_pci_info();
291db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
292db4728e6SMichael S. Tsirkin }
293db4728e6SMichael S. Tsirkin 
294b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1)
295b32bd763SIgor Mammedov 
296ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
297ec266f40SDavid Hildenbrand                                    DeviceState *dev, Error **errp)
298ec266f40SDavid Hildenbrand {
299b32bd763SIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
300b32bd763SIgor Mammedov 
301ec266f40SDavid Hildenbrand     /* Only hotplugged devices need the hotplug capability. */
302ec266f40SDavid Hildenbrand     if (dev->hotplugged &&
303028f1a88SAni Sinha         acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
304ec266f40SDavid Hildenbrand         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
305ec266f40SDavid Hildenbrand                    ACPI_PCIHP_PROP_BSEL "' set");
306ec266f40SDavid Hildenbrand         return;
307ec266f40SDavid Hildenbrand     }
308b32bd763SIgor Mammedov 
309b32bd763SIgor Mammedov     /*
310b32bd763SIgor Mammedov      * capped by systemd (see: udev-builtin-net_id.c)
311b32bd763SIgor Mammedov      * as it's the only known user honor it to avoid users
312b32bd763SIgor Mammedov      * misconfigure QEMU and then wonder why acpi-index doesn't work
313b32bd763SIgor Mammedov      */
314b32bd763SIgor Mammedov     if (pdev->acpi_index > ONBOARD_INDEX_MAX) {
315b32bd763SIgor Mammedov         error_setg(errp, "acpi-index should be less or equal to %u",
316b32bd763SIgor Mammedov                    ONBOARD_INDEX_MAX);
317b32bd763SIgor Mammedov         return;
318b32bd763SIgor Mammedov     }
3194fd7da4cSIgor Mammedov 
3204fd7da4cSIgor Mammedov     /*
3214fd7da4cSIgor Mammedov      * make sure that acpi-index is unique across all present PCI devices
3224fd7da4cSIgor Mammedov      */
3234fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
3244fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
3254fd7da4cSIgor Mammedov 
3264fd7da4cSIgor Mammedov         if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index),
3274fd7da4cSIgor Mammedov                               g_cmp_uint32, NULL)) {
3284fd7da4cSIgor Mammedov             error_setg(errp, "a PCI device with acpi-index = %" PRIu32
3294fd7da4cSIgor Mammedov                        " already exist", pdev->acpi_index);
3304fd7da4cSIgor Mammedov             return;
3314fd7da4cSIgor Mammedov         }
3324fd7da4cSIgor Mammedov         g_sequence_insert_sorted(used_indexes,
3334fd7da4cSIgor Mammedov                                  GINT_TO_POINTER(pdev->acpi_index),
3344fd7da4cSIgor Mammedov                                  g_cmp_uint32, NULL);
3354fd7da4cSIgor Mammedov     }
336ec266f40SDavid Hildenbrand }
337ec266f40SDavid Hildenbrand 
3380058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
339c24d5e0bSIgor Mammedov                                DeviceState *dev, Error **errp)
340db4728e6SMichael S. Tsirkin {
341c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
342c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
3436b0969f1SIgor Mammedov     PCIDevice *bridge;
3446b0969f1SIgor Mammedov     PCIBus *bus;
345ec266f40SDavid Hildenbrand     int bsel;
346db4728e6SMichael S. Tsirkin 
347db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
348db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
349db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
350c24d5e0bSIgor Mammedov     if (!dev->hotplugged) {
3513e520926SDavid Hildenbrand         /*
3523e520926SDavid Hildenbrand          * Overwrite the default hotplug handler with the ACPI PCI one
3533e520926SDavid Hildenbrand          * for cold plugged bridges only.
3543e520926SDavid Hildenbrand          */
3553e520926SDavid Hildenbrand         if (!s->legacy_piix &&
3563e520926SDavid Hildenbrand             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
3573e520926SDavid Hildenbrand             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
3583e520926SDavid Hildenbrand 
3593f3cbbb2SJulia Suvorova             /* Remove all hot-plug handlers if hot-plug is disabled on slot */
3603f3cbbb2SJulia Suvorova             if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT) &&
3613f3cbbb2SJulia Suvorova                 !PCIE_SLOT(pdev)->hotplug) {
3623f3cbbb2SJulia Suvorova                 qbus_set_hotplug_handler(BUS(sec), NULL);
3633f3cbbb2SJulia Suvorova                 return;
3643f3cbbb2SJulia Suvorova             }
3653f3cbbb2SJulia Suvorova 
3669bc6bfdfSMarkus Armbruster             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
3673e520926SDavid Hildenbrand             /* We don't have to overwrite any other hotplug handler yet */
3683e520926SDavid Hildenbrand             assert(QLIST_EMPTY(&sec->child));
3693e520926SDavid Hildenbrand         }
3703e520926SDavid Hildenbrand 
371c24d5e0bSIgor Mammedov         return;
372db4728e6SMichael S. Tsirkin     }
373db4728e6SMichael S. Tsirkin 
3746b0969f1SIgor Mammedov     bus = pci_get_bus(pdev);
3756b0969f1SIgor Mammedov     bridge = pci_bridge_get_device(bus);
3766b0969f1SIgor Mammedov     if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) ||
3776b0969f1SIgor Mammedov         object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) {
3786b0969f1SIgor Mammedov         pcie_cap_slot_enable_power(bridge);
3796b0969f1SIgor Mammedov     }
3806b0969f1SIgor Mammedov 
3816b0969f1SIgor Mammedov     bsel = acpi_pcihp_get_bsel(bus);
382ec266f40SDavid Hildenbrand     g_assert(bsel >= 0);
3838f5001f9SIgor Mammedov     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
3840058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
385db4728e6SMichael S. Tsirkin }
386db4728e6SMichael S. Tsirkin 
3870058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
388c24d5e0bSIgor Mammedov                                  DeviceState *dev, Error **errp)
389c24d5e0bSIgor Mammedov {
3904fd7da4cSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
3914fd7da4cSIgor Mammedov 
392028f1a88SAni Sinha     trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
393028f1a88SAni Sinha                           acpi_pcihp_get_bsel(pci_get_bus(pdev)));
3944fd7da4cSIgor Mammedov 
3954fd7da4cSIgor Mammedov     /*
3964fd7da4cSIgor Mammedov      * clean up acpi-index so it could reused by another device
3974fd7da4cSIgor Mammedov      */
3984fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
3994fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
4004fd7da4cSIgor Mammedov 
4014fd7da4cSIgor Mammedov         g_sequence_remove(g_sequence_lookup(used_indexes,
4024fd7da4cSIgor Mammedov                           GINT_TO_POINTER(pdev->acpi_index),
4034fd7da4cSIgor Mammedov                           g_cmp_uint32, NULL));
4044fd7da4cSIgor Mammedov     }
4054fd7da4cSIgor Mammedov 
406981c3dcdSMarkus Armbruster     qdev_unrealize(dev);
407c97adf3cSDavid Hildenbrand }
408c97adf3cSDavid Hildenbrand 
409c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
410c97adf3cSDavid Hildenbrand                                          AcpiPciHpState *s, DeviceState *dev,
411c97adf3cSDavid Hildenbrand                                          Error **errp)
412c97adf3cSDavid Hildenbrand {
413c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
414c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
415fd56e061SDavid Gibson     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
41603459ea3SMarkus Armbruster 
41703459ea3SMarkus Armbruster     trace_acpi_pci_unplug_request(bsel, slot);
41803459ea3SMarkus Armbruster 
419c24d5e0bSIgor Mammedov     if (bsel < 0) {
420c24d5e0bSIgor Mammedov         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
421c24d5e0bSIgor Mammedov                    ACPI_PCIHP_PROP_BSEL "' set");
422c24d5e0bSIgor Mammedov         return;
423c24d5e0bSIgor Mammedov     }
424c24d5e0bSIgor Mammedov 
4259323f892SLaurent Vivier     /*
4269323f892SLaurent Vivier      * pending_deleted_event is used by virtio-net failover to detect the
4279323f892SLaurent Vivier      * end of the unplug operation, the flag is set to false in
4289323f892SLaurent Vivier      * acpi_pcihp_eject_slot() when the operation is completed.
4299323f892SLaurent Vivier      */
4309323f892SLaurent Vivier     pdev->qdev.pending_deleted_event = true;
431c24d5e0bSIgor Mammedov     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
4320058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
433db4728e6SMichael S. Tsirkin }
434db4728e6SMichael S. Tsirkin 
435db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
436db4728e6SMichael S. Tsirkin {
437db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
438db4728e6SMichael S. Tsirkin     uint32_t val = 0;
439db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
440db4728e6SMichael S. Tsirkin 
441fa365d7cSGonglei     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
442db4728e6SMichael S. Tsirkin         return 0;
443db4728e6SMichael S. Tsirkin     }
444db4728e6SMichael S. Tsirkin 
445db4728e6SMichael S. Tsirkin     switch (addr) {
446a7b613cfSIgor Mammedov     case PCI_UP_BASE:
4475a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
44899d09dd3SIgor Mammedov         if (!s->legacy_piix) {
4495a2223caSMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].up = 0;
45099d09dd3SIgor Mammedov         }
451df93b194SMarkus Armbruster         trace_acpi_pci_up_read(val);
452db4728e6SMichael S. Tsirkin         break;
453a7b613cfSIgor Mammedov     case PCI_DOWN_BASE:
454db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
455df93b194SMarkus Armbruster         trace_acpi_pci_down_read(val);
456db4728e6SMichael S. Tsirkin         break;
457a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
458df93b194SMarkus Armbruster         trace_acpi_pci_features_read(val);
459db4728e6SMichael S. Tsirkin         break;
460a7b613cfSIgor Mammedov     case PCI_RMV_BASE:
461db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
462df93b194SMarkus Armbruster         trace_acpi_pci_rmv_read(val);
463db4728e6SMichael S. Tsirkin         break;
464a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
465db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
466df93b194SMarkus Armbruster         trace_acpi_pci_sel_read(val);
467b32bd763SIgor Mammedov         break;
468b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
469b32bd763SIgor Mammedov         val = s->acpi_index;
470b32bd763SIgor Mammedov         s->acpi_index = 0;
471b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_read(val);
472b32bd763SIgor Mammedov         break;
473db4728e6SMichael S. Tsirkin     default:
474db4728e6SMichael S. Tsirkin         break;
475db4728e6SMichael S. Tsirkin     }
476db4728e6SMichael S. Tsirkin 
477db4728e6SMichael S. Tsirkin     return val;
478db4728e6SMichael S. Tsirkin }
479db4728e6SMichael S. Tsirkin 
480db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
481db4728e6SMichael S. Tsirkin                       unsigned int size)
482db4728e6SMichael S. Tsirkin {
483b32bd763SIgor Mammedov     int slot;
484b32bd763SIgor Mammedov     PCIBus *bus;
485b32bd763SIgor Mammedov     BusChild *kid, *next;
486db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
487b32bd763SIgor Mammedov 
488b32bd763SIgor Mammedov     s->acpi_index = 0;
489db4728e6SMichael S. Tsirkin     switch (addr) {
490b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
491b32bd763SIgor Mammedov         /*
492b32bd763SIgor Mammedov          * fetch acpi-index for specified slot so that follow up read from
493b32bd763SIgor Mammedov          * PCI_AIDX_BASE can return it to guest
494b32bd763SIgor Mammedov          */
495b32bd763SIgor Mammedov         slot = ctz32(data);
496b32bd763SIgor Mammedov 
497b32bd763SIgor Mammedov         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
498b32bd763SIgor Mammedov             break;
499b32bd763SIgor Mammedov         }
500b32bd763SIgor Mammedov 
501b32bd763SIgor Mammedov         bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
5029bd6565cSMichael S. Tsirkin         if (!bus) {
5039bd6565cSMichael S. Tsirkin             break;
5049bd6565cSMichael S. Tsirkin         }
505b32bd763SIgor Mammedov         QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
506b32bd763SIgor Mammedov             Object *o = OBJECT(kid->child);
507b32bd763SIgor Mammedov             PCIDevice *dev = PCI_DEVICE(o);
508b32bd763SIgor Mammedov             if (PCI_SLOT(dev->devfn) == slot) {
509b32bd763SIgor Mammedov                 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
510b32bd763SIgor Mammedov                 break;
511b32bd763SIgor Mammedov             }
512b32bd763SIgor Mammedov         }
513b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
514b32bd763SIgor Mammedov         break;
515a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
516db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
517db4728e6SMichael S. Tsirkin             break;
518db4728e6SMichael S. Tsirkin         }
519db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
520df93b194SMarkus Armbruster         trace_acpi_pci_ej_write(addr, data);
521db4728e6SMichael S. Tsirkin         break;
522a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
523f5855994SAnthony PERARD         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
524df93b194SMarkus Armbruster         trace_acpi_pci_sel_write(addr, data);
525db4728e6SMichael S. Tsirkin     default:
526db4728e6SMichael S. Tsirkin         break;
527db4728e6SMichael S. Tsirkin     }
528db4728e6SMichael S. Tsirkin }
529db4728e6SMichael S. Tsirkin 
530db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
531db4728e6SMichael S. Tsirkin     .read = pci_read,
532db4728e6SMichael S. Tsirkin     .write = pci_write,
533db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
534db4728e6SMichael S. Tsirkin     .valid = {
535db4728e6SMichael S. Tsirkin         .min_access_size = 4,
536db4728e6SMichael S. Tsirkin         .max_access_size = 4,
537db4728e6SMichael S. Tsirkin     },
538db4728e6SMichael S. Tsirkin };
539db4728e6SMichael S. Tsirkin 
54078c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
541caf108bcSJulia Suvorova                      MemoryRegion *address_space_io, bool bridges_enabled,
542caf108bcSJulia Suvorova                      uint16_t io_base)
543db4728e6SMichael S. Tsirkin {
54478c2d872SIgor Mammedov     s->io_len = ACPI_PCIHP_SIZE;
545caf108bcSJulia Suvorova     s->io_base = io_base;
546e358edc8SIgor Mammedov 
547db4728e6SMichael S. Tsirkin     s->root = root_bus;
54899d09dd3SIgor Mammedov     s->legacy_piix = !bridges_enabled;
549e358edc8SIgor Mammedov 
55078c2d872SIgor Mammedov     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
55178c2d872SIgor Mammedov                           "acpi-pci-hotplug", s->io_len);
55278c2d872SIgor Mammedov     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
55378c2d872SIgor Mammedov 
55478c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
555d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
55678c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
557d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
558db4728e6SMichael S. Tsirkin }
559db4728e6SMichael S. Tsirkin 
560db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
561db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
562db4728e6SMichael S. Tsirkin     .version_id = 1,
563db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
564db4728e6SMichael S. Tsirkin     .fields = (VMStateField[]) {
565db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
566db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
567db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
568db4728e6SMichael S. Tsirkin     }
569db4728e6SMichael S. Tsirkin };
570