1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
29 #include "pipe/p_state.h"
30 #include "util/u_memory.h"
31 #include "util/u_inlines.h"
33 #include "freedreno_query_hw.h"
34 #include "freedreno_context.h"
35 #include "freedreno_util.h"
37 struct fd_hw_sample_period
{
38 struct fd_hw_sample
*start
, *end
;
39 struct list_head list
;
42 /* maps query_type to sample provider idx: */
43 static int pidx(unsigned query_type
)
46 case PIPE_QUERY_OCCLUSION_COUNTER
:
48 case PIPE_QUERY_OCCLUSION_PREDICATE
:
55 static struct fd_hw_sample
*
56 get_sample(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
59 struct fd_hw_sample
*samp
= NULL
;
60 int idx
= pidx(query_type
);
62 if (!ctx
->sample_cache
[idx
]) {
63 ctx
->sample_cache
[idx
] =
64 ctx
->sample_providers
[idx
]->get_sample(ctx
, ring
);
67 fd_hw_sample_reference(ctx
, &samp
, ctx
->sample_cache
[idx
]);
73 clear_sample_cache(struct fd_context
*ctx
)
77 for (i
= 0; i
< ARRAY_SIZE(ctx
->sample_cache
); i
++)
78 fd_hw_sample_reference(ctx
, &ctx
->sample_cache
[i
], NULL
);
82 is_active(struct fd_hw_query
*hq
, enum fd_render_stage stage
)
84 return !!(hq
->provider
->active
& stage
);
89 resume_query(struct fd_context
*ctx
, struct fd_hw_query
*hq
,
90 struct fd_ringbuffer
*ring
)
92 int idx
= pidx(hq
->provider
->query_type
);
94 ctx
->active_providers
|= (1 << idx
);
95 hq
->period
= util_slab_alloc(&ctx
->sample_period_pool
);
96 list_inithead(&hq
->period
->list
);
97 hq
->period
->start
= get_sample(ctx
, ring
, hq
->base
.type
);
98 /* NOTE: util_slab_alloc() does not zero out the buffer: */
99 hq
->period
->end
= NULL
;
103 pause_query(struct fd_context
*ctx
, struct fd_hw_query
*hq
,
104 struct fd_ringbuffer
*ring
)
106 int idx
= pidx(hq
->provider
->query_type
);
107 assert(hq
->period
&& !hq
->period
->end
);
108 assert(ctx
->active_providers
& (1 << idx
));
109 hq
->period
->end
= get_sample(ctx
, ring
, hq
->base
.type
);
110 list_addtail(&hq
->period
->list
, &hq
->current_periods
);
115 destroy_periods(struct fd_context
*ctx
, struct list_head
*list
)
117 struct fd_hw_sample_period
*period
, *s
;
118 LIST_FOR_EACH_ENTRY_SAFE(period
, s
, list
, list
) {
119 fd_hw_sample_reference(ctx
, &period
->start
, NULL
);
120 fd_hw_sample_reference(ctx
, &period
->end
, NULL
);
121 list_del(&period
->list
);
122 util_slab_free(&ctx
->sample_period_pool
, period
);
127 fd_hw_destroy_query(struct fd_context
*ctx
, struct fd_query
*q
)
129 struct fd_hw_query
*hq
= fd_hw_query(q
);
131 destroy_periods(ctx
, &hq
->periods
);
132 destroy_periods(ctx
, &hq
->current_periods
);
139 fd_hw_begin_query(struct fd_context
*ctx
, struct fd_query
*q
)
141 struct fd_hw_query
*hq
= fd_hw_query(q
);
145 /* begin_query() should clear previous results: */
146 destroy_periods(ctx
, &hq
->periods
);
148 if (is_active(hq
, ctx
->stage
))
149 resume_query(ctx
, hq
, ctx
->ring
);
153 /* add to active list: */
155 list_addtail(&hq
->list
, &ctx
->active_queries
);
160 fd_hw_end_query(struct fd_context
*ctx
, struct fd_query
*q
)
162 struct fd_hw_query
*hq
= fd_hw_query(q
);
163 /* there are a couple special cases, which don't have
164 * a matching ->begin_query():
166 if (skip_begin_query(q
->type
) && !q
->active
) {
167 fd_hw_begin_query(ctx
, q
);
171 if (is_active(hq
, ctx
->stage
))
172 pause_query(ctx
, hq
, ctx
->ring
);
174 /* move to current list: */
176 list_addtail(&hq
->list
, &ctx
->current_queries
);
179 /* helper to get ptr to specified sample: */
180 static void * sampptr(struct fd_hw_sample
*samp
, uint32_t n
, void *ptr
)
182 return ((char *)ptr
) + (samp
->tile_stride
* n
) + samp
->offset
;
186 fd_hw_get_query_result(struct fd_context
*ctx
, struct fd_query
*q
,
187 boolean wait
, union pipe_query_result
*result
)
189 struct fd_hw_query
*hq
= fd_hw_query(q
);
190 const struct fd_hw_sample_provider
*p
= hq
->provider
;
191 struct fd_hw_sample_period
*period
;
196 /* if the app tries to read back the query result before the
197 * batch is submitted, that forces us to flush so that there
198 * are actually results to wait for:
200 if (!LIST_IS_EMPTY(&hq
->list
)) {
201 /* if app didn't actually trigger any cmdstream, then
202 * we have nothing to do:
204 if (!ctx
->needs_flush
)
206 DBG("reading query result forces flush!");
207 fd_context_render(&ctx
->base
);
210 util_query_clear_result(result
, q
->type
);
212 if (LIST_IS_EMPTY(&hq
->periods
))
215 assert(LIST_IS_EMPTY(&hq
->list
));
216 assert(LIST_IS_EMPTY(&hq
->current_periods
));
219 /* if !wait, then check the last sample (the one most likely to
220 * not be ready yet) and bail if it is not ready:
225 period
= LIST_ENTRY(struct fd_hw_sample_period
,
226 hq
->periods
.prev
, list
);
228 ret
= fd_bo_cpu_prep(period
->end
->bo
, ctx
->screen
->pipe
,
229 DRM_FREEDRENO_PREP_READ
| DRM_FREEDRENO_PREP_NOSYNC
);
233 fd_bo_cpu_fini(period
->end
->bo
);
236 /* sum the result across all sample periods: */
237 LIST_FOR_EACH_ENTRY(period
, &hq
->periods
, list
) {
238 struct fd_hw_sample
*start
= period
->start
;
239 struct fd_hw_sample
*end
= period
->end
;
242 /* start and end samples should be from same batch: */
243 assert(start
->bo
== end
->bo
);
244 assert(start
->num_tiles
== end
->num_tiles
);
246 for (i
= 0; i
< start
->num_tiles
; i
++) {
249 fd_bo_cpu_prep(start
->bo
, ctx
->screen
->pipe
,
250 DRM_FREEDRENO_PREP_READ
);
252 ptr
= fd_bo_map(start
->bo
);
254 p
->accumulate_result(ctx
, sampptr(period
->start
, i
, ptr
),
255 sampptr(period
->end
, i
, ptr
), result
);
257 fd_bo_cpu_fini(start
->bo
);
264 static const struct fd_query_funcs hw_query_funcs
= {
265 .destroy_query
= fd_hw_destroy_query
,
266 .begin_query
= fd_hw_begin_query
,
267 .end_query
= fd_hw_end_query
,
268 .get_query_result
= fd_hw_get_query_result
,
272 fd_hw_create_query(struct fd_context
*ctx
, unsigned query_type
)
274 struct fd_hw_query
*hq
;
276 int idx
= pidx(query_type
);
278 if ((idx
< 0) || !ctx
->sample_providers
[idx
])
281 hq
= CALLOC_STRUCT(fd_hw_query
);
285 hq
->provider
= ctx
->sample_providers
[idx
];
287 list_inithead(&hq
->periods
);
288 list_inithead(&hq
->current_periods
);
289 list_inithead(&hq
->list
);
292 q
->funcs
= &hw_query_funcs
;
293 q
->type
= query_type
;
298 struct fd_hw_sample
*
299 fd_hw_sample_init(struct fd_context
*ctx
, uint32_t size
)
301 struct fd_hw_sample
*samp
= util_slab_alloc(&ctx
->sample_pool
);
302 pipe_reference_init(&samp
->reference
, 1);
304 debug_assert(util_is_power_of_two(size
));
305 ctx
->next_sample_offset
= align(ctx
->next_sample_offset
, size
);
306 samp
->offset
= ctx
->next_sample_offset
;
307 /* NOTE: util_slab_alloc() does not zero out the buffer: */
310 samp
->tile_stride
= 0;
311 ctx
->next_sample_offset
+= size
;
316 __fd_hw_sample_destroy(struct fd_context
*ctx
, struct fd_hw_sample
*samp
)
320 util_slab_free(&ctx
->sample_pool
, samp
);
324 prepare_sample(struct fd_hw_sample
*samp
, struct fd_bo
*bo
,
325 uint32_t num_tiles
, uint32_t tile_stride
)
328 assert(samp
->bo
== bo
);
329 assert(samp
->num_tiles
== num_tiles
);
330 assert(samp
->tile_stride
== tile_stride
);
333 samp
->bo
= fd_bo_ref(bo
);
334 samp
->num_tiles
= num_tiles
;
335 samp
->tile_stride
= tile_stride
;
339 prepare_query(struct fd_hw_query
*hq
, struct fd_bo
*bo
,
340 uint32_t num_tiles
, uint32_t tile_stride
)
342 struct fd_hw_sample_period
*period
, *s
;
344 /* prepare all the samples in the query: */
345 LIST_FOR_EACH_ENTRY_SAFE(period
, s
, &hq
->current_periods
, list
) {
346 prepare_sample(period
->start
, bo
, num_tiles
, tile_stride
);
347 prepare_sample(period
->end
, bo
, num_tiles
, tile_stride
);
349 /* move from current_periods list to periods list: */
350 list_del(&period
->list
);
351 list_addtail(&period
->list
, &hq
->periods
);
356 prepare_queries(struct fd_context
*ctx
, struct fd_bo
*bo
,
357 uint32_t num_tiles
, uint32_t tile_stride
,
358 struct list_head
*list
, bool remove
)
360 struct fd_hw_query
*hq
, *s
;
361 LIST_FOR_EACH_ENTRY_SAFE(hq
, s
, list
, list
) {
362 prepare_query(hq
, bo
, num_tiles
, tile_stride
);
364 list_delinit(&hq
->list
);
368 /* called from gmem code once total storage requirements are known (ie.
369 * number of samples times number of tiles)
372 fd_hw_query_prepare(struct fd_context
*ctx
, uint32_t num_tiles
)
374 uint32_t tile_stride
= ctx
->next_sample_offset
;
378 fd_bo_del(ctx
->query_bo
);
380 if (tile_stride
> 0) {
381 bo
= fd_bo_new(ctx
->dev
, tile_stride
* num_tiles
,
382 DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
383 DRM_FREEDRENO_GEM_TYPE_KMEM
);
389 ctx
->query_tile_stride
= tile_stride
;
391 prepare_queries(ctx
, bo
, num_tiles
, tile_stride
,
392 &ctx
->active_queries
, false);
393 prepare_queries(ctx
, bo
, num_tiles
, tile_stride
,
394 &ctx
->current_queries
, true);
396 /* reset things for next batch: */
397 ctx
->next_sample_offset
= 0;
401 fd_hw_query_prepare_tile(struct fd_context
*ctx
, uint32_t n
,
402 struct fd_ringbuffer
*ring
)
404 uint32_t tile_stride
= ctx
->query_tile_stride
;
405 uint32_t offset
= tile_stride
* n
;
407 /* bail if no queries: */
408 if (tile_stride
== 0)
412 OUT_PKT0 (ring
, HW_QUERY_BASE_REG
, 1);
413 OUT_RELOCW(ring
, ctx
->query_bo
, offset
, 0, 0);
417 fd_hw_query_set_stage(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
418 enum fd_render_stage stage
)
420 /* special case: internal blits (like mipmap level generation)
421 * go through normal draw path (via util_blitter_blit()).. but
422 * we need to ignore the FD_STAGE_DRAW which will be set, so we
423 * don't enable queries which should be paused during internal
426 if ((ctx
->stage
== FD_STAGE_BLIT
) &&
427 (stage
!= FD_STAGE_NULL
))
430 if (stage
!= ctx
->stage
) {
431 struct fd_hw_query
*hq
;
432 LIST_FOR_EACH_ENTRY(hq
, &ctx
->active_queries
, list
) {
433 bool was_active
= is_active(hq
, ctx
->stage
);
434 bool now_active
= is_active(hq
, stage
);
436 if (now_active
&& !was_active
)
437 resume_query(ctx
, hq
, ring
);
438 else if (was_active
&& !now_active
)
439 pause_query(ctx
, hq
, ring
);
442 clear_sample_cache(ctx
);
446 /* call the provider->enable() for all the hw queries that were active
447 * in the current batch. This sets up perfctr selector regs statically
448 * for the duration of the batch.
451 fd_hw_query_enable(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
)
453 for (int idx
= 0; idx
< MAX_HW_SAMPLE_PROVIDERS
; idx
++) {
454 if (ctx
->active_providers
& (1 << idx
)) {
455 assert(ctx
->sample_providers
[idx
]);
456 if (ctx
->sample_providers
[idx
]->enable
)
457 ctx
->sample_providers
[idx
]->enable(ctx
, ring
);
460 ctx
->active_providers
= 0; /* clear it for next frame */
464 fd_hw_query_register_provider(struct pipe_context
*pctx
,
465 const struct fd_hw_sample_provider
*provider
)
467 struct fd_context
*ctx
= fd_context(pctx
);
468 int idx
= pidx(provider
->query_type
);
470 assert((0 <= idx
) && (idx
< MAX_HW_SAMPLE_PROVIDERS
));
471 assert(!ctx
->sample_providers
[idx
]);
473 ctx
->sample_providers
[idx
] = provider
;
477 fd_hw_query_init(struct pipe_context
*pctx
)
479 struct fd_context
*ctx
= fd_context(pctx
);
481 util_slab_create(&ctx
->sample_pool
, sizeof(struct fd_hw_sample
),
482 16, UTIL_SLAB_SINGLETHREADED
);
483 util_slab_create(&ctx
->sample_period_pool
, sizeof(struct fd_hw_sample_period
),
484 16, UTIL_SLAB_SINGLETHREADED
);
485 list_inithead(&ctx
->active_queries
);
486 list_inithead(&ctx
->current_queries
);
490 fd_hw_query_fini(struct pipe_context
*pctx
)
492 struct fd_context
*ctx
= fd_context(pctx
);
494 util_slab_destroy(&ctx
->sample_pool
);
495 util_slab_destroy(&ctx
->sample_period_pool
);