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