Debian Codegraph @

src/panfrost/lib/kmod/panthor_kmod.c

raw
1 /*
2 * Copyright © 2023 Collabora, Ltd.
3 * SPDX-License-Identifier: MIT
4 */
5
6 #include <errno.h>
7 #include <fcntl.h>
8 #include <string.h>
9 #include <xf86drm.h>
10
11 #include "util/hash_table.h"
12 #include "util/libsync.h"
13 #include "util/macros.h"
14 #include "util/os_time.h"
15 #include "util/stack_array.h"
16 #include "util/simple_mtx.h"
17 #include "util/u_debug.h"
18 #include "util/vma.h"
19
20 #include "drm-uapi/dma-buf.h"
21 #include "drm-uapi/panthor_drm.h"
22
23 #include "pan_kmod_backend.h"
24 #include "pan_props.h"
25
26 /* Maximum kmod BO label length, including NUL-terminator */
27 #define PANTHOR_BO_LABEL_MAXLEN 4096
28
29 const struct pan_kmod_ops panthor_kmod_ops;
30
31 /* Objects used to track VAs returned through async unmaps. */
32 struct panthor_kmod_va_collect {
33 struct list_head node;
34
35 /* VM sync point at which the VA range should be released. */
36 uint64_t sync_point;
37
38 /* Start of the VA range to release. */
39 uint64_t va;
40
41 /* Size of the VA range to release. */
42 uint64_t size;
43 };
44
45 struct panthor_kmod_vm {
46 struct pan_kmod_vm base;
47
48 /* Fields used for auto-VA management. Since the kernel doesn't do it for
49 * us, we need to deal with the VA allocation ourselves.
50 */
51 struct {
52 /* Lock protecting VA allocation/freeing. */
53 simple_mtx_t lock;
54
55 /* VA heap used to automatically assign a VA. */
56 struct util_vma_heap heap;
57
58 /* VA ranges to garbage collect. */
59 struct list_head gc_list;
60 } auto_va;
61
62 /* Fields used for VM activity tracking (TRACK_ACTIVITY flag). */
63 struct {
64 /* VM sync handle. */
65 uint32_t handle;
66
67 /* Current VM sync point. Incremented every time a GPU job or VM
68 * operation is issued.
69 */
70 uint64_t point;
71
72 /* Lock protecting insertion of sync points to the timeline syncobj. */
73 simple_mtx_t lock;
74 } sync;
75 };
76
77 struct panthor_kmod_dev {
78 struct pan_kmod_dev base;
79
80 /* Userspace mapping of the LATEST_FLUSH_ID register page. */
81 uint32_t *flush_id;
82
83 /* Cached device properties. Filled at device creation time. */
84 struct {
85 struct drm_panthor_gpu_info gpu;
86 struct drm_panthor_csif_info csif;
87 struct drm_panthor_timestamp_info timestamp;
88 struct drm_panthor_group_priorities_info group_priorities;
89 } props;
90 };
91
92 struct panthor_kmod_bo {
93 struct pan_kmod_bo base;
94 struct {
95 /* BO sync handle. Will point to the VM BO if the object is not shared. */
96 uint32_t handle;
97
98 /* BO read sync point. Zero when the object is shared. */
99 uint64_t read_point;
100
101 /* BO write sync point. Zero when the object is shared. */
102 uint64_t write_point;
103 } sync;
104 };
105
106 static uint32_t
107 to_kmod_group_allow_priority_flags(uint32_t panthor_flags)
108 {
109 uint32_t kmod_flags = 0;
110
111 if (panthor_flags & BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_REALTIME))
112 kmod_flags |= PAN_KMOD_GROUP_ALLOW_PRIORITY_REALTIME;
113
114 if (panthor_flags & BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_HIGH))
115 kmod_flags |= PAN_KMOD_GROUP_ALLOW_PRIORITY_HIGH;
116
117 if (panthor_flags & BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_MEDIUM))
118 kmod_flags |= PAN_KMOD_GROUP_ALLOW_PRIORITY_MEDIUM;
119
120 if (panthor_flags & BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_LOW))
121 kmod_flags |= PAN_KMOD_GROUP_ALLOW_PRIORITY_LOW;
122
123 return kmod_flags;
124 }
125
126 static void
127 panthor_dev_query_thread_props(struct panthor_kmod_dev *panthor_dev)
128 {
129 struct pan_kmod_dev_props *props = &panthor_dev->base.props;
130
131 props->max_threads_per_wg = panthor_dev->props.gpu.thread_max_workgroup_size;
132 props->max_threads_per_core = panthor_dev->props.gpu.max_threads;
133 props->max_tasks_per_core = panthor_dev->props.gpu.thread_features >> 24;
134 props->num_registers_per_core =
135 panthor_dev->props.gpu.thread_features & 0x3fffff;
136
137 /* We assume that all thread properties are populated. If we ever have a GPU
138 * that have one of the THREAD_xxx register that's zero, we can always add a
139 * quirk here.
140 */
141 assert(props->max_threads_per_wg && props->max_threads_per_core &&
142 props->max_tasks_per_core && props->num_registers_per_core);
143
144 /* There is no THREAD_TLS_ALLOC register on v10+, and the maximum number
145 * of TLS instance per core is assumed to be the maximum number of threads
146 * per core.
147 */
148 props->max_tls_instance_per_core = props->max_threads_per_core;
149 }
150
151 static void
152 panthor_dev_query_props(struct panthor_kmod_dev *panthor_dev)
153 {
154 struct pan_kmod_dev_props *props = &panthor_dev->base.props;
155
156 *props = (struct pan_kmod_dev_props){
157 .gpu_id = panthor_dev->props.gpu.gpu_id,
158 .gpu_variant = panthor_dev->props.gpu.core_features & 0xff,
159 .shader_present = panthor_dev->props.gpu.shader_present,
160 .tiler_features = panthor_dev->props.gpu.tiler_features,
161 .mem_features = panthor_dev->props.gpu.mem_features,
162 .mmu_features = panthor_dev->props.gpu.mmu_features,
163
164 /* This register does not exist because AFBC is no longer optional. */
165 .afbc_features = 0,
166
167 /* Access to timstamp from the GPU is always supported on Panthor. */
168 .gpu_can_query_timestamp = true,
169
170 .timestamp_frequency = panthor_dev->props.timestamp.timestamp_frequency,
171
172 .allowed_group_priorities_mask = to_kmod_group_allow_priority_flags(
173 panthor_dev->props.group_priorities.allowed_mask),
174
175 .supported_bo_flags = PAN_KMOD_BO_FLAG_EXECUTABLE |
176 PAN_KMOD_BO_FLAG_NO_MMAP |
177 PAN_KMOD_BO_FLAG_GPU_UNCACHED,
178 };
179
180 if (pan_kmod_driver_version_at_least(&panthor_dev->base.driver, 1, 6))
181 props->timestamp_device_coherent = true;
182
183 if (pan_kmod_driver_version_at_least(&panthor_dev->base.driver, 1, 7)) {
184 props->is_io_coherent = panthor_dev->props.gpu.selected_coherency !=
185 DRM_PANTHOR_GPU_COHERENCY_NONE;
186 props->supported_bo_flags |= PAN_KMOD_BO_FLAG_WB_MMAP;
187 }
188
189 static_assert(sizeof(props->texture_features) ==
190 sizeof(panthor_dev->props.gpu.texture_features),
191 "Mismatch in texture_features array size");
192
193 memcpy(props->texture_features, panthor_dev->props.gpu.texture_features,
194 sizeof(props->texture_features));
195
196 panthor_dev_query_thread_props(panthor_dev);
197 }
198
199 static struct pan_kmod_dev *
200 panthor_kmod_dev_create(int fd, uint32_t flags, drmVersionPtr version,
201 const struct pan_kmod_allocator *allocator)
202 {
203 struct panthor_kmod_dev *panthor_dev =
204 pan_kmod_alloc(allocator, sizeof(*panthor_dev));
205 if (!panthor_dev) {
206 mesa_loge("failed to allocate a panthor_kmod_dev object");
207 return NULL;
208 }
209
210 /* Cache GPU and CSIF information. */
211 struct drm_panthor_dev_query query = {
212 .type = DRM_PANTHOR_DEV_QUERY_GPU_INFO,
213 .size = sizeof(panthor_dev->props.gpu),
214 .pointer = (uint64_t)(uintptr_t)&panthor_dev->props.gpu,
215 };
216
217 int ret = pan_kmod_ioctl(fd, DRM_IOCTL_PANTHOR_DEV_QUERY, &query);
218 if (ret) {
219 mesa_loge("DRM_IOCTL_PANTHOR_DEV_QUERY failed (err=%d)", errno);
220 goto err_free_dev;
221 }
222
223 query = (struct drm_panthor_dev_query){
224 .type = DRM_PANTHOR_DEV_QUERY_CSIF_INFO,
225 .size = sizeof(panthor_dev->props.csif),
226 .pointer = (uint64_t)(uintptr_t)&panthor_dev->props.csif,
227 };
228
229 ret = pan_kmod_ioctl(fd, DRM_IOCTL_PANTHOR_DEV_QUERY, &query);
230 if (ret) {
231 mesa_loge("DRM_IOCTL_PANTHOR_DEV_QUERY failed (err=%d)", errno);
232 goto err_free_dev;
233 }
234
235 if (version->version_major > 1 || version->version_minor >= 1) {
236 query = (struct drm_panthor_dev_query){
237 .type = DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO,
238 .size = sizeof(panthor_dev->props.timestamp),
239 .pointer = (uint64_t)(uintptr_t)&panthor_dev->props.timestamp,
240 };
241
242 ret = pan_kmod_ioctl(fd, DRM_IOCTL_PANTHOR_DEV_QUERY, &query);
243 if (ret) {
244 mesa_loge("DRM_IOCTL_PANTHOR_DEV_QUERY failed (err=%d)", errno);
245 goto err_free_dev;
246 }
247 }
248
249 /* Map the LATEST_FLUSH_ID register at device creation time. */
250 if (version->version_major > 1 || version->version_minor >= 5) {
251 struct drm_panthor_set_user_mmio_offset user_mmio_offset = {
252 .offset = DRM_PANTHOR_USER_MMIO_OFFSET,
253 };
254
255 ret = drmIoctl(fd, DRM_IOCTL_PANTHOR_SET_USER_MMIO_OFFSET, &user_mmio_offset);
256 if (ret) {
257 mesa_loge("DRM_IOCTL_PANTHOR_SET_USER_MMIO_OFFSET, failed (err=%d)", errno);
258 goto err_free_dev;
259 }
260 }
261
262 panthor_dev->flush_id = os_mmap(0, getpagesize(), PROT_READ, MAP_SHARED, fd,
263 DRM_PANTHOR_USER_FLUSH_ID_MMIO_OFFSET);
264 if (panthor_dev->flush_id == MAP_FAILED) {
265 mesa_loge("failed to mmap the LATEST_FLUSH_ID register (err=%d)", errno);
266 goto err_free_dev;
267 }
268
269 if (version->version_major > 1 || version->version_minor >= 2) {
270 query = (struct drm_panthor_dev_query){
271 .type = DRM_PANTHOR_DEV_QUERY_GROUP_PRIORITIES_INFO,
272 .size = sizeof(panthor_dev->props.group_priorities),
273 .pointer = (uint64_t)(uintptr_t)&panthor_dev->props.group_priorities,
274 };
275
276 ret = pan_kmod_ioctl(fd, DRM_IOCTL_PANTHOR_DEV_QUERY, &query);
277 if (ret) {
278 mesa_loge("DRM_IOCTL_PANTHOR_DEV_QUERY failed (err=%d)", errno);
279 goto err_free_dev;
280 }
281 } else {
282 /* If the query isn't available, Panthor always allow LOW and MEDIUM
283 * priority */
284 panthor_dev->props.group_priorities.allowed_mask |=
285 BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_MEDIUM);
286 panthor_dev->props.group_priorities.allowed_mask |=
287 BITFIELD_BIT(PANTHOR_GROUP_PRIORITY_LOW);
288 }
289
290 assert(!ret);
291
292 pan_kmod_dev_init(&panthor_dev->base, fd, flags, version,
293 &panthor_kmod_ops, allocator);
294 panthor_dev_query_props(panthor_dev);
295
296 return &panthor_dev->base;
297
298 err_free_dev:
299 pan_kmod_free(allocator, panthor_dev);
300 return NULL;
301 }
302
303 static void
304 panthor_kmod_dev_destroy(struct pan_kmod_dev *dev)
305 {
306 struct panthor_kmod_dev *panthor_dev =
307 container_of(dev, struct panthor_kmod_dev, base);
308
309 os_munmap(panthor_dev->flush_id, getpagesize());
310 pan_kmod_dev_cleanup(dev);
311 pan_kmod_free(dev->allocator, panthor_dev);
312 }
313
314 static struct pan_kmod_va_range
315 panthor_kmod_dev_query_user_va_range(const struct pan_kmod_dev *dev)
316 {
317 struct panthor_kmod_dev *panthor_dev =
318 container_of(dev, struct panthor_kmod_dev, base);
319 uint8_t va_bits = MMU_FEATURES_VA_BITS(panthor_dev->props.gpu.mmu_features);
320
321 /* If we have less than 32-bit VA space it starts to be tricky, so let's
322 * assume we always have at least that.
323 */
324 assert(va_bits >= 32);
325
326 return (struct pan_kmod_va_range){
327 .start = 0,
328
329 /* 3G/1G user/kernel VA split for 32-bit VA space. Otherwise, we reserve
330 * half of the VA space for kernel objects.
331 */
332 .size =
333 va_bits == 32 ? (1ull << (va_bits - 2)) * 3 : 1ull << (va_bits - 1),
334 };
335 }
336
337 static uint32_t
338 to_panthor_bo_flags(uint32_t flags)
339 {
340 uint32_t panthor_flags = 0;
341
342 if (flags & PAN_KMOD_BO_FLAG_NO_MMAP)
343 panthor_flags |= DRM_PANTHOR_BO_NO_MMAP;
344
345 if (flags & PAN_KMOD_BO_FLAG_WB_MMAP) {
346 assert(!(flags & PAN_KMOD_BO_FLAG_NO_MMAP));
347 panthor_flags |= DRM_PANTHOR_BO_WB_MMAP;
348 }
349
350 return panthor_flags;
351 }
352
353 static struct pan_kmod_bo *
354 panthor_kmod_bo_alloc(struct pan_kmod_dev *dev,
355 struct pan_kmod_vm *exclusive_vm, uint64_t size,
356 uint32_t flags)
357 {
358 /* We don't support allocating on-fault. */
359 if (flags & PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT) {
360 mesa_loge("panthor_kmod doesn't support PAN_KMOD_BO_FLAG_ALLOC_ON_FAULT");
361 return NULL;
362 }
363
364 struct panthor_kmod_vm *panthor_vm =
365 exclusive_vm ? container_of(exclusive_vm, struct panthor_kmod_vm, base)
366 : NULL;
367 struct panthor_kmod_bo *bo = pan_kmod_dev_alloc(dev, sizeof(*bo));
368 if (!bo) {
369 mesa_loge("failed to allocate a panthor_kmod_bo object");
370 return NULL;
371 }
372
373 struct drm_panthor_bo_create req = {
374 .size = size,
375 .flags = to_panthor_bo_flags(flags),
376 .exclusive_vm_id = panthor_vm ? panthor_vm->base.handle : 0,
377 };
378
379 int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANTHOR_BO_CREATE, &req);
380 if (ret) {
381 mesa_loge("DRM_IOCTL_PANTHOR_BO_CREATE failed (err=%d)", errno);
382 goto err_free_bo;
383 }
384
385 if (!exclusive_vm) {
386 /* For buffers we know will be shared, create our own syncobj. */
387 int ret = drmSyncobjCreate(dev->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
388 &bo->sync.handle);
389 if (ret) {
390 mesa_loge("drmSyncobjCreate() failed (err=%d)", errno);
391 goto err_destroy_bo;
392 }
393 } else {
394 /* If the buffer is private to the VM, we just use the VM syncobj. */
395 bo->sync.handle = panthor_vm->sync.handle;
396 }
397
398 bo->sync.read_point = bo->sync.write_point = 0;
399
400 pan_kmod_bo_init(&bo->base, dev, exclusive_vm, req.size, flags, req.handle);
401 return &bo->base;
402
403 err_destroy_bo:
404 drmCloseBufferHandle(dev->fd, bo->base.handle);
405 err_free_bo:
406 pan_kmod_dev_free(dev, bo);
407 return NULL;
408 }
409
410 static void
411 panthor_kmod_bo_free(struct pan_kmod_bo *bo)
412 {
413 struct panthor_kmod_bo *panthor_bo =
414 container_of(bo, struct panthor_kmod_bo, base);
415
416 pan_kmod_bo_cleanup(bo);
417
418 if (!bo->exclusive_vm)
419 drmSyncobjDestroy(bo->dev->fd, panthor_bo->sync.handle);
420
421 drmCloseBufferHandle(bo->dev->fd, bo->handle);
422 pan_kmod_dev_free(bo->dev, bo);
423 }
424
425 static struct pan_kmod_bo *
426 panthor_kmod_bo_import(struct pan_kmod_dev *dev, uint32_t handle, uint64_t size)
427 {
428 int ret;
429 struct panthor_kmod_bo *panthor_bo =
430 pan_kmod_dev_alloc(dev, sizeof(*panthor_bo));
431 if (!panthor_bo) {
432 mesa_loge("failed to allocate a panthor_kmod_bo object");
433 return NULL;
434 }
435
436 uint32_t flags = PAN_KMOD_BO_FLAG_IMPORTED;
437 if (pan_kmod_driver_version_at_least(&dev->driver, 1, 7)) {
438 struct drm_panthor_bo_query_info args = {
439 .handle = handle,
440 };
441
442 ret = drmIoctl(dev->fd, DRM_IOCTL_PANTHOR_BO_QUERY_INFO, &args);
443 if (ret) {
444 mesa_loge("PANTHOR_BO_QUERY_INFO failed (err=%d)", errno);
445 goto err_free_bo;
446 }
447
448 /* FIXME: If the BO comes from a different subsystem
449 * (args.extra_flags & DRM_PANTHOR_BO_IS_IMPORTED), we should normally
450 * add extra DMA_BUF_IOCTL_SYNC calls around CPU accesses to ensure the
451 * CPU mapping consistency, but this is something we never worried about
452 * (we've always assumed exporters were exposing uncached mappings with
453 * NOP {begin,end}_cpu_access() implementations), and it worked fine until
454 * now.
455 * The long term plan is to hook up DMA_BUF_IOCTL_SYNC, but this requires
456 * more work.
457 */
458 }
459
460 /* Create a unsignalled syncobj on import. Will serve as a
461 * temporary container for the exported dmabuf sync file.
462 */
463 ret = drmSyncobjCreate(dev->fd, 0, &panthor_bo->sync.handle);
464 if (ret) {
465 mesa_loge("drmSyncobjCreate() failed (err=%d)", errno);
466 goto err_free_bo;
467 }
468
469 pan_kmod_bo_init(&panthor_bo->base, dev, NULL, size, flags, handle);
470 return &panthor_bo->base;
471
472 err_free_bo:
473 pan_kmod_dev_free(dev, panthor_bo);
474 return NULL;
475 }
476
477 static int
478 panthor_kmod_bo_export(struct pan_kmod_bo *bo, int dmabuf_fd)
479 {
480 struct panthor_kmod_bo *panthor_bo =
481 container_of(bo, struct panthor_kmod_bo, base);
482
483 bool shared =
484 bo->flags & (PAN_KMOD_BO_FLAG_EXPORTED | PAN_KMOD_BO_FLAG_IMPORTED);
485
486 /* If the BO wasn't already shared, we migrate our internal sync points to
487 * the dmabuf itself, so implicit sync can work correctly after this point.
488 */
489 if (!shared) {
490 if (panthor_bo->sync.read_point || panthor_bo->sync.write_point) {
491 struct dma_buf_import_sync_file isync = {
492 .flags = DMA_BUF_SYNC_RW,
493 };
494 int ret = drmSyncobjExportSyncFile(bo->dev->fd,
495 panthor_bo->sync.handle, &isync.fd);
496 if (ret) {
497 mesa_loge("drmSyncobjExportSyncFile() failed (err=%d)", errno);
498 return -1;
499 }
500
501 ret = pan_kmod_ioctl(dmabuf_fd, DMA_BUF_IOCTL_IMPORT_SYNC_FILE,
502 &isync);
503 close(isync.fd);
504 if (ret) {
505 mesa_loge("DMA_BUF_IOCTL_IMPORT_SYNC_FILE failed (err=%d)", errno);
506 return -1;
507 }
508 }
509
510 /* Make sure we reset the syncobj on export. We will use it as a
511 * temporary binary syncobj to import sync_file FD from now on.
512 */
513 int ret = drmSyncobjReset(bo->dev->fd, &panthor_bo->sync.handle, 1);
514 if (ret) {
515 mesa_loge("drmSyncobjReset() failed (err=%d)", errno);
516 return -1;
517 }
518
519 panthor_bo->sync.read_point = 0;
520 panthor_bo->sync.write_point = 0;
521 }
522
523 bo->flags |= PAN_KMOD_BO_FLAG_EXPORTED;
524 return 0;
525 }
526
527 static off_t
528 panthor_kmod_bo_get_mmap_offset(struct pan_kmod_bo *bo)
529 {
530 struct drm_panthor_bo_mmap_offset req = {.handle = bo->handle};
531 int ret = pan_kmod_ioctl(bo->dev->fd, DRM_IOCTL_PANTHOR_BO_MMAP_OFFSET,
532 &req);
533
534 if (ret) {
535 mesa_loge("DRM_IOCTL_PANTHOR_BO_MMAP_OFFSET failed (err=%d)", errno);
536 return -1;
537 }
538
539 return req.offset;
540 }
541
542 static bool
543 panthor_kmod_bo_wait(struct pan_kmod_bo *bo, int64_t timeout_ns,
544 bool for_read_only_access)
545 {
546 struct panthor_kmod_bo *panthor_bo =
547 container_of(bo, struct panthor_kmod_bo, base);
548 bool shared =
549 bo->flags & (PAN_KMOD_BO_FLAG_EXPORTED | PAN_KMOD_BO_FLAG_IMPORTED);
550
551 if (shared) {
552 /* If the object is shared, we have to do this export sync-file dance
553 * to reconcile with the implicit sync model. This implies exporting
554 * our GEM object as a dma-buf and closing it right after the
555 * EXPORT_SYNC_FILE, unfortunately.
556 */
557 int dmabuf_fd;
558 int ret =
559 drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, &dmabuf_fd);
560
561 if (ret) {
562 mesa_loge("drmPrimeHandleToFD() failed (err=%d)", errno);
563 return false;
564 }
565
566 struct dma_buf_export_sync_file esync = {
567 .flags = for_read_only_access ? DMA_BUF_SYNC_READ : DMA_BUF_SYNC_RW,
568 };
569
570 ret = pan_kmod_ioctl(dmabuf_fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &esync);
571 close(dmabuf_fd);
572
573 if (ret) {
574 mesa_loge("DMA_BUF_IOCTL_EXPORT_SYNC_FILE failed (err=%d)", errno);
575 return false;
576 }
577
578 ret = sync_wait(esync.fd, timeout_ns / 1000000);
579 close(esync.fd);
580 return ret == 0;
581 } else {
582 /* Waiting on non-shared object is much simpler. We just pick the
583 * right sync point based on for_read_only_access and call
584 * drmSyncobjTimelineWait().
585 */
586 uint64_t sync_point =
587 for_read_only_access
588 ? panthor_bo->sync.write_point
589 : MAX2(panthor_bo->sync.write_point, panthor_bo->sync.read_point);
590
591 if (!sync_point)
592 return true;
593
594 int64_t abs_timeout_ns = timeout_ns < INT64_MAX - os_time_get_nano()
595 ? timeout_ns + os_time_get_nano()
596 : INT64_MAX;
597 int ret = drmSyncobjTimelineWait(bo->dev->fd, &panthor_bo->sync.handle,
598 &sync_point, 1, abs_timeout_ns,
599 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
600 if (ret >= 0)
601 return true;
602
603 if (ret != -ETIME)
604 mesa_loge("DMA_BUF_IOCTL_EXPORT_SYNC_FILE failed (err=%d)", ret);
605
606 return false;
607 }
608 }
609
610 static int
611 panthor_kmod_flush_bo_map_syncs(struct pan_kmod_dev *dev)
612 {
613 STACK_ARRAY(struct drm_panthor_bo_sync_op, panthor_ops,
614 util_dynarray_num_elements(&dev->pending_bo_syncs.array,
615 struct pan_kmod_deferred_bo_sync));
616
617 uint32_t panthor_count = 0;
618 util_dynarray_foreach(&dev->pending_bo_syncs.array,
619 struct pan_kmod_deferred_bo_sync, sync) {
620 panthor_ops[panthor_count++] = (struct drm_panthor_bo_sync_op){
621 .handle = sync->bo->handle,
622 .type = sync->type == PAN_KMOD_BO_SYNC_CPU_CACHE_FLUSH
623 ? DRM_PANTHOR_BO_SYNC_CPU_CACHE_FLUSH
624 : DRM_PANTHOR_BO_SYNC_CPU_CACHE_FLUSH_AND_INVALIDATE,
625 .offset = sync->start,
626 .size = sync->size,
627 };
628 }
629
630 struct drm_panthor_bo_sync req = {
631 .ops = DRM_PANTHOR_OBJ_ARRAY(panthor_count, panthor_ops),
632 };
633 int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANTHOR_BO_SYNC, &req);
634 if (ret)
635 mesa_loge("DRM_IOCTL_PANTHOR_BO_SYNC failed (err=%d)", errno);
636
637 STACK_ARRAY_FINISH(panthor_ops);
638
639 return ret;
640 }
641
642 /* Attach a sync to a buffer object. */
643 int
644 panthor_kmod_bo_attach_sync_point(struct pan_kmod_bo *bo, uint32_t sync_handle,
645 uint64_t sync_point, bool written)
646 {
647 struct panthor_kmod_bo *panthor_bo =
648 container_of(bo, struct panthor_kmod_bo, base);
649 struct panthor_kmod_vm *panthor_vm =
650 bo->exclusive_vm
651 ? container_of(bo->exclusive_vm, struct panthor_kmod_vm, base)
652 : NULL;
653 bool shared =
654 bo->flags & (PAN_KMOD_BO_FLAG_EXPORTED | PAN_KMOD_BO_FLAG_IMPORTED);
655
656 if (shared) {
657 /* Reconciling explicit/implicit sync again: we need to import the
658 * new sync point in the dma-buf, so other parties can rely on
659 * implicit deps.
660 */
661 struct dma_buf_import_sync_file isync = {
662 .flags = written ? DMA_BUF_SYNC_RW : DMA_BUF_SYNC_READ,
663 };
664 int dmabuf_fd;
665 int ret = drmSyncobjExportSyncFile(bo->dev->fd, sync_handle, &isync.fd);
666 if (ret) {
667 mesa_loge("drmSyncobjExportSyncFile() failed (err=%d)", errno);
668 return -1;
669 }
670
671 ret =
672 drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, &dmabuf_fd);
673 if (ret) {
674 mesa_loge("drmPrimeHandleToFD() failed (err=%d)", errno);
675 close(isync.fd);
676 return -1;
677 }
678
679 ret = pan_kmod_ioctl(dmabuf_fd, DMA_BUF_IOCTL_IMPORT_SYNC_FILE, &isync);
680 close(dmabuf_fd);
681 close(isync.fd);
682 if (ret) {
683 mesa_loge("DMA_BUF_IOCTL_IMPORT_SYNC_FILE failed (err=%d)", errno);
684 return -1;
685 }
686 } else if (panthor_vm) {
687 /* Private BOs should be passed the VM syncobj. */
688 assert(sync_handle == panthor_vm->sync.handle);
689
690 panthor_bo->sync.read_point =
691 MAX2(sync_point, panthor_bo->sync.read_point);
692 if (written) {
693 panthor_bo->sync.write_point =
694 MAX2(sync_point, panthor_bo->sync.write_point);
695 }
696 } else {
697 /* For non-private BOs that are not shared yet, we add a new sync point
698 * to our timeline syncobj, and push the sync there.
699 */
700 uint32_t new_sync_point =
701 MAX2(panthor_bo->sync.write_point, panthor_bo->sync.read_point) + 1;
702
703 int ret = drmSyncobjTransfer(bo->dev->fd, panthor_bo->sync.handle,
704 new_sync_point, sync_handle, sync_point, 0);
705 if (ret) {
706 mesa_loge("drmSyncobjTransfer() failed (err=%d)", errno);
707 return -1;
708 }
709
710 panthor_bo->sync.read_point = new_sync_point;
711 if (written)
712 panthor_bo->sync.write_point = new_sync_point;
713 }
714
715 return 0;
716 }
717
718 /* Get the sync point for a read or write operation on a buffer object. */
719 int
720 panthor_kmod_bo_get_sync_point(struct pan_kmod_bo *bo, uint32_t *sync_handle,
721 uint64_t *sync_point, bool for_read_only_access)
722 {
723 struct panthor_kmod_bo *panthor_bo =
724 container_of(bo, struct panthor_kmod_bo, base);
725 bool shared =
726 bo->flags & (PAN_KMOD_BO_FLAG_EXPORTED | PAN_KMOD_BO_FLAG_IMPORTED);
727
728 if (shared) {
729 /* Explicit/implicit sync reconciliation point. We need to export
730 * a sync-file from the dmabuf and make it a syncobj.
731 */
732 int dmabuf_fd;
733 int ret =
734 drmPrimeHandleToFD(bo->dev->fd, bo->handle, DRM_CLOEXEC | DRM_RDWR, &dmabuf_fd);
735 if (ret) {
736 mesa_loge("drmPrimeHandleToFD() failed (err=%d)\n", errno);
737 return -1;
738 }
739
740 struct dma_buf_export_sync_file esync = {
741 .flags = for_read_only_access ? DMA_BUF_SYNC_READ : DMA_BUF_SYNC_RW,
742 };
743
744 ret = pan_kmod_ioctl(dmabuf_fd, DMA_BUF_IOCTL_EXPORT_SYNC_FILE, &esync);
745 close(dmabuf_fd);
746 if (ret) {
747 mesa_loge("DMA_BUF_IOCTL_EXPORT_SYNC_FILE failed (err=%d)", errno);
748 return -1;
749 }
750
751 /* We store the resulting sync in our BO syncobj, which will be assigned
752 * a new sync next time we enter this function.
753 */
754 ret = drmSyncobjImportSyncFile(bo->dev->fd, panthor_bo->sync.handle,
755 esync.fd);
756 close(esync.fd);
757 if (ret) {
758 mesa_loge("drmSyncobjImportSyncFile() failed (err=%d)", errno);
759 return -1;
760 }
761
762 /* The syncobj is a binary syncobj in that case. */
763 *sync_handle = panthor_bo->sync.handle;
764 *sync_point = 0;
765 } else {
766 /* Fortunately, the non-shared path is much simpler, we just return
767 * the read/write sync point depending on the access type. The syncobj
768 * is a timeline syncobj in that case.
769 */
770 *sync_handle = panthor_bo->sync.handle;
771 *sync_point = for_read_only_access ? panthor_bo->sync.write_point
772 : MAX2(panthor_bo->sync.read_point,
773 panthor_bo->sync.write_point);
774 }
775 return 0;
776 }
777
778 static struct pan_kmod_vm *
779 panthor_kmod_vm_create(struct pan_kmod_dev *dev, uint32_t flags,
780 uint64_t user_va_start, uint64_t user_va_range)
781 {
782 struct panthor_kmod_vm *panthor_vm =
783 pan_kmod_dev_alloc(dev, sizeof(*panthor_vm));
784 if (!panthor_vm) {
785 mesa_loge("failed to allocate a panthor_kmod_vm object");
786 return NULL;
787 }
788
789 if (flags & PAN_KMOD_VM_FLAG_AUTO_VA) {
790 simple_mtx_init(&panthor_vm->auto_va.lock, mtx_plain);
791 list_inithead(&panthor_vm->auto_va.gc_list);
792 util_vma_heap_init(&panthor_vm->auto_va.heap, user_va_start,
793 user_va_range);
794 }
795
796 if (flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY) {
797 simple_mtx_init(&panthor_vm->sync.lock, mtx_plain);
798 panthor_vm->sync.point = 0;
799 if (drmSyncobjCreate(dev->fd, DRM_SYNCOBJ_CREATE_SIGNALED,
800 &panthor_vm->sync.handle)) {
801 mesa_loge("drmSyncobjCreate() failed (err=%d)", errno);
802 goto err_free_vm;
803 }
804 }
805
806 struct drm_panthor_vm_create req = {
807 .user_va_range = user_va_start + user_va_range,
808 };
809
810 if (pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANTHOR_VM_CREATE, &req)) {
811 mesa_loge("DRM_IOCTL_PANTHOR_VM_CREATE failed (err=%d)", errno);
812 goto err_destroy_sync;
813 }
814
815 pan_kmod_vm_init(&panthor_vm->base, dev, req.id, flags, PAN_PGSIZE_4K | PAN_PGSIZE_2M);
816 return &panthor_vm->base;
817
818 err_destroy_sync:
819 if (flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY) {
820 drmSyncobjDestroy(dev->fd, panthor_vm->sync.handle);
821 simple_mtx_destroy(&panthor_vm->sync.lock);
822 }
823
824 err_free_vm:
825 if (flags & PAN_KMOD_VM_FLAG_AUTO_VA) {
826 util_vma_heap_finish(&panthor_vm->auto_va.heap);
827 simple_mtx_destroy(&panthor_vm->auto_va.lock);
828 }
829
830 pan_kmod_dev_free(dev, panthor_vm);
831 return NULL;
832 }
833
834 static void
835 panthor_kmod_vm_collect_freed_vas(struct panthor_kmod_vm *vm)
836 {
837 if (!(vm->base.flags & PAN_KMOD_VM_FLAG_AUTO_VA))
838 return;
839
840 bool done = false;
841
842 simple_mtx_assert_locked(&vm->auto_va.lock);
843 list_for_each_entry_safe_rev(struct panthor_kmod_va_collect, req,
844 &vm->auto_va.gc_list, node)
845 {
846 /* Unmaps are queued in order of execution */
847 if (!done) {
848 int ret = drmSyncobjTimelineWait(
849 vm->base.dev->fd, &vm->sync.handle, &req->sync_point, 1, 0,
850 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL, NULL);
851 if (ret >= 0)
852 done = true;
853 else
854 continue;
855 }
856
857 list_del(&req->node);
858 util_vma_heap_free(&vm->auto_va.heap, req->va, req->size);
859 pan_kmod_dev_free(vm->base.dev, req);
860 }
861 }
862
863 static void
864 panthor_kmod_vm_destroy(struct pan_kmod_vm *vm)
865 {
866 struct panthor_kmod_vm *panthor_vm =
867 container_of(vm, struct panthor_kmod_vm, base);
868 struct drm_panthor_vm_destroy req = {.id = vm->handle};
869 int ret = pan_kmod_ioctl(vm->dev->fd, DRM_IOCTL_PANTHOR_VM_DESTROY, &req);
870 if (ret)
871 mesa_loge("DRM_IOCTL_PANTHOR_VM_DESTROY failed (err=%d)", errno);
872
873 assert(!ret);
874
875 if (panthor_vm->base.flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY) {
876 drmSyncobjDestroy(vm->dev->fd, panthor_vm->sync.handle);
877 simple_mtx_destroy(&panthor_vm->sync.lock);
878 }
879
880 if (panthor_vm->base.flags & PAN_KMOD_VM_FLAG_AUTO_VA) {
881 simple_mtx_lock(&panthor_vm->auto_va.lock);
882 list_for_each_entry_safe(struct panthor_kmod_va_collect, req,
883 &panthor_vm->auto_va.gc_list, node) {
884 list_del(&req->node);
885 util_vma_heap_free(&panthor_vm->auto_va.heap, req->va, req->size);
886 pan_kmod_dev_free(vm->dev, req);
887 }
888 util_vma_heap_finish(&panthor_vm->auto_va.heap);
889 simple_mtx_unlock(&panthor_vm->auto_va.lock);
890 simple_mtx_destroy(&panthor_vm->auto_va.lock);
891 }
892
893 pan_kmod_dev_free(vm->dev, panthor_vm);
894 }
895
896 static uint64_t
897 panthor_kmod_vm_alloc_va(struct panthor_kmod_vm *panthor_vm, uint64_t size)
898 {
899 uint64_t va;
900
901 assert(panthor_vm->base.flags & PAN_KMOD_VM_FLAG_AUTO_VA);
902
903 simple_mtx_lock(&panthor_vm->auto_va.lock);
904 panthor_kmod_vm_collect_freed_vas(panthor_vm);
905 va = util_vma_heap_alloc(&panthor_vm->auto_va.heap, size,
906 pan_choose_gpu_va_alignment(&panthor_vm->base, size));
907 simple_mtx_unlock(&panthor_vm->auto_va.lock);
908
909 return va;
910 }
911
912 static void
913 panthor_kmod_vm_free_va(struct panthor_kmod_vm *panthor_vm, uint64_t va,
914 uint64_t size)
915 {
916 assert(panthor_vm->base.flags & PAN_KMOD_VM_FLAG_AUTO_VA);
917
918 simple_mtx_lock(&panthor_vm->auto_va.lock);
919 util_vma_heap_free(&panthor_vm->auto_va.heap, va, size);
920 simple_mtx_unlock(&panthor_vm->auto_va.lock);
921 }
922
923 static int
924 panthor_kmod_vm_bind(struct pan_kmod_vm *vm, enum pan_kmod_vm_op_mode mode,
925 struct pan_kmod_vm_op *ops, uint32_t op_count)
926 {
927 struct panthor_kmod_vm *panthor_vm =
928 container_of(vm, struct panthor_kmod_vm, base);
929 struct drm_panthor_vm_bind_op bind_ops_storage[16];
930 struct drm_panthor_vm_bind_op *bind_ops = NULL;
931 struct drm_panthor_sync_op sync_ops_storage[16];
932 struct drm_panthor_sync_op *sync_ops = NULL;
933 uint32_t syncop_cnt = 0, syncop_ptr = 0;
934 bool async = mode == PAN_KMOD_VM_OP_MODE_ASYNC ||
935 mode == PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT;
936 bool auto_va = vm->flags & PAN_KMOD_VM_FLAG_AUTO_VA;
937 bool track_activity = vm->flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY;
938 struct panthor_kmod_va_collect *cur_va_collect = NULL;
939 struct list_head va_collect_list;
940 uint32_t va_collect_cnt = 0;
941 int ret = -1;
942
943 /* For any asynchronous VM bind, we assume the user is managing the VM
944 * address space, so we don't have to collect VMAs in that case.
945 */
946 if (mode == PAN_KMOD_VM_OP_MODE_ASYNC && auto_va) {
947 mesa_loge(
948 "auto-VA allocation is incompatible with PAN_KMOD_VM_OP_MODE_ASYNC");
949 return -1;
950 }
951
952 if (mode == PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT &&
953 !track_activity) {
954 mesa_loge(
955 "PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT requires PAN_KMOD_VM_FLAG_TRACK_ACTIVITY");
956 return -1;
957 }
958
959 if (op_count == 0)
960 return 0;
961
962 /* If this is an async operation and VM activity tracking is enabled, we
963 * reserve one syncop per VM operation for the signaling of our VM timeline
964 * slot.
965 */
966 if (async && track_activity)
967 syncop_cnt += op_count;
968
969 /* With PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT, we need to push our
970 * wait VM syncobj in all of the submissions, hence the extra syncop per
971 * operation.
972 */
973 if (mode == PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT)
974 syncop_cnt += op_count;
975
976 for (uint32_t i = 0; i < op_count; i++) {
977 if (pan_kmod_vm_op_check(vm, mode, &ops[i]))
978 return -1;
979
980 /* If auto-VA is used, for any asynchronous unmap operation, we need
981 * to register a VA collection node and add it to the GC list.
982 */
983 if (auto_va && async && ops[i].type == PAN_KMOD_VM_OP_TYPE_UNMAP &&
984 ops[i].va.size)
985 va_collect_cnt++;
986
987 syncop_cnt += ops[i].syncs.count;
988 }
989
990 /* Pre-allocate the VA collection nodes. */
991 list_inithead(&va_collect_list);
992 for (uint32_t i = 0; i < va_collect_cnt; i++) {
993 struct panthor_kmod_va_collect *va_collect =
994 pan_kmod_dev_alloc(vm->dev, sizeof(*va_collect));
995 if (!va_collect) {
996 mesa_loge("panthor_kmod_va_collect allocation failed");
997 goto out_free_va_collect;
998 }
999
1000 if (!i)
1001 cur_va_collect = va_collect;
1002
1003 list_addtail(&va_collect->node, &va_collect_list);
1004 }
1005
1006 if (syncop_cnt && syncop_cnt > ARRAY_SIZE(sync_ops_storage)) {
1007 sync_ops =
1008 pan_kmod_dev_alloc_transient(vm->dev, sizeof(*sync_ops) * syncop_cnt);
1009 if (!sync_ops) {
1010 mesa_loge("drm_panthor_sync_op[%d] array allocation failed",
1011 syncop_cnt);
1012 goto out_free_va_collect;
1013 }
1014 } else if (syncop_cnt) {
1015 sync_ops = sync_ops_storage;
1016 memset(sync_ops, 0, sizeof(*sync_ops) * syncop_cnt);
1017 }
1018
1019 if (op_count > ARRAY_SIZE(bind_ops_storage)) {
1020 bind_ops =
1021 pan_kmod_dev_alloc_transient(vm->dev, sizeof(*bind_ops) * op_count);
1022 if (!bind_ops) {
1023 mesa_loge("drm_panthor_vm_bind_op[%d] array allocation failed",
1024 op_count);
1025 goto out_free_sync_ops;
1026 }
1027 } else {
1028 bind_ops = bind_ops_storage;
1029 memset(bind_ops, 0, sizeof(*bind_ops) * op_count);
1030 }
1031
1032 struct drm_panthor_vm_bind req = {
1033 .vm_id = vm->handle,
1034 .flags =
1035 mode != PAN_KMOD_VM_OP_MODE_IMMEDIATE ? DRM_PANTHOR_VM_BIND_ASYNC : 0,
1036 .ops = DRM_PANTHOR_OBJ_ARRAY(op_count, bind_ops),
1037 };
1038
1039 uint64_t vm_orig_sync_point = 0, vm_new_sync_point = 0;
1040
1041 if (track_activity)
1042 vm_orig_sync_point = vm_new_sync_point = panthor_kmod_vm_sync_lock(vm);
1043
1044 for (uint32_t i = 0; i < op_count; i++) {
1045 uint64_t signal_vm_point = 0;
1046 uint32_t op_sync_cnt = 0;
1047
1048 if (async && track_activity) {
1049 signal_vm_point = ++vm_new_sync_point;
1050 op_sync_cnt++;
1051 sync_ops[syncop_ptr++] = (struct drm_panthor_sync_op){
1052 .flags = DRM_PANTHOR_SYNC_OP_SIGNAL |
1053 DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ,
1054 .handle = panthor_vm->sync.handle,
1055 .timeline_value = signal_vm_point,
1056 };
1057 }
1058
1059 if (mode == PAN_KMOD_VM_OP_MODE_DEFER_TO_NEXT_IDLE_POINT) {
1060 op_sync_cnt++;
1061 sync_ops[syncop_ptr++] = (struct drm_panthor_sync_op){
1062 .flags = DRM_PANTHOR_SYNC_OP_WAIT |
1063 DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ,
1064 .handle = panthor_vm->sync.handle,
1065 .timeline_value = vm_orig_sync_point,
1066 };
1067
1068 if (auto_va && ops[i].type == PAN_KMOD_VM_OP_TYPE_UNMAP &&
1069 ops[i].va.size) {
1070 struct panthor_kmod_va_collect *va_collect = cur_va_collect;
1071
1072 assert(&va_collect->node != &va_collect_list);
1073 assert(signal_vm_point);
1074 va_collect->sync_point = signal_vm_point;
1075 va_collect->va = ops[i].va.start;
1076 va_collect->size = ops[i].va.size;
1077
1078 cur_va_collect = list_entry(cur_va_collect->node.next,
1079 struct panthor_kmod_va_collect, node);
1080 }
1081 }
1082
1083 for (uint32_t j = 0; j < ops[i].syncs.count; j++) {
1084 sync_ops[syncop_ptr++] = (struct drm_panthor_sync_op){
1085 .flags = (ops[i].syncs.array[j].type == PAN_KMOD_SYNC_TYPE_WAIT
1086 ? DRM_PANTHOR_SYNC_OP_WAIT
1087 : DRM_PANTHOR_SYNC_OP_SIGNAL) |
1088 DRM_PANTHOR_SYNC_OP_HANDLE_TYPE_TIMELINE_SYNCOBJ,
1089 .handle = ops[i].syncs.array[j].handle,
1090 .timeline_value = ops[i].syncs.array[j].point,
1091 };
1092 }
1093 op_sync_cnt += ops[i].syncs.count;
1094
1095 bind_ops[i].syncs = (struct drm_panthor_obj_array)DRM_PANTHOR_OBJ_ARRAY(
1096 op_sync_cnt, op_sync_cnt ? &sync_ops[syncop_ptr - op_sync_cnt] : NULL);
1097
1098 if (ops[i].type == PAN_KMOD_VM_OP_TYPE_MAP) {
1099 bind_ops[i].flags = DRM_PANTHOR_VM_BIND_OP_TYPE_MAP;
1100 bind_ops[i].size = ops[i].va.size;
1101 bind_ops[i].bo_handle = ops[i].map.bo->handle;
1102 bind_ops[i].bo_offset = ops[i].map.bo_offset;
1103
1104 if (ops[i].va.start == PAN_KMOD_VM_MAP_AUTO_VA) {
1105 bind_ops[i].va =
1106 panthor_kmod_vm_alloc_va(panthor_vm, bind_ops[i].size);
1107 if (!bind_ops[i].va) {
1108 mesa_loge("VA allocation failed");
1109 ret = -1;
1110 goto out_update_vas;
1111 }
1112 } else {
1113 bind_ops[i].va = ops[i].va.start;
1114 }
1115
1116 if (ops[i].map.bo->flags & PAN_KMOD_BO_FLAG_EXECUTABLE)
1117 bind_ops[i].flags |= DRM_PANTHOR_VM_BIND_OP_MAP_READONLY;
1118 else
1119 bind_ops[i].flags |= DRM_PANTHOR_VM_BIND_OP_MAP_NOEXEC;
1120
1121 if (ops[i].map.bo->flags & PAN_KMOD_BO_FLAG_GPU_UNCACHED)
1122 bind_ops[i].flags |= DRM_PANTHOR_VM_BIND_OP_MAP_UNCACHED;
1123
1124 } else if (ops[i].type == PAN_KMOD_VM_OP_TYPE_UNMAP) {
1125 bind_ops[i].flags = DRM_PANTHOR_VM_BIND_OP_TYPE_UNMAP;
1126 bind_ops[i].va = ops[i].va.start;
1127 bind_ops[i].size = ops[i].va.size;
1128 } else {
1129 assert(ops[i].type == PAN_KMOD_VM_OP_TYPE_SYNC_ONLY);
1130 bind_ops[i].flags = DRM_PANTHOR_VM_BIND_OP_TYPE_SYNC_ONLY;
1131 }
1132 }
1133
1134 ret = pan_kmod_ioctl(vm->dev->fd, DRM_IOCTL_PANTHOR_VM_BIND, &req);
1135 if (ret)
1136 mesa_loge("DRM_IOCTL_PANTHOR_VM_BIND failed (err=%d)", errno);
1137
1138 if (!ret && va_collect_cnt) {
1139 assert(&cur_va_collect->node == &va_collect_list);
1140 simple_mtx_lock(&panthor_vm->auto_va.lock);
1141 list_splicetail(&va_collect_list, &panthor_vm->auto_va.gc_list);
1142 list_inithead(&va_collect_list);
1143 simple_mtx_unlock(&panthor_vm->auto_va.lock);
1144 }
1145
1146 out_update_vas:
1147 if (track_activity) {
1148 panthor_kmod_vm_sync_unlock(vm,
1149 ret ? vm_orig_sync_point : vm_new_sync_point);
1150 }
1151
1152 for (uint32_t i = 0; i < op_count; i++) {
1153 if (ops[i].type == PAN_KMOD_VM_OP_TYPE_MAP &&
1154 ops[i].va.start == PAN_KMOD_VM_MAP_AUTO_VA) {
1155 if (!ret) {
1156 ops[i].va.start = bind_ops[i].va;
1157 } else if (bind_ops[i].va != 0) {
1158 panthor_kmod_vm_free_va(panthor_vm, bind_ops[i].va,
1159 bind_ops[i].size);
1160 }
1161 }
1162
1163 if (ops[i].type == PAN_KMOD_VM_OP_TYPE_UNMAP && auto_va && !async &&
1164 !ret) {
1165 panthor_kmod_vm_free_va(panthor_vm, bind_ops[i].va, bind_ops[i].size);
1166 }
1167 }
1168
1169 if (bind_ops != bind_ops_storage)
1170 pan_kmod_dev_free(vm->dev, bind_ops);
1171
1172 out_free_sync_ops:
1173 if (sync_ops != sync_ops_storage)
1174 pan_kmod_dev_free(vm->dev, sync_ops);
1175
1176 out_free_va_collect:
1177 list_for_each_entry_safe(struct panthor_kmod_va_collect, va_collect,
1178 &va_collect_list, node) {
1179 list_del(&va_collect->node);
1180 pan_kmod_dev_free(vm->dev, va_collect);
1181 }
1182
1183 return ret;
1184 }
1185
1186 static enum pan_kmod_vm_state
1187 panthor_kmod_vm_query_state(struct pan_kmod_vm *vm)
1188 {
1189 struct drm_panthor_vm_get_state query = {.vm_id = vm->handle};
1190 int ret = pan_kmod_ioctl(vm->dev->fd, DRM_IOCTL_PANTHOR_VM_GET_STATE,
1191 &query);
1192
1193 if (ret || query.state == DRM_PANTHOR_VM_STATE_UNUSABLE)
1194 return PAN_KMOD_VM_FAULTY;
1195
1196 return PAN_KMOD_VM_USABLE;
1197 }
1198
1199 uint32_t
1200 panthor_kmod_vm_sync_handle(struct pan_kmod_vm *vm)
1201 {
1202 struct panthor_kmod_vm *panthor_vm =
1203 container_of(vm, struct panthor_kmod_vm, base);
1204
1205 assert(vm->flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY);
1206 return panthor_vm->sync.handle;
1207 }
1208
1209 uint64_t
1210 panthor_kmod_vm_sync_lock(struct pan_kmod_vm *vm)
1211 {
1212 struct panthor_kmod_vm *panthor_vm =
1213 container_of(vm, struct panthor_kmod_vm, base);
1214
1215 assert(vm->flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY);
1216
1217 simple_mtx_lock(&panthor_vm->sync.lock);
1218 return panthor_vm->sync.point;
1219 }
1220
1221 void
1222 panthor_kmod_vm_sync_unlock(struct pan_kmod_vm *vm, uint64_t new_sync_point)
1223 {
1224 struct panthor_kmod_vm *panthor_vm =
1225 container_of(vm, struct panthor_kmod_vm, base);
1226
1227 assert(vm->flags & PAN_KMOD_VM_FLAG_TRACK_ACTIVITY);
1228 assert(new_sync_point >= panthor_vm->sync.point);
1229
1230 /* Check that the new syncpoint has a fence attached to it. */
1231 assert(new_sync_point == panthor_vm->sync.point ||
1232 drmSyncobjTimelineWait(
1233 vm->dev->fd, &panthor_vm->sync.handle, &new_sync_point, 1, 0,
1234 DRM_SYNCOBJ_WAIT_FLAGS_WAIT_AVAILABLE, NULL) >= 0);
1235
1236 panthor_vm->sync.point = new_sync_point;
1237 simple_mtx_unlock(&panthor_vm->sync.lock);
1238 }
1239
1240 uint32_t
1241 panthor_kmod_get_flush_id(const struct pan_kmod_dev *dev)
1242 {
1243 struct panthor_kmod_dev *panthor_dev =
1244 container_of(dev, struct panthor_kmod_dev, base);
1245
1246 return *(panthor_dev->flush_id);
1247 }
1248
1249 const struct drm_panthor_csif_info *
1250 panthor_kmod_get_csif_props(const struct pan_kmod_dev *dev)
1251 {
1252 struct panthor_kmod_dev *panthor_dev =
1253 container_of(dev, struct panthor_kmod_dev, base);
1254
1255 return &panthor_dev->props.csif;
1256 }
1257
1258 static uint64_t
1259 panthor_kmod_query_timestamp(const struct pan_kmod_dev *dev)
1260 {
1261 if (!pan_kmod_driver_version_at_least(&dev->driver, 1, 1))
1262 return 0;
1263
1264 struct drm_panthor_timestamp_info timestamp_info = {};
1265
1266 struct drm_panthor_dev_query query = (struct drm_panthor_dev_query){
1267 .type = DRM_PANTHOR_DEV_QUERY_TIMESTAMP_INFO,
1268 .size = sizeof(timestamp_info),
1269 .pointer = (uint64_t)(uintptr_t)&timestamp_info,
1270 };
1271
1272 int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANTHOR_DEV_QUERY, &query);
1273 if (ret) {
1274 mesa_loge("DRM_IOCTL_PANTHOR_DEV_QUERY failed (err=%d)", errno);
1275 return 0;
1276 }
1277
1278 return timestamp_info.current_timestamp;
1279 }
1280
1281 static void
1282 panthor_kmod_bo_label(struct pan_kmod_dev *dev, struct pan_kmod_bo *bo, const char *label)
1283 {
1284 char truncated_label[PANTHOR_BO_LABEL_MAXLEN];
1285
1286 if (!pan_kmod_driver_version_at_least(&dev->driver, 1, 4))
1287 return;
1288
1289 if (strnlen(label, PANTHOR_BO_LABEL_MAXLEN) == PANTHOR_BO_LABEL_MAXLEN) {
1290 strncpy(truncated_label, label, PANTHOR_BO_LABEL_MAXLEN - 1);
1291 truncated_label[PANTHOR_BO_LABEL_MAXLEN - 1] = '\0';
1292 label = truncated_label;
1293 }
1294
1295 struct drm_panthor_bo_set_label set_label = (struct drm_panthor_bo_set_label) {
1296 .handle = bo->handle,
1297 .label = (uint64_t)(uintptr_t)label,
1298 };
1299
1300 int ret = pan_kmod_ioctl(dev->fd, DRM_IOCTL_PANTHOR_BO_SET_LABEL, &set_label);
1301 if (ret)
1302 mesa_loge("DRM_IOCTL_PANTHOR_BO_SET_LABEL failed (err=%d)", errno);
1303 }
1304
1305 const struct pan_kmod_ops panthor_kmod_ops = {
1306 .dev_create = panthor_kmod_dev_create,
1307 .dev_destroy = panthor_kmod_dev_destroy,
1308 .dev_query_user_va_range = panthor_kmod_dev_query_user_va_range,
1309 .bo_alloc = panthor_kmod_bo_alloc,
1310 .bo_free = panthor_kmod_bo_free,
1311 .bo_import = panthor_kmod_bo_import,
1312 .bo_export = panthor_kmod_bo_export,
1313 .bo_get_mmap_offset = panthor_kmod_bo_get_mmap_offset,
1314 .bo_wait = panthor_kmod_bo_wait,
1315 .flush_bo_map_syncs = panthor_kmod_flush_bo_map_syncs,
1316 .vm_create = panthor_kmod_vm_create,
1317 .vm_destroy = panthor_kmod_vm_destroy,
1318 .vm_bind = panthor_kmod_vm_bind,
1319 .vm_query_state = panthor_kmod_vm_query_state,
1320 .query_timestamp = panthor_kmod_query_timestamp,
1321 .bo_set_label = panthor_kmod_bo_label,
1322 };