msm_ringbuffer.c 18.6 KB
Newer Older
Rob Clark's avatar
Rob Clark committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */

/*
 * Copyright (C) 2013 Rob Clark <robclark@freedesktop.org>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *    Rob Clark <robclark@freedesktop.org>
 */

#include <assert.h>
30
#include <inttypes.h>
Rob Clark's avatar
Rob Clark committed
31
32
33
34

#include "freedreno_ringbuffer.h"
#include "msm_priv.h"

35
36
37
38
/* represents a single cmd buffer in the submit ioctl.  Each cmd buffer has
 * a backing bo, and a reloc table.
 */
struct msm_cmd {
39
40
	struct list_head list;

41
42
43
44
	struct fd_ringbuffer *ring;
	struct fd_bo *ring_bo;

	/* reloc's table: */
Rob Clark's avatar
Rob Clark committed
45
	DECLARE_ARRAY(struct drm_msm_gem_submit_reloc, relocs);
46
47

	uint32_t size;
48
49
};

Rob Clark's avatar
Rob Clark committed
50
51
52
struct msm_ringbuffer {
	struct fd_ringbuffer base;

Rob Clark's avatar
Rob Clark committed
53
54
	atomic_t refcnt;

55
56
57
58
59
	/* submit ioctl related tables:
	 * Note that bos and cmds are tracked by the parent ringbuffer, since
	 * that is global to the submit ioctl call.  The reloc's table is tracked
	 * per cmd-buffer.
	 */
60
61
	struct {
		/* bo's table: */
Rob Clark's avatar
Rob Clark committed
62
		DECLARE_ARRAY(struct drm_msm_gem_submit_bo, bos);
63
64

		/* cmd's table: */
Rob Clark's avatar
Rob Clark committed
65
		DECLARE_ARRAY(struct drm_msm_gem_submit_cmd, cmds);
66
67
	} submit;

68
	/* should have matching entries in submit.bos: */
69
	/* Note, only in parent ringbuffer */
Rob Clark's avatar
Rob Clark committed
70
	DECLARE_ARRAY(struct fd_bo *, bos);
71

72
	/* should have matching entries in submit.cmds: */
Rob Clark's avatar
Rob Clark committed
73
	DECLARE_ARRAY(struct msm_cmd *, cmds);
74

75
76
77
78
79
80
81
82
83
84
85
86
87
	/* List of physical cmdstream buffers (msm_cmd) assocated with this
	 * logical fd_ringbuffer.
	 *
	 * Note that this is different from msm_ringbuffer::cmds (which
	 * shadows msm_ringbuffer::submit::cmds for tracking submit ioctl
	 * related stuff, and *only* is tracked in the parent ringbuffer.
	 * And only has "completed" cmd buffers (ie. we already know the
	 * size) added via get_cmd().
	 */
	struct list_head cmd_list;

	int is_growable;
	unsigned cmd_count;
88
89
90
91
92

	unsigned seqno;

	/* maps fd_bo to idx: */
	void *bo_table;
Rob Clark's avatar
Rob Clark committed
93
94
};

95
96
97
98
99
static inline struct msm_ringbuffer * to_msm_ringbuffer(struct fd_ringbuffer *x)
{
	return (struct msm_ringbuffer *)x;
}

Rob Clark's avatar
Rob Clark committed
100
101
102
static void msm_ringbuffer_unref(struct fd_ringbuffer *ring);
static void msm_ringbuffer_ref(struct fd_ringbuffer *ring);

103
104
#define INIT_SIZE 0x1000

105
static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
106
107
108
109
110
111
drm_private extern pthread_mutex_t table_lock;

static void ring_bo_del(struct fd_device *dev, struct fd_bo *bo)
{
	int ret;

112
113
	assert(atomic_read(&bo->refcnt) == 1);

114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
	pthread_mutex_lock(&table_lock);
	ret = fd_bo_cache_free(&to_msm_device(dev)->ring_cache, bo);
	pthread_mutex_unlock(&table_lock);

	if (ret == 0)
		return;

	fd_bo_del(bo);
}

static struct fd_bo * ring_bo_new(struct fd_device *dev, uint32_t size)
{
	struct fd_bo *bo;

