blob: 59aee1f6146971d768bfd2e2676ade81f36a27a1 [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 Glass93f7f822020-04-26 09:19:46 -060011#include <version.h>
Simon Glass91fe8b72020-04-08 16:57:38 -060012#include <acpi/acpi_table.h>
Simon Glassf50cc952020-04-08 16:57:34 -060013#include <dm/acpi.h>
14#include <dm/test.h>
15#include <test/ut.h>
16
17#define ACPI_TEST_DEV_NAME "ABCD"
Simon Glass93f7f822020-04-26 09:19:46 -060018#define BUF_SIZE 4096
19
20static 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}
Simon Glassf50cc952020-04-08 16:57:34 -060031
32static 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
37struct acpi_ops testacpi_ops = {
38 .get_name = testacpi_get_name,
Simon Glass93f7f822020-04-26 09:19:46 -060039 .write_tables = testacpi_write_tables,
Simon Glassf50cc952020-04-08 16:57:34 -060040};
41
42static const struct udevice_id testacpi_ids[] = {
43 { .compatible = "denx,u-boot-acpi-test" },
44 { }
45};
46
47U_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
54UCLASS_DRIVER(testacpi) = {
55 .name = "testacpi",
56 .id = UCLASS_TEST_ACPI,
57};
58
59/* Test ACPI get_name() */
60static 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}
71DM_TEST(dm_test_acpi_get_name, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass91fe8b72020-04-08 16:57:38 -060072
73/* Test acpi_get_table_revision() */
74static 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}
83DM_TEST(dm_test_acpi_get_table_revision,
84 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassbfeb5d42020-04-08 16:57:39 -060085
Simon Glassbfeb5d42020-04-08 16:57:39 -060086/* Test acpi_create_dmar() */
87static 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}
97DM_TEST(dm_test_acpi_create_dmar, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass93f7f822020-04-26 09:19:46 -060098
99/* Test acpi_fill_header() */
100static 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}
124DM_TEST(dm_test_acpi_fill_header, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
125
126/* Test ACPI write_tables() */
127static 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}
153DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass86e17782020-04-26 09:19:47 -0600154
155/* Test basic ACPI functions */
156static int dm_test_acpi_basic(struct unit_test_state *uts)
157{
158 struct acpi_ctx ctx;
159
160 /* Check align works */
161 ctx.current = (void *)5;
162 acpi_align(&ctx);
163 ut_asserteq_ptr((void *)16, ctx.current);
164
165 /* Check that align does nothing if already aligned */
166 acpi_align(&ctx);
167 ut_asserteq_ptr((void *)16, ctx.current);
168 acpi_align64(&ctx);
169 ut_asserteq_ptr((void *)64, ctx.current);
170 acpi_align64(&ctx);
171 ut_asserteq_ptr((void *)64, ctx.current);
172
173 /* Check incrementing */
174 acpi_inc(&ctx, 3);
175 ut_asserteq_ptr((void *)67, ctx.current);
176 acpi_inc_align(&ctx, 3);
177 ut_asserteq_ptr((void *)80, ctx.current);
178
179 return 0;
180}
181DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);