blob: 4c46dd83a695bbc0d5b5afd312d7af9ad798eeb8 [file] [log] [blame]
Simon Glass2d67fdf2020-04-08 16:57:34 -06001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Tests for ACPI table generation
4 *
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
7 */
8
9#include <common.h>
Simon Glassfaf08c72020-04-26 09:19:53 -060010#include <console.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060011#include <dm.h>
Simon Glass9c442a62020-04-26 09:19:51 -060012#include <malloc.h>
Simon Glass575a5472020-04-26 09:19:50 -060013#include <mapmem.h>
Simon Glassfaf08c72020-04-26 09:19:53 -060014#include <version.h>
Simon Glass9c442a62020-04-26 09:19:51 -060015#include <tables_csum.h>
Simon Glass17968c32020-04-26 09:19:46 -060016#include <version.h>
Simon Glassb2672ea2020-04-08 16:57:38 -060017#include <acpi/acpi_table.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060018#include <dm/acpi.h>
19#include <dm/test.h>
20#include <test/ut.h>
21
22#define ACPI_TEST_DEV_NAME "ABCD"
Simon Glass17968c32020-04-26 09:19:46 -060023#define BUF_SIZE 4096
24
25static int testacpi_write_tables(const struct udevice *dev,
26 struct acpi_ctx *ctx)
27{
28 struct acpi_dmar *dmar;
Simon Glass575a5472020-04-26 09:19:50 -060029 int ret;
Simon Glass17968c32020-04-26 09:19:46 -060030
31 dmar = (struct acpi_dmar *)ctx->current;
32 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
33 ctx->current += sizeof(struct acpi_dmar);
Simon Glass575a5472020-04-26 09:19:50 -060034 ret = acpi_add_table(ctx, dmar);
35 if (ret)
36 return log_msg_ret("add", ret);
Simon Glass17968c32020-04-26 09:19:46 -060037
38 return 0;
39}
Simon Glass2d67fdf2020-04-08 16:57:34 -060040
41static int testacpi_get_name(const struct udevice *dev, char *out_name)
42{
43 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
44}
45
46struct acpi_ops testacpi_ops = {
47 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -060048 .write_tables = testacpi_write_tables,
Simon Glass2d67fdf2020-04-08 16:57:34 -060049};
50
51static const struct udevice_id testacpi_ids[] = {
52 { .compatible = "denx,u-boot-acpi-test" },
53 { }
54};
55
56U_BOOT_DRIVER(testacpi_drv) = {
57 .name = "testacpi_drv",
58 .of_match = testacpi_ids,
59 .id = UCLASS_TEST_ACPI,
60 ACPI_OPS_PTR(&testacpi_ops)
61};
62
63UCLASS_DRIVER(testacpi) = {
64 .name = "testacpi",
65 .id = UCLASS_TEST_ACPI,
66};
67
68/* Test ACPI get_name() */
69static int dm_test_acpi_get_name(struct unit_test_state *uts)
70{
71 char name[ACPI_NAME_MAX];
72 struct udevice *dev;
73
74 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
75 ut_assertok(acpi_get_name(dev, name));
76 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
77
78 return 0;
79}
80DM_TEST(dm_test_acpi_get_name, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -060081
82/* Test acpi_get_table_revision() */
83static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
84{
85 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
86 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
87 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
88 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
89
90 return 0;
91}
92DM_TEST(dm_test_acpi_get_table_revision,
93 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -060094
Simon Glasse9629892020-04-08 16:57:39 -060095/* Test acpi_create_dmar() */
96static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
97{
98 struct acpi_dmar dmar;
99
100 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
101 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
102 ut_asserteq(32 - 1, dmar.host_address_width);
103
104 return 0;
105}
106DM_TEST(dm_test_acpi_create_dmar, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600107
108/* Test acpi_fill_header() */
109static int dm_test_acpi_fill_header(struct unit_test_state *uts)
110{
111 struct acpi_table_header hdr;
112
113 /* Make sure these 5 fields are not changed */
114 hdr.length = 0x11;
115 hdr.revision = 0x22;
116 hdr.checksum = 0x33;
117 hdr.aslc_revision = 0x44;
118 acpi_fill_header(&hdr, "ABCD");
119
120 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
121 ut_asserteq(0x11, hdr.length);
122 ut_asserteq(0x22, hdr.revision);
123 ut_asserteq(0x33, hdr.checksum);
124 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
125 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
126 sizeof(hdr.oem_table_id));
127 ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
128 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
129 ut_asserteq(0x44, hdr.aslc_revision);
130
131 return 0;
132}
133DM_TEST(dm_test_acpi_fill_header, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
134
135/* Test ACPI write_tables() */
136static int dm_test_acpi_write_tables(struct unit_test_state *uts)
137{
138 struct acpi_dmar *dmar;
139 struct acpi_ctx ctx;
140 void *buf;
141
142 buf = malloc(BUF_SIZE);
143 ut_assertnonnull(buf);
144
Simon Glass9c442a62020-04-26 09:19:51 -0600145 acpi_setup_base_tables(&ctx, buf);
Simon Glass575a5472020-04-26 09:19:50 -0600146 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600147 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600148
149 /*
150 * We should have two dmar tables, one for each "denx,u-boot-acpi-test"
151 * device
152 */
153 ut_asserteq_ptr(dmar + 2, ctx.current);
154 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
155 ut_asserteq(32 - 1, dmar->host_address_width);
156
157 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
158 ut_asserteq(32 - 1, dmar[1].host_address_width);
159
Simon Glass9c442a62020-04-26 09:19:51 -0600160 /* Check that the pointers were added correctly */
161 ut_asserteq(map_to_sysmem(dmar), ctx.rsdt->entry[0]);
162 ut_asserteq(map_to_sysmem(dmar + 1), ctx.rsdt->entry[1]);
163 ut_asserteq(0, ctx.rsdt->entry[2]);
164
Simon Glassabeaca82020-04-26 09:19:52 -0600165 ut_asserteq(map_to_sysmem(dmar), ctx.xsdt->entry[0]);
166 ut_asserteq(map_to_sysmem(dmar + 1), ctx.xsdt->entry[1]);
167 ut_asserteq(0, ctx.xsdt->entry[2]);
168
Simon Glass17968c32020-04-26 09:19:46 -0600169 return 0;
170}
171DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600172
173/* Test basic ACPI functions */
174static int dm_test_acpi_basic(struct unit_test_state *uts)
175{
176 struct acpi_ctx ctx;
177
178 /* Check align works */
179 ctx.current = (void *)5;
180 acpi_align(&ctx);
181 ut_asserteq_ptr((void *)16, ctx.current);
182
183 /* Check that align does nothing if already aligned */
184 acpi_align(&ctx);
185 ut_asserteq_ptr((void *)16, ctx.current);
186 acpi_align64(&ctx);
187 ut_asserteq_ptr((void *)64, ctx.current);
188 acpi_align64(&ctx);
189 ut_asserteq_ptr((void *)64, ctx.current);
190
191 /* Check incrementing */
192 acpi_inc(&ctx, 3);
193 ut_asserteq_ptr((void *)67, ctx.current);
194 acpi_inc_align(&ctx, 3);
195 ut_asserteq_ptr((void *)80, ctx.current);
196
197 return 0;
198}
199DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600200
201/* Test acpi_setup_base_tables */
202static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
203{
204 struct acpi_rsdp *rsdp;
205 struct acpi_rsdt *rsdt;
206 struct acpi_xsdt *xsdt;
207 struct acpi_ctx ctx;
208 void *buf, *end;
209
210 /*
211 * Use an unaligned address deliberately, by allocating an aligned
212 * address and then adding 4 to it
213 */
214 buf = memalign(64, BUF_SIZE);
215 ut_assertnonnull(buf);
216 acpi_setup_base_tables(&ctx, buf + 4);
Simon Glassfaf08c72020-04-26 09:19:53 -0600217 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start);
Simon Glass9c442a62020-04-26 09:19:51 -0600218
219 rsdp = buf + 16;
220 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassa3186e62020-05-10 12:52:45 -0600221 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass9c442a62020-04-26 09:19:51 -0600222 ut_asserteq(sizeof(*rsdp), rsdp->length);
223 ut_assertok(table_compute_checksum(rsdp, 20));
224 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
225
226 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
227 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600228 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600229 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
230 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
231
232 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassabeaca82020-04-26 09:19:52 -0600233 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600234 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600235 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
236 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
237
238 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
239 ut_asserteq_ptr(end, ctx.current);
240
241 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
242 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
243
244 return 0;
245}
246DM_TEST(dm_test_acpi_setup_base_tables,
247 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600248
249/* Test 'acpi list' command */
250static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
251{
252 struct acpi_ctx ctx;
253 ulong addr;
254 void *buf;
255
256 buf = memalign(16, BUF_SIZE);
257 ut_assertnonnull(buf);
258 acpi_setup_base_tables(&ctx, buf);
259
260 ut_assertok(acpi_write_dev_tables(&ctx));
261
262 console_record_reset();
263 run_command("acpi list", 0);
264 addr = (ulong)map_to_sysmem(buf);
265 ut_assert_nextline("ACPI tables start at %lx", addr);
266 ut_assert_nextline("RSDP %08lx %06lx (v02 U-BOOT)", addr,
267 sizeof(struct acpi_rsdp));
268 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
269 ut_assert_nextline("RSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
270 addr, sizeof(struct acpi_table_header) +
271 2 * sizeof(u32), U_BOOT_BUILD_DATE);
272 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
273 ut_assert_nextline("XSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
274 addr, sizeof(struct acpi_table_header) +
275 2 * sizeof(u64), U_BOOT_BUILD_DATE);
276 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
277 ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
278 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
279 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
280 ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %u INTL 0)",
281 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
282 ut_assert_console_end();
283
284 return 0;
285}
286DM_TEST(dm_test_acpi_cmd_list, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
287
288/* Test 'acpi dump' command */
289static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
290{
291 struct acpi_ctx ctx;
292 ulong addr;
293 void *buf;
294
295 buf = memalign(16, BUF_SIZE);
296 ut_assertnonnull(buf);
297 acpi_setup_base_tables(&ctx, buf);
298
299 ut_assertok(acpi_write_dev_tables(&ctx));
300
301 /* First search for a non-existent table */
302 console_record_reset();
303 run_command("acpi dump rdst", 0);
304 ut_assert_nextline("Table 'RDST' not found");
305 ut_assert_console_end();
306
307 /* Now a real table */
308 console_record_reset();
309 run_command("acpi dump dmar", 0);
310 addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
311 ut_assert_nextline("DMAR @ %08lx", addr);
312 ut_assert_nextlines_are_dump(0x30);
313 ut_assert_console_end();
314
315 return 0;
316}
317DM_TEST(dm_test_acpi_cmd_dump, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);