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