blob: 62d48dc419ca35b5ff344265d627ee5740bed042 [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>
Simon Glass88490e12020-09-22 12:44:58 -060013#include <uuid.h>
Simon Glass61cc9332020-07-07 13:11:42 -060014#include <acpi/acpigen.h>
Simon Glassff715c62020-07-07 13:11:43 -060015#include <acpi/acpi_device.h>
Simon Glassd7d631d2020-07-07 21:32:11 -060016#include <acpi/acpi_table.h>
Simon Glassa9e0a072020-07-07 13:11:46 -060017#include <asm/gpio.h>
Simon Glass61cc9332020-07-07 13:11:42 -060018#include <asm/unaligned.h>
19#include <dm/acpi.h>
20#include <dm/test.h>
Simon Glass4ebc9402020-07-07 13:11:47 -060021#include <dm/uclass-internal.h>
Simon Glass61cc9332020-07-07 13:11:42 -060022#include <test/ut.h>
Simon Glass0e5a0a02020-07-07 13:11:56 -060023#include "acpi.h"
Simon Glass61cc9332020-07-07 13:11:42 -060024
25/* Maximum size of the ACPI context needed for most tests */
26#define ACPI_CONTEXT_SIZE 150
27
Simon Glass7fb8da42020-07-07 13:11:45 -060028#define TEST_STRING "frogmore"
Simon Glass3df33bd2020-07-07 13:11:53 -060029#define TEST_STRING2 "ranch"
Simon Glass7fb8da42020-07-07 13:11:45 -060030#define TEST_STREAM2 "\xfa\xde"
31
Simon Glass83b2bd52020-07-07 13:11:52 -060032#define TEST_INT8 0x7d
33#define TEST_INT16 0x2345
34#define TEST_INT32 0x12345678
35#define TEST_INT64 0x4567890123456
36
Simon Glass0e5a0a02020-07-07 13:11:56 -060037int acpi_test_alloc_context_size(struct acpi_ctx **ctxp, int size)
Simon Glass61cc9332020-07-07 13:11:42 -060038{
39 struct acpi_ctx *ctx;
40
41 *ctxp = NULL;
42 ctx = malloc(sizeof(*ctx));
43 if (!ctx)
44 return -ENOMEM;
Simon Glass7e148f22020-07-07 13:11:50 -060045 ctx->base = malloc(size);
Simon Glass61cc9332020-07-07 13:11:42 -060046 if (!ctx->base) {
47 free(ctx);
48 return -ENOMEM;
49 }
Simon Glass7e148f22020-07-07 13:11:50 -060050 ctx->ltop = 0;
Simon Glass61cc9332020-07-07 13:11:42 -060051 ctx->current = ctx->base;
52 *ctxp = ctx;
53
54 return 0;
55}
56
Simon Glass0e5a0a02020-07-07 13:11:56 -060057int acpi_test_get_length(u8 *ptr)
58{
59 if (!(*ptr & 0x80))
60 return -EINVAL;
61
62 return (*ptr & 0xf) | ptr[1] << 4 | ptr[2] << 12;
63}
64
Simon Glass7e148f22020-07-07 13:11:50 -060065static int alloc_context(struct acpi_ctx **ctxp)
66{
Simon Glass0e5a0a02020-07-07 13:11:56 -060067 return acpi_test_alloc_context_size(ctxp, ACPI_CONTEXT_SIZE);
Simon Glass7e148f22020-07-07 13:11:50 -060068}
69
Simon Glass61cc9332020-07-07 13:11:42 -060070static void free_context(struct acpi_ctx **ctxp)
71{
72 free((*ctxp)->base);
73 free(*ctxp);
74 *ctxp = NULL;
75}
76
77/* Test emitting simple types and acpigen_get_current() */
78static int dm_test_acpi_emit_simple(struct unit_test_state *uts)
79{
80 struct acpi_ctx *ctx;
81 u8 *ptr;
82
83 ut_assertok(alloc_context(&ctx));
84
85 ptr = acpigen_get_current(ctx);
86 acpigen_emit_byte(ctx, 0x23);
87 ut_asserteq(1, acpigen_get_current(ctx) - ptr);
88 ut_asserteq(0x23, *(u8 *)ptr);
89
90 acpigen_emit_word(ctx, 0x1234);
91 ut_asserteq(3, acpigen_get_current(ctx) - ptr);
92 ut_asserteq(0x1234, get_unaligned((u16 *)(ptr + 1)));
93
94 acpigen_emit_dword(ctx, 0x87654321);
95 ut_asserteq(7, acpigen_get_current(ctx) - ptr);
96 ut_asserteq(0x87654321, get_unaligned((u32 *)(ptr + 3)));
97
98 free_context(&ctx);
99
100 return 0;
101}
102DM_TEST(dm_test_acpi_emit_simple, 0);
Simon Glassff715c62020-07-07 13:11:43 -0600103
Simon Glass7fb8da42020-07-07 13:11:45 -0600104/* Test emitting a stream */
105static int dm_test_acpi_emit_stream(struct unit_test_state *uts)
106{
107 struct acpi_ctx *ctx;
108 u8 *ptr;
109
110 ut_assertok(alloc_context(&ctx));
111
112 ptr = acpigen_get_current(ctx);
113 acpigen_emit_stream(ctx, TEST_STREAM2, 2);
114 ut_asserteq(2, acpigen_get_current(ctx) - ptr);
115 ut_asserteq((u8)TEST_STREAM2[0], ptr[0]);
116 ut_asserteq((u8)TEST_STREAM2[1], ptr[1]);
117
118 free_context(&ctx);
119
120 return 0;
121}
122DM_TEST(dm_test_acpi_emit_stream, 0);
123
124/* Test emitting a string */
125static int dm_test_acpi_emit_string(struct unit_test_state *uts)
126{
127 struct acpi_ctx *ctx;
128 u8 *ptr;
129
130 ut_assertok(alloc_context(&ctx));
131
132 ptr = acpigen_get_current(ctx);
133 acpigen_emit_string(ctx, TEST_STRING);
134 ut_asserteq(sizeof(TEST_STRING), acpigen_get_current(ctx) - ptr);
135 ut_asserteq_str(TEST_STRING, (char *)ptr);
136
137 free_context(&ctx);
138
139 return 0;
140}
141DM_TEST(dm_test_acpi_emit_string, 0);
142
Simon Glassff715c62020-07-07 13:11:43 -0600143/* Test emitting an interrupt descriptor */
144static int dm_test_acpi_interrupt(struct unit_test_state *uts)
145{
146 struct acpi_ctx *ctx;
147 struct udevice *dev;
148 struct irq irq;
149 u8 *ptr;
150
151 ut_assertok(alloc_context(&ctx));
152
153 ptr = acpigen_get_current(ctx);
154
155 ut_assertok(uclass_first_device_err(UCLASS_TEST_FDT, &dev));
156 ut_assertok(irq_get_by_index(dev, 0, &irq));
157
158 /* See a-test, property interrupts-extended in the device tree */
159 ut_asserteq(3, acpi_device_write_interrupt_irq(ctx, &irq));
160 ut_asserteq(9, acpigen_get_current(ctx) - ptr);
161 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
162 ut_asserteq(6, get_unaligned((u16 *)(ptr + 1)));
163 ut_asserteq(0x19, ptr[3]);
164 ut_asserteq(1, ptr[4]);
165 ut_asserteq(3, get_unaligned((u32 *)(ptr + 5)));
166
167 free_context(&ctx);
168
169 return 0;
170}
Simon Glasse180c2b2020-07-28 19:41:12 -0600171DM_TEST(dm_test_acpi_interrupt, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa9e0a072020-07-07 13:11:46 -0600172
173/* Test emitting a GPIO descriptor */
174static int dm_test_acpi_gpio(struct unit_test_state *uts)
175{
176 struct gpio_desc desc;
177 struct acpi_ctx *ctx;
178 struct udevice *dev;
179 u8 *ptr;
180
181 ut_assertok(alloc_context(&ctx));
182
183 ptr = acpigen_get_current(ctx);
184
185 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
186 ut_asserteq_str("a-test", dev->name);
187 ut_assertok(gpio_request_by_name(dev, "test-gpios", 1, &desc, 0));
188
189 /* This should write GPIO pin 4 (see device tree test.dts ) */
190 ut_asserteq(4, acpi_device_write_gpio_desc(ctx, &desc));
191 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
192 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
193 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
194 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
195 ut_asserteq(ACPI_GPIO_TYPE_IO, ptr[4]);
196 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
197 ut_asserteq(9, get_unaligned((u16 *)(ptr + 7)));
198 ut_asserteq(ACPI_GPIO_PULL_UP, ptr[9]);
199 ut_asserteq(1234, get_unaligned((u16 *)(ptr + 10)));
200 ut_asserteq(0, get_unaligned((u16 *)(ptr + 12)));
201 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
202 ut_asserteq(0, ptr[16]);
203 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
204 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
205 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
206
207 /* pin0 */
208 ut_asserteq(4, get_unaligned((u16 *)(ptr + 23)));
209
210 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
211
212 free_context(&ctx);
213
214 return 0;
215}
Simon Glasse180c2b2020-07-28 19:41:12 -0600216DM_TEST(dm_test_acpi_gpio, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassa9e0a072020-07-07 13:11:46 -0600217
218/* Test emitting a GPIO descriptor with an interrupt */
219static int dm_test_acpi_gpio_irq(struct unit_test_state *uts)
220{
221 struct gpio_desc desc;
222 struct acpi_ctx *ctx;
223 struct udevice *dev;
224 u8 *ptr;
225
226 ut_assertok(alloc_context(&ctx));
227
228 ptr = acpigen_get_current(ctx);
229
230 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
231 ut_asserteq_str("a-test", dev->name);
232 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
233
234 /* This should write GPIO pin 6 (see device tree test.dts ) */
235 ut_asserteq(6, acpi_device_write_gpio_desc(ctx, &desc));
236 ut_asserteq(35, acpigen_get_current(ctx) - ptr);
237 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
238 ut_asserteq(32, get_unaligned((u16 *)(ptr + 1)));
239 ut_asserteq(ACPI_GPIO_REVISION_ID, ptr[3]);
240 ut_asserteq(ACPI_GPIO_TYPE_INTERRUPT, ptr[4]);
241 ut_asserteq(1, get_unaligned((u16 *)(ptr + 5)));
242 ut_asserteq(29, get_unaligned((u16 *)(ptr + 7)));
243 ut_asserteq(ACPI_GPIO_PULL_DOWN, ptr[9]);
244 ut_asserteq(0, get_unaligned((u16 *)(ptr + 10)));
245 ut_asserteq(4321, get_unaligned((u16 *)(ptr + 12)));
246 ut_asserteq(23, get_unaligned((u16 *)(ptr + 14)));
247 ut_asserteq(0, ptr[16]);
248 ut_asserteq(25, get_unaligned((u16 *)(ptr + 17)));
249 ut_asserteq(35, get_unaligned((u16 *)(ptr + 19)));
250 ut_asserteq(0, get_unaligned((u16 *)(ptr + 21)));
251
252 /* pin0 */
253 ut_asserteq(6, get_unaligned((u16 *)(ptr + 23)));
254
255 ut_asserteq_str("\\_SB.PINC", (char *)ptr + 25);
256
257 free_context(&ctx);
258
259 return 0;
260}
Simon Glasse180c2b2020-07-28 19:41:12 -0600261DM_TEST(dm_test_acpi_gpio_irq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass4ebc9402020-07-07 13:11:47 -0600262
263/* Test emitting either a GPIO or interrupt descriptor */
264static int dm_test_acpi_interrupt_or_gpio(struct unit_test_state *uts)
265{
266 struct acpi_ctx *ctx;
267 struct udevice *dev;
268 u8 *ptr;
269
270 ut_assertok(alloc_context(&ctx));
271
272 ptr = acpigen_get_current(ctx);
273
274 /* This should produce an interrupt, even though it also has a GPIO */
275 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
276 ut_asserteq_str("a-test", dev->name);
277 ut_asserteq(3, acpi_device_write_interrupt_or_gpio(ctx, dev,
278 "test2-gpios"));
279 ut_asserteq(ACPI_DESCRIPTOR_INTERRUPT, ptr[0]);
280
281 /* This has no interrupt so should produce a GPIO */
282 ptr = ctx->current;
283 ut_assertok(uclass_find_first_device(UCLASS_PANEL_BACKLIGHT, &dev));
284 ut_asserteq(1, acpi_device_write_interrupt_or_gpio(ctx, dev,
285 "enable-gpios"));
286 ut_asserteq(ACPI_DESCRIPTOR_GPIO, ptr[0]);
287
288 /* This one has neither */
289 ptr = acpigen_get_current(ctx);
290 ut_assertok(uclass_get_device_by_seq(UCLASS_TEST_FDT, 3, &dev));
291 ut_asserteq_str("b-test", dev->name);
292 ut_asserteq(-ENOENT,
293 acpi_device_write_interrupt_or_gpio(ctx, dev,
294 "enable-gpios"));
295
296 free_context(&ctx);
297
298 return 0;
299}
300DM_TEST(dm_test_acpi_interrupt_or_gpio,
Simon Glasse180c2b2020-07-28 19:41:12 -0600301 UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass31e17872020-07-07 13:11:48 -0600302
303/* Test emitting an I2C descriptor */
304static int dm_test_acpi_i2c(struct unit_test_state *uts)
305{
306 struct acpi_ctx *ctx;
307 struct udevice *dev;
308 u8 *ptr;
309
310 ut_assertok(alloc_context(&ctx));
311
312 ptr = acpigen_get_current(ctx);
313
314 ut_assertok(uclass_get_device(UCLASS_RTC, 0, &dev));
315 ut_asserteq(0x43, acpi_device_write_i2c_dev(ctx, dev));
316 ut_asserteq(28, acpigen_get_current(ctx) - ptr);
317 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
318 ut_asserteq(25, get_unaligned((u16 *)(ptr + 1)));
319 ut_asserteq(ACPI_I2C_SERIAL_BUS_REVISION_ID, ptr[3]);
320 ut_asserteq(0, ptr[4]);
321 ut_asserteq(ACPI_SERIAL_BUS_TYPE_I2C, ptr[5]);
322 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
323 ut_asserteq(ACPI_I2C_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
324 ut_asserteq(6, get_unaligned((u16 *)(ptr + 10)));
325 ut_asserteq(100000, get_unaligned((u32 *)(ptr + 12)));
326 ut_asserteq(0x43, get_unaligned((u16 *)(ptr + 16)));
327 ut_asserteq_str("\\_SB.I2C0", (char *)ptr + 18);
328
329 free_context(&ctx);
330
331 return 0;
332}
Simon Glasse180c2b2020-07-28 19:41:12 -0600333DM_TEST(dm_test_acpi_i2c, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass70e5e672020-07-07 13:11:49 -0600334
335/* Test emitting a SPI descriptor */
336static int dm_test_acpi_spi(struct unit_test_state *uts)
337{
338 struct acpi_ctx *ctx;
339 struct udevice *dev;
340 u8 *ptr;
341
342 ut_assertok(alloc_context(&ctx));
343
344 ptr = acpigen_get_current(ctx);
345
346 ut_assertok(uclass_first_device_err(UCLASS_SPI_FLASH, &dev));
347 ut_assertok(acpi_device_write_spi_dev(ctx, dev));
348 ut_asserteq(31, acpigen_get_current(ctx) - ptr);
349 ut_asserteq(ACPI_DESCRIPTOR_SERIAL_BUS, ptr[0]);
350 ut_asserteq(28, get_unaligned((u16 *)(ptr + 1)));
351 ut_asserteq(ACPI_SPI_SERIAL_BUS_REVISION_ID, ptr[3]);
352 ut_asserteq(0, ptr[4]);
353 ut_asserteq(ACPI_SERIAL_BUS_TYPE_SPI, ptr[5]);
354 ut_asserteq(2, ptr[6]);
355 ut_asserteq(0, get_unaligned((u16 *)(ptr + 7)));
356 ut_asserteq(ACPI_SPI_TYPE_SPECIFIC_REVISION_ID, ptr[9]);
357 ut_asserteq(9, get_unaligned((u16 *)(ptr + 10)));
358 ut_asserteq(40000000, get_unaligned((u32 *)(ptr + 12)));
359 ut_asserteq(8, ptr[16]);
360 ut_asserteq(0, ptr[17]);
361 ut_asserteq(0, ptr[18]);
362 ut_asserteq(0, get_unaligned((u16 *)(ptr + 19)));
363 ut_asserteq_str("\\_SB.SPI0", (char *)ptr + 21);
364
365 free_context(&ctx);
366
367 return 0;
368}
Simon Glasse180c2b2020-07-28 19:41:12 -0600369DM_TEST(dm_test_acpi_spi, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass7e148f22020-07-07 13:11:50 -0600370
Simon Glass7e148f22020-07-07 13:11:50 -0600371/* Test emitting a length */
372static int dm_test_acpi_len(struct unit_test_state *uts)
373{
374 const int size = 0xc0000;
375 struct acpi_ctx *ctx;
376 u8 *ptr;
377 int i;
378
Simon Glass0e5a0a02020-07-07 13:11:56 -0600379 ut_assertok(acpi_test_alloc_context_size(&ctx, size));
Simon Glass7e148f22020-07-07 13:11:50 -0600380
381 ptr = acpigen_get_current(ctx);
382
383 /* Write a byte and a 3-byte length */
384 acpigen_write_len_f(ctx);
385 acpigen_emit_byte(ctx, 0x23);
386 acpigen_pop_len(ctx);
Simon Glass0e5a0a02020-07-07 13:11:56 -0600387 ut_asserteq(1 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-07-07 13:11:50 -0600388
389 /* Write 200 bytes so we need two length bytes */
390 ptr = ctx->current;
391 acpigen_write_len_f(ctx);
392 for (i = 0; i < 200; i++)
393 acpigen_emit_byte(ctx, 0x23);
394 acpigen_pop_len(ctx);
Simon Glass0e5a0a02020-07-07 13:11:56 -0600395 ut_asserteq(200 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-07-07 13:11:50 -0600396
397 /* Write 40KB so we need three length bytes */
398 ptr = ctx->current;
399 acpigen_write_len_f(ctx);
400 for (i = 0; i < 40000; i++)
401 acpigen_emit_byte(ctx, 0x23);
402 acpigen_pop_len(ctx);
Simon Glass0e5a0a02020-07-07 13:11:56 -0600403 ut_asserteq(40000 + 3, acpi_test_get_length(ptr));
Simon Glass7e148f22020-07-07 13:11:50 -0600404
405 free_context(&ctx);
406
407 return 0;
408}
409DM_TEST(dm_test_acpi_len, 0);
Simon Glass03967ce2020-07-07 13:11:51 -0600410
411/* Test writing a package */
412static int dm_test_acpi_package(struct unit_test_state *uts)
413{
414 struct acpi_ctx *ctx;
415 char *num_elements;
416 u8 *ptr;
417
418 ut_assertok(alloc_context(&ctx));
419
420 ptr = acpigen_get_current(ctx);
421
422 num_elements = acpigen_write_package(ctx, 3);
423 ut_asserteq_ptr(num_elements, ptr + 4);
424
425 /* For ease of testing, just emit a byte, not valid package contents */
426 acpigen_emit_byte(ctx, 0x23);
427 acpigen_pop_len(ctx);
428 ut_asserteq(PACKAGE_OP, ptr[0]);
Simon Glass0e5a0a02020-07-07 13:11:56 -0600429 ut_asserteq(5, acpi_test_get_length(ptr + 1));
Simon Glass03967ce2020-07-07 13:11:51 -0600430 ut_asserteq(3, ptr[4]);
431
432 free_context(&ctx);
433
434 return 0;
435}
436DM_TEST(dm_test_acpi_package, 0);
Simon Glass83b2bd52020-07-07 13:11:52 -0600437
438/* Test writing an integer */
439static int dm_test_acpi_integer(struct unit_test_state *uts)
440{
441 struct acpi_ctx *ctx;
442 u8 *ptr;
443
444 ut_assertok(alloc_context(&ctx));
445
446 ptr = acpigen_get_current(ctx);
447
448 acpigen_write_integer(ctx, 0);
449 acpigen_write_integer(ctx, 1);
450 acpigen_write_integer(ctx, TEST_INT8);
451 acpigen_write_integer(ctx, TEST_INT16);
452 acpigen_write_integer(ctx, TEST_INT32);
453 acpigen_write_integer(ctx, TEST_INT64);
454
455 ut_asserteq(6 + 1 + 2 + 4 + 8, acpigen_get_current(ctx) - ptr);
456
457 ut_asserteq(ZERO_OP, ptr[0]);
458
459 ut_asserteq(ONE_OP, ptr[1]);
460
461 ut_asserteq(BYTE_PREFIX, ptr[2]);
462 ut_asserteq(TEST_INT8, ptr[3]);
463
464 ut_asserteq(WORD_PREFIX, ptr[4]);
465 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 5)));
466
467 ut_asserteq(DWORD_PREFIX, ptr[7]);
468 ut_asserteq(TEST_INT32, get_unaligned((u32 *)(ptr + 8)));
469
470 ut_asserteq(QWORD_PREFIX, ptr[12]);
471 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 13)));
472
473 free_context(&ctx);
474
475 return 0;
476}
477DM_TEST(dm_test_acpi_integer, 0);
Simon Glass3df33bd2020-07-07 13:11:53 -0600478
479/* Test writing a string */
480static int dm_test_acpi_string(struct unit_test_state *uts)
481{
482 struct acpi_ctx *ctx;
483 u8 *ptr;
484
485 ut_assertok(alloc_context(&ctx));
486
487 ptr = acpigen_get_current(ctx);
488
489 acpigen_write_string(ctx, TEST_STRING);
490 acpigen_write_string(ctx, TEST_STRING2);
491
492 ut_asserteq(2 + sizeof(TEST_STRING) + sizeof(TEST_STRING2),
493 acpigen_get_current(ctx) - ptr);
494 ut_asserteq(STRING_PREFIX, ptr[0]);
495 ut_asserteq_str(TEST_STRING, (char *)ptr + 1);
496 ptr += 1 + sizeof(TEST_STRING);
497 ut_asserteq(STRING_PREFIX, ptr[0]);
498 ut_asserteq_str(TEST_STRING2, (char *)ptr + 1);
499
500 free_context(&ctx);
501
502 return 0;
503}
504DM_TEST(dm_test_acpi_string, 0);
Simon Glass7aed90d2020-07-07 13:11:54 -0600505
506/* Test writing a name */
507static int dm_test_acpi_name(struct unit_test_state *uts)
508{
509 struct acpi_ctx *ctx;
510 u8 *ptr;
511
512 ut_assertok(alloc_context(&ctx));
513
514 ptr = acpigen_get_current(ctx);
515
516 /*
517 * The names here are made up for testing the various cases. The
518 * grammar is in the ACPI spec 6.3 section 19.2.2
519 */
520 acpigen_write_name(ctx, "\\_SB");
521 acpigen_write_name(ctx, "\\_SB.I2C0");
522 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2");
523 acpigen_write_name(ctx, "\\_SB.I2C0.TPM2.LONG");
524 acpigen_write_name(ctx, "^^^^SPI0.FLAS");
525 acpigen_write_name(ctx, "NN");
526 acpigen_write_name(ctx, "^AB.CD.D.EFG");
527 acpigen_write_name(ctx, "^^^^");
528 acpigen_write_name(ctx, "\\");
529 acpigen_write_name(ctx, "\\ABCD");
530
531 ut_asserteq(107, acpigen_get_current(ctx) - ptr);
532 ut_asserteq(NAME_OP, ptr[0]);
533 ut_asserteq_strn("\\_SB_", (char *)ptr + 1);
534 ptr += 6;
535
536 ut_asserteq(NAME_OP, ptr[0]);
537 ut_asserteq('\\', ptr[1]);
538 ut_asserteq(DUAL_NAME_PREFIX, ptr[2]);
539 ut_asserteq_strn("_SB_I2C0", (char *)ptr + 3);
540 ptr += 11;
541
542 ut_asserteq(NAME_OP, ptr[0]);
543 ut_asserteq('\\', ptr[1]);
544 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
545 ut_asserteq(3, ptr[3]);
546 ut_asserteq_strn("_SB_I2C0TPM2", (char *)ptr + 4);
547 ptr += 16;
548
549 ut_asserteq(NAME_OP, ptr[0]);
550 ut_asserteq('\\', ptr[1]);
551 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
552 ut_asserteq(4, ptr[3]);
553 ut_asserteq_strn("_SB_I2C0TPM2LONG", (char *)ptr + 4);
554 ptr += 20;
555
556 ut_asserteq(NAME_OP, ptr[0]);
557 ut_asserteq('^', ptr[1]);
558 ut_asserteq('^', ptr[2]);
559 ut_asserteq('^', ptr[3]);
560 ut_asserteq('^', ptr[4]);
561 ut_asserteq(DUAL_NAME_PREFIX, ptr[5]);
562 ut_asserteq_strn("SPI0FLAS", (char *)ptr + 6);
563 ptr += 14;
564
565 ut_asserteq(NAME_OP, ptr[0]);
566 ut_asserteq_strn("NN__", (char *)ptr + 1);
567 ptr += 5;
568
569 ut_asserteq(NAME_OP, ptr[0]);
570 ut_asserteq('^', ptr[1]);
571 ut_asserteq(MULTI_NAME_PREFIX, ptr[2]);
572 ut_asserteq(4, ptr[3]);
573 ut_asserteq_strn("AB__CD__D___EFG_", (char *)ptr + 4);
574 ptr += 20;
575
576 ut_asserteq(NAME_OP, ptr[0]);
577 ut_asserteq('^', ptr[1]);
578 ut_asserteq('^', ptr[2]);
579 ut_asserteq('^', ptr[3]);
580 ut_asserteq('^', ptr[4]);
581 ut_asserteq(ZERO_OP, ptr[5]);
582 ptr += 6;
583
584 ut_asserteq(NAME_OP, ptr[0]);
585 ut_asserteq('\\', ptr[1]);
586 ut_asserteq(ZERO_OP, ptr[2]);
587 ptr += 3;
588
589 ut_asserteq(NAME_OP, ptr[0]);
590 ut_asserteq_strn("\\ABCD", (char *)ptr + 1);
591 ptr += 5;
592
593 free_context(&ctx);
594
595 return 0;
596}
597DM_TEST(dm_test_acpi_name, 0);
Simon Glass29df8452020-07-07 13:11:55 -0600598
599/* Test writing a UUID */
600static int dm_test_acpi_uuid(struct unit_test_state *uts)
601{
602 struct acpi_ctx *ctx;
603 u8 *ptr;
604
605 ut_assertok(alloc_context(&ctx));
606
607 ptr = acpigen_get_current(ctx);
608
609 ut_assertok(acpigen_write_uuid(ctx,
610 "dbb8e3e6-5886-4ba6-8795-1319f52a966b"));
611 ut_asserteq(23, acpigen_get_current(ctx) - ptr);
612 ut_asserteq(BUFFER_OP, ptr[0]);
Simon Glass0e5a0a02020-07-07 13:11:56 -0600613 ut_asserteq(22, acpi_test_get_length(ptr + 1));
Simon Glass29df8452020-07-07 13:11:55 -0600614 ut_asserteq(0xdbb8e3e6, get_unaligned((u32 *)(ptr + 7)));
615 ut_asserteq(0x5886, get_unaligned((u16 *)(ptr + 11)));
616 ut_asserteq(0x4ba6, get_unaligned((u16 *)(ptr + 13)));
617 ut_asserteq(0x9587, get_unaligned((u16 *)(ptr + 15)));
618 ut_asserteq(0x2af51913, get_unaligned((u32 *)(ptr + 17)));
619 ut_asserteq(0x6b96, get_unaligned((u16 *)(ptr + 21)));
620
621 /* Try a bad UUID */
622 ut_asserteq(-EINVAL,
623 acpigen_write_uuid(ctx,
624 "dbb8e3e6-5886-4ba6x8795-1319f52a966b"));
625
626 free_context(&ctx);
627
628 return 0;
629}
630DM_TEST(dm_test_acpi_uuid, 0);
Simon Glass9c70e7e2020-07-07 13:11:59 -0600631
632/* Test writing misc ACPI codes */
633static int dm_test_acpi_misc(struct unit_test_state *uts)
634{
635 struct acpi_ctx *ctx;
636 const int flags = 3;
637 const int nargs = 4;
638 u8 *ptr;
639
640 ut_assertok(alloc_context(&ctx));
641
642 ptr = acpigen_get_current(ctx);
643 acpigen_write_sleep(ctx, TEST_INT64);
644 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)(ptr + 3)));
645 ptr += 11;
646
647 acpigen_write_store(ctx);
648 ut_asserteq(STORE_OP, *ptr);
649 ptr++;
650
651 acpigen_write_debug_string(ctx, TEST_STRING);
652 ut_asserteq_str(TEST_STRING, (char *)ptr + 2);
653 ptr += 2 + sizeof(TEST_STRING);
654 ut_asserteq(EXT_OP_PREFIX, ptr[0]);
655 ut_asserteq(DEBUG_OP, ptr[1]);
656 ptr += 2;
657
658 acpigen_write_sta(ctx, flags);
659 ut_asserteq(METHOD_OP, ptr[0]);
660 ut_asserteq(11, acpi_test_get_length(ptr + 1));
661 ut_asserteq_strn("_STA", (char *)ptr + 4);
662 ut_asserteq(0, ptr[8]);
663 ut_asserteq(RETURN_OP, ptr[9]);
664 ut_asserteq(BYTE_PREFIX, ptr[10]);
665 ut_asserteq(flags, ptr[11]);
666 ptr += 12;
667
668 acpigen_write_sleep(ctx, TEST_INT16);
669 ut_asserteq(SLEEP_OP, ptr[1]);
670 ut_asserteq(TEST_INT16, get_unaligned((u16 *)(ptr + 3)));
671 ptr += 5;
672
673 acpigen_write_method_serialized(ctx, "FRED", nargs);
674 ut_asserteq(METHOD_OP, ptr[0]);
675 ut_asserteq_strn("FRED", (char *)ptr + 4);
676 ut_asserteq(1 << 3 | nargs, ptr[8]);
677 ut_asserteq(1, ctx->ltop); /* method is unfinished */
678
679 ptr += 9;
680 acpigen_write_or(ctx, LOCAL0_OP, LOCAL1_OP, LOCAL2_OP);
681 acpigen_write_and(ctx, LOCAL3_OP, LOCAL4_OP, LOCAL5_OP);
682 acpigen_write_not(ctx, LOCAL6_OP, LOCAL7_OP);
683 ut_asserteq(OR_OP, ptr[0]);
684 ut_asserteq(LOCAL0_OP, ptr[1]);
685 ut_asserteq(LOCAL1_OP, ptr[2]);
686 ut_asserteq(LOCAL2_OP, ptr[3]);
687
688 ptr += 4;
689 ut_asserteq(AND_OP, ptr[0]);
690 ut_asserteq(LOCAL3_OP, ptr[1]);
691 ut_asserteq(LOCAL4_OP, ptr[2]);
692 ut_asserteq(LOCAL5_OP, ptr[3]);
693
694 ptr += 4;
695 ut_asserteq(NOT_OP, ptr[0]);
696 ut_asserteq(LOCAL6_OP, ptr[1]);
697 ut_asserteq(LOCAL7_OP, ptr[2]);
698 ptr += 3;
699 ut_asserteq_ptr(ptr, ctx->current);
700
701 free_context(&ctx);
702
703 return 0;
704}
705DM_TEST(dm_test_acpi_misc, 0);
Simon Glassf9189d52020-07-07 13:12:00 -0600706
707/* Test writing an ACPI power resource */
708static int dm_test_acpi_power_res(struct unit_test_state *uts)
709{
710 const char *const states[] = { "_PR0", "_PR3" };
711 const char *name = "PRIC";
712 const int level = 3;
713 const int order = 2;
714 struct acpi_ctx *ctx;
715 u8 *ptr;
716
717 ut_assertok(alloc_context(&ctx));
718
719 ptr = acpigen_get_current(ctx);
720
721 /* PowerResource (PRIC, 0, 0) */
722 acpigen_write_power_res(ctx, name, level, order, states,
723 ARRAY_SIZE(states));
724 ut_asserteq(0x28, acpigen_get_current(ctx) - ptr);
725 ut_asserteq(NAME_OP, ptr[0]);
726 ut_asserteq_strn(states[0], (char *)ptr + 1);
727 ut_asserteq(8, acpi_test_get_length(ptr + 6));
728 ut_asserteq_strn(name, (char *)ptr + 0xa);
729
730 ut_asserteq_strn(states[1], (char *)ptr + 0xf);
731 ut_asserteq(8, acpi_test_get_length(ptr + 0x14));
732 ut_asserteq_strn(name, (char *)ptr + 0x18);
733
734 ut_asserteq(POWER_RES_OP, ptr[0x1d]);
735 ut_asserteq_strn(name, (char *)ptr + 0x21);
736 ut_asserteq(level, ptr[0x25]);
737 ut_asserteq(order, get_unaligned((u16 *)(ptr + 0x26)));
738
739 /* The length is not set - caller must use acpigen_pop_len() */
740 ut_asserteq(1, ctx->ltop);
741
742 free_context(&ctx);
743
744 return 0;
745}
746DM_TEST(dm_test_acpi_power_res, 0);
Simon Glassf8054dd2020-07-07 13:12:01 -0600747
748/* Test writing ACPI code to toggle a GPIO */
749static int dm_test_acpi_gpio_toggle(struct unit_test_state *uts)
750{
751 const uint addr = 0x80012;
752 const int txbit = BIT(2);
753 struct gpio_desc desc;
754 struct acpi_gpio gpio;
755 struct acpi_ctx *ctx;
756 struct udevice *dev;
757 u8 *ptr;
758
759 ut_assertok(alloc_context(&ctx));
760
761 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
762 ut_asserteq_str("a-test", dev->name);
763 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &desc, 0));
764 ut_assertok(gpio_get_acpi(&desc, &gpio));
765
766 /* Spot-check the results - see sb_gpio_get_acpi() */
767 ptr = acpigen_get_current(ctx);
768 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
769 &gpio, true);
770 acpigen_set_enable_tx_gpio(ctx, txbit, "\\_SB.GPC0", "\\_SB.SPC0",
771 &gpio, false);
772
773 /* Since this GPIO is active low, we expect it to be cleared here */
774 ut_asserteq(STORE_OP, *ptr);
775 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 3);
776 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0xc)));
777 ut_asserteq(LOCAL5_OP, ptr[0x10]);
778
779 ut_asserteq(STORE_OP, ptr[0x11]);
780 ut_asserteq(BYTE_PREFIX, ptr[0x12]);
781 ut_asserteq(txbit, ptr[0x13]);
782 ut_asserteq(LOCAL0_OP, ptr[0x14]);
783
784 ut_asserteq(NOT_OP, ptr[0x15]);
785 ut_asserteq(LOCAL0_OP, ptr[0x16]);
786 ut_asserteq(LOCAL6_OP, ptr[0x17]);
787 ut_asserteq(AND_OP, ptr[0x18]);
788 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x1e);
789 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x27)));
790 ut_asserteq(LOCAL5_OP, ptr[0x2b]);
791
792 /* Now the second one, which should be set */
793 ut_asserteq_strn("_SB_GPC0", (char *)ptr + 0x2f);
794 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x38)));
795 ut_asserteq(LOCAL5_OP, ptr[0x3c]);
796
797 ut_asserteq(STORE_OP, ptr[0x3d]);
798
799 ut_asserteq(OR_OP, ptr[0x41]);
800 ut_asserteq(LOCAL0_OP, ptr[0x43]);
801 ut_asserteq_strn("_SB_SPC0", (char *)ptr + 0x47);
802 ut_asserteq(addr + desc.offset, get_unaligned((u32 *)(ptr + 0x50)));
803 ut_asserteq(LOCAL5_OP, ptr[0x54]);
804 ut_asserteq(0x55, acpigen_get_current(ctx) - ptr);
805
806 free_context(&ctx);
807
808 return 0;
809}
Simon Glasse180c2b2020-07-28 19:41:12 -0600810DM_TEST(dm_test_acpi_gpio_toggle, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glass740630b2020-07-07 13:12:02 -0600811
812/* Test writing ACPI code to output power-sequence info */
813static int dm_test_acpi_power_seq(struct unit_test_state *uts)
814{
815 struct gpio_desc reset, enable, stop;
816 const uint addr = 0xc00dc, addr_act_low = 0x80012;
817 const int txbit = BIT(2);
818 struct acpi_ctx *ctx;
819 struct udevice *dev;
820 u8 *ptr;
821
822 ut_assertok(acpi_test_alloc_context_size(&ctx, 400));
823
824 ut_assertok(uclass_get_device(UCLASS_TEST_FDT, 0, &dev));
825 ut_asserteq_str("a-test", dev->name);
826 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 0, &reset, 0));
827 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 1, &enable, 0));
828 ut_assertok(gpio_request_by_name(dev, "test2-gpios", 2, &stop, 0));
829 ptr = acpigen_get_current(ctx);
830
831 ut_assertok(acpi_device_add_power_res(ctx, txbit, "\\_SB.GPC0",
832 "\\_SB.SPC0", &reset, 2, 3,
833 &enable, 4, 5, &stop, 6, 7));
834 ut_asserteq(0x186, acpigen_get_current(ctx) - ptr);
835 ut_asserteq_strn("PRIC", (char *)ptr + 0x18);
836
837 /* First the 'ON' sequence - spot check */
838 ut_asserteq_strn("_ON_", (char *)ptr + 0x38);
839
840 /* reset set */
841 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x49)));
842 ut_asserteq(OR_OP, ptr[0x52]);
843
844 /* enable set */
845 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x72)));
846 ut_asserteq(OR_OP, ptr[0x7b]);
847
848 /* reset clear */
849 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x9f)));
850 ut_asserteq(NOT_OP, ptr[0xa8]);
851
852 /* stop set (disable, active low) */
853 ut_asserteq(addr_act_low + stop.offset,
854 get_unaligned((u32 *)(ptr + 0xcf)));
855 ut_asserteq(OR_OP, ptr[0xd8]);
856
857 /* Now the 'OFF' sequence */
858 ut_asserteq_strn("_OFF", (char *)ptr + 0xf4);
859
860 /* stop clear (enable, active low) */
861 ut_asserteq(addr_act_low + stop.offset,
862 get_unaligned((u32 *)(ptr + 0x105)));
863 ut_asserteq(NOT_OP, ptr[0x10e]);
864
865 /* reset clear */
866 ut_asserteq(addr + reset.offset, get_unaligned((u32 *)(ptr + 0x135)));
867 ut_asserteq(OR_OP, ptr[0x13e]);
868
869 /* enable clear */
870 ut_asserteq(addr + enable.offset, get_unaligned((u32 *)(ptr + 0x162)));
871 ut_asserteq(NOT_OP, ptr[0x16b]);
872
873 free_context(&ctx);
874
875 return 0;
876}
Simon Glasse180c2b2020-07-28 19:41:12 -0600877DM_TEST(dm_test_acpi_power_seq, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassfea96512020-07-07 21:32:09 -0600878
879/* Test writing values */
880static int dm_test_acpi_write_values(struct unit_test_state *uts)
881{
882 struct acpi_ctx *ctx;
883 u8 *ptr;
884
885 ut_assertok(alloc_context(&ctx));
886 ptr = acpigen_get_current(ctx);
887
888 acpigen_write_zero(ctx);
889 acpigen_write_one(ctx);
890 acpigen_write_byte(ctx, TEST_INT8);
891 acpigen_write_word(ctx, TEST_INT16);
892 acpigen_write_dword(ctx, TEST_INT32);
893 acpigen_write_qword(ctx, TEST_INT64);
894
895 ut_asserteq(ZERO_OP, *ptr++);
896
897 ut_asserteq(ONE_OP, *ptr++);
898
899 ut_asserteq(BYTE_PREFIX, *ptr++);
900 ut_asserteq(TEST_INT8, *ptr++);
901
902 ut_asserteq(WORD_PREFIX, *ptr++);
903 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
904 ptr += 2;
905
906 ut_asserteq(DWORD_PREFIX, *ptr++);
907 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
908 ptr += 4;
909
910 ut_asserteq(QWORD_PREFIX, *ptr++);
911 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
912 ptr += 8;
913
914 ut_asserteq_ptr(ptr, ctx->current);
915
916 free_context(&ctx);
917
918 return 0;
919}
920DM_TEST(dm_test_acpi_write_values, 0);
Simon Glass82659cc2020-07-07 21:32:10 -0600921
922/* Test writing a scope */
923static int dm_test_acpi_scope(struct unit_test_state *uts)
924{
925 char buf[ACPI_PATH_MAX];
926 struct acpi_ctx *ctx;
927 struct udevice *dev;
928 u8 *ptr;
929
930 ut_assertok(alloc_context(&ctx));
931 ptr = acpigen_get_current(ctx);
932
933 ut_assertok(uclass_first_device_err(UCLASS_TEST_ACPI, &dev));
934 ut_assertok(acpi_device_path(dev, buf, sizeof(buf)));
935 acpigen_write_scope(ctx, buf);
936 acpigen_pop_len(ctx);
937
938 ut_asserteq(SCOPE_OP, *ptr++);
939 ut_asserteq(13, acpi_test_get_length(ptr));
940 ptr += 3;
941 ut_asserteq(ROOT_PREFIX, *ptr++);
942 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
943 ut_asserteq_strn("_SB_" ACPI_TEST_DEV_NAME, (char *)ptr);
944 ptr += 8;
945 ut_asserteq_ptr(ptr, ctx->current);
946
947 free_context(&ctx);
948
949 return 0;
950}
Simon Glasse180c2b2020-07-28 19:41:12 -0600951DM_TEST(dm_test_acpi_scope, UT_TESTF_SCAN_PDATA | UT_TESTF_SCAN_FDT);
Simon Glassd7d631d2020-07-07 21:32:11 -0600952
953/* Test writing a resource template */
954static int dm_test_acpi_resource_template(struct unit_test_state *uts)
955{
956 struct acpi_gen_regaddr addr;
957 struct acpi_ctx *ctx;
958 u8 *ptr;
959
960 ut_assertok(alloc_context(&ctx));
961 ptr = acpigen_get_current(ctx);
962
963 addr.space_id = ACPI_ADDRESS_SPACE_EC;
964 addr.bit_width = 32;
965 addr.bit_offset = 8;
966 addr.access_size = ACPI_ACCESS_SIZE_DWORD_ACCESS;
967 addr.addrl = TEST_INT64 & 0xffffffff;
968 addr.addrh = TEST_INT64 >> 32;
969 acpigen_write_register_resource(ctx, &addr);
970
971 ut_asserteq(BUFFER_OP, *ptr++);
972 ut_asserteq(0x17, acpi_test_get_length(ptr));
973 ptr += 3;
974 ut_asserteq(WORD_PREFIX, *ptr++);
975 ut_asserteq(0x11, get_unaligned((u16 *)ptr));
976 ptr += 2;
977 ut_asserteq(ACPI_DESCRIPTOR_REGISTER, *ptr++);
978 ut_asserteq(0xc, *ptr++);
979 ut_asserteq(0, *ptr++);
980 ut_asserteq(ACPI_ADDRESS_SPACE_EC, *ptr++);
981 ut_asserteq(32, *ptr++);
982 ut_asserteq(8, *ptr++);
983 ut_asserteq(ACPI_ACCESS_SIZE_DWORD_ACCESS, *ptr++);
984 ut_asserteq(TEST_INT64 & 0xffffffff, get_unaligned((u32 *)ptr));
985 ptr += 4;
986 ut_asserteq(TEST_INT64 >> 32, get_unaligned((u32 *)ptr));
987 ptr += 4;
988 ut_asserteq(ACPI_END_TAG, *ptr++);
989 ut_asserteq(0x00, *ptr++);
990 ut_asserteq_ptr(ptr, ctx->current);
991
992 free_context(&ctx);
993
994 return 0;
995}
996DM_TEST(dm_test_acpi_resource_template, 0);
Simon Glass91c2f9c2020-07-07 21:32:14 -0600997
998/* Test writing a device */
999static int dm_test_acpi_device(struct unit_test_state *uts)
1000{
1001 struct acpi_ctx *ctx;
1002 u8 *ptr;
1003
1004 ut_assertok(alloc_context(&ctx));
1005 ptr = acpigen_get_current(ctx);
1006
1007 acpigen_write_device(ctx, "\\_SB." ACPI_TEST_DEV_NAME);
1008 acpigen_pop_len(ctx);
1009
1010 ut_asserteq(EXT_OP_PREFIX, *ptr++);
1011 ut_asserteq(DEVICE_OP, *ptr++);
1012 ut_asserteq(0xd, acpi_test_get_length(ptr));
1013 ptr += 3;
1014 ut_asserteq(ROOT_PREFIX, *ptr++);
1015 ut_asserteq(DUAL_NAME_PREFIX, *ptr++);
1016 ptr += 8;
1017 ut_asserteq_ptr(ptr, ctx->current);
1018
1019 free_context(&ctx);
1020
1021 return 0;
1022}
1023DM_TEST(dm_test_acpi_device, 0);
Simon Glassbb6772c2020-07-07 21:32:15 -06001024
1025/* Test writing named values */
1026static int dm_test_acpi_write_name(struct unit_test_state *uts)
1027{
1028 const char *name = "\\_SB." ACPI_TEST_DEV_NAME;
1029 struct acpi_ctx *ctx;
1030 u8 *ptr;
1031
1032 ut_assertok(alloc_context(&ctx));
1033 ptr = acpigen_get_current(ctx);
1034
1035 acpigen_write_name_zero(ctx, name);
1036 acpigen_write_name_one(ctx, name);
1037 acpigen_write_name_byte(ctx, name, TEST_INT8);
1038 acpigen_write_name_word(ctx, name, TEST_INT16);
1039 acpigen_write_name_dword(ctx, name, TEST_INT32);
1040 acpigen_write_name_qword(ctx, name, TEST_INT64);
1041 acpigen_write_name_integer(ctx, name, TEST_INT64 + 1);
1042 acpigen_write_name_string(ctx, name, "baldrick");
1043 acpigen_write_name_string(ctx, name, NULL);
1044
1045 ut_asserteq(NAME_OP, *ptr++);
1046 ut_asserteq_strn("\\._SB_ABCD", (char *)ptr);
1047 ptr += 10;
1048 ut_asserteq(ZERO_OP, *ptr++);
1049
1050 ut_asserteq(NAME_OP, *ptr++);
1051 ptr += 10;
1052 ut_asserteq(ONE_OP, *ptr++);
1053
1054 ut_asserteq(NAME_OP, *ptr++);
1055 ptr += 10;
1056 ut_asserteq(BYTE_PREFIX, *ptr++);
1057 ut_asserteq(TEST_INT8, *ptr++);
1058
1059 ut_asserteq(NAME_OP, *ptr++);
1060 ptr += 10;
1061 ut_asserteq(WORD_PREFIX, *ptr++);
1062 ut_asserteq(TEST_INT16, get_unaligned((u16 *)ptr));
1063 ptr += 2;
1064
1065 ut_asserteq(NAME_OP, *ptr++);
1066 ptr += 10;
1067 ut_asserteq(DWORD_PREFIX, *ptr++);
1068 ut_asserteq(TEST_INT32, get_unaligned((u32 *)ptr));
1069 ptr += 4;
1070
1071 ut_asserteq(NAME_OP, *ptr++);
1072 ptr += 10;
1073 ut_asserteq(QWORD_PREFIX, *ptr++);
1074 ut_asserteq_64(TEST_INT64, get_unaligned((u64 *)ptr));
1075 ptr += 8;
1076
1077 ut_asserteq(NAME_OP, *ptr++);
1078 ptr += 10;
1079 ut_asserteq(QWORD_PREFIX, *ptr++);
1080 ut_asserteq_64(TEST_INT64 + 1, get_unaligned((u64 *)ptr));
1081 ptr += 8;
1082
1083 ut_asserteq(NAME_OP, *ptr++);
1084 ptr += 10;
1085 ut_asserteq(STRING_PREFIX, *ptr++);
1086 ut_asserteq_str("baldrick", (char *)ptr)
1087 ptr += 9;
1088
1089 ut_asserteq(NAME_OP, *ptr++);
1090 ptr += 10;
1091 ut_asserteq(STRING_PREFIX, *ptr++);
1092 ut_asserteq('\0', *ptr++);
1093
1094 ut_asserteq_ptr(ptr, ctx->current);
1095
1096 free_context(&ctx);
1097
1098 return 0;
1099}
1100DM_TEST(dm_test_acpi_write_name, 0);
Simon Glasse0a896b2020-09-22 12:44:56 -06001101
1102/* Test emitting a _PRW component */
1103static int dm_test_acpi_write_prw(struct unit_test_state *uts)
1104{
1105 struct acpi_ctx *ctx;
1106 u8 *ptr;
1107
1108 ut_assertok(alloc_context(&ctx));
1109
1110 ptr = acpigen_get_current(ctx);
1111 acpigen_write_prw(ctx, 5, 3);
1112 ut_asserteq(NAME_OP, *ptr++);
1113
1114 ut_asserteq_strn("_PRW", (char *)ptr);
1115 ptr += 4;
1116 ut_asserteq(PACKAGE_OP, *ptr++);
1117 ut_asserteq(8, acpi_test_get_length(ptr));
1118 ptr += 3;
1119 ut_asserteq(2, *ptr++);
1120 ut_asserteq(BYTE_PREFIX, *ptr++);
1121 ut_asserteq(5, *ptr++);
1122 ut_asserteq(BYTE_PREFIX, *ptr++);
1123 ut_asserteq(3, *ptr++);
1124 ut_asserteq_ptr(ptr, ctx->current);
1125
1126 free_context(&ctx);
1127
1128 return 0;
1129}
1130DM_TEST(dm_test_acpi_write_prw, 0);
Simon Glassda7cff32020-09-22 12:44:57 -06001131
1132/* Test emitting writing conditionals */
1133static int dm_test_acpi_write_cond(struct unit_test_state *uts)
1134{
1135 struct acpi_ctx *ctx;
1136 u8 *ptr;
1137
1138 ut_assertok(alloc_context(&ctx));
1139
1140 ptr = acpigen_get_current(ctx);
1141 acpigen_write_if(ctx);
1142 acpigen_pop_len(ctx);
1143 ut_asserteq(IF_OP, *ptr++);
1144 ut_asserteq(3, acpi_test_get_length(ptr));
1145 ptr += 3;
1146
1147 acpigen_write_else(ctx);
1148 acpigen_pop_len(ctx);
1149 ut_asserteq(ELSE_OP, *ptr++);
1150 ut_asserteq(3, acpi_test_get_length(ptr));
1151 ptr += 3;
1152
1153 acpigen_write_if_lequal_op_int(ctx, LOCAL1_OP, 5);
1154 acpigen_pop_len(ctx);
1155 ut_asserteq(IF_OP, *ptr++);
1156 ut_asserteq(7, acpi_test_get_length(ptr));
1157 ptr += 3;
1158 ut_asserteq(LEQUAL_OP, *ptr++);
1159 ut_asserteq(LOCAL1_OP, *ptr++);
1160 ut_asserteq(BYTE_PREFIX, *ptr++);
1161 ut_asserteq(5, *ptr++);
1162
1163 ut_asserteq_ptr(ptr, ctx->current);
1164
1165 free_context(&ctx);
1166
1167 return 0;
1168}
1169DM_TEST(dm_test_acpi_write_cond, 0);
1170
1171/* Test emitting writing return values and ToBuffer/ToInteger */
1172static int dm_test_acpi_write_return(struct unit_test_state *uts)
1173{
1174 int len = sizeof(TEST_STRING);
1175 struct acpi_ctx *ctx;
1176 u8 *ptr;
1177
1178 ut_assertok(alloc_context(&ctx));
1179
1180 ptr = acpigen_get_current(ctx);
1181 acpigen_write_to_buffer(ctx, ARG0_OP, LOCAL0_OP);
1182 ut_asserteq(TO_BUFFER_OP, *ptr++);
1183 ut_asserteq(ARG0_OP, *ptr++);
1184 ut_asserteq(LOCAL0_OP, *ptr++);
1185
1186 acpigen_write_to_integer(ctx, ARG0_OP, LOCAL0_OP);
1187 ut_asserteq(TO_INTEGER_OP, *ptr++);
1188 ut_asserteq(ARG0_OP, *ptr++);
1189 ut_asserteq(LOCAL0_OP, *ptr++);
1190
1191 acpigen_write_return_byte_buffer(ctx, (u8 *)TEST_STRING, len);
1192 ut_asserteq(RETURN_OP, *ptr++);
1193 ut_asserteq(BUFFER_OP, *ptr++);
1194 ut_asserteq(5 + len, acpi_test_get_length(ptr));
1195 ptr += 3;
1196 ut_asserteq(BYTE_PREFIX, *ptr++);
1197 ut_asserteq(len, *ptr++);
1198 ut_asserteq_mem(TEST_STRING, ptr, len);
1199 ptr += len;
1200
1201 acpigen_write_return_singleton_buffer(ctx, 123);
1202 len = 1;
1203 ut_asserteq(RETURN_OP, *ptr++);
1204 ut_asserteq(BUFFER_OP, *ptr++);
1205 ut_asserteq(4 + len, acpi_test_get_length(ptr));
1206 ptr += 3;
1207 ut_asserteq(ONE_OP, *ptr++);
1208 ut_asserteq(123, *ptr++);
1209
1210 acpigen_write_return_byte(ctx, 43);
1211 ut_asserteq(RETURN_OP, *ptr++);
1212 ut_asserteq(BYTE_PREFIX, *ptr++);
1213 ut_asserteq(43, *ptr++);
1214
1215 ut_asserteq_ptr(ptr, ctx->current);
1216
1217 free_context(&ctx);
1218
1219 return 0;
1220}
1221DM_TEST(dm_test_acpi_write_return, 0);
Simon Glass88490e12020-09-22 12:44:58 -06001222
1223/* Test emitting a DSM for an I2C HID */
1224static int dm_test_acpi_write_i2c_dsm(struct unit_test_state *uts)
1225{
1226 char uuid_str[UUID_STR_LEN + 1];
1227 const int reg_offset = 0x20;
1228 struct acpi_ctx *ctx;
1229 u8 *ptr;
1230
1231 ut_assertok(alloc_context(&ctx));
1232
1233 ptr = acpigen_get_current(ctx);
1234 ut_assertok(acpi_device_write_dsm_i2c_hid(ctx, reg_offset));
1235
1236 /* acpigen_write_dsm_start() */
1237 ut_asserteq(METHOD_OP, *ptr++);
1238 ut_asserteq(0x78, acpi_test_get_length(ptr));
1239 ptr += 3;
1240 ut_asserteq_strn("_DSM", (char *)ptr);
1241 ptr += 4;
1242 ut_asserteq(ACPI_METHOD_SERIALIZED_MASK | 4, *ptr++);
1243
1244 ut_asserteq(TO_BUFFER_OP, *ptr++);
1245 ut_asserteq(ARG0_OP, *ptr++);
1246 ut_asserteq(LOCAL0_OP, *ptr++);
1247
1248 /* acpigen_write_dsm_uuid_start() */
1249 ut_asserteq(IF_OP, *ptr++);
1250 ut_asserteq(0x65, acpi_test_get_length(ptr));
1251 ptr += 3;
1252 ut_asserteq(LEQUAL_OP, *ptr++);
1253 ut_asserteq(LOCAL0_OP, *ptr++);
1254
1255 ut_asserteq(BUFFER_OP, *ptr++);
1256 ut_asserteq(UUID_BIN_LEN + 6, acpi_test_get_length(ptr));
1257 ptr += 3;
1258 ut_asserteq(WORD_PREFIX, *ptr++);
1259 ut_asserteq(UUID_BIN_LEN, get_unaligned((u16 *)ptr));
1260 ptr += 2;
1261 uuid_bin_to_str(ptr, uuid_str, UUID_STR_FORMAT_GUID);
1262 ut_asserteq_str(ACPI_DSM_I2C_HID_UUID, uuid_str);
1263 ptr += UUID_BIN_LEN;
1264
1265 ut_asserteq(TO_INTEGER_OP, *ptr++);
1266 ut_asserteq(ARG2_OP, *ptr++);
1267 ut_asserteq(LOCAL1_OP, *ptr++);
1268
1269 /* acpigen_write_dsm_uuid_start_cond() */
1270 ut_asserteq(IF_OP, *ptr++);
1271 ut_asserteq(0x34, acpi_test_get_length(ptr));
1272 ptr += 3;
1273 ut_asserteq(LEQUAL_OP, *ptr++);
1274 ut_asserteq(LOCAL1_OP, *ptr++);
1275 ut_asserteq(ZERO_OP, *ptr++);
1276
1277 /*
1278 * See code from acpi_device_write_dsm_i2c_hid(). We don't check every
1279 * piece
1280 */
1281 ut_asserteq(TO_INTEGER_OP, *ptr++);
1282 ut_asserteq(ARG1_OP, *ptr++);
1283 ut_asserteq(LOCAL2_OP, *ptr++);
1284
1285 ut_asserteq(IF_OP, *ptr++);
1286 ut_asserteq(0xd, acpi_test_get_length(ptr));
1287 ptr += 3;
1288 ut_asserteq(LEQUAL_OP, *ptr++);
1289 ut_asserteq(LOCAL2_OP, *ptr++);
1290 ut_asserteq(ZERO_OP, *ptr++); /* function 0 */
1291
1292 ut_asserteq(RETURN_OP, *ptr++);
1293 ut_asserteq(BUFFER_OP, *ptr++);
1294 ptr += 5;
1295
1296 ut_asserteq(ELSE_OP, *ptr++);
1297 ptr += 3;
1298
1299 ut_asserteq(IF_OP, *ptr++);
1300 ut_asserteq(0xd, acpi_test_get_length(ptr));
1301 ptr += 3;
1302 ut_asserteq(LEQUAL_OP, *ptr++);
1303 ut_asserteq(LOCAL2_OP, *ptr++);
1304 ut_asserteq(ONE_OP, *ptr++);
1305
1306 ut_asserteq(RETURN_OP, *ptr++);
1307 ut_asserteq(BUFFER_OP, *ptr++);
1308 ptr += 5;
1309
1310 ut_asserteq(ELSE_OP, *ptr++);
1311 ptr += 3;
1312
1313 ut_asserteq(RETURN_OP, *ptr++);
1314 ut_asserteq(BUFFER_OP, *ptr++);
1315 ptr += 5;
1316
1317 /* acpigen_write_dsm_uuid_start_cond() */
1318 ut_asserteq(IF_OP, *ptr++);
1319 ut_asserteq(9, acpi_test_get_length(ptr));
1320 ptr += 3;
1321 ut_asserteq(LEQUAL_OP, *ptr++);
1322 ut_asserteq(LOCAL1_OP, *ptr++);
1323 ut_asserteq(ONE_OP, *ptr++); /* function 1 */
1324
1325 ut_asserteq(RETURN_OP, *ptr++);
1326 ut_asserteq(BYTE_PREFIX, *ptr++);
1327 ut_asserteq(reg_offset, *ptr++);
1328
1329 /* acpigen_write_dsm_uuid_end() */
1330 ut_asserteq(RETURN_OP, *ptr++);
1331 ut_asserteq(BUFFER_OP, *ptr++);
1332 ptr += 5;
1333
1334 /* acpigen_write_dsm_end */
1335 ut_asserteq(RETURN_OP, *ptr++);
1336 ut_asserteq(BUFFER_OP, *ptr++);
1337 ptr += 5;
1338
1339 ut_asserteq_ptr(ptr, ctx->current);
1340
1341 free_context(&ctx);
1342
1343 return 0;
1344}
1345DM_TEST(dm_test_acpi_write_i2c_dsm, 0);