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 | |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 9 | #include <console.h> |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 11 | #include <malloc.h> |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 13 | #include <tables_csum.h> |
Simon Glass | 90b0127 | 2023-04-29 19:21:46 -0600 | [diff] [blame] | 14 | #include <version_string.h> |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 15 | #include <acpi/acpigen.h> |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 16 | #include <acpi/acpi_device.h> |
Simon Glass | b2672ea | 2020-04-08 16:57:38 -0600 | [diff] [blame] | 17 | #include <acpi/acpi_table.h> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 18 | #include <asm/global_data.h> |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 19 | #include <dm/acpi.h> |
| 20 | #include <dm/test.h> |
| 21 | #include <test/ut.h> |
Simon Glass | 6fcdaf1 | 2020-07-07 21:32:10 -0600 | [diff] [blame] | 22 | #include "acpi.h" |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 23 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 24 | #define BUF_SIZE 4096 |
| 25 | |
Simon Glass | 90b0127 | 2023-04-29 19:21:46 -0600 | [diff] [blame] | 26 | #define OEM_REVISION ((((version_num / 1000) % 10) << 28) | \ |
| 27 | (((version_num / 100) % 10) << 24) | \ |
| 28 | (((version_num / 10) % 10) << 20) | \ |
| 29 | ((version_num % 10) << 16) | \ |
| 30 | (((version_num_patch / 10) % 10) << 12) | \ |
| 31 | ((version_num_patch % 10) << 8) | \ |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 32 | 0x01) |
| 33 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 34 | /** |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 35 | * struct testacpi_plat - Platform data for the test ACPI device |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 36 | * |
| 37 | * @no_name: true to emit an empty ACPI name from testacpi_get_name() |
| 38 | * @return_error: true to return an error instead of a name |
| 39 | */ |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 40 | struct testacpi_plat { |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 41 | bool return_error; |
| 42 | bool no_name; |
| 43 | }; |
| 44 | |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 45 | /** |
| 46 | * setup_ctx_and_base_tables() - Set up context along with RSDP, RSDT and XSDT |
| 47 | * |
| 48 | * Set up the context with the given start position. Some basic tables are |
| 49 | * always needed, so set them up as well. |
| 50 | * |
| 51 | * @ctx: Context to set up |
| 52 | */ |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 53 | static int setup_ctx_and_base_tables(struct unit_test_state *uts, |
| 54 | struct acpi_ctx *ctx, ulong start) |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 55 | { |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 56 | struct acpi_writer *entry = ACPI_WRITER_GET(0base); |
| 57 | |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 58 | acpi_setup_ctx(ctx, start); |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 59 | |
| 60 | ctx->tab_start = ctx->current; |
| 61 | ut_assertok(acpi_write_one(ctx, entry)); |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 62 | |
| 63 | return 0; |
| 64 | } |
| 65 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 66 | static int testacpi_write_tables(const struct udevice *dev, |
| 67 | struct acpi_ctx *ctx) |
| 68 | { |
| 69 | struct acpi_dmar *dmar; |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 70 | int ret; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 71 | |
| 72 | dmar = (struct acpi_dmar *)ctx->current; |
| 73 | acpi_create_dmar(dmar, DMAR_INTR_REMAP); |
| 74 | ctx->current += sizeof(struct acpi_dmar); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 75 | ret = acpi_add_table(ctx, dmar); |
| 76 | if (ret) |
| 77 | return log_msg_ret("add", ret); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 78 | |
| 79 | return 0; |
| 80 | } |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 81 | |
| 82 | static int testacpi_get_name(const struct udevice *dev, char *out_name) |
| 83 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 84 | struct testacpi_plat *plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 85 | |
| 86 | if (plat->return_error) |
| 87 | return -EINVAL; |
| 88 | if (plat->no_name) { |
| 89 | *out_name = '\0'; |
| 90 | return 0; |
| 91 | } |
| 92 | if (device_get_uclass_id(dev->parent) == UCLASS_TEST_ACPI) |
| 93 | return acpi_copy_name(out_name, ACPI_TEST_CHILD_NAME); |
| 94 | else |
| 95 | return acpi_copy_name(out_name, ACPI_TEST_DEV_NAME); |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 96 | } |
| 97 | |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 98 | static int testacpi_fill_ssdt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 99 | { |
| 100 | const char *data; |
| 101 | |
| 102 | data = dev_read_string(dev, "acpi-ssdt-test-data"); |
| 103 | if (data) { |
| 104 | while (*data) |
| 105 | acpigen_emit_byte(ctx, *data++); |
| 106 | } |
| 107 | |
| 108 | return 0; |
| 109 | } |
| 110 | |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 111 | static int testacpi_inject_dsdt(const struct udevice *dev, struct acpi_ctx *ctx) |
| 112 | { |
| 113 | const char *data; |
| 114 | |
| 115 | data = dev_read_string(dev, "acpi-dsdt-test-data"); |
| 116 | if (data) { |
| 117 | while (*data) |
| 118 | acpigen_emit_byte(ctx, *data++); |
| 119 | } |
| 120 | |
| 121 | return 0; |
| 122 | } |
| 123 | |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 124 | struct acpi_ops testacpi_ops = { |
| 125 | .get_name = testacpi_get_name, |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 126 | .write_tables = testacpi_write_tables, |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 127 | .fill_ssdt = testacpi_fill_ssdt, |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 128 | .inject_dsdt = testacpi_inject_dsdt, |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | static const struct udevice_id testacpi_ids[] = { |
| 132 | { .compatible = "denx,u-boot-acpi-test" }, |
| 133 | { } |
| 134 | }; |
| 135 | |
| 136 | U_BOOT_DRIVER(testacpi_drv) = { |
| 137 | .name = "testacpi_drv", |
| 138 | .of_match = testacpi_ids, |
| 139 | .id = UCLASS_TEST_ACPI, |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 140 | .bind = dm_scan_fdt_dev, |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 141 | .plat_auto = sizeof(struct testacpi_plat), |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 142 | ACPI_OPS_PTR(&testacpi_ops) |
| 143 | }; |
| 144 | |
| 145 | UCLASS_DRIVER(testacpi) = { |
| 146 | .name = "testacpi", |
| 147 | .id = UCLASS_TEST_ACPI, |
| 148 | }; |
| 149 | |
| 150 | /* Test ACPI get_name() */ |
| 151 | static int dm_test_acpi_get_name(struct unit_test_state *uts) |
| 152 | { |
| 153 | char name[ACPI_NAME_MAX]; |
Simon Glass | 9a65513 | 2020-12-16 21:20:23 -0700 | [diff] [blame] | 154 | struct udevice *dev, *dev2, *i2c, *spi, *timer, *sound; |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 155 | struct udevice *pci, *root; |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 156 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 157 | /* Test getting the name from the driver */ |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 158 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 159 | ut_assertok(acpi_get_name(dev, name)); |
| 160 | ut_asserteq_str(ACPI_TEST_DEV_NAME, name); |
| 161 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 162 | /* Test getting the name from the device tree */ |
| 163 | ut_assertok(uclass_get_device_by_name(UCLASS_TEST_FDT, "a-test", |
| 164 | &dev2)); |
| 165 | ut_assertok(acpi_get_name(dev2, name)); |
| 166 | ut_asserteq_str("GHIJ", name); |
| 167 | |
| 168 | /* Test getting the name from acpi_device_get_name() */ |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 169 | ut_assertok(uclass_first_device_err(UCLASS_I2C, &i2c)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 170 | ut_assertok(acpi_get_name(i2c, name)); |
| 171 | ut_asserteq_str("I2C0", name); |
| 172 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 173 | ut_assertok(uclass_first_device_err(UCLASS_SPI, &spi)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 174 | ut_assertok(acpi_get_name(spi, name)); |
| 175 | ut_asserteq_str("SPI0", name); |
| 176 | |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 177 | /* ACPI doesn't know about the timer */ |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 178 | ut_assertok(uclass_first_device_err(UCLASS_TIMER, &timer)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 179 | ut_asserteq(-ENOENT, acpi_get_name(timer, name)); |
| 180 | |
| 181 | /* May as well test the rest of the cases */ |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 182 | ut_assertok(uclass_first_device_err(UCLASS_SOUND, &sound)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 183 | ut_assertok(acpi_get_name(sound, name)); |
| 184 | ut_asserteq_str("HDAS", name); |
| 185 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 186 | ut_assertok(uclass_first_device_err(UCLASS_PCI, &pci)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 187 | ut_assertok(acpi_get_name(pci, name)); |
| 188 | ut_asserteq_str("PCI0", name); |
| 189 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 190 | ut_assertok(uclass_first_device_err(UCLASS_ROOT, &root)); |
Simon Glass | 0964239 | 2020-07-07 13:12:11 -0600 | [diff] [blame] | 191 | ut_assertok(acpi_get_name(root, name)); |
| 192 | ut_asserteq_str("\\_SB", name); |
| 193 | |
| 194 | /* Note that we don't have tests for acpi_name_from_id() */ |
| 195 | |
Simon Glass | 2d67fdf | 2020-04-08 16:57:34 -0600 | [diff] [blame] | 196 | return 0; |
| 197 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 198 | 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] | 199 | |
| 200 | /* Test acpi_get_table_revision() */ |
| 201 | static int dm_test_acpi_get_table_revision(struct unit_test_state *uts) |
| 202 | { |
| 203 | ut_asserteq(1, acpi_get_table_revision(ACPITAB_MCFG)); |
| 204 | ut_asserteq(2, acpi_get_table_revision(ACPITAB_RSDP)); |
| 205 | ut_asserteq(4, acpi_get_table_revision(ACPITAB_TPM2)); |
| 206 | ut_asserteq(-EINVAL, acpi_get_table_revision(ACPITAB_COUNT)); |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | DM_TEST(dm_test_acpi_get_table_revision, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 211 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 212 | |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 213 | /* Test acpi_create_dmar() */ |
| 214 | static int dm_test_acpi_create_dmar(struct unit_test_state *uts) |
| 215 | { |
| 216 | struct acpi_dmar dmar; |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 217 | struct udevice *cpu; |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 218 | |
Michal Suchanek | ac12a2f | 2022-10-12 21:57:59 +0200 | [diff] [blame] | 219 | ut_assertok(uclass_first_device_err(UCLASS_CPU, &cpu)); |
Heinrich Schuchardt | 6c2a871 | 2020-07-17 00:20:14 +0200 | [diff] [blame] | 220 | ut_assertnonnull(cpu); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 221 | ut_assertok(acpi_create_dmar(&dmar, DMAR_INTR_REMAP)); |
| 222 | ut_asserteq(DMAR_INTR_REMAP, dmar.flags); |
Marek Vasut | 795f0d7 | 2023-08-13 03:25:59 +0200 | [diff] [blame] | 223 | ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, |
| 224 | dmar.host_address_width); |
Simon Glass | e962989 | 2020-04-08 16:57:39 -0600 | [diff] [blame] | 225 | |
| 226 | return 0; |
| 227 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 228 | 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] | 229 | |
| 230 | /* Test acpi_fill_header() */ |
| 231 | static int dm_test_acpi_fill_header(struct unit_test_state *uts) |
| 232 | { |
| 233 | struct acpi_table_header hdr; |
| 234 | |
| 235 | /* Make sure these 5 fields are not changed */ |
| 236 | hdr.length = 0x11; |
| 237 | hdr.revision = 0x22; |
| 238 | hdr.checksum = 0x33; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 239 | acpi_fill_header(&hdr, "ABCD"); |
| 240 | |
| 241 | ut_asserteq_mem("ABCD", hdr.signature, sizeof(hdr.signature)); |
| 242 | ut_asserteq(0x11, hdr.length); |
| 243 | ut_asserteq(0x22, hdr.revision); |
| 244 | ut_asserteq(0x33, hdr.checksum); |
| 245 | ut_asserteq_mem(OEM_ID, hdr.oem_id, sizeof(hdr.oem_id)); |
| 246 | ut_asserteq_mem(OEM_TABLE_ID, hdr.oem_table_id, |
| 247 | sizeof(hdr.oem_table_id)); |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 248 | ut_asserteq(OEM_REVISION, hdr.oem_revision); |
Heinrich Schuchardt | 10de8a8 | 2024-01-21 12:52:48 +0100 | [diff] [blame] | 249 | ut_asserteq_mem(ASLC_ID, hdr.creator_id, sizeof(hdr.creator_id)); |
Heinrich Schuchardt | 9838d34 | 2024-04-18 05:11:13 +0200 | [diff] [blame^] | 250 | ut_asserteq(ASL_REVISION, hdr.creator_revision); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 251 | |
| 252 | return 0; |
| 253 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 254 | 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] | 255 | |
| 256 | /* Test ACPI write_tables() */ |
| 257 | static int dm_test_acpi_write_tables(struct unit_test_state *uts) |
| 258 | { |
| 259 | struct acpi_dmar *dmar; |
| 260 | struct acpi_ctx ctx; |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 261 | ulong addr; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 262 | void *buf; |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 263 | int i; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 264 | |
| 265 | buf = malloc(BUF_SIZE); |
| 266 | ut_assertnonnull(buf); |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 267 | addr = map_to_sysmem(buf); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 268 | |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 269 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr)); |
Simon Glass | 575a547 | 2020-04-26 09:19:50 -0600 | [diff] [blame] | 270 | dmar = ctx.current; |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 271 | ut_assertok(acpi_write_dev_tables(&ctx)); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 272 | |
| 273 | /* |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 274 | * We should have three dmar tables, one for each |
| 275 | * "denx,u-boot-acpi-test" device |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 276 | */ |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 277 | ut_asserteq_ptr(dmar + 3, ctx.current); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 278 | ut_asserteq(DMAR_INTR_REMAP, dmar->flags); |
Marek Vasut | 795f0d7 | 2023-08-13 03:25:59 +0200 | [diff] [blame] | 279 | ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, |
| 280 | dmar->host_address_width); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 281 | |
| 282 | ut_asserteq(DMAR_INTR_REMAP, dmar[1].flags); |
Marek Vasut | 795f0d7 | 2023-08-13 03:25:59 +0200 | [diff] [blame] | 283 | ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, |
| 284 | dmar[1].host_address_width); |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 285 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 286 | ut_asserteq(DMAR_INTR_REMAP, dmar[2].flags); |
Marek Vasut | 795f0d7 | 2023-08-13 03:25:59 +0200 | [diff] [blame] | 287 | ut_asserteq((IS_ENABLED(CONFIG_PHYS_64BIT) ? 64 : 32) - 1, |
| 288 | dmar[2].host_address_width); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 289 | |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 290 | /* Check that the pointers were added correctly */ |
| 291 | for (i = 0; i < 3; i++) { |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 292 | ut_asserteq(nomap_to_sysmem(dmar + i), ctx.rsdt->entry[i]); |
| 293 | ut_asserteq(nomap_to_sysmem(dmar + i), ctx.xsdt->entry[i]); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 294 | } |
| 295 | ut_asserteq(0, ctx.rsdt->entry[3]); |
| 296 | ut_asserteq(0, ctx.xsdt->entry[3]); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 297 | |
Simon Glass | 17968c3 | 2020-04-26 09:19:46 -0600 | [diff] [blame] | 298 | return 0; |
| 299 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 300 | 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] | 301 | |
| 302 | /* Test basic ACPI functions */ |
| 303 | static int dm_test_acpi_basic(struct unit_test_state *uts) |
| 304 | { |
| 305 | struct acpi_ctx ctx; |
| 306 | |
| 307 | /* Check align works */ |
| 308 | ctx.current = (void *)5; |
| 309 | acpi_align(&ctx); |
| 310 | ut_asserteq_ptr((void *)16, ctx.current); |
| 311 | |
| 312 | /* Check that align does nothing if already aligned */ |
| 313 | acpi_align(&ctx); |
| 314 | ut_asserteq_ptr((void *)16, ctx.current); |
| 315 | acpi_align64(&ctx); |
| 316 | ut_asserteq_ptr((void *)64, ctx.current); |
| 317 | acpi_align64(&ctx); |
| 318 | ut_asserteq_ptr((void *)64, ctx.current); |
| 319 | |
| 320 | /* Check incrementing */ |
| 321 | acpi_inc(&ctx, 3); |
| 322 | ut_asserteq_ptr((void *)67, ctx.current); |
| 323 | acpi_inc_align(&ctx, 3); |
| 324 | ut_asserteq_ptr((void *)80, ctx.current); |
| 325 | |
| 326 | return 0; |
| 327 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 328 | 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] | 329 | |
Simon Glass | 1617088 | 2021-12-01 09:02:49 -0700 | [diff] [blame] | 330 | /* Test setup_ctx_and_base_tables */ |
Simon Glass | 8181e18 | 2023-12-31 08:25:53 -0700 | [diff] [blame] | 331 | static int dm_test_acpi_ctx_and_base_tables(struct unit_test_state *uts) |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 332 | { |
| 333 | struct acpi_rsdp *rsdp; |
| 334 | struct acpi_rsdt *rsdt; |
| 335 | struct acpi_xsdt *xsdt; |
| 336 | struct acpi_ctx ctx; |
| 337 | void *buf, *end; |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 338 | ulong addr; |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 339 | |
| 340 | /* |
| 341 | * Use an unaligned address deliberately, by allocating an aligned |
| 342 | * address and then adding 4 to it |
| 343 | */ |
| 344 | buf = memalign(64, BUF_SIZE); |
| 345 | ut_assertnonnull(buf); |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 346 | addr = map_to_sysmem(buf); |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 347 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr + 4)); |
Simon Glass | b33f2f9 | 2021-12-01 09:02:37 -0700 | [diff] [blame] | 348 | ut_asserteq(map_to_sysmem(PTR_ALIGN(buf + 4, 16)), gd_acpi_start()); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 349 | |
| 350 | rsdp = buf + 16; |
| 351 | ut_asserteq_ptr(rsdp, ctx.rsdp); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 352 | ut_asserteq_mem(RSDP_SIG, rsdp->signature, sizeof(rsdp->signature)); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 353 | ut_asserteq(sizeof(*rsdp), rsdp->length); |
| 354 | ut_assertok(table_compute_checksum(rsdp, 20)); |
| 355 | ut_assertok(table_compute_checksum(rsdp, sizeof(*rsdp))); |
| 356 | |
| 357 | rsdt = PTR_ALIGN((void *)rsdp + sizeof(*rsdp), 16); |
| 358 | ut_asserteq_ptr(rsdt, ctx.rsdt); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 359 | ut_asserteq_mem("RSDT", rsdt->header.signature, ACPI_NAME_LEN); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 360 | ut_asserteq(sizeof(*rsdt), rsdt->header.length); |
| 361 | ut_assertok(table_compute_checksum(rsdt, sizeof(*rsdt))); |
| 362 | |
| 363 | xsdt = PTR_ALIGN((void *)rsdt + sizeof(*rsdt), 16); |
Simon Glass | abeaca8 | 2020-04-26 09:19:52 -0600 | [diff] [blame] | 364 | ut_asserteq_ptr(xsdt, ctx.xsdt); |
Simon Glass | a3186e6 | 2020-05-10 12:52:45 -0600 | [diff] [blame] | 365 | ut_asserteq_mem("XSDT", xsdt->header.signature, ACPI_NAME_LEN); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 366 | ut_asserteq(sizeof(*xsdt), xsdt->header.length); |
| 367 | ut_assertok(table_compute_checksum(xsdt, sizeof(*xsdt))); |
| 368 | |
| 369 | end = PTR_ALIGN((void *)xsdt + sizeof(*xsdt), 64); |
| 370 | ut_asserteq_ptr(end, ctx.current); |
| 371 | |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 372 | ut_asserteq(nomap_to_sysmem(rsdt), rsdp->rsdt_address); |
| 373 | ut_asserteq(nomap_to_sysmem(xsdt), rsdp->xsdt_address); |
Simon Glass | 9c442a6 | 2020-04-26 09:19:51 -0600 | [diff] [blame] | 374 | |
| 375 | return 0; |
| 376 | } |
Simon Glass | 8181e18 | 2023-12-31 08:25:53 -0700 | [diff] [blame] | 377 | DM_TEST(dm_test_acpi_ctx_and_base_tables, |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 378 | UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 379 | |
| 380 | /* Test 'acpi list' command */ |
| 381 | static int dm_test_acpi_cmd_list(struct unit_test_state *uts) |
| 382 | { |
| 383 | struct acpi_ctx ctx; |
| 384 | ulong addr; |
| 385 | void *buf; |
| 386 | |
| 387 | buf = memalign(16, BUF_SIZE); |
| 388 | ut_assertnonnull(buf); |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 389 | addr = map_to_sysmem(buf); |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 390 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr)); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 391 | |
| 392 | ut_assertok(acpi_write_dev_tables(&ctx)); |
| 393 | |
| 394 | console_record_reset(); |
| 395 | run_command("acpi list", 0); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 396 | ut_assert_nextline("Name Base Size Detail"); |
| 397 | ut_assert_nextline("---- ---------------- ----- ----------------------------"); |
| 398 | ut_assert_nextline("RSDP %16lx %5zx v02 U-BOOT", addr, |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 399 | sizeof(struct acpi_rsdp)); |
| 400 | addr = ALIGN(addr + sizeof(struct acpi_rsdp), 16); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 401 | ut_assert_nextline("RSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0", |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 402 | addr, sizeof(struct acpi_table_header) + |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 403 | 3 * sizeof(u32), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 404 | addr = ALIGN(addr + sizeof(struct acpi_rsdt), 16); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 405 | ut_assert_nextline("XSDT %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0", |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 406 | addr, sizeof(struct acpi_table_header) + |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 407 | 3 * sizeof(u64), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 408 | addr = ALIGN(addr + sizeof(struct acpi_xsdt), 64); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 409 | ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 410 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 411 | addr = ALIGN(addr + sizeof(struct acpi_dmar), 16); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 412 | ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 413 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 414 | addr = ALIGN(addr + sizeof(struct acpi_dmar), 16); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 415 | ut_assert_nextline("DMAR %16lx %5zx v01 U-BOOT U-BOOTBL %x INTL 0", |
Pali Rohár | f0de20e | 2021-07-10 13:10:01 +0200 | [diff] [blame] | 416 | addr, sizeof(struct acpi_dmar), OEM_REVISION); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 417 | ut_assert_console_end(); |
| 418 | |
| 419 | return 0; |
| 420 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 421 | 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] | 422 | |
| 423 | /* Test 'acpi dump' command */ |
| 424 | static int dm_test_acpi_cmd_dump(struct unit_test_state *uts) |
| 425 | { |
| 426 | struct acpi_ctx ctx; |
| 427 | ulong addr; |
| 428 | void *buf; |
| 429 | |
| 430 | buf = memalign(16, BUF_SIZE); |
| 431 | ut_assertnonnull(buf); |
Simon Glass | 37acc66 | 2021-12-01 09:02:48 -0700 | [diff] [blame] | 432 | addr = map_to_sysmem(buf); |
Simon Glass | e9f6e6a | 2021-12-01 09:02:50 -0700 | [diff] [blame] | 433 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr)); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 434 | |
| 435 | ut_assertok(acpi_write_dev_tables(&ctx)); |
| 436 | |
| 437 | /* First search for a non-existent table */ |
| 438 | console_record_reset(); |
| 439 | run_command("acpi dump rdst", 0); |
| 440 | ut_assert_nextline("Table 'RDST' not found"); |
| 441 | ut_assert_console_end(); |
| 442 | |
| 443 | /* Now a real table */ |
| 444 | console_record_reset(); |
| 445 | run_command("acpi dump dmar", 0); |
Simon Glass | 919f835 | 2023-12-31 08:25:54 -0700 | [diff] [blame] | 446 | addr = ALIGN(nomap_to_sysmem(ctx.xsdt) + sizeof(struct acpi_xsdt), 64); |
Heinrich Schuchardt | 27e9ebb | 2023-11-21 15:41:28 +0100 | [diff] [blame] | 447 | ut_assert_nextline("DMAR @ %16lx", addr); |
Simon Glass | faf08c7 | 2020-04-26 09:19:53 -0600 | [diff] [blame] | 448 | ut_assert_nextlines_are_dump(0x30); |
| 449 | ut_assert_console_end(); |
| 450 | |
| 451 | return 0; |
| 452 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 453 | 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] | 454 | |
| 455 | /* Test acpi_device_path() */ |
| 456 | static int dm_test_acpi_device_path(struct unit_test_state *uts) |
| 457 | { |
Simon Glass | b75b15b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 458 | struct testacpi_plat *plat; |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 459 | char buf[ACPI_PATH_MAX]; |
| 460 | struct udevice *dev, *child; |
| 461 | |
| 462 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 463 | ut_assertok(acpi_device_path(dev, buf, sizeof(buf))); |
| 464 | ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME, buf); |
| 465 | |
| 466 | /* Test running out of space */ |
| 467 | buf[5] = '\0'; |
| 468 | ut_asserteq(-ENOSPC, acpi_device_path(dev, buf, 5)); |
| 469 | ut_asserteq('\0', buf[5]); |
| 470 | |
| 471 | /* Test a three-component name */ |
| 472 | ut_assertok(device_first_child_err(dev, &child)); |
| 473 | ut_assertok(acpi_device_path(child, buf, sizeof(buf))); |
| 474 | ut_asserteq_str("\\_SB." ACPI_TEST_DEV_NAME "." ACPI_TEST_CHILD_NAME, |
| 475 | buf); |
| 476 | |
| 477 | /* Test handling of a device which doesn't produce a name */ |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 478 | plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 479 | plat->no_name = true; |
| 480 | ut_assertok(acpi_device_path(child, buf, sizeof(buf))); |
| 481 | ut_asserteq_str("\\_SB." ACPI_TEST_CHILD_NAME, buf); |
| 482 | |
| 483 | /* Test handling of a device which returns an error */ |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 484 | plat = dev_get_plat(dev); |
Simon Glass | ebb2e83 | 2020-07-07 13:11:39 -0600 | [diff] [blame] | 485 | plat->return_error = true; |
| 486 | ut_asserteq(-EINVAL, acpi_device_path(child, buf, sizeof(buf))); |
| 487 | |
| 488 | return 0; |
| 489 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 490 | 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] | 491 | |
| 492 | /* Test acpi_device_status() */ |
| 493 | static int dm_test_acpi_device_status(struct unit_test_state *uts) |
| 494 | { |
| 495 | struct udevice *dev; |
| 496 | |
| 497 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 498 | ut_asserteq(ACPI_DSTATUS_ALL_ON, acpi_device_status(dev)); |
| 499 | |
| 500 | return 0; |
| 501 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 502 | 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] | 503 | |
| 504 | /* Test acpi_fill_ssdt() */ |
| 505 | static int dm_test_acpi_fill_ssdt(struct unit_test_state *uts) |
| 506 | { |
| 507 | struct acpi_ctx ctx; |
| 508 | u8 *buf; |
| 509 | |
| 510 | buf = malloc(BUF_SIZE); |
| 511 | ut_assertnonnull(buf); |
| 512 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 513 | acpi_reset_items(); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 514 | ctx.current = buf; |
| 515 | buf[4] = 'z'; /* sentinel */ |
| 516 | ut_assertok(acpi_fill_ssdt(&ctx)); |
| 517 | |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 518 | /* |
| 519 | * These values come from acpi-test2's acpi-ssdt-test-data property. |
| 520 | * This device comes first because of u-boot,acpi-ssdt-order |
| 521 | */ |
| 522 | ut_asserteq('c', buf[0]); |
| 523 | ut_asserteq('d', buf[1]); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 524 | |
Simon Glass | 3c601b1 | 2020-07-07 13:12:06 -0600 | [diff] [blame] | 525 | /* These values come from acpi-test's acpi-ssdt-test-data property */ |
| 526 | ut_asserteq('a', buf[2]); |
| 527 | ut_asserteq('b', buf[3]); |
Simon Glass | d43e0ba | 2020-07-07 13:12:03 -0600 | [diff] [blame] | 528 | |
| 529 | ut_asserteq('z', buf[4]); |
| 530 | |
| 531 | return 0; |
| 532 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 533 | 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] | 534 | |
| 535 | /* Test acpi_inject_dsdt() */ |
| 536 | static int dm_test_acpi_inject_dsdt(struct unit_test_state *uts) |
| 537 | { |
| 538 | struct acpi_ctx ctx; |
| 539 | u8 *buf; |
| 540 | |
| 541 | buf = malloc(BUF_SIZE); |
| 542 | ut_assertnonnull(buf); |
| 543 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 544 | acpi_reset_items(); |
Simon Glass | 990cd5b | 2020-07-07 13:12:08 -0600 | [diff] [blame] | 545 | ctx.current = buf; |
| 546 | buf[4] = 'z'; /* sentinel */ |
| 547 | ut_assertok(acpi_inject_dsdt(&ctx)); |
| 548 | |
| 549 | /* |
| 550 | * These values come from acpi-test's acpi-dsdt-test-data property. |
| 551 | * There is no u-boot,acpi-dsdt-order so device-tree order is used. |
| 552 | */ |
| 553 | ut_asserteq('h', buf[0]); |
| 554 | ut_asserteq('i', buf[1]); |
| 555 | |
| 556 | /* These values come from acpi-test's acpi-dsdt-test-data property */ |
| 557 | ut_asserteq('j', buf[2]); |
| 558 | ut_asserteq('k', buf[3]); |
| 559 | |
| 560 | ut_asserteq('z', buf[4]); |
| 561 | |
| 562 | return 0; |
| 563 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 564 | 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] | 565 | |
| 566 | /* Test 'acpi items' command */ |
| 567 | static int dm_test_acpi_cmd_items(struct unit_test_state *uts) |
| 568 | { |
| 569 | struct acpi_ctx ctx; |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 570 | ulong addr; |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 571 | void *buf; |
| 572 | |
| 573 | buf = malloc(BUF_SIZE); |
| 574 | ut_assertnonnull(buf); |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 575 | addr = map_to_sysmem(buf); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 576 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 577 | acpi_reset_items(); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 578 | ctx.current = buf; |
| 579 | ut_assertok(acpi_fill_ssdt(&ctx)); |
| 580 | console_record_reset(); |
| 581 | run_command("acpi items", 0); |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 582 | ut_assert_nextline("Seq Type Base Size Device/Writer"); |
| 583 | ut_assert_nextline("--- ----- -------- ---- -------------"); |
| 584 | ut_assert_nextline(" 0 ssdt %8lx 2 acpi-test", addr); |
| 585 | ut_assert_nextline(" 1 ssdt %8lx 2 acpi-test2", addr + 2); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 586 | ut_assert_console_end(); |
| 587 | |
Simon Glass | a385d49 | 2020-11-04 09:57:33 -0700 | [diff] [blame] | 588 | acpi_reset_items(); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 589 | ctx.current = buf; |
| 590 | ut_assertok(acpi_inject_dsdt(&ctx)); |
| 591 | console_record_reset(); |
| 592 | run_command("acpi items", 0); |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 593 | ut_assert_nextlinen("Seq"); |
| 594 | ut_assert_nextlinen("---"); |
| 595 | ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr); |
| 596 | ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 597 | ut_assert_console_end(); |
| 598 | |
| 599 | console_record_reset(); |
| 600 | run_command("acpi items -d", 0); |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 601 | ut_assert_nextlinen("Seq"); |
| 602 | ut_assert_nextlinen("---"); |
| 603 | ut_assert_nextline(" 0 dsdt %8lx 2 acpi-test", addr); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 604 | ut_assert_nextlines_are_dump(2); |
| 605 | ut_assert_nextline("%s", ""); |
Simon Glass | 884dcfa | 2021-12-01 09:03:05 -0700 | [diff] [blame] | 606 | ut_assert_nextline(" 1 dsdt %8lx 2 acpi-test2", addr + 2); |
Simon Glass | 64bbc0d | 2020-07-07 13:12:12 -0600 | [diff] [blame] | 607 | ut_assert_nextlines_are_dump(2); |
| 608 | ut_assert_nextline("%s", ""); |
| 609 | ut_assert_console_end(); |
| 610 | |
| 611 | return 0; |
| 612 | } |
Simon Glass | 974dccd | 2020-07-28 19:41:12 -0600 | [diff] [blame] | 613 | DM_TEST(dm_test_acpi_cmd_items, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Simon Glass | 2b94342 | 2023-07-15 21:38:45 -0600 | [diff] [blame] | 614 | |
| 615 | /* Test 'acpi set' command */ |
| 616 | static int dm_test_acpi_cmd_set(struct unit_test_state *uts) |
| 617 | { |
| 618 | struct acpi_ctx ctx; |
| 619 | ulong addr; |
| 620 | void *buf; |
| 621 | |
| 622 | gd_set_acpi_start(0); |
| 623 | |
| 624 | console_record_reset(); |
| 625 | ut_asserteq(0, gd_acpi_start()); |
| 626 | ut_assertok(run_command("acpi set", 0)); |
| 627 | ut_assert_nextline("ACPI pointer: 0"); |
| 628 | |
| 629 | buf = memalign(16, BUF_SIZE); |
| 630 | ut_assertnonnull(buf); |
| 631 | addr = map_to_sysmem(buf); |
| 632 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr)); |
| 633 | |
| 634 | ut_assertok(acpi_write_dev_tables(&ctx)); |
| 635 | |
| 636 | ut_assertok(run_command("acpi set", 0)); |
| 637 | ut_assert_nextline("ACPI pointer: %lx", addr); |
| 638 | |
| 639 | ut_assertok(run_command("acpi set 0", 0)); |
| 640 | ut_assert_nextline("Setting ACPI pointer to 0"); |
| 641 | ut_asserteq(0, gd_acpi_start()); |
| 642 | |
| 643 | ut_assertok(run_commandf("acpi set %lx", addr)); |
| 644 | ut_assert_nextline("Setting ACPI pointer to %lx", addr); |
| 645 | ut_asserteq(addr, gd_acpi_start()); |
| 646 | |
| 647 | ut_assert_console_end(); |
| 648 | |
| 649 | return 0; |
| 650 | } |
| 651 | DM_TEST(dm_test_acpi_cmd_set, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT); |
Heinrich Schuchardt | edf77e6 | 2023-11-18 22:57:27 +0100 | [diff] [blame] | 652 | |
| 653 | /** |
| 654 | * dm_test_write_test_table() - create test ACPI table |
| 655 | * |
| 656 | * Create an ACPI table TSTn, where n is given by @index. |
| 657 | * |
| 658 | * @ctx: ACPI table writing context |
| 659 | * @index: table index |
| 660 | * Return: generated table |
| 661 | */ |
| 662 | static struct acpi_table_header |
| 663 | *dm_test_write_test_table(struct acpi_ctx *ctx, int index) |
| 664 | { |
| 665 | struct acpi_table_header *tbl = ctx->current; |
| 666 | char signature[5]; |
| 667 | |
| 668 | snprintf(signature, sizeof(signature), "TST%1d", index); |
| 669 | memset(tbl, 0, sizeof(*tbl)); |
| 670 | acpi_fill_header(tbl, signature); |
| 671 | acpi_inc(ctx, sizeof(struct acpi_table_header)); |
| 672 | tbl->length = (u8 *)ctx->current - (u8 *)tbl; |
| 673 | tbl->checksum = table_compute_checksum(tbl, tbl->length); |
| 674 | acpi_add_table(ctx, tbl); |
| 675 | |
| 676 | return tbl; |
| 677 | } |
| 678 | |
| 679 | /* Test acpi_find_table() */ |
| 680 | static int dm_test_acpi_find_table(struct unit_test_state *uts) |
| 681 | { |
| 682 | struct acpi_ctx ctx; |
| 683 | ulong acpi_start, addr; |
| 684 | void *buf; |
| 685 | struct acpi_table_header *table, *table1, *table2, *table3; |
| 686 | struct acpi_rsdp *rsdp; |
| 687 | ulong rsdt; |
| 688 | ulong xsdt; |
| 689 | |
| 690 | /* Keep reference to original ACPI tables */ |
| 691 | acpi_start = gd_acpi_start(); |
| 692 | |
| 693 | /* Setup new ACPI tables */ |
| 694 | buf = memalign(16, BUF_SIZE); |
| 695 | ut_assertnonnull(buf); |
| 696 | addr = map_to_sysmem(buf); |
| 697 | ut_assertok(setup_ctx_and_base_tables(uts, &ctx, addr)); |
| 698 | table3 = dm_test_write_test_table(&ctx, 3); |
| 699 | table1 = dm_test_write_test_table(&ctx, 1); |
| 700 | table2 = dm_test_write_test_table(&ctx, 2); |
| 701 | |
| 702 | /* Retrieve RSDP, RSDT, XSDT */ |
| 703 | rsdp = map_sysmem(gd_acpi_start(), 0); |
| 704 | ut_assertnonnull(rsdp); |
| 705 | rsdt = rsdp->rsdt_address; |
| 706 | ut_assert(rsdt); |
| 707 | xsdt = rsdp->xsdt_address; |
| 708 | ut_assert(xsdt); |
| 709 | |
| 710 | /* Find with both RSDT and XSDT */ |
| 711 | table = acpi_find_table("TST1"); |
| 712 | ut_asserteq_ptr(table1, table); |
| 713 | ut_asserteq_strn("TST1", table->signature); |
| 714 | table = acpi_find_table("TST2"); |
| 715 | ut_asserteq_ptr(table2, table); |
| 716 | ut_asserteq_strn("TST2", table->signature); |
| 717 | table = acpi_find_table("TST3"); |
| 718 | ut_asserteq_ptr(table3, table); |
| 719 | ut_asserteq_strn("TST3", table->signature); |
| 720 | |
| 721 | /* Find with XSDT only */ |
| 722 | rsdp->rsdt_address = 0; |
| 723 | table = acpi_find_table("TST1"); |
| 724 | ut_asserteq_ptr(table1, table); |
| 725 | table = acpi_find_table("TST2"); |
| 726 | ut_asserteq_ptr(table2, table); |
| 727 | table = acpi_find_table("TST3"); |
| 728 | ut_asserteq_ptr(table3, table); |
| 729 | rsdp->rsdt_address = rsdt; |
| 730 | |
| 731 | /* Find with RSDT only */ |
| 732 | rsdp->xsdt_address = 0; |
| 733 | table = acpi_find_table("TST1"); |
| 734 | ut_asserteq_ptr(table1, table); |
| 735 | table = acpi_find_table("TST2"); |
| 736 | ut_asserteq_ptr(table2, table); |
| 737 | table = acpi_find_table("TST3"); |
| 738 | ut_asserteq_ptr(table3, table); |
| 739 | rsdp->xsdt_address = xsdt; |
| 740 | |
| 741 | /* Restore previous ACPI tables */ |
| 742 | gd_set_acpi_start(acpi_start); |
| 743 | free(buf); |
| 744 | |
| 745 | return 0; |
| 746 | } |
| 747 | DM_TEST(dm_test_acpi_find_table, 0); |
Heinrich Schuchardt | bb307a6 | 2023-11-21 15:41:27 +0100 | [diff] [blame] | 748 | |
| 749 | /* Test offsets in RSDT, XSDT */ |
| 750 | static int dm_test_acpi_offsets(struct unit_test_state *uts) |
| 751 | { |
| 752 | ut_asserteq(36, offsetof(struct acpi_rsdt, entry)); |
| 753 | ut_asserteq(36, offsetof(struct acpi_xsdt, entry)); |
| 754 | |
| 755 | return 0; |
| 756 | } |
| 757 | DM_TEST(dm_test_acpi_offsets, 0); |