Simon Glass | f50cc95 | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 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> |
Simon Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 29b3511 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 13 | #include <tables_csum.h> |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 14 | #include <version.h> |
Simon Glass | 91fe8b7 | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 15 | #include <acpi/acpi_table.h> |
Simon Glass | f50cc95 | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 16 | #include <dm/acpi.h> |
| 17 | #include <dm/test.h> |
| 18 | #include <test/ut.h> |
| 19 | |
| 20 | #define ACPI_TEST_DEV_NAME "ABCD" |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 21 | #define BUF_SIZE 4096 |
| 22 | |
| 23 | static int testacpi_write_tables(const struct udevice *dev, |
| 24 | struct acpi_ctx *ctx) |
| 25 | { |
| 26 | struct acpi_dmar *dmar; |
Simon Glass | 29b3511 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 27 | int ret; |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 28 | |
| 29 | dmar = (struct acpi_dmar *)ctx->current; |
| 30 | acpi_create_dmar(dmar, DMAR_INTR_REMAP); |
| 31 | ctx->current += sizeof(struct acpi_dmar); |
Simon Glass | 29b3511 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 32 | ret = acpi_add_table(ctx, dmar); |
| 33 | if (ret) |
| 34 | return log_msg_ret("add", ret); |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 35 | |
| 36 | return 0; |
| 37 | } |
Simon Glass | f50cc95 | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 38 | |
| 39 | static 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 | |
| 44 | struct acpi_ops testacpi_ops = { |
| 45 | .get_name = testacpi_get_name, |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 46 | .write_tables = testacpi_write_tables, |
Simon Glass | f50cc95 | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 47 | }; |
| 48 | |
| 49 | static const struct udevice_id testacpi_ids[] = { |
| 50 | { .compatible = "denx,u-boot-acpi-test" }, |
| 51 | { } |
| 52 | }; |
| 53 | |
| 54 | U_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 | |
| 61 | UCLASS_DRIVER(testacpi) = { |
| 62 | .name = "testacpi", |
| 63 | .id = UCLASS_TEST_ACPI, |
| 64 | }; |
| 65 | |
| 66 | /* Test ACPI get_name() */ |
| 67 | static 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 | } |
| 78 | DM_TEST(dm_test_acpi_get_name, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 91fe8b7 | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 79 | |
| 80 | /* Test acpi_get_table_revision() */ |
| 81 | static 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 | } |
| 90 | DM_TEST(dm_test_acpi_get_table_revision, |
| 91 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | bfeb5d4 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 92 | |
Simon Glass | bfeb5d4 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 93 | /* Test acpi_create_dmar() */ |
| 94 | static 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 | } |
| 104 | DM_TEST(dm_test_acpi_create_dmar, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 105 | |
| 106 | /* Test acpi_fill_header() */ |
| 107 | static 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 | } |
| 131 | DM_TEST(dm_test_acpi_fill_header, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 132 | |
| 133 | /* Test ACPI write_tables() */ |
| 134 | static 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 Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 143 | acpi_setup_base_tables(&ctx, buf); |
Simon Glass | 29b3511 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 144 | dmar = ctx.current; |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 145 | ut_assertok(acpi_write_dev_tables(&ctx)); |
Simon Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 146 | |
| 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 Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 158 | /* 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 Glass | b38309b | 2020-04-26 09:19:52 -0600 | [diff] [blame^] | 163 | 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 Glass | 93f7f82 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 167 | return 0; |
| 168 | } |
| 169 | DM_TEST(dm_test_acpi_write_tables, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 86e1778 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 170 | |
| 171 | /* Test basic ACPI functions */ |
| 172 | static 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 | } |
| 197 | DM_TEST(dm_test_acpi_basic, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 198 | |
| 199 | /* Test acpi_setup_base_tables */ |
| 200 | static 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 Glass | b38309b | 2020-04-26 09:19:52 -0600 | [diff] [blame^] | 230 | ut_asserteq_ptr(xsdt, ctx.xsdt); |
Simon Glass | 7e586f6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 231 | 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 | } |
| 243 | DM_TEST(dm_test_acpi_setup_base_tables, |
| 244 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |