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