acpi: Add a method to write tables for a device
[oweals/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 <dm.h>
11 #include <version.h>
12 #include <acpi/acpi_table.h>
13 #include <dm/acpi.h>
14 #include <dm/test.h>
15 #include <test/ut.h>
16
17 #define ACPI_TEST_DEV_NAME      "ABCD"
18 #define BUF_SIZE                4096
19
20 static int testacpi_write_tables(const struct udevice *dev,
21                                  struct acpi_ctx *ctx)
22 {
23         struct acpi_dmar *dmar;
24
25         dmar = (struct acpi_dmar *)ctx->current;
26         acpi_create_dmar(dmar, DMAR_INTR_REMAP);
27         ctx->current += sizeof(struct acpi_dmar);
28
29         return 0;
30 }
31
32 static int testacpi_get_name(const struct udevice *dev, char *out_name)
33 {
34         return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
35 }
36
37 struct acpi_ops testacpi_ops = {
38         .get_name       = testacpi_get_name,
39         .write_tables   = testacpi_write_tables,
40 };
41
42 static const struct udevice_id testacpi_ids[] = {
43         { .compatible = "denx,u-boot-acpi-test" },
44         { }
45 };
46
47 U_BOOT_DRIVER(testacpi_drv) = {
48         .name   = "testacpi_drv",
49         .of_match       = testacpi_ids,
50         .id     = UCLASS_TEST_ACPI,
51         ACPI_OPS_PTR(&testacpi_ops)
52 };
53
54 UCLASS_DRIVER(testacpi) = {
55         .name           = "testacpi",
56         .id             = UCLASS_TEST_ACPI,
57 };
58
59 /* Test ACPI get_name() */
60 static int dm_test_acpi_get_name(struct unit_test_state *uts)
61 {
62         char name[ACPI_NAME_MAX];
63         struct udevice *dev;
64
65         ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
66         ut_assertok(acpi_get_name(dev, name));
67         ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
68
69         return 0;
70 }
71 DM_TEST(dm_test_acpi_get_name, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
72
73 /* Test acpi_get_table_revision() */
74 static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
75 {
76         ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
77         ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
78         ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
79         ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
80
81         return 0;
82 }
83 DM_TEST(dm_test_acpi_get_table_revision,
84         DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
85
86 /* Test acpi_create_dmar() */
87 static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
88 {
89         struct acpi_dmar dmar;
90
91         ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
92         ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
93         ut_asserteq(32 - 1, dmar.host_address_width);
94
95         return 0;
96 }
97 DM_TEST(dm_test_acpi_create_dmar, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
98
99 /* Test acpi_fill_header() */
100 static int dm_test_acpi_fill_header(struct unit_test_state *uts)
101 {
102         struct acpi_table_header hdr;
103
104         /* Make sure these 5 fields are not changed */
105         hdr.length = 0x11;
106         hdr.revision = 0x22;
107         hdr.checksum = 0x33;
108         hdr.aslc_revision = 0x44;
109         acpi_fill_header(&hdr, "ABCD");
110
111         ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
112         ut_asserteq(0x11, hdr.length);
113         ut_asserteq(0x22, hdr.revision);
114         ut_asserteq(0x33, hdr.checksum);
115         ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
116         ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
117                         sizeof(hdr.oem_table_id));
118         ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
119         ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
120         ut_asserteq(0x44, hdr.aslc_revision);
121
122         return 0;
123 }
124 DM_TEST(dm_test_acpi_fill_header, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
125
126 /* Test ACPI write_tables() */
127 static int dm_test_acpi_write_tables(struct unit_test_state *uts)
128 {
129         struct acpi_dmar *dmar;
130         struct acpi_ctx ctx;
131         void *buf;
132
133         buf = malloc(BUF_SIZE);
134         ut_assertnonnull(buf);
135
136         ctx.current = buf;
137         ut_assertok(acpi_write_dev_tables(&ctx));
138         dmar = buf;
139
140         /*
141          * We should have two dmar tables, one for each "denx,u-boot-acpi-test"
142          * device
143          */
144         ut_asserteq_ptr(dmar + 2, ctx.current);
145         ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
146         ut_asserteq(32 - 1, dmar->host_address_width);
147
148         ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
149         ut_asserteq(32 - 1, dmar[1].host_address_width);
150
151         return 0;
152 }
153 DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);