Coverage Report

Created: 2026-10-31 08:12

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/hw/i386/acpi-build.c
Line
Count
Source
1
/* Support for generating ACPI tables and passing them to Guests
2
 *
3
 * Copyright (C) 2008-2010  Kevin O'Connor <kevin@koconnor.net>
4
 * Copyright (C) 2006 Fabrice Bellard
5
 * Copyright (C) 2013 Red Hat Inc
6
 *
7
 * Author: Michael S. Tsirkin <mst@redhat.com>
8
 *
9
 * This program is free software; you can redistribute it and/or modify
10
 * it under the terms of the GNU General Public License as published by
11
 * the Free Software Foundation; either version 2 of the License, or
12
 * (at your option) any later version.
13
14
 * This program is distributed in the hope that it will be useful,
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 * GNU General Public License for more details.
18
19
 * You should have received a copy of the GNU General Public License along
20
 * with this program; if not, see <http://www.gnu.org/licenses/>.
21
 */
22
23
#include "qemu/osdep.h"
24
#include "qapi/error.h"
25
#include "qobject/qnum.h"
26
#include "acpi-build.h"
27
#include "acpi-common.h"
28
#include "qemu/bitmap.h"
29
#include "qemu/error-report.h"
30
#include "hw/pci/pci_bridge.h"
31
#include "hw/cxl/cxl.h"
32
#include "hw/core/cpu.h"
33
#include "target/i386/cpu.h"
34
#include "hw/timer/hpet.h"
35
#include "hw/acpi/acpi-defs.h"
36
#include "hw/acpi/acpi.h"
37
#include "hw/acpi/cpu.h"
38
#include "hw/acpi/pc-hotplug.h"
39
#include "hw/nvram/fw_cfg.h"
40
#include "hw/acpi/bios-linker-loader.h"
41
#include "hw/acpi/acpi_aml_interface.h"
42
#include "hw/input/i8042.h"
43
#include "hw/acpi/memory_hotplug.h"
44
#include "system/tpm.h"
45
#include "hw/acpi/tpm.h"
46
#include "hw/acpi/vmgenid.h"
47
#include "hw/acpi/vmclock.h"
48
#include "hw/acpi/erst.h"
49
#include "hw/acpi/piix4.h"
50
#include "system/tpm_backend.h"
51
#include "hw/rtc/mc146818rtc_regs.h"
52
#include "migration/vmstate.h"
53
#include "hw/mem/memory-device.h"
54
#include "hw/mem/nvdimm.h"
55
#include "hw/mem/sp-mem.h"
56
#include "system/numa.h"
57
#include "system/reset.h"
58
#include "hw/hyperv/vmbus-bridge.h"
59
60
/* Supported chipsets: */
61
#include "hw/southbridge/ich9.h"
62
#include "hw/acpi/pcihp.h"
63
#include "hw/i386/fw_cfg.h"
64
#include "hw/i386/pc.h"
65
#include "hw/pci/pci_bus.h"
66
#include "hw/pci-host/i440fx.h"
67
#include "hw/pci-host/q35.h"
68
#include "hw/i386/x86-iommu.h"
69
70
#include "hw/acpi/aml-build.h"
71
#include "hw/acpi/utils.h"
72
#include "hw/acpi/pci.h"
73
#include "hw/acpi/cxl.h"
74
75
#include "qom/qom-qobject.h"
76
#include "hw/i386/amd_iommu.h"
77
#include "hw/i386/intel_iommu.h"
78
#include "hw/virtio/virtio-iommu.h"
79
80
#include "hw/acpi/hmat.h"
81
#include "hw/acpi/viot.h"
82
#include "hw/acpi/wdat-ich9.h"
83
84
#include CONFIG_DEVICES
85
86
/* These are used to size the ACPI tables for -M pc-i440fx-1.7 and
87
 * -M pc-i440fx-2.0.  Even if the actual amount of AML generated grows
88
 * a little bit, there should be plenty of free space since the DSDT
89
 * shrunk by ~1.5k between QEMU 2.0 and QEMU 2.1.
90
 */
91
61
#define ACPI_BUILD_ALIGN_SIZE             0x1000
92
93
61
#define ACPI_BUILD_TABLE_SIZE             0x20000
94
95
/* #define DEBUG_ACPI_BUILD */
96
#ifdef DEBUG_ACPI_BUILD
97
#define ACPI_BUILD_DPRINTF(fmt, ...)        \
98
    do {printf("ACPI_BUILD: " fmt, ## __VA_ARGS__); } while (0)
99
#else
100
#define ACPI_BUILD_DPRINTF(fmt, ...)
101
#endif
102
103
typedef struct AcpiPmInfo {
104
    bool s3_disabled;
105
    bool s4_disabled;
106
    bool pcihp_bridge_en;
107
    bool smi_on_cpuhp;
108
    bool smi_on_cpu_unplug;
109
    bool pcihp_root_en;
110
    uint8_t s4_val;
111
    AcpiFadtData fadt;
112
    uint16_t cpu_hp_io_base;
113
    uint16_t pcihp_io_base;
114
    uint16_t pcihp_io_len;
115
    uint64_t tco_io_base;
116
} AcpiPmInfo;
117
118
typedef struct AcpiMiscInfo {
119
    bool has_hpet;
120
#ifdef CONFIG_TPM
121
    TPMVersion tpm_version;
122
#endif
123
} AcpiMiscInfo;
124
125
typedef struct FwCfgTPMConfig {
126
    uint32_t tpmppi_address;
127
    uint8_t tpm_version;
128
    uint8_t tpmppi_version;
129
} QEMU_PACKED FwCfgTPMConfig;
130
131
static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg);
132
133
const struct AcpiGenericAddress x86_nvdimm_acpi_dsmio = {
134
    .space_id = AML_AS_SYSTEM_IO,
135
    .address = NVDIMM_ACPI_IO_BASE,
136
    .bit_width = NVDIMM_ACPI_IO_LEN << 3
137
};
138
139
static void init_common_fadt_data(MachineState *ms, Object *o,
140
                                  AcpiFadtData *data)
141
61
{
142
61
    X86MachineState *x86ms = X86_MACHINE(ms);
143
    /*
144
     * "ICH9-LPC" or "PIIX4_PM" has "smm-compat" property to keep the old
145
     * behavior for compatibility irrelevant to smm_enabled, which doesn't
146
     * conform to the ACPI spec.
147
     */
148
61
    bool smm_enabled = object_property_get_bool(o, "smm-compat", NULL) ?
149
61
        true : x86_machine_is_smm_enabled(x86ms);
150
61
    uint32_t io = object_property_get_uint(o, ACPI_PM_PROP_PM_IO_BASE, NULL);
151
61
    AmlAddressSpace as = AML_AS_SYSTEM_IO;
152
61
    AcpiFadtData fadt = {
153
61
        .rev = 3,
154
61
        .flags =
155
61
            (1 << ACPI_FADT_F_WBINVD) |
156
61
            (1 << ACPI_FADT_F_PROC_C1) |
157
61
            (1 << ACPI_FADT_F_SLP_BUTTON) |
158
61
            (1 << ACPI_FADT_F_RTC_S4) |
159
61
            (1 << ACPI_FADT_F_USE_PLATFORM_CLOCK) |
160
            /* APIC destination mode ("Flat Logical") has an upper limit of 8
161
             * CPUs for more than 8 CPUs, "Clustered Logical" mode has to be
162
             * used
163
             */
164
61
            ((ms->smp.max_cpus > 8) ?
165
61
                        (1 << ACPI_FADT_F_FORCE_APIC_CLUSTER_MODEL) : 0),
166
61
        .int_model = 1 /* Multiple APIC */,
167
61
        .rtc_century = RTC_CENTURY,
168
61
        .plvl2_lat = 0xfff /* C2 state not supported */,
169
61
        .plvl3_lat = 0xfff /* C3 state not supported */,
170
61
        .smi_cmd = smm_enabled ? ACPI_PORT_SMI_CMD : 0,
171
61
        .sci_int = object_property_get_uint(o, ACPI_PM_PROP_SCI_INT, NULL),
172
61
        .acpi_enable_cmd =
173
61
            smm_enabled ?
174
61
            object_property_get_uint(o, ACPI_PM_PROP_ACPI_ENABLE_CMD, NULL) :
175
61
            0,
176
61
        .acpi_disable_cmd =
177
61
            smm_enabled ?
178
61
            object_property_get_uint(o, ACPI_PM_PROP_ACPI_DISABLE_CMD, NULL) :
179
61
            0,
180
61
        .pm1a_evt = { .space_id = as, .bit_width = 4 * 8, .address = io },
181
61
        .pm1a_cnt = { .space_id = as, .bit_width = 2 * 8,
182
61
                      .address = io + 0x04 },
183
61
        .pm_tmr = { .space_id = as, .bit_width = 4 * 8, .address = io + 0x08 },
184
61
        .gpe0_blk = { .space_id = as, .bit_width =
185
61
            object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK_LEN, NULL) * 8,
186
61
            .address = object_property_get_uint(o, ACPI_PM_PROP_GPE0_BLK, NULL)
187
61
        },
188
61
    };
189
190
    /*
191
     * ACPI v2, Table 5-10 - Fixed ACPI Description Table Boot Architecture
192
     * Flags, bit offset 1 - 8042.
193
     */
194
61
    fadt.iapc_boot_arch = iapc_boot_arch_8042();
195
196
61
    *data = fadt;
197
61
}
198
199
static void acpi_get_pm_info(MachineState *machine, AcpiPmInfo *pm)
200
61
{
201
61
    Object *piix = object_resolve_type_unambiguous(TYPE_PIIX4_PM, NULL);
202
61
    Object *lpc = object_resolve_type_unambiguous(TYPE_ICH9_LPC_DEVICE, NULL);
203
61
    Object *obj = piix ? piix : lpc;
204
61
    QObject *o;
205
61
    pm->cpu_hp_io_base = 0;
206
61
    pm->pcihp_io_base = 0;
207
61
    pm->pcihp_io_len = 0;
208
61
    pm->smi_on_cpuhp = false;
209
61
    pm->smi_on_cpu_unplug = false;
210
61
    pm->tco_io_base = 0;
211
212
61
    assert(obj);
213
61
    init_common_fadt_data(machine, obj, &pm->fadt);
214
61
    if (piix) {
215
        /* w2k requires FADT(rev1) or it won't boot, keep PC compatible */
216
8
        pm->fadt.rev = 1;
217
8
        pm->cpu_hp_io_base = PIIX4_CPU_HOTPLUG_IO_BASE;
218
8
    }
219
61
    if (lpc) {
220
53
        uint64_t smi_features = object_property_get_uint(lpc,
221
53
            ICH9_LPC_SMI_NEGOTIATED_FEAT_PROP, NULL);
222
53
        struct AcpiGenericAddress r = { .space_id = AML_AS_SYSTEM_IO,
223
53
            .bit_width = 8, .address = ICH9_RST_CNT_IOPORT };
224
53
        pm->fadt.reset_reg = r;
225
53
        pm->fadt.reset_val = 0xf;
226
53
        pm->fadt.flags |= 1 << ACPI_FADT_F_RESET_REG_SUP;
227
53
        pm->cpu_hp_io_base = ICH9_CPU_HOTPLUG_IO_BASE;
228
53
        pm->smi_on_cpuhp =
229
53
            !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOTPLUG_BIT));
230
53
        pm->smi_on_cpu_unplug =
231
53
            !!(smi_features & BIT_ULL(ICH9_LPC_SMI_F_CPU_HOT_UNPLUG_BIT));
232
53
        pm->tco_io_base = object_property_get_uint(obj, ACPI_PM_PROP_PM_IO_BASE,
233
53
            NULL) + ICH9_PMIO_TCO_RLD;
234
53
    }
235
61
    pm->pcihp_io_base =
236
61
        object_property_get_uint(obj, ACPI_PCIHP_IO_BASE_PROP, NULL);
237
61
    pm->pcihp_io_len =
238
61
        object_property_get_uint(obj, ACPI_PCIHP_IO_LEN_PROP, NULL);
239
240
    /* Fill in optional s3/s4 related properties */
241
61
    o = object_property_get_qobject(obj, ACPI_PM_PROP_S3_DISABLED, NULL);
242
61
    if (o) {
243
61
        pm->s3_disabled = qnum_get_uint(qobject_to(QNum, o));
244
61
    } else {
245
0
        pm->s3_disabled = false;
246
0
    }
247
61
    qobject_unref(o);
248
61
    o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_DISABLED, NULL);
249
61
    if (o) {
250
61
        pm->s4_disabled = qnum_get_uint(qobject_to(QNum, o));
251
61
    } else {
252
0
        pm->s4_disabled = false;
253
0
    }
254
61
    qobject_unref(o);
255
61
    o = object_property_get_qobject(obj, ACPI_PM_PROP_S4_VAL, NULL);
256
61
    if (o) {
257
61
        pm->s4_val = qnum_get_uint(qobject_to(QNum, o));
258
61
    } else {
259
0
        pm->s4_val = false;
260
0
    }
261
61
    qobject_unref(o);
262
263
61
    pm->pcihp_bridge_en =
264
61
        object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCIHP_BRIDGE,
265
61
                                 NULL);
266
61
    pm->pcihp_root_en =
267
61
        object_property_get_bool(obj, ACPI_PM_PROP_ACPI_PCI_ROOTHP,
268
61
                                 NULL);
269
61
}
270
271
static void acpi_get_misc_info(AcpiMiscInfo *info)
272
61
{
273
61
    info->has_hpet = hpet_find();
274
61
#ifdef CONFIG_TPM
275
61
    info->tpm_version = tpm_get_version(tpm_find());
276
61
#endif
277
61
}
278
279
/*
280
 * Because of the PXB hosts we cannot simply query TYPE_PCI_HOST_BRIDGE.
281
 * On i386 arch we only have two pci hosts, so we can look only for them.
282
 */