	bo = fd_bo_cache_alloc(&to_msm_device(dev)->ring_cache, &size, 0);
	if (bo)
		return bo;

	bo = fd_bo_new(dev, size, 0);
	if (!bo)
		return NULL;

	/* keep ringbuffer bo's out of the normal bo cache: */
	bo->bo_reuse = FALSE;

	return bo;
}
141

142
143
144
145
static void ring_cmd_del(struct msm_cmd *cmd)
{
	if (cmd->ring_bo)
		ring_bo_del(cmd->ring->pipe->dev, cmd->ring_bo);
146
147
	list_del(&cmd->list);
	to_msm_ringbuffer(cmd->ring)->cmd_count--;
148
149
150
151
152
153
	free(cmd->relocs);
	free(cmd);
}

static struct msm_cmd * ring_cmd_new(struct fd_ringbuffer *ring, uint32_t size)
{
154
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
155
156
157
158
159
160
161
162
163
164
	struct msm_cmd *cmd = calloc(1, sizeof(*cmd));

	if (!cmd)
		return NULL;

	cmd->ring = ring;
	cmd->ring_bo = ring_bo_new(ring->pipe->dev, size);
	if (!cmd->ring_bo)
		goto fail;

165
166
167
	list_addtail(&cmd->list, &msm_ring->cmd_list);
	msm_ring->cmd_count++;

168
169
170
171
172
173
174
	return cmd;

fail:
	ring_cmd_del(cmd);
	return NULL;
}

175
static struct msm_cmd *current_cmd(struct fd_ringbuffer *ring)
Rob Clark's avatar
Rob Clark committed
176
{
177
178
179
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	assert(!LIST_IS_EMPTY(&msm_ring->cmd_list));
	return LIST_LAST_ENTRY(&msm_ring->cmd_list, struct msm_cmd, list);
Rob Clark's avatar
Rob Clark committed
180
181
}

182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
static uint32_t append_bo(struct fd_ringbuffer *ring, struct fd_bo *bo)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	uint32_t idx;

	idx = APPEND(&msm_ring->submit, bos);
	idx = APPEND(msm_ring, bos);

	msm_ring->submit.bos[idx].flags = 0;
	msm_ring->submit.bos[idx].handle = bo->handle;
	msm_ring->submit.bos[idx].presumed = to_msm_bo(bo)->presumed;

	msm_ring->bos[idx] = fd_bo_ref(bo);

	return idx;
}

Rob Clark's avatar
Rob Clark committed
199
200
201
202
203
204
/* add (if needed) bo, return idx: */
static uint32_t bo2idx(struct fd_ringbuffer *ring, struct fd_bo *bo, uint32_t flags)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	struct msm_bo *msm_bo = to_msm_bo(bo);
	uint32_t idx;
205
	pthread_mutex_lock(&idx_lock);
206
	if (msm_bo->current_ring_seqno == msm_ring->seqno) {
207
		idx = msm_bo->idx;
Rob Clark's avatar
Rob Clark committed
208
	} else {
209
210
211
212
213
214
215
		void *val;

		if (!msm_ring->bo_table)
			msm_ring->bo_table = drmHashCreate();

		if (!drmHashLookup(msm_ring->bo_table, bo->handle, &val)) {
			/* found */
Rob Clark's avatar
Rob Clark committed
216
			idx = (uint32_t)(uintptr_t)val;
217
		} else {
218
			idx = append_bo(ring, bo);
Rob Clark's avatar
Rob Clark committed
219
			val = (void *)(uintptr_t)idx;
220
			drmHashInsert(msm_ring->bo_table, bo->handle, val);
221
		}
222
223
		msm_bo->current_ring_seqno = msm_ring->seqno;
		msm_bo->idx = idx;
Rob Clark's avatar
Rob Clark committed
224
	}
225
	pthread_mutex_unlock(&idx_lock);
Rob Clark's avatar
Rob Clark committed
226
	if (flags & FD_RELOC_READ)
227
		msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_READ;
Rob Clark's avatar
Rob Clark committed
228
	if (flags & FD_RELOC_WRITE)
229
		msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_WRITE;
Rob Clark's avatar
Rob Clark committed
230
231
232
	return idx;
}

