1 /*
2 * Copyright © 2021 Intel Corporation
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
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 */
23
24 #include "anv_private.h"
25
26 #include "perf/intel_perf.h"
27
28 static uint32_t
command_buffers_count_utraces(struct anv_device * device,uint32_t cmd_buffer_count,struct anv_cmd_buffer ** cmd_buffers,uint32_t * utrace_copies)29 command_buffers_count_utraces(struct anv_device *device,
30 uint32_t cmd_buffer_count,
31 struct anv_cmd_buffer **cmd_buffers,
32 uint32_t *utrace_copies)
33 {
34 if (!u_trace_should_process(&device->ds.trace_context))
35 return 0;
36
37 uint32_t utraces = 0;
38 for (uint32_t i = 0; i < cmd_buffer_count; i++) {
39 if (u_trace_has_points(&cmd_buffers[i]->trace)) {
40 utraces++;
41 if (!(cmd_buffers[i]->usage_flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT))
42 *utrace_copies += list_length(&cmd_buffers[i]->trace.trace_chunks);
43 }
44 }
45
46 return utraces;
47 }
48
49 static void
anv_utrace_delete_flush_data(struct u_trace_context * utctx,void * flush_data)50 anv_utrace_delete_flush_data(struct u_trace_context *utctx,
51 void *flush_data)
52 {
53 struct anv_device *device =
54 container_of(utctx, struct anv_device, ds.trace_context);
55 struct anv_utrace_flush_copy *flush = flush_data;
56
57 intel_ds_flush_data_fini(&flush->ds);
58
59 if (flush->trace_bo) {
60 assert(flush->batch_bo);
61 anv_reloc_list_finish(&flush->relocs, &device->vk.alloc);
62 anv_device_release_bo(device, flush->batch_bo);
63 anv_device_release_bo(device, flush->trace_bo);
64 }
65
66 vk_sync_destroy(&device->vk, flush->sync);
67
68 vk_free(&device->vk.alloc, flush);
69 }
70
71 static void
anv_device_utrace_emit_copy_ts_buffer(struct u_trace_context * utctx,void * cmdstream,void * ts_from,uint32_t from_offset,void * ts_to,uint32_t to_offset,uint32_t count)72 anv_device_utrace_emit_copy_ts_buffer(struct u_trace_context *utctx,
73 void *cmdstream,
74 void *ts_from, uint32_t from_offset,
75 void *ts_to, uint32_t to_offset,
76 uint32_t count)
77 {
78 struct anv_device *device =
79 container_of(utctx, struct anv_device, ds.trace_context);
80 struct anv_utrace_flush_copy *flush = cmdstream;
81 struct anv_address from_addr = (struct anv_address) {
82 .bo = ts_from, .offset = from_offset * sizeof(uint64_t) };
83 struct anv_address to_addr = (struct anv_address) {
84 .bo = ts_to, .offset = to_offset * sizeof(uint64_t) };
85
86 anv_genX(device->info, emit_so_memcpy)(&flush->memcpy_state,
87 to_addr, from_addr, count * sizeof(uint64_t));
88 }
89
90 VkResult
anv_device_utrace_flush_cmd_buffers(struct anv_queue * queue,uint32_t cmd_buffer_count,struct anv_cmd_buffer ** cmd_buffers,struct anv_utrace_flush_copy ** out_flush_data)91 anv_device_utrace_flush_cmd_buffers(struct anv_queue *queue,
92 uint32_t cmd_buffer_count,
93 struct anv_cmd_buffer **cmd_buffers,
94 struct anv_utrace_flush_copy **out_flush_data)
95 {
96 struct anv_device *device = queue->device;
97 uint32_t utrace_copies = 0;
98 uint32_t utraces = command_buffers_count_utraces(device,
99 cmd_buffer_count,
100 cmd_buffers,
101 &utrace_copies);
102 if (!utraces) {
103 *out_flush_data = NULL;
104 return VK_SUCCESS;
105 }
106
107 VkResult result;
108 struct anv_utrace_flush_copy *flush =
109 vk_zalloc(&device->vk.alloc, sizeof(struct anv_utrace_flush_copy),
110 8, VK_SYSTEM_ALLOCATION_SCOPE_DEVICE);
111 if (!flush)
112 return vk_error(device, VK_ERROR_OUT_OF_HOST_MEMORY);
113
114 intel_ds_flush_data_init(&flush->ds, &queue->ds, queue->ds.submission_id);
115
116 result = vk_sync_create(&device->vk, &device->physical->sync_syncobj_type,
117 0, 0, &flush->sync);
118 if (result != VK_SUCCESS)
119 goto error_sync;
120
121 if (utrace_copies > 0) {
122 result = anv_bo_pool_alloc(&device->utrace_bo_pool,
123 utrace_copies * 4096,
124 &flush->trace_bo);
125 if (result != VK_SUCCESS)
126 goto error_trace_buf;
127
128 result = anv_bo_pool_alloc(&device->utrace_bo_pool,
129 /* 128 dwords of setup + 64 dwords per copy */
130 align(512 + 64 * utrace_copies, 4096),
131 &flush->batch_bo);
132 if (result != VK_SUCCESS)
133 goto error_batch_buf;
134
135 result = anv_reloc_list_init(&flush->relocs, &device->vk.alloc);
136 if (result != VK_SUCCESS)
137 goto error_reloc_list;
138
139 flush->batch.alloc = &device->vk.alloc;
140 flush->batch.relocs = &flush->relocs;
141 anv_batch_set_storage(&flush->batch,
142 (struct anv_address) { .bo = flush->batch_bo, },
143 flush->batch_bo->map, flush->batch_bo->size);
144
145 /* Emit the copies */
146 anv_genX(device->info, emit_so_memcpy_init)(&flush->memcpy_state,
147 device,
148 &flush->batch);
149 for (uint32_t i = 0; i < cmd_buffer_count; i++) {
150 if (cmd_buffers[i]->usage_flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT) {
151 intel_ds_queue_flush_data(&queue->ds, &cmd_buffers[i]->trace,
152 &flush->ds, false);
153 } else {
154 u_trace_clone_append(u_trace_begin_iterator(&cmd_buffers[i]->trace),
155 u_trace_end_iterator(&cmd_buffers[i]->trace),
156 &flush->ds.trace,
157 flush,
158 anv_device_utrace_emit_copy_ts_buffer);
159 }
160 }
161 anv_genX(device->info, emit_so_memcpy_fini)(&flush->memcpy_state);
162
163 intel_ds_queue_flush_data(&queue->ds, &flush->ds.trace, &flush->ds, true);
164
165 if (flush->batch.status != VK_SUCCESS) {
166 result = flush->batch.status;
167 goto error_batch;
168 }
169 } else {
170 for (uint32_t i = 0; i < cmd_buffer_count; i++) {
171 assert(cmd_buffers[i]->usage_flags & VK_COMMAND_BUFFER_USAGE_ONE_TIME_SUBMIT_BIT);
172 intel_ds_queue_flush_data(&queue->ds, &cmd_buffers[i]->trace,
173 &flush->ds, i == (cmd_buffer_count - 1));
174 }
175 }
176
177 flush->queue = queue;
178
179 *out_flush_data = flush;
180
181 return VK_SUCCESS;
182
183 error_batch:
184 anv_reloc_list_finish(&flush->relocs, &device->vk.alloc);
185 error_reloc_list:
186 anv_bo_pool_free(&device->utrace_bo_pool, flush->batch_bo);
187 error_batch_buf:
188 anv_bo_pool_free(&device->utrace_bo_pool, flush->trace_bo);
189 error_trace_buf:
190 vk_sync_destroy(&device->vk, flush->sync);
191 error_sync:
192 vk_free(&device->vk.alloc, flush);
193 return result;
194 }
195
196 static void *
anv_utrace_create_ts_buffer(struct u_trace_context * utctx,uint32_t size_b)197 anv_utrace_create_ts_buffer(struct u_trace_context *utctx, uint32_t size_b)
198 {
199 struct anv_device *device =
200 container_of(utctx, struct anv_device, ds.trace_context);
201
202 struct anv_bo *bo = NULL;
203 UNUSED VkResult result =
204 anv_bo_pool_alloc(&device->utrace_bo_pool,
205 align(size_b, 4096),
206 &bo);
207 assert(result == VK_SUCCESS);
208
209 return bo;
210 }
211
212 static void
anv_utrace_destroy_ts_buffer(struct u_trace_context * utctx,void * timestamps)213 anv_utrace_destroy_ts_buffer(struct u_trace_context *utctx, void *timestamps)
214 {
215 struct anv_device *device =
216 container_of(utctx, struct anv_device, ds.trace_context);
217 struct anv_bo *bo = timestamps;
218
219 anv_bo_pool_free(&device->utrace_bo_pool, bo);
220 }
221
222 static void
anv_utrace_record_ts(struct u_trace * ut,void * cs,void * timestamps,unsigned idx,bool end_of_pipe)223 anv_utrace_record_ts(struct u_trace *ut, void *cs,
224 void *timestamps, unsigned idx,
225 bool end_of_pipe)
226 {
227 struct anv_cmd_buffer *cmd_buffer =
228 container_of(ut, struct anv_cmd_buffer, trace);
229 struct anv_device *device = cmd_buffer->device;
230 struct anv_bo *bo = timestamps;
231
232 enum anv_timestamp_capture_type capture_type =
233 (end_of_pipe) ? ANV_TIMESTAMP_CAPTURE_END_OF_PIPE
234 : ANV_TIMESTAMP_CAPTURE_TOP_OF_PIPE;
235 device->physical->cmd_emit_timestamp(&cmd_buffer->batch, device,
236 (struct anv_address) {
237 .bo = bo,
238 .offset = idx * sizeof(uint64_t) },
239 capture_type);
240 }
241
242 static uint64_t
anv_utrace_read_ts(struct u_trace_context * utctx,void * timestamps,unsigned idx,void * flush_data)243 anv_utrace_read_ts(struct u_trace_context *utctx,
244 void *timestamps, unsigned idx, void *flush_data)
245 {
246 struct anv_device *device =
247 container_of(utctx, struct anv_device, ds.trace_context);
248 struct anv_bo *bo = timestamps;
249 struct anv_utrace_flush_copy *flush = flush_data;
250
251 /* Only need to stall on results for the first entry: */
252 if (idx == 0) {
253 UNUSED VkResult result =
254 vk_sync_wait(&device->vk,
255 flush->sync,
256 0,
257 VK_SYNC_WAIT_COMPLETE,
258 os_time_get_absolute_timeout(OS_TIMEOUT_INFINITE));
259 assert(result == VK_SUCCESS);
260 }
261
262 uint64_t *ts = bo->map;
263
264 /* Don't translate the no-timestamp marker: */
265 if (ts[idx] == U_TRACE_NO_TIMESTAMP)
266 return U_TRACE_NO_TIMESTAMP;
267
268 return intel_device_info_timebase_scale(device->info, ts[idx]);
269 }
270
271 void
anv_device_utrace_init(struct anv_device * device)272 anv_device_utrace_init(struct anv_device *device)
273 {
274 anv_bo_pool_init(&device->utrace_bo_pool, device, "utrace");
275 intel_ds_device_init(&device->ds, device->info, device->fd,
276 device->physical->local_minor,
277 INTEL_DS_API_VULKAN);
278 u_trace_context_init(&device->ds.trace_context,
279 &device->ds,
280 anv_utrace_create_ts_buffer,
281 anv_utrace_destroy_ts_buffer,
282 anv_utrace_record_ts,
283 anv_utrace_read_ts,
284 anv_utrace_delete_flush_data);
285
286 for (uint32_t q = 0; q < device->queue_count; q++) {
287 struct anv_queue *queue = &device->queues[q];
288
289 intel_ds_device_init_queue(&device->ds, &queue->ds, "%s%u",
290 intel_engines_class_to_string(queue->family->engine_class),
291 queue->vk.index_in_family);
292 }
293 }
294
295 void
anv_device_utrace_finish(struct anv_device * device)296 anv_device_utrace_finish(struct anv_device *device)
297 {
298 intel_ds_device_process(&device->ds, true);
299 intel_ds_device_fini(&device->ds);
300 anv_bo_pool_finish(&device->utrace_bo_pool);
301 }
302
303 enum intel_ds_stall_flag
anv_pipe_flush_bit_to_ds_stall_flag(enum anv_pipe_bits bits)304 anv_pipe_flush_bit_to_ds_stall_flag(enum anv_pipe_bits bits)
305 {
306 static const struct {
307 enum anv_pipe_bits anv;
308 enum intel_ds_stall_flag ds;
309 } anv_to_ds_flags[] = {
310 { .anv = ANV_PIPE_DEPTH_CACHE_FLUSH_BIT, .ds = INTEL_DS_DEPTH_CACHE_FLUSH_BIT, },
311 { .anv = ANV_PIPE_DATA_CACHE_FLUSH_BIT, .ds = INTEL_DS_DATA_CACHE_FLUSH_BIT, },
312 { .anv = ANV_PIPE_TILE_CACHE_FLUSH_BIT, .ds = INTEL_DS_TILE_CACHE_FLUSH_BIT, },
313 { .anv = ANV_PIPE_RENDER_TARGET_CACHE_FLUSH_BIT, .ds = INTEL_DS_RENDER_TARGET_CACHE_FLUSH_BIT, },
314 { .anv = ANV_PIPE_STATE_CACHE_INVALIDATE_BIT, .ds = INTEL_DS_STATE_CACHE_INVALIDATE_BIT, },
315 { .anv = ANV_PIPE_CONSTANT_CACHE_INVALIDATE_BIT, .ds = INTEL_DS_CONST_CACHE_INVALIDATE_BIT, },
316 { .anv = ANV_PIPE_VF_CACHE_INVALIDATE_BIT, .ds = INTEL_DS_VF_CACHE_INVALIDATE_BIT, },
317 { .anv = ANV_PIPE_TEXTURE_CACHE_INVALIDATE_BIT, .ds = INTEL_DS_TEXTURE_CACHE_INVALIDATE_BIT, },
318 { .anv = ANV_PIPE_INSTRUCTION_CACHE_INVALIDATE_BIT, .ds = INTEL_DS_INST_CACHE_INVALIDATE_BIT, },
319 { .anv = ANV_PIPE_DEPTH_STALL_BIT, .ds = INTEL_DS_DEPTH_STALL_BIT, },
320 { .anv = ANV_PIPE_CS_STALL_BIT, .ds = INTEL_DS_CS_STALL_BIT, },
321 { .anv = ANV_PIPE_HDC_PIPELINE_FLUSH_BIT, .ds = INTEL_DS_HDC_PIPELINE_FLUSH_BIT, },
322 { .anv = ANV_PIPE_STALL_AT_SCOREBOARD_BIT, .ds = INTEL_DS_STALL_AT_SCOREBOARD_BIT, },
323 { .anv = ANV_PIPE_UNTYPED_DATAPORT_CACHE_FLUSH_BIT, .ds = INTEL_DS_UNTYPED_DATAPORT_CACHE_FLUSH_BIT, },
324 };
325
326 enum intel_ds_stall_flag ret = 0;
327 for (uint32_t i = 0; i < ARRAY_SIZE(anv_to_ds_flags); i++) {
328 if (anv_to_ds_flags[i].anv & bits)
329 ret |= anv_to_ds_flags[i].ds;
330 }
331
332 return ret;
333 }
334