Subversion Repositories Kolibri OS

Rev

Rev 5354 | Rev 6084 | 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->map_and_fenceable ||
  256.                 obj->cache_level != I915_CACHE_NONE);
  257. }
  258.  
  259. static int
  260. relocate_entry_cpu(struct drm_i915_gem_object *obj,
  261.                    struct drm_i915_gem_relocation_entry *reloc,
  262.                    uint64_t target_offset)
  263. {
  264.     struct drm_device *dev = obj->base.dev;
  265.     uint32_t page_offset = offset_in_page(reloc->offset);
  266.         uint64_t delta = reloc->delta + target_offset;
  267.         char *vaddr;
  268.         int ret;
  269.  
  270.         ret = i915_gem_object_set_to_cpu_domain(obj, true);
  271.         if (ret)
  272.                 return ret;
  273.  
  274.         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  275.                                 reloc->offset >> PAGE_SHIFT));
  276.         *(uint32_t *)(vaddr + page_offset) = lower_32_bits(delta);
  277.  
  278.         if (INTEL_INFO(dev)->gen >= 8) {
  279.                 page_offset = offset_in_page(page_offset + sizeof(uint32_t));
  280.  
  281.                 if (page_offset == 0) {
  282.                         kunmap_atomic(vaddr);
  283.                         vaddr = kmap_atomic(i915_gem_object_get_page(obj,
  284.                             (reloc->offset + sizeof(uint32_t)) >> PAGE_SHIFT));
  285.                 }
  286.  
  287.                 *(uint32_t *)(vaddr + page_offset) = upper_32_bits(delta);
  288.         }
  289.  
  290.         kunmap_atomic(vaddr);
  291.  
  292.         return 0;
  293. }
  294.  
  295. static int
  296. relocate_entry_gtt(struct drm_i915_gem_object *obj,
  297.                    struct drm_i915_gem_relocation_entry *reloc,
  298.                    uint64_t target_offset)
  299. {
  300.         struct drm_device *dev = obj->base.dev;
  301.         struct drm_i915_private *dev_priv = dev->dev_private;
  302.         uint64_t delta = reloc->delta + target_offset;
  303.         uint64_t offset;
  304.         void __iomem *reloc_page;
  305.         int ret;
  306.  
  307.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  308.         if (ret)
  309.                 return ret;
  310.  
  311.         ret = i915_gem_object_put_fence(obj);
  312.         if (ret)
  313.                 return ret;
  314.  
  315.         /* Map the page containing the relocation we're going to perform.  */
  316.         offset = i915_gem_obj_ggtt_offset(obj);
  317.         offset += reloc->offset;
  318.     MapPage(dev_priv->gtt.mappable,dev_priv->gtt.mappable_base +
  319.                                  (offset & PAGE_MASK), PG_SW);
  320.         reloc_page = dev_priv->gtt.mappable;
  321.         iowrite32(lower_32_bits(delta), reloc_page + offset_in_page(offset));
  322.  
  323.  
  324.  
  325.         return 0;
  326. }
  327.  
  328. static int
  329. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  330.                                    struct eb_vmas *eb,
  331.                                    struct drm_i915_gem_relocation_entry *reloc)
  332. {
  333.         struct drm_device *dev = obj->base.dev;
  334.         struct drm_gem_object *target_obj;
  335.         struct drm_i915_gem_object *target_i915_obj;
  336.         struct i915_vma *target_vma;
  337.         uint64_t target_offset;
  338.         int ret;
  339.  
  340.         /* we've already hold a reference to all valid objects */
  341.         target_vma = eb_get_vma(eb, reloc->target_handle);
  342.         if (unlikely(target_vma == NULL))
  343.                 return -ENOENT;
  344.         target_i915_obj = target_vma->obj;
  345.         target_obj = &target_vma->obj->base;
  346.  
  347.         target_offset = target_vma->node.start;
  348.  
  349.         /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  350.          * pipe_control writes because the gpu doesn't properly redirect them
  351.          * through the ppgtt for non_secure batchbuffers. */
  352.         if (unlikely(IS_GEN6(dev) &&
  353.             reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  354.             !(target_vma->bound & GLOBAL_BIND)))
  355.                 target_vma->bind_vma(target_vma, target_i915_obj->cache_level,
  356.                                 GLOBAL_BIND);
  357.  
  358.         /* Validate that the target is in a valid r/w GPU domain */
  359.         if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  360.                 DRM_DEBUG("reloc with multiple write domains: "
  361.                           "obj %p target %d offset %d "
  362.                           "read %08x write %08x",
  363.                           obj, reloc->target_handle,
  364.                           (int) reloc->offset,
  365.                           reloc->read_domains,
  366.                           reloc->write_domain);
  367.                 return -EINVAL;
  368.         }
  369.         if (unlikely((reloc->write_domain | reloc->read_domains)
  370.                      & ~I915_GEM_GPU_DOMAINS)) {
  371.                 DRM_DEBUG("reloc with read/write non-GPU domains: "
  372.                           "obj %p target %d offset %d "
  373.                           "read %08x write %08x",
  374.                           obj, reloc->target_handle,
  375.                           (int) reloc->offset,
  376.                           reloc->read_domains,
  377.                           reloc->write_domain);
  378.                 return -EINVAL;
  379.         }
  380.  
  381.         target_obj->pending_read_domains |= reloc->read_domains;
  382.         target_obj->pending_write_domain |= reloc->write_domain;
  383.  
  384.         /* If the relocation already has the right value in it, no
  385.          * more work needs to be done.
  386.          */
  387.         if (target_offset == reloc->presumed_offset)
  388.                 return 0;
  389.  
  390.         /* Check that the relocation address is valid... */
  391.         if (unlikely(reloc->offset >
  392.                 obj->base.size - (INTEL_INFO(dev)->gen >= 8 ? 8 : 4))) {
  393.                 DRM_DEBUG("Relocation beyond object bounds: "
  394.                           "obj %p target %d offset %d size %d.\n",
  395.                           obj, reloc->target_handle,
  396.                           (int) reloc->offset,
  397.                           (int) obj->base.size);
  398.                 return -EINVAL;
  399.         }
  400.         if (unlikely(reloc->offset & 3)) {
  401.                 DRM_DEBUG("Relocation not 4-byte aligned: "
  402.                           "obj %p target %d offset %d.\n",
  403.                           obj, reloc->target_handle,
  404.                           (int) reloc->offset);
  405.                 return -EINVAL;
  406.         }
  407.  
  408.         /* We can't wait for rendering with pagefaults disabled */
  409.  
  410.         if (use_cpu_reloc(obj))
  411.                 ret = relocate_entry_cpu(obj, reloc, target_offset);
  412.         else
  413.                 ret = relocate_entry_gtt(obj, reloc, target_offset);
  414.  
  415.                 if (ret)
  416.                         return ret;
  417.  
  418.         /* and update the user's relocation entry */
  419.         reloc->presumed_offset = target_offset;
  420.  
  421.         return 0;
  422. }
  423.  
  424. static int
  425. i915_gem_execbuffer_relocate_vma(struct i915_vma *vma,
  426.                                  struct eb_vmas *eb)
  427. {
  428. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  429.         struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(64)];
  430.         struct drm_i915_gem_relocation_entry __user *user_relocs;
  431.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  432.         int remain, ret;
  433.  
  434.         user_relocs = to_user_ptr(entry->relocs_ptr);
  435.  
  436.         remain = entry->relocation_count;
  437.         while (remain) {
  438.                 struct drm_i915_gem_relocation_entry *r = stack_reloc;
  439.                 int count = remain;
  440.                 if (count > ARRAY_SIZE(stack_reloc))
  441.                         count = ARRAY_SIZE(stack_reloc);
  442.                 remain -= count;
  443.  
  444.         memcpy(r, user_relocs, count*sizeof(r[0]));
  445.  
  446.                 do {
  447.                         u64 offset = r->presumed_offset;
  448.  
  449.                         ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, r);
  450.                         if (ret)
  451.                                 return ret;
  452.  
  453.                 if (r->presumed_offset != offset)
  454.                 {
  455.             memcpy(&user_relocs->presumed_offset,
  456.                    &r->presumed_offset,
  457.                    sizeof(r->presumed_offset));
  458.                 }
  459.  
  460.                         user_relocs++;
  461.                         r++;
  462.                 } while (--count);
  463.         }
  464.  
  465.         return 0;
  466. #undef N_RELOC
  467. }
  468.  
  469. static int
  470. i915_gem_execbuffer_relocate_vma_slow(struct i915_vma *vma,
  471.                                       struct eb_vmas *eb,
  472.                                       struct drm_i915_gem_relocation_entry *relocs)
  473. {
  474.         const struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  475.         int i, ret;
  476.  
  477.         for (i = 0; i < entry->relocation_count; i++) {
  478.                 ret = i915_gem_execbuffer_relocate_entry(vma->obj, eb, &relocs[i]);
  479.                 if (ret)
  480.                         return ret;
  481.         }
  482.  
  483.         return 0;
  484. }
  485.  
  486. static int
  487. i915_gem_execbuffer_relocate(struct eb_vmas *eb)
  488. {
  489.         struct i915_vma *vma;
  490.         int ret = 0;
  491.  
  492.         /* This is the fast path and we cannot handle a pagefault whilst
  493.          * holding the struct mutex lest the user pass in the relocations
  494.          * contained within a mmaped bo. For in such a case we, the page
  495.          * fault handler would call i915_gem_fault() and we would try to
  496.          * acquire the struct mutex again. Obviously this is bad and so
  497.          * lockdep complains vehemently.
  498.          */
  499. //      pagefault_disable();
  500.         list_for_each_entry(vma, &eb->vmas, exec_list) {
  501.                 ret = i915_gem_execbuffer_relocate_vma(vma, eb);
  502.                 if (ret)
  503.                         break;
  504.         }
  505. //   pagefault_enable();
  506.  
  507.         return ret;
  508. }
  509.  
  510. static int
  511. i915_gem_execbuffer_reserve_vma(struct i915_vma *vma,
  512.                                 struct intel_engine_cs *ring,
  513.                                    bool *need_reloc)
  514. {
  515.         struct drm_i915_gem_object *obj = vma->obj;
  516.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  517.         uint64_t flags;
  518.         int ret;
  519.  
  520.         flags = 0;
  521.         if (entry->flags & __EXEC_OBJECT_NEEDS_MAP)
  522.                 flags |= PIN_GLOBAL | PIN_MAPPABLE;
  523.         if (entry->flags & EXEC_OBJECT_NEEDS_GTT)
  524.                 flags |= PIN_GLOBAL;
  525.         if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS)
  526.                 flags |= BATCH_OFFSET_BIAS | PIN_OFFSET_BIAS;
  527.  
  528.         ret = i915_gem_object_pin(obj, vma->vm, entry->alignment, flags);
  529.         if (ret)
  530.                 return ret;
  531.  
  532.         entry->flags |= __EXEC_OBJECT_HAS_PIN;
  533.  
  534.                 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  535.                         ret = i915_gem_object_get_fence(obj);
  536.                         if (ret)
  537.                                 return ret;
  538.  
  539.                         if (i915_gem_object_pin_fence(obj))
  540.                                 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  541.         }
  542.  
  543.         if (entry->offset != vma->node.start) {
  544.                 entry->offset = vma->node.start;
  545.                 *need_reloc = true;
  546.         }
  547.  
  548.         if (entry->flags & EXEC_OBJECT_WRITE) {
  549.                 obj->base.pending_read_domains = I915_GEM_DOMAIN_RENDER;
  550.                 obj->base.pending_write_domain = I915_GEM_DOMAIN_RENDER;
  551.         }
  552.  
  553.         return 0;
  554. }
  555.  
  556. static bool
  557. need_reloc_mappable(struct i915_vma *vma)
  558. {
  559.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  560.  
  561.         if (entry->relocation_count == 0)
  562.                 return false;
  563.  
  564.         if (!i915_is_ggtt(vma->vm))
  565.                 return false;
  566.  
  567.         /* See also use_cpu_reloc() */
  568.         if (HAS_LLC(vma->obj->base.dev))
  569.                 return false;
  570.  
  571.         if (vma->obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  572.                 return false;
  573.  
  574.         return true;
  575. }
  576.  
  577. static bool
  578. eb_vma_misplaced(struct i915_vma *vma)
  579. {
  580.         struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  581.         struct drm_i915_gem_object *obj = vma->obj;
  582.  
  583.         WARN_ON(entry->flags & __EXEC_OBJECT_NEEDS_MAP &&
  584.                !i915_is_ggtt(vma->vm));
  585.  
  586.         if (entry->alignment &&
  587.             vma->node.start & (entry->alignment - 1))
  588.                 return true;
  589.  
  590.         if (entry->flags & __EXEC_OBJECT_NEEDS_MAP && !obj->map_and_fenceable)
  591.                 return true;
  592.  
  593.         if (entry->flags & __EXEC_OBJECT_NEEDS_BIAS &&
  594.             vma->node.start < BATCH_OFFSET_BIAS)
  595.                 return true;
  596.  
  597.         return false;
  598. }
  599.  
  600. static int
  601. i915_gem_execbuffer_reserve(struct intel_engine_cs *ring,
  602.                             struct list_head *vmas,
  603.                             bool *need_relocs)
  604. {
  605.         struct drm_i915_gem_object *obj;
  606.         struct i915_vma *vma;
  607.         struct i915_address_space *vm;
  608.         struct list_head ordered_vmas;
  609.         bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  610.         int retry;
  611.  
  612.         i915_gem_retire_requests_ring(ring);
  613.  
  614.         vm = list_first_entry(vmas, struct i915_vma, exec_list)->vm;
  615.  
  616.         INIT_LIST_HEAD(&ordered_vmas);
  617.         while (!list_empty(vmas)) {
  618.                 struct drm_i915_gem_exec_object2 *entry;
  619.                 bool need_fence, need_mappable;
  620.  
  621.                 vma = list_first_entry(vmas, struct i915_vma, exec_list);
  622.                 obj = vma->obj;
  623.                 entry = vma->exec_entry;
  624.  
  625.                 if (!has_fenced_gpu_access)
  626.                         entry->flags &= ~EXEC_OBJECT_NEEDS_FENCE;
  627.                 need_fence =
  628.                         entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  629.                         obj->tiling_mode != I915_TILING_NONE;
  630.                 need_mappable = need_fence || need_reloc_mappable(vma);
  631.  
  632.                 if (need_mappable) {
  633.                         entry->flags |= __EXEC_OBJECT_NEEDS_MAP;
  634.                         list_move(&vma->exec_list, &ordered_vmas);
  635.                 } else
  636.                         list_move_tail(&vma->exec_list, &ordered_vmas);
  637.  
  638.                 obj->base.pending_read_domains = I915_GEM_GPU_DOMAINS & ~I915_GEM_DOMAIN_COMMAND;
  639.                 obj->base.pending_write_domain = 0;
  640.         }
  641.         list_splice(&ordered_vmas, vmas);
  642.  
  643.         /* Attempt to pin all of the buffers into the GTT.
  644.          * This is done in 3 phases:
  645.          *
  646.          * 1a. Unbind all objects that do not match the GTT constraints for
  647.          *     the execbuffer (fenceable, mappable, alignment etc).
  648.          * 1b. Increment pin count for already bound objects.
  649.          * 2.  Bind new objects.
  650.          * 3.  Decrement pin count.
  651.          *
  652.          * This avoid unnecessary unbinding of later objects in order to make
  653.          * room for the earlier objects *unless* we need to defragment.
  654.          */
  655.         retry = 0;
  656.         do {
  657.                 int ret = 0;
  658.  
  659.                 /* Unbind any ill-fitting objects or pin. */
  660.                 list_for_each_entry(vma, vmas, exec_list) {
  661.                         if (!drm_mm_node_allocated(&vma->node))
  662.                                 continue;
  663.  
  664.                         if (eb_vma_misplaced(vma))
  665.                                 ret = i915_vma_unbind(vma);
  666.                         else
  667.                                 ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  668.                         if (ret)
  669.                                 goto err;
  670.                 }
  671.  
  672.                 /* Bind fresh objects */
  673.                 list_for_each_entry(vma, vmas, exec_list) {
  674.                         if (drm_mm_node_allocated(&vma->node))
  675.                                 continue;
  676.  
  677.                         ret = i915_gem_execbuffer_reserve_vma(vma, ring, need_relocs);
  678.                         if (ret)
  679.                                 goto err;
  680.                 }
  681.  
  682. err:
  683.                 if (ret != -ENOSPC || retry++)
  684.                         return ret;
  685.  
  686.                 /* Decrement pin count for bound objects */
  687.                 list_for_each_entry(vma, vmas, exec_list)
  688.                         i915_gem_execbuffer_unreserve_vma(vma);
  689.  
  690.                 ret = i915_gem_evict_vm(vm, true);
  691.                 if (ret)
  692.                         return ret;
  693.         } while (1);
  694. }
  695.  
  696. static int
  697. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  698.                                   struct drm_i915_gem_execbuffer2 *args,
  699.                                   struct drm_file *file,
  700.                                   struct intel_engine_cs *ring,
  701.                                   struct eb_vmas *eb,
  702.                                   struct drm_i915_gem_exec_object2 *exec)
  703. {
  704.         struct drm_i915_gem_relocation_entry *reloc;
  705.         struct i915_address_space *vm;
  706.         struct i915_vma *vma;
  707.         bool need_relocs;
  708.         int *reloc_offset;
  709.         int i, total, ret;
  710.         unsigned count = args->buffer_count;
  711.  
  712.         vm = list_first_entry(&eb->vmas, struct i915_vma, exec_list)->vm;
  713.  
  714.         /* We may process another execbuffer during the unlock... */
  715.         while (!list_empty(&eb->vmas)) {
  716.                 vma = list_first_entry(&eb->vmas, struct i915_vma, exec_list);
  717.                 list_del_init(&vma->exec_list);
  718.                 i915_gem_execbuffer_unreserve_vma(vma);
  719.                 drm_gem_object_unreference(&vma->obj->base);
  720.         }
  721.  
  722.         mutex_unlock(&dev->struct_mutex);
  723.  
  724.         total = 0;
  725.         for (i = 0; i < count; i++)
  726.                 total += exec[i].relocation_count;
  727.  
  728.     reloc_offset = malloc(count * sizeof(*reloc_offset));
  729.     reloc = malloc(total * sizeof(*reloc));
  730.         if (reloc == NULL || reloc_offset == NULL) {
  731.         kfree(reloc);
  732.         kfree(reloc_offset);
  733.                 mutex_lock(&dev->struct_mutex);
  734.                 return -ENOMEM;
  735.         }
  736.  
  737.         total = 0;
  738.         for (i = 0; i < count; i++) {
  739.                 struct drm_i915_gem_relocation_entry __user *user_relocs;
  740.                 u64 invalid_offset = (u64)-1;
  741.                 int j;
  742.  
  743.                 user_relocs = to_user_ptr(exec[i].relocs_ptr);
  744.  
  745.                 if (copy_from_user(reloc+total, user_relocs,
  746.                                    exec[i].relocation_count * sizeof(*reloc))) {
  747.                         ret = -EFAULT;
  748.                         mutex_lock(&dev->struct_mutex);
  749.                         goto err;
  750.                 }
  751.  
  752.                 /* As we do not update the known relocation offsets after
  753.                  * relocating (due to the complexities in lock handling),
  754.                  * we need to mark them as invalid now so that we force the
  755.                  * relocation processing next time. Just in case the target
  756.                  * object is evicted and then rebound into its old
  757.                  * presumed_offset before the next execbuffer - if that
  758.                  * happened we would make the mistake of assuming that the
  759.                  * relocations were valid.
  760.                  */
  761.                 for (j = 0; j < exec[i].relocation_count; j++) {
  762.                         if (__copy_to_user(&user_relocs[j].presumed_offset,
  763.                                          &invalid_offset,
  764.                                          sizeof(invalid_offset))) {
  765.                                 ret = -EFAULT;
  766.                                 mutex_lock(&dev->struct_mutex);
  767.                                 goto err;
  768.                         }
  769.                 }
  770.  
  771.                 reloc_offset[i] = total;
  772.                 total += exec[i].relocation_count;
  773.         }
  774.  
  775.         ret = i915_mutex_lock_interruptible(dev);
  776.         if (ret) {
  777.                 mutex_lock(&dev->struct_mutex);
  778.                 goto err;
  779.         }
  780.  
  781.         /* reacquire the objects */
  782.         eb_reset(eb);
  783.         ret = eb_lookup_vmas(eb, exec, args, vm, file);
  784.         if (ret)
  785.                         goto err;
  786.  
  787.         need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  788.         ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  789.         if (ret)
  790.                 goto err;
  791.  
  792.         list_for_each_entry(vma, &eb->vmas, exec_list) {
  793.                 int offset = vma->exec_entry - exec;
  794.                 ret = i915_gem_execbuffer_relocate_vma_slow(vma, eb,
  795.                                                             reloc + reloc_offset[offset]);
  796.                 if (ret)
  797.                         goto err;
  798.         }
  799.  
  800.         /* Leave the user relocations as are, this is the painfully slow path,
  801.          * and we want to avoid the complication of dropping the lock whilst
  802.          * having buffers reserved in the aperture and so causing spurious
  803.          * ENOSPC for random operations.
  804.          */
  805.  
  806. err:
  807.     kfree(reloc);
  808.     kfree(reloc_offset);
  809.         return ret;
  810. }
  811.  
  812. static int
  813. i915_gem_execbuffer_move_to_gpu(struct intel_engine_cs *ring,
  814.                                 struct list_head *vmas)
  815. {
  816.         struct i915_vma *vma;
  817.         uint32_t flush_domains = 0;
  818.         bool flush_chipset = false;
  819.         int ret;
  820.  
  821.         list_for_each_entry(vma, vmas, exec_list) {
  822.                 struct drm_i915_gem_object *obj = vma->obj;
  823.                 ret = i915_gem_object_sync(obj, ring);
  824.                 if (ret)
  825.                         return ret;
  826.  
  827.                 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  828.                         flush_chipset |= i915_gem_clflush_object(obj, false);
  829.  
  830.                 flush_domains |= obj->base.write_domain;
  831.         }
  832.  
  833.         if (flush_chipset)
  834.                 i915_gem_chipset_flush(ring->dev);
  835.  
  836.         if (flush_domains & I915_GEM_DOMAIN_GTT)
  837.                 wmb();
  838.  
  839.         /* Unconditionally invalidate gpu caches and ensure that we do flush
  840.          * any residual writes from the previous batch.
  841.          */
  842.         return intel_ring_invalidate_all_caches(ring);
  843. }
  844.  
  845. static bool
  846. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  847. {
  848.         if (exec->flags & __I915_EXEC_UNKNOWN_FLAGS)
  849.                 return false;
  850.  
  851.         return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  852. }
  853.  
  854. static int
  855. validate_exec_list(struct drm_device *dev,
  856.                    struct drm_i915_gem_exec_object2 *exec,
  857.                    int count)
  858. {
  859.         unsigned relocs_total = 0;
  860.         unsigned relocs_max = UINT_MAX / sizeof(struct drm_i915_gem_relocation_entry);
  861.         unsigned invalid_flags;
  862.         int i;
  863.  
  864.         invalid_flags = __EXEC_OBJECT_UNKNOWN_FLAGS;
  865.         if (USES_FULL_PPGTT(dev))
  866.                 invalid_flags |= EXEC_OBJECT_NEEDS_GTT;
  867.  
  868.         for (i = 0; i < count; i++) {
  869.                 char __user *ptr = to_user_ptr(exec[i].relocs_ptr);
  870.                 int length; /* limited by fault_in_pages_readable() */
  871.  
  872.                 if (exec[i].flags & invalid_flags)
  873.                         return -EINVAL;
  874.  
  875.                 /* First check for malicious input causing overflow in
  876.                  * the worst case where we need to allocate the entire
  877.                  * relocation tree as a single array.
  878.                  */
  879.                 if (exec[i].relocation_count > relocs_max - relocs_total)
  880.                         return -EINVAL;
  881.                 relocs_total += exec[i].relocation_count;
  882.  
  883.                 length = exec[i].relocation_count *
  884.                         sizeof(struct drm_i915_gem_relocation_entry);
  885.                 /*
  886.                  * We must check that the entire relocation array is safe
  887.                  * to read, but since we may need to update the presumed
  888.                  * offsets during execution, check for full write access.
  889.                  */
  890.         }
  891.  
  892.         return 0;
  893. }
  894.  
  895. static struct intel_context *
  896. i915_gem_validate_context(struct drm_device *dev, struct drm_file *file,
  897.                           struct intel_engine_cs *ring, const u32 ctx_id)
  898. {
  899.         struct intel_context *ctx = NULL;
  900.         struct i915_ctx_hang_stats *hs;
  901.  
  902.         if (ring->id != RCS && ctx_id != DEFAULT_CONTEXT_HANDLE)
  903.                 return ERR_PTR(-EINVAL);
  904.  
  905.         ctx = i915_gem_context_get(file->driver_priv, ctx_id);
  906.         if (IS_ERR(ctx))
  907.                 return ctx;
  908.  
  909.         hs = &ctx->hang_stats;
  910.         if (hs->banned) {
  911.                 DRM_DEBUG("Context %u tried to submit while banned\n", ctx_id);
  912.                 return ERR_PTR(-EIO);
  913.         }
  914.  
  915.         if (i915.enable_execlists && !ctx->engine[ring->id].state) {
  916.                 int ret = intel_lr_context_deferred_create(ctx, ring);
  917.                 if (ret) {
  918.                         DRM_DEBUG("Could not create LRC %u: %d\n", ctx_id, ret);
  919.                         return ERR_PTR(ret);
  920.                 }
  921.         }
  922.  
  923.         return ctx;
  924. }
  925.  
  926. void
  927. i915_gem_execbuffer_move_to_active(struct list_head *vmas,
  928.                                    struct intel_engine_cs *ring)
  929. {
  930.         u32 seqno = intel_ring_get_seqno(ring);
  931.         struct i915_vma *vma;
  932.  
  933.         list_for_each_entry(vma, vmas, exec_list) {
  934.                 struct drm_i915_gem_exec_object2 *entry = vma->exec_entry;
  935.                 struct drm_i915_gem_object *obj = vma->obj;
  936.                 u32 old_read = obj->base.read_domains;
  937.                 u32 old_write = obj->base.write_domain;
  938.  
  939.                 obj->base.write_domain = obj->base.pending_write_domain;
  940.                 if (obj->base.write_domain == 0)
  941.                         obj->base.pending_read_domains |= obj->base.read_domains;
  942.                 obj->base.read_domains = obj->base.pending_read_domains;
  943.  
  944.                 i915_vma_move_to_active(vma, ring);
  945.                 if (obj->base.write_domain) {
  946.                         obj->dirty = 1;
  947.                         obj->last_write_seqno = seqno;
  948.  
  949.                         intel_fb_obj_invalidate(obj, ring);
  950.  
  951.                         /* update for the implicit flush after a batch */
  952.                         obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
  953.                 }
  954.                 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  955.                         obj->last_fenced_seqno = seqno;
  956.                         if (entry->flags & __EXEC_OBJECT_HAS_FENCE) {
  957.                                 struct drm_i915_private *dev_priv = to_i915(ring->dev);
  958.                                 list_move_tail(&dev_priv->fence_regs[obj->fence_reg].lru_list,
  959.                                                &dev_priv->mm.fence_list);
  960.                         }
  961.                 }
  962.  
  963.                 trace_i915_gem_object_change_domain(obj, old_read, old_write);
  964.         }
  965. }
  966.  
  967. void
  968. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  969.                                     struct drm_file *file,
  970.                                     struct intel_engine_cs *ring,
  971.                                     struct drm_i915_gem_object *obj)
  972. {
  973.         /* Unconditionally force add_request to emit a full flush. */
  974.         ring->gpu_caches_dirty = true;
  975.  
  976.         /* Add a breadcrumb for the completion of the batch buffer */
  977.         (void)__i915_add_request(ring, file, obj, NULL);
  978. }
  979.  
  980. static int
  981. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  982.                             struct intel_engine_cs *ring)
  983. {
  984.         struct drm_i915_private *dev_priv = dev->dev_private;
  985.         int ret, i;
  986.  
  987.         if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS]) {
  988.                 DRM_DEBUG("sol reset is gen7/rcs only\n");
  989.                 return -EINVAL;
  990.         }
  991.  
  992.         ret = intel_ring_begin(ring, 4 * 3);
  993.         if (ret)
  994.                 return ret;
  995.  
  996.         for (i = 0; i < 4; i++) {
  997.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  998.                 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  999.                 intel_ring_emit(ring, 0);
  1000.         }
  1001.  
  1002.         intel_ring_advance(ring);
  1003.  
  1004.         return 0;
  1005. }
  1006.  
  1007. static int
  1008. i915_emit_box(struct intel_engine_cs *ring,
  1009.               struct drm_clip_rect *box,
  1010.               int DR1, int DR4)
  1011. {
  1012.         int ret;
  1013.  
  1014.         if (box->y2 <= box->y1 || box->x2 <= box->x1 ||
  1015.             box->y2 <= 0 || box->x2 <= 0) {
  1016.                 DRM_ERROR("Bad box %d,%d..%d,%d\n",
  1017.                           box->x1, box->y1, box->x2, box->y2);
  1018.                 return -EINVAL;
  1019.         }
  1020.  
  1021.         if (INTEL_INFO(ring->dev)->gen >= 4) {
  1022.                 ret = intel_ring_begin(ring, 4);
  1023.                 if (ret)
  1024.                         return ret;
  1025.  
  1026.                 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO_I965);
  1027.                 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
  1028.                 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
  1029.                 intel_ring_emit(ring, DR4);
  1030.         } else {
  1031.                 ret = intel_ring_begin(ring, 6);
  1032.                 if (ret)
  1033.                         return ret;
  1034.  
  1035.                 intel_ring_emit(ring, GFX_OP_DRAWRECT_INFO);
  1036.                 intel_ring_emit(ring, DR1);
  1037.                 intel_ring_emit(ring, (box->x1 & 0xffff) | box->y1 << 16);
  1038.                 intel_ring_emit(ring, ((box->x2 - 1) & 0xffff) | (box->y2 - 1) << 16);
  1039.                 intel_ring_emit(ring, DR4);
  1040.                 intel_ring_emit(ring, 0);
  1041.         }
  1042.         intel_ring_advance(ring);
  1043.  
  1044.         return 0;
  1045. }
  1046.  
  1047.  
  1048. int
  1049. i915_gem_ringbuffer_submission(struct drm_device *dev, struct drm_file *file,
  1050.                              struct intel_engine_cs *ring,
  1051.                              struct intel_context *ctx,
  1052.                              struct drm_i915_gem_execbuffer2 *args,
  1053.                              struct list_head *vmas,
  1054.                              struct drm_i915_gem_object *batch_obj,
  1055.                              u64 exec_start, u32 flags)
  1056. {
  1057.         struct drm_clip_rect *cliprects = NULL;
  1058.         struct drm_i915_private *dev_priv = dev->dev_private;
  1059.         u64 exec_len;
  1060.         int instp_mode;
  1061.         u32 instp_mask;
  1062.         int i, ret = 0;
  1063.  
  1064.         if (args->num_cliprects != 0) {
  1065.                 if (ring != &dev_priv->ring[RCS]) {
  1066.                         DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  1067.                         return -EINVAL;
  1068.                 }
  1069.  
  1070.                 if (INTEL_INFO(dev)->gen >= 5) {
  1071.                         DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  1072.                         return -EINVAL;
  1073.                 }
  1074.  
  1075.                 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  1076.                         DRM_DEBUG("execbuf with %u cliprects\n",
  1077.                                   args->num_cliprects);
  1078.                         return -EINVAL;
  1079.                 }
  1080.  
  1081.                 cliprects = kcalloc(args->num_cliprects,
  1082.                                     sizeof(*cliprects),
  1083.                                     GFP_KERNEL);
  1084.                 if (cliprects == NULL) {
  1085.                         ret = -ENOMEM;
  1086.                         goto error;
  1087.                 }
  1088.  
  1089.                 if (copy_from_user(cliprects,
  1090.                                    to_user_ptr(args->cliprects_ptr),
  1091.                                    sizeof(*cliprects)*args->num_cliprects)) {
  1092.                         ret = -EFAULT;
  1093.                         goto error;
  1094.                 }
  1095.         } else {
  1096.                 if (args->DR4 == 0xffffffff) {
  1097.                         DRM_DEBUG("UXA submitting garbage DR4, fixing up\n");
  1098.                         args->DR4 = 0;
  1099.                 }
  1100.  
  1101.                 if (args->DR1 || args->DR4 || args->cliprects_ptr) {
  1102.                         DRM_DEBUG("0 cliprects but dirt in cliprects fields\n");
  1103.                         return -EINVAL;
  1104.                 }
  1105.         }
  1106.  
  1107.         ret = i915_gem_execbuffer_move_to_gpu(ring, vmas);
  1108.         if (ret)
  1109.                 goto error;
  1110.  
  1111.         ret = i915_switch_context(ring, ctx);
  1112.         if (ret)
  1113.                 goto error;
  1114.  
  1115.         instp_mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  1116.         instp_mask = I915_EXEC_CONSTANTS_MASK;
  1117.         switch (instp_mode) {
  1118.         case I915_EXEC_CONSTANTS_REL_GENERAL:
  1119.         case I915_EXEC_CONSTANTS_ABSOLUTE:
  1120.         case I915_EXEC_CONSTANTS_REL_SURFACE:
  1121.                 if (instp_mode != 0 && ring != &dev_priv->ring[RCS]) {
  1122.                         DRM_DEBUG("non-0 rel constants mode on non-RCS\n");
  1123.                         ret = -EINVAL;
  1124.                         goto error;
  1125.                 }
  1126.  
  1127.                 if (instp_mode != dev_priv->relative_constants_mode) {
  1128.                         if (INTEL_INFO(dev)->gen < 4) {
  1129.                                 DRM_DEBUG("no rel constants on pre-gen4\n");
  1130.                                 ret = -EINVAL;
  1131.                                 goto error;
  1132.                         }
  1133.  
  1134.                         if (INTEL_INFO(dev)->gen > 5 &&
  1135.                             instp_mode == I915_EXEC_CONSTANTS_REL_SURFACE) {
  1136.                                 DRM_DEBUG("rel surface constants mode invalid on gen5+\n");
  1137.                                 ret = -EINVAL;
  1138.                                 goto error;
  1139.                         }
  1140.  
  1141.                         /* The HW changed the meaning on this bit on gen6 */
  1142.                         if (INTEL_INFO(dev)->gen >= 6)
  1143.                                 instp_mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  1144.                 }
  1145.                 break;
  1146.         default:
  1147.                 DRM_DEBUG("execbuf with unknown constants: %d\n", instp_mode);
  1148.                 ret = -EINVAL;
  1149.                 goto error;
  1150.         }
  1151.  
  1152.         if (ring == &dev_priv->ring[RCS] &&
  1153.                         instp_mode != dev_priv->relative_constants_mode) {
  1154.                 ret = intel_ring_begin(ring, 4);
  1155.                 if (ret)
  1156.                         goto error;
  1157.  
  1158.                 intel_ring_emit(ring, MI_NOOP);
  1159.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1160.                 intel_ring_emit(ring, INSTPM);
  1161.                 intel_ring_emit(ring, instp_mask << 16 | instp_mode);
  1162.                 intel_ring_advance(ring);
  1163.  
  1164.                 dev_priv->relative_constants_mode = instp_mode;
  1165.         }
  1166.  
  1167.         if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1168.                 ret = i915_reset_gen7_sol_offsets(dev, ring);
  1169.                 if (ret)
  1170.                         goto error;
  1171.         }
  1172.  
  1173.         exec_len = args->batch_len;
  1174.         if (cliprects) {
  1175.                 for (i = 0; i < args->num_cliprects; i++) {
  1176.                         ret = i915_emit_box(ring, &cliprects[i],
  1177.                                             args->DR1, args->DR4);
  1178.                         if (ret)
  1179.                                 goto error;
  1180.  
  1181.                         ret = ring->dispatch_execbuffer(ring,
  1182.                                                         exec_start, exec_len,
  1183.                                                         flags);
  1184.                         if (ret)
  1185.                                 goto error;
  1186.                 }
  1187.         } else {
  1188.                 ret = ring->dispatch_execbuffer(ring,
  1189.                                                 exec_start, exec_len,
  1190.                                                 flags);
  1191.                 if (ret)
  1192.                         return ret;
  1193.         }
  1194.  
  1195.         trace_i915_gem_ring_dispatch(ring, intel_ring_get_seqno(ring), flags);
  1196.  
  1197.         i915_gem_execbuffer_move_to_active(vmas, ring);
  1198.         i915_gem_execbuffer_retire_commands(dev, file, ring, batch_obj);
  1199.  
  1200. error:
  1201.         kfree(cliprects);
  1202.         return ret;
  1203. }
  1204.  
  1205. /**
  1206.  * Find one BSD ring to dispatch the corresponding BSD command.
  1207.  * The Ring ID is returned.
  1208.  */
  1209. static int gen8_dispatch_bsd_ring(struct drm_device *dev,
  1210.                                   struct drm_file *file)
  1211. {
  1212.         struct drm_i915_private *dev_priv = dev->dev_private;
  1213.         struct drm_i915_file_private *file_priv = file->driver_priv;
  1214.  
  1215.         /* Check whether the file_priv is using one ring */
  1216.         if (file_priv->bsd_ring)
  1217.                 return file_priv->bsd_ring->id;
  1218.         else {
  1219.                 /* If no, use the ping-pong mechanism to select one ring */
  1220.                 int ring_id;
  1221.  
  1222.                 mutex_lock(&dev->struct_mutex);
  1223.                 if (dev_priv->mm.bsd_ring_dispatch_index == 0) {
  1224.                         ring_id = VCS;
  1225.                         dev_priv->mm.bsd_ring_dispatch_index = 1;
  1226.                 } else {
  1227.                         ring_id = VCS2;
  1228.                         dev_priv->mm.bsd_ring_dispatch_index = 0;
  1229.                 }
  1230.                 file_priv->bsd_ring = &dev_priv->ring[ring_id];
  1231.                 mutex_unlock(&dev->struct_mutex);
  1232.                 return ring_id;
  1233.         }
  1234. }
  1235.  
  1236. static struct drm_i915_gem_object *
  1237. eb_get_batch(struct eb_vmas *eb)
  1238. {
  1239.         struct i915_vma *vma = list_entry(eb->vmas.prev, typeof(*vma), exec_list);
  1240.  
  1241.         /*
  1242.          * SNA is doing fancy tricks with compressing batch buffers, which leads
  1243.          * to negative relocation deltas. Usually that works out ok since the
  1244.          * relocate address is still positive, except when the batch is placed
  1245.          * very low in the GTT. Ensure this doesn't happen.
  1246.          *
  1247.          * Note that actual hangs have only been observed on gen7, but for
  1248.          * paranoia do it everywhere.
  1249.          */
  1250.         vma->exec_entry->flags |= __EXEC_OBJECT_NEEDS_BIAS;
  1251.  
  1252.         return vma->obj;
  1253. }
  1254.  
  1255. static int
  1256. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  1257.                        struct drm_file *file,
  1258.                        struct drm_i915_gem_execbuffer2 *args,
  1259.                        struct drm_i915_gem_exec_object2 *exec)
  1260. {
  1261.         struct drm_i915_private *dev_priv = dev->dev_private;
  1262.         struct eb_vmas *eb;
  1263.         struct drm_i915_gem_object *batch_obj;
  1264.         struct intel_engine_cs *ring;
  1265.         struct intel_context *ctx;
  1266.         struct i915_address_space *vm;
  1267.         const u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  1268.         u64 exec_start = args->batch_start_offset;
  1269.         u32 flags;
  1270.         int ret;
  1271.         bool need_relocs;
  1272.  
  1273.         if (!i915_gem_check_execbuffer(args))
  1274.                 return -EINVAL;
  1275.  
  1276.         ret = validate_exec_list(dev, exec, args->buffer_count);
  1277.         if (ret)
  1278.                 return ret;
  1279.  
  1280.         flags = 0;
  1281.         if (args->flags & I915_EXEC_SECURE) {
  1282.  
  1283.                 flags |= I915_DISPATCH_SECURE;
  1284.         }
  1285.         if (args->flags & I915_EXEC_IS_PINNED)
  1286.                 flags |= I915_DISPATCH_PINNED;
  1287.  
  1288.         if ((args->flags & I915_EXEC_RING_MASK) > LAST_USER_RING) {
  1289.                 DRM_DEBUG("execbuf with unknown ring: %d\n",
  1290.                           (int)(args->flags & I915_EXEC_RING_MASK));
  1291.                 return -EINVAL;
  1292.         }
  1293.  
  1294.         if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_DEFAULT)
  1295.                 ring = &dev_priv->ring[RCS];
  1296.         else if ((args->flags & I915_EXEC_RING_MASK) == I915_EXEC_BSD) {
  1297.                 if (HAS_BSD2(dev)) {
  1298.                         int ring_id;
  1299.                         ring_id = gen8_dispatch_bsd_ring(dev, file);
  1300.                         ring = &dev_priv->ring[ring_id];
  1301.                 } else
  1302.                         ring = &dev_priv->ring[VCS];
  1303.         } else
  1304.                 ring = &dev_priv->ring[(args->flags & I915_EXEC_RING_MASK) - 1];
  1305.  
  1306.         if (!intel_ring_initialized(ring)) {
  1307.                 DRM_DEBUG("execbuf with invalid ring: %d\n",
  1308.                           (int)(args->flags & I915_EXEC_RING_MASK));
  1309.                 return -EINVAL;
  1310.         }
  1311.  
  1312.         if (args->buffer_count < 1) {
  1313.                 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1314.                 return -EINVAL;
  1315.         }
  1316.  
  1317.         intel_runtime_pm_get(dev_priv);
  1318.  
  1319.         ret = i915_mutex_lock_interruptible(dev);
  1320.         if (ret)
  1321.                 goto pre_mutex_err;
  1322.  
  1323.         ctx = i915_gem_validate_context(dev, file, ring, ctx_id);
  1324.         if (IS_ERR(ctx)) {
  1325.                 mutex_unlock(&dev->struct_mutex);
  1326.                 ret = PTR_ERR(ctx);
  1327.                 goto pre_mutex_err;
  1328.         }
  1329.  
  1330.         i915_gem_context_reference(ctx);
  1331.  
  1332.         if (ctx->ppgtt)
  1333.                 vm = &ctx->ppgtt->base;
  1334.         else
  1335.                 vm = &dev_priv->gtt.base;
  1336.  
  1337.         eb = eb_create(args);
  1338.         if (eb == NULL) {
  1339.                 i915_gem_context_unreference(ctx);
  1340.                 mutex_unlock(&dev->struct_mutex);
  1341.                 ret = -ENOMEM;
  1342.         goto pre_mutex_err;
  1343.         }
  1344.  
  1345.         /* Look up object handles */
  1346.         ret = eb_lookup_vmas(eb, exec, args, vm, file);
  1347.         if (ret)
  1348.                         goto err;
  1349.  
  1350.         /* take note of the batch buffer before we might reorder the lists */
  1351.         batch_obj = eb_get_batch(eb);
  1352.  
  1353.         /* Move the objects en-masse into the GTT, evicting if necessary. */
  1354.         need_relocs = (args->flags & I915_EXEC_NO_RELOC) == 0;
  1355.         ret = i915_gem_execbuffer_reserve(ring, &eb->vmas, &need_relocs);
  1356.         if (ret)
  1357.                 goto err;
  1358.  
  1359.         /* The objects are in their final locations, apply the relocations. */
  1360.         if (need_relocs)
  1361.                 ret = i915_gem_execbuffer_relocate(eb);
  1362.         if (ret) {
  1363.                 if (ret == -EFAULT) {
  1364.                         ret = i915_gem_execbuffer_relocate_slow(dev, args, file, ring,
  1365.                                                                 eb, exec);
  1366.                         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1367.                 }
  1368.                 if (ret)
  1369.                         goto err;
  1370.         }
  1371.  
  1372.         /* Set the pending read domains for the batch buffer to COMMAND */
  1373.         if (batch_obj->base.pending_write_domain) {
  1374.                 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1375.                 ret = -EINVAL;
  1376.                 goto err;
  1377.         }
  1378.         batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1379. #if 0
  1380.         if (i915_needs_cmd_parser(ring)) {
  1381.                 ret = i915_parse_cmds(ring,
  1382.                                       batch_obj,
  1383.                                       args->batch_start_offset,
  1384.                                       file->is_master);
  1385.                 if (ret) {
  1386.                         if (ret != -EACCES)
  1387.                         goto err;
  1388.                 } else {
  1389.                 /*
  1390.                  * XXX: Actually do this when enabling batch copy...
  1391.                  *
  1392.                  * Set the DISPATCH_SECURE bit to remove the NON_SECURE bit
  1393.                  * from MI_BATCH_BUFFER_START commands issued in the
  1394.                  * dispatch_execbuffer implementations. We specifically don't
  1395.                  * want that set when the command parser is enabled.
  1396.                  */
  1397.         }
  1398.         }
  1399.  
  1400. #endif
  1401.         /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1402.          * batch" bit. Hence we need to pin secure batches into the global gtt.
  1403.          * hsw should have this fixed, but bdw mucks it up again. */
  1404.         if (flags & I915_DISPATCH_SECURE) {
  1405.                 /*
  1406.                  * So on first glance it looks freaky that we pin the batch here
  1407.                  * outside of the reservation loop. But:
  1408.                  * - The batch is already pinned into the relevant ppgtt, so we
  1409.                  *   already have the backing storage fully allocated.
  1410.                  * - No other BO uses the global gtt (well contexts, but meh),
  1411.                  *   so we don't really have issues with mutliple objects not
  1412.                  *   fitting due to fragmentation.
  1413.                  * So this is actually safe.
  1414.                  */
  1415.                 ret = i915_gem_obj_ggtt_pin(batch_obj, 0, 0);
  1416.                 if (ret)
  1417.                         goto err;
  1418.  
  1419.                 exec_start += i915_gem_obj_ggtt_offset(batch_obj);
  1420.         } else
  1421.                 exec_start += i915_gem_obj_offset(batch_obj, vm);
  1422.  
  1423.         ret = dev_priv->gt.do_execbuf(dev, file, ring, ctx, args,
  1424.                                       &eb->vmas, batch_obj, exec_start, flags);
  1425.  
  1426.         /*
  1427.          * FIXME: We crucially rely upon the active tracking for the (ppgtt)
  1428.          * batch vma for correctness. For less ugly and less fragility this
  1429.          * needs to be adjusted to also track the ggtt batch vma properly as
  1430.          * active.
  1431.          */
  1432.         if (flags & I915_DISPATCH_SECURE)
  1433.                 i915_gem_object_ggtt_unpin(batch_obj);
  1434. err:
  1435.         /* the request owns the ref now */
  1436.         i915_gem_context_unreference(ctx);
  1437.         eb_destroy(eb);
  1438.  
  1439.         mutex_unlock(&dev->struct_mutex);
  1440.  
  1441. pre_mutex_err:
  1442.         /* intel_gpu_busy should also get a ref, so it will free when the device
  1443.          * is really idle. */
  1444.         intel_runtime_pm_put(dev_priv);
  1445.         return ret;
  1446. }
  1447.  
  1448. #if 0
  1449. /*
  1450.  * Legacy execbuffer just creates an exec2 list from the original exec object
  1451.  * list array and passes it to the real function.
  1452.  */
  1453. int
  1454. i915_gem_execbuffer(struct drm_device *dev, void *data,
  1455.                     struct drm_file *file)
  1456. {
  1457.         struct drm_i915_gem_execbuffer *args = data;
  1458.         struct drm_i915_gem_execbuffer2 exec2;
  1459.         struct drm_i915_gem_exec_object *exec_list = NULL;
  1460.         struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1461.         int ret, i;
  1462.  
  1463.         if (args->buffer_count < 1) {
  1464.                 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  1465.                 return -EINVAL;
  1466.         }
  1467.  
  1468.         /* Copy in the exec list from userland */
  1469.         exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
  1470.         exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
  1471.         if (exec_list == NULL || exec2_list == NULL) {
  1472.                 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1473.                           args->buffer_count);
  1474.                 drm_free_large(exec_list);
  1475.                 drm_free_large(exec2_list);
  1476.                 return -ENOMEM;
  1477.         }
  1478.         ret = copy_from_user(exec_list,
  1479.                              to_user_ptr(args->buffers_ptr),
  1480.                              sizeof(*exec_list) * args->buffer_count);
  1481.         if (ret != 0) {
  1482.                 DRM_DEBUG("copy %d exec entries failed %d\n",
  1483.                           args->buffer_count, ret);
  1484.                 drm_free_large(exec_list);
  1485.                 drm_free_large(exec2_list);
  1486.                 return -EFAULT;
  1487.         }
  1488.  
  1489.         for (i = 0; i < args->buffer_count; i++) {
  1490.                 exec2_list[i].handle = exec_list[i].handle;
  1491.                 exec2_list[i].relocation_count = exec_list[i].relocation_count;
  1492.                 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
  1493.                 exec2_list[i].alignment = exec_list[i].alignment;
  1494.                 exec2_list[i].offset = exec_list[i].offset;
  1495.                 if (INTEL_INFO(dev)->gen < 4)
  1496.                         exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
  1497.                 else
  1498.                         exec2_list[i].flags = 0;
  1499.         }
  1500.  
  1501.         exec2.buffers_ptr = args->buffers_ptr;
  1502.         exec2.buffer_count = args->buffer_count;
  1503.         exec2.batch_start_offset = args->batch_start_offset;
  1504.         exec2.batch_len = args->batch_len;
  1505.         exec2.DR1 = args->DR1;
  1506.         exec2.DR4 = args->DR4;
  1507.         exec2.num_cliprects = args->num_cliprects;
  1508.         exec2.cliprects_ptr = args->cliprects_ptr;
  1509.         exec2.flags = I915_EXEC_RENDER;
  1510.         i915_execbuffer2_set_context_id(exec2, 0);
  1511.  
  1512.         ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
  1513.         if (!ret) {
  1514.                 struct drm_i915_gem_exec_object __user *user_exec_list =
  1515.                         to_user_ptr(args->buffers_ptr);
  1516.  
  1517.                 /* Copy the new buffer offsets back to the user's exec list. */
  1518.                 for (i = 0; i < args->buffer_count; i++) {
  1519.                         ret = __copy_to_user(&user_exec_list[i].offset,
  1520.                                              &exec2_list[i].offset,
  1521.                                              sizeof(user_exec_list[i].offset));
  1522.                 if (ret) {
  1523.                         ret = -EFAULT;
  1524.                         DRM_DEBUG("failed to copy %d exec entries "
  1525.                                   "back to user (%d)\n",
  1526.                                   args->buffer_count, ret);
  1527.                                 break;
  1528.                         }
  1529.                 }
  1530.         }
  1531.  
  1532.         drm_free_large(exec_list);
  1533.         drm_free_large(exec2_list);
  1534.         return ret;
  1535. }
  1536. #endif
  1537.  
  1538. int
  1539. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1540.                      struct drm_file *file)
  1541. {
  1542.         struct drm_i915_gem_execbuffer2 *args = data;
  1543.         struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1544.         int ret;
  1545.  
  1546.         if (args->buffer_count < 1 ||
  1547.             args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1548.                 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1549.                 return -EINVAL;
  1550.         }
  1551.  
  1552.         if (args->rsvd2 != 0) {
  1553.                 DRM_DEBUG("dirty rvsd2 field\n");
  1554.                 return -EINVAL;
  1555.         }
  1556.  
  1557.         exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count,
  1558.                              GFP_TEMPORARY | __GFP_NOWARN | __GFP_NORETRY);
  1559.         if (exec2_list == NULL) {
  1560.                 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1561.                           args->buffer_count);
  1562.                 return -ENOMEM;
  1563.         }
  1564.         ret = copy_from_user(exec2_list,
  1565.                              to_user_ptr(args->buffers_ptr),
  1566.                              sizeof(*exec2_list) * args->buffer_count);
  1567.         if (ret != 0) {
  1568.                 DRM_DEBUG("copy %d exec entries failed %d\n",
  1569.                           args->buffer_count, ret);
  1570.         kfree(exec2_list);
  1571.         FAIL();
  1572.                 return -EFAULT;
  1573.         }
  1574.  
  1575.         ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1576.         if (!ret) {
  1577.                 /* Copy the new buffer offsets back to the user's exec list. */
  1578.                 struct drm_i915_gem_exec_object2 __user *user_exec_list =
  1579.                                    to_user_ptr(args->buffers_ptr);
  1580.                 int i;
  1581.  
  1582.                 for (i = 0; i < args->buffer_count; i++) {
  1583.                         ret = __copy_to_user(&user_exec_list[i].offset,
  1584.                                              &exec2_list[i].offset,
  1585.                                              sizeof(user_exec_list[i].offset));
  1586.                 if (ret) {
  1587.                         ret = -EFAULT;
  1588.                         DRM_DEBUG("failed to copy %d exec entries "
  1589.                                           "back to user\n",
  1590.                                           args->buffer_count);
  1591.                                 break;
  1592.                         }
  1593.                 }
  1594.         }
  1595.  
  1596.     kfree(exec2_list);
  1597.         return ret;
  1598. }
  1599.