blob: 9ff9b685325c3cc9407f36ba9da40c066117dbec [file] [log] [blame]
Simon Glass61cc9332020-07-07 13:11:42 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for ACPI code generation
4 *
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
10#include <dm.h>
11#include <malloc.h>
12#include <acpi/acpigen.h>
13#include <asm/unaligned.h>
14#include <dm/acpi.h>
15#include <dm/test.h>
16#include <test/ut.h>
17
18/* Maximum size of the ACPI context needed for most tests */
19#define ACPI_CONTEXT_SIZE 150
20
21static int alloc_context(struct acpi_ctx **ctxp)
22{
23 struct acpi_ctx *ctx;
24
25 *ctxp = NULL;
26 ctx = malloc(sizeof(*ctx));
27 if (!ctx)
28 return -ENOMEM;
29 ctx->base = malloc(ACPI_CONTEXT_SIZE);
30 if (!ctx->base) {
31 free(ctx);
32 return -ENOMEM;
33 }
34 ctx->current = ctx->base;
35 *ctxp = ctx;
36
37 return 0;
38}
39
40static void free_context(struct acpi_ctx **ctxp)
41{
42 free((*ctxp)->base);
43 free(*ctxp);
44 *ctxp = NULL;
45}
46
47/* Test emitting simple types and acpigen_get_current() */
48static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
49{
50 struct acpi_ctx *ctx;
51 u8 *ptr;
52
53 ut_assertok(alloc_context(&ctx));
54
55 ptr = acpigen_get_current(ctx);
56 acpigen_emit_byte(ctx, 0x23);
57 ut_asserteq(1, acpigen_get_current(ctx) - ptr);
58 ut_asserteq(0x23, *(u8 *)ptr);
59
60 acpigen_emit_word(ctx, 0x1234);
61 ut_asserteq(3, acpigen_get_current(ctx) - ptr);
62 ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1)));
63
64 acpigen_emit_dword(ctx, 0x87654321);
65 ut_asserteq(7, acpigen_get_current(ctx) - ptr);
66 ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3)));
67
68 free_context(&ctx);
69
70 return 0;
71}
72DM_TEST(dm_test_acpi_emit_simple, 0);