1 /* 2 * SPDX-License-Identifier: MIT 3 * 4 * Copyright © 2008,2010 Intel Corporation 5 */ 6 7 #include <linux/intel-iommu.h> 8 #include <linux/dma-resv.h> 9 #include <linux/sync_file.h> 10 #include <linux/uaccess.h> 11 12 #include <drm/drm_syncobj.h> 13 14 #include "display/intel_frontbuffer.h" 15 16 #include "gem/i915_gem_ioctls.h" 17 #include "gt/intel_context.h" 18 #include "gt/intel_gt.h" 19 #include "gt/intel_gt_buffer_pool.h" 20 #include "gt/intel_gt_pm.h" 21 #include "gt/intel_ring.h" 22 23 #include "i915_drv.h" 24 #include "i915_gem_clflush.h" 25 #include "i915_gem_context.h" 26 #include "i915_gem_ioctls.h" 27 #include "i915_sw_fence_work.h" 28 #include "i915_trace.h" 29 #include "i915_user_extensions.h" 30 31 struct eb_vma { 32 struct i915_vma *vma; 33 unsigned int flags; 34 35 /** This vma's place in the execbuf reservation list */ 36 struct drm_i915_gem_exec_object2 *exec; 37 struct list_head bind_link; 38 struct list_head reloc_link; 39 40 struct hlist_node node; 41 u32 handle; 42 }; 43 44 enum { 45 FORCE_CPU_RELOC = 1, 46 FORCE_GTT_RELOC, 47 FORCE_GPU_RELOC, 48 #define DBG_FORCE_RELOC 0 /* choose one of the above! */ 49 }; 50 51 #define __EXEC_OBJECT_HAS_PIN BIT(31) 52 #define __EXEC_OBJECT_HAS_FENCE BIT(30) 53 #define __EXEC_OBJECT_NEEDS_MAP BIT(29) 54 #define __EXEC_OBJECT_NEEDS_BIAS BIT(28) 55 #define __EXEC_OBJECT_INTERNAL_FLAGS (~0u << 28) /* all of the above */ 56 #define __EXEC_OBJECT_RESERVED (__EXEC_OBJECT_HAS_PIN | __EXEC_OBJECT_HAS_FENCE) 57 58 #define __EXEC_HAS_RELOC BIT(31) 59 #define __EXEC_ENGINE_PINNED BIT(30) 60 #define __EXEC_INTERNAL_FLAGS (~0u << 30) 61 #define UPDATE PIN_OFFSET_FIXED 62 63 #define BATCH_OFFSET_BIAS (256*1024) 64 65 #define __I915_EXEC_ILLEGAL_FLAGS \ 66 (__I915_EXEC_UNKNOWN_FLAGS | \ 67 I915_EXEC_CONSTANTS_MASK | \ 68 I915_EXEC_RESOURCE_STREAMER) 69 70 /* Catch emission of unexpected errors for CI! */ 71 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) 72 #undef EINVAL 73 #define EINVAL ({ \ 74 DRM_DEBUG_DRIVER("EINVAL at %s:%d\n", __func__, __LINE__); \ 75 22; \ 76 }) 77 #endif 78 79 /** 80 * DOC: User command execution 81 * 82 * Userspace submits commands to be executed on the GPU as an instruction 83 * stream within a GEM object we call a batchbuffer. This instructions may 84 * refer to other GEM objects containing auxiliary state such as kernels, 85 * samplers, render targets and even secondary batchbuffers. Userspace does 86 * not know where in the GPU memory these objects reside and so before the 87 * batchbuffer is passed to the GPU for execution, those addresses in the 88 * batchbuffer and auxiliary objects are updated. This is known as relocation, 89 * or patching. To try and avoid having to relocate each object on the next 90 * execution, userspace is told the location of those objects in this pass, 91 * but this remains just a hint as the kernel may choose a new location for 92 * any object in the future. 93 * 94 * At the level of talking to the hardware, submitting a batchbuffer for the 95 * GPU to execute is to add content to a buffer from which the HW 96 * command streamer is reading. 97 * 98 * 1. Add a command to load the HW context. For Logical Ring Contexts, i.e. 99 * Execlists, this command is not placed on the same buffer as the 100 * remaining items. 101 * 102 * 2. Add a command to invalidate caches to the buffer. 103 * 104 * 3. Add a batchbuffer start command to the buffer; the start command is 105 * essentially a token together with the GPU address of the batchbuffer 106 * to be executed. 107 * 108 * 4. Add a pipeline flush to the buffer. 109 * 110 * 5. Add a memory write command to the buffer to record when the GPU 111 * is done executing the batchbuffer. The memory write writes the 112 * global sequence number of the request, ``i915_request::global_seqno``; 113 * the i915 driver uses the current value in the register to determine 114 * if the GPU has completed the batchbuffer. 115 * 116 * 6. Add a user interrupt command to the buffer. This command instructs 117 * the GPU to issue an interrupt when the command, pipeline flush and 118 * memory write are completed. 119 * 120 * 7. Inform the hardware of the additional commands added to the buffer 121 * (by updating the tail pointer). 122 * 123 * Processing an execbuf ioctl is conceptually split up into a few phases. 124 * 125 * 1. Validation - Ensure all the pointers, handles and flags are valid. 126 * 2. Reservation - Assign GPU address space for every object 127 * 3. Relocation - Update any addresses to point to the final locations 128 * 4. Serialisation - Order the request with respect to its dependencies 129 * 5. Construction - Construct a request to execute the batchbuffer 130 * 6. Submission (at some point in the future execution) 131 * 132 * Reserving resources for the execbuf is the most complicated phase. We 133 * neither want to have to migrate the object in the address space, nor do 134 * we want to have to update any relocations pointing to this object. Ideally, 135 * we want to leave the object where it is and for all the existing relocations 136 * to match. If the object is given a new address, or if userspace thinks the 137 * object is elsewhere, we have to parse all the relocation entries and update 138 * the addresses. Userspace can set the I915_EXEC_NORELOC flag to hint that 139 * all the target addresses in all of its objects match the value in the 140 * relocation entries and that they all match the presumed offsets given by the 141 * list of execbuffer objects. Using this knowledge, we know that if we haven't 142 * moved any buffers, all the relocation entries are valid and we can skip 143 * the update. (If userspace is wrong, the likely outcome is an impromptu GPU 144 * hang.) The requirement for using I915_EXEC_NO_RELOC are: 145 * 146 * The addresses written in the objects must match the corresponding 147 * reloc.presumed_offset which in turn must match the corresponding 148 * execobject.offset. 149 * 150 * Any render targets written to in the batch must be flagged with 151 * EXEC_OBJECT_WRITE. 152 * 153 * To avoid stalling, execobject.offset should match the current 154 * address of that object within the active context. 155 * 156 * The reservation is done is multiple phases. First we try and keep any 157 * object already bound in its current location - so as long as meets the 158 * constraints imposed by the new execbuffer. Any object left unbound after the 159 * first pass is then fitted into any available idle space. If an object does 160 * not fit, all objects are removed from the reservation and the process rerun 161 * after sorting the objects into a priority order (more difficult to fit 162 * objects are tried first). Failing that, the entire VM is cleared and we try 163 * to fit the execbuf once last time before concluding that it simply will not 164 * fit. 165 * 166 * A small complication to all of this is that we allow userspace not only to 167 * specify an alignment and a size for the object in the address space, but 168 * we also allow userspace to specify the exact offset. This objects are 169 * simpler to place (the location is known a priori) all we have to do is make 170 * sure the space is available. 171 * 172 * Once all the objects are in place, patching up the buried pointers to point 173 * to the final locations is a fairly simple job of walking over the relocation 174 * entry arrays, looking up the right address and rewriting the value into 175 * the object. Simple! ... The relocation entries are stored in user memory 176 * and so to access them we have to copy them into a local buffer. That copy 177 * has to avoid taking any pagefaults as they may lead back to a GEM object 178 * requiring the struct_mutex (i.e. recursive deadlock). So once again we split 179 * the relocation into multiple passes. First we try to do everything within an 180 * atomic context (avoid the pagefaults) which requires that we never wait. If 181 * we detect that we may wait, or if we need to fault, then we have to fallback 182 * to a slower path. The slowpath has to drop the mutex. (Can you hear alarm 183 * bells yet?) Dropping the mutex means that we lose all the state we have 184 * built up so far for the execbuf and we must reset any global data. However, 185 * we do leave the objects pinned in their final locations - which is a 186 * potential issue for concurrent execbufs. Once we have left the mutex, we can 187 * allocate and copy all the relocation entries into a large array at our 188 * leisure, reacquire the mutex, reclaim all the objects and other state and 189 * then proceed to update any incorrect addresses with the objects. 190 * 191 * As we process the relocation entries, we maintain a record of whether the 192 * object is being written to. Using NORELOC, we expect userspace to provide 193 * this information instead. We also check whether we can skip the relocation 194 * by comparing the expected value inside the relocation entry with the target's 195 * final address. If they differ, we have to map the current object and rewrite 196 * the 4 or 8 byte pointer within. 197 * 198 * Serialising an execbuf is quite simple according to the rules of the GEM 199 * ABI. Execution within each context is ordered by the order of submission. 200 * Writes to any GEM object are in order of submission and are exclusive. Reads 201 * from a GEM object are unordered with respect to other reads, but ordered by 202 * writes. A write submitted after a read cannot occur before the read, and 203 * similarly any read submitted after a write cannot occur before the write. 204 * Writes are ordered between engines such that only one write occurs at any 205 * time (completing any reads beforehand) - using semaphores where available 206 * and CPU serialisation otherwise. Other GEM access obey the same rules, any 207 * write (either via mmaps using set-domain, or via pwrite) must flush all GPU 208 * reads before starting, and any read (either using set-domain or pread) must 209 * flush all GPU writes before starting. (Note we only employ a barrier before, 210 * we currently rely on userspace not concurrently starting a new execution 211 * whilst reading or writing to an object. This may be an advantage or not 212 * depending on how much you trust userspace not to shoot themselves in the 213 * foot.) Serialisation may just result in the request being inserted into 214 * a DAG awaiting its turn, but most simple is to wait on the CPU until 215 * all dependencies are resolved. 216 * 217 * After all of that, is just a matter of closing the request and handing it to 218 * the hardware (well, leaving it in a queue to be executed). However, we also 219 * offer the ability for batchbuffers to be run with elevated privileges so 220 * that they access otherwise hidden registers. (Used to adjust L3 cache etc.) 221 * Before any batch is given extra privileges we first must check that it 222 * contains no nefarious instructions, we check that each instruction is from 223 * our whitelist and all registers are also from an allowed list. We first 224 * copy the user's batchbuffer to a shadow (so that the user doesn't have 225 * access to it, either by the CPU or GPU as we scan it) and then parse each 226 * instruction. If everything is ok, we set a flag telling the hardware to run 227 * the batchbuffer in trusted mode, otherwise the ioctl is rejected. 228 */ 229 230 struct eb_fence { 231 struct drm_syncobj *syncobj; /* Use with ptr_mask_bits() */ 232 struct dma_fence *dma_fence; 233 u64 value; 234 struct dma_fence_chain *chain_fence; 235 }; 236 237 struct i915_execbuffer { 238 struct drm_i915_private *i915; /** i915 backpointer */ 239 struct drm_file *file; /** per-file lookup tables and limits */ 240 struct drm_i915_gem_execbuffer2 *args; /** ioctl parameters */ 241 struct drm_i915_gem_exec_object2 *exec; /** ioctl execobj[] */ 242 struct eb_vma *vma; 243 244 struct intel_engine_cs *engine; /** engine to queue the request to */ 245 struct intel_context *context; /* logical state for the request */ 246 struct i915_gem_context *gem_context; /** caller's context */ 247 248 struct i915_request *request; /** our request to build */ 249 struct eb_vma *batch; /** identity of the batch obj/vma */ 250 struct i915_vma *trampoline; /** trampoline used for chaining */ 251 252 /** actual size of execobj[] as we may extend it for the cmdparser */ 253 unsigned int buffer_count; 254 255 /** list of vma not yet bound during reservation phase */ 256 struct list_head unbound; 257 258 /** list of vma that have execobj.relocation_count */ 259 struct list_head relocs; 260 261 struct i915_gem_ww_ctx ww; 262 263 /** 264 * Track the most recently used object for relocations, as we 265 * frequently have to perform multiple relocations within the same 266 * obj/page 267 */ 268 struct reloc_cache { 269 struct drm_mm_node node; /** temporary GTT binding */ 270 unsigned long vaddr; /** Current kmap address */ 271 unsigned long page; /** Currently mapped page index */ 272 unsigned int gen; /** Cached value of INTEL_GEN */ 273 bool use_64bit_reloc : 1; 274 bool has_llc : 1; 275 bool has_fence : 1; 276 bool needs_unfenced : 1; 277 278 struct i915_request *rq; 279 u32 *rq_cmd; 280 unsigned int rq_size; 281 struct intel_gt_buffer_pool_node *pool; 282 } reloc_cache; 283 284 struct intel_gt_buffer_pool_node *reloc_pool; /** relocation pool for -EDEADLK handling */ 285 struct intel_context *reloc_context; 286 287 u64 invalid_flags; /** Set of execobj.flags that are invalid */ 288 u32 context_flags; /** Set of execobj.flags to insert from the ctx */ 289 290 u32 batch_start_offset; /** Location within object of batch */ 291 u32 batch_len; /** Length of batch within object */ 292 u32 batch_flags; /** Flags composed for emit_bb_start() */ 293 struct intel_gt_buffer_pool_node *batch_pool; /** pool node for batch buffer */ 294 295 /** 296 * Indicate either the size of the hastable used to resolve 297 * relocation handles, or if negative that we are using a direct 298 * index into the execobj[]. 299 */ 300 int lut_size; 301 struct hlist_head *buckets; /** ht for relocation handles */ 302 303 struct eb_fence *fences; 304 unsigned long num_fences; 305 }; 306 307 static int eb_parse(struct i915_execbuffer *eb); 308 static struct i915_request *eb_pin_engine(struct i915_execbuffer *eb, 309 bool throttle); 310 static void eb_unpin_engine(struct i915_execbuffer *eb); 311 312 static inline bool eb_use_cmdparser(const struct i915_execbuffer *eb) 313 { 314 return intel_engine_requires_cmd_parser(eb->engine) || 315 (intel_engine_using_cmd_parser(eb->engine) && 316 eb->args->batch_len); 317 } 318 319 static int eb_create(struct i915_execbuffer *eb) 320 { 321 if (!(eb->args->flags & I915_EXEC_HANDLE_LUT)) { 322 unsigned int size = 1 + ilog2(eb->buffer_count); 323 324 /* 325 * Without a 1:1 association between relocation handles and 326 * the execobject[] index, we instead create a hashtable. 327 * We size it dynamically based on available memory, starting 328 * first with 1:1 assocative hash and scaling back until 329 * the allocation succeeds. 330 * 331 * Later on we use a positive lut_size to indicate we are 332 * using this hashtable, and a negative value to indicate a 333 * direct lookup. 334 */ 335 do { 336 gfp_t flags; 337 338 /* While we can still reduce the allocation size, don't 339 * raise a warning and allow the allocation to fail. 340 * On the last pass though, we want to try as hard 341 * as possible to perform the allocation and warn 342 * if it fails. 343 */ 344 flags = GFP_KERNEL; 345 if (size > 1) 346 flags |= __GFP_NORETRY | __GFP_NOWARN; 347 348 eb->buckets = kzalloc(sizeof(struct hlist_head) << size, 349 flags); 350 if (eb->buckets) 351 break; 352 } while (--size); 353 354 if (unlikely(!size)) 355 return -ENOMEM; 356 357 eb->lut_size = size; 358 } else { 359 eb->lut_size = -eb->buffer_count; 360 } 361 362 return 0; 363 } 364 365 static bool 366 eb_vma_misplaced(const struct drm_i915_gem_exec_object2 *entry, 367 const struct i915_vma *vma, 368 unsigned int flags) 369 { 370 if (vma->node.size < entry->pad_to_size) 371 return true; 372 373 if (entry->alignment && !IS_ALIGNED(vma->node.start, entry->alignment)) 374 return true; 375 376 if (flags & EXEC_OBJECT_PINNED && 377 vma->node.start != entry->offset) 378 return true; 379 380 if (flags & __EXEC_OBJECT_NEEDS_BIAS && 381 vma->node.start < BATCH_OFFSET_BIAS) 382 return true; 383 384 if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) && 385 (vma->node.start + vma->node.size - 1) >> 32) 386 return true; 387 388 if (flags & __EXEC_OBJECT_NEEDS_MAP && 389 !i915_vma_is_map_and_fenceable(vma)) 390 return true; 391 392 return false; 393 } 394 395 static u64 eb_pin_flags(const struct drm_i915_gem_exec_object2 *entry, 396 unsigned int exec_flags) 397 { 398 u64 pin_flags = 0; 399 400 if (exec_flags & EXEC_OBJECT_NEEDS_GTT) 401 pin_flags |= PIN_GLOBAL; 402 403 /* 404 * Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset, 405 * limit address to the first 4GBs for unflagged objects. 406 */ 407 if (!(exec_flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) 408 pin_flags |= PIN_ZONE_4G; 409 410 if (exec_flags & __EXEC_OBJECT_NEEDS_MAP) 411 pin_flags |= PIN_MAPPABLE; 412 413 if (exec_flags & EXEC_OBJECT_PINNED) 414 pin_flags |= entry->offset | PIN_OFFSET_FIXED; 415 else if (exec_flags & __EXEC_OBJECT_NEEDS_BIAS) 416 pin_flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS; 417 418 return pin_flags; 419 } 420 421 static inline bool 422 eb_pin_vma(struct i915_execbuffer *eb, 423 const struct drm_i915_gem_exec_object2 *entry, 424 struct eb_vma *ev) 425 { 426 struct i915_vma *vma = ev->vma; 427 u64 pin_flags; 428 429 if (vma->node.size) 430 pin_flags = vma->node.start; 431 else 432 pin_flags = entry->offset & PIN_OFFSET_MASK; 433 434 pin_flags |= PIN_USER | PIN_NOEVICT | PIN_OFFSET_FIXED; 435 if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_GTT)) 436 pin_flags |= PIN_GLOBAL; 437 438 /* Attempt to reuse the current location if available */ 439 /* TODO: Add -EDEADLK handling here */ 440 if (unlikely(i915_vma_pin_ww(vma, &eb->ww, 0, 0, pin_flags))) { 441 if (entry->flags & EXEC_OBJECT_PINNED) 442 return false; 443 444 /* Failing that pick any _free_ space if suitable */ 445 if (unlikely(i915_vma_pin_ww(vma, &eb->ww, 446 entry->pad_to_size, 447 entry->alignment, 448 eb_pin_flags(entry, ev->flags) | 449 PIN_USER | PIN_NOEVICT))) 450 return false; 451 } 452 453 if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) { 454 if (unlikely(i915_vma_pin_fence(vma))) { 455 i915_vma_unpin(vma); 456 return false; 457 } 458 459 if (vma->fence) 460 ev->flags |= __EXEC_OBJECT_HAS_FENCE; 461 } 462 463 ev->flags |= __EXEC_OBJECT_HAS_PIN; 464 return !eb_vma_misplaced(entry, vma, ev->flags); 465 } 466 467 static inline void 468 eb_unreserve_vma(struct eb_vma *ev) 469 { 470 if (!(ev->flags & __EXEC_OBJECT_HAS_PIN)) 471 return; 472 473 if (unlikely(ev->flags & __EXEC_OBJECT_HAS_FENCE)) 474 __i915_vma_unpin_fence(ev->vma); 475 476 __i915_vma_unpin(ev->vma); 477 ev->flags &= ~__EXEC_OBJECT_RESERVED; 478 } 479 480 static int 481 eb_validate_vma(struct i915_execbuffer *eb, 482 struct drm_i915_gem_exec_object2 *entry, 483 struct i915_vma *vma) 484 { 485 if (unlikely(entry->flags & eb->invalid_flags)) 486 return -EINVAL; 487 488 if (unlikely(entry->alignment && 489 !is_power_of_2_u64(entry->alignment))) 490 return -EINVAL; 491 492 /* 493 * Offset can be used as input (EXEC_OBJECT_PINNED), reject 494 * any non-page-aligned or non-canonical addresses. 495 */ 496 if (unlikely(entry->flags & EXEC_OBJECT_PINNED && 497 entry->offset != gen8_canonical_addr(entry->offset & I915_GTT_PAGE_MASK))) 498 return -EINVAL; 499 500 /* pad_to_size was once a reserved field, so sanitize it */ 501 if (entry->flags & EXEC_OBJECT_PAD_TO_SIZE) { 502 if (unlikely(offset_in_page(entry->pad_to_size))) 503 return -EINVAL; 504 } else { 505 entry->pad_to_size = 0; 506 } 507 /* 508 * From drm_mm perspective address space is continuous, 509 * so from this point we're always using non-canonical 510 * form internally. 511 */ 512 entry->offset = gen8_noncanonical_addr(entry->offset); 513 514 if (!eb->reloc_cache.has_fence) { 515 entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE; 516 } else { 517 if ((entry->flags & EXEC_OBJECT_NEEDS_FENCE || 518 eb->reloc_cache.needs_unfenced) && 519 i915_gem_object_is_tiled(vma->obj)) 520 entry->flags |= EXEC_OBJECT_NEEDS_GTT | __EXEC_OBJECT_NEEDS_MAP; 521 } 522 523 if (!(entry->flags & EXEC_OBJECT_PINNED)) 524 entry->flags |= eb->context_flags; 525 526 return 0; 527 } 528 529 static void 530 eb_add_vma(struct i915_execbuffer *eb, 531 unsigned int i, unsigned batch_idx, 532 struct i915_vma *vma) 533 { 534 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; 535 struct eb_vma *ev = &eb->vma[i]; 536 537 GEM_BUG_ON(i915_vma_is_closed(vma)); 538 539 ev->vma = vma; 540 ev->exec = entry; 541 ev->flags = entry->flags; 542 543 if (eb->lut_size > 0) { 544 ev->handle = entry->handle; 545 hlist_add_head(&ev->node, 546 &eb->buckets[hash_32(entry->handle, 547 eb->lut_size)]); 548 } 549 550 if (entry->relocation_count) 551 list_add_tail(&ev->reloc_link, &eb->relocs); 552 553 /* 554 * SNA is doing fancy tricks with compressing batch buffers, which leads 555 * to negative relocation deltas. Usually that works out ok since the 556 * relocate address is still positive, except when the batch is placed 557 * very low in the GTT. Ensure this doesn't happen. 558 * 559 * Note that actual hangs have only been observed on gen7, but for 560 * paranoia do it everywhere. 561 */ 562 if (i == batch_idx) { 563 if (entry->relocation_count && 564 !(ev->flags & EXEC_OBJECT_PINNED)) 565 ev->flags |= __EXEC_OBJECT_NEEDS_BIAS; 566 if (eb->reloc_cache.has_fence) 567 ev->flags |= EXEC_OBJECT_NEEDS_FENCE; 568 569 eb->batch = ev; 570 } 571 } 572 573 static inline int use_cpu_reloc(const struct reloc_cache *cache, 574 const struct drm_i915_gem_object *obj) 575 { 576 if (!i915_gem_object_has_struct_page(obj)) 577 return false; 578 579 if (DBG_FORCE_RELOC == FORCE_CPU_RELOC) 580 return true; 581 582 if (DBG_FORCE_RELOC == FORCE_GTT_RELOC) 583 return false; 584 585 return (cache->has_llc || 586 obj->cache_dirty || 587 obj->cache_level != I915_CACHE_NONE); 588 } 589 590 static int eb_reserve_vma(struct i915_execbuffer *eb, 591 struct eb_vma *ev, 592 u64 pin_flags) 593 { 594 struct drm_i915_gem_exec_object2 *entry = ev->exec; 595 struct i915_vma *vma = ev->vma; 596 int err; 597 598 if (drm_mm_node_allocated(&vma->node) && 599 eb_vma_misplaced(entry, vma, ev->flags)) { 600 err = i915_vma_unbind(vma); 601 if (err) 602 return err; 603 } 604 605 err = i915_vma_pin_ww(vma, &eb->ww, 606 entry->pad_to_size, entry->alignment, 607 eb_pin_flags(entry, ev->flags) | pin_flags); 608 if (err) 609 return err; 610 611 if (entry->offset != vma->node.start) { 612 entry->offset = vma->node.start | UPDATE; 613 eb->args->flags |= __EXEC_HAS_RELOC; 614 } 615 616 if (unlikely(ev->flags & EXEC_OBJECT_NEEDS_FENCE)) { 617 err = i915_vma_pin_fence(vma); 618 if (unlikely(err)) { 619 i915_vma_unpin(vma); 620 return err; 621 } 622 623 if (vma->fence) 624 ev->flags |= __EXEC_OBJECT_HAS_FENCE; 625 } 626 627 ev->flags |= __EXEC_OBJECT_HAS_PIN; 628 GEM_BUG_ON(eb_vma_misplaced(entry, vma, ev->flags)); 629 630 return 0; 631 } 632 633 static int eb_reserve(struct i915_execbuffer *eb) 634 { 635 const unsigned int count = eb->buffer_count; 636 unsigned int pin_flags = PIN_USER | PIN_NONBLOCK; 637 struct list_head last; 638 struct eb_vma *ev; 639 unsigned int i, pass; 640 int err = 0; 641 642 /* 643 * Attempt to pin all of the buffers into the GTT. 644 * This is done in 3 phases: 645 * 646 * 1a. Unbind all objects that do not match the GTT constraints for 647 * the execbuffer (fenceable, mappable, alignment etc). 648 * 1b. Increment pin count for already bound objects. 649 * 2. Bind new objects. 650 * 3. Decrement pin count. 651 * 652 * This avoid unnecessary unbinding of later objects in order to make 653 * room for the earlier objects *unless* we need to defragment. 654 */ 655 pass = 0; 656 do { 657 list_for_each_entry(ev, &eb->unbound, bind_link) { 658 err = eb_reserve_vma(eb, ev, pin_flags); 659 if (err) 660 break; 661 } 662 if (err != -ENOSPC) 663 return err; 664 665 /* Resort *all* the objects into priority order */ 666 INIT_LIST_HEAD(&eb->unbound); 667 INIT_LIST_HEAD(&last); 668 for (i = 0; i < count; i++) { 669 unsigned int flags; 670 671 ev = &eb->vma[i]; 672 flags = ev->flags; 673 if (flags & EXEC_OBJECT_PINNED && 674 flags & __EXEC_OBJECT_HAS_PIN) 675 continue; 676 677 eb_unreserve_vma(ev); 678 679 if (flags & EXEC_OBJECT_PINNED) 680 /* Pinned must have their slot */ 681 list_add(&ev->bind_link, &eb->unbound); 682 else if (flags & __EXEC_OBJECT_NEEDS_MAP) 683 /* Map require the lowest 256MiB (aperture) */ 684 list_add_tail(&ev->bind_link, &eb->unbound); 685 else if (!(flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS)) 686 /* Prioritise 4GiB region for restricted bo */ 687 list_add(&ev->bind_link, &last); 688 else 689 list_add_tail(&ev->bind_link, &last); 690 } 691 list_splice_tail(&last, &eb->unbound); 692 693 switch (pass++) { 694 case 0: 695 break; 696 697 case 1: 698 /* Too fragmented, unbind everything and retry */ 699 mutex_lock(&eb->context->vm->mutex); 700 err = i915_gem_evict_vm(eb->context->vm); 701 mutex_unlock(&eb->context->vm->mutex); 702 if (err) 703 return err; 704 break; 705 706 default: 707 return -ENOSPC; 708 } 709 710 pin_flags = PIN_USER; 711 } while (1); 712 } 713 714 static unsigned int eb_batch_index(const struct i915_execbuffer *eb) 715 { 716 if (eb->args->flags & I915_EXEC_BATCH_FIRST) 717 return 0; 718 else 719 return eb->buffer_count - 1; 720 } 721 722 static int eb_select_context(struct i915_execbuffer *eb) 723 { 724 struct i915_gem_context *ctx; 725 726 ctx = i915_gem_context_lookup(eb->file->driver_priv, eb->args->rsvd1); 727 if (unlikely(!ctx)) 728 return -ENOENT; 729 730 eb->gem_context = ctx; 731 if (rcu_access_pointer(ctx->vm)) 732 eb->invalid_flags |= EXEC_OBJECT_NEEDS_GTT; 733 734 eb->context_flags = 0; 735 if (test_bit(UCONTEXT_NO_ZEROMAP, &ctx->user_flags)) 736 eb->context_flags |= __EXEC_OBJECT_NEEDS_BIAS; 737 738 return 0; 739 } 740 741 static int __eb_add_lut(struct i915_execbuffer *eb, 742 u32 handle, struct i915_vma *vma) 743 { 744 struct i915_gem_context *ctx = eb->gem_context; 745 struct i915_lut_handle *lut; 746 int err; 747 748 lut = i915_lut_handle_alloc(); 749 if (unlikely(!lut)) 750 return -ENOMEM; 751 752 i915_vma_get(vma); 753 if (!atomic_fetch_inc(&vma->open_count)) 754 i915_vma_reopen(vma); 755 lut->handle = handle; 756 lut->ctx = ctx; 757 758 /* Check that the context hasn't been closed in the meantime */ 759 err = -EINTR; 760 if (!mutex_lock_interruptible(&ctx->lut_mutex)) { 761 struct i915_address_space *vm = rcu_access_pointer(ctx->vm); 762 763 if (unlikely(vm && vma->vm != vm)) 764 err = -EAGAIN; /* user racing with ctx set-vm */ 765 else if (likely(!i915_gem_context_is_closed(ctx))) 766 err = radix_tree_insert(&ctx->handles_vma, handle, vma); 767 else 768 err = -ENOENT; 769 if (err == 0) { /* And nor has this handle */ 770 struct drm_i915_gem_object *obj = vma->obj; 771 772 spin_lock(&obj->lut_lock); 773 if (idr_find(&eb->file->object_idr, handle) == obj) { 774 list_add(&lut->obj_link, &obj->lut_list); 775 } else { 776 radix_tree_delete(&ctx->handles_vma, handle); 777 err = -ENOENT; 778 } 779 spin_unlock(&obj->lut_lock); 780 } 781 mutex_unlock(&ctx->lut_mutex); 782 } 783 if (unlikely(err)) 784 goto err; 785 786 return 0; 787 788 err: 789 i915_vma_close(vma); 790 i915_vma_put(vma); 791 i915_lut_handle_free(lut); 792 return err; 793 } 794 795 static struct i915_vma *eb_lookup_vma(struct i915_execbuffer *eb, u32 handle) 796 { 797 struct i915_address_space *vm = eb->context->vm; 798 799 do { 800 struct drm_i915_gem_object *obj; 801 struct i915_vma *vma; 802 int err; 803 804 rcu_read_lock(); 805 vma = radix_tree_lookup(&eb->gem_context->handles_vma, handle); 806 if (likely(vma && vma->vm == vm)) 807 vma = i915_vma_tryget(vma); 808 rcu_read_unlock(); 809 if (likely(vma)) 810 return vma; 811 812 obj = i915_gem_object_lookup(eb->file, handle); 813 if (unlikely(!obj)) 814 return ERR_PTR(-ENOENT); 815 816 vma = i915_vma_instance(obj, vm, NULL); 817 if (IS_ERR(vma)) { 818 i915_gem_object_put(obj); 819 return vma; 820 } 821 822 err = __eb_add_lut(eb, handle, vma); 823 if (likely(!err)) 824 return vma; 825 826 i915_gem_object_put(obj); 827 if (err != -EEXIST) 828 return ERR_PTR(err); 829 } while (1); 830 } 831 832 static int eb_lookup_vmas(struct i915_execbuffer *eb) 833 { 834 struct drm_i915_private *i915 = eb->i915; 835 unsigned int batch = eb_batch_index(eb); 836 unsigned int i; 837 int err = 0; 838 839 INIT_LIST_HEAD(&eb->relocs); 840 841 for (i = 0; i < eb->buffer_count; i++) { 842 struct i915_vma *vma; 843 844 vma = eb_lookup_vma(eb, eb->exec[i].handle); 845 if (IS_ERR(vma)) { 846 err = PTR_ERR(vma); 847 goto err; 848 } 849 850 err = eb_validate_vma(eb, &eb->exec[i], vma); 851 if (unlikely(err)) { 852 i915_vma_put(vma); 853 goto err; 854 } 855 856 eb_add_vma(eb, i, batch, vma); 857 } 858 859 if (unlikely(eb->batch->flags & EXEC_OBJECT_WRITE)) { 860 drm_dbg(&i915->drm, 861 "Attempting to use self-modifying batch buffer\n"); 862 return -EINVAL; 863 } 864 865 if (range_overflows_t(u64, 866 eb->batch_start_offset, eb->batch_len, 867 eb->batch->vma->size)) { 868 drm_dbg(&i915->drm, "Attempting to use out-of-bounds batch\n"); 869 return -EINVAL; 870 } 871 872 if (eb->batch_len == 0) 873 eb->batch_len = eb->batch->vma->size - eb->batch_start_offset; 874 875 return 0; 876 877 err: 878 eb->vma[i].vma = NULL; 879 return err; 880 } 881 882 static int eb_validate_vmas(struct i915_execbuffer *eb) 883 { 884 unsigned int i; 885 int err; 886 887 INIT_LIST_HEAD(&eb->unbound); 888 889 for (i = 0; i < eb->buffer_count; i++) { 890 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; 891 struct eb_vma *ev = &eb->vma[i]; 892 struct i915_vma *vma = ev->vma; 893 894 err = i915_gem_object_lock(vma->obj, &eb->ww); 895 if (err) 896 return err; 897 898 if (eb_pin_vma(eb, entry, ev)) { 899 if (entry->offset != vma->node.start) { 900 entry->offset = vma->node.start | UPDATE; 901 eb->args->flags |= __EXEC_HAS_RELOC; 902 } 903 } else { 904 eb_unreserve_vma(ev); 905 906 list_add_tail(&ev->bind_link, &eb->unbound); 907 if (drm_mm_node_allocated(&vma->node)) { 908 err = i915_vma_unbind(vma); 909 if (err) 910 return err; 911 } 912 } 913 914 GEM_BUG_ON(drm_mm_node_allocated(&vma->node) && 915 eb_vma_misplaced(&eb->exec[i], vma, ev->flags)); 916 } 917 918 if (!list_empty(&eb->unbound)) 919 return eb_reserve(eb); 920 921 return 0; 922 } 923 924 static struct eb_vma * 925 eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) 926 { 927 if (eb->lut_size < 0) { 928 if (handle >= -eb->lut_size) 929 return NULL; 930 return &eb->vma[handle]; 931 } else { 932 struct hlist_head *head; 933 struct eb_vma *ev; 934 935 head = &eb->buckets[hash_32(handle, eb->lut_size)]; 936 hlist_for_each_entry(ev, head, node) { 937 if (ev->handle == handle) 938 return ev; 939 } 940 return NULL; 941 } 942 } 943 944 static void eb_release_vmas(struct i915_execbuffer *eb, bool final) 945 { 946 const unsigned int count = eb->buffer_count; 947 unsigned int i; 948 949 for (i = 0; i < count; i++) { 950 struct eb_vma *ev = &eb->vma[i]; 951 struct i915_vma *vma = ev->vma; 952 953 if (!vma) 954 break; 955 956 eb_unreserve_vma(ev); 957 958 if (final) 959 i915_vma_put(vma); 960 } 961 962 eb_unpin_engine(eb); 963 } 964 965 static void eb_destroy(const struct i915_execbuffer *eb) 966 { 967 GEM_BUG_ON(eb->reloc_cache.rq); 968 969 if (eb->lut_size > 0) 970 kfree(eb->buckets); 971 } 972 973 static inline u64 974 relocation_target(const struct drm_i915_gem_relocation_entry *reloc, 975 const struct i915_vma *target) 976 { 977 return gen8_canonical_addr((int)reloc->delta + target->node.start); 978 } 979 980 static void reloc_cache_clear(struct reloc_cache *cache) 981 { 982 cache->rq = NULL; 983 cache->rq_cmd = NULL; 984 cache->pool = NULL; 985 cache->rq_size = 0; 986 } 987 988 static void reloc_cache_init(struct reloc_cache *cache, 989 struct drm_i915_private *i915) 990 { 991 cache->page = -1; 992 cache->vaddr = 0; 993 /* Must be a variable in the struct to allow GCC to unroll. */ 994 cache->gen = INTEL_GEN(i915); 995 cache->has_llc = HAS_LLC(i915); 996 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915); 997 cache->has_fence = cache->gen < 4; 998 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment; 999 cache->node.flags = 0; 1000 reloc_cache_clear(cache); 1001 } 1002 1003 static inline void *unmask_page(unsigned long p) 1004 { 1005 return (void *)(uintptr_t)(p & PAGE_MASK); 1006 } 1007 1008 static inline unsigned int unmask_flags(unsigned long p) 1009 { 1010 return p & ~PAGE_MASK; 1011 } 1012 1013 #define KMAP 0x4 /* after CLFLUSH_FLAGS */ 1014 1015 static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache) 1016 { 1017 struct drm_i915_private *i915 = 1018 container_of(cache, struct i915_execbuffer, reloc_cache)->i915; 1019 return &i915->ggtt; 1020 } 1021 1022 static void reloc_cache_put_pool(struct i915_execbuffer *eb, struct reloc_cache *cache) 1023 { 1024 if (!cache->pool) 1025 return; 1026 1027 /* 1028 * This is a bit nasty, normally we keep objects locked until the end 1029 * of execbuffer, but we already submit this, and have to unlock before 1030 * dropping the reference. Fortunately we can only hold 1 pool node at 1031 * a time, so this should be harmless. 1032 */ 1033 i915_gem_ww_unlock_single(cache->pool->obj); 1034 intel_gt_buffer_pool_put(cache->pool); 1035 cache->pool = NULL; 1036 } 1037 1038 static void reloc_gpu_flush(struct i915_execbuffer *eb, struct reloc_cache *cache) 1039 { 1040 struct drm_i915_gem_object *obj = cache->rq->batch->obj; 1041 1042 GEM_BUG_ON(cache->rq_size >= obj->base.size / sizeof(u32)); 1043 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END; 1044 1045 __i915_gem_object_flush_map(obj, 0, sizeof(u32) * (cache->rq_size + 1)); 1046 i915_gem_object_unpin_map(obj); 1047 1048 intel_gt_chipset_flush(cache->rq->engine->gt); 1049 1050 i915_request_add(cache->rq); 1051 reloc_cache_put_pool(eb, cache); 1052 reloc_cache_clear(cache); 1053 1054 eb->reloc_pool = NULL; 1055 } 1056 1057 static void reloc_cache_reset(struct reloc_cache *cache, struct i915_execbuffer *eb) 1058 { 1059 void *vaddr; 1060 1061 if (cache->rq) 1062 reloc_gpu_flush(eb, cache); 1063 1064 if (!cache->vaddr) 1065 return; 1066 1067 vaddr = unmask_page(cache->vaddr); 1068 if (cache->vaddr & KMAP) { 1069 struct drm_i915_gem_object *obj = 1070 (struct drm_i915_gem_object *)cache->node.mm; 1071 if (cache->vaddr & CLFLUSH_AFTER) 1072 mb(); 1073 1074 kunmap_atomic(vaddr); 1075 i915_gem_object_finish_access(obj); 1076 } else { 1077 struct i915_ggtt *ggtt = cache_to_ggtt(cache); 1078 1079 intel_gt_flush_ggtt_writes(ggtt->vm.gt); 1080 io_mapping_unmap_atomic((void __iomem *)vaddr); 1081 1082 if (drm_mm_node_allocated(&cache->node)) { 1083 ggtt->vm.clear_range(&ggtt->vm, 1084 cache->node.start, 1085 cache->node.size); 1086 mutex_lock(&ggtt->vm.mutex); 1087 drm_mm_remove_node(&cache->node); 1088 mutex_unlock(&ggtt->vm.mutex); 1089 } else { 1090 i915_vma_unpin((struct i915_vma *)cache->node.mm); 1091 } 1092 } 1093 1094 cache->vaddr = 0; 1095 cache->page = -1; 1096 } 1097 1098 static void *reloc_kmap(struct drm_i915_gem_object *obj, 1099 struct reloc_cache *cache, 1100 unsigned long pageno) 1101 { 1102 void *vaddr; 1103 struct page *page; 1104 1105 if (cache->vaddr) { 1106 kunmap_atomic(unmask_page(cache->vaddr)); 1107 } else { 1108 unsigned int flushes; 1109 int err; 1110 1111 err = i915_gem_object_prepare_write(obj, &flushes); 1112 if (err) 1113 return ERR_PTR(err); 1114 1115 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS); 1116 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK); 1117 1118 cache->vaddr = flushes | KMAP; 1119 cache->node.mm = (void *)obj; 1120 if (flushes) 1121 mb(); 1122 } 1123 1124 page = i915_gem_object_get_page(obj, pageno); 1125 if (!obj->mm.dirty) 1126 set_page_dirty(page); 1127 1128 vaddr = kmap_atomic(page); 1129 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr; 1130 cache->page = pageno; 1131 1132 return vaddr; 1133 } 1134 1135 static void *reloc_iomap(struct drm_i915_gem_object *obj, 1136 struct i915_execbuffer *eb, 1137 unsigned long page) 1138 { 1139 struct reloc_cache *cache = &eb->reloc_cache; 1140 struct i915_ggtt *ggtt = cache_to_ggtt(cache); 1141 unsigned long offset; 1142 void *vaddr; 1143 1144 if (cache->vaddr) { 1145 intel_gt_flush_ggtt_writes(ggtt->vm.gt); 1146 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr)); 1147 } else { 1148 struct i915_vma *vma; 1149 int err; 1150 1151 if (i915_gem_object_is_tiled(obj)) 1152 return ERR_PTR(-EINVAL); 1153 1154 if (use_cpu_reloc(cache, obj)) 1155 return NULL; 1156 1157 err = i915_gem_object_set_to_gtt_domain(obj, true); 1158 if (err) 1159 return ERR_PTR(err); 1160 1161 vma = i915_gem_object_ggtt_pin_ww(obj, &eb->ww, NULL, 0, 0, 1162 PIN_MAPPABLE | 1163 PIN_NONBLOCK /* NOWARN */ | 1164 PIN_NOEVICT); 1165 if (vma == ERR_PTR(-EDEADLK)) 1166 return vma; 1167 1168 if (IS_ERR(vma)) { 1169 memset(&cache->node, 0, sizeof(cache->node)); 1170 mutex_lock(&ggtt->vm.mutex); 1171 err = drm_mm_insert_node_in_range 1172 (&ggtt->vm.mm, &cache->node, 1173 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE, 1174 0, ggtt->mappable_end, 1175 DRM_MM_INSERT_LOW); 1176 mutex_unlock(&ggtt->vm.mutex); 1177 if (err) /* no inactive aperture space, use cpu reloc */ 1178 return NULL; 1179 } else { 1180 cache->node.start = vma->node.start; 1181 cache->node.mm = (void *)vma; 1182 } 1183 } 1184 1185 offset = cache->node.start; 1186 if (drm_mm_node_allocated(&cache->node)) { 1187 ggtt->vm.insert_page(&ggtt->vm, 1188 i915_gem_object_get_dma_address(obj, page), 1189 offset, I915_CACHE_NONE, 0); 1190 } else { 1191 offset += page << PAGE_SHIFT; 1192 } 1193 1194 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap, 1195 offset); 1196 cache->page = page; 1197 cache->vaddr = (unsigned long)vaddr; 1198 1199 return vaddr; 1200 } 1201 1202 static void *reloc_vaddr(struct drm_i915_gem_object *obj, 1203 struct i915_execbuffer *eb, 1204 unsigned long page) 1205 { 1206 struct reloc_cache *cache = &eb->reloc_cache; 1207 void *vaddr; 1208 1209 if (cache->page == page) { 1210 vaddr = unmask_page(cache->vaddr); 1211 } else { 1212 vaddr = NULL; 1213 if ((cache->vaddr & KMAP) == 0) 1214 vaddr = reloc_iomap(obj, eb, page); 1215 if (!vaddr) 1216 vaddr = reloc_kmap(obj, cache, page); 1217 } 1218 1219 return vaddr; 1220 } 1221 1222 static void clflush_write32(u32 *addr, u32 value, unsigned int flushes) 1223 { 1224 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) { 1225 if (flushes & CLFLUSH_BEFORE) { 1226 clflushopt(addr); 1227 mb(); 1228 } 1229 1230 *addr = value; 1231 1232 /* 1233 * Writes to the same cacheline are serialised by the CPU 1234 * (including clflush). On the write path, we only require 1235 * that it hits memory in an orderly fashion and place 1236 * mb barriers at the start and end of the relocation phase 1237 * to ensure ordering of clflush wrt to the system. 1238 */ 1239 if (flushes & CLFLUSH_AFTER) 1240 clflushopt(addr); 1241 } else 1242 *addr = value; 1243 } 1244 1245 static int reloc_move_to_gpu(struct i915_request *rq, struct i915_vma *vma) 1246 { 1247 struct drm_i915_gem_object *obj = vma->obj; 1248 int err; 1249 1250 assert_vma_held(vma); 1251 1252 if (obj->cache_dirty & ~obj->cache_coherent) 1253 i915_gem_clflush_object(obj, 0); 1254 obj->write_domain = 0; 1255 1256 err = i915_request_await_object(rq, vma->obj, true); 1257 if (err == 0) 1258 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE); 1259 1260 return err; 1261 } 1262 1263 static int __reloc_gpu_alloc(struct i915_execbuffer *eb, 1264 struct intel_engine_cs *engine, 1265 struct i915_vma *vma, 1266 unsigned int len) 1267 { 1268 struct reloc_cache *cache = &eb->reloc_cache; 1269 struct intel_gt_buffer_pool_node *pool = eb->reloc_pool; 1270 struct i915_request *rq; 1271 struct i915_vma *batch; 1272 u32 *cmd; 1273 int err; 1274 1275 if (!pool) { 1276 pool = intel_gt_get_buffer_pool(engine->gt, PAGE_SIZE); 1277 if (IS_ERR(pool)) 1278 return PTR_ERR(pool); 1279 } 1280 eb->reloc_pool = NULL; 1281 1282 err = i915_gem_object_lock(pool->obj, &eb->ww); 1283 if (err) 1284 goto err_pool; 1285 1286 cmd = i915_gem_object_pin_map(pool->obj, 1287 cache->has_llc ? 1288 I915_MAP_FORCE_WB : 1289 I915_MAP_FORCE_WC); 1290 if (IS_ERR(cmd)) { 1291 err = PTR_ERR(cmd); 1292 goto err_pool; 1293 } 1294 1295 batch = i915_vma_instance(pool->obj, vma->vm, NULL); 1296 if (IS_ERR(batch)) { 1297 err = PTR_ERR(batch); 1298 goto err_unmap; 1299 } 1300 1301 err = i915_vma_pin_ww(batch, &eb->ww, 0, 0, PIN_USER | PIN_NONBLOCK); 1302 if (err) 1303 goto err_unmap; 1304 1305 if (engine == eb->context->engine) { 1306 rq = i915_request_create(eb->context); 1307 } else { 1308 struct intel_context *ce = eb->reloc_context; 1309 1310 if (!ce) { 1311 ce = intel_context_create(engine); 1312 if (IS_ERR(ce)) { 1313 err = PTR_ERR(ce); 1314 goto err_unpin; 1315 } 1316 1317 i915_vm_put(ce->vm); 1318 ce->vm = i915_vm_get(eb->context->vm); 1319 eb->reloc_context = ce; 1320 } 1321 1322 err = intel_context_pin_ww(ce, &eb->ww); 1323 if (err) 1324 goto err_unpin; 1325 1326 rq = i915_request_create(ce); 1327 intel_context_unpin(ce); 1328 } 1329 if (IS_ERR(rq)) { 1330 err = PTR_ERR(rq); 1331 goto err_unpin; 1332 } 1333 1334 err = intel_gt_buffer_pool_mark_active(pool, rq); 1335 if (err) 1336 goto err_request; 1337 1338 err = reloc_move_to_gpu(rq, vma); 1339 if (err) 1340 goto err_request; 1341 1342 err = eb->engine->emit_bb_start(rq, 1343 batch->node.start, PAGE_SIZE, 1344 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE); 1345 if (err) 1346 goto skip_request; 1347 1348 assert_vma_held(batch); 1349 err = i915_request_await_object(rq, batch->obj, false); 1350 if (err == 0) 1351 err = i915_vma_move_to_active(batch, rq, 0); 1352 if (err) 1353 goto skip_request; 1354 1355 rq->batch = batch; 1356 i915_vma_unpin(batch); 1357 1358 cache->rq = rq; 1359 cache->rq_cmd = cmd; 1360 cache->rq_size = 0; 1361 cache->pool = pool; 1362 1363 /* Return with batch mapping (cmd) still pinned */ 1364 return 0; 1365 1366 skip_request: 1367 i915_request_set_error_once(rq, err); 1368 err_request: 1369 i915_request_add(rq); 1370 err_unpin: 1371 i915_vma_unpin(batch); 1372 err_unmap: 1373 i915_gem_object_unpin_map(pool->obj); 1374 err_pool: 1375 eb->reloc_pool = pool; 1376 return err; 1377 } 1378 1379 static bool reloc_can_use_engine(const struct intel_engine_cs *engine) 1380 { 1381 return engine->class != VIDEO_DECODE_CLASS || !IS_GEN(engine->i915, 6); 1382 } 1383 1384 static u32 *reloc_gpu(struct i915_execbuffer *eb, 1385 struct i915_vma *vma, 1386 unsigned int len) 1387 { 1388 struct reloc_cache *cache = &eb->reloc_cache; 1389 u32 *cmd; 1390 1391 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1)) 1392 reloc_gpu_flush(eb, cache); 1393 1394 if (unlikely(!cache->rq)) { 1395 int err; 1396 struct intel_engine_cs *engine = eb->engine; 1397 1398 if (!reloc_can_use_engine(engine)) { 1399 engine = engine->gt->engine_class[COPY_ENGINE_CLASS][0]; 1400 if (!engine) 1401 return ERR_PTR(-ENODEV); 1402 } 1403 1404 err = __reloc_gpu_alloc(eb, engine, vma, len); 1405 if (unlikely(err)) 1406 return ERR_PTR(err); 1407 } 1408 1409 cmd = cache->rq_cmd + cache->rq_size; 1410 cache->rq_size += len; 1411 1412 return cmd; 1413 } 1414 1415 static inline bool use_reloc_gpu(struct i915_vma *vma) 1416 { 1417 if (DBG_FORCE_RELOC == FORCE_GPU_RELOC) 1418 return true; 1419 1420 if (DBG_FORCE_RELOC) 1421 return false; 1422 1423 return !dma_resv_test_signaled_rcu(vma->resv, true); 1424 } 1425 1426 static unsigned long vma_phys_addr(struct i915_vma *vma, u32 offset) 1427 { 1428 struct page *page; 1429 unsigned long addr; 1430 1431 GEM_BUG_ON(vma->pages != vma->obj->mm.pages); 1432 1433 page = i915_gem_object_get_page(vma->obj, offset >> PAGE_SHIFT); 1434 addr = PFN_PHYS(page_to_pfn(page)); 1435 GEM_BUG_ON(overflows_type(addr, u32)); /* expected dma32 */ 1436 1437 return addr + offset_in_page(offset); 1438 } 1439 1440 static int __reloc_entry_gpu(struct i915_execbuffer *eb, 1441 struct i915_vma *vma, 1442 u64 offset, 1443 u64 target_addr) 1444 { 1445 const unsigned int gen = eb->reloc_cache.gen; 1446 unsigned int len; 1447 u32 *batch; 1448 u64 addr; 1449 1450 if (gen >= 8) 1451 len = offset & 7 ? 8 : 5; 1452 else if (gen >= 4) 1453 len = 4; 1454 else 1455 len = 3; 1456 1457 batch = reloc_gpu(eb, vma, len); 1458 if (batch == ERR_PTR(-EDEADLK)) 1459 return -EDEADLK; 1460 else if (IS_ERR(batch)) 1461 return false; 1462 1463 addr = gen8_canonical_addr(vma->node.start + offset); 1464 if (gen >= 8) { 1465 if (offset & 7) { 1466 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1467 *batch++ = lower_32_bits(addr); 1468 *batch++ = upper_32_bits(addr); 1469 *batch++ = lower_32_bits(target_addr); 1470 1471 addr = gen8_canonical_addr(addr + 4); 1472 1473 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1474 *batch++ = lower_32_bits(addr); 1475 *batch++ = upper_32_bits(addr); 1476 *batch++ = upper_32_bits(target_addr); 1477 } else { 1478 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1; 1479 *batch++ = lower_32_bits(addr); 1480 *batch++ = upper_32_bits(addr); 1481 *batch++ = lower_32_bits(target_addr); 1482 *batch++ = upper_32_bits(target_addr); 1483 } 1484 } else if (gen >= 6) { 1485 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1486 *batch++ = 0; 1487 *batch++ = addr; 1488 *batch++ = target_addr; 1489 } else if (IS_I965G(eb->i915)) { 1490 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1491 *batch++ = 0; 1492 *batch++ = vma_phys_addr(vma, offset); 1493 *batch++ = target_addr; 1494 } else if (gen >= 4) { 1495 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT; 1496 *batch++ = 0; 1497 *batch++ = addr; 1498 *batch++ = target_addr; 1499 } else if (gen >= 3 && 1500 !(IS_I915G(eb->i915) || IS_I915GM(eb->i915))) { 1501 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL; 1502 *batch++ = addr; 1503 *batch++ = target_addr; 1504 } else { 1505 *batch++ = MI_STORE_DWORD_IMM; 1506 *batch++ = vma_phys_addr(vma, offset); 1507 *batch++ = target_addr; 1508 } 1509 1510 return true; 1511 } 1512 1513 static int reloc_entry_gpu(struct i915_execbuffer *eb, 1514 struct i915_vma *vma, 1515 u64 offset, 1516 u64 target_addr) 1517 { 1518 if (eb->reloc_cache.vaddr) 1519 return false; 1520 1521 if (!use_reloc_gpu(vma)) 1522 return false; 1523 1524 return __reloc_entry_gpu(eb, vma, offset, target_addr); 1525 } 1526 1527 static u64 1528 relocate_entry(struct i915_vma *vma, 1529 const struct drm_i915_gem_relocation_entry *reloc, 1530 struct i915_execbuffer *eb, 1531 const struct i915_vma *target) 1532 { 1533 u64 target_addr = relocation_target(reloc, target); 1534 u64 offset = reloc->offset; 1535 int reloc_gpu = reloc_entry_gpu(eb, vma, offset, target_addr); 1536 1537 if (reloc_gpu < 0) 1538 return reloc_gpu; 1539 1540 if (!reloc_gpu) { 1541 bool wide = eb->reloc_cache.use_64bit_reloc; 1542 void *vaddr; 1543 1544 repeat: 1545 vaddr = reloc_vaddr(vma->obj, eb, 1546 offset >> PAGE_SHIFT); 1547 if (IS_ERR(vaddr)) 1548 return PTR_ERR(vaddr); 1549 1550 GEM_BUG_ON(!IS_ALIGNED(offset, sizeof(u32))); 1551 clflush_write32(vaddr + offset_in_page(offset), 1552 lower_32_bits(target_addr), 1553 eb->reloc_cache.vaddr); 1554 1555 if (wide) { 1556 offset += sizeof(u32); 1557 target_addr >>= 32; 1558 wide = false; 1559 goto repeat; 1560 } 1561 } 1562 1563 return target->node.start | UPDATE; 1564 } 1565 1566 static u64 1567 eb_relocate_entry(struct i915_execbuffer *eb, 1568 struct eb_vma *ev, 1569 const struct drm_i915_gem_relocation_entry *reloc) 1570 { 1571 struct drm_i915_private *i915 = eb->i915; 1572 struct eb_vma *target; 1573 int err; 1574 1575 /* we've already hold a reference to all valid objects */ 1576 target = eb_get_vma(eb, reloc->target_handle); 1577 if (unlikely(!target)) 1578 return -ENOENT; 1579 1580 /* Validate that the target is in a valid r/w GPU domain */ 1581 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { 1582 drm_dbg(&i915->drm, "reloc with multiple write domains: " 1583 "target %d offset %d " 1584 "read %08x write %08x", 1585 reloc->target_handle, 1586 (int) reloc->offset, 1587 reloc->read_domains, 1588 reloc->write_domain); 1589 return -EINVAL; 1590 } 1591 if (unlikely((reloc->write_domain | reloc->read_domains) 1592 & ~I915_GEM_GPU_DOMAINS)) { 1593 drm_dbg(&i915->drm, "reloc with read/write non-GPU domains: " 1594 "target %d offset %d " 1595 "read %08x write %08x", 1596 reloc->target_handle, 1597 (int) reloc->offset, 1598 reloc->read_domains, 1599 reloc->write_domain); 1600 return -EINVAL; 1601 } 1602 1603 if (reloc->write_domain) { 1604 target->flags |= EXEC_OBJECT_WRITE; 1605 1606 /* 1607 * Sandybridge PPGTT errata: We need a global gtt mapping 1608 * for MI and pipe_control writes because the gpu doesn't 1609 * properly redirect them through the ppgtt for non_secure 1610 * batchbuffers. 1611 */ 1612 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && 1613 IS_GEN(eb->i915, 6)) { 1614 err = i915_vma_bind(target->vma, 1615 target->vma->obj->cache_level, 1616 PIN_GLOBAL, NULL); 1617 if (err) 1618 return err; 1619 } 1620 } 1621 1622 /* 1623 * If the relocation already has the right value in it, no 1624 * more work needs to be done. 1625 */ 1626 if (!DBG_FORCE_RELOC && 1627 gen8_canonical_addr(target->vma->node.start) == reloc->presumed_offset) 1628 return 0; 1629 1630 /* Check that the relocation address is valid... */ 1631 if (unlikely(reloc->offset > 1632 ev->vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) { 1633 drm_dbg(&i915->drm, "Relocation beyond object bounds: " 1634 "target %d offset %d size %d.\n", 1635 reloc->target_handle, 1636 (int)reloc->offset, 1637 (int)ev->vma->size); 1638 return -EINVAL; 1639 } 1640 if (unlikely(reloc->offset & 3)) { 1641 drm_dbg(&i915->drm, "Relocation not 4-byte aligned: " 1642 "target %d offset %d.\n", 1643 reloc->target_handle, 1644 (int)reloc->offset); 1645 return -EINVAL; 1646 } 1647 1648 /* 1649 * If we write into the object, we need to force the synchronisation 1650 * barrier, either with an asynchronous clflush or if we executed the 1651 * patching using the GPU (though that should be serialised by the 1652 * timeline). To be completely sure, and since we are required to 1653 * do relocations we are already stalling, disable the user's opt 1654 * out of our synchronisation. 1655 */ 1656 ev->flags &= ~EXEC_OBJECT_ASYNC; 1657 1658 /* and update the user's relocation entry */ 1659 return relocate_entry(ev->vma, reloc, eb, target->vma); 1660 } 1661 1662 static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev) 1663 { 1664 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) 1665 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)]; 1666 const struct drm_i915_gem_exec_object2 *entry = ev->exec; 1667 struct drm_i915_gem_relocation_entry __user *urelocs = 1668 u64_to_user_ptr(entry->relocs_ptr); 1669 unsigned long remain = entry->relocation_count; 1670 1671 if (unlikely(remain > N_RELOC(ULONG_MAX))) 1672 return -EINVAL; 1673 1674 /* 1675 * We must check that the entire relocation array is safe 1676 * to read. However, if the array is not writable the user loses 1677 * the updated relocation values. 1678 */ 1679 if (unlikely(!access_ok(urelocs, remain * sizeof(*urelocs)))) 1680 return -EFAULT; 1681 1682 do { 1683 struct drm_i915_gem_relocation_entry *r = stack; 1684 unsigned int count = 1685 min_t(unsigned long, remain, ARRAY_SIZE(stack)); 1686 unsigned int copied; 1687 1688 /* 1689 * This is the fast path and we cannot handle a pagefault 1690 * whilst holding the struct mutex lest the user pass in the 1691 * relocations contained within a mmaped bo. For in such a case 1692 * we, the page fault handler would call i915_gem_fault() and 1693 * we would try to acquire the struct mutex again. Obviously 1694 * this is bad and so lockdep complains vehemently. 1695 */ 1696 pagefault_disable(); 1697 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0])); 1698 pagefault_enable(); 1699 if (unlikely(copied)) { 1700 remain = -EFAULT; 1701 goto out; 1702 } 1703 1704 remain -= count; 1705 do { 1706 u64 offset = eb_relocate_entry(eb, ev, r); 1707 1708 if (likely(offset == 0)) { 1709 } else if ((s64)offset < 0) { 1710 remain = (int)offset; 1711 goto out; 1712 } else { 1713 /* 1714 * Note that reporting an error now 1715 * leaves everything in an inconsistent 1716 * state as we have *already* changed 1717 * the relocation value inside the 1718 * object. As we have not changed the 1719 * reloc.presumed_offset or will not 1720 * change the execobject.offset, on the 1721 * call we may not rewrite the value 1722 * inside the object, leaving it 1723 * dangling and causing a GPU hang. Unless 1724 * userspace dynamically rebuilds the 1725 * relocations on each execbuf rather than 1726 * presume a static tree. 1727 * 1728 * We did previously check if the relocations 1729 * were writable (access_ok), an error now 1730 * would be a strange race with mprotect, 1731 * having already demonstrated that we 1732 * can read from this userspace address. 1733 */ 1734 offset = gen8_canonical_addr(offset & ~UPDATE); 1735 __put_user(offset, 1736 &urelocs[r - stack].presumed_offset); 1737 } 1738 } while (r++, --count); 1739 urelocs += ARRAY_SIZE(stack); 1740 } while (remain); 1741 out: 1742 reloc_cache_reset(&eb->reloc_cache, eb); 1743 return remain; 1744 } 1745 1746 static int 1747 eb_relocate_vma_slow(struct i915_execbuffer *eb, struct eb_vma *ev) 1748 { 1749 const struct drm_i915_gem_exec_object2 *entry = ev->exec; 1750 struct drm_i915_gem_relocation_entry *relocs = 1751 u64_to_ptr(typeof(*relocs), entry->relocs_ptr); 1752 unsigned int i; 1753 int err; 1754 1755 for (i = 0; i < entry->relocation_count; i++) { 1756 u64 offset = eb_relocate_entry(eb, ev, &relocs[i]); 1757 1758 if ((s64)offset < 0) { 1759 err = (int)offset; 1760 goto err; 1761 } 1762 } 1763 err = 0; 1764 err: 1765 reloc_cache_reset(&eb->reloc_cache, eb); 1766 return err; 1767 } 1768 1769 static int check_relocations(const struct drm_i915_gem_exec_object2 *entry) 1770 { 1771 const char __user *addr, *end; 1772 unsigned long size; 1773 char __maybe_unused c; 1774 1775 size = entry->relocation_count; 1776 if (size == 0) 1777 return 0; 1778 1779 if (size > N_RELOC(ULONG_MAX)) 1780 return -EINVAL; 1781 1782 addr = u64_to_user_ptr(entry->relocs_ptr); 1783 size *= sizeof(struct drm_i915_gem_relocation_entry); 1784 if (!access_ok(addr, size)) 1785 return -EFAULT; 1786 1787 end = addr + size; 1788 for (; addr < end; addr += PAGE_SIZE) { 1789 int err = __get_user(c, addr); 1790 if (err) 1791 return err; 1792 } 1793 return __get_user(c, end - 1); 1794 } 1795 1796 static int eb_copy_relocations(const struct i915_execbuffer *eb) 1797 { 1798 struct drm_i915_gem_relocation_entry *relocs; 1799 const unsigned int count = eb->buffer_count; 1800 unsigned int i; 1801 int err; 1802 1803 for (i = 0; i < count; i++) { 1804 const unsigned int nreloc = eb->exec[i].relocation_count; 1805 struct drm_i915_gem_relocation_entry __user *urelocs; 1806 unsigned long size; 1807 unsigned long copied; 1808 1809 if (nreloc == 0) 1810 continue; 1811 1812 err = check_relocations(&eb->exec[i]); 1813 if (err) 1814 goto err; 1815 1816 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr); 1817 size = nreloc * sizeof(*relocs); 1818 1819 relocs = kvmalloc_array(size, 1, GFP_KERNEL); 1820 if (!relocs) { 1821 err = -ENOMEM; 1822 goto err; 1823 } 1824 1825 /* copy_from_user is limited to < 4GiB */ 1826 copied = 0; 1827 do { 1828 unsigned int len = 1829 min_t(u64, BIT_ULL(31), size - copied); 1830 1831 if (__copy_from_user((char *)relocs + copied, 1832 (char __user *)urelocs + copied, 1833 len)) 1834 goto end; 1835 1836 copied += len; 1837 } while (copied < size); 1838 1839 /* 1840 * As we do not update the known relocation offsets after 1841 * relocating (due to the complexities in lock handling), 1842 * we need to mark them as invalid now so that we force the 1843 * relocation processing next time. Just in case the target 1844 * object is evicted and then rebound into its old 1845 * presumed_offset before the next execbuffer - if that 1846 * happened we would make the mistake of assuming that the 1847 * relocations were valid. 1848 */ 1849 if (!user_access_begin(urelocs, size)) 1850 goto end; 1851 1852 for (copied = 0; copied < nreloc; copied++) 1853 unsafe_put_user(-1, 1854 &urelocs[copied].presumed_offset, 1855 end_user); 1856 user_access_end(); 1857 1858 eb->exec[i].relocs_ptr = (uintptr_t)relocs; 1859 } 1860 1861 return 0; 1862 1863 end_user: 1864 user_access_end(); 1865 end: 1866 kvfree(relocs); 1867 err = -EFAULT; 1868 err: 1869 while (i--) { 1870 relocs = u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr); 1871 if (eb->exec[i].relocation_count) 1872 kvfree(relocs); 1873 } 1874 return err; 1875 } 1876 1877 static int eb_prefault_relocations(const struct i915_execbuffer *eb) 1878 { 1879 const unsigned int count = eb->buffer_count; 1880 unsigned int i; 1881 1882 for (i = 0; i < count; i++) { 1883 int err; 1884 1885 err = check_relocations(&eb->exec[i]); 1886 if (err) 1887 return err; 1888 } 1889 1890 return 0; 1891 } 1892 1893 static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb, 1894 struct i915_request *rq) 1895 { 1896 bool have_copy = false; 1897 struct eb_vma *ev; 1898 int err = 0; 1899 1900 repeat: 1901 if (signal_pending(current)) { 1902 err = -ERESTARTSYS; 1903 goto out; 1904 } 1905 1906 /* We may process another execbuffer during the unlock... */ 1907 eb_release_vmas(eb, false); 1908 i915_gem_ww_ctx_fini(&eb->ww); 1909 1910 if (rq) { 1911 /* nonblocking is always false */ 1912 if (i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE, 1913 MAX_SCHEDULE_TIMEOUT) < 0) { 1914 i915_request_put(rq); 1915 rq = NULL; 1916 1917 err = -EINTR; 1918 goto err_relock; 1919 } 1920 1921 i915_request_put(rq); 1922 rq = NULL; 1923 } 1924 1925 /* 1926 * We take 3 passes through the slowpatch. 1927 * 1928 * 1 - we try to just prefault all the user relocation entries and 1929 * then attempt to reuse the atomic pagefault disabled fast path again. 1930 * 1931 * 2 - we copy the user entries to a local buffer here outside of the 1932 * local and allow ourselves to wait upon any rendering before 1933 * relocations 1934 * 1935 * 3 - we already have a local copy of the relocation entries, but 1936 * were interrupted (EAGAIN) whilst waiting for the objects, try again. 1937 */ 1938 if (!err) { 1939 err = eb_prefault_relocations(eb); 1940 } else if (!have_copy) { 1941 err = eb_copy_relocations(eb); 1942 have_copy = err == 0; 1943 } else { 1944 cond_resched(); 1945 err = 0; 1946 } 1947 1948 if (!err) 1949 flush_workqueue(eb->i915->mm.userptr_wq); 1950 1951 err_relock: 1952 i915_gem_ww_ctx_init(&eb->ww, true); 1953 if (err) 1954 goto out; 1955 1956 /* reacquire the objects */ 1957 repeat_validate: 1958 rq = eb_pin_engine(eb, false); 1959 if (IS_ERR(rq)) { 1960 err = PTR_ERR(rq); 1961 rq = NULL; 1962 goto err; 1963 } 1964 1965 /* We didn't throttle, should be NULL */ 1966 GEM_WARN_ON(rq); 1967 1968 err = eb_validate_vmas(eb); 1969 if (err) 1970 goto err; 1971 1972 GEM_BUG_ON(!eb->batch); 1973 1974 list_for_each_entry(ev, &eb->relocs, reloc_link) { 1975 if (!have_copy) { 1976 pagefault_disable(); 1977 err = eb_relocate_vma(eb, ev); 1978 pagefault_enable(); 1979 if (err) 1980 break; 1981 } else { 1982 err = eb_relocate_vma_slow(eb, ev); 1983 if (err) 1984 break; 1985 } 1986 } 1987 1988 if (err == -EDEADLK) 1989 goto err; 1990 1991 if (err && !have_copy) 1992 goto repeat; 1993 1994 if (err) 1995 goto err; 1996 1997 /* as last step, parse the command buffer */ 1998 err = eb_parse(eb); 1999 if (err) 2000 goto err; 2001 2002 /* 2003 * Leave the user relocations as are, this is the painfully slow path, 2004 * and we want to avoid the complication of dropping the lock whilst 2005 * having buffers reserved in the aperture and so causing spurious 2006 * ENOSPC for random operations. 2007 */ 2008 2009 err: 2010 if (err == -EDEADLK) { 2011 eb_release_vmas(eb, false); 2012 err = i915_gem_ww_ctx_backoff(&eb->ww); 2013 if (!err) 2014 goto repeat_validate; 2015 } 2016 2017 if (err == -EAGAIN) 2018 goto repeat; 2019 2020 out: 2021 if (have_copy) { 2022 const unsigned int count = eb->buffer_count; 2023 unsigned int i; 2024 2025 for (i = 0; i < count; i++) { 2026 const struct drm_i915_gem_exec_object2 *entry = 2027 &eb->exec[i]; 2028 struct drm_i915_gem_relocation_entry *relocs; 2029 2030 if (!entry->relocation_count) 2031 continue; 2032 2033 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr); 2034 kvfree(relocs); 2035 } 2036 } 2037 2038 if (rq) 2039 i915_request_put(rq); 2040 2041 return err; 2042 } 2043 2044 static int eb_relocate_parse(struct i915_execbuffer *eb) 2045 { 2046 int err; 2047 struct i915_request *rq = NULL; 2048 bool throttle = true; 2049 2050 retry: 2051 rq = eb_pin_engine(eb, throttle); 2052 if (IS_ERR(rq)) { 2053 err = PTR_ERR(rq); 2054 rq = NULL; 2055 if (err != -EDEADLK) 2056 return err; 2057 2058 goto err; 2059 } 2060 2061 if (rq) { 2062 bool nonblock = eb->file->filp->f_flags & O_NONBLOCK; 2063 2064 /* Need to drop all locks now for throttling, take slowpath */ 2065 err = i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE, 0); 2066 if (err == -ETIME) { 2067 if (nonblock) { 2068 err = -EWOULDBLOCK; 2069 i915_request_put(rq); 2070 goto err; 2071 } 2072 goto slow; 2073 } 2074 i915_request_put(rq); 2075 rq = NULL; 2076 } 2077 2078 /* only throttle once, even if we didn't need to throttle */ 2079 throttle = false; 2080 2081 err = eb_validate_vmas(eb); 2082 if (err == -EAGAIN) 2083 goto slow; 2084 else if (err) 2085 goto err; 2086 2087 /* The objects are in their final locations, apply the relocations. */ 2088 if (eb->args->flags & __EXEC_HAS_RELOC) { 2089 struct eb_vma *ev; 2090 2091 list_for_each_entry(ev, &eb->relocs, reloc_link) { 2092 err = eb_relocate_vma(eb, ev); 2093 if (err) 2094 break; 2095 } 2096 2097 if (err == -EDEADLK) 2098 goto err; 2099 else if (err) 2100 goto slow; 2101 } 2102 2103 if (!err) 2104 err = eb_parse(eb); 2105 2106 err: 2107 if (err == -EDEADLK) { 2108 eb_release_vmas(eb, false); 2109 err = i915_gem_ww_ctx_backoff(&eb->ww); 2110 if (!err) 2111 goto retry; 2112 } 2113 2114 return err; 2115 2116 slow: 2117 err = eb_relocate_parse_slow(eb, rq); 2118 if (err) 2119 /* 2120 * If the user expects the execobject.offset and 2121 * reloc.presumed_offset to be an exact match, 2122 * as for using NO_RELOC, then we cannot update 2123 * the execobject.offset until we have completed 2124 * relocation. 2125 */ 2126 eb->args->flags &= ~__EXEC_HAS_RELOC; 2127 2128 return err; 2129 } 2130 2131 static int eb_move_to_gpu(struct i915_execbuffer *eb) 2132 { 2133 const unsigned int count = eb->buffer_count; 2134 unsigned int i = count; 2135 int err = 0; 2136 2137 while (i--) { 2138 struct eb_vma *ev = &eb->vma[i]; 2139 struct i915_vma *vma = ev->vma; 2140 unsigned int flags = ev->flags; 2141 struct drm_i915_gem_object *obj = vma->obj; 2142 2143 assert_vma_held(vma); 2144 2145 if (flags & EXEC_OBJECT_CAPTURE) { 2146 struct i915_capture_list *capture; 2147 2148 capture = kmalloc(sizeof(*capture), GFP_KERNEL); 2149 if (capture) { 2150 capture->next = eb->request->capture_list; 2151 capture->vma = vma; 2152 eb->request->capture_list = capture; 2153 } 2154 } 2155 2156 /* 2157 * If the GPU is not _reading_ through the CPU cache, we need 2158 * to make sure that any writes (both previous GPU writes from 2159 * before a change in snooping levels and normal CPU writes) 2160 * caught in that cache are flushed to main memory. 2161 * 2162 * We want to say 2163 * obj->cache_dirty && 2164 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ) 2165 * but gcc's optimiser doesn't handle that as well and emits 2166 * two jumps instead of one. Maybe one day... 2167 */ 2168 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) { 2169 if (i915_gem_clflush_object(obj, 0)) 2170 flags &= ~EXEC_OBJECT_ASYNC; 2171 } 2172 2173 if (err == 0 && !(flags & EXEC_OBJECT_ASYNC)) { 2174 err = i915_request_await_object 2175 (eb->request, obj, flags & EXEC_OBJECT_WRITE); 2176 } 2177 2178 if (err == 0) 2179 err = i915_vma_move_to_active(vma, eb->request, flags); 2180 } 2181 2182 if (unlikely(err)) 2183 goto err_skip; 2184 2185 /* Unconditionally flush any chipset caches (for streaming writes). */ 2186 intel_gt_chipset_flush(eb->engine->gt); 2187 return 0; 2188 2189 err_skip: 2190 i915_request_set_error_once(eb->request, err); 2191 return err; 2192 } 2193 2194 static int i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) 2195 { 2196 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS) 2197 return -EINVAL; 2198 2199 /* Kernel clipping was a DRI1 misfeature */ 2200 if (!(exec->flags & (I915_EXEC_FENCE_ARRAY | 2201 I915_EXEC_USE_EXTENSIONS))) { 2202 if (exec->num_cliprects || exec->cliprects_ptr) 2203 return -EINVAL; 2204 } 2205 2206 if (exec->DR4 == 0xffffffff) { 2207 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); 2208 exec->DR4 = 0; 2209 } 2210 if (exec->DR1 || exec->DR4) 2211 return -EINVAL; 2212 2213 if ((exec->batch_start_offset | exec->batch_len) & 0x7) 2214 return -EINVAL; 2215 2216 return 0; 2217 } 2218 2219 static int i915_reset_gen7_sol_offsets(struct i915_request *rq) 2220 { 2221 u32 *cs; 2222 int i; 2223 2224 if (!IS_GEN(rq->engine->i915, 7) || rq->engine->id != RCS0) { 2225 drm_dbg(&rq->engine->i915->drm, "sol reset is gen7/rcs only\n"); 2226 return -EINVAL; 2227 } 2228 2229 cs = intel_ring_begin(rq, 4 * 2 + 2); 2230 if (IS_ERR(cs)) 2231 return PTR_ERR(cs); 2232 2233 *cs++ = MI_LOAD_REGISTER_IMM(4); 2234 for (i = 0; i < 4; i++) { 2235 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i)); 2236 *cs++ = 0; 2237 } 2238 *cs++ = MI_NOOP; 2239 intel_ring_advance(rq, cs); 2240 2241 return 0; 2242 } 2243 2244 static struct i915_vma * 2245 shadow_batch_pin(struct i915_execbuffer *eb, 2246 struct drm_i915_gem_object *obj, 2247 struct i915_address_space *vm, 2248 unsigned int flags) 2249 { 2250 struct i915_vma *vma; 2251 int err; 2252 2253 vma = i915_vma_instance(obj, vm, NULL); 2254 if (IS_ERR(vma)) 2255 return vma; 2256 2257 err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, flags); 2258 if (err) 2259 return ERR_PTR(err); 2260 2261 return vma; 2262 } 2263 2264 struct eb_parse_work { 2265 struct dma_fence_work base; 2266 struct intel_engine_cs *engine; 2267 struct i915_vma *batch; 2268 struct i915_vma *shadow; 2269 struct i915_vma *trampoline; 2270 unsigned int batch_offset; 2271 unsigned int batch_length; 2272 }; 2273 2274 static int __eb_parse(struct dma_fence_work *work) 2275 { 2276 struct eb_parse_work *pw = container_of(work, typeof(*pw), base); 2277 2278 return intel_engine_cmd_parser(pw->engine, 2279 pw->batch, 2280 pw->batch_offset, 2281 pw->batch_length, 2282 pw->shadow, 2283 pw->trampoline); 2284 } 2285 2286 static void __eb_parse_release(struct dma_fence_work *work) 2287 { 2288 struct eb_parse_work *pw = container_of(work, typeof(*pw), base); 2289 2290 if (pw->trampoline) 2291 i915_active_release(&pw->trampoline->active); 2292 i915_active_release(&pw->shadow->active); 2293 i915_active_release(&pw->batch->active); 2294 } 2295 2296 static const struct dma_fence_work_ops eb_parse_ops = { 2297 .name = "eb_parse", 2298 .work = __eb_parse, 2299 .release = __eb_parse_release, 2300 }; 2301 2302 static inline int 2303 __parser_mark_active(struct i915_vma *vma, 2304 struct intel_timeline *tl, 2305 struct dma_fence *fence) 2306 { 2307 struct intel_gt_buffer_pool_node *node = vma->private; 2308 2309 return i915_active_ref(&node->active, tl->fence_context, fence); 2310 } 2311 2312 static int 2313 parser_mark_active(struct eb_parse_work *pw, struct intel_timeline *tl) 2314 { 2315 int err; 2316 2317 mutex_lock(&tl->mutex); 2318 2319 err = __parser_mark_active(pw->shadow, tl, &pw->base.dma); 2320 if (err) 2321 goto unlock; 2322 2323 if (pw->trampoline) { 2324 err = __parser_mark_active(pw->trampoline, tl, &pw->base.dma); 2325 if (err) 2326 goto unlock; 2327 } 2328 2329 unlock: 2330 mutex_unlock(&tl->mutex); 2331 return err; 2332 } 2333 2334 static int eb_parse_pipeline(struct i915_execbuffer *eb, 2335 struct i915_vma *shadow, 2336 struct i915_vma *trampoline) 2337 { 2338 struct eb_parse_work *pw; 2339 int err; 2340 2341 pw = kzalloc(sizeof(*pw), GFP_KERNEL); 2342 if (!pw) 2343 return -ENOMEM; 2344 2345 err = i915_active_acquire(&eb->batch->vma->active); 2346 if (err) 2347 goto err_free; 2348 2349 err = i915_active_acquire(&shadow->active); 2350 if (err) 2351 goto err_batch; 2352 2353 if (trampoline) { 2354 err = i915_active_acquire(&trampoline->active); 2355 if (err) 2356 goto err_shadow; 2357 } 2358 2359 dma_fence_work_init(&pw->base, &eb_parse_ops); 2360 2361 pw->engine = eb->engine; 2362 pw->batch = eb->batch->vma; 2363 pw->batch_offset = eb->batch_start_offset; 2364 pw->batch_length = eb->batch_len; 2365 pw->shadow = shadow; 2366 pw->trampoline = trampoline; 2367 2368 /* Mark active refs early for this worker, in case we get interrupted */ 2369 err = parser_mark_active(pw, eb->context->timeline); 2370 if (err) 2371 goto err_commit; 2372 2373 err = dma_resv_reserve_shared(pw->batch->resv, 1); 2374 if (err) 2375 goto err_commit; 2376 2377 /* Wait for all writes (and relocs) into the batch to complete */ 2378 err = i915_sw_fence_await_reservation(&pw->base.chain, 2379 pw->batch->resv, NULL, false, 2380 0, I915_FENCE_GFP); 2381 if (err < 0) 2382 goto err_commit; 2383 2384 /* Keep the batch alive and unwritten as we parse */ 2385 dma_resv_add_shared_fence(pw->batch->resv, &pw->base.dma); 2386 2387 /* Force execution to wait for completion of the parser */ 2388 dma_resv_add_excl_fence(shadow->resv, &pw->base.dma); 2389 2390 dma_fence_work_commit_imm(&pw->base); 2391 return 0; 2392 2393 err_commit: 2394 i915_sw_fence_set_error_once(&pw->base.chain, err); 2395 dma_fence_work_commit_imm(&pw->base); 2396 return err; 2397 2398 err_shadow: 2399 i915_active_release(&shadow->active); 2400 err_batch: 2401 i915_active_release(&eb->batch->vma->active); 2402 err_free: 2403 kfree(pw); 2404 return err; 2405 } 2406 2407 static struct i915_vma *eb_dispatch_secure(struct i915_execbuffer *eb, struct i915_vma *vma) 2408 { 2409 /* 2410 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure 2411 * batch" bit. Hence we need to pin secure batches into the global gtt. 2412 * hsw should have this fixed, but bdw mucks it up again. */ 2413 if (eb->batch_flags & I915_DISPATCH_SECURE) 2414 return i915_gem_object_ggtt_pin_ww(vma->obj, &eb->ww, NULL, 0, 0, 0); 2415 2416 return NULL; 2417 } 2418 2419 static int eb_parse(struct i915_execbuffer *eb) 2420 { 2421 struct drm_i915_private *i915 = eb->i915; 2422 struct intel_gt_buffer_pool_node *pool = eb->batch_pool; 2423 struct i915_vma *shadow, *trampoline, *batch; 2424 unsigned int len; 2425 int err; 2426 2427 if (!eb_use_cmdparser(eb)) { 2428 batch = eb_dispatch_secure(eb, eb->batch->vma); 2429 if (IS_ERR(batch)) 2430 return PTR_ERR(batch); 2431 2432 goto secure_batch; 2433 } 2434 2435 len = eb->batch_len; 2436 if (!CMDPARSER_USES_GGTT(eb->i915)) { 2437 /* 2438 * ppGTT backed shadow buffers must be mapped RO, to prevent 2439 * post-scan tampering 2440 */ 2441 if (!eb->context->vm->has_read_only) { 2442 drm_dbg(&i915->drm, 2443 "Cannot prevent post-scan tampering without RO capable vm\n"); 2444 return -EINVAL; 2445 } 2446 } else { 2447 len += I915_CMD_PARSER_TRAMPOLINE_SIZE; 2448 } 2449 2450 if (!pool) { 2451 pool = intel_gt_get_buffer_pool(eb->engine->gt, len); 2452 if (IS_ERR(pool)) 2453 return PTR_ERR(pool); 2454 eb->batch_pool = pool; 2455 } 2456 2457 err = i915_gem_object_lock(pool->obj, &eb->ww); 2458 if (err) 2459 goto err; 2460 2461 shadow = shadow_batch_pin(eb, pool->obj, eb->context->vm, PIN_USER); 2462 if (IS_ERR(shadow)) { 2463 err = PTR_ERR(shadow); 2464 goto err; 2465 } 2466 i915_gem_object_set_readonly(shadow->obj); 2467 shadow->private = pool; 2468 2469 trampoline = NULL; 2470 if (CMDPARSER_USES_GGTT(eb->i915)) { 2471 trampoline = shadow; 2472 2473 shadow = shadow_batch_pin(eb, pool->obj, 2474 &eb->engine->gt->ggtt->vm, 2475 PIN_GLOBAL); 2476 if (IS_ERR(shadow)) { 2477 err = PTR_ERR(shadow); 2478 shadow = trampoline; 2479 goto err_shadow; 2480 } 2481 shadow->private = pool; 2482 2483 eb->batch_flags |= I915_DISPATCH_SECURE; 2484 } 2485 2486 batch = eb_dispatch_secure(eb, shadow); 2487 if (IS_ERR(batch)) { 2488 err = PTR_ERR(batch); 2489 goto err_trampoline; 2490 } 2491 2492 err = eb_parse_pipeline(eb, shadow, trampoline); 2493 if (err) 2494 goto err_unpin_batch; 2495 2496 eb->batch = &eb->vma[eb->buffer_count++]; 2497 eb->batch->vma = i915_vma_get(shadow); 2498 eb->batch->flags = __EXEC_OBJECT_HAS_PIN; 2499 2500 eb->trampoline = trampoline; 2501 eb->batch_start_offset = 0; 2502 2503 secure_batch: 2504 if (batch) { 2505 eb->batch = &eb->vma[eb->buffer_count++]; 2506 eb->batch->flags = __EXEC_OBJECT_HAS_PIN; 2507 eb->batch->vma = i915_vma_get(batch); 2508 } 2509 return 0; 2510 2511 err_unpin_batch: 2512 if (batch) 2513 i915_vma_unpin(batch); 2514 err_trampoline: 2515 if (trampoline) 2516 i915_vma_unpin(trampoline); 2517 err_shadow: 2518 i915_vma_unpin(shadow); 2519 err: 2520 return err; 2521 } 2522 2523 static int eb_submit(struct i915_execbuffer *eb, struct i915_vma *batch) 2524 { 2525 int err; 2526 2527 err = eb_move_to_gpu(eb); 2528 if (err) 2529 return err; 2530 2531 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) { 2532 err = i915_reset_gen7_sol_offsets(eb->request); 2533 if (err) 2534 return err; 2535 } 2536 2537 /* 2538 * After we completed waiting for other engines (using HW semaphores) 2539 * then we can signal that this request/batch is ready to run. This 2540 * allows us to determine if the batch is still waiting on the GPU 2541 * or actually running by checking the breadcrumb. 2542 */ 2543 if (eb->engine->emit_init_breadcrumb) { 2544 err = eb->engine->emit_init_breadcrumb(eb->request); 2545 if (err) 2546 return err; 2547 } 2548 2549 err = eb->engine->emit_bb_start(eb->request, 2550 batch->node.start + 2551 eb->batch_start_offset, 2552 eb->batch_len, 2553 eb->batch_flags); 2554 if (err) 2555 return err; 2556 2557 if (eb->trampoline) { 2558 GEM_BUG_ON(eb->batch_start_offset); 2559 err = eb->engine->emit_bb_start(eb->request, 2560 eb->trampoline->node.start + 2561 eb->batch_len, 2562 0, 0); 2563 if (err) 2564 return err; 2565 } 2566 2567 if (intel_context_nopreempt(eb->context)) 2568 __set_bit(I915_FENCE_FLAG_NOPREEMPT, &eb->request->fence.flags); 2569 2570 return 0; 2571 } 2572 2573 static int num_vcs_engines(const struct drm_i915_private *i915) 2574 { 2575 return hweight64(VDBOX_MASK(&i915->gt)); 2576 } 2577 2578 /* 2579 * Find one BSD ring to dispatch the corresponding BSD command. 2580 * The engine index is returned. 2581 */ 2582 static unsigned int 2583 gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv, 2584 struct drm_file *file) 2585 { 2586 struct drm_i915_file_private *file_priv = file->driver_priv; 2587 2588 /* Check whether the file_priv has already selected one ring. */ 2589 if ((int)file_priv->bsd_engine < 0) 2590 file_priv->bsd_engine = 2591 get_random_int() % num_vcs_engines(dev_priv); 2592 2593 return file_priv->bsd_engine; 2594 } 2595 2596 static const enum intel_engine_id user_ring_map[] = { 2597 [I915_EXEC_DEFAULT] = RCS0, 2598 [I915_EXEC_RENDER] = RCS0, 2599 [I915_EXEC_BLT] = BCS0, 2600 [I915_EXEC_BSD] = VCS0, 2601 [I915_EXEC_VEBOX] = VECS0 2602 }; 2603 2604 static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel_context *ce) 2605 { 2606 struct intel_ring *ring = ce->ring; 2607 struct intel_timeline *tl = ce->timeline; 2608 struct i915_request *rq; 2609 2610 /* 2611 * Completely unscientific finger-in-the-air estimates for suitable 2612 * maximum user request size (to avoid blocking) and then backoff. 2613 */ 2614 if (intel_ring_update_space(ring) >= PAGE_SIZE) 2615 return NULL; 2616 2617 /* 2618 * Find a request that after waiting upon, there will be at least half 2619 * the ring available. The hysteresis allows us to compete for the 2620 * shared ring and should mean that we sleep less often prior to 2621 * claiming our resources, but not so long that the ring completely 2622 * drains before we can submit our next request. 2623 */ 2624 list_for_each_entry(rq, &tl->requests, link) { 2625 if (rq->ring != ring) 2626 continue; 2627 2628 if (__intel_ring_space(rq->postfix, 2629 ring->emit, ring->size) > ring->size / 2) 2630 break; 2631 } 2632 if (&rq->link == &tl->requests) 2633 return NULL; /* weird, we will check again later for real */ 2634 2635 return i915_request_get(rq); 2636 } 2637 2638 static struct i915_request *eb_pin_engine(struct i915_execbuffer *eb, bool throttle) 2639 { 2640 struct intel_context *ce = eb->context; 2641 struct intel_timeline *tl; 2642 struct i915_request *rq = NULL; 2643 int err; 2644 2645 GEM_BUG_ON(eb->args->flags & __EXEC_ENGINE_PINNED); 2646 2647 if (unlikely(intel_context_is_banned(ce))) 2648 return ERR_PTR(-EIO); 2649 2650 /* 2651 * Pinning the contexts may generate requests in order to acquire 2652 * GGTT space, so do this first before we reserve a seqno for 2653 * ourselves. 2654 */ 2655 err = intel_context_pin_ww(ce, &eb->ww); 2656 if (err) 2657 return ERR_PTR(err); 2658 2659 /* 2660 * Take a local wakeref for preparing to dispatch the execbuf as 2661 * we expect to access the hardware fairly frequently in the 2662 * process, and require the engine to be kept awake between accesses. 2663 * Upon dispatch, we acquire another prolonged wakeref that we hold 2664 * until the timeline is idle, which in turn releases the wakeref 2665 * taken on the engine, and the parent device. 2666 */ 2667 tl = intel_context_timeline_lock(ce); 2668 if (IS_ERR(tl)) { 2669 intel_context_unpin(ce); 2670 return ERR_CAST(tl); 2671 } 2672 2673 intel_context_enter(ce); 2674 if (throttle) 2675 rq = eb_throttle(eb, ce); 2676 intel_context_timeline_unlock(tl); 2677 2678 eb->args->flags |= __EXEC_ENGINE_PINNED; 2679 return rq; 2680 } 2681 2682 static void eb_unpin_engine(struct i915_execbuffer *eb) 2683 { 2684 struct intel_context *ce = eb->context; 2685 struct intel_timeline *tl = ce->timeline; 2686 2687 if (!(eb->args->flags & __EXEC_ENGINE_PINNED)) 2688 return; 2689 2690 eb->args->flags &= ~__EXEC_ENGINE_PINNED; 2691 2692 mutex_lock(&tl->mutex); 2693 intel_context_exit(ce); 2694 mutex_unlock(&tl->mutex); 2695 2696 intel_context_unpin(ce); 2697 } 2698 2699 static unsigned int 2700 eb_select_legacy_ring(struct i915_execbuffer *eb) 2701 { 2702 struct drm_i915_private *i915 = eb->i915; 2703 struct drm_i915_gem_execbuffer2 *args = eb->args; 2704 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK; 2705 2706 if (user_ring_id != I915_EXEC_BSD && 2707 (args->flags & I915_EXEC_BSD_MASK)) { 2708 drm_dbg(&i915->drm, 2709 "execbuf with non bsd ring but with invalid " 2710 "bsd dispatch flags: %d\n", (int)(args->flags)); 2711 return -1; 2712 } 2713 2714 if (user_ring_id == I915_EXEC_BSD && num_vcs_engines(i915) > 1) { 2715 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK; 2716 2717 if (bsd_idx == I915_EXEC_BSD_DEFAULT) { 2718 bsd_idx = gen8_dispatch_bsd_engine(i915, eb->file); 2719 } else if (bsd_idx >= I915_EXEC_BSD_RING1 && 2720 bsd_idx <= I915_EXEC_BSD_RING2) { 2721 bsd_idx >>= I915_EXEC_BSD_SHIFT; 2722 bsd_idx--; 2723 } else { 2724 drm_dbg(&i915->drm, 2725 "execbuf with unknown bsd ring: %u\n", 2726 bsd_idx); 2727 return -1; 2728 } 2729 2730 return _VCS(bsd_idx); 2731 } 2732 2733 if (user_ring_id >= ARRAY_SIZE(user_ring_map)) { 2734 drm_dbg(&i915->drm, "execbuf with unknown ring: %u\n", 2735 user_ring_id); 2736 return -1; 2737 } 2738 2739 return user_ring_map[user_ring_id]; 2740 } 2741 2742 static int 2743 eb_select_engine(struct i915_execbuffer *eb) 2744 { 2745 struct intel_context *ce; 2746 unsigned int idx; 2747 int err; 2748 2749 if (i915_gem_context_user_engines(eb->gem_context)) 2750 idx = eb->args->flags & I915_EXEC_RING_MASK; 2751 else 2752 idx = eb_select_legacy_ring(eb); 2753 2754 ce = i915_gem_context_get_engine(eb->gem_context, idx); 2755 if (IS_ERR(ce)) 2756 return PTR_ERR(ce); 2757 2758 intel_gt_pm_get(ce->engine->gt); 2759 2760 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) { 2761 err = intel_context_alloc_state(ce); 2762 if (err) 2763 goto err; 2764 } 2765 2766 /* 2767 * ABI: Before userspace accesses the GPU (e.g. execbuffer), report 2768 * EIO if the GPU is already wedged. 2769 */ 2770 err = intel_gt_terminally_wedged(ce->engine->gt); 2771 if (err) 2772 goto err; 2773 2774 eb->context = ce; 2775 eb->engine = ce->engine; 2776 2777 /* 2778 * Make sure engine pool stays alive even if we call intel_context_put 2779 * during ww handling. The pool is destroyed when last pm reference 2780 * is dropped, which breaks our -EDEADLK handling. 2781 */ 2782 return err; 2783 2784 err: 2785 intel_gt_pm_put(ce->engine->gt); 2786 intel_context_put(ce); 2787 return err; 2788 } 2789 2790 static void 2791 eb_put_engine(struct i915_execbuffer *eb) 2792 { 2793 intel_gt_pm_put(eb->engine->gt); 2794 intel_context_put(eb->context); 2795 } 2796 2797 static void 2798 __free_fence_array(struct eb_fence *fences, unsigned int n) 2799 { 2800 while (n--) { 2801 drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2)); 2802 dma_fence_put(fences[n].dma_fence); 2803 kfree(fences[n].chain_fence); 2804 } 2805 kvfree(fences); 2806 } 2807 2808 static int 2809 add_timeline_fence_array(struct i915_execbuffer *eb, 2810 const struct drm_i915_gem_execbuffer_ext_timeline_fences *timeline_fences) 2811 { 2812 struct drm_i915_gem_exec_fence __user *user_fences; 2813 u64 __user *user_values; 2814 struct eb_fence *f; 2815 u64 nfences; 2816 int err = 0; 2817 2818 nfences = timeline_fences->fence_count; 2819 if (!nfences) 2820 return 0; 2821 2822 /* Check multiplication overflow for access_ok() and kvmalloc_array() */ 2823 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); 2824 if (nfences > min_t(unsigned long, 2825 ULONG_MAX / sizeof(*user_fences), 2826 SIZE_MAX / sizeof(*f)) - eb->num_fences) 2827 return -EINVAL; 2828 2829 user_fences = u64_to_user_ptr(timeline_fences->handles_ptr); 2830 if (!access_ok(user_fences, nfences * sizeof(*user_fences))) 2831 return -EFAULT; 2832 2833 user_values = u64_to_user_ptr(timeline_fences->values_ptr); 2834 if (!access_ok(user_values, nfences * sizeof(*user_values))) 2835 return -EFAULT; 2836 2837 f = krealloc(eb->fences, 2838 (eb->num_fences + nfences) * sizeof(*f), 2839 __GFP_NOWARN | GFP_KERNEL); 2840 if (!f) 2841 return -ENOMEM; 2842 2843 eb->fences = f; 2844 f += eb->num_fences; 2845 2846 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & 2847 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); 2848 2849 while (nfences--) { 2850 struct drm_i915_gem_exec_fence user_fence; 2851 struct drm_syncobj *syncobj; 2852 struct dma_fence *fence = NULL; 2853 u64 point; 2854 2855 if (__copy_from_user(&user_fence, 2856 user_fences++, 2857 sizeof(user_fence))) 2858 return -EFAULT; 2859 2860 if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) 2861 return -EINVAL; 2862 2863 if (__get_user(point, user_values++)) 2864 return -EFAULT; 2865 2866 syncobj = drm_syncobj_find(eb->file, user_fence.handle); 2867 if (!syncobj) { 2868 DRM_DEBUG("Invalid syncobj handle provided\n"); 2869 return -ENOENT; 2870 } 2871 2872 fence = drm_syncobj_fence_get(syncobj); 2873 2874 if (!fence && user_fence.flags && 2875 !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2876 DRM_DEBUG("Syncobj handle has no fence\n"); 2877 drm_syncobj_put(syncobj); 2878 return -EINVAL; 2879 } 2880 2881 if (fence) 2882 err = dma_fence_chain_find_seqno(&fence, point); 2883 2884 if (err && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2885 DRM_DEBUG("Syncobj handle missing requested point %llu\n", point); 2886 dma_fence_put(fence); 2887 drm_syncobj_put(syncobj); 2888 return err; 2889 } 2890 2891 /* 2892 * A point might have been signaled already and 2893 * garbage collected from the timeline. In this case 2894 * just ignore the point and carry on. 2895 */ 2896 if (!fence && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2897 drm_syncobj_put(syncobj); 2898 continue; 2899 } 2900 2901 /* 2902 * For timeline syncobjs we need to preallocate chains for 2903 * later signaling. 2904 */ 2905 if (point != 0 && user_fence.flags & I915_EXEC_FENCE_SIGNAL) { 2906 /* 2907 * Waiting and signaling the same point (when point != 2908 * 0) would break the timeline. 2909 */ 2910 if (user_fence.flags & I915_EXEC_FENCE_WAIT) { 2911 DRM_DEBUG("Trying to wait & signal the same timeline point.\n"); 2912 dma_fence_put(fence); 2913 drm_syncobj_put(syncobj); 2914 return -EINVAL; 2915 } 2916 2917 f->chain_fence = 2918 kmalloc(sizeof(*f->chain_fence), 2919 GFP_KERNEL); 2920 if (!f->chain_fence) { 2921 drm_syncobj_put(syncobj); 2922 dma_fence_put(fence); 2923 return -ENOMEM; 2924 } 2925 } else { 2926 f->chain_fence = NULL; 2927 } 2928 2929 f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); 2930 f->dma_fence = fence; 2931 f->value = point; 2932 f++; 2933 eb->num_fences++; 2934 } 2935 2936 return 0; 2937 } 2938 2939 static int add_fence_array(struct i915_execbuffer *eb) 2940 { 2941 struct drm_i915_gem_execbuffer2 *args = eb->args; 2942 struct drm_i915_gem_exec_fence __user *user; 2943 unsigned long num_fences = args->num_cliprects; 2944 struct eb_fence *f; 2945 2946 if (!(args->flags & I915_EXEC_FENCE_ARRAY)) 2947 return 0; 2948 2949 if (!num_fences) 2950 return 0; 2951 2952 /* Check multiplication overflow for access_ok() and kvmalloc_array() */ 2953 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); 2954 if (num_fences > min_t(unsigned long, 2955 ULONG_MAX / sizeof(*user), 2956 SIZE_MAX / sizeof(*f) - eb->num_fences)) 2957 return -EINVAL; 2958 2959 user = u64_to_user_ptr(args->cliprects_ptr); 2960 if (!access_ok(user, num_fences * sizeof(*user))) 2961 return -EFAULT; 2962 2963 f = krealloc(eb->fences, 2964 (eb->num_fences + num_fences) * sizeof(*f), 2965 __GFP_NOWARN | GFP_KERNEL); 2966 if (!f) 2967 return -ENOMEM; 2968 2969 eb->fences = f; 2970 f += eb->num_fences; 2971 while (num_fences--) { 2972 struct drm_i915_gem_exec_fence user_fence; 2973 struct drm_syncobj *syncobj; 2974 struct dma_fence *fence = NULL; 2975 2976 if (__copy_from_user(&user_fence, user++, sizeof(user_fence))) 2977 return -EFAULT; 2978 2979 if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) 2980 return -EINVAL; 2981 2982 syncobj = drm_syncobj_find(eb->file, user_fence.handle); 2983 if (!syncobj) { 2984 DRM_DEBUG("Invalid syncobj handle provided\n"); 2985 return -ENOENT; 2986 } 2987 2988 if (user_fence.flags & I915_EXEC_FENCE_WAIT) { 2989 fence = drm_syncobj_fence_get(syncobj); 2990 if (!fence) { 2991 DRM_DEBUG("Syncobj handle has no fence\n"); 2992 drm_syncobj_put(syncobj); 2993 return -EINVAL; 2994 } 2995 } 2996 2997 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & 2998 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); 2999 3000 f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); 3001 f->dma_fence = fence; 3002 f->value = 0; 3003 f->chain_fence = NULL; 3004 f++; 3005 eb->num_fences++; 3006 } 3007 3008 return 0; 3009 } 3010 3011 static void put_fence_array(struct eb_fence *fences, int num_fences) 3012 { 3013 if (fences) 3014 __free_fence_array(fences, num_fences); 3015 } 3016 3017 static int 3018 await_fence_array(struct i915_execbuffer *eb) 3019 { 3020 unsigned int n; 3021 int err; 3022 3023 for (n = 0; n < eb->num_fences; n++) { 3024 struct drm_syncobj *syncobj; 3025 unsigned int flags; 3026 3027 syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); 3028 3029 if (!eb->fences[n].dma_fence) 3030 continue; 3031 3032 err = i915_request_await_dma_fence(eb->request, 3033 eb->fences[n].dma_fence); 3034 if (err < 0) 3035 return err; 3036 } 3037 3038 return 0; 3039 } 3040 3041 static void signal_fence_array(const struct i915_execbuffer *eb) 3042 { 3043 struct dma_fence * const fence = &eb->request->fence; 3044 unsigned int n; 3045 3046 for (n = 0; n < eb->num_fences; n++) { 3047 struct drm_syncobj *syncobj; 3048 unsigned int flags; 3049 3050 syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); 3051 if (!(flags & I915_EXEC_FENCE_SIGNAL)) 3052 continue; 3053 3054 if (eb->fences[n].chain_fence) { 3055 drm_syncobj_add_point(syncobj, 3056 eb->fences[n].chain_fence, 3057 fence, 3058 eb->fences[n].value); 3059 /* 3060 * The chain's ownership is transferred to the 3061 * timeline. 3062 */ 3063 eb->fences[n].chain_fence = NULL; 3064 } else { 3065 drm_syncobj_replace_fence(syncobj, fence); 3066 } 3067 } 3068 } 3069 3070 static int 3071 parse_timeline_fences(struct i915_user_extension __user *ext, void *data) 3072 { 3073 struct i915_execbuffer *eb = data; 3074 struct drm_i915_gem_execbuffer_ext_timeline_fences timeline_fences; 3075 3076 if (copy_from_user(&timeline_fences, ext, sizeof(timeline_fences))) 3077 return -EFAULT; 3078 3079 return add_timeline_fence_array(eb, &timeline_fences); 3080 } 3081 3082 static void retire_requests(struct intel_timeline *tl, struct i915_request *end) 3083 { 3084 struct i915_request *rq, *rn; 3085 3086 list_for_each_entry_safe(rq, rn, &tl->requests, link) 3087 if (rq == end || !i915_request_retire(rq)) 3088 break; 3089 } 3090 3091 static void eb_request_add(struct i915_execbuffer *eb) 3092 { 3093 struct i915_request *rq = eb->request; 3094 struct intel_timeline * const tl = i915_request_timeline(rq); 3095 struct i915_sched_attr attr = {}; 3096 struct i915_request *prev; 3097 3098 lockdep_assert_held(&tl->mutex); 3099 lockdep_unpin_lock(&tl->mutex, rq->cookie); 3100 3101 trace_i915_request_add(rq); 3102 3103 prev = __i915_request_commit(rq); 3104 3105 /* Check that the context wasn't destroyed before submission */ 3106 if (likely(!intel_context_is_closed(eb->context))) { 3107 attr = eb->gem_context->sched; 3108 } else { 3109 /* Serialise with context_close via the add_to_timeline */ 3110 i915_request_set_error_once(rq, -ENOENT); 3111 __i915_request_skip(rq); 3112 } 3113 3114 __i915_request_queue(rq, &attr); 3115 3116 /* Try to clean up the client's timeline after submitting the request */ 3117 if (prev) 3118 retire_requests(tl, prev); 3119 3120 mutex_unlock(&tl->mutex); 3121 } 3122 3123 static const i915_user_extension_fn execbuf_extensions[] = { 3124 [DRM_I915_GEM_EXECBUFFER_EXT_TIMELINE_FENCES] = parse_timeline_fences, 3125 }; 3126 3127 static int 3128 parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 *args, 3129 struct i915_execbuffer *eb) 3130 { 3131 if (!(args->flags & I915_EXEC_USE_EXTENSIONS)) 3132 return 0; 3133 3134 /* The execbuf2 extension mechanism reuses cliprects_ptr. So we cannot 3135 * have another flag also using it at the same time. 3136 */ 3137 if (eb->args->flags & I915_EXEC_FENCE_ARRAY) 3138 return -EINVAL; 3139 3140 if (args->num_cliprects != 0) 3141 return -EINVAL; 3142 3143 return i915_user_extensions(u64_to_user_ptr(args->cliprects_ptr), 3144 execbuf_extensions, 3145 ARRAY_SIZE(execbuf_extensions), 3146 eb); 3147 } 3148 3149 static int 3150 i915_gem_do_execbuffer(struct drm_device *dev, 3151 struct drm_file *file, 3152 struct drm_i915_gem_execbuffer2 *args, 3153 struct drm_i915_gem_exec_object2 *exec) 3154 { 3155 struct drm_i915_private *i915 = to_i915(dev); 3156 struct i915_execbuffer eb; 3157 struct dma_fence *in_fence = NULL; 3158 struct sync_file *out_fence = NULL; 3159 struct i915_vma *batch; 3160 int out_fence_fd = -1; 3161 int err; 3162 3163 BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS); 3164 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & 3165 ~__EXEC_OBJECT_UNKNOWN_FLAGS); 3166 3167 eb.i915 = i915; 3168 eb.file = file; 3169 eb.args = args; 3170 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC)) 3171 args->flags |= __EXEC_HAS_RELOC; 3172 3173 eb.exec = exec; 3174 eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1); 3175 eb.vma[0].vma = NULL; 3176 eb.reloc_pool = eb.batch_pool = NULL; 3177 eb.reloc_context = NULL; 3178 3179 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; 3180 reloc_cache_init(&eb.reloc_cache, eb.i915); 3181 3182 eb.buffer_count = args->buffer_count; 3183 eb.batch_start_offset = args->batch_start_offset; 3184 eb.batch_len = args->batch_len; 3185 eb.trampoline = NULL; 3186 3187 eb.fences = NULL; 3188 eb.num_fences = 0; 3189 3190 eb.batch_flags = 0; 3191 if (args->flags & I915_EXEC_SECURE) { 3192 if (INTEL_GEN(i915) >= 11) 3193 return -ENODEV; 3194 3195 /* Return -EPERM to trigger fallback code on old binaries. */ 3196 if (!HAS_SECURE_BATCHES(i915)) 3197 return -EPERM; 3198 3199 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN)) 3200 return -EPERM; 3201 3202 eb.batch_flags |= I915_DISPATCH_SECURE; 3203 } 3204 if (args->flags & I915_EXEC_IS_PINNED) 3205 eb.batch_flags |= I915_DISPATCH_PINNED; 3206 3207 err = parse_execbuf2_extensions(args, &eb); 3208 if (err) 3209 goto err_ext; 3210 3211 err = add_fence_array(&eb); 3212 if (err) 3213 goto err_ext; 3214 3215 #define IN_FENCES (I915_EXEC_FENCE_IN | I915_EXEC_FENCE_SUBMIT) 3216 if (args->flags & IN_FENCES) { 3217 if ((args->flags & IN_FENCES) == IN_FENCES) 3218 return -EINVAL; 3219 3220 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2)); 3221 if (!in_fence) { 3222 err = -EINVAL; 3223 goto err_ext; 3224 } 3225 } 3226 #undef IN_FENCES 3227 3228 if (args->flags & I915_EXEC_FENCE_OUT) { 3229 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 3230 if (out_fence_fd < 0) { 3231 err = out_fence_fd; 3232 goto err_in_fence; 3233 } 3234 } 3235 3236 err = eb_create(&eb); 3237 if (err) 3238 goto err_out_fence; 3239 3240 GEM_BUG_ON(!eb.lut_size); 3241 3242 err = eb_select_context(&eb); 3243 if (unlikely(err)) 3244 goto err_destroy; 3245 3246 err = eb_select_engine(&eb); 3247 if (unlikely(err)) 3248 goto err_context; 3249 3250 err = eb_lookup_vmas(&eb); 3251 if (err) { 3252 eb_release_vmas(&eb, true); 3253 goto err_engine; 3254 } 3255 3256 i915_gem_ww_ctx_init(&eb.ww, true); 3257 3258 err = eb_relocate_parse(&eb); 3259 if (err) { 3260 /* 3261 * If the user expects the execobject.offset and 3262 * reloc.presumed_offset to be an exact match, 3263 * as for using NO_RELOC, then we cannot update 3264 * the execobject.offset until we have completed 3265 * relocation. 3266 */ 3267 args->flags &= ~__EXEC_HAS_RELOC; 3268 goto err_vma; 3269 } 3270 3271 ww_acquire_done(&eb.ww.ctx); 3272 3273 batch = eb.batch->vma; 3274 3275 /* All GPU relocation batches must be submitted prior to the user rq */ 3276 GEM_BUG_ON(eb.reloc_cache.rq); 3277 3278 /* Allocate a request for this batch buffer nice and early. */ 3279 eb.request = i915_request_create(eb.context); 3280 if (IS_ERR(eb.request)) { 3281 err = PTR_ERR(eb.request); 3282 goto err_vma; 3283 } 3284 3285 if (in_fence) { 3286 if (args->flags & I915_EXEC_FENCE_SUBMIT) 3287 err = i915_request_await_execution(eb.request, 3288 in_fence, 3289 eb.engine->bond_execute); 3290 else 3291 err = i915_request_await_dma_fence(eb.request, 3292 in_fence); 3293 if (err < 0) 3294 goto err_request; 3295 } 3296 3297 if (eb.fences) { 3298 err = await_fence_array(&eb); 3299 if (err) 3300 goto err_request; 3301 } 3302 3303 if (out_fence_fd != -1) { 3304 out_fence = sync_file_create(&eb.request->fence); 3305 if (!out_fence) { 3306 err = -ENOMEM; 3307 goto err_request; 3308 } 3309 } 3310 3311 /* 3312 * Whilst this request exists, batch_obj will be on the 3313 * active_list, and so will hold the active reference. Only when this 3314 * request is retired will the the batch_obj be moved onto the 3315 * inactive_list and lose its active reference. Hence we do not need 3316 * to explicitly hold another reference here. 3317 */ 3318 eb.request->batch = batch; 3319 if (eb.batch_pool) 3320 intel_gt_buffer_pool_mark_active(eb.batch_pool, eb.request); 3321 3322 trace_i915_request_queue(eb.request, eb.batch_flags); 3323 err = eb_submit(&eb, batch); 3324 err_request: 3325 i915_request_get(eb.request); 3326 eb_request_add(&eb); 3327 3328 if (eb.fences) 3329 signal_fence_array(&eb); 3330 3331 if (out_fence) { 3332 if (err == 0) { 3333 fd_install(out_fence_fd, out_fence->file); 3334 args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */ 3335 args->rsvd2 |= (u64)out_fence_fd << 32; 3336 out_fence_fd = -1; 3337 } else { 3338 fput(out_fence->file); 3339 } 3340 } 3341 i915_request_put(eb.request); 3342 3343 err_vma: 3344 eb_release_vmas(&eb, true); 3345 if (eb.trampoline) 3346 i915_vma_unpin(eb.trampoline); 3347 WARN_ON(err == -EDEADLK); 3348 i915_gem_ww_ctx_fini(&eb.ww); 3349 3350 if (eb.batch_pool) 3351 intel_gt_buffer_pool_put(eb.batch_pool); 3352 if (eb.reloc_pool) 3353 intel_gt_buffer_pool_put(eb.reloc_pool); 3354 if (eb.reloc_context) 3355 intel_context_put(eb.reloc_context); 3356 err_engine: 3357 eb_put_engine(&eb); 3358 err_context: 3359 i915_gem_context_put(eb.gem_context); 3360 err_destroy: 3361 eb_destroy(&eb); 3362 err_out_fence: 3363 if (out_fence_fd != -1) 3364 put_unused_fd(out_fence_fd); 3365 err_in_fence: 3366 dma_fence_put(in_fence); 3367 err_ext: 3368 put_fence_array(eb.fences, eb.num_fences); 3369 return err; 3370 } 3371 3372 static size_t eb_element_size(void) 3373 { 3374 return sizeof(struct drm_i915_gem_exec_object2) + sizeof(struct eb_vma); 3375 } 3376 3377 static bool check_buffer_count(size_t count) 3378 { 3379 const size_t sz = eb_element_size(); 3380 3381 /* 3382 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup 3383 * array size (see eb_create()). Otherwise, we can accept an array as 3384 * large as can be addressed (though use large arrays at your peril)! 3385 */ 3386 3387 return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1); 3388 } 3389 3390 /* 3391 * Legacy execbuffer just creates an exec2 list from the original exec object 3392 * list array and passes it to the real function. 3393 */ 3394 int 3395 i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data, 3396 struct drm_file *file) 3397 { 3398 struct drm_i915_private *i915 = to_i915(dev); 3399 struct drm_i915_gem_execbuffer *args = data; 3400 struct drm_i915_gem_execbuffer2 exec2; 3401 struct drm_i915_gem_exec_object *exec_list = NULL; 3402 struct drm_i915_gem_exec_object2 *exec2_list = NULL; 3403 const size_t count = args->buffer_count; 3404 unsigned int i; 3405 int err; 3406 3407 if (!check_buffer_count(count)) { 3408 drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count); 3409 return -EINVAL; 3410 } 3411 3412 exec2.buffers_ptr = args->buffers_ptr; 3413 exec2.buffer_count = args->buffer_count; 3414 exec2.batch_start_offset = args->batch_start_offset; 3415 exec2.batch_len = args->batch_len; 3416 exec2.DR1 = args->DR1; 3417 exec2.DR4 = args->DR4; 3418 exec2.num_cliprects = args->num_cliprects; 3419 exec2.cliprects_ptr = args->cliprects_ptr; 3420 exec2.flags = I915_EXEC_RENDER; 3421 i915_execbuffer2_set_context_id(exec2, 0); 3422 3423 err = i915_gem_check_execbuffer(&exec2); 3424 if (err) 3425 return err; 3426 3427 /* Copy in the exec list from userland */ 3428 exec_list = kvmalloc_array(count, sizeof(*exec_list), 3429 __GFP_NOWARN | GFP_KERNEL); 3430 3431 /* Allocate extra slots for use by the command parser */ 3432 exec2_list = kvmalloc_array(count + 2, eb_element_size(), 3433 __GFP_NOWARN | GFP_KERNEL); 3434 if (exec_list == NULL || exec2_list == NULL) { 3435 drm_dbg(&i915->drm, 3436 "Failed to allocate exec list for %d buffers\n", 3437 args->buffer_count); 3438 kvfree(exec_list); 3439 kvfree(exec2_list); 3440 return -ENOMEM; 3441 } 3442 err = copy_from_user(exec_list, 3443 u64_to_user_ptr(args->buffers_ptr), 3444 sizeof(*exec_list) * count); 3445 if (err) { 3446 drm_dbg(&i915->drm, "copy %d exec entries failed %d\n", 3447 args->buffer_count, err); 3448 kvfree(exec_list); 3449 kvfree(exec2_list); 3450 return -EFAULT; 3451 } 3452 3453 for (i = 0; i < args->buffer_count; i++) { 3454 exec2_list[i].handle = exec_list[i].handle; 3455 exec2_list[i].relocation_count = exec_list[i].relocation_count; 3456 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; 3457 exec2_list[i].alignment = exec_list[i].alignment; 3458 exec2_list[i].offset = exec_list[i].offset; 3459 if (INTEL_GEN(to_i915(dev)) < 4) 3460 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; 3461 else 3462 exec2_list[i].flags = 0; 3463 } 3464 3465 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list); 3466 if (exec2.flags & __EXEC_HAS_RELOC) { 3467 struct drm_i915_gem_exec_object __user *user_exec_list = 3468 u64_to_user_ptr(args->buffers_ptr); 3469 3470 /* Copy the new buffer offsets back to the user's exec list. */ 3471 for (i = 0; i < args->buffer_count; i++) { 3472 if (!(exec2_list[i].offset & UPDATE)) 3473 continue; 3474 3475 exec2_list[i].offset = 3476 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); 3477 exec2_list[i].offset &= PIN_OFFSET_MASK; 3478 if (__copy_to_user(&user_exec_list[i].offset, 3479 &exec2_list[i].offset, 3480 sizeof(user_exec_list[i].offset))) 3481 break; 3482 } 3483 } 3484 3485 kvfree(exec_list); 3486 kvfree(exec2_list); 3487 return err; 3488 } 3489 3490 int 3491 i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data, 3492 struct drm_file *file) 3493 { 3494 struct drm_i915_private *i915 = to_i915(dev); 3495 struct drm_i915_gem_execbuffer2 *args = data; 3496 struct drm_i915_gem_exec_object2 *exec2_list; 3497 const size_t count = args->buffer_count; 3498 int err; 3499 3500 if (!check_buffer_count(count)) { 3501 drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count); 3502 return -EINVAL; 3503 } 3504 3505 err = i915_gem_check_execbuffer(args); 3506 if (err) 3507 return err; 3508 3509 /* Allocate extra slots for use by the command parser */ 3510 exec2_list = kvmalloc_array(count + 2, eb_element_size(), 3511 __GFP_NOWARN | GFP_KERNEL); 3512 if (exec2_list == NULL) { 3513 drm_dbg(&i915->drm, "Failed to allocate exec list for %zd buffers\n", 3514 count); 3515 return -ENOMEM; 3516 } 3517 if (copy_from_user(exec2_list, 3518 u64_to_user_ptr(args->buffers_ptr), 3519 sizeof(*exec2_list) * count)) { 3520 drm_dbg(&i915->drm, "copy %zd exec entries failed\n", count); 3521 kvfree(exec2_list); 3522 return -EFAULT; 3523 } 3524 3525 err = i915_gem_do_execbuffer(dev, file, args, exec2_list); 3526 3527 /* 3528 * Now that we have begun execution of the batchbuffer, we ignore 3529 * any new error after this point. Also given that we have already 3530 * updated the associated relocations, we try to write out the current 3531 * object locations irrespective of any error. 3532 */ 3533 if (args->flags & __EXEC_HAS_RELOC) { 3534 struct drm_i915_gem_exec_object2 __user *user_exec_list = 3535 u64_to_user_ptr(args->buffers_ptr); 3536 unsigned int i; 3537 3538 /* Copy the new buffer offsets back to the user's exec list. */ 3539 /* 3540 * Note: count * sizeof(*user_exec_list) does not overflow, 3541 * because we checked 'count' in check_buffer_count(). 3542 * 3543 * And this range already got effectively checked earlier 3544 * when we did the "copy_from_user()" above. 3545 */ 3546 if (!user_write_access_begin(user_exec_list, 3547 count * sizeof(*user_exec_list))) 3548 goto end; 3549 3550 for (i = 0; i < args->buffer_count; i++) { 3551 if (!(exec2_list[i].offset & UPDATE)) 3552 continue; 3553 3554 exec2_list[i].offset = 3555 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); 3556 unsafe_put_user(exec2_list[i].offset, 3557 &user_exec_list[i].offset, 3558 end_user); 3559 } 3560 end_user: 3561 user_write_access_end(); 3562 end:; 3563 } 3564 3565 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS; 3566 kvfree(exec2_list); 3567 return err; 3568 } 3569 3570 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 3571 #include "selftests/i915_gem_execbuffer.c" 3572 #endif 3573