master
h 517 lines 16.1 KB
Raw
1 #ifndef HW_ACPI_AML_BUILD_H
2 #define HW_ACPI_AML_BUILD_H
3
4 #include "hw/acpi/acpi-defs.h"
5 #include "hw/acpi/bios-linker-loader.h"
6 #include "hw/core/cpu.h"
7
8 #define ACPI_BUILD_APPNAME6 "BOCHS "
9 #define ACPI_BUILD_APPNAME8 "BXPC "
10
11 #define ACPI_BUILD_TABLE_FILE "etc/acpi/tables"
12 #define ACPI_BUILD_RSDP_FILE "etc/acpi/rsdp"
13 #define ACPI_BUILD_TPMLOG_FILE "etc/tpm/log"
14 #define ACPI_BUILD_LOADER_FILE "etc/table-loader"
15
16 #define AML_NOTIFY_METHOD "NTFY"
17
18 typedef enum {
19 AML_NO_OPCODE = 0,/* has only data */
20 AML_OPCODE, /* has opcode optionally followed by data */
21 AML_PACKAGE, /* has opcode and uses PkgLength for its length */
22 AML_EXT_PACKAGE, /* Same as AML_PACKAGE but also has 'ExOpPrefix' */
23 AML_BUFFER, /* data encoded as 'DefBuffer' */
24 AML_RES_TEMPLATE, /* encoded as ResourceTemplate macro */
25 } AmlBlockFlags;
26
27 struct Aml {
28 GArray *buf;
29
30 /*< private >*/
31 uint8_t op;
32 AmlBlockFlags block_flags;
33 };
34
35 typedef enum {
36 AML_COMPATIBILITY = 0,
37 AML_TYPEA = 1,
38 AML_TYPEB = 2,
39 AML_TYPEF = 3,
40 } AmlDmaType;
41
42 typedef enum {
43 AML_NOTBUSMASTER = 0,
44 AML_BUSMASTER = 1,
45 } AmlDmaBusMaster;
46
47 typedef enum {
48 AML_TRANSFER8 = 0,
49 AML_TRANSFER8_16 = 1,
50 AML_TRANSFER16 = 2,
51 } AmlTransferSize;
52
53 typedef enum {
54 AML_DECODE10 = 0,
55 AML_DECODE16 = 1,
56 } AmlIODecode;
57
58 typedef enum {
59 AML_ANY_ACC = 0,
60 AML_BYTE_ACC = 1,
61 AML_WORD_ACC = 2,
62 AML_DWORD_ACC = 3,
63 AML_QWORD_ACC = 4,
64 AML_BUFFER_ACC = 5,
65 } AmlAccessType;
66
67 typedef enum {
68 AML_NOLOCK = 0,
69 AML_LOCK = 1,
70 } AmlLockRule;
71
72 typedef enum {
73 AML_PRESERVE = 0,
74 AML_WRITE_AS_ONES = 1,
75 AML_WRITE_AS_ZEROS = 2,
76 } AmlUpdateRule;
77
78 typedef enum {
79 AML_AS_SYSTEM_MEMORY = 0X00,
80 AML_AS_SYSTEM_IO = 0X01,
81 AML_AS_PCI_CONFIG = 0X02,
82 AML_AS_EMBEDDED_CTRL = 0X03,
83 AML_AS_SMBUS = 0X04,
84 AML_AS_FFH = 0X7F,
85 } AmlAddressSpace;
86
87 typedef enum {
88 AML_SYSTEM_MEMORY = 0X00,
89 AML_SYSTEM_IO = 0X01,
90 AML_PCI_CONFIG = 0X02,
91 } AmlRegionSpace;
92
93 typedef enum {
94 AML_MEMORY_RANGE = 0,
95 AML_IO_RANGE = 1,
96 AML_BUS_NUMBER_RANGE = 2,
97 } AmlResourceType;
98
99 typedef enum {
100 AML_SUB_DECODE = 1 << 1,
101 AML_POS_DECODE = 0
102 } AmlDecode;
103
104 typedef enum {
105 AML_MAX_FIXED = 1 << 3,
106 AML_MAX_NOT_FIXED = 0,
107 } AmlMaxFixed;
108
109 typedef enum {
110 AML_MIN_FIXED = 1 << 2,
111 AML_MIN_NOT_FIXED = 0
112 } AmlMinFixed;
113
114 /*
115 * ACPI 1.0b: Table 6-26 I/O Resource Flag (Resource Type = 1) Definitions
116 * _RNG field definition
117 */
118 typedef enum {
119 AML_ISA_ONLY = 1,
120 AML_NON_ISA_ONLY = 2,
121 AML_ENTIRE_RANGE = 3,
122 } AmlISARanges;
123
124 /*
125 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
126 * _MEM field definition
127 */
128 typedef enum {
129 AML_NON_CACHEABLE = 0,
130 AML_CACHEABLE = 1,
131 AML_WRITE_COMBINING = 2,
132 AML_PREFETCHABLE = 3,
133 } AmlCacheable;
134
135 /*
136 * ACPI 1.0b: Table 6-25 Memory Resource Flag (Resource Type = 0) Definitions
137 * _RW field definition
138 */
139 typedef enum {
140 AML_READ_ONLY = 0,
141 AML_READ_WRITE = 1,
142 } AmlReadAndWrite;
143
144 /*
145 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
146 * Interrupt Vector Flags Bits[0] Consumer/Producer
147 */
148 typedef enum {
149 AML_CONSUMER_PRODUCER = 0,
150 AML_CONSUMER = 1,
151 } AmlConsumerAndProducer;
152
153 /*
154 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
155 * _HE field definition
156 */
157 typedef enum {
158 AML_LEVEL = 0,
159 AML_EDGE = 1,
160 } AmlLevelAndEdge;
161
162 /*
163 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
164 * _LL field definition
165 */
166 typedef enum {
167 AML_ACTIVE_HIGH = 0,
168 AML_ACTIVE_LOW = 1,
169 } AmlActiveHighAndLow;
170
171 /*
172 * ACPI 5.0: Table 6-187 Extended Interrupt Descriptor Definition
173 * _SHR field definition
174 */
175 typedef enum {
176 AML_EXCLUSIVE = 0,
177 AML_SHARED = 1,
178 AML_EXCLUSIVE_AND_WAKE = 2,
179 AML_SHARED_AND_WAKE = 3,
180 } AmlShared;
181
182 /* ACPI 1.0b: 16.2.5.2 Named Objects Encoding: MethodFlags */
183 typedef enum {
184 AML_NOTSERIALIZED = 0,
185 AML_SERIALIZED = 1,
186 } AmlSerializeFlag;
187
188 /*
189 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition
190 * GPIO Connection Type
191 */
192 typedef enum {
193 AML_INTERRUPT_CONNECTION = 0,
194 AML_IO_CONNECTION = 1,
195 } AmlGpioConnectionType;
196
197 /*
198 * ACPI 5.0: Table 6-189 GPIO Connection Descriptor Definition
199 * _PPI field definition
200 */
201 typedef enum {
202 AML_PULL_DEFAULT = 0,
203 AML_PULL_UP = 1,
204 AML_PULL_DOWN = 2,
205 AML_PULL_NONE = 3,
206 } AmlPinConfig;
207
208 typedef enum {
209 MEM_AFFINITY_NOFLAGS = 0,
210 MEM_AFFINITY_ENABLED = (1 << 0),
211 MEM_AFFINITY_HOTPLUGGABLE = (1 << 1),
212 MEM_AFFINITY_NON_VOLATILE = (1 << 2),
213 } MemoryAffinityFlags;
214
215 typedef
216 struct AcpiBuildTables {
217 GArray *table_data;
218 GArray *rsdp;
219 GArray *tcpalog;
220 GArray *vmgenid;
221 GArray *hardware_errors;
222 BIOSLinker *linker;
223 } AcpiBuildTables;
224
225 typedef
226 struct CrsRangeEntry {
227 uint64_t base;
228 uint64_t limit;
229 } CrsRangeEntry;
230
231 typedef
232 struct CrsRangeSet {
233 GPtrArray *io_ranges;
234 GPtrArray *mem_ranges;
235 GPtrArray *mem_64bit_ranges;
236 } CrsRangeSet;
237
238
239 /*
240 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors
241 * Serial Bus Type
242 */
243 #define AML_SERIAL_BUS_TYPE_I2C 1
244 #define AML_SERIAL_BUS_TYPE_SPI 2
245 #define AML_SERIAL_BUS_TYPE_UART 3
246
247 /*
248 * ACPI 5.0: 6.4.3.8.2 Serial Bus Connection Descriptors
249 * General Flags
250 */
251 /* Slave Mode */
252 #define AML_SERIAL_BUS_FLAG_MASTER_DEVICE (1 << 0)
253 /* Consumer/Producer */
254 #define AML_SERIAL_BUS_FLAG_CONSUME_ONLY (1 << 1)
255
256 #define ACPI_APEI_ERROR_DEVICE "GEDD"
257 /**
258 * init_aml_allocator:
259 *
260 * Called for initializing API allocator which allow to use
261 * AML API.
262 * Returns: toplevel container which accumulates all other
263 * AML elements for a table.
264 */
265 Aml *init_aml_allocator(void);
266
267 /**
268 * free_aml_allocator:
269 *
270 * Releases all elements used by AML API, frees associated memory
271 * and invalidates AML allocator. After this call @init_aml_allocator
272 * should be called again if AML API is to be used again.
273 */
274 void free_aml_allocator(void);
275
276 /**
277 * aml_append:
278 * @parent_ctx: context to which @child element is added
279 * @child: element that is copied into @parent_ctx context
280 *
281 * Joins Aml elements together and helps to construct AML tables
282 * Example of usage:
283 * Aml *table = aml_def_block("SSDT", ...);
284 * Aml *sb = aml_scope("\\_SB");
285 * Aml *dev = aml_device("PCI0");
286 *
287 * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03")));
288 * aml_append(sb, dev);
289 * aml_append(table, sb);
290 */
291 void aml_append(Aml *parent_ctx, Aml *child);
292
293 /* non block AML object primitives */
294 Aml *aml_name(const char *name_format, ...) G_GNUC_PRINTF(1, 2);
295 Aml *aml_name_decl(const char *name, Aml *val);
296 Aml *aml_debug(void);
297 Aml *aml_return(Aml *val);
298 Aml *aml_int(const uint64_t val);
299 Aml *aml_arg(int pos);
300 Aml *aml_to_integer(Aml *arg);
301 Aml *aml_to_hexstring(Aml *src, Aml *dst);
302 Aml *aml_to_buffer(Aml *src, Aml *dst);
303 Aml *aml_to_decimalstring(Aml *src, Aml *dst);
304 Aml *aml_store(Aml *val, Aml *target);
305 Aml *aml_and(Aml *arg1, Aml *arg2, Aml *dst);
306 Aml *aml_or(Aml *arg1, Aml *arg2, Aml *dst);
307 Aml *aml_land(Aml *arg1, Aml *arg2);
308 Aml *aml_lor(Aml *arg1, Aml *arg2);
309 Aml *aml_shiftleft(Aml *arg1, Aml *count);
310 Aml *aml_shiftright(Aml *arg1, Aml *count, Aml *dst);
311 Aml *aml_lless(Aml *arg1, Aml *arg2);
312 Aml *aml_add(Aml *arg1, Aml *arg2, Aml *dst);
313 Aml *aml_subtract(Aml *arg1, Aml *arg2, Aml *dst);
314 Aml *aml_increment(Aml *arg);
315 Aml *aml_decrement(Aml *arg);
316 Aml *aml_index(Aml *arg1, Aml *idx);
317 Aml *aml_notify(Aml *arg1, Aml *arg2);
318 Aml *aml_break(void);
319 Aml *aml_call0(const char *method);
320 Aml *aml_call1(const char *method, Aml *arg1);
321 Aml *aml_call2(const char *method, Aml *arg1, Aml *arg2);
322 Aml *aml_call3(const char *method, Aml *arg1, Aml *arg2, Aml *arg3);
323 Aml *aml_call4(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4);
324 Aml *aml_call5(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
325 Aml *arg5);
326 Aml *aml_call6(const char *method, Aml *arg1, Aml *arg2, Aml *arg3, Aml *arg4,
327 Aml *arg5, Aml *arg6);
328 Aml *aml_gpio_int(AmlConsumerAndProducer con_and_pro,
329 AmlLevelAndEdge edge_level,
330 AmlActiveHighAndLow active_level, AmlShared shared,
331 AmlPinConfig pin_config, uint16_t debounce_timeout,
332 const uint32_t pin_list[], uint32_t pin_count,
333 const char *resource_source_name,
334 const uint8_t *vendor_data, uint16_t vendor_data_len);
335 Aml *aml_memory32_fixed(uint32_t addr, uint32_t size,
336 AmlReadAndWrite read_and_write);
337 Aml *aml_interrupt(AmlConsumerAndProducer con_and_pro,
338 AmlLevelAndEdge level_and_edge,
339 AmlActiveHighAndLow high_and_low, AmlShared shared,
340 uint32_t *irq_list, uint8_t irq_count);
341 Aml *aml_io(AmlIODecode dec, uint16_t min_base, uint16_t max_base,
342 uint8_t aln, uint8_t len);
343 Aml *aml_operation_region(const char *name, AmlRegionSpace rs,
344 Aml *offset, uint32_t len);
345 Aml *aml_irq_no_flags(uint8_t irq);
346 Aml *aml_irq(uint8_t irq, AmlLevelAndEdge level_and_edge,
347 AmlActiveHighAndLow high_and_low, AmlShared shared);
348 Aml *aml_named_field(const char *name, unsigned length);
349 Aml *aml_reserved_field(unsigned length);
350 Aml *aml_local(int num);
351 Aml *aml_string(const char *name_format, ...) G_GNUC_PRINTF(1, 2);
352 Aml *aml_lnot(Aml *arg);
353 Aml *aml_equal(Aml *arg1, Aml *arg2);
354 Aml *aml_lgreater(Aml *arg1, Aml *arg2);
355 Aml *aml_lgreater_equal(Aml *arg1, Aml *arg2);
356 Aml *aml_processor(uint8_t proc_id, uint32_t pblk_addr, uint8_t pblk_len,
357 const char *name_format, ...) G_GNUC_PRINTF(4, 5);
358 Aml *aml_eisaid(const char *str);
359 Aml *aml_word_bus_number(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
360 AmlDecode dec, uint16_t addr_gran,
361 uint16_t addr_min, uint16_t addr_max,
362 uint16_t addr_trans, uint16_t len);
363 Aml *aml_word_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
364 AmlDecode dec, AmlISARanges isa_ranges,
365 uint16_t addr_gran, uint16_t addr_min,
366 uint16_t addr_max, uint16_t addr_trans,
367 uint16_t len);
368 Aml *aml_dword_io(AmlMinFixed min_fixed, AmlMaxFixed max_fixed,
369 AmlDecode dec, AmlISARanges isa_ranges,
370 uint32_t addr_gran, uint32_t addr_min,
371 uint32_t addr_max, uint32_t addr_trans,
372 uint32_t len);
373 Aml *aml_dword_memory(AmlDecode dec, AmlMinFixed min_fixed,
374 AmlMaxFixed max_fixed, AmlCacheable cacheable,
375 AmlReadAndWrite read_and_write,
376 uint32_t addr_gran, uint32_t addr_min,
377 uint32_t addr_max, uint32_t addr_trans,
378 uint32_t len);
379 Aml *aml_qword_memory(AmlDecode dec, AmlMinFixed min_fixed,
380 AmlMaxFixed max_fixed, AmlCacheable cacheable,
381 AmlReadAndWrite read_and_write,
382 uint64_t addr_gran, uint64_t addr_min,
383 uint64_t addr_max, uint64_t addr_trans,
384 uint64_t len);
385 Aml *aml_dma(AmlDmaType typ, AmlDmaBusMaster bm, AmlTransferSize sz,
386 uint8_t channel);
387 Aml *aml_sleep(uint64_t msec);
388 Aml *aml_i2c_serial_bus_device(uint16_t address, const char *resource_source);
389 Aml *aml_error_device(void);
390
391 /* Block AML object primitives */
392 Aml *aml_scope(const char *name_format, ...) G_GNUC_PRINTF(1, 2);
393 Aml *aml_device(const char *name_format, ...) G_GNUC_PRINTF(1, 2);
394 Aml *aml_method(const char *name, int arg_count, AmlSerializeFlag sflag);
395 Aml *aml_if(Aml *predicate);
396 Aml *aml_else(void);
397 Aml *aml_while(Aml *predicate);
398 Aml *aml_package(uint8_t num_elements);
399 Aml *aml_buffer(int buffer_size, uint8_t *byte_list);
400 Aml *aml_resource_template(void);
401 Aml *aml_field(const char *name, AmlAccessType type, AmlLockRule lock,
402 AmlUpdateRule rule);
403 Aml *aml_mutex(const char *name, uint8_t sync_level);
404 Aml *aml_acquire(Aml *mutex, uint16_t timeout);
405 Aml *aml_release(Aml *mutex);
406 Aml *aml_alias(const char *source_object, const char *alias_object);
407 Aml *aml_create_field(Aml *srcbuf, Aml *bit_index, Aml *num_bits,
408 const char *name);
409 Aml *aml_create_dword_field(Aml *srcbuf, Aml *index, const char *name);
410 Aml *aml_create_qword_field(Aml *srcbuf, Aml *index, const char *name);
411 Aml *aml_varpackage(uint32_t num_elements);
412 Aml *aml_touuid(const char *uuid);
413 Aml *aml_unicode(const char *str);
414 Aml *aml_refof(Aml *arg);
415 Aml *aml_derefof(Aml *arg);
416 Aml *aml_sizeof(Aml *arg);
417 Aml *aml_concatenate(Aml *source1, Aml *source2, Aml *target);
418 Aml *aml_object_type(Aml *object);
419
420 void build_append_int_noprefix(GArray *table, uint64_t value, int size);
421
422 typedef struct AcpiTable {
423 const char *sig;
424 const uint8_t rev;
425 const char *oem_id;
426 const char *oem_table_id;
427 /* private vars tracking table state */
428 GArray *array;
429 unsigned table_offset;
430 } AcpiTable;
431
432 /**
433 * acpi_table_begin:
434 * initializes table header and keeps track of
435 * table data/offsets
436 * @desc: ACPI table descriptor
437 * @array: blob where the ACPI table will be composed/stored.
438 */
439 void acpi_table_begin(AcpiTable *desc, GArray *array);
440
441 /**
442 * acpi_table_end:
443 * sets actual table length and tells bios loader
444 * where table is for the later initialization on
445 * guest side.
446 * @linker: reference to BIOSLinker object to use for the table
447 * @table: ACPI table descriptor that was used with @acpi_table_begin
448 * counterpart
449 */
450 void acpi_table_end(BIOSLinker *linker, AcpiTable *table);
451
452 void *acpi_data_push(GArray *table_data, unsigned size);
453 unsigned acpi_data_len(GArray *table);
454 void acpi_add_table(GArray *table_offsets, GArray *table_data);
455 void acpi_build_tables_init(AcpiBuildTables *tables);
456 void acpi_build_tables_cleanup(AcpiBuildTables *tables, bool mfre);
457 void
458 build_rsdp(GArray *tbl, BIOSLinker *linker, AcpiRsdpData *rsdp_data);
459 void
460 build_rsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
461 const char *oem_id, const char *oem_table_id);
462 void
463 build_xsdt(GArray *table_data, BIOSLinker *linker, GArray *table_offsets,
464 const char *oem_id, const char *oem_table_id);
465
466 int
467 build_append_named_dword(GArray *array, const char *name_format, ...)
468 G_GNUC_PRINTF(2, 3);
469
470 void build_append_gas(GArray *table, AmlAddressSpace as,
471 uint8_t bit_width, uint8_t bit_offset,
472 uint8_t access_width, uint64_t address);
473
474 static inline void
475 build_append_gas_from_struct(GArray *table, const struct AcpiGenericAddress *s)
476 {
477 build_append_gas(table, s->space_id, s->bit_width, s->bit_offset,
478 s->access_width, s->address);
479 }
480
481 void crs_range_insert(GPtrArray *ranges, uint64_t base, uint64_t limit);
482 void crs_replace_with_free_ranges(GPtrArray *ranges,
483 uint64_t start, uint64_t end);
484 void crs_range_set_init(CrsRangeSet *range_set);
485 void crs_range_set_free(CrsRangeSet *range_set);
486
487 Aml *build_crs(PCIHostState *host, CrsRangeSet *range_set, uint32_t io_offset,
488 uint32_t mmio32_offset, uint64_t mmio64_offset,
489 uint16_t bus_nr_offset);
490
491 void build_srat_memory(GArray *table_data, uint64_t base,
492 uint64_t len, int node, MemoryAffinityFlags flags);
493
494 void build_srat_pci_generic_initiator(GArray *table_data, uint32_t node,
495 uint16_t segment, uint8_t bus,
496 uint8_t devfn);
497
498 void build_srat_acpi_generic_port(GArray *table_data, uint32_t node,
499 const char *hid, uint32_t uid);
500
501 void build_slit(GArray *table_data, BIOSLinker *linker, MachineState *ms,
502 const char *oem_id, const char *oem_table_id);
503
504 void build_pptt(GArray *table_data, BIOSLinker *linker, MachineState *ms,
505 const char *oem_id, const char *oem_table_id,
506 int num_caches, CPUCoreCaches *caches);
507
508 void build_fadt(GArray *tbl, BIOSLinker *linker, const AcpiFadtData *f,
509 const char *oem_id, const char *oem_table_id);
510
511 void build_tpm2(GArray *table_data, BIOSLinker *linker, GArray *tcpalog,
512 const char *oem_id, const char *oem_table_id);
513
514 void build_spcr(GArray *table_data, BIOSLinker *linker,
515 const AcpiSpcrData *f, const uint8_t rev,
516 const char *oem_id, const char *oem_table_id, const char *name);
517 #endif