Debian Codegraph @

src/panfrost/lib/kmod/pan_kmod.c

raw
1 /*
2 * Copyright © 2023 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <string.h>
7 #include <xf86drm.h>
8
9 #include "util/cache_ops.h"
10 #include "util/u_memory.h"
11 #include "util/macros.h"
12 #include "pan_kmod.h"
13 #include "pan_kmod_backend.h"
14
15 extern const struct pan_kmod_ops panfrost_kmod_ops;
16 extern const struct pan_kmod_ops panthor_kmod_ops;
17
18 static const struct {
19 const char *name;
20 const struct pan_kmod_ops *ops;
21 } drivers[] = {
22 {
23 "panfrost",
24 &panfrost_kmod_ops,
25 },
26 {
27 "panthor",
28 &panthor_kmod_ops,
29 },
30 };
31
32 static void *
33 default_zalloc(const struct pan_kmod_allocator *allocator, size_t size,
34 UNUSED bool transient)
35 {
36 return os_calloc(1, size);
37 }
38
39 static void
40 default_free(const struct pan_kmod_allocator *allocator, void *data)
41 {
42 os_free(data);
43 }
44
45 static const struct pan_kmod_allocator default_allocator = {
46 .zalloc = default_zalloc,
47 .free = default_free,
48 };
49
50 struct pan_kmod_dev *
51 pan_kmod_dev_create(int fd, uint32_t flags,
52 const struct pan_kmod_allocator *allocator)
53 {
54 drmVersionPtr version = drmGetVersion(fd);
55 struct pan_kmod_dev *dev = NULL;
56
57 if (!version)
58 return NULL;
59
60 if (!allocator)
61 allocator = &default_allocator;
62
63 for (unsigned i = 0; i < ARRAY_SIZE(drivers); i++) {
64 if (!strcmp(drivers[i].name, version->name)) {
65 const struct pan_kmod_ops *ops = drivers[i].ops;
66
67 dev = ops->dev_create(fd, flags, version, allocator);
68 break;
69 }
70 }
71
72 drmFreeVersion(version);
73 return dev;
74 }
75
76 void
77 pan_kmod_dev_destroy(struct pan_kmod_dev *dev)
78 {
79 dev->ops->dev_destroy(dev);
80 }
81
82 struct pan_kmod_bo *
83 pan_kmod_bo_alloc(struct pan_kmod_dev *dev, struct pan_kmod_vm *exclusive_vm,
84 uint64_t size, uint32_t flags)
85 {
86 struct pan_kmod_bo *bo;
87
88 bo = dev->ops->bo_alloc(dev, exclusive_vm, size, flags);
89 if (!bo)
90 return NULL;
91
92 /* We intentionally don't take the lock when filling the sparse array,
93 * because we just created the BO, and haven't exported it yet, so
94 * there's no risk of imports racing with our BO insertion.
95 */
96 struct pan_kmod_bo **slot =
97 util_sparse_array_get(&dev->handle_to_bo.array, bo->handle);
98
99 if (!slot) {
100 mesa_loge("failed to allocate slot in the handle_to_bo array");
101 bo->dev->ops->bo_free(bo);
102 return NULL;
103 }
104
105 assert(*slot == NULL);
106 *slot = bo;
107 return bo;
108 }
109
110 void
111 pan_kmod_bo_put(struct pan_kmod_bo *bo)
112 {
113 if (!bo)
114 return;
115
116 int32_t refcnt = p_atomic_dec_return(&bo->refcnt);
117
118 assert(refcnt >= 0);
119
120 if (refcnt)
121 return;
122
123 struct pan_kmod_dev *dev = bo->dev;
124
125 simple_mtx_lock(&dev->handle_to_bo.lock);
126
127 /* If some import took a ref on this BO while we were trying to acquire the
128 * lock, skip the destruction.
129 */
130 if (!p_atomic_read(&bo->refcnt)) {
131 struct pan_kmod_bo **slot = (struct pan_kmod_bo **)util_sparse_array_get(
132 &dev->handle_to_bo.array, bo->handle);
133
134 assert(slot);
135 *slot = NULL;
136 bo->dev->ops->bo_free(bo);
137 }
138
139 simple_mtx_unlock(&dev->handle_to_bo.lock);
140 }
141
142 struct pan_kmod_bo *
143 pan_kmod_bo_import(struct pan_kmod_dev *dev, int fd)
144 {
145 struct pan_kmod_bo *bo = NULL;
146 struct pan_kmod_bo **slot;
147
148 simple_mtx_lock(&dev->handle_to_bo.lock);
149
150 uint32_t handle;
151 int ret = drmPrimeFDToHandle(dev->fd, fd, &handle);
152 if (ret)
153 goto err_unlock;
154
155 slot = util_sparse_array_get(&dev->handle_to_bo.array, handle);
156 if (!slot)
157 goto err_close_handle;
158
159 if (*slot) {
160 bo = *slot;
161
162 p_atomic_inc(&bo->refcnt);
163 } else {
164 size_t size = lseek(fd, 0, SEEK_END);
165 if (size == 0 || size == (size_t)-1) {
166 mesa_loge("invalid dmabuf size");
167 goto err_close_handle;
168 }
169
170 bo = dev->ops->bo_import(dev, handle, size);
171 if (!bo)
172 goto err_close_handle;
173
174 *slot = bo;
175 }
176
177 assert(p_atomic_read(&bo->refcnt) > 0);
178
179 simple_mtx_unlock(&dev->handle_to_bo.lock);
180
181 return bo;
182
183 err_close_handle:
184 drmCloseBufferHandle(dev->fd, handle);
185
186 err_unlock:
187 simple_mtx_unlock(&dev->handle_to_bo.lock);
188
189 return NULL;
190 }
191
192 void
193 pan_kmod_flush_bo_map_syncs_locked(struct pan_kmod_dev *dev)
194 {
195 ASSERTED int ret = dev->ops->flush_bo_map_syncs(dev);
196 assert(!ret);
197
198 util_dynarray_foreach(&dev->pending_bo_syncs.array,
199 struct pan_kmod_deferred_bo_sync, sync)
200 sync->bo->has_pending_deferred_syncs = false;
201
202 util_dynarray_clear(&dev->pending_bo_syncs.array);
203 }
204
205 void
206 pan_kmod_flush_bo_map_syncs(struct pan_kmod_dev *dev)
207 {
208 if (dev->props.is_io_coherent)
209 return;
210
211 /* Barrier to make sure all flush/invalidate requests are effective. */
212 if (p_atomic_xchg(&dev->pending_bo_syncs.user_cache_ops_pending, false))
213 util_post_flush_inval_fence();
214
215 /* This can be racy, but that's fine, because we expect a future call to
216 * pan_kmod_flush_bo_map_syncs() if new ops are being added while we check
217 * this value.
218 */
219 if (!util_dynarray_num_elements(&dev->pending_bo_syncs.array,
220 struct pan_kmod_deferred_bo_sync))
221 return;
222
223 simple_mtx_lock(&dev->pending_bo_syncs.lock);
224 pan_kmod_flush_bo_map_syncs_locked(dev);
225 simple_mtx_unlock(&dev->pending_bo_syncs.lock);
226 }
227
228 /* Arbitrary limit for now. Pick something bigger or make it configurable if it
229 * becomes problematic.
230 */
231 #define MAX_PENDING_SYNC_OPS 4096
232
233 void
234 pan_kmod_queue_bo_map_sync(struct pan_kmod_bo *bo, uint64_t bo_offset,
235 void *cpu_ptr, uint64_t range,
236 enum pan_kmod_bo_sync_type type)
237 {
238 struct pan_kmod_dev *dev = bo->dev;
239
240 /* Nothing to do if the buffer is IO coherent or if the BO is not mapped
241 * cacheable.
242 */
243 if (!(bo->flags & PAN_KMOD_BO_FLAG_WB_MMAP) ||
244 (bo->flags & PAN_KMOD_BO_FLAG_IO_COHERENT))
245 return;
246
247 /* If we have userspace cache flushing ops, use them instead of trapping
248 * through to the kernel.
249 */
250 if (pan_kmod_can_sync_bo_map_from_userland(dev)) {
251 /* Pre-flush needs to be executed before each flush/inval operation, but
252 * we can batch the post flush/inval fence. util_pre_flush_fence() being
253 * a NOP on aarch64, it's effectively free there, but we keep it here for
254 * clarity (not sure we care about Mali on x86 to be honest :D).
255 */
256 util_pre_flush_fence();
257
258 if (type == PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH)
259 util_flush_range_no_fence(cpu_ptr, range);
260 else
261 util_flush_inval_range_no_fence(cpu_ptr, range);
262
263 /* The util_pre_flush_inval_fence() is inserted by
264 * pan_kmod_flush_bo_map_syncs() to avoid unnecessary serialization when
265 * flush/invalidate operations are batched.
266 */
267 p_atomic_set(&dev->pending_bo_syncs.user_cache_ops_pending, true);
268 return;
269 }
270
271 simple_mtx_lock(&dev->pending_bo_syncs.lock);
272
273 /* If we reach the limit, flush the pending ops before queuing new ones. */
274 if (util_dynarray_num_elements(&dev->pending_bo_syncs.array,
275 struct pan_kmod_deferred_bo_sync) >=
276 MAX_PENDING_SYNC_OPS)
277 pan_kmod_flush_bo_map_syncs_locked(dev);
278
279 uint64_t start = bo_offset & ~((uint64_t)util_cache_granularity() - 1);
280 uint64_t end = ALIGN_POT(bo_offset + range, util_cache_granularity());
281
282 struct pan_kmod_deferred_bo_sync new_sync = {
283 .bo = bo,
284 .start = start,
285 .size = end - start,
286 .type = type,
287 };
288
289 bo->has_pending_deferred_syncs = true;
290 util_dynarray_append(&dev->pending_bo_syncs.array, new_sync);
291
292 simple_mtx_unlock(&dev->pending_bo_syncs.lock);
293 }