blob: 39a26bbb49265abc5ef28aabfa557997045a6206 [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
Patrick Rudolphbff7c572024-10-23 15:19:51 +020098static int testacpi_fill_madt(const struct udevice *dev, struct acpi_ctx *ctx)
99{
100 u64 *data = ctx->current;
101
102 /* Only fill madt once */
103 if (device_get_uclass_id(dev->parent) != UCLASS_TEST_ACPI)
104 return 0;
105
106 *data = 0xdeadbeef;
107
108 acpi_inc(ctx, sizeof(u64));
109
110 return 0;
111}
112
Simon Glassd43e0ba2020-07-07 13:12:03 -0600113static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
114{
115 const char *data;
116
117 data = dev_read_string(dev, "acpi-ssdt-test-data");
118 if (data) {
119 while (*data)
120 acpigen_emit_byte(ctx, *data++);
121 }
122
123 return 0;
124}
125
Simon Glass990cd5b2020-07-07 13:12:08 -0600126static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
127{
128 const char *data;
129
130 data = dev_read_string(dev, "acpi-dsdt-test-data");
131 if (data) {
132 while (*data)
133 acpigen_emit_byte(ctx, *data++);
134 }
135
136 return 0;
137}
138
Simon Glass2d67fdf2020-04-08 16:57:34 -0600139struct acpi_ops testacpi_ops = {
140 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -0600141 .write_tables = testacpi_write_tables,
Patrick Rudolphbff7c572024-10-23 15:19:51 +0200142 .fill_madt = testacpi_fill_madt,
Simon Glassd43e0ba2020-07-07 13:12:03 -0600143 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass990cd5b2020-07-07 13:12:08 -0600144 .inject_dsdt = testacpi_inject_dsdt,
Simon Glass2d67fdf2020-04-08 16:57:34 -0600145};
146
147static const struct udevice_id testacpi_ids[] = {
148 { .compatible = "denx,u-boot-acpi-test" },
149 { }
150};
151
152U_BOOT_DRIVER(testacpi_drv) = {
153 .name = "testacpi_drv",
154 .of_match = testacpi_ids,
155 .id = UCLASS_TEST_ACPI,
Simon Glassebb2e832020-07-07 13:11:39 -0600156 .bind = dm_scan_fdt_dev,
Simon Glassb75b15b2020-12-03 16:55:23 -0700157 .plat_auto = sizeof(struct testacpi_plat),
Simon Glass2d67fdf2020-04-08 16:57:34 -0600158 ACPI_OPS_PTR(&testacpi_ops)
159};
160
161UCLASS_DRIVER(testacpi) = {
162 .name = "testacpi",
163 .id = UCLASS_TEST_ACPI,
164};
165
166/* Test ACPI get_name() */
167static int dm_test_acpi_get_name(struct unit_test_state *uts)
168{
169 char name[ACPI_NAME_MAX];
Simon Glass9a655132020-12-16 21:20:23 -0700170 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glass09642392020-07-07 13:12:11 -0600171 struct udevice *pci, *root;
Simon Glass2d67fdf2020-04-08 16:57:34 -0600172
Simon Glass09642392020-07-07 13:12:11 -0600173 /* Test getting the name from the driver */
Simon Glass2d67fdf2020-04-08 16:57:34 -0600174 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
175 ut_assertok(acpi_get_name(dev, name));
176 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
177
Simon Glass09642392020-07-07 13:12:11 -0600178 /* Test getting the name from the device tree */
179 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
180 &dev2));
181 ut_assertok(acpi_get_name(dev2, name));
182 ut_asserteq_str("GHIJ", name);
183
184 /* Test getting the name from acpi_device_get_name() */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200185 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
Simon Glass09642392020-07-07 13:12:11 -0600186 ut_assertok(acpi_get_name(i2c, name));
187 ut_asserteq_str("I2C0", name);
188
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200189 ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi));
Simon Glass09642392020-07-07 13:12:11 -0600190 ut_assertok(acpi_get_name(spi, name));
191 ut_asserteq_str("SPI0", name);
192
Simon Glass09642392020-07-07 13:12:11 -0600193 /* ACPI doesn't know about the timer */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200194 ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer));
Simon Glass09642392020-07-07 13:12:11 -0600195 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
196
197 /* May as well test the rest of the cases */
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200198 ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound));
Simon Glass09642392020-07-07 13:12:11 -0600199 ut_assertok(acpi_get_name(sound, name));
200 ut_asserteq_str("HDAS", name);
201
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200202 ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci));
Simon Glass09642392020-07-07 13:12:11 -0600203 ut_assertok(acpi_get_name(pci, name));
204 ut_asserteq_str("PCI0", name);
205
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200206 ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root));
Simon Glass09642392020-07-07 13:12:11 -0600207 ut_assertok(acpi_get_name(root, name));
208 ut_asserteq_str("\\_SB", name);
209
210 /* Note that we don't have tests for acpi_name_from_id() */
211
Simon Glass2d67fdf2020-04-08 16:57:34 -0600212 return 0;
213}
Simon Glass1a92f832024-08-22 07:57:48 -0600214DM_TEST(dm_test_acpi_get_name, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -0600215
216/* Test acpi_get_table_revision() */
217static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
218{
219 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
220 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
221 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
222 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
223
224 return 0;
225}
Simon Glass1a92f832024-08-22 07:57:48 -0600226DM_TEST(dm_test_acpi_get_table_revision, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -0600227
Simon Glasse9629892020-04-08 16:57:39 -0600228/* Test acpi_create_dmar() */
229static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
230{
231 struct acpi_dmar dmar;
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200232 struct udevice *cpu;
Simon Glasse9629892020-04-08 16:57:39 -0600233
Michal Suchanekac12a2f2022-10-12 21:57:59 +0200234 ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu));
Heinrich Schuchardt6c2a8712020-07-17 00:20:14 +0200235 ut_assertnonnull(cpu);
Simon Glasse9629892020-04-08 16:57:39 -0600236 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
237 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200238 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
239 dmar.host_address_width);
Simon Glasse9629892020-04-08 16:57:39 -0600240
241 return 0;
242}
Simon Glass1a92f832024-08-22 07:57:48 -0600243DM_TEST(dm_test_acpi_create_dmar, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600244
245/* Test acpi_fill_header() */
246static int dm_test_acpi_fill_header(struct unit_test_state *uts)
247{
248 struct acpi_table_header hdr;
249
250 /* Make sure these 5 fields are not changed */
251 hdr.length = 0x11;
252 hdr.revision = 0x22;
253 hdr.checksum = 0x33;
Simon Glass17968c32020-04-26 09:19:46 -0600254 acpi_fill_header(&hdr, "ABCD");
255
256 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
257 ut_asserteq(0x11, hdr.length);
258 ut_asserteq(0x22, hdr.revision);
259 ut_asserteq(0x33, hdr.checksum);
260 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
261 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
262 sizeof(hdr.oem_table_id));
Pali Rohárf0de20e2021-07-10 13:10:01 +0200263 ut_asserteq(OEM_REVISION, hdr.oem_revision);
Heinrich Schuchardt10de8a82024-01-21 12:52:48 +0100264 ut_asserteq_mem(ASLC_ID, hdr.creator_id, sizeof(hdr.creator_id));
Heinrich Schuchardt9838d342024-04-18 05:11:13 +0200265 ut_asserteq(ASL_REVISION, hdr.creator_revision);
Simon Glass17968c32020-04-26 09:19:46 -0600266
267 return 0;
268}
Simon Glass1a92f832024-08-22 07:57:48 -0600269DM_TEST(dm_test_acpi_fill_header, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600270
271/* Test ACPI write_tables() */
272static int dm_test_acpi_write_tables(struct unit_test_state *uts)
273{
274 struct acpi_dmar *dmar;
275 struct acpi_ctx ctx;
Simon Glass37acc662021-12-01 09:02:48 -0700276 ulong addr;
Simon Glass17968c32020-04-26 09:19:46 -0600277 void *buf;
Simon Glassebb2e832020-07-07 13:11:39 -0600278 int i;
Simon Glass17968c32020-04-26 09:19:46 -0600279
280 buf = malloc(BUF_SIZE);
281 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700282 addr = map_to_sysmem(buf);
Simon Glass17968c32020-04-26 09:19:46 -0600283
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700284 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass575a5472020-04-26 09:19:50 -0600285 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600286 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600287
288 /*
Simon Glassebb2e832020-07-07 13:11:39 -0600289 * We should have three dmar tables, one for each
290 * "denx,u-boot-acpi-test" device
Simon Glass17968c32020-04-26 09:19:46 -0600291 */
Simon Glassebb2e832020-07-07 13:11:39 -0600292 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass17968c32020-04-26 09:19:46 -0600293 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200294 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
295 dmar->host_address_width);
Simon Glass17968c32020-04-26 09:19:46 -0600296
297 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200298 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
299 dmar[1].host_address_width);
Simon Glass17968c32020-04-26 09:19:46 -0600300
Simon Glassebb2e832020-07-07 13:11:39 -0600301 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
Marek Vasut795f0d72023-08-13 03:25:59 +0200302 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
303 dmar[2].host_address_width);
Simon Glass9c442a62020-04-26 09:19:51 -0600304
Simon Glassebb2e832020-07-07 13:11:39 -0600305 /* Check that the pointers were added correctly */
306 for (i = 0; i < 3; i++) {
Simon Glass919f8352023-12-31 08:25:54 -0700307 ut_asserteq(nomap_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
308 ut_asserteq(nomap_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
Simon Glassebb2e832020-07-07 13:11:39 -0600309 }
310 ut_asserteq(0, ctx.rsdt->entry[3]);
311 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassabeaca82020-04-26 09:19:52 -0600312
Simon Glass17968c32020-04-26 09:19:46 -0600313 return 0;
314}
Simon Glass1a92f832024-08-22 07:57:48 -0600315DM_TEST(dm_test_acpi_write_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600316
317/* Test basic ACPI functions */
318static int dm_test_acpi_basic(struct unit_test_state *uts)
319{
320 struct acpi_ctx ctx;
321
322 /* Check align works */
323 ctx.current = (void *)5;
324 acpi_align(&ctx);
325 ut_asserteq_ptr((void *)16, ctx.current);
326
327 /* Check that align does nothing if already aligned */
328 acpi_align(&ctx);
329 ut_asserteq_ptr((void *)16, ctx.current);
330 acpi_align64(&ctx);
331 ut_asserteq_ptr((void *)64, ctx.current);
332 acpi_align64(&ctx);
333 ut_asserteq_ptr((void *)64, ctx.current);
334
335 /* Check incrementing */
336 acpi_inc(&ctx, 3);
337 ut_asserteq_ptr((void *)67, ctx.current);
338 acpi_inc_align(&ctx, 3);
339 ut_asserteq_ptr((void *)80, ctx.current);
340
341 return 0;
342}
Simon Glass1a92f832024-08-22 07:57:48 -0600343DM_TEST(dm_test_acpi_basic, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600344
Simon Glass16170882021-12-01 09:02:49 -0700345/* Test setup_ctx_and_base_tables */
Simon Glass8181e182023-12-31 08:25:53 -0700346static int dm_test_acpi_ctx_and_base_tables(struct unit_test_state *uts)
Simon Glass9c442a62020-04-26 09:19:51 -0600347{
348 struct acpi_rsdp *rsdp;
349 struct acpi_rsdt *rsdt;
350 struct acpi_xsdt *xsdt;
351 struct acpi_ctx ctx;
352 void *buf, *end;
Simon Glass37acc662021-12-01 09:02:48 -0700353 ulong addr;
Simon Glass9c442a62020-04-26 09:19:51 -0600354
355 /*
356 * Use an unaligned address deliberately, by allocating an aligned
357 * address and then adding 4 to it
358 */
359 buf = memalign(64, BUF_SIZE);
360 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700361 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700362 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
Simon Glassb33f2f92021-12-01 09:02:37 -0700363 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());
Simon Glass9c442a62020-04-26 09:19:51 -0600364
365 rsdp = buf + 16;
366 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassa3186e62020-05-10 12:52:45 -0600367 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass9c442a62020-04-26 09:19:51 -0600368 ut_asserteq(sizeof(*rsdp), rsdp->length);
369 ut_assertok(table_compute_checksum(rsdp, 20));
370 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
371
372 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
373 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600374 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600375 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
376 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
377
378 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassabeaca82020-04-26 09:19:52 -0600379 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassa3186e62020-05-10 12:52:45 -0600380 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass9c442a62020-04-26 09:19:51 -0600381 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
382 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
383
384 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
385 ut_asserteq_ptr(end, ctx.current);
386
Simon Glass919f8352023-12-31 08:25:54 -0700387 ut_asserteq(nomap_to_sysmem(rsdt), rsdp->rsdt_address);
388 ut_asserteq(nomap_to_sysmem(xsdt), rsdp->xsdt_address);
Simon Glass9c442a62020-04-26 09:19:51 -0600389
390 return 0;
391}
Simon Glass1a92f832024-08-22 07:57:48 -0600392DM_TEST(dm_test_acpi_ctx_and_base_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassfaf08c72020-04-26 09:19:53 -0600393
394/* Test 'acpi list' command */
395static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
396{
397 struct acpi_ctx ctx;
398 ulong addr;
399 void *buf;
400
401 buf = memalign(16, BUF_SIZE);
402 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700403 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700404 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600405
406 ut_assertok(acpi_write_dev_tables(&ctx));
407
Simon Glassfaf08c72020-04-26 09:19:53 -0600408 run_command("acpi list", 0);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100409 ut_assert_nextline("Name Base Size Detail");
410 ut_assert_nextline("---- ---------------- ----- ----------------------------");
411 ut_assert_nextline("RSDP %16lx %5zx v02 U-BOOT", addr,
Simon Glassfaf08c72020-04-26 09:19:53 -0600412 sizeof(struct acpi_rsdp));
413 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100414 ut_assert_nextline("RSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glassfaf08c72020-04-26 09:19:53 -0600415 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200416 3 * sizeof(u32), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600417 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100418 ut_assert_nextline("XSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glassfaf08c72020-04-26 09:19:53 -0600419 addr, sizeof(struct acpi_table_header) +
Pali Rohárf0de20e2021-07-10 13:10:01 +0200420 3 * sizeof(u64), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600421 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100422 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200423 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600424 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100425 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200426 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassebb2e832020-07-07 13:11:39 -0600427 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100428 ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohárf0de20e2021-07-10 13:10:01 +0200429 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glassfaf08c72020-04-26 09:19:53 -0600430 ut_assert_console_end();
431
432 return 0;
433}
Simon Glassf54458b2024-08-22 07:57:59 -0600434DM_TEST(dm_test_acpi_cmd_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassfaf08c72020-04-26 09:19:53 -0600435
436/* Test 'acpi dump' command */
437static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
438{
439 struct acpi_ctx ctx;
440 ulong addr;
441 void *buf;
442
443 buf = memalign(16, BUF_SIZE);
444 ut_assertnonnull(buf);
Simon Glass37acc662021-12-01 09:02:48 -0700445 addr = map_to_sysmem(buf);
Simon Glasse9f6e6a2021-12-01 09:02:50 -0700446 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glassfaf08c72020-04-26 09:19:53 -0600447
448 ut_assertok(acpi_write_dev_tables(&ctx));
449
450 /* First search for a non-existent table */
Simon Glassfaf08c72020-04-26 09:19:53 -0600451 run_command("acpi dump rdst", 0);
452 ut_assert_nextline("Table 'RDST' not found");
453 ut_assert_console_end();
454
455 /* Now a real table */
Simon Glassfaf08c72020-04-26 09:19:53 -0600456 run_command("acpi dump dmar", 0);
Simon Glass919f8352023-12-31 08:25:54 -0700457 addr = ALIGN(nomap_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
Heinrich Schuchardt27e9ebb2023-11-21 15:41:28 +0100458 ut_assert_nextline("DMAR @ %16lx", addr);
Simon Glassfaf08c72020-04-26 09:19:53 -0600459 ut_assert_nextlines_are_dump(0x30);
460 ut_assert_console_end();
461
462 return 0;
463}
Simon Glassf54458b2024-08-22 07:57:59 -0600464DM_TEST(dm_test_acpi_cmd_dump, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glassebb2e832020-07-07 13:11:39 -0600465
466/* Test acpi_device_path() */
467static int dm_test_acpi_device_path(struct unit_test_state *uts)
468{
Simon Glassb75b15b2020-12-03 16:55:23 -0700469 struct testacpi_plat *plat;
Simon Glassebb2e832020-07-07 13:11:39 -0600470 char buf[ACPI_PATH_MAX];
471 struct udevice *dev, *child;
472
473 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
474 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
475 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
476
477 /* Test running out of space */
478 buf[5] = '\0';
479 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
480 ut_asserteq('\0', buf[5]);
481
482 /* Test a three-component name */
483 ut_assertok(device_first_child_err(dev, &child));
484 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
485 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
486 buf);
487
488 /* Test handling of a device which doesn't produce a name */
Simon Glassfa20e932020-12-03 16:55:20 -0700489 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600490 plat->no_name = true;
491 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
492 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
493
494 /* Test handling of a device which returns an error */
Simon Glassfa20e932020-12-03 16:55:20 -0700495 plat = dev_get_plat(dev);
Simon Glassebb2e832020-07-07 13:11:39 -0600496 plat->return_error = true;
497 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
498
499 return 0;
500}
Simon Glass1a92f832024-08-22 07:57:48 -0600501DM_TEST(dm_test_acpi_device_path, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass8965cc92020-07-07 13:11:40 -0600502
503/* Test acpi_device_status() */
504static int dm_test_acpi_device_status(struct unit_test_state *uts)
505{
506 struct udevice *dev;
507
508 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
509 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
510
511 return 0;
512}
Simon Glass1a92f832024-08-22 07:57:48 -0600513DM_TEST(dm_test_acpi_device_status, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600514
515/* Test acpi_fill_ssdt() */
516static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
517{
518 struct acpi_ctx ctx;
519 u8 *buf;
520
521 buf = malloc(BUF_SIZE);
522 ut_assertnonnull(buf);
523
Simon Glassa385d492020-11-04 09:57:33 -0700524 acpi_reset_items();
Simon Glassd43e0ba2020-07-07 13:12:03 -0600525 ctx.current = buf;
526 buf[4] = 'z'; /* sentinel */
527 ut_assertok(acpi_fill_ssdt(&ctx));
528
Simon Glass3c601b12020-07-07 13:12:06 -0600529 /*
530 * These values come from acpi-test2's acpi-ssdt-test-data property.
531 * This device comes first because of u-boot,acpi-ssdt-order
532 */
533 ut_asserteq('c', buf[0]);
534 ut_asserteq('d', buf[1]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600535
Simon Glass3c601b12020-07-07 13:12:06 -0600536 /* These values come from acpi-test's acpi-ssdt-test-data property */
537 ut_asserteq('a', buf[2]);
538 ut_asserteq('b', buf[3]);
Simon Glassd43e0ba2020-07-07 13:12:03 -0600539
540 ut_asserteq('z', buf[4]);
541
542 return 0;
543}
Simon Glass1a92f832024-08-22 07:57:48 -0600544DM_TEST(dm_test_acpi_fill_ssdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass990cd5b2020-07-07 13:12:08 -0600545
Patrick Rudolphbff7c572024-10-23 15:19:51 +0200546/* Test acpi_fill_madt() */
547static int dm_test_acpi_fill_madt(struct unit_test_state *uts)
548{
549 struct acpi_ctx ctx;
550 u64 *buf;
551
552 buf = malloc(BUF_SIZE);
553 ut_assertnonnull(buf);
554
555 acpi_reset_items();
556 ctx.current = buf;
557 buf[1] = 'z'; /* sentinel */
558 ut_assertok(acpi_fill_madt_subtbl(&ctx));
559
560 /*
561 * These values come from acpi-test2's acpi-ssdt-test-data property.
562 * This device comes first because of u-boot,acpi-ssdt-order
563 */
564 ut_asserteq(0xdeadbeef, buf[0]);
565
566 ut_asserteq('z', buf[1]);
567
568 return 0;
569}
570
571DM_TEST(dm_test_acpi_fill_madt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
572
Simon Glass990cd5b2020-07-07 13:12:08 -0600573/* Test acpi_inject_dsdt() */
574static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
575{
576 struct acpi_ctx ctx;
577 u8 *buf;
578
579 buf = malloc(BUF_SIZE);
580 ut_assertnonnull(buf);
581
Simon Glassa385d492020-11-04 09:57:33 -0700582 acpi_reset_items();
Simon Glass990cd5b2020-07-07 13:12:08 -0600583 ctx.current = buf;
584 buf[4] = 'z'; /* sentinel */
585 ut_assertok(acpi_inject_dsdt(&ctx));
586
587 /*
588 * These values come from acpi-test's acpi-dsdt-test-data property.
589 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
590 */
591 ut_asserteq('h', buf[0]);
592 ut_asserteq('i', buf[1]);
593
594 /* These values come from acpi-test's acpi-dsdt-test-data property */
595 ut_asserteq('j', buf[2]);
596 ut_asserteq('k', buf[3]);
597
598 ut_asserteq('z', buf[4]);
599
600 return 0;
601}
Simon Glass1a92f832024-08-22 07:57:48 -0600602DM_TEST(dm_test_acpi_inject_dsdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600603
604/* Test 'acpi items' command */
605static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
606{
607 struct acpi_ctx ctx;
Simon Glass884dcfa2021-12-01 09:03:05 -0700608 ulong addr;
Simon Glass64bbc0d2020-07-07 13:12:12 -0600609 void *buf;
610
611 buf = malloc(BUF_SIZE);
612 ut_assertnonnull(buf);
Simon Glass884dcfa2021-12-01 09:03:05 -0700613 addr = map_to_sysmem(buf);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600614
Simon Glassa385d492020-11-04 09:57:33 -0700615 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600616 ctx.current = buf;
617 ut_assertok(acpi_fill_ssdt(&ctx));
Simon Glass64bbc0d2020-07-07 13:12:12 -0600618 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700619 ut_assert_nextline("Seq Type Base Size Device/Writer");
620 ut_assert_nextline("--- ----- -------- ---- -------------");
621 ut_assert_nextline(" 0 ssdt %8lx 2 acpi-test", addr);
622 ut_assert_nextline(" 1 ssdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600623 ut_assert_console_end();
624
Simon Glassa385d492020-11-04 09:57:33 -0700625 acpi_reset_items();
Simon Glass64bbc0d2020-07-07 13:12:12 -0600626 ctx.current = buf;
627 ut_assertok(acpi_inject_dsdt(&ctx));
Simon Glass64bbc0d2020-07-07 13:12:12 -0600628 run_command("acpi items", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700629 ut_assert_nextlinen("Seq");
630 ut_assert_nextlinen("---");
631 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
632 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600633 ut_assert_console_end();
634
Simon Glass64bbc0d2020-07-07 13:12:12 -0600635 run_command("acpi items -d", 0);
Simon Glass884dcfa2021-12-01 09:03:05 -0700636 ut_assert_nextlinen("Seq");
637 ut_assert_nextlinen("---");
638 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600639 ut_assert_nextlines_are_dump(2);
640 ut_assert_nextline("%s", "");
Simon Glass884dcfa2021-12-01 09:03:05 -0700641 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glass64bbc0d2020-07-07 13:12:12 -0600642 ut_assert_nextlines_are_dump(2);
643 ut_assert_nextline("%s", "");
644 ut_assert_console_end();
645
646 return 0;
647}
Simon Glassf54458b2024-08-22 07:57:59 -0600648DM_TEST(dm_test_acpi_cmd_items, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Simon Glass2b943422023-07-15 21:38:45 -0600649
650/* Test 'acpi set' command */
651static int dm_test_acpi_cmd_set(struct unit_test_state *uts)
652{
653 struct acpi_ctx ctx;
654 ulong addr;
655 void *buf;
656
657 gd_set_acpi_start(0);
658
Simon Glass2b943422023-07-15 21:38:45 -0600659 ut_asserteq(0, gd_acpi_start());
660 ut_assertok(run_command("acpi set", 0));
661 ut_assert_nextline("ACPI pointer: 0");
662
663 buf = memalign(16, BUF_SIZE);
664 ut_assertnonnull(buf);
665 addr = map_to_sysmem(buf);
666 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
667
668 ut_assertok(acpi_write_dev_tables(&ctx));
669
670 ut_assertok(run_command("acpi set", 0));
671 ut_assert_nextline("ACPI pointer: %lx", addr);
672
673 ut_assertok(run_command("acpi set 0", 0));
674 ut_assert_nextline("Setting ACPI pointer to 0");
675 ut_asserteq(0, gd_acpi_start());
676
677 ut_assertok(run_commandf("acpi set %lx", addr));
678 ut_assert_nextline("Setting ACPI pointer to %lx", addr);
679 ut_asserteq(addr, gd_acpi_start());
680
681 ut_assert_console_end();
682
683 return 0;
684}
Simon Glassf54458b2024-08-22 07:57:59 -0600685DM_TEST(dm_test_acpi_cmd_set, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
Heinrich Schuchardtedf77e62023-11-18 22:57:27 +0100686
687/**
688 * dm_test_write_test_table() - create test ACPI table
689 *
690 * Create an ACPI table TSTn, where n is given by @index.
691 *
692 * @ctx: ACPI table writing context
693 * @index: table index
694 * Return: generated table
695 */
696static struct acpi_table_header
697*dm_test_write_test_table(struct acpi_ctx *ctx, int index)
698{
699 struct acpi_table_header *tbl = ctx->current;
700 char signature[5];
701
702 snprintf(signature, sizeof(signature), "TST%1d", index);
703 memset(tbl, 0, sizeof(*tbl));
704 acpi_fill_header(tbl, signature);
705 acpi_inc(ctx, sizeof(struct acpi_table_header));
706 tbl->length = (u8 *)ctx->current - (u8 *)tbl;
707 tbl->checksum = table_compute_checksum(tbl, tbl->length);
708 acpi_add_table(ctx, tbl);
709
710 return tbl;
711}
712
713/* Test acpi_find_table() */
714static int dm_test_acpi_find_table(struct unit_test_state *uts)
715{
716 struct acpi_ctx ctx;
717 ulong acpi_start, addr;
718 void *buf;
719 struct acpi_table_header *table, *table1, *table2, *table3;
720 struct acpi_rsdp *rsdp;
721 ulong rsdt;
722 ulong xsdt;
723
724 /* Keep reference to original ACPI tables */
725 acpi_start = gd_acpi_start();
726
727 /* Setup new ACPI tables */
728 buf = memalign(16, BUF_SIZE);
729 ut_assertnonnull(buf);
730 addr = map_to_sysmem(buf);
731 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
732 table3 = dm_test_write_test_table(&ctx, 3);
733 table1 = dm_test_write_test_table(&ctx, 1);
734 table2 = dm_test_write_test_table(&ctx, 2);
735
736 /* Retrieve RSDP, RSDT, XSDT */
737 rsdp = map_sysmem(gd_acpi_start(), 0);
738 ut_assertnonnull(rsdp);
739 rsdt = rsdp->rsdt_address;
740 ut_assert(rsdt);
741 xsdt = rsdp->xsdt_address;
742 ut_assert(xsdt);
743
744 /* Find with both RSDT and XSDT */
745 table = acpi_find_table("TST1");
746 ut_asserteq_ptr(table1, table);
747 ut_asserteq_strn("TST1", table->signature);
748 table = acpi_find_table("TST2");
749 ut_asserteq_ptr(table2, table);
750 ut_asserteq_strn("TST2", table->signature);
751 table = acpi_find_table("TST3");
752 ut_asserteq_ptr(table3, table);
753 ut_asserteq_strn("TST3", table->signature);
754
755 /* Find with XSDT only */
756 rsdp->rsdt_address = 0;
757 table = acpi_find_table("TST1");
758 ut_asserteq_ptr(table1, table);
759 table = acpi_find_table("TST2");
760 ut_asserteq_ptr(table2, table);
761 table = acpi_find_table("TST3");
762 ut_asserteq_ptr(table3, table);
763 rsdp->rsdt_address = rsdt;
764
765 /* Find with RSDT only */
766 rsdp->xsdt_address = 0;
767 table = acpi_find_table("TST1");
768 ut_asserteq_ptr(table1, table);
769 table = acpi_find_table("TST2");
770 ut_asserteq_ptr(table2, table);
771 table = acpi_find_table("TST3");
772 ut_asserteq_ptr(table3, table);
773 rsdp->xsdt_address = xsdt;
774
775 /* Restore previous ACPI tables */
776 gd_set_acpi_start(acpi_start);
777 free(buf);
778
779 return 0;
780}
781DM_TEST(dm_test_acpi_find_table, 0);
Heinrich Schuchardtbb307a62023-11-21 15:41:27 +0100782
783/* Test offsets in RSDT, XSDT */
784static int dm_test_acpi_offsets(struct unit_test_state *uts)
785{
786 ut_asserteq(36, offsetof(struct acpi_rsdt, entry));
787 ut_asserteq(36, offsetof(struct acpi_xsdt, entry));
788
789 return 0;
790}
791DM_TEST(dm_test_acpi_offsets, 0);