blob: d648f3003a3a13fc3bd28dee3f7a410183fa7c4a [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>
Pali Rohár6e1f0852021-08-02 15:18:38 +020014#include <timestamp.h>
Simon Glassfaf08c72020-04-26 09:19:53 -060015#include <version.h>
Simon Glass9c442a62020-04-26 09:19:51 -060016#include <tables_csum.h>
Simon Glass17968c32020-04-26 09:19:46 -060017#include <version.h>
Simon Glassd43e0ba2020-07-07 13:12:03 -060018#include <acpi/acpigen.h>
Simon Glassebb2e832020-07-07 13:11:39 -060019#include <acpi/acpi_device.h>
Simon Glassb2672ea2020-04-08 16:57:38 -060020#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060022#include <dm/acpi.h>
23#include <dm/test.h>
24#include <test/ut.h>
Simon Glass6fcdaf12020-07-07 21:32:10 -060025#include "acpi.h"
Simon Glass2d67fdf2020-04-08 16:57:34 -060026
Simon Glass17968c32020-04-26 09:19:46 -060027#define BUF_SIZE 4096
28
Pali Rohárf0de20e2021-07-10 13:10:01 +020029#define OEM_REVISION ((((U_BOOT_VERSION_NUM / 1000) % 10) << 28) | \
30 (((U_BOOT_VERSION_NUM / 100) % 10) << 24) | \
31 (((U_BOOT_VERSION_NUM / 10) % 10) << 20) | \
32 ((U_BOOT_VERSION_NUM % 10) << 16) | \
33 (((U_BOOT_VERSION_NUM_PATCH / 10) % 10) << 12) | \
34 ((U_BOOT_VERSION_NUM_PATCH % 10) << 8) | \
35 0x01)
36
Simon Glassebb2e832020-07-07 13:11:39 -060037/**
Simon Glassb75b15b2020-12-03 16:55:23 -070038 * struct testacpi_plat - Platform data for the test ACPI device
Simon Glassebb2e832020-07-07 13:11:39 -060039 *
40 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
41 * @return_error: true to return an error instead of a name
42 */
Simon Glassb75b15b2020-12-03 16:55:23 -070043struct testacpi_plat {
Simon Glassebb2e832020-07-07 13:11:39 -060044 bool return_error;
45 bool no_name;
46};
47
Simon Glass37acc662021-12-01 09:02:48 -070048/**
49 * setup_ctx_and_base_tables() - Set up context along with RSDP, RSDT and XSDT
50 *
51 * Set up the context with the given start position. Some basic tables are
52 * always needed, so set them up as well.
53 *
54 * @ctx: Context to set up
55 */
Simon Glasse9f6e6a2021-12-01 09:02:50 -070056static int setup_ctx_and_base_tables(struct unit_test_state *uts,
57 struct acpi_ctx *ctx, ulong start)
Simon Glass37acc662021-12-01 09:02:48 -070058{
Simon Glasse9f6e6a2021-12-01 09:02:50 -070059 struct acpi_writer *entry = ACPI_WRITER_GET(0base);
60
Simon Glass37acc662021-12-01 09:02:48 -070061 acpi_setup_ctx(ctx, start);
Simon Glasse9f6e6a2021-12-01 09:02:50 -070062
63 ctx->tab_start = ctx->current;
64 ut_assertok(acpi_write_one(ctx, entry));
Simon Glass37acc662021-12-01 09:02:48 -070065
66 return 0;
67}
68
Simon Glass17968c32020-04-26 09:19:46 -060069static int testacpi_write_tables(const struct udevice *dev,
70 struct acpi_ctx *ctx)
71{
72 struct acpi_dmar *dmar;
Simon Glass575a5472020-04-26 09:19:50 -060073 int ret;
Simon Glass17968c32020-04-26 09:19:46 -060074
75 dmar = (struct acpi_dmar *)ctx->current;
76 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
77 ctx->current += sizeof(struct acpi_dmar);
Simon Glass575a5472020-04-26 09:19:50 -060078 ret = acpi_add_table(ctx, dmar);
79 if (ret)
80 return log_msg_ret("add", ret);
Simon Glass17968c32020-04-26 09:19:46 -060081
82 return 0;
83}
Simon Glass2d67fdf2020-04-08 16:57:34 -060084
85static int testacpi_get_name(const struct udevice *dev, char *out_name)
86{
Simon Glassb75b15b2020-12-03 16:55:23 -070087 struct testacpi_plat *plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -060088
89 if (plat->return_error)
90 return -EINVAL;
91 if (plat->no_name) {
92 *out_name = '\0';
93 return 0;
94 }
95 if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
96 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
97 else
98 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
Simon Glass2d67fdf2020-04-08 16:57:34 -060099}
100
Simon Glassd43e0ba2020-07-07 13:12:03 -0600101static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
102{
103 const char *data;
104
105 data = dev_read_string(dev, "acpi-ssdt-test-data");
106 if (data) {
107 while (*data)
108 acpigen_emit_byte(ctx, *data++);
109 }
110
111 return 0;
112}
113
Simon Glass990cd5b2020-07-07 13:12:08 -0600114static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
115{
116 const char *data;
117
118 data = dev_read_string(dev, "acpi-dsdt-test-data");
119 if (data) {
120 while (*data)
121 acpigen_emit_byte(ctx, *data++);
122 }
123
124 return 0;
125}
126
Simon Glass2d67fdf2020-04-08 16:57:34 -0600127struct acpi_ops testacpi_ops = {
128 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -0600129 .write_tables = testacpi_write_tables,
Simon Glassd43e0ba2020-07-07 13:12:03 -0600130 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass990cd5b2020-07-07 13:12:08 -0600131 .inject_dsdt = testacpi_inject_dsdt,
Simon Glass2d67fdf2020-04-08 16:57:34 -0600132};
133
134static const struct udevice_id testacpi_ids[] = {
135 { .compatible = "denx,u-boot-acpi-test" },
136 { }
137};
138
139U_BOOT_DRIVER(testacpi_drv) = {
140 .name = "testacpi_drv",
141 .of_match = testacpi_ids,
142 .id = UCLASS_TEST_ACPI,
Simon Glassebb2e832020-07-07 13:11:39 -0600143 .bind = dm_scan_fdt_dev,
Simon Glassb75b15b2020-12-03 16:55:23 -0700144 .plat_auto = sizeof(struct testacpi_plat),
Simon Glass2d67fdf2020-04-08 16:57:34 -0600145 ACPI_OPS_PTR(&testacpi_ops)
146};
147
148UCLASS_DRIVER(testacpi) = {
149 .name = "testacpi",
150 .id = UCLASS_TEST_ACPI,
151};
152
153/* Test ACPI get_name() */
154static int dm_test_acpi_get_name(struct unit_test_state *uts)
155{
156 char name[ACPI_NAME_MAX];
Simon Glass9a655132020-12-16 21:20:23 -0700157 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glass09642392020-07-07 13:12:11 -0600158 struct udevice *pci, *root;
Simon Glass2d67fdf2020-04-08 16:57:34 -0600159
Simon Glass09642392020-07-07 13:12:11 -0600160 /* Test getting the name from the driver */
Simon Glass2d67fdf2020-04-08 16:57:34 -0600161 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
162 ut_assertok(acpi_get_name(dev, name));
163 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
164
Simon Glass09642392020-07-07 13:12:11 -0600165 /* Test getting the name from the device tree */
166 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
167 &dev2));
168 ut_assertok(acpi_get_name(dev2, name));
169 ut_asserteq_str("GHIJ", name);
170
171 /* Test getting the name from acpi_device_get_name() */
172 ut_assertok(uclass_first_device(UCLASS_I2C, &i2c));
173 ut_assertok(acpi_get_name(i2c, name));
174 ut_asserteq_str("I2C0", name);
175
176 ut_assertok(uclass_first_device(UCLASS_SPI, &spi));
177 ut_assertok(acpi_get_name(spi, name));
178 ut_asserteq_str("SPI0", name);
179
Simon Glass09642392020-07-07 13:12:11 -0600180 /* ACPI doesn't know about the timer */
181 ut_assertok(uclass_first_device(UCLASS_TIMER, &timer));
182 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
183
184 /* May as well test the rest of the cases */
185 ut_assertok(uclass_first_device(UCLASS_SOUND, &sound));
186 ut_assertok(acpi_get_name(sound, name));
187 ut_asserteq_str("HDAS", name);
188
189 ut_assertok(uclass_first_device(UCLASS_PCI, &pci));
190 ut_assertok(acpi_get_name(pci, name));
191 ut_asserteq_str("PCI0", name);
192
193 ut_assertok(uclass_first_device(UCLASS_ROOT, &root));
194 ut_assertok(acpi_get_name(root, name));
195 ut_asserteq_str("\\_SB", name);
196
197 /* Note that we don't have tests for acpi_name_from_id() */
198
Simon Glass2d67fdf2020-04-08 16:57:34 -0600199 return 0;
200}
Simon Glass974dccd2020-07-28 19:41:12 -0600201DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -0600202
203/* Test acpi_get_table_revision() */
204static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
205{
206 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
207 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
208 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
209 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
210
211 return 0;
212}
213DM_TEST(dm_test_acpi_get_table_revision,
Simon Glass974dccd2020-07-28 19:41:12 -0600214 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -0600215
Simon Glasse9629892020-04-08 16:57:39 -0600216/* Test acpi_create_dmar() */
217static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
218{
219 struct acpi_dmar dmar;
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200220 struct udevice *cpu;
Simon Glasse9629892020-04-08 16:57:39 -0600221
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200222 ut_assertok(uclass_first_device(UCLASS_CPU, &cpu));
223 ut_assertnonnull(cpu);
Simon Glasse9629892020-04-08 16:57:39 -0600224 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
225 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
226 ut_asserteq(32 - 1, dmar.host_address_width);
227
228 return 0;
229}
Simon Glass974dccd2020-07-28 19:41:12 -0600230DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600231
232/* Test acpi_fill_header() */
233static int dm_test_acpi_fill_header(struct unit_test_state *uts)
234{
235 struct acpi_table_header hdr;
236
237 /* Make sure these 5 fields are not changed */
238 hdr.length = 0x11;
239 hdr.revision = 0x22;
240 hdr.checksum = 0x33;
241 hdr.aslc_revision = 0x44;
242 acpi_fill_header(&hdr, "ABCD");
243
244 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
245 ut_asserteq(0x11, hdr.length);
246 ut_asserteq(0x22, hdr.revision);
247 ut_asserteq(0x33, hdr.checksum);
248 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
249 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
250 sizeof(hdr.oem_table_id));
Pali Rohárf0de20e2021-07-10 13:10:01 +0200251 ut_asserteq(OEM_REVISION, hdr.oem_revision);
Simon Glass17968c32020-04-26 09:19:46 -0600252 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
253 ut_asserteq(0x44, hdr.aslc_revision);
254
255 return 0;
256}
Simon Glass974dccd2020-07-28 19:41:12 -0600257DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600258
259/* Test ACPI write_tables() */
260static int dm_test_acpi_write_tables(struct unit_test_state *uts)
261{
262 struct acpi_dmar *dmar;
263 struct acpi_ctx ctx;
Simon Glass37acc662021-12-01 09:02:48 -0700264 ulong addr;
Simon Glass17968c32020-04-26 09:19:46 -0600265 void *buf;
Simon Glassebb2e832020-07-07 13:11:39 -0600266 int i;
Simon Glass17968c32020-04-26 09:19:46 -0600267
268 buf = malloc(BUF_SIZE);
269 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700270 addr = map_to_sysmem(buf);
Simon Glass17968c32020-04-26 09:19:46 -0600271
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700272 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass575a5472020-04-26 09:19:50 -0600273 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600274 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600275
276 /*
Simon Glassebb2e832020-07-07 13:11:39 -0600277 * We should have three dmar tables, one for each
278 * "denx,u-boot-acpi-test" device
Simon Glass17968c32020-04-26 09:19:46 -0600279 */
Simon Glassebb2e832020-07-07 13:11:39 -0600280 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass17968c32020-04-26 09:19:46 -0600281 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
282 ut_asserteq(32 - 1, dmar->host_address_width);
283
284 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
285 ut_asserteq(32 - 1, dmar[1].host_address_width);
286
Simon Glassebb2e832020-07-07 13:11:39 -0600287 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
288 ut_asserteq(32 - 1, dmar[2].host_address_width);
Simon Glass9c442a62020-04-26 09:19:51 -0600289
Simon Glassebb2e832020-07-07 13:11:39 -0600290 /* Check that the pointers were added correctly */
291 for (i = 0; i < 3; i++) {
292 ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
293 ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
294 }
295 ut_asserteq(0, ctx.rsdt->entry[3]);
296 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassabeaca82020-04-26 09:19:52 -0600297
Simon Glass17968c32020-04-26 09:19:46 -0600298 return 0;
299}
Simon Glass974dccd2020-07-28 19:41:12 -0600300DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600301
302/* Test basic ACPI functions */
303static int dm_test_acpi_basic(struct unit_test_state *uts)
304{
305 struct acpi_ctx ctx;
306
307 /* Check align works */
308 ctx.current = (void *)5;
309 acpi_align(&ctx);
310 ut_asserteq_ptr((void *)16, ctx.current);
311
312 /* Check that align does nothing if already aligned */
313 acpi_align(&ctx);
314 ut_asserteq_ptr((void *)16, ctx.current);
315 acpi_align64(&ctx);
316 ut_asserteq_ptr((void *)64, ctx.current);
317 acpi_align64(&ctx);
318 ut_asserteq_ptr((void *)64, ctx.current);
319
320 /* Check incrementing */
321 acpi_inc(&ctx, 3);
322 ut_asserteq_ptr((void *)67, ctx.current);
323 acpi_inc_align(&ctx, 3);
324 ut_asserteq_ptr((void *)80, ctx.current);
325
326 return 0;
327}
Simon Glass974dccd2020-07-28 19:41:12 -0600328DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600329
Simon Glass16170882021-12-01 09:02:49 -0700330/* Test setup_ctx_and_base_tables */
331static int dm_test_setup_ctx_and_base_tables(struct unit_test_state *uts)
Simon Glass9c442a62020-04-26 09:19:51 -0600332{
333 struct acpi_rsdp *rsdp;
334 struct acpi_rsdt *rsdt;
335 struct acpi_xsdt *xsdt;
336 struct acpi_ctx ctx;
337 void *buf, *end;
Simon Glass37acc662021-12-01 09:02:48 -0700338 ulong addr;
Simon Glass9c442a62020-04-26 09:19:51 -0600339
340 /*
341 * Use an unaligned address deliberately, by allocating an aligned
342 * address and then adding 4 to it
343 */
344 buf = memalign(64, BUF_SIZE);
345 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700346 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700347 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
Simon Glassb33f2f92021-12-01 09:02:37 -0700348 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());
Simon Glass9c442a62020-04-26 09:19:51 -0600349
350 rsdp = buf + 16;
351 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassa3186e62020-05-10 12:52:45 -0600352 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass9c442a62020-04-26 09:19:51 -0600353 ut_asserteq(sizeof(*rsdp), rsdp->length);
354 ut_assertok(table_compute_checksum(rsdp, 20));
355 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
356
357 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
358 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600359 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600360 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
361 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
362
363 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassabeaca82020-04-26 09:19:52 -0600364 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600365 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600366 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
367 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
368
369 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
370 ut_asserteq_ptr(end, ctx.current);
371
372 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
373 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
374
375 return 0;
376}
Simon Glass16170882021-12-01 09:02:49 -0700377DM_TEST(dm_test_setup_ctx_and_base_tables,
Simon Glass974dccd2020-07-28 19:41:12 -0600378 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600379
380/* Test 'acpi list' command */
381static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
382{
383 struct acpi_ctx ctx;
384 ulong addr;
385 void *buf;
386
387 buf = memalign(16, BUF_SIZE);
388 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700389 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700390 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600391
392 ut_assertok(acpi_write_dev_tables(&ctx));
393
394 console_record_reset();
395 run_command("acpi list", 0);
Simon Glassfaf08c72020-04-26 09:19:53 -0600396 ut_assert_nextline("ACPI tables start at %lx", addr);
Simon Glass60e08ee2021-02-07 14:27:05 -0700397 ut_assert_nextline("RSDP %08lx %06zx (v02 U-BOOT)", addr,
Simon Glassfaf08c72020-04-26 09:19:53 -0600398 sizeof(struct acpi_rsdp));
399 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Simon Glass60e08ee2021-02-07 14:27:05 -0700400 ut_assert_nextline("RSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600401 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200402 3 * sizeof(u32), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600403 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Simon Glass60e08ee2021-02-07 14:27:05 -0700404 ut_assert_nextline("XSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600405 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200406 3 * sizeof(u64), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600407 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Simon Glass60e08ee2021-02-07 14:27:05 -0700408 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200409 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600410 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass60e08ee2021-02-07 14:27:05 -0700411 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200412 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassebb2e832020-07-07 13:11:39 -0600413 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass60e08ee2021-02-07 14:27:05 -0700414 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200415 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600416 ut_assert_console_end();
417
418 return 0;
419}
Simon Glass974dccd2020-07-28 19:41:12 -0600420DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600421
422/* Test 'acpi dump' command */
423static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
424{
425 struct acpi_ctx ctx;
426 ulong addr;
427 void *buf;
428
429 buf = memalign(16, BUF_SIZE);
430 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700431 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700432 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600433
434 ut_assertok(acpi_write_dev_tables(&ctx));
435
436 /* First search for a non-existent table */
437 console_record_reset();
438 run_command("acpi dump rdst", 0);
439 ut_assert_nextline("Table 'RDST' not found");
440 ut_assert_console_end();
441
442 /* Now a real table */
443 console_record_reset();
444 run_command("acpi dump dmar", 0);
445 addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
446 ut_assert_nextline("DMAR @ %08lx", addr);
447 ut_assert_nextlines_are_dump(0x30);
448 ut_assert_console_end();
449
450 return 0;
451}
Simon Glass974dccd2020-07-28 19:41:12 -0600452DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassebb2e832020-07-07 13:11:39 -0600453
454/* Test acpi_device_path() */
455static int dm_test_acpi_device_path(struct unit_test_state *uts)
456{
Simon Glassb75b15b2020-12-03 16:55:23 -0700457 struct testacpi_plat *plat;
Simon Glassebb2e832020-07-07 13:11:39 -0600458 char buf[ACPI_PATH_MAX];
459 struct udevice *dev, *child;
460
461 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
462 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
463 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
464
465 /* Test running out of space */
466 buf[5] = '\0';
467 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
468 ut_asserteq('\0', buf[5]);
469
470 /* Test a three-component name */
471 ut_assertok(device_first_child_err(dev, &child));
472 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
473 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
474 buf);
475
476 /* Test handling of a device which doesn't produce a name */
Simon Glassfa20e932020-12-03 16:55:20 -0700477 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600478 plat->no_name = true;
479 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
480 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
481
482 /* Test handling of a device which returns an error */
Simon Glassfa20e932020-12-03 16:55:20 -0700483 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600484 plat->return_error = true;
485 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
486
487 return 0;
488}
Simon Glass974dccd2020-07-28 19:41:12 -0600489DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass8965cc92020-07-07 13:11:40 -0600490
491/* Test acpi_device_status() */
492static int dm_test_acpi_device_status(struct unit_test_state *uts)
493{
494 struct udevice *dev;
495
496 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
497 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
498
499 return 0;
500}
Simon Glass974dccd2020-07-28 19:41:12 -0600501DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600502
503/* Test acpi_fill_ssdt() */
504static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
505{
506 struct acpi_ctx ctx;
507 u8 *buf;
508
509 buf = malloc(BUF_SIZE);
510 ut_assertnonnull(buf);
511
Simon Glassa385d492020-11-04 09:57:33 -0700512 acpi_reset_items();
Simon Glassd43e0ba2020-07-07 13:12:03 -0600513 ctx.current = buf;
514 buf[4] = 'z'; /* sentinel */
515 ut_assertok(acpi_fill_ssdt(&ctx));
516
Simon Glass3c601b12020-07-07 13:12:06 -0600517 /*
518 * These values come from acpi-test2's acpi-ssdt-test-data property.
519 * This device comes first because of u-boot,acpi-ssdt-order
520 */
521 ut_asserteq('c', buf[0]);
522 ut_asserteq('d', buf[1]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600523
Simon Glass3c601b12020-07-07 13:12:06 -0600524 /* These values come from acpi-test's acpi-ssdt-test-data property */
525 ut_asserteq('a', buf[2]);
526 ut_asserteq('b', buf[3]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600527
528 ut_asserteq('z', buf[4]);
529
530 return 0;
531}
Simon Glass974dccd2020-07-28 19:41:12 -0600532DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass990cd5b2020-07-07 13:12:08 -0600533
534/* Test acpi_inject_dsdt() */
535static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
536{
537 struct acpi_ctx ctx;
538 u8 *buf;
539
540 buf = malloc(BUF_SIZE);
541 ut_assertnonnull(buf);
542
Simon Glassa385d492020-11-04 09:57:33 -0700543 acpi_reset_items();
Simon Glass990cd5b2020-07-07 13:12:08 -0600544 ctx.current = buf;
545 buf[4] = 'z'; /* sentinel */
546 ut_assertok(acpi_inject_dsdt(&ctx));
547
548 /*
549 * These values come from acpi-test's acpi-dsdt-test-data property.
550 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
551 */
552 ut_asserteq('h', buf[0]);
553 ut_asserteq('i', buf[1]);
554
555 /* These values come from acpi-test's acpi-dsdt-test-data property */
556 ut_asserteq('j', buf[2]);
557 ut_asserteq('k', buf[3]);
558
559 ut_asserteq('z', buf[4]);
560
561 return 0;
562}
Simon Glass974dccd2020-07-28 19:41:12 -0600563DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600564
565/* Test 'acpi items' command */
566static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
567{
568 struct acpi_ctx ctx;
Simon Glass884dcfa2021-12-01 09:03:05 -0700569 ulong addr;
Simon Glass64bbc0d2020-07-07 13:12:12 -0600570 void *buf;
571
572 buf = malloc(BUF_SIZE);
573 ut_assertnonnull(buf);
Simon Glass884dcfa2021-12-01 09:03:05 -0700574 addr = map_to_sysmem(buf);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600575
Simon Glassa385d492020-11-04 09:57:33 -0700576 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600577 ctx.current = buf;
578 ut_assertok(acpi_fill_ssdt(&ctx));
579 console_record_reset();
580 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700581 ut_assert_nextline("Seq Type Base Size Device/Writer");
582 ut_assert_nextline("--- ----- -------- ---- -------------");
583 ut_assert_nextline(" 0 ssdt %8lx 2 acpi-test", addr);
584 ut_assert_nextline(" 1 ssdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600585 ut_assert_console_end();
586
Simon Glassa385d492020-11-04 09:57:33 -0700587 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600588 ctx.current = buf;
589 ut_assertok(acpi_inject_dsdt(&ctx));
590 console_record_reset();
591 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700592 ut_assert_nextlinen("Seq");
593 ut_assert_nextlinen("---");
594 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
595 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600596 ut_assert_console_end();
597
598 console_record_reset();
599 run_command("acpi items -d", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700600 ut_assert_nextlinen("Seq");
601 ut_assert_nextlinen("---");
602 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600603 ut_assert_nextlines_are_dump(2);
604 ut_assert_nextline("%s", "");
Simon Glass884dcfa2021-12-01 09:03:05 -0700605 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600606 ut_assert_nextlines_are_dump(2);
607 ut_assert_nextline("%s", "");
608 ut_assert_console_end();
609
610 return 0;
611}
Simon Glass974dccd2020-07-28 19:41:12 -0600612DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);