1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2015 Advanced Micro Devices, Inc.
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 *  Nicolai Hähnle <nicolai.haehnle@amd.com>
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci */
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#ifndef R600_QUERY_H
29bf215546Sopenharmony_ci#define R600_QUERY_H
30bf215546Sopenharmony_ci
31bf215546Sopenharmony_ci#include "util/u_threaded_context.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistruct pipe_context;
34bf215546Sopenharmony_cistruct pipe_query;
35bf215546Sopenharmony_cistruct pipe_resource;
36bf215546Sopenharmony_ci
37bf215546Sopenharmony_cistruct r600_common_context;
38bf215546Sopenharmony_cistruct r600_common_screen;
39bf215546Sopenharmony_cistruct r600_query;
40bf215546Sopenharmony_cistruct r600_query_hw;
41bf215546Sopenharmony_cistruct r600_resource;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cienum {
44bf215546Sopenharmony_ci	R600_QUERY_DRAW_CALLS = PIPE_QUERY_DRIVER_SPECIFIC,
45bf215546Sopenharmony_ci	R600_QUERY_DECOMPRESS_CALLS,
46bf215546Sopenharmony_ci	R600_QUERY_MRT_DRAW_CALLS,
47bf215546Sopenharmony_ci	R600_QUERY_PRIM_RESTART_CALLS,
48bf215546Sopenharmony_ci	R600_QUERY_SPILL_DRAW_CALLS,
49bf215546Sopenharmony_ci	R600_QUERY_COMPUTE_CALLS,
50bf215546Sopenharmony_ci	R600_QUERY_SPILL_COMPUTE_CALLS,
51bf215546Sopenharmony_ci	R600_QUERY_DMA_CALLS,
52bf215546Sopenharmony_ci	R600_QUERY_CP_DMA_CALLS,
53bf215546Sopenharmony_ci	R600_QUERY_NUM_VS_FLUSHES,
54bf215546Sopenharmony_ci	R600_QUERY_NUM_PS_FLUSHES,
55bf215546Sopenharmony_ci	R600_QUERY_NUM_CS_FLUSHES,
56bf215546Sopenharmony_ci	R600_QUERY_NUM_CB_CACHE_FLUSHES,
57bf215546Sopenharmony_ci	R600_QUERY_NUM_DB_CACHE_FLUSHES,
58bf215546Sopenharmony_ci	R600_QUERY_NUM_RESIDENT_HANDLES,
59bf215546Sopenharmony_ci	R600_QUERY_TC_OFFLOADED_SLOTS,
60bf215546Sopenharmony_ci	R600_QUERY_TC_DIRECT_SLOTS,
61bf215546Sopenharmony_ci	R600_QUERY_TC_NUM_SYNCS,
62bf215546Sopenharmony_ci	R600_QUERY_CS_THREAD_BUSY,
63bf215546Sopenharmony_ci	R600_QUERY_GALLIUM_THREAD_BUSY,
64bf215546Sopenharmony_ci	R600_QUERY_REQUESTED_VRAM,
65bf215546Sopenharmony_ci	R600_QUERY_REQUESTED_GTT,
66bf215546Sopenharmony_ci	R600_QUERY_MAPPED_VRAM,
67bf215546Sopenharmony_ci	R600_QUERY_MAPPED_GTT,
68bf215546Sopenharmony_ci	R600_QUERY_BUFFER_WAIT_TIME,
69bf215546Sopenharmony_ci	R600_QUERY_NUM_MAPPED_BUFFERS,
70bf215546Sopenharmony_ci	R600_QUERY_NUM_GFX_IBS,
71bf215546Sopenharmony_ci	R600_QUERY_NUM_SDMA_IBS,
72bf215546Sopenharmony_ci	R600_QUERY_GFX_BO_LIST_SIZE,
73bf215546Sopenharmony_ci	R600_QUERY_NUM_BYTES_MOVED,
74bf215546Sopenharmony_ci	R600_QUERY_NUM_EVICTIONS,
75bf215546Sopenharmony_ci	R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS,
76bf215546Sopenharmony_ci	R600_QUERY_VRAM_USAGE,
77bf215546Sopenharmony_ci	R600_QUERY_VRAM_VIS_USAGE,
78bf215546Sopenharmony_ci	R600_QUERY_GTT_USAGE,
79bf215546Sopenharmony_ci	R600_QUERY_GPU_TEMPERATURE,
80bf215546Sopenharmony_ci	R600_QUERY_CURRENT_GPU_SCLK,
81bf215546Sopenharmony_ci	R600_QUERY_CURRENT_GPU_MCLK,
82bf215546Sopenharmony_ci	R600_QUERY_GPU_LOAD,
83bf215546Sopenharmony_ci	R600_QUERY_GPU_SHADERS_BUSY,
84bf215546Sopenharmony_ci	R600_QUERY_GPU_TA_BUSY,
85bf215546Sopenharmony_ci	R600_QUERY_GPU_GDS_BUSY,
86bf215546Sopenharmony_ci	R600_QUERY_GPU_VGT_BUSY,
87bf215546Sopenharmony_ci	R600_QUERY_GPU_IA_BUSY,
88bf215546Sopenharmony_ci	R600_QUERY_GPU_SX_BUSY,
89bf215546Sopenharmony_ci	R600_QUERY_GPU_WD_BUSY,
90bf215546Sopenharmony_ci	R600_QUERY_GPU_BCI_BUSY,
91bf215546Sopenharmony_ci	R600_QUERY_GPU_SC_BUSY,
92bf215546Sopenharmony_ci	R600_QUERY_GPU_PA_BUSY,
93bf215546Sopenharmony_ci	R600_QUERY_GPU_DB_BUSY,
94bf215546Sopenharmony_ci	R600_QUERY_GPU_CP_BUSY,
95bf215546Sopenharmony_ci	R600_QUERY_GPU_CB_BUSY,
96bf215546Sopenharmony_ci	R600_QUERY_GPU_SDMA_BUSY,
97bf215546Sopenharmony_ci	R600_QUERY_GPU_PFP_BUSY,
98bf215546Sopenharmony_ci	R600_QUERY_GPU_MEQ_BUSY,
99bf215546Sopenharmony_ci	R600_QUERY_GPU_ME_BUSY,
100bf215546Sopenharmony_ci	R600_QUERY_GPU_SURF_SYNC_BUSY,
101bf215546Sopenharmony_ci	R600_QUERY_GPU_CP_DMA_BUSY,
102bf215546Sopenharmony_ci	R600_QUERY_GPU_SCRATCH_RAM_BUSY,
103bf215546Sopenharmony_ci	R600_QUERY_NUM_COMPILATIONS,
104bf215546Sopenharmony_ci	R600_QUERY_NUM_SHADERS_CREATED,
105bf215546Sopenharmony_ci	R600_QUERY_NUM_SHADER_CACHE_HITS,
106bf215546Sopenharmony_ci	R600_QUERY_GPIN_ASIC_ID,
107bf215546Sopenharmony_ci	R600_QUERY_GPIN_NUM_SIMD,
108bf215546Sopenharmony_ci	R600_QUERY_GPIN_NUM_RB,
109bf215546Sopenharmony_ci	R600_QUERY_GPIN_NUM_SPI,
110bf215546Sopenharmony_ci	R600_QUERY_GPIN_NUM_SE,
111bf215546Sopenharmony_ci
112bf215546Sopenharmony_ci	R600_QUERY_FIRST_PERFCOUNTER = PIPE_QUERY_DRIVER_SPECIFIC + 100,
113bf215546Sopenharmony_ci};
114bf215546Sopenharmony_ci
115bf215546Sopenharmony_cienum {
116bf215546Sopenharmony_ci	R600_QUERY_GROUP_GPIN = 0,
117bf215546Sopenharmony_ci	R600_NUM_SW_QUERY_GROUPS
118bf215546Sopenharmony_ci};
119bf215546Sopenharmony_ci
120bf215546Sopenharmony_cistruct r600_query_ops {
121bf215546Sopenharmony_ci	void (*destroy)(struct r600_common_screen *, struct r600_query *);
122bf215546Sopenharmony_ci	bool (*begin)(struct r600_common_context *, struct r600_query *);
123bf215546Sopenharmony_ci	bool (*end)(struct r600_common_context *, struct r600_query *);
124bf215546Sopenharmony_ci	bool (*get_result)(struct r600_common_context *,
125bf215546Sopenharmony_ci			   struct r600_query *, bool wait,
126bf215546Sopenharmony_ci			   union pipe_query_result *result);
127bf215546Sopenharmony_ci	void (*get_result_resource)(struct r600_common_context *,
128bf215546Sopenharmony_ci				    struct r600_query *,
129bf215546Sopenharmony_ci				    enum pipe_query_flags flags,
130bf215546Sopenharmony_ci				    enum pipe_query_value_type result_type,
131bf215546Sopenharmony_ci				    int index,
132bf215546Sopenharmony_ci				    struct pipe_resource *resource,
133bf215546Sopenharmony_ci				    unsigned offset);
134bf215546Sopenharmony_ci};
135bf215546Sopenharmony_ci
136bf215546Sopenharmony_cistruct r600_query {
137bf215546Sopenharmony_ci	struct threaded_query b;
138bf215546Sopenharmony_ci	struct r600_query_ops *ops;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci	/* The type of query */
141bf215546Sopenharmony_ci	unsigned type;
142bf215546Sopenharmony_ci};
143bf215546Sopenharmony_ci
144bf215546Sopenharmony_cienum {
145bf215546Sopenharmony_ci	R600_QUERY_HW_FLAG_NO_START = (1 << 0),
146bf215546Sopenharmony_ci	/* gap */
147bf215546Sopenharmony_ci	/* whether begin_query doesn't clear the result */
148bf215546Sopenharmony_ci	R600_QUERY_HW_FLAG_BEGIN_RESUMES = (1 << 2),
149bf215546Sopenharmony_ci};
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_cistruct r600_query_hw_ops {
152bf215546Sopenharmony_ci	bool (*prepare_buffer)(struct r600_common_screen *,
153bf215546Sopenharmony_ci			       struct r600_query_hw *,
154bf215546Sopenharmony_ci			       struct r600_resource *);
155bf215546Sopenharmony_ci	void (*emit_start)(struct r600_common_context *,
156bf215546Sopenharmony_ci			   struct r600_query_hw *,
157bf215546Sopenharmony_ci			   struct r600_resource *buffer, uint64_t va);
158bf215546Sopenharmony_ci	void (*emit_stop)(struct r600_common_context *,
159bf215546Sopenharmony_ci			  struct r600_query_hw *,
160bf215546Sopenharmony_ci			  struct r600_resource *buffer, uint64_t va);
161bf215546Sopenharmony_ci	void (*clear_result)(struct r600_query_hw *, union pipe_query_result *);
162bf215546Sopenharmony_ci	void (*add_result)(struct r600_common_screen *screen,
163bf215546Sopenharmony_ci			   struct r600_query_hw *, void *buffer,
164bf215546Sopenharmony_ci			   union pipe_query_result *result);
165bf215546Sopenharmony_ci};
166bf215546Sopenharmony_ci
167bf215546Sopenharmony_cistruct r600_query_buffer {
168bf215546Sopenharmony_ci	/* The buffer where query results are stored. */
169bf215546Sopenharmony_ci	struct r600_resource		*buf;
170bf215546Sopenharmony_ci	/* Offset of the next free result after current query data */
171bf215546Sopenharmony_ci	unsigned			results_end;
172bf215546Sopenharmony_ci	/* If a query buffer is full, a new buffer is created and the old one
173bf215546Sopenharmony_ci	 * is put in here. When we calculate the result, we sum up the samples
174bf215546Sopenharmony_ci	 * from all buffers. */
175bf215546Sopenharmony_ci	struct r600_query_buffer	*previous;
176bf215546Sopenharmony_ci};
177bf215546Sopenharmony_ci
178bf215546Sopenharmony_cistruct r600_query_hw {
179bf215546Sopenharmony_ci	struct r600_query b;
180bf215546Sopenharmony_ci	struct r600_query_hw_ops *ops;
181bf215546Sopenharmony_ci	unsigned flags;
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci	/* The query buffer and how many results are in it. */
184bf215546Sopenharmony_ci	struct r600_query_buffer buffer;
185bf215546Sopenharmony_ci	/* Size of the result in memory for both begin_query and end_query,
186bf215546Sopenharmony_ci	 * this can be one or two numbers, or it could even be a size of a structure. */
187bf215546Sopenharmony_ci	unsigned result_size;
188bf215546Sopenharmony_ci	/* The number of dwords for begin_query or end_query. */
189bf215546Sopenharmony_ci	unsigned num_cs_dw_begin;
190bf215546Sopenharmony_ci	unsigned num_cs_dw_end;
191bf215546Sopenharmony_ci	/* Linked list of queries */
192bf215546Sopenharmony_ci	struct list_head list;
193bf215546Sopenharmony_ci	/* For transform feedback: which stream the query is for */
194bf215546Sopenharmony_ci	unsigned stream;
195bf215546Sopenharmony_ci};
196bf215546Sopenharmony_ci
197bf215546Sopenharmony_cibool r600_query_hw_init(struct r600_common_screen *rscreen,
198bf215546Sopenharmony_ci			struct r600_query_hw *query);
199bf215546Sopenharmony_civoid r600_query_hw_destroy(struct r600_common_screen *rscreen,
200bf215546Sopenharmony_ci			   struct r600_query *rquery);
201bf215546Sopenharmony_cibool r600_query_hw_begin(struct r600_common_context *rctx,
202bf215546Sopenharmony_ci			 struct r600_query *rquery);
203bf215546Sopenharmony_cibool r600_query_hw_end(struct r600_common_context *rctx,
204bf215546Sopenharmony_ci		       struct r600_query *rquery);
205bf215546Sopenharmony_cibool r600_query_hw_get_result(struct r600_common_context *rctx,
206bf215546Sopenharmony_ci			      struct r600_query *rquery,
207bf215546Sopenharmony_ci			      bool wait,
208bf215546Sopenharmony_ci			      union pipe_query_result *result);
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci/* Performance counters */
211bf215546Sopenharmony_cienum {
212bf215546Sopenharmony_ci	/* This block is part of the shader engine */
213bf215546Sopenharmony_ci	R600_PC_BLOCK_SE = (1 << 0),
214bf215546Sopenharmony_ci
215bf215546Sopenharmony_ci	/* Expose per-instance groups instead of summing all instances (within
216bf215546Sopenharmony_ci	 * an SE). */
217bf215546Sopenharmony_ci	R600_PC_BLOCK_INSTANCE_GROUPS = (1 << 1),
218bf215546Sopenharmony_ci
219bf215546Sopenharmony_ci	/* Expose per-SE groups instead of summing instances across SEs. */
220bf215546Sopenharmony_ci	R600_PC_BLOCK_SE_GROUPS = (1 << 2),
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ci	/* Shader block */
223bf215546Sopenharmony_ci	R600_PC_BLOCK_SHADER = (1 << 3),
224bf215546Sopenharmony_ci
225bf215546Sopenharmony_ci	/* Non-shader block with perfcounters windowed by shaders. */
226bf215546Sopenharmony_ci	R600_PC_BLOCK_SHADER_WINDOWED = (1 << 4),
227bf215546Sopenharmony_ci};
228bf215546Sopenharmony_ci
229bf215546Sopenharmony_ci/* Describes a hardware block with performance counters. Multiple instances of
230bf215546Sopenharmony_ci * each block, possibly per-SE, may exist on the chip. Depending on the block
231bf215546Sopenharmony_ci * and on the user's configuration, we either
232bf215546Sopenharmony_ci *  (a) expose every instance as a performance counter group,
233bf215546Sopenharmony_ci *  (b) expose a single performance counter group that reports the sum over all
234bf215546Sopenharmony_ci *      instances, or
235bf215546Sopenharmony_ci *  (c) expose one performance counter group per instance, but summed over all
236bf215546Sopenharmony_ci *      shader engines.
237bf215546Sopenharmony_ci */
238bf215546Sopenharmony_cistruct r600_perfcounter_block {
239bf215546Sopenharmony_ci	const char *basename;
240bf215546Sopenharmony_ci	unsigned flags;
241bf215546Sopenharmony_ci	unsigned num_counters;
242bf215546Sopenharmony_ci	unsigned num_selectors;
243bf215546Sopenharmony_ci	unsigned num_instances;
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_ci	unsigned num_groups;
246bf215546Sopenharmony_ci	char *group_names;
247bf215546Sopenharmony_ci	unsigned group_name_stride;
248bf215546Sopenharmony_ci
249bf215546Sopenharmony_ci	char *selector_names;
250bf215546Sopenharmony_ci	unsigned selector_name_stride;
251bf215546Sopenharmony_ci
252bf215546Sopenharmony_ci	void *data;
253bf215546Sopenharmony_ci};
254bf215546Sopenharmony_ci
255bf215546Sopenharmony_cistruct r600_perfcounters {
256bf215546Sopenharmony_ci	unsigned num_groups;
257bf215546Sopenharmony_ci	unsigned num_blocks;
258bf215546Sopenharmony_ci	struct r600_perfcounter_block *blocks;
259bf215546Sopenharmony_ci
260bf215546Sopenharmony_ci	unsigned num_start_cs_dwords;
261bf215546Sopenharmony_ci	unsigned num_stop_cs_dwords;
262bf215546Sopenharmony_ci	unsigned num_instance_cs_dwords;
263bf215546Sopenharmony_ci	unsigned num_shaders_cs_dwords;
264bf215546Sopenharmony_ci
265bf215546Sopenharmony_ci	unsigned num_shader_types;
266bf215546Sopenharmony_ci	const char * const *shader_type_suffixes;
267bf215546Sopenharmony_ci	const unsigned *shader_type_bits;
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_ci	void (*get_size)(struct r600_perfcounter_block *,
270bf215546Sopenharmony_ci			 unsigned count, unsigned *selectors,
271bf215546Sopenharmony_ci			 unsigned *num_select_dw, unsigned *num_read_dw);
272bf215546Sopenharmony_ci
273bf215546Sopenharmony_ci	void (*emit_instance)(struct r600_common_context *,
274bf215546Sopenharmony_ci			      int se, int instance);
275bf215546Sopenharmony_ci	void (*emit_shaders)(struct r600_common_context *, unsigned shaders);
276bf215546Sopenharmony_ci	void (*emit_select)(struct r600_common_context *,
277bf215546Sopenharmony_ci			    struct r600_perfcounter_block *,
278bf215546Sopenharmony_ci			    unsigned count, unsigned *selectors);
279bf215546Sopenharmony_ci	void (*emit_start)(struct r600_common_context *,
280bf215546Sopenharmony_ci			  struct r600_resource *buffer, uint64_t va);
281bf215546Sopenharmony_ci	void (*emit_stop)(struct r600_common_context *,
282bf215546Sopenharmony_ci			  struct r600_resource *buffer, uint64_t va);
283bf215546Sopenharmony_ci	void (*emit_read)(struct r600_common_context *,
284bf215546Sopenharmony_ci			  struct r600_perfcounter_block *,
285bf215546Sopenharmony_ci			  unsigned count, unsigned *selectors,
286bf215546Sopenharmony_ci			  struct r600_resource *buffer, uint64_t va);
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_ci	void (*cleanup)(struct r600_common_screen *);
289bf215546Sopenharmony_ci
290bf215546Sopenharmony_ci	bool separate_se;
291bf215546Sopenharmony_ci	bool separate_instance;
292bf215546Sopenharmony_ci};
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_cistruct pipe_query *r600_create_batch_query(struct pipe_context *ctx,
295bf215546Sopenharmony_ci					   unsigned num_queries,
296bf215546Sopenharmony_ci					   unsigned *query_types);
297bf215546Sopenharmony_ci
298bf215546Sopenharmony_ciint r600_get_perfcounter_info(struct r600_common_screen *,
299bf215546Sopenharmony_ci			      unsigned index,
300bf215546Sopenharmony_ci			      struct pipe_driver_query_info *info);
301bf215546Sopenharmony_ciint r600_get_perfcounter_group_info(struct r600_common_screen *,
302bf215546Sopenharmony_ci				    unsigned index,
303bf215546Sopenharmony_ci				    struct pipe_driver_query_group_info *info);
304bf215546Sopenharmony_ci
305bf215546Sopenharmony_cibool r600_perfcounters_init(struct r600_perfcounters *, unsigned num_blocks);
306bf215546Sopenharmony_civoid r600_perfcounters_add_block(struct r600_common_screen *,
307bf215546Sopenharmony_ci				 struct r600_perfcounters *,
308bf215546Sopenharmony_ci				 const char *name, unsigned flags,
309bf215546Sopenharmony_ci				 unsigned counters, unsigned selectors,
310bf215546Sopenharmony_ci				 unsigned instances, void *data);
311bf215546Sopenharmony_civoid r600_perfcounters_do_destroy(struct r600_perfcounters *);
312bf215546Sopenharmony_civoid r600_query_hw_reset_buffers(struct r600_common_context *rctx,
313bf215546Sopenharmony_ci				 struct r600_query_hw *query);
314bf215546Sopenharmony_ci
315bf215546Sopenharmony_cistruct r600_qbo_state {
316bf215546Sopenharmony_ci	void *saved_compute;
317bf215546Sopenharmony_ci	struct pipe_constant_buffer saved_const0;
318bf215546Sopenharmony_ci	struct pipe_shader_buffer saved_ssbo[3];
319bf215546Sopenharmony_ci};
320bf215546Sopenharmony_ci
321bf215546Sopenharmony_ci#endif /* R600_QUERY_H */
322