Subversion Repositories Kolibri OS

Rev

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