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 | |
| 28 | #include "drmP.h" |
| 29 | #include "drm.h" |
| 30 | #include "i915_drm.h" |
| 31 | #include "i915_drv.h" |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 32 | #include "i915_trace.h" |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 33 | #include "intel_drv.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> |
Zhenyu Wang | f8f235e | 2010-08-27 11:08:57 +0800 | [diff] [blame] | 37 | #include <linux/intel-gtt.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 38 | |
Daniel Vetter | 0108a3e | 2010-08-07 11:01:21 +0100 | [diff] [blame] | 39 | static uint32_t i915_gem_get_gtt_alignment(struct drm_gem_object *obj); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 40 | |
| 41 | static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj, |
| 42 | bool pipelined); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 43 | static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj); |
| 44 | static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 45 | static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, |
| 46 | int write); |
| 47 | static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 48 | uint64_t offset, |
| 49 | uint64_t size); |
| 50 | static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj); |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 51 | static int i915_gem_object_wait_rendering(struct drm_gem_object *obj, |
| 52 | bool interruptible); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 53 | static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 54 | unsigned alignment, bool mappable); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 55 | static void i915_gem_clear_fence_reg(struct drm_gem_object *obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 56 | static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 57 | struct drm_i915_gem_pwrite *args, |
| 58 | struct drm_file *file_priv); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 59 | static void i915_gem_free_object_tail(struct drm_gem_object *obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 60 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 61 | static int |
| 62 | i915_gem_object_get_pages(struct drm_gem_object *obj, |
| 63 | gfp_t gfpmask); |
| 64 | |
| 65 | static void |
| 66 | i915_gem_object_put_pages(struct drm_gem_object *obj); |
| 67 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 68 | static int i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 69 | int nr_to_scan, |
| 70 | gfp_t gfp_mask); |
| 71 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 72 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 73 | /* some bookkeeping */ |
| 74 | static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv, |
| 75 | size_t size) |
| 76 | { |
| 77 | dev_priv->mm.object_count++; |
| 78 | dev_priv->mm.object_memory += size; |
| 79 | } |
| 80 | |
| 81 | static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv, |
| 82 | size_t size) |
| 83 | { |
| 84 | dev_priv->mm.object_count--; |
| 85 | dev_priv->mm.object_memory -= size; |
| 86 | } |
| 87 | |
| 88 | static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 89 | struct drm_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 90 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 91 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 92 | dev_priv->mm.gtt_count++; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 93 | dev_priv->mm.gtt_memory += obj->size; |
| 94 | if (obj_priv->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
| 95 | dev_priv->mm.mappable_gtt_used += |
| 96 | min_t(size_t, obj->size, |
| 97 | dev_priv->mm.gtt_mappable_end |
| 98 | - obj_priv->gtt_offset); |
| 99 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 103 | struct drm_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 104 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 105 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 106 | dev_priv->mm.gtt_count--; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 107 | dev_priv->mm.gtt_memory -= obj->size; |
| 108 | if (obj_priv->gtt_offset < dev_priv->mm.gtt_mappable_end) { |
| 109 | dev_priv->mm.mappable_gtt_used -= |
| 110 | min_t(size_t, obj->size, |
| 111 | dev_priv->mm.gtt_mappable_end |
| 112 | - obj_priv->gtt_offset); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Update the mappable working set counters. Call _only_ when there is a change |
| 118 | * in one of (pin|fault)_mappable and update *_mappable _before_ calling. |
| 119 | * @mappable: new state the changed mappable flag (either pin_ or fault_). |
| 120 | */ |
| 121 | static void |
| 122 | i915_gem_info_update_mappable(struct drm_i915_private *dev_priv, |
| 123 | struct drm_gem_object *obj, |
| 124 | bool mappable) |
| 125 | { |
| 126 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 127 | |
| 128 | if (mappable) { |
| 129 | if (obj_priv->pin_mappable && obj_priv->fault_mappable) |
| 130 | /* Combined state was already mappable. */ |
| 131 | return; |
| 132 | dev_priv->mm.gtt_mappable_count++; |
| 133 | dev_priv->mm.gtt_mappable_memory += obj->size; |
| 134 | } else { |
| 135 | if (obj_priv->pin_mappable || obj_priv->fault_mappable) |
| 136 | /* Combined state still mappable. */ |
| 137 | return; |
| 138 | dev_priv->mm.gtt_mappable_count--; |
| 139 | dev_priv->mm.gtt_mappable_memory -= obj->size; |
| 140 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 144 | struct drm_gem_object *obj, |
| 145 | bool mappable) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 146 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 147 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 148 | dev_priv->mm.pin_count++; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 149 | dev_priv->mm.pin_memory += obj->size; |
| 150 | if (mappable) { |
| 151 | obj_priv->pin_mappable = true; |
| 152 | i915_gem_info_update_mappable(dev_priv, obj, true); |
| 153 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv, |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 157 | struct drm_gem_object *obj) |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 158 | { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 159 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 160 | dev_priv->mm.pin_count--; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 161 | dev_priv->mm.pin_memory -= obj->size; |
| 162 | if (obj_priv->pin_mappable) { |
| 163 | obj_priv->pin_mappable = false; |
| 164 | i915_gem_info_update_mappable(dev_priv, obj, false); |
| 165 | } |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 166 | } |
| 167 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 168 | int |
| 169 | i915_gem_check_is_wedged(struct drm_device *dev) |
| 170 | { |
| 171 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 172 | struct completion *x = &dev_priv->error_completion; |
| 173 | unsigned long flags; |
| 174 | int ret; |
| 175 | |
| 176 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 177 | return 0; |
| 178 | |
| 179 | ret = wait_for_completion_interruptible(x); |
| 180 | if (ret) |
| 181 | return ret; |
| 182 | |
| 183 | /* Success, we reset the GPU! */ |
| 184 | if (!atomic_read(&dev_priv->mm.wedged)) |
| 185 | return 0; |
| 186 | |
| 187 | /* GPU is hung, bump the completion count to account for |
| 188 | * the token we just consumed so that we never hit zero and |
| 189 | * end up waiting upon a subsequent completion event that |
| 190 | * will never happen. |
| 191 | */ |
| 192 | spin_lock_irqsave(&x->wait.lock, flags); |
| 193 | x->done++; |
| 194 | spin_unlock_irqrestore(&x->wait.lock, flags); |
| 195 | return -EIO; |
| 196 | } |
| 197 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 198 | static int i915_mutex_lock_interruptible(struct drm_device *dev) |
| 199 | { |
| 200 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 201 | int ret; |
| 202 | |
| 203 | ret = i915_gem_check_is_wedged(dev); |
| 204 | if (ret) |
| 205 | return ret; |
| 206 | |
| 207 | ret = mutex_lock_interruptible(&dev->struct_mutex); |
| 208 | if (ret) |
| 209 | return ret; |
| 210 | |
| 211 | if (atomic_read(&dev_priv->mm.wedged)) { |
| 212 | mutex_unlock(&dev->struct_mutex); |
| 213 | return -EAGAIN; |
| 214 | } |
| 215 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 216 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 217 | return 0; |
| 218 | } |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 219 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 220 | static inline bool |
| 221 | i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv) |
| 222 | { |
| 223 | return obj_priv->gtt_space && |
| 224 | !obj_priv->active && |
| 225 | obj_priv->pin_count == 0; |
| 226 | } |
| 227 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 228 | int i915_gem_do_init(struct drm_device *dev, |
| 229 | unsigned long start, |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 230 | unsigned long mappable_end, |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 231 | unsigned long end) |
| 232 | { |
| 233 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 234 | |
| 235 | if (start >= end || |
| 236 | (start & (PAGE_SIZE - 1)) != 0 || |
| 237 | (end & (PAGE_SIZE - 1)) != 0) { |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | |
| 241 | drm_mm_init(&dev_priv->mm.gtt_space, start, |
| 242 | end - start); |
| 243 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 244 | dev_priv->mm.gtt_total = end - start; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 245 | dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start; |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 246 | dev_priv->mm.gtt_mappable_end = mappable_end; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 250 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 251 | int |
| 252 | i915_gem_init_ioctl(struct drm_device *dev, void *data, |
| 253 | struct drm_file *file_priv) |
| 254 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 255 | struct drm_i915_gem_init *args = data; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 256 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 257 | |
| 258 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | 5398463 | 2010-09-22 23:44:24 +0200 | [diff] [blame] | 259 | ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 260 | mutex_unlock(&dev->struct_mutex); |
| 261 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 262 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 263 | } |
| 264 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 265 | int |
| 266 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
| 267 | struct drm_file *file_priv) |
| 268 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 269 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 270 | struct drm_i915_gem_get_aperture *args = data; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 271 | |
| 272 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 273 | return -ENODEV; |
| 274 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 275 | mutex_lock(&dev->struct_mutex); |
| 276 | args->aper_size = dev_priv->mm.gtt_total; |
| 277 | args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory; |
| 278 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 279 | |
| 280 | return 0; |
| 281 | } |
| 282 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 283 | |
| 284 | /** |
| 285 | * Creates a new mm object and returns a handle to it. |
| 286 | */ |
| 287 | int |
| 288 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 289 | struct drm_file *file_priv) |
| 290 | { |
| 291 | struct drm_i915_gem_create *args = data; |
| 292 | struct drm_gem_object *obj; |
Pekka Paalanen | a1a2d1d | 2009-08-23 12:40:55 +0300 | [diff] [blame] | 293 | int ret; |
| 294 | u32 handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 295 | |
| 296 | args->size = roundup(args->size, PAGE_SIZE); |
| 297 | |
| 298 | /* Allocate the new object */ |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 299 | obj = i915_gem_alloc_object(dev, args->size); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 300 | if (obj == NULL) |
| 301 | return -ENOMEM; |
| 302 | |
| 303 | ret = drm_gem_handle_create(file_priv, obj, &handle); |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 304 | if (ret) { |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 305 | drm_gem_object_release(obj); |
| 306 | i915_gem_info_remove_obj(dev->dev_private, obj->size); |
| 307 | kfree(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 308 | return ret; |
Chris Wilson | 1dfd975 | 2010-09-06 14:44:14 +0100 | [diff] [blame] | 309 | } |
| 310 | |
Chris Wilson | 202f2fe | 2010-10-14 13:20:40 +0100 | [diff] [blame] | 311 | /* drop reference from allocate - handle holds it now */ |
| 312 | drm_gem_object_unreference(obj); |
| 313 | trace_i915_gem_object_create(obj); |
| 314 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 315 | args->handle = handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 316 | return 0; |
| 317 | } |
| 318 | |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 319 | static bool |
| 320 | i915_gem_object_cpu_accessible(struct drm_i915_gem_object *obj) |
| 321 | { |
| 322 | struct drm_device *dev = obj->base.dev; |
| 323 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 324 | |
| 325 | return obj->gtt_space == NULL || |
| 326 | obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end; |
| 327 | } |
| 328 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 329 | static inline int |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 330 | fast_shmem_read(struct page **pages, |
| 331 | loff_t page_base, int page_offset, |
| 332 | char __user *data, |
| 333 | int length) |
| 334 | { |
Chris Wilson | b5e4feb | 2010-10-14 13:47:43 +0100 | [diff] [blame] | 335 | char *vaddr; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 336 | int ret; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 337 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 338 | vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 339 | ret = __copy_to_user_inatomic(data, vaddr + page_offset, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 340 | kunmap_atomic(vaddr); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 341 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 342 | return ret; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 345 | static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj) |
| 346 | { |
| 347 | drm_i915_private_t *dev_priv = obj->dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 348 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 349 | |
| 350 | return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 && |
| 351 | obj_priv->tiling_mode != I915_TILING_NONE; |
| 352 | } |
| 353 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 354 | static inline void |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 355 | slow_shmem_copy(struct page *dst_page, |
| 356 | int dst_offset, |
| 357 | struct page *src_page, |
| 358 | int src_offset, |
| 359 | int length) |
| 360 | { |
| 361 | char *dst_vaddr, *src_vaddr; |
| 362 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 363 | dst_vaddr = kmap(dst_page); |
| 364 | src_vaddr = kmap(src_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 365 | |
| 366 | memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length); |
| 367 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 368 | kunmap(src_page); |
| 369 | kunmap(dst_page); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 370 | } |
| 371 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 372 | static inline void |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 373 | slow_shmem_bit17_copy(struct page *gpu_page, |
| 374 | int gpu_offset, |
| 375 | struct page *cpu_page, |
| 376 | int cpu_offset, |
| 377 | int length, |
| 378 | int is_read) |
| 379 | { |
| 380 | char *gpu_vaddr, *cpu_vaddr; |
| 381 | |
| 382 | /* Use the unswizzled path if this page isn't affected. */ |
| 383 | if ((page_to_phys(gpu_page) & (1 << 17)) == 0) { |
| 384 | if (is_read) |
| 385 | return slow_shmem_copy(cpu_page, cpu_offset, |
| 386 | gpu_page, gpu_offset, length); |
| 387 | else |
| 388 | return slow_shmem_copy(gpu_page, gpu_offset, |
| 389 | cpu_page, cpu_offset, length); |
| 390 | } |
| 391 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 392 | gpu_vaddr = kmap(gpu_page); |
| 393 | cpu_vaddr = kmap(cpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 394 | |
| 395 | /* Copy the data, XORing A6 with A17 (1). The user already knows he's |
| 396 | * XORing with the other bits (A9 for Y, A9 and A10 for X) |
| 397 | */ |
| 398 | while (length > 0) { |
| 399 | int cacheline_end = ALIGN(gpu_offset + 1, 64); |
| 400 | int this_length = min(cacheline_end - gpu_offset, length); |
| 401 | int swizzled_gpu_offset = gpu_offset ^ 64; |
| 402 | |
| 403 | if (is_read) { |
| 404 | memcpy(cpu_vaddr + cpu_offset, |
| 405 | gpu_vaddr + swizzled_gpu_offset, |
| 406 | this_length); |
| 407 | } else { |
| 408 | memcpy(gpu_vaddr + swizzled_gpu_offset, |
| 409 | cpu_vaddr + cpu_offset, |
| 410 | this_length); |
| 411 | } |
| 412 | cpu_offset += this_length; |
| 413 | gpu_offset += this_length; |
| 414 | length -= this_length; |
| 415 | } |
| 416 | |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 417 | kunmap(cpu_page); |
| 418 | kunmap(gpu_page); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 421 | /** |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 422 | * This is the fast shmem pread path, which attempts to copy_from_user directly |
| 423 | * from the backing pages of the object to the user's address space. On a |
| 424 | * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow(). |
| 425 | */ |
| 426 | static int |
| 427 | i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 428 | struct drm_i915_gem_pread *args, |
| 429 | struct drm_file *file_priv) |
| 430 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 431 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 432 | ssize_t remain; |
| 433 | loff_t offset, page_base; |
| 434 | char __user *user_data; |
| 435 | int page_offset, page_length; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 436 | |
| 437 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 438 | remain = args->size; |
| 439 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 440 | obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 441 | offset = args->offset; |
| 442 | |
| 443 | while (remain > 0) { |
| 444 | /* Operation in this page |
| 445 | * |
| 446 | * page_base = page offset within aperture |
| 447 | * page_offset = offset within page |
| 448 | * page_length = bytes to copy for this page |
| 449 | */ |
| 450 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 451 | page_offset = offset & (PAGE_SIZE-1); |
| 452 | page_length = remain; |
| 453 | if ((page_offset + remain) > PAGE_SIZE) |
| 454 | page_length = PAGE_SIZE - page_offset; |
| 455 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 456 | if (fast_shmem_read(obj_priv->pages, |
| 457 | page_base, page_offset, |
| 458 | user_data, page_length)) |
| 459 | return -EFAULT; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 460 | |
| 461 | remain -= page_length; |
| 462 | user_data += page_length; |
| 463 | offset += page_length; |
| 464 | } |
| 465 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 466 | return 0; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 467 | } |
| 468 | |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 469 | static int |
| 470 | i915_gem_object_get_pages_or_evict(struct drm_gem_object *obj) |
| 471 | { |
| 472 | int ret; |
| 473 | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 474 | ret = i915_gem_object_get_pages(obj, __GFP_NORETRY | __GFP_NOWARN); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 475 | |
| 476 | /* If we've insufficient memory to map in the pages, attempt |
| 477 | * to make some space by throwing out some old buffers. |
| 478 | */ |
| 479 | if (ret == -ENOMEM) { |
| 480 | struct drm_device *dev = obj->dev; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 481 | |
Daniel Vetter | 0108a3e | 2010-08-07 11:01:21 +0100 | [diff] [blame] | 482 | ret = i915_gem_evict_something(dev, obj->size, |
Daniel Vetter | a6e0aa4 | 2010-09-16 15:45:15 +0200 | [diff] [blame] | 483 | i915_gem_get_gtt_alignment(obj), |
| 484 | false); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 485 | if (ret) |
| 486 | return ret; |
| 487 | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 488 | ret = i915_gem_object_get_pages(obj, 0); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 489 | } |
| 490 | |
| 491 | return ret; |
| 492 | } |
| 493 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 494 | /** |
| 495 | * This is the fallback shmem pread path, which allocates temporary storage |
| 496 | * in kernel space to copy_to_user into outside of the struct_mutex, so we |
| 497 | * can copy out of the object's backing pages while holding the struct mutex |
| 498 | * and not take page faults. |
| 499 | */ |
| 500 | static int |
| 501 | i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 502 | struct drm_i915_gem_pread *args, |
| 503 | struct drm_file *file_priv) |
| 504 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 505 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 506 | struct mm_struct *mm = current->mm; |
| 507 | struct page **user_pages; |
| 508 | ssize_t remain; |
| 509 | loff_t offset, pinned_pages, i; |
| 510 | loff_t first_data_page, last_data_page, num_pages; |
| 511 | int shmem_page_index, shmem_page_offset; |
| 512 | int data_page_index, data_page_offset; |
| 513 | int page_length; |
| 514 | int ret; |
| 515 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 516 | int do_bit17_swizzling; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 517 | |
| 518 | remain = args->size; |
| 519 | |
| 520 | /* Pin the user pages containing the data. We can't fault while |
| 521 | * holding the struct mutex, yet we want to hold it while |
| 522 | * dereferencing the user data. |
| 523 | */ |
| 524 | first_data_page = data_ptr / PAGE_SIZE; |
| 525 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 526 | num_pages = last_data_page - first_data_page + 1; |
| 527 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 528 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 529 | if (user_pages == NULL) |
| 530 | return -ENOMEM; |
| 531 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 532 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 533 | down_read(&mm->mmap_sem); |
| 534 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
Eric Anholt | e5e9ecd | 2009-04-07 16:01:22 -0700 | [diff] [blame] | 535 | num_pages, 1, 0, user_pages, NULL); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 536 | up_read(&mm->mmap_sem); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 537 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 538 | if (pinned_pages < num_pages) { |
| 539 | ret = -EFAULT; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 540 | goto out; |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 541 | } |
| 542 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 543 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 544 | args->offset, |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 545 | args->size); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 546 | if (ret) |
| 547 | goto out; |
| 548 | |
| 549 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 550 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 551 | obj_priv = to_intel_bo(obj); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 552 | offset = args->offset; |
| 553 | |
| 554 | while (remain > 0) { |
| 555 | /* Operation in this page |
| 556 | * |
| 557 | * shmem_page_index = page number within shmem file |
| 558 | * shmem_page_offset = offset within page in shmem file |
| 559 | * data_page_index = page number in get_user_pages return |
| 560 | * data_page_offset = offset with data_page_index page. |
| 561 | * page_length = bytes to copy for this page |
| 562 | */ |
| 563 | shmem_page_index = offset / PAGE_SIZE; |
| 564 | shmem_page_offset = offset & ~PAGE_MASK; |
| 565 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 566 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 567 | |
| 568 | page_length = remain; |
| 569 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 570 | page_length = PAGE_SIZE - shmem_page_offset; |
| 571 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 572 | page_length = PAGE_SIZE - data_page_offset; |
| 573 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 574 | if (do_bit17_swizzling) { |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 575 | slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index], |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 576 | shmem_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 577 | user_pages[data_page_index], |
| 578 | data_page_offset, |
| 579 | page_length, |
| 580 | 1); |
| 581 | } else { |
| 582 | slow_shmem_copy(user_pages[data_page_index], |
| 583 | data_page_offset, |
| 584 | obj_priv->pages[shmem_page_index], |
| 585 | shmem_page_offset, |
| 586 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 587 | } |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 588 | |
| 589 | remain -= page_length; |
| 590 | data_ptr += page_length; |
| 591 | offset += page_length; |
| 592 | } |
| 593 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 594 | out: |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 595 | for (i = 0; i < pinned_pages; i++) { |
| 596 | SetPageDirty(user_pages[i]); |
| 597 | page_cache_release(user_pages[i]); |
| 598 | } |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 599 | drm_free_large(user_pages); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 600 | |
| 601 | return ret; |
| 602 | } |
| 603 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 604 | /** |
| 605 | * Reads data from the object referenced by handle. |
| 606 | * |
| 607 | * On error, the contents of *data are undefined. |
| 608 | */ |
| 609 | int |
| 610 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 611 | struct drm_file *file_priv) |
| 612 | { |
| 613 | struct drm_i915_gem_pread *args = data; |
| 614 | struct drm_gem_object *obj; |
| 615 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 616 | int ret = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 617 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 618 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 619 | if (ret) |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 620 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 621 | |
| 622 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 623 | if (obj == NULL) { |
| 624 | ret = -ENOENT; |
| 625 | goto unlock; |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 626 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 627 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 628 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 629 | /* Bounds check source. */ |
| 630 | if (args->offset > obj->size || args->size > obj->size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 631 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 632 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 633 | } |
| 634 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 635 | if (args->size == 0) |
| 636 | goto out; |
| 637 | |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 638 | if (!access_ok(VERIFY_WRITE, |
| 639 | (char __user *)(uintptr_t)args->data_ptr, |
| 640 | args->size)) { |
| 641 | ret = -EFAULT; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 642 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 643 | } |
| 644 | |
Chris Wilson | b5e4feb | 2010-10-14 13:47:43 +0100 | [diff] [blame] | 645 | ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr, |
| 646 | args->size); |
| 647 | if (ret) { |
| 648 | ret = -EFAULT; |
| 649 | goto out; |
| 650 | } |
| 651 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 652 | ret = i915_gem_object_get_pages_or_evict(obj); |
| 653 | if (ret) |
| 654 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 655 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 656 | ret = i915_gem_object_set_cpu_read_domain_range(obj, |
| 657 | args->offset, |
| 658 | args->size); |
| 659 | if (ret) |
| 660 | goto out_put; |
| 661 | |
| 662 | ret = -EFAULT; |
| 663 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 664 | ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv); |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 665 | if (ret == -EFAULT) |
| 666 | ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 667 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 668 | out_put: |
| 669 | i915_gem_object_put_pages(obj); |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 670 | out: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 671 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 672 | unlock: |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 673 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 674 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 675 | } |
| 676 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 677 | /* This is the fast write path which cannot handle |
| 678 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 679 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 680 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 681 | static inline int |
| 682 | fast_user_write(struct io_mapping *mapping, |
| 683 | loff_t page_base, int page_offset, |
| 684 | char __user *user_data, |
| 685 | int length) |
| 686 | { |
| 687 | char *vaddr_atomic; |
| 688 | unsigned long unwritten; |
| 689 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 690 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 691 | unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset, |
| 692 | user_data, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 693 | io_mapping_unmap_atomic(vaddr_atomic); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 694 | return unwritten; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 695 | } |
| 696 | |
| 697 | /* Here's the write path which can sleep for |
| 698 | * page faults |
| 699 | */ |
| 700 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 701 | static inline void |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 702 | slow_kernel_write(struct io_mapping *mapping, |
| 703 | loff_t gtt_base, int gtt_offset, |
| 704 | struct page *user_page, int user_offset, |
| 705 | int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 706 | { |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 707 | char __iomem *dst_vaddr; |
| 708 | char *src_vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 709 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 710 | dst_vaddr = io_mapping_map_wc(mapping, gtt_base); |
| 711 | src_vaddr = kmap(user_page); |
| 712 | |
| 713 | memcpy_toio(dst_vaddr + gtt_offset, |
| 714 | src_vaddr + user_offset, |
| 715 | length); |
| 716 | |
| 717 | kunmap(user_page); |
| 718 | io_mapping_unmap(dst_vaddr); |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 719 | } |
| 720 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 721 | static inline int |
| 722 | fast_shmem_write(struct page **pages, |
| 723 | loff_t page_base, int page_offset, |
| 724 | char __user *data, |
| 725 | int length) |
| 726 | { |
Chris Wilson | b5e4feb | 2010-10-14 13:47:43 +0100 | [diff] [blame] | 727 | char *vaddr; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 728 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 729 | |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 730 | vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT]); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 731 | ret = __copy_from_user_inatomic(vaddr + page_offset, data, length); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 732 | kunmap_atomic(vaddr); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 733 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 734 | return ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 735 | } |
| 736 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 737 | /** |
| 738 | * This is the fast pwrite path, where we copy the data directly from the |
| 739 | * user into the GTT, uncached. |
| 740 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 741 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 742 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 743 | struct drm_i915_gem_pwrite *args, |
| 744 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 745 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 746 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 747 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 748 | ssize_t remain; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 749 | loff_t offset, page_base; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 750 | char __user *user_data; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 751 | int page_offset, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 752 | |
| 753 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 754 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 755 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 756 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 757 | offset = obj_priv->gtt_offset + args->offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 758 | |
| 759 | while (remain > 0) { |
| 760 | /* Operation in this page |
| 761 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 762 | * page_base = page offset within aperture |
| 763 | * page_offset = offset within page |
| 764 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 765 | */ |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 766 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 767 | page_offset = offset & (PAGE_SIZE-1); |
| 768 | page_length = remain; |
| 769 | if ((page_offset + remain) > PAGE_SIZE) |
| 770 | page_length = PAGE_SIZE - page_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 771 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 772 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 773 | * source page isn't available. Return the error and we'll |
| 774 | * retry in the slow path. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 775 | */ |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 776 | if (fast_user_write(dev_priv->mm.gtt_mapping, page_base, |
| 777 | page_offset, user_data, page_length)) |
| 778 | |
| 779 | return -EFAULT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 780 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 781 | remain -= page_length; |
| 782 | user_data += page_length; |
| 783 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 784 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 785 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 786 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 789 | /** |
| 790 | * This is the fallback GTT pwrite path, which uses get_user_pages to pin |
| 791 | * the memory and maps it using kmap_atomic for copying. |
| 792 | * |
| 793 | * This code resulted in x11perf -rgb10text consuming about 10% more CPU |
| 794 | * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit). |
| 795 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 796 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 797 | i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 798 | struct drm_i915_gem_pwrite *args, |
| 799 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 800 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 801 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 802 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 803 | ssize_t remain; |
| 804 | loff_t gtt_page_base, offset; |
| 805 | loff_t first_data_page, last_data_page, num_pages; |
| 806 | loff_t pinned_pages, i; |
| 807 | struct page **user_pages; |
| 808 | struct mm_struct *mm = current->mm; |
| 809 | int gtt_page_offset, data_page_offset, data_page_index, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 810 | int ret; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 811 | uint64_t data_ptr = args->data_ptr; |
| 812 | |
| 813 | remain = args->size; |
| 814 | |
| 815 | /* Pin the user pages containing the data. We can't fault while |
| 816 | * holding the struct mutex, and all of the pwrite implementations |
| 817 | * want to hold it while dereferencing the user data. |
| 818 | */ |
| 819 | first_data_page = data_ptr / PAGE_SIZE; |
| 820 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 821 | num_pages = last_data_page - first_data_page + 1; |
| 822 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 823 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 824 | if (user_pages == NULL) |
| 825 | return -ENOMEM; |
| 826 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 827 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 828 | down_read(&mm->mmap_sem); |
| 829 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 830 | num_pages, 0, 0, user_pages, NULL); |
| 831 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 832 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 833 | if (pinned_pages < num_pages) { |
| 834 | ret = -EFAULT; |
| 835 | goto out_unpin_pages; |
| 836 | } |
| 837 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 838 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 839 | if (ret) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 840 | goto out_unpin_pages; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 841 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 842 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 843 | offset = obj_priv->gtt_offset + args->offset; |
| 844 | |
| 845 | while (remain > 0) { |
| 846 | /* Operation in this page |
| 847 | * |
| 848 | * gtt_page_base = page offset within aperture |
| 849 | * gtt_page_offset = offset within page in aperture |
| 850 | * data_page_index = page number in get_user_pages return |
| 851 | * data_page_offset = offset with data_page_index page. |
| 852 | * page_length = bytes to copy for this page |
| 853 | */ |
| 854 | gtt_page_base = offset & PAGE_MASK; |
| 855 | gtt_page_offset = offset & ~PAGE_MASK; |
| 856 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 857 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 858 | |
| 859 | page_length = remain; |
| 860 | if ((gtt_page_offset + page_length) > PAGE_SIZE) |
| 861 | page_length = PAGE_SIZE - gtt_page_offset; |
| 862 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 863 | page_length = PAGE_SIZE - data_page_offset; |
| 864 | |
Chris Wilson | ab34c22 | 2010-05-27 14:15:35 +0100 | [diff] [blame] | 865 | slow_kernel_write(dev_priv->mm.gtt_mapping, |
| 866 | gtt_page_base, gtt_page_offset, |
| 867 | user_pages[data_page_index], |
| 868 | data_page_offset, |
| 869 | page_length); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 870 | |
| 871 | remain -= page_length; |
| 872 | offset += page_length; |
| 873 | data_ptr += page_length; |
| 874 | } |
| 875 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 876 | out_unpin_pages: |
| 877 | for (i = 0; i < pinned_pages; i++) |
| 878 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 879 | drm_free_large(user_pages); |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 880 | |
| 881 | return ret; |
| 882 | } |
| 883 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 884 | /** |
| 885 | * This is the fast shmem pwrite path, which attempts to directly |
| 886 | * copy_from_user into the kmapped pages backing the object. |
| 887 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 888 | static int |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 889 | i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 890 | struct drm_i915_gem_pwrite *args, |
| 891 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 892 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 893 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 894 | ssize_t remain; |
| 895 | loff_t offset, page_base; |
| 896 | char __user *user_data; |
| 897 | int page_offset, page_length; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 898 | |
| 899 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 900 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 901 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 902 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 903 | offset = args->offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 904 | obj_priv->dirty = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 905 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 906 | while (remain > 0) { |
| 907 | /* Operation in this page |
| 908 | * |
| 909 | * page_base = page offset within aperture |
| 910 | * page_offset = offset within page |
| 911 | * page_length = bytes to copy for this page |
| 912 | */ |
| 913 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 914 | page_offset = offset & (PAGE_SIZE-1); |
| 915 | page_length = remain; |
| 916 | if ((page_offset + remain) > PAGE_SIZE) |
| 917 | page_length = PAGE_SIZE - page_offset; |
| 918 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 919 | if (fast_shmem_write(obj_priv->pages, |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 920 | page_base, page_offset, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 921 | user_data, page_length)) |
| 922 | return -EFAULT; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 923 | |
| 924 | remain -= page_length; |
| 925 | user_data += page_length; |
| 926 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 927 | } |
| 928 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 929 | return 0; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 930 | } |
| 931 | |
| 932 | /** |
| 933 | * This is the fallback shmem pwrite path, which uses get_user_pages to pin |
| 934 | * the memory and maps it using kmap_atomic for copying. |
| 935 | * |
| 936 | * This avoids taking mmap_sem for faulting on the user's address while the |
| 937 | * struct_mutex is held. |
| 938 | */ |
| 939 | static int |
| 940 | i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 941 | struct drm_i915_gem_pwrite *args, |
| 942 | struct drm_file *file_priv) |
| 943 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 944 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 945 | struct mm_struct *mm = current->mm; |
| 946 | struct page **user_pages; |
| 947 | ssize_t remain; |
| 948 | loff_t offset, pinned_pages, i; |
| 949 | loff_t first_data_page, last_data_page, num_pages; |
| 950 | int shmem_page_index, shmem_page_offset; |
| 951 | int data_page_index, data_page_offset; |
| 952 | int page_length; |
| 953 | int ret; |
| 954 | uint64_t data_ptr = args->data_ptr; |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 955 | int do_bit17_swizzling; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 956 | |
| 957 | remain = args->size; |
| 958 | |
| 959 | /* Pin the user pages containing the data. We can't fault while |
| 960 | * holding the struct mutex, and all of the pwrite implementations |
| 961 | * want to hold it while dereferencing the user data. |
| 962 | */ |
| 963 | first_data_page = data_ptr / PAGE_SIZE; |
| 964 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 965 | num_pages = last_data_page - first_data_page + 1; |
| 966 | |
Chris Wilson | 4f27b75 | 2010-10-14 15:26:45 +0100 | [diff] [blame] | 967 | user_pages = drm_malloc_ab(num_pages, sizeof(struct page *)); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 968 | if (user_pages == NULL) |
| 969 | return -ENOMEM; |
| 970 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 971 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 972 | down_read(&mm->mmap_sem); |
| 973 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 974 | num_pages, 0, 0, user_pages, NULL); |
| 975 | up_read(&mm->mmap_sem); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 976 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 977 | if (pinned_pages < num_pages) { |
| 978 | ret = -EFAULT; |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 979 | goto out; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 982 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 983 | if (ret) |
| 984 | goto out; |
| 985 | |
| 986 | do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 987 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 988 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 989 | offset = args->offset; |
| 990 | obj_priv->dirty = 1; |
| 991 | |
| 992 | while (remain > 0) { |
| 993 | /* Operation in this page |
| 994 | * |
| 995 | * shmem_page_index = page number within shmem file |
| 996 | * shmem_page_offset = offset within page in shmem file |
| 997 | * data_page_index = page number in get_user_pages return |
| 998 | * data_page_offset = offset with data_page_index page. |
| 999 | * page_length = bytes to copy for this page |
| 1000 | */ |
| 1001 | shmem_page_index = offset / PAGE_SIZE; |
| 1002 | shmem_page_offset = offset & ~PAGE_MASK; |
| 1003 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 1004 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 1005 | |
| 1006 | page_length = remain; |
| 1007 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 1008 | page_length = PAGE_SIZE - shmem_page_offset; |
| 1009 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 1010 | page_length = PAGE_SIZE - data_page_offset; |
| 1011 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1012 | if (do_bit17_swizzling) { |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 1013 | slow_shmem_bit17_copy(obj_priv->pages[shmem_page_index], |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1014 | shmem_page_offset, |
| 1015 | user_pages[data_page_index], |
| 1016 | data_page_offset, |
Chris Wilson | 99a03df | 2010-05-27 14:15:34 +0100 | [diff] [blame] | 1017 | page_length, |
| 1018 | 0); |
| 1019 | } else { |
| 1020 | slow_shmem_copy(obj_priv->pages[shmem_page_index], |
| 1021 | shmem_page_offset, |
| 1022 | user_pages[data_page_index], |
| 1023 | data_page_offset, |
| 1024 | page_length); |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1025 | } |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1026 | |
| 1027 | remain -= page_length; |
| 1028 | data_ptr += page_length; |
| 1029 | offset += page_length; |
| 1030 | } |
| 1031 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1032 | out: |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1033 | for (i = 0; i < pinned_pages; i++) |
| 1034 | page_cache_release(user_pages[i]); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 1035 | drm_free_large(user_pages); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1036 | |
| 1037 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
| 1040 | /** |
| 1041 | * Writes data to the object referenced by handle. |
| 1042 | * |
| 1043 | * On error, the contents of the buffer that were to be modified are undefined. |
| 1044 | */ |
| 1045 | int |
| 1046 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1047 | struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1048 | { |
| 1049 | struct drm_i915_gem_pwrite *args = data; |
| 1050 | struct drm_gem_object *obj; |
| 1051 | struct drm_i915_gem_object *obj_priv; |
| 1052 | int ret = 0; |
| 1053 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1054 | ret = i915_mutex_lock_interruptible(dev); |
| 1055 | if (ret) |
| 1056 | return ret; |
| 1057 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1058 | obj = drm_gem_object_lookup(dev, file, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1059 | if (obj == NULL) { |
| 1060 | ret = -ENOENT; |
| 1061 | goto unlock; |
| 1062 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1063 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1064 | |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1065 | |
Chris Wilson | 7dcd249 | 2010-09-26 20:21:44 +0100 | [diff] [blame] | 1066 | /* Bounds check destination. */ |
| 1067 | if (args->offset > obj->size || args->size > obj->size - args->offset) { |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1068 | ret = -EINVAL; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1069 | goto out; |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1070 | } |
| 1071 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1072 | if (args->size == 0) |
| 1073 | goto out; |
| 1074 | |
Chris Wilson | ce9d419 | 2010-09-26 20:50:05 +0100 | [diff] [blame] | 1075 | if (!access_ok(VERIFY_READ, |
| 1076 | (char __user *)(uintptr_t)args->data_ptr, |
| 1077 | args->size)) { |
| 1078 | ret = -EFAULT; |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1079 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1080 | } |
| 1081 | |
Chris Wilson | b5e4feb | 2010-10-14 13:47:43 +0100 | [diff] [blame] | 1082 | ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr, |
| 1083 | args->size); |
| 1084 | if (ret) { |
| 1085 | ret = -EFAULT; |
| 1086 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 1090 | * it would end up going through the fenced access, and we'll get |
| 1091 | * different detiling behavior between reading and writing. |
| 1092 | * pread/pwrite currently are reading and writing from the CPU |
| 1093 | * perspective, requiring manual detiling by the client. |
| 1094 | */ |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1095 | if (obj_priv->phys_obj) |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1096 | ret = i915_gem_phys_pwrite(dev, obj, args, file); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 1097 | else if (obj_priv->tiling_mode == I915_TILING_NONE && |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1098 | obj_priv->gtt_space && |
Chris Wilson | 9b8c4a0 | 2010-05-27 14:21:01 +0100 | [diff] [blame] | 1099 | obj->write_domain != I915_GEM_DOMAIN_CPU) { |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 1100 | ret = i915_gem_object_pin(obj, 0, true); |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1101 | if (ret) |
| 1102 | goto out; |
| 1103 | |
| 1104 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 1105 | if (ret) |
| 1106 | goto out_unpin; |
| 1107 | |
| 1108 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file); |
| 1109 | if (ret == -EFAULT) |
| 1110 | ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file); |
| 1111 | |
| 1112 | out_unpin: |
| 1113 | i915_gem_object_unpin(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1114 | } else { |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1115 | ret = i915_gem_object_get_pages_or_evict(obj); |
| 1116 | if (ret) |
| 1117 | goto out; |
| 1118 | |
| 1119 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 1120 | if (ret) |
| 1121 | goto out_put; |
| 1122 | |
| 1123 | ret = -EFAULT; |
| 1124 | if (!i915_gem_object_needs_bit17_swizzle(obj)) |
| 1125 | ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file); |
| 1126 | if (ret == -EFAULT) |
| 1127 | ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file); |
| 1128 | |
| 1129 | out_put: |
| 1130 | i915_gem_object_put_pages(obj); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 1131 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1132 | |
Chris Wilson | 35b62a8 | 2010-09-26 20:23:38 +0100 | [diff] [blame] | 1133 | out: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1134 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1135 | unlock: |
Chris Wilson | fbd5a26 | 2010-10-14 15:03:58 +0100 | [diff] [blame] | 1136 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1137 | return ret; |
| 1138 | } |
| 1139 | |
| 1140 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1141 | * Called when user space prepares to use an object with the CPU, either |
| 1142 | * through the mmap ioctl's mapping or a GTT mapping. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1143 | */ |
| 1144 | int |
| 1145 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 1146 | struct drm_file *file_priv) |
| 1147 | { |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1148 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1149 | struct drm_i915_gem_set_domain *args = data; |
| 1150 | struct drm_gem_object *obj; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1151 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1152 | uint32_t read_domains = args->read_domains; |
| 1153 | uint32_t write_domain = args->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1154 | int ret; |
| 1155 | |
| 1156 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1157 | return -ENODEV; |
| 1158 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1159 | /* Only handle setting domains to types used by the CPU. */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1160 | if (write_domain & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1161 | return -EINVAL; |
| 1162 | |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 1163 | if (read_domains & I915_GEM_GPU_DOMAINS) |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1164 | return -EINVAL; |
| 1165 | |
| 1166 | /* Having something in the write domain implies it's in the read |
| 1167 | * domain, and only that read domain. Enforce that in the request. |
| 1168 | */ |
| 1169 | if (write_domain != 0 && read_domains != write_domain) |
| 1170 | return -EINVAL; |
| 1171 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1172 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1173 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1174 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1175 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1176 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1177 | if (obj == NULL) { |
| 1178 | ret = -ENOENT; |
| 1179 | goto unlock; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1180 | } |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1181 | obj_priv = to_intel_bo(obj); |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 1182 | |
| 1183 | intel_mark_busy(dev, obj); |
| 1184 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1185 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
| 1186 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1187 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1188 | /* Update the LRU on the fence for the CPU access that's |
| 1189 | * about to occur. |
| 1190 | */ |
| 1191 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1192 | struct drm_i915_fence_reg *reg = |
| 1193 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 1194 | list_move_tail(®->lru_list, |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1195 | &dev_priv->mm.fence_list); |
| 1196 | } |
| 1197 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 1198 | /* Silently promote "you're not bound, there was nothing to do" |
| 1199 | * to success, since the client was just asking us to |
| 1200 | * make sure everything was done. |
| 1201 | */ |
| 1202 | if (ret == -EINVAL) |
| 1203 | ret = 0; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1204 | } else { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1205 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 1206 | } |
| 1207 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1208 | /* Maintain LRU order of "inactive" objects */ |
| 1209 | if (ret == 0 && i915_gem_object_is_inactive(obj_priv)) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1210 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1211 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1212 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1213 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1214 | mutex_unlock(&dev->struct_mutex); |
| 1215 | return ret; |
| 1216 | } |
| 1217 | |
| 1218 | /** |
| 1219 | * Called when user space has done writes to this buffer |
| 1220 | */ |
| 1221 | int |
| 1222 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
| 1223 | struct drm_file *file_priv) |
| 1224 | { |
| 1225 | struct drm_i915_gem_sw_finish *args = data; |
| 1226 | struct drm_gem_object *obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1227 | int ret = 0; |
| 1228 | |
| 1229 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1230 | return -ENODEV; |
| 1231 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1232 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1233 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1234 | return ret; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1235 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1236 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1237 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1238 | ret = -ENOENT; |
| 1239 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1240 | } |
| 1241 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1242 | /* Pinned buffers may be scanout, so flush the cache */ |
Chris Wilson | 3d2a812 | 2010-09-29 11:39:53 +0100 | [diff] [blame] | 1243 | if (to_intel_bo(obj)->pin_count) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1244 | i915_gem_object_flush_cpu_write_domain(obj); |
| 1245 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1246 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1247 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1248 | mutex_unlock(&dev->struct_mutex); |
| 1249 | return ret; |
| 1250 | } |
| 1251 | |
| 1252 | /** |
| 1253 | * Maps the contents of an object, returning the address it is mapped |
| 1254 | * into. |
| 1255 | * |
| 1256 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 1257 | * imply a ref on the object itself. |
| 1258 | */ |
| 1259 | int |
| 1260 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 1261 | struct drm_file *file_priv) |
| 1262 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1263 | struct drm_i915_private *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1264 | struct drm_i915_gem_mmap *args = data; |
| 1265 | struct drm_gem_object *obj; |
| 1266 | loff_t offset; |
| 1267 | unsigned long addr; |
| 1268 | |
| 1269 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1270 | return -ENODEV; |
| 1271 | |
| 1272 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1273 | if (obj == NULL) |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 1274 | return -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1275 | |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1276 | if (obj->size > dev_priv->mm.gtt_mappable_end) { |
| 1277 | drm_gem_object_unreference_unlocked(obj); |
| 1278 | return -E2BIG; |
| 1279 | } |
| 1280 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1281 | offset = args->offset; |
| 1282 | |
| 1283 | down_write(¤t->mm->mmap_sem); |
| 1284 | addr = do_mmap(obj->filp, 0, args->size, |
| 1285 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 1286 | args->offset); |
| 1287 | up_write(¤t->mm->mmap_sem); |
Luca Barbieri | bc9025b | 2010-02-09 05:49:12 +0000 | [diff] [blame] | 1288 | drm_gem_object_unreference_unlocked(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1289 | if (IS_ERR((void *)addr)) |
| 1290 | return addr; |
| 1291 | |
| 1292 | args->addr_ptr = (uint64_t) addr; |
| 1293 | |
| 1294 | return 0; |
| 1295 | } |
| 1296 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1297 | /** |
| 1298 | * i915_gem_fault - fault a page into the GTT |
| 1299 | * vma: VMA in question |
| 1300 | * vmf: fault info |
| 1301 | * |
| 1302 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1303 | * from userspace. The fault handler takes care of binding the object to |
| 1304 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1305 | * only if needed based on whether the old reg is still valid or the object |
| 1306 | * is tiled) and inserting a new PTE into the faulting process. |
| 1307 | * |
| 1308 | * Note that the faulting process may involve evicting existing objects |
| 1309 | * from the GTT and/or fence registers to make room. So performance may |
| 1310 | * suffer if the GTT working set is large or there are few fence registers |
| 1311 | * left. |
| 1312 | */ |
| 1313 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1314 | { |
| 1315 | struct drm_gem_object *obj = vma->vm_private_data; |
| 1316 | struct drm_device *dev = obj->dev; |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1317 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1318 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1319 | pgoff_t page_offset; |
| 1320 | unsigned long pfn; |
| 1321 | int ret = 0; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1322 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1323 | |
| 1324 | /* We don't use vmf->pgoff since that has the fake offset */ |
| 1325 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> |
| 1326 | PAGE_SHIFT; |
| 1327 | |
| 1328 | /* Now bind it into the GTT if needed */ |
| 1329 | mutex_lock(&dev->struct_mutex); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1330 | BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable); |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 1331 | if (!i915_gem_object_cpu_accessible(obj_priv)) |
| 1332 | i915_gem_object_unbind(obj); |
| 1333 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1334 | if (!obj_priv->gtt_space) { |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 1335 | ret = i915_gem_object_bind_to_gtt(obj, 0, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1336 | if (ret) |
| 1337 | goto unlock; |
Kristian Høgsberg | 07f4f3e | 2009-05-27 14:37:28 -0400 | [diff] [blame] | 1338 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1339 | ret = i915_gem_object_set_to_gtt_domain(obj, write); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1340 | if (ret) |
| 1341 | goto unlock; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1342 | } |
| 1343 | |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1344 | if (!obj_priv->fault_mappable) { |
| 1345 | obj_priv->fault_mappable = true; |
| 1346 | i915_gem_info_update_mappable(dev_priv, obj, true); |
| 1347 | } |
| 1348 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1349 | /* Need a new fence register? */ |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 1350 | if (obj_priv->tiling_mode != I915_TILING_NONE) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 1351 | ret = i915_gem_object_get_fence_reg(obj, true); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1352 | if (ret) |
| 1353 | goto unlock; |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1354 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1355 | |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1356 | if (i915_gem_object_is_inactive(obj_priv)) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1357 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Chris Wilson | 7d1c480 | 2010-08-07 21:45:03 +0100 | [diff] [blame] | 1358 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1359 | pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) + |
| 1360 | page_offset; |
| 1361 | |
| 1362 | /* Finally, remap it using the new GTT offset */ |
| 1363 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1364 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1365 | mutex_unlock(&dev->struct_mutex); |
| 1366 | |
| 1367 | switch (ret) { |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1368 | case 0: |
| 1369 | case -ERESTARTSYS: |
| 1370 | return VM_FAULT_NOPAGE; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1371 | case -ENOMEM: |
| 1372 | case -EAGAIN: |
| 1373 | return VM_FAULT_OOM; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1374 | default: |
Chris Wilson | c715089 | 2009-09-23 00:43:56 +0100 | [diff] [blame] | 1375 | return VM_FAULT_SIGBUS; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1376 | } |
| 1377 | } |
| 1378 | |
| 1379 | /** |
| 1380 | * i915_gem_create_mmap_offset - create a fake mmap offset for an object |
| 1381 | * @obj: obj in question |
| 1382 | * |
| 1383 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
| 1384 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
| 1385 | * up the object based on the offset and sets up the various memory mapping |
| 1386 | * structures. |
| 1387 | * |
| 1388 | * This routine allocates and attaches a fake offset for @obj. |
| 1389 | */ |
| 1390 | static int |
| 1391 | i915_gem_create_mmap_offset(struct drm_gem_object *obj) |
| 1392 | { |
| 1393 | struct drm_device *dev = obj->dev; |
| 1394 | struct drm_gem_mm *mm = dev->mm_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1395 | struct drm_map_list *list; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1396 | struct drm_local_map *map; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1397 | int ret = 0; |
| 1398 | |
| 1399 | /* Set the object up for mmap'ing */ |
| 1400 | list = &obj->map_list; |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1401 | list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1402 | if (!list->map) |
| 1403 | return -ENOMEM; |
| 1404 | |
| 1405 | map = list->map; |
| 1406 | map->type = _DRM_GEM; |
| 1407 | map->size = obj->size; |
| 1408 | map->handle = obj; |
| 1409 | |
| 1410 | /* Get a DRM GEM mmap offset allocated... */ |
| 1411 | list->file_offset_node = drm_mm_search_free(&mm->offset_manager, |
| 1412 | obj->size / PAGE_SIZE, 0, 0); |
| 1413 | if (!list->file_offset_node) { |
| 1414 | DRM_ERROR("failed to allocate offset for bo %d\n", obj->name); |
Chris Wilson | 9e0ae534 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1415 | ret = -ENOSPC; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1416 | goto out_free_list; |
| 1417 | } |
| 1418 | |
| 1419 | list->file_offset_node = drm_mm_get_block(list->file_offset_node, |
| 1420 | obj->size / PAGE_SIZE, 0); |
| 1421 | if (!list->file_offset_node) { |
| 1422 | ret = -ENOMEM; |
| 1423 | goto out_free_list; |
| 1424 | } |
| 1425 | |
| 1426 | list->hash.key = list->file_offset_node->start; |
Chris Wilson | 9e0ae534 | 2010-09-21 15:05:24 +0100 | [diff] [blame] | 1427 | ret = drm_ht_insert_item(&mm->offset_hash, &list->hash); |
| 1428 | if (ret) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1429 | DRM_ERROR("failed to add to map hash\n"); |
| 1430 | goto out_free_mm; |
| 1431 | } |
| 1432 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1433 | return 0; |
| 1434 | |
| 1435 | out_free_mm: |
| 1436 | drm_mm_put_block(list->file_offset_node); |
| 1437 | out_free_list: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 1438 | kfree(list->map); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1439 | list->map = NULL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1440 | |
| 1441 | return ret; |
| 1442 | } |
| 1443 | |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1444 | /** |
| 1445 | * i915_gem_release_mmap - remove physical page mappings |
| 1446 | * @obj: obj in question |
| 1447 | * |
André Goddard Rosa | af901ca | 2009-11-14 13:09:05 -0200 | [diff] [blame] | 1448 | * Preserve the reservation of the mmapping with the DRM core code, but |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1449 | * relinquish ownership of the pages back to the system. |
| 1450 | * |
| 1451 | * It is vital that we remove the page mapping if we have mapped a tiled |
| 1452 | * object through the GTT and then lose the fence register due to |
| 1453 | * resource pressure. Similarly if the object has been moved out of the |
| 1454 | * aperture, than pages mapped into userspace must be revoked. Removing the |
| 1455 | * mapping will then trigger a page fault on the next user access, allowing |
| 1456 | * fixup by i915_gem_fault(). |
| 1457 | */ |
Eric Anholt | d05ca30 | 2009-07-10 13:02:26 -0700 | [diff] [blame] | 1458 | void |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1459 | i915_gem_release_mmap(struct drm_gem_object *obj) |
| 1460 | { |
| 1461 | struct drm_device *dev = obj->dev; |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1462 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1463 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1464 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1465 | if (unlikely(obj->map_list.map && dev->dev_mapping)) |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1466 | unmap_mapping_range(dev->dev_mapping, |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1467 | (loff_t)obj->map_list.hash.key<<PAGE_SHIFT, |
| 1468 | obj->size, 1); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 1469 | |
| 1470 | if (obj_priv->fault_mappable) { |
| 1471 | obj_priv->fault_mappable = false; |
| 1472 | i915_gem_info_update_mappable(dev_priv, obj, false); |
| 1473 | } |
Chris Wilson | 901782b | 2009-07-10 08:18:50 +0100 | [diff] [blame] | 1474 | } |
| 1475 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1476 | static void |
| 1477 | i915_gem_free_mmap_offset(struct drm_gem_object *obj) |
| 1478 | { |
| 1479 | struct drm_device *dev = obj->dev; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1480 | struct drm_gem_mm *mm = dev->mm_private; |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1481 | struct drm_map_list *list = &obj->map_list; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1482 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1483 | drm_ht_remove_item(&mm->offset_hash, &list->hash); |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1484 | drm_mm_put_block(list->file_offset_node); |
| 1485 | kfree(list->map); |
| 1486 | list->map = NULL; |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1487 | } |
| 1488 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1489 | /** |
| 1490 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object |
| 1491 | * @obj: object to check |
| 1492 | * |
| 1493 | * Return the required GTT alignment for an object, taking into account |
| 1494 | * potential fence register mapping if needed. |
| 1495 | */ |
| 1496 | static uint32_t |
| 1497 | i915_gem_get_gtt_alignment(struct drm_gem_object *obj) |
| 1498 | { |
| 1499 | struct drm_device *dev = obj->dev; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1500 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1501 | int start, i; |
| 1502 | |
| 1503 | /* |
| 1504 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1505 | * if a fence register is needed for the object. |
| 1506 | */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1507 | if (INTEL_INFO(dev)->gen >= 4 || obj_priv->tiling_mode == I915_TILING_NONE) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1508 | return 4096; |
| 1509 | |
| 1510 | /* |
| 1511 | * Previous chips need to be aligned to the size of the smallest |
| 1512 | * fence register that can contain the object. |
| 1513 | */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1514 | if (INTEL_INFO(dev)->gen == 3) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1515 | start = 1024*1024; |
| 1516 | else |
| 1517 | start = 512*1024; |
| 1518 | |
| 1519 | for (i = start; i < obj->size; i <<= 1) |
| 1520 | ; |
| 1521 | |
| 1522 | return i; |
| 1523 | } |
| 1524 | |
| 1525 | /** |
| 1526 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 1527 | * @dev: DRM device |
| 1528 | * @data: GTT mapping ioctl data |
| 1529 | * @file_priv: GEM object info |
| 1530 | * |
| 1531 | * Simply returns the fake offset to userspace so it can mmap it. |
| 1532 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 1533 | * up so we can get faults in the handler above. |
| 1534 | * |
| 1535 | * The fault handler will take care of binding the object into the GTT |
| 1536 | * (since it may have been evicted to make room for something), allocating |
| 1537 | * a fence register, and mapping the appropriate aperture address into |
| 1538 | * userspace. |
| 1539 | */ |
| 1540 | int |
| 1541 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 1542 | struct drm_file *file_priv) |
| 1543 | { |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1544 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1545 | struct drm_i915_gem_mmap_gtt *args = data; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1546 | struct drm_gem_object *obj; |
| 1547 | struct drm_i915_gem_object *obj_priv; |
| 1548 | int ret; |
| 1549 | |
| 1550 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1551 | return -ENODEV; |
| 1552 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1553 | ret = i915_mutex_lock_interruptible(dev); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1554 | if (ret) |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 1555 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1556 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1557 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1558 | if (obj == NULL) { |
| 1559 | ret = -ENOENT; |
| 1560 | goto unlock; |
| 1561 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1562 | obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1563 | |
Chris Wilson | da761a6 | 2010-10-27 17:37:08 +0100 | [diff] [blame] | 1564 | if (obj->size > dev_priv->mm.gtt_mappable_end) { |
| 1565 | ret = -E2BIG; |
| 1566 | goto unlock; |
| 1567 | } |
| 1568 | |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1569 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
| 1570 | DRM_ERROR("Attempting to mmap a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1571 | ret = -EINVAL; |
| 1572 | goto out; |
Chris Wilson | ab18282 | 2009-09-22 18:46:17 +0100 | [diff] [blame] | 1573 | } |
| 1574 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1575 | if (!obj->map_list.map) { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1576 | ret = i915_gem_create_mmap_offset(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1577 | if (ret) |
| 1578 | goto out; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1579 | } |
| 1580 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 1581 | args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1582 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1583 | out: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1584 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1585 | unlock: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1586 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 1587 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1588 | } |
| 1589 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 1590 | static void |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1591 | i915_gem_object_put_pages(struct drm_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1592 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1593 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1594 | int page_count = obj->size / PAGE_SIZE; |
| 1595 | int i; |
| 1596 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1597 | BUG_ON(obj_priv->pages_refcount == 0); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1598 | BUG_ON(obj_priv->madv == __I915_MADV_PURGED); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1599 | |
| 1600 | if (--obj_priv->pages_refcount != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1601 | return; |
| 1602 | |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 1603 | if (obj_priv->tiling_mode != I915_TILING_NONE) |
| 1604 | i915_gem_object_save_bit_17_swizzle(obj); |
| 1605 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1606 | if (obj_priv->madv == I915_MADV_DONTNEED) |
Chris Wilson | 13a05fd | 2009-09-20 23:03:19 +0100 | [diff] [blame] | 1607 | obj_priv->dirty = 0; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1608 | |
| 1609 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1610 | if (obj_priv->dirty) |
| 1611 | set_page_dirty(obj_priv->pages[i]); |
| 1612 | |
| 1613 | if (obj_priv->madv == I915_MADV_WILLNEED) |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1614 | mark_page_accessed(obj_priv->pages[i]); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 1615 | |
| 1616 | page_cache_release(obj_priv->pages[i]); |
| 1617 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1618 | obj_priv->dirty = 0; |
| 1619 | |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 1620 | drm_free_large(obj_priv->pages); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1621 | obj_priv->pages = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1622 | } |
| 1623 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1624 | static uint32_t |
| 1625 | i915_gem_next_request_seqno(struct drm_device *dev, |
| 1626 | struct intel_ring_buffer *ring) |
| 1627 | { |
| 1628 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1629 | |
| 1630 | ring->outstanding_lazy_request = true; |
| 1631 | return dev_priv->next_seqno; |
| 1632 | } |
| 1633 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1634 | static void |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1635 | i915_gem_object_move_to_active(struct drm_gem_object *obj, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1636 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1637 | { |
| 1638 | struct drm_device *dev = obj->dev; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1639 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1640 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1641 | uint32_t seqno = i915_gem_next_request_seqno(dev, ring); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1642 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1643 | BUG_ON(ring == NULL); |
| 1644 | obj_priv->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1645 | |
| 1646 | /* Add a reference if we're newly entering the active list. */ |
| 1647 | if (!obj_priv->active) { |
| 1648 | drm_gem_object_reference(obj); |
| 1649 | obj_priv->active = 1; |
| 1650 | } |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 1651 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1652 | /* Move from whatever list we were on to the tail of execution. */ |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1653 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list); |
| 1654 | list_move_tail(&obj_priv->ring_list, &ring->active_list); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1655 | obj_priv->last_rendering_seqno = seqno; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1656 | } |
| 1657 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1658 | static void |
| 1659 | i915_gem_object_move_to_flushing(struct drm_gem_object *obj) |
| 1660 | { |
| 1661 | struct drm_device *dev = obj->dev; |
| 1662 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1663 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1664 | |
| 1665 | BUG_ON(!obj_priv->active); |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1666 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list); |
| 1667 | list_del_init(&obj_priv->ring_list); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1668 | obj_priv->last_rendering_seqno = 0; |
| 1669 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1670 | |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1671 | /* Immediately discard the backing storage */ |
| 1672 | static void |
| 1673 | i915_gem_object_truncate(struct drm_gem_object *obj) |
| 1674 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1675 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1676 | struct inode *inode; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1677 | |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1678 | /* Our goal here is to return as much of the memory as |
| 1679 | * is possible back to the system as we are called from OOM. |
| 1680 | * To do this we must instruct the shmfs to drop all of its |
| 1681 | * backing pages, *now*. Here we mirror the actions taken |
| 1682 | * when by shmem_delete_inode() to release the backing store. |
| 1683 | */ |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1684 | inode = obj->filp->f_path.dentry->d_inode; |
Chris Wilson | ae9fed6 | 2010-08-07 11:01:30 +0100 | [diff] [blame] | 1685 | truncate_inode_pages(inode->i_mapping, 0); |
| 1686 | if (inode->i_op->truncate_range) |
| 1687 | inode->i_op->truncate_range(inode, 0, (loff_t)-1); |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 1688 | |
| 1689 | obj_priv->madv = __I915_MADV_PURGED; |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 1690 | } |
| 1691 | |
| 1692 | static inline int |
| 1693 | i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv) |
| 1694 | { |
| 1695 | return obj_priv->madv == I915_MADV_DONTNEED; |
| 1696 | } |
| 1697 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1698 | static void |
| 1699 | i915_gem_object_move_to_inactive(struct drm_gem_object *obj) |
| 1700 | { |
| 1701 | struct drm_device *dev = obj->dev; |
| 1702 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 1703 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1704 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1705 | if (obj_priv->pin_count != 0) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1706 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1707 | else |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1708 | list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
| 1709 | list_del_init(&obj_priv->ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1710 | |
Daniel Vetter | 99fcb76 | 2010-02-07 16:20:18 +0100 | [diff] [blame] | 1711 | BUG_ON(!list_empty(&obj_priv->gpu_write_list)); |
| 1712 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1713 | obj_priv->last_rendering_seqno = 0; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1714 | obj_priv->ring = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1715 | if (obj_priv->active) { |
| 1716 | obj_priv->active = 0; |
| 1717 | drm_gem_object_unreference(obj); |
| 1718 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1719 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1720 | } |
| 1721 | |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1722 | static void |
| 1723 | i915_gem_process_flushing_list(struct drm_device *dev, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1724 | uint32_t flush_domains, |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1725 | struct intel_ring_buffer *ring) |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1726 | { |
| 1727 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1728 | struct drm_i915_gem_object *obj_priv, *next; |
| 1729 | |
| 1730 | list_for_each_entry_safe(obj_priv, next, |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 1731 | &ring->gpu_write_list, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1732 | gpu_write_list) { |
Daniel Vetter | a8089e8 | 2010-04-09 19:05:09 +0000 | [diff] [blame] | 1733 | struct drm_gem_object *obj = &obj_priv->base; |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1734 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 1735 | if (obj->write_domain & flush_domains) { |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1736 | uint32_t old_write_domain = obj->write_domain; |
| 1737 | |
| 1738 | obj->write_domain = 0; |
| 1739 | list_del_init(&obj_priv->gpu_write_list); |
Daniel Vetter | 617dbe2 | 2010-02-11 22:16:02 +0100 | [diff] [blame] | 1740 | i915_gem_object_move_to_active(obj, ring); |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1741 | |
| 1742 | /* update the fence lru list */ |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1743 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
| 1744 | struct drm_i915_fence_reg *reg = |
| 1745 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 1746 | list_move_tail(®->lru_list, |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1747 | &dev_priv->mm.fence_list); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 1748 | } |
Daniel Vetter | 6356039 | 2010-02-19 11:51:59 +0100 | [diff] [blame] | 1749 | |
| 1750 | trace_i915_gem_object_change_domain(obj, |
| 1751 | obj->read_domains, |
| 1752 | old_write_domain); |
| 1753 | } |
| 1754 | } |
| 1755 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 1756 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1757 | int |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1758 | i915_add_request(struct drm_device *dev, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1759 | struct drm_file *file, |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 1760 | struct drm_i915_gem_request *request, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1761 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1762 | { |
| 1763 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1764 | struct drm_i915_file_private *file_priv = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1765 | uint32_t seqno; |
| 1766 | int was_empty; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1767 | int ret; |
| 1768 | |
| 1769 | BUG_ON(request == NULL); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1770 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1771 | if (file != NULL) |
| 1772 | file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1773 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1774 | ret = ring->add_request(ring, &seqno); |
| 1775 | if (ret) |
| 1776 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1777 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 1778 | ring->outstanding_lazy_request = false; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1779 | |
| 1780 | request->seqno = seqno; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1781 | request->ring = ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1782 | request->emitted_jiffies = jiffies; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1783 | was_empty = list_empty(&ring->request_list); |
| 1784 | list_add_tail(&request->list, &ring->request_list); |
| 1785 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1786 | if (file_priv) { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1787 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1788 | request->file_priv = file_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1789 | list_add_tail(&request->client_list, |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1790 | &file_priv->mm.request_list); |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1791 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 1792 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1793 | |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1794 | if (!dev_priv->mm.suspended) { |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1795 | mod_timer(&dev_priv->hangcheck_timer, |
| 1796 | jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD)); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1797 | if (was_empty) |
Chris Wilson | b3b079d | 2010-09-13 23:44:34 +0100 | [diff] [blame] | 1798 | queue_delayed_work(dev_priv->wq, |
| 1799 | &dev_priv->mm.retire_work, HZ); |
Ben Gamari | f65d942 | 2009-09-14 17:48:44 -0400 | [diff] [blame] | 1800 | } |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 1801 | return 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | /** |
| 1805 | * Command execution barrier |
| 1806 | * |
| 1807 | * Ensures that all commands in the ring are finished |
| 1808 | * before signalling the CPU |
| 1809 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 1810 | static void |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1811 | i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1812 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1813 | uint32_t flush_domains = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1814 | |
| 1815 | /* The sampler always gets flushed on i965 (sigh) */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 1816 | if (INTEL_INFO(dev)->gen >= 4) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1817 | flush_domains |= I915_GEM_DOMAIN_SAMPLER; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1818 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1819 | ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1822 | static inline void |
| 1823 | i915_gem_request_remove_from_client(struct drm_i915_gem_request *request) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1824 | { |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1825 | struct drm_i915_file_private *file_priv = request->file_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1826 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1827 | if (!file_priv) |
| 1828 | return; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 1829 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 1830 | spin_lock(&file_priv->mm.lock); |
| 1831 | list_del(&request->client_list); |
| 1832 | request->file_priv = NULL; |
| 1833 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1834 | } |
| 1835 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1836 | static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv, |
| 1837 | struct intel_ring_buffer *ring) |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1838 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1839 | while (!list_empty(&ring->request_list)) { |
| 1840 | struct drm_i915_gem_request *request; |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1841 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1842 | request = list_first_entry(&ring->request_list, |
| 1843 | struct drm_i915_gem_request, |
| 1844 | list); |
| 1845 | |
| 1846 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1847 | i915_gem_request_remove_from_client(request); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1848 | kfree(request); |
| 1849 | } |
| 1850 | |
| 1851 | while (!list_empty(&ring->active_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1852 | struct drm_i915_gem_object *obj_priv; |
| 1853 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1854 | obj_priv = list_first_entry(&ring->active_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1855 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1856 | ring_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1857 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1858 | obj_priv->base.write_domain = 0; |
| 1859 | list_del_init(&obj_priv->gpu_write_list); |
| 1860 | i915_gem_object_move_to_inactive(&obj_priv->base); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1861 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1862 | } |
| 1863 | |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1864 | void i915_gem_reset(struct drm_device *dev) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1865 | { |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1866 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1867 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1868 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1869 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1870 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 1871 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 1872 | i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring); |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1873 | |
| 1874 | /* Remove anything from the flushing lists. The GPU cache is likely |
| 1875 | * to be lost on reset along with the data, so simply move the |
| 1876 | * lost bo to the inactive list. |
| 1877 | */ |
| 1878 | while (!list_empty(&dev_priv->mm.flushing_list)) { |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1879 | obj_priv = list_first_entry(&dev_priv->mm.flushing_list, |
| 1880 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1881 | mm_list); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1882 | |
| 1883 | obj_priv->base.write_domain = 0; |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1884 | list_del_init(&obj_priv->gpu_write_list); |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1885 | i915_gem_object_move_to_inactive(&obj_priv->base); |
| 1886 | } |
Chris Wilson | 9375e44 | 2010-09-19 12:21:28 +0100 | [diff] [blame] | 1887 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1888 | /* Move everything out of the GPU domains to ensure we do any |
| 1889 | * necessary invalidation upon reuse. |
| 1890 | */ |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1891 | list_for_each_entry(obj_priv, |
| 1892 | &dev_priv->mm.inactive_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1893 | mm_list) |
Chris Wilson | 77f0123 | 2010-09-19 12:31:36 +0100 | [diff] [blame] | 1894 | { |
| 1895 | obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS; |
| 1896 | } |
Chris Wilson | 069efc1 | 2010-09-30 16:53:18 +0100 | [diff] [blame] | 1897 | |
| 1898 | /* The fence registers are invalidated so clear them out */ |
| 1899 | for (i = 0; i < 16; i++) { |
| 1900 | struct drm_i915_fence_reg *reg; |
| 1901 | |
| 1902 | reg = &dev_priv->fence_regs[i]; |
| 1903 | if (!reg->obj) |
| 1904 | continue; |
| 1905 | |
| 1906 | i915_gem_clear_fence_reg(reg->obj); |
| 1907 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1908 | } |
| 1909 | |
| 1910 | /** |
| 1911 | * This function clears the request list as sequence numbers are passed. |
| 1912 | */ |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1913 | static void |
| 1914 | i915_gem_retire_requests_ring(struct drm_device *dev, |
| 1915 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1916 | { |
| 1917 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1918 | uint32_t seqno; |
| 1919 | |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1920 | if (!ring->status_page.page_addr || |
| 1921 | list_empty(&ring->request_list)) |
Karsten Wiese | 6c0594a | 2009-02-23 15:07:57 +0100 | [diff] [blame] | 1922 | return; |
| 1923 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1924 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1925 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1926 | seqno = ring->get_seqno(ring); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1927 | while (!list_empty(&ring->request_list)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1928 | struct drm_i915_gem_request *request; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1929 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 1930 | request = list_first_entry(&ring->request_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1931 | struct drm_i915_gem_request, |
| 1932 | list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1933 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1934 | if (!i915_seqno_passed(seqno, request->seqno)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1935 | break; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1936 | |
| 1937 | trace_i915_gem_request_retire(dev, request->seqno); |
| 1938 | |
| 1939 | list_del(&request->list); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 1940 | i915_gem_request_remove_from_client(request); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1941 | kfree(request); |
| 1942 | } |
| 1943 | |
| 1944 | /* Move any buffers on the active list that are no longer referenced |
| 1945 | * by the ringbuffer to the flushing/inactive lists as appropriate. |
| 1946 | */ |
| 1947 | while (!list_empty(&ring->active_list)) { |
| 1948 | struct drm_gem_object *obj; |
| 1949 | struct drm_i915_gem_object *obj_priv; |
| 1950 | |
| 1951 | obj_priv = list_first_entry(&ring->active_list, |
| 1952 | struct drm_i915_gem_object, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1953 | ring_list); |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1954 | |
Chris Wilson | dfaae39 | 2010-09-22 10:31:52 +0100 | [diff] [blame] | 1955 | if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno)) |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1956 | break; |
| 1957 | |
| 1958 | obj = &obj_priv->base; |
Chris Wilson | b84d5f0 | 2010-09-18 01:38:04 +0100 | [diff] [blame] | 1959 | if (obj->write_domain != 0) |
| 1960 | i915_gem_object_move_to_flushing(obj); |
| 1961 | else |
| 1962 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1963 | } |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 1964 | |
| 1965 | if (unlikely (dev_priv->trace_irq_seqno && |
| 1966 | i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 1967 | ring->user_irq_put(ring); |
Chris Wilson | 9d34e5d | 2009-09-24 05:26:06 +0100 | [diff] [blame] | 1968 | dev_priv->trace_irq_seqno = 0; |
| 1969 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 1970 | |
| 1971 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1972 | } |
| 1973 | |
| 1974 | void |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1975 | i915_gem_retire_requests(struct drm_device *dev) |
| 1976 | { |
| 1977 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1978 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 1979 | if (!list_empty(&dev_priv->mm.deferred_free_list)) { |
| 1980 | struct drm_i915_gem_object *obj_priv, *tmp; |
| 1981 | |
| 1982 | /* We must be careful that during unbind() we do not |
| 1983 | * accidentally infinitely recurse into retire requests. |
| 1984 | * Currently: |
| 1985 | * retire -> free -> unbind -> wait -> retire_ring |
| 1986 | */ |
| 1987 | list_for_each_entry_safe(obj_priv, tmp, |
| 1988 | &dev_priv->mm.deferred_free_list, |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 1989 | mm_list) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 1990 | i915_gem_free_object_tail(&obj_priv->base); |
| 1991 | } |
| 1992 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1993 | i915_gem_retire_requests_ring(dev, &dev_priv->render_ring); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 1994 | i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 1995 | i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring); |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 1996 | } |
| 1997 | |
Daniel Vetter | 75ef9da | 2010-08-21 00:25:16 +0200 | [diff] [blame] | 1998 | static void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1999 | i915_gem_retire_work_handler(struct work_struct *work) |
| 2000 | { |
| 2001 | drm_i915_private_t *dev_priv; |
| 2002 | struct drm_device *dev; |
| 2003 | |
| 2004 | dev_priv = container_of(work, drm_i915_private_t, |
| 2005 | mm.retire_work.work); |
| 2006 | dev = dev_priv->dev; |
| 2007 | |
Chris Wilson | 891b48c | 2010-09-29 12:26:37 +0100 | [diff] [blame] | 2008 | /* Come back later if the device is busy... */ |
| 2009 | if (!mutex_trylock(&dev->struct_mutex)) { |
| 2010 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
| 2011 | return; |
| 2012 | } |
| 2013 | |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2014 | i915_gem_retire_requests(dev); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2015 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 2016 | if (!dev_priv->mm.suspended && |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2017 | (!list_empty(&dev_priv->render_ring.request_list) || |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2018 | !list_empty(&dev_priv->bsd_ring.request_list) || |
| 2019 | !list_empty(&dev_priv->blt_ring.request_list))) |
Eric Anholt | 9c9fe1f | 2009-08-03 16:09:16 -0700 | [diff] [blame] | 2020 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2021 | mutex_unlock(&dev->struct_mutex); |
| 2022 | } |
| 2023 | |
Daniel Vetter | 5a5a0c6 | 2009-09-15 22:57:36 +0200 | [diff] [blame] | 2024 | int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2025 | i915_do_wait_request(struct drm_device *dev, uint32_t seqno, |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2026 | bool interruptible, struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2027 | { |
| 2028 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2029 | u32 ier; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2030 | int ret = 0; |
| 2031 | |
| 2032 | BUG_ON(seqno == 0); |
| 2033 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2034 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2035 | return -EAGAIN; |
Ben Gamari | ffed1d0 | 2009-09-14 17:48:41 -0400 | [diff] [blame] | 2036 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2037 | if (ring->outstanding_lazy_request) { |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2038 | struct drm_i915_gem_request *request; |
| 2039 | |
| 2040 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 2041 | if (request == NULL) |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2042 | return -ENOMEM; |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 2043 | |
| 2044 | ret = i915_add_request(dev, NULL, request, ring); |
| 2045 | if (ret) { |
| 2046 | kfree(request); |
| 2047 | return ret; |
| 2048 | } |
| 2049 | |
| 2050 | seqno = request->seqno; |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2051 | } |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2052 | BUG_ON(seqno == dev_priv->next_seqno); |
Daniel Vetter | e35a41d | 2010-02-11 22:13:59 +0100 | [diff] [blame] | 2053 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2054 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Eric Anholt | bad720f | 2009-10-22 16:11:14 -0700 | [diff] [blame] | 2055 | if (HAS_PCH_SPLIT(dev)) |
Zhenyu Wang | 036a4a7 | 2009-06-08 14:40:19 +0800 | [diff] [blame] | 2056 | ier = I915_READ(DEIER) | I915_READ(GTIER); |
| 2057 | else |
| 2058 | ier = I915_READ(IER); |
Jesse Barnes | 802c7eb | 2009-05-05 16:03:48 -0700 | [diff] [blame] | 2059 | if (!ier) { |
| 2060 | DRM_ERROR("something (likely vbetool) disabled " |
| 2061 | "interrupts, re-enabling\n"); |
| 2062 | i915_driver_irq_preinstall(dev); |
| 2063 | i915_driver_irq_postinstall(dev); |
| 2064 | } |
| 2065 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2066 | trace_i915_gem_request_wait_begin(dev, seqno); |
| 2067 | |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2068 | ring->waiting_seqno = seqno; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2069 | ring->user_irq_get(ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2070 | if (interruptible) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2071 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2072 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2073 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2074 | else |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2075 | wait_event(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2076 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2077 | || atomic_read(&dev_priv->mm.wedged)); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2078 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2079 | ring->user_irq_put(ring); |
Chris Wilson | b222349 | 2010-10-27 15:27:33 +0100 | [diff] [blame] | 2080 | ring->waiting_seqno = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2081 | |
| 2082 | trace_i915_gem_request_wait_end(dev, seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2083 | } |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 2084 | if (atomic_read(&dev_priv->mm.wedged)) |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 2085 | ret = -EAGAIN; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2086 | |
| 2087 | if (ret && ret != -ERESTARTSYS) |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2088 | DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n", |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2089 | __func__, ret, seqno, ring->get_seqno(ring), |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2090 | dev_priv->next_seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2091 | |
| 2092 | /* Directly dispatch request retiring. While we have the work queue |
| 2093 | * to handle this, the waiter on a request often wants an associated |
| 2094 | * buffer to have made it to the inactive list, and we would need |
| 2095 | * a separate wait queue to handle that. |
| 2096 | */ |
| 2097 | if (ret == 0) |
Chris Wilson | b09a1fe | 2010-07-23 23:18:49 +0100 | [diff] [blame] | 2098 | i915_gem_retire_requests_ring(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2099 | |
| 2100 | return ret; |
| 2101 | } |
| 2102 | |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2103 | /** |
| 2104 | * Waits for a sequence number to be signaled, and cleans up the |
| 2105 | * request and object lists appropriately for that event. |
| 2106 | */ |
| 2107 | static int |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2108 | i915_wait_request(struct drm_device *dev, uint32_t seqno, |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2109 | struct intel_ring_buffer *ring) |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2110 | { |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2111 | return i915_do_wait_request(dev, seqno, 1, ring); |
Daniel Vetter | 48764bf | 2009-09-15 22:57:32 +0200 | [diff] [blame] | 2112 | } |
| 2113 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2114 | static void |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2115 | i915_gem_flush_ring(struct drm_device *dev, |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2116 | struct drm_file *file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2117 | struct intel_ring_buffer *ring, |
| 2118 | uint32_t invalidate_domains, |
| 2119 | uint32_t flush_domains) |
| 2120 | { |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 2121 | ring->flush(ring, invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2122 | i915_gem_process_flushing_list(dev, flush_domains, ring); |
| 2123 | } |
| 2124 | |
| 2125 | static void |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2126 | i915_gem_flush(struct drm_device *dev, |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2127 | struct drm_file *file_priv, |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2128 | uint32_t invalidate_domains, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2129 | uint32_t flush_domains, |
| 2130 | uint32_t flush_rings) |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2131 | { |
| 2132 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 8bff917 | 2010-02-11 22:19:40 +0100 | [diff] [blame] | 2133 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2134 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
| 2135 | drm_agp_chipset_flush(dev); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2136 | |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2137 | if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) { |
| 2138 | if (flush_rings & RING_RENDER) |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2139 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2140 | &dev_priv->render_ring, |
| 2141 | invalidate_domains, flush_domains); |
| 2142 | if (flush_rings & RING_BSD) |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2143 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2144 | &dev_priv->bsd_ring, |
| 2145 | invalidate_domains, flush_domains); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2146 | if (flush_rings & RING_BLT) |
| 2147 | i915_gem_flush_ring(dev, file_priv, |
| 2148 | &dev_priv->blt_ring, |
| 2149 | invalidate_domains, flush_domains); |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2150 | } |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 2151 | } |
| 2152 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2153 | /** |
| 2154 | * Ensures that all rendering to the object has completed and the object is |
| 2155 | * safe to unbind from the GTT or access from the CPU. |
| 2156 | */ |
| 2157 | static int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2158 | i915_gem_object_wait_rendering(struct drm_gem_object *obj, |
| 2159 | bool interruptible) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2160 | { |
| 2161 | struct drm_device *dev = obj->dev; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2162 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2163 | int ret; |
| 2164 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2165 | /* This function only exists to support waiting for existing rendering, |
| 2166 | * not for emitting required flushes. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2167 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2168 | BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2169 | |
| 2170 | /* If there is rendering queued on the buffer being evicted, wait for |
| 2171 | * it. |
| 2172 | */ |
| 2173 | if (obj_priv->active) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2174 | ret = i915_do_wait_request(dev, |
| 2175 | obj_priv->last_rendering_seqno, |
| 2176 | interruptible, |
| 2177 | obj_priv->ring); |
| 2178 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2179 | return ret; |
| 2180 | } |
| 2181 | |
| 2182 | return 0; |
| 2183 | } |
| 2184 | |
| 2185 | /** |
| 2186 | * Unbinds an object from the GTT aperture. |
| 2187 | */ |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2188 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2189 | i915_gem_object_unbind(struct drm_gem_object *obj) |
| 2190 | { |
| 2191 | struct drm_device *dev = obj->dev; |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2192 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2193 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2194 | int ret = 0; |
| 2195 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2196 | if (obj_priv->gtt_space == NULL) |
| 2197 | return 0; |
| 2198 | |
| 2199 | if (obj_priv->pin_count != 0) { |
| 2200 | DRM_ERROR("Attempting to unbind pinned buffer\n"); |
| 2201 | return -EINVAL; |
| 2202 | } |
| 2203 | |
Eric Anholt | 5323fd0 | 2009-09-09 11:50:45 -0700 | [diff] [blame] | 2204 | /* blow away mappings if mapped through GTT */ |
| 2205 | i915_gem_release_mmap(obj); |
| 2206 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2207 | /* Move the object to the CPU domain to ensure that |
| 2208 | * any possible CPU writes while it's not in the GTT |
| 2209 | * are flushed when we go to remap it. This will |
| 2210 | * also ensure that all pending GPU writes are finished |
| 2211 | * before we unbind. |
| 2212 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2213 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2214 | if (ret == -ERESTARTSYS) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2215 | return ret; |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2216 | /* Continue on if we fail due to EIO, the GPU is hung so we |
| 2217 | * should be safe and we need to cleanup or else we might |
| 2218 | * cause memory corruption through use-after-free. |
| 2219 | */ |
Chris Wilson | 812ed492 | 2010-09-30 15:08:57 +0100 | [diff] [blame] | 2220 | if (ret) { |
| 2221 | i915_gem_clflush_object(obj); |
| 2222 | obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 2223 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2224 | |
Daniel Vetter | 96b47b6 | 2009-12-15 17:50:00 +0100 | [diff] [blame] | 2225 | /* release the fence reg _after_ flushing */ |
| 2226 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) |
| 2227 | i915_gem_clear_fence_reg(obj); |
| 2228 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2229 | drm_unbind_agp(obj_priv->agp_mem); |
| 2230 | drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2231 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2232 | i915_gem_object_put_pages(obj); |
Chris Wilson | a32808c | 2009-09-20 21:29:47 +0100 | [diff] [blame] | 2233 | BUG_ON(obj_priv->pages_refcount); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2234 | |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 2235 | i915_gem_info_remove_gtt(dev_priv, obj); |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2236 | list_del_init(&obj_priv->mm_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2237 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 2238 | drm_mm_put_block(obj_priv->gtt_space); |
| 2239 | obj_priv->gtt_space = NULL; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 2240 | obj_priv->gtt_offset = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2241 | |
Chris Wilson | 963b483 | 2009-09-20 23:03:54 +0100 | [diff] [blame] | 2242 | if (i915_gem_object_is_purgeable(obj_priv)) |
| 2243 | i915_gem_object_truncate(obj); |
| 2244 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2245 | trace_i915_gem_object_unbind(obj); |
| 2246 | |
Chris Wilson | 8dc1775 | 2010-07-23 23:18:51 +0100 | [diff] [blame] | 2247 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2248 | } |
| 2249 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2250 | static int i915_ring_idle(struct drm_device *dev, |
| 2251 | struct intel_ring_buffer *ring) |
| 2252 | { |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 2253 | if (list_empty(&ring->gpu_write_list)) |
| 2254 | return 0; |
| 2255 | |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2256 | i915_gem_flush_ring(dev, NULL, ring, |
| 2257 | I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS); |
| 2258 | return i915_wait_request(dev, |
| 2259 | i915_gem_next_request_seqno(dev, ring), |
| 2260 | ring); |
| 2261 | } |
| 2262 | |
Chris Wilson | b47eb4a | 2010-08-07 11:01:23 +0100 | [diff] [blame] | 2263 | int |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2264 | i915_gpu_idle(struct drm_device *dev) |
| 2265 | { |
| 2266 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2267 | bool lists_empty; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 2268 | int ret; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2269 | |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2270 | lists_empty = (list_empty(&dev_priv->mm.flushing_list) && |
| 2271 | list_empty(&dev_priv->render_ring.active_list) && |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2272 | list_empty(&dev_priv->bsd_ring.active_list) && |
| 2273 | list_empty(&dev_priv->blt_ring.active_list)); |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2274 | if (lists_empty) |
| 2275 | return 0; |
| 2276 | |
| 2277 | /* Flush everything onto the inactive list. */ |
Chris Wilson | a56ba56 | 2010-09-28 10:07:56 +0100 | [diff] [blame] | 2278 | ret = i915_ring_idle(dev, &dev_priv->render_ring); |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2279 | if (ret) |
| 2280 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2281 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 2282 | ret = i915_ring_idle(dev, &dev_priv->bsd_ring); |
| 2283 | if (ret) |
| 2284 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2285 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 2286 | ret = i915_ring_idle(dev, &dev_priv->blt_ring); |
| 2287 | if (ret) |
| 2288 | return ret; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 2289 | |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 2290 | return 0; |
Daniel Vetter | 4df2faf | 2010-02-19 11:52:00 +0100 | [diff] [blame] | 2291 | } |
| 2292 | |
Chris Wilson | 5cdf588 | 2010-09-27 15:51:07 +0100 | [diff] [blame] | 2293 | static int |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2294 | i915_gem_object_get_pages(struct drm_gem_object *obj, |
| 2295 | gfp_t gfpmask) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2296 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2297 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2298 | int page_count, i; |
| 2299 | struct address_space *mapping; |
| 2300 | struct inode *inode; |
| 2301 | struct page *page; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2302 | |
Daniel Vetter | 778c354 | 2010-05-13 11:49:44 +0200 | [diff] [blame] | 2303 | BUG_ON(obj_priv->pages_refcount |
| 2304 | == DRM_I915_GEM_OBJECT_MAX_PAGES_REFCOUNT); |
| 2305 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2306 | if (obj_priv->pages_refcount++ != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2307 | return 0; |
| 2308 | |
| 2309 | /* Get the list of pages out of our struct file. They'll be pinned |
| 2310 | * at this point until we release them. |
| 2311 | */ |
| 2312 | page_count = obj->size / PAGE_SIZE; |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2313 | BUG_ON(obj_priv->pages != NULL); |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 2314 | obj_priv->pages = drm_calloc_large(page_count, sizeof(struct page *)); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2315 | if (obj_priv->pages == NULL) { |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2316 | obj_priv->pages_refcount--; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2317 | return -ENOMEM; |
| 2318 | } |
| 2319 | |
| 2320 | inode = obj->filp->f_path.dentry->d_inode; |
| 2321 | mapping = inode->i_mapping; |
| 2322 | for (i = 0; i < page_count; i++) { |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2323 | page = read_cache_page_gfp(mapping, i, |
Linus Torvalds | 985b823 | 2010-07-02 10:04:42 +1000 | [diff] [blame] | 2324 | GFP_HIGHUSER | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2325 | __GFP_COLD | |
Linus Torvalds | cd9f040 | 2010-07-18 09:44:37 -0700 | [diff] [blame] | 2326 | __GFP_RECLAIMABLE | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2327 | gfpmask); |
Chris Wilson | 1f2b101 | 2010-03-12 19:52:55 +0000 | [diff] [blame] | 2328 | if (IS_ERR(page)) |
| 2329 | goto err_pages; |
| 2330 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2331 | obj_priv->pages[i] = page; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2332 | } |
Eric Anholt | 280b713 | 2009-03-12 16:56:27 -0700 | [diff] [blame] | 2333 | |
| 2334 | if (obj_priv->tiling_mode != I915_TILING_NONE) |
| 2335 | i915_gem_object_do_bit_17_swizzle(obj); |
| 2336 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2337 | return 0; |
Chris Wilson | 1f2b101 | 2010-03-12 19:52:55 +0000 | [diff] [blame] | 2338 | |
| 2339 | err_pages: |
| 2340 | while (i--) |
| 2341 | page_cache_release(obj_priv->pages[i]); |
| 2342 | |
| 2343 | drm_free_large(obj_priv->pages); |
| 2344 | obj_priv->pages = NULL; |
| 2345 | obj_priv->pages_refcount--; |
| 2346 | return PTR_ERR(page); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2347 | } |
| 2348 | |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2349 | static void sandybridge_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 2350 | { |
| 2351 | struct drm_gem_object *obj = reg->obj; |
| 2352 | struct drm_device *dev = obj->dev; |
| 2353 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2354 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2355 | int regnum = obj_priv->fence_reg; |
| 2356 | uint64_t val; |
| 2357 | |
| 2358 | val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) & |
| 2359 | 0xfffff000) << 32; |
| 2360 | val |= obj_priv->gtt_offset & 0xfffff000; |
| 2361 | val |= (uint64_t)((obj_priv->stride / 128) - 1) << |
| 2362 | SANDYBRIDGE_FENCE_PITCH_SHIFT; |
| 2363 | |
| 2364 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2365 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2366 | val |= I965_FENCE_REG_VALID; |
| 2367 | |
| 2368 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val); |
| 2369 | } |
| 2370 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2371 | static void i965_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 2372 | { |
| 2373 | struct drm_gem_object *obj = reg->obj; |
| 2374 | struct drm_device *dev = obj->dev; |
| 2375 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2376 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2377 | int regnum = obj_priv->fence_reg; |
| 2378 | uint64_t val; |
| 2379 | |
| 2380 | val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) & |
| 2381 | 0xfffff000) << 32; |
| 2382 | val |= obj_priv->gtt_offset & 0xfffff000; |
| 2383 | val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT; |
| 2384 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2385 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 2386 | val |= I965_FENCE_REG_VALID; |
| 2387 | |
| 2388 | I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val); |
| 2389 | } |
| 2390 | |
| 2391 | static void i915_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 2392 | { |
| 2393 | struct drm_gem_object *obj = reg->obj; |
| 2394 | struct drm_device *dev = obj->dev; |
| 2395 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2396 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2397 | int regnum = obj_priv->fence_reg; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2398 | int tile_width; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2399 | uint32_t fence_reg, val; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2400 | uint32_t pitch_val; |
| 2401 | |
| 2402 | if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) || |
| 2403 | (obj_priv->gtt_offset & (obj->size - 1))) { |
Linus Torvalds | f06da26 | 2009-02-09 08:57:29 -0800 | [diff] [blame] | 2404 | WARN(1, "%s: object 0x%08x not 1M or size (0x%zx) aligned\n", |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2405 | __func__, obj_priv->gtt_offset, obj->size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2406 | return; |
| 2407 | } |
| 2408 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2409 | if (obj_priv->tiling_mode == I915_TILING_Y && |
| 2410 | HAS_128_BYTE_Y_TILING(dev)) |
| 2411 | tile_width = 128; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2412 | else |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2413 | tile_width = 512; |
| 2414 | |
| 2415 | /* Note: pitch better be a power of two tile widths */ |
| 2416 | pitch_val = obj_priv->stride / tile_width; |
| 2417 | pitch_val = ffs(pitch_val) - 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2418 | |
Daniel Vetter | c36a2a6 | 2010-04-17 15:12:03 +0200 | [diff] [blame] | 2419 | if (obj_priv->tiling_mode == I915_TILING_Y && |
| 2420 | HAS_128_BYTE_Y_TILING(dev)) |
| 2421 | WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL); |
| 2422 | else |
| 2423 | WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL); |
| 2424 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2425 | val = obj_priv->gtt_offset; |
| 2426 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2427 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
| 2428 | val |= I915_FENCE_SIZE_BITS(obj->size); |
| 2429 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2430 | val |= I830_FENCE_REG_VALID; |
| 2431 | |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2432 | if (regnum < 8) |
| 2433 | fence_reg = FENCE_REG_830_0 + (regnum * 4); |
| 2434 | else |
| 2435 | fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4); |
| 2436 | I915_WRITE(fence_reg, val); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2437 | } |
| 2438 | |
| 2439 | static void i830_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 2440 | { |
| 2441 | struct drm_gem_object *obj = reg->obj; |
| 2442 | struct drm_device *dev = obj->dev; |
| 2443 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2444 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2445 | int regnum = obj_priv->fence_reg; |
| 2446 | uint32_t val; |
| 2447 | uint32_t pitch_val; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2448 | uint32_t fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2449 | |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2450 | if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) || |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2451 | (obj_priv->gtt_offset & (obj->size - 1))) { |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2452 | WARN(1, "%s: object 0x%08x not 512K or size aligned\n", |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2453 | __func__, obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2454 | return; |
| 2455 | } |
| 2456 | |
Eric Anholt | e76a16d | 2009-05-26 17:44:56 -0700 | [diff] [blame] | 2457 | pitch_val = obj_priv->stride / 128; |
| 2458 | pitch_val = ffs(pitch_val) - 1; |
| 2459 | WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL); |
| 2460 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2461 | val = obj_priv->gtt_offset; |
| 2462 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2463 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2464 | fence_size_bits = I830_FENCE_SIZE_BITS(obj->size); |
| 2465 | WARN_ON(fence_size_bits & ~0x00000f00); |
| 2466 | val |= fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2467 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2468 | val |= I830_FENCE_REG_VALID; |
| 2469 | |
| 2470 | I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2471 | } |
| 2472 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2473 | static int i915_find_fence_reg(struct drm_device *dev, |
| 2474 | bool interruptible) |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2475 | { |
| 2476 | struct drm_i915_fence_reg *reg = NULL; |
| 2477 | struct drm_i915_gem_object *obj_priv = NULL; |
| 2478 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 2479 | struct drm_gem_object *obj = NULL; |
| 2480 | int i, avail, ret; |
| 2481 | |
| 2482 | /* First try to find a free reg */ |
| 2483 | avail = 0; |
| 2484 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
| 2485 | reg = &dev_priv->fence_regs[i]; |
| 2486 | if (!reg->obj) |
| 2487 | return i; |
| 2488 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2489 | obj_priv = to_intel_bo(reg->obj); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2490 | if (!obj_priv->pin_count) |
| 2491 | avail++; |
| 2492 | } |
| 2493 | |
| 2494 | if (avail == 0) |
| 2495 | return -ENOSPC; |
| 2496 | |
| 2497 | /* None available, try to steal one or wait for a user to finish */ |
| 2498 | i = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2499 | list_for_each_entry(reg, &dev_priv->mm.fence_list, |
| 2500 | lru_list) { |
| 2501 | obj = reg->obj; |
| 2502 | obj_priv = to_intel_bo(obj); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2503 | |
| 2504 | if (obj_priv->pin_count) |
| 2505 | continue; |
| 2506 | |
| 2507 | /* found one! */ |
| 2508 | i = obj_priv->fence_reg; |
| 2509 | break; |
| 2510 | } |
| 2511 | |
| 2512 | BUG_ON(i == I915_FENCE_REG_NONE); |
| 2513 | |
| 2514 | /* We only have a reference on obj from the active list. put_fence_reg |
| 2515 | * might drop that one, causing a use-after-free in it. So hold a |
| 2516 | * private reference to obj like the other callers of put_fence_reg |
| 2517 | * (set_tiling ioctl) do. */ |
| 2518 | drm_gem_object_reference(obj); |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2519 | ret = i915_gem_object_put_fence_reg(obj, interruptible); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2520 | drm_gem_object_unreference(obj); |
| 2521 | if (ret != 0) |
| 2522 | return ret; |
| 2523 | |
| 2524 | return i; |
| 2525 | } |
| 2526 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2527 | /** |
| 2528 | * i915_gem_object_get_fence_reg - set up a fence reg for an object |
| 2529 | * @obj: object to map through a fence reg |
| 2530 | * |
| 2531 | * When mapping objects through the GTT, userspace wants to be able to write |
| 2532 | * to them without having to worry about swizzling if the object is tiled. |
| 2533 | * |
| 2534 | * This function walks the fence regs looking for a free one for @obj, |
| 2535 | * stealing one if it can't find any. |
| 2536 | * |
| 2537 | * It then sets up the reg based on the object's properties: address, pitch |
| 2538 | * and tiling format. |
| 2539 | */ |
Chris Wilson | 8c4b8c3 | 2009-06-17 22:08:52 +0100 | [diff] [blame] | 2540 | int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2541 | i915_gem_object_get_fence_reg(struct drm_gem_object *obj, |
| 2542 | bool interruptible) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2543 | { |
| 2544 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2545 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2546 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2547 | struct drm_i915_fence_reg *reg = NULL; |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2548 | int ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2549 | |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2550 | /* Just update our place in the LRU if our fence is getting used. */ |
| 2551 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) { |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2552 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 2553 | list_move_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2554 | return 0; |
| 2555 | } |
| 2556 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2557 | switch (obj_priv->tiling_mode) { |
| 2558 | case I915_TILING_NONE: |
| 2559 | WARN(1, "allocating a fence for non-tiled object?\n"); |
| 2560 | break; |
| 2561 | case I915_TILING_X: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2562 | if (!obj_priv->stride) |
| 2563 | return -EINVAL; |
| 2564 | WARN((obj_priv->stride & (512 - 1)), |
| 2565 | "object 0x%08x is X tiled but has non-512B pitch\n", |
| 2566 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2567 | break; |
| 2568 | case I915_TILING_Y: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2569 | if (!obj_priv->stride) |
| 2570 | return -EINVAL; |
| 2571 | WARN((obj_priv->stride & (128 - 1)), |
| 2572 | "object 0x%08x is Y tiled but has non-128B pitch\n", |
| 2573 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2574 | break; |
| 2575 | } |
| 2576 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2577 | ret = i915_find_fence_reg(dev, interruptible); |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2578 | if (ret < 0) |
| 2579 | return ret; |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2580 | |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2581 | obj_priv->fence_reg = ret; |
| 2582 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2583 | list_add_tail(®->lru_list, &dev_priv->mm.fence_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 2584 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2585 | reg->obj = obj; |
| 2586 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2587 | switch (INTEL_INFO(dev)->gen) { |
| 2588 | case 6: |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2589 | sandybridge_write_fence_reg(reg); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2590 | break; |
| 2591 | case 5: |
| 2592 | case 4: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2593 | i965_write_fence_reg(reg); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2594 | break; |
| 2595 | case 3: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2596 | i915_write_fence_reg(reg); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2597 | break; |
| 2598 | case 2: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2599 | i830_write_fence_reg(reg); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2600 | break; |
| 2601 | } |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2602 | |
Daniel Vetter | ae3db24 | 2010-02-19 11:51:58 +0100 | [diff] [blame] | 2603 | trace_i915_gem_object_get_fence(obj, obj_priv->fence_reg, |
| 2604 | obj_priv->tiling_mode); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2605 | |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2606 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2607 | } |
| 2608 | |
| 2609 | /** |
| 2610 | * i915_gem_clear_fence_reg - clear out fence register info |
| 2611 | * @obj: object to clear |
| 2612 | * |
| 2613 | * Zeroes out the fence register itself and clears out the associated |
| 2614 | * data structures in dev_priv and obj_priv. |
| 2615 | */ |
| 2616 | static void |
| 2617 | i915_gem_clear_fence_reg(struct drm_gem_object *obj) |
| 2618 | { |
| 2619 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2620 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2621 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2622 | struct drm_i915_fence_reg *reg = |
| 2623 | &dev_priv->fence_regs[obj_priv->fence_reg]; |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2624 | uint32_t fence_reg; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2625 | |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2626 | switch (INTEL_INFO(dev)->gen) { |
| 2627 | case 6: |
Eric Anholt | 4e901fd | 2009-10-26 16:44:17 -0700 | [diff] [blame] | 2628 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + |
| 2629 | (obj_priv->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2630 | break; |
| 2631 | case 5: |
| 2632 | case 4: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2633 | I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2634 | break; |
| 2635 | case 3: |
Chris Wilson | 9b74f73 | 2010-09-22 19:10:44 +0100 | [diff] [blame] | 2636 | if (obj_priv->fence_reg >= 8) |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2637 | fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2638 | else |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2639 | case 2: |
| 2640 | fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2641 | |
| 2642 | I915_WRITE(fence_reg, 0); |
Chris Wilson | e259bef | 2010-09-17 00:32:02 +0100 | [diff] [blame] | 2643 | break; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2644 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2645 | |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2646 | reg->obj = NULL; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2647 | obj_priv->fence_reg = I915_FENCE_REG_NONE; |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 2648 | list_del_init(®->lru_list); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2649 | } |
| 2650 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2651 | /** |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2652 | * i915_gem_object_put_fence_reg - waits on outstanding fenced access |
| 2653 | * to the buffer to finish, and then resets the fence register. |
| 2654 | * @obj: tiled object holding a fence register. |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2655 | * @bool: whether the wait upon the fence is interruptible |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2656 | * |
| 2657 | * Zeroes out the fence register itself and clears out the associated |
| 2658 | * data structures in dev_priv and obj_priv. |
| 2659 | */ |
| 2660 | int |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2661 | i915_gem_object_put_fence_reg(struct drm_gem_object *obj, |
| 2662 | bool interruptible) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2663 | { |
| 2664 | struct drm_device *dev = obj->dev; |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2665 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2666 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2667 | struct drm_i915_fence_reg *reg; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2668 | |
| 2669 | if (obj_priv->fence_reg == I915_FENCE_REG_NONE) |
| 2670 | return 0; |
| 2671 | |
Daniel Vetter | 10ae9bd | 2010-02-01 13:59:17 +0100 | [diff] [blame] | 2672 | /* If we've changed tiling, GTT-mappings of the object |
| 2673 | * need to re-fault to ensure that the correct fence register |
| 2674 | * setup is in place. |
| 2675 | */ |
| 2676 | i915_gem_release_mmap(obj); |
| 2677 | |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2678 | /* On the i915, GPU access to tiled buffers is via a fence, |
| 2679 | * therefore we must wait for any outstanding access to complete |
| 2680 | * before clearing the fence. |
| 2681 | */ |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2682 | reg = &dev_priv->fence_regs[obj_priv->fence_reg]; |
| 2683 | if (reg->gpu) { |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2684 | int ret; |
| 2685 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2686 | ret = i915_gem_object_flush_gpu_write_domain(obj, true); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2687 | if (ret) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2688 | return ret; |
| 2689 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2690 | ret = i915_gem_object_wait_rendering(obj, interruptible); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2691 | if (ret) |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2692 | return ret; |
Chris Wilson | 53640e1 | 2010-09-20 11:40:50 +0100 | [diff] [blame] | 2693 | |
| 2694 | reg->gpu = false; |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Daniel Vetter | 4a72661 | 2010-02-01 13:59:16 +0100 | [diff] [blame] | 2697 | i915_gem_object_flush_gtt_write_domain(obj); |
Chris Wilson | 0bc23aa | 2010-09-14 10:22:23 +0100 | [diff] [blame] | 2698 | i915_gem_clear_fence_reg(obj); |
Chris Wilson | 52dc7d3 | 2009-06-06 09:46:01 +0100 | [diff] [blame] | 2699 | |
| 2700 | return 0; |
| 2701 | } |
| 2702 | |
| 2703 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2704 | * Finds free space in the GTT aperture and binds the object there. |
| 2705 | */ |
| 2706 | static int |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2707 | i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, |
| 2708 | unsigned alignment, |
| 2709 | bool mappable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2710 | { |
| 2711 | struct drm_device *dev = obj->dev; |
| 2712 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2713 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2714 | struct drm_mm_node *free_space; |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2715 | gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2716 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2717 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 2718 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 2719 | DRM_ERROR("Attempting to bind a purgeable object\n"); |
| 2720 | return -EINVAL; |
| 2721 | } |
| 2722 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2723 | if (alignment == 0) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2724 | alignment = i915_gem_get_gtt_alignment(obj); |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame] | 2725 | if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2726 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
| 2727 | return -EINVAL; |
| 2728 | } |
| 2729 | |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 2730 | /* If the object is bigger than the entire aperture, reject it early |
| 2731 | * before evicting everything in a vain attempt to find space. |
| 2732 | */ |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2733 | if (obj->size > |
| 2734 | (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) { |
Chris Wilson | 654fc60 | 2010-05-27 13:18:21 +0100 | [diff] [blame] | 2735 | DRM_ERROR("Attempting to bind an object larger than the aperture\n"); |
| 2736 | return -E2BIG; |
| 2737 | } |
| 2738 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2739 | search_free: |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2740 | if (mappable) |
| 2741 | free_space = |
| 2742 | drm_mm_search_free_in_range(&dev_priv->mm.gtt_space, |
| 2743 | obj->size, alignment, 0, |
| 2744 | dev_priv->mm.gtt_mappable_end, |
| 2745 | 0); |
| 2746 | else |
| 2747 | free_space = drm_mm_search_free(&dev_priv->mm.gtt_space, |
| 2748 | obj->size, alignment, 0); |
| 2749 | |
| 2750 | if (free_space != NULL) { |
| 2751 | if (mappable) |
| 2752 | obj_priv->gtt_space = |
| 2753 | drm_mm_get_block_range_generic(free_space, |
| 2754 | obj->size, |
| 2755 | alignment, 0, |
| 2756 | dev_priv->mm.gtt_mappable_end, |
| 2757 | 0); |
| 2758 | else |
| 2759 | obj_priv->gtt_space = |
| 2760 | drm_mm_get_block(free_space, obj->size, |
| 2761 | alignment); |
| 2762 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2763 | if (obj_priv->gtt_space == NULL) { |
| 2764 | /* If the gtt is empty and we're still having trouble |
| 2765 | * fitting our object in, we're out of memory. |
| 2766 | */ |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2767 | ret = i915_gem_evict_something(dev, obj->size, alignment, |
| 2768 | mappable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2769 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2770 | return ret; |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2771 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2772 | goto search_free; |
| 2773 | } |
| 2774 | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2775 | ret = i915_gem_object_get_pages(obj, gfpmask); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2776 | if (ret) { |
| 2777 | drm_mm_put_block(obj_priv->gtt_space); |
| 2778 | obj_priv->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2779 | |
| 2780 | if (ret == -ENOMEM) { |
| 2781 | /* first try to clear up some space from the GTT */ |
Daniel Vetter | 0108a3e | 2010-08-07 11:01:21 +0100 | [diff] [blame] | 2782 | ret = i915_gem_evict_something(dev, obj->size, |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2783 | alignment, mappable); |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2784 | if (ret) { |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2785 | /* now try to shrink everyone else */ |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 2786 | if (gfpmask) { |
| 2787 | gfpmask = 0; |
| 2788 | goto search_free; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2789 | } |
| 2790 | |
| 2791 | return ret; |
| 2792 | } |
| 2793 | |
| 2794 | goto search_free; |
| 2795 | } |
| 2796 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2797 | return ret; |
| 2798 | } |
| 2799 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2800 | /* Create an AGP memory structure pointing at our pages, and bind it |
| 2801 | * into the GTT. |
| 2802 | */ |
| 2803 | obj_priv->agp_mem = drm_agp_bind_pages(dev, |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2804 | obj_priv->pages, |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2805 | obj->size >> PAGE_SHIFT, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 2806 | obj_priv->gtt_space->start, |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 2807 | obj_priv->agp_type); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2808 | if (obj_priv->agp_mem == NULL) { |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2809 | i915_gem_object_put_pages(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2810 | drm_mm_put_block(obj_priv->gtt_space); |
| 2811 | obj_priv->gtt_space = NULL; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2812 | |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 2813 | ret = i915_gem_evict_something(dev, obj->size, alignment, |
| 2814 | mappable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 2815 | if (ret) |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2816 | return ret; |
Chris Wilson | 07f73f6 | 2009-09-14 16:50:30 +0100 | [diff] [blame] | 2817 | |
| 2818 | goto search_free; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2819 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2820 | |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 2821 | obj_priv->gtt_offset = obj_priv->gtt_space->start; |
| 2822 | |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2823 | /* keep track of bounds object by adding it to the inactive list */ |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 2824 | list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 2825 | i915_gem_info_add_gtt(dev_priv, obj); |
Chris Wilson | bf1a109 | 2010-08-07 11:01:20 +0100 | [diff] [blame] | 2826 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2827 | /* Assert that the object is not currently in any GPU domain. As it |
| 2828 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2829 | * a GPU cache |
| 2830 | */ |
Chris Wilson | 21d509e | 2009-06-06 09:46:02 +0100 | [diff] [blame] | 2831 | BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS); |
| 2832 | BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2833 | |
Daniel Vetter | ec57d26 | 2010-09-30 23:42:15 +0200 | [diff] [blame] | 2834 | trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2835 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2836 | return 0; |
| 2837 | } |
| 2838 | |
| 2839 | void |
| 2840 | i915_gem_clflush_object(struct drm_gem_object *obj) |
| 2841 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2842 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2843 | |
| 2844 | /* If we don't have a page list set up, then we're not pinned |
| 2845 | * to GPU, and we can ignore the cache flush because it'll happen |
| 2846 | * again at bind time. |
| 2847 | */ |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2848 | if (obj_priv->pages == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2849 | return; |
| 2850 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2851 | trace_i915_gem_object_clflush(obj); |
Eric Anholt | cfa16a0 | 2009-05-26 18:46:16 -0700 | [diff] [blame] | 2852 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2853 | drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2854 | } |
| 2855 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2856 | /** Flushes any GPU write domain for the object if it's dirty. */ |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2857 | static int |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2858 | i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj, |
| 2859 | bool pipelined) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2860 | { |
| 2861 | struct drm_device *dev = obj->dev; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2862 | uint32_t old_write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2863 | |
| 2864 | if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0) |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2865 | return 0; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2866 | |
| 2867 | /* Queue the GPU write cache flushing we need. */ |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2868 | old_write_domain = obj->write_domain; |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 2869 | i915_gem_flush_ring(dev, NULL, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 2870 | to_intel_bo(obj)->ring, |
| 2871 | 0, obj->write_domain); |
Chris Wilson | 48b956c | 2010-09-14 12:50:34 +0100 | [diff] [blame] | 2872 | BUG_ON(obj->write_domain); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2873 | |
| 2874 | trace_i915_gem_object_change_domain(obj, |
| 2875 | obj->read_domains, |
| 2876 | old_write_domain); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2877 | |
| 2878 | if (pipelined) |
| 2879 | return 0; |
| 2880 | |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2881 | return i915_gem_object_wait_rendering(obj, true); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2882 | } |
| 2883 | |
| 2884 | /** Flushes the GTT write domain for the object if it's dirty. */ |
| 2885 | static void |
| 2886 | i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj) |
| 2887 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2888 | uint32_t old_write_domain; |
| 2889 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2890 | if (obj->write_domain != I915_GEM_DOMAIN_GTT) |
| 2891 | return; |
| 2892 | |
| 2893 | /* No actual flushing is required for the GTT write domain. Writes |
| 2894 | * to it immediately go to main memory as far as we know, so there's |
| 2895 | * no chipset flush. It also doesn't land in render cache. |
| 2896 | */ |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2897 | old_write_domain = obj->write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2898 | obj->write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2899 | |
| 2900 | trace_i915_gem_object_change_domain(obj, |
| 2901 | obj->read_domains, |
| 2902 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2903 | } |
| 2904 | |
| 2905 | /** Flushes the CPU write domain for the object if it's dirty. */ |
| 2906 | static void |
| 2907 | i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj) |
| 2908 | { |
| 2909 | struct drm_device *dev = obj->dev; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2910 | uint32_t old_write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2911 | |
| 2912 | if (obj->write_domain != I915_GEM_DOMAIN_CPU) |
| 2913 | return; |
| 2914 | |
| 2915 | i915_gem_clflush_object(obj); |
| 2916 | drm_agp_chipset_flush(dev); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2917 | old_write_domain = obj->write_domain; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2918 | obj->write_domain = 0; |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2919 | |
| 2920 | trace_i915_gem_object_change_domain(obj, |
| 2921 | obj->read_domains, |
| 2922 | old_write_domain); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2923 | } |
| 2924 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2925 | /** |
| 2926 | * Moves a single object to the GTT read, and possibly write domain. |
| 2927 | * |
| 2928 | * This function returns when the move is complete, including waiting on |
| 2929 | * flushes to occur. |
| 2930 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2931 | int |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2932 | i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write) |
| 2933 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2934 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2935 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2936 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2937 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 2938 | /* Not valid to be called on unbound objects. */ |
| 2939 | if (obj_priv->gtt_space == NULL) |
| 2940 | return -EINVAL; |
| 2941 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2942 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2943 | if (ret != 0) |
| 2944 | return ret; |
| 2945 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 2946 | i915_gem_object_flush_cpu_write_domain(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2947 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2948 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 2949 | ret = i915_gem_object_wait_rendering(obj, true); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2950 | if (ret) |
| 2951 | return ret; |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2952 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2953 | |
| 2954 | old_write_domain = obj->write_domain; |
| 2955 | old_read_domains = obj->read_domains; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2956 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2957 | /* It should now be out of any other write domains, and we can update |
| 2958 | * the domain values for our changes. |
| 2959 | */ |
| 2960 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 2961 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2962 | if (write) { |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 2963 | obj->read_domains = I915_GEM_DOMAIN_GTT; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2964 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2965 | obj_priv->dirty = 1; |
| 2966 | } |
| 2967 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 2968 | trace_i915_gem_object_change_domain(obj, |
| 2969 | old_read_domains, |
| 2970 | old_write_domain); |
| 2971 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2972 | return 0; |
| 2973 | } |
| 2974 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2975 | /* |
| 2976 | * Prepare buffer for display plane. Use uninterruptible for possible flush |
| 2977 | * wait, as in modesetting process we're not supposed to be interrupted. |
| 2978 | */ |
| 2979 | int |
Chris Wilson | 48b956c | 2010-09-14 12:50:34 +0100 | [diff] [blame] | 2980 | i915_gem_object_set_to_display_plane(struct drm_gem_object *obj, |
| 2981 | bool pipelined) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2982 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 2983 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 2984 | uint32_t old_read_domains; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2985 | int ret; |
| 2986 | |
| 2987 | /* Not valid to be called on unbound objects. */ |
| 2988 | if (obj_priv->gtt_space == NULL) |
| 2989 | return -EINVAL; |
| 2990 | |
Chris Wilson | ced270f | 2010-09-26 22:47:46 +0100 | [diff] [blame] | 2991 | ret = i915_gem_object_flush_gpu_write_domain(obj, true); |
Chris Wilson | 2dafb1e | 2010-06-07 14:03:05 +0100 | [diff] [blame] | 2992 | if (ret) |
| 2993 | return ret; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2994 | |
Chris Wilson | ced270f | 2010-09-26 22:47:46 +0100 | [diff] [blame] | 2995 | /* Currently, we are always called from an non-interruptible context. */ |
| 2996 | if (!pipelined) { |
| 2997 | ret = i915_gem_object_wait_rendering(obj, false); |
| 2998 | if (ret) |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 2999 | return ret; |
| 3000 | } |
| 3001 | |
Chris Wilson | b118c1e | 2010-05-27 13:18:14 +0100 | [diff] [blame] | 3002 | i915_gem_object_flush_cpu_write_domain(obj); |
| 3003 | |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3004 | old_read_domains = obj->read_domains; |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 3005 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3006 | |
| 3007 | trace_i915_gem_object_change_domain(obj, |
| 3008 | old_read_domains, |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3009 | obj->write_domain); |
Zhenyu Wang | b9241ea | 2009-11-25 13:09:39 +0800 | [diff] [blame] | 3010 | |
| 3011 | return 0; |
| 3012 | } |
| 3013 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3014 | /** |
| 3015 | * Moves a single object to the CPU read, and possibly write domain. |
| 3016 | * |
| 3017 | * This function returns when the move is complete, including waiting on |
| 3018 | * flushes to occur. |
| 3019 | */ |
| 3020 | static int |
| 3021 | i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write) |
| 3022 | { |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3023 | uint32_t old_write_domain, old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3024 | int ret; |
| 3025 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3026 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3027 | if (ret != 0) |
| 3028 | return ret; |
| 3029 | |
| 3030 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3031 | |
| 3032 | /* If we have a partially-valid cache of the object in the CPU, |
| 3033 | * finish invalidating it and free the per-page flags. |
| 3034 | */ |
| 3035 | i915_gem_object_set_to_full_cpu_read_domain(obj); |
| 3036 | |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3037 | if (write) { |
Chris Wilson | 2cf34d7 | 2010-09-14 13:03:28 +0100 | [diff] [blame] | 3038 | ret = i915_gem_object_wait_rendering(obj, true); |
Chris Wilson | 7213342 | 2010-09-13 23:56:38 +0100 | [diff] [blame] | 3039 | if (ret) |
| 3040 | return ret; |
| 3041 | } |
| 3042 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3043 | old_write_domain = obj->write_domain; |
| 3044 | old_read_domains = obj->read_domains; |
| 3045 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3046 | /* Flush the CPU cache if it's still invalid. */ |
| 3047 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
| 3048 | i915_gem_clflush_object(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3049 | |
| 3050 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 3051 | } |
| 3052 | |
| 3053 | /* It should now be out of any other write domains, and we can update |
| 3054 | * the domain values for our changes. |
| 3055 | */ |
| 3056 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 3057 | |
| 3058 | /* If we're writing through the CPU, then the GPU read domains will |
| 3059 | * need to be invalidated at next use. |
| 3060 | */ |
| 3061 | if (write) { |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 3062 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3063 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 3064 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3065 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3066 | trace_i915_gem_object_change_domain(obj, |
| 3067 | old_read_domains, |
| 3068 | old_write_domain); |
| 3069 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 3070 | return 0; |
| 3071 | } |
| 3072 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3073 | /* |
| 3074 | * Set the next domain for the specified object. This |
| 3075 | * may not actually perform the necessary flushing/invaliding though, |
| 3076 | * as that may want to be batched with other set_domain operations |
| 3077 | * |
| 3078 | * This is (we hope) the only really tricky part of gem. The goal |
| 3079 | * is fairly simple -- track which caches hold bits of the object |
| 3080 | * and make sure they remain coherent. A few concrete examples may |
| 3081 | * help to explain how it works. For shorthand, we use the notation |
| 3082 | * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the |
| 3083 | * a pair of read and write domain masks. |
| 3084 | * |
| 3085 | * Case 1: the batch buffer |
| 3086 | * |
| 3087 | * 1. Allocated |
| 3088 | * 2. Written by CPU |
| 3089 | * 3. Mapped to GTT |
| 3090 | * 4. Read by GPU |
| 3091 | * 5. Unmapped from GTT |
| 3092 | * 6. Freed |
| 3093 | * |
| 3094 | * Let's take these a step at a time |
| 3095 | * |
| 3096 | * 1. Allocated |
| 3097 | * Pages allocated from the kernel may still have |
| 3098 | * cache contents, so we set them to (CPU, CPU) always. |
| 3099 | * 2. Written by CPU (using pwrite) |
| 3100 | * The pwrite function calls set_domain (CPU, CPU) and |
| 3101 | * this function does nothing (as nothing changes) |
| 3102 | * 3. Mapped by GTT |
| 3103 | * This function asserts that the object is not |
| 3104 | * currently in any GPU-based read or write domains |
| 3105 | * 4. Read by GPU |
| 3106 | * i915_gem_execbuffer calls set_domain (COMMAND, 0). |
| 3107 | * As write_domain is zero, this function adds in the |
| 3108 | * current read domains (CPU+COMMAND, 0). |
| 3109 | * flush_domains is set to CPU. |
| 3110 | * invalidate_domains is set to COMMAND |
| 3111 | * clflush is run to get data out of the CPU caches |
| 3112 | * then i915_dev_set_domain calls i915_gem_flush to |
| 3113 | * emit an MI_FLUSH and drm_agp_chipset_flush |
| 3114 | * 5. Unmapped from GTT |
| 3115 | * i915_gem_object_unbind calls set_domain (CPU, CPU) |
| 3116 | * flush_domains and invalidate_domains end up both zero |
| 3117 | * so no flushing/invalidating happens |
| 3118 | * 6. Freed |
| 3119 | * yay, done |
| 3120 | * |
| 3121 | * Case 2: The shared render buffer |
| 3122 | * |
| 3123 | * 1. Allocated |
| 3124 | * 2. Mapped to GTT |
| 3125 | * 3. Read/written by GPU |
| 3126 | * 4. set_domain to (CPU,CPU) |
| 3127 | * 5. Read/written by CPU |
| 3128 | * 6. Read/written by GPU |
| 3129 | * |
| 3130 | * 1. Allocated |
| 3131 | * Same as last example, (CPU, CPU) |
| 3132 | * 2. Mapped to GTT |
| 3133 | * Nothing changes (assertions find that it is not in the GPU) |
| 3134 | * 3. Read/written by GPU |
| 3135 | * execbuffer calls set_domain (RENDER, RENDER) |
| 3136 | * flush_domains gets CPU |
| 3137 | * invalidate_domains gets GPU |
| 3138 | * clflush (obj) |
| 3139 | * MI_FLUSH and drm_agp_chipset_flush |
| 3140 | * 4. set_domain (CPU, CPU) |
| 3141 | * flush_domains gets GPU |
| 3142 | * invalidate_domains gets CPU |
| 3143 | * wait_rendering (obj) to make sure all drawing is complete. |
| 3144 | * This will include an MI_FLUSH to get the data from GPU |
| 3145 | * to memory |
| 3146 | * clflush (obj) to invalidate the CPU cache |
| 3147 | * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) |
| 3148 | * 5. Read/written by CPU |
| 3149 | * cache lines are loaded and dirtied |
| 3150 | * 6. Read written by GPU |
| 3151 | * Same as last GPU access |
| 3152 | * |
| 3153 | * Case 3: The constant buffer |
| 3154 | * |
| 3155 | * 1. Allocated |
| 3156 | * 2. Written by CPU |
| 3157 | * 3. Read by GPU |
| 3158 | * 4. Updated (written) by CPU again |
| 3159 | * 5. Read by GPU |
| 3160 | * |
| 3161 | * 1. Allocated |
| 3162 | * (CPU, CPU) |
| 3163 | * 2. Written by CPU |
| 3164 | * (CPU, CPU) |
| 3165 | * 3. Read by GPU |
| 3166 | * (CPU+RENDER, 0) |
| 3167 | * flush_domains = CPU |
| 3168 | * invalidate_domains = RENDER |
| 3169 | * clflush (obj) |
| 3170 | * MI_FLUSH |
| 3171 | * drm_agp_chipset_flush |
| 3172 | * 4. Updated (written) by CPU again |
| 3173 | * (CPU, CPU) |
| 3174 | * flush_domains = 0 (no previous write domain) |
| 3175 | * invalidate_domains = 0 (no new read domains) |
| 3176 | * 5. Read by GPU |
| 3177 | * (CPU+RENDER, 0) |
| 3178 | * flush_domains = CPU |
| 3179 | * invalidate_domains = RENDER |
| 3180 | * clflush (obj) |
| 3181 | * MI_FLUSH |
| 3182 | * drm_agp_chipset_flush |
| 3183 | */ |
Keith Packard | c0d9082 | 2008-11-20 23:11:08 -0800 | [diff] [blame] | 3184 | static void |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3185 | i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj, |
| 3186 | struct intel_ring_buffer *ring) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3187 | { |
| 3188 | struct drm_device *dev = obj->dev; |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 3189 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3190 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3191 | uint32_t invalidate_domains = 0; |
| 3192 | uint32_t flush_domains = 0; |
Jesse Barnes | 652c393 | 2009-08-17 13:31:43 -0700 | [diff] [blame] | 3193 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3194 | /* |
| 3195 | * If the object isn't moving to a new write domain, |
| 3196 | * let the object stay in multiple read domains |
| 3197 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3198 | if (obj->pending_write_domain == 0) |
| 3199 | obj->pending_read_domains |= obj->read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3200 | |
| 3201 | /* |
| 3202 | * Flush the current write domain if |
| 3203 | * the new read domains don't match. Invalidate |
| 3204 | * any read domains which differ from the old |
| 3205 | * write domain |
| 3206 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3207 | if (obj->write_domain && |
| 3208 | obj->write_domain != obj->pending_read_domains) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3209 | flush_domains |= obj->write_domain; |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3210 | invalidate_domains |= |
| 3211 | obj->pending_read_domains & ~obj->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3212 | } |
| 3213 | /* |
| 3214 | * Invalidate any read caches which may have |
| 3215 | * stale data. That is, any new read domains. |
| 3216 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3217 | invalidate_domains |= obj->pending_read_domains & ~obj->read_domains; |
Chris Wilson | 3d2a812 | 2010-09-29 11:39:53 +0100 | [diff] [blame] | 3218 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3219 | i915_gem_clflush_object(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3220 | |
Eric Anholt | efbeed9 | 2009-02-19 14:54:51 -0800 | [diff] [blame] | 3221 | /* The actual obj->write_domain will be updated with |
| 3222 | * pending_write_domain after we emit the accumulated flush for all |
| 3223 | * of our domain changes in execbuffers (which clears objects' |
| 3224 | * write_domains). So if we have a current write domain that we |
| 3225 | * aren't changing, set pending_write_domain to that. |
| 3226 | */ |
| 3227 | if (flush_domains == 0 && obj->pending_write_domain == 0) |
| 3228 | obj->pending_write_domain = obj->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3229 | |
| 3230 | dev->invalidate_domains |= invalidate_domains; |
| 3231 | dev->flush_domains |= flush_domains; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3232 | if (flush_domains & I915_GEM_GPU_DOMAINS) |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 3233 | dev_priv->mm.flush_rings |= obj_priv->ring->id; |
Chris Wilson | b665145 | 2010-10-23 10:15:06 +0100 | [diff] [blame] | 3234 | if (invalidate_domains & I915_GEM_GPU_DOMAINS) |
| 3235 | dev_priv->mm.flush_rings |= ring->id; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3236 | } |
| 3237 | |
| 3238 | /** |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3239 | * Moves the object from a partially CPU read to a full one. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3240 | * |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3241 | * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(), |
| 3242 | * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU). |
| 3243 | */ |
| 3244 | static void |
| 3245 | i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj) |
| 3246 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3247 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3248 | |
| 3249 | if (!obj_priv->page_cpu_valid) |
| 3250 | return; |
| 3251 | |
| 3252 | /* If we're partially in the CPU read domain, finish moving it in. |
| 3253 | */ |
| 3254 | if (obj->read_domains & I915_GEM_DOMAIN_CPU) { |
| 3255 | int i; |
| 3256 | |
| 3257 | for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) { |
| 3258 | if (obj_priv->page_cpu_valid[i]) |
| 3259 | continue; |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3260 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3261 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3262 | } |
| 3263 | |
| 3264 | /* Free the page_cpu_valid mappings which are now stale, whether |
| 3265 | * or not we've got I915_GEM_DOMAIN_CPU. |
| 3266 | */ |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3267 | kfree(obj_priv->page_cpu_valid); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3268 | obj_priv->page_cpu_valid = NULL; |
| 3269 | } |
| 3270 | |
| 3271 | /** |
| 3272 | * Set the CPU read domain on a range of the object. |
| 3273 | * |
| 3274 | * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's |
| 3275 | * not entirely valid. The page_cpu_valid member of the object flags which |
| 3276 | * pages have been flushed, and will be respected by |
| 3277 | * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping |
| 3278 | * of the whole object. |
| 3279 | * |
| 3280 | * This function returns when the move is complete, including waiting on |
| 3281 | * flushes to occur. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3282 | */ |
| 3283 | static int |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3284 | i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 3285 | uint64_t offset, uint64_t size) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3286 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3287 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3288 | uint32_t old_read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3289 | int i, ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3290 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3291 | if (offset == 0 && size == obj->size) |
| 3292 | return i915_gem_object_set_to_cpu_domain(obj, 0); |
| 3293 | |
Daniel Vetter | ba3d8d7 | 2010-02-11 22:37:04 +0100 | [diff] [blame] | 3294 | ret = i915_gem_object_flush_gpu_write_domain(obj, false); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3295 | if (ret != 0) |
| 3296 | return ret; |
| 3297 | i915_gem_object_flush_gtt_write_domain(obj); |
| 3298 | |
| 3299 | /* If we're already fully in the CPU read domain, we're done. */ |
| 3300 | if (obj_priv->page_cpu_valid == NULL && |
| 3301 | (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3302 | return 0; |
| 3303 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3304 | /* Otherwise, create/clear the per-page CPU read domain flag if we're |
| 3305 | * newly adding I915_GEM_DOMAIN_CPU |
| 3306 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3307 | if (obj_priv->page_cpu_valid == NULL) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3308 | obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE, |
| 3309 | GFP_KERNEL); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3310 | if (obj_priv->page_cpu_valid == NULL) |
| 3311 | return -ENOMEM; |
| 3312 | } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) |
| 3313 | memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3314 | |
| 3315 | /* Flush the cache on any pages that are still invalid from the CPU's |
| 3316 | * perspective. |
| 3317 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3318 | for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; |
| 3319 | i++) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3320 | if (obj_priv->page_cpu_valid[i]) |
| 3321 | continue; |
| 3322 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3323 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3324 | |
| 3325 | obj_priv->page_cpu_valid[i] = 1; |
| 3326 | } |
| 3327 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3328 | /* It should now be out of any other write domains, and we can update |
| 3329 | * the domain values for our changes. |
| 3330 | */ |
| 3331 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 3332 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3333 | old_read_domains = obj->read_domains; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3334 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 3335 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 3336 | trace_i915_gem_object_change_domain(obj, |
| 3337 | old_read_domains, |
| 3338 | obj->write_domain); |
| 3339 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3340 | return 0; |
| 3341 | } |
| 3342 | |
| 3343 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3344 | * Pin an object to the GTT and evaluate the relocations landing in it. |
| 3345 | */ |
| 3346 | static int |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3347 | i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj, |
| 3348 | struct drm_file *file_priv, |
| 3349 | struct drm_i915_gem_exec_object2 *entry) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3350 | { |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3351 | struct drm_device *dev = obj->base.dev; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 3352 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3353 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3354 | struct drm_gem_object *target_obj = NULL; |
| 3355 | uint32_t target_handle = 0; |
| 3356 | int i, ret = 0; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3357 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3358 | user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3359 | for (i = 0; i < entry->relocation_count; i++) { |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3360 | struct drm_i915_gem_relocation_entry reloc; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3361 | uint32_t target_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3362 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3363 | if (__copy_from_user_inatomic(&reloc, |
| 3364 | user_relocs+i, |
| 3365 | sizeof(reloc))) { |
| 3366 | ret = -EFAULT; |
| 3367 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3368 | } |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3369 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3370 | if (reloc.target_handle != target_handle) { |
| 3371 | drm_gem_object_unreference(target_obj); |
| 3372 | |
| 3373 | target_obj = drm_gem_object_lookup(dev, file_priv, |
| 3374 | reloc.target_handle); |
| 3375 | if (target_obj == NULL) { |
| 3376 | ret = -ENOENT; |
| 3377 | break; |
| 3378 | } |
| 3379 | |
| 3380 | target_handle = reloc.target_handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3381 | } |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3382 | target_offset = to_intel_bo(target_obj)->gtt_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3383 | |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3384 | #if WATCH_RELOC |
| 3385 | DRM_INFO("%s: obj %p offset %08x target %d " |
| 3386 | "read %08x write %08x gtt %08x " |
| 3387 | "presumed %08x delta %08x\n", |
| 3388 | __func__, |
| 3389 | obj, |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3390 | (int) reloc.offset, |
| 3391 | (int) reloc.target_handle, |
| 3392 | (int) reloc.read_domains, |
| 3393 | (int) reloc.write_domain, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3394 | (int) target_offset, |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3395 | (int) reloc.presumed_offset, |
| 3396 | reloc.delta); |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3397 | #endif |
| 3398 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3399 | /* The target buffer should have appeared before us in the |
| 3400 | * exec_object list, so it should have a GTT space bound by now. |
| 3401 | */ |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3402 | if (target_offset == 0) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3403 | DRM_ERROR("No GTT space found for object %d\n", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3404 | reloc.target_handle); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3405 | ret = -EINVAL; |
| 3406 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3407 | } |
| 3408 | |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3409 | /* Validate that the target is in a valid r/w GPU domain */ |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3410 | if (reloc.write_domain & (reloc.write_domain - 1)) { |
Daniel Vetter | 16edd55 | 2010-02-19 11:52:02 +0100 | [diff] [blame] | 3411 | DRM_ERROR("reloc with multiple write domains: " |
| 3412 | "obj %p target %d offset %d " |
| 3413 | "read %08x write %08x", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3414 | obj, reloc.target_handle, |
| 3415 | (int) reloc.offset, |
| 3416 | reloc.read_domains, |
| 3417 | reloc.write_domain); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3418 | ret = -EINVAL; |
| 3419 | break; |
Daniel Vetter | 16edd55 | 2010-02-19 11:52:02 +0100 | [diff] [blame] | 3420 | } |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3421 | if (reloc.write_domain & I915_GEM_DOMAIN_CPU || |
| 3422 | reloc.read_domains & I915_GEM_DOMAIN_CPU) { |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3423 | DRM_ERROR("reloc with read/write CPU domains: " |
| 3424 | "obj %p target %d offset %d " |
| 3425 | "read %08x write %08x", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3426 | obj, reloc.target_handle, |
| 3427 | (int) reloc.offset, |
| 3428 | reloc.read_domains, |
| 3429 | reloc.write_domain); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3430 | ret = -EINVAL; |
| 3431 | break; |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3432 | } |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3433 | if (reloc.write_domain && target_obj->pending_write_domain && |
| 3434 | reloc.write_domain != target_obj->pending_write_domain) { |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3435 | DRM_ERROR("Write domain conflict: " |
| 3436 | "obj %p target %d offset %d " |
| 3437 | "new %08x old %08x\n", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3438 | obj, reloc.target_handle, |
| 3439 | (int) reloc.offset, |
| 3440 | reloc.write_domain, |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3441 | target_obj->pending_write_domain); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3442 | ret = -EINVAL; |
| 3443 | break; |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3444 | } |
| 3445 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3446 | target_obj->pending_read_domains |= reloc.read_domains; |
Chris Wilson | 878a3c3 | 2010-10-22 10:48:12 +0100 | [diff] [blame] | 3447 | target_obj->pending_write_domain |= reloc.write_domain; |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3448 | |
| 3449 | /* If the relocation already has the right value in it, no |
| 3450 | * more work needs to be done. |
| 3451 | */ |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3452 | if (target_offset == reloc.presumed_offset) |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3453 | continue; |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3454 | |
| 3455 | /* Check that the relocation address is valid... */ |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3456 | if (reloc.offset > obj->base.size - 4) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3457 | DRM_ERROR("Relocation beyond object bounds: " |
| 3458 | "obj %p target %d offset %d size %d.\n", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3459 | obj, reloc.target_handle, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3460 | (int) reloc.offset, (int) obj->base.size); |
| 3461 | ret = -EINVAL; |
| 3462 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3463 | } |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3464 | if (reloc.offset & 3) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3465 | DRM_ERROR("Relocation not 4-byte aligned: " |
| 3466 | "obj %p target %d offset %d.\n", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3467 | obj, reloc.target_handle, |
| 3468 | (int) reloc.offset); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3469 | ret = -EINVAL; |
| 3470 | break; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3471 | } |
| 3472 | |
Chris Wilson | 8542a0b | 2009-09-09 21:15:15 +0100 | [diff] [blame] | 3473 | /* and points to somewhere within the target object. */ |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3474 | if (reloc.delta >= target_obj->size) { |
Chris Wilson | cd0b9fb | 2009-09-15 23:23:18 +0100 | [diff] [blame] | 3475 | DRM_ERROR("Relocation beyond target object bounds: " |
| 3476 | "obj %p target %d delta %d size %d.\n", |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3477 | obj, reloc.target_handle, |
| 3478 | (int) reloc.delta, (int) target_obj->size); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3479 | ret = -EINVAL; |
| 3480 | break; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3481 | } |
| 3482 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3483 | reloc.delta += target_offset; |
| 3484 | if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) { |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3485 | uint32_t page_offset = reloc.offset & ~PAGE_MASK; |
| 3486 | char *vaddr; |
| 3487 | |
Linus Torvalds | c48c43e | 2010-10-26 18:57:59 -0700 | [diff] [blame] | 3488 | vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]); |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3489 | *(uint32_t *)(vaddr + page_offset) = reloc.delta; |
Linus Torvalds | c48c43e | 2010-10-26 18:57:59 -0700 | [diff] [blame] | 3490 | kunmap_atomic(vaddr); |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3491 | } else { |
| 3492 | uint32_t __iomem *reloc_entry; |
| 3493 | void __iomem *reloc_page; |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3494 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3495 | ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1); |
| 3496 | if (ret) |
| 3497 | break; |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3498 | |
| 3499 | /* Map the page containing the relocation we're going to perform. */ |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3500 | reloc.offset += obj->gtt_offset; |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3501 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
Linus Torvalds | c48c43e | 2010-10-26 18:57:59 -0700 | [diff] [blame] | 3502 | reloc.offset & PAGE_MASK); |
Chris Wilson | f0c43d9 | 2010-10-14 12:44:48 +0100 | [diff] [blame] | 3503 | reloc_entry = (uint32_t __iomem *) |
| 3504 | (reloc_page + (reloc.offset & ~PAGE_MASK)); |
| 3505 | iowrite32(reloc.delta, reloc_entry); |
Linus Torvalds | c48c43e | 2010-10-26 18:57:59 -0700 | [diff] [blame] | 3506 | io_mapping_unmap_atomic(reloc_page); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3507 | } |
| 3508 | |
Chris Wilson | b5dc608 | 2010-10-20 20:59:57 +0100 | [diff] [blame] | 3509 | /* and update the user's relocation entry */ |
| 3510 | reloc.presumed_offset = target_offset; |
| 3511 | if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset, |
| 3512 | &reloc.presumed_offset, |
| 3513 | sizeof(reloc.presumed_offset))) { |
| 3514 | ret = -EFAULT; |
| 3515 | break; |
| 3516 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3517 | } |
| 3518 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3519 | drm_gem_object_unreference(target_obj); |
| 3520 | return ret; |
| 3521 | } |
| 3522 | |
| 3523 | static int |
| 3524 | i915_gem_execbuffer_pin(struct drm_device *dev, |
| 3525 | struct drm_file *file, |
| 3526 | struct drm_gem_object **object_list, |
| 3527 | struct drm_i915_gem_exec_object2 *exec_list, |
| 3528 | int count) |
| 3529 | { |
| 3530 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3531 | int ret, i, retry; |
| 3532 | |
| 3533 | /* attempt to pin all of the buffers into the GTT */ |
| 3534 | for (retry = 0; retry < 2; retry++) { |
| 3535 | ret = 0; |
| 3536 | for (i = 0; i < count; i++) { |
| 3537 | struct drm_i915_gem_exec_object2 *entry = &exec_list[i]; |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3538 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3539 | bool need_fence = |
| 3540 | entry->flags & EXEC_OBJECT_NEEDS_FENCE && |
| 3541 | obj->tiling_mode != I915_TILING_NONE; |
| 3542 | |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3543 | /* g33/pnv can't fence buffers in the unmappable part */ |
| 3544 | bool need_mappable = |
| 3545 | entry->relocation_count ? true : need_fence; |
| 3546 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3547 | /* Check fence reg constraints and rebind if necessary */ |
| 3548 | if (need_fence && |
| 3549 | !i915_gem_object_fence_offset_ok(&obj->base, |
| 3550 | obj->tiling_mode)) { |
| 3551 | ret = i915_gem_object_unbind(&obj->base); |
| 3552 | if (ret) |
| 3553 | break; |
| 3554 | } |
| 3555 | |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 3556 | ret = i915_gem_object_pin(&obj->base, |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 3557 | entry->alignment, |
| 3558 | need_mappable); |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3559 | if (ret) |
| 3560 | break; |
| 3561 | |
| 3562 | /* |
| 3563 | * Pre-965 chips need a fence register set up in order |
| 3564 | * to properly handle blits to/from tiled surfaces. |
| 3565 | */ |
| 3566 | if (need_fence) { |
| 3567 | ret = i915_gem_object_get_fence_reg(&obj->base, true); |
| 3568 | if (ret) { |
| 3569 | i915_gem_object_unpin(&obj->base); |
| 3570 | break; |
| 3571 | } |
| 3572 | |
| 3573 | dev_priv->fence_regs[obj->fence_reg].gpu = true; |
| 3574 | } |
| 3575 | |
| 3576 | entry->offset = obj->gtt_offset; |
| 3577 | } |
| 3578 | |
| 3579 | while (i--) |
| 3580 | i915_gem_object_unpin(object_list[i]); |
| 3581 | |
| 3582 | if (ret == 0) |
| 3583 | break; |
| 3584 | |
| 3585 | if (ret != -ENOSPC || retry) |
| 3586 | return ret; |
| 3587 | |
| 3588 | ret = i915_gem_evict_everything(dev); |
| 3589 | if (ret) |
| 3590 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3591 | } |
| 3592 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3593 | return 0; |
| 3594 | } |
| 3595 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3596 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 3597 | * emitted over 20 msec ago. |
| 3598 | * |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3599 | * Note that if we were to use the current jiffies each time around the loop, |
| 3600 | * we wouldn't escape the function with any frames outstanding if the time to |
| 3601 | * render a frame was over 20ms. |
| 3602 | * |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3603 | * This should get us reasonable parallelism between CPU and GPU but also |
| 3604 | * relatively low latency when blocking on a particular request to finish. |
| 3605 | */ |
| 3606 | static int |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3607 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3608 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3609 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 3610 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3611 | unsigned long recent_enough = jiffies - msecs_to_jiffies(20); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3612 | struct drm_i915_gem_request *request; |
| 3613 | struct intel_ring_buffer *ring = NULL; |
| 3614 | u32 seqno = 0; |
| 3615 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3616 | |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3617 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3618 | list_for_each_entry(request, &file_priv->mm.request_list, client_list) { |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3619 | if (time_after_eq(request->emitted_jiffies, recent_enough)) |
| 3620 | break; |
| 3621 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3622 | ring = request->ring; |
| 3623 | seqno = request->seqno; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3624 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 3625 | spin_unlock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3626 | |
| 3627 | if (seqno == 0) |
| 3628 | return 0; |
| 3629 | |
| 3630 | ret = 0; |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3631 | if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3632 | /* And wait for the seqno passing without holding any locks and |
| 3633 | * causing extra latency for others. This is safe as the irq |
| 3634 | * generation is designed to be run atomically and so is |
| 3635 | * lockless. |
| 3636 | */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3637 | ring->user_irq_get(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3638 | ret = wait_event_interruptible(ring->irq_queue, |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3639 | i915_seqno_passed(ring->get_seqno(ring), seqno) |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3640 | || atomic_read(&dev_priv->mm.wedged)); |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3641 | ring->user_irq_put(ring); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 3642 | |
| 3643 | if (ret == 0 && atomic_read(&dev_priv->mm.wedged)) |
| 3644 | ret = -EIO; |
| 3645 | } |
| 3646 | |
| 3647 | if (ret == 0) |
| 3648 | queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 3649 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3650 | return ret; |
| 3651 | } |
| 3652 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3653 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3654 | i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec, |
| 3655 | uint64_t exec_offset) |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 3656 | { |
| 3657 | uint32_t exec_start, exec_len; |
| 3658 | |
| 3659 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 3660 | exec_len = (uint32_t) exec->batch_len; |
| 3661 | |
| 3662 | if ((exec_start | exec_len) & 0x7) |
| 3663 | return -EINVAL; |
| 3664 | |
| 3665 | if (!exec_start) |
| 3666 | return -EINVAL; |
| 3667 | |
| 3668 | return 0; |
| 3669 | } |
| 3670 | |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3671 | static int |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3672 | validate_exec_list(struct drm_i915_gem_exec_object2 *exec, |
| 3673 | int count) |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3674 | { |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3675 | int i; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3676 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3677 | for (i = 0; i < count; i++) { |
| 3678 | char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr; |
| 3679 | size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry); |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3680 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3681 | if (!access_ok(VERIFY_READ, ptr, length)) |
| 3682 | return -EFAULT; |
| 3683 | |
Chris Wilson | b5dc608 | 2010-10-20 20:59:57 +0100 | [diff] [blame] | 3684 | /* we may also need to update the presumed offsets */ |
| 3685 | if (!access_ok(VERIFY_WRITE, ptr, length)) |
| 3686 | return -EFAULT; |
| 3687 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3688 | if (fault_in_pages_readable(ptr, length)) |
| 3689 | return -EFAULT; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3690 | } |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3691 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3692 | return 0; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3693 | } |
| 3694 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3695 | static int |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3696 | i915_gem_do_execbuffer(struct drm_device *dev, void *data, |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3697 | struct drm_file *file, |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3698 | struct drm_i915_gem_execbuffer2 *args, |
| 3699 | struct drm_i915_gem_exec_object2 *exec_list) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3700 | { |
| 3701 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3702 | struct drm_gem_object **object_list = NULL; |
| 3703 | struct drm_gem_object *batch_obj; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3704 | struct drm_clip_rect *cliprects = NULL; |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3705 | struct drm_i915_gem_request *request = NULL; |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3706 | int ret, i, flips; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3707 | uint64_t exec_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3708 | |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 3709 | struct intel_ring_buffer *ring = NULL; |
| 3710 | |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 3711 | ret = i915_gem_check_is_wedged(dev); |
| 3712 | if (ret) |
| 3713 | return ret; |
| 3714 | |
Chris Wilson | 2549d6c | 2010-10-14 12:10:41 +0100 | [diff] [blame] | 3715 | ret = validate_exec_list(exec_list, args->buffer_count); |
| 3716 | if (ret) |
| 3717 | return ret; |
| 3718 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3719 | #if WATCH_EXEC |
| 3720 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 3721 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 3722 | #endif |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3723 | switch (args->flags & I915_EXEC_RING_MASK) { |
| 3724 | case I915_EXEC_DEFAULT: |
| 3725 | case I915_EXEC_RENDER: |
| 3726 | ring = &dev_priv->render_ring; |
| 3727 | break; |
| 3728 | case I915_EXEC_BSD: |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3729 | if (!HAS_BSD(dev)) { |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3730 | DRM_ERROR("execbuf with invalid ring (BSD)\n"); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3731 | return -EINVAL; |
| 3732 | } |
| 3733 | ring = &dev_priv->bsd_ring; |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 3734 | break; |
| 3735 | case I915_EXEC_BLT: |
| 3736 | if (!HAS_BLT(dev)) { |
| 3737 | DRM_ERROR("execbuf with invalid ring (BLT)\n"); |
| 3738 | return -EINVAL; |
| 3739 | } |
| 3740 | ring = &dev_priv->blt_ring; |
| 3741 | break; |
| 3742 | default: |
| 3743 | DRM_ERROR("execbuf with unknown ring: %d\n", |
| 3744 | (int)(args->flags & I915_EXEC_RING_MASK)); |
| 3745 | return -EINVAL; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 3746 | } |
| 3747 | |
Eric Anholt | 4f481ed | 2008-09-10 14:22:49 -0700 | [diff] [blame] | 3748 | if (args->buffer_count < 1) { |
| 3749 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 3750 | return -EINVAL; |
| 3751 | } |
Eric Anholt | c8e0f93 | 2009-11-22 03:49:37 +0100 | [diff] [blame] | 3752 | object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count); |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3753 | if (object_list == NULL) { |
| 3754 | DRM_ERROR("Failed to allocate object list for %d buffers\n", |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3755 | args->buffer_count); |
| 3756 | ret = -ENOMEM; |
| 3757 | goto pre_mutex_err; |
| 3758 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3759 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3760 | if (args->num_cliprects != 0) { |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3761 | cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects), |
| 3762 | GFP_KERNEL); |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3763 | if (cliprects == NULL) { |
| 3764 | ret = -ENOMEM; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3765 | goto pre_mutex_err; |
Owain Ainsworth | a40e8d3 | 2010-02-09 14:25:55 +0000 | [diff] [blame] | 3766 | } |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3767 | |
| 3768 | ret = copy_from_user(cliprects, |
| 3769 | (struct drm_clip_rect __user *) |
| 3770 | (uintptr_t) args->cliprects_ptr, |
| 3771 | sizeof(*cliprects) * args->num_cliprects); |
| 3772 | if (ret != 0) { |
| 3773 | DRM_ERROR("copy %d cliprects failed: %d\n", |
| 3774 | args->num_cliprects, ret); |
Dan Carpenter | c877cdc | 2010-06-23 19:03:01 +0200 | [diff] [blame] | 3775 | ret = -EFAULT; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3776 | goto pre_mutex_err; |
| 3777 | } |
| 3778 | } |
| 3779 | |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3780 | request = kzalloc(sizeof(*request), GFP_KERNEL); |
| 3781 | if (request == NULL) { |
| 3782 | ret = -ENOMEM; |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3783 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3784 | } |
| 3785 | |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 3786 | ret = i915_mutex_lock_interruptible(dev); |
| 3787 | if (ret) |
| 3788 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3789 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3790 | if (dev_priv->mm.suspended) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3791 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3792 | ret = -EBUSY; |
| 3793 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3794 | } |
| 3795 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3796 | /* Look up object handles */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3797 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 3798 | struct drm_i915_gem_object *obj_priv; |
| 3799 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3800 | object_list[i] = drm_gem_object_lookup(dev, file, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3801 | exec_list[i].handle); |
| 3802 | if (object_list[i] == NULL) { |
| 3803 | DRM_ERROR("Invalid object handle %d at index %d\n", |
| 3804 | exec_list[i].handle, i); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 3805 | /* prevent error path from reading uninitialized data */ |
| 3806 | args->buffer_count = i + 1; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 3807 | ret = -ENOENT; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3808 | goto err; |
| 3809 | } |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3810 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 3811 | obj_priv = to_intel_bo(object_list[i]); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3812 | if (obj_priv->in_execbuffer) { |
| 3813 | DRM_ERROR("Object %p appears more than once in object list\n", |
| 3814 | object_list[i]); |
Chris Wilson | 0ce907f | 2010-01-23 20:26:35 +0000 | [diff] [blame] | 3815 | /* prevent error path from reading uninitialized data */ |
| 3816 | args->buffer_count = i + 1; |
Chris Wilson | bf79cb9 | 2010-08-04 14:19:46 +0100 | [diff] [blame] | 3817 | ret = -EINVAL; |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3818 | goto err; |
| 3819 | } |
| 3820 | obj_priv->in_execbuffer = true; |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 3821 | } |
| 3822 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3823 | /* Move the objects en-masse into the GTT, evicting if necessary. */ |
| 3824 | ret = i915_gem_execbuffer_pin(dev, file, |
| 3825 | object_list, exec_list, |
| 3826 | args->buffer_count); |
| 3827 | if (ret) |
| 3828 | goto err; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3829 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3830 | /* The objects are in their final locations, apply the relocations. */ |
| 3831 | for (i = 0; i < args->buffer_count; i++) { |
| 3832 | struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]); |
| 3833 | obj->base.pending_read_domains = 0; |
| 3834 | obj->base.pending_write_domain = 0; |
| 3835 | ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3836 | if (ret) |
| 3837 | goto err; |
| 3838 | } |
| 3839 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3840 | /* Set the pending read domains for the batch buffer to COMMAND */ |
| 3841 | batch_obj = object_list[args->buffer_count-1]; |
Chris Wilson | 5f26a2c | 2009-06-06 09:45:58 +0100 | [diff] [blame] | 3842 | if (batch_obj->pending_write_domain) { |
| 3843 | DRM_ERROR("Attempting to use self-modifying batch buffer\n"); |
| 3844 | ret = -EINVAL; |
| 3845 | goto err; |
| 3846 | } |
| 3847 | batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3848 | |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3849 | /* Sanity check the batch buffer */ |
| 3850 | exec_offset = to_intel_bo(batch_obj)->gtt_offset; |
| 3851 | ret = i915_gem_check_execbuffer(args, exec_offset); |
Chris Wilson | 83d6079 | 2009-06-06 09:45:57 +0100 | [diff] [blame] | 3852 | if (ret != 0) { |
| 3853 | DRM_ERROR("execbuf with invalid offset/length\n"); |
| 3854 | goto err; |
| 3855 | } |
| 3856 | |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3857 | /* Zero the global flush/invalidate flags. These |
| 3858 | * will be modified as new domains are computed |
| 3859 | * for each object |
| 3860 | */ |
| 3861 | dev->invalidate_domains = 0; |
| 3862 | dev->flush_domains = 0; |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 3863 | dev_priv->mm.flush_rings = 0; |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 3864 | for (i = 0; i < args->buffer_count; i++) |
| 3865 | i915_gem_object_set_to_gpu_domain(object_list[i], ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3866 | |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3867 | if (dev->invalidate_domains | dev->flush_domains) { |
| 3868 | #if WATCH_EXEC |
| 3869 | DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n", |
| 3870 | __func__, |
| 3871 | dev->invalidate_domains, |
| 3872 | dev->flush_domains); |
| 3873 | #endif |
Chris Wilson | 9af90d1 | 2010-10-17 10:01:56 +0100 | [diff] [blame] | 3874 | i915_gem_flush(dev, file, |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3875 | dev->invalidate_domains, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 3876 | dev->flush_domains, |
| 3877 | dev_priv->mm.flush_rings); |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3878 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3879 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3880 | #if WATCH_COHERENCY |
| 3881 | for (i = 0; i < args->buffer_count; i++) { |
| 3882 | i915_gem_object_check_coherency(object_list[i], |
| 3883 | exec_list[i].handle); |
| 3884 | } |
| 3885 | #endif |
| 3886 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3887 | #if WATCH_EXEC |
Ben Gamari | 6911a9b | 2009-04-02 11:24:54 -0700 | [diff] [blame] | 3888 | i915_gem_dump_object(batch_obj, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3889 | args->batch_len, |
| 3890 | __func__, |
| 3891 | ~0); |
| 3892 | #endif |
| 3893 | |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 3894 | /* Check for any pending flips. As we only maintain a flip queue depth |
| 3895 | * of 1, we can simply insert a WAIT for the next display flip prior |
| 3896 | * to executing the batch and avoid stalling the CPU. |
| 3897 | */ |
| 3898 | flips = 0; |
| 3899 | for (i = 0; i < args->buffer_count; i++) { |
| 3900 | if (object_list[i]->write_domain) |
| 3901 | flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip); |
| 3902 | } |
| 3903 | if (flips) { |
| 3904 | int plane, flip_mask; |
| 3905 | |
| 3906 | for (plane = 0; flips >> plane; plane++) { |
| 3907 | if (((flips >> plane) & 1) == 0) |
| 3908 | continue; |
| 3909 | |
| 3910 | if (plane) |
| 3911 | flip_mask = MI_WAIT_FOR_PLANE_B_FLIP; |
| 3912 | else |
| 3913 | flip_mask = MI_WAIT_FOR_PLANE_A_FLIP; |
| 3914 | |
Chris Wilson | e1f99ce | 2010-10-27 12:45:26 +0100 | [diff] [blame] | 3915 | ret = intel_ring_begin(ring, 2); |
| 3916 | if (ret) |
| 3917 | goto err; |
| 3918 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3919 | intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask); |
| 3920 | intel_ring_emit(ring, MI_NOOP); |
| 3921 | intel_ring_advance(ring); |
Chris Wilson | e59f2ba | 2010-10-07 17:28:15 +0100 | [diff] [blame] | 3922 | } |
| 3923 | } |
| 3924 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3925 | /* Exec the batchbuffer */ |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 3926 | ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3927 | if (ret) { |
| 3928 | DRM_ERROR("dispatch failed %d\n", ret); |
| 3929 | goto err; |
| 3930 | } |
| 3931 | |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 3932 | for (i = 0; i < args->buffer_count; i++) { |
| 3933 | struct drm_gem_object *obj = object_list[i]; |
| 3934 | |
| 3935 | obj->read_domains = obj->pending_read_domains; |
| 3936 | obj->write_domain = obj->pending_write_domain; |
| 3937 | |
| 3938 | i915_gem_object_move_to_active(obj, ring); |
| 3939 | if (obj->write_domain) { |
| 3940 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 3941 | obj_priv->dirty = 1; |
| 3942 | list_move_tail(&obj_priv->gpu_write_list, |
| 3943 | &ring->gpu_write_list); |
| 3944 | intel_mark_busy(dev, obj); |
| 3945 | } |
| 3946 | |
| 3947 | trace_i915_gem_object_change_domain(obj, |
| 3948 | obj->read_domains, |
| 3949 | obj->write_domain); |
| 3950 | } |
| 3951 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3952 | /* |
| 3953 | * Ensure that the commands in the batch buffer are |
| 3954 | * finished before the interrupt fires |
| 3955 | */ |
Daniel Vetter | 8a1a49f | 2010-02-11 22:29:04 +0100 | [diff] [blame] | 3956 | i915_retire_commands(dev, ring); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3957 | |
Chris Wilson | 3cce469 | 2010-10-27 16:11:02 +0100 | [diff] [blame] | 3958 | if (i915_add_request(dev, file, request, ring)) |
| 3959 | ring->outstanding_lazy_request = true; |
| 3960 | else |
| 3961 | request = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3962 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3963 | err: |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3964 | for (i = 0; i < args->buffer_count; i++) { |
Chris Wilson | 7e318e1 | 2010-10-27 13:43:39 +0100 | [diff] [blame] | 3965 | if (object_list[i] == NULL) |
| 3966 | break; |
| 3967 | |
| 3968 | to_intel_bo(object_list[i])->in_execbuffer = false; |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 3969 | drm_gem_object_unreference(object_list[i]); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3970 | } |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 3971 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3972 | mutex_unlock(&dev->struct_mutex); |
| 3973 | |
Chris Wilson | 93533c2 | 2010-01-31 10:40:48 +0000 | [diff] [blame] | 3974 | pre_mutex_err: |
Jesse Barnes | 8e7d2b2 | 2009-05-08 16:13:25 -0700 | [diff] [blame] | 3975 | drm_free_large(object_list); |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 3976 | kfree(cliprects); |
Chris Wilson | 8dc5d14 | 2010-08-12 12:36:12 +0100 | [diff] [blame] | 3977 | kfree(request); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3978 | |
| 3979 | return ret; |
| 3980 | } |
| 3981 | |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 3982 | /* |
| 3983 | * Legacy execbuffer just creates an exec2 list from the original exec object |
| 3984 | * list array and passes it to the real function. |
| 3985 | */ |
| 3986 | int |
| 3987 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 3988 | struct drm_file *file_priv) |
| 3989 | { |
| 3990 | struct drm_i915_gem_execbuffer *args = data; |
| 3991 | struct drm_i915_gem_execbuffer2 exec2; |
| 3992 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 3993 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 3994 | int ret, i; |
| 3995 | |
| 3996 | #if WATCH_EXEC |
| 3997 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 3998 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 3999 | #endif |
| 4000 | |
| 4001 | if (args->buffer_count < 1) { |
| 4002 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 4003 | return -EINVAL; |
| 4004 | } |
| 4005 | |
| 4006 | /* Copy in the exec list from userland */ |
| 4007 | exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count); |
| 4008 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4009 | if (exec_list == NULL || exec2_list == NULL) { |
| 4010 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4011 | args->buffer_count); |
| 4012 | drm_free_large(exec_list); |
| 4013 | drm_free_large(exec2_list); |
| 4014 | return -ENOMEM; |
| 4015 | } |
| 4016 | ret = copy_from_user(exec_list, |
| 4017 | (struct drm_i915_relocation_entry __user *) |
| 4018 | (uintptr_t) args->buffers_ptr, |
| 4019 | sizeof(*exec_list) * args->buffer_count); |
| 4020 | if (ret != 0) { |
| 4021 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4022 | args->buffer_count, ret); |
| 4023 | drm_free_large(exec_list); |
| 4024 | drm_free_large(exec2_list); |
| 4025 | return -EFAULT; |
| 4026 | } |
| 4027 | |
| 4028 | for (i = 0; i < args->buffer_count; i++) { |
| 4029 | exec2_list[i].handle = exec_list[i].handle; |
| 4030 | exec2_list[i].relocation_count = exec_list[i].relocation_count; |
| 4031 | exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr; |
| 4032 | exec2_list[i].alignment = exec_list[i].alignment; |
| 4033 | exec2_list[i].offset = exec_list[i].offset; |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4034 | if (INTEL_INFO(dev)->gen < 4) |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4035 | exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE; |
| 4036 | else |
| 4037 | exec2_list[i].flags = 0; |
| 4038 | } |
| 4039 | |
| 4040 | exec2.buffers_ptr = args->buffers_ptr; |
| 4041 | exec2.buffer_count = args->buffer_count; |
| 4042 | exec2.batch_start_offset = args->batch_start_offset; |
| 4043 | exec2.batch_len = args->batch_len; |
| 4044 | exec2.DR1 = args->DR1; |
| 4045 | exec2.DR4 = args->DR4; |
| 4046 | exec2.num_cliprects = args->num_cliprects; |
| 4047 | exec2.cliprects_ptr = args->cliprects_ptr; |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4048 | exec2.flags = I915_EXEC_RENDER; |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4049 | |
| 4050 | ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list); |
| 4051 | if (!ret) { |
| 4052 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4053 | for (i = 0; i < args->buffer_count; i++) |
| 4054 | exec_list[i].offset = exec2_list[i].offset; |
| 4055 | /* ... and back out to userspace */ |
| 4056 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4057 | (uintptr_t) args->buffers_ptr, |
| 4058 | exec_list, |
| 4059 | sizeof(*exec_list) * args->buffer_count); |
| 4060 | if (ret) { |
| 4061 | ret = -EFAULT; |
| 4062 | DRM_ERROR("failed to copy %d exec entries " |
| 4063 | "back to user (%d)\n", |
| 4064 | args->buffer_count, ret); |
| 4065 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4066 | } |
| 4067 | |
| 4068 | drm_free_large(exec_list); |
| 4069 | drm_free_large(exec2_list); |
| 4070 | return ret; |
| 4071 | } |
| 4072 | |
| 4073 | int |
| 4074 | i915_gem_execbuffer2(struct drm_device *dev, void *data, |
| 4075 | struct drm_file *file_priv) |
| 4076 | { |
| 4077 | struct drm_i915_gem_execbuffer2 *args = data; |
| 4078 | struct drm_i915_gem_exec_object2 *exec2_list = NULL; |
| 4079 | int ret; |
| 4080 | |
| 4081 | #if WATCH_EXEC |
| 4082 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 4083 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 4084 | #endif |
| 4085 | |
| 4086 | if (args->buffer_count < 1) { |
| 4087 | DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count); |
| 4088 | return -EINVAL; |
| 4089 | } |
| 4090 | |
| 4091 | exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count); |
| 4092 | if (exec2_list == NULL) { |
| 4093 | DRM_ERROR("Failed to allocate exec list for %d buffers\n", |
| 4094 | args->buffer_count); |
| 4095 | return -ENOMEM; |
| 4096 | } |
| 4097 | ret = copy_from_user(exec2_list, |
| 4098 | (struct drm_i915_relocation_entry __user *) |
| 4099 | (uintptr_t) args->buffers_ptr, |
| 4100 | sizeof(*exec2_list) * args->buffer_count); |
| 4101 | if (ret != 0) { |
| 4102 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 4103 | args->buffer_count, ret); |
| 4104 | drm_free_large(exec2_list); |
| 4105 | return -EFAULT; |
| 4106 | } |
| 4107 | |
| 4108 | ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list); |
| 4109 | if (!ret) { |
| 4110 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 4111 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 4112 | (uintptr_t) args->buffers_ptr, |
| 4113 | exec2_list, |
| 4114 | sizeof(*exec2_list) * args->buffer_count); |
| 4115 | if (ret) { |
| 4116 | ret = -EFAULT; |
| 4117 | DRM_ERROR("failed to copy %d exec entries " |
| 4118 | "back to user (%d)\n", |
| 4119 | args->buffer_count, ret); |
| 4120 | } |
| 4121 | } |
| 4122 | |
| 4123 | drm_free_large(exec2_list); |
| 4124 | return ret; |
| 4125 | } |
| 4126 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4127 | int |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 4128 | i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment, |
| 4129 | bool mappable) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4130 | { |
| 4131 | struct drm_device *dev = obj->dev; |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4132 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4133 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4134 | int ret; |
| 4135 | |
Daniel Vetter | 778c354 | 2010-05-13 11:49:44 +0200 | [diff] [blame] | 4136 | BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT); |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4137 | WARN_ON(i915_verify_lists(dev)); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4138 | |
| 4139 | if (obj_priv->gtt_space != NULL) { |
| 4140 | if (alignment == 0) |
| 4141 | alignment = i915_gem_get_gtt_alignment(obj); |
Daniel Vetter | 16e809a | 2010-09-16 19:37:04 +0200 | [diff] [blame] | 4142 | if (obj_priv->gtt_offset & (alignment - 1) || |
| 4143 | (mappable && !i915_gem_object_cpu_accessible(obj_priv))) { |
Chris Wilson | ae7d49d | 2010-08-04 12:37:41 +0100 | [diff] [blame] | 4144 | WARN(obj_priv->pin_count, |
| 4145 | "bo is already pinned with incorrect alignment:" |
| 4146 | " offset=%x, req.alignment=%x\n", |
| 4147 | obj_priv->gtt_offset, alignment); |
Chris Wilson | ac0c6b5 | 2010-05-27 13:18:18 +0100 | [diff] [blame] | 4148 | ret = i915_gem_object_unbind(obj); |
| 4149 | if (ret) |
| 4150 | return ret; |
| 4151 | } |
| 4152 | } |
| 4153 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4154 | if (obj_priv->gtt_space == NULL) { |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 4155 | ret = i915_gem_object_bind_to_gtt(obj, alignment, mappable); |
Chris Wilson | 9731129 | 2009-09-21 00:22:34 +0100 | [diff] [blame] | 4156 | if (ret) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4157 | return ret; |
Chris Wilson | 22c344e | 2009-02-11 14:26:45 +0000 | [diff] [blame] | 4158 | } |
Jesse Barnes | 76446ca | 2009-12-17 22:05:42 -0500 | [diff] [blame] | 4159 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4160 | obj_priv->pin_count++; |
| 4161 | |
| 4162 | /* If the object is not active and not pending a flush, |
| 4163 | * remove it from the inactive list |
| 4164 | */ |
| 4165 | if (obj_priv->pin_count == 1) { |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 4166 | i915_gem_info_add_pin(dev_priv, obj, mappable); |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4167 | if (!obj_priv->active) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4168 | list_move_tail(&obj_priv->mm_list, |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4169 | &dev_priv->mm.pinned_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4170 | } |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 4171 | BUG_ON(!obj_priv->pin_mappable && mappable); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4172 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4173 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4174 | return 0; |
| 4175 | } |
| 4176 | |
| 4177 | void |
| 4178 | i915_gem_object_unpin(struct drm_gem_object *obj) |
| 4179 | { |
| 4180 | struct drm_device *dev = obj->dev; |
| 4181 | drm_i915_private_t *dev_priv = dev->dev_private; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4182 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4183 | |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4184 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4185 | obj_priv->pin_count--; |
| 4186 | BUG_ON(obj_priv->pin_count < 0); |
| 4187 | BUG_ON(obj_priv->gtt_space == NULL); |
| 4188 | |
| 4189 | /* If the object is no longer pinned, and is |
| 4190 | * neither active nor being flushed, then stick it on |
| 4191 | * the inactive list |
| 4192 | */ |
| 4193 | if (obj_priv->pin_count == 0) { |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4194 | if (!obj_priv->active) |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4195 | list_move_tail(&obj_priv->mm_list, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4196 | &dev_priv->mm.inactive_list); |
Daniel Vetter | fb7d516 | 2010-10-01 22:05:20 +0200 | [diff] [blame] | 4197 | i915_gem_info_remove_pin(dev_priv, obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4198 | } |
Chris Wilson | 23bc598 | 2010-09-29 16:10:57 +0100 | [diff] [blame] | 4199 | WARN_ON(i915_verify_lists(dev)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4200 | } |
| 4201 | |
| 4202 | int |
| 4203 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, |
| 4204 | struct drm_file *file_priv) |
| 4205 | { |
| 4206 | struct drm_i915_gem_pin *args = data; |
| 4207 | struct drm_gem_object *obj; |
| 4208 | struct drm_i915_gem_object *obj_priv; |
| 4209 | int ret; |
| 4210 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4211 | ret = i915_mutex_lock_interruptible(dev); |
| 4212 | if (ret) |
| 4213 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4214 | |
| 4215 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4216 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4217 | ret = -ENOENT; |
| 4218 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4219 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4220 | obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4221 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4222 | if (obj_priv->madv != I915_MADV_WILLNEED) { |
| 4223 | DRM_ERROR("Attempting to pin a purgeable buffer\n"); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4224 | ret = -EINVAL; |
| 4225 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4226 | } |
| 4227 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4228 | if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) { |
| 4229 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
| 4230 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4231 | ret = -EINVAL; |
| 4232 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4233 | } |
| 4234 | |
| 4235 | obj_priv->user_pin_count++; |
| 4236 | obj_priv->pin_filp = file_priv; |
| 4237 | if (obj_priv->user_pin_count == 1) { |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 4238 | ret = i915_gem_object_pin(obj, args->alignment, true); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4239 | if (ret) |
| 4240 | goto out; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4241 | } |
| 4242 | |
| 4243 | /* XXX - flush the CPU caches for pinned objects |
| 4244 | * as the X server doesn't manage domains yet |
| 4245 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 4246 | i915_gem_object_flush_cpu_write_domain(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4247 | args->offset = obj_priv->gtt_offset; |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4248 | out: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4249 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4250 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4251 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4252 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | int |
| 4256 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, |
| 4257 | struct drm_file *file_priv) |
| 4258 | { |
| 4259 | struct drm_i915_gem_pin *args = data; |
| 4260 | struct drm_gem_object *obj; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4261 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4262 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4263 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4264 | ret = i915_mutex_lock_interruptible(dev); |
| 4265 | if (ret) |
| 4266 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4267 | |
| 4268 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4269 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4270 | ret = -ENOENT; |
| 4271 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4272 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4273 | obj_priv = to_intel_bo(obj); |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4274 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4275 | if (obj_priv->pin_filp != file_priv) { |
| 4276 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
| 4277 | args->handle); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4278 | ret = -EINVAL; |
| 4279 | goto out; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4280 | } |
| 4281 | obj_priv->user_pin_count--; |
| 4282 | if (obj_priv->user_pin_count == 0) { |
| 4283 | obj_priv->pin_filp = NULL; |
| 4284 | i915_gem_object_unpin(obj); |
| 4285 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4286 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4287 | out: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4288 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4289 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4290 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4291 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4292 | } |
| 4293 | |
| 4294 | int |
| 4295 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 4296 | struct drm_file *file_priv) |
| 4297 | { |
| 4298 | struct drm_i915_gem_busy *args = data; |
| 4299 | struct drm_gem_object *obj; |
| 4300 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4301 | int ret; |
| 4302 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4303 | ret = i915_mutex_lock_interruptible(dev); |
| 4304 | if (ret) |
| 4305 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4306 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4307 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4308 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4309 | ret = -ENOENT; |
| 4310 | goto unlock; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4311 | } |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4312 | obj_priv = to_intel_bo(obj); |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4313 | |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4314 | /* Count all active objects as busy, even if they are currently not used |
| 4315 | * by the gpu. Users of this interface expect objects to eventually |
| 4316 | * become non-busy without any further actions, therefore emit any |
| 4317 | * necessary flushes here. |
Eric Anholt | c4de0a5 | 2008-12-14 19:05:04 -0800 | [diff] [blame] | 4318 | */ |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4319 | args->busy = obj_priv->active; |
| 4320 | if (args->busy) { |
| 4321 | /* Unconditionally flush objects, even when the gpu still uses this |
| 4322 | * object. Userspace calling this function indicates that it wants to |
| 4323 | * use this buffer rather sooner than later, so issuing the required |
| 4324 | * flush earlier is beneficial. |
| 4325 | */ |
Chris Wilson | c78ec30 | 2010-09-20 12:50:23 +0100 | [diff] [blame] | 4326 | if (obj->write_domain & I915_GEM_GPU_DOMAINS) |
| 4327 | i915_gem_flush_ring(dev, file_priv, |
Chris Wilson | 9220434 | 2010-09-18 11:02:01 +0100 | [diff] [blame] | 4328 | obj_priv->ring, |
| 4329 | 0, obj->write_domain); |
Chris Wilson | 0be555b | 2010-08-04 15:36:30 +0100 | [diff] [blame] | 4330 | |
| 4331 | /* Update the active list for the hardware's current position. |
| 4332 | * Otherwise this only updates on a delayed timer or when irqs |
| 4333 | * are actually unmasked, and our working set ends up being |
| 4334 | * larger than required. |
| 4335 | */ |
| 4336 | i915_gem_retire_requests_ring(dev, obj_priv->ring); |
| 4337 | |
| 4338 | args->busy = obj_priv->active; |
| 4339 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4340 | |
| 4341 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4342 | unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4343 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4344 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | int |
| 4348 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 4349 | struct drm_file *file_priv) |
| 4350 | { |
| 4351 | return i915_gem_ring_throttle(dev, file_priv); |
| 4352 | } |
| 4353 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4354 | int |
| 4355 | i915_gem_madvise_ioctl(struct drm_device *dev, void *data, |
| 4356 | struct drm_file *file_priv) |
| 4357 | { |
| 4358 | struct drm_i915_gem_madvise *args = data; |
| 4359 | struct drm_gem_object *obj; |
| 4360 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 76c1dec | 2010-09-25 11:22:51 +0100 | [diff] [blame] | 4361 | int ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4362 | |
| 4363 | switch (args->madv) { |
| 4364 | case I915_MADV_DONTNEED: |
| 4365 | case I915_MADV_WILLNEED: |
| 4366 | break; |
| 4367 | default: |
| 4368 | return -EINVAL; |
| 4369 | } |
| 4370 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4371 | ret = i915_mutex_lock_interruptible(dev); |
| 4372 | if (ret) |
| 4373 | return ret; |
| 4374 | |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4375 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 4376 | if (obj == NULL) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4377 | ret = -ENOENT; |
| 4378 | goto unlock; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4379 | } |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4380 | obj_priv = to_intel_bo(obj); |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4381 | |
| 4382 | if (obj_priv->pin_count) { |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4383 | ret = -EINVAL; |
| 4384 | goto out; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4387 | if (obj_priv->madv != __I915_MADV_PURGED) |
| 4388 | obj_priv->madv = args->madv; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4389 | |
Chris Wilson | 2d7ef39 | 2009-09-20 23:13:10 +0100 | [diff] [blame] | 4390 | /* if the object is no longer bound, discard its backing storage */ |
| 4391 | if (i915_gem_object_is_purgeable(obj_priv) && |
| 4392 | obj_priv->gtt_space == NULL) |
| 4393 | i915_gem_object_truncate(obj); |
| 4394 | |
Chris Wilson | bb6baf7 | 2009-09-22 14:24:13 +0100 | [diff] [blame] | 4395 | args->retained = obj_priv->madv != __I915_MADV_PURGED; |
| 4396 | |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4397 | out: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4398 | drm_gem_object_unreference(obj); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4399 | unlock: |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4400 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | 1d7cfea | 2010-10-17 09:45:41 +0100 | [diff] [blame] | 4401 | return ret; |
Chris Wilson | 3ef94da | 2009-09-14 16:50:29 +0100 | [diff] [blame] | 4402 | } |
| 4403 | |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4404 | struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev, |
| 4405 | size_t size) |
| 4406 | { |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4407 | struct drm_i915_private *dev_priv = dev->dev_private; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4408 | struct drm_i915_gem_object *obj; |
| 4409 | |
| 4410 | obj = kzalloc(sizeof(*obj), GFP_KERNEL); |
| 4411 | if (obj == NULL) |
| 4412 | return NULL; |
| 4413 | |
| 4414 | if (drm_gem_object_init(dev, &obj->base, size) != 0) { |
| 4415 | kfree(obj); |
| 4416 | return NULL; |
| 4417 | } |
| 4418 | |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4419 | i915_gem_info_add_obj(dev_priv, size); |
| 4420 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4421 | obj->base.write_domain = I915_GEM_DOMAIN_CPU; |
| 4422 | obj->base.read_domains = I915_GEM_DOMAIN_CPU; |
| 4423 | |
| 4424 | obj->agp_type = AGP_USER_MEMORY; |
Daniel Vetter | 62b8b21 | 2010-04-09 19:05:08 +0000 | [diff] [blame] | 4425 | obj->base.driver_private = NULL; |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4426 | obj->fence_reg = I915_FENCE_REG_NONE; |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4427 | INIT_LIST_HEAD(&obj->mm_list); |
| 4428 | INIT_LIST_HEAD(&obj->ring_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4429 | INIT_LIST_HEAD(&obj->gpu_write_list); |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4430 | obj->madv = I915_MADV_WILLNEED; |
| 4431 | |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4432 | return &obj->base; |
Daniel Vetter | ac52bc5 | 2010-04-09 19:05:06 +0000 | [diff] [blame] | 4433 | } |
| 4434 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4435 | int i915_gem_init_object(struct drm_gem_object *obj) |
| 4436 | { |
Daniel Vetter | c397b90 | 2010-04-09 19:05:07 +0000 | [diff] [blame] | 4437 | BUG(); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4438 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4439 | return 0; |
| 4440 | } |
| 4441 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4442 | static void i915_gem_free_object_tail(struct drm_gem_object *obj) |
| 4443 | { |
| 4444 | struct drm_device *dev = obj->dev; |
| 4445 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4446 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
| 4447 | int ret; |
| 4448 | |
| 4449 | ret = i915_gem_object_unbind(obj); |
| 4450 | if (ret == -ERESTARTSYS) { |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4451 | list_move(&obj_priv->mm_list, |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4452 | &dev_priv->mm.deferred_free_list); |
| 4453 | return; |
| 4454 | } |
| 4455 | |
Chris Wilson | 39a01d1 | 2010-10-28 13:03:06 +0100 | [diff] [blame^] | 4456 | if (obj->map_list.map) |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4457 | i915_gem_free_mmap_offset(obj); |
| 4458 | |
| 4459 | drm_gem_object_release(obj); |
Chris Wilson | 73aa808 | 2010-09-30 11:46:12 +0100 | [diff] [blame] | 4460 | i915_gem_info_remove_obj(dev_priv, obj->size); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4461 | |
| 4462 | kfree(obj_priv->page_cpu_valid); |
| 4463 | kfree(obj_priv->bit_17); |
| 4464 | kfree(obj_priv); |
| 4465 | } |
| 4466 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4467 | void i915_gem_free_object(struct drm_gem_object *obj) |
| 4468 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4469 | struct drm_device *dev = obj->dev; |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4470 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4471 | |
Chris Wilson | 1c5d22f | 2009-08-25 11:15:50 +0100 | [diff] [blame] | 4472 | trace_i915_gem_object_destroy(obj); |
| 4473 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4474 | while (obj_priv->pin_count > 0) |
| 4475 | i915_gem_object_unpin(obj); |
| 4476 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4477 | if (obj_priv->phys_obj) |
| 4478 | i915_gem_detach_phys_object(dev, obj); |
| 4479 | |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4480 | i915_gem_free_object_tail(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4481 | } |
| 4482 | |
Jesse Barnes | 5669fca | 2009-02-17 15:13:31 -0800 | [diff] [blame] | 4483 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4484 | i915_gem_idle(struct drm_device *dev) |
| 4485 | { |
| 4486 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4487 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4488 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4489 | mutex_lock(&dev->struct_mutex); |
| 4490 | |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4491 | if (dev_priv->mm.suspended) { |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4492 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4493 | return 0; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4494 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4495 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4496 | ret = i915_gpu_idle(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4497 | if (ret) { |
| 4498 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4499 | return ret; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4500 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4501 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4502 | /* Under UMS, be paranoid and evict. */ |
| 4503 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) { |
Chris Wilson | b47eb4a | 2010-08-07 11:01:23 +0100 | [diff] [blame] | 4504 | ret = i915_gem_evict_inactive(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4505 | if (ret) { |
| 4506 | mutex_unlock(&dev->struct_mutex); |
| 4507 | return ret; |
| 4508 | } |
| 4509 | } |
| 4510 | |
| 4511 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
| 4512 | * We need to replace this with a semaphore, or something. |
| 4513 | * And not confound mm.suspended! |
| 4514 | */ |
| 4515 | dev_priv->mm.suspended = 1; |
Daniel Vetter | bc0c7f1 | 2010-08-20 18:18:48 +0200 | [diff] [blame] | 4516 | del_timer_sync(&dev_priv->hangcheck_timer); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4517 | |
| 4518 | i915_kernel_lost_context(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4519 | i915_gem_cleanup_ringbuffer(dev); |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4520 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4521 | mutex_unlock(&dev->struct_mutex); |
| 4522 | |
Chris Wilson | 29105cc | 2010-01-07 10:39:13 +0000 | [diff] [blame] | 4523 | /* Cancel the retire work handler, which should be idle now. */ |
| 4524 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
| 4525 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4526 | return 0; |
| 4527 | } |
| 4528 | |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4529 | /* |
| 4530 | * 965+ support PIPE_CONTROL commands, which provide finer grained control |
| 4531 | * over cache flushing. |
| 4532 | */ |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4533 | static int |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4534 | i915_gem_init_pipe_control(struct drm_device *dev) |
| 4535 | { |
| 4536 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4537 | struct drm_gem_object *obj; |
| 4538 | struct drm_i915_gem_object *obj_priv; |
| 4539 | int ret; |
| 4540 | |
Eric Anholt | 34dc4d4 | 2010-05-07 14:30:03 -0700 | [diff] [blame] | 4541 | obj = i915_gem_alloc_object(dev, 4096); |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4542 | if (obj == NULL) { |
| 4543 | DRM_ERROR("Failed to allocate seqno page\n"); |
| 4544 | ret = -ENOMEM; |
| 4545 | goto err; |
| 4546 | } |
| 4547 | obj_priv = to_intel_bo(obj); |
| 4548 | obj_priv->agp_type = AGP_USER_CACHED_MEMORY; |
| 4549 | |
Daniel Vetter | 920afa7 | 2010-09-16 17:54:23 +0200 | [diff] [blame] | 4550 | ret = i915_gem_object_pin(obj, 4096, true); |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4551 | if (ret) |
| 4552 | goto err_unref; |
| 4553 | |
| 4554 | dev_priv->seqno_gfx_addr = obj_priv->gtt_offset; |
| 4555 | dev_priv->seqno_page = kmap(obj_priv->pages[0]); |
| 4556 | if (dev_priv->seqno_page == NULL) |
| 4557 | goto err_unpin; |
| 4558 | |
| 4559 | dev_priv->seqno_obj = obj; |
| 4560 | memset(dev_priv->seqno_page, 0, PAGE_SIZE); |
| 4561 | |
| 4562 | return 0; |
| 4563 | |
| 4564 | err_unpin: |
| 4565 | i915_gem_object_unpin(obj); |
| 4566 | err_unref: |
| 4567 | drm_gem_object_unreference(obj); |
| 4568 | err: |
| 4569 | return ret; |
| 4570 | } |
| 4571 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4572 | |
| 4573 | static void |
Jesse Barnes | e552eb7 | 2010-04-21 11:39:23 -0700 | [diff] [blame] | 4574 | i915_gem_cleanup_pipe_control(struct drm_device *dev) |
| 4575 | { |
| 4576 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4577 | struct drm_gem_object *obj; |
| 4578 | struct drm_i915_gem_object *obj_priv; |
| 4579 | |
| 4580 | obj = dev_priv->seqno_obj; |
| 4581 | obj_priv = to_intel_bo(obj); |
| 4582 | kunmap(obj_priv->pages[0]); |
| 4583 | i915_gem_object_unpin(obj); |
| 4584 | drm_gem_object_unreference(obj); |
| 4585 | dev_priv->seqno_obj = NULL; |
| 4586 | |
| 4587 | dev_priv->seqno_page = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4588 | } |
| 4589 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4590 | int |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4591 | i915_gem_init_ringbuffer(struct drm_device *dev) |
| 4592 | { |
| 4593 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4594 | int ret; |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4595 | |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4596 | if (HAS_PIPE_CONTROL(dev)) { |
| 4597 | ret = i915_gem_init_pipe_control(dev); |
| 4598 | if (ret) |
| 4599 | return ret; |
| 4600 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4601 | |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4602 | ret = intel_init_render_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4603 | if (ret) |
| 4604 | goto cleanup_pipe_control; |
| 4605 | |
| 4606 | if (HAS_BSD(dev)) { |
Xiang, Haihao | 5c1143b | 2010-09-16 10:43:11 +0800 | [diff] [blame] | 4607 | ret = intel_init_bsd_ring_buffer(dev); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4608 | if (ret) |
| 4609 | goto cleanup_render_ring; |
Zou Nan hai | d1b851f | 2010-05-21 09:08:57 +0800 | [diff] [blame] | 4610 | } |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4611 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4612 | if (HAS_BLT(dev)) { |
| 4613 | ret = intel_init_blt_ring_buffer(dev); |
| 4614 | if (ret) |
| 4615 | goto cleanup_bsd_ring; |
| 4616 | } |
| 4617 | |
Chris Wilson | 6f392d548 | 2010-08-07 11:01:22 +0100 | [diff] [blame] | 4618 | dev_priv->next_seqno = 1; |
| 4619 | |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4620 | return 0; |
| 4621 | |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4622 | cleanup_bsd_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4623 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4624 | cleanup_render_ring: |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4625 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
Chris Wilson | 68f95ba | 2010-05-27 13:18:22 +0100 | [diff] [blame] | 4626 | cleanup_pipe_control: |
| 4627 | if (HAS_PIPE_CONTROL(dev)) |
| 4628 | i915_gem_cleanup_pipe_control(dev); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4629 | return ret; |
| 4630 | } |
| 4631 | |
| 4632 | void |
| 4633 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) |
| 4634 | { |
| 4635 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4636 | |
Chris Wilson | 78501ea | 2010-10-27 12:18:21 +0100 | [diff] [blame] | 4637 | intel_cleanup_ring_buffer(&dev_priv->render_ring); |
| 4638 | intel_cleanup_ring_buffer(&dev_priv->bsd_ring); |
| 4639 | intel_cleanup_ring_buffer(&dev_priv->blt_ring); |
Zou Nan hai | 8187a2b | 2010-05-21 09:08:55 +0800 | [diff] [blame] | 4640 | if (HAS_PIPE_CONTROL(dev)) |
| 4641 | i915_gem_cleanup_pipe_control(dev); |
| 4642 | } |
| 4643 | |
| 4644 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4645 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, |
| 4646 | struct drm_file *file_priv) |
| 4647 | { |
| 4648 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4649 | int ret; |
| 4650 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4651 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4652 | return 0; |
| 4653 | |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4654 | if (atomic_read(&dev_priv->mm.wedged)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4655 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
Ben Gamari | ba1234d | 2009-09-14 17:48:47 -0400 | [diff] [blame] | 4656 | atomic_set(&dev_priv->mm.wedged, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4657 | } |
| 4658 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4659 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4660 | dev_priv->mm.suspended = 0; |
| 4661 | |
| 4662 | ret = i915_gem_init_ringbuffer(dev); |
Wu Fengguang | d816f6a | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4663 | if (ret != 0) { |
| 4664 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4665 | return ret; |
Wu Fengguang | d816f6a | 2009-04-18 10:43:32 +0800 | [diff] [blame] | 4666 | } |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 4667 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4668 | BUG_ON(!list_empty(&dev_priv->mm.active_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4669 | BUG_ON(!list_empty(&dev_priv->render_ring.active_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4670 | BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4671 | BUG_ON(!list_empty(&dev_priv->blt_ring.active_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4672 | BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); |
| 4673 | BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); |
Zou Nan hai | 852835f | 2010-05-21 09:08:56 +0800 | [diff] [blame] | 4674 | BUG_ON(!list_empty(&dev_priv->render_ring.request_list)); |
Chris Wilson | 87acb0a | 2010-10-19 10:13:00 +0100 | [diff] [blame] | 4675 | BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list)); |
Chris Wilson | 549f736 | 2010-10-19 11:19:32 +0100 | [diff] [blame] | 4676 | BUG_ON(!list_empty(&dev_priv->blt_ring.request_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4677 | mutex_unlock(&dev->struct_mutex); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4678 | |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4679 | ret = drm_irq_install(dev); |
| 4680 | if (ret) |
| 4681 | goto cleanup_ringbuffer; |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4682 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4683 | return 0; |
Chris Wilson | 5f35308 | 2010-06-07 14:03:03 +0100 | [diff] [blame] | 4684 | |
| 4685 | cleanup_ringbuffer: |
| 4686 | mutex_lock(&dev->struct_mutex); |
| 4687 | i915_gem_cleanup_ringbuffer(dev); |
| 4688 | dev_priv->mm.suspended = 1; |
| 4689 | mutex_unlock(&dev->struct_mutex); |
| 4690 | |
| 4691 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4692 | } |
| 4693 | |
| 4694 | int |
| 4695 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, |
| 4696 | struct drm_file *file_priv) |
| 4697 | { |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 4698 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4699 | return 0; |
| 4700 | |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 4701 | drm_irq_uninstall(dev); |
Linus Torvalds | e6890f6 | 2009-09-08 17:09:24 -0700 | [diff] [blame] | 4702 | return i915_gem_idle(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4703 | } |
| 4704 | |
| 4705 | void |
| 4706 | i915_gem_lastclose(struct drm_device *dev) |
| 4707 | { |
| 4708 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4709 | |
Eric Anholt | e806b49 | 2009-01-22 09:56:58 -0800 | [diff] [blame] | 4710 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4711 | return; |
| 4712 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 4713 | ret = i915_gem_idle(dev); |
| 4714 | if (ret) |
| 4715 | DRM_ERROR("failed to idle hardware: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4716 | } |
| 4717 | |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4718 | static void |
| 4719 | init_ring_lists(struct intel_ring_buffer *ring) |
| 4720 | { |
| 4721 | INIT_LIST_HEAD(&ring->active_list); |
| 4722 | INIT_LIST_HEAD(&ring->request_list); |
| 4723 | INIT_LIST_HEAD(&ring->gpu_write_list); |
| 4724 | } |
| 4725 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4726 | void |
| 4727 | i915_gem_load(struct drm_device *dev) |
| 4728 | { |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4729 | int i; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4730 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4731 | |
Chris Wilson | 69dc498 | 2010-10-19 10:36:51 +0100 | [diff] [blame] | 4732 | INIT_LIST_HEAD(&dev_priv->mm.active_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4733 | INIT_LIST_HEAD(&dev_priv->mm.flushing_list); |
| 4734 | INIT_LIST_HEAD(&dev_priv->mm.inactive_list); |
Chris Wilson | f13d3f7 | 2010-09-20 17:36:15 +0100 | [diff] [blame] | 4735 | INIT_LIST_HEAD(&dev_priv->mm.pinned_list); |
Eric Anholt | a09ba7f | 2009-08-29 12:49:51 -0700 | [diff] [blame] | 4736 | INIT_LIST_HEAD(&dev_priv->mm.fence_list); |
Chris Wilson | be72615 | 2010-07-23 23:18:50 +0100 | [diff] [blame] | 4737 | INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list); |
Chris Wilson | 6419340 | 2010-10-24 12:38:05 +0100 | [diff] [blame] | 4738 | init_ring_lists(&dev_priv->render_ring); |
| 4739 | init_ring_lists(&dev_priv->bsd_ring); |
| 4740 | init_ring_lists(&dev_priv->blt_ring); |
Daniel Vetter | 007cc8a | 2010-04-28 11:02:31 +0200 | [diff] [blame] | 4741 | for (i = 0; i < 16; i++) |
| 4742 | INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4743 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
| 4744 | i915_gem_retire_work_handler); |
Chris Wilson | 30dbf0c | 2010-09-25 10:19:17 +0100 | [diff] [blame] | 4745 | init_completion(&dev_priv->error_completion); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4746 | |
Dave Airlie | 9440012 | 2010-07-20 13:15:31 +1000 | [diff] [blame] | 4747 | /* On GEN3 we really need to make sure the ARB C3 LP bit is set */ |
| 4748 | if (IS_GEN3(dev)) { |
| 4749 | u32 tmp = I915_READ(MI_ARB_STATE); |
| 4750 | if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) { |
| 4751 | /* arb state is a masked write, so set bit + bit in mask */ |
| 4752 | tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT); |
| 4753 | I915_WRITE(MI_ARB_STATE, tmp); |
| 4754 | } |
| 4755 | } |
| 4756 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4757 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
Eric Anholt | b397c83 | 2010-01-26 09:43:10 -0800 | [diff] [blame] | 4758 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 4759 | dev_priv->fence_reg_start = 3; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4760 | |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4761 | 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] | 4762 | dev_priv->num_fence_regs = 16; |
| 4763 | else |
| 4764 | dev_priv->num_fence_regs = 8; |
| 4765 | |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4766 | /* Initialize fence registers to zero */ |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4767 | switch (INTEL_INFO(dev)->gen) { |
| 4768 | case 6: |
| 4769 | for (i = 0; i < 16; i++) |
| 4770 | I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0); |
| 4771 | break; |
| 4772 | case 5: |
| 4773 | case 4: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4774 | for (i = 0; i < 16; i++) |
| 4775 | I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4776 | break; |
| 4777 | case 3: |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4778 | if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
| 4779 | for (i = 0; i < 8; i++) |
| 4780 | I915_WRITE(FENCE_REG_945_8 + (i * 4), 0); |
Chris Wilson | a6c45cf | 2010-09-17 00:32:17 +0100 | [diff] [blame] | 4781 | case 2: |
| 4782 | for (i = 0; i < 8; i++) |
| 4783 | I915_WRITE(FENCE_REG_830_0 + (i * 4), 0); |
| 4784 | break; |
Grégoire Henry | b5aa8a0 | 2009-06-23 15:41:02 +0200 | [diff] [blame] | 4785 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4786 | i915_gem_detect_bit_6_swizzle(dev); |
Kristian Høgsberg | 6b95a20 | 2009-11-18 11:25:18 -0500 | [diff] [blame] | 4787 | init_waitqueue_head(&dev_priv->pending_flip_queue); |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 4788 | |
| 4789 | dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink; |
| 4790 | dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS; |
| 4791 | register_shrinker(&dev_priv->mm.inactive_shrinker); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4792 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4793 | |
| 4794 | /* |
| 4795 | * Create a physically contiguous memory object for this object |
| 4796 | * e.g. for cursor + overlay regs |
| 4797 | */ |
Chris Wilson | 995b676 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4798 | static int i915_gem_init_phys_object(struct drm_device *dev, |
| 4799 | int id, int size, int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4800 | { |
| 4801 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4802 | struct drm_i915_gem_phys_object *phys_obj; |
| 4803 | int ret; |
| 4804 | |
| 4805 | if (dev_priv->mm.phys_objs[id - 1] || !size) |
| 4806 | return 0; |
| 4807 | |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4808 | phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4809 | if (!phys_obj) |
| 4810 | return -ENOMEM; |
| 4811 | |
| 4812 | phys_obj->id = id; |
| 4813 | |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4814 | phys_obj->handle = drm_pci_alloc(dev, size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4815 | if (!phys_obj->handle) { |
| 4816 | ret = -ENOMEM; |
| 4817 | goto kfree_obj; |
| 4818 | } |
| 4819 | #ifdef CONFIG_X86 |
| 4820 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4821 | #endif |
| 4822 | |
| 4823 | dev_priv->mm.phys_objs[id - 1] = phys_obj; |
| 4824 | |
| 4825 | return 0; |
| 4826 | kfree_obj: |
Eric Anholt | 9a298b2 | 2009-03-24 12:23:04 -0700 | [diff] [blame] | 4827 | kfree(phys_obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4828 | return ret; |
| 4829 | } |
| 4830 | |
Chris Wilson | 995b676 | 2010-08-20 13:23:26 +0100 | [diff] [blame] | 4831 | 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] | 4832 | { |
| 4833 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4834 | struct drm_i915_gem_phys_object *phys_obj; |
| 4835 | |
| 4836 | if (!dev_priv->mm.phys_objs[id - 1]) |
| 4837 | return; |
| 4838 | |
| 4839 | phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4840 | if (phys_obj->cur_obj) { |
| 4841 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); |
| 4842 | } |
| 4843 | |
| 4844 | #ifdef CONFIG_X86 |
| 4845 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4846 | #endif |
| 4847 | drm_pci_free(dev, phys_obj->handle); |
| 4848 | kfree(phys_obj); |
| 4849 | dev_priv->mm.phys_objs[id - 1] = NULL; |
| 4850 | } |
| 4851 | |
| 4852 | void i915_gem_free_all_phys_object(struct drm_device *dev) |
| 4853 | { |
| 4854 | int i; |
| 4855 | |
Dave Airlie | 260883c | 2009-01-22 17:58:49 +1000 | [diff] [blame] | 4856 | 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] | 4857 | i915_gem_free_phys_object(dev, i); |
| 4858 | } |
| 4859 | |
| 4860 | void i915_gem_detach_phys_object(struct drm_device *dev, |
| 4861 | struct drm_gem_object *obj) |
| 4862 | { |
| 4863 | struct drm_i915_gem_object *obj_priv; |
| 4864 | int i; |
| 4865 | int ret; |
| 4866 | int page_count; |
| 4867 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4868 | obj_priv = to_intel_bo(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4869 | if (!obj_priv->phys_obj) |
| 4870 | return; |
| 4871 | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 4872 | ret = i915_gem_object_get_pages(obj, 0); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4873 | if (ret) |
| 4874 | goto out; |
| 4875 | |
| 4876 | page_count = obj->size / PAGE_SIZE; |
| 4877 | |
| 4878 | for (i = 0; i < page_count; i++) { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 4879 | char *dst = kmap_atomic(obj_priv->pages[i]); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4880 | char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
| 4881 | |
| 4882 | memcpy(dst, src, PAGE_SIZE); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 4883 | kunmap_atomic(dst); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4884 | } |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4885 | drm_clflush_pages(obj_priv->pages, page_count); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4886 | drm_agp_chipset_flush(dev); |
Chris Wilson | d78b47b | 2009-06-17 21:52:49 +0100 | [diff] [blame] | 4887 | |
| 4888 | i915_gem_object_put_pages(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4889 | out: |
| 4890 | obj_priv->phys_obj->cur_obj = NULL; |
| 4891 | obj_priv->phys_obj = NULL; |
| 4892 | } |
| 4893 | |
| 4894 | int |
| 4895 | i915_gem_attach_phys_object(struct drm_device *dev, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4896 | struct drm_gem_object *obj, |
| 4897 | int id, |
| 4898 | int align) |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4899 | { |
| 4900 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4901 | struct drm_i915_gem_object *obj_priv; |
| 4902 | int ret = 0; |
| 4903 | int page_count; |
| 4904 | int i; |
| 4905 | |
| 4906 | if (id > I915_MAX_PHYS_OBJECT) |
| 4907 | return -EINVAL; |
| 4908 | |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4909 | obj_priv = to_intel_bo(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4910 | |
| 4911 | if (obj_priv->phys_obj) { |
| 4912 | if (obj_priv->phys_obj->id == id) |
| 4913 | return 0; |
| 4914 | i915_gem_detach_phys_object(dev, obj); |
| 4915 | } |
| 4916 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4917 | /* create a new object */ |
| 4918 | if (!dev_priv->mm.phys_objs[id - 1]) { |
| 4919 | ret = i915_gem_init_phys_object(dev, id, |
Chris Wilson | 6eeefaf | 2010-08-07 11:01:39 +0100 | [diff] [blame] | 4920 | obj->size, align); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4921 | if (ret) { |
Linus Torvalds | aeb565d | 2009-01-26 10:01:53 -0800 | [diff] [blame] | 4922 | DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4923 | goto out; |
| 4924 | } |
| 4925 | } |
| 4926 | |
| 4927 | /* bind to the object */ |
| 4928 | obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4929 | obj_priv->phys_obj->cur_obj = obj; |
| 4930 | |
Chris Wilson | 4bdadb9 | 2010-01-27 13:36:32 +0000 | [diff] [blame] | 4931 | ret = i915_gem_object_get_pages(obj, 0); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4932 | if (ret) { |
| 4933 | DRM_ERROR("failed to get page list\n"); |
| 4934 | goto out; |
| 4935 | } |
| 4936 | |
| 4937 | page_count = obj->size / PAGE_SIZE; |
| 4938 | |
| 4939 | for (i = 0; i < page_count; i++) { |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 4940 | char *src = kmap_atomic(obj_priv->pages[i]); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4941 | char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
| 4942 | |
| 4943 | memcpy(dst, src, PAGE_SIZE); |
Peter Zijlstra | 3e4d3af | 2010-10-26 14:21:51 -0700 | [diff] [blame] | 4944 | kunmap_atomic(src); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4945 | } |
| 4946 | |
Chris Wilson | d78b47b | 2009-06-17 21:52:49 +0100 | [diff] [blame] | 4947 | i915_gem_object_put_pages(obj); |
| 4948 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4949 | return 0; |
| 4950 | out: |
| 4951 | return ret; |
| 4952 | } |
| 4953 | |
| 4954 | static int |
| 4955 | i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 4956 | struct drm_i915_gem_pwrite *args, |
| 4957 | struct drm_file *file_priv) |
| 4958 | { |
Daniel Vetter | 23010e4 | 2010-03-08 13:35:02 +0100 | [diff] [blame] | 4959 | struct drm_i915_gem_object *obj_priv = to_intel_bo(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4960 | void *obj_addr; |
| 4961 | int ret; |
| 4962 | char __user *user_data; |
| 4963 | |
| 4964 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 4965 | obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset; |
| 4966 | |
Zhao Yakui | 44d98a6 | 2009-10-09 11:39:40 +0800 | [diff] [blame] | 4967 | DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4968 | ret = copy_from_user(obj_addr, user_data, args->size); |
| 4969 | if (ret) |
| 4970 | return -EFAULT; |
| 4971 | |
| 4972 | drm_agp_chipset_flush(dev); |
| 4973 | return 0; |
| 4974 | } |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4975 | |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4976 | void i915_gem_release(struct drm_device *dev, struct drm_file *file) |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4977 | { |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4978 | struct drm_i915_file_private *file_priv = file->driver_priv; |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4979 | |
| 4980 | /* Clean up our request list when the client is going away, so that |
| 4981 | * later retire_requests won't dereference our soon-to-be-gone |
| 4982 | * file_priv. |
| 4983 | */ |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4984 | spin_lock(&file_priv->mm.lock); |
Chris Wilson | f787a5f | 2010-09-24 16:02:42 +0100 | [diff] [blame] | 4985 | while (!list_empty(&file_priv->mm.request_list)) { |
| 4986 | struct drm_i915_gem_request *request; |
| 4987 | |
| 4988 | request = list_first_entry(&file_priv->mm.request_list, |
| 4989 | struct drm_i915_gem_request, |
| 4990 | client_list); |
| 4991 | list_del(&request->client_list); |
| 4992 | request->file_priv = NULL; |
| 4993 | } |
Chris Wilson | 1c25595 | 2010-09-26 11:03:27 +0100 | [diff] [blame] | 4994 | spin_unlock(&file_priv->mm.lock); |
Eric Anholt | b962442 | 2009-06-03 07:27:35 +0000 | [diff] [blame] | 4995 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4996 | |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 4997 | static int |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 4998 | i915_gpu_is_active(struct drm_device *dev) |
| 4999 | { |
| 5000 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 5001 | int lists_empty; |
| 5002 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5003 | lists_empty = list_empty(&dev_priv->mm.flushing_list) && |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5004 | list_empty(&dev_priv->mm.active_list); |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5005 | |
| 5006 | return !lists_empty; |
| 5007 | } |
| 5008 | |
| 5009 | static int |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5010 | i915_gem_inactive_shrink(struct shrinker *shrinker, |
| 5011 | int nr_to_scan, |
| 5012 | gfp_t gfp_mask) |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5013 | { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5014 | struct drm_i915_private *dev_priv = |
| 5015 | container_of(shrinker, |
| 5016 | struct drm_i915_private, |
| 5017 | mm.inactive_shrinker); |
| 5018 | struct drm_device *dev = dev_priv->dev; |
| 5019 | struct drm_i915_gem_object *obj, *next; |
| 5020 | int cnt; |
| 5021 | |
| 5022 | if (!mutex_trylock(&dev->struct_mutex)) |
| 5023 | return nr_to_scan ? 0 : -1; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5024 | |
| 5025 | /* "fast-path" to count number of available objects */ |
| 5026 | if (nr_to_scan == 0) { |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5027 | cnt = 0; |
| 5028 | list_for_each_entry(obj, |
| 5029 | &dev_priv->mm.inactive_list, |
| 5030 | mm_list) |
| 5031 | cnt++; |
| 5032 | mutex_unlock(&dev->struct_mutex); |
| 5033 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5034 | } |
| 5035 | |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5036 | rescan: |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5037 | /* first scan for clean buffers */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5038 | i915_gem_retire_requests(dev); |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5039 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5040 | list_for_each_entry_safe(obj, next, |
| 5041 | &dev_priv->mm.inactive_list, |
| 5042 | mm_list) { |
| 5043 | if (i915_gem_object_is_purgeable(obj)) { |
| 5044 | i915_gem_object_unbind(&obj->base); |
| 5045 | if (--nr_to_scan == 0) |
| 5046 | break; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5047 | } |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5048 | } |
| 5049 | |
| 5050 | /* second pass, evict/count anything still on the inactive list */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5051 | cnt = 0; |
| 5052 | list_for_each_entry_safe(obj, next, |
| 5053 | &dev_priv->mm.inactive_list, |
| 5054 | mm_list) { |
| 5055 | if (nr_to_scan) { |
| 5056 | i915_gem_object_unbind(&obj->base); |
| 5057 | nr_to_scan--; |
| 5058 | } else |
| 5059 | cnt++; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5060 | } |
| 5061 | |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5062 | if (nr_to_scan && i915_gpu_is_active(dev)) { |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5063 | /* |
| 5064 | * We are desperate for pages, so as a last resort, wait |
| 5065 | * for the GPU to finish and discard whatever we can. |
| 5066 | * This has a dramatic impact to reduce the number of |
| 5067 | * OOM-killer events whilst running the GPU aggressively. |
| 5068 | */ |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5069 | if (i915_gpu_idle(dev) == 0) |
Chris Wilson | 1637ef4 | 2010-04-20 17:10:35 +0100 | [diff] [blame] | 5070 | goto rescan; |
| 5071 | } |
Chris Wilson | 17250b7 | 2010-10-28 12:51:39 +0100 | [diff] [blame] | 5072 | mutex_unlock(&dev->struct_mutex); |
| 5073 | return cnt / 100 * sysctl_vfs_cache_pressure; |
Chris Wilson | 3116971 | 2009-09-14 16:50:28 +0100 | [diff] [blame] | 5074 | } |