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