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