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