Simon Glass | 2d67fdf | 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> |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 10 | #include <console.h> |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 13 | #include <mapmem.h> |
Pali Rohár | 6e1f085 | 2021-08-02 15:18:38 +0200 | [diff] [blame] | 14 | #include <timestamp.h> |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 15 | #include <version.h> |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 16 | #include <tables_csum.h> |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 17 | #include <version.h> |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 18 | #include <acpi/acpigen.h> |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 19 | #include <acpi/acpi_device.h> |
Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 20 | #include <acpi/acpi_table.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 21 | #include <asm/global_data.h> |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 22 | #include <dm/acpi.h> |
| 23 | #include <dm/test.h> |
| 24 | #include <test/ut.h> |
Simon Glass | 6fcdaf1 | 2020-07-07 21:32:10 -0600 | [diff] [blame] | 25 | #include "acpi.h" |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 26 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 27 | #define BUF_SIZE 4096 |
| 28 | |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 29 | #define OEM_REVISION ((((U_BOOT_VERSION_NUM / 1000) % 10) << 28) | \ |
| 30 | (((U_BOOT_VERSION_NUM / 100) % 10) << 24) | \ |
| 31 | (((U_BOOT_VERSION_NUM / 10) % 10) << 20) | \ |
| 32 | ((U_BOOT_VERSION_NUM % 10) << 16) | \ |
| 33 | (((U_BOOT_VERSION_NUM_PATCH / 10) % 10) << 12) | \ |
| 34 | ((U_BOOT_VERSION_NUM_PATCH % 10) << 8) | \ |
| 35 | 0x01) |
| 36 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 37 | /** |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 38 | * struct testacpi_plat - Platform data for the test ACPI device |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 39 | * |
| 40 | * @no_name: true to emit an empty ACPI name from testacpi_get_name() |
| 41 | * @return_error: true to return an error instead of a name |
| 42 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 43 | struct testacpi_plat { |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 44 | bool return_error; |
| 45 | bool no_name; |
| 46 | }; |
| 47 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 48 | static int testacpi_write_tables(const struct udevice *dev, |
| 49 | struct acpi_ctx *ctx) |
| 50 | { |
| 51 | struct acpi_dmar *dmar; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 52 | int ret; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 53 | |
| 54 | dmar = (struct acpi_dmar *)ctx->current; |
| 55 | acpi_create_dmar(dmar, DMAR_INTR_REMAP); |
| 56 | ctx->current += sizeof(struct acpi_dmar); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 57 | ret = acpi_add_table(ctx, dmar); |
| 58 | if (ret) |
| 59 | return log_msg_ret("add", ret); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 60 | |
| 61 | return 0; |
| 62 | } |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 63 | |
| 64 | static int testacpi_get_name(const struct udevice *dev, char *out_name) |
| 65 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 66 | struct testacpi_plat *plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 67 | |
| 68 | if (plat->return_error) |
| 69 | return -EINVAL; |
| 70 | if (plat->no_name) { |
| 71 | *out_name = '\0'; |
| 72 | return 0; |
| 73 | } |
| 74 | if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI) |
| 75 | return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME); |
| 76 | else |
| 77 | return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME); |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 78 | } |
| 79 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 80 | static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 81 | { |
| 82 | const char *data; |
| 83 | |
| 84 | data = dev_read_string(dev, "acpi-ssdt-test-data"); |
| 85 | if (data) { |
| 86 | while (*data) |
| 87 | acpigen_emit_byte(ctx, *data++); |
| 88 | } |
| 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 93 | static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 94 | { |
| 95 | const char *data; |
| 96 | |
| 97 | data = dev_read_string(dev, "acpi-dsdt-test-data"); |
| 98 | if (data) { |
| 99 | while (*data) |
| 100 | acpigen_emit_byte(ctx, *data++); |
| 101 | } |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 106 | struct acpi_ops testacpi_ops = { |
| 107 | .get_name = testacpi_get_name, |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 108 | .write_tables = testacpi_write_tables, |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 109 | .fill_ssdt = testacpi_fill_ssdt, |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 110 | .inject_dsdt = testacpi_inject_dsdt, |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 111 | }; |
| 112 | |
| 113 | static const struct udevice_id testacpi_ids[] = { |
| 114 | { .compatible = "denx,u-boot-acpi-test" }, |
| 115 | { } |
| 116 | }; |
| 117 | |
| 118 | U_BOOT_DRIVER(testacpi_drv) = { |
| 119 | .name = "testacpi_drv", |
| 120 | .of_match = testacpi_ids, |
| 121 | .id = UCLASS_TEST_ACPI, |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 122 | .bind = dm_scan_fdt_dev, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 123 | .plat_auto = sizeof(struct testacpi_plat), |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 124 | ACPI_OPS_PTR(&testacpi_ops) |
| 125 | }; |
| 126 | |
| 127 | UCLASS_DRIVER(testacpi) = { |
| 128 | .name = "testacpi", |
| 129 | .id = UCLASS_TEST_ACPI, |
| 130 | }; |
| 131 | |
| 132 | /* Test ACPI get_name() */ |
| 133 | static int dm_test_acpi_get_name(struct unit_test_state *uts) |
| 134 | { |
| 135 | char name[ACPI_NAME_MAX]; |
Simon Glass | 9a65513 | 2020-12-16 21:20:23 -0700 | [diff] [blame] | 136 | struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound; |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 137 | struct udevice *pci, *root; |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 138 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 139 | /* Test getting the name from the driver */ |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 140 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 141 | ut_assertok(acpi_get_name(dev, name)); |
| 142 | ut_asserteq_str(ACPI_TEST_DEV_NAME, name); |
| 143 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 144 | /* Test getting the name from the device tree */ |
| 145 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test", |
| 146 | &dev2)); |
| 147 | ut_assertok(acpi_get_name(dev2, name)); |
| 148 | ut_asserteq_str("GHIJ", name); |
| 149 | |
| 150 | /* Test getting the name from acpi_device_get_name() */ |
| 151 | ut_assertok(uclass_first_device(UCLASS_I2C, &i2c)); |
| 152 | ut_assertok(acpi_get_name(i2c, name)); |
| 153 | ut_asserteq_str("I2C0", name); |
| 154 | |
| 155 | ut_assertok(uclass_first_device(UCLASS_SPI, &spi)); |
| 156 | ut_assertok(acpi_get_name(spi, name)); |
| 157 | ut_asserteq_str("SPI0", name); |
| 158 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 159 | /* ACPI doesn't know about the timer */ |
| 160 | ut_assertok(uclass_first_device(UCLASS_TIMER, &timer)); |
| 161 | ut_asserteq(-ENOENT, acpi_get_name(timer, name)); |
| 162 | |
| 163 | /* May as well test the rest of the cases */ |
| 164 | ut_assertok(uclass_first_device(UCLASS_SOUND, &sound)); |
| 165 | ut_assertok(acpi_get_name(sound, name)); |
| 166 | ut_asserteq_str("HDAS", name); |
| 167 | |
| 168 | ut_assertok(uclass_first_device(UCLASS_PCI, &pci)); |
| 169 | ut_assertok(acpi_get_name(pci, name)); |
| 170 | ut_asserteq_str("PCI0", name); |
| 171 | |
| 172 | ut_assertok(uclass_first_device(UCLASS_ROOT, &root)); |
| 173 | ut_assertok(acpi_get_name(root, name)); |
| 174 | ut_asserteq_str("\\_SB", name); |
| 175 | |
| 176 | /* Note that we don't have tests for acpi_name_from_id() */ |
| 177 | |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 178 | return 0; |
| 179 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 180 | DM_TEST(dm_test_acpi_get_name, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 181 | |
| 182 | /* Test acpi_get_table_revision() */ |
| 183 | static int dm_test_acpi_get_table_revision(struct unit_test_state *uts) |
| 184 | { |
| 185 | ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG)); |
| 186 | ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP)); |
| 187 | ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2)); |
| 188 | ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT)); |
| 189 | |
| 190 | return 0; |
| 191 | } |
| 192 | DM_TEST(dm_test_acpi_get_table_revision, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 193 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 194 | |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 195 | /* Test acpi_create_dmar() */ |
| 196 | static int dm_test_acpi_create_dmar(struct unit_test_state *uts) |
| 197 | { |
| 198 | struct acpi_dmar dmar; |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 199 | struct udevice *cpu; |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 200 | |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 201 | ut_assertok(uclass_first_device(UCLASS_CPU, &cpu)); |
| 202 | ut_assertnonnull(cpu); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 203 | ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP)); |
| 204 | ut_asserteq(DMAR_INTR_REMAP, dmar.flags); |
| 205 | ut_asserteq(32 - 1, dmar.host_address_width); |
| 206 | |
| 207 | return 0; |
| 208 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 209 | DM_TEST(dm_test_acpi_create_dmar, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 210 | |
| 211 | /* Test acpi_fill_header() */ |
| 212 | static int dm_test_acpi_fill_header(struct unit_test_state *uts) |
| 213 | { |
| 214 | struct acpi_table_header hdr; |
| 215 | |
| 216 | /* Make sure these 5 fields are not changed */ |
| 217 | hdr.length = 0x11; |
| 218 | hdr.revision = 0x22; |
| 219 | hdr.checksum = 0x33; |
| 220 | hdr.aslc_revision = 0x44; |
| 221 | acpi_fill_header(&hdr, "ABCD"); |
| 222 | |
| 223 | ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature)); |
| 224 | ut_asserteq(0x11, hdr.length); |
| 225 | ut_asserteq(0x22, hdr.revision); |
| 226 | ut_asserteq(0x33, hdr.checksum); |
| 227 | ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id)); |
| 228 | ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id, |
| 229 | sizeof(hdr.oem_table_id)); |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 230 | ut_asserteq(OEM_REVISION, hdr.oem_revision); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 231 | ut_asserteq_mem(ASLC_ID, hdr.aslc_id, sizeof(hdr.aslc_id)); |
| 232 | ut_asserteq(0x44, hdr.aslc_revision); |
| 233 | |
| 234 | return 0; |
| 235 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 236 | DM_TEST(dm_test_acpi_fill_header, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 237 | |
| 238 | /* Test ACPI write_tables() */ |
| 239 | static int dm_test_acpi_write_tables(struct unit_test_state *uts) |
| 240 | { |
| 241 | struct acpi_dmar *dmar; |
| 242 | struct acpi_ctx ctx; |
| 243 | void *buf; |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 244 | int i; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 245 | |
| 246 | buf = malloc(BUF_SIZE); |
| 247 | ut_assertnonnull(buf); |
| 248 | |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 249 | acpi_setup_base_tables(&ctx, buf); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 250 | dmar = ctx.current; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 251 | ut_assertok(acpi_write_dev_tables(&ctx)); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 252 | |
| 253 | /* |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 254 | * We should have three dmar tables, one for each |
| 255 | * "denx,u-boot-acpi-test" device |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 256 | */ |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 257 | ut_asserteq_ptr(dmar + 3, ctx.current); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 258 | ut_asserteq(DMAR_INTR_REMAP, dmar->flags); |
| 259 | ut_asserteq(32 - 1, dmar->host_address_width); |
| 260 | |
| 261 | ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags); |
| 262 | ut_asserteq(32 - 1, dmar[1].host_address_width); |
| 263 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 264 | ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags); |
| 265 | ut_asserteq(32 - 1, dmar[2].host_address_width); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 266 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 267 | /* Check that the pointers were added correctly */ |
| 268 | for (i = 0; i < 3; i++) { |
| 269 | ut_asserteq(map_to_sysmem(dmar + i), ctx.rsdt->entry[i]); |
| 270 | ut_asserteq(map_to_sysmem(dmar + i), ctx.xsdt->entry[i]); |
| 271 | } |
| 272 | ut_asserteq(0, ctx.rsdt->entry[3]); |
| 273 | ut_asserteq(0, ctx.xsdt->entry[3]); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 274 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 275 | return 0; |
| 276 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 277 | DM_TEST(dm_test_acpi_write_tables, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 0e11384 | 2020-04-26 09:19:47 -0600 | [diff] [blame] | 278 | |
| 279 | /* Test basic ACPI functions */ |
| 280 | static int dm_test_acpi_basic(struct unit_test_state *uts) |
| 281 | { |
| 282 | struct acpi_ctx ctx; |
| 283 | |
| 284 | /* Check align works */ |
| 285 | ctx.current = (void *)5; |
| 286 | acpi_align(&ctx); |
| 287 | ut_asserteq_ptr((void *)16, ctx.current); |
| 288 | |
| 289 | /* Check that align does nothing if already aligned */ |
| 290 | acpi_align(&ctx); |
| 291 | ut_asserteq_ptr((void *)16, ctx.current); |
| 292 | acpi_align64(&ctx); |
| 293 | ut_asserteq_ptr((void *)64, ctx.current); |
| 294 | acpi_align64(&ctx); |
| 295 | ut_asserteq_ptr((void *)64, ctx.current); |
| 296 | |
| 297 | /* Check incrementing */ |
| 298 | acpi_inc(&ctx, 3); |
| 299 | ut_asserteq_ptr((void *)67, ctx.current); |
| 300 | acpi_inc_align(&ctx, 3); |
| 301 | ut_asserteq_ptr((void *)80, ctx.current); |
| 302 | |
| 303 | return 0; |
| 304 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 305 | DM_TEST(dm_test_acpi_basic, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 306 | |
| 307 | /* Test acpi_setup_base_tables */ |
| 308 | static int dm_test_acpi_setup_base_tables(struct unit_test_state *uts) |
| 309 | { |
| 310 | struct acpi_rsdp *rsdp; |
| 311 | struct acpi_rsdt *rsdt; |
| 312 | struct acpi_xsdt *xsdt; |
| 313 | struct acpi_ctx ctx; |
| 314 | void *buf, *end; |
| 315 | |
| 316 | /* |
| 317 | * Use an unaligned address deliberately, by allocating an aligned |
| 318 | * address and then adding 4 to it |
| 319 | */ |
| 320 | buf = memalign(64, BUF_SIZE); |
| 321 | ut_assertnonnull(buf); |
| 322 | acpi_setup_base_tables(&ctx, buf + 4); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 323 | ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd->arch.acpi_start); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 324 | |
| 325 | rsdp = buf + 16; |
| 326 | ut_asserteq_ptr(rsdp, ctx.rsdp); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 327 | ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature)); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 328 | ut_asserteq(sizeof(*rsdp), rsdp->length); |
| 329 | ut_assertok(table_compute_checksum(rsdp, 20)); |
| 330 | ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp))); |
| 331 | |
| 332 | rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16); |
| 333 | ut_asserteq_ptr(rsdt, ctx.rsdt); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 334 | ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 335 | ut_asserteq(sizeof(*rsdt), rsdt->header.length); |
| 336 | ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt))); |
| 337 | |
| 338 | xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 339 | ut_asserteq_ptr(xsdt, ctx.xsdt); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 340 | ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 341 | ut_asserteq(sizeof(*xsdt), xsdt->header.length); |
| 342 | ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt))); |
| 343 | |
| 344 | end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64); |
| 345 | ut_asserteq_ptr(end, ctx.current); |
| 346 | |
| 347 | ut_asserteq(map_to_sysmem(rsdt), rsdp->rsdt_address); |
| 348 | ut_asserteq(map_to_sysmem(xsdt), rsdp->xsdt_address); |
| 349 | |
| 350 | return 0; |
| 351 | } |
| 352 | DM_TEST(dm_test_acpi_setup_base_tables, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 353 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 354 | |
| 355 | /* Test 'acpi list' command */ |
| 356 | static int dm_test_acpi_cmd_list(struct unit_test_state *uts) |
| 357 | { |
| 358 | struct acpi_ctx ctx; |
| 359 | ulong addr; |
| 360 | void *buf; |
| 361 | |
| 362 | buf = memalign(16, BUF_SIZE); |
| 363 | ut_assertnonnull(buf); |
| 364 | acpi_setup_base_tables(&ctx, buf); |
| 365 | |
| 366 | ut_assertok(acpi_write_dev_tables(&ctx)); |
| 367 | |
| 368 | console_record_reset(); |
| 369 | run_command("acpi list", 0); |
| 370 | addr = (ulong)map_to_sysmem(buf); |
| 371 | ut_assert_nextline("ACPI tables start at %lx", addr); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 372 | ut_assert_nextline("RSDP %08lx %06zx (v02 U-BOOT)", addr, |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 373 | sizeof(struct acpi_rsdp)); |
| 374 | addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 375 | ut_assert_nextline("RSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)", |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 376 | addr, sizeof(struct acpi_table_header) + |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 377 | 3 * sizeof(u32), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 378 | addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 379 | ut_assert_nextline("XSDT %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)", |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 380 | addr, sizeof(struct acpi_table_header) + |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 381 | 3 * sizeof(u64), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 382 | addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 383 | ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 384 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 385 | addr = ALIGN(addr + sizeof(struct acpi_dmar), 16); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 386 | ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 387 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 388 | addr = ALIGN(addr + sizeof(struct acpi_dmar), 16); |
Simon Glass | 60e08ee | 2021-02-07 14:27:05 -0700 | [diff] [blame] | 389 | ut_assert_nextline("DMAR %08lx %06zx (v01 U-BOOT U-BOOTBL %x INTL 0)", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 390 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 391 | ut_assert_console_end(); |
| 392 | |
| 393 | return 0; |
| 394 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 395 | DM_TEST(dm_test_acpi_cmd_list, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 396 | |
| 397 | /* Test 'acpi dump' command */ |
| 398 | static int dm_test_acpi_cmd_dump(struct unit_test_state *uts) |
| 399 | { |
| 400 | struct acpi_ctx ctx; |
| 401 | ulong addr; |
| 402 | void *buf; |
| 403 | |
| 404 | buf = memalign(16, BUF_SIZE); |
| 405 | ut_assertnonnull(buf); |
| 406 | acpi_setup_base_tables(&ctx, buf); |
| 407 | |
| 408 | ut_assertok(acpi_write_dev_tables(&ctx)); |
| 409 | |
| 410 | /* First search for a non-existent table */ |
| 411 | console_record_reset(); |
| 412 | run_command("acpi dump rdst", 0); |
| 413 | ut_assert_nextline("Table 'RDST' not found"); |
| 414 | ut_assert_console_end(); |
| 415 | |
| 416 | /* Now a real table */ |
| 417 | console_record_reset(); |
| 418 | run_command("acpi dump dmar", 0); |
| 419 | addr = ALIGN(map_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64); |
| 420 | ut_assert_nextline("DMAR @ %08lx", addr); |
| 421 | ut_assert_nextlines_are_dump(0x30); |
| 422 | ut_assert_console_end(); |
| 423 | |
| 424 | return 0; |
| 425 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 426 | DM_TEST(dm_test_acpi_cmd_dump, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 427 | |
| 428 | /* Test acpi_device_path() */ |
| 429 | static int dm_test_acpi_device_path(struct unit_test_state *uts) |
| 430 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 431 | struct testacpi_plat *plat; |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 432 | char buf[ACPI_PATH_MAX]; |
| 433 | struct udevice *dev, *child; |
| 434 | |
| 435 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 436 | ut_assertok(acpi_device_path(dev, buf, sizeof(buf))); |
| 437 | ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf); |
| 438 | |
| 439 | /* Test running out of space */ |
| 440 | buf[5] = '\0'; |
| 441 | ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5)); |
| 442 | ut_asserteq('\0', buf[5]); |
| 443 | |
| 444 | /* Test a three-component name */ |
| 445 | ut_assertok(device_first_child_err(dev, &child)); |
| 446 | ut_assertok(acpi_device_path(child, buf, sizeof(buf))); |
| 447 | ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME, |
| 448 | buf); |
| 449 | |
| 450 | /* Test handling of a device which doesn't produce a name */ |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 451 | plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 452 | plat->no_name = true; |
| 453 | ut_assertok(acpi_device_path(child, buf, sizeof(buf))); |
| 454 | ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf); |
| 455 | |
| 456 | /* Test handling of a device which returns an error */ |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 457 | plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 458 | plat->return_error = true; |
| 459 | ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf))); |
| 460 | |
| 461 | return 0; |
| 462 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 463 | DM_TEST(dm_test_acpi_device_path, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 8965cc9 | 2020-07-07 13:11:40 -0600 | [diff] [blame] | 464 | |
| 465 | /* Test acpi_device_status() */ |
| 466 | static int dm_test_acpi_device_status(struct unit_test_state *uts) |
| 467 | { |
| 468 | struct udevice *dev; |
| 469 | |
| 470 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 471 | ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev)); |
| 472 | |
| 473 | return 0; |
| 474 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 475 | DM_TEST(dm_test_acpi_device_status, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 476 | |
| 477 | /* Test acpi_fill_ssdt() */ |
| 478 | static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts) |
| 479 | { |
| 480 | struct acpi_ctx ctx; |
| 481 | u8 *buf; |
| 482 | |
| 483 | buf = malloc(BUF_SIZE); |
| 484 | ut_assertnonnull(buf); |
| 485 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 486 | acpi_reset_items(); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 487 | ctx.current = buf; |
| 488 | buf[4] = 'z'; /* sentinel */ |
| 489 | ut_assertok(acpi_fill_ssdt(&ctx)); |
| 490 | |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 491 | /* |
| 492 | * These values come from acpi-test2's acpi-ssdt-test-data property. |
| 493 | * This device comes first because of u-boot,acpi-ssdt-order |
| 494 | */ |
| 495 | ut_asserteq('c', buf[0]); |
| 496 | ut_asserteq('d', buf[1]); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 497 | |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 498 | /* These values come from acpi-test's acpi-ssdt-test-data property */ |
| 499 | ut_asserteq('a', buf[2]); |
| 500 | ut_asserteq('b', buf[3]); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 501 | |
| 502 | ut_asserteq('z', buf[4]); |
| 503 | |
| 504 | return 0; |
| 505 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 506 | DM_TEST(dm_test_acpi_fill_ssdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 507 | |
| 508 | /* Test acpi_inject_dsdt() */ |
| 509 | static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts) |
| 510 | { |
| 511 | struct acpi_ctx ctx; |
| 512 | u8 *buf; |
| 513 | |
| 514 | buf = malloc(BUF_SIZE); |
| 515 | ut_assertnonnull(buf); |
| 516 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 517 | acpi_reset_items(); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 518 | ctx.current = buf; |
| 519 | buf[4] = 'z'; /* sentinel */ |
| 520 | ut_assertok(acpi_inject_dsdt(&ctx)); |
| 521 | |
| 522 | /* |
| 523 | * These values come from acpi-test's acpi-dsdt-test-data property. |
| 524 | * There is no u-boot,acpi-dsdt-order so device-tree order is used. |
| 525 | */ |
| 526 | ut_asserteq('h', buf[0]); |
| 527 | ut_asserteq('i', buf[1]); |
| 528 | |
| 529 | /* These values come from acpi-test's acpi-dsdt-test-data property */ |
| 530 | ut_asserteq('j', buf[2]); |
| 531 | ut_asserteq('k', buf[3]); |
| 532 | |
| 533 | ut_asserteq('z', buf[4]); |
| 534 | |
| 535 | return 0; |
| 536 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 537 | DM_TEST(dm_test_acpi_inject_dsdt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 538 | |
| 539 | /* Test 'acpi items' command */ |
| 540 | static int dm_test_acpi_cmd_items(struct unit_test_state *uts) |
| 541 | { |
| 542 | struct acpi_ctx ctx; |
| 543 | void *buf; |
| 544 | |
| 545 | buf = malloc(BUF_SIZE); |
| 546 | ut_assertnonnull(buf); |
| 547 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 548 | acpi_reset_items(); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 549 | ctx.current = buf; |
| 550 | ut_assertok(acpi_fill_ssdt(&ctx)); |
| 551 | console_record_reset(); |
| 552 | run_command("acpi items", 0); |
| 553 | ut_assert_nextline("dev 'acpi-test', type 1, size 2"); |
| 554 | ut_assert_nextline("dev 'acpi-test2', type 1, size 2"); |
| 555 | ut_assert_console_end(); |
| 556 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 557 | acpi_reset_items(); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 558 | ctx.current = buf; |
| 559 | ut_assertok(acpi_inject_dsdt(&ctx)); |
| 560 | console_record_reset(); |
| 561 | run_command("acpi items", 0); |
| 562 | ut_assert_nextline("dev 'acpi-test', type 2, size 2"); |
| 563 | ut_assert_nextline("dev 'acpi-test2', type 2, size 2"); |
| 564 | ut_assert_console_end(); |
| 565 | |
| 566 | console_record_reset(); |
| 567 | run_command("acpi items -d", 0); |
| 568 | ut_assert_nextline("dev 'acpi-test', type 2, size 2"); |
| 569 | ut_assert_nextlines_are_dump(2); |
| 570 | ut_assert_nextline("%s", ""); |
| 571 | ut_assert_nextline("dev 'acpi-test2', type 2, size 2"); |
| 572 | ut_assert_nextlines_are_dump(2); |
| 573 | ut_assert_nextline("%s", ""); |
| 574 | ut_assert_console_end(); |
| 575 | |
| 576 | return 0; |
| 577 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 578 | DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |