• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
occlusion_resume(struct fd_acc_query * aq,struct fd_batch * batch)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
occlusion_pause(struct fd_acc_query * aq,struct fd_batch * batch)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
occlusion_counter_result(struct fd_context * ctx,void * buf,union pipe_query_result * result)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
occlusion_predicate_result(struct fd_context * ctx,void * buf,union pipe_query_result * result)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 static const struct fd_acc_sample_provider occlusion_predicate_conservative = {
148 		.query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
149 		.active = FD_STAGE_DRAW,
150 		.size = sizeof(struct fd5_query_sample),
151 		.resume = occlusion_resume,
152 		.pause = occlusion_pause,
153 		.result = occlusion_predicate_result,
154 };
155 
156 /*
157  * Timestamp Queries:
158  */
159 
160 static void
timestamp_resume(struct fd_acc_query * aq,struct fd_batch * batch)161 timestamp_resume(struct fd_acc_query *aq, struct fd_batch *batch)
162 {
163 	struct fd_ringbuffer *ring = batch->draw;
164 
165 	OUT_PKT7(ring, CP_EVENT_WRITE, 4);
166 	OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
167 			CP_EVENT_WRITE_0_TIMESTAMP);
168 	OUT_RELOCW(ring, query_sample(aq, start));
169 	OUT_RING(ring, 0x00000000);
170 
171 	fd_reset_wfi(batch);
172 }
173 
174 static void
timestamp_pause(struct fd_acc_query * aq,struct fd_batch * batch)175 timestamp_pause(struct fd_acc_query *aq, struct fd_batch *batch)
176 {
177 	struct fd_ringbuffer *ring = batch->draw;
178 
179 	OUT_PKT7(ring, CP_EVENT_WRITE, 4);
180 	OUT_RING(ring, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT) |
181 			CP_EVENT_WRITE_0_TIMESTAMP);
182 	OUT_RELOCW(ring, query_sample(aq, stop));
183 	OUT_RING(ring, 0x00000000);
184 
185 	fd_reset_wfi(batch);
186 	fd_wfi(batch, ring);
187 
188 	/* result += stop - start: */
189 	OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
190 	OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE |
191 			CP_MEM_TO_MEM_0_NEG_C);
192 	OUT_RELOCW(ring, query_sample(aq, result));     /* dst */
193 	OUT_RELOC(ring, query_sample(aq, result));      /* srcA */
194 	OUT_RELOC(ring, query_sample(aq, stop));        /* srcB */
195 	OUT_RELOC(ring, query_sample(aq, start));       /* srcC */
196 }
197 
198 static uint64_t
ticks_to_ns(struct fd_context * ctx,uint32_t ts)199 ticks_to_ns(struct fd_context *ctx, uint32_t ts)
200 {
201 	/* This is based on the 19.2MHz always-on rbbm timer.
202 	 *
203 	 * TODO we should probably query this value from kernel..
204 	 */
205 	return ts * (1000000000 / 19200000);
206 }
207 
208 static void
time_elapsed_accumulate_result(struct fd_context * ctx,void * buf,union pipe_query_result * result)209 time_elapsed_accumulate_result(struct fd_context *ctx, void *buf,
210 		union pipe_query_result *result)
211 {
212 	struct fd5_query_sample *sp = buf;
213 	result->u64 = ticks_to_ns(ctx, sp->result);
214 }
215 
216 static void
timestamp_accumulate_result(struct fd_context * ctx,void * buf,union pipe_query_result * result)217 timestamp_accumulate_result(struct fd_context *ctx, void *buf,
218 		union pipe_query_result *result)
219 {
220 	struct fd5_query_sample *sp = buf;
221 	result->u64 = ticks_to_ns(ctx, sp->result);
222 }
223 
224 static const struct fd_acc_sample_provider time_elapsed = {
225 		.query_type = PIPE_QUERY_TIME_ELAPSED,
226 		.active = FD_STAGE_DRAW | FD_STAGE_CLEAR,
227 		.size = sizeof(struct fd5_query_sample),
228 		.resume = timestamp_resume,
229 		.pause = timestamp_pause,
230 		.result = time_elapsed_accumulate_result,
231 };
232 
233 /* NOTE: timestamp query isn't going to give terribly sensible results
234  * on a tiler.  But it is needed by qapitrace profile heatmap.  If you
235  * add in a binning pass, the results get even more non-sensical.  So
236  * we just return the timestamp on the first tile and hope that is
237  * kind of good enough.
238  */
239 
240 static const struct fd_acc_sample_provider timestamp = {
241 		.query_type = PIPE_QUERY_TIMESTAMP,
242 		.active = FD_STAGE_ALL,
243 		.size = sizeof(struct fd5_query_sample),
244 		.resume = timestamp_resume,
245 		.pause = timestamp_pause,
246 		.result = timestamp_accumulate_result,
247 };
248 
249 void
fd5_query_context_init(struct pipe_context * pctx)250 fd5_query_context_init(struct pipe_context *pctx)
251 {
252 	struct fd_context *ctx = fd_context(pctx);
253 
254 	ctx->create_query = fd_acc_create_query;
255 	ctx->query_set_stage = fd_acc_query_set_stage;
256 
257 	fd_acc_query_register_provider(pctx, &occlusion_counter);
258 	fd_acc_query_register_provider(pctx, &occlusion_predicate);
259 	fd_acc_query_register_provider(pctx, &occlusion_predicate_conservative);
260 
261 	fd_acc_query_register_provider(pctx, &time_elapsed);
262 	fd_acc_query_register_provider(pctx, &timestamp);
263 }
264