1 /* 2 * Copyright 2016 Intel Corporation 3 * 4 * Permission is hereby granted, free of charge, to any person obtaining a 5 * copy of this software and associated documentation files (the "Software"), 6 * to deal in the Software without restriction, including without limitation 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 * and/or sell copies of the Software, and to permit persons to whom the 9 * Software is furnished to do so, subject to the following conditions: 10 * 11 * The above copyright notice and this permission notice (including the next 12 * paragraph) shall be included in all copies or substantial portions of the 13 * Software. 14 * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS 21 * IN THE SOFTWARE. 22 */ 23 24 #ifndef GEN_DECODER_H 25 #define GEN_DECODER_H 26 27 #include <stdint.h> 28 #include <stdbool.h> 29 #include <stdio.h> 30 31 #include "common/gen_device_info.h" 32 #include "util/hash_table.h" 33 34 #ifdef __cplusplus 35 extern "C" { 36 #endif 37 38 struct gen_spec; 39 struct gen_group; 40 struct gen_field; 41 union gen_field_value; 42 43 static inline uint32_t gen_make_gen(uint32_t major, uint32_t minor) 44 { 45 return (major << 8) | minor; 46 } 47 48 struct gen_group *gen_spec_find_struct(struct gen_spec *spec, const char *name); 49 struct gen_spec *gen_spec_load(const struct gen_device_info *devinfo); 50 struct gen_spec *gen_spec_load_from_path(const struct gen_device_info *devinfo, 51 const char *path); 52 void gen_spec_destroy(struct gen_spec *spec); 53 uint32_t gen_spec_get_gen(struct gen_spec *spec); 54 struct gen_group *gen_spec_find_instruction(struct gen_spec *spec, const uint32_t *p); 55 struct gen_group *gen_spec_find_register(struct gen_spec *spec, uint32_t offset); 56 struct gen_group *gen_spec_find_register_by_name(struct gen_spec *spec, const char *name); 57 struct gen_enum *gen_spec_find_enum(struct gen_spec *spec, const char *name); 58 59 int gen_group_get_length(struct gen_group *group, const uint32_t *p); 60 const char *gen_group_get_name(struct gen_group *group); 61 uint32_t gen_group_get_opcode(struct gen_group *group); 62 struct gen_field *gen_group_find_field(struct gen_group *group, const char *name); 63 struct gen_enum *gen_spec_find_enum(struct gen_spec *spec, const char *name); 64 65 bool gen_field_is_header(struct gen_field *field); 66 67 struct gen_field_iterator { 68 struct gen_group *group; 69 char name[128]; 70 char value[128]; 71 uint64_t raw_value; 72 struct gen_group *struct_desc; 73 const uint32_t *p; 74 int p_bit; /**< bit offset into p */ 75 const uint32_t *p_end; 76 int bit; /**< current field starts at this bit offset into p */ 77 78 int group_iter; 79 80 struct gen_field *field; 81 bool print_colors; 82 }; 83 84 struct gen_spec { 85 uint32_t gen; 86 87 struct hash_table *commands; 88 struct hash_table *structs; 89 struct hash_table *registers_by_name; 90 struct hash_table *registers_by_offset; 91 struct hash_table *enums; 92 93 struct hash_table *access_cache; 94 }; 95 96 struct gen_group { 97 struct gen_spec *spec; 98 char *name; 99 100 struct gen_field *fields; /* linked list of fields */ 101 102 uint32_t dw_length; 103 uint32_t group_offset, group_count; 104 uint32_t group_size; 105 bool variable; 106 107 struct gen_group *parent; 108 struct gen_group *next; 109 110 uint32_t opcode_mask; 111 uint32_t opcode; 112 113 /* Register specific */ 114 uint32_t register_offset; 115 }; 116 117 struct gen_value { 118 char *name; 119 uint64_t value; 120 }; 121 122 struct gen_enum { 123 char *name; 124 int nvalues; 125 struct gen_value **values; 126 }; 127 128 struct gen_type { 129 enum { 130 GEN_TYPE_UNKNOWN, 131 GEN_TYPE_INT, 132 GEN_TYPE_UINT, 133 GEN_TYPE_BOOL, 134 GEN_TYPE_FLOAT, 135 GEN_TYPE_ADDRESS, 136 GEN_TYPE_OFFSET, 137 GEN_TYPE_STRUCT, 138 GEN_TYPE_UFIXED, 139 GEN_TYPE_SFIXED, 140 GEN_TYPE_MBO, 141 GEN_TYPE_ENUM 142 } kind; 143 144 /* Struct definition for GEN_TYPE_STRUCT */ 145 union { 146 struct gen_group *gen_struct; 147 struct gen_enum *gen_enum; 148 struct { 149 /* Integer and fractional sizes for GEN_TYPE_UFIXED and GEN_TYPE_SFIXED */ 150 int i, f; 151 }; 152 }; 153 }; 154 155 union gen_field_value { 156 bool b32; 157 float f32; 158 uint64_t u64; 159 int64_t i64; 160 }; 161 162 struct gen_field { 163 struct gen_group *parent; 164 struct gen_field *next; 165 166 char *name; 167 int start, end; 168 struct gen_type type; 169 bool has_default; 170 uint32_t default_value; 171 172 struct gen_enum inline_enum; 173 }; 174 175 void gen_field_iterator_init(struct gen_field_iterator *iter, 176 struct gen_group *group, 177 const uint32_t *p, int p_bit, 178 bool print_colors); 179 180 bool gen_field_iterator_next(struct gen_field_iterator *iter); 181 182 void gen_print_group(FILE *out, 183 struct gen_group *group, 184 uint64_t offset, const uint32_t *p, int p_bit, 185 bool color); 186 187 enum gen_batch_decode_flags { 188 /** Print in color! */ 189 GEN_BATCH_DECODE_IN_COLOR = (1 << 0), 190 /** Print everything, not just headers */ 191 GEN_BATCH_DECODE_FULL = (1 << 1), 192 /** Print offsets along with the batch */ 193 GEN_BATCH_DECODE_OFFSETS = (1 << 2), 194 /** Guess when a value is a float and print it as such */ 195 GEN_BATCH_DECODE_FLOATS = (1 << 3), 196 }; 197 198 struct gen_batch_decode_bo { 199 uint64_t addr; 200 uint32_t size; 201 const void *map; 202 }; 203 204 struct gen_disasm *disasm; 205 206 struct gen_batch_decode_ctx { 207 struct gen_batch_decode_bo (*get_bo)(void *user_data, 208 uint64_t base_address); 209 void *user_data; 210 211 FILE *fp; 212 struct gen_spec *spec; 213 enum gen_batch_decode_flags flags; 214 215 struct gen_disasm *disasm; 216 217 struct gen_batch_decode_bo surface_base; 218 struct gen_batch_decode_bo dynamic_base; 219 struct gen_batch_decode_bo instruction_base; 220 }; 221 222 void gen_batch_decode_ctx_init(struct gen_batch_decode_ctx *ctx, 223 const struct gen_device_info *devinfo, 224 FILE *fp, enum gen_batch_decode_flags flags, 225 const char *xml_path, 226 struct gen_batch_decode_bo (*get_bo)(void *, 227 uint64_t), 228 void *user_data); 229 void gen_batch_decode_ctx_finish(struct gen_batch_decode_ctx *ctx); 230 231 232 void gen_print_batch(struct gen_batch_decode_ctx *ctx, 233 const uint32_t *batch, uint32_t batch_size, 234 uint64_t batch_addr); 235 236 #ifdef __cplusplus 237 } 238 #endif 239 240 241 #endif /* GEN_DECODER_H */ 242