233
234
235
236
static int check_cmd_bo(struct fd_ringbuffer *ring,
		struct drm_msm_gem_submit_cmd *cmd, struct fd_bo *bo)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
237
	return msm_ring->submit.bos[cmd->submit_idx].handle == bo->handle;
238
239
}

240
241
/* Ensure that submit has corresponding entry in cmds table for the
 * target cmdstream buffer:
242
243
244
 *
 * Returns TRUE if new cmd added (else FALSE if it was already in
 * the cmds table)
245
 */
246
static int get_cmd(struct fd_ringbuffer *ring, struct msm_cmd *target_cmd,
247
		uint32_t submit_offset, uint32_t size, uint32_t type)
Rob Clark's avatar
Rob Clark committed
248
249
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
250
	struct drm_msm_gem_submit_cmd *cmd;
Rob Clark's avatar
Rob Clark committed
251
252
253
	uint32_t i;

	/* figure out if we already have a cmd buf: */
254
255
	for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
		cmd = &msm_ring->submit.cmds[i];
Rob Clark's avatar
Rob Clark committed
256
257
		if ((cmd->submit_offset == submit_offset) &&
				(cmd->size == size) &&
258
				(cmd->type == type) &&
259
				check_cmd_bo(ring, cmd, target_cmd->ring_bo))
260
			return FALSE;
Rob Clark's avatar
Rob Clark committed
261
262
263
	}

	/* create cmd buf if not: */
264
	i = APPEND(&msm_ring->submit, cmds);
265
266
	APPEND(msm_ring, cmds);
	msm_ring->cmds[i] = target_cmd;
267
268
	cmd = &msm_ring->submit.cmds[i];
	cmd->type = type;
269
	cmd->submit_idx = bo2idx(ring, target_cmd->ring_bo, FD_RELOC_READ);
270
271
272
	cmd->submit_offset = submit_offset;
	cmd->size = size;
	cmd->pad = 0;
273
274

	target_cmd->size = size;
275
276

	return TRUE;
Rob Clark's avatar
Rob Clark committed
277
278
279
280
}

static void * msm_ringbuffer_hostptr(struct fd_ringbuffer *ring)
{
281
	return fd_bo_map(current_cmd(ring)->ring_bo);
Rob Clark's avatar
Rob Clark committed
282
283
}

284
static uint32_t find_next_reloc_idx(struct msm_cmd *msm_cmd,
Rob Clark's avatar
Rob Clark committed
285
286
287
288
289
		uint32_t start, uint32_t offset)
{
	uint32_t i;

	/* a binary search would be more clever.. */
290
291
	for (i = start; i < msm_cmd->nr_relocs; i++) {
		struct drm_msm_gem_submit_reloc *reloc = &msm_cmd->relocs[i];
Rob Clark's avatar
Rob Clark committed
292
293
294
295
296
297
298
		if (reloc->submit_offset >= offset)
			return i;
	}

	return i;
}

299
300
301
302
303
304
305
306
307
static void delete_cmds(struct msm_ringbuffer *msm_ring)
{
	struct msm_cmd *cmd, *tmp;

	LIST_FOR_EACH_ENTRY_SAFE(cmd, tmp, &msm_ring->cmd_list, list) {
		ring_cmd_del(cmd);
	}
}

308
309
310
311
312
static void flush_reset(struct fd_ringbuffer *ring)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	unsigned i;

313
314
	for (i = 0; i < msm_ring->nr_bos; i++) {
		struct msm_bo *msm_bo = to_msm_bo(msm_ring->bos[i]);
315
316
		if (!msm_bo)
			continue;
317
		msm_bo->current_ring_seqno = 0;
318
319
320
		fd_bo_del(&msm_bo->base);
	}

321
	/* for each of the cmd buffers, clear their reloc's: */
322
	for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
323
		struct msm_cmd *target_cmd = msm_ring->cmds[i];
324
		if (!target_cmd)
Rob Clark's avatar
Rob Clark committed
325
			continue;
326
		target_cmd->nr_relocs = 0;
327
328
	}

329
330
	msm_ring->submit.nr_cmds = 0;
	msm_ring->submit.nr_bos = 0;
331
	msm_ring->nr_cmds = 0;
332
	msm_ring->nr_bos = 0;
333

