xref: /openbmc/qemu/include/hw/acpi/aml-build.h (revision 6ece7053d6a4a502d2ea5d24ecf512caaa1437c7)
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 
2652fa397cSIgor Mammedov typedef enum {
2752fa397cSIgor Mammedov     aml_decode10 = 0,
2852fa397cSIgor Mammedov     aml_decode16 = 1,
2952fa397cSIgor Mammedov } AmlIODecode;
3052fa397cSIgor Mammedov 
3131127938SIgor Mammedov typedef enum {
32e2ea299bSIgor Mammedov     aml_any_acc = 0,
33214ae59fSIgor Mammedov     aml_byte_acc = 1,
34e2ea299bSIgor Mammedov     aml_word_acc = 2,
35e2ea299bSIgor Mammedov     aml_dword_acc = 3,
36e2ea299bSIgor Mammedov     aml_qword_acc = 4,
37e2ea299bSIgor Mammedov     aml_buffer_acc = 5,
38214ae59fSIgor Mammedov } AmlFieldFlags;
39214ae59fSIgor Mammedov 
40214ae59fSIgor Mammedov typedef enum {
4131127938SIgor Mammedov     aml_system_memory = 0x00,
4231127938SIgor Mammedov     aml_system_io = 0x01,
4331127938SIgor Mammedov } AmlRegionSpace;
4431127938SIgor Mammedov 
45*6ece7053SIgor Mammedov typedef enum {
46*6ece7053SIgor Mammedov     aml_memory_range = 0,
47*6ece7053SIgor Mammedov     aml_io_range = 1,
48*6ece7053SIgor Mammedov     aml_bus_number_range = 2,
49*6ece7053SIgor Mammedov } AmlResourceType;
50*6ece7053SIgor Mammedov 
51*6ece7053SIgor Mammedov typedef enum {
52*6ece7053SIgor Mammedov     aml_sub_decode = 1 << 1,
53*6ece7053SIgor Mammedov     aml_pos_decode = 0
54*6ece7053SIgor Mammedov } AmlDecode;
55*6ece7053SIgor Mammedov 
56*6ece7053SIgor Mammedov typedef enum {
57*6ece7053SIgor Mammedov     aml_max_fixed = 1 << 3,
58*6ece7053SIgor Mammedov     aml_max_not_fixed = 0,
59*6ece7053SIgor Mammedov } AmlMaxFixed;
60*6ece7053SIgor Mammedov 
61*6ece7053SIgor Mammedov typedef enum {
62*6ece7053SIgor Mammedov     aml_min_fixed = 1 << 2,
63*6ece7053SIgor Mammedov     aml_min_not_fixed = 0
64*6ece7053SIgor Mammedov } AmlMinFixed;
65*6ece7053SIgor Mammedov 
66*6ece7053SIgor Mammedov /*
67*6ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions
68*6ece7053SIgor Mammedov  * _RNG field definition
69*6ece7053SIgor Mammedov  */
70*6ece7053SIgor Mammedov typedef enum {
71*6ece7053SIgor Mammedov     aml_isa_only = 1,
72*6ece7053SIgor Mammedov     aml_non_isa_only = 2,
73*6ece7053SIgor Mammedov     aml_entire_range = 3,
74*6ece7053SIgor Mammedov } AmlISARanges;
75*6ece7053SIgor Mammedov 
76*6ece7053SIgor Mammedov /*
77*6ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
78*6ece7053SIgor Mammedov  * _MEM field definition
79*6ece7053SIgor Mammedov  */
80*6ece7053SIgor Mammedov typedef enum {
81*6ece7053SIgor Mammedov     aml_non_cacheable = 0,
82*6ece7053SIgor Mammedov     aml_cacheable = 1,
83*6ece7053SIgor Mammedov     aml_write_combining = 2,
84*6ece7053SIgor Mammedov     aml_prefetchable = 3,
85*6ece7053SIgor Mammedov } AmlCacheble;
86*6ece7053SIgor Mammedov 
87*6ece7053SIgor Mammedov /*
88*6ece7053SIgor Mammedov  * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
89*6ece7053SIgor Mammedov  * _RW field definition
90*6ece7053SIgor Mammedov  */
91*6ece7053SIgor Mammedov typedef enum {
92*6ece7053SIgor Mammedov     aml_ReadOnly = 0,
93*6ece7053SIgor Mammedov     aml_ReadWrite = 1,
94*6ece7053SIgor Mammedov } AmlReadAndWrite;
95*6ece7053SIgor Mammedov 
960f2707e4SIgor Mammedov /**
970f2707e4SIgor Mammedov  * init_aml_allocator:
980f2707e4SIgor Mammedov  *
990f2707e4SIgor Mammedov  * Called for initializing API allocator which allow to use
1000f2707e4SIgor Mammedov  * AML API.
1010f2707e4SIgor Mammedov  * Returns: toplevel container which accumulates all other
1020f2707e4SIgor Mammedov  * AML elements for a table.
1030f2707e4SIgor Mammedov  */
1040f2707e4SIgor Mammedov Aml *init_aml_allocator(void);
1050f2707e4SIgor Mammedov 
1060f2707e4SIgor Mammedov /**
1070f2707e4SIgor Mammedov  * free_aml_allocator:
1080f2707e4SIgor Mammedov  *
1090f2707e4SIgor Mammedov  * Releases all elements used by AML API, frees associated memory
1100f2707e4SIgor Mammedov  * and invalidates AML allocator. After this call @init_aml_allocator
1110f2707e4SIgor Mammedov  * should be called again if AML API is to be used again.
1120f2707e4SIgor Mammedov  */
1130f2707e4SIgor Mammedov void free_aml_allocator(void);
1140f2707e4SIgor Mammedov 
1150f2707e4SIgor Mammedov /**
1160f2707e4SIgor Mammedov  * aml_append:
1170f2707e4SIgor Mammedov  * @parent_ctx: context to which @child element is added
1180f2707e4SIgor Mammedov  * @child: element that is copied into @parent_ctx context
1190f2707e4SIgor Mammedov  *
1200f2707e4SIgor Mammedov  * Joins Aml elements together and helps to construct AML tables
1210f2707e4SIgor Mammedov  * Examle of usage:
1220f2707e4SIgor Mammedov  *   Aml *table = aml_def_block("SSDT", ...);
1230f2707e4SIgor Mammedov  *   Aml *sb = aml_scope("\_SB");
1240f2707e4SIgor Mammedov  *   Aml *dev = aml_device("PCI0");
1250f2707e4SIgor Mammedov  *
1260f2707e4SIgor Mammedov  *   aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
1270f2707e4SIgor Mammedov  *   aml_append(sb, dev);
1280f2707e4SIgor Mammedov  *   aml_append(table, sb);
1290f2707e4SIgor Mammedov  */
1300f2707e4SIgor Mammedov void aml_append(Aml *parent_ctx, Aml *child);
1310f2707e4SIgor Mammedov 
1323c054bd5SIgor Mammedov /* non block AML object primitives */
1333c054bd5SIgor Mammedov Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
1343c054bd5SIgor Mammedov Aml *aml_name_decl(const char *name, Aml *val);
135b25af5adSIgor Mammedov Aml *aml_return(Aml *val);
136295a515dSIgor Mammedov Aml *aml_int(const uint64_t val);
1377193f3a6SIgor Mammedov Aml *aml_arg(int pos);
138c263b3f7SIgor Mammedov Aml *aml_store(Aml *val, Aml *target);
139926f5aaeSIgor Mammedov Aml *aml_and(Aml *arg1, Aml *arg2);
14034189453SIgor Mammedov Aml *aml_notify(Aml *arg1, Aml *arg2);
1413f3992b7SIgor Mammedov Aml *aml_call1(const char *method, Aml *arg1);
1423f3992b7SIgor Mammedov Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
1433f3992b7SIgor Mammedov Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
1443f3992b7SIgor Mammedov Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
14552fa397cSIgor Mammedov Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
14652fa397cSIgor Mammedov             uint8_t aln, uint8_t len);
14731127938SIgor Mammedov Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
14831127938SIgor Mammedov                           uint32_t offset, uint32_t len);
149214ae59fSIgor Mammedov Aml *aml_named_field(const char *name, unsigned length);
150e2ea299bSIgor Mammedov Aml *aml_reserved_field(unsigned length);
151b8a5d689SIgor Mammedov Aml *aml_local(int num);
152d5e5830fSIgor Mammedov Aml *aml_string(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
15315e44e56SIgor Mammedov Aml *aml_equal(Aml *arg1, Aml *arg2);
1543dd15643SIgor Mammedov Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
1553dd15643SIgor Mammedov                    const char *name_format, ...) GCC_FMT_ATTR(4, 5);
156a7891dacSIgor Mammedov Aml *aml_eisaid(const char *str);
157*6ece7053SIgor Mammedov Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
158*6ece7053SIgor Mammedov                          AmlDecode dec, uint16_t addr_gran,
159*6ece7053SIgor Mammedov                          uint16_t addr_min, uint16_t addr_max,
160*6ece7053SIgor Mammedov                          uint16_t addr_trans, uint16_t len);
161*6ece7053SIgor Mammedov Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
162*6ece7053SIgor Mammedov                  AmlDecode dec, AmlISARanges isa_ranges,
163*6ece7053SIgor Mammedov                  uint16_t addr_gran, uint16_t addr_min,
164*6ece7053SIgor Mammedov                  uint16_t addr_max, uint16_t addr_trans,
165*6ece7053SIgor Mammedov                  uint16_t len);
166*6ece7053SIgor Mammedov Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
167*6ece7053SIgor Mammedov                       AmlMaxFixed max_fixed, AmlCacheble cacheable,
168*6ece7053SIgor Mammedov                       AmlReadAndWrite read_and_write,
169*6ece7053SIgor Mammedov                       uint32_t addr_gran, uint32_t addr_min,
170*6ece7053SIgor Mammedov                       uint32_t addr_max, uint32_t addr_trans,
171*6ece7053SIgor Mammedov                       uint32_t len);
172*6ece7053SIgor Mammedov Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
173*6ece7053SIgor Mammedov                       AmlMaxFixed max_fixed, AmlCacheble cacheable,
174*6ece7053SIgor Mammedov                       AmlReadAndWrite read_and_write,
175*6ece7053SIgor Mammedov                       uint64_t addr_gran, uint64_t addr_min,
176*6ece7053SIgor Mammedov                       uint64_t addr_max, uint64_t addr_trans,
177*6ece7053SIgor Mammedov                       uint64_t len);
1783c054bd5SIgor Mammedov 
1792ef7c27bSIgor Mammedov /* Block AML object primitives */
1802ef7c27bSIgor Mammedov Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
181be06ebd0SIgor Mammedov Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2);
182ea2407d7SIgor Mammedov Aml *aml_method(const char *name, int arg_count);
18332acac9eSIgor Mammedov Aml *aml_if(Aml *predicate);
1843bfa74a7SIgor Mammedov Aml *aml_package(uint8_t num_elements);
18504b8da54SIgor Mammedov Aml *aml_buffer(void);
186ad4a80bcSIgor Mammedov Aml *aml_resource_template(void);
187214ae59fSIgor Mammedov Aml *aml_field(const char *name, AmlFieldFlags flags);
188a678508eSIgor Mammedov Aml *aml_varpackage(uint32_t num_elements);
1892ef7c27bSIgor Mammedov 
1900f2707e4SIgor Mammedov /* other helpers */
19119934e0eSIgor Mammedov GArray *build_alloc_array(void);
19219934e0eSIgor Mammedov void build_free_array(GArray *array);
19319934e0eSIgor Mammedov void build_prepend_byte(GArray *array, uint8_t val);
19419934e0eSIgor Mammedov void build_append_byte(GArray *array, uint8_t val);
19519934e0eSIgor Mammedov void build_append_array(GArray *array, GArray *val);
19619934e0eSIgor Mammedov 
19719934e0eSIgor Mammedov void GCC_FMT_ATTR(2, 3)
198eae8bdedSIgor Mammedov build_append_namestring(GArray *array, const char *format, ...);
19919934e0eSIgor Mammedov 
20019fff2d4SIgor Mammedov void
20119fff2d4SIgor Mammedov build_prepend_package_length(GArray *package, unsigned length, bool incl_self);
202661875e9SIgor Mammedov void build_package(GArray *package, uint8_t op);
203295a515dSIgor Mammedov void build_append_int(GArray *table, uint64_t value);
20419934e0eSIgor Mammedov void build_extop_package(GArray *package, uint8_t op);
20519934e0eSIgor Mammedov 
20619934e0eSIgor Mammedov #endif
207