283
Object *acpi_get_i386_pci_host(void)
284
305
{
285
305
    PCIHostState *host;
286
287
305
    host = PCI_HOST_BRIDGE(object_resolve_path("/machine/i440fx", NULL));
288
305
    if (!host) {
289
265
        host = PCI_HOST_BRIDGE(object_resolve_path("/machine/q35", NULL));
290
265
    }
291
292
305
    return OBJECT(host);
293
305
}
294
295
static void acpi_get_pci_holes(Range *hole, Range *hole64)
296
61
{
297
61
    Object *pci_host;
298
299
61
    pci_host = acpi_get_i386_pci_host();
300
301
61
    if (!pci_host) {
302
0
        return;
303
0
    }
304
305
61
    range_set_bounds1(hole,
306
61
                      object_property_get_uint(pci_host,
307
61
                                               PCI_HOST_PROP_PCI_HOLE_START,
308
61
                                               NULL),
309
61
                      object_property_get_uint(pci_host,
310
61
                                               PCI_HOST_PROP_PCI_HOLE_END,
311
61
                                               NULL));
312
61
    range_set_bounds1(hole64,
313
61
                      object_property_get_uint(pci_host,
314
61
                                               PCI_HOST_PROP_PCI_HOLE64_START,
315
61
                                               NULL),
316
61
                      object_property_get_uint(pci_host,
317
61
                                               PCI_HOST_PROP_PCI_HOLE64_END,
318
61
                                               NULL));
319
61
}
320
321
static void acpi_align_size(GArray *blob, unsigned align)
322
122
{
323
    /* Align size to multiple of given size. This reduces the chance
324
     * we need to change size in the future (breaking cross version migration).
325
     */
326
122
    g_array_set_size(blob, ROUND_UP(acpi_data_len(blob), align));
327
122
}
328
329
/*
330
 * ACPI spec 1.0b,
331
 * 5.2.6 Firmware ACPI Control Structure
332
 */
333
static void
334
build_facs(GArray *table_data)
335
61
{
336
61
    const char *sig = "FACS";
337
61
    const uint8_t reserved[40] = {};
338
339
61
    g_array_append_vals(table_data, sig, 4); /* Signature */
340
61
    build_append_int_noprefix(table_data, 64, 4); /* Length */
341
61
    build_append_int_noprefix(table_data, 0, 4); /* Hardware Signature */
342
61
    build_append_int_noprefix(table_data, 0, 4); /* Firmware Waking Vector */
343
61
    build_append_int_noprefix(table_data, 0, 4); /* Global Lock */
344
61
    build_append_int_noprefix(table_data, 0, 4); /* Flags */
345
61
    g_array_append_vals(table_data, reserved, 40); /* Reserved */
346
61
}
347
348
/*
349
 * build_prt - Define interrupt routing rules
350
 *
351
 * Returns an array of 128 routes, one for each device,
352
 * based on device location.
353
 * The main goal is to equally distribute the interrupts
354
 * over the 4 existing ACPI links (works only for i440fx).
355
 * The hash function is: (slot + pin) & 3 -> "LNK[D|A|B|C]".
356
 *
357
 */
358
static Aml *build_prt(bool is_pci0_prt)
359
8
{
360
8
    const int nroutes = 128;
361
8
    Aml *rt_pkg, *method;
362
8
    int pin;
363
364
8
    method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
365
8
    assert(nroutes < 256);
366
8
    rt_pkg = aml_package(nroutes);
367
368
1.03k
    for (pin = 0; pin < nroutes; pin++) {
369
1.02k
        Aml *pkg = aml_package(4);
370
1.02k
        int slot = pin >> 2;
371
372
1.02k
        aml_append(pkg, aml_int((slot << 16) | 0xFFFF));
373
1.02k
        aml_append(pkg, aml_int(pin & 3));
374
        /* device 1 is the power-management device, needs SCI */
375
1.02k
        if (is_pci0_prt && pin == 4) {
376
8
            aml_append(pkg, aml_name("%s", "LNKS"));
377
1.01k
        } else {
378
1.01k
            static const char link_name[][5] = {"LNKD", "LNKA", "LNKB", "LNKC"};
379
1.01k
            int hash = (slot + pin) & 3;
380
1.01k
            aml_append(pkg, aml_name("%s", link_name[hash]));
381
1.01k
        }
382
1.02k
        aml_append(pkg, aml_int(0));
383
1.02k
        aml_append(rt_pkg, pkg);
384
1.02k
    }
385
386
8
    aml_append(method, aml_return(rt_pkg));
387
388
8
    return method;
389
8
}
390
391
static void build_hpet_aml(Aml *table)
392
61
{
393
61
    Aml *crs;
394
61
    Aml *field;
395
61
    Aml *method;
396
61
    Aml *if_ctx;
397
61
    Aml *scope = aml_scope("_SB");
398
61
    Aml *dev = aml_device("HPET");
399
61
    Aml *zero = aml_int(0);
400
61
    Aml *id = aml_local(0);
401
61
    Aml *period = aml_local(1);
402
403
61
    aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0103")));
404
61
    aml_append(dev, aml_name_decl("_UID", zero));
405
406
61
    aml_append(dev,
407
61
        aml_operation_region("HPTM", AML_SYSTEM_MEMORY, aml_int(HPET_BASE),
408
61
                             HPET_LEN));
409
61
    field = aml_field("HPTM", AML_DWORD_ACC, AML_LOCK, AML_PRESERVE);
410
61
    aml_append(field, aml_named_field("VEND", 32));
411
61
    aml_append(field, aml_named_field("PRD", 32));
412
61
    aml_append(dev, field);
413
414
61
    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
415
61
    aml_append(method, aml_store(aml_name("VEND"), id));
416
61
    aml_append(method, aml_store(aml_name("PRD"), period));
417
61
    aml_append(method, aml_shiftright(id, aml_int(16), id));
418
61
    if_ctx = aml_if(aml_lor(aml_equal(id, zero),
419
61
                            aml_equal(id, aml_int(0xffff))));
420
61
    {
421
61
        aml_append(if_ctx, aml_return(zero));
422
61
    }
423
61
    aml_append(method, if_ctx);
424
425
61
    if_ctx = aml_if(aml_lor(aml_equal(period, zero),
426
61
                            aml_lgreater(period, aml_int(100000000))));
427
61
    {
428
61
        aml_append(if_ctx, aml_return(zero));
429
61
    }
430
61
    aml_append(method, if_ctx);
431
432
61
    aml_append(method, aml_return(aml_int(0x0F)));
433
61
    aml_append(dev, method);
434
435
61
    crs = aml_resource_template();
436
61
    aml_append(crs, aml_memory32_fixed(HPET_BASE, HPET_LEN, AML_READ_ONLY));
437
61
    aml_append(dev, aml_name_decl("_CRS", crs));
438
439
61
    aml_append(scope, dev);
440
61
    aml_append(table, scope);
441
61
}
442
443
static Aml *build_vmbus_device_aml(VMBusBridge *vmbus_bridge)
444
0
{
445
0
    Aml *dev;
446
0
    Aml *method;
447
0
    Aml *crs;
448
449
0
    dev = aml_device("VMBS");
450
0
    aml_append(dev, aml_name_decl("STA", aml_int(0xF)));
451
0
    aml_append(dev, aml_name_decl("_HID", aml_string("VMBus")));
452
0
    aml_append(dev, aml_name_decl("_UID", aml_int(0x0)));
453
0
    aml_append(dev, aml_name_decl("_DDN", aml_string("VMBUS")));
454
455
0
    method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
456
0
    aml_append(method, aml_store(aml_and(aml_name("STA"), aml_int(0xD), NULL),
457
0
                                     aml_name("STA")));
458
0
    aml_append(dev, method);
459
460
0
    method = aml_method("_PS0", 0, AML_NOTSERIALIZED);
461
0
    aml_append(method, aml_store(aml_or(aml_name("STA"), aml_int(0xF), NULL),
462
0
                                     aml_name("STA")));
463
0
    aml_append(dev, method);
464
465
0
    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
466
0
    aml_append(method, aml_return(aml_name("STA")));
467
0
    aml_append(dev, method);
468
469
0
    aml_append(dev, aml_name_decl("_PS3", aml_int(0x0)));
470
471
0
    crs = aml_resource_template();
472
0
    aml_append(crs, aml_irq_no_flags(vmbus_bridge->irq));
473
0
    aml_append(dev, aml_name_decl("_CRS", crs));
474
475
0
    return dev;
476
0
}
477
478
static void build_dbg_aml(Aml *table)
479
61
{
480
61
    Aml *field;
481
61
    Aml *method;
482
61
    Aml *while_ctx;
483
61
    Aml *scope = aml_scope("\\");
484
61
    Aml *buf = aml_local(0);
485
61
    Aml *len = aml_local(1);
486
61
    Aml *idx = aml_local(2);
487
488
61
    aml_append(scope,
489
61
       aml_operation_region("DBG", AML_SYSTEM_IO, aml_int(0x0402), 0x01));
490
61
    field = aml_field("DBG", AML_BYTE_ACC, AML_NOLOCK, AML_PRESERVE);
491
61
    aml_append(field, aml_named_field("DBGB", 8));
492
61
    aml_append(scope, field);
493
494
61
    method = aml_method("DBUG", 1, AML_NOTSERIALIZED);
495
496
61
    aml_append(method, aml_to_hexstring(aml_arg(0), buf));
497
61
    aml_append(method, aml_to_buffer(buf, buf));
498
61
    aml_append(method, aml_subtract(aml_sizeof(buf), aml_int(1), len));
499
61
    aml_append(method, aml_store(aml_int(0), idx));
500
501
61
    while_ctx = aml_while(aml_lless(idx, len));
502
61
    aml_append(while_ctx,
503
61
        aml_store(aml_derefof(aml_index(buf, idx)), aml_name("DBGB")));
504
61
    aml_append(while_ctx, aml_increment(idx));
505
61
    aml_append(method, while_ctx);
506
507
61
    aml_append(method, aml_store(aml_int(0x0A), aml_name("DBGB")));
508
61
    aml_append(scope, method);
509
510
61
    aml_append(table, scope);
511
61
}
512
513
static Aml *build_link_dev(const char *name, uint8_t uid, Aml *reg)
514
456
{
515
456
    Aml *dev;
516
456
    Aml *crs;
517
456
    Aml *method;
518
456
    uint32_t irqs[] = {5, 10, 11};
519
520
456
    dev = aml_device("%s", name);
521
456
    aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
522
456
    aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
523
524
456
    crs = aml_resource_template();
525
456
    aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
526
456
                                  AML_SHARED, irqs, ARRAY_SIZE(irqs)));
527
456
    aml_append(dev, aml_name_decl("_PRS", crs));
528
529
456
    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
530
456
    aml_append(method, aml_return(aml_call1("IQST", reg)));
531
456
    aml_append(dev, method);
532
533
456
    method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
534
456
    aml_append(method, aml_or(reg, aml_int(0x80), reg));
535
456
    aml_append(dev, method);
536
537
456
    method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
538
456
    aml_append(method, aml_return(aml_call1("IQCR", reg)));
539
456
    aml_append(dev, method);
540
541
456
    method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
542
456
    aml_append(method, aml_create_dword_field(aml_arg(0), aml_int(5), "PRRI"));
543
456
    aml_append(method, aml_store(aml_name("PRRI"), reg));
544
456
    aml_append(dev, method);
545
546
456
    return dev;
547
456
 }
548
549
static Aml *build_gsi_link_dev(const char *name, uint8_t uid, uint8_t gsi)
550
424
{
551
424
    Aml *dev;
552
424
    Aml *crs;
553
424
    Aml *method;
554
424
    uint32_t irqs;
555
556
424
    dev = aml_device("%s", name);
557
424
    aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
558
424
    aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
559
560
424
    crs = aml_resource_template();
561
424
    irqs = gsi;
562
424
    aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL, AML_ACTIVE_HIGH,
563
424
                                  AML_SHARED, &irqs, 1));
564
424
    aml_append(dev, aml_name_decl("_PRS", crs));
565
566
424
    aml_append(dev, aml_name_decl("_CRS", crs));
567
568
    /*
569
     * _DIS can be no-op because the interrupt cannot be disabled.
570
     */
571
424
    method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
572
424
    aml_append(dev, method);
573
574
424
    method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
575
424
    aml_append(dev, method);
576
577
424
    return dev;
578
424
}
579
580
/* _CRS method - get current settings */
581
static Aml *build_iqcr_method(bool is_piix4)
582
61
{
583
61
    Aml *if_ctx;
584
61
    uint32_t irqs;
585
61
    Aml *method = aml_method("IQCR", 1, AML_SERIALIZED);
586
61
    Aml *crs = aml_resource_template();
587
588
61
    irqs = 0;
589
61
    aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
590
61
                                  AML_ACTIVE_HIGH, AML_SHARED, &irqs, 1));
591
61
    aml_append(method, aml_name_decl("PRR0", crs));
592
593
61
    aml_append(method,
594
61
        aml_create_dword_field(aml_name("PRR0"), aml_int(5), "PRRI"));
595
596
61
    if (is_piix4) {
597
8
        if_ctx = aml_if(aml_lless(aml_arg(0), aml_int(0x80)));
598
8
        aml_append(if_ctx, aml_store(aml_arg(0), aml_name("PRRI")));
599
8
        aml_append(method, if_ctx);
600
53
    } else {
601
53
        aml_append(method,
602
53
            aml_store(aml_and(aml_arg(0), aml_int(0xF), NULL),
603
53
                      aml_name("PRRI")));
604
53
    }
605
606
61
    aml_append(method, aml_return(aml_name("PRR0")));
607
61
    return method;
