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);
432 * ACPI 6.3 Section 19.6.142 table 19-438 defines a special output order for the
433 * bytes that make up a UUID Buffer object:
435 * UUID byte order for input to this function:
436 * aabbccdd-eeff-gghh-iijj-kkllmmnnoopp
438 * UUID byte order output by this function:
439 * ddccbbaa-ffee-hhgg-iijj-kkllmmnnoopp
441 int acpigen_write_uuid(struct acpi_ctx *ctx, const char *uuid)
443 u8 buf[UUID_BIN_LEN];
446 /* Parse UUID string into bytes */
447 ret = uuid_str_to_bin(uuid, buf, UUID_STR_FORMAT_GUID);
449 return log_msg_ret("bad hex", -EINVAL);
452 acpigen_emit_byte(ctx, BUFFER_OP);
453 acpigen_write_len_f(ctx);
455 /* Buffer length in bytes */
456 acpigen_write_word(ctx, UUID_BIN_LEN);
458 /* Output UUID in expected order */
459 acpigen_emit_stream(ctx, (char *)buf, UUID_BIN_LEN);
461 acpigen_pop_len(ctx);
466 void acpigen_write_power_res(struct acpi_ctx *ctx, const char *name, uint level,
467 uint order, const char *const dev_states[],
468 size_t dev_states_count)
472 for (i = 0; i < dev_states_count; i++) {
473 acpigen_write_name(ctx, dev_states[i]);
474 acpigen_write_package(ctx, 1);
475 acpigen_emit_simple_namestring(ctx, name);
476 acpigen_pop_len(ctx); /* Package */
479 acpigen_emit_ext_op(ctx, POWER_RES_OP);
481 acpigen_write_len_f(ctx);
483 acpigen_emit_simple_namestring(ctx, name);
484 acpigen_emit_byte(ctx, level);
485 acpigen_emit_word(ctx, order);
489 void acpigen_write_sleep(struct acpi_ctx *ctx, u64 sleep_ms)
491 acpigen_emit_ext_op(ctx, SLEEP_OP);
492 acpigen_write_integer(ctx, sleep_ms);
495 void acpigen_write_store(struct acpi_ctx *ctx)
497 acpigen_emit_byte(ctx, STORE_OP);
500 /* Or (arg1, arg2, res) */
501 void acpigen_write_or(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
503 acpigen_emit_byte(ctx, OR_OP);
504 acpigen_emit_byte(ctx, arg1);
505 acpigen_emit_byte(ctx, arg2);
506 acpigen_emit_byte(ctx, res);
509 /* And (arg1, arg2, res) */
510 void acpigen_write_and(struct acpi_ctx *ctx, u8 arg1, u8 arg2, u8 res)
512 acpigen_emit_byte(ctx, AND_OP);
513 acpigen_emit_byte(ctx, arg1);
514 acpigen_emit_byte(ctx, arg2);
515 acpigen_emit_byte(ctx, res);
519 void acpigen_write_not(struct acpi_ctx *ctx, u8 arg, u8 res)
521 acpigen_emit_byte(ctx, NOT_OP);
522 acpigen_emit_byte(ctx, arg);
523 acpigen_emit_byte(ctx, res);
526 /* Store (str, DEBUG) */
527 void acpigen_write_debug_string(struct acpi_ctx *ctx, const char *str)
529 acpigen_write_store(ctx);
530 acpigen_write_string(ctx, str);
531 acpigen_emit_ext_op(ctx, DEBUG_OP);
535 * acpigen_get_dw0_in_local5() - Generate code to put dw0 cfg0 in local5
537 * Store (\_SB.GPC0 (addr), Local5)
539 * \_SB.GPC0 is used to read cfg0 value from dw0. It is typically defined in
540 * the board's gpiolib.asl
542 * The value needs to be stored in a local variable so that it can be used in
543 * expressions in the ACPI code.
545 * @ctx: ACPI context pointer
546 * @dw0_read: Name to use to read dw0, e.g. "\\_SB.GPC0"
547 * @addr: GPIO pin configuration register address
550 static void acpigen_get_dw0_in_local5(struct acpi_ctx *ctx,
551 const char *dw0_read, ulong addr)
553 acpigen_write_store(ctx);
554 acpigen_emit_namestring(ctx, dw0_read);
555 acpigen_write_integer(ctx, addr);
556 acpigen_emit_byte(ctx, LOCAL5_OP);
560 * acpigen_set_gpio_val() - Emit code to set value of TX GPIO to on/off
562 * @ctx: ACPI context pointer
563 * @dw0_read: Method name to use to read dw0, e.g. "\\_SB.GPC0"
564 * @dw0_write: Method name to use to read dw0, e.g. "\\_SB.SPC0"
565 * @gpio_num: GPIO number to adjust
566 * @vaL: true to set on, false to set off
568 static int acpigen_set_gpio_val(struct acpi_ctx *ctx, u32 tx_state_val,
569 const char *dw0_read, const char *dw0_write,
570 struct acpi_gpio *gpio, bool val)
572 acpigen_get_dw0_in_local5(ctx, dw0_read, gpio->pin0_addr);
574 /* Store (0x40, Local0) */
575 acpigen_write_store(ctx);
576 acpigen_write_integer(ctx, tx_state_val);
577 acpigen_emit_byte(ctx, LOCAL0_OP);
580 /* Or (Local5, PAD_CFG0_TX_STATE, Local5) */
581 acpigen_write_or(ctx, LOCAL5_OP, LOCAL0_OP, LOCAL5_OP);
583 /* Not (PAD_CFG0_TX_STATE, Local6) */
584 acpigen_write_not(ctx, LOCAL0_OP, LOCAL6_OP);
586 /* And (Local5, Local6, Local5) */
587 acpigen_write_and(ctx, LOCAL5_OP, LOCAL6_OP, LOCAL5_OP);
591 * \_SB.SPC0 (addr, Local5)
592 * \_SB.SPC0 is used to write cfg0 value in dw0. It is defined in
595 acpigen_emit_namestring(ctx, dw0_write);
596 acpigen_write_integer(ctx, gpio->pin0_addr);
597 acpigen_emit_byte(ctx, LOCAL5_OP);
602 int acpigen_set_enable_tx_gpio(struct acpi_ctx *ctx, u32 tx_state_val,
603 const char *dw0_read, const char *dw0_write,
604 struct acpi_gpio *gpio, bool enable)
609 set = gpio->polarity == ACPI_GPIO_ACTIVE_HIGH ? enable : !enable;
610 ret = acpigen_set_gpio_val(ctx, tx_state_val, dw0_read, dw0_write, gpio,
613 return log_msg_ret("call", ret);