1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright © 2017 Intel Corporation
3bf215546Sopenharmony_ci *
4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation
7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
10bf215546Sopenharmony_ci *
11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included
12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci */
22bf215546Sopenharmony_ci
23bf215546Sopenharmony_ci/**
24bf215546Sopenharmony_ci * @file iris_query.c
25bf215546Sopenharmony_ci *
26bf215546Sopenharmony_ci * ============================= GENXML CODE =============================
27bf215546Sopenharmony_ci *              [This file is compiled once per generation.]
28bf215546Sopenharmony_ci * =======================================================================
29bf215546Sopenharmony_ci *
30bf215546Sopenharmony_ci * Query object support.  This allows measuring various simple statistics
31bf215546Sopenharmony_ci * via counters on the GPU.  We use GenX code for MI_MATH calculations.
32bf215546Sopenharmony_ci */
33bf215546Sopenharmony_ci
34bf215546Sopenharmony_ci#include <stdio.h>
35bf215546Sopenharmony_ci#include <errno.h>
36bf215546Sopenharmony_ci#include "pipe/p_defines.h"
37bf215546Sopenharmony_ci#include "pipe/p_state.h"
38bf215546Sopenharmony_ci#include "pipe/p_context.h"
39bf215546Sopenharmony_ci#include "pipe/p_screen.h"
40bf215546Sopenharmony_ci#include "util/u_inlines.h"
41bf215546Sopenharmony_ci#include "util/u_upload_mgr.h"
42bf215546Sopenharmony_ci#include "iris_context.h"
43bf215546Sopenharmony_ci#include "iris_defines.h"
44bf215546Sopenharmony_ci#include "iris_fence.h"
45bf215546Sopenharmony_ci#include "iris_monitor.h"
46bf215546Sopenharmony_ci#include "iris_resource.h"
47bf215546Sopenharmony_ci#include "iris_screen.h"
48bf215546Sopenharmony_ci
49bf215546Sopenharmony_ci#include "iris_genx_macros.h"
50bf215546Sopenharmony_ci
51bf215546Sopenharmony_ci#define SO_PRIM_STORAGE_NEEDED(n) (GENX(SO_PRIM_STORAGE_NEEDED0_num) + (n) * 8)
52bf215546Sopenharmony_ci#define SO_NUM_PRIMS_WRITTEN(n)   (GENX(SO_NUM_PRIMS_WRITTEN0_num) + (n) * 8)
53bf215546Sopenharmony_ci
54bf215546Sopenharmony_cistruct iris_query {
55bf215546Sopenharmony_ci   struct threaded_query b;
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_ci   enum pipe_query_type type;
58bf215546Sopenharmony_ci   int index;
59bf215546Sopenharmony_ci
60bf215546Sopenharmony_ci   bool ready;
61bf215546Sopenharmony_ci
62bf215546Sopenharmony_ci   bool stalled;
63bf215546Sopenharmony_ci
64bf215546Sopenharmony_ci   uint64_t result;
65bf215546Sopenharmony_ci
66bf215546Sopenharmony_ci   struct iris_state_ref query_state_ref;
67bf215546Sopenharmony_ci   struct iris_query_snapshots *map;
68bf215546Sopenharmony_ci   struct iris_syncobj *syncobj;
69bf215546Sopenharmony_ci
70bf215546Sopenharmony_ci   int batch_idx;
71bf215546Sopenharmony_ci
72bf215546Sopenharmony_ci   struct iris_monitor_object *monitor;
73bf215546Sopenharmony_ci
74bf215546Sopenharmony_ci   /* Fence for PIPE_QUERY_GPU_FINISHED. */
75bf215546Sopenharmony_ci   struct pipe_fence_handle *fence;
76bf215546Sopenharmony_ci};
77bf215546Sopenharmony_ci
78bf215546Sopenharmony_cistruct iris_query_snapshots {
79bf215546Sopenharmony_ci   /** iris_render_condition's saved MI_PREDICATE_RESULT value. */
80bf215546Sopenharmony_ci   uint64_t predicate_result;
81bf215546Sopenharmony_ci
82bf215546Sopenharmony_ci   /** Have the start/end snapshots landed? */
83bf215546Sopenharmony_ci   uint64_t snapshots_landed;
84bf215546Sopenharmony_ci
85bf215546Sopenharmony_ci   /** Starting and ending counter snapshots */
86bf215546Sopenharmony_ci   uint64_t start;
87bf215546Sopenharmony_ci   uint64_t end;
88bf215546Sopenharmony_ci};
89bf215546Sopenharmony_ci
90bf215546Sopenharmony_cistruct iris_query_so_overflow {
91bf215546Sopenharmony_ci   uint64_t predicate_result;
92bf215546Sopenharmony_ci   uint64_t snapshots_landed;
93bf215546Sopenharmony_ci
94bf215546Sopenharmony_ci   struct {
95bf215546Sopenharmony_ci      uint64_t prim_storage_needed[2];
96bf215546Sopenharmony_ci      uint64_t num_prims[2];
97bf215546Sopenharmony_ci   } stream[4];
98bf215546Sopenharmony_ci};
99bf215546Sopenharmony_ci
100bf215546Sopenharmony_cistatic struct mi_value
101bf215546Sopenharmony_ciquery_mem64(struct iris_query *q, uint32_t offset)
102bf215546Sopenharmony_ci{
103bf215546Sopenharmony_ci   struct iris_address addr = {
104bf215546Sopenharmony_ci      .bo = iris_resource_bo(q->query_state_ref.res),
105bf215546Sopenharmony_ci      .offset = q->query_state_ref.offset + offset,
106bf215546Sopenharmony_ci      .access = IRIS_DOMAIN_OTHER_WRITE
107bf215546Sopenharmony_ci   };
108bf215546Sopenharmony_ci   return mi_mem64(addr);
109bf215546Sopenharmony_ci}
110bf215546Sopenharmony_ci
111bf215546Sopenharmony_ci/**
112bf215546Sopenharmony_ci * Is this type of query written by PIPE_CONTROL?
113bf215546Sopenharmony_ci */
114bf215546Sopenharmony_cistatic bool
115bf215546Sopenharmony_ciiris_is_query_pipelined(struct iris_query *q)
116bf215546Sopenharmony_ci{
117bf215546Sopenharmony_ci   switch (q->type) {
118bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_COUNTER:
119bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE:
120bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
121bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP:
122bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP_DISJOINT:
123bf215546Sopenharmony_ci   case PIPE_QUERY_TIME_ELAPSED:
124bf215546Sopenharmony_ci      return true;
125bf215546Sopenharmony_ci
126bf215546Sopenharmony_ci   default:
127bf215546Sopenharmony_ci      return false;
128bf215546Sopenharmony_ci   }
129bf215546Sopenharmony_ci}
130bf215546Sopenharmony_ci
131bf215546Sopenharmony_cistatic void
132bf215546Sopenharmony_cimark_available(struct iris_context *ice, struct iris_query *q)
133bf215546Sopenharmony_ci{
134bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[q->batch_idx];
135bf215546Sopenharmony_ci   unsigned flags = PIPE_CONTROL_WRITE_IMMEDIATE;
136bf215546Sopenharmony_ci   unsigned offset = offsetof(struct iris_query_snapshots, snapshots_landed);
137bf215546Sopenharmony_ci   struct iris_bo *bo = iris_resource_bo(q->query_state_ref.res);
138bf215546Sopenharmony_ci   offset += q->query_state_ref.offset;
139bf215546Sopenharmony_ci
140bf215546Sopenharmony_ci   if (!iris_is_query_pipelined(q)) {
141bf215546Sopenharmony_ci      batch->screen->vtbl.store_data_imm64(batch, bo, offset, true);
142bf215546Sopenharmony_ci   } else {
143bf215546Sopenharmony_ci      /* Order available *after* the query results. */
144bf215546Sopenharmony_ci      flags |= PIPE_CONTROL_FLUSH_ENABLE;
145bf215546Sopenharmony_ci      iris_emit_pipe_control_write(batch, "query: mark available",
146bf215546Sopenharmony_ci                                   flags, bo, offset, true);
147bf215546Sopenharmony_ci   }
148bf215546Sopenharmony_ci}
149bf215546Sopenharmony_ci
150bf215546Sopenharmony_ci/**
151bf215546Sopenharmony_ci * Write PS_DEPTH_COUNT to q->(dest) via a PIPE_CONTROL.
152bf215546Sopenharmony_ci */
153bf215546Sopenharmony_cistatic void
154bf215546Sopenharmony_ciiris_pipelined_write(struct iris_batch *batch,
155bf215546Sopenharmony_ci                     struct iris_query *q,
156bf215546Sopenharmony_ci                     enum pipe_control_flags flags,
157bf215546Sopenharmony_ci                     unsigned offset)
158bf215546Sopenharmony_ci{
159bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &batch->screen->devinfo;
160bf215546Sopenharmony_ci   const unsigned optional_cs_stall =
161bf215546Sopenharmony_ci      GFX_VER == 9 && devinfo->gt == 4 ?  PIPE_CONTROL_CS_STALL : 0;
162bf215546Sopenharmony_ci   struct iris_bo *bo = iris_resource_bo(q->query_state_ref.res);
163bf215546Sopenharmony_ci
164bf215546Sopenharmony_ci   iris_emit_pipe_control_write(batch, "query: pipelined snapshot write",
165bf215546Sopenharmony_ci                                flags | optional_cs_stall,
166bf215546Sopenharmony_ci                                bo, offset, 0ull);
167bf215546Sopenharmony_ci}
168bf215546Sopenharmony_ci
169bf215546Sopenharmony_cistatic void
170bf215546Sopenharmony_ciwrite_value(struct iris_context *ice, struct iris_query *q, unsigned offset)
171bf215546Sopenharmony_ci{
172bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[q->batch_idx];
173bf215546Sopenharmony_ci   struct iris_bo *bo = iris_resource_bo(q->query_state_ref.res);
174bf215546Sopenharmony_ci
175bf215546Sopenharmony_ci   if (!iris_is_query_pipelined(q)) {
176bf215546Sopenharmony_ci      iris_emit_pipe_control_flush(batch,
177bf215546Sopenharmony_ci                                   "query: non-pipelined snapshot write",
178bf215546Sopenharmony_ci                                   PIPE_CONTROL_CS_STALL |
179bf215546Sopenharmony_ci                                   PIPE_CONTROL_STALL_AT_SCOREBOARD);
180bf215546Sopenharmony_ci      q->stalled = true;
181bf215546Sopenharmony_ci   }
182bf215546Sopenharmony_ci
183bf215546Sopenharmony_ci   switch (q->type) {
184bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_COUNTER:
185bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE:
186bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
187bf215546Sopenharmony_ci      if (GFX_VER >= 10) {
188bf215546Sopenharmony_ci         /* "Driver must program PIPE_CONTROL with only Depth Stall Enable
189bf215546Sopenharmony_ci          *  bit set prior to programming a PIPE_CONTROL with Write PS Depth
190bf215546Sopenharmony_ci          *  Count sync operation."
191bf215546Sopenharmony_ci          */
192bf215546Sopenharmony_ci         iris_emit_pipe_control_flush(batch,
193bf215546Sopenharmony_ci                                      "workaround: depth stall before writing "
194bf215546Sopenharmony_ci                                      "PS_DEPTH_COUNT",
195bf215546Sopenharmony_ci                                      PIPE_CONTROL_DEPTH_STALL);
196bf215546Sopenharmony_ci      }
197bf215546Sopenharmony_ci      iris_pipelined_write(&ice->batches[IRIS_BATCH_RENDER], q,
198bf215546Sopenharmony_ci                           PIPE_CONTROL_WRITE_DEPTH_COUNT |
199bf215546Sopenharmony_ci                           PIPE_CONTROL_DEPTH_STALL,
200bf215546Sopenharmony_ci                           offset);
201bf215546Sopenharmony_ci      break;
202bf215546Sopenharmony_ci   case PIPE_QUERY_TIME_ELAPSED:
203bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP:
204bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP_DISJOINT:
205bf215546Sopenharmony_ci      iris_pipelined_write(&ice->batches[IRIS_BATCH_RENDER], q,
206bf215546Sopenharmony_ci                           PIPE_CONTROL_WRITE_TIMESTAMP,
207bf215546Sopenharmony_ci                           offset);
208bf215546Sopenharmony_ci      break;
209bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_GENERATED:
210bf215546Sopenharmony_ci      batch->screen->vtbl.store_register_mem64(batch,
211bf215546Sopenharmony_ci                                     q->index == 0 ?
212bf215546Sopenharmony_ci                                     GENX(CL_INVOCATION_COUNT_num) :
213bf215546Sopenharmony_ci                                     SO_PRIM_STORAGE_NEEDED(q->index),
214bf215546Sopenharmony_ci                                     bo, offset, false);
215bf215546Sopenharmony_ci      break;
216bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_EMITTED:
217bf215546Sopenharmony_ci      batch->screen->vtbl.store_register_mem64(batch,
218bf215546Sopenharmony_ci                                     SO_NUM_PRIMS_WRITTEN(q->index),
219bf215546Sopenharmony_ci                                     bo, offset, false);
220bf215546Sopenharmony_ci      break;
221bf215546Sopenharmony_ci   case PIPE_QUERY_PIPELINE_STATISTICS_SINGLE: {
222bf215546Sopenharmony_ci      static const uint32_t index_to_reg[] = {
223bf215546Sopenharmony_ci         GENX(IA_VERTICES_COUNT_num),
224bf215546Sopenharmony_ci         GENX(IA_PRIMITIVES_COUNT_num),
225bf215546Sopenharmony_ci         GENX(VS_INVOCATION_COUNT_num),
226bf215546Sopenharmony_ci         GENX(GS_INVOCATION_COUNT_num),
227bf215546Sopenharmony_ci         GENX(GS_PRIMITIVES_COUNT_num),
228bf215546Sopenharmony_ci         GENX(CL_INVOCATION_COUNT_num),
229bf215546Sopenharmony_ci         GENX(CL_PRIMITIVES_COUNT_num),
230bf215546Sopenharmony_ci         GENX(PS_INVOCATION_COUNT_num),
231bf215546Sopenharmony_ci         GENX(HS_INVOCATION_COUNT_num),
232bf215546Sopenharmony_ci         GENX(DS_INVOCATION_COUNT_num),
233bf215546Sopenharmony_ci         GENX(CS_INVOCATION_COUNT_num),
234bf215546Sopenharmony_ci      };
235bf215546Sopenharmony_ci      const uint32_t reg = index_to_reg[q->index];
236bf215546Sopenharmony_ci
237bf215546Sopenharmony_ci      batch->screen->vtbl.store_register_mem64(batch, reg, bo, offset, false);
238bf215546Sopenharmony_ci      break;
239bf215546Sopenharmony_ci   }
240bf215546Sopenharmony_ci   default:
241bf215546Sopenharmony_ci      assert(false);
242bf215546Sopenharmony_ci   }
243bf215546Sopenharmony_ci}
244bf215546Sopenharmony_ci
245bf215546Sopenharmony_cistatic void
246bf215546Sopenharmony_ciwrite_overflow_values(struct iris_context *ice, struct iris_query *q, bool end)
247bf215546Sopenharmony_ci{
248bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
249bf215546Sopenharmony_ci   uint32_t count = q->type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ? 1 : 4;
250bf215546Sopenharmony_ci   struct iris_bo *bo = iris_resource_bo(q->query_state_ref.res);
251bf215546Sopenharmony_ci   uint32_t offset = q->query_state_ref.offset;
252bf215546Sopenharmony_ci
253bf215546Sopenharmony_ci   iris_emit_pipe_control_flush(batch,
254bf215546Sopenharmony_ci                                "query: write SO overflow snapshots",
255bf215546Sopenharmony_ci                                PIPE_CONTROL_CS_STALL |
256bf215546Sopenharmony_ci                                PIPE_CONTROL_STALL_AT_SCOREBOARD);
257bf215546Sopenharmony_ci   for (uint32_t i = 0; i < count; i++) {
258bf215546Sopenharmony_ci      int s = q->index + i;
259bf215546Sopenharmony_ci      int g_idx = offset + offsetof(struct iris_query_so_overflow,
260bf215546Sopenharmony_ci                           stream[s].num_prims[end]);
261bf215546Sopenharmony_ci      int w_idx = offset + offsetof(struct iris_query_so_overflow,
262bf215546Sopenharmony_ci                           stream[s].prim_storage_needed[end]);
263bf215546Sopenharmony_ci      batch->screen->vtbl.store_register_mem64(batch, SO_NUM_PRIMS_WRITTEN(s),
264bf215546Sopenharmony_ci                                     bo, g_idx, false);
265bf215546Sopenharmony_ci      batch->screen->vtbl.store_register_mem64(batch, SO_PRIM_STORAGE_NEEDED(s),
266bf215546Sopenharmony_ci                                     bo, w_idx, false);
267bf215546Sopenharmony_ci   }
268bf215546Sopenharmony_ci}
269bf215546Sopenharmony_ci
270bf215546Sopenharmony_cistatic uint64_t
271bf215546Sopenharmony_ciiris_raw_timestamp_delta(uint64_t time0, uint64_t time1)
272bf215546Sopenharmony_ci{
273bf215546Sopenharmony_ci   if (time0 > time1) {
274bf215546Sopenharmony_ci      return (1ULL << TIMESTAMP_BITS) + time1 - time0;
275bf215546Sopenharmony_ci   } else {
276bf215546Sopenharmony_ci      return time1 - time0;
277bf215546Sopenharmony_ci   }
278bf215546Sopenharmony_ci}
279bf215546Sopenharmony_ci
280bf215546Sopenharmony_cistatic bool
281bf215546Sopenharmony_cistream_overflowed(struct iris_query_so_overflow *so, int s)
282bf215546Sopenharmony_ci{
283bf215546Sopenharmony_ci   return (so->stream[s].prim_storage_needed[1] -
284bf215546Sopenharmony_ci           so->stream[s].prim_storage_needed[0]) !=
285bf215546Sopenharmony_ci          (so->stream[s].num_prims[1] - so->stream[s].num_prims[0]);
286bf215546Sopenharmony_ci}
287bf215546Sopenharmony_ci
288bf215546Sopenharmony_cistatic void
289bf215546Sopenharmony_cicalculate_result_on_cpu(const struct intel_device_info *devinfo,
290bf215546Sopenharmony_ci                        struct iris_query *q)
291bf215546Sopenharmony_ci{
292bf215546Sopenharmony_ci   switch (q->type) {
293bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE:
294bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
295bf215546Sopenharmony_ci      q->result = q->map->end != q->map->start;
296bf215546Sopenharmony_ci      break;
297bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP:
298bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP_DISJOINT:
299bf215546Sopenharmony_ci      /* The timestamp is the single starting snapshot. */
300bf215546Sopenharmony_ci      q->result = intel_device_info_timebase_scale(devinfo, q->map->start);
301bf215546Sopenharmony_ci      q->result &= (1ull << TIMESTAMP_BITS) - 1;
302bf215546Sopenharmony_ci      break;
303bf215546Sopenharmony_ci   case PIPE_QUERY_TIME_ELAPSED:
304bf215546Sopenharmony_ci      q->result = iris_raw_timestamp_delta(q->map->start, q->map->end);
305bf215546Sopenharmony_ci      q->result = intel_device_info_timebase_scale(devinfo, q->result);
306bf215546Sopenharmony_ci      q->result &= (1ull << TIMESTAMP_BITS) - 1;
307bf215546Sopenharmony_ci      break;
308bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
309bf215546Sopenharmony_ci      q->result = stream_overflowed((void *) q->map, q->index);
310bf215546Sopenharmony_ci      break;
311bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
312bf215546Sopenharmony_ci      q->result = false;
313bf215546Sopenharmony_ci      for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++)
314bf215546Sopenharmony_ci         q->result |= stream_overflowed((void *) q->map, i);
315bf215546Sopenharmony_ci      break;
316bf215546Sopenharmony_ci   case PIPE_QUERY_PIPELINE_STATISTICS_SINGLE:
317bf215546Sopenharmony_ci      q->result = q->map->end - q->map->start;
318bf215546Sopenharmony_ci
319bf215546Sopenharmony_ci      /* WaDividePSInvocationCountBy4:HSW,BDW */
320bf215546Sopenharmony_ci      if (GFX_VER == 8 && q->index == PIPE_STAT_QUERY_PS_INVOCATIONS)
321bf215546Sopenharmony_ci         q->result /= 4;
322bf215546Sopenharmony_ci      break;
323bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_COUNTER:
324bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_GENERATED:
325bf215546Sopenharmony_ci   case PIPE_QUERY_PRIMITIVES_EMITTED:
326bf215546Sopenharmony_ci   default:
327bf215546Sopenharmony_ci      q->result = q->map->end - q->map->start;
328bf215546Sopenharmony_ci      break;
329bf215546Sopenharmony_ci   }
330bf215546Sopenharmony_ci
331bf215546Sopenharmony_ci   q->ready = true;
332bf215546Sopenharmony_ci}
333bf215546Sopenharmony_ci
334bf215546Sopenharmony_ci/**
335bf215546Sopenharmony_ci * Calculate the streamout overflow for stream \p idx:
336bf215546Sopenharmony_ci *
337bf215546Sopenharmony_ci * (num_prims[1] - num_prims[0]) - (storage_needed[1] - storage_needed[0])
338bf215546Sopenharmony_ci */
339bf215546Sopenharmony_cistatic struct mi_value
340bf215546Sopenharmony_cicalc_overflow_for_stream(struct mi_builder *b,
341bf215546Sopenharmony_ci                         struct iris_query *q,
342bf215546Sopenharmony_ci                         int idx)
343bf215546Sopenharmony_ci{
344bf215546Sopenharmony_ci#define C(counter, i) query_mem64(q, \
345bf215546Sopenharmony_ci   offsetof(struct iris_query_so_overflow, stream[idx].counter[i]))
346bf215546Sopenharmony_ci
347bf215546Sopenharmony_ci   return mi_isub(b, mi_isub(b, C(num_prims, 1), C(num_prims, 0)),
348bf215546Sopenharmony_ci                     mi_isub(b, C(prim_storage_needed, 1),
349bf215546Sopenharmony_ci                                C(prim_storage_needed, 0)));
350bf215546Sopenharmony_ci#undef C
351bf215546Sopenharmony_ci}
352bf215546Sopenharmony_ci
353bf215546Sopenharmony_ci/**
354bf215546Sopenharmony_ci * Calculate whether any stream has overflowed.
355bf215546Sopenharmony_ci */
356bf215546Sopenharmony_cistatic struct mi_value
357bf215546Sopenharmony_cicalc_overflow_any_stream(struct mi_builder *b, struct iris_query *q)
358bf215546Sopenharmony_ci{
359bf215546Sopenharmony_ci   struct mi_value stream_result[PIPE_MAX_VERTEX_STREAMS];
360bf215546Sopenharmony_ci   for (int i = 0; i < PIPE_MAX_VERTEX_STREAMS; i++)
361bf215546Sopenharmony_ci      stream_result[i] = calc_overflow_for_stream(b, q, i);
362bf215546Sopenharmony_ci
363bf215546Sopenharmony_ci   struct mi_value result = stream_result[0];
364bf215546Sopenharmony_ci   for (int i = 1; i < PIPE_MAX_VERTEX_STREAMS; i++)
365bf215546Sopenharmony_ci      result = mi_ior(b, result, stream_result[i]);
366bf215546Sopenharmony_ci
367bf215546Sopenharmony_ci   return result;
368bf215546Sopenharmony_ci}
369bf215546Sopenharmony_ci
370bf215546Sopenharmony_cistatic bool
371bf215546Sopenharmony_ciquery_is_boolean(enum pipe_query_type type)
372bf215546Sopenharmony_ci{
373bf215546Sopenharmony_ci   switch (type) {
374bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE:
375bf215546Sopenharmony_ci   case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
376bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
377bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
378bf215546Sopenharmony_ci      return true;
379bf215546Sopenharmony_ci   default:
380bf215546Sopenharmony_ci      return false;
381bf215546Sopenharmony_ci   }
382bf215546Sopenharmony_ci}
383bf215546Sopenharmony_ci
384bf215546Sopenharmony_ci/**
385bf215546Sopenharmony_ci * Calculate the result using MI_MATH.
386bf215546Sopenharmony_ci */
387bf215546Sopenharmony_cistatic struct mi_value
388bf215546Sopenharmony_cicalculate_result_on_gpu(const struct intel_device_info *devinfo,
389bf215546Sopenharmony_ci                        struct mi_builder *b,
390bf215546Sopenharmony_ci                        struct iris_query *q)
391bf215546Sopenharmony_ci{
392bf215546Sopenharmony_ci   struct mi_value result;
393bf215546Sopenharmony_ci   struct mi_value start_val =
394bf215546Sopenharmony_ci      query_mem64(q, offsetof(struct iris_query_snapshots, start));
395bf215546Sopenharmony_ci   struct mi_value end_val =
396bf215546Sopenharmony_ci      query_mem64(q, offsetof(struct iris_query_snapshots, end));
397bf215546Sopenharmony_ci
398bf215546Sopenharmony_ci   switch (q->type) {
399bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
400bf215546Sopenharmony_ci      result = calc_overflow_for_stream(b, q, q->index);
401bf215546Sopenharmony_ci      break;
402bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
403bf215546Sopenharmony_ci      result = calc_overflow_any_stream(b, q);
404bf215546Sopenharmony_ci      break;
405bf215546Sopenharmony_ci   case PIPE_QUERY_TIMESTAMP: {
406bf215546Sopenharmony_ci      /* TODO: This discards any fractional bits of the timebase scale.
407bf215546Sopenharmony_ci       * We would need to do a bit of fixed point math on the CS ALU, or
408bf215546Sopenharmony_ci       * launch an actual shader to calculate this with full precision.
409bf215546Sopenharmony_ci       */
410bf215546Sopenharmony_ci      uint32_t scale = 1000000000ull / devinfo->timestamp_frequency;
411bf215546Sopenharmony_ci      result = mi_iand(b, mi_imm((1ull << 36) - 1),
412bf215546Sopenharmony_ci                          mi_imul_imm(b, start_val, scale));
413bf215546Sopenharmony_ci      break;
414bf215546Sopenharmony_ci   }
415bf215546Sopenharmony_ci   case PIPE_QUERY_TIME_ELAPSED: {
416bf215546Sopenharmony_ci      /* TODO: This discards fractional bits (see above). */
417bf215546Sopenharmony_ci      uint32_t scale = 1000000000ull / devinfo->timestamp_frequency;
418bf215546Sopenharmony_ci      result = mi_imul_imm(b, mi_isub(b, end_val, start_val), scale);
419bf215546Sopenharmony_ci      break;
420bf215546Sopenharmony_ci   }
421bf215546Sopenharmony_ci   default:
422bf215546Sopenharmony_ci      result = mi_isub(b, end_val, start_val);
423bf215546Sopenharmony_ci      break;
424bf215546Sopenharmony_ci   }
425bf215546Sopenharmony_ci
426bf215546Sopenharmony_ci   /* WaDividePSInvocationCountBy4:HSW,BDW */
427bf215546Sopenharmony_ci   if (GFX_VER == 8 &&
428bf215546Sopenharmony_ci       q->type == PIPE_QUERY_PIPELINE_STATISTICS_SINGLE &&
429bf215546Sopenharmony_ci       q->index == PIPE_STAT_QUERY_PS_INVOCATIONS)
430bf215546Sopenharmony_ci      result = mi_ushr32_imm(b, result, 2);
431bf215546Sopenharmony_ci
432bf215546Sopenharmony_ci   if (query_is_boolean(q->type))
433bf215546Sopenharmony_ci      result = mi_iand(b, mi_nz(b, result), mi_imm(1));
434bf215546Sopenharmony_ci
435bf215546Sopenharmony_ci   return result;
436bf215546Sopenharmony_ci}
437bf215546Sopenharmony_ci
438bf215546Sopenharmony_cistatic struct pipe_query *
439bf215546Sopenharmony_ciiris_create_query(struct pipe_context *ctx,
440bf215546Sopenharmony_ci                  unsigned query_type,
441bf215546Sopenharmony_ci                  unsigned index)
442bf215546Sopenharmony_ci{
443bf215546Sopenharmony_ci   struct iris_query *q = calloc(1, sizeof(struct iris_query));
444bf215546Sopenharmony_ci
445bf215546Sopenharmony_ci   q->type = query_type;
446bf215546Sopenharmony_ci   q->index = index;
447bf215546Sopenharmony_ci   q->monitor = NULL;
448bf215546Sopenharmony_ci
449bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_PIPELINE_STATISTICS_SINGLE &&
450bf215546Sopenharmony_ci       q->index == PIPE_STAT_QUERY_CS_INVOCATIONS)
451bf215546Sopenharmony_ci      q->batch_idx = IRIS_BATCH_COMPUTE;
452bf215546Sopenharmony_ci   else
453bf215546Sopenharmony_ci      q->batch_idx = IRIS_BATCH_RENDER;
454bf215546Sopenharmony_ci   return (struct pipe_query *) q;
455bf215546Sopenharmony_ci}
456bf215546Sopenharmony_ci
457bf215546Sopenharmony_cistatic struct pipe_query *
458bf215546Sopenharmony_ciiris_create_batch_query(struct pipe_context *ctx,
459bf215546Sopenharmony_ci                        unsigned num_queries,
460bf215546Sopenharmony_ci                        unsigned *query_types)
461bf215546Sopenharmony_ci{
462bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
463bf215546Sopenharmony_ci   struct iris_query *q = calloc(1, sizeof(struct iris_query));
464bf215546Sopenharmony_ci   if (unlikely(!q))
465bf215546Sopenharmony_ci      return NULL;
466bf215546Sopenharmony_ci   q->type = PIPE_QUERY_DRIVER_SPECIFIC;
467bf215546Sopenharmony_ci   q->index = -1;
468bf215546Sopenharmony_ci   q->monitor = iris_create_monitor_object(ice, num_queries, query_types);
469bf215546Sopenharmony_ci   if (unlikely(!q->monitor)) {
470bf215546Sopenharmony_ci      free(q);
471bf215546Sopenharmony_ci      return NULL;
472bf215546Sopenharmony_ci   }
473bf215546Sopenharmony_ci
474bf215546Sopenharmony_ci   return (struct pipe_query *) q;
475bf215546Sopenharmony_ci}
476bf215546Sopenharmony_ci
477bf215546Sopenharmony_cistatic void
478bf215546Sopenharmony_ciiris_destroy_query(struct pipe_context *ctx, struct pipe_query *p_query)
479bf215546Sopenharmony_ci{
480bf215546Sopenharmony_ci   struct iris_query *query = (void *) p_query;
481bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) ctx->screen;
482bf215546Sopenharmony_ci   if (query->monitor) {
483bf215546Sopenharmony_ci      iris_destroy_monitor_object(ctx, query->monitor);
484bf215546Sopenharmony_ci      query->monitor = NULL;
485bf215546Sopenharmony_ci   } else {
486bf215546Sopenharmony_ci      iris_syncobj_reference(screen->bufmgr, &query->syncobj, NULL);
487bf215546Sopenharmony_ci      screen->base.fence_reference(ctx->screen, &query->fence, NULL);
488bf215546Sopenharmony_ci   }
489bf215546Sopenharmony_ci   pipe_resource_reference(&query->query_state_ref.res, NULL);
490bf215546Sopenharmony_ci   free(query);
491bf215546Sopenharmony_ci}
492bf215546Sopenharmony_ci
493bf215546Sopenharmony_ci
494bf215546Sopenharmony_cistatic bool
495bf215546Sopenharmony_ciiris_begin_query(struct pipe_context *ctx, struct pipe_query *query)
496bf215546Sopenharmony_ci{
497bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
498bf215546Sopenharmony_ci   struct iris_query *q = (void *) query;
499bf215546Sopenharmony_ci
500bf215546Sopenharmony_ci   if (q->monitor)
501bf215546Sopenharmony_ci      return iris_begin_monitor(ctx, q->monitor);
502bf215546Sopenharmony_ci
503bf215546Sopenharmony_ci   void *ptr = NULL;
504bf215546Sopenharmony_ci   uint32_t size;
505bf215546Sopenharmony_ci
506bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
507bf215546Sopenharmony_ci       q->type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE)
508bf215546Sopenharmony_ci      size = sizeof(struct iris_query_so_overflow);
509bf215546Sopenharmony_ci   else
510bf215546Sopenharmony_ci      size = sizeof(struct iris_query_snapshots);
511bf215546Sopenharmony_ci
512bf215546Sopenharmony_ci   u_upload_alloc(ice->query_buffer_uploader, 0,
513bf215546Sopenharmony_ci                  size, size, &q->query_state_ref.offset,
514bf215546Sopenharmony_ci                  &q->query_state_ref.res, &ptr);
515bf215546Sopenharmony_ci
516bf215546Sopenharmony_ci   if (!iris_resource_bo(q->query_state_ref.res))
517bf215546Sopenharmony_ci      return false;
518bf215546Sopenharmony_ci
519bf215546Sopenharmony_ci   q->map = ptr;
520bf215546Sopenharmony_ci   if (!q->map)
521bf215546Sopenharmony_ci      return false;
522bf215546Sopenharmony_ci
523bf215546Sopenharmony_ci   q->result = 0ull;
524bf215546Sopenharmony_ci   q->ready = false;
525bf215546Sopenharmony_ci   WRITE_ONCE(q->map->snapshots_landed, false);
526bf215546Sopenharmony_ci
527bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_PRIMITIVES_GENERATED && q->index == 0) {
528bf215546Sopenharmony_ci      ice->state.prims_generated_query_active = true;
529bf215546Sopenharmony_ci      ice->state.dirty |= IRIS_DIRTY_STREAMOUT | IRIS_DIRTY_CLIP;
530bf215546Sopenharmony_ci   }
531bf215546Sopenharmony_ci
532bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
533bf215546Sopenharmony_ci       q->type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE)
534bf215546Sopenharmony_ci      write_overflow_values(ice, q, false);
535bf215546Sopenharmony_ci   else
536bf215546Sopenharmony_ci      write_value(ice, q,
537bf215546Sopenharmony_ci                  q->query_state_ref.offset +
538bf215546Sopenharmony_ci                  offsetof(struct iris_query_snapshots, start));
539bf215546Sopenharmony_ci
540bf215546Sopenharmony_ci   return true;
541bf215546Sopenharmony_ci}
542bf215546Sopenharmony_ci
543bf215546Sopenharmony_cistatic bool
544bf215546Sopenharmony_ciiris_end_query(struct pipe_context *ctx, struct pipe_query *query)
545bf215546Sopenharmony_ci{
546bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
547bf215546Sopenharmony_ci   struct iris_query *q = (void *) query;
548bf215546Sopenharmony_ci
549bf215546Sopenharmony_ci   if (q->monitor)
550bf215546Sopenharmony_ci      return iris_end_monitor(ctx, q->monitor);
551bf215546Sopenharmony_ci
552bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_GPU_FINISHED) {
553bf215546Sopenharmony_ci      ctx->flush(ctx, &q->fence, PIPE_FLUSH_DEFERRED);
554bf215546Sopenharmony_ci      return true;
555bf215546Sopenharmony_ci   }
556bf215546Sopenharmony_ci
557bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[q->batch_idx];
558bf215546Sopenharmony_ci
559bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_TIMESTAMP) {
560bf215546Sopenharmony_ci      iris_begin_query(ctx, query);
561bf215546Sopenharmony_ci      iris_batch_reference_signal_syncobj(batch, &q->syncobj);
562bf215546Sopenharmony_ci      mark_available(ice, q);
563bf215546Sopenharmony_ci      return true;
564bf215546Sopenharmony_ci   }
565bf215546Sopenharmony_ci
566bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_PRIMITIVES_GENERATED && q->index == 0) {
567bf215546Sopenharmony_ci      ice->state.prims_generated_query_active = false;
568bf215546Sopenharmony_ci      ice->state.dirty |= IRIS_DIRTY_STREAMOUT | IRIS_DIRTY_CLIP;
569bf215546Sopenharmony_ci   }
570bf215546Sopenharmony_ci
571bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_SO_OVERFLOW_PREDICATE ||
572bf215546Sopenharmony_ci       q->type == PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE)
573bf215546Sopenharmony_ci      write_overflow_values(ice, q, true);
574bf215546Sopenharmony_ci   else
575bf215546Sopenharmony_ci      write_value(ice, q,
576bf215546Sopenharmony_ci                  q->query_state_ref.offset +
577bf215546Sopenharmony_ci                  offsetof(struct iris_query_snapshots, end));
578bf215546Sopenharmony_ci
579bf215546Sopenharmony_ci   iris_batch_reference_signal_syncobj(batch, &q->syncobj);
580bf215546Sopenharmony_ci   mark_available(ice, q);
581bf215546Sopenharmony_ci
582bf215546Sopenharmony_ci   return true;
583bf215546Sopenharmony_ci}
584bf215546Sopenharmony_ci
585bf215546Sopenharmony_ci/**
586bf215546Sopenharmony_ci * See if the snapshots have landed for a query, and if so, compute the
587bf215546Sopenharmony_ci * result and mark it ready.  Does not flush (unlike iris_get_query_result).
588bf215546Sopenharmony_ci */
589bf215546Sopenharmony_cistatic void
590bf215546Sopenharmony_ciiris_check_query_no_flush(struct iris_context *ice, struct iris_query *q)
591bf215546Sopenharmony_ci{
592bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) ice->ctx.screen;
593bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
594bf215546Sopenharmony_ci
595bf215546Sopenharmony_ci   if (!q->ready && READ_ONCE(q->map->snapshots_landed)) {
596bf215546Sopenharmony_ci      calculate_result_on_cpu(devinfo, q);
597bf215546Sopenharmony_ci   }
598bf215546Sopenharmony_ci}
599bf215546Sopenharmony_ci
600bf215546Sopenharmony_cistatic bool
601bf215546Sopenharmony_ciiris_get_query_result(struct pipe_context *ctx,
602bf215546Sopenharmony_ci                      struct pipe_query *query,
603bf215546Sopenharmony_ci                      bool wait,
604bf215546Sopenharmony_ci                      union pipe_query_result *result)
605bf215546Sopenharmony_ci{
606bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
607bf215546Sopenharmony_ci   struct iris_query *q = (void *) query;
608bf215546Sopenharmony_ci
609bf215546Sopenharmony_ci   if (q->monitor)
610bf215546Sopenharmony_ci      return iris_get_monitor_result(ctx, q->monitor, wait, result->batch);
611bf215546Sopenharmony_ci
612bf215546Sopenharmony_ci   struct iris_screen *screen = (void *) ctx->screen;
613bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &screen->devinfo;
614bf215546Sopenharmony_ci
615bf215546Sopenharmony_ci   if (unlikely(screen->devinfo.no_hw)) {
616bf215546Sopenharmony_ci      result->u64 = 0;
617bf215546Sopenharmony_ci      return true;
618bf215546Sopenharmony_ci   }
619bf215546Sopenharmony_ci
620bf215546Sopenharmony_ci   if (q->type == PIPE_QUERY_GPU_FINISHED) {
621bf215546Sopenharmony_ci      struct pipe_screen *screen = ctx->screen;
622bf215546Sopenharmony_ci
623bf215546Sopenharmony_ci      result->b = screen->fence_finish(screen, ctx, q->fence,
624bf215546Sopenharmony_ci                                       wait ? PIPE_TIMEOUT_INFINITE : 0);
625bf215546Sopenharmony_ci      return result->b;
626bf215546Sopenharmony_ci   }
627bf215546Sopenharmony_ci
628bf215546Sopenharmony_ci   if (!q->ready) {
629bf215546Sopenharmony_ci      struct iris_batch *batch = &ice->batches[q->batch_idx];
630bf215546Sopenharmony_ci      if (q->syncobj == iris_batch_get_signal_syncobj(batch))
631bf215546Sopenharmony_ci         iris_batch_flush(batch);
632bf215546Sopenharmony_ci
633bf215546Sopenharmony_ci      while (!READ_ONCE(q->map->snapshots_landed)) {
634bf215546Sopenharmony_ci         if (wait)
635bf215546Sopenharmony_ci            iris_wait_syncobj(screen->bufmgr, q->syncobj, INT64_MAX);
636bf215546Sopenharmony_ci         else
637bf215546Sopenharmony_ci            return false;
638bf215546Sopenharmony_ci      }
639bf215546Sopenharmony_ci
640bf215546Sopenharmony_ci      assert(READ_ONCE(q->map->snapshots_landed));
641bf215546Sopenharmony_ci      calculate_result_on_cpu(devinfo, q);
642bf215546Sopenharmony_ci   }
643bf215546Sopenharmony_ci
644bf215546Sopenharmony_ci   assert(q->ready);
645bf215546Sopenharmony_ci
646bf215546Sopenharmony_ci   result->u64 = q->result;
647bf215546Sopenharmony_ci
648bf215546Sopenharmony_ci   return true;
649bf215546Sopenharmony_ci}
650bf215546Sopenharmony_ci
651bf215546Sopenharmony_cistatic void
652bf215546Sopenharmony_ciiris_get_query_result_resource(struct pipe_context *ctx,
653bf215546Sopenharmony_ci                               struct pipe_query *query,
654bf215546Sopenharmony_ci                               enum pipe_query_flags flags,
655bf215546Sopenharmony_ci                               enum pipe_query_value_type result_type,
656bf215546Sopenharmony_ci                               int index,
657bf215546Sopenharmony_ci                               struct pipe_resource *p_res,
658bf215546Sopenharmony_ci                               unsigned offset)
659bf215546Sopenharmony_ci{
660bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
661bf215546Sopenharmony_ci   struct iris_query *q = (void *) query;
662bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[q->batch_idx];
663bf215546Sopenharmony_ci   const struct intel_device_info *devinfo = &batch->screen->devinfo;
664bf215546Sopenharmony_ci   struct iris_resource *res = (void *) p_res;
665bf215546Sopenharmony_ci   struct iris_bo *query_bo = iris_resource_bo(q->query_state_ref.res);
666bf215546Sopenharmony_ci   struct iris_bo *dst_bo = iris_resource_bo(p_res);
667bf215546Sopenharmony_ci   unsigned snapshots_landed_offset =
668bf215546Sopenharmony_ci      offsetof(struct iris_query_snapshots, snapshots_landed);
669bf215546Sopenharmony_ci
670bf215546Sopenharmony_ci   res->bind_history |= PIPE_BIND_QUERY_BUFFER;
671bf215546Sopenharmony_ci
672bf215546Sopenharmony_ci   if (index == -1) {
673bf215546Sopenharmony_ci      /* They're asking for the availability of the result.  If we still
674bf215546Sopenharmony_ci       * have commands queued up which produce the result, submit them
675bf215546Sopenharmony_ci       * now so that progress happens.  Either way, copy the snapshots
676bf215546Sopenharmony_ci       * landed field to the destination resource.
677bf215546Sopenharmony_ci       */
678bf215546Sopenharmony_ci      if (q->syncobj == iris_batch_get_signal_syncobj(batch))
679bf215546Sopenharmony_ci         iris_batch_flush(batch);
680bf215546Sopenharmony_ci
681bf215546Sopenharmony_ci      batch->screen->vtbl.copy_mem_mem(batch, dst_bo, offset,
682bf215546Sopenharmony_ci                             query_bo, snapshots_landed_offset,
683bf215546Sopenharmony_ci                             result_type <= PIPE_QUERY_TYPE_U32 ? 4 : 8);
684bf215546Sopenharmony_ci      return;
685bf215546Sopenharmony_ci   }
686bf215546Sopenharmony_ci
687bf215546Sopenharmony_ci   if (!q->ready && READ_ONCE(q->map->snapshots_landed)) {
688bf215546Sopenharmony_ci      /* The final snapshots happen to have landed, so let's just compute
689bf215546Sopenharmony_ci       * the result on the CPU now...
690bf215546Sopenharmony_ci       */
691bf215546Sopenharmony_ci      calculate_result_on_cpu(devinfo, q);
692bf215546Sopenharmony_ci   }
693bf215546Sopenharmony_ci
694bf215546Sopenharmony_ci   if (q->ready) {
695bf215546Sopenharmony_ci      /* We happen to have the result on the CPU, so just copy it. */
696bf215546Sopenharmony_ci      if (result_type <= PIPE_QUERY_TYPE_U32) {
697bf215546Sopenharmony_ci         batch->screen->vtbl.store_data_imm32(batch, dst_bo, offset, q->result);
698bf215546Sopenharmony_ci      } else {
699bf215546Sopenharmony_ci         batch->screen->vtbl.store_data_imm64(batch, dst_bo, offset, q->result);
700bf215546Sopenharmony_ci      }
701bf215546Sopenharmony_ci
702bf215546Sopenharmony_ci      /* Make sure QBO is flushed before its result is used elsewhere. */
703bf215546Sopenharmony_ci      iris_dirty_for_history(ice, res);
704bf215546Sopenharmony_ci      return;
705bf215546Sopenharmony_ci   }
706bf215546Sopenharmony_ci
707bf215546Sopenharmony_ci   bool predicated = !(flags & PIPE_QUERY_WAIT) && !q->stalled;
708bf215546Sopenharmony_ci
709bf215546Sopenharmony_ci   struct mi_builder b;
710bf215546Sopenharmony_ci   mi_builder_init(&b, &batch->screen->devinfo, batch);
711bf215546Sopenharmony_ci
712bf215546Sopenharmony_ci   iris_batch_sync_region_start(batch);
713bf215546Sopenharmony_ci
714bf215546Sopenharmony_ci   struct mi_value result = calculate_result_on_gpu(devinfo, &b, q);
715bf215546Sopenharmony_ci   struct mi_value dst =
716bf215546Sopenharmony_ci      result_type <= PIPE_QUERY_TYPE_U32 ?
717bf215546Sopenharmony_ci      mi_mem32(rw_bo(dst_bo, offset, IRIS_DOMAIN_OTHER_WRITE)) :
718bf215546Sopenharmony_ci      mi_mem64(rw_bo(dst_bo, offset, IRIS_DOMAIN_OTHER_WRITE));
719bf215546Sopenharmony_ci
720bf215546Sopenharmony_ci   if (predicated) {
721bf215546Sopenharmony_ci      mi_store(&b, mi_reg32(MI_PREDICATE_RESULT),
722bf215546Sopenharmony_ci                   mi_mem64(ro_bo(query_bo, snapshots_landed_offset)));
723bf215546Sopenharmony_ci      mi_store_if(&b, dst, result);
724bf215546Sopenharmony_ci   } else {
725bf215546Sopenharmony_ci      mi_store(&b, dst, result);
726bf215546Sopenharmony_ci   }
727bf215546Sopenharmony_ci
728bf215546Sopenharmony_ci   iris_batch_sync_region_end(batch);
729bf215546Sopenharmony_ci}
730bf215546Sopenharmony_ci
731bf215546Sopenharmony_cistatic void
732bf215546Sopenharmony_ciiris_set_active_query_state(struct pipe_context *ctx, bool enable)
733bf215546Sopenharmony_ci{
734bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
735bf215546Sopenharmony_ci
736bf215546Sopenharmony_ci   if (ice->state.statistics_counters_enabled == enable)
737bf215546Sopenharmony_ci      return;
738bf215546Sopenharmony_ci
739bf215546Sopenharmony_ci   // XXX: most packets aren't paying attention to this yet, because it'd
740bf215546Sopenharmony_ci   // have to be done dynamically at draw time, which is a pain
741bf215546Sopenharmony_ci   ice->state.statistics_counters_enabled = enable;
742bf215546Sopenharmony_ci   ice->state.dirty |= IRIS_DIRTY_CLIP |
743bf215546Sopenharmony_ci                       IRIS_DIRTY_RASTER |
744bf215546Sopenharmony_ci                       IRIS_DIRTY_STREAMOUT |
745bf215546Sopenharmony_ci                       IRIS_DIRTY_WM;
746bf215546Sopenharmony_ci   ice->state.stage_dirty |= IRIS_STAGE_DIRTY_GS |
747bf215546Sopenharmony_ci                             IRIS_STAGE_DIRTY_TCS |
748bf215546Sopenharmony_ci                             IRIS_STAGE_DIRTY_TES |
749bf215546Sopenharmony_ci                             IRIS_STAGE_DIRTY_VS;
750bf215546Sopenharmony_ci}
751bf215546Sopenharmony_ci
752bf215546Sopenharmony_cistatic void
753bf215546Sopenharmony_ciset_predicate_enable(struct iris_context *ice, bool value)
754bf215546Sopenharmony_ci{
755bf215546Sopenharmony_ci   if (value)
756bf215546Sopenharmony_ci      ice->state.predicate = IRIS_PREDICATE_STATE_RENDER;
757bf215546Sopenharmony_ci   else
758bf215546Sopenharmony_ci      ice->state.predicate = IRIS_PREDICATE_STATE_DONT_RENDER;
759bf215546Sopenharmony_ci}
760bf215546Sopenharmony_ci
761bf215546Sopenharmony_cistatic void
762bf215546Sopenharmony_ciset_predicate_for_result(struct iris_context *ice,
763bf215546Sopenharmony_ci                         struct iris_query *q,
764bf215546Sopenharmony_ci                         bool inverted)
765bf215546Sopenharmony_ci{
766bf215546Sopenharmony_ci   struct iris_batch *batch = &ice->batches[IRIS_BATCH_RENDER];
767bf215546Sopenharmony_ci   struct iris_bo *bo = iris_resource_bo(q->query_state_ref.res);
768bf215546Sopenharmony_ci
769bf215546Sopenharmony_ci   iris_batch_sync_region_start(batch);
770bf215546Sopenharmony_ci
771bf215546Sopenharmony_ci   /* The CPU doesn't have the query result yet; use hardware predication */
772bf215546Sopenharmony_ci   ice->state.predicate = IRIS_PREDICATE_STATE_USE_BIT;
773bf215546Sopenharmony_ci
774bf215546Sopenharmony_ci   /* Ensure the memory is coherent for MI_LOAD_REGISTER_* commands. */
775bf215546Sopenharmony_ci   iris_emit_pipe_control_flush(batch,
776bf215546Sopenharmony_ci                                "conditional rendering: set predicate",
777bf215546Sopenharmony_ci                                PIPE_CONTROL_FLUSH_ENABLE);
778bf215546Sopenharmony_ci   q->stalled = true;
779bf215546Sopenharmony_ci
780bf215546Sopenharmony_ci   struct mi_builder b;
781bf215546Sopenharmony_ci   mi_builder_init(&b, &batch->screen->devinfo, batch);
782bf215546Sopenharmony_ci
783bf215546Sopenharmony_ci   struct mi_value result;
784bf215546Sopenharmony_ci
785bf215546Sopenharmony_ci   switch (q->type) {
786bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
787bf215546Sopenharmony_ci      result = calc_overflow_for_stream(&b, q, q->index);
788bf215546Sopenharmony_ci      break;
789bf215546Sopenharmony_ci   case PIPE_QUERY_SO_OVERFLOW_ANY_PREDICATE:
790bf215546Sopenharmony_ci      result = calc_overflow_any_stream(&b, q);
791bf215546Sopenharmony_ci      break;
792bf215546Sopenharmony_ci   default: {
793bf215546Sopenharmony_ci      /* PIPE_QUERY_OCCLUSION_* */
794bf215546Sopenharmony_ci      struct mi_value start =
795bf215546Sopenharmony_ci         query_mem64(q, offsetof(struct iris_query_snapshots, start));
796bf215546Sopenharmony_ci      struct mi_value end =
797bf215546Sopenharmony_ci         query_mem64(q, offsetof(struct iris_query_snapshots, end));
798bf215546Sopenharmony_ci      result = mi_isub(&b, end, start);
799bf215546Sopenharmony_ci      break;
800bf215546Sopenharmony_ci   }
801bf215546Sopenharmony_ci   }
802bf215546Sopenharmony_ci
803bf215546Sopenharmony_ci   result = inverted ? mi_z(&b, result) : mi_nz(&b, result);
804bf215546Sopenharmony_ci   result = mi_iand(&b, result, mi_imm(1));
805bf215546Sopenharmony_ci
806bf215546Sopenharmony_ci   /* We immediately set the predicate on the render batch, as all the
807bf215546Sopenharmony_ci    * counters come from 3D operations.  However, we may need to predicate
808bf215546Sopenharmony_ci    * a compute dispatch, which executes in a different GEM context and has
809bf215546Sopenharmony_ci    * a different MI_PREDICATE_RESULT register.  So, we save the result to
810bf215546Sopenharmony_ci    * memory and reload it in iris_launch_grid.
811bf215546Sopenharmony_ci    */
812bf215546Sopenharmony_ci   mi_value_ref(&b, result);
813bf215546Sopenharmony_ci   mi_store(&b, mi_reg32(MI_PREDICATE_RESULT), result);
814bf215546Sopenharmony_ci   mi_store(&b, query_mem64(q, offsetof(struct iris_query_snapshots,
815bf215546Sopenharmony_ci                                        predicate_result)), result);
816bf215546Sopenharmony_ci   ice->state.compute_predicate = bo;
817bf215546Sopenharmony_ci
818bf215546Sopenharmony_ci   iris_batch_sync_region_end(batch);
819bf215546Sopenharmony_ci}
820bf215546Sopenharmony_ci
821bf215546Sopenharmony_cistatic void
822bf215546Sopenharmony_ciiris_render_condition(struct pipe_context *ctx,
823bf215546Sopenharmony_ci                      struct pipe_query *query,
824bf215546Sopenharmony_ci                      bool condition,
825bf215546Sopenharmony_ci                      enum pipe_render_cond_flag mode)
826bf215546Sopenharmony_ci{
827bf215546Sopenharmony_ci   struct iris_context *ice = (void *) ctx;
828bf215546Sopenharmony_ci   struct iris_query *q = (void *) query;
829bf215546Sopenharmony_ci
830bf215546Sopenharmony_ci   /* The old condition isn't relevant; we'll update it if necessary */
831bf215546Sopenharmony_ci   ice->state.compute_predicate = NULL;
832bf215546Sopenharmony_ci
833bf215546Sopenharmony_ci   if (!q) {
834bf215546Sopenharmony_ci      ice->state.predicate = IRIS_PREDICATE_STATE_RENDER;
835bf215546Sopenharmony_ci      return;
836bf215546Sopenharmony_ci   }
837bf215546Sopenharmony_ci
838bf215546Sopenharmony_ci   iris_check_query_no_flush(ice, q);
839bf215546Sopenharmony_ci
840bf215546Sopenharmony_ci   if (q->result || q->ready) {
841bf215546Sopenharmony_ci      set_predicate_enable(ice, (q->result != 0) ^ condition);
842bf215546Sopenharmony_ci   } else {
843bf215546Sopenharmony_ci      if (mode == PIPE_RENDER_COND_NO_WAIT ||
844bf215546Sopenharmony_ci          mode == PIPE_RENDER_COND_BY_REGION_NO_WAIT) {
845bf215546Sopenharmony_ci         perf_debug(&ice->dbg, "Conditional rendering demoted from "
846bf215546Sopenharmony_ci                    "\"no wait\" to \"wait\".");
847bf215546Sopenharmony_ci      }
848bf215546Sopenharmony_ci      set_predicate_for_result(ice, q, condition);
849bf215546Sopenharmony_ci   }
850bf215546Sopenharmony_ci}
851bf215546Sopenharmony_ci
852bf215546Sopenharmony_civoid
853bf215546Sopenharmony_cigenX(init_query)(struct iris_context *ice)
854bf215546Sopenharmony_ci{
855bf215546Sopenharmony_ci   struct pipe_context *ctx = &ice->ctx;
856bf215546Sopenharmony_ci
857bf215546Sopenharmony_ci   ctx->create_query = iris_create_query;
858bf215546Sopenharmony_ci   ctx->create_batch_query = iris_create_batch_query;
859bf215546Sopenharmony_ci   ctx->destroy_query = iris_destroy_query;
860bf215546Sopenharmony_ci   ctx->begin_query = iris_begin_query;
861bf215546Sopenharmony_ci   ctx->end_query = iris_end_query;
862bf215546Sopenharmony_ci   ctx->get_query_result = iris_get_query_result;
863bf215546Sopenharmony_ci   ctx->get_query_result_resource = iris_get_query_result_resource;
864bf215546Sopenharmony_ci   ctx->set_active_query_state = iris_set_active_query_state;
865bf215546Sopenharmony_ci   ctx->render_condition = iris_render_condition;
866bf215546Sopenharmony_ci}
867