608
61
}
609
610
/* _STA method - get status */
611
static Aml *build_irq_status_method(void)
612
61
{
613
61
    Aml *if_ctx;
614
61
    Aml *method = aml_method("IQST", 1, AML_NOTSERIALIZED);
615
616
61
    if_ctx = aml_if(aml_and(aml_int(0x80), aml_arg(0), NULL));
617
61
    aml_append(if_ctx, aml_return(aml_int(0x09)));
618
61
    aml_append(method, if_ctx);
619
61
    aml_append(method, aml_return(aml_int(0x0B)));
620
61
    return method;
621
61
}
622
623
static void build_piix4_pci0_int(Aml *table)
624
8
{
625
8
    Aml *dev;
626
8
    Aml *crs;
627
8
    Aml *method;
628
8
    uint32_t irqs;
629
8
    Aml *sb_scope = aml_scope("_SB");
630
8
    Aml *pci0_scope = aml_scope("PCI0");
631
632
8
    aml_append(pci0_scope, build_prt(true));
633
8
    aml_append(sb_scope, pci0_scope);
634
635
8
    aml_append(sb_scope, build_irq_status_method());
636
8
    aml_append(sb_scope, build_iqcr_method(true));
637
638
8
    aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQ0")));
639
8
    aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQ1")));
640
8
    aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQ2")));
641
8
    aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQ3")));
642
643
8
    dev = aml_device("LNKS");
644
8
    {
645
8
        aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0C0F")));
646
8
        aml_append(dev, aml_name_decl("_UID", aml_int(4)));
647
648
8
        crs = aml_resource_template();
649
8
        irqs = 9;
650
8
        aml_append(crs, aml_interrupt(AML_CONSUMER, AML_LEVEL,
651
8
                                      AML_ACTIVE_HIGH, AML_SHARED,
652
8
                                      &irqs, 1));
653
8
        aml_append(dev, aml_name_decl("_PRS", crs));
654
655
        /* The SCI cannot be disabled and is always attached to GSI 9,
656
         * so these are no-ops.  We only need this link to override the
657
         * polarity to active high and match the content of the MADT.
658
         */
659
8
        method = aml_method("_STA", 0, AML_NOTSERIALIZED);
660
8
        aml_append(method, aml_return(aml_int(0x0b)));
661
8
        aml_append(dev, method);
662
663
8
        method = aml_method("_DIS", 0, AML_NOTSERIALIZED);
664
8
        aml_append(dev, method);
665
666
8
        method = aml_method("_CRS", 0, AML_NOTSERIALIZED);
667
8
        aml_append(method, aml_return(aml_name("_PRS")));
668
8
        aml_append(dev, method);
669
670
8
        method = aml_method("_SRS", 1, AML_NOTSERIALIZED);
671
8
        aml_append(dev, method);
672
8
    }
673
8
    aml_append(sb_scope, dev);
674
675
8
    aml_append(table, sb_scope);
676
8
}
677
678
static void append_q35_prt_entry(Aml *ctx, uint32_t nr, const char *name)
679
3.39k
{
680
3.39k
    int i;
681
3.39k
    int head;
682
3.39k
    Aml *pkg;
683
3.39k
    char base = name[3] < 'E' ? 'A' : 'E';
684
3.39k
    char *s = g_strdup(name);
685
3.39k
    Aml *a_nr = aml_int((nr << 16) | 0xffff);
686
687
3.39k
    assert(strlen(s) == 4);
688
689
3.39k
    head = name[3] - base;
690
16.9k
    for (i = 0; i < 4; i++) {
691
13.5k
        if (head + i > 3) {
692
1.90k
            head = i * -1;
693
1.90k
        }
694
13.5k
        s[3] = base + head + i;
695
13.5k
        pkg = aml_package(4);
696
13.5k
        aml_append(pkg, a_nr);
697
13.5k
        aml_append(pkg, aml_int(i));
698
13.5k
        aml_append(pkg, aml_name("%s", s));
699
13.5k
        aml_append(pkg, aml_int(0));
700
13.5k
        aml_append(ctx, pkg);
701
13.5k
    }
702
3.39k
    g_free(s);
703
3.39k
}
704
705
static Aml *build_q35_routing_table(const char *str)
706
106
{
707
106
    int i;
708
106
    Aml *pkg;
709
106
    char *name = g_strdup_printf("%s ", str);
710
711
106
    pkg = aml_package(128);
712
2.65k
    for (i = 0; i < 0x18; i++) {
713
2.54k
            name[3] = 'E' + (i & 0x3);
714
2.54k
            append_q35_prt_entry(pkg, i, name);
715
2.54k
    }
716
717
106
    name[3] = 'E';
718
106
    append_q35_prt_entry(pkg, 0x18, name);
719
720
    /* INTA -> PIRQA for slot 25 - 31, see the default value of D<N>IR */
721
636
    for (i = 0x0019; i < 0x1e; i++) {
722
530
        name[3] = 'A';
723
530
        append_q35_prt_entry(pkg, i, name);
724
530
    }
725
726
    /* PCIe->PCI bridge. use PIRQ[E-H] */
727
106
    name[3] = 'E';
728
106
    append_q35_prt_entry(pkg, 0x1e, name);
729
106
    name[3] = 'A';
730
106
    append_q35_prt_entry(pkg, 0x1f, name);
731
732
106
    g_free(name);
733
106
    return pkg;
734
106
}
735
736
static void build_q35_pci0_int(Aml *table)
737
53
{
738
53
    Aml *method;
739
53
    Aml *sb_scope = aml_scope("_SB");
740
53
    Aml *pci0_scope = aml_scope("PCI0");
741
742
    /* Zero => PIC mode, One => APIC Mode */
743
53
    aml_append(table, aml_name_decl("PICF", aml_int(0)));
744
53
    method = aml_method("_PIC", 1, AML_NOTSERIALIZED);
745
53
    {
746
53
        aml_append(method, aml_store(aml_arg(0), aml_name("PICF")));
747
53
    }
748
53
    aml_append(table, method);
749
750
53
    aml_append(pci0_scope,
751
53
        aml_name_decl("PRTP", build_q35_routing_table("LNK")));
752
53
    aml_append(pci0_scope,
753
53
        aml_name_decl("PRTA", build_q35_routing_table("GSI")));
754
755
53
    method = aml_method("_PRT", 0, AML_NOTSERIALIZED);
756
53
    {
757
53
        Aml *if_ctx;
758
53
        Aml *else_ctx;
759
760
        /* PCI IRQ routing table, example from ACPI 2.0a specification,
761
           section 6.2.8.1 */
762
        /* Note: we provide the same info as the PCI routing
763
           table of the Bochs BIOS */
764
53
        if_ctx = aml_if(aml_equal(aml_name("PICF"), aml_int(0)));
765
53
        aml_append(if_ctx, aml_return(aml_name("PRTP")));
766
53
        aml_append(method, if_ctx);
767
53
        else_ctx = aml_else();
768
53
        aml_append(else_ctx, aml_return(aml_name("PRTA")));
769
53
        aml_append(method, else_ctx);
770
53
    }
771
53
    aml_append(pci0_scope, method);
772
53
    aml_append(sb_scope, pci0_scope);
773
774
53
    aml_append(sb_scope, build_irq_status_method());
775
53
    aml_append(sb_scope, build_iqcr_method(false));
776
777
53
    aml_append(sb_scope, build_link_dev("LNKA", 0, aml_name("PRQA")));
778
53
    aml_append(sb_scope, build_link_dev("LNKB", 1, aml_name("PRQB")));
779
53
    aml_append(sb_scope, build_link_dev("LNKC", 2, aml_name("PRQC")));
780
53
    aml_append(sb_scope, build_link_dev("LNKD", 3, aml_name("PRQD")));
781
53
    aml_append(sb_scope, build_link_dev("LNKE", 4, aml_name("PRQE")));
782
53
    aml_append(sb_scope, build_link_dev("LNKF", 5, aml_name("PRQF")));
783
53
    aml_append(sb_scope, build_link_dev("LNKG", 6, aml_name("PRQG")));
784
53
    aml_append(sb_scope, build_link_dev("LNKH", 7, aml_name("PRQH")));
785
786
53
    aml_append(sb_scope, build_gsi_link_dev("GSIA", 0x10, 0x10));
787
53
    aml_append(sb_scope, build_gsi_link_dev("GSIB", 0x11, 0x11));
788
53
    aml_append(sb_scope, build_gsi_link_dev("GSIC", 0x12, 0x12));
789
53
    aml_append(sb_scope, build_gsi_link_dev("GSID", 0x13, 0x13));
790
53
    aml_append(sb_scope, build_gsi_link_dev("GSIE", 0x14, 0x14));
791
53
    aml_append(sb_scope, build_gsi_link_dev("GSIF", 0x15, 0x15));
792
53
    aml_append(sb_scope, build_gsi_link_dev("GSIG", 0x16, 0x16));
793
53
    aml_append(sb_scope, build_gsi_link_dev("GSIH", 0x17, 0x17));
794
795
53
    aml_append(table, sb_scope);
796
53
}
797
798
static Aml *build_q35_dram_controller(const AcpiMcfgInfo *mcfg)
799
0
{
800
0
    Aml *dev;
801
0
    Aml *resource_template;
802
803
    /* DRAM controller */
804
0
    dev = aml_device("DRAC");
805
0
    aml_append(dev, aml_name_decl("_HID", aml_string("PNP0C01")));
806
807
0
    resource_template = aml_resource_template();
808
0
    if (mcfg->base + mcfg->size - 1 >= (1ULL << 32)) {
809
0
        aml_append(resource_template,
810
0
                   aml_qword_memory(AML_POS_DECODE,
811
0
                                    AML_MIN_FIXED,
812
0
                                    AML_MAX_FIXED,
813
0
                                    AML_NON_CACHEABLE,
814
0
                                    AML_READ_WRITE,
815
0
                                    0x0000000000000000,
816
0
                                    mcfg->base,
817
0
                                    mcfg->base + mcfg->size - 1,
818
0
                                    0x0000000000000000,
819
0
                                    mcfg->size));
820
0
    } else {
821
0
        aml_append(resource_template,
822
0
                   aml_dword_memory(AML_POS_DECODE,
823
0
                                    AML_MIN_FIXED,
824
0
                                    AML_MAX_FIXED,
825
0
                                    AML_NON_CACHEABLE,
826
0
                                    AML_READ_WRITE,
827
0
                                    0x0000000000000000,
828
0
                                    mcfg->base,
829
0
                                    mcfg->base + mcfg->size - 1,
830
0
                                    0x0000000000000000,
831
0
                                    mcfg->size));
832
0
    }
833
0
    aml_append(dev, aml_name_decl("_CRS", resource_template));
834
835
0
    return dev;
836
0
}
837
838
static void build_acpi0017(Aml *table)
839
0
{
840
0
    Aml *dev, *scope, *method;
841
842
0
    scope =  aml_scope("_SB");
843
0
    dev = aml_device("CXLM");
844
0
    aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0017")));
845
846
0
    method = aml_method("_STA", 0, AML_NOTSERIALIZED);
847
0
    aml_append(method, aml_return(aml_int(0x0B)));
848
0
    aml_append(dev, method);
849
0
    build_cxl_dsm_method(dev);
850
851
0
    aml_append(scope, dev);
852
0
    aml_append(table, scope);
853
0
}
854
855
static void
856
build_dsdt(GArray *table_data, BIOSLinker *linker,
857
           AcpiPmInfo *pm, AcpiMiscInfo *misc,
858
           Range *pci_hole, Range *pci_hole64, MachineState *machine)
859
61
{
860
61
    Object *i440fx = object_resolve_type_unambiguous(TYPE_I440FX_PCI_HOST_BRIDGE,
861
61
                                                     NULL);
862
61
    Object *q35 = object_resolve_type_unambiguous(TYPE_Q35_HOST_DEVICE, NULL);
863
61
    CrsRangeEntry *entry;
864
61
    Aml *dsdt, *sb_scope, *scope, *dev, *method, *field, *pkg, *crs;
865
61
    CrsRangeSet crs_range_set;
866
61
    PCMachineState *pcms = PC_MACHINE(machine);
867
61
    PCMachineClass *pcmc = PC_MACHINE_GET_CLASS(machine);
868
61
    X86MachineState *x86ms = X86_MACHINE(machine);
869
61
    AcpiMcfgInfo mcfg;
870
61
    bool mcfg_valid = !!acpi_get_mcfg(&mcfg);
871
61
    uint32_t nr_mem = machine->ram_slots;
872
61
    int root_bus_limit = 0xFF;
873
61
    PCIBus *bus = NULL;
874
61
#ifdef CONFIG_TPM
875
61
    TPMIf *tpm = tpm_find();
876
61
#endif
877
61
    bool cxl_present = false;
878
61
    int i;
879
61
    VMBusBridge *vmbus_bridge = vmbus_bridge_find();
880
61
    AcpiTable table = { .sig = "DSDT", .rev = 1, .oem_id = x86ms->oem_id,
881
61
                        .oem_table_id = x86ms->oem_table_id };
882
883
61
    assert(!!i440fx != !!q35);
884
885
61
    acpi_table_begin(&table, table_data);
886
61
    dsdt = init_aml_allocator();
887
888
61
    build_dbg_aml(dsdt);
889
61
    if (i440fx) {
890
8
        sb_scope = aml_scope("_SB");
891
8
        dev = aml_device("PCI0");
892
8
        aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
893
8
        aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
894
8
        aml_append(dev, build_pci_bridge_edsm());
895
8
        aml_append(sb_scope, dev);
896
8
        aml_append(dsdt, sb_scope);
897
898
8
        if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
899
8
            build_acpi_pci_hotplug(dsdt, AML_SYSTEM_IO, pm->pcihp_io_base);
900
8
        }
901
8
        build_piix4_pci0_int(dsdt);
902
53
    } else if (q35) {
903
53
        sb_scope = aml_scope("_SB");
904
53
        dev = aml_device("PCI0");
905
53
        aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
906
53
        aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
907
53
        aml_append(dev, aml_name_decl("_UID", aml_int(pcmc->pci_root_uid)));
908
53
        aml_append(dev, build_pci_host_bridge_osc_method(!pm->pcihp_bridge_en));
909
53
        aml_append(dev, build_pci_bridge_edsm());
910
53
        aml_append(sb_scope, dev);
911
53
        if (mcfg_valid) {
912
0
            aml_append(sb_scope, build_q35_dram_controller(&mcfg));
913
0
        }
914
915
53
        if (pm->smi_on_cpuhp) {
916
            /* reserve SMI block resources, IO ports 0xB2, 0xB3 */
917
0
            dev = aml_device("PCI0.SMI0");
918
0
            aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A06")));
919
0
            aml_append(dev, aml_name_decl("_UID", aml_string("SMI resources")));
920
0
            crs = aml_resource_template();
921
0
            aml_append(crs,
922
0
                aml_io(
923
0
                       AML_DECODE16,
924
0
                       pm->fadt.smi_cmd,
925
0
                       pm->fadt.smi_cmd,
926
0
                       1,
927
0
                       2)
928
0
            );
929
0
            aml_append(dev, aml_name_decl("_CRS", crs));
930
0
            aml_append(dev, aml_operation_region("SMIR", AML_SYSTEM_IO,
931
0
                aml_int(pm->fadt.smi_cmd), 2));
932
0
            field = aml_field("SMIR", AML_BYTE_ACC, AML_NOLOCK,
933
0
                              AML_WRITE_AS_ZEROS);
934
0
            aml_append(field, aml_named_field("SMIC", 8));
935
0
            aml_append(field, aml_reserved_field(8));
936
0
            aml_append(dev, field);
937
0
            aml_append(sb_scope, dev);
938
0
        }
939
940
53
        aml_append(dsdt, sb_scope);
941
942
53
        if (pm->pcihp_bridge_en) {
943
53
            build_acpi_pci_hotplug(dsdt, AML_SYSTEM_IO, pm->pcihp_io_base);
944
53
        }
945
53
        build_q35_pci0_int(dsdt);
946
53
    }
947
948
61
    if (misc->has_hpet) {
949
61
        build_hpet_aml(dsdt);
950
61
    }
951
952
61
    if (vmbus_bridge) {
953
0
        sb_scope = aml_scope("_SB");
954
0
        aml_append(sb_scope, build_vmbus_device_aml(vmbus_bridge));
955
0
        aml_append(dsdt, sb_scope);
956
0
    }
