freedreno/a5xx: timestamp / time-elapsed queries
[mesa.git] / src / gallium / drivers / freedreno / a5xx / fd5_query.c
1 /*
2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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 FROM,
20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 * SOFTWARE.
22 *
23 * Authors:
24 * Rob Clark <robclark@freedesktop.org>
25 */
26
27 /* NOTE: see https://github.com/freedreno/freedreno/wiki/A5xx-Queries */
28
29 #include "freedreno_query_acc.h"
30 #include "freedreno_resource.h"
31
32 #include "fd5_context.h"
33 #include "fd5_format.h"
34 #include "fd5_query.h"
35
36 struct PACKED fd5_query_sample {
37 uint64_t start;
38 uint64_t result;
39 uint64_t stop;
40 };
41
42 #define query_sample(aq, field) \
43 fd_resource((aq)->prsc)->bo, \
44 offsetof(struct fd5_query_sample, field), \
45 0, 0
46
47 /*
48 * Occlusion Query:
49 *
50 * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
51 * interpret results
52 */
53
54 static void
55 occlusion_resume(struct fd_acc_query *aq, struct fd_batch *batch)
56 {
57 struct fd_ringbuffer *ring = batch->draw;
58
59 OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_CONTROL, 1);
60 OUT_RING(ring, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY);
61
62 OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
63 OUT_RELOCW(ring, query_sample(aq, start));
64
65 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
66 OUT_RING(ring, ZPASS_DONE);
67 fd_reset_wfi(batch);
68
69 fd5_context(batch->ctx)->samples_passed_queries++;
70 }
71
72 static void
73 occlusion_pause(struct fd_acc_query *aq, struct fd_batch *batch)
74 {
75 struct fd_ringbuffer *ring = batch->draw;
76
77 OUT_PKT7(ring, CP_MEM_WRITE, 4);
78 OUT_RELOCW(ring, query_sample(aq, stop));
79 OUT_RING(ring, 0xffffffff);
80 OUT_RING(ring, 0xffffffff);
81
82 OUT_PKT7(ring, CP_WAIT_MEM_WRITES, 0);
83
84 OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_CONTROL, 1);
85 OUT_RING(ring, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY);
86
87 OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
88 OUT_RELOCW(ring, query_sample(aq, stop));
89
90 OUT_PKT7(ring, CP_EVENT_WRITE, 1);
91 OUT_RING(ring, ZPASS_DONE);
92 fd_reset_wfi(batch);
93
94 OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
95 OUT_RING(ring, 0x00000014); // XXX
96 OUT_RELOC(ring, query_sample(aq, stop));
97 OUT_RING(ring, 0xffffffff);
98 OUT_RING(ring, 0xffffffff);
99 OUT_RING(ring, 0x00000010); // XXX
100
101 /* result += stop - start: */
102 OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
103 OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE |
104 CP_MEM_TO_MEM_0_NEG_C);
105 OUT_RELOCW(ring, query_sample(aq, result)); /* dst */
106 OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
107 OUT_RELOC(ring, query_sample(aq, stop)); /* srcB */
108 OUT_RELOC(ring, query_sample(aq, start)); /* srcC */
109
110 fd5_context(batch->ctx)->samples_passed_queries--;
111 }
112
113 static void
114 occlusion_counter_result(struct fd_context *ctx, void *buf,
115 union pipe_query_result *result)
116 {
117 struct fd5_query_sample *sp = buf;
118 result->u64 = sp->result;
119 }
120
121 static void
122 occlusion_predicate_result(struct fd_context *ctx, void *buf,
123 union pipe_query_result *result)
124 {
125 struct fd5_query_sample *sp = buf;
126 result->b = !!sp->result;
127 }
128
129 static const struct fd_acc_sample_provider occlusion_counter = {
130 .query_type = PIPE_QUERY_OCCLUSION_COUNTER,
131 .active = FD_STAGE_DRAW,
132 .size = sizeof(struct fd5_query_sample),
133 .resume = occlusion_resume,
134 .pause = occlusion_pause,
135 .result = occlusion_counter_result,
136 };
137
138 static const struct fd_acc_sample_provider occlusion_predicate = {
139 .query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
140 .active = FD_STAGE_DRAW,
141 .size = sizeof(struct fd5_query_sample),
142 .resume = occlusion_resume,
143 .pause = occlusion_pause,
144 .result = occlusion_predicate_result,
145 };
146
147 /*
148 * Timestamp Queries:
149 */
150
151 static void
152 timestamp_resume(struct fd_acc_query *aq, struct fd_batch *batch)
153 {
154 struct fd_ringbuffer *ring = batch->draw;
155
156 OUT_PKT7(ring, CP_EVENT_WRITE, 4);
157 OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
158 CP_EVENT_WRITE_0_TIMESTAMP);
159 OUT_RELOCW(ring, query_sample(aq, start));
160 OUT_RING(ring, 0x00000000);
161
162 fd_reset_wfi(batch);
163 }
164
165 static void
166 timestamp_pause(struct fd_acc_query *aq, struct fd_batch *batch)
167 {
168 struct fd_ringbuffer *ring = batch->draw;
169
170 OUT_PKT7(ring, CP_EVENT_WRITE, 4);
171 OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
172 CP_EVENT_WRITE_0_TIMESTAMP);
173 OUT_RELOCW(ring, query_sample(aq, stop));
174 OUT_RING(ring, 0x00000000);
175
176 fd_reset_wfi(batch);
177 fd_wfi(batch, ring);
178
179 /* result += stop - start: */
180 OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
181 OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE |
182 CP_MEM_TO_MEM_0_NEG_C);
183 OUT_RELOCW(ring, query_sample(aq, result)); /* dst */
184 OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
185 OUT_RELOC(ring, query_sample(aq, stop)); /* srcB */
186 OUT_RELOC(ring, query_sample(aq, start)); /* srcC */
187 }
188
189 static uint64_t
190 ticks_to_ns(struct fd_context *ctx, uint32_t ts)
191 {
192 /* This is based on the 19.2MHz always-on rbbm timer.
193 *
194 * TODO we should probably query this value from kernel..
195 */
196 return ts * (1000000000 / 19200000);
197 }
198
199 static void
200 time_elapsed_accumulate_result(struct fd_context *ctx, void *buf,
201 union pipe_query_result *result)
202 {
203 struct fd5_query_sample *sp = buf;
204 result->u64 = ticks_to_ns(ctx, sp->result);
205 }
206
207 static void
208 timestamp_accumulate_result(struct fd_context *ctx, void *buf,
209 union pipe_query_result *result)
210 {
211 struct fd5_query_sample *sp = buf;
212 result->u64 = ticks_to_ns(ctx, sp->result);
213 }
214
215 static const struct fd_acc_sample_provider time_elapsed = {
216 .query_type = PIPE_QUERY_TIME_ELAPSED,
217 .active = FD_STAGE_DRAW | FD_STAGE_CLEAR,
218 .size = sizeof(struct fd5_query_sample),
219 .resume = timestamp_resume,
220 .pause = timestamp_pause,
221 .result = time_elapsed_accumulate_result,
222 };
223
224 /* NOTE: timestamp query isn't going to give terribly sensible results
225 * on a tiler. But it is needed by qapitrace profile heatmap. If you
226 * add in a binning pass, the results get even more non-sensical. So
227 * we just return the timestamp on the first tile and hope that is
228 * kind of good enough.
229 */
230
231 static const struct fd_acc_sample_provider timestamp = {
232 .query_type = PIPE_QUERY_TIMESTAMP,
233 .active = FD_STAGE_ALL,
234 .size = sizeof(struct fd5_query_sample),
235 .resume = timestamp_resume,
236 .pause = timestamp_pause,
237 .result = timestamp_accumulate_result,
238 };
239
240 void
241 fd5_query_context_init(struct pipe_context *pctx)
242 {
243 struct fd_context *ctx = fd_context(pctx);
244
245 ctx->create_query = fd_acc_create_query;
246 ctx->query_set_stage = fd_acc_query_set_stage;
247
248 fd_acc_query_register_provider(pctx, &occlusion_counter);
249 fd_acc_query_register_provider(pctx, &occlusion_predicate);
250
251 fd_acc_query_register_provider(pctx, &time_elapsed);
252 fd_acc_query_register_provider(pctx, &timestamp);
253 }