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