msm_ringbuffer.c revision e88f27b3
1/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
2
3/*
4 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
15 * Software.
16 *
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 * SOFTWARE.
24 *
25 * Authors:
26 *    Rob Clark <robclark@freedesktop.org>
27 */
28
29#include <assert.h>
30
31#include "freedreno_ringbuffer.h"
32#include "msm_priv.h"
33
34struct msm_ringbuffer {
35	struct fd_ringbuffer base;
36	struct fd_bo *ring_bo;
37
38	struct list_head submit_list;
39
40	/* bo's table: */
41	struct drm_msm_gem_submit_bo *bos;
42	uint32_t nr_bos, max_bos;
43
44	/* cmd's table: */
45	struct drm_msm_gem_submit_cmd *cmds;
46	uint32_t nr_cmds, max_cmds;
47	struct fd_ringbuffer **rings;
48	uint32_t nr_rings, max_rings;
49
50	/* reloc's table: */
51	struct drm_msm_gem_submit_reloc *relocs;
52	uint32_t nr_relocs, max_relocs;
53};
54
55static void *grow(void *ptr, uint32_t nr, uint32_t *max, uint32_t sz)
56{
57	if ((nr + 1) > *max) {
58		if ((*max * 2) < (nr + 1))
59			*max = nr + 5;
60		else
61			*max = *max * 2;
62		ptr = realloc(ptr, *max * sz);
63	}
64	return ptr;
65}
66
67#define APPEND(x, name) ({ \
68	(x)->name = grow((x)->name, (x)->nr_ ## name, &(x)->max_ ## name, sizeof((x)->name[0])); \
69	(x)->nr_ ## name ++; \
70})
71
72static inline struct msm_ringbuffer * to_msm_ringbuffer(struct fd_ringbuffer *x)
73{
74	return (struct msm_ringbuffer *)x;
75}
76
77/* add (if needed) bo, return idx: */
78static uint32_t bo2idx(struct fd_ringbuffer *ring, struct fd_bo *bo, uint32_t flags)
79{
80	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
81	struct msm_bo *msm_bo = to_msm_bo(bo);
82	int id = ring->pipe->id;
83	uint32_t idx;
84	if (!msm_bo->indexp1[id]) {
85		struct list_head *list = &msm_bo->list[id];
86		idx = APPEND(msm_ring, bos);
87		msm_ring->bos[idx].flags = 0;
88		msm_ring->bos[idx].handle = bo->handle;
89		msm_ring->bos[idx].presumed = msm_bo->presumed;
90		msm_bo->indexp1[id] = idx + 1;
91
92		assert(LIST_IS_EMPTY(list));
93		fd_bo_ref(bo);
94		list_addtail(list, &msm_ring->submit_list);
95	} else {
96		idx = msm_bo->indexp1[id] - 1;
97	}
98	if (flags & FD_RELOC_READ)
99		msm_ring->bos[idx].flags |= MSM_SUBMIT_BO_READ;
100	if (flags & FD_RELOC_WRITE)
101		msm_ring->bos[idx].flags |= MSM_SUBMIT_BO_WRITE;
102	return idx;
103}
104
105static int check_cmd_bo(struct fd_ringbuffer *ring,
106		struct drm_msm_gem_submit_cmd *cmd, struct fd_bo *bo)
107{
108	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
109	return msm_ring->bos[cmd->submit_idx].handle == bo->handle;
110}
111
112static uint32_t offset_bytes(void *end, void *start)
113{
114	return ((char *)end) - ((char *)start);
115}
116
117static struct drm_msm_gem_submit_cmd * get_cmd(struct fd_ringbuffer *ring,
118		struct fd_ringbuffer *target_ring, struct fd_bo *target_bo,
119		uint32_t submit_offset, uint32_t size, uint32_t type)
120{
121	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
122	struct drm_msm_gem_submit_cmd *cmd = NULL;
123	uint32_t i;
124
125	/* figure out if we already have a cmd buf: */
126	for (i = 0; i < msm_ring->nr_cmds; i++) {
127		cmd = &msm_ring->cmds[i];
128		if ((cmd->submit_offset == submit_offset) &&
129				(cmd->size == size) &&
130				(cmd->type == type) &&
131				check_cmd_bo(ring, cmd, target_bo))
132			break;
133		cmd = NULL;
134	}
135
136	/* create cmd buf if not: */
137	if (!cmd) {
138		uint32_t idx = APPEND(msm_ring, cmds);
139		APPEND(msm_ring, rings);
140		msm_ring->rings[idx] = target_ring;
141		cmd = &msm_ring->cmds[idx];
142		cmd->type = type;
143		cmd->submit_idx = bo2idx(ring, target_bo, FD_RELOC_READ);
144		cmd->submit_offset = submit_offset;
145		cmd->size = size;
146	}
147
148	return cmd;
149}
150
151static void * msm_ringbuffer_hostptr(struct fd_ringbuffer *ring)
152{
153	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
154	return fd_bo_map(msm_ring->ring_bo);
155}
156
157static uint32_t find_next_reloc_idx(struct msm_ringbuffer *msm_ring,
158		uint32_t start, uint32_t offset)
159{
160	uint32_t i;
161
162	/* a binary search would be more clever.. */
163	for (i = start; i < msm_ring->nr_relocs; i++) {
164		struct drm_msm_gem_submit_reloc *reloc = &msm_ring->relocs[i];
165		if (reloc->submit_offset >= offset)
166			return i;
167	}
168
169	return i;
170}
171
172static int msm_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start)
173{
174	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
175	struct fd_bo *ring_bo = msm_ring->ring_bo;
176	struct drm_msm_gem_submit req = {
177			.pipe = to_msm_pipe(ring->pipe)->pipe,
178	};
179	struct msm_bo *msm_bo = NULL, *tmp;
180	uint32_t i, submit_offset, size;
181	int ret, id = ring->pipe->id;
182
183	submit_offset = offset_bytes(last_start, ring->start);
184	size = offset_bytes(ring->cur, last_start);
185
186	get_cmd(ring, ring, ring_bo, submit_offset, size, MSM_SUBMIT_CMD_BUF);
187
188	/* needs to be after get_cmd() as that could create bos/cmds table: */
189	req.bos = VOID2U64(msm_ring->bos),
190	req.nr_bos = msm_ring->nr_bos;
191	req.cmds = VOID2U64(msm_ring->cmds),
192	req.nr_cmds = msm_ring->nr_cmds;
193
194	/* for each of the cmd's fix up their reloc's: */
195	for (i = 0; i < msm_ring->nr_cmds; i++) {
196		struct drm_msm_gem_submit_cmd *cmd = &msm_ring->cmds[i];
197		struct msm_ringbuffer *target_ring = to_msm_ringbuffer(msm_ring->rings[i]);
198		uint32_t a = find_next_reloc_idx(target_ring, 0, cmd->submit_offset);
199		uint32_t b = find_next_reloc_idx(target_ring, a, cmd->submit_offset + cmd->size);
200		cmd->relocs = VOID2U64(&target_ring->relocs[a]);
201		cmd->nr_relocs = (b > a) ? b - a : 0;
202	}
203
204	DEBUG_MSG("nr_cmds=%u, nr_bos=%u\n", req.nr_cmds, req.nr_bos);
205
206	ret = drmCommandWriteRead(ring->pipe->dev->fd, DRM_MSM_GEM_SUBMIT,
207			&req, sizeof(req));
208	if (ret)
209		ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
210
211	LIST_FOR_EACH_ENTRY_SAFE(msm_bo, tmp, &msm_ring->submit_list, list[id]) {
212		struct list_head *list = &msm_bo->list[id];
213		list_delinit(list);
214		msm_bo->indexp1[id] = 0;
215		fd_bo_del(&msm_bo->base);
216	}
217
218	/* for each of the cmd buffers, clear their reloc's: */
219	for (i = 0; i < msm_ring->nr_cmds; i++) {
220		struct msm_ringbuffer *target_ring = to_msm_ringbuffer(msm_ring->rings[i]);
221		target_ring->nr_relocs = 0;
222	}
223
224	msm_ring->nr_cmds = 0;
225	msm_ring->nr_bos = 0;
226
227	return ret;
228}
229
230static void msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
231		const struct fd_reloc *r)
232{
233	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
234	struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
235	struct msm_bo *msm_bo = to_msm_bo(r->bo);
236	struct drm_msm_gem_submit_reloc *reloc;
237	uint32_t idx = APPEND(msm_ring, relocs);
238	uint32_t addr;
239
240	reloc = &msm_ring->relocs[idx];
241
242	reloc->reloc_idx = bo2idx(parent, r->bo, r->flags);
243	reloc->reloc_offset = r->offset;
244	reloc->or = r->or;
245	reloc->shift = r->shift;
246	reloc->submit_offset = offset_bytes(ring->cur, ring->start);
247
248	addr = msm_bo->presumed;
249	if (r->shift < 0)
250		addr >>= -r->shift;
251	else
252		addr <<= r->shift;
253	(*ring->cur++) = addr | r->or;
254}
255
256static void msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
257		struct fd_ringmarker *target, struct fd_ringmarker *end)
258{
259	struct fd_bo *target_bo = to_msm_ringbuffer(target->ring)->ring_bo;
260	struct drm_msm_gem_submit_cmd *cmd;
261	uint32_t submit_offset, size;
262
263	submit_offset = offset_bytes(target->cur, target->ring->start);
264	size = offset_bytes(end->cur, target->cur);
265
266	cmd = get_cmd(ring, target->ring, target_bo, submit_offset, size,
267			MSM_SUBMIT_CMD_IB_TARGET_BUF);
268	assert(cmd);
269
270	msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){
271		.bo = target_bo,
272		.flags = FD_RELOC_READ,
273		.offset = submit_offset,
274	});
275}
276
277static void msm_ringbuffer_destroy(struct fd_ringbuffer *ring)
278{
279	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
280	if (msm_ring->ring_bo)
281		fd_bo_del(msm_ring->ring_bo);
282	free(msm_ring);
283}
284
285static struct fd_ringbuffer_funcs funcs = {
286		.hostptr = msm_ringbuffer_hostptr,
287		.flush = msm_ringbuffer_flush,
288		.emit_reloc = msm_ringbuffer_emit_reloc,
289		.emit_reloc_ring = msm_ringbuffer_emit_reloc_ring,
290		.destroy = msm_ringbuffer_destroy,
291};
292
293struct fd_ringbuffer * msm_ringbuffer_new(struct fd_pipe *pipe,
294		uint32_t size)
295{
296	struct msm_ringbuffer *msm_ring;
297	struct fd_ringbuffer *ring = NULL;
298
299	msm_ring = calloc(1, sizeof(*msm_ring));
300	if (!msm_ring) {
301		ERROR_MSG("allocation failed");
302		goto fail;
303	}
304
305	ring = &msm_ring->base;
306	ring->funcs = &funcs;
307
308	list_inithead(&msm_ring->submit_list);
309
310	msm_ring->ring_bo = fd_bo_new(pipe->dev, size, 0);
311	if (!msm_ring->ring_bo) {
312		ERROR_MSG("ringbuffer allocation failed");
313		goto fail;
314	}
315
316	return ring;
317fail:
318	if (ring)
319		fd_ringbuffer_del(ring);
320	return NULL;
321}
322