Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * Tests for ACPI code generation |
| 4 | * |
| 5 | * Copyright 2019 Google LLC |
| 6 | * Written by Simon Glass <sjg@chromium.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
| 10 | #include <dm.h> |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 11 | #include <irq.h> |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 12 | #include <malloc.h> |
| 13 | #include <acpi/acpigen.h> |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 14 | #include <acpi/acpi_device.h> |
Simon Glass | a9e0a07 | 2020-07-07 13:11:46 -0600 | [diff] [blame] | 15 | #include <asm/gpio.h> |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
| 17 | #include <dm/acpi.h> |
| 18 | #include <dm/test.h> |
Simon Glass | 4ebc940 | 2020-07-07 13:11:47 -0600 | [diff] [blame] | 19 | #include <dm/uclass-internal.h> |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 20 | #include <test/ut.h> |
| 21 | |
| 22 | /* Maximum size of the ACPI context needed for most tests */ |
| 23 | #define ACPI_CONTEXT_SIZE 150 |
| 24 | |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 25 | #define TEST_STRING "frogmore" |
Simon Glass | 3df33bd | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 26 | #define TEST_STRING2 "ranch" |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 27 | #define TEST_STREAM2 "\xfa\xde" |
| 28 | |
Simon Glass | 83b2bd5 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 29 | #define TEST_INT8 0x7d |
| 30 | #define TEST_INT16 0x2345 |
| 31 | #define TEST_INT32 0x12345678 |
| 32 | #define TEST_INT64 0x4567890123456 |
| 33 | |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 34 | static int alloc_context_size(struct acpi_ctx **ctxp, int size) |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 35 | { |
| 36 | struct acpi_ctx *ctx; |
| 37 | |
| 38 | *ctxp = NULL; |
| 39 | ctx = malloc(sizeof(*ctx)); |
| 40 | if (!ctx) |
| 41 | return -ENOMEM; |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 42 | ctx->base = malloc(size); |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 43 | if (!ctx->base) { |
| 44 | free(ctx); |
| 45 | return -ENOMEM; |
| 46 | } |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 47 | ctx->ltop = 0; |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 48 | ctx->current = ctx->base; |
| 49 | *ctxp = ctx; |
| 50 | |
| 51 | return 0; |
| 52 | } |
| 53 | |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 54 | static int alloc_context(struct acpi_ctx **ctxp) |
| 55 | { |
| 56 | return alloc_context_size(ctxp, ACPI_CONTEXT_SIZE); |
| 57 | } |
| 58 | |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 59 | static void free_context(struct acpi_ctx **ctxp) |
| 60 | { |
| 61 | free((*ctxp)->base); |
| 62 | free(*ctxp); |
| 63 | *ctxp = NULL; |
| 64 | } |
| 65 | |
| 66 | /* Test emitting simple types and acpigen_get_current() */ |
| 67 | static int dm_test_acpi_emit_simple(struct unit_test_state *uts) |
| 68 | { |
| 69 | struct acpi_ctx *ctx; |
| 70 | u8 *ptr; |
| 71 | |
| 72 | ut_assertok(alloc_context(&ctx)); |
| 73 | |
| 74 | ptr = acpigen_get_current(ctx); |
| 75 | acpigen_emit_byte(ctx, 0x23); |
| 76 | ut_asserteq(1, acpigen_get_current(ctx) - ptr); |
| 77 | ut_asserteq(0x23, *(u8 *)ptr); |
| 78 | |
| 79 | acpigen_emit_word(ctx, 0x1234); |
| 80 | ut_asserteq(3, acpigen_get_current(ctx) - ptr); |
| 81 | ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1))); |
| 82 | |
| 83 | acpigen_emit_dword(ctx, 0x87654321); |
| 84 | ut_asserteq(7, acpigen_get_current(ctx) - ptr); |
| 85 | ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3))); |
| 86 | |
| 87 | free_context(&ctx); |
| 88 | |
| 89 | return 0; |
| 90 | } |
| 91 | DM_TEST(dm_test_acpi_emit_simple, 0); |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 92 | |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 93 | /* Test emitting a stream */ |
| 94 | static int dm_test_acpi_emit_stream(struct unit_test_state *uts) |
| 95 | { |
| 96 | struct acpi_ctx *ctx; |
| 97 | u8 *ptr; |
| 98 | |
| 99 | ut_assertok(alloc_context(&ctx)); |
| 100 | |
| 101 | ptr = acpigen_get_current(ctx); |
| 102 | acpigen_emit_stream(ctx, TEST_STREAM2, 2); |
| 103 | ut_asserteq(2, acpigen_get_current(ctx) - ptr); |
| 104 | ut_asserteq((u8)TEST_STREAM2[0], ptr[0]); |
| 105 | ut_asserteq((u8)TEST_STREAM2[1], ptr[1]); |
| 106 | |
| 107 | free_context(&ctx); |
| 108 | |
| 109 | return 0; |
| 110 | } |
| 111 | DM_TEST(dm_test_acpi_emit_stream, 0); |
| 112 | |
| 113 | /* Test emitting a string */ |
| 114 | static int dm_test_acpi_emit_string(struct unit_test_state *uts) |
| 115 | { |
| 116 | struct acpi_ctx *ctx; |
| 117 | u8 *ptr; |
| 118 | |
| 119 | ut_assertok(alloc_context(&ctx)); |
| 120 | |
| 121 | ptr = acpigen_get_current(ctx); |
| 122 | acpigen_emit_string(ctx, TEST_STRING); |
| 123 | ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr); |
| 124 | ut_asserteq_str(TEST_STRING, (char *)ptr); |
| 125 | |
| 126 | free_context(&ctx); |
| 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | DM_TEST(dm_test_acpi_emit_string, 0); |
| 131 | |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 132 | /* Test emitting an interrupt descriptor */ |
| 133 | static int dm_test_acpi_interrupt(struct unit_test_state *uts) |
| 134 | { |
| 135 | struct acpi_ctx *ctx; |
| 136 | struct udevice *dev; |
| 137 | struct irq irq; |
| 138 | u8 *ptr; |
| 139 | |
| 140 | ut_assertok(alloc_context(&ctx)); |
| 141 | |
| 142 | ptr = acpigen_get_current(ctx); |
| 143 | |
| 144 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 145 | ut_assertok(irq_get_by_index(dev, 0, &irq)); |
| 146 | |
| 147 | /* See a-test, property interrupts-extended in the device tree */ |
| 148 | ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq)); |
| 149 | ut_asserteq(9, acpigen_get_current(ctx) - ptr); |
| 150 | ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]); |
| 151 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 1))); |
| 152 | ut_asserteq(0x19, ptr[3]); |
| 153 | ut_asserteq(1, ptr[4]); |
| 154 | ut_asserteq(3, get_unaligned((u32 *)(ptr + 5))); |
| 155 | |
| 156 | free_context(&ctx); |
| 157 | |
| 158 | return 0; |
| 159 | } |
| 160 | DM_TEST(dm_test_acpi_interrupt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | a9e0a07 | 2020-07-07 13:11:46 -0600 | [diff] [blame] | 161 | |
| 162 | /* Test emitting a GPIO descriptor */ |
| 163 | static int dm_test_acpi_gpio(struct unit_test_state *uts) |
| 164 | { |
| 165 | struct gpio_desc desc; |
| 166 | struct acpi_ctx *ctx; |
| 167 | struct udevice *dev; |
| 168 | u8 *ptr; |
| 169 | |
| 170 | ut_assertok(alloc_context(&ctx)); |
| 171 | |
| 172 | ptr = acpigen_get_current(ctx); |
| 173 | |
| 174 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 175 | ut_asserteq_str("a-test", dev->name); |
| 176 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 177 | |
| 178 | /* This should write GPIO pin 4 (see device tree test.dts ) */ |
| 179 | ut_asserteq(4, acpi_device_write_gpio_desc(ctx, &desc)); |
| 180 | ut_asserteq(35, acpigen_get_current(ctx) - ptr); |
| 181 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 182 | ut_asserteq(32, get_unaligned((u16 *)(ptr + 1))); |
| 183 | ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]); |
| 184 | ut_asserteq(ACPI_GPIO_TYPE_IO, ptr[4]); |
| 185 | ut_asserteq(1, get_unaligned((u16 *)(ptr + 5))); |
| 186 | ut_asserteq(9, get_unaligned((u16 *)(ptr + 7))); |
| 187 | ut_asserteq(ACPI_GPIO_PULL_UP, ptr[9]); |
| 188 | ut_asserteq(1234, get_unaligned((u16 *)(ptr + 10))); |
| 189 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 12))); |
| 190 | ut_asserteq(23, get_unaligned((u16 *)(ptr + 14))); |
| 191 | ut_asserteq(0, ptr[16]); |
| 192 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 17))); |
| 193 | ut_asserteq(35, get_unaligned((u16 *)(ptr + 19))); |
| 194 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 21))); |
| 195 | |
| 196 | /* pin0 */ |
| 197 | ut_asserteq(4, get_unaligned((u16 *)(ptr + 23))); |
| 198 | |
| 199 | ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25); |
| 200 | |
| 201 | free_context(&ctx); |
| 202 | |
| 203 | return 0; |
| 204 | } |
| 205 | DM_TEST(dm_test_acpi_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 206 | |
| 207 | /* Test emitting a GPIO descriptor with an interrupt */ |
| 208 | static int dm_test_acpi_gpio_irq(struct unit_test_state *uts) |
| 209 | { |
| 210 | struct gpio_desc desc; |
| 211 | struct acpi_ctx *ctx; |
| 212 | struct udevice *dev; |
| 213 | u8 *ptr; |
| 214 | |
| 215 | ut_assertok(alloc_context(&ctx)); |
| 216 | |
| 217 | ptr = acpigen_get_current(ctx); |
| 218 | |
| 219 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 220 | ut_asserteq_str("a-test", dev->name); |
| 221 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 222 | |
| 223 | /* This should write GPIO pin 6 (see device tree test.dts ) */ |
| 224 | ut_asserteq(6, acpi_device_write_gpio_desc(ctx, &desc)); |
| 225 | ut_asserteq(35, acpigen_get_current(ctx) - ptr); |
| 226 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 227 | ut_asserteq(32, get_unaligned((u16 *)(ptr + 1))); |
| 228 | ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]); |
| 229 | ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, ptr[4]); |
| 230 | ut_asserteq(1, get_unaligned((u16 *)(ptr + 5))); |
| 231 | ut_asserteq(29, get_unaligned((u16 *)(ptr + 7))); |
| 232 | ut_asserteq(ACPI_GPIO_PULL_DOWN, ptr[9]); |
| 233 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 10))); |
| 234 | ut_asserteq(4321, get_unaligned((u16 *)(ptr + 12))); |
| 235 | ut_asserteq(23, get_unaligned((u16 *)(ptr + 14))); |
| 236 | ut_asserteq(0, ptr[16]); |
| 237 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 17))); |
| 238 | ut_asserteq(35, get_unaligned((u16 *)(ptr + 19))); |
| 239 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 21))); |
| 240 | |
| 241 | /* pin0 */ |
| 242 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 23))); |
| 243 | |
| 244 | ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25); |
| 245 | |
| 246 | free_context(&ctx); |
| 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | DM_TEST(dm_test_acpi_gpio_irq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 4ebc940 | 2020-07-07 13:11:47 -0600 | [diff] [blame] | 251 | |
| 252 | /* Test emitting either a GPIO or interrupt descriptor */ |
| 253 | static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts) |
| 254 | { |
| 255 | struct acpi_ctx *ctx; |
| 256 | struct udevice *dev; |
| 257 | u8 *ptr; |
| 258 | |
| 259 | ut_assertok(alloc_context(&ctx)); |
| 260 | |
| 261 | ptr = acpigen_get_current(ctx); |
| 262 | |
| 263 | /* This should produce an interrupt, even though it also has a GPIO */ |
| 264 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 265 | ut_asserteq_str("a-test", dev->name); |
| 266 | ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 267 | "test2-gpios")); |
| 268 | ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]); |
| 269 | |
| 270 | /* This has no interrupt so should produce a GPIO */ |
| 271 | ptr = ctx->current; |
| 272 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev)); |
| 273 | ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 274 | "enable-gpios")); |
| 275 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 276 | |
| 277 | /* This one has neither */ |
| 278 | ptr = acpigen_get_current(ctx); |
| 279 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
| 280 | ut_asserteq_str("b-test", dev->name); |
| 281 | ut_asserteq(-ENOENT, |
| 282 | acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 283 | "enable-gpios")); |
| 284 | |
| 285 | free_context(&ctx); |
| 286 | |
| 287 | return 0; |
| 288 | } |
| 289 | DM_TEST(dm_test_acpi_interrupt_or_gpio, |
| 290 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 31e1787 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 291 | |
| 292 | /* Test emitting an I2C descriptor */ |
| 293 | static int dm_test_acpi_i2c(struct unit_test_state *uts) |
| 294 | { |
| 295 | struct acpi_ctx *ctx; |
| 296 | struct udevice *dev; |
| 297 | u8 *ptr; |
| 298 | |
| 299 | ut_assertok(alloc_context(&ctx)); |
| 300 | |
| 301 | ptr = acpigen_get_current(ctx); |
| 302 | |
| 303 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 304 | ut_asserteq(0x43, acpi_device_write_i2c_dev(ctx, dev)); |
| 305 | ut_asserteq(28, acpigen_get_current(ctx) - ptr); |
| 306 | ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]); |
| 307 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 1))); |
| 308 | ut_asserteq(ACPI_I2C_SERIAL_BUS_REVISION_ID, ptr[3]); |
| 309 | ut_asserteq(0, ptr[4]); |
| 310 | ut_asserteq(ACPI_SERIAL_BUS_TYPE_I2C, ptr[5]); |
| 311 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 7))); |
| 312 | ut_asserteq(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID, ptr[9]); |
| 313 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 10))); |
| 314 | ut_asserteq(100000, get_unaligned((u32 *)(ptr + 12))); |
| 315 | ut_asserteq(0x43, get_unaligned((u16 *)(ptr + 16))); |
| 316 | ut_asserteq_str("\\_SB.I2C0", (char *)ptr + 18); |
| 317 | |
| 318 | free_context(&ctx); |
| 319 | |
| 320 | return 0; |
| 321 | } |
| 322 | DM_TEST(dm_test_acpi_i2c, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 70e5e67 | 2020-07-07 13:11:49 -0600 | [diff] [blame] | 323 | |
| 324 | /* Test emitting a SPI descriptor */ |
| 325 | static int dm_test_acpi_spi(struct unit_test_state *uts) |
| 326 | { |
| 327 | struct acpi_ctx *ctx; |
| 328 | struct udevice *dev; |
| 329 | u8 *ptr; |
| 330 | |
| 331 | ut_assertok(alloc_context(&ctx)); |
| 332 | |
| 333 | ptr = acpigen_get_current(ctx); |
| 334 | |
| 335 | ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev)); |
| 336 | ut_assertok(acpi_device_write_spi_dev(ctx, dev)); |
| 337 | ut_asserteq(31, acpigen_get_current(ctx) - ptr); |
| 338 | ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]); |
| 339 | ut_asserteq(28, get_unaligned((u16 *)(ptr + 1))); |
| 340 | ut_asserteq(ACPI_SPI_SERIAL_BUS_REVISION_ID, ptr[3]); |
| 341 | ut_asserteq(0, ptr[4]); |
| 342 | ut_asserteq(ACPI_SERIAL_BUS_TYPE_SPI, ptr[5]); |
| 343 | ut_asserteq(2, ptr[6]); |
| 344 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 7))); |
| 345 | ut_asserteq(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID, ptr[9]); |
| 346 | ut_asserteq(9, get_unaligned((u16 *)(ptr + 10))); |
| 347 | ut_asserteq(40000000, get_unaligned((u32 *)(ptr + 12))); |
| 348 | ut_asserteq(8, ptr[16]); |
| 349 | ut_asserteq(0, ptr[17]); |
| 350 | ut_asserteq(0, ptr[18]); |
| 351 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 19))); |
| 352 | ut_asserteq_str("\\_SB.SPI0", (char *)ptr + 21); |
| 353 | |
| 354 | free_context(&ctx); |
| 355 | |
| 356 | return 0; |
| 357 | } |
| 358 | DM_TEST(dm_test_acpi_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 359 | |
| 360 | /** |
| 361 | * get_length() - decode a three-byte length field |
| 362 | * |
| 363 | * @ptr: Length encoded as per ACPI |
| 364 | * @return decoded length, or -EINVAL on error |
| 365 | */ |
| 366 | static int get_length(u8 *ptr) |
| 367 | { |
| 368 | if (!(*ptr & 0x80)) |
| 369 | return -EINVAL; |
| 370 | |
| 371 | return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12; |
| 372 | } |
| 373 | |
| 374 | /* Test emitting a length */ |
| 375 | static int dm_test_acpi_len(struct unit_test_state *uts) |
| 376 | { |
| 377 | const int size = 0xc0000; |
| 378 | struct acpi_ctx *ctx; |
| 379 | u8 *ptr; |
| 380 | int i; |
| 381 | |
| 382 | ut_assertok(alloc_context_size(&ctx, size)); |
| 383 | |
| 384 | ptr = acpigen_get_current(ctx); |
| 385 | |
| 386 | /* Write a byte and a 3-byte length */ |
| 387 | acpigen_write_len_f(ctx); |
| 388 | acpigen_emit_byte(ctx, 0x23); |
| 389 | acpigen_pop_len(ctx); |
| 390 | ut_asserteq(1 + 3, get_length(ptr)); |
| 391 | |
| 392 | /* Write 200 bytes so we need two length bytes */ |
| 393 | ptr = ctx->current; |
| 394 | acpigen_write_len_f(ctx); |
| 395 | for (i = 0; i < 200; i++) |
| 396 | acpigen_emit_byte(ctx, 0x23); |
| 397 | acpigen_pop_len(ctx); |
| 398 | ut_asserteq(200 + 3, get_length(ptr)); |
| 399 | |
| 400 | /* Write 40KB so we need three length bytes */ |
| 401 | ptr = ctx->current; |
| 402 | acpigen_write_len_f(ctx); |
| 403 | for (i = 0; i < 40000; i++) |
| 404 | acpigen_emit_byte(ctx, 0x23); |
| 405 | acpigen_pop_len(ctx); |
| 406 | ut_asserteq(40000 + 3, get_length(ptr)); |
| 407 | |
| 408 | free_context(&ctx); |
| 409 | |
| 410 | return 0; |
| 411 | } |
| 412 | DM_TEST(dm_test_acpi_len, 0); |
Simon Glass | 03967ce | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 413 | |
| 414 | /* Test writing a package */ |
| 415 | static int dm_test_acpi_package(struct unit_test_state *uts) |
| 416 | { |
| 417 | struct acpi_ctx *ctx; |
| 418 | char *num_elements; |
| 419 | u8 *ptr; |
| 420 | |
| 421 | ut_assertok(alloc_context(&ctx)); |
| 422 | |
| 423 | ptr = acpigen_get_current(ctx); |
| 424 | |
| 425 | num_elements = acpigen_write_package(ctx, 3); |
| 426 | ut_asserteq_ptr(num_elements, ptr + 4); |
| 427 | |
| 428 | /* For ease of testing, just emit a byte, not valid package contents */ |
| 429 | acpigen_emit_byte(ctx, 0x23); |
| 430 | acpigen_pop_len(ctx); |
| 431 | ut_asserteq(PACKAGE_OP, ptr[0]); |
| 432 | ut_asserteq(5, get_length(ptr + 1)); |
| 433 | ut_asserteq(3, ptr[4]); |
| 434 | |
| 435 | free_context(&ctx); |
| 436 | |
| 437 | return 0; |
| 438 | } |
| 439 | DM_TEST(dm_test_acpi_package, 0); |
Simon Glass | 83b2bd5 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 440 | |
| 441 | /* Test writing an integer */ |
| 442 | static int dm_test_acpi_integer(struct unit_test_state *uts) |
| 443 | { |
| 444 | struct acpi_ctx *ctx; |
| 445 | u8 *ptr; |
| 446 | |
| 447 | ut_assertok(alloc_context(&ctx)); |
| 448 | |
| 449 | ptr = acpigen_get_current(ctx); |
| 450 | |
| 451 | acpigen_write_integer(ctx, 0); |
| 452 | acpigen_write_integer(ctx, 1); |
| 453 | acpigen_write_integer(ctx, TEST_INT8); |
| 454 | acpigen_write_integer(ctx, TEST_INT16); |
| 455 | acpigen_write_integer(ctx, TEST_INT32); |
| 456 | acpigen_write_integer(ctx, TEST_INT64); |
| 457 | |
| 458 | ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr); |
| 459 | |
| 460 | ut_asserteq(ZERO_OP, ptr[0]); |
| 461 | |
| 462 | ut_asserteq(ONE_OP, ptr[1]); |
| 463 | |
| 464 | ut_asserteq(BYTE_PREFIX, ptr[2]); |
| 465 | ut_asserteq(TEST_INT8, ptr[3]); |
| 466 | |
| 467 | ut_asserteq(WORD_PREFIX, ptr[4]); |
| 468 | ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5))); |
| 469 | |
| 470 | ut_asserteq(DWORD_PREFIX, ptr[7]); |
| 471 | ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8))); |
| 472 | |
| 473 | ut_asserteq(QWORD_PREFIX, ptr[12]); |
| 474 | ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13))); |
| 475 | |
| 476 | free_context(&ctx); |
| 477 | |
| 478 | return 0; |
| 479 | } |
| 480 | DM_TEST(dm_test_acpi_integer, 0); |
Simon Glass | 3df33bd | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 481 | |
| 482 | /* Test writing a string */ |
| 483 | static int dm_test_acpi_string(struct unit_test_state *uts) |
| 484 | { |
| 485 | struct acpi_ctx *ctx; |
| 486 | u8 *ptr; |
| 487 | |
| 488 | ut_assertok(alloc_context(&ctx)); |
| 489 | |
| 490 | ptr = acpigen_get_current(ctx); |
| 491 | |
| 492 | acpigen_write_string(ctx, TEST_STRING); |
| 493 | acpigen_write_string(ctx, TEST_STRING2); |
| 494 | |
| 495 | ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2), |
| 496 | acpigen_get_current(ctx) - ptr); |
| 497 | ut_asserteq(STRING_PREFIX, ptr[0]); |
| 498 | ut_asserteq_str(TEST_STRING, (char *)ptr + 1); |
| 499 | ptr += 1 + sizeof(TEST_STRING); |
| 500 | ut_asserteq(STRING_PREFIX, ptr[0]); |
| 501 | ut_asserteq_str(TEST_STRING2, (char *)ptr + 1); |
| 502 | |
| 503 | free_context(&ctx); |
| 504 | |
| 505 | return 0; |
| 506 | } |
| 507 | DM_TEST(dm_test_acpi_string, 0); |
Simon Glass | 7aed90d | 2020-07-07 13:11:54 -0600 | [diff] [blame^] | 508 | |
| 509 | /* Test writing a name */ |
| 510 | static int dm_test_acpi_name(struct unit_test_state *uts) |
| 511 | { |
| 512 | struct acpi_ctx *ctx; |
| 513 | u8 *ptr; |
| 514 | |
| 515 | ut_assertok(alloc_context(&ctx)); |
| 516 | |
| 517 | ptr = acpigen_get_current(ctx); |
| 518 | |
| 519 | /* |
| 520 | * The names here are made up for testing the various cases. The |
| 521 | * grammar is in the ACPI spec 6.3 section 19.2.2 |
| 522 | */ |
| 523 | acpigen_write_name(ctx, "\\_SB"); |
| 524 | acpigen_write_name(ctx, "\\_SB.I2C0"); |
| 525 | acpigen_write_name(ctx, "\\_SB.I2C0.TPM2"); |
| 526 | acpigen_write_name(ctx, "\\_SB.I2C0.TPM2.LONG"); |
| 527 | acpigen_write_name(ctx, "^^^^SPI0.FLAS"); |
| 528 | acpigen_write_name(ctx, "NN"); |
| 529 | acpigen_write_name(ctx, "^AB.CD.D.EFG"); |
| 530 | acpigen_write_name(ctx, "^^^^"); |
| 531 | acpigen_write_name(ctx, "\\"); |
| 532 | acpigen_write_name(ctx, "\\ABCD"); |
| 533 | |
| 534 | ut_asserteq(107, acpigen_get_current(ctx) - ptr); |
| 535 | ut_asserteq(NAME_OP, ptr[0]); |
| 536 | ut_asserteq_strn("\\_SB_", (char *)ptr + 1); |
| 537 | ptr += 6; |
| 538 | |
| 539 | ut_asserteq(NAME_OP, ptr[0]); |
| 540 | ut_asserteq('\\', ptr[1]); |
| 541 | ut_asserteq(DUAL_NAME_PREFIX, ptr[2]); |
| 542 | ut_asserteq_strn("_SB_I2C0", (char *)ptr + 3); |
| 543 | ptr += 11; |
| 544 | |
| 545 | ut_asserteq(NAME_OP, ptr[0]); |
| 546 | ut_asserteq('\\', ptr[1]); |
| 547 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 548 | ut_asserteq(3, ptr[3]); |
| 549 | ut_asserteq_strn("_SB_I2C0TPM2", (char *)ptr + 4); |
| 550 | ptr += 16; |
| 551 | |
| 552 | ut_asserteq(NAME_OP, ptr[0]); |
| 553 | ut_asserteq('\\', ptr[1]); |
| 554 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 555 | ut_asserteq(4, ptr[3]); |
| 556 | ut_asserteq_strn("_SB_I2C0TPM2LONG", (char *)ptr + 4); |
| 557 | ptr += 20; |
| 558 | |
| 559 | ut_asserteq(NAME_OP, ptr[0]); |
| 560 | ut_asserteq('^', ptr[1]); |
| 561 | ut_asserteq('^', ptr[2]); |
| 562 | ut_asserteq('^', ptr[3]); |
| 563 | ut_asserteq('^', ptr[4]); |
| 564 | ut_asserteq(DUAL_NAME_PREFIX, ptr[5]); |
| 565 | ut_asserteq_strn("SPI0FLAS", (char *)ptr + 6); |
| 566 | ptr += 14; |
| 567 | |
| 568 | ut_asserteq(NAME_OP, ptr[0]); |
| 569 | ut_asserteq_strn("NN__", (char *)ptr + 1); |
| 570 | ptr += 5; |
| 571 | |
| 572 | ut_asserteq(NAME_OP, ptr[0]); |
| 573 | ut_asserteq('^', ptr[1]); |
| 574 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 575 | ut_asserteq(4, ptr[3]); |
| 576 | ut_asserteq_strn("AB__CD__D___EFG_", (char *)ptr + 4); |
| 577 | ptr += 20; |
| 578 | |
| 579 | ut_asserteq(NAME_OP, ptr[0]); |
| 580 | ut_asserteq('^', ptr[1]); |
| 581 | ut_asserteq('^', ptr[2]); |
| 582 | ut_asserteq('^', ptr[3]); |
| 583 | ut_asserteq('^', ptr[4]); |
| 584 | ut_asserteq(ZERO_OP, ptr[5]); |
| 585 | ptr += 6; |
| 586 | |
| 587 | ut_asserteq(NAME_OP, ptr[0]); |
| 588 | ut_asserteq('\\', ptr[1]); |
| 589 | ut_asserteq(ZERO_OP, ptr[2]); |
| 590 | ptr += 3; |
| 591 | |
| 592 | ut_asserteq(NAME_OP, ptr[0]); |
| 593 | ut_asserteq_strn("\\ABCD", (char *)ptr + 1); |
| 594 | ptr += 5; |
| 595 | |
| 596 | free_context(&ctx); |
| 597 | |
| 598 | return 0; |
| 599 | } |
| 600 | DM_TEST(dm_test_acpi_name, 0); |