blob: 7ccd7f8d20a56f41010ddf2f4d6d43bd2f029a8c [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
Simon Glassfaf08c72020-04-26 09:19:53 -06009#include <console.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060010#include <dm.h>
Simon Glass9c442a62020-04-26 09:19:51 -060011#include <malloc.h>
Simon Glass575a5472020-04-26 09:19:50 -060012#include <mapmem.h>
Simon Glass9c442a62020-04-26 09:19:51 -060013#include <tables_csum.h>
Simon Glass90b01272023-04-29 19:21:46 -060014#include <version_string.h>
Simon Glassd43e0ba2020-07-07 13:12:03 -060015#include <acpi/acpigen.h>
Simon Glassebb2e832020-07-07 13:11:39 -060016#include <acpi/acpi_device.h>
Simon Glassb2672ea2020-04-08 16:57:38 -060017#include <acpi/acpi_table.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060018#include <asm/global_data.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060019#include <dm/acpi.h>
20#include <dm/test.h>
21#include <test/ut.h>
Simon Glass6fcdaf12020-07-07 21:32:10 -060022#include "acpi.h"
Simon Glass2d67fdf2020-04-08 16:57:34 -060023
Simon Glass17968c32020-04-26 09:19:46 -060024#define BUF_SIZE 4096
25
Simon Glass90b01272023-04-29 19:21:46 -060026#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
27 (((version_num / 100) % 10) << 24) | \
28 (((version_num / 10) % 10) << 20) | \
29 ((version_num % 10) << 16) | \
30 (((version_num_patch / 10) % 10) << 12) | \
31 ((version_num_patch % 10) << 8) | \
Pali Rohárf0de20e2021-07-10 13:10:01 +020032 0x01)
33
Simon Glassebb2e832020-07-07 13:11:39 -060034/**
Simon Glassb75b15b2020-12-03 16:55:23 -070035 * struct testacpi_plat - Platform data for the test ACPI device
Simon Glassebb2e832020-07-07 13:11:39 -060036 *
37 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
38 * @return_error: true to return an error instead of a name
39 */
Simon Glassb75b15b2020-12-03 16:55:23 -070040struct testacpi_plat {
Simon Glassebb2e832020-07-07 13:11:39 -060041 bool return_error;
42 bool no_name;
43};
44
Simon Glass37acc662021-12-01 09:02:48 -070045/**
46 * setup_ctx_and_base_tables() - Set up context along with RSDP, RSDT and XSDT
47 *
48 * Set up the context with the given start position. Some basic tables are
49 * always needed, so set them up as well.
50 *
51 * @ctx: Context to set up
52 */
Simon Glasse9f6e6a2021-12-01 09:02:50 -070053static int setup_ctx_and_base_tables(struct unit_test_state *uts,
54 struct acpi_ctx *ctx, ulong start)
Simon Glass37acc662021-12-01 09:02:48 -070055{
Simon Glasse9f6e6a2021-12-01 09:02:50 -070056 struct acpi_writer *entry = ACPI_WRITER_GET(0base);
57
Simon Glass37acc662021-12-01 09:02:48 -070058 acpi_setup_ctx(ctx, start);
Simon Glasse9f6e6a2021-12-01 09:02:50 -070059
60 ctx->tab_start = ctx->current;
61 ut_assertok(acpi_write_one(ctx, entry));
Simon Glass37acc662021-12-01 09:02:48 -070062
63 return 0;
64}
65
Simon Glass17968c32020-04-26 09:19:46 -060066static int testacpi_write_tables(const struct udevice *dev,
67 struct acpi_ctx *ctx)
68{
69 struct acpi_dmar *dmar;
Simon Glass575a5472020-04-26 09:19:50 -060070 int ret;
Simon Glass17968c32020-04-26 09:19:46 -060071
72 dmar = (struct acpi_dmar *)ctx->current;
73 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
74 ctx->current += sizeof(struct acpi_dmar);
Simon Glass575a5472020-04-26 09:19:50 -060075 ret = acpi_add_table(ctx, dmar);
76 if (ret)
77 return log_msg_ret("add", ret);
Simon Glass17968c32020-04-26 09:19:46 -060078
79 return 0;
80}
Simon Glass2d67fdf2020-04-08 16:57:34 -060081
82static int testacpi_get_name(const struct udevice *dev, char *out_name)
83{
Simon Glassb75b15b2020-12-03 16:55:23 -070084 struct testacpi_plat *plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -060085
86 if (plat->return_error)
87 return -EINVAL;
88 if (plat->no_name) {
89 *out_name = '\0';
90 return 0;
91 }
92 if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
93 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
94 else
95 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
Simon Glass2d67fdf2020-04-08 16:57:34 -060096}
97
Simon Glassd43e0ba2020-07-07 13:12:03 -060098static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
99{
100 const char *data;
101
102 data = dev_read_string(dev, "acpi-ssdt-test-data");
103 if (data) {
104 while (*data)
105 acpigen_emit_byte(ctx, *data++);
106 }
107
108 return 0;
109}
110
Simon Glass990cd5b2020-07-07 13:12:08 -0600111static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
112{
113 const char *data;
114
115 data = dev_read_string(dev, "acpi-dsdt-test-data");
116 if (data) {
117 while (*data)
118 acpigen_emit_byte(ctx, *data++);
119 }
120
121 return 0;
122}
123
Simon Glass2d67fdf2020-04-08 16:57:34 -0600124struct acpi_ops testacpi_ops = {
125 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -0600126 .write_tables = testacpi_write_tables,
Simon Glassd43e0ba2020-07-07 13:12:03 -0600127 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass990cd5b2020-07-07 13:12:08 -0600128 .inject_dsdt = testacpi_inject_dsdt,
Simon Glass2d67fdf2020-04-08 16:57:34 -0600129};
130
131static const struct udevice_id testacpi_ids[] = {
132 { .compatible = "denx,u-boot-acpi-test" },
133 { }
134};
135
136U_BOOT_DRIVER(testacpi_drv) = {
137 .name = "testacpi_drv",
138 .of_match = testacpi_ids,
139 .id = UCLASS_TEST_ACPI,
Simon Glassebb2e832020-07-07 13:11:39 -0600140 .bind = dm_scan_fdt_dev,
Simon Glassb75b15b2020-12-03 16:55:23 -0700141 .plat_auto = sizeof(struct testacpi_plat),
Simon Glass2d67fdf2020-04-08 16:57:34 -0600142 ACPI_OPS_PTR(&testacpi_ops)
143};
144
145UCLASS_DRIVER(testacpi) = {
146 .name = "testacpi",
147 .id = UCLASS_TEST_ACPI,
148};
149
150/* Test ACPI get_name() */
151static int dm_test_acpi_get_name(struct unit_test_state *uts)
152{
153 char name[ACPI_NAME_MAX];
Simon Glass9a655132020-12-16 21:20:23 -0700154 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glass09642392020-07-07 13:12:11 -0600155 struct udevice *pci, *root;
Simon Glass2d67fdf2020-04-08 16:57:34 -0600156
Simon Glass09642392020-07-07 13:12:11 -0600157 /* Test getting the name from the driver */
Simon Glass2d67fdf2020-04-08 16:57:34 -0600158 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
159 ut_assertok(acpi_get_name(dev, name));
160 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
161
Simon Glass09642392020-07-07 13:12:11 -0600162 /* Test getting the name from the device tree */
163 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
164 &dev2));
165 ut_assertok(acpi_get_name(dev2, name));
166 ut_asserteq_str("GHIJ", name);
167
168 /* Test getting the name from acpi_device_get_name() */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200169 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
Simon Glass09642392020-07-07 13:12:11 -0600170 ut_assertok(acpi_get_name(i2c, name));
171 ut_asserteq_str("I2C0", name);
172
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200173 ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi));
Simon Glass09642392020-07-07 13:12:11 -0600174 ut_assertok(acpi_get_name(spi, name));
175 ut_asserteq_str("SPI0", name);
176
Simon Glass09642392020-07-07 13:12:11 -0600177 /* ACPI doesn't know about the timer */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200178 ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer));
Simon Glass09642392020-07-07 13:12:11 -0600179 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
180
181 /* May as well test the rest of the cases */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200182 ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound));
Simon Glass09642392020-07-07 13:12:11 -0600183 ut_assertok(acpi_get_name(sound, name));
184 ut_asserteq_str("HDAS", name);
185
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200186 ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci));
Simon Glass09642392020-07-07 13:12:11 -0600187 ut_assertok(acpi_get_name(pci, name));
188 ut_asserteq_str("PCI0", name);
189
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200190 ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root));
Simon Glass09642392020-07-07 13:12:11 -0600191 ut_assertok(acpi_get_name(root, name));
192 ut_asserteq_str("\\_SB", name);
193
194 /* Note that we don't have tests for acpi_name_from_id() */
195
Simon Glass2d67fdf2020-04-08 16:57:34 -0600196 return 0;
197}
Simon Glass1a92f832024-08-22 07:57:48 -0600198DM_TEST(dm_test_acpi_get_name, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -0600199
200/* Test acpi_get_table_revision() */
201static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
202{
203 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
204 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
205 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
206 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
207
208 return 0;
209}
Simon Glass1a92f832024-08-22 07:57:48 -0600210DM_TEST(dm_test_acpi_get_table_revision, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -0600211
Simon Glasse9629892020-04-08 16:57:39 -0600212/* Test acpi_create_dmar() */
213static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
214{
215 struct acpi_dmar dmar;
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200216 struct udevice *cpu;
Simon Glasse9629892020-04-08 16:57:39 -0600217
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200218 ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200219 ut_assertnonnull(cpu);
Simon Glasse9629892020-04-08 16:57:39 -0600220 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
221 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200222 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
223 dmar.host_address_width);
Simon Glasse9629892020-04-08 16:57:39 -0600224
225 return 0;
226}
Simon Glass1a92f832024-08-22 07:57:48 -0600227DM_TEST(dm_test_acpi_create_dmar, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600228
229/* Test acpi_fill_header() */
230static int dm_test_acpi_fill_header(struct unit_test_state *uts)
231{
232 struct acpi_table_header hdr;
233
234 /* Make sure these 5 fields are not changed */
235 hdr.length = 0x11;
236 hdr.revision = 0x22;
237 hdr.checksum = 0x33;
Simon Glass17968c32020-04-26 09:19:46 -0600238 acpi_fill_header(&hdr, "ABCD");
239
240 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
241 ut_asserteq(0x11, hdr.length);
242 ut_asserteq(0x22, hdr.revision);
243 ut_asserteq(0x33, hdr.checksum);
244 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
245 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
246 sizeof(hdr.oem_table_id));
Pali Rohárf0de20e2021-07-10 13:10:01 +0200247 ut_asserteq(OEM_REVISION, hdr.oem_revision);
Heinrich Schuchardt10de8a82024-01-21 12:52:48 +0100248 ut_asserteq_mem(ASLC_ID, hdr.creator_id, sizeof(hdr.creator_id));
Heinrich Schuchardt9838d342024-04-18 05:11:13 +0200249 ut_asserteq(ASL_REVISION, hdr.creator_revision);
Simon Glass17968c32020-04-26 09:19:46 -0600250
251 return 0;
252}
Simon Glass1a92f832024-08-22 07:57:48 -0600253DM_TEST(dm_test_acpi_fill_header, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600254
255/* Test ACPI write_tables() */
256static int dm_test_acpi_write_tables(struct unit_test_state *uts)
257{
258 struct acpi_dmar *dmar;
259 struct acpi_ctx ctx;
Simon Glass37acc662021-12-01 09:02:48 -0700260 ulong addr;
Simon Glass17968c32020-04-26 09:19:46 -0600261 void *buf;
Simon Glassebb2e832020-07-07 13:11:39 -0600262 int i;
Simon Glass17968c32020-04-26 09:19:46 -0600263
264 buf = malloc(BUF_SIZE);
265 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700266 addr = map_to_sysmem(buf);
Simon Glass17968c32020-04-26 09:19:46 -0600267
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700268 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass575a5472020-04-26 09:19:50 -0600269 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600270 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600271
272 /*
Simon Glassebb2e832020-07-07 13:11:39 -0600273 * We should have three dmar tables, one for each
274 * "denx,u-boot-acpi-test" device
Simon Glass17968c32020-04-26 09:19:46 -0600275 */
Simon Glassebb2e832020-07-07 13:11:39 -0600276 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass17968c32020-04-26 09:19:46 -0600277 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200278 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
279 dmar->host_address_width);
Simon Glass17968c32020-04-26 09:19:46 -0600280
281 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200282 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
283 dmar[1].host_address_width);
Simon Glass17968c32020-04-26 09:19:46 -0600284
Simon Glassebb2e832020-07-07 13:11:39 -0600285 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200286 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
287 dmar[2].host_address_width);
Simon Glass9c442a62020-04-26 09:19:51 -0600288
Simon Glassebb2e832020-07-07 13:11:39 -0600289 /* Check that the pointers were added correctly */
290 for (i = 0; i < 3; i++) {
Simon Glass919f8352023-12-31 08:25:54 -0700291 ut_asserteq(nomap_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
292 ut_asserteq(nomap_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
Simon Glassebb2e832020-07-07 13:11:39 -0600293 }
294 ut_asserteq(0, ctx.rsdt->entry[3]);
295 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassabeaca82020-04-26 09:19:52 -0600296
Simon Glass17968c32020-04-26 09:19:46 -0600297 return 0;
298}
Simon Glass1a92f832024-08-22 07:57:48 -0600299DM_TEST(dm_test_acpi_write_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600300
301/* Test basic ACPI functions */
302static int dm_test_acpi_basic(struct unit_test_state *uts)
303{
304 struct acpi_ctx ctx;
305
306 /* Check align works */
307 ctx.current = (void *)5;
308 acpi_align(&ctx);
309 ut_asserteq_ptr((void *)16, ctx.current);
310
311 /* Check that align does nothing if already aligned */
312 acpi_align(&ctx);
313 ut_asserteq_ptr((void *)16, ctx.current);
314 acpi_align64(&ctx);
315 ut_asserteq_ptr((void *)64, ctx.current);
316 acpi_align64(&ctx);
317 ut_asserteq_ptr((void *)64, ctx.current);
318
319 /* Check incrementing */
320 acpi_inc(&ctx, 3);
321 ut_asserteq_ptr((void *)67, ctx.current);
322 acpi_inc_align(&ctx, 3);
323 ut_asserteq_ptr((void *)80, ctx.current);
324
325 return 0;
326}
Simon Glass1a92f832024-08-22 07:57:48 -0600327DM_TEST(dm_test_acpi_basic, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600328
Simon Glass16170882021-12-01 09:02:49 -0700329/* Test setup_ctx_and_base_tables */
Simon Glass8181e182023-12-31 08:25:53 -0700330static int dm_test_acpi_ctx_and_base_tables(struct unit_test_state *uts)
Simon Glass9c442a62020-04-26 09:19:51 -0600331{
332 struct acpi_rsdp *rsdp;
333 struct acpi_rsdt *rsdt;
334 struct acpi_xsdt *xsdt;
335 struct acpi_ctx ctx;
336 void *buf, *end;
Simon Glass37acc662021-12-01 09:02:48 -0700337 ulong addr;
Simon Glass9c442a62020-04-26 09:19:51 -0600338
339 /*
340 * Use an unaligned address deliberately, by allocating an aligned
341 * address and then adding 4 to it
342 */
343 buf = memalign(64, BUF_SIZE);
344 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700345 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700346 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
Simon Glassb33f2f92021-12-01 09:02:37 -0700347 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());
Simon Glass9c442a62020-04-26 09:19:51 -0600348
349 rsdp = buf + 16;
350 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassa3186e62020-05-10 12:52:45 -0600351 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass9c442a62020-04-26 09:19:51 -0600352 ut_asserteq(sizeof(*rsdp), rsdp->length);
353 ut_assertok(table_compute_checksum(rsdp, 20));
354 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
355
356 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
357 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600358 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600359 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
360 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
361
362 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassabeaca82020-04-26 09:19:52 -0600363 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600364 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600365 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
366 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
367
368 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
369 ut_asserteq_ptr(end, ctx.current);
370
Simon Glass919f8352023-12-31 08:25:54 -0700371 ut_asserteq(nomap_to_sysmem(rsdt), rsdp->rsdt_address);
372 ut_asserteq(nomap_to_sysmem(xsdt), rsdp->xsdt_address);
Simon Glass9c442a62020-04-26 09:19:51 -0600373
374 return 0;
375}
Simon Glass1a92f832024-08-22 07:57:48 -0600376DM_TEST(dm_test_acpi_ctx_and_base_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600377
378/* Test 'acpi list' command */
379static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
380{
381 struct acpi_ctx ctx;
382 ulong addr;
383 void *buf;
384
385 buf = memalign(16, BUF_SIZE);
386 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700387 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700388 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600389
390 ut_assertok(acpi_write_dev_tables(&ctx));
391
Simon Glassfaf08c72020-04-26 09:19:53 -0600392 run_command("acpi list", 0);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100393 ut_assert_nextline("Name Base Size Detail");
394 ut_assert_nextline("---- ---------------- ----- ----------------------------");
395 ut_assert_nextline("RSDP %16lx %5zx v02 U-BOOT", addr,
Simon Glassfaf08c72020-04-26 09:19:53 -0600396 sizeof(struct acpi_rsdp));
397 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100398 ut_assert_nextline("RSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glassfaf08c72020-04-26 09:19:53 -0600399 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200400 3 * sizeof(u32), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600401 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100402 ut_assert_nextline("XSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glassfaf08c72020-04-26 09:19:53 -0600403 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200404 3 * sizeof(u64), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600405 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100406 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200407 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600408 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100409 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200410 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassebb2e832020-07-07 13:11:39 -0600411 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100412 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200413 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600414 ut_assert_console_end();
415
416 return 0;
417}
Simon Glassf54458b2024-08-22 07:57:59 -0600418DM_TEST(dm_test_acpi_cmd_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassfaf08c72020-04-26 09:19:53 -0600419
420/* Test 'acpi dump' command */
421static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
422{
423 struct acpi_ctx ctx;
424 ulong addr;
425 void *buf;
426
427 buf = memalign(16, BUF_SIZE);
428 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700429 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700430 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600431
432 ut_assertok(acpi_write_dev_tables(&ctx));
433
434 /* First search for a non-existent table */
Simon Glassfaf08c72020-04-26 09:19:53 -0600435 run_command("acpi dump rdst", 0);
436 ut_assert_nextline("Table 'RDST' not found");
437 ut_assert_console_end();
438
439 /* Now a real table */
Simon Glassfaf08c72020-04-26 09:19:53 -0600440 run_command("acpi dump dmar", 0);
Simon Glass919f8352023-12-31 08:25:54 -0700441 addr = ALIGN(nomap_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100442 ut_assert_nextline("DMAR @ %16lx", addr);
Simon Glassfaf08c72020-04-26 09:19:53 -0600443 ut_assert_nextlines_are_dump(0x30);
444 ut_assert_console_end();
445
446 return 0;
447}
Simon Glassf54458b2024-08-22 07:57:59 -0600448DM_TEST(dm_test_acpi_cmd_dump, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassebb2e832020-07-07 13:11:39 -0600449
450/* Test acpi_device_path() */
451static int dm_test_acpi_device_path(struct unit_test_state *uts)
452{
Simon Glassb75b15b2020-12-03 16:55:23 -0700453 struct testacpi_plat *plat;
Simon Glassebb2e832020-07-07 13:11:39 -0600454 char buf[ACPI_PATH_MAX];
455 struct udevice *dev, *child;
456
457 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
458 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
459 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
460
461 /* Test running out of space */
462 buf[5] = '\0';
463 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
464 ut_asserteq('\0', buf[5]);
465
466 /* Test a three-component name */
467 ut_assertok(device_first_child_err(dev, &child));
468 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
469 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
470 buf);
471
472 /* Test handling of a device which doesn't produce a name */
Simon Glassfa20e932020-12-03 16:55:20 -0700473 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600474 plat->no_name = true;
475 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
476 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
477
478 /* Test handling of a device which returns an error */
Simon Glassfa20e932020-12-03 16:55:20 -0700479 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600480 plat->return_error = true;
481 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
482
483 return 0;
484}
Simon Glass1a92f832024-08-22 07:57:48 -0600485DM_TEST(dm_test_acpi_device_path, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass8965cc92020-07-07 13:11:40 -0600486
487/* Test acpi_device_status() */
488static int dm_test_acpi_device_status(struct unit_test_state *uts)
489{
490 struct udevice *dev;
491
492 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
493 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
494
495 return 0;
496}
Simon Glass1a92f832024-08-22 07:57:48 -0600497DM_TEST(dm_test_acpi_device_status, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600498
499/* Test acpi_fill_ssdt() */
500static int dm_test_acpi_fill_ssdt(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 Glassd43e0ba2020-07-07 13:12:03 -0600509 ctx.current = buf;
510 buf[4] = 'z'; /* sentinel */
511 ut_assertok(acpi_fill_ssdt(&ctx));
512
Simon Glass3c601b12020-07-07 13:12:06 -0600513 /*
514 * These values come from acpi-test2's acpi-ssdt-test-data property.
515 * This device comes first because of u-boot,acpi-ssdt-order
516 */
517 ut_asserteq('c', buf[0]);
518 ut_asserteq('d', buf[1]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600519
Simon Glass3c601b12020-07-07 13:12:06 -0600520 /* These values come from acpi-test's acpi-ssdt-test-data property */
521 ut_asserteq('a', buf[2]);
522 ut_asserteq('b', buf[3]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600523
524 ut_asserteq('z', buf[4]);
525
526 return 0;
527}
Simon Glass1a92f832024-08-22 07:57:48 -0600528DM_TEST(dm_test_acpi_fill_ssdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass990cd5b2020-07-07 13:12:08 -0600529
530/* Test acpi_inject_dsdt() */
531static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
532{
533 struct acpi_ctx ctx;
534 u8 *buf;
535
536 buf = malloc(BUF_SIZE);
537 ut_assertnonnull(buf);
538
Simon Glassa385d492020-11-04 09:57:33 -0700539 acpi_reset_items();
Simon Glass990cd5b2020-07-07 13:12:08 -0600540 ctx.current = buf;
541 buf[4] = 'z'; /* sentinel */
542 ut_assertok(acpi_inject_dsdt(&ctx));
543
544 /*
545 * These values come from acpi-test's acpi-dsdt-test-data property.
546 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
547 */
548 ut_asserteq('h', buf[0]);
549 ut_asserteq('i', buf[1]);
550
551 /* These values come from acpi-test's acpi-dsdt-test-data property */
552 ut_asserteq('j', buf[2]);
553 ut_asserteq('k', buf[3]);
554
555 ut_asserteq('z', buf[4]);
556
557 return 0;
558}
Simon Glass1a92f832024-08-22 07:57:48 -0600559DM_TEST(dm_test_acpi_inject_dsdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600560
561/* Test 'acpi items' command */
562static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
563{
564 struct acpi_ctx ctx;
Simon Glass884dcfa2021-12-01 09:03:05 -0700565 ulong addr;
Simon Glass64bbc0d2020-07-07 13:12:12 -0600566 void *buf;
567
568 buf = malloc(BUF_SIZE);
569 ut_assertnonnull(buf);
Simon Glass884dcfa2021-12-01 09:03:05 -0700570 addr = map_to_sysmem(buf);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600571
Simon Glassa385d492020-11-04 09:57:33 -0700572 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600573 ctx.current = buf;
574 ut_assertok(acpi_fill_ssdt(&ctx));
Simon Glass64bbc0d2020-07-07 13:12:12 -0600575 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700576 ut_assert_nextline("Seq Type Base Size Device/Writer");
577 ut_assert_nextline("--- ----- -------- ---- -------------");
578 ut_assert_nextline(" 0 ssdt %8lx 2 acpi-test", addr);
579 ut_assert_nextline(" 1 ssdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600580 ut_assert_console_end();
581
Simon Glassa385d492020-11-04 09:57:33 -0700582 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600583 ctx.current = buf;
584 ut_assertok(acpi_inject_dsdt(&ctx));
Simon Glass64bbc0d2020-07-07 13:12:12 -0600585 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700586 ut_assert_nextlinen("Seq");
587 ut_assert_nextlinen("---");
588 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
589 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600590 ut_assert_console_end();
591
Simon Glass64bbc0d2020-07-07 13:12:12 -0600592 run_command("acpi items -d", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700593 ut_assert_nextlinen("Seq");
594 ut_assert_nextlinen("---");
595 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600596 ut_assert_nextlines_are_dump(2);
597 ut_assert_nextline("%s", "");
Simon Glass884dcfa2021-12-01 09:03:05 -0700598 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600599 ut_assert_nextlines_are_dump(2);
600 ut_assert_nextline("%s", "");
601 ut_assert_console_end();
602
603 return 0;
604}
Simon Glassf54458b2024-08-22 07:57:59 -0600605DM_TEST(dm_test_acpi_cmd_items, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass2b943422023-07-15 21:38:45 -0600606
607/* Test 'acpi set' command */
608static int dm_test_acpi_cmd_set(struct unit_test_state *uts)
609{
610 struct acpi_ctx ctx;
611 ulong addr;
612 void *buf;
613
614 gd_set_acpi_start(0);
615
Simon Glass2b943422023-07-15 21:38:45 -0600616 ut_asserteq(0, gd_acpi_start());
617 ut_assertok(run_command("acpi set", 0));
618 ut_assert_nextline("ACPI pointer: 0");
619
620 buf = memalign(16, BUF_SIZE);
621 ut_assertnonnull(buf);
622 addr = map_to_sysmem(buf);
623 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
624
625 ut_assertok(acpi_write_dev_tables(&ctx));
626
627 ut_assertok(run_command("acpi set", 0));
628 ut_assert_nextline("ACPI pointer: %lx", addr);
629
630 ut_assertok(run_command("acpi set 0", 0));
631 ut_assert_nextline("Setting ACPI pointer to 0");
632 ut_asserteq(0, gd_acpi_start());
633
634 ut_assertok(run_commandf("acpi set %lx", addr));
635 ut_assert_nextline("Setting ACPI pointer to %lx", addr);
636 ut_asserteq(addr, gd_acpi_start());
637
638 ut_assert_console_end();
639
640 return 0;
641}
Simon Glassf54458b2024-08-22 07:57:59 -0600642DM_TEST(dm_test_acpi_cmd_set, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Heinrich Schuchardtedf77e62023-11-18 22:57:27 +0100643
644/**
645 * dm_test_write_test_table() - create test ACPI table
646 *
647 * Create an ACPI table TSTn, where n is given by @index.
648 *
649 * @ctx: ACPI table writing context
650 * @index: table index
651 * Return: generated table
652 */
653static struct acpi_table_header
654*dm_test_write_test_table(struct acpi_ctx *ctx, int index)
655{
656 struct acpi_table_header *tbl = ctx->current;
657 char signature[5];
658
659 snprintf(signature, sizeof(signature), "TST%1d", index);
660 memset(tbl, 0, sizeof(*tbl));
661 acpi_fill_header(tbl, signature);
662 acpi_inc(ctx, sizeof(struct acpi_table_header));
663 tbl->length = (u8 *)ctx->current - (u8 *)tbl;
664 tbl->checksum = table_compute_checksum(tbl, tbl->length);
665 acpi_add_table(ctx, tbl);
666
667 return tbl;
668}
669
670/* Test acpi_find_table() */
671static int dm_test_acpi_find_table(struct unit_test_state *uts)
672{
673 struct acpi_ctx ctx;
674 ulong acpi_start, addr;
675 void *buf;
676 struct acpi_table_header *table, *table1, *table2, *table3;
677 struct acpi_rsdp *rsdp;
678 ulong rsdt;
679 ulong xsdt;
680
681 /* Keep reference to original ACPI tables */
682 acpi_start = gd_acpi_start();
683
684 /* Setup new ACPI tables */
685 buf = memalign(16, BUF_SIZE);
686 ut_assertnonnull(buf);
687 addr = map_to_sysmem(buf);
688 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
689 table3 = dm_test_write_test_table(&ctx, 3);
690 table1 = dm_test_write_test_table(&ctx, 1);
691 table2 = dm_test_write_test_table(&ctx, 2);
692
693 /* Retrieve RSDP, RSDT, XSDT */
694 rsdp = map_sysmem(gd_acpi_start(), 0);
695 ut_assertnonnull(rsdp);
696 rsdt = rsdp->rsdt_address;
697 ut_assert(rsdt);
698 xsdt = rsdp->xsdt_address;
699 ut_assert(xsdt);
700
701 /* Find with both RSDT and XSDT */
702 table = acpi_find_table("TST1");
703 ut_asserteq_ptr(table1, table);
704 ut_asserteq_strn("TST1", table->signature);
705 table = acpi_find_table("TST2");
706 ut_asserteq_ptr(table2, table);
707 ut_asserteq_strn("TST2", table->signature);
708 table = acpi_find_table("TST3");
709 ut_asserteq_ptr(table3, table);
710 ut_asserteq_strn("TST3", table->signature);
711
712 /* Find with XSDT only */
713 rsdp->rsdt_address = 0;
714 table = acpi_find_table("TST1");
715 ut_asserteq_ptr(table1, table);
716 table = acpi_find_table("TST2");
717 ut_asserteq_ptr(table2, table);
718 table = acpi_find_table("TST3");
719 ut_asserteq_ptr(table3, table);
720 rsdp->rsdt_address = rsdt;
721
722 /* Find with RSDT only */
723 rsdp->xsdt_address = 0;
724 table = acpi_find_table("TST1");
725 ut_asserteq_ptr(table1, table);
726 table = acpi_find_table("TST2");
727 ut_asserteq_ptr(table2, table);
728 table = acpi_find_table("TST3");
729 ut_asserteq_ptr(table3, table);
730 rsdp->xsdt_address = xsdt;
731
732 /* Restore previous ACPI tables */
733 gd_set_acpi_start(acpi_start);
734 free(buf);
735
736 return 0;
737}
738DM_TEST(dm_test_acpi_find_table, 0);
Heinrich Schuchardtbb307a62023-11-21 15:41:27 +0100739
740/* Test offsets in RSDT, XSDT */
741static int dm_test_acpi_offsets(struct unit_test_state *uts)
742{
743 ut_asserteq(36, offsetof(struct acpi_rsdt, entry));
744 ut_asserteq(36, offsetof(struct acpi_xsdt, entry));
745
746 return 0;
747}
748DM_TEST(dm_test_acpi_offsets, 0);