1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2014 Rob Clark <robclark@freedesktop.org>
5 *
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:
12 *
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
15 * Software.
16 *
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
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
29#ifndef FREEDRENO_QUERY_HW_H_
30#define FREEDRENO_QUERY_HW_H_
31
32#include "util/list.h"
33
34#include "freedreno_query.h"
35#include "freedreno_context.h"
36
37
38/*
39 * HW Queries:
40 *
41 * See: https://github.com/freedreno/freedreno/wiki/Queries#hardware-queries
42 *
43 * Hardware queries will be specific to gpu generation, but they need
44 * some common infrastructure for triggering start/stop samples at
45 * various points (for example, to exclude mem2gmem/gmem2mem or clear)
46 * as well as per tile tracking.
47 *
48 * NOTE: in at least some cases hw writes sample values to memory addr
49 * specified in some register.  So we don't really have the option to
50 * just sample the same counter multiple times for multiple different
51 * queries with the same query_type.  So we cache per sample provider
52 * the most recent sample since the last draw.  This way multiple
53 * sample periods for multiple queries can reference the same sample.
54 *
55 * fd_hw_sample_provider:
56 *   - one per query type, registered/implemented by gpu generation
57 *     specific code
58 *   - can construct fd_hw_samples on demand
59 *   - most recent sample (since last draw) cached so multiple
60 *     different queries can ref the same sample
61 *
62 * fd_hw_sample:
63 *   - abstracts one snapshot of counter value(s) across N tiles
64 *   - backing object not allocated until submit time when number
65 *     of samples and number of tiles is known
66 *
67 * fd_hw_sample_period:
68 *   - consists of start and stop sample
69 *   - a query accumulates a list of sample periods
70 *   - the query result is the sum of the sample periods
71 */
72
73struct fd_hw_sample_provider {
74	unsigned query_type;
75
76	/* stages applicable to the query type: */
77	enum fd_render_stage active;
78
79	/* Optional hook for enabling a counter.  Guaranteed to happen
80	 * at least once before the first ->get_sample() in a batch.
81	 */
82	void (*enable)(struct fd_context *ctx, struct fd_ringbuffer *ring);
83
84	/* when a new sample is required, emit appropriate cmdstream
85	 * and return a sample object:
86	 */
87	struct fd_hw_sample *(*get_sample)(struct fd_batch *batch,
88			struct fd_ringbuffer *ring);
89
90	/* accumulate the results from specified sample period: */
91	void (*accumulate_result)(struct fd_context *ctx,
92			const void *start, const void *end,
93			union pipe_query_result *result);
94};
95
96struct fd_hw_sample {
97	struct pipe_reference reference;  /* keep this first */
98
99	/* offset and size of the sample are know at the time the
100	 * sample is constructed.
101	 */
102	uint32_t size;
103	uint32_t offset;
104
105	/* backing object, offset/stride/etc are determined not when
106	 * the sample is constructed, but when the batch is submitted.
107	 * This way we can defer allocation until total # of requested
108	 * samples, and total # of tiles, is known.
109	 */
110	struct pipe_resource *prsc;
111	uint32_t num_tiles;
112	uint32_t tile_stride;
113};
114
115struct fd_hw_sample_period;
116
117struct fd_hw_query {
118	struct fd_query base;
119
120	const struct fd_hw_sample_provider *provider;
121
122	/* list of fd_hw_sample_periods: */
123	struct list_head periods;
124
125	/* if active and not paused, the current sample period (not
126	 * yet added to current_periods):
127	 */
128	struct fd_hw_sample_period *period;
129
130	struct list_head list;   /* list-node in batch->active_queries */
131
132	int no_wait_cnt;         /* see fd_hw_get_query_result */
133};
134
135static inline struct fd_hw_query *
136fd_hw_query(struct fd_query *q)
137{
138	return (struct fd_hw_query *)q;
139}
140
141struct fd_query * fd_hw_create_query(struct fd_context *ctx, unsigned query_type);
142/* helper for sample providers: */
143struct fd_hw_sample * fd_hw_sample_init(struct fd_batch *batch, uint32_t size);
144/* don't call directly, use fd_hw_sample_reference() */
145void __fd_hw_sample_destroy(struct fd_context *ctx, struct fd_hw_sample *samp);
146void fd_hw_query_prepare(struct fd_batch *batch, uint32_t num_tiles);
147void fd_hw_query_prepare_tile(struct fd_batch *batch, uint32_t n,
148		struct fd_ringbuffer *ring);
149void fd_hw_query_set_stage(struct fd_batch *batch,
150		struct fd_ringbuffer *ring, enum fd_render_stage stage);
151void fd_hw_query_enable(struct fd_batch *batch, struct fd_ringbuffer *ring);
152void fd_hw_query_register_provider(struct pipe_context *pctx,
153		const struct fd_hw_sample_provider *provider);
154void fd_hw_query_init(struct pipe_context *pctx);
155void fd_hw_query_fini(struct pipe_context *pctx);
156
157static inline void
158fd_hw_sample_reference(struct fd_context *ctx,
159		struct fd_hw_sample **ptr, struct fd_hw_sample *samp)
160{
161	struct fd_hw_sample *old_samp = *ptr;
162
163	if (pipe_reference(&(*ptr)->reference, &samp->reference))
164		__fd_hw_sample_destroy(ctx, old_samp);
165	*ptr = samp;
166}
167
168#endif /* FREEDRENO_QUERY_HW_H_ */
169