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