1 // 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 <console.h>
10 #include <dm.h>
11 #include <malloc.h>
12 #include <mapmem.h>
13 #include <tables_csum.h>
14 #include <version_string.h>
15 #include <acpi/acpigen.h>
16 #include <acpi/acpi_device.h>
17 #include <acpi/acpi_table.h>
18 #include <asm/global_data.h>
19 #include <dm/acpi.h>
20 #include <dm/test.h>
21 #include <test/ut.h>
22 #include "acpi.h"
23 
24 #define BUF_SIZE		4096
25 
26 #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) | \
32 		      0x01)
33 
34 /**
35  * struct testacpi_plat - Platform data for the test ACPI device
36  *
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  */
40 struct testacpi_plat {
41 	bool return_error;
42 	bool no_name;
43 };
44 
45 /**
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  */
setup_ctx_and_base_tables(struct unit_test_state * uts,struct acpi_ctx * ctx,ulong start)53 static int setup_ctx_and_base_tables(struct unit_test_state *uts,
54 				     struct acpi_ctx *ctx, ulong start)
55 {
56 	struct acpi_writer *entry = ACPI_WRITER_GET(0base);
57 
58 	acpi_setup_ctx(ctx, start);
59 
60 	ctx->tab_start = ctx->current;
61 	ut_assertok(acpi_write_one(ctx, entry));
62 
63 	return 0;
64 }
65 
testacpi_write_tables(const struct udevice * dev,struct acpi_ctx * ctx)66 static int testacpi_write_tables(const struct udevice *dev,
67 				 struct acpi_ctx *ctx)
68 {
69 	struct acpi_dmar *dmar;
70 	int ret;
71 
72 	dmar = (struct acpi_dmar *)ctx->current;
73 	acpi_create_dmar(dmar, DMAR_INTR_REMAP);
74 	ctx->current += sizeof(struct acpi_dmar);
75 	ret = acpi_add_table(ctx, dmar);
76 	if (ret)
77 		return log_msg_ret("add", ret);
78 
79 	return 0;
80 }
81 
testacpi_get_name(const struct udevice * dev,char * out_name)82 static int testacpi_get_name(const struct udevice *dev, char *out_name)
83 {
84 	struct testacpi_plat *plat = dev_get_plat(dev);
85 
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);
96 }
97 
testacpi_fill_madt(const struct udevice * dev,struct acpi_ctx * ctx)98 static 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 
testacpi_fill_ssdt(const struct udevice * dev,struct acpi_ctx * ctx)113 static 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 
testacpi_inject_dsdt(const struct udevice * dev,struct acpi_ctx * ctx)126 static 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 
139 struct acpi_ops testacpi_ops = {
140 	.get_name	= testacpi_get_name,
141 	.write_tables	= testacpi_write_tables,
142 	.fill_madt	= testacpi_fill_madt,
143 	.fill_ssdt	= testacpi_fill_ssdt,
144 	.inject_dsdt	= testacpi_inject_dsdt,
145 };
146 
147 static const struct udevice_id testacpi_ids[] = {
148 	{ .compatible = "denx,u-boot-acpi-test" },
149 	{ }
150 };
151 
152 U_BOOT_DRIVER(testacpi_drv) = {
153 	.name	= "testacpi_drv",
154 	.of_match	= testacpi_ids,
155 	.id	= UCLASS_TEST_ACPI,
156 	.bind	= dm_scan_fdt_dev,
157 	.plat_auto	= sizeof(struct testacpi_plat),
158 	ACPI_OPS_PTR(&testacpi_ops)
159 };
160 
161 UCLASS_DRIVER(testacpi) = {
162 	.name		= "testacpi",
163 	.id		= UCLASS_TEST_ACPI,
164 };
165 
166 /* Test ACPI get_name() */
dm_test_acpi_get_name(struct unit_test_state * uts)167 static int dm_test_acpi_get_name(struct unit_test_state *uts)
168 {
169 	char name[ACPI_NAME_MAX];
170 	struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound;
171 	struct udevice *pci, *root;
172 
173 	/* Test getting the name from the driver */
174 	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 
178 	/* 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() */
185 	ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c));
186 	ut_assertok(acpi_get_name(i2c, name));
187 	ut_asserteq_str("I2C0", name);
188 
189 	ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi));
190 	ut_assertok(acpi_get_name(spi, name));
191 	ut_asserteq_str("SPI0", name);
192 
193 	/* ACPI doesn't know about the timer */
194 	ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer));
195 	ut_asserteq(-ENOENT, acpi_get_name(timer, name));
196 
197 	/* May as well test the rest of the cases */
198 	ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound));
199 	ut_assertok(acpi_get_name(sound, name));
200 	ut_asserteq_str("HDAS", name);
201 
202 	ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci));
203 	ut_assertok(acpi_get_name(pci, name));
204 	ut_asserteq_str("PCI0", name);
205 
206 	ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root));
207 	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 
212 	return 0;
213 }
214 DM_TEST(dm_test_acpi_get_name, UTF_SCAN_PDATA | UTF_SCAN_FDT);
215 
216 /* Test acpi_get_table_revision() */
dm_test_acpi_get_table_revision(struct unit_test_state * uts)217 static 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 }
226 DM_TEST(dm_test_acpi_get_table_revision, UTF_SCAN_PDATA | UTF_SCAN_FDT);
227 
228 /* Test acpi_create_dmar() */
dm_test_acpi_create_dmar(struct unit_test_state * uts)229 static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
230 {
231 	struct acpi_dmar dmar;
232 	struct udevice *cpu;
233 
234 	ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu));
235 	ut_assertnonnull(cpu);
236 	ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
237 	ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
238 	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
239 		    dmar.host_address_width);
240 
241 	return 0;
242 }
243 DM_TEST(dm_test_acpi_create_dmar, UTF_SCAN_PDATA | UTF_SCAN_FDT);
244 
245 /* Test acpi_fill_header() */
dm_test_acpi_fill_header(struct unit_test_state * uts)246 static 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;
254 	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));
263 	ut_asserteq(OEM_REVISION, hdr.oem_revision);
264 	ut_asserteq_mem(ASLC_ID, hdr.creator_id, sizeof(hdr.creator_id));
265 	ut_asserteq(ASL_REVISION, hdr.creator_revision);
266 
267 	return 0;
268 }
269 DM_TEST(dm_test_acpi_fill_header, UTF_SCAN_PDATA | UTF_SCAN_FDT);
270 
271 /* Test ACPI write_tables() */
dm_test_acpi_write_tables(struct unit_test_state * uts)272 static int dm_test_acpi_write_tables(struct unit_test_state *uts)
273 {
274 	struct acpi_dmar *dmar;
275 	struct acpi_ctx ctx;
276 	ulong addr;
277 	void *buf;
278 	int i;
279 
280 	buf = malloc(BUF_SIZE);
281 	ut_assertnonnull(buf);
282 	addr = map_to_sysmem(buf);
283 
284 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
285 	dmar = ctx.current;
286 	ut_assertok(acpi_write_dev_tables(&ctx));
287 
288 	/*
289 	 * We should have three dmar tables, one for each
290 	 * "denx,u-boot-acpi-test" device
291 	 */
292 	ut_asserteq_ptr(dmar + 3, ctx.current);
293 	ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
294 	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
295 		    dmar->host_address_width);
296 
297 	ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
298 	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
299 		    dmar[1].host_address_width);
300 
301 	ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
302 	ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1,
303 		    dmar[2].host_address_width);
304 
305 	/* Check that the pointers were added correctly */
306 	for (i = 0; i < 3; i++) {
307 		ut_asserteq(nomap_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
308 		ut_asserteq(nomap_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
309 	}
310 	ut_asserteq(0, ctx.rsdt->entry[3]);
311 	ut_asserteq(0, ctx.xsdt->entry[3]);
312 	unmap_sysmem(buf);
313 	free(buf);
314 
315 	return 0;
316 }
317 DM_TEST(dm_test_acpi_write_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
318 
319 /* Test basic ACPI functions */
dm_test_acpi_basic(struct unit_test_state * uts)320 static int dm_test_acpi_basic(struct unit_test_state *uts)
321 {
322 	struct acpi_ctx ctx;
323 
324 	/* Check align works */
325 	ctx.current = (void *)5;
326 	acpi_align(&ctx);
327 	ut_asserteq_ptr((void *)16, ctx.current);
328 
329 	/* Check that align does nothing if already aligned */
330 	acpi_align(&ctx);
331 	ut_asserteq_ptr((void *)16, ctx.current);
332 	acpi_align64(&ctx);
333 	ut_asserteq_ptr((void *)64, ctx.current);
334 	acpi_align64(&ctx);
335 	ut_asserteq_ptr((void *)64, ctx.current);
336 
337 	/* Check incrementing */
338 	acpi_inc(&ctx, 3);
339 	ut_asserteq_ptr((void *)67, ctx.current);
340 	acpi_inc_align(&ctx, 3);
341 	ut_asserteq_ptr((void *)80, ctx.current);
342 
343 	return 0;
344 }
345 DM_TEST(dm_test_acpi_basic, UTF_SCAN_PDATA | UTF_SCAN_FDT);
346 
347 /* Test setup_ctx_and_base_tables */
dm_test_acpi_ctx_and_base_tables(struct unit_test_state * uts)348 static int dm_test_acpi_ctx_and_base_tables(struct unit_test_state *uts)
349 {
350 	struct acpi_rsdp *rsdp;
351 	struct acpi_rsdt *rsdt;
352 	struct acpi_xsdt *xsdt;
353 	struct acpi_ctx ctx;
354 	void *buf, *end;
355 	ulong addr;
356 
357 	/*
358 	 * Use an unaligned address deliberately, by allocating an aligned
359 	 * address and then adding 4 to it
360 	 */
361 	buf = memalign(64, BUF_SIZE);
362 	ut_assertnonnull(buf);
363 	addr = map_to_sysmem(buf);
364 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4));
365 	ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start());
366 
367 	rsdp = buf + 16;
368 	ut_asserteq_ptr(rsdp, ctx.rsdp);
369 	ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
370 	ut_asserteq(sizeof(*rsdp), rsdp->length);
371 	ut_assertok(table_compute_checksum(rsdp, 20));
372 	ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
373 
374 	rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
375 	ut_asserteq_ptr(rsdt, ctx.rsdt);
376 	ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
377 	ut_asserteq(sizeof(*rsdt), rsdt->header.length);
378 	ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
379 
380 	xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
381 	ut_asserteq_ptr(xsdt, ctx.xsdt);
382 	ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
383 	ut_asserteq(sizeof(*xsdt), xsdt->header.length);
384 	ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
385 
386 	end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
387 	ut_asserteq_ptr(end, ctx.current);
388 
389 	ut_asserteq(nomap_to_sysmem(rsdt), rsdp->rsdt_address);
390 	ut_asserteq(nomap_to_sysmem(xsdt), rsdp->xsdt_address);
391 	unmap_sysmem(buf);
392 	free(buf);
393 
394 	return 0;
395 }
396 DM_TEST(dm_test_acpi_ctx_and_base_tables, UTF_SCAN_PDATA | UTF_SCAN_FDT);
397 
398 /* Test 'acpi list' command */
dm_test_acpi_cmd_list(struct unit_test_state * uts)399 static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
400 {
401 	struct acpi_ctx ctx;
402 	ulong addr;
403 	void *buf;
404 
405 	buf = memalign(16, BUF_SIZE);
406 	ut_assertnonnull(buf);
407 	addr = map_to_sysmem(buf);
408 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
409 
410 	ut_assertok(acpi_write_dev_tables(&ctx));
411 
412 	run_command("acpi list", 0);
413 	ut_assert_nextline("Name              Base   Size  Detail");
414 	ut_assert_nextline("----  ----------------  -----  ----------------------------");
415 	ut_assert_nextline("RSDP  %16lx  %5zx  v02 U-BOOT", addr,
416 			   sizeof(struct acpi_rsdp));
417 	addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
418 	ut_assert_nextline("RSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
419 			   addr, sizeof(struct acpi_table_header) +
420 			   3 * sizeof(u32), OEM_REVISION);
421 	addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
422 	ut_assert_nextline("XSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
423 			   addr, sizeof(struct acpi_table_header) +
424 			   3 * sizeof(u64), OEM_REVISION);
425 	addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
426 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
427 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
428 	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
429 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
430 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
431 	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
432 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0",
433 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
434 	ut_assert_console_end();
435 	unmap_sysmem(buf);
436 	free(buf);
437 
438 	return 0;
439 }
440 DM_TEST(dm_test_acpi_cmd_list, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
441 
442 /* Test 'acpi list -c' command */
dm_test_acpi_cmd_list_chksum(struct unit_test_state * uts)443 static int dm_test_acpi_cmd_list_chksum(struct unit_test_state *uts)
444 {
445 	struct acpi_ctx ctx;
446 	ulong addr;
447 	void *buf;
448 
449 	buf = memalign(16, BUF_SIZE);
450 	ut_assertnonnull(buf);
451 	addr = map_to_sysmem(buf);
452 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
453 
454 	ut_assertok(acpi_write_dev_tables(&ctx));
455 
456 	run_command("acpi list -c", 0);
457 	ut_assert_nextline("Name              Base   Size  Detail");
458 	ut_assert_nextline("----  ----------------  -----  ----------------------------");
459 	ut_assert_nextline("RSDP  %16lx  %5zx  v02 U-BOOT  OK  OK", addr,
460 			   sizeof(struct acpi_rsdp));
461 	addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
462 	ut_assert_nextline("RSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
463 			   addr, sizeof(struct acpi_table_header) +
464 			   3 * sizeof(u32), OEM_REVISION);
465 	addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
466 	ut_assert_nextline("XSDT  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
467 			   addr, sizeof(struct acpi_table_header) +
468 			   3 * sizeof(u64), OEM_REVISION);
469 	addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
470 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
471 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
472 	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
473 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
474 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
475 	addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
476 	ut_assert_nextline("DMAR  %16lx  %5zx  v01 U-BOOT U-BOOTBL %x INTL 0  OK",
477 			   addr, sizeof(struct acpi_dmar), OEM_REVISION);
478 	ut_assert_console_end();
479 	ut_assert_console_end();
480 	unmap_sysmem(buf);
481 	free(buf);
482 
483 	return 0;
484 }
485 DM_TEST(dm_test_acpi_cmd_list_chksum,
486 	UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
487 
488 /* Test 'acpi dump' command */
dm_test_acpi_cmd_dump(struct unit_test_state * uts)489 static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
490 {
491 	struct acpi_ctx ctx;
492 	ulong addr;
493 	void *buf;
494 
495 	buf = memalign(16, BUF_SIZE);
496 	ut_assertnonnull(buf);
497 	addr = map_to_sysmem(buf);
498 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
499 
500 	ut_assertok(acpi_write_dev_tables(&ctx));
501 
502 	/* First search for a non-existent table */
503 	run_command("acpi dump rdst", 0);
504 	ut_assert_nextline("Table 'RDST' not found");
505 	ut_assert_console_end();
506 
507 	/* Now a real table */
508 	run_command("acpi dump dmar", 0);
509 	addr = ALIGN(nomap_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
510 	ut_assert_nextline("DMAR @ %16lx", addr);
511 	ut_assert_nextlines_are_dump(0x30);
512 	ut_assert_console_end();
513 	unmap_sysmem(buf);
514 	free(buf);
515 
516 	return 0;
517 }
518 DM_TEST(dm_test_acpi_cmd_dump, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
519 
520 /* Test acpi_device_path() */
dm_test_acpi_device_path(struct unit_test_state * uts)521 static int dm_test_acpi_device_path(struct unit_test_state *uts)
522 {
523 	struct testacpi_plat *plat;
524 	char buf[ACPI_PATH_MAX];
525 	struct udevice *dev, *child;
526 
527 	ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
528 	ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
529 	ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
530 
531 	/* Test running out of space */
532 	buf[5] = '\0';
533 	ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
534 	ut_asserteq('\0', buf[5]);
535 
536 	/* Test a three-component name */
537 	ut_assertok(device_first_child_err(dev, &child));
538 	ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
539 	ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
540 			buf);
541 
542 	/* Test handling of a device which doesn't produce a name */
543 	plat = dev_get_plat(dev);
544 	plat->no_name = true;
545 	ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
546 	ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
547 
548 	/* Test handling of a device which returns an error */
549 	plat = dev_get_plat(dev);
550 	plat->return_error = true;
551 	ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
552 
553 	return 0;
554 }
555 DM_TEST(dm_test_acpi_device_path, UTF_SCAN_PDATA | UTF_SCAN_FDT);
556 
557 /* Test acpi_device_status() */
dm_test_acpi_device_status(struct unit_test_state * uts)558 static int dm_test_acpi_device_status(struct unit_test_state *uts)
559 {
560 	struct udevice *dev;
561 
562 	ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
563 	ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
564 
565 	return 0;
566 }
567 DM_TEST(dm_test_acpi_device_status, UTF_SCAN_PDATA | UTF_SCAN_FDT);
568 
569 /* Test acpi_fill_ssdt() */
dm_test_acpi_fill_ssdt(struct unit_test_state * uts)570 static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
571 {
572 	struct acpi_ctx ctx;
573 	u8 *buf;
574 
575 	buf = malloc(BUF_SIZE);
576 	ut_assertnonnull(buf);
577 
578 	acpi_reset_items();
579 	ctx.current = buf;
580 	buf[4] = 'z';	/* sentinel */
581 	ut_assertok(acpi_fill_ssdt(&ctx));
582 
583 	/*
584 	 * These values come from acpi-test2's acpi-ssdt-test-data property.
585 	 * This device comes first because of u-boot,acpi-ssdt-order
586 	 */
587 	ut_asserteq('c', buf[0]);
588 	ut_asserteq('d', buf[1]);
589 
590 	/* These values come from acpi-test's acpi-ssdt-test-data property */
591 	ut_asserteq('a', buf[2]);
592 	ut_asserteq('b', buf[3]);
593 
594 	ut_asserteq('z', buf[4]);
595 
596 	return 0;
597 }
598 DM_TEST(dm_test_acpi_fill_ssdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
599 
600 /* Test acpi_fill_madt() */
dm_test_acpi_fill_madt(struct unit_test_state * uts)601 static int dm_test_acpi_fill_madt(struct unit_test_state *uts)
602 {
603 	struct acpi_ctx ctx;
604 	u64 *buf;
605 
606 	buf = malloc(BUF_SIZE);
607 	ut_assertnonnull(buf);
608 
609 	acpi_reset_items();
610 	ctx.current = buf;
611 	buf[1] = 'z';	/* sentinel */
612 	ut_assertok(acpi_fill_madt_subtbl(&ctx));
613 
614 	/*
615 	 * These values come from acpi-test2's acpi-ssdt-test-data property.
616 	 * This device comes first because of u-boot,acpi-ssdt-order
617 	 */
618 	ut_asserteq(0xdeadbeef, buf[0]);
619 
620 	ut_asserteq('z', buf[1]);
621 
622 	return 0;
623 }
624 
625 DM_TEST(dm_test_acpi_fill_madt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
626 
627 /* Test acpi_inject_dsdt() */
dm_test_acpi_inject_dsdt(struct unit_test_state * uts)628 static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
629 {
630 	struct acpi_ctx ctx;
631 	u8 *buf;
632 
633 	buf = malloc(BUF_SIZE);
634 	ut_assertnonnull(buf);
635 
636 	acpi_reset_items();
637 	ctx.current = buf;
638 	buf[4] = 'z';	/* sentinel */
639 	ut_assertok(acpi_inject_dsdt(&ctx));
640 
641 	/*
642 	 * These values come from acpi-test's acpi-dsdt-test-data property.
643 	 * There is no u-boot,acpi-dsdt-order so device-tree order is used.
644 	 */
645 	ut_asserteq('h', buf[0]);
646 	ut_asserteq('i', buf[1]);
647 
648 	/* These values come from acpi-test's acpi-dsdt-test-data property */
649 	ut_asserteq('j', buf[2]);
650 	ut_asserteq('k', buf[3]);
651 
652 	ut_asserteq('z', buf[4]);
653 
654 	return 0;
655 }
656 DM_TEST(dm_test_acpi_inject_dsdt, UTF_SCAN_PDATA | UTF_SCAN_FDT);
657 
658 /* Test 'acpi items' command */
dm_test_acpi_cmd_items(struct unit_test_state * uts)659 static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
660 {
661 	struct acpi_ctx ctx;
662 	ulong addr;
663 	void *buf;
664 
665 	buf = malloc(BUF_SIZE);
666 	ut_assertnonnull(buf);
667 	addr = map_to_sysmem(buf);
668 
669 	acpi_reset_items();
670 	ctx.current = buf;
671 	ut_assertok(acpi_fill_ssdt(&ctx));
672 	run_command("acpi items", 0);
673 	ut_assert_nextline("Seq  Type       Base   Size  Device/Writer");
674 	ut_assert_nextline("---  -----  --------   ----  -------------");
675 	ut_assert_nextline("  0  ssdt   %8lx      2  acpi-test", addr);
676 	ut_assert_nextline("  1  ssdt   %8lx      2  acpi-test2", addr + 2);
677 	ut_assert_console_end();
678 
679 	acpi_reset_items();
680 	ctx.current = buf;
681 	ut_assertok(acpi_inject_dsdt(&ctx));
682 	run_command("acpi items", 0);
683 	ut_assert_nextlinen("Seq");
684 	ut_assert_nextlinen("---");
685 	ut_assert_nextline("  0  dsdt   %8lx      2  acpi-test", addr);
686 	ut_assert_nextline("  1  dsdt   %8lx      2  acpi-test2", addr + 2);
687 	ut_assert_console_end();
688 
689 	run_command("acpi items -d", 0);
690 	ut_assert_nextlinen("Seq");
691 	ut_assert_nextlinen("---");
692 	ut_assert_nextline("  0  dsdt   %8lx      2  acpi-test", addr);
693 	ut_assert_nextlines_are_dump(2);
694 	ut_assert_nextline("%s", "");
695 	ut_assert_nextline("  1  dsdt   %8lx      2  acpi-test2", addr + 2);
696 	ut_assert_nextlines_are_dump(2);
697 	ut_assert_nextline("%s", "");
698 	ut_assert_console_end();
699 	unmap_sysmem(buf);
700 	free(buf);
701 
702 	return 0;
703 }
704 DM_TEST(dm_test_acpi_cmd_items, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
705 
706 /* Test 'acpi set' command */
dm_test_acpi_cmd_set(struct unit_test_state * uts)707 static int dm_test_acpi_cmd_set(struct unit_test_state *uts)
708 {
709 	struct acpi_ctx ctx;
710 	ulong addr;
711 	void *buf;
712 
713 	gd_set_acpi_start(0);
714 
715 	ut_asserteq(0, gd_acpi_start());
716 	ut_assertok(run_command("acpi set", 0));
717 	ut_assert_nextline("ACPI pointer: 0");
718 
719 	buf = memalign(16, BUF_SIZE);
720 	ut_assertnonnull(buf);
721 	addr = map_to_sysmem(buf);
722 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
723 
724 	ut_assertok(acpi_write_dev_tables(&ctx));
725 
726 	ut_assertok(run_command("acpi set", 0));
727 	ut_assert_nextline("ACPI pointer: %lx", addr);
728 
729 	ut_assertok(run_command("acpi set 0", 0));
730 	ut_assert_nextline("Setting ACPI pointer to 0");
731 	ut_asserteq(0, gd_acpi_start());
732 
733 	ut_assertok(run_commandf("acpi set %lx", addr));
734 	ut_assert_nextline("Setting ACPI pointer to %lx", addr);
735 	ut_asserteq(addr, gd_acpi_start());
736 
737 	ut_assert_console_end();
738 	unmap_sysmem(buf);
739 	free(buf);
740 
741 	return 0;
742 }
743 DM_TEST(dm_test_acpi_cmd_set, UTF_SCAN_PDATA | UTF_SCAN_FDT | UTF_CONSOLE);
744 
745 /**
746  * dm_test_write_test_table() - create test ACPI table
747  *
748  * Create an ACPI table TSTn, where n is given by @index.
749  *
750  * @ctx:	ACPI table writing context
751  * @index:	table index
752  * Return:	generated table
753  */
754 static struct acpi_table_header
dm_test_write_test_table(struct acpi_ctx * ctx,int index)755 *dm_test_write_test_table(struct acpi_ctx *ctx, int index)
756 {
757 	struct acpi_table_header *tbl = ctx->current;
758 	char signature[5];
759 
760 	snprintf(signature, sizeof(signature), "TST%1d", index);
761 	memset(tbl, 0, sizeof(*tbl));
762 	acpi_fill_header(tbl, signature);
763 	acpi_inc(ctx, sizeof(struct acpi_table_header));
764 	tbl->length = (u8 *)ctx->current - (u8 *)tbl;
765 	tbl->checksum = table_compute_checksum(tbl, tbl->length);
766 	acpi_add_table(ctx, tbl);
767 
768 	return tbl;
769 }
770 
771 /* Test acpi_find_table() */
dm_test_acpi_find_table(struct unit_test_state * uts)772 static int dm_test_acpi_find_table(struct unit_test_state *uts)
773 {
774 	struct acpi_ctx ctx;
775 	ulong acpi_start, addr;
776 	void *buf;
777 	struct acpi_table_header *table, *table1, *table2, *table3;
778 	struct acpi_rsdp *rsdp;
779 	ulong rsdt;
780 	ulong xsdt;
781 
782 	/* Keep reference to original ACPI tables */
783 	acpi_start = gd_acpi_start();
784 
785 	/* Setup new ACPI tables */
786 	buf = memalign(16, BUF_SIZE);
787 	ut_assertnonnull(buf);
788 	addr = map_to_sysmem(buf);
789 	ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr));
790 	table3 = dm_test_write_test_table(&ctx, 3);
791 	table1 = dm_test_write_test_table(&ctx, 1);
792 	table2 = dm_test_write_test_table(&ctx, 2);
793 
794 	/* Retrieve RSDP, RSDT, XSDT */
795 	rsdp = map_sysmem(gd_acpi_start(), 0);
796 	ut_assertnonnull(rsdp);
797 	rsdt = rsdp->rsdt_address;
798 	ut_assert(rsdt);
799 	xsdt = rsdp->xsdt_address;
800 	ut_assert(xsdt);
801 
802 	/* Find with both RSDT and XSDT */
803 	table = acpi_find_table("TST1");
804 	ut_asserteq_ptr(table1, table);
805 	ut_asserteq_strn("TST1", table->signature);
806 	table = acpi_find_table("TST2");
807 	ut_asserteq_ptr(table2, table);
808 	ut_asserteq_strn("TST2", table->signature);
809 	table = acpi_find_table("TST3");
810 	ut_asserteq_ptr(table3, table);
811 	ut_asserteq_strn("TST3", table->signature);
812 
813 	/* Find with XSDT only */
814 	rsdp->rsdt_address = 0;
815 	table = acpi_find_table("TST1");
816 	ut_asserteq_ptr(table1, table);
817 	table = acpi_find_table("TST2");
818 	ut_asserteq_ptr(table2, table);
819 	table = acpi_find_table("TST3");
820 	ut_asserteq_ptr(table3, table);
821 	rsdp->rsdt_address = rsdt;
822 
823 	/* Find with RSDT only */
824 	rsdp->xsdt_address = 0;
825 	table = acpi_find_table("TST1");
826 	ut_asserteq_ptr(table1, table);
827 	table = acpi_find_table("TST2");
828 	ut_asserteq_ptr(table2, table);
829 	table = acpi_find_table("TST3");
830 	ut_asserteq_ptr(table3, table);
831 	rsdp->xsdt_address = xsdt;
832 
833 	/* Restore previous ACPI tables */
834 	gd_set_acpi_start(acpi_start);
835 	unmap_sysmem(buf);
836 	free(buf);
837 
838 	return 0;
839 }
840 DM_TEST(dm_test_acpi_find_table, 0);
841 
842 /* Test offsets in RSDT, XSDT */
dm_test_acpi_offsets(struct unit_test_state * uts)843 static int dm_test_acpi_offsets(struct unit_test_state *uts)
844 {
845 	ut_asserteq(36, offsetof(struct acpi_rsdt, entry));
846 	ut_asserteq(36, offsetof(struct acpi_xsdt, entry));
847 
848 	return 0;
849 }
850 DM_TEST(dm_test_acpi_offsets, 0);
851