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