File: libxl_arm_acpi.c

package info (click to toggle)
xen 4.11.4+24-gddaaccbbab-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 36,336 kB
  • sloc: ansic: 489,281; asm: 7,844; python: 7,435; makefile: 7,344; sh: 6,424; ml: 4,752; perl: 4,223; cpp: 1,829; lex: 708; yacc: 656; pascal: 433
file content (413 lines) | stat: -rw-r--r-- 13,411 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
/*
 * ARM DomU ACPI generation
 *
 * Copyright (C) 2016      Linaro Ltd.
 *
 * Author: Shannon Zhao <shannon.zhao@linaro.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published
 * by the Free Software Foundation; version 2.1 only. with the special
 * exception on linking described in file LICENSE.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 */

#include "libxl_arm.h"

#include <stdint.h>

/* Below typedefs are useful for the headers under acpi/ */
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
typedef uint64_t u64;
typedef int64_t s64;

#include <acpi/acconfig.h>
#include <acpi/actbl.h>

#ifndef BITS_PER_LONG
#ifdef _LP64
#define BITS_PER_LONG 64
#else
#define BITS_PER_LONG 32
#endif
#endif
#define ACPI_MACHINE_WIDTH BITS_PER_LONG
#define COMPILER_DEPENDENT_INT64 int64_t
#define COMPILER_DEPENDENT_UINT64 uint64_t

#include <acpi/actypes.h>

_hidden
extern const unsigned char dsdt_anycpu_arm[];
_hidden
extern const int dsdt_anycpu_arm_len;

#define ACPI_OEM_ID "Xen\0\0"
#define ACPI_OEM_TABLE_ID "ARM\0\0\0\0"
#define ACPI_ASL_COMPILER_ID "XL\0"

enum {
    RSDP,
    XSDT,
    GTDT,
    MADT,
    FADT,
    DSDT,
    MAX_TABLE_NUMS,
};

struct acpitable {
    uint64_t addr;
    size_t size;
};

static int libxl__estimate_madt_size(libxl__gc *gc,
                                     const libxl_domain_build_info *info,
                                     size_t *size)
{
    int rc = 0;

    switch (info->arch_arm.gic_version) {
    case LIBXL_GIC_VERSION_V2:
        *size = sizeof(struct acpi_table_madt) +
                ACPI_MADT_GICC_SIZE_v5 * info->max_vcpus +
                sizeof(struct acpi_madt_generic_distributor);
        break;
    case LIBXL_GIC_VERSION_V3:
        *size = sizeof(struct acpi_table_madt) +
                ACPI_MADT_GICC_SIZE_v5 * info->max_vcpus +
                sizeof(struct acpi_madt_generic_distributor) +
                sizeof(struct acpi_madt_generic_redistributor);
        break;
    default:
        LOG(ERROR, "Unknown GIC version");
        *size = 0;
        rc = ERROR_FAIL;
        break;
    }

    return rc;
}

int libxl__get_acpi_size(libxl__gc *gc,
                         const libxl_domain_build_info *info,
                         uint64_t *out)
{
    uint64_t size;
    int rc = 0;


    rc = libxl__estimate_madt_size(gc, info, &size);
    if (rc < 0)
        goto out;

    *out = ROUNDUP(size, 3) +
           ROUNDUP(sizeof(struct acpi_table_rsdp), 3) +
           ROUNDUP(sizeof(struct acpi_table_xsdt), 3) +
           ROUNDUP(sizeof(struct acpi_table_gtdt), 3) +
           ROUNDUP(sizeof(struct acpi_table_fadt), 3) +
           ROUNDUP(sizeof(dsdt_anycpu_arm_len), 3);

out:
    return rc;
}

static int libxl__allocate_acpi_tables(libxl__gc *gc,
                                       libxl_domain_build_info *info,
                                       struct xc_dom_image *dom,
                                       struct acpitable acpitables[])
{
    int rc;
    size_t size;

    acpitables[RSDP].addr = GUEST_ACPI_BASE;
    acpitables[RSDP].size = sizeof(struct acpi_table_rsdp);
    dom->acpi_modules[0].length += ROUNDUP(acpitables[RSDP].size, 3);

