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