334
335
336
337
338
	if (msm_ring->bo_table) {
		drmHashDestroy(msm_ring->bo_table);
		msm_ring->bo_table = NULL;
	}

339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
	if (msm_ring->is_growable) {
		delete_cmds(msm_ring);
	} else {
		/* in old mode, just reset the # of relocs: */
		current_cmd(ring)->nr_relocs = 0;
	}
}

static void finalize_current_cmd(struct fd_ringbuffer *ring, uint32_t *last_start)
{
	uint32_t submit_offset, size, type;
	struct fd_ringbuffer *parent;

	if (ring->parent) {
		parent = ring->parent;
		type = MSM_SUBMIT_CMD_IB_TARGET_BUF;
	} else {
		parent = ring;
		type = MSM_SUBMIT_CMD_BUF;
	}

	submit_offset = offset_bytes(last_start, ring->start);
	size = offset_bytes(ring->cur, last_start);

	get_cmd(parent, current_cmd(ring), submit_offset, size, type);
364
365
}

366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
static void dump_submit(struct msm_ringbuffer *msm_ring)
{
	uint32_t i, j;

	for (i = 0; i < msm_ring->submit.nr_bos; i++) {
		struct drm_msm_gem_submit_bo *bo = &msm_ring->submit.bos[i];
		ERROR_MSG("  bos[%d]: handle=%u, flags=%x", i, bo->handle, bo->flags);
	}
	for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
		struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
		struct drm_msm_gem_submit_reloc *relocs = U642VOID(cmd->relocs);
		ERROR_MSG("  cmd[%d]: type=%u, submit_idx=%u, submit_offset=%u, size=%u",
				i, cmd->type, cmd->submit_idx, cmd->submit_offset, cmd->size);
		for (j = 0; j < cmd->nr_relocs; j++) {
			struct drm_msm_gem_submit_reloc *r = &relocs[j];
			ERROR_MSG("    reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
					", reloc_offset=%"PRIu64, j, r->submit_offset, r->or, r->shift,
					r->reloc_idx, r->reloc_offset);
		}
	}
}

Rob Clark's avatar
Rob Clark committed
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
static struct drm_msm_gem_submit_reloc *
handle_stateobj_relocs(struct fd_ringbuffer *parent, struct fd_ringbuffer *stateobj,
		struct drm_msm_gem_submit_reloc *orig_relocs, unsigned nr_relocs)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(stateobj);
	struct drm_msm_gem_submit_reloc *relocs = malloc(nr_relocs * sizeof(*relocs));
	unsigned i;

	for (i = 0; i < nr_relocs; i++) {
		unsigned idx = orig_relocs[i].reloc_idx;
		struct fd_bo *bo = msm_ring->bos[idx];
		unsigned flags = 0;

		if (msm_ring->submit.bos[idx].flags & MSM_SUBMIT_BO_READ)
			flags |= FD_RELOC_READ;
		if (msm_ring->submit.bos[idx].flags & MSM_SUBMIT_BO_WRITE)
			flags |= FD_RELOC_WRITE;

		relocs[i] = orig_relocs[i];
		relocs[i].reloc_idx = bo2idx(parent, bo, flags);
	}

	return relocs;
}

Rob Clark's avatar
Rob Clark committed
413
414
static int msm_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start,
		int in_fence_fd, int *out_fence_fd)
Rob Clark's avatar
Rob Clark committed
415
416
417
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	struct drm_msm_gem_submit req = {
Rob Clark's avatar
Rob Clark committed
418
			.flags = to_msm_pipe(ring->pipe)->pipe,
419
			.queueid = to_msm_pipe(ring->pipe)->queue_id,
Rob Clark's avatar
Rob Clark committed
420
	};
421
	uint32_t i;
422
	int ret;
Rob Clark's avatar
Rob Clark committed
423

Rob Clark's avatar
Rob Clark committed
424
425
	assert(!ring->parent);

Rob Clark's avatar
Rob Clark committed
426
427
428
429
430
431
432
433
434
	if (in_fence_fd != -1) {
		req.flags |= MSM_SUBMIT_FENCE_FD_IN | MSM_SUBMIT_NO_IMPLICIT;
		req.fence_fd = in_fence_fd;
	}

	if (out_fence_fd) {
		req.flags |= MSM_SUBMIT_FENCE_FD_OUT;
	}

