xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 3f3cbbb2369ebba67cccf8c60d6a0043b315e17c)
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"
34*3f3cbbb2SJulia Suvorova #include "hw/pci/pcie_port.h"
35c0e427d6SJulia Suvorova #include "hw/i386/acpi-build.h"
36db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h"
37db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h"
38d6454270SMarkus Armbruster #include "migration/vmstate.h"
39da34e65cSMarkus Armbruster #include "qapi/error.h"
40db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h"
41df93b194SMarkus Armbruster #include "trace.h"
42db4728e6SMichael S. Tsirkin 
43b32bd763SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0018
44a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000
45a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004
46a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008
47a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c
48a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010
49b32bd763SIgor Mammedov #define PCI_AIDX_BASE 0x0014
50db4728e6SMichael S. Tsirkin 
51db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind {
52db4728e6SMichael S. Tsirkin     int bsel;
53db4728e6SMichael S. Tsirkin     PCIBus *bus;
54db4728e6SMichael S. Tsirkin } AcpiPciHpFind;
55db4728e6SMichael S. Tsirkin 
564fd7da4cSIgor Mammedov static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data)
574fd7da4cSIgor Mammedov {
584fd7da4cSIgor Mammedov     return a - b;
594fd7da4cSIgor Mammedov }
604fd7da4cSIgor Mammedov 
614fd7da4cSIgor Mammedov static GSequence *pci_acpi_index_list(void)
624fd7da4cSIgor Mammedov {
634fd7da4cSIgor Mammedov     static GSequence *used_acpi_index_list;
644fd7da4cSIgor Mammedov 
654fd7da4cSIgor Mammedov     if (!used_acpi_index_list) {
664fd7da4cSIgor Mammedov         used_acpi_index_list = g_sequence_new(NULL);
674fd7da4cSIgor Mammedov     }
684fd7da4cSIgor Mammedov     return used_acpi_index_list;
694fd7da4cSIgor Mammedov }
704fd7da4cSIgor Mammedov 
71db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus)
72db4728e6SMichael S. Tsirkin {
737c38ecd0SKirill Batuzov     Error *local_err = NULL;
74c03d83d5SMarc-André Lureau     uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
757c38ecd0SKirill Batuzov                                              &local_err);
767c38ecd0SKirill Batuzov 
77c03d83d5SMarc-André Lureau     if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
787c38ecd0SKirill Batuzov         if (local_err) {
797c38ecd0SKirill Batuzov             error_free(local_err);
80db4728e6SMichael S. Tsirkin         }
81db4728e6SMichael S. Tsirkin         return -1;
827c38ecd0SKirill Batuzov     } else {
83db4728e6SMichael S. Tsirkin         return bsel;
84db4728e6SMichael S. Tsirkin     }
857c38ecd0SKirill Batuzov }
86db4728e6SMichael S. Tsirkin 
87ab938ae4SAnthony PERARD /* Assign BSEL property to all buses.  In the future, this can be changed
88ab938ae4SAnthony PERARD  * to only assign to buses that support hotplug.
89ab938ae4SAnthony PERARD  */
90ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque)
91ab938ae4SAnthony PERARD {
92ab938ae4SAnthony PERARD     unsigned *bsel_alloc = opaque;
93ab938ae4SAnthony PERARD     unsigned *bus_bsel;
94ab938ae4SAnthony PERARD 
95ab938ae4SAnthony PERARD     if (qbus_is_hotpluggable(BUS(bus))) {
96ab938ae4SAnthony PERARD         bus_bsel = g_malloc(sizeof *bus_bsel);
97ab938ae4SAnthony PERARD 
98ab938ae4SAnthony PERARD         *bus_bsel = (*bsel_alloc)++;
99ab938ae4SAnthony PERARD         object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
100d2623129SMarkus Armbruster                                        bus_bsel, OBJ_PROP_FLAG_READ);
101ab938ae4SAnthony PERARD     }
102ab938ae4SAnthony PERARD 
103ab938ae4SAnthony PERARD     return bsel_alloc;
104ab938ae4SAnthony PERARD }
105ab938ae4SAnthony PERARD 
106ab938ae4SAnthony PERARD static void acpi_set_pci_info(void)
107ab938ae4SAnthony PERARD {
108ab938ae4SAnthony PERARD     static bool bsel_is_set;
109c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
110ab938ae4SAnthony PERARD     PCIBus *bus;
111ab938ae4SAnthony PERARD     unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
112ab938ae4SAnthony PERARD 
113ab938ae4SAnthony PERARD     if (bsel_is_set) {
114ab938ae4SAnthony PERARD         return;
115ab938ae4SAnthony PERARD     }
116ab938ae4SAnthony PERARD     bsel_is_set = true;
117ab938ae4SAnthony PERARD 
118c0e427d6SJulia Suvorova     if (!host) {
119c0e427d6SJulia Suvorova         return;
120c0e427d6SJulia Suvorova     }
121c0e427d6SJulia Suvorova 
122c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
123ab938ae4SAnthony PERARD     if (bus) {
124ab938ae4SAnthony PERARD         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
125ab938ae4SAnthony PERARD         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
126ab938ae4SAnthony PERARD     }
127ab938ae4SAnthony PERARD }
128ab938ae4SAnthony PERARD 
1293d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void)
1303d7e78aaSAni Sinha {
1313d7e78aaSAni Sinha     static bool root_hp_disabled;
132c0e427d6SJulia Suvorova     Object *host = acpi_get_i386_pci_host();
1333d7e78aaSAni Sinha     PCIBus *bus;
1343d7e78aaSAni Sinha 
1353d7e78aaSAni Sinha     if (root_hp_disabled) {
1363d7e78aaSAni Sinha         return;
1373d7e78aaSAni Sinha     }
1383d7e78aaSAni Sinha 
139c0e427d6SJulia Suvorova     bus = PCI_HOST_BRIDGE(host)->bus;
1403d7e78aaSAni Sinha     if (bus) {
1413d7e78aaSAni Sinha         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
1423d7e78aaSAni Sinha         qbus_set_hotplug_handler(BUS(bus), NULL);
1433d7e78aaSAni Sinha     }
1443d7e78aaSAni Sinha     root_hp_disabled = true;
1453d7e78aaSAni Sinha     return;
1463d7e78aaSAni Sinha }
1473d7e78aaSAni Sinha 
148db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
149db4728e6SMichael S. Tsirkin {
150db4728e6SMichael S. Tsirkin     AcpiPciHpFind *find = opaque;
151db4728e6SMichael S. Tsirkin     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
152db4728e6SMichael S. Tsirkin         find->bus = bus;
153db4728e6SMichael S. Tsirkin     }
154db4728e6SMichael S. Tsirkin }
155db4728e6SMichael S. Tsirkin 
156db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
157db4728e6SMichael S. Tsirkin {
158db4728e6SMichael S. Tsirkin     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
159db4728e6SMichael S. Tsirkin 
160db4728e6SMichael S. Tsirkin     if (bsel < 0) {
161db4728e6SMichael S. Tsirkin         return NULL;
162db4728e6SMichael S. Tsirkin     }
163db4728e6SMichael S. Tsirkin 
164db4728e6SMichael S. Tsirkin     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
165db4728e6SMichael S. Tsirkin 
166db4728e6SMichael S. Tsirkin     /* Make bsel 0 eject root bus if bsel property is not set,
167db4728e6SMichael S. Tsirkin      * for compatibility with non acpi setups.
168db4728e6SMichael S. Tsirkin      * TODO: really needed?
169db4728e6SMichael S. Tsirkin      */
170db4728e6SMichael S. Tsirkin     if (!bsel && !find.bus) {
171db4728e6SMichael S. Tsirkin         find.bus = s->root;
172db4728e6SMichael S. Tsirkin     }
1738ad038abSAni Sinha 
1748ad038abSAni Sinha     /*
1758ad038abSAni Sinha      * Check if find.bus is actually hotpluggable. If bsel is set to
1768ad038abSAni Sinha      * NULL for example on the root bus in order to make it
1778ad038abSAni Sinha      * non-hotpluggable, find.bus will match the root bus when bsel
1788ad038abSAni Sinha      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
1798ad038abSAni Sinha      * bus is not hotpluggable however, we should not select the bus.
1808ad038abSAni Sinha      * Instead, we should set find.bus to NULL in that case. In the check
1818ad038abSAni Sinha      * below, we generalize this case for all buses, not just the root bus.
1828ad038abSAni Sinha      * The callers of this function check for a null return value and
1838ad038abSAni Sinha      * handle them appropriately.
1848ad038abSAni Sinha      */
1858ad038abSAni Sinha     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
1868ad038abSAni Sinha         find.bus = NULL;
1878ad038abSAni Sinha     }
188db4728e6SMichael S. Tsirkin     return find.bus;
189db4728e6SMichael S. Tsirkin }
190db4728e6SMichael S. Tsirkin 
191db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
192db4728e6SMichael S. Tsirkin {
193db4728e6SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1942897ae02SIgor Mammedov     DeviceClass *dc = DEVICE_GET_CLASS(dev);
195db4728e6SMichael S. Tsirkin     /*
196db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
197db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
198db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
199db4728e6SMichael S. Tsirkin      */
2002897ae02SIgor Mammedov     return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
201db4728e6SMichael S. Tsirkin }
202db4728e6SMichael S. Tsirkin 
203db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
204db4728e6SMichael S. Tsirkin {
205c97adf3cSDavid Hildenbrand     HotplugHandler *hotplug_ctrl;
206db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
207786a4ea8SStefan Hajnoczi     int slot = ctz32(slots);
208db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
209db4728e6SMichael S. Tsirkin 
21003459ea3SMarkus Armbruster     trace_acpi_pci_eject_slot(bsel, slot);
21103459ea3SMarkus Armbruster 
212a3ec4bb7SIgor Mammedov     if (!bus || slot > 31) {
213db4728e6SMichael S. Tsirkin         return;
214db4728e6SMichael S. Tsirkin     }
215db4728e6SMichael S. Tsirkin 
216db4728e6SMichael S. Tsirkin     /* Mark request as complete */
217db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
2185a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
219db4728e6SMichael S. Tsirkin 
220db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
221db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
222db4728e6SMichael S. Tsirkin         PCIDevice *dev = PCI_DEVICE(qdev);
223db4728e6SMichael S. Tsirkin         if (PCI_SLOT(dev->devfn) == slot) {
2245a2223caSMichael S. Tsirkin             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
225c97adf3cSDavid Hildenbrand                 hotplug_ctrl = qdev_get_hotplug_handler(qdev);
226c97adf3cSDavid Hildenbrand                 hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
22707578b0aSDavid Hildenbrand                 object_unparent(OBJECT(qdev));
228db4728e6SMichael S. Tsirkin             }
229db4728e6SMichael S. Tsirkin         }
230db4728e6SMichael S. Tsirkin     }
231db4728e6SMichael S. Tsirkin }
232db4728e6SMichael S. Tsirkin 
233db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
234db4728e6SMichael S. Tsirkin {
235db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
236db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
237db4728e6SMichael S. Tsirkin 
238db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
239db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
240db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
241db4728e6SMichael S. Tsirkin     }
242db4728e6SMichael S. Tsirkin 
243db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
244db4728e6SMichael S. Tsirkin 
245db4728e6SMichael S. Tsirkin     if (!bus) {
246db4728e6SMichael S. Tsirkin         return;
247db4728e6SMichael S. Tsirkin     }
248db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
249db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
250db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
251db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
252db4728e6SMichael S. Tsirkin 
253db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
254db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
255db4728e6SMichael S. Tsirkin         }
256db4728e6SMichael S. Tsirkin     }
257db4728e6SMichael S. Tsirkin }
258db4728e6SMichael S. Tsirkin 
259db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
260db4728e6SMichael S. Tsirkin {
261db4728e6SMichael S. Tsirkin     int i;
262db4728e6SMichael S. Tsirkin 
263db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
264db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
265db4728e6SMichael S. Tsirkin     }
266db4728e6SMichael S. Tsirkin }
267db4728e6SMichael S. Tsirkin 
2683d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
269db4728e6SMichael S. Tsirkin {
2703d7e78aaSAni Sinha     if (acpihp_root_off) {
2713d7e78aaSAni Sinha         acpi_pcihp_disable_root_bus();
2723d7e78aaSAni Sinha     }
273ab938ae4SAnthony PERARD     acpi_set_pci_info();
274db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
275db4728e6SMichael S. Tsirkin }
276db4728e6SMichael S. Tsirkin 
277b32bd763SIgor Mammedov #define ONBOARD_INDEX_MAX (16 * 1024 - 1)
278b32bd763SIgor Mammedov 
279ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
280ec266f40SDavid Hildenbrand                                    DeviceState *dev, Error **errp)
281ec266f40SDavid Hildenbrand {
282b32bd763SIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
283b32bd763SIgor Mammedov 
284ec266f40SDavid Hildenbrand     /* Only hotplugged devices need the hotplug capability. */
285ec266f40SDavid Hildenbrand     if (dev->hotplugged &&
286ec266f40SDavid Hildenbrand         acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) {
287ec266f40SDavid Hildenbrand         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
288ec266f40SDavid Hildenbrand                    ACPI_PCIHP_PROP_BSEL "' set");
289ec266f40SDavid Hildenbrand         return;
290ec266f40SDavid Hildenbrand     }
291b32bd763SIgor Mammedov 
292b32bd763SIgor Mammedov     /*
293b32bd763SIgor Mammedov      * capped by systemd (see: udev-builtin-net_id.c)
294b32bd763SIgor Mammedov      * as it's the only known user honor it to avoid users
295b32bd763SIgor Mammedov      * misconfigure QEMU and then wonder why acpi-index doesn't work
296b32bd763SIgor Mammedov      */
297b32bd763SIgor Mammedov     if (pdev->acpi_index > ONBOARD_INDEX_MAX) {
298b32bd763SIgor Mammedov         error_setg(errp, "acpi-index should be less or equal to %u",
299b32bd763SIgor Mammedov                    ONBOARD_INDEX_MAX);
300b32bd763SIgor Mammedov         return;
301b32bd763SIgor Mammedov     }
3024fd7da4cSIgor Mammedov 
3034fd7da4cSIgor Mammedov     /*
3044fd7da4cSIgor Mammedov      * make sure that acpi-index is unique across all present PCI devices
3054fd7da4cSIgor Mammedov      */
3064fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
3074fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
3084fd7da4cSIgor Mammedov 
3094fd7da4cSIgor Mammedov         if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index),
3104fd7da4cSIgor Mammedov                               g_cmp_uint32, NULL)) {
3114fd7da4cSIgor Mammedov             error_setg(errp, "a PCI device with acpi-index = %" PRIu32
3124fd7da4cSIgor Mammedov                        " already exist", pdev->acpi_index);
3134fd7da4cSIgor Mammedov             return;
3144fd7da4cSIgor Mammedov         }
3154fd7da4cSIgor Mammedov         g_sequence_insert_sorted(used_indexes,
3164fd7da4cSIgor Mammedov                                  GINT_TO_POINTER(pdev->acpi_index),
3174fd7da4cSIgor Mammedov                                  g_cmp_uint32, NULL);
3184fd7da4cSIgor Mammedov     }
319ec266f40SDavid Hildenbrand }
320ec266f40SDavid Hildenbrand 
3210058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
322c24d5e0bSIgor Mammedov                                DeviceState *dev, Error **errp)
323db4728e6SMichael S. Tsirkin {
324c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
325c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
326ec266f40SDavid Hildenbrand     int bsel;
327db4728e6SMichael S. Tsirkin 
328db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
329db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
330db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
331c24d5e0bSIgor Mammedov     if (!dev->hotplugged) {
3323e520926SDavid Hildenbrand         /*
3333e520926SDavid Hildenbrand          * Overwrite the default hotplug handler with the ACPI PCI one
3343e520926SDavid Hildenbrand          * for cold plugged bridges only.
3353e520926SDavid Hildenbrand          */
3363e520926SDavid Hildenbrand         if (!s->legacy_piix &&
3373e520926SDavid Hildenbrand             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
3383e520926SDavid Hildenbrand             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
3393e520926SDavid Hildenbrand 
340*3f3cbbb2SJulia Suvorova             /* Remove all hot-plug handlers if hot-plug is disabled on slot */
341*3f3cbbb2SJulia Suvorova             if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT) &&
342*3f3cbbb2SJulia Suvorova                 !PCIE_SLOT(pdev)->hotplug) {
343*3f3cbbb2SJulia Suvorova                 qbus_set_hotplug_handler(BUS(sec), NULL);
344*3f3cbbb2SJulia Suvorova                 return;
345*3f3cbbb2SJulia Suvorova             }
346*3f3cbbb2SJulia Suvorova 
3479bc6bfdfSMarkus Armbruster             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
3483e520926SDavid Hildenbrand             /* We don't have to overwrite any other hotplug handler yet */
3493e520926SDavid Hildenbrand             assert(QLIST_EMPTY(&sec->child));
3503e520926SDavid Hildenbrand         }
3513e520926SDavid Hildenbrand 
352c24d5e0bSIgor Mammedov         return;
353db4728e6SMichael S. Tsirkin     }
354db4728e6SMichael S. Tsirkin 
355ec266f40SDavid Hildenbrand     bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
356ec266f40SDavid Hildenbrand     g_assert(bsel >= 0);
3578f5001f9SIgor Mammedov     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
3580058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
359db4728e6SMichael S. Tsirkin }
360db4728e6SMichael S. Tsirkin 
3610058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
362c24d5e0bSIgor Mammedov                                  DeviceState *dev, Error **errp)
363c24d5e0bSIgor Mammedov {
3644fd7da4cSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
3654fd7da4cSIgor Mammedov 
36603459ea3SMarkus Armbruster     trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn),
36703459ea3SMarkus Armbruster                           acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))));
3684fd7da4cSIgor Mammedov 
3694fd7da4cSIgor Mammedov     /*
3704fd7da4cSIgor Mammedov      * clean up acpi-index so it could reused by another device
3714fd7da4cSIgor Mammedov      */
3724fd7da4cSIgor Mammedov     if (pdev->acpi_index) {
3734fd7da4cSIgor Mammedov         GSequence *used_indexes = pci_acpi_index_list();
3744fd7da4cSIgor Mammedov 
3754fd7da4cSIgor Mammedov         g_sequence_remove(g_sequence_lookup(used_indexes,
3764fd7da4cSIgor Mammedov                           GINT_TO_POINTER(pdev->acpi_index),
3774fd7da4cSIgor Mammedov                           g_cmp_uint32, NULL));
3784fd7da4cSIgor Mammedov     }
3794fd7da4cSIgor Mammedov 
380981c3dcdSMarkus Armbruster     qdev_unrealize(dev);
381c97adf3cSDavid Hildenbrand }
382c97adf3cSDavid Hildenbrand 
383c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
384c97adf3cSDavid Hildenbrand                                          AcpiPciHpState *s, DeviceState *dev,
385c97adf3cSDavid Hildenbrand                                          Error **errp)
386c97adf3cSDavid Hildenbrand {
387c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
388c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
389fd56e061SDavid Gibson     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
39003459ea3SMarkus Armbruster 
39103459ea3SMarkus Armbruster     trace_acpi_pci_unplug_request(bsel, slot);
39203459ea3SMarkus Armbruster 
393c24d5e0bSIgor Mammedov     if (bsel < 0) {
394c24d5e0bSIgor Mammedov         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
395c24d5e0bSIgor Mammedov                    ACPI_PCIHP_PROP_BSEL "' set");
396c24d5e0bSIgor Mammedov         return;
397c24d5e0bSIgor Mammedov     }
398c24d5e0bSIgor Mammedov 
399c24d5e0bSIgor Mammedov     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
4000058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
401db4728e6SMichael S. Tsirkin }
402db4728e6SMichael S. Tsirkin 
403db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
404db4728e6SMichael S. Tsirkin {
405db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
406db4728e6SMichael S. Tsirkin     uint32_t val = 0;
407db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
408db4728e6SMichael S. Tsirkin 
409fa365d7cSGonglei     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
410db4728e6SMichael S. Tsirkin         return 0;
411db4728e6SMichael S. Tsirkin     }
412db4728e6SMichael S. Tsirkin 
413db4728e6SMichael S. Tsirkin     switch (addr) {
414a7b613cfSIgor Mammedov     case PCI_UP_BASE:
4155a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
41699d09dd3SIgor Mammedov         if (!s->legacy_piix) {
4175a2223caSMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].up = 0;
41899d09dd3SIgor Mammedov         }
419df93b194SMarkus Armbruster         trace_acpi_pci_up_read(val);
420db4728e6SMichael S. Tsirkin         break;
421a7b613cfSIgor Mammedov     case PCI_DOWN_BASE:
422db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
423df93b194SMarkus Armbruster         trace_acpi_pci_down_read(val);
424db4728e6SMichael S. Tsirkin         break;
425a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
426df93b194SMarkus Armbruster         trace_acpi_pci_features_read(val);
427db4728e6SMichael S. Tsirkin         break;
428a7b613cfSIgor Mammedov     case PCI_RMV_BASE:
429db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
430df93b194SMarkus Armbruster         trace_acpi_pci_rmv_read(val);
431db4728e6SMichael S. Tsirkin         break;
432a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
433db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
434df93b194SMarkus Armbruster         trace_acpi_pci_sel_read(val);
435b32bd763SIgor Mammedov         break;
436b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
437b32bd763SIgor Mammedov         val = s->acpi_index;
438b32bd763SIgor Mammedov         s->acpi_index = 0;
439b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_read(val);
440b32bd763SIgor Mammedov         break;
441db4728e6SMichael S. Tsirkin     default:
442db4728e6SMichael S. Tsirkin         break;
443db4728e6SMichael S. Tsirkin     }
444db4728e6SMichael S. Tsirkin 
445db4728e6SMichael S. Tsirkin     return val;
446db4728e6SMichael S. Tsirkin }
447db4728e6SMichael S. Tsirkin 
448db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
449db4728e6SMichael S. Tsirkin                       unsigned int size)
450db4728e6SMichael S. Tsirkin {
451b32bd763SIgor Mammedov     int slot;
452b32bd763SIgor Mammedov     PCIBus *bus;
453b32bd763SIgor Mammedov     BusChild *kid, *next;
454db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
455b32bd763SIgor Mammedov 
456b32bd763SIgor Mammedov     s->acpi_index = 0;
457db4728e6SMichael S. Tsirkin     switch (addr) {
458b32bd763SIgor Mammedov     case PCI_AIDX_BASE:
459b32bd763SIgor Mammedov         /*
460b32bd763SIgor Mammedov          * fetch acpi-index for specified slot so that follow up read from
461b32bd763SIgor Mammedov          * PCI_AIDX_BASE can return it to guest
462b32bd763SIgor Mammedov          */
463b32bd763SIgor Mammedov         slot = ctz32(data);
464b32bd763SIgor Mammedov 
465b32bd763SIgor Mammedov         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
466b32bd763SIgor Mammedov             break;
467b32bd763SIgor Mammedov         }
468b32bd763SIgor Mammedov 
469b32bd763SIgor Mammedov         bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
470b32bd763SIgor Mammedov         QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
471b32bd763SIgor Mammedov             Object *o = OBJECT(kid->child);
472b32bd763SIgor Mammedov             PCIDevice *dev = PCI_DEVICE(o);
473b32bd763SIgor Mammedov             if (PCI_SLOT(dev->devfn) == slot) {
474b32bd763SIgor Mammedov                 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
475b32bd763SIgor Mammedov                 break;
476b32bd763SIgor Mammedov             }
477b32bd763SIgor Mammedov         }
478b32bd763SIgor Mammedov         trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
479b32bd763SIgor Mammedov         break;
480a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
481db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
482db4728e6SMichael S. Tsirkin             break;
483db4728e6SMichael S. Tsirkin         }
484db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
485df93b194SMarkus Armbruster         trace_acpi_pci_ej_write(addr, data);
486db4728e6SMichael S. Tsirkin         break;
487a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
488f5855994SAnthony PERARD         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
489df93b194SMarkus Armbruster         trace_acpi_pci_sel_write(addr, data);
490db4728e6SMichael S. Tsirkin     default:
491db4728e6SMichael S. Tsirkin         break;
492db4728e6SMichael S. Tsirkin     }
493db4728e6SMichael S. Tsirkin }
494db4728e6SMichael S. Tsirkin 
495db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
496db4728e6SMichael S. Tsirkin     .read = pci_read,
497db4728e6SMichael S. Tsirkin     .write = pci_write,
498db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
499db4728e6SMichael S. Tsirkin     .valid = {
500db4728e6SMichael S. Tsirkin         .min_access_size = 4,
501db4728e6SMichael S. Tsirkin         .max_access_size = 4,
502db4728e6SMichael S. Tsirkin     },
503db4728e6SMichael S. Tsirkin };
504db4728e6SMichael S. Tsirkin 
50578c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
506caf108bcSJulia Suvorova                      MemoryRegion *address_space_io, bool bridges_enabled,
507caf108bcSJulia Suvorova                      uint16_t io_base)
508db4728e6SMichael S. Tsirkin {
50978c2d872SIgor Mammedov     s->io_len = ACPI_PCIHP_SIZE;
510caf108bcSJulia Suvorova     s->io_base = io_base;
511e358edc8SIgor Mammedov 
512db4728e6SMichael S. Tsirkin     s->root = root_bus;
51399d09dd3SIgor Mammedov     s->legacy_piix = !bridges_enabled;
514e358edc8SIgor Mammedov 
51578c2d872SIgor Mammedov     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
51678c2d872SIgor Mammedov                           "acpi-pci-hotplug", s->io_len);
51778c2d872SIgor Mammedov     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
51878c2d872SIgor Mammedov 
51978c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
520d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
52178c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
522d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
523db4728e6SMichael S. Tsirkin }
524db4728e6SMichael S. Tsirkin 
525b32bd763SIgor Mammedov bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id)
526b32bd763SIgor Mammedov {
527b32bd763SIgor Mammedov      AcpiPciHpState *s = opaque;
528b32bd763SIgor Mammedov      return s->acpi_index;
529b32bd763SIgor Mammedov }
530b32bd763SIgor Mammedov 
531db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
532db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
533db4728e6SMichael S. Tsirkin     .version_id = 1,
534db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
535db4728e6SMichael S. Tsirkin     .fields = (VMStateField[]) {
536db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
537db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
538db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
539db4728e6SMichael S. Tsirkin     }
540db4728e6SMichael S. Tsirkin };
541