blob: 5997bda649bda5eec54fe6a802939edc2598b2ec [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>
Simon Glass7e586f62020-04-26 09:19:51 -060014#include <tables_csum.h>
Simon Glass1e4d9652023-04-29 19:21:46 -060015#include <version_string.h>
Simon Glassb5183172020-07-07 13:12:03 -060016#include <acpi/acpigen.h>
Simon Glass1361a532020-07-07 13:11:39 -060017#include <acpi/acpi_device.h>
Simon Glass91fe8b72020-04-08 16:57:38 -060018#include <acpi/acpi_table.h>
Simon Glass401d1c42020-10-30 21:38:53 -060019#include <asm/global_data.h>
Simon Glassf50cc952020-04-08 16:57:34 -060020#include <dm/acpi.h>
21#include <dm/test.h>
22#include <test/ut.h>
Simon Glass82659cc2020-07-07 21:32:10 -060023#include "acpi.h"
Simon Glassf50cc952020-04-08 16:57:34 -060024
Simon Glass93f7f822020-04-26 09:19:46 -060025#define BUF_SIZE 4096
26
Simon Glass1e4d9652023-04-29 19:21:46 -060027#define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \
28 (((version_num / 100) % 10) << 24) | \
29 (((version_num / 10) % 10) << 20) | \
30 ((version_num % 10) << 16) | \
31 (((version_num_patch / 10) % 10) << 12) | \
32 ((version_num_patch % 10) << 8) | \
Pali Rohára3423b32021-07-10 13:10:01 +020033 0x01)
34
Simon Glass1361a532020-07-07 13:11:39 -060035/**
Simon Glass8a8d24b2020-12-03 16:55:23 -070036 * struct testacpi_plat - Platform data for the test ACPI device
Simon Glass1361a532020-07-07 13:11:39 -060037 *
38 * @no_name: true to emit an empty ACPI name from testacpi_get_name()
39 * @return_error: true to return an error instead of a name
40 */
Simon Glass8a8d24b2020-12-03 16:55:23 -070041struct testacpi_plat {
Simon Glass1361a532020-07-07 13:11:39 -060042 bool return_error;
43 bool no_name;
44};
45
Simon Glasscc1f8c32021-12-01 09:02:48 -070046/**
47 * setup_ctx_and_base_tables() - Set up context along with RSDP, RSDT and XSDT
48 *
49 * Set up the context with the given start position. Some basic tables are
50 * always needed, so set them up as well.
51 *
52 * @ctx: Context to set up
53 */
Simon Glass94ba15a2021-12-01 09:02:50 -070054static int setup_ctx_and_base_tables(struct unit_test_state *uts,
55 struct acpi_ctx *ctx, ulong start)
Simon Glasscc1f8c32021-12-01 09:02:48 -070056{
Simon Glass94ba15a2021-12-01 09:02:50 -070057 struct acpi_writer *entry = ACPI_WRITER_GET(0base);
58
Simon Glasscc1f8c32021-12-01 09:02:48 -070059 acpi_setup_ctx(ctx, start);
Simon Glass94ba15a2021-12-01 09:02:50 -070060
61 ctx->tab_start = ctx->current;
62 ut_assertok(acpi_write_one(ctx, entry));
Simon Glasscc1f8c32021-12-01 09:02:48 -070063
64 return 0;
65}
66
Simon Glass93f7f822020-04-26 09:19:46 -060067static int testacpi_write_tables(const struct udevice *dev,
68 struct acpi_ctx *ctx)
69{
70 struct acpi_dmar *dmar;
Simon Glass29b35112020-04-26 09:19:50 -060071 int ret;
Simon Glass93f7f822020-04-26 09:19:46 -060072
73 dmar = (struct acpi_dmar *)ctx->current;
74 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
75 ctx->current += sizeof(struct acpi_dmar);
Simon Glass29b35112020-04-26 09:19:50 -060076 ret = acpi_add_table(ctx, dmar);
77 if (ret)
78 return log_msg_ret("add", ret);
Simon Glass93f7f822020-04-26 09:19:46 -060079
80 return 0;
81}
Simon Glassf50cc952020-04-08 16:57:34 -060082
83static int testacpi_get_name(const struct udevice *dev, char *out_name)
84{
Simon Glass8a8d24b2020-12-03 16:55:23 -070085 struct testacpi_plat *plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -060086
87 if (plat->return_error)
88 return -EINVAL;
89 if (plat->no_name) {
90 *out_name = '\0';
91 return 0;
92 }
93 if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
94 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
95 else
96 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
Simon Glassf50cc952020-04-08 16:57:34 -060097}
98
Simon Glassb5183172020-07-07 13:12:03 -060099static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
100{
101 const char *data;
102
103 data = dev_read_string(dev, "acpi-ssdt-test-data");
104 if (data) {
105 while (*data)
106 acpigen_emit_byte(ctx, *data++);
107 }
108
109 return 0;
110}
111
Simon Glass01694582020-07-07 13:12:08 -0600112static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
113{
114 const char *data;
115
116 data = dev_read_string(dev, "acpi-dsdt-test-data");
117 if (data) {
118 while (*data)
119 acpigen_emit_byte(ctx, *data++);
120 }
121
122 return 0;
123}
124
Simon Glassf50cc952020-04-08 16:57:34 -0600125struct acpi_ops testacpi_ops = {
126 .get_name = testacpi_get_name,
Simon Glass93f7f822020-04-26 09:19:46 -0600127 .write_tables = testacpi_write_tables,
Simon Glassb5183172020-07-07 13:12:03 -0600128 .fill_ssdt = testacpi_fill_ssdt,
Simon Glass01694582020-07-07 13:12:08 -0600129 .inject_dsdt = testacpi_inject_dsdt,
Simon Glassf50cc952020-04-08 16:57:34 -0600130};
131
132static const struct udevice_id testacpi_ids[] = {
133 { .compatible = "denx,u-boot-acpi-test" },
134 { }
135};
136
137U_BOOT_DRIVER(testacpi_drv) = {
138 .name = "testacpi_drv",
139 .of_match = testacpi_ids,
140 .id = UCLASS_TEST_ACPI,
Simon Glass1361a532020-07-07 13:11:39 -0600141 .bind = dm_scan_fdt_dev,
Simon Glass8a8d24b2020-12-03 16:55:23 -0700142 .plat_auto = sizeof(struct testacpi_plat),
Simon Glassf50cc952020-04-08 16:57:34 -0600143 ACPI_OPS_PTR(&testacpi_ops)
144};
145
146UCLASS_DRIVER(testacpi) = {
147 .name = "testacpi",
148 .id = UCLASS_TEST_ACPI,
149};
150
151/* Test ACPI get_name() */
152static int dm_test_acpi_get_name(struct unit_test_state *uts)
153{
154 char name[ACPI_NAME_MAX];
Simon Glassd1e85302020-12-16 21:20:23 -0700155 struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
Simon Glassfefac0b2020-07-07 13:12:11 -0600156 struct udevice *pci, *root;
Simon Glassf50cc952020-04-08 16:57:34 -0600157
Simon Glassfefac0b2020-07-07 13:12:11 -0600158 /* Test getting the name from the driver */
Simon Glassf50cc952020-04-08 16:57:34 -0600159 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
160 ut_assertok(acpi_get_name(dev, name));
161 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
162
Simon Glassfefac0b2020-07-07 13:12:11 -0600163 /* Test getting the name from the device tree */
164 ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
165 &dev2));
166 ut_assertok(acpi_get_name(dev2, name));
167 ut_asserteq_str("GHIJ", name);
168
169 /* Test getting the name from acpi_device_get_name() */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200170 ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
Simon Glassfefac0b2020-07-07 13:12:11 -0600171 ut_assertok(acpi_get_name(i2c, name));
172 ut_asserteq_str("I2C0", name);
173
Michal Suchanekc726fc02022-10-12 21:57:59 +0200174 ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi));
Simon Glassfefac0b2020-07-07 13:12:11 -0600175 ut_assertok(acpi_get_name(spi, name));
176 ut_asserteq_str("SPI0", name);
177
Simon Glassfefac0b2020-07-07 13:12:11 -0600178 /* ACPI doesn't know about the timer */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200179 ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer));
Simon Glassfefac0b2020-07-07 13:12:11 -0600180 ut_asserteq(-ENOENT, acpi_get_name(timer, name));
181
182 /* May as well test the rest of the cases */
Michal Suchanekc726fc02022-10-12 21:57:59 +0200183 ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound));
Simon Glassfefac0b2020-07-07 13:12:11 -0600184 ut_assertok(acpi_get_name(sound, name));
185 ut_asserteq_str("HDAS", name);
186
Michal Suchanekc726fc02022-10-12 21:57:59 +0200187 ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci));
Simon Glassfefac0b2020-07-07 13:12:11 -0600188 ut_assertok(acpi_get_name(pci, name));
189 ut_asserteq_str("PCI0", name);
190
Michal Suchanekc726fc02022-10-12 21:57:59 +0200191 ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root));
Simon Glassfefac0b2020-07-07 13:12:11 -0600192 ut_assertok(acpi_get_name(root, name));
193 ut_asserteq_str("\\_SB", name);
194
195 /* Note that we don't have tests for acpi_name_from_id() */
196
Simon Glassf50cc952020-04-08 16:57:34 -0600197 return 0;
198}
Simon Glasse180c2b2020-07-28 19:41:12 -0600199DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass91fe8b72020-04-08 16:57:38 -0600200
201/* Test acpi_get_table_revision() */
202static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
203{
204 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
205 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
206 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
207 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
208
209 return 0;
210}
211DM_TEST(dm_test_acpi_get_table_revision,
Simon Glasse180c2b2020-07-28 19:41:12 -0600212 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassbfeb5d42020-04-08 16:57:39 -0600213
Simon Glassbfeb5d42020-04-08 16:57:39 -0600214/* Test acpi_create_dmar() */
215static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
216{
217 struct acpi_dmar dmar;
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200218 struct udevice *cpu;
Simon Glassbfeb5d42020-04-08 16:57:39 -0600219
Michal Suchanekc726fc02022-10-12 21:57:59 +0200220 ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu));
Heinrich Schuchardt331caea2020-07-17 00:20:14 +0200221 ut_assertnonnull(cpu);
Simon Glassbfeb5d42020-04-08 16:57:39 -0600222 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
223 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
Marek Vasut5ebe7902023-08-13 03:25:59 +0200224 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
225 dmar.host_address_width);
Simon Glassbfeb5d42020-04-08 16:57:39 -0600226
227 return 0;
228}
Simon Glasse180c2b2020-07-28 19:41:12 -0600229DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass93f7f822020-04-26 09:19:46 -0600230
231/* Test acpi_fill_header() */
232static int dm_test_acpi_fill_header(struct unit_test_state *uts)
233{
234 struct acpi_table_header hdr;
235
236 /* Make sure these 5 fields are not changed */
237 hdr.length = 0x11;
238 hdr.revision = 0x22;
239 hdr.checksum = 0x33;
240 hdr.aslc_revision = 0x44;
241 acpi_fill_header(&hdr, "ABCD");
242
243 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
244 ut_asserteq(0x11, hdr.length);
245 ut_asserteq(0x22, hdr.revision);
246 ut_asserteq(0x33, hdr.checksum);
247 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
248 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
249 sizeof(hdr.oem_table_id));
Pali Rohára3423b32021-07-10 13:10:01 +0200250 ut_asserteq(OEM_REVISION, hdr.oem_revision);
Simon Glass93f7f822020-04-26 09:19:46 -0600251 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
252 ut_asserteq(0x44, hdr.aslc_revision);
253
254 return 0;
255}
Simon Glasse180c2b2020-07-28 19:41:12 -0600256DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass93f7f822020-04-26 09:19:46 -0600257
258/* Test ACPI write_tables() */
259static int dm_test_acpi_write_tables(struct unit_test_state *uts)
260{
261 struct acpi_dmar *dmar;
262 struct acpi_ctx ctx;
Simon Glasscc1f8c32021-12-01 09:02:48 -0700263 ulong addr;
Simon Glass93f7f822020-04-26 09:19:46 -0600264 void *buf;
Simon Glass1361a532020-07-07 13:11:39 -0600265 int i;
Simon Glass93f7f822020-04-26 09:19:46 -0600266
267 buf = malloc(BUF_SIZE);
268 ut_assertnonnull(buf);
Simon Glasscc1f8c32021-12-01 09:02:48 -0700269 addr = map_to_sysmem(buf);
Simon Glass93f7f822020-04-26 09:19:46 -0600270
Simon Glass94ba15a2021-12-01 09:02:50 -0700271 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass29b35112020-04-26 09:19:50 -0600272 dmar = ctx.current;
Simon Glass93f7f822020-04-26 09:19:46 -0600273 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass93f7f822020-04-26 09:19:46 -0600274
275 /*
Simon Glass1361a532020-07-07 13:11:39 -0600276 * We should have three dmar tables, one for each
277 * "denx,u-boot-acpi-test" device
Simon Glass93f7f822020-04-26 09:19:46 -0600278 */
Simon Glass1361a532020-07-07 13:11:39 -0600279 ut_asserteq_ptr(dmar + 3, ctx.current);
Simon Glass93f7f822020-04-26 09:19:46 -0600280 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
Marek Vasut5ebe7902023-08-13 03:25:59 +0200281 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
282 dmar->host_address_width);
Simon Glass93f7f822020-04-26 09:19:46 -0600283
284 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
Marek Vasut5ebe7902023-08-13 03:25:59 +0200285 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
286 dmar[1].host_address_width);
Simon Glass93f7f822020-04-26 09:19:46 -0600287
Simon Glass1361a532020-07-07 13:11:39 -0600288 ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
Marek Vasut5ebe7902023-08-13 03:25:59 +0200289 ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
290 dmar[2].host_address_width);
Simon Glass7e586f62020-04-26 09:19:51 -0600291
Simon Glass1361a532020-07-07 13:11:39 -0600292 /* Check that the pointers were added correctly */
293 for (i = 0; i < 3; i++) {
294 ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
295 ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
296 }
297 ut_asserteq(0, ctx.rsdt->entry[3]);
298 ut_asserteq(0, ctx.xsdt->entry[3]);
Simon Glassb38309b2020-04-26 09:19:52 -0600299
Simon Glass93f7f822020-04-26 09:19:46 -0600300 return 0;
301}
Simon Glasse180c2b2020-07-28 19:41:12 -0600302DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass86e17782020-04-26 09:19:47 -0600303
304/* Test basic ACPI functions */
305static int dm_test_acpi_basic(struct unit_test_state *uts)
306{
307 struct acpi_ctx ctx;
308
309 /* Check align works */
310 ctx.current = (void *)5;
311 acpi_align(&ctx);
312 ut_asserteq_ptr((void *)16, ctx.current);
313
314 /* Check that align does nothing if already aligned */
315 acpi_align(&ctx);
316 ut_asserteq_ptr((void *)16, ctx.current);
317 acpi_align64(&ctx);
318 ut_asserteq_ptr((void *)64, ctx.current);
319 acpi_align64(&ctx);
320 ut_asserteq_ptr((void *)64, ctx.current);
321
322 /* Check incrementing */
323 acpi_inc(&ctx, 3);
324 ut_asserteq_ptr((void *)67, ctx.current);
325 acpi_inc_align(&ctx, 3);
326 ut_asserteq_ptr((void *)80, ctx.current);
327
328 return 0;
329}
Simon Glasse180c2b2020-07-28 19:41:12 -0600330DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7e586f62020-04-26 09:19:51 -0600331
Simon Glass31c27eb2021-12-01 09:02:49 -0700332/* Test setup_ctx_and_base_tables */
333static int dm_test_setup_ctx_and_base_tables(struct unit_test_state *uts)
Simon Glass7e586f62020-04-26 09:19:51 -0600334{
335 struct acpi_rsdp *rsdp;
336 struct acpi_rsdt *rsdt;
337 struct acpi_xsdt *xsdt;
338 struct acpi_ctx ctx;
339 void *buf, *end;
Simon Glasscc1f8c32021-12-01 09:02:48 -0700340 ulong addr;
Simon Glass7e586f62020-04-26 09:19:51 -0600341
342 /*
343 * Use an unaligned address deliberately, by allocating an aligned
344 * address and then adding 4 to it
345 */
346 buf = memalign(64, BUF_SIZE);
347 ut_assertnonnull(buf);
Simon Glasscc1f8c32021-12-01 09:02:48 -0700348 addr = map_to_sysmem(buf);
Simon Glass94ba15a2021-12-01 09:02:50 -0700349 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
Simon Glass233f0e32021-12-01 09:02:37 -0700350 ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());
Simon Glass7e586f62020-04-26 09:19:51 -0600351
352 rsdp = buf + 16;
353 ut_asserteq_ptr(rsdp, ctx.rsdp);
Simon Glassf91f3662020-05-10 12:52:45 -0600354 ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
Simon Glass7e586f62020-04-26 09:19:51 -0600355 ut_asserteq(sizeof(*rsdp), rsdp->length);
356 ut_assertok(table_compute_checksum(rsdp, 20));
357 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
358
359 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
360 ut_asserteq_ptr(rsdt, ctx.rsdt);
Simon Glassf91f3662020-05-10 12:52:45 -0600361 ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
Simon Glass7e586f62020-04-26 09:19:51 -0600362 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
363 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
364
365 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassb38309b2020-04-26 09:19:52 -0600366 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glassf91f3662020-05-10 12:52:45 -0600367 ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
Simon Glass7e586f62020-04-26 09:19:51 -0600368 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
369 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
370
371 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
372 ut_asserteq_ptr(end, ctx.current);
373
374 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
375 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
376
377 return 0;
378}
Simon Glass31c27eb2021-12-01 09:02:49 -0700379DM_TEST(dm_test_setup_ctx_and_base_tables,
Simon Glasse180c2b2020-07-28 19:41:12 -0600380 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0b885bc2020-04-26 09:19:53 -0600381
382/* Test 'acpi list' command */
383static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
384{
385 struct acpi_ctx ctx;
386 ulong addr;
387 void *buf;
388
389 buf = memalign(16, BUF_SIZE);
390 ut_assertnonnull(buf);
Simon Glasscc1f8c32021-12-01 09:02:48 -0700391 addr = map_to_sysmem(buf);
Simon Glass94ba15a2021-12-01 09:02:50 -0700392 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass0b885bc2020-04-26 09:19:53 -0600393
394 ut_assertok(acpi_write_dev_tables(&ctx));
395
396 console_record_reset();
397 run_command("acpi list", 0);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700398 ut_assert_nextline("Name Base Size Detail");
399 ut_assert_nextline("---- -------- ----- ------");
400 ut_assert_nextline("RSDP %08lx %5zx v02 U-BOOT", addr,
Simon Glass0b885bc2020-04-26 09:19:53 -0600401 sizeof(struct acpi_rsdp));
402 addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700403 ut_assert_nextline("RSDT %08lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glass0b885bc2020-04-26 09:19:53 -0600404 addr, sizeof(struct acpi_table_header) +
Pali Rohára3423b32021-07-10 13:10:01 +0200405 3 * sizeof(u32), OEM_REVISION);
Simon Glass0b885bc2020-04-26 09:19:53 -0600406 addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700407 ut_assert_nextline("XSDT %08lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Simon Glass0b885bc2020-04-26 09:19:53 -0600408 addr, sizeof(struct acpi_table_header) +
Pali Rohára3423b32021-07-10 13:10:01 +0200409 3 * sizeof(u64), OEM_REVISION);
Simon Glass0b885bc2020-04-26 09:19:53 -0600410 addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700411 ut_assert_nextline("DMAR %08lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohára3423b32021-07-10 13:10:01 +0200412 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glass0b885bc2020-04-26 09:19:53 -0600413 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700414 ut_assert_nextline("DMAR %08lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohára3423b32021-07-10 13:10:01 +0200415 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glass1361a532020-07-07 13:11:39 -0600416 addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
Simon Glass06f6f3d2021-12-01 09:03:06 -0700417 ut_assert_nextline("DMAR %08lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0",
Pali Rohára3423b32021-07-10 13:10:01 +0200418 addr, sizeof(struct acpi_dmar), OEM_REVISION);
Simon Glass0b885bc2020-04-26 09:19:53 -0600419 ut_assert_console_end();
420
421 return 0;
422}
Simon Glasse180c2b2020-07-28 19:41:12 -0600423DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0b885bc2020-04-26 09:19:53 -0600424
425/* Test 'acpi dump' command */
426static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
427{
428 struct acpi_ctx ctx;
429 ulong addr;
430 void *buf;
431
432 buf = memalign(16, BUF_SIZE);
433 ut_assertnonnull(buf);
Simon Glasscc1f8c32021-12-01 09:02:48 -0700434 addr = map_to_sysmem(buf);
Simon Glass94ba15a2021-12-01 09:02:50 -0700435 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
Simon Glass0b885bc2020-04-26 09:19:53 -0600436
437 ut_assertok(acpi_write_dev_tables(&ctx));
438
439 /* First search for a non-existent table */
440 console_record_reset();
441 run_command("acpi dump rdst", 0);
442 ut_assert_nextline("Table 'RDST' not found");
443 ut_assert_console_end();
444
445 /* Now a real table */
446 console_record_reset();
447 run_command("acpi dump dmar", 0);
448 addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
449 ut_assert_nextline("DMAR @ %08lx", addr);
450 ut_assert_nextlines_are_dump(0x30);
451 ut_assert_console_end();
452
453 return 0;
454}
Simon Glasse180c2b2020-07-28 19:41:12 -0600455DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass1361a532020-07-07 13:11:39 -0600456
457/* Test acpi_device_path() */
458static int dm_test_acpi_device_path(struct unit_test_state *uts)
459{
Simon Glass8a8d24b2020-12-03 16:55:23 -0700460 struct testacpi_plat *plat;
Simon Glass1361a532020-07-07 13:11:39 -0600461 char buf[ACPI_PATH_MAX];
462 struct udevice *dev, *child;
463
464 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
465 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
466 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
467
468 /* Test running out of space */
469 buf[5] = '\0';
470 ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
471 ut_asserteq('\0', buf[5]);
472
473 /* Test a three-component name */
474 ut_assertok(device_first_child_err(dev, &child));
475 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
476 ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
477 buf);
478
479 /* Test handling of a device which doesn't produce a name */
Simon Glassc69cda22020-12-03 16:55:20 -0700480 plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -0600481 plat->no_name = true;
482 ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
483 ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
484
485 /* Test handling of a device which returns an error */
Simon Glassc69cda22020-12-03 16:55:20 -0700486 plat = dev_get_plat(dev);
Simon Glass1361a532020-07-07 13:11:39 -0600487 plat->return_error = true;
488 ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
489
490 return 0;
491}
Simon Glasse180c2b2020-07-28 19:41:12 -0600492DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass2715b362020-07-07 13:11:40 -0600493
494/* Test acpi_device_status() */
495static int dm_test_acpi_device_status(struct unit_test_state *uts)
496{
497 struct udevice *dev;
498
499 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
500 ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
501
502 return 0;
503}
Simon Glasse180c2b2020-07-28 19:41:12 -0600504DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassb5183172020-07-07 13:12:03 -0600505
506/* Test acpi_fill_ssdt() */
507static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
508{
509 struct acpi_ctx ctx;
510 u8 *buf;
511
512 buf = malloc(BUF_SIZE);
513 ut_assertnonnull(buf);
514
Simon Glass18434ae2020-11-04 09:57:33 -0700515 acpi_reset_items();
Simon Glassb5183172020-07-07 13:12:03 -0600516 ctx.current = buf;
517 buf[4] = 'z'; /* sentinel */
518 ut_assertok(acpi_fill_ssdt(&ctx));
519
Simon Glass0f7b1112020-07-07 13:12:06 -0600520 /*
521 * These values come from acpi-test2's acpi-ssdt-test-data property.
522 * This device comes first because of u-boot,acpi-ssdt-order
523 */
524 ut_asserteq('c', buf[0]);
525 ut_asserteq('d', buf[1]);
Simon Glassb5183172020-07-07 13:12:03 -0600526
Simon Glass0f7b1112020-07-07 13:12:06 -0600527 /* These values come from acpi-test's acpi-ssdt-test-data property */
528 ut_asserteq('a', buf[2]);
529 ut_asserteq('b', buf[3]);
Simon Glassb5183172020-07-07 13:12:03 -0600530
531 ut_asserteq('z', buf[4]);
532
533 return 0;
534}
Simon Glasse180c2b2020-07-28 19:41:12 -0600535DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass01694582020-07-07 13:12:08 -0600536
537/* Test acpi_inject_dsdt() */
538static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
539{
540 struct acpi_ctx ctx;
541 u8 *buf;
542
543 buf = malloc(BUF_SIZE);
544 ut_assertnonnull(buf);
545
Simon Glass18434ae2020-11-04 09:57:33 -0700546 acpi_reset_items();
Simon Glass01694582020-07-07 13:12:08 -0600547 ctx.current = buf;
548 buf[4] = 'z'; /* sentinel */
549 ut_assertok(acpi_inject_dsdt(&ctx));
550
551 /*
552 * These values come from acpi-test's acpi-dsdt-test-data property.
553 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
554 */
555 ut_asserteq('h', buf[0]);
556 ut_asserteq('i', buf[1]);
557
558 /* These values come from acpi-test's acpi-dsdt-test-data property */
559 ut_asserteq('j', buf[2]);
560 ut_asserteq('k', buf[3]);
561
562 ut_asserteq('z', buf[4]);
563
564 return 0;
565}
Simon Glasse180c2b2020-07-28 19:41:12 -0600566DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa4f82082020-07-07 13:12:12 -0600567
568/* Test 'acpi items' command */
569static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
570{
571 struct acpi_ctx ctx;
Simon Glassa9246412021-12-01 09:03:05 -0700572 ulong addr;
Simon Glassa4f82082020-07-07 13:12:12 -0600573 void *buf;
574
575 buf = malloc(BUF_SIZE);
576 ut_assertnonnull(buf);
Simon Glassa9246412021-12-01 09:03:05 -0700577 addr = map_to_sysmem(buf);
Simon Glassa4f82082020-07-07 13:12:12 -0600578
Simon Glass18434ae2020-11-04 09:57:33 -0700579 acpi_reset_items();
Simon Glassa4f82082020-07-07 13:12:12 -0600580 ctx.current = buf;
581 ut_assertok(acpi_fill_ssdt(&ctx));
582 console_record_reset();
583 run_command("acpi items", 0);
Simon Glassa9246412021-12-01 09:03:05 -0700584 ut_assert_nextline("Seq Type Base Size Device/Writer");
585 ut_assert_nextline("--- ----- -------- ---- -------------");
586 ut_assert_nextline(" 0 ssdt %8lx 2 acpi-test", addr);
587 ut_assert_nextline(" 1 ssdt %8lx 2 acpi-test2", addr + 2);
Simon Glassa4f82082020-07-07 13:12:12 -0600588 ut_assert_console_end();
589
Simon Glass18434ae2020-11-04 09:57:33 -0700590 acpi_reset_items();
Simon Glassa4f82082020-07-07 13:12:12 -0600591 ctx.current = buf;
592 ut_assertok(acpi_inject_dsdt(&ctx));
593 console_record_reset();
594 run_command("acpi items", 0);
Simon Glassa9246412021-12-01 09:03:05 -0700595 ut_assert_nextlinen("Seq");
596 ut_assert_nextlinen("---");
597 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
598 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glassa4f82082020-07-07 13:12:12 -0600599 ut_assert_console_end();
600
601 console_record_reset();
602 run_command("acpi items -d", 0);
Simon Glassa9246412021-12-01 09:03:05 -0700603 ut_assert_nextlinen("Seq");
604 ut_assert_nextlinen("---");
605 ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr);
Simon Glassa4f82082020-07-07 13:12:12 -0600606 ut_assert_nextlines_are_dump(2);
607 ut_assert_nextline("%s", "");
Simon Glassa9246412021-12-01 09:03:05 -0700608 ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2);
Simon Glassa4f82082020-07-07 13:12:12 -0600609 ut_assert_nextlines_are_dump(2);
610 ut_assert_nextline("%s", "");
611 ut_assert_console_end();
612
613 return 0;
614}
Simon Glasse180c2b2020-07-28 19:41:12 -0600615DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass29718412023-07-15 21:38:45 -0600616
617/* Test 'acpi set' command */
618static int dm_test_acpi_cmd_set(struct unit_test_state *uts)
619{
620 struct acpi_ctx ctx;
621 ulong addr;
622 void *buf;
623
624 gd_set_acpi_start(0);
625
626 console_record_reset();
627 ut_asserteq(0, gd_acpi_start());
628 ut_assertok(run_command("acpi set", 0));
629 ut_assert_nextline("ACPI pointer: 0");
630
631 buf = memalign(16, BUF_SIZE);
632 ut_assertnonnull(buf);
633 addr = map_to_sysmem(buf);
634 ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
635
636 ut_assertok(acpi_write_dev_tables(&ctx));
637
638 ut_assertok(run_command("acpi set", 0));
639 ut_assert_nextline("ACPI pointer: %lx", addr);
640
641 ut_assertok(run_command("acpi set 0", 0));
642 ut_assert_nextline("Setting ACPI pointer to 0");
643 ut_asserteq(0, gd_acpi_start());
644
645 ut_assertok(run_commandf("acpi set %lx", addr));
646 ut_assert_nextline("Setting ACPI pointer to %lx", addr);
647 ut_asserteq(addr, gd_acpi_start());
648
649 ut_assert_console_end();
650
651 return 0;
652}
653DM_TEST(dm_test_acpi_cmd_set, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);