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