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