blob: df062a210c73dcdb1b04a9b8fe0345f00b70e9fd [file] [log] [blame]
Simon Glass61cc9332020-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
9#include <common.h>
10#include <dm.h>
Simon Glassff715c62020-07-07 13:11:43 -060011#include <irq.h>
Simon Glass61cc9332020-07-07 13:11:42 -060012#include <malloc.h>
13#include <acpi/acpigen.h>
Simon Glassff715c62020-07-07 13:11:43 -060014#include <acpi/acpi_device.h>
Simon Glassa9e0a072020-07-07 13:11:46 -060015#include <asm/gpio.h>
Simon Glass61cc9332020-07-07 13:11:42 -060016#include <asm/unaligned.h>
17#include <dm/acpi.h>
18#include <dm/test.h>
Simon Glass4ebc9402020-07-07 13:11:47 -060019#include <dm/uclass-internal.h>
Simon Glass61cc9332020-07-07 13:11:42 -060020#include <test/ut.h>
Simon Glass0e5a0a02020-07-07 13:11:56 -060021#include "acpi.h"
Simon Glass61cc9332020-07-07 13:11:42 -060022
23/* Maximum size of the ACPI context needed for most tests */
24#define ACPI_CONTEXT_SIZE 150
25
Simon Glass7fb8da42020-07-07 13:11:45 -060026#define TEST_STRING "frogmore"
Simon Glass3df33bd2020-07-07 13:11:53 -060027#define TEST_STRING2 "ranch"
Simon Glass7fb8da42020-07-07 13:11:45 -060028#define TEST_STREAM2 "\xfa\xde"
29
Simon Glass83b2bd52020-07-07 13:11:52 -060030#define TEST_INT8 0x7d
31#define TEST_INT16 0x2345
32#define TEST_INT32 0x12345678
33#define TEST_INT64 0x4567890123456
34
Simon Glass0e5a0a02020-07-07 13:11:56 -060035int acpi_test_alloc_context_size(struct acpi_ctx **ctxp, int size)
Simon Glass61cc9332020-07-07 13:11:42 -060036{
37 struct acpi_ctx *ctx;
38
39 *ctxp = NULL;
40 ctx = malloc(sizeof(*ctx));
41 if (!ctx)
42 return -ENOMEM;
Simon Glass7e148f22020-07-07 13:11:50 -060043 ctx->base = malloc(size);
Simon Glass61cc9332020-07-07 13:11:42 -060044 if (!ctx->base) {
45 free(ctx);
46 return -ENOMEM;
47 }
Simon Glass7e148f22020-07-07 13:11:50 -060048 ctx->ltop = 0;
Simon Glass61cc9332020-07-07 13:11:42 -060049 ctx->current = ctx->base;
50 *ctxp = ctx;
51
52 return 0;
53}
54
Simon Glass0e5a0a02020-07-07 13:11:56 -060055int acpi_test_get_length(u8 *ptr)
56{
57 if (!(*ptr & 0x80))
58 return -EINVAL;
59
60 return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
61}
62
Simon Glass7e148f22020-07-07 13:11:50 -060063static int alloc_context(struct acpi_ctx **ctxp)
64{
Simon Glass0e5a0a02020-07-07 13:11:56 -060065 return acpi_test_alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
Simon Glass7e148f22020-07-07 13:11:50 -060066}
67
Simon Glass61cc9332020-07-07 13:11:42 -060068static void free_context(struct acpi_ctx **ctxp)
69{
70 free((*ctxp)->base);
71 free(*ctxp);
72 *ctxp = NULL;
73}
74
75/* Test emitting simple types and acpigen_get_current() */
76static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
77{
78 struct acpi_ctx *ctx;
79 u8 *ptr;
80
81 ut_assertok(alloc_context(&ctx));
82
83 ptr = acpigen_get_current(ctx);
84 acpigen_emit_byte(ctx, 0x23);
85 ut_asserteq(1, acpigen_get_current(ctx) - ptr);
86 ut_asserteq(0x23, *(u8 *)ptr);
87
88 acpigen_emit_word(ctx, 0x1234);
89 ut_asserteq(3, acpigen_get_current(ctx) - ptr);
90 ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1)));
91
92 acpigen_emit_dword(ctx, 0x87654321);
93 ut_asserteq(7, acpigen_get_current(ctx) - ptr);
94 ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3)));
95
96 free_context(&ctx);
97
98 return 0;
99}
100DM_TEST(dm_test_acpi_emit_simple, 0);
Simon Glassff715c62020-07-07 13:11:43 -0600101
Simon Glass7fb8da42020-07-07 13:11:45 -0600102/* Test emitting a stream */
103static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
104{
105 struct acpi_ctx *ctx;
106 u8 *ptr;
107
108 ut_assertok(alloc_context(&ctx));
109
110 ptr = acpigen_get_current(ctx);
111 acpigen_emit_stream(ctx, TEST_STREAM2, 2);
112 ut_asserteq(2, acpigen_get_current(ctx) - ptr);
113 ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
114 ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
115
116 free_context(&ctx);
117
118 return 0;
119}
120DM_TEST(dm_test_acpi_emit_stream, 0);
121
122/* Test emitting a string */
123static int dm_test_acpi_emit_string(struct unit_test_state *uts)
124{
125 struct acpi_ctx *ctx;
126 u8 *ptr;
127
128 ut_assertok(alloc_context(&ctx));
129
130 ptr = acpigen_get_current(ctx);
131 acpigen_emit_string(ctx, TEST_STRING);
132 ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
133 ut_asserteq_str(TEST_STRING, (char *)ptr);
134
135 free_context(&ctx);
136
137 return 0;
138}
139DM_TEST(dm_test_acpi_emit_string, 0);
140
Simon Glassff715c62020-07-07 13:11:43 -0600141/* Test emitting an interrupt descriptor */
142static int dm_test_acpi_interrupt(struct unit_test_state *uts)
143{
144 struct acpi_ctx *ctx;
145 struct udevice *dev;
146 struct irq irq;
147 u8 *ptr;
148
149 ut_assertok(alloc_context(&ctx));
150
151 ptr = acpigen_get_current(ctx);
152
153 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
154 ut_assertok(irq_get_by_index(dev, 0, &irq));
155
156 /* See a-test, property interrupts-extended in the device tree */
157 ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq));
158 ut_asserteq(9, acpigen_get_current(ctx) - ptr);
159 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
160 ut_asserteq(6, get_unaligned((u16 *)(ptr + 1)));
161 ut_asserteq(0x19, ptr[3]);
162 ut_asserteq(1, ptr[4]);
163 ut_asserteq(3, get_unaligned((u32 *)(ptr + 5)));
164
165 free_context(&ctx);
166
167 return 0;
168}
169DM_TEST(dm_test_acpi_interrupt, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glassa9e0a072020-07-07 13:11:46 -0600170
171/* Test emitting a GPIO descriptor */
172static int dm_test_acpi_gpio(struct unit_test_state *uts)
173{
174 struct gpio_desc desc;
175 struct acpi_ctx *ctx;
176 struct udevice *dev;
177 u8 *ptr;
178
179 ut_assertok(alloc_context(&ctx));
180
181 ptr = acpigen_get_current(ctx);
182
183 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
184 ut_asserteq_str("a-test", dev->name);
185 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
186
187 /* This should write GPIO pin 4 (see device tree test.dts ) */
188 ut_asserteq(4, acpi_device_write_gpio_desc(ctx, &desc));
189 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
190 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
191 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
192 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
193 ut_asserteq(ACPI_GPIO_TYPE_IO, ptr[4]);
194 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
195 ut_asserteq(9, get_unaligned((u16 *)(ptr + 7)));
196 ut_asserteq(ACPI_GPIO_PULL_UP, ptr[9]);
197 ut_asserteq(1234, get_unaligned((u16 *)(ptr + 10)));
198 ut_asserteq(0, get_unaligned((u16 *)(ptr + 12)));
199 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
200 ut_asserteq(0, ptr[16]);
201 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
202 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
203 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
204
205 /* pin0 */
206 ut_asserteq(4, get_unaligned((u16 *)(ptr + 23)));
207
208 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
209
210 free_context(&ctx);
211
212 return 0;
213}
214DM_TEST(dm_test_acpi_gpio, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
215
216/* Test emitting a GPIO descriptor with an interrupt */
217static int dm_test_acpi_gpio_irq(struct unit_test_state *uts)
218{
219 struct gpio_desc desc;
220 struct acpi_ctx *ctx;
221 struct udevice *dev;
222 u8 *ptr;
223
224 ut_assertok(alloc_context(&ctx));
225
226 ptr = acpigen_get_current(ctx);
227
228 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
229 ut_asserteq_str("a-test", dev->name);
230 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
231
232 /* This should write GPIO pin 6 (see device tree test.dts ) */
233 ut_asserteq(6, acpi_device_write_gpio_desc(ctx, &desc));
234 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
235 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
236 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
237 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
238 ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, ptr[4]);
239 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
240 ut_asserteq(29, get_unaligned((u16 *)(ptr + 7)));
241 ut_asserteq(ACPI_GPIO_PULL_DOWN, ptr[9]);
242 ut_asserteq(0, get_unaligned((u16 *)(ptr + 10)));
243 ut_asserteq(4321, get_unaligned((u16 *)(ptr + 12)));
244 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
245 ut_asserteq(0, ptr[16]);
246 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
247 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
248 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
249
250 /* pin0 */
251 ut_asserteq(6, get_unaligned((u16 *)(ptr + 23)));
252
253 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
254
255 free_context(&ctx);
256
257 return 0;
258}
259DM_TEST(dm_test_acpi_gpio_irq, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass4ebc9402020-07-07 13:11:47 -0600260
261/* Test emitting either a GPIO or interrupt descriptor */
262static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts)
263{
264 struct acpi_ctx *ctx;
265 struct udevice *dev;
266 u8 *ptr;
267
268 ut_assertok(alloc_context(&ctx));
269
270 ptr = acpigen_get_current(ctx);
271
272 /* This should produce an interrupt, even though it also has a GPIO */
273 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
274 ut_asserteq_str("a-test", dev->name);
275 ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev,
276 "test2-gpios"));
277 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
278
279 /* This has no interrupt so should produce a GPIO */
280 ptr = ctx->current;
281 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev));
282 ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev,
283 "enable-gpios"));
284 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
285
286 /* This one has neither */
287 ptr = acpigen_get_current(ctx);
288 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
289 ut_asserteq_str("b-test", dev->name);
290 ut_asserteq(-ENOENT,
291 acpi_device_write_interrupt_or_gpio(ctx, dev,
292 "enable-gpios"));
293
294 free_context(&ctx);
295
296 return 0;
297}
298DM_TEST(dm_test_acpi_interrupt_or_gpio,
299 DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass31e17872020-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}
331DM_TEST(dm_test_acpi_i2c, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass70e5e672020-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}
367DM_TEST(dm_test_acpi_spi, DM_TESTF_SCAN_PDATA | DM_TESTF_SCAN_FDT);
Simon Glass7e148f22020-07-07 13:11:50 -0600368
Simon Glass7e148f22020-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 Glass0e5a0a02020-07-07 13:11:56 -0600377 ut_assertok(acpi_test_alloc_context_size(&ctx, size));
Simon Glass7e148f22020-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 Glass0e5a0a02020-07-07 13:11:56 -0600385 ut_asserteq(1 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-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 Glass0e5a0a02020-07-07 13:11:56 -0600393 ut_asserteq(200 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-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 Glass0e5a0a02020-07-07 13:11:56 -0600401 ut_asserteq(40000 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-07-07 13:11:50 -0600402
403 free_context(&ctx);
404
405 return 0;
406}
407DM_TEST(dm_test_acpi_len, 0);
Simon Glass03967ce2020-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 Glass0e5a0a02020-07-07 13:11:56 -0600427 ut_asserteq(5, acpi_test_get_length(ptr + 1));
Simon Glass03967ce2020-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 Glass83b2bd52020-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 Glass3df33bd2020-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 Glass7aed90d2020-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 Glass29df8452020-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 Glass0e5a0a02020-07-07 13:11:56 -0600611 ut_asserteq(22, acpi_test_get_length(ptr + 1));
Simon Glass29df8452020-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 Glass9c70e7e2020-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 Glassf9189d52020-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);