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
:
50 case PIPE_QUERY_TIME_ELAPSED
:
57 static struct fd_hw_sample
*
58 get_sample(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
61 struct fd_hw_sample
*samp
= NULL
;
62 int idx
= pidx(query_type
);
64 assume(idx
>= 0); /* query never would have been created otherwise */
66 if (!ctx
->sample_cache
[idx
]) {
67 ctx
->sample_cache
[idx
] =
68 ctx
->sample_providers
[idx
]->get_sample(ctx
, ring
);
69 ctx
->needs_flush
= true;
72 fd_hw_sample_reference(ctx
, &samp
, ctx
->sample_cache
[idx
]);
78 clear_sample_cache(struct fd_context
*ctx
)
82 for (i
= 0; i
< ARRAY_SIZE(ctx
->sample_cache
); i
++)
83 fd_hw_sample_reference(ctx
, &ctx
->sample_cache
[i
], NULL
);
87 is_active(struct fd_hw_query
*hq
, enum fd_render_stage stage
)
89 return !!(hq
->provider
->active
& stage
);
94 resume_query(struct fd_context
*ctx
, struct fd_hw_query
*hq
,
95 struct fd_ringbuffer
*ring
)
97 int idx
= pidx(hq
->provider
->query_type
);
98 assert(idx
>= 0); /* query never would have been created otherwise */
100 ctx
->active_providers
|= (1 << idx
);
101 hq
->period
= util_slab_alloc(&ctx
->sample_period_pool
);
102 list_inithead(&hq
->period
->list
);
103 hq
->period
->start
= get_sample(ctx
, ring
, hq
->base
.type
);
104 /* NOTE: util_slab_alloc() does not zero out the buffer: */
105 hq
->period
->end
= NULL
;
109 pause_query(struct fd_context
*ctx
, struct fd_hw_query
*hq
,
110 struct fd_ringbuffer
*ring
)
112 int idx
= pidx(hq
->provider
->query_type
);
113 assert(idx
>= 0); /* query never would have been created otherwise */
114 assert(hq
->period
&& !hq
->period
->end
);
115 assert(ctx
->active_providers
& (1 << idx
));
116 hq
->period
->end
= get_sample(ctx
, ring
, hq
->base
.type
);
117 list_addtail(&hq
->period
->list
, &hq
->current_periods
);
122 destroy_periods(struct fd_context
*ctx
, struct list_head
*list
)
124 struct fd_hw_sample_period
*period
, *s
;
125 LIST_FOR_EACH_ENTRY_SAFE(period
, s
, list
, list
) {
126 fd_hw_sample_reference(ctx
, &period
->start
, NULL
);
127 fd_hw_sample_reference(ctx
, &period
->end
, NULL
);
128 list_del(&period
->list
);
129 util_slab_free(&ctx
->sample_period_pool
, period
);
134 fd_hw_destroy_query(struct fd_context
*ctx
, struct fd_query
*q
)
136 struct fd_hw_query
*hq
= fd_hw_query(q
);
138 destroy_periods(ctx
, &hq
->periods
);
139 destroy_periods(ctx
, &hq
->current_periods
);
146 fd_hw_begin_query(struct fd_context
*ctx
, struct fd_query
*q
)
148 struct fd_hw_query
*hq
= fd_hw_query(q
);
152 /* begin_query() should clear previous results: */
153 destroy_periods(ctx
, &hq
->periods
);
155 if (is_active(hq
, ctx
->stage
))
156 resume_query(ctx
, hq
, ctx
->ring
);
160 /* add to active list: */
162 list_addtail(&hq
->list
, &ctx
->active_queries
);
167 fd_hw_end_query(struct fd_context
*ctx
, struct fd_query
*q
)
169 struct fd_hw_query
*hq
= fd_hw_query(q
);
170 /* there are a couple special cases, which don't have
171 * a matching ->begin_query():
173 if (skip_begin_query(q
->type
) && !q
->active
) {
174 fd_hw_begin_query(ctx
, q
);
178 if (is_active(hq
, ctx
->stage
))
179 pause_query(ctx
, hq
, ctx
->ring
);
181 /* move to current list: */
183 list_addtail(&hq
->list
, &ctx
->current_queries
);
186 /* helper to get ptr to specified sample: */
187 static void * sampptr(struct fd_hw_sample
*samp
, uint32_t n
, void *ptr
)
189 return ((char *)ptr
) + (samp
->tile_stride
* n
) + samp
->offset
;
193 fd_hw_get_query_result(struct fd_context
*ctx
, struct fd_query
*q
,
194 boolean wait
, union pipe_query_result
*result
)
196 struct fd_hw_query
*hq
= fd_hw_query(q
);
197 const struct fd_hw_sample_provider
*p
= hq
->provider
;
198 struct fd_hw_sample_period
*period
;
203 /* if the app tries to read back the query result before the
204 * batch is submitted, that forces us to flush so that there
205 * are actually results to wait for:
207 if (!LIST_IS_EMPTY(&hq
->list
)) {
208 /* if app didn't actually trigger any cmdstream, then
209 * we have nothing to do:
211 if (!ctx
->needs_flush
)
213 DBG("reading query result forces flush!");
214 fd_context_render(&ctx
->base
);
217 util_query_clear_result(result
, q
->type
);
219 if (LIST_IS_EMPTY(&hq
->periods
))
222 assert(LIST_IS_EMPTY(&hq
->list
));
223 assert(LIST_IS_EMPTY(&hq
->current_periods
));
226 /* if !wait, then check the last sample (the one most likely to
227 * not be ready yet) and bail if it is not ready:
232 period
= LIST_ENTRY(struct fd_hw_sample_period
,
233 hq
->periods
.prev
, list
);
235 ret
= fd_bo_cpu_prep(period
->end
->bo
, ctx
->screen
->pipe
,
236 DRM_FREEDRENO_PREP_READ
| DRM_FREEDRENO_PREP_NOSYNC
);
240 fd_bo_cpu_fini(period
->end
->bo
);
243 /* sum the result across all sample periods: */
244 LIST_FOR_EACH_ENTRY(period
, &hq
->periods
, list
) {
245 struct fd_hw_sample
*start
= period
->start
;
246 struct fd_hw_sample
*end
= period
->end
;
249 /* start and end samples should be from same batch: */
250 assert(start
->bo
== end
->bo
);
251 assert(start
->num_tiles
== end
->num_tiles
);
253 for (i
= 0; i
< start
->num_tiles
; i
++) {
256 fd_bo_cpu_prep(start
->bo
, ctx
->screen
->pipe
,
257 DRM_FREEDRENO_PREP_READ
);
259 ptr
= fd_bo_map(start
->bo
);
261 p
->accumulate_result(ctx
, sampptr(period
->start
, i
, ptr
),
262 sampptr(period
->end
, i
, ptr
), result
);
264 fd_bo_cpu_fini(start
->bo
);
271 static const struct fd_query_funcs hw_query_funcs
= {
272 .destroy_query
= fd_hw_destroy_query
,
273 .begin_query
= fd_hw_begin_query
,
274 .end_query
= fd_hw_end_query
,
275 .get_query_result
= fd_hw_get_query_result
,
279 fd_hw_create_query(struct fd_context
*ctx
, unsigned query_type
)
281 struct fd_hw_query
*hq
;
283 int idx
= pidx(query_type
);
285 if ((idx
< 0) || !ctx
->sample_providers
[idx
])
288 hq
= CALLOC_STRUCT(fd_hw_query
);
292 hq
->provider
= ctx
->sample_providers
[idx
];
294 list_inithead(&hq
->periods
);
295 list_inithead(&hq
->current_periods
);
296 list_inithead(&hq
->list
);
299 q
->funcs
= &hw_query_funcs
;
300 q
->type
= query_type
;
305 struct fd_hw_sample
*
306 fd_hw_sample_init(struct fd_context
*ctx
, uint32_t size
)
308 struct fd_hw_sample
*samp
= util_slab_alloc(&ctx
->sample_pool
);
309 pipe_reference_init(&samp
->reference
, 1);
311 debug_assert(util_is_power_of_two(size
));
312 ctx
->next_sample_offset
= align(ctx
->next_sample_offset
, size
);
313 samp
->offset
= ctx
->next_sample_offset
;
314 /* NOTE: util_slab_alloc() does not zero out the buffer: */
317 samp
->tile_stride
= 0;
318 ctx
->next_sample_offset
+= size
;
323 __fd_hw_sample_destroy(struct fd_context
*ctx
, struct fd_hw_sample
*samp
)
327 util_slab_free(&ctx
->sample_pool
, samp
);
331 prepare_sample(struct fd_hw_sample
*samp
, struct fd_bo
*bo
,
332 uint32_t num_tiles
, uint32_t tile_stride
)
335 assert(samp
->bo
== bo
);
336 assert(samp
->num_tiles
== num_tiles
);
337 assert(samp
->tile_stride
== tile_stride
);
340 samp
->bo
= fd_bo_ref(bo
);
341 samp
->num_tiles
= num_tiles
;
342 samp
->tile_stride
= tile_stride
;
346 prepare_query(struct fd_hw_query
*hq
, struct fd_bo
*bo
,
347 uint32_t num_tiles
, uint32_t tile_stride
)
349 struct fd_hw_sample_period
*period
, *s
;
351 /* prepare all the samples in the query: */
352 LIST_FOR_EACH_ENTRY_SAFE(period
, s
, &hq
->current_periods
, list
) {
353 prepare_sample(period
->start
, bo
, num_tiles
, tile_stride
);
354 prepare_sample(period
->end
, bo
, num_tiles
, tile_stride
);
356 /* move from current_periods list to periods list: */
357 list_del(&period
->list
);
358 list_addtail(&period
->list
, &hq
->periods
);
363 prepare_queries(struct fd_context
*ctx
, struct fd_bo
*bo
,
364 uint32_t num_tiles
, uint32_t tile_stride
,
365 struct list_head
*list
, bool remove
)
367 struct fd_hw_query
*hq
, *s
;
368 LIST_FOR_EACH_ENTRY_SAFE(hq
, s
, list
, list
) {
369 prepare_query(hq
, bo
, num_tiles
, tile_stride
);
371 list_delinit(&hq
->list
);
375 /* called from gmem code once total storage requirements are known (ie.
376 * number of samples times number of tiles)
379 fd_hw_query_prepare(struct fd_context
*ctx
, uint32_t num_tiles
)
381 uint32_t tile_stride
= ctx
->next_sample_offset
;
385 fd_bo_del(ctx
->query_bo
);
387 if (tile_stride
> 0) {
388 bo
= fd_bo_new(ctx
->dev
, tile_stride
* num_tiles
,
389 DRM_FREEDRENO_GEM_CACHE_WCOMBINE
|
390 DRM_FREEDRENO_GEM_TYPE_KMEM
);
396 ctx
->query_tile_stride
= tile_stride
;
398 prepare_queries(ctx
, bo
, num_tiles
, tile_stride
,
399 &ctx
->active_queries
, false);
400 prepare_queries(ctx
, bo
, num_tiles
, tile_stride
,
401 &ctx
->current_queries
, true);
403 /* reset things for next batch: */
404 ctx
->next_sample_offset
= 0;
408 fd_hw_query_prepare_tile(struct fd_context
*ctx
, uint32_t n
,
409 struct fd_ringbuffer
*ring
)
411 uint32_t tile_stride
= ctx
->query_tile_stride
;
412 uint32_t offset
= tile_stride
* n
;
414 /* bail if no queries: */
415 if (tile_stride
== 0)
419 OUT_PKT0 (ring
, HW_QUERY_BASE_REG
, 1);
420 OUT_RELOCW(ring
, ctx
->query_bo
, offset
, 0, 0);
424 fd_hw_query_set_stage(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
,
425 enum fd_render_stage stage
)
427 /* special case: internal blits (like mipmap level generation)
428 * go through normal draw path (via util_blitter_blit()).. but
429 * we need to ignore the FD_STAGE_DRAW which will be set, so we
430 * don't enable queries which should be paused during internal
433 if ((ctx
->stage
== FD_STAGE_BLIT
) &&
434 (stage
!= FD_STAGE_NULL
))
437 if (stage
!= ctx
->stage
) {
438 struct fd_hw_query
*hq
;
439 LIST_FOR_EACH_ENTRY(hq
, &ctx
->active_queries
, list
) {
440 bool was_active
= is_active(hq
, ctx
->stage
);
441 bool now_active
= is_active(hq
, stage
);
443 if (now_active
&& !was_active
)
444 resume_query(ctx
, hq
, ring
);
445 else if (was_active
&& !now_active
)
446 pause_query(ctx
, hq
, ring
);
449 clear_sample_cache(ctx
);
453 /* call the provider->enable() for all the hw queries that were active
454 * in the current batch. This sets up perfctr selector regs statically
455 * for the duration of the batch.
458 fd_hw_query_enable(struct fd_context
*ctx
, struct fd_ringbuffer
*ring
)
460 for (int idx
= 0; idx
< MAX_HW_SAMPLE_PROVIDERS
; idx
++) {
461 if (ctx
->active_providers
& (1 << idx
)) {
462 assert(ctx
->sample_providers
[idx
]);
463 if (ctx
->sample_providers
[idx
]->enable
)
464 ctx
->sample_providers
[idx
]->enable(ctx
, ring
);
467 ctx
->active_providers
= 0; /* clear it for next frame */
471 fd_hw_query_register_provider(struct pipe_context
*pctx
,
472 const struct fd_hw_sample_provider
*provider
)
474 struct fd_context
*ctx
= fd_context(pctx
);
475 int idx
= pidx(provider
->query_type
);
477 assert((0 <= idx
) && (idx
< MAX_HW_SAMPLE_PROVIDERS
));
478 assert(!ctx
->sample_providers
[idx
]);
480 ctx
->sample_providers
[idx
] = provider
;
484 fd_hw_query_init(struct pipe_context
*pctx
)
486 struct fd_context
*ctx
= fd_context(pctx
);
488 util_slab_create(&ctx
->sample_pool
, sizeof(struct fd_hw_sample
),
489 16, UTIL_SLAB_SINGLETHREADED
);
490 util_slab_create(&ctx
->sample_period_pool
, sizeof(struct fd_hw_sample_period
),
491 16, UTIL_SLAB_SINGLETHREADED
);
492 list_inithead(&ctx
->active_queries
);
493 list_inithead(&ctx
->current_queries
);
497 fd_hw_query_fini(struct pipe_context
*pctx
)
499 struct fd_context
*ctx
= fd_context(pctx
);
501 util_slab_destroy(&ctx
->sample_pool
);
502 util_slab_destroy(&ctx
->sample_period_pool
);