xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 3d7e78aa7777f052d72ec6070e611767f0db35ce)
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
13db4728e6SMichael S. Tsirkin  * License version 2 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 
107*3d7e78aaSAni Sinha static void acpi_pcihp_disable_root_bus(void)
108*3d7e78aaSAni Sinha {
109*3d7e78aaSAni Sinha     static bool root_hp_disabled;
110*3d7e78aaSAni Sinha     PCIBus *bus;
111*3d7e78aaSAni Sinha 
112*3d7e78aaSAni Sinha     if (root_hp_disabled) {
113*3d7e78aaSAni Sinha         return;
114*3d7e78aaSAni Sinha     }
115*3d7e78aaSAni Sinha 
116*3d7e78aaSAni Sinha     bus = find_i440fx();
117*3d7e78aaSAni Sinha     if (bus) {
118*3d7e78aaSAni Sinha         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
119*3d7e78aaSAni Sinha         qbus_set_hotplug_handler(BUS(bus), NULL);
120*3d7e78aaSAni Sinha     }
121*3d7e78aaSAni Sinha     root_hp_disabled = true;
122*3d7e78aaSAni Sinha     return;
123*3d7e78aaSAni Sinha }
124*3d7e78aaSAni 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     }
150db4728e6SMichael S. Tsirkin     return find.bus;
151db4728e6SMichael S. Tsirkin }
152db4728e6SMichael S. Tsirkin 
153db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
154db4728e6SMichael S. Tsirkin {
155db4728e6SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
1562897ae02SIgor Mammedov     DeviceClass *dc = DEVICE_GET_CLASS(dev);
157db4728e6SMichael S. Tsirkin     /*
158db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
159db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
160db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
161db4728e6SMichael S. Tsirkin      */
1622897ae02SIgor Mammedov     return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
163db4728e6SMichael S. Tsirkin }
164db4728e6SMichael S. Tsirkin 
165db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
166db4728e6SMichael S. Tsirkin {
167c97adf3cSDavid Hildenbrand     HotplugHandler *hotplug_ctrl;
168db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
169786a4ea8SStefan Hajnoczi     int slot = ctz32(slots);
170db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
171db4728e6SMichael S. Tsirkin 
17203459ea3SMarkus Armbruster     trace_acpi_pci_eject_slot(bsel, slot);
17303459ea3SMarkus Armbruster 
174a3ec4bb7SIgor Mammedov     if (!bus || slot > 31) {
175db4728e6SMichael S. Tsirkin         return;
176db4728e6SMichael S. Tsirkin     }
177db4728e6SMichael S. Tsirkin 
178db4728e6SMichael S. Tsirkin     /* Mark request as complete */
179db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
1805a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
181db4728e6SMichael S. Tsirkin 
182db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
183db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
184db4728e6SMichael S. Tsirkin         PCIDevice *dev = PCI_DEVICE(qdev);
185db4728e6SMichael S. Tsirkin         if (PCI_SLOT(dev->devfn) == slot) {
1865a2223caSMichael S. Tsirkin             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
187c97adf3cSDavid Hildenbrand                 hotplug_ctrl = qdev_get_hotplug_handler(qdev);
188c97adf3cSDavid Hildenbrand                 hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
18907578b0aSDavid Hildenbrand                 object_unparent(OBJECT(qdev));
190db4728e6SMichael S. Tsirkin             }
191db4728e6SMichael S. Tsirkin         }
192db4728e6SMichael S. Tsirkin     }
193db4728e6SMichael S. Tsirkin }
194db4728e6SMichael S. Tsirkin 
195db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
196db4728e6SMichael S. Tsirkin {
197db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
198db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
199db4728e6SMichael S. Tsirkin 
200db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
201db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
202db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
203db4728e6SMichael S. Tsirkin     }
204db4728e6SMichael S. Tsirkin 
205db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
206db4728e6SMichael S. Tsirkin 
207db4728e6SMichael S. Tsirkin     if (!bus) {
208db4728e6SMichael S. Tsirkin         return;
209db4728e6SMichael S. Tsirkin     }
210db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
211db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
212db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
213db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
214db4728e6SMichael S. Tsirkin 
215db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
216db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
217db4728e6SMichael S. Tsirkin         }
218db4728e6SMichael S. Tsirkin     }
219db4728e6SMichael S. Tsirkin }
220db4728e6SMichael S. Tsirkin 
221db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
222db4728e6SMichael S. Tsirkin {
223db4728e6SMichael S. Tsirkin     int i;
224db4728e6SMichael S. Tsirkin 
225db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
226db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
227db4728e6SMichael S. Tsirkin     }
228db4728e6SMichael S. Tsirkin }
229db4728e6SMichael S. Tsirkin 
230*3d7e78aaSAni Sinha void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
231db4728e6SMichael S. Tsirkin {
232*3d7e78aaSAni Sinha     if (acpihp_root_off) {
233*3d7e78aaSAni Sinha         acpi_pcihp_disable_root_bus();
234*3d7e78aaSAni Sinha     }
235ab938ae4SAnthony PERARD     acpi_set_pci_info();
236db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
237db4728e6SMichael S. Tsirkin }
238db4728e6SMichael S. Tsirkin 
239ec266f40SDavid Hildenbrand void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
240ec266f40SDavid Hildenbrand                                    DeviceState *dev, Error **errp)
241ec266f40SDavid Hildenbrand {
242ec266f40SDavid Hildenbrand     /* Only hotplugged devices need the hotplug capability. */
243ec266f40SDavid Hildenbrand     if (dev->hotplugged &&
244ec266f40SDavid Hildenbrand         acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))) < 0) {
245ec266f40SDavid Hildenbrand         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
246ec266f40SDavid Hildenbrand                    ACPI_PCIHP_PROP_BSEL "' set");
247ec266f40SDavid Hildenbrand         return;
248ec266f40SDavid Hildenbrand     }
249ec266f40SDavid Hildenbrand }
250ec266f40SDavid Hildenbrand 
2510058c082SIgor Mammedov void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
252c24d5e0bSIgor Mammedov                                DeviceState *dev, Error **errp)
253db4728e6SMichael S. Tsirkin {
254c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
255c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
256ec266f40SDavid Hildenbrand     int bsel;
257db4728e6SMichael S. Tsirkin 
258db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
259db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
260db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
261c24d5e0bSIgor Mammedov     if (!dev->hotplugged) {
2623e520926SDavid Hildenbrand         /*
2633e520926SDavid Hildenbrand          * Overwrite the default hotplug handler with the ACPI PCI one
2643e520926SDavid Hildenbrand          * for cold plugged bridges only.
2653e520926SDavid Hildenbrand          */
2663e520926SDavid Hildenbrand         if (!s->legacy_piix &&
2673e520926SDavid Hildenbrand             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
2683e520926SDavid Hildenbrand             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
2693e520926SDavid Hildenbrand 
2709bc6bfdfSMarkus Armbruster             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
2713e520926SDavid Hildenbrand             /* We don't have to overwrite any other hotplug handler yet */
2723e520926SDavid Hildenbrand             assert(QLIST_EMPTY(&sec->child));
2733e520926SDavid Hildenbrand         }
2743e520926SDavid Hildenbrand 
275c24d5e0bSIgor Mammedov         return;
276db4728e6SMichael S. Tsirkin     }
277db4728e6SMichael S. Tsirkin 
278ec266f40SDavid Hildenbrand     bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
279ec266f40SDavid Hildenbrand     g_assert(bsel >= 0);
2808f5001f9SIgor Mammedov     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
2810058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
282db4728e6SMichael S. Tsirkin }
283db4728e6SMichael S. Tsirkin 
2840058c082SIgor Mammedov void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
285c24d5e0bSIgor Mammedov                                  DeviceState *dev, Error **errp)
286c24d5e0bSIgor Mammedov {
28703459ea3SMarkus Armbruster     trace_acpi_pci_unplug(PCI_SLOT(PCI_DEVICE(dev)->devfn),
28803459ea3SMarkus Armbruster                           acpi_pcihp_get_bsel(pci_get_bus(PCI_DEVICE(dev))));
289981c3dcdSMarkus Armbruster     qdev_unrealize(dev);
290c97adf3cSDavid Hildenbrand }
291c97adf3cSDavid Hildenbrand 
292c97adf3cSDavid Hildenbrand void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
293c97adf3cSDavid Hildenbrand                                          AcpiPciHpState *s, DeviceState *dev,
294c97adf3cSDavid Hildenbrand                                          Error **errp)
295c97adf3cSDavid Hildenbrand {
296c24d5e0bSIgor Mammedov     PCIDevice *pdev = PCI_DEVICE(dev);
297c24d5e0bSIgor Mammedov     int slot = PCI_SLOT(pdev->devfn);
298fd56e061SDavid Gibson     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
29903459ea3SMarkus Armbruster 
30003459ea3SMarkus Armbruster     trace_acpi_pci_unplug_request(bsel, slot);
30103459ea3SMarkus Armbruster 
302c24d5e0bSIgor Mammedov     if (bsel < 0) {
303c24d5e0bSIgor Mammedov         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
304c24d5e0bSIgor Mammedov                    ACPI_PCIHP_PROP_BSEL "' set");
305c24d5e0bSIgor Mammedov         return;
306c24d5e0bSIgor Mammedov     }
307c24d5e0bSIgor Mammedov 
308c24d5e0bSIgor Mammedov     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
3090058c082SIgor Mammedov     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
310db4728e6SMichael S. Tsirkin }
311db4728e6SMichael S. Tsirkin 
312db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
313db4728e6SMichael S. Tsirkin {
314db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
315db4728e6SMichael S. Tsirkin     uint32_t val = 0;
316db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
317db4728e6SMichael S. Tsirkin 
318fa365d7cSGonglei     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
319db4728e6SMichael S. Tsirkin         return 0;
320db4728e6SMichael S. Tsirkin     }
321db4728e6SMichael S. Tsirkin 
322db4728e6SMichael S. Tsirkin     switch (addr) {
323a7b613cfSIgor Mammedov     case PCI_UP_BASE:
3245a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
32599d09dd3SIgor Mammedov         if (!s->legacy_piix) {
3265a2223caSMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].up = 0;
32799d09dd3SIgor Mammedov         }
328df93b194SMarkus Armbruster         trace_acpi_pci_up_read(val);
329db4728e6SMichael S. Tsirkin         break;
330a7b613cfSIgor Mammedov     case PCI_DOWN_BASE:
331db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
332df93b194SMarkus Armbruster         trace_acpi_pci_down_read(val);
333db4728e6SMichael S. Tsirkin         break;
334a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
335db4728e6SMichael S. Tsirkin         /* No feature defined yet */
336df93b194SMarkus Armbruster         trace_acpi_pci_features_read(val);
337db4728e6SMichael S. Tsirkin         break;
338a7b613cfSIgor Mammedov     case PCI_RMV_BASE:
339db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
340df93b194SMarkus Armbruster         trace_acpi_pci_rmv_read(val);
341db4728e6SMichael S. Tsirkin         break;
342a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
343db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
344df93b194SMarkus Armbruster         trace_acpi_pci_sel_read(val);
345db4728e6SMichael S. Tsirkin     default:
346db4728e6SMichael S. Tsirkin         break;
347db4728e6SMichael S. Tsirkin     }
348db4728e6SMichael S. Tsirkin 
349db4728e6SMichael S. Tsirkin     return val;
350db4728e6SMichael S. Tsirkin }
351db4728e6SMichael S. Tsirkin 
352db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
353db4728e6SMichael S. Tsirkin                       unsigned int size)
354db4728e6SMichael S. Tsirkin {
355db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
356db4728e6SMichael S. Tsirkin     switch (addr) {
357a7b613cfSIgor Mammedov     case PCI_EJ_BASE:
358db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
359db4728e6SMichael S. Tsirkin             break;
360db4728e6SMichael S. Tsirkin         }
361db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
362df93b194SMarkus Armbruster         trace_acpi_pci_ej_write(addr, data);
363db4728e6SMichael S. Tsirkin         break;
364a7b613cfSIgor Mammedov     case PCI_SEL_BASE:
365f5855994SAnthony PERARD         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
366df93b194SMarkus Armbruster         trace_acpi_pci_sel_write(addr, data);
367db4728e6SMichael S. Tsirkin     default:
368db4728e6SMichael S. Tsirkin         break;
369db4728e6SMichael S. Tsirkin     }
370db4728e6SMichael S. Tsirkin }
371db4728e6SMichael S. Tsirkin 
372db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
373db4728e6SMichael S. Tsirkin     .read = pci_read,
374db4728e6SMichael S. Tsirkin     .write = pci_write,
375db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
376db4728e6SMichael S. Tsirkin     .valid = {
377db4728e6SMichael S. Tsirkin         .min_access_size = 4,
378db4728e6SMichael S. Tsirkin         .max_access_size = 4,
379db4728e6SMichael S. Tsirkin     },
380db4728e6SMichael S. Tsirkin };
381db4728e6SMichael S. Tsirkin 
38278c2d872SIgor Mammedov void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
38399d09dd3SIgor Mammedov                      MemoryRegion *address_space_io, bool bridges_enabled)
384db4728e6SMichael S. Tsirkin {
38578c2d872SIgor Mammedov     s->io_len = ACPI_PCIHP_SIZE;
38678c2d872SIgor Mammedov     s->io_base = ACPI_PCIHP_ADDR;
387e358edc8SIgor Mammedov 
388db4728e6SMichael S. Tsirkin     s->root= root_bus;
38999d09dd3SIgor Mammedov     s->legacy_piix = !bridges_enabled;
390e358edc8SIgor Mammedov 
39178c2d872SIgor Mammedov     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
39278c2d872SIgor Mammedov                           "acpi-pci-hotplug", s->io_len);
39378c2d872SIgor Mammedov     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
39478c2d872SIgor Mammedov 
39578c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
396d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
39778c2d872SIgor Mammedov     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
398d2623129SMarkus Armbruster                                    OBJ_PROP_FLAG_READ);
399db4728e6SMichael S. Tsirkin }
400db4728e6SMichael S. Tsirkin 
401db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
402db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
403db4728e6SMichael S. Tsirkin     .version_id = 1,
404db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
405db4728e6SMichael S. Tsirkin     .fields = (VMStateField[]) {
406db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
407db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
408db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
409db4728e6SMichael S. Tsirkin     }
410db4728e6SMichael S. Tsirkin };
411