1 /*
2 * Copyright © 2021 Google, Inc.
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
24 #ifdef X
25 #undef X
26 #endif
27
28 #if PTRSZ == 32
29 #define X(n) n##_32
30 #else
31 #define X(n) n##_64
32 #endif
33
X(emit_reloc_common)34 static void X(emit_reloc_common)(struct fd_ringbuffer *ring, uint64_t iova)
35 {
36 #if PTRSZ == 64
37 uint64_t *p64 = (uint64_t *)ring->cur;
38 *p64 = iova;
39 ring->cur += 2;
40 #else
41 (*ring->cur++) = (uint32_t)iova;
42 #endif
43 }
44
X(fd_ringbuffer_sp_emit_reloc_nonobj)45 static void X(fd_ringbuffer_sp_emit_reloc_nonobj)(struct fd_ringbuffer *ring,
46 const struct fd_reloc *reloc)
47 {
48 X(emit_reloc_common)(ring, reloc->iova);
49 fd_ringbuffer_sp_emit_bo_nonobj(ring, reloc->bo);
50 }
51
X(fd_ringbuffer_sp_emit_reloc_obj)52 static void X(fd_ringbuffer_sp_emit_reloc_obj)(struct fd_ringbuffer *ring,
53 const struct fd_reloc *reloc)
54 {
55 X(emit_reloc_common)(ring, reloc->iova);
56 fd_ringbuffer_sp_emit_bo_obj(ring, reloc->bo);
57 }
58
X(fd_ringbuffer_sp_emit_reloc_ring)59 static uint32_t X(fd_ringbuffer_sp_emit_reloc_ring)(
60 struct fd_ringbuffer *ring, struct fd_ringbuffer *target, uint32_t cmd_idx)
61 {
62 struct fd_ringbuffer_sp *fd_target = to_fd_ringbuffer_sp(target);
63 struct fd_bo *bo;
64 uint32_t size;
65
66 if ((target->flags & FD_RINGBUFFER_GROWABLE) &&
67 (cmd_idx < fd_target->u.nr_cmds)) {
68 bo = fd_target->u.cmds[cmd_idx].ring_bo;
69 size = fd_target->u.cmds[cmd_idx].size;
70 } else {
71 bo = fd_target->ring_bo;
72 size = offset_bytes(target->cur, target->start);
73 }
74
75 if (ring->flags & _FD_RINGBUFFER_OBJECT) {
76 X(fd_ringbuffer_sp_emit_reloc_obj)(ring, &(struct fd_reloc){
77 .bo = bo,
78 .iova = bo->iova + fd_target->offset,
79 .offset = fd_target->offset,
80 });
81 } else {
82 X(fd_ringbuffer_sp_emit_reloc_nonobj)(ring, &(struct fd_reloc){
83 .bo = bo,
84 .iova = bo->iova + fd_target->offset,
85 .offset = fd_target->offset,
86 });
87 }
88
89 if (!(target->flags & _FD_RINGBUFFER_OBJECT))
90 return size;
91
92 struct fd_ringbuffer_sp *fd_ring = to_fd_ringbuffer_sp(ring);
93
94 if (ring->flags & _FD_RINGBUFFER_OBJECT) {
95 for (unsigned i = 0; i < fd_target->u.nr_reloc_bos; i++) {
96 struct fd_bo *target_bo = fd_target->u.reloc_bos[i];
97 if (!fd_ringbuffer_references_bo(ring, target_bo))
98 APPEND(&fd_ring->u, reloc_bos, fd_bo_ref(target_bo));
99 }
100 } else {
101 struct fd_submit_sp *fd_submit = to_fd_submit_sp(fd_ring->u.submit);
102
103 if (fd_submit->seqno != fd_target->u.last_submit_seqno) {
104 for (unsigned i = 0; i < fd_target->u.nr_reloc_bos; i++) {
105 fd_submit_append_bo(fd_submit, fd_target->u.reloc_bos[i]);
106 }
107 fd_target->u.last_submit_seqno = fd_submit->seqno;
108 }
109
110 #ifndef NDEBUG
111 /* Dealing with assert'd BOs is deferred until the submit is known,
112 * since the batch resource tracking attaches BOs directly to
113 * the submit instead of the long lived stateobj
114 */
115 for (unsigned i = 0; i < fd_target->u.nr_assert_bos; i++) {
116 fd_ringbuffer_sp_assert_attached_nonobj(ring, fd_target->u.assert_bos[i]);
117 }
118 #endif
119 }
120
121 return size;
122 }
123