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 u64 batch_len; /** Length of batch within object */ 291 u32 batch_start_offset; /** Location within object of batch */ 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 + 4095) >> 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 if (unlikely(eb->batch_len == 0)) { /* impossible! */ 875 drm_dbg(&i915->drm, "Invalid batch length\n"); 876 return -EINVAL; 877 } 878 879 return 0; 880 881 err: 882 eb->vma[i].vma = NULL; 883 return err; 884 } 885 886 static int eb_validate_vmas(struct i915_execbuffer *eb) 887 { 888 unsigned int i; 889 int err; 890 891 INIT_LIST_HEAD(&eb->unbound); 892 893 for (i = 0; i < eb->buffer_count; i++) { 894 struct drm_i915_gem_exec_object2 *entry = &eb->exec[i]; 895 struct eb_vma *ev = &eb->vma[i]; 896 struct i915_vma *vma = ev->vma; 897 898 err = i915_gem_object_lock(vma->obj, &eb->ww); 899 if (err) 900 return err; 901 902 if (eb_pin_vma(eb, entry, ev)) { 903 if (entry->offset != vma->node.start) { 904 entry->offset = vma->node.start | UPDATE; 905 eb->args->flags |= __EXEC_HAS_RELOC; 906 } 907 } else { 908 eb_unreserve_vma(ev); 909 910 list_add_tail(&ev->bind_link, &eb->unbound); 911 if (drm_mm_node_allocated(&vma->node)) { 912 err = i915_vma_unbind(vma); 913 if (err) 914 return err; 915 } 916 } 917 918 GEM_BUG_ON(drm_mm_node_allocated(&vma->node) && 919 eb_vma_misplaced(&eb->exec[i], vma, ev->flags)); 920 } 921 922 if (!list_empty(&eb->unbound)) 923 return eb_reserve(eb); 924 925 return 0; 926 } 927 928 static struct eb_vma * 929 eb_get_vma(const struct i915_execbuffer *eb, unsigned long handle) 930 { 931 if (eb->lut_size < 0) { 932 if (handle >= -eb->lut_size) 933 return NULL; 934 return &eb->vma[handle]; 935 } else { 936 struct hlist_head *head; 937 struct eb_vma *ev; 938 939 head = &eb->buckets[hash_32(handle, eb->lut_size)]; 940 hlist_for_each_entry(ev, head, node) { 941 if (ev->handle == handle) 942 return ev; 943 } 944 return NULL; 945 } 946 } 947 948 static void eb_release_vmas(struct i915_execbuffer *eb, bool final) 949 { 950 const unsigned int count = eb->buffer_count; 951 unsigned int i; 952 953 for (i = 0; i < count; i++) { 954 struct eb_vma *ev = &eb->vma[i]; 955 struct i915_vma *vma = ev->vma; 956 957 if (!vma) 958 break; 959 960 eb_unreserve_vma(ev); 961 962 if (final) 963 i915_vma_put(vma); 964 } 965 966 eb_unpin_engine(eb); 967 } 968 969 static void eb_destroy(const struct i915_execbuffer *eb) 970 { 971 GEM_BUG_ON(eb->reloc_cache.rq); 972 973 if (eb->lut_size > 0) 974 kfree(eb->buckets); 975 } 976 977 static inline u64 978 relocation_target(const struct drm_i915_gem_relocation_entry *reloc, 979 const struct i915_vma *target) 980 { 981 return gen8_canonical_addr((int)reloc->delta + target->node.start); 982 } 983 984 static void reloc_cache_clear(struct reloc_cache *cache) 985 { 986 cache->rq = NULL; 987 cache->rq_cmd = NULL; 988 cache->pool = NULL; 989 cache->rq_size = 0; 990 } 991 992 static void reloc_cache_init(struct reloc_cache *cache, 993 struct drm_i915_private *i915) 994 { 995 cache->page = -1; 996 cache->vaddr = 0; 997 /* Must be a variable in the struct to allow GCC to unroll. */ 998 cache->gen = INTEL_GEN(i915); 999 cache->has_llc = HAS_LLC(i915); 1000 cache->use_64bit_reloc = HAS_64BIT_RELOC(i915); 1001 cache->has_fence = cache->gen < 4; 1002 cache->needs_unfenced = INTEL_INFO(i915)->unfenced_needs_alignment; 1003 cache->node.flags = 0; 1004 reloc_cache_clear(cache); 1005 } 1006 1007 static inline void *unmask_page(unsigned long p) 1008 { 1009 return (void *)(uintptr_t)(p & PAGE_MASK); 1010 } 1011 1012 static inline unsigned int unmask_flags(unsigned long p) 1013 { 1014 return p & ~PAGE_MASK; 1015 } 1016 1017 #define KMAP 0x4 /* after CLFLUSH_FLAGS */ 1018 1019 static inline struct i915_ggtt *cache_to_ggtt(struct reloc_cache *cache) 1020 { 1021 struct drm_i915_private *i915 = 1022 container_of(cache, struct i915_execbuffer, reloc_cache)->i915; 1023 return &i915->ggtt; 1024 } 1025 1026 static void reloc_cache_put_pool(struct i915_execbuffer *eb, struct reloc_cache *cache) 1027 { 1028 if (!cache->pool) 1029 return; 1030 1031 /* 1032 * This is a bit nasty, normally we keep objects locked until the end 1033 * of execbuffer, but we already submit this, and have to unlock before 1034 * dropping the reference. Fortunately we can only hold 1 pool node at 1035 * a time, so this should be harmless. 1036 */ 1037 i915_gem_ww_unlock_single(cache->pool->obj); 1038 intel_gt_buffer_pool_put(cache->pool); 1039 cache->pool = NULL; 1040 } 1041 1042 static void reloc_gpu_flush(struct i915_execbuffer *eb, struct reloc_cache *cache) 1043 { 1044 struct drm_i915_gem_object *obj = cache->rq->batch->obj; 1045 1046 GEM_BUG_ON(cache->rq_size >= obj->base.size / sizeof(u32)); 1047 cache->rq_cmd[cache->rq_size] = MI_BATCH_BUFFER_END; 1048 1049 i915_gem_object_flush_map(obj); 1050 i915_gem_object_unpin_map(obj); 1051 1052 intel_gt_chipset_flush(cache->rq->engine->gt); 1053 1054 i915_request_add(cache->rq); 1055 reloc_cache_put_pool(eb, cache); 1056 reloc_cache_clear(cache); 1057 1058 eb->reloc_pool = NULL; 1059 } 1060 1061 static void reloc_cache_reset(struct reloc_cache *cache, struct i915_execbuffer *eb) 1062 { 1063 void *vaddr; 1064 1065 if (cache->rq) 1066 reloc_gpu_flush(eb, cache); 1067 1068 if (!cache->vaddr) 1069 return; 1070 1071 vaddr = unmask_page(cache->vaddr); 1072 if (cache->vaddr & KMAP) { 1073 struct drm_i915_gem_object *obj = 1074 (struct drm_i915_gem_object *)cache->node.mm; 1075 if (cache->vaddr & CLFLUSH_AFTER) 1076 mb(); 1077 1078 kunmap_atomic(vaddr); 1079 i915_gem_object_finish_access(obj); 1080 } else { 1081 struct i915_ggtt *ggtt = cache_to_ggtt(cache); 1082 1083 intel_gt_flush_ggtt_writes(ggtt->vm.gt); 1084 io_mapping_unmap_atomic((void __iomem *)vaddr); 1085 1086 if (drm_mm_node_allocated(&cache->node)) { 1087 ggtt->vm.clear_range(&ggtt->vm, 1088 cache->node.start, 1089 cache->node.size); 1090 mutex_lock(&ggtt->vm.mutex); 1091 drm_mm_remove_node(&cache->node); 1092 mutex_unlock(&ggtt->vm.mutex); 1093 } else { 1094 i915_vma_unpin((struct i915_vma *)cache->node.mm); 1095 } 1096 } 1097 1098 cache->vaddr = 0; 1099 cache->page = -1; 1100 } 1101 1102 static void *reloc_kmap(struct drm_i915_gem_object *obj, 1103 struct reloc_cache *cache, 1104 unsigned long pageno) 1105 { 1106 void *vaddr; 1107 struct page *page; 1108 1109 if (cache->vaddr) { 1110 kunmap_atomic(unmask_page(cache->vaddr)); 1111 } else { 1112 unsigned int flushes; 1113 int err; 1114 1115 err = i915_gem_object_prepare_write(obj, &flushes); 1116 if (err) 1117 return ERR_PTR(err); 1118 1119 BUILD_BUG_ON(KMAP & CLFLUSH_FLAGS); 1120 BUILD_BUG_ON((KMAP | CLFLUSH_FLAGS) & PAGE_MASK); 1121 1122 cache->vaddr = flushes | KMAP; 1123 cache->node.mm = (void *)obj; 1124 if (flushes) 1125 mb(); 1126 } 1127 1128 page = i915_gem_object_get_page(obj, pageno); 1129 if (!obj->mm.dirty) 1130 set_page_dirty(page); 1131 1132 vaddr = kmap_atomic(page); 1133 cache->vaddr = unmask_flags(cache->vaddr) | (unsigned long)vaddr; 1134 cache->page = pageno; 1135 1136 return vaddr; 1137 } 1138 1139 static void *reloc_iomap(struct drm_i915_gem_object *obj, 1140 struct i915_execbuffer *eb, 1141 unsigned long page) 1142 { 1143 struct reloc_cache *cache = &eb->reloc_cache; 1144 struct i915_ggtt *ggtt = cache_to_ggtt(cache); 1145 unsigned long offset; 1146 void *vaddr; 1147 1148 if (cache->vaddr) { 1149 intel_gt_flush_ggtt_writes(ggtt->vm.gt); 1150 io_mapping_unmap_atomic((void __force __iomem *) unmask_page(cache->vaddr)); 1151 } else { 1152 struct i915_vma *vma; 1153 int err; 1154 1155 if (i915_gem_object_is_tiled(obj)) 1156 return ERR_PTR(-EINVAL); 1157 1158 if (use_cpu_reloc(cache, obj)) 1159 return NULL; 1160 1161 err = i915_gem_object_set_to_gtt_domain(obj, true); 1162 if (err) 1163 return ERR_PTR(err); 1164 1165 vma = i915_gem_object_ggtt_pin_ww(obj, &eb->ww, NULL, 0, 0, 1166 PIN_MAPPABLE | 1167 PIN_NONBLOCK /* NOWARN */ | 1168 PIN_NOEVICT); 1169 if (vma == ERR_PTR(-EDEADLK)) 1170 return vma; 1171 1172 if (IS_ERR(vma)) { 1173 memset(&cache->node, 0, sizeof(cache->node)); 1174 mutex_lock(&ggtt->vm.mutex); 1175 err = drm_mm_insert_node_in_range 1176 (&ggtt->vm.mm, &cache->node, 1177 PAGE_SIZE, 0, I915_COLOR_UNEVICTABLE, 1178 0, ggtt->mappable_end, 1179 DRM_MM_INSERT_LOW); 1180 mutex_unlock(&ggtt->vm.mutex); 1181 if (err) /* no inactive aperture space, use cpu reloc */ 1182 return NULL; 1183 } else { 1184 cache->node.start = vma->node.start; 1185 cache->node.mm = (void *)vma; 1186 } 1187 } 1188 1189 offset = cache->node.start; 1190 if (drm_mm_node_allocated(&cache->node)) { 1191 ggtt->vm.insert_page(&ggtt->vm, 1192 i915_gem_object_get_dma_address(obj, page), 1193 offset, I915_CACHE_NONE, 0); 1194 } else { 1195 offset += page << PAGE_SHIFT; 1196 } 1197 1198 vaddr = (void __force *)io_mapping_map_atomic_wc(&ggtt->iomap, 1199 offset); 1200 cache->page = page; 1201 cache->vaddr = (unsigned long)vaddr; 1202 1203 return vaddr; 1204 } 1205 1206 static void *reloc_vaddr(struct drm_i915_gem_object *obj, 1207 struct i915_execbuffer *eb, 1208 unsigned long page) 1209 { 1210 struct reloc_cache *cache = &eb->reloc_cache; 1211 void *vaddr; 1212 1213 if (cache->page == page) { 1214 vaddr = unmask_page(cache->vaddr); 1215 } else { 1216 vaddr = NULL; 1217 if ((cache->vaddr & KMAP) == 0) 1218 vaddr = reloc_iomap(obj, eb, page); 1219 if (!vaddr) 1220 vaddr = reloc_kmap(obj, cache, page); 1221 } 1222 1223 return vaddr; 1224 } 1225 1226 static void clflush_write32(u32 *addr, u32 value, unsigned int flushes) 1227 { 1228 if (unlikely(flushes & (CLFLUSH_BEFORE | CLFLUSH_AFTER))) { 1229 if (flushes & CLFLUSH_BEFORE) { 1230 clflushopt(addr); 1231 mb(); 1232 } 1233 1234 *addr = value; 1235 1236 /* 1237 * Writes to the same cacheline are serialised by the CPU 1238 * (including clflush). On the write path, we only require 1239 * that it hits memory in an orderly fashion and place 1240 * mb barriers at the start and end of the relocation phase 1241 * to ensure ordering of clflush wrt to the system. 1242 */ 1243 if (flushes & CLFLUSH_AFTER) 1244 clflushopt(addr); 1245 } else 1246 *addr = value; 1247 } 1248 1249 static int reloc_move_to_gpu(struct i915_request *rq, struct i915_vma *vma) 1250 { 1251 struct drm_i915_gem_object *obj = vma->obj; 1252 int err; 1253 1254 assert_vma_held(vma); 1255 1256 if (obj->cache_dirty & ~obj->cache_coherent) 1257 i915_gem_clflush_object(obj, 0); 1258 obj->write_domain = 0; 1259 1260 err = i915_request_await_object(rq, vma->obj, true); 1261 if (err == 0) 1262 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE); 1263 1264 return err; 1265 } 1266 1267 static int __reloc_gpu_alloc(struct i915_execbuffer *eb, 1268 struct intel_engine_cs *engine, 1269 struct i915_vma *vma, 1270 unsigned int len) 1271 { 1272 struct reloc_cache *cache = &eb->reloc_cache; 1273 struct intel_gt_buffer_pool_node *pool = eb->reloc_pool; 1274 struct i915_request *rq; 1275 struct i915_vma *batch; 1276 u32 *cmd; 1277 int err; 1278 1279 if (!pool) { 1280 pool = intel_gt_get_buffer_pool(engine->gt, PAGE_SIZE); 1281 if (IS_ERR(pool)) 1282 return PTR_ERR(pool); 1283 } 1284 eb->reloc_pool = NULL; 1285 1286 err = i915_gem_object_lock(pool->obj, &eb->ww); 1287 if (err) 1288 goto err_pool; 1289 1290 cmd = i915_gem_object_pin_map(pool->obj, 1291 cache->has_llc ? 1292 I915_MAP_FORCE_WB : 1293 I915_MAP_FORCE_WC); 1294 if (IS_ERR(cmd)) { 1295 err = PTR_ERR(cmd); 1296 goto err_pool; 1297 } 1298 1299 memset32(cmd, 0, pool->obj->base.size / sizeof(u32)); 1300 1301 batch = i915_vma_instance(pool->obj, vma->vm, NULL); 1302 if (IS_ERR(batch)) { 1303 err = PTR_ERR(batch); 1304 goto err_unmap; 1305 } 1306 1307 err = i915_vma_pin_ww(batch, &eb->ww, 0, 0, PIN_USER | PIN_NONBLOCK); 1308 if (err) 1309 goto err_unmap; 1310 1311 if (engine == eb->context->engine) { 1312 rq = i915_request_create(eb->context); 1313 } else { 1314 struct intel_context *ce = eb->reloc_context; 1315 1316 if (!ce) { 1317 ce = intel_context_create(engine); 1318 if (IS_ERR(ce)) { 1319 err = PTR_ERR(ce); 1320 goto err_unpin; 1321 } 1322 1323 i915_vm_put(ce->vm); 1324 ce->vm = i915_vm_get(eb->context->vm); 1325 eb->reloc_context = ce; 1326 } 1327 1328 err = intel_context_pin_ww(ce, &eb->ww); 1329 if (err) 1330 goto err_unpin; 1331 1332 rq = i915_request_create(ce); 1333 intel_context_unpin(ce); 1334 } 1335 if (IS_ERR(rq)) { 1336 err = PTR_ERR(rq); 1337 goto err_unpin; 1338 } 1339 1340 err = intel_gt_buffer_pool_mark_active(pool, rq); 1341 if (err) 1342 goto err_request; 1343 1344 err = reloc_move_to_gpu(rq, vma); 1345 if (err) 1346 goto err_request; 1347 1348 err = eb->engine->emit_bb_start(rq, 1349 batch->node.start, PAGE_SIZE, 1350 cache->gen > 5 ? 0 : I915_DISPATCH_SECURE); 1351 if (err) 1352 goto skip_request; 1353 1354 assert_vma_held(batch); 1355 err = i915_request_await_object(rq, batch->obj, false); 1356 if (err == 0) 1357 err = i915_vma_move_to_active(batch, rq, 0); 1358 if (err) 1359 goto skip_request; 1360 1361 rq->batch = batch; 1362 i915_vma_unpin(batch); 1363 1364 cache->rq = rq; 1365 cache->rq_cmd = cmd; 1366 cache->rq_size = 0; 1367 cache->pool = pool; 1368 1369 /* Return with batch mapping (cmd) still pinned */ 1370 return 0; 1371 1372 skip_request: 1373 i915_request_set_error_once(rq, err); 1374 err_request: 1375 i915_request_add(rq); 1376 err_unpin: 1377 i915_vma_unpin(batch); 1378 err_unmap: 1379 i915_gem_object_unpin_map(pool->obj); 1380 err_pool: 1381 eb->reloc_pool = pool; 1382 return err; 1383 } 1384 1385 static bool reloc_can_use_engine(const struct intel_engine_cs *engine) 1386 { 1387 return engine->class != VIDEO_DECODE_CLASS || !IS_GEN(engine->i915, 6); 1388 } 1389 1390 static u32 *reloc_gpu(struct i915_execbuffer *eb, 1391 struct i915_vma *vma, 1392 unsigned int len) 1393 { 1394 struct reloc_cache *cache = &eb->reloc_cache; 1395 u32 *cmd; 1396 1397 if (cache->rq_size > PAGE_SIZE/sizeof(u32) - (len + 1)) 1398 reloc_gpu_flush(eb, cache); 1399 1400 if (unlikely(!cache->rq)) { 1401 int err; 1402 struct intel_engine_cs *engine = eb->engine; 1403 1404 if (!reloc_can_use_engine(engine)) { 1405 engine = engine->gt->engine_class[COPY_ENGINE_CLASS][0]; 1406 if (!engine) 1407 return ERR_PTR(-ENODEV); 1408 } 1409 1410 err = __reloc_gpu_alloc(eb, engine, vma, len); 1411 if (unlikely(err)) 1412 return ERR_PTR(err); 1413 } 1414 1415 cmd = cache->rq_cmd + cache->rq_size; 1416 cache->rq_size += len; 1417 1418 return cmd; 1419 } 1420 1421 static inline bool use_reloc_gpu(struct i915_vma *vma) 1422 { 1423 if (DBG_FORCE_RELOC == FORCE_GPU_RELOC) 1424 return true; 1425 1426 if (DBG_FORCE_RELOC) 1427 return false; 1428 1429 return !dma_resv_test_signaled_rcu(vma->resv, true); 1430 } 1431 1432 static unsigned long vma_phys_addr(struct i915_vma *vma, u32 offset) 1433 { 1434 struct page *page; 1435 unsigned long addr; 1436 1437 GEM_BUG_ON(vma->pages != vma->obj->mm.pages); 1438 1439 page = i915_gem_object_get_page(vma->obj, offset >> PAGE_SHIFT); 1440 addr = PFN_PHYS(page_to_pfn(page)); 1441 GEM_BUG_ON(overflows_type(addr, u32)); /* expected dma32 */ 1442 1443 return addr + offset_in_page(offset); 1444 } 1445 1446 static int __reloc_entry_gpu(struct i915_execbuffer *eb, 1447 struct i915_vma *vma, 1448 u64 offset, 1449 u64 target_addr) 1450 { 1451 const unsigned int gen = eb->reloc_cache.gen; 1452 unsigned int len; 1453 u32 *batch; 1454 u64 addr; 1455 1456 if (gen >= 8) 1457 len = offset & 7 ? 8 : 5; 1458 else if (gen >= 4) 1459 len = 4; 1460 else 1461 len = 3; 1462 1463 batch = reloc_gpu(eb, vma, len); 1464 if (batch == ERR_PTR(-EDEADLK)) 1465 return -EDEADLK; 1466 else if (IS_ERR(batch)) 1467 return false; 1468 1469 addr = gen8_canonical_addr(vma->node.start + offset); 1470 if (gen >= 8) { 1471 if (offset & 7) { 1472 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1473 *batch++ = lower_32_bits(addr); 1474 *batch++ = upper_32_bits(addr); 1475 *batch++ = lower_32_bits(target_addr); 1476 1477 addr = gen8_canonical_addr(addr + 4); 1478 1479 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1480 *batch++ = lower_32_bits(addr); 1481 *batch++ = upper_32_bits(addr); 1482 *batch++ = upper_32_bits(target_addr); 1483 } else { 1484 *batch++ = (MI_STORE_DWORD_IMM_GEN4 | (1 << 21)) + 1; 1485 *batch++ = lower_32_bits(addr); 1486 *batch++ = upper_32_bits(addr); 1487 *batch++ = lower_32_bits(target_addr); 1488 *batch++ = upper_32_bits(target_addr); 1489 } 1490 } else if (gen >= 6) { 1491 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1492 *batch++ = 0; 1493 *batch++ = addr; 1494 *batch++ = target_addr; 1495 } else if (IS_I965G(eb->i915)) { 1496 *batch++ = MI_STORE_DWORD_IMM_GEN4; 1497 *batch++ = 0; 1498 *batch++ = vma_phys_addr(vma, offset); 1499 *batch++ = target_addr; 1500 } else if (gen >= 4) { 1501 *batch++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT; 1502 *batch++ = 0; 1503 *batch++ = addr; 1504 *batch++ = target_addr; 1505 } else if (gen >= 3 && 1506 !(IS_I915G(eb->i915) || IS_I915GM(eb->i915))) { 1507 *batch++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL; 1508 *batch++ = addr; 1509 *batch++ = target_addr; 1510 } else { 1511 *batch++ = MI_STORE_DWORD_IMM; 1512 *batch++ = vma_phys_addr(vma, offset); 1513 *batch++ = target_addr; 1514 } 1515 1516 return true; 1517 } 1518 1519 static int reloc_entry_gpu(struct i915_execbuffer *eb, 1520 struct i915_vma *vma, 1521 u64 offset, 1522 u64 target_addr) 1523 { 1524 if (eb->reloc_cache.vaddr) 1525 return false; 1526 1527 if (!use_reloc_gpu(vma)) 1528 return false; 1529 1530 return __reloc_entry_gpu(eb, vma, offset, target_addr); 1531 } 1532 1533 static u64 1534 relocate_entry(struct i915_vma *vma, 1535 const struct drm_i915_gem_relocation_entry *reloc, 1536 struct i915_execbuffer *eb, 1537 const struct i915_vma *target) 1538 { 1539 u64 target_addr = relocation_target(reloc, target); 1540 u64 offset = reloc->offset; 1541 int reloc_gpu = reloc_entry_gpu(eb, vma, offset, target_addr); 1542 1543 if (reloc_gpu < 0) 1544 return reloc_gpu; 1545 1546 if (!reloc_gpu) { 1547 bool wide = eb->reloc_cache.use_64bit_reloc; 1548 void *vaddr; 1549 1550 repeat: 1551 vaddr = reloc_vaddr(vma->obj, eb, 1552 offset >> PAGE_SHIFT); 1553 if (IS_ERR(vaddr)) 1554 return PTR_ERR(vaddr); 1555 1556 GEM_BUG_ON(!IS_ALIGNED(offset, sizeof(u32))); 1557 clflush_write32(vaddr + offset_in_page(offset), 1558 lower_32_bits(target_addr), 1559 eb->reloc_cache.vaddr); 1560 1561 if (wide) { 1562 offset += sizeof(u32); 1563 target_addr >>= 32; 1564 wide = false; 1565 goto repeat; 1566 } 1567 } 1568 1569 return target->node.start | UPDATE; 1570 } 1571 1572 static u64 1573 eb_relocate_entry(struct i915_execbuffer *eb, 1574 struct eb_vma *ev, 1575 const struct drm_i915_gem_relocation_entry *reloc) 1576 { 1577 struct drm_i915_private *i915 = eb->i915; 1578 struct eb_vma *target; 1579 int err; 1580 1581 /* we've already hold a reference to all valid objects */ 1582 target = eb_get_vma(eb, reloc->target_handle); 1583 if (unlikely(!target)) 1584 return -ENOENT; 1585 1586 /* Validate that the target is in a valid r/w GPU domain */ 1587 if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) { 1588 drm_dbg(&i915->drm, "reloc with multiple write domains: " 1589 "target %d offset %d " 1590 "read %08x write %08x", 1591 reloc->target_handle, 1592 (int) reloc->offset, 1593 reloc->read_domains, 1594 reloc->write_domain); 1595 return -EINVAL; 1596 } 1597 if (unlikely((reloc->write_domain | reloc->read_domains) 1598 & ~I915_GEM_GPU_DOMAINS)) { 1599 drm_dbg(&i915->drm, "reloc with read/write non-GPU domains: " 1600 "target %d offset %d " 1601 "read %08x write %08x", 1602 reloc->target_handle, 1603 (int) reloc->offset, 1604 reloc->read_domains, 1605 reloc->write_domain); 1606 return -EINVAL; 1607 } 1608 1609 if (reloc->write_domain) { 1610 target->flags |= EXEC_OBJECT_WRITE; 1611 1612 /* 1613 * Sandybridge PPGTT errata: We need a global gtt mapping 1614 * for MI and pipe_control writes because the gpu doesn't 1615 * properly redirect them through the ppgtt for non_secure 1616 * batchbuffers. 1617 */ 1618 if (reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION && 1619 IS_GEN(eb->i915, 6)) { 1620 err = i915_vma_bind(target->vma, 1621 target->vma->obj->cache_level, 1622 PIN_GLOBAL, NULL); 1623 if (err) 1624 return err; 1625 } 1626 } 1627 1628 /* 1629 * If the relocation already has the right value in it, no 1630 * more work needs to be done. 1631 */ 1632 if (!DBG_FORCE_RELOC && 1633 gen8_canonical_addr(target->vma->node.start) == reloc->presumed_offset) 1634 return 0; 1635 1636 /* Check that the relocation address is valid... */ 1637 if (unlikely(reloc->offset > 1638 ev->vma->size - (eb->reloc_cache.use_64bit_reloc ? 8 : 4))) { 1639 drm_dbg(&i915->drm, "Relocation beyond object bounds: " 1640 "target %d offset %d size %d.\n", 1641 reloc->target_handle, 1642 (int)reloc->offset, 1643 (int)ev->vma->size); 1644 return -EINVAL; 1645 } 1646 if (unlikely(reloc->offset & 3)) { 1647 drm_dbg(&i915->drm, "Relocation not 4-byte aligned: " 1648 "target %d offset %d.\n", 1649 reloc->target_handle, 1650 (int)reloc->offset); 1651 return -EINVAL; 1652 } 1653 1654 /* 1655 * If we write into the object, we need to force the synchronisation 1656 * barrier, either with an asynchronous clflush or if we executed the 1657 * patching using the GPU (though that should be serialised by the 1658 * timeline). To be completely sure, and since we are required to 1659 * do relocations we are already stalling, disable the user's opt 1660 * out of our synchronisation. 1661 */ 1662 ev->flags &= ~EXEC_OBJECT_ASYNC; 1663 1664 /* and update the user's relocation entry */ 1665 return relocate_entry(ev->vma, reloc, eb, target->vma); 1666 } 1667 1668 static int eb_relocate_vma(struct i915_execbuffer *eb, struct eb_vma *ev) 1669 { 1670 #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry)) 1671 struct drm_i915_gem_relocation_entry stack[N_RELOC(512)]; 1672 const struct drm_i915_gem_exec_object2 *entry = ev->exec; 1673 struct drm_i915_gem_relocation_entry __user *urelocs = 1674 u64_to_user_ptr(entry->relocs_ptr); 1675 unsigned long remain = entry->relocation_count; 1676 1677 if (unlikely(remain > N_RELOC(ULONG_MAX))) 1678 return -EINVAL; 1679 1680 /* 1681 * We must check that the entire relocation array is safe 1682 * to read. However, if the array is not writable the user loses 1683 * the updated relocation values. 1684 */ 1685 if (unlikely(!access_ok(urelocs, remain * sizeof(*urelocs)))) 1686 return -EFAULT; 1687 1688 do { 1689 struct drm_i915_gem_relocation_entry *r = stack; 1690 unsigned int count = 1691 min_t(unsigned long, remain, ARRAY_SIZE(stack)); 1692 unsigned int copied; 1693 1694 /* 1695 * This is the fast path and we cannot handle a pagefault 1696 * whilst holding the struct mutex lest the user pass in the 1697 * relocations contained within a mmaped bo. For in such a case 1698 * we, the page fault handler would call i915_gem_fault() and 1699 * we would try to acquire the struct mutex again. Obviously 1700 * this is bad and so lockdep complains vehemently. 1701 */ 1702 pagefault_disable(); 1703 copied = __copy_from_user_inatomic(r, urelocs, count * sizeof(r[0])); 1704 pagefault_enable(); 1705 if (unlikely(copied)) { 1706 remain = -EFAULT; 1707 goto out; 1708 } 1709 1710 remain -= count; 1711 do { 1712 u64 offset = eb_relocate_entry(eb, ev, r); 1713 1714 if (likely(offset == 0)) { 1715 } else if ((s64)offset < 0) { 1716 remain = (int)offset; 1717 goto out; 1718 } else { 1719 /* 1720 * Note that reporting an error now 1721 * leaves everything in an inconsistent 1722 * state as we have *already* changed 1723 * the relocation value inside the 1724 * object. As we have not changed the 1725 * reloc.presumed_offset or will not 1726 * change the execobject.offset, on the 1727 * call we may not rewrite the value 1728 * inside the object, leaving it 1729 * dangling and causing a GPU hang. Unless 1730 * userspace dynamically rebuilds the 1731 * relocations on each execbuf rather than 1732 * presume a static tree. 1733 * 1734 * We did previously check if the relocations 1735 * were writable (access_ok), an error now 1736 * would be a strange race with mprotect, 1737 * having already demonstrated that we 1738 * can read from this userspace address. 1739 */ 1740 offset = gen8_canonical_addr(offset & ~UPDATE); 1741 __put_user(offset, 1742 &urelocs[r - stack].presumed_offset); 1743 } 1744 } while (r++, --count); 1745 urelocs += ARRAY_SIZE(stack); 1746 } while (remain); 1747 out: 1748 reloc_cache_reset(&eb->reloc_cache, eb); 1749 return remain; 1750 } 1751 1752 static int 1753 eb_relocate_vma_slow(struct i915_execbuffer *eb, struct eb_vma *ev) 1754 { 1755 const struct drm_i915_gem_exec_object2 *entry = ev->exec; 1756 struct drm_i915_gem_relocation_entry *relocs = 1757 u64_to_ptr(typeof(*relocs), entry->relocs_ptr); 1758 unsigned int i; 1759 int err; 1760 1761 for (i = 0; i < entry->relocation_count; i++) { 1762 u64 offset = eb_relocate_entry(eb, ev, &relocs[i]); 1763 1764 if ((s64)offset < 0) { 1765 err = (int)offset; 1766 goto err; 1767 } 1768 } 1769 err = 0; 1770 err: 1771 reloc_cache_reset(&eb->reloc_cache, eb); 1772 return err; 1773 } 1774 1775 static int check_relocations(const struct drm_i915_gem_exec_object2 *entry) 1776 { 1777 const char __user *addr, *end; 1778 unsigned long size; 1779 char __maybe_unused c; 1780 1781 size = entry->relocation_count; 1782 if (size == 0) 1783 return 0; 1784 1785 if (size > N_RELOC(ULONG_MAX)) 1786 return -EINVAL; 1787 1788 addr = u64_to_user_ptr(entry->relocs_ptr); 1789 size *= sizeof(struct drm_i915_gem_relocation_entry); 1790 if (!access_ok(addr, size)) 1791 return -EFAULT; 1792 1793 end = addr + size; 1794 for (; addr < end; addr += PAGE_SIZE) { 1795 int err = __get_user(c, addr); 1796 if (err) 1797 return err; 1798 } 1799 return __get_user(c, end - 1); 1800 } 1801 1802 static int eb_copy_relocations(const struct i915_execbuffer *eb) 1803 { 1804 struct drm_i915_gem_relocation_entry *relocs; 1805 const unsigned int count = eb->buffer_count; 1806 unsigned int i; 1807 int err; 1808 1809 for (i = 0; i < count; i++) { 1810 const unsigned int nreloc = eb->exec[i].relocation_count; 1811 struct drm_i915_gem_relocation_entry __user *urelocs; 1812 unsigned long size; 1813 unsigned long copied; 1814 1815 if (nreloc == 0) 1816 continue; 1817 1818 err = check_relocations(&eb->exec[i]); 1819 if (err) 1820 goto err; 1821 1822 urelocs = u64_to_user_ptr(eb->exec[i].relocs_ptr); 1823 size = nreloc * sizeof(*relocs); 1824 1825 relocs = kvmalloc_array(size, 1, GFP_KERNEL); 1826 if (!relocs) { 1827 err = -ENOMEM; 1828 goto err; 1829 } 1830 1831 /* copy_from_user is limited to < 4GiB */ 1832 copied = 0; 1833 do { 1834 unsigned int len = 1835 min_t(u64, BIT_ULL(31), size - copied); 1836 1837 if (__copy_from_user((char *)relocs + copied, 1838 (char __user *)urelocs + copied, 1839 len)) 1840 goto end; 1841 1842 copied += len; 1843 } while (copied < size); 1844 1845 /* 1846 * As we do not update the known relocation offsets after 1847 * relocating (due to the complexities in lock handling), 1848 * we need to mark them as invalid now so that we force the 1849 * relocation processing next time. Just in case the target 1850 * object is evicted and then rebound into its old 1851 * presumed_offset before the next execbuffer - if that 1852 * happened we would make the mistake of assuming that the 1853 * relocations were valid. 1854 */ 1855 if (!user_access_begin(urelocs, size)) 1856 goto end; 1857 1858 for (copied = 0; copied < nreloc; copied++) 1859 unsafe_put_user(-1, 1860 &urelocs[copied].presumed_offset, 1861 end_user); 1862 user_access_end(); 1863 1864 eb->exec[i].relocs_ptr = (uintptr_t)relocs; 1865 } 1866 1867 return 0; 1868 1869 end_user: 1870 user_access_end(); 1871 end: 1872 kvfree(relocs); 1873 err = -EFAULT; 1874 err: 1875 while (i--) { 1876 relocs = u64_to_ptr(typeof(*relocs), eb->exec[i].relocs_ptr); 1877 if (eb->exec[i].relocation_count) 1878 kvfree(relocs); 1879 } 1880 return err; 1881 } 1882 1883 static int eb_prefault_relocations(const struct i915_execbuffer *eb) 1884 { 1885 const unsigned int count = eb->buffer_count; 1886 unsigned int i; 1887 1888 for (i = 0; i < count; i++) { 1889 int err; 1890 1891 err = check_relocations(&eb->exec[i]); 1892 if (err) 1893 return err; 1894 } 1895 1896 return 0; 1897 } 1898 1899 static noinline int eb_relocate_parse_slow(struct i915_execbuffer *eb, 1900 struct i915_request *rq) 1901 { 1902 bool have_copy = false; 1903 struct eb_vma *ev; 1904 int err = 0; 1905 1906 repeat: 1907 if (signal_pending(current)) { 1908 err = -ERESTARTSYS; 1909 goto out; 1910 } 1911 1912 /* We may process another execbuffer during the unlock... */ 1913 eb_release_vmas(eb, false); 1914 i915_gem_ww_ctx_fini(&eb->ww); 1915 1916 if (rq) { 1917 /* nonblocking is always false */ 1918 if (i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE, 1919 MAX_SCHEDULE_TIMEOUT) < 0) { 1920 i915_request_put(rq); 1921 rq = NULL; 1922 1923 err = -EINTR; 1924 goto err_relock; 1925 } 1926 1927 i915_request_put(rq); 1928 rq = NULL; 1929 } 1930 1931 /* 1932 * We take 3 passes through the slowpatch. 1933 * 1934 * 1 - we try to just prefault all the user relocation entries and 1935 * then attempt to reuse the atomic pagefault disabled fast path again. 1936 * 1937 * 2 - we copy the user entries to a local buffer here outside of the 1938 * local and allow ourselves to wait upon any rendering before 1939 * relocations 1940 * 1941 * 3 - we already have a local copy of the relocation entries, but 1942 * were interrupted (EAGAIN) whilst waiting for the objects, try again. 1943 */ 1944 if (!err) { 1945 err = eb_prefault_relocations(eb); 1946 } else if (!have_copy) { 1947 err = eb_copy_relocations(eb); 1948 have_copy = err == 0; 1949 } else { 1950 cond_resched(); 1951 err = 0; 1952 } 1953 1954 if (!err) 1955 flush_workqueue(eb->i915->mm.userptr_wq); 1956 1957 err_relock: 1958 i915_gem_ww_ctx_init(&eb->ww, true); 1959 if (err) 1960 goto out; 1961 1962 /* reacquire the objects */ 1963 repeat_validate: 1964 rq = eb_pin_engine(eb, false); 1965 if (IS_ERR(rq)) { 1966 err = PTR_ERR(rq); 1967 rq = NULL; 1968 goto err; 1969 } 1970 1971 /* We didn't throttle, should be NULL */ 1972 GEM_WARN_ON(rq); 1973 1974 err = eb_validate_vmas(eb); 1975 if (err) 1976 goto err; 1977 1978 GEM_BUG_ON(!eb->batch); 1979 1980 list_for_each_entry(ev, &eb->relocs, reloc_link) { 1981 if (!have_copy) { 1982 pagefault_disable(); 1983 err = eb_relocate_vma(eb, ev); 1984 pagefault_enable(); 1985 if (err) 1986 break; 1987 } else { 1988 err = eb_relocate_vma_slow(eb, ev); 1989 if (err) 1990 break; 1991 } 1992 } 1993 1994 if (err == -EDEADLK) 1995 goto err; 1996 1997 if (err && !have_copy) 1998 goto repeat; 1999 2000 if (err) 2001 goto err; 2002 2003 /* as last step, parse the command buffer */ 2004 err = eb_parse(eb); 2005 if (err) 2006 goto err; 2007 2008 /* 2009 * Leave the user relocations as are, this is the painfully slow path, 2010 * and we want to avoid the complication of dropping the lock whilst 2011 * having buffers reserved in the aperture and so causing spurious 2012 * ENOSPC for random operations. 2013 */ 2014 2015 err: 2016 if (err == -EDEADLK) { 2017 eb_release_vmas(eb, false); 2018 err = i915_gem_ww_ctx_backoff(&eb->ww); 2019 if (!err) 2020 goto repeat_validate; 2021 } 2022 2023 if (err == -EAGAIN) 2024 goto repeat; 2025 2026 out: 2027 if (have_copy) { 2028 const unsigned int count = eb->buffer_count; 2029 unsigned int i; 2030 2031 for (i = 0; i < count; i++) { 2032 const struct drm_i915_gem_exec_object2 *entry = 2033 &eb->exec[i]; 2034 struct drm_i915_gem_relocation_entry *relocs; 2035 2036 if (!entry->relocation_count) 2037 continue; 2038 2039 relocs = u64_to_ptr(typeof(*relocs), entry->relocs_ptr); 2040 kvfree(relocs); 2041 } 2042 } 2043 2044 if (rq) 2045 i915_request_put(rq); 2046 2047 return err; 2048 } 2049 2050 static int eb_relocate_parse(struct i915_execbuffer *eb) 2051 { 2052 int err; 2053 struct i915_request *rq = NULL; 2054 bool throttle = true; 2055 2056 retry: 2057 rq = eb_pin_engine(eb, throttle); 2058 if (IS_ERR(rq)) { 2059 err = PTR_ERR(rq); 2060 rq = NULL; 2061 if (err != -EDEADLK) 2062 return err; 2063 2064 goto err; 2065 } 2066 2067 if (rq) { 2068 bool nonblock = eb->file->filp->f_flags & O_NONBLOCK; 2069 2070 /* Need to drop all locks now for throttling, take slowpath */ 2071 err = i915_request_wait(rq, I915_WAIT_INTERRUPTIBLE, 0); 2072 if (err == -ETIME) { 2073 if (nonblock) { 2074 err = -EWOULDBLOCK; 2075 i915_request_put(rq); 2076 goto err; 2077 } 2078 goto slow; 2079 } 2080 i915_request_put(rq); 2081 rq = NULL; 2082 } 2083 2084 /* only throttle once, even if we didn't need to throttle */ 2085 throttle = false; 2086 2087 err = eb_validate_vmas(eb); 2088 if (err == -EAGAIN) 2089 goto slow; 2090 else if (err) 2091 goto err; 2092 2093 /* The objects are in their final locations, apply the relocations. */ 2094 if (eb->args->flags & __EXEC_HAS_RELOC) { 2095 struct eb_vma *ev; 2096 2097 list_for_each_entry(ev, &eb->relocs, reloc_link) { 2098 err = eb_relocate_vma(eb, ev); 2099 if (err) 2100 break; 2101 } 2102 2103 if (err == -EDEADLK) 2104 goto err; 2105 else if (err) 2106 goto slow; 2107 } 2108 2109 if (!err) 2110 err = eb_parse(eb); 2111 2112 err: 2113 if (err == -EDEADLK) { 2114 eb_release_vmas(eb, false); 2115 err = i915_gem_ww_ctx_backoff(&eb->ww); 2116 if (!err) 2117 goto retry; 2118 } 2119 2120 return err; 2121 2122 slow: 2123 err = eb_relocate_parse_slow(eb, rq); 2124 if (err) 2125 /* 2126 * If the user expects the execobject.offset and 2127 * reloc.presumed_offset to be an exact match, 2128 * as for using NO_RELOC, then we cannot update 2129 * the execobject.offset until we have completed 2130 * relocation. 2131 */ 2132 eb->args->flags &= ~__EXEC_HAS_RELOC; 2133 2134 return err; 2135 } 2136 2137 static int eb_move_to_gpu(struct i915_execbuffer *eb) 2138 { 2139 const unsigned int count = eb->buffer_count; 2140 unsigned int i = count; 2141 int err = 0; 2142 2143 while (i--) { 2144 struct eb_vma *ev = &eb->vma[i]; 2145 struct i915_vma *vma = ev->vma; 2146 unsigned int flags = ev->flags; 2147 struct drm_i915_gem_object *obj = vma->obj; 2148 2149 assert_vma_held(vma); 2150 2151 if (flags & EXEC_OBJECT_CAPTURE) { 2152 struct i915_capture_list *capture; 2153 2154 capture = kmalloc(sizeof(*capture), GFP_KERNEL); 2155 if (capture) { 2156 capture->next = eb->request->capture_list; 2157 capture->vma = vma; 2158 eb->request->capture_list = capture; 2159 } 2160 } 2161 2162 /* 2163 * If the GPU is not _reading_ through the CPU cache, we need 2164 * to make sure that any writes (both previous GPU writes from 2165 * before a change in snooping levels and normal CPU writes) 2166 * caught in that cache are flushed to main memory. 2167 * 2168 * We want to say 2169 * obj->cache_dirty && 2170 * !(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ) 2171 * but gcc's optimiser doesn't handle that as well and emits 2172 * two jumps instead of one. Maybe one day... 2173 */ 2174 if (unlikely(obj->cache_dirty & ~obj->cache_coherent)) { 2175 if (i915_gem_clflush_object(obj, 0)) 2176 flags &= ~EXEC_OBJECT_ASYNC; 2177 } 2178 2179 if (err == 0 && !(flags & EXEC_OBJECT_ASYNC)) { 2180 err = i915_request_await_object 2181 (eb->request, obj, flags & EXEC_OBJECT_WRITE); 2182 } 2183 2184 if (err == 0) 2185 err = i915_vma_move_to_active(vma, eb->request, flags); 2186 } 2187 2188 if (unlikely(err)) 2189 goto err_skip; 2190 2191 /* Unconditionally flush any chipset caches (for streaming writes). */ 2192 intel_gt_chipset_flush(eb->engine->gt); 2193 return 0; 2194 2195 err_skip: 2196 i915_request_set_error_once(eb->request, err); 2197 return err; 2198 } 2199 2200 static int i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec) 2201 { 2202 if (exec->flags & __I915_EXEC_ILLEGAL_FLAGS) 2203 return -EINVAL; 2204 2205 /* Kernel clipping was a DRI1 misfeature */ 2206 if (!(exec->flags & (I915_EXEC_FENCE_ARRAY | 2207 I915_EXEC_USE_EXTENSIONS))) { 2208 if (exec->num_cliprects || exec->cliprects_ptr) 2209 return -EINVAL; 2210 } 2211 2212 if (exec->DR4 == 0xffffffff) { 2213 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n"); 2214 exec->DR4 = 0; 2215 } 2216 if (exec->DR1 || exec->DR4) 2217 return -EINVAL; 2218 2219 if ((exec->batch_start_offset | exec->batch_len) & 0x7) 2220 return -EINVAL; 2221 2222 return 0; 2223 } 2224 2225 static int i915_reset_gen7_sol_offsets(struct i915_request *rq) 2226 { 2227 u32 *cs; 2228 int i; 2229 2230 if (!IS_GEN(rq->engine->i915, 7) || rq->engine->id != RCS0) { 2231 drm_dbg(&rq->engine->i915->drm, "sol reset is gen7/rcs only\n"); 2232 return -EINVAL; 2233 } 2234 2235 cs = intel_ring_begin(rq, 4 * 2 + 2); 2236 if (IS_ERR(cs)) 2237 return PTR_ERR(cs); 2238 2239 *cs++ = MI_LOAD_REGISTER_IMM(4); 2240 for (i = 0; i < 4; i++) { 2241 *cs++ = i915_mmio_reg_offset(GEN7_SO_WRITE_OFFSET(i)); 2242 *cs++ = 0; 2243 } 2244 *cs++ = MI_NOOP; 2245 intel_ring_advance(rq, cs); 2246 2247 return 0; 2248 } 2249 2250 static struct i915_vma * 2251 shadow_batch_pin(struct i915_execbuffer *eb, 2252 struct drm_i915_gem_object *obj, 2253 struct i915_address_space *vm, 2254 unsigned int flags) 2255 { 2256 struct i915_vma *vma; 2257 int err; 2258 2259 vma = i915_vma_instance(obj, vm, NULL); 2260 if (IS_ERR(vma)) 2261 return vma; 2262 2263 err = i915_vma_pin_ww(vma, &eb->ww, 0, 0, flags); 2264 if (err) 2265 return ERR_PTR(err); 2266 2267 return vma; 2268 } 2269 2270 struct eb_parse_work { 2271 struct dma_fence_work base; 2272 struct intel_engine_cs *engine; 2273 struct i915_vma *batch; 2274 struct i915_vma *shadow; 2275 struct i915_vma *trampoline; 2276 unsigned long batch_offset; 2277 unsigned long batch_length; 2278 }; 2279 2280 static int __eb_parse(struct dma_fence_work *work) 2281 { 2282 struct eb_parse_work *pw = container_of(work, typeof(*pw), base); 2283 2284 return intel_engine_cmd_parser(pw->engine, 2285 pw->batch, 2286 pw->batch_offset, 2287 pw->batch_length, 2288 pw->shadow, 2289 pw->trampoline); 2290 } 2291 2292 static void __eb_parse_release(struct dma_fence_work *work) 2293 { 2294 struct eb_parse_work *pw = container_of(work, typeof(*pw), base); 2295 2296 if (pw->trampoline) 2297 i915_active_release(&pw->trampoline->active); 2298 i915_active_release(&pw->shadow->active); 2299 i915_active_release(&pw->batch->active); 2300 } 2301 2302 static const struct dma_fence_work_ops eb_parse_ops = { 2303 .name = "eb_parse", 2304 .work = __eb_parse, 2305 .release = __eb_parse_release, 2306 }; 2307 2308 static inline int 2309 __parser_mark_active(struct i915_vma *vma, 2310 struct intel_timeline *tl, 2311 struct dma_fence *fence) 2312 { 2313 struct intel_gt_buffer_pool_node *node = vma->private; 2314 2315 return i915_active_ref(&node->active, tl->fence_context, fence); 2316 } 2317 2318 static int 2319 parser_mark_active(struct eb_parse_work *pw, struct intel_timeline *tl) 2320 { 2321 int err; 2322 2323 mutex_lock(&tl->mutex); 2324 2325 err = __parser_mark_active(pw->shadow, tl, &pw->base.dma); 2326 if (err) 2327 goto unlock; 2328 2329 if (pw->trampoline) { 2330 err = __parser_mark_active(pw->trampoline, tl, &pw->base.dma); 2331 if (err) 2332 goto unlock; 2333 } 2334 2335 unlock: 2336 mutex_unlock(&tl->mutex); 2337 return err; 2338 } 2339 2340 static int eb_parse_pipeline(struct i915_execbuffer *eb, 2341 struct i915_vma *shadow, 2342 struct i915_vma *trampoline) 2343 { 2344 struct eb_parse_work *pw; 2345 int err; 2346 2347 GEM_BUG_ON(overflows_type(eb->batch_start_offset, pw->batch_offset)); 2348 GEM_BUG_ON(overflows_type(eb->batch_len, pw->batch_length)); 2349 2350 pw = kzalloc(sizeof(*pw), GFP_KERNEL); 2351 if (!pw) 2352 return -ENOMEM; 2353 2354 err = i915_active_acquire(&eb->batch->vma->active); 2355 if (err) 2356 goto err_free; 2357 2358 err = i915_active_acquire(&shadow->active); 2359 if (err) 2360 goto err_batch; 2361 2362 if (trampoline) { 2363 err = i915_active_acquire(&trampoline->active); 2364 if (err) 2365 goto err_shadow; 2366 } 2367 2368 dma_fence_work_init(&pw->base, &eb_parse_ops); 2369 2370 pw->engine = eb->engine; 2371 pw->batch = eb->batch->vma; 2372 pw->batch_offset = eb->batch_start_offset; 2373 pw->batch_length = eb->batch_len; 2374 pw->shadow = shadow; 2375 pw->trampoline = trampoline; 2376 2377 /* Mark active refs early for this worker, in case we get interrupted */ 2378 err = parser_mark_active(pw, eb->context->timeline); 2379 if (err) 2380 goto err_commit; 2381 2382 err = dma_resv_reserve_shared(pw->batch->resv, 1); 2383 if (err) 2384 goto err_commit; 2385 2386 /* Wait for all writes (and relocs) into the batch to complete */ 2387 err = i915_sw_fence_await_reservation(&pw->base.chain, 2388 pw->batch->resv, NULL, false, 2389 0, I915_FENCE_GFP); 2390 if (err < 0) 2391 goto err_commit; 2392 2393 /* Keep the batch alive and unwritten as we parse */ 2394 dma_resv_add_shared_fence(pw->batch->resv, &pw->base.dma); 2395 2396 /* Force execution to wait for completion of the parser */ 2397 dma_resv_add_excl_fence(shadow->resv, &pw->base.dma); 2398 2399 dma_fence_work_commit_imm(&pw->base); 2400 return 0; 2401 2402 err_commit: 2403 i915_sw_fence_set_error_once(&pw->base.chain, err); 2404 dma_fence_work_commit_imm(&pw->base); 2405 return err; 2406 2407 err_shadow: 2408 i915_active_release(&shadow->active); 2409 err_batch: 2410 i915_active_release(&eb->batch->vma->active); 2411 err_free: 2412 kfree(pw); 2413 return err; 2414 } 2415 2416 static struct i915_vma *eb_dispatch_secure(struct i915_execbuffer *eb, struct i915_vma *vma) 2417 { 2418 /* 2419 * snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure 2420 * batch" bit. Hence we need to pin secure batches into the global gtt. 2421 * hsw should have this fixed, but bdw mucks it up again. */ 2422 if (eb->batch_flags & I915_DISPATCH_SECURE) 2423 return i915_gem_object_ggtt_pin_ww(vma->obj, &eb->ww, NULL, 0, 0, 0); 2424 2425 return NULL; 2426 } 2427 2428 static int eb_parse(struct i915_execbuffer *eb) 2429 { 2430 struct drm_i915_private *i915 = eb->i915; 2431 struct intel_gt_buffer_pool_node *pool = eb->batch_pool; 2432 struct i915_vma *shadow, *trampoline, *batch; 2433 unsigned long len; 2434 int err; 2435 2436 if (!eb_use_cmdparser(eb)) { 2437 batch = eb_dispatch_secure(eb, eb->batch->vma); 2438 if (IS_ERR(batch)) 2439 return PTR_ERR(batch); 2440 2441 goto secure_batch; 2442 } 2443 2444 len = eb->batch_len; 2445 if (!CMDPARSER_USES_GGTT(eb->i915)) { 2446 /* 2447 * ppGTT backed shadow buffers must be mapped RO, to prevent 2448 * post-scan tampering 2449 */ 2450 if (!eb->context->vm->has_read_only) { 2451 drm_dbg(&i915->drm, 2452 "Cannot prevent post-scan tampering without RO capable vm\n"); 2453 return -EINVAL; 2454 } 2455 } else { 2456 len += I915_CMD_PARSER_TRAMPOLINE_SIZE; 2457 } 2458 if (unlikely(len < eb->batch_len)) /* last paranoid check of overflow */ 2459 return -EINVAL; 2460 2461 if (!pool) { 2462 pool = intel_gt_get_buffer_pool(eb->engine->gt, len); 2463 if (IS_ERR(pool)) 2464 return PTR_ERR(pool); 2465 eb->batch_pool = pool; 2466 } 2467 2468 err = i915_gem_object_lock(pool->obj, &eb->ww); 2469 if (err) 2470 goto err; 2471 2472 shadow = shadow_batch_pin(eb, pool->obj, eb->context->vm, PIN_USER); 2473 if (IS_ERR(shadow)) { 2474 err = PTR_ERR(shadow); 2475 goto err; 2476 } 2477 i915_gem_object_set_readonly(shadow->obj); 2478 shadow->private = pool; 2479 2480 trampoline = NULL; 2481 if (CMDPARSER_USES_GGTT(eb->i915)) { 2482 trampoline = shadow; 2483 2484 shadow = shadow_batch_pin(eb, pool->obj, 2485 &eb->engine->gt->ggtt->vm, 2486 PIN_GLOBAL); 2487 if (IS_ERR(shadow)) { 2488 err = PTR_ERR(shadow); 2489 shadow = trampoline; 2490 goto err_shadow; 2491 } 2492 shadow->private = pool; 2493 2494 eb->batch_flags |= I915_DISPATCH_SECURE; 2495 } 2496 2497 batch = eb_dispatch_secure(eb, shadow); 2498 if (IS_ERR(batch)) { 2499 err = PTR_ERR(batch); 2500 goto err_trampoline; 2501 } 2502 2503 err = eb_parse_pipeline(eb, shadow, trampoline); 2504 if (err) 2505 goto err_unpin_batch; 2506 2507 eb->batch = &eb->vma[eb->buffer_count++]; 2508 eb->batch->vma = i915_vma_get(shadow); 2509 eb->batch->flags = __EXEC_OBJECT_HAS_PIN; 2510 2511 eb->trampoline = trampoline; 2512 eb->batch_start_offset = 0; 2513 2514 secure_batch: 2515 if (batch) { 2516 eb->batch = &eb->vma[eb->buffer_count++]; 2517 eb->batch->flags = __EXEC_OBJECT_HAS_PIN; 2518 eb->batch->vma = i915_vma_get(batch); 2519 } 2520 return 0; 2521 2522 err_unpin_batch: 2523 if (batch) 2524 i915_vma_unpin(batch); 2525 err_trampoline: 2526 if (trampoline) 2527 i915_vma_unpin(trampoline); 2528 err_shadow: 2529 i915_vma_unpin(shadow); 2530 err: 2531 return err; 2532 } 2533 2534 static int eb_submit(struct i915_execbuffer *eb, struct i915_vma *batch) 2535 { 2536 int err; 2537 2538 err = eb_move_to_gpu(eb); 2539 if (err) 2540 return err; 2541 2542 if (eb->args->flags & I915_EXEC_GEN7_SOL_RESET) { 2543 err = i915_reset_gen7_sol_offsets(eb->request); 2544 if (err) 2545 return err; 2546 } 2547 2548 /* 2549 * After we completed waiting for other engines (using HW semaphores) 2550 * then we can signal that this request/batch is ready to run. This 2551 * allows us to determine if the batch is still waiting on the GPU 2552 * or actually running by checking the breadcrumb. 2553 */ 2554 if (eb->engine->emit_init_breadcrumb) { 2555 err = eb->engine->emit_init_breadcrumb(eb->request); 2556 if (err) 2557 return err; 2558 } 2559 2560 err = eb->engine->emit_bb_start(eb->request, 2561 batch->node.start + 2562 eb->batch_start_offset, 2563 eb->batch_len, 2564 eb->batch_flags); 2565 if (err) 2566 return err; 2567 2568 if (eb->trampoline) { 2569 GEM_BUG_ON(eb->batch_start_offset); 2570 err = eb->engine->emit_bb_start(eb->request, 2571 eb->trampoline->node.start + 2572 eb->batch_len, 2573 0, 0); 2574 if (err) 2575 return err; 2576 } 2577 2578 if (intel_context_nopreempt(eb->context)) 2579 __set_bit(I915_FENCE_FLAG_NOPREEMPT, &eb->request->fence.flags); 2580 2581 return 0; 2582 } 2583 2584 static int num_vcs_engines(const struct drm_i915_private *i915) 2585 { 2586 return hweight64(VDBOX_MASK(&i915->gt)); 2587 } 2588 2589 /* 2590 * Find one BSD ring to dispatch the corresponding BSD command. 2591 * The engine index is returned. 2592 */ 2593 static unsigned int 2594 gen8_dispatch_bsd_engine(struct drm_i915_private *dev_priv, 2595 struct drm_file *file) 2596 { 2597 struct drm_i915_file_private *file_priv = file->driver_priv; 2598 2599 /* Check whether the file_priv has already selected one ring. */ 2600 if ((int)file_priv->bsd_engine < 0) 2601 file_priv->bsd_engine = 2602 get_random_int() % num_vcs_engines(dev_priv); 2603 2604 return file_priv->bsd_engine; 2605 } 2606 2607 static const enum intel_engine_id user_ring_map[] = { 2608 [I915_EXEC_DEFAULT] = RCS0, 2609 [I915_EXEC_RENDER] = RCS0, 2610 [I915_EXEC_BLT] = BCS0, 2611 [I915_EXEC_BSD] = VCS0, 2612 [I915_EXEC_VEBOX] = VECS0 2613 }; 2614 2615 static struct i915_request *eb_throttle(struct i915_execbuffer *eb, struct intel_context *ce) 2616 { 2617 struct intel_ring *ring = ce->ring; 2618 struct intel_timeline *tl = ce->timeline; 2619 struct i915_request *rq; 2620 2621 /* 2622 * Completely unscientific finger-in-the-air estimates for suitable 2623 * maximum user request size (to avoid blocking) and then backoff. 2624 */ 2625 if (intel_ring_update_space(ring) >= PAGE_SIZE) 2626 return NULL; 2627 2628 /* 2629 * Find a request that after waiting upon, there will be at least half 2630 * the ring available. The hysteresis allows us to compete for the 2631 * shared ring and should mean that we sleep less often prior to 2632 * claiming our resources, but not so long that the ring completely 2633 * drains before we can submit our next request. 2634 */ 2635 list_for_each_entry(rq, &tl->requests, link) { 2636 if (rq->ring != ring) 2637 continue; 2638 2639 if (__intel_ring_space(rq->postfix, 2640 ring->emit, ring->size) > ring->size / 2) 2641 break; 2642 } 2643 if (&rq->link == &tl->requests) 2644 return NULL; /* weird, we will check again later for real */ 2645 2646 return i915_request_get(rq); 2647 } 2648 2649 static struct i915_request *eb_pin_engine(struct i915_execbuffer *eb, bool throttle) 2650 { 2651 struct intel_context *ce = eb->context; 2652 struct intel_timeline *tl; 2653 struct i915_request *rq = NULL; 2654 int err; 2655 2656 GEM_BUG_ON(eb->args->flags & __EXEC_ENGINE_PINNED); 2657 2658 if (unlikely(intel_context_is_banned(ce))) 2659 return ERR_PTR(-EIO); 2660 2661 /* 2662 * Pinning the contexts may generate requests in order to acquire 2663 * GGTT space, so do this first before we reserve a seqno for 2664 * ourselves. 2665 */ 2666 err = intel_context_pin_ww(ce, &eb->ww); 2667 if (err) 2668 return ERR_PTR(err); 2669 2670 /* 2671 * Take a local wakeref for preparing to dispatch the execbuf as 2672 * we expect to access the hardware fairly frequently in the 2673 * process, and require the engine to be kept awake between accesses. 2674 * Upon dispatch, we acquire another prolonged wakeref that we hold 2675 * until the timeline is idle, which in turn releases the wakeref 2676 * taken on the engine, and the parent device. 2677 */ 2678 tl = intel_context_timeline_lock(ce); 2679 if (IS_ERR(tl)) { 2680 intel_context_unpin(ce); 2681 return ERR_CAST(tl); 2682 } 2683 2684 intel_context_enter(ce); 2685 if (throttle) 2686 rq = eb_throttle(eb, ce); 2687 intel_context_timeline_unlock(tl); 2688 2689 eb->args->flags |= __EXEC_ENGINE_PINNED; 2690 return rq; 2691 } 2692 2693 static void eb_unpin_engine(struct i915_execbuffer *eb) 2694 { 2695 struct intel_context *ce = eb->context; 2696 struct intel_timeline *tl = ce->timeline; 2697 2698 if (!(eb->args->flags & __EXEC_ENGINE_PINNED)) 2699 return; 2700 2701 eb->args->flags &= ~__EXEC_ENGINE_PINNED; 2702 2703 mutex_lock(&tl->mutex); 2704 intel_context_exit(ce); 2705 mutex_unlock(&tl->mutex); 2706 2707 intel_context_unpin(ce); 2708 } 2709 2710 static unsigned int 2711 eb_select_legacy_ring(struct i915_execbuffer *eb) 2712 { 2713 struct drm_i915_private *i915 = eb->i915; 2714 struct drm_i915_gem_execbuffer2 *args = eb->args; 2715 unsigned int user_ring_id = args->flags & I915_EXEC_RING_MASK; 2716 2717 if (user_ring_id != I915_EXEC_BSD && 2718 (args->flags & I915_EXEC_BSD_MASK)) { 2719 drm_dbg(&i915->drm, 2720 "execbuf with non bsd ring but with invalid " 2721 "bsd dispatch flags: %d\n", (int)(args->flags)); 2722 return -1; 2723 } 2724 2725 if (user_ring_id == I915_EXEC_BSD && num_vcs_engines(i915) > 1) { 2726 unsigned int bsd_idx = args->flags & I915_EXEC_BSD_MASK; 2727 2728 if (bsd_idx == I915_EXEC_BSD_DEFAULT) { 2729 bsd_idx = gen8_dispatch_bsd_engine(i915, eb->file); 2730 } else if (bsd_idx >= I915_EXEC_BSD_RING1 && 2731 bsd_idx <= I915_EXEC_BSD_RING2) { 2732 bsd_idx >>= I915_EXEC_BSD_SHIFT; 2733 bsd_idx--; 2734 } else { 2735 drm_dbg(&i915->drm, 2736 "execbuf with unknown bsd ring: %u\n", 2737 bsd_idx); 2738 return -1; 2739 } 2740 2741 return _VCS(bsd_idx); 2742 } 2743 2744 if (user_ring_id >= ARRAY_SIZE(user_ring_map)) { 2745 drm_dbg(&i915->drm, "execbuf with unknown ring: %u\n", 2746 user_ring_id); 2747 return -1; 2748 } 2749 2750 return user_ring_map[user_ring_id]; 2751 } 2752 2753 static int 2754 eb_select_engine(struct i915_execbuffer *eb) 2755 { 2756 struct intel_context *ce; 2757 unsigned int idx; 2758 int err; 2759 2760 if (i915_gem_context_user_engines(eb->gem_context)) 2761 idx = eb->args->flags & I915_EXEC_RING_MASK; 2762 else 2763 idx = eb_select_legacy_ring(eb); 2764 2765 ce = i915_gem_context_get_engine(eb->gem_context, idx); 2766 if (IS_ERR(ce)) 2767 return PTR_ERR(ce); 2768 2769 intel_gt_pm_get(ce->engine->gt); 2770 2771 if (!test_bit(CONTEXT_ALLOC_BIT, &ce->flags)) { 2772 err = intel_context_alloc_state(ce); 2773 if (err) 2774 goto err; 2775 } 2776 2777 /* 2778 * ABI: Before userspace accesses the GPU (e.g. execbuffer), report 2779 * EIO if the GPU is already wedged. 2780 */ 2781 err = intel_gt_terminally_wedged(ce->engine->gt); 2782 if (err) 2783 goto err; 2784 2785 eb->context = ce; 2786 eb->engine = ce->engine; 2787 2788 /* 2789 * Make sure engine pool stays alive even if we call intel_context_put 2790 * during ww handling. The pool is destroyed when last pm reference 2791 * is dropped, which breaks our -EDEADLK handling. 2792 */ 2793 return err; 2794 2795 err: 2796 intel_gt_pm_put(ce->engine->gt); 2797 intel_context_put(ce); 2798 return err; 2799 } 2800 2801 static void 2802 eb_put_engine(struct i915_execbuffer *eb) 2803 { 2804 intel_gt_pm_put(eb->engine->gt); 2805 intel_context_put(eb->context); 2806 } 2807 2808 static void 2809 __free_fence_array(struct eb_fence *fences, unsigned int n) 2810 { 2811 while (n--) { 2812 drm_syncobj_put(ptr_mask_bits(fences[n].syncobj, 2)); 2813 dma_fence_put(fences[n].dma_fence); 2814 kfree(fences[n].chain_fence); 2815 } 2816 kvfree(fences); 2817 } 2818 2819 static int 2820 add_timeline_fence_array(struct i915_execbuffer *eb, 2821 const struct drm_i915_gem_execbuffer_ext_timeline_fences *timeline_fences) 2822 { 2823 struct drm_i915_gem_exec_fence __user *user_fences; 2824 u64 __user *user_values; 2825 struct eb_fence *f; 2826 u64 nfences; 2827 int err = 0; 2828 2829 nfences = timeline_fences->fence_count; 2830 if (!nfences) 2831 return 0; 2832 2833 /* Check multiplication overflow for access_ok() and kvmalloc_array() */ 2834 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); 2835 if (nfences > min_t(unsigned long, 2836 ULONG_MAX / sizeof(*user_fences), 2837 SIZE_MAX / sizeof(*f)) - eb->num_fences) 2838 return -EINVAL; 2839 2840 user_fences = u64_to_user_ptr(timeline_fences->handles_ptr); 2841 if (!access_ok(user_fences, nfences * sizeof(*user_fences))) 2842 return -EFAULT; 2843 2844 user_values = u64_to_user_ptr(timeline_fences->values_ptr); 2845 if (!access_ok(user_values, nfences * sizeof(*user_values))) 2846 return -EFAULT; 2847 2848 f = krealloc(eb->fences, 2849 (eb->num_fences + nfences) * sizeof(*f), 2850 __GFP_NOWARN | GFP_KERNEL); 2851 if (!f) 2852 return -ENOMEM; 2853 2854 eb->fences = f; 2855 f += eb->num_fences; 2856 2857 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & 2858 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); 2859 2860 while (nfences--) { 2861 struct drm_i915_gem_exec_fence user_fence; 2862 struct drm_syncobj *syncobj; 2863 struct dma_fence *fence = NULL; 2864 u64 point; 2865 2866 if (__copy_from_user(&user_fence, 2867 user_fences++, 2868 sizeof(user_fence))) 2869 return -EFAULT; 2870 2871 if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) 2872 return -EINVAL; 2873 2874 if (__get_user(point, user_values++)) 2875 return -EFAULT; 2876 2877 syncobj = drm_syncobj_find(eb->file, user_fence.handle); 2878 if (!syncobj) { 2879 DRM_DEBUG("Invalid syncobj handle provided\n"); 2880 return -ENOENT; 2881 } 2882 2883 fence = drm_syncobj_fence_get(syncobj); 2884 2885 if (!fence && user_fence.flags && 2886 !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2887 DRM_DEBUG("Syncobj handle has no fence\n"); 2888 drm_syncobj_put(syncobj); 2889 return -EINVAL; 2890 } 2891 2892 if (fence) 2893 err = dma_fence_chain_find_seqno(&fence, point); 2894 2895 if (err && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2896 DRM_DEBUG("Syncobj handle missing requested point %llu\n", point); 2897 dma_fence_put(fence); 2898 drm_syncobj_put(syncobj); 2899 return err; 2900 } 2901 2902 /* 2903 * A point might have been signaled already and 2904 * garbage collected from the timeline. In this case 2905 * just ignore the point and carry on. 2906 */ 2907 if (!fence && !(user_fence.flags & I915_EXEC_FENCE_SIGNAL)) { 2908 drm_syncobj_put(syncobj); 2909 continue; 2910 } 2911 2912 /* 2913 * For timeline syncobjs we need to preallocate chains for 2914 * later signaling. 2915 */ 2916 if (point != 0 && user_fence.flags & I915_EXEC_FENCE_SIGNAL) { 2917 /* 2918 * Waiting and signaling the same point (when point != 2919 * 0) would break the timeline. 2920 */ 2921 if (user_fence.flags & I915_EXEC_FENCE_WAIT) { 2922 DRM_DEBUG("Trying to wait & signal the same timeline point.\n"); 2923 dma_fence_put(fence); 2924 drm_syncobj_put(syncobj); 2925 return -EINVAL; 2926 } 2927 2928 f->chain_fence = 2929 kmalloc(sizeof(*f->chain_fence), 2930 GFP_KERNEL); 2931 if (!f->chain_fence) { 2932 drm_syncobj_put(syncobj); 2933 dma_fence_put(fence); 2934 return -ENOMEM; 2935 } 2936 } else { 2937 f->chain_fence = NULL; 2938 } 2939 2940 f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); 2941 f->dma_fence = fence; 2942 f->value = point; 2943 f++; 2944 eb->num_fences++; 2945 } 2946 2947 return 0; 2948 } 2949 2950 static int add_fence_array(struct i915_execbuffer *eb) 2951 { 2952 struct drm_i915_gem_execbuffer2 *args = eb->args; 2953 struct drm_i915_gem_exec_fence __user *user; 2954 unsigned long num_fences = args->num_cliprects; 2955 struct eb_fence *f; 2956 2957 if (!(args->flags & I915_EXEC_FENCE_ARRAY)) 2958 return 0; 2959 2960 if (!num_fences) 2961 return 0; 2962 2963 /* Check multiplication overflow for access_ok() and kvmalloc_array() */ 2964 BUILD_BUG_ON(sizeof(size_t) > sizeof(unsigned long)); 2965 if (num_fences > min_t(unsigned long, 2966 ULONG_MAX / sizeof(*user), 2967 SIZE_MAX / sizeof(*f) - eb->num_fences)) 2968 return -EINVAL; 2969 2970 user = u64_to_user_ptr(args->cliprects_ptr); 2971 if (!access_ok(user, num_fences * sizeof(*user))) 2972 return -EFAULT; 2973 2974 f = krealloc(eb->fences, 2975 (eb->num_fences + num_fences) * sizeof(*f), 2976 __GFP_NOWARN | GFP_KERNEL); 2977 if (!f) 2978 return -ENOMEM; 2979 2980 eb->fences = f; 2981 f += eb->num_fences; 2982 while (num_fences--) { 2983 struct drm_i915_gem_exec_fence user_fence; 2984 struct drm_syncobj *syncobj; 2985 struct dma_fence *fence = NULL; 2986 2987 if (__copy_from_user(&user_fence, user++, sizeof(user_fence))) 2988 return -EFAULT; 2989 2990 if (user_fence.flags & __I915_EXEC_FENCE_UNKNOWN_FLAGS) 2991 return -EINVAL; 2992 2993 syncobj = drm_syncobj_find(eb->file, user_fence.handle); 2994 if (!syncobj) { 2995 DRM_DEBUG("Invalid syncobj handle provided\n"); 2996 return -ENOENT; 2997 } 2998 2999 if (user_fence.flags & I915_EXEC_FENCE_WAIT) { 3000 fence = drm_syncobj_fence_get(syncobj); 3001 if (!fence) { 3002 DRM_DEBUG("Syncobj handle has no fence\n"); 3003 drm_syncobj_put(syncobj); 3004 return -EINVAL; 3005 } 3006 } 3007 3008 BUILD_BUG_ON(~(ARCH_KMALLOC_MINALIGN - 1) & 3009 ~__I915_EXEC_FENCE_UNKNOWN_FLAGS); 3010 3011 f->syncobj = ptr_pack_bits(syncobj, user_fence.flags, 2); 3012 f->dma_fence = fence; 3013 f->value = 0; 3014 f->chain_fence = NULL; 3015 f++; 3016 eb->num_fences++; 3017 } 3018 3019 return 0; 3020 } 3021 3022 static void put_fence_array(struct eb_fence *fences, int num_fences) 3023 { 3024 if (fences) 3025 __free_fence_array(fences, num_fences); 3026 } 3027 3028 static int 3029 await_fence_array(struct i915_execbuffer *eb) 3030 { 3031 unsigned int n; 3032 int err; 3033 3034 for (n = 0; n < eb->num_fences; n++) { 3035 struct drm_syncobj *syncobj; 3036 unsigned int flags; 3037 3038 syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); 3039 3040 if (!eb->fences[n].dma_fence) 3041 continue; 3042 3043 err = i915_request_await_dma_fence(eb->request, 3044 eb->fences[n].dma_fence); 3045 if (err < 0) 3046 return err; 3047 } 3048 3049 return 0; 3050 } 3051 3052 static void signal_fence_array(const struct i915_execbuffer *eb) 3053 { 3054 struct dma_fence * const fence = &eb->request->fence; 3055 unsigned int n; 3056 3057 for (n = 0; n < eb->num_fences; n++) { 3058 struct drm_syncobj *syncobj; 3059 unsigned int flags; 3060 3061 syncobj = ptr_unpack_bits(eb->fences[n].syncobj, &flags, 2); 3062 if (!(flags & I915_EXEC_FENCE_SIGNAL)) 3063 continue; 3064 3065 if (eb->fences[n].chain_fence) { 3066 drm_syncobj_add_point(syncobj, 3067 eb->fences[n].chain_fence, 3068 fence, 3069 eb->fences[n].value); 3070 /* 3071 * The chain's ownership is transferred to the 3072 * timeline. 3073 */ 3074 eb->fences[n].chain_fence = NULL; 3075 } else { 3076 drm_syncobj_replace_fence(syncobj, fence); 3077 } 3078 } 3079 } 3080 3081 static int 3082 parse_timeline_fences(struct i915_user_extension __user *ext, void *data) 3083 { 3084 struct i915_execbuffer *eb = data; 3085 struct drm_i915_gem_execbuffer_ext_timeline_fences timeline_fences; 3086 3087 if (copy_from_user(&timeline_fences, ext, sizeof(timeline_fences))) 3088 return -EFAULT; 3089 3090 return add_timeline_fence_array(eb, &timeline_fences); 3091 } 3092 3093 static void retire_requests(struct intel_timeline *tl, struct i915_request *end) 3094 { 3095 struct i915_request *rq, *rn; 3096 3097 list_for_each_entry_safe(rq, rn, &tl->requests, link) 3098 if (rq == end || !i915_request_retire(rq)) 3099 break; 3100 } 3101 3102 static int eb_request_add(struct i915_execbuffer *eb, int err) 3103 { 3104 struct i915_request *rq = eb->request; 3105 struct intel_timeline * const tl = i915_request_timeline(rq); 3106 struct i915_sched_attr attr = {}; 3107 struct i915_request *prev; 3108 3109 lockdep_assert_held(&tl->mutex); 3110 lockdep_unpin_lock(&tl->mutex, rq->cookie); 3111 3112 trace_i915_request_add(rq); 3113 3114 prev = __i915_request_commit(rq); 3115 3116 /* Check that the context wasn't destroyed before submission */ 3117 if (likely(!intel_context_is_closed(eb->context))) { 3118 attr = eb->gem_context->sched; 3119 } else { 3120 /* Serialise with context_close via the add_to_timeline */ 3121 i915_request_set_error_once(rq, -ENOENT); 3122 __i915_request_skip(rq); 3123 err = -ENOENT; /* override any transient errors */ 3124 } 3125 3126 __i915_request_queue(rq, &attr); 3127 3128 /* Try to clean up the client's timeline after submitting the request */ 3129 if (prev) 3130 retire_requests(tl, prev); 3131 3132 mutex_unlock(&tl->mutex); 3133 3134 return err; 3135 } 3136 3137 static const i915_user_extension_fn execbuf_extensions[] = { 3138 [DRM_I915_GEM_EXECBUFFER_EXT_TIMELINE_FENCES] = parse_timeline_fences, 3139 }; 3140 3141 static int 3142 parse_execbuf2_extensions(struct drm_i915_gem_execbuffer2 *args, 3143 struct i915_execbuffer *eb) 3144 { 3145 if (!(args->flags & I915_EXEC_USE_EXTENSIONS)) 3146 return 0; 3147 3148 /* The execbuf2 extension mechanism reuses cliprects_ptr. So we cannot 3149 * have another flag also using it at the same time. 3150 */ 3151 if (eb->args->flags & I915_EXEC_FENCE_ARRAY) 3152 return -EINVAL; 3153 3154 if (args->num_cliprects != 0) 3155 return -EINVAL; 3156 3157 return i915_user_extensions(u64_to_user_ptr(args->cliprects_ptr), 3158 execbuf_extensions, 3159 ARRAY_SIZE(execbuf_extensions), 3160 eb); 3161 } 3162 3163 static int 3164 i915_gem_do_execbuffer(struct drm_device *dev, 3165 struct drm_file *file, 3166 struct drm_i915_gem_execbuffer2 *args, 3167 struct drm_i915_gem_exec_object2 *exec) 3168 { 3169 struct drm_i915_private *i915 = to_i915(dev); 3170 struct i915_execbuffer eb; 3171 struct dma_fence *in_fence = NULL; 3172 struct sync_file *out_fence = NULL; 3173 struct i915_vma *batch; 3174 int out_fence_fd = -1; 3175 int err; 3176 3177 BUILD_BUG_ON(__EXEC_INTERNAL_FLAGS & ~__I915_EXEC_ILLEGAL_FLAGS); 3178 BUILD_BUG_ON(__EXEC_OBJECT_INTERNAL_FLAGS & 3179 ~__EXEC_OBJECT_UNKNOWN_FLAGS); 3180 3181 eb.i915 = i915; 3182 eb.file = file; 3183 eb.args = args; 3184 if (DBG_FORCE_RELOC || !(args->flags & I915_EXEC_NO_RELOC)) 3185 args->flags |= __EXEC_HAS_RELOC; 3186 3187 eb.exec = exec; 3188 eb.vma = (struct eb_vma *)(exec + args->buffer_count + 1); 3189 eb.vma[0].vma = NULL; 3190 eb.reloc_pool = eb.batch_pool = NULL; 3191 eb.reloc_context = NULL; 3192 3193 eb.invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS; 3194 reloc_cache_init(&eb.reloc_cache, eb.i915); 3195 3196 eb.buffer_count = args->buffer_count; 3197 eb.batch_start_offset = args->batch_start_offset; 3198 eb.batch_len = args->batch_len; 3199 eb.trampoline = NULL; 3200 3201 eb.fences = NULL; 3202 eb.num_fences = 0; 3203 3204 eb.batch_flags = 0; 3205 if (args->flags & I915_EXEC_SECURE) { 3206 if (INTEL_GEN(i915) >= 11) 3207 return -ENODEV; 3208 3209 /* Return -EPERM to trigger fallback code on old binaries. */ 3210 if (!HAS_SECURE_BATCHES(i915)) 3211 return -EPERM; 3212 3213 if (!drm_is_current_master(file) || !capable(CAP_SYS_ADMIN)) 3214 return -EPERM; 3215 3216 eb.batch_flags |= I915_DISPATCH_SECURE; 3217 } 3218 if (args->flags & I915_EXEC_IS_PINNED) 3219 eb.batch_flags |= I915_DISPATCH_PINNED; 3220 3221 err = parse_execbuf2_extensions(args, &eb); 3222 if (err) 3223 goto err_ext; 3224 3225 err = add_fence_array(&eb); 3226 if (err) 3227 goto err_ext; 3228 3229 #define IN_FENCES (I915_EXEC_FENCE_IN | I915_EXEC_FENCE_SUBMIT) 3230 if (args->flags & IN_FENCES) { 3231 if ((args->flags & IN_FENCES) == IN_FENCES) 3232 return -EINVAL; 3233 3234 in_fence = sync_file_get_fence(lower_32_bits(args->rsvd2)); 3235 if (!in_fence) { 3236 err = -EINVAL; 3237 goto err_ext; 3238 } 3239 } 3240 #undef IN_FENCES 3241 3242 if (args->flags & I915_EXEC_FENCE_OUT) { 3243 out_fence_fd = get_unused_fd_flags(O_CLOEXEC); 3244 if (out_fence_fd < 0) { 3245 err = out_fence_fd; 3246 goto err_in_fence; 3247 } 3248 } 3249 3250 err = eb_create(&eb); 3251 if (err) 3252 goto err_out_fence; 3253 3254 GEM_BUG_ON(!eb.lut_size); 3255 3256 err = eb_select_context(&eb); 3257 if (unlikely(err)) 3258 goto err_destroy; 3259 3260 err = eb_select_engine(&eb); 3261 if (unlikely(err)) 3262 goto err_context; 3263 3264 err = eb_lookup_vmas(&eb); 3265 if (err) { 3266 eb_release_vmas(&eb, true); 3267 goto err_engine; 3268 } 3269 3270 i915_gem_ww_ctx_init(&eb.ww, true); 3271 3272 err = eb_relocate_parse(&eb); 3273 if (err) { 3274 /* 3275 * If the user expects the execobject.offset and 3276 * reloc.presumed_offset to be an exact match, 3277 * as for using NO_RELOC, then we cannot update 3278 * the execobject.offset until we have completed 3279 * relocation. 3280 */ 3281 args->flags &= ~__EXEC_HAS_RELOC; 3282 goto err_vma; 3283 } 3284 3285 ww_acquire_done(&eb.ww.ctx); 3286 3287 batch = eb.batch->vma; 3288 3289 /* All GPU relocation batches must be submitted prior to the user rq */ 3290 GEM_BUG_ON(eb.reloc_cache.rq); 3291 3292 /* Allocate a request for this batch buffer nice and early. */ 3293 eb.request = i915_request_create(eb.context); 3294 if (IS_ERR(eb.request)) { 3295 err = PTR_ERR(eb.request); 3296 goto err_vma; 3297 } 3298 3299 if (in_fence) { 3300 if (args->flags & I915_EXEC_FENCE_SUBMIT) 3301 err = i915_request_await_execution(eb.request, 3302 in_fence, 3303 eb.engine->bond_execute); 3304 else 3305 err = i915_request_await_dma_fence(eb.request, 3306 in_fence); 3307 if (err < 0) 3308 goto err_request; 3309 } 3310 3311 if (eb.fences) { 3312 err = await_fence_array(&eb); 3313 if (err) 3314 goto err_request; 3315 } 3316 3317 if (out_fence_fd != -1) { 3318 out_fence = sync_file_create(&eb.request->fence); 3319 if (!out_fence) { 3320 err = -ENOMEM; 3321 goto err_request; 3322 } 3323 } 3324 3325 /* 3326 * Whilst this request exists, batch_obj will be on the 3327 * active_list, and so will hold the active reference. Only when this 3328 * request is retired will the the batch_obj be moved onto the 3329 * inactive_list and lose its active reference. Hence we do not need 3330 * to explicitly hold another reference here. 3331 */ 3332 eb.request->batch = batch; 3333 if (eb.batch_pool) 3334 intel_gt_buffer_pool_mark_active(eb.batch_pool, eb.request); 3335 3336 trace_i915_request_queue(eb.request, eb.batch_flags); 3337 err = eb_submit(&eb, batch); 3338 err_request: 3339 i915_request_get(eb.request); 3340 err = eb_request_add(&eb, err); 3341 3342 if (eb.fences) 3343 signal_fence_array(&eb); 3344 3345 if (out_fence) { 3346 if (err == 0) { 3347 fd_install(out_fence_fd, out_fence->file); 3348 args->rsvd2 &= GENMASK_ULL(31, 0); /* keep in-fence */ 3349 args->rsvd2 |= (u64)out_fence_fd << 32; 3350 out_fence_fd = -1; 3351 } else { 3352 fput(out_fence->file); 3353 } 3354 } 3355 i915_request_put(eb.request); 3356 3357 err_vma: 3358 eb_release_vmas(&eb, true); 3359 if (eb.trampoline) 3360 i915_vma_unpin(eb.trampoline); 3361 WARN_ON(err == -EDEADLK); 3362 i915_gem_ww_ctx_fini(&eb.ww); 3363 3364 if (eb.batch_pool) 3365 intel_gt_buffer_pool_put(eb.batch_pool); 3366 if (eb.reloc_pool) 3367 intel_gt_buffer_pool_put(eb.reloc_pool); 3368 if (eb.reloc_context) 3369 intel_context_put(eb.reloc_context); 3370 err_engine: 3371 eb_put_engine(&eb); 3372 err_context: 3373 i915_gem_context_put(eb.gem_context); 3374 err_destroy: 3375 eb_destroy(&eb); 3376 err_out_fence: 3377 if (out_fence_fd != -1) 3378 put_unused_fd(out_fence_fd); 3379 err_in_fence: 3380 dma_fence_put(in_fence); 3381 err_ext: 3382 put_fence_array(eb.fences, eb.num_fences); 3383 return err; 3384 } 3385 3386 static size_t eb_element_size(void) 3387 { 3388 return sizeof(struct drm_i915_gem_exec_object2) + sizeof(struct eb_vma); 3389 } 3390 3391 static bool check_buffer_count(size_t count) 3392 { 3393 const size_t sz = eb_element_size(); 3394 3395 /* 3396 * When using LUT_HANDLE, we impose a limit of INT_MAX for the lookup 3397 * array size (see eb_create()). Otherwise, we can accept an array as 3398 * large as can be addressed (though use large arrays at your peril)! 3399 */ 3400 3401 return !(count < 1 || count > INT_MAX || count > SIZE_MAX / sz - 1); 3402 } 3403 3404 /* 3405 * Legacy execbuffer just creates an exec2 list from the original exec object 3406 * list array and passes it to the real function. 3407 */ 3408 int 3409 i915_gem_execbuffer_ioctl(struct drm_device *dev, void *data, 3410 struct drm_file *file) 3411 { 3412 struct drm_i915_private *i915 = to_i915(dev); 3413 struct drm_i915_gem_execbuffer *args = data; 3414 struct drm_i915_gem_execbuffer2 exec2; 3415 struct drm_i915_gem_exec_object *exec_list = NULL; 3416 struct drm_i915_gem_exec_object2 *exec2_list = NULL; 3417 const size_t count = args->buffer_count; 3418 unsigned int i; 3419 int err; 3420 3421 if (!check_buffer_count(count)) { 3422 drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count); 3423 return -EINVAL; 3424 } 3425 3426 exec2.buffers_ptr = args->buffers_ptr; 3427 exec2.buffer_count = args->buffer_count; 3428 exec2.batch_start_offset = args->batch_start_offset; 3429 exec2.batch_len = args->batch_len; 3430 exec2.DR1 = args->DR1; 3431 exec2.DR4 = args->DR4; 3432 exec2.num_cliprects = args->num_cliprects; 3433 exec2.cliprects_ptr = args->cliprects_ptr; 3434 exec2.flags = I915_EXEC_RENDER; 3435 i915_execbuffer2_set_context_id(exec2, 0); 3436 3437 err = i915_gem_check_execbuffer(&exec2); 3438 if (err) 3439 return err; 3440 3441 /* Copy in the exec list from userland */ 3442 exec_list = kvmalloc_array(count, sizeof(*exec_list), 3443 __GFP_NOWARN | GFP_KERNEL); 3444 3445 /* Allocate extra slots for use by the command parser */ 3446 exec2_list = kvmalloc_array(count + 2, eb_element_size(), 3447 __GFP_NOWARN | GFP_KERNEL); 3448 if (exec_list == NULL || exec2_list == NULL) { 3449 drm_dbg(&i915->drm, 3450 "Failed to allocate exec list for %d buffers\n", 3451 args->buffer_count); 3452 kvfree(exec_list); 3453 kvfree(exec2_list); 3454 return -ENOMEM; 3455 } 3456 err = copy_from_user(exec_list, 3457 u64_to_user_ptr(args->buffers_ptr), 3458 sizeof(*exec_list) * count); 3459 if (err) { 3460 drm_dbg(&i915->drm, "copy %d exec entries failed %d\n", 3461 args->buffer_count, err); 3462 kvfree(exec_list); 3463 kvfree(exec2_list); 3464 return -EFAULT; 3465 } 3466 3467 for (i = 0; i < args->buffer_count; i++) { 3468 exec2_list[i].handle = exec_list[i].handle; 3469 exec2_list[i].relocation_count = exec_list[i].relocation_count; 3470 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; 3471 exec2_list[i].alignment = exec_list[i].alignment; 3472 exec2_list[i].offset = exec_list[i].offset; 3473 if (INTEL_GEN(to_i915(dev)) < 4) 3474 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; 3475 else 3476 exec2_list[i].flags = 0; 3477 } 3478 3479 err = i915_gem_do_execbuffer(dev, file, &exec2, exec2_list); 3480 if (exec2.flags & __EXEC_HAS_RELOC) { 3481 struct drm_i915_gem_exec_object __user *user_exec_list = 3482 u64_to_user_ptr(args->buffers_ptr); 3483 3484 /* Copy the new buffer offsets back to the user's exec list. */ 3485 for (i = 0; i < args->buffer_count; i++) { 3486 if (!(exec2_list[i].offset & UPDATE)) 3487 continue; 3488 3489 exec2_list[i].offset = 3490 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); 3491 exec2_list[i].offset &= PIN_OFFSET_MASK; 3492 if (__copy_to_user(&user_exec_list[i].offset, 3493 &exec2_list[i].offset, 3494 sizeof(user_exec_list[i].offset))) 3495 break; 3496 } 3497 } 3498 3499 kvfree(exec_list); 3500 kvfree(exec2_list); 3501 return err; 3502 } 3503 3504 int 3505 i915_gem_execbuffer2_ioctl(struct drm_device *dev, void *data, 3506 struct drm_file *file) 3507 { 3508 struct drm_i915_private *i915 = to_i915(dev); 3509 struct drm_i915_gem_execbuffer2 *args = data; 3510 struct drm_i915_gem_exec_object2 *exec2_list; 3511 const size_t count = args->buffer_count; 3512 int err; 3513 3514 if (!check_buffer_count(count)) { 3515 drm_dbg(&i915->drm, "execbuf2 with %zd buffers\n", count); 3516 return -EINVAL; 3517 } 3518 3519 err = i915_gem_check_execbuffer(args); 3520 if (err) 3521 return err; 3522 3523 /* Allocate extra slots for use by the command parser */ 3524 exec2_list = kvmalloc_array(count + 2, eb_element_size(), 3525 __GFP_NOWARN | GFP_KERNEL); 3526 if (exec2_list == NULL) { 3527 drm_dbg(&i915->drm, "Failed to allocate exec list for %zd buffers\n", 3528 count); 3529 return -ENOMEM; 3530 } 3531 if (copy_from_user(exec2_list, 3532 u64_to_user_ptr(args->buffers_ptr), 3533 sizeof(*exec2_list) * count)) { 3534 drm_dbg(&i915->drm, "copy %zd exec entries failed\n", count); 3535 kvfree(exec2_list); 3536 return -EFAULT; 3537 } 3538 3539 err = i915_gem_do_execbuffer(dev, file, args, exec2_list); 3540 3541 /* 3542 * Now that we have begun execution of the batchbuffer, we ignore 3543 * any new error after this point. Also given that we have already 3544 * updated the associated relocations, we try to write out the current 3545 * object locations irrespective of any error. 3546 */ 3547 if (args->flags & __EXEC_HAS_RELOC) { 3548 struct drm_i915_gem_exec_object2 __user *user_exec_list = 3549 u64_to_user_ptr(args->buffers_ptr); 3550 unsigned int i; 3551 3552 /* Copy the new buffer offsets back to the user's exec list. */ 3553 /* 3554 * Note: count * sizeof(*user_exec_list) does not overflow, 3555 * because we checked 'count' in check_buffer_count(). 3556 * 3557 * And this range already got effectively checked earlier 3558 * when we did the "copy_from_user()" above. 3559 */ 3560 if (!user_write_access_begin(user_exec_list, 3561 count * sizeof(*user_exec_list))) 3562 goto end; 3563 3564 for (i = 0; i < args->buffer_count; i++) { 3565 if (!(exec2_list[i].offset & UPDATE)) 3566 continue; 3567 3568 exec2_list[i].offset = 3569 gen8_canonical_addr(exec2_list[i].offset & PIN_OFFSET_MASK); 3570 unsafe_put_user(exec2_list[i].offset, 3571 &user_exec_list[i].offset, 3572 end_user); 3573 } 3574 end_user: 3575 user_write_access_end(); 3576 end:; 3577 } 3578 3579 args->flags &= ~__I915_EXEC_UNKNOWN_FLAGS; 3580 kvfree(exec2_list); 3581 return err; 3582 } 3583 3584 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST) 3585 #include "selftests/i915_gem_execbuffer.c" 3586 #endif 3587