2f99076231578dfeb8de4d305800610c90826cd1
[mesa.git] / src / gallium / drivers / iris / iris_performance_query.c
1 /*
2 * Copyright © 2019 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 shall be included
12 * in all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22
23 #include <xf86drm.h>
24
25 #include "iris_context.h"
26 #include "iris_perf.h"
27
28 #include "perf/gen_perf.h"
29 #include "perf/gen_perf_regs.h"
30
31 struct iris_perf_query {
32 struct gl_perf_query_object base;
33 struct gen_perf_query_object *query;
34 };
35
36 static unsigned
37 iris_init_perf_query_info(struct pipe_context *pipe)
38 {
39 struct iris_context *ice = (void *) pipe;
40 struct iris_screen *screen = (struct iris_screen *) ice->ctx.screen;
41 struct gen_perf_config *perf_cfg = NULL;
42
43 /* make sure pipe perf counter type/data-type enums are matched with gen_perf's */
44 STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_EVENT == (enum pipe_perf_counter_type)GEN_PERF_COUNTER_TYPE_EVENT);
45 STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_DURATION_NORM == (enum pipe_perf_counter_type)GEN_PERF_COUNTER_TYPE_DURATION_NORM);
46 STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_DURATION_RAW == (enum pipe_perf_counter_type)GEN_PERF_COUNTER_TYPE_DURATION_RAW);
47 STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_THROUGHPUT == (enum pipe_perf_counter_type)GEN_PERF_COUNTER_TYPE_THROUGHPUT);
48 STATIC_ASSERT(PIPE_PERF_COUNTER_TYPE_RAW == (enum pipe_perf_counter_type)GEN_PERF_COUNTER_TYPE_RAW);
49
50 STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_BOOL32 == (enum pipe_perf_counter_data_type)GEN_PERF_COUNTER_DATA_TYPE_BOOL32);
51 STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_UINT32 == (enum pipe_perf_counter_data_type)GEN_PERF_COUNTER_DATA_TYPE_UINT32);
52 STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_UINT64 == (enum pipe_perf_counter_data_type)GEN_PERF_COUNTER_DATA_TYPE_UINT64);
53 STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_FLOAT == (enum pipe_perf_counter_data_type)GEN_PERF_COUNTER_DATA_TYPE_FLOAT);
54 STATIC_ASSERT(PIPE_PERF_COUNTER_DATA_TYPE_DOUBLE == (enum pipe_perf_counter_data_type)GEN_PERF_COUNTER_DATA_TYPE_DOUBLE);
55
56 if (!ice->perf_ctx)
57 ice->perf_ctx = gen_perf_new_context(ice);
58
59 if (unlikely(!ice->perf_ctx))
60 return 0;
61
62 perf_cfg = gen_perf_config(ice->perf_ctx);
63
64 if (perf_cfg)
65 return perf_cfg->n_queries;
66
67 perf_cfg = gen_perf_new(ice->perf_ctx);
68
69 iris_perf_init_vtbl(perf_cfg);
70
71 gen_perf_init_context(ice->perf_ctx,
72 perf_cfg,
73 ice,
74 screen->bufmgr,
75 &screen->devinfo,
76 ice->batches[IRIS_BATCH_RENDER].hw_ctx_id,
77 screen->fd);
78
79 gen_perf_init_metrics(perf_cfg, &screen->devinfo, screen->fd);
80
81 return perf_cfg->n_queries;
82 }
83
84 static struct pipe_query *
85 iris_new_perf_query_obj(struct pipe_context *pipe, unsigned query_index)
86 {
87 struct iris_context *ice = (void *) pipe;
88 struct gen_perf_context *perf_ctx = ice->perf_ctx;
89 struct gen_perf_query_object * obj = gen_perf_new_query(perf_ctx, query_index);
90 if (unlikely(!obj))
91 return NULL;
92
93 struct iris_perf_query *q = calloc(1, sizeof(struct iris_perf_query));
94 if (unlikely(!q)) {
95 gen_perf_delete_query(perf_ctx, obj);
96 return NULL;
97 }
98
99 q->query = obj;
100 return (struct pipe_query *)&q->base;
101 }
102
103 static void
104 iris_begin_perf_query(struct pipe_context *pipe, struct pipe_query *q)
105 {
106 struct iris_context *ice = (void *) pipe;
107 struct iris_perf_query *perf_query= (struct iris_perf_query *) q;
108 struct gen_perf_query_object *obj = perf_query->query;
109 struct gen_perf_context *perf_ctx = ice->perf_ctx;
110
111 gen_perf_begin_query(perf_ctx, obj);
112 }
113
114 static void
115 iris_end_perf_query(struct pipe_context *pipe, struct pipe_query *q)
116 {
117 struct iris_context *ice = (void *) pipe;
118 struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
119 struct gen_perf_query_object *obj = perf_query->query;
120 struct gen_perf_context *perf_ctx = ice->perf_ctx;
121
122 gen_perf_end_query(perf_ctx, obj);
123 }
124
125 static void
126 iris_delete_perf_query(struct pipe_context *pipe, struct pipe_query *q)
127 {
128 struct iris_context *ice = (void *) pipe;
129 struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
130 struct gen_perf_query_object *obj = perf_query->query;
131 struct gen_perf_context *perf_ctx = ice->perf_ctx;
132
133 gen_perf_delete_query(perf_ctx, obj);
134 free(q);
135 }
136
137 static void
138 iris_get_perf_query_info(struct pipe_context *pipe,
139 unsigned query_index,
140 const char **name,
141 uint32_t *data_size,
142 uint32_t *n_counters,
143 uint32_t *n_active)
144 {
145 struct iris_context *ice = (void *) pipe;
146 struct gen_perf_context *perf_ctx = ice->perf_ctx;
147 struct gen_perf_config *perf_cfg = gen_perf_config(perf_ctx);
148 const struct gen_perf_query_info *info = &perf_cfg->queries[query_index];
149
150 *name = info->name;
151 *data_size = info->data_size;
152 *n_counters = info->n_counters;
153 *n_active = gen_perf_active_queries(perf_ctx, info);
154 }
155
156 static void
157 iris_get_perf_counter_info(struct pipe_context *pipe,
158 unsigned query_index,
159 unsigned counter_index,
160 const char **name,
161 const char **desc,
162 uint32_t *offset,
163 uint32_t *data_size,
164 uint32_t *type_enum,
165 uint32_t *data_type_enum,
166 uint64_t *raw_max)
167 {
168 struct iris_context *ice = (void *) pipe;
169 struct gen_perf_context *perf_ctx = ice->perf_ctx;
170 struct gen_perf_config *perf_cfg = gen_perf_config(perf_ctx);
171 const struct gen_perf_query_info *info = &perf_cfg->queries[query_index];
172 const struct gen_perf_query_counter *counter = &info->counters[counter_index];
173
174 *name = counter->name;
175 *desc = counter->desc;
176 *offset = counter->offset;
177 *data_size = gen_perf_query_counter_get_size(counter);
178 *type_enum = counter->type;
179 *data_type_enum = counter->data_type;
180 *raw_max = counter->raw_max;
181 }
182
183 static void
184 iris_wait_perf_query(struct pipe_context *pipe, struct pipe_query *q)
185 {
186 struct iris_context *ice = (void *) pipe;
187 struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
188 struct gen_perf_query_object *obj = perf_query->query;
189 struct gen_perf_context *perf_ctx = ice->perf_ctx;
190
191 gen_perf_wait_query(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER]);
192 }
193
194 static bool
195 iris_is_perf_query_ready(struct pipe_context *pipe, struct pipe_query *q)
196 {
197 struct iris_context *ice = (void *) pipe;
198 struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
199 struct gen_perf_query_object *obj = perf_query->query;
200 struct gen_perf_context *perf_ctx = ice->perf_ctx;
201
202 if (perf_query->base.Ready)
203 return true;
204
205 return gen_perf_is_query_ready(perf_ctx, obj, &ice->batches[IRIS_BATCH_RENDER]);
206 }
207
208 static void
209 iris_get_perf_query_data(struct pipe_context *pipe,
210 struct pipe_query *q,
211 size_t data_size,
212 uint32_t *data,
213 uint32_t *bytes_written)
214 {
215 struct iris_context *ice = (void *) pipe;
216 struct iris_perf_query *perf_query = (struct iris_perf_query *) q;
217 struct gen_perf_query_object *obj = perf_query->query;
218 struct gen_perf_context *perf_ctx = ice->perf_ctx;
219
220 gen_perf_get_query_data(perf_ctx, obj, data_size, data, bytes_written);
221 }
222
223 void
224 iris_init_perfquery_functions(struct pipe_context *ctx)
225 {
226 ctx->init_intel_perf_query_info = iris_init_perf_query_info;
227 ctx->get_intel_perf_query_info = iris_get_perf_query_info;
228 ctx->get_intel_perf_query_counter_info = iris_get_perf_counter_info;
229 ctx->new_intel_perf_query_obj = iris_new_perf_query_obj;
230 ctx->begin_intel_perf_query = iris_begin_perf_query;
231 ctx->end_intel_perf_query = iris_end_perf_query;
232 ctx->delete_intel_perf_query = iris_delete_perf_query;
233 ctx->wait_intel_perf_query = iris_wait_perf_query;
234 ctx->is_intel_perf_query_ready = iris_is_perf_query_ready;
235 ctx->get_intel_perf_query_data = iris_get_perf_query_data;
236 }