xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 5a2223ca26b1a34e131b5b9a63599d9426d2c25c)
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 
27db4728e6SMichael S. Tsirkin #include "hw/acpi/pcihp.h"
28db4728e6SMichael S. Tsirkin 
29db4728e6SMichael S. Tsirkin #include "hw/hw.h"
30db4728e6SMichael S. Tsirkin #include "hw/i386/pc.h"
31db4728e6SMichael S. Tsirkin #include "hw/pci/pci.h"
32db4728e6SMichael S. Tsirkin #include "hw/acpi/acpi.h"
33db4728e6SMichael S. Tsirkin #include "sysemu/sysemu.h"
34db4728e6SMichael S. Tsirkin #include "qemu/range.h"
35db4728e6SMichael S. Tsirkin #include "exec/ioport.h"
36db4728e6SMichael S. Tsirkin #include "exec/address-spaces.h"
37db4728e6SMichael S. Tsirkin #include "hw/pci/pci_bus.h"
38db4728e6SMichael S. Tsirkin #include "qom/qom-qobject.h"
39db4728e6SMichael S. Tsirkin #include "qapi/qmp/qint.h"
40db4728e6SMichael S. Tsirkin 
41db4728e6SMichael S. Tsirkin //#define DEBUG
42db4728e6SMichael S. Tsirkin 
43db4728e6SMichael S. Tsirkin #ifdef DEBUG
44db4728e6SMichael S. Tsirkin # define ACPI_PCIHP_DPRINTF(format, ...)     printf(format, ## __VA_ARGS__)
45db4728e6SMichael S. Tsirkin #else
46db4728e6SMichael S. Tsirkin # define ACPI_PCIHP_DPRINTF(format, ...)     do { } while (0)
47db4728e6SMichael S. Tsirkin #endif
48db4728e6SMichael S. Tsirkin 
49db4728e6SMichael S. Tsirkin #define PCI_HOTPLUG_ADDR 0xae00
50db4728e6SMichael S. Tsirkin #define PCI_HOTPLUG_SIZE 0x0014
51db4728e6SMichael S. Tsirkin #define PCI_UP_BASE 0xae00
52db4728e6SMichael S. Tsirkin #define PCI_DOWN_BASE 0xae04
53db4728e6SMichael S. Tsirkin #define PCI_EJ_BASE 0xae08
54db4728e6SMichael S. Tsirkin #define PCI_RMV_BASE 0xae0c
55db4728e6SMichael S. Tsirkin #define PCI_SEL_BASE 0xae10
56db4728e6SMichael S. Tsirkin 
57db4728e6SMichael S. Tsirkin typedef struct AcpiPciHpFind {
58db4728e6SMichael S. Tsirkin     int bsel;
59db4728e6SMichael S. Tsirkin     PCIBus *bus;
60db4728e6SMichael S. Tsirkin } AcpiPciHpFind;
61db4728e6SMichael S. Tsirkin 
62db4728e6SMichael S. Tsirkin static int acpi_pcihp_get_bsel(PCIBus *bus)
63db4728e6SMichael S. Tsirkin {
64db4728e6SMichael S. Tsirkin     QObject *o = object_property_get_qobject(OBJECT(bus),
65db4728e6SMichael S. Tsirkin                                              ACPI_PCIHP_PROP_BSEL, NULL);
66db4728e6SMichael S. Tsirkin     int64_t bsel = -1;
67db4728e6SMichael S. Tsirkin     if (o) {
68db4728e6SMichael S. Tsirkin         bsel = qint_get_int(qobject_to_qint(o));
69db4728e6SMichael S. Tsirkin     }
70db4728e6SMichael S. Tsirkin     if (bsel < 0) {
71db4728e6SMichael S. Tsirkin         return -1;
72db4728e6SMichael S. Tsirkin     }
73db4728e6SMichael S. Tsirkin     return bsel;
74db4728e6SMichael S. Tsirkin }
75db4728e6SMichael S. Tsirkin 
76db4728e6SMichael S. Tsirkin static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
77db4728e6SMichael S. Tsirkin {
78db4728e6SMichael S. Tsirkin     AcpiPciHpFind *find = opaque;
79db4728e6SMichael S. Tsirkin     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
80db4728e6SMichael S. Tsirkin         find->bus = bus;
81db4728e6SMichael S. Tsirkin     }
82db4728e6SMichael S. Tsirkin }
83db4728e6SMichael S. Tsirkin 
84db4728e6SMichael S. Tsirkin static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
85db4728e6SMichael S. Tsirkin {
86db4728e6SMichael S. Tsirkin     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
87db4728e6SMichael S. Tsirkin 
88db4728e6SMichael S. Tsirkin     if (bsel < 0) {
89db4728e6SMichael S. Tsirkin         return NULL;
90db4728e6SMichael S. Tsirkin     }
91db4728e6SMichael S. Tsirkin 
92db4728e6SMichael S. Tsirkin     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
93db4728e6SMichael S. Tsirkin 
94db4728e6SMichael S. Tsirkin     /* Make bsel 0 eject root bus if bsel property is not set,
95db4728e6SMichael S. Tsirkin      * for compatibility with non acpi setups.
96db4728e6SMichael S. Tsirkin      * TODO: really needed?
97db4728e6SMichael S. Tsirkin      */
98db4728e6SMichael S. Tsirkin     if (!bsel && !find.bus) {
99db4728e6SMichael S. Tsirkin         find.bus = s->root;
100db4728e6SMichael S. Tsirkin     }
101db4728e6SMichael S. Tsirkin     return find.bus;
102db4728e6SMichael S. Tsirkin }
103db4728e6SMichael S. Tsirkin 
104db4728e6SMichael S. Tsirkin static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
105db4728e6SMichael S. Tsirkin {
106db4728e6SMichael S. Tsirkin     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
107db4728e6SMichael S. Tsirkin     /*
108db4728e6SMichael S. Tsirkin      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
109db4728e6SMichael S. Tsirkin      * hot-unplug of bridge devices unless they were added by hotplug
110db4728e6SMichael S. Tsirkin      * (and so, not described by acpi).
111db4728e6SMichael S. Tsirkin      */
112db4728e6SMichael S. Tsirkin     return (pc->is_bridge && !dev->qdev.hotplugged) || pc->no_hotplug;
113db4728e6SMichael S. Tsirkin }
114db4728e6SMichael S. Tsirkin 
115db4728e6SMichael S. Tsirkin static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
116db4728e6SMichael S. Tsirkin {
117db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
118db4728e6SMichael S. Tsirkin     int slot = ffs(slots) - 1;
119db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
120db4728e6SMichael S. Tsirkin 
121db4728e6SMichael S. Tsirkin     if (!bus) {
122db4728e6SMichael S. Tsirkin         return;
123db4728e6SMichael S. Tsirkin     }
124db4728e6SMichael S. Tsirkin 
125db4728e6SMichael S. Tsirkin     /* Mark request as complete */
126db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
127*5a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
128db4728e6SMichael S. Tsirkin 
129db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
130db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
131db4728e6SMichael S. Tsirkin         PCIDevice *dev = PCI_DEVICE(qdev);
132db4728e6SMichael S. Tsirkin         if (PCI_SLOT(dev->devfn) == slot) {
133*5a2223caSMichael S. Tsirkin             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
134db4728e6SMichael S. Tsirkin                 object_unparent(OBJECT(qdev));
135db4728e6SMichael S. Tsirkin             }
136db4728e6SMichael S. Tsirkin         }
137db4728e6SMichael S. Tsirkin     }
138db4728e6SMichael S. Tsirkin }
139db4728e6SMichael S. Tsirkin 
140db4728e6SMichael S. Tsirkin static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
141db4728e6SMichael S. Tsirkin {
142db4728e6SMichael S. Tsirkin     BusChild *kid, *next;
143db4728e6SMichael S. Tsirkin     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
144db4728e6SMichael S. Tsirkin 
145db4728e6SMichael S. Tsirkin     /* Execute any pending removes during reset */
146db4728e6SMichael S. Tsirkin     while (s->acpi_pcihp_pci_status[bsel].down) {
147db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
148db4728e6SMichael S. Tsirkin     }
149db4728e6SMichael S. Tsirkin 
150db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
151db4728e6SMichael S. Tsirkin 
152db4728e6SMichael S. Tsirkin     if (!bus) {
153db4728e6SMichael S. Tsirkin         return;
154db4728e6SMichael S. Tsirkin     }
155db4728e6SMichael S. Tsirkin     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
156db4728e6SMichael S. Tsirkin         DeviceState *qdev = kid->child;
157db4728e6SMichael S. Tsirkin         PCIDevice *pdev = PCI_DEVICE(qdev);
158db4728e6SMichael S. Tsirkin         int slot = PCI_SLOT(pdev->devfn);
159db4728e6SMichael S. Tsirkin 
160db4728e6SMichael S. Tsirkin         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
161db4728e6SMichael S. Tsirkin             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
162db4728e6SMichael S. Tsirkin         }
163db4728e6SMichael S. Tsirkin     }
164db4728e6SMichael S. Tsirkin }
165db4728e6SMichael S. Tsirkin 
166db4728e6SMichael S. Tsirkin static void acpi_pcihp_update(AcpiPciHpState *s)
167db4728e6SMichael S. Tsirkin {
168db4728e6SMichael S. Tsirkin     int i;
169db4728e6SMichael S. Tsirkin 
170db4728e6SMichael S. Tsirkin     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
171db4728e6SMichael S. Tsirkin         acpi_pcihp_update_hotplug_bus(s, i);
172db4728e6SMichael S. Tsirkin     }
173db4728e6SMichael S. Tsirkin }
174db4728e6SMichael S. Tsirkin 
175db4728e6SMichael S. Tsirkin void acpi_pcihp_reset(AcpiPciHpState *s)
176db4728e6SMichael S. Tsirkin {
177db4728e6SMichael S. Tsirkin     acpi_pcihp_update(s);
178db4728e6SMichael S. Tsirkin }
179db4728e6SMichael S. Tsirkin 
180db4728e6SMichael S. Tsirkin static void enable_device(AcpiPciHpState *s, unsigned bsel, int slot)
181db4728e6SMichael S. Tsirkin {
182*5a2223caSMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
183db4728e6SMichael S. Tsirkin }
184db4728e6SMichael S. Tsirkin 
185db4728e6SMichael S. Tsirkin static void disable_device(AcpiPciHpState *s, unsigned bsel, int slot)
186db4728e6SMichael S. Tsirkin {
187db4728e6SMichael S. Tsirkin     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
188db4728e6SMichael S. Tsirkin }
189db4728e6SMichael S. Tsirkin 
190db4728e6SMichael S. Tsirkin int acpi_pcihp_device_hotplug(AcpiPciHpState *s, PCIDevice *dev,
191db4728e6SMichael S. Tsirkin                               PCIHotplugState state)
192db4728e6SMichael S. Tsirkin {
193db4728e6SMichael S. Tsirkin     int slot = PCI_SLOT(dev->devfn);
194db4728e6SMichael S. Tsirkin     int bsel = acpi_pcihp_get_bsel(dev->bus);
195db4728e6SMichael S. Tsirkin     if (bsel < 0) {
196db4728e6SMichael S. Tsirkin         return -1;
197db4728e6SMichael S. Tsirkin     }
198db4728e6SMichael S. Tsirkin 
199db4728e6SMichael S. Tsirkin     /* Don't send event when device is enabled during qemu machine creation:
200db4728e6SMichael S. Tsirkin      * it is present on boot, no hotplug event is necessary. We do send an
201db4728e6SMichael S. Tsirkin      * event when the device is disabled later. */
202db4728e6SMichael S. Tsirkin     if (state == PCI_COLDPLUG_ENABLED) {
203db4728e6SMichael S. Tsirkin         return 0;
204db4728e6SMichael S. Tsirkin     }
205db4728e6SMichael S. Tsirkin 
206db4728e6SMichael S. Tsirkin     if (state == PCI_HOTPLUG_ENABLED) {
207db4728e6SMichael S. Tsirkin         enable_device(s, bsel, slot);
208db4728e6SMichael S. Tsirkin     } else {
209db4728e6SMichael S. Tsirkin         disable_device(s, bsel, slot);
210db4728e6SMichael S. Tsirkin     }
211db4728e6SMichael S. Tsirkin 
212db4728e6SMichael S. Tsirkin     return 0;
213db4728e6SMichael S. Tsirkin }
214db4728e6SMichael S. Tsirkin 
215db4728e6SMichael S. Tsirkin static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
216db4728e6SMichael S. Tsirkin {
217db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
218db4728e6SMichael S. Tsirkin     uint32_t val = 0;
219db4728e6SMichael S. Tsirkin     int bsel = s->hotplug_select;
220db4728e6SMichael S. Tsirkin 
221db4728e6SMichael S. Tsirkin     if (bsel < 0 || bsel > ACPI_PCIHP_MAX_HOTPLUG_BUS) {
222db4728e6SMichael S. Tsirkin         return 0;
223db4728e6SMichael S. Tsirkin     }
224db4728e6SMichael S. Tsirkin 
225db4728e6SMichael S. Tsirkin     switch (addr) {
226db4728e6SMichael S. Tsirkin     case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
227*5a2223caSMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].up;
228*5a2223caSMichael S. Tsirkin         s->acpi_pcihp_pci_status[bsel].up = 0;
229db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
230db4728e6SMichael S. Tsirkin         break;
231db4728e6SMichael S. Tsirkin     case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
232db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].down;
233db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
234db4728e6SMichael S. Tsirkin         break;
235db4728e6SMichael S. Tsirkin     case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
236db4728e6SMichael S. Tsirkin         /* No feature defined yet */
237db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
238db4728e6SMichael S. Tsirkin         break;
239db4728e6SMichael S. Tsirkin     case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
240db4728e6SMichael S. Tsirkin         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
241db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
242db4728e6SMichael S. Tsirkin         break;
243db4728e6SMichael S. Tsirkin     case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
244db4728e6SMichael S. Tsirkin         val = s->hotplug_select;
245db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
246db4728e6SMichael S. Tsirkin     default:
247db4728e6SMichael S. Tsirkin         break;
248db4728e6SMichael S. Tsirkin     }
249db4728e6SMichael S. Tsirkin 
250db4728e6SMichael S. Tsirkin     return val;
251db4728e6SMichael S. Tsirkin }
252db4728e6SMichael S. Tsirkin 
253db4728e6SMichael S. Tsirkin static void pci_write(void *opaque, hwaddr addr, uint64_t data,
254db4728e6SMichael S. Tsirkin                       unsigned int size)
255db4728e6SMichael S. Tsirkin {
256db4728e6SMichael S. Tsirkin     AcpiPciHpState *s = opaque;
257db4728e6SMichael S. Tsirkin     switch (addr) {
258db4728e6SMichael S. Tsirkin     case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
259db4728e6SMichael S. Tsirkin         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
260db4728e6SMichael S. Tsirkin             break;
261db4728e6SMichael S. Tsirkin         }
262db4728e6SMichael S. Tsirkin         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
263db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
264db4728e6SMichael S. Tsirkin                       addr, data);
265db4728e6SMichael S. Tsirkin         break;
266db4728e6SMichael S. Tsirkin     case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
267db4728e6SMichael S. Tsirkin         s->hotplug_select = data;
268db4728e6SMichael S. Tsirkin         ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
269db4728e6SMichael S. Tsirkin                       addr, data);
270db4728e6SMichael S. Tsirkin     default:
271db4728e6SMichael S. Tsirkin         break;
272db4728e6SMichael S. Tsirkin     }
273db4728e6SMichael S. Tsirkin }
274db4728e6SMichael S. Tsirkin 
275db4728e6SMichael S. Tsirkin static const MemoryRegionOps acpi_pcihp_io_ops = {
276db4728e6SMichael S. Tsirkin     .read = pci_read,
277db4728e6SMichael S. Tsirkin     .write = pci_write,
278db4728e6SMichael S. Tsirkin     .endianness = DEVICE_LITTLE_ENDIAN,
279db4728e6SMichael S. Tsirkin     .valid = {
280db4728e6SMichael S. Tsirkin         .min_access_size = 4,
281db4728e6SMichael S. Tsirkin         .max_access_size = 4,
282db4728e6SMichael S. Tsirkin     },
283db4728e6SMichael S. Tsirkin };
284db4728e6SMichael S. Tsirkin 
285db4728e6SMichael S. Tsirkin void acpi_pcihp_init(AcpiPciHpState *s, PCIBus *root_bus,
286db4728e6SMichael S. Tsirkin                      MemoryRegion *address_space_io)
287db4728e6SMichael S. Tsirkin {
288db4728e6SMichael S. Tsirkin     s->root= root_bus;
289db4728e6SMichael S. Tsirkin     memory_region_init_io(&s->io, NULL, &acpi_pcihp_io_ops, s,
290db4728e6SMichael S. Tsirkin                           "acpi-pci-hotplug",
291db4728e6SMichael S. Tsirkin                           PCI_HOTPLUG_SIZE);
292db4728e6SMichael S. Tsirkin     memory_region_add_subregion(address_space_io, PCI_HOTPLUG_ADDR, &s->io);
293db4728e6SMichael S. Tsirkin }
294db4728e6SMichael S. Tsirkin 
295db4728e6SMichael S. Tsirkin const VMStateDescription vmstate_acpi_pcihp_pci_status = {
296db4728e6SMichael S. Tsirkin     .name = "acpi_pcihp_pci_status",
297db4728e6SMichael S. Tsirkin     .version_id = 1,
298db4728e6SMichael S. Tsirkin     .minimum_version_id = 1,
299db4728e6SMichael S. Tsirkin     .minimum_version_id_old = 1,
300db4728e6SMichael S. Tsirkin     .fields      = (VMStateField []) {
301db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
302db4728e6SMichael S. Tsirkin         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
303db4728e6SMichael S. Tsirkin         VMSTATE_END_OF_LIST()
304db4728e6SMichael S. Tsirkin     }
305db4728e6SMichael S. Tsirkin };
306