1 // SPDX-License-Identifier: GPL-2.0+
3 * Tests for ACPI code generation
5 * Copyright 2019 Google LLC
6 * Written by Simon Glass <sjg@chromium.org>
14 #include <acpi/acpigen.h>
15 #include <acpi/acpi_device.h>
16 #include <acpi/acpi_table.h>
18 #include <asm/unaligned.h>
21 #include <dm/uclass-internal.h>
25 /* Maximum size of the ACPI context needed for most tests */
26 #define ACPI_CONTEXT_SIZE 150
28 #define TEST_STRING "frogmore"
29 #define TEST_STRING2 "ranch"
30 #define TEST_STREAM2 "\xfa\xde"
32 #define TEST_INT8 0x7d
33 #define TEST_INT16 0x2345
34 #define TEST_INT32 0x12345678
35 #define TEST_INT64 0x4567890123456
37 int acpi_test_alloc_context_size(struct acpi_ctx **ctxp, int size)
42 ctx = malloc(sizeof(*ctx));
45 ctx->base = malloc(size);
51 ctx->current = ctx->base;
57 int acpi_test_get_length(u8 *ptr)
62 return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
65 static int alloc_context(struct acpi_ctx **ctxp)
67 return acpi_test_alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
70 static void free_context(struct acpi_ctx **ctxp)
77 /* Test emitting simple types and acpigen_get_current() */
78 static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
83 ut_assertok(alloc_context(&ctx));
85 ptr = acpigen_get_current(ctx);
86 acpigen_emit_byte(ctx, 0x23);
87 ut_asserteq(1, acpigen_get_current(ctx) - ptr);
88 ut_asserteq(0x23, *(u8 *)ptr);
90 acpigen_emit_word(ctx, 0x1234);
91 ut_asserteq(3, acpigen_get_current(ctx) - ptr);
92 ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1)));
94 acpigen_emit_dword(ctx, 0x87654321);
95 ut_asserteq(7, acpigen_get_current(ctx) - ptr);
96 ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3)));
102 DM_TEST(dm_test_acpi_emit_simple, 0);
104 /* Test emitting a stream */
105 static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
107 struct acpi_ctx *ctx;
110 ut_assertok(alloc_context(&ctx));
112 ptr = acpigen_get_current(ctx);
113 acpigen_emit_stream(ctx, TEST_STREAM2, 2);
114 ut_asserteq(2, acpigen_get_current(ctx) - ptr);
115 ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
116 ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
122 DM_TEST(dm_test_acpi_emit_stream, 0);
124 /* Test emitting a string */
125 static int dm_test_acpi_emit_string(struct unit_test_state *uts)
127 struct acpi_ctx *ctx;
130 ut_assertok(alloc_context(&ctx));
132 ptr = acpigen_get_current(ctx);
133 acpigen_emit_string(ctx, TEST_STRING);
134 ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
135 ut_asserteq_str(TEST_STRING, (char *)ptr);
141 DM_TEST(dm_test_acpi_emit_string, 0);
143 /* Test emitting an interrupt descriptor */
144 static int dm_test_acpi_interrupt(struct unit_test_state *uts)
146 struct acpi_ctx *ctx;
151 ut_assertok(alloc_context(&ctx));
153 ptr = acpigen_get_current(ctx);
155 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
156 ut_assertok(irq_get_by_index(dev, 0, &irq));
158 /* See a-test, property interrupts-extended in the device tree */
159 ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq));
160 ut_asserteq(9, acpigen_get_current(ctx) - ptr);
161 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
162 ut_asserteq(6, get_unaligned((u16 *)(ptr + 1)));
163 ut_asserteq(0x19, ptr[3]);
164 ut_asserteq(1, ptr[4]);
165 ut_asserteq(3, get_unaligned((u32 *)(ptr + 5)));
171 DM_TEST(dm_test_acpi_interrupt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
173 /* Test emitting a GPIO descriptor */
174 static int dm_test_acpi_gpio(struct unit_test_state *uts)
176 struct gpio_desc desc;
177 struct acpi_ctx *ctx;
181 ut_assertok(alloc_context(&ctx));
183 ptr = acpigen_get_current(ctx);
185 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
186 ut_asserteq_str("a-test", dev->name);
187 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
189 /* This should write GPIO pin 4 (see device tree test.dts ) */
190 ut_asserteq(4, acpi_device_write_gpio_desc(ctx, &desc));
191 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
192 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
193 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
194 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
195 ut_asserteq(ACPI_GPIO_TYPE_IO, ptr[4]);
196 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
197 ut_asserteq(9, get_unaligned((u16 *)(ptr + 7)));
198 ut_asserteq(ACPI_GPIO_PULL_UP, ptr[9]);
199 ut_asserteq(1234, get_unaligned((u16 *)(ptr + 10)));
200 ut_asserteq(0, get_unaligned((u16 *)(ptr + 12)));
201 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
202 ut_asserteq(0, ptr[16]);
203 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
204 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
205 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
208 ut_asserteq(4, get_unaligned((u16 *)(ptr + 23)));
210 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
216 DM_TEST(dm_test_acpi_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
218 /* Test emitting a GPIO descriptor with an interrupt */
219 static int dm_test_acpi_gpio_irq(struct unit_test_state *uts)
221 struct gpio_desc desc;
222 struct acpi_ctx *ctx;
226 ut_assertok(alloc_context(&ctx));
228 ptr = acpigen_get_current(ctx);
230 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
231 ut_asserteq_str("a-test", dev->name);
232 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
234 /* This should write GPIO pin 6 (see device tree test.dts ) */
235 ut_asserteq(6, acpi_device_write_gpio_desc(ctx, &desc));
236 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
237 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
238 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
239 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
240 ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, ptr[4]);
241 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
242 ut_asserteq(29, get_unaligned((u16 *)(ptr + 7)));
243 ut_asserteq(ACPI_GPIO_PULL_DOWN, ptr[9]);
244 ut_asserteq(0, get_unaligned((u16 *)(ptr + 10)));
245 ut_asserteq(4321, get_unaligned((u16 *)(ptr + 12)));
246 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
247 ut_asserteq(0, ptr[16]);
248 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
249 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
250 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
253 ut_asserteq(6, get_unaligned((u16 *)(ptr + 23)));
255 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
261 DM_TEST(dm_test_acpi_gpio_irq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
263 /* Test emitting either a GPIO or interrupt descriptor */
264 static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts)
266 struct acpi_ctx *ctx;
270 ut_assertok(alloc_context(&ctx));
272 ptr = acpigen_get_current(ctx);
274 /* This should produce an interrupt, even though it also has a GPIO */
275 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
276 ut_asserteq_str("a-test", dev->name);
277 ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev,
279 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
281 /* This has no interrupt so should produce a GPIO */
283 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev));
284 ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev,
286 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
288 /* This one has neither */
289 ptr = acpigen_get_current(ctx);
290 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
291 ut_asserteq_str("b-test", dev->name);
293 acpi_device_write_interrupt_or_gpio(ctx, dev,
300 DM_TEST(dm_test_acpi_interrupt_or_gpio,
301 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
303 /* Test emitting an I2C descriptor */
304 static int dm_test_acpi_i2c(struct unit_test_state *uts)
306 struct acpi_ctx *ctx;
310 ut_assertok(alloc_context(&ctx));
312 ptr = acpigen_get_current(ctx);
314 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
315 ut_asserteq(0x43, acpi_device_write_i2c_dev(ctx, dev));
316 ut_asserteq(28, acpigen_get_current(ctx) - ptr);
317 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
318 ut_asserteq(25, get_unaligned((u16 *)(ptr + 1)));
319 ut_asserteq(ACPI_I2C_SERIAL_BUS_REVISION_ID, ptr[3]);
320 ut_asserteq(0, ptr[4]);
321 ut_asserteq(ACPI_SERIAL_BUS_TYPE_I2C, ptr[5]);
322 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
323 ut_asserteq(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
324 ut_asserteq(6, get_unaligned((u16 *)(ptr + 10)));
325 ut_asserteq(100000, get_unaligned((u32 *)(ptr + 12)));
326 ut_asserteq(0x43, get_unaligned((u16 *)(ptr + 16)));
327 ut_asserteq_str("\\_SB.I2C0", (char *)ptr + 18);
333 DM_TEST(dm_test_acpi_i2c, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
335 /* Test emitting a SPI descriptor */
336 static int dm_test_acpi_spi(struct unit_test_state *uts)
338 struct acpi_ctx *ctx;
342 ut_assertok(alloc_context(&ctx));
344 ptr = acpigen_get_current(ctx);
346 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
347 ut_assertok(acpi_device_write_spi_dev(ctx, dev));
348 ut_asserteq(31, acpigen_get_current(ctx) - ptr);
349 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
350 ut_asserteq(28, get_unaligned((u16 *)(ptr + 1)));
351 ut_asserteq(ACPI_SPI_SERIAL_BUS_REVISION_ID, ptr[3]);
352 ut_asserteq(0, ptr[4]);
353 ut_asserteq(ACPI_SERIAL_BUS_TYPE_SPI, ptr[5]);
354 ut_asserteq(2, ptr[6]);
355 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
356 ut_asserteq(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
357 ut_asserteq(9, get_unaligned((u16 *)(ptr + 10)));
358 ut_asserteq(40000000, get_unaligned((u32 *)(ptr + 12)));
359 ut_asserteq(8, ptr[16]);
360 ut_asserteq(0, ptr[17]);
361 ut_asserteq(0, ptr[18]);
362 ut_asserteq(0, get_unaligned((u16 *)(ptr + 19)));
363 ut_asserteq_str("\\_SB.SPI0", (char *)ptr + 21);
369 DM_TEST(dm_test_acpi_spi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
371 /* Test emitting a length */
372 static int dm_test_acpi_len(struct unit_test_state *uts)
374 const int size = 0xc0000;
375 struct acpi_ctx *ctx;
379 ut_assertok(acpi_test_alloc_context_size(&ctx, size));
381 ptr = acpigen_get_current(ctx);
383 /* Write a byte and a 3-byte length */
384 acpigen_write_len_f(ctx);
385 acpigen_emit_byte(ctx, 0x23);
386 acpigen_pop_len(ctx);
387 ut_asserteq(1 + 3, acpi_test_get_length(ptr));
389 /* Write 200 bytes so we need two length bytes */
391 acpigen_write_len_f(ctx);
392 for (i = 0; i < 200; i++)
393 acpigen_emit_byte(ctx, 0x23);
394 acpigen_pop_len(ctx);
395 ut_asserteq(200 + 3, acpi_test_get_length(ptr));
397 /* Write 40KB so we need three length bytes */
399 acpigen_write_len_f(ctx);
400 for (i = 0; i < 40000; i++)
401 acpigen_emit_byte(ctx, 0x23);
402 acpigen_pop_len(ctx);
403 ut_asserteq(40000 + 3, acpi_test_get_length(ptr));
409 DM_TEST(dm_test_acpi_len, 0);
411 /* Test writing a package */
412 static int dm_test_acpi_package(struct unit_test_state *uts)
414 struct acpi_ctx *ctx;
418 ut_assertok(alloc_context(&ctx));
420 ptr = acpigen_get_current(ctx);
422 num_elements = acpigen_write_package(ctx, 3);
423 ut_asserteq_ptr(num_elements, ptr + 4);
425 /* For ease of testing, just emit a byte, not valid package contents */
426 acpigen_emit_byte(ctx, 0x23);
427 acpigen_pop_len(ctx);
428 ut_asserteq(PACKAGE_OP, ptr[0]);
429 ut_asserteq(5, acpi_test_get_length(ptr + 1));
430 ut_asserteq(3, ptr[4]);
436 DM_TEST(dm_test_acpi_package, 0);
438 /* Test writing an integer */
439 static int dm_test_acpi_integer(struct unit_test_state *uts)
441 struct acpi_ctx *ctx;
444 ut_assertok(alloc_context(&ctx));
446 ptr = acpigen_get_current(ctx);
448 acpigen_write_integer(ctx, 0);
449 acpigen_write_integer(ctx, 1);
450 acpigen_write_integer(ctx, TEST_INT8);
451 acpigen_write_integer(ctx, TEST_INT16);
452 acpigen_write_integer(ctx, TEST_INT32);
453 acpigen_write_integer(ctx, TEST_INT64);
455 ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr);
457 ut_asserteq(ZERO_OP, ptr[0]);
459 ut_asserteq(ONE_OP, ptr[1]);
461 ut_asserteq(BYTE_PREFIX, ptr[2]);
462 ut_asserteq(TEST_INT8, ptr[3]);
464 ut_asserteq(WORD_PREFIX, ptr[4]);
465 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5)));
467 ut_asserteq(DWORD_PREFIX, ptr[7]);
468 ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8)));
470 ut_asserteq(QWORD_PREFIX, ptr[12]);
471 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13)));
477 DM_TEST(dm_test_acpi_integer, 0);
479 /* Test writing a string */
480 static int dm_test_acpi_string(struct unit_test_state *uts)
482 struct acpi_ctx *ctx;
485 ut_assertok(alloc_context(&ctx));
487 ptr = acpigen_get_current(ctx);
489 acpigen_write_string(ctx, TEST_STRING);
490 acpigen_write_string(ctx, TEST_STRING2);
492 ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2),
493 acpigen_get_current(ctx) - ptr);
494 ut_asserteq(STRING_PREFIX, ptr[0]);
495 ut_asserteq_str(TEST_STRING, (char *)ptr + 1);
496 ptr += 1 + sizeof(TEST_STRING);
497 ut_asserteq(STRING_PREFIX, ptr[0]);
498 ut_asserteq_str(TEST_STRING2, (char *)ptr + 1);
504 DM_TEST(dm_test_acpi_string, 0);
506 /* Test writing a name */
507 static int dm_test_acpi_name(struct unit_test_state *uts)
509 struct acpi_ctx *ctx;
512 ut_assertok(alloc_context(&ctx));
514 ptr = acpigen_get_current(ctx);
517 * The names here are made up for testing the various cases. The
518 * grammar is in the ACPI spec 6.3 section 19.2.2
520 acpigen_write_name(ctx, "\\_SB");
521 acpigen_write_name(ctx, "\\_SB.I2C0");
522 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2");
523 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2.LONG");
524 acpigen_write_name(ctx, "^^^^SPI0.FLAS");
525 acpigen_write_name(ctx, "NN");
526 acpigen_write_name(ctx, "^AB.CD.D.EFG");
527 acpigen_write_name(ctx, "^^^^");
528 acpigen_write_name(ctx, "\\");
529 acpigen_write_name(ctx, "\\ABCD");
531 ut_asserteq(107, acpigen_get_current(ctx) - ptr);
532 ut_asserteq(NAME_OP, ptr[0]);
533 ut_asserteq_strn("\\_SB_", (char *)ptr + 1);
536 ut_asserteq(NAME_OP, ptr[0]);
537 ut_asserteq('\\', ptr[1]);
538 ut_asserteq(DUAL_NAME_PREFIX, ptr[2]);
539 ut_asserteq_strn("_SB_I2C0", (char *)ptr + 3);
542 ut_asserteq(NAME_OP, ptr[0]);
543 ut_asserteq('\\', ptr[1]);
544 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
545 ut_asserteq(3, ptr[3]);
546 ut_asserteq_strn("_SB_I2C0TPM2", (char *)ptr + 4);
549 ut_asserteq(NAME_OP, ptr[0]);
550 ut_asserteq('\\', ptr[1]);
551 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
552 ut_asserteq(4, ptr[3]);
553 ut_asserteq_strn("_SB_I2C0TPM2LONG", (char *)ptr + 4);
556 ut_asserteq(NAME_OP, ptr[0]);
557 ut_asserteq('^', ptr[1]);
558 ut_asserteq('^', ptr[2]);
559 ut_asserteq('^', ptr[3]);
560 ut_asserteq('^', ptr[4]);
561 ut_asserteq(DUAL_NAME_PREFIX, ptr[5]);
562 ut_asserteq_strn("SPI0FLAS", (char *)ptr + 6);
565 ut_asserteq(NAME_OP, ptr[0]);
566 ut_asserteq_strn("NN__", (char *)ptr + 1);
569 ut_asserteq(NAME_OP, ptr[0]);
570 ut_asserteq('^', ptr[1]);
571 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
572 ut_asserteq(4, ptr[3]);
573 ut_asserteq_strn("AB__CD__D___EFG_", (char *)ptr + 4);
576 ut_asserteq(NAME_OP, ptr[0]);
577 ut_asserteq('^', ptr[1]);
578 ut_asserteq('^', ptr[2]);
579 ut_asserteq('^', ptr[3]);
580 ut_asserteq('^', ptr[4]);
581 ut_asserteq(ZERO_OP, ptr[5]);
584 ut_asserteq(NAME_OP, ptr[0]);
585 ut_asserteq('\\', ptr[1]);
586 ut_asserteq(ZERO_OP, ptr[2]);
589 ut_asserteq(NAME_OP, ptr[0]);
590 ut_asserteq_strn("\\ABCD", (char *)ptr + 1);
597 DM_TEST(dm_test_acpi_name, 0);
599 /* Test writing a UUID */
600 static int dm_test_acpi_uuid(struct unit_test_state *uts)
602 struct acpi_ctx *ctx;
605 ut_assertok(alloc_context(&ctx));
607 ptr = acpigen_get_current(ctx);
609 ut_assertok(acpigen_write_uuid(ctx,
610 "dbb8e3e6-5886-4ba6-8795-1319f52a966b"));
611 ut_asserteq(23, acpigen_get_current(ctx) - ptr);
612 ut_asserteq(BUFFER_OP, ptr[0]);
613 ut_asserteq(22, acpi_test_get_length(ptr + 1));
614 ut_asserteq(0xdbb8e3e6, get_unaligned((u32 *)(ptr + 7)));
615 ut_asserteq(0x5886, get_unaligned((u16 *)(ptr + 11)));
616 ut_asserteq(0x4ba6, get_unaligned((u16 *)(ptr + 13)));
617 ut_asserteq(0x9587, get_unaligned((u16 *)(ptr + 15)));
618 ut_asserteq(0x2af51913, get_unaligned((u32 *)(ptr + 17)));
619 ut_asserteq(0x6b96, get_unaligned((u16 *)(ptr + 21)));
623 acpigen_write_uuid(ctx,
624 "dbb8e3e6-5886-4ba6x8795-1319f52a966b"));
630 DM_TEST(dm_test_acpi_uuid, 0);
632 /* Test writing misc ACPI codes */
633 static int dm_test_acpi_misc(struct unit_test_state *uts)
635 struct acpi_ctx *ctx;
640 ut_assertok(alloc_context(&ctx));
642 ptr = acpigen_get_current(ctx);
643 acpigen_write_sleep(ctx, TEST_INT64);
644 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 3)));
647 acpigen_write_store(ctx);
648 ut_asserteq(STORE_OP, *ptr);
651 acpigen_write_debug_string(ctx, TEST_STRING);
652 ut_asserteq_str(TEST_STRING, (char *)ptr + 2);
653 ptr += 2 + sizeof(TEST_STRING);
654 ut_asserteq(EXT_OP_PREFIX, ptr[0]);
655 ut_asserteq(DEBUG_OP, ptr[1]);
658 acpigen_write_sta(ctx, flags);
659 ut_asserteq(METHOD_OP, ptr[0]);
660 ut_asserteq(11, acpi_test_get_length(ptr + 1));
661 ut_asserteq_strn("_STA", (char *)ptr + 4);
662 ut_asserteq(0, ptr[8]);
663 ut_asserteq(RETURN_OP, ptr[9]);
664 ut_asserteq(BYTE_PREFIX, ptr[10]);
665 ut_asserteq(flags, ptr[11]);
668 acpigen_write_sleep(ctx, TEST_INT16);
669 ut_asserteq(SLEEP_OP, ptr[1]);
670 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 3)));
673 acpigen_write_method_serialized(ctx, "FRED", nargs);
674 ut_asserteq(METHOD_OP, ptr[0]);
675 ut_asserteq_strn("FRED", (char *)ptr + 4);
676 ut_asserteq(1 << 3 | nargs, ptr[8]);
677 ut_asserteq(1, ctx->ltop); /* method is unfinished */
680 acpigen_write_or(ctx, LOCAL0_OP, LOCAL1_OP, LOCAL2_OP);
681 acpigen_write_and(ctx, LOCAL3_OP, LOCAL4_OP, LOCAL5_OP);
682 acpigen_write_not(ctx, LOCAL6_OP, LOCAL7_OP);
683 ut_asserteq(OR_OP, ptr[0]);
684 ut_asserteq(LOCAL0_OP, ptr[1]);
685 ut_asserteq(LOCAL1_OP, ptr[2]);
686 ut_asserteq(LOCAL2_OP, ptr[3]);
689 ut_asserteq(AND_OP, ptr[0]);
690 ut_asserteq(LOCAL3_OP, ptr[1]);
691 ut_asserteq(LOCAL4_OP, ptr[2]);
692 ut_asserteq(LOCAL5_OP, ptr[3]);
695 ut_asserteq(NOT_OP, ptr[0]);
696 ut_asserteq(LOCAL6_OP, ptr[1]);
697 ut_asserteq(LOCAL7_OP, ptr[2]);
699 ut_asserteq_ptr(ptr, ctx->current);
705 DM_TEST(dm_test_acpi_misc, 0);
707 /* Test writing an ACPI power resource */
708 static int dm_test_acpi_power_res(struct unit_test_state *uts)
710 const char *const states[] = { "_PR0", "_PR3" };
711 const char *name = "PRIC";
714 struct acpi_ctx *ctx;
717 ut_assertok(alloc_context(&ctx));
719 ptr = acpigen_get_current(ctx);
721 /* PowerResource (PRIC, 0, 0) */
722 acpigen_write_power_res(ctx, name, level, order, states,
724 ut_asserteq(0x28, acpigen_get_current(ctx) - ptr);
725 ut_asserteq(NAME_OP, ptr[0]);
726 ut_asserteq_strn(states[0], (char *)ptr + 1);
727 ut_asserteq(8, acpi_test_get_length(ptr + 6));
728 ut_asserteq_strn(name, (char *)ptr + 0xa);
730 ut_asserteq_strn(states[1], (char *)ptr + 0xf);
731 ut_asserteq(8, acpi_test_get_length(ptr + 0x14));
732 ut_asserteq_strn(name, (char *)ptr + 0x18);
734 ut_asserteq(POWER_RES_OP, ptr[0x1d]);
735 ut_asserteq_strn(name, (char *)ptr + 0x21);
736 ut_asserteq(level, ptr[0x25]);
737 ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26)));
739 /* The length is not set - caller must use acpigen_pop_len() */
740 ut_asserteq(1, ctx->ltop);
746 DM_TEST(dm_test_acpi_power_res, 0);
748 /* Test writing ACPI code to toggle a GPIO */
749 static int dm_test_acpi_gpio_toggle(struct unit_test_state *uts)
751 const uint addr = 0x80012;
752 const int txbit = BIT(2);
753 struct gpio_desc desc;
754 struct acpi_gpio gpio;
755 struct acpi_ctx *ctx;
759 ut_assertok(alloc_context(&ctx));
761 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
762 ut_asserteq_str("a-test", dev->name);
763 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
764 ut_assertok(gpio_get_acpi(&desc, &gpio));
766 /* Spot-check the results - see sb_gpio_get_acpi() */
767 ptr = acpigen_get_current(ctx);
768 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
770 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
773 /* Since this GPIO is active low, we expect it to be cleared here */
774 ut_asserteq(STORE_OP, *ptr);
775 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 3);
776 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0xc)));
777 ut_asserteq(LOCAL5_OP, ptr[0x10]);
779 ut_asserteq(STORE_OP, ptr[0x11]);
780 ut_asserteq(BYTE_PREFIX, ptr[0x12]);
781 ut_asserteq(txbit, ptr[0x13]);
782 ut_asserteq(LOCAL0_OP, ptr[0x14]);
784 ut_asserteq(NOT_OP, ptr[0x15]);
785 ut_asserteq(LOCAL0_OP, ptr[0x16]);
786 ut_asserteq(LOCAL6_OP, ptr[0x17]);
787 ut_asserteq(AND_OP, ptr[0x18]);
788 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x1e);
789 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x27)));
790 ut_asserteq(LOCAL5_OP, ptr[0x2b]);
792 /* Now the second one, which should be set */
793 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 0x2f);
794 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x38)));
795 ut_asserteq(LOCAL5_OP, ptr[0x3c]);
797 ut_asserteq(STORE_OP, ptr[0x3d]);
799 ut_asserteq(OR_OP, ptr[0x41]);
800 ut_asserteq(LOCAL0_OP, ptr[0x43]);
801 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x47);
802 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x50)));
803 ut_asserteq(LOCAL5_OP, ptr[0x54]);
804 ut_asserteq(0x55, acpigen_get_current(ctx) - ptr);
810 DM_TEST(dm_test_acpi_gpio_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
812 /* Test writing ACPI code to output power-sequence info */
813 static int dm_test_acpi_power_seq(struct unit_test_state *uts)
815 struct gpio_desc reset, enable, stop;
816 const uint addr = 0xc00dc, addr_act_low = 0x80012;
817 const int txbit = BIT(2);
818 struct acpi_ctx *ctx;
822 ut_assertok(acpi_test_alloc_context_size(&ctx, 400));
824 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
825 ut_asserteq_str("a-test", dev->name);
826 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 0, &reset, 0));
827 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 1, &enable, 0));
828 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &stop, 0));
829 ptr = acpigen_get_current(ctx);
831 ut_assertok(acpi_device_add_power_res(ctx, txbit, "\\_SB.GPC0",
832 "\\_SB.SPC0", &reset, 2, 3,
833 &enable, 4, 5, &stop, 6, 7));
834 ut_asserteq(0x186, acpigen_get_current(ctx) - ptr);
835 ut_asserteq_strn("PRIC", (char *)ptr + 0x18);
837 /* First the 'ON' sequence - spot check */
838 ut_asserteq_strn("_ON_", (char *)ptr + 0x38);
841 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x49)));
842 ut_asserteq(OR_OP, ptr[0x52]);
845 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x72)));
846 ut_asserteq(OR_OP, ptr[0x7b]);
849 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x9f)));
850 ut_asserteq(NOT_OP, ptr[0xa8]);
852 /* stop set (disable, active low) */
853 ut_asserteq(addr_act_low + stop.offset,
854 get_unaligned((u32 *)(ptr + 0xcf)));
855 ut_asserteq(OR_OP, ptr[0xd8]);
857 /* Now the 'OFF' sequence */
858 ut_asserteq_strn("_OFF", (char *)ptr + 0xf4);
860 /* stop clear (enable, active low) */
861 ut_asserteq(addr_act_low + stop.offset,
862 get_unaligned((u32 *)(ptr + 0x105)));
863 ut_asserteq(NOT_OP, ptr[0x10e]);
866 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x135)));
867 ut_asserteq(OR_OP, ptr[0x13e]);
870 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x162)));
871 ut_asserteq(NOT_OP, ptr[0x16b]);
877 DM_TEST(dm_test_acpi_power_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
879 /* Test writing values */
880 static int dm_test_acpi_write_values(struct unit_test_state *uts)
882 struct acpi_ctx *ctx;
885 ut_assertok(alloc_context(&ctx));
886 ptr = acpigen_get_current(ctx);
888 acpigen_write_zero(ctx);
889 acpigen_write_one(ctx);
890 acpigen_write_byte(ctx, TEST_INT8);
891 acpigen_write_word(ctx, TEST_INT16);
892 acpigen_write_dword(ctx, TEST_INT32);
893 acpigen_write_qword(ctx, TEST_INT64);
895 ut_asserteq(ZERO_OP, *ptr++);
897 ut_asserteq(ONE_OP, *ptr++);
899 ut_asserteq(BYTE_PREFIX, *ptr++);
900 ut_asserteq(TEST_INT8, *ptr++);
902 ut_asserteq(WORD_PREFIX, *ptr++);
903 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
906 ut_asserteq(DWORD_PREFIX, *ptr++);
907 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
910 ut_asserteq(QWORD_PREFIX, *ptr++);
911 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
914 ut_asserteq_ptr(ptr, ctx->current);
920 DM_TEST(dm_test_acpi_write_values, 0);
922 /* Test writing a scope */
923 static int dm_test_acpi_scope(struct unit_test_state *uts)
925 char buf[ACPI_PATH_MAX];
926 struct acpi_ctx *ctx;
930 ut_assertok(alloc_context(&ctx));
931 ptr = acpigen_get_current(ctx);
933 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
934 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
935 acpigen_write_scope(ctx, buf);
936 acpigen_pop_len(ctx);
938 ut_asserteq(SCOPE_OP, *ptr++);
939 ut_asserteq(13, acpi_test_get_length(ptr));
941 ut_asserteq(ROOT_PREFIX, *ptr++);
942 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
943 ut_asserteq_strn("_SB_" ACPI_TEST_DEV_NAME, (char *)ptr);
945 ut_asserteq_ptr(ptr, ctx->current);
951 DM_TEST(dm_test_acpi_scope, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
953 /* Test writing a resource template */
954 static int dm_test_acpi_resource_template(struct unit_test_state *uts)
956 struct acpi_gen_regaddr addr;
957 struct acpi_ctx *ctx;
960 ut_assertok(alloc_context(&ctx));
961 ptr = acpigen_get_current(ctx);
963 addr.space_id = ACPI_ADDRESS_SPACE_EC;
966 addr.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
967 addr.addrl = TEST_INT64 & 0xffffffff;
968 addr.addrh = TEST_INT64 >> 32;
969 acpigen_write_register_resource(ctx, &addr);
971 ut_asserteq(BUFFER_OP, *ptr++);
972 ut_asserteq(0x17, acpi_test_get_length(ptr));
974 ut_asserteq(WORD_PREFIX, *ptr++);
975 ut_asserteq(0x11, get_unaligned((u16 *)ptr));
977 ut_asserteq(ACPI_DESCRIPTOR_REGISTER, *ptr++);
978 ut_asserteq(0xc, *ptr++);
979 ut_asserteq(0, *ptr++);
980 ut_asserteq(ACPI_ADDRESS_SPACE_EC, *ptr++);
981 ut_asserteq(32, *ptr++);
982 ut_asserteq(8, *ptr++);
983 ut_asserteq(ACPI_ACCESS_SIZE_DWORD_ACCESS, *ptr++);
984 ut_asserteq(TEST_INT64 & 0xffffffff, get_unaligned((u32 *)ptr));
986 ut_asserteq(TEST_INT64 >> 32, get_unaligned((u32 *)ptr));
988 ut_asserteq(ACPI_END_TAG, *ptr++);
989 ut_asserteq(0x00, *ptr++);
990 ut_asserteq_ptr(ptr, ctx->current);
996 DM_TEST(dm_test_acpi_resource_template, 0);
998 /* Test writing a device */
999 static int dm_test_acpi_device(struct unit_test_state *uts)
1001 struct acpi_ctx *ctx;
1004 ut_assertok(alloc_context(&ctx));
1005 ptr = acpigen_get_current(ctx);
1007 acpigen_write_device(ctx, "\\_SB." ACPI_TEST_DEV_NAME);
1008 acpigen_pop_len(ctx);
1010 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1011 ut_asserteq(DEVICE_OP, *ptr++);
1012 ut_asserteq(0xd, acpi_test_get_length(ptr));
1014 ut_asserteq(ROOT_PREFIX, *ptr++);
1015 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
1017 ut_asserteq_ptr(ptr, ctx->current);
1023 DM_TEST(dm_test_acpi_device, 0);
1025 /* Test writing named values */
1026 static int dm_test_acpi_write_name(struct unit_test_state *uts)
1028 const char *name = "\\_SB." ACPI_TEST_DEV_NAME;
1029 struct acpi_ctx *ctx;
1032 ut_assertok(alloc_context(&ctx));
1033 ptr = acpigen_get_current(ctx);
1035 acpigen_write_name_zero(ctx, name);
1036 acpigen_write_name_one(ctx, name);
1037 acpigen_write_name_byte(ctx, name, TEST_INT8);
1038 acpigen_write_name_word(ctx, name, TEST_INT16);
1039 acpigen_write_name_dword(ctx, name, TEST_INT32);
1040 acpigen_write_name_qword(ctx, name, TEST_INT64);
1041 acpigen_write_name_integer(ctx, name, TEST_INT64 + 1);
1042 acpigen_write_name_string(ctx, name, "baldrick");
1043 acpigen_write_name_string(ctx, name, NULL);
1045 ut_asserteq(NAME_OP, *ptr++);
1046 ut_asserteq_strn("\\._SB_ABCD", (char *)ptr);
1048 ut_asserteq(ZERO_OP, *ptr++);
1050 ut_asserteq(NAME_OP, *ptr++);
1052 ut_asserteq(ONE_OP, *ptr++);
1054 ut_asserteq(NAME_OP, *ptr++);
1056 ut_asserteq(BYTE_PREFIX, *ptr++);
1057 ut_asserteq(TEST_INT8, *ptr++);
1059 ut_asserteq(NAME_OP, *ptr++);
1061 ut_asserteq(WORD_PREFIX, *ptr++);
1062 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
1065 ut_asserteq(NAME_OP, *ptr++);
1067 ut_asserteq(DWORD_PREFIX, *ptr++);
1068 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
1071 ut_asserteq(NAME_OP, *ptr++);
1073 ut_asserteq(QWORD_PREFIX, *ptr++);
1074 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
1077 ut_asserteq(NAME_OP, *ptr++);
1079 ut_asserteq(QWORD_PREFIX, *ptr++);
1080 ut_asserteq_64(TEST_INT64 + 1, get_unaligned((u64 *)ptr));
1083 ut_asserteq(NAME_OP, *ptr++);
1085 ut_asserteq(STRING_PREFIX, *ptr++);
1086 ut_asserteq_str("baldrick", (char *)ptr)
1089 ut_asserteq(NAME_OP, *ptr++);
1091 ut_asserteq(STRING_PREFIX, *ptr++);
1092 ut_asserteq('\0', *ptr++);
1094 ut_asserteq_ptr(ptr, ctx->current);
1100 DM_TEST(dm_test_acpi_write_name, 0);
1102 /* Test emitting a _PRW component */
1103 static int dm_test_acpi_write_prw(struct unit_test_state *uts)
1105 struct acpi_ctx *ctx;
1108 ut_assertok(alloc_context(&ctx));
1110 ptr = acpigen_get_current(ctx);
1111 acpigen_write_prw(ctx, 5, 3);
1112 ut_asserteq(NAME_OP, *ptr++);
1114 ut_asserteq_strn("_PRW", (char *)ptr);
1116 ut_asserteq(PACKAGE_OP, *ptr++);
1117 ut_asserteq(8, acpi_test_get_length(ptr));
1119 ut_asserteq(2, *ptr++);
1120 ut_asserteq(BYTE_PREFIX, *ptr++);
1121 ut_asserteq(5, *ptr++);
1122 ut_asserteq(BYTE_PREFIX, *ptr++);
1123 ut_asserteq(3, *ptr++);
1124 ut_asserteq_ptr(ptr, ctx->current);
1130 DM_TEST(dm_test_acpi_write_prw, 0);
1132 /* Test emitting writing conditionals */
1133 static int dm_test_acpi_write_cond(struct unit_test_state *uts)
1135 struct acpi_ctx *ctx;
1138 ut_assertok(alloc_context(&ctx));
1140 ptr = acpigen_get_current(ctx);
1141 acpigen_write_if(ctx);
1142 acpigen_pop_len(ctx);
1143 ut_asserteq(IF_OP, *ptr++);
1144 ut_asserteq(3, acpi_test_get_length(ptr));
1147 acpigen_write_else(ctx);
1148 acpigen_pop_len(ctx);
1149 ut_asserteq(ELSE_OP, *ptr++);
1150 ut_asserteq(3, acpi_test_get_length(ptr));
1153 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, 5);
1154 acpigen_pop_len(ctx);
1155 ut_asserteq(IF_OP, *ptr++);
1156 ut_asserteq(7, acpi_test_get_length(ptr));
1158 ut_asserteq(LEQUAL_OP, *ptr++);
1159 ut_asserteq(LOCAL1_OP, *ptr++);
1160 ut_asserteq(BYTE_PREFIX, *ptr++);
1161 ut_asserteq(5, *ptr++);
1163 ut_asserteq_ptr(ptr, ctx->current);
1169 DM_TEST(dm_test_acpi_write_cond, 0);
1171 /* Test emitting writing return values and ToBuffer/ToInteger */
1172 static int dm_test_acpi_write_return(struct unit_test_state *uts)
1174 int len = sizeof(TEST_STRING);
1175 struct acpi_ctx *ctx;
1178 ut_assertok(alloc_context(&ctx));
1180 ptr = acpigen_get_current(ctx);
1181 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
1182 ut_asserteq(TO_BUFFER_OP, *ptr++);
1183 ut_asserteq(ARG0_OP, *ptr++);
1184 ut_asserteq(LOCAL0_OP, *ptr++);
1186 acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP);
1187 ut_asserteq(TO_INTEGER_OP, *ptr++);
1188 ut_asserteq(ARG0_OP, *ptr++);
1189 ut_asserteq(LOCAL0_OP, *ptr++);
1191 acpigen_write_return_byte_buffer(ctx, (u8 *)TEST_STRING, len);
1192 ut_asserteq(RETURN_OP, *ptr++);
1193 ut_asserteq(BUFFER_OP, *ptr++);
1194 ut_asserteq(5 + len, acpi_test_get_length(ptr));
1196 ut_asserteq(BYTE_PREFIX, *ptr++);
1197 ut_asserteq(len, *ptr++);
1198 ut_asserteq_mem(TEST_STRING, ptr, len);
1201 acpigen_write_return_singleton_buffer(ctx, 123);
1203 ut_asserteq(RETURN_OP, *ptr++);
1204 ut_asserteq(BUFFER_OP, *ptr++);
1205 ut_asserteq(4 + len, acpi_test_get_length(ptr));
1207 ut_asserteq(ONE_OP, *ptr++);
1208 ut_asserteq(123, *ptr++);
1210 acpigen_write_return_byte(ctx, 43);
1211 ut_asserteq(RETURN_OP, *ptr++);
1212 ut_asserteq(BYTE_PREFIX, *ptr++);
1213 ut_asserteq(43, *ptr++);
1215 ut_asserteq_ptr(ptr, ctx->current);
1221 DM_TEST(dm_test_acpi_write_return, 0);
1223 /* Test emitting a DSM for an I2C HID */
1224 static int dm_test_acpi_write_i2c_dsm(struct unit_test_state *uts)
1226 char uuid_str[UUID_STR_LEN + 1];
1227 const int reg_offset = 0x20;
1228 struct acpi_ctx *ctx;
1231 ut_assertok(alloc_context(&ctx));
1233 ptr = acpigen_get_current(ctx);
1234 ut_assertok(acpi_device_write_dsm_i2c_hid(ctx, reg_offset));
1236 /* acpigen_write_dsm_start() */
1237 ut_asserteq(METHOD_OP, *ptr++);
1238 ut_asserteq(0x78, acpi_test_get_length(ptr));
1240 ut_asserteq_strn("_DSM", (char *)ptr);
1242 ut_asserteq(ACPI_METHOD_SERIALIZED_MASK | 4, *ptr++);
1244 ut_asserteq(TO_BUFFER_OP, *ptr++);
1245 ut_asserteq(ARG0_OP, *ptr++);
1246 ut_asserteq(LOCAL0_OP, *ptr++);
1248 /* acpigen_write_dsm_uuid_start() */
1249 ut_asserteq(IF_OP, *ptr++);
1250 ut_asserteq(0x65, acpi_test_get_length(ptr));
1252 ut_asserteq(LEQUAL_OP, *ptr++);
1253 ut_asserteq(LOCAL0_OP, *ptr++);
1255 ut_asserteq(BUFFER_OP, *ptr++);
1256 ut_asserteq(UUID_BIN_LEN + 6, acpi_test_get_length(ptr));
1258 ut_asserteq(WORD_PREFIX, *ptr++);
1259 ut_asserteq(UUID_BIN_LEN, get_unaligned((u16 *)ptr));
1261 uuid_bin_to_str(ptr, uuid_str, UUID_STR_FORMAT_GUID);
1262 ut_asserteq_str(ACPI_DSM_I2C_HID_UUID, uuid_str);
1263 ptr += UUID_BIN_LEN;
1265 ut_asserteq(TO_INTEGER_OP, *ptr++);
1266 ut_asserteq(ARG2_OP, *ptr++);
1267 ut_asserteq(LOCAL1_OP, *ptr++);
1269 /* acpigen_write_dsm_uuid_start_cond() */
1270 ut_asserteq(IF_OP, *ptr++);
1271 ut_asserteq(0x34, acpi_test_get_length(ptr));
1273 ut_asserteq(LEQUAL_OP, *ptr++);
1274 ut_asserteq(LOCAL1_OP, *ptr++);
1275 ut_asserteq(ZERO_OP, *ptr++);
1278 * See code from acpi_device_write_dsm_i2c_hid(). We don't check every
1281 ut_asserteq(TO_INTEGER_OP, *ptr++);
1282 ut_asserteq(ARG1_OP, *ptr++);
1283 ut_asserteq(LOCAL2_OP, *ptr++);
1285 ut_asserteq(IF_OP, *ptr++);
1286 ut_asserteq(0xd, acpi_test_get_length(ptr));
1288 ut_asserteq(LEQUAL_OP, *ptr++);
1289 ut_asserteq(LOCAL2_OP, *ptr++);
1290 ut_asserteq(ZERO_OP, *ptr++); /* function 0 */
1292 ut_asserteq(RETURN_OP, *ptr++);
1293 ut_asserteq(BUFFER_OP, *ptr++);
1296 ut_asserteq(ELSE_OP, *ptr++);
1299 ut_asserteq(IF_OP, *ptr++);
1300 ut_asserteq(0xd, acpi_test_get_length(ptr));
1302 ut_asserteq(LEQUAL_OP, *ptr++);
1303 ut_asserteq(LOCAL2_OP, *ptr++);
1304 ut_asserteq(ONE_OP, *ptr++);
1306 ut_asserteq(RETURN_OP, *ptr++);
1307 ut_asserteq(BUFFER_OP, *ptr++);
1310 ut_asserteq(ELSE_OP, *ptr++);
1313 ut_asserteq(RETURN_OP, *ptr++);
1314 ut_asserteq(BUFFER_OP, *ptr++);
1317 /* acpigen_write_dsm_uuid_start_cond() */
1318 ut_asserteq(IF_OP, *ptr++);
1319 ut_asserteq(9, acpi_test_get_length(ptr));
1321 ut_asserteq(LEQUAL_OP, *ptr++);
1322 ut_asserteq(LOCAL1_OP, *ptr++);
1323 ut_asserteq(ONE_OP, *ptr++); /* function 1 */
1325 ut_asserteq(RETURN_OP, *ptr++);
1326 ut_asserteq(BYTE_PREFIX, *ptr++);
1327 ut_asserteq(reg_offset, *ptr++);
1329 /* acpigen_write_dsm_uuid_end() */
1330 ut_asserteq(RETURN_OP, *ptr++);
1331 ut_asserteq(BUFFER_OP, *ptr++);
1334 /* acpigen_write_dsm_end */
1335 ut_asserteq(RETURN_OP, *ptr++);
1336 ut_asserteq(BUFFER_OP, *ptr++);
1339 ut_asserteq_ptr(ptr, ctx->current);
1345 DM_TEST(dm_test_acpi_write_i2c_dsm, 0);
1347 /* Test emitting a processor */
1348 static int dm_test_acpi_write_processor(struct unit_test_state *uts)
1350 const int cpuindex = 6;
1351 const u32 pblock_addr = 0x12345600;
1352 const u32 pblock_len = 0x60;
1353 struct acpi_ctx *ctx;
1356 ut_assertok(alloc_context(&ctx));
1358 ptr = acpigen_get_current(ctx);
1359 acpigen_write_processor(ctx, cpuindex, pblock_addr, pblock_len);
1360 acpigen_pop_len(ctx);
1362 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1363 ut_asserteq(PROCESSOR_OP, *ptr++);
1364 ut_asserteq(0x13, acpi_test_get_length(ptr));
1366 ut_asserteq_strn("\\._PR_CP06", (char *)ptr);
1368 ut_asserteq(cpuindex, *ptr++);
1369 ut_asserteq(pblock_addr, get_unaligned((u32 *)ptr));
1371 ut_asserteq(pblock_len, *ptr++);
1373 ut_asserteq_ptr(ptr, ctx->current);
1379 DM_TEST(dm_test_acpi_write_processor, 0);
1381 /* Test emitting a processor package */
1382 static int dm_test_acpi_write_processor_package(struct unit_test_state *uts)
1384 const int core_count = 3;
1385 struct acpi_ctx *ctx;
1388 ut_assertok(alloc_context(&ctx));
1390 ptr = acpigen_get_current(ctx);
1391 acpigen_write_processor_package(ctx, "XCPU", 0, core_count);
1393 ut_asserteq(NAME_OP, *ptr++);
1394 ut_asserteq_strn("XCPU", (char *)ptr);
1396 ut_asserteq(PACKAGE_OP, *ptr++);
1397 ptr += 3; /* skip length */
1398 ut_asserteq(core_count, *ptr++);
1400 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1402 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1404 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1407 ut_asserteq_ptr(ptr, ctx->current);
1413 DM_TEST(dm_test_acpi_write_processor_package, 0);
1415 /* Test emitting a processor notification package */
1416 static int dm_test_acpi_write_processor_cnot(struct unit_test_state *uts)
1418 const int core_count = 3;
1419 struct acpi_ctx *ctx;
1422 ut_assertok(alloc_context(&ctx));
1424 ptr = acpigen_get_current(ctx);
1425 acpigen_write_processor_cnot(ctx, core_count);
1427 ut_asserteq(METHOD_OP, *ptr++);
1428 ptr += 3; /* skip length */
1429 ut_asserteq_strn("\\._PR_CNOT", (char *)ptr);
1431 ut_asserteq(1, *ptr++);
1433 ut_asserteq(NOTIFY_OP, *ptr++);
1434 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1436 ut_asserteq(ARG0_OP, *ptr++);
1437 ut_asserteq(NOTIFY_OP, *ptr++);
1438 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1440 ut_asserteq(ARG0_OP, *ptr++);
1441 ut_asserteq(NOTIFY_OP, *ptr++);
1442 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1444 ut_asserteq(ARG0_OP, *ptr++);
1446 ut_asserteq_ptr(ptr, ctx->current);
1452 DM_TEST(dm_test_acpi_write_processor_cnot, 0);
1454 /* Test acpigen_write_tpc */
1455 static int dm_test_acpi_write_tpc(struct unit_test_state *uts)
1457 struct acpi_ctx *ctx;
1460 ut_assertok(alloc_context(&ctx));
1462 ptr = acpigen_get_current(ctx);
1463 acpigen_write_tpc(ctx, "\\TLVL");
1465 ut_asserteq(METHOD_OP, *ptr++);
1466 ptr += 3; /* skip length */
1467 ut_asserteq_strn("_TPC", (char *)ptr);
1469 ut_asserteq(0, *ptr++);
1470 ut_asserteq(RETURN_OP, *ptr++);
1471 ut_asserteq_strn("\\TLVL", (char *)ptr);
1474 ut_asserteq_ptr(ptr, ctx->current);
1480 DM_TEST(dm_test_acpi_write_tpc, 0);
1482 /* Test acpigen_write_pss_package(), etc. */
1483 static int dm_test_acpi_write_pss_psd(struct unit_test_state *uts)
1485 struct acpi_ctx *ctx;
1488 ut_assertok(alloc_context(&ctx));
1490 ptr = acpigen_get_current(ctx);
1491 acpigen_write_pss_package(ctx, 1, 2, 3, 4, 5, 6);
1492 ut_asserteq(PACKAGE_OP, *ptr++);
1493 ptr += 3; /* skip length */
1494 ut_asserteq(6, *ptr++);
1496 ut_asserteq(DWORD_PREFIX, *ptr++);
1497 ut_asserteq(1, get_unaligned((u32 *)ptr));
1500 ut_asserteq(2, get_unaligned((u32 *)ptr));
1503 ut_asserteq(3, get_unaligned((u32 *)ptr));
1506 ut_asserteq(4, get_unaligned((u32 *)ptr));
1509 ut_asserteq(5, get_unaligned((u32 *)ptr));
1512 ut_asserteq(6, get_unaligned((u32 *)ptr));
1515 acpigen_write_psd_package(ctx, 6, 7, HW_ALL);
1516 ut_asserteq(NAME_OP, *ptr++);
1517 ut_asserteq_strn("_PSD", (char *)ptr);
1519 ut_asserteq(PACKAGE_OP, *ptr++);
1520 ptr += 3; /* skip length */
1521 ut_asserteq(1, *ptr++);
1522 ut_asserteq(PACKAGE_OP, *ptr++);
1523 ptr += 3; /* skip length */
1524 ut_asserteq(5, *ptr++);
1526 ut_asserteq(BYTE_PREFIX, *ptr++);
1527 ut_asserteq(5, *ptr++);
1528 ut_asserteq(BYTE_PREFIX, *ptr++);
1529 ut_asserteq(0, *ptr++);
1531 ut_asserteq(DWORD_PREFIX, *ptr++);
1532 ut_asserteq(6, get_unaligned((u32 *)ptr));
1535 ut_asserteq(HW_ALL, get_unaligned((u32 *)ptr));
1538 ut_asserteq(7, get_unaligned((u32 *)ptr));
1541 ut_asserteq_ptr(ptr, ctx->current);
1547 DM_TEST(dm_test_acpi_write_pss_psd, 0);
1549 /* Test acpi_write_cst_package() */
1550 static int dm_test_acpi_write_cst(struct unit_test_state *uts)
1552 static struct acpi_cstate cstate_map[] = {
1555 .ctype = 1, /* ACPI C1 */
1559 .space_id = ACPI_ADDRESS_SPACE_FIXED,
1562 .ctype = 2, /* ACPI C2 */
1566 .space_id = ACPI_ADDRESS_SPACE_IO,
1572 int nentries = ARRAY_SIZE(cstate_map);
1573 struct acpi_ctx *ctx;
1577 ut_assertok(alloc_context(&ctx));
1579 ptr = acpigen_get_current(ctx);
1580 acpigen_write_cst_package(ctx, cstate_map, nentries);
1582 ut_asserteq(NAME_OP, *ptr++);
1583 ut_asserteq_strn("_CST", (char *)ptr);
1585 ut_asserteq(PACKAGE_OP, *ptr++);
1586 ptr += 3; /* skip length */
1587 ut_asserteq(nentries + 1, *ptr++);
1588 ut_asserteq(DWORD_PREFIX, *ptr++);
1589 ut_asserteq(nentries, get_unaligned((u32 *)ptr));
1592 for (i = 0; i < nentries; i++) {
1593 ut_asserteq(PACKAGE_OP, *ptr++);
1594 ptr += 3; /* skip length */
1595 ut_asserteq(4, *ptr++);
1596 ut_asserteq(BUFFER_OP, *ptr++);
1598 ut_asserteq(DWORD_PREFIX, *ptr++);
1599 ut_asserteq(cstate_map[i].ctype, get_unaligned((u32 *)ptr));
1601 ut_asserteq(cstate_map[i].latency, get_unaligned((u32 *)ptr));
1603 ut_asserteq(cstate_map[i].power, get_unaligned((u32 *)ptr));
1607 ut_asserteq_ptr(ptr, ctx->current);
1613 DM_TEST(dm_test_acpi_write_cst, 0);
1615 /* Test acpi_write_cst_package() */
1616 static int dm_test_acpi_write_csd(struct unit_test_state *uts)
1618 struct acpi_ctx *ctx;
1621 ut_assertok(alloc_context(&ctx));
1623 ptr = acpigen_get_current(ctx);
1624 acpigen_write_csd_package(ctx, 12, 34, CSD_HW_ALL, 56);
1626 ut_asserteq(NAME_OP, *ptr++);
1627 ut_asserteq_strn("_CSD", (char *)ptr);
1629 ut_asserteq(PACKAGE_OP, *ptr++);
1630 ptr += 3; /* skip length */
1631 ut_asserteq(1, *ptr++);
1632 ut_asserteq(PACKAGE_OP, *ptr++);
1633 ptr += 3; /* skip length */
1634 ut_asserteq(6, *ptr++);
1636 ut_asserteq(BYTE_PREFIX, *ptr++);
1637 ut_asserteq(6, *ptr++);
1638 ut_asserteq(BYTE_PREFIX, *ptr++);
1639 ut_asserteq(0, *ptr++);
1640 ut_asserteq(DWORD_PREFIX, *ptr++);
1641 ut_asserteq(12, get_unaligned((u32 *)ptr));
1643 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1645 ut_asserteq(34, get_unaligned((u32 *)ptr));
1647 ut_asserteq(56, get_unaligned((u32 *)ptr));
1650 ut_asserteq_ptr(ptr, ctx->current);
1656 DM_TEST(dm_test_acpi_write_csd, 0);
1658 /* Test acpigen_write_tss_package() */
1659 static int dm_test_acpi_write_tss(struct unit_test_state *uts)
1661 static struct acpi_tstate tstate_list[] = {
1663 { 6, 7, 8, 9, 10, },
1665 int nentries = ARRAY_SIZE(tstate_list);
1666 struct acpi_ctx *ctx;
1670 ut_assertok(alloc_context(&ctx));
1672 ptr = acpigen_get_current(ctx);
1673 acpigen_write_tss_package(ctx, tstate_list, nentries);
1675 ut_asserteq(NAME_OP, *ptr++);
1676 ut_asserteq_strn("_TSS", (char *)ptr);
1678 ut_asserteq(PACKAGE_OP, *ptr++);
1679 ptr += 3; /* skip length */
1680 ut_asserteq(nentries, *ptr++);
1682 for (i = 0; i < nentries; i++) {
1683 ut_asserteq(PACKAGE_OP, *ptr++);
1684 ptr += 3; /* skip length */
1685 ut_asserteq(5, *ptr++);
1686 ut_asserteq(DWORD_PREFIX, *ptr++);
1687 ut_asserteq(tstate_list[i].percent, get_unaligned((u32 *)ptr));
1689 ut_asserteq(tstate_list[i].power, get_unaligned((u32 *)ptr));
1691 ut_asserteq(tstate_list[i].latency, get_unaligned((u32 *)ptr));
1693 ut_asserteq(tstate_list[i].control, get_unaligned((u32 *)ptr));
1695 ut_asserteq(tstate_list[i].status, get_unaligned((u32 *)ptr));
1699 ut_asserteq_ptr(ptr, ctx->current);
1705 DM_TEST(dm_test_acpi_write_tss, 0);
1707 /* Test acpigen_write_tsd_package() */
1708 static int dm_test_acpi_write_tsd_package(struct unit_test_state *uts)
1710 struct acpi_ctx *ctx;
1713 ut_assertok(alloc_context(&ctx));
1715 ptr = acpigen_get_current(ctx);
1716 acpigen_write_tsd_package(ctx, 12, 34, HW_ALL);
1718 ut_asserteq(NAME_OP, *ptr++);
1719 ut_asserteq_strn("_TSD", (char *)ptr);
1721 ut_asserteq(PACKAGE_OP, *ptr++);
1722 ptr += 3; /* skip length */
1723 ut_asserteq(1, *ptr++);
1724 ut_asserteq(PACKAGE_OP, *ptr++);
1725 ptr += 3; /* skip length */
1726 ut_asserteq(5, *ptr++);
1728 ut_asserteq(BYTE_PREFIX, *ptr++);
1729 ut_asserteq(5, *ptr++);
1730 ut_asserteq(BYTE_PREFIX, *ptr++);
1731 ut_asserteq(0, *ptr++);
1732 ut_asserteq(DWORD_PREFIX, *ptr++);
1733 ut_asserteq(12, get_unaligned((u32 *)ptr));
1735 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1737 ut_asserteq(34, get_unaligned((u32 *)ptr));
1740 ut_asserteq_ptr(ptr, ctx->current);
1746 DM_TEST(dm_test_acpi_write_tsd_package, 0);