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