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