blob: 240187c52350d75d653b109e717efe22b47101fd [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 Glassd43e0ba2020-07-07 13:12:03 -060017#include <acpi/acpigen.h>
Simon Glassebb2e832020-07-07 13:11:39 -060018#include <acpi/acpi_device.h>
Simon Glassb2672ea2020-04-08 16:57:38 -060019#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060020#include <asm/global_data.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060021#include <dm/acpi.h>
22#include <dm/test.h>
23#include <test/ut.h>
Simon Glass6fcdaf12020-07-07 21:32:10 -060024#include "acpi.h"
Simon Glass2d67fdf2020-04-08 16:57:34 -060025
Simon Glass17968c32020-04-26 09:19:46 -060026#define BUF_SIZE 4096
27
Simon Glassebb2e832020-07-07 13:11:39 -060028/**
Simon Glassb75b15b2020-12-03 16:55:23 -070029 * struct testacpi_plat - Platform data for the test ACPI device
Simon Glassebb2e832020-07-07 13:11:39 -060030 *
31 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
32 * @return_error: true to return an error instead of a name
33 */
Simon Glassb75b15b2020-12-03 16:55:23 -070034struct testacpi_plat {
Simon Glassebb2e832020-07-07 13:11:39 -060035 bool return_error;
36 bool no_name;
37};
38
Simon Glass17968c32020-04-26 09:19:46 -060039static int testacpi_write_tables(const struct udevice *dev,
40 struct acpi_ctx *ctx)
41{
42 struct acpi_dmar *dmar;
Simon Glass575a5472020-04-26 09:19:50 -060043 int ret;
Simon Glass17968c32020-04-26 09:19:46 -060044
45 dmar = (struct acpi_dmar *)ctx->current;
46 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
47 ctx->current += sizeof(struct acpi_dmar);
Simon Glass575a5472020-04-26 09:19:50 -060048 ret = acpi_add_table(ctx, dmar);
49 if (ret)
50 return log_msg_ret("add", ret);
Simon Glass17968c32020-04-26 09:19:46 -060051
52 return 0;
53}
Simon Glass2d67fdf2020-04-08 16:57:34 -060054
55static int testacpi_get_name(const struct udevice *dev, char *out_name)
56{
Simon Glassb75b15b2020-12-03 16:55:23 -070057 struct testacpi_plat *plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -060058
59 if (plat->return_error)
60 return -EINVAL;
61 if (plat->no_name) {
62 *out_name = '\0';
63 return 0;
64 }
65 if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
66 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
67 else
68 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
Simon Glass2d67fdf2020-04-08 16:57:34 -060069}
70
Simon Glassd43e0ba2020-07-07 13:12:03 -060071static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
72{
73 const char *data;
74
75 data = dev_read_string(dev, "acpi-ssdt-test-data");
76 if (data) {
77 while (*data)
78 acpigen_emit_byte(ctx, *data++);
79 }
80
81 return 0;
82}
83
Simon Glass990cd5b2020-07-07 13:12:08 -060084static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
85{
86 const char *data;
87
88 data = dev_read_string(dev, "acpi-dsdt-test-data");
89 if (data) {
90 while (*data)
91 acpigen_emit_byte(ctx, *data++);
92 }
93
94 return 0;
95}
96
Simon Glass2d67fdf2020-04-08 16:57:34 -060097struct acpi_ops testacpi_ops = {
98 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -060099 .write_tables = testacpi_write_tables,
Simon Glassd43e0ba2020-07-07 13:12:03 -0600100 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass990cd5b2020-07-07 13:12:08 -0600101 .inject_dsdt = testacpi_inject_dsdt,
Simon Glass2d67fdf2020-04-08 16:57:34 -0600102};
103
104static const struct udevice_id testacpi_ids[] = {
105 { .compatible = "denx,u-boot-acpi-test" },
106 { }
107};
108
109U_BOOT_DRIVER(testacpi_drv) = {
110 .name = "testacpi_drv",
111 .of_match = testacpi_ids,
112 .id = UCLASS_TEST_ACPI,
Simon Glassebb2e832020-07-07 13:11:39 -0600113 .bind = dm_scan_fdt_dev,
Simon Glassb75b15b2020-12-03 16:55:23 -0700114 .plat_auto = sizeof(struct testacpi_plat),
Simon Glass2d67fdf2020-04-08 16:57:34 -0600115 ACPI_OPS_PTR(&testacpi_ops)
116};
117
118UCLASS_DRIVER(testacpi) = {
119 .name = "testacpi",
120 .id = UCLASS_TEST_ACPI,
121};
122
123/* Test ACPI get_name() */
124static int dm_test_acpi_get_name(struct unit_test_state *uts)
125{
126 char name[ACPI_NAME_MAX];
Simon Glass9a655132020-12-16 21:20:23 -0700127 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glass09642392020-07-07 13:12:11 -0600128 struct udevice *pci, *root;
Simon Glass2d67fdf2020-04-08 16:57:34 -0600129
Simon Glass09642392020-07-07 13:12:11 -0600130 /* Test getting the name from the driver */
Simon Glass2d67fdf2020-04-08 16:57:34 -0600131 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
132 ut_assertok(acpi_get_name(dev, name));
133 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
134
Simon Glass09642392020-07-07 13:12:11 -0600135 /* Test getting the name from the device tree */
136 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
137 &dev2));
138 ut_assertok(acpi_get_name(dev2, name));
139 ut_asserteq_str("GHIJ", name);
140
141 /* Test getting the name from acpi_device_get_name() */
142 ut_assertok(uclass_first_device(UCLASS_I2C, &i2c));
143 ut_assertok(acpi_get_name(i2c, name));
144 ut_asserteq_str("I2C0", name);
145
146 ut_assertok(uclass_first_device(UCLASS_SPI, &spi));
147 ut_assertok(acpi_get_name(spi, name));
148 ut_asserteq_str("SPI0", name);
149
Simon Glass09642392020-07-07 13:12:11 -0600150 /* ACPI doesn't know about the timer */
151 ut_assertok(uclass_first_device(UCLASS_TIMER, &timer));
152 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
153
154 /* May as well test the rest of the cases */
155 ut_assertok(uclass_first_device(UCLASS_SOUND, &sound));
156 ut_assertok(acpi_get_name(sound, name));
157 ut_asserteq_str("HDAS", name);
158
159 ut_assertok(uclass_first_device(UCLASS_PCI, &pci));
160 ut_assertok(acpi_get_name(pci, name));
161 ut_asserteq_str("PCI0", name);
162
163 ut_assertok(uclass_first_device(UCLASS_ROOT, &root));
164 ut_assertok(acpi_get_name(root, name));
165 ut_asserteq_str("\\_SB", name);
166
167 /* Note that we don't have tests for acpi_name_from_id() */
168
Simon Glass2d67fdf2020-04-08 16:57:34 -0600169 return 0;
170}
Simon Glass974dccd2020-07-28 19:41:12 -0600171DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -0600172
173/* Test acpi_get_table_revision() */
174static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
175{
176 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
177 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
178 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
179 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
180
181 return 0;
182}
183DM_TEST(dm_test_acpi_get_table_revision,
Simon Glass974dccd2020-07-28 19:41:12 -0600184 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -0600185
Simon Glasse9629892020-04-08 16:57:39 -0600186/* Test acpi_create_dmar() */
187static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
188{
189 struct acpi_dmar dmar;
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200190 struct udevice *cpu;
Simon Glasse9629892020-04-08 16:57:39 -0600191
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200192 ut_assertok(uclass_first_device(UCLASS_CPU, &cpu));
193 ut_assertnonnull(cpu);
Simon Glasse9629892020-04-08 16:57:39 -0600194 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
195 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
196 ut_asserteq(32 - 1, dmar.host_address_width);
197
198 return 0;
199}
Simon Glass974dccd2020-07-28 19:41:12 -0600200DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600201
202/* Test acpi_fill_header() */
203static int dm_test_acpi_fill_header(struct unit_test_state *uts)
204{
205 struct acpi_table_header hdr;
206
207 /* Make sure these 5 fields are not changed */
208 hdr.length = 0x11;
209 hdr.revision = 0x22;
210 hdr.checksum = 0x33;
211 hdr.aslc_revision = 0x44;
212 acpi_fill_header(&hdr, "ABCD");
213
214 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
215 ut_asserteq(0x11, hdr.length);
216 ut_asserteq(0x22, hdr.revision);
217 ut_asserteq(0x33, hdr.checksum);
218 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
219 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
220 sizeof(hdr.oem_table_id));
221 ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
222 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
223 ut_asserteq(0x44, hdr.aslc_revision);
224
225 return 0;
226}
Simon Glass974dccd2020-07-28 19:41:12 -0600227DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600228
229/* Test ACPI write_tables() */
230static int dm_test_acpi_write_tables(struct unit_test_state *uts)
231{
232 struct acpi_dmar *dmar;
233 struct acpi_ctx ctx;
234 void *buf;
Simon Glassebb2e832020-07-07 13:11:39 -0600235 int i;
Simon Glass17968c32020-04-26 09:19:46 -0600236
237 buf = malloc(BUF_SIZE);
238 ut_assertnonnull(buf);
239
Simon Glass9c442a62020-04-26 09:19:51 -0600240 acpi_setup_base_tables(&ctx, buf);
Simon Glass575a5472020-04-26 09:19:50 -0600241 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600242 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600243
244 /*
Simon Glassebb2e832020-07-07 13:11:39 -0600245 * We should have three dmar tables, one for each
246 * "denx,u-boot-acpi-test" device
Simon Glass17968c32020-04-26 09:19:46 -0600247 */
Simon Glassebb2e832020-07-07 13:11:39 -0600248 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass17968c32020-04-26 09:19:46 -0600249 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
250 ut_asserteq(32 - 1, dmar->host_address_width);
251
252 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
253 ut_asserteq(32 - 1, dmar[1].host_address_width);
254
Simon Glassebb2e832020-07-07 13:11:39 -0600255 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
256 ut_asserteq(32 - 1, dmar[2].host_address_width);
Simon Glass9c442a62020-04-26 09:19:51 -0600257
Simon Glassebb2e832020-07-07 13:11:39 -0600258 /* Check that the pointers were added correctly */
259 for (i = 0; i < 3; i++) {
260 ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
261 ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
262 }
263 ut_asserteq(0, ctx.rsdt->entry[3]);
264 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassabeaca82020-04-26 09:19:52 -0600265
Simon Glass17968c32020-04-26 09:19:46 -0600266 return 0;
267}
Simon Glass974dccd2020-07-28 19:41:12 -0600268DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600269
270/* Test basic ACPI functions */
271static int dm_test_acpi_basic(struct unit_test_state *uts)
272{
273 struct acpi_ctx ctx;
274
275 /* Check align works */
276 ctx.current = (void *)5;
277 acpi_align(&ctx);
278 ut_asserteq_ptr((void *)16, ctx.current);
279
280 /* Check that align does nothing if already aligned */
281 acpi_align(&ctx);
282 ut_asserteq_ptr((void *)16, ctx.current);
283 acpi_align64(&ctx);
284 ut_asserteq_ptr((void *)64, ctx.current);
285 acpi_align64(&ctx);
286 ut_asserteq_ptr((void *)64, ctx.current);
287
288 /* Check incrementing */
289 acpi_inc(&ctx, 3);
290 ut_asserteq_ptr((void *)67, ctx.current);
291 acpi_inc_align(&ctx, 3);
292 ut_asserteq_ptr((void *)80, ctx.current);
293
294 return 0;
295}
Simon Glass974dccd2020-07-28 19:41:12 -0600296DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600297
298/* Test acpi_setup_base_tables */
299static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
300{
301 struct acpi_rsdp *rsdp;
302 struct acpi_rsdt *rsdt;
303 struct acpi_xsdt *xsdt;
304 struct acpi_ctx ctx;
305 void *buf, *end;
306
307 /*
308 * Use an unaligned address deliberately, by allocating an aligned
309 * address and then adding 4 to it
310 */
311 buf = memalign(64, BUF_SIZE);
312 ut_assertnonnull(buf);
313 acpi_setup_base_tables(&ctx, buf + 4);
Simon Glassfaf08c72020-04-26 09:19:53 -0600314 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start);
Simon Glass9c442a62020-04-26 09:19:51 -0600315
316 rsdp = buf + 16;
317 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassa3186e62020-05-10 12:52:45 -0600318 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass9c442a62020-04-26 09:19:51 -0600319 ut_asserteq(sizeof(*rsdp), rsdp->length);
320 ut_assertok(table_compute_checksum(rsdp, 20));
321 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
322
323 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
324 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600325 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600326 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
327 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
328
329 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassabeaca82020-04-26 09:19:52 -0600330 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600331 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600332 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
333 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
334
335 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
336 ut_asserteq_ptr(end, ctx.current);
337
338 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
339 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
340
341 return 0;
342}
343DM_TEST(dm_test_acpi_setup_base_tables,
Simon Glass974dccd2020-07-28 19:41:12 -0600344 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600345
346/* Test 'acpi list' command */
347static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
348{
349 struct acpi_ctx ctx;
350 ulong addr;
351 void *buf;
352
353 buf = memalign(16, BUF_SIZE);
354 ut_assertnonnull(buf);
355 acpi_setup_base_tables(&ctx, buf);
356
357 ut_assertok(acpi_write_dev_tables(&ctx));
358
359 console_record_reset();
360 run_command("acpi list", 0);
361 addr = (ulong)map_to_sysmem(buf);
362 ut_assert_nextline("ACPI tables start at %lx", addr);
363 ut_assert_nextline("RSDP %08lx %06lx (v02 U-BOOT)", addr,
364 sizeof(struct acpi_rsdp));
365 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Andy Shevchenkoc2487422020-09-21 11:34:21 +0300366 ut_assert_nextline("RSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600367 addr, sizeof(struct acpi_table_header) +
Simon Glassebb2e832020-07-07 13:11:39 -0600368 3 * sizeof(u32), U_BOOT_BUILD_DATE);
Simon Glassfaf08c72020-04-26 09:19:53 -0600369 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Andy Shevchenkoc2487422020-09-21 11:34:21 +0300370 ut_assert_nextline("XSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600371 addr, sizeof(struct acpi_table_header) +
Simon Glassebb2e832020-07-07 13:11:39 -0600372 3 * sizeof(u64), U_BOOT_BUILD_DATE);
Simon Glassfaf08c72020-04-26 09:19:53 -0600373 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Andy Shevchenkoc2487422020-09-21 11:34:21 +0300374 ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600375 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
376 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Andy Shevchenkoc2487422020-09-21 11:34:21 +0300377 ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassfaf08c72020-04-26 09:19:53 -0600378 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
Simon Glassebb2e832020-07-07 13:11:39 -0600379 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Andy Shevchenkoc2487422020-09-21 11:34:21 +0300380 ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
Simon Glassebb2e832020-07-07 13:11:39 -0600381 addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
Simon Glassfaf08c72020-04-26 09:19:53 -0600382 ut_assert_console_end();
383
384 return 0;
385}
Simon Glass974dccd2020-07-28 19:41:12 -0600386DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600387
388/* Test 'acpi dump' command */
389static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
390{
391 struct acpi_ctx ctx;
392 ulong addr;
393 void *buf;
394
395 buf = memalign(16, BUF_SIZE);
396 ut_assertnonnull(buf);
397 acpi_setup_base_tables(&ctx, buf);
398
399 ut_assertok(acpi_write_dev_tables(&ctx));
400
401 /* First search for a non-existent table */
402 console_record_reset();
403 run_command("acpi dump rdst", 0);
404 ut_assert_nextline("Table 'RDST' not found");
405 ut_assert_console_end();
406
407 /* Now a real table */
408 console_record_reset();
409 run_command("acpi dump dmar", 0);
410 addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
411 ut_assert_nextline("DMAR @ %08lx", addr);
412 ut_assert_nextlines_are_dump(0x30);
413 ut_assert_console_end();
414
415 return 0;
416}
Simon Glass974dccd2020-07-28 19:41:12 -0600417DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassebb2e832020-07-07 13:11:39 -0600418
419/* Test acpi_device_path() */
420static int dm_test_acpi_device_path(struct unit_test_state *uts)
421{
Simon Glassb75b15b2020-12-03 16:55:23 -0700422 struct testacpi_plat *plat;
Simon Glassebb2e832020-07-07 13:11:39 -0600423 char buf[ACPI_PATH_MAX];
424 struct udevice *dev, *child;
425
426 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
427 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
428 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
429
430 /* Test running out of space */
431 buf[5] = '\0';
432 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
433 ut_asserteq('\0', buf[5]);
434
435 /* Test a three-component name */
436 ut_assertok(device_first_child_err(dev, &child));
437 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
438 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
439 buf);
440
441 /* Test handling of a device which doesn't produce a name */
Simon Glassfa20e932020-12-03 16:55:20 -0700442 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600443 plat->no_name = true;
444 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
445 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
446
447 /* Test handling of a device which returns an error */
Simon Glassfa20e932020-12-03 16:55:20 -0700448 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600449 plat->return_error = true;
450 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
451
452 return 0;
453}
Simon Glass974dccd2020-07-28 19:41:12 -0600454DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass8965cc92020-07-07 13:11:40 -0600455
456/* Test acpi_device_status() */
457static int dm_test_acpi_device_status(struct unit_test_state *uts)
458{
459 struct udevice *dev;
460
461 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
462 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
463
464 return 0;
465}
Simon Glass974dccd2020-07-28 19:41:12 -0600466DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600467
468/* Test acpi_fill_ssdt() */
469static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
470{
471 struct acpi_ctx ctx;
472 u8 *buf;
473
474 buf = malloc(BUF_SIZE);
475 ut_assertnonnull(buf);
476
Simon Glassa385d492020-11-04 09:57:33 -0700477 acpi_reset_items();
Simon Glassd43e0ba2020-07-07 13:12:03 -0600478 ctx.current = buf;
479 buf[4] = 'z'; /* sentinel */
480 ut_assertok(acpi_fill_ssdt(&ctx));
481
Simon Glass3c601b12020-07-07 13:12:06 -0600482 /*
483 * These values come from acpi-test2's acpi-ssdt-test-data property.
484 * This device comes first because of u-boot,acpi-ssdt-order
485 */
486 ut_asserteq('c', buf[0]);
487 ut_asserteq('d', buf[1]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600488
Simon Glass3c601b12020-07-07 13:12:06 -0600489 /* These values come from acpi-test's acpi-ssdt-test-data property */
490 ut_asserteq('a', buf[2]);
491 ut_asserteq('b', buf[3]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600492
493 ut_asserteq('z', buf[4]);
494
495 return 0;
496}
Simon Glass974dccd2020-07-28 19:41:12 -0600497DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass990cd5b2020-07-07 13:12:08 -0600498
499/* Test acpi_inject_dsdt() */
500static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
501{
502 struct acpi_ctx ctx;
503 u8 *buf;
504
505 buf = malloc(BUF_SIZE);
506 ut_assertnonnull(buf);
507
Simon Glassa385d492020-11-04 09:57:33 -0700508 acpi_reset_items();
Simon Glass990cd5b2020-07-07 13:12:08 -0600509 ctx.current = buf;
510 buf[4] = 'z'; /* sentinel */
511 ut_assertok(acpi_inject_dsdt(&ctx));
512
513 /*
514 * These values come from acpi-test's acpi-dsdt-test-data property.
515 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
516 */
517 ut_asserteq('h', buf[0]);
518 ut_asserteq('i', buf[1]);
519
520 /* These values come from acpi-test's acpi-dsdt-test-data property */
521 ut_asserteq('j', buf[2]);
522 ut_asserteq('k', buf[3]);
523
524 ut_asserteq('z', buf[4]);
525
526 return 0;
527}
Simon Glass974dccd2020-07-28 19:41:12 -0600528DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600529
530/* Test 'acpi items' command */
531static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
532{
533 struct acpi_ctx ctx;
534 void *buf;
535
536 buf = malloc(BUF_SIZE);
537 ut_assertnonnull(buf);
538
Simon Glassa385d492020-11-04 09:57:33 -0700539 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600540 ctx.current = buf;
541 ut_assertok(acpi_fill_ssdt(&ctx));
542 console_record_reset();
543 run_command("acpi items", 0);
544 ut_assert_nextline("dev 'acpi-test', type 1, size 2");
545 ut_assert_nextline("dev 'acpi-test2', type 1, size 2");
546 ut_assert_console_end();
547
Simon Glassa385d492020-11-04 09:57:33 -0700548 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600549 ctx.current = buf;
550 ut_assertok(acpi_inject_dsdt(&ctx));
551 console_record_reset();
552 run_command("acpi items", 0);
553 ut_assert_nextline("dev 'acpi-test', type 2, size 2");
554 ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
555 ut_assert_console_end();
556
557 console_record_reset();
558 run_command("acpi items -d", 0);
559 ut_assert_nextline("dev 'acpi-test', type 2, size 2");
560 ut_assert_nextlines_are_dump(2);
561 ut_assert_nextline("%s", "");
562 ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
563 ut_assert_nextlines_are_dump(2);
564 ut_assert_nextline("%s", "");
565 ut_assert_console_end();
566
567 return 0;
568}
Simon Glass974dccd2020-07-28 19:41:12 -0600569DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);