• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <christian.gmeiner@gmail.com>
25  */
26 
27 #include <assert.h>
28 #include <stdlib.h>
29 
30 #include "util/hash_table.h"
31 
32 #include "etnaviv_drmif.h"
33 #include "etnaviv_priv.h"
34 
grow(void * ptr,uint32_t nr,uint32_t * max,uint32_t sz)35 static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
36 {
37 	if ((nr + 1) > *max) {
38 		if ((*max * 2) < (nr + 1))
39 			*max = nr + 5;
40 		else
41 			*max = *max * 2;
42 		ptr = realloc(ptr, *max * sz);
43 	}
44 
45 	return ptr;
46 }
47 
48 #define APPEND(x, name) ({ \
49 	(x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
50 	(x)->nr_ ## name ++; \
51 })
52 
etna_cmd_stream_realloc(struct etna_cmd_stream * stream,size_t n)53 void etna_cmd_stream_realloc(struct etna_cmd_stream *stream, size_t n)
54 {
55 	size_t size;
56 	void *buffer;
57 
58 	/*
59 	 * Increase the command buffer size by 4 kiB. Here we pick 4 kiB
60 	 * increment to prevent it from growing too much too quickly.
61 	 */
62 	size = ALIGN(stream->size + n, 1024);
63 
64 	/* Command buffer is too big for older kernel versions */
65 	if (size > 0x4000)
66 		goto error;
67 
68 	buffer = realloc(stream->buffer, size * 4);
69 	if (!buffer)
70 		goto error;
71 
72 	stream->buffer = buffer;
73 	stream->size = size;
74 
75 	return;
76 
77 error:
78 	DEBUG_MSG("command buffer too long, forcing flush.");
79 	etna_cmd_stream_force_flush(stream);
80 }
81 
82 static inline struct etna_cmd_stream_priv *
etna_cmd_stream_priv(struct etna_cmd_stream * stream)83 etna_cmd_stream_priv(struct etna_cmd_stream *stream)
84 {
85     return (struct etna_cmd_stream_priv *)stream;
86 }
87 
etna_cmd_stream_new(struct etna_pipe * pipe,uint32_t size,void (* force_flush)(struct etna_cmd_stream * stream,void * priv),void * priv)88 struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe,
89         uint32_t size,
90 		void (*force_flush)(struct etna_cmd_stream *stream, void *priv),
91 		void *priv)
92 {
93 	struct etna_cmd_stream_priv *stream = NULL;
94 
95 	if (size == 0) {
96 		ERROR_MSG("invalid size of 0");
97 		goto fail;
98 	}
99 
100 	stream = calloc(1, sizeof(*stream));
101 	if (!stream) {
102 		ERROR_MSG("allocation failed");
103 		goto fail;
104 	}
105 
106 	/* allocate even number of 32-bit words */
107 	size = ALIGN(size, 2);
108 
109 	stream->base.buffer = malloc(size * sizeof(uint32_t));
110 	if (!stream->base.buffer) {
111 		ERROR_MSG("allocation failed");
112 		goto fail;
113 	}
114 
115 	stream->base.size = size;
116 	stream->pipe = pipe;
117 	stream->force_flush = force_flush;
118 	stream->force_flush_priv = priv;
119 
120 	stream->bo_table = _mesa_pointer_hash_table_create(NULL);
121 
122 	return &stream->base;
123 
124 fail:
125 	if (stream)
126 		etna_cmd_stream_del(&stream->base);
127 
128 	return NULL;
129 }
130 
etna_cmd_stream_del(struct etna_cmd_stream * stream)131 void etna_cmd_stream_del(struct etna_cmd_stream *stream)
132 {
133 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
134 
135 	_mesa_hash_table_destroy(priv->bo_table, NULL);
136 
137 	free(stream->buffer);
138 	free(priv->submit.relocs);
139 	free(priv->submit.pmrs);
140 	free(priv);
141 }
142 
etna_cmd_stream_force_flush(struct etna_cmd_stream * stream)143 void etna_cmd_stream_force_flush(struct etna_cmd_stream *stream)
144 {
145 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
146 
147 	if (priv->force_flush)
148 		priv->force_flush(stream, priv->force_flush_priv);
149 }
150 
etna_cmd_stream_timestamp(struct etna_cmd_stream * stream)151 uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
152 {
153 	return etna_cmd_stream_priv(stream)->last_timestamp;
154 }
155 
append_bo(struct etna_cmd_stream * stream,struct etna_bo * bo)156 static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
157 {
158 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
159 	uint32_t idx;
160 
161 	idx = APPEND(&priv->submit, bos);
162 	idx = APPEND(priv, bos);
163 
164 	priv->submit.bos[idx].flags = 0;
165 	priv->submit.bos[idx].handle = bo->handle;
166 	priv->submit.bos[idx].presumed = bo->va;
167 
168 	priv->bos[idx] = etna_bo_ref(bo);
169 
170 	return idx;
171 }
172 
173 /* add (if needed) bo, return idx: */
bo2idx(struct etna_cmd_stream * stream,struct etna_bo * bo,uint32_t flags)174 static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
175 		uint32_t flags)
176 {
177 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
178 	uint32_t hash = _mesa_hash_pointer(bo);
179 	struct hash_entry *entry;
180 	uint32_t idx;
181 
182 	entry = _mesa_hash_table_search_pre_hashed(priv->bo_table, hash, bo);
183 
184 	if (entry) {
185 		idx = (uint32_t)(uintptr_t)entry->data;
186 	} else {
187 		idx = append_bo(stream, bo);
188 		_mesa_hash_table_insert_pre_hashed(priv->bo_table, hash, bo,
189 			(void *)(uintptr_t)idx);
190 	}
191 
192 	if (flags & ETNA_RELOC_READ)
193 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
194 	if (flags & ETNA_RELOC_WRITE)
195 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
196 
197 	return idx;
198 }
199 
etna_cmd_stream_flush(struct etna_cmd_stream * stream,int in_fence_fd,int * out_fence_fd,bool is_noop)200 void etna_cmd_stream_flush(struct etna_cmd_stream *stream, int in_fence_fd,
201 		int *out_fence_fd, bool is_noop)
202 {
203 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
204 	int ret, id = priv->pipe->id;
205 	struct etna_gpu *gpu = priv->pipe->gpu;
206 
207 	struct drm_etnaviv_gem_submit req = {
208 		.pipe = gpu->core,
209 		.exec_state = id,
210 		.bos = VOID2U64(priv->submit.bos),
211 		.nr_bos = priv->submit.nr_bos,
212 		.relocs = VOID2U64(priv->submit.relocs),
213 		.nr_relocs = priv->submit.nr_relocs,
214 		.pmrs = VOID2U64(priv->submit.pmrs),
215 		.nr_pmrs = priv->submit.nr_pmrs,
216 		.stream = VOID2U64(stream->buffer),
217 		.stream_size = stream->offset * 4, /* in bytes */
218 	};
219 
220 	if (in_fence_fd != -1) {
221 		req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
222 		req.fence_fd = in_fence_fd;
223 	}
224 
225 	if (out_fence_fd)
226 		req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
227 
228 	if (gpu->dev->use_softpin)
229 		req.flags |= ETNA_SUBMIT_SOFTPIN;
230 
231 	if (unlikely(is_noop))
232 		ret = 0;
233 	else
234 		ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
235 				&req, sizeof(req));
236 
237 	if (ret)
238 		ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
239 	else
240 		priv->last_timestamp = req.fence;
241 
242 	for (uint32_t i = 0; i < priv->nr_bos; i++)
243 		etna_bo_del(priv->bos[i]);
244 
245 	_mesa_hash_table_clear(priv->bo_table, NULL);
246 
247 	if (out_fence_fd)
248 		*out_fence_fd = req.fence_fd;
249 
250 	stream->offset = 0;
251 	priv->submit.nr_bos = 0;
252 	priv->submit.nr_relocs = 0;
253 	priv->submit.nr_pmrs = 0;
254 	priv->nr_bos = 0;
255 }
256 
etna_cmd_stream_reloc(struct etna_cmd_stream * stream,const struct etna_reloc * r)257 void etna_cmd_stream_reloc(struct etna_cmd_stream *stream,
258 									  const struct etna_reloc *r)
259 {
260 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
261 	struct drm_etnaviv_gem_submit_reloc *reloc;
262 	uint32_t addr = r->bo->va + r->offset;
263 	uint32_t bo_idx = bo2idx(stream, r->bo, r->flags);
264 	uint32_t idx;
265 
266 	if (!priv->pipe->gpu->dev->use_softpin) {
267 		idx = APPEND(&priv->submit, relocs);
268 		reloc = &priv->submit.relocs[idx];
269 
270 		reloc->reloc_idx = bo_idx;
271 		reloc->reloc_offset = r->offset;
272 		reloc->submit_offset = stream->offset * 4; /* in bytes */
273 		reloc->flags = 0;
274 	}
275 
276 	etna_cmd_stream_emit(stream, addr);
277 }
278 
etna_cmd_stream_ref_bo(struct etna_cmd_stream * stream,struct etna_bo * bo,uint32_t flags)279 void etna_cmd_stream_ref_bo(struct etna_cmd_stream *stream, struct etna_bo *bo,
280 		uint32_t flags)
281 {
282 	bo2idx(stream, bo, flags);
283 }
284 
etna_cmd_stream_perf(struct etna_cmd_stream * stream,const struct etna_perf * p)285 void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)
286 {
287 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
288 	struct drm_etnaviv_gem_submit_pmr *pmr;
289 	uint32_t idx = APPEND(&priv->submit, pmrs);
290 
291 	pmr = &priv->submit.pmrs[idx];
292 
293 	pmr->flags = p->flags;
294 	pmr->sequence = p->sequence;
295 	pmr->read_offset = p->offset;
296 	pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);
297 	pmr->domain = p->signal->domain->id;
298 	pmr->signal = p->signal->signal;
299 }
300