1 // SPDX-License-Identifier: GPL-2.0
3 * Generation of ACPI (Advanced Configuration and Power Interface) tables
5 * Copyright 2019 Google LLC
6 * Mostly taken from coreboot
9 #define LOG_CATEGORY LOGC_ACPI
15 #include <acpi/acpigen.h>
16 #include <acpi/acpi_device.h>
17 #include <acpi/acpi_table.h>
20 u8 *acpigen_get_current(struct acpi_ctx *ctx)
25 void acpigen_emit_byte(struct acpi_ctx *ctx, uint data)
27 *(u8 *)ctx->current++ = data;
30 void acpigen_emit_word(struct acpi_ctx *ctx, uint data)
32 acpigen_emit_byte(ctx, data & 0xff);
33 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
36 void acpigen_emit_dword(struct acpi_ctx *ctx, uint data)
38 /* Output the value in little-endian format */
39 acpigen_emit_byte(ctx, data & 0xff);
40 acpigen_emit_byte(ctx, (data >> 8) & 0xff);
41 acpigen_emit_byte(ctx, (data >> 16) & 0xff);
42 acpigen_emit_byte(ctx, (data >> 24) & 0xff);
46 * Maximum length for an ACPI object generated by this code,
48 * If you need to change this, change acpigen_write_len_f(ctx) and
49 * acpigen_pop_len(ctx)
51 #define ACPIGEN_MAXLEN 0xfffff
53 void acpigen_write_len_f(struct acpi_ctx *ctx)
55 assert(ctx->ltop < (ACPIGEN_LENSTACK_SIZE - 1));
56 ctx->len_stack[ctx->ltop++] = ctx->current;
57 acpigen_emit_byte(ctx, 0);
58 acpigen_emit_byte(ctx, 0);
59 acpigen_emit_byte(ctx, 0);
62 void acpigen_pop_len(struct acpi_ctx *ctx)
67 assert(ctx->ltop > 0);
68 p = ctx->len_stack[--ctx->ltop];
69 len = ctx->current - (void *)p;
70 assert(len <= ACPIGEN_MAXLEN);
71 /* generate store length for 0xfffff max */
72 p[0] = ACPI_PKG_LEN_3_BYTES | (len & 0xf);
73 p[1] = len >> 4 & 0xff;
74 p[2] = len >> 12 & 0xff;
77 void acpigen_emit_ext_op(struct acpi_ctx *ctx, uint op)
79 acpigen_emit_byte(ctx, EXT_OP_PREFIX);
80 acpigen_emit_byte(ctx, op);
83 char *acpigen_write_package(struct acpi_ctx *ctx, int nr_el)
87 acpigen_emit_byte(ctx, PACKAGE_OP);
88 acpigen_write_len_f(ctx);
90 acpigen_emit_byte(ctx, nr_el);
95 void acpigen_write_byte(struct acpi_ctx *ctx, unsigned int data)
97 acpigen_emit_byte(ctx, BYTE_PREFIX);
98 acpigen_emit_byte(ctx, data & 0xff);
101 void acpigen_write_word(struct acpi_ctx *ctx, unsigned int data)
103 acpigen_emit_byte(ctx, WORD_PREFIX);
104 acpigen_emit_word(ctx, data);
107 void acpigen_write_dword(struct acpi_ctx *ctx, unsigned int data)
109 acpigen_emit_byte(ctx, DWORD_PREFIX);
110 acpigen_emit_dword(ctx, data);
113 void acpigen_write_qword(struct acpi_ctx *ctx, u64 data)
115 acpigen_emit_byte(ctx, QWORD_PREFIX);
116 acpigen_emit_dword(ctx, data & 0xffffffff);
117 acpigen_emit_dword(ctx, (data >> 32) & 0xffffffff);
120 void acpigen_write_zero(struct acpi_ctx *ctx)
122 acpigen_emit_byte(ctx, ZERO_OP);
125 void acpigen_write_one(struct acpi_ctx *ctx)
127 acpigen_emit_byte(ctx, ONE_OP);
130 void acpigen_write_integer(struct acpi_ctx *ctx, u64 data)
133 acpigen_write_zero(ctx);
135 acpigen_write_one(ctx);
136 else if (data <= 0xff)
137 acpigen_write_byte(ctx, (unsigned char)data);
138 else if (data <= 0xffff)
139 acpigen_write_word(ctx, (unsigned int)data);
140 else if (data <= 0xffffffff)
141 acpigen_write_dword(ctx, (unsigned int)data);
143 acpigen_write_qword(ctx, data);
146 void acpigen_write_name_zero(struct acpi_ctx *ctx, const char *name)
148 acpigen_write_name(ctx, name);
149 acpigen_write_zero(ctx);
152 void acpigen_write_name_one(struct acpi_ctx *ctx, const char *name)
154 acpigen_write_name(ctx, name);
155 acpigen_write_one(ctx);
158 void acpigen_write_name_byte(struct acpi_ctx *ctx, const char *name, uint val)
160 acpigen_write_name(ctx, name);
161 acpigen_write_byte(ctx, val);
164 void acpigen_write_name_word(struct acpi_ctx *ctx, const char *name, uint val)
166 acpigen_write_name(ctx, name);
167 acpigen_write_word(ctx, val);
170 void acpigen_write_name_dword(struct acpi_ctx *ctx, const char *name, uint val)
172 acpigen_write_name(ctx, name);
173 acpigen_write_dword(ctx, val);
176 void acpigen_write_name_qword(struct acpi_ctx *ctx, const char *name, u64 val)
178 acpigen_write_name(ctx, name);
179 acpigen_write_qword(ctx, val);
182 void acpigen_write_name_integer(struct acpi_ctx *ctx, const char *name, u64 val)
184 acpigen_write_name(ctx, name);
185 acpigen_write_integer(ctx, val);
188 void acpigen_write_name_string(struct acpi_ctx *ctx, const char *name,
191 acpigen_write_name(ctx, name);
192 acpigen_write_string(ctx, string);
195 void acpigen_emit_stream(struct acpi_ctx *ctx, const char *data, int size)
199 for (i = 0; i < size; i++)
200 acpigen_emit_byte(ctx, data[i]);
203 void acpigen_emit_string(struct acpi_ctx *ctx, const char *str)
205 acpigen_emit_stream(ctx, str, str ? strlen(str) : 0);
206 acpigen_emit_byte(ctx, '\0');
209 void acpigen_write_string(struct acpi_ctx *ctx, const char *str)
211 acpigen_emit_byte(ctx, STRING_PREFIX);
212 acpigen_emit_string(ctx, str);
216 * The naming conventions for ACPI namespace names are a bit tricky as
217 * each element has to be 4 chars wide ("All names are a fixed 32 bits.")
218 * and "By convention, when an ASL compiler pads a name shorter than 4
219 * characters, it is done so with trailing underscores ('_')".
221 * Check sections 5.3, 20.2.2 and 20.4 of ACPI spec 6.3 for details.
223 static void acpigen_emit_simple_namestring(struct acpi_ctx *ctx,
229 for (i = 0, ptr = name; i < 4; i++) {
230 if (!*ptr || *ptr == '.')
231 acpigen_emit_byte(ctx, '_');
233 acpigen_emit_byte(ctx, *ptr++);
237 static void acpigen_emit_double_namestring(struct acpi_ctx *ctx,
238 const char *name, int dotpos)
240 acpigen_emit_byte(ctx, DUAL_NAME_PREFIX);
241 acpigen_emit_simple_namestring(ctx, name);
242 acpigen_emit_simple_namestring(ctx, &name[dotpos + 1]);
245 static void acpigen_emit_multi_namestring(struct acpi_ctx *ctx,
248 unsigned char *pathlen;
251 acpigen_emit_byte(ctx, MULTI_NAME_PREFIX);
252 pathlen = ctx->current;
253 acpigen_emit_byte(ctx, 0);
256 acpigen_emit_simple_namestring(ctx, name);
257 /* find end or next entity */
258 while (*name != '.' && *name)
260 /* forward to next */
269 void acpigen_emit_namestring(struct acpi_ctx *ctx, const char *namepath)
275 /* We can start with a '\' */
276 if (*namepath == '\\') {
277 acpigen_emit_byte(ctx, '\\');
281 /* And there can be any number of '^' */
282 while (*namepath == '^') {
283 acpigen_emit_byte(ctx, '^');
287 for (i = 0, dotcount = 0; namepath[i]; i++) {
288 if (namepath[i] == '.') {
294 /* If we have only \\ or only ^* then we need to add a null name */
296 acpigen_emit_byte(ctx, ZERO_OP);
297 else if (dotcount == 0)
298 acpigen_emit_simple_namestring(ctx, namepath);
299 else if (dotcount == 1)
300 acpigen_emit_double_namestring(ctx, namepath, dotpos);
302 acpigen_emit_multi_namestring(ctx, namepath);
305 void acpigen_write_name(struct acpi_ctx *ctx, const char *namepath)
307 acpigen_emit_byte(ctx, NAME_OP);
308 acpigen_emit_namestring(ctx, namepath);
311 void acpigen_write_scope(struct acpi_ctx *ctx, const char *scope)
313 acpigen_emit_byte(ctx, SCOPE_OP);
314 acpigen_write_len_f(ctx);
315 acpigen_emit_namestring(ctx, scope);
318 static void acpigen_write_method_internal(struct acpi_ctx *ctx,
319 const char *name, uint flags)
321 acpigen_emit_byte(ctx, METHOD_OP);
322 acpigen_write_len_f(ctx);
323 acpigen_emit_namestring(ctx, name);
324 acpigen_emit_byte(ctx, flags);
327 /* Method (name, nargs, NotSerialized) */
328 void acpigen_write_method(struct acpi_ctx *ctx, const char *name, int nargs)
330 acpigen_write_method_internal(ctx, name,
331 nargs & ACPI_METHOD_NARGS_MASK);
334 /* Method (name, nargs, Serialized) */
335 void acpigen_write_method_serialized(struct acpi_ctx *ctx, const char *name,
338 acpigen_write_method_internal(ctx, name,
339 (nargs & ACPI_METHOD_NARGS_MASK) |
340 ACPI_METHOD_SERIALIZED_MASK);
343 void acpigen_write_device(struct acpi_ctx *ctx, const char *name)
345 acpigen_emit_ext_op(ctx, DEVICE_OP);
346 acpigen_write_len_f(ctx);
347 acpigen_emit_namestring(ctx, name);
350 void acpigen_write_sta(struct acpi_ctx *ctx, uint status)
352 /* Method (_STA, 0, NotSerialized) { Return (status) } */
353 acpigen_write_method(ctx, "_STA", 0);
354 acpigen_emit_byte(ctx, RETURN_OP);
355 acpigen_write_byte(ctx, status);
356 acpigen_pop_len(ctx);
359 static void acpigen_write_register(struct acpi_ctx *ctx,
360 const struct acpi_gen_regaddr *addr)
362 /* See ACPI v6.3 section 6.4.3.7: Generic Register Descriptor */
363 acpigen_emit_byte(ctx, ACPI_DESCRIPTOR_REGISTER);
364 acpigen_emit_byte(ctx, 0x0c); /* Register Length 7:0 */
365 acpigen_emit_byte(ctx, 0x00); /* Register Length 15:8 */
366 acpigen_emit_byte(ctx, addr->space_id);
367 acpigen_emit_byte(ctx, addr->bit_width);
368 acpigen_emit_byte(ctx, addr->bit_offset);
369 acpigen_emit_byte(ctx, addr->access_size);
370 acpigen_emit_dword(ctx, addr->addrl);
371 acpigen_emit_dword(ctx, addr->addrh);
374 void acpigen_write_resourcetemplate_header(struct acpi_ctx *ctx)
377 * A ResourceTemplate() is a Buffer() with a
378 * (Byte|Word|DWord) containing the length, followed by one or more
379 * resource items, terminated by the end tag.
380 * (small item 0xf, len 1)
382 acpigen_emit_byte(ctx, BUFFER_OP);
383 acpigen_write_len_f(ctx);
384 acpigen_emit_byte(ctx, WORD_PREFIX);
385 ctx->len_stack[ctx->ltop++] = ctx->current;
388 * Add two dummy bytes for the ACPI word (keep aligned with the
389 * calculation in acpigen_write_resourcetemplate_footer() below)
391 acpigen_emit_byte(ctx, 0x00);
392 acpigen_emit_byte(ctx, 0x00);
395 void acpigen_write_resourcetemplate_footer(struct acpi_ctx *ctx)
397 char *p = ctx->len_stack[--ctx->ltop];
400 * See ACPI v6.3 section 6.4.2.9: End Tag
402 * 0x00 is treated as a good checksum according to the spec
403 * and is what iasl generates.
405 acpigen_emit_byte(ctx, ACPI_END_TAG);
406 acpigen_emit_byte(ctx, 0x00);
409 * Start counting past the 2-bytes length added in
410 * acpigen_write_resourcetemplate_header() above
412 len = (char *)ctx->current - (p + 2);
416 p[1] = (len >> 8) & 0xff;
418 acpigen_pop_len(ctx);
421 void acpigen_write_register_resource(struct acpi_ctx *ctx,
422 const struct acpi_gen_regaddr *addr)
424 acpigen_write_resourcetemplate_header(ctx);
425 acpigen_write_register(ctx, addr);
426 acpigen_write_resourcetemplate_footer(ctx);
429 void acpigen_write_prw(struct acpi_ctx *ctx, uint wake, uint level)
431 /* Name (_PRW, Package () { wake, level } */
432 acpigen_write_name(ctx, "_PRW");
433 acpigen_write_package(ctx, 2);
434 acpigen_write_integer(ctx, wake);
435 acpigen_write_integer(ctx, level);
436 acpigen_pop_len(ctx);
442 * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
443 * bytes that make up a UUID Buffer object:
445 * UUID byte order for input to this function:
446 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
448 * UUID byte order output by this function:
449 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
451 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
453 u8 buf[UUID_BIN_LEN];
456 /* Parse UUID string into bytes */
457 ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
459 return log_msg_ret("bad hex", -EINVAL);
462 acpigen_emit_byte(ctx, BUFFER_OP);
463 acpigen_write_len_f(ctx);
465 /* Buffer length in bytes */
466 acpigen_write_word(ctx, UUID_BIN_LEN);
468 /* Output UUID in expected order */
469 acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
471 acpigen_pop_len(ctx);
476 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
477 uint order, const char *const dev_states[],
478 size_t dev_states_count)
482 for (i = 0; i < dev_states_count; i++) {
483 acpigen_write_name(ctx, dev_states[i]);
484 acpigen_write_package(ctx, 1);
485 acpigen_emit_simple_namestring(ctx, name);
486 acpigen_pop_len(ctx); /* Package */
489 acpigen_emit_ext_op(ctx, POWER_RES_OP);
491 acpigen_write_len_f(ctx);
493 acpigen_emit_simple_namestring(ctx, name);
494 acpigen_emit_byte(ctx, level);
495 acpigen_emit_word(ctx, order);
499 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
501 acpigen_emit_ext_op(ctx, SLEEP_OP);
502 acpigen_write_integer(ctx, sleep_ms);
505 void acpigen_write_store(struct acpi_ctx *ctx)
507 acpigen_emit_byte(ctx, STORE_OP);
510 /* Or (arg1, arg2, res) */
511 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
513 acpigen_emit_byte(ctx, OR_OP);
514 acpigen_emit_byte(ctx, arg1);
515 acpigen_emit_byte(ctx, arg2);
516 acpigen_emit_byte(ctx, res);
519 /* And (arg1, arg2, res) */
520 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
522 acpigen_emit_byte(ctx, AND_OP);
523 acpigen_emit_byte(ctx, arg1);
524 acpigen_emit_byte(ctx, arg2);
525 acpigen_emit_byte(ctx, res);
529 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
531 acpigen_emit_byte(ctx, NOT_OP);
532 acpigen_emit_byte(ctx, arg);
533 acpigen_emit_byte(ctx, res);
536 /* Store (str, DEBUG) */
537 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
539 acpigen_write_store(ctx);
540 acpigen_write_string(ctx, str);
541 acpigen_emit_ext_op(ctx, DEBUG_OP);
545 * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
547 * Store (\_SB.GPC0 (addr), Local5)
549 * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
550 * the board's gpiolib.asl
552 * The value needs to be stored in a local variable so that it can be used in
553 * expressions in the ACPI code.
555 * @ctx: ACPI context pointer
556 * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
557 * @addr: GPIO pin configuration register address
560 static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
561 const char *dw0_read, ulong addr)
563 acpigen_write_store(ctx);
564 acpigen_emit_namestring(ctx, dw0_read);
565 acpigen_write_integer(ctx, addr);
566 acpigen_emit_byte(ctx, LOCAL5_OP);
570 * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
572 * @ctx: ACPI context pointer
573 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
574 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
575 * @gpio_num: GPIO number to adjust
576 * @vaL: true to set on, false to set off
578 static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
579 const char *dw0_read, const char *dw0_write,
580 struct acpi_gpio *gpio, bool val)
582 acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
584 /* Store (0x40, Local0) */
585 acpigen_write_store(ctx);
586 acpigen_write_integer(ctx, tx_state_val);
587 acpigen_emit_byte(ctx, LOCAL0_OP);
590 /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
591 acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
593 /* Not (PAD_CFG0_TX_STATE, Local6) */
594 acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
596 /* And (Local5, Local6, Local5) */
597 acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
601 * \_SB.SPC0 (addr, Local5)
602 * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
605 acpigen_emit_namestring(ctx, dw0_write);
606 acpigen_write_integer(ctx, gpio->pin0_addr);
607 acpigen_emit_byte(ctx, LOCAL5_OP);
612 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
613 const char *dw0_read, const char *dw0_write,
614 struct acpi_gpio *gpio, bool enable)
619 set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
620 ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
623 return log_msg_ret("call", ret);