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 "dev/gen_device_info.h"
32#include "util/hash_table.h"
33#include "util/bitset.h"
34
35#include "drm-uapi/i915_drm.h"
36
37#ifdef __cplusplus
38extern "C" {
39#endif
40
41struct gen_spec;
42struct gen_group;
43struct gen_field;
44union gen_field_value;
45
46#define I915_ENGINE_CLASS_TO_MASK(x) BITSET_BIT(x)
47
48static inline uint32_t gen_make_gen(uint32_t major, uint32_t minor)
49{
50   return (major << 8) | minor;
51}
52
53struct gen_group *gen_spec_find_struct(struct gen_spec *spec, const char *name);
54struct gen_spec *gen_spec_load(const struct gen_device_info *devinfo);
55struct gen_spec *gen_spec_load_from_path(const struct gen_device_info *devinfo,
56                                         const char *path);
57void gen_spec_destroy(struct gen_spec *spec);
58uint32_t gen_spec_get_gen(struct gen_spec *spec);
59struct gen_group *gen_spec_find_instruction(struct gen_spec *spec,
60                                            enum drm_i915_gem_engine_class engine,
61                                            const uint32_t *p);
62struct gen_group *gen_spec_find_register(struct gen_spec *spec, uint32_t offset);
63struct gen_group *gen_spec_find_register_by_name(struct gen_spec *spec, const char *name);
64struct gen_enum *gen_spec_find_enum(struct gen_spec *spec, const char *name);
65
66int gen_group_get_length(struct gen_group *group, const uint32_t *p);
67const char *gen_group_get_name(struct gen_group *group);
68uint32_t gen_group_get_opcode(struct gen_group *group);
69struct gen_field *gen_group_find_field(struct gen_group *group, const char *name);
70struct gen_enum *gen_spec_find_enum(struct gen_spec *spec, const char *name);
71
72bool gen_field_is_header(struct gen_field *field);
73
74struct gen_field_iterator {
75   struct gen_group *group;
76   char name[128];
77   char value[128];
78   uint64_t raw_value;
79   struct gen_group *struct_desc;
80   const uint32_t *p;
81   int p_bit; /**< bit offset into p */
82   const uint32_t *p_end;
83   int start_bit; /**< current field starts at this bit offset into p */
84   int end_bit; /**< current field ends at this bit offset into p */
85
86   int group_iter;
87
88   struct gen_field *field;
89   bool print_colors;
90};
91
92struct gen_spec {
93   uint32_t gen;
94
95   struct hash_table *commands;
96   struct hash_table *structs;
97   struct hash_table *registers_by_name;
98   struct hash_table *registers_by_offset;
99   struct hash_table *enums;
100
101   struct hash_table *access_cache;
102};
103
104struct gen_group {
105   struct gen_spec *spec;
106   char *name;
107
108   struct gen_field *fields; /* linked list of fields */
109   struct gen_field *dword_length_field; /* <instruction> specific */
110
111   uint32_t dw_length;
112   uint32_t engine_mask; /* <instruction> specific */
113   uint32_t bias; /* <instruction> specific */
114   uint32_t group_offset, group_count;
115   uint32_t group_size;
116   bool variable; /* <group> specific */
117   bool fixed_length; /* True for <struct> & <register> */
118
119   struct gen_group *parent;
120   struct gen_group *next;
121
122   uint32_t opcode_mask;
123   uint32_t opcode;
124
125   uint32_t register_offset; /* <register> specific */
126};
127
128struct gen_value {
129   char *name;
130   uint64_t value;
131};
132
133struct gen_enum {
134   char *name;
135   int nvalues;
136   struct gen_value **values;
137};
138
139struct gen_type {
140   enum {
141      GEN_TYPE_UNKNOWN,
142      GEN_TYPE_INT,
143      GEN_TYPE_UINT,
144      GEN_TYPE_BOOL,
145      GEN_TYPE_FLOAT,
146      GEN_TYPE_ADDRESS,
147      GEN_TYPE_OFFSET,
148      GEN_TYPE_STRUCT,
149      GEN_TYPE_UFIXED,
150      GEN_TYPE_SFIXED,
151      GEN_TYPE_MBO,
152      GEN_TYPE_ENUM
153   } kind;
154
155   /* Struct definition for  GEN_TYPE_STRUCT */
156   union {
157      struct gen_group *gen_struct;
158      struct gen_enum *gen_enum;
159      struct {
160         /* Integer and fractional sizes for GEN_TYPE_UFIXED and GEN_TYPE_SFIXED */
161         int i, f;
162      };
163   };
164};
165
166union gen_field_value {
167   bool b32;
168   float f32;
169   uint64_t u64;
170   int64_t i64;
171};
172
173struct gen_field {
174   struct gen_group *parent;
175   struct gen_field *next;
176
177   char *name;
178   int start, end;
179   struct gen_type type;
180   bool has_default;
181   uint32_t default_value;
182
183   struct gen_enum inline_enum;
184};
185
186void gen_field_iterator_init(struct gen_field_iterator *iter,
187                             struct gen_group *group,
188                             const uint32_t *p, int p_bit,
189                             bool print_colors);
190
191bool gen_field_iterator_next(struct gen_field_iterator *iter);
192
193void gen_print_group(FILE *out,
194                     struct gen_group *group,
195                     uint64_t offset, const uint32_t *p, int p_bit,
196                     bool color);
197
198enum gen_batch_decode_flags {
199   /** Print in color! */
200   GEN_BATCH_DECODE_IN_COLOR  = (1 << 0),
201   /** Print everything, not just headers */
202   GEN_BATCH_DECODE_FULL      = (1 << 1),
203   /** Print offsets along with the batch */
204   GEN_BATCH_DECODE_OFFSETS   = (1 << 2),
205   /** Guess when a value is a float and print it as such */
206   GEN_BATCH_DECODE_FLOATS    = (1 << 3),
207};
208
209struct gen_batch_decode_bo {
210   uint64_t addr;
211   uint32_t size;
212   const void *map;
213};
214
215struct gen_batch_decode_ctx {
216   /**
217    * Return information about the buffer containing the given address.
218    *
219    * If the given address is inside a buffer, the map pointer should be
220    * offset accordingly so it points at the data corresponding to address.
221    */
222   struct gen_batch_decode_bo (*get_bo)(void *user_data, bool ppgtt, uint64_t address);
223   unsigned (*get_state_size)(void *user_data,
224                              uint32_t offset_from_dynamic_state_base_addr);
225   void *user_data;
226
227   FILE *fp;
228   struct gen_spec *spec;
229   enum gen_batch_decode_flags flags;
230
231   struct gen_disasm *disasm;
232
233   uint64_t surface_base;
234   uint64_t dynamic_base;
235   uint64_t instruction_base;
236
237   int max_vbo_decoded_lines;
238
239   enum drm_i915_gem_engine_class engine;
240
241   int n_batch_buffer_start;
242};
243
244void gen_batch_decode_ctx_init(struct gen_batch_decode_ctx *ctx,
245                               const struct gen_device_info *devinfo,
246                               FILE *fp, enum gen_batch_decode_flags flags,
247                               const char *xml_path,
248                               struct gen_batch_decode_bo (*get_bo)(void *,
249                                                                    bool,
250                                                                    uint64_t),
251
252                               unsigned (*get_state_size)(void *, uint32_t),
253                               void *user_data);
254void gen_batch_decode_ctx_finish(struct gen_batch_decode_ctx *ctx);
255
256
257void gen_print_batch(struct gen_batch_decode_ctx *ctx,
258                     const uint32_t *batch, uint32_t batch_size,
259                     uint64_t batch_addr, bool from_ring);
260
261#ifdef __cplusplus
262}
263#endif
264
265
266#endif /* GEN_DECODER_H */
267