• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #include <assert.h>
28 #include <inttypes.h>
29 
30 #include "util/hash_table.h"
31 #include "util/set.h"
32 #include "util/slab.h"
33 
34 #include "drm/freedreno_ringbuffer.h"
35 #include "msm_priv.h"
36 
37 /* The legacy implementation of submit/ringbuffer, which still does the
38  * traditional reloc and cmd tracking
39  */
40 
41 #define INIT_SIZE 0x1000
42 
43 struct msm_submit {
44    struct fd_submit base;
45 
46    DECLARE_ARRAY(struct drm_msm_gem_submit_bo, submit_bos);
47    DECLARE_ARRAY(struct fd_bo *, bos);
48 
49    /* maps fd_bo to idx in bos table: */
50    struct hash_table *bo_table;
51 
52    struct slab_mempool ring_pool;
53 
54    /* hash-set of associated rings: */
55    struct set *ring_set;
56 
57    /* Allow for sub-allocation of stateobj ring buffers (ie. sharing
58     * the same underlying bo)..
59     *
60     * We also rely on previous stateobj having been fully constructed
61     * so we can reclaim extra space at it's end.
62     */
63    struct fd_ringbuffer *suballoc_ring;
64 };
65 FD_DEFINE_CAST(fd_submit, msm_submit);
66 
67 /* for FD_RINGBUFFER_GROWABLE rb's, tracks the 'finalized' cmdstream buffers
68  * and sizes.  Ie. a finalized buffer can have no more commands appended to
69  * it.
70  */
71 struct msm_cmd {
72    struct fd_bo *ring_bo;
73    unsigned size;
74    DECLARE_ARRAY(struct drm_msm_gem_submit_reloc, relocs);
75 };
76 
77 static struct msm_cmd *
cmd_new(struct fd_bo * ring_bo)78 cmd_new(struct fd_bo *ring_bo)
79 {
80    struct msm_cmd *cmd = malloc(sizeof(*cmd));
81    cmd->ring_bo = fd_bo_ref(ring_bo);
82    cmd->size = 0;
83    cmd->nr_relocs = cmd->max_relocs = 0;
84    cmd->relocs = NULL;
85    return cmd;
86 }
87 
88 static void
cmd_free(struct msm_cmd * cmd)89 cmd_free(struct msm_cmd *cmd)
90 {
91    fd_bo_del(cmd->ring_bo);
92    free(cmd->relocs);
93    free(cmd);
94 }
95 
96 struct msm_ringbuffer {
97    struct fd_ringbuffer base;
98 
99    /* for FD_RINGBUFFER_STREAMING rb's which are sub-allocated */
100    unsigned offset;
101 
102    union {
103       /* for _FD_RINGBUFFER_OBJECT case: */
104       struct {
105          struct fd_pipe *pipe;
106          DECLARE_ARRAY(struct fd_bo *, reloc_bos);
107          struct set *ring_set;
108       };
109       /* for other cases: */
110       struct {
111          struct fd_submit *submit;
112          DECLARE_ARRAY(struct msm_cmd *, cmds);
113       };
114    } u;
115 
116    struct msm_cmd *cmd; /* current cmd */
117    struct fd_bo *ring_bo;
118 };
119 FD_DEFINE_CAST(fd_ringbuffer, msm_ringbuffer);
120 
121 static void finalize_current_cmd(struct fd_ringbuffer *ring);
122 static struct fd_ringbuffer *
123 msm_ringbuffer_init(struct msm_ringbuffer *msm_ring, uint32_t size,
124                     enum fd_ringbuffer_flags flags);
125 
126 /* add (if needed) bo to submit and return index: */
127 static uint32_t
append_bo(struct msm_submit * submit,struct fd_bo * bo)128 append_bo(struct msm_submit *submit, struct fd_bo *bo)
129 {
130    uint32_t idx;
131 
132    /* NOTE: it is legal to use the same bo on different threads for
133     * different submits.  But it is not legal to use the same submit
134     * from given threads.
135     */
136    idx = READ_ONCE(bo->idx);
137 
138    if (unlikely((idx >= submit->nr_submit_bos) ||
139                 (submit->submit_bos[idx].handle != bo->handle))) {
140       uint32_t hash = _mesa_hash_pointer(bo);
141       struct hash_entry *entry;
142 
143       entry = _mesa_hash_table_search_pre_hashed(submit->bo_table, hash, bo);
144       if (entry) {
145          /* found */
146          idx = (uint32_t)(uintptr_t)entry->data;
147       } else {
148          idx = APPEND(
149             submit, submit_bos,
150             (struct drm_msm_gem_submit_bo){
151                .flags = bo->reloc_flags & (MSM_SUBMIT_BO_READ | MSM_SUBMIT_BO_WRITE),
152                .handle = bo->handle,
153                .presumed = 0,
154             });
155          APPEND(submit, bos, fd_bo_ref(bo));
156 
157          _mesa_hash_table_insert_pre_hashed(submit->bo_table, hash, bo,
158                                             (void *)(uintptr_t)idx);
159       }
160       bo->idx = idx;
161    }
162 
163    return idx;
164 }
165 
166 static void
append_ring(struct set * set,struct fd_ringbuffer * ring)167 append_ring(struct set *set, struct fd_ringbuffer *ring)
168 {
169    uint32_t hash = _mesa_hash_pointer(ring);
170 
171    if (!_mesa_set_search_pre_hashed(set, hash, ring)) {
172       fd_ringbuffer_ref(ring);
173       _mesa_set_add_pre_hashed(set, hash, ring);
174    }
175 }
176 
177 static void
msm_submit_suballoc_ring_bo(struct fd_submit * submit,struct msm_ringbuffer * msm_ring,uint32_t size)178 msm_submit_suballoc_ring_bo(struct fd_submit *submit,
179                             struct msm_ringbuffer *msm_ring, uint32_t size)
180 {
181    struct msm_submit *msm_submit = to_msm_submit(submit);
182    unsigned suballoc_offset = 0;
183    struct fd_bo *suballoc_bo = NULL;
184 
185    if (msm_submit->suballoc_ring) {
186       struct msm_ringbuffer *suballoc_ring =
187          to_msm_ringbuffer(msm_submit->suballoc_ring);
188 
189       suballoc_bo = suballoc_ring->ring_bo;
190       suballoc_offset =
191          fd_ringbuffer_size(msm_submit->suballoc_ring) + suballoc_ring->offset;
192 
193       suballoc_offset = align(suballoc_offset, 0x10);
194 
195       if ((size + suballoc_offset) > suballoc_bo->size) {
196          suballoc_bo = NULL;
197       }
198    }
199 
200    if (!suballoc_bo) {
201       // TODO possibly larger size for streaming bo?
202       msm_ring->ring_bo = fd_bo_new_ring(submit->pipe->dev, 0x8000);
203       msm_ring->offset = 0;
204    } else {
205       msm_ring->ring_bo = fd_bo_ref(suballoc_bo);
206       msm_ring->offset = suballoc_offset;
207    }
208 
209    struct fd_ringbuffer *old_suballoc_ring = msm_submit->suballoc_ring;
210 
211    msm_submit->suballoc_ring = fd_ringbuffer_ref(&msm_ring->base);
212 
213    if (old_suballoc_ring)
214       fd_ringbuffer_del(old_suballoc_ring);
215 }
216 
217 static struct fd_ringbuffer *
msm_submit_new_ringbuffer(struct fd_submit * submit,uint32_t size,enum fd_ringbuffer_flags flags)218 msm_submit_new_ringbuffer(struct fd_submit *submit, uint32_t size,
219                           enum fd_ringbuffer_flags flags)
220 {
221    struct msm_submit *msm_submit = to_msm_submit(submit);
222    struct msm_ringbuffer *msm_ring;
223 
224    msm_ring = slab_alloc_st(&msm_submit->ring_pool);
225 
226    msm_ring->u.submit = submit;
227 
228    /* NOTE: needs to be before _suballoc_ring_bo() since it could
229     * increment the refcnt of the current ring
230     */
231    msm_ring->base.refcnt = 1;
232 
233    if (flags & FD_RINGBUFFER_STREAMING) {
234       msm_submit_suballoc_ring_bo(submit, msm_ring, size);
235    } else {
236       if (flags & FD_RINGBUFFER_GROWABLE)
237          size = INIT_SIZE;
238 
239       msm_ring->offset = 0;
240       msm_ring->ring_bo = fd_bo_new_ring(submit->pipe->dev, size);
241    }
242 
243    if (!msm_ringbuffer_init(msm_ring, size, flags))
244       return NULL;
245 
246    return &msm_ring->base;
247 }
248 
249 static struct drm_msm_gem_submit_reloc *
handle_stateobj_relocs(struct msm_submit * submit,struct msm_ringbuffer * ring)250 handle_stateobj_relocs(struct msm_submit *submit, struct msm_ringbuffer *ring)
251 {
252    struct msm_cmd *cmd = ring->cmd;
253    struct drm_msm_gem_submit_reloc *relocs;
254 
255    relocs = malloc(cmd->nr_relocs * sizeof(*relocs));
256 
257    for (unsigned i = 0; i < cmd->nr_relocs; i++) {
258       unsigned idx = cmd->relocs[i].reloc_idx;
259       struct fd_bo *bo = ring->u.reloc_bos[idx];
260 
261       relocs[i] = cmd->relocs[i];
262       relocs[i].reloc_idx = append_bo(submit, bo);
263    }
264 
265    return relocs;
266 }
267 
268 static int
msm_submit_flush(struct fd_submit * submit,int in_fence_fd,struct fd_submit_fence * out_fence)269 msm_submit_flush(struct fd_submit *submit, int in_fence_fd,
270                  struct fd_submit_fence *out_fence)
271 {
272    struct msm_submit *msm_submit = to_msm_submit(submit);
273    struct msm_pipe *msm_pipe = to_msm_pipe(submit->pipe);
274    struct drm_msm_gem_submit req = {
275       .flags = msm_pipe->pipe,
276       .queueid = msm_pipe->queue_id,
277    };
278    int ret;
279 
280    finalize_current_cmd(submit->primary);
281    append_ring(msm_submit->ring_set, submit->primary);
282 
283    unsigned nr_cmds = 0;
284    unsigned nr_objs = 0;
285 
286    set_foreach (msm_submit->ring_set, entry) {
287       struct fd_ringbuffer *ring = (void *)entry->key;
288       if (ring->flags & _FD_RINGBUFFER_OBJECT) {
289          nr_cmds += 1;
290          nr_objs += 1;
291       } else {
292          if (ring != submit->primary)
293             finalize_current_cmd(ring);
294          nr_cmds += to_msm_ringbuffer(ring)->u.nr_cmds;
295       }
296    }
297 
298    void *obj_relocs[nr_objs];
299    struct drm_msm_gem_submit_cmd cmds[nr_cmds];
300    unsigned i = 0, o = 0;
301 
302    set_foreach (msm_submit->ring_set, entry) {
303       struct fd_ringbuffer *ring = (void *)entry->key;
304       struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
305 
306       assert(i < nr_cmds);
307 
308       // TODO handle relocs:
309       if (ring->flags & _FD_RINGBUFFER_OBJECT) {
310 
311          assert(o < nr_objs);
312 
313          void *relocs = handle_stateobj_relocs(msm_submit, msm_ring);
314          obj_relocs[o++] = relocs;
315 
316          cmds[i].type = MSM_SUBMIT_CMD_IB_TARGET_BUF;
317          cmds[i].submit_idx = append_bo(msm_submit, msm_ring->ring_bo);
318          cmds[i].submit_offset = msm_ring->offset;
319          cmds[i].size = offset_bytes(ring->cur, ring->start);
320          cmds[i].pad = 0;
321          cmds[i].nr_relocs = msm_ring->cmd->nr_relocs;
322          cmds[i].relocs = VOID2U64(relocs);
323 
324          i++;
325       } else {
326          for (unsigned j = 0; j < msm_ring->u.nr_cmds; j++) {
327             if (ring->flags & FD_RINGBUFFER_PRIMARY) {
328                cmds[i].type = MSM_SUBMIT_CMD_BUF;
329             } else {
330                cmds[i].type = MSM_SUBMIT_CMD_IB_TARGET_BUF;
331             }
332             cmds[i].submit_idx =
333                append_bo(msm_submit, msm_ring->u.cmds[j]->ring_bo);
334             cmds[i].submit_offset = msm_ring->offset;
335             cmds[i].size = msm_ring->u.cmds[j]->size;
336             cmds[i].pad = 0;
337             cmds[i].nr_relocs = msm_ring->u.cmds[j]->nr_relocs;
338             cmds[i].relocs = VOID2U64(msm_ring->u.cmds[j]->relocs);
339 
340             i++;
341          }
342       }
343    }
344 
345    simple_mtx_lock(&table_lock);
346    for (unsigned j = 0; j < msm_submit->nr_bos; j++) {
347       fd_bo_add_fence(msm_submit->bos[j], submit->pipe, submit->fence);
348    }
349    simple_mtx_unlock(&table_lock);
350 
351    if (in_fence_fd != -1) {
352       req.flags |= MSM_SUBMIT_FENCE_FD_IN | MSM_SUBMIT_NO_IMPLICIT;
353       req.fence_fd = in_fence_fd;
354    }
355 
356    if (out_fence && out_fence->use_fence_fd) {
357       req.flags |= MSM_SUBMIT_FENCE_FD_OUT;
358    }
359 
360    /* needs to be after get_cmd() as that could create bos/cmds table: */
361    req.bos = VOID2U64(msm_submit->submit_bos),
362    req.nr_bos = msm_submit->nr_submit_bos;
363    req.cmds = VOID2U64(cmds), req.nr_cmds = nr_cmds;
364 
365    DEBUG_MSG("nr_cmds=%u, nr_bos=%u", req.nr_cmds, req.nr_bos);
366 
367    ret = drmCommandWriteRead(submit->pipe->dev->fd, DRM_MSM_GEM_SUBMIT, &req,
368                              sizeof(req));
369    if (ret) {
370       ERROR_MSG("submit failed: %d (%s)", ret, strerror(errno));
371       msm_dump_submit(&req);
372    } else if (!ret && out_fence) {
373       out_fence->fence.kfence = req.fence;
374       out_fence->fence.ufence = submit->fence;
375       out_fence->fence_fd = req.fence_fd;
376    }
377 
378    for (unsigned o = 0; o < nr_objs; o++)
379       free(obj_relocs[o]);
380 
381    return ret;
382 }
383 
384 static void
unref_rings(struct set_entry * entry)385 unref_rings(struct set_entry *entry)
386 {
387    struct fd_ringbuffer *ring = (void *)entry->key;
388    fd_ringbuffer_del(ring);
389 }
390 
391 static void
msm_submit_destroy(struct fd_submit * submit)392 msm_submit_destroy(struct fd_submit *submit)
393 {
394    struct msm_submit *msm_submit = to_msm_submit(submit);
395 
396    if (msm_submit->suballoc_ring)
397       fd_ringbuffer_del(msm_submit->suballoc_ring);
398 
399    _mesa_hash_table_destroy(msm_submit->bo_table, NULL);
400    _mesa_set_destroy(msm_submit->ring_set, unref_rings);
401 
402    // TODO it would be nice to have a way to assert() if all
403    // rb's haven't been free'd back to the slab, because that is
404    // an indication that we are leaking bo's
405    slab_destroy(&msm_submit->ring_pool);
406 
407    for (unsigned i = 0; i < msm_submit->nr_bos; i++)
408       fd_bo_del(msm_submit->bos[i]);
409 
410    free(msm_submit->submit_bos);
411    free(msm_submit->bos);
412    free(msm_submit);
413 }
414 
415 static const struct fd_submit_funcs submit_funcs = {
416    .new_ringbuffer = msm_submit_new_ringbuffer,
417    .flush = msm_submit_flush,
418    .destroy = msm_submit_destroy,
419 };
420 
421 struct fd_submit *
msm_submit_new(struct fd_pipe * pipe)422 msm_submit_new(struct fd_pipe *pipe)
423 {
424    struct msm_submit *msm_submit = calloc(1, sizeof(*msm_submit));
425    struct fd_submit *submit;
426 
427    msm_submit->bo_table = _mesa_hash_table_create(NULL, _mesa_hash_pointer,
428                                                   _mesa_key_pointer_equal);
429    msm_submit->ring_set =
430       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
431    // TODO tune size:
432    slab_create(&msm_submit->ring_pool, sizeof(struct msm_ringbuffer), 16);
433 
434    submit = &msm_submit->base;
435    submit->funcs = &submit_funcs;
436 
437    return submit;
438 }
439 
440 static void
finalize_current_cmd(struct fd_ringbuffer * ring)441 finalize_current_cmd(struct fd_ringbuffer *ring)
442 {
443    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
444 
445    assert(!(ring->flags & _FD_RINGBUFFER_OBJECT));
446 
447    if (!msm_ring->cmd)
448       return;
449 
450    assert(msm_ring->cmd->ring_bo == msm_ring->ring_bo);
451 
452    msm_ring->cmd->size = offset_bytes(ring->cur, ring->start);
453    APPEND(&msm_ring->u, cmds, msm_ring->cmd);
454    msm_ring->cmd = NULL;
455 }
456 
457 static void
msm_ringbuffer_grow(struct fd_ringbuffer * ring,uint32_t size)458 msm_ringbuffer_grow(struct fd_ringbuffer *ring, uint32_t size)
459 {
460    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
461    struct fd_pipe *pipe = msm_ring->u.submit->pipe;
462 
463    assert(ring->flags & FD_RINGBUFFER_GROWABLE);
464 
465    finalize_current_cmd(ring);
466 
467    fd_bo_del(msm_ring->ring_bo);
468    msm_ring->ring_bo = fd_bo_new_ring(pipe->dev, size);
469    msm_ring->cmd = cmd_new(msm_ring->ring_bo);
470 
471    ring->start = fd_bo_map(msm_ring->ring_bo);
472    ring->end = &(ring->start[size / 4]);
473    ring->cur = ring->start;
474    ring->size = size;
475 }
476 
477 static void
msm_ringbuffer_emit_reloc(struct fd_ringbuffer * ring,const struct fd_reloc * reloc)478 msm_ringbuffer_emit_reloc(struct fd_ringbuffer *ring,
479                           const struct fd_reloc *reloc)
480 {
481    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
482    struct fd_pipe *pipe;
483    unsigned reloc_idx;
484 
485    if (ring->flags & _FD_RINGBUFFER_OBJECT) {
486       unsigned idx = APPEND(&msm_ring->u, reloc_bos, fd_bo_ref(reloc->bo));
487 
488       /* this gets fixed up at submit->flush() time, since this state-
489        * object rb can be used with many different submits
490        */
491       reloc_idx = idx;
492 
493       pipe = msm_ring->u.pipe;
494    } else {
495       struct msm_submit *msm_submit = to_msm_submit(msm_ring->u.submit);
496 
497       reloc_idx = append_bo(msm_submit, reloc->bo);
498 
499       pipe = msm_ring->u.submit->pipe;
500    }
501 
502    APPEND(msm_ring->cmd, relocs,
503           (struct drm_msm_gem_submit_reloc){
504              .reloc_idx = reloc_idx,
505              .reloc_offset = reloc->offset,
506              .or = reloc->orval,
507              .shift = reloc->shift,
508              .submit_offset =
509                 offset_bytes(ring->cur, ring->start) + msm_ring->offset,
510           });
511 
512    ring->cur++;
513 
514    if (fd_dev_64b(&pipe->dev_id)) {
515       APPEND(msm_ring->cmd, relocs,
516              (struct drm_msm_gem_submit_reloc){
517                 .reloc_idx = reloc_idx,
518                 .reloc_offset = reloc->offset,
519                 .or = reloc->orval >> 32,
520                 .shift = reloc->shift - 32,
521                 .submit_offset =
522                    offset_bytes(ring->cur, ring->start) + msm_ring->offset,
523              });
524 
525       ring->cur++;
526    }
527 }
528 
529 static void
append_stateobj_rings(struct msm_submit * submit,struct fd_ringbuffer * target)530 append_stateobj_rings(struct msm_submit *submit, struct fd_ringbuffer *target)
531 {
532    struct msm_ringbuffer *msm_target = to_msm_ringbuffer(target);
533 
534    assert(target->flags & _FD_RINGBUFFER_OBJECT);
535 
536    set_foreach (msm_target->u.ring_set, entry) {
537       struct fd_ringbuffer *ring = (void *)entry->key;
538 
539       append_ring(submit->ring_set, ring);
540 
541       if (ring->flags & _FD_RINGBUFFER_OBJECT) {
542          append_stateobj_rings(submit, ring);
543       }
544    }
545 }
546 
547 static uint32_t
msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer * ring,struct fd_ringbuffer * target,uint32_t cmd_idx)548 msm_ringbuffer_emit_reloc_ring(struct fd_ringbuffer *ring,
549                                struct fd_ringbuffer *target, uint32_t cmd_idx)
550 {
551    struct msm_ringbuffer *msm_target = to_msm_ringbuffer(target);
552    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
553    struct fd_bo *bo;
554    uint32_t size;
555 
556    if ((target->flags & FD_RINGBUFFER_GROWABLE) &&
557        (cmd_idx < msm_target->u.nr_cmds)) {
558       bo = msm_target->u.cmds[cmd_idx]->ring_bo;
559       size = msm_target->u.cmds[cmd_idx]->size;
560    } else {
561       bo = msm_target->ring_bo;
562       size = offset_bytes(target->cur, target->start);
563    }
564 
565    msm_ringbuffer_emit_reloc(ring, &(struct fd_reloc){
566                                       .bo = bo,
567                                       .iova = bo->iova + msm_target->offset,
568                                       .offset = msm_target->offset,
569                                    });
570 
571    if (!size)
572       return 0;
573 
574    if ((target->flags & _FD_RINGBUFFER_OBJECT) &&
575        !(ring->flags & _FD_RINGBUFFER_OBJECT)) {
576       struct msm_submit *msm_submit = to_msm_submit(msm_ring->u.submit);
577 
578       append_stateobj_rings(msm_submit, target);
579    }
580 
581    if (ring->flags & _FD_RINGBUFFER_OBJECT) {
582       append_ring(msm_ring->u.ring_set, target);
583    } else {
584       struct msm_submit *msm_submit = to_msm_submit(msm_ring->u.submit);
585       append_ring(msm_submit->ring_set, target);
586    }
587 
588    return size;
589 }
590 
591 static uint32_t
msm_ringbuffer_cmd_count(struct fd_ringbuffer * ring)592 msm_ringbuffer_cmd_count(struct fd_ringbuffer *ring)
593 {
594    if (ring->flags & FD_RINGBUFFER_GROWABLE)
595       return to_msm_ringbuffer(ring)->u.nr_cmds + 1;
596    return 1;
597 }
598 
599 static bool
msm_ringbuffer_check_size(struct fd_ringbuffer * ring)600 msm_ringbuffer_check_size(struct fd_ringbuffer *ring)
601 {
602    assert(!(ring->flags & _FD_RINGBUFFER_OBJECT));
603    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
604    struct fd_submit *submit = msm_ring->u.submit;
605    struct fd_pipe *pipe = submit->pipe;
606 
607    if ((fd_device_version(pipe->dev) < FD_VERSION_UNLIMITED_CMDS) &&
608        ((ring->cur - ring->start) > (ring->size / 4 - 0x1000))) {
609       return false;
610    }
611 
612    if (to_msm_submit(submit)->nr_bos > MAX_ARRAY_SIZE/2) {
613       return false;
614    }
615 
616    return true;
617 }
618 
619 static void
msm_ringbuffer_destroy(struct fd_ringbuffer * ring)620 msm_ringbuffer_destroy(struct fd_ringbuffer *ring)
621 {
622    struct msm_ringbuffer *msm_ring = to_msm_ringbuffer(ring);
623 
624    fd_bo_del(msm_ring->ring_bo);
625    if (msm_ring->cmd)
626       cmd_free(msm_ring->cmd);
627 
628    if (ring->flags & _FD_RINGBUFFER_OBJECT) {
629       for (unsigned i = 0; i < msm_ring->u.nr_reloc_bos; i++) {
630          fd_bo_del(msm_ring->u.reloc_bos[i]);
631       }
632 
633       _mesa_set_destroy(msm_ring->u.ring_set, unref_rings);
634 
635       free(msm_ring->u.reloc_bos);
636       free(msm_ring);
637    } else {
638       struct fd_submit *submit = msm_ring->u.submit;
639 
640       for (unsigned i = 0; i < msm_ring->u.nr_cmds; i++) {
641          cmd_free(msm_ring->u.cmds[i]);
642       }
643 
644       free(msm_ring->u.cmds);
645       slab_free_st(&to_msm_submit(submit)->ring_pool, msm_ring);
646    }
647 }
648 
649 static const struct fd_ringbuffer_funcs ring_funcs = {
650    .grow = msm_ringbuffer_grow,
651    .emit_reloc = msm_ringbuffer_emit_reloc,
652    .emit_reloc_ring = msm_ringbuffer_emit_reloc_ring,
653    .cmd_count = msm_ringbuffer_cmd_count,
654    .check_size = msm_ringbuffer_check_size,
655    .destroy = msm_ringbuffer_destroy,
656 };
657 
658 static inline struct fd_ringbuffer *
msm_ringbuffer_init(struct msm_ringbuffer * msm_ring,uint32_t size,enum fd_ringbuffer_flags flags)659 msm_ringbuffer_init(struct msm_ringbuffer *msm_ring, uint32_t size,
660                     enum fd_ringbuffer_flags flags)
661 {
662    struct fd_ringbuffer *ring = &msm_ring->base;
663 
664    assert(msm_ring->ring_bo);
665 
666    uint8_t *base = fd_bo_map(msm_ring->ring_bo);
667    ring->start = (void *)(base + msm_ring->offset);
668    ring->end = &(ring->start[size / 4]);
669    ring->cur = ring->start;
670 
671    ring->size = size;
672    ring->flags = flags;
673 
674    ring->funcs = &ring_funcs;
675 
676    msm_ring->u.cmds = NULL;
677    msm_ring->u.nr_cmds = msm_ring->u.max_cmds = 0;
678 
679    msm_ring->cmd = cmd_new(msm_ring->ring_bo);
680 
681    return ring;
682 }
683 
684 struct fd_ringbuffer *
msm_ringbuffer_new_object(struct fd_pipe * pipe,uint32_t size)685 msm_ringbuffer_new_object(struct fd_pipe *pipe, uint32_t size)
686 {
687    struct msm_ringbuffer *msm_ring = malloc(sizeof(*msm_ring));
688 
689    msm_ring->u.pipe = pipe;
690    msm_ring->offset = 0;
691    msm_ring->ring_bo = fd_bo_new_ring(pipe->dev, size);
692    msm_ring->base.refcnt = 1;
693 
694    msm_ring->u.reloc_bos = NULL;
695    msm_ring->u.nr_reloc_bos = msm_ring->u.max_reloc_bos = 0;
696 
697    msm_ring->u.ring_set =
698       _mesa_set_create(NULL, _mesa_hash_pointer, _mesa_key_pointer_equal);
699 
700    return msm_ringbuffer_init(msm_ring, size, _FD_RINGBUFFER_OBJECT);
701 }
702