435
	finalize_current_cmd(ring, last_start);
Rob Clark's avatar
Rob Clark committed
436
437

	/* for each of the cmd's fix up their reloc's: */
438
439
	for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
		struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
440
441
442
		struct msm_cmd *msm_cmd = msm_ring->cmds[i];
		uint32_t a = find_next_reloc_idx(msm_cmd, 0, cmd->submit_offset);
		uint32_t b = find_next_reloc_idx(msm_cmd, a, cmd->submit_offset + cmd->size);
Rob Clark's avatar
Rob Clark committed
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
		struct drm_msm_gem_submit_reloc *relocs = &msm_cmd->relocs[a];
		unsigned nr_relocs = (b > a) ? b - a : 0;

		/* for reusable stateobjs, the reloc table has reloc_idx that
		 * points into it's own private bos table, rather than the global
		 * bos table used for the submit, so we need to add the stateobj's
		 * bos to the global table and construct new relocs table with
		 * corresponding reloc_idx
		 */
		if (msm_cmd->ring->flags & FD_RINGBUFFER_OBJECT) {
			relocs = handle_stateobj_relocs(ring, msm_cmd->ring,
					relocs, nr_relocs);
		}

		cmd->relocs = VOID2U64(relocs);
		cmd->nr_relocs = nr_relocs;
Rob Clark's avatar
Rob Clark committed
459
460
	}

Rob Clark's avatar
Rob Clark committed
461
462
463
464
465
466
	/* needs to be after get_cmd() as that could create bos/cmds table: */
	req.bos = VOID2U64(msm_ring->submit.bos),
	req.nr_bos = msm_ring->submit.nr_bos;
	req.cmds = VOID2U64(msm_ring->submit.cmds),
	req.nr_cmds = msm_ring->submit.nr_cmds;

Rob Clark's avatar
Rob Clark committed
467
	DEBUG_MSG("nr_cmds=%u, nr_bos=%u", req.nr_cmds, req.nr_bos);
Rob Clark's avatar
Rob Clark committed
468
469
470

	ret = drmCommandWriteRead(ring->pipe->dev->fd, DRM_MSM_GEM_SUBMIT,
			&req, sizeof(req));
471
	if (ret) {
Rob Clark's avatar
Rob Clark committed
472
		ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
473
		dump_submit(msm_ring);
474
	} else if (!ret) {
475
		/* update timestamp on all rings associated with submit: */
476
		for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
477
478
			struct msm_cmd *msm_cmd = msm_ring->cmds[i];
			msm_cmd->ring->last_timestamp = req.fence;
479
		}
Rob Clark's avatar
Rob Clark committed
480
481
482
483

		if (out_fence_fd) {
			*out_fence_fd = req.fence_fd;
		}
484
	}
Rob Clark's avatar
Rob Clark committed
485

Rob Clark's avatar
Rob Clark committed
486
487
488
489
490
	/* free dynamically constructed stateobj relocs tables: */
	for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
		struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
		struct msm_cmd *msm_cmd = msm_ring->cmds[i];
		if (msm_cmd->ring->flags & FD_RINGBUFFER_OBJECT) {
491
492
493
494
495
496
497
498
499
500
			/* we could have dropped last reference: */
			msm_ring->cmds[i] = NULL;

			/* need to drop ring_bo ref prior to unref'ing the ring,
			 * because ring_bo_del assumes it is dropping the *last*
			 * reference:
			 */
			fd_bo_del(msm_ring->bos[cmd->submit_idx]);
			msm_ring->bos[cmd->submit_idx] = NULL;

Rob Clark's avatar
Rob Clark committed
501
502
503
504
505
			msm_ringbuffer_unref(msm_cmd->ring);
			free(U642VOID(cmd->relocs));
		}
	}

506
	flush_reset(ring);
Rob Clark's avatar
Rob Clark committed
507
508
509
510

	return ret;
}

511
512
513
514
515
516
517
static void msm_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t size)
{
	assert(to_msm_ringbuffer(ring)->is_growable);
	finalize_current_cmd(ring, ring->last_start);
	ring_cmd_new(ring, size);
}

