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 | d7d631d | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 15 | #include <acpi/acpi_table.h> |
Simon Glass | a9e0a07 | 2020-07-07 13:11:46 -0600 | [diff] [blame] | 16 | #include <asm/gpio.h> |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 17 | #include <asm/unaligned.h> |
| 18 | #include <dm/acpi.h> |
| 19 | #include <dm/test.h> |
Simon Glass | 4ebc940 | 2020-07-07 13:11:47 -0600 | [diff] [blame] | 20 | #include <dm/uclass-internal.h> |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 21 | #include <test/ut.h> |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 22 | #include "acpi.h" |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 23 | |
| 24 | /* Maximum size of the ACPI context needed for most tests */ |
| 25 | #define ACPI_CONTEXT_SIZE 150 |
| 26 | |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 27 | #define TEST_STRING "frogmore" |
Simon Glass | 3df33bd | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 28 | #define TEST_STRING2 "ranch" |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 29 | #define TEST_STREAM2 "\xfa\xde" |
| 30 | |
Simon Glass | 83b2bd5 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 31 | #define TEST_INT8 0x7d |
| 32 | #define TEST_INT16 0x2345 |
| 33 | #define TEST_INT32 0x12345678 |
| 34 | #define TEST_INT64 0x4567890123456 |
| 35 | |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 36 | int acpi_test_alloc_context_size(struct acpi_ctx **ctxp, int size) |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 37 | { |
| 38 | struct acpi_ctx *ctx; |
| 39 | |
| 40 | *ctxp = NULL; |
| 41 | ctx = malloc(sizeof(*ctx)); |
| 42 | if (!ctx) |
| 43 | return -ENOMEM; |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 44 | ctx->base = malloc(size); |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 45 | if (!ctx->base) { |
| 46 | free(ctx); |
| 47 | return -ENOMEM; |
| 48 | } |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 49 | ctx->ltop = 0; |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 50 | ctx->current = ctx->base; |
| 51 | *ctxp = ctx; |
| 52 | |
| 53 | return 0; |
| 54 | } |
| 55 | |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 56 | int acpi_test_get_length(u8 *ptr) |
| 57 | { |
| 58 | if (!(*ptr & 0x80)) |
| 59 | return -EINVAL; |
| 60 | |
| 61 | return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12; |
| 62 | } |
| 63 | |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 64 | static int alloc_context(struct acpi_ctx **ctxp) |
| 65 | { |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 66 | return acpi_test_alloc_context_size(ctxp, ACPI_CONTEXT_SIZE); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 67 | } |
| 68 | |
Simon Glass | 61cc933 | 2020-07-07 13:11:42 -0600 | [diff] [blame] | 69 | static void free_context(struct acpi_ctx **ctxp) |
| 70 | { |
| 71 | free((*ctxp)->base); |
| 72 | free(*ctxp); |
| 73 | *ctxp = NULL; |
| 74 | } |
| 75 | |
| 76 | /* Test emitting simple types and acpigen_get_current() */ |
| 77 | static int dm_test_acpi_emit_simple(struct unit_test_state *uts) |
| 78 | { |
| 79 | struct acpi_ctx *ctx; |
| 80 | u8 *ptr; |
| 81 | |
| 82 | ut_assertok(alloc_context(&ctx)); |
| 83 | |
| 84 | ptr = acpigen_get_current(ctx); |
| 85 | acpigen_emit_byte(ctx, 0x23); |
| 86 | ut_asserteq(1, acpigen_get_current(ctx) - ptr); |
| 87 | ut_asserteq(0x23, *(u8 *)ptr); |
| 88 | |
| 89 | acpigen_emit_word(ctx, 0x1234); |
| 90 | ut_asserteq(3, acpigen_get_current(ctx) - ptr); |
| 91 | ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1))); |
| 92 | |
| 93 | acpigen_emit_dword(ctx, 0x87654321); |
| 94 | ut_asserteq(7, acpigen_get_current(ctx) - ptr); |
| 95 | ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3))); |
| 96 | |
| 97 | free_context(&ctx); |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | DM_TEST(dm_test_acpi_emit_simple, 0); |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 102 | |
Simon Glass | 7fb8da4 | 2020-07-07 13:11:45 -0600 | [diff] [blame] | 103 | /* Test emitting a stream */ |
| 104 | static int dm_test_acpi_emit_stream(struct unit_test_state *uts) |
| 105 | { |
| 106 | struct acpi_ctx *ctx; |
| 107 | u8 *ptr; |
| 108 | |
| 109 | ut_assertok(alloc_context(&ctx)); |
| 110 | |
| 111 | ptr = acpigen_get_current(ctx); |
| 112 | acpigen_emit_stream(ctx, TEST_STREAM2, 2); |
| 113 | ut_asserteq(2, acpigen_get_current(ctx) - ptr); |
| 114 | ut_asserteq((u8)TEST_STREAM2[0], ptr[0]); |
| 115 | ut_asserteq((u8)TEST_STREAM2[1], ptr[1]); |
| 116 | |
| 117 | free_context(&ctx); |
| 118 | |
| 119 | return 0; |
| 120 | } |
| 121 | DM_TEST(dm_test_acpi_emit_stream, 0); |
| 122 | |
| 123 | /* Test emitting a string */ |
| 124 | static int dm_test_acpi_emit_string(struct unit_test_state *uts) |
| 125 | { |
| 126 | struct acpi_ctx *ctx; |
| 127 | u8 *ptr; |
| 128 | |
| 129 | ut_assertok(alloc_context(&ctx)); |
| 130 | |
| 131 | ptr = acpigen_get_current(ctx); |
| 132 | acpigen_emit_string(ctx, TEST_STRING); |
| 133 | ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr); |
| 134 | ut_asserteq_str(TEST_STRING, (char *)ptr); |
| 135 | |
| 136 | free_context(&ctx); |
| 137 | |
| 138 | return 0; |
| 139 | } |
| 140 | DM_TEST(dm_test_acpi_emit_string, 0); |
| 141 | |
Simon Glass | ff715c6 | 2020-07-07 13:11:43 -0600 | [diff] [blame] | 142 | /* Test emitting an interrupt descriptor */ |
| 143 | static int dm_test_acpi_interrupt(struct unit_test_state *uts) |
| 144 | { |
| 145 | struct acpi_ctx *ctx; |
| 146 | struct udevice *dev; |
| 147 | struct irq irq; |
| 148 | u8 *ptr; |
| 149 | |
| 150 | ut_assertok(alloc_context(&ctx)); |
| 151 | |
| 152 | ptr = acpigen_get_current(ctx); |
| 153 | |
| 154 | ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev)); |
| 155 | ut_assertok(irq_get_by_index(dev, 0, &irq)); |
| 156 | |
| 157 | /* See a-test, property interrupts-extended in the device tree */ |
| 158 | ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq)); |
| 159 | ut_asserteq(9, acpigen_get_current(ctx) - ptr); |
| 160 | ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]); |
| 161 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 1))); |
| 162 | ut_asserteq(0x19, ptr[3]); |
| 163 | ut_asserteq(1, ptr[4]); |
| 164 | ut_asserteq(3, get_unaligned((u32 *)(ptr + 5))); |
| 165 | |
| 166 | free_context(&ctx); |
| 167 | |
| 168 | return 0; |
| 169 | } |
| 170 | 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] | 171 | |
| 172 | /* Test emitting a GPIO descriptor */ |
| 173 | static int dm_test_acpi_gpio(struct unit_test_state *uts) |
| 174 | { |
| 175 | struct gpio_desc desc; |
| 176 | struct acpi_ctx *ctx; |
| 177 | struct udevice *dev; |
| 178 | u8 *ptr; |
| 179 | |
| 180 | ut_assertok(alloc_context(&ctx)); |
| 181 | |
| 182 | ptr = acpigen_get_current(ctx); |
| 183 | |
| 184 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 185 | ut_asserteq_str("a-test", dev->name); |
| 186 | ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0)); |
| 187 | |
| 188 | /* This should write GPIO pin 4 (see device tree test.dts ) */ |
| 189 | ut_asserteq(4, acpi_device_write_gpio_desc(ctx, &desc)); |
| 190 | ut_asserteq(35, acpigen_get_current(ctx) - ptr); |
| 191 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 192 | ut_asserteq(32, get_unaligned((u16 *)(ptr + 1))); |
| 193 | ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]); |
| 194 | ut_asserteq(ACPI_GPIO_TYPE_IO, ptr[4]); |
| 195 | ut_asserteq(1, get_unaligned((u16 *)(ptr + 5))); |
| 196 | ut_asserteq(9, get_unaligned((u16 *)(ptr + 7))); |
| 197 | ut_asserteq(ACPI_GPIO_PULL_UP, ptr[9]); |
| 198 | ut_asserteq(1234, get_unaligned((u16 *)(ptr + 10))); |
| 199 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 12))); |
| 200 | ut_asserteq(23, get_unaligned((u16 *)(ptr + 14))); |
| 201 | ut_asserteq(0, ptr[16]); |
| 202 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 17))); |
| 203 | ut_asserteq(35, get_unaligned((u16 *)(ptr + 19))); |
| 204 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 21))); |
| 205 | |
| 206 | /* pin0 */ |
| 207 | ut_asserteq(4, get_unaligned((u16 *)(ptr + 23))); |
| 208 | |
| 209 | ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25); |
| 210 | |
| 211 | free_context(&ctx); |
| 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | DM_TEST(dm_test_acpi_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
| 216 | |
| 217 | /* Test emitting a GPIO descriptor with an interrupt */ |
| 218 | static int dm_test_acpi_gpio_irq(struct unit_test_state *uts) |
| 219 | { |
| 220 | struct gpio_desc desc; |
| 221 | struct acpi_ctx *ctx; |
| 222 | struct udevice *dev; |
| 223 | u8 *ptr; |
| 224 | |
| 225 | ut_assertok(alloc_context(&ctx)); |
| 226 | |
| 227 | ptr = acpigen_get_current(ctx); |
| 228 | |
| 229 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 230 | ut_asserteq_str("a-test", dev->name); |
| 231 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 232 | |
| 233 | /* This should write GPIO pin 6 (see device tree test.dts ) */ |
| 234 | ut_asserteq(6, acpi_device_write_gpio_desc(ctx, &desc)); |
| 235 | ut_asserteq(35, acpigen_get_current(ctx) - ptr); |
| 236 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 237 | ut_asserteq(32, get_unaligned((u16 *)(ptr + 1))); |
| 238 | ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]); |
| 239 | ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, ptr[4]); |
| 240 | ut_asserteq(1, get_unaligned((u16 *)(ptr + 5))); |
| 241 | ut_asserteq(29, get_unaligned((u16 *)(ptr + 7))); |
| 242 | ut_asserteq(ACPI_GPIO_PULL_DOWN, ptr[9]); |
| 243 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 10))); |
| 244 | ut_asserteq(4321, get_unaligned((u16 *)(ptr + 12))); |
| 245 | ut_asserteq(23, get_unaligned((u16 *)(ptr + 14))); |
| 246 | ut_asserteq(0, ptr[16]); |
| 247 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 17))); |
| 248 | ut_asserteq(35, get_unaligned((u16 *)(ptr + 19))); |
| 249 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 21))); |
| 250 | |
| 251 | /* pin0 */ |
| 252 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 23))); |
| 253 | |
| 254 | ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25); |
| 255 | |
| 256 | free_context(&ctx); |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | 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] | 261 | |
| 262 | /* Test emitting either a GPIO or interrupt descriptor */ |
| 263 | static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts) |
| 264 | { |
| 265 | struct acpi_ctx *ctx; |
| 266 | struct udevice *dev; |
| 267 | u8 *ptr; |
| 268 | |
| 269 | ut_assertok(alloc_context(&ctx)); |
| 270 | |
| 271 | ptr = acpigen_get_current(ctx); |
| 272 | |
| 273 | /* This should produce an interrupt, even though it also has a GPIO */ |
| 274 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 275 | ut_asserteq_str("a-test", dev->name); |
| 276 | ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 277 | "test2-gpios")); |
| 278 | ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]); |
| 279 | |
| 280 | /* This has no interrupt so should produce a GPIO */ |
| 281 | ptr = ctx->current; |
| 282 | ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev)); |
| 283 | ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 284 | "enable-gpios")); |
| 285 | ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]); |
| 286 | |
| 287 | /* This one has neither */ |
| 288 | ptr = acpigen_get_current(ctx); |
| 289 | ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev)); |
| 290 | ut_asserteq_str("b-test", dev->name); |
| 291 | ut_asserteq(-ENOENT, |
| 292 | acpi_device_write_interrupt_or_gpio(ctx, dev, |
| 293 | "enable-gpios")); |
| 294 | |
| 295 | free_context(&ctx); |
| 296 | |
| 297 | return 0; |
| 298 | } |
| 299 | DM_TEST(dm_test_acpi_interrupt_or_gpio, |
| 300 | DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 31e1787 | 2020-07-07 13:11:48 -0600 | [diff] [blame] | 301 | |
| 302 | /* Test emitting an I2C descriptor */ |
| 303 | static int dm_test_acpi_i2c(struct unit_test_state *uts) |
| 304 | { |
| 305 | struct acpi_ctx *ctx; |
| 306 | struct udevice *dev; |
| 307 | u8 *ptr; |
| 308 | |
| 309 | ut_assertok(alloc_context(&ctx)); |
| 310 | |
| 311 | ptr = acpigen_get_current(ctx); |
| 312 | |
| 313 | ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev)); |
| 314 | ut_asserteq(0x43, acpi_device_write_i2c_dev(ctx, dev)); |
| 315 | ut_asserteq(28, acpigen_get_current(ctx) - ptr); |
| 316 | ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]); |
| 317 | ut_asserteq(25, get_unaligned((u16 *)(ptr + 1))); |
| 318 | ut_asserteq(ACPI_I2C_SERIAL_BUS_REVISION_ID, ptr[3]); |
| 319 | ut_asserteq(0, ptr[4]); |
| 320 | ut_asserteq(ACPI_SERIAL_BUS_TYPE_I2C, ptr[5]); |
| 321 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 7))); |
| 322 | ut_asserteq(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID, ptr[9]); |
| 323 | ut_asserteq(6, get_unaligned((u16 *)(ptr + 10))); |
| 324 | ut_asserteq(100000, get_unaligned((u32 *)(ptr + 12))); |
| 325 | ut_asserteq(0x43, get_unaligned((u16 *)(ptr + 16))); |
| 326 | ut_asserteq_str("\\_SB.I2C0", (char *)ptr + 18); |
| 327 | |
| 328 | free_context(&ctx); |
| 329 | |
| 330 | return 0; |
| 331 | } |
| 332 | 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] | 333 | |
| 334 | /* Test emitting a SPI descriptor */ |
| 335 | static int dm_test_acpi_spi(struct unit_test_state *uts) |
| 336 | { |
| 337 | struct acpi_ctx *ctx; |
| 338 | struct udevice *dev; |
| 339 | u8 *ptr; |
| 340 | |
| 341 | ut_assertok(alloc_context(&ctx)); |
| 342 | |
| 343 | ptr = acpigen_get_current(ctx); |
| 344 | |
| 345 | ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev)); |
| 346 | ut_assertok(acpi_device_write_spi_dev(ctx, dev)); |
| 347 | ut_asserteq(31, acpigen_get_current(ctx) - ptr); |
| 348 | ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]); |
| 349 | ut_asserteq(28, get_unaligned((u16 *)(ptr + 1))); |
| 350 | ut_asserteq(ACPI_SPI_SERIAL_BUS_REVISION_ID, ptr[3]); |
| 351 | ut_asserteq(0, ptr[4]); |
| 352 | ut_asserteq(ACPI_SERIAL_BUS_TYPE_SPI, ptr[5]); |
| 353 | ut_asserteq(2, ptr[6]); |
| 354 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 7))); |
| 355 | ut_asserteq(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID, ptr[9]); |
| 356 | ut_asserteq(9, get_unaligned((u16 *)(ptr + 10))); |
| 357 | ut_asserteq(40000000, get_unaligned((u32 *)(ptr + 12))); |
| 358 | ut_asserteq(8, ptr[16]); |
| 359 | ut_asserteq(0, ptr[17]); |
| 360 | ut_asserteq(0, ptr[18]); |
| 361 | ut_asserteq(0, get_unaligned((u16 *)(ptr + 19))); |
| 362 | ut_asserteq_str("\\_SB.SPI0", (char *)ptr + 21); |
| 363 | |
| 364 | free_context(&ctx); |
| 365 | |
| 366 | return 0; |
| 367 | } |
| 368 | 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] | 369 | |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 370 | /* Test emitting a length */ |
| 371 | static int dm_test_acpi_len(struct unit_test_state *uts) |
| 372 | { |
| 373 | const int size = 0xc0000; |
| 374 | struct acpi_ctx *ctx; |
| 375 | u8 *ptr; |
| 376 | int i; |
| 377 | |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 378 | ut_assertok(acpi_test_alloc_context_size(&ctx, size)); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 379 | |
| 380 | ptr = acpigen_get_current(ctx); |
| 381 | |
| 382 | /* Write a byte and a 3-byte length */ |
| 383 | acpigen_write_len_f(ctx); |
| 384 | acpigen_emit_byte(ctx, 0x23); |
| 385 | acpigen_pop_len(ctx); |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 386 | ut_asserteq(1 + 3, acpi_test_get_length(ptr)); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 387 | |
| 388 | /* Write 200 bytes so we need two length bytes */ |
| 389 | ptr = ctx->current; |
| 390 | acpigen_write_len_f(ctx); |
| 391 | for (i = 0; i < 200; i++) |
| 392 | acpigen_emit_byte(ctx, 0x23); |
| 393 | acpigen_pop_len(ctx); |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 394 | ut_asserteq(200 + 3, acpi_test_get_length(ptr)); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 395 | |
| 396 | /* Write 40KB so we need three length bytes */ |
| 397 | ptr = ctx->current; |
| 398 | acpigen_write_len_f(ctx); |
| 399 | for (i = 0; i < 40000; i++) |
| 400 | acpigen_emit_byte(ctx, 0x23); |
| 401 | acpigen_pop_len(ctx); |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 402 | ut_asserteq(40000 + 3, acpi_test_get_length(ptr)); |
Simon Glass | 7e148f2 | 2020-07-07 13:11:50 -0600 | [diff] [blame] | 403 | |
| 404 | free_context(&ctx); |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | DM_TEST(dm_test_acpi_len, 0); |
Simon Glass | 03967ce | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 409 | |
| 410 | /* Test writing a package */ |
| 411 | static int dm_test_acpi_package(struct unit_test_state *uts) |
| 412 | { |
| 413 | struct acpi_ctx *ctx; |
| 414 | char *num_elements; |
| 415 | u8 *ptr; |
| 416 | |
| 417 | ut_assertok(alloc_context(&ctx)); |
| 418 | |
| 419 | ptr = acpigen_get_current(ctx); |
| 420 | |
| 421 | num_elements = acpigen_write_package(ctx, 3); |
| 422 | ut_asserteq_ptr(num_elements, ptr + 4); |
| 423 | |
| 424 | /* For ease of testing, just emit a byte, not valid package contents */ |
| 425 | acpigen_emit_byte(ctx, 0x23); |
| 426 | acpigen_pop_len(ctx); |
| 427 | ut_asserteq(PACKAGE_OP, ptr[0]); |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 428 | ut_asserteq(5, acpi_test_get_length(ptr + 1)); |
Simon Glass | 03967ce | 2020-07-07 13:11:51 -0600 | [diff] [blame] | 429 | ut_asserteq(3, ptr[4]); |
| 430 | |
| 431 | free_context(&ctx); |
| 432 | |
| 433 | return 0; |
| 434 | } |
| 435 | DM_TEST(dm_test_acpi_package, 0); |
Simon Glass | 83b2bd5 | 2020-07-07 13:11:52 -0600 | [diff] [blame] | 436 | |
| 437 | /* Test writing an integer */ |
| 438 | static int dm_test_acpi_integer(struct unit_test_state *uts) |
| 439 | { |
| 440 | struct acpi_ctx *ctx; |
| 441 | u8 *ptr; |
| 442 | |
| 443 | ut_assertok(alloc_context(&ctx)); |
| 444 | |
| 445 | ptr = acpigen_get_current(ctx); |
| 446 | |
| 447 | acpigen_write_integer(ctx, 0); |
| 448 | acpigen_write_integer(ctx, 1); |
| 449 | acpigen_write_integer(ctx, TEST_INT8); |
| 450 | acpigen_write_integer(ctx, TEST_INT16); |
| 451 | acpigen_write_integer(ctx, TEST_INT32); |
| 452 | acpigen_write_integer(ctx, TEST_INT64); |
| 453 | |
| 454 | ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr); |
| 455 | |
| 456 | ut_asserteq(ZERO_OP, ptr[0]); |
| 457 | |
| 458 | ut_asserteq(ONE_OP, ptr[1]); |
| 459 | |
| 460 | ut_asserteq(BYTE_PREFIX, ptr[2]); |
| 461 | ut_asserteq(TEST_INT8, ptr[3]); |
| 462 | |
| 463 | ut_asserteq(WORD_PREFIX, ptr[4]); |
| 464 | ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5))); |
| 465 | |
| 466 | ut_asserteq(DWORD_PREFIX, ptr[7]); |
| 467 | ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8))); |
| 468 | |
| 469 | ut_asserteq(QWORD_PREFIX, ptr[12]); |
| 470 | ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13))); |
| 471 | |
| 472 | free_context(&ctx); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | DM_TEST(dm_test_acpi_integer, 0); |
Simon Glass | 3df33bd | 2020-07-07 13:11:53 -0600 | [diff] [blame] | 477 | |
| 478 | /* Test writing a string */ |
| 479 | static int dm_test_acpi_string(struct unit_test_state *uts) |
| 480 | { |
| 481 | struct acpi_ctx *ctx; |
| 482 | u8 *ptr; |
| 483 | |
| 484 | ut_assertok(alloc_context(&ctx)); |
| 485 | |
| 486 | ptr = acpigen_get_current(ctx); |
| 487 | |
| 488 | acpigen_write_string(ctx, TEST_STRING); |
| 489 | acpigen_write_string(ctx, TEST_STRING2); |
| 490 | |
| 491 | ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2), |
| 492 | acpigen_get_current(ctx) - ptr); |
| 493 | ut_asserteq(STRING_PREFIX, ptr[0]); |
| 494 | ut_asserteq_str(TEST_STRING, (char *)ptr + 1); |
| 495 | ptr += 1 + sizeof(TEST_STRING); |
| 496 | ut_asserteq(STRING_PREFIX, ptr[0]); |
| 497 | ut_asserteq_str(TEST_STRING2, (char *)ptr + 1); |
| 498 | |
| 499 | free_context(&ctx); |
| 500 | |
| 501 | return 0; |
| 502 | } |
| 503 | DM_TEST(dm_test_acpi_string, 0); |
Simon Glass | 7aed90d | 2020-07-07 13:11:54 -0600 | [diff] [blame] | 504 | |
| 505 | /* Test writing a name */ |
| 506 | static int dm_test_acpi_name(struct unit_test_state *uts) |
| 507 | { |
| 508 | struct acpi_ctx *ctx; |
| 509 | u8 *ptr; |
| 510 | |
| 511 | ut_assertok(alloc_context(&ctx)); |
| 512 | |
| 513 | ptr = acpigen_get_current(ctx); |
| 514 | |
| 515 | /* |
| 516 | * The names here are made up for testing the various cases. The |
| 517 | * grammar is in the ACPI spec 6.3 section 19.2.2 |
| 518 | */ |
| 519 | acpigen_write_name(ctx, "\\_SB"); |
| 520 | acpigen_write_name(ctx, "\\_SB.I2C0"); |
| 521 | acpigen_write_name(ctx, "\\_SB.I2C0.TPM2"); |
| 522 | acpigen_write_name(ctx, "\\_SB.I2C0.TPM2.LONG"); |
| 523 | acpigen_write_name(ctx, "^^^^SPI0.FLAS"); |
| 524 | acpigen_write_name(ctx, "NN"); |
| 525 | acpigen_write_name(ctx, "^AB.CD.D.EFG"); |
| 526 | acpigen_write_name(ctx, "^^^^"); |
| 527 | acpigen_write_name(ctx, "\\"); |
| 528 | acpigen_write_name(ctx, "\\ABCD"); |
| 529 | |
| 530 | ut_asserteq(107, acpigen_get_current(ctx) - ptr); |
| 531 | ut_asserteq(NAME_OP, ptr[0]); |
| 532 | ut_asserteq_strn("\\_SB_", (char *)ptr + 1); |
| 533 | ptr += 6; |
| 534 | |
| 535 | ut_asserteq(NAME_OP, ptr[0]); |
| 536 | ut_asserteq('\\', ptr[1]); |
| 537 | ut_asserteq(DUAL_NAME_PREFIX, ptr[2]); |
| 538 | ut_asserteq_strn("_SB_I2C0", (char *)ptr + 3); |
| 539 | ptr += 11; |
| 540 | |
| 541 | ut_asserteq(NAME_OP, ptr[0]); |
| 542 | ut_asserteq('\\', ptr[1]); |
| 543 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 544 | ut_asserteq(3, ptr[3]); |
| 545 | ut_asserteq_strn("_SB_I2C0TPM2", (char *)ptr + 4); |
| 546 | ptr += 16; |
| 547 | |
| 548 | ut_asserteq(NAME_OP, ptr[0]); |
| 549 | ut_asserteq('\\', ptr[1]); |
| 550 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 551 | ut_asserteq(4, ptr[3]); |
| 552 | ut_asserteq_strn("_SB_I2C0TPM2LONG", (char *)ptr + 4); |
| 553 | ptr += 20; |
| 554 | |
| 555 | ut_asserteq(NAME_OP, ptr[0]); |
| 556 | ut_asserteq('^', ptr[1]); |
| 557 | ut_asserteq('^', ptr[2]); |
| 558 | ut_asserteq('^', ptr[3]); |
| 559 | ut_asserteq('^', ptr[4]); |
| 560 | ut_asserteq(DUAL_NAME_PREFIX, ptr[5]); |
| 561 | ut_asserteq_strn("SPI0FLAS", (char *)ptr + 6); |
| 562 | ptr += 14; |
| 563 | |
| 564 | ut_asserteq(NAME_OP, ptr[0]); |
| 565 | ut_asserteq_strn("NN__", (char *)ptr + 1); |
| 566 | ptr += 5; |
| 567 | |
| 568 | ut_asserteq(NAME_OP, ptr[0]); |
| 569 | ut_asserteq('^', ptr[1]); |
| 570 | ut_asserteq(MULTI_NAME_PREFIX, ptr[2]); |
| 571 | ut_asserteq(4, ptr[3]); |
| 572 | ut_asserteq_strn("AB__CD__D___EFG_", (char *)ptr + 4); |
| 573 | ptr += 20; |
| 574 | |
| 575 | ut_asserteq(NAME_OP, ptr[0]); |
| 576 | ut_asserteq('^', ptr[1]); |
| 577 | ut_asserteq('^', ptr[2]); |
| 578 | ut_asserteq('^', ptr[3]); |
| 579 | ut_asserteq('^', ptr[4]); |
| 580 | ut_asserteq(ZERO_OP, ptr[5]); |
| 581 | ptr += 6; |
| 582 | |
| 583 | ut_asserteq(NAME_OP, ptr[0]); |
| 584 | ut_asserteq('\\', ptr[1]); |
| 585 | ut_asserteq(ZERO_OP, ptr[2]); |
| 586 | ptr += 3; |
| 587 | |
| 588 | ut_asserteq(NAME_OP, ptr[0]); |
| 589 | ut_asserteq_strn("\\ABCD", (char *)ptr + 1); |
| 590 | ptr += 5; |
| 591 | |
| 592 | free_context(&ctx); |
| 593 | |
| 594 | return 0; |
| 595 | } |
| 596 | DM_TEST(dm_test_acpi_name, 0); |
Simon Glass | 29df845 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 597 | |
| 598 | /* Test writing a UUID */ |
| 599 | static int dm_test_acpi_uuid(struct unit_test_state *uts) |
| 600 | { |
| 601 | struct acpi_ctx *ctx; |
| 602 | u8 *ptr; |
| 603 | |
| 604 | ut_assertok(alloc_context(&ctx)); |
| 605 | |
| 606 | ptr = acpigen_get_current(ctx); |
| 607 | |
| 608 | ut_assertok(acpigen_write_uuid(ctx, |
| 609 | "dbb8e3e6-5886-4ba6-8795-1319f52a966b")); |
| 610 | ut_asserteq(23, acpigen_get_current(ctx) - ptr); |
| 611 | ut_asserteq(BUFFER_OP, ptr[0]); |
Simon Glass | 0e5a0a0 | 2020-07-07 13:11:56 -0600 | [diff] [blame] | 612 | ut_asserteq(22, acpi_test_get_length(ptr + 1)); |
Simon Glass | 29df845 | 2020-07-07 13:11:55 -0600 | [diff] [blame] | 613 | ut_asserteq(0xdbb8e3e6, get_unaligned((u32 *)(ptr + 7))); |
| 614 | ut_asserteq(0x5886, get_unaligned((u16 *)(ptr + 11))); |
| 615 | ut_asserteq(0x4ba6, get_unaligned((u16 *)(ptr + 13))); |
| 616 | ut_asserteq(0x9587, get_unaligned((u16 *)(ptr + 15))); |
| 617 | ut_asserteq(0x2af51913, get_unaligned((u32 *)(ptr + 17))); |
| 618 | ut_asserteq(0x6b96, get_unaligned((u16 *)(ptr + 21))); |
| 619 | |
| 620 | /* Try a bad UUID */ |
| 621 | ut_asserteq(-EINVAL, |
| 622 | acpigen_write_uuid(ctx, |
| 623 | "dbb8e3e6-5886-4ba6x8795-1319f52a966b")); |
| 624 | |
| 625 | free_context(&ctx); |
| 626 | |
| 627 | return 0; |
| 628 | } |
| 629 | DM_TEST(dm_test_acpi_uuid, 0); |
Simon Glass | 9c70e7e | 2020-07-07 13:11:59 -0600 | [diff] [blame] | 630 | |
| 631 | /* Test writing misc ACPI codes */ |
| 632 | static int dm_test_acpi_misc(struct unit_test_state *uts) |
| 633 | { |
| 634 | struct acpi_ctx *ctx; |
| 635 | const int flags = 3; |
| 636 | const int nargs = 4; |
| 637 | u8 *ptr; |
| 638 | |
| 639 | ut_assertok(alloc_context(&ctx)); |
| 640 | |
| 641 | ptr = acpigen_get_current(ctx); |
| 642 | acpigen_write_sleep(ctx, TEST_INT64); |
| 643 | ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 3))); |
| 644 | ptr += 11; |
| 645 | |
| 646 | acpigen_write_store(ctx); |
| 647 | ut_asserteq(STORE_OP, *ptr); |
| 648 | ptr++; |
| 649 | |
| 650 | acpigen_write_debug_string(ctx, TEST_STRING); |
| 651 | ut_asserteq_str(TEST_STRING, (char *)ptr + 2); |
| 652 | ptr += 2 + sizeof(TEST_STRING); |
| 653 | ut_asserteq(EXT_OP_PREFIX, ptr[0]); |
| 654 | ut_asserteq(DEBUG_OP, ptr[1]); |
| 655 | ptr += 2; |
| 656 | |
| 657 | acpigen_write_sta(ctx, flags); |
| 658 | ut_asserteq(METHOD_OP, ptr[0]); |
| 659 | ut_asserteq(11, acpi_test_get_length(ptr + 1)); |
| 660 | ut_asserteq_strn("_STA", (char *)ptr + 4); |
| 661 | ut_asserteq(0, ptr[8]); |
| 662 | ut_asserteq(RETURN_OP, ptr[9]); |
| 663 | ut_asserteq(BYTE_PREFIX, ptr[10]); |
| 664 | ut_asserteq(flags, ptr[11]); |
| 665 | ptr += 12; |
| 666 | |
| 667 | acpigen_write_sleep(ctx, TEST_INT16); |
| 668 | ut_asserteq(SLEEP_OP, ptr[1]); |
| 669 | ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 3))); |
| 670 | ptr += 5; |
| 671 | |
| 672 | acpigen_write_method_serialized(ctx, "FRED", nargs); |
| 673 | ut_asserteq(METHOD_OP, ptr[0]); |
| 674 | ut_asserteq_strn("FRED", (char *)ptr + 4); |
| 675 | ut_asserteq(1 << 3 | nargs, ptr[8]); |
| 676 | ut_asserteq(1, ctx->ltop); /* method is unfinished */ |
| 677 | |
| 678 | ptr += 9; |
| 679 | acpigen_write_or(ctx, LOCAL0_OP, LOCAL1_OP, LOCAL2_OP); |
| 680 | acpigen_write_and(ctx, LOCAL3_OP, LOCAL4_OP, LOCAL5_OP); |
| 681 | acpigen_write_not(ctx, LOCAL6_OP, LOCAL7_OP); |
| 682 | ut_asserteq(OR_OP, ptr[0]); |
| 683 | ut_asserteq(LOCAL0_OP, ptr[1]); |
| 684 | ut_asserteq(LOCAL1_OP, ptr[2]); |
| 685 | ut_asserteq(LOCAL2_OP, ptr[3]); |
| 686 | |
| 687 | ptr += 4; |
| 688 | ut_asserteq(AND_OP, ptr[0]); |
| 689 | ut_asserteq(LOCAL3_OP, ptr[1]); |
| 690 | ut_asserteq(LOCAL4_OP, ptr[2]); |
| 691 | ut_asserteq(LOCAL5_OP, ptr[3]); |
| 692 | |
| 693 | ptr += 4; |
| 694 | ut_asserteq(NOT_OP, ptr[0]); |
| 695 | ut_asserteq(LOCAL6_OP, ptr[1]); |
| 696 | ut_asserteq(LOCAL7_OP, ptr[2]); |
| 697 | ptr += 3; |
| 698 | ut_asserteq_ptr(ptr, ctx->current); |
| 699 | |
| 700 | free_context(&ctx); |
| 701 | |
| 702 | return 0; |
| 703 | } |
| 704 | DM_TEST(dm_test_acpi_misc, 0); |
Simon Glass | f9189d5 | 2020-07-07 13:12:00 -0600 | [diff] [blame] | 705 | |
| 706 | /* Test writing an ACPI power resource */ |
| 707 | static int dm_test_acpi_power_res(struct unit_test_state *uts) |
| 708 | { |
| 709 | const char *const states[] = { "_PR0", "_PR3" }; |
| 710 | const char *name = "PRIC"; |
| 711 | const int level = 3; |
| 712 | const int order = 2; |
| 713 | struct acpi_ctx *ctx; |
| 714 | u8 *ptr; |
| 715 | |
| 716 | ut_assertok(alloc_context(&ctx)); |
| 717 | |
| 718 | ptr = acpigen_get_current(ctx); |
| 719 | |
| 720 | /* PowerResource (PRIC, 0, 0) */ |
| 721 | acpigen_write_power_res(ctx, name, level, order, states, |
| 722 | ARRAY_SIZE(states)); |
| 723 | ut_asserteq(0x28, acpigen_get_current(ctx) - ptr); |
| 724 | ut_asserteq(NAME_OP, ptr[0]); |
| 725 | ut_asserteq_strn(states[0], (char *)ptr + 1); |
| 726 | ut_asserteq(8, acpi_test_get_length(ptr + 6)); |
| 727 | ut_asserteq_strn(name, (char *)ptr + 0xa); |
| 728 | |
| 729 | ut_asserteq_strn(states[1], (char *)ptr + 0xf); |
| 730 | ut_asserteq(8, acpi_test_get_length(ptr + 0x14)); |
| 731 | ut_asserteq_strn(name, (char *)ptr + 0x18); |
| 732 | |
| 733 | ut_asserteq(POWER_RES_OP, ptr[0x1d]); |
| 734 | ut_asserteq_strn(name, (char *)ptr + 0x21); |
| 735 | ut_asserteq(level, ptr[0x25]); |
| 736 | ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26))); |
| 737 | |
| 738 | /* The length is not set - caller must use acpigen_pop_len() */ |
| 739 | ut_asserteq(1, ctx->ltop); |
| 740 | |
| 741 | free_context(&ctx); |
| 742 | |
| 743 | return 0; |
| 744 | } |
| 745 | DM_TEST(dm_test_acpi_power_res, 0); |
Simon Glass | f8054dd | 2020-07-07 13:12:01 -0600 | [diff] [blame] | 746 | |
| 747 | /* Test writing ACPI code to toggle a GPIO */ |
| 748 | static int dm_test_acpi_gpio_toggle(struct unit_test_state *uts) |
| 749 | { |
| 750 | const uint addr = 0x80012; |
| 751 | const int txbit = BIT(2); |
| 752 | struct gpio_desc desc; |
| 753 | struct acpi_gpio gpio; |
| 754 | struct acpi_ctx *ctx; |
| 755 | struct udevice *dev; |
| 756 | u8 *ptr; |
| 757 | |
| 758 | ut_assertok(alloc_context(&ctx)); |
| 759 | |
| 760 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 761 | ut_asserteq_str("a-test", dev->name); |
| 762 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0)); |
| 763 | ut_assertok(gpio_get_acpi(&desc, &gpio)); |
| 764 | |
| 765 | /* Spot-check the results - see sb_gpio_get_acpi() */ |
| 766 | ptr = acpigen_get_current(ctx); |
| 767 | acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0", |
| 768 | &gpio, true); |
| 769 | acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0", |
| 770 | &gpio, false); |
| 771 | |
| 772 | /* Since this GPIO is active low, we expect it to be cleared here */ |
| 773 | ut_asserteq(STORE_OP, *ptr); |
| 774 | ut_asserteq_strn("_SB_GPC0", (char *)ptr + 3); |
| 775 | ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0xc))); |
| 776 | ut_asserteq(LOCAL5_OP, ptr[0x10]); |
| 777 | |
| 778 | ut_asserteq(STORE_OP, ptr[0x11]); |
| 779 | ut_asserteq(BYTE_PREFIX, ptr[0x12]); |
| 780 | ut_asserteq(txbit, ptr[0x13]); |
| 781 | ut_asserteq(LOCAL0_OP, ptr[0x14]); |
| 782 | |
| 783 | ut_asserteq(NOT_OP, ptr[0x15]); |
| 784 | ut_asserteq(LOCAL0_OP, ptr[0x16]); |
| 785 | ut_asserteq(LOCAL6_OP, ptr[0x17]); |
| 786 | ut_asserteq(AND_OP, ptr[0x18]); |
| 787 | ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x1e); |
| 788 | ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x27))); |
| 789 | ut_asserteq(LOCAL5_OP, ptr[0x2b]); |
| 790 | |
| 791 | /* Now the second one, which should be set */ |
| 792 | ut_asserteq_strn("_SB_GPC0", (char *)ptr + 0x2f); |
| 793 | ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x38))); |
| 794 | ut_asserteq(LOCAL5_OP, ptr[0x3c]); |
| 795 | |
| 796 | ut_asserteq(STORE_OP, ptr[0x3d]); |
| 797 | |
| 798 | ut_asserteq(OR_OP, ptr[0x41]); |
| 799 | ut_asserteq(LOCAL0_OP, ptr[0x43]); |
| 800 | ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x47); |
| 801 | ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x50))); |
| 802 | ut_asserteq(LOCAL5_OP, ptr[0x54]); |
| 803 | ut_asserteq(0x55, acpigen_get_current(ctx) - ptr); |
| 804 | |
| 805 | free_context(&ctx); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | DM_TEST(dm_test_acpi_gpio_toggle, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | 740630b | 2020-07-07 13:12:02 -0600 | [diff] [blame] | 810 | |
| 811 | /* Test writing ACPI code to output power-sequence info */ |
| 812 | static int dm_test_acpi_power_seq(struct unit_test_state *uts) |
| 813 | { |
| 814 | struct gpio_desc reset, enable, stop; |
| 815 | const uint addr = 0xc00dc, addr_act_low = 0x80012; |
| 816 | const int txbit = BIT(2); |
| 817 | struct acpi_ctx *ctx; |
| 818 | struct udevice *dev; |
| 819 | u8 *ptr; |
| 820 | |
| 821 | ut_assertok(acpi_test_alloc_context_size(&ctx, 400)); |
| 822 | |
| 823 | ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev)); |
| 824 | ut_asserteq_str("a-test", dev->name); |
| 825 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 0, &reset, 0)); |
| 826 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 1, &enable, 0)); |
| 827 | ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &stop, 0)); |
| 828 | ptr = acpigen_get_current(ctx); |
| 829 | |
| 830 | ut_assertok(acpi_device_add_power_res(ctx, txbit, "\\_SB.GPC0", |
| 831 | "\\_SB.SPC0", &reset, 2, 3, |
| 832 | &enable, 4, 5, &stop, 6, 7)); |
| 833 | ut_asserteq(0x186, acpigen_get_current(ctx) - ptr); |
| 834 | ut_asserteq_strn("PRIC", (char *)ptr + 0x18); |
| 835 | |
| 836 | /* First the 'ON' sequence - spot check */ |
| 837 | ut_asserteq_strn("_ON_", (char *)ptr + 0x38); |
| 838 | |
| 839 | /* reset set */ |
| 840 | ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x49))); |
| 841 | ut_asserteq(OR_OP, ptr[0x52]); |
| 842 | |
| 843 | /* enable set */ |
| 844 | ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x72))); |
| 845 | ut_asserteq(OR_OP, ptr[0x7b]); |
| 846 | |
| 847 | /* reset clear */ |
| 848 | ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x9f))); |
| 849 | ut_asserteq(NOT_OP, ptr[0xa8]); |
| 850 | |
| 851 | /* stop set (disable, active low) */ |
| 852 | ut_asserteq(addr_act_low + stop.offset, |
| 853 | get_unaligned((u32 *)(ptr + 0xcf))); |
| 854 | ut_asserteq(OR_OP, ptr[0xd8]); |
| 855 | |
| 856 | /* Now the 'OFF' sequence */ |
| 857 | ut_asserteq_strn("_OFF", (char *)ptr + 0xf4); |
| 858 | |
| 859 | /* stop clear (enable, active low) */ |
| 860 | ut_asserteq(addr_act_low + stop.offset, |
| 861 | get_unaligned((u32 *)(ptr + 0x105))); |
| 862 | ut_asserteq(NOT_OP, ptr[0x10e]); |
| 863 | |
| 864 | /* reset clear */ |
| 865 | ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x135))); |
| 866 | ut_asserteq(OR_OP, ptr[0x13e]); |
| 867 | |
| 868 | /* enable clear */ |
| 869 | ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x162))); |
| 870 | ut_asserteq(NOT_OP, ptr[0x16b]); |
| 871 | |
| 872 | free_context(&ctx); |
| 873 | |
| 874 | return 0; |
| 875 | } |
Simon Glass | 740630b | 2020-07-07 13:12:02 -0600 | [diff] [blame] | 876 | DM_TEST(dm_test_acpi_power_seq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | fea9651 | 2020-07-07 21:32:09 -0600 | [diff] [blame] | 877 | |
| 878 | /* Test writing values */ |
| 879 | static int dm_test_acpi_write_values(struct unit_test_state *uts) |
| 880 | { |
| 881 | struct acpi_ctx *ctx; |
| 882 | u8 *ptr; |
| 883 | |
| 884 | ut_assertok(alloc_context(&ctx)); |
| 885 | ptr = acpigen_get_current(ctx); |
| 886 | |
| 887 | acpigen_write_zero(ctx); |
| 888 | acpigen_write_one(ctx); |
| 889 | acpigen_write_byte(ctx, TEST_INT8); |
| 890 | acpigen_write_word(ctx, TEST_INT16); |
| 891 | acpigen_write_dword(ctx, TEST_INT32); |
| 892 | acpigen_write_qword(ctx, TEST_INT64); |
| 893 | |
| 894 | ut_asserteq(ZERO_OP, *ptr++); |
| 895 | |
| 896 | ut_asserteq(ONE_OP, *ptr++); |
| 897 | |
| 898 | ut_asserteq(BYTE_PREFIX, *ptr++); |
| 899 | ut_asserteq(TEST_INT8, *ptr++); |
| 900 | |
| 901 | ut_asserteq(WORD_PREFIX, *ptr++); |
| 902 | ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr)); |
| 903 | ptr += 2; |
| 904 | |
| 905 | ut_asserteq(DWORD_PREFIX, *ptr++); |
| 906 | ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr)); |
| 907 | ptr += 4; |
| 908 | |
| 909 | ut_asserteq(QWORD_PREFIX, *ptr++); |
| 910 | ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr)); |
| 911 | ptr += 8; |
| 912 | |
| 913 | ut_asserteq_ptr(ptr, ctx->current); |
| 914 | |
| 915 | free_context(&ctx); |
| 916 | |
| 917 | return 0; |
| 918 | } |
| 919 | DM_TEST(dm_test_acpi_write_values, 0); |
Simon Glass | 82659cc | 2020-07-07 21:32:10 -0600 | [diff] [blame] | 920 | |
| 921 | /* Test writing a scope */ |
| 922 | static int dm_test_acpi_scope(struct unit_test_state *uts) |
| 923 | { |
| 924 | char buf[ACPI_PATH_MAX]; |
| 925 | struct acpi_ctx *ctx; |
| 926 | struct udevice *dev; |
| 927 | u8 *ptr; |
| 928 | |
| 929 | ut_assertok(alloc_context(&ctx)); |
| 930 | ptr = acpigen_get_current(ctx); |
| 931 | |
| 932 | ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev)); |
| 933 | ut_assertok(acpi_device_path(dev, buf, sizeof(buf))); |
| 934 | acpigen_write_scope(ctx, buf); |
| 935 | acpigen_pop_len(ctx); |
| 936 | |
| 937 | ut_asserteq(SCOPE_OP, *ptr++); |
| 938 | ut_asserteq(13, acpi_test_get_length(ptr)); |
| 939 | ptr += 3; |
| 940 | ut_asserteq(ROOT_PREFIX, *ptr++); |
| 941 | ut_asserteq(DUAL_NAME_PREFIX, *ptr++); |
| 942 | ut_asserteq_strn("_SB_" ACPI_TEST_DEV_NAME, (char *)ptr); |
| 943 | ptr += 8; |
| 944 | ut_asserteq_ptr(ptr, ctx->current); |
| 945 | |
| 946 | free_context(&ctx); |
| 947 | |
| 948 | return 0; |
| 949 | } |
| 950 | DM_TEST(dm_test_acpi_scope, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT); |
Simon Glass | d7d631d | 2020-07-07 21:32:11 -0600 | [diff] [blame] | 951 | |
| 952 | /* Test writing a resource template */ |
| 953 | static int dm_test_acpi_resource_template(struct unit_test_state *uts) |
| 954 | { |
| 955 | struct acpi_gen_regaddr addr; |
| 956 | struct acpi_ctx *ctx; |
| 957 | u8 *ptr; |
| 958 | |
| 959 | ut_assertok(alloc_context(&ctx)); |
| 960 | ptr = acpigen_get_current(ctx); |
| 961 | |
| 962 | addr.space_id = ACPI_ADDRESS_SPACE_EC; |
| 963 | addr.bit_width = 32; |
| 964 | addr.bit_offset = 8; |
| 965 | addr.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS; |
| 966 | addr.addrl = TEST_INT64 & 0xffffffff; |
| 967 | addr.addrh = TEST_INT64 >> 32; |
| 968 | acpigen_write_register_resource(ctx, &addr); |
| 969 | |
| 970 | ut_asserteq(BUFFER_OP, *ptr++); |
| 971 | ut_asserteq(0x17, acpi_test_get_length(ptr)); |
| 972 | ptr += 3; |
| 973 | ut_asserteq(WORD_PREFIX, *ptr++); |
| 974 | ut_asserteq(0x11, get_unaligned((u16 *)ptr)); |
| 975 | ptr += 2; |
| 976 | ut_asserteq(ACPI_DESCRIPTOR_REGISTER, *ptr++); |
| 977 | ut_asserteq(0xc, *ptr++); |
| 978 | ut_asserteq(0, *ptr++); |
| 979 | ut_asserteq(ACPI_ADDRESS_SPACE_EC, *ptr++); |
| 980 | ut_asserteq(32, *ptr++); |
| 981 | ut_asserteq(8, *ptr++); |
| 982 | ut_asserteq(ACPI_ACCESS_SIZE_DWORD_ACCESS, *ptr++); |
| 983 | ut_asserteq(TEST_INT64 & 0xffffffff, get_unaligned((u32 *)ptr)); |
| 984 | ptr += 4; |
| 985 | ut_asserteq(TEST_INT64 >> 32, get_unaligned((u32 *)ptr)); |
| 986 | ptr += 4; |
| 987 | ut_asserteq(ACPI_END_TAG, *ptr++); |
| 988 | ut_asserteq(0x00, *ptr++); |
| 989 | ut_asserteq_ptr(ptr, ctx->current); |
| 990 | |
| 991 | free_context(&ctx); |
| 992 | |
| 993 | return 0; |
| 994 | } |
| 995 | DM_TEST(dm_test_acpi_resource_template, 0); |
Simon Glass | 91c2f9c | 2020-07-07 21:32:14 -0600 | [diff] [blame^] | 996 | |
| 997 | /* Test writing a device */ |
| 998 | static int dm_test_acpi_device(struct unit_test_state *uts) |
| 999 | { |
| 1000 | struct acpi_ctx *ctx; |
| 1001 | u8 *ptr; |
| 1002 | |
| 1003 | ut_assertok(alloc_context(&ctx)); |
| 1004 | ptr = acpigen_get_current(ctx); |
| 1005 | |
| 1006 | acpigen_write_device(ctx, "\\_SB." ACPI_TEST_DEV_NAME); |
| 1007 | acpigen_pop_len(ctx); |
| 1008 | |
| 1009 | ut_asserteq(EXT_OP_PREFIX, *ptr++); |
| 1010 | ut_asserteq(DEVICE_OP, *ptr++); |
| 1011 | ut_asserteq(0xd, acpi_test_get_length(ptr)); |
| 1012 | ptr += 3; |
| 1013 | ut_asserteq(ROOT_PREFIX, *ptr++); |
| 1014 | ut_asserteq(DUAL_NAME_PREFIX, *ptr++); |
| 1015 | ptr += 8; |
| 1016 | ut_asserteq_ptr(ptr, ctx->current); |
| 1017 | |
| 1018 | free_context(&ctx); |
| 1019 | |
| 1020 | return 0; |
| 1021 | } |
| 1022 | DM_TEST(dm_test_acpi_device, 0); |