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 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.  *
  26.  */
  27.  
  28. #include <drm/drmP.h>
  29. #include <drm/drm_vma_manager.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/shmem_fs.h>
  35. #include <linux/slab.h>
  36. //#include <linux/swap.h>
  37. #include <linux/scatterlist.h>
  38. #include <linux/pci.h>
  39.  
  40. extern int x86_clflush_size;
  41.  
  42. #define PROT_READ       0x1             /* page can be read */
  43. #define PROT_WRITE      0x2             /* page can be written */
  44. #define MAP_SHARED      0x01            /* Share changes */
  45.  
  46.  
  47. u64 nsecs_to_jiffies64(u64 n)
  48. {
  49. #if (NSEC_PER_SEC % HZ) == 0
  50.         /* Common case, HZ = 100, 128, 200, 250, 256, 500, 512, 1000 etc. */
  51.         return div_u64(n, NSEC_PER_SEC / HZ);
  52. #elif (HZ % 512) == 0
  53.         /* overflow after 292 years if HZ = 1024 */
  54.         return div_u64(n * HZ / 512, NSEC_PER_SEC / 512);
  55. #else
  56.         /*
  57.          * Generic case - optimized for cases where HZ is a multiple of 3.
  58.          * overflow after 64.99 years, exact for HZ = 60, 72, 90, 120 etc.
  59.          */
  60.         return div_u64(n * 9, (9ull * NSEC_PER_SEC + HZ / 2) / HZ);
  61. #endif
  62. }
  63.  
  64. unsigned long nsecs_to_jiffies(u64 n)
  65. {
  66.     return (unsigned long)nsecs_to_jiffies64(n);
  67. }
  68.  
  69.  
  70. struct drm_i915_gem_object *get_fb_obj();
  71.  
  72. unsigned long vm_mmap(struct file *file, unsigned long addr,
  73.          unsigned long len, unsigned long prot,
  74.          unsigned long flag, unsigned long offset);
  75.  
  76.  
  77. #define MAX_ERRNO       4095
  78.  
  79. #define IS_ERR_VALUE(x) unlikely((x) >= (unsigned long)-MAX_ERRNO)
  80.  
  81.  
  82. static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
  83. static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  84.                                                    bool force);
  85. static __must_check int
  86. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  87.                                bool readonly);
  88. static void
  89. i915_gem_object_retire(struct drm_i915_gem_object *obj);
  90.  
  91. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  92.                                  struct drm_i915_gem_object *obj);
  93. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  94.                                          struct drm_i915_fence_reg *fence,
  95.                                          bool enable);
  96.  
  97.  
  98. static unsigned long i915_gem_shrink_all(struct drm_i915_private *dev_priv);
  99.  
  100. static bool cpu_cache_is_coherent(struct drm_device *dev,
  101.                                   enum i915_cache_level level)
  102. {
  103.         return HAS_LLC(dev) || level != I915_CACHE_NONE;
  104. }
  105.  
  106. static bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
  107. {
  108.         if (!cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  109.                 return true;
  110.  
  111.         return obj->pin_display;
  112. }
  113.  
  114. static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
  115. {
  116.         if (obj->tiling_mode)
  117.                 i915_gem_release_mmap(obj);
  118.  
  119.         /* As we do not have an associated fence register, we will force
  120.          * a tiling change if we ever need to acquire one.
  121.          */
  122.         obj->fence_dirty = false;
  123.         obj->fence_reg = I915_FENCE_REG_NONE;
  124. }
  125.  
  126. /* some bookkeeping */
  127. static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
  128.                                   size_t size)
  129. {
  130.         spin_lock(&dev_priv->mm.object_stat_lock);
  131.         dev_priv->mm.object_count++;
  132.         dev_priv->mm.object_memory += size;
  133.         spin_unlock(&dev_priv->mm.object_stat_lock);
  134. }
  135.  
  136. static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
  137.                                      size_t size)
  138. {
  139.         spin_lock(&dev_priv->mm.object_stat_lock);
  140.         dev_priv->mm.object_count--;
  141.         dev_priv->mm.object_memory -= size;
  142.         spin_unlock(&dev_priv->mm.object_stat_lock);
  143. }
  144.  
  145. static int
  146. i915_gem_wait_for_error(struct i915_gpu_error *error)
  147. {
  148.         int ret;
  149.  
  150. #define EXIT_COND (!i915_reset_in_progress(error))
  151.         if (EXIT_COND)
  152.                 return 0;
  153. #if 0
  154.         /*
  155.          * Only wait 10 seconds for the gpu reset to complete to avoid hanging
  156.          * userspace. If it takes that long something really bad is going on and
  157.          * we should simply try to bail out and fail as gracefully as possible.
  158.          */
  159.         ret = wait_event_interruptible_timeout(error->reset_queue,
  160.                                                EXIT_COND,
  161.                                                10*HZ);
  162.         if (ret == 0) {
  163.                 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
  164.                 return -EIO;
  165.         } else if (ret < 0) {
  166.                 return ret;
  167.         }
  168.  
  169. #endif
  170. #undef EXIT_COND
  171.  
  172.         return 0;
  173. }
  174.  
  175. int i915_mutex_lock_interruptible(struct drm_device *dev)
  176. {
  177.         struct drm_i915_private *dev_priv = dev->dev_private;
  178.         int ret;
  179.  
  180.         ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  181.         if (ret)
  182.                 return ret;
  183.  
  184.         ret = mutex_lock_interruptible(&dev->struct_mutex);
  185.         if (ret)
  186.                 return ret;
  187.  
  188.         WARN_ON(i915_verify_lists(dev));
  189.         return 0;
  190. }
  191.  
  192. static inline bool
  193. i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
  194. {
  195.         return i915_gem_obj_bound_any(obj) && !obj->active;
  196. }
  197.  
  198. int
  199. i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
  200.                             struct drm_file *file)
  201. {
  202.         struct drm_i915_private *dev_priv = dev->dev_private;
  203.         struct drm_i915_gem_get_aperture *args = data;
  204.         struct drm_i915_gem_object *obj;
  205.         size_t pinned;
  206.  
  207.         pinned = 0;
  208.         mutex_lock(&dev->struct_mutex);
  209.         list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
  210.                 if (i915_gem_obj_is_pinned(obj))
  211.                         pinned += i915_gem_obj_ggtt_size(obj);
  212.         mutex_unlock(&dev->struct_mutex);
  213.  
  214.         args->aper_size = dev_priv->gtt.base.total;
  215.         args->aper_available_size = args->aper_size - pinned;
  216.  
  217.         return 0;
  218. }
  219.  
  220. void *i915_gem_object_alloc(struct drm_device *dev)
  221. {
  222.         struct drm_i915_private *dev_priv = dev->dev_private;
  223.     return kzalloc(sizeof(struct drm_i915_gem_object), 0);
  224. }
  225.  
  226. void i915_gem_object_free(struct drm_i915_gem_object *obj)
  227. {
  228.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  229.         kfree(obj);
  230. }
  231.  
  232. static int
  233. i915_gem_create(struct drm_file *file,
  234.                 struct drm_device *dev,
  235.                 uint64_t size,
  236.                 uint32_t *handle_p)
  237. {
  238.         struct drm_i915_gem_object *obj;
  239.         int ret;
  240.         u32 handle;
  241.  
  242.         size = roundup(size, PAGE_SIZE);
  243.         if (size == 0)
  244.                 return -EINVAL;
  245.  
  246.         /* Allocate the new object */
  247.         obj = i915_gem_alloc_object(dev, size);
  248.         if (obj == NULL)
  249.                 return -ENOMEM;
  250.  
  251.         ret = drm_gem_handle_create(file, &obj->base, &handle);
  252.         /* drop reference from allocate - handle holds it now */
  253.         drm_gem_object_unreference_unlocked(&obj->base);
  254.         if (ret)
  255.                 return ret;
  256.  
  257.         *handle_p = handle;
  258.         return 0;
  259. }
  260.  
  261. int
  262. i915_gem_dumb_create(struct drm_file *file,
  263.                      struct drm_device *dev,
  264.                      struct drm_mode_create_dumb *args)
  265. {
  266.         /* have to work out size/pitch and return them */
  267.         args->pitch = ALIGN(args->width * DIV_ROUND_UP(args->bpp, 8), 64);
  268.         args->size = args->pitch * args->height;
  269.         return i915_gem_create(file, dev,
  270.                                args->size, &args->handle);
  271. }
  272.  
  273. /**
  274.  * Creates a new mm object and returns a handle to it.
  275.  */
  276. int
  277. i915_gem_create_ioctl(struct drm_device *dev, void *data,
  278.                       struct drm_file *file)
  279. {
  280.         struct drm_i915_gem_create *args = data;
  281.  
  282.         return i915_gem_create(file, dev,
  283.                                args->size, &args->handle);
  284. }
  285.  
  286.  
  287. #if 0
  288.  
  289. static inline int
  290. __copy_to_user_swizzled(char __user *cpu_vaddr,
  291.                         const char *gpu_vaddr, int gpu_offset,
  292.                 int length)
  293. {
  294.         int ret, cpu_offset = 0;
  295.  
  296.         while (length > 0) {
  297.                 int cacheline_end = ALIGN(gpu_offset + 1, 64);
  298.                 int this_length = min(cacheline_end - gpu_offset, length);
  299.                 int swizzled_gpu_offset = gpu_offset ^ 64;
  300.  
  301.                 ret = __copy_to_user(cpu_vaddr + cpu_offset,
  302.                                      gpu_vaddr + swizzled_gpu_offset,
  303.                                      this_length);
  304.                 if (ret)
  305.                         return ret + length;
  306.  
  307.                 cpu_offset += this_length;
  308.                 gpu_offset += this_length;
  309.                 length -= this_length;
  310.         }
  311.  
  312.         return 0;
  313. }
  314.  
  315. static inline int
  316. __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
  317.                           const char __user *cpu_vaddr,
  318.                           int length)
  319. {
  320.         int ret, cpu_offset = 0;
  321.  
  322.         while (length > 0) {
  323.                 int cacheline_end = ALIGN(gpu_offset + 1, 64);
  324.                 int this_length = min(cacheline_end - gpu_offset, length);
  325.                 int swizzled_gpu_offset = gpu_offset ^ 64;
  326.  
  327.                 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
  328.                                cpu_vaddr + cpu_offset,
  329.                                this_length);
  330.                 if (ret)
  331.                         return ret + length;
  332.  
  333.                 cpu_offset += this_length;
  334.                 gpu_offset += this_length;
  335.                 length -= this_length;
  336.         }
  337.  
  338.         return 0;
  339. }
  340.  
  341. /* Per-page copy function for the shmem pread fastpath.
  342.  * Flushes invalid cachelines before reading the target if
  343.  * needs_clflush is set. */
  344. static int
  345. shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
  346.                  char __user *user_data,
  347.                  bool page_do_bit17_swizzling, bool needs_clflush)
  348. {
  349.                 char *vaddr;
  350.                 int ret;
  351.  
  352.         if (unlikely(page_do_bit17_swizzling))
  353.                 return -EINVAL;
  354.  
  355.                 vaddr = kmap_atomic(page);
  356.         if (needs_clflush)
  357.                 drm_clflush_virt_range(vaddr + shmem_page_offset,
  358.                                        page_length);
  359.                 ret = __copy_to_user_inatomic(user_data,
  360.                                       vaddr + shmem_page_offset,
  361.                                               page_length);
  362.                 kunmap_atomic(vaddr);
  363.  
  364.         return ret ? -EFAULT : 0;
  365. }
  366.  
  367. static void
  368. shmem_clflush_swizzled_range(char *addr, unsigned long length,
  369.                              bool swizzled)
  370. {
  371.         if (unlikely(swizzled)) {
  372.                 unsigned long start = (unsigned long) addr;
  373.                 unsigned long end = (unsigned long) addr + length;
  374.  
  375.                 /* For swizzling simply ensure that we always flush both
  376.                  * channels. Lame, but simple and it works. Swizzled
  377.                  * pwrite/pread is far from a hotpath - current userspace
  378.                  * doesn't use it at all. */
  379.                 start = round_down(start, 128);
  380.                 end = round_up(end, 128);
  381.  
  382.                 drm_clflush_virt_range((void *)start, end - start);
  383.         } else {
  384.                 drm_clflush_virt_range(addr, length);
  385.         }
  386.  
  387. }
  388.  
  389. /* Only difference to the fast-path function is that this can handle bit17
  390.  * and uses non-atomic copy and kmap functions. */
  391. static int
  392. shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
  393.                  char __user *user_data,
  394.                  bool page_do_bit17_swizzling, bool needs_clflush)
  395. {
  396.         char *vaddr;
  397.         int ret;
  398.  
  399.         vaddr = kmap(page);
  400.         if (needs_clflush)
  401.                 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  402.                                              page_length,
  403.                                              page_do_bit17_swizzling);
  404.  
  405.         if (page_do_bit17_swizzling)
  406.                 ret = __copy_to_user_swizzled(user_data,
  407.                                               vaddr, shmem_page_offset,
  408.                                               page_length);
  409.         else
  410.                 ret = __copy_to_user(user_data,
  411.                                      vaddr + shmem_page_offset,
  412.                                      page_length);
  413.         kunmap(page);
  414.  
  415.         return ret ? - EFAULT : 0;
  416. }
  417.  
  418. static int
  419. i915_gem_shmem_pread(struct drm_device *dev,
  420.                           struct drm_i915_gem_object *obj,
  421.                           struct drm_i915_gem_pread *args,
  422.                           struct drm_file *file)
  423. {
  424.         char __user *user_data;
  425.         ssize_t remain;
  426.         loff_t offset;
  427.         int shmem_page_offset, page_length, ret = 0;
  428.         int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  429.         int prefaulted = 0;
  430.         int needs_clflush = 0;
  431.         struct sg_page_iter sg_iter;
  432.  
  433.         user_data = to_user_ptr(args->data_ptr);
  434.         remain = args->size;
  435.  
  436.         obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  437.  
  438.         ret = i915_gem_obj_prepare_shmem_read(obj, &needs_clflush);
  439.         if (ret)
  440.                 return ret;
  441.  
  442.         offset = args->offset;
  443.  
  444.         for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  445.                          offset >> PAGE_SHIFT) {
  446.                 struct page *page = sg_page_iter_page(&sg_iter);
  447.  
  448.                 if (remain <= 0)
  449.                         break;
  450.  
  451.                 /* Operation in this page
  452.                  *
  453.                  * shmem_page_offset = offset within page in shmem file
  454.                  * page_length = bytes to copy for this page
  455.                  */
  456.                 shmem_page_offset = offset_in_page(offset);
  457.                 page_length = remain;
  458.                 if ((shmem_page_offset + page_length) > PAGE_SIZE)
  459.                         page_length = PAGE_SIZE - shmem_page_offset;
  460.  
  461.                 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  462.                         (page_to_phys(page) & (1 << 17)) != 0;
  463.  
  464.                 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
  465.                                        user_data, page_do_bit17_swizzling,
  466.                                        needs_clflush);
  467.                 if (ret == 0)
  468.                         goto next_page;
  469.  
  470.                 mutex_unlock(&dev->struct_mutex);
  471.  
  472.                 if (likely(!i915.prefault_disable) && !prefaulted) {
  473.                         ret = fault_in_multipages_writeable(user_data, remain);
  474.                         /* Userspace is tricking us, but we've already clobbered
  475.                          * its pages with the prefault and promised to write the
  476.                          * data up to the first fault. Hence ignore any errors
  477.                          * and just continue. */
  478.                         (void)ret;
  479.                         prefaulted = 1;
  480.                 }
  481.  
  482.                 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
  483.                                        user_data, page_do_bit17_swizzling,
  484.                                        needs_clflush);
  485.  
  486.                 mutex_lock(&dev->struct_mutex);
  487.  
  488.                 if (ret)
  489.                         goto out;
  490.  
  491. next_page:
  492.                 remain -= page_length;
  493.                 user_data += page_length;
  494.                 offset += page_length;
  495.         }
  496.  
  497. out:
  498.         i915_gem_object_unpin_pages(obj);
  499.  
  500.         return ret;
  501. }
  502.  
  503. /**
  504.  * Reads data from the object referenced by handle.
  505.  *
  506.  * On error, the contents of *data are undefined.
  507.  */
  508. int
  509. i915_gem_pread_ioctl(struct drm_device *dev, void *data,
  510.                      struct drm_file *file)
  511. {
  512.         struct drm_i915_gem_pread *args = data;
  513.         struct drm_i915_gem_object *obj;
  514.         int ret = 0;
  515.  
  516.         if (args->size == 0)
  517.                 return 0;
  518.  
  519.         if (!access_ok(VERIFY_WRITE,
  520.                        to_user_ptr(args->data_ptr),
  521.                        args->size))
  522.                 return -EFAULT;
  523.  
  524.         ret = i915_mutex_lock_interruptible(dev);
  525.         if (ret)
  526.                 return ret;
  527.  
  528.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  529.         if (&obj->base == NULL) {
  530.                 ret = -ENOENT;
  531.                 goto unlock;
  532.         }
  533.  
  534.         /* Bounds check source.  */
  535.         if (args->offset > obj->base.size ||
  536.             args->size > obj->base.size - args->offset) {
  537.                 ret = -EINVAL;
  538.                 goto out;
  539.         }
  540.  
  541.         /* prime objects have no backing filp to GEM pread/pwrite
  542.          * pages from.
  543.          */
  544.         if (!obj->base.filp) {
  545.                 ret = -EINVAL;
  546.                 goto out;
  547.         }
  548.  
  549.         trace_i915_gem_object_pread(obj, args->offset, args->size);
  550.  
  551.         ret = i915_gem_shmem_pread(dev, obj, args, file);
  552.  
  553. out:
  554.         drm_gem_object_unreference(&obj->base);
  555. unlock:
  556.         mutex_unlock(&dev->struct_mutex);
  557.         return ret;
  558. }
  559.  
  560. /* This is the fast write path which cannot handle
  561.  * page faults in the source data
  562.  */
  563.  
  564. static inline int
  565. fast_user_write(struct io_mapping *mapping,
  566.                 loff_t page_base, int page_offset,
  567.                 char __user *user_data,
  568.                 int length)
  569. {
  570.         void __iomem *vaddr_atomic;
  571.         void *vaddr;
  572.         unsigned long unwritten;
  573.  
  574.         vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
  575.         /* We can use the cpu mem copy function because this is X86. */
  576.         vaddr = (void __force*)vaddr_atomic + page_offset;
  577.         unwritten = __copy_from_user_inatomic_nocache(vaddr,
  578.                                                       user_data, length);
  579.         io_mapping_unmap_atomic(vaddr_atomic);
  580.         return unwritten;
  581. }
  582. #endif
  583.  
  584. #define offset_in_page(p)       ((unsigned long)(p) & ~PAGE_MASK)
  585. /**
  586.  * This is the fast pwrite path, where we copy the data directly from the
  587.  * user into the GTT, uncached.
  588.  */
  589. static int
  590. i915_gem_gtt_pwrite_fast(struct drm_device *dev,
  591.                          struct drm_i915_gem_object *obj,
  592.                          struct drm_i915_gem_pwrite *args,
  593.                          struct drm_file *file)
  594. {
  595.         struct drm_i915_private *dev_priv = dev->dev_private;
  596.         ssize_t remain;
  597.         loff_t offset, page_base;
  598.         char __user *user_data;
  599.         int page_offset, page_length, ret;
  600.  
  601.         ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  602.         if (ret)
  603.                 goto out;
  604.  
  605.         ret = i915_gem_object_set_to_gtt_domain(obj, true);
  606.         if (ret)
  607.                 goto out_unpin;
  608.  
  609.         ret = i915_gem_object_put_fence(obj);
  610.         if (ret)
  611.                 goto out_unpin;
  612.  
  613.         user_data = to_user_ptr(args->data_ptr);
  614.         remain = args->size;
  615.  
  616.         offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
  617.  
  618.         while (remain > 0) {
  619.                 /* Operation in this page
  620.                  *
  621.                  * page_base = page offset within aperture
  622.                  * page_offset = offset within page
  623.                  * page_length = bytes to copy for this page
  624.                  */
  625.                 page_base = offset & PAGE_MASK;
  626.                 page_offset = offset_in_page(offset);
  627.                 page_length = remain;
  628.                 if ((page_offset + remain) > PAGE_SIZE)
  629.                         page_length = PAGE_SIZE - page_offset;
  630.  
  631.         MapPage(dev_priv->gtt.mappable, dev_priv->gtt.mappable_base+page_base, PG_SW);
  632.  
  633.         memcpy((char*)dev_priv->gtt.mappable+page_offset, user_data, page_length);
  634.  
  635.                 remain -= page_length;
  636.                 user_data += page_length;
  637.                 offset += page_length;
  638.         }
  639.  
  640. out_unpin:
  641.         i915_gem_object_ggtt_unpin(obj);
  642. out:
  643.     return ret;
  644. }
  645.  
  646. /* Per-page copy function for the shmem pwrite fastpath.
  647.  * Flushes invalid cachelines before writing to the target if
  648.  * needs_clflush_before is set and flushes out any written cachelines after
  649.  * writing if needs_clflush is set. */
  650. static int
  651. shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
  652.                   char __user *user_data,
  653.                   bool page_do_bit17_swizzling,
  654.                   bool needs_clflush_before,
  655.                   bool needs_clflush_after)
  656. {
  657.         char *vaddr;
  658.         int ret;
  659.  
  660.         if (unlikely(page_do_bit17_swizzling))
  661.                 return -EINVAL;
  662.  
  663.         vaddr = kmap_atomic(page);
  664.         if (needs_clflush_before)
  665.                 drm_clflush_virt_range(vaddr + shmem_page_offset,
  666.                                        page_length);
  667.         memcpy(vaddr + shmem_page_offset,
  668.                                                 user_data,
  669.                                                 page_length);
  670.         if (needs_clflush_after)
  671.                 drm_clflush_virt_range(vaddr + shmem_page_offset,
  672.                                        page_length);
  673.         kunmap_atomic(vaddr);
  674.  
  675.         return ret ? -EFAULT : 0;
  676. }
  677. #if 0
  678.  
  679. /* Only difference to the fast-path function is that this can handle bit17
  680.  * and uses non-atomic copy and kmap functions. */
  681. static int
  682. shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
  683.                   char __user *user_data,
  684.                   bool page_do_bit17_swizzling,
  685.                   bool needs_clflush_before,
  686.                   bool needs_clflush_after)
  687. {
  688.         char *vaddr;
  689.         int ret;
  690.  
  691.         vaddr = kmap(page);
  692.         if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
  693.                 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  694.                                              page_length,
  695.                                              page_do_bit17_swizzling);
  696.         if (page_do_bit17_swizzling)
  697.                 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
  698.                                                 user_data,
  699.                                                 page_length);
  700.         else
  701.                 ret = __copy_from_user(vaddr + shmem_page_offset,
  702.                                        user_data,
  703.                                        page_length);
  704.         if (needs_clflush_after)
  705.                 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
  706.                                              page_length,
  707.                                              page_do_bit17_swizzling);
  708.         kunmap(page);
  709.  
  710.         return ret ? -EFAULT : 0;
  711. }
  712. #endif
  713.  
  714.  
  715. static int
  716. i915_gem_shmem_pwrite(struct drm_device *dev,
  717.                       struct drm_i915_gem_object *obj,
  718.                       struct drm_i915_gem_pwrite *args,
  719.                       struct drm_file *file)
  720. {
  721.         ssize_t remain;
  722.         loff_t offset;
  723.         char __user *user_data;
  724.         int shmem_page_offset, page_length, ret = 0;
  725.         int obj_do_bit17_swizzling, page_do_bit17_swizzling;
  726.         int hit_slowpath = 0;
  727.         int needs_clflush_after = 0;
  728.         int needs_clflush_before = 0;
  729.         struct sg_page_iter sg_iter;
  730.  
  731.         user_data = to_user_ptr(args->data_ptr);
  732.         remain = args->size;
  733.  
  734.         obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
  735.  
  736.         if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  737.                 /* If we're not in the cpu write domain, set ourself into the gtt
  738.                  * write domain and manually flush cachelines (if required). This
  739.                  * optimizes for the case when the gpu will use the data
  740.                  * right away and we therefore have to clflush anyway. */
  741.                 needs_clflush_after = cpu_write_needs_clflush(obj);
  742.                 ret = i915_gem_object_wait_rendering(obj, false);
  743.                         if (ret)
  744.                                 return ret;
  745.  
  746.                 i915_gem_object_retire(obj);
  747.                 }
  748.         /* Same trick applies to invalidate partially written cachelines read
  749.          * before writing. */
  750.         if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
  751.                 needs_clflush_before =
  752.                         !cpu_cache_is_coherent(dev, obj->cache_level);
  753.  
  754.         ret = i915_gem_object_get_pages(obj);
  755.         if (ret)
  756.                 return ret;
  757.  
  758.         i915_gem_object_pin_pages(obj);
  759.  
  760.         offset = args->offset;
  761.         obj->dirty = 1;
  762.  
  763.         for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
  764.                          offset >> PAGE_SHIFT) {
  765.                 struct page *page = sg_page_iter_page(&sg_iter);
  766.                 int partial_cacheline_write;
  767.  
  768.                 if (remain <= 0)
  769.                         break;
  770.  
  771.                 /* Operation in this page
  772.                  *
  773.                  * shmem_page_offset = offset within page in shmem file
  774.                  * page_length = bytes to copy for this page
  775.                  */
  776.                 shmem_page_offset = offset_in_page(offset);
  777.  
  778.                 page_length = remain;
  779.                 if ((shmem_page_offset + page_length) > PAGE_SIZE)
  780.                         page_length = PAGE_SIZE - shmem_page_offset;
  781.  
  782.                 /* If we don't overwrite a cacheline completely we need to be
  783.                  * careful to have up-to-date data by first clflushing. Don't
  784.                  * overcomplicate things and flush the entire patch. */
  785.                 partial_cacheline_write = needs_clflush_before &&
  786.                         ((shmem_page_offset | page_length)
  787.                                 & (x86_clflush_size - 1));
  788.  
  789.                 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
  790.                         (page_to_phys(page) & (1 << 17)) != 0;
  791.  
  792.                 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
  793.                                         user_data, page_do_bit17_swizzling,
  794.                                         partial_cacheline_write,
  795.                                         needs_clflush_after);
  796.                 if (ret == 0)
  797.                         goto next_page;
  798.  
  799.                 hit_slowpath = 1;
  800.                 mutex_unlock(&dev->struct_mutex);
  801.                 dbgprintf("%s need shmem_pwrite_slow\n",__FUNCTION__);
  802.  
  803. //              ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
  804. //                                      user_data, page_do_bit17_swizzling,
  805. //                                      partial_cacheline_write,
  806. //                                      needs_clflush_after);
  807.  
  808.                 mutex_lock(&dev->struct_mutex);
  809.  
  810.                 if (ret)
  811.                         goto out;
  812.  
  813. next_page:
  814.                 remain -= page_length;
  815.                 user_data += page_length;
  816.                 offset += page_length;
  817.         }
  818.  
  819. out:
  820.         i915_gem_object_unpin_pages(obj);
  821.  
  822.         if (hit_slowpath) {
  823.                 /*
  824.                  * Fixup: Flush cpu caches in case we didn't flush the dirty
  825.                  * cachelines in-line while writing and the object moved
  826.                  * out of the cpu write domain while we've dropped the lock.
  827.                  */
  828.                 if (!needs_clflush_after &&
  829.                     obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
  830.                         if (i915_gem_clflush_object(obj, obj->pin_display))
  831.                         i915_gem_chipset_flush(dev);
  832.                 }
  833.         }
  834.  
  835.         if (needs_clflush_after)
  836.                 i915_gem_chipset_flush(dev);
  837.  
  838.         return ret;
  839. }
  840.  
  841. /**
  842.  * Writes data to the object referenced by handle.
  843.  *
  844.  * On error, the contents of the buffer that were to be modified are undefined.
  845.  */
  846. int
  847. i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
  848.                       struct drm_file *file)
  849. {
  850.         struct drm_i915_gem_pwrite *args = data;
  851.         struct drm_i915_gem_object *obj;
  852.         int ret;
  853.  
  854.         if (args->size == 0)
  855.                 return 0;
  856.  
  857.  
  858.         ret = i915_mutex_lock_interruptible(dev);
  859.         if (ret)
  860.                 return ret;
  861.  
  862.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  863.         if (&obj->base == NULL) {
  864.                 ret = -ENOENT;
  865.                 goto unlock;
  866.         }
  867.  
  868.         /* Bounds check destination. */
  869.         if (args->offset > obj->base.size ||
  870.             args->size > obj->base.size - args->offset) {
  871.                 ret = -EINVAL;
  872.                 goto out;
  873.         }
  874.  
  875.         /* prime objects have no backing filp to GEM pread/pwrite
  876.          * pages from.
  877.          */
  878.         if (!obj->base.filp) {
  879.                 ret = -EINVAL;
  880.                 goto out;
  881.         }
  882.  
  883.         trace_i915_gem_object_pwrite(obj, args->offset, args->size);
  884.  
  885.         ret = -EFAULT;
  886.         /* We can only do the GTT pwrite on untiled buffers, as otherwise
  887.          * it would end up going through the fenced access, and we'll get
  888.          * different detiling behavior between reading and writing.
  889.          * pread/pwrite currently are reading and writing from the CPU
  890.          * perspective, requiring manual detiling by the client.
  891.          */
  892.         if (obj->tiling_mode == I915_TILING_NONE &&
  893.             obj->base.write_domain != I915_GEM_DOMAIN_CPU &&
  894.             cpu_write_needs_clflush(obj)) {
  895.                 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
  896.                 /* Note that the gtt paths might fail with non-page-backed user
  897.                  * pointers (e.g. gtt mappings when moving data between
  898.                  * textures). Fallback to the shmem path in that case. */
  899.         }
  900.  
  901.         if (ret == -EFAULT || ret == -ENOSPC)
  902.        ret = i915_gem_shmem_pwrite(dev, obj, args, file);
  903.  
  904. out:
  905.         drm_gem_object_unreference(&obj->base);
  906. unlock:
  907.         mutex_unlock(&dev->struct_mutex);
  908.         return ret;
  909. }
  910.  
  911. int
  912. i915_gem_check_wedge(struct i915_gpu_error *error,
  913.                      bool interruptible)
  914. {
  915.         if (i915_reset_in_progress(error)) {
  916.                 /* Non-interruptible callers can't handle -EAGAIN, hence return
  917.                  * -EIO unconditionally for these. */
  918.                 if (!interruptible)
  919.                         return -EIO;
  920.  
  921.                 /* Recovery complete, but the reset failed ... */
  922.                 if (i915_terminally_wedged(error))
  923.                         return -EIO;
  924.  
  925.                 return -EAGAIN;
  926.         }
  927.  
  928.         return 0;
  929. }
  930.  
  931. /*
  932.  * Compare seqno against outstanding lazy request. Emit a request if they are
  933.  * equal.
  934.  */
  935. int
  936. i915_gem_check_olr(struct intel_engine_cs *ring, u32 seqno)
  937. {
  938.         int ret;
  939.  
  940.         BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
  941.  
  942.         ret = 0;
  943.         if (seqno == ring->outstanding_lazy_seqno)
  944.                 ret = i915_add_request(ring, NULL);
  945.  
  946.         return ret;
  947. }
  948.  
  949. static void fake_irq(unsigned long data)
  950. {
  951. //      wake_up_process((struct task_struct *)data);
  952. }
  953.  
  954. static bool missed_irq(struct drm_i915_private *dev_priv,
  955.                        struct intel_engine_cs *ring)
  956. {
  957.         return test_bit(ring->id, &dev_priv->gpu_error.missed_irq_rings);
  958. }
  959.  
  960. static bool can_wait_boost(struct drm_i915_file_private *file_priv)
  961. {
  962.         if (file_priv == NULL)
  963.                 return true;
  964.  
  965.         return !atomic_xchg(&file_priv->rps_wait_boost, true);
  966. }
  967.  
  968. /**
  969.  * __i915_wait_seqno - wait until execution of seqno has finished
  970.  * @ring: the ring expected to report seqno
  971.  * @seqno: duh!
  972.  * @reset_counter: reset sequence associated with the given seqno
  973.  * @interruptible: do an interruptible wait (normally yes)
  974.  * @timeout: in - how long to wait (NULL forever); out - how much time remaining
  975.  *
  976.  * Note: It is of utmost importance that the passed in seqno and reset_counter
  977.  * values have been read by the caller in an smp safe manner. Where read-side
  978.  * locks are involved, it is sufficient to read the reset_counter before
  979.  * unlocking the lock that protects the seqno. For lockless tricks, the
  980.  * reset_counter _must_ be read before, and an appropriate smp_rmb must be
  981.  * inserted.
  982.  *
  983.  * Returns 0 if the seqno was found within the alloted time. Else returns the
  984.  * errno with remaining time filled in timeout argument.
  985.  */
  986. int __i915_wait_seqno(struct intel_engine_cs *ring, u32 seqno,
  987.                         unsigned reset_counter,
  988.                         bool interruptible,
  989.                         s64 *timeout,
  990.                         struct drm_i915_file_private *file_priv)
  991. {
  992.         struct drm_device *dev = ring->dev;
  993.         struct drm_i915_private *dev_priv = dev->dev_private;
  994.         const bool irq_test_in_progress =
  995.                 ACCESS_ONCE(dev_priv->gpu_error.test_irq_rings) & intel_ring_flag(ring);
  996.         unsigned long timeout_expire;
  997.         s64 before, now;
  998.  
  999.     wait_queue_t __wait;
  1000.         int ret;
  1001.  
  1002.         WARN(!intel_irqs_enabled(dev_priv), "IRQs disabled");
  1003.  
  1004.         if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
  1005.                 return 0;
  1006.  
  1007.         timeout_expire = timeout ?
  1008.                 jiffies + nsecs_to_jiffies_timeout((u64)*timeout) : 0;
  1009.  
  1010.         if (INTEL_INFO(dev)->gen >= 6 && ring->id == RCS && can_wait_boost(file_priv)) {
  1011.                 gen6_rps_boost(dev_priv);
  1012.                 if (file_priv)
  1013.                         mod_delayed_work(dev_priv->wq,
  1014.                                          &file_priv->mm.idle_work,
  1015.                                          msecs_to_jiffies(100));
  1016.         }
  1017.  
  1018.         if (!irq_test_in_progress && WARN_ON(!ring->irq_get(ring)))
  1019.                 return -ENODEV;
  1020.  
  1021.     INIT_LIST_HEAD(&__wait.task_list);
  1022.     __wait.evnt = CreateEvent(NULL, MANUAL_DESTROY);
  1023.  
  1024.         /* Record current time in case interrupted by signal, or wedged */
  1025.         trace_i915_gem_request_wait_begin(ring, seqno);
  1026.  
  1027.         for (;;) {
  1028.         unsigned long flags;
  1029.  
  1030.                 /* We need to check whether any gpu reset happened in between
  1031.                  * the caller grabbing the seqno and now ... */
  1032.                 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter)) {
  1033.                         /* ... but upgrade the -EAGAIN to an -EIO if the gpu
  1034.                          * is truely gone. */
  1035.                         ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1036.                         if (ret == 0)
  1037.                                 ret = -EAGAIN;
  1038.                         break;
  1039.                 }
  1040.  
  1041.                 if (i915_seqno_passed(ring->get_seqno(ring, false), seqno)) {
  1042.                         ret = 0;
  1043.                         break;
  1044.                 }
  1045.  
  1046.         if (timeout && time_after_eq(jiffies, timeout_expire)) {
  1047.                         ret = -ETIME;
  1048.                         break;
  1049.                 }
  1050.  
  1051.         spin_lock_irqsave(&ring->irq_queue.lock, flags);
  1052.         if (list_empty(&__wait.task_list))
  1053.             __add_wait_queue(&ring->irq_queue, &__wait);
  1054.         spin_unlock_irqrestore(&ring->irq_queue.lock, flags);
  1055.  
  1056.         WaitEventTimeout(__wait.evnt, 1);
  1057.  
  1058.         if (!list_empty(&__wait.task_list)) {
  1059.             spin_lock_irqsave(&ring->irq_queue.lock, flags);
  1060.             list_del_init(&__wait.task_list);
  1061.             spin_unlock_irqrestore(&ring->irq_queue.lock, flags);
  1062.         }
  1063.     };
  1064.     trace_i915_gem_request_wait_end(ring, seqno);
  1065.  
  1066.     DestroyEvent(__wait.evnt);
  1067.  
  1068.         if (!irq_test_in_progress)
  1069.         ring->irq_put(ring);
  1070.  
  1071. //      finish_wait(&ring->irq_queue, &wait);
  1072.         return ret;
  1073. }
  1074.  
  1075. /**
  1076.  * Waits for a sequence number to be signaled, and cleans up the
  1077.  * request and object lists appropriately for that event.
  1078.  */
  1079. int
  1080. i915_wait_seqno(struct intel_engine_cs *ring, uint32_t seqno)
  1081. {
  1082.         struct drm_device *dev = ring->dev;
  1083.         struct drm_i915_private *dev_priv = dev->dev_private;
  1084.         bool interruptible = dev_priv->mm.interruptible;
  1085.         unsigned reset_counter;
  1086.         int ret;
  1087.  
  1088.         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1089.         BUG_ON(seqno == 0);
  1090.  
  1091.         ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
  1092.         if (ret)
  1093.                 return ret;
  1094.  
  1095.         ret = i915_gem_check_olr(ring, seqno);
  1096.         if (ret)
  1097.                 return ret;
  1098.  
  1099.         reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1100.         return __i915_wait_seqno(ring, seqno, reset_counter, interruptible,
  1101.                                  NULL, NULL);
  1102. }
  1103.  
  1104. static int
  1105. i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj)
  1106. {
  1107.         if (!obj->active)
  1108.                 return 0;
  1109.  
  1110.         /* Manually manage the write flush as we may have not yet
  1111.          * retired the buffer.
  1112.          *
  1113.          * Note that the last_write_seqno is always the earlier of
  1114.          * the two (read/write) seqno, so if we haved successfully waited,
  1115.          * we know we have passed the last write.
  1116.          */
  1117.         obj->last_write_seqno = 0;
  1118.  
  1119.         return 0;
  1120. }
  1121.  
  1122. /**
  1123.  * Ensures that all rendering to the object has completed and the object is
  1124.  * safe to unbind from the GTT or access from the CPU.
  1125.  */
  1126. static __must_check int
  1127. i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
  1128.                                bool readonly)
  1129. {
  1130.         struct intel_engine_cs *ring = obj->ring;
  1131.         u32 seqno;
  1132.         int ret;
  1133.  
  1134.         seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1135.         if (seqno == 0)
  1136.                 return 0;
  1137.  
  1138.         ret = i915_wait_seqno(ring, seqno);
  1139.     if (ret)
  1140.         return ret;
  1141.  
  1142.         return i915_gem_object_wait_rendering__tail(obj);
  1143. }
  1144.  
  1145. /* A nonblocking variant of the above wait. This is a highly dangerous routine
  1146.  * as the object state may change during this call.
  1147.  */
  1148. static __must_check int
  1149. i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
  1150.                                             struct drm_i915_file_private *file_priv,
  1151.                                             bool readonly)
  1152. {
  1153.         struct drm_device *dev = obj->base.dev;
  1154.         struct drm_i915_private *dev_priv = dev->dev_private;
  1155.         struct intel_engine_cs *ring = obj->ring;
  1156.         unsigned reset_counter;
  1157.         u32 seqno;
  1158.         int ret;
  1159.  
  1160.         BUG_ON(!mutex_is_locked(&dev->struct_mutex));
  1161.         BUG_ON(!dev_priv->mm.interruptible);
  1162.  
  1163.         seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
  1164.         if (seqno == 0)
  1165.                 return 0;
  1166.  
  1167.         ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
  1168.         if (ret)
  1169.                 return ret;
  1170.  
  1171.         ret = i915_gem_check_olr(ring, seqno);
  1172.         if (ret)
  1173.                 return ret;
  1174.  
  1175.         reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  1176.         mutex_unlock(&dev->struct_mutex);
  1177.         ret = __i915_wait_seqno(ring, seqno, reset_counter, true, NULL,
  1178.                                 file_priv);
  1179.         mutex_lock(&dev->struct_mutex);
  1180.         if (ret)
  1181.                 return ret;
  1182.  
  1183.         return i915_gem_object_wait_rendering__tail(obj);
  1184. }
  1185.  
  1186. /**
  1187.  * Called when user space prepares to use an object with the CPU, either
  1188.  * through the mmap ioctl's mapping or a GTT mapping.
  1189.  */
  1190. int
  1191. i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
  1192.                           struct drm_file *file)
  1193. {
  1194.         struct drm_i915_gem_set_domain *args = data;
  1195.         struct drm_i915_gem_object *obj;
  1196.         uint32_t read_domains = args->read_domains;
  1197.         uint32_t write_domain = args->write_domain;
  1198.         int ret;
  1199.  
  1200.         /* Only handle setting domains to types used by the CPU. */
  1201.         if (write_domain & I915_GEM_GPU_DOMAINS)
  1202.                 return -EINVAL;
  1203.  
  1204.         if (read_domains & I915_GEM_GPU_DOMAINS)
  1205.                 return -EINVAL;
  1206.  
  1207.         /* Having something in the write domain implies it's in the read
  1208.          * domain, and only that read domain.  Enforce that in the request.
  1209.          */
  1210.         if (write_domain != 0 && read_domains != write_domain)
  1211.                 return -EINVAL;
  1212.  
  1213.         ret = i915_mutex_lock_interruptible(dev);
  1214.         if (ret)
  1215.                 return ret;
  1216.  
  1217.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1218.         if (&obj->base == NULL) {
  1219.                 ret = -ENOENT;
  1220.                 goto unlock;
  1221.         }
  1222.  
  1223.         /* Try to flush the object off the GPU without holding the lock.
  1224.          * We will repeat the flush holding the lock in the normal manner
  1225.          * to catch cases where we are gazumped.
  1226.          */
  1227.         ret = i915_gem_object_wait_rendering__nonblocking(obj,
  1228.                                                           file->driver_priv,
  1229.                                                           !write_domain);
  1230.         if (ret)
  1231.                 goto unref;
  1232.  
  1233.         if (read_domains & I915_GEM_DOMAIN_GTT) {
  1234.                 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
  1235.  
  1236.                 /* Silently promote "you're not bound, there was nothing to do"
  1237.                  * to success, since the client was just asking us to
  1238.                  * make sure everything was done.
  1239.                  */
  1240.                 if (ret == -EINVAL)
  1241.                         ret = 0;
  1242.         } else {
  1243.                 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
  1244.         }
  1245.  
  1246. unref:
  1247.         drm_gem_object_unreference(&obj->base);
  1248. unlock:
  1249.         mutex_unlock(&dev->struct_mutex);
  1250.         return ret;
  1251. }
  1252.  
  1253. /**
  1254.  * Called when user space has done writes to this buffer
  1255.  */
  1256. int
  1257. i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
  1258.                          struct drm_file *file)
  1259. {
  1260.         struct drm_i915_gem_sw_finish *args = data;
  1261.         struct drm_i915_gem_object *obj;
  1262.         int ret = 0;
  1263.  
  1264.         ret = i915_mutex_lock_interruptible(dev);
  1265.         if (ret)
  1266.                 return ret;
  1267.  
  1268.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  1269.         if (&obj->base == NULL) {
  1270.                 ret = -ENOENT;
  1271.                 goto unlock;
  1272.         }
  1273.  
  1274.         /* Pinned buffers may be scanout, so flush the cache */
  1275.         if (obj->pin_display)
  1276.                 i915_gem_object_flush_cpu_write_domain(obj, true);
  1277.  
  1278.         drm_gem_object_unreference(&obj->base);
  1279. unlock:
  1280.         mutex_unlock(&dev->struct_mutex);
  1281.         return ret;
  1282. }
  1283.  
  1284. /**
  1285.  * Maps the contents of an object, returning the address it is mapped
  1286.  * into.
  1287.  *
  1288.  * While the mapping holds a reference on the contents of the object, it doesn't
  1289.  * imply a ref on the object itself.
  1290.  *
  1291.  * IMPORTANT:
  1292.  *
  1293.  * DRM driver writers who look a this function as an example for how to do GEM
  1294.  * mmap support, please don't implement mmap support like here. The modern way
  1295.  * to implement DRM mmap support is with an mmap offset ioctl (like
  1296.  * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
  1297.  * That way debug tooling like valgrind will understand what's going on, hiding
  1298.  * the mmap call in a driver private ioctl will break that. The i915 driver only
  1299.  * does cpu mmaps this way because we didn't know better.
  1300.  */
  1301. int
  1302. i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
  1303.                     struct drm_file *file)
  1304. {
  1305.         struct drm_i915_gem_mmap *args = data;
  1306.         struct drm_gem_object *obj;
  1307.         unsigned long addr;
  1308.  
  1309.         obj = drm_gem_object_lookup(dev, file, args->handle);
  1310.         if (obj == NULL)
  1311.                 return -ENOENT;
  1312.  
  1313.         /* prime objects have no backing filp to GEM mmap
  1314.          * pages from.
  1315.          */
  1316.         if (!obj->filp) {
  1317.                 drm_gem_object_unreference_unlocked(obj);
  1318.                 return -EINVAL;
  1319.         }
  1320.  
  1321.     addr = vm_mmap(obj->filp, 0, args->size,
  1322.               PROT_READ | PROT_WRITE, MAP_SHARED,
  1323.               args->offset);
  1324.         drm_gem_object_unreference_unlocked(obj);
  1325.     if (IS_ERR((void *)addr))
  1326.         return addr;
  1327.  
  1328.         args->addr_ptr = (uint64_t) addr;
  1329.  
  1330.     return 0;
  1331. }
  1332.  
  1333.  
  1334.  
  1335.  
  1336.  
  1337.  
  1338.  
  1339.  
  1340.  
  1341.  
  1342.  
  1343.  
  1344.  
  1345. /**
  1346.  * i915_gem_release_mmap - remove physical page mappings
  1347.  * @obj: obj in question
  1348.  *
  1349.  * Preserve the reservation of the mmapping with the DRM core code, but
  1350.  * relinquish ownership of the pages back to the system.
  1351.  *
  1352.  * It is vital that we remove the page mapping if we have mapped a tiled
  1353.  * object through the GTT and then lose the fence register due to
  1354.  * resource pressure. Similarly if the object has been moved out of the
  1355.  * aperture, than pages mapped into userspace must be revoked. Removing the
  1356.  * mapping will then trigger a page fault on the next user access, allowing
  1357.  * fixup by i915_gem_fault().
  1358.  */
  1359. void
  1360. i915_gem_release_mmap(struct drm_i915_gem_object *obj)
  1361. {
  1362.         if (!obj->fault_mappable)
  1363.                 return;
  1364.  
  1365. //      drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
  1366.         obj->fault_mappable = false;
  1367. }
  1368.  
  1369. uint32_t
  1370. i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
  1371. {
  1372.         uint32_t gtt_size;
  1373.  
  1374.         if (INTEL_INFO(dev)->gen >= 4 ||
  1375.             tiling_mode == I915_TILING_NONE)
  1376.                 return size;
  1377.  
  1378.         /* Previous chips need a power-of-two fence region when tiling */
  1379.         if (INTEL_INFO(dev)->gen == 3)
  1380.                 gtt_size = 1024*1024;
  1381.         else
  1382.                 gtt_size = 512*1024;
  1383.  
  1384.         while (gtt_size < size)
  1385.                 gtt_size <<= 1;
  1386.  
  1387.         return gtt_size;
  1388. }
  1389.  
  1390. /**
  1391.  * i915_gem_get_gtt_alignment - return required GTT alignment for an object
  1392.  * @obj: object to check
  1393.  *
  1394.  * Return the required GTT alignment for an object, taking into account
  1395.  * potential fence register mapping.
  1396.  */
  1397. uint32_t
  1398. i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
  1399.                            int tiling_mode, bool fenced)
  1400. {
  1401.         /*
  1402.          * Minimum alignment is 4k (GTT page size), but might be greater
  1403.          * if a fence register is needed for the object.
  1404.          */
  1405.         if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
  1406.             tiling_mode == I915_TILING_NONE)
  1407.                 return 4096;
  1408.  
  1409.         /*
  1410.          * Previous chips need to be aligned to the size of the smallest
  1411.          * fence register that can contain the object.
  1412.          */
  1413.         return i915_gem_get_gtt_size(dev, size, tiling_mode);
  1414. }
  1415.  
  1416.  
  1417.  
  1418. int
  1419. i915_gem_mmap_gtt(struct drm_file *file,
  1420.           struct drm_device *dev,
  1421.           uint32_t handle,
  1422.           uint64_t *offset)
  1423. {
  1424.     struct drm_i915_private *dev_priv = dev->dev_private;
  1425.     struct drm_i915_gem_object *obj;
  1426.     unsigned long pfn;
  1427.     char *mem, *ptr;
  1428.     int ret;
  1429.  
  1430.     ret = i915_mutex_lock_interruptible(dev);
  1431.     if (ret)
  1432.         return ret;
  1433.  
  1434.     obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
  1435.     if (&obj->base == NULL) {
  1436.         ret = -ENOENT;
  1437.         goto unlock;
  1438.     }
  1439.  
  1440.     if (obj->base.size > dev_priv->gtt.mappable_end) {
  1441.         ret = -E2BIG;
  1442.         goto out;
  1443.     }
  1444.  
  1445.     if (obj->madv != I915_MADV_WILLNEED) {
  1446.                 DRM_DEBUG("Attempting to mmap a purgeable buffer\n");
  1447.                 ret = -EFAULT;
  1448.         goto out;
  1449.     }
  1450.     /* Now bind it into the GTT if needed */
  1451.         ret = i915_gem_obj_ggtt_pin(obj, 0, PIN_MAPPABLE | PIN_NONBLOCK);
  1452.     if (ret)
  1453.         goto out;
  1454.  
  1455.     ret = i915_gem_object_set_to_gtt_domain(obj, 1);
  1456.     if (ret)
  1457.         goto unpin;
  1458.  
  1459.     ret = i915_gem_object_get_fence(obj);
  1460.     if (ret)
  1461.         goto unpin;
  1462.  
  1463.     obj->fault_mappable = true;
  1464.  
  1465.     pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
  1466.  
  1467.     /* Finally, remap it using the new GTT offset */
  1468.  
  1469.     mem = UserAlloc(obj->base.size);
  1470.     if(unlikely(mem == NULL))
  1471.     {
  1472.         ret = -ENOMEM;
  1473.         goto unpin;
  1474.     }
  1475.  
  1476.     for(ptr = mem; ptr < mem + obj->base.size; ptr+= 4096, pfn+= 4096)
  1477.         MapPage(ptr, pfn, PG_SHARED|PG_UW);
  1478.  
  1479. unpin:
  1480.     i915_gem_object_unpin_pages(obj);
  1481.  
  1482.  
  1483.     *offset = (uint32_t)mem;
  1484.  
  1485. out:
  1486.     drm_gem_object_unreference(&obj->base);
  1487. unlock:
  1488.     mutex_unlock(&dev->struct_mutex);
  1489.     return ret;
  1490. }
  1491.  
  1492. /**
  1493.  * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
  1494.  * @dev: DRM device
  1495.  * @data: GTT mapping ioctl data
  1496.  * @file: GEM object info
  1497.  *
  1498.  * Simply returns the fake offset to userspace so it can mmap it.
  1499.  * The mmap call will end up in drm_gem_mmap(), which will set things
  1500.  * up so we can get faults in the handler above.
  1501.  *
  1502.  * The fault handler will take care of binding the object into the GTT
  1503.  * (since it may have been evicted to make room for something), allocating
  1504.  * a fence register, and mapping the appropriate aperture address into
  1505.  * userspace.
  1506.  */
  1507. int
  1508. i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
  1509.             struct drm_file *file)
  1510. {
  1511.     struct drm_i915_gem_mmap_gtt *args = data;
  1512.  
  1513.     return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
  1514. }
  1515.  
  1516. static inline int
  1517. i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
  1518. {
  1519.         return obj->madv == I915_MADV_DONTNEED;
  1520. }
  1521.  
  1522. /* Immediately discard the backing storage */
  1523. static void
  1524. i915_gem_object_truncate(struct drm_i915_gem_object *obj)
  1525. {
  1526. //      i915_gem_object_free_mmap_offset(obj);
  1527.  
  1528.         if (obj->base.filp == NULL)
  1529.                 return;
  1530.  
  1531.         /* Our goal here is to return as much of the memory as
  1532.          * is possible back to the system as we are called from OOM.
  1533.          * To do this we must instruct the shmfs to drop all of its
  1534.          * backing pages, *now*.
  1535.          */
  1536. //      shmem_truncate_range(file_inode(obj->base.filp), 0, (loff_t)-1);
  1537.         obj->madv = __I915_MADV_PURGED;
  1538. }
  1539.  
  1540. /* Try to discard unwanted pages */
  1541. static void
  1542. i915_gem_object_invalidate(struct drm_i915_gem_object *obj)
  1543. {
  1544.         struct address_space *mapping;
  1545.  
  1546.         switch (obj->madv) {
  1547.         case I915_MADV_DONTNEED:
  1548.                 i915_gem_object_truncate(obj);
  1549.         case __I915_MADV_PURGED:
  1550.                 return;
  1551.         }
  1552.  
  1553.         if (obj->base.filp == NULL)
  1554.                 return;
  1555.  
  1556. }
  1557.  
  1558. static void
  1559. i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
  1560. {
  1561.         struct sg_page_iter sg_iter;
  1562.         int ret;
  1563.  
  1564.         BUG_ON(obj->madv == __I915_MADV_PURGED);
  1565.  
  1566.         ret = i915_gem_object_set_to_cpu_domain(obj, true);
  1567.         if (ret) {
  1568.                 /* In the event of a disaster, abandon all caches and
  1569.                  * hope for the best.
  1570.                  */
  1571.                 WARN_ON(ret != -EIO);
  1572.                 i915_gem_clflush_object(obj, true);
  1573.                 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  1574.         }
  1575.  
  1576.         if (obj->madv == I915_MADV_DONTNEED)
  1577.                 obj->dirty = 0;
  1578.  
  1579.         for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
  1580.                 struct page *page = sg_page_iter_page(&sg_iter);
  1581.  
  1582.         page_cache_release(page);
  1583.         }
  1584.     obj->dirty = 0;
  1585.  
  1586.         sg_free_table(obj->pages);
  1587.         kfree(obj->pages);
  1588. }
  1589.  
  1590. int
  1591. i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
  1592. {
  1593.         const struct drm_i915_gem_object_ops *ops = obj->ops;
  1594.  
  1595.         if (obj->pages == NULL)
  1596.                 return 0;
  1597.  
  1598.         if (obj->pages_pin_count)
  1599.                 return -EBUSY;
  1600.  
  1601.         BUG_ON(i915_gem_obj_bound_any(obj));
  1602.  
  1603.         /* ->put_pages might need to allocate memory for the bit17 swizzle
  1604.          * array, hence protect them from being reaped by removing them from gtt
  1605.          * lists early. */
  1606.         list_del(&obj->global_list);
  1607.  
  1608.         ops->put_pages(obj);
  1609.         obj->pages = NULL;
  1610.  
  1611.         i915_gem_object_invalidate(obj);
  1612.  
  1613.         return 0;
  1614. }
  1615.  
  1616.  
  1617.  
  1618.  
  1619.  
  1620.  
  1621.  
  1622.  
  1623. static int
  1624. i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
  1625. {
  1626.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1627.     int page_count, i;
  1628.     struct sg_table *st;
  1629.         struct scatterlist *sg;
  1630.         struct sg_page_iter sg_iter;
  1631.         struct page *page;
  1632.         unsigned long last_pfn = 0;     /* suppress gcc warning */
  1633.         gfp_t gfp;
  1634.  
  1635.         /* Assert that the object is not currently in any GPU domain. As it
  1636.          * wasn't in the GTT, there shouldn't be any way it could have been in
  1637.          * a GPU cache
  1638.          */
  1639.         BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
  1640.         BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
  1641.  
  1642.         st = kmalloc(sizeof(*st), GFP_KERNEL);
  1643.         if (st == NULL)
  1644.                 return -ENOMEM;
  1645.  
  1646.         page_count = obj->base.size / PAGE_SIZE;
  1647.         if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
  1648.                 kfree(st);
  1649.                 return -ENOMEM;
  1650.         }
  1651.  
  1652.         /* Get the list of pages out of our struct file.  They'll be pinned
  1653.          * at this point until we release them.
  1654.          *
  1655.          * Fail silently without starting the shrinker
  1656.          */
  1657.         sg = st->sgl;
  1658.         st->nents = 0;
  1659.         for (i = 0; i < page_count; i++) {
  1660.         page = shmem_read_mapping_page_gfp(obj->base.filp, i, gfp);
  1661.                 if (IS_ERR(page)) {
  1662.             dbgprintf("%s invalid page %p\n", __FUNCTION__, page);
  1663.                         goto err_pages;
  1664.                 }
  1665. #ifdef CONFIG_SWIOTLB
  1666.                 if (swiotlb_nr_tbl()) {
  1667.                         st->nents++;
  1668.                         sg_set_page(sg, page, PAGE_SIZE, 0);
  1669.                         sg = sg_next(sg);
  1670.                         continue;
  1671.                 }
  1672. #endif
  1673.                 if (!i || page_to_pfn(page) != last_pfn + 1) {
  1674.                         if (i)
  1675.                                 sg = sg_next(sg);
  1676.                         st->nents++;
  1677.                 sg_set_page(sg, page, PAGE_SIZE, 0);
  1678.                 } else {
  1679.                         sg->length += PAGE_SIZE;
  1680.                 }
  1681.                 last_pfn = page_to_pfn(page);
  1682.         }
  1683. #ifdef CONFIG_SWIOTLB
  1684.         if (!swiotlb_nr_tbl())
  1685. #endif
  1686.                 sg_mark_end(sg);
  1687.         obj->pages = st;
  1688.  
  1689.  
  1690.         if (obj->tiling_mode != I915_TILING_NONE &&
  1691.             dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES)
  1692.                 i915_gem_object_pin_pages(obj);
  1693.  
  1694.         return 0;
  1695.  
  1696. err_pages:
  1697.         sg_mark_end(sg);
  1698.         for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
  1699.                 page_cache_release(sg_page_iter_page(&sg_iter));
  1700.         sg_free_table(st);
  1701.         kfree(st);
  1702.  
  1703.         return PTR_ERR(page);
  1704. }
  1705.  
  1706. /* Ensure that the associated pages are gathered from the backing storage
  1707.  * and pinned into our object. i915_gem_object_get_pages() may be called
  1708.  * multiple times before they are released by a single call to
  1709.  * i915_gem_object_put_pages() - once the pages are no longer referenced
  1710.  * either as a result of memory pressure (reaping pages under the shrinker)
  1711.  * or as the object is itself released.
  1712.  */
  1713. int
  1714. i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
  1715. {
  1716.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1717.         const struct drm_i915_gem_object_ops *ops = obj->ops;
  1718.         int ret;
  1719.  
  1720.         if (obj->pages)
  1721.                 return 0;
  1722.  
  1723.         if (obj->madv != I915_MADV_WILLNEED) {
  1724.                 DRM_DEBUG("Attempting to obtain a purgeable object\n");
  1725.                 return -EFAULT;
  1726.         }
  1727.  
  1728.         BUG_ON(obj->pages_pin_count);
  1729.  
  1730.         ret = ops->get_pages(obj);
  1731.         if (ret)
  1732.                 return ret;
  1733.  
  1734.         list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  1735.     return 0;
  1736. }
  1737.  
  1738. static void
  1739. i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
  1740.                                struct intel_engine_cs *ring)
  1741. {
  1742.         u32 seqno = intel_ring_get_seqno(ring);
  1743.  
  1744.         BUG_ON(ring == NULL);
  1745.         if (obj->ring != ring && obj->last_write_seqno) {
  1746.                 /* Keep the seqno relative to the current ring */
  1747.                 obj->last_write_seqno = seqno;
  1748.         }
  1749.         obj->ring = ring;
  1750.  
  1751.         /* Add a reference if we're newly entering the active list. */
  1752.         if (!obj->active) {
  1753.                 drm_gem_object_reference(&obj->base);
  1754.                 obj->active = 1;
  1755.         }
  1756.  
  1757.         list_move_tail(&obj->ring_list, &ring->active_list);
  1758.  
  1759.         obj->last_read_seqno = seqno;
  1760. }
  1761.  
  1762. void i915_vma_move_to_active(struct i915_vma *vma,
  1763.                              struct intel_engine_cs *ring)
  1764. {
  1765.         list_move_tail(&vma->mm_list, &vma->vm->active_list);
  1766.         return i915_gem_object_move_to_active(vma->obj, ring);
  1767. }
  1768.  
  1769. static void
  1770. i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
  1771. {
  1772.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  1773.         struct i915_address_space *vm;
  1774.         struct i915_vma *vma;
  1775.  
  1776.         BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
  1777.         BUG_ON(!obj->active);
  1778.  
  1779.         list_for_each_entry(vm, &dev_priv->vm_list, global_link) {
  1780.                 vma = i915_gem_obj_to_vma(obj, vm);
  1781.                 if (vma && !list_empty(&vma->mm_list))
  1782.                         list_move_tail(&vma->mm_list, &vm->inactive_list);
  1783.         }
  1784.  
  1785.         intel_fb_obj_flush(obj, true);
  1786.  
  1787.         list_del_init(&obj->ring_list);
  1788.         obj->ring = NULL;
  1789.  
  1790.         obj->last_read_seqno = 0;
  1791.         obj->last_write_seqno = 0;
  1792.         obj->base.write_domain = 0;
  1793.  
  1794.         obj->last_fenced_seqno = 0;
  1795.  
  1796.         obj->active = 0;
  1797.         drm_gem_object_unreference(&obj->base);
  1798.  
  1799.         WARN_ON(i915_verify_lists(dev));
  1800. }
  1801.  
  1802. static void
  1803. i915_gem_object_retire(struct drm_i915_gem_object *obj)
  1804. {
  1805.         struct intel_engine_cs *ring = obj->ring;
  1806.  
  1807.         if (ring == NULL)
  1808.                 return;
  1809.  
  1810.         if (i915_seqno_passed(ring->get_seqno(ring, true),
  1811.                               obj->last_read_seqno))
  1812.                 i915_gem_object_move_to_inactive(obj);
  1813. }
  1814.  
  1815. static int
  1816. i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
  1817. {
  1818.         struct drm_i915_private *dev_priv = dev->dev_private;
  1819.         struct intel_engine_cs *ring;
  1820.         int ret, i, j;
  1821.  
  1822.         /* Carefully retire all requests without writing to the rings */
  1823.         for_each_ring(ring, dev_priv, i) {
  1824.                 ret = intel_ring_idle(ring);
  1825.         if (ret)
  1826.                 return ret;
  1827.         }
  1828.         i915_gem_retire_requests(dev);
  1829.  
  1830.         /* Finally reset hw state */
  1831.         for_each_ring(ring, dev_priv, i) {
  1832.                 intel_ring_init_seqno(ring, seqno);
  1833.  
  1834.                 for (j = 0; j < ARRAY_SIZE(ring->semaphore.sync_seqno); j++)
  1835.                         ring->semaphore.sync_seqno[j] = 0;
  1836.         }
  1837.  
  1838.         return 0;
  1839. }
  1840.  
  1841. int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
  1842. {
  1843.         struct drm_i915_private *dev_priv = dev->dev_private;
  1844.         int ret;
  1845.  
  1846.         if (seqno == 0)
  1847.                 return -EINVAL;
  1848.  
  1849.         /* HWS page needs to be set less than what we
  1850.          * will inject to ring
  1851.          */
  1852.         ret = i915_gem_init_seqno(dev, seqno - 1);
  1853.         if (ret)
  1854.                 return ret;
  1855.  
  1856.         /* Carefully set the last_seqno value so that wrap
  1857.          * detection still works
  1858.          */
  1859.         dev_priv->next_seqno = seqno;
  1860.         dev_priv->last_seqno = seqno - 1;
  1861.         if (dev_priv->last_seqno == 0)
  1862.                 dev_priv->last_seqno--;
  1863.  
  1864.         return 0;
  1865. }
  1866.  
  1867. int
  1868. i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
  1869. {
  1870.         struct drm_i915_private *dev_priv = dev->dev_private;
  1871.  
  1872.         /* reserve 0 for non-seqno */
  1873.         if (dev_priv->next_seqno == 0) {
  1874.                 int ret = i915_gem_init_seqno(dev, 0);
  1875.                 if (ret)
  1876.                         return ret;
  1877.  
  1878.                 dev_priv->next_seqno = 1;
  1879.         }
  1880.  
  1881.         *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
  1882.         return 0;
  1883. }
  1884.  
  1885. int __i915_add_request(struct intel_engine_cs *ring,
  1886.                  struct drm_file *file,
  1887.                        struct drm_i915_gem_object *obj,
  1888.                  u32 *out_seqno)
  1889. {
  1890.         struct drm_i915_private *dev_priv = ring->dev->dev_private;
  1891.         struct drm_i915_gem_request *request;
  1892.         struct intel_ringbuffer *ringbuf;
  1893.         u32 request_ring_position, request_start;
  1894.         int ret;
  1895.  
  1896.         request = ring->preallocated_lazy_request;
  1897.         if (WARN_ON(request == NULL))
  1898.                 return -ENOMEM;
  1899.  
  1900.         if (i915.enable_execlists) {
  1901.                 struct intel_context *ctx = request->ctx;
  1902.                 ringbuf = ctx->engine[ring->id].ringbuf;
  1903.         } else
  1904.                 ringbuf = ring->buffer;
  1905.  
  1906.         request_start = intel_ring_get_tail(ringbuf);
  1907.         /*
  1908.          * Emit any outstanding flushes - execbuf can fail to emit the flush
  1909.          * after having emitted the batchbuffer command. Hence we need to fix
  1910.          * things up similar to emitting the lazy request. The difference here
  1911.          * is that the flush _must_ happen before the next request, no matter
  1912.          * what.
  1913.          */
  1914.         if (i915.enable_execlists) {
  1915.                 ret = logical_ring_flush_all_caches(ringbuf);
  1916.                 if (ret)
  1917.                         return ret;
  1918.         } else {
  1919.    ret = intel_ring_flush_all_caches(ring);
  1920.    if (ret)
  1921.        return ret;
  1922.         }
  1923.  
  1924.         /* Record the position of the start of the request so that
  1925.          * should we detect the updated seqno part-way through the
  1926.     * GPU processing the request, we never over-estimate the
  1927.          * position of the head.
  1928.          */
  1929.         request_ring_position = intel_ring_get_tail(ringbuf);
  1930.  
  1931.         if (i915.enable_execlists) {
  1932.                 ret = ring->emit_request(ringbuf);
  1933.                 if (ret)
  1934.                         return ret;
  1935.         } else {
  1936.         ret = ring->add_request(ring);
  1937.         if (ret)
  1938.                 return ret;
  1939.         }
  1940.  
  1941.         request->seqno = intel_ring_get_seqno(ring);
  1942.         request->ring = ring;
  1943.         request->head = request_start;
  1944.         request->tail = request_ring_position;
  1945.  
  1946.         /* Whilst this request exists, batch_obj will be on the
  1947.          * active_list, and so will hold the active reference. Only when this
  1948.          * request is retired will the the batch_obj be moved onto the
  1949.          * inactive_list and lose its active reference. Hence we do not need
  1950.          * to explicitly hold another reference here.
  1951.          */
  1952.         request->batch_obj = obj;
  1953.  
  1954.         if (!i915.enable_execlists) {
  1955.         /* Hold a reference to the current context so that we can inspect
  1956.          * it later in case a hangcheck error event fires.
  1957.          */
  1958.         request->ctx = ring->last_context;
  1959.         if (request->ctx)
  1960.                 i915_gem_context_reference(request->ctx);
  1961.         }
  1962.  
  1963.         request->emitted_jiffies = jiffies;
  1964.         list_add_tail(&request->list, &ring->request_list);
  1965.         request->file_priv = NULL;
  1966.  
  1967.         if (file) {
  1968.                 struct drm_i915_file_private *file_priv = file->driver_priv;
  1969.  
  1970.                 spin_lock(&file_priv->mm.lock);
  1971.                 request->file_priv = file_priv;
  1972.                 list_add_tail(&request->client_list,
  1973.                               &file_priv->mm.request_list);
  1974.                 spin_unlock(&file_priv->mm.lock);
  1975.         }
  1976.  
  1977.         trace_i915_gem_request_add(ring, request->seqno);
  1978.         ring->outstanding_lazy_seqno = 0;
  1979.         ring->preallocated_lazy_request = NULL;
  1980.  
  1981. //              i915_queue_hangcheck(ring->dev);
  1982.  
  1983.            queue_delayed_work(dev_priv->wq,
  1984.                                            &dev_priv->mm.retire_work,
  1985.                                            round_jiffies_up_relative(HZ));
  1986.            intel_mark_busy(dev_priv->dev);
  1987.  
  1988.         if (out_seqno)
  1989.                 *out_seqno = request->seqno;
  1990.         return 0;
  1991. }
  1992.  
  1993. static inline void
  1994. i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
  1995. {
  1996.         struct drm_i915_file_private *file_priv = request->file_priv;
  1997.  
  1998.         if (!file_priv)
  1999.                 return;
  2000.  
  2001.         spin_lock(&file_priv->mm.lock);
  2002.                 list_del(&request->client_list);
  2003.                 request->file_priv = NULL;
  2004.         spin_unlock(&file_priv->mm.lock);
  2005. }
  2006.  
  2007. static bool i915_context_is_banned(struct drm_i915_private *dev_priv,
  2008.                                    const struct intel_context *ctx)
  2009. {
  2010.         unsigned long elapsed;
  2011.  
  2012.     elapsed = GetTimerTicks()/100 - ctx->hang_stats.guilty_ts;
  2013.  
  2014.         if (ctx->hang_stats.banned)
  2015.                 return true;
  2016.  
  2017.         if (elapsed <= DRM_I915_CTX_BAN_PERIOD) {
  2018.                 if (!i915_gem_context_is_default(ctx)) {
  2019.                         DRM_DEBUG("context hanging too fast, banning!\n");
  2020.                         return true;
  2021.                 } else if (i915_stop_ring_allow_ban(dev_priv)) {
  2022.                         if (i915_stop_ring_allow_warn(dev_priv))
  2023.                         DRM_ERROR("gpu hanging too fast, banning!\n");
  2024.                         return true;
  2025.         }
  2026.         }
  2027.  
  2028.         return false;
  2029. }
  2030.  
  2031. static void i915_set_reset_status(struct drm_i915_private *dev_priv,
  2032.                                   struct intel_context *ctx,
  2033.                                   const bool guilty)
  2034. {
  2035.         struct i915_ctx_hang_stats *hs;
  2036.  
  2037.         if (WARN_ON(!ctx))
  2038.                 return;
  2039.  
  2040.         hs = &ctx->hang_stats;
  2041.  
  2042.         if (guilty) {
  2043.                 hs->banned = i915_context_is_banned(dev_priv, ctx);
  2044.                 hs->batch_active++;
  2045.         hs->guilty_ts = GetTimerTicks()/100;
  2046.         } else {
  2047.                 hs->batch_pending++;
  2048.         }
  2049. }
  2050.  
  2051. static void i915_gem_free_request(struct drm_i915_gem_request *request)
  2052. {
  2053.         struct intel_context *ctx = request->ctx;
  2054.  
  2055.         list_del(&request->list);
  2056.         i915_gem_request_remove_from_client(request);
  2057.  
  2058.         if (ctx) {
  2059.                 if (i915.enable_execlists) {
  2060.                         struct intel_engine_cs *ring = request->ring;
  2061.  
  2062.                         if (ctx != ring->default_context)
  2063.                                 intel_lr_context_unpin(ring, ctx);
  2064.                 }
  2065.                 i915_gem_context_unreference(ctx);
  2066.         }
  2067.         kfree(request);
  2068. }
  2069.  
  2070. struct drm_i915_gem_request *
  2071. i915_gem_find_active_request(struct intel_engine_cs *ring)
  2072. {
  2073.         struct drm_i915_gem_request *request;
  2074.         u32 completed_seqno;
  2075.  
  2076.         completed_seqno = ring->get_seqno(ring, false);
  2077.  
  2078.         list_for_each_entry(request, &ring->request_list, list) {
  2079.                 if (i915_seqno_passed(completed_seqno, request->seqno))
  2080.                         continue;
  2081.  
  2082.                 return request;
  2083.         }
  2084.  
  2085.         return NULL;
  2086. }
  2087.  
  2088. static void i915_gem_reset_ring_status(struct drm_i915_private *dev_priv,
  2089.                                        struct intel_engine_cs *ring)
  2090. {
  2091.         struct drm_i915_gem_request *request;
  2092.         bool ring_hung;
  2093.  
  2094.         request = i915_gem_find_active_request(ring);
  2095.  
  2096.         if (request == NULL)
  2097.                 return;
  2098.  
  2099.         ring_hung = ring->hangcheck.score >= HANGCHECK_SCORE_RING_HUNG;
  2100.  
  2101.         i915_set_reset_status(dev_priv, request->ctx, ring_hung);
  2102.  
  2103.         list_for_each_entry_continue(request, &ring->request_list, list)
  2104.                 i915_set_reset_status(dev_priv, request->ctx, false);
  2105. }
  2106.  
  2107. static void i915_gem_reset_ring_cleanup(struct drm_i915_private *dev_priv,
  2108.                                         struct intel_engine_cs *ring)
  2109. {
  2110.         while (!list_empty(&ring->active_list)) {
  2111.                 struct drm_i915_gem_object *obj;
  2112.  
  2113.                 obj = list_first_entry(&ring->active_list,
  2114.                                        struct drm_i915_gem_object,
  2115.                                        ring_list);
  2116.  
  2117.                 i915_gem_object_move_to_inactive(obj);
  2118.         }
  2119.  
  2120.         /*
  2121.          * Clear the execlists queue up before freeing the requests, as those
  2122.          * are the ones that keep the context and ringbuffer backing objects
  2123.          * pinned in place.
  2124.          */
  2125.         while (!list_empty(&ring->execlist_queue)) {
  2126.                 struct intel_ctx_submit_request *submit_req;
  2127.  
  2128.                 submit_req = list_first_entry(&ring->execlist_queue,
  2129.                                 struct intel_ctx_submit_request,
  2130.                                 execlist_link);
  2131.                 list_del(&submit_req->execlist_link);
  2132.                 intel_runtime_pm_put(dev_priv);
  2133.                 i915_gem_context_unreference(submit_req->ctx);
  2134.                 kfree(submit_req);
  2135.         }
  2136.  
  2137.         /*
  2138.          * We must free the requests after all the corresponding objects have
  2139.          * been moved off active lists. Which is the same order as the normal
  2140.          * retire_requests function does. This is important if object hold
  2141.          * implicit references on things like e.g. ppgtt address spaces through
  2142.          * the request.
  2143.          */
  2144.         while (!list_empty(&ring->request_list)) {
  2145.                 struct drm_i915_gem_request *request;
  2146.  
  2147.                 request = list_first_entry(&ring->request_list,
  2148.                                            struct drm_i915_gem_request,
  2149.                                            list);
  2150.  
  2151.                 i915_gem_free_request(request);
  2152.         }
  2153.  
  2154.         /* These may not have been flush before the reset, do so now */
  2155.         kfree(ring->preallocated_lazy_request);
  2156.         ring->preallocated_lazy_request = NULL;
  2157.         ring->outstanding_lazy_seqno = 0;
  2158. }
  2159.  
  2160. void i915_gem_restore_fences(struct drm_device *dev)
  2161. {
  2162.         struct drm_i915_private *dev_priv = dev->dev_private;
  2163.         int i;
  2164.  
  2165.         for (i = 0; i < dev_priv->num_fence_regs; i++) {
  2166.                 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
  2167.  
  2168.                 /*
  2169.                  * Commit delayed tiling changes if we have an object still
  2170.                  * attached to the fence, otherwise just clear the fence.
  2171.                  */
  2172.                 if (reg->obj) {
  2173.                         i915_gem_object_update_fence(reg->obj, reg,
  2174.                                                      reg->obj->tiling_mode);
  2175.                 } else {
  2176.                         i915_gem_write_fence(dev, i, NULL);
  2177.                 }
  2178.         }
  2179. }
  2180.  
  2181. void i915_gem_reset(struct drm_device *dev)
  2182. {
  2183.         struct drm_i915_private *dev_priv = dev->dev_private;
  2184.         struct intel_engine_cs *ring;
  2185.         int i;
  2186.  
  2187.         /*
  2188.          * Before we free the objects from the requests, we need to inspect
  2189.          * them for finding the guilty party. As the requests only borrow
  2190.          * their reference to the objects, the inspection must be done first.
  2191.          */
  2192.         for_each_ring(ring, dev_priv, i)
  2193.                 i915_gem_reset_ring_status(dev_priv, ring);
  2194.  
  2195.         for_each_ring(ring, dev_priv, i)
  2196.                 i915_gem_reset_ring_cleanup(dev_priv, ring);
  2197.  
  2198.         i915_gem_context_reset(dev);
  2199.  
  2200.         i915_gem_restore_fences(dev);
  2201. }
  2202.  
  2203. /**
  2204.  * This function clears the request list as sequence numbers are passed.
  2205.  */
  2206. void
  2207. i915_gem_retire_requests_ring(struct intel_engine_cs *ring)
  2208. {
  2209.         uint32_t seqno;
  2210.  
  2211.         if (list_empty(&ring->request_list))
  2212.                 return;
  2213.  
  2214.         WARN_ON(i915_verify_lists(ring->dev));
  2215.  
  2216.         seqno = ring->get_seqno(ring, true);
  2217.  
  2218.         /* Move any buffers on the active list that are no longer referenced
  2219.          * by the ringbuffer to the flushing/inactive lists as appropriate,
  2220.          * before we free the context associated with the requests.
  2221.          */
  2222.         while (!list_empty(&ring->active_list)) {
  2223.                 struct drm_i915_gem_object *obj;
  2224.  
  2225.                 obj = list_first_entry(&ring->active_list,
  2226.                                       struct drm_i915_gem_object,
  2227.                                       ring_list);
  2228.  
  2229.                 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
  2230.                         break;
  2231.  
  2232.                 i915_gem_object_move_to_inactive(obj);
  2233.         }
  2234.  
  2235.  
  2236.         while (!list_empty(&ring->request_list)) {
  2237.                 struct drm_i915_gem_request *request;
  2238.                 struct intel_ringbuffer *ringbuf;
  2239.  
  2240.                 request = list_first_entry(&ring->request_list,
  2241.                                            struct drm_i915_gem_request,
  2242.                                            list);
  2243.  
  2244.                 if (!i915_seqno_passed(seqno, request->seqno))
  2245.                         break;
  2246.  
  2247.                 trace_i915_gem_request_retire(ring, request->seqno);
  2248.  
  2249.                 /* This is one of the few common intersection points
  2250.                  * between legacy ringbuffer submission and execlists:
  2251.                  * we need to tell them apart in order to find the correct
  2252.                  * ringbuffer to which the request belongs to.
  2253.                  */
  2254.                 if (i915.enable_execlists) {
  2255.                         struct intel_context *ctx = request->ctx;
  2256.                         ringbuf = ctx->engine[ring->id].ringbuf;
  2257.                 } else
  2258.                         ringbuf = ring->buffer;
  2259.  
  2260.                 /* We know the GPU must have read the request to have
  2261.                  * sent us the seqno + interrupt, so use the position
  2262.                  * of tail of the request to update the last known position
  2263.                  * of the GPU head.
  2264.                  */
  2265.                 ringbuf->last_retired_head = request->tail;
  2266.  
  2267.                 i915_gem_free_request(request);
  2268.         }
  2269.  
  2270.         if (unlikely(ring->trace_irq_seqno &&
  2271.                      i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
  2272.                 ring->irq_put(ring);
  2273.                 ring->trace_irq_seqno = 0;
  2274.         }
  2275.  
  2276.         WARN_ON(i915_verify_lists(ring->dev));
  2277. }
  2278.  
  2279. bool
  2280. i915_gem_retire_requests(struct drm_device *dev)
  2281. {
  2282.         struct drm_i915_private *dev_priv = dev->dev_private;
  2283.         struct intel_engine_cs *ring;
  2284.         bool idle = true;
  2285.         int i;
  2286.  
  2287.         for_each_ring(ring, dev_priv, i) {
  2288.                 i915_gem_retire_requests_ring(ring);
  2289.                 idle &= list_empty(&ring->request_list);
  2290.                 if (i915.enable_execlists) {
  2291.                         unsigned long flags;
  2292.  
  2293.                         spin_lock_irqsave(&ring->execlist_lock, flags);
  2294.                         idle &= list_empty(&ring->execlist_queue);
  2295.                         spin_unlock_irqrestore(&ring->execlist_lock, flags);
  2296.  
  2297.                         intel_execlists_retire_requests(ring);
  2298.                 }
  2299.         }
  2300.  
  2301.         if (idle)
  2302.                 mod_delayed_work(dev_priv->wq,
  2303.                                    &dev_priv->mm.idle_work,
  2304.                                    msecs_to_jiffies(100));
  2305.  
  2306.         return idle;
  2307. }
  2308.  
  2309. static void
  2310. i915_gem_retire_work_handler(struct work_struct *work)
  2311. {
  2312.         struct drm_i915_private *dev_priv =
  2313.                 container_of(work, typeof(*dev_priv), mm.retire_work.work);
  2314.         struct drm_device *dev = dev_priv->dev;
  2315.         bool idle;
  2316.  
  2317.         /* Come back later if the device is busy... */
  2318.         idle = false;
  2319.         if (mutex_trylock(&dev->struct_mutex)) {
  2320.                 idle = i915_gem_retire_requests(dev);
  2321.                 mutex_unlock(&dev->struct_mutex);
  2322.         }
  2323.         if (!idle)
  2324.                 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
  2325.                                    round_jiffies_up_relative(HZ));
  2326. }
  2327.  
  2328. static void
  2329. i915_gem_idle_work_handler(struct work_struct *work)
  2330. {
  2331.         struct drm_i915_private *dev_priv =
  2332.                 container_of(work, typeof(*dev_priv), mm.idle_work.work);
  2333.  
  2334.         intel_mark_idle(dev_priv->dev);
  2335. }
  2336.  
  2337. /**
  2338.  * Ensures that an object will eventually get non-busy by flushing any required
  2339.  * write domains, emitting any outstanding lazy request and retiring and
  2340.  * completed requests.
  2341.  */
  2342. static int
  2343. i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
  2344. {
  2345.         int ret;
  2346.  
  2347.         if (obj->active) {
  2348.                 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
  2349.                 if (ret)
  2350.                         return ret;
  2351.  
  2352.                 i915_gem_retire_requests_ring(obj->ring);
  2353.         }
  2354.  
  2355.         return 0;
  2356. }
  2357.  
  2358. /**
  2359.  * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
  2360.  * @DRM_IOCTL_ARGS: standard ioctl arguments
  2361.  *
  2362.  * Returns 0 if successful, else an error is returned with the remaining time in
  2363.  * the timeout parameter.
  2364.  *  -ETIME: object is still busy after timeout
  2365.  *  -ERESTARTSYS: signal interrupted the wait
  2366.  *  -ENONENT: object doesn't exist
  2367.  * Also possible, but rare:
  2368.  *  -EAGAIN: GPU wedged
  2369.  *  -ENOMEM: damn
  2370.  *  -ENODEV: Internal IRQ fail
  2371.  *  -E?: The add request failed
  2372.  *
  2373.  * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
  2374.  * non-zero timeout parameter the wait ioctl will wait for the given number of
  2375.  * nanoseconds on an object becoming unbusy. Since the wait itself does so
  2376.  * without holding struct_mutex the object may become re-busied before this
  2377.  * function completes. A similar but shorter * race condition exists in the busy
  2378.  * ioctl
  2379.  */
  2380. int
  2381. i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  2382. {
  2383.         struct drm_i915_private *dev_priv = dev->dev_private;
  2384.         struct drm_i915_gem_wait *args = data;
  2385.         struct drm_i915_gem_object *obj;
  2386.         struct intel_engine_cs *ring = NULL;
  2387.         unsigned reset_counter;
  2388.         u32 seqno = 0;
  2389.         int ret = 0;
  2390.  
  2391.         if (args->flags != 0)
  2392.                 return -EINVAL;
  2393.  
  2394.         ret = i915_mutex_lock_interruptible(dev);
  2395.         if (ret)
  2396.                 return ret;
  2397.  
  2398.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
  2399.         if (&obj->base == NULL) {
  2400.                 mutex_unlock(&dev->struct_mutex);
  2401.                 return -ENOENT;
  2402.         }
  2403.  
  2404.         /* Need to make sure the object gets inactive eventually. */
  2405.         ret = i915_gem_object_flush_active(obj);
  2406.         if (ret)
  2407.                 goto out;
  2408.  
  2409.         if (obj->active) {
  2410.                 seqno = obj->last_read_seqno;
  2411.                 ring = obj->ring;
  2412.         }
  2413.  
  2414.         if (seqno == 0)
  2415.                  goto out;
  2416.  
  2417.         /* Do this after OLR check to make sure we make forward progress polling
  2418.          * on this IOCTL with a timeout <=0 (like busy ioctl)
  2419.          */
  2420.         if (args->timeout_ns <= 0) {
  2421.                 ret = -ETIME;
  2422.                 goto out;
  2423.         }
  2424.  
  2425.         drm_gem_object_unreference(&obj->base);
  2426.         reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  2427.         mutex_unlock(&dev->struct_mutex);
  2428.  
  2429.         return __i915_wait_seqno(ring, seqno, reset_counter, true,
  2430.                                  &args->timeout_ns, file->driver_priv);
  2431.  
  2432. out:
  2433.         drm_gem_object_unreference(&obj->base);
  2434.         mutex_unlock(&dev->struct_mutex);
  2435.         return ret;
  2436. }
  2437.  
  2438. /**
  2439.  * i915_gem_object_sync - sync an object to a ring.
  2440.  *
  2441.  * @obj: object which may be in use on another ring.
  2442.  * @to: ring we wish to use the object on. May be NULL.
  2443.  *
  2444.  * This code is meant to abstract object synchronization with the GPU.
  2445.  * Calling with NULL implies synchronizing the object with the CPU
  2446.  * rather than a particular GPU ring.
  2447.  *
  2448.  * Returns 0 if successful, else propagates up the lower layer error.
  2449.  */
  2450. int
  2451. i915_gem_object_sync(struct drm_i915_gem_object *obj,
  2452.                      struct intel_engine_cs *to)
  2453. {
  2454.         struct intel_engine_cs *from = obj->ring;
  2455.         u32 seqno;
  2456.         int ret, idx;
  2457.  
  2458.         if (from == NULL || to == from)
  2459.                 return 0;
  2460.  
  2461.         if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
  2462.                 return i915_gem_object_wait_rendering(obj, false);
  2463.  
  2464.         idx = intel_ring_sync_index(from, to);
  2465.  
  2466.         seqno = obj->last_read_seqno;
  2467.         /* Optimization: Avoid semaphore sync when we are sure we already
  2468.          * waited for an object with higher seqno */
  2469.         if (seqno <= from->semaphore.sync_seqno[idx])
  2470.                 return 0;
  2471.  
  2472.         ret = i915_gem_check_olr(obj->ring, seqno);
  2473.         if (ret)
  2474.                 return ret;
  2475.  
  2476.         trace_i915_gem_ring_sync_to(from, to, seqno);
  2477.         ret = to->semaphore.sync_to(to, from, seqno);
  2478.         if (!ret)
  2479.                 /* We use last_read_seqno because sync_to()
  2480.                  * might have just caused seqno wrap under
  2481.                  * the radar.
  2482.                  */
  2483.                 from->semaphore.sync_seqno[idx] = obj->last_read_seqno;
  2484.  
  2485.         return ret;
  2486. }
  2487.  
  2488. static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
  2489. {
  2490.         u32 old_write_domain, old_read_domains;
  2491.  
  2492.         /* Force a pagefault for domain tracking on next user access */
  2493. //      i915_gem_release_mmap(obj);
  2494.  
  2495.         if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  2496.                 return;
  2497.  
  2498.         /* Wait for any direct GTT access to complete */
  2499.         mb();
  2500.  
  2501.         old_read_domains = obj->base.read_domains;
  2502.         old_write_domain = obj->base.write_domain;
  2503.  
  2504.         obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
  2505.         obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
  2506.  
  2507.         trace_i915_gem_object_change_domain(obj,
  2508.                                             old_read_domains,
  2509.                                             old_write_domain);
  2510. }
  2511.  
  2512. int i915_vma_unbind(struct i915_vma *vma)
  2513. {
  2514.         struct drm_i915_gem_object *obj = vma->obj;
  2515.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2516.         int ret;
  2517.     if(obj == get_fb_obj())
  2518.     {
  2519.         WARN(1,"attempt to unbind fb object\n");
  2520.         return 0;
  2521.     };
  2522.  
  2523.         if (list_empty(&vma->vma_link))
  2524.                 return 0;
  2525.  
  2526.         if (!drm_mm_node_allocated(&vma->node)) {
  2527.                 i915_gem_vma_destroy(vma);
  2528.                 return 0;
  2529.         }
  2530.  
  2531.         if (vma->pin_count)
  2532.                 return -EBUSY;
  2533.  
  2534.         BUG_ON(obj->pages == NULL);
  2535.  
  2536.         ret = i915_gem_object_finish_gpu(obj);
  2537.         if (ret)
  2538.                 return ret;
  2539.         /* Continue on if we fail due to EIO, the GPU is hung so we
  2540.          * should be safe and we need to cleanup or else we might
  2541.          * cause memory corruption through use-after-free.
  2542.          */
  2543.  
  2544.         /* Throw away the active reference before moving to the unbound list */
  2545.         i915_gem_object_retire(obj);
  2546.  
  2547.         if (i915_is_ggtt(vma->vm)) {
  2548.         i915_gem_object_finish_gtt(obj);
  2549.  
  2550.         /* release the fence reg _after_ flushing */
  2551.         ret = i915_gem_object_put_fence(obj);
  2552.         if (ret)
  2553.                 return ret;
  2554.         }
  2555.  
  2556.         trace_i915_vma_unbind(vma);
  2557.  
  2558.         vma->unbind_vma(vma);
  2559.  
  2560.         list_del_init(&vma->mm_list);
  2561.         if (i915_is_ggtt(vma->vm))
  2562.                 obj->map_and_fenceable = false;
  2563.  
  2564.         drm_mm_remove_node(&vma->node);
  2565.         i915_gem_vma_destroy(vma);
  2566.  
  2567.         /* Since the unbound list is global, only move to that list if
  2568.          * no more VMAs exist. */
  2569.         if (list_empty(&obj->vma_list)) {
  2570.                 i915_gem_gtt_finish_object(obj);
  2571.                 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
  2572.         }
  2573.  
  2574.         /* And finally now the object is completely decoupled from this vma,
  2575.          * we can drop its hold on the backing storage and allow it to be
  2576.          * reaped by the shrinker.
  2577.          */
  2578.         i915_gem_object_unpin_pages(obj);
  2579.  
  2580.         return 0;
  2581. }
  2582.  
  2583. int i915_gpu_idle(struct drm_device *dev)
  2584. {
  2585.         struct drm_i915_private *dev_priv = dev->dev_private;
  2586.         struct intel_engine_cs *ring;
  2587.         int ret, i;
  2588.  
  2589.         /* Flush everything onto the inactive list. */
  2590.         for_each_ring(ring, dev_priv, i) {
  2591.                 if (!i915.enable_execlists) {
  2592.                 ret = i915_switch_context(ring, ring->default_context);
  2593.                 if (ret)
  2594.                         return ret;
  2595.                 }
  2596.  
  2597.                 ret = intel_ring_idle(ring);
  2598.                 if (ret)
  2599.                         return ret;
  2600.         }
  2601.  
  2602.         return 0;
  2603. }
  2604.  
  2605. static void i965_write_fence_reg(struct drm_device *dev, int reg,
  2606.                                         struct drm_i915_gem_object *obj)
  2607. {
  2608.         struct drm_i915_private *dev_priv = dev->dev_private;
  2609.         int fence_reg;
  2610.         int fence_pitch_shift;
  2611.  
  2612.         if (INTEL_INFO(dev)->gen >= 6) {
  2613.                 fence_reg = FENCE_REG_SANDYBRIDGE_0;
  2614.                 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
  2615.         } else {
  2616.                 fence_reg = FENCE_REG_965_0;
  2617.                 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
  2618.         }
  2619.  
  2620.         fence_reg += reg * 8;
  2621.  
  2622.         /* To w/a incoherency with non-atomic 64-bit register updates,
  2623.          * we split the 64-bit update into two 32-bit writes. In order
  2624.          * for a partial fence not to be evaluated between writes, we
  2625.          * precede the update with write to turn off the fence register,
  2626.          * and only enable the fence as the last step.
  2627.          *
  2628.          * For extra levels of paranoia, we make sure each step lands
  2629.          * before applying the next step.
  2630.          */
  2631.         I915_WRITE(fence_reg, 0);
  2632.         POSTING_READ(fence_reg);
  2633.  
  2634.         if (obj) {
  2635.                 u32 size = i915_gem_obj_ggtt_size(obj);
  2636.                 uint64_t val;
  2637.  
  2638.                 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
  2639.                                  0xfffff000) << 32;
  2640.                 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
  2641.                 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
  2642.                 if (obj->tiling_mode == I915_TILING_Y)
  2643.                         val |= 1 << I965_FENCE_TILING_Y_SHIFT;
  2644.                 val |= I965_FENCE_REG_VALID;
  2645.  
  2646.                 I915_WRITE(fence_reg + 4, val >> 32);
  2647.                 POSTING_READ(fence_reg + 4);
  2648.  
  2649.                 I915_WRITE(fence_reg + 0, val);
  2650.                 POSTING_READ(fence_reg);
  2651.         } else {
  2652.                 I915_WRITE(fence_reg + 4, 0);
  2653.                 POSTING_READ(fence_reg + 4);
  2654.         }
  2655. }
  2656.  
  2657. static void i915_write_fence_reg(struct drm_device *dev, int reg,
  2658.                                  struct drm_i915_gem_object *obj)
  2659. {
  2660.         struct drm_i915_private *dev_priv = dev->dev_private;
  2661.         u32 val;
  2662.  
  2663.         if (obj) {
  2664.                 u32 size = i915_gem_obj_ggtt_size(obj);
  2665.                 int pitch_val;
  2666.                 int tile_width;
  2667.  
  2668.                 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
  2669.                      (size & -size) != size ||
  2670.                      (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2671.                      "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
  2672.                      i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
  2673.  
  2674.                 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
  2675.                         tile_width = 128;
  2676.                 else
  2677.                         tile_width = 512;
  2678.  
  2679.                 /* Note: pitch better be a power of two tile widths */
  2680.                 pitch_val = obj->stride / tile_width;
  2681.                 pitch_val = ffs(pitch_val) - 1;
  2682.  
  2683.                 val = i915_gem_obj_ggtt_offset(obj);
  2684.                 if (obj->tiling_mode == I915_TILING_Y)
  2685.                         val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2686.                 val |= I915_FENCE_SIZE_BITS(size);
  2687.                 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2688.                 val |= I830_FENCE_REG_VALID;
  2689.         } else
  2690.                 val = 0;
  2691.  
  2692.         if (reg < 8)
  2693.                 reg = FENCE_REG_830_0 + reg * 4;
  2694.         else
  2695.                 reg = FENCE_REG_945_8 + (reg - 8) * 4;
  2696.  
  2697.         I915_WRITE(reg, val);
  2698.         POSTING_READ(reg);
  2699. }
  2700.  
  2701. static void i830_write_fence_reg(struct drm_device *dev, int reg,
  2702.                                 struct drm_i915_gem_object *obj)
  2703. {
  2704.         struct drm_i915_private *dev_priv = dev->dev_private;
  2705.         uint32_t val;
  2706.  
  2707.         if (obj) {
  2708.                 u32 size = i915_gem_obj_ggtt_size(obj);
  2709.                 uint32_t pitch_val;
  2710.  
  2711.                 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
  2712.                      (size & -size) != size ||
  2713.                      (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
  2714.                      "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
  2715.                      i915_gem_obj_ggtt_offset(obj), size);
  2716.  
  2717.                 pitch_val = obj->stride / 128;
  2718.                 pitch_val = ffs(pitch_val) - 1;
  2719.  
  2720.                 val = i915_gem_obj_ggtt_offset(obj);
  2721.                 if (obj->tiling_mode == I915_TILING_Y)
  2722.                         val |= 1 << I830_FENCE_TILING_Y_SHIFT;
  2723.                 val |= I830_FENCE_SIZE_BITS(size);
  2724.                 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
  2725.                 val |= I830_FENCE_REG_VALID;
  2726.         } else
  2727.                 val = 0;
  2728.  
  2729.         I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
  2730.         POSTING_READ(FENCE_REG_830_0 + reg * 4);
  2731. }
  2732.  
  2733. inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
  2734. {
  2735.         return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
  2736. }
  2737.  
  2738. static void i915_gem_write_fence(struct drm_device *dev, int reg,
  2739.                                  struct drm_i915_gem_object *obj)
  2740. {
  2741.         struct drm_i915_private *dev_priv = dev->dev_private;
  2742.  
  2743.         /* Ensure that all CPU reads are completed before installing a fence
  2744.          * and all writes before removing the fence.
  2745.          */
  2746.         if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
  2747.                 mb();
  2748.  
  2749.         WARN(obj && (!obj->stride || !obj->tiling_mode),
  2750.              "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
  2751.              obj->stride, obj->tiling_mode);
  2752.  
  2753.         switch (INTEL_INFO(dev)->gen) {
  2754.         case 9:
  2755.         case 8:
  2756.         case 7:
  2757.         case 6:
  2758.         case 5:
  2759.         case 4: i965_write_fence_reg(dev, reg, obj); break;
  2760.         case 3: i915_write_fence_reg(dev, reg, obj); break;
  2761.         case 2: i830_write_fence_reg(dev, reg, obj); break;
  2762.         default: BUG();
  2763.         }
  2764.  
  2765.         /* And similarly be paranoid that no direct access to this region
  2766.          * is reordered to before the fence is installed.
  2767.          */
  2768.         if (i915_gem_object_needs_mb(obj))
  2769.                 mb();
  2770. }
  2771.  
  2772. static inline int fence_number(struct drm_i915_private *dev_priv,
  2773.                                struct drm_i915_fence_reg *fence)
  2774. {
  2775.         return fence - dev_priv->fence_regs;
  2776. }
  2777.  
  2778. static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
  2779.                                          struct drm_i915_fence_reg *fence,
  2780.                                          bool enable)
  2781. {
  2782.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2783.         int reg = fence_number(dev_priv, fence);
  2784.  
  2785.         i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
  2786.  
  2787.         if (enable) {
  2788.                 obj->fence_reg = reg;
  2789.                 fence->obj = obj;
  2790.                 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
  2791.         } else {
  2792.                 obj->fence_reg = I915_FENCE_REG_NONE;
  2793.                 fence->obj = NULL;
  2794.                 list_del_init(&fence->lru_list);
  2795.         }
  2796.         obj->fence_dirty = false;
  2797. }
  2798.  
  2799. static int
  2800. i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
  2801. {
  2802.         if (obj->last_fenced_seqno) {
  2803.                 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
  2804.                         if (ret)
  2805.                                 return ret;
  2806.  
  2807.                 obj->last_fenced_seqno = 0;
  2808.         }
  2809.  
  2810.         return 0;
  2811. }
  2812.  
  2813. int
  2814. i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
  2815. {
  2816.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  2817.         struct drm_i915_fence_reg *fence;
  2818.         int ret;
  2819.  
  2820.         ret = i915_gem_object_wait_fence(obj);
  2821.     if (ret)
  2822.        return ret;
  2823.  
  2824.         if (obj->fence_reg == I915_FENCE_REG_NONE)
  2825.                 return 0;
  2826.  
  2827.         fence = &dev_priv->fence_regs[obj->fence_reg];
  2828.  
  2829.         if (WARN_ON(fence->pin_count))
  2830.                 return -EBUSY;
  2831.  
  2832.         i915_gem_object_fence_lost(obj);
  2833.         i915_gem_object_update_fence(obj, fence, false);
  2834.  
  2835.         return 0;
  2836. }
  2837.  
  2838. static struct drm_i915_fence_reg *
  2839. i915_find_fence_reg(struct drm_device *dev)
  2840. {
  2841.         struct drm_i915_private *dev_priv = dev->dev_private;
  2842.         struct drm_i915_fence_reg *reg, *avail;
  2843.         int i;
  2844.  
  2845.         /* First try to find a free reg */
  2846.         avail = NULL;
  2847.         for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
  2848.                 reg = &dev_priv->fence_regs[i];
  2849.                 if (!reg->obj)
  2850.                         return reg;
  2851.  
  2852.                 if (!reg->pin_count)
  2853.                         avail = reg;
  2854.         }
  2855.  
  2856.         if (avail == NULL)
  2857.                 goto deadlock;
  2858.  
  2859.         /* None available, try to steal one or wait for a user to finish */
  2860.         list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
  2861.                 if (reg->pin_count)
  2862.                         continue;
  2863.  
  2864.                 return reg;
  2865.         }
  2866.  
  2867. deadlock:
  2868.         /* Wait for completion of pending flips which consume fences */
  2869. //   if (intel_has_pending_fb_unpin(dev))
  2870. //       return ERR_PTR(-EAGAIN);
  2871.  
  2872.         return ERR_PTR(-EDEADLK);
  2873. }
  2874.  
  2875. /**
  2876.  * i915_gem_object_get_fence - set up fencing for an object
  2877.  * @obj: object to map through a fence reg
  2878.  *
  2879.  * When mapping objects through the GTT, userspace wants to be able to write
  2880.  * to them without having to worry about swizzling if the object is tiled.
  2881.  * This function walks the fence regs looking for a free one for @obj,
  2882.  * stealing one if it can't find any.
  2883.  *
  2884.  * It then sets up the reg based on the object's properties: address, pitch
  2885.  * and tiling format.
  2886.  *
  2887.  * For an untiled surface, this removes any existing fence.
  2888.  */
  2889. int
  2890. i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
  2891. {
  2892.         struct drm_device *dev = obj->base.dev;
  2893.         struct drm_i915_private *dev_priv = dev->dev_private;
  2894.         bool enable = obj->tiling_mode != I915_TILING_NONE;
  2895.         struct drm_i915_fence_reg *reg;
  2896.         int ret;
  2897.  
  2898.         /* Have we updated the tiling parameters upon the object and so
  2899.          * will need to serialise the write to the associated fence register?
  2900.          */
  2901.         if (obj->fence_dirty) {
  2902.                 ret = i915_gem_object_wait_fence(obj);
  2903.                 if (ret)
  2904.                         return ret;
  2905.         }
  2906.  
  2907.         /* Just update our place in the LRU if our fence is getting reused. */
  2908.         if (obj->fence_reg != I915_FENCE_REG_NONE) {
  2909.                 reg = &dev_priv->fence_regs[obj->fence_reg];
  2910.                 if (!obj->fence_dirty) {
  2911.                         list_move_tail(&reg->lru_list,
  2912.                                        &dev_priv->mm.fence_list);
  2913.                         return 0;
  2914.                 }
  2915.         } else if (enable) {
  2916.                 if (WARN_ON(!obj->map_and_fenceable))
  2917.                         return -EINVAL;
  2918.  
  2919.                 reg = i915_find_fence_reg(dev);
  2920.                 if (IS_ERR(reg))
  2921.                         return PTR_ERR(reg);
  2922.  
  2923.                 if (reg->obj) {
  2924.                         struct drm_i915_gem_object *old = reg->obj;
  2925.  
  2926.                         ret = i915_gem_object_wait_fence(old);
  2927.                         if (ret)
  2928.                                 return ret;
  2929.  
  2930.                         i915_gem_object_fence_lost(old);
  2931.                 }
  2932.         } else
  2933.                 return 0;
  2934.  
  2935.         i915_gem_object_update_fence(obj, reg, enable);
  2936.  
  2937.         return 0;
  2938. }
  2939.  
  2940. static bool i915_gem_valid_gtt_space(struct i915_vma *vma,
  2941.                                      unsigned long cache_level)
  2942. {
  2943.         struct drm_mm_node *gtt_space = &vma->node;
  2944.         struct drm_mm_node *other;
  2945.  
  2946.         /*
  2947.          * On some machines we have to be careful when putting differing types
  2948.          * of snoopable memory together to avoid the prefetcher crossing memory
  2949.          * domains and dying. During vm initialisation, we decide whether or not
  2950.          * these constraints apply and set the drm_mm.color_adjust
  2951.          * appropriately.
  2952.          */
  2953.         if (vma->vm->mm.color_adjust == NULL)
  2954.                 return true;
  2955.  
  2956.         if (!drm_mm_node_allocated(gtt_space))
  2957.                 return true;
  2958.  
  2959.         if (list_empty(&gtt_space->node_list))
  2960.                 return true;
  2961.  
  2962.         other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
  2963.         if (other->allocated && !other->hole_follows && other->color != cache_level)
  2964.                 return false;
  2965.  
  2966.         other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
  2967.         if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
  2968.                 return false;
  2969.  
  2970.         return true;
  2971. }
  2972.  
  2973. /**
  2974.  * Finds free space in the GTT aperture and binds the object there.
  2975.  */
  2976. static struct i915_vma *
  2977. i915_gem_object_bind_to_vm(struct drm_i915_gem_object *obj,
  2978.                            struct i915_address_space *vm,
  2979.                             unsigned alignment,
  2980.                            uint64_t flags)
  2981. {
  2982.         struct drm_device *dev = obj->base.dev;
  2983.         struct drm_i915_private *dev_priv = dev->dev_private;
  2984.         u32 size, fence_size, fence_alignment, unfenced_alignment;
  2985.         unsigned long start =
  2986.                 flags & PIN_OFFSET_BIAS ? flags & PIN_OFFSET_MASK : 0;
  2987.         unsigned long end =
  2988.                 flags & PIN_MAPPABLE ? dev_priv->gtt.mappable_end : vm->total;
  2989.         struct i915_vma *vma;
  2990.         int ret;
  2991.  
  2992.         fence_size = i915_gem_get_gtt_size(dev,
  2993.                                            obj->base.size,
  2994.                                            obj->tiling_mode);
  2995.         fence_alignment = i915_gem_get_gtt_alignment(dev,
  2996.                                                      obj->base.size,
  2997.                                                      obj->tiling_mode, true);
  2998.         unfenced_alignment =
  2999.                 i915_gem_get_gtt_alignment(dev,
  3000.                                                     obj->base.size,
  3001.                                                     obj->tiling_mode, false);
  3002.  
  3003.         if (alignment == 0)
  3004.                 alignment = flags & PIN_MAPPABLE ? fence_alignment :
  3005.                                                 unfenced_alignment;
  3006.         if (flags & PIN_MAPPABLE && alignment & (fence_alignment - 1)) {
  3007.                 DRM_DEBUG("Invalid object alignment requested %u\n", alignment);
  3008.                 return ERR_PTR(-EINVAL);
  3009.         }
  3010.  
  3011.         size = flags & PIN_MAPPABLE ? fence_size : obj->base.size;
  3012.  
  3013.         /* If the object is bigger than the entire aperture, reject it early
  3014.          * before evicting everything in a vain attempt to find space.
  3015.          */
  3016.         if (obj->base.size > end) {
  3017.                 DRM_DEBUG("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%lu\n",
  3018.                           obj->base.size,
  3019.                           flags & PIN_MAPPABLE ? "mappable" : "total",
  3020.                           end);
  3021.                 return ERR_PTR(-E2BIG);
  3022.         }
  3023.  
  3024.         ret = i915_gem_object_get_pages(obj);
  3025.         if (ret)
  3026.                 return ERR_PTR(ret);
  3027.  
  3028.         i915_gem_object_pin_pages(obj);
  3029.  
  3030.         vma = i915_gem_obj_lookup_or_create_vma(obj, vm);
  3031.         if (IS_ERR(vma))
  3032.                 goto err_unpin;
  3033.  
  3034. search_free:
  3035.         ret = drm_mm_insert_node_in_range_generic(&vm->mm, &vma->node,
  3036.                                                   size, alignment,
  3037.                                                   obj->cache_level,
  3038.                                                   start, end,
  3039.                                                   DRM_MM_SEARCH_DEFAULT,
  3040.                                                   DRM_MM_CREATE_DEFAULT);
  3041.         if (ret) {
  3042.  
  3043.                 goto err_free_vma;
  3044.         }
  3045.         if (WARN_ON(!i915_gem_valid_gtt_space(vma, obj->cache_level))) {
  3046.                 ret = -EINVAL;
  3047.                 goto err_remove_node;
  3048.         }
  3049.  
  3050.         ret = i915_gem_gtt_prepare_object(obj);
  3051.         if (ret)
  3052.                 goto err_remove_node;
  3053.  
  3054.         list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
  3055.         list_add_tail(&vma->mm_list, &vm->inactive_list);
  3056.  
  3057.         trace_i915_vma_bind(vma, flags);
  3058.         vma->bind_vma(vma, obj->cache_level,
  3059.                       flags & PIN_GLOBAL ? GLOBAL_BIND : 0);
  3060.  
  3061.         return vma;
  3062.  
  3063. err_remove_node:
  3064.         drm_mm_remove_node(&vma->node);
  3065. err_free_vma:
  3066.         i915_gem_vma_destroy(vma);
  3067.         vma = ERR_PTR(ret);
  3068. err_unpin:
  3069.         i915_gem_object_unpin_pages(obj);
  3070.         return vma;
  3071. }
  3072.  
  3073. bool
  3074. i915_gem_clflush_object(struct drm_i915_gem_object *obj,
  3075.                         bool force)
  3076. {
  3077.         /* If we don't have a page list set up, then we're not pinned
  3078.          * to GPU, and we can ignore the cache flush because it'll happen
  3079.          * again at bind time.
  3080.          */
  3081.         if (obj->pages == NULL)
  3082.                 return false;
  3083.  
  3084.         /*
  3085.          * Stolen memory is always coherent with the GPU as it is explicitly
  3086.          * marked as wc by the system, or the system is cache-coherent.
  3087.          */
  3088.         if (obj->stolen || obj->phys_handle)
  3089.                 return false;
  3090.  
  3091.         /* If the GPU is snooping the contents of the CPU cache,
  3092.          * we do not need to manually clear the CPU cache lines.  However,
  3093.          * the caches are only snooped when the render cache is
  3094.          * flushed/invalidated.  As we always have to emit invalidations
  3095.          * and flushes when moving into and out of the RENDER domain, correct
  3096.          * snooping behaviour occurs naturally as the result of our domain
  3097.          * tracking.
  3098.          */
  3099.         if (!force && cpu_cache_is_coherent(obj->base.dev, obj->cache_level))
  3100.                 return false;
  3101.  
  3102.         trace_i915_gem_object_clflush(obj);
  3103.         drm_clflush_sg(obj->pages);
  3104.  
  3105.         return true;
  3106. }
  3107.  
  3108. /** Flushes the GTT write domain for the object if it's dirty. */
  3109. static void
  3110. i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
  3111. {
  3112.         uint32_t old_write_domain;
  3113.  
  3114.         if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
  3115.                 return;
  3116.  
  3117.         /* No actual flushing is required for the GTT write domain.  Writes
  3118.          * to it immediately go to main memory as far as we know, so there's
  3119.          * no chipset flush.  It also doesn't land in render cache.
  3120.          *
  3121.          * However, we do have to enforce the order so that all writes through
  3122.          * the GTT land before any writes to the device, such as updates to
  3123.          * the GATT itself.
  3124.          */
  3125.         wmb();
  3126.  
  3127.         old_write_domain = obj->base.write_domain;
  3128.         obj->base.write_domain = 0;
  3129.  
  3130.         intel_fb_obj_flush(obj, false);
  3131.  
  3132.         trace_i915_gem_object_change_domain(obj,
  3133.                                             obj->base.read_domains,
  3134.                                             old_write_domain);
  3135. }
  3136.  
  3137. /** Flushes the CPU write domain for the object if it's dirty. */
  3138. static void
  3139. i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj,
  3140.                                        bool force)
  3141. {
  3142.         uint32_t old_write_domain;
  3143.  
  3144.         if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
  3145.                 return;
  3146.  
  3147.         if (i915_gem_clflush_object(obj, force))
  3148.         i915_gem_chipset_flush(obj->base.dev);
  3149.  
  3150.         old_write_domain = obj->base.write_domain;
  3151.         obj->base.write_domain = 0;
  3152.  
  3153.         intel_fb_obj_flush(obj, false);
  3154.  
  3155.         trace_i915_gem_object_change_domain(obj,
  3156.                                             obj->base.read_domains,
  3157.                                             old_write_domain);
  3158. }
  3159.  
  3160. /**
  3161.  * Moves a single object to the GTT read, and possibly write domain.
  3162.  *
  3163.  * This function returns when the move is complete, including waiting on
  3164.  * flushes to occur.
  3165.  */
  3166. int
  3167. i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
  3168. {
  3169.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3170.         struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3171.         uint32_t old_write_domain, old_read_domains;
  3172.         int ret;
  3173.  
  3174.         /* Not valid to be called on unbound objects. */
  3175.         if (vma == NULL)
  3176.                 return -EINVAL;
  3177.  
  3178.         if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
  3179.                 return 0;
  3180.  
  3181.         ret = i915_gem_object_wait_rendering(obj, !write);
  3182.                 if (ret)
  3183.                         return ret;
  3184.  
  3185.         i915_gem_object_retire(obj);
  3186.         i915_gem_object_flush_cpu_write_domain(obj, false);
  3187.  
  3188.         /* Serialise direct access to this object with the barriers for
  3189.          * coherent writes from the GPU, by effectively invalidating the
  3190.          * GTT domain upon first access.
  3191.          */
  3192.         if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
  3193.                 mb();
  3194.  
  3195.         old_write_domain = obj->base.write_domain;
  3196.         old_read_domains = obj->base.read_domains;
  3197.  
  3198.         /* It should now be out of any other write domains, and we can update
  3199.          * the domain values for our changes.
  3200.          */
  3201.         BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
  3202.         obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3203.         if (write) {
  3204.                 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
  3205.                 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
  3206.                 obj->dirty = 1;
  3207.         }
  3208.  
  3209.         if (write)
  3210.                 intel_fb_obj_invalidate(obj, NULL);
  3211.  
  3212.         trace_i915_gem_object_change_domain(obj,
  3213.                                             old_read_domains,
  3214.                                             old_write_domain);
  3215.  
  3216.         /* And bump the LRU for this access */
  3217.         if (i915_gem_object_is_inactive(obj))
  3218.                         list_move_tail(&vma->mm_list,
  3219.                                        &dev_priv->gtt.base.inactive_list);
  3220.  
  3221.         return 0;
  3222. }
  3223.  
  3224. int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
  3225.                                     enum i915_cache_level cache_level)
  3226. {
  3227.         struct drm_device *dev = obj->base.dev;
  3228.         struct i915_vma *vma, *next;
  3229.         int ret;
  3230.  
  3231.         if (obj->cache_level == cache_level)
  3232.                 return 0;
  3233.  
  3234.         if (i915_gem_obj_is_pinned(obj)) {
  3235.                 DRM_DEBUG("can not change the cache level of pinned objects\n");
  3236.                 return -EBUSY;
  3237.         }
  3238.  
  3239.         list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  3240.                 if (!i915_gem_valid_gtt_space(vma, cache_level)) {
  3241.                         ret = i915_vma_unbind(vma);
  3242.                 if (ret)
  3243.                         return ret;
  3244.                 }
  3245.         }
  3246.  
  3247.         if (i915_gem_obj_bound_any(obj)) {
  3248.                 ret = i915_gem_object_finish_gpu(obj);
  3249.                 if (ret)
  3250.                         return ret;
  3251.  
  3252.                 i915_gem_object_finish_gtt(obj);
  3253.  
  3254.                 /* Before SandyBridge, you could not use tiling or fence
  3255.                  * registers with snooped memory, so relinquish any fences
  3256.                  * currently pointing to our region in the aperture.
  3257.                  */
  3258.                 if (INTEL_INFO(dev)->gen < 6) {
  3259.                         ret = i915_gem_object_put_fence(obj);
  3260.                         if (ret)
  3261.                                 return ret;
  3262.             }
  3263.  
  3264.                 list_for_each_entry(vma, &obj->vma_list, vma_link)
  3265.                         if (drm_mm_node_allocated(&vma->node))
  3266.                                 vma->bind_vma(vma, cache_level,
  3267.                                                 vma->bound & GLOBAL_BIND);
  3268.         }
  3269.  
  3270.         list_for_each_entry(vma, &obj->vma_list, vma_link)
  3271.                 vma->node.color = cache_level;
  3272.         obj->cache_level = cache_level;
  3273.  
  3274.         if (cpu_write_needs_clflush(obj)) {
  3275.                 u32 old_read_domains, old_write_domain;
  3276.  
  3277.                 /* If we're coming from LLC cached, then we haven't
  3278.                  * actually been tracking whether the data is in the
  3279.                  * CPU cache or not, since we only allow one bit set
  3280.                  * in obj->write_domain and have been skipping the clflushes.
  3281.                  * Just set it to the CPU cache for now.
  3282.                  */
  3283.                 i915_gem_object_retire(obj);
  3284.                 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
  3285.  
  3286.                 old_read_domains = obj->base.read_domains;
  3287.                 old_write_domain = obj->base.write_domain;
  3288.  
  3289.                 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3290.                 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3291.  
  3292.                 trace_i915_gem_object_change_domain(obj,
  3293.                                                     old_read_domains,
  3294.                                                     old_write_domain);
  3295.     }
  3296.  
  3297.         return 0;
  3298. }
  3299.  
  3300. int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
  3301.                                struct drm_file *file)
  3302. {
  3303.         struct drm_i915_gem_caching *args = data;
  3304.         struct drm_i915_gem_object *obj;
  3305.         int ret;
  3306.  
  3307.         ret = i915_mutex_lock_interruptible(dev);
  3308.         if (ret)
  3309.                 return ret;
  3310.  
  3311.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3312.         if (&obj->base == NULL) {
  3313.                 ret = -ENOENT;
  3314.                 goto unlock;
  3315.         }
  3316.  
  3317.         switch (obj->cache_level) {
  3318.         case I915_CACHE_LLC:
  3319.         case I915_CACHE_L3_LLC:
  3320.                 args->caching = I915_CACHING_CACHED;
  3321.                 break;
  3322.  
  3323.         case I915_CACHE_WT:
  3324.                 args->caching = I915_CACHING_DISPLAY;
  3325.                 break;
  3326.  
  3327.         default:
  3328.                 args->caching = I915_CACHING_NONE;
  3329.                 break;
  3330.         }
  3331.  
  3332.         drm_gem_object_unreference(&obj->base);
  3333. unlock:
  3334.         mutex_unlock(&dev->struct_mutex);
  3335.         return ret;
  3336. }
  3337.  
  3338. int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
  3339.                                struct drm_file *file)
  3340. {
  3341.         struct drm_i915_gem_caching *args = data;
  3342.         struct drm_i915_gem_object *obj;
  3343.         enum i915_cache_level level;
  3344.         int ret;
  3345.  
  3346.         switch (args->caching) {
  3347.         case I915_CACHING_NONE:
  3348.                 level = I915_CACHE_NONE;
  3349.                 break;
  3350.         case I915_CACHING_CACHED:
  3351.                 level = I915_CACHE_LLC;
  3352.                 break;
  3353.         case I915_CACHING_DISPLAY:
  3354.                 level = HAS_WT(dev) ? I915_CACHE_WT : I915_CACHE_NONE;
  3355.                 break;
  3356.         default:
  3357.                 return -EINVAL;
  3358.         }
  3359.  
  3360.         ret = i915_mutex_lock_interruptible(dev);
  3361.         if (ret)
  3362.                 return ret;
  3363.  
  3364.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3365.         if (&obj->base == NULL) {
  3366.                 ret = -ENOENT;
  3367.                 goto unlock;
  3368.         }
  3369.  
  3370.         ret = i915_gem_object_set_cache_level(obj, level);
  3371.  
  3372.         drm_gem_object_unreference(&obj->base);
  3373. unlock:
  3374.         mutex_unlock(&dev->struct_mutex);
  3375.         return ret;
  3376. }
  3377.  
  3378. static bool is_pin_display(struct drm_i915_gem_object *obj)
  3379. {
  3380.         struct i915_vma *vma;
  3381.  
  3382.         vma = i915_gem_obj_to_ggtt(obj);
  3383.         if (!vma)
  3384.                 return false;
  3385.  
  3386.         /* There are 3 sources that pin objects:
  3387.          *   1. The display engine (scanouts, sprites, cursors);
  3388.          *   2. Reservations for execbuffer;
  3389.          *   3. The user.
  3390.          *
  3391.          * We can ignore reservations as we hold the struct_mutex and
  3392.          * are only called outside of the reservation path.  The user
  3393.          * can only increment pin_count once, and so if after
  3394.          * subtracting the potential reference by the user, any pin_count
  3395.          * remains, it must be due to another use by the display engine.
  3396.          */
  3397.         return vma->pin_count - !!obj->user_pin_count;
  3398. }
  3399.  
  3400. /*
  3401.  * Prepare buffer for display plane (scanout, cursors, etc).
  3402.  * Can be called from an uninterruptible phase (modesetting) and allows
  3403.  * any flushes to be pipelined (for pageflips).
  3404.  */
  3405. int
  3406. i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
  3407.                                      u32 alignment,
  3408.                                      struct intel_engine_cs *pipelined)
  3409. {
  3410.         u32 old_read_domains, old_write_domain;
  3411.         bool was_pin_display;
  3412.         int ret;
  3413.  
  3414.         if (pipelined != obj->ring) {
  3415.                 ret = i915_gem_object_sync(obj, pipelined);
  3416.         if (ret)
  3417.                 return ret;
  3418.         }
  3419.  
  3420.         /* Mark the pin_display early so that we account for the
  3421.          * display coherency whilst setting up the cache domains.
  3422.          */
  3423.         was_pin_display = obj->pin_display;
  3424.         obj->pin_display = true;
  3425.  
  3426.         /* The display engine is not coherent with the LLC cache on gen6.  As
  3427.          * a result, we make sure that the pinning that is about to occur is
  3428.          * done with uncached PTEs. This is lowest common denominator for all
  3429.          * chipsets.
  3430.          *
  3431.          * However for gen6+, we could do better by using the GFDT bit instead
  3432.          * of uncaching, which would allow us to flush all the LLC-cached data
  3433.          * with that bit in the PTE to main memory with just one PIPE_CONTROL.
  3434.          */
  3435.         ret = i915_gem_object_set_cache_level(obj,
  3436.                                               HAS_WT(obj->base.dev) ? I915_CACHE_WT : I915_CACHE_NONE);
  3437.         if (ret)
  3438.                 goto err_unpin_display;
  3439.  
  3440.         /* As the user may map the buffer once pinned in the display plane
  3441.          * (e.g. libkms for the bootup splash), we have to ensure that we
  3442.          * always use map_and_fenceable for all scanout buffers.
  3443.          */
  3444.         ret = i915_gem_obj_ggtt_pin(obj, alignment, PIN_MAPPABLE);
  3445.         if (ret)
  3446.                 goto err_unpin_display;
  3447.  
  3448.         i915_gem_object_flush_cpu_write_domain(obj, true);
  3449.  
  3450.         old_write_domain = obj->base.write_domain;
  3451.         old_read_domains = obj->base.read_domains;
  3452.  
  3453.         /* It should now be out of any other write domains, and we can update
  3454.          * the domain values for our changes.
  3455.          */
  3456.         obj->base.write_domain = 0;
  3457.         obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
  3458.  
  3459.         trace_i915_gem_object_change_domain(obj,
  3460.                                             old_read_domains,
  3461.                                             old_write_domain);
  3462.  
  3463.         return 0;
  3464.  
  3465. err_unpin_display:
  3466.         WARN_ON(was_pin_display != is_pin_display(obj));
  3467.         obj->pin_display = was_pin_display;
  3468.         return ret;
  3469. }
  3470.  
  3471. void
  3472. i915_gem_object_unpin_from_display_plane(struct drm_i915_gem_object *obj)
  3473. {
  3474.         i915_gem_object_ggtt_unpin(obj);
  3475.         obj->pin_display = is_pin_display(obj);
  3476. }
  3477.  
  3478. int
  3479. i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
  3480. {
  3481.         int ret;
  3482.  
  3483.         if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
  3484.                 return 0;
  3485.  
  3486.         ret = i915_gem_object_wait_rendering(obj, false);
  3487.     if (ret)
  3488.         return ret;
  3489.  
  3490.         /* Ensure that we invalidate the GPU's caches and TLBs. */
  3491.         obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
  3492.         return 0;
  3493. }
  3494.  
  3495. /**
  3496.  * Moves a single object to the CPU read, and possibly write domain.
  3497.  *
  3498.  * This function returns when the move is complete, including waiting on
  3499.  * flushes to occur.
  3500.  */
  3501. int
  3502. i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
  3503. {
  3504.         uint32_t old_write_domain, old_read_domains;
  3505.         int ret;
  3506.  
  3507.         if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
  3508.                 return 0;
  3509.  
  3510.         ret = i915_gem_object_wait_rendering(obj, !write);
  3511.         if (ret)
  3512.                 return ret;
  3513.  
  3514.         i915_gem_object_retire(obj);
  3515.         i915_gem_object_flush_gtt_write_domain(obj);
  3516.  
  3517.         old_write_domain = obj->base.write_domain;
  3518.         old_read_domains = obj->base.read_domains;
  3519.  
  3520.         /* Flush the CPU cache if it's still invalid. */
  3521.         if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
  3522.                 i915_gem_clflush_object(obj, false);
  3523.  
  3524.                 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
  3525.         }
  3526.  
  3527.         /* It should now be out of any other write domains, and we can update
  3528.          * the domain values for our changes.
  3529.          */
  3530.         BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
  3531.  
  3532.         /* If we're writing through the CPU, then the GPU read domains will
  3533.          * need to be invalidated at next use.
  3534.          */
  3535.         if (write) {
  3536.                 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3537.                 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3538.         }
  3539.  
  3540.         if (write)
  3541.                 intel_fb_obj_invalidate(obj, NULL);
  3542.  
  3543.         trace_i915_gem_object_change_domain(obj,
  3544.                                             old_read_domains,
  3545.                                             old_write_domain);
  3546.  
  3547.         return 0;
  3548. }
  3549.  
  3550. /* Throttle our rendering by waiting until the ring has completed our requests
  3551.  * emitted over 20 msec ago.
  3552.  *
  3553.  * Note that if we were to use the current jiffies each time around the loop,
  3554.  * we wouldn't escape the function with any frames outstanding if the time to
  3555.  * render a frame was over 20ms.
  3556.  *
  3557.  * This should get us reasonable parallelism between CPU and GPU but also
  3558.  * relatively low latency when blocking on a particular request to finish.
  3559.  */
  3560. static int
  3561. i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
  3562. {
  3563.         struct drm_i915_private *dev_priv = dev->dev_private;
  3564.         struct drm_i915_file_private *file_priv = file->driver_priv;
  3565.         unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
  3566.         struct drm_i915_gem_request *request;
  3567.         struct intel_engine_cs *ring = NULL;
  3568.         unsigned reset_counter;
  3569.         u32 seqno = 0;
  3570.         int ret;
  3571.  
  3572.         ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
  3573.         if (ret)
  3574.                 return ret;
  3575.  
  3576.         ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
  3577.         if (ret)
  3578.                 return ret;
  3579.  
  3580.         spin_lock(&file_priv->mm.lock);
  3581.         list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
  3582.                 if (time_after_eq(request->emitted_jiffies, recent_enough))
  3583.                         break;
  3584.  
  3585.                 ring = request->ring;
  3586.                 seqno = request->seqno;
  3587.         }
  3588.         reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
  3589.         spin_unlock(&file_priv->mm.lock);
  3590.  
  3591.         if (seqno == 0)
  3592.                 return 0;
  3593.  
  3594.         ret = __i915_wait_seqno(ring, seqno, reset_counter, true, NULL, NULL);
  3595.         if (ret == 0)
  3596.                 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
  3597.  
  3598.         return ret;
  3599. }
  3600.  
  3601. static bool
  3602. i915_vma_misplaced(struct i915_vma *vma, uint32_t alignment, uint64_t flags)
  3603. {
  3604.         struct drm_i915_gem_object *obj = vma->obj;
  3605.  
  3606.         if (alignment &&
  3607.             vma->node.start & (alignment - 1))
  3608.                 return true;
  3609.  
  3610.         if (flags & PIN_MAPPABLE && !obj->map_and_fenceable)
  3611.                 return true;
  3612.  
  3613.         if (flags & PIN_OFFSET_BIAS &&
  3614.             vma->node.start < (flags & PIN_OFFSET_MASK))
  3615.                 return true;
  3616.  
  3617.         return false;
  3618. }
  3619.  
  3620. int
  3621. i915_gem_object_pin(struct drm_i915_gem_object *obj,
  3622.                     struct i915_address_space *vm,
  3623.                     uint32_t alignment,
  3624.                     uint64_t flags)
  3625. {
  3626.         struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3627.         struct i915_vma *vma;
  3628.         unsigned bound;
  3629.         int ret;
  3630.  
  3631.         if (WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base))
  3632.                 return -ENODEV;
  3633.  
  3634.         if (WARN_ON(flags & (PIN_GLOBAL | PIN_MAPPABLE) && !i915_is_ggtt(vm)))
  3635.                 return -EINVAL;
  3636.  
  3637.         if (WARN_ON((flags & (PIN_MAPPABLE | PIN_GLOBAL)) == PIN_MAPPABLE))
  3638.                 return -EINVAL;
  3639.  
  3640.         vma = i915_gem_obj_to_vma(obj, vm);
  3641.         if (vma) {
  3642.                 if (WARN_ON(vma->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
  3643.                         return -EBUSY;
  3644.  
  3645.                 if (i915_vma_misplaced(vma, alignment, flags)) {
  3646.                         WARN(vma->pin_count,
  3647.                              "bo is already pinned with incorrect alignment:"
  3648.                              " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
  3649.                              " obj->map_and_fenceable=%d\n",
  3650.                              i915_gem_obj_offset(obj, vm), alignment,
  3651.                              !!(flags & PIN_MAPPABLE),
  3652.                              obj->map_and_fenceable);
  3653.                         ret = i915_vma_unbind(vma);
  3654.                         if (ret)
  3655.                                 return ret;
  3656.  
  3657.                         vma = NULL;
  3658.                 }
  3659.         }
  3660.  
  3661.         bound = vma ? vma->bound : 0;
  3662.         if (vma == NULL || !drm_mm_node_allocated(&vma->node)) {
  3663.                 vma = i915_gem_object_bind_to_vm(obj, vm, alignment, flags);
  3664.                 if (IS_ERR(vma))
  3665.                         return PTR_ERR(vma);
  3666.         }
  3667.  
  3668.         if (flags & PIN_GLOBAL && !(vma->bound & GLOBAL_BIND))
  3669.                 vma->bind_vma(vma, obj->cache_level, GLOBAL_BIND);
  3670.  
  3671.         if ((bound ^ vma->bound) & GLOBAL_BIND) {
  3672.                 bool mappable, fenceable;
  3673.                 u32 fence_size, fence_alignment;
  3674.  
  3675.                 fence_size = i915_gem_get_gtt_size(obj->base.dev,
  3676.                                                    obj->base.size,
  3677.                                                    obj->tiling_mode);
  3678.                 fence_alignment = i915_gem_get_gtt_alignment(obj->base.dev,
  3679.                                                              obj->base.size,
  3680.                                                              obj->tiling_mode,
  3681.                                                              true);
  3682.  
  3683.                 fenceable = (vma->node.size == fence_size &&
  3684.                              (vma->node.start & (fence_alignment - 1)) == 0);
  3685.  
  3686.                 mappable = (vma->node.start + obj->base.size <=
  3687.                             dev_priv->gtt.mappable_end);
  3688.  
  3689.                 obj->map_and_fenceable = mappable && fenceable;
  3690.         }
  3691.  
  3692.         WARN_ON(flags & PIN_MAPPABLE && !obj->map_and_fenceable);
  3693.  
  3694.         vma->pin_count++;
  3695.         if (flags & PIN_MAPPABLE)
  3696.                 obj->pin_mappable |= true;
  3697.  
  3698.         return 0;
  3699. }
  3700.  
  3701. void
  3702. i915_gem_object_ggtt_unpin(struct drm_i915_gem_object *obj)
  3703. {
  3704.         struct i915_vma *vma = i915_gem_obj_to_ggtt(obj);
  3705.  
  3706.         BUG_ON(!vma);
  3707.         BUG_ON(vma->pin_count == 0);
  3708.         BUG_ON(!i915_gem_obj_ggtt_bound(obj));
  3709.  
  3710.         if (--vma->pin_count == 0)
  3711.                 obj->pin_mappable = false;
  3712. }
  3713.  
  3714. bool
  3715. i915_gem_object_pin_fence(struct drm_i915_gem_object *obj)
  3716. {
  3717.         if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3718.                 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3719.                 struct i915_vma *ggtt_vma = i915_gem_obj_to_ggtt(obj);
  3720.  
  3721.                 WARN_ON(!ggtt_vma ||
  3722.                         dev_priv->fence_regs[obj->fence_reg].pin_count >
  3723.                         ggtt_vma->pin_count);
  3724.                 dev_priv->fence_regs[obj->fence_reg].pin_count++;
  3725.                 return true;
  3726.         } else
  3727.                 return false;
  3728. }
  3729.  
  3730. void
  3731. i915_gem_object_unpin_fence(struct drm_i915_gem_object *obj)
  3732. {
  3733.         if (obj->fence_reg != I915_FENCE_REG_NONE) {
  3734.                 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
  3735.                 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count <= 0);
  3736.                 dev_priv->fence_regs[obj->fence_reg].pin_count--;
  3737.         }
  3738. }
  3739.  
  3740. int
  3741. i915_gem_pin_ioctl(struct drm_device *dev, void *data,
  3742.                    struct drm_file *file)
  3743. {
  3744.         struct drm_i915_gem_pin *args = data;
  3745.         struct drm_i915_gem_object *obj;
  3746.         int ret;
  3747.  
  3748.         if (drm_core_check_feature(dev, DRIVER_MODESET))
  3749.                 return -ENODEV;
  3750.  
  3751.         ret = i915_mutex_lock_interruptible(dev);
  3752.         if (ret)
  3753.                 return ret;
  3754.  
  3755.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3756.         if (&obj->base == NULL) {
  3757.                 ret = -ENOENT;
  3758.                 goto unlock;
  3759.         }
  3760.  
  3761.         if (obj->madv != I915_MADV_WILLNEED) {
  3762.                 DRM_DEBUG("Attempting to pin a purgeable buffer\n");
  3763.                 ret = -EFAULT;
  3764.                 goto out;
  3765.         }
  3766.  
  3767.         if (obj->pin_filp != NULL && obj->pin_filp != file) {
  3768.                 DRM_DEBUG("Already pinned in i915_gem_pin_ioctl(): %d\n",
  3769.                           args->handle);
  3770.                 ret = -EINVAL;
  3771.                 goto out;
  3772.         }
  3773.  
  3774.         if (obj->user_pin_count == ULONG_MAX) {
  3775.                 ret = -EBUSY;
  3776.                 goto out;
  3777.         }
  3778.  
  3779.         if (obj->user_pin_count == 0) {
  3780.                 ret = i915_gem_obj_ggtt_pin(obj, args->alignment, PIN_MAPPABLE);
  3781.                 if (ret)
  3782.                         goto out;
  3783.         }
  3784.  
  3785.         obj->user_pin_count++;
  3786.         obj->pin_filp = file;
  3787.  
  3788.         args->offset = i915_gem_obj_ggtt_offset(obj);
  3789. out:
  3790.         drm_gem_object_unreference(&obj->base);
  3791. unlock:
  3792.         mutex_unlock(&dev->struct_mutex);
  3793.         return ret;
  3794. }
  3795.  
  3796. int
  3797. i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
  3798.                      struct drm_file *file)
  3799. {
  3800.         struct drm_i915_gem_pin *args = data;
  3801.         struct drm_i915_gem_object *obj;
  3802.         int ret;
  3803.  
  3804.         if (drm_core_check_feature(dev, DRIVER_MODESET))
  3805.                 return -ENODEV;
  3806.  
  3807.         ret = i915_mutex_lock_interruptible(dev);
  3808.         if (ret)
  3809.                 return ret;
  3810.  
  3811.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3812.         if (&obj->base == NULL) {
  3813.                 ret = -ENOENT;
  3814.                 goto unlock;
  3815.         }
  3816.  
  3817.         if (obj->pin_filp != file) {
  3818.                 DRM_DEBUG("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
  3819.                           args->handle);
  3820.                 ret = -EINVAL;
  3821.                 goto out;
  3822.         }
  3823.         obj->user_pin_count--;
  3824.         if (obj->user_pin_count == 0) {
  3825.                 obj->pin_filp = NULL;
  3826.                 i915_gem_object_ggtt_unpin(obj);
  3827.         }
  3828.  
  3829. out:
  3830.         drm_gem_object_unreference(&obj->base);
  3831. unlock:
  3832.         mutex_unlock(&dev->struct_mutex);
  3833.         return ret;
  3834. }
  3835.  
  3836. int
  3837. i915_gem_busy_ioctl(struct drm_device *dev, void *data,
  3838.                     struct drm_file *file)
  3839. {
  3840.         struct drm_i915_gem_busy *args = data;
  3841.         struct drm_i915_gem_object *obj;
  3842.         int ret;
  3843.  
  3844.         ret = i915_mutex_lock_interruptible(dev);
  3845.         if (ret)
  3846.                 return ret;
  3847.  
  3848.         obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
  3849.         if (&obj->base == NULL) {
  3850.                 ret = -ENOENT;
  3851.                 goto unlock;
  3852.         }
  3853.  
  3854.         /* Count all active objects as busy, even if they are currently not used
  3855.          * by the gpu. Users of this interface expect objects to eventually
  3856.          * become non-busy without any further actions, therefore emit any
  3857.          * necessary flushes here.
  3858.          */
  3859.         ret = i915_gem_object_flush_active(obj);
  3860.  
  3861.         args->busy = obj->active;
  3862.         if (obj->ring) {
  3863.                 BUILD_BUG_ON(I915_NUM_RINGS > 16);
  3864.                 args->busy |= intel_ring_flag(obj->ring) << 16;
  3865.         }
  3866.  
  3867.         drm_gem_object_unreference(&obj->base);
  3868. unlock:
  3869.         mutex_unlock(&dev->struct_mutex);
  3870.         return ret;
  3871. }
  3872.  
  3873. int
  3874. i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
  3875.                         struct drm_file *file_priv)
  3876. {
  3877.         return i915_gem_ring_throttle(dev, file_priv);
  3878. }
  3879.  
  3880. #if 0
  3881.  
  3882. int
  3883. i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
  3884.                        struct drm_file *file_priv)
  3885. {
  3886.         struct drm_i915_private *dev_priv = dev->dev_private;
  3887.         struct drm_i915_gem_madvise *args = data;
  3888.         struct drm_i915_gem_object *obj;
  3889.         int ret;
  3890.  
  3891.         switch (args->madv) {
  3892.         case I915_MADV_DONTNEED:
  3893.         case I915_MADV_WILLNEED:
  3894.             break;
  3895.         default:
  3896.             return -EINVAL;
  3897.         }
  3898.  
  3899.         ret = i915_mutex_lock_interruptible(dev);
  3900.         if (ret)
  3901.                 return ret;
  3902.  
  3903.         obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
  3904.         if (&obj->base == NULL) {
  3905.                 ret = -ENOENT;
  3906.                 goto unlock;
  3907.         }
  3908.  
  3909.         if (i915_gem_obj_is_pinned(obj)) {
  3910.                 ret = -EINVAL;
  3911.                 goto out;
  3912.         }
  3913.  
  3914.         if (obj->pages &&
  3915.             obj->tiling_mode != I915_TILING_NONE &&
  3916.             dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
  3917.                 if (obj->madv == I915_MADV_WILLNEED)
  3918.                         i915_gem_object_unpin_pages(obj);
  3919.                 if (args->madv == I915_MADV_WILLNEED)
  3920.                         i915_gem_object_pin_pages(obj);
  3921.         }
  3922.  
  3923.         if (obj->madv != __I915_MADV_PURGED)
  3924.                 obj->madv = args->madv;
  3925.  
  3926.         /* if the object is no longer attached, discard its backing storage */
  3927.         if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
  3928.                 i915_gem_object_truncate(obj);
  3929.  
  3930.         args->retained = obj->madv != __I915_MADV_PURGED;
  3931.  
  3932. out:
  3933.         drm_gem_object_unreference(&obj->base);
  3934. unlock:
  3935.         mutex_unlock(&dev->struct_mutex);
  3936.         return ret;
  3937. }
  3938. #endif
  3939.  
  3940. void i915_gem_object_init(struct drm_i915_gem_object *obj,
  3941.                           const struct drm_i915_gem_object_ops *ops)
  3942. {
  3943.         INIT_LIST_HEAD(&obj->global_list);
  3944.         INIT_LIST_HEAD(&obj->ring_list);
  3945.         INIT_LIST_HEAD(&obj->obj_exec_link);
  3946.         INIT_LIST_HEAD(&obj->vma_list);
  3947.  
  3948.         obj->ops = ops;
  3949.  
  3950.         obj->fence_reg = I915_FENCE_REG_NONE;
  3951.         obj->madv = I915_MADV_WILLNEED;
  3952.  
  3953.         i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
  3954. }
  3955.  
  3956. static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
  3957.         .get_pages = i915_gem_object_get_pages_gtt,
  3958.         .put_pages = i915_gem_object_put_pages_gtt,
  3959. };
  3960.  
  3961. struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
  3962.                                                   size_t size)
  3963. {
  3964.         struct drm_i915_gem_object *obj;
  3965.         struct address_space *mapping;
  3966.         gfp_t mask;
  3967.  
  3968.         obj = i915_gem_object_alloc(dev);
  3969.         if (obj == NULL)
  3970.                 return NULL;
  3971.  
  3972.         if (drm_gem_object_init(dev, &obj->base, size) != 0) {
  3973.                 i915_gem_object_free(obj);
  3974.                 return NULL;
  3975.         }
  3976.  
  3977.  
  3978.         i915_gem_object_init(obj, &i915_gem_object_ops);
  3979.  
  3980.         obj->base.write_domain = I915_GEM_DOMAIN_CPU;
  3981.         obj->base.read_domains = I915_GEM_DOMAIN_CPU;
  3982.  
  3983.         if (HAS_LLC(dev)) {
  3984.                 /* On some devices, we can have the GPU use the LLC (the CPU
  3985.                  * cache) for about a 10% performance improvement
  3986.                  * compared to uncached.  Graphics requests other than
  3987.                  * display scanout are coherent with the CPU in
  3988.                  * accessing this cache.  This means in this mode we
  3989.                  * don't need to clflush on the CPU side, and on the
  3990.                  * GPU side we only need to flush internal caches to
  3991.                  * get data visible to the CPU.
  3992.                  *
  3993.                  * However, we maintain the display planes as UC, and so
  3994.                  * need to rebind when first used as such.
  3995.                  */
  3996.                 obj->cache_level = I915_CACHE_LLC;
  3997.         } else
  3998.                 obj->cache_level = I915_CACHE_NONE;
  3999.  
  4000.         trace_i915_gem_object_create(obj);
  4001.  
  4002.         return obj;
  4003. }
  4004.  
  4005. void i915_gem_free_object(struct drm_gem_object *gem_obj)
  4006. {
  4007.         struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
  4008.         struct drm_device *dev = obj->base.dev;
  4009.         struct drm_i915_private *dev_priv = dev->dev_private;
  4010.         struct i915_vma *vma, *next;
  4011.  
  4012.         intel_runtime_pm_get(dev_priv);
  4013.  
  4014.         trace_i915_gem_object_destroy(obj);
  4015.  
  4016.         list_for_each_entry_safe(vma, next, &obj->vma_list, vma_link) {
  4017.                 int ret;
  4018.  
  4019.                 vma->pin_count = 0;
  4020.                 ret = i915_vma_unbind(vma);
  4021.                 if (WARN_ON(ret == -ERESTARTSYS)) {
  4022.                 bool was_interruptible;
  4023.  
  4024.                 was_interruptible = dev_priv->mm.interruptible;
  4025.                 dev_priv->mm.interruptible = false;
  4026.  
  4027.                         WARN_ON(i915_vma_unbind(vma));
  4028.  
  4029.                 dev_priv->mm.interruptible = was_interruptible;
  4030.         }
  4031.         }
  4032.  
  4033.         /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
  4034.          * before progressing. */
  4035.         if (obj->stolen)
  4036.                 i915_gem_object_unpin_pages(obj);
  4037.  
  4038.         WARN_ON(obj->frontbuffer_bits);
  4039.  
  4040.         if (obj->pages && obj->madv == I915_MADV_WILLNEED &&
  4041.             dev_priv->quirks & QUIRK_PIN_SWIZZLED_PAGES &&
  4042.             obj->tiling_mode != I915_TILING_NONE)
  4043.                 i915_gem_object_unpin_pages(obj);
  4044.  
  4045.         if (WARN_ON(obj->pages_pin_count))
  4046.         obj->pages_pin_count = 0;
  4047.         i915_gem_object_put_pages(obj);
  4048. //   i915_gem_object_free_mmap_offset(obj);
  4049.  
  4050.         BUG_ON(obj->pages);
  4051.  
  4052.  
  4053.     if(obj->base.filp != NULL)
  4054.     {
  4055. //        printf("filp %p\n", obj->base.filp);
  4056.         shmem_file_delete(obj->base.filp);
  4057.     }
  4058.  
  4059.         drm_gem_object_release(&obj->base);
  4060.         i915_gem_info_remove_obj(dev_priv, obj->base.size);
  4061.  
  4062.         kfree(obj->bit_17);
  4063.         i915_gem_object_free(obj);
  4064.  
  4065.         intel_runtime_pm_put(dev_priv);
  4066. }
  4067.  
  4068. struct i915_vma *i915_gem_obj_to_vma(struct drm_i915_gem_object *obj,
  4069.                                      struct i915_address_space *vm)
  4070. {
  4071.         struct i915_vma *vma;
  4072.         list_for_each_entry(vma, &obj->vma_list, vma_link)
  4073.                 if (vma->vm == vm)
  4074.                         return vma;
  4075.  
  4076.         return NULL;
  4077. }
  4078.  
  4079. void i915_gem_vma_destroy(struct i915_vma *vma)
  4080. {
  4081.         struct i915_address_space *vm = NULL;
  4082.         WARN_ON(vma->node.allocated);
  4083.  
  4084.         /* Keep the vma as a placeholder in the execbuffer reservation lists */
  4085.         if (!list_empty(&vma->exec_list))
  4086.                 return;
  4087.  
  4088.         vm = vma->vm;
  4089.  
  4090.         if (!i915_is_ggtt(vm))
  4091.                 i915_ppgtt_put(i915_vm_to_ppgtt(vm));
  4092.  
  4093.         list_del(&vma->vma_link);
  4094.  
  4095.         kfree(vma);
  4096. }
  4097.  
  4098. #if 0
  4099. int
  4100. i915_gem_suspend(struct drm_device *dev)
  4101. {
  4102.         struct drm_i915_private *dev_priv = dev->dev_private;
  4103.         int ret = 0;
  4104.  
  4105.         mutex_lock(&dev->struct_mutex);
  4106.         ret = i915_gpu_idle(dev);
  4107.         if (ret)
  4108.                 goto err;
  4109.  
  4110.         i915_gem_retire_requests(dev);
  4111.  
  4112.         /* Under UMS, be paranoid and evict. */
  4113.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4114.                 i915_gem_evict_everything(dev);
  4115.  
  4116.         i915_gem_stop_ringbuffers(dev);
  4117.         mutex_unlock(&dev->struct_mutex);
  4118.  
  4119.         del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
  4120.         cancel_delayed_work_sync(&dev_priv->mm.retire_work);
  4121.         flush_delayed_work(&dev_priv->mm.idle_work);
  4122.  
  4123.         return 0;
  4124.  
  4125. err:
  4126.         mutex_unlock(&dev->struct_mutex);
  4127.         return ret;
  4128. }
  4129. #endif
  4130.  
  4131. int i915_gem_l3_remap(struct intel_engine_cs *ring, int slice)
  4132. {
  4133.         struct drm_device *dev = ring->dev;
  4134.         struct drm_i915_private *dev_priv = dev->dev_private;
  4135.         u32 reg_base = GEN7_L3LOG_BASE + (slice * 0x200);
  4136.         u32 *remap_info = dev_priv->l3_parity.remap_info[slice];
  4137.         int i, ret;
  4138.  
  4139.         if (!HAS_L3_DPF(dev) || !remap_info)
  4140.                 return 0;
  4141.  
  4142.         ret = intel_ring_begin(ring, GEN7_L3LOG_SIZE / 4 * 3);
  4143.         if (ret)
  4144.                 return ret;
  4145.  
  4146.         /*
  4147.          * Note: We do not worry about the concurrent register cacheline hang
  4148.          * here because no other code should access these registers other than
  4149.          * at initialization time.
  4150.          */
  4151.         for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
  4152.                 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
  4153.                 intel_ring_emit(ring, reg_base + i);
  4154.                 intel_ring_emit(ring, remap_info[i/4]);
  4155.         }
  4156.  
  4157.         intel_ring_advance(ring);
  4158.  
  4159.         return ret;
  4160. }
  4161.  
  4162. void i915_gem_init_swizzling(struct drm_device *dev)
  4163. {
  4164.         struct drm_i915_private *dev_priv = dev->dev_private;
  4165.  
  4166.         if (INTEL_INFO(dev)->gen < 5 ||
  4167.             dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
  4168.                 return;
  4169.  
  4170.         I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
  4171.                                  DISP_TILE_SURFACE_SWIZZLING);
  4172.  
  4173.         if (IS_GEN5(dev))
  4174.                 return;
  4175.  
  4176.         I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
  4177.         if (IS_GEN6(dev))
  4178.                 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
  4179.         else if (IS_GEN7(dev))
  4180.                 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
  4181.         else if (IS_GEN8(dev))
  4182.                 I915_WRITE(GAMTARBMODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW));
  4183.         else
  4184.                 BUG();
  4185. }
  4186.  
  4187. static bool
  4188. intel_enable_blt(struct drm_device *dev)
  4189. {
  4190.         if (!HAS_BLT(dev))
  4191.                 return false;
  4192.  
  4193.         /* The blitter was dysfunctional on early prototypes */
  4194.         if (IS_GEN6(dev) && dev->pdev->revision < 8) {
  4195.                 DRM_INFO("BLT not supported on this pre-production hardware;"
  4196.                          " graphics performance will be degraded.\n");
  4197.                 return false;
  4198.         }
  4199.  
  4200.         return true;
  4201. }
  4202.  
  4203. static void init_unused_ring(struct drm_device *dev, u32 base)
  4204. {
  4205.         struct drm_i915_private *dev_priv = dev->dev_private;
  4206.  
  4207.         I915_WRITE(RING_CTL(base), 0);
  4208.         I915_WRITE(RING_HEAD(base), 0);
  4209.         I915_WRITE(RING_TAIL(base), 0);
  4210.         I915_WRITE(RING_START(base), 0);
  4211. }
  4212.  
  4213. static void init_unused_rings(struct drm_device *dev)
  4214. {
  4215.         if (IS_I830(dev)) {
  4216.                 init_unused_ring(dev, PRB1_BASE);
  4217.                 init_unused_ring(dev, SRB0_BASE);
  4218.                 init_unused_ring(dev, SRB1_BASE);
  4219.                 init_unused_ring(dev, SRB2_BASE);
  4220.                 init_unused_ring(dev, SRB3_BASE);
  4221.         } else if (IS_GEN2(dev)) {
  4222.                 init_unused_ring(dev, SRB0_BASE);
  4223.                 init_unused_ring(dev, SRB1_BASE);
  4224.         } else if (IS_GEN3(dev)) {
  4225.                 init_unused_ring(dev, PRB1_BASE);
  4226.                 init_unused_ring(dev, PRB2_BASE);
  4227.         }
  4228. }
  4229.  
  4230. int i915_gem_init_rings(struct drm_device *dev)
  4231. {
  4232.         struct drm_i915_private *dev_priv = dev->dev_private;
  4233.         int ret;
  4234.  
  4235.         /*
  4236.          * At least 830 can leave some of the unused rings
  4237.          * "active" (ie. head != tail) after resume which
  4238.          * will prevent c3 entry. Makes sure all unused rings
  4239.          * are totally idle.
  4240.          */
  4241.         init_unused_rings(dev);
  4242.  
  4243.         ret = intel_init_render_ring_buffer(dev);
  4244.         if (ret)
  4245.                 return ret;
  4246.  
  4247.     if (HAS_BSD(dev)) {
  4248.                 ret = intel_init_bsd_ring_buffer(dev);
  4249.                 if (ret)
  4250.                         goto cleanup_render_ring;
  4251.         }
  4252.  
  4253.         if (intel_enable_blt(dev)) {
  4254.                 ret = intel_init_blt_ring_buffer(dev);
  4255.                 if (ret)
  4256.                         goto cleanup_bsd_ring;
  4257.         }
  4258.  
  4259.         if (HAS_VEBOX(dev)) {
  4260.                 ret = intel_init_vebox_ring_buffer(dev);
  4261.                 if (ret)
  4262.                         goto cleanup_blt_ring;
  4263.         }
  4264.  
  4265.         if (HAS_BSD2(dev)) {
  4266.                 ret = intel_init_bsd2_ring_buffer(dev);
  4267.                 if (ret)
  4268.                         goto cleanup_vebox_ring;
  4269.         }
  4270.  
  4271.         ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
  4272.         if (ret)
  4273.                 goto cleanup_bsd2_ring;
  4274.  
  4275.         return 0;
  4276.  
  4277. cleanup_bsd2_ring:
  4278.         intel_cleanup_ring_buffer(&dev_priv->ring[VCS2]);
  4279. cleanup_vebox_ring:
  4280.         intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
  4281. cleanup_blt_ring:
  4282.         intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
  4283. cleanup_bsd_ring:
  4284.         intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
  4285. cleanup_render_ring:
  4286.         intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
  4287.  
  4288.         return ret;
  4289. }
  4290.  
  4291. int
  4292. i915_gem_init_hw(struct drm_device *dev)
  4293. {
  4294.         struct drm_i915_private *dev_priv = dev->dev_private;
  4295.         int ret, i;
  4296.  
  4297.         if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
  4298.                 return -EIO;
  4299.  
  4300.         if (dev_priv->ellc_size)
  4301.                 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
  4302.  
  4303.         if (IS_HASWELL(dev))
  4304.                 I915_WRITE(MI_PREDICATE_RESULT_2, IS_HSW_GT3(dev) ?
  4305.                            LOWER_SLICE_ENABLED : LOWER_SLICE_DISABLED);
  4306.  
  4307.         if (HAS_PCH_NOP(dev)) {
  4308.                 if (IS_IVYBRIDGE(dev)) {
  4309.                 u32 temp = I915_READ(GEN7_MSG_CTL);
  4310.                 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
  4311.                 I915_WRITE(GEN7_MSG_CTL, temp);
  4312.                 } else if (INTEL_INFO(dev)->gen >= 7) {
  4313.                         u32 temp = I915_READ(HSW_NDE_RSTWRN_OPT);
  4314.                         temp &= ~RESET_PCH_HANDSHAKE_ENABLE;
  4315.                         I915_WRITE(HSW_NDE_RSTWRN_OPT, temp);
  4316.                 }
  4317.         }
  4318.  
  4319.         i915_gem_init_swizzling(dev);
  4320.  
  4321.         ret = dev_priv->gt.init_rings(dev);
  4322.         if (ret)
  4323.                 return ret;
  4324.  
  4325.         for (i = 0; i < NUM_L3_SLICES(dev); i++)
  4326.                 i915_gem_l3_remap(&dev_priv->ring[RCS], i);
  4327.  
  4328.         /*
  4329.          * XXX: Contexts should only be initialized once. Doing a switch to the
  4330.          * default context switch however is something we'd like to do after
  4331.          * reset or thaw (the latter may not actually be necessary for HW, but
  4332.          * goes with our code better). Context switching requires rings (for
  4333.          * the do_switch), but before enabling PPGTT. So don't move this.
  4334.          */
  4335.         ret = i915_gem_context_enable(dev_priv);
  4336.         if (ret && ret != -EIO) {
  4337.                 DRM_ERROR("Context enable failed %d\n", ret);
  4338.                 i915_gem_cleanup_ringbuffer(dev);
  4339.  
  4340.                 return ret;
  4341.         }
  4342.  
  4343.         ret = i915_ppgtt_init_hw(dev);
  4344.         if (ret && ret != -EIO) {
  4345.                 DRM_ERROR("PPGTT enable failed %d\n", ret);
  4346.                 i915_gem_cleanup_ringbuffer(dev);
  4347.         }
  4348.  
  4349.         return ret;
  4350. }
  4351.  
  4352. int i915_gem_init(struct drm_device *dev)
  4353. {
  4354.         struct drm_i915_private *dev_priv = dev->dev_private;
  4355.         int ret;
  4356.  
  4357.         i915.enable_execlists = intel_sanitize_enable_execlists(dev,
  4358.                         i915.enable_execlists);
  4359.  
  4360.         mutex_lock(&dev->struct_mutex);
  4361.  
  4362.         if (IS_VALLEYVIEW(dev)) {
  4363.                 /* VLVA0 (potential hack), BIOS isn't actually waking us */
  4364.                 I915_WRITE(VLV_GTLC_WAKE_CTRL, VLV_GTLC_ALLOWWAKEREQ);
  4365.                 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) &
  4366.                               VLV_GTLC_ALLOWWAKEACK), 10))
  4367.                         DRM_DEBUG_DRIVER("allow wake ack timed out\n");
  4368.         }
  4369.  
  4370.         if (!i915.enable_execlists) {
  4371.                 dev_priv->gt.do_execbuf = i915_gem_ringbuffer_submission;
  4372.                 dev_priv->gt.init_rings = i915_gem_init_rings;
  4373.                 dev_priv->gt.cleanup_ring = intel_cleanup_ring_buffer;
  4374.                 dev_priv->gt.stop_ring = intel_stop_ring_buffer;
  4375.         } else {
  4376.                 dev_priv->gt.do_execbuf = intel_execlists_submission;
  4377.                 dev_priv->gt.init_rings = intel_logical_rings_init;
  4378.                 dev_priv->gt.cleanup_ring = intel_logical_ring_cleanup;
  4379.                 dev_priv->gt.stop_ring = intel_logical_ring_stop;
  4380.         }
  4381.  
  4382. //   ret = i915_gem_init_userptr(dev);
  4383. //   if (ret) {
  4384. //       mutex_unlock(&dev->struct_mutex);
  4385. //       return ret;
  4386. //   }
  4387.  
  4388.     i915_gem_init_global_gtt(dev);
  4389.  
  4390.         ret = i915_gem_context_init(dev);
  4391.         if (ret) {
  4392.                 mutex_unlock(&dev->struct_mutex);
  4393.                 return ret;
  4394.         }
  4395.  
  4396.         ret = i915_gem_init_hw(dev);
  4397.         if (ret == -EIO) {
  4398.                 /* Allow ring initialisation to fail by marking the GPU as
  4399.                  * wedged. But we only want to do this where the GPU is angry,
  4400.                  * for all other failure, such as an allocation failure, bail.
  4401.                  */
  4402.                 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
  4403.                 atomic_set_mask(I915_WEDGED, &dev_priv->gpu_error.reset_counter);
  4404.                 ret = 0;
  4405.         }
  4406.         mutex_unlock(&dev->struct_mutex);
  4407.  
  4408.                 return ret;
  4409. }
  4410.  
  4411. void
  4412. i915_gem_cleanup_ringbuffer(struct drm_device *dev)
  4413. {
  4414.         struct drm_i915_private *dev_priv = dev->dev_private;
  4415.         struct intel_engine_cs *ring;
  4416.         int i;
  4417.  
  4418.         for_each_ring(ring, dev_priv, i)
  4419.                 dev_priv->gt.cleanup_ring(ring);
  4420. }
  4421.  
  4422. static void
  4423. init_ring_lists(struct intel_engine_cs *ring)
  4424. {
  4425.     INIT_LIST_HEAD(&ring->active_list);
  4426.     INIT_LIST_HEAD(&ring->request_list);
  4427. }
  4428.  
  4429. void i915_init_vm(struct drm_i915_private *dev_priv,
  4430.                          struct i915_address_space *vm)
  4431. {
  4432.         if (!i915_is_ggtt(vm))
  4433.                 drm_mm_init(&vm->mm, vm->start, vm->total);
  4434.         vm->dev = dev_priv->dev;
  4435.         INIT_LIST_HEAD(&vm->active_list);
  4436.         INIT_LIST_HEAD(&vm->inactive_list);
  4437.         INIT_LIST_HEAD(&vm->global_link);
  4438.         list_add_tail(&vm->global_link, &dev_priv->vm_list);
  4439. }
  4440.  
  4441. void
  4442. i915_gem_load(struct drm_device *dev)
  4443. {
  4444.         struct drm_i915_private *dev_priv = dev->dev_private;
  4445.     int i;
  4446.  
  4447.         INIT_LIST_HEAD(&dev_priv->vm_list);
  4448.         i915_init_vm(dev_priv, &dev_priv->gtt.base);
  4449.  
  4450.         INIT_LIST_HEAD(&dev_priv->context_list);
  4451.         INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
  4452.         INIT_LIST_HEAD(&dev_priv->mm.bound_list);
  4453.     INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4454.     for (i = 0; i < I915_NUM_RINGS; i++)
  4455.         init_ring_lists(&dev_priv->ring[i]);
  4456.         for (i = 0; i < I915_MAX_NUM_FENCES; i++)
  4457.         INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
  4458.         INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
  4459.                           i915_gem_retire_work_handler);
  4460.         INIT_DELAYED_WORK(&dev_priv->mm.idle_work,
  4461.                           i915_gem_idle_work_handler);
  4462.         init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
  4463.  
  4464.     /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
  4465.         if (!drm_core_check_feature(dev, DRIVER_MODESET) && IS_GEN3(dev)) {
  4466.                 I915_WRITE(MI_ARB_STATE,
  4467.                            _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
  4468.     }
  4469.  
  4470.     dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
  4471.  
  4472.         /* Old X drivers will take 0-2 for front, back, depth buffers */
  4473.         if (!drm_core_check_feature(dev, DRIVER_MODESET))
  4474.                 dev_priv->fence_reg_start = 3;
  4475.  
  4476.         if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
  4477.                 dev_priv->num_fence_regs = 32;
  4478.         else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
  4479.         dev_priv->num_fence_regs = 16;
  4480.     else
  4481.         dev_priv->num_fence_regs = 8;
  4482.  
  4483.     /* Initialize fence registers to zero */
  4484.         INIT_LIST_HEAD(&dev_priv->mm.fence_list);
  4485.         i915_gem_restore_fences(dev);
  4486.  
  4487.     i915_gem_detect_bit_6_swizzle(dev);
  4488.  
  4489.     dev_priv->mm.interruptible = true;
  4490.  
  4491.         mutex_init(&dev_priv->fb_tracking.lock);
  4492. }
  4493.  
  4494. int i915_gem_open(struct drm_device *dev, struct drm_file *file)
  4495. {
  4496.         struct drm_i915_file_private *file_priv;
  4497.         int ret;
  4498.  
  4499.         DRM_DEBUG_DRIVER("\n");
  4500.  
  4501.         file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
  4502.         if (!file_priv)
  4503.                 return -ENOMEM;
  4504.  
  4505.         file->driver_priv = file_priv;
  4506.         file_priv->dev_priv = dev->dev_private;
  4507.         file_priv->file = file;
  4508.  
  4509.         spin_lock_init(&file_priv->mm.lock);
  4510.         INIT_LIST_HEAD(&file_priv->mm.request_list);
  4511. //      INIT_DELAYED_WORK(&file_priv->mm.idle_work,
  4512. //                        i915_gem_file_idle_work_handler);
  4513.  
  4514.         ret = i915_gem_context_open(dev, file);
  4515.         if (ret)
  4516.                 kfree(file_priv);
  4517.  
  4518.         return ret;
  4519. }
  4520.  
  4521. /**
  4522.  * i915_gem_track_fb - update frontbuffer tracking
  4523.  * old: current GEM buffer for the frontbuffer slots
  4524.  * new: new GEM buffer for the frontbuffer slots
  4525.  * frontbuffer_bits: bitmask of frontbuffer slots
  4526.  *
  4527.  * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
  4528.  * from @old and setting them in @new. Both @old and @new can be NULL.
  4529.  */
  4530. void i915_gem_track_fb(struct drm_i915_gem_object *old,
  4531.                        struct drm_i915_gem_object *new,
  4532.                        unsigned frontbuffer_bits)
  4533. {
  4534.         if (old) {
  4535.                 WARN_ON(!mutex_is_locked(&old->base.dev->struct_mutex));
  4536.                 WARN_ON(!(old->frontbuffer_bits & frontbuffer_bits));
  4537.                 old->frontbuffer_bits &= ~frontbuffer_bits;
  4538.         }
  4539.  
  4540.         if (new) {
  4541.                 WARN_ON(!mutex_is_locked(&new->base.dev->struct_mutex));
  4542.                 WARN_ON(new->frontbuffer_bits & frontbuffer_bits);
  4543.                 new->frontbuffer_bits |= frontbuffer_bits;
  4544.         }
  4545. }
  4546.  
  4547. static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
  4548. {
  4549.         if (!mutex_is_locked(mutex))
  4550.                 return false;
  4551.  
  4552. #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
  4553.         return mutex->owner == task;
  4554. #else
  4555.         /* Since UP may be pre-empted, we cannot assume that we own the lock */
  4556.         return false;
  4557. #endif
  4558. }
  4559.  
  4560. /* All the new VM stuff */
  4561. unsigned long i915_gem_obj_offset(struct drm_i915_gem_object *o,
  4562.                                   struct i915_address_space *vm)
  4563. {
  4564.         struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4565.         struct i915_vma *vma;
  4566.  
  4567.         WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4568.  
  4569.         list_for_each_entry(vma, &o->vma_list, vma_link) {
  4570.                 if (vma->vm == vm)
  4571.                         return vma->node.start;
  4572.  
  4573.         }
  4574.         WARN(1, "%s vma for this object not found.\n",
  4575.              i915_is_ggtt(vm) ? "global" : "ppgtt");
  4576.         return -1;
  4577. }
  4578.  
  4579. bool i915_gem_obj_bound(struct drm_i915_gem_object *o,
  4580.                         struct i915_address_space *vm)
  4581. {
  4582.         struct i915_vma *vma;
  4583.  
  4584.         list_for_each_entry(vma, &o->vma_list, vma_link)
  4585.                 if (vma->vm == vm && drm_mm_node_allocated(&vma->node))
  4586.                         return true;
  4587.  
  4588.         return false;
  4589. }
  4590.  
  4591. bool i915_gem_obj_bound_any(struct drm_i915_gem_object *o)
  4592. {
  4593.         struct i915_vma *vma;
  4594.  
  4595.         list_for_each_entry(vma, &o->vma_list, vma_link)
  4596.                 if (drm_mm_node_allocated(&vma->node))
  4597.                         return true;
  4598.  
  4599.         return false;
  4600. }
  4601.  
  4602. unsigned long i915_gem_obj_size(struct drm_i915_gem_object *o,
  4603.                                 struct i915_address_space *vm)
  4604. {
  4605.         struct drm_i915_private *dev_priv = o->base.dev->dev_private;
  4606.         struct i915_vma *vma;
  4607.  
  4608.         WARN_ON(vm == &dev_priv->mm.aliasing_ppgtt->base);
  4609.  
  4610.         BUG_ON(list_empty(&o->vma_list));
  4611.  
  4612.         list_for_each_entry(vma, &o->vma_list, vma_link)
  4613.                 if (vma->vm == vm)
  4614.                         return vma->node.size;
  4615.  
  4616.         return 0;
  4617. }
  4618.  
  4619.  
  4620.  
  4621. struct i915_vma *i915_gem_obj_to_ggtt(struct drm_i915_gem_object *obj)
  4622. {
  4623.         struct i915_vma *vma;
  4624.  
  4625.         vma = list_first_entry(&obj->vma_list, typeof(*vma), vma_link);
  4626.         if (vma->vm != i915_obj_to_ggtt(obj))
  4627.                 return NULL;
  4628.  
  4629.         return vma;
  4630. }
  4631.