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