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