xref: /openbmc/qemu/include/hw/acpi/aml-build.h (revision 3bfa74a7e8c0787877e0f23a873413ad8817c66c)
119934e0eSIgor Mammedov #ifndef HW_ACPI_GEN_UTILS_H
219934e0eSIgor Mammedov #define HW_ACPI_GEN_UTILS_H
319934e0eSIgor Mammedov 
419934e0eSIgor Mammedov #include <stdint.h>
519934e0eSIgor Mammedov #include <glib.h>
619934e0eSIgor Mammedov #include "qemu/compiler.h"
719934e0eSIgor Mammedov 
80f2707e4SIgor Mammedov typedef enum {
90f2707e4SIgor Mammedov     AML_NO_OPCODE = 0,/* has only data */
100f2707e4SIgor Mammedov     AML_OPCODE,       /* has opcode optionally followed by data */
110f2707e4SIgor Mammedov     AML_PACKAGE,      /* has opcode and uses PkgLength for its length */
120f2707e4SIgor Mammedov     AML_EXT_PACKAGE,  /* ame as AML_PACKAGE but also has 'ExOpPrefix' */
130f2707e4SIgor Mammedov     AML_BUFFER,       /* data encoded as 'DefBuffer' */
140f2707e4SIgor Mammedov     AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */
150f2707e4SIgor Mammedov } AmlBlockFlags;
160f2707e4SIgor Mammedov 
170f2707e4SIgor Mammedov struct Aml {
180f2707e4SIgor Mammedov     GArray *buf;
190f2707e4SIgor Mammedov 
200f2707e4SIgor Mammedov     /*< private >*/
210f2707e4SIgor Mammedov     uint8_t op;
220f2707e4SIgor Mammedov     AmlBlockFlags block_flags;
230f2707e4SIgor Mammedov };
240f2707e4SIgor Mammedov typedef struct Aml Aml;
250f2707e4SIgor Mammedov 
260f2707e4SIgor Mammedov /**
270f2707e4SIgor Mammedov  * init_aml_allocator:
280f2707e4SIgor Mammedov  *
290f2707e4SIgor Mammedov  * Called for initializing API allocator which allow to use
300f2707e4SIgor Mammedov  * AML API.
310f2707e4SIgor Mammedov  * Returns: toplevel container which accumulates all other
320f2707e4SIgor Mammedov  * AML elements for a table.
330f2707e4SIgor Mammedov  */
340f2707e4SIgor Mammedov Aml *init_aml_allocator(void);
350f2707e4SIgor Mammedov 
360f2707e4SIgor Mammedov /**
370f2707e4SIgor Mammedov  * free_aml_allocator:
380f2707e4SIgor Mammedov  *
390f2707e4SIgor Mammedov  * Releases all elements used by AML API, frees associated memory
400f2707e4SIgor Mammedov  * and invalidates AML allocator. After this call @init_aml_allocator
410f2707e4SIgor Mammedov  * should be called again if AML API is to be used again.
420f2707e4SIgor Mammedov  */
430f2707e4SIgor Mammedov void free_aml_allocator(void);
440f2707e4SIgor Mammedov 
450f2707e4SIgor Mammedov /**
460f2707e4SIgor Mammedov  * aml_append:
470f2707e4SIgor Mammedov  * @parent_ctx: context to which @child element is added
480f2707e4SIgor Mammedov  * @child: element that is copied into @parent_ctx context
490f2707e4SIgor Mammedov  *
500f2707e4SIgor Mammedov  * Joins Aml elements together and helps to construct AML tables
510f2707e4SIgor Mammedov  * Examle of usage:
520f2707e4SIgor Mammedov  *   Aml *table = aml_def_block("SSDT", ...);
530f2707e4SIgor Mammedov  *   Aml *sb = aml_scope("\_SB");
540f2707e4SIgor Mammedov  *   Aml *dev = aml_device("PCI0");
550f2707e4SIgor Mammedov  *
560f2707e4SIgor Mammedov  *   aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
570f2707e4SIgor Mammedov  *   aml_append(sb, dev);
580f2707e4SIgor Mammedov  *   aml_append(table, sb);
590f2707e4SIgor Mammedov  */
600f2707e4SIgor Mammedov void aml_append(Aml *parent_ctx, Aml *child);
610f2707e4SIgor Mammedov 
623c054bd5SIgor Mammedov /* non block AML object primitives */
633c054bd5SIgor Mammedov Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
643c054bd5SIgor Mammedov Aml *aml_name_decl(const char *name, Aml *val);
65b25af5adSIgor Mammedov Aml *aml_return(Aml *val);
66295a515dSIgor Mammedov Aml *aml_int(const uint64_t val);
677193f3a6SIgor Mammedov Aml *aml_arg(int pos);
68c263b3f7SIgor Mammedov Aml *aml_store(Aml *val, Aml *target);
69926f5aaeSIgor Mammedov Aml *aml_and(Aml *arg1, Aml *arg2);
7034189453SIgor Mammedov Aml *aml_notify(Aml *arg1, Aml *arg2);
713f3992b7SIgor Mammedov Aml *aml_call1(const char *method, Aml *arg1);
723f3992b7SIgor Mammedov Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
733f3992b7SIgor Mammedov Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
743f3992b7SIgor Mammedov Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
753c054bd5SIgor Mammedov 
762ef7c27bSIgor Mammedov /* Block AML object primitives */
772ef7c27bSIgor Mammedov Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
78be06ebd0SIgor Mammedov Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
79ea2407d7SIgor Mammedov Aml *aml_method(const char *name, int arg_count);
8032acac9eSIgor Mammedov Aml *aml_if(Aml *predicate);
81*3bfa74a7SIgor Mammedov Aml *aml_package(uint8_t num_elements);
822ef7c27bSIgor Mammedov 
830f2707e4SIgor Mammedov /* other helpers */
8419934e0eSIgor Mammedov GArray *build_alloc_array(void);
8519934e0eSIgor Mammedov void build_free_array(GArray *array);
8619934e0eSIgor Mammedov void build_prepend_byte(GArray *array, uint8_t val);
8719934e0eSIgor Mammedov void build_append_byte(GArray *array, uint8_t val);
8819934e0eSIgor Mammedov void build_append_array(GArray *array, GArray *val);
8919934e0eSIgor Mammedov 
9019934e0eSIgor Mammedov void GCC_FMT_ATTR(2, 3)
91eae8bdedSIgor Mammedov build_append_namestring(GArray *array, const char *format, ...);
9219934e0eSIgor Mammedov 
93661875e9SIgor Mammedov void build_prepend_package_length(GArray *package);
94661875e9SIgor Mammedov void build_package(GArray *package, uint8_t op);
95295a515dSIgor Mammedov void build_append_int(GArray *table, uint64_t value);
9619934e0eSIgor Mammedov void build_extop_package(GArray *package, uint8_t op);
9719934e0eSIgor Mammedov 
9819934e0eSIgor Mammedov #endif
99