blob: 23c16bd986691074510e53f85a22cb2f93dd4947 [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>
Caleb Connolly29cab7c2024-08-30 13:34:37 +010012#include <u-boot/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 Glass1a92f832024-08-22 07:57:48 -0600170DM_TEST(dm_test_acpi_interrupt, UTF_SCAN_PDATA | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600215DM_TEST(dm_test_acpi_gpio, UTF_SCAN_PDATA | UTF_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 Glass1a92f832024-08-22 07:57:48 -0600260DM_TEST(dm_test_acpi_gpio_irq, UTF_SCAN_PDATA | UTF_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}
Simon Glass1a92f832024-08-22 07:57:48 -0600299DM_TEST(dm_test_acpi_interrupt_or_gpio, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass39ab8672020-07-07 13:11:48 -0600300
301/* Test emitting an I2C descriptor */
302static int dm_test_acpi_i2c(struct unit_test_state *uts)
303{
304 struct acpi_ctx *ctx;
305 struct udevice *dev;
306 u8 *ptr;
307
308 ut_assertok(alloc_context(&ctx));
309
310 ptr = acpigen_get_current(ctx);
311
312 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
313 ut_asserteq(0x43, acpi_device_write_i2c_dev(ctx, dev));
314 ut_asserteq(28, acpigen_get_current(ctx) - ptr);
315 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
316 ut_asserteq(25, get_unaligned((u16 *)(ptr + 1)));
317 ut_asserteq(ACPI_I2C_SERIAL_BUS_REVISION_ID, ptr[3]);
318 ut_asserteq(0, ptr[4]);
319 ut_asserteq(ACPI_SERIAL_BUS_TYPE_I2C, ptr[5]);
320 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
321 ut_asserteq(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
322 ut_asserteq(6, get_unaligned((u16 *)(ptr + 10)));
323 ut_asserteq(100000, get_unaligned((u32 *)(ptr + 12)));
324 ut_asserteq(0x43, get_unaligned((u16 *)(ptr + 16)));
325 ut_asserteq_str("\\_SB.I2C0", (char *)ptr + 18);
326
327 free_context(&ctx);
328
329 return 0;
330}
Simon Glass1a92f832024-08-22 07:57:48 -0600331DM_TEST(dm_test_acpi_i2c, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass711fd982020-07-07 13:11:49 -0600332
333/* Test emitting a SPI descriptor */
334static int dm_test_acpi_spi(struct unit_test_state *uts)
335{
336 struct acpi_ctx *ctx;
337 struct udevice *dev;
338 u8 *ptr;
339
340 ut_assertok(alloc_context(&ctx));
341
342 ptr = acpigen_get_current(ctx);
343
344 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
345 ut_assertok(acpi_device_write_spi_dev(ctx, dev));
346 ut_asserteq(31, acpigen_get_current(ctx) - ptr);
347 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
348 ut_asserteq(28, get_unaligned((u16 *)(ptr + 1)));
349 ut_asserteq(ACPI_SPI_SERIAL_BUS_REVISION_ID, ptr[3]);
350 ut_asserteq(0, ptr[4]);
351 ut_asserteq(ACPI_SERIAL_BUS_TYPE_SPI, ptr[5]);
352 ut_asserteq(2, ptr[6]);
353 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
354 ut_asserteq(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
355 ut_asserteq(9, get_unaligned((u16 *)(ptr + 10)));
356 ut_asserteq(40000000, get_unaligned((u32 *)(ptr + 12)));
357 ut_asserteq(8, ptr[16]);
358 ut_asserteq(0, ptr[17]);
359 ut_asserteq(0, ptr[18]);
360 ut_asserteq(0, get_unaligned((u16 *)(ptr + 19)));
361 ut_asserteq_str("\\_SB.SPI0", (char *)ptr + 21);
362
363 free_context(&ctx);
364
365 return 0;
366}
Simon Glass1a92f832024-08-22 07:57:48 -0600367DM_TEST(dm_test_acpi_spi, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass0f277632020-07-07 13:11:50 -0600368
Simon Glass0f277632020-07-07 13:11:50 -0600369/* Test emitting a length */
370static int dm_test_acpi_len(struct unit_test_state *uts)
371{
372 const int size = 0xc0000;
373 struct acpi_ctx *ctx;
374 u8 *ptr;
375 int i;
376
Simon Glassc993ff72020-07-07 13:11:56 -0600377 ut_assertok(acpi_test_alloc_context_size(&ctx, size));
Simon Glass0f277632020-07-07 13:11:50 -0600378
379 ptr = acpigen_get_current(ctx);
380
381 /* Write a byte and a 3-byte length */
382 acpigen_write_len_f(ctx);
383 acpigen_emit_byte(ctx, 0x23);
384 acpigen_pop_len(ctx);
Simon Glassc993ff72020-07-07 13:11:56 -0600385 ut_asserteq(1 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600386
387 /* Write 200 bytes so we need two length bytes */
388 ptr = ctx->current;
389 acpigen_write_len_f(ctx);
390 for (i = 0; i < 200; i++)
391 acpigen_emit_byte(ctx, 0x23);
392 acpigen_pop_len(ctx);
Simon Glassc993ff72020-07-07 13:11:56 -0600393 ut_asserteq(200 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600394
395 /* Write 40KB so we need three length bytes */
396 ptr = ctx->current;
397 acpigen_write_len_f(ctx);
398 for (i = 0; i < 40000; i++)
399 acpigen_emit_byte(ctx, 0x23);
400 acpigen_pop_len(ctx);
Simon Glassc993ff72020-07-07 13:11:56 -0600401 ut_asserteq(40000 + 3, acpi_test_get_length(ptr));
Simon Glass0f277632020-07-07 13:11:50 -0600402
403 free_context(&ctx);
404
405 return 0;
406}
407DM_TEST(dm_test_acpi_len, 0);
Simon Glassedc26802020-07-07 13:11:51 -0600408
409/* Test writing a package */
410static int dm_test_acpi_package(struct unit_test_state *uts)
411{
412 struct acpi_ctx *ctx;
413 char *num_elements;
414 u8 *ptr;
415
416 ut_assertok(alloc_context(&ctx));
417
418 ptr = acpigen_get_current(ctx);
419
420 num_elements = acpigen_write_package(ctx, 3);
421 ut_asserteq_ptr(num_elements, ptr + 4);
422
423 /* For ease of testing, just emit a byte, not valid package contents */
424 acpigen_emit_byte(ctx, 0x23);
425 acpigen_pop_len(ctx);
426 ut_asserteq(PACKAGE_OP, ptr[0]);
Simon Glassc993ff72020-07-07 13:11:56 -0600427 ut_asserteq(5, acpi_test_get_length(ptr + 1));
Simon Glassedc26802020-07-07 13:11:51 -0600428 ut_asserteq(3, ptr[4]);
429
430 free_context(&ctx);
431
432 return 0;
433}
434DM_TEST(dm_test_acpi_package, 0);
Simon Glass8715ce02020-07-07 13:11:52 -0600435
436/* Test writing an integer */
437static int dm_test_acpi_integer(struct unit_test_state *uts)
438{
439 struct acpi_ctx *ctx;
440 u8 *ptr;
441
442 ut_assertok(alloc_context(&ctx));
443
444 ptr = acpigen_get_current(ctx);
445
446 acpigen_write_integer(ctx, 0);
447 acpigen_write_integer(ctx, 1);
448 acpigen_write_integer(ctx, TEST_INT8);
449 acpigen_write_integer(ctx, TEST_INT16);
450 acpigen_write_integer(ctx, TEST_INT32);
451 acpigen_write_integer(ctx, TEST_INT64);
452
453 ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr);
454
455 ut_asserteq(ZERO_OP, ptr[0]);
456
457 ut_asserteq(ONE_OP, ptr[1]);
458
459 ut_asserteq(BYTE_PREFIX, ptr[2]);
460 ut_asserteq(TEST_INT8, ptr[3]);
461
462 ut_asserteq(WORD_PREFIX, ptr[4]);
463 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5)));
464
465 ut_asserteq(DWORD_PREFIX, ptr[7]);
466 ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8)));
467
468 ut_asserteq(QWORD_PREFIX, ptr[12]);
469 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13)));
470
471 free_context(&ctx);
472
473 return 0;
474}
475DM_TEST(dm_test_acpi_integer, 0);
Simon Glass45d6d952020-07-07 13:11:53 -0600476
477/* Test writing a string */
478static int dm_test_acpi_string(struct unit_test_state *uts)
479{
480 struct acpi_ctx *ctx;
481 u8 *ptr;
482
483 ut_assertok(alloc_context(&ctx));
484
485 ptr = acpigen_get_current(ctx);
486
487 acpigen_write_string(ctx, TEST_STRING);
488 acpigen_write_string(ctx, TEST_STRING2);
489
490 ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2),
491 acpigen_get_current(ctx) - ptr);
492 ut_asserteq(STRING_PREFIX, ptr[0]);
493 ut_asserteq_str(TEST_STRING, (char *)ptr + 1);
494 ptr += 1 + sizeof(TEST_STRING);
495 ut_asserteq(STRING_PREFIX, ptr[0]);
496 ut_asserteq_str(TEST_STRING2, (char *)ptr + 1);
497
498 free_context(&ctx);
499
500 return 0;
501}
502DM_TEST(dm_test_acpi_string, 0);
Simon Glass0dc3d512020-07-07 13:11:54 -0600503
504/* Test writing a name */
505static int dm_test_acpi_name(struct unit_test_state *uts)
506{
507 struct acpi_ctx *ctx;
508 u8 *ptr;
509
510 ut_assertok(alloc_context(&ctx));
511
512 ptr = acpigen_get_current(ctx);
513
514 /*
515 * The names here are made up for testing the various cases. The
516 * grammar is in the ACPI spec 6.3 section 19.2.2
517 */
518 acpigen_write_name(ctx, "\\_SB");
519 acpigen_write_name(ctx, "\\_SB.I2C0");
520 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2");
521 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2.LONG");
522 acpigen_write_name(ctx, "^^^^SPI0.FLAS");
523 acpigen_write_name(ctx, "NN");
524 acpigen_write_name(ctx, "^AB.CD.D.EFG");
525 acpigen_write_name(ctx, "^^^^");
526 acpigen_write_name(ctx, "\\");
527 acpigen_write_name(ctx, "\\ABCD");
528
529 ut_asserteq(107, acpigen_get_current(ctx) - ptr);
530 ut_asserteq(NAME_OP, ptr[0]);
531 ut_asserteq_strn("\\_SB_", (char *)ptr + 1);
532 ptr += 6;
533
534 ut_asserteq(NAME_OP, ptr[0]);
535 ut_asserteq('\\', ptr[1]);
536 ut_asserteq(DUAL_NAME_PREFIX, ptr[2]);
537 ut_asserteq_strn("_SB_I2C0", (char *)ptr + 3);
538 ptr += 11;
539
540 ut_asserteq(NAME_OP, ptr[0]);
541 ut_asserteq('\\', ptr[1]);
542 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
543 ut_asserteq(3, ptr[3]);
544 ut_asserteq_strn("_SB_I2C0TPM2", (char *)ptr + 4);
545 ptr += 16;
546
547 ut_asserteq(NAME_OP, ptr[0]);
548 ut_asserteq('\\', ptr[1]);
549 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
550 ut_asserteq(4, ptr[3]);
551 ut_asserteq_strn("_SB_I2C0TPM2LONG", (char *)ptr + 4);
552 ptr += 20;
553
554 ut_asserteq(NAME_OP, ptr[0]);
555 ut_asserteq('^', ptr[1]);
556 ut_asserteq('^', ptr[2]);
557 ut_asserteq('^', ptr[3]);
558 ut_asserteq('^', ptr[4]);
559 ut_asserteq(DUAL_NAME_PREFIX, ptr[5]);
560 ut_asserteq_strn("SPI0FLAS", (char *)ptr + 6);
561 ptr += 14;
562
563 ut_asserteq(NAME_OP, ptr[0]);
564 ut_asserteq_strn("NN__", (char *)ptr + 1);
565 ptr += 5;
566
567 ut_asserteq(NAME_OP, ptr[0]);
568 ut_asserteq('^', ptr[1]);
569 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
570 ut_asserteq(4, ptr[3]);
571 ut_asserteq_strn("AB__CD__D___EFG_", (char *)ptr + 4);
572 ptr += 20;
573
574 ut_asserteq(NAME_OP, ptr[0]);
575 ut_asserteq('^', ptr[1]);
576 ut_asserteq('^', ptr[2]);
577 ut_asserteq('^', ptr[3]);
578 ut_asserteq('^', ptr[4]);
579 ut_asserteq(ZERO_OP, ptr[5]);
580 ptr += 6;
581
582 ut_asserteq(NAME_OP, ptr[0]);
583 ut_asserteq('\\', ptr[1]);
584 ut_asserteq(ZERO_OP, ptr[2]);
585 ptr += 3;
586
587 ut_asserteq(NAME_OP, ptr[0]);
588 ut_asserteq_strn("\\ABCD", (char *)ptr + 1);
589 ptr += 5;
590
591 free_context(&ctx);
592
593 return 0;
594}
595DM_TEST(dm_test_acpi_name, 0);
Simon Glass48342b02020-07-07 13:11:55 -0600596
597/* Test writing a UUID */
598static int dm_test_acpi_uuid(struct unit_test_state *uts)
599{
600 struct acpi_ctx *ctx;
601 u8 *ptr;
602
603 ut_assertok(alloc_context(&ctx));
604
605 ptr = acpigen_get_current(ctx);
606
607 ut_assertok(acpigen_write_uuid(ctx,
608 "dbb8e3e6-5886-4ba6-8795-1319f52a966b"));
609 ut_asserteq(23, acpigen_get_current(ctx) - ptr);
610 ut_asserteq(BUFFER_OP, ptr[0]);
Simon Glassc993ff72020-07-07 13:11:56 -0600611 ut_asserteq(22, acpi_test_get_length(ptr + 1));
Simon Glass48342b02020-07-07 13:11:55 -0600612 ut_asserteq(0xdbb8e3e6, get_unaligned((u32 *)(ptr + 7)));
613 ut_asserteq(0x5886, get_unaligned((u16 *)(ptr + 11)));
614 ut_asserteq(0x4ba6, get_unaligned((u16 *)(ptr + 13)));
615 ut_asserteq(0x9587, get_unaligned((u16 *)(ptr + 15)));
616 ut_asserteq(0x2af51913, get_unaligned((u32 *)(ptr + 17)));
617 ut_asserteq(0x6b96, get_unaligned((u16 *)(ptr + 21)));
618
619 /* Try a bad UUID */
620 ut_asserteq(-EINVAL,
621 acpigen_write_uuid(ctx,
622 "dbb8e3e6-5886-4ba6x8795-1319f52a966b"));
623
624 free_context(&ctx);
625
626 return 0;
627}
628DM_TEST(dm_test_acpi_uuid, 0);
Simon Glass9238fac2020-07-07 13:11:59 -0600629
630/* Test writing misc ACPI codes */
631static int dm_test_acpi_misc(struct unit_test_state *uts)
632{
633 struct acpi_ctx *ctx;
634 const int flags = 3;
635 const int nargs = 4;
636 u8 *ptr;
637
638 ut_assertok(alloc_context(&ctx));
639
640 ptr = acpigen_get_current(ctx);
641 acpigen_write_sleep(ctx, TEST_INT64);
642 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 3)));
643 ptr += 11;
644
645 acpigen_write_store(ctx);
646 ut_asserteq(STORE_OP, *ptr);
647 ptr++;
648
649 acpigen_write_debug_string(ctx, TEST_STRING);
650 ut_asserteq_str(TEST_STRING, (char *)ptr + 2);
651 ptr += 2 + sizeof(TEST_STRING);
652 ut_asserteq(EXT_OP_PREFIX, ptr[0]);
653 ut_asserteq(DEBUG_OP, ptr[1]);
654 ptr += 2;
655
656 acpigen_write_sta(ctx, flags);
657 ut_asserteq(METHOD_OP, ptr[0]);
658 ut_asserteq(11, acpi_test_get_length(ptr + 1));
659 ut_asserteq_strn("_STA", (char *)ptr + 4);
660 ut_asserteq(0, ptr[8]);
661 ut_asserteq(RETURN_OP, ptr[9]);
662 ut_asserteq(BYTE_PREFIX, ptr[10]);
663 ut_asserteq(flags, ptr[11]);
664 ptr += 12;
665
666 acpigen_write_sleep(ctx, TEST_INT16);
667 ut_asserteq(SLEEP_OP, ptr[1]);
668 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 3)));
669 ptr += 5;
670
671 acpigen_write_method_serialized(ctx, "FRED", nargs);
672 ut_asserteq(METHOD_OP, ptr[0]);
673 ut_asserteq_strn("FRED", (char *)ptr + 4);
674 ut_asserteq(1 << 3 | nargs, ptr[8]);
675 ut_asserteq(1, ctx->ltop); /* method is unfinished */
676
677 ptr += 9;
678 acpigen_write_or(ctx, LOCAL0_OP, LOCAL1_OP, LOCAL2_OP);
679 acpigen_write_and(ctx, LOCAL3_OP, LOCAL4_OP, LOCAL5_OP);
680 acpigen_write_not(ctx, LOCAL6_OP, LOCAL7_OP);
681 ut_asserteq(OR_OP, ptr[0]);
682 ut_asserteq(LOCAL0_OP, ptr[1]);
683 ut_asserteq(LOCAL1_OP, ptr[2]);
684 ut_asserteq(LOCAL2_OP, ptr[3]);
685
686 ptr += 4;
687 ut_asserteq(AND_OP, ptr[0]);
688 ut_asserteq(LOCAL3_OP, ptr[1]);
689 ut_asserteq(LOCAL4_OP, ptr[2]);
690 ut_asserteq(LOCAL5_OP, ptr[3]);
691
692 ptr += 4;
693 ut_asserteq(NOT_OP, ptr[0]);
694 ut_asserteq(LOCAL6_OP, ptr[1]);
695 ut_asserteq(LOCAL7_OP, ptr[2]);
696 ptr += 3;
697 ut_asserteq_ptr(ptr, ctx->current);
698
699 free_context(&ctx);
700
701 return 0;
702}
703DM_TEST(dm_test_acpi_misc, 0);
Simon Glassd0f7d9b2020-07-07 13:12:00 -0600704
705/* Test writing an ACPI power resource */
706static int dm_test_acpi_power_res(struct unit_test_state *uts)
707{
708 const char *const states[] = { "_PR0", "_PR3" };
709 const char *name = "PRIC";
710 const int level = 3;
711 const int order = 2;
712 struct acpi_ctx *ctx;
713 u8 *ptr;
714
715 ut_assertok(alloc_context(&ctx));
716
717 ptr = acpigen_get_current(ctx);
718
719 /* PowerResource (PRIC, 0, 0) */
720 acpigen_write_power_res(ctx, name, level, order, states,
721 ARRAY_SIZE(states));
722 ut_asserteq(0x28, acpigen_get_current(ctx) - ptr);
723 ut_asserteq(NAME_OP, ptr[0]);
724 ut_asserteq_strn(states[0], (char *)ptr + 1);
725 ut_asserteq(8, acpi_test_get_length(ptr + 6));
726 ut_asserteq_strn(name, (char *)ptr + 0xa);
727
728 ut_asserteq_strn(states[1], (char *)ptr + 0xf);
729 ut_asserteq(8, acpi_test_get_length(ptr + 0x14));
730 ut_asserteq_strn(name, (char *)ptr + 0x18);
731
732 ut_asserteq(POWER_RES_OP, ptr[0x1d]);
733 ut_asserteq_strn(name, (char *)ptr + 0x21);
734 ut_asserteq(level, ptr[0x25]);
735 ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26)));
736
737 /* The length is not set - caller must use acpigen_pop_len() */
738 ut_asserteq(1, ctx->ltop);
739
740 free_context(&ctx);
741
742 return 0;
743}
744DM_TEST(dm_test_acpi_power_res, 0);
Simon Glass8a200762020-07-07 13:12:01 -0600745
746/* Test writing ACPI code to toggle a GPIO */
747static int dm_test_acpi_gpio_toggle(struct unit_test_state *uts)
748{
749 const uint addr = 0x80012;
750 const int txbit = BIT(2);
751 struct gpio_desc desc;
752 struct acpi_gpio gpio;
753 struct acpi_ctx *ctx;
754 struct udevice *dev;
755 u8 *ptr;
756
757 ut_assertok(alloc_context(&ctx));
758
759 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
760 ut_asserteq_str("a-test", dev->name);
761 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
762 ut_assertok(gpio_get_acpi(&desc, &gpio));
763
764 /* Spot-check the results - see sb_gpio_get_acpi() */
765 ptr = acpigen_get_current(ctx);
766 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
767 &gpio, true);
768 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
769 &gpio, false);
770
771 /* Since this GPIO is active low, we expect it to be cleared here */
772 ut_asserteq(STORE_OP, *ptr);
773 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 3);
774 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0xc)));
775 ut_asserteq(LOCAL5_OP, ptr[0x10]);
776
777 ut_asserteq(STORE_OP, ptr[0x11]);
778 ut_asserteq(BYTE_PREFIX, ptr[0x12]);
779 ut_asserteq(txbit, ptr[0x13]);
780 ut_asserteq(LOCAL0_OP, ptr[0x14]);
781
782 ut_asserteq(NOT_OP, ptr[0x15]);
783 ut_asserteq(LOCAL0_OP, ptr[0x16]);
784 ut_asserteq(LOCAL6_OP, ptr[0x17]);
785 ut_asserteq(AND_OP, ptr[0x18]);
786 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x1e);
787 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x27)));
788 ut_asserteq(LOCAL5_OP, ptr[0x2b]);
789
790 /* Now the second one, which should be set */
791 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 0x2f);
792 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x38)));
793 ut_asserteq(LOCAL5_OP, ptr[0x3c]);
794
795 ut_asserteq(STORE_OP, ptr[0x3d]);
796
797 ut_asserteq(OR_OP, ptr[0x41]);
798 ut_asserteq(LOCAL0_OP, ptr[0x43]);
799 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x47);
800 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x50)));
801 ut_asserteq(LOCAL5_OP, ptr[0x54]);
802 ut_asserteq(0x55, acpigen_get_current(ctx) - ptr);
803
804 free_context(&ctx);
805
806 return 0;
807}
Simon Glass1a92f832024-08-22 07:57:48 -0600808DM_TEST(dm_test_acpi_gpio_toggle, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glasscec3af02020-07-07 13:12:02 -0600809
810/* Test writing ACPI code to output power-sequence info */
811static int dm_test_acpi_power_seq(struct unit_test_state *uts)
812{
813 struct gpio_desc reset, enable, stop;
814 const uint addr = 0xc00dc, addr_act_low = 0x80012;
815 const int txbit = BIT(2);
816 struct acpi_ctx *ctx;
817 struct udevice *dev;
818 u8 *ptr;
819
820 ut_assertok(acpi_test_alloc_context_size(&ctx, 400));
821
822 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
823 ut_asserteq_str("a-test", dev->name);
824 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 0, &reset, 0));
825 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 1, &enable, 0));
826 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &stop, 0));
827 ptr = acpigen_get_current(ctx);
828
829 ut_assertok(acpi_device_add_power_res(ctx, txbit, "\\_SB.GPC0",
830 "\\_SB.SPC0", &reset, 2, 3,
831 &enable, 4, 5, &stop, 6, 7));
832 ut_asserteq(0x186, acpigen_get_current(ctx) - ptr);
833 ut_asserteq_strn("PRIC", (char *)ptr + 0x18);
834
835 /* First the 'ON' sequence - spot check */
836 ut_asserteq_strn("_ON_", (char *)ptr + 0x38);
837
838 /* reset set */
839 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x49)));
840 ut_asserteq(OR_OP, ptr[0x52]);
841
842 /* enable set */
843 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x72)));
844 ut_asserteq(OR_OP, ptr[0x7b]);
845
846 /* reset clear */
847 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x9f)));
848 ut_asserteq(NOT_OP, ptr[0xa8]);
849
850 /* stop set (disable, active low) */
851 ut_asserteq(addr_act_low + stop.offset,
852 get_unaligned((u32 *)(ptr + 0xcf)));
853 ut_asserteq(OR_OP, ptr[0xd8]);
854
855 /* Now the 'OFF' sequence */
856 ut_asserteq_strn("_OFF", (char *)ptr + 0xf4);
857
858 /* stop clear (enable, active low) */
859 ut_asserteq(addr_act_low + stop.offset,
860 get_unaligned((u32 *)(ptr + 0x105)));
861 ut_asserteq(NOT_OP, ptr[0x10e]);
862
863 /* reset clear */
864 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x135)));
865 ut_asserteq(OR_OP, ptr[0x13e]);
866
867 /* enable clear */
868 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x162)));
869 ut_asserteq(NOT_OP, ptr[0x16b]);
870
871 free_context(&ctx);
872
873 return 0;
874}
Simon Glass1a92f832024-08-22 07:57:48 -0600875DM_TEST(dm_test_acpi_power_seq, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glassd6814e22020-07-07 21:32:09 -0600876
877/* Test writing values */
878static int dm_test_acpi_write_values(struct unit_test_state *uts)
879{
880 struct acpi_ctx *ctx;
881 u8 *ptr;
882
883 ut_assertok(alloc_context(&ctx));
884 ptr = acpigen_get_current(ctx);
885
886 acpigen_write_zero(ctx);
887 acpigen_write_one(ctx);
888 acpigen_write_byte(ctx, TEST_INT8);
889 acpigen_write_word(ctx, TEST_INT16);
890 acpigen_write_dword(ctx, TEST_INT32);
891 acpigen_write_qword(ctx, TEST_INT64);
892
893 ut_asserteq(ZERO_OP, *ptr++);
894
895 ut_asserteq(ONE_OP, *ptr++);
896
897 ut_asserteq(BYTE_PREFIX, *ptr++);
898 ut_asserteq(TEST_INT8, *ptr++);
899
900 ut_asserteq(WORD_PREFIX, *ptr++);
901 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
902 ptr += 2;
903
904 ut_asserteq(DWORD_PREFIX, *ptr++);
905 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
906 ptr += 4;
907
908 ut_asserteq(QWORD_PREFIX, *ptr++);
909 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
910 ptr += 8;
911
912 ut_asserteq_ptr(ptr, ctx->current);
913
914 free_context(&ctx);
915
916 return 0;
917}
918DM_TEST(dm_test_acpi_write_values, 0);
Simon Glass6fcdaf12020-07-07 21:32:10 -0600919
920/* Test writing a scope */
921static int dm_test_acpi_scope(struct unit_test_state *uts)
922{
923 char buf[ACPI_PATH_MAX];
924 struct acpi_ctx *ctx;
925 struct udevice *dev;
926 u8 *ptr;
927
928 ut_assertok(alloc_context(&ctx));
929 ptr = acpigen_get_current(ctx);
930
931 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
932 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
933 acpigen_write_scope(ctx, buf);
934 acpigen_pop_len(ctx);
935
936 ut_asserteq(SCOPE_OP, *ptr++);
937 ut_asserteq(13, acpi_test_get_length(ptr));
938 ptr += 3;
939 ut_asserteq(ROOT_PREFIX, *ptr++);
940 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
941 ut_asserteq_strn("_SB_" ACPI_TEST_DEV_NAME, (char *)ptr);
942 ptr += 8;
943 ut_asserteq_ptr(ptr, ctx->current);
944
945 free_context(&ctx);
946
947 return 0;
948}
Simon Glass1a92f832024-08-22 07:57:48 -0600949DM_TEST(dm_test_acpi_scope, UTF_SCAN_PDATA | UTF_SCAN_FDT);
Simon Glass837127f2020-07-07 21:32:11 -0600950
951/* Test writing a resource template */
952static int dm_test_acpi_resource_template(struct unit_test_state *uts)
953{
954 struct acpi_gen_regaddr addr;
955 struct acpi_ctx *ctx;
956 u8 *ptr;
957
958 ut_assertok(alloc_context(&ctx));
959 ptr = acpigen_get_current(ctx);
960
961 addr.space_id = ACPI_ADDRESS_SPACE_EC;
962 addr.bit_width = 32;
963 addr.bit_offset = 8;
964 addr.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
965 addr.addrl = TEST_INT64 & 0xffffffff;
966 addr.addrh = TEST_INT64 >> 32;
967 acpigen_write_register_resource(ctx, &addr);
968
969 ut_asserteq(BUFFER_OP, *ptr++);
970 ut_asserteq(0x17, acpi_test_get_length(ptr));
971 ptr += 3;
972 ut_asserteq(WORD_PREFIX, *ptr++);
973 ut_asserteq(0x11, get_unaligned((u16 *)ptr));
974 ptr += 2;
975 ut_asserteq(ACPI_DESCRIPTOR_REGISTER, *ptr++);
976 ut_asserteq(0xc, *ptr++);
977 ut_asserteq(0, *ptr++);
978 ut_asserteq(ACPI_ADDRESS_SPACE_EC, *ptr++);
979 ut_asserteq(32, *ptr++);
980 ut_asserteq(8, *ptr++);
981 ut_asserteq(ACPI_ACCESS_SIZE_DWORD_ACCESS, *ptr++);
982 ut_asserteq(TEST_INT64 & 0xffffffff, get_unaligned((u32 *)ptr));
983 ptr += 4;
984 ut_asserteq(TEST_INT64 >> 32, get_unaligned((u32 *)ptr));
985 ptr += 4;
986 ut_asserteq(ACPI_END_TAG, *ptr++);
987 ut_asserteq(0x00, *ptr++);
988 ut_asserteq_ptr(ptr, ctx->current);
989
990 free_context(&ctx);
991
992 return 0;
993}
994DM_TEST(dm_test_acpi_resource_template, 0);
Simon Glassa186d932020-07-07 21:32:14 -0600995
996/* Test writing a device */
997static int dm_test_acpi_device(struct unit_test_state *uts)
998{
999 struct acpi_ctx *ctx;
1000 u8 *ptr;
1001
1002 ut_assertok(alloc_context(&ctx));
1003 ptr = acpigen_get_current(ctx);
1004
1005 acpigen_write_device(ctx, "\\_SB." ACPI_TEST_DEV_NAME);
1006 acpigen_pop_len(ctx);
1007
1008 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1009 ut_asserteq(DEVICE_OP, *ptr++);
1010 ut_asserteq(0xd, acpi_test_get_length(ptr));
1011 ptr += 3;
1012 ut_asserteq(ROOT_PREFIX, *ptr++);
1013 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
1014 ptr += 8;
1015 ut_asserteq_ptr(ptr, ctx->current);
1016
1017 free_context(&ctx);
1018
1019 return 0;
1020}
1021DM_TEST(dm_test_acpi_device, 0);
Simon Glassb4df0782020-07-07 21:32:15 -06001022
1023/* Test writing named values */
1024static int dm_test_acpi_write_name(struct unit_test_state *uts)
1025{
1026 const char *name = "\\_SB." ACPI_TEST_DEV_NAME;
1027 struct acpi_ctx *ctx;
1028 u8 *ptr;
1029
1030 ut_assertok(alloc_context(&ctx));
1031 ptr = acpigen_get_current(ctx);
1032
1033 acpigen_write_name_zero(ctx, name);
1034 acpigen_write_name_one(ctx, name);
1035 acpigen_write_name_byte(ctx, name, TEST_INT8);
1036 acpigen_write_name_word(ctx, name, TEST_INT16);
1037 acpigen_write_name_dword(ctx, name, TEST_INT32);
1038 acpigen_write_name_qword(ctx, name, TEST_INT64);
1039 acpigen_write_name_integer(ctx, name, TEST_INT64 + 1);
1040 acpigen_write_name_string(ctx, name, "baldrick");
1041 acpigen_write_name_string(ctx, name, NULL);
1042
1043 ut_asserteq(NAME_OP, *ptr++);
1044 ut_asserteq_strn("\\._SB_ABCD", (char *)ptr);
1045 ptr += 10;
1046 ut_asserteq(ZERO_OP, *ptr++);
1047
1048 ut_asserteq(NAME_OP, *ptr++);
1049 ptr += 10;
1050 ut_asserteq(ONE_OP, *ptr++);
1051
1052 ut_asserteq(NAME_OP, *ptr++);
1053 ptr += 10;
1054 ut_asserteq(BYTE_PREFIX, *ptr++);
1055 ut_asserteq(TEST_INT8, *ptr++);
1056
1057 ut_asserteq(NAME_OP, *ptr++);
1058 ptr += 10;
1059 ut_asserteq(WORD_PREFIX, *ptr++);
1060 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
1061 ptr += 2;
1062
1063 ut_asserteq(NAME_OP, *ptr++);
1064 ptr += 10;
1065 ut_asserteq(DWORD_PREFIX, *ptr++);
1066 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
1067 ptr += 4;
1068
1069 ut_asserteq(NAME_OP, *ptr++);
1070 ptr += 10;
1071 ut_asserteq(QWORD_PREFIX, *ptr++);
1072 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
1073 ptr += 8;
1074
1075 ut_asserteq(NAME_OP, *ptr++);
1076 ptr += 10;
1077 ut_asserteq(QWORD_PREFIX, *ptr++);
1078 ut_asserteq_64(TEST_INT64 + 1, get_unaligned((u64 *)ptr));
1079 ptr += 8;
1080
1081 ut_asserteq(NAME_OP, *ptr++);
1082 ptr += 10;
1083 ut_asserteq(STRING_PREFIX, *ptr++);
Marek Vasut1531c4e2023-03-10 04:33:13 +01001084 ut_asserteq_str("baldrick", (char *)ptr);
Simon Glassb4df0782020-07-07 21:32:15 -06001085 ptr += 9;
1086
1087 ut_asserteq(NAME_OP, *ptr++);
1088 ptr += 10;
1089 ut_asserteq(STRING_PREFIX, *ptr++);
1090 ut_asserteq('\0', *ptr++);
1091
1092 ut_asserteq_ptr(ptr, ctx->current);
1093
1094 free_context(&ctx);
1095
1096 return 0;
1097}
1098DM_TEST(dm_test_acpi_write_name, 0);
Simon Glass77dfd032020-09-22 12:44:56 -06001099
1100/* Test emitting a _PRW component */
1101static int dm_test_acpi_write_prw(struct unit_test_state *uts)
1102{
1103 struct acpi_ctx *ctx;
1104 u8 *ptr;
1105
1106 ut_assertok(alloc_context(&ctx));
1107
1108 ptr = acpigen_get_current(ctx);
1109 acpigen_write_prw(ctx, 5, 3);
1110 ut_asserteq(NAME_OP, *ptr++);
1111
1112 ut_asserteq_strn("_PRW", (char *)ptr);
1113 ptr += 4;
1114 ut_asserteq(PACKAGE_OP, *ptr++);
1115 ut_asserteq(8, acpi_test_get_length(ptr));
1116 ptr += 3;
1117 ut_asserteq(2, *ptr++);
1118 ut_asserteq(BYTE_PREFIX, *ptr++);
1119 ut_asserteq(5, *ptr++);
1120 ut_asserteq(BYTE_PREFIX, *ptr++);
1121 ut_asserteq(3, *ptr++);
1122 ut_asserteq_ptr(ptr, ctx->current);
1123
1124 free_context(&ctx);
1125
1126 return 0;
1127}
1128DM_TEST(dm_test_acpi_write_prw, 0);
Simon Glassca727632020-09-22 12:44:57 -06001129
1130/* Test emitting writing conditionals */
1131static int dm_test_acpi_write_cond(struct unit_test_state *uts)
1132{
1133 struct acpi_ctx *ctx;
1134 u8 *ptr;
1135
1136 ut_assertok(alloc_context(&ctx));
1137
1138 ptr = acpigen_get_current(ctx);
1139 acpigen_write_if(ctx);
1140 acpigen_pop_len(ctx);
1141 ut_asserteq(IF_OP, *ptr++);
1142 ut_asserteq(3, acpi_test_get_length(ptr));
1143 ptr += 3;
1144
1145 acpigen_write_else(ctx);
1146 acpigen_pop_len(ctx);
1147 ut_asserteq(ELSE_OP, *ptr++);
1148 ut_asserteq(3, acpi_test_get_length(ptr));
1149 ptr += 3;
1150
1151 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, 5);
1152 acpigen_pop_len(ctx);
1153 ut_asserteq(IF_OP, *ptr++);
1154 ut_asserteq(7, acpi_test_get_length(ptr));
1155 ptr += 3;
1156 ut_asserteq(LEQUAL_OP, *ptr++);
1157 ut_asserteq(LOCAL1_OP, *ptr++);
1158 ut_asserteq(BYTE_PREFIX, *ptr++);
1159 ut_asserteq(5, *ptr++);
1160
1161 ut_asserteq_ptr(ptr, ctx->current);
1162
1163 free_context(&ctx);
1164
1165 return 0;
1166}
1167DM_TEST(dm_test_acpi_write_cond, 0);
1168
1169/* Test emitting writing return values and ToBuffer/ToInteger */
1170static int dm_test_acpi_write_return(struct unit_test_state *uts)
1171{
1172 int len = sizeof(TEST_STRING);
1173 struct acpi_ctx *ctx;
1174 u8 *ptr;
1175
1176 ut_assertok(alloc_context(&ctx));
1177
1178 ptr = acpigen_get_current(ctx);
1179 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
1180 ut_asserteq(TO_BUFFER_OP, *ptr++);
1181 ut_asserteq(ARG0_OP, *ptr++);
1182 ut_asserteq(LOCAL0_OP, *ptr++);
1183
1184 acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP);
1185 ut_asserteq(TO_INTEGER_OP, *ptr++);
1186 ut_asserteq(ARG0_OP, *ptr++);
1187 ut_asserteq(LOCAL0_OP, *ptr++);
1188
1189 acpigen_write_return_byte_buffer(ctx, (u8 *)TEST_STRING, len);
1190 ut_asserteq(RETURN_OP, *ptr++);
1191 ut_asserteq(BUFFER_OP, *ptr++);
1192 ut_asserteq(5 + len, acpi_test_get_length(ptr));
1193 ptr += 3;
1194 ut_asserteq(BYTE_PREFIX, *ptr++);
1195 ut_asserteq(len, *ptr++);
1196 ut_asserteq_mem(TEST_STRING, ptr, len);
1197 ptr += len;
1198
1199 acpigen_write_return_singleton_buffer(ctx, 123);
1200 len = 1;
1201 ut_asserteq(RETURN_OP, *ptr++);
1202 ut_asserteq(BUFFER_OP, *ptr++);
1203 ut_asserteq(4 + len, acpi_test_get_length(ptr));
1204 ptr += 3;
1205 ut_asserteq(ONE_OP, *ptr++);
1206 ut_asserteq(123, *ptr++);
1207
1208 acpigen_write_return_byte(ctx, 43);
1209 ut_asserteq(RETURN_OP, *ptr++);
1210 ut_asserteq(BYTE_PREFIX, *ptr++);
1211 ut_asserteq(43, *ptr++);
1212
1213 ut_asserteq_ptr(ptr, ctx->current);
1214
1215 free_context(&ctx);
1216
1217 return 0;
1218}
1219DM_TEST(dm_test_acpi_write_return, 0);
Simon Glass4c8cbbd2020-09-22 12:44:58 -06001220
1221/* Test emitting a DSM for an I2C HID */
1222static int dm_test_acpi_write_i2c_dsm(struct unit_test_state *uts)
1223{
1224 char uuid_str[UUID_STR_LEN + 1];
1225 const int reg_offset = 0x20;
1226 struct acpi_ctx *ctx;
1227 u8 *ptr;
1228
1229 ut_assertok(alloc_context(&ctx));
1230
1231 ptr = acpigen_get_current(ctx);
1232 ut_assertok(acpi_device_write_dsm_i2c_hid(ctx, reg_offset));
1233
1234 /* acpigen_write_dsm_start() */
1235 ut_asserteq(METHOD_OP, *ptr++);
1236 ut_asserteq(0x78, acpi_test_get_length(ptr));
1237 ptr += 3;
1238 ut_asserteq_strn("_DSM", (char *)ptr);
1239 ptr += 4;
1240 ut_asserteq(ACPI_METHOD_SERIALIZED_MASK | 4, *ptr++);
1241
1242 ut_asserteq(TO_BUFFER_OP, *ptr++);
1243 ut_asserteq(ARG0_OP, *ptr++);
1244 ut_asserteq(LOCAL0_OP, *ptr++);
1245
1246 /* acpigen_write_dsm_uuid_start() */
1247 ut_asserteq(IF_OP, *ptr++);
1248 ut_asserteq(0x65, acpi_test_get_length(ptr));
1249 ptr += 3;
1250 ut_asserteq(LEQUAL_OP, *ptr++);
1251 ut_asserteq(LOCAL0_OP, *ptr++);
1252
1253 ut_asserteq(BUFFER_OP, *ptr++);
1254 ut_asserteq(UUID_BIN_LEN + 6, acpi_test_get_length(ptr));
1255 ptr += 3;
1256 ut_asserteq(WORD_PREFIX, *ptr++);
1257 ut_asserteq(UUID_BIN_LEN, get_unaligned((u16 *)ptr));
1258 ptr += 2;
1259 uuid_bin_to_str(ptr, uuid_str, UUID_STR_FORMAT_GUID);
1260 ut_asserteq_str(ACPI_DSM_I2C_HID_UUID, uuid_str);
1261 ptr += UUID_BIN_LEN;
1262
1263 ut_asserteq(TO_INTEGER_OP, *ptr++);
1264 ut_asserteq(ARG2_OP, *ptr++);
1265 ut_asserteq(LOCAL1_OP, *ptr++);
1266
1267 /* acpigen_write_dsm_uuid_start_cond() */
1268 ut_asserteq(IF_OP, *ptr++);
1269 ut_asserteq(0x34, acpi_test_get_length(ptr));
1270 ptr += 3;
1271 ut_asserteq(LEQUAL_OP, *ptr++);
1272 ut_asserteq(LOCAL1_OP, *ptr++);
1273 ut_asserteq(ZERO_OP, *ptr++);
1274
1275 /*
1276 * See code from acpi_device_write_dsm_i2c_hid(). We don't check every
1277 * piece
1278 */
1279 ut_asserteq(TO_INTEGER_OP, *ptr++);
1280 ut_asserteq(ARG1_OP, *ptr++);
1281 ut_asserteq(LOCAL2_OP, *ptr++);
1282
1283 ut_asserteq(IF_OP, *ptr++);
1284 ut_asserteq(0xd, acpi_test_get_length(ptr));
1285 ptr += 3;
1286 ut_asserteq(LEQUAL_OP, *ptr++);
1287 ut_asserteq(LOCAL2_OP, *ptr++);
1288 ut_asserteq(ZERO_OP, *ptr++); /* function 0 */
1289
1290 ut_asserteq(RETURN_OP, *ptr++);
1291 ut_asserteq(BUFFER_OP, *ptr++);
1292 ptr += 5;
1293
1294 ut_asserteq(ELSE_OP, *ptr++);
1295 ptr += 3;
1296
1297 ut_asserteq(IF_OP, *ptr++);
1298 ut_asserteq(0xd, acpi_test_get_length(ptr));
1299 ptr += 3;
1300 ut_asserteq(LEQUAL_OP, *ptr++);
1301 ut_asserteq(LOCAL2_OP, *ptr++);
1302 ut_asserteq(ONE_OP, *ptr++);
1303
1304 ut_asserteq(RETURN_OP, *ptr++);
1305 ut_asserteq(BUFFER_OP, *ptr++);
1306 ptr += 5;
1307
1308 ut_asserteq(ELSE_OP, *ptr++);
1309 ptr += 3;
1310
1311 ut_asserteq(RETURN_OP, *ptr++);
1312 ut_asserteq(BUFFER_OP, *ptr++);
1313 ptr += 5;
1314
1315 /* acpigen_write_dsm_uuid_start_cond() */
1316 ut_asserteq(IF_OP, *ptr++);
1317 ut_asserteq(9, acpi_test_get_length(ptr));
1318 ptr += 3;
1319 ut_asserteq(LEQUAL_OP, *ptr++);
1320 ut_asserteq(LOCAL1_OP, *ptr++);
1321 ut_asserteq(ONE_OP, *ptr++); /* function 1 */
1322
1323 ut_asserteq(RETURN_OP, *ptr++);
1324 ut_asserteq(BYTE_PREFIX, *ptr++);
1325 ut_asserteq(reg_offset, *ptr++);
1326
1327 /* acpigen_write_dsm_uuid_end() */
1328 ut_asserteq(RETURN_OP, *ptr++);
1329 ut_asserteq(BUFFER_OP, *ptr++);
1330 ptr += 5;
1331
1332 /* acpigen_write_dsm_end */
1333 ut_asserteq(RETURN_OP, *ptr++);
1334 ut_asserteq(BUFFER_OP, *ptr++);
1335 ptr += 5;
1336
1337 ut_asserteq_ptr(ptr, ctx->current);
1338
1339 free_context(&ctx);
1340
1341 return 0;
1342}
1343DM_TEST(dm_test_acpi_write_i2c_dsm, 0);
Simon Glass412dc4b2020-09-22 12:45:11 -06001344
1345/* Test emitting a processor */
1346static int dm_test_acpi_write_processor(struct unit_test_state *uts)
1347{
1348 const int cpuindex = 6;
1349 const u32 pblock_addr = 0x12345600;
1350 const u32 pblock_len = 0x60;
1351 struct acpi_ctx *ctx;
1352 u8 *ptr;
1353
1354 ut_assertok(alloc_context(&ctx));
1355
1356 ptr = acpigen_get_current(ctx);
1357 acpigen_write_processor(ctx, cpuindex, pblock_addr, pblock_len);
1358 acpigen_pop_len(ctx);
1359
1360 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1361 ut_asserteq(PROCESSOR_OP, *ptr++);
1362 ut_asserteq(0x13, acpi_test_get_length(ptr));
1363 ptr += 3;
1364 ut_asserteq_strn("\\._PR_CP06", (char *)ptr);
1365 ptr += 10;
1366 ut_asserteq(cpuindex, *ptr++);
1367 ut_asserteq(pblock_addr, get_unaligned((u32 *)ptr));
1368 ptr += 4;
1369 ut_asserteq(pblock_len, *ptr++);
1370
1371 ut_asserteq_ptr(ptr, ctx->current);
1372
1373 free_context(&ctx);
1374
1375 return 0;
1376}
1377DM_TEST(dm_test_acpi_write_processor, 0);
1378
1379/* Test emitting a processor package */
1380static int dm_test_acpi_write_processor_package(struct unit_test_state *uts)
1381{
1382 const int core_count = 3;
1383 struct acpi_ctx *ctx;
1384 u8 *ptr;
1385
1386 ut_assertok(alloc_context(&ctx));
1387
1388 ptr = acpigen_get_current(ctx);
1389 acpigen_write_processor_package(ctx, "XCPU", 0, core_count);
1390
1391 ut_asserteq(NAME_OP, *ptr++);
1392 ut_asserteq_strn("XCPU", (char *)ptr);
1393 ptr += 4;
1394 ut_asserteq(PACKAGE_OP, *ptr++);
1395 ptr += 3; /* skip length */
1396 ut_asserteq(core_count, *ptr++);
1397
1398 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1399 ptr += 10;
1400 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1401 ptr += 10;
1402 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1403 ptr += 10;
1404
1405 ut_asserteq_ptr(ptr, ctx->current);
1406
1407 free_context(&ctx);
1408
1409 return 0;
1410}
1411DM_TEST(dm_test_acpi_write_processor_package, 0);
1412
1413/* Test emitting a processor notification package */
1414static int dm_test_acpi_write_processor_cnot(struct unit_test_state *uts)
1415{
1416 const int core_count = 3;
1417 struct acpi_ctx *ctx;
1418 u8 *ptr;
1419
1420 ut_assertok(alloc_context(&ctx));
1421
1422 ptr = acpigen_get_current(ctx);
1423 acpigen_write_processor_cnot(ctx, core_count);
1424
1425 ut_asserteq(METHOD_OP, *ptr++);
1426 ptr += 3; /* skip length */
1427 ut_asserteq_strn("\\._PR_CNOT", (char *)ptr);
1428 ptr += 10;
1429 ut_asserteq(1, *ptr++);
1430
1431 ut_asserteq(NOTIFY_OP, *ptr++);
1432 ut_asserteq_strn("\\._PR_CP00", (char *)ptr);
1433 ptr += 10;
1434 ut_asserteq(ARG0_OP, *ptr++);
1435 ut_asserteq(NOTIFY_OP, *ptr++);
1436 ut_asserteq_strn("\\._PR_CP01", (char *)ptr);
1437 ptr += 10;
1438 ut_asserteq(ARG0_OP, *ptr++);
1439 ut_asserteq(NOTIFY_OP, *ptr++);
1440 ut_asserteq_strn("\\._PR_CP02", (char *)ptr);
1441 ptr += 10;
1442 ut_asserteq(ARG0_OP, *ptr++);
1443
1444 ut_asserteq_ptr(ptr, ctx->current);
1445
1446 free_context(&ctx);
1447
1448 return 0;
1449}
1450DM_TEST(dm_test_acpi_write_processor_cnot, 0);
Simon Glass2f8b6af2020-09-22 12:45:13 -06001451
1452/* Test acpigen_write_tpc */
1453static int dm_test_acpi_write_tpc(struct unit_test_state *uts)
1454{
1455 struct acpi_ctx *ctx;
1456 u8 *ptr;
1457
1458 ut_assertok(alloc_context(&ctx));
1459
1460 ptr = acpigen_get_current(ctx);
1461 acpigen_write_tpc(ctx, "\\TLVL");
1462
1463 ut_asserteq(METHOD_OP, *ptr++);
1464 ptr += 3; /* skip length */
1465 ut_asserteq_strn("_TPC", (char *)ptr);
1466 ptr += 4;
1467 ut_asserteq(0, *ptr++);
1468 ut_asserteq(RETURN_OP, *ptr++);
1469 ut_asserteq_strn("\\TLVL", (char *)ptr);
1470 ptr += 5;
1471
1472 ut_asserteq_ptr(ptr, ctx->current);
1473
1474 free_context(&ctx);
1475
1476 return 0;
1477}
1478DM_TEST(dm_test_acpi_write_tpc, 0);
1479
1480/* Test acpigen_write_pss_package(), etc. */
1481static int dm_test_acpi_write_pss_psd(struct unit_test_state *uts)
1482{
1483 struct acpi_ctx *ctx;
1484 u8 *ptr;
1485
1486 ut_assertok(alloc_context(&ctx));
1487
1488 ptr = acpigen_get_current(ctx);
1489 acpigen_write_pss_package(ctx, 1, 2, 3, 4, 5, 6);
1490 ut_asserteq(PACKAGE_OP, *ptr++);
1491 ptr += 3; /* skip length */
1492 ut_asserteq(6, *ptr++);
1493
1494 ut_asserteq(DWORD_PREFIX, *ptr++);
1495 ut_asserteq(1, get_unaligned((u32 *)ptr));
1496 ptr += 5;
1497
1498 ut_asserteq(2, get_unaligned((u32 *)ptr));
1499 ptr += 5;
1500
1501 ut_asserteq(3, get_unaligned((u32 *)ptr));
1502 ptr += 5;
1503
1504 ut_asserteq(4, get_unaligned((u32 *)ptr));
1505 ptr += 5;
1506
1507 ut_asserteq(5, get_unaligned((u32 *)ptr));
1508 ptr += 5;
1509
1510 ut_asserteq(6, get_unaligned((u32 *)ptr));
1511 ptr += 4;
1512
1513 acpigen_write_psd_package(ctx, 6, 7, HW_ALL);
1514 ut_asserteq(NAME_OP, *ptr++);
1515 ut_asserteq_strn("_PSD", (char *)ptr);
1516 ptr += 4;
1517 ut_asserteq(PACKAGE_OP, *ptr++);
1518 ptr += 3; /* skip length */
1519 ut_asserteq(1, *ptr++);
1520 ut_asserteq(PACKAGE_OP, *ptr++);
1521 ptr += 3; /* skip length */
1522 ut_asserteq(5, *ptr++);
1523
1524 ut_asserteq(BYTE_PREFIX, *ptr++);
1525 ut_asserteq(5, *ptr++);
1526 ut_asserteq(BYTE_PREFIX, *ptr++);
1527 ut_asserteq(0, *ptr++);
1528
1529 ut_asserteq(DWORD_PREFIX, *ptr++);
1530 ut_asserteq(6, get_unaligned((u32 *)ptr));
1531 ptr += 5;
1532
1533 ut_asserteq(HW_ALL, get_unaligned((u32 *)ptr));
1534 ptr += 5;
1535
1536 ut_asserteq(7, get_unaligned((u32 *)ptr));
1537 ptr += 4;
1538
1539 ut_asserteq_ptr(ptr, ctx->current);
1540
1541 free_context(&ctx);
1542
1543 return 0;
1544}
1545DM_TEST(dm_test_acpi_write_pss_psd, 0);
1546
1547/* Test acpi_write_cst_package() */
1548static int dm_test_acpi_write_cst(struct unit_test_state *uts)
1549{
1550 static struct acpi_cstate cstate_map[] = {
1551 {
1552 /* C1 */
1553 .ctype = 1, /* ACPI C1 */
1554 .latency = 1,
1555 .power = 1000,
1556 .resource = {
1557 .space_id = ACPI_ADDRESS_SPACE_FIXED,
1558 },
1559 }, {
1560 .ctype = 2, /* ACPI C2 */
1561 .latency = 50,
1562 .power = 10,
1563 .resource = {
1564 .space_id = ACPI_ADDRESS_SPACE_IO,
1565 .bit_width = 8,
1566 .addrl = 0x415,
1567 },
1568 },
1569 };
1570 int nentries = ARRAY_SIZE(cstate_map);
1571 struct acpi_ctx *ctx;
1572 u8 *ptr;
1573 int i;
1574
1575 ut_assertok(alloc_context(&ctx));
1576
1577 ptr = acpigen_get_current(ctx);
1578 acpigen_write_cst_package(ctx, cstate_map, nentries);
1579
1580 ut_asserteq(NAME_OP, *ptr++);
1581 ut_asserteq_strn("_CST", (char *)ptr);
1582 ptr += 4;
1583 ut_asserteq(PACKAGE_OP, *ptr++);
1584 ptr += 3; /* skip length */
1585 ut_asserteq(nentries + 1, *ptr++);
1586 ut_asserteq(DWORD_PREFIX, *ptr++);
1587 ut_asserteq(nentries, get_unaligned((u32 *)ptr));
1588 ptr += 4;
1589
1590 for (i = 0; i < nentries; i++) {
1591 ut_asserteq(PACKAGE_OP, *ptr++);
1592 ptr += 3; /* skip length */
1593 ut_asserteq(4, *ptr++);
1594 ut_asserteq(BUFFER_OP, *ptr++);
1595 ptr += 0x17;
1596 ut_asserteq(DWORD_PREFIX, *ptr++);
1597 ut_asserteq(cstate_map[i].ctype, get_unaligned((u32 *)ptr));
1598 ptr += 5;
1599 ut_asserteq(cstate_map[i].latency, get_unaligned((u32 *)ptr));
1600 ptr += 5;
1601 ut_asserteq(cstate_map[i].power, get_unaligned((u32 *)ptr));
1602 ptr += 4;
1603 }
1604
1605 ut_asserteq_ptr(ptr, ctx->current);
1606
1607 free_context(&ctx);
1608
1609 return 0;
1610}
1611DM_TEST(dm_test_acpi_write_cst, 0);
1612
1613/* Test acpi_write_cst_package() */
1614static int dm_test_acpi_write_csd(struct unit_test_state *uts)
1615{
1616 struct acpi_ctx *ctx;
1617 u8 *ptr;
1618
1619 ut_assertok(alloc_context(&ctx));
1620
1621 ptr = acpigen_get_current(ctx);
1622 acpigen_write_csd_package(ctx, 12, 34, CSD_HW_ALL, 56);
1623
1624 ut_asserteq(NAME_OP, *ptr++);
1625 ut_asserteq_strn("_CSD", (char *)ptr);
1626 ptr += 4;
1627 ut_asserteq(PACKAGE_OP, *ptr++);
1628 ptr += 3; /* skip length */
1629 ut_asserteq(1, *ptr++);
1630 ut_asserteq(PACKAGE_OP, *ptr++);
1631 ptr += 3; /* skip length */
1632 ut_asserteq(6, *ptr++);
1633
1634 ut_asserteq(BYTE_PREFIX, *ptr++);
1635 ut_asserteq(6, *ptr++);
1636 ut_asserteq(BYTE_PREFIX, *ptr++);
1637 ut_asserteq(0, *ptr++);
1638 ut_asserteq(DWORD_PREFIX, *ptr++);
1639 ut_asserteq(12, get_unaligned((u32 *)ptr));
1640 ptr += 5;
1641 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1642 ptr += 5;
1643 ut_asserteq(34, get_unaligned((u32 *)ptr));
1644 ptr += 5;
1645 ut_asserteq(56, get_unaligned((u32 *)ptr));
1646 ptr += 4;
1647
1648 ut_asserteq_ptr(ptr, ctx->current);
1649
1650 free_context(&ctx);
1651
1652 return 0;
1653}
1654DM_TEST(dm_test_acpi_write_csd, 0);
1655
1656/* Test acpigen_write_tss_package() */
1657static int dm_test_acpi_write_tss(struct unit_test_state *uts)
1658{
1659 static struct acpi_tstate tstate_list[] = {
1660 { 1, 2, 3, 4, 5, },
1661 { 6, 7, 8, 9, 10, },
1662 };
1663 int nentries = ARRAY_SIZE(tstate_list);
1664 struct acpi_ctx *ctx;
1665 u8 *ptr;
1666 int i;
1667
1668 ut_assertok(alloc_context(&ctx));
1669
1670 ptr = acpigen_get_current(ctx);
1671 acpigen_write_tss_package(ctx, tstate_list, nentries);
1672
1673 ut_asserteq(NAME_OP, *ptr++);
1674 ut_asserteq_strn("_TSS", (char *)ptr);
1675 ptr += 4;
1676 ut_asserteq(PACKAGE_OP, *ptr++);
1677 ptr += 3; /* skip length */
1678 ut_asserteq(nentries, *ptr++);
1679
1680 for (i = 0; i < nentries; i++) {
1681 ut_asserteq(PACKAGE_OP, *ptr++);
1682 ptr += 3; /* skip length */
1683 ut_asserteq(5, *ptr++);
1684 ut_asserteq(DWORD_PREFIX, *ptr++);
1685 ut_asserteq(tstate_list[i].percent, get_unaligned((u32 *)ptr));
1686 ptr += 5;
1687 ut_asserteq(tstate_list[i].power, get_unaligned((u32 *)ptr));
1688 ptr += 5;
1689 ut_asserteq(tstate_list[i].latency, get_unaligned((u32 *)ptr));
1690 ptr += 5;
1691 ut_asserteq(tstate_list[i].control, get_unaligned((u32 *)ptr));
1692 ptr += 5;
1693 ut_asserteq(tstate_list[i].status, get_unaligned((u32 *)ptr));
1694 ptr += 4;
1695 }
1696
1697 ut_asserteq_ptr(ptr, ctx->current);
1698
1699 free_context(&ctx);
1700
1701 return 0;
1702}
1703DM_TEST(dm_test_acpi_write_tss, 0);
1704
1705/* Test acpigen_write_tsd_package() */
1706static int dm_test_acpi_write_tsd_package(struct unit_test_state *uts)
1707{
1708 struct acpi_ctx *ctx;
1709 u8 *ptr;
1710
1711 ut_assertok(alloc_context(&ctx));
1712
1713 ptr = acpigen_get_current(ctx);
1714 acpigen_write_tsd_package(ctx, 12, 34, HW_ALL);
1715
1716 ut_asserteq(NAME_OP, *ptr++);
1717 ut_asserteq_strn("_TSD", (char *)ptr);
1718 ptr += 4;
1719 ut_asserteq(PACKAGE_OP, *ptr++);
1720 ptr += 3; /* skip length */
1721 ut_asserteq(1, *ptr++);
1722 ut_asserteq(PACKAGE_OP, *ptr++);
1723 ptr += 3; /* skip length */
1724 ut_asserteq(5, *ptr++);
1725
1726 ut_asserteq(BYTE_PREFIX, *ptr++);
1727 ut_asserteq(5, *ptr++);
1728 ut_asserteq(BYTE_PREFIX, *ptr++);
1729 ut_asserteq(0, *ptr++);
1730 ut_asserteq(DWORD_PREFIX, *ptr++);
1731 ut_asserteq(12, get_unaligned((u32 *)ptr));
1732 ptr += 5;
1733 ut_asserteq(CSD_HW_ALL, get_unaligned((u32 *)ptr));
1734 ptr += 5;
1735 ut_asserteq(34, get_unaligned((u32 *)ptr));
1736 ptr += 4;
1737
1738 ut_asserteq_ptr(ptr, ctx->current);
1739
1740 free_context(&ctx);
1741
1742 return 0;
1743}
1744DM_TEST(dm_test_acpi_write_tsd_package, 0);