xref: /openbmc/qemu/hw/acpi/pcihp.c (revision e87ab64e8f614ae8702209dd801ac08ca5897ff1)
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.rst
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 #include "hw/acpi/aml-build.h"
30 #include "hw/acpi/acpi_aml_interface.h"
31 #include "hw/pci-host/i440fx.h"
32 #include "hw/pci/pci.h"
33 #include "hw/pci/pci_bridge.h"
34 #include "hw/pci/pci_host.h"
35 #include "hw/pci/pcie_port.h"
36 #include "hw/pci-bridge/xio3130_downstream.h"
37 #include "hw/i386/acpi-build.h"
38 #include "hw/acpi/acpi.h"
39 #include "hw/pci/pci_bus.h"
40 #include "migration/vmstate.h"
41 #include "qapi/error.h"
42 #include "qom/qom-qobject.h"
43 #include "qobject/qnum.h"
44 #include "trace.h"
45 
46 #define ACPI_PCIHP_SIZE 0x0018
47 #define PCI_UP_BASE 0x0000
48 #define PCI_DOWN_BASE 0x0004
49 #define PCI_EJ_BASE 0x0008
50 #define PCI_RMV_BASE 0x000c
51 #define PCI_SEL_BASE 0x0010
52 #define PCI_AIDX_BASE 0x0014
53 
54 typedef struct AcpiPciHpFind {
55     int bsel;
56     PCIBus *bus;
57 } AcpiPciHpFind;
58 
59 static int acpi_pcihp_get_bsel(PCIBus *bus)
60 {
61     Error *local_err = NULL;
62     uint64_t bsel = object_property_get_uint(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
63                                              &local_err);
64 
65     if (local_err || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
66         if (local_err) {
67             error_free(local_err);
68         }
69         return -1;
70     } else {
71         return bsel;
72     }
73 }
74 
75 typedef struct {
76     unsigned bsel_alloc;
77     bool has_bridge_hotplug;
78 } BSELInfo;
79 
80 /* Assign BSEL property only to buses that support hotplug. */
81 static void *acpi_set_bsel(PCIBus *bus, void *opaque)
82 {
83     BSELInfo *info = opaque;
84     unsigned *bus_bsel;
85     DeviceState *br = bus->qbus.parent;
86     bool is_bridge = IS_PCI_BRIDGE(br);
87 
88     /* hotplugged bridges can't be described in ACPI ignore them */
89     if (qbus_is_hotpluggable(BUS(bus))) {
90         if (!is_bridge || (!br->hotplugged && info->has_bridge_hotplug)) {
91             bus_bsel = g_malloc(sizeof *bus_bsel);
92 
93             *bus_bsel = info->bsel_alloc++;
94             object_property_add_uint32_ptr(OBJECT(bus), ACPI_PCIHP_PROP_BSEL,
95                                            bus_bsel, OBJ_PROP_FLAG_READ);
96         }
97     }
98 
99     return info;
100 }
101 
102 static void acpi_set_pci_info(bool has_bridge_hotplug)
103 {
104     static bool bsel_is_set;
105     Object *host = acpi_get_i386_pci_host();
106     PCIBus *bus;
107     BSELInfo info = { .bsel_alloc = ACPI_PCIHP_BSEL_DEFAULT,
108                       .has_bridge_hotplug = has_bridge_hotplug };
109 
110     if (bsel_is_set) {
111         return;
112     }
113     bsel_is_set = true;
114 
115     if (!host) {
116         return;
117     }
118 
119     bus = PCI_HOST_BRIDGE(host)->bus;
120     if (bus) {
121         /* Scan all PCI buses. Set property to enable acpi based hotplug. */
122         pci_for_each_bus_depth_first(bus, acpi_set_bsel, NULL, &info);
123     }
124 }
125 
126 static void acpi_pcihp_test_hotplug_bus(PCIBus *bus, void *opaque)
127 {
128     AcpiPciHpFind *find = opaque;
129     if (find->bsel == acpi_pcihp_get_bsel(bus)) {
130         find->bus = bus;
131     }
132 }
133 
134 static PCIBus *acpi_pcihp_find_hotplug_bus(AcpiPciHpState *s, int bsel)
135 {
136     AcpiPciHpFind find = { .bsel = bsel, .bus = NULL };
137 
138     if (bsel < 0) {
139         return NULL;
140     }
141 
142     pci_for_each_bus(s->root, acpi_pcihp_test_hotplug_bus, &find);
143 
144     /* Make bsel 0 eject root bus if bsel property is not set,
145      * for compatibility with non acpi setups.
146      * TODO: really needed?
147      */
148     if (!bsel && !find.bus) {
149         find.bus = s->root;
150     }
151 
152     /*
153      * Check if find.bus is actually hotpluggable. If bsel is set to
154      * NULL for example on the root bus in order to make it
155      * non-hotpluggable, find.bus will match the root bus when bsel
156      * is 0. See acpi_pcihp_test_hotplug_bus() above. Since the
157      * bus is not hotpluggable however, we should not select the bus.
158      * Instead, we should set find.bus to NULL in that case. In the check
159      * below, we generalize this case for all buses, not just the root bus.
160      * The callers of this function check for a null return value and
161      * handle them appropriately.
162      */
163     if (find.bus && !qbus_is_hotpluggable(BUS(find.bus))) {
164         find.bus = NULL;
165     }
166     return find.bus;
167 }
168 
169 static bool acpi_pcihp_pc_no_hotplug(AcpiPciHpState *s, PCIDevice *dev)
170 {
171     DeviceClass *dc = DEVICE_GET_CLASS(dev);
172     /*
173      * ACPI doesn't allow hotplug of bridge devices.  Don't allow
174      * hot-unplug of bridge devices unless they were added by hotplug
175      * (and so, not described by acpi).
176      *
177      * Don't allow hot-unplug of SR-IOV Virtual Functions, as they
178      * will be removed implicitly, when Physical Function is unplugged.
179      */
180     return (IS_PCI_BRIDGE(dev) && !dev->qdev.hotplugged) || !dc->hotpluggable ||
181            pci_is_vf(dev);
182 }
183 
184 static void acpi_pcihp_eject_slot(AcpiPciHpState *s, unsigned bsel, unsigned slots)
185 {
186     HotplugHandler *hotplug_ctrl;
187     BusChild *kid, *next;
188     int slot = ctz32(slots);
189     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
190 
191     trace_acpi_pci_eject_slot(bsel, slot);
192 
193     if (!bus || slot > 31) {
194         return;
195     }
196 
197     /* Mark request as complete */
198     s->acpi_pcihp_pci_status[bsel].down &= ~(1U << slot);
199     s->acpi_pcihp_pci_status[bsel].up &= ~(1U << slot);
200 
201     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
202         DeviceState *qdev = kid->child;
203         PCIDevice *dev = PCI_DEVICE(qdev);
204         if (PCI_SLOT(dev->devfn) == slot) {
205             if (!acpi_pcihp_pc_no_hotplug(s, dev)) {
206                 /*
207                  * partially_hotplugged is used by virtio-net failover:
208                  * failover has asked the guest OS to unplug the device
209                  * but we need to keep some references to the device
210                  * to be able to plug it back in case of failure so
211                  * we don't execute hotplug_handler_unplug().
212                  */
213                 if (dev->partially_hotplugged) {
214                     /*
215                      * pending_deleted_event is set to true when
216                      * virtio-net failover asks to unplug the device,
217                      * and set to false here when the operation is done
218                      * This is used by the migration loop to detect the
219                      * end of the operation and really start the migration.
220                      */
221                     qdev->pending_deleted_event = false;
222                 } else {
223                     hotplug_ctrl = qdev_get_hotplug_handler(qdev);
224                     hotplug_handler_unplug(hotplug_ctrl, qdev, &error_abort);
225                     object_unparent(OBJECT(qdev));
226                 }
227             }
228         }
229     }
230 }
231 
232 static void acpi_pcihp_update_hotplug_bus(AcpiPciHpState *s, int bsel)
233 {
234     BusChild *kid, *next;
235     PCIBus *bus = acpi_pcihp_find_hotplug_bus(s, bsel);
236 
237     /* Execute any pending removes during reset */
238     while (s->acpi_pcihp_pci_status[bsel].down) {
239         acpi_pcihp_eject_slot(s, bsel, s->acpi_pcihp_pci_status[bsel].down);
240     }
241 
242     s->acpi_pcihp_pci_status[bsel].hotplug_enable = ~0;
243 
244     if (!bus) {
245         return;
246     }
247     QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
248         DeviceState *qdev = kid->child;
249         PCIDevice *pdev = PCI_DEVICE(qdev);
250         int slot = PCI_SLOT(pdev->devfn);
251 
252         if (acpi_pcihp_pc_no_hotplug(s, pdev)) {
253             s->acpi_pcihp_pci_status[bsel].hotplug_enable &= ~(1U << slot);
254         }
255     }
256 }
257 
258 static void acpi_pcihp_update(AcpiPciHpState *s)
259 {
260     int i;
261 
262     for (i = 0; i < ACPI_PCIHP_MAX_HOTPLUG_BUS; ++i) {
263         acpi_pcihp_update_hotplug_bus(s, i);
264     }
265 }
266 
267 void acpi_pcihp_reset(AcpiPciHpState *s)
268 {
269     acpi_set_pci_info(s->use_acpi_hotplug_bridge);
270     acpi_pcihp_update(s);
271 }
272 
273 void acpi_pcihp_device_pre_plug_cb(HotplugHandler *hotplug_dev,
274                                    DeviceState *dev, Error **errp)
275 {
276     PCIDevice *pdev = PCI_DEVICE(dev);
277 
278     /* Only hotplugged devices need the hotplug capability. */
279     if (dev->hotplugged &&
280         acpi_pcihp_get_bsel(pci_get_bus(pdev)) < 0) {
281         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
282                    ACPI_PCIHP_PROP_BSEL "' set");
283         return;
284     }
285 }
286 
287 void acpi_pcihp_device_plug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
288                                DeviceState *dev, Error **errp)
289 {
290     PCIDevice *pdev = PCI_DEVICE(dev);
291     int slot = PCI_SLOT(pdev->devfn);
292     PCIDevice *bridge;
293     PCIBus *bus;
294     int bsel;
295 
296     /* Don't send event when device is enabled during qemu machine creation:
297      * it is present on boot, no hotplug event is necessary. We do send an
298      * event when the device is disabled later. */
299     if (!dev->hotplugged) {
300         /*
301          * Overwrite the default hotplug handler with the ACPI PCI one
302          * for cold plugged bridges only.
303          */
304         if (s->use_acpi_hotplug_bridge &&
305             object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
306             PCIBus *sec = pci_bridge_get_sec_bus(PCI_BRIDGE(pdev));
307 
308             qbus_set_hotplug_handler(BUS(sec), OBJECT(hotplug_dev));
309             /* We don't have to overwrite any other hotplug handler yet */
310             assert(QLIST_EMPTY(&sec->child));
311         }
312 
313         return;
314     }
315 
316     bus = pci_get_bus(pdev);
317     bridge = pci_bridge_get_device(bus);
318     if (object_dynamic_cast(OBJECT(bridge), TYPE_PCIE_ROOT_PORT) ||
319         object_dynamic_cast(OBJECT(bridge), TYPE_XIO3130_DOWNSTREAM)) {
320         pcie_cap_slot_enable_power(bridge);
321     }
322 
323     bsel = acpi_pcihp_get_bsel(bus);
324     g_assert(bsel >= 0);
325     s->acpi_pcihp_pci_status[bsel].up |= (1U << slot);
326     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
327 }
328 
329 void acpi_pcihp_device_unplug_cb(HotplugHandler *hotplug_dev, AcpiPciHpState *s,
330                                  DeviceState *dev, Error **errp)
331 {
332     PCIDevice *pdev = PCI_DEVICE(dev);
333 
334     trace_acpi_pci_unplug(PCI_SLOT(pdev->devfn),
335                           acpi_pcihp_get_bsel(pci_get_bus(pdev)));
336 
337     qdev_unrealize(dev);
338 }
339 
340 void acpi_pcihp_device_unplug_request_cb(HotplugHandler *hotplug_dev,
341                                          AcpiPciHpState *s, DeviceState *dev,
342                                          Error **errp)
343 {
344     PCIDevice *pdev = PCI_DEVICE(dev);
345     int slot = PCI_SLOT(pdev->devfn);
346     int bsel = acpi_pcihp_get_bsel(pci_get_bus(pdev));
347 
348     trace_acpi_pci_unplug_request(bsel, slot);
349 
350     if (bsel < 0) {
351         error_setg(errp, "Unsupported bus. Bus doesn't have property '"
352                    ACPI_PCIHP_PROP_BSEL "' set");
353         return;
354     }
355 
356     /*
357      * pending_deleted_event is used by virtio-net failover to detect the
358      * end of the unplug operation, the flag is set to false in
359      * acpi_pcihp_eject_slot() when the operation is completed.
360      */
361     pdev->qdev.pending_deleted_event = true;
362     /* if unplug was requested before OSPM is initialized,
363      * linux kernel will clear GPE0.sts[] bits during boot, which effectively
364      * hides unplug event. And than followup qmp_device_del() calls remain
365      * blocked by above flag permanently.
366      * Unblock qmp_device_del() by setting expire limit, so user can
367      * repeat unplug request later when OSPM has been booted.
368      */
369     pdev->qdev.pending_deleted_expires_ms =
370         qemu_clock_get_ms(QEMU_CLOCK_VIRTUAL); /* 1 msec */
371 
372     s->acpi_pcihp_pci_status[bsel].down |= (1U << slot);
373     acpi_send_event(DEVICE(hotplug_dev), ACPI_PCI_HOTPLUG_STATUS);
374 }
375 
376 bool acpi_pcihp_is_hotpluggable_bus(AcpiPciHpState *s, BusState *bus)
377 {
378     Object *o = OBJECT(bus->parent);
379 
380     if (s->use_acpi_hotplug_bridge &&
381         object_dynamic_cast(o, TYPE_PCI_BRIDGE)) {
382         if (object_dynamic_cast(o, TYPE_PCIE_SLOT) && !PCIE_SLOT(o)->hotplug) {
383             return false;
384         }
385         return true;
386     }
387 
388     if (s->use_acpi_root_pci_hotplug) {
389         return true;
390     }
391     return false;
392 }
393 
394 static uint64_t pci_read(void *opaque, hwaddr addr, unsigned int size)
395 {
396     AcpiPciHpState *s = opaque;
397     uint32_t val = 0;
398     int bsel = s->hotplug_select;
399 
400     if (bsel < 0 || bsel >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
401         return 0;
402     }
403 
404     switch (addr) {
405     case PCI_UP_BASE:
406         val = s->acpi_pcihp_pci_status[bsel].up;
407         if (s->use_acpi_hotplug_bridge) {
408             s->acpi_pcihp_pci_status[bsel].up = 0;
409         }
410         trace_acpi_pci_up_read(val);
411         break;
412     case PCI_DOWN_BASE:
413         val = s->acpi_pcihp_pci_status[bsel].down;
414         trace_acpi_pci_down_read(val);
415         break;
416     case PCI_EJ_BASE:
417         trace_acpi_pci_features_read(val);
418         break;
419     case PCI_RMV_BASE:
420         val = s->acpi_pcihp_pci_status[bsel].hotplug_enable;
421         trace_acpi_pci_rmv_read(val);
422         break;
423     case PCI_SEL_BASE:
424         val = s->hotplug_select;
425         trace_acpi_pci_sel_read(val);
426         break;
427     case PCI_AIDX_BASE:
428         val = s->acpi_index;
429         s->acpi_index = 0;
430         trace_acpi_pci_acpi_index_read(val);
431         break;
432     default:
433         break;
434     }
435 
436     return val;
437 }
438 
439 static void pci_write(void *opaque, hwaddr addr, uint64_t data,
440                       unsigned int size)
441 {
442     int slot;
443     PCIBus *bus;
444     BusChild *kid, *next;
445     AcpiPciHpState *s = opaque;
446 
447     s->acpi_index = 0;
448     switch (addr) {
449     case PCI_AIDX_BASE:
450         /*
451          * fetch acpi-index for specified slot so that follow up read from
452          * PCI_AIDX_BASE can return it to guest
453          */
454         slot = ctz32(data);
455 
456         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
457             break;
458         }
459 
460         bus = acpi_pcihp_find_hotplug_bus(s, s->hotplug_select);
461         if (!bus) {
462             break;
463         }
464         QTAILQ_FOREACH_SAFE(kid, &bus->qbus.children, sibling, next) {
465             Object *o = OBJECT(kid->child);
466             PCIDevice *dev = PCI_DEVICE(o);
467             if (PCI_SLOT(dev->devfn) == slot) {
468                 s->acpi_index = object_property_get_uint(o, "acpi-index", NULL);
469                 break;
470             }
471         }
472         trace_acpi_pci_acpi_index_write(s->hotplug_select, slot, s->acpi_index);
473         break;
474     case PCI_EJ_BASE:
475         if (s->hotplug_select >= ACPI_PCIHP_MAX_HOTPLUG_BUS) {
476             break;
477         }
478         acpi_pcihp_eject_slot(s, s->hotplug_select, data);
479         trace_acpi_pci_ej_write(addr, data);
480         break;
481     case PCI_SEL_BASE:
482         s->hotplug_select = s->use_acpi_hotplug_bridge ? data :
483             ACPI_PCIHP_BSEL_DEFAULT;
484         trace_acpi_pci_sel_write(addr, data);
485     default:
486         break;
487     }
488 }
489 
490 static const MemoryRegionOps acpi_pcihp_io_ops = {
491     .read = pci_read,
492     .write = pci_write,
493     .endianness = DEVICE_LITTLE_ENDIAN,
494     .valid = {
495         .min_access_size = 4,
496         .max_access_size = 4,
497     },
498 };
499 
500 void acpi_pcihp_init(Object *owner, AcpiPciHpState *s, PCIBus *root_bus,
501                      MemoryRegion *io, uint16_t io_base)
502 {
503     s->io_len = ACPI_PCIHP_SIZE;
504     s->io_base = io_base;
505 
506     s->root = root_bus;
507 
508     memory_region_init_io(&s->io, owner, &acpi_pcihp_io_ops, s,
509                           "acpi-pci-hotplug", s->io_len);
510     memory_region_add_subregion(io, s->io_base, &s->io);
511 
512     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_BASE_PROP, &s->io_base,
513                                    OBJ_PROP_FLAG_READ);
514     object_property_add_uint16_ptr(owner, ACPI_PCIHP_IO_LEN_PROP, &s->io_len,
515                                    OBJ_PROP_FLAG_READ);
516 }
517 
518 void build_append_pci_dsm_func0_common(Aml *ctx, Aml *retvar)
519 {
520     Aml *UUID, *ifctx1;
521     uint8_t byte_list[1] = { 0 }; /* nothing supported yet */
522 
523     aml_append(ctx, aml_store(aml_buffer(1, byte_list), retvar));
524     /*
525      * PCI Firmware Specification 3.1
526      * 4.6.  _DSM Definitions for PCI
527      */
528     UUID = aml_touuid("E5C937D0-3553-4D7A-9117-EA4D19C3434D");
529     ifctx1 = aml_if(aml_lnot(aml_equal(aml_arg(0), UUID)));
530     {
531         /* call is for unsupported UUID, bail out */
532         aml_append(ifctx1, aml_return(retvar));
533     }
534     aml_append(ctx, ifctx1);
535 
536     ifctx1 = aml_if(aml_lless(aml_arg(1), aml_int(2)));
537     {
538         /* call is for unsupported REV, bail out */
539         aml_append(ifctx1, aml_return(retvar));
540     }
541     aml_append(ctx, ifctx1);
542 }
543 
544 static Aml *aml_pci_pdsm(void)
545 {
546     Aml *method, *ifctx, *ifctx1;
547     Aml *ret = aml_local(0);
548     Aml *caps = aml_local(1);
549     Aml *acpi_index = aml_local(2);
550     Aml *zero = aml_int(0);
551     Aml *one = aml_int(1);
552     Aml *not_supp = aml_int(0xFFFFFFFF);
553     Aml *func = aml_arg(2);
554     Aml *params = aml_arg(4);
555     Aml *bnum = aml_derefof(aml_index(params, aml_int(0)));
556     Aml *sunum = aml_derefof(aml_index(params, aml_int(1)));
557 
558     method = aml_method("PDSM", 5, AML_SERIALIZED);
559 
560     /* get supported functions */
561     ifctx = aml_if(aml_equal(func, zero));
562     {
563         build_append_pci_dsm_func0_common(ifctx, ret);
564 
565         aml_append(ifctx, aml_store(zero, caps));
566         aml_append(ifctx,
567             aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
568         /*
569          * advertise function 7 if device has acpi-index
570          * acpi_index values:
571          *            0: not present (default value)
572          *     FFFFFFFF: not supported (old QEMU without PIDX reg)
573          *        other: device's acpi-index
574          */
575         ifctx1 = aml_if(aml_lnot(
576                      aml_or(aml_equal(acpi_index, zero),
577                             aml_equal(acpi_index, not_supp), NULL)
578                  ));
579         {
580             /* have supported functions */
581             aml_append(ifctx1, aml_or(caps, one, caps));
582             /* support for function 7 */
583             aml_append(ifctx1,
584                 aml_or(caps, aml_shiftleft(one, aml_int(7)), caps));
585         }
586         aml_append(ifctx, ifctx1);
587 
588         aml_append(ifctx, aml_store(caps, aml_index(ret, zero)));
589         aml_append(ifctx, aml_return(ret));
590     }
591     aml_append(method, ifctx);
592 
593     /* handle specific functions requests */
594     /*
595      * PCI Firmware Specification 3.1
596      * 4.6.7. _DSM for Naming a PCI or PCI Express Device Under
597      *        Operating Systems
598      */
599     ifctx = aml_if(aml_equal(func, aml_int(7)));
600     {
601        Aml *pkg = aml_package(2);
602 
603        aml_append(ifctx, aml_store(aml_call2("AIDX", bnum, sunum), acpi_index));
604        aml_append(ifctx, aml_store(pkg, ret));
605        /*
606         * Windows calls func=7 without checking if it's available,
607         * as workaround Microsoft has suggested to return invalid for func7
608         * Package, so return 2 elements package but only initialize elements
609         * when acpi_index is supported and leave them uninitialized, which
610         * leads elements to being Uninitialized ObjectType and should trip
611         * Windows into discarding result as an unexpected and prevent setting
612         * bogus 'PCI Label' on the device.
613         */
614        ifctx1 = aml_if(aml_lnot(aml_lor(
615                     aml_equal(acpi_index, zero), aml_equal(acpi_index, not_supp)
616                 )));
617        {
618            aml_append(ifctx1, aml_store(acpi_index, aml_index(ret, zero)));
619            /*
620             * optional, if not impl. should return null string
621             */
622            aml_append(ifctx1, aml_store(aml_string("%s", ""),
623                                         aml_index(ret, one)));
624        }
625        aml_append(ifctx, ifctx1);
626 
627        aml_append(ifctx, aml_return(ret));
628     }
629 
630     aml_append(method, ifctx);
631     return method;
632 }
633 
634 void build_acpi_pci_hotplug(Aml *table, AmlRegionSpace rs, uint64_t pcihp_addr)
635 {
636     Aml *scope;
637     Aml *field;
638     Aml *method;
639 
640     scope =  aml_scope("_SB.PCI0");
641 
642     aml_append(scope,
643         aml_operation_region("PCST", rs, aml_int(pcihp_addr), 0x08));
644     field = aml_field("PCST", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
645     aml_append(field, aml_named_field("PCIU", 32));
646     aml_append(field, aml_named_field("PCID", 32));
647     aml_append(scope, field);
648 
649     aml_append(scope,
650         aml_operation_region("SEJ", rs,
651                              aml_int(pcihp_addr + ACPI_PCIHP_SEJ_BASE), 0x04));
652     field = aml_field("SEJ", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
653     aml_append(field, aml_named_field("B0EJ", 32));
654     aml_append(scope, field);
655 
656     aml_append(scope,
657         aml_operation_region("BNMR", rs,
658                              aml_int(pcihp_addr + ACPI_PCIHP_BNMR_BASE), 0x08));
659     field = aml_field("BNMR", AML_DWORD_ACC, AML_NOLOCK, AML_WRITE_AS_ZEROS);
660     aml_append(field, aml_named_field("BNUM", 32));
661     aml_append(field, aml_named_field("PIDX", 32));
662     aml_append(scope, field);
663 
664     aml_append(scope, aml_mutex("BLCK", 0));
665 
666         method = aml_method("PCEJ", 2, AML_NOTSERIALIZED);
667     aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
668     aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
669     aml_append(method,
670         aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("B0EJ")));
671     aml_append(method, aml_release(aml_name("BLCK")));
672     aml_append(method, aml_return(aml_int(0)));
673     aml_append(scope, method);
674 
675     method = aml_method("AIDX", 2, AML_NOTSERIALIZED);
676     aml_append(method, aml_acquire(aml_name("BLCK"), 0xFFFF));
677     aml_append(method, aml_store(aml_arg(0), aml_name("BNUM")));
678     aml_append(method,
679         aml_store(aml_shiftleft(aml_int(1), aml_arg(1)), aml_name("PIDX")));
680     aml_append(method, aml_store(aml_name("PIDX"), aml_local(0)));
681     aml_append(method, aml_release(aml_name("BLCK")));
682     aml_append(method, aml_return(aml_local(0)));
683     aml_append(scope, method);
684 
685     aml_append(scope, aml_pci_pdsm());
686 
687     aml_append(table, scope);
688 }
689 
690 /* Reserve PCIHP resources */
691 void build_append_pcihp_resources(Aml *scope /* \\_SB.PCI0 */,
692                                   uint64_t io_addr, uint64_t io_len)
693 {
694     Aml *dev, *crs;
695 
696     dev = aml_device("PHPR");
697     aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
698     aml_append(dev,
699                aml_name_decl("_UID", aml_string("PCI Hotplug resources")));
700     /* device present, functioning, decoding, not shown in UI */
701     aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
702     crs = aml_resource_template();
703     aml_append(crs, aml_io(AML_DECODE16, io_addr, io_addr, 1, io_len));
704     aml_append(dev, aml_name_decl("_CRS", crs));
705     aml_append(scope, dev);
706 }
707 
708 bool build_append_notification_callback(Aml *parent_scope, const PCIBus *bus)
709 {
710     Aml *method;
711     PCIBus *sec;
712     QObject *bsel;
713     int nr_notifiers = 0;
714     GQueue *pcnt_bus_list = g_queue_new();
715 
716     QLIST_FOREACH(sec, &bus->child, sibling) {
717         Aml *br_scope = aml_scope("S%.02X", sec->parent_dev->devfn);
718         if (pci_bus_is_root(sec)) {
719             continue;
720         }
721         nr_notifiers = nr_notifiers +
722                        build_append_notification_callback(br_scope, sec);
723         /*
724          * add new child scope to parent
725          * and keep track of bus that have PCNT,
726          * bus list is used later to call children PCNTs from this level PCNT
727          */
728         if (nr_notifiers) {
729             g_queue_push_tail(pcnt_bus_list, sec);
730             aml_append(parent_scope, br_scope);
731         }
732     }
733 
734     /*
735      * Append PCNT method to notify about events on local and child buses.
736      * ps: hostbridge might not have hotplug (bsel) enabled but might have
737      * child bridges that do have bsel.
738      */
739     method = aml_method("PCNT", 0, AML_NOTSERIALIZED);
740 
741     /* If bus supports hotplug select it and notify about local events */
742     bsel = object_property_get_qobject(OBJECT(bus), ACPI_PCIHP_PROP_BSEL, NULL);
743     if (bsel) {
744         uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
745 
746         aml_append(method, aml_store(aml_int(bsel_val), aml_name("BNUM")));
747         aml_append(method, aml_call2("DVNT", aml_name("PCIU"),
748                                      aml_int(1))); /* Device Check */
749         aml_append(method, aml_call2("DVNT", aml_name("PCID"),
750                                      aml_int(3))); /* Eject Request */
751         nr_notifiers++;
752     }
753 
754     /* Notify about child bus events in any case */
755     while ((sec = g_queue_pop_head(pcnt_bus_list))) {
756         aml_append(method, aml_name("^S%.02X.PCNT", sec->parent_dev->devfn));
757     }
758 
759     aml_append(parent_scope, method);
760     qobject_unref(bsel);
761     g_queue_free(pcnt_bus_list);
762     return !!nr_notifiers;
763 }
764 
765 static Aml *aml_pci_device_dsm(void)
766 {
767     Aml *method;
768 
769     method = aml_method("_DSM", 4, AML_SERIALIZED);
770     {
771         Aml *params = aml_local(0);
772         Aml *pkg = aml_package(2);
773         aml_append(pkg, aml_int(0));
774         aml_append(pkg, aml_int(0));
775         aml_append(method, aml_store(pkg, params));
776         aml_append(method,
777             aml_store(aml_name("BSEL"), aml_index(params, aml_int(0))));
778         aml_append(method,
779             aml_store(aml_name("ASUN"), aml_index(params, aml_int(1))));
780         aml_append(method,
781             aml_return(aml_call5("PDSM", aml_arg(0), aml_arg(1),
782                                  aml_arg(2), aml_arg(3), params))
783         );
784     }
785     return method;
786 }
787 
788 static Aml *aml_pci_static_endpoint_dsm(PCIDevice *pdev)
789 {
790     Aml *method;
791 
792     g_assert(pdev->acpi_index != 0);
793     method = aml_method("_DSM", 4, AML_SERIALIZED);
794     {
795         Aml *params = aml_local(0);
796         Aml *pkg = aml_package(1);
797         aml_append(pkg, aml_int(pdev->acpi_index));
798         aml_append(method, aml_store(pkg, params));
799         aml_append(method,
800             aml_return(aml_call5("EDSM", aml_arg(0), aml_arg(1),
801                                  aml_arg(2), aml_arg(3), params))
802         );
803     }
804     return method;
805 }
806 
807 static void build_append_pcihp_notify_entry(Aml *method, int slot)
808 {
809     Aml *if_ctx;
810     int32_t devfn = PCI_DEVFN(slot, 0);
811 
812     if_ctx = aml_if(aml_and(aml_arg(0), aml_int(0x1U << slot), NULL));
813     aml_append(if_ctx, aml_notify(aml_name("S%.02X", devfn), aml_arg(1)));
814     aml_append(method, if_ctx);
815 }
816 
817 static bool is_devfn_ignored_generic(const int devfn, const PCIBus *bus)
818 {
819     const PCIDevice *pdev = bus->devices[devfn];
820 
821     if (PCI_FUNC(devfn)) {
822         if (IS_PCI_BRIDGE(pdev)) {
823             /*
824              * Ignore only hotplugged PCI bridges on !0 functions, but
825              * allow describing cold plugged bridges on all functions
826              */
827             if (DEVICE(pdev)->hotplugged) {
828                 return true;
829             }
830         }
831     }
832     return false;
833 }
834 
835 static bool is_devfn_ignored_hotplug(const int devfn, const PCIBus *bus)
836 {
837     PCIDevice *pdev = bus->devices[devfn];
838     if (pdev) {
839         return is_devfn_ignored_generic(devfn, bus) ||
840                !DEVICE_GET_CLASS(pdev)->hotpluggable ||
841                /* Cold plugged bridges aren't themselves hot-pluggable */
842                (IS_PCI_BRIDGE(pdev) && !DEVICE(pdev)->hotplugged);
843     } else { /* non populated slots */
844          /*
845           * hotplug is supported only for non-multifunction device
846           * so generate device description only for function 0
847           */
848         if (PCI_FUNC(devfn) ||
849             (pci_bus_is_express(bus) && PCI_SLOT(devfn) > 0)) {
850             return true;
851         }
852     }
853     return false;
854 }
855 
856 void build_append_pcihp_slots(Aml *parent_scope, PCIBus *bus)
857 {
858     int devfn;
859     Aml *dev, *notify_method = NULL, *method;
860     QObject *bsel = object_property_get_qobject(OBJECT(bus),
861                         ACPI_PCIHP_PROP_BSEL, NULL);
862     uint64_t bsel_val = qnum_get_uint(qobject_to(QNum, bsel));
863     qobject_unref(bsel);
864 
865     aml_append(parent_scope, aml_name_decl("BSEL", aml_int(bsel_val)));
866     notify_method = aml_method("DVNT", 2, AML_NOTSERIALIZED);
867 
868     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
869         int slot = PCI_SLOT(devfn);
870         int adr = slot << 16 | PCI_FUNC(devfn);
871 
872         if (is_devfn_ignored_hotplug(devfn, bus)) {
873             continue;
874         }
875 
876         if (bus->devices[devfn]) {
877             dev = aml_scope("S%.02X", devfn);
878         } else {
879             dev = aml_device("S%.02X", devfn);
880             aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
881         }
882 
883         /*
884          * Can't declare _SUN here for every device as it changes 'slot'
885          * enumeration order in linux kernel, so use another variable for it
886          */
887         aml_append(dev, aml_name_decl("ASUN", aml_int(slot)));
888         aml_append(dev, aml_pci_device_dsm());
889 
890         aml_append(dev, aml_name_decl("_SUN", aml_int(slot)));
891         /* add _EJ0 to make slot hotpluggable  */
892         method = aml_method("_EJ0", 1, AML_NOTSERIALIZED);
893         aml_append(method,
894             aml_call2("PCEJ", aml_name("BSEL"), aml_name("_SUN"))
895         );
896         aml_append(dev, method);
897 
898         build_append_pcihp_notify_entry(notify_method, slot);
899 
900         /* device descriptor has been composed, add it into parent context */
901         aml_append(parent_scope, dev);
902     }
903     aml_append(parent_scope, notify_method);
904 }
905 
906 void build_append_pci_bus_devices(Aml *parent_scope, PCIBus *bus)
907 {
908     int devfn;
909     Aml *dev;
910 
911     for (devfn = 0; devfn < ARRAY_SIZE(bus->devices); devfn++) {
912         /* ACPI spec: 1.0b: Table 6-2 _ADR Object Bus Types, PCI type */
913         int adr = PCI_SLOT(devfn) << 16 | PCI_FUNC(devfn);
914         PCIDevice *pdev = bus->devices[devfn];
915 
916         if (!pdev || is_devfn_ignored_generic(devfn, bus)) {
917             continue;
918         }
919 
920         /* start to compose PCI device descriptor */
921         dev = aml_device("S%.02X", devfn);
922         aml_append(dev, aml_name_decl("_ADR", aml_int(adr)));
923 
924         call_dev_aml_func(DEVICE(bus->devices[devfn]), dev);
925         /* add _DSM if device has acpi-index set */
926         if (pdev->acpi_index &&
927             !object_property_get_bool(OBJECT(pdev), "hotpluggable",
928                                       &error_abort)) {
929             aml_append(dev, aml_pci_static_endpoint_dsm(pdev));
930         }
931 
932         /* device descriptor has been composed, add it into parent context */
933         aml_append(parent_scope, dev);
934     }
935 }
936 
937 const VMStateDescription vmstate_acpi_pcihp_pci_status = {
938     .name = "acpi_pcihp_pci_status",
939     .version_id = 1,
940     .minimum_version_id = 1,
941     .fields = (const VMStateField[]) {
942         VMSTATE_UINT32(up, AcpiPciHpPciStatus),
943         VMSTATE_UINT32(down, AcpiPciHpPciStatus),
944         VMSTATE_END_OF_LIST()
945     }
946 };
947