xref: /openbmc/qemu/hw/acpi/pcihp.c (revision 4e116893)
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.1 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 "qemu/osdep.h"
28 #include "hw/acpi/pcihp.h"
29 
30 #include "hw/pci-host/i440fx.h"
31 #include "hw/pci/pci.h"
32 #include "hw/pci/pci_bridge.h"
33 #include "hw/pci/pci_host.h"
34 #include "hw/pci/pcie_port.h"
35 #include "hw/i386/acpi-build.h"
36 #include "hw/acpi/acpi.h"
37 #include "hw/pci/pci_bus.h"
38 #include "migration/vmstate.h"
39 #include "qapi/error.h"
40 #include "qom/qom-qobject.h"
41 #include "trace.h"
42 
43 #define ACPI_PCIHP_SIZE 0x0018
44 #define PCI_UP_BASE 0x0000
45 #define PCI_DOWN_BASE 0x0004
46 #define PCI_EJ_BASE 0x0008
47 #define PCI_RMV_BASE 0x000c
48 #define PCI_SEL_BASE 0x0010
49 #define PCI_AIDX_BASE 0x0014
50 
51 typedef struct AcpiPciHpFind {
52     int bsel;
53     PCIBus *bus;
54 } AcpiPciHpFind;
55 
56 static gint g_cmp_uint32(gconstpointer a, gconstpointer b, gpointer user_data)
57 {
58     return a - b;
59 }
60 
61 static GSequence *pci_acpi_index_list(void)
62 {
63     static GSequence *used_acpi_index_list;
64 
65     if (!used_acpi_index_list) {
66         used_acpi_index_list = g_sequence_new(NULL);
67     }
68     return used_acpi_index_list;
69 }
70 
71 static int acpi_pcihp_get_bsel(PCIBus *bus)
72 {
73     Error *local_err = NULL;
74     uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
75                                              &local_err);
76 
77     if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
78         if (local_err) {
79             error_free(local_err);
80         }
81         return -1;
82     } else {
83         return bsel;
84     }
85 }
86 
87 /* Assign BSEL property to all buses.  In the future, this can be changed
88  * to only assign to buses that support hotplug.
89  */
90 static void *acpi_set_bsel(PCIBus *bus, void *opaque)
91 {
92     unsigned *bsel_alloc = opaque;
93     unsigned *bus_bsel;
94 
95     if (qbus_is_hotpluggable(BUS(bus))) {
96         bus_bsel = g_malloc(sizeof *bus_bsel);
97 
98         *bus_bsel = (*bsel_alloc)++;
99         object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
100                                        bus_bsel, OBJ_PROP_FLAG_READ);
101     }
102 
103     return bsel_alloc;
104 }
105 
106 static void acpi_set_pci_info(void)
107 {
108     static bool bsel_is_set;
109     Object *host = acpi_get_i386_pci_host();
110     PCIBus *bus;
111     unsigned bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT;
112 
113     if (bsel_is_set) {
114         return;
115     }
116     bsel_is_set = true;
117 
118     if (!host) {
119         return;
120     }
121 
122     bus = PCI_HOST_BRIDGE(host)->bus;
123     if (bus) {
124         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
125         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &bsel_alloc);
126     }
127 }
128 
129 static void acpi_pcihp_disable_root_bus(void)
130 {
131     static bool root_hp_disabled;
132     Object *host = acpi_get_i386_pci_host();
133     PCIBus *bus;
134 
135     if (root_hp_disabled) {
136         return;
137     }
138 
139     bus = PCI_HOST_BRIDGE(host)->bus;
140     if (bus) {
141         /* setting the hotplug handler to NULL makes the bus non-hotpluggable */
142         qbus_set_hotplug_handler(BUS(bus), NULL);
143     }
144     root_hp_disabled = true;
145     return;
146 }
147 
148 static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
149 {
150     AcpiPciHpFind *find = opaque;
151     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
152         find->bus = bus;
153     }
154 }
155 
156 static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
157 {
158     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
159 
160     if (bsel < 0) {
161         return NULL;
162     }
163 
164     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
165 
166     /* Make bsel 0 eject root bus if bsel property is not set,
167      * for compatibility with non acpi setups.
168      * TODO: really needed?
169      */
170     if (!bsel && !find.bus) {
171         find.bus = s->root;
172     }
173 
174     /*
175      * Check if find.bus is actually hotpluggable. If bsel is set to
176      * NULL for example on the root bus in order to make it
177      * non-hotpluggable, find.bus will match the root bus when bsel
178      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
179      * bus is not hotpluggable however, we should not select the bus.
180      * Instead, we should set find.bus to NULL in that case. In the check
181      * below, we generalize this case for all buses, not just the root bus.
182      * The callers of this function check for a null return value and
183      * handle them appropriately.
184      */
185     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
186         find.bus = NULL;
187     }
188     return find.bus;
189 }
190 
191 static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
192 {
193     PCIDeviceClass *pc = PCI_DEVICE_GET_CLASS(dev);
194     DeviceClass *dc = DEVICE_GET_CLASS(dev);
195     /*
196      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
197      * hot-unplug of bridge devices unless they were added by hotplug
198      * (and so, not described by acpi).
199      */
200     return (pc->is_bridge && !dev->qdev.hotplugged) || !dc->hotpluggable;
201 }
202 
203 static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
204 {
205     HotplugHandler *hotplug_ctrl;
206     BusChild *kid, *next;
207     int slot = ctz32(slots);
208     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
209 
210     trace_acpi_pci_eject_slot(bsel, slot);
211 
212     if (!bus || slot > 31) {
213         return;
214     }
215 
216     /* Mark request as complete */
217     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
218     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
219 
220     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
221         DeviceState *qdev = kid->child;
222         PCIDevice *dev = PCI_DEVICE(qdev);
223         if (PCI_SLOT(dev->devfn) == slot) {
224             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
225                 hotplug_ctrl = qdev_get_hotplug_handler(qdev);
226                 hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
227                 object_unparent(OBJECT(qdev));
228             }
229         }
230     }
231 }
232 
233 static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
234 {
235     BusChild *kid, *next;
236     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
237 
238     /* Execute any pending removes during reset */
239     while (s->acpi_pcihp_pci_status[bsel].down) {
240         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
241     }
242 
243     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
244 
245     if (!bus) {
246         return;
247     }
248     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
249         DeviceState *qdev = kid->child;
250         PCIDevice *pdev = PCI_DEVICE(qdev);
251         int slot = PCI_SLOT(pdev->devfn);
252 
253         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
254             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
255         }
256     }
257 }
258 
259 static void acpi_pcihp_update(AcpiPciHpState *s)
260 {
261     int i;
262 
263     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
264         acpi_pcihp_update_hotplug_bus(s, i);
265     }
266 }
267 
268 void acpi_pcihp_reset(AcpiPciHpState *s, bool acpihp_root_off)
269 {
270     if (acpihp_root_off) {
271         acpi_pcihp_disable_root_bus();
272     }
273     acpi_set_pci_info();
274     acpi_pcihp_update(s);
275 }
276 
277 #define ONBOARD_INDEX_MAX (16 * 1024 - 1)
278 
279 void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
280                                    DeviceState *dev, Error **errp)
281 {
282     PCIDevice *pdev = PCI_DEVICE(dev);
283 
284     /* Only hotplugged devices need the hotplug capability. */
285     if (dev->hotplugged &&
286         acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
287         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
288                    ACPI_PCIHP_PROP_BSEL "' set");
289         return;
290     }
291 
292     /*
293      * capped by systemd (see: udev-builtin-net_id.c)
294      * as it's the only known user honor it to avoid users
295      * misconfigure QEMU and then wonder why acpi-index doesn't work
296      */
297     if (pdev->acpi_index > ONBOARD_INDEX_MAX) {
298         error_setg(errp, "acpi-index should be less or equal to %u",
299                    ONBOARD_INDEX_MAX);
300         return;
301     }
302 
303     /*
304      * make sure that acpi-index is unique across all present PCI devices
305      */
306     if (pdev->acpi_index) {
307         GSequence *used_indexes = pci_acpi_index_list();
308 
309         if (g_sequence_lookup(used_indexes, GINT_TO_POINTER(pdev->acpi_index),
310                               g_cmp_uint32, NULL)) {
311             error_setg(errp, "a PCI device with acpi-index = %" PRIu32
312                        " already exist", pdev->acpi_index);
313             return;
314         }
315         g_sequence_insert_sorted(used_indexes,
316                                  GINT_TO_POINTER(pdev->acpi_index),
317                                  g_cmp_uint32, NULL);
318     }
319 }
320 
321 void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
322                                DeviceState *dev, Error **errp)
323 {
324     PCIDevice *pdev = PCI_DEVICE(dev);
325     int slot = PCI_SLOT(pdev->devfn);
326     int bsel;
327 
328     /* Don't send event when device is enabled during qemu machine creation:
329      * it is present on boot, no hotplug event is necessary. We do send an
330      * event when the device is disabled later. */
331     if (!dev->hotplugged) {
332         /*
333          * Overwrite the default hotplug handler with the ACPI PCI one
334          * for cold plugged bridges only.
335          */
336         if (!s->legacy_piix &&
337             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
338             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
339 
340             /* Remove all hot-plug handlers if hot-plug is disabled on slot */
341             if (object_dynamic_cast(OBJECT(dev), TYPE_PCIE_SLOT) &&
342                 !PCIE_SLOT(pdev)->hotplug) {
343                 qbus_set_hotplug_handler(BUS(sec), NULL);
344                 return;
345             }
346 
347             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
348             /* We don't have to overwrite any other hotplug handler yet */
349             assert(QLIST_EMPTY(&sec->child));
350         }
351 
352         return;
353     }
354 
355     bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
356     g_assert(bsel >= 0);
357     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
358     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
359 }
360 
361 void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
362                                  DeviceState *dev, Error **errp)
363 {
364     PCIDevice *pdev = PCI_DEVICE(dev);
365 
366     trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
367                           acpi_pcihp_get_bsel(pci_get_bus(pdev)));
368 
369     /*
370      * clean up acpi-index so it could reused by another device
371      */
372     if (pdev->acpi_index) {
373         GSequence *used_indexes = pci_acpi_index_list();
374 
375         g_sequence_remove(g_sequence_lookup(used_indexes,
376                           GINT_TO_POINTER(pdev->acpi_index),
377                           g_cmp_uint32, NULL));
378     }
379 
380     qdev_unrealize(dev);
381 }
382 
383 void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
384                                          AcpiPciHpState *s, DeviceState *dev,
385                                          Error **errp)
386 {
387     PCIDevice *pdev = PCI_DEVICE(dev);
388     int slot = PCI_SLOT(pdev->devfn);
389     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
390 
391     trace_acpi_pci_unplug_request(bsel, slot);
392 
393     if (bsel < 0) {
394         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
395                    ACPI_PCIHP_PROP_BSEL "' set");
396         return;
397     }
398 
399     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
400     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
401 }
402 
403 static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
404 {
405     AcpiPciHpState *s = opaque;
406     uint32_t val = 0;
407     int bsel = s->hotplug_select;
408 
409     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
410         return 0;
411     }
412 
413     switch (addr) {
414     case PCI_UP_BASE:
415         val = s->acpi_pcihp_pci_status[bsel].up;
416         if (!s->legacy_piix) {
417             s->acpi_pcihp_pci_status[bsel].up = 0;
418         }
419         trace_acpi_pci_up_read(val);
420         break;
421     case PCI_DOWN_BASE:
422         val = s->acpi_pcihp_pci_status[bsel].down;
423         trace_acpi_pci_down_read(val);
424         break;
425     case PCI_EJ_BASE:
426         trace_acpi_pci_features_read(val);
427         break;
428     case PCI_RMV_BASE:
429         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
430         trace_acpi_pci_rmv_read(val);
431         break;
432     case PCI_SEL_BASE:
433         val = s->hotplug_select;
434         trace_acpi_pci_sel_read(val);
435         break;
436     case PCI_AIDX_BASE:
437         val = s->acpi_index;
438         s->acpi_index = 0;
439         trace_acpi_pci_acpi_index_read(val);
440         break;
441     default:
442         break;
443     }
444 
445     return val;
446 }
447 
448 static void pci_write(void *opaque, hwaddr addr, uint64_t data,
449                       unsigned int size)
450 {
451     int slot;
452     PCIBus *bus;
453     BusChild *kid, *next;
454     AcpiPciHpState *s = opaque;
455 
456     s->acpi_index = 0;
457     switch (addr) {
458     case PCI_AIDX_BASE:
459         /*
460          * fetch acpi-index for specified slot so that follow up read from
461          * PCI_AIDX_BASE can return it to guest
462          */
463         slot = ctz32(data);
464 
465         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
466             break;
467         }
468 
469         bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
470         QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
471             Object *o = OBJECT(kid->child);
472             PCIDevice *dev = PCI_DEVICE(o);
473             if (PCI_SLOT(dev->devfn) == slot) {
474                 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
475                 break;
476             }
477         }
478         trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
479         break;
480     case PCI_EJ_BASE:
481         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
482             break;
483         }
484         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
485         trace_acpi_pci_ej_write(addr, data);
486         break;
487     case PCI_SEL_BASE:
488         s->hotplug_select = s->legacy_piix ? ACPI_PCIHP_BSEL_DEFAULT : data;
489         trace_acpi_pci_sel_write(addr, data);
490     default:
491         break;
492     }
493 }
494 
495 static const MemoryRegionOps acpi_pcihp_io_ops = {
496     .read = pci_read,
497     .write = pci_write,
498     .endianness = DEVICE_LITTLE_ENDIAN,
499     .valid = {
500         .min_access_size = 4,
501         .max_access_size = 4,
502     },
503 };
504 
505 void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
506                      MemoryRegion *address_space_io, bool bridges_enabled,
507                      uint16_t io_base)
508 {
509     s->io_len = ACPI_PCIHP_SIZE;
510     s->io_base = io_base;
511 
512     s->root = root_bus;
513     s->legacy_piix = !bridges_enabled;
514 
515     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
516                           "acpi-pci-hotplug", s->io_len);
517     memory_region_add_subregion(address_space_io, s->io_base, &s->io);
518 
519     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
520                                    OBJ_PROP_FLAG_READ);
521     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
522                                    OBJ_PROP_FLAG_READ);
523 }
524 
525 bool vmstate_acpi_pcihp_use_acpi_index(void *opaque, int version_id)
526 {
527      AcpiPciHpState *s = opaque;
528      return s->acpi_index;
529 }
530 
531 const VMStateDescription vmstate_acpi_pcihp_pci_status = {
532     .name = "acpi_pcihp_pci_status",
533     .version_id = 1,
534     .minimum_version_id = 1,
535     .fields = (VMStateField[]) {
536         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
537         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
538         VMSTATE_END_OF_LIST()
539     }
540 };
541