etnaviv: add basic infrastructure for hw queries
[mesa.git] / src / gallium / drivers / etnaviv / etnaviv_query_hw.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_query_hw.h"
34 #include "etnaviv_screen.h"
35
36 static void
37 etna_hw_destroy_query(struct etna_context *ctx, struct etna_query *q)
38 {
39 struct etna_hw_query *hq = etna_hw_query(q);
40
41 pipe_resource_reference(&hq->prsc, NULL);
42 list_del(&hq->node);
43
44 FREE(hq);
45 }
46
47 static void
48 realloc_query_bo(struct etna_context *ctx, struct etna_hw_query *hq)
49 {
50 struct etna_resource *rsc;
51 void *map;
52
53 pipe_resource_reference(&hq->prsc, NULL);
54
55 /* allocate resource with space for 64 * 64bit values */
56 hq->prsc = pipe_buffer_create(&ctx->screen->base, PIPE_BIND_QUERY_BUFFER,
57 0, 0x1000);
58
59 /* don't assume the buffer is zero-initialized */
60 rsc = etna_resource(hq->prsc);
61
62 etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_WRITE);
63
64 map = etna_bo_map(rsc->bo);
65 memset(map, 0, 0x1000);
66 etna_bo_cpu_fini(rsc->bo);
67 }
68
69 static boolean
70 etna_hw_begin_query(struct etna_context *ctx, struct etna_query *q)
71 {
72 struct etna_hw_query *hq = etna_hw_query(q);
73 const struct etna_hw_sample_provider *p = hq->provider;
74
75 /* ->begin_query() discards previous results, so realloc bo */
76 realloc_query_bo(ctx, hq);
77
78 p->start(hq, ctx);
79
80 /* add to active list */
81 assert(list_empty(&hq->node));
82 list_addtail(&hq->node, &ctx->active_hw_queries);
83
84 return true;
85 }
86
87 static void
88 etna_hw_end_query(struct etna_context *ctx, struct etna_query *q)
89 {
90 struct etna_hw_query *hq = etna_hw_query(q);
91 const struct etna_hw_sample_provider *p = hq->provider;
92
93 p->stop(hq, ctx);
94
95 /* remove from active list */
96 list_delinit(&hq->node);
97 }
98
99 static boolean
100 etna_hw_get_query_result(struct etna_context *ctx, struct etna_query *q,
101 boolean wait, union pipe_query_result *result)
102 {
103 struct etna_hw_query *hq = etna_hw_query(q);
104 struct etna_resource *rsc = etna_resource(hq->prsc);
105 const struct etna_hw_sample_provider *p = hq->provider;
106
107 assert(LIST_IS_EMPTY(&hq->node));
108
109 if (!wait) {
110 int ret;
111
112 if (rsc->status & ETNA_PENDING_WRITE) {
113 /* piglit spec@arb_occlusion_query@occlusion_query_conform
114 * test, and silly apps perhaps, get stuck in a loop trying
115 * to get query result forever with wait==false.. we don't
116 * wait to flush unnecessarily but we also don't want to
117 * spin forever.
118 */
119 if (hq->no_wait_cnt++ > 5)
120 ctx->base.flush(&ctx->base, NULL, 0);
121 return false;
122 }
123
124 ret = etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_READ | DRM_ETNA_PREP_NOSYNC);
125 if (ret)
126 return false;
127
128 etna_bo_cpu_fini(rsc->bo);
129 }
130
131 /* flush that GPU executes all query related actions */
132 ctx->base.flush(&ctx->base, NULL, 0);
133
134 /* get the result */
135 etna_bo_cpu_prep(rsc->bo, DRM_ETNA_PREP_READ);
136
137 void *ptr = etna_bo_map(rsc->bo);
138 p->result(hq, ptr, result);
139
140 etna_bo_cpu_fini(rsc->bo);
141
142 return true;
143 }
144
145 static const struct etna_query_funcs hw_query_funcs = {
146 .destroy_query = etna_hw_destroy_query,
147 .begin_query = etna_hw_begin_query,
148 .end_query = etna_hw_end_query,
149 .get_query_result = etna_hw_get_query_result,
150 };
151
152 static inline const struct etna_hw_sample_provider *
153 query_sample_provider(unsigned query_type)
154 {
155 switch (query_type) {
156 default:
157 return NULL;
158 }
159 }
160
161 struct etna_query *
162 etna_hw_create_query(struct etna_context *ctx, unsigned query_type)
163 {
164 struct etna_hw_query *hq;
165 struct etna_query *q;
166 const struct etna_hw_sample_provider *p;
167
168 p = query_sample_provider(query_type);
169 if (!p)
170 return NULL;
171
172 hq = CALLOC_STRUCT(etna_hw_query);
173 if (!hq)
174 return NULL;
175
176 hq->provider = p;
177
178 list_inithead(&hq->node);
179
180 q = &hq->base;
181 q->funcs = &hw_query_funcs;
182 q->type = query_type;
183
184 return q;
185 }