blob: beb1b6da7373166b78efd775f8f3e4729fada43b [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
9#include <common.h>
10#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 Glass17968c32020-04-26 09:19:46 -060014#include <version.h>
Simon Glassb2672ea2020-04-08 16:57:38 -060015#include <acpi/acpi_table.h>
Simon Glass2d67fdf2020-04-08 16:57:34 -060016#include <dm/acpi.h>
17#include <dm/test.h>
18#include <test/ut.h>
19
20#define ACPI_TEST_DEV_NAME "ABCD"
Simon Glass17968c32020-04-26 09:19:46 -060021#define BUF_SIZE 4096
22
23static int testacpi_write_tables(const struct udevice *dev,
24 struct acpi_ctx *ctx)
25{
26 struct acpi_dmar *dmar;
Simon Glass575a5472020-04-26 09:19:50 -060027 int ret;
Simon Glass17968c32020-04-26 09:19:46 -060028
29 dmar = (struct acpi_dmar *)ctx->current;
30 acpi_create_dmar(dmar, DMAR_INTR_REMAP);
31 ctx->current += sizeof(struct acpi_dmar);
Simon Glass575a5472020-04-26 09:19:50 -060032 ret = acpi_add_table(ctx, dmar);
33 if (ret)
34 return log_msg_ret("add", ret);
Simon Glass17968c32020-04-26 09:19:46 -060035
36 return 0;
37}
Simon Glass2d67fdf2020-04-08 16:57:34 -060038
39static int testacpi_get_name(const struct udevice *dev, char *out_name)
40{
41 return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME);
42}
43
44struct acpi_ops testacpi_ops = {
45 .get_name = testacpi_get_name,
Simon Glass17968c32020-04-26 09:19:46 -060046 .write_tables = testacpi_write_tables,
Simon Glass2d67fdf2020-04-08 16:57:34 -060047};
48
49static const struct udevice_id testacpi_ids[] = {
50 { .compatible = "denx,u-boot-acpi-test" },
51 { }
52};
53
54U_BOOT_DRIVER(testacpi_drv) = {
55 .name = "testacpi_drv",
56 .of_match = testacpi_ids,
57 .id = UCLASS_TEST_ACPI,
58 ACPI_OPS_PTR(&testacpi_ops)
59};
60
61UCLASS_DRIVER(testacpi) = {
62 .name = "testacpi",
63 .id = UCLASS_TEST_ACPI,
64};
65
66/* Test ACPI get_name() */
67static int dm_test_acpi_get_name(struct unit_test_state *uts)
68{
69 char name[ACPI_NAME_MAX];
70 struct udevice *dev;
71
72 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
73 ut_assertok(acpi_get_name(dev, name));
74 ut_asserteq_str(ACPI_TEST_DEV_NAME, name);
75
76 return 0;
77}
78DM_TEST(dm_test_acpi_get_name, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassb2672ea2020-04-08 16:57:38 -060079
80/* Test acpi_get_table_revision() */
81static int dm_test_acpi_get_table_revision(struct unit_test_state *uts)
82{
83 ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG));
84 ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP));
85 ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2));
86 ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT));
87
88 return 0;
89}
90DM_TEST(dm_test_acpi_get_table_revision,
91 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glasse9629892020-04-08 16:57:39 -060092
Simon Glasse9629892020-04-08 16:57:39 -060093/* Test acpi_create_dmar() */
94static int dm_test_acpi_create_dmar(struct unit_test_state *uts)
95{
96 struct acpi_dmar dmar;
97
98 ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP));
99 ut_asserteq(DMAR_INTR_REMAP, dmar.flags);
100 ut_asserteq(32 - 1, dmar.host_address_width);
101
102 return 0;
103}
104DM_TEST(dm_test_acpi_create_dmar, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass17968c32020-04-26 09:19:46 -0600105
106/* Test acpi_fill_header() */
107static int dm_test_acpi_fill_header(struct unit_test_state *uts)
108{
109 struct acpi_table_header hdr;
110
111 /* Make sure these 5 fields are not changed */
112 hdr.length = 0x11;
113 hdr.revision = 0x22;
114 hdr.checksum = 0x33;
115 hdr.aslc_revision = 0x44;
116 acpi_fill_header(&hdr, "ABCD");
117
118 ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature));
119 ut_asserteq(0x11, hdr.length);
120 ut_asserteq(0x22, hdr.revision);
121 ut_asserteq(0x33, hdr.checksum);
122 ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id));
123 ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id,
124 sizeof(hdr.oem_table_id));
125 ut_asserteq(U_BOOT_BUILD_DATE, hdr.oem_revision);
126 ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id));
127 ut_asserteq(0x44, hdr.aslc_revision);
128
129 return 0;
130}
131DM_TEST(dm_test_acpi_fill_header, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
132
133/* Test ACPI write_tables() */
134static int dm_test_acpi_write_tables(struct unit_test_state *uts)
135{
136 struct acpi_dmar *dmar;
137 struct acpi_ctx ctx;
138 void *buf;
139
140 buf = malloc(BUF_SIZE);
141 ut_assertnonnull(buf);
142
Simon Glass9c442a62020-04-26 09:19:51 -0600143 acpi_setup_base_tables(&ctx, buf);
Simon Glass575a5472020-04-26 09:19:50 -0600144 dmar = ctx.current;
Simon Glass17968c32020-04-26 09:19:46 -0600145 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass17968c32020-04-26 09:19:46 -0600146
147 /*
148 * We should have two dmar tables, one for each "denx,u-boot-acpi-test"
149 * device
150 */
151 ut_asserteq_ptr(dmar + 2, ctx.current);
152 ut_asserteq(DMAR_INTR_REMAP, dmar->flags);
153 ut_asserteq(32 - 1, dmar->host_address_width);
154
155 ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags);
156 ut_asserteq(32 - 1, dmar[1].host_address_width);
157
Simon Glass9c442a62020-04-26 09:19:51 -0600158 /* Check that the pointers were added correctly */
159 ut_asserteq(map_to_sysmem(dmar), ctx.rsdt->entry[0]);
160 ut_asserteq(map_to_sysmem(dmar + 1), ctx.rsdt->entry[1]);
161 ut_asserteq(0, ctx.rsdt->entry[2]);
162
Simon Glass17968c32020-04-26 09:19:46 -0600163 return 0;
164}
165DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass0e113842020-04-26 09:19:47 -0600166
167/* Test basic ACPI functions */
168static int dm_test_acpi_basic(struct unit_test_state *uts)
169{
170 struct acpi_ctx ctx;
171
172 /* Check align works */
173 ctx.current = (void *)5;
174 acpi_align(&ctx);
175 ut_asserteq_ptr((void *)16, ctx.current);
176
177 /* Check that align does nothing if already aligned */
178 acpi_align(&ctx);
179 ut_asserteq_ptr((void *)16, ctx.current);
180 acpi_align64(&ctx);
181 ut_asserteq_ptr((void *)64, ctx.current);
182 acpi_align64(&ctx);
183 ut_asserteq_ptr((void *)64, ctx.current);
184
185 /* Check incrementing */
186 acpi_inc(&ctx, 3);
187 ut_asserteq_ptr((void *)67, ctx.current);
188 acpi_inc_align(&ctx, 3);
189 ut_asserteq_ptr((void *)80, ctx.current);
190
191 return 0;
192}
193DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass9c442a62020-04-26 09:19:51 -0600194
195/* Test acpi_setup_base_tables */
196static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
197{
198 struct acpi_rsdp *rsdp;
199 struct acpi_rsdt *rsdt;
200 struct acpi_xsdt *xsdt;
201 struct acpi_ctx ctx;
202 void *buf, *end;
203
204 /*
205 * Use an unaligned address deliberately, by allocating an aligned
206 * address and then adding 4 to it
207 */
208 buf = memalign(64, BUF_SIZE);
209 ut_assertnonnull(buf);
210 acpi_setup_base_tables(&ctx, buf + 4);
211
212 rsdp = buf + 16;
213 ut_asserteq_ptr(rsdp, ctx.rsdp);
214 ut_assertok(memcmp(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature)));
215 ut_asserteq(sizeof(*rsdp), rsdp->length);
216 ut_assertok(table_compute_checksum(rsdp, 20));
217 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
218
219 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
220 ut_asserteq_ptr(rsdt, ctx.rsdt);
221 ut_assertok(memcmp("RSDT", rsdt->header.signature, ACPI_NAME_LEN));
222 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
223 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
224
225 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
226 ut_assertok(memcmp("XSDT", xsdt->header.signature, ACPI_NAME_LEN));
227 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
228 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
229
230 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
231 ut_asserteq_ptr(end, ctx.current);
232
233 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
234 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
235
236 return 0;
237}
238DM_TEST(dm_test_acpi_setup_base_tables,
239 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);