1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright (C) 2017 Rob Clark <robclark@freedesktop.org>
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
13bf215546Sopenharmony_ci * Software.
14bf215546Sopenharmony_ci *
15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21bf215546Sopenharmony_ci * SOFTWARE.
22bf215546Sopenharmony_ci *
23bf215546Sopenharmony_ci * Authors:
24bf215546Sopenharmony_ci *    Rob Clark <robclark@freedesktop.org>
25bf215546Sopenharmony_ci */
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci/* NOTE: see https://github.com/freedreno/freedreno/wiki/A5xx-Queries */
28bf215546Sopenharmony_ci
29bf215546Sopenharmony_ci#include "freedreno_query_acc.h"
30bf215546Sopenharmony_ci#include "freedreno_resource.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_ci#include "fd5_context.h"
33bf215546Sopenharmony_ci#include "fd5_emit.h"
34bf215546Sopenharmony_ci#include "fd5_format.h"
35bf215546Sopenharmony_ci#include "fd5_query.h"
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct PACKED fd5_query_sample {
38bf215546Sopenharmony_ci   uint64_t start;
39bf215546Sopenharmony_ci   uint64_t result;
40bf215546Sopenharmony_ci   uint64_t stop;
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci/* offset of a single field of an array of fd5_query_sample: */
44bf215546Sopenharmony_ci#define query_sample_idx(aq, idx, field)                                       \
45bf215546Sopenharmony_ci   fd_resource((aq)->prsc)->bo,                                                \
46bf215546Sopenharmony_ci      (idx * sizeof(struct fd5_query_sample)) +                                \
47bf215546Sopenharmony_ci         offsetof(struct fd5_query_sample, field),                             \
48bf215546Sopenharmony_ci      0, 0
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci/* offset of a single field of fd5_query_sample: */
51bf215546Sopenharmony_ci#define query_sample(aq, field) query_sample_idx(aq, 0, field)
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci/*
54bf215546Sopenharmony_ci * Occlusion Query:
55bf215546Sopenharmony_ci *
56bf215546Sopenharmony_ci * OCCLUSION_COUNTER and OCCLUSION_PREDICATE differ only in how they
57bf215546Sopenharmony_ci * interpret results
58bf215546Sopenharmony_ci */
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_cistatic void
61bf215546Sopenharmony_ciocclusion_resume(struct fd_acc_query *aq, struct fd_batch *batch)
62bf215546Sopenharmony_ci{
63bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_CONTROL, 1);
66bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY);
67bf215546Sopenharmony_ci
68bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
69bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, start));
70bf215546Sopenharmony_ci
71bf215546Sopenharmony_ci   fd5_event_write(batch, ring, ZPASS_DONE, false);
72bf215546Sopenharmony_ci   fd_reset_wfi(batch);
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   fd5_context(batch->ctx)->samples_passed_queries++;
75bf215546Sopenharmony_ci}
76bf215546Sopenharmony_ci
77bf215546Sopenharmony_cistatic void
78bf215546Sopenharmony_ciocclusion_pause(struct fd_acc_query *aq, struct fd_batch *batch)
79bf215546Sopenharmony_ci{
80bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_MEM_WRITE, 4);
83bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));
84bf215546Sopenharmony_ci   OUT_RING(ring, 0xffffffff);
85bf215546Sopenharmony_ci   OUT_RING(ring, 0xffffffff);
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_MEM_WRITES, 0);
88bf215546Sopenharmony_ci
89bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_CONTROL, 1);
90bf215546Sopenharmony_ci   OUT_RING(ring, A5XX_RB_SAMPLE_COUNT_CONTROL_COPY);
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_ci   OUT_PKT4(ring, REG_A5XX_RB_SAMPLE_COUNT_ADDR_LO, 2);
93bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));
94bf215546Sopenharmony_ci
95bf215546Sopenharmony_ci   fd5_event_write(batch, ring, ZPASS_DONE, false);
96bf215546Sopenharmony_ci   fd_reset_wfi(batch);
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_WAIT_REG_MEM, 6);
99bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000014); // XXX
100bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));
101bf215546Sopenharmony_ci   OUT_RING(ring, 0xffffffff);
102bf215546Sopenharmony_ci   OUT_RING(ring, 0xffffffff);
103bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000010); // XXX
104bf215546Sopenharmony_ci
105bf215546Sopenharmony_ci   /* result += stop - start: */
106bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
107bf215546Sopenharmony_ci   OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE | CP_MEM_TO_MEM_0_NEG_C);
108bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, result)); /* dst */
109bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
110bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));   /* srcB */
111bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, start));  /* srcC */
112bf215546Sopenharmony_ci
113bf215546Sopenharmony_ci   fd5_context(batch->ctx)->samples_passed_queries--;
114bf215546Sopenharmony_ci}
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_cistatic void
117bf215546Sopenharmony_ciocclusion_counter_result(struct fd_acc_query *aq, void *buf,
118bf215546Sopenharmony_ci                         union pipe_query_result *result)
119bf215546Sopenharmony_ci{
120bf215546Sopenharmony_ci   struct fd5_query_sample *sp = buf;
121bf215546Sopenharmony_ci   result->u64 = sp->result;
122bf215546Sopenharmony_ci}
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cistatic void
125bf215546Sopenharmony_ciocclusion_predicate_result(struct fd_acc_query *aq, void *buf,
126bf215546Sopenharmony_ci                           union pipe_query_result *result)
127bf215546Sopenharmony_ci{
128bf215546Sopenharmony_ci   struct fd5_query_sample *sp = buf;
129bf215546Sopenharmony_ci   result->b = !!sp->result;
130bf215546Sopenharmony_ci}
131bf215546Sopenharmony_ci
132bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider occlusion_counter = {
133bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_COUNTER,
134bf215546Sopenharmony_ci   .size = sizeof(struct fd5_query_sample),
135bf215546Sopenharmony_ci   .resume = occlusion_resume,
136bf215546Sopenharmony_ci   .pause = occlusion_pause,
137bf215546Sopenharmony_ci   .result = occlusion_counter_result,
138bf215546Sopenharmony_ci};
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider occlusion_predicate = {
141bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_PREDICATE,
142bf215546Sopenharmony_ci   .size = sizeof(struct fd5_query_sample),
143bf215546Sopenharmony_ci   .resume = occlusion_resume,
144bf215546Sopenharmony_ci   .pause = occlusion_pause,
145bf215546Sopenharmony_ci   .result = occlusion_predicate_result,
146bf215546Sopenharmony_ci};
147bf215546Sopenharmony_ci
148bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider occlusion_predicate_conservative = {
149bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE,
150bf215546Sopenharmony_ci   .size = sizeof(struct fd5_query_sample),
151bf215546Sopenharmony_ci   .resume = occlusion_resume,
152bf215546Sopenharmony_ci   .pause = occlusion_pause,
153bf215546Sopenharmony_ci   .result = occlusion_predicate_result,
154bf215546Sopenharmony_ci};
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci/*
157bf215546Sopenharmony_ci * Timestamp Queries:
158bf215546Sopenharmony_ci */
159bf215546Sopenharmony_ci
160bf215546Sopenharmony_cistatic void
161bf215546Sopenharmony_citimestamp_resume(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt
162bf215546Sopenharmony_ci{
163bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
164bf215546Sopenharmony_ci
165bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_EVENT_WRITE, 4);
166bf215546Sopenharmony_ci   OUT_RING(ring,
167bf215546Sopenharmony_ci            CP_EVENT_WRITE_0_EVENT(RB_DONE_TS) | CP_EVENT_WRITE_0_TIMESTAMP);
168bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, start));
169bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_ci   fd_reset_wfi(batch);
172bf215546Sopenharmony_ci}
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_cistatic void
175bf215546Sopenharmony_citimestamp_pause(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt
176bf215546Sopenharmony_ci{
177bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_EVENT_WRITE, 4);
180bf215546Sopenharmony_ci   OUT_RING(ring,
181bf215546Sopenharmony_ci            CP_EVENT_WRITE_0_EVENT(RB_DONE_TS) | CP_EVENT_WRITE_0_TIMESTAMP);
182bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));
183bf215546Sopenharmony_ci   OUT_RING(ring, 0x00000000);
184bf215546Sopenharmony_ci
185bf215546Sopenharmony_ci   fd_reset_wfi(batch);
186bf215546Sopenharmony_ci   fd_wfi(batch, ring);
187bf215546Sopenharmony_ci
188bf215546Sopenharmony_ci   /* result += stop - start: */
189bf215546Sopenharmony_ci   OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
190bf215546Sopenharmony_ci   OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE | CP_MEM_TO_MEM_0_NEG_C);
191bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, result)); /* dst */
192bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, result)); /* srcA */
193bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, stop));   /* srcB */
194bf215546Sopenharmony_ci   OUT_RELOC(ring, query_sample(aq, start));  /* srcC */
195bf215546Sopenharmony_ci}
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_cistatic uint64_t
198bf215546Sopenharmony_citicks_to_ns(uint32_t ts)
199bf215546Sopenharmony_ci{
200bf215546Sopenharmony_ci   /* This is based on the 19.2MHz always-on rbbm timer.
201bf215546Sopenharmony_ci    *
202bf215546Sopenharmony_ci    * TODO we should probably query this value from kernel..
203bf215546Sopenharmony_ci    */
204bf215546Sopenharmony_ci   return ts * (1000000000 / 19200000);
205bf215546Sopenharmony_ci}
206bf215546Sopenharmony_ci
207bf215546Sopenharmony_cistatic void
208bf215546Sopenharmony_citime_elapsed_accumulate_result(struct fd_acc_query *aq, void *buf,
209bf215546Sopenharmony_ci                               union pipe_query_result *result)
210bf215546Sopenharmony_ci{
211bf215546Sopenharmony_ci   struct fd5_query_sample *sp = buf;
212bf215546Sopenharmony_ci   result->u64 = ticks_to_ns(sp->result);
213bf215546Sopenharmony_ci}
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_cistatic void
216bf215546Sopenharmony_citimestamp_accumulate_result(struct fd_acc_query *aq, void *buf,
217bf215546Sopenharmony_ci                            union pipe_query_result *result)
218bf215546Sopenharmony_ci{
219bf215546Sopenharmony_ci   struct fd5_query_sample *sp = buf;
220bf215546Sopenharmony_ci   result->u64 = ticks_to_ns(sp->result);
221bf215546Sopenharmony_ci}
222bf215546Sopenharmony_ci
223bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider time_elapsed = {
224bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_TIME_ELAPSED,
225bf215546Sopenharmony_ci   .always = true,
226bf215546Sopenharmony_ci   .size = sizeof(struct fd5_query_sample),
227bf215546Sopenharmony_ci   .resume = timestamp_resume,
228bf215546Sopenharmony_ci   .pause = timestamp_pause,
229bf215546Sopenharmony_ci   .result = time_elapsed_accumulate_result,
230bf215546Sopenharmony_ci};
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci/* NOTE: timestamp query isn't going to give terribly sensible results
233bf215546Sopenharmony_ci * on a tiler.  But it is needed by qapitrace profile heatmap.  If you
234bf215546Sopenharmony_ci * add in a binning pass, the results get even more non-sensical.  So
235bf215546Sopenharmony_ci * we just return the timestamp on the first tile and hope that is
236bf215546Sopenharmony_ci * kind of good enough.
237bf215546Sopenharmony_ci */
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider timestamp = {
240bf215546Sopenharmony_ci   .query_type = PIPE_QUERY_TIMESTAMP,
241bf215546Sopenharmony_ci   .always = true,
242bf215546Sopenharmony_ci   .size = sizeof(struct fd5_query_sample),
243bf215546Sopenharmony_ci   .resume = timestamp_resume,
244bf215546Sopenharmony_ci   .pause = timestamp_pause,
245bf215546Sopenharmony_ci   .result = timestamp_accumulate_result,
246bf215546Sopenharmony_ci};
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci/*
249bf215546Sopenharmony_ci * Performance Counter (batch) queries:
250bf215546Sopenharmony_ci *
251bf215546Sopenharmony_ci * Only one of these is active at a time, per design of the gallium
252bf215546Sopenharmony_ci * batch_query API design.  On perfcntr query tracks N query_types,
253bf215546Sopenharmony_ci * each of which has a 'fd_batch_query_entry' that maps it back to
254bf215546Sopenharmony_ci * the associated group and counter.
255bf215546Sopenharmony_ci */
256bf215546Sopenharmony_ci
257bf215546Sopenharmony_cistruct fd_batch_query_entry {
258bf215546Sopenharmony_ci   uint8_t gid; /* group-id */
259bf215546Sopenharmony_ci   uint8_t cid; /* countable-id within the group */
260bf215546Sopenharmony_ci};
261bf215546Sopenharmony_ci
262bf215546Sopenharmony_cistruct fd_batch_query_data {
263bf215546Sopenharmony_ci   struct fd_screen *screen;
264bf215546Sopenharmony_ci   unsigned num_query_entries;
265bf215546Sopenharmony_ci   struct fd_batch_query_entry query_entries[];
266bf215546Sopenharmony_ci};
267bf215546Sopenharmony_ci
268bf215546Sopenharmony_cistatic void
269bf215546Sopenharmony_ciperfcntr_resume(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt
270bf215546Sopenharmony_ci{
271bf215546Sopenharmony_ci   struct fd_batch_query_data *data = aq->query_data;
272bf215546Sopenharmony_ci   struct fd_screen *screen = data->screen;
273bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
274bf215546Sopenharmony_ci
275bf215546Sopenharmony_ci   unsigned counters_per_group[screen->num_perfcntr_groups];
276bf215546Sopenharmony_ci   memset(counters_per_group, 0, sizeof(counters_per_group));
277bf215546Sopenharmony_ci
278bf215546Sopenharmony_ci   fd_wfi(batch, ring);
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_ci   /* configure performance counters for the requested queries: */
281bf215546Sopenharmony_ci   for (unsigned i = 0; i < data->num_query_entries; i++) {
282bf215546Sopenharmony_ci      struct fd_batch_query_entry *entry = &data->query_entries[i];
283bf215546Sopenharmony_ci      const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];
284bf215546Sopenharmony_ci      unsigned counter_idx = counters_per_group[entry->gid]++;
285bf215546Sopenharmony_ci
286bf215546Sopenharmony_ci      assert(counter_idx < g->num_counters);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci      OUT_PKT4(ring, g->counters[counter_idx].select_reg, 1);
289bf215546Sopenharmony_ci      OUT_RING(ring, g->countables[entry->cid].selector);
290bf215546Sopenharmony_ci   }
291bf215546Sopenharmony_ci
292bf215546Sopenharmony_ci   memset(counters_per_group, 0, sizeof(counters_per_group));
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ci   /* and snapshot the start values */
295bf215546Sopenharmony_ci   for (unsigned i = 0; i < data->num_query_entries; i++) {
296bf215546Sopenharmony_ci      struct fd_batch_query_entry *entry = &data->query_entries[i];
297bf215546Sopenharmony_ci      const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];
298bf215546Sopenharmony_ci      unsigned counter_idx = counters_per_group[entry->gid]++;
299bf215546Sopenharmony_ci      const struct fd_perfcntr_counter *counter = &g->counters[counter_idx];
300bf215546Sopenharmony_ci
301bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_REG_TO_MEM, 3);
302bf215546Sopenharmony_ci      OUT_RING(ring, CP_REG_TO_MEM_0_64B |
303bf215546Sopenharmony_ci                        CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
304bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, start));
305bf215546Sopenharmony_ci   }
306bf215546Sopenharmony_ci}
307bf215546Sopenharmony_ci
308bf215546Sopenharmony_cistatic void
309bf215546Sopenharmony_ciperfcntr_pause(struct fd_acc_query *aq, struct fd_batch *batch) assert_dt
310bf215546Sopenharmony_ci{
311bf215546Sopenharmony_ci   struct fd_batch_query_data *data = aq->query_data;
312bf215546Sopenharmony_ci   struct fd_screen *screen = data->screen;
313bf215546Sopenharmony_ci   struct fd_ringbuffer *ring = batch->draw;
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_ci   unsigned counters_per_group[screen->num_perfcntr_groups];
316bf215546Sopenharmony_ci   memset(counters_per_group, 0, sizeof(counters_per_group));
317bf215546Sopenharmony_ci
318bf215546Sopenharmony_ci   fd_wfi(batch, ring);
319bf215546Sopenharmony_ci
320bf215546Sopenharmony_ci   /* TODO do we need to bother to turn anything off? */
321bf215546Sopenharmony_ci
322bf215546Sopenharmony_ci   /* snapshot the end values: */
323bf215546Sopenharmony_ci   for (unsigned i = 0; i < data->num_query_entries; i++) {
324bf215546Sopenharmony_ci      struct fd_batch_query_entry *entry = &data->query_entries[i];
325bf215546Sopenharmony_ci      const struct fd_perfcntr_group *g = &screen->perfcntr_groups[entry->gid];
326bf215546Sopenharmony_ci      unsigned counter_idx = counters_per_group[entry->gid]++;
327bf215546Sopenharmony_ci      const struct fd_perfcntr_counter *counter = &g->counters[counter_idx];
328bf215546Sopenharmony_ci
329bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_REG_TO_MEM, 3);
330bf215546Sopenharmony_ci      OUT_RING(ring, CP_REG_TO_MEM_0_64B |
331bf215546Sopenharmony_ci                        CP_REG_TO_MEM_0_REG(counter->counter_reg_lo));
332bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, stop));
333bf215546Sopenharmony_ci   }
334bf215546Sopenharmony_ci
335bf215546Sopenharmony_ci   /* and compute the result: */
336bf215546Sopenharmony_ci   for (unsigned i = 0; i < data->num_query_entries; i++) {
337bf215546Sopenharmony_ci      /* result += stop - start: */
338bf215546Sopenharmony_ci      OUT_PKT7(ring, CP_MEM_TO_MEM, 9);
339bf215546Sopenharmony_ci      OUT_RING(ring, CP_MEM_TO_MEM_0_DOUBLE | CP_MEM_TO_MEM_0_NEG_C);
340bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, result)); /* dst */
341bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, result)); /* srcA */
342bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, stop));   /* srcB */
343bf215546Sopenharmony_ci      OUT_RELOC(ring, query_sample_idx(aq, i, start));  /* srcC */
344bf215546Sopenharmony_ci   }
345bf215546Sopenharmony_ci}
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_cistatic void
348bf215546Sopenharmony_ciperfcntr_accumulate_result(struct fd_acc_query *aq, void *buf,
349bf215546Sopenharmony_ci                           union pipe_query_result *result)
350bf215546Sopenharmony_ci{
351bf215546Sopenharmony_ci   struct fd_batch_query_data *data = aq->query_data;
352bf215546Sopenharmony_ci   struct fd5_query_sample *sp = buf;
353bf215546Sopenharmony_ci
354bf215546Sopenharmony_ci   for (unsigned i = 0; i < data->num_query_entries; i++) {
355bf215546Sopenharmony_ci      result->batch[i].u64 = sp[i].result;
356bf215546Sopenharmony_ci   }
357bf215546Sopenharmony_ci}
358bf215546Sopenharmony_ci
359bf215546Sopenharmony_cistatic const struct fd_acc_sample_provider perfcntr = {
360bf215546Sopenharmony_ci   .query_type = FD_QUERY_FIRST_PERFCNTR,
361bf215546Sopenharmony_ci   .always = true,
362bf215546Sopenharmony_ci   .resume = perfcntr_resume,
363bf215546Sopenharmony_ci   .pause = perfcntr_pause,
364bf215546Sopenharmony_ci   .result = perfcntr_accumulate_result,
365bf215546Sopenharmony_ci};
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_cistatic struct pipe_query *
368bf215546Sopenharmony_cifd5_create_batch_query(struct pipe_context *pctx, unsigned num_queries,
369bf215546Sopenharmony_ci                       unsigned *query_types)
370bf215546Sopenharmony_ci{
371bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
372bf215546Sopenharmony_ci   struct fd_screen *screen = ctx->screen;
373bf215546Sopenharmony_ci   struct fd_query *q;
374bf215546Sopenharmony_ci   struct fd_acc_query *aq;
375bf215546Sopenharmony_ci   struct fd_batch_query_data *data;
376bf215546Sopenharmony_ci
377bf215546Sopenharmony_ci   data = CALLOC_VARIANT_LENGTH_STRUCT(
378bf215546Sopenharmony_ci      fd_batch_query_data, num_queries * sizeof(data->query_entries[0]));
379bf215546Sopenharmony_ci
380bf215546Sopenharmony_ci   data->screen = screen;
381bf215546Sopenharmony_ci   data->num_query_entries = num_queries;
382bf215546Sopenharmony_ci
383bf215546Sopenharmony_ci   /* validate the requested query_types and ensure we don't try
384bf215546Sopenharmony_ci    * to request more query_types of a given group than we have
385bf215546Sopenharmony_ci    * counters:
386bf215546Sopenharmony_ci    */
387bf215546Sopenharmony_ci   unsigned counters_per_group[screen->num_perfcntr_groups];
388bf215546Sopenharmony_ci   memset(counters_per_group, 0, sizeof(counters_per_group));
389bf215546Sopenharmony_ci
390bf215546Sopenharmony_ci   for (unsigned i = 0; i < num_queries; i++) {
391bf215546Sopenharmony_ci      unsigned idx = query_types[i] - FD_QUERY_FIRST_PERFCNTR;
392bf215546Sopenharmony_ci
393bf215546Sopenharmony_ci      /* verify valid query_type, ie. is it actually a perfcntr? */
394bf215546Sopenharmony_ci      if ((query_types[i] < FD_QUERY_FIRST_PERFCNTR) ||
395bf215546Sopenharmony_ci          (idx >= screen->num_perfcntr_queries)) {
396bf215546Sopenharmony_ci         mesa_loge("invalid batch query query_type: %u", query_types[i]);
397bf215546Sopenharmony_ci         goto error;
398bf215546Sopenharmony_ci      }
399bf215546Sopenharmony_ci
400bf215546Sopenharmony_ci      struct fd_batch_query_entry *entry = &data->query_entries[i];
401bf215546Sopenharmony_ci      struct pipe_driver_query_info *pq = &screen->perfcntr_queries[idx];
402bf215546Sopenharmony_ci
403bf215546Sopenharmony_ci      entry->gid = pq->group_id;
404bf215546Sopenharmony_ci
405bf215546Sopenharmony_ci      /* the perfcntr_queries[] table flattens all the countables
406bf215546Sopenharmony_ci       * for each group in series, ie:
407bf215546Sopenharmony_ci       *
408bf215546Sopenharmony_ci       *   (G0,C0), .., (G0,Cn), (G1,C0), .., (G1,Cm), ...
409bf215546Sopenharmony_ci       *
410bf215546Sopenharmony_ci       * So to find the countable index just step back through the
411bf215546Sopenharmony_ci       * table to find the first entry with the same group-id.
412bf215546Sopenharmony_ci       */
413bf215546Sopenharmony_ci      while (pq > screen->perfcntr_queries) {
414bf215546Sopenharmony_ci         pq--;
415bf215546Sopenharmony_ci         if (pq->group_id == entry->gid)
416bf215546Sopenharmony_ci            entry->cid++;
417bf215546Sopenharmony_ci      }
418bf215546Sopenharmony_ci
419bf215546Sopenharmony_ci      if (counters_per_group[entry->gid] >=
420bf215546Sopenharmony_ci          screen->perfcntr_groups[entry->gid].num_counters) {
421bf215546Sopenharmony_ci         mesa_loge("too many counters for group %u\n", entry->gid);
422bf215546Sopenharmony_ci         goto error;
423bf215546Sopenharmony_ci      }
424bf215546Sopenharmony_ci
425bf215546Sopenharmony_ci      counters_per_group[entry->gid]++;
426bf215546Sopenharmony_ci   }
427bf215546Sopenharmony_ci
428bf215546Sopenharmony_ci   q = fd_acc_create_query2(ctx, 0, 0, &perfcntr);
429bf215546Sopenharmony_ci   aq = fd_acc_query(q);
430bf215546Sopenharmony_ci
431bf215546Sopenharmony_ci   /* sample buffer size is based on # of queries: */
432bf215546Sopenharmony_ci   aq->size = num_queries * sizeof(struct fd5_query_sample);
433bf215546Sopenharmony_ci   aq->query_data = data;
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci   return (struct pipe_query *)q;
436bf215546Sopenharmony_ci
437bf215546Sopenharmony_cierror:
438bf215546Sopenharmony_ci   free(data);
439bf215546Sopenharmony_ci   return NULL;
440bf215546Sopenharmony_ci}
441bf215546Sopenharmony_ci
442bf215546Sopenharmony_civoid
443bf215546Sopenharmony_cifd5_query_context_init(struct pipe_context *pctx) disable_thread_safety_analysis
444bf215546Sopenharmony_ci{
445bf215546Sopenharmony_ci   struct fd_context *ctx = fd_context(pctx);
446bf215546Sopenharmony_ci
447bf215546Sopenharmony_ci   ctx->create_query = fd_acc_create_query;
448bf215546Sopenharmony_ci   ctx->query_update_batch = fd_acc_query_update_batch;
449bf215546Sopenharmony_ci
450bf215546Sopenharmony_ci   pctx->create_batch_query = fd5_create_batch_query;
451bf215546Sopenharmony_ci
452bf215546Sopenharmony_ci   fd_acc_query_register_provider(pctx, &occlusion_counter);
453bf215546Sopenharmony_ci   fd_acc_query_register_provider(pctx, &occlusion_predicate);
454bf215546Sopenharmony_ci   fd_acc_query_register_provider(pctx, &occlusion_predicate_conservative);
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci   fd_acc_query_register_provider(pctx, &time_elapsed);
457bf215546Sopenharmony_ci   fd_acc_query_register_provider(pctx, &timestamp);
458bf215546Sopenharmony_ci}
459