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 260f2707e4SIgor Mammedov /** 270f2707e4SIgor Mammedov * init_aml_allocator: 280f2707e4SIgor Mammedov * 290f2707e4SIgor Mammedov * Called for initializing API allocator which allow to use 300f2707e4SIgor Mammedov * AML API. 310f2707e4SIgor Mammedov * Returns: toplevel container which accumulates all other 320f2707e4SIgor Mammedov * AML elements for a table. 330f2707e4SIgor Mammedov */ 340f2707e4SIgor Mammedov Aml *init_aml_allocator(void); 350f2707e4SIgor Mammedov 360f2707e4SIgor Mammedov /** 370f2707e4SIgor Mammedov * free_aml_allocator: 380f2707e4SIgor Mammedov * 390f2707e4SIgor Mammedov * Releases all elements used by AML API, frees associated memory 400f2707e4SIgor Mammedov * and invalidates AML allocator. After this call @init_aml_allocator 410f2707e4SIgor Mammedov * should be called again if AML API is to be used again. 420f2707e4SIgor Mammedov */ 430f2707e4SIgor Mammedov void free_aml_allocator(void); 440f2707e4SIgor Mammedov 450f2707e4SIgor Mammedov /** 460f2707e4SIgor Mammedov * aml_append: 470f2707e4SIgor Mammedov * @parent_ctx: context to which @child element is added 480f2707e4SIgor Mammedov * @child: element that is copied into @parent_ctx context 490f2707e4SIgor Mammedov * 500f2707e4SIgor Mammedov * Joins Aml elements together and helps to construct AML tables 510f2707e4SIgor Mammedov * Examle of usage: 520f2707e4SIgor Mammedov * Aml *table = aml_def_block("SSDT", ...); 530f2707e4SIgor Mammedov * Aml *sb = aml_scope("\_SB"); 540f2707e4SIgor Mammedov * Aml *dev = aml_device("PCI0"); 550f2707e4SIgor Mammedov * 560f2707e4SIgor Mammedov * aml_append(dev, aml_name_decl("HID", aml_eisaid("PNP0A03"))); 570f2707e4SIgor Mammedov * aml_append(sb, dev); 580f2707e4SIgor Mammedov * aml_append(table, sb); 590f2707e4SIgor Mammedov */ 600f2707e4SIgor Mammedov void aml_append(Aml *parent_ctx, Aml *child); 610f2707e4SIgor Mammedov 623c054bd5SIgor Mammedov /* non block AML object primitives */ 633c054bd5SIgor Mammedov Aml *aml_name(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 643c054bd5SIgor Mammedov Aml *aml_name_decl(const char *name, Aml *val); 65*295a515dSIgor Mammedov Aml *aml_int(const uint64_t val); 663c054bd5SIgor Mammedov 672ef7c27bSIgor Mammedov /* Block AML object primitives */ 682ef7c27bSIgor Mammedov Aml *aml_scope(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 69be06ebd0SIgor Mammedov Aml *aml_device(const char *name_format, ...) GCC_FMT_ATTR(1, 2); 70ea2407d7SIgor Mammedov Aml *aml_method(const char *name, int arg_count); 7132acac9eSIgor Mammedov Aml *aml_if(Aml *predicate); 722ef7c27bSIgor Mammedov 730f2707e4SIgor Mammedov /* other helpers */ 7419934e0eSIgor Mammedov GArray *build_alloc_array(void); 7519934e0eSIgor Mammedov void build_free_array(GArray *array); 7619934e0eSIgor Mammedov void build_prepend_byte(GArray *array, uint8_t val); 7719934e0eSIgor Mammedov void build_append_byte(GArray *array, uint8_t val); 7819934e0eSIgor Mammedov void build_append_array(GArray *array, GArray *val); 7919934e0eSIgor Mammedov 8019934e0eSIgor Mammedov void GCC_FMT_ATTR(2, 3) 81eae8bdedSIgor Mammedov build_append_namestring(GArray *array, const char *format, ...); 8219934e0eSIgor Mammedov 83661875e9SIgor Mammedov void build_prepend_package_length(GArray *package); 84661875e9SIgor Mammedov void build_package(GArray *package, uint8_t op); 85*295a515dSIgor Mammedov void build_append_int(GArray *table, uint64_t value); 8619934e0eSIgor Mammedov void build_extop_package(GArray *package, uint8_t op); 8719934e0eSIgor Mammedov 8819934e0eSIgor Mammedov #endif 89