2 * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
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:
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
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
24 * Rob Clark <robclark@freedesktop.org>
27 /* NOTE: see https://github.com/freedreno/freedreno/wiki/A5xx-Queries */
29 #include "freedreno_query_acc.h"
30 #include "freedreno_resource.h"
32 #include "fd5_context.h"
33 #include "fd5_format.h"
34 #include "fd5_query.h"
36 struct PACKED fd5_query_sample
{
42 #define query_sample(aq, field) \
43 fd_resource((aq)->prsc)->bo, \
44 offsetof(struct fd5_query_sample, field), \
50 * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
55 occlusion_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
57 struct fd_ringbuffer
*ring
= batch
->draw
;
59 OUT_PKT4(ring
, REG_A5XX_RB_SAMPLE_COUNT_CONTROL
, 1);
60 OUT_RING(ring
, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
62 OUT_PKT4(ring
, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO
, 2);
63 OUT_RELOCW(ring
, query_sample(aq
, start
));
65 OUT_PKT7(ring
, CP_EVENT_WRITE
, 1);
66 OUT_RING(ring
, ZPASS_DONE
);
69 fd5_context(batch
->ctx
)->samples_passed_queries
++;
73 occlusion_pause(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
75 struct fd_ringbuffer
*ring
= batch
->draw
;
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);
82 OUT_PKT7(ring
, CP_WAIT_MEM_WRITES
, 0);
84 OUT_PKT4(ring
, REG_A5XX_RB_SAMPLE_COUNT_CONTROL
, 1);
85 OUT_RING(ring
, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
87 OUT_PKT4(ring
, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO
, 2);
88 OUT_RELOCW(ring
, query_sample(aq
, stop
));
90 OUT_PKT7(ring
, CP_EVENT_WRITE
, 1);
91 OUT_RING(ring
, ZPASS_DONE
);
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
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 */
110 fd5_context(batch
->ctx
)->samples_passed_queries
--;
114 occlusion_counter_result(struct fd_context
*ctx
, void *buf
,
115 union pipe_query_result
*result
)
117 struct fd5_query_sample
*sp
= buf
;
118 result
->u64
= sp
->result
;
122 occlusion_predicate_result(struct fd_context
*ctx
, void *buf
,
123 union pipe_query_result
*result
)
125 struct fd5_query_sample
*sp
= buf
;
126 result
->b
= !!sp
->result
;
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
,
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
,
152 timestamp_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
154 struct fd_ringbuffer
*ring
= batch
->draw
;
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);
166 timestamp_pause(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
168 struct fd_ringbuffer
*ring
= batch
->draw
;
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);
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 */
190 ticks_to_ns(struct fd_context
*ctx
, uint32_t ts
)
192 /* This is based on the 19.2MHz always-on rbbm timer.
194 * TODO we should probably query this value from kernel..
196 return ts
* (1000000000 / 19200000);
200 time_elapsed_accumulate_result(struct fd_context
*ctx
, void *buf
,
201 union pipe_query_result
*result
)
203 struct fd5_query_sample
*sp
= buf
;
204 result
->u64
= ticks_to_ns(ctx
, sp
->result
);
208 timestamp_accumulate_result(struct fd_context
*ctx
, void *buf
,
209 union pipe_query_result
*result
)
211 struct fd5_query_sample
*sp
= buf
;
212 result
->u64
= ticks_to_ns(ctx
, sp
->result
);
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
,
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.
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
,
241 fd5_query_context_init(struct pipe_context
*pctx
)
243 struct fd_context
*ctx
= fd_context(pctx
);
245 ctx
->create_query
= fd_acc_create_query
;
246 ctx
->query_set_stage
= fd_acc_query_set_stage
;
248 fd_acc_query_register_provider(pctx
, &occlusion_counter
);
249 fd_acc_query_register_provider(pctx
, &occlusion_predicate
);
251 fd_acc_query_register_provider(pctx
, &time_elapsed
);
252 fd_acc_query_register_provider(pctx
, ×tamp
);