1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2015 Advanced Micro Devices, Inc.
3bf215546Sopenharmony_ci * All Rights Reserved.
4bf215546Sopenharmony_ci *
5bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
6bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
7bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
8bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
10bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
11bf215546Sopenharmony_ci *
12bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next
13bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the
14bf215546Sopenharmony_ci * Software.
15bf215546Sopenharmony_ci *
16bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22bf215546Sopenharmony_ci * SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#ifndef SI_QUERY_H
26bf215546Sopenharmony_ci#define SI_QUERY_H
27bf215546Sopenharmony_ci
28bf215546Sopenharmony_ci#include "util/u_threaded_context.h"
29bf215546Sopenharmony_ci
30bf215546Sopenharmony_ci#include "ac_perfcounter.h"
31bf215546Sopenharmony_ci
32bf215546Sopenharmony_cistruct pipe_context;
33bf215546Sopenharmony_cistruct pipe_query;
34bf215546Sopenharmony_cistruct pipe_resource;
35bf215546Sopenharmony_ci
36bf215546Sopenharmony_cistruct si_screen;
37bf215546Sopenharmony_cistruct si_context;
38bf215546Sopenharmony_cistruct si_query;
39bf215546Sopenharmony_cistruct si_query_buffer;
40bf215546Sopenharmony_cistruct si_query_hw;
41bf215546Sopenharmony_cistruct si_resource;
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci#define SI_MAX_STREAMS 4
44bf215546Sopenharmony_ci
45bf215546Sopenharmony_cienum
46bf215546Sopenharmony_ci{
47bf215546Sopenharmony_ci   SI_QUERY_DRAW_CALLS = PIPE_QUERY_DRIVER_SPECIFIC,
48bf215546Sopenharmony_ci   SI_QUERY_DECOMPRESS_CALLS,
49bf215546Sopenharmony_ci   SI_QUERY_PRIM_RESTART_CALLS,
50bf215546Sopenharmony_ci   SI_QUERY_COMPUTE_CALLS,
51bf215546Sopenharmony_ci   SI_QUERY_CP_DMA_CALLS,
52bf215546Sopenharmony_ci   SI_QUERY_NUM_VS_FLUSHES,
53bf215546Sopenharmony_ci   SI_QUERY_NUM_PS_FLUSHES,
54bf215546Sopenharmony_ci   SI_QUERY_NUM_CS_FLUSHES,
55bf215546Sopenharmony_ci   SI_QUERY_NUM_CB_CACHE_FLUSHES,
56bf215546Sopenharmony_ci   SI_QUERY_NUM_DB_CACHE_FLUSHES,
57bf215546Sopenharmony_ci   SI_QUERY_NUM_L2_INVALIDATES,
58bf215546Sopenharmony_ci   SI_QUERY_NUM_L2_WRITEBACKS,
59bf215546Sopenharmony_ci   SI_QUERY_NUM_RESIDENT_HANDLES,
60bf215546Sopenharmony_ci   SI_QUERY_TC_OFFLOADED_SLOTS,
61bf215546Sopenharmony_ci   SI_QUERY_TC_DIRECT_SLOTS,
62bf215546Sopenharmony_ci   SI_QUERY_TC_NUM_SYNCS,
63bf215546Sopenharmony_ci   SI_QUERY_CS_THREAD_BUSY,
64bf215546Sopenharmony_ci   SI_QUERY_GALLIUM_THREAD_BUSY,
65bf215546Sopenharmony_ci   SI_QUERY_REQUESTED_VRAM,
66bf215546Sopenharmony_ci   SI_QUERY_REQUESTED_GTT,
67bf215546Sopenharmony_ci   SI_QUERY_MAPPED_VRAM,
68bf215546Sopenharmony_ci   SI_QUERY_MAPPED_GTT,
69bf215546Sopenharmony_ci   SI_QUERY_SLAB_WASTED_VRAM,
70bf215546Sopenharmony_ci   SI_QUERY_SLAB_WASTED_GTT,
71bf215546Sopenharmony_ci   SI_QUERY_BUFFER_WAIT_TIME,
72bf215546Sopenharmony_ci   SI_QUERY_NUM_MAPPED_BUFFERS,
73bf215546Sopenharmony_ci   SI_QUERY_NUM_GFX_IBS,
74bf215546Sopenharmony_ci   SI_QUERY_GFX_BO_LIST_SIZE,
75bf215546Sopenharmony_ci   SI_QUERY_GFX_IB_SIZE,
76bf215546Sopenharmony_ci   SI_QUERY_NUM_BYTES_MOVED,
77bf215546Sopenharmony_ci   SI_QUERY_NUM_EVICTIONS,
78bf215546Sopenharmony_ci   SI_QUERY_NUM_VRAM_CPU_PAGE_FAULTS,
79bf215546Sopenharmony_ci   SI_QUERY_VRAM_USAGE,
80bf215546Sopenharmony_ci   SI_QUERY_VRAM_VIS_USAGE,
81bf215546Sopenharmony_ci   SI_QUERY_GTT_USAGE,
82bf215546Sopenharmony_ci   SI_QUERY_GPU_TEMPERATURE,
83bf215546Sopenharmony_ci   SI_QUERY_CURRENT_GPU_SCLK,
84bf215546Sopenharmony_ci   SI_QUERY_CURRENT_GPU_MCLK,
85bf215546Sopenharmony_ci   SI_QUERY_GPU_LOAD,
86bf215546Sopenharmony_ci   SI_QUERY_GPU_SHADERS_BUSY,
87bf215546Sopenharmony_ci   SI_QUERY_GPU_TA_BUSY,
88bf215546Sopenharmony_ci   SI_QUERY_GPU_GDS_BUSY,
89bf215546Sopenharmony_ci   SI_QUERY_GPU_VGT_BUSY,
90bf215546Sopenharmony_ci   SI_QUERY_GPU_IA_BUSY,
91bf215546Sopenharmony_ci   SI_QUERY_GPU_SX_BUSY,
92bf215546Sopenharmony_ci   SI_QUERY_GPU_WD_BUSY,
93bf215546Sopenharmony_ci   SI_QUERY_GPU_BCI_BUSY,
94bf215546Sopenharmony_ci   SI_QUERY_GPU_SC_BUSY,
95bf215546Sopenharmony_ci   SI_QUERY_GPU_PA_BUSY,
96bf215546Sopenharmony_ci   SI_QUERY_GPU_DB_BUSY,
97bf215546Sopenharmony_ci   SI_QUERY_GPU_CP_BUSY,
98bf215546Sopenharmony_ci   SI_QUERY_GPU_CB_BUSY,
99bf215546Sopenharmony_ci   SI_QUERY_GPU_SDMA_BUSY,
100bf215546Sopenharmony_ci   SI_QUERY_GPU_PFP_BUSY,
101bf215546Sopenharmony_ci   SI_QUERY_GPU_MEQ_BUSY,
102bf215546Sopenharmony_ci   SI_QUERY_GPU_ME_BUSY,
103bf215546Sopenharmony_ci   SI_QUERY_GPU_SURF_SYNC_BUSY,
104bf215546Sopenharmony_ci   SI_QUERY_GPU_CP_DMA_BUSY,
105bf215546Sopenharmony_ci   SI_QUERY_GPU_SCRATCH_RAM_BUSY,
106bf215546Sopenharmony_ci   SI_QUERY_NUM_COMPILATIONS,
107bf215546Sopenharmony_ci   SI_QUERY_NUM_SHADERS_CREATED,
108bf215546Sopenharmony_ci   SI_QUERY_BACK_BUFFER_PS_DRAW_RATIO,
109bf215546Sopenharmony_ci   SI_QUERY_GPIN_ASIC_ID,
110bf215546Sopenharmony_ci   SI_QUERY_GPIN_NUM_SIMD,
111bf215546Sopenharmony_ci   SI_QUERY_GPIN_NUM_RB,
112bf215546Sopenharmony_ci   SI_QUERY_GPIN_NUM_SPI,
113bf215546Sopenharmony_ci   SI_QUERY_GPIN_NUM_SE,
114bf215546Sopenharmony_ci   SI_QUERY_LIVE_SHADER_CACHE_HITS,
115bf215546Sopenharmony_ci   SI_QUERY_LIVE_SHADER_CACHE_MISSES,
116bf215546Sopenharmony_ci   SI_QUERY_MEMORY_SHADER_CACHE_HITS,
117bf215546Sopenharmony_ci   SI_QUERY_MEMORY_SHADER_CACHE_MISSES,
118bf215546Sopenharmony_ci   SI_QUERY_DISK_SHADER_CACHE_HITS,
119bf215546Sopenharmony_ci   SI_QUERY_DISK_SHADER_CACHE_MISSES,
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   SI_QUERY_FIRST_PERFCOUNTER = PIPE_QUERY_DRIVER_SPECIFIC + 100,
122bf215546Sopenharmony_ci};
123bf215546Sopenharmony_ci
124bf215546Sopenharmony_cienum
125bf215546Sopenharmony_ci{
126bf215546Sopenharmony_ci   SI_QUERY_GROUP_GPIN = 0,
127bf215546Sopenharmony_ci   SI_NUM_SW_QUERY_GROUPS
128bf215546Sopenharmony_ci};
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_cistruct si_query_ops {
131bf215546Sopenharmony_ci   void (*destroy)(struct si_context *, struct si_query *);
132bf215546Sopenharmony_ci   bool (*begin)(struct si_context *, struct si_query *);
133bf215546Sopenharmony_ci   bool (*end)(struct si_context *, struct si_query *);
134bf215546Sopenharmony_ci   bool (*get_result)(struct si_context *, struct si_query *, bool wait,
135bf215546Sopenharmony_ci                      union pipe_query_result *result);
136bf215546Sopenharmony_ci   void (*get_result_resource)(struct si_context *, struct si_query *,
137bf215546Sopenharmony_ci                               enum pipe_query_flags flags,
138bf215546Sopenharmony_ci                               enum pipe_query_value_type result_type, int index,
139bf215546Sopenharmony_ci                               struct pipe_resource *resource, unsigned offset);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   void (*suspend)(struct si_context *, struct si_query *);
142bf215546Sopenharmony_ci   void (*resume)(struct si_context *, struct si_query *);
143bf215546Sopenharmony_ci};
144bf215546Sopenharmony_ci
145bf215546Sopenharmony_cistruct si_query {
146bf215546Sopenharmony_ci   struct threaded_query b;
147bf215546Sopenharmony_ci   const struct si_query_ops *ops;
148bf215546Sopenharmony_ci
149bf215546Sopenharmony_ci   /* The PIPE_QUERY_xxx type of query */
150bf215546Sopenharmony_ci   unsigned type;
151bf215546Sopenharmony_ci
152bf215546Sopenharmony_ci   /* The number of dwords for suspend. */
153bf215546Sopenharmony_ci   unsigned num_cs_dw_suspend;
154bf215546Sopenharmony_ci
155bf215546Sopenharmony_ci   /* Linked list of queries that must be suspended at end of CS. */
156bf215546Sopenharmony_ci   struct list_head active_list;
157bf215546Sopenharmony_ci};
158bf215546Sopenharmony_ci
159bf215546Sopenharmony_cienum
160bf215546Sopenharmony_ci{
161bf215546Sopenharmony_ci   SI_QUERY_HW_FLAG_NO_START = (1 << 0),
162bf215546Sopenharmony_ci   /* gap */
163bf215546Sopenharmony_ci   /* whether begin_query doesn't clear the result */
164bf215546Sopenharmony_ci   SI_QUERY_HW_FLAG_BEGIN_RESUMES = (1 << 2),
165bf215546Sopenharmony_ci   /* whether GS invocations and emitted primitives counters are emulated
166bf215546Sopenharmony_ci    * using atomic adds.
167bf215546Sopenharmony_ci    */
168bf215546Sopenharmony_ci   SI_QUERY_EMULATE_GS_COUNTERS = (1 << 3),
169bf215546Sopenharmony_ci};
170bf215546Sopenharmony_ci
171bf215546Sopenharmony_cistruct si_query_hw_ops {
172bf215546Sopenharmony_ci   bool (*prepare_buffer)(struct si_context *, struct si_query_buffer *);
173bf215546Sopenharmony_ci   void (*emit_start)(struct si_context *, struct si_query_hw *, struct si_resource *buffer,
174bf215546Sopenharmony_ci                      uint64_t va);
175bf215546Sopenharmony_ci   void (*emit_stop)(struct si_context *, struct si_query_hw *, struct si_resource *buffer,
176bf215546Sopenharmony_ci                     uint64_t va);
177bf215546Sopenharmony_ci   void (*clear_result)(struct si_query_hw *, union pipe_query_result *);
178bf215546Sopenharmony_ci   void (*add_result)(struct si_screen *screen, struct si_query_hw *, void *buffer,
179bf215546Sopenharmony_ci                      union pipe_query_result *result);
180bf215546Sopenharmony_ci};
181bf215546Sopenharmony_ci
182bf215546Sopenharmony_cistruct si_query_buffer {
183bf215546Sopenharmony_ci   /* The buffer where query results are stored. */
184bf215546Sopenharmony_ci   struct si_resource *buf;
185bf215546Sopenharmony_ci   /* If a query buffer is full, a new buffer is created and the old one
186bf215546Sopenharmony_ci    * is put in here. When we calculate the result, we sum up the samples
187bf215546Sopenharmony_ci    * from all buffers. */
188bf215546Sopenharmony_ci   struct si_query_buffer *previous;
189bf215546Sopenharmony_ci   /* Offset of the next free result after current query data */
190bf215546Sopenharmony_ci   unsigned results_end;
191bf215546Sopenharmony_ci   bool unprepared;
192bf215546Sopenharmony_ci};
193bf215546Sopenharmony_ci
194bf215546Sopenharmony_civoid si_query_buffer_destroy(struct si_screen *sctx, struct si_query_buffer *buffer);
195bf215546Sopenharmony_civoid si_query_buffer_reset(struct si_context *sctx, struct si_query_buffer *buffer);
196bf215546Sopenharmony_cibool si_query_buffer_alloc(struct si_context *sctx, struct si_query_buffer *buffer,
197bf215546Sopenharmony_ci                           bool (*prepare_buffer)(struct si_context *, struct si_query_buffer *),
198bf215546Sopenharmony_ci                           unsigned size);
199bf215546Sopenharmony_ci
200bf215546Sopenharmony_cistruct si_query_hw {
201bf215546Sopenharmony_ci   struct si_query b;
202bf215546Sopenharmony_ci   struct si_query_hw_ops *ops;
203bf215546Sopenharmony_ci   unsigned flags;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   /* The query buffer and how many results are in it. */
206bf215546Sopenharmony_ci   struct si_query_buffer buffer;
207bf215546Sopenharmony_ci   /* Size of the result in memory for both begin_query and end_query,
208bf215546Sopenharmony_ci    * this can be one or two numbers, or it could even be a size of a structure. */
209bf215546Sopenharmony_ci   unsigned result_size;
210bf215546Sopenharmony_ci   union {
211bf215546Sopenharmony_ci      /* For transform feedback: which stream the query is for */
212bf215546Sopenharmony_ci      unsigned stream;
213bf215546Sopenharmony_ci      /* For pipeline stats: which counter is active */
214bf215546Sopenharmony_ci      unsigned index;
215bf215546Sopenharmony_ci   };
216bf215546Sopenharmony_ci
217bf215546Sopenharmony_ci   /* Workaround via compute shader */
218bf215546Sopenharmony_ci   struct si_resource *workaround_buf;
219bf215546Sopenharmony_ci   unsigned workaround_offset;
220bf215546Sopenharmony_ci};
221bf215546Sopenharmony_ci
222bf215546Sopenharmony_ciunsigned si_query_pipestat_end_dw_offset(struct si_screen *sscreen,
223bf215546Sopenharmony_ci                                         enum pipe_statistics_query_index index);
224bf215546Sopenharmony_civoid si_query_hw_destroy(struct si_context *sctx, struct si_query *squery);
225bf215546Sopenharmony_cibool si_query_hw_begin(struct si_context *sctx, struct si_query *squery);
226bf215546Sopenharmony_cibool si_query_hw_end(struct si_context *sctx, struct si_query *squery);
227bf215546Sopenharmony_cibool si_query_hw_get_result(struct si_context *sctx, struct si_query *squery, bool wait,
228bf215546Sopenharmony_ci                            union pipe_query_result *result);
229bf215546Sopenharmony_civoid si_query_hw_suspend(struct si_context *sctx, struct si_query *query);
230bf215546Sopenharmony_civoid si_query_hw_resume(struct si_context *sctx, struct si_query *query);
231bf215546Sopenharmony_ci
232bf215546Sopenharmony_ci/* Shader-based queries */
233bf215546Sopenharmony_ci
234bf215546Sopenharmony_ci/**
235bf215546Sopenharmony_ci * The query buffer is written to by ESGS NGG shaders with statistics about
236bf215546Sopenharmony_ci * generated and (streamout-)emitted primitives.
237bf215546Sopenharmony_ci *
238bf215546Sopenharmony_ci * The context maintains a ring of these query buffers, and queries simply
239bf215546Sopenharmony_ci * point into the ring, allowing an arbitrary number of queries to be active
240bf215546Sopenharmony_ci * without additional GPU cost.
241bf215546Sopenharmony_ci */
242bf215546Sopenharmony_cistruct gfx10_sh_query_buffer {
243bf215546Sopenharmony_ci   struct list_head list;
244bf215546Sopenharmony_ci   struct si_resource *buf;
245bf215546Sopenharmony_ci   unsigned refcount;
246bf215546Sopenharmony_ci
247bf215546Sopenharmony_ci   /* Offset into the buffer in bytes; points at the first un-emitted entry. */
248bf215546Sopenharmony_ci   unsigned head;
249bf215546Sopenharmony_ci};
250bf215546Sopenharmony_ci
251bf215546Sopenharmony_ci/* Memory layout of the query buffer. Must be kept in sync with shaders
252bf215546Sopenharmony_ci * (including QBO shaders) and should be aligned to cachelines.
253bf215546Sopenharmony_ci *
254bf215546Sopenharmony_ci * The somewhat awkward memory layout is for compatibility with the
255bf215546Sopenharmony_ci * SET_PREDICATION packet, which also means that we're setting the high bit
256bf215546Sopenharmony_ci * of all those values unconditionally.
257bf215546Sopenharmony_ci */
258bf215546Sopenharmony_cistruct gfx10_sh_query_buffer_mem {
259bf215546Sopenharmony_ci   struct {
260bf215546Sopenharmony_ci      uint64_t generated_primitives_start_dummy;
261bf215546Sopenharmony_ci      uint64_t emitted_primitives_start_dummy;
262bf215546Sopenharmony_ci      uint64_t generated_primitives;
263bf215546Sopenharmony_ci      uint64_t emitted_primitives;
264bf215546Sopenharmony_ci   } stream[4];
265bf215546Sopenharmony_ci   uint32_t fence; /* bottom-of-pipe fence: set to ~0 when draws have finished */
266bf215546Sopenharmony_ci   uint32_t pad[31];
267bf215546Sopenharmony_ci};
268bf215546Sopenharmony_ci
269bf215546Sopenharmony_cistruct gfx10_sh_query {
270bf215546Sopenharmony_ci   struct si_query b;
271bf215546Sopenharmony_ci
272bf215546Sopenharmony_ci   struct gfx10_sh_query_buffer *first;
273bf215546Sopenharmony_ci   struct gfx10_sh_query_buffer *last;
274bf215546Sopenharmony_ci   unsigned first_begin;
275bf215546Sopenharmony_ci   unsigned last_end;
276bf215546Sopenharmony_ci
277bf215546Sopenharmony_ci   unsigned stream;
278bf215546Sopenharmony_ci};
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_cistruct pipe_query *gfx10_sh_query_create(struct si_screen *screen, enum pipe_query_type query_type,
281bf215546Sopenharmony_ci                                         unsigned index);
282bf215546Sopenharmony_ci
283bf215546Sopenharmony_ci/* Performance counters */
284bf215546Sopenharmony_cistruct si_perfcounters {
285bf215546Sopenharmony_ci   struct ac_perfcounters base;
286bf215546Sopenharmony_ci
287bf215546Sopenharmony_ci   unsigned num_stop_cs_dwords;
288bf215546Sopenharmony_ci   unsigned num_instance_cs_dwords;
289bf215546Sopenharmony_ci};
290bf215546Sopenharmony_ci
291bf215546Sopenharmony_cistruct pipe_query *si_create_batch_query(struct pipe_context *ctx, unsigned num_queries,
292bf215546Sopenharmony_ci                                         unsigned *query_types);
293bf215546Sopenharmony_ci
294bf215546Sopenharmony_ciint si_get_perfcounter_info(struct si_screen *, unsigned index,
295bf215546Sopenharmony_ci                            struct pipe_driver_query_info *info);
296bf215546Sopenharmony_ciint si_get_perfcounter_group_info(struct si_screen *, unsigned index,
297bf215546Sopenharmony_ci                                  struct pipe_driver_query_group_info *info);
298bf215546Sopenharmony_ci
299bf215546Sopenharmony_cistruct si_qbo_state {
300bf215546Sopenharmony_ci   struct pipe_constant_buffer saved_const0;
301bf215546Sopenharmony_ci};
302bf215546Sopenharmony_ci
303bf215546Sopenharmony_ci#endif /* SI_QUERY_H */
304