xref: /aosp_15_r20/external/mesa3d/src/etnaviv/drm/etnaviv_cmd_stream.c (revision 6104692788411f58d303aa86923a9ff6ecaded22)
1 /*
2  * Copyright (C) 2014-2015 Etnaviv Project
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Christian Gmeiner <[email protected]>
25  */
26 
27 #include <assert.h>
28 #include <stdlib.h>
29 
30 #include "util/hash_table.h"
31 #include "util/u_math.h"
32 
33 #include "etnaviv_drmif.h"
34 #include "etnaviv_priv.h"
35 
grow(void * ptr,uint32_t nr,uint32_t * max,uint32_t sz)36 static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
37 {
38 	if ((nr + 1) > *max) {
39 		if ((*max * 2) < (nr + 1))
40 			*max = nr + 5;
41 		else
42 			*max = *max * 2;
43 		ptr = realloc(ptr, *max * sz);
44 	}
45 
46 	return ptr;
47 }
48 
49 #define APPEND(x, name) ({ \
50 	(x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
51 	(x)->nr_ ## name ++; \
52 })
53 
etna_cmd_stream_realloc(struct etna_cmd_stream * stream,size_t n)54 void etna_cmd_stream_realloc(struct etna_cmd_stream *stream, size_t n)
55 {
56 	size_t size;
57 	void *buffer;
58 
59 	/*
60 	 * Increase the command buffer size by 4 kiB. Here we pick 4 kiB
61 	 * increment to prevent it from growing too much too quickly.
62 	 */
63 	size = align_uintptr(stream->size + n, 1024);
64 
65 	/* Command buffer is too big for older kernel versions */
66 	if (size > 0x4000)
67 		goto error;
68 
69 	buffer = realloc(stream->buffer, size * 4);
70 	if (!buffer)
71 		goto error;
72 
73 	stream->buffer = buffer;
74 	stream->size = size;
75 
76 	return;
77 
78 error:
79 	DEBUG_MSG("command buffer too long, forcing flush.");
80 	etna_cmd_stream_force_flush(stream);
81 }
82 
83 static inline struct etna_cmd_stream_priv *
etna_cmd_stream_priv(struct etna_cmd_stream * stream)84 etna_cmd_stream_priv(struct etna_cmd_stream *stream)
85 {
86     return (struct etna_cmd_stream_priv *)stream;
87 }
88 
etna_cmd_stream_new(struct etna_pipe * pipe,uint32_t size,void (* force_flush)(struct etna_cmd_stream * stream,void * priv),void * priv)89 struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe,
90         uint32_t size,
91 		void (*force_flush)(struct etna_cmd_stream *stream, void *priv),
92 		void *priv)
93 {
94 	struct etna_cmd_stream_priv *stream = NULL;
95 
96 	if (size == 0) {
97 		ERROR_MSG("invalid size of 0");
98 		goto fail;
99 	}
100 
101 	stream = calloc(1, sizeof(*stream));
102 	if (!stream) {
103 		ERROR_MSG("allocation failed");
104 		goto fail;
105 	}
106 
107 	/* allocate even number of 32-bit words */
108 	size = ALIGN(size, 2);
109 
110 	stream->base.buffer = malloc(size * sizeof(uint32_t));
111 	if (!stream->base.buffer) {
112 		ERROR_MSG("allocation failed");
113 		goto fail;
114 	}
115 
116 	stream->base.size = size;
117 	stream->pipe = pipe;
118 	stream->force_flush = force_flush;
119 	stream->force_flush_priv = priv;
120 
121 	stream->bo_table = _mesa_pointer_hash_table_create(NULL);
122 
123 	return &stream->base;
124 
125 fail:
126 	if (stream)
127 		etna_cmd_stream_del(&stream->base);
128 
129 	return NULL;
130 }
131 
etna_cmd_stream_del(struct etna_cmd_stream * stream)132 void etna_cmd_stream_del(struct etna_cmd_stream *stream)
133 {
134 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
135 
136 	_mesa_hash_table_destroy(priv->bo_table, NULL);
137 
138 	free(stream->buffer);
139 	free(priv->bos);
140 	free(priv->submit.bos);
141 	free(priv->submit.relocs);
142 	free(priv->submit.pmrs);
143 	free(priv);
144 }
145 
etna_cmd_stream_force_flush(struct etna_cmd_stream * stream)146 void etna_cmd_stream_force_flush(struct etna_cmd_stream *stream)
147 {
148 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
149 
150 	if (priv->force_flush)
151 		priv->force_flush(stream, priv->force_flush_priv);
152 }
153 
etna_cmd_stream_timestamp(struct etna_cmd_stream * stream)154 uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
155 {
156 	return etna_cmd_stream_priv(stream)->last_timestamp;
157 }
158 
append_bo(struct etna_cmd_stream * stream,struct etna_bo * bo)159 static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
160 {
161 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
162 	uint32_t idx;
163 
164 	idx = APPEND(&priv->submit, bos);
165 	idx = APPEND(priv, bos);
166 
167 	priv->submit.bos[idx].flags = 0;
168 	priv->submit.bos[idx].handle = bo->handle;
169 	priv->submit.bos[idx].presumed = bo->va;
170 
171 	priv->bos[idx] = etna_bo_ref(bo);
172 
173 	return idx;
174 }
175 
176 /* add (if needed) bo, return idx: */
bo2idx(struct etna_cmd_stream * stream,struct etna_bo * bo,uint32_t flags)177 static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
178 		uint32_t flags)
179 {
180 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
181 	uint32_t hash = _mesa_hash_pointer(bo);
182 	struct hash_entry *entry;
183 	uint32_t idx;
184 
185 	entry = _mesa_hash_table_search_pre_hashed(priv->bo_table, hash, bo);
186 
187 	if (entry) {
188 		idx = (uint32_t)(uintptr_t)entry->data;
189 	} else {
190 		idx = append_bo(stream, bo);
191 		_mesa_hash_table_insert_pre_hashed(priv->bo_table, hash, bo,
192 			(void *)(uintptr_t)idx);
193 	}
194 
195 	if (flags & ETNA_RELOC_READ)
196 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
197 	if (flags & ETNA_RELOC_WRITE)
198 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
199 
200 	return idx;
201 }
202 
etna_cmd_stream_flush(struct etna_cmd_stream * stream,int in_fence_fd,int * out_fence_fd,bool is_noop)203 void etna_cmd_stream_flush(struct etna_cmd_stream *stream, int in_fence_fd,
204 		int *out_fence_fd, bool is_noop)
205 {
206 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
207 	struct etna_gpu *gpu = priv->pipe->gpu;
208 
209 	struct drm_etnaviv_gem_submit req = {
210 		.pipe = gpu->core,
211 		.exec_state = priv->pipe->id,
212 		.bos = VOID2U64(priv->submit.bos),
213 		.nr_bos = priv->submit.nr_bos,
214 		.relocs = VOID2U64(priv->submit.relocs),
215 		.nr_relocs = priv->submit.nr_relocs,
216 		.pmrs = VOID2U64(priv->submit.pmrs),
217 		.nr_pmrs = priv->submit.nr_pmrs,
218 		.stream = VOID2U64(stream->buffer),
219 		.stream_size = stream->offset * 4, /* in bytes */
220 	};
221 
222 	if (in_fence_fd != -1) {
223 		req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
224 		req.fence_fd = in_fence_fd;
225 	}
226 
227 	if (out_fence_fd)
228 		req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
229 
230 	if (gpu->dev->use_softpin)
231 		req.flags |= ETNA_SUBMIT_SOFTPIN;
232 
233 	if (stream->offset == priv->offset_end_of_context_init && !out_fence_fd &&
234 	    !priv->submit.nr_pmrs)
235 		is_noop = true;
236 
237 	if (likely(!is_noop)) {
238 		int ret;
239 
240 		ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
241 				&req, sizeof(req));
242 
243 		if (ret)
244 			ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
245 		else
246 			priv->last_timestamp = req.fence;
247 	}
248 
249 	for (uint32_t i = 0; i < priv->nr_bos; i++)
250 		etna_bo_del(priv->bos[i]);
251 
252 	_mesa_hash_table_clear(priv->bo_table, NULL);
253 
254 	if (out_fence_fd)
255 		*out_fence_fd = req.fence_fd;
256 
257 	stream->offset = 0;
258 	priv->submit.nr_bos = 0;
259 	priv->submit.nr_relocs = 0;
260 	priv->submit.nr_pmrs = 0;
261 	priv->nr_bos = 0;
262 	priv->offset_end_of_context_init = 0;
263 }
264 
etna_cmd_stream_reloc(struct etna_cmd_stream * stream,const struct etna_reloc * r)265 void etna_cmd_stream_reloc(struct etna_cmd_stream *stream,
266 									  const struct etna_reloc *r)
267 {
268 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
269 	struct drm_etnaviv_gem_submit_reloc *reloc;
270 	uint32_t addr = r->bo->va + r->offset;
271 	uint32_t bo_idx = bo2idx(stream, r->bo, r->flags);
272 	uint32_t idx;
273 
274 	if (!priv->pipe->gpu->dev->use_softpin) {
275 		idx = APPEND(&priv->submit, relocs);
276 		reloc = &priv->submit.relocs[idx];
277 
278 		reloc->reloc_idx = bo_idx;
279 		reloc->reloc_offset = r->offset;
280 		reloc->submit_offset = stream->offset * 4; /* in bytes */
281 		reloc->flags = 0;
282 	}
283 
284 	etna_cmd_stream_emit(stream, addr);
285 }
286 
etna_cmd_stream_ref_bo(struct etna_cmd_stream * stream,struct etna_bo * bo,uint32_t flags)287 void etna_cmd_stream_ref_bo(struct etna_cmd_stream *stream, struct etna_bo *bo,
288 		uint32_t flags)
289 {
290 	bo2idx(stream, bo, flags);
291 }
292 
etna_cmd_stream_mark_end_of_context_init(struct etna_cmd_stream * stream)293 void etna_cmd_stream_mark_end_of_context_init(struct etna_cmd_stream *stream)
294 {
295    struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
296 
297    /*
298     * All commands before the end of context init are guaranteed to only alter GPU internal
299     * state and have no externally visible side effects, so we can skip the submit if the
300     * command buffer contains only context init commands.
301     */
302    priv->offset_end_of_context_init = stream->offset;
303 }
304 
etna_cmd_stream_perf(struct etna_cmd_stream * stream,const struct etna_perf * p)305 void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)
306 {
307 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
308 	struct drm_etnaviv_gem_submit_pmr *pmr;
309 	uint32_t idx = APPEND(&priv->submit, pmrs);
310 
311 	pmr = &priv->submit.pmrs[idx];
312 
313 	pmr->flags = p->flags;
314 	pmr->sequence = p->sequence;
315 	pmr->read_offset = p->offset;
316 	pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);
317 	pmr->domain = p->signal->domain->id;
318 	pmr->signal = p->signal->signal;
319 }
320