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