957
958
61
    scope =  aml_scope("_GPE");
959
61
    {
960
61
        aml_append(scope, aml_name_decl("_HID", aml_string("ACPI0006")));
961
61
        if (machine->nvdimms_state->is_enabled) {
962
0
            method = aml_method("_E04", 0, AML_NOTSERIALIZED);
963
0
            aml_append(method, aml_notify(aml_name("\\_SB.NVDR"),
964
0
                                          aml_int(0x80)));
965
0
            aml_append(scope, method);
966
0
        }
967
61
    }
968
61
    aml_append(dsdt, scope);
969
970
61
    {
971
61
        CPUHotplugFeatures opts = {
972
61
            .acpi_1_compatible = true,
973
61
            .smi_path = pm->smi_on_cpuhp ? "\\_SB.PCI0.SMI0.SMIC" : NULL,
974
61
            .fw_unplugs_cpu = pm->smi_on_cpu_unplug,
975
61
        };
976
61
        build_cpus_aml(dsdt, machine, opts, pc_madt_cpu_entry,
977
61
                       pm->cpu_hp_io_base, "\\_SB.PCI0", "\\_GPE._E02",
978
61
                       AML_SYSTEM_IO);
979
61
    }
980
981
61
    if (pcms->memhp_io_base && nr_mem) {
982
0
        build_memory_hotplug_aml(dsdt, nr_mem, "\\_SB.PCI0",
983
0
                                 "\\_GPE._E03", AML_SYSTEM_IO,
984
0
                                 pcms->memhp_io_base);
985
0
    }
986
987
61
    crs_range_set_init(&crs_range_set);
988
61
    bus = PC_MACHINE(machine)->pcibus;
989
61
    if (bus) {
990
61
        QLIST_FOREACH(bus, &bus->child, sibling) {
991
0
            uint8_t bus_num = pci_bus_num(bus);
992
0
            uint8_t numa_node = pci_bus_numa_node(bus);
993
0
            uint32_t uid;
994
995
            /* look only for expander root buses */
996
0
            if (!pci_bus_is_root(bus)) {
997
0
                continue;
998
0
            }
999
1000
0
            if (bus_num < root_bus_limit) {
1001
0
                root_bus_limit = bus_num - 1;
1002
0
            }
1003
1004
0
            uid = object_property_get_uint(OBJECT(bus), "acpi_uid",
1005
0
                                           &error_fatal);
1006
0
            scope = aml_scope("\\_SB");
1007
1008
0
            if (pci_bus_is_cxl(bus)) {
1009
0
                dev = aml_device("CL%.02X", bus_num);
1010
0
            } else {
1011
0
                dev = aml_device("PC%.02X", bus_num);
1012
0
            }
1013
0
            aml_append(dev, aml_name_decl("_UID", aml_int(uid)));
1014
0
            aml_append(dev, aml_name_decl("_BBN", aml_int(bus_num)));
1015
0
            if (pci_bus_is_cxl(bus)) {
1016
0
                struct Aml *aml_pkg = aml_package(2);
1017
1018
0
                aml_append(dev, aml_name_decl("_HID", aml_string("ACPI0016")));
1019
0
                aml_append(aml_pkg, aml_eisaid("PNP0A08"));
1020
0
                aml_append(aml_pkg, aml_eisaid("PNP0A03"));
1021
0
                aml_append(dev, aml_name_decl("_CID", aml_pkg));
1022
0
                build_cxl_osc_method(dev);
1023
0
            } else if (pci_bus_is_express(bus)) {
1024
0
                aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A08")));
1025
0
                aml_append(dev, aml_name_decl("_CID", aml_eisaid("PNP0A03")));
1026
1027
                /* Expander bridges do not have ACPI PCI Hot-plug enabled */
1028
0
                aml_append(dev, build_pci_host_bridge_osc_method(true));
1029
0
            } else {
1030
0
                aml_append(dev, aml_name_decl("_HID", aml_eisaid("PNP0A03")));
1031
0
            }
1032
1033
0
            if (numa_node != NUMA_NODE_UNASSIGNED) {
1034
0
                aml_append(dev, aml_name_decl("_PXM", aml_int(numa_node)));
1035
0
            }
1036
1037
0
            aml_append(dev, build_prt(false));
1038
0
            crs = build_crs(PCI_HOST_BRIDGE(BUS(bus)->parent), &crs_range_set,
1039
0
                            0, 0, 0, 0);
1040
0
            aml_append(dev, aml_name_decl("_CRS", crs));
1041
0
            aml_append(scope, dev);
1042
0
            aml_append(dsdt, scope);
1043
1044
            /* Handle the ranges for the PXB expanders */
1045
0
            if (pci_bus_is_cxl(bus)) {
1046
0
                MemoryRegion *mr = &pcms->cxl_devices_state.host_mr;
1047
0
                uint64_t base = mr->addr;
1048
1049
0
                cxl_present = true;
1050
0
                crs_range_insert(crs_range_set.mem_ranges, base,
1051
0
                                 base + memory_region_size(mr) - 1);
1052
0
            }
1053
0
        }
1054
61
    }
1055
1056
61
    if (cxl_present) {
1057
0
        build_acpi0017(dsdt);
1058
0
    }
1059
1060
    /*
1061
     * At this point crs_range_set has all the ranges used by pci
1062
     * busses *other* than PCI0.  These ranges will be excluded from
1063
     * the PCI0._CRS.  Add mmconfig to the set so it will be excluded
1064
     * too.
1065
     */
1066
61
    if (mcfg_valid) {
1067
0
        crs_range_insert(crs_range_set.mem_ranges,
1068
0
                         mcfg.base, mcfg.base + mcfg.size - 1);
1069
0
    }
1070
1071
61
    scope = aml_scope("\\_SB.PCI0");
1072
    /* build PCI0._CRS */
1073
61
    crs = aml_resource_template();
1074
61
    aml_append(crs,
1075
61
        aml_word_bus_number(AML_MIN_FIXED, AML_MAX_FIXED, AML_POS_DECODE,
1076
61
                            0x0000, 0x0, root_bus_limit,
1077
61
                            0x0000, root_bus_limit + 1));
1078
61
    aml_append(crs, aml_io(AML_DECODE16, 0x0CF8, 0x0CF8, 0x01, 0x08));
1079
1080
61
    aml_append(crs,
1081
61
        aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1082
61
                    AML_POS_DECODE, AML_ENTIRE_RANGE,
1083
61
                    0x0000, 0x0000, 0x0CF7, 0x0000, 0x0CF8));
1084
1085
61
    crs_replace_with_free_ranges(crs_range_set.io_ranges, 0x0D00, 0xFFFF);
1086
122
    for (i = 0; i < crs_range_set.io_ranges->len; i++) {
1087
61
        entry = g_ptr_array_index(crs_range_set.io_ranges, i);
1088
61
        aml_append(crs,
1089
61
            aml_word_io(AML_MIN_FIXED, AML_MAX_FIXED,
1090
61
                        AML_POS_DECODE, AML_ENTIRE_RANGE,
1091
61
                        0x0000, entry->base, entry->limit,
1092
61
                        0x0000, entry->limit - entry->base + 1));
1093
61
    }
1094
1095
61
    aml_append(crs,
1096
61
        aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1097
61
                         AML_CACHEABLE, AML_READ_WRITE,
1098
61
                         0, 0x000A0000, 0x000BFFFF, 0, 0x00020000));
1099
1100
61
    crs_replace_with_free_ranges(crs_range_set.mem_ranges,
1101
61
                                 range_lob(pci_hole),
1102
61
                                 range_upb(pci_hole));
1103
69
    for (i = 0; i < crs_range_set.mem_ranges->len; i++) {
1104
8
        entry = g_ptr_array_index(crs_range_set.mem_ranges, i);
1105
8
        aml_append(crs,
1106
8
            aml_dword_memory(AML_POS_DECODE, AML_MIN_FIXED, AML_MAX_FIXED,
1107
8
                             AML_NON_CACHEABLE, AML_READ_WRITE,
1108
8
                             0, entry->base, entry->limit,
1109
8
                             0, entry->limit - entry->base + 1));
1110
8
    }
1111
1112
61
    if (!range_is_empty(pci_hole64)) {
1113
61
        crs_replace_with_free_ranges(crs_range_set.mem_64bit_ranges,
1114
61
                                     range_lob(pci_hole64),
1115
61
                                     range_upb(pci_hole64));
1116
122
        for (i = 0; i < crs_range_set.mem_64bit_ranges->len; i++) {
1117
61
            entry = g_ptr_array_index(crs_range_set.mem_64bit_ranges, i);
1118
61
            aml_append(crs,
1119
61
                       aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1120
61
                                        AML_MAX_FIXED,
1121
61
                                        AML_CACHEABLE, AML_READ_WRITE,
1122
61
                                        0, entry->base, entry->limit,
1123
61
                                        0, entry->limit - entry->base + 1));
1124
61
        }
1125
61
    }
1126
1127
61
#ifdef CONFIG_TPM
1128
61
    if (TPM_IS_TIS_ISA(tpm_find())) {
1129
0
        aml_append(crs, aml_memory32_fixed(TPM_TIS_ADDR_BASE,
1130
0
                   TPM_TIS_ADDR_SIZE, AML_READ_WRITE));
1131
0
    }
1132
61
#endif
1133
61
    aml_append(scope, aml_name_decl("_CRS", crs));
1134
1135
    /* reserve GPE0 block resources */
1136
61
    dev = aml_device("GPE0");
1137
61
    aml_append(dev, aml_name_decl("_HID", aml_string("PNP0A06")));
1138
61
    aml_append(dev, aml_name_decl("_UID", aml_string("GPE0 resources")));
1139
    /* device present, functioning, decoding, not shown in UI */
1140
61
    aml_append(dev, aml_name_decl("_STA", aml_int(0xB)));
1141
61
    crs = aml_resource_template();
1142
61
    aml_append(crs,
1143
61
        aml_io(
1144
61
               AML_DECODE16,
1145
61
               pm->fadt.gpe0_blk.address,
1146
61
               pm->fadt.gpe0_blk.address,
1147
61
               1,
1148
61
               pm->fadt.gpe0_blk.bit_width / 8)
1149
61
    );
1150
61
    aml_append(dev, aml_name_decl("_CRS", crs));
1151
61
    aml_append(scope, dev);
1152
1153
61
    crs_range_set_free(&crs_range_set);
1154
1155
    /* reserve PCIHP resources */
1156
61
    if (pm->pcihp_io_len && (pm->pcihp_bridge_en || pm->pcihp_root_en)) {
1157
61
        build_append_pcihp_resources(scope,
1158
61
                                      pm->pcihp_io_base, pm->pcihp_io_len);
1159
61
    }
1160
61
    aml_append(dsdt, scope);
1161
1162
    /*  create S3_ / S4_ / S5_ packages if necessary */
1163
61
    scope = aml_scope("\\");
1164
61
    if (!pm->s3_disabled) {
1165
61
        pkg = aml_package(4);
1166
61
        aml_append(pkg, aml_int(1)); /* PM1a_CNT.SLP_TYP */
1167
61
        aml_append(pkg, aml_int(1)); /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1168
61
        aml_append(pkg, aml_int(0)); /* reserved */
1169
61
        aml_append(pkg, aml_int(0)); /* reserved */
1170
61
        aml_append(scope, aml_name_decl("_S3", pkg));
1171
61
    }
1172
1173
61
    if (!pm->s4_disabled) {
1174
61
        pkg = aml_package(4);
1175
61
        aml_append(pkg, aml_int(pm->s4_val)); /* PM1a_CNT.SLP_TYP */
1176
        /* PM1b_CNT.SLP_TYP, FIXME: not impl. */
1177
61
        aml_append(pkg, aml_int(pm->s4_val));
1178
61
        aml_append(pkg, aml_int(0)); /* reserved */
1179
61
        aml_append(pkg, aml_int(0)); /* reserved */
1180
61
        aml_append(scope, aml_name_decl("_S4", pkg));
1181
61
    }
1182
1183
61
    pkg = aml_package(4);
1184
61
    aml_append(pkg, aml_int(0)); /* PM1a_CNT.SLP_TYP */
1185
61
    aml_append(pkg, aml_int(0)); /* PM1b_CNT.SLP_TYP not impl. */
1186
61
    aml_append(pkg, aml_int(0)); /* reserved */
1187
61
    aml_append(pkg, aml_int(0)); /* reserved */
1188
61
    aml_append(scope, aml_name_decl("_S5", pkg));
1189
61
    aml_append(dsdt, scope);
1190
1191
    /* create fw_cfg node, unconditionally */
1192
61
    {
1193
61
        scope = aml_scope("\\_SB.PCI0");
1194
61
        fw_cfg_add_acpi_dsdt(scope, x86ms->fw_cfg);
1195
61
        aml_append(dsdt, scope);
1196
61
    }
1197
1198
61
    sb_scope = aml_scope("\\_SB");
1199
61
    {
1200
61
        Object *pci_host = acpi_get_i386_pci_host();
1201
61
        uint32_t bsel;
1202
1203
61
        if (pci_host) {
1204
61
            PCIBus *pbus = PCI_HOST_BRIDGE(pci_host)->bus;
1205
61
            Aml *ascope = aml_scope("PCI0");
1206
            /* Scan all PCI buses. Generate tables to support hotplug. */
1207
61
            build_append_pci_bus_devices(ascope, pbus);
1208
61
            bsel = object_property_get_uint(OBJECT(pbus), ACPI_PCIHP_PROP_BSEL,
1209
61
                                            &error_abort);
1210
61
            if (bsel != UINT32_MAX) {
1211
0
                build_append_pcihp_slots(ascope, pbus);
1212
0
            }
1213
61
            aml_append(sb_scope, ascope);
1214
61
        }
1215
61
    }
1216
1217
61
#ifdef CONFIG_TPM
1218
61
    if (TPM_IS_CRB(tpm)) {
1219
0
        dev = aml_device("TPM");
1220
0
        aml_append(dev, aml_name_decl("_HID", aml_string("MSFT0101")));
1221
0
        aml_append(dev, aml_name_decl("_STR",
1222
0
                                      aml_string("TPM 2.0 Device")));
1223
0
        crs = aml_resource_template();
1224
0
        aml_append(crs, aml_memory32_fixed(TPM_CRB_ADDR_BASE,
1225
0
                                           TPM_CRB_ADDR_SIZE, AML_READ_WRITE));
1226
0
        aml_append(dev, aml_name_decl("_CRS", crs));
1227
1228
0
        aml_append(dev, aml_name_decl("_STA", aml_int(0xf)));
1229
0
        aml_append(dev, aml_name_decl("_UID", aml_int(1)));
1230
1231
0
        tpm_build_ppi_acpi(tpm, dev, TPM_PPI_ADDR_BASE);
1232
1233
0
        aml_append(sb_scope, dev);
1234
0
    }