518
519
520
521
522
static void msm_ringbuffer_reset(struct fd_ringbuffer *ring)
{
	flush_reset(ring);
}

Rob Clark's avatar
Rob Clark committed
523
524
525
static void msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
		const struct fd_reloc *r)
{
526
	struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
Rob Clark's avatar
Rob Clark committed
527
528
	struct msm_bo *msm_bo = to_msm_bo(r->bo);
	struct drm_msm_gem_submit_reloc *reloc;
529
530
	struct msm_cmd *cmd = current_cmd(ring);
	uint32_t idx = APPEND(cmd, relocs);
Rob Clark's avatar
Rob Clark committed
531
532
	uint32_t addr;

533
	reloc = &cmd->relocs[idx];
Rob Clark's avatar
Rob Clark committed
534

535
	reloc->reloc_idx = bo2idx(parent, r->bo, r->flags);
Rob Clark's avatar
Rob Clark committed
536
537
538
539
540
541
	reloc->reloc_offset = r->offset;
	reloc->or = r->or;
	reloc->shift = r->shift;
	reloc->submit_offset = offset_bytes(ring->cur, ring->start);

	addr = msm_bo->presumed;
Rob Clark's avatar
Rob Clark committed
542
543
	if (reloc->shift < 0)
		addr >>= -reloc->shift;
Rob Clark's avatar
Rob Clark committed
544
	else
Rob Clark's avatar
Rob Clark committed
545
		addr <<= reloc->shift;
Rob Clark's avatar
Rob Clark committed
546
	(*ring->cur++) = addr | r->or;
Rob Clark's avatar
Rob Clark committed
547
548
549
550

	if (ring->pipe->gpu_id >= 500) {
		struct drm_msm_gem_submit_reloc *reloc_hi;

551
552
553
554
555
		/* NOTE: grab reloc_idx *before* APPEND() since that could
		 * realloc() meaning that 'reloc' ptr is no longer valid:
		 */
		uint32_t reloc_idx = reloc->reloc_idx;

Rob Clark's avatar
Rob Clark committed
556
557
558
559
		idx = APPEND(cmd, relocs);

		reloc_hi = &cmd->relocs[idx];

560
		reloc_hi->reloc_idx = reloc_idx;
Rob Clark's avatar
Rob Clark committed
561
562
563
564
565
566
567
568
569
570
571
572
		reloc_hi->reloc_offset = r->offset;
		reloc_hi->or = r->orhi;
		reloc_hi->shift = r->shift - 32;
		reloc_hi->submit_offset = offset_bytes(ring->cur, ring->start);

		addr = msm_bo->presumed >> 32;
		if (reloc_hi->shift < 0)
			addr >>= -reloc_hi->shift;
		else
			addr <<= reloc_hi->shift;
		(*ring->cur++) = addr | r->orhi;
	}
Rob Clark's avatar
Rob Clark committed
573
574
}

575
576
static uint32_t msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
		struct fd_ringbuffer *target, uint32_t cmd_idx,
577
		uint32_t submit_offset, uint32_t size)
Rob Clark's avatar
Rob Clark committed
578
{
579
580
	struct msm_cmd *cmd = NULL;
	uint32_t idx = 0;
581
	int added_cmd = FALSE;
582
583
584
585
586
587

	LIST_FOR_EACH_ENTRY(cmd, &to_msm_ringbuffer(target)->cmd_list, list) {
		if (idx == cmd_idx)
			break;
		idx++;
	}
Rob Clark's avatar
Rob Clark committed
588

589
590
591
592
593
594
595
596
597
598
	assert(cmd && (idx == cmd_idx));

	if (idx < (to_msm_ringbuffer(target)->cmd_count - 1)) {
		/* All but the last cmd buffer is fully "baked" (ie. already has
		 * done get_cmd() to add it to the cmds table).  But in this case,
		 * the size we get is invalid (since it is calculated from the
		 * last cmd buffer):
		 */
		size = cmd->size;
	} else {
Rob Clark's avatar
Rob Clark committed
599
		struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
600
601
		added_cmd = get_cmd(parent, cmd, submit_offset, size,
				MSM_SUBMIT_CMD_IB_TARGET_BUF);
602
	}
Rob Clark's avatar
Rob Clark committed
603
604

	msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){
605
		.bo = cmd->ring_bo,
Rob Clark's avatar
Rob Clark committed
606
607
608
		.flags = FD_RELOC_READ,
		.offset = submit_offset,
	});
