Debian Codegraph @

src/panfrost/vulkan/panvk_utrace.c

raw
1 /*
2 * Copyright 2024 Google LLC
3 * Copyright 2025 Arm Ltd.
4 * SPDX-License-Identifier: MIT
5 */
6
7 #include "panvk_utrace.h"
8
9 #include "kmod/pan_kmod.h"
10 #include "util/log.h"
11 #include "util/timespec.h"
12 #include "panvk_device.h"
13 #include "panvk_physical_device.h"
14 #include "panvk_priv_bo.h"
15 #include "vk_sync.h"
16
17 static struct panvk_device *
18 to_dev(struct u_trace_context *utctx)
19 {
20 return container_of(utctx, struct panvk_device, utrace.utctx);
21 }
22
23 void *
24 panvk_utrace_create_buffer(struct u_trace_context *utctx, uint64_t size_B)
25 {
26 struct panvk_device *dev = to_dev(utctx);
27
28 /* This memory is also used to write CSF commands, therefore we align to a
29 * cache line. */
30 const uint64_t alignment = 0x40;
31
32 simple_mtx_lock(&dev->utrace.copy_buf_heap_lock);
33 const uint64_t addr_dev =
34 util_vma_heap_alloc(&dev->utrace.copy_buf_heap, size_B, alignment);
35 simple_mtx_unlock(&dev->utrace.copy_buf_heap_lock);
36
37 if (!addr_dev) {
38 mesa_loge("Couldn't allocate utrace buffer (size = 0x%" PRIx64 ")."
39 "Provide larger PANVK_UTRACE_CLONE_MEM_SIZE (current = 0x%" PRIx64 ")",
40 size_B, dev->utrace.copy_buf_heap_bo->bo->size);
41 return NULL;
42 }
43
44 struct panvk_utrace_buf *container = malloc(sizeof(struct panvk_utrace_buf));
45 void *addr_host = dev->utrace.copy_buf_heap_bo->addr.host + addr_dev -
46 dev->utrace.copy_buf_heap_bo->addr.dev;
47
48 *container = (struct panvk_utrace_buf){
49 .host = addr_host,
50 .dev = addr_dev,
51 .size = size_B,
52 };
53
54 memset(addr_host, 0, size_B);
55
56 return container;
57 }
58
59 void
60 panvk_utrace_delete_buffer(struct u_trace_context *utctx, void *buffer)
61 {
62 struct panvk_device *dev = to_dev(utctx);
63 struct panvk_utrace_buf *buf = buffer;
64
65 simple_mtx_lock(&dev->utrace.copy_buf_heap_lock);
66 util_vma_heap_free(&dev->utrace.copy_buf_heap, buf->dev, buf->size);
67 simple_mtx_unlock(&dev->utrace.copy_buf_heap_lock);
68
69 free(buffer);
70 }
71
72 uint64_t
73 panvk_utrace_read_ts(struct u_trace_context *utctx, void *timestamps,
74 uint64_t offset_B, uint32_t flags, void *flush_data)
75 {
76 struct panvk_device *dev = to_dev(utctx);
77 const struct panvk_physical_device *pdev =
78 to_panvk_physical_device(dev->vk.physical);
79 const struct pan_kmod_dev_props *props = &pdev->kmod.dev->props;
80 const struct panvk_utrace_buf *buf = timestamps;
81 struct panvk_utrace_flush_data *data = flush_data;
82
83 assert(props->timestamp_frequency);
84
85 /* wait for the submit */
86 if (data->sync) {
87 if (vk_sync_wait(&dev->vk, data->sync, data->wait_value,
88 VK_SYNC_WAIT_COMPLETE, UINT64_MAX) != VK_SUCCESS)
89 mesa_logw("failed to wait for utrace timestamps");
90
91 data->sync = NULL;
92 data->wait_value = 0;
93 }
94
95 const uint64_t *ts_ptr = buf->host + offset_B;
96 uint64_t ts = *ts_ptr;
97 if (ts != U_TRACE_NO_TIMESTAMP)
98 ts = (ts * NSEC_PER_SEC) / props->timestamp_frequency;
99
100 return ts;
101 }
102
103 const void *
104 panvk_utrace_get_data(struct u_trace_context *utctx, void *buffer,
105 uint64_t offset_B, uint32_t size_B)
106 {
107 const struct panvk_utrace_buf *buf = buffer;
108 return buf->host + offset_B;
109 }
110
111 void
112 panvk_utrace_delete_flush_data(struct u_trace_context *utctx, void *flush_data)
113 {
114 struct panvk_utrace_flush_data *data = flush_data;
115
116 if (data->clone_cs_root) {
117 panvk_utrace_delete_buffer(utctx, data->clone_cs_root);
118 data->clone_cs_root = NULL;
119 }
120
121 if (data->free_self)
122 free(data);
123 }