1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2010 Jerome Glisse <glisse@freedesktop.org>
3bf215546Sopenharmony_ci * Copyright 2014 Marek Olšák <marek.olsak@amd.com>
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 * on the rights to use, copy, modify, merge, publish, distribute, sub
9bf215546Sopenharmony_ci * license, and/or sell copies of the Software, and to permit persons to whom
10bf215546Sopenharmony_ci * the 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 NON-INFRINGEMENT. IN NO EVENT SHALL
19bf215546Sopenharmony_ci * THE AUTHOR(S) AND/OR THEIR SUPPLIERS BE LIABLE FOR ANY CLAIM,
20bf215546Sopenharmony_ci * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
21bf215546Sopenharmony_ci * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
22bf215546Sopenharmony_ci * USE OR OTHER DEALINGS IN THE SOFTWARE.
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#include "r600_query.h"
26bf215546Sopenharmony_ci#include "r600_pipe.h"
27bf215546Sopenharmony_ci#include "r600_cs.h"
28bf215546Sopenharmony_ci#include "util/u_memory.h"
29bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
30bf215546Sopenharmony_ci#include "util/os_time.h"
31bf215546Sopenharmony_ci#include "tgsi/tgsi_text.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_ci#define R600_MAX_STREAMS 4
34bf215546Sopenharmony_ci
35bf215546Sopenharmony_cistruct r600_hw_query_params {
36bf215546Sopenharmony_ci	unsigned start_offset;
37bf215546Sopenharmony_ci	unsigned end_offset;
38bf215546Sopenharmony_ci	unsigned fence_offset;
39bf215546Sopenharmony_ci	unsigned pair_stride;
40bf215546Sopenharmony_ci	unsigned pair_count;
41bf215546Sopenharmony_ci};
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_ci/* Queries without buffer handling or suspend/resume. */
44bf215546Sopenharmony_cistruct r600_query_sw {
45bf215546Sopenharmony_ci	struct r600_query b;
46bf215546Sopenharmony_ci
47bf215546Sopenharmony_ci	uint64_t begin_result;
48bf215546Sopenharmony_ci	uint64_t end_result;
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_ci	uint64_t begin_time;
51bf215546Sopenharmony_ci	uint64_t end_time;
52bf215546Sopenharmony_ci
53bf215546Sopenharmony_ci	/* Fence for GPU_FINISHED. */
54bf215546Sopenharmony_ci	struct pipe_fence_handle *fence;
55bf215546Sopenharmony_ci};
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistatic void r600_query_sw_destroy(struct r600_common_screen *rscreen,
58bf215546Sopenharmony_ci				  struct r600_query *rquery)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci	struct r600_query_sw *query = (struct r600_query_sw *)rquery;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci	rscreen->b.fence_reference(&rscreen->b, &query->fence, NULL);
63bf215546Sopenharmony_ci	FREE(query);
64bf215546Sopenharmony_ci}
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_cistatic enum radeon_value_id winsys_id_from_type(unsigned type)
67bf215546Sopenharmony_ci{
68bf215546Sopenharmony_ci	switch (type) {
69bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_VRAM: return RADEON_REQUESTED_VRAM_MEMORY;
70bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_GTT: return RADEON_REQUESTED_GTT_MEMORY;
71bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_VRAM: return RADEON_MAPPED_VRAM;
72bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_GTT: return RADEON_MAPPED_GTT;
73bf215546Sopenharmony_ci	case R600_QUERY_BUFFER_WAIT_TIME: return RADEON_BUFFER_WAIT_TIME_NS;
74bf215546Sopenharmony_ci	case R600_QUERY_NUM_MAPPED_BUFFERS: return RADEON_NUM_MAPPED_BUFFERS;
75bf215546Sopenharmony_ci	case R600_QUERY_NUM_GFX_IBS: return RADEON_NUM_GFX_IBS;
76bf215546Sopenharmony_ci	case R600_QUERY_NUM_SDMA_IBS: return RADEON_NUM_SDMA_IBS;
77bf215546Sopenharmony_ci	case R600_QUERY_GFX_BO_LIST_SIZE: return RADEON_GFX_BO_LIST_COUNTER;
78bf215546Sopenharmony_ci	case R600_QUERY_NUM_BYTES_MOVED: return RADEON_NUM_BYTES_MOVED;
79bf215546Sopenharmony_ci	case R600_QUERY_NUM_EVICTIONS: return RADEON_NUM_EVICTIONS;
80bf215546Sopenharmony_ci	case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS: return RADEON_NUM_VRAM_CPU_PAGE_FAULTS;
81bf215546Sopenharmony_ci	case R600_QUERY_VRAM_USAGE: return RADEON_VRAM_USAGE;
82bf215546Sopenharmony_ci	case R600_QUERY_VRAM_VIS_USAGE: return RADEON_VRAM_VIS_USAGE;
83bf215546Sopenharmony_ci	case R600_QUERY_GTT_USAGE: return RADEON_GTT_USAGE;
84bf215546Sopenharmony_ci	case R600_QUERY_GPU_TEMPERATURE: return RADEON_GPU_TEMPERATURE;
85bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_SCLK: return RADEON_CURRENT_SCLK;
86bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_MCLK: return RADEON_CURRENT_MCLK;
87bf215546Sopenharmony_ci	case R600_QUERY_CS_THREAD_BUSY: return RADEON_CS_THREAD_TIME;
88bf215546Sopenharmony_ci	default: unreachable("query type does not correspond to winsys id");
89bf215546Sopenharmony_ci	}
90bf215546Sopenharmony_ci}
91bf215546Sopenharmony_ci
92bf215546Sopenharmony_cistatic bool r600_query_sw_begin(struct r600_common_context *rctx,
93bf215546Sopenharmony_ci				struct r600_query *rquery)
94bf215546Sopenharmony_ci{
95bf215546Sopenharmony_ci	struct r600_query_sw *query = (struct r600_query_sw *)rquery;
96bf215546Sopenharmony_ci	enum radeon_value_id ws_id;
97bf215546Sopenharmony_ci
98bf215546Sopenharmony_ci	switch(query->b.type) {
99bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP_DISJOINT:
100bf215546Sopenharmony_ci	case PIPE_QUERY_GPU_FINISHED:
101bf215546Sopenharmony_ci		break;
102bf215546Sopenharmony_ci	case R600_QUERY_DRAW_CALLS:
103bf215546Sopenharmony_ci		query->begin_result = rctx->num_draw_calls;
104bf215546Sopenharmony_ci		break;
105bf215546Sopenharmony_ci	case R600_QUERY_DECOMPRESS_CALLS:
106bf215546Sopenharmony_ci		query->begin_result = rctx->num_decompress_calls;
107bf215546Sopenharmony_ci		break;
108bf215546Sopenharmony_ci	case R600_QUERY_MRT_DRAW_CALLS:
109bf215546Sopenharmony_ci		query->begin_result = rctx->num_mrt_draw_calls;
110bf215546Sopenharmony_ci		break;
111bf215546Sopenharmony_ci	case R600_QUERY_PRIM_RESTART_CALLS:
112bf215546Sopenharmony_ci		query->begin_result = rctx->num_prim_restart_calls;
113bf215546Sopenharmony_ci		break;
114bf215546Sopenharmony_ci	case R600_QUERY_SPILL_DRAW_CALLS:
115bf215546Sopenharmony_ci		query->begin_result = rctx->num_spill_draw_calls;
116bf215546Sopenharmony_ci		break;
117bf215546Sopenharmony_ci	case R600_QUERY_COMPUTE_CALLS:
118bf215546Sopenharmony_ci		query->begin_result = rctx->num_compute_calls;
119bf215546Sopenharmony_ci		break;
120bf215546Sopenharmony_ci	case R600_QUERY_SPILL_COMPUTE_CALLS:
121bf215546Sopenharmony_ci		query->begin_result = rctx->num_spill_compute_calls;
122bf215546Sopenharmony_ci		break;
123bf215546Sopenharmony_ci	case R600_QUERY_DMA_CALLS:
124bf215546Sopenharmony_ci		query->begin_result = rctx->num_dma_calls;
125bf215546Sopenharmony_ci		break;
126bf215546Sopenharmony_ci	case R600_QUERY_CP_DMA_CALLS:
127bf215546Sopenharmony_ci		query->begin_result = rctx->num_cp_dma_calls;
128bf215546Sopenharmony_ci		break;
129bf215546Sopenharmony_ci	case R600_QUERY_NUM_VS_FLUSHES:
130bf215546Sopenharmony_ci		query->begin_result = rctx->num_vs_flushes;
131bf215546Sopenharmony_ci		break;
132bf215546Sopenharmony_ci	case R600_QUERY_NUM_PS_FLUSHES:
133bf215546Sopenharmony_ci		query->begin_result = rctx->num_ps_flushes;
134bf215546Sopenharmony_ci		break;
135bf215546Sopenharmony_ci	case R600_QUERY_NUM_CS_FLUSHES:
136bf215546Sopenharmony_ci		query->begin_result = rctx->num_cs_flushes;
137bf215546Sopenharmony_ci		break;
138bf215546Sopenharmony_ci	case R600_QUERY_NUM_CB_CACHE_FLUSHES:
139bf215546Sopenharmony_ci		query->begin_result = rctx->num_cb_cache_flushes;
140bf215546Sopenharmony_ci		break;
141bf215546Sopenharmony_ci	case R600_QUERY_NUM_DB_CACHE_FLUSHES:
142bf215546Sopenharmony_ci		query->begin_result = rctx->num_db_cache_flushes;
143bf215546Sopenharmony_ci		break;
144bf215546Sopenharmony_ci	case R600_QUERY_NUM_RESIDENT_HANDLES:
145bf215546Sopenharmony_ci		query->begin_result = rctx->num_resident_handles;
146bf215546Sopenharmony_ci		break;
147bf215546Sopenharmony_ci	case R600_QUERY_TC_OFFLOADED_SLOTS:
148bf215546Sopenharmony_ci		query->begin_result = rctx->tc ? rctx->tc->num_offloaded_slots : 0;
149bf215546Sopenharmony_ci		break;
150bf215546Sopenharmony_ci	case R600_QUERY_TC_DIRECT_SLOTS:
151bf215546Sopenharmony_ci		query->begin_result = rctx->tc ? rctx->tc->num_direct_slots : 0;
152bf215546Sopenharmony_ci		break;
153bf215546Sopenharmony_ci	case R600_QUERY_TC_NUM_SYNCS:
154bf215546Sopenharmony_ci		query->begin_result = rctx->tc ? rctx->tc->num_syncs : 0;
155bf215546Sopenharmony_ci		break;
156bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_VRAM:
157bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_GTT:
158bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_VRAM:
159bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_GTT:
160bf215546Sopenharmony_ci	case R600_QUERY_VRAM_USAGE:
161bf215546Sopenharmony_ci	case R600_QUERY_VRAM_VIS_USAGE:
162bf215546Sopenharmony_ci	case R600_QUERY_GTT_USAGE:
163bf215546Sopenharmony_ci	case R600_QUERY_GPU_TEMPERATURE:
164bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_SCLK:
165bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_MCLK:
166bf215546Sopenharmony_ci	case R600_QUERY_NUM_MAPPED_BUFFERS:
167bf215546Sopenharmony_ci		query->begin_result = 0;
168bf215546Sopenharmony_ci		break;
169bf215546Sopenharmony_ci	case R600_QUERY_BUFFER_WAIT_TIME:
170bf215546Sopenharmony_ci	case R600_QUERY_NUM_GFX_IBS:
171bf215546Sopenharmony_ci	case R600_QUERY_NUM_SDMA_IBS:
172bf215546Sopenharmony_ci	case R600_QUERY_NUM_BYTES_MOVED:
173bf215546Sopenharmony_ci	case R600_QUERY_NUM_EVICTIONS:
174bf215546Sopenharmony_ci	case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS: {
175bf215546Sopenharmony_ci		enum radeon_value_id ws_id = winsys_id_from_type(query->b.type);
176bf215546Sopenharmony_ci		query->begin_result = rctx->ws->query_value(rctx->ws, ws_id);
177bf215546Sopenharmony_ci		break;
178bf215546Sopenharmony_ci	}
179bf215546Sopenharmony_ci	case R600_QUERY_GFX_BO_LIST_SIZE:
180bf215546Sopenharmony_ci		ws_id = winsys_id_from_type(query->b.type);
181bf215546Sopenharmony_ci		query->begin_result = rctx->ws->query_value(rctx->ws, ws_id);
182bf215546Sopenharmony_ci		query->begin_time = rctx->ws->query_value(rctx->ws,
183bf215546Sopenharmony_ci							  RADEON_NUM_GFX_IBS);
184bf215546Sopenharmony_ci		break;
185bf215546Sopenharmony_ci	case R600_QUERY_CS_THREAD_BUSY:
186bf215546Sopenharmony_ci		ws_id = winsys_id_from_type(query->b.type);
187bf215546Sopenharmony_ci		query->begin_result = rctx->ws->query_value(rctx->ws, ws_id);
188bf215546Sopenharmony_ci		query->begin_time = os_time_get_nano();
189bf215546Sopenharmony_ci		break;
190bf215546Sopenharmony_ci	case R600_QUERY_GALLIUM_THREAD_BUSY:
191bf215546Sopenharmony_ci		query->begin_result =
192bf215546Sopenharmony_ci			rctx->tc ? util_queue_get_thread_time_nano(&rctx->tc->queue, 0) : 0;
193bf215546Sopenharmony_ci		query->begin_time = os_time_get_nano();
194bf215546Sopenharmony_ci		break;
195bf215546Sopenharmony_ci	case R600_QUERY_GPU_LOAD:
196bf215546Sopenharmony_ci	case R600_QUERY_GPU_SHADERS_BUSY:
197bf215546Sopenharmony_ci	case R600_QUERY_GPU_TA_BUSY:
198bf215546Sopenharmony_ci	case R600_QUERY_GPU_GDS_BUSY:
199bf215546Sopenharmony_ci	case R600_QUERY_GPU_VGT_BUSY:
200bf215546Sopenharmony_ci	case R600_QUERY_GPU_IA_BUSY:
201bf215546Sopenharmony_ci	case R600_QUERY_GPU_SX_BUSY:
202bf215546Sopenharmony_ci	case R600_QUERY_GPU_WD_BUSY:
203bf215546Sopenharmony_ci	case R600_QUERY_GPU_BCI_BUSY:
204bf215546Sopenharmony_ci	case R600_QUERY_GPU_SC_BUSY:
205bf215546Sopenharmony_ci	case R600_QUERY_GPU_PA_BUSY:
206bf215546Sopenharmony_ci	case R600_QUERY_GPU_DB_BUSY:
207bf215546Sopenharmony_ci	case R600_QUERY_GPU_CP_BUSY:
208bf215546Sopenharmony_ci	case R600_QUERY_GPU_CB_BUSY:
209bf215546Sopenharmony_ci	case R600_QUERY_GPU_SDMA_BUSY:
210bf215546Sopenharmony_ci	case R600_QUERY_GPU_PFP_BUSY:
211bf215546Sopenharmony_ci	case R600_QUERY_GPU_MEQ_BUSY:
212bf215546Sopenharmony_ci	case R600_QUERY_GPU_ME_BUSY:
213bf215546Sopenharmony_ci	case R600_QUERY_GPU_SURF_SYNC_BUSY:
214bf215546Sopenharmony_ci	case R600_QUERY_GPU_CP_DMA_BUSY:
215bf215546Sopenharmony_ci	case R600_QUERY_GPU_SCRATCH_RAM_BUSY:
216bf215546Sopenharmony_ci		query->begin_result = r600_begin_counter(rctx->screen,
217bf215546Sopenharmony_ci							 query->b.type);
218bf215546Sopenharmony_ci		break;
219bf215546Sopenharmony_ci	case R600_QUERY_NUM_COMPILATIONS:
220bf215546Sopenharmony_ci		query->begin_result = p_atomic_read(&rctx->screen->num_compilations);
221bf215546Sopenharmony_ci		break;
222bf215546Sopenharmony_ci	case R600_QUERY_NUM_SHADERS_CREATED:
223bf215546Sopenharmony_ci		query->begin_result = p_atomic_read(&rctx->screen->num_shaders_created);
224bf215546Sopenharmony_ci		break;
225bf215546Sopenharmony_ci	case R600_QUERY_NUM_SHADER_CACHE_HITS:
226bf215546Sopenharmony_ci		query->begin_result =
227bf215546Sopenharmony_ci			p_atomic_read(&rctx->screen->num_shader_cache_hits);
228bf215546Sopenharmony_ci		break;
229bf215546Sopenharmony_ci	case R600_QUERY_GPIN_ASIC_ID:
230bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SIMD:
231bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_RB:
232bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SPI:
233bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SE:
234bf215546Sopenharmony_ci		break;
235bf215546Sopenharmony_ci	default:
236bf215546Sopenharmony_ci		unreachable("r600_query_sw_begin: bad query type");
237bf215546Sopenharmony_ci	}
238bf215546Sopenharmony_ci
239bf215546Sopenharmony_ci	return true;
240bf215546Sopenharmony_ci}
241bf215546Sopenharmony_ci
242bf215546Sopenharmony_cistatic bool r600_query_sw_end(struct r600_common_context *rctx,
243bf215546Sopenharmony_ci			      struct r600_query *rquery)
244bf215546Sopenharmony_ci{
245bf215546Sopenharmony_ci	struct r600_query_sw *query = (struct r600_query_sw *)rquery;
246bf215546Sopenharmony_ci	enum radeon_value_id ws_id;
247bf215546Sopenharmony_ci
248bf215546Sopenharmony_ci	switch(query->b.type) {
249bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP_DISJOINT:
250bf215546Sopenharmony_ci		break;
251bf215546Sopenharmony_ci	case PIPE_QUERY_GPU_FINISHED:
252bf215546Sopenharmony_ci		rctx->b.flush(&rctx->b, &query->fence, PIPE_FLUSH_DEFERRED);
253bf215546Sopenharmony_ci		break;
254bf215546Sopenharmony_ci	case R600_QUERY_DRAW_CALLS:
255bf215546Sopenharmony_ci		query->end_result = rctx->num_draw_calls;
256bf215546Sopenharmony_ci		break;
257bf215546Sopenharmony_ci	case R600_QUERY_DECOMPRESS_CALLS:
258bf215546Sopenharmony_ci		query->end_result = rctx->num_decompress_calls;
259bf215546Sopenharmony_ci		break;
260bf215546Sopenharmony_ci	case R600_QUERY_MRT_DRAW_CALLS:
261bf215546Sopenharmony_ci		query->end_result = rctx->num_mrt_draw_calls;
262bf215546Sopenharmony_ci		break;
263bf215546Sopenharmony_ci	case R600_QUERY_PRIM_RESTART_CALLS:
264bf215546Sopenharmony_ci		query->end_result = rctx->num_prim_restart_calls;
265bf215546Sopenharmony_ci		break;
266bf215546Sopenharmony_ci	case R600_QUERY_SPILL_DRAW_CALLS:
267bf215546Sopenharmony_ci		query->end_result = rctx->num_spill_draw_calls;
268bf215546Sopenharmony_ci		break;
269bf215546Sopenharmony_ci	case R600_QUERY_COMPUTE_CALLS:
270bf215546Sopenharmony_ci		query->end_result = rctx->num_compute_calls;
271bf215546Sopenharmony_ci		break;
272bf215546Sopenharmony_ci	case R600_QUERY_SPILL_COMPUTE_CALLS:
273bf215546Sopenharmony_ci		query->end_result = rctx->num_spill_compute_calls;
274bf215546Sopenharmony_ci		break;
275bf215546Sopenharmony_ci	case R600_QUERY_DMA_CALLS:
276bf215546Sopenharmony_ci		query->end_result = rctx->num_dma_calls;
277bf215546Sopenharmony_ci		break;
278bf215546Sopenharmony_ci	case R600_QUERY_CP_DMA_CALLS:
279bf215546Sopenharmony_ci		query->end_result = rctx->num_cp_dma_calls;
280bf215546Sopenharmony_ci		break;
281bf215546Sopenharmony_ci	case R600_QUERY_NUM_VS_FLUSHES:
282bf215546Sopenharmony_ci		query->end_result = rctx->num_vs_flushes;
283bf215546Sopenharmony_ci		break;
284bf215546Sopenharmony_ci	case R600_QUERY_NUM_PS_FLUSHES:
285bf215546Sopenharmony_ci		query->end_result = rctx->num_ps_flushes;
286bf215546Sopenharmony_ci		break;
287bf215546Sopenharmony_ci	case R600_QUERY_NUM_CS_FLUSHES:
288bf215546Sopenharmony_ci		query->end_result = rctx->num_cs_flushes;
289bf215546Sopenharmony_ci		break;
290bf215546Sopenharmony_ci	case R600_QUERY_NUM_CB_CACHE_FLUSHES:
291bf215546Sopenharmony_ci		query->end_result = rctx->num_cb_cache_flushes;
292bf215546Sopenharmony_ci		break;
293bf215546Sopenharmony_ci	case R600_QUERY_NUM_DB_CACHE_FLUSHES:
294bf215546Sopenharmony_ci		query->end_result = rctx->num_db_cache_flushes;
295bf215546Sopenharmony_ci		break;
296bf215546Sopenharmony_ci	case R600_QUERY_NUM_RESIDENT_HANDLES:
297bf215546Sopenharmony_ci		query->end_result = rctx->num_resident_handles;
298bf215546Sopenharmony_ci		break;
299bf215546Sopenharmony_ci	case R600_QUERY_TC_OFFLOADED_SLOTS:
300bf215546Sopenharmony_ci		query->end_result = rctx->tc ? rctx->tc->num_offloaded_slots : 0;
301bf215546Sopenharmony_ci		break;
302bf215546Sopenharmony_ci	case R600_QUERY_TC_DIRECT_SLOTS:
303bf215546Sopenharmony_ci		query->end_result = rctx->tc ? rctx->tc->num_direct_slots : 0;
304bf215546Sopenharmony_ci		break;
305bf215546Sopenharmony_ci	case R600_QUERY_TC_NUM_SYNCS:
306bf215546Sopenharmony_ci		query->end_result = rctx->tc ? rctx->tc->num_syncs : 0;
307bf215546Sopenharmony_ci		break;
308bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_VRAM:
309bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_GTT:
310bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_VRAM:
311bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_GTT:
312bf215546Sopenharmony_ci	case R600_QUERY_VRAM_USAGE:
313bf215546Sopenharmony_ci	case R600_QUERY_VRAM_VIS_USAGE:
314bf215546Sopenharmony_ci	case R600_QUERY_GTT_USAGE:
315bf215546Sopenharmony_ci	case R600_QUERY_GPU_TEMPERATURE:
316bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_SCLK:
317bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_MCLK:
318bf215546Sopenharmony_ci	case R600_QUERY_BUFFER_WAIT_TIME:
319bf215546Sopenharmony_ci	case R600_QUERY_NUM_MAPPED_BUFFERS:
320bf215546Sopenharmony_ci	case R600_QUERY_NUM_GFX_IBS:
321bf215546Sopenharmony_ci	case R600_QUERY_NUM_SDMA_IBS:
322bf215546Sopenharmony_ci	case R600_QUERY_NUM_BYTES_MOVED:
323bf215546Sopenharmony_ci	case R600_QUERY_NUM_EVICTIONS:
324bf215546Sopenharmony_ci	case R600_QUERY_NUM_VRAM_CPU_PAGE_FAULTS: {
325bf215546Sopenharmony_ci		enum radeon_value_id ws_id = winsys_id_from_type(query->b.type);
326bf215546Sopenharmony_ci		query->end_result = rctx->ws->query_value(rctx->ws, ws_id);
327bf215546Sopenharmony_ci		break;
328bf215546Sopenharmony_ci	}
329bf215546Sopenharmony_ci	case R600_QUERY_GFX_BO_LIST_SIZE:
330bf215546Sopenharmony_ci		ws_id = winsys_id_from_type(query->b.type);
331bf215546Sopenharmony_ci		query->end_result = rctx->ws->query_value(rctx->ws, ws_id);
332bf215546Sopenharmony_ci		query->end_time = rctx->ws->query_value(rctx->ws,
333bf215546Sopenharmony_ci							RADEON_NUM_GFX_IBS);
334bf215546Sopenharmony_ci		break;
335bf215546Sopenharmony_ci	case R600_QUERY_CS_THREAD_BUSY:
336bf215546Sopenharmony_ci		ws_id = winsys_id_from_type(query->b.type);
337bf215546Sopenharmony_ci		query->end_result = rctx->ws->query_value(rctx->ws, ws_id);
338bf215546Sopenharmony_ci		query->end_time = os_time_get_nano();
339bf215546Sopenharmony_ci		break;
340bf215546Sopenharmony_ci	case R600_QUERY_GALLIUM_THREAD_BUSY:
341bf215546Sopenharmony_ci		query->end_result =
342bf215546Sopenharmony_ci			rctx->tc ? util_queue_get_thread_time_nano(&rctx->tc->queue, 0) : 0;
343bf215546Sopenharmony_ci		query->end_time = os_time_get_nano();
344bf215546Sopenharmony_ci		break;
345bf215546Sopenharmony_ci	case R600_QUERY_GPU_LOAD:
346bf215546Sopenharmony_ci	case R600_QUERY_GPU_SHADERS_BUSY:
347bf215546Sopenharmony_ci	case R600_QUERY_GPU_TA_BUSY:
348bf215546Sopenharmony_ci	case R600_QUERY_GPU_GDS_BUSY:
349bf215546Sopenharmony_ci	case R600_QUERY_GPU_VGT_BUSY:
350bf215546Sopenharmony_ci	case R600_QUERY_GPU_IA_BUSY:
351bf215546Sopenharmony_ci	case R600_QUERY_GPU_SX_BUSY:
352bf215546Sopenharmony_ci	case R600_QUERY_GPU_WD_BUSY:
353bf215546Sopenharmony_ci	case R600_QUERY_GPU_BCI_BUSY:
354bf215546Sopenharmony_ci	case R600_QUERY_GPU_SC_BUSY:
355bf215546Sopenharmony_ci	case R600_QUERY_GPU_PA_BUSY:
356bf215546Sopenharmony_ci	case R600_QUERY_GPU_DB_BUSY:
357bf215546Sopenharmony_ci	case R600_QUERY_GPU_CP_BUSY:
358bf215546Sopenharmony_ci	case R600_QUERY_GPU_CB_BUSY:
359bf215546Sopenharmony_ci	case R600_QUERY_GPU_SDMA_BUSY:
360bf215546Sopenharmony_ci	case R600_QUERY_GPU_PFP_BUSY:
361bf215546Sopenharmony_ci	case R600_QUERY_GPU_MEQ_BUSY:
362bf215546Sopenharmony_ci	case R600_QUERY_GPU_ME_BUSY:
363bf215546Sopenharmony_ci	case R600_QUERY_GPU_SURF_SYNC_BUSY:
364bf215546Sopenharmony_ci	case R600_QUERY_GPU_CP_DMA_BUSY:
365bf215546Sopenharmony_ci	case R600_QUERY_GPU_SCRATCH_RAM_BUSY:
366bf215546Sopenharmony_ci		query->end_result = r600_end_counter(rctx->screen,
367bf215546Sopenharmony_ci						     query->b.type,
368bf215546Sopenharmony_ci						     query->begin_result);
369bf215546Sopenharmony_ci		query->begin_result = 0;
370bf215546Sopenharmony_ci		break;
371bf215546Sopenharmony_ci	case R600_QUERY_NUM_COMPILATIONS:
372bf215546Sopenharmony_ci		query->end_result = p_atomic_read(&rctx->screen->num_compilations);
373bf215546Sopenharmony_ci		break;
374bf215546Sopenharmony_ci	case R600_QUERY_NUM_SHADERS_CREATED:
375bf215546Sopenharmony_ci		query->end_result = p_atomic_read(&rctx->screen->num_shaders_created);
376bf215546Sopenharmony_ci		break;
377bf215546Sopenharmony_ci	case R600_QUERY_NUM_SHADER_CACHE_HITS:
378bf215546Sopenharmony_ci		query->end_result =
379bf215546Sopenharmony_ci			p_atomic_read(&rctx->screen->num_shader_cache_hits);
380bf215546Sopenharmony_ci		break;
381bf215546Sopenharmony_ci	case R600_QUERY_GPIN_ASIC_ID:
382bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SIMD:
383bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_RB:
384bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SPI:
385bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SE:
386bf215546Sopenharmony_ci		break;
387bf215546Sopenharmony_ci	default:
388bf215546Sopenharmony_ci		unreachable("r600_query_sw_end: bad query type");
389bf215546Sopenharmony_ci	}
390bf215546Sopenharmony_ci
391bf215546Sopenharmony_ci	return true;
392bf215546Sopenharmony_ci}
393bf215546Sopenharmony_ci
394bf215546Sopenharmony_cistatic bool r600_query_sw_get_result(struct r600_common_context *rctx,
395bf215546Sopenharmony_ci				     struct r600_query *rquery,
396bf215546Sopenharmony_ci				     bool wait,
397bf215546Sopenharmony_ci				     union pipe_query_result *result)
398bf215546Sopenharmony_ci{
399bf215546Sopenharmony_ci	struct r600_query_sw *query = (struct r600_query_sw *)rquery;
400bf215546Sopenharmony_ci
401bf215546Sopenharmony_ci	switch (query->b.type) {
402bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP_DISJOINT:
403bf215546Sopenharmony_ci		/* Convert from cycles per millisecond to cycles per second (Hz). */
404bf215546Sopenharmony_ci		result->timestamp_disjoint.frequency =
405bf215546Sopenharmony_ci			(uint64_t)rctx->screen->info.clock_crystal_freq * 1000;
406bf215546Sopenharmony_ci		result->timestamp_disjoint.disjoint = false;
407bf215546Sopenharmony_ci		return true;
408bf215546Sopenharmony_ci	case PIPE_QUERY_GPU_FINISHED: {
409bf215546Sopenharmony_ci		struct pipe_screen *screen = rctx->b.screen;
410bf215546Sopenharmony_ci		struct pipe_context *ctx = rquery->b.flushed ? NULL : &rctx->b;
411bf215546Sopenharmony_ci
412bf215546Sopenharmony_ci		result->b = screen->fence_finish(screen, ctx, query->fence,
413bf215546Sopenharmony_ci						 wait ? PIPE_TIMEOUT_INFINITE : 0);
414bf215546Sopenharmony_ci		return result->b;
415bf215546Sopenharmony_ci	}
416bf215546Sopenharmony_ci
417bf215546Sopenharmony_ci	case R600_QUERY_GFX_BO_LIST_SIZE:
418bf215546Sopenharmony_ci		result->u64 = (query->end_result - query->begin_result) /
419bf215546Sopenharmony_ci			      (query->end_time - query->begin_time);
420bf215546Sopenharmony_ci		return true;
421bf215546Sopenharmony_ci	case R600_QUERY_CS_THREAD_BUSY:
422bf215546Sopenharmony_ci	case R600_QUERY_GALLIUM_THREAD_BUSY:
423bf215546Sopenharmony_ci		result->u64 = (query->end_result - query->begin_result) * 100 /
424bf215546Sopenharmony_ci			      (query->end_time - query->begin_time);
425bf215546Sopenharmony_ci		return true;
426bf215546Sopenharmony_ci	case R600_QUERY_GPIN_ASIC_ID:
427bf215546Sopenharmony_ci		result->u32 = 0;
428bf215546Sopenharmony_ci		return true;
429bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SIMD:
430bf215546Sopenharmony_ci		result->u32 = rctx->screen->info.num_cu;
431bf215546Sopenharmony_ci		return true;
432bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_RB:
433bf215546Sopenharmony_ci		result->u32 = rctx->screen->info.max_render_backends;
434bf215546Sopenharmony_ci		return true;
435bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SPI:
436bf215546Sopenharmony_ci		result->u32 = 1; /* all supported chips have one SPI per SE */
437bf215546Sopenharmony_ci		return true;
438bf215546Sopenharmony_ci	case R600_QUERY_GPIN_NUM_SE:
439bf215546Sopenharmony_ci		result->u32 = rctx->screen->info.max_se;
440bf215546Sopenharmony_ci		return true;
441bf215546Sopenharmony_ci	}
442bf215546Sopenharmony_ci
443bf215546Sopenharmony_ci	result->u64 = query->end_result - query->begin_result;
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci	switch (query->b.type) {
446bf215546Sopenharmony_ci	case R600_QUERY_BUFFER_WAIT_TIME:
447bf215546Sopenharmony_ci	case R600_QUERY_GPU_TEMPERATURE:
448bf215546Sopenharmony_ci		result->u64 /= 1000;
449bf215546Sopenharmony_ci		break;
450bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_SCLK:
451bf215546Sopenharmony_ci	case R600_QUERY_CURRENT_GPU_MCLK:
452bf215546Sopenharmony_ci		result->u64 *= 1000000;
453bf215546Sopenharmony_ci		break;
454bf215546Sopenharmony_ci	}
455bf215546Sopenharmony_ci
456bf215546Sopenharmony_ci	return true;
457bf215546Sopenharmony_ci}
458bf215546Sopenharmony_ci
459bf215546Sopenharmony_ci
460bf215546Sopenharmony_cistatic struct r600_query_ops sw_query_ops = {
461bf215546Sopenharmony_ci	.destroy = r600_query_sw_destroy,
462bf215546Sopenharmony_ci	.begin = r600_query_sw_begin,
463bf215546Sopenharmony_ci	.end = r600_query_sw_end,
464bf215546Sopenharmony_ci	.get_result = r600_query_sw_get_result,
465bf215546Sopenharmony_ci	.get_result_resource = NULL
466bf215546Sopenharmony_ci};
467bf215546Sopenharmony_ci
468bf215546Sopenharmony_cistatic struct pipe_query *r600_query_sw_create(unsigned query_type)
469bf215546Sopenharmony_ci{
470bf215546Sopenharmony_ci	struct r600_query_sw *query;
471bf215546Sopenharmony_ci
472bf215546Sopenharmony_ci	query = CALLOC_STRUCT(r600_query_sw);
473bf215546Sopenharmony_ci	if (!query)
474bf215546Sopenharmony_ci		return NULL;
475bf215546Sopenharmony_ci
476bf215546Sopenharmony_ci	query->b.type = query_type;
477bf215546Sopenharmony_ci	query->b.ops = &sw_query_ops;
478bf215546Sopenharmony_ci
479bf215546Sopenharmony_ci	return (struct pipe_query *)query;
480bf215546Sopenharmony_ci}
481bf215546Sopenharmony_ci
482bf215546Sopenharmony_civoid r600_query_hw_destroy(struct r600_common_screen *rscreen,
483bf215546Sopenharmony_ci			   struct r600_query *rquery)
484bf215546Sopenharmony_ci{
485bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)rquery;
486bf215546Sopenharmony_ci	struct r600_query_buffer *prev = query->buffer.previous;
487bf215546Sopenharmony_ci
488bf215546Sopenharmony_ci	/* Release all query buffers. */
489bf215546Sopenharmony_ci	while (prev) {
490bf215546Sopenharmony_ci		struct r600_query_buffer *qbuf = prev;
491bf215546Sopenharmony_ci		prev = prev->previous;
492bf215546Sopenharmony_ci		r600_resource_reference(&qbuf->buf, NULL);
493bf215546Sopenharmony_ci		FREE(qbuf);
494bf215546Sopenharmony_ci	}
495bf215546Sopenharmony_ci
496bf215546Sopenharmony_ci	r600_resource_reference(&query->buffer.buf, NULL);
497bf215546Sopenharmony_ci	FREE(rquery);
498bf215546Sopenharmony_ci}
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_cistatic struct r600_resource *r600_new_query_buffer(struct r600_common_screen *rscreen,
501bf215546Sopenharmony_ci						   struct r600_query_hw *query)
502bf215546Sopenharmony_ci{
503bf215546Sopenharmony_ci	unsigned buf_size = MAX2(query->result_size,
504bf215546Sopenharmony_ci				 rscreen->info.min_alloc_size);
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci	/* Queries are normally read by the CPU after
507bf215546Sopenharmony_ci	 * being written by the gpu, hence staging is probably a good
508bf215546Sopenharmony_ci	 * usage pattern.
509bf215546Sopenharmony_ci	 */
510bf215546Sopenharmony_ci	struct r600_resource *buf = (struct r600_resource*)
511bf215546Sopenharmony_ci		pipe_buffer_create(&rscreen->b, 0,
512bf215546Sopenharmony_ci				   PIPE_USAGE_STAGING, buf_size);
513bf215546Sopenharmony_ci	if (!buf)
514bf215546Sopenharmony_ci		return NULL;
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci	if (!query->ops->prepare_buffer(rscreen, query, buf)) {
517bf215546Sopenharmony_ci		r600_resource_reference(&buf, NULL);
518bf215546Sopenharmony_ci		return NULL;
519bf215546Sopenharmony_ci	}
520bf215546Sopenharmony_ci
521bf215546Sopenharmony_ci	return buf;
522bf215546Sopenharmony_ci}
523bf215546Sopenharmony_ci
524bf215546Sopenharmony_cistatic bool r600_query_hw_prepare_buffer(struct r600_common_screen *rscreen,
525bf215546Sopenharmony_ci					 struct r600_query_hw *query,
526bf215546Sopenharmony_ci					 struct r600_resource *buffer)
527bf215546Sopenharmony_ci{
528bf215546Sopenharmony_ci	/* Callers ensure that the buffer is currently unused by the GPU. */
529bf215546Sopenharmony_ci	uint32_t *results = rscreen->ws->buffer_map(rscreen->ws, buffer->buf, NULL,
530bf215546Sopenharmony_ci						   PIPE_MAP_WRITE |
531bf215546Sopenharmony_ci						   PIPE_MAP_UNSYNCHRONIZED);
532bf215546Sopenharmony_ci	if (!results)
533bf215546Sopenharmony_ci		return false;
534bf215546Sopenharmony_ci
535bf215546Sopenharmony_ci	memset(results, 0, buffer->b.b.width0);
536bf215546Sopenharmony_ci
537bf215546Sopenharmony_ci	if (query->b.type == PIPE_QUERY_OCCLUSION_COUNTER ||
538bf215546Sopenharmony_ci	    query->b.type == PIPE_QUERY_OCCLUSION_PREDICATE ||
539bf215546Sopenharmony_ci	    query->b.type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
540bf215546Sopenharmony_ci		unsigned max_rbs = rscreen->info.max_render_backends;
541bf215546Sopenharmony_ci		unsigned enabled_rb_mask = rscreen->info.enabled_rb_mask;
542bf215546Sopenharmony_ci		unsigned num_results;
543bf215546Sopenharmony_ci		unsigned i, j;
544bf215546Sopenharmony_ci
545bf215546Sopenharmony_ci		/* Set top bits for unused backends. */
546bf215546Sopenharmony_ci		num_results = buffer->b.b.width0 / query->result_size;
547bf215546Sopenharmony_ci		for (j = 0; j < num_results; j++) {
548bf215546Sopenharmony_ci			for (i = 0; i < max_rbs; i++) {
549bf215546Sopenharmony_ci				if (!(enabled_rb_mask & (1<<i))) {
550bf215546Sopenharmony_ci					results[(i * 4)+1] = 0x80000000;
551bf215546Sopenharmony_ci					results[(i * 4)+3] = 0x80000000;
552bf215546Sopenharmony_ci				}
553bf215546Sopenharmony_ci			}
554bf215546Sopenharmony_ci			results += 4 * max_rbs;
555bf215546Sopenharmony_ci		}
556bf215546Sopenharmony_ci	}
557bf215546Sopenharmony_ci
558bf215546Sopenharmony_ci	return true;
559bf215546Sopenharmony_ci}
560bf215546Sopenharmony_ci
561bf215546Sopenharmony_cistatic void r600_query_hw_get_result_resource(struct r600_common_context *rctx,
562bf215546Sopenharmony_ci                                              struct r600_query *rquery,
563bf215546Sopenharmony_ci                                              enum pipe_query_flags flags,
564bf215546Sopenharmony_ci                                              enum pipe_query_value_type result_type,
565bf215546Sopenharmony_ci                                              int index,
566bf215546Sopenharmony_ci                                              struct pipe_resource *resource,
567bf215546Sopenharmony_ci                                              unsigned offset);
568bf215546Sopenharmony_ci
569bf215546Sopenharmony_cistatic struct r600_query_ops query_hw_ops = {
570bf215546Sopenharmony_ci	.destroy = r600_query_hw_destroy,
571bf215546Sopenharmony_ci	.begin = r600_query_hw_begin,
572bf215546Sopenharmony_ci	.end = r600_query_hw_end,
573bf215546Sopenharmony_ci	.get_result = r600_query_hw_get_result,
574bf215546Sopenharmony_ci	.get_result_resource = r600_query_hw_get_result_resource,
575bf215546Sopenharmony_ci};
576bf215546Sopenharmony_ci
577bf215546Sopenharmony_cistatic void r600_query_hw_do_emit_start(struct r600_common_context *ctx,
578bf215546Sopenharmony_ci					struct r600_query_hw *query,
579bf215546Sopenharmony_ci					struct r600_resource *buffer,
580bf215546Sopenharmony_ci					uint64_t va);
581bf215546Sopenharmony_cistatic void r600_query_hw_do_emit_stop(struct r600_common_context *ctx,
582bf215546Sopenharmony_ci				       struct r600_query_hw *query,
583bf215546Sopenharmony_ci				       struct r600_resource *buffer,
584bf215546Sopenharmony_ci				       uint64_t va);
585bf215546Sopenharmony_cistatic void r600_query_hw_add_result(struct r600_common_screen *rscreen,
586bf215546Sopenharmony_ci				     struct r600_query_hw *, void *buffer,
587bf215546Sopenharmony_ci				     union pipe_query_result *result);
588bf215546Sopenharmony_cistatic void r600_query_hw_clear_result(struct r600_query_hw *,
589bf215546Sopenharmony_ci				       union pipe_query_result *);
590bf215546Sopenharmony_ci
591bf215546Sopenharmony_cistatic struct r600_query_hw_ops query_hw_default_hw_ops = {
592bf215546Sopenharmony_ci	.prepare_buffer = r600_query_hw_prepare_buffer,
593bf215546Sopenharmony_ci	.emit_start = r600_query_hw_do_emit_start,
594bf215546Sopenharmony_ci	.emit_stop = r600_query_hw_do_emit_stop,
595bf215546Sopenharmony_ci	.clear_result = r600_query_hw_clear_result,
596bf215546Sopenharmony_ci	.add_result = r600_query_hw_add_result,
597bf215546Sopenharmony_ci};
598bf215546Sopenharmony_ci
599bf215546Sopenharmony_cibool r600_query_hw_init(struct r600_common_screen *rscreen,
600bf215546Sopenharmony_ci			struct r600_query_hw *query)
601bf215546Sopenharmony_ci{
602bf215546Sopenharmony_ci	query->buffer.buf = r600_new_query_buffer(rscreen, query);
603bf215546Sopenharmony_ci	if (!query->buffer.buf)
604bf215546Sopenharmony_ci		return false;
605bf215546Sopenharmony_ci
606bf215546Sopenharmony_ci	return true;
607bf215546Sopenharmony_ci}
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_cistatic struct pipe_query *r600_query_hw_create(struct r600_common_screen *rscreen,
610bf215546Sopenharmony_ci					       unsigned query_type,
611bf215546Sopenharmony_ci					       unsigned index)
612bf215546Sopenharmony_ci{
613bf215546Sopenharmony_ci	struct r600_query_hw *query = CALLOC_STRUCT(r600_query_hw);
614bf215546Sopenharmony_ci	if (!query)
615bf215546Sopenharmony_ci		return NULL;
616bf215546Sopenharmony_ci
617bf215546Sopenharmony_ci	query->b.type = query_type;
618bf215546Sopenharmony_ci	query->b.ops = &query_hw_ops;
619bf215546Sopenharmony_ci	query->ops = &query_hw_default_hw_ops;
620bf215546Sopenharmony_ci
621bf215546Sopenharmony_ci	switch (query_type) {
622bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER:
623bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
624bf215546Sopenharmony_ci  	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
625bf215546Sopenharmony_ci		query->result_size = 16 * rscreen->info.max_render_backends;
626bf215546Sopenharmony_ci		query->result_size += 16; /* for the fence + alignment */
627bf215546Sopenharmony_ci		query->num_cs_dw_begin = 6;
628bf215546Sopenharmony_ci		query->num_cs_dw_end = 6 + r600_gfx_write_fence_dwords(rscreen);
629bf215546Sopenharmony_ci		break;
630bf215546Sopenharmony_ci	case PIPE_QUERY_TIME_ELAPSED:
631bf215546Sopenharmony_ci		query->result_size = 24;
632bf215546Sopenharmony_ci		query->num_cs_dw_begin = 8;
633bf215546Sopenharmony_ci		query->num_cs_dw_end = 8 + r600_gfx_write_fence_dwords(rscreen);
634bf215546Sopenharmony_ci		break;
635bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP:
636bf215546Sopenharmony_ci		query->result_size = 16;
637bf215546Sopenharmony_ci		query->num_cs_dw_end = 8 + r600_gfx_write_fence_dwords(rscreen);
638bf215546Sopenharmony_ci		query->flags = R600_QUERY_HW_FLAG_NO_START;
639bf215546Sopenharmony_ci		break;
640bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_EMITTED:
641bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_GENERATED:
642bf215546Sopenharmony_ci	case PIPE_QUERY_SO_STATISTICS:
643bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
644bf215546Sopenharmony_ci		/* NumPrimitivesWritten, PrimitiveStorageNeeded. */
645bf215546Sopenharmony_ci		query->result_size = 32;
646bf215546Sopenharmony_ci		query->num_cs_dw_begin = 6;
647bf215546Sopenharmony_ci		query->num_cs_dw_end = 6;
648bf215546Sopenharmony_ci		query->stream = index;
649bf215546Sopenharmony_ci		break;
650bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
651bf215546Sopenharmony_ci		/* NumPrimitivesWritten, PrimitiveStorageNeeded. */
652bf215546Sopenharmony_ci		query->result_size = 32 * R600_MAX_STREAMS;
653bf215546Sopenharmony_ci		query->num_cs_dw_begin = 6 * R600_MAX_STREAMS;
654bf215546Sopenharmony_ci		query->num_cs_dw_end = 6 * R600_MAX_STREAMS;
655bf215546Sopenharmony_ci		break;
656bf215546Sopenharmony_ci	case PIPE_QUERY_PIPELINE_STATISTICS:
657bf215546Sopenharmony_ci		/* 11 values on EG, 8 on R600. */
658bf215546Sopenharmony_ci		query->result_size = (rscreen->gfx_level >= EVERGREEN ? 11 : 8) * 16;
659bf215546Sopenharmony_ci		query->result_size += 8; /* for the fence + alignment */
660bf215546Sopenharmony_ci		query->num_cs_dw_begin = 6;
661bf215546Sopenharmony_ci		query->num_cs_dw_end = 6 + r600_gfx_write_fence_dwords(rscreen);
662bf215546Sopenharmony_ci		break;
663bf215546Sopenharmony_ci	default:
664bf215546Sopenharmony_ci		assert(0);
665bf215546Sopenharmony_ci		FREE(query);
666bf215546Sopenharmony_ci		return NULL;
667bf215546Sopenharmony_ci	}
668bf215546Sopenharmony_ci
669bf215546Sopenharmony_ci	if (!r600_query_hw_init(rscreen, query)) {
670bf215546Sopenharmony_ci		FREE(query);
671bf215546Sopenharmony_ci		return NULL;
672bf215546Sopenharmony_ci	}
673bf215546Sopenharmony_ci
674bf215546Sopenharmony_ci	return (struct pipe_query *)query;
675bf215546Sopenharmony_ci}
676bf215546Sopenharmony_ci
677bf215546Sopenharmony_cistatic void r600_update_occlusion_query_state(struct r600_common_context *rctx,
678bf215546Sopenharmony_ci					      unsigned type, int diff)
679bf215546Sopenharmony_ci{
680bf215546Sopenharmony_ci	if (type == PIPE_QUERY_OCCLUSION_COUNTER ||
681bf215546Sopenharmony_ci	    type == PIPE_QUERY_OCCLUSION_PREDICATE ||
682bf215546Sopenharmony_ci	    type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
683bf215546Sopenharmony_ci		bool old_enable = rctx->num_occlusion_queries != 0;
684bf215546Sopenharmony_ci		bool old_perfect_enable =
685bf215546Sopenharmony_ci			rctx->num_perfect_occlusion_queries != 0;
686bf215546Sopenharmony_ci		bool enable, perfect_enable;
687bf215546Sopenharmony_ci
688bf215546Sopenharmony_ci		rctx->num_occlusion_queries += diff;
689bf215546Sopenharmony_ci		assert(rctx->num_occlusion_queries >= 0);
690bf215546Sopenharmony_ci
691bf215546Sopenharmony_ci		if (type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
692bf215546Sopenharmony_ci			rctx->num_perfect_occlusion_queries += diff;
693bf215546Sopenharmony_ci			assert(rctx->num_perfect_occlusion_queries >= 0);
694bf215546Sopenharmony_ci		}
695bf215546Sopenharmony_ci
696bf215546Sopenharmony_ci		enable = rctx->num_occlusion_queries != 0;
697bf215546Sopenharmony_ci		perfect_enable = rctx->num_perfect_occlusion_queries != 0;
698bf215546Sopenharmony_ci
699bf215546Sopenharmony_ci		if (enable != old_enable || perfect_enable != old_perfect_enable) {
700bf215546Sopenharmony_ci			struct r600_context *ctx = (struct r600_context*)rctx;
701bf215546Sopenharmony_ci			r600_mark_atom_dirty(ctx, &ctx->db_misc_state.atom);
702bf215546Sopenharmony_ci		}
703bf215546Sopenharmony_ci	}
704bf215546Sopenharmony_ci}
705bf215546Sopenharmony_ci
706bf215546Sopenharmony_cistatic unsigned event_type_for_stream(unsigned stream)
707bf215546Sopenharmony_ci{
708bf215546Sopenharmony_ci	switch (stream) {
709bf215546Sopenharmony_ci	default:
710bf215546Sopenharmony_ci	case 0: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS;
711bf215546Sopenharmony_ci	case 1: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS1;
712bf215546Sopenharmony_ci	case 2: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS2;
713bf215546Sopenharmony_ci	case 3: return EVENT_TYPE_SAMPLE_STREAMOUTSTATS3;
714bf215546Sopenharmony_ci	}
715bf215546Sopenharmony_ci}
716bf215546Sopenharmony_ci
717bf215546Sopenharmony_cistatic void emit_sample_streamout(struct radeon_cmdbuf *cs, uint64_t va,
718bf215546Sopenharmony_ci				  unsigned stream)
719bf215546Sopenharmony_ci{
720bf215546Sopenharmony_ci	radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
721bf215546Sopenharmony_ci	radeon_emit(cs, EVENT_TYPE(event_type_for_stream(stream)) | EVENT_INDEX(3));
722bf215546Sopenharmony_ci	radeon_emit(cs, va);
723bf215546Sopenharmony_ci	radeon_emit(cs, va >> 32);
724bf215546Sopenharmony_ci}
725bf215546Sopenharmony_ci
726bf215546Sopenharmony_cistatic void r600_query_hw_do_emit_start(struct r600_common_context *ctx,
727bf215546Sopenharmony_ci					struct r600_query_hw *query,
728bf215546Sopenharmony_ci					struct r600_resource *buffer,
729bf215546Sopenharmony_ci					uint64_t va)
730bf215546Sopenharmony_ci{
731bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &ctx->gfx.cs;
732bf215546Sopenharmony_ci
733bf215546Sopenharmony_ci	switch (query->b.type) {
734bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER:
735bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
736bf215546Sopenharmony_ci  	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
737bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
738bf215546Sopenharmony_ci		radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
739bf215546Sopenharmony_ci		radeon_emit(cs, va);
740bf215546Sopenharmony_ci		radeon_emit(cs, va >> 32);
741bf215546Sopenharmony_ci		break;
742bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_EMITTED:
743bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_GENERATED:
744bf215546Sopenharmony_ci	case PIPE_QUERY_SO_STATISTICS:
745bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
746bf215546Sopenharmony_ci		emit_sample_streamout(cs, va, query->stream);
747bf215546Sopenharmony_ci		break;
748bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
749bf215546Sopenharmony_ci		for (unsigned stream = 0; stream < R600_MAX_STREAMS; ++stream)
750bf215546Sopenharmony_ci			emit_sample_streamout(cs, va + 32 * stream, stream);
751bf215546Sopenharmony_ci		break;
752bf215546Sopenharmony_ci	case PIPE_QUERY_TIME_ELAPSED:
753bf215546Sopenharmony_ci		/* Write the timestamp after the last draw is done.
754bf215546Sopenharmony_ci		 * (bottom-of-pipe)
755bf215546Sopenharmony_ci		 */
756bf215546Sopenharmony_ci		r600_gfx_write_event_eop(ctx, EVENT_TYPE_BOTTOM_OF_PIPE_TS,
757bf215546Sopenharmony_ci					 0, EOP_DATA_SEL_TIMESTAMP,
758bf215546Sopenharmony_ci					 NULL, va, 0, query->b.type);
759bf215546Sopenharmony_ci		break;
760bf215546Sopenharmony_ci	case PIPE_QUERY_PIPELINE_STATISTICS:
761bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
762bf215546Sopenharmony_ci		radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT) | EVENT_INDEX(2));
763bf215546Sopenharmony_ci		radeon_emit(cs, va);
764bf215546Sopenharmony_ci		radeon_emit(cs, va >> 32);
765bf215546Sopenharmony_ci		break;
766bf215546Sopenharmony_ci	default:
767bf215546Sopenharmony_ci		assert(0);
768bf215546Sopenharmony_ci	}
769bf215546Sopenharmony_ci	r600_emit_reloc(ctx, &ctx->gfx, query->buffer.buf, RADEON_USAGE_WRITE |
770bf215546Sopenharmony_ci			RADEON_PRIO_QUERY);
771bf215546Sopenharmony_ci}
772bf215546Sopenharmony_ci
773bf215546Sopenharmony_cistatic void r600_query_hw_emit_start(struct r600_common_context *ctx,
774bf215546Sopenharmony_ci				     struct r600_query_hw *query)
775bf215546Sopenharmony_ci{
776bf215546Sopenharmony_ci	uint64_t va;
777bf215546Sopenharmony_ci
778bf215546Sopenharmony_ci	if (!query->buffer.buf)
779bf215546Sopenharmony_ci		return; // previous buffer allocation failure
780bf215546Sopenharmony_ci
781bf215546Sopenharmony_ci	r600_update_occlusion_query_state(ctx, query->b.type, 1);
782bf215546Sopenharmony_ci	r600_update_prims_generated_query_state(ctx, query->b.type, 1);
783bf215546Sopenharmony_ci
784bf215546Sopenharmony_ci	ctx->need_gfx_cs_space(&ctx->b, query->num_cs_dw_begin + query->num_cs_dw_end,
785bf215546Sopenharmony_ci			       true);
786bf215546Sopenharmony_ci
787bf215546Sopenharmony_ci	/* Get a new query buffer if needed. */
788bf215546Sopenharmony_ci	if (query->buffer.results_end + query->result_size > query->buffer.buf->b.b.width0) {
789bf215546Sopenharmony_ci		struct r600_query_buffer *qbuf = MALLOC_STRUCT(r600_query_buffer);
790bf215546Sopenharmony_ci		*qbuf = query->buffer;
791bf215546Sopenharmony_ci		query->buffer.results_end = 0;
792bf215546Sopenharmony_ci		query->buffer.previous = qbuf;
793bf215546Sopenharmony_ci		query->buffer.buf = r600_new_query_buffer(ctx->screen, query);
794bf215546Sopenharmony_ci		if (!query->buffer.buf)
795bf215546Sopenharmony_ci			return;
796bf215546Sopenharmony_ci	}
797bf215546Sopenharmony_ci
798bf215546Sopenharmony_ci	/* emit begin query */
799bf215546Sopenharmony_ci	va = query->buffer.buf->gpu_address + query->buffer.results_end;
800bf215546Sopenharmony_ci
801bf215546Sopenharmony_ci	query->ops->emit_start(ctx, query, query->buffer.buf, va);
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_ci	ctx->num_cs_dw_queries_suspend += query->num_cs_dw_end;
804bf215546Sopenharmony_ci}
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_cistatic void r600_query_hw_do_emit_stop(struct r600_common_context *ctx,
807bf215546Sopenharmony_ci				       struct r600_query_hw *query,
808bf215546Sopenharmony_ci				       struct r600_resource *buffer,
809bf215546Sopenharmony_ci				       uint64_t va)
810bf215546Sopenharmony_ci{
811bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &ctx->gfx.cs;
812bf215546Sopenharmony_ci	uint64_t fence_va = 0;
813bf215546Sopenharmony_ci
814bf215546Sopenharmony_ci	switch (query->b.type) {
815bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER:
816bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
817bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
818bf215546Sopenharmony_ci		va += 8;
819bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
820bf215546Sopenharmony_ci		radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
821bf215546Sopenharmony_ci		radeon_emit(cs, va);
822bf215546Sopenharmony_ci		radeon_emit(cs, va >> 32);
823bf215546Sopenharmony_ci
824bf215546Sopenharmony_ci		fence_va = va + ctx->screen->info.max_render_backends * 16 - 8;
825bf215546Sopenharmony_ci		break;
826bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_EMITTED:
827bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_GENERATED:
828bf215546Sopenharmony_ci	case PIPE_QUERY_SO_STATISTICS:
829bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
830bf215546Sopenharmony_ci		va += 16;
831bf215546Sopenharmony_ci		emit_sample_streamout(cs, va, query->stream);
832bf215546Sopenharmony_ci		break;
833bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
834bf215546Sopenharmony_ci		va += 16;
835bf215546Sopenharmony_ci		for (unsigned stream = 0; stream < R600_MAX_STREAMS; ++stream)
836bf215546Sopenharmony_ci			emit_sample_streamout(cs, va + 32 * stream, stream);
837bf215546Sopenharmony_ci		break;
838bf215546Sopenharmony_ci	case PIPE_QUERY_TIME_ELAPSED:
839bf215546Sopenharmony_ci		va += 8;
840bf215546Sopenharmony_ci		FALLTHROUGH;
841bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP:
842bf215546Sopenharmony_ci		r600_gfx_write_event_eop(ctx, EVENT_TYPE_BOTTOM_OF_PIPE_TS,
843bf215546Sopenharmony_ci					 0, EOP_DATA_SEL_TIMESTAMP, NULL, va,
844bf215546Sopenharmony_ci					 0, query->b.type);
845bf215546Sopenharmony_ci		fence_va = va + 8;
846bf215546Sopenharmony_ci		break;
847bf215546Sopenharmony_ci	case PIPE_QUERY_PIPELINE_STATISTICS: {
848bf215546Sopenharmony_ci		unsigned sample_size = (query->result_size - 8) / 2;
849bf215546Sopenharmony_ci
850bf215546Sopenharmony_ci		va += sample_size;
851bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
852bf215546Sopenharmony_ci		radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_SAMPLE_PIPELINESTAT) | EVENT_INDEX(2));
853bf215546Sopenharmony_ci		radeon_emit(cs, va);
854bf215546Sopenharmony_ci		radeon_emit(cs, va >> 32);
855bf215546Sopenharmony_ci
856bf215546Sopenharmony_ci		fence_va = va + sample_size;
857bf215546Sopenharmony_ci		break;
858bf215546Sopenharmony_ci	}
859bf215546Sopenharmony_ci	default:
860bf215546Sopenharmony_ci		assert(0);
861bf215546Sopenharmony_ci	}
862bf215546Sopenharmony_ci	r600_emit_reloc(ctx, &ctx->gfx, query->buffer.buf, RADEON_USAGE_WRITE |
863bf215546Sopenharmony_ci			RADEON_PRIO_QUERY);
864bf215546Sopenharmony_ci
865bf215546Sopenharmony_ci	if (fence_va)
866bf215546Sopenharmony_ci		r600_gfx_write_event_eop(ctx, EVENT_TYPE_BOTTOM_OF_PIPE_TS, 0,
867bf215546Sopenharmony_ci					 EOP_DATA_SEL_VALUE_32BIT,
868bf215546Sopenharmony_ci					 query->buffer.buf, fence_va, 0x80000000,
869bf215546Sopenharmony_ci					 query->b.type);
870bf215546Sopenharmony_ci}
871bf215546Sopenharmony_ci
872bf215546Sopenharmony_cistatic void r600_query_hw_emit_stop(struct r600_common_context *ctx,
873bf215546Sopenharmony_ci				    struct r600_query_hw *query)
874bf215546Sopenharmony_ci{
875bf215546Sopenharmony_ci	uint64_t va;
876bf215546Sopenharmony_ci
877bf215546Sopenharmony_ci	if (!query->buffer.buf)
878bf215546Sopenharmony_ci		return; // previous buffer allocation failure
879bf215546Sopenharmony_ci
880bf215546Sopenharmony_ci	/* The queries which need begin already called this in begin_query. */
881bf215546Sopenharmony_ci	if (query->flags & R600_QUERY_HW_FLAG_NO_START) {
882bf215546Sopenharmony_ci		ctx->need_gfx_cs_space(&ctx->b, query->num_cs_dw_end, false);
883bf215546Sopenharmony_ci	}
884bf215546Sopenharmony_ci
885bf215546Sopenharmony_ci	/* emit end query */
886bf215546Sopenharmony_ci	va = query->buffer.buf->gpu_address + query->buffer.results_end;
887bf215546Sopenharmony_ci
888bf215546Sopenharmony_ci	query->ops->emit_stop(ctx, query, query->buffer.buf, va);
889bf215546Sopenharmony_ci
890bf215546Sopenharmony_ci	query->buffer.results_end += query->result_size;
891bf215546Sopenharmony_ci
892bf215546Sopenharmony_ci	if (!(query->flags & R600_QUERY_HW_FLAG_NO_START))
893bf215546Sopenharmony_ci		ctx->num_cs_dw_queries_suspend -= query->num_cs_dw_end;
894bf215546Sopenharmony_ci
895bf215546Sopenharmony_ci	r600_update_occlusion_query_state(ctx, query->b.type, -1);
896bf215546Sopenharmony_ci	r600_update_prims_generated_query_state(ctx, query->b.type, -1);
897bf215546Sopenharmony_ci}
898bf215546Sopenharmony_ci
899bf215546Sopenharmony_cistatic void emit_set_predicate(struct r600_common_context *ctx,
900bf215546Sopenharmony_ci			       struct r600_resource *buf, uint64_t va,
901bf215546Sopenharmony_ci			       uint32_t op)
902bf215546Sopenharmony_ci{
903bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &ctx->gfx.cs;
904bf215546Sopenharmony_ci
905bf215546Sopenharmony_ci	radeon_emit(cs, PKT3(PKT3_SET_PREDICATION, 1, 0));
906bf215546Sopenharmony_ci	radeon_emit(cs, va);
907bf215546Sopenharmony_ci	radeon_emit(cs, op | ((va >> 32) & 0xFF));
908bf215546Sopenharmony_ci	r600_emit_reloc(ctx, &ctx->gfx, buf, RADEON_USAGE_READ |
909bf215546Sopenharmony_ci			RADEON_PRIO_QUERY);
910bf215546Sopenharmony_ci}
911bf215546Sopenharmony_ci
912bf215546Sopenharmony_cistatic void r600_emit_query_predication(struct r600_common_context *ctx,
913bf215546Sopenharmony_ci					struct r600_atom *atom)
914bf215546Sopenharmony_ci{
915bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)ctx->render_cond;
916bf215546Sopenharmony_ci	struct r600_query_buffer *qbuf;
917bf215546Sopenharmony_ci	uint32_t op;
918bf215546Sopenharmony_ci	bool flag_wait, invert;
919bf215546Sopenharmony_ci
920bf215546Sopenharmony_ci	if (!query)
921bf215546Sopenharmony_ci		return;
922bf215546Sopenharmony_ci
923bf215546Sopenharmony_ci	invert = ctx->render_cond_invert;
924bf215546Sopenharmony_ci	flag_wait = ctx->render_cond_mode == PIPE_RENDER_COND_WAIT ||
925bf215546Sopenharmony_ci		    ctx->render_cond_mode == PIPE_RENDER_COND_BY_REGION_WAIT;
926bf215546Sopenharmony_ci
927bf215546Sopenharmony_ci	switch (query->b.type) {
928bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER:
929bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
930bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
931bf215546Sopenharmony_ci		op = PRED_OP(PREDICATION_OP_ZPASS);
932bf215546Sopenharmony_ci		break;
933bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
934bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
935bf215546Sopenharmony_ci		op = PRED_OP(PREDICATION_OP_PRIMCOUNT);
936bf215546Sopenharmony_ci		invert = !invert;
937bf215546Sopenharmony_ci		break;
938bf215546Sopenharmony_ci	default:
939bf215546Sopenharmony_ci		assert(0);
940bf215546Sopenharmony_ci		return;
941bf215546Sopenharmony_ci	}
942bf215546Sopenharmony_ci
943bf215546Sopenharmony_ci	/* if true then invert, see GL_ARB_conditional_render_inverted */
944bf215546Sopenharmony_ci	if (invert)
945bf215546Sopenharmony_ci		op |= PREDICATION_DRAW_NOT_VISIBLE; /* Draw if not visible or overflow */
946bf215546Sopenharmony_ci	else
947bf215546Sopenharmony_ci		op |= PREDICATION_DRAW_VISIBLE; /* Draw if visible or no overflow */
948bf215546Sopenharmony_ci
949bf215546Sopenharmony_ci	op |= flag_wait ? PREDICATION_HINT_WAIT : PREDICATION_HINT_NOWAIT_DRAW;
950bf215546Sopenharmony_ci
951bf215546Sopenharmony_ci	/* emit predicate packets for all data blocks */
952bf215546Sopenharmony_ci	for (qbuf = &query->buffer; qbuf; qbuf = qbuf->previous) {
953bf215546Sopenharmony_ci		unsigned results_base = 0;
954bf215546Sopenharmony_ci		uint64_t va_base = qbuf->buf->gpu_address;
955bf215546Sopenharmony_ci
956bf215546Sopenharmony_ci		while (results_base < qbuf->results_end) {
957bf215546Sopenharmony_ci			uint64_t va = va_base + results_base;
958bf215546Sopenharmony_ci
959bf215546Sopenharmony_ci			if (query->b.type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE) {
960bf215546Sopenharmony_ci				for (unsigned stream = 0; stream < R600_MAX_STREAMS; ++stream) {
961bf215546Sopenharmony_ci					emit_set_predicate(ctx, qbuf->buf, va + 32 * stream, op);
962bf215546Sopenharmony_ci
963bf215546Sopenharmony_ci					/* set CONTINUE bit for all packets except the first */
964bf215546Sopenharmony_ci					op |= PREDICATION_CONTINUE;
965bf215546Sopenharmony_ci				}
966bf215546Sopenharmony_ci			} else {
967bf215546Sopenharmony_ci				emit_set_predicate(ctx, qbuf->buf, va, op);
968bf215546Sopenharmony_ci				op |= PREDICATION_CONTINUE;
969bf215546Sopenharmony_ci			}
970bf215546Sopenharmony_ci
971bf215546Sopenharmony_ci			results_base += query->result_size;
972bf215546Sopenharmony_ci		}
973bf215546Sopenharmony_ci	}
974bf215546Sopenharmony_ci}
975bf215546Sopenharmony_ci
976bf215546Sopenharmony_cistatic struct pipe_query *r600_create_query(struct pipe_context *ctx, unsigned query_type, unsigned index)
977bf215546Sopenharmony_ci{
978bf215546Sopenharmony_ci	struct r600_common_screen *rscreen =
979bf215546Sopenharmony_ci		(struct r600_common_screen *)ctx->screen;
980bf215546Sopenharmony_ci
981bf215546Sopenharmony_ci	if (query_type == PIPE_QUERY_TIMESTAMP_DISJOINT ||
982bf215546Sopenharmony_ci	    query_type == PIPE_QUERY_GPU_FINISHED ||
983bf215546Sopenharmony_ci	    query_type >= PIPE_QUERY_DRIVER_SPECIFIC)
984bf215546Sopenharmony_ci		return r600_query_sw_create(query_type);
985bf215546Sopenharmony_ci
986bf215546Sopenharmony_ci	return r600_query_hw_create(rscreen, query_type, index);
987bf215546Sopenharmony_ci}
988bf215546Sopenharmony_ci
989bf215546Sopenharmony_cistatic void r600_destroy_query(struct pipe_context *ctx, struct pipe_query *query)
990bf215546Sopenharmony_ci{
991bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
992bf215546Sopenharmony_ci	struct r600_query *rquery = (struct r600_query *)query;
993bf215546Sopenharmony_ci
994bf215546Sopenharmony_ci	rquery->ops->destroy(rctx->screen, rquery);
995bf215546Sopenharmony_ci}
996bf215546Sopenharmony_ci
997bf215546Sopenharmony_cistatic bool r600_begin_query(struct pipe_context *ctx,
998bf215546Sopenharmony_ci			     struct pipe_query *query)
999bf215546Sopenharmony_ci{
1000bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
1001bf215546Sopenharmony_ci	struct r600_query *rquery = (struct r600_query *)query;
1002bf215546Sopenharmony_ci
1003bf215546Sopenharmony_ci	return rquery->ops->begin(rctx, rquery);
1004bf215546Sopenharmony_ci}
1005bf215546Sopenharmony_ci
1006bf215546Sopenharmony_civoid r600_query_hw_reset_buffers(struct r600_common_context *rctx,
1007bf215546Sopenharmony_ci				 struct r600_query_hw *query)
1008bf215546Sopenharmony_ci{
1009bf215546Sopenharmony_ci	struct r600_query_buffer *prev = query->buffer.previous;
1010bf215546Sopenharmony_ci
1011bf215546Sopenharmony_ci	/* Discard the old query buffers. */
1012bf215546Sopenharmony_ci	while (prev) {
1013bf215546Sopenharmony_ci		struct r600_query_buffer *qbuf = prev;
1014bf215546Sopenharmony_ci		prev = prev->previous;
1015bf215546Sopenharmony_ci		r600_resource_reference(&qbuf->buf, NULL);
1016bf215546Sopenharmony_ci		FREE(qbuf);
1017bf215546Sopenharmony_ci	}
1018bf215546Sopenharmony_ci
1019bf215546Sopenharmony_ci	query->buffer.results_end = 0;
1020bf215546Sopenharmony_ci	query->buffer.previous = NULL;
1021bf215546Sopenharmony_ci
1022bf215546Sopenharmony_ci	/* Obtain a new buffer if the current one can't be mapped without a stall. */
1023bf215546Sopenharmony_ci	if (r600_rings_is_buffer_referenced(rctx, query->buffer.buf->buf, RADEON_USAGE_READWRITE) ||
1024bf215546Sopenharmony_ci	    !rctx->ws->buffer_wait(rctx->ws, query->buffer.buf->buf, 0, RADEON_USAGE_READWRITE)) {
1025bf215546Sopenharmony_ci		r600_resource_reference(&query->buffer.buf, NULL);
1026bf215546Sopenharmony_ci		query->buffer.buf = r600_new_query_buffer(rctx->screen, query);
1027bf215546Sopenharmony_ci	} else {
1028bf215546Sopenharmony_ci		if (!query->ops->prepare_buffer(rctx->screen, query, query->buffer.buf))
1029bf215546Sopenharmony_ci			r600_resource_reference(&query->buffer.buf, NULL);
1030bf215546Sopenharmony_ci	}
1031bf215546Sopenharmony_ci}
1032bf215546Sopenharmony_ci
1033bf215546Sopenharmony_cibool r600_query_hw_begin(struct r600_common_context *rctx,
1034bf215546Sopenharmony_ci			 struct r600_query *rquery)
1035bf215546Sopenharmony_ci{
1036bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)rquery;
1037bf215546Sopenharmony_ci
1038bf215546Sopenharmony_ci	if (query->flags & R600_QUERY_HW_FLAG_NO_START) {
1039bf215546Sopenharmony_ci		assert(0);
1040bf215546Sopenharmony_ci		return false;
1041bf215546Sopenharmony_ci	}
1042bf215546Sopenharmony_ci
1043bf215546Sopenharmony_ci	if (!(query->flags & R600_QUERY_HW_FLAG_BEGIN_RESUMES))
1044bf215546Sopenharmony_ci		r600_query_hw_reset_buffers(rctx, query);
1045bf215546Sopenharmony_ci
1046bf215546Sopenharmony_ci	r600_query_hw_emit_start(rctx, query);
1047bf215546Sopenharmony_ci	if (!query->buffer.buf)
1048bf215546Sopenharmony_ci		return false;
1049bf215546Sopenharmony_ci
1050bf215546Sopenharmony_ci	list_addtail(&query->list, &rctx->active_queries);
1051bf215546Sopenharmony_ci	return true;
1052bf215546Sopenharmony_ci}
1053bf215546Sopenharmony_ci
1054bf215546Sopenharmony_cistatic bool r600_end_query(struct pipe_context *ctx, struct pipe_query *query)
1055bf215546Sopenharmony_ci{
1056bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
1057bf215546Sopenharmony_ci	struct r600_query *rquery = (struct r600_query *)query;
1058bf215546Sopenharmony_ci
1059bf215546Sopenharmony_ci	return rquery->ops->end(rctx, rquery);
1060bf215546Sopenharmony_ci}
1061bf215546Sopenharmony_ci
1062bf215546Sopenharmony_cibool r600_query_hw_end(struct r600_common_context *rctx,
1063bf215546Sopenharmony_ci		       struct r600_query *rquery)
1064bf215546Sopenharmony_ci{
1065bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)rquery;
1066bf215546Sopenharmony_ci
1067bf215546Sopenharmony_ci	if (query->flags & R600_QUERY_HW_FLAG_NO_START)
1068bf215546Sopenharmony_ci		r600_query_hw_reset_buffers(rctx, query);
1069bf215546Sopenharmony_ci
1070bf215546Sopenharmony_ci	r600_query_hw_emit_stop(rctx, query);
1071bf215546Sopenharmony_ci
1072bf215546Sopenharmony_ci	if (!(query->flags & R600_QUERY_HW_FLAG_NO_START))
1073bf215546Sopenharmony_ci		list_delinit(&query->list);
1074bf215546Sopenharmony_ci
1075bf215546Sopenharmony_ci	if (!query->buffer.buf)
1076bf215546Sopenharmony_ci		return false;
1077bf215546Sopenharmony_ci
1078bf215546Sopenharmony_ci	return true;
1079bf215546Sopenharmony_ci}
1080bf215546Sopenharmony_ci
1081bf215546Sopenharmony_cistatic void r600_get_hw_query_params(struct r600_common_context *rctx,
1082bf215546Sopenharmony_ci				     struct r600_query_hw *rquery, int index,
1083bf215546Sopenharmony_ci				     struct r600_hw_query_params *params)
1084bf215546Sopenharmony_ci{
1085bf215546Sopenharmony_ci	unsigned max_rbs = rctx->screen->info.max_render_backends;
1086bf215546Sopenharmony_ci
1087bf215546Sopenharmony_ci	params->pair_stride = 0;
1088bf215546Sopenharmony_ci	params->pair_count = 1;
1089bf215546Sopenharmony_ci
1090bf215546Sopenharmony_ci	switch (rquery->b.type) {
1091bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER:
1092bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
1093bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
1094bf215546Sopenharmony_ci		params->start_offset = 0;
1095bf215546Sopenharmony_ci		params->end_offset = 8;
1096bf215546Sopenharmony_ci		params->fence_offset = max_rbs * 16;
1097bf215546Sopenharmony_ci		params->pair_stride = 16;
1098bf215546Sopenharmony_ci		params->pair_count = max_rbs;
1099bf215546Sopenharmony_ci		break;
1100bf215546Sopenharmony_ci	case PIPE_QUERY_TIME_ELAPSED:
1101bf215546Sopenharmony_ci		params->start_offset = 0;
1102bf215546Sopenharmony_ci		params->end_offset = 8;
1103bf215546Sopenharmony_ci		params->fence_offset = 16;
1104bf215546Sopenharmony_ci		break;
1105bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP:
1106bf215546Sopenharmony_ci		params->start_offset = 0;
1107bf215546Sopenharmony_ci		params->end_offset = 0;
1108bf215546Sopenharmony_ci		params->fence_offset = 8;
1109bf215546Sopenharmony_ci		break;
1110bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_EMITTED:
1111bf215546Sopenharmony_ci		params->start_offset = 8;
1112bf215546Sopenharmony_ci		params->end_offset = 24;
1113bf215546Sopenharmony_ci		params->fence_offset = params->end_offset + 4;
1114bf215546Sopenharmony_ci		break;
1115bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_GENERATED:
1116bf215546Sopenharmony_ci		params->start_offset = 0;
1117bf215546Sopenharmony_ci		params->end_offset = 16;
1118bf215546Sopenharmony_ci		params->fence_offset = params->end_offset + 4;
1119bf215546Sopenharmony_ci		break;
1120bf215546Sopenharmony_ci	case PIPE_QUERY_SO_STATISTICS:
1121bf215546Sopenharmony_ci		params->start_offset = 8 - index * 8;
1122bf215546Sopenharmony_ci		params->end_offset = 24 - index * 8;
1123bf215546Sopenharmony_ci		params->fence_offset = params->end_offset + 4;
1124bf215546Sopenharmony_ci		break;
1125bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
1126bf215546Sopenharmony_ci		params->pair_count = R600_MAX_STREAMS;
1127bf215546Sopenharmony_ci		params->pair_stride = 32;
1128bf215546Sopenharmony_ci		FALLTHROUGH;
1129bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
1130bf215546Sopenharmony_ci		params->start_offset = 0;
1131bf215546Sopenharmony_ci		params->end_offset = 16;
1132bf215546Sopenharmony_ci
1133bf215546Sopenharmony_ci		/* We can re-use the high dword of the last 64-bit value as a
1134bf215546Sopenharmony_ci		 * fence: it is initialized as 0, and the high bit is set by
1135bf215546Sopenharmony_ci		 * the write of the streamout stats event.
1136bf215546Sopenharmony_ci		 */
1137bf215546Sopenharmony_ci		params->fence_offset = rquery->result_size - 4;
1138bf215546Sopenharmony_ci		break;
1139bf215546Sopenharmony_ci	case PIPE_QUERY_PIPELINE_STATISTICS:
1140bf215546Sopenharmony_ci	{
1141bf215546Sopenharmony_ci		/* Offsets apply to EG+ */
1142bf215546Sopenharmony_ci		static const unsigned offsets[] = {56, 48, 24, 32, 40, 16, 8, 0, 64, 72, 80};
1143bf215546Sopenharmony_ci		params->start_offset = offsets[index];
1144bf215546Sopenharmony_ci		params->end_offset = 88 + offsets[index];
1145bf215546Sopenharmony_ci		params->fence_offset = 2 * 88;
1146bf215546Sopenharmony_ci		break;
1147bf215546Sopenharmony_ci	}
1148bf215546Sopenharmony_ci	default:
1149bf215546Sopenharmony_ci		unreachable("r600_get_hw_query_params unsupported");
1150bf215546Sopenharmony_ci	}
1151bf215546Sopenharmony_ci}
1152bf215546Sopenharmony_ci
1153bf215546Sopenharmony_cistatic unsigned r600_query_read_result(void *map, unsigned start_index, unsigned end_index,
1154bf215546Sopenharmony_ci				       bool test_status_bit)
1155bf215546Sopenharmony_ci{
1156bf215546Sopenharmony_ci	uint32_t *current_result = (uint32_t*)map;
1157bf215546Sopenharmony_ci	uint64_t start, end;
1158bf215546Sopenharmony_ci
1159bf215546Sopenharmony_ci	start = (uint64_t)current_result[start_index] |
1160bf215546Sopenharmony_ci		(uint64_t)current_result[start_index+1] << 32;
1161bf215546Sopenharmony_ci	end = (uint64_t)current_result[end_index] |
1162bf215546Sopenharmony_ci	      (uint64_t)current_result[end_index+1] << 32;
1163bf215546Sopenharmony_ci
1164bf215546Sopenharmony_ci	if (!test_status_bit ||
1165bf215546Sopenharmony_ci	    ((start & 0x8000000000000000UL) && (end & 0x8000000000000000UL))) {
1166bf215546Sopenharmony_ci		return end - start;
1167bf215546Sopenharmony_ci	}
1168bf215546Sopenharmony_ci	return 0;
1169bf215546Sopenharmony_ci}
1170bf215546Sopenharmony_ci
1171bf215546Sopenharmony_cistatic void r600_query_hw_add_result(struct r600_common_screen *rscreen,
1172bf215546Sopenharmony_ci				     struct r600_query_hw *query,
1173bf215546Sopenharmony_ci				     void *buffer,
1174bf215546Sopenharmony_ci				     union pipe_query_result *result)
1175bf215546Sopenharmony_ci{
1176bf215546Sopenharmony_ci	unsigned max_rbs = rscreen->info.max_render_backends;
1177bf215546Sopenharmony_ci
1178bf215546Sopenharmony_ci	switch (query->b.type) {
1179bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_COUNTER: {
1180bf215546Sopenharmony_ci		for (unsigned i = 0; i < max_rbs; ++i) {
1181bf215546Sopenharmony_ci			unsigned results_base = i * 16;
1182bf215546Sopenharmony_ci			result->u64 +=
1183bf215546Sopenharmony_ci				r600_query_read_result(buffer + results_base, 0, 2, true);
1184bf215546Sopenharmony_ci		}
1185bf215546Sopenharmony_ci		break;
1186bf215546Sopenharmony_ci	}
1187bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE:
1188bf215546Sopenharmony_ci	case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE: {
1189bf215546Sopenharmony_ci		for (unsigned i = 0; i < max_rbs; ++i) {
1190bf215546Sopenharmony_ci			unsigned results_base = i * 16;
1191bf215546Sopenharmony_ci			result->b = result->b ||
1192bf215546Sopenharmony_ci				r600_query_read_result(buffer + results_base, 0, 2, true) != 0;
1193bf215546Sopenharmony_ci		}
1194bf215546Sopenharmony_ci		break;
1195bf215546Sopenharmony_ci	}
1196bf215546Sopenharmony_ci	case PIPE_QUERY_TIME_ELAPSED:
1197bf215546Sopenharmony_ci		result->u64 += r600_query_read_result(buffer, 0, 2, false);
1198bf215546Sopenharmony_ci		break;
1199bf215546Sopenharmony_ci	case PIPE_QUERY_TIMESTAMP:
1200bf215546Sopenharmony_ci		result->u64 = *(uint64_t*)buffer;
1201bf215546Sopenharmony_ci		break;
1202bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_EMITTED:
1203bf215546Sopenharmony_ci		/* SAMPLE_STREAMOUTSTATS stores this structure:
1204bf215546Sopenharmony_ci		 * {
1205bf215546Sopenharmony_ci		 *    u64 NumPrimitivesWritten;
1206bf215546Sopenharmony_ci		 *    u64 PrimitiveStorageNeeded;
1207bf215546Sopenharmony_ci		 * }
1208bf215546Sopenharmony_ci		 * We only need NumPrimitivesWritten here. */
1209bf215546Sopenharmony_ci		result->u64 += r600_query_read_result(buffer, 2, 6, true);
1210bf215546Sopenharmony_ci		break;
1211bf215546Sopenharmony_ci	case PIPE_QUERY_PRIMITIVES_GENERATED:
1212bf215546Sopenharmony_ci		/* Here we read PrimitiveStorageNeeded. */
1213bf215546Sopenharmony_ci		result->u64 += r600_query_read_result(buffer, 0, 4, true);
1214bf215546Sopenharmony_ci		break;
1215bf215546Sopenharmony_ci	case PIPE_QUERY_SO_STATISTICS:
1216bf215546Sopenharmony_ci		result->so_statistics.num_primitives_written +=
1217bf215546Sopenharmony_ci			r600_query_read_result(buffer, 2, 6, true);
1218bf215546Sopenharmony_ci		result->so_statistics.primitives_storage_needed +=
1219bf215546Sopenharmony_ci			r600_query_read_result(buffer, 0, 4, true);
1220bf215546Sopenharmony_ci		break;
1221bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
1222bf215546Sopenharmony_ci		result->b = result->b ||
1223bf215546Sopenharmony_ci			r600_query_read_result(buffer, 2, 6, true) !=
1224bf215546Sopenharmony_ci			r600_query_read_result(buffer, 0, 4, true);
1225bf215546Sopenharmony_ci		break;
1226bf215546Sopenharmony_ci	case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
1227bf215546Sopenharmony_ci		for (unsigned stream = 0; stream < R600_MAX_STREAMS; ++stream) {
1228bf215546Sopenharmony_ci			result->b = result->b ||
1229bf215546Sopenharmony_ci				r600_query_read_result(buffer, 2, 6, true) !=
1230bf215546Sopenharmony_ci				r600_query_read_result(buffer, 0, 4, true);
1231bf215546Sopenharmony_ci			buffer = (char *)buffer + 32;
1232bf215546Sopenharmony_ci		}
1233bf215546Sopenharmony_ci		break;
1234bf215546Sopenharmony_ci	case PIPE_QUERY_PIPELINE_STATISTICS:
1235bf215546Sopenharmony_ci		if (rscreen->gfx_level >= EVERGREEN) {
1236bf215546Sopenharmony_ci			result->pipeline_statistics.ps_invocations +=
1237bf215546Sopenharmony_ci				r600_query_read_result(buffer, 0, 22, false);
1238bf215546Sopenharmony_ci			result->pipeline_statistics.c_primitives +=
1239bf215546Sopenharmony_ci				r600_query_read_result(buffer, 2, 24, false);
1240bf215546Sopenharmony_ci			result->pipeline_statistics.c_invocations +=
1241bf215546Sopenharmony_ci				r600_query_read_result(buffer, 4, 26, false);
1242bf215546Sopenharmony_ci			result->pipeline_statistics.vs_invocations +=
1243bf215546Sopenharmony_ci				r600_query_read_result(buffer, 6, 28, false);
1244bf215546Sopenharmony_ci			result->pipeline_statistics.gs_invocations +=
1245bf215546Sopenharmony_ci				r600_query_read_result(buffer, 8, 30, false);
1246bf215546Sopenharmony_ci			result->pipeline_statistics.gs_primitives +=
1247bf215546Sopenharmony_ci				r600_query_read_result(buffer, 10, 32, false);
1248bf215546Sopenharmony_ci			result->pipeline_statistics.ia_primitives +=
1249bf215546Sopenharmony_ci				r600_query_read_result(buffer, 12, 34, false);
1250bf215546Sopenharmony_ci			result->pipeline_statistics.ia_vertices +=
1251bf215546Sopenharmony_ci				r600_query_read_result(buffer, 14, 36, false);
1252bf215546Sopenharmony_ci			result->pipeline_statistics.hs_invocations +=
1253bf215546Sopenharmony_ci				r600_query_read_result(buffer, 16, 38, false);
1254bf215546Sopenharmony_ci			result->pipeline_statistics.ds_invocations +=
1255bf215546Sopenharmony_ci				r600_query_read_result(buffer, 18, 40, false);
1256bf215546Sopenharmony_ci			result->pipeline_statistics.cs_invocations +=
1257bf215546Sopenharmony_ci				r600_query_read_result(buffer, 20, 42, false);
1258bf215546Sopenharmony_ci		} else {
1259bf215546Sopenharmony_ci			result->pipeline_statistics.ps_invocations +=
1260bf215546Sopenharmony_ci				r600_query_read_result(buffer, 0, 16, false);
1261bf215546Sopenharmony_ci			result->pipeline_statistics.c_primitives +=
1262bf215546Sopenharmony_ci				r600_query_read_result(buffer, 2, 18, false);
1263bf215546Sopenharmony_ci			result->pipeline_statistics.c_invocations +=
1264bf215546Sopenharmony_ci				r600_query_read_result(buffer, 4, 20, false);
1265bf215546Sopenharmony_ci			result->pipeline_statistics.vs_invocations +=
1266bf215546Sopenharmony_ci				r600_query_read_result(buffer, 6, 22, false);
1267bf215546Sopenharmony_ci			result->pipeline_statistics.gs_invocations +=
1268bf215546Sopenharmony_ci				r600_query_read_result(buffer, 8, 24, false);
1269bf215546Sopenharmony_ci			result->pipeline_statistics.gs_primitives +=
1270bf215546Sopenharmony_ci				r600_query_read_result(buffer, 10, 26, false);
1271bf215546Sopenharmony_ci			result->pipeline_statistics.ia_primitives +=
1272bf215546Sopenharmony_ci				r600_query_read_result(buffer, 12, 28, false);
1273bf215546Sopenharmony_ci			result->pipeline_statistics.ia_vertices +=
1274bf215546Sopenharmony_ci				r600_query_read_result(buffer, 14, 30, false);
1275bf215546Sopenharmony_ci		}
1276bf215546Sopenharmony_ci#if 0 /* for testing */
1277bf215546Sopenharmony_ci		printf("Pipeline stats: IA verts=%llu, IA prims=%llu, VS=%llu, HS=%llu, "
1278bf215546Sopenharmony_ci		       "DS=%llu, GS=%llu, GS prims=%llu, Clipper=%llu, "
1279bf215546Sopenharmony_ci		       "Clipper prims=%llu, PS=%llu, CS=%llu\n",
1280bf215546Sopenharmony_ci		       result->pipeline_statistics.ia_vertices,
1281bf215546Sopenharmony_ci		       result->pipeline_statistics.ia_primitives,
1282bf215546Sopenharmony_ci		       result->pipeline_statistics.vs_invocations,
1283bf215546Sopenharmony_ci		       result->pipeline_statistics.hs_invocations,
1284bf215546Sopenharmony_ci		       result->pipeline_statistics.ds_invocations,
1285bf215546Sopenharmony_ci		       result->pipeline_statistics.gs_invocations,
1286bf215546Sopenharmony_ci		       result->pipeline_statistics.gs_primitives,
1287bf215546Sopenharmony_ci		       result->pipeline_statistics.c_invocations,
1288bf215546Sopenharmony_ci		       result->pipeline_statistics.c_primitives,
1289bf215546Sopenharmony_ci		       result->pipeline_statistics.ps_invocations,
1290bf215546Sopenharmony_ci		       result->pipeline_statistics.cs_invocations);
1291bf215546Sopenharmony_ci#endif
1292bf215546Sopenharmony_ci		break;
1293bf215546Sopenharmony_ci	default:
1294bf215546Sopenharmony_ci		assert(0);
1295bf215546Sopenharmony_ci	}
1296bf215546Sopenharmony_ci}
1297bf215546Sopenharmony_ci
1298bf215546Sopenharmony_cistatic bool r600_get_query_result(struct pipe_context *ctx,
1299bf215546Sopenharmony_ci				  struct pipe_query *query, bool wait,
1300bf215546Sopenharmony_ci				  union pipe_query_result *result)
1301bf215546Sopenharmony_ci{
1302bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
1303bf215546Sopenharmony_ci	struct r600_query *rquery = (struct r600_query *)query;
1304bf215546Sopenharmony_ci
1305bf215546Sopenharmony_ci	return rquery->ops->get_result(rctx, rquery, wait, result);
1306bf215546Sopenharmony_ci}
1307bf215546Sopenharmony_ci
1308bf215546Sopenharmony_cistatic void r600_get_query_result_resource(struct pipe_context *ctx,
1309bf215546Sopenharmony_ci                                           struct pipe_query *query,
1310bf215546Sopenharmony_ci                                           enum pipe_query_flags flags,
1311bf215546Sopenharmony_ci                                           enum pipe_query_value_type result_type,
1312bf215546Sopenharmony_ci                                           int index,
1313bf215546Sopenharmony_ci                                           struct pipe_resource *resource,
1314bf215546Sopenharmony_ci                                           unsigned offset)
1315bf215546Sopenharmony_ci{
1316bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
1317bf215546Sopenharmony_ci	struct r600_query *rquery = (struct r600_query *)query;
1318bf215546Sopenharmony_ci
1319bf215546Sopenharmony_ci	rquery->ops->get_result_resource(rctx, rquery, flags, result_type, index,
1320bf215546Sopenharmony_ci	                                 resource, offset);
1321bf215546Sopenharmony_ci}
1322bf215546Sopenharmony_ci
1323bf215546Sopenharmony_cistatic void r600_query_hw_clear_result(struct r600_query_hw *query,
1324bf215546Sopenharmony_ci				       union pipe_query_result *result)
1325bf215546Sopenharmony_ci{
1326bf215546Sopenharmony_ci	util_query_clear_result(result, query->b.type);
1327bf215546Sopenharmony_ci}
1328bf215546Sopenharmony_ci
1329bf215546Sopenharmony_cibool r600_query_hw_get_result(struct r600_common_context *rctx,
1330bf215546Sopenharmony_ci			      struct r600_query *rquery,
1331bf215546Sopenharmony_ci			      bool wait, union pipe_query_result *result)
1332bf215546Sopenharmony_ci{
1333bf215546Sopenharmony_ci	struct r600_common_screen *rscreen = rctx->screen;
1334bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)rquery;
1335bf215546Sopenharmony_ci	struct r600_query_buffer *qbuf;
1336bf215546Sopenharmony_ci
1337bf215546Sopenharmony_ci	query->ops->clear_result(query, result);
1338bf215546Sopenharmony_ci
1339bf215546Sopenharmony_ci	for (qbuf = &query->buffer; qbuf; qbuf = qbuf->previous) {
1340bf215546Sopenharmony_ci		unsigned usage = PIPE_MAP_READ |
1341bf215546Sopenharmony_ci				 (wait ? 0 : PIPE_MAP_DONTBLOCK);
1342bf215546Sopenharmony_ci		unsigned results_base = 0;
1343bf215546Sopenharmony_ci		void *map;
1344bf215546Sopenharmony_ci
1345bf215546Sopenharmony_ci		if (rquery->b.flushed)
1346bf215546Sopenharmony_ci			map = rctx->ws->buffer_map(rctx->ws, qbuf->buf->buf, NULL, usage);
1347bf215546Sopenharmony_ci		else
1348bf215546Sopenharmony_ci			map = r600_buffer_map_sync_with_rings(rctx, qbuf->buf, usage);
1349bf215546Sopenharmony_ci
1350bf215546Sopenharmony_ci		if (!map)
1351bf215546Sopenharmony_ci			return false;
1352bf215546Sopenharmony_ci
1353bf215546Sopenharmony_ci		while (results_base != qbuf->results_end) {
1354bf215546Sopenharmony_ci			query->ops->add_result(rscreen, query, map + results_base,
1355bf215546Sopenharmony_ci					       result);
1356bf215546Sopenharmony_ci			results_base += query->result_size;
1357bf215546Sopenharmony_ci		}
1358bf215546Sopenharmony_ci	}
1359bf215546Sopenharmony_ci
1360bf215546Sopenharmony_ci	/* Convert the time to expected units. */
1361bf215546Sopenharmony_ci	if (rquery->type == PIPE_QUERY_TIME_ELAPSED ||
1362bf215546Sopenharmony_ci	    rquery->type == PIPE_QUERY_TIMESTAMP) {
1363bf215546Sopenharmony_ci		result->u64 = (1000000 * result->u64) / rscreen->info.clock_crystal_freq;
1364bf215546Sopenharmony_ci	}
1365bf215546Sopenharmony_ci	return true;
1366bf215546Sopenharmony_ci}
1367bf215546Sopenharmony_ci
1368bf215546Sopenharmony_ci/* Create the compute shader that is used to collect the results.
1369bf215546Sopenharmony_ci *
1370bf215546Sopenharmony_ci * One compute grid with a single thread is launched for every query result
1371bf215546Sopenharmony_ci * buffer. The thread (optionally) reads a previous summary buffer, then
1372bf215546Sopenharmony_ci * accumulates data from the query result buffer, and writes the result either
1373bf215546Sopenharmony_ci * to a summary buffer to be consumed by the next grid invocation or to the
1374bf215546Sopenharmony_ci * user-supplied buffer.
1375bf215546Sopenharmony_ci *
1376bf215546Sopenharmony_ci * Data layout:
1377bf215546Sopenharmony_ci *
1378bf215546Sopenharmony_ci * CONST
1379bf215546Sopenharmony_ci *  0.x = end_offset
1380bf215546Sopenharmony_ci *  0.y = result_stride
1381bf215546Sopenharmony_ci *  0.z = result_count
1382bf215546Sopenharmony_ci *  0.w = bit field:
1383bf215546Sopenharmony_ci *          1: read previously accumulated values
1384bf215546Sopenharmony_ci *          2: write accumulated values for chaining
1385bf215546Sopenharmony_ci *          4: write result available
1386bf215546Sopenharmony_ci *          8: convert result to boolean (0/1)
1387bf215546Sopenharmony_ci *         16: only read one dword and use that as result
1388bf215546Sopenharmony_ci *         32: apply timestamp conversion
1389bf215546Sopenharmony_ci *         64: store full 64 bits result
1390bf215546Sopenharmony_ci *        128: store signed 32 bits result
1391bf215546Sopenharmony_ci *        256: SO_OVERFLOW mode: take the difference of two successive half-pairs
1392bf215546Sopenharmony_ci *  1.x = fence_offset
1393bf215546Sopenharmony_ci *  1.y = pair_stride
1394bf215546Sopenharmony_ci *  1.z = pair_count
1395bf215546Sopenharmony_ci *  1.w = result_offset
1396bf215546Sopenharmony_ci *  2.x = buffer0 offset
1397bf215546Sopenharmony_ci *
1398bf215546Sopenharmony_ci * BUFFER[0] = query result buffer
1399bf215546Sopenharmony_ci * BUFFER[1] = previous summary buffer
1400bf215546Sopenharmony_ci * BUFFER[2] = next summary buffer or user-supplied buffer
1401bf215546Sopenharmony_ci */
1402bf215546Sopenharmony_cistatic void r600_create_query_result_shader(struct r600_common_context *rctx)
1403bf215546Sopenharmony_ci{
1404bf215546Sopenharmony_ci	/* TEMP[0].xy = accumulated result so far
1405bf215546Sopenharmony_ci	 * TEMP[0].z = result not available
1406bf215546Sopenharmony_ci	 *
1407bf215546Sopenharmony_ci	 * TEMP[1].x = current result index
1408bf215546Sopenharmony_ci	 * TEMP[1].y = current pair index
1409bf215546Sopenharmony_ci	 */
1410bf215546Sopenharmony_ci	static const char text_tmpl[] =
1411bf215546Sopenharmony_ci		"COMP\n"
1412bf215546Sopenharmony_ci		"PROPERTY CS_FIXED_BLOCK_WIDTH 1\n"
1413bf215546Sopenharmony_ci		"PROPERTY CS_FIXED_BLOCK_HEIGHT 1\n"
1414bf215546Sopenharmony_ci		"PROPERTY CS_FIXED_BLOCK_DEPTH 1\n"
1415bf215546Sopenharmony_ci		"DCL BUFFER[0]\n"
1416bf215546Sopenharmony_ci		"DCL BUFFER[1]\n"
1417bf215546Sopenharmony_ci		"DCL BUFFER[2]\n"
1418bf215546Sopenharmony_ci		"DCL CONST[0][0..2]\n"
1419bf215546Sopenharmony_ci		"DCL TEMP[0..5]\n"
1420bf215546Sopenharmony_ci		"IMM[0] UINT32 {0, 31, 2147483647, 4294967295}\n"
1421bf215546Sopenharmony_ci		"IMM[1] UINT32 {1, 2, 4, 8}\n"
1422bf215546Sopenharmony_ci		"IMM[2] UINT32 {16, 32, 64, 128}\n"
1423bf215546Sopenharmony_ci		"IMM[3] UINT32 {1000000, 0, %u, 0}\n" /* for timestamp conversion */
1424bf215546Sopenharmony_ci		"IMM[4] UINT32 {256, 0, 0, 0}\n"
1425bf215546Sopenharmony_ci
1426bf215546Sopenharmony_ci		"AND TEMP[5], CONST[0][0].wwww, IMM[2].xxxx\n"
1427bf215546Sopenharmony_ci		"UIF TEMP[5]\n"
1428bf215546Sopenharmony_ci			/* Check result availability. */
1429bf215546Sopenharmony_ci			"UADD TEMP[1].x, CONST[0][1].xxxx, CONST[0][2].xxxx\n"
1430bf215546Sopenharmony_ci			"LOAD TEMP[1].x, BUFFER[0], TEMP[1].xxxx\n"
1431bf215546Sopenharmony_ci			"ISHR TEMP[0].z, TEMP[1].xxxx, IMM[0].yyyy\n"
1432bf215546Sopenharmony_ci			"MOV TEMP[1], TEMP[0].zzzz\n"
1433bf215546Sopenharmony_ci			"NOT TEMP[0].z, TEMP[0].zzzz\n"
1434bf215546Sopenharmony_ci
1435bf215546Sopenharmony_ci			/* Load result if available. */
1436bf215546Sopenharmony_ci			"UIF TEMP[1]\n"
1437bf215546Sopenharmony_ci				"UADD TEMP[0].x, IMM[0].xxxx, CONST[0][2].xxxx\n"
1438bf215546Sopenharmony_ci				"LOAD TEMP[0].xy, BUFFER[0], TEMP[0].xxxx\n"
1439bf215546Sopenharmony_ci			"ENDIF\n"
1440bf215546Sopenharmony_ci		"ELSE\n"
1441bf215546Sopenharmony_ci			/* Load previously accumulated result if requested. */
1442bf215546Sopenharmony_ci			"MOV TEMP[0], IMM[0].xxxx\n"
1443bf215546Sopenharmony_ci			"AND TEMP[4], CONST[0][0].wwww, IMM[1].xxxx\n"
1444bf215546Sopenharmony_ci			"UIF TEMP[4]\n"
1445bf215546Sopenharmony_ci				"LOAD TEMP[0].xyz, BUFFER[1], IMM[0].xxxx\n"
1446bf215546Sopenharmony_ci			"ENDIF\n"
1447bf215546Sopenharmony_ci
1448bf215546Sopenharmony_ci			"MOV TEMP[1].x, IMM[0].xxxx\n"
1449bf215546Sopenharmony_ci			"BGNLOOP\n"
1450bf215546Sopenharmony_ci				/* Break if accumulated result so far is not available. */
1451bf215546Sopenharmony_ci				"UIF TEMP[0].zzzz\n"
1452bf215546Sopenharmony_ci					"BRK\n"
1453bf215546Sopenharmony_ci				"ENDIF\n"
1454bf215546Sopenharmony_ci
1455bf215546Sopenharmony_ci				/* Break if result_index >= result_count. */
1456bf215546Sopenharmony_ci				"USGE TEMP[5], TEMP[1].xxxx, CONST[0][0].zzzz\n"
1457bf215546Sopenharmony_ci				"UIF TEMP[5]\n"
1458bf215546Sopenharmony_ci					"BRK\n"
1459bf215546Sopenharmony_ci				"ENDIF\n"
1460bf215546Sopenharmony_ci
1461bf215546Sopenharmony_ci				/* Load fence and check result availability */
1462bf215546Sopenharmony_ci				"UMAD TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy, CONST[0][1].xxxx\n"
1463bf215546Sopenharmony_ci				"UADD TEMP[5].x, TEMP[5].xxxx, CONST[0][2].xxxx\n"
1464bf215546Sopenharmony_ci				"LOAD TEMP[5].x, BUFFER[0], TEMP[5].xxxx\n"
1465bf215546Sopenharmony_ci				"ISHR TEMP[0].z, TEMP[5].xxxx, IMM[0].yyyy\n"
1466bf215546Sopenharmony_ci				"NOT TEMP[0].z, TEMP[0].zzzz\n"
1467bf215546Sopenharmony_ci				"UIF TEMP[0].zzzz\n"
1468bf215546Sopenharmony_ci					"BRK\n"
1469bf215546Sopenharmony_ci				"ENDIF\n"
1470bf215546Sopenharmony_ci
1471bf215546Sopenharmony_ci				"MOV TEMP[1].y, IMM[0].xxxx\n"
1472bf215546Sopenharmony_ci				"BGNLOOP\n"
1473bf215546Sopenharmony_ci					/* Load start and end. */
1474bf215546Sopenharmony_ci					"UMUL TEMP[5].x, TEMP[1].xxxx, CONST[0][0].yyyy\n"
1475bf215546Sopenharmony_ci					"UMAD TEMP[5].x, TEMP[1].yyyy, CONST[0][1].yyyy, TEMP[5].xxxx\n"
1476bf215546Sopenharmony_ci					"UADD TEMP[5].x, TEMP[5].xxxx, CONST[0][2].xxxx\n"
1477bf215546Sopenharmony_ci					"LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
1478bf215546Sopenharmony_ci
1479bf215546Sopenharmony_ci					"UADD TEMP[5].y, TEMP[5].xxxx, CONST[0][0].xxxx\n"
1480bf215546Sopenharmony_ci					"LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
1481bf215546Sopenharmony_ci
1482bf215546Sopenharmony_ci					"U64ADD TEMP[4].xy, TEMP[3], -TEMP[2]\n"
1483bf215546Sopenharmony_ci
1484bf215546Sopenharmony_ci					"AND TEMP[5].z, CONST[0][0].wwww, IMM[4].xxxx\n"
1485bf215546Sopenharmony_ci					"UIF TEMP[5].zzzz\n"
1486bf215546Sopenharmony_ci						/* Load second start/end half-pair and
1487bf215546Sopenharmony_ci						 * take the difference
1488bf215546Sopenharmony_ci						 */
1489bf215546Sopenharmony_ci						"UADD TEMP[5].xy, TEMP[5], IMM[1].wwww\n"
1490bf215546Sopenharmony_ci						"LOAD TEMP[2].xy, BUFFER[0], TEMP[5].xxxx\n"
1491bf215546Sopenharmony_ci						"LOAD TEMP[3].xy, BUFFER[0], TEMP[5].yyyy\n"
1492bf215546Sopenharmony_ci
1493bf215546Sopenharmony_ci						"U64ADD TEMP[3].xy, TEMP[3], -TEMP[2]\n"
1494bf215546Sopenharmony_ci						"U64ADD TEMP[4].xy, TEMP[4], -TEMP[3]\n"
1495bf215546Sopenharmony_ci					"ENDIF\n"
1496bf215546Sopenharmony_ci
1497bf215546Sopenharmony_ci					"U64ADD TEMP[0].xy, TEMP[0], TEMP[4]\n"
1498bf215546Sopenharmony_ci
1499bf215546Sopenharmony_ci					/* Increment pair index */
1500bf215546Sopenharmony_ci					"UADD TEMP[1].y, TEMP[1].yyyy, IMM[1].xxxx\n"
1501bf215546Sopenharmony_ci					"USGE TEMP[5], TEMP[1].yyyy, CONST[0][1].zzzz\n"
1502bf215546Sopenharmony_ci					"UIF TEMP[5]\n"
1503bf215546Sopenharmony_ci						"BRK\n"
1504bf215546Sopenharmony_ci					"ENDIF\n"
1505bf215546Sopenharmony_ci				"ENDLOOP\n"
1506bf215546Sopenharmony_ci
1507bf215546Sopenharmony_ci				/* Increment result index */
1508bf215546Sopenharmony_ci				"UADD TEMP[1].x, TEMP[1].xxxx, IMM[1].xxxx\n"
1509bf215546Sopenharmony_ci			"ENDLOOP\n"
1510bf215546Sopenharmony_ci		"ENDIF\n"
1511bf215546Sopenharmony_ci
1512bf215546Sopenharmony_ci		"AND TEMP[4], CONST[0][0].wwww, IMM[1].yyyy\n"
1513bf215546Sopenharmony_ci		"UIF TEMP[4]\n"
1514bf215546Sopenharmony_ci			/* Store accumulated data for chaining. */
1515bf215546Sopenharmony_ci			"STORE BUFFER[2].xyz, CONST[0][1].wwww, TEMP[0]\n"
1516bf215546Sopenharmony_ci		"ELSE\n"
1517bf215546Sopenharmony_ci			"AND TEMP[4], CONST[0][0].wwww, IMM[1].zzzz\n"
1518bf215546Sopenharmony_ci			"UIF TEMP[4]\n"
1519bf215546Sopenharmony_ci				/* Store result availability. */
1520bf215546Sopenharmony_ci				"NOT TEMP[0].z, TEMP[0]\n"
1521bf215546Sopenharmony_ci				"AND TEMP[0].z, TEMP[0].zzzz, IMM[1].xxxx\n"
1522bf215546Sopenharmony_ci				"STORE BUFFER[2].x, CONST[0][1].wwww, TEMP[0].zzzz\n"
1523bf215546Sopenharmony_ci
1524bf215546Sopenharmony_ci				"AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
1525bf215546Sopenharmony_ci				"UIF TEMP[4]\n"
1526bf215546Sopenharmony_ci					"STORE BUFFER[2].y, CONST[0][1].wwww, IMM[0].xxxx\n"
1527bf215546Sopenharmony_ci				"ENDIF\n"
1528bf215546Sopenharmony_ci			"ELSE\n"
1529bf215546Sopenharmony_ci				/* Store result if it is available. */
1530bf215546Sopenharmony_ci				"NOT TEMP[4], TEMP[0].zzzz\n"
1531bf215546Sopenharmony_ci				"UIF TEMP[4]\n"
1532bf215546Sopenharmony_ci					/* Apply timestamp conversion */
1533bf215546Sopenharmony_ci					"AND TEMP[4], CONST[0][0].wwww, IMM[2].yyyy\n"
1534bf215546Sopenharmony_ci					"UIF TEMP[4]\n"
1535bf215546Sopenharmony_ci						"U64MUL TEMP[0].xy, TEMP[0], IMM[3].xyxy\n"
1536bf215546Sopenharmony_ci						"U64DIV TEMP[0].xy, TEMP[0], IMM[3].zwzw\n"
1537bf215546Sopenharmony_ci					"ENDIF\n"
1538bf215546Sopenharmony_ci
1539bf215546Sopenharmony_ci					/* Convert to boolean */
1540bf215546Sopenharmony_ci					"AND TEMP[4], CONST[0][0].wwww, IMM[1].wwww\n"
1541bf215546Sopenharmony_ci					"UIF TEMP[4]\n"
1542bf215546Sopenharmony_ci						"U64SNE TEMP[0].x, TEMP[0].xyxy, IMM[4].zwzw\n"
1543bf215546Sopenharmony_ci						"AND TEMP[0].x, TEMP[0].xxxx, IMM[1].xxxx\n"
1544bf215546Sopenharmony_ci						"MOV TEMP[0].y, IMM[0].xxxx\n"
1545bf215546Sopenharmony_ci					"ENDIF\n"
1546bf215546Sopenharmony_ci
1547bf215546Sopenharmony_ci					"AND TEMP[4], CONST[0][0].wwww, IMM[2].zzzz\n"
1548bf215546Sopenharmony_ci					"UIF TEMP[4]\n"
1549bf215546Sopenharmony_ci						"STORE BUFFER[2].xy, CONST[0][1].wwww, TEMP[0].xyxy\n"
1550bf215546Sopenharmony_ci					"ELSE\n"
1551bf215546Sopenharmony_ci						/* Clamping */
1552bf215546Sopenharmony_ci						"UIF TEMP[0].yyyy\n"
1553bf215546Sopenharmony_ci							"MOV TEMP[0].x, IMM[0].wwww\n"
1554bf215546Sopenharmony_ci						"ENDIF\n"
1555bf215546Sopenharmony_ci
1556bf215546Sopenharmony_ci						"AND TEMP[4], CONST[0][0].wwww, IMM[2].wwww\n"
1557bf215546Sopenharmony_ci						"UIF TEMP[4]\n"
1558bf215546Sopenharmony_ci							"UMIN TEMP[0].x, TEMP[0].xxxx, IMM[0].zzzz\n"
1559bf215546Sopenharmony_ci						"ENDIF\n"
1560bf215546Sopenharmony_ci
1561bf215546Sopenharmony_ci						"STORE BUFFER[2].x, CONST[0][1].wwww, TEMP[0].xxxx\n"
1562bf215546Sopenharmony_ci					"ENDIF\n"
1563bf215546Sopenharmony_ci				"ENDIF\n"
1564bf215546Sopenharmony_ci			"ENDIF\n"
1565bf215546Sopenharmony_ci		"ENDIF\n"
1566bf215546Sopenharmony_ci
1567bf215546Sopenharmony_ci		"END\n";
1568bf215546Sopenharmony_ci
1569bf215546Sopenharmony_ci	char text[sizeof(text_tmpl) + 32];
1570bf215546Sopenharmony_ci	struct tgsi_token tokens[1024];
1571bf215546Sopenharmony_ci	struct pipe_compute_state state = {};
1572bf215546Sopenharmony_ci
1573bf215546Sopenharmony_ci	/* Hard code the frequency into the shader so that the backend can
1574bf215546Sopenharmony_ci	 * use the full range of optimizations for divide-by-constant.
1575bf215546Sopenharmony_ci	 */
1576bf215546Sopenharmony_ci	snprintf(text, sizeof(text), text_tmpl,
1577bf215546Sopenharmony_ci		 rctx->screen->info.clock_crystal_freq);
1578bf215546Sopenharmony_ci
1579bf215546Sopenharmony_ci	if (!tgsi_text_translate(text, tokens, ARRAY_SIZE(tokens))) {
1580bf215546Sopenharmony_ci		assert(false);
1581bf215546Sopenharmony_ci		return;
1582bf215546Sopenharmony_ci	}
1583bf215546Sopenharmony_ci
1584bf215546Sopenharmony_ci	state.ir_type = PIPE_SHADER_IR_TGSI;
1585bf215546Sopenharmony_ci	state.prog = tokens;
1586bf215546Sopenharmony_ci
1587bf215546Sopenharmony_ci	rctx->query_result_shader = rctx->b.create_compute_state(&rctx->b, &state);
1588bf215546Sopenharmony_ci}
1589bf215546Sopenharmony_ci
1590bf215546Sopenharmony_cistatic void r600_restore_qbo_state(struct r600_common_context *rctx,
1591bf215546Sopenharmony_ci				   struct r600_qbo_state *st)
1592bf215546Sopenharmony_ci{
1593bf215546Sopenharmony_ci	rctx->b.bind_compute_state(&rctx->b, st->saved_compute);
1594bf215546Sopenharmony_ci	rctx->b.set_constant_buffer(&rctx->b, PIPE_SHADER_COMPUTE, 0, true, &st->saved_const0);
1595bf215546Sopenharmony_ci	rctx->b.set_shader_buffers(&rctx->b, PIPE_SHADER_COMPUTE, 0, 3, st->saved_ssbo, ~0);
1596bf215546Sopenharmony_ci	for (unsigned i = 0; i < 3; ++i)
1597bf215546Sopenharmony_ci		pipe_resource_reference(&st->saved_ssbo[i].buffer, NULL);
1598bf215546Sopenharmony_ci}
1599bf215546Sopenharmony_ci
1600bf215546Sopenharmony_cistatic void r600_query_hw_get_result_resource(struct r600_common_context *rctx,
1601bf215546Sopenharmony_ci                                              struct r600_query *rquery,
1602bf215546Sopenharmony_ci                                              enum pipe_query_flags flags,
1603bf215546Sopenharmony_ci                                              enum pipe_query_value_type result_type,
1604bf215546Sopenharmony_ci                                              int index,
1605bf215546Sopenharmony_ci                                              struct pipe_resource *resource,
1606bf215546Sopenharmony_ci                                              unsigned offset)
1607bf215546Sopenharmony_ci{
1608bf215546Sopenharmony_ci	struct r600_query_hw *query = (struct r600_query_hw *)rquery;
1609bf215546Sopenharmony_ci	struct r600_query_buffer *qbuf;
1610bf215546Sopenharmony_ci	struct r600_query_buffer *qbuf_prev;
1611bf215546Sopenharmony_ci	struct pipe_resource *tmp_buffer = NULL;
1612bf215546Sopenharmony_ci	unsigned tmp_buffer_offset = 0;
1613bf215546Sopenharmony_ci	struct r600_qbo_state saved_state = {};
1614bf215546Sopenharmony_ci	struct pipe_grid_info grid = {};
1615bf215546Sopenharmony_ci	struct pipe_constant_buffer constant_buffer = {};
1616bf215546Sopenharmony_ci	struct pipe_shader_buffer ssbo[3];
1617bf215546Sopenharmony_ci	struct r600_hw_query_params params;
1618bf215546Sopenharmony_ci	struct {
1619bf215546Sopenharmony_ci		uint32_t end_offset;
1620bf215546Sopenharmony_ci		uint32_t result_stride;
1621bf215546Sopenharmony_ci		uint32_t result_count;
1622bf215546Sopenharmony_ci		uint32_t config;
1623bf215546Sopenharmony_ci		uint32_t fence_offset;
1624bf215546Sopenharmony_ci		uint32_t pair_stride;
1625bf215546Sopenharmony_ci		uint32_t pair_count;
1626bf215546Sopenharmony_ci		uint32_t buffer_offset;
1627bf215546Sopenharmony_ci		uint32_t buffer0_offset;
1628bf215546Sopenharmony_ci	} consts;
1629bf215546Sopenharmony_ci
1630bf215546Sopenharmony_ci	if (!rctx->query_result_shader) {
1631bf215546Sopenharmony_ci		r600_create_query_result_shader(rctx);
1632bf215546Sopenharmony_ci		if (!rctx->query_result_shader)
1633bf215546Sopenharmony_ci			return;
1634bf215546Sopenharmony_ci	}
1635bf215546Sopenharmony_ci
1636bf215546Sopenharmony_ci	if (query->buffer.previous) {
1637bf215546Sopenharmony_ci		u_suballocator_alloc(&rctx->allocator_zeroed_memory, 16, 256,
1638bf215546Sopenharmony_ci				     &tmp_buffer_offset, &tmp_buffer);
1639bf215546Sopenharmony_ci		if (!tmp_buffer)
1640bf215546Sopenharmony_ci			return;
1641bf215546Sopenharmony_ci	}
1642bf215546Sopenharmony_ci
1643bf215546Sopenharmony_ci	rctx->save_qbo_state(&rctx->b, &saved_state);
1644bf215546Sopenharmony_ci
1645bf215546Sopenharmony_ci	r600_get_hw_query_params(rctx, query, index >= 0 ? index : 0, &params);
1646bf215546Sopenharmony_ci	consts.end_offset = params.end_offset - params.start_offset;
1647bf215546Sopenharmony_ci	consts.fence_offset = params.fence_offset - params.start_offset;
1648bf215546Sopenharmony_ci	consts.result_stride = query->result_size;
1649bf215546Sopenharmony_ci	consts.pair_stride = params.pair_stride;
1650bf215546Sopenharmony_ci	consts.pair_count = params.pair_count;
1651bf215546Sopenharmony_ci
1652bf215546Sopenharmony_ci	constant_buffer.buffer_size = sizeof(consts);
1653bf215546Sopenharmony_ci	constant_buffer.user_buffer = &consts;
1654bf215546Sopenharmony_ci
1655bf215546Sopenharmony_ci	ssbo[1].buffer = tmp_buffer;
1656bf215546Sopenharmony_ci	ssbo[1].buffer_offset = tmp_buffer_offset;
1657bf215546Sopenharmony_ci	ssbo[1].buffer_size = 16;
1658bf215546Sopenharmony_ci
1659bf215546Sopenharmony_ci	ssbo[2] = ssbo[1];
1660bf215546Sopenharmony_ci
1661bf215546Sopenharmony_ci	rctx->b.bind_compute_state(&rctx->b, rctx->query_result_shader);
1662bf215546Sopenharmony_ci
1663bf215546Sopenharmony_ci	grid.block[0] = 1;
1664bf215546Sopenharmony_ci	grid.block[1] = 1;
1665bf215546Sopenharmony_ci	grid.block[2] = 1;
1666bf215546Sopenharmony_ci	grid.grid[0] = 1;
1667bf215546Sopenharmony_ci	grid.grid[1] = 1;
1668bf215546Sopenharmony_ci	grid.grid[2] = 1;
1669bf215546Sopenharmony_ci
1670bf215546Sopenharmony_ci	consts.config = 0;
1671bf215546Sopenharmony_ci	if (index < 0)
1672bf215546Sopenharmony_ci		consts.config |= 4;
1673bf215546Sopenharmony_ci	if (query->b.type == PIPE_QUERY_OCCLUSION_PREDICATE ||
1674bf215546Sopenharmony_ci	    query->b.type == PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE)
1675bf215546Sopenharmony_ci		consts.config |= 8;
1676bf215546Sopenharmony_ci	else if (query->b.type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
1677bf215546Sopenharmony_ci		 query->b.type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE)
1678bf215546Sopenharmony_ci		consts.config |= 8 | 256;
1679bf215546Sopenharmony_ci	else if (query->b.type == PIPE_QUERY_TIMESTAMP ||
1680bf215546Sopenharmony_ci		 query->b.type == PIPE_QUERY_TIME_ELAPSED)
1681bf215546Sopenharmony_ci		consts.config |= 32;
1682bf215546Sopenharmony_ci
1683bf215546Sopenharmony_ci	switch (result_type) {
1684bf215546Sopenharmony_ci	case PIPE_QUERY_TYPE_U64:
1685bf215546Sopenharmony_ci	case PIPE_QUERY_TYPE_I64:
1686bf215546Sopenharmony_ci		consts.config |= 64;
1687bf215546Sopenharmony_ci		break;
1688bf215546Sopenharmony_ci	case PIPE_QUERY_TYPE_I32:
1689bf215546Sopenharmony_ci		consts.config |= 128;
1690bf215546Sopenharmony_ci		break;
1691bf215546Sopenharmony_ci	case PIPE_QUERY_TYPE_U32:
1692bf215546Sopenharmony_ci		break;
1693bf215546Sopenharmony_ci	}
1694bf215546Sopenharmony_ci
1695bf215546Sopenharmony_ci	rctx->flags |= rctx->screen->barrier_flags.cp_to_L2;
1696bf215546Sopenharmony_ci
1697bf215546Sopenharmony_ci	for (qbuf = &query->buffer; qbuf; qbuf = qbuf_prev) {
1698bf215546Sopenharmony_ci		if (query->b.type != PIPE_QUERY_TIMESTAMP) {
1699bf215546Sopenharmony_ci			qbuf_prev = qbuf->previous;
1700bf215546Sopenharmony_ci			consts.result_count = qbuf->results_end / query->result_size;
1701bf215546Sopenharmony_ci			consts.config &= ~3;
1702bf215546Sopenharmony_ci			if (qbuf != &query->buffer)
1703bf215546Sopenharmony_ci				consts.config |= 1;
1704bf215546Sopenharmony_ci			if (qbuf->previous)
1705bf215546Sopenharmony_ci				consts.config |= 2;
1706bf215546Sopenharmony_ci		} else {
1707bf215546Sopenharmony_ci			/* Only read the last timestamp. */
1708bf215546Sopenharmony_ci			qbuf_prev = NULL;
1709bf215546Sopenharmony_ci			consts.result_count = 0;
1710bf215546Sopenharmony_ci			consts.config |= 16;
1711bf215546Sopenharmony_ci			params.start_offset += qbuf->results_end - query->result_size;
1712bf215546Sopenharmony_ci		}
1713bf215546Sopenharmony_ci
1714bf215546Sopenharmony_ci		ssbo[0].buffer = &qbuf->buf->b.b;
1715bf215546Sopenharmony_ci		ssbo[0].buffer_offset = params.start_offset & ~0xff;
1716bf215546Sopenharmony_ci		ssbo[0].buffer_size = qbuf->results_end - ssbo[0].buffer_offset;
1717bf215546Sopenharmony_ci		consts.buffer0_offset = (params.start_offset & 0xff);
1718bf215546Sopenharmony_ci		if (!qbuf->previous) {
1719bf215546Sopenharmony_ci
1720bf215546Sopenharmony_ci			ssbo[2].buffer = resource;
1721bf215546Sopenharmony_ci			ssbo[2].buffer_offset = offset & ~0xff;
1722bf215546Sopenharmony_ci			ssbo[2].buffer_size = offset + 8;
1723bf215546Sopenharmony_ci			consts.buffer_offset = (offset & 0xff);
1724bf215546Sopenharmony_ci		} else
1725bf215546Sopenharmony_ci			consts.buffer_offset = 0;
1726bf215546Sopenharmony_ci
1727bf215546Sopenharmony_ci		rctx->b.set_constant_buffer(&rctx->b, PIPE_SHADER_COMPUTE, 0, false, &constant_buffer);
1728bf215546Sopenharmony_ci
1729bf215546Sopenharmony_ci		rctx->b.set_shader_buffers(&rctx->b, PIPE_SHADER_COMPUTE, 0, 3, ssbo, ~0);
1730bf215546Sopenharmony_ci
1731bf215546Sopenharmony_ci		if ((flags & PIPE_QUERY_WAIT) && qbuf == &query->buffer) {
1732bf215546Sopenharmony_ci			uint64_t va;
1733bf215546Sopenharmony_ci
1734bf215546Sopenharmony_ci			/* Wait for result availability. Wait only for readiness
1735bf215546Sopenharmony_ci			 * of the last entry, since the fence writes should be
1736bf215546Sopenharmony_ci			 * serialized in the CP.
1737bf215546Sopenharmony_ci			 */
1738bf215546Sopenharmony_ci			va = qbuf->buf->gpu_address + qbuf->results_end - query->result_size;
1739bf215546Sopenharmony_ci			va += params.fence_offset;
1740bf215546Sopenharmony_ci
1741bf215546Sopenharmony_ci			r600_gfx_wait_fence(rctx, qbuf->buf, va, 0x80000000, 0x80000000);
1742bf215546Sopenharmony_ci		}
1743bf215546Sopenharmony_ci
1744bf215546Sopenharmony_ci		rctx->b.launch_grid(&rctx->b, &grid);
1745bf215546Sopenharmony_ci		rctx->flags |= rctx->screen->barrier_flags.compute_to_L2;
1746bf215546Sopenharmony_ci	}
1747bf215546Sopenharmony_ci
1748bf215546Sopenharmony_ci	r600_restore_qbo_state(rctx, &saved_state);
1749bf215546Sopenharmony_ci	pipe_resource_reference(&tmp_buffer, NULL);
1750bf215546Sopenharmony_ci}
1751bf215546Sopenharmony_ci
1752bf215546Sopenharmony_cistatic void r600_render_condition(struct pipe_context *ctx,
1753bf215546Sopenharmony_ci				  struct pipe_query *query,
1754bf215546Sopenharmony_ci				  bool condition,
1755bf215546Sopenharmony_ci				  enum pipe_render_cond_flag mode)
1756bf215546Sopenharmony_ci{
1757bf215546Sopenharmony_ci	struct r600_common_context *rctx = (struct r600_common_context *)ctx;
1758bf215546Sopenharmony_ci	struct r600_query_hw *rquery = (struct r600_query_hw *)query;
1759bf215546Sopenharmony_ci	struct r600_query_buffer *qbuf;
1760bf215546Sopenharmony_ci	struct r600_atom *atom = &rctx->render_cond_atom;
1761bf215546Sopenharmony_ci
1762bf215546Sopenharmony_ci	/* Compute the size of SET_PREDICATION packets. */
1763bf215546Sopenharmony_ci	atom->num_dw = 0;
1764bf215546Sopenharmony_ci	if (query) {
1765bf215546Sopenharmony_ci		for (qbuf = &rquery->buffer; qbuf; qbuf = qbuf->previous)
1766bf215546Sopenharmony_ci			atom->num_dw += (qbuf->results_end / rquery->result_size) * 5;
1767bf215546Sopenharmony_ci
1768bf215546Sopenharmony_ci		if (rquery->b.type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE)
1769bf215546Sopenharmony_ci			atom->num_dw *= R600_MAX_STREAMS;
1770bf215546Sopenharmony_ci	}
1771bf215546Sopenharmony_ci
1772bf215546Sopenharmony_ci	rctx->render_cond = query;
1773bf215546Sopenharmony_ci	rctx->render_cond_invert = condition;
1774bf215546Sopenharmony_ci	rctx->render_cond_mode = mode;
1775bf215546Sopenharmony_ci
1776bf215546Sopenharmony_ci	rctx->set_atom_dirty(rctx, atom, query != NULL);
1777bf215546Sopenharmony_ci}
1778bf215546Sopenharmony_ci
1779bf215546Sopenharmony_civoid r600_suspend_queries(struct r600_common_context *ctx)
1780bf215546Sopenharmony_ci{
1781bf215546Sopenharmony_ci	struct r600_query_hw *query;
1782bf215546Sopenharmony_ci
1783bf215546Sopenharmony_ci	LIST_FOR_EACH_ENTRY(query, &ctx->active_queries, list) {
1784bf215546Sopenharmony_ci		r600_query_hw_emit_stop(ctx, query);
1785bf215546Sopenharmony_ci	}
1786bf215546Sopenharmony_ci	assert(ctx->num_cs_dw_queries_suspend == 0);
1787bf215546Sopenharmony_ci}
1788bf215546Sopenharmony_ci
1789bf215546Sopenharmony_cistatic unsigned r600_queries_num_cs_dw_for_resuming(struct r600_common_context *ctx,
1790bf215546Sopenharmony_ci						    struct list_head *query_list)
1791bf215546Sopenharmony_ci{
1792bf215546Sopenharmony_ci	struct r600_query_hw *query;
1793bf215546Sopenharmony_ci	unsigned num_dw = 0;
1794bf215546Sopenharmony_ci
1795bf215546Sopenharmony_ci	LIST_FOR_EACH_ENTRY(query, query_list, list) {
1796bf215546Sopenharmony_ci		/* begin + end */
1797bf215546Sopenharmony_ci		num_dw += query->num_cs_dw_begin + query->num_cs_dw_end;
1798bf215546Sopenharmony_ci
1799bf215546Sopenharmony_ci		/* Workaround for the fact that
1800bf215546Sopenharmony_ci		 * num_cs_dw_nontimer_queries_suspend is incremented for every
1801bf215546Sopenharmony_ci		 * resumed query, which raises the bar in need_cs_space for
1802bf215546Sopenharmony_ci		 * queries about to be resumed.
1803bf215546Sopenharmony_ci		 */
1804bf215546Sopenharmony_ci		num_dw += query->num_cs_dw_end;
1805bf215546Sopenharmony_ci	}
1806bf215546Sopenharmony_ci	/* primitives generated query */
1807bf215546Sopenharmony_ci	num_dw += ctx->streamout.enable_atom.num_dw;
1808bf215546Sopenharmony_ci	/* guess for ZPASS enable or PERFECT_ZPASS_COUNT enable updates */
1809bf215546Sopenharmony_ci	num_dw += 13;
1810bf215546Sopenharmony_ci
1811bf215546Sopenharmony_ci	return num_dw;
1812bf215546Sopenharmony_ci}
1813bf215546Sopenharmony_ci
1814bf215546Sopenharmony_civoid r600_resume_queries(struct r600_common_context *ctx)
1815bf215546Sopenharmony_ci{
1816bf215546Sopenharmony_ci	struct r600_query_hw *query;
1817bf215546Sopenharmony_ci	unsigned num_cs_dw = r600_queries_num_cs_dw_for_resuming(ctx, &ctx->active_queries);
1818bf215546Sopenharmony_ci
1819bf215546Sopenharmony_ci	assert(ctx->num_cs_dw_queries_suspend == 0);
1820bf215546Sopenharmony_ci
1821bf215546Sopenharmony_ci	/* Check CS space here. Resuming must not be interrupted by flushes. */
1822bf215546Sopenharmony_ci	ctx->need_gfx_cs_space(&ctx->b, num_cs_dw, true);
1823bf215546Sopenharmony_ci
1824bf215546Sopenharmony_ci	LIST_FOR_EACH_ENTRY(query, &ctx->active_queries, list) {
1825bf215546Sopenharmony_ci		r600_query_hw_emit_start(ctx, query);
1826bf215546Sopenharmony_ci	}
1827bf215546Sopenharmony_ci}
1828bf215546Sopenharmony_ci
1829bf215546Sopenharmony_ci/* Fix radeon_info::enabled_rb_mask for R600, R700, EVERGREEN, NI. */
1830bf215546Sopenharmony_civoid r600_query_fix_enabled_rb_mask(struct r600_common_screen *rscreen)
1831bf215546Sopenharmony_ci{
1832bf215546Sopenharmony_ci	struct r600_common_context *ctx =
1833bf215546Sopenharmony_ci		(struct r600_common_context*)rscreen->aux_context;
1834bf215546Sopenharmony_ci	struct radeon_cmdbuf *cs = &ctx->gfx.cs;
1835bf215546Sopenharmony_ci	struct r600_resource *buffer;
1836bf215546Sopenharmony_ci	uint32_t *results;
1837bf215546Sopenharmony_ci	unsigned i, mask = 0;
1838bf215546Sopenharmony_ci	unsigned max_rbs;
1839bf215546Sopenharmony_ci
1840bf215546Sopenharmony_ci	if (ctx->family == CHIP_JUNIPER) {
1841bf215546Sopenharmony_ci		/*
1842bf215546Sopenharmony_ci		 * Fix for predication lockups - the chip can only ever have
1843bf215546Sopenharmony_ci		 * 4 RBs, however it looks like the predication logic assumes
1844bf215546Sopenharmony_ci		 * there's 8, trying to read results from query buffers never
1845bf215546Sopenharmony_ci		 * written to. By increasing this number we'll write the
1846bf215546Sopenharmony_ci		 * status bit for these as per the normal disabled rb logic.
1847bf215546Sopenharmony_ci		 */
1848bf215546Sopenharmony_ci		ctx->screen->info.max_render_backends = 8;
1849bf215546Sopenharmony_ci	}
1850bf215546Sopenharmony_ci	max_rbs = ctx->screen->info.max_render_backends;
1851bf215546Sopenharmony_ci
1852bf215546Sopenharmony_ci	assert(rscreen->gfx_level <= CAYMAN);
1853bf215546Sopenharmony_ci
1854bf215546Sopenharmony_ci	/*
1855bf215546Sopenharmony_ci	 * if backend_map query is supported by the kernel.
1856bf215546Sopenharmony_ci	 * Note the kernel drm driver for a long time never filled in the
1857bf215546Sopenharmony_ci	 * associated data on eg/cm, only on r600/r700, hence ignore the valid
1858bf215546Sopenharmony_ci	 * bit there if the map is zero.
1859bf215546Sopenharmony_ci	 * (Albeit some chips with just one active rb can have a valid 0 map.)
1860bf215546Sopenharmony_ci	 */
1861bf215546Sopenharmony_ci	if (rscreen->info.r600_gb_backend_map_valid &&
1862bf215546Sopenharmony_ci	    (ctx->gfx_level < EVERGREEN || rscreen->info.r600_gb_backend_map != 0)) {
1863bf215546Sopenharmony_ci		unsigned num_tile_pipes = rscreen->info.num_tile_pipes;
1864bf215546Sopenharmony_ci		unsigned backend_map = rscreen->info.r600_gb_backend_map;
1865bf215546Sopenharmony_ci		unsigned item_width, item_mask;
1866bf215546Sopenharmony_ci
1867bf215546Sopenharmony_ci		if (ctx->gfx_level >= EVERGREEN) {
1868bf215546Sopenharmony_ci			item_width = 4;
1869bf215546Sopenharmony_ci			item_mask = 0x7;
1870bf215546Sopenharmony_ci		} else {
1871bf215546Sopenharmony_ci			item_width = 2;
1872bf215546Sopenharmony_ci			item_mask = 0x3;
1873bf215546Sopenharmony_ci		}
1874bf215546Sopenharmony_ci
1875bf215546Sopenharmony_ci		while (num_tile_pipes--) {
1876bf215546Sopenharmony_ci			i = backend_map & item_mask;
1877bf215546Sopenharmony_ci			mask |= (1<<i);
1878bf215546Sopenharmony_ci			backend_map >>= item_width;
1879bf215546Sopenharmony_ci		}
1880bf215546Sopenharmony_ci		if (mask != 0) {
1881bf215546Sopenharmony_ci			rscreen->info.enabled_rb_mask = mask;
1882bf215546Sopenharmony_ci			return;
1883bf215546Sopenharmony_ci		}
1884bf215546Sopenharmony_ci	}
1885bf215546Sopenharmony_ci
1886bf215546Sopenharmony_ci	/* otherwise backup path for older kernels */
1887bf215546Sopenharmony_ci
1888bf215546Sopenharmony_ci	/* create buffer for event data */
1889bf215546Sopenharmony_ci	buffer = (struct r600_resource*)
1890bf215546Sopenharmony_ci		pipe_buffer_create(ctx->b.screen, 0,
1891bf215546Sopenharmony_ci				   PIPE_USAGE_STAGING, max_rbs * 16);
1892bf215546Sopenharmony_ci	if (!buffer)
1893bf215546Sopenharmony_ci		return;
1894bf215546Sopenharmony_ci
1895bf215546Sopenharmony_ci	/* initialize buffer with zeroes */
1896bf215546Sopenharmony_ci	results = r600_buffer_map_sync_with_rings(ctx, buffer, PIPE_MAP_WRITE);
1897bf215546Sopenharmony_ci	if (results) {
1898bf215546Sopenharmony_ci		memset(results, 0, max_rbs * 4 * 4);
1899bf215546Sopenharmony_ci
1900bf215546Sopenharmony_ci		/* emit EVENT_WRITE for ZPASS_DONE */
1901bf215546Sopenharmony_ci		radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
1902bf215546Sopenharmony_ci		radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
1903bf215546Sopenharmony_ci		radeon_emit(cs, buffer->gpu_address);
1904bf215546Sopenharmony_ci		radeon_emit(cs, buffer->gpu_address >> 32);
1905bf215546Sopenharmony_ci
1906bf215546Sopenharmony_ci		r600_emit_reloc(ctx, &ctx->gfx, buffer,
1907bf215546Sopenharmony_ci                                RADEON_USAGE_WRITE | RADEON_PRIO_QUERY);
1908bf215546Sopenharmony_ci
1909bf215546Sopenharmony_ci		/* analyze results */
1910bf215546Sopenharmony_ci		results = r600_buffer_map_sync_with_rings(ctx, buffer, PIPE_MAP_READ);
1911bf215546Sopenharmony_ci		if (results) {
1912bf215546Sopenharmony_ci			for(i = 0; i < max_rbs; i++) {
1913bf215546Sopenharmony_ci				/* at least highest bit will be set if backend is used */
1914bf215546Sopenharmony_ci				if (results[i*4 + 1])
1915bf215546Sopenharmony_ci					mask |= (1<<i);
1916bf215546Sopenharmony_ci			}
1917bf215546Sopenharmony_ci		}
1918bf215546Sopenharmony_ci	}
1919bf215546Sopenharmony_ci
1920bf215546Sopenharmony_ci	r600_resource_reference(&buffer, NULL);
1921bf215546Sopenharmony_ci
1922bf215546Sopenharmony_ci	if (mask) {
1923bf215546Sopenharmony_ci		if (rscreen->debug_flags & DBG_INFO &&
1924bf215546Sopenharmony_ci		    mask != rscreen->info.enabled_rb_mask) {
1925bf215546Sopenharmony_ci			printf("enabled_rb_mask (fixed) = 0x%x\n", mask);
1926bf215546Sopenharmony_ci		}
1927bf215546Sopenharmony_ci		rscreen->info.enabled_rb_mask = mask;
1928bf215546Sopenharmony_ci	}
1929bf215546Sopenharmony_ci}
1930bf215546Sopenharmony_ci
1931bf215546Sopenharmony_ci#define XFULL(name_, query_type_, type_, result_type_, group_id_) \
1932bf215546Sopenharmony_ci	{ \
1933bf215546Sopenharmony_ci		.name = name_, \
1934bf215546Sopenharmony_ci		.query_type = R600_QUERY_##query_type_, \
1935bf215546Sopenharmony_ci		.type = PIPE_DRIVER_QUERY_TYPE_##type_, \
1936bf215546Sopenharmony_ci		.result_type = PIPE_DRIVER_QUERY_RESULT_TYPE_##result_type_, \
1937bf215546Sopenharmony_ci		.group_id = group_id_ \
1938bf215546Sopenharmony_ci	}
1939bf215546Sopenharmony_ci
1940bf215546Sopenharmony_ci#define X(name_, query_type_, type_, result_type_) \
1941bf215546Sopenharmony_ci	XFULL(name_, query_type_, type_, result_type_, ~(unsigned)0)
1942bf215546Sopenharmony_ci
1943bf215546Sopenharmony_ci#define XG(group_, name_, query_type_, type_, result_type_) \
1944bf215546Sopenharmony_ci	XFULL(name_, query_type_, type_, result_type_, R600_QUERY_GROUP_##group_)
1945bf215546Sopenharmony_ci
1946bf215546Sopenharmony_cistatic const struct pipe_driver_query_info r600_driver_query_list[] = {
1947bf215546Sopenharmony_ci	X("num-compilations",		NUM_COMPILATIONS,	UINT64, CUMULATIVE),
1948bf215546Sopenharmony_ci	X("num-shaders-created",	NUM_SHADERS_CREATED,	UINT64, CUMULATIVE),
1949bf215546Sopenharmony_ci	X("num-shader-cache-hits",	NUM_SHADER_CACHE_HITS,	UINT64, CUMULATIVE),
1950bf215546Sopenharmony_ci	X("draw-calls",			DRAW_CALLS,		UINT64, AVERAGE),
1951bf215546Sopenharmony_ci	X("decompress-calls",		DECOMPRESS_CALLS,	UINT64, AVERAGE),
1952bf215546Sopenharmony_ci	X("MRT-draw-calls",		MRT_DRAW_CALLS,		UINT64, AVERAGE),
1953bf215546Sopenharmony_ci	X("prim-restart-calls",		PRIM_RESTART_CALLS,	UINT64, AVERAGE),
1954bf215546Sopenharmony_ci	X("spill-draw-calls",		SPILL_DRAW_CALLS,	UINT64, AVERAGE),
1955bf215546Sopenharmony_ci	X("compute-calls",		COMPUTE_CALLS,		UINT64, AVERAGE),
1956bf215546Sopenharmony_ci	X("spill-compute-calls",	SPILL_COMPUTE_CALLS,	UINT64, AVERAGE),
1957bf215546Sopenharmony_ci	X("dma-calls",			DMA_CALLS,		UINT64, AVERAGE),
1958bf215546Sopenharmony_ci	X("cp-dma-calls",		CP_DMA_CALLS,		UINT64, AVERAGE),
1959bf215546Sopenharmony_ci	X("num-vs-flushes",		NUM_VS_FLUSHES,		UINT64, AVERAGE),
1960bf215546Sopenharmony_ci	X("num-ps-flushes",		NUM_PS_FLUSHES,		UINT64, AVERAGE),
1961bf215546Sopenharmony_ci	X("num-cs-flushes",		NUM_CS_FLUSHES,		UINT64, AVERAGE),
1962bf215546Sopenharmony_ci	X("num-CB-cache-flushes",	NUM_CB_CACHE_FLUSHES,	UINT64, AVERAGE),
1963bf215546Sopenharmony_ci	X("num-DB-cache-flushes",	NUM_DB_CACHE_FLUSHES,	UINT64, AVERAGE),
1964bf215546Sopenharmony_ci	X("num-resident-handles",	NUM_RESIDENT_HANDLES,	UINT64, AVERAGE),
1965bf215546Sopenharmony_ci	X("tc-offloaded-slots",		TC_OFFLOADED_SLOTS,     UINT64, AVERAGE),
1966bf215546Sopenharmony_ci	X("tc-direct-slots",		TC_DIRECT_SLOTS,	UINT64, AVERAGE),
1967bf215546Sopenharmony_ci	X("tc-num-syncs",		TC_NUM_SYNCS,		UINT64, AVERAGE),
1968bf215546Sopenharmony_ci	X("CS-thread-busy",		CS_THREAD_BUSY,		UINT64, AVERAGE),
1969bf215546Sopenharmony_ci	X("gallium-thread-busy",	GALLIUM_THREAD_BUSY,	UINT64, AVERAGE),
1970bf215546Sopenharmony_ci	X("requested-VRAM",		REQUESTED_VRAM,		BYTES, AVERAGE),
1971bf215546Sopenharmony_ci	X("requested-GTT",		REQUESTED_GTT,		BYTES, AVERAGE),
1972bf215546Sopenharmony_ci	X("mapped-VRAM",		MAPPED_VRAM,		BYTES, AVERAGE),
1973bf215546Sopenharmony_ci	X("mapped-GTT",			MAPPED_GTT,		BYTES, AVERAGE),
1974bf215546Sopenharmony_ci	X("buffer-wait-time",		BUFFER_WAIT_TIME,	MICROSECONDS, CUMULATIVE),
1975bf215546Sopenharmony_ci	X("num-mapped-buffers",		NUM_MAPPED_BUFFERS,	UINT64, AVERAGE),
1976bf215546Sopenharmony_ci	X("num-GFX-IBs",		NUM_GFX_IBS,		UINT64, AVERAGE),
1977bf215546Sopenharmony_ci	X("num-SDMA-IBs",		NUM_SDMA_IBS,		UINT64, AVERAGE),
1978bf215546Sopenharmony_ci	X("GFX-BO-list-size",		GFX_BO_LIST_SIZE,	UINT64, AVERAGE),
1979bf215546Sopenharmony_ci	X("num-bytes-moved",		NUM_BYTES_MOVED,	BYTES, CUMULATIVE),
1980bf215546Sopenharmony_ci	X("num-evictions",		NUM_EVICTIONS,		UINT64, CUMULATIVE),
1981bf215546Sopenharmony_ci	X("VRAM-CPU-page-faults",	NUM_VRAM_CPU_PAGE_FAULTS, UINT64, CUMULATIVE),
1982bf215546Sopenharmony_ci	X("VRAM-usage",			VRAM_USAGE,		BYTES, AVERAGE),
1983bf215546Sopenharmony_ci	X("VRAM-vis-usage",		VRAM_VIS_USAGE,		BYTES, AVERAGE),
1984bf215546Sopenharmony_ci	X("GTT-usage",			GTT_USAGE,		BYTES, AVERAGE),
1985bf215546Sopenharmony_ci
1986bf215546Sopenharmony_ci	/* GPIN queries are for the benefit of old versions of GPUPerfStudio,
1987bf215546Sopenharmony_ci	 * which use it as a fallback path to detect the GPU type.
1988bf215546Sopenharmony_ci	 *
1989bf215546Sopenharmony_ci	 * Note: The names of these queries are significant for GPUPerfStudio
1990bf215546Sopenharmony_ci	 * (and possibly their order as well). */
1991bf215546Sopenharmony_ci	XG(GPIN, "GPIN_000",		GPIN_ASIC_ID,		UINT, AVERAGE),
1992bf215546Sopenharmony_ci	XG(GPIN, "GPIN_001",		GPIN_NUM_SIMD,		UINT, AVERAGE),
1993bf215546Sopenharmony_ci	XG(GPIN, "GPIN_002",		GPIN_NUM_RB,		UINT, AVERAGE),
1994bf215546Sopenharmony_ci	XG(GPIN, "GPIN_003",		GPIN_NUM_SPI,		UINT, AVERAGE),
1995bf215546Sopenharmony_ci	XG(GPIN, "GPIN_004",		GPIN_NUM_SE,		UINT, AVERAGE),
1996bf215546Sopenharmony_ci
1997bf215546Sopenharmony_ci	X("temperature",		GPU_TEMPERATURE,	UINT64, AVERAGE),
1998bf215546Sopenharmony_ci	X("shader-clock",		CURRENT_GPU_SCLK,	HZ, AVERAGE),
1999bf215546Sopenharmony_ci	X("memory-clock",		CURRENT_GPU_MCLK,	HZ, AVERAGE),
2000bf215546Sopenharmony_ci
2001bf215546Sopenharmony_ci	/* The following queries must be at the end of the list because their
2002bf215546Sopenharmony_ci	 * availability is adjusted dynamically based on the DRM version. */
2003bf215546Sopenharmony_ci	X("GPU-load",			GPU_LOAD,		UINT64, AVERAGE),
2004bf215546Sopenharmony_ci	X("GPU-shaders-busy",		GPU_SHADERS_BUSY,	UINT64, AVERAGE),
2005bf215546Sopenharmony_ci	X("GPU-ta-busy",		GPU_TA_BUSY,		UINT64, AVERAGE),
2006bf215546Sopenharmony_ci	X("GPU-gds-busy",		GPU_GDS_BUSY,		UINT64, AVERAGE),
2007bf215546Sopenharmony_ci	X("GPU-vgt-busy",		GPU_VGT_BUSY,		UINT64, AVERAGE),
2008bf215546Sopenharmony_ci	X("GPU-ia-busy",		GPU_IA_BUSY,		UINT64, AVERAGE),
2009bf215546Sopenharmony_ci	X("GPU-sx-busy",		GPU_SX_BUSY,		UINT64, AVERAGE),
2010bf215546Sopenharmony_ci	X("GPU-wd-busy",		GPU_WD_BUSY,		UINT64, AVERAGE),
2011bf215546Sopenharmony_ci	X("GPU-bci-busy",		GPU_BCI_BUSY,		UINT64, AVERAGE),
2012bf215546Sopenharmony_ci	X("GPU-sc-busy",		GPU_SC_BUSY,		UINT64, AVERAGE),
2013bf215546Sopenharmony_ci	X("GPU-pa-busy",		GPU_PA_BUSY,		UINT64, AVERAGE),
2014bf215546Sopenharmony_ci	X("GPU-db-busy",		GPU_DB_BUSY,		UINT64, AVERAGE),
2015bf215546Sopenharmony_ci	X("GPU-cp-busy",		GPU_CP_BUSY,		UINT64, AVERAGE),
2016bf215546Sopenharmony_ci	X("GPU-cb-busy",		GPU_CB_BUSY,		UINT64, AVERAGE),
2017bf215546Sopenharmony_ci	X("GPU-sdma-busy",		GPU_SDMA_BUSY,		UINT64, AVERAGE),
2018bf215546Sopenharmony_ci	X("GPU-pfp-busy",		GPU_PFP_BUSY,		UINT64, AVERAGE),
2019bf215546Sopenharmony_ci	X("GPU-meq-busy",		GPU_MEQ_BUSY,		UINT64, AVERAGE),
2020bf215546Sopenharmony_ci	X("GPU-me-busy",		GPU_ME_BUSY,		UINT64, AVERAGE),
2021bf215546Sopenharmony_ci	X("GPU-surf-sync-busy",		GPU_SURF_SYNC_BUSY,	UINT64, AVERAGE),
2022bf215546Sopenharmony_ci	X("GPU-cp-dma-busy",		GPU_CP_DMA_BUSY,	UINT64, AVERAGE),
2023bf215546Sopenharmony_ci	X("GPU-scratch-ram-busy",	GPU_SCRATCH_RAM_BUSY,	UINT64, AVERAGE),
2024bf215546Sopenharmony_ci};
2025bf215546Sopenharmony_ci
2026bf215546Sopenharmony_ci#undef X
2027bf215546Sopenharmony_ci#undef XG
2028bf215546Sopenharmony_ci#undef XFULL
2029bf215546Sopenharmony_ci
2030bf215546Sopenharmony_cistatic unsigned r600_get_num_queries(struct r600_common_screen *rscreen)
2031bf215546Sopenharmony_ci{
2032bf215546Sopenharmony_ci	return ARRAY_SIZE(r600_driver_query_list);
2033bf215546Sopenharmony_ci}
2034bf215546Sopenharmony_ci
2035bf215546Sopenharmony_cistatic int r600_get_driver_query_info(struct pipe_screen *screen,
2036bf215546Sopenharmony_ci				      unsigned index,
2037bf215546Sopenharmony_ci				      struct pipe_driver_query_info *info)
2038bf215546Sopenharmony_ci{
2039bf215546Sopenharmony_ci	struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
2040bf215546Sopenharmony_ci	unsigned num_queries = r600_get_num_queries(rscreen);
2041bf215546Sopenharmony_ci
2042bf215546Sopenharmony_ci	if (!info) {
2043bf215546Sopenharmony_ci		unsigned num_perfcounters =
2044bf215546Sopenharmony_ci			r600_get_perfcounter_info(rscreen, 0, NULL);
2045bf215546Sopenharmony_ci
2046bf215546Sopenharmony_ci		return num_queries + num_perfcounters;
2047bf215546Sopenharmony_ci	}
2048bf215546Sopenharmony_ci
2049bf215546Sopenharmony_ci	if (index >= num_queries)
2050bf215546Sopenharmony_ci		return r600_get_perfcounter_info(rscreen, index - num_queries, info);
2051bf215546Sopenharmony_ci
2052bf215546Sopenharmony_ci	*info = r600_driver_query_list[index];
2053bf215546Sopenharmony_ci
2054bf215546Sopenharmony_ci	switch (info->query_type) {
2055bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_VRAM:
2056bf215546Sopenharmony_ci	case R600_QUERY_VRAM_USAGE:
2057bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_VRAM:
2058bf215546Sopenharmony_ci		info->max_value.u64 = (uint64_t)rscreen->info.vram_size_kb * 1024;
2059bf215546Sopenharmony_ci		break;
2060bf215546Sopenharmony_ci	case R600_QUERY_REQUESTED_GTT:
2061bf215546Sopenharmony_ci	case R600_QUERY_GTT_USAGE:
2062bf215546Sopenharmony_ci	case R600_QUERY_MAPPED_GTT:
2063bf215546Sopenharmony_ci		info->max_value.u64 = (uint64_t)rscreen->info.gart_size_kb * 1024;
2064bf215546Sopenharmony_ci		break;
2065bf215546Sopenharmony_ci	case R600_QUERY_GPU_TEMPERATURE:
2066bf215546Sopenharmony_ci		info->max_value.u64 = 125;
2067bf215546Sopenharmony_ci		break;
2068bf215546Sopenharmony_ci	case R600_QUERY_VRAM_VIS_USAGE:
2069bf215546Sopenharmony_ci		info->max_value.u64 = (uint64_t)rscreen->info.vram_vis_size_kb * 1024;
2070bf215546Sopenharmony_ci		break;
2071bf215546Sopenharmony_ci	}
2072bf215546Sopenharmony_ci
2073bf215546Sopenharmony_ci	if (info->group_id != ~(unsigned)0 && rscreen->perfcounters)
2074bf215546Sopenharmony_ci		info->group_id += rscreen->perfcounters->num_groups;
2075bf215546Sopenharmony_ci
2076bf215546Sopenharmony_ci	return 1;
2077bf215546Sopenharmony_ci}
2078bf215546Sopenharmony_ci
2079bf215546Sopenharmony_ci/* Note: Unfortunately, GPUPerfStudio hardcodes the order of hardware
2080bf215546Sopenharmony_ci * performance counter groups, so be careful when changing this and related
2081bf215546Sopenharmony_ci * functions.
2082bf215546Sopenharmony_ci */
2083bf215546Sopenharmony_cistatic int r600_get_driver_query_group_info(struct pipe_screen *screen,
2084bf215546Sopenharmony_ci					    unsigned index,
2085bf215546Sopenharmony_ci					    struct pipe_driver_query_group_info *info)
2086bf215546Sopenharmony_ci{
2087bf215546Sopenharmony_ci	struct r600_common_screen *rscreen = (struct r600_common_screen *)screen;
2088bf215546Sopenharmony_ci	unsigned num_pc_groups = 0;
2089bf215546Sopenharmony_ci
2090bf215546Sopenharmony_ci	if (rscreen->perfcounters)
2091bf215546Sopenharmony_ci		num_pc_groups = rscreen->perfcounters->num_groups;
2092bf215546Sopenharmony_ci
2093bf215546Sopenharmony_ci	if (!info)
2094bf215546Sopenharmony_ci		return num_pc_groups + R600_NUM_SW_QUERY_GROUPS;
2095bf215546Sopenharmony_ci
2096bf215546Sopenharmony_ci	if (index < num_pc_groups)
2097bf215546Sopenharmony_ci		return r600_get_perfcounter_group_info(rscreen, index, info);
2098bf215546Sopenharmony_ci
2099bf215546Sopenharmony_ci	index -= num_pc_groups;
2100bf215546Sopenharmony_ci	if (index >= R600_NUM_SW_QUERY_GROUPS)
2101bf215546Sopenharmony_ci		return 0;
2102bf215546Sopenharmony_ci
2103bf215546Sopenharmony_ci	info->name = "GPIN";
2104bf215546Sopenharmony_ci	info->max_active_queries = 5;
2105bf215546Sopenharmony_ci	info->num_queries = 5;
2106bf215546Sopenharmony_ci	return 1;
2107bf215546Sopenharmony_ci}
2108bf215546Sopenharmony_ci
2109bf215546Sopenharmony_civoid r600_query_init(struct r600_common_context *rctx)
2110bf215546Sopenharmony_ci{
2111bf215546Sopenharmony_ci	rctx->b.create_query = r600_create_query;
2112bf215546Sopenharmony_ci	rctx->b.create_batch_query = r600_create_batch_query;
2113bf215546Sopenharmony_ci	rctx->b.destroy_query = r600_destroy_query;
2114bf215546Sopenharmony_ci	rctx->b.begin_query = r600_begin_query;
2115bf215546Sopenharmony_ci	rctx->b.end_query = r600_end_query;
2116bf215546Sopenharmony_ci	rctx->b.get_query_result = r600_get_query_result;
2117bf215546Sopenharmony_ci	rctx->b.get_query_result_resource = r600_get_query_result_resource;
2118bf215546Sopenharmony_ci	rctx->render_cond_atom.emit = r600_emit_query_predication;
2119bf215546Sopenharmony_ci
2120bf215546Sopenharmony_ci	if (((struct r600_common_screen*)rctx->b.screen)->info.max_render_backends > 0)
2121bf215546Sopenharmony_ci	    rctx->b.render_condition = r600_render_condition;
2122bf215546Sopenharmony_ci
2123bf215546Sopenharmony_ci	list_inithead(&rctx->active_queries);
2124bf215546Sopenharmony_ci}
2125bf215546Sopenharmony_ci
2126bf215546Sopenharmony_civoid r600_init_screen_query_functions(struct r600_common_screen *rscreen)
2127bf215546Sopenharmony_ci{
2128bf215546Sopenharmony_ci	rscreen->b.get_driver_query_info = r600_get_driver_query_info;
2129bf215546Sopenharmony_ci	rscreen->b.get_driver_query_group_info = r600_get_driver_query_group_info;
2130bf215546Sopenharmony_ci}
2131