1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for ACPI code generation
5 * Copyright 2019 Google LLC
13 #include <acpi/acpigen.h>
14 #include <acpi/acpi_device.h>
15 #include <asm/unaligned.h>
20 /* Maximum size of the ACPI context needed for most tests */
21 #define ACPI_CONTEXT_SIZE 150
23 #define TEST_STRING "frogmore"
24 #define TEST_STREAM2 "\xfa\xde"
26 static int alloc_context(struct acpi_ctx **ctxp)
31 ctx = malloc(sizeof(*ctx));
34 ctx->base = malloc(ACPI_CONTEXT_SIZE);
39 ctx->current = ctx->base;
45 static void free_context(struct acpi_ctx **ctxp)
52 /* Test emitting simple types and acpigen_get_current() */
53 static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
58 ut_assertok(alloc_context(&ctx));
60 ptr = acpigen_get_current(ctx);
61 acpigen_emit_byte(ctx, 0x23);
62 ut_asserteq(1, acpigen_get_current(ctx) - ptr);
63 ut_asserteq(0x23, *(u8 *)ptr);
65 acpigen_emit_word(ctx, 0x1234);
66 ut_asserteq(3, acpigen_get_current(ctx) - ptr);
67 ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1)));
69 acpigen_emit_dword(ctx, 0x87654321);
70 ut_asserteq(7, acpigen_get_current(ctx) - ptr);
71 ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3)));
77 DM_TEST(dm_test_acpi_emit_simple, 0);
79 /* Test emitting a stream */
80 static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
85 ut_assertok(alloc_context(&ctx));
87 ptr = acpigen_get_current(ctx);
88 acpigen_emit_stream(ctx, TEST_STREAM2, 2);
89 ut_asserteq(2, acpigen_get_current(ctx) - ptr);
90 ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
91 ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
97 DM_TEST(dm_test_acpi_emit_stream, 0);
99 /* Test emitting a string */
100 static int dm_test_acpi_emit_string(struct unit_test_state *uts)
102 struct acpi_ctx *ctx;
105 ut_assertok(alloc_context(&ctx));
107 ptr = acpigen_get_current(ctx);
108 acpigen_emit_string(ctx, TEST_STRING);
109 ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
110 ut_asserteq_str(TEST_STRING, (char *)ptr);
116 DM_TEST(dm_test_acpi_emit_string, 0);
118 /* Test emitting an interrupt descriptor */
119 static int dm_test_acpi_interrupt(struct unit_test_state *uts)
121 struct acpi_ctx *ctx;
126 ut_assertok(alloc_context(&ctx));
128 ptr = acpigen_get_current(ctx);
130 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
131 ut_assertok(irq_get_by_index(dev, 0, &irq));
133 /* See a-test, property interrupts-extended in the device tree */
134 ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq));
135 ut_asserteq(9, acpigen_get_current(ctx) - ptr);
136 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
137 ut_asserteq(6, get_unaligned((u16 *)(ptr + 1)));
138 ut_asserteq(0x19, ptr[3]);
139 ut_asserteq(1, ptr[4]);
140 ut_asserteq(3, get_unaligned((u32 *)(ptr + 5)));
146 DM_TEST(dm_test_acpi_interrupt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);