1235
61
#endif
1236
1237
61
    if (pcms->sgx_epc.size != 0) {
1238
0
        uint64_t epc_base = pcms->sgx_epc.base;
1239
0
        uint64_t epc_size = pcms->sgx_epc.size;
1240
1241
0
        dev = aml_device("EPC");
1242
0
        aml_append(dev, aml_name_decl("_HID", aml_eisaid("INT0E0C")));
1243
0
        aml_append(dev, aml_name_decl("_STR",
1244
0
                                      aml_unicode("Enclave Page Cache 1.0")));
1245
0
        crs = aml_resource_template();
1246
0
        aml_append(crs,
1247
0
                   aml_qword_memory(AML_POS_DECODE, AML_MIN_FIXED,
1248
0
                                    AML_MAX_FIXED, AML_NON_CACHEABLE,
1249
0
                                    AML_READ_WRITE, 0, epc_base,
1250
0
                                    epc_base + epc_size - 1, 0, epc_size));
1251
0
        aml_append(dev, aml_name_decl("_CRS", crs));
1252
1253
0
        method = aml_method("_STA", 0, AML_NOTSERIALIZED);
1254
0
        aml_append(method, aml_return(aml_int(0x0f)));
1255
0
        aml_append(dev, method);
1256
1257
0
        aml_append(sb_scope, dev);
1258
0
    }
1259
61
    aml_append(dsdt, sb_scope);
1260
1261
61
    if (pm->pcihp_bridge_en || pm->pcihp_root_en) {
1262
61
        bool has_pcnt;
1263
1264
61
        Object *pci_host = acpi_get_i386_pci_host();
1265
61
        PCIBus *b = PCI_HOST_BRIDGE(pci_host)->bus;
1266
1267
61
        scope = aml_scope("\\_SB.PCI0");
1268
61
        has_pcnt = build_append_notification_callback(scope, b);
1269
61
        if (has_pcnt) {
1270
0
            aml_append(dsdt, scope);
1271
0
        }
1272
1273
61
        scope =  aml_scope("_GPE");
1274
61
        {
1275
61
            method = aml_method("_E01", 0, AML_NOTSERIALIZED);
1276
61
            if (has_pcnt) {
1277
0
                aml_append(method,
1278
0
                    aml_acquire(aml_name("\\_SB.PCI0.BLCK"), 0xFFFF));
1279
0
                aml_append(method, aml_call0("\\_SB.PCI0.PCNT"));
1280
0
                aml_append(method, aml_release(aml_name("\\_SB.PCI0.BLCK")));
1281
0
            }
1282
61
            aml_append(scope, method);
1283
61
        }
1284
61
        aml_append(dsdt, scope);
1285
61
    }
1286
1287
    /* copy AML table into ACPI tables blob and patch header there */
1288
61
    g_array_append_vals(table_data, dsdt->buf->data, dsdt->buf->len);
1289
61
    acpi_table_end(linker, &table);
1290
61
    free_aml_allocator();
1291
61
}
1292
1293
/*
1294
 * IA-PC HPET (High Precision Event Timers) Specification (Revision: 1.0a)
1295
 * 3.2.4The ACPI 2.0 HPET Description Table (HPET)
1296
 */
1297
static void
1298
build_hpet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1299
           const char *oem_table_id)
1300
61
{
1301
61
    AcpiTable table = { .sig = "HPET", .rev = 1,
1302
61
                        .oem_id = oem_id, .oem_table_id = oem_table_id };
1303
1304
61
    acpi_table_begin(&table, table_data);
1305
    /* Note timer_block_id value must be kept in sync with value advertised by
1306
     * emulated hpet
1307
     */
1308
    /* Event Timer Block ID */
1309
61
    build_append_int_noprefix(table_data, 0x8086a201, 4);
1310
    /* BASE_ADDRESS */
1311
61
    build_append_gas(table_data, AML_AS_SYSTEM_MEMORY, 0, 0, 0, HPET_BASE);
1312
    /* HPET Number */
1313
61
    build_append_int_noprefix(table_data, 0, 1);
1314
    /* Main Counter Minimum Clock_tick in Periodic Mode */
1315
61
    build_append_int_noprefix(table_data, 0, 2);
1316
    /* Page Protection And OEM Attribute */
1317
61
    build_append_int_noprefix(table_data, 0, 1);
1318
61
    acpi_table_end(linker, &table);
1319
61
}
1320
1321
#ifdef CONFIG_TPM
1322
/*
1323
 * TCPA Description Table
1324
 *
1325
 * Following Level 00, Rev 00.37 of specs:
1326
 * http://www.trustedcomputinggroup.org/resources/tcg_acpi_specification
1327
 * 7.1.2 ACPI Table Layout
1328
 */
1329
static void
1330
build_tpm_tcpa(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
1331
               const char *oem_id, const char *oem_table_id)
1332
0
{
1333
0
    unsigned log_addr_offset;
1334
0
    AcpiTable table = { .sig = "TCPA", .rev = 2,
1335
0
                        .oem_id = oem_id, .oem_table_id = oem_table_id };
1336
1337
0
    acpi_table_begin(&table, table_data);
1338
    /* Platform Class */
1339
0
    build_append_int_noprefix(table_data, TPM_TCPA_ACPI_CLASS_CLIENT, 2);
1340
    /* Log Area Minimum Length (LAML) */
1341
0
    build_append_int_noprefix(table_data, TPM_LOG_AREA_MINIMUM_SIZE, 4);
1342
    /* Log Area Start Address (LASA) */
1343
0
    log_addr_offset = table_data->len;
1344
0
    build_append_int_noprefix(table_data, 0, 8);
1345
1346
    /* allocate/reserve space for TPM log area */
1347
0
    acpi_data_push(tcpalog, TPM_LOG_AREA_MINIMUM_SIZE);
1348
0
    bios_linker_loader_alloc(linker, ACPI_BUILD_TPMLOG_FILE, tcpalog, 1,
1349
0
                             false /* high memory */);
1350
    /* log area start address to be filled by Guest linker */
1351
0
    bios_linker_loader_add_pointer(linker, ACPI_BUILD_TABLE_FILE,
1352
0
        log_addr_offset, 8, ACPI_BUILD_TPMLOG_FILE, 0);
1353
1354
0
    acpi_table_end(linker, &table);
1355
0
}
1356
#endif
1357
1358
typedef struct {
1359
    uint64_t addr;
1360
    uint64_t size;
1361
    uint32_t node;
1362
} SpMemRange;
1363
1364
static int sp_mem_collect_ranges_cb(Object *obj, void *opaque)
1365
0
{
1366
0
    GArray *ranges = opaque;
1367
0
    SpMemDevice *spm;
1368
0
    MemoryDeviceClass *mdc;
1369
0
    SpMemRange r;
1370
1371
0
    if (!object_dynamic_cast(obj, TYPE_SP_MEM)) {
1372
0
        return 0;
1373
0
    }
1374
0
    spm = SP_MEM(obj);
1375
0
    mdc = MEMORY_DEVICE_GET_CLASS(MEMORY_DEVICE(spm));
1376
0
    r.addr = mdc->get_addr(MEMORY_DEVICE(spm));
1377
0
    r.size = memory_region_size(
1378
0
                 host_memory_backend_get_memory(spm->hostmem));
1379
0
    r.node = spm->node;
1380
0
    g_array_append_val(ranges, r);
1381
0
    return 0;
1382
0
}
1383
1384
static gint sp_mem_range_compare(gconstpointer a, gconstpointer b)
1385
0
{
1386
0
    const SpMemRange *range_a = a;
1387
0
    const SpMemRange *range_b = b;
1388
1389
0
    if (range_a->addr < range_b->addr) {
1390
0
        return -1;
1391
0
    }
1392
0
    if (range_a->addr > range_b->addr) {
1393
0
        return 1;
1394
0
    }
1395
0
    return 0;
1396
0
}
1397
1398
/*
1399
 * Emit SRAT memory-affinity entries covering the device_memory region.
1400
 *
1401
 * For each plugged TYPE_SP_MEM device, emit an ENABLED entry at the
1402
 * device's own proximity_domain.  All remaining sub-ranges (gaps
1403
 * between sp-mem devices, leading and trailing padding, and ranges
1404
 * occupied by other memory devices) are covered by HOTPLUGGABLE |
1405
 * ENABLED placeholder entries at PXM = nb_numa_nodes - 1.
1406
 */
1407
static void build_srat_device_memory(GArray *table_data, MachineState *ms)
1408
0
{
1409
0
    g_autoptr(GArray) ranges = g_array_new(FALSE, TRUE, sizeof(SpMemRange));
1410
0
    uint32_t hotplug_pxm = ms->numa_state->num_nodes - 1;
1411
0
    uint64_t region_start, region_end;
1412
0
    guint i;
1413
1414
0
    region_start = ms->device_memory->base;
1415
0
    region_end = region_start + memory_region_size(&ms->device_memory->mr);
1416
1417
0
    object_child_foreach_recursive(qdev_get_machine(),
1418
0
                                   sp_mem_collect_ranges_cb, ranges);
1419
0
    g_array_sort(ranges, sp_mem_range_compare);
1420
1421
0
    for (i = 0; i < ranges->len; i++) {
1422
0
        SpMemRange *r = &g_array_index(ranges, SpMemRange, i);
1423
1424
0
        if (region_start < r->addr) {
1425
0
            build_srat_memory(table_data, region_start, r->addr - region_start,
1426
0
                              hotplug_pxm,
1427
0
                              MEM_AFFINITY_HOTPLUGGABLE |
1428
0
                              MEM_AFFINITY_ENABLED);
1429
0
        }
1430
0
        build_srat_memory(table_data, r->addr, r->size, r->node,
1431
0
                          MEM_AFFINITY_ENABLED);
1432
0
        region_start = r->addr + r->size;
1433
0
    }
1434
1435
    /*
1436
     * Cover the rest of the device_memory window that no sp-mem device
1437
     * occupies. Keeping it HOTPLUGGABLE preserves the umbrella entry's
1438
     * role for future pc-dimm / virtio-mem hot-add into this window.
1439
     */
1440
0
    if (region_start < region_end) {
1441
0
        build_srat_memory(table_data, region_start, region_end - region_start,
1442
0
                          hotplug_pxm,
1443
0
                          MEM_AFFINITY_HOTPLUGGABLE |
1444
0
                          MEM_AFFINITY_ENABLED);
1445
0
    }
1446
0
}
1447
1448
0
#define HOLE_640K_START  (640 * KiB)
1449
0
#define HOLE_640K_END   (1 * MiB)
1450
1451
/*
1452
 * ACPI spec, Revision 3.0
1453
 * 5.2.15 System Resource Affinity Table (SRAT)
1454
 */
1455
static void
1456
build_srat(GArray *table_data, BIOSLinker *linker, MachineState *machine)
1457
0
{
1458
0
    int i;
1459
0
    int numa_mem_start, slots;
1460
0
    uint64_t mem_len, mem_base, next_base;
1461
0
    MachineClass *mc = MACHINE_GET_CLASS(machine);
1462
0
    X86MachineState *x86ms = X86_MACHINE(machine);
1463
0
    const CPUArchIdList *apic_ids = mc->possible_cpu_arch_ids(machine);
1464
0
    int nb_numa_nodes = machine->numa_state->num_nodes;
1465
0
    NodeInfo *numa_info = machine->numa_state->nodes;
1466
0
    AcpiTable table = { .sig = "SRAT", .rev = 1, .oem_id = x86ms->oem_id,
1467
0
                        .oem_table_id = x86ms->oem_table_id };
1468
1469
0
    acpi_table_begin(&table, table_data);
1470
0
    build_append_int_noprefix(table_data, 1, 4); /* Reserved */
1471
0
    build_append_int_noprefix(table_data, 0, 8); /* Reserved */
1472
1473
0
    for (i = 0; i < apic_ids->len; i++) {
1474
0
        int node_id = apic_ids->cpus[i].props.node_id;
1475
0
        uint32_t apic_id = apic_ids->cpus[i].arch_id;
1476
1477
0
        if (apic_id < 255) {
1478
            /* 5.2.15.1 Processor Local APIC/SAPIC Affinity Structure */
1479
0
            build_append_int_noprefix(table_data, 0, 1);  /* Type  */
1480
0
            build_append_int_noprefix(table_data, 16, 1); /* Length */
1481
            /* Proximity Domain [7:0] */
1482
0
            build_append_int_noprefix(table_data, node_id, 1);
1483
0
            build_append_int_noprefix(table_data, apic_id, 1); /* APIC ID */
1484
            /* Flags, Table 5-36 */
1485
0
            build_append_int_noprefix(table_data, 1, 4);
1486
0
            build_append_int_noprefix(table_data, 0, 1); /* Local SAPIC EID */
1487
            /* Proximity Domain [31:8] */
1488
0
            build_append_int_noprefix(table_data, 0, 3);
1489
0
            build_append_int_noprefix(table_data, 0, 4); /* Reserved */
1490
0
        } else {
1491
            /*
1492
             * ACPI spec, Revision 4.0
1493
             * 5.2.16.3 Processor Local x2APIC Affinity Structure
1494
             */
1495
0
            build_append_int_noprefix(table_data, 2, 1);  /* Type  */
1496
0
            build_append_int_noprefix(table_data, 24, 1); /* Length */
1497
0
            build_append_int_noprefix(table_data, 0, 2); /* Reserved */
1498
            /* Proximity Domain */
1499
0
            build_append_int_noprefix(table_data, node_id, 4);
1500
0
            build_append_int_noprefix(table_data, apic_id, 4); /* X2APIC ID */
1501
            /* Flags, Table 5-39 */
1502
0
            build_append_int_noprefix(table_data, 1 /* Enabled */, 4);
1503
0
            build_append_int_noprefix(table_data, 0, 4); /* Clock Domain */
1504
0
            build_append_int_noprefix(table_data, 0, 4); /* Reserved */
1505
0
        }
1506
0
    }
1507
1508
    /* the memory map is a bit tricky, it contains at least one hole
1509
     * from 640k-1M and possibly another one from 3.5G-4G.
1510
     */
1511
0
    next_base = 0;
1512
0
    numa_mem_start = table_data->len;
1513
1514
0
    for (i = 1; i < nb_numa_nodes + 1; ++i) {
1515
0
        mem_base = next_base;
1516
0
        mem_len = numa_info[i - 1].node_mem;
1517
0
        next_base = mem_base + mem_len;
1518
1519
        /* Cut out the 640K hole */
1520
0
        if (mem_base <= HOLE_640K_START &&
1521
0
            next_base > HOLE_640K_START) {
1522
0
            mem_len -= next_base - HOLE_640K_START;
1523
0
            if (mem_len > 0) {
1524
0
                build_srat_memory(table_data, mem_base, mem_len, i - 1,
1525
0
                                  MEM_AFFINITY_ENABLED);
1526
0
            }
1527
1528
            /* Check for the rare case: 640K < RAM < 1M */
1529
0
            if (next_base <= HOLE_640K_END) {
1530
0
                next_base = HOLE_640K_END;
1531
0
                continue;
1532
0
            }
1533
0
            mem_base = HOLE_640K_END;
1534
0
            mem_len = next_base - HOLE_640K_END;
1535
0
        }
1536
1537
        /* Cut out the ACPI_PCI hole */
1538
0
        if (mem_base <= x86ms->below_4g_mem_size &&
1539
0
            next_base > x86ms->below_4g_mem_size) {
1540
0
            mem_len -= next_base - x86ms->below_4g_mem_size;
1541
0
            if (mem_len > 0) {
1542
0
                build_srat_memory(table_data, mem_base, mem_len, i - 1,
1543
0
                                  MEM_AFFINITY_ENABLED);
1544
0
            }
1545
0
            mem_base = x86ms->above_4g_mem_start;
1546
0
            mem_len = next_base - x86ms->below_4g_mem_size;
1547
0
            next_base = mem_base + mem_len;
1548
0
        }
1549
1550
0
        if (mem_len > 0) {
1551
0
            build_srat_memory(table_data, mem_base, mem_len, i - 1,
1552
0
                              MEM_AFFINITY_ENABLED);
1553
0
        }
1554
0
    }
1555
1556
0
    if (machine->nvdimms_state->is_enabled) {
1557
0
        nvdimm_build_srat(table_data);
1558
0
    }
1559
1560
0
    sgx_epc_build_srat(table_data);
1561
1562
    /*
1563
     * TODO: this part is not in ACPI spec and current linux kernel boots fine
1564
     * without these entries. But I recall there were issues the last time I
1565
     * tried to remove it with some ancient guest OS, however I can't remember
1566
     * what that was so keep this around for now
1567
     */
1568
0
    slots = (table_data->len - numa_mem_start) / 40 /* mem affinity len */;
1569
0
    for (; slots < nb_numa_nodes + 2; slots++) {
1570
0
        build_srat_memory(table_data, 0, 0, 0, MEM_AFFINITY_NOFLAGS);
1571
0
    }
1572
1573
0
    build_srat_generic_affinity_structures(table_data);
1574
1575
    /*
1576
     * Entry is required for Windows to enable memory hotplug in OS
1577
     * and for Linux to enable SWIOTLB when booted with less than
1578
     * 4G of RAM. Windows works better if the entry sets proximity
1579
     * to the highest NUMA node in the machine.
1580
     * Memory devices may override proximity set by this entry,
1581
     * providing _PXM method if necessary.
1582
     */
1583
0
    if (machine->device_memory) {
1584
0
        build_srat_device_memory(table_data, machine);
1585
0
    }
1586
1587
0
    acpi_table_end(linker, &table);
1588
0
}
1589
1590
/*
1591
 * Insert DMAR scope for PCI bridges and endpoint devices
1592
 */
