xref: /openbmc/qemu/hw/acpi/pcihp.c (revision b8bcf811)
1 /*
2  * QEMU<->ACPI BIOS PCI hotplug interface
3  *
4  * QEMU supports PCI hotplug via ACPI. This module
5  * implements the interface between QEMU and the ACPI BIOS.
6  * Interface specification - see docs/specs/acpi_pci_hotplug.txt
7  *
8  * Copyright (c) 2013, Red Hat Inc, Michael S. Tsirkin (mst@redhat.com)
9  * Copyright (c) 2006 Fabrice Bellard
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License version 2 as published by the Free Software Foundation.
14  *
15  * This library is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18  * Lesser General Public License for more details.
19  *
20  * You should have received a copy of the GNU Lesser General Public
21  * License along with this library; if not, see <http://www.gnu.org/licenses/>
22  *
23  * Contributions after 2012-01-13 are licensed under the terms of the
24  * GNU GPL, version 2 or (at your option) any later version.
25  */
26 
27 #include "hw/acpi/pcihp.h"
28 
29 #include "hw/hw.h"
30 #include "hw/i386/pc.h"
31 #include "hw/pci/pci.h"
32 #include "hw/acpi/acpi.h"
33 #include "sysemu/sysemu.h"
34 #include "qemu/range.h"
35 #include "exec/ioport.h"
36 #include "exec/address-spaces.h"
37 #include "hw/pci/pci_bus.h"
38 #include "qom/qom-qobject.h"
39 #include "qapi/qmp/qint.h"
40 
41 //#define DEBUG
42 
43 #ifdef DEBUG
44 # define ACPI_PCIHP_DPRINTF(format, ...)     printf(format, ## __VA_ARGS__)
45 #else
46 # define ACPI_PCIHP_DPRINTF(format, ...)     do { } while (0)
47 #endif
48 
49 #define PCI_HOTPLUG_ADDR 0xae00
50 #define PCI_HOTPLUG_SIZE 0x0014
51 #define PCI_UP_BASE 0xae00
52 #define PCI_DOWN_BASE 0xae04
53 #define PCI_EJ_BASE 0xae08
54 #define PCI_RMV_BASE 0xae0c
55 #define PCI_SEL_BASE 0xae10
56 
57 typedef struct AcpiPciHpFind {
58     int bsel;
59     PCIBus *bus;
60 } AcpiPciHpFind;
61 
62 static int acpi_pcihp_get_bsel(PCIBus *bus)
63 {
64     QObject *o = object_property_get_qobject(OBJECT(bus),
65                                              ACPI_PCIHP_PROP_BSEL, NULL);
66     int64_t bsel = -1;
67     if (o) {
68         bsel = qint_get_int(qobject_to_qint(o));
69     }
70     if (bsel < 0) {
71         return -1;
72     }
73     return bsel;
74 }
75 
76 static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
77 {
78     AcpiPciHpFind *find = opaque;
79     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
80         find->bus = bus;
81     }
82 }
83 
84 static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
85 {
86     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
87 
88     if (bsel < 0) {
89         return NULL;
90     }
91 
92     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
93 
94     /* Make bsel 0 eject root bus if bsel property is not set,
95      * for compatibility with non acpi setups.
96      * TODO: really needed?
97      */
98     if (!bsel && !find.bus) {
99         find.bus = s->root;
100     }
101     return find.bus;
102 }
103 
104 static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
105 {
106     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
107     /*
108      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
109      * hot-unplug of bridge devices unless they were added by hotplug
110      * (and so, not described by acpi).
111      */
112     return (pc->is_bridge && !dev->qdev.hotplugged) || pc->no_hotplug;
113 }
114 
115 static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
116 {
117     BusChild *kid, *next;
118     int slot = ffs(slots) - 1;
119     bool slot_free = true;
120     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
121 
122     if (!bus) {
123         return;
124     }
125 
126     /* Mark request as complete */
127     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
128 
129     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
130         DeviceState *qdev = kid->child;
131         PCIDevice *dev = PCI_DEVICE(qdev);
132         if (PCI_SLOT(dev->devfn) == slot) {
133             if (acpi_pcihp_pc_no_hotplug(s, dev)) {
134                 slot_free = false;
135             } else {
136                 object_unparent(OBJECT(qdev));
137             }
138         }
139     }
140     if (slot_free) {
141         s->acpi_pcihp_pci_status[bsel].device_present &= ~(1U << slot);
142     }
143 }
144 
145 static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
146 {
147     BusChild *kid, *next;
148     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
149 
150     /* Execute any pending removes during reset */
151     while (s->acpi_pcihp_pci_status[bsel].down) {
152         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
153     }
154 
155     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
156     s->acpi_pcihp_pci_status[bsel].device_present = 0;
157 
158     if (!bus) {
159         return;
160     }
161     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
162         DeviceState *qdev = kid->child;
163         PCIDevice *pdev = PCI_DEVICE(qdev);
164         int slot = PCI_SLOT(pdev->devfn);
165 
166         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
167             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
168         }
169 
170         s->acpi_pcihp_pci_status[bsel].device_present |= (1U << slot);
171     }
172 }
173 
174 static void acpi_pcihp_update(AcpiPciHpState *s)
175 {
176     int i;
177 
178     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
179         acpi_pcihp_update_hotplug_bus(s, i);
180     }
181 }
182 
183 void acpi_pcihp_reset(AcpiPciHpState *s)
184 {
185     acpi_pcihp_update(s);
186 }
187 
188 static void enable_device(AcpiPciHpState *s, unsigned bsel, int slot)
189 {
190     s->acpi_pcihp_pci_status[bsel].device_present |= (1U << slot);
191 }
192 
193 static void disable_device(AcpiPciHpState *s, unsigned bsel, int slot)
194 {
195     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
196 }
197 
198 int acpi_pcihp_device_hotplug(AcpiPciHpState *s, PCIDevice *dev,
199                               PCIHotplugState state)
200 {
201     int slot = PCI_SLOT(dev->devfn);
202     int bsel = acpi_pcihp_get_bsel(dev->bus);
203     if (bsel < 0) {
204         return -1;
205     }
206 
207     /* Don't send event when device is enabled during qemu machine creation:
208      * it is present on boot, no hotplug event is necessary. We do send an
209      * event when the device is disabled later. */
210     if (state == PCI_COLDPLUG_ENABLED) {
211         s->acpi_pcihp_pci_status[bsel].device_present |= (1U << slot);
212         return 0;
213     }
214 
215     if (state == PCI_HOTPLUG_ENABLED) {
216         enable_device(s, bsel, slot);
217     } else {
218         disable_device(s, bsel, slot);
219     }
220 
221     return 0;
222 }
223 
224 static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
225 {
226     AcpiPciHpState *s = opaque;
227     uint32_t val = 0;
228     int bsel = s->hotplug_select;
229 
230     if (bsel < 0 || bsel > ACPI_PCIHP_MAX_HOTPLUG_BUS) {
231         return 0;
232     }
233 
234     switch (addr) {
235     case PCI_UP_BASE - PCI_HOTPLUG_ADDR:
236         /* Manufacture an "up" value to cause a device check on any hotplug
237          * slot with a device.  Extra device checks are harmless. */
238         val = s->acpi_pcihp_pci_status[bsel].device_present &
239             s->acpi_pcihp_pci_status[bsel].hotplug_enable;
240         ACPI_PCIHP_DPRINTF("pci_up_read %" PRIu32 "\n", val);
241         break;
242     case PCI_DOWN_BASE - PCI_HOTPLUG_ADDR:
243         val = s->acpi_pcihp_pci_status[bsel].down;
244         ACPI_PCIHP_DPRINTF("pci_down_read %" PRIu32 "\n", val);
245         break;
246     case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
247         /* No feature defined yet */
248         ACPI_PCIHP_DPRINTF("pci_features_read %" PRIu32 "\n", val);
249         break;
250     case PCI_RMV_BASE - PCI_HOTPLUG_ADDR:
251         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
252         ACPI_PCIHP_DPRINTF("pci_rmv_read %" PRIu32 "\n", val);
253         break;
254     case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
255         val = s->hotplug_select;
256         ACPI_PCIHP_DPRINTF("pci_sel_read %" PRIu32 "\n", val);
257     default:
258         break;
259     }
260 
261     return val;
262 }
263 
264 static void pci_write(void *opaque, hwaddr addr, uint64_t data,
265                       unsigned int size)
266 {
267     AcpiPciHpState *s = opaque;
268     switch (addr) {
269     case PCI_EJ_BASE - PCI_HOTPLUG_ADDR:
270         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
271             break;
272         }
273         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
274         ACPI_PCIHP_DPRINTF("pciej write %" HWADDR_PRIx " <== %" PRIu64 "\n",
275                       addr, data);
276         break;
277     case PCI_SEL_BASE - PCI_HOTPLUG_ADDR:
278         s->hotplug_select = data;
279         ACPI_PCIHP_DPRINTF("pcisel write %" HWADDR_PRIx " <== %" PRIu64 "\n",
280                       addr, data);
281     default:
282         break;
283     }
284 }
285 
286 static const MemoryRegionOps acpi_pcihp_io_ops = {
287     .read = pci_read,
288     .write = pci_write,
289     .endianness = DEVICE_LITTLE_ENDIAN,
290     .valid = {
291         .min_access_size = 4,
292         .max_access_size = 4,
293     },
294 };
295 
296 void acpi_pcihp_init(AcpiPciHpState *s, PCIBus *root_bus,
297                      MemoryRegion *address_space_io)
298 {
299     s->root= root_bus;
300     memory_region_init_io(&s->io, NULL, &acpi_pcihp_io_ops, s,
301                           "acpi-pci-hotplug",
302                           PCI_HOTPLUG_SIZE);
303     memory_region_add_subregion(address_space_io, PCI_HOTPLUG_ADDR, &s->io);
304 }
305 
306 const VMStateDescription vmstate_acpi_pcihp_pci_status = {
307     .name = "acpi_pcihp_pci_status",
308     .version_id = 1,
309     .minimum_version_id = 1,
310     .minimum_version_id_old = 1,
311     .fields      = (VMStateField []) {
312         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
313         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
314         VMSTATE_END_OF_LIST()
315     }
316 };
317