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