2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3 * Copyright © 2018 Google, Inc.
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * Rob Clark <robclark@freedesktop.org>
28 /* NOTE: see https://github.com/freedreno/freedreno/wiki/A5xx-Queries */
30 #include "freedreno_query_acc.h"
31 #include "freedreno_resource.h"
33 #include "fd6_context.h"
34 #include "fd6_format.h"
35 #include "fd6_query.h"
37 struct PACKED fd6_query_sample
{
43 #define query_sample(aq, field) \
44 fd_resource((aq)->prsc)->bo, \
45 offsetof(struct fd6_query_sample, field), \
51 * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
56 occlusion_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
58 struct fd_ringbuffer
*ring
= batch
->draw
;
60 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_CONTROL
, 1);
61 OUT_RING(ring
, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
63 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_ADDR_LO
, 2);
64 OUT_RELOCW(ring
, query_sample(aq
, start
));
66 OUT_PKT7(ring
, CP_EVENT_WRITE
, 1);
67 OUT_RING(ring
, ZPASS_DONE
);
70 fd6_context(batch
->ctx
)->samples_passed_queries
++;
74 occlusion_pause(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
76 struct fd_ringbuffer
*ring
= batch
->draw
;
78 OUT_PKT7(ring
, CP_MEM_WRITE
, 4);
79 OUT_RELOCW(ring
, query_sample(aq
, stop
));
80 OUT_RING(ring
, 0xffffffff);
81 OUT_RING(ring
, 0xffffffff);
83 OUT_PKT7(ring
, CP_WAIT_MEM_WRITES
, 0);
85 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_CONTROL
, 1);
86 OUT_RING(ring
, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
88 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_ADDR_LO
, 2);
89 OUT_RELOCW(ring
, query_sample(aq
, stop
));
91 OUT_PKT7(ring
, CP_EVENT_WRITE
, 1);
92 OUT_RING(ring
, ZPASS_DONE
);
95 OUT_PKT7(ring
, CP_WAIT_REG_MEM
, 6);
96 OUT_RING(ring
, 0x00000014); // XXX
97 OUT_RELOC(ring
, query_sample(aq
, stop
));
98 OUT_RING(ring
, 0xffffffff);
99 OUT_RING(ring
, 0xffffffff);
100 OUT_RING(ring
, 0x00000010); // XXX
102 /* result += stop - start: */
103 OUT_PKT7(ring
, CP_MEM_TO_MEM
, 9);
104 OUT_RING(ring
, CP_MEM_TO_MEM_0_DOUBLE
|
105 CP_MEM_TO_MEM_0_NEG_C
);
106 OUT_RELOCW(ring
, query_sample(aq
, result
)); /* dst */
107 OUT_RELOC(ring
, query_sample(aq
, result
)); /* srcA */
108 OUT_RELOC(ring
, query_sample(aq
, stop
)); /* srcB */
109 OUT_RELOC(ring
, query_sample(aq
, start
)); /* srcC */
111 fd6_context(batch
->ctx
)->samples_passed_queries
--;
115 occlusion_counter_result(struct fd_acc_query
*aq
, void *buf
,
116 union pipe_query_result
*result
)
118 struct fd6_query_sample
*sp
= buf
;
119 result
->u64
= sp
->result
;
123 occlusion_predicate_result(struct fd_acc_query
*aq
, void *buf
,
124 union pipe_query_result
*result
)
126 struct fd6_query_sample
*sp
= buf
;
127 result
->b
= !!sp
->result
;
130 static const struct fd_acc_sample_provider occlusion_counter
= {
131 .query_type
= PIPE_QUERY_OCCLUSION_COUNTER
,
132 .active
= FD_STAGE_DRAW
,
133 .size
= sizeof(struct fd6_query_sample
),
134 .resume
= occlusion_resume
,
135 .pause
= occlusion_pause
,
136 .result
= occlusion_counter_result
,
139 static const struct fd_acc_sample_provider occlusion_predicate
= {
140 .query_type
= PIPE_QUERY_OCCLUSION_PREDICATE
,
141 .active
= FD_STAGE_DRAW
,
142 .size
= sizeof(struct fd6_query_sample
),
143 .resume
= occlusion_resume
,
144 .pause
= occlusion_pause
,
145 .result
= occlusion_predicate_result
,
148 static const struct fd_acc_sample_provider occlusion_predicate_conservative
= {
149 .query_type
= PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE
,
150 .active
= FD_STAGE_DRAW
,
151 .size
= sizeof(struct fd6_query_sample
),
152 .resume
= occlusion_resume
,
153 .pause
= occlusion_pause
,
154 .result
= occlusion_predicate_result
,
162 timestamp_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
164 struct fd_ringbuffer
*ring
= batch
->draw
;
166 OUT_PKT7(ring
, CP_EVENT_WRITE
, 4);
167 OUT_RING(ring
, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT
) |
168 CP_EVENT_WRITE_0_TIMESTAMP
);
169 OUT_RELOCW(ring
, query_sample(aq
, start
));
170 OUT_RING(ring
, 0x00000000);
176 timestamp_pause(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
178 struct fd_ringbuffer
*ring
= batch
->draw
;
180 OUT_PKT7(ring
, CP_EVENT_WRITE
, 4);
181 OUT_RING(ring
, CP_EVENT_WRITE_0_EVENT(CACHE_FLUSH_AND_INV_EVENT
) |
182 CP_EVENT_WRITE_0_TIMESTAMP
);
183 OUT_RELOCW(ring
, query_sample(aq
, stop
));
184 OUT_RING(ring
, 0x00000000);
189 /* result += stop - start: */
190 OUT_PKT7(ring
, CP_MEM_TO_MEM
, 9);
191 OUT_RING(ring
, CP_MEM_TO_MEM_0_DOUBLE
|
192 CP_MEM_TO_MEM_0_NEG_C
);
193 OUT_RELOCW(ring
, query_sample(aq
, result
)); /* dst */
194 OUT_RELOC(ring
, query_sample(aq
, result
)); /* srcA */
195 OUT_RELOC(ring
, query_sample(aq
, stop
)); /* srcB */
196 OUT_RELOC(ring
, query_sample(aq
, start
)); /* srcC */
200 ticks_to_ns(uint32_t ts
)
202 /* This is based on the 19.2MHz always-on rbbm timer.
204 * TODO we should probably query this value from kernel..
206 return ts
* (1000000000 / 19200000);
210 time_elapsed_accumulate_result(struct fd_acc_query
*aq
, void *buf
,
211 union pipe_query_result
*result
)
213 struct fd6_query_sample
*sp
= buf
;
214 result
->u64
= ticks_to_ns(sp
->result
);
218 timestamp_accumulate_result(struct fd_acc_query
*aq
, void *buf
,
219 union pipe_query_result
*result
)
221 struct fd6_query_sample
*sp
= buf
;
222 result
->u64
= ticks_to_ns(sp
->result
);
225 static const struct fd_acc_sample_provider time_elapsed
= {
226 .query_type
= PIPE_QUERY_TIME_ELAPSED
,
227 .active
= FD_STAGE_DRAW
| FD_STAGE_CLEAR
,
228 .size
= sizeof(struct fd6_query_sample
),
229 .resume
= timestamp_resume
,
230 .pause
= timestamp_pause
,
231 .result
= time_elapsed_accumulate_result
,
234 /* NOTE: timestamp query isn't going to give terribly sensible results
235 * on a tiler. But it is needed by qapitrace profile heatmap. If you
236 * add in a binning pass, the results get even more non-sensical. So
237 * we just return the timestamp on the first tile and hope that is
238 * kind of good enough.
241 static const struct fd_acc_sample_provider timestamp
= {
242 .query_type
= PIPE_QUERY_TIMESTAMP
,
243 .active
= FD_STAGE_ALL
,
244 .size
= sizeof(struct fd6_query_sample
),
245 .resume
= timestamp_resume
,
246 .pause
= timestamp_pause
,
247 .result
= timestamp_accumulate_result
,
251 fd6_query_context_init(struct pipe_context
*pctx
)
253 struct fd_context
*ctx
= fd_context(pctx
);
255 ctx
->create_query
= fd_acc_create_query
;
256 ctx
->query_set_stage
= fd_acc_query_set_stage
;
258 fd_acc_query_register_provider(pctx
, &occlusion_counter
);
259 fd_acc_query_register_provider(pctx
, &occlusion_predicate
);
260 fd_acc_query_register_provider(pctx
, &occlusion_predicate_conservative
);
262 fd_acc_query_register_provider(pctx
, &time_elapsed
);
263 fd_acc_query_register_provider(pctx
, ×tamp
);