1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2021 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#if PTRSZ == 32 29bf215546Sopenharmony_ci#define X(n) n##_32 30bf215546Sopenharmony_ci#else 31bf215546Sopenharmony_ci#define X(n) n##_64 32bf215546Sopenharmony_ci#endif 33bf215546Sopenharmony_ci 34bf215546Sopenharmony_cistatic void X(emit_reloc_common)(struct fd_ringbuffer *ring, 35bf215546Sopenharmony_ci const struct fd_reloc *reloc) 36bf215546Sopenharmony_ci{ 37bf215546Sopenharmony_ci (*ring->cur++) = (uint32_t)reloc->iova; 38bf215546Sopenharmony_ci#if PTRSZ == 64 39bf215546Sopenharmony_ci (*ring->cur++) = (uint32_t)(reloc->iova >> 32); 40bf215546Sopenharmony_ci#endif 41bf215546Sopenharmony_ci} 42bf215546Sopenharmony_ci 43bf215546Sopenharmony_cistatic void X(fd_ringbuffer_sp_emit_reloc_nonobj)(struct fd_ringbuffer *ring, 44bf215546Sopenharmony_ci const struct fd_reloc *reloc) 45bf215546Sopenharmony_ci{ 46bf215546Sopenharmony_ci X(emit_reloc_common)(ring, reloc); 47bf215546Sopenharmony_ci 48bf215546Sopenharmony_ci assert(!(ring->flags & _FD_RINGBUFFER_OBJECT)); 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_ci struct fd_ringbuffer_sp *fd_ring = to_fd_ringbuffer_sp(ring); 51bf215546Sopenharmony_ci 52bf215546Sopenharmony_ci struct fd_submit_sp *fd_submit = to_fd_submit_sp(fd_ring->u.submit); 53bf215546Sopenharmony_ci 54bf215546Sopenharmony_ci fd_submit_append_bo(fd_submit, reloc->bo); 55bf215546Sopenharmony_ci} 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_cistatic void X(fd_ringbuffer_sp_emit_reloc_obj)(struct fd_ringbuffer *ring, 58bf215546Sopenharmony_ci const struct fd_reloc *reloc) 59bf215546Sopenharmony_ci{ 60bf215546Sopenharmony_ci X(emit_reloc_common)(ring, reloc); 61bf215546Sopenharmony_ci 62bf215546Sopenharmony_ci assert(ring->flags & _FD_RINGBUFFER_OBJECT); 63bf215546Sopenharmony_ci 64bf215546Sopenharmony_ci struct fd_ringbuffer_sp *fd_ring = to_fd_ringbuffer_sp(ring); 65bf215546Sopenharmony_ci 66bf215546Sopenharmony_ci /* Avoid emitting duplicate BO references into the list. Ringbuffer 67bf215546Sopenharmony_ci * objects are long-lived, so this saves ongoing work at draw time in 68bf215546Sopenharmony_ci * exchange for a bit at context setup/first draw. And the number of 69bf215546Sopenharmony_ci * relocs per ringbuffer object is fairly small, so the O(n^2) doesn't 70bf215546Sopenharmony_ci * hurt much. 71bf215546Sopenharmony_ci */ 72bf215546Sopenharmony_ci if (!fd_ringbuffer_references_bo(ring, reloc->bo)) { 73bf215546Sopenharmony_ci APPEND(&fd_ring->u, reloc_bos, fd_bo_ref(reloc->bo)); 74bf215546Sopenharmony_ci } 75bf215546Sopenharmony_ci} 76bf215546Sopenharmony_ci 77bf215546Sopenharmony_cistatic uint32_t X(fd_ringbuffer_sp_emit_reloc_ring)( 78bf215546Sopenharmony_ci struct fd_ringbuffer *ring, struct fd_ringbuffer *target, uint32_t cmd_idx) 79bf215546Sopenharmony_ci{ 80bf215546Sopenharmony_ci struct fd_ringbuffer_sp *fd_target = to_fd_ringbuffer_sp(target); 81bf215546Sopenharmony_ci struct fd_bo *bo; 82bf215546Sopenharmony_ci uint32_t size; 83bf215546Sopenharmony_ci 84bf215546Sopenharmony_ci if ((target->flags & FD_RINGBUFFER_GROWABLE) && 85bf215546Sopenharmony_ci (cmd_idx < fd_target->u.nr_cmds)) { 86bf215546Sopenharmony_ci bo = fd_target->u.cmds[cmd_idx].ring_bo; 87bf215546Sopenharmony_ci size = fd_target->u.cmds[cmd_idx].size; 88bf215546Sopenharmony_ci } else { 89bf215546Sopenharmony_ci bo = fd_target->ring_bo; 90bf215546Sopenharmony_ci size = offset_bytes(target->cur, target->start); 91bf215546Sopenharmony_ci } 92bf215546Sopenharmony_ci 93bf215546Sopenharmony_ci if (ring->flags & _FD_RINGBUFFER_OBJECT) { 94bf215546Sopenharmony_ci X(fd_ringbuffer_sp_emit_reloc_obj)(ring, &(struct fd_reloc){ 95bf215546Sopenharmony_ci .bo = bo, 96bf215546Sopenharmony_ci .iova = bo->iova + fd_target->offset, 97bf215546Sopenharmony_ci .offset = fd_target->offset, 98bf215546Sopenharmony_ci }); 99bf215546Sopenharmony_ci } else { 100bf215546Sopenharmony_ci X(fd_ringbuffer_sp_emit_reloc_nonobj)(ring, &(struct fd_reloc){ 101bf215546Sopenharmony_ci .bo = bo, 102bf215546Sopenharmony_ci .iova = bo->iova + fd_target->offset, 103bf215546Sopenharmony_ci .offset = fd_target->offset, 104bf215546Sopenharmony_ci }); 105bf215546Sopenharmony_ci } 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci if (!(target->flags & _FD_RINGBUFFER_OBJECT)) 108bf215546Sopenharmony_ci return size; 109bf215546Sopenharmony_ci 110bf215546Sopenharmony_ci struct fd_ringbuffer_sp *fd_ring = to_fd_ringbuffer_sp(ring); 111bf215546Sopenharmony_ci 112bf215546Sopenharmony_ci if (ring->flags & _FD_RINGBUFFER_OBJECT) { 113bf215546Sopenharmony_ci for (unsigned i = 0; i < fd_target->u.nr_reloc_bos; i++) { 114bf215546Sopenharmony_ci struct fd_bo *target_bo = fd_target->u.reloc_bos[i]; 115bf215546Sopenharmony_ci if (!fd_ringbuffer_references_bo(ring, target_bo)) 116bf215546Sopenharmony_ci APPEND(&fd_ring->u, reloc_bos, fd_bo_ref(target_bo)); 117bf215546Sopenharmony_ci } 118bf215546Sopenharmony_ci } else { 119bf215546Sopenharmony_ci // TODO it would be nice to know whether we have already 120bf215546Sopenharmony_ci // seen this target before. But hopefully we hit the 121bf215546Sopenharmony_ci // append_bo() fast path enough for this to not matter: 122bf215546Sopenharmony_ci struct fd_submit_sp *fd_submit = to_fd_submit_sp(fd_ring->u.submit); 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci for (unsigned i = 0; i < fd_target->u.nr_reloc_bos; i++) { 125bf215546Sopenharmony_ci fd_submit_append_bo(fd_submit, fd_target->u.reloc_bos[i]); 126bf215546Sopenharmony_ci } 127bf215546Sopenharmony_ci } 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci return size; 130bf215546Sopenharmony_ci} 131