1bf215546Sopenharmony_ci/* 2bf215546Sopenharmony_ci * Copyright © 2018 Intel Corporation 3bf215546Sopenharmony_ci * 4bf215546Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a 5bf215546Sopenharmony_ci * copy of this software and associated documentation files (the "Software"), 6bf215546Sopenharmony_ci * to deal in the Software without restriction, including without limitation 7bf215546Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense, 8bf215546Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the 9bf215546Sopenharmony_ci * Software is furnished to do so, subject to the following conditions: 10bf215546Sopenharmony_ci * 11bf215546Sopenharmony_ci * The above copyright notice and this permission notice shall be included 12bf215546Sopenharmony_ci * in all copies or substantial portions of the Software. 13bf215546Sopenharmony_ci * 14bf215546Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 15bf215546Sopenharmony_ci * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 16bf215546Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 17bf215546Sopenharmony_ci * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 18bf215546Sopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 19bf215546Sopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 20bf215546Sopenharmony_ci * DEALINGS IN THE SOFTWARE. 21bf215546Sopenharmony_ci */ 22bf215546Sopenharmony_ci 23bf215546Sopenharmony_ci/** 24bf215546Sopenharmony_ci * @file crocus_fence.c 25bf215546Sopenharmony_ci * 26bf215546Sopenharmony_ci * Fences for driver and IPC serialisation, scheduling and synchronisation. 27bf215546Sopenharmony_ci */ 28bf215546Sopenharmony_ci 29bf215546Sopenharmony_ci#include "util/u_inlines.h" 30bf215546Sopenharmony_ci#include "intel/common/intel_gem.h" 31bf215546Sopenharmony_ci 32bf215546Sopenharmony_ci#include "crocus_batch.h" 33bf215546Sopenharmony_ci#include "crocus_bufmgr.h" 34bf215546Sopenharmony_ci#include "crocus_context.h" 35bf215546Sopenharmony_ci#include "crocus_fence.h" 36bf215546Sopenharmony_ci#include "crocus_screen.h" 37bf215546Sopenharmony_ci 38bf215546Sopenharmony_cistatic uint32_t 39bf215546Sopenharmony_cigem_syncobj_create(int fd, uint32_t flags) 40bf215546Sopenharmony_ci{ 41bf215546Sopenharmony_ci struct drm_syncobj_create args = { 42bf215546Sopenharmony_ci .flags = flags, 43bf215546Sopenharmony_ci }; 44bf215546Sopenharmony_ci 45bf215546Sopenharmony_ci intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args); 46bf215546Sopenharmony_ci 47bf215546Sopenharmony_ci return args.handle; 48bf215546Sopenharmony_ci} 49bf215546Sopenharmony_ci 50bf215546Sopenharmony_cistatic void 51bf215546Sopenharmony_cigem_syncobj_destroy(int fd, uint32_t handle) 52bf215546Sopenharmony_ci{ 53bf215546Sopenharmony_ci struct drm_syncobj_destroy args = { 54bf215546Sopenharmony_ci .handle = handle, 55bf215546Sopenharmony_ci }; 56bf215546Sopenharmony_ci 57bf215546Sopenharmony_ci intel_ioctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args); 58bf215546Sopenharmony_ci} 59bf215546Sopenharmony_ci 60bf215546Sopenharmony_ci/** 61bf215546Sopenharmony_ci * Make a new sync-point. 62bf215546Sopenharmony_ci */ 63bf215546Sopenharmony_cistruct crocus_syncobj * 64bf215546Sopenharmony_cicrocus_create_syncobj(struct crocus_screen *screen) 65bf215546Sopenharmony_ci{ 66bf215546Sopenharmony_ci struct crocus_syncobj *syncobj = malloc(sizeof(*syncobj)); 67bf215546Sopenharmony_ci 68bf215546Sopenharmony_ci if (!syncobj) 69bf215546Sopenharmony_ci return NULL; 70bf215546Sopenharmony_ci 71bf215546Sopenharmony_ci syncobj->handle = gem_syncobj_create(screen->fd, 0); 72bf215546Sopenharmony_ci assert(syncobj->handle); 73bf215546Sopenharmony_ci 74bf215546Sopenharmony_ci pipe_reference_init(&syncobj->ref, 1); 75bf215546Sopenharmony_ci 76bf215546Sopenharmony_ci return syncobj; 77bf215546Sopenharmony_ci} 78bf215546Sopenharmony_ci 79bf215546Sopenharmony_civoid 80bf215546Sopenharmony_cicrocus_syncobj_destroy(struct crocus_screen *screen, 81bf215546Sopenharmony_ci struct crocus_syncobj *syncobj) 82bf215546Sopenharmony_ci{ 83bf215546Sopenharmony_ci gem_syncobj_destroy(screen->fd, syncobj->handle); 84bf215546Sopenharmony_ci free(syncobj); 85bf215546Sopenharmony_ci} 86bf215546Sopenharmony_ci 87bf215546Sopenharmony_ci/** 88bf215546Sopenharmony_ci * Add a sync-point to the batch, with the given flags. 89bf215546Sopenharmony_ci * 90bf215546Sopenharmony_ci * \p flags One of I915_EXEC_FENCE_WAIT or I915_EXEC_FENCE_SIGNAL. 91bf215546Sopenharmony_ci */ 92bf215546Sopenharmony_civoid 93bf215546Sopenharmony_cicrocus_batch_add_syncobj(struct crocus_batch *batch, 94bf215546Sopenharmony_ci struct crocus_syncobj *syncobj, unsigned flags) 95bf215546Sopenharmony_ci{ 96bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence *fence = 97bf215546Sopenharmony_ci util_dynarray_grow(&batch->exec_fences, struct drm_i915_gem_exec_fence, 1); 98bf215546Sopenharmony_ci 99bf215546Sopenharmony_ci *fence = (struct drm_i915_gem_exec_fence){ 100bf215546Sopenharmony_ci .handle = syncobj->handle, 101bf215546Sopenharmony_ci .flags = flags, 102bf215546Sopenharmony_ci }; 103bf215546Sopenharmony_ci 104bf215546Sopenharmony_ci struct crocus_syncobj **store = 105bf215546Sopenharmony_ci util_dynarray_grow(&batch->syncobjs, struct crocus_syncobj *, 1); 106bf215546Sopenharmony_ci 107bf215546Sopenharmony_ci *store = NULL; 108bf215546Sopenharmony_ci crocus_syncobj_reference(batch->screen, store, syncobj); 109bf215546Sopenharmony_ci} 110bf215546Sopenharmony_ci 111bf215546Sopenharmony_ci/** 112bf215546Sopenharmony_ci * Walk through a batch's dependencies (any I915_EXEC_FENCE_WAIT syncobjs) 113bf215546Sopenharmony_ci * and unreference any which have already passed. 114bf215546Sopenharmony_ci * 115bf215546Sopenharmony_ci * Sometimes the compute batch is seldom used, and accumulates references 116bf215546Sopenharmony_ci * to stale render batches that are no longer of interest, so we can free 117bf215546Sopenharmony_ci * those up. 118bf215546Sopenharmony_ci */ 119bf215546Sopenharmony_cistatic void 120bf215546Sopenharmony_ciclear_stale_syncobjs(struct crocus_batch *batch) 121bf215546Sopenharmony_ci{ 122bf215546Sopenharmony_ci struct crocus_screen *screen = batch->screen; 123bf215546Sopenharmony_ci 124bf215546Sopenharmony_ci int n = util_dynarray_num_elements(&batch->syncobjs, struct crocus_syncobj *); 125bf215546Sopenharmony_ci 126bf215546Sopenharmony_ci assert(n == util_dynarray_num_elements(&batch->exec_fences, 127bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence)); 128bf215546Sopenharmony_ci 129bf215546Sopenharmony_ci /* Skip the first syncobj, as it's the signalling one. */ 130bf215546Sopenharmony_ci for (int i = n - 1; i > 1; i--) { 131bf215546Sopenharmony_ci struct crocus_syncobj **syncobj = 132bf215546Sopenharmony_ci util_dynarray_element(&batch->syncobjs, struct crocus_syncobj *, i); 133bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence *fence = 134bf215546Sopenharmony_ci util_dynarray_element(&batch->exec_fences, 135bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence, i); 136bf215546Sopenharmony_ci assert(fence->flags & I915_EXEC_FENCE_WAIT); 137bf215546Sopenharmony_ci 138bf215546Sopenharmony_ci if (crocus_wait_syncobj(&screen->base, *syncobj, 0)) 139bf215546Sopenharmony_ci continue; 140bf215546Sopenharmony_ci 141bf215546Sopenharmony_ci /* This sync object has already passed, there's no need to continue 142bf215546Sopenharmony_ci * marking it as a dependency; we can stop holding on to the reference. 143bf215546Sopenharmony_ci */ 144bf215546Sopenharmony_ci crocus_syncobj_reference(screen, syncobj, NULL); 145bf215546Sopenharmony_ci 146bf215546Sopenharmony_ci /* Remove it from the lists; move the last element here. */ 147bf215546Sopenharmony_ci struct crocus_syncobj **nth_syncobj = 148bf215546Sopenharmony_ci util_dynarray_pop_ptr(&batch->syncobjs, struct crocus_syncobj *); 149bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence *nth_fence = 150bf215546Sopenharmony_ci util_dynarray_pop_ptr(&batch->exec_fences, 151bf215546Sopenharmony_ci struct drm_i915_gem_exec_fence); 152bf215546Sopenharmony_ci 153bf215546Sopenharmony_ci if (syncobj != nth_syncobj) { 154bf215546Sopenharmony_ci *syncobj = *nth_syncobj; 155bf215546Sopenharmony_ci memcpy(fence, nth_fence, sizeof(*fence)); 156bf215546Sopenharmony_ci } 157bf215546Sopenharmony_ci } 158bf215546Sopenharmony_ci} 159bf215546Sopenharmony_ci 160bf215546Sopenharmony_ci/* ------------------------------------------------------------------- */ 161bf215546Sopenharmony_ci 162bf215546Sopenharmony_cistruct pipe_fence_handle { 163bf215546Sopenharmony_ci struct pipe_reference ref; 164bf215546Sopenharmony_ci 165bf215546Sopenharmony_ci struct pipe_context *unflushed_ctx; 166bf215546Sopenharmony_ci 167bf215546Sopenharmony_ci struct crocus_fine_fence *fine[CROCUS_BATCH_COUNT]; 168bf215546Sopenharmony_ci}; 169bf215546Sopenharmony_ci 170bf215546Sopenharmony_cistatic void 171bf215546Sopenharmony_cicrocus_fence_destroy(struct pipe_screen *p_screen, 172bf215546Sopenharmony_ci struct pipe_fence_handle *fence) 173bf215546Sopenharmony_ci{ 174bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)p_screen; 175bf215546Sopenharmony_ci 176bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) 177bf215546Sopenharmony_ci crocus_fine_fence_reference(screen, &fence->fine[i], NULL); 178bf215546Sopenharmony_ci 179bf215546Sopenharmony_ci free(fence); 180bf215546Sopenharmony_ci} 181bf215546Sopenharmony_ci 182bf215546Sopenharmony_cistatic void 183bf215546Sopenharmony_cicrocus_fence_reference(struct pipe_screen *p_screen, 184bf215546Sopenharmony_ci struct pipe_fence_handle **dst, 185bf215546Sopenharmony_ci struct pipe_fence_handle *src) 186bf215546Sopenharmony_ci{ 187bf215546Sopenharmony_ci if (pipe_reference(&(*dst)->ref, &src->ref)) 188bf215546Sopenharmony_ci crocus_fence_destroy(p_screen, *dst); 189bf215546Sopenharmony_ci 190bf215546Sopenharmony_ci *dst = src; 191bf215546Sopenharmony_ci} 192bf215546Sopenharmony_ci 193bf215546Sopenharmony_cibool 194bf215546Sopenharmony_cicrocus_wait_syncobj(struct pipe_screen *p_screen, 195bf215546Sopenharmony_ci struct crocus_syncobj *syncobj, int64_t timeout_nsec) 196bf215546Sopenharmony_ci{ 197bf215546Sopenharmony_ci if (!syncobj) 198bf215546Sopenharmony_ci return false; 199bf215546Sopenharmony_ci 200bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)p_screen; 201bf215546Sopenharmony_ci struct drm_syncobj_wait args = { 202bf215546Sopenharmony_ci .handles = (uintptr_t)&syncobj->handle, 203bf215546Sopenharmony_ci .count_handles = 1, 204bf215546Sopenharmony_ci .timeout_nsec = timeout_nsec, 205bf215546Sopenharmony_ci }; 206bf215546Sopenharmony_ci return intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args); 207bf215546Sopenharmony_ci} 208bf215546Sopenharmony_ci 209bf215546Sopenharmony_cistatic void 210bf215546Sopenharmony_cicrocus_fence_flush(struct pipe_context *ctx, 211bf215546Sopenharmony_ci struct pipe_fence_handle **out_fence, unsigned flags) 212bf215546Sopenharmony_ci{ 213bf215546Sopenharmony_ci struct crocus_screen *screen = (void *)ctx->screen; 214bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 215bf215546Sopenharmony_ci 216bf215546Sopenharmony_ci const bool deferred = flags & PIPE_FLUSH_DEFERRED; 217bf215546Sopenharmony_ci 218bf215546Sopenharmony_ci if (!deferred) { 219bf215546Sopenharmony_ci for (unsigned i = 0; i < ice->batch_count; i++) 220bf215546Sopenharmony_ci crocus_batch_flush(&ice->batches[i]); 221bf215546Sopenharmony_ci } 222bf215546Sopenharmony_ci 223bf215546Sopenharmony_ci if (!out_fence) 224bf215546Sopenharmony_ci return; 225bf215546Sopenharmony_ci 226bf215546Sopenharmony_ci struct pipe_fence_handle *fence = calloc(1, sizeof(*fence)); 227bf215546Sopenharmony_ci if (!fence) 228bf215546Sopenharmony_ci return; 229bf215546Sopenharmony_ci 230bf215546Sopenharmony_ci pipe_reference_init(&fence->ref, 1); 231bf215546Sopenharmony_ci 232bf215546Sopenharmony_ci if (deferred) 233bf215546Sopenharmony_ci fence->unflushed_ctx = ctx; 234bf215546Sopenharmony_ci 235bf215546Sopenharmony_ci for (unsigned b = 0; b < ice->batch_count; b++) { 236bf215546Sopenharmony_ci struct crocus_batch *batch = &ice->batches[b]; 237bf215546Sopenharmony_ci 238bf215546Sopenharmony_ci if (deferred && crocus_batch_bytes_used(batch) > 0) { 239bf215546Sopenharmony_ci struct crocus_fine_fence *fine = 240bf215546Sopenharmony_ci crocus_fine_fence_new(batch, CROCUS_FENCE_BOTTOM_OF_PIPE); 241bf215546Sopenharmony_ci crocus_fine_fence_reference(screen, &fence->fine[b], fine); 242bf215546Sopenharmony_ci crocus_fine_fence_reference(screen, &fine, NULL); 243bf215546Sopenharmony_ci } else { 244bf215546Sopenharmony_ci /* This batch has no commands queued up (perhaps we just flushed, 245bf215546Sopenharmony_ci * or all the commands are on the other batch). Wait for the last 246bf215546Sopenharmony_ci * syncobj on this engine - unless it's already finished by now. 247bf215546Sopenharmony_ci */ 248bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(batch->last_fence)) 249bf215546Sopenharmony_ci continue; 250bf215546Sopenharmony_ci 251bf215546Sopenharmony_ci crocus_fine_fence_reference(screen, &fence->fine[b], 252bf215546Sopenharmony_ci batch->last_fence); 253bf215546Sopenharmony_ci } 254bf215546Sopenharmony_ci } 255bf215546Sopenharmony_ci 256bf215546Sopenharmony_ci crocus_fence_reference(ctx->screen, out_fence, NULL); 257bf215546Sopenharmony_ci *out_fence = fence; 258bf215546Sopenharmony_ci} 259bf215546Sopenharmony_ci 260bf215546Sopenharmony_cistatic void 261bf215546Sopenharmony_cicrocus_fence_await(struct pipe_context *ctx, struct pipe_fence_handle *fence) 262bf215546Sopenharmony_ci{ 263bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 264bf215546Sopenharmony_ci 265bf215546Sopenharmony_ci /* Unflushed fences from the same context are no-ops. */ 266bf215546Sopenharmony_ci if (ctx && ctx == fence->unflushed_ctx) 267bf215546Sopenharmony_ci return; 268bf215546Sopenharmony_ci 269bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) { 270bf215546Sopenharmony_ci struct crocus_fine_fence *fine = fence->fine[i]; 271bf215546Sopenharmony_ci 272bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(fine)) 273bf215546Sopenharmony_ci continue; 274bf215546Sopenharmony_ci 275bf215546Sopenharmony_ci for (unsigned b = 0; b < ice->batch_count; b++) { 276bf215546Sopenharmony_ci struct crocus_batch *batch = &ice->batches[b]; 277bf215546Sopenharmony_ci 278bf215546Sopenharmony_ci /* We're going to make any future work in this batch wait for our 279bf215546Sopenharmony_ci * fence to have gone by. But any currently queued work doesn't 280bf215546Sopenharmony_ci * need to wait. Flush the batch now, so it can happen sooner. 281bf215546Sopenharmony_ci */ 282bf215546Sopenharmony_ci crocus_batch_flush(batch); 283bf215546Sopenharmony_ci 284bf215546Sopenharmony_ci /* Before adding a new reference, clean out any stale ones. */ 285bf215546Sopenharmony_ci clear_stale_syncobjs(batch); 286bf215546Sopenharmony_ci 287bf215546Sopenharmony_ci crocus_batch_add_syncobj(batch, fine->syncobj, I915_EXEC_FENCE_WAIT); 288bf215546Sopenharmony_ci } 289bf215546Sopenharmony_ci } 290bf215546Sopenharmony_ci} 291bf215546Sopenharmony_ci 292bf215546Sopenharmony_ci#define NSEC_PER_SEC (1000 * USEC_PER_SEC) 293bf215546Sopenharmony_ci#define USEC_PER_SEC (1000 * MSEC_PER_SEC) 294bf215546Sopenharmony_ci#define MSEC_PER_SEC (1000) 295bf215546Sopenharmony_ci 296bf215546Sopenharmony_cistatic uint64_t 297bf215546Sopenharmony_cigettime_ns(void) 298bf215546Sopenharmony_ci{ 299bf215546Sopenharmony_ci struct timespec current; 300bf215546Sopenharmony_ci clock_gettime(CLOCK_MONOTONIC, ¤t); 301bf215546Sopenharmony_ci return (uint64_t)current.tv_sec * NSEC_PER_SEC + current.tv_nsec; 302bf215546Sopenharmony_ci} 303bf215546Sopenharmony_ci 304bf215546Sopenharmony_cistatic uint64_t 305bf215546Sopenharmony_cirel2abs(uint64_t timeout) 306bf215546Sopenharmony_ci{ 307bf215546Sopenharmony_ci if (timeout == 0) 308bf215546Sopenharmony_ci return 0; 309bf215546Sopenharmony_ci 310bf215546Sopenharmony_ci uint64_t current_time = gettime_ns(); 311bf215546Sopenharmony_ci uint64_t max_timeout = (uint64_t)INT64_MAX - current_time; 312bf215546Sopenharmony_ci 313bf215546Sopenharmony_ci timeout = MIN2(max_timeout, timeout); 314bf215546Sopenharmony_ci 315bf215546Sopenharmony_ci return current_time + timeout; 316bf215546Sopenharmony_ci} 317bf215546Sopenharmony_ci 318bf215546Sopenharmony_cistatic bool 319bf215546Sopenharmony_cicrocus_fence_finish(struct pipe_screen *p_screen, struct pipe_context *ctx, 320bf215546Sopenharmony_ci struct pipe_fence_handle *fence, uint64_t timeout) 321bf215546Sopenharmony_ci{ 322bf215546Sopenharmony_ci ctx = threaded_context_unwrap_sync(ctx); 323bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 324bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)p_screen; 325bf215546Sopenharmony_ci 326bf215546Sopenharmony_ci /* If we created the fence with PIPE_FLUSH_DEFERRED, we may not have 327bf215546Sopenharmony_ci * flushed yet. Check if our syncobj is the current batch's signalling 328bf215546Sopenharmony_ci * syncobj - if so, we haven't flushed and need to now. 329bf215546Sopenharmony_ci * 330bf215546Sopenharmony_ci * The Gallium docs mention that a flush will occur if \p ctx matches 331bf215546Sopenharmony_ci * the context the fence was created with. It may be NULL, so we check 332bf215546Sopenharmony_ci * that it matches first. 333bf215546Sopenharmony_ci */ 334bf215546Sopenharmony_ci if (ctx && ctx == fence->unflushed_ctx) { 335bf215546Sopenharmony_ci for (unsigned i = 0; i < ice->batch_count; i++) { 336bf215546Sopenharmony_ci struct crocus_fine_fence *fine = fence->fine[i]; 337bf215546Sopenharmony_ci 338bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(fine)) 339bf215546Sopenharmony_ci continue; 340bf215546Sopenharmony_ci 341bf215546Sopenharmony_ci if (fine->syncobj == crocus_batch_get_signal_syncobj(&ice->batches[i])) 342bf215546Sopenharmony_ci crocus_batch_flush(&ice->batches[i]); 343bf215546Sopenharmony_ci } 344bf215546Sopenharmony_ci 345bf215546Sopenharmony_ci /* The fence is no longer deferred. */ 346bf215546Sopenharmony_ci fence->unflushed_ctx = NULL; 347bf215546Sopenharmony_ci } 348bf215546Sopenharmony_ci 349bf215546Sopenharmony_ci unsigned int handle_count = 0; 350bf215546Sopenharmony_ci uint32_t handles[ARRAY_SIZE(fence->fine)]; 351bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) { 352bf215546Sopenharmony_ci struct crocus_fine_fence *fine = fence->fine[i]; 353bf215546Sopenharmony_ci 354bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(fine)) 355bf215546Sopenharmony_ci continue; 356bf215546Sopenharmony_ci 357bf215546Sopenharmony_ci handles[handle_count++] = fine->syncobj->handle; 358bf215546Sopenharmony_ci } 359bf215546Sopenharmony_ci 360bf215546Sopenharmony_ci if (handle_count == 0) 361bf215546Sopenharmony_ci return true; 362bf215546Sopenharmony_ci 363bf215546Sopenharmony_ci struct drm_syncobj_wait args = { 364bf215546Sopenharmony_ci .handles = (uintptr_t)handles, 365bf215546Sopenharmony_ci .count_handles = handle_count, 366bf215546Sopenharmony_ci .timeout_nsec = rel2abs(timeout), 367bf215546Sopenharmony_ci .flags = DRM_SYNCOBJ_WAIT_FLAGS_WAIT_ALL 368bf215546Sopenharmony_ci }; 369bf215546Sopenharmony_ci if (fence->unflushed_ctx) { 370bf215546Sopenharmony_ci /* This fence had a deferred flush from another context. We can't 371bf215546Sopenharmony_ci * safely flush it here, because the context might be bound to a 372bf215546Sopenharmony_ci * different thread, and poking at its internals wouldn't be safe. 373bf215546Sopenharmony_ci * 374bf215546Sopenharmony_ci * Instead, use the WAIT_FOR_SUBMIT flag to block and hope that 375bf215546Sopenharmony_ci * another thread submits the work. 376bf215546Sopenharmony_ci */ 377bf215546Sopenharmony_ci args.flags |= DRM_SYNCOBJ_WAIT_FLAGS_WAIT_FOR_SUBMIT; 378bf215546Sopenharmony_ci } 379bf215546Sopenharmony_ci return intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_WAIT, &args) == 0; 380bf215546Sopenharmony_ci} 381bf215546Sopenharmony_ci 382bf215546Sopenharmony_ci#ifndef SYNC_IOC_MAGIC 383bf215546Sopenharmony_ci/* duplicated from linux/sync_file.h to avoid build-time dependency 384bf215546Sopenharmony_ci * on new (v4.7) kernel headers. Once distro's are mostly using 385bf215546Sopenharmony_ci * something newer than v4.7 drop this and #include <linux/sync_file.h> 386bf215546Sopenharmony_ci * instead. 387bf215546Sopenharmony_ci */ 388bf215546Sopenharmony_cistruct sync_merge_data { 389bf215546Sopenharmony_ci char name[32]; 390bf215546Sopenharmony_ci __s32 fd2; 391bf215546Sopenharmony_ci __s32 fence; 392bf215546Sopenharmony_ci __u32 flags; 393bf215546Sopenharmony_ci __u32 pad; 394bf215546Sopenharmony_ci}; 395bf215546Sopenharmony_ci 396bf215546Sopenharmony_ci#define SYNC_IOC_MAGIC '>' 397bf215546Sopenharmony_ci#define SYNC_IOC_MERGE _IOWR(SYNC_IOC_MAGIC, 3, struct sync_merge_data) 398bf215546Sopenharmony_ci#endif 399bf215546Sopenharmony_ci 400bf215546Sopenharmony_cistatic int 401bf215546Sopenharmony_cisync_merge_fd(int sync_fd, int new_fd) 402bf215546Sopenharmony_ci{ 403bf215546Sopenharmony_ci if (sync_fd == -1) 404bf215546Sopenharmony_ci return new_fd; 405bf215546Sopenharmony_ci 406bf215546Sopenharmony_ci if (new_fd == -1) 407bf215546Sopenharmony_ci return sync_fd; 408bf215546Sopenharmony_ci 409bf215546Sopenharmony_ci struct sync_merge_data args = { 410bf215546Sopenharmony_ci .name = "crocus fence", 411bf215546Sopenharmony_ci .fd2 = new_fd, 412bf215546Sopenharmony_ci .fence = -1, 413bf215546Sopenharmony_ci }; 414bf215546Sopenharmony_ci 415bf215546Sopenharmony_ci intel_ioctl(sync_fd, SYNC_IOC_MERGE, &args); 416bf215546Sopenharmony_ci close(new_fd); 417bf215546Sopenharmony_ci close(sync_fd); 418bf215546Sopenharmony_ci 419bf215546Sopenharmony_ci return args.fence; 420bf215546Sopenharmony_ci} 421bf215546Sopenharmony_ci 422bf215546Sopenharmony_cistatic int 423bf215546Sopenharmony_cicrocus_fence_get_fd(struct pipe_screen *p_screen, 424bf215546Sopenharmony_ci struct pipe_fence_handle *fence) 425bf215546Sopenharmony_ci{ 426bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)p_screen; 427bf215546Sopenharmony_ci int fd = -1; 428bf215546Sopenharmony_ci 429bf215546Sopenharmony_ci /* Deferred fences aren't supported. */ 430bf215546Sopenharmony_ci if (fence->unflushed_ctx) 431bf215546Sopenharmony_ci return -1; 432bf215546Sopenharmony_ci 433bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) { 434bf215546Sopenharmony_ci struct crocus_fine_fence *fine = fence->fine[i]; 435bf215546Sopenharmony_ci 436bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(fine)) 437bf215546Sopenharmony_ci continue; 438bf215546Sopenharmony_ci 439bf215546Sopenharmony_ci struct drm_syncobj_handle args = { 440bf215546Sopenharmony_ci .handle = fine->syncobj->handle, 441bf215546Sopenharmony_ci .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE, 442bf215546Sopenharmony_ci .fd = -1, 443bf215546Sopenharmony_ci }; 444bf215546Sopenharmony_ci 445bf215546Sopenharmony_ci intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args); 446bf215546Sopenharmony_ci fd = sync_merge_fd(fd, args.fd); 447bf215546Sopenharmony_ci } 448bf215546Sopenharmony_ci 449bf215546Sopenharmony_ci if (fd == -1) { 450bf215546Sopenharmony_ci /* Our fence has no syncobj's recorded. This means that all of the 451bf215546Sopenharmony_ci * batches had already completed, their syncobj's had been signalled, 452bf215546Sopenharmony_ci * and so we didn't bother to record them. But we're being asked to 453bf215546Sopenharmony_ci * export such a fence. So export a dummy already-signalled syncobj. 454bf215546Sopenharmony_ci */ 455bf215546Sopenharmony_ci struct drm_syncobj_handle args = { 456bf215546Sopenharmony_ci .flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE, 457bf215546Sopenharmony_ci .fd = -1, 458bf215546Sopenharmony_ci }; 459bf215546Sopenharmony_ci 460bf215546Sopenharmony_ci args.handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED); 461bf215546Sopenharmony_ci intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args); 462bf215546Sopenharmony_ci gem_syncobj_destroy(screen->fd, args.handle); 463bf215546Sopenharmony_ci return args.fd; 464bf215546Sopenharmony_ci } 465bf215546Sopenharmony_ci 466bf215546Sopenharmony_ci return fd; 467bf215546Sopenharmony_ci} 468bf215546Sopenharmony_ci 469bf215546Sopenharmony_cistatic void 470bf215546Sopenharmony_cicrocus_fence_create_fd(struct pipe_context *ctx, struct pipe_fence_handle **out, 471bf215546Sopenharmony_ci int fd, enum pipe_fd_type type) 472bf215546Sopenharmony_ci{ 473bf215546Sopenharmony_ci assert(type == PIPE_FD_TYPE_NATIVE_SYNC || type == PIPE_FD_TYPE_SYNCOBJ); 474bf215546Sopenharmony_ci 475bf215546Sopenharmony_ci struct crocus_screen *screen = (struct crocus_screen *)ctx->screen; 476bf215546Sopenharmony_ci struct drm_syncobj_handle args = { 477bf215546Sopenharmony_ci .fd = fd, 478bf215546Sopenharmony_ci }; 479bf215546Sopenharmony_ci 480bf215546Sopenharmony_ci if (type == PIPE_FD_TYPE_NATIVE_SYNC) { 481bf215546Sopenharmony_ci args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE; 482bf215546Sopenharmony_ci args.handle = gem_syncobj_create(screen->fd, DRM_SYNCOBJ_CREATE_SIGNALED); 483bf215546Sopenharmony_ci } 484bf215546Sopenharmony_ci 485bf215546Sopenharmony_ci if (intel_ioctl(screen->fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args) == -1) { 486bf215546Sopenharmony_ci fprintf(stderr, "DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE failed: %s\n", 487bf215546Sopenharmony_ci strerror(errno)); 488bf215546Sopenharmony_ci if (type == PIPE_FD_TYPE_NATIVE_SYNC) 489bf215546Sopenharmony_ci gem_syncobj_destroy(screen->fd, args.handle); 490bf215546Sopenharmony_ci *out = NULL; 491bf215546Sopenharmony_ci return; 492bf215546Sopenharmony_ci } 493bf215546Sopenharmony_ci 494bf215546Sopenharmony_ci struct crocus_syncobj *syncobj = malloc(sizeof(*syncobj)); 495bf215546Sopenharmony_ci if (!syncobj) { 496bf215546Sopenharmony_ci *out = NULL; 497bf215546Sopenharmony_ci return; 498bf215546Sopenharmony_ci } 499bf215546Sopenharmony_ci syncobj->handle = args.handle; 500bf215546Sopenharmony_ci pipe_reference_init(&syncobj->ref, 1); 501bf215546Sopenharmony_ci 502bf215546Sopenharmony_ci struct crocus_fine_fence *fine = calloc(1, sizeof(*fine)); 503bf215546Sopenharmony_ci if (!fine) { 504bf215546Sopenharmony_ci free(syncobj); 505bf215546Sopenharmony_ci *out = NULL; 506bf215546Sopenharmony_ci return; 507bf215546Sopenharmony_ci } 508bf215546Sopenharmony_ci 509bf215546Sopenharmony_ci static const uint32_t zero = 0; 510bf215546Sopenharmony_ci 511bf215546Sopenharmony_ci /* Fences work in terms of crocus_fine_fence, but we don't actually have a 512bf215546Sopenharmony_ci * seqno for an imported fence. So, create a fake one which always 513bf215546Sopenharmony_ci * returns as 'not signaled' so we fall back to using the sync object. 514bf215546Sopenharmony_ci */ 515bf215546Sopenharmony_ci fine->seqno = UINT32_MAX; 516bf215546Sopenharmony_ci fine->map = &zero; 517bf215546Sopenharmony_ci fine->syncobj = syncobj; 518bf215546Sopenharmony_ci fine->flags = CROCUS_FENCE_END; 519bf215546Sopenharmony_ci pipe_reference_init(&fine->reference, 1); 520bf215546Sopenharmony_ci 521bf215546Sopenharmony_ci struct pipe_fence_handle *fence = calloc(1, sizeof(*fence)); 522bf215546Sopenharmony_ci if (!fence) { 523bf215546Sopenharmony_ci free(fine); 524bf215546Sopenharmony_ci free(syncobj); 525bf215546Sopenharmony_ci *out = NULL; 526bf215546Sopenharmony_ci return; 527bf215546Sopenharmony_ci } 528bf215546Sopenharmony_ci pipe_reference_init(&fence->ref, 1); 529bf215546Sopenharmony_ci fence->fine[0] = fine; 530bf215546Sopenharmony_ci 531bf215546Sopenharmony_ci *out = fence; 532bf215546Sopenharmony_ci} 533bf215546Sopenharmony_ci 534bf215546Sopenharmony_cistatic void 535bf215546Sopenharmony_cicrocus_fence_signal(struct pipe_context *ctx, struct pipe_fence_handle *fence) 536bf215546Sopenharmony_ci{ 537bf215546Sopenharmony_ci struct crocus_context *ice = (struct crocus_context *)ctx; 538bf215546Sopenharmony_ci 539bf215546Sopenharmony_ci if (ctx == fence->unflushed_ctx) 540bf215546Sopenharmony_ci return; 541bf215546Sopenharmony_ci 542bf215546Sopenharmony_ci for (unsigned b = 0; b < ice->batch_count; b++) { 543bf215546Sopenharmony_ci for (unsigned i = 0; i < ARRAY_SIZE(fence->fine); i++) { 544bf215546Sopenharmony_ci struct crocus_fine_fence *fine = fence->fine[i]; 545bf215546Sopenharmony_ci 546bf215546Sopenharmony_ci /* already signaled fence skipped */ 547bf215546Sopenharmony_ci if (crocus_fine_fence_signaled(fine)) 548bf215546Sopenharmony_ci continue; 549bf215546Sopenharmony_ci 550bf215546Sopenharmony_ci ice->batches[b].contains_fence_signal = true; 551bf215546Sopenharmony_ci crocus_batch_add_syncobj(&ice->batches[b], fine->syncobj, 552bf215546Sopenharmony_ci I915_EXEC_FENCE_SIGNAL); 553bf215546Sopenharmony_ci } 554bf215546Sopenharmony_ci if (ice->batches[b].contains_fence_signal) 555bf215546Sopenharmony_ci crocus_batch_flush(&ice->batches[b]); 556bf215546Sopenharmony_ci } 557bf215546Sopenharmony_ci} 558bf215546Sopenharmony_ci 559bf215546Sopenharmony_civoid 560bf215546Sopenharmony_cicrocus_init_screen_fence_functions(struct pipe_screen *screen) 561bf215546Sopenharmony_ci{ 562bf215546Sopenharmony_ci screen->fence_reference = crocus_fence_reference; 563bf215546Sopenharmony_ci screen->fence_finish = crocus_fence_finish; 564bf215546Sopenharmony_ci screen->fence_get_fd = crocus_fence_get_fd; 565bf215546Sopenharmony_ci} 566bf215546Sopenharmony_ci 567bf215546Sopenharmony_civoid 568bf215546Sopenharmony_cicrocus_init_context_fence_functions(struct pipe_context *ctx) 569bf215546Sopenharmony_ci{ 570bf215546Sopenharmony_ci ctx->flush = crocus_fence_flush; 571bf215546Sopenharmony_ci ctx->create_fence_fd = crocus_fence_create_fd; 572bf215546Sopenharmony_ci ctx->fence_server_sync = crocus_fence_await; 573bf215546Sopenharmony_ci ctx->fence_server_signal = crocus_fence_signal; 574bf215546Sopenharmony_ci} 575