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