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