Subversion Repositories Kolibri OS

Rev

Rev 6084 | Rev 6935 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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 <drm/drmP.h>
  30. #include <drm/i915_drm.h>
  31. #include "i915_drv.h"
  32. #include "i915_trace.h"
  33. #include "intel_drv.h"
  34. #include <linux/dma_remapping.h>
  35.  
  36. #define  __EXEC_OBJECT_HAS_PIN (1<<31)
  37. #define  __EXEC_OBJECT_HAS_FENCE (1<<30)
  38. #define  __EXEC_OBJECT_NEEDS_MAP (1<<29)
  39. #define  __EXEC_OBJECT_NEEDS_BIAS (1<<28)
  40.  
  41. #define BATCH_OFFSET_BIAS (256*1024)
  42.  
  43. struct eb_vmas {
  44.         struct list_head vmas;
  45.         int and;
  46.         union {
  47.                 struct i915_vma *lut[0];
  48.                 struct hlist_head buckets[0];
  49.         };
  50. };
  51.  
  52. static struct eb_vmas *
  53. eb_create(struct drm_i915_gem_execbuffer2 *args)
  54. {
  55.         struct eb_vmas *eb = NULL;
  56.  
  57.         if (args->flags & I915_EXEC_HANDLE_LUT) {
  58.                 unsigned size = args->buffer_count;
  59.                 size *= sizeof(struct i915_vma *);
  60.                 size += sizeof(struct eb_vmas);
  61.                 eb = kmalloc(size, GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  62.         }
  63.  
  64.         if (eb == NULL) {
  65.                 unsigned size = args->buffer_count;
  66.                 unsigned count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  67.                 BUILD_BUG_ON_NOT_POWER_OF_2(PAGE_SIZE / sizeof(struct hlist_head));
  68.                 while (count > 2*size)
  69.                         count >>= 1;
  70.                 eb = kzalloc(count*sizeof(struct hlist_head) +
  71.                              sizeof(struct eb_vmas),
  72.                              GFP_TEMPORARY);
  73.                 if (eb == NULL)
  74.                         return eb;
  75.  
  76.                 eb->and = count - 1;
  77.         } else
  78.                 eb->and = -args->buffer_count;
  79.  
  80.         INIT_LIST_HEAD(&eb->vmas);
  81.         return eb;
  82. }
  83.  
  84. static void
  85. eb_reset(struct eb_vmas *eb)
  86. {
  87.         if (eb->and >= 0)
  88.                 memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  89. }
  90.  
  91. static int
  92. eb_lookup_vmas(struct eb_vmas *eb,
  93.                struct drm_i915_gem_exec_object2 *exec,
  94.                const struct drm_i915_gem_execbuffer2 *args,
  95.                struct i915_address_space *vm,
  96.                struct drm_file *file)
  97. {
  98.         struct drm_i915_gem_object *obj;
  99.         struct list_head objects;
  100.         int i, ret;
  101.  
  102.         INIT_LIST_HEAD(&objects);
  103.         spin_lock(&file->table_lock);
  104.         /* Grab a reference to the object and release the lock so we can lookup
  105.          * or create the VMA without using GFP_ATOMIC */
  106.         for (i = 0; i < args->buffer_count; i++) {
  107.                 obj = to_intel_bo(idr_find(&file->object_idr, exec[i].handle));
  108.                 if (obj == NULL) {
  109.                         spin_unlock(&file->table_lock);
  110.                         DRM_DEBUG("Invalid object handle %d at index %d\n",
  111.                                    exec[i].handle, i);
  112.                         ret = -ENOENT;
  113.                         goto err;
  114.                 }
  115.  
  116.                 if (!list_empty(&obj->obj_exec_link)) {
  117.                         spin_unlock(&file->table_lock);
  118.                         DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  119.                                    obj, exec[i].handle, i);
  120.                         ret = -EINVAL;
  121.                         goto err;
  122.                 }
  123.  
  124.                 drm_gem_object_reference(&obj->base);
  125.                 list_add_tail(&obj->obj_exec_link, &objects);
  126.         }
  127.         spin_unlock(&file->table_lock);
  128.  
  129.         i = 0;
  130.         while (!list_empty(&objects)) {
  131.                 struct i915_vma *vma;
  132.  
  133.                 obj = list_first_entry(&objects,
  134.                                        struct drm_i915_gem_object,
  135.                                        obj_exec_link);
  136.  
  137.                 /*
  138.                  * NOTE: We can leak any vmas created here when something fails
  139.                  * later on. But that's no issue since vma_unbind can deal with
  140.                  * vmas which are not actually bound. And since only
  141.                  * lookup_or_create exists as an interface to get at the vma
  142.                  * from the (obj, vm) we don't run the risk of creating
  143.                  * duplicated vmas for the same vm.
  144.                  */
  145.                 vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  146.                 if (IS_ERR(vma)) {
  147.                         DRM_DEBUG("Failed to lookup VMA\n");
  148.                         ret = PTR_ERR(vma);
  149.                         goto err;
  150.                 }
  151.  
  152.                 /* Transfer ownership from the objects list to the vmas list. */
  153.                 list_add_tail(&vma->exec_list, &eb->vmas);
  154.                 list_del_init(&obj->obj_exec_link);
  155.  
  156.                 vma->exec_entry = &exec[i];
  157.                 if (eb->and < 0) {
  158.                         eb->lut[i] = vma;
  159.                 } else {
  160.                         uint32_t handle = args->flags & I915_EXEC_HANDLE_LUT ? i : exec[i].handle;
  161.                         vma->exec_handle = handle;
  162.                         hlist_add_head(&vma->exec_node,
  163.                                        &eb->buckets[handle & eb->and]);
  164.                 }
  165.                 ++i;
  166.         }
  167.  
  168.         return 0;
  169.  
  170.  
  171. err:
  172.         while (!list_empty(&objects)) {
  173.                 obj = list_first_entry(&objects,
  174.                                        struct drm_i915_gem_object,
  175.                                        obj_exec_link);
  176.                 list_del_init(&obj->obj_exec_link);
  177.                 drm_gem_object_unreference(&obj->base);
  178.         }
  179.         /*
  180.          * Objects already transfered to the vmas list will be unreferenced by
  181.          * eb_destroy.
  182.          */
  183.  
  184.         return ret;
  185. }
  186.  
  187. static struct i915_vma *eb_get_vma(struct eb_vmas *eb, unsigned long handle)
  188. {
  189.         if (eb->and < 0) {
  190.                 if (handle >= -eb->and)
  191.                         return NULL;
  192.                 return eb->lut[handle];
  193.         } else {
  194.                 struct hlist_head *head;
  195.                 struct hlist_node *node;
  196.  
  197.                 head = &eb->buckets[handle & eb->and];
  198.                 hlist_for_each(node, head) {
  199.                         struct i915_vma *vma;
  200.  
  201.                         vma = hlist_entry(node, struct i915_vma, exec_node);
  202.                         if (vma->exec_handle == handle)
  203.                                 return vma;
  204.                 }
  205.                 return NULL;
  206.         }
  207. }
  208.  
  209. static void
  210. i915_gem_execbuffer_unreserve_vma(struct i915_vma *vma)
  211. {
  212.         struct drm_i915_gem_exec_object2 *entry;
  213.         struct drm_i915_gem_object *obj = vma->obj;
  214.  
  215.         if (!drm_mm_node_allocated(&vma->node))
  216.                 return;
  217.  
  218.         entry = vma->exec_entry;
  219.  
  220.         if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  221.                 i915_gem_object_unpin_fence(obj);
  222.  
  223.         if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  224.                 vma->pin_count--;
  225.  
  226.         entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  227. }
  228.  
  229. static void eb_destroy(struct eb_vmas *eb)
  230. {
  231.         while (!list_empty(&eb->vmas)) {
  232.                 struct i915_vma *vma;
  233.  
  234.                 vma = list_first_entry(&eb->vmas,
  235.                                        struct i915_vma,
  236.                                        exec_list);
  237.                 list_del_init(&vma->exec_list);
  238.                 i915_gem_execbuffer_unreserve_vma(vma);
  239.                 drm_gem_object_unreference(&vma->obj->base);
  240.         }
  241.         kfree(eb);
  242. }
  243.  
  244. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  245. {
  246.         return (HAS_LLC(obj->base.dev) ||
  247.                 obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  248.                 obj->cache_level != I915_CACHE_NONE);
  249. }
  250.  
  251. static int
  252. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  253.                    struct drm_i915_gem_relocation_entry *reloc,
  254.                    uint64_t target_offset)
  255. {
  256.         struct drm_device *dev = obj->base.dev;
  257.         uint32_t page_offset = offset_in_page(reloc->offset);
  258.         uint64_t delta = reloc->delta + target_offset;
  259.         char *vaddr;
  260.         int ret;
  261.  
  262.         ret = i915_gem_object_set_to_cpu_domain(obj, true);
  263.         if (ret)
  264.                 return ret;
  265.  
  266.         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  267.                                 reloc->offset >> PAGE_SHIFT));
  268.         *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
  269.  
  270.         if (INTEL_INFO(dev)->gen >= 8) {
  271.                 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  272.  
  273.                 if (page_offset == 0) {
  274.                         kunmap_atomic(vaddr);
  275.                         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  276.                             (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  277.                 }
  278.  
  279.                 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
  280.         }
  281.  
  282.         kunmap_atomic(vaddr);
  283.  
  284.         return 0;
  285. }
  286.  
  287. static int
  288. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  289.                    struct drm_i915_gem_relocation_entry *reloc,
  290.                    uint64_t target_offset)
  291. {
  292.         struct drm_device *dev = obj->base.dev;
  293.         struct drm_i915_private *dev_priv = dev->dev_private;
  294.         uint64_t delta = reloc->delta + target_offset;
  295.         uint64_t offset;
  296.         void __iomem *reloc_page;
  297.         int ret;
  298.  
  299.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  300.         if (ret)
  301.                 return ret;
  302.  
  303.         ret = i915_gem_object_put_fence(obj);
  304.         if (ret)
  305.                 return ret;
  306.  
  307.         /* Map the page containing the relocation we're going to perform.  */
  308.         offset = i915_gem_obj_ggtt_offset(obj);
  309.         offset += reloc->offset;
  310.     MapPage(dev_priv->gtt.mappable,dev_priv->gtt.mappable_base +
  311.                                  (offset & PAGE_MASK), PG_SW);
  312.         reloc_page = dev_priv->gtt.mappable;
  313.         iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
  314.  
  315. //      io_mapping_unmap_atomic(reloc_page);
  316.  
  317.         return 0;
  318. }
  319.  
  320. static void
  321. clflush_write32(void *addr, uint32_t value)
  322. {
  323.         /* This is not a fast path, so KISS. */
  324.         drm_clflush_virt_range(addr, sizeof(uint32_t));
  325.         *(uint32_t *)addr = value;
  326.         drm_clflush_virt_range(addr, sizeof(uint32_t));
  327. }
  328.  
  329. static int
  330. relocate_entry_clflush(struct drm_i915_gem_object *obj,
  331.                        struct drm_i915_gem_relocation_entry *reloc,
  332.                        uint64_t target_offset)
  333. {
  334.         struct drm_device *dev = obj->base.dev;
  335.         uint32_t page_offset = offset_in_page(reloc->offset);
  336.         uint64_t delta = (int)reloc->delta + target_offset;
  337.         char *vaddr;
  338.         int ret;
  339.  
  340.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  341.         if (ret)
  342.                 return ret;
  343.  
  344.         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  345.                                 reloc->offset >> PAGE_SHIFT));
  346.         clflush_write32(vaddr + page_offset, lower_32_bits(delta));
  347.  
  348.         if (INTEL_INFO(dev)->gen >= 8) {
  349.                 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  350.  
  351.                 if (page_offset == 0) {
  352.                         kunmap_atomic(vaddr);
  353.                         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  354.                             (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  355.                 }
  356.  
  357.                 clflush_write32(vaddr + page_offset, upper_32_bits(delta));
  358.         }
  359.  
  360.         kunmap_atomic(vaddr);
  361.  
  362.         return 0;
  363. }
  364.  
  365. static int
  366. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  367.                                    struct eb_vmas *eb,
  368.                                    struct drm_i915_gem_relocation_entry *reloc)
  369. {
  370.         struct drm_device *dev = obj->base.dev;
  371.         struct drm_gem_object *target_obj;
  372.         struct drm_i915_gem_object *target_i915_obj;
  373.         struct i915_vma *target_vma;
  374.         uint64_t target_offset;
  375.         int ret;
  376.  
  377.         /* we've already hold a reference to all valid objects */
  378.         target_vma = eb_get_vma(eb, reloc->target_handle);
  379.         if (unlikely(target_vma == NULL))
  380.                 return -ENOENT;
  381.         target_i915_obj = target_vma->obj;
  382.         target_obj = &target_vma->obj->base;
  383.  
  384.         target_offset = target_vma->node.start;
  385.  
  386.         /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  387.          * pipe_control writes because the gpu doesn't properly redirect them
  388.          * through the ppgtt for non_secure batchbuffers. */
  389.         if (unlikely(IS_GEN6(dev) &&
  390.             reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION)) {
  391.                 ret = i915_vma_bind(target_vma, target_i915_obj->cache_level,
  392.                                     PIN_GLOBAL);
  393.                 if (WARN_ONCE(ret, "Unexpected failure to bind target VMA!"))
  394.                         return ret;
  395.         }
  396.  
  397.         /* Validate that the target is in a valid r/w GPU domain */
  398.         if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  399.                 DRM_DEBUG("reloc with multiple write domains: "
  400.                           "obj %p target %d offset %d "
  401.                           "read %08x write %08x",
  402.                           obj, reloc->target_handle,
  403.                           (int) reloc->offset,
  404.                           reloc->read_domains,
  405.                           reloc->write_domain);
  406.                 return -EINVAL;
  407.         }
  408.         if (unlikely((reloc->write_domain | reloc->read_domains)
  409.                      & ~I915_GEM_GPU_DOMAINS)) {
  410.                 DRM_DEBUG("reloc with read/write non-GPU domains: "
  411.                           "obj %p target %d offset %d "
  412.                           "read %08x write %08x",
  413.                           obj, reloc->target_handle,
  414.                           (int) reloc->offset,
  415.                           reloc->read_domains,
  416.                           reloc->write_domain);
  417.                 return -EINVAL;
  418.         }
  419.  
  420.         target_obj->pending_read_domains |= reloc->read_domains;
  421.         target_obj->pending_write_domain |= reloc->write_domain;
  422.  
  423.         /* If the relocation already has the right value in it, no
  424.          * more work needs to be done.
  425.          */
  426.         if (target_offset == reloc->presumed_offset)
  427.                 return 0;
  428.  
  429.         /* Check that the relocation address is valid... */
  430.         if (unlikely(reloc->offset >
  431.                 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  432.                 DRM_DEBUG("Relocation beyond object bounds: "
  433.                           "obj %p target %d offset %d size %d.\n",
  434.                           obj, reloc->target_handle,
  435.                           (int) reloc->offset,
  436.                           (int) obj->base.size);
  437.                 return -EINVAL;
  438.         }
  439.         if (unlikely(reloc->offset & 3)) {
  440.                 DRM_DEBUG("Relocation not 4-byte aligned: "
  441.                           "obj %p target %d offset %d.\n",
  442.                           obj, reloc->target_handle,
  443.                           (int) reloc->offset);
  444.                 return -EINVAL;
  445.         }
  446.  
  447.         /* We can't wait for rendering with pagefaults disabled */
  448.  
  449.         if (use_cpu_reloc(obj))
  450.                 ret = relocate_entry_cpu(obj, reloc, target_offset);
  451.         else if (obj->map_and_fenceable)
  452.                 ret = relocate_entry_gtt(obj, reloc, target_offset);
  453.     else if (1)
  454.                 ret = relocate_entry_clflush(obj, reloc, target_offset);
  455.         else {
  456.                 WARN_ONCE(1, "Impossible case in relocation handling\n");
  457.                 ret = -ENODEV;
  458.         }
  459.  
  460.         if (ret)
  461.                 return ret;
  462.  
  463.         /* and update the user's relocation entry */
  464.         reloc->presumed_offset = target_offset;
  465.  
  466.         return 0;
  467. }
  468.  
  469. static int
  470. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  471.                                  struct eb_vmas *eb)
  472. {
  473. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  474.         struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(64)];
  475.         struct drm_i915_gem_relocation_entry __user *user_relocs;
  476.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  477.         int remain, ret;
  478.  
  479.         user_relocs = to_user_ptr(entry->relocs_ptr);
  480.  
  481.         remain = entry->relocation_count;
  482.         while (remain) {
  483.                 struct drm_i915_gem_relocation_entry *r = stack_reloc;
  484.                 int count = remain;
  485.                 if (count > ARRAY_SIZE(stack_reloc))
  486.                         count = ARRAY_SIZE(stack_reloc);
  487.                 remain -= count;
  488.  
  489.         memcpy(r, user_relocs, count*sizeof(r[0]));
  490.  
  491.                 do {
  492.                         u64 offset = r->presumed_offset;
  493.  
  494.                         ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
  495.                         if (ret)
  496.                                 return ret;
  497.  
  498.                 if (r->presumed_offset != offset)
  499.                 {
  500.             memcpy(&user_relocs->presumed_offset,
  501.                    &r->presumed_offset,
  502.                    sizeof(r->presumed_offset));
  503.                 }
  504.  
  505.                         user_relocs++;
  506.                         r++;
  507.                 } while (--count);
  508.         }
  509.  
  510.         return 0;
  511. #undef N_RELOC
  512. }
  513.  
  514. static int
  515. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  516.                                       struct eb_vmas *eb,
  517.                                       struct drm_i915_gem_relocation_entry *relocs)
  518. {
  519.         const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  520.         int i, ret;
  521.  
  522.         for (i = 0; i < entry->relocation_count; i++) {
  523.                 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
  524.                 if (ret)
  525.                         return ret;
  526.         }
  527.  
  528.         return 0;
  529. }
  530.  
  531. static int
  532. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  533. {
  534.         struct i915_vma *vma;
  535.         int ret = 0;
  536.  
  537.         /* This is the fast path and we cannot handle a pagefault whilst
  538.          * holding the struct mutex lest the user pass in the relocations
  539.          * contained within a mmaped bo. For in such a case we, the page
  540.          * fault handler would call i915_gem_fault() and we would try to
  541.          * acquire the struct mutex again. Obviously this is bad and so
  542.          * lockdep complains vehemently.
  543.          */
  544. //      pagefault_disable();
  545.         list_for_each_entry(vma, &eb->vmas, exec_list) {
  546.                 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  547.                 if (ret)
  548.                         break;
  549.         }
  550. //   pagefault_enable();
  551.  
  552.         return ret;
  553. }
  554.  
  555. static bool only_mappable_for_reloc(unsigned int flags)
  556. {
  557.         return (flags & (EXEC_OBJECT_NEEDS_FENCE | __EXEC_OBJECT_NEEDS_MAP)) ==
  558.                 __EXEC_OBJECT_NEEDS_MAP;
  559. }
  560.  
  561. static int
  562. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  563.                                 struct intel_engine_cs *ring,
  564.                                 bool *need_reloc)
  565. {
  566.         struct drm_i915_gem_object *obj = vma->obj;
  567.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  568.         uint64_t flags;
  569.         int ret;
  570.  
  571.         flags = PIN_USER;
  572.         if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  573.                 flags |= PIN_GLOBAL;
  574.  
  575.         if (!drm_mm_node_allocated(&vma->node)) {
  576.                 /* Wa32bitGeneralStateOffset & Wa32bitInstructionBaseOffset,
  577.                  * limit address to the first 4GBs for unflagged objects.
  578.                  */
  579.                 if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0)
  580.                         flags |= PIN_ZONE_4G;
  581.                 if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  582.                         flags |= PIN_GLOBAL | PIN_MAPPABLE;
  583.                 if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  584.                         flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  585.                 if ((flags & PIN_MAPPABLE) == 0)
  586.                         flags |= PIN_HIGH;
  587.         }
  588.  
  589.         ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
  590.         if ((ret == -ENOSPC  || ret == -E2BIG) &&
  591.             only_mappable_for_reloc(entry->flags))
  592.                 ret = i915_gem_object_pin(obj, vma->vm,
  593.                                           entry->alignment,
  594.                                           flags & ~PIN_MAPPABLE);
  595.         if (ret)
  596.                 return ret;
  597.  
  598.         entry->flags |= __EXEC_OBJECT_HAS_PIN;
  599.  
  600.         if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  601.                 ret = i915_gem_object_get_fence(obj);
  602.                 if (ret)
  603.                         return ret;
  604.  
  605.                 if (i915_gem_object_pin_fence(obj))
  606.                         entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  607.         }
  608.  
  609.         if (entry->offset != vma->node.start) {
  610.                 entry->offset = vma->node.start;
  611.                 *need_reloc = true;
  612.         }
  613.  
  614.         if (entry->flags & EXEC_OBJECT_WRITE) {
  615.                 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  616.                 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  617.         }
  618.  
  619.         return 0;
  620. }
  621.  
  622. static bool
  623. need_reloc_mappable(struct i915_vma *vma)
  624. {
  625.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  626.  
  627.         if (entry->relocation_count == 0)
  628.                 return false;
  629.  
  630.         if (!i915_is_ggtt(vma->vm))
  631.                 return false;
  632.  
  633.         /* See also use_cpu_reloc() */
  634.         if (HAS_LLC(vma->obj->base.dev))
  635.                 return false;
  636.  
  637.         if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  638.                 return false;
  639.  
  640.         return true;
  641. }
  642.  
  643. static bool
  644. eb_vma_misplaced(struct i915_vma *vma)
  645. {
  646.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  647.         struct drm_i915_gem_object *obj = vma->obj;
  648.  
  649.         WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  650.                !i915_is_ggtt(vma->vm));
  651.  
  652.         if (entry->alignment &&
  653.             vma->node.start & (entry->alignment - 1))
  654.                 return true;
  655.  
  656.         if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  657.             vma->node.start < BATCH_OFFSET_BIAS)
  658.                 return true;
  659.  
  660.         /* avoid costly ping-pong once a batch bo ended up non-mappable */
  661.         if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
  662.                 return !only_mappable_for_reloc(entry->flags);
  663.  
  664.         if ((entry->flags & EXEC_OBJECT_SUPPORTS_48B_ADDRESS) == 0 &&
  665.             (vma->node.start + vma->node.size - 1) >> 32)
  666.                 return true;
  667.  
  668.         return false;
  669. }
  670.  
  671. static int
  672. i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
  673.                             struct list_head *vmas,
  674.                             struct intel_context *ctx,
  675.                             bool *need_relocs)
  676. {
  677.         struct drm_i915_gem_object *obj;
  678.         struct i915_vma *vma;
  679.         struct i915_address_space *vm;
  680.         struct list_head ordered_vmas;
  681.         bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  682.         int retry;
  683.  
  684.         i915_gem_retire_requests_ring(ring);
  685.  
  686.         vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  687.  
  688.         INIT_LIST_HEAD(&ordered_vmas);
  689.         while (!list_empty(vmas)) {
  690.                 struct drm_i915_gem_exec_object2 *entry;
  691.                 bool need_fence, need_mappable;
  692.  
  693.                 vma = list_first_entry(vmas, struct i915_vma, exec_list);
  694.                 obj = vma->obj;
  695.                 entry = vma->exec_entry;
  696.  
  697.                 if (ctx->flags & CONTEXT_NO_ZEROMAP)
  698.                         entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  699.  
  700.                 if (!has_fenced_gpu_access)
  701.                         entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  702.                 need_fence =
  703.                         entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  704.                         obj->tiling_mode != I915_TILING_NONE;
  705.                 need_mappable = need_fence || need_reloc_mappable(vma);
  706.  
  707.                 if (need_mappable) {
  708.                         entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  709.                         list_move(&vma->exec_list, &ordered_vmas);
  710.                 } else
  711.                         list_move_tail(&vma->exec_list, &ordered_vmas);
  712.  
  713.                 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  714.                 obj->base.pending_write_domain = 0;
  715.         }
  716.         list_splice(&ordered_vmas, vmas);
  717.  
  718.         /* Attempt to pin all of the buffers into the GTT.
  719.          * This is done in 3 phases:
  720.          *
  721.          * 1a. Unbind all objects that do not match the GTT constraints for
  722.          *     the execbuffer (fenceable, mappable, alignment etc).
  723.          * 1b. Increment pin count for already bound objects.
  724.          * 2.  Bind new objects.
  725.          * 3.  Decrement pin count.
  726.          *
  727.          * This avoid unnecessary unbinding of later objects in order to make
  728.          * room for the earlier objects *unless* we need to defragment.
  729.          */
  730.         retry = 0;
  731.         do {
  732.                 int ret = 0;
  733.  
  734.                 /* Unbind any ill-fitting objects or pin. */
  735.                 list_for_each_entry(vma, vmas, exec_list) {
  736.                         if (!drm_mm_node_allocated(&vma->node))
  737.                                 continue;
  738.  
  739.                         if (eb_vma_misplaced(vma))
  740.                                 ret = i915_vma_unbind(vma);
  741.                         else
  742.                                 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  743.                         if (ret)
  744.                                 goto err;
  745.                 }
  746.  
  747.                 /* Bind fresh objects */
  748.                 list_for_each_entry(vma, vmas, exec_list) {
  749.                         if (drm_mm_node_allocated(&vma->node))
  750.                                 continue;
  751.  
  752.                         ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  753.                         if (ret)
  754.                                 goto err;
  755.                 }
  756.  
  757. err:
  758.                 if (ret != -ENOSPC || retry++)
  759.                         return ret;
  760.  
  761.                 /* Decrement pin count for bound objects */
  762.                 list_for_each_entry(vma, vmas, exec_list)
  763.                         i915_gem_execbuffer_unreserve_vma(vma);
  764.  
  765.                 ret = i915_gem_evict_vm(vm, true);
  766.                 if (ret)
  767.                         return ret;
  768.         } while (1);
  769. }
  770.  
  771. static int
  772. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  773.                                   struct drm_i915_gem_execbuffer2 *args,
  774.                                   struct drm_file *file,
  775.                                   struct intel_engine_cs *ring,
  776.                                   struct eb_vmas *eb,
  777.                                   struct drm_i915_gem_exec_object2 *exec,
  778.                                   struct intel_context *ctx)
  779. {
  780.         struct drm_i915_gem_relocation_entry *reloc;
  781.         struct i915_address_space *vm;
  782.         struct i915_vma *vma;
  783.         bool need_relocs;
  784.         int *reloc_offset;
  785.         int i, total, ret;
  786.         unsigned count = args->buffer_count;
  787.  
  788.         vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  789.  
  790.         /* We may process another execbuffer during the unlock... */
  791.         while (!list_empty(&eb->vmas)) {
  792.                 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  793.                 list_del_init(&vma->exec_list);
  794.                 i915_gem_execbuffer_unreserve_vma(vma);
  795.                 drm_gem_object_unreference(&vma->obj->base);
  796.         }
  797.  
  798.         mutex_unlock(&dev->struct_mutex);
  799.  
  800.         total = 0;
  801.         for (i = 0; i < count; i++)
  802.                 total += exec[i].relocation_count;
  803.  
  804.     reloc_offset = __builtin_malloc(count * sizeof(*reloc_offset));
  805.     reloc = __builtin_malloc(total * sizeof(*reloc));
  806.         if (reloc == NULL || reloc_offset == NULL) {
  807.         kfree(reloc);
  808.         kfree(reloc_offset);
  809.                 mutex_lock(&dev->struct_mutex);
  810.                 return -ENOMEM;
  811.         }
  812.  
  813.         total = 0;
  814.         for (i = 0; i < count; i++) {
  815.                 struct drm_i915_gem_relocation_entry __user *user_relocs;
  816.                 u64 invalid_offset = (u64)-1;
  817.                 int j;
  818.  
  819.                 user_relocs = to_user_ptr(exec[i].relocs_ptr);
  820.  
  821.                 if (copy_from_user(reloc+total, user_relocs,
  822.                                    exec[i].relocation_count * sizeof(*reloc))) {
  823.                         ret = -EFAULT;
  824.                         mutex_lock(&dev->struct_mutex);
  825.                         goto err;
  826.                 }
  827.  
  828.                 /* As we do not update the known relocation offsets after
  829.                  * relocating (due to the complexities in lock handling),
  830.                  * we need to mark them as invalid now so that we force the
  831.                  * relocation processing next time. Just in case the target
  832.                  * object is evicted and then rebound into its old
  833.                  * presumed_offset before the next execbuffer - if that
  834.                  * happened we would make the mistake of assuming that the
  835.                  * relocations were valid.
  836.                  */
  837.                 for (j = 0; j < exec[i].relocation_count; j++) {
  838.                         if (__copy_to_user(&user_relocs[j].presumed_offset,
  839.                                            &invalid_offset,
  840.                                            sizeof(invalid_offset))) {
  841.                                 ret = -EFAULT;
  842.                                 mutex_lock(&dev->struct_mutex);
  843.                                 goto err;
  844.                         }
  845.                 }
  846.  
  847.                 reloc_offset[i] = total;
  848.                 total += exec[i].relocation_count;
  849.         }
  850.  
  851.         ret = i915_mutex_lock_interruptible(dev);
  852.         if (ret) {
  853.                 mutex_lock(&dev->struct_mutex);
  854.                 goto err;
  855.         }
  856.  
  857.         /* reacquire the objects */
  858.         eb_reset(eb);
  859.         ret = eb_lookup_vmas(eb, exec, args, vm, file);
  860.         if (ret)
  861.                 goto err;
  862.  
  863.         need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  864.         ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
  865.         if (ret)
  866.                 goto err;
  867.  
  868.         list_for_each_entry(vma, &eb->vmas, exec_list) {
  869.                 int offset = vma->exec_entry - exec;
  870.                 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  871.                                                             reloc + reloc_offset[offset]);
  872.                 if (ret)
  873.                         goto err;
  874.         }
  875.  
  876.         /* Leave the user relocations as are, this is the painfully slow path,
  877.          * and we want to avoid the complication of dropping the lock whilst
  878.          * having buffers reserved in the aperture and so causing spurious
  879.          * ENOSPC for random operations.
  880.          */
  881.  
  882. err:
  883.     kfree(reloc);
  884.     kfree(reloc_offset);
  885.         return ret;
  886. }
  887.  
  888. static int
  889. i915_gem_execbuffer_move_to_gpu(struct drm_i915_gem_request *req,
  890.                                 struct list_head *vmas)
  891. {
  892.         const unsigned other_rings = ~intel_ring_flag(req->ring);
  893.         struct i915_vma *vma;
  894.         uint32_t flush_domains = 0;
  895.         bool flush_chipset = false;
  896.         int ret;
  897.  
  898.         list_for_each_entry(vma, vmas, exec_list) {
  899.                 struct drm_i915_gem_object *obj = vma->obj;
  900.  
  901.                 if (obj->active & other_rings) {
  902.                         ret = i915_gem_object_sync(obj, req->ring, &req);
  903.                         if (ret)
  904.                                 return ret;
  905.                 }
  906.  
  907.                 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  908.                         flush_chipset |= i915_gem_clflush_object(obj, false);
  909.  
  910.                 flush_domains |= obj->base.write_domain;
  911.         }
  912.  
  913.         if (flush_chipset)
  914.                 i915_gem_chipset_flush(req->ring->dev);
  915.  
  916.         if (flush_domains & I915_GEM_DOMAIN_GTT)
  917.                 wmb();
  918.  
  919.         /* Unconditionally invalidate gpu caches and ensure that we do flush
  920.          * any residual writes from the previous batch.
  921.          */
  922.         return intel_ring_invalidate_all_caches(req);
  923. }
  924.  
  925. static bool
  926. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  927. {
  928.         if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  929.                 return false;
  930.  
  931.         /* Kernel clipping was a DRI1 misfeature */
  932.         if (exec->num_cliprects || exec->cliprects_ptr)
  933.                 return false;
  934.  
  935.         if (exec->DR4 == 0xffffffff) {
  936.                 DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  937.                 exec->DR4 = 0;
  938.         }
  939.         if (exec->DR1 || exec->DR4)
  940.                 return false;
  941.  
  942.         if ((exec->batch_start_offset | exec->batch_len) & 0x7)
  943.                 return false;
  944.  
  945.         return true;
  946. }
  947.  
  948. static int
  949. validate_exec_list(struct drm_device *dev,
  950.                    struct drm_i915_gem_exec_object2 *exec,
  951.                    int count)
  952. {
  953.         unsigned relocs_total = 0;
  954.         unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  955.         unsigned invalid_flags;
  956.         int i;
  957.  
  958.         invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  959.         if (USES_FULL_PPGTT(dev))
  960.                 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  961.  
  962.         for (i = 0; i < count; i++) {
  963.                 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  964.                 int length; /* limited by fault_in_pages_readable() */
  965.  
  966.                 if (exec[i].flags & invalid_flags)
  967.                         return -EINVAL;
  968.  
  969.                 if (exec[i].alignment && !is_power_of_2(exec[i].alignment))
  970.                         return -EINVAL;
  971.  
  972.                 /* First check for malicious input causing overflow in
  973.                  * the worst case where we need to allocate the entire
  974.                  * relocation tree as a single array.
  975.                  */
  976.                 if (exec[i].relocation_count > relocs_max - relocs_total)
  977.                         return -EINVAL;
  978.                 relocs_total += exec[i].relocation_count;
  979.  
  980.                 length = exec[i].relocation_count *
  981.                         sizeof(struct drm_i915_gem_relocation_entry);
  982.                 /*
  983.                  * We must check that the entire relocation array is safe
  984.                  * to read, but since we may need to update the presumed
  985.                  * offsets during execution, check for full write access.
  986.                  */
  987.         }
  988.  
  989.         return 0;
  990. }
  991.  
  992. static struct intel_context *
  993. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  994.                           struct intel_engine_cs *ring, const u32 ctx_id)
  995. {
  996.         struct intel_context *ctx = NULL;
  997.         struct i915_ctx_hang_stats *hs;
  998.  
  999.         if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  1000.                 return ERR_PTR(-EINVAL);
  1001.  
  1002.         ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  1003.         if (IS_ERR(ctx))
  1004.                 return ctx;
  1005.  
  1006.         hs = &ctx->hang_stats;
  1007.         if (hs->banned) {
  1008.                 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  1009.                 return ERR_PTR(-EIO);
  1010.         }
  1011.  
  1012.         if (i915.enable_execlists && !ctx->engine[ring->id].state) {
  1013.                 int ret = intel_lr_context_deferred_alloc(ctx, ring);
  1014.                 if (ret) {
  1015.                         DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  1016.                         return ERR_PTR(ret);
  1017.                 }
  1018.         }
  1019.  
  1020.         return ctx;
  1021. }
  1022.  
  1023. void
  1024. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  1025.                                    struct drm_i915_gem_request *req)
  1026. {
  1027.         struct intel_engine_cs *ring = i915_gem_request_get_ring(req);
  1028.         struct i915_vma *vma;
  1029.  
  1030.         list_for_each_entry(vma, vmas, exec_list) {
  1031.                 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  1032.                 struct drm_i915_gem_object *obj = vma->obj;
  1033.                 u32 old_read = obj->base.read_domains;
  1034.                 u32 old_write = obj->base.write_domain;
  1035.  
  1036.                 obj->dirty = 1; /* be paranoid  */
  1037.                 obj->base.write_domain = obj->base.pending_write_domain;
  1038.                 if (obj->base.write_domain == 0)
  1039.                         obj->base.pending_read_domains |= obj->base.read_domains;
  1040.                 obj->base.read_domains = obj->base.pending_read_domains;
  1041.  
  1042.                 i915_vma_move_to_active(vma, req);
  1043.                 if (obj->base.write_domain) {
  1044.                         i915_gem_request_assign(&obj->last_write_req, req);
  1045.  
  1046.                         intel_fb_obj_invalidate(obj, ORIGIN_CS);
  1047.  
  1048.                         /* update for the implicit flush after a batch */
  1049.                         obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  1050.                 }
  1051.                 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  1052.                         i915_gem_request_assign(&obj->last_fenced_req, req);
  1053.                         if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  1054.                                 struct drm_i915_private *dev_priv = to_i915(ring->dev);
  1055.                                 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  1056.                                                &dev_priv->mm.fence_list);
  1057.                         }
  1058.                 }
  1059.  
  1060.                 trace_i915_gem_object_change_domain(obj, old_read, old_write);
  1061.         }
  1062. }
  1063.  
  1064. void
  1065. i915_gem_execbuffer_retire_commands(struct i915_execbuffer_params *params)
  1066. {
  1067.         /* Unconditionally force add_request to emit a full flush. */
  1068.         params->ring->gpu_caches_dirty = true;
  1069.  
  1070.         /* Add a breadcrumb for the completion of the batch buffer */
  1071.         __i915_add_request(params->request, params->batch_obj, true);
  1072. }
  1073.  
  1074. static int
  1075. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  1076.                             struct drm_i915_gem_request *req)
  1077. {
  1078.         struct intel_engine_cs *ring = req->ring;
  1079.         struct drm_i915_private *dev_priv = dev->dev_private;
  1080.         int ret, i;
  1081.  
  1082.         if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
  1083.                 DRM_DEBUG("sol reset is gen7/rcs only\n");
  1084.                 return -EINVAL;
  1085.         }
  1086.  
  1087.         ret = intel_ring_begin(req, 4 * 3);
  1088.         if (ret)
  1089.                 return ret;
  1090.  
  1091.         for (i = 0; i < 4; i++) {
  1092.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1093.                 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  1094.                 intel_ring_emit(ring, 0);
  1095.         }
  1096.  
  1097.         intel_ring_advance(ring);
  1098.  
  1099.         return 0;
  1100. }
  1101.  
  1102. static struct drm_i915_gem_object*
  1103. i915_gem_execbuffer_parse(struct intel_engine_cs *ring,
  1104.                           struct drm_i915_gem_exec_object2 *shadow_exec_entry,
  1105.                           struct eb_vmas *eb,
  1106.                           struct drm_i915_gem_object *batch_obj,
  1107.                           u32 batch_start_offset,
  1108.                           u32 batch_len,
  1109.                           bool is_master)
  1110. {
  1111.         struct drm_i915_gem_object *shadow_batch_obj;
  1112.         struct i915_vma *vma;
  1113.         int ret;
  1114.  
  1115.         shadow_batch_obj = i915_gem_batch_pool_get(&ring->batch_pool,
  1116.                                                    PAGE_ALIGN(batch_len));
  1117.         if (IS_ERR(shadow_batch_obj))
  1118.                 return shadow_batch_obj;
  1119.  
  1120.         ret = i915_parse_cmds(ring,
  1121.                               batch_obj,
  1122.                               shadow_batch_obj,
  1123.                               batch_start_offset,
  1124.                               batch_len,
  1125.                               is_master);
  1126.         if (ret)
  1127.                 goto err;
  1128.  
  1129.         ret = i915_gem_obj_ggtt_pin(shadow_batch_obj, 0, 0);
  1130.         if (ret)
  1131.                 goto err;
  1132.  
  1133.         i915_gem_object_unpin_pages(shadow_batch_obj);
  1134.  
  1135.         memset(shadow_exec_entry, 0, sizeof(*shadow_exec_entry));
  1136.  
  1137.         vma = i915_gem_obj_to_ggtt(shadow_batch_obj);
  1138.         vma->exec_entry = shadow_exec_entry;
  1139.         vma->exec_entry->flags = __EXEC_OBJECT_HAS_PIN;
  1140.         drm_gem_object_reference(&shadow_batch_obj->base);
  1141.         list_add_tail(&vma->exec_list, &eb->vmas);
  1142.  
  1143.         shadow_batch_obj->base.pending_read_domains = I915_GEM_DOMAIN_COMMAND;
  1144.  
  1145.         return shadow_batch_obj;
  1146.  
  1147. err:
  1148.         i915_gem_object_unpin_pages(shadow_batch_obj);
  1149.         if (ret == -EACCES) /* unhandled chained batch */
  1150.                 return batch_obj;
  1151.         else
  1152.                 return ERR_PTR(ret);
  1153. }
  1154.  
  1155. int
  1156. i915_gem_ringbuffer_submission(struct i915_execbuffer_params *params,
  1157.                                struct drm_i915_gem_execbuffer2 *args,
  1158.                                struct list_head *vmas)
  1159. {
  1160.         struct drm_device *dev = params->dev;
  1161.         struct intel_engine_cs *ring = params->ring;
  1162.         struct drm_i915_private *dev_priv = dev->dev_private;
  1163.         u64 exec_start, exec_len;
  1164.         int instp_mode;
  1165.         u32 instp_mask;
  1166.         int ret;
  1167.  
  1168.         ret = i915_gem_execbuffer_move_to_gpu(params->request, vmas);
  1169.         if (ret)
  1170.                 return ret;
  1171.  
  1172.         ret = i915_switch_context(params->request);
  1173.         if (ret)
  1174.                 return ret;
  1175.  
  1176.         WARN(params->ctx->ppgtt && params->ctx->ppgtt->pd_dirty_rings & (1<<ring->id),
  1177.              "%s didn't clear reload\n", ring->name);
  1178.  
  1179.         instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1180.         instp_mask = I915_EXEC_CONSTANTS_MASK;
  1181.         switch (instp_mode) {
  1182.         case I915_EXEC_CONSTANTS_REL_GENERAL:
  1183.         case I915_EXEC_CONSTANTS_ABSOLUTE:
  1184.         case I915_EXEC_CONSTANTS_REL_SURFACE:
  1185.                 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  1186.                         DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1187.                         return -EINVAL;
  1188.                 }
  1189.  
  1190.                 if (instp_mode != dev_priv->relative_constants_mode) {
  1191.                         if (INTEL_INFO(dev)->gen < 4) {
  1192.                                 DRM_DEBUG("no rel constants on pre-gen4\n");
  1193.                                 return -EINVAL;
  1194.                         }
  1195.  
  1196.                         if (INTEL_INFO(dev)->gen > 5 &&
  1197.                             instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1198.                                 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1199.                                 return -EINVAL;
  1200.                         }
  1201.  
  1202.                         /* The HW changed the meaning on this bit on gen6 */
  1203.                         if (INTEL_INFO(dev)->gen >= 6)
  1204.                                 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1205.                 }
  1206.                 break;
  1207.         default:
  1208.                 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1209.                 return -EINVAL;
  1210.         }
  1211.  
  1212.         if (ring == &dev_priv->ring[RCS] &&
  1213.             instp_mode != dev_priv->relative_constants_mode) {
  1214.                 ret = intel_ring_begin(params->request, 4);
  1215.                 if (ret)
  1216.                         return ret;
  1217.  
  1218.                 intel_ring_emit(ring, MI_NOOP);
  1219.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1220.                 intel_ring_emit(ring, INSTPM);
  1221.                 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1222.                 intel_ring_advance(ring);
  1223.  
  1224.                 dev_priv->relative_constants_mode = instp_mode;
  1225.         }
  1226.  
  1227.         if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1228.                 ret = i915_reset_gen7_sol_offsets(dev, params->request);
  1229.                 if (ret)
  1230.                         return ret;
  1231.         }
  1232.  
  1233.         exec_len   = args->batch_len;
  1234.         exec_start = params->batch_obj_vm_offset +
  1235.                      params->args_batch_start_offset;
  1236.  
  1237.         ret = ring->dispatch_execbuffer(params->request,
  1238.                                         exec_start, exec_len,
  1239.                                         params->dispatch_flags);
  1240.         if (ret)
  1241.                 return ret;
  1242.  
  1243.         trace_i915_gem_ring_dispatch(params->request, params->dispatch_flags);
  1244.  
  1245.         i915_gem_execbuffer_move_to_active(vmas, params->request);
  1246.         i915_gem_execbuffer_retire_commands(params);
  1247.  
  1248.         return 0;
  1249. }
  1250.  
  1251. /**
  1252.  * Find one BSD ring to dispatch the corresponding BSD command.
  1253.  * The Ring ID is returned.
  1254.  */
  1255. static int gen8_dispatch_bsd_ring(struct drm_device *dev,
  1256.                                   struct drm_file *file)
  1257. {
  1258.         struct drm_i915_private *dev_priv = dev->dev_private;
  1259.         struct drm_i915_file_private *file_priv = file->driver_priv;
  1260.  
  1261.         /* Check whether the file_priv is using one ring */
  1262.         if (file_priv->bsd_ring)
  1263.                 return file_priv->bsd_ring->id;
  1264.         else {
  1265.                 /* If no, use the ping-pong mechanism to select one ring */
  1266.                 int ring_id;
  1267.  
  1268.                 mutex_lock(&dev->struct_mutex);
  1269.                 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
  1270.                         ring_id = VCS;
  1271.                         dev_priv->mm.bsd_ring_dispatch_index = 1;
  1272.                 } else {
  1273.                         ring_id = VCS2;
  1274.                         dev_priv->mm.bsd_ring_dispatch_index = 0;
  1275.                 }
  1276.                 file_priv->bsd_ring = &dev_priv->ring[ring_id];
  1277.                 mutex_unlock(&dev->struct_mutex);
  1278.                 return ring_id;
  1279.         }
  1280. }
  1281.  
  1282. static struct drm_i915_gem_object *
  1283. eb_get_batch(struct eb_vmas *eb)
  1284. {
  1285.         struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1286.  
  1287.         /*
  1288.          * SNA is doing fancy tricks with compressing batch buffers, which leads
  1289.          * to negative relocation deltas. Usually that works out ok since the
  1290.          * relocate address is still positive, except when the batch is placed
  1291.          * very low in the GTT. Ensure this doesn't happen.
  1292.          *
  1293.          * Note that actual hangs have only been observed on gen7, but for
  1294.          * paranoia do it everywhere.
  1295.          */
  1296.         vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1297.  
  1298.         return vma->obj;
  1299. }
  1300.  
  1301. static int
  1302. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1303.                        struct drm_file *file,
  1304.                        struct drm_i915_gem_execbuffer2 *args,
  1305.                        struct drm_i915_gem_exec_object2 *exec)
  1306. {
  1307.         struct drm_i915_private *dev_priv = dev->dev_private;
  1308.         struct eb_vmas *eb;
  1309.         struct drm_i915_gem_object *batch_obj;
  1310.         struct drm_i915_gem_exec_object2 shadow_exec_entry;
  1311.         struct intel_engine_cs *ring;
  1312.         struct intel_context *ctx;
  1313.         struct i915_address_space *vm;
  1314.         struct i915_execbuffer_params params_master; /* XXX: will be removed later */
  1315.         struct i915_execbuffer_params *params = &params_master;
  1316.         const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1317.         u32 dispatch_flags;
  1318.         int ret;
  1319.         bool need_relocs;
  1320.  
  1321.         if (!i915_gem_check_execbuffer(args))
  1322.                 return -EINVAL;
  1323.  
  1324.         ret = validate_exec_list(dev, exec, args->buffer_count);
  1325.         if (ret)
  1326.                 return ret;
  1327.  
  1328.         dispatch_flags = 0;
  1329.         if (args->flags & I915_EXEC_SECURE) {
  1330.  
  1331.                 dispatch_flags |= I915_DISPATCH_SECURE;
  1332.         }
  1333.         if (args->flags & I915_EXEC_IS_PINNED)
  1334.                 dispatch_flags |= I915_DISPATCH_PINNED;
  1335.  
  1336.         if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
  1337.                 DRM_DEBUG("execbuf with unknown ring: %d\n",
  1338.                           (int)(args->flags & I915_EXEC_RING_MASK));
  1339.                 return -EINVAL;
  1340.         }
  1341.  
  1342.         if (((args->flags & I915_EXEC_RING_MASK) != I915_EXEC_BSD) &&
  1343.             ((args->flags & I915_EXEC_BSD_MASK) != 0)) {
  1344.                 DRM_DEBUG("execbuf with non bsd ring but with invalid "
  1345.                         "bsd dispatch flags: %d\n", (int)(args->flags));
  1346.                 return -EINVAL;
  1347.         }
  1348.  
  1349.         if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
  1350.                 ring = &dev_priv->ring[RCS];
  1351.         else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
  1352.                 if (HAS_BSD2(dev)) {
  1353.                         int ring_id;
  1354.  
  1355.                         switch (args->flags & I915_EXEC_BSD_MASK) {
  1356.                         case I915_EXEC_BSD_DEFAULT:
  1357.                                 ring_id = gen8_dispatch_bsd_ring(dev, file);
  1358.                                 ring = &dev_priv->ring[ring_id];
  1359.                                 break;
  1360.                         case I915_EXEC_BSD_RING1:
  1361.                                 ring = &dev_priv->ring[VCS];
  1362.                                 break;
  1363.                         case I915_EXEC_BSD_RING2:
  1364.                                 ring = &dev_priv->ring[VCS2];
  1365.                                 break;
  1366.                         default:
  1367.                                 DRM_DEBUG("execbuf with unknown bsd ring: %d\n",
  1368.                                           (int)(args->flags & I915_EXEC_BSD_MASK));
  1369.                                 return -EINVAL;
  1370.                         }
  1371.                 } else
  1372.                         ring = &dev_priv->ring[VCS];
  1373.         } else
  1374.                 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
  1375.  
  1376.         if (!intel_ring_initialized(ring)) {
  1377.                 DRM_DEBUG("execbuf with invalid ring: %d\n",
  1378.                           (int)(args->flags & I915_EXEC_RING_MASK));
  1379.                 return -EINVAL;
  1380.         }
  1381.  
  1382.         if (args->buffer_count < 1) {
  1383.                 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1384.                 return -EINVAL;
  1385.         }
  1386.  
  1387.         if (args->flags & I915_EXEC_RESOURCE_STREAMER) {
  1388.                 if (!HAS_RESOURCE_STREAMER(dev)) {
  1389.                         DRM_DEBUG("RS is only allowed for Haswell, Gen8 and above\n");
  1390.                         return -EINVAL;
  1391.                 }
  1392.                 if (ring->id != RCS) {
  1393.                         DRM_DEBUG("RS is not available on %s\n",
  1394.                                  ring->name);
  1395.                         return -EINVAL;
  1396.                 }
  1397.  
  1398.                 dispatch_flags |= I915_DISPATCH_RS;
  1399.         }
  1400.  
  1401.         intel_runtime_pm_get(dev_priv);
  1402.  
  1403.         ret = i915_mutex_lock_interruptible(dev);
  1404.         if (ret)
  1405.                 goto pre_mutex_err;
  1406.  
  1407.         ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
  1408.         if (IS_ERR(ctx)) {
  1409.                 mutex_unlock(&dev->struct_mutex);
  1410.                 ret = PTR_ERR(ctx);
  1411.                 goto pre_mutex_err;
  1412.         }
  1413.  
  1414.         i915_gem_context_reference(ctx);
  1415.  
  1416.         if (ctx->ppgtt)
  1417.                 vm = &ctx->ppgtt->base;
  1418.         else
  1419.                 vm = &dev_priv->gtt.base;
  1420.  
  1421.         memset(&params_master, 0x00, sizeof(params_master));
  1422.  
  1423.         eb = eb_create(args);
  1424.         if (eb == NULL) {
  1425.                 i915_gem_context_unreference(ctx);
  1426.                 mutex_unlock(&dev->struct_mutex);
  1427.                 ret = -ENOMEM;
  1428.                 goto pre_mutex_err;
  1429.         }
  1430.  
  1431.         /* Look up object handles */
  1432.         ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1433.         if (ret)
  1434.                 goto err;
  1435.  
  1436.         /* take note of the batch buffer before we might reorder the lists */
  1437.         batch_obj = eb_get_batch(eb);
  1438.  
  1439.         /* Move the objects en-masse into the GTT, evicting if necessary. */
  1440.         need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1441.         ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, ctx, &need_relocs);
  1442.         if (ret)
  1443.                 goto err;
  1444.  
  1445.         /* The objects are in their final locations, apply the relocations. */
  1446.         if (need_relocs)
  1447.                 ret = i915_gem_execbuffer_relocate(eb);
  1448.         if (ret) {
  1449.                 if (ret == -EFAULT) {
  1450.                         ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  1451.                                                                 eb, exec, ctx);
  1452.                         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1453.                 }
  1454.                 if (ret)
  1455.                         goto err;
  1456.         }
  1457.  
  1458.         /* Set the pending read domains for the batch buffer to COMMAND */
  1459.         if (batch_obj->base.pending_write_domain) {
  1460.                 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1461.                 ret = -EINVAL;
  1462.                 goto err;
  1463.         }
  1464.  
  1465.         params->args_batch_start_offset = args->batch_start_offset;
  1466.  
  1467. #if 0
  1468.         if (i915_needs_cmd_parser(ring) && args->batch_len) {
  1469.                 struct drm_i915_gem_object *parsed_batch_obj;
  1470.  
  1471.                 parsed_batch_obj = i915_gem_execbuffer_parse(ring,
  1472.                                                       &shadow_exec_entry,
  1473.                                                       eb,
  1474.                                                       batch_obj,
  1475.                                                       args->batch_start_offset,
  1476.                                                       args->batch_len,
  1477.                                                       file->is_master);
  1478.                 if (IS_ERR(parsed_batch_obj)) {
  1479.                         ret = PTR_ERR(parsed_batch_obj);
  1480.                         goto err;
  1481.                 }
  1482.  
  1483.                 /*
  1484.                  * parsed_batch_obj == batch_obj means batch not fully parsed:
  1485.                  * Accept, but don't promote to secure.
  1486.                  */
  1487.  
  1488.                 if (parsed_batch_obj != batch_obj) {
  1489.                         /*
  1490.                          * Batch parsed and accepted:
  1491.                          *
  1492.                          * Set the DISPATCH_SECURE bit to remove the NON_SECURE
  1493.                          * bit from MI_BATCH_BUFFER_START commands issued in
  1494.                          * the dispatch_execbuffer implementations. We
  1495.                          * specifically don't want that set on batches the
  1496.                          * command parser has accepted.
  1497.                          */
  1498.                         dispatch_flags |= I915_DISPATCH_SECURE;
  1499.                         params->args_batch_start_offset = 0;
  1500.                         batch_obj = parsed_batch_obj;
  1501.                 }
  1502.         }
  1503. #endif
  1504.  
  1505.         batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1506.  
  1507.         /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1508.          * batch" bit. Hence we need to pin secure batches into the global gtt.
  1509.          * hsw should have this fixed, but bdw mucks it up again. */
  1510.         if (dispatch_flags & I915_DISPATCH_SECURE) {
  1511.                 /*
  1512.                  * So on first glance it looks freaky that we pin the batch here
  1513.                  * outside of the reservation loop. But:
  1514.                  * - The batch is already pinned into the relevant ppgtt, so we
  1515.                  *   already have the backing storage fully allocated.
  1516.                  * - No other BO uses the global gtt (well contexts, but meh),
  1517.                  *   so we don't really have issues with multiple objects not
  1518.                  *   fitting due to fragmentation.
  1519.                  * So this is actually safe.
  1520.                  */
  1521.                 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1522.                 if (ret)
  1523.                         goto err;
  1524.  
  1525.                 params->batch_obj_vm_offset = i915_gem_obj_ggtt_offset(batch_obj);
  1526.         } else
  1527.                 params->batch_obj_vm_offset = i915_gem_obj_offset(batch_obj, vm);
  1528.  
  1529.         /* Allocate a request for this batch buffer nice and early. */
  1530.         ret = i915_gem_request_alloc(ring, ctx, &params->request);
  1531.         if (ret)
  1532.                 goto err_batch_unpin;
  1533.  
  1534.         ret = i915_gem_request_add_to_client(params->request, file);
  1535.         if (ret)
  1536.                 goto err_batch_unpin;
  1537.  
  1538.         /*
  1539.          * Save assorted stuff away to pass through to *_submission().
  1540.          * NB: This data should be 'persistent' and not local as it will
  1541.          * kept around beyond the duration of the IOCTL once the GPU
  1542.          * scheduler arrives.
  1543.          */
  1544.         params->dev                     = dev;
  1545.         params->file                    = file;
  1546.         params->ring                    = ring;
  1547.         params->dispatch_flags          = dispatch_flags;
  1548.         params->batch_obj               = batch_obj;
  1549.         params->ctx                     = ctx;
  1550.  
  1551.         ret = dev_priv->gt.execbuf_submit(params, args, &eb->vmas);
  1552.  
  1553. err_batch_unpin:
  1554.         /*
  1555.          * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1556.          * batch vma for correctness. For less ugly and less fragility this
  1557.          * needs to be adjusted to also track the ggtt batch vma properly as
  1558.          * active.
  1559.          */
  1560.         if (dispatch_flags & I915_DISPATCH_SECURE)
  1561.                 i915_gem_object_ggtt_unpin(batch_obj);
  1562.  
  1563. err:
  1564.         /* the request owns the ref now */
  1565.         i915_gem_context_unreference(ctx);
  1566.         eb_destroy(eb);
  1567.  
  1568.         /*
  1569.          * If the request was created but not successfully submitted then it
  1570.          * must be freed again. If it was submitted then it is being tracked
  1571.          * on the active request list and no clean up is required here.
  1572.          */
  1573.         if (ret && params->request)
  1574.                 i915_gem_request_cancel(params->request);
  1575.  
  1576.         mutex_unlock(&dev->struct_mutex);
  1577.  
  1578. pre_mutex_err:
  1579.         /* intel_gpu_busy should also get a ref, so it will free when the device
  1580.          * is really idle. */
  1581.         intel_runtime_pm_put(dev_priv);
  1582.         return ret;
  1583. }
  1584.  
  1585. #if 0
  1586. /*
  1587.  * Legacy execbuffer just creates an exec2 list from the original exec object
  1588.  * list array and passes it to the real function.
  1589.  */
  1590. int
  1591. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1592.                     struct drm_file *file)
  1593. {
  1594.         struct drm_i915_gem_execbuffer *args = data;
  1595.         struct drm_i915_gem_execbuffer2 exec2;
  1596.         struct drm_i915_gem_exec_object *exec_list = NULL;
  1597.         struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1598.         int ret, i;
  1599.  
  1600.         if (args->buffer_count < 1) {
  1601.                 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1602.                 return -EINVAL;
  1603.         }
  1604.  
  1605.         /* Copy in the exec list from userland */
  1606.         exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1607.         exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1608.         if (exec_list == NULL || exec2_list == NULL) {
  1609.                 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1610.                           args->buffer_count);
  1611.                 drm_free_large(exec_list);
  1612.                 drm_free_large(exec2_list);
  1613.                 return -ENOMEM;
  1614.         }
  1615.         ret = copy_from_user(exec_list,
  1616.                              to_user_ptr(args->buffers_ptr),
  1617.                              sizeof(*exec_list) * args->buffer_count);
  1618.         if (ret != 0) {
  1619.                 DRM_DEBUG("copy %d exec entries failed %d\n",
  1620.                           args->buffer_count, ret);
  1621.                 drm_free_large(exec_list);
  1622.                 drm_free_large(exec2_list);
  1623.                 return -EFAULT;
  1624.         }
  1625.  
  1626.         for (i = 0; i < args->buffer_count; i++) {
  1627.                 exec2_list[i].handle = exec_list[i].handle;
  1628.                 exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1629.                 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1630.                 exec2_list[i].alignment = exec_list[i].alignment;
  1631.                 exec2_list[i].offset = exec_list[i].offset;
  1632.                 if (INTEL_INFO(dev)->gen < 4)
  1633.                         exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1634.                 else
  1635.                         exec2_list[i].flags = 0;
  1636.         }
  1637.  
  1638.         exec2.buffers_ptr = args->buffers_ptr;
  1639.         exec2.buffer_count = args->buffer_count;
  1640.         exec2.batch_start_offset = args->batch_start_offset;
  1641.         exec2.batch_len = args->batch_len;
  1642.         exec2.DR1 = args->DR1;
  1643.         exec2.DR4 = args->DR4;
  1644.         exec2.num_cliprects = args->num_cliprects;
  1645.         exec2.cliprects_ptr = args->cliprects_ptr;
  1646.         exec2.flags = I915_EXEC_RENDER;
  1647.         i915_execbuffer2_set_context_id(exec2, 0);
  1648.  
  1649.         ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1650.         if (!ret) {
  1651.                 struct drm_i915_gem_exec_object __user *user_exec_list =
  1652.                         to_user_ptr(args->buffers_ptr);
  1653.  
  1654.                 /* Copy the new buffer offsets back to the user's exec list. */
  1655.                 for (i = 0; i < args->buffer_count; i++) {
  1656.                         ret = __copy_to_user(&user_exec_list[i].offset,
  1657.                                              &exec2_list[i].offset,
  1658.                                              sizeof(user_exec_list[i].offset));
  1659.                         if (ret) {
  1660.                                 ret = -EFAULT;
  1661.                                 DRM_DEBUG("failed to copy %d exec entries "
  1662.                                           "back to user (%d)\n",
  1663.                                           args->buffer_count, ret);
  1664.                                 break;
  1665.                         }
  1666.                 }
  1667.         }
  1668.  
  1669.         drm_free_large(exec_list);
  1670.         drm_free_large(exec2_list);
  1671.         return ret;
  1672. }
  1673. #endif
  1674.  
  1675. int
  1676. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1677.                      struct drm_file *file)
  1678. {
  1679.         struct drm_i915_gem_execbuffer2 *args = data;
  1680.         struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1681.         int ret;
  1682.  
  1683.         if (args->buffer_count < 1 ||
  1684.             args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1685.                 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1686.                 return -EINVAL;
  1687.         }
  1688.  
  1689.         if (args->rsvd2 != 0) {
  1690.                 DRM_DEBUG("dirty rvsd2 field\n");
  1691.                 return -EINVAL;
  1692.         }
  1693.  
  1694.         exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1695.                              GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1696.         if (exec2_list == NULL) {
  1697.                 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1698.                           args->buffer_count);
  1699.                 return -ENOMEM;
  1700.         }
  1701.         ret = copy_from_user(exec2_list,
  1702.                              to_user_ptr(args->buffers_ptr),
  1703.                              sizeof(*exec2_list) * args->buffer_count);
  1704.         if (ret != 0) {
  1705.                 DRM_DEBUG("copy %d exec entries failed %d\n",
  1706.                           args->buffer_count, ret);
  1707.         kfree(exec2_list);
  1708.         FAIL();
  1709.                 return -EFAULT;
  1710.         }
  1711.  
  1712.         ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1713.         if (!ret) {
  1714.                 /* Copy the new buffer offsets back to the user's exec list. */
  1715.                 struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1716.                                    to_user_ptr(args->buffers_ptr);
  1717.                 int i;
  1718.  
  1719.                 for (i = 0; i < args->buffer_count; i++) {
  1720.                         ret = __copy_to_user(&user_exec_list[i].offset,
  1721.                                              &exec2_list[i].offset,
  1722.                                              sizeof(user_exec_list[i].offset));
  1723.                         if (ret) {
  1724.                                 ret = -EFAULT;
  1725.                                 DRM_DEBUG("failed to copy %d exec entries "
  1726.                                           "back to user\n",
  1727.                                           args->buffer_count);
  1728.                                 break;
  1729.                         }
  1730.                 }
  1731.         }
  1732.  
  1733.     kfree(exec2_list);
  1734.         return ret;
  1735. }
  1736.