xref: /aosp_15_r20/external/mesa3d/src/gallium/drivers/freedreno/freedreno_query_hw.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright © 2014 Rob Clark <[email protected]>
3  * SPDX-License-Identifier: MIT
4  *
5  * Authors:
6  *    Rob Clark <[email protected]>
7  */
8 
9 #include "pipe/p_state.h"
10 #include "util/u_inlines.h"
11 #include "util/u_memory.h"
12 
13 #include "freedreno_context.h"
14 #include "freedreno_query_hw.h"
15 #include "freedreno_resource.h"
16 #include "freedreno_util.h"
17 
18 struct fd_hw_sample_period {
19    struct fd_hw_sample *start, *end;
20    struct list_head list;
21 };
22 
23 static struct fd_hw_sample *
get_sample(struct fd_batch * batch,struct fd_ringbuffer * ring,unsigned query_type)24 get_sample(struct fd_batch *batch, struct fd_ringbuffer *ring,
25            unsigned query_type) assert_dt
26 {
27    struct fd_context *ctx = batch->ctx;
28    struct fd_hw_sample *samp = NULL;
29    int idx = pidx(query_type);
30 
31    assume(idx >= 0); /* query never would have been created otherwise */
32 
33    if (!batch->sample_cache[idx]) {
34       struct fd_hw_sample *new_samp =
35          ctx->hw_sample_providers[idx]->get_sample(batch, ring);
36       fd_hw_sample_reference(ctx, &batch->sample_cache[idx], new_samp);
37       util_dynarray_append(&batch->samples, struct fd_hw_sample *, new_samp);
38       fd_batch_needs_flush(batch);
39    }
40 
41    fd_hw_sample_reference(ctx, &samp, batch->sample_cache[idx]);
42 
43    return samp;
44 }
45 
46 static void
clear_sample_cache(struct fd_batch * batch)47 clear_sample_cache(struct fd_batch *batch)
48 {
49    int i;
50 
51    for (i = 0; i < ARRAY_SIZE(batch->sample_cache); i++)
52       fd_hw_sample_reference(batch->ctx, &batch->sample_cache[i], NULL);
53 }
54 
55 static bool
query_active_in_batch(struct fd_batch * batch,struct fd_hw_query * hq)56 query_active_in_batch(struct fd_batch *batch, struct fd_hw_query *hq)
57 {
58    int idx = pidx(hq->provider->query_type);
59    return batch->query_providers_active & (1 << idx);
60 }
61 
62 static void
resume_query(struct fd_batch * batch,struct fd_hw_query * hq,struct fd_ringbuffer * ring)63 resume_query(struct fd_batch *batch, struct fd_hw_query *hq,
64              struct fd_ringbuffer *ring) assert_dt
65 {
66    int idx = pidx(hq->provider->query_type);
67    DBG("%p", hq);
68    assert(idx >= 0); /* query never would have been created otherwise */
69    assert(!hq->period);
70    batch->query_providers_used |= (1 << idx);
71    batch->query_providers_active |= (1 << idx);
72    hq->period = slab_alloc_st(&batch->ctx->sample_period_pool);
73    list_inithead(&hq->period->list);
74    hq->period->start = get_sample(batch, ring, hq->base.type);
75    /* NOTE: slab_alloc_st() does not zero out the buffer: */
76    hq->period->end = NULL;
77 }
78 
79 static void
pause_query(struct fd_batch * batch,struct fd_hw_query * hq,struct fd_ringbuffer * ring)80 pause_query(struct fd_batch *batch, struct fd_hw_query *hq,
81             struct fd_ringbuffer *ring) assert_dt
82 {
83    ASSERTED int idx = pidx(hq->provider->query_type);
84    DBG("%p", hq);
85    assert(idx >= 0); /* query never would have been created otherwise */
86    assert(hq->period && !hq->period->end);
87    assert(query_active_in_batch(batch, hq));
88    batch->query_providers_active &= ~(1 << idx);
89    hq->period->end = get_sample(batch, ring, hq->base.type);
90    list_addtail(&hq->period->list, &hq->periods);
91    hq->period = NULL;
92 }
93 
94 static void
destroy_periods(struct fd_context * ctx,struct fd_hw_query * hq)95 destroy_periods(struct fd_context *ctx, struct fd_hw_query *hq)
96 {
97    struct fd_hw_sample_period *period, *s;
98    LIST_FOR_EACH_ENTRY_SAFE (period, s, &hq->periods, list) {
99       fd_hw_sample_reference(ctx, &period->start, NULL);
100       fd_hw_sample_reference(ctx, &period->end, NULL);
101       list_del(&period->list);
102       slab_free_st(&ctx->sample_period_pool, period);
103    }
104 }
105 
106 static void
fd_hw_destroy_query(struct fd_context * ctx,struct fd_query * q)107 fd_hw_destroy_query(struct fd_context *ctx, struct fd_query *q)
108 {
109    struct fd_hw_query *hq = fd_hw_query(q);
110 
111    DBG("%p", q);
112 
113    destroy_periods(ctx, hq);
114    list_del(&hq->list);
115 
116    free(hq);
117 }
118 
119 static void
fd_hw_begin_query(struct fd_context * ctx,struct fd_query * q)120 fd_hw_begin_query(struct fd_context *ctx, struct fd_query *q) assert_dt
121 {
122    struct fd_batch *batch = fd_context_batch(ctx);
123    struct fd_hw_query *hq = fd_hw_query(q);
124 
125    DBG("%p", q);
126 
127    /* begin_query() should clear previous results: */
128    destroy_periods(ctx, hq);
129 
130    if (batch && (ctx->active_queries || hq->provider->always))
131       resume_query(batch, hq, batch->draw);
132 
133    /* add to active list: */
134    assert(list_is_empty(&hq->list));
135    list_addtail(&hq->list, &ctx->hw_active_queries);
136 
137    fd_batch_reference(&batch, NULL);
138 }
139 
140 static void
fd_hw_end_query(struct fd_context * ctx,struct fd_query * q)141 fd_hw_end_query(struct fd_context *ctx, struct fd_query *q) assert_dt
142 {
143    struct fd_batch *batch = fd_context_batch(ctx);
144    struct fd_hw_query *hq = fd_hw_query(q);
145 
146    DBG("%p", q);
147 
148    if (batch && (ctx->active_queries || hq->provider->always))
149       pause_query(batch, hq, batch->draw);
150 
151    /* remove from active list: */
152    list_delinit(&hq->list);
153 
154    fd_batch_reference(&batch, NULL);
155 }
156 
157 /* helper to get ptr to specified sample: */
158 static void *
sampptr(struct fd_hw_sample * samp,uint32_t n,void * ptr)159 sampptr(struct fd_hw_sample *samp, uint32_t n, void *ptr)
160 {
161    return ((char *)ptr) + (samp->tile_stride * n) + samp->offset;
162 }
163 
164 static bool
fd_hw_get_query_result(struct fd_context * ctx,struct fd_query * q,bool wait,union pipe_query_result * result)165 fd_hw_get_query_result(struct fd_context *ctx, struct fd_query *q, bool wait,
166                        union pipe_query_result *result)
167 {
168    struct fd_hw_query *hq = fd_hw_query(q);
169    const struct fd_hw_sample_provider *p = hq->provider;
170    struct fd_hw_sample_period *period, *tmp;
171 
172    DBG("%p: wait=%d", q, wait);
173 
174    if (list_is_empty(&hq->periods))
175       return true;
176 
177    assert(list_is_empty(&hq->list));
178    assert(!hq->period);
179 
180    /* sum the result across all sample periods.  Start with the last period
181     * so that no-wait will bail quickly.
182     */
183    LIST_FOR_EACH_ENTRY_SAFE_REV (period, tmp, &hq->periods, list) {
184       struct fd_hw_sample *start = period->start;
185       ASSERTED struct fd_hw_sample *end = period->end;
186       unsigned i;
187 
188       /* start and end samples should be from same batch: */
189       assert(start->prsc == end->prsc);
190       assert(start->num_tiles == end->num_tiles);
191 
192       struct fd_resource *rsc = fd_resource(start->prsc);
193 
194       /* ARB_occlusion_query says:
195        *
196        *     "Querying the state for a given occlusion query forces that
197        *      occlusion query to complete within a finite amount of time."
198        *
199        * So, regardless of whether we are supposed to wait or not, we do need to
200        * flush now.
201        */
202       if (fd_get_query_result_in_driver_thread(q)) {
203          tc_assert_driver_thread(ctx->tc);
204          fd_context_access_begin(ctx);
205          fd_bc_flush_writer(ctx, rsc);
206          fd_context_access_end(ctx);
207       }
208 
209       /* some piglit tests at least do query with no draws, I guess: */
210       if (!rsc->bo)
211          continue;
212 
213       if (!wait) {
214          int ret = fd_resource_wait(
215             ctx, rsc, FD_BO_PREP_READ | FD_BO_PREP_NOSYNC | FD_BO_PREP_FLUSH);
216          if (ret)
217             return false;
218       } else {
219          fd_resource_wait(ctx, rsc, FD_BO_PREP_READ);
220       }
221 
222       void *ptr = fd_bo_map(rsc->bo);
223 
224       for (i = 0; i < start->num_tiles; i++) {
225          p->accumulate_result(ctx, sampptr(period->start, i, ptr),
226                               sampptr(period->end, i, ptr), result);
227       }
228    }
229 
230    return true;
231 }
232 
233 static const struct fd_query_funcs hw_query_funcs = {
234    .destroy_query = fd_hw_destroy_query,
235    .begin_query = fd_hw_begin_query,
236    .end_query = fd_hw_end_query,
237    .get_query_result = fd_hw_get_query_result,
238 };
239 
240 struct fd_query *
fd_hw_create_query(struct fd_context * ctx,unsigned query_type,unsigned index)241 fd_hw_create_query(struct fd_context *ctx, unsigned query_type, unsigned index)
242 {
243    struct fd_hw_query *hq;
244    struct fd_query *q;
245    int idx = pidx(query_type);
246 
247    if ((idx < 0) || !ctx->hw_sample_providers[idx])
248       return NULL;
249 
250    hq = CALLOC_STRUCT(fd_hw_query);
251    if (!hq)
252       return NULL;
253 
254    DBG("%p: query_type=%u", hq, query_type);
255 
256    hq->provider = ctx->hw_sample_providers[idx];
257 
258    list_inithead(&hq->periods);
259    list_inithead(&hq->list);
260 
261    q = &hq->base;
262    q->funcs = &hw_query_funcs;
263    q->type = query_type;
264    q->index = index;
265 
266    return q;
267 }
268 
269 struct fd_hw_sample *
fd_hw_sample_init(struct fd_batch * batch,uint32_t size)270 fd_hw_sample_init(struct fd_batch *batch, uint32_t size)
271 {
272    struct fd_hw_sample *samp = slab_alloc_st(&batch->ctx->sample_pool);
273    pipe_reference_init(&samp->reference, 1);
274    samp->size = size;
275    assert(util_is_power_of_two_or_zero(size));
276    batch->next_sample_offset = align(batch->next_sample_offset, size);
277    samp->offset = batch->next_sample_offset;
278    /* NOTE: slab_alloc_st() does not zero out the buffer: */
279    samp->prsc = NULL;
280    samp->num_tiles = 0;
281    samp->tile_stride = 0;
282    batch->next_sample_offset += size;
283 
284    pipe_resource_reference(&samp->prsc, batch->query_buf);
285 
286    return samp;
287 }
288 
289 void
__fd_hw_sample_destroy(struct fd_context * ctx,struct fd_hw_sample * samp)290 __fd_hw_sample_destroy(struct fd_context *ctx, struct fd_hw_sample *samp)
291 {
292    pipe_resource_reference(&samp->prsc, NULL);
293    slab_free_st(&ctx->sample_pool, samp);
294 }
295 
296 /* called from gmem code once total storage requirements are known (ie.
297  * number of samples times number of tiles)
298  */
299 void
fd_hw_query_prepare(struct fd_batch * batch,uint32_t num_tiles)300 fd_hw_query_prepare(struct fd_batch *batch, uint32_t num_tiles)
301 {
302    uint32_t tile_stride = batch->next_sample_offset;
303 
304    if (tile_stride > 0)
305       fd_resource_resize(batch->query_buf, tile_stride * num_tiles);
306 
307    batch->query_tile_stride = tile_stride;
308 
309    while (batch->samples.size > 0) {
310       struct fd_hw_sample *samp =
311          util_dynarray_pop(&batch->samples, struct fd_hw_sample *);
312       samp->num_tiles = num_tiles;
313       samp->tile_stride = tile_stride;
314       fd_hw_sample_reference(batch->ctx, &samp, NULL);
315    }
316 
317    /* reset things for next batch: */
318    batch->next_sample_offset = 0;
319 }
320 
321 void
fd_hw_query_prepare_tile(struct fd_batch * batch,uint32_t n,struct fd_ringbuffer * ring)322 fd_hw_query_prepare_tile(struct fd_batch *batch, uint32_t n,
323                          struct fd_ringbuffer *ring)
324 {
325    uint32_t tile_stride = batch->query_tile_stride;
326    uint32_t offset = tile_stride * n;
327 
328    /* bail if no queries: */
329    if (tile_stride == 0)
330       return;
331 
332    fd_wfi(batch, ring);
333    OUT_PKT0(ring, HW_QUERY_BASE_REG, 1);
334    OUT_RELOC(ring, fd_resource(batch->query_buf)->bo, offset, 0, 0);
335 }
336 
337 void
fd_hw_query_update_batch(struct fd_batch * batch,bool disable_all)338 fd_hw_query_update_batch(struct fd_batch *batch, bool disable_all)
339 {
340    struct fd_context *ctx = batch->ctx;
341 
342    if (disable_all || (ctx->dirty & FD_DIRTY_QUERY)) {
343       struct fd_hw_query *hq;
344       LIST_FOR_EACH_ENTRY (hq, &batch->ctx->hw_active_queries, list) {
345          bool was_active = query_active_in_batch(batch, hq);
346          bool now_active =
347             !disable_all && (ctx->active_queries || hq->provider->always);
348 
349          if (now_active && !was_active)
350             resume_query(batch, hq, batch->draw);
351          else if (was_active && !now_active)
352             pause_query(batch, hq, batch->draw);
353       }
354    }
355    clear_sample_cache(batch);
356 }
357 
358 /* call the provider->enable() for all the hw queries that were active
359  * in the current batch.  This sets up perfctr selector regs statically
360  * for the duration of the batch.
361  */
362 void
fd_hw_query_enable(struct fd_batch * batch,struct fd_ringbuffer * ring)363 fd_hw_query_enable(struct fd_batch *batch, struct fd_ringbuffer *ring)
364 {
365    struct fd_context *ctx = batch->ctx;
366    for (int idx = 0; idx < MAX_HW_SAMPLE_PROVIDERS; idx++) {
367       if (batch->query_providers_used & (1 << idx)) {
368          assert(ctx->hw_sample_providers[idx]);
369          if (ctx->hw_sample_providers[idx]->enable)
370             ctx->hw_sample_providers[idx]->enable(ctx, ring);
371       }
372    }
373 }
374 
375 void
fd_hw_query_register_provider(struct pipe_context * pctx,const struct fd_hw_sample_provider * provider)376 fd_hw_query_register_provider(struct pipe_context *pctx,
377                               const struct fd_hw_sample_provider *provider)
378 {
379    struct fd_context *ctx = fd_context(pctx);
380    int idx = pidx(provider->query_type);
381 
382    assert((0 <= idx) && (idx < MAX_HW_SAMPLE_PROVIDERS));
383    assert(!ctx->hw_sample_providers[idx]);
384 
385    ctx->hw_sample_providers[idx] = provider;
386 }
387 
388 void
fd_hw_query_init(struct pipe_context * pctx)389 fd_hw_query_init(struct pipe_context *pctx)
390 {
391    struct fd_context *ctx = fd_context(pctx);
392 
393    slab_create(&ctx->sample_pool, sizeof(struct fd_hw_sample), 16);
394    slab_create(&ctx->sample_period_pool, sizeof(struct fd_hw_sample_period),
395                16);
396 }
397 
398 void
fd_hw_query_fini(struct pipe_context * pctx)399 fd_hw_query_fini(struct pipe_context *pctx)
400 {
401    struct fd_context *ctx = fd_context(pctx);
402 
403    slab_destroy(&ctx->sample_pool);
404    slab_destroy(&ctx->sample_period_pool);
405 }
406