1 #ifndef HW_ACPI_GEN_UTILS_H
2 #define HW_ACPI_GEN_UTILS_H
4 #include "hw/acpi/acpi-defs.h"
5 #include "hw/acpi/bios-linker-loader.h"
7 /* Reserve RAM space for tables: add another order of magnitude. */
8 #define ACPI_BUILD_TABLE_MAX_SIZE 0x200000
10 #define ACPI_BUILD_APPNAME6 "BOCHS "
11 #define ACPI_BUILD_APPNAME4 "BXPC"
13 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
14 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
15 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log"
17 #define AML_NOTIFY_METHOD "NTFY"
20 AML_NO_OPCODE
= 0,/* has only data */
21 AML_OPCODE
, /* has opcode optionally followed by data */
22 AML_PACKAGE
, /* has opcode and uses PkgLength for its length */
23 AML_EXT_PACKAGE
, /* Same as AML_PACKAGE but also has 'ExOpPrefix' */
24 AML_BUFFER
, /* data encoded as 'DefBuffer' */
25 AML_RES_TEMPLATE
, /* encoded as ResourceTemplate macro */
33 AmlBlockFlags block_flags
;
35 typedef struct Aml Aml
;
38 AML_COMPATIBILITY
= 0,
76 AML_WRITE_AS_ONES
= 1,
77 AML_WRITE_AS_ZEROS
= 2,
81 AML_SYSTEM_MEMORY
= 0X00,
83 AML_PCI_CONFIG
= 0X02,
89 AML_BUS_NUMBER_RANGE
= 2,
93 AML_SUB_DECODE
= 1 << 1,
98 AML_MAX_FIXED
= 1 << 3,
99 AML_MAX_NOT_FIXED
= 0,
103 AML_MIN_FIXED
= 1 << 2,
104 AML_MIN_NOT_FIXED
= 0
108 * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions
109 * _RNG field definition
113 AML_NON_ISA_ONLY
= 2,
114 AML_ENTIRE_RANGE
= 3,
118 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
119 * _MEM field definition
122 AML_NON_CACHEABLE
= 0,
124 AML_WRITE_COMBINING
= 2,
125 AML_PREFETCHABLE
= 3,
129 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
130 * _RW field definition
138 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
139 * Interrupt Vector Flags Bits[0] Consumer/Producer
142 AML_CONSUMER_PRODUCER
= 0,
144 } AmlConsumerAndProducer
;
147 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
148 * _HE field definition
156 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
157 * _LL field definition
162 } AmlActiveHighAndLow
;
165 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
166 * _SHR field definition
171 AML_EXCLUSIVE_AND_WAKE
= 2,
172 AML_SHARED_AND_WAKE
= 3,
175 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: MethodFlags */
177 AML_NOTSERIALIZED
= 0,
182 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition
183 * GPIO Connection Type
186 AML_INTERRUPT_CONNECTION
= 0,
187 AML_IO_CONNECTION
= 1,
188 } AmlGpioConnectionType
;
191 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition
192 * _PPI field definition
195 AML_PULL_DEFAULT
= 0,
202 MEM_AFFINITY_NOFLAGS
= 0,
203 MEM_AFFINITY_ENABLED
= (1 << 0),
204 MEM_AFFINITY_HOTPLUGGABLE
= (1 << 1),
205 MEM_AFFINITY_NON_VOLATILE
= (1 << 2),
206 } MemoryAffinityFlags
;
209 struct AcpiBuildTables
{
217 * init_aml_allocator:
219 * Called for initializing API allocator which allow to use
221 * Returns: toplevel container which accumulates all other
222 * AML elements for a table.
224 Aml
*init_aml_allocator(void);
227 * free_aml_allocator:
229 * Releases all elements used by AML API, frees associated memory
230 * and invalidates AML allocator. After this call @init_aml_allocator
231 * should be called again if AML API is to be used again.
233 void free_aml_allocator(void);
237 * @parent_ctx: context to which @child element is added
238 * @child: element that is copied into @parent_ctx context
240 * Joins Aml elements together and helps to construct AML tables
242 * Aml *table = aml_def_block("SSDT", ...);
243 * Aml *sb = aml_scope("\\_SB");
244 * Aml *dev = aml_device("PCI0");
246 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
247 * aml_append(sb, dev);
248 * aml_append(table, sb);
250 void aml_append(Aml
*parent_ctx
, Aml
*child
);
252 /* non block AML object primitives */
253 Aml
*aml_name(const char *name_format
, ...) GCC_FMT_ATTR(1, 2);
254 Aml
*aml_name_decl(const char *name
, Aml
*val
);
255 Aml
*aml_debug(void);
256 Aml
*aml_return(Aml
*val
);
257 Aml
*aml_int(const uint64_t val
);
258 Aml
*aml_arg(int pos
);
259 Aml
*aml_to_integer(Aml
*arg
);
260 Aml
*aml_to_hexstring(Aml
*src
, Aml
*dst
);
261 Aml
*aml_to_buffer(Aml
*src
, Aml
*dst
);
262 Aml
*aml_store(Aml
*val
, Aml
*target
);
263 Aml
*aml_and(Aml
*arg1
, Aml
*arg2
, Aml
*dst
);
264 Aml
*aml_or(Aml
*arg1
, Aml
*arg2
, Aml
*dst
);
265 Aml
*aml_lor(Aml
*arg1
, Aml
*arg2
);
266 Aml
*aml_shiftleft(Aml
*arg1
, Aml
*count
);
267 Aml
*aml_shiftright(Aml
*arg1
, Aml
*count
, Aml
*dst
);
268 Aml
*aml_lless(Aml
*arg1
, Aml
*arg2
);
269 Aml
*aml_add(Aml
*arg1
, Aml
*arg2
, Aml
*dst
);
270 Aml
*aml_subtract(Aml
*arg1
, Aml
*arg2
, Aml
*dst
);
271 Aml
*aml_increment(Aml
*arg
);
272 Aml
*aml_decrement(Aml
*arg
);
273 Aml
*aml_index(Aml
*arg1
, Aml
*idx
);
274 Aml
*aml_notify(Aml
*arg1
, Aml
*arg2
);
275 Aml
*aml_call0(const char *method
);
276 Aml
*aml_call1(const char *method
, Aml
*arg1
);
277 Aml
*aml_call2(const char *method
, Aml
*arg1
, Aml
*arg2
);
278 Aml
*aml_call3(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
);
279 Aml
*aml_call4(const char *method
, Aml
*arg1
, Aml
*arg2
, Aml
*arg3
, Aml
*arg4
);
280 Aml
*aml_gpio_int(AmlConsumerAndProducer con_and_pro
,
281 AmlLevelAndEdge edge_level
,
282 AmlActiveHighAndLow active_level
, AmlShared shared
,
283 AmlPinConfig pin_config
, uint16_t debounce_timeout
,
284 const uint32_t pin_list
[], uint32_t pin_count
,
285 const char *resource_source_name
,
286 const uint8_t *vendor_data
, uint16_t vendor_data_len
);
287 Aml
*aml_memory32_fixed(uint32_t addr
, uint32_t size
,
288 AmlReadAndWrite read_and_write
);
289 Aml
*aml_interrupt(AmlConsumerAndProducer con_and_pro
,
290 AmlLevelAndEdge level_and_edge
,
291 AmlActiveHighAndLow high_and_low
, AmlShared shared
,
292 uint32_t *irq_list
, uint8_t irq_count
);
293 Aml
*aml_io(AmlIODecode dec
, uint16_t min_base
, uint16_t max_base
,
294 uint8_t aln
, uint8_t len
);
295 Aml
*aml_operation_region(const char *name
, AmlRegionSpace rs
,
296 Aml
*offset
, uint32_t len
);
297 Aml
*aml_irq_no_flags(uint8_t irq
);
298 Aml
*aml_named_field(const char *name
, unsigned length
);
299 Aml
*aml_reserved_field(unsigned length
);
300 Aml
*aml_local(int num
);
301 Aml
*aml_string(const char *name_format
, ...) GCC_FMT_ATTR(1, 2);
302 Aml
*aml_lnot(Aml
*arg
);
303 Aml
*aml_equal(Aml
*arg1
, Aml
*arg2
);
304 Aml
*aml_lgreater(Aml
*arg1
, Aml
*arg2
);
305 Aml
*aml_lgreater_equal(Aml
*arg1
, Aml
*arg2
);
306 Aml
*aml_processor(uint8_t proc_id
, uint32_t pblk_addr
, uint8_t pblk_len
,
307 const char *name_format
, ...) GCC_FMT_ATTR(4, 5);
308 Aml
*aml_eisaid(const char *str
);
309 Aml
*aml_word_bus_number(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
310 AmlDecode dec
, uint16_t addr_gran
,
311 uint16_t addr_min
, uint16_t addr_max
,
312 uint16_t addr_trans
, uint16_t len
);
313 Aml
*aml_word_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
314 AmlDecode dec
, AmlISARanges isa_ranges
,
315 uint16_t addr_gran
, uint16_t addr_min
,
316 uint16_t addr_max
, uint16_t addr_trans
,
318 Aml
*aml_dword_io(AmlMinFixed min_fixed
, AmlMaxFixed max_fixed
,
319 AmlDecode dec
, AmlISARanges isa_ranges
,
320 uint32_t addr_gran
, uint32_t addr_min
,
321 uint32_t addr_max
, uint32_t addr_trans
,
323 Aml
*aml_dword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
324 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
325 AmlReadAndWrite read_and_write
,
326 uint32_t addr_gran
, uint32_t addr_min
,
327 uint32_t addr_max
, uint32_t addr_trans
,
329 Aml
*aml_qword_memory(AmlDecode dec
, AmlMinFixed min_fixed
,
330 AmlMaxFixed max_fixed
, AmlCacheable cacheable
,
331 AmlReadAndWrite read_and_write
,
332 uint64_t addr_gran
, uint64_t addr_min
,
333 uint64_t addr_max
, uint64_t addr_trans
,
335 Aml
*aml_dma(AmlDmaType typ
, AmlDmaBusMaster bm
, AmlTransferSize sz
,
337 Aml
*aml_sleep(uint64_t msec
);
339 /* Block AML object primitives */
340 Aml
*aml_scope(const char *name_format
, ...) GCC_FMT_ATTR(1, 2);
341 Aml
*aml_device(const char *name_format
, ...) GCC_FMT_ATTR(1, 2);
342 Aml
*aml_method(const char *name
, int arg_count
, AmlSerializeFlag sflag
);
343 Aml
*aml_if(Aml
*predicate
);
345 Aml
*aml_while(Aml
*predicate
);
346 Aml
*aml_package(uint8_t num_elements
);
347 Aml
*aml_buffer(int buffer_size
, uint8_t *byte_list
);
348 Aml
*aml_resource_template(void);
349 Aml
*aml_field(const char *name
, AmlAccessType type
, AmlLockRule lock
,
351 Aml
*aml_mutex(const char *name
, uint8_t sync_level
);
352 Aml
*aml_acquire(Aml
*mutex
, uint16_t timeout
);
353 Aml
*aml_release(Aml
*mutex
);
354 Aml
*aml_alias(const char *source_object
, const char *alias_object
);
355 Aml
*aml_create_field(Aml
*srcbuf
, Aml
*bit_index
, Aml
*num_bits
,
357 Aml
*aml_create_dword_field(Aml
*srcbuf
, Aml
*index
, const char *name
);
358 Aml
*aml_create_qword_field(Aml
*srcbuf
, Aml
*index
, const char *name
);
359 Aml
*aml_varpackage(uint32_t num_elements
);
360 Aml
*aml_touuid(const char *uuid
);
361 Aml
*aml_unicode(const char *str
);
362 Aml
*aml_refof(Aml
*arg
);
363 Aml
*aml_derefof(Aml
*arg
);
364 Aml
*aml_sizeof(Aml
*arg
);
365 Aml
*aml_concatenate(Aml
*source1
, Aml
*source2
, Aml
*target
);
368 build_header(BIOSLinker
*linker
, GArray
*table_data
,
369 AcpiTableHeader
*h
, const char *sig
, int len
, uint8_t rev
,
370 const char *oem_id
, const char *oem_table_id
);
371 void *acpi_data_push(GArray
*table_data
, unsigned size
);
372 unsigned acpi_data_len(GArray
*table
);
373 void acpi_add_table(GArray
*table_offsets
, GArray
*table_data
);
374 void acpi_build_tables_init(AcpiBuildTables
*tables
);
375 void acpi_build_tables_cleanup(AcpiBuildTables
*tables
, bool mfre
);
377 build_rsdt(GArray
*table_data
, BIOSLinker
*linker
, GArray
*table_offsets
,
378 const char *oem_id
, const char *oem_table_id
);
381 build_append_named_dword(GArray
*array
, const char *name_format
, ...)
384 void build_srat_memory(AcpiSratMemoryAffinity
*numamem
, uint64_t base
,
385 uint64_t len
, int node
, MemoryAffinityFlags flags
);