aml-build.h 16 KB

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