etnaviv: convert perfmon queries to acc queries
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_query_acc.c
1 /*
2 * Copyright (c) 2017 Etnaviv Project
3 * Copyright (C) 2017 Zodiac Inflight Innovations
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sub license,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
14 * of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Authors:
25 * Rob Clark <robclark@freedesktop.org>
26 * Christian Gmeiner <christian.gmeiner@gmail.com>
27 */
28
29 #include "util/u_inlines.h"
30 #include "util/u_memory.h"
31
32 #include "etnaviv_context.h"
33 #include "etnaviv_debug.h"
34 #include "etnaviv_emit.h"
35 #include "etnaviv_query_acc.h"
36 #include "etnaviv_screen.h"
37
38
39 extern const struct etna_acc_sample_provider occlusion_provider;
40 extern const struct etna_acc_sample_provider perfmon_provider;
41
42 static const struct etna_acc_sample_provider *acc_sample_provider[] =
43 {
44 &occlusion_provider,
45 &perfmon_provider,
46 };
47
48 static void
49 etna_acc_destroy_query(struct etna_context *ctx, struct etna_query *q)
50 {
51 struct etna_acc_query *aq = etna_acc_query(q);
52
53 pipe_resource_reference(&aq->prsc, NULL);
54 list_del(&aq->node);
55
56 FREE(aq);
57 }
58
59 static void
60 realloc_query_bo(struct etna_context *ctx, struct etna_acc_query *aq)
61 {
62 struct etna_resource *rsc;
63 void *map;
64
65 pipe_resource_reference(&aq->prsc, NULL);
66
67 /* allocate resource with space for 64 * 64bit values */
68 aq->prsc = pipe_buffer_create(&ctx->screen->base, PIPE_BIND_QUERY_BUFFER,
69 0, 0x1000);
70
71 /* don't assume the buffer is zero-initialized */
72 rsc = etna_resource(aq->prsc);
73
74 etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE);
75
76 map = etna_bo_map(rsc->bo);
77 memset(map, 0, 0x1000);
78 etna_bo_cpu_fini(rsc->bo);
79 }
80
81 static bool
82 etna_acc_begin_query(struct etna_context *ctx, struct etna_query *q)
83 {
84 struct etna_acc_query *aq = etna_acc_query(q);
85 const struct etna_acc_sample_provider *p = aq->provider;
86
87 /* ->begin_query() discards previous results, so realloc bo */
88 realloc_query_bo(ctx, aq);
89
90 p->resume(aq, ctx);
91 aq->samples++;
92
93 /* add to active list */
94 assert(list_is_empty(&aq->node));
95 list_addtail(&aq->node, &ctx->active_acc_queries);
96
97 return true;
98 }
99
100 static void
101 etna_acc_end_query(struct etna_context *ctx, struct etna_query *q)
102 {
103 struct etna_acc_query *aq = etna_acc_query(q);
104 const struct etna_acc_sample_provider *p = aq->provider;
105
106 p->suspend(aq, ctx);
107 aq->samples++;
108
109 /* remove from active list */
110 list_delinit(&aq->node);
111 }
112
113 static bool
114 etna_acc_get_query_result(struct etna_context *ctx, struct etna_query *q,
115 bool wait, union pipe_query_result *result)
116 {
117 struct etna_acc_query *aq = etna_acc_query(q);
118 struct etna_resource *rsc = etna_resource(aq->prsc);
119 const struct etna_acc_sample_provider *p = aq->provider;
120
121 assert(list_is_empty(&aq->node));
122
123 if (rsc->status & ETNA_PENDING_WRITE) {
124 if (!wait) {
125 /* piglit spec@arb_occlusion_query@occlusion_query_conform
126 * test, and silly apps perhaps, get stuck in a loop trying
127 * to get query result forever with wait==false.. we don't
128 * wait to flush unnecessarily but we also don't want to
129 * spin forever.
130 */
131 if (aq->no_wait_cnt++ > 5) {
132 ctx->base.flush(&ctx->base, NULL, 0);
133 aq->no_wait_cnt = 0;
134 }
135
136 return false;
137 } else {
138 /* flush that GPU executes all query related actions */
139 ctx->base.flush(&ctx->base, NULL, 0);
140 }
141 }
142
143 /* get the result */
144 int ret = etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_READ);
145 if (ret)
146 return false;
147
148 void *ptr = etna_bo_map(rsc->bo);
149 bool success = p->result(aq, ptr, result);
150
151 if (success)
152 aq->samples = 0;
153
154 etna_bo_cpu_fini(rsc->bo);
155
156 return success;
157 }
158
159 static const struct etna_query_funcs acc_query_funcs = {
160 .destroy_query = etna_acc_destroy_query,
161 .begin_query = etna_acc_begin_query,
162 .end_query = etna_acc_end_query,
163 .get_query_result = etna_acc_get_query_result,
164 };
165
166 struct etna_query *
167 etna_acc_create_query(struct etna_context *ctx, unsigned query_type)
168 {
169 const struct etna_acc_sample_provider *p = NULL;
170 struct etna_acc_query *aq;
171 struct etna_query *q;
172
173 /* find a sample provide for the requested query type */
174 for (unsigned i = 0; i < ARRAY_SIZE(acc_sample_provider); i++) {
175 p = acc_sample_provider[i];
176
177 if (p->supports(query_type))
178 break;
179 else
180 p = NULL;
181 }
182
183 if (!p)
184 return NULL;
185
186 aq = p->allocate(ctx, query_type);
187 if (!aq)
188 return NULL;
189
190 aq->provider = p;
191
192 list_inithead(&aq->node);
193
194 q = &aq->base;
195 q->funcs = &acc_query_funcs;
196 q->type = query_type;
197
198 return q;
199 }