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