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
etna_acc_destroy_query(struct etna_context * ctx,struct etna_query * q)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
realloc_query_bo(struct etna_context * ctx,struct etna_acc_query * aq)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 aq->prsc = pipe_buffer_create(&ctx->screen->base, PIPE_BIND_QUERY_BUFFER,
68 0, 0x1000);
69
70 /* don't assume the buffer is zero-initialized */
71 rsc = etna_resource(aq->prsc);
72
73 etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE);
74
75 map = etna_bo_map(rsc->bo);
76 memset(map, 0, 0x1000);
77 etna_bo_cpu_fini(rsc->bo);
78 }
79
80 static void
etna_acc_begin_query(struct etna_context * ctx,struct etna_query * q)81 etna_acc_begin_query(struct etna_context *ctx, struct etna_query *q)
82 {
83 struct etna_acc_query *aq = etna_acc_query(q);
84 const struct etna_acc_sample_provider *p = aq->provider;
85
86 /* ->begin_query() discards previous results, so realloc bo */
87 realloc_query_bo(ctx, aq);
88 aq->samples = 0;
89
90 p->resume(aq, ctx);
91
92 /* add to active list */
93 assert(list_is_empty(&aq->node));
94 list_addtail(&aq->node, &ctx->active_acc_queries);
95 }
96
97 static void
etna_acc_end_query(struct etna_context * ctx,struct etna_query * q)98 etna_acc_end_query(struct etna_context *ctx, struct etna_query *q)
99 {
100 struct etna_acc_query *aq = etna_acc_query(q);
101 const struct etna_acc_sample_provider *p = aq->provider;
102
103 p->suspend(aq, ctx);
104
105 /* remove from active list */
106 list_delinit(&aq->node);
107 }
108
109 static bool
etna_acc_get_query_result(struct etna_context * ctx,struct etna_query * q,bool wait,union pipe_query_result * result)110 etna_acc_get_query_result(struct etna_context *ctx, struct etna_query *q,
111 bool wait, union pipe_query_result *result)
112 {
113 struct etna_acc_query *aq = etna_acc_query(q);
114 struct etna_resource *rsc = etna_resource(aq->prsc);
115 const struct etna_acc_sample_provider *p = aq->provider;
116
117 assert(list_is_empty(&aq->node));
118
119 if (etna_resource_status(ctx, rsc) & ETNA_PENDING_WRITE) {
120 if (!wait) {
121 /* piglit spec@arb_occlusion_query@occlusion_query_conform
122 * test, and silly apps perhaps, get stuck in a loop trying
123 * to get query result forever with wait==false.. we don't
124 * wait to flush unnecessarily but we also don't want to
125 * spin forever.
126 */
127 if (aq->no_wait_cnt++ > 5) {
128 etna_flush(&ctx->base, NULL, 0, true);
129 aq->no_wait_cnt = 0;
130 }
131
132 return false;
133 } else {
134 /* flush that GPU executes all query related actions */
135 etna_flush(&ctx->base, NULL, 0, true);
136 }
137 }
138
139 /* get the result */
140 int ret = etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_READ);
141 if (ret)
142 return false;
143
144 void *ptr = etna_bo_map(rsc->bo);
145 bool success = p->result(aq, ptr, result);
146
147 etna_bo_cpu_fini(rsc->bo);
148
149 return success;
150 }
151
152 static const struct etna_query_funcs acc_query_funcs = {
153 .destroy_query = etna_acc_destroy_query,
154 .begin_query = etna_acc_begin_query,
155 .end_query = etna_acc_end_query,
156 .get_query_result = etna_acc_get_query_result,
157 };
158
159 struct etna_query *
etna_acc_create_query(struct etna_context * ctx,unsigned query_type)160 etna_acc_create_query(struct etna_context *ctx, unsigned query_type)
161 {
162 const struct etna_acc_sample_provider *p = NULL;
163 struct etna_acc_query *aq;
164 struct etna_query *q;
165
166 /* find a sample provide for the requested query type */
167 for (unsigned i = 0; i < ARRAY_SIZE(acc_sample_provider); i++) {
168 p = acc_sample_provider[i];
169
170 if (p->supports(query_type))
171 break;
172 else
173 p = NULL;
174 }
175
176 if (!p)
177 return NULL;
178
179 aq = p->allocate(ctx, query_type);
180 if (!aq)
181 return NULL;
182
183 aq->provider = p;
184
185 list_inithead(&aq->node);
186
187 q = &aq->base;
188 q->funcs = &acc_query_funcs;
189 q->type = query_type;
190
191 return q;
192 }
193