blob: 7113219792e602504f3677a707b7de36ce1025a9 [file] [log] [blame]
Simon Glass98528d42020-07-07 13:11:42 -06001// 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
Simon Glass98528d42020-07-07 13:11:42 -06009#include <dm.h>
Simon Glass3d39c132020-07-07 13:11:43 -060010#include <irq.h>
Simon Glass98528d42020-07-07 13:11:42 -060011#include <malloc.h>
Simon Glass4c8cbbd2020-09-22 12:44:58 -060012#include <uuid.h>
Simon Glass98528d42020-07-07 13:11:42 -060013#include <acpi/acpigen.h>
Simon Glass3d39c132020-07-07 13:11:43 -060014#include <acpi/acpi_device.h>
Simon Glass837127f2020-07-07 21:32:11 -060015#include <acpi/acpi_table.h>
Simon Glass4fa63312020-07-07 13:11:46 -060016#include <asm/gpio.h>
Simon Glass98528d42020-07-07 13:11:42 -060017#include <asm/unaligned.h>
18#include <dm/acpi.h>
19#include <dm/test.h>
Simon Glasse2bbb132020-07-07 13:11:47 -060020#include <dm/uclass-internal.h>
Simon Glass98528d42020-07-07 13:11:42 -060021#include <test/ut.h>
Simon Glassc993ff72020-07-07 13:11:56 -060022#include "acpi.h"
Simon Glass98528d42020-07-07 13:11:42 -060023
24/* Maximum size of the ACPI context needed for most tests */
25#define ACPI_CONTEXT_SIZE 150
26
Simon Glass071c4a52020-07-07 13:11:45 -060027#define TEST_STRING "frogmore"
Simon Glass45d6d952020-07-07 13:11:53 -060028#define TEST_STRING2 "ranch"
Simon Glass071c4a52020-07-07 13:11:45 -060029#define TEST_STREAM2 "\xfa\xde"
30
Simon Glass8715ce02020-07-07 13:11:52 -060031#define TEST_INT8 0x7d
32#define TEST_INT16 0x2345
33#define TEST_INT32 0x12345678
34#define TEST_INT64 0x4567890123456
35
Simon Glassc993ff72020-07-07 13:11:56 -060036int acpi_test_alloc_context_size(struct acpi_ctx **ctxp, int size)
Simon Glass98528d42020-07-07 13:11:42 -060037{
38 struct acpi_ctx *ctx;
39
40 *ctxp = NULL;
41 ctx = malloc(sizeof(*ctx));
42 if (!ctx)
43 return -ENOMEM;
Simon Glass0f277632020-07-07 13:11:50 -060044 ctx->base = malloc(size);
Simon Glass98528d42020-07-07 13:11:42 -060045 if (!ctx->base) {
46 free(ctx);
47 return -ENOMEM;
48 }
Simon Glass0f277632020-07-07 13:11:50 -060049 ctx->ltop = 0;
Simon Glass98528d42020-07-07 13:11:42 -060050 ctx->current = ctx->base;
51 *ctxp = ctx;
52
53 return 0;
54}
55
Simon Glassc993ff72020-07-07 13:11:56 -060056int 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 Glass0f277632020-07-07 13:11:50 -060064static int alloc_context(struct acpi_ctx **ctxp)
65{
Simon Glassc993ff72020-07-07 13:11:56 -060066 return acpi_test_alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
Simon Glass0f277632020-07-07 13:11:50 -060067}
68
Simon Glass98528d42020-07-07 13:11:42 -060069static 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() */
77static 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}
101DM_TEST(dm_test_acpi_emit_simple, 0);
Simon Glass3d39c132020-07-07 13:11:43 -0600102
Simon Glass071c4a52020-07-07 13:11:45 -0600103/* Test emitting a stream */
104static 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}
121DM_TEST(dm_test_acpi_emit_stream, 0);
122
123/* Test emitting a string */
124static 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}
140DM_TEST(dm_test_acpi_emit_string, 0);
141
Simon Glass3d39c132020-07-07 13:11:43 -0600142/* Test emitting an interrupt descriptor */
143static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600170DM_TEST(dm_test_acpi_interrupt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4fa63312020-07-07 13:11:46 -0600171
172/* Test emitting a GPIO descriptor */
173static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600215DM_TEST(dm_test_acpi_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4fa63312020-07-07 13:11:46 -0600216
217/* Test emitting a GPIO descriptor with an interrupt */
218static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600260DM_TEST(dm_test_acpi_gpio_irq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasse2bbb132020-07-07 13:11:47 -0600261
262/* Test emitting either a GPIO or interrupt descriptor */
263static 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}
299DM_TEST(dm_test_acpi_interrupt_or_gpio,
Simon Glass974dccd2020-07-28 19:41:12 -0600300 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass39ab8672020-07-07 13:11:48 -0600301
302/* Test emitting an I2C descriptor */
303static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600332DM_TEST(dm_test_acpi_i2c, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass711fd982020-07-07 13:11:49 -0600333
334/* Test emitting a SPI descriptor */
335static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600368DM_TEST(dm_test_acpi_spi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass0f277632020-07-07 13:11:50 -0600369
Simon Glass0f277632020-07-07 13:11:50 -0600370/* Test emitting a length */
371static 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 Glassc993ff72020-07-07 13:11:56 -0600378 ut_assertok(acpi_test_alloc_context_size(&ctx, size));
Simon Glass0f277632020-07-07 13:11:50 -0600379
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 Glassc993ff72020-07-07 13:11:56 -0600386 ut_asserteq(1 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600387
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 Glassc993ff72020-07-07 13:11:56 -0600394 ut_asserteq(200 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600395
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 Glassc993ff72020-07-07 13:11:56 -0600402 ut_asserteq(40000 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600403
404 free_context(&ctx);
405
406 return 0;
407}
408DM_TEST(dm_test_acpi_len, 0);
Simon Glassedc26802020-07-07 13:11:51 -0600409
410/* Test writing a package */
411static 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 Glassc993ff72020-07-07 13:11:56 -0600428 ut_asserteq(5, acpi_test_get_length(ptr + 1));
Simon Glassedc26802020-07-07 13:11:51 -0600429 ut_asserteq(3, ptr[4]);
430
431 free_context(&ctx);
432
433 return 0;
434}
435DM_TEST(dm_test_acpi_package, 0);
Simon Glass8715ce02020-07-07 13:11:52 -0600436
437/* Test writing an integer */
438static 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}
476DM_TEST(dm_test_acpi_integer, 0);
Simon Glass45d6d952020-07-07 13:11:53 -0600477
478/* Test writing a string */
479static 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}
503DM_TEST(dm_test_acpi_string, 0);
Simon Glass0dc3d512020-07-07 13:11:54 -0600504
505/* Test writing a name */
506static 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}
596DM_TEST(dm_test_acpi_name, 0);
Simon Glass48342b02020-07-07 13:11:55 -0600597
598/* Test writing a UUID */
599static 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 Glassc993ff72020-07-07 13:11:56 -0600612 ut_asserteq(22, acpi_test_get_length(ptr + 1));
Simon Glass48342b02020-07-07 13:11:55 -0600613 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}
629DM_TEST(dm_test_acpi_uuid, 0);
Simon Glass9238fac2020-07-07 13:11:59 -0600630
631/* Test writing misc ACPI codes */
632static 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}
704DM_TEST(dm_test_acpi_misc, 0);
Simon Glassd0f7d9b2020-07-07 13:12:00 -0600705
706/* Test writing an ACPI power resource */
707static 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}
745DM_TEST(dm_test_acpi_power_res, 0);
Simon Glass8a200762020-07-07 13:12:01 -0600746
747/* Test writing ACPI code to toggle a GPIO */
748static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600809DM_TEST(dm_test_acpi_gpio_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glasscec3af02020-07-07 13:12:02 -0600810
811/* Test writing ACPI code to output power-sequence info */
812static 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 Glass974dccd2020-07-28 19:41:12 -0600876DM_TEST(dm_test_acpi_power_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd6814e22020-07-07 21:32:09 -0600877
878/* Test writing values */
879static 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}
919DM_TEST(dm_test_acpi_write_values, 0);
Simon Glass6fcdaf12020-07-07 21:32:10 -0600920
921/* Test writing a scope */
922static 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}
Simon Glass974dccd2020-07-28 19:41:12 -0600950DM_TEST(dm_test_acpi_scope, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass837127f2020-07-07 21:32:11 -0600951
952/* Test writing a resource template */
953static 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}
995DM_TEST(dm_test_acpi_resource_template, 0);
Simon Glassa186d932020-07-07 21:32:14 -0600996
997/* Test writing a device */
998static 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}
1022DM_TEST(dm_test_acpi_device, 0);
Simon Glassb4df0782020-07-07 21:32:15 -06001023
1024/* Test writing named values */
1025static int dm_test_acpi_write_name(struct unit_test_state *uts)
1026{
1027 const char *name = "\\_SB." ACPI_TEST_DEV_NAME;
1028 struct acpi_ctx *ctx;
1029 u8 *ptr;
1030
1031 ut_assertok(alloc_context(&ctx));
1032 ptr = acpigen_get_current(ctx);
1033
1034 acpigen_write_name_zero(ctx, name);
1035 acpigen_write_name_one(ctx, name);
1036 acpigen_write_name_byte(ctx, name, TEST_INT8);
1037 acpigen_write_name_word(ctx, name, TEST_INT16);
1038 acpigen_write_name_dword(ctx, name, TEST_INT32);
1039 acpigen_write_name_qword(ctx, name, TEST_INT64);
1040 acpigen_write_name_integer(ctx, name, TEST_INT64 + 1);
1041 acpigen_write_name_string(ctx, name, "baldrick");
1042 acpigen_write_name_string(ctx, name, NULL);
1043
1044 ut_asserteq(NAME_OP, *ptr++);
1045 ut_asserteq_strn("\\._SB_ABCD", (char *)ptr);
1046 ptr += 10;
1047 ut_asserteq(ZERO_OP, *ptr++);
1048
1049 ut_asserteq(NAME_OP, *ptr++);
1050 ptr += 10;
1051 ut_asserteq(ONE_OP, *ptr++);
1052
1053 ut_asserteq(NAME_OP, *ptr++);
1054 ptr += 10;
1055 ut_asserteq(BYTE_PREFIX, *ptr++);
1056 ut_asserteq(TEST_INT8, *ptr++);
1057
1058 ut_asserteq(NAME_OP, *ptr++);
1059 ptr += 10;
1060 ut_asserteq(WORD_PREFIX, *ptr++);
1061 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
1062 ptr += 2;
1063
1064 ut_asserteq(NAME_OP, *ptr++);
1065 ptr += 10;
1066 ut_asserteq(DWORD_PREFIX, *ptr++);
1067 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
1068 ptr += 4;
1069
1070 ut_asserteq(NAME_OP, *ptr++);
1071 ptr += 10;
1072 ut_asserteq(QWORD_PREFIX, *ptr++);
1073 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
1074 ptr += 8;
1075
1076 ut_asserteq(NAME_OP, *ptr++);
1077 ptr += 10;
1078 ut_asserteq(QWORD_PREFIX, *ptr++);
1079 ut_asserteq_64(TEST_INT64 + 1, get_unaligned((u64 *)ptr));
1080 ptr += 8;
1081
1082 ut_asserteq(NAME_OP, *ptr++);
1083 ptr += 10;
1084 ut_asserteq(STRING_PREFIX, *ptr++);
Marek Vasut1531c4e2023-03-10 04:33:13 +01001085 ut_asserteq_str("baldrick", (char *)ptr);
Simon Glassb4df0782020-07-07 21:32:15 -06001086 ptr += 9;
1087
1088 ut_asserteq(NAME_OP, *ptr++);
1089 ptr += 10;
1090 ut_asserteq(STRING_PREFIX, *ptr++);
1091 ut_asserteq('\0', *ptr++);
1092
1093 ut_asserteq_ptr(ptr, ctx->current);
1094
1095 free_context(&ctx);
1096
1097 return 0;
1098}
1099DM_TEST(dm_test_acpi_write_name, 0);
Simon Glass77dfd032020-09-22 12:44:56 -06001100
1101/* Test emitting a _PRW component */
1102static int dm_test_acpi_write_prw(struct unit_test_state *uts)
1103{
1104 struct acpi_ctx *ctx;
1105 u8 *ptr;
1106
1107 ut_assertok(alloc_context(&ctx));
1108
1109 ptr = acpigen_get_current(ctx);
1110 acpigen_write_prw(ctx, 5, 3);
1111 ut_asserteq(NAME_OP, *ptr++);
1112
1113 ut_asserteq_strn("_PRW", (char *)ptr);
1114 ptr += 4;
1115 ut_asserteq(PACKAGE_OP, *ptr++);
1116 ut_asserteq(8, acpi_test_get_length(ptr));
1117 ptr += 3;
1118 ut_asserteq(2, *ptr++);
1119 ut_asserteq(BYTE_PREFIX, *ptr++);
1120 ut_asserteq(5, *ptr++);
1121 ut_asserteq(BYTE_PREFIX, *ptr++);
1122 ut_asserteq(3, *ptr++);
1123 ut_asserteq_ptr(ptr, ctx->current);
1124
1125 free_context(&ctx);
1126
1127 return 0;
1128}
1129DM_TEST(dm_test_acpi_write_prw, 0);
Simon Glassca727632020-09-22 12:44:57 -06001130
1131/* Test emitting writing conditionals */
1132static int dm_test_acpi_write_cond(struct unit_test_state *uts)
1133{
1134 struct acpi_ctx *ctx;
1135 u8 *ptr;
1136
1137 ut_assertok(alloc_context(&ctx));
1138
1139 ptr = acpigen_get_current(ctx);
1140 acpigen_write_if(ctx);
1141 acpigen_pop_len(ctx);
1142 ut_asserteq(IF_OP, *ptr++);
1143 ut_asserteq(3, acpi_test_get_length(ptr));
1144 ptr += 3;
1145
1146 acpigen_write_else(ctx);
1147 acpigen_pop_len(ctx);
1148 ut_asserteq(ELSE_OP, *ptr++);
1149 ut_asserteq(3, acpi_test_get_length(ptr));
1150 ptr += 3;
1151
1152 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, 5);
1153 acpigen_pop_len(ctx);
1154 ut_asserteq(IF_OP, *ptr++);
1155 ut_asserteq(7, acpi_test_get_length(ptr));
1156 ptr += 3;
1157 ut_asserteq(LEQUAL_OP, *ptr++);
1158 ut_asserteq(LOCAL1_OP, *ptr++);
1159 ut_asserteq(BYTE_PREFIX, *ptr++);
1160 ut_asserteq(5, *ptr++);
1161
1162 ut_asserteq_ptr(ptr, ctx->current);
1163
1164 free_context(&ctx);
1165
1166 return 0;
1167}
1168DM_TEST(dm_test_acpi_write_cond, 0);
1169
1170/* Test emitting writing return values and ToBuffer/ToInteger */
1171static int dm_test_acpi_write_return(struct unit_test_state *uts)
1172{
1173 int len = sizeof(TEST_STRING);
1174 struct acpi_ctx *ctx;
1175 u8 *ptr;
1176
1177 ut_assertok(alloc_context(&ctx));
1178
1179 ptr = acpigen_get_current(ctx);
1180 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
1181 ut_asserteq(TO_BUFFER_OP, *ptr++);
1182 ut_asserteq(ARG0_OP, *ptr++);
1183 ut_asserteq(LOCAL0_OP, *ptr++);
1184
1185 acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP);
1186 ut_asserteq(TO_INTEGER_OP, *ptr++);
1187 ut_asserteq(ARG0_OP, *ptr++);
1188 ut_asserteq(LOCAL0_OP, *ptr++);
1189
1190 acpigen_write_return_byte_buffer(ctx, (u8 *)TEST_STRING, len);
1191 ut_asserteq(RETURN_OP, *ptr++);
1192 ut_asserteq(BUFFER_OP, *ptr++);
1193 ut_asserteq(5 + len, acpi_test_get_length(ptr));
1194 ptr += 3;
1195 ut_asserteq(BYTE_PREFIX, *ptr++);
1196 ut_asserteq(len, *ptr++);
1197 ut_asserteq_mem(TEST_STRING, ptr, len);
1198 ptr += len;
1199
1200 acpigen_write_return_singleton_buffer(ctx, 123);
1201 len = 1;
1202 ut_asserteq(RETURN_OP, *ptr++);
1203 ut_asserteq(BUFFER_OP, *ptr++);
1204 ut_asserteq(4 + len, acpi_test_get_length(ptr));
1205 ptr += 3;
1206 ut_asserteq(ONE_OP, *ptr++);
1207 ut_asserteq(123, *ptr++);
1208
1209 acpigen_write_return_byte(ctx, 43);
1210 ut_asserteq(RETURN_OP, *ptr++);
1211 ut_asserteq(BYTE_PREFIX, *ptr++);
1212 ut_asserteq(43, *ptr++);
1213
1214 ut_asserteq_ptr(ptr, ctx->current);
1215
1216 free_context(&ctx);
1217
1218 return 0;
1219}
1220DM_TEST(dm_test_acpi_write_return, 0);
Simon Glass4c8cbbd2020-09-22 12:44:58 -06001221
1222/* Test emitting a DSM for an I2C HID */
1223static int dm_test_acpi_write_i2c_dsm(struct unit_test_state *uts)
1224{
1225 char uuid_str[UUID_STR_LEN + 1];
1226 const int reg_offset = 0x20;
1227 struct acpi_ctx *ctx;
1228 u8 *ptr;
1229
1230 ut_assertok(alloc_context(&ctx));
1231
1232 ptr = acpigen_get_current(ctx);
1233 ut_assertok(acpi_device_write_dsm_i2c_hid(ctx, reg_offset));
1234
1235 /* acpigen_write_dsm_start() */
1236 ut_asserteq(METHOD_OP, *ptr++);
1237 ut_asserteq(0x78, acpi_test_get_length(ptr));
1238 ptr += 3;
1239 ut_asserteq_strn("_DSM", (char *)ptr);
1240 ptr += 4;
1241 ut_asserteq(ACPI_METHOD_SERIALIZED_MASK | 4, *ptr++);
1242
1243 ut_asserteq(TO_BUFFER_OP, *ptr++);
1244 ut_asserteq(ARG0_OP, *ptr++);
1245 ut_asserteq(LOCAL0_OP, *ptr++);
1246
1247 /* acpigen_write_dsm_uuid_start() */
1248 ut_asserteq(IF_OP, *ptr++);
1249 ut_asserteq(0x65, acpi_test_get_length(ptr));
1250 ptr += 3;
1251 ut_asserteq(LEQUAL_OP, *ptr++);
1252 ut_asserteq(LOCAL0_OP, *ptr++);
1253
1254 ut_asserteq(BUFFER_OP, *ptr++);
1255 ut_asserteq(UUID_BIN_LEN + 6, acpi_test_get_length(ptr));
1256 ptr += 3;
1257 ut_asserteq(WORD_PREFIX, *ptr++);
1258 ut_asserteq(UUID_BIN_LEN, get_unaligned((u16 *)ptr));
1259 ptr += 2;
1260 uuid_bin_to_str(ptr, uuid_str, UUID_STR_FORMAT_GUID);
1261 ut_asserteq_str(ACPI_DSM_I2C_HID_UUID, uuid_str);
1262 ptr += UUID_BIN_LEN;
1263
1264 ut_asserteq(TO_INTEGER_OP, *ptr++);
1265 ut_asserteq(ARG2_OP, *ptr++);
1266 ut_asserteq(LOCAL1_OP, *ptr++);
1267
1268 /* acpigen_write_dsm_uuid_start_cond() */
1269 ut_asserteq(IF_OP, *ptr++);
1270 ut_asserteq(0x34, acpi_test_get_length(ptr));
1271 ptr += 3;
1272 ut_asserteq(LEQUAL_OP, *ptr++);
1273 ut_asserteq(LOCAL1_OP, *ptr++);
1274 ut_asserteq(ZERO_OP, *ptr++);
1275
1276 /*
1277 * See code from acpi_device_write_dsm_i2c_hid(). We don't check every
1278 * piece
1279 */
1280 ut_asserteq(TO_INTEGER_OP, *ptr++);
1281 ut_asserteq(ARG1_OP, *ptr++);
1282 ut_asserteq(LOCAL2_OP, *ptr++);
1283
1284 ut_asserteq(IF_OP, *ptr++);
1285 ut_asserteq(0xd, acpi_test_get_length(ptr));
1286 ptr += 3;
1287 ut_asserteq(LEQUAL_OP, *ptr++);
1288 ut_asserteq(LOCAL2_OP, *ptr++);
1289 ut_asserteq(ZERO_OP, *ptr++); /* function 0 */
1290
1291 ut_asserteq(RETURN_OP, *ptr++);
1292 ut_asserteq(BUFFER_OP, *ptr++);
1293 ptr += 5;
1294
1295 ut_asserteq(ELSE_OP, *ptr++);
1296 ptr += 3;
1297
1298 ut_asserteq(IF_OP, *ptr++);
1299 ut_asserteq(0xd, acpi_test_get_length(ptr));
1300 ptr += 3;
1301 ut_asserteq(LEQUAL_OP, *ptr++);
1302 ut_asserteq(LOCAL2_OP, *ptr++);
1303 ut_asserteq(ONE_OP, *ptr++);
1304
1305 ut_asserteq(RETURN_OP, *ptr++);
1306 ut_asserteq(BUFFER_OP, *ptr++);
1307 ptr += 5;
1308
1309 ut_asserteq(ELSE_OP, *ptr++);
1310 ptr += 3;
1311
1312 ut_asserteq(RETURN_OP, *ptr++);
1313 ut_asserteq(BUFFER_OP, *ptr++);
1314 ptr += 5;
1315
1316 /* acpigen_write_dsm_uuid_start_cond() */
1317 ut_asserteq(IF_OP, *ptr++);
1318 ut_asserteq(9, acpi_test_get_length(ptr));
1319 ptr += 3;
1320 ut_asserteq(LEQUAL_OP, *ptr++);
1321 ut_asserteq(LOCAL1_OP, *ptr++);
1322 ut_asserteq(ONE_OP, *ptr++); /* function 1 */
1323
1324 ut_asserteq(RETURN_OP, *ptr++);
1325 ut_asserteq(BYTE_PREFIX, *ptr++);
1326 ut_asserteq(reg_offset, *ptr++);
1327
1328 /* acpigen_write_dsm_uuid_end() */
1329 ut_asserteq(RETURN_OP, *ptr++);
1330 ut_asserteq(BUFFER_OP, *ptr++);
1331 ptr += 5;
1332
1333 /* acpigen_write_dsm_end */
1334 ut_asserteq(RETURN_OP, *ptr++);
1335 ut_asserteq(BUFFER_OP, *ptr++);
1336 ptr += 5;
1337
1338 ut_asserteq_ptr(ptr, ctx->current);
1339
1340 free_context(&ctx);
1341
1342 return 0;
1343}
1344DM_TEST(dm_test_acpi_write_i2c_dsm, 0);
Simon Glass412dc4b2020-09-22 12:45:11 -06001345
1346/* Test emitting a processor */
1347static int dm_test_acpi_write_processor(struct unit_test_state *uts)
1348{
1349 const int cpuindex = 6;
1350 const u32 pblock_addr = 0x12345600;
1351 const u32 pblock_len = 0x60;
1352 struct acpi_ctx *ctx;
1353 u8 *ptr;
1354
1355 ut_assertok(alloc_context(&ctx));
1356
1357 ptr = acpigen_get_current(ctx);
1358 acpigen_write_processor(ctx, cpuindex, pblock_addr, pblock_len);
1359 acpigen_pop_len(ctx);
1360
1361 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1362 ut_asserteq(PROCESSOR_OP, *ptr++);
1363 ut_asserteq(0x13, acpi_test_get_length(ptr));
1364 ptr += 3;
1365 ut_asserteq_strn("\\._PR_CP06", (char *)ptr);
1366 ptr += 10;
1367 ut_asserteq(cpuindex, *ptr++);
1368 ut_asserteq(pblock_addr, get_unaligned((u32 *)ptr));
1369 ptr += 4;
1370 ut_asserteq(pblock_len, *ptr++);
1371
1372 ut_asserteq_ptr(ptr, ctx->current);
1373
1374 free_context(&ctx);
1375
1376 return 0;
1377}
1378DM_TEST(dm_test_acpi_write_processor, 0);
1379
1380/* Test emitting a processor package */
1381static int dm_test_acpi_write_processor_package(struct unit_test_state *uts)
1382{
1383 const int core_count = 3;
1384 struct acpi_ctx *ctx;
1385 u8 *ptr;
1386
1387 ut_assertok(alloc_context(&ctx));
1388
1389 ptr = acpigen_get_current(ctx);
1390 acpigen_write_processor_package(ctx, "XCPU", 0, core_count);
1391
1392 ut_asserteq(NAME_OP, *ptr++);
1393 ut_asserteq_strn("XCPU", (char *)ptr);
1394 ptr += 4;
1395 ut_asserteq(PACKAGE_OP, *ptr++);
1396 ptr += 3; /* skip length */
1397 ut_asserteq(core_count, *ptr++);
1398
1399 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1400 ptr += 10;
1401 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1402 ptr += 10;
1403 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1404 ptr += 10;
1405
1406 ut_asserteq_ptr(ptr, ctx->current);
1407
1408 free_context(&ctx);
1409
1410 return 0;
1411}
1412DM_TEST(dm_test_acpi_write_processor_package, 0);
1413
1414/* Test emitting a processor notification package */
1415static int dm_test_acpi_write_processor_cnot(struct unit_test_state *uts)
1416{
1417 const int core_count = 3;
1418 struct acpi_ctx *ctx;
1419 u8 *ptr;
1420
1421 ut_assertok(alloc_context(&ctx));
1422
1423 ptr = acpigen_get_current(ctx);
1424 acpigen_write_processor_cnot(ctx, core_count);
1425
1426 ut_asserteq(METHOD_OP, *ptr++);
1427 ptr += 3; /* skip length */
1428 ut_asserteq_strn("\\._PR_CNOT", (char *)ptr);
1429 ptr += 10;
1430 ut_asserteq(1, *ptr++);
1431
1432 ut_asserteq(NOTIFY_OP, *ptr++);
1433 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1434 ptr += 10;
1435 ut_asserteq(ARG0_OP, *ptr++);
1436 ut_asserteq(NOTIFY_OP, *ptr++);
1437 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1438 ptr += 10;
1439 ut_asserteq(ARG0_OP, *ptr++);
1440 ut_asserteq(NOTIFY_OP, *ptr++);
1441 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1442 ptr += 10;
1443 ut_asserteq(ARG0_OP, *ptr++);
1444
1445 ut_asserteq_ptr(ptr, ctx->current);
1446
1447 free_context(&ctx);
1448
1449 return 0;
1450}
1451DM_TEST(dm_test_acpi_write_processor_cnot, 0);
Simon Glass2f8b6af2020-09-22 12:45:13 -06001452
1453/* Test acpigen_write_tpc */
1454static int dm_test_acpi_write_tpc(struct unit_test_state *uts)
1455{
1456 struct acpi_ctx *ctx;
1457 u8 *ptr;
1458
1459 ut_assertok(alloc_context(&ctx));
1460
1461 ptr = acpigen_get_current(ctx);
1462 acpigen_write_tpc(ctx, "\\TLVL");
1463
1464 ut_asserteq(METHOD_OP, *ptr++);
1465 ptr += 3; /* skip length */
1466 ut_asserteq_strn("_TPC", (char *)ptr);
1467 ptr += 4;
1468 ut_asserteq(0, *ptr++);
1469 ut_asserteq(RETURN_OP, *ptr++);
1470 ut_asserteq_strn("\\TLVL", (char *)ptr);
1471 ptr += 5;
1472
1473 ut_asserteq_ptr(ptr, ctx->current);
1474
1475 free_context(&ctx);
1476
1477 return 0;
1478}
1479DM_TEST(dm_test_acpi_write_tpc, 0);
1480
1481/* Test acpigen_write_pss_package(), etc. */
1482static int dm_test_acpi_write_pss_psd(struct unit_test_state *uts)
1483{
1484 struct acpi_ctx *ctx;
1485 u8 *ptr;
1486
1487 ut_assertok(alloc_context(&ctx));
1488
1489 ptr = acpigen_get_current(ctx);
1490 acpigen_write_pss_package(ctx, 1, 2, 3, 4, 5, 6);
1491 ut_asserteq(PACKAGE_OP, *ptr++);
1492 ptr += 3; /* skip length */
1493 ut_asserteq(6, *ptr++);
1494
1495 ut_asserteq(DWORD_PREFIX, *ptr++);
1496 ut_asserteq(1, get_unaligned((u32 *)ptr));
1497 ptr += 5;
1498
1499 ut_asserteq(2, get_unaligned((u32 *)ptr));
1500 ptr += 5;
1501
1502 ut_asserteq(3, get_unaligned((u32 *)ptr));
1503 ptr += 5;
1504
1505 ut_asserteq(4, get_unaligned((u32 *)ptr));
1506 ptr += 5;
1507
1508 ut_asserteq(5, get_unaligned((u32 *)ptr));
1509 ptr += 5;
1510
1511 ut_asserteq(6, get_unaligned((u32 *)ptr));
1512 ptr += 4;
1513
1514 acpigen_write_psd_package(ctx, 6, 7, HW_ALL);
1515 ut_asserteq(NAME_OP, *ptr++);
1516 ut_asserteq_strn("_PSD", (char *)ptr);
1517 ptr += 4;
1518 ut_asserteq(PACKAGE_OP, *ptr++);
1519 ptr += 3; /* skip length */
1520 ut_asserteq(1, *ptr++);
1521 ut_asserteq(PACKAGE_OP, *ptr++);
1522 ptr += 3; /* skip length */
1523 ut_asserteq(5, *ptr++);
1524
1525 ut_asserteq(BYTE_PREFIX, *ptr++);
1526 ut_asserteq(5, *ptr++);
1527 ut_asserteq(BYTE_PREFIX, *ptr++);
1528 ut_asserteq(0, *ptr++);
1529
1530 ut_asserteq(DWORD_PREFIX, *ptr++);
1531 ut_asserteq(6, get_unaligned((u32 *)ptr));
1532 ptr += 5;
1533
1534 ut_asserteq(HW_ALL, get_unaligned((u32 *)ptr));
1535 ptr += 5;
1536
1537 ut_asserteq(7, get_unaligned((u32 *)ptr));
1538 ptr += 4;
1539
1540 ut_asserteq_ptr(ptr, ctx->current);
1541
1542 free_context(&ctx);
1543
1544 return 0;
1545}
1546DM_TEST(dm_test_acpi_write_pss_psd, 0);
1547
1548/* Test acpi_write_cst_package() */
1549static int dm_test_acpi_write_cst(struct unit_test_state *uts)
1550{
1551 static struct acpi_cstate cstate_map[] = {
1552 {
1553 /* C1 */
1554 .ctype = 1, /* ACPI C1 */
1555 .latency = 1,
1556 .power = 1000,
1557 .resource = {
1558 .space_id = ACPI_ADDRESS_SPACE_FIXED,
1559 },
1560 }, {
1561 .ctype = 2, /* ACPI C2 */
1562 .latency = 50,
1563 .power = 10,
1564 .resource = {
1565 .space_id = ACPI_ADDRESS_SPACE_IO,
1566 .bit_width = 8,
1567 .addrl = 0x415,
1568 },
1569 },
1570 };
1571 int nentries = ARRAY_SIZE(cstate_map);
1572 struct acpi_ctx *ctx;
1573 u8 *ptr;
1574 int i;
1575
1576 ut_assertok(alloc_context(&ctx));
1577
1578 ptr = acpigen_get_current(ctx);
1579 acpigen_write_cst_package(ctx, cstate_map, nentries);
1580
1581 ut_asserteq(NAME_OP, *ptr++);
1582 ut_asserteq_strn("_CST", (char *)ptr);
1583 ptr += 4;
1584 ut_asserteq(PACKAGE_OP, *ptr++);
1585 ptr += 3; /* skip length */
1586 ut_asserteq(nentries + 1, *ptr++);
1587 ut_asserteq(DWORD_PREFIX, *ptr++);
1588 ut_asserteq(nentries, get_unaligned((u32 *)ptr));
1589 ptr += 4;
1590
1591 for (i = 0; i < nentries; i++) {
1592 ut_asserteq(PACKAGE_OP, *ptr++);
1593 ptr += 3; /* skip length */
1594 ut_asserteq(4, *ptr++);
1595 ut_asserteq(BUFFER_OP, *ptr++);
1596 ptr += 0x17;
1597 ut_asserteq(DWORD_PREFIX, *ptr++);
1598 ut_asserteq(cstate_map[i].ctype, get_unaligned((u32 *)ptr));
1599 ptr += 5;
1600 ut_asserteq(cstate_map[i].latency, get_unaligned((u32 *)ptr));
1601 ptr += 5;
1602 ut_asserteq(cstate_map[i].power, get_unaligned((u32 *)ptr));
1603 ptr += 4;
1604 }
1605
1606 ut_asserteq_ptr(ptr, ctx->current);
1607
1608 free_context(&ctx);
1609
1610 return 0;
1611}
1612DM_TEST(dm_test_acpi_write_cst, 0);
1613
1614/* Test acpi_write_cst_package() */
1615static int dm_test_acpi_write_csd(struct unit_test_state *uts)
1616{
1617 struct acpi_ctx *ctx;
1618 u8 *ptr;
1619
1620 ut_assertok(alloc_context(&ctx));
1621
1622 ptr = acpigen_get_current(ctx);
1623 acpigen_write_csd_package(ctx, 12, 34, CSD_HW_ALL, 56);
1624
1625 ut_asserteq(NAME_OP, *ptr++);
1626 ut_asserteq_strn("_CSD", (char *)ptr);
1627 ptr += 4;
1628 ut_asserteq(PACKAGE_OP, *ptr++);
1629 ptr += 3; /* skip length */
1630 ut_asserteq(1, *ptr++);
1631 ut_asserteq(PACKAGE_OP, *ptr++);
1632 ptr += 3; /* skip length */
1633 ut_asserteq(6, *ptr++);
1634
1635 ut_asserteq(BYTE_PREFIX, *ptr++);
1636 ut_asserteq(6, *ptr++);
1637 ut_asserteq(BYTE_PREFIX, *ptr++);
1638 ut_asserteq(0, *ptr++);
1639 ut_asserteq(DWORD_PREFIX, *ptr++);
1640 ut_asserteq(12, get_unaligned((u32 *)ptr));
1641 ptr += 5;
1642 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1643 ptr += 5;
1644 ut_asserteq(34, get_unaligned((u32 *)ptr));
1645 ptr += 5;
1646 ut_asserteq(56, get_unaligned((u32 *)ptr));
1647 ptr += 4;
1648
1649 ut_asserteq_ptr(ptr, ctx->current);
1650
1651 free_context(&ctx);
1652
1653 return 0;
1654}
1655DM_TEST(dm_test_acpi_write_csd, 0);
1656
1657/* Test acpigen_write_tss_package() */
1658static int dm_test_acpi_write_tss(struct unit_test_state *uts)
1659{
1660 static struct acpi_tstate tstate_list[] = {
1661 { 1, 2, 3, 4, 5, },
1662 { 6, 7, 8, 9, 10, },
1663 };
1664 int nentries = ARRAY_SIZE(tstate_list);
1665 struct acpi_ctx *ctx;
1666 u8 *ptr;
1667 int i;
1668
1669 ut_assertok(alloc_context(&ctx));
1670
1671 ptr = acpigen_get_current(ctx);
1672 acpigen_write_tss_package(ctx, tstate_list, nentries);
1673
1674 ut_asserteq(NAME_OP, *ptr++);
1675 ut_asserteq_strn("_TSS", (char *)ptr);
1676 ptr += 4;
1677 ut_asserteq(PACKAGE_OP, *ptr++);
1678 ptr += 3; /* skip length */
1679 ut_asserteq(nentries, *ptr++);
1680
1681 for (i = 0; i < nentries; i++) {
1682 ut_asserteq(PACKAGE_OP, *ptr++);
1683 ptr += 3; /* skip length */
1684 ut_asserteq(5, *ptr++);
1685 ut_asserteq(DWORD_PREFIX, *ptr++);
1686 ut_asserteq(tstate_list[i].percent, get_unaligned((u32 *)ptr));
1687 ptr += 5;
1688 ut_asserteq(tstate_list[i].power, get_unaligned((u32 *)ptr));
1689 ptr += 5;
1690 ut_asserteq(tstate_list[i].latency, get_unaligned((u32 *)ptr));
1691 ptr += 5;
1692 ut_asserteq(tstate_list[i].control, get_unaligned((u32 *)ptr));
1693 ptr += 5;
1694 ut_asserteq(tstate_list[i].status, get_unaligned((u32 *)ptr));
1695 ptr += 4;
1696 }
1697
1698 ut_asserteq_ptr(ptr, ctx->current);
1699
1700 free_context(&ctx);
1701
1702 return 0;
1703}
1704DM_TEST(dm_test_acpi_write_tss, 0);
1705
1706/* Test acpigen_write_tsd_package() */
1707static int dm_test_acpi_write_tsd_package(struct unit_test_state *uts)
1708{
1709 struct acpi_ctx *ctx;
1710 u8 *ptr;
1711
1712 ut_assertok(alloc_context(&ctx));
1713
1714 ptr = acpigen_get_current(ctx);
1715 acpigen_write_tsd_package(ctx, 12, 34, HW_ALL);
1716
1717 ut_asserteq(NAME_OP, *ptr++);
1718 ut_asserteq_strn("_TSD", (char *)ptr);
1719 ptr += 4;
1720 ut_asserteq(PACKAGE_OP, *ptr++);
1721 ptr += 3; /* skip length */
1722 ut_asserteq(1, *ptr++);
1723 ut_asserteq(PACKAGE_OP, *ptr++);
1724 ptr += 3; /* skip length */
1725 ut_asserteq(5, *ptr++);
1726
1727 ut_asserteq(BYTE_PREFIX, *ptr++);
1728 ut_asserteq(5, *ptr++);
1729 ut_asserteq(BYTE_PREFIX, *ptr++);
1730 ut_asserteq(0, *ptr++);
1731 ut_asserteq(DWORD_PREFIX, *ptr++);
1732 ut_asserteq(12, get_unaligned((u32 *)ptr));
1733 ptr += 5;
1734 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1735 ptr += 5;
1736 ut_asserteq(34, get_unaligned((u32 *)ptr));
1737 ptr += 4;
1738
1739 ut_asserteq_ptr(ptr, ctx->current);
1740
1741 free_context(&ctx);
1742
1743 return 0;
1744}
1745DM_TEST(dm_test_acpi_write_tsd_package, 0);