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