xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 61f3c91a67c4695dee6904d841b393ea21003969)
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
13*61f3c91aSChetan Pant  * License version 2.1 as published by the Free Software Foundation.
14db4728e6SMichael S. Tsirkin  *
15db4728e6SMichael S. Tsirkin  * This library is distributed in the hope that it will be useful,
16db4728e6SMichael S. Tsirkin  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17db4728e6SMichael S. Tsirkin  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18db4728e6SMichael S. Tsirkin  * Lesser General Public License for more details.
19db4728e6SMichael S. Tsirkin  *
20db4728e6SMichael S. Tsirkin  * You should have received a copy of the GNU Lesser General Public
21db4728e6SMichael S. Tsirkin  * License along with this library; if not, see <http://www.gnu.org/licenses/>
22db4728e6SMichael S. Tsirkin  *
23db4728e6SMichael S. Tsirkin  * Contributions after 2012-01-13 are licensed under the terms of the
24db4728e6SMichael S. Tsirkin  * GNU GPL, version 2 or (at your option) any later version.
25db4728e6SMichael S. Tsirkin  */
26db4728e6SMichael S. Tsirkin 
27b6a0aa05SPeter Maydell #include "qemu/osdep.h"
28db4728e6SMichael S. Tsirkin #include "hw/acpi/pcihp.h"
29db4728e6SMichael S. Tsirkin 
300fd61a2dSPhilippe Mathieu-Daudé #include "hw/pci-host/i440fx.h"
31db4728e6SMichael S. Tsirkin #include "hw/pci/pci.h"
323e520926SDavid Hildenbrand #include "hw/pci/pci_bridge.h"
33db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h"
34db4728e6SMichael S. Tsirkin #include "exec/address-spaces.h"
35db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h"
36d6454270SMarkus Armbruster #include "migration/vmstate.h"
37da34e65cSMarkus Armbruster #include "qapi/error.h"
38db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h"
39df93b194SMarkus Armbruster #include "trace.h"
40db4728e6SMichael S. Tsirkin 
41e358edc8SIgor Mammedov #define ACPI_PCIHP_ADDR 0xae00
42e358edc8SIgor Mammedov #define ACPI_PCIHP_SIZE 0x0014
43a7b613cfSIgor Mammedov #define PCI_UP_BASE 0x0000
44a7b613cfSIgor Mammedov #define PCI_DOWN_BASE 0x0004
45a7b613cfSIgor Mammedov #define PCI_EJ_BASE 0x0008
46a7b613cfSIgor Mammedov #define PCI_RMV_BASE 0x000c
47a7b613cfSIgor Mammedov #define PCI_SEL_BASE 0x0010
48db4728e6SMichael S. Tsirkin 
49db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind {
50db4728e6SMichael S. Tsirkin     int bsel;
51db4728e6SMichael S. Tsirkin     PCIBus *bus;
52db4728e6SMichael S. Tsirkin } AcpiPciHpFind;
53db4728e6SMichael S. Tsirkin 
54db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus)
55db4728e6SMichael S. Tsirkin {
567c38ecd0SKirill Batuzov     Error *local_err = NULL;
57c03d83d5SMarc-André Lureau     uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
587c38ecd0SKirill Batuzov                                              &local_err);
597c38ecd0SKirill Batuzov 
60c03d83d5SMarc-André Lureau     if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
617c38ecd0SKirill Batuzov         if (local_err) {
627c38ecd0SKirill Batuzov             error_free(local_err);
63db4728e6SMichael S. Tsirkin         }
64db4728e6SMichael S. Tsirkin         return -1;
657c38ecd0SKirill Batuzov     } else {
66db4728e6SMichael S. Tsirkin         return bsel;
67db4728e6SMichael S. Tsirkin     }
687c38ecd0SKirill Batuzov }
69db4728e6SMichael S. Tsirkin 
70ab938ae4SAnthony PERARD /* Assign BSEL property to all buses.  In the future, this can be changed
71ab938ae4SAnthony PERARD  * to only assign to buses that support hotplug.
72ab938ae4SAnthony PERARD  */
73ab938ae4SAnthony PERARD static void *acpi_set_bsel(PCIBus *bus, void *opaque)
74ab938ae4SAnthony PERARD {
75ab938ae4SAnthony PERARD     unsigned *bsel_alloc = opaque;
76ab938ae4SAnthony PERARD     unsigned *bus_bsel;
77ab938ae4SAnthony PERARD 
78ab938ae4SAnthony PERARD     if (qbus_is_hotpluggable(BUS(bus))) {
79ab938ae4SAnthony PERARD         bus_bsel = g_malloc(sizeof *bus_bsel);
80ab938ae4SAnthony PERARD 
81ab938ae4SAnthony PERARD         *bus_bsel = (*bsel_alloc)++;
82ab938ae4SAnthony PERARD         object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
83d2623129SMarkus Armbruster                                        bus_bsel, OBJ_PROP_FLAG_READ);
84ab938ae4SAnthony PERARD     }
85ab938ae4SAnthony PERARD 
86ab938ae4SAnthony PERARD     return bsel_alloc;
87ab938ae4SAnthony PERARD }
88ab938ae4SAnthony PERARD 
89ab938ae4SAnthony PERARD static void acpi_set_pci_info(void)
90ab938ae4SAnthony PERARD {
91ab938ae4SAnthony PERARD     static bool bsel_is_set;
92ab938ae4SAnthony PERARD     PCIBus *bus;
93ab938ae4SAnthony PERARD     unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
94ab938ae4SAnthony PERARD 
95ab938ae4SAnthony PERARD     if (bsel_is_set) {
96ab938ae4SAnthony PERARD         return;
97ab938ae4SAnthony PERARD     }
98ab938ae4SAnthony PERARD     bsel_is_set = true;
99ab938ae4SAnthony PERARD 
100ab938ae4SAnthony PERARD     bus = find_i440fx(); /* TODO: Q35 support */
101ab938ae4SAnthony PERARD     if (bus) {
102ab938ae4SAnthony PERARD         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
103ab938ae4SAnthony PERARD         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
104ab938ae4SAnthony PERARD     }
105ab938ae4SAnthony PERARD }
106ab938ae4SAnthony PERARD 
1073d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void)
1083d7e78aaSAni Sinha {
1093d7e78aaSAni Sinha     static bool root_hp_disabled;
1103d7e78aaSAni Sinha     PCIBus *bus;
1113d7e78aaSAni Sinha 
1123d7e78aaSAni Sinha     if (root_hp_disabled) {
1133d7e78aaSAni Sinha         return;
1143d7e78aaSAni Sinha     }
1153d7e78aaSAni Sinha 
1163d7e78aaSAni Sinha     bus = find_i440fx();
1173d7e78aaSAni Sinha     if (bus) {
1183d7e78aaSAni Sinha         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
1193d7e78aaSAni Sinha         qbus_set_hotplug_handler(BUS(bus), NULL);
1203d7e78aaSAni Sinha     }
1213d7e78aaSAni Sinha     root_hp_disabled = true;
1223d7e78aaSAni Sinha     return;
1233d7e78aaSAni Sinha }
1243d7e78aaSAni Sinha 
125db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
126db4728e6SMichael S. Tsirkin {
127db4728e6SMichael S. Tsirkin     AcpiPciHpFind *find = opaque;
128db4728e6SMichael S. Tsirkin     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
129db4728e6SMichael S. Tsirkin         find->bus = bus;
130db4728e6SMichael S. Tsirkin     }
131db4728e6SMichael S. Tsirkin }
132db4728e6SMichael S. Tsirkin 
133db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
134db4728e6SMichael S. Tsirkin {
135db4728e6SMichael S. Tsirkin     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
136db4728e6SMichael S. Tsirkin 
137db4728e6SMichael S. Tsirkin     if (bsel < 0) {
138db4728e6SMichael S. Tsirkin         return NULL;
139db4728e6SMichael S. Tsirkin     }
140db4728e6SMichael S. Tsirkin 
141db4728e6SMichael S. Tsirkin     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
142db4728e6SMichael S. Tsirkin 
143db4728e6SMichael S. Tsirkin     /* Make bsel 0 eject root bus if bsel property is not set,
144db4728e6SMichael S. Tsirkin      * for compatibility with non acpi setups.
145db4728e6SMichael S. Tsirkin      * TODO: really needed?
146db4728e6SMichael S. Tsirkin      */
147db4728e6SMichael S. Tsirkin     if (!bsel && !find.bus) {
148db4728e6SMichael S. Tsirkin         find.bus = s->root;
149db4728e6SMichael S. Tsirkin     }
1508ad038abSAni Sinha 
1518ad038abSAni Sinha     /*
1528ad038abSAni Sinha      * Check if find.bus is actually hotpluggable. If bsel is set to
1538ad038abSAni Sinha      * NULL for example on the root bus in order to make it
1548ad038abSAni Sinha      * non-hotpluggable, find.bus will match the root bus when bsel
1558ad038abSAni Sinha      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
1568ad038abSAni Sinha      * bus is not hotpluggable however, we should not select the bus.
1578ad038abSAni Sinha      * Instead, we should set find.bus to NULL in that case. In the check
1588ad038abSAni Sinha      * below, we generalize this case for all buses, not just the root bus.
1598ad038abSAni Sinha      * The callers of this function check for a null return value and
1608ad038abSAni Sinha      * handle them appropriately.
1618ad038abSAni Sinha      */
1628ad038abSAni Sinha     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
1638ad038abSAni Sinha         find.bus = NULL;
1648ad038abSAni Sinha     }
165db4728e6SMichael S. Tsirkin     return find.bus;
166db4728e6SMichael S. Tsirkin }
167db4728e6SMichael S. Tsirkin 
168db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
169db4728e6SMichael S. Tsirkin {
170db4728e6SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1712897ae02SIgor Mammedov     DeviceClass *dc = DEVICE_GET_CLASS(dev);
172db4728e6SMichael S. Tsirkin     /*
173db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
174db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
175db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
176db4728e6SMichael S. Tsirkin      */
1772897ae02SIgor Mammedov     return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
178db4728e6SMichael S. Tsirkin }
179db4728e6SMichael S. Tsirkin 
180db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
181db4728e6SMichael S. Tsirkin {
182c97adf3cSDavid Hildenbrand     HotplugHandler *hotplug_ctrl;
183db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
184786a4ea8SStefan Hajnoczi     int slot = ctz32(slots);
185db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
186db4728e6SMichael S. Tsirkin 
18703459ea3SMarkus Armbruster     trace_acpi_pci_eject_slot(bsel, slot);
18803459ea3SMarkus Armbruster 
189a3ec4bb7SIgor Mammedov     if (!bus || slot > 31) {
190db4728e6SMichael S. Tsirkin         return;
191db4728e6SMichael S. Tsirkin     }
192db4728e6SMichael S. Tsirkin 
193db4728e6SMichael S. Tsirkin     /* Mark request as complete */
194db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
1955a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
196db4728e6SMichael S. Tsirkin 
197db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
198db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
199db4728e6SMichael S. Tsirkin         PCIDevice *dev = PCI_DEVICE(qdev);
200db4728e6SMichael S. Tsirkin         if (PCI_SLOT(dev->devfn) == slot) {
2015a2223caSMichael S. Tsirkin             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
202c97adf3cSDavid Hildenbrand                 hotplug_ctrl = qdev_get_hotplug_handler(qdev);
203c97adf3cSDavid Hildenbrand                 hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
20407578b0aSDavid Hildenbrand                 object_unparent(OBJECT(qdev));
205db4728e6SMichael S. Tsirkin             }
206db4728e6SMichael S. Tsirkin         }
207db4728e6SMichael S. Tsirkin     }
208db4728e6SMichael S. Tsirkin }
209db4728e6SMichael S. Tsirkin 
210db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
211db4728e6SMichael S. Tsirkin {
212db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
213db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
214db4728e6SMichael S. Tsirkin 
215db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
216db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
217db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
218db4728e6SMichael S. Tsirkin     }
219db4728e6SMichael S. Tsirkin 
220db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
221db4728e6SMichael S. Tsirkin 
222db4728e6SMichael S. Tsirkin     if (!bus) {
223db4728e6SMichael S. Tsirkin         return;
224db4728e6SMichael S. Tsirkin     }
225db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
226db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
227db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
228db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
229db4728e6SMichael S. Tsirkin 
230db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
231db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
232db4728e6SMichael S. Tsirkin         }
233db4728e6SMichael S. Tsirkin     }
234db4728e6SMichael S. Tsirkin }
235db4728e6SMichael S. Tsirkin 
236db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
237db4728e6SMichael S. Tsirkin {
238db4728e6SMichael S. Tsirkin     int i;
239db4728e6SMichael S. Tsirkin 
240db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
241db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
242db4728e6SMichael S. Tsirkin     }
243db4728e6SMichael S. Tsirkin }
244db4728e6SMichael S. Tsirkin 
2453d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
246db4728e6SMichael S. Tsirkin {
2473d7e78aaSAni Sinha     if (acpihp_root_off) {
2483d7e78aaSAni Sinha         acpi_pcihp_disable_root_bus();
2493d7e78aaSAni Sinha     }
250ab938ae4SAnthony PERARD     acpi_set_pci_info();
251db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
252db4728e6SMichael S. Tsirkin }
253db4728e6SMichael S. Tsirkin 
254ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
255ec266f40SDavid Hildenbrand                                    DeviceState *dev, Error **errp)
256ec266f40SDavid Hildenbrand {
257ec266f40SDavid Hildenbrand     /* Only hotplugged devices need the hotplug capability. */
258ec266f40SDavid Hildenbrand     if (dev->hotplugged &&
259ec266f40SDavid Hildenbrand         acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) {
260ec266f40SDavid Hildenbrand         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
261ec266f40SDavid Hildenbrand                    ACPI_PCIHP_PROP_BSEL "' set");
262ec266f40SDavid Hildenbrand         return;
263ec266f40SDavid Hildenbrand     }
264ec266f40SDavid Hildenbrand }
265ec266f40SDavid Hildenbrand 
2660058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
267c24d5e0bSIgor Mammedov                                DeviceState *dev, Error **errp)
268db4728e6SMichael S. Tsirkin {
269c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
270c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
271ec266f40SDavid Hildenbrand     int bsel;
272db4728e6SMichael S. Tsirkin 
273db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
274db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
275db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
276c24d5e0bSIgor Mammedov     if (!dev->hotplugged) {
2773e520926SDavid Hildenbrand         /*
2783e520926SDavid Hildenbrand          * Overwrite the default hotplug handler with the ACPI PCI one
2793e520926SDavid Hildenbrand          * for cold plugged bridges only.
2803e520926SDavid Hildenbrand          */
2813e520926SDavid Hildenbrand         if (!s->legacy_piix &&
2823e520926SDavid Hildenbrand             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2833e520926SDavid Hildenbrand             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2843e520926SDavid Hildenbrand 
2859bc6bfdfSMarkus Armbruster             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
2863e520926SDavid Hildenbrand             /* We don't have to overwrite any other hotplug handler yet */
2873e520926SDavid Hildenbrand             assert(QLIST_EMPTY(&sec->child));
2883e520926SDavid Hildenbrand         }
2893e520926SDavid Hildenbrand 
290c24d5e0bSIgor Mammedov         return;
291db4728e6SMichael S. Tsirkin     }
292db4728e6SMichael S. Tsirkin 
293ec266f40SDavid Hildenbrand     bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
294ec266f40SDavid Hildenbrand     g_assert(bsel >= 0);
2958f5001f9SIgor Mammedov     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
2960058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
297db4728e6SMichael S. Tsirkin }
298db4728e6SMichael S. Tsirkin 
2990058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
300c24d5e0bSIgor Mammedov                                  DeviceState *dev, Error **errp)
301c24d5e0bSIgor Mammedov {
30203459ea3SMarkus Armbruster     trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn),
30303459ea3SMarkus Armbruster                           acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))));
304981c3dcdSMarkus Armbruster     qdev_unrealize(dev);
305c97adf3cSDavid Hildenbrand }
306c97adf3cSDavid Hildenbrand 
307c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
308c97adf3cSDavid Hildenbrand                                          AcpiPciHpState *s, DeviceState *dev,
309c97adf3cSDavid Hildenbrand                                          Error **errp)
310c97adf3cSDavid Hildenbrand {
311c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
312c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
313fd56e061SDavid Gibson     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
31403459ea3SMarkus Armbruster 
31503459ea3SMarkus Armbruster     trace_acpi_pci_unplug_request(bsel, slot);
31603459ea3SMarkus Armbruster 
317c24d5e0bSIgor Mammedov     if (bsel < 0) {
318c24d5e0bSIgor Mammedov         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
319c24d5e0bSIgor Mammedov                    ACPI_PCIHP_PROP_BSEL "' set");
320c24d5e0bSIgor Mammedov         return;
321c24d5e0bSIgor Mammedov     }
322c24d5e0bSIgor Mammedov 
323c24d5e0bSIgor Mammedov     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
3240058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
325db4728e6SMichael S. Tsirkin }
326db4728e6SMichael S. Tsirkin 
327db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
328db4728e6SMichael S. Tsirkin {
329db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
330db4728e6SMichael S. Tsirkin     uint32_t val = 0;
331db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
332db4728e6SMichael S. Tsirkin 
333fa365d7cSGonglei     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
334db4728e6SMichael S. Tsirkin         return 0;
335db4728e6SMichael S. Tsirkin     }
336db4728e6SMichael S. Tsirkin 
337db4728e6SMichael S. Tsirkin     switch (addr) {
338a7b613cfSIgor Mammedov     case PCI_UP_BASE:
3395a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
34099d09dd3SIgor Mammedov         if (!s->legacy_piix) {
3415a2223caSMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].up = 0;
34299d09dd3SIgor Mammedov         }
343df93b194SMarkus Armbruster         trace_acpi_pci_up_read(val);
344db4728e6SMichael S. Tsirkin         break;
345a7b613cfSIgor Mammedov     case PCI_DOWN_BASE:
346db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
347df93b194SMarkus Armbruster         trace_acpi_pci_down_read(val);
348db4728e6SMichael S. Tsirkin         break;
349a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
350db4728e6SMichael S. Tsirkin         /* No feature defined yet */
351df93b194SMarkus Armbruster         trace_acpi_pci_features_read(val);
352db4728e6SMichael S. Tsirkin         break;
353a7b613cfSIgor Mammedov     case PCI_RMV_BASE:
354db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
355df93b194SMarkus Armbruster         trace_acpi_pci_rmv_read(val);
356db4728e6SMichael S. Tsirkin         break;
357a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
358db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
359df93b194SMarkus Armbruster         trace_acpi_pci_sel_read(val);
360db4728e6SMichael S. Tsirkin     default:
361db4728e6SMichael S. Tsirkin         break;
362db4728e6SMichael S. Tsirkin     }
363db4728e6SMichael S. Tsirkin 
364db4728e6SMichael S. Tsirkin     return val;
365db4728e6SMichael S. Tsirkin }
366db4728e6SMichael S. Tsirkin 
367db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
368db4728e6SMichael S. Tsirkin                       unsigned int size)
369db4728e6SMichael S. Tsirkin {
370db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
371db4728e6SMichael S. Tsirkin     switch (addr) {
372a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
373db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
374db4728e6SMichael S. Tsirkin             break;
375db4728e6SMichael S. Tsirkin         }
376db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
377df93b194SMarkus Armbruster         trace_acpi_pci_ej_write(addr, data);
378db4728e6SMichael S. Tsirkin         break;
379a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
380f5855994SAnthony PERARD         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
381df93b194SMarkus Armbruster         trace_acpi_pci_sel_write(addr, data);
382db4728e6SMichael S. Tsirkin     default:
383db4728e6SMichael S. Tsirkin         break;
384db4728e6SMichael S. Tsirkin     }
385db4728e6SMichael S. Tsirkin }
386db4728e6SMichael S. Tsirkin 
387db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
388db4728e6SMichael S. Tsirkin     .read = pci_read,
389db4728e6SMichael S. Tsirkin     .write = pci_write,
390db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
391db4728e6SMichael S. Tsirkin     .valid = {
392db4728e6SMichael S. Tsirkin         .min_access_size = 4,
393db4728e6SMichael S. Tsirkin         .max_access_size = 4,
394db4728e6SMichael S. Tsirkin     },
395db4728e6SMichael S. Tsirkin };
396db4728e6SMichael S. Tsirkin 
39778c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
39899d09dd3SIgor Mammedov                      MemoryRegion *address_space_io, bool bridges_enabled)
399db4728e6SMichael S. Tsirkin {
40078c2d872SIgor Mammedov     s->io_len = ACPI_PCIHP_SIZE;
40178c2d872SIgor Mammedov     s->io_base = ACPI_PCIHP_ADDR;
402e358edc8SIgor Mammedov 
403db4728e6SMichael S. Tsirkin     s->root = root_bus;
40499d09dd3SIgor Mammedov     s->legacy_piix = !bridges_enabled;
405e358edc8SIgor Mammedov 
40678c2d872SIgor Mammedov     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
40778c2d872SIgor Mammedov                           "acpi-pci-hotplug", s->io_len);
40878c2d872SIgor Mammedov     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
40978c2d872SIgor Mammedov 
41078c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
411d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
41278c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
413d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
414db4728e6SMichael S. Tsirkin }
415db4728e6SMichael S. Tsirkin 
416db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
417db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
418db4728e6SMichael S. Tsirkin     .version_id = 1,
419db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
420db4728e6SMichael S. Tsirkin     .fields = (VMStateField[]) {
421db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
422db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
423db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
424db4728e6SMichael S. Tsirkin     }
425db4728e6SMichael S. Tsirkin };
426