xref: /qemu/include/hw/acpi/aml-build.h (revision e2ea299b01fbd9d4d0262cfbfcbdbd02ada984a5)
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 typedef enum {
32     aml_any_acc = 0,
33     aml_byte_acc = 1,
34     aml_word_acc = 2,
35     aml_dword_acc = 3,
36     aml_qword_acc = 4,
37     aml_buffer_acc = 5,
38 } AmlFieldFlags;
39 
40 typedef enum {
41     aml_system_memory = 0x00,
42     aml_system_io = 0x01,
43 } AmlRegionSpace;
44 
45 /**
46  * init_aml_allocator:
47  *
48  * Called for initializing API allocator which allow to use
49  * AML API.
50  * Returns: toplevel container which accumulates all other
51  * AML elements for a table.
52  */
53 Aml *init_aml_allocator(void);
54 
55 /**
56  * free_aml_allocator:
57  *
58  * Releases all elements used by AML API, frees associated memory
59  * and invalidates AML allocator. After this call @init_aml_allocator
60  * should be called again if AML API is to be used again.
61  */
62 void free_aml_allocator(void);
63 
64 /**
65  * aml_append:
66  * @parent_ctx: context to which @child element is added
67  * @child: element that is copied into @parent_ctx context
68  *
69  * Joins Aml elements together and helps to construct AML tables
70  * Examle of usage:
71  *   Aml *table = aml_def_block("SSDT", ...);
72  *   Aml *sb = aml_scope("\_SB");
73  *   Aml *dev = aml_device("PCI0");
74  *
75  *   aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
76  *   aml_append(sb, dev);
77  *   aml_append(table, sb);
78  */
79 void aml_append(Aml *parent_ctx, Aml *child);
80 
81 /* non block AML object primitives */
82 Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
83 Aml *aml_name_decl(const char *name, Aml *val);
84 Aml *aml_return(Aml *val);
85 Aml *aml_int(const uint64_t val);
86 Aml *aml_arg(int pos);
87 Aml *aml_store(Aml *val, Aml *target);
88 Aml *aml_and(Aml *arg1, Aml *arg2);
89 Aml *aml_notify(Aml *arg1, Aml *arg2);
90 Aml *aml_call1(const char *method, Aml *arg1);
91 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
92 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
93 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
94 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
95             uint8_t aln, uint8_t len);
96 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
97                           uint32_t offset, uint32_t len);
98 Aml *aml_named_field(const char *name, unsigned length);
99 Aml *aml_reserved_field(unsigned length);
100 Aml *aml_local(int num);
101 Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
102 Aml *aml_equal(Aml *arg1, Aml *arg2);
103 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
104                    const char *name_format, ...) GCC_FMT_ATTR(4, 5);
105 Aml *aml_eisaid(const char *str);
106 
107 /* Block AML object primitives */
108 Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
109 Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
110 Aml *aml_method(const char *name, int arg_count);
111 Aml *aml_if(Aml *predicate);
112 Aml *aml_package(uint8_t num_elements);
113 Aml *aml_buffer(void);
114 Aml *aml_resource_template(void);
115 Aml *aml_field(const char *name, AmlFieldFlags flags);
116 Aml *aml_varpackage(uint32_t num_elements);
117 
118 /* other helpers */
119 GArray *build_alloc_array(void);
120 void build_free_array(GArray *array);
121 void build_prepend_byte(GArray *array, uint8_t val);
122 void build_append_byte(GArray *array, uint8_t val);
123 void build_append_array(GArray *array, GArray *val);
124 
125 void GCC_FMT_ATTR(2, 3)
126 build_append_namestring(GArray *array, const char *format, ...);
127 
128 void
129 build_prepend_package_length(GArray *package, unsigned length, bool incl_self);
130 void build_package(GArray *package, uint8_t op);
131 void build_append_int(GArray *table, uint64_t value);
132 void build_extop_package(GArray *package, uint8_t op);
133 
134 #endif
135