1593
static void
1594
insert_scope(PCIBus *bus, PCIDevice *dev, void *opaque)
1595
0
{
1596
0
    const size_t device_scope_size = 6 /* device scope structure */ +
1597
0
                                     2 /* 1 path entry */;
1598
0
    GArray *scope_blob = opaque;
1599
1600
0
    if (object_dynamic_cast(OBJECT(dev), TYPE_PCI_BRIDGE)) {
1601
        /* Dmar Scope Type: 0x02 for PCI Bridge */
1602
0
        build_append_int_noprefix(scope_blob, 0x02, 1);
1603
0
    } else {
1604
        /* Dmar Scope Type: 0x01 for PCI Endpoint Device */
1605
0
        build_append_int_noprefix(scope_blob, 0x01, 1);
1606
0
    }
1607
1608
    /* length */
1609
0
    build_append_int_noprefix(scope_blob, device_scope_size, 1);
1610
    /* reserved */
1611
0
    build_append_int_noprefix(scope_blob, 0, 2);
1612
    /* enumeration_id */
1613
0
    build_append_int_noprefix(scope_blob, 0, 1);
1614
    /* bus */
1615
0
    build_append_int_noprefix(scope_blob, pci_bus_num(bus), 1);
1616
    /* device */
1617
0
    build_append_int_noprefix(scope_blob, PCI_SLOT(dev->devfn), 1);
1618
    /* function */
1619
0
    build_append_int_noprefix(scope_blob, PCI_FUNC(dev->devfn), 1);
1620
0
}
1621
1622
/* For a given PCI host bridge, walk and insert DMAR scope */
1623
static int
1624
dmar_host_bridges(Object *obj, void *opaque)
1625
0
{
1626
0
    GArray *scope_blob = opaque;
1627
1628
0
    if (object_dynamic_cast(obj, TYPE_PCI_HOST_BRIDGE)) {
1629
0
        PCIBus *bus = PCI_HOST_BRIDGE(obj)->bus;
1630
1631
0
        if (bus && !pci_bus_bypass_iommu(bus)) {
1632
0
            pci_for_each_device_under_bus(bus, insert_scope, scope_blob);
1633
0
        }
1634
0
    }
1635
1636
0
    return 0;
1637
0
}
1638
1639
/*
1640
 * Intel ® Virtualization Technology for Directed I/O
1641
 * Architecture Specification. Revision 3.3
1642
 * 8.1 DMA Remapping Reporting Structure
1643
 */
1644
static void
1645
build_dmar_q35(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1646
               const char *oem_table_id)
1647
0
{
1648
0
    uint8_t dmar_flags = 0;
1649
0
    uint8_t rsvd10[10] = {};
1650
    /* Root complex IOAPIC uses one path only */
1651
0
    const size_t ioapic_scope_size = 6 /* device scope structure */ +
1652
0
                                     2 /* 1 path entry */;
1653
0
    X86IOMMUState *iommu = x86_iommu_get_default();
1654
0
    IntelIOMMUState *intel_iommu = INTEL_IOMMU_DEVICE(iommu);
1655
0
    GArray *scope_blob = g_array_new(false, true, 1);
1656
1657
0
    AcpiTable table = { .sig = "DMAR", .rev = 1, .oem_id = oem_id,
1658
0
                        .oem_table_id = oem_table_id };
1659
1660
    /*
1661
     * A PCI bus walk, for each PCI host bridge.
1662
     * Insert scope for each PCI bridge and endpoint device which
1663
     * is attached to a bus with iommu enabled.
1664
     */
1665
0
    object_child_foreach_recursive(object_get_root(),
1666
0
                                   dmar_host_bridges, scope_blob);
1667
1668
0
    assert(iommu);
1669
0
    if (x86_iommu_ir_supported(iommu)) {
1670
0
        dmar_flags |= 0x1;      /* Flags: 0x1: INT_REMAP */
1671
0
    }
1672
1673
0
    acpi_table_begin(&table, table_data);
1674
    /* Host Address Width */
1675
0
    build_append_int_noprefix(table_data, intel_iommu->aw_bits - 1, 1);
1676
0
    build_append_int_noprefix(table_data, dmar_flags, 1); /* Flags */
1677
0
    g_array_append_vals(table_data, rsvd10, sizeof(rsvd10)); /* Reserved */
1678
1679
    /* 8.3 DMAR Remapping Hardware Unit Definition structure */
1680
0
    build_append_int_noprefix(table_data, 0, 2); /* Type */
1681
    /* Length */
1682
0
    build_append_int_noprefix(table_data,
1683
0
                              16 + ioapic_scope_size + scope_blob->len, 2);
1684
    /* Flags */
1685
0
    build_append_int_noprefix(table_data, 0 /* Don't include all pci device */ ,
1686
0
                              1);
1687
0
    build_append_int_noprefix(table_data, 0 , 1); /* Reserved */
1688
0
    build_append_int_noprefix(table_data, 0 , 2); /* Segment Number */
1689
    /* Register Base Address */
1690
0
    build_append_int_noprefix(table_data, Q35_HOST_BRIDGE_IOMMU_ADDR , 8);
1691
1692
    /* Scope definition for the root-complex IOAPIC. See VT-d spec
1693
     * 8.3.1 (version Oct. 2014 or later). */
1694
0
    build_append_int_noprefix(table_data, 0x03 /* IOAPIC */, 1); /* Type */
1695
0
    build_append_int_noprefix(table_data, ioapic_scope_size, 1); /* Length */
1696
0
    build_append_int_noprefix(table_data, 0, 2); /* Reserved */
1697
    /* Enumeration ID */
1698
0
    build_append_int_noprefix(table_data, ACPI_BUILD_IOAPIC_ID, 1);
1699
    /* Start Bus Number */
1700
0
    build_append_int_noprefix(table_data, Q35_PSEUDO_BUS_PLATFORM, 1);
1701
    /* Path, {Device, Function} pair */
1702
0
    build_append_int_noprefix(table_data, PCI_SLOT(Q35_PSEUDO_DEVFN_IOAPIC), 1);
1703
0
    build_append_int_noprefix(table_data, PCI_FUNC(Q35_PSEUDO_DEVFN_IOAPIC), 1);
1704
1705
    /* Add scope found above */
1706
0
    g_array_append_vals(table_data, scope_blob->data, scope_blob->len);
1707
0
    g_array_free(scope_blob, true);
1708
1709
0
    if (iommu->dt_supported) {
1710
        /* 8.5 Root Port ATS Capability Reporting Structure */
1711
0
        build_append_int_noprefix(table_data, 2, 2); /* Type */
1712
0
        build_append_int_noprefix(table_data, 8, 2); /* Length */
1713
0
        build_append_int_noprefix(table_data, 1 /* ALL_PORTS */, 1); /* Flags */
1714
0
        build_append_int_noprefix(table_data, 0, 1); /* Reserved */
1715
0
        build_append_int_noprefix(table_data, 0, 2); /* Segment Number */
1716
0
    }
1717
1718
0
    acpi_table_end(linker, &table);
1719
0
}
1720
1721
/*
1722
 * Windows ACPI Emulated Devices Table
1723
 * (Version 1.0 - April 6, 2009)
1724
 * Spec: http://download.microsoft.com/download/7/E/7/7E7662CF-CBEA-470B-A97E-CE7CE0D98DC2/WAET.docx
1725
 *
1726
 * Helpful to speedup Windows guests and ignored by others.
1727
 */
1728
static void
1729
build_waet(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1730
           const char *oem_table_id)
1731
61
{
1732
61
    AcpiTable table = { .sig = "WAET", .rev = 1, .oem_id = oem_id,
1733
61
                        .oem_table_id = oem_table_id };
1734
1735
61
    acpi_table_begin(&table, table_data);
1736
    /*
1737
     * Set "ACPI PM timer good" flag.
1738
     *
1739
     * Tells Windows guests that our ACPI PM timer is reliable in the
1740
     * sense that guest can read it only once to obtain a reliable value.
1741
     * Which avoids costly VMExits caused by guest re-reading it unnecessarily.
1742
     */
1743
61
    build_append_int_noprefix(table_data, 1 << 1 /* ACPI PM timer good */, 4);
1744
61
    acpi_table_end(linker, &table);
1745
61
}
1746
1747
/*
1748
 *   IVRS table as specified in AMD IOMMU Specification v2.62, Section 5.2
1749
 *   accessible here http://support.amd.com/TechDocs/48882_IOMMU.pdf
1750
 */
1751
0
#define IOAPIC_SB_DEVID   (uint64_t)PCI_BUILD_BDF(0, PCI_DEVFN(0x14, 0))
1752
1753
/*
1754
 * Insert IVHD entry for device and recurse, insert alias, or insert range as
1755
 * necessary for the PCI topology.
1756
 */
1757
static void
1758
insert_ivhd(PCIBus *bus, PCIDevice *dev, void *opaque)
1759
0
{
1760
0
    GArray *table_data = opaque;
1761
0
    uint32_t entry;
1762
1763
    /* "Select" IVHD entry, type 0x2 */
1764
0
    entry = PCI_BUILD_BDF(pci_bus_num(bus), dev->devfn) << 8 | 0x2;
1765
0
    build_append_int_noprefix(table_data, entry, 4);
1766
0
}
1767
1768
/*
1769
 * Insert IVHD entry for device and recurse, insert alias, or insert range as
1770
 * necessary for the PCI topology.
1771
 */
