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