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