intel/perf: extract register configuration
[mesa.git] / src / intel / perf / gen_perf.h
1 /*
2 * Copyright © 2018 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_PERF_H
25 #define GEN_PERF_H
26
27 #include <stdio.h>
28 #include <stdint.h>
29 #include <string.h>
30
31 #if defined(MAJOR_IN_SYSMACROS)
32 #include <sys/sysmacros.h>
33 #elif defined(MAJOR_IN_MKDEV)
34 #include <sys/mkdev.h>
35 #endif
36
37 #include "util/hash_table.h"
38 #include "compiler/glsl/list.h"
39 #include "util/ralloc.h"
40
41 struct gen_device_info;
42
43 struct gen_perf_config;
44 struct gen_perf_query_info;
45
46 #define GEN7_RPSTAT1 0xA01C
47 #define GEN7_RPSTAT1_CURR_GT_FREQ_SHIFT 7
48 #define GEN7_RPSTAT1_CURR_GT_FREQ_MASK INTEL_MASK(13, 7)
49 #define GEN7_RPSTAT1_PREV_GT_FREQ_SHIFT 0
50 #define GEN7_RPSTAT1_PREV_GT_FREQ_MASK INTEL_MASK(6, 0)
51
52 #define GEN9_RPSTAT0 0xA01C
53 #define GEN9_RPSTAT0_CURR_GT_FREQ_SHIFT 23
54 #define GEN9_RPSTAT0_CURR_GT_FREQ_MASK INTEL_MASK(31, 23)
55 #define GEN9_RPSTAT0_PREV_GT_FREQ_SHIFT 0
56 #define GEN9_RPSTAT0_PREV_GT_FREQ_MASK INTEL_MASK(8, 0)
57
58 enum gen_perf_counter_type {
59 GEN_PERF_COUNTER_TYPE_EVENT,
60 GEN_PERF_COUNTER_TYPE_DURATION_NORM,
61 GEN_PERF_COUNTER_TYPE_DURATION_RAW,
62 GEN_PERF_COUNTER_TYPE_THROUGHPUT,
63 GEN_PERF_COUNTER_TYPE_RAW,
64 GEN_PERF_COUNTER_TYPE_TIMESTAMP,
65 };
66
67 enum gen_perf_counter_data_type {
68 GEN_PERF_COUNTER_DATA_TYPE_BOOL32,
69 GEN_PERF_COUNTER_DATA_TYPE_UINT32,
70 GEN_PERF_COUNTER_DATA_TYPE_UINT64,
71 GEN_PERF_COUNTER_DATA_TYPE_FLOAT,
72 GEN_PERF_COUNTER_DATA_TYPE_DOUBLE,
73 };
74
75 struct gen_pipeline_stat {
76 uint32_t reg;
77 uint32_t numerator;
78 uint32_t denominator;
79 };
80
81 /*
82 * The largest OA formats we can use include:
83 * For Haswell:
84 * 1 timestamp, 45 A counters, 8 B counters and 8 C counters.
85 * For Gen8+
86 * 1 timestamp, 1 clock, 36 A counters, 8 B counters and 8 C counters
87 */
88 #define MAX_OA_REPORT_COUNTERS 62
89
90 #define IA_VERTICES_COUNT 0x2310
91 #define IA_PRIMITIVES_COUNT 0x2318
92 #define VS_INVOCATION_COUNT 0x2320
93 #define HS_INVOCATION_COUNT 0x2300
94 #define DS_INVOCATION_COUNT 0x2308
95 #define GS_INVOCATION_COUNT 0x2328
96 #define GS_PRIMITIVES_COUNT 0x2330
97 #define CL_INVOCATION_COUNT 0x2338
98 #define CL_PRIMITIVES_COUNT 0x2340
99 #define PS_INVOCATION_COUNT 0x2348
100 #define CS_INVOCATION_COUNT 0x2290
101 #define PS_DEPTH_COUNT 0x2350
102
103 /*
104 * When currently allocate only one page for pipeline statistics queries. Here
105 * we derived the maximum number of counters for that amount.
106 */
107 #define STATS_BO_SIZE 4096
108 #define STATS_BO_END_OFFSET_BYTES (STATS_BO_SIZE / 2)
109 #define MAX_STAT_COUNTERS (STATS_BO_END_OFFSET_BYTES / 8)
110
111 #define I915_PERF_OA_SAMPLE_SIZE (8 + /* drm_i915_perf_record_header */ \
112 256) /* OA counter report */
113
114 struct gen_perf_query_result {
115 /**
116 * Storage for the final accumulated OA counters.
117 */
118 uint64_t accumulator[MAX_OA_REPORT_COUNTERS];
119
120 /**
121 * Hw ID used by the context on which the query was running.
122 */
123 uint32_t hw_id;
124
125 /**
126 * Number of reports accumulated to produce the results.
127 */
128 uint32_t reports_accumulated;
129
130 /**
131 * Frequency in the slices of the GT at the begin and end of the
132 * query.
133 */
134 uint64_t slice_frequency[2];
135
136 /**
137 * Frequency in the unslice of the GT at the begin and end of the
138 * query.
139 */
140 uint64_t unslice_frequency[2];
141 };
142
143 struct gen_perf_query_counter {
144 const char *name;
145 const char *desc;
146 enum gen_perf_counter_type type;
147 enum gen_perf_counter_data_type data_type;
148 uint64_t raw_max;
149 size_t offset;
150
151 union {
152 uint64_t (*oa_counter_read_uint64)(struct gen_perf_config *perf,
153 const struct gen_perf_query_info *query,
154 const uint64_t *accumulator);
155 float (*oa_counter_read_float)(struct gen_perf_config *perf,
156 const struct gen_perf_query_info *query,
157 const uint64_t *accumulator);
158 struct gen_pipeline_stat pipeline_stat;
159 };
160 };
161
162 struct gen_perf_query_register_prog {
163 uint32_t reg;
164 uint32_t val;
165 };
166
167 /* Register programming for a given query */
168 struct gen_perf_configuration {
169 struct gen_perf_query_register_prog *flex_regs;
170 uint32_t n_flex_regs;
171
172 struct gen_perf_query_register_prog *mux_regs;
173 uint32_t n_mux_regs;
174
175 struct gen_perf_query_register_prog *b_counter_regs;
176 uint32_t n_b_counter_regs;
177 };
178
179 struct gen_perf_query_info {
180 enum gen_perf_query_type {
181 GEN_PERF_QUERY_TYPE_OA,
182 GEN_PERF_QUERY_TYPE_RAW,
183 GEN_PERF_QUERY_TYPE_PIPELINE,
184 } kind;
185 const char *name;
186 const char *guid;
187 struct gen_perf_query_counter *counters;
188 int n_counters;
189 int max_counters;
190 size_t data_size;
191
192 /* OA specific */
193 uint64_t oa_metrics_set_id;
194 int oa_format;
195
196 /* For indexing into the accumulator[] ... */
197 int gpu_time_offset;
198 int gpu_clock_offset;
199 int a_offset;
200 int b_offset;
201 int c_offset;
202
203 struct gen_perf_configuration config;
204 };
205
206 struct gen_perf_config {
207 struct gen_perf_query_info *queries;
208 int n_queries;
209
210 /* Variables referenced in the XML meta data for OA performance
211 * counters, e.g in the normalization equations.
212 *
213 * All uint64_t for consistent operand types in generated code
214 */
215 struct {
216 uint64_t timestamp_frequency; /** $GpuTimestampFrequency */
217 uint64_t n_eus; /** $EuCoresTotalCount */
218 uint64_t n_eu_slices; /** $EuSlicesTotalCount */
219 uint64_t n_eu_sub_slices; /** $EuSubslicesTotalCount */
220 uint64_t eu_threads_count; /** $EuThreadsCount */
221 uint64_t slice_mask; /** $SliceMask */
222 uint64_t subslice_mask; /** $SubsliceMask */
223 uint64_t gt_min_freq; /** $GpuMinFrequency */
224 uint64_t gt_max_freq; /** $GpuMaxFrequency */
225 uint64_t revision; /** $SkuRevisionId */
226 } sys_vars;
227
228 /* OA metric sets, indexed by GUID, as know by Mesa at build time, to
229 * cross-reference with the GUIDs of configs advertised by the kernel at
230 * runtime
231 */
232 struct hash_table *oa_metrics_table;
233
234 /* Location of the device's sysfs entry. */
235 char sysfs_dev_dir[256];
236
237 struct {
238 void *(*bo_alloc)(void *bufmgr, const char *name, uint64_t size);
239 void (*bo_unreference)(void *bo);
240 void *(*bo_map)(void *ctx, void *bo, unsigned flags);
241 void (*bo_unmap)(void *bo);
242 bool (*batch_references)(void *batch, void *bo);
243 void (*bo_wait_rendering)(void *bo);
244 int (*bo_busy)(void *bo);
245 void (*emit_mi_flush)(void *ctx);
246 void (*emit_mi_report_perf_count)(void *ctx,
247 void *bo,
248 uint32_t offset_in_bytes,
249 uint32_t report_id);
250 void (*batchbuffer_flush)(void *ctx,
251 const char *file, int line);
252 void (*capture_frequency_stat_register)(void *ctx, void *bo,
253 uint32_t bo_offset);
254 void (*store_register_mem64)(void *ctx, void *bo, uint32_t reg, uint32_t offset);
255
256 } vtbl;
257 };
258
259 struct gen_perf_query_object;
260 const struct gen_perf_query_info* gen_perf_query_info(const struct gen_perf_query_object *);
261
262 void gen_perf_init_metrics(struct gen_perf_config *perf_cfg,
263 const struct gen_device_info *devinfo,
264 int drm_fd);
265
266 /** Query i915 for a metric id using guid.
267 */
268 bool gen_perf_load_metric_id(struct gen_perf_config *perf_cfg,
269 const char *guid,
270 uint64_t *metric_id);
271
272 /** Read the slice/unslice frequency from 2 OA reports and store then into
273 * result.
274 */
275 void gen_perf_query_result_read_frequencies(struct gen_perf_query_result *result,
276 const struct gen_device_info *devinfo,
277 const uint32_t *start,
278 const uint32_t *end);
279 /** Accumulate the delta between 2 OA reports into result for a given query.
280 */
281 void gen_perf_query_result_accumulate(struct gen_perf_query_result *result,
282 const struct gen_perf_query_info *query,
283 const uint32_t *start,
284 const uint32_t *end);
285 void gen_perf_query_result_clear(struct gen_perf_query_result *result);
286
287 struct gen_perf_context;
288 struct gen_perf_context *gen_perf_new_context(void *parent);
289
290 void gen_perf_init_context(struct gen_perf_context *perf_ctx,
291 struct gen_perf_config *perf_cfg,
292 void * ctx, /* driver context (eg, brw_context) */
293 void * bufmgr, /* eg brw_bufmgr */
294 const struct gen_device_info *devinfo,
295 uint32_t hw_ctx,
296 int drm_fd);
297
298 struct gen_perf_config *gen_perf_config(struct gen_perf_context *ctx);
299
300 int gen_perf_active_queries(struct gen_perf_context *perf_ctx,
301 const struct gen_perf_query_info *query);
302
303 static inline size_t
304 gen_perf_query_counter_get_size(const struct gen_perf_query_counter *counter)
305 {
306 switch (counter->data_type) {
307 case GEN_PERF_COUNTER_DATA_TYPE_BOOL32:
308 return sizeof(uint32_t);
309 case GEN_PERF_COUNTER_DATA_TYPE_UINT32:
310 return sizeof(uint32_t);
311 case GEN_PERF_COUNTER_DATA_TYPE_UINT64:
312 return sizeof(uint64_t);
313 case GEN_PERF_COUNTER_DATA_TYPE_FLOAT:
314 return sizeof(float);
315 case GEN_PERF_COUNTER_DATA_TYPE_DOUBLE:
316 return sizeof(double);
317 default:
318 unreachable("invalid counter data type");
319 }
320 }
321
322 static inline struct gen_perf_config *
323 gen_perf_new(void *ctx)
324 {
325 struct gen_perf_config *perf = rzalloc(ctx, struct gen_perf_config);
326 return perf;
327 }
328
329 struct gen_perf_query_object *
330 gen_perf_new_query(struct gen_perf_context *, unsigned query_index);
331
332
333 bool gen_perf_begin_query(struct gen_perf_context *perf_ctx,
334 struct gen_perf_query_object *query);
335 void gen_perf_end_query(struct gen_perf_context *perf_ctx,
336 struct gen_perf_query_object *query);
337 void gen_perf_wait_query(struct gen_perf_context *perf_ctx,
338 struct gen_perf_query_object *query,
339 void *current_batch);
340 bool gen_perf_is_query_ready(struct gen_perf_context *perf_ctx,
341 struct gen_perf_query_object *query,
342 void *current_batch);
343 void gen_perf_delete_query(struct gen_perf_context *perf_ctx,
344 struct gen_perf_query_object *query);
345 void gen_perf_get_query_data(struct gen_perf_context *perf_ctx,
346 struct gen_perf_query_object *query,
347 int data_size,
348 unsigned *data,
349 unsigned *bytes_written);
350
351 void gen_perf_dump_query_count(struct gen_perf_context *perf_ctx);
352 void gen_perf_dump_query(struct gen_perf_context *perf_ctx,
353 struct gen_perf_query_object *obj,
354 void *current_batch);
355
356 #endif /* GEN_PERF_H */