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"
35 #include "fd6_format.h"
36 #include "fd6_query.h"
38 struct PACKED fd6_query_sample
{
44 #define query_sample(aq, field) \
45 fd_resource((aq)->prsc)->bo, \
46 offsetof(struct fd6_query_sample, field), \
52 * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
57 occlusion_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
59 struct fd_ringbuffer
*ring
= batch
->draw
;
61 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_CONTROL
, 1);
62 OUT_RING(ring
, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
64 OUT_PKT4(ring
, REG_A6XX_RB_SAMPLE_COUNT_ADDR_LO
, 2);
65 OUT_RELOCW(ring
, query_sample(aq
, start
));
67 fd6_event_write(batch
, ring
, ZPASS_DONE
, false);
69 fd6_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_A6XX_RB_SAMPLE_COUNT_CONTROL
, 1);
85 OUT_RING(ring
, A6XX_RB_SAMPLE_COUNT_CONTROL_COPY
);
87 OUT_PKT4(ring
, REG_A6XX_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 fd6_context(batch
->ctx
)->samples_passed_queries
--;
114 occlusion_counter_result(struct fd_acc_query
*aq
, void *buf
,
115 union pipe_query_result
*result
)
117 struct fd6_query_sample
*sp
= buf
;
118 result
->u64
= sp
->result
;
122 occlusion_predicate_result(struct fd_acc_query
*aq
, void *buf
,
123 union pipe_query_result
*result
)
125 struct fd6_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 fd6_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 fd6_query_sample
),
142 .resume
= occlusion_resume
,
143 .pause
= occlusion_pause
,
144 .result
= occlusion_predicate_result
,
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 fd6_query_sample
),
151 .resume
= occlusion_resume
,
152 .pause
= occlusion_pause
,
153 .result
= occlusion_predicate_result
,
161 timestamp_resume(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
163 struct fd_ringbuffer
*ring
= batch
->draw
;
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);
175 timestamp_pause(struct fd_acc_query
*aq
, struct fd_batch
*batch
)
177 struct fd_ringbuffer
*ring
= batch
->draw
;
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);
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 */
199 ticks_to_ns(uint32_t ts
)
201 /* This is based on the 19.2MHz always-on rbbm timer.
203 * TODO we should probably query this value from kernel..
205 return ts
* (1000000000 / 19200000);
209 time_elapsed_accumulate_result(struct fd_acc_query
*aq
, void *buf
,
210 union pipe_query_result
*result
)
212 struct fd6_query_sample
*sp
= buf
;
213 result
->u64
= ticks_to_ns(sp
->result
);
217 timestamp_accumulate_result(struct fd_acc_query
*aq
, void *buf
,
218 union pipe_query_result
*result
)
220 struct fd6_query_sample
*sp
= buf
;
221 result
->u64
= ticks_to_ns(sp
->result
);
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 fd6_query_sample
),
228 .resume
= timestamp_resume
,
229 .pause
= timestamp_pause
,
230 .result
= time_elapsed_accumulate_result
,
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.
240 static const struct fd_acc_sample_provider timestamp
= {
241 .query_type
= PIPE_QUERY_TIMESTAMP
,
242 .active
= FD_STAGE_ALL
,
243 .size
= sizeof(struct fd6_query_sample
),
244 .resume
= timestamp_resume
,
245 .pause
= timestamp_pause
,
246 .result
= timestamp_accumulate_result
,
250 fd6_query_context_init(struct pipe_context
*pctx
)
252 struct fd_context
*ctx
= fd_context(pctx
);
254 ctx
->create_query
= fd_acc_create_query
;
255 ctx
->query_set_stage
= fd_acc_query_set_stage
;
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
);
261 fd_acc_query_register_provider(pctx
, &time_elapsed
);
262 fd_acc_query_register_provider(pctx
, ×tamp
);