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