1772
static void add_ivhd_entries_from_bus(PCIBus *bus, GArray *table_data)
1773
0
{
1774
0
    uint8_t sub;
1775
0
    uint32_t entry;
1776
0
    PCIBus *child_bus;
1777
0
    PCIDevice *parent_dev;
1778
1779
0
    if (!bus->parent_dev) {
1780
0
        goto select_devices;
1781
0
    }
1782
1783
0
    parent_dev = bus->parent_dev;
1784
0
    sub = parent_dev->config[PCI_SUBORDINATE_BUS];
1785
1786
0
    if (pci_bus_is_express(bus)) {
1787
        /*
1788
         * Walk the bus if there are subordinates, otherwise use a range
1789
         * to cover an entire leaf bus.  We could potentially also use a
1790
         * range for traversed buses, but we'd need to take care not to
1791
         * create both Select and Range entries covering the same device.
1792
         * This is easier and potentially more compact.
1793
         *
1794
         * An example bare metal system seems to use Select entries for
1795
         * root ports without a slot (ie. built-ins) and Range entries
1796
         * when there is a slot.  The same system also only hard-codes
1797
         * the alias range for an onboard PCIe-to-PCI bridge, apparently
1798
         * making no effort to support nested bridges.  We attempt to
1799
         * be more thorough here.
1800
         */
1801
0
        if (sub != pci_bus_num(bus)) {
1802
0
            goto select_devices;
1803
0
        }
1804
        /* "Start of Range" IVHD entry, type 0x3 */
1805
0
        entry = PCI_BUILD_BDF(sub, PCI_DEVFN(0, 0)) << 8 | 0x3;
1806
0
        build_append_int_noprefix(table_data, entry, 4);
1807
        /* "End of Range" IVHD entry, type 0x4 */
1808
0
        entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
1809
0
        build_append_int_noprefix(table_data, entry, 4);
1810
1811
0
        return;
1812
0
    } else {
1813
        /*
1814
         * If the secondary bus is conventional, then we need to create an
1815
         * Alias range for everything downstream.  The range covers the
1816
         * first devfn on the secondary bus to the last devfn on the
1817
         * subordinate bus.  The alias target depends on legacy versus
1818
         * express bridges, just as in pci_device_iommu_address_space().
1819
         * DeviceIDa vs DeviceIDb as per the AMD IOMMU spec.
1820
         */
1821
0
        uint16_t dev_id_a, dev_id_b;
1822
1823
0
        dev_id_a = PCI_BUILD_BDF(pci_bus_num(bus), PCI_DEVFN(0, 0));
1824
1825
0
        if (pci_is_express(parent_dev) &&
1826
0
            pcie_cap_get_type(parent_dev) == PCI_EXP_TYPE_PCI_BRIDGE) {
1827
0
            dev_id_b = dev_id_a;
1828
0
        } else {
1829
0
            dev_id_b = PCI_BUILD_BDF(pci_bus_num(pci_get_bus(parent_dev)),
1830
0
                                     parent_dev->devfn);
1831
0
        }
1832
1833
        /* "Alias Start of Range" IVHD entry, type 0x43, 8 bytes */
1834
0
        build_append_int_noprefix(table_data, dev_id_a << 8 | 0x43, 4);
1835
0
        build_append_int_noprefix(table_data, dev_id_b << 8 | 0x0, 4);
1836
1837
        /* "End of Range" IVHD entry, type 0x4 */
1838
0
        entry = PCI_BUILD_BDF(sub, PCI_DEVFN(31, 7)) << 8 | 0x4;
1839
0
        build_append_int_noprefix(table_data, entry, 4);
1840
0
        return;
1841
0
    }
1842
1843
0
select_devices:
1844
0
    pci_for_each_device_under_bus(bus, insert_ivhd, table_data);
1845
1846
0
    QLIST_FOREACH(child_bus, &bus->child, sibling) {
1847
0
        add_ivhd_entries_from_bus(child_bus, table_data);
1848
0
    }
1849
0
}
1850
1851
/*
1852
 * IVHD type 0x10 reports features using Feature Reporting field, which has
1853
 * different format than extended feature register (EFR) in the IOMMU MMIO
1854
 * space.
1855
 *
1856
 * Convert the EFR format to feature reporting format.
1857
 */
1858
static uint32_t
1859
get_amd_ivhd_feature_report(uint64_t extended_feature)
1860
0
{
1861
0
    uint32_t feature_report;
1862
0
    uint64_t hats_mode = (extended_feature & AMDVI_HATS_MODE_MASK) >>
1863
0
                         AMDVI_HATS_MODE_SHIFT;
1864
0
    uint64_t gats_mode = (extended_feature & AMDVI_GATS_MODE_MASK) >>
1865
0
                         AMDVI_GATS_MODE_SHIFT;
1866
0
    uint32_t is_ia = !!(extended_feature & AMDVI_FEATURE_IA);
1867
0
    uint32_t is_ga = !!(extended_feature & AMDVI_FEATURE_GA);
1868
0
    uint32_t is_gt = !!(extended_feature & AMDVI_FEATURE_GT);
1869
0
    uint32_t is_xt = !!(extended_feature & AMDVI_FEATURE_XT);
1870
1871
0
    feature_report =
1872
0
        hats_mode << AMDVI_IVHD_FEATURE_REPORT_HATS_SHIFT |  /* HATS[31:30] */
1873
0
        gats_mode << AMDVI_IVHD_FEATURE_REPORT_GATS_SHIFT |  /* GATS[29:28] */
1874
0
        is_ia << AMDVI_IVHD_FEATURE_REPORT_IA_SUP_SHIFT |    /* IASup[5]    */
1875
0
        is_ga << AMDVI_IVHD_FEATURE_REPORT_GA_SUP_SHIFT |    /* GASup[6]    */
1876
0
        is_gt << AMDVI_IVHD_FEATURE_REPORT_GT_SUP_SHIFT |    /* GTSup[2]    */
1877
0
        is_xt << AMDVI_IVHD_FEATURE_REPORT_XT_SUP_SHIFT;     /* XTSup[0]    */
1878
1879
0
    return feature_report;
1880
0
}
1881
1882
static void
1883
build_amd_iommu(GArray *table_data, BIOSLinker *linker, const char *oem_id,
1884
                const char *oem_table_id)
1885
0
{
1886
0
    AMDVIState *s = AMD_IOMMU_DEVICE(x86_iommu_get_default());
1887
0
    GArray *ivhd_blob = g_array_new(false, true, 1);
1888
0
    AcpiTable table = { .sig = "IVRS", .rev = 1, .oem_id = oem_id,
1889
0
                        .oem_table_id = oem_table_id };
1890
0
    uint16_t iommu_devid = pci_get_bdf(&s->pci->dev);
1891
0
    uint64_t extended_feature = amdvi_extended_feature_register(s);
1892
1893
0
    acpi_table_begin(&table, table_data);
1894
    /* IVinfo - IO virtualization information common to all
1895
     * IOMMU units in a system
1896
     */
1897
0
    build_append_int_noprefix(table_data,
1898
0
                             (1UL << 0) | /* EFRSup */
1899
0
                             AMDVI_GVA_SIZE_48 | /* GVASize:     010b = 48 bits */
1900
0
                             AMDVI_PA_SIZE_52 |  /* PASize: 011_0100b = 52 bits */
1901
0
                             AMDVI_VA_SIZE_64,   /* VASize: 100_0000b = 64 bits */
1902
0
                             4);
1903
    /* reserved */
1904
0
    build_append_int_noprefix(table_data, 0, 8);
1905
1906
    /*
1907
     * A PCI bus walk, for each PCI host bridge, is necessary to create a
1908
     * complete set of IVHD entries.  Do this into a separate blob so that we
1909
     * can calculate the total IVRS table length here and then append the new
1910
     * blob further below.  Fall back to an entry covering all devices, which
1911
     * is sufficient when no aliases are present.
1912
     */
1913
0
    assert(s->root_bus);
1914
0
    add_ivhd_entries_from_bus(s->root_bus, ivhd_blob);
1915
1916
0
    if (!ivhd_blob->len) {
1917
        /*
1918
         *   Type 1 device entry reporting all devices
1919
         *   These are 4-byte device entries currently reporting the range of
1920
         *   Refer to Spec - Table 95:IVHD Device Entry Type Codes(4-byte)
1921
         */
1922
0
        build_append_int_noprefix(ivhd_blob, 0x0000001, 4);
1923
0
    }
1924
1925
    /*
1926
     * When interrupt remapping is supported, we add a special IVHD device
1927
     * for type IO-APIC
1928
     * Refer to spec - Table 95: IVHD device entry type codes
1929
     *
1930
     * Linux IOMMU driver checks for the special IVHD device (type IO-APIC).
1931
     * See Linux kernel commit 'c2ff5cf5294bcbd7fa50f7d860e90a66db7e5059'
1932
     */
1933
0
    if (x86_iommu_ir_supported(x86_iommu_get_default())) {
1934
0
        build_append_int_noprefix(ivhd_blob,
1935
0
                                 (0x1ull << 56) |           /* type IOAPIC */
1936
0
                                 (IOAPIC_SB_DEVID << 40) |  /* IOAPIC devid */
1937
0
                                 0x48,                      /* special device */
1938
0
                                 8);
1939
0
    }
1940
1941
    /* IVHD definition - type 10h */
1942
0
    build_append_int_noprefix(table_data, 0x10, 1);
1943
    /* virtualization flags */
1944
0
    build_append_int_noprefix(table_data,
1945
0
                             (1UL << 0) | /* HtTunEn      */
1946
0
                             (1UL << 4) | /* iotblSup     */
1947
0
                             (1UL << 6),  /* PrefSup      */
1948
0
                             1);
1949
1950
    /* IVHD length */
1951
0
    build_append_int_noprefix(table_data, ivhd_blob->len + 24, 2);
1952
    /* DeviceID */
1953
0
    build_append_int_noprefix(table_data, iommu_devid, 2);
1954
    /* Capability offset */
1955
0
    build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
1956
    /* IOMMU base address */
1957
0
    build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
1958
    /* PCI Segment Group */
1959
0
    build_append_int_noprefix(table_data, 0, 2);
1960
    /* IOMMU info */
1961
0
    build_append_int_noprefix(table_data, 0, 2);
1962
    /* IOMMU Feature Reporting */
1963
0
    build_append_int_noprefix(table_data,
1964
0
                              get_amd_ivhd_feature_report(extended_feature),
1965
0
                              4);
1966
1967
    /* IVHD entries as found above */
1968
0
    g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
1969
1970
   /* IVHD definition - type 11h */
1971
0
    build_append_int_noprefix(table_data, 0x11, 1);
1972
    /* virtualization flags */
1973
0
    build_append_int_noprefix(table_data,
1974
0
                             (1UL << 0) | /* HtTunEn      */
1975
0
                             (1UL << 4),  /* iotblSup     */
1976
0
                             1);
1977
1978
    /* IVHD length */
1979
0
    build_append_int_noprefix(table_data, ivhd_blob->len + 40, 2);
1980
1981
    /* DeviceID */
1982
0
    build_append_int_noprefix(table_data, iommu_devid, 2);
1983
    /* Capability offset */
1984
0
    build_append_int_noprefix(table_data, s->pci->capab_offset, 2);
1985
    /* IOMMU base address */
1986
0
    build_append_int_noprefix(table_data, s->mr_mmio.addr, 8);
1987
    /* PCI Segment Group */
1988
0
    build_append_int_noprefix(table_data, 0, 2);
1989
    /* IOMMU info */
1990
0
    build_append_int_noprefix(table_data, 0, 2);
1991
    /* IOMMU Attributes */
1992
0
    if (!s->iommu.dma_translation) {
1993
0
        build_append_int_noprefix(table_data, (1UL << 0) /* HATDis */, 4);
1994
0
    } else {
1995
0
        build_append_int_noprefix(table_data, 0, 4);
1996
0
    }
1997
    /* EFR Register Image */
1998
0
    build_append_int_noprefix(table_data, extended_feature, 8);
1999
    /* EFR Register Image 2 */
2000
0
    build_append_int_noprefix(table_data, 0, 8);
2001
2002
    /* IVHD entries as found above */
2003
0
    g_array_append_vals(table_data, ivhd_blob->data, ivhd_blob->len);
2004
2005
0
    g_array_free(ivhd_blob, TRUE);
2006
0
    acpi_table_end(linker, &table);
2007
0
}
2008
2009
typedef
2010
struct AcpiBuildState {
2011
    /* Copy of table in RAM (for patching). */
2012
    MemoryRegion *table_mr;
2013
    /* Is table patched? */
2014
    uint8_t patched;
2015
    MemoryRegion *rsdp_mr;
2016
    MemoryRegion *linker_mr;
2017
} AcpiBuildState;
2018
2019
static bool acpi_get_mcfg(AcpiMcfgInfo *mcfg)
2020
122
{
2021
122
    Object *pci_host;
2022
122
    QObject *o;
2023
2024
122
    pci_host = acpi_get_i386_pci_host();
2025
122
    if (!pci_host) {
2026
0
        return false;
2027
0
    }
2028
2029
122
    o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_BASE, NULL);
2030
122
    if (!o) {
2031
16
        return false;
2032
16
    }
2033
106
    mcfg->base = qnum_get_uint(qobject_to(QNum, o));
2034
106
    qobject_unref(o);
2035
106
    if (mcfg->base == PCIE_BASE_ADDR_UNMAPPED) {
2036
106
        return false;
2037
106
    }
2038
2039
0
    o = object_property_get_qobject(pci_host, PCIE_HOST_MCFG_SIZE, NULL);
2040
0
    assert(o);
2041
0
    mcfg->size = qnum_get_uint(qobject_to(QNum, o));
2042
0
    qobject_unref(o);
2043
0
    return true;
