freedreno/msm: simplify emit_reloc_ring() vfunc
[platform/upstream/libdrm.git] / freedreno / msm / msm_ringbuffer.c
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 #include <inttypes.h>
31
32 #include "xf86atomic.h"
33 #include "freedreno_ringbuffer.h"
34 #include "msm_priv.h"
35
36 /* represents a single cmd buffer in the submit ioctl.  Each cmd buffer has
37  * a backing bo, and a reloc table.
38  */
39 struct msm_cmd {
40         struct list_head list;
41
42         struct fd_ringbuffer *ring;
43         struct fd_bo *ring_bo;
44
45         /* reloc's table: */
46         DECLARE_ARRAY(struct drm_msm_gem_submit_reloc, relocs);
47
48         uint32_t size;
49 };
50
51 struct msm_ringbuffer {
52         struct fd_ringbuffer base;
53
54         /* submit ioctl related tables:
55          * Note that bos and cmds are tracked by the parent ringbuffer, since
56          * that is global to the submit ioctl call.  The reloc's table is tracked
57          * per cmd-buffer.
58          */
59         struct {
60                 /* bo's table: */
61                 DECLARE_ARRAY(struct drm_msm_gem_submit_bo, bos);
62
63                 /* cmd's table: */
64                 DECLARE_ARRAY(struct drm_msm_gem_submit_cmd, cmds);
65         } submit;
66
67         /* should have matching entries in submit.bos: */
68         /* Note, only in parent ringbuffer */
69         DECLARE_ARRAY(struct fd_bo *, bos);
70
71         /* should have matching entries in submit.cmds: */
72         DECLARE_ARRAY(struct msm_cmd *, cmds);
73
74         /* List of physical cmdstream buffers (msm_cmd) assocated with this
75          * logical fd_ringbuffer.
76          *
77          * Note that this is different from msm_ringbuffer::cmds (which
78          * shadows msm_ringbuffer::submit::cmds for tracking submit ioctl
79          * related stuff, and *only* is tracked in the parent ringbuffer.
80          * And only has "completed" cmd buffers (ie. we already know the
81          * size) added via get_cmd().
82          */
83         struct list_head cmd_list;
84
85         int is_growable;
86         unsigned cmd_count;
87
88         unsigned offset;    /* for sub-allocated stateobj rb's */
89
90         unsigned seqno;
91
92         /* maps fd_bo to idx: */
93         void *bo_table;
94 };
95
96 static inline struct msm_ringbuffer * to_msm_ringbuffer(struct fd_ringbuffer *x)
97 {
98         return (struct msm_ringbuffer *)x;
99 }
100
101 #define INIT_SIZE 0x1000
102
103 static pthread_mutex_t idx_lock = PTHREAD_MUTEX_INITIALIZER;
104
105 static struct msm_cmd *current_cmd(struct fd_ringbuffer *ring)
106 {
107         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
108         assert(!LIST_IS_EMPTY(&msm_ring->cmd_list));
109         return LIST_LAST_ENTRY(&msm_ring->cmd_list, struct msm_cmd, list);
110 }
111
112 static void ring_cmd_del(struct msm_cmd *cmd)
113 {
114         fd_bo_del(cmd->ring_bo);
115         list_del(&cmd->list);
116         to_msm_ringbuffer(cmd->ring)->cmd_count--;
117         free(cmd->relocs);
118         free(cmd);
119 }
120
121 static struct msm_cmd * ring_cmd_new(struct fd_ringbuffer *ring, uint32_t size,
122                 enum fd_ringbuffer_flags flags)
123 {
124         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
125         struct msm_cmd *cmd = calloc(1, sizeof(*cmd));
126
127         if (!cmd)
128                 return NULL;
129
130         cmd->ring = ring;
131
132         /* TODO separate suballoc buffer for small non-streaming state, using
133          * smaller page-sized backing bo's.
134          */
135         if (flags & FD_RINGBUFFER_STREAMING) {
136                 struct msm_pipe *msm_pipe = to_msm_pipe(ring->pipe);
137                 unsigned suballoc_offset = 0;
138                 struct fd_bo *suballoc_bo = NULL;
139
140                 if (msm_pipe->suballoc_ring) {
141                         struct msm_ringbuffer *suballoc_ring = to_msm_ringbuffer(msm_pipe->suballoc_ring);
142
143                         assert(msm_pipe->suballoc_ring->flags & FD_RINGBUFFER_OBJECT);
144                         assert(suballoc_ring->cmd_count == 1);
145
146                         suballoc_bo = current_cmd(msm_pipe->suballoc_ring)->ring_bo;
147
148                         suballoc_offset = fd_ringbuffer_size(msm_pipe->suballoc_ring) +
149                                         suballoc_ring->offset;
150
151                         suballoc_offset = ALIGN(suballoc_offset, 0x10);
152
153                         if ((size + suballoc_offset) > suballoc_bo->size) {
154                                 suballoc_bo = NULL;
155                         }
156                 }
157
158                 if (!suballoc_bo) {
159                         cmd->ring_bo = fd_bo_new_ring(ring->pipe->dev, 0x8000, 0);
160                         msm_ring->offset = 0;
161                 } else {
162                         cmd->ring_bo = fd_bo_ref(suballoc_bo);
163                         msm_ring->offset = suballoc_offset;
164                 }
165
166                 if (msm_pipe->suballoc_ring)
167                         fd_ringbuffer_del(msm_pipe->suballoc_ring);
168
169                 msm_pipe->suballoc_ring = fd_ringbuffer_ref(ring);
170         } else {
171                 cmd->ring_bo = fd_bo_new_ring(ring->pipe->dev, size, 0);
172         }
173         if (!cmd->ring_bo)
174                 goto fail;
175
176         list_addtail(&cmd->list, &msm_ring->cmd_list);
177         msm_ring->cmd_count++;
178
179         return cmd;
180
181 fail:
182         ring_cmd_del(cmd);
183         return NULL;
184 }
185
186 static uint32_t append_bo(struct fd_ringbuffer *ring, struct fd_bo *bo)
187 {
188         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
189         uint32_t idx;
190
191         idx = APPEND(&msm_ring->submit, bos);
192         idx = APPEND(msm_ring, bos);
193
194         msm_ring->submit.bos[idx].flags = 0;
195         msm_ring->submit.bos[idx].handle = bo->handle;
196         msm_ring->submit.bos[idx].presumed = to_msm_bo(bo)->presumed;
197
198         msm_ring->bos[idx] = fd_bo_ref(bo);
199
200         return idx;
201 }
202
203 /* add (if needed) bo, return idx: */
204 static uint32_t bo2idx(struct fd_ringbuffer *ring, struct fd_bo *bo, uint32_t flags)
205 {
206         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
207         struct msm_bo *msm_bo = to_msm_bo(bo);
208         uint32_t idx;
209         pthread_mutex_lock(&idx_lock);
210         if (msm_bo->current_ring_seqno == msm_ring->seqno) {
211                 idx = msm_bo->idx;
212         } else {
213                 void *val;
214
215                 if (!msm_ring->bo_table)
216                         msm_ring->bo_table = drmHashCreate();
217
218                 if (!drmHashLookup(msm_ring->bo_table, bo->handle, &val)) {
219                         /* found */
220                         idx = (uint32_t)(uintptr_t)val;
221                 } else {
222                         idx = append_bo(ring, bo);
223                         val = (void *)(uintptr_t)idx;
224                         drmHashInsert(msm_ring->bo_table, bo->handle, val);
225                 }
226                 msm_bo->current_ring_seqno = msm_ring->seqno;
227                 msm_bo->idx = idx;
228         }
229         pthread_mutex_unlock(&idx_lock);
230         if (flags & FD_RELOC_READ)
231                 msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_READ;
232         if (flags & FD_RELOC_WRITE)
233                 msm_ring->submit.bos[idx].flags |= MSM_SUBMIT_BO_WRITE;
234         return idx;
235 }
236
237 static int check_cmd_bo(struct fd_ringbuffer *ring,
238                 struct drm_msm_gem_submit_cmd *cmd, struct fd_bo *bo)
239 {
240         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
241         return msm_ring->submit.bos[cmd->submit_idx].handle == bo->handle;
242 }
243
244 /* Ensure that submit has corresponding entry in cmds table for the
245  * target cmdstream buffer:
246  *
247  * Returns TRUE if new cmd added (else FALSE if it was already in
248  * the cmds table)
249  */
250 static int get_cmd(struct fd_ringbuffer *ring, struct msm_cmd *target_cmd,
251                 uint32_t submit_offset, uint32_t size, uint32_t type)
252 {
253         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
254         struct drm_msm_gem_submit_cmd *cmd;
255         uint32_t i;
256
257         /* figure out if we already have a cmd buf: */
258         for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
259                 cmd = &msm_ring->submit.cmds[i];
260                 if ((cmd->submit_offset == submit_offset) &&
261                                 (cmd->size == size) &&
262                                 (cmd->type == type) &&
263                                 check_cmd_bo(ring, cmd, target_cmd->ring_bo))
264                         return FALSE;
265         }
266
267         /* create cmd buf if not: */
268         i = APPEND(&msm_ring->submit, cmds);
269         APPEND(msm_ring, cmds);
270         msm_ring->cmds[i] = target_cmd;
271         cmd = &msm_ring->submit.cmds[i];
272         cmd->type = type;
273         cmd->submit_idx = bo2idx(ring, target_cmd->ring_bo, FD_RELOC_READ);
274         cmd->submit_offset = submit_offset;
275         cmd->size = size;
276         cmd->pad = 0;
277
278         target_cmd->size = size;
279
280         return TRUE;
281 }
282
283 static void * msm_ringbuffer_hostptr(struct fd_ringbuffer *ring)
284 {
285         struct msm_cmd *cmd = current_cmd(ring);
286         uint8_t *base = fd_bo_map(cmd->ring_bo);
287         return base + to_msm_ringbuffer(ring)->offset;
288 }
289
290 static uint32_t find_next_reloc_idx(struct msm_cmd *msm_cmd,
291                 uint32_t start, uint32_t offset)
292 {
293         uint32_t i;
294
295         /* a binary search would be more clever.. */
296         for (i = start; i < msm_cmd->nr_relocs; i++) {
297                 struct drm_msm_gem_submit_reloc *reloc = &msm_cmd->relocs[i];
298                 if (reloc->submit_offset >= offset)
299                         return i;
300         }
301
302         return i;
303 }
304
305 static void delete_cmds(struct msm_ringbuffer *msm_ring)
306 {
307         struct msm_cmd *cmd, *tmp;
308
309         LIST_FOR_EACH_ENTRY_SAFE(cmd, tmp, &msm_ring->cmd_list, list) {
310                 ring_cmd_del(cmd);
311         }
312 }
313
314 static void flush_reset(struct fd_ringbuffer *ring)
315 {
316         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
317         unsigned i;
318
319         for (i = 0; i < msm_ring->nr_bos; i++) {
320                 struct msm_bo *msm_bo = to_msm_bo(msm_ring->bos[i]);
321                 if (!msm_bo)
322                         continue;
323                 msm_bo->current_ring_seqno = 0;
324                 fd_bo_del(&msm_bo->base);
325         }
326
327         msm_ring->submit.nr_cmds = 0;
328         msm_ring->submit.nr_bos = 0;
329         msm_ring->nr_cmds = 0;
330         msm_ring->nr_bos = 0;
331
332         if (msm_ring->bo_table) {
333                 drmHashDestroy(msm_ring->bo_table);
334                 msm_ring->bo_table = NULL;
335         }
336
337         if (msm_ring->is_growable) {
338                 delete_cmds(msm_ring);
339         } else {
340                 /* in old mode, just reset the # of relocs: */
341                 current_cmd(ring)->nr_relocs = 0;
342         }
343 }
344
345 static void finalize_current_cmd(struct fd_ringbuffer *ring, uint32_t *last_start)
346 {
347         uint32_t submit_offset, size, type;
348         struct fd_ringbuffer *parent;
349
350         if (ring->parent) {
351                 parent = ring->parent;
352                 type = MSM_SUBMIT_CMD_IB_TARGET_BUF;
353         } else {
354                 parent = ring;
355                 type = MSM_SUBMIT_CMD_BUF;
356         }
357
358         submit_offset = offset_bytes(last_start, ring->start);
359         size = offset_bytes(ring->cur, last_start);
360
361         get_cmd(parent, current_cmd(ring), submit_offset, size, type);
362 }
363
364 static void dump_submit(struct msm_ringbuffer *msm_ring)
365 {
366         uint32_t i, j;
367
368         for (i = 0; i < msm_ring->submit.nr_bos; i++) {
369                 struct drm_msm_gem_submit_bo *bo = &msm_ring->submit.bos[i];
370                 ERROR_MSG("  bos[%d]: handle=%u, flags=%x", i, bo->handle, bo->flags);
371         }
372         for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
373                 struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
374                 struct drm_msm_gem_submit_reloc *relocs = U642VOID(cmd->relocs);
375                 ERROR_MSG("  cmd[%d]: type=%u, submit_idx=%u, submit_offset=%u, size=%u",
376                                 i, cmd->type, cmd->submit_idx, cmd->submit_offset, cmd->size);
377                 for (j = 0; j < cmd->nr_relocs; j++) {
378                         struct drm_msm_gem_submit_reloc *r = &relocs[j];
379                         ERROR_MSG("    reloc[%d]: submit_offset=%u, or=%08x, shift=%d, reloc_idx=%u"
380                                         ", reloc_offset=%"PRIu64, j, r->submit_offset, r->or, r->shift,
381                                         r->reloc_idx, r->reloc_offset);
382                 }
383         }
384 }
385
386 static struct drm_msm_gem_submit_reloc *
387 handle_stateobj_relocs(struct fd_ringbuffer *parent, struct fd_ringbuffer *stateobj,
388                 struct drm_msm_gem_submit_reloc *orig_relocs, unsigned nr_relocs)
389 {
390         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(stateobj);
391         struct drm_msm_gem_submit_reloc *relocs = malloc(nr_relocs * sizeof(*relocs));
392         unsigned i;
393
394         for (i = 0; i < nr_relocs; i++) {
395                 unsigned idx = orig_relocs[i].reloc_idx;
396                 struct fd_bo *bo = msm_ring->bos[idx];
397                 unsigned flags = 0;
398
399                 if (msm_ring->submit.bos[idx].flags & MSM_SUBMIT_BO_READ)
400                         flags |= FD_RELOC_READ;
401                 if (msm_ring->submit.bos[idx].flags & MSM_SUBMIT_BO_WRITE)
402                         flags |= FD_RELOC_WRITE;
403
404                 relocs[i] = orig_relocs[i];
405                 relocs[i].reloc_idx = bo2idx(parent, bo, flags);
406         }
407
408         return relocs;
409 }
410
411 static int msm_ringbuffer_flush(struct fd_ringbuffer *ring, uint32_t *last_start,
412                 int in_fence_fd, int *out_fence_fd)
413 {
414         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
415         struct msm_pipe *msm_pipe = to_msm_pipe(ring->pipe);
416         struct drm_msm_gem_submit req = {
417                         .flags = msm_pipe->pipe,
418                         .queueid = msm_pipe->queue_id,
419         };
420         uint32_t i;
421         int ret;
422
423         assert(!ring->parent);
424
425         if (in_fence_fd != -1) {
426                 req.flags |= MSM_SUBMIT_FENCE_FD_IN | MSM_SUBMIT_NO_IMPLICIT;
427                 req.fence_fd = in_fence_fd;
428         }
429
430         if (out_fence_fd) {
431                 req.flags |= MSM_SUBMIT_FENCE_FD_OUT;
432         }
433
434         finalize_current_cmd(ring, last_start);
435
436         /* for each of the cmd's fix up their reloc's: */
437         for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
438                 struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
439                 struct msm_cmd *msm_cmd = msm_ring->cmds[i];
440                 uint32_t a = find_next_reloc_idx(msm_cmd, 0, cmd->submit_offset);
441                 uint32_t b = find_next_reloc_idx(msm_cmd, a, cmd->submit_offset + cmd->size);
442                 struct drm_msm_gem_submit_reloc *relocs = &msm_cmd->relocs[a];
443                 unsigned nr_relocs = (b > a) ? b - a : 0;
444
445                 /* for reusable stateobjs, the reloc table has reloc_idx that
446                  * points into it's own private bos table, rather than the global
447                  * bos table used for the submit, so we need to add the stateobj's
448                  * bos to the global table and construct new relocs table with
449                  * corresponding reloc_idx
450                  */
451                 if (msm_cmd->ring->flags & FD_RINGBUFFER_OBJECT) {
452                         relocs = handle_stateobj_relocs(ring, msm_cmd->ring,
453                                         relocs, nr_relocs);
454                 }
455
456                 cmd->relocs = VOID2U64(relocs);
457                 cmd->nr_relocs = nr_relocs;
458         }
459
460         /* needs to be after get_cmd() as that could create bos/cmds table: */
461         req.bos = VOID2U64(msm_ring->submit.bos),
462         req.nr_bos = msm_ring->submit.nr_bos;
463         req.cmds = VOID2U64(msm_ring->submit.cmds),
464         req.nr_cmds = msm_ring->submit.nr_cmds;
465
466         DEBUG_MSG("nr_cmds=%u, nr_bos=%u", req.nr_cmds, req.nr_bos);
467
468         ret = drmCommandWriteRead(ring->pipe->dev->fd, DRM_MSM_GEM_SUBMIT,
469                         &req, sizeof(req));
470         if (ret) {
471                 ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
472                 dump_submit(msm_ring);
473         } else if (!ret) {
474                 /* update timestamp on all rings associated with submit: */
475                 for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
476                         struct msm_cmd *msm_cmd = msm_ring->cmds[i];
477                         msm_cmd->ring->last_timestamp = req.fence;
478                 }
479
480                 if (out_fence_fd) {
481                         *out_fence_fd = req.fence_fd;
482                 }
483         }
484
485         /* free dynamically constructed stateobj relocs tables: */
486         for (i = 0; i < msm_ring->submit.nr_cmds; i++) {
487                 struct drm_msm_gem_submit_cmd *cmd = &msm_ring->submit.cmds[i];
488                 struct msm_cmd *msm_cmd = msm_ring->cmds[i];
489                 if (msm_cmd->ring->flags & FD_RINGBUFFER_OBJECT) {
490                         /* we could have dropped last reference: */
491                         msm_ring->cmds[i] = NULL;
492                         fd_ringbuffer_del(msm_cmd->ring);
493                         free(U642VOID(cmd->relocs));
494                 }
495         }
496
497         flush_reset(ring);
498
499         return ret;
500 }
501
502 static void msm_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t size)
503 {
504         assert(to_msm_ringbuffer(ring)->is_growable);
505         finalize_current_cmd(ring, ring->last_start);
506         ring_cmd_new(ring, size, 0);
507 }
508
509 static void msm_ringbuffer_reset(struct fd_ringbuffer *ring)
510 {
511         flush_reset(ring);
512 }
513
514 static void msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
515                 const struct fd_reloc *r)
516 {
517         struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
518         struct msm_bo *msm_bo = to_msm_bo(r->bo);
519         struct drm_msm_gem_submit_reloc *reloc;
520         struct msm_cmd *cmd = current_cmd(ring);
521         uint32_t idx = APPEND(cmd, relocs);
522         uint32_t addr;
523
524         reloc = &cmd->relocs[idx];
525
526         reloc->reloc_idx = bo2idx(parent, r->bo, r->flags);
527         reloc->reloc_offset = r->offset;
528         reloc->or = r->or;
529         reloc->shift = r->shift;
530         reloc->submit_offset = offset_bytes(ring->cur, ring->start) +
531                         to_msm_ringbuffer(ring)->offset;
532
533         addr = msm_bo->presumed;
534         if (reloc->shift < 0)
535                 addr >>= -reloc->shift;
536         else
537                 addr <<= reloc->shift;
538         (*ring->cur++) = addr | r->or;
539
540         if (ring->pipe->gpu_id >= 500) {
541                 struct drm_msm_gem_submit_reloc *reloc_hi;
542
543                 /* NOTE: grab reloc_idx *before* APPEND() since that could
544                  * realloc() meaning that 'reloc' ptr is no longer valid:
545                  */
546                 uint32_t reloc_idx = reloc->reloc_idx;
547
548                 idx = APPEND(cmd, relocs);
549
550                 reloc_hi = &cmd->relocs[idx];
551
552                 reloc_hi->reloc_idx = reloc_idx;
553                 reloc_hi->reloc_offset = r->offset;
554                 reloc_hi->or = r->orhi;
555                 reloc_hi->shift = r->shift - 32;
556                 reloc_hi->submit_offset = offset_bytes(ring->cur, ring->start) +
557                                 to_msm_ringbuffer(ring)->offset;
558
559                 addr = msm_bo->presumed >> 32;
560                 if (reloc_hi->shift < 0)
561                         addr >>= -reloc_hi->shift;
562                 else
563                         addr <<= reloc_hi->shift;
564                 (*ring->cur++) = addr | r->orhi;
565         }
566 }
567
568 static uint32_t msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
569                 struct fd_ringbuffer *target, uint32_t cmd_idx)
570 {
571         struct msm_cmd *cmd = NULL;
572         struct msm_ringbuffer *msm_target = to_msm_ringbuffer(target);
573         uint32_t idx = 0;
574         int added_cmd = FALSE;
575         uint32_t size;
576         uint32_t submit_offset = msm_target->offset;
577
578         LIST_FOR_EACH_ENTRY(cmd, &msm_target->cmd_list, list) {
579                 if (idx == cmd_idx)
580                         break;
581                 idx++;
582         }
583
584         assert(cmd && (idx == cmd_idx));
585
586         if (idx < (msm_target->cmd_count - 1)) {
587                 /* All but the last cmd buffer is fully "baked" (ie. already has
588                  * done get_cmd() to add it to the cmds table).  But in this case,
589                  * the size we get is invalid (since it is calculated from the
590                  * last cmd buffer):
591                  */
592                 size = cmd->size;
593         } else {
594                 struct fd_ringbuffer *parent = ring->parent ? ring->parent : ring;
595                 size = offset_bytes(target->cur, target->start);
596                 added_cmd = get_cmd(parent, cmd, submit_offset, size,
597                                 MSM_SUBMIT_CMD_IB_TARGET_BUF);
598         }
599
600         msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){
601                 .bo = cmd->ring_bo,
602                 .flags = FD_RELOC_READ,
603                 .offset = submit_offset,
604         });
605
606         /* Unlike traditional ringbuffers which are deleted as a set (after
607          * being flushed), mesa can't really guarantee that a stateobj isn't
608          * destroyed after emitted but before flush, so we must hold a ref:
609          */
610         if (added_cmd && (target->flags & FD_RINGBUFFER_OBJECT)) {
611                 fd_ringbuffer_ref(target);
612         }
613
614         return size;
615 }
616
617 static uint32_t msm_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
618 {
619         return to_msm_ringbuffer(ring)->cmd_count;
620 }
621
622 static void msm_ringbuffer_destroy(struct fd_ringbuffer *ring)
623 {
624         struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
625
626         flush_reset(ring);
627         delete_cmds(msm_ring);
628
629         free(msm_ring->submit.cmds);
630         free(msm_ring->submit.bos);
631         free(msm_ring->bos);
632         free(msm_ring->cmds);
633         free(msm_ring);
634 }
635
636 static const struct fd_ringbuffer_funcs funcs = {
637                 .hostptr = msm_ringbuffer_hostptr,
638                 .flush = msm_ringbuffer_flush,
639                 .grow = msm_ringbuffer_grow,
640                 .reset = msm_ringbuffer_reset,
641                 .emit_reloc = msm_ringbuffer_emit_reloc,
642                 .emit_reloc_ring = msm_ringbuffer_emit_reloc_ring,
643                 .cmd_count = msm_ringbuffer_cmd_count,
644                 .destroy = msm_ringbuffer_destroy,
645 };
646
647 drm_private struct fd_ringbuffer * msm_ringbuffer_new(struct fd_pipe *pipe,
648                 uint32_t size, enum fd_ringbuffer_flags flags)
649 {
650         struct msm_ringbuffer *msm_ring;
651         struct fd_ringbuffer *ring;
652
653         msm_ring = calloc(1, sizeof(*msm_ring));
654         if (!msm_ring) {
655                 ERROR_MSG("allocation failed");
656                 return NULL;
657         }
658
659         if (size == 0) {
660                 assert(pipe->dev->version >= FD_VERSION_UNLIMITED_CMDS);
661                 size = INIT_SIZE;
662                 msm_ring->is_growable = TRUE;
663         }
664
665         list_inithead(&msm_ring->cmd_list);
666         msm_ring->seqno = ++to_msm_device(pipe->dev)->ring_cnt;
667
668         ring = &msm_ring->base;
669         atomic_set(&ring->refcnt, 1);
670
671         ring->funcs = &funcs;
672         ring->size = size;
673         ring->pipe = pipe;   /* needed in ring_cmd_new() */
674
675         ring_cmd_new(ring, size, flags);
676
677         return ring;
678 }