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