xref: /qemu/include/hw/acpi/aml-build.h (revision 52fa397c58667080979e8aa64177c0f69b1851b7)
1 #ifndef HW_ACPI_GEN_UTILS_H
2 #define HW_ACPI_GEN_UTILS_H
3 
4 #include <stdint.h>
5 #include <glib.h>
6 #include "qemu/compiler.h"
7 
8 typedef enum {
9     AML_NO_OPCODE = 0,/* has only data */
10     AML_OPCODE,       /* has opcode optionally followed by data */
11     AML_PACKAGE,      /* has opcode and uses PkgLength for its length */
12     AML_EXT_PACKAGE,  /* ame as AML_PACKAGE but also has 'ExOpPrefix' */
13     AML_BUFFER,       /* data encoded as 'DefBuffer' */
14     AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */
15 } AmlBlockFlags;
16 
17 struct Aml {
18     GArray *buf;
19 
20     /*< private >*/
21     uint8_t op;
22     AmlBlockFlags block_flags;
23 };
24 typedef struct Aml Aml;
25 
26 typedef enum {
27     aml_decode10 = 0,
28     aml_decode16 = 1,
29 } AmlIODecode;
30 
31 /**
32  * init_aml_allocator:
33  *
34  * Called for initializing API allocator which allow to use
35  * AML API.
36  * Returns: toplevel container which accumulates all other
37  * AML elements for a table.
38  */
39 Aml *init_aml_allocator(void);
40 
41 /**
42  * free_aml_allocator:
43  *
44  * Releases all elements used by AML API, frees associated memory
45  * and invalidates AML allocator. After this call @init_aml_allocator
46  * should be called again if AML API is to be used again.
47  */
48 void free_aml_allocator(void);
49 
50 /**
51  * aml_append:
52  * @parent_ctx: context to which @child element is added
53  * @child: element that is copied into @parent_ctx context
54  *
55  * Joins Aml elements together and helps to construct AML tables
56  * Examle of usage:
57  *   Aml *table = aml_def_block("SSDT", ...);
58  *   Aml *sb = aml_scope("\_SB");
59  *   Aml *dev = aml_device("PCI0");
60  *
61  *   aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
62  *   aml_append(sb, dev);
63  *   aml_append(table, sb);
64  */
65 void aml_append(Aml *parent_ctx, Aml *child);
66 
67 /* non block AML object primitives */
68 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
69 Aml *aml_name_decl(const char *name, Aml *val);
70 Aml *aml_return(Aml *val);
71 Aml *aml_int(const uint64_t val);
72 Aml *aml_arg(int pos);
73 Aml *aml_store(Aml *val, Aml *target);
74 Aml *aml_and(Aml *arg1, Aml *arg2);
75 Aml *aml_notify(Aml *arg1, Aml *arg2);
76 Aml *aml_call1(const char *method, Aml *arg1);
77 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
78 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
79 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
80 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
81             uint8_t aln, uint8_t len);
82 
83 /* Block AML object primitives */
84 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
85 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
86 Aml *aml_method(const char *name, int arg_count);
87 Aml *aml_if(Aml *predicate);
88 Aml *aml_package(uint8_t num_elements);
89 Aml *aml_buffer(void);
90 Aml *aml_resource_template(void);
91 
92 /* other helpers */
93 GArray *build_alloc_array(void);
94 void build_free_array(GArray *array);
95 void build_prepend_byte(GArray *array, uint8_t val);
96 void build_append_byte(GArray *array, uint8_t val);
97 void build_append_array(GArray *array, GArray *val);
98 
99 void GCC_FMT_ATTR(2, 3)
100 build_append_namestring(GArray *array, const char *format, ...);
101 
102 void build_prepend_package_length(GArray *package);
103 void build_package(GArray *package, uint8_t op);
104 void build_append_int(GArray *table, uint64_t value);
105 void build_extop_package(GArray *package, uint8_t op);
106 
107 #endif
108