1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2022 Google, Inc. 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice (including the next 12bf215546Sopenharmony_ci * paragraph) shall be included in all copies or substantial portions of the 13bf215546Sopenharmony_ci * Software. 14bf215546Sopenharmony_ci * 15bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16bf215546Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 18bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20bf215546Sopenharmony_ci * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21bf215546Sopenharmony_ci * SOFTWARE. 22bf215546Sopenharmony_ci */ 23bf215546Sopenharmony_ci 24bf215546Sopenharmony_ci#ifdef X 25bf215546Sopenharmony_ci#undef X 26bf215546Sopenharmony_ci#endif 27bf215546Sopenharmony_ci 28bf215546Sopenharmony_ci/* 29bf215546Sopenharmony_ci * TODO make this magic easier to share btw msm_ringbuffer_sp and virtio_ringbuffer 30bf215546Sopenharmony_ci */ 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#if PTRSZ == 32 33bf215546Sopenharmony_ci#define X(n) n##_32 34bf215546Sopenharmony_ci#else 35bf215546Sopenharmony_ci#define X(n) n##_64 36bf215546Sopenharmony_ci#endif 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_cistatic void X(emit_reloc_common)(struct fd_ringbuffer *ring, 39bf215546Sopenharmony_ci const struct fd_reloc *reloc) 40bf215546Sopenharmony_ci{ 41bf215546Sopenharmony_ci (*ring->cur++) = (uint32_t)reloc->iova; 42bf215546Sopenharmony_ci#if PTRSZ == 64 43bf215546Sopenharmony_ci (*ring->cur++) = (uint32_t)(reloc->iova >> 32); 44bf215546Sopenharmony_ci#endif 45bf215546Sopenharmony_ci} 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_cistatic void X(virtio_ringbuffer_emit_reloc_nonobj)(struct fd_ringbuffer *ring, 48bf215546Sopenharmony_ci const struct fd_reloc *reloc) 49bf215546Sopenharmony_ci{ 50bf215546Sopenharmony_ci X(emit_reloc_common)(ring, reloc); 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci assert(!(ring->flags & _FD_RINGBUFFER_OBJECT)); 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci struct virtio_ringbuffer *virtio_ring = to_virtio_ringbuffer(ring); 55bf215546Sopenharmony_ci 56bf215546Sopenharmony_ci struct virtio_submit *virtio_submit = to_virtio_submit(virtio_ring->u.submit); 57bf215546Sopenharmony_ci 58bf215546Sopenharmony_ci virtio_submit_append_bo(virtio_submit, reloc->bo); 59bf215546Sopenharmony_ci} 60bf215546Sopenharmony_ci 61bf215546Sopenharmony_cistatic void X(virtio_ringbuffer_emit_reloc_obj)(struct fd_ringbuffer *ring, 62bf215546Sopenharmony_ci const struct fd_reloc *reloc) 63bf215546Sopenharmony_ci{ 64bf215546Sopenharmony_ci X(emit_reloc_common)(ring, reloc); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci assert(ring->flags & _FD_RINGBUFFER_OBJECT); 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci struct virtio_ringbuffer *virtio_ring = to_virtio_ringbuffer(ring); 69bf215546Sopenharmony_ci 70bf215546Sopenharmony_ci /* Avoid emitting duplicate BO references into the list. Ringbuffer 71bf215546Sopenharmony_ci * objects are long-lived, so this saves ongoing work at draw time in 72bf215546Sopenharmony_ci * exchange for a bit at context setup/first draw. And the number of 73bf215546Sopenharmony_ci * relocs per ringbuffer object is fairly small, so the O(n^2) doesn't 74bf215546Sopenharmony_ci * hurt much. 75bf215546Sopenharmony_ci */ 76bf215546Sopenharmony_ci if (!virtio_ringbuffer_references_bo(ring, reloc->bo)) { 77bf215546Sopenharmony_ci APPEND(&virtio_ring->u, reloc_bos, fd_bo_ref(reloc->bo)); 78bf215546Sopenharmony_ci } 79bf215546Sopenharmony_ci} 80bf215546Sopenharmony_ci 81bf215546Sopenharmony_cistatic uint32_t X(virtio_ringbuffer_emit_reloc_ring)( 82bf215546Sopenharmony_ci struct fd_ringbuffer *ring, struct fd_ringbuffer *target, uint32_t cmd_idx) 83bf215546Sopenharmony_ci{ 84bf215546Sopenharmony_ci struct virtio_ringbuffer *virtio_target = to_virtio_ringbuffer(target); 85bf215546Sopenharmony_ci struct fd_bo *bo; 86bf215546Sopenharmony_ci uint32_t size; 87bf215546Sopenharmony_ci 88bf215546Sopenharmony_ci if ((target->flags & FD_RINGBUFFER_GROWABLE) && 89bf215546Sopenharmony_ci (cmd_idx < virtio_target->u.nr_cmds)) { 90bf215546Sopenharmony_ci bo = virtio_target->u.cmds[cmd_idx].ring_bo; 91bf215546Sopenharmony_ci size = virtio_target->u.cmds[cmd_idx].size; 92bf215546Sopenharmony_ci } else { 93bf215546Sopenharmony_ci bo = virtio_target->ring_bo; 94bf215546Sopenharmony_ci size = offset_bytes(target->cur, target->start); 95bf215546Sopenharmony_ci } 96bf215546Sopenharmony_ci 97bf215546Sopenharmony_ci if (ring->flags & _FD_RINGBUFFER_OBJECT) { 98bf215546Sopenharmony_ci X(virtio_ringbuffer_emit_reloc_obj)(ring, &(struct fd_reloc){ 99bf215546Sopenharmony_ci .bo = bo, 100bf215546Sopenharmony_ci .iova = bo->iova + virtio_target->offset, 101bf215546Sopenharmony_ci .offset = virtio_target->offset, 102bf215546Sopenharmony_ci }); 103bf215546Sopenharmony_ci } else { 104bf215546Sopenharmony_ci X(virtio_ringbuffer_emit_reloc_nonobj)(ring, &(struct fd_reloc){ 105bf215546Sopenharmony_ci .bo = bo, 106bf215546Sopenharmony_ci .iova = bo->iova + virtio_target->offset, 107bf215546Sopenharmony_ci .offset = virtio_target->offset, 108bf215546Sopenharmony_ci }); 109bf215546Sopenharmony_ci } 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci if (!(target->flags & _FD_RINGBUFFER_OBJECT)) 112bf215546Sopenharmony_ci return size; 113bf215546Sopenharmony_ci 114bf215546Sopenharmony_ci struct virtio_ringbuffer *virtio_ring = to_virtio_ringbuffer(ring); 115bf215546Sopenharmony_ci 116bf215546Sopenharmony_ci if (ring->flags & _FD_RINGBUFFER_OBJECT) { 117bf215546Sopenharmony_ci for (unsigned i = 0; i < virtio_target->u.nr_reloc_bos; i++) { 118bf215546Sopenharmony_ci struct fd_bo *target_bo = virtio_target->u.reloc_bos[i]; 119bf215546Sopenharmony_ci if (!virtio_ringbuffer_references_bo(ring, target_bo)) 120bf215546Sopenharmony_ci APPEND(&virtio_ring->u, reloc_bos, fd_bo_ref(target_bo)); 121bf215546Sopenharmony_ci } 122bf215546Sopenharmony_ci } else { 123bf215546Sopenharmony_ci // TODO it would be nice to know whether we have already 124bf215546Sopenharmony_ci // seen this target before. But hopefully we hit the 125bf215546Sopenharmony_ci // append_bo() fast path enough for this to not matter: 126bf215546Sopenharmony_ci struct virtio_submit *virtio_submit = to_virtio_submit(virtio_ring->u.submit); 127bf215546Sopenharmony_ci 128bf215546Sopenharmony_ci for (unsigned i = 0; i < virtio_target->u.nr_reloc_bos; i++) { 129bf215546Sopenharmony_ci virtio_submit_append_bo(virtio_submit, virtio_target->u.reloc_bos[i]); 130bf215546Sopenharmony_ci } 131bf215546Sopenharmony_ci } 132bf215546Sopenharmony_ci 133bf215546Sopenharmony_ci return size; 134bf215546Sopenharmony_ci} 135