• 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 
29 #include "etnaviv_drmif.h"
30 #include "etnaviv_priv.h"
31 
32 static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
33 
grow(void * ptr,uint32_t nr,uint32_t * max,uint32_t sz)34 static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
35 {
36 	if ((nr + 1) > *max) {
37 		if ((*max * 2) < (nr + 1))
38 			*max = nr + 5;
39 		else
40 			*max = *max * 2;
41 		ptr = realloc(ptr, *max * sz);
42 	}
43 
44 	return ptr;
45 }
46 
47 #define APPEND(x, name) ({ \
48 	(x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
49 	(x)->nr_ ## name ++; \
50 })
51 
52 static inline struct etna_cmd_stream_priv *
etna_cmd_stream_priv(struct etna_cmd_stream * stream)53 etna_cmd_stream_priv(struct etna_cmd_stream *stream)
54 {
55     return (struct etna_cmd_stream_priv *)stream;
56 }
57 
etna_cmd_stream_new(struct etna_pipe * pipe,uint32_t size,void (* reset_notify)(struct etna_cmd_stream * stream,void * priv),void * priv)58 struct etna_cmd_stream *etna_cmd_stream_new(struct etna_pipe *pipe, uint32_t size,
59 		void (*reset_notify)(struct etna_cmd_stream *stream, void *priv),
60 		void *priv)
61 {
62 	struct etna_cmd_stream_priv *stream = NULL;
63 
64 	if (size == 0) {
65 		ERROR_MSG("invalid size of 0");
66 		goto fail;
67 	}
68 
69 	stream = calloc(1, sizeof(*stream));
70 	if (!stream) {
71 		ERROR_MSG("allocation failed");
72 		goto fail;
73 	}
74 
75 	/* allocate even number of 32-bit words */
76 	size = ALIGN(size, 2);
77 
78 	stream->base.buffer = malloc(size * sizeof(uint32_t));
79 	if (!stream->base.buffer) {
80 		ERROR_MSG("allocation failed");
81 		goto fail;
82 	}
83 
84 	stream->base.size = size;
85 	stream->pipe = pipe;
86 	stream->reset_notify = reset_notify;
87 	stream->reset_notify_priv = priv;
88 
89 	return &stream->base;
90 
91 fail:
92 	if (stream)
93 		etna_cmd_stream_del(&stream->base);
94 
95 	return NULL;
96 }
97 
etna_cmd_stream_del(struct etna_cmd_stream * stream)98 void etna_cmd_stream_del(struct etna_cmd_stream *stream)
99 {
100 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
101 
102 	free(stream->buffer);
103 	free(priv->submit.relocs);
104 	free(priv->submit.pmrs);
105 	free(priv);
106 }
107 
reset_buffer(struct etna_cmd_stream * stream)108 static void reset_buffer(struct etna_cmd_stream *stream)
109 {
110 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
111 
112 	stream->offset = 0;
113 	priv->submit.nr_bos = 0;
114 	priv->submit.nr_relocs = 0;
115 	priv->submit.nr_pmrs = 0;
116 	priv->nr_bos = 0;
117 
118 	if (priv->reset_notify)
119 		priv->reset_notify(stream, priv->reset_notify_priv);
120 }
121 
etna_cmd_stream_timestamp(struct etna_cmd_stream * stream)122 uint32_t etna_cmd_stream_timestamp(struct etna_cmd_stream *stream)
123 {
124 	return etna_cmd_stream_priv(stream)->last_timestamp;
125 }
126 
append_bo(struct etna_cmd_stream * stream,struct etna_bo * bo)127 static uint32_t append_bo(struct etna_cmd_stream *stream, struct etna_bo *bo)
128 {
129 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
130 	uint32_t idx;
131 
132 	idx = APPEND(&priv->submit, bos);
133 	idx = APPEND(priv, bos);
134 
135 	priv->submit.bos[idx].flags = 0;
136 	priv->submit.bos[idx].handle = bo->handle;
137 
138 	priv->bos[idx] = etna_bo_ref(bo);
139 
140 	return idx;
141 }
142 
143 /* add (if needed) bo, return idx: */
bo2idx(struct etna_cmd_stream * stream,struct etna_bo * bo,uint32_t flags)144 static uint32_t bo2idx(struct etna_cmd_stream *stream, struct etna_bo *bo,
145 		uint32_t flags)
146 {
147 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
148 	uint32_t idx;
149 
150 	pthread_mutex_lock(&idx_lock);
151 
152 	if (!bo->current_stream) {
153 		idx = append_bo(stream, bo);
154 		bo->current_stream = stream;
155 		bo->idx = idx;
156 	} else if (bo->current_stream == stream) {
157 		idx = bo->idx;
158 	} else {
159 		/* slow-path: */
160 		for (idx = 0; idx < priv->nr_bos; idx++)
161 			if (priv->bos[idx] == bo)
162 				break;
163 		if (idx == priv->nr_bos) {
164 			/* not found */
165 			idx = append_bo(stream, bo);
166 		}
167 	}
168 	pthread_mutex_unlock(&idx_lock);
169 
170 	if (flags & ETNA_RELOC_READ)
171 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_READ;
172 	if (flags & ETNA_RELOC_WRITE)
173 		priv->submit.bos[idx].flags |= ETNA_SUBMIT_BO_WRITE;
174 
175 	return idx;
176 }
177 
flush(struct etna_cmd_stream * stream,int in_fence_fd,int * out_fence_fd)178 static void flush(struct etna_cmd_stream *stream, int in_fence_fd,
179 		  int *out_fence_fd)
180 {
181 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
182 	int ret, id = priv->pipe->id;
183 	struct etna_gpu *gpu = priv->pipe->gpu;
184 
185 	struct drm_etnaviv_gem_submit req = {
186 		.pipe = gpu->core,
187 		.exec_state = id,
188 		.bos = VOID2U64(priv->submit.bos),
189 		.nr_bos = priv->submit.nr_bos,
190 		.relocs = VOID2U64(priv->submit.relocs),
191 		.nr_relocs = priv->submit.nr_relocs,
192 		.pmrs = VOID2U64(priv->submit.pmrs),
193 		.nr_pmrs = priv->submit.nr_pmrs,
194 		.stream = VOID2U64(stream->buffer),
195 		.stream_size = stream->offset * 4, /* in bytes */
196 	};
197 
198 	if (in_fence_fd != -1) {
199 		req.flags |= ETNA_SUBMIT_FENCE_FD_IN | ETNA_SUBMIT_NO_IMPLICIT;
200 		req.fence_fd = in_fence_fd;
201 	}
202 
203 	if (out_fence_fd)
204 		req.flags |= ETNA_SUBMIT_FENCE_FD_OUT;
205 
206 	ret = drmCommandWriteRead(gpu->dev->fd, DRM_ETNAVIV_GEM_SUBMIT,
207 			&req, sizeof(req));
208 
209 	if (ret)
210 		ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
211 	else
212 		priv->last_timestamp = req.fence;
213 
214 	for (uint32_t i = 0; i < priv->nr_bos; i++) {
215 		struct etna_bo *bo = priv->bos[i];
216 
217 		bo->current_stream = NULL;
218 		etna_bo_del(bo);
219 	}
220 
221 	if (out_fence_fd)
222 		*out_fence_fd = req.fence_fd;
223 }
224 
etna_cmd_stream_flush(struct etna_cmd_stream * stream)225 void etna_cmd_stream_flush(struct etna_cmd_stream *stream)
226 {
227 	flush(stream, -1, NULL);
228 	reset_buffer(stream);
229 }
230 
etna_cmd_stream_flush2(struct etna_cmd_stream * stream,int in_fence_fd,int * out_fence_fd)231 void etna_cmd_stream_flush2(struct etna_cmd_stream *stream, int in_fence_fd,
232 			    int *out_fence_fd)
233 {
234 	flush(stream, in_fence_fd, out_fence_fd);
235 	reset_buffer(stream);
236 }
237 
etna_cmd_stream_finish(struct etna_cmd_stream * stream)238 void etna_cmd_stream_finish(struct etna_cmd_stream *stream)
239 {
240 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
241 
242 	flush(stream, -1, NULL);
243 	etna_pipe_wait(priv->pipe, priv->last_timestamp, 5000);
244 	reset_buffer(stream);
245 }
246 
etna_cmd_stream_reloc(struct etna_cmd_stream * stream,const struct etna_reloc * r)247 void etna_cmd_stream_reloc(struct etna_cmd_stream *stream, const struct etna_reloc *r)
248 {
249 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
250 	struct drm_etnaviv_gem_submit_reloc *reloc;
251 	uint32_t idx = APPEND(&priv->submit, relocs);
252 	uint32_t addr = 0;
253 
254 	reloc = &priv->submit.relocs[idx];
255 
256 	reloc->reloc_idx = bo2idx(stream, r->bo, r->flags);
257 	reloc->reloc_offset = r->offset;
258 	reloc->submit_offset = stream->offset * 4; /* in bytes */
259 	reloc->flags = 0;
260 
261 	etna_cmd_stream_emit(stream, addr);
262 }
263 
etna_cmd_stream_perf(struct etna_cmd_stream * stream,const struct etna_perf * p)264 void etna_cmd_stream_perf(struct etna_cmd_stream *stream, const struct etna_perf *p)
265 {
266 	struct etna_cmd_stream_priv *priv = etna_cmd_stream_priv(stream);
267 	struct drm_etnaviv_gem_submit_pmr *pmr;
268 	uint32_t idx = APPEND(&priv->submit, pmrs);
269 
270 	pmr = &priv->submit.pmrs[idx];
271 
272 	pmr->flags = p->flags;
273 	pmr->sequence = p->sequence;
274 	pmr->read_offset = p->offset;
275 	pmr->read_idx = bo2idx(stream, p->bo, ETNA_SUBMIT_BO_READ | ETNA_SUBMIT_BO_WRITE);
276 	pmr->domain = p->signal->domain->id;
277 	pmr->signal = p->signal->signal;
278 }
279