blob: a29c3cab3e02f7240d3a3231670b841bfd38b086 [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>
10#include <dm.h>
Simon Glass7e586f62020-04-26 09:19:51 -060011#include <malloc.h>
Simon Glass29b35112020-04-26 09:19:50 -060012#include <mapmem.h>
Simon Glass7e586f62020-04-26 09:19:51 -060013#include <tables_csum.h>
Simon Glass93f7f822020-04-26 09:19:46 -060014#include <version.h>
Simon Glass91fe8b72020-04-08 16:57:38 -060015#include <acpi/acpi_table.h>
Simon Glassf50cc952020-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 Glass93f7f822020-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 Glass29b35112020-04-26 09:19:50 -060027 int ret;
Simon Glass93f7f822020-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 Glass29b35112020-04-26 09:19:50 -060032 ret = acpi_add_table(ctx, dmar);
33 if (ret)
34 return log_msg_ret("add", ret);
Simon Glass93f7f822020-04-26 09:19:46 -060035
36 return 0;
37}
Simon Glassf50cc952020-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 Glass93f7f822020-04-26 09:19:46 -060046 .write_tables = testacpi_write_tables,
Simon Glassf50cc952020-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 Glass91fe8b72020-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 Glassbfeb5d42020-04-08 16:57:39 -060092
Simon Glassbfeb5d42020-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 Glass93f7f822020-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 Glass7e586f62020-04-26 09:19:51 -0600143 acpi_setup_base_tables(&ctx, buf);
Simon Glass29b35112020-04-26 09:19:50 -0600144 dmar = ctx.current;
Simon Glass93f7f822020-04-26 09:19:46 -0600145 ut_assertok(acpi_write_dev_tables(&ctx));
Simon Glass93f7f822020-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 Glass7e586f62020-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 Glassb38309b2020-04-26 09:19:52 -0600163 ut_asserteq(map_to_sysmem(dmar), ctx.xsdt->entry[0]);
164 ut_asserteq(map_to_sysmem(dmar + 1), ctx.xsdt->entry[1]);
165 ut_asserteq(0, ctx.xsdt->entry[2]);
166
Simon Glass93f7f822020-04-26 09:19:46 -0600167 return 0;
168}
169DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass86e17782020-04-26 09:19:47 -0600170
171/* Test basic ACPI functions */
172static int dm_test_acpi_basic(struct unit_test_state *uts)
173{
174 struct acpi_ctx ctx;
175
176 /* Check align works */
177 ctx.current = (void *)5;
178 acpi_align(&ctx);
179 ut_asserteq_ptr((void *)16, ctx.current);
180
181 /* Check that align does nothing if already aligned */
182 acpi_align(&ctx);
183 ut_asserteq_ptr((void *)16, ctx.current);
184 acpi_align64(&ctx);
185 ut_asserteq_ptr((void *)64, ctx.current);
186 acpi_align64(&ctx);
187 ut_asserteq_ptr((void *)64, ctx.current);
188
189 /* Check incrementing */
190 acpi_inc(&ctx, 3);
191 ut_asserteq_ptr((void *)67, ctx.current);
192 acpi_inc_align(&ctx, 3);
193 ut_asserteq_ptr((void *)80, ctx.current);
194
195 return 0;
196}
197DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass7e586f62020-04-26 09:19:51 -0600198
199/* Test acpi_setup_base_tables */
200static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts)
201{
202 struct acpi_rsdp *rsdp;
203 struct acpi_rsdt *rsdt;
204 struct acpi_xsdt *xsdt;
205 struct acpi_ctx ctx;
206 void *buf, *end;
207
208 /*
209 * Use an unaligned address deliberately, by allocating an aligned
210 * address and then adding 4 to it
211 */
212 buf = memalign(64, BUF_SIZE);
213 ut_assertnonnull(buf);
214 acpi_setup_base_tables(&ctx, buf + 4);
215
216 rsdp = buf + 16;
217 ut_asserteq_ptr(rsdp, ctx.rsdp);
218 ut_assertok(memcmp(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature)));
219 ut_asserteq(sizeof(*rsdp), rsdp->length);
220 ut_assertok(table_compute_checksum(rsdp, 20));
221 ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp)));
222
223 rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16);
224 ut_asserteq_ptr(rsdt, ctx.rsdt);
225 ut_assertok(memcmp("RSDT", rsdt->header.signature, ACPI_NAME_LEN));
226 ut_asserteq(sizeof(*rsdt), rsdt->header.length);
227 ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt)));
228
229 xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16);
Simon Glassb38309b2020-04-26 09:19:52 -0600230 ut_asserteq_ptr(xsdt, ctx.xsdt);
Simon Glass7e586f62020-04-26 09:19:51 -0600231 ut_assertok(memcmp("XSDT", xsdt->header.signature, ACPI_NAME_LEN));
232 ut_asserteq(sizeof(*xsdt), xsdt->header.length);
233 ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt)));
234
235 end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64);
236 ut_asserteq_ptr(end, ctx.current);
237
238 ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address);
239 ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address);
240
241 return 0;
242}
243DM_TEST(dm_test_acpi_setup_base_tables,
244 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);