1bf215546Sopenharmony_ci/*
2bf215546Sopenharmony_ci * Copyright 2011 Nouveau Project
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 in
12bf215546Sopenharmony_ci * all copies or substantial portions of the Software.
13bf215546Sopenharmony_ci *
14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15bf215546Sopenharmony_ci * 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
18bf215546Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19bf215546Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20bf215546Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
21bf215546Sopenharmony_ci *
22bf215546Sopenharmony_ci * Authors: Christoph Bumiller
23bf215546Sopenharmony_ci */
24bf215546Sopenharmony_ci
25bf215546Sopenharmony_ci#define NV50_PUSH_EXPLICIT_SPACE_CHECKING
26bf215546Sopenharmony_ci
27bf215546Sopenharmony_ci#include "nv50/nv50_context.h"
28bf215546Sopenharmony_ci#include "nv50/nv50_query.h"
29bf215546Sopenharmony_ci#include "nv50/nv50_query_hw.h"
30bf215546Sopenharmony_ci#include "nv50/nv50_query_hw_metric.h"
31bf215546Sopenharmony_ci#include "nv50/nv50_query_hw_sm.h"
32bf215546Sopenharmony_ci
33bf215546Sopenharmony_cistatic struct pipe_query *
34bf215546Sopenharmony_cinv50_create_query(struct pipe_context *pipe, unsigned type, unsigned index)
35bf215546Sopenharmony_ci{
36bf215546Sopenharmony_ci   struct nv50_context *nv50 = nv50_context(pipe);
37bf215546Sopenharmony_ci   struct nv50_query *q;
38bf215546Sopenharmony_ci
39bf215546Sopenharmony_ci   q = nv50_hw_create_query(nv50, type, index);
40bf215546Sopenharmony_ci   return (struct pipe_query *)q;
41bf215546Sopenharmony_ci}
42bf215546Sopenharmony_ci
43bf215546Sopenharmony_cistatic void
44bf215546Sopenharmony_cinv50_destroy_query(struct pipe_context *pipe, struct pipe_query *pq)
45bf215546Sopenharmony_ci{
46bf215546Sopenharmony_ci   struct nv50_query *q = nv50_query(pq);
47bf215546Sopenharmony_ci   q->funcs->destroy_query(nv50_context(pipe), q);
48bf215546Sopenharmony_ci}
49bf215546Sopenharmony_ci
50bf215546Sopenharmony_cistatic bool
51bf215546Sopenharmony_cinv50_begin_query(struct pipe_context *pipe, struct pipe_query *pq)
52bf215546Sopenharmony_ci{
53bf215546Sopenharmony_ci   struct nv50_query *q = nv50_query(pq);
54bf215546Sopenharmony_ci   return q->funcs->begin_query(nv50_context(pipe), q);
55bf215546Sopenharmony_ci}
56bf215546Sopenharmony_ci
57bf215546Sopenharmony_cistatic bool
58bf215546Sopenharmony_cinv50_end_query(struct pipe_context *pipe, struct pipe_query *pq)
59bf215546Sopenharmony_ci{
60bf215546Sopenharmony_ci   struct nv50_query *q = nv50_query(pq);
61bf215546Sopenharmony_ci   q->funcs->end_query(nv50_context(pipe), q);
62bf215546Sopenharmony_ci   return true;
63bf215546Sopenharmony_ci}
64bf215546Sopenharmony_ci
65bf215546Sopenharmony_cistatic bool
66bf215546Sopenharmony_cinv50_get_query_result(struct pipe_context *pipe, struct pipe_query *pq,
67bf215546Sopenharmony_ci                      bool wait, union pipe_query_result *result)
68bf215546Sopenharmony_ci{
69bf215546Sopenharmony_ci   struct nv50_query *q = nv50_query(pq);
70bf215546Sopenharmony_ci   return q->funcs->get_query_result(nv50_context(pipe), q, wait, result);
71bf215546Sopenharmony_ci}
72bf215546Sopenharmony_ci
73bf215546Sopenharmony_cistatic void
74bf215546Sopenharmony_cinv50_render_condition(struct pipe_context *pipe,
75bf215546Sopenharmony_ci                      struct pipe_query *pq,
76bf215546Sopenharmony_ci                      bool condition, enum pipe_render_cond_flag mode)
77bf215546Sopenharmony_ci{
78bf215546Sopenharmony_ci   struct nv50_context *nv50 = nv50_context(pipe);
79bf215546Sopenharmony_ci   struct nouveau_pushbuf *push = nv50->base.pushbuf;
80bf215546Sopenharmony_ci   struct nv50_query *q = nv50_query(pq);
81bf215546Sopenharmony_ci   struct nv50_hw_query *hq = nv50_hw_query(q);
82bf215546Sopenharmony_ci   uint32_t cond;
83bf215546Sopenharmony_ci   bool wait =
84bf215546Sopenharmony_ci      mode != PIPE_RENDER_COND_NO_WAIT &&
85bf215546Sopenharmony_ci      mode != PIPE_RENDER_COND_BY_REGION_NO_WAIT;
86bf215546Sopenharmony_ci
87bf215546Sopenharmony_ci   if (!pq) {
88bf215546Sopenharmony_ci      cond = NV50_3D_COND_MODE_ALWAYS;
89bf215546Sopenharmony_ci   }
90bf215546Sopenharmony_ci   else {
91bf215546Sopenharmony_ci      /* NOTE: comparison of 2 queries only works if both have completed */
92bf215546Sopenharmony_ci      switch (q->type) {
93bf215546Sopenharmony_ci      case PIPE_QUERY_SO_OVERFLOW_PREDICATE:
94bf215546Sopenharmony_ci         cond = condition ? NV50_3D_COND_MODE_EQUAL :
95bf215546Sopenharmony_ci                            NV50_3D_COND_MODE_NOT_EQUAL;
96bf215546Sopenharmony_ci         wait = true;
97bf215546Sopenharmony_ci         break;
98bf215546Sopenharmony_ci      case PIPE_QUERY_OCCLUSION_COUNTER:
99bf215546Sopenharmony_ci      case PIPE_QUERY_OCCLUSION_PREDICATE:
100bf215546Sopenharmony_ci      case PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE:
101bf215546Sopenharmony_ci         if (hq->state == NV50_HW_QUERY_STATE_READY)
102bf215546Sopenharmony_ci            wait = true;
103bf215546Sopenharmony_ci         if (likely(!condition)) {
104bf215546Sopenharmony_ci            cond = wait ? NV50_3D_COND_MODE_NOT_EQUAL : NV50_3D_COND_MODE_ALWAYS;
105bf215546Sopenharmony_ci         } else {
106bf215546Sopenharmony_ci            cond = wait ? NV50_3D_COND_MODE_EQUAL : NV50_3D_COND_MODE_ALWAYS;
107bf215546Sopenharmony_ci         }
108bf215546Sopenharmony_ci         break;
109bf215546Sopenharmony_ci      default:
110bf215546Sopenharmony_ci         assert(!"render condition query not a predicate");
111bf215546Sopenharmony_ci         cond = NV50_3D_COND_MODE_ALWAYS;
112bf215546Sopenharmony_ci         break;
113bf215546Sopenharmony_ci      }
114bf215546Sopenharmony_ci   }
115bf215546Sopenharmony_ci
116bf215546Sopenharmony_ci   nv50->cond_query = pq;
117bf215546Sopenharmony_ci   nv50->cond_cond = condition;
118bf215546Sopenharmony_ci   nv50->cond_condmode = cond;
119bf215546Sopenharmony_ci   nv50->cond_mode = mode;
120bf215546Sopenharmony_ci
121bf215546Sopenharmony_ci   if (!pq) {
122bf215546Sopenharmony_ci      PUSH_SPACE(push, 2);
123bf215546Sopenharmony_ci      BEGIN_NV04(push, NV50_3D(COND_MODE), 1);
124bf215546Sopenharmony_ci      PUSH_DATA (push, cond);
125bf215546Sopenharmony_ci      return;
126bf215546Sopenharmony_ci   }
127bf215546Sopenharmony_ci
128bf215546Sopenharmony_ci   PUSH_SPACE(push, 9);
129bf215546Sopenharmony_ci
130bf215546Sopenharmony_ci   if (wait && hq->state != NV50_HW_QUERY_STATE_READY) {
131bf215546Sopenharmony_ci      BEGIN_NV04(push, SUBC_3D(NV50_GRAPH_SERIALIZE), 1);
132bf215546Sopenharmony_ci      PUSH_DATA (push, 0);
133bf215546Sopenharmony_ci   }
134bf215546Sopenharmony_ci
135bf215546Sopenharmony_ci   PUSH_REFN (push, hq->bo, NOUVEAU_BO_GART | NOUVEAU_BO_RD);
136bf215546Sopenharmony_ci   BEGIN_NV04(push, NV50_3D(COND_ADDRESS_HIGH), 3);
137bf215546Sopenharmony_ci   PUSH_DATAh(push, hq->bo->offset + hq->offset);
138bf215546Sopenharmony_ci   PUSH_DATA (push, hq->bo->offset + hq->offset);
139bf215546Sopenharmony_ci   PUSH_DATA (push, cond);
140bf215546Sopenharmony_ci
141bf215546Sopenharmony_ci   BEGIN_NV04(push, NV50_2D(COND_ADDRESS_HIGH), 2);
142bf215546Sopenharmony_ci   PUSH_DATAh(push, hq->bo->offset + hq->offset);
143bf215546Sopenharmony_ci   PUSH_DATA (push, hq->bo->offset + hq->offset);
144bf215546Sopenharmony_ci}
145bf215546Sopenharmony_ci
146bf215546Sopenharmony_cistatic void
147bf215546Sopenharmony_cinv50_set_active_query_state(struct pipe_context *pipe, bool enable)
148bf215546Sopenharmony_ci{
149bf215546Sopenharmony_ci}
150bf215546Sopenharmony_ci
151bf215546Sopenharmony_civoid
152bf215546Sopenharmony_cinv50_init_query_functions(struct nv50_context *nv50)
153bf215546Sopenharmony_ci{
154bf215546Sopenharmony_ci   struct pipe_context *pipe = &nv50->base.pipe;
155bf215546Sopenharmony_ci
156bf215546Sopenharmony_ci   pipe->create_query = nv50_create_query;
157bf215546Sopenharmony_ci   pipe->destroy_query = nv50_destroy_query;
158bf215546Sopenharmony_ci   pipe->begin_query = nv50_begin_query;
159bf215546Sopenharmony_ci   pipe->end_query = nv50_end_query;
160bf215546Sopenharmony_ci   pipe->get_query_result = nv50_get_query_result;
161bf215546Sopenharmony_ci   pipe->set_active_query_state = nv50_set_active_query_state;
162bf215546Sopenharmony_ci   pipe->render_condition = nv50_render_condition;
163bf215546Sopenharmony_ci   nv50->cond_condmode = NV50_3D_COND_MODE_ALWAYS;
164bf215546Sopenharmony_ci}
165bf215546Sopenharmony_ci
166bf215546Sopenharmony_ciint
167bf215546Sopenharmony_cinv50_screen_get_driver_query_info(struct pipe_screen *pscreen,
168bf215546Sopenharmony_ci                                  unsigned id,
169bf215546Sopenharmony_ci                                  struct pipe_driver_query_info *info)
170bf215546Sopenharmony_ci{
171bf215546Sopenharmony_ci   struct nv50_screen *screen = nv50_screen(pscreen);
172bf215546Sopenharmony_ci   int num_hw_queries = 0;
173bf215546Sopenharmony_ci
174bf215546Sopenharmony_ci   num_hw_queries = nv50_hw_get_driver_query_info(screen, 0, NULL);
175bf215546Sopenharmony_ci
176bf215546Sopenharmony_ci   if (!info)
177bf215546Sopenharmony_ci      return num_hw_queries;
178bf215546Sopenharmony_ci
179bf215546Sopenharmony_ci   /* Init default values. */
180bf215546Sopenharmony_ci   info->name = "this_is_not_the_query_you_are_looking_for";
181bf215546Sopenharmony_ci   info->query_type = 0xdeadd01d;
182bf215546Sopenharmony_ci   info->max_value.u64 = 0;
183bf215546Sopenharmony_ci   info->type = PIPE_DRIVER_QUERY_TYPE_UINT64;
184bf215546Sopenharmony_ci   info->group_id = -1;
185bf215546Sopenharmony_ci   info->flags = 0;
186bf215546Sopenharmony_ci
187bf215546Sopenharmony_ci   return nv50_hw_get_driver_query_info(screen, id, info);
188bf215546Sopenharmony_ci}
189bf215546Sopenharmony_ci
190bf215546Sopenharmony_ciint
191bf215546Sopenharmony_cinv50_screen_get_driver_query_group_info(struct pipe_screen *pscreen,
192bf215546Sopenharmony_ci                                        unsigned id,
193bf215546Sopenharmony_ci                                        struct pipe_driver_query_group_info *info)
194bf215546Sopenharmony_ci{
195bf215546Sopenharmony_ci   struct nv50_screen *screen = nv50_screen(pscreen);
196bf215546Sopenharmony_ci   int count = 0;
197bf215546Sopenharmony_ci
198bf215546Sopenharmony_ci   if (screen->compute)
199bf215546Sopenharmony_ci      if (screen->base.class_3d >= NV84_3D_CLASS)
200bf215546Sopenharmony_ci         count += 2;
201bf215546Sopenharmony_ci
202bf215546Sopenharmony_ci   if (!info)
203bf215546Sopenharmony_ci      return count;
204bf215546Sopenharmony_ci
205bf215546Sopenharmony_ci   if (id == NV50_HW_SM_QUERY_GROUP) {
206bf215546Sopenharmony_ci      if (screen->compute) {
207bf215546Sopenharmony_ci         if (screen->base.class_3d >= NV84_3D_CLASS) {
208bf215546Sopenharmony_ci            info->name = "MP counters";
209bf215546Sopenharmony_ci
210bf215546Sopenharmony_ci            /* Expose the maximum number of hardware counters available,
211bf215546Sopenharmony_ci             * although some queries use more than one counter. Expect failures
212bf215546Sopenharmony_ci             * in that case but as performance counters are for developers,
213bf215546Sopenharmony_ci             * this should not have a real impact. */
214bf215546Sopenharmony_ci            info->max_active_queries = 4;
215bf215546Sopenharmony_ci            info->num_queries = NV50_HW_SM_QUERY_COUNT;
216bf215546Sopenharmony_ci            return 1;
217bf215546Sopenharmony_ci         }
218bf215546Sopenharmony_ci      }
219bf215546Sopenharmony_ci   } else
220bf215546Sopenharmony_ci   if (id == NV50_HW_METRIC_QUERY_GROUP) {
221bf215546Sopenharmony_ci      if (screen->compute) {
222bf215546Sopenharmony_ci         if (screen->base.class_3d >= NV84_3D_CLASS) {
223bf215546Sopenharmony_ci            info->name = "Performance metrics";
224bf215546Sopenharmony_ci            info->max_active_queries = 2; /* A metric uses at least 2 queries */
225bf215546Sopenharmony_ci            info->num_queries = NV50_HW_METRIC_QUERY_COUNT;
226bf215546Sopenharmony_ci            return 1;
227bf215546Sopenharmony_ci         }
228bf215546Sopenharmony_ci      }
229bf215546Sopenharmony_ci   }
230bf215546Sopenharmony_ci
231bf215546Sopenharmony_ci   /* user asked for info about non-existing query group */
232bf215546Sopenharmony_ci   info->name = "this_is_not_the_query_group_you_are_looking_for";
233bf215546Sopenharmony_ci   info->max_active_queries = 0;
234bf215546Sopenharmony_ci   info->num_queries = 0;
235bf215546Sopenharmony_ci   return 0;
236bf215546Sopenharmony_ci}
237