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