blob: 6f0025814ed139881cbec273a31ba25ac01ac882 [file] [log] [blame]
Simon Glassf50cc952020-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 Glass0b885bc2020-04-26 09:19:53 -060010#include <console.h>
Simon Glassf50cc952020-04-08 16:57:34 -060011#include <dm.h>
Simon Glass7e586f62020-04-26 09:19:51 -060012#include <malloc.h>
Simon Glass29b35112020-04-26 09:19:50 -060013#include <mapmem.h>
Pali Rohár121a1652021-08-02 15:18:38 +020014#include <timestamp.h>
Simon Glass0b885bc2020-04-26 09:19:53 -060015#include <version.h>
Simon Glass7e586f62020-04-26 09:19:51 -060016#include <tables_csum.h>
Simon Glass93f7f822020-04-26 09:19:46 -060017#include <version.h>
Simon Glassb5183172020-07-07 13:12:03 -060018#include <acpi/acpigen.h>
Simon Glass1361a532020-07-07 13:11:39 -060019#include <acpi/acpi_device.h>
Simon Glass91fe8b72020-04-08 16:57:38 -060020#include <acpi/acpi_table.h>
Simon Glass401d1c42020-10-30 21:38:53 -060021#include <asm/global_data.h>
Simon Glassf50cc952020-04-08 16:57:34 -060022#include <dm/acpi.h>
23#include <dm/test.h>
24#include <test/ut.h>
Simon Glass82659cc2020-07-07 21:32:10 -060025#include "acpi.h"
Simon Glassf50cc952020-04-08 16:57:34 -060026
Simon Glass93f7f822020-04-26 09:19:46 -060027#define BUF_SIZE 4096
28
Simon Glass1361a532020-07-07 13:11:39 -060029/**
Simon Glass8a8d24b2020-12-03 16:55:23 -070030 * struct testacpi_plat - Platform data for the test ACPI device
Simon Glass1361a532020-07-07 13:11:39 -060031 *
32 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
33 * @return_error: true to return an error instead of a name
34 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070035struct testacpi_plat {
Simon Glass1361a532020-07-07 13:11:39 -060036 bool return_error;
37 bool no_name;
38};
39
Simon Glass93f7f822020-04-26 09:19:46 -060040static int testacpi_write_tables(const struct udevice *dev,
41 struct acpi_ctx *ctx)
42{
43 struct acpi_dmar *dmar;
Simon Glass29b35112020-04-26 09:19:50 -060044 int ret;
Simon Glass93f7f822020-04-26 09:19:46 -060045
46 dmar = (struct acpi_dmar *)ctx->current;
47 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
48 ctx->current += sizeof(struct acpi_dmar);
Simon Glass29b35112020-04-26 09:19:50 -060049 ret = acpi_add_table(ctx, dmar);
50 if (ret)
51 return log_msg_ret("add", ret);
Simon Glass93f7f822020-04-26 09:19:46 -060052
53 return 0;
54}
Simon Glassf50cc952020-04-08 16:57:34 -060055
56static int testacpi_get_name(const struct udevice *dev, char *out_name)
57{
Simon Glass8a8d24b2020-12-03 16:55:23 -070058 struct testacpi_plat *plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -060059
60 if (plat->return_error)
61 return -EINVAL;
62 if (plat->no_name) {
63 *out_name = '\0';
64 return 0;
65 }
66 if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
67 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
68 else
69 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
Simon Glassf50cc952020-04-08 16:57:34 -060070}
71
Simon Glassb5183172020-07-07 13:12:03 -060072static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
73{
74 const char *data;
75
76 data = dev_read_string(dev, "acpi-ssdt-test-data");
77 if (data) {
78 while (*data)
79 acpigen_emit_byte(ctx, *data++);
80 }
81
82 return 0;
83}
84
Simon Glass01694582020-07-07 13:12:08 -060085static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
86{
87 const char *data;
88
89 data = dev_read_string(dev, "acpi-dsdt-test-data");
90 if (data) {
91 while (*data)
92 acpigen_emit_byte(ctx, *data++);
93 }
94
95 return 0;
96}
97
Simon Glassf50cc952020-04-08 16:57:34 -060098struct acpi_ops testacpi_ops = {
99 .get_name = testacpi_get_name,
Simon Glass93f7f822020-04-26 09:19:46 -0600100 .write_tables = testacpi_write_tables,
Simon Glassb5183172020-07-07 13:12:03 -0600101 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass01694582020-07-07 13:12:08 -0600102 .inject_dsdt = testacpi_inject_dsdt,
Simon Glassf50cc952020-04-08 16:57:34 -0600103};
104
105static const struct udevice_id testacpi_ids[] = {
106 { .compatible = "denx,u-boot-acpi-test" },
107 { }
108};
109
110U_BOOT_DRIVER(testacpi_drv) = {
111 .name = "testacpi_drv",
112 .of_match = testacpi_ids,
113 .id = UCLASS_TEST_ACPI,
Simon Glass1361a532020-07-07 13:11:39 -0600114 .bind = dm_scan_fdt_dev,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700115 .plat_auto = sizeof(struct testacpi_plat),
Simon Glassf50cc952020-04-08 16:57:34 -0600116 ACPI_OPS_PTR(&testacpi_ops)
117};
118
119UCLASS_DRIVER(testacpi) = {
120 .name = "testacpi",
121 .id = UCLASS_TEST_ACPI,
122};
123
124/* Test ACPI get_name() */
125static int dm_test_acpi_get_name(struct unit_test_state *uts)
126{
127 char name[ACPI_NAME_MAX];
Simon Glassd1e85302020-12-16 21:20:23 -0700128 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glassfefac0b2020-07-07 13:12:11 -0600129 struct udevice *pci, *root;
Simon Glassf50cc952020-04-08 16:57:34 -0600130
Simon Glassfefac0b2020-07-07 13:12:11 -0600131 /* Test getting the name from the driver */
Simon Glassf50cc952020-04-08 16:57:34 -0600132 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
133 ut_assertok(acpi_get_name(dev, name));
134 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
135
Simon Glassfefac0b2020-07-07 13:12:11 -0600136 /* Test getting the name from the device tree */
137 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
138 &dev2));
139 ut_assertok(acpi_get_name(dev2, name));
140 ut_asserteq_str("GHIJ", name);
141
142 /* Test getting the name from acpi_device_get_name() */
143 ut_assertok(uclass_first_device(UCLASS_I2C, &i2c));
144 ut_assertok(acpi_get_name(i2c, name));
145 ut_asserteq_str("I2C0", name);
146
147 ut_assertok(uclass_first_device(UCLASS_SPI, &spi));
148 ut_assertok(acpi_get_name(spi, name));
149 ut_asserteq_str("SPI0", name);
150
Simon Glassfefac0b2020-07-07 13:12:11 -0600151 /* ACPI doesn't know about the timer */
152 ut_assertok(uclass_first_device(UCLASS_TIMER, &timer));
153 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
154
155 /* May as well test the rest of the cases */
156 ut_assertok(uclass_first_device(UCLASS_SOUND, &sound));
157 ut_assertok(acpi_get_name(sound, name));
158 ut_asserteq_str("HDAS", name);
159
160 ut_assertok(uclass_first_device(UCLASS_PCI, &pci));
161 ut_assertok(acpi_get_name(pci, name));
162 ut_asserteq_str("PCI0", name);
163
164 ut_assertok(uclass_first_device(UCLASS_ROOT, &root));
165 ut_assertok(acpi_get_name(root, name));
166 ut_asserteq_str("\\_SB", name);
167
168 /* Note that we don't have tests for acpi_name_from_id() */
169
Simon Glassf50cc952020-04-08 16:57:34 -0600170 return 0;
171}
Simon Glasse180c2b2020-07-28 19:41:12 -0600172DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass91fe8b72020-04-08 16:57:38 -0600173
174/* Test acpi_get_table_revision() */
175static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
176{
177 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
178 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
179 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
180 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
181
182 return 0;
183}
184DM_TEST(dm_test_acpi_get_table_revision,
Simon Glasse180c2b2020-07-28 19:41:12 -0600185 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassbfeb5d42020-04-08 16:57:39 -0600186
Simon Glassbfeb5d42020-04-08 16:57:39 -0600187/* Test acpi_create_dmar() */
188static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
189{
190 struct acpi_dmar dmar;
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200191 struct udevice *cpu;
Simon Glassbfeb5d42020-04-08 16:57:39 -0600192
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200193 ut_assertok(uclass_first_device(UCLASS_CPU, &cpu));
194 ut_assertnonnull(cpu);
Simon Glassbfeb5d42020-04-08 16:57:39 -0600195 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
196 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
197 ut_asserteq(32 - 1, dmar.host_address_width);
198
199 return 0;
200}
Simon Glasse180c2b2020-07-28 19:41:12 -0600201DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass93f7f822020-04-26 09:19:46 -0600202
203/* Test acpi_fill_header() */
204static int dm_test_acpi_fill_header(struct unit_test_state *uts)
205{
206 struct acpi_table_header hdr;
207
208 /* Make sure these 5 fields are not changed */
209 hdr.length = 0x11;
210 hdr.revision = 0x22;
211 hdr.checksum = 0x33;
212 hdr.aslc_revision = 0x44;
213 acpi_fill_header(&hdr, "ABCD");
214
215 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
216 ut_asserteq(0x11, hdr.length);
217 ut_asserteq(0x22, hdr.revision);
218 ut_asserteq(0x33, hdr.checksum);
219 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
220 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
221 sizeof(hdr.oem_table_id));
222 ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
223 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
224 ut_asserteq(0x44, hdr.aslc_revision);
225
226 return 0;
227}
Simon Glasse180c2b2020-07-28 19:41:12 -0600228DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass93f7f822020-04-26 09:19:46 -0600229
230/* Test ACPI write_tables() */
231static int dm_test_acpi_write_tables(struct unit_test_state *uts)
232{
233 struct acpi_dmar *dmar;
234 struct acpi_ctx ctx;
235 void *buf;
Simon Glass1361a532020-07-07 13:11:39 -0600236 int i;
Simon Glass93f7f822020-04-26 09:19:46 -0600237
238 buf = malloc(BUF_SIZE);
239 ut_assertnonnull(buf);
240
Simon Glass7e586f62020-04-26 09:19:51 -0600241 acpi_setup_base_tables(&ctx, buf);
Simon Glass29b35112020-04-26 09:19:50 -0600242 dmar = ctx.current;
Simon Glass93f7f822020-04-26 09:19:46 -0600243 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass93f7f822020-04-26 09:19:46 -0600244
245 /*
Simon Glass1361a532020-07-07 13:11:39 -0600246 * We should have three dmar tables, one for each
247 * "denx,u-boot-acpi-test" device
Simon Glass93f7f822020-04-26 09:19:46 -0600248 */
Simon Glass1361a532020-07-07 13:11:39 -0600249 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass93f7f822020-04-26 09:19:46 -0600250 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
251 ut_asserteq(32 - 1, dmar->host_address_width);
252
253 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
254 ut_asserteq(32 - 1, dmar[1].host_address_width);
255
Simon Glass1361a532020-07-07 13:11:39 -0600256 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
257 ut_asserteq(32 - 1, dmar[2].host_address_width);
Simon Glass7e586f62020-04-26 09:19:51 -0600258
Simon Glass1361a532020-07-07 13:11:39 -0600259 /* Check that the pointers were added correctly */
260 for (i = 0; i < 3; i++) {
261 ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
262 ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
263 }
264 ut_asserteq(0, ctx.rsdt->entry[3]);
265 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassb38309b2020-04-26 09:19:52 -0600266
Simon Glass93f7f822020-04-26 09:19:46 -0600267 return 0;
268}
Simon Glasse180c2b2020-07-28 19:41:12 -0600269DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass86e17782020-04-26 09:19:47 -0600270
271/* Test basic ACPI functions */
272static int dm_test_acpi_basic(struct unit_test_state *uts)
273{
274 struct acpi_ctx ctx;
275
276 /* Check align works */
277 ctx.current = (void *)5;
278 acpi_align(&ctx);
279 ut_asserteq_ptr((void *)16, ctx.current);
280
281 /* Check that align does nothing if already aligned */
282 acpi_align(&ctx);
283 ut_asserteq_ptr((void *)16, ctx.current);
284 acpi_align64(&ctx);
285 ut_asserteq_ptr((void *)64, ctx.current);
286 acpi_align64(&ctx);
287 ut_asserteq_ptr((void *)64, ctx.current);
288
289 /* Check incrementing */
290 acpi_inc(&ctx, 3);
291 ut_asserteq_ptr((void *)67, ctx.current);
292 acpi_inc_align(&ctx, 3);
293 ut_asserteq_ptr((void *)80, ctx.current);
294
295 return 0;
296}
Simon Glasse180c2b2020-07-28 19:41:12 -0600297DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7e586f62020-04-26 09:19:51 -0600298
299/* Test acpi_setup_base_tables */
300static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
301{
302 struct acpi_rsdp *rsdp;
303 struct acpi_rsdt *rsdt;
304 struct acpi_xsdt *xsdt;
305 struct acpi_ctx ctx;
306 void *buf, *end;
307
308 /*
309 * Use an unaligned address deliberately, by allocating an aligned
310 * address and then adding 4 to it
311 */
312 buf = memalign(64, BUF_SIZE);
313 ut_assertnonnull(buf);
314 acpi_setup_base_tables(&ctx, buf + 4);
Simon Glass0b885bc2020-04-26 09:19:53 -0600315 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start);
Simon Glass7e586f62020-04-26 09:19:51 -0600316
317 rsdp = buf + 16;
318 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassf91f3662020-05-10 12:52:45 -0600319 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass7e586f62020-04-26 09:19:51 -0600320 ut_asserteq(sizeof(*rsdp), rsdp->length);
321 ut_assertok(table_compute_checksum(rsdp, 20));
322 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
323
324 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
325 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassf91f3662020-05-10 12:52:45 -0600326 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass7e586f62020-04-26 09:19:51 -0600327 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
328 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
329
330 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassb38309b2020-04-26 09:19:52 -0600331 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassf91f3662020-05-10 12:52:45 -0600332 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass7e586f62020-04-26 09:19:51 -0600333 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
334 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
335
336 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
337 ut_asserteq_ptr(end, ctx.current);
338
339 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
340 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
341
342 return 0;
343}
344DM_TEST(dm_test_acpi_setup_base_tables,
Simon Glasse180c2b2020-07-28 19:41:12 -0600345 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0b885bc2020-04-26 09:19:53 -0600346
347/* Test 'acpi list' command */
348static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
349{
350 struct acpi_ctx ctx;
351 ulong addr;
352 void *buf;
353
354 buf = memalign(16, BUF_SIZE);
355 ut_assertnonnull(buf);
356 acpi_setup_base_tables(&ctx, buf);
357
358 ut_assertok(acpi_write_dev_tables(&ctx));
359
360 console_record_reset();
361 run_command("acpi list", 0);
362 addr = (ulong)map_to_sysmem(buf);
363 ut_assert_nextline("ACPI tables start at %lx", addr);
Simon Glass634f5ad2021-02-07 14:27:05 -0700364 ut_assert_nextline("RSDP %08lx %06zx (v02 U-BOOT)", addr,
Simon Glass0b885bc2020-04-26 09:19:53 -0600365 sizeof(struct acpi_rsdp));
366 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Simon Glass634f5ad2021-02-07 14:27:05 -0700367 ut_assert_nextline("RSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glass0b885bc2020-04-26 09:19:53 -0600368 addr, sizeof(struct acpi_table_header) +
Simon Glass1361a532020-07-07 13:11:39 -0600369 3 * sizeof(u32), U_BOOT_BUILD_DATE);
Simon Glass0b885bc2020-04-26 09:19:53 -0600370 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Simon Glass634f5ad2021-02-07 14:27:05 -0700371 ut_assert_nextline("XSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glass0b885bc2020-04-26 09:19:53 -0600372 addr, sizeof(struct acpi_table_header) +
Simon Glass1361a532020-07-07 13:11:39 -0600373 3 * sizeof(u64), U_BOOT_BUILD_DATE);
Simon Glass0b885bc2020-04-26 09:19:53 -0600374 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Simon Glass634f5ad2021-02-07 14:27:05 -0700375 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glass0b885bc2020-04-26 09:19:53 -0600376 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
377 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass634f5ad2021-02-07 14:27:05 -0700378 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glass0b885bc2020-04-26 09:19:53 -0600379 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
Simon Glass1361a532020-07-07 13:11:39 -0600380 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass634f5ad2021-02-07 14:27:05 -0700381 ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glass1361a532020-07-07 13:11:39 -0600382 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
Simon Glass0b885bc2020-04-26 09:19:53 -0600383 ut_assert_console_end();
384
385 return 0;
386}
Simon Glasse180c2b2020-07-28 19:41:12 -0600387DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0b885bc2020-04-26 09:19:53 -0600388
389/* Test 'acpi dump' command */
390static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
391{
392 struct acpi_ctx ctx;
393 ulong addr;
394 void *buf;
395
396 buf = memalign(16, BUF_SIZE);
397 ut_assertnonnull(buf);
398 acpi_setup_base_tables(&ctx, buf);
399
400 ut_assertok(acpi_write_dev_tables(&ctx));
401
402 /* First search for a non-existent table */
403 console_record_reset();
404 run_command("acpi dump rdst", 0);
405 ut_assert_nextline("Table 'RDST' not found");
406 ut_assert_console_end();
407
408 /* Now a real table */
409 console_record_reset();
410 run_command("acpi dump dmar", 0);
411 addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
412 ut_assert_nextline("DMAR @ %08lx", addr);
413 ut_assert_nextlines_are_dump(0x30);
414 ut_assert_console_end();
415
416 return 0;
417}
Simon Glasse180c2b2020-07-28 19:41:12 -0600418DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass1361a532020-07-07 13:11:39 -0600419
420/* Test acpi_device_path() */
421static int dm_test_acpi_device_path(struct unit_test_state *uts)
422{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700423 struct testacpi_plat *plat;
Simon Glass1361a532020-07-07 13:11:39 -0600424 char buf[ACPI_PATH_MAX];
425 struct udevice *dev, *child;
426
427 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
428 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
429 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
430
431 /* Test running out of space */
432 buf[5] = '\0';
433 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
434 ut_asserteq('\0', buf[5]);
435
436 /* Test a three-component name */
437 ut_assertok(device_first_child_err(dev, &child));
438 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
439 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
440 buf);
441
442 /* Test handling of a device which doesn't produce a name */
Simon Glassc69cda22020-12-03 16:55:20 -0700443 plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -0600444 plat->no_name = true;
445 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
446 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
447
448 /* Test handling of a device which returns an error */
Simon Glassc69cda22020-12-03 16:55:20 -0700449 plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -0600450 plat->return_error = true;
451 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
452
453 return 0;
454}
Simon Glasse180c2b2020-07-28 19:41:12 -0600455DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass2715b362020-07-07 13:11:40 -0600456
457/* Test acpi_device_status() */
458static int dm_test_acpi_device_status(struct unit_test_state *uts)
459{
460 struct udevice *dev;
461
462 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
463 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
464
465 return 0;
466}
Simon Glasse180c2b2020-07-28 19:41:12 -0600467DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb5183172020-07-07 13:12:03 -0600468
469/* Test acpi_fill_ssdt() */
470static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
471{
472 struct acpi_ctx ctx;
473 u8 *buf;
474
475 buf = malloc(BUF_SIZE);
476 ut_assertnonnull(buf);
477
Simon Glass18434ae2020-11-04 09:57:33 -0700478 acpi_reset_items();
Simon Glassb5183172020-07-07 13:12:03 -0600479 ctx.current = buf;
480 buf[4] = 'z'; /* sentinel */
481 ut_assertok(acpi_fill_ssdt(&ctx));
482
Simon Glass0f7b1112020-07-07 13:12:06 -0600483 /*
484 * These values come from acpi-test2's acpi-ssdt-test-data property.
485 * This device comes first because of u-boot,acpi-ssdt-order
486 */
487 ut_asserteq('c', buf[0]);
488 ut_asserteq('d', buf[1]);
Simon Glassb5183172020-07-07 13:12:03 -0600489
Simon Glass0f7b1112020-07-07 13:12:06 -0600490 /* These values come from acpi-test's acpi-ssdt-test-data property */
491 ut_asserteq('a', buf[2]);
492 ut_asserteq('b', buf[3]);
Simon Glassb5183172020-07-07 13:12:03 -0600493
494 ut_asserteq('z', buf[4]);
495
496 return 0;
497}
Simon Glasse180c2b2020-07-28 19:41:12 -0600498DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass01694582020-07-07 13:12:08 -0600499
500/* Test acpi_inject_dsdt() */
501static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
502{
503 struct acpi_ctx ctx;
504 u8 *buf;
505
506 buf = malloc(BUF_SIZE);
507 ut_assertnonnull(buf);
508
Simon Glass18434ae2020-11-04 09:57:33 -0700509 acpi_reset_items();
Simon Glass01694582020-07-07 13:12:08 -0600510 ctx.current = buf;
511 buf[4] = 'z'; /* sentinel */
512 ut_assertok(acpi_inject_dsdt(&ctx));
513
514 /*
515 * These values come from acpi-test's acpi-dsdt-test-data property.
516 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
517 */
518 ut_asserteq('h', buf[0]);
519 ut_asserteq('i', buf[1]);
520
521 /* These values come from acpi-test's acpi-dsdt-test-data property */
522 ut_asserteq('j', buf[2]);
523 ut_asserteq('k', buf[3]);
524
525 ut_asserteq('z', buf[4]);
526
527 return 0;
528}
Simon Glasse180c2b2020-07-28 19:41:12 -0600529DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa4f82082020-07-07 13:12:12 -0600530
531/* Test 'acpi items' command */
532static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
533{
534 struct acpi_ctx ctx;
535 void *buf;
536
537 buf = malloc(BUF_SIZE);
538 ut_assertnonnull(buf);
539
Simon Glass18434ae2020-11-04 09:57:33 -0700540 acpi_reset_items();
Simon Glassa4f82082020-07-07 13:12:12 -0600541 ctx.current = buf;
542 ut_assertok(acpi_fill_ssdt(&ctx));
543 console_record_reset();
544 run_command("acpi items", 0);
545 ut_assert_nextline("dev 'acpi-test', type 1, size 2");
546 ut_assert_nextline("dev 'acpi-test2', type 1, size 2");
547 ut_assert_console_end();
548
Simon Glass18434ae2020-11-04 09:57:33 -0700549 acpi_reset_items();
Simon Glassa4f82082020-07-07 13:12:12 -0600550 ctx.current = buf;
551 ut_assertok(acpi_inject_dsdt(&ctx));
552 console_record_reset();
553 run_command("acpi items", 0);
554 ut_assert_nextline("dev 'acpi-test', type 2, size 2");
555 ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
556 ut_assert_console_end();
557
558 console_record_reset();
559 run_command("acpi items -d", 0);
560 ut_assert_nextline("dev 'acpi-test', type 2, size 2");
561 ut_assert_nextlines_are_dump(2);
562 ut_assert_nextline("%s", "");
563 ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
564 ut_assert_nextlines_are_dump(2);
565 ut_assert_nextline("%s", "");
566 ut_assert_console_end();
567
568 return 0;
569}
Simon Glasse180c2b2020-07-28 19:41:12 -0600570DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);