    acpitables[XSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
    /*
     * Currently only 3 tables(GTDT, FADT, MADT) are pointed by XSDT. Alloc
     * entries for them.
     */
    acpitables[XSDT].size = sizeof(struct acpi_table_xsdt) +
                            sizeof(uint64_t) * 2;
    dom->acpi_modules[0].length += ROUNDUP(acpitables[XSDT].size, 3);

    acpitables[GTDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
    acpitables[GTDT].size = sizeof(struct acpi_table_gtdt);
    dom->acpi_modules[0].length += ROUNDUP(acpitables[GTDT].size, 3);

    acpitables[MADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;

    rc = libxl__estimate_madt_size(gc, info, &size);
    if (rc < 0)
        goto out;

    acpitables[MADT].size = size;
    dom->acpi_modules[0].length += ROUNDUP(acpitables[MADT].size, 3);

    acpitables[FADT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
    acpitables[FADT].size = sizeof(struct acpi_table_fadt);
    dom->acpi_modules[0].length += ROUNDUP(acpitables[FADT].size, 3);

    acpitables[DSDT].addr = GUEST_ACPI_BASE + dom->acpi_modules[0].length;
    acpitables[DSDT].size = dsdt_anycpu_arm_len;
    dom->acpi_modules[0].length += ROUNDUP(acpitables[DSDT].size, 3);

    assert(dom->acpi_modules[0].length <= GUEST_ACPI_SIZE);
    dom->acpi_modules[0].data = libxl__zalloc(gc, dom->acpi_modules[0].length);

    rc = 0;
out:
    return rc;
}

static void calculate_checksum(void *table, uint32_t checksum_offset,
                               uint32_t length)
{
    uint8_t *p, sum = 0;

    p = table;
    p[checksum_offset] = 0;

    while (length--)
        sum = sum + *p++;

    p = table;
    p[checksum_offset] = -sum;
}

static void make_acpi_rsdp(libxl__gc *gc, struct xc_dom_image *dom,
                           struct acpitable acpitables[])
{
    uint64_t offset = acpitables[RSDP].addr - GUEST_ACPI_BASE;
    struct acpi_table_rsdp *rsdp = (void *)dom->acpi_modules[0].data + offset;

    memcpy(rsdp->signature, "RSD PTR ", sizeof(rsdp->signature));
    BUILD_BUG_ON(sizeof(ACPI_OEM_ID) != sizeof(rsdp->oem_id));
    memcpy(rsdp->oem_id, ACPI_OEM_ID, sizeof(rsdp->oem_id));
    rsdp->length = acpitables[RSDP].size;
    rsdp->revision = 0x02;
    rsdp->xsdt_physical_address = acpitables[XSDT].addr;
    calculate_checksum(rsdp,
                       offsetof(struct acpi_table_rsdp, extended_checksum),
                       acpitables[RSDP].size);
}

static void make_acpi_header(struct acpi_table_header *h, const char *sig,
                             size_t len, uint8_t rev)
{
    memcpy(h->signature, sig, 4);
    h->length = len;
    h->revision = rev;
    BUILD_BUG_ON(sizeof(ACPI_OEM_ID) != sizeof(h->oem_id));
    memcpy(h->oem_id, ACPI_OEM_ID, sizeof(h->oem_id));
    BUILD_BUG_ON(sizeof(ACPI_OEM_TABLE_ID) != sizeof(h->oem_table_id));
    memcpy(h->oem_table_id, ACPI_OEM_TABLE_ID, sizeof(h->oem_table_id));
    h->oem_revision = 0;
    BUILD_BUG_ON(sizeof(ACPI_ASL_COMPILER_ID) != sizeof(h->asl_compiler_id));
    memcpy(h->asl_compiler_id, ACPI_ASL_COMPILER_ID,
           sizeof(h->asl_compiler_id));
    h->asl_compiler_revision = 0;
    h->checksum = 0;
}

static void make_acpi_xsdt(libxl__gc *gc, struct xc_dom_image *dom,
                           struct acpitable acpitables[])
{
    uint64_t offset = acpitables[XSDT].addr - GUEST_ACPI_BASE;
    struct acpi_table_xsdt *xsdt = (void *)dom->acpi_modules[0].data + offset;

    xsdt->table_offset_entry[0] = acpitables[MADT].addr;
    xsdt->table_offset_entry[1] = acpitables[GTDT].addr;
    xsdt->table_offset_entry[2] = acpitables[FADT].addr;
    make_acpi_header(&xsdt->header, "XSDT", acpitables[XSDT].size, 1);
    calculate_checksum(xsdt, offsetof(struct acpi_table_header, checksum),
                       acpitables[XSDT].size);
}

static void make_acpi_gtdt(libxl__gc *gc, struct xc_dom_image *dom,
                           struct acpitable acpitables[])
{
    uint64_t offset = acpitables[GTDT].addr - GUEST_ACPI_BASE;
    struct acpi_table_gtdt *gtdt = (void *)dom->acpi_modules[0].data + offset;

    gtdt->non_secure_el1_interrupt = GUEST_TIMER_PHYS_NS_PPI;
    gtdt->non_secure_el1_flags =
                             (ACPI_LEVEL_SENSITIVE << ACPI_GTDT_INTERRUPT_MODE)
                             |(ACPI_ACTIVE_LOW << ACPI_GTDT_INTERRUPT_POLARITY);
    gtdt->virtual_timer_interrupt = GUEST_TIMER_VIRT_PPI;
    gtdt->virtual_timer_flags =
                             (ACPI_LEVEL_SENSITIVE << ACPI_GTDT_INTERRUPT_MODE)
                             |(ACPI_ACTIVE_LOW << ACPI_GTDT_INTERRUPT_POLARITY);

    gtdt->counter_block_addresss = ~((uint64_t)0);
    gtdt->counter_read_block_address = ~((uint64_t)0);

    make_acpi_header(&gtdt->header, "GTDT", acpitables[GTDT].size, 2);
    calculate_checksum(gtdt, offsetof(struct acpi_table_header, checksum),
                       acpitables[GTDT].size);
}

static void make_acpi_madt_gicc(void *table, int nr_cpus, uint64_t gicc_base)
{
    int i;
    struct acpi_madt_generic_interrupt *gicc = table;

    for (i = 0; i < nr_cpus; i++) {
        gicc->header.type = ACPI_MADT_TYPE_GENERIC_INTERRUPT;
        gicc->header.length = ACPI_MADT_GICC_SIZE_v5;
        gicc->base_address = gicc_base;
        gicc->cpu_interface_number = i;
        gicc->arm_mpidr = libxl__compute_mpdir(i);
        gicc->uid = i;
        gicc->flags = ACPI_MADT_ENABLED;
        gicc = table + ACPI_MADT_GICC_SIZE_v5;
    }
}

static void make_acpi_madt_gicd(void *table, uint64_t gicd_base,
                                uint8_t gic_version)
{
    struct acpi_madt_generic_distributor *gicd = table;

    gicd->header.type = ACPI_MADT_TYPE_GENERIC_DISTRIBUTOR;
    gicd->header.length = sizeof(*gicd);
    gicd->base_address = gicd_base;
    /* This version field has no meaning before ACPI 5.1 errata. */
    gicd->version = gic_version;
}

static void make_acpi_madt_gicr(void *table, uint64_t gicr_base,
                                uint64_t gicr_size)
{
    struct acpi_madt_generic_redistributor *gicr = table;

    gicr->header.type = ACPI_MADT_TYPE_GENERIC_REDISTRIBUTOR;
    gicr->header.length = sizeof(*gicr);
    gicr->base_address = gicr_base;
    gicr->length = gicr_size;
}

static int make_acpi_madt(libxl__gc *gc, struct xc_dom_image *dom,
                          libxl_domain_build_info *info,
                          struct acpitable acpitables[])
{
    uint64_t offset = acpitables[MADT].addr - GUEST_ACPI_BASE;
    void *table = dom->acpi_modules[0].data + offset;
    struct acpi_table_madt *madt = table;
    int rc = 0;

    switch (info->arch_arm.gic_version) {
    case LIBXL_GIC_VERSION_V2:
        table += sizeof(struct acpi_table_madt);
        make_acpi_madt_gicc(table, info->max_vcpus, GUEST_GICC_BASE);

        table += ACPI_MADT_GICC_SIZE_v5 * info->max_vcpus;
        make_acpi_madt_gicd(table, GUEST_GICD_BASE, ACPI_MADT_GIC_VERSION_V2);
        break;
    case LIBXL_GIC_VERSION_V3:
        table += sizeof(struct acpi_table_madt);
        make_acpi_madt_gicc(table, info->max_vcpus, 0);

        table += ACPI_MADT_GICC_SIZE_v5 * info->max_vcpus;
        make_acpi_madt_gicd(table, GUEST_GICV3_GICD_BASE,
                            ACPI_MADT_GIC_VERSION_V3);

        table += sizeof(struct acpi_madt_generic_distributor);
        make_acpi_madt_gicr(table, GUEST_GICV3_GICR0_BASE,
                            GUEST_GICV3_GICR0_SIZE);
        break;
    default:
        LOG(ERROR, "Unknown GIC version");
        rc = ERROR_FAIL;
        goto out;
    }

    make_acpi_header(&madt->header, "APIC", acpitables[MADT].size, 3);
    calculate_checksum(madt, offsetof(struct acpi_table_header, checksum),
                       acpitables[MADT].size);

out:
    return rc;
}

static void make_acpi_fadt(libxl__gc *gc, struct xc_dom_image *dom,
                           struct acpitable acpitables[])
{
    uint64_t offset = acpitables[FADT].addr - GUEST_ACPI_BASE;
    struct acpi_table_fadt *fadt = (void *)dom->acpi_modules[0].data + offset;

    /* Hardware Reduced = 1 and use PSCI 0.2+ and with HVC */
    fadt->flags = ACPI_FADT_HW_REDUCED;
    fadt->arm_boot_flags = ACPI_FADT_PSCI_COMPLIANT | ACPI_FADT_PSCI_USE_HVC;

    /* ACPI v5.1 (fadt->revision.fadt->minor_revision) */
    fadt->minor_revision = 0x1;
    fadt->dsdt = acpitables[DSDT].addr;

    make_acpi_header(&fadt->header, "FACP", acpitables[FADT].size, 5);
    calculate_checksum(fadt, offsetof(struct acpi_table_header, checksum),
                       acpitables[FADT].size);
}

static void make_acpi_dsdt(libxl__gc *gc, struct xc_dom_image *dom,
                           struct acpitable acpitables[])
{
    uint64_t offset = acpitables[DSDT].addr - GUEST_ACPI_BASE;
    void *dsdt = dom->acpi_modules[0].data + offset;

    memcpy(dsdt, dsdt_anycpu_arm, dsdt_anycpu_arm_len);
}

int libxl__prepare_acpi(libxl__gc *gc, libxl_domain_build_info *info,
                        struct xc_dom_image *dom)
{
    const libxl_version_info *vers;
    int rc = 0;
    struct acpitable acpitables[MAX_TABLE_NUMS];

    vers = libxl_get_version_info(CTX);
    if (vers == NULL) {
        rc = ERROR_FAIL;
        goto out;
    }

    LOG(DEBUG, "constructing ACPI tables for Xen version %d.%d guest",
        vers->xen_version_major, vers->xen_version_minor);

    dom->acpi_modules[0].data = NULL;
    dom->acpi_modules[0].length = 0;
    dom->acpi_modules[0].guest_addr_out = GUEST_ACPI_BASE;

    rc = libxl__allocate_acpi_tables(gc, info, dom, acpitables);
    if (rc)
        goto out;

    make_acpi_rsdp(gc, dom, acpitables);
    make_acpi_xsdt(gc, dom, acpitables);
    make_acpi_gtdt(gc, dom, acpitables);
    rc = make_acpi_madt(gc, dom, info, acpitables);
    if (rc)
        goto out;

    make_acpi_fadt(gc, dom, acpitables);
    make_acpi_dsdt(gc, dom, acpitables);

out:
    return rc;
}

/*
 * Local variables:
 * mode: C
 * c-basic-offset: 4
 * indent-tabs-mode: nil
 * End:
 */