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