xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 2940a4b9e3d206cc759c7630dde2fb7ded3e9ec2)
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 
88*2940a4b9SIgor Mammedov typedef struct {
89*2940a4b9SIgor Mammedov     unsigned bsel_alloc;
90*2940a4b9SIgor Mammedov     bool has_bridge_hotplug;
91*2940a4b9SIgor Mammedov } BSELInfo;
92*2940a4b9SIgor Mammedov 
93*2940a4b9SIgor Mammedov /* Assign BSEL property only to buses that support hotplug. */
94ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque)
95ab938ae4SAnthony PERARD {
96*2940a4b9SIgor Mammedov     BSELInfo *info = opaque;
97ab938ae4SAnthony PERARD     unsigned *bus_bsel;
98*2940a4b9SIgor Mammedov     DeviceState *br = bus->qbus.parent;
99*2940a4b9SIgor Mammedov     bool is_bridge = IS_PCI_BRIDGE(br);
100ab938ae4SAnthony PERARD 
101*2940a4b9SIgor Mammedov     /* hotplugged bridges can't be described in ACPI ignore them */
102ab938ae4SAnthony PERARD     if (qbus_is_hotpluggable(BUS(bus))) {
103*2940a4b9SIgor Mammedov         if (!is_bridge || (!br->hotplugged && info->has_bridge_hotplug)) {
104ab938ae4SAnthony PERARD             bus_bsel = g_malloc(sizeof *bus_bsel);
105ab938ae4SAnthony PERARD 
106*2940a4b9SIgor Mammedov             *bus_bsel = info->bsel_alloc++;
107ab938ae4SAnthony PERARD             object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
108d2623129SMarkus Armbruster                                            bus_bsel, OBJ_PROP_FLAG_READ);
109ab938ae4SAnthony PERARD         }
110ab938ae4SAnthony PERARD     }
111ab938ae4SAnthony PERARD 
112*2940a4b9SIgor Mammedov     return info;
113*2940a4b9SIgor Mammedov }
114*2940a4b9SIgor Mammedov 
115*2940a4b9SIgor Mammedov static void acpi_set_pci_info(bool has_bridge_hotplug)
116ab938ae4SAnthony PERARD {
117ab938ae4SAnthony PERARD     static bool bsel_is_set;
118c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
119ab938ae4SAnthony PERARD     PCIBus *bus;
120*2940a4b9SIgor Mammedov     BSELInfo info = { .bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT,
121*2940a4b9SIgor Mammedov                       .has_bridge_hotplug = has_bridge_hotplug };
122ab938ae4SAnthony PERARD 
123ab938ae4SAnthony PERARD     if (bsel_is_set) {
124ab938ae4SAnthony PERARD         return;
125ab938ae4SAnthony PERARD     }
126ab938ae4SAnthony PERARD     bsel_is_set = true;
127ab938ae4SAnthony PERARD 
128c0e427d6SJulia Suvorova     if (!host) {
129c0e427d6SJulia Suvorova         return;
130c0e427d6SJulia Suvorova     }
131c0e427d6SJulia Suvorova 
132c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
133ab938ae4SAnthony PERARD     if (bus) {
134ab938ae4SAnthony PERARD         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
135*2940a4b9SIgor Mammedov         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &info);
136ab938ae4SAnthony PERARD     }
137ab938ae4SAnthony PERARD }
138ab938ae4SAnthony PERARD 
1393d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void)
1403d7e78aaSAni Sinha {
141c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
1423d7e78aaSAni Sinha     PCIBus *bus;
1433d7e78aaSAni Sinha 
144c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
14578480268SAni Sinha     if (bus && qbus_is_hotpluggable(BUS(bus))) {
1463d7e78aaSAni Sinha         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
1473d7e78aaSAni Sinha         qbus_set_hotplug_handler(BUS(bus), NULL);
1483d7e78aaSAni Sinha     }
14978480268SAni Sinha 
1503d7e78aaSAni Sinha     return;
1513d7e78aaSAni Sinha }
1523d7e78aaSAni Sinha 
153db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
154db4728e6SMichael S. Tsirkin {
155db4728e6SMichael S. Tsirkin     AcpiPciHpFind *find = opaque;
156db4728e6SMichael S. Tsirkin     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
157db4728e6SMichael S. Tsirkin         find->bus = bus;
158db4728e6SMichael S. Tsirkin     }
159db4728e6SMichael S. Tsirkin }
160db4728e6SMichael S. Tsirkin 
161db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
162db4728e6SMichael S. Tsirkin {
163db4728e6SMichael S. Tsirkin     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
164db4728e6SMichael S. Tsirkin 
165db4728e6SMichael S. Tsirkin     if (bsel < 0) {
166db4728e6SMichael S. Tsirkin         return NULL;
167db4728e6SMichael S. Tsirkin     }
168db4728e6SMichael S. Tsirkin 
169db4728e6SMichael S. Tsirkin     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
170db4728e6SMichael S. Tsirkin 
171db4728e6SMichael S. Tsirkin     /* Make bsel 0 eject root bus if bsel property is not set,
172db4728e6SMichael S. Tsirkin      * for compatibility with non acpi setups.
173db4728e6SMichael S. Tsirkin      * TODO: really needed?
174db4728e6SMichael S. Tsirkin      */
175db4728e6SMichael S. Tsirkin     if (!bsel && !find.bus) {
176db4728e6SMichael S. Tsirkin         find.bus = s->root;
177db4728e6SMichael S. Tsirkin     }
1788ad038abSAni Sinha 
1798ad038abSAni Sinha     /*
1808ad038abSAni Sinha      * Check if find.bus is actually hotpluggable. If bsel is set to
1818ad038abSAni Sinha      * NULL for example on the root bus in order to make it
1828ad038abSAni Sinha      * non-hotpluggable, find.bus will match the root bus when bsel
1838ad038abSAni Sinha      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
1848ad038abSAni Sinha      * bus is not hotpluggable however, we should not select the bus.
1858ad038abSAni Sinha      * Instead, we should set find.bus to NULL in that case. In the check
1868ad038abSAni Sinha      * below, we generalize this case for all buses, not just the root bus.
1878ad038abSAni Sinha      * The callers of this function check for a null return value and
1888ad038abSAni Sinha      * handle them appropriately.
1898ad038abSAni Sinha      */
1908ad038abSAni Sinha     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
1918ad038abSAni Sinha         find.bus = NULL;
1928ad038abSAni Sinha     }
193db4728e6SMichael S. Tsirkin     return find.bus;
194db4728e6SMichael S. Tsirkin }
195db4728e6SMichael S. Tsirkin 
196db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
197db4728e6SMichael S. Tsirkin {
1982897ae02SIgor Mammedov     DeviceClass *dc = DEVICE_GET_CLASS(dev);
199db4728e6SMichael S. Tsirkin     /*
200db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
201db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
202db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
20358660bfaSŁukasz Gieryk      *
20458660bfaSŁukasz Gieryk      * Don't allow hot-unplug of SR-IOV Virtual Functions, as they
20558660bfaSŁukasz Gieryk      * will be removed implicitly, when Physical Function is unplugged.
206db4728e6SMichael S. Tsirkin      */
207ad494274SIgor Mammedov     return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable ||
20858660bfaSŁukasz Gieryk            pci_is_vf(dev);
209db4728e6SMichael S. Tsirkin }
210db4728e6SMichael S. Tsirkin 
211db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
212db4728e6SMichael S. Tsirkin {
213c97adf3cSDavid Hildenbrand     HotplugHandler *hotplug_ctrl;
214db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
215786a4ea8SStefan Hajnoczi     int slot = ctz32(slots);
216db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
217db4728e6SMichael S. Tsirkin 
21803459ea3SMarkus Armbruster     trace_acpi_pci_eject_slot(bsel, slot);
21903459ea3SMarkus Armbruster 
220a3ec4bb7SIgor Mammedov     if (!bus || slot > 31) {
221db4728e6SMichael S. Tsirkin         return;
222db4728e6SMichael S. Tsirkin     }
223db4728e6SMichael S. Tsirkin 
224db4728e6SMichael S. Tsirkin     /* Mark request as complete */
225db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
2265a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
227db4728e6SMichael S. Tsirkin 
228db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
229db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
230db4728e6SMichael S. Tsirkin         PCIDevice *dev = PCI_DEVICE(qdev);
231db4728e6SMichael S. Tsirkin         if (PCI_SLOT(dev->devfn) == slot) {
2325a2223caSMichael S. Tsirkin             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
2339323f892SLaurent Vivier                 /*
2349323f892SLaurent Vivier                  * partially_hotplugged is used by virtio-net failover:
2359323f892SLaurent Vivier                  * failover has asked the guest OS to unplug the device
2369323f892SLaurent Vivier                  * but we need to keep some references to the device
2379323f892SLaurent Vivier                  * to be able to plug it back in case of failure so
2389323f892SLaurent Vivier                  * we don't execute hotplug_handler_unplug().
2399323f892SLaurent Vivier                  */
2409323f892SLaurent Vivier                 if (dev->partially_hotplugged) {
2419323f892SLaurent Vivier                     /*
2429323f892SLaurent Vivier                      * pending_deleted_event is set to true when
2439323f892SLaurent Vivier                      * virtio-net failover asks to unplug the device,
2449323f892SLaurent Vivier                      * and set to false here when the operation is done
2459323f892SLaurent Vivier                      * This is used by the migration loop to detect the
2469323f892SLaurent Vivier                      * end of the operation and really start the migration.
2479323f892SLaurent Vivier                      */
2489323f892SLaurent Vivier                     qdev->pending_deleted_event = false;
2499323f892SLaurent Vivier                 } else {
250c97adf3cSDavid Hildenbrand                     hotplug_ctrl = qdev_get_hotplug_handler(qdev);
251c97adf3cSDavid Hildenbrand                     hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
25207578b0aSDavid Hildenbrand                     object_unparent(OBJECT(qdev));
253db4728e6SMichael S. Tsirkin                 }
254db4728e6SMichael S. Tsirkin             }
255db4728e6SMichael S. Tsirkin         }
256db4728e6SMichael S. Tsirkin     }
2579323f892SLaurent Vivier }
258db4728e6SMichael S. Tsirkin 
259db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
260db4728e6SMichael S. Tsirkin {
261db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
262db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
263db4728e6SMichael S. Tsirkin 
264db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
265db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
266db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
267db4728e6SMichael S. Tsirkin     }
268db4728e6SMichael S. Tsirkin 
269db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
270db4728e6SMichael S. Tsirkin 
271db4728e6SMichael S. Tsirkin     if (!bus) {
272db4728e6SMichael S. Tsirkin         return;
273db4728e6SMichael S. Tsirkin     }
274db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
275db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
276db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
277db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
278db4728e6SMichael S. Tsirkin 
279db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
280db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
281db4728e6SMichael S. Tsirkin         }
282db4728e6SMichael S. Tsirkin     }
283db4728e6SMichael S. Tsirkin }
284db4728e6SMichael S. Tsirkin 
285db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
286db4728e6SMichael S. Tsirkin {
287db4728e6SMichael S. Tsirkin     int i;
288db4728e6SMichael S. Tsirkin 
289db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
290db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
291db4728e6SMichael S. Tsirkin     }
292db4728e6SMichael S. Tsirkin }
293db4728e6SMichael S. Tsirkin 
2943d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
295db4728e6SMichael S. Tsirkin {
2963d7e78aaSAni Sinha     if (acpihp_root_off) {
2973d7e78aaSAni Sinha         acpi_pcihp_disable_root_bus();
2983d7e78aaSAni Sinha     }
299*2940a4b9SIgor Mammedov     acpi_set_pci_info(!s->legacy_piix);
300db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
301db4728e6SMichael S. Tsirkin }
302db4728e6SMichael S. Tsirkin 
303b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1)
304b32bd763SIgor Mammedov 
305ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
306ec266f40SDavid Hildenbrand                                    DeviceState *dev, Error **errp)
307ec266f40SDavid Hildenbrand {
308b32bd763SIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
309b32bd763SIgor Mammedov 
310ec266f40SDavid Hildenbrand     /* Only hotplugged devices need the hotplug capability. */
311ec266f40SDavid Hildenbrand     if (dev->hotplugged &&
312028f1a88SAni Sinha         acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
313ec266f40SDavid Hildenbrand         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
314ec266f40SDavid Hildenbrand                    ACPI_PCIHP_PROP_BSEL "' set");
315ec266f40SDavid Hildenbrand         return;
316ec266f40SDavid Hildenbrand     }
317b32bd763SIgor Mammedov 
318b32bd763SIgor Mammedov     /*
319b32bd763SIgor Mammedov      * capped by systemd (see: udev-builtin-net_id.c)
320b32bd763SIgor Mammedov      * as it's the only known user honor it to avoid users
321b32bd763SIgor Mammedov      * misconfigure QEMU and then wonder why acpi-index doesn't work
322b32bd763SIgor Mammedov      */
323b32bd763SIgor Mammedov     if (pdev->acpi_index > ONBOARD_INDEX_MAX) {
324b32bd763SIgor Mammedov         error_setg(errp, "acpi-index should be less or equal to %u",
325b32bd763SIgor Mammedov                    ONBOARD_INDEX_MAX);
326b32bd763SIgor Mammedov         return;
327b32bd763SIgor Mammedov     }
3284fd7da4cSIgor Mammedov 
3294fd7da4cSIgor Mammedov     /*
3304fd7da4cSIgor Mammedov      * make sure that acpi-index is unique across all present PCI devices
3314fd7da4cSIgor Mammedov      */
3324fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
3334fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
3344fd7da4cSIgor Mammedov 
3354fd7da4cSIgor Mammedov         if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index),
3364fd7da4cSIgor Mammedov                               g_cmp_uint32, NULL)) {
3374fd7da4cSIgor Mammedov             error_setg(errp, "a PCI device with acpi-index = %" PRIu32
3384fd7da4cSIgor Mammedov                        " already exist", pdev->acpi_index);
3394fd7da4cSIgor Mammedov             return;
3404fd7da4cSIgor Mammedov         }
3414fd7da4cSIgor Mammedov         g_sequence_insert_sorted(used_indexes,
3424fd7da4cSIgor Mammedov                                  GINT_TO_POINTER(pdev->acpi_index),
3434fd7da4cSIgor Mammedov                                  g_cmp_uint32, NULL);
3444fd7da4cSIgor Mammedov     }
345ec266f40SDavid Hildenbrand }
346ec266f40SDavid Hildenbrand 
3470058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
348c24d5e0bSIgor Mammedov                                DeviceState *dev, Error **errp)
349db4728e6SMichael S. Tsirkin {
350c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
351c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
3526b0969f1SIgor Mammedov     PCIDevice *bridge;
3536b0969f1SIgor Mammedov     PCIBus *bus;
354ec266f40SDavid Hildenbrand     int bsel;
355db4728e6SMichael S. Tsirkin 
356db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
357db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
358db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
359c24d5e0bSIgor Mammedov     if (!dev->hotplugged) {
3603e520926SDavid Hildenbrand         /*
3613e520926SDavid Hildenbrand          * Overwrite the default hotplug handler with the ACPI PCI one
3623e520926SDavid Hildenbrand          * for cold plugged bridges only.
3633e520926SDavid Hildenbrand          */
3643e520926SDavid Hildenbrand         if (!s->legacy_piix &&
3653e520926SDavid Hildenbrand             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
3663e520926SDavid Hildenbrand             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
3673e520926SDavid Hildenbrand 
3683f3cbbb2SJulia Suvorova             /* Remove all hot-plug handlers if hot-plug is disabled on slot */
3693f3cbbb2SJulia Suvorova             if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT) &&
3703f3cbbb2SJulia Suvorova                 !PCIE_SLOT(pdev)->hotplug) {
3713f3cbbb2SJulia Suvorova                 qbus_set_hotplug_handler(BUS(sec), NULL);
3723f3cbbb2SJulia Suvorova                 return;
3733f3cbbb2SJulia Suvorova             }
3743f3cbbb2SJulia Suvorova 
3759bc6bfdfSMarkus Armbruster             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
3763e520926SDavid Hildenbrand             /* We don't have to overwrite any other hotplug handler yet */
3773e520926SDavid Hildenbrand             assert(QLIST_EMPTY(&sec->child));
3783e520926SDavid Hildenbrand         }
3793e520926SDavid Hildenbrand 
380c24d5e0bSIgor Mammedov         return;
381db4728e6SMichael S. Tsirkin     }
382db4728e6SMichael S. Tsirkin 
3836b0969f1SIgor Mammedov     bus = pci_get_bus(pdev);
3846b0969f1SIgor Mammedov     bridge = pci_bridge_get_device(bus);
3856b0969f1SIgor Mammedov     if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) ||
3866b0969f1SIgor Mammedov         object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) {
3876b0969f1SIgor Mammedov         pcie_cap_slot_enable_power(bridge);
3886b0969f1SIgor Mammedov     }
3896b0969f1SIgor Mammedov 
3906b0969f1SIgor Mammedov     bsel = acpi_pcihp_get_bsel(bus);
391ec266f40SDavid Hildenbrand     g_assert(bsel >= 0);
3928f5001f9SIgor Mammedov     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
3930058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
394db4728e6SMichael S. Tsirkin }
395db4728e6SMichael S. Tsirkin 
3960058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
397c24d5e0bSIgor Mammedov                                  DeviceState *dev, Error **errp)
398c24d5e0bSIgor Mammedov {
3994fd7da4cSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
4004fd7da4cSIgor Mammedov 
401028f1a88SAni Sinha     trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
402028f1a88SAni Sinha                           acpi_pcihp_get_bsel(pci_get_bus(pdev)));
4034fd7da4cSIgor Mammedov 
4044fd7da4cSIgor Mammedov     /*
4054fd7da4cSIgor Mammedov      * clean up acpi-index so it could reused by another device
4064fd7da4cSIgor Mammedov      */
4074fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
4084fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
4094fd7da4cSIgor Mammedov 
4104fd7da4cSIgor Mammedov         g_sequence_remove(g_sequence_lookup(used_indexes,
4114fd7da4cSIgor Mammedov                           GINT_TO_POINTER(pdev->acpi_index),
4124fd7da4cSIgor Mammedov                           g_cmp_uint32, NULL));
4134fd7da4cSIgor Mammedov     }
4144fd7da4cSIgor Mammedov 
415981c3dcdSMarkus Armbruster     qdev_unrealize(dev);
416c97adf3cSDavid Hildenbrand }
417c97adf3cSDavid Hildenbrand 
418c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
419c97adf3cSDavid Hildenbrand                                          AcpiPciHpState *s, DeviceState *dev,
420c97adf3cSDavid Hildenbrand                                          Error **errp)
421c97adf3cSDavid Hildenbrand {
422c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
423c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
424fd56e061SDavid Gibson     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
42503459ea3SMarkus Armbruster 
42603459ea3SMarkus Armbruster     trace_acpi_pci_unplug_request(bsel, slot);
42703459ea3SMarkus Armbruster 
428c24d5e0bSIgor Mammedov     if (bsel < 0) {
429c24d5e0bSIgor Mammedov         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
430c24d5e0bSIgor Mammedov                    ACPI_PCIHP_PROP_BSEL "' set");
431c24d5e0bSIgor Mammedov         return;
432c24d5e0bSIgor Mammedov     }
433c24d5e0bSIgor Mammedov 
4349323f892SLaurent Vivier     /*
4359323f892SLaurent Vivier      * pending_deleted_event is used by virtio-net failover to detect the
4369323f892SLaurent Vivier      * end of the unplug operation, the flag is set to false in
4379323f892SLaurent Vivier      * acpi_pcihp_eject_slot() when the operation is completed.
4389323f892SLaurent Vivier      */
4399323f892SLaurent Vivier     pdev->qdev.pending_deleted_event = true;
440c24d5e0bSIgor Mammedov     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
4410058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
442db4728e6SMichael S. Tsirkin }
443db4728e6SMichael S. Tsirkin 
444db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
445db4728e6SMichael S. Tsirkin {
446db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
447db4728e6SMichael S. Tsirkin     uint32_t val = 0;
448db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
449db4728e6SMichael S. Tsirkin 
450fa365d7cSGonglei     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
451db4728e6SMichael S. Tsirkin         return 0;
452db4728e6SMichael S. Tsirkin     }
453db4728e6SMichael S. Tsirkin 
454db4728e6SMichael S. Tsirkin     switch (addr) {
455a7b613cfSIgor Mammedov     case PCI_UP_BASE:
4565a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
45799d09dd3SIgor Mammedov         if (!s->legacy_piix) {
4585a2223caSMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].up = 0;
45999d09dd3SIgor Mammedov         }
460df93b194SMarkus Armbruster         trace_acpi_pci_up_read(val);
461db4728e6SMichael S. Tsirkin         break;
462a7b613cfSIgor Mammedov     case PCI_DOWN_BASE:
463db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
464df93b194SMarkus Armbruster         trace_acpi_pci_down_read(val);
465db4728e6SMichael S. Tsirkin         break;
466a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
467df93b194SMarkus Armbruster         trace_acpi_pci_features_read(val);
468db4728e6SMichael S. Tsirkin         break;
469a7b613cfSIgor Mammedov     case PCI_RMV_BASE:
470db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
471df93b194SMarkus Armbruster         trace_acpi_pci_rmv_read(val);
472db4728e6SMichael S. Tsirkin         break;
473a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
474db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
475df93b194SMarkus Armbruster         trace_acpi_pci_sel_read(val);
476b32bd763SIgor Mammedov         break;
477b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
478b32bd763SIgor Mammedov         val = s->acpi_index;
479b32bd763SIgor Mammedov         s->acpi_index = 0;
480b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_read(val);
481b32bd763SIgor Mammedov         break;
482db4728e6SMichael S. Tsirkin     default:
483db4728e6SMichael S. Tsirkin         break;
484db4728e6SMichael S. Tsirkin     }
485db4728e6SMichael S. Tsirkin 
486db4728e6SMichael S. Tsirkin     return val;
487db4728e6SMichael S. Tsirkin }
488db4728e6SMichael S. Tsirkin 
489db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
490db4728e6SMichael S. Tsirkin                       unsigned int size)
491db4728e6SMichael S. Tsirkin {
492b32bd763SIgor Mammedov     int slot;
493b32bd763SIgor Mammedov     PCIBus *bus;
494b32bd763SIgor Mammedov     BusChild *kid, *next;
495db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
496b32bd763SIgor Mammedov 
497b32bd763SIgor Mammedov     s->acpi_index = 0;
498db4728e6SMichael S. Tsirkin     switch (addr) {
499b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
500b32bd763SIgor Mammedov         /*
501b32bd763SIgor Mammedov          * fetch acpi-index for specified slot so that follow up read from
502b32bd763SIgor Mammedov          * PCI_AIDX_BASE can return it to guest
503b32bd763SIgor Mammedov          */
504b32bd763SIgor Mammedov         slot = ctz32(data);
505b32bd763SIgor Mammedov 
506b32bd763SIgor Mammedov         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
507b32bd763SIgor Mammedov             break;
508b32bd763SIgor Mammedov         }
509b32bd763SIgor Mammedov 
510b32bd763SIgor Mammedov         bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
5119bd6565cSMichael S. Tsirkin         if (!bus) {
5129bd6565cSMichael S. Tsirkin             break;
5139bd6565cSMichael S. Tsirkin         }
514b32bd763SIgor Mammedov         QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
515b32bd763SIgor Mammedov             Object *o = OBJECT(kid->child);
516b32bd763SIgor Mammedov             PCIDevice *dev = PCI_DEVICE(o);
517b32bd763SIgor Mammedov             if (PCI_SLOT(dev->devfn) == slot) {
518b32bd763SIgor Mammedov                 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
519b32bd763SIgor Mammedov                 break;
520b32bd763SIgor Mammedov             }
521b32bd763SIgor Mammedov         }
522b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
523b32bd763SIgor Mammedov         break;
524a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
525db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
526db4728e6SMichael S. Tsirkin             break;
527db4728e6SMichael S. Tsirkin         }
528db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
529df93b194SMarkus Armbruster         trace_acpi_pci_ej_write(addr, data);
530db4728e6SMichael S. Tsirkin         break;
531a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
532f5855994SAnthony PERARD         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
533df93b194SMarkus Armbruster         trace_acpi_pci_sel_write(addr, data);
534db4728e6SMichael S. Tsirkin     default:
535db4728e6SMichael S. Tsirkin         break;
536db4728e6SMichael S. Tsirkin     }
537db4728e6SMichael S. Tsirkin }
538db4728e6SMichael S. Tsirkin 
539db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
540db4728e6SMichael S. Tsirkin     .read = pci_read,
541db4728e6SMichael S. Tsirkin     .write = pci_write,
542db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
543db4728e6SMichael S. Tsirkin     .valid = {
544db4728e6SMichael S. Tsirkin         .min_access_size = 4,
545db4728e6SMichael S. Tsirkin         .max_access_size = 4,
546db4728e6SMichael S. Tsirkin     },
547db4728e6SMichael S. Tsirkin };
548db4728e6SMichael S. Tsirkin 
54978c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
550caf108bcSJulia Suvorova                      MemoryRegion *address_space_io, bool bridges_enabled,
551caf108bcSJulia Suvorova                      uint16_t io_base)
552db4728e6SMichael S. Tsirkin {
55378c2d872SIgor Mammedov     s->io_len = ACPI_PCIHP_SIZE;
554caf108bcSJulia Suvorova     s->io_base = io_base;
555e358edc8SIgor Mammedov 
556db4728e6SMichael S. Tsirkin     s->root = root_bus;
55799d09dd3SIgor Mammedov     s->legacy_piix = !bridges_enabled;
558e358edc8SIgor Mammedov 
55978c2d872SIgor Mammedov     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
56078c2d872SIgor Mammedov                           "acpi-pci-hotplug", s->io_len);
56178c2d872SIgor Mammedov     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
56278c2d872SIgor Mammedov 
56378c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
564d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
56578c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
566d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
567db4728e6SMichael S. Tsirkin }
568db4728e6SMichael S. Tsirkin 
569db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
570db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
571db4728e6SMichael S. Tsirkin     .version_id = 1,
572db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
573db4728e6SMichael S. Tsirkin     .fields = (VMStateField[]) {
574db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
575db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
576db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
577db4728e6SMichael S. Tsirkin     }
578db4728e6SMichael S. Tsirkin };
579