Merge https://gitlab.denx.de/u-boot/custodians/u-boot-x86
[platform/kernel/u-boot.git] / test / dm / acpi.c
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 <common.h>
10 #include <console.h>
11 #include <dm.h>
12 #include <malloc.h>
13 #include <mapmem.h>
14 #include <version.h>
15 #include <tables_csum.h>
16 #include <version.h>
17 #include <acpi/acpigen.h>
18 #include <acpi/acpi_device.h>
19 #include <acpi/acpi_table.h>
20 #include <dm/acpi.h>
21 #include <dm/test.h>
22 #include <test/ut.h>
23 #include "acpi.h"
24
25 #define BUF_SIZE                4096
26
27 /**
28  * struct testacpi_platdata - Platform data for the test ACPI device
29  *
30  * @no_name: true to emit an empty ACPI name from testacpi_get_name()
31  * @return_error: true to return an error instead of a name
32  */
33 struct testacpi_platdata {
34         bool return_error;
35         bool no_name;
36 };
37
38 static int testacpi_write_tables(const struct udevice *dev,
39                                  struct acpi_ctx *ctx)
40 {
41         struct acpi_dmar *dmar;
42         int ret;
43
44         dmar = (struct acpi_dmar *)ctx->current;
45         acpi_create_dmar(dmar, DMAR_INTR_REMAP);
46         ctx->current += sizeof(struct acpi_dmar);
47         ret = acpi_add_table(ctx, dmar);
48         if (ret)
49                 return log_msg_ret("add", ret);
50
51         return 0;
52 }
53
54 static int testacpi_get_name(const struct udevice *dev, char *out_name)
55 {
56         struct testacpi_platdata *plat = dev_get_platdata(dev);
57
58         if (plat->return_error)
59                 return -EINVAL;
60         if (plat->no_name) {
61                 *out_name = '\0';
62                 return 0;
63         }
64         if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI)
65                 return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME);
66         else
67                 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
68 }
69
70 static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx)
71 {
72         const char *data;
73
74         data = dev_read_string(dev, "acpi-ssdt-test-data");
75         if (data) {
76                 while (*data)
77                         acpigen_emit_byte(ctx, *data++);
78         }
79
80         return 0;
81 }
82
83 static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx)
84 {
85         const char *data;
86
87         data = dev_read_string(dev, "acpi-dsdt-test-data");
88         if (data) {
89                 while (*data)
90                         acpigen_emit_byte(ctx, *data++);
91         }
92
93         return 0;
94 }
95
96 struct acpi_ops testacpi_ops = {
97         .get_name       = testacpi_get_name,
98         .write_tables   = testacpi_write_tables,
99         .fill_ssdt      = testacpi_fill_ssdt,
100         .inject_dsdt    = testacpi_inject_dsdt,
101 };
102
103 static const struct udevice_id testacpi_ids[] = {
104         { .compatible = "denx,u-boot-acpi-test" },
105         { }
106 };
107
108 U_BOOT_DRIVER(testacpi_drv) = {
109         .name   = "testacpi_drv",
110         .of_match       = testacpi_ids,
111         .id     = UCLASS_TEST_ACPI,
112         .bind   = dm_scan_fdt_dev,
113         .platdata_auto_alloc_size       = sizeof(struct testacpi_platdata),
114         ACPI_OPS_PTR(&testacpi_ops)
115 };
116
117 UCLASS_DRIVER(testacpi) = {
118         .name           = "testacpi",
119         .id             = UCLASS_TEST_ACPI,
120 };
121
122 /* Test ACPI get_name() */
123 static int dm_test_acpi_get_name(struct unit_test_state *uts)
124 {
125         char name[ACPI_NAME_MAX];
126         struct udevice *dev, *dev2, *i2c, *spi, *serial, *timer, *sound;
127         struct udevice *pci, *root;
128
129         /* Test getting the name from the driver */
130         ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
131         ut_assertok(acpi_get_name(dev, name));
132         ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
133
134         /* Test getting the name from the device tree */
135         ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test",
136                                               &dev2));
137         ut_assertok(acpi_get_name(dev2, name));
138         ut_asserteq_str("GHIJ", name);
139
140         /* Test getting the name from acpi_device_get_name() */
141         ut_assertok(uclass_first_device(UCLASS_I2C, &i2c));
142         ut_assertok(acpi_get_name(i2c, name));
143         ut_asserteq_str("I2C0", name);
144
145         ut_assertok(uclass_first_device(UCLASS_SPI, &spi));
146         ut_assertok(acpi_get_name(spi, name));
147         ut_asserteq_str("SPI0", name);
148
149         /* The uart has no sequence number, so this should fail */
150         ut_assertok(uclass_first_device(UCLASS_SERIAL, &serial));
151         ut_asserteq(-ENXIO, acpi_get_name(serial, name));
152
153         /* ACPI doesn't know about the timer */
154         ut_assertok(uclass_first_device(UCLASS_TIMER, &timer));
155         ut_asserteq(-ENOENT, acpi_get_name(timer, name));
156
157         /* May as well test the rest of the cases */
158         ut_assertok(uclass_first_device(UCLASS_SOUND, &sound));
159         ut_assertok(acpi_get_name(sound, name));
160         ut_asserteq_str("HDAS", name);
161
162         ut_assertok(uclass_first_device(UCLASS_PCI, &pci));
163         ut_assertok(acpi_get_name(pci, name));
164         ut_asserteq_str("PCI0", name);
165
166         ut_assertok(uclass_first_device(UCLASS_ROOT, &root));
167         ut_assertok(acpi_get_name(root, name));
168         ut_asserteq_str("\\_SB", name);
169
170         /* Note that we don't have tests for acpi_name_from_id() */
171
172         return 0;
173 }
174 DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
175
176 /* Test acpi_get_table_revision() */
177 static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
178 {
179         ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
180         ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
181         ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
182         ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
183
184         return 0;
185 }
186 DM_TEST(dm_test_acpi_get_table_revision,
187         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
188
189 /* Test acpi_create_dmar() */
190 static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
191 {
192         struct acpi_dmar dmar;
193         struct udevice *cpu;
194
195         ut_assertok(uclass_first_device(UCLASS_CPU, &cpu));
196         ut_assertnonnull(cpu);
197         ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
198         ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
199         ut_asserteq(32 - 1, dmar.host_address_width);
200
201         return 0;
202 }
203 DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
204
205 /* Test acpi_fill_header() */
206 static int dm_test_acpi_fill_header(struct unit_test_state *uts)
207 {
208         struct acpi_table_header hdr;
209
210         /* Make sure these 5 fields are not changed */
211         hdr.length = 0x11;
212         hdr.revision = 0x22;
213         hdr.checksum = 0x33;
214         hdr.aslc_revision = 0x44;
215         acpi_fill_header(&hdr, "ABCD");
216
217         ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
218         ut_asserteq(0x11, hdr.length);
219         ut_asserteq(0x22, hdr.revision);
220         ut_asserteq(0x33, hdr.checksum);
221         ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
222         ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
223                         sizeof(hdr.oem_table_id));
224         ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
225         ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
226         ut_asserteq(0x44, hdr.aslc_revision);
227
228         return 0;
229 }
230 DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
231
232 /* Test ACPI write_tables() */
233 static int dm_test_acpi_write_tables(struct unit_test_state *uts)
234 {
235         struct acpi_dmar *dmar;
236         struct acpi_ctx ctx;
237         void *buf;
238         int i;
239
240         buf = malloc(BUF_SIZE);
241         ut_assertnonnull(buf);
242
243         acpi_setup_base_tables(&ctx, buf);
244         dmar = ctx.current;
245         ut_assertok(acpi_write_dev_tables(&ctx));
246
247         /*
248          * We should have three dmar tables, one for each
249          * "denx,u-boot-acpi-test" device
250          */
251         ut_asserteq_ptr(dmar + 3, ctx.current);
252         ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
253         ut_asserteq(32 - 1, dmar->host_address_width);
254
255         ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
256         ut_asserteq(32 - 1, dmar[1].host_address_width);
257
258         ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags);
259         ut_asserteq(32 - 1, dmar[2].host_address_width);
260
261         /* Check that the pointers were added correctly */
262         for (i = 0; i < 3; i++) {
263                 ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]);
264                 ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]);
265         }
266         ut_asserteq(0, ctx.rsdt->entry[3]);
267         ut_asserteq(0, ctx.xsdt->entry[3]);
268
269         return 0;
270 }
271 DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
272
273 /* Test basic ACPI functions */
274 static int dm_test_acpi_basic(struct unit_test_state *uts)
275 {
276         struct acpi_ctx ctx;
277
278         /* Check align works */
279         ctx.current = (void *)5;
280         acpi_align(&ctx);
281         ut_asserteq_ptr((void *)16, ctx.current);
282
283         /* Check that align does nothing if already aligned */
284         acpi_align(&ctx);
285         ut_asserteq_ptr((void *)16, ctx.current);
286         acpi_align64(&ctx);
287         ut_asserteq_ptr((void *)64, ctx.current);
288         acpi_align64(&ctx);
289         ut_asserteq_ptr((void *)64, ctx.current);
290
291         /* Check incrementing */
292         acpi_inc(&ctx, 3);
293         ut_asserteq_ptr((void *)67, ctx.current);
294         acpi_inc_align(&ctx, 3);
295         ut_asserteq_ptr((void *)80, ctx.current);
296
297         return 0;
298 }
299 DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
300
301 /* Test acpi_setup_base_tables */
302 static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
303 {
304         struct acpi_rsdp *rsdp;
305         struct acpi_rsdt *rsdt;
306         struct acpi_xsdt *xsdt;
307         struct acpi_ctx ctx;
308         void *buf, *end;
309
310         /*
311          * Use an unaligned address deliberately, by allocating an aligned
312          * address and then adding 4 to it
313          */
314         buf = memalign(64, BUF_SIZE);
315         ut_assertnonnull(buf);
316         acpi_setup_base_tables(&ctx, buf + 4);
317         ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start);
318
319         rsdp = buf + 16;
320         ut_asserteq_ptr(rsdp, ctx.rsdp);
321         ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature));
322         ut_asserteq(sizeof(*rsdp), rsdp->length);
323         ut_assertok(table_compute_checksum(rsdp, 20));
324         ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
325
326         rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
327         ut_asserteq_ptr(rsdt, ctx.rsdt);
328         ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN);
329         ut_asserteq(sizeof(*rsdt), rsdt->header.length);
330         ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
331
332         xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
333         ut_asserteq_ptr(xsdt, ctx.xsdt);
334         ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN);
335         ut_asserteq(sizeof(*xsdt), xsdt->header.length);
336         ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
337
338         end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
339         ut_asserteq_ptr(end, ctx.current);
340
341         ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
342         ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
343
344         return 0;
345 }
346 DM_TEST(dm_test_acpi_setup_base_tables,
347         UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
348
349 /* Test 'acpi list' command */
350 static int dm_test_acpi_cmd_list(struct unit_test_state *uts)
351 {
352         struct acpi_ctx ctx;
353         ulong addr;
354         void *buf;
355
356         buf = memalign(16, BUF_SIZE);
357         ut_assertnonnull(buf);
358         acpi_setup_base_tables(&ctx, buf);
359
360         ut_assertok(acpi_write_dev_tables(&ctx));
361
362         console_record_reset();
363         run_command("acpi list", 0);
364         addr = (ulong)map_to_sysmem(buf);
365         ut_assert_nextline("ACPI tables start at %lx", addr);
366         ut_assert_nextline("RSDP %08lx %06lx (v02 U-BOOT)", addr,
367                            sizeof(struct acpi_rsdp));
368         addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16);
369         ut_assert_nextline("RSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
370                            addr, sizeof(struct acpi_table_header) +
371                            3 * sizeof(u32), U_BOOT_BUILD_DATE);
372         addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16);
373         ut_assert_nextline("XSDT %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
374                            addr, sizeof(struct acpi_table_header) +
375                            3 * sizeof(u64), U_BOOT_BUILD_DATE);
376         addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64);
377         ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
378                            addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
379         addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
380         ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
381                            addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
382         addr = ALIGN(addr + sizeof(struct acpi_dmar), 16);
383         ut_assert_nextline("DMAR %08lx %06lx (v01 U-BOOT U-BOOTBL %x INTL 0)",
384                            addr, sizeof(struct acpi_dmar), U_BOOT_BUILD_DATE);
385         ut_assert_console_end();
386
387         return 0;
388 }
389 DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
390
391 /* Test 'acpi dump' command */
392 static int dm_test_acpi_cmd_dump(struct unit_test_state *uts)
393 {
394         struct acpi_ctx ctx;
395         ulong addr;
396         void *buf;
397
398         buf = memalign(16, BUF_SIZE);
399         ut_assertnonnull(buf);
400         acpi_setup_base_tables(&ctx, buf);
401
402         ut_assertok(acpi_write_dev_tables(&ctx));
403
404         /* First search for a non-existent table */
405         console_record_reset();
406         run_command("acpi dump rdst", 0);
407         ut_assert_nextline("Table 'RDST' not found");
408         ut_assert_console_end();
409
410         /* Now a real table */
411         console_record_reset();
412         run_command("acpi dump dmar", 0);
413         addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64);
414         ut_assert_nextline("DMAR @ %08lx", addr);
415         ut_assert_nextlines_are_dump(0x30);
416         ut_assert_console_end();
417
418         return 0;
419 }
420 DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
421
422 /* Test acpi_device_path() */
423 static int dm_test_acpi_device_path(struct unit_test_state *uts)
424 {
425         struct testacpi_platdata *plat;
426         char buf[ACPI_PATH_MAX];
427         struct udevice *dev, *child;
428
429         ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
430         ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
431         ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf);
432
433         /* Test running out of space */
434         buf[5] = '\0';
435         ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5));
436         ut_asserteq('\0', buf[5]);
437
438         /* Test a three-component name */
439         ut_assertok(device_first_child_err(dev, &child));
440         ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
441         ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME,
442                         buf);
443
444         /* Test handling of a device which doesn't produce a name */
445         plat = dev_get_platdata(dev);
446         plat->no_name = true;
447         ut_assertok(acpi_device_path(child, buf, sizeof(buf)));
448         ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf);
449
450         /* Test handling of a device which returns an error */
451         plat = dev_get_platdata(dev);
452         plat->return_error = true;
453         ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf)));
454
455         return 0;
456 }
457 DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
458
459 /* Test acpi_device_status() */
460 static int dm_test_acpi_device_status(struct unit_test_state *uts)
461 {
462         struct udevice *dev;
463
464         ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
465         ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev));
466
467         return 0;
468 }
469 DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
470
471 /* Test acpi_fill_ssdt() */
472 static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts)
473 {
474         struct acpi_ctx ctx;
475         u8 *buf;
476
477         buf = malloc(BUF_SIZE);
478         ut_assertnonnull(buf);
479
480         acpi_reset_items();
481         ctx.current = buf;
482         buf[4] = 'z';   /* sentinel */
483         ut_assertok(acpi_fill_ssdt(&ctx));
484
485         /*
486          * These values come from acpi-test2's acpi-ssdt-test-data property.
487          * This device comes first because of u-boot,acpi-ssdt-order
488          */
489         ut_asserteq('c', buf[0]);
490         ut_asserteq('d', buf[1]);
491
492         /* These values come from acpi-test's acpi-ssdt-test-data property */
493         ut_asserteq('a', buf[2]);
494         ut_asserteq('b', buf[3]);
495
496         ut_asserteq('z', buf[4]);
497
498         return 0;
499 }
500 DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
501
502 /* Test acpi_inject_dsdt() */
503 static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts)
504 {
505         struct acpi_ctx ctx;
506         u8 *buf;
507
508         buf = malloc(BUF_SIZE);
509         ut_assertnonnull(buf);
510
511         acpi_reset_items();
512         ctx.current = buf;
513         buf[4] = 'z';   /* sentinel */
514         ut_assertok(acpi_inject_dsdt(&ctx));
515
516         /*
517          * These values come from acpi-test's acpi-dsdt-test-data property.
518          * There is no u-boot,acpi-dsdt-order so device-tree order is used.
519          */
520         ut_asserteq('h', buf[0]);
521         ut_asserteq('i', buf[1]);
522
523         /* These values come from acpi-test's acpi-dsdt-test-data property */
524         ut_asserteq('j', buf[2]);
525         ut_asserteq('k', buf[3]);
526
527         ut_asserteq('z', buf[4]);
528
529         return 0;
530 }
531 DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
532
533 /* Test 'acpi items' command */
534 static int dm_test_acpi_cmd_items(struct unit_test_state *uts)
535 {
536         struct acpi_ctx ctx;
537         void *buf;
538
539         buf = malloc(BUF_SIZE);
540         ut_assertnonnull(buf);
541
542         acpi_reset_items();
543         ctx.current = buf;
544         ut_assertok(acpi_fill_ssdt(&ctx));
545         console_record_reset();
546         run_command("acpi items", 0);
547         ut_assert_nextline("dev 'acpi-test', type 1, size 2");
548         ut_assert_nextline("dev 'acpi-test2', type 1, size 2");
549         ut_assert_console_end();
550
551         acpi_reset_items();
552         ctx.current = buf;
553         ut_assertok(acpi_inject_dsdt(&ctx));
554         console_record_reset();
555         run_command("acpi items", 0);
556         ut_assert_nextline("dev 'acpi-test', type 2, size 2");
557         ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
558         ut_assert_console_end();
559
560         console_record_reset();
561         run_command("acpi items -d", 0);
562         ut_assert_nextline("dev 'acpi-test', type 2, size 2");
563         ut_assert_nextlines_are_dump(2);
564         ut_assert_nextline("%s", "");
565         ut_assert_nextline("dev 'acpi-test2', type 2, size 2");
566         ut_assert_nextlines_are_dump(2);
567         ut_assert_nextline("%s", "");
568         ut_assert_console_end();
569
570         return 0;
571 }
572 DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);