609

Rob Clark's avatar
Rob Clark committed
610
611
612
613
	/* Unlike traditional ringbuffers which are deleted as a set (after
	 * being flushed), mesa can't really guarantee that a stateobj isn't
	 * destroyed after emitted but before flush, so we must hold a ref:
	 */
614
	if (added_cmd && (target->flags & FD_RINGBUFFER_OBJECT)) {
Rob Clark's avatar
Rob Clark committed
615
616
617
		msm_ringbuffer_ref(target);
	}

618
619
620
621
622
623
	return size;
}

static uint32_t msm_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
{
	return to_msm_ringbuffer(ring)->cmd_count;
Rob Clark's avatar
Rob Clark committed
624
625
}

Rob Clark's avatar
Rob Clark committed
626
static void msm_ringbuffer_unref(struct fd_ringbuffer *ring)
Rob Clark's avatar
Rob Clark committed
627
628
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
629

Rob Clark's avatar
Rob Clark committed
630
631
632
	if (!atomic_dec_and_test(&msm_ring->refcnt))
		return;

633
634
635
	flush_reset(ring);
	delete_cmds(msm_ring);

636
637
638
	free(msm_ring->submit.cmds);
	free(msm_ring->submit.bos);
	free(msm_ring->bos);
639
	free(msm_ring->cmds);
Rob Clark's avatar
Rob Clark committed
640
641
642
	free(msm_ring);
}

Rob Clark's avatar
Rob Clark committed
643
644
645
646
647
648
static void msm_ringbuffer_ref(struct fd_ringbuffer *ring)
{
	struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
	atomic_inc(&msm_ring->refcnt);
}

649
static const struct fd_ringbuffer_funcs funcs = {
Rob Clark's avatar
Rob Clark committed
650
651
		.hostptr = msm_ringbuffer_hostptr,
		.flush = msm_ringbuffer_flush,
652
		.grow = msm_ringbuffer_grow,
653
		.reset = msm_ringbuffer_reset,
Rob Clark's avatar
Rob Clark committed
654
655
		.emit_reloc = msm_ringbuffer_emit_reloc,
		.emit_reloc_ring = msm_ringbuffer_emit_reloc_ring,
656
		.cmd_count = msm_ringbuffer_cmd_count,
Rob Clark's avatar
Rob Clark committed
657
		.destroy = msm_ringbuffer_unref,
Rob Clark's avatar
Rob Clark committed
658
659
};

660
drm_private struct fd_ringbuffer * msm_ringbuffer_new(struct fd_pipe *pipe,
661
		uint32_t size, enum fd_ringbuffer_flags flags)
Rob Clark's avatar
Rob Clark committed
662
663
{
	struct msm_ringbuffer *msm_ring;
664
	struct fd_ringbuffer *ring;
Rob Clark's avatar
Rob Clark committed
665
666
667
668

	msm_ring = calloc(1, sizeof(*msm_ring));
	if (!msm_ring) {
		ERROR_MSG("allocation failed");
669
		return NULL;
Rob Clark's avatar
Rob Clark committed
670
671
	}

672
673
674
675
676
677
678
	if (size == 0) {
		assert(pipe->dev->version >= FD_VERSION_UNLIMITED_CMDS);
		size = INIT_SIZE;
		msm_ring->is_growable = TRUE;
	}

	list_inithead(&msm_ring->cmd_list);
679
	msm_ring->seqno = ++to_msm_device(pipe->dev)->ring_cnt;
Rob Clark's avatar
Rob Clark committed
680
	atomic_set(&msm_ring->refcnt, 1);
681

Rob Clark's avatar
Rob Clark committed
682
683
	ring = &msm_ring->base;
	ring->funcs = &funcs;
684
	ring->size = size;
685
	ring->pipe = pipe;   /* needed in ring_cmd_new() */
Rob Clark's avatar
Rob Clark committed
686

687
	ring_cmd_new(ring, size);
Rob Clark's avatar
Rob Clark committed
688
689
690

	return ring;
}