Subversion Repositories Kolibri OS

Rev

Rev 3266 | 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. #define iowrite32(v, addr)      writel((v), (addr))
  30.  
  31. #include <drm/drmP.h>
  32. #include <drm/i915_drm.h>
  33. #include "i915_drv.h"
  34. #include "i915_trace.h"
  35. #include "intel_drv.h"
  36. //#include <linux/dma_remapping.h>
  37.  
  38. #define I915_EXEC_SECURE        (1<<9)
  39. #define I915_EXEC_IS_PINNED     (1<<10)
  40.  
  41. #define wmb() asm volatile ("sfence")
  42.  
  43. struct drm_i915_gem_object *get_fb_obj();
  44.  
  45. static inline __attribute__((const))
  46. bool is_power_of_2(unsigned long n)
  47. {
  48.     return (n != 0 && ((n & (n - 1)) == 0));
  49. }
  50.  
  51. static unsigned long
  52. copy_to_user(void __user *to, const void *from, unsigned long n)
  53. {
  54.     memcpy(to, from, n);
  55.     return 0;
  56. }
  57.  
  58. static unsigned long
  59. copy_from_user(void *to, const void __user *from, unsigned long n)
  60. {
  61.     memcpy(to, from, n);
  62.     return 0;
  63. }
  64.  
  65. struct eb_objects {
  66.         int and;
  67.         struct hlist_head buckets[0];
  68. };
  69.  
  70. static struct eb_objects *
  71. eb_create(int size)
  72. {
  73.         struct eb_objects *eb;
  74.         int count = PAGE_SIZE / sizeof(struct hlist_head) / 2;
  75.         BUILD_BUG_ON(!is_power_of_2(PAGE_SIZE / sizeof(struct hlist_head)));
  76.         while (count > size)
  77.                 count >>= 1;
  78.         eb = kzalloc(count*sizeof(struct hlist_head) +
  79.                      sizeof(struct eb_objects),
  80.                      GFP_KERNEL);
  81.         if (eb == NULL)
  82.                 return eb;
  83.  
  84.         eb->and = count - 1;
  85.         return eb;
  86. }
  87.  
  88. static void
  89. eb_reset(struct eb_objects *eb)
  90. {
  91.         memset(eb->buckets, 0, (eb->and+1)*sizeof(struct hlist_head));
  92. }
  93.  
  94. static void
  95. eb_add_object(struct eb_objects *eb, struct drm_i915_gem_object *obj)
  96. {
  97.         hlist_add_head(&obj->exec_node,
  98.                        &eb->buckets[obj->exec_handle & eb->and]);
  99. }
  100.  
  101. static struct drm_i915_gem_object *
  102. eb_get_object(struct eb_objects *eb, unsigned long handle)
  103. {
  104.         struct hlist_head *head;
  105.         struct hlist_node *node;
  106.         struct drm_i915_gem_object *obj;
  107.  
  108.         head = &eb->buckets[handle & eb->and];
  109.         hlist_for_each(node, head) {
  110.                 obj = hlist_entry(node, struct drm_i915_gem_object, exec_node);
  111.                 if (obj->exec_handle == handle)
  112.                         return obj;
  113.         }
  114.  
  115.         return NULL;
  116. }
  117.  
  118. static void
  119. eb_destroy(struct eb_objects *eb)
  120. {
  121.         kfree(eb);
  122. }
  123.  
  124. static inline int use_cpu_reloc(struct drm_i915_gem_object *obj)
  125. {
  126.         return (obj->base.write_domain == I915_GEM_DOMAIN_CPU ||
  127.                 !obj->map_and_fenceable ||
  128.                 obj->cache_level != I915_CACHE_NONE);
  129. }
  130.  
  131. static int
  132. i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
  133.                                    struct eb_objects *eb,
  134.                                    struct drm_i915_gem_relocation_entry *reloc)
  135. {
  136.         struct drm_device *dev = obj->base.dev;
  137.         struct drm_gem_object *target_obj;
  138.         struct drm_i915_gem_object *target_i915_obj;
  139.         uint32_t target_offset;
  140.         int ret = -EINVAL;
  141.  
  142.         /* we've already hold a reference to all valid objects */
  143.         target_obj = &eb_get_object(eb, reloc->target_handle)->base;
  144.         if (unlikely(target_obj == NULL))
  145.                 return -ENOENT;
  146.  
  147.         target_i915_obj = to_intel_bo(target_obj);
  148.         target_offset = target_i915_obj->gtt_offset;
  149.  
  150.         /* Sandybridge PPGTT errata: We need a global gtt mapping for MI and
  151.          * pipe_control writes because the gpu doesn't properly redirect them
  152.          * through the ppgtt for non_secure batchbuffers. */
  153.         if (unlikely(IS_GEN6(dev) &&
  154.             reloc->write_domain == I915_GEM_DOMAIN_INSTRUCTION &&
  155.             !target_i915_obj->has_global_gtt_mapping)) {
  156.                 i915_gem_gtt_bind_object(target_i915_obj,
  157.                                          target_i915_obj->cache_level);
  158.         }
  159.  
  160.         /* Validate that the target is in a valid r/w GPU domain */
  161.         if (unlikely(reloc->write_domain & (reloc->write_domain - 1))) {
  162.                 DRM_DEBUG("reloc with multiple write domains: "
  163.                           "obj %p target %d offset %d "
  164.                           "read %08x write %08x",
  165.                           obj, reloc->target_handle,
  166.                           (int) reloc->offset,
  167.                           reloc->read_domains,
  168.                           reloc->write_domain);
  169.                 return ret;
  170.         }
  171.         if (unlikely((reloc->write_domain | reloc->read_domains)
  172.                      & ~I915_GEM_GPU_DOMAINS)) {
  173.                 DRM_DEBUG("reloc with read/write non-GPU domains: "
  174.                           "obj %p target %d offset %d "
  175.                           "read %08x write %08x",
  176.                           obj, reloc->target_handle,
  177.                           (int) reloc->offset,
  178.                           reloc->read_domains,
  179.                           reloc->write_domain);
  180.                 return ret;
  181.         }
  182.         if (unlikely(reloc->write_domain && target_obj->pending_write_domain &&
  183.                      reloc->write_domain != target_obj->pending_write_domain)) {
  184.                 DRM_DEBUG("Write domain conflict: "
  185.                           "obj %p target %d offset %d "
  186.                           "new %08x old %08x\n",
  187.                           obj, reloc->target_handle,
  188.                           (int) reloc->offset,
  189.                           reloc->write_domain,
  190.                           target_obj->pending_write_domain);
  191.                 return ret;
  192.         }
  193.  
  194.         target_obj->pending_read_domains |= reloc->read_domains;
  195.         target_obj->pending_write_domain |= reloc->write_domain;
  196.  
  197.         /* If the relocation already has the right value in it, no
  198.          * more work needs to be done.
  199.          */
  200.         if (target_offset == reloc->presumed_offset)
  201.                 return 0;
  202.  
  203.         /* Check that the relocation address is valid... */
  204.         if (unlikely(reloc->offset > obj->base.size - 4)) {
  205.                 DRM_DEBUG("Relocation beyond object bounds: "
  206.                           "obj %p target %d offset %d size %d.\n",
  207.                           obj, reloc->target_handle,
  208.                           (int) reloc->offset,
  209.                           (int) obj->base.size);
  210.                 return ret;
  211.         }
  212.         if (unlikely(reloc->offset & 3)) {
  213.                 DRM_DEBUG("Relocation not 4-byte aligned: "
  214.                           "obj %p target %d offset %d.\n",
  215.                           obj, reloc->target_handle,
  216.                           (int) reloc->offset);
  217.                 return ret;
  218.         }
  219.  
  220.         /* We can't wait for rendering with pagefaults disabled */
  221. //   if (obj->active && in_atomic())
  222. //       return -EFAULT;
  223.  
  224.         reloc->delta += target_offset;
  225.         if (use_cpu_reloc(obj)) {
  226.                 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
  227.                 char *vaddr;
  228.  
  229.                 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
  230.                 if (ret)
  231.                         return ret;
  232.  
  233.         vaddr = (char *)MapIoMem((addr_t)i915_gem_object_get_page(obj,
  234.                                  reloc->offset >> PAGE_SHIFT), 4096, 3);
  235.                 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
  236.         FreeKernelSpace(vaddr);
  237.         } else {
  238.                 struct drm_i915_private *dev_priv = dev->dev_private;
  239.                 uint32_t __iomem *reloc_entry;
  240.                 void __iomem *reloc_page;
  241.  
  242.                 ret = i915_gem_object_set_to_gtt_domain(obj, true);
  243.                 if (ret)
  244.                         return ret;
  245.  
  246.                 ret = i915_gem_object_put_fence(obj);
  247.                 if (ret)
  248.                         return ret;
  249.  
  250.                 /* Map the page containing the relocation we're going to perform.  */
  251.                 reloc->offset += obj->gtt_offset;
  252.         reloc_page = (void*)MapIoMem(reloc->offset & PAGE_MASK, 4096, 3);
  253.                 reloc_entry = (uint32_t __iomem *)
  254.                         (reloc_page + (reloc->offset & ~PAGE_MASK));
  255.                 iowrite32(reloc->delta, reloc_entry);
  256.         FreeKernelSpace(reloc_page);
  257.         }
  258.  
  259.         /* and update the user's relocation entry */
  260.         reloc->presumed_offset = target_offset;
  261.  
  262.         return 0;
  263. }
  264.  
  265. static int
  266. i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
  267.                                     struct eb_objects *eb)
  268. {
  269. #define N_RELOC(x) ((x) / sizeof(struct drm_i915_gem_relocation_entry))
  270.         struct drm_i915_gem_relocation_entry stack_reloc[N_RELOC(512)];
  271.         struct drm_i915_gem_relocation_entry __user *user_relocs;
  272.         struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  273.         int remain, ret;
  274.  
  275.         user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
  276.  
  277.         remain = entry->relocation_count;
  278.         while (remain) {
  279.                 struct drm_i915_gem_relocation_entry *r = stack_reloc;
  280.                 int count = remain;
  281.                 if (count > ARRAY_SIZE(stack_reloc))
  282.                         count = ARRAY_SIZE(stack_reloc);
  283.                 remain -= count;
  284.  
  285.         memcpy(r, user_relocs, count*sizeof(r[0]));
  286.  
  287.                 do {
  288.                         u64 offset = r->presumed_offset;
  289.  
  290.                         ret = i915_gem_execbuffer_relocate_entry(obj, eb, r);
  291.                         if (ret)
  292.                                 return ret;
  293.  
  294.             memcpy(&user_relocs->presumed_offset,
  295.                    &r->presumed_offset,
  296.                    sizeof(r->presumed_offset));
  297.  
  298.                         user_relocs++;
  299.                         r++;
  300.                 } while (--count);
  301.         }
  302.  
  303.         return 0;
  304. #undef N_RELOC
  305. }
  306.  
  307. static int
  308. i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
  309.                                          struct eb_objects *eb,
  310.                                          struct drm_i915_gem_relocation_entry *relocs)
  311. {
  312.         const struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  313.         int i, ret;
  314.  
  315.         for (i = 0; i < entry->relocation_count; i++) {
  316.                 ret = i915_gem_execbuffer_relocate_entry(obj, eb, &relocs[i]);
  317.                 if (ret)
  318.                         return ret;
  319.         }
  320.  
  321.         return 0;
  322. }
  323.  
  324. static int
  325. i915_gem_execbuffer_relocate(struct drm_device *dev,
  326.                              struct eb_objects *eb,
  327.                              struct list_head *objects)
  328. {
  329.         struct drm_i915_gem_object *obj;
  330.         int ret = 0;
  331.  
  332.         /* This is the fast path and we cannot handle a pagefault whilst
  333.          * holding the struct mutex lest the user pass in the relocations
  334.          * contained within a mmaped bo. For in such a case we, the page
  335.          * fault handler would call i915_gem_fault() and we would try to
  336.          * acquire the struct mutex again. Obviously this is bad and so
  337.          * lockdep complains vehemently.
  338.          */
  339. //   pagefault_disable();
  340.         list_for_each_entry(obj, objects, exec_list) {
  341.                 ret = i915_gem_execbuffer_relocate_object(obj, eb);
  342.                 if (ret)
  343.                         break;
  344.         }
  345. //   pagefault_enable();
  346.  
  347.         return ret;
  348. }
  349.  
  350. #define  __EXEC_OBJECT_HAS_PIN (1<<31)
  351. #define  __EXEC_OBJECT_HAS_FENCE (1<<30)
  352.  
  353. static int
  354. need_reloc_mappable(struct drm_i915_gem_object *obj)
  355. {
  356.         struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  357.         return entry->relocation_count && !use_cpu_reloc(obj);
  358. }
  359.  
  360. static int
  361. i915_gem_execbuffer_reserve_object(struct drm_i915_gem_object *obj,
  362.                                    struct intel_ring_buffer *ring)
  363. {
  364.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  365.         struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  366.         bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  367.         bool need_fence, need_mappable;
  368.         int ret;
  369.  
  370.         need_fence =
  371.                 has_fenced_gpu_access &&
  372.                 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  373.                 obj->tiling_mode != I915_TILING_NONE;
  374.         need_mappable = need_fence || need_reloc_mappable(obj);
  375.  
  376.         ret = i915_gem_object_pin(obj, entry->alignment, need_mappable, false);
  377.         if (ret)
  378.                 return ret;
  379.  
  380.         entry->flags |= __EXEC_OBJECT_HAS_PIN;
  381.  
  382.         if (has_fenced_gpu_access) {
  383.                 if (entry->flags & EXEC_OBJECT_NEEDS_FENCE) {
  384.                         ret = i915_gem_object_get_fence(obj);
  385.                         if (ret)
  386.                                 return ret;
  387.  
  388.                         if (i915_gem_object_pin_fence(obj))
  389.                                 entry->flags |= __EXEC_OBJECT_HAS_FENCE;
  390.  
  391.                         obj->pending_fenced_gpu_access = true;
  392.                 }
  393.         }
  394.  
  395.         /* Ensure ppgtt mapping exists if needed */
  396.         if (dev_priv->mm.aliasing_ppgtt && !obj->has_aliasing_ppgtt_mapping) {
  397.                 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
  398.                                        obj, obj->cache_level);
  399.  
  400.                 obj->has_aliasing_ppgtt_mapping = 1;
  401.         }
  402.  
  403.         entry->offset = obj->gtt_offset;
  404.         return 0;
  405. }
  406.  
  407. static void
  408. i915_gem_execbuffer_unreserve_object(struct drm_i915_gem_object *obj)
  409. {
  410.         struct drm_i915_gem_exec_object2 *entry;
  411.  
  412.         if (!obj->gtt_space)
  413.                 return;
  414.  
  415.         entry = obj->exec_entry;
  416.  
  417.         if (entry->flags & __EXEC_OBJECT_HAS_FENCE)
  418.                 i915_gem_object_unpin_fence(obj);
  419.  
  420.         if (entry->flags & __EXEC_OBJECT_HAS_PIN)
  421.                 i915_gem_object_unpin(obj);
  422.  
  423.         entry->flags &= ~(__EXEC_OBJECT_HAS_FENCE | __EXEC_OBJECT_HAS_PIN);
  424. }
  425.  
  426. static int
  427. i915_gem_execbuffer_reserve(struct intel_ring_buffer *ring,
  428.                             struct drm_file *file,
  429.                             struct list_head *objects)
  430. {
  431.         struct drm_i915_gem_object *obj;
  432.         struct list_head ordered_objects;
  433.         bool has_fenced_gpu_access = INTEL_INFO(ring->dev)->gen < 4;
  434.         int retry;
  435.  
  436.         INIT_LIST_HEAD(&ordered_objects);
  437.         while (!list_empty(objects)) {
  438.                 struct drm_i915_gem_exec_object2 *entry;
  439.                 bool need_fence, need_mappable;
  440.  
  441.                 obj = list_first_entry(objects,
  442.                                        struct drm_i915_gem_object,
  443.                                        exec_list);
  444.                 entry = obj->exec_entry;
  445.  
  446.                 need_fence =
  447.                         has_fenced_gpu_access &&
  448.                         entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  449.                         obj->tiling_mode != I915_TILING_NONE;
  450.                 need_mappable = need_fence || need_reloc_mappable(obj);
  451.  
  452.                 if (need_mappable)
  453.                         list_move(&obj->exec_list, &ordered_objects);
  454.                 else
  455.                         list_move_tail(&obj->exec_list, &ordered_objects);
  456.  
  457.                 obj->base.pending_read_domains = 0;
  458.                 obj->base.pending_write_domain = 0;
  459.                 obj->pending_fenced_gpu_access = false;
  460.         }
  461.         list_splice(&ordered_objects, objects);
  462.  
  463.         /* Attempt to pin all of the buffers into the GTT.
  464.          * This is done in 3 phases:
  465.          *
  466.          * 1a. Unbind all objects that do not match the GTT constraints for
  467.          *     the execbuffer (fenceable, mappable, alignment etc).
  468.          * 1b. Increment pin count for already bound objects.
  469.          * 2.  Bind new objects.
  470.          * 3.  Decrement pin count.
  471.          *
  472.          * This avoid unnecessary unbinding of later objects in order to make
  473.          * room for the earlier objects *unless* we need to defragment.
  474.          */
  475.         retry = 0;
  476.         do {
  477.                 int ret = 0;
  478.  
  479.                 /* Unbind any ill-fitting objects or pin. */
  480.                 list_for_each_entry(obj, objects, exec_list) {
  481.                         struct drm_i915_gem_exec_object2 *entry = obj->exec_entry;
  482.                         bool need_fence, need_mappable;
  483.  
  484.                         if (!obj->gtt_space)
  485.                                 continue;
  486.  
  487.                         need_fence =
  488.                                 has_fenced_gpu_access &&
  489.                                 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
  490.                                 obj->tiling_mode != I915_TILING_NONE;
  491.                         need_mappable = need_fence || need_reloc_mappable(obj);
  492.  
  493.                         if ((entry->alignment && obj->gtt_offset & (entry->alignment - 1)) ||
  494.                             (need_mappable && !obj->map_and_fenceable))
  495.                                 ret = i915_gem_object_unbind(obj);
  496.                         else
  497.                                 ret = i915_gem_execbuffer_reserve_object(obj, ring);
  498.                         if (ret)
  499.                                 goto err;
  500.                 }
  501.  
  502.                 /* Bind fresh objects */
  503.                 list_for_each_entry(obj, objects, exec_list) {
  504.                         if (obj->gtt_space)
  505.                                 continue;
  506.  
  507.                         ret = i915_gem_execbuffer_reserve_object(obj, ring);
  508.                         if (ret)
  509.                                 goto err;
  510.                 }
  511.  
  512. err:            /* Decrement pin count for bound objects */
  513.                 list_for_each_entry(obj, objects, exec_list)
  514.                         i915_gem_execbuffer_unreserve_object(obj);
  515.  
  516.                 if (ret != -ENOSPC || retry++)
  517.                         return ret;
  518.  
  519. //       ret = i915_gem_evict_everything(ring->dev);
  520.                 if (ret)
  521.                         return ret;
  522.         } while (1);
  523. }
  524.  
  525. static int
  526. i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
  527.                                   struct drm_file *file,
  528.                                   struct intel_ring_buffer *ring,
  529.                                   struct list_head *objects,
  530.                                   struct eb_objects *eb,
  531.                                   struct drm_i915_gem_exec_object2 *exec,
  532.                                   int count)
  533. {
  534.         struct drm_i915_gem_relocation_entry *reloc;
  535.         struct drm_i915_gem_object *obj;
  536.         int *reloc_offset;
  537.         int i, total, ret;
  538.  
  539.         /* We may process another execbuffer during the unlock... */
  540.         while (!list_empty(objects)) {
  541.                 obj = list_first_entry(objects,
  542.                                        struct drm_i915_gem_object,
  543.                                        exec_list);
  544.                 list_del_init(&obj->exec_list);
  545.                 drm_gem_object_unreference(&obj->base);
  546.         }
  547.  
  548.         mutex_unlock(&dev->struct_mutex);
  549.  
  550.         total = 0;
  551.         for (i = 0; i < count; i++)
  552.                 total += exec[i].relocation_count;
  553.  
  554.     reloc_offset = malloc(count * sizeof(*reloc_offset));
  555.     reloc = malloc(total * sizeof(*reloc));
  556.         if (reloc == NULL || reloc_offset == NULL) {
  557.         free(reloc);
  558.         free(reloc_offset);
  559.                 mutex_lock(&dev->struct_mutex);
  560.                 return -ENOMEM;
  561.         }
  562.  
  563.         total = 0;
  564.         for (i = 0; i < count; i++) {
  565.                 struct drm_i915_gem_relocation_entry __user *user_relocs;
  566.                 u64 invalid_offset = (u64)-1;
  567.                 int j;
  568.  
  569.                 user_relocs = (void __user *)(uintptr_t)exec[i].relocs_ptr;
  570.  
  571.                 if (copy_from_user(reloc+total, user_relocs,
  572.                                    exec[i].relocation_count * sizeof(*reloc))) {
  573.                         ret = -EFAULT;
  574.                         mutex_lock(&dev->struct_mutex);
  575.                         goto err;
  576.                 }
  577.  
  578.                 /* As we do not update the known relocation offsets after
  579.                  * relocating (due to the complexities in lock handling),
  580.                  * we need to mark them as invalid now so that we force the
  581.                  * relocation processing next time. Just in case the target
  582.                  * object is evicted and then rebound into its old
  583.                  * presumed_offset before the next execbuffer - if that
  584.                  * happened we would make the mistake of assuming that the
  585.                  * relocations were valid.
  586.                  */
  587.                 for (j = 0; j < exec[i].relocation_count; j++) {
  588.                         if (copy_to_user(&user_relocs[j].presumed_offset,
  589.                                          &invalid_offset,
  590.                                          sizeof(invalid_offset))) {
  591.                                 ret = -EFAULT;
  592.                                 mutex_lock(&dev->struct_mutex);
  593.                                 goto err;
  594.                         }
  595.                 }
  596.  
  597.                 reloc_offset[i] = total;
  598.                 total += exec[i].relocation_count;
  599.         }
  600.  
  601.         ret = i915_mutex_lock_interruptible(dev);
  602.         if (ret) {
  603.                 mutex_lock(&dev->struct_mutex);
  604.                 goto err;
  605.         }
  606.  
  607.         /* reacquire the objects */
  608.         eb_reset(eb);
  609.         for (i = 0; i < count; i++) {
  610.  
  611.         if(exec[i].handle == -2)
  612.             obj = get_fb_obj();
  613.         else
  614.             obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  615.                               exec[i].handle));
  616.                 if (&obj->base == NULL) {
  617.                         DRM_DEBUG("Invalid object handle %d at index %d\n",
  618.                                    exec[i].handle, i);
  619.                         ret = -ENOENT;
  620.                         goto err;
  621.                 }
  622.  
  623.                 list_add_tail(&obj->exec_list, objects);
  624.                 obj->exec_handle = exec[i].handle;
  625.                 obj->exec_entry = &exec[i];
  626.                 eb_add_object(eb, obj);
  627.         }
  628.  
  629.         ret = i915_gem_execbuffer_reserve(ring, file, objects);
  630.         if (ret)
  631.                 goto err;
  632.  
  633.         list_for_each_entry(obj, objects, exec_list) {
  634.                 int offset = obj->exec_entry - exec;
  635.                 ret = i915_gem_execbuffer_relocate_object_slow(obj, eb,
  636.                                                                reloc + reloc_offset[offset]);
  637.                 if (ret)
  638.                         goto err;
  639.         }
  640.  
  641.         /* Leave the user relocations as are, this is the painfully slow path,
  642.          * and we want to avoid the complication of dropping the lock whilst
  643.          * having buffers reserved in the aperture and so causing spurious
  644.          * ENOSPC for random operations.
  645.          */
  646.  
  647. err:
  648.     free(reloc);
  649.     free(reloc_offset);
  650.         return ret;
  651. }
  652.  
  653. static int
  654. i915_gem_execbuffer_wait_for_flips(struct intel_ring_buffer *ring, u32 flips)
  655. {
  656.         u32 plane, flip_mask;
  657.         int ret;
  658.  
  659.         /* Check for any pending flips. As we only maintain a flip queue depth
  660.          * of 1, we can simply insert a WAIT for the next display flip prior
  661.          * to executing the batch and avoid stalling the CPU.
  662.          */
  663.  
  664.         for (plane = 0; flips >> plane; plane++) {
  665.                 if (((flips >> plane) & 1) == 0)
  666.                         continue;
  667.  
  668.                 if (plane)
  669.                         flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
  670.                 else
  671.                         flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
  672.  
  673.                 ret = intel_ring_begin(ring, 2);
  674.                 if (ret)
  675.                         return ret;
  676.  
  677.                 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
  678.                 intel_ring_emit(ring, MI_NOOP);
  679.                 intel_ring_advance(ring);
  680.         }
  681.  
  682.         return 0;
  683. }
  684.  
  685. static int
  686. i915_gem_execbuffer_move_to_gpu(struct intel_ring_buffer *ring,
  687.                                 struct list_head *objects)
  688. {
  689.         struct drm_i915_gem_object *obj;
  690.         uint32_t flush_domains = 0;
  691.         uint32_t flips = 0;
  692.         int ret;
  693.  
  694.         list_for_each_entry(obj, objects, exec_list) {
  695.                 ret = i915_gem_object_sync(obj, ring);
  696.                 if (ret)
  697.                         return ret;
  698.  
  699.                 if (obj->base.write_domain & I915_GEM_DOMAIN_CPU)
  700.                         i915_gem_clflush_object(obj);
  701.  
  702.                 if (obj->base.pending_write_domain)
  703.                         flips |= atomic_read(&obj->pending_flip);
  704.  
  705.                 flush_domains |= obj->base.write_domain;
  706.         }
  707.  
  708.         if (flips) {
  709.                 ret = i915_gem_execbuffer_wait_for_flips(ring, flips);
  710.                 if (ret)
  711.                         return ret;
  712.         }
  713.  
  714.         if (flush_domains & I915_GEM_DOMAIN_CPU)
  715.                 i915_gem_chipset_flush(ring->dev);
  716.  
  717.         if (flush_domains & I915_GEM_DOMAIN_GTT)
  718.                 wmb();
  719.  
  720.         /* Unconditionally invalidate gpu caches and ensure that we do flush
  721.          * any residual writes from the previous batch.
  722.          */
  723.         return intel_ring_invalidate_all_caches(ring);
  724. }
  725.  
  726. static bool
  727. i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec)
  728. {
  729.         return ((exec->batch_start_offset | exec->batch_len) & 0x7) == 0;
  730. }
  731.  
  732. static int
  733. validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
  734.                    int count)
  735. {
  736.         int i;
  737.  
  738.         for (i = 0; i < count; i++) {
  739.                 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
  740.                 int length; /* limited by fault_in_pages_readable() */
  741.  
  742.                 /* First check for malicious input causing overflow */
  743.                 if (exec[i].relocation_count >
  744.                     INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
  745.                         return -EINVAL;
  746.  
  747.                 length = exec[i].relocation_count *
  748.                         sizeof(struct drm_i915_gem_relocation_entry);
  749. //       if (!access_ok(VERIFY_READ, ptr, length))
  750. //           return -EFAULT;
  751.  
  752.                 /* we may also need to update the presumed offsets */
  753. //       if (!access_ok(VERIFY_WRITE, ptr, length))
  754. //           return -EFAULT;
  755.  
  756. //       if (fault_in_multipages_readable(ptr, length))
  757. //           return -EFAULT;
  758.         }
  759.  
  760.         return 0;
  761. }
  762.  
  763. static void
  764. i915_gem_execbuffer_move_to_active(struct list_head *objects,
  765.                                    struct intel_ring_buffer *ring)
  766. {
  767.         struct drm_i915_gem_object *obj;
  768.  
  769.         list_for_each_entry(obj, objects, exec_list) {
  770.                 u32 old_read = obj->base.read_domains;
  771.                 u32 old_write = obj->base.write_domain;
  772.  
  773.                 obj->base.read_domains = obj->base.pending_read_domains;
  774.                 obj->base.write_domain = obj->base.pending_write_domain;
  775.                 obj->fenced_gpu_access = obj->pending_fenced_gpu_access;
  776.  
  777.                 i915_gem_object_move_to_active(obj, ring);
  778.                 if (obj->base.write_domain) {
  779.                         obj->dirty = 1;
  780.                         obj->last_write_seqno = intel_ring_get_seqno(ring);
  781.                         if (obj->pin_count) /* check for potential scanout */
  782.                                 intel_mark_fb_busy(obj);
  783.                 }
  784.  
  785.                 trace_i915_gem_object_change_domain(obj, old_read, old_write);
  786.         }
  787. }
  788.  
  789. static void
  790. i915_gem_execbuffer_retire_commands(struct drm_device *dev,
  791.                                     struct drm_file *file,
  792.                                     struct intel_ring_buffer *ring)
  793. {
  794.         /* Unconditionally force add_request to emit a full flush. */
  795.         ring->gpu_caches_dirty = true;
  796.  
  797.         /* Add a breadcrumb for the completion of the batch buffer */
  798.         (void)i915_add_request(ring, file, NULL);
  799. }
  800.  
  801. static int
  802. i915_reset_gen7_sol_offsets(struct drm_device *dev,
  803.                             struct intel_ring_buffer *ring)
  804. {
  805.         drm_i915_private_t *dev_priv = dev->dev_private;
  806.         int ret, i;
  807.  
  808.         if (!IS_GEN7(dev) || ring != &dev_priv->ring[RCS])
  809.                 return 0;
  810.  
  811.         ret = intel_ring_begin(ring, 4 * 3);
  812.         if (ret)
  813.                 return ret;
  814.  
  815.         for (i = 0; i < 4; i++) {
  816.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  817.                 intel_ring_emit(ring, GEN7_SO_WRITE_OFFSET(i));
  818.                 intel_ring_emit(ring, 0);
  819.         }
  820.  
  821.         intel_ring_advance(ring);
  822.  
  823.         return 0;
  824. }
  825.  
  826. static int
  827. i915_gem_do_execbuffer(struct drm_device *dev, void *data,
  828.                        struct drm_file *file,
  829.                        struct drm_i915_gem_execbuffer2 *args,
  830.                        struct drm_i915_gem_exec_object2 *exec)
  831. {
  832.         drm_i915_private_t *dev_priv = dev->dev_private;
  833.         struct list_head objects;
  834.         struct eb_objects *eb;
  835.         struct drm_i915_gem_object *batch_obj;
  836.         struct drm_clip_rect *cliprects = NULL;
  837.         struct intel_ring_buffer *ring;
  838.         u32 ctx_id = i915_execbuffer2_get_context_id(*args);
  839.         u32 exec_start, exec_len;
  840.         u32 mask;
  841.         u32 flags;
  842.         int ret, mode, i;
  843.  
  844.         if (!i915_gem_check_execbuffer(args)) {
  845.                 DRM_DEBUG("execbuf with invalid offset/length\n");
  846.                 return -EINVAL;
  847.         }
  848.  
  849.         ret = validate_exec_list(exec, args->buffer_count);
  850.         if (ret)
  851.                 return ret;
  852.  
  853.         flags = 0;
  854.         if (args->flags & I915_EXEC_SECURE) {
  855. //       if (!file->is_master || !capable(CAP_SYS_ADMIN))
  856. //           return -EPERM;
  857.  
  858.                 flags |= I915_DISPATCH_SECURE;
  859.         }
  860.         if (args->flags & I915_EXEC_IS_PINNED)
  861.                 flags |= I915_DISPATCH_PINNED;
  862.  
  863.         switch (args->flags & I915_EXEC_RING_MASK) {
  864.         case I915_EXEC_DEFAULT:
  865.         case I915_EXEC_RENDER:
  866.                 ring = &dev_priv->ring[RCS];
  867.                 break;
  868.         case I915_EXEC_BSD:
  869.                 ring = &dev_priv->ring[VCS];
  870.                 if (ctx_id != 0) {
  871.                         DRM_DEBUG("Ring %s doesn't support contexts\n",
  872.                                   ring->name);
  873.                         return -EPERM;
  874.                 }
  875.                 break;
  876.         case I915_EXEC_BLT:
  877.                 ring = &dev_priv->ring[BCS];
  878.                 if (ctx_id != 0) {
  879.                         DRM_DEBUG("Ring %s doesn't support contexts\n",
  880.                                   ring->name);
  881.                         return -EPERM;
  882.                 }
  883.                 break;
  884.         default:
  885.                 DRM_DEBUG("execbuf with unknown ring: %d\n",
  886.                           (int)(args->flags & I915_EXEC_RING_MASK));
  887.                 return -EINVAL;
  888.         }
  889.         if (!intel_ring_initialized(ring)) {
  890.                 DRM_DEBUG("execbuf with invalid ring: %d\n",
  891.                           (int)(args->flags & I915_EXEC_RING_MASK));
  892.                 return -EINVAL;
  893.         }
  894.  
  895.         mode = args->flags & I915_EXEC_CONSTANTS_MASK;
  896.         mask = I915_EXEC_CONSTANTS_MASK;
  897.         switch (mode) {
  898.         case I915_EXEC_CONSTANTS_REL_GENERAL:
  899.         case I915_EXEC_CONSTANTS_ABSOLUTE:
  900.         case I915_EXEC_CONSTANTS_REL_SURFACE:
  901.                 if (ring == &dev_priv->ring[RCS] &&
  902.                     mode != dev_priv->relative_constants_mode) {
  903.                         if (INTEL_INFO(dev)->gen < 4)
  904.                                 return -EINVAL;
  905.  
  906.                         if (INTEL_INFO(dev)->gen > 5 &&
  907.                             mode == I915_EXEC_CONSTANTS_REL_SURFACE)
  908.                                 return -EINVAL;
  909.  
  910.                         /* The HW changed the meaning on this bit on gen6 */
  911.                         if (INTEL_INFO(dev)->gen >= 6)
  912.                                 mask &= ~I915_EXEC_CONSTANTS_REL_SURFACE;
  913.                 }
  914.                 break;
  915.         default:
  916.                 DRM_DEBUG("execbuf with unknown constants: %d\n", mode);
  917.                 return -EINVAL;
  918.         }
  919.  
  920.         if (args->buffer_count < 1) {
  921.                 DRM_DEBUG("execbuf with %d buffers\n", args->buffer_count);
  922.                 return -EINVAL;
  923.         }
  924.  
  925.         if (args->num_cliprects != 0) {
  926.                 if (ring != &dev_priv->ring[RCS]) {
  927.                         DRM_DEBUG("clip rectangles are only valid with the render ring\n");
  928.                         return -EINVAL;
  929.                 }
  930.  
  931.                 if (INTEL_INFO(dev)->gen >= 5) {
  932.                         DRM_DEBUG("clip rectangles are only valid on pre-gen5\n");
  933.                         return -EINVAL;
  934.                 }
  935.  
  936.                 if (args->num_cliprects > UINT_MAX / sizeof(*cliprects)) {
  937.                         DRM_DEBUG("execbuf with %u cliprects\n",
  938.                                   args->num_cliprects);
  939.                         return -EINVAL;
  940.                 }
  941.  
  942.                 cliprects = kmalloc(args->num_cliprects * sizeof(*cliprects),
  943.                                     GFP_KERNEL);
  944.                 if (cliprects == NULL) {
  945.                         ret = -ENOMEM;
  946.                         goto pre_mutex_err;
  947.                 }
  948.  
  949.                 if (copy_from_user(cliprects,
  950.                                      (struct drm_clip_rect __user *)(uintptr_t)
  951.                                      args->cliprects_ptr,
  952.                                      sizeof(*cliprects)*args->num_cliprects)) {
  953.                         ret = -EFAULT;
  954.                         goto pre_mutex_err;
  955.                 }
  956.         }
  957.  
  958.         ret = i915_mutex_lock_interruptible(dev);
  959.         if (ret)
  960.                 goto pre_mutex_err;
  961.  
  962.         if (dev_priv->mm.suspended) {
  963.                 mutex_unlock(&dev->struct_mutex);
  964.                 ret = -EBUSY;
  965.                 goto pre_mutex_err;
  966.         }
  967.  
  968.         eb = eb_create(args->buffer_count);
  969.         if (eb == NULL) {
  970.                 mutex_unlock(&dev->struct_mutex);
  971.                 ret = -ENOMEM;
  972.                 goto pre_mutex_err;
  973.         }
  974.  
  975.         /* Look up object handles */
  976.         INIT_LIST_HEAD(&objects);
  977.         for (i = 0; i < args->buffer_count; i++) {
  978.                 struct drm_i915_gem_object *obj;
  979.  
  980.         if(exec[i].handle == -2)
  981.             obj = get_fb_obj();
  982.         else
  983.             obj = to_intel_bo(drm_gem_object_lookup(dev, file,
  984.                               exec[i].handle));
  985.                 if (&obj->base == NULL) {
  986.                         DRM_DEBUG("Invalid object handle %d at index %d\n",
  987.                                    exec[i].handle, i);
  988.                         /* prevent error path from reading uninitialized data */
  989.                         ret = -ENOENT;
  990.                         goto err;
  991.                 }
  992.  
  993.                 if (!list_empty(&obj->exec_list)) {
  994.                         DRM_DEBUG("Object %p [handle %d, index %d] appears more than once in object list\n",
  995.                                    obj, exec[i].handle, i);
  996.                         ret = -EINVAL;
  997.                         goto err;
  998.                 }
  999.  
  1000.                 list_add_tail(&obj->exec_list, &objects);
  1001.                 obj->exec_handle = exec[i].handle;
  1002.                 obj->exec_entry = &exec[i];
  1003.                 eb_add_object(eb, obj);
  1004.         }
  1005.  
  1006.         /* take note of the batch buffer before we might reorder the lists */
  1007.         batch_obj = list_entry(objects.prev,
  1008.                                struct drm_i915_gem_object,
  1009.                                exec_list);
  1010.  
  1011.         /* Move the objects en-masse into the GTT, evicting if necessary. */
  1012.         ret = i915_gem_execbuffer_reserve(ring, file, &objects);
  1013.         if (ret)
  1014.                 goto err;
  1015.  
  1016.         /* The objects are in their final locations, apply the relocations. */
  1017.         ret = i915_gem_execbuffer_relocate(dev, eb, &objects);
  1018.         if (ret) {
  1019.                 if (ret == -EFAULT) {
  1020.                         ret = i915_gem_execbuffer_relocate_slow(dev, file, ring,
  1021.                                                                 &objects, eb,
  1022.                                                                 exec,
  1023.                                                                 args->buffer_count);
  1024.                         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1025.                 }
  1026.                 if (ret)
  1027.                         goto err;
  1028.         }
  1029.  
  1030.         /* Set the pending read domains for the batch buffer to COMMAND */
  1031.         if (batch_obj->base.pending_write_domain) {
  1032.                 DRM_DEBUG("Attempting to use self-modifying batch buffer\n");
  1033.                 ret = -EINVAL;
  1034.                 goto err;
  1035.         }
  1036.         batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
  1037.  
  1038.         /* snb/ivb/vlv conflate the "batch in ppgtt" bit with the "non-secure
  1039.          * batch" bit. Hence we need to pin secure batches into the global gtt.
  1040.          * hsw should have this fixed, but let's be paranoid and do it
  1041.          * unconditionally for now. */
  1042.         if (flags & I915_DISPATCH_SECURE && !batch_obj->has_global_gtt_mapping)
  1043.                 i915_gem_gtt_bind_object(batch_obj, batch_obj->cache_level);
  1044.  
  1045.         ret = i915_gem_execbuffer_move_to_gpu(ring, &objects);
  1046.         if (ret)
  1047.                 goto err;
  1048.  
  1049.         ret = i915_switch_context(ring, file, ctx_id);
  1050.         if (ret)
  1051.                 goto err;
  1052.  
  1053.         if (ring == &dev_priv->ring[RCS] &&
  1054.             mode != dev_priv->relative_constants_mode) {
  1055.                 ret = intel_ring_begin(ring, 4);
  1056.                 if (ret)
  1057.                                 goto err;
  1058.  
  1059.                 intel_ring_emit(ring, MI_NOOP);
  1060.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  1061.                 intel_ring_emit(ring, INSTPM);
  1062.                 intel_ring_emit(ring, mask << 16 | mode);
  1063.                 intel_ring_advance(ring);
  1064.  
  1065.                 dev_priv->relative_constants_mode = mode;
  1066.         }
  1067.  
  1068.         if (args->flags & I915_EXEC_GEN7_SOL_RESET) {
  1069.                 ret = i915_reset_gen7_sol_offsets(dev, ring);
  1070.                 if (ret)
  1071.                         goto err;
  1072.         }
  1073.  
  1074.         exec_start = batch_obj->gtt_offset + args->batch_start_offset;
  1075.         exec_len = args->batch_len;
  1076.         if (cliprects) {
  1077. //       for (i = 0; i < args->num_cliprects; i++) {
  1078. //           ret = i915_emit_box(dev, &cliprects[i],
  1079. //                       args->DR1, args->DR4);
  1080. //           if (ret)
  1081. //               goto err;
  1082.  
  1083. //           ret = ring->dispatch_execbuffer(ring,
  1084. //                           exec_start, exec_len,
  1085. //                           flags);
  1086. //           if (ret)
  1087. //               goto err;
  1088. //       }
  1089.         } else {
  1090.                 ret = ring->dispatch_execbuffer(ring,
  1091.                                                 exec_start, exec_len,
  1092.                                                 flags);
  1093.                 if (ret)
  1094.                         goto err;
  1095.         }
  1096.  
  1097. //   i915_gem_execbuffer_move_to_active(&objects, ring);
  1098. //   i915_gem_execbuffer_retire_commands(dev, file, ring);
  1099.     ring->gpu_caches_dirty = true;
  1100.     intel_ring_flush_all_caches(ring);
  1101.  
  1102. err:
  1103.         eb_destroy(eb);
  1104.         while (!list_empty(&objects)) {
  1105.                 struct drm_i915_gem_object *obj;
  1106.  
  1107.                 obj = list_first_entry(&objects,
  1108.                                        struct drm_i915_gem_object,
  1109.                                        exec_list);
  1110.                 list_del_init(&obj->exec_list);
  1111.                 drm_gem_object_unreference(&obj->base);
  1112.         }
  1113.  
  1114.         mutex_unlock(&dev->struct_mutex);
  1115.  
  1116. pre_mutex_err:
  1117.         kfree(cliprects);
  1118.         return ret;
  1119. }
  1120.  
  1121.  
  1122. int
  1123. i915_gem_execbuffer2(struct drm_device *dev, void *data,
  1124.                      struct drm_file *file)
  1125. {
  1126.         struct drm_i915_gem_execbuffer2 *args = data;
  1127.         struct drm_i915_gem_exec_object2 *exec2_list = NULL;
  1128.         int ret;
  1129.  
  1130.         if (args->buffer_count < 1 ||
  1131.             args->buffer_count > UINT_MAX / sizeof(*exec2_list)) {
  1132.                 DRM_DEBUG("execbuf2 with %d buffers\n", args->buffer_count);
  1133.                 return -EINVAL;
  1134.         }
  1135.  
  1136.     exec2_list = kmalloc(sizeof(*exec2_list)*args->buffer_count, 0);
  1137.         if (exec2_list == NULL)
  1138.         exec2_list = malloc(sizeof(*exec2_list) * args->buffer_count);
  1139.         if (exec2_list == NULL) {
  1140.                 DRM_DEBUG("Failed to allocate exec list for %d buffers\n",
  1141.                           args->buffer_count);
  1142.                 return -ENOMEM;
  1143.         }
  1144.         ret = copy_from_user(exec2_list,
  1145.                              (struct drm_i915_relocation_entry __user *)
  1146.                              (uintptr_t) args->buffers_ptr,
  1147.                              sizeof(*exec2_list) * args->buffer_count);
  1148.         if (ret != 0) {
  1149.                 DRM_DEBUG("copy %d exec entries failed %d\n",
  1150.                           args->buffer_count, ret);
  1151.         free(exec2_list);
  1152.                 return -EFAULT;
  1153.         }
  1154.  
  1155.         ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
  1156.         if (!ret) {
  1157.                 /* Copy the new buffer offsets back to the user's exec list. */
  1158.                 ret = copy_to_user((void __user *)(uintptr_t)args->buffers_ptr,
  1159.                                    exec2_list,
  1160.                                    sizeof(*exec2_list) * args->buffer_count);
  1161.                 if (ret) {
  1162.                         ret = -EFAULT;
  1163.                         DRM_DEBUG("failed to copy %d exec entries "
  1164.                                   "back to user (%d)\n",
  1165.                                   args->buffer_count, ret);
  1166.                 }
  1167.         }
  1168.  
  1169.     free(exec2_list);
  1170.         return ret;
  1171. }
  1172.