2044
0
}
2045
2046
static
2047
void acpi_build(AcpiBuildTables *tables, MachineState *machine)
2048
61
{
2049
61
    PCMachineState *pcms = PC_MACHINE(machine);
2050
61
    X86MachineState *x86ms = X86_MACHINE(machine);
2051
61
    DeviceState *iommu = pcms->iommu;
2052
61
    GArray *table_offsets;
2053
61
    unsigned facs, dsdt, rsdt;
2054
61
    AcpiPmInfo pm;
2055
61
    AcpiMiscInfo misc;
2056
61
    AcpiMcfgInfo mcfg;
2057
61
    Range pci_hole = {}, pci_hole64 = {};
2058
61
    uint8_t *u;
2059
61
    GArray *tables_blob = tables->table_data;
2060
61
    AcpiSlicOem slic_oem = { .id = NULL, .table_id = NULL };
2061
61
    Object *vmgenid_dev, *vmclock_dev;
2062
61
    char *oem_id;
2063
61
    char *oem_table_id;
2064
2065
61
    acpi_get_pm_info(machine, &pm);
2066
61
    acpi_get_misc_info(&misc);
2067
61
    acpi_get_pci_holes(&pci_hole, &pci_hole64);
2068
61
    acpi_get_slic_oem(&slic_oem);
2069
2070
61
    if (slic_oem.id) {
2071
0
        oem_id = slic_oem.id;
2072
61
    } else {
2073
61
        oem_id = x86ms->oem_id;
2074
61
    }
2075
2076
61
    if (slic_oem.table_id) {
2077
0
        oem_table_id = slic_oem.table_id;
2078
61
    } else {
2079
61
        oem_table_id = x86ms->oem_table_id;
2080
61
    }
2081
2082
61
    table_offsets = g_array_new(false, true /* clear */,
2083
61
                                        sizeof(uint32_t));
2084
61
    ACPI_BUILD_DPRINTF("init ACPI tables\n");
2085
2086
61
    bios_linker_loader_alloc(tables->linker,
2087
61
                             ACPI_BUILD_TABLE_FILE, tables_blob,
2088
61
                             64 /* Ensure FACS is aligned */,
2089
61
                             false /* high memory */);
2090
2091
    /*
2092
     * FACS is pointed to by FADT.
2093
     * We place it first since it's the only table that has alignment
2094
     * requirements.
2095
     */
2096
61
    facs = tables_blob->len;
2097
61
    build_facs(tables_blob);
2098
2099
    /* DSDT is pointed to by FADT */
2100
61
    dsdt = tables_blob->len;
2101
61
    build_dsdt(tables_blob, tables->linker, &pm, &misc,
2102
61
               &pci_hole, &pci_hole64, machine);
2103
2104
    /* ACPI tables pointed to by RSDT */
2105
61
    acpi_add_table(table_offsets, tables_blob);
2106
61
    pm.fadt.facs_tbl_offset = &facs;
2107
61
    pm.fadt.dsdt_tbl_offset = &dsdt;
2108
61
    pm.fadt.xdsdt_tbl_offset = &dsdt;
2109
61
    build_fadt(tables_blob, tables->linker, &pm.fadt, oem_id, oem_table_id);
2110
2111
61
    acpi_add_table(table_offsets, tables_blob);
2112
61
    acpi_build_madt(tables_blob, tables->linker, x86ms,
2113
61
                    x86ms->oem_id, x86ms->oem_table_id);
2114
2115
61
#ifdef CONFIG_ACPI_ERST
2116
61
    {
2117
61
        Object *erst_dev;
2118
61
        erst_dev = find_erst_dev();
2119
61
        if (erst_dev) {
2120
0
            acpi_add_table(table_offsets, tables_blob);
2121
0
            build_erst(tables_blob, tables->linker, erst_dev,
2122
0
                       x86ms->oem_id, x86ms->oem_table_id);
2123
0
        }
2124
61
    }
2125
61
#endif
2126
2127
61
    vmgenid_dev = find_vmgenid_dev();
2128
61
    if (vmgenid_dev) {
2129
0
        acpi_add_table(table_offsets, tables_blob);
2130
0
        vmgenid_build_acpi(VMGENID(vmgenid_dev), tables_blob,
2131
0
                           tables->vmgenid, tables->linker, x86ms->oem_id);
2132
0
    }
2133
2134
61
    vmclock_dev = find_vmclock_dev();
2135
61
    if (vmclock_dev) {
2136
0
        acpi_add_table(table_offsets, tables_blob);
2137
0
        vmclock_build_acpi(VMCLOCK(vmclock_dev), tables_blob, tables->linker,
2138
0
                           x86ms->oem_id);
2139
0
    }
2140
2141
61
    if (misc.has_hpet) {
2142
61
        acpi_add_table(table_offsets, tables_blob);
2143
61
        build_hpet(tables_blob, tables->linker, x86ms->oem_id,
2144
61
                   x86ms->oem_table_id);
2145
61
    }
2146
61
#ifdef CONFIG_TPM
2147
61
    if (misc.tpm_version != TPM_VERSION_UNSPEC) {
2148
0
        if (misc.tpm_version == TPM_VERSION_1_2) {
2149
0
            acpi_add_table(table_offsets, tables_blob);
2150
0
            build_tpm_tcpa(tables_blob, tables->linker, tables->tcpalog,
2151
0
                           x86ms->oem_id, x86ms->oem_table_id);
2152
0
        } else { /* TPM_VERSION_2_0 */
2153
0
            acpi_add_table(table_offsets, tables_blob);
2154
0
            build_tpm2(tables_blob, tables->linker, tables->tcpalog,
2155
0
                       x86ms->oem_id, x86ms->oem_table_id);
2156
0
        }
2157
0
    }
2158
61
#endif
2159
61
    if (machine->numa_state->num_nodes) {
2160
0
        acpi_add_table(table_offsets, tables_blob);
2161
0
        build_srat(tables_blob, tables->linker, machine);
2162
0
        if (machine->numa_state->have_numa_distance) {
2163
0
            acpi_add_table(table_offsets, tables_blob);
2164
0
            build_slit(tables_blob, tables->linker, machine, x86ms->oem_id,
2165
0
                       x86ms->oem_table_id);
2166
0
        }
2167
0
        if (machine->numa_state->hmat_enabled) {
2168
0
            acpi_add_table(table_offsets, tables_blob);
2169
0
            build_hmat(tables_blob, tables->linker, machine->numa_state,
2170
0
                       x86ms->oem_id, x86ms->oem_table_id);
2171
0
        }
2172
0
    }
2173
61
    if (acpi_get_mcfg(&mcfg)) {
2174
0
        acpi_add_table(table_offsets, tables_blob);
2175
0
        build_mcfg(tables_blob, tables->linker, &mcfg, x86ms->oem_id,
2176
0
                   x86ms->oem_table_id);
2177
0
    }
2178
61
    if (object_dynamic_cast(OBJECT(iommu), TYPE_AMD_IOMMU_DEVICE)) {
2179
0
        acpi_add_table(table_offsets, tables_blob);
2180
0
        build_amd_iommu(tables_blob, tables->linker, x86ms->oem_id,
2181
0
                        x86ms->oem_table_id);
2182
61
    } else if (object_dynamic_cast(OBJECT(iommu), TYPE_INTEL_IOMMU_DEVICE)) {
2183
0
        acpi_add_table(table_offsets, tables_blob);
2184
0
        build_dmar_q35(tables_blob, tables->linker, x86ms->oem_id,
2185
0
                       x86ms->oem_table_id);
2186
61
    } else if (object_dynamic_cast(OBJECT(iommu), TYPE_VIRTIO_IOMMU_PCI)) {
2187
0
        PCIDevice *pdev = PCI_DEVICE(iommu);
2188
2189
0
        acpi_add_table(table_offsets, tables_blob);
2190
0
        build_viot(machine, tables_blob, tables->linker, pci_get_bdf(pdev),
2191
0
                   x86ms->oem_id, x86ms->oem_table_id);
2192
0
    }
2193
61
    if (machine->nvdimms_state->is_enabled) {
2194
0
        nvdimm_build_acpi(table_offsets, tables_blob, tables->linker,
2195
0
                          machine->nvdimms_state, machine->ram_slots,
2196
0
                          x86ms->oem_id, x86ms->oem_table_id);
2197
0
    }
2198
61
    if (pcms->cxl_devices_state.is_enabled) {
2199
0
        cxl_build_cedt(table_offsets, tables_blob, tables->linker,
2200
0
                       x86ms->oem_id, x86ms->oem_table_id, &pcms->cxl_devices_state);
2201
0
    }
2202
2203
61
    acpi_add_table(table_offsets, tables_blob);
2204
61
    build_waet(tables_blob, tables->linker, x86ms->oem_id, x86ms->oem_table_id);
2205
2206
61
    if (pcms->wdat_enabled == true) {
2207
0
        g_assert(pm.tco_io_base);
2208
0
        acpi_add_table(table_offsets, tables_blob);
2209
0
        build_ich9_wdat(tables_blob, tables->linker, x86ms->oem_id,
2210
0
                        x86ms->oem_table_id, pm.tco_io_base);
2211
0
    }
2212
2213
    /* Add tables supplied by user (if any) */
2214
61
    for (u = acpi_table_first(); u; u = acpi_table_next(u)) {
2215
0
        unsigned len = acpi_table_len(u);
2216
2217
0
        acpi_add_table(table_offsets, tables_blob);
2218
0
        g_array_append_vals(tables_blob, u, len);
2219
0
    }
2220
2221
    /* RSDT is pointed to by RSDP */
2222
61
    rsdt = tables_blob->len;
2223
61
    build_rsdt(tables_blob, tables->linker, table_offsets,
2224
61
               oem_id, oem_table_id);
2225
2226
    /* RSDP is in FSEG memory, so allocate it separately */
2227
61
    {
2228
61
        AcpiRsdpData rsdp_data = {
2229
61
            .revision = 0,
2230
61
            .oem_id = x86ms->oem_id,
2231
61
            .xsdt_tbl_offset = NULL,
2232
61
            .rsdt_tbl_offset = &rsdt,
2233
61
        };
2234
61
        build_rsdp(tables->rsdp, tables->linker, &rsdp_data);
2235
61
    }
2236
2237
    /* We'll expose it all to Guest so we want to reduce
2238
     * chance of size changes.
2239
     *
2240
     * We used to align the tables to 4k, but of course this would
2241
     * too simple to be enough.  4k turned out to be too small an
2242
     * alignment very soon, and in fact it is almost impossible to
2243
     * keep the table size stable for all (max_cpus, max_memory_slots)
2244
     * combinations.
2245
     */
2246
61
    acpi_align_size(tables_blob, ACPI_BUILD_TABLE_SIZE);
2247
2248
61
    acpi_align_size(tables->linker->cmd_blob, ACPI_BUILD_ALIGN_SIZE);
2249
2250
    /* Cleanup memory that's no longer used. */
2251
61
    g_array_free(table_offsets, true);
2252
61
    g_free(slic_oem.id);
2253
61
    g_free(slic_oem.table_id);
2254
61
}
2255
2256
static void acpi_ram_update(MemoryRegion *mr, GArray *data)
2257
0
{
2258
0
    uint32_t size = acpi_data_len(data);
2259
2260
    /* Make sure RAM size is correct - in case it got changed e.g. by migration */
2261
0
    memory_region_ram_resize(mr, size, &error_abort);
2262
2263
0
    memcpy(memory_region_get_ram_ptr(mr), data->data, size);
2264
0
    memory_region_set_dirty(mr, 0, size);
2265
0
}
2266
2267
static void acpi_build_update(void *build_opaque)
2268
0
{
2269
0
    AcpiBuildState *build_state = build_opaque;
2270
0
    AcpiBuildTables tables;
2271
2272
    /* No state to update or already patched? Nothing to do. */
2273
0
    if (!build_state || build_state->patched) {
2274
0
        return;
2275
0
    }
2276
0
    build_state->patched = 1;
2277
2278
0
    acpi_build_tables_init(&tables);
2279
2280
0
    acpi_build(&tables, MACHINE(qdev_get_machine()));
2281
2282
0
    acpi_ram_update(build_state->table_mr, tables.table_data);
2283
2284
0
    acpi_ram_update(build_state->rsdp_mr, tables.rsdp);
2285
2286
0
    acpi_ram_update(build_state->linker_mr, tables.linker->cmd_blob);
2287
0
    acpi_build_tables_cleanup(&tables, true);
2288
0
}
2289
2290
static void acpi_build_reset(void *build_opaque)
2291
92.8k
{
2292
92.8k
    AcpiBuildState *build_state = build_opaque;
2293
92.8k
    build_state->patched = 0;
2294
92.8k
}
2295
2296
static const VMStateDescription vmstate_acpi_build = {
2297
    .name = "acpi_build",
2298
    .version_id = 1,
2299
    .minimum_version_id = 1,
2300
    .fields = (const VMStateField[]) {
2301
        VMSTATE_UINT8(patched, AcpiBuildState),
2302
        VMSTATE_END_OF_LIST()
2303
    },
2304
};
2305
2306
void acpi_setup(void)
2307
61
{
2308
61
    PCMachineState *pcms = PC_MACHINE(qdev_get_machine());
2309
61
    X86MachineState *x86ms = X86_MACHINE(pcms);
2310
61
    AcpiBuildTables tables;
2311
61
    AcpiBuildState *build_state;
2312
61
    Object *vmgenid_dev;
2313
61
#ifdef CONFIG_TPM
2314
61
    TPMIf *tpm;
2315
61
    static FwCfgTPMConfig tpm_config;
2316
61
#endif
2317
2318
61
    if (!x86ms->fw_cfg) {
2319
0
        ACPI_BUILD_DPRINTF("No fw cfg. Bailing out.\n");
2320
0
        return;
2321
0
    }
2322
2323
61
    if (!pcms->acpi_build_enabled) {
2324
0
        ACPI_BUILD_DPRINTF("ACPI build disabled. Bailing out.\n");
2325
0
        return;
2326
0
    }
2327
2328
61
    if (!x86_machine_is_acpi_enabled(X86_MACHINE(pcms))) {
2329
0
        ACPI_BUILD_DPRINTF("ACPI disabled. Bailing out.\n");
2330
0
        return;
2331
0
    }
2332
2333
61
    build_state = g_malloc0(sizeof *build_state);
2334
2335
61
    acpi_build_tables_init(&tables);
2336
61
    acpi_build(&tables, MACHINE(pcms));
2337
2338
    /* Now expose it all to Guest */
2339
61
    build_state->table_mr = acpi_add_rom_blob(acpi_build_update,
2340
61
                                              build_state, tables.table_data,
2341
61
                                              ACPI_BUILD_TABLE_FILE);
2342
61
    assert(build_state->table_mr != NULL);
2343
2344
61
    build_state->linker_mr =
2345
61
        acpi_add_rom_blob(acpi_build_update, build_state,
2346
61
                          tables.linker->cmd_blob, ACPI_BUILD_LOADER_FILE);
2347
2348
61
#ifdef CONFIG_TPM
2349
61
    fw_cfg_add_file(x86ms->fw_cfg, ACPI_BUILD_TPMLOG_FILE,
2350
61
                    tables.tcpalog->data, acpi_data_len(tables.tcpalog));
2351
2352
61
    tpm = tpm_find();
2353
61
    if (tpm_ppi_enabled(tpm)) {
2354
0
        tpm_config = (FwCfgTPMConfig) {
2355
0
            .tpmppi_address = cpu_to_le32(TPM_PPI_ADDR_BASE),
2356
0
            .tpm_version = tpm_get_version(tpm),
2357
0
            .tpmppi_version = TPM_PPI_VERSION_1_30
2358
0
        };
2359
0
        fw_cfg_add_file(x86ms->fw_cfg, "etc/tpm/config",
2360
0
                        &tpm_config, sizeof tpm_config);
2361
0
    }
2362
61
#endif
2363
2364
61
    vmgenid_dev = find_vmgenid_dev();
2365
61
    if (vmgenid_dev) {
2366
0
        vmgenid_add_fw_cfg(VMGENID(vmgenid_dev), x86ms->fw_cfg,
2367
0
                           tables.vmgenid);
2368
0
    }
2369
2370
61
    build_state->rsdp_mr = acpi_add_rom_blob(acpi_build_update,
2371
61
                                             build_state, tables.rsdp,
2372
61
                                             ACPI_BUILD_RSDP_FILE);
2373
2374
61
    qemu_register_reset(acpi_build_reset, build_state);
2375
61
    acpi_build_reset(build_state);
2376
61
    vmstate_register(NULL, 0, &vmstate_acpi_build, build_state);
2377
2378
    /* Cleanup tables but don't free the memory: we track it
2379
     * in build_state.
2380
     */
2381
61
    acpi_build_tables_cleanup(&tables, false);
2382
61
}
2383
2384
GArray *acpi_build_madt_standalone(MachineState *machine)
2385
0
{
2386
0
  X86MachineState *x86ms = X86_MACHINE(machine);
2387
0
  GArray *table = g_array_new(false, true, 1);
2388
  acpi_build_madt(table, NULL, x86ms, x86ms->oem_id,
2389
0
                  x86ms->oem_table_id);
2390
0
  return table;
2391
0
}