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