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