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" |
| 32 | #include <linux/swap.h> |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 33 | #include <linux/pci.h> |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 34 | |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 35 | #define I915_GEM_GPU_DOMAINS (~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) |
| 36 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 37 | static void i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj); |
| 38 | static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj); |
| 39 | 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] | 40 | static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, |
| 41 | int write); |
| 42 | static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 43 | uint64_t offset, |
| 44 | uint64_t size); |
| 45 | static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 46 | static int i915_gem_object_get_pages(struct drm_gem_object *obj); |
| 47 | static void i915_gem_object_put_pages(struct drm_gem_object *obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 48 | static int i915_gem_object_wait_rendering(struct drm_gem_object *obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 49 | static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, |
| 50 | unsigned alignment); |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 51 | static int i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 52 | static void i915_gem_clear_fence_reg(struct drm_gem_object *obj); |
| 53 | static int i915_gem_evict_something(struct drm_device *dev); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 54 | static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 55 | struct drm_i915_gem_pwrite *args, |
| 56 | struct drm_file *file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 57 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 58 | int i915_gem_do_init(struct drm_device *dev, unsigned long start, |
| 59 | unsigned long end) |
| 60 | { |
| 61 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 62 | |
| 63 | if (start >= end || |
| 64 | (start & (PAGE_SIZE - 1)) != 0 || |
| 65 | (end & (PAGE_SIZE - 1)) != 0) { |
| 66 | return -EINVAL; |
| 67 | } |
| 68 | |
| 69 | drm_mm_init(&dev_priv->mm.gtt_space, start, |
| 70 | end - start); |
| 71 | |
| 72 | dev->gtt_total = (uint32_t) (end - start); |
| 73 | |
| 74 | return 0; |
| 75 | } |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 76 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 77 | int |
| 78 | i915_gem_init_ioctl(struct drm_device *dev, void *data, |
| 79 | struct drm_file *file_priv) |
| 80 | { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 81 | struct drm_i915_gem_init *args = data; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 82 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 83 | |
| 84 | mutex_lock(&dev->struct_mutex); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 85 | ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 86 | mutex_unlock(&dev->struct_mutex); |
| 87 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 88 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 91 | int |
| 92 | i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data, |
| 93 | struct drm_file *file_priv) |
| 94 | { |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 95 | struct drm_i915_gem_get_aperture *args = data; |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 96 | |
| 97 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 98 | return -ENODEV; |
| 99 | |
| 100 | args->aper_size = dev->gtt_total; |
Keith Packard | 2678d9d | 2008-11-20 22:54:54 -0800 | [diff] [blame] | 101 | args->aper_available_size = (args->aper_size - |
| 102 | atomic_read(&dev->pin_memory)); |
Eric Anholt | 5a125c3 | 2008-10-22 21:40:13 -0700 | [diff] [blame] | 103 | |
| 104 | return 0; |
| 105 | } |
| 106 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 107 | |
| 108 | /** |
| 109 | * Creates a new mm object and returns a handle to it. |
| 110 | */ |
| 111 | int |
| 112 | i915_gem_create_ioctl(struct drm_device *dev, void *data, |
| 113 | struct drm_file *file_priv) |
| 114 | { |
| 115 | struct drm_i915_gem_create *args = data; |
| 116 | struct drm_gem_object *obj; |
| 117 | int handle, ret; |
| 118 | |
| 119 | args->size = roundup(args->size, PAGE_SIZE); |
| 120 | |
| 121 | /* Allocate the new object */ |
| 122 | obj = drm_gem_object_alloc(dev, args->size); |
| 123 | if (obj == NULL) |
| 124 | return -ENOMEM; |
| 125 | |
| 126 | ret = drm_gem_handle_create(file_priv, obj, &handle); |
| 127 | mutex_lock(&dev->struct_mutex); |
| 128 | drm_gem_object_handle_unreference(obj); |
| 129 | mutex_unlock(&dev->struct_mutex); |
| 130 | |
| 131 | if (ret) |
| 132 | return ret; |
| 133 | |
| 134 | args->handle = handle; |
| 135 | |
| 136 | return 0; |
| 137 | } |
| 138 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 139 | static inline int |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 140 | fast_shmem_read(struct page **pages, |
| 141 | loff_t page_base, int page_offset, |
| 142 | char __user *data, |
| 143 | int length) |
| 144 | { |
| 145 | char __iomem *vaddr; |
| 146 | int ret; |
| 147 | |
| 148 | vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0); |
| 149 | if (vaddr == NULL) |
| 150 | return -ENOMEM; |
| 151 | ret = __copy_to_user_inatomic(data, vaddr + page_offset, length); |
| 152 | kunmap_atomic(vaddr, KM_USER0); |
| 153 | |
| 154 | return ret; |
| 155 | } |
| 156 | |
| 157 | static inline int |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 158 | slow_shmem_copy(struct page *dst_page, |
| 159 | int dst_offset, |
| 160 | struct page *src_page, |
| 161 | int src_offset, |
| 162 | int length) |
| 163 | { |
| 164 | char *dst_vaddr, *src_vaddr; |
| 165 | |
| 166 | dst_vaddr = kmap_atomic(dst_page, KM_USER0); |
| 167 | if (dst_vaddr == NULL) |
| 168 | return -ENOMEM; |
| 169 | |
| 170 | src_vaddr = kmap_atomic(src_page, KM_USER1); |
| 171 | if (src_vaddr == NULL) { |
| 172 | kunmap_atomic(dst_vaddr, KM_USER0); |
| 173 | return -ENOMEM; |
| 174 | } |
| 175 | |
| 176 | memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length); |
| 177 | |
| 178 | kunmap_atomic(src_vaddr, KM_USER1); |
| 179 | kunmap_atomic(dst_vaddr, KM_USER0); |
| 180 | |
| 181 | return 0; |
| 182 | } |
| 183 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 184 | /** |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 185 | * This is the fast shmem pread path, which attempts to copy_from_user directly |
| 186 | * from the backing pages of the object to the user's address space. On a |
| 187 | * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow(). |
| 188 | */ |
| 189 | static int |
| 190 | i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 191 | struct drm_i915_gem_pread *args, |
| 192 | struct drm_file *file_priv) |
| 193 | { |
| 194 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 195 | ssize_t remain; |
| 196 | loff_t offset, page_base; |
| 197 | char __user *user_data; |
| 198 | int page_offset, page_length; |
| 199 | int ret; |
| 200 | |
| 201 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 202 | remain = args->size; |
| 203 | |
| 204 | mutex_lock(&dev->struct_mutex); |
| 205 | |
| 206 | ret = i915_gem_object_get_pages(obj); |
| 207 | if (ret != 0) |
| 208 | goto fail_unlock; |
| 209 | |
| 210 | ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset, |
| 211 | args->size); |
| 212 | if (ret != 0) |
| 213 | goto fail_put_pages; |
| 214 | |
| 215 | obj_priv = obj->driver_private; |
| 216 | offset = args->offset; |
| 217 | |
| 218 | while (remain > 0) { |
| 219 | /* Operation in this page |
| 220 | * |
| 221 | * page_base = page offset within aperture |
| 222 | * page_offset = offset within page |
| 223 | * page_length = bytes to copy for this page |
| 224 | */ |
| 225 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 226 | page_offset = offset & (PAGE_SIZE-1); |
| 227 | page_length = remain; |
| 228 | if ((page_offset + remain) > PAGE_SIZE) |
| 229 | page_length = PAGE_SIZE - page_offset; |
| 230 | |
| 231 | ret = fast_shmem_read(obj_priv->pages, |
| 232 | page_base, page_offset, |
| 233 | user_data, page_length); |
| 234 | if (ret) |
| 235 | goto fail_put_pages; |
| 236 | |
| 237 | remain -= page_length; |
| 238 | user_data += page_length; |
| 239 | offset += page_length; |
| 240 | } |
| 241 | |
| 242 | fail_put_pages: |
| 243 | i915_gem_object_put_pages(obj); |
| 244 | fail_unlock: |
| 245 | mutex_unlock(&dev->struct_mutex); |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
| 250 | /** |
| 251 | * This is the fallback shmem pread path, which allocates temporary storage |
| 252 | * in kernel space to copy_to_user into outside of the struct_mutex, so we |
| 253 | * can copy out of the object's backing pages while holding the struct mutex |
| 254 | * and not take page faults. |
| 255 | */ |
| 256 | static int |
| 257 | i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 258 | struct drm_i915_gem_pread *args, |
| 259 | struct drm_file *file_priv) |
| 260 | { |
| 261 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 262 | struct mm_struct *mm = current->mm; |
| 263 | struct page **user_pages; |
| 264 | ssize_t remain; |
| 265 | loff_t offset, pinned_pages, i; |
| 266 | loff_t first_data_page, last_data_page, num_pages; |
| 267 | int shmem_page_index, shmem_page_offset; |
| 268 | int data_page_index, data_page_offset; |
| 269 | int page_length; |
| 270 | int ret; |
| 271 | uint64_t data_ptr = args->data_ptr; |
| 272 | |
| 273 | remain = args->size; |
| 274 | |
| 275 | /* Pin the user pages containing the data. We can't fault while |
| 276 | * holding the struct mutex, yet we want to hold it while |
| 277 | * dereferencing the user data. |
| 278 | */ |
| 279 | first_data_page = data_ptr / PAGE_SIZE; |
| 280 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 281 | num_pages = last_data_page - first_data_page + 1; |
| 282 | |
| 283 | user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); |
| 284 | if (user_pages == NULL) |
| 285 | return -ENOMEM; |
| 286 | |
| 287 | down_read(&mm->mmap_sem); |
| 288 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 289 | num_pages, 0, 0, user_pages, NULL); |
| 290 | up_read(&mm->mmap_sem); |
| 291 | if (pinned_pages < num_pages) { |
| 292 | ret = -EFAULT; |
| 293 | goto fail_put_user_pages; |
| 294 | } |
| 295 | |
| 296 | mutex_lock(&dev->struct_mutex); |
| 297 | |
| 298 | ret = i915_gem_object_get_pages(obj); |
| 299 | if (ret != 0) |
| 300 | goto fail_unlock; |
| 301 | |
| 302 | ret = i915_gem_object_set_cpu_read_domain_range(obj, args->offset, |
| 303 | args->size); |
| 304 | if (ret != 0) |
| 305 | goto fail_put_pages; |
| 306 | |
| 307 | obj_priv = obj->driver_private; |
| 308 | offset = args->offset; |
| 309 | |
| 310 | while (remain > 0) { |
| 311 | /* Operation in this page |
| 312 | * |
| 313 | * shmem_page_index = page number within shmem file |
| 314 | * shmem_page_offset = offset within page in shmem file |
| 315 | * data_page_index = page number in get_user_pages return |
| 316 | * data_page_offset = offset with data_page_index page. |
| 317 | * page_length = bytes to copy for this page |
| 318 | */ |
| 319 | shmem_page_index = offset / PAGE_SIZE; |
| 320 | shmem_page_offset = offset & ~PAGE_MASK; |
| 321 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 322 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 323 | |
| 324 | page_length = remain; |
| 325 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 326 | page_length = PAGE_SIZE - shmem_page_offset; |
| 327 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 328 | page_length = PAGE_SIZE - data_page_offset; |
| 329 | |
| 330 | ret = slow_shmem_copy(user_pages[data_page_index], |
| 331 | data_page_offset, |
| 332 | obj_priv->pages[shmem_page_index], |
| 333 | shmem_page_offset, |
| 334 | page_length); |
| 335 | if (ret) |
| 336 | goto fail_put_pages; |
| 337 | |
| 338 | remain -= page_length; |
| 339 | data_ptr += page_length; |
| 340 | offset += page_length; |
| 341 | } |
| 342 | |
| 343 | fail_put_pages: |
| 344 | i915_gem_object_put_pages(obj); |
| 345 | fail_unlock: |
| 346 | mutex_unlock(&dev->struct_mutex); |
| 347 | fail_put_user_pages: |
| 348 | for (i = 0; i < pinned_pages; i++) { |
| 349 | SetPageDirty(user_pages[i]); |
| 350 | page_cache_release(user_pages[i]); |
| 351 | } |
| 352 | kfree(user_pages); |
| 353 | |
| 354 | return ret; |
| 355 | } |
| 356 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 357 | /** |
| 358 | * Reads data from the object referenced by handle. |
| 359 | * |
| 360 | * On error, the contents of *data are undefined. |
| 361 | */ |
| 362 | int |
| 363 | i915_gem_pread_ioctl(struct drm_device *dev, void *data, |
| 364 | struct drm_file *file_priv) |
| 365 | { |
| 366 | struct drm_i915_gem_pread *args = data; |
| 367 | struct drm_gem_object *obj; |
| 368 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 369 | int ret; |
| 370 | |
| 371 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 372 | if (obj == NULL) |
| 373 | return -EBADF; |
| 374 | obj_priv = obj->driver_private; |
| 375 | |
| 376 | /* Bounds check source. |
| 377 | * |
| 378 | * XXX: This could use review for overflow issues... |
| 379 | */ |
| 380 | if (args->offset > obj->size || args->size > obj->size || |
| 381 | args->offset + args->size > obj->size) { |
| 382 | drm_gem_object_unreference(obj); |
| 383 | return -EINVAL; |
| 384 | } |
| 385 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 386 | ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv); |
| 387 | if (ret != 0) |
| 388 | ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 389 | |
| 390 | drm_gem_object_unreference(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 391 | |
Eric Anholt | eb01459 | 2009-03-10 11:44:52 -0700 | [diff] [blame] | 392 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 395 | /* This is the fast write path which cannot handle |
| 396 | * page faults in the source data |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 397 | */ |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 398 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 399 | static inline int |
| 400 | fast_user_write(struct io_mapping *mapping, |
| 401 | loff_t page_base, int page_offset, |
| 402 | char __user *user_data, |
| 403 | int length) |
| 404 | { |
| 405 | char *vaddr_atomic; |
| 406 | unsigned long unwritten; |
| 407 | |
| 408 | vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base); |
| 409 | unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset, |
| 410 | user_data, length); |
| 411 | io_mapping_unmap_atomic(vaddr_atomic); |
| 412 | if (unwritten) |
| 413 | return -EFAULT; |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 414 | return 0; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 415 | } |
| 416 | |
| 417 | /* Here's the write path which can sleep for |
| 418 | * page faults |
| 419 | */ |
| 420 | |
| 421 | static inline int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 422 | slow_kernel_write(struct io_mapping *mapping, |
| 423 | loff_t gtt_base, int gtt_offset, |
| 424 | struct page *user_page, int user_offset, |
| 425 | int length) |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 426 | { |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 427 | char *src_vaddr, *dst_vaddr; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 428 | unsigned long unwritten; |
| 429 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 430 | dst_vaddr = io_mapping_map_atomic_wc(mapping, gtt_base); |
| 431 | src_vaddr = kmap_atomic(user_page, KM_USER1); |
| 432 | unwritten = __copy_from_user_inatomic_nocache(dst_vaddr + gtt_offset, |
| 433 | src_vaddr + user_offset, |
| 434 | length); |
| 435 | kunmap_atomic(src_vaddr, KM_USER1); |
| 436 | io_mapping_unmap_atomic(dst_vaddr); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 437 | if (unwritten) |
| 438 | return -EFAULT; |
| 439 | return 0; |
Linus Torvalds | 9b7530cc | 2008-10-20 14:16:43 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 442 | static inline int |
| 443 | fast_shmem_write(struct page **pages, |
| 444 | loff_t page_base, int page_offset, |
| 445 | char __user *data, |
| 446 | int length) |
| 447 | { |
| 448 | char __iomem *vaddr; |
Dave Airlie | d008877 | 2009-03-28 20:29:48 -0400 | [diff] [blame] | 449 | unsigned long unwritten; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 450 | |
| 451 | vaddr = kmap_atomic(pages[page_base >> PAGE_SHIFT], KM_USER0); |
| 452 | if (vaddr == NULL) |
| 453 | return -ENOMEM; |
Dave Airlie | d008877 | 2009-03-28 20:29:48 -0400 | [diff] [blame] | 454 | unwritten = __copy_from_user_inatomic(vaddr + page_offset, data, length); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 455 | kunmap_atomic(vaddr, KM_USER0); |
| 456 | |
Dave Airlie | d008877 | 2009-03-28 20:29:48 -0400 | [diff] [blame] | 457 | if (unwritten) |
| 458 | return -EFAULT; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 459 | return 0; |
| 460 | } |
| 461 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 462 | /** |
| 463 | * This is the fast pwrite path, where we copy the data directly from the |
| 464 | * user into the GTT, uncached. |
| 465 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 466 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 467 | i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 468 | struct drm_i915_gem_pwrite *args, |
| 469 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 470 | { |
| 471 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 472 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 473 | ssize_t remain; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 474 | loff_t offset, page_base; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 475 | char __user *user_data; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 476 | int page_offset, page_length; |
| 477 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 478 | |
| 479 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 480 | remain = args->size; |
| 481 | if (!access_ok(VERIFY_READ, user_data, remain)) |
| 482 | return -EFAULT; |
| 483 | |
| 484 | |
| 485 | mutex_lock(&dev->struct_mutex); |
| 486 | ret = i915_gem_object_pin(obj, 0); |
| 487 | if (ret) { |
| 488 | mutex_unlock(&dev->struct_mutex); |
| 489 | return ret; |
| 490 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 491 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 492 | if (ret) |
| 493 | goto fail; |
| 494 | |
| 495 | obj_priv = obj->driver_private; |
| 496 | offset = obj_priv->gtt_offset + args->offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 497 | |
| 498 | while (remain > 0) { |
| 499 | /* Operation in this page |
| 500 | * |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 501 | * page_base = page offset within aperture |
| 502 | * page_offset = offset within page |
| 503 | * page_length = bytes to copy for this page |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 504 | */ |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 505 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 506 | page_offset = offset & (PAGE_SIZE-1); |
| 507 | page_length = remain; |
| 508 | if ((page_offset + remain) > PAGE_SIZE) |
| 509 | page_length = PAGE_SIZE - page_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 510 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 511 | ret = fast_user_write (dev_priv->mm.gtt_mapping, page_base, |
| 512 | page_offset, user_data, page_length); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 513 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 514 | /* If we get a fault while copying data, then (presumably) our |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 515 | * source page isn't available. Return the error and we'll |
| 516 | * retry in the slow path. |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 517 | */ |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 518 | if (ret) |
| 519 | goto fail; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 520 | |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 521 | remain -= page_length; |
| 522 | user_data += page_length; |
| 523 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 524 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 525 | |
| 526 | fail: |
| 527 | i915_gem_object_unpin(obj); |
| 528 | mutex_unlock(&dev->struct_mutex); |
| 529 | |
| 530 | return ret; |
| 531 | } |
| 532 | |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 533 | /** |
| 534 | * This is the fallback GTT pwrite path, which uses get_user_pages to pin |
| 535 | * the memory and maps it using kmap_atomic for copying. |
| 536 | * |
| 537 | * This code resulted in x11perf -rgb10text consuming about 10% more CPU |
| 538 | * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit). |
| 539 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 540 | static int |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 541 | i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 542 | struct drm_i915_gem_pwrite *args, |
| 543 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 544 | { |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 545 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 546 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 547 | ssize_t remain; |
| 548 | loff_t gtt_page_base, offset; |
| 549 | loff_t first_data_page, last_data_page, num_pages; |
| 550 | loff_t pinned_pages, i; |
| 551 | struct page **user_pages; |
| 552 | struct mm_struct *mm = current->mm; |
| 553 | int gtt_page_offset, data_page_offset, data_page_index, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 554 | int ret; |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 555 | uint64_t data_ptr = args->data_ptr; |
| 556 | |
| 557 | remain = args->size; |
| 558 | |
| 559 | /* Pin the user pages containing the data. We can't fault while |
| 560 | * holding the struct mutex, and all of the pwrite implementations |
| 561 | * want to hold it while dereferencing the user data. |
| 562 | */ |
| 563 | first_data_page = data_ptr / PAGE_SIZE; |
| 564 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 565 | num_pages = last_data_page - first_data_page + 1; |
| 566 | |
| 567 | user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); |
| 568 | if (user_pages == NULL) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | down_read(&mm->mmap_sem); |
| 572 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 573 | num_pages, 0, 0, user_pages, NULL); |
| 574 | up_read(&mm->mmap_sem); |
| 575 | if (pinned_pages < num_pages) { |
| 576 | ret = -EFAULT; |
| 577 | goto out_unpin_pages; |
| 578 | } |
| 579 | |
| 580 | mutex_lock(&dev->struct_mutex); |
| 581 | ret = i915_gem_object_pin(obj, 0); |
| 582 | if (ret) |
| 583 | goto out_unlock; |
| 584 | |
| 585 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 586 | if (ret) |
| 587 | goto out_unpin_object; |
| 588 | |
| 589 | obj_priv = obj->driver_private; |
| 590 | offset = obj_priv->gtt_offset + args->offset; |
| 591 | |
| 592 | while (remain > 0) { |
| 593 | /* Operation in this page |
| 594 | * |
| 595 | * gtt_page_base = page offset within aperture |
| 596 | * gtt_page_offset = offset within page in aperture |
| 597 | * data_page_index = page number in get_user_pages return |
| 598 | * data_page_offset = offset with data_page_index page. |
| 599 | * page_length = bytes to copy for this page |
| 600 | */ |
| 601 | gtt_page_base = offset & PAGE_MASK; |
| 602 | gtt_page_offset = offset & ~PAGE_MASK; |
| 603 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 604 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 605 | |
| 606 | page_length = remain; |
| 607 | if ((gtt_page_offset + page_length) > PAGE_SIZE) |
| 608 | page_length = PAGE_SIZE - gtt_page_offset; |
| 609 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 610 | page_length = PAGE_SIZE - data_page_offset; |
| 611 | |
| 612 | ret = slow_kernel_write(dev_priv->mm.gtt_mapping, |
| 613 | gtt_page_base, gtt_page_offset, |
| 614 | user_pages[data_page_index], |
| 615 | data_page_offset, |
| 616 | page_length); |
| 617 | |
| 618 | /* If we get a fault while copying data, then (presumably) our |
| 619 | * source page isn't available. Return the error and we'll |
| 620 | * retry in the slow path. |
| 621 | */ |
| 622 | if (ret) |
| 623 | goto out_unpin_object; |
| 624 | |
| 625 | remain -= page_length; |
| 626 | offset += page_length; |
| 627 | data_ptr += page_length; |
| 628 | } |
| 629 | |
| 630 | out_unpin_object: |
| 631 | i915_gem_object_unpin(obj); |
| 632 | out_unlock: |
| 633 | mutex_unlock(&dev->struct_mutex); |
| 634 | out_unpin_pages: |
| 635 | for (i = 0; i < pinned_pages; i++) |
| 636 | page_cache_release(user_pages[i]); |
| 637 | kfree(user_pages); |
| 638 | |
| 639 | return ret; |
| 640 | } |
| 641 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 642 | /** |
| 643 | * This is the fast shmem pwrite path, which attempts to directly |
| 644 | * copy_from_user into the kmapped pages backing the object. |
| 645 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 646 | static int |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 647 | i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj, |
| 648 | struct drm_i915_gem_pwrite *args, |
| 649 | struct drm_file *file_priv) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 650 | { |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 651 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 652 | ssize_t remain; |
| 653 | loff_t offset, page_base; |
| 654 | char __user *user_data; |
| 655 | int page_offset, page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 656 | int ret; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 657 | |
| 658 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 659 | remain = args->size; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 660 | |
| 661 | mutex_lock(&dev->struct_mutex); |
| 662 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 663 | ret = i915_gem_object_get_pages(obj); |
| 664 | if (ret != 0) |
| 665 | goto fail_unlock; |
| 666 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 667 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 668 | if (ret != 0) |
| 669 | goto fail_put_pages; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 670 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 671 | obj_priv = obj->driver_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 672 | offset = args->offset; |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 673 | obj_priv->dirty = 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 674 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 675 | while (remain > 0) { |
| 676 | /* Operation in this page |
| 677 | * |
| 678 | * page_base = page offset within aperture |
| 679 | * page_offset = offset within page |
| 680 | * page_length = bytes to copy for this page |
| 681 | */ |
| 682 | page_base = (offset & ~(PAGE_SIZE-1)); |
| 683 | page_offset = offset & (PAGE_SIZE-1); |
| 684 | page_length = remain; |
| 685 | if ((page_offset + remain) > PAGE_SIZE) |
| 686 | page_length = PAGE_SIZE - page_offset; |
| 687 | |
| 688 | ret = fast_shmem_write(obj_priv->pages, |
| 689 | page_base, page_offset, |
| 690 | user_data, page_length); |
| 691 | if (ret) |
| 692 | goto fail_put_pages; |
| 693 | |
| 694 | remain -= page_length; |
| 695 | user_data += page_length; |
| 696 | offset += page_length; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 697 | } |
| 698 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 699 | fail_put_pages: |
| 700 | i915_gem_object_put_pages(obj); |
| 701 | fail_unlock: |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 702 | mutex_unlock(&dev->struct_mutex); |
| 703 | |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 704 | return ret; |
| 705 | } |
| 706 | |
| 707 | /** |
| 708 | * This is the fallback shmem pwrite path, which uses get_user_pages to pin |
| 709 | * the memory and maps it using kmap_atomic for copying. |
| 710 | * |
| 711 | * This avoids taking mmap_sem for faulting on the user's address while the |
| 712 | * struct_mutex is held. |
| 713 | */ |
| 714 | static int |
| 715 | i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj, |
| 716 | struct drm_i915_gem_pwrite *args, |
| 717 | struct drm_file *file_priv) |
| 718 | { |
| 719 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 720 | struct mm_struct *mm = current->mm; |
| 721 | struct page **user_pages; |
| 722 | ssize_t remain; |
| 723 | loff_t offset, pinned_pages, i; |
| 724 | loff_t first_data_page, last_data_page, num_pages; |
| 725 | int shmem_page_index, shmem_page_offset; |
| 726 | int data_page_index, data_page_offset; |
| 727 | int page_length; |
| 728 | int ret; |
| 729 | uint64_t data_ptr = args->data_ptr; |
| 730 | |
| 731 | remain = args->size; |
| 732 | |
| 733 | /* Pin the user pages containing the data. We can't fault while |
| 734 | * holding the struct mutex, and all of the pwrite implementations |
| 735 | * want to hold it while dereferencing the user data. |
| 736 | */ |
| 737 | first_data_page = data_ptr / PAGE_SIZE; |
| 738 | last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE; |
| 739 | num_pages = last_data_page - first_data_page + 1; |
| 740 | |
| 741 | user_pages = kcalloc(num_pages, sizeof(struct page *), GFP_KERNEL); |
| 742 | if (user_pages == NULL) |
| 743 | return -ENOMEM; |
| 744 | |
| 745 | down_read(&mm->mmap_sem); |
| 746 | pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr, |
| 747 | num_pages, 0, 0, user_pages, NULL); |
| 748 | up_read(&mm->mmap_sem); |
| 749 | if (pinned_pages < num_pages) { |
| 750 | ret = -EFAULT; |
| 751 | goto fail_put_user_pages; |
| 752 | } |
| 753 | |
| 754 | mutex_lock(&dev->struct_mutex); |
| 755 | |
| 756 | ret = i915_gem_object_get_pages(obj); |
| 757 | if (ret != 0) |
| 758 | goto fail_unlock; |
| 759 | |
| 760 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
| 761 | if (ret != 0) |
| 762 | goto fail_put_pages; |
| 763 | |
| 764 | obj_priv = obj->driver_private; |
| 765 | offset = args->offset; |
| 766 | obj_priv->dirty = 1; |
| 767 | |
| 768 | while (remain > 0) { |
| 769 | /* Operation in this page |
| 770 | * |
| 771 | * shmem_page_index = page number within shmem file |
| 772 | * shmem_page_offset = offset within page in shmem file |
| 773 | * data_page_index = page number in get_user_pages return |
| 774 | * data_page_offset = offset with data_page_index page. |
| 775 | * page_length = bytes to copy for this page |
| 776 | */ |
| 777 | shmem_page_index = offset / PAGE_SIZE; |
| 778 | shmem_page_offset = offset & ~PAGE_MASK; |
| 779 | data_page_index = data_ptr / PAGE_SIZE - first_data_page; |
| 780 | data_page_offset = data_ptr & ~PAGE_MASK; |
| 781 | |
| 782 | page_length = remain; |
| 783 | if ((shmem_page_offset + page_length) > PAGE_SIZE) |
| 784 | page_length = PAGE_SIZE - shmem_page_offset; |
| 785 | if ((data_page_offset + page_length) > PAGE_SIZE) |
| 786 | page_length = PAGE_SIZE - data_page_offset; |
| 787 | |
| 788 | ret = slow_shmem_copy(obj_priv->pages[shmem_page_index], |
| 789 | shmem_page_offset, |
| 790 | user_pages[data_page_index], |
| 791 | data_page_offset, |
| 792 | page_length); |
| 793 | if (ret) |
| 794 | goto fail_put_pages; |
| 795 | |
| 796 | remain -= page_length; |
| 797 | data_ptr += page_length; |
| 798 | offset += page_length; |
| 799 | } |
| 800 | |
| 801 | fail_put_pages: |
| 802 | i915_gem_object_put_pages(obj); |
| 803 | fail_unlock: |
| 804 | mutex_unlock(&dev->struct_mutex); |
| 805 | fail_put_user_pages: |
| 806 | for (i = 0; i < pinned_pages; i++) |
| 807 | page_cache_release(user_pages[i]); |
| 808 | kfree(user_pages); |
| 809 | |
| 810 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /** |
| 814 | * Writes data to the object referenced by handle. |
| 815 | * |
| 816 | * On error, the contents of the buffer that were to be modified are undefined. |
| 817 | */ |
| 818 | int |
| 819 | i915_gem_pwrite_ioctl(struct drm_device *dev, void *data, |
| 820 | struct drm_file *file_priv) |
| 821 | { |
| 822 | struct drm_i915_gem_pwrite *args = data; |
| 823 | struct drm_gem_object *obj; |
| 824 | struct drm_i915_gem_object *obj_priv; |
| 825 | int ret = 0; |
| 826 | |
| 827 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 828 | if (obj == NULL) |
| 829 | return -EBADF; |
| 830 | obj_priv = obj->driver_private; |
| 831 | |
| 832 | /* Bounds check destination. |
| 833 | * |
| 834 | * XXX: This could use review for overflow issues... |
| 835 | */ |
| 836 | if (args->offset > obj->size || args->size > obj->size || |
| 837 | args->offset + args->size > obj->size) { |
| 838 | drm_gem_object_unreference(obj); |
| 839 | return -EINVAL; |
| 840 | } |
| 841 | |
| 842 | /* We can only do the GTT pwrite on untiled buffers, as otherwise |
| 843 | * it would end up going through the fenced access, and we'll get |
| 844 | * different detiling behavior between reading and writing. |
| 845 | * pread/pwrite currently are reading and writing from the CPU |
| 846 | * perspective, requiring manual detiling by the client. |
| 847 | */ |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 848 | if (obj_priv->phys_obj) |
| 849 | ret = i915_gem_phys_pwrite(dev, obj, args, file_priv); |
| 850 | else if (obj_priv->tiling_mode == I915_TILING_NONE && |
Eric Anholt | 3de09aa | 2009-03-09 09:42:23 -0700 | [diff] [blame] | 851 | dev->gtt_total != 0) { |
| 852 | ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file_priv); |
| 853 | if (ret == -EFAULT) { |
| 854 | ret = i915_gem_gtt_pwrite_slow(dev, obj, args, |
| 855 | file_priv); |
| 856 | } |
Eric Anholt | 40123c1 | 2009-03-09 13:42:30 -0700 | [diff] [blame] | 857 | } else { |
| 858 | ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file_priv); |
| 859 | if (ret == -EFAULT) { |
| 860 | ret = i915_gem_shmem_pwrite_slow(dev, obj, args, |
| 861 | file_priv); |
| 862 | } |
| 863 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 864 | |
| 865 | #if WATCH_PWRITE |
| 866 | if (ret) |
| 867 | DRM_INFO("pwrite failed %d\n", ret); |
| 868 | #endif |
| 869 | |
| 870 | drm_gem_object_unreference(obj); |
| 871 | |
| 872 | return ret; |
| 873 | } |
| 874 | |
| 875 | /** |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 876 | * Called when user space prepares to use an object with the CPU, either |
| 877 | * through the mmap ioctl's mapping or a GTT mapping. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 878 | */ |
| 879 | int |
| 880 | i915_gem_set_domain_ioctl(struct drm_device *dev, void *data, |
| 881 | struct drm_file *file_priv) |
| 882 | { |
| 883 | struct drm_i915_gem_set_domain *args = data; |
| 884 | struct drm_gem_object *obj; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 885 | uint32_t read_domains = args->read_domains; |
| 886 | uint32_t write_domain = args->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 887 | int ret; |
| 888 | |
| 889 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 890 | return -ENODEV; |
| 891 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 892 | /* Only handle setting domains to types used by the CPU. */ |
| 893 | if (write_domain & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) |
| 894 | return -EINVAL; |
| 895 | |
| 896 | if (read_domains & ~(I915_GEM_DOMAIN_CPU | I915_GEM_DOMAIN_GTT)) |
| 897 | return -EINVAL; |
| 898 | |
| 899 | /* Having something in the write domain implies it's in the read |
| 900 | * domain, and only that read domain. Enforce that in the request. |
| 901 | */ |
| 902 | if (write_domain != 0 && read_domains != write_domain) |
| 903 | return -EINVAL; |
| 904 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 905 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 906 | if (obj == NULL) |
| 907 | return -EBADF; |
| 908 | |
| 909 | mutex_lock(&dev->struct_mutex); |
| 910 | #if WATCH_BUF |
| 911 | DRM_INFO("set_domain_ioctl %p(%d), %08x %08x\n", |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 912 | obj, obj->size, read_domains, write_domain); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 913 | #endif |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 914 | if (read_domains & I915_GEM_DOMAIN_GTT) { |
| 915 | ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0); |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 916 | |
| 917 | /* Silently promote "you're not bound, there was nothing to do" |
| 918 | * to success, since the client was just asking us to |
| 919 | * make sure everything was done. |
| 920 | */ |
| 921 | if (ret == -EINVAL) |
| 922 | ret = 0; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 923 | } else { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 924 | ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 925 | } |
| 926 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 927 | drm_gem_object_unreference(obj); |
| 928 | mutex_unlock(&dev->struct_mutex); |
| 929 | return ret; |
| 930 | } |
| 931 | |
| 932 | /** |
| 933 | * Called when user space has done writes to this buffer |
| 934 | */ |
| 935 | int |
| 936 | i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data, |
| 937 | struct drm_file *file_priv) |
| 938 | { |
| 939 | struct drm_i915_gem_sw_finish *args = data; |
| 940 | struct drm_gem_object *obj; |
| 941 | struct drm_i915_gem_object *obj_priv; |
| 942 | int ret = 0; |
| 943 | |
| 944 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 945 | return -ENODEV; |
| 946 | |
| 947 | mutex_lock(&dev->struct_mutex); |
| 948 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 949 | if (obj == NULL) { |
| 950 | mutex_unlock(&dev->struct_mutex); |
| 951 | return -EBADF; |
| 952 | } |
| 953 | |
| 954 | #if WATCH_BUF |
| 955 | DRM_INFO("%s: sw_finish %d (%p %d)\n", |
| 956 | __func__, args->handle, obj, obj->size); |
| 957 | #endif |
| 958 | obj_priv = obj->driver_private; |
| 959 | |
| 960 | /* Pinned buffers may be scanout, so flush the cache */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 961 | if (obj_priv->pin_count) |
| 962 | i915_gem_object_flush_cpu_write_domain(obj); |
| 963 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 964 | drm_gem_object_unreference(obj); |
| 965 | mutex_unlock(&dev->struct_mutex); |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | /** |
| 970 | * Maps the contents of an object, returning the address it is mapped |
| 971 | * into. |
| 972 | * |
| 973 | * While the mapping holds a reference on the contents of the object, it doesn't |
| 974 | * imply a ref on the object itself. |
| 975 | */ |
| 976 | int |
| 977 | i915_gem_mmap_ioctl(struct drm_device *dev, void *data, |
| 978 | struct drm_file *file_priv) |
| 979 | { |
| 980 | struct drm_i915_gem_mmap *args = data; |
| 981 | struct drm_gem_object *obj; |
| 982 | loff_t offset; |
| 983 | unsigned long addr; |
| 984 | |
| 985 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 986 | return -ENODEV; |
| 987 | |
| 988 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 989 | if (obj == NULL) |
| 990 | return -EBADF; |
| 991 | |
| 992 | offset = args->offset; |
| 993 | |
| 994 | down_write(¤t->mm->mmap_sem); |
| 995 | addr = do_mmap(obj->filp, 0, args->size, |
| 996 | PROT_READ | PROT_WRITE, MAP_SHARED, |
| 997 | args->offset); |
| 998 | up_write(¤t->mm->mmap_sem); |
| 999 | mutex_lock(&dev->struct_mutex); |
| 1000 | drm_gem_object_unreference(obj); |
| 1001 | mutex_unlock(&dev->struct_mutex); |
| 1002 | if (IS_ERR((void *)addr)) |
| 1003 | return addr; |
| 1004 | |
| 1005 | args->addr_ptr = (uint64_t) addr; |
| 1006 | |
| 1007 | return 0; |
| 1008 | } |
| 1009 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1010 | /** |
| 1011 | * i915_gem_fault - fault a page into the GTT |
| 1012 | * vma: VMA in question |
| 1013 | * vmf: fault info |
| 1014 | * |
| 1015 | * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped |
| 1016 | * from userspace. The fault handler takes care of binding the object to |
| 1017 | * the GTT (if needed), allocating and programming a fence register (again, |
| 1018 | * only if needed based on whether the old reg is still valid or the object |
| 1019 | * is tiled) and inserting a new PTE into the faulting process. |
| 1020 | * |
| 1021 | * Note that the faulting process may involve evicting existing objects |
| 1022 | * from the GTT and/or fence registers to make room. So performance may |
| 1023 | * suffer if the GTT working set is large or there are few fence registers |
| 1024 | * left. |
| 1025 | */ |
| 1026 | int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf) |
| 1027 | { |
| 1028 | struct drm_gem_object *obj = vma->vm_private_data; |
| 1029 | struct drm_device *dev = obj->dev; |
| 1030 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1031 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1032 | pgoff_t page_offset; |
| 1033 | unsigned long pfn; |
| 1034 | int ret = 0; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1035 | bool write = !!(vmf->flags & FAULT_FLAG_WRITE); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1036 | |
| 1037 | /* We don't use vmf->pgoff since that has the fake offset */ |
| 1038 | page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >> |
| 1039 | PAGE_SHIFT; |
| 1040 | |
| 1041 | /* Now bind it into the GTT if needed */ |
| 1042 | mutex_lock(&dev->struct_mutex); |
| 1043 | if (!obj_priv->gtt_space) { |
| 1044 | ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment); |
| 1045 | if (ret) { |
| 1046 | mutex_unlock(&dev->struct_mutex); |
| 1047 | return VM_FAULT_SIGBUS; |
| 1048 | } |
| 1049 | list_add(&obj_priv->list, &dev_priv->mm.inactive_list); |
| 1050 | } |
| 1051 | |
| 1052 | /* Need a new fence register? */ |
| 1053 | if (obj_priv->fence_reg == I915_FENCE_REG_NONE && |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1054 | obj_priv->tiling_mode != I915_TILING_NONE) { |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1055 | ret = i915_gem_object_get_fence_reg(obj, write); |
Chris Wilson | 7d8d58b | 2009-02-04 14:15:10 +0000 | [diff] [blame] | 1056 | if (ret) { |
| 1057 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1058 | return VM_FAULT_SIGBUS; |
Chris Wilson | 7d8d58b | 2009-02-04 14:15:10 +0000 | [diff] [blame] | 1059 | } |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 1060 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1061 | |
| 1062 | pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) + |
| 1063 | page_offset; |
| 1064 | |
| 1065 | /* Finally, remap it using the new GTT offset */ |
| 1066 | ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn); |
| 1067 | |
| 1068 | mutex_unlock(&dev->struct_mutex); |
| 1069 | |
| 1070 | switch (ret) { |
| 1071 | case -ENOMEM: |
| 1072 | case -EAGAIN: |
| 1073 | return VM_FAULT_OOM; |
| 1074 | case -EFAULT: |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1075 | return VM_FAULT_SIGBUS; |
| 1076 | default: |
| 1077 | return VM_FAULT_NOPAGE; |
| 1078 | } |
| 1079 | } |
| 1080 | |
| 1081 | /** |
| 1082 | * i915_gem_create_mmap_offset - create a fake mmap offset for an object |
| 1083 | * @obj: obj in question |
| 1084 | * |
| 1085 | * GEM memory mapping works by handing back to userspace a fake mmap offset |
| 1086 | * it can use in a subsequent mmap(2) call. The DRM core code then looks |
| 1087 | * up the object based on the offset and sets up the various memory mapping |
| 1088 | * structures. |
| 1089 | * |
| 1090 | * This routine allocates and attaches a fake offset for @obj. |
| 1091 | */ |
| 1092 | static int |
| 1093 | i915_gem_create_mmap_offset(struct drm_gem_object *obj) |
| 1094 | { |
| 1095 | struct drm_device *dev = obj->dev; |
| 1096 | struct drm_gem_mm *mm = dev->mm_private; |
| 1097 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1098 | struct drm_map_list *list; |
Benjamin Herrenschmidt | f77d390 | 2009-02-02 16:55:46 +1100 | [diff] [blame] | 1099 | struct drm_local_map *map; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1100 | int ret = 0; |
| 1101 | |
| 1102 | /* Set the object up for mmap'ing */ |
| 1103 | list = &obj->map_list; |
| 1104 | list->map = drm_calloc(1, sizeof(struct drm_map_list), |
| 1105 | DRM_MEM_DRIVER); |
| 1106 | if (!list->map) |
| 1107 | return -ENOMEM; |
| 1108 | |
| 1109 | map = list->map; |
| 1110 | map->type = _DRM_GEM; |
| 1111 | map->size = obj->size; |
| 1112 | map->handle = obj; |
| 1113 | |
| 1114 | /* Get a DRM GEM mmap offset allocated... */ |
| 1115 | list->file_offset_node = drm_mm_search_free(&mm->offset_manager, |
| 1116 | obj->size / PAGE_SIZE, 0, 0); |
| 1117 | if (!list->file_offset_node) { |
| 1118 | DRM_ERROR("failed to allocate offset for bo %d\n", obj->name); |
| 1119 | ret = -ENOMEM; |
| 1120 | goto out_free_list; |
| 1121 | } |
| 1122 | |
| 1123 | list->file_offset_node = drm_mm_get_block(list->file_offset_node, |
| 1124 | obj->size / PAGE_SIZE, 0); |
| 1125 | if (!list->file_offset_node) { |
| 1126 | ret = -ENOMEM; |
| 1127 | goto out_free_list; |
| 1128 | } |
| 1129 | |
| 1130 | list->hash.key = list->file_offset_node->start; |
| 1131 | if (drm_ht_insert_item(&mm->offset_hash, &list->hash)) { |
| 1132 | DRM_ERROR("failed to add to map hash\n"); |
| 1133 | goto out_free_mm; |
| 1134 | } |
| 1135 | |
| 1136 | /* By now we should be all set, any drm_mmap request on the offset |
| 1137 | * below will get to our mmap & fault handler */ |
| 1138 | obj_priv->mmap_offset = ((uint64_t) list->hash.key) << PAGE_SHIFT; |
| 1139 | |
| 1140 | return 0; |
| 1141 | |
| 1142 | out_free_mm: |
| 1143 | drm_mm_put_block(list->file_offset_node); |
| 1144 | out_free_list: |
| 1145 | drm_free(list->map, sizeof(struct drm_map_list), DRM_MEM_DRIVER); |
| 1146 | |
| 1147 | return ret; |
| 1148 | } |
| 1149 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 1150 | static void |
| 1151 | i915_gem_free_mmap_offset(struct drm_gem_object *obj) |
| 1152 | { |
| 1153 | struct drm_device *dev = obj->dev; |
| 1154 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1155 | struct drm_gem_mm *mm = dev->mm_private; |
| 1156 | struct drm_map_list *list; |
| 1157 | |
| 1158 | list = &obj->map_list; |
| 1159 | drm_ht_remove_item(&mm->offset_hash, &list->hash); |
| 1160 | |
| 1161 | if (list->file_offset_node) { |
| 1162 | drm_mm_put_block(list->file_offset_node); |
| 1163 | list->file_offset_node = NULL; |
| 1164 | } |
| 1165 | |
| 1166 | if (list->map) { |
| 1167 | drm_free(list->map, sizeof(struct drm_map), DRM_MEM_DRIVER); |
| 1168 | list->map = NULL; |
| 1169 | } |
| 1170 | |
| 1171 | obj_priv->mmap_offset = 0; |
| 1172 | } |
| 1173 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1174 | /** |
| 1175 | * i915_gem_get_gtt_alignment - return required GTT alignment for an object |
| 1176 | * @obj: object to check |
| 1177 | * |
| 1178 | * Return the required GTT alignment for an object, taking into account |
| 1179 | * potential fence register mapping if needed. |
| 1180 | */ |
| 1181 | static uint32_t |
| 1182 | i915_gem_get_gtt_alignment(struct drm_gem_object *obj) |
| 1183 | { |
| 1184 | struct drm_device *dev = obj->dev; |
| 1185 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1186 | int start, i; |
| 1187 | |
| 1188 | /* |
| 1189 | * Minimum alignment is 4k (GTT page size), but might be greater |
| 1190 | * if a fence register is needed for the object. |
| 1191 | */ |
| 1192 | if (IS_I965G(dev) || obj_priv->tiling_mode == I915_TILING_NONE) |
| 1193 | return 4096; |
| 1194 | |
| 1195 | /* |
| 1196 | * Previous chips need to be aligned to the size of the smallest |
| 1197 | * fence register that can contain the object. |
| 1198 | */ |
| 1199 | if (IS_I9XX(dev)) |
| 1200 | start = 1024*1024; |
| 1201 | else |
| 1202 | start = 512*1024; |
| 1203 | |
| 1204 | for (i = start; i < obj->size; i <<= 1) |
| 1205 | ; |
| 1206 | |
| 1207 | return i; |
| 1208 | } |
| 1209 | |
| 1210 | /** |
| 1211 | * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing |
| 1212 | * @dev: DRM device |
| 1213 | * @data: GTT mapping ioctl data |
| 1214 | * @file_priv: GEM object info |
| 1215 | * |
| 1216 | * Simply returns the fake offset to userspace so it can mmap it. |
| 1217 | * The mmap call will end up in drm_gem_mmap(), which will set things |
| 1218 | * up so we can get faults in the handler above. |
| 1219 | * |
| 1220 | * The fault handler will take care of binding the object into the GTT |
| 1221 | * (since it may have been evicted to make room for something), allocating |
| 1222 | * a fence register, and mapping the appropriate aperture address into |
| 1223 | * userspace. |
| 1224 | */ |
| 1225 | int |
| 1226 | i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data, |
| 1227 | struct drm_file *file_priv) |
| 1228 | { |
| 1229 | struct drm_i915_gem_mmap_gtt *args = data; |
| 1230 | struct drm_i915_private *dev_priv = dev->dev_private; |
| 1231 | struct drm_gem_object *obj; |
| 1232 | struct drm_i915_gem_object *obj_priv; |
| 1233 | int ret; |
| 1234 | |
| 1235 | if (!(dev->driver->driver_features & DRIVER_GEM)) |
| 1236 | return -ENODEV; |
| 1237 | |
| 1238 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 1239 | if (obj == NULL) |
| 1240 | return -EBADF; |
| 1241 | |
| 1242 | mutex_lock(&dev->struct_mutex); |
| 1243 | |
| 1244 | obj_priv = obj->driver_private; |
| 1245 | |
| 1246 | if (!obj_priv->mmap_offset) { |
| 1247 | ret = i915_gem_create_mmap_offset(obj); |
Chris Wilson | 13af106 | 2009-02-11 14:26:31 +0000 | [diff] [blame] | 1248 | if (ret) { |
| 1249 | drm_gem_object_unreference(obj); |
| 1250 | mutex_unlock(&dev->struct_mutex); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1251 | return ret; |
Chris Wilson | 13af106 | 2009-02-11 14:26:31 +0000 | [diff] [blame] | 1252 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1253 | } |
| 1254 | |
| 1255 | args->offset = obj_priv->mmap_offset; |
| 1256 | |
| 1257 | obj_priv->gtt_alignment = i915_gem_get_gtt_alignment(obj); |
| 1258 | |
| 1259 | /* Make sure the alignment is correct for fence regs etc */ |
| 1260 | if (obj_priv->agp_mem && |
| 1261 | (obj_priv->gtt_offset & (obj_priv->gtt_alignment - 1))) { |
| 1262 | drm_gem_object_unreference(obj); |
| 1263 | mutex_unlock(&dev->struct_mutex); |
| 1264 | return -EINVAL; |
| 1265 | } |
| 1266 | |
| 1267 | /* |
| 1268 | * Pull it into the GTT so that we have a page list (makes the |
| 1269 | * initial fault faster and any subsequent flushing possible). |
| 1270 | */ |
| 1271 | if (!obj_priv->agp_mem) { |
| 1272 | ret = i915_gem_object_bind_to_gtt(obj, obj_priv->gtt_alignment); |
| 1273 | if (ret) { |
| 1274 | drm_gem_object_unreference(obj); |
| 1275 | mutex_unlock(&dev->struct_mutex); |
| 1276 | return ret; |
| 1277 | } |
| 1278 | list_add(&obj_priv->list, &dev_priv->mm.inactive_list); |
| 1279 | } |
| 1280 | |
| 1281 | drm_gem_object_unreference(obj); |
| 1282 | mutex_unlock(&dev->struct_mutex); |
| 1283 | |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1287 | static void |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1288 | i915_gem_object_put_pages(struct drm_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1289 | { |
| 1290 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1291 | int page_count = obj->size / PAGE_SIZE; |
| 1292 | int i; |
| 1293 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1294 | BUG_ON(obj_priv->pages_refcount == 0); |
| 1295 | |
| 1296 | if (--obj_priv->pages_refcount != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1297 | return; |
| 1298 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1299 | for (i = 0; i < page_count; i++) |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1300 | if (obj_priv->pages[i] != NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1301 | if (obj_priv->dirty) |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1302 | set_page_dirty(obj_priv->pages[i]); |
| 1303 | mark_page_accessed(obj_priv->pages[i]); |
| 1304 | page_cache_release(obj_priv->pages[i]); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1305 | } |
| 1306 | obj_priv->dirty = 0; |
| 1307 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1308 | drm_free(obj_priv->pages, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1309 | page_count * sizeof(struct page *), |
| 1310 | DRM_MEM_DRIVER); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1311 | obj_priv->pages = NULL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1312 | } |
| 1313 | |
| 1314 | static void |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1315 | i915_gem_object_move_to_active(struct drm_gem_object *obj, uint32_t seqno) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1316 | { |
| 1317 | struct drm_device *dev = obj->dev; |
| 1318 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1319 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1320 | |
| 1321 | /* Add a reference if we're newly entering the active list. */ |
| 1322 | if (!obj_priv->active) { |
| 1323 | drm_gem_object_reference(obj); |
| 1324 | obj_priv->active = 1; |
| 1325 | } |
| 1326 | /* Move from whatever list we were on to the tail of execution. */ |
| 1327 | list_move_tail(&obj_priv->list, |
| 1328 | &dev_priv->mm.active_list); |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1329 | obj_priv->last_rendering_seqno = seqno; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1330 | } |
| 1331 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1332 | static void |
| 1333 | i915_gem_object_move_to_flushing(struct drm_gem_object *obj) |
| 1334 | { |
| 1335 | struct drm_device *dev = obj->dev; |
| 1336 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1337 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1338 | |
| 1339 | BUG_ON(!obj_priv->active); |
| 1340 | list_move_tail(&obj_priv->list, &dev_priv->mm.flushing_list); |
| 1341 | obj_priv->last_rendering_seqno = 0; |
| 1342 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1343 | |
| 1344 | static void |
| 1345 | i915_gem_object_move_to_inactive(struct drm_gem_object *obj) |
| 1346 | { |
| 1347 | struct drm_device *dev = obj->dev; |
| 1348 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1349 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1350 | |
| 1351 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 1352 | if (obj_priv->pin_count != 0) |
| 1353 | list_del_init(&obj_priv->list); |
| 1354 | else |
| 1355 | list_move_tail(&obj_priv->list, &dev_priv->mm.inactive_list); |
| 1356 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1357 | obj_priv->last_rendering_seqno = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1358 | if (obj_priv->active) { |
| 1359 | obj_priv->active = 0; |
| 1360 | drm_gem_object_unreference(obj); |
| 1361 | } |
| 1362 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 1363 | } |
| 1364 | |
| 1365 | /** |
| 1366 | * Creates a new sequence number, emitting a write of it to the status page |
| 1367 | * plus an interrupt, which will trigger i915_user_interrupt_handler. |
| 1368 | * |
| 1369 | * Must be called with struct_lock held. |
| 1370 | * |
| 1371 | * Returned sequence numbers are nonzero on success. |
| 1372 | */ |
| 1373 | static uint32_t |
| 1374 | i915_add_request(struct drm_device *dev, uint32_t flush_domains) |
| 1375 | { |
| 1376 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1377 | struct drm_i915_gem_request *request; |
| 1378 | uint32_t seqno; |
| 1379 | int was_empty; |
| 1380 | RING_LOCALS; |
| 1381 | |
| 1382 | request = drm_calloc(1, sizeof(*request), DRM_MEM_DRIVER); |
| 1383 | if (request == NULL) |
| 1384 | return 0; |
| 1385 | |
| 1386 | /* Grab the seqno we're going to make this request be, and bump the |
| 1387 | * next (skipping 0 so it can be the reserved no-seqno value). |
| 1388 | */ |
| 1389 | seqno = dev_priv->mm.next_gem_seqno; |
| 1390 | dev_priv->mm.next_gem_seqno++; |
| 1391 | if (dev_priv->mm.next_gem_seqno == 0) |
| 1392 | dev_priv->mm.next_gem_seqno++; |
| 1393 | |
| 1394 | BEGIN_LP_RING(4); |
| 1395 | OUT_RING(MI_STORE_DWORD_INDEX); |
| 1396 | OUT_RING(I915_GEM_HWS_INDEX << MI_STORE_DWORD_INDEX_SHIFT); |
| 1397 | OUT_RING(seqno); |
| 1398 | |
| 1399 | OUT_RING(MI_USER_INTERRUPT); |
| 1400 | ADVANCE_LP_RING(); |
| 1401 | |
| 1402 | DRM_DEBUG("%d\n", seqno); |
| 1403 | |
| 1404 | request->seqno = seqno; |
| 1405 | request->emitted_jiffies = jiffies; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1406 | was_empty = list_empty(&dev_priv->mm.request_list); |
| 1407 | list_add_tail(&request->list, &dev_priv->mm.request_list); |
| 1408 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1409 | /* Associate any objects on the flushing list matching the write |
| 1410 | * domain we're flushing with our flush. |
| 1411 | */ |
| 1412 | if (flush_domains != 0) { |
| 1413 | struct drm_i915_gem_object *obj_priv, *next; |
| 1414 | |
| 1415 | list_for_each_entry_safe(obj_priv, next, |
| 1416 | &dev_priv->mm.flushing_list, list) { |
| 1417 | struct drm_gem_object *obj = obj_priv->obj; |
| 1418 | |
| 1419 | if ((obj->write_domain & flush_domains) == |
| 1420 | obj->write_domain) { |
| 1421 | obj->write_domain = 0; |
| 1422 | i915_gem_object_move_to_active(obj, seqno); |
| 1423 | } |
| 1424 | } |
| 1425 | |
| 1426 | } |
| 1427 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 1428 | if (was_empty && !dev_priv->mm.suspended) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1429 | schedule_delayed_work(&dev_priv->mm.retire_work, HZ); |
| 1430 | return seqno; |
| 1431 | } |
| 1432 | |
| 1433 | /** |
| 1434 | * Command execution barrier |
| 1435 | * |
| 1436 | * Ensures that all commands in the ring are finished |
| 1437 | * before signalling the CPU |
| 1438 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 1439 | static uint32_t |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1440 | i915_retire_commands(struct drm_device *dev) |
| 1441 | { |
| 1442 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1443 | uint32_t cmd = MI_FLUSH | MI_NO_WRITE_FLUSH; |
| 1444 | uint32_t flush_domains = 0; |
| 1445 | RING_LOCALS; |
| 1446 | |
| 1447 | /* The sampler always gets flushed on i965 (sigh) */ |
| 1448 | if (IS_I965G(dev)) |
| 1449 | flush_domains |= I915_GEM_DOMAIN_SAMPLER; |
| 1450 | BEGIN_LP_RING(2); |
| 1451 | OUT_RING(cmd); |
| 1452 | OUT_RING(0); /* noop */ |
| 1453 | ADVANCE_LP_RING(); |
| 1454 | return flush_domains; |
| 1455 | } |
| 1456 | |
| 1457 | /** |
| 1458 | * Moves buffers associated only with the given active seqno from the active |
| 1459 | * to inactive list, potentially freeing them. |
| 1460 | */ |
| 1461 | static void |
| 1462 | i915_gem_retire_request(struct drm_device *dev, |
| 1463 | struct drm_i915_gem_request *request) |
| 1464 | { |
| 1465 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1466 | |
| 1467 | /* Move any buffers on the active list that are no longer referenced |
| 1468 | * by the ringbuffer to the flushing/inactive lists as appropriate. |
| 1469 | */ |
| 1470 | while (!list_empty(&dev_priv->mm.active_list)) { |
| 1471 | struct drm_gem_object *obj; |
| 1472 | struct drm_i915_gem_object *obj_priv; |
| 1473 | |
| 1474 | obj_priv = list_first_entry(&dev_priv->mm.active_list, |
| 1475 | struct drm_i915_gem_object, |
| 1476 | list); |
| 1477 | obj = obj_priv->obj; |
| 1478 | |
| 1479 | /* If the seqno being retired doesn't match the oldest in the |
| 1480 | * list, then the oldest in the list must still be newer than |
| 1481 | * this seqno. |
| 1482 | */ |
| 1483 | if (obj_priv->last_rendering_seqno != request->seqno) |
| 1484 | return; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1485 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1486 | #if WATCH_LRU |
| 1487 | DRM_INFO("%s: retire %d moves to inactive list %p\n", |
| 1488 | __func__, request->seqno, obj); |
| 1489 | #endif |
| 1490 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 1491 | if (obj->write_domain != 0) |
| 1492 | i915_gem_object_move_to_flushing(obj); |
| 1493 | else |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1494 | i915_gem_object_move_to_inactive(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1495 | } |
| 1496 | } |
| 1497 | |
| 1498 | /** |
| 1499 | * Returns true if seq1 is later than seq2. |
| 1500 | */ |
| 1501 | static int |
| 1502 | i915_seqno_passed(uint32_t seq1, uint32_t seq2) |
| 1503 | { |
| 1504 | return (int32_t)(seq1 - seq2) >= 0; |
| 1505 | } |
| 1506 | |
| 1507 | uint32_t |
| 1508 | i915_get_gem_seqno(struct drm_device *dev) |
| 1509 | { |
| 1510 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1511 | |
| 1512 | return READ_HWSP(dev_priv, I915_GEM_HWS_INDEX); |
| 1513 | } |
| 1514 | |
| 1515 | /** |
| 1516 | * This function clears the request list as sequence numbers are passed. |
| 1517 | */ |
| 1518 | void |
| 1519 | i915_gem_retire_requests(struct drm_device *dev) |
| 1520 | { |
| 1521 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1522 | uint32_t seqno; |
| 1523 | |
Karsten Wiese | 6c0594a | 2009-02-23 15:07:57 +0100 | [diff] [blame] | 1524 | if (!dev_priv->hw_status_page) |
| 1525 | return; |
| 1526 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1527 | seqno = i915_get_gem_seqno(dev); |
| 1528 | |
| 1529 | while (!list_empty(&dev_priv->mm.request_list)) { |
| 1530 | struct drm_i915_gem_request *request; |
| 1531 | uint32_t retiring_seqno; |
| 1532 | |
| 1533 | request = list_first_entry(&dev_priv->mm.request_list, |
| 1534 | struct drm_i915_gem_request, |
| 1535 | list); |
| 1536 | retiring_seqno = request->seqno; |
| 1537 | |
| 1538 | if (i915_seqno_passed(seqno, retiring_seqno) || |
| 1539 | dev_priv->mm.wedged) { |
| 1540 | i915_gem_retire_request(dev, request); |
| 1541 | |
| 1542 | list_del(&request->list); |
| 1543 | drm_free(request, sizeof(*request), DRM_MEM_DRIVER); |
| 1544 | } else |
| 1545 | break; |
| 1546 | } |
| 1547 | } |
| 1548 | |
| 1549 | void |
| 1550 | i915_gem_retire_work_handler(struct work_struct *work) |
| 1551 | { |
| 1552 | drm_i915_private_t *dev_priv; |
| 1553 | struct drm_device *dev; |
| 1554 | |
| 1555 | dev_priv = container_of(work, drm_i915_private_t, |
| 1556 | mm.retire_work.work); |
| 1557 | dev = dev_priv->dev; |
| 1558 | |
| 1559 | mutex_lock(&dev->struct_mutex); |
| 1560 | i915_gem_retire_requests(dev); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 1561 | if (!dev_priv->mm.suspended && |
| 1562 | !list_empty(&dev_priv->mm.request_list)) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1563 | schedule_delayed_work(&dev_priv->mm.retire_work, HZ); |
| 1564 | mutex_unlock(&dev->struct_mutex); |
| 1565 | } |
| 1566 | |
| 1567 | /** |
| 1568 | * Waits for a sequence number to be signaled, and cleans up the |
| 1569 | * request and object lists appropriately for that event. |
| 1570 | */ |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 1571 | static int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1572 | i915_wait_request(struct drm_device *dev, uint32_t seqno) |
| 1573 | { |
| 1574 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1575 | int ret = 0; |
| 1576 | |
| 1577 | BUG_ON(seqno == 0); |
| 1578 | |
| 1579 | if (!i915_seqno_passed(i915_get_gem_seqno(dev), seqno)) { |
| 1580 | dev_priv->mm.waiting_gem_seqno = seqno; |
| 1581 | i915_user_irq_get(dev); |
| 1582 | ret = wait_event_interruptible(dev_priv->irq_queue, |
| 1583 | i915_seqno_passed(i915_get_gem_seqno(dev), |
| 1584 | seqno) || |
| 1585 | dev_priv->mm.wedged); |
| 1586 | i915_user_irq_put(dev); |
| 1587 | dev_priv->mm.waiting_gem_seqno = 0; |
| 1588 | } |
| 1589 | if (dev_priv->mm.wedged) |
| 1590 | ret = -EIO; |
| 1591 | |
| 1592 | if (ret && ret != -ERESTARTSYS) |
| 1593 | DRM_ERROR("%s returns %d (awaiting %d at %d)\n", |
| 1594 | __func__, ret, seqno, i915_get_gem_seqno(dev)); |
| 1595 | |
| 1596 | /* Directly dispatch request retiring. While we have the work queue |
| 1597 | * to handle this, the waiter on a request often wants an associated |
| 1598 | * buffer to have made it to the inactive list, and we would need |
| 1599 | * a separate wait queue to handle that. |
| 1600 | */ |
| 1601 | if (ret == 0) |
| 1602 | i915_gem_retire_requests(dev); |
| 1603 | |
| 1604 | return ret; |
| 1605 | } |
| 1606 | |
| 1607 | static void |
| 1608 | i915_gem_flush(struct drm_device *dev, |
| 1609 | uint32_t invalidate_domains, |
| 1610 | uint32_t flush_domains) |
| 1611 | { |
| 1612 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1613 | uint32_t cmd; |
| 1614 | RING_LOCALS; |
| 1615 | |
| 1616 | #if WATCH_EXEC |
| 1617 | DRM_INFO("%s: invalidate %08x flush %08x\n", __func__, |
| 1618 | invalidate_domains, flush_domains); |
| 1619 | #endif |
| 1620 | |
| 1621 | if (flush_domains & I915_GEM_DOMAIN_CPU) |
| 1622 | drm_agp_chipset_flush(dev); |
| 1623 | |
| 1624 | if ((invalidate_domains | flush_domains) & ~(I915_GEM_DOMAIN_CPU | |
| 1625 | I915_GEM_DOMAIN_GTT)) { |
| 1626 | /* |
| 1627 | * read/write caches: |
| 1628 | * |
| 1629 | * I915_GEM_DOMAIN_RENDER is always invalidated, but is |
| 1630 | * only flushed if MI_NO_WRITE_FLUSH is unset. On 965, it is |
| 1631 | * also flushed at 2d versus 3d pipeline switches. |
| 1632 | * |
| 1633 | * read-only caches: |
| 1634 | * |
| 1635 | * I915_GEM_DOMAIN_SAMPLER is flushed on pre-965 if |
| 1636 | * MI_READ_FLUSH is set, and is always flushed on 965. |
| 1637 | * |
| 1638 | * I915_GEM_DOMAIN_COMMAND may not exist? |
| 1639 | * |
| 1640 | * I915_GEM_DOMAIN_INSTRUCTION, which exists on 965, is |
| 1641 | * invalidated when MI_EXE_FLUSH is set. |
| 1642 | * |
| 1643 | * I915_GEM_DOMAIN_VERTEX, which exists on 965, is |
| 1644 | * invalidated with every MI_FLUSH. |
| 1645 | * |
| 1646 | * TLBs: |
| 1647 | * |
| 1648 | * On 965, TLBs associated with I915_GEM_DOMAIN_COMMAND |
| 1649 | * and I915_GEM_DOMAIN_CPU in are invalidated at PTE write and |
| 1650 | * I915_GEM_DOMAIN_RENDER and I915_GEM_DOMAIN_SAMPLER |
| 1651 | * are flushed at any MI_FLUSH. |
| 1652 | */ |
| 1653 | |
| 1654 | cmd = MI_FLUSH | MI_NO_WRITE_FLUSH; |
| 1655 | if ((invalidate_domains|flush_domains) & |
| 1656 | I915_GEM_DOMAIN_RENDER) |
| 1657 | cmd &= ~MI_NO_WRITE_FLUSH; |
| 1658 | if (!IS_I965G(dev)) { |
| 1659 | /* |
| 1660 | * On the 965, the sampler cache always gets flushed |
| 1661 | * and this bit is reserved. |
| 1662 | */ |
| 1663 | if (invalidate_domains & I915_GEM_DOMAIN_SAMPLER) |
| 1664 | cmd |= MI_READ_FLUSH; |
| 1665 | } |
| 1666 | if (invalidate_domains & I915_GEM_DOMAIN_INSTRUCTION) |
| 1667 | cmd |= MI_EXE_FLUSH; |
| 1668 | |
| 1669 | #if WATCH_EXEC |
| 1670 | DRM_INFO("%s: queue flush %08x to ring\n", __func__, cmd); |
| 1671 | #endif |
| 1672 | BEGIN_LP_RING(2); |
| 1673 | OUT_RING(cmd); |
| 1674 | OUT_RING(0); /* noop */ |
| 1675 | ADVANCE_LP_RING(); |
| 1676 | } |
| 1677 | } |
| 1678 | |
| 1679 | /** |
| 1680 | * Ensures that all rendering to the object has completed and the object is |
| 1681 | * safe to unbind from the GTT or access from the CPU. |
| 1682 | */ |
| 1683 | static int |
| 1684 | i915_gem_object_wait_rendering(struct drm_gem_object *obj) |
| 1685 | { |
| 1686 | struct drm_device *dev = obj->dev; |
| 1687 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1688 | int ret; |
| 1689 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1690 | /* This function only exists to support waiting for existing rendering, |
| 1691 | * not for emitting required flushes. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1692 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1693 | BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1694 | |
| 1695 | /* If there is rendering queued on the buffer being evicted, wait for |
| 1696 | * it. |
| 1697 | */ |
| 1698 | if (obj_priv->active) { |
| 1699 | #if WATCH_BUF |
| 1700 | DRM_INFO("%s: object %p wait for seqno %08x\n", |
| 1701 | __func__, obj, obj_priv->last_rendering_seqno); |
| 1702 | #endif |
| 1703 | ret = i915_wait_request(dev, obj_priv->last_rendering_seqno); |
| 1704 | if (ret != 0) |
| 1705 | return ret; |
| 1706 | } |
| 1707 | |
| 1708 | return 0; |
| 1709 | } |
| 1710 | |
| 1711 | /** |
| 1712 | * Unbinds an object from the GTT aperture. |
| 1713 | */ |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1714 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1715 | i915_gem_object_unbind(struct drm_gem_object *obj) |
| 1716 | { |
| 1717 | struct drm_device *dev = obj->dev; |
| 1718 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1719 | loff_t offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1720 | int ret = 0; |
| 1721 | |
| 1722 | #if WATCH_BUF |
| 1723 | DRM_INFO("%s:%d %p\n", __func__, __LINE__, obj); |
| 1724 | DRM_INFO("gtt_space %p\n", obj_priv->gtt_space); |
| 1725 | #endif |
| 1726 | if (obj_priv->gtt_space == NULL) |
| 1727 | return 0; |
| 1728 | |
| 1729 | if (obj_priv->pin_count != 0) { |
| 1730 | DRM_ERROR("Attempting to unbind pinned buffer\n"); |
| 1731 | return -EINVAL; |
| 1732 | } |
| 1733 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1734 | /* Move the object to the CPU domain to ensure that |
| 1735 | * any possible CPU writes while it's not in the GTT |
| 1736 | * are flushed when we go to remap it. This will |
| 1737 | * also ensure that all pending GPU writes are finished |
| 1738 | * before we unbind. |
| 1739 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1740 | ret = i915_gem_object_set_to_cpu_domain(obj, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1741 | if (ret) { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 1742 | if (ret != -ERESTARTSYS) |
| 1743 | DRM_ERROR("set_domain failed: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1744 | return ret; |
| 1745 | } |
| 1746 | |
| 1747 | if (obj_priv->agp_mem != NULL) { |
| 1748 | drm_unbind_agp(obj_priv->agp_mem); |
| 1749 | drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE); |
| 1750 | obj_priv->agp_mem = NULL; |
| 1751 | } |
| 1752 | |
| 1753 | BUG_ON(obj_priv->active); |
| 1754 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1755 | /* blow away mappings if mapped through GTT */ |
| 1756 | offset = ((loff_t) obj->map_list.hash.key) << PAGE_SHIFT; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 1757 | if (dev->dev_mapping) |
| 1758 | unmap_mapping_range(dev->dev_mapping, offset, obj->size, 1); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1759 | |
| 1760 | if (obj_priv->fence_reg != I915_FENCE_REG_NONE) |
| 1761 | i915_gem_clear_fence_reg(obj); |
| 1762 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1763 | i915_gem_object_put_pages(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1764 | |
| 1765 | if (obj_priv->gtt_space) { |
| 1766 | atomic_dec(&dev->gtt_count); |
| 1767 | atomic_sub(obj->size, &dev->gtt_memory); |
| 1768 | |
| 1769 | drm_mm_put_block(obj_priv->gtt_space); |
| 1770 | obj_priv->gtt_space = NULL; |
| 1771 | } |
| 1772 | |
| 1773 | /* Remove ourselves from the LRU list if present. */ |
| 1774 | if (!list_empty(&obj_priv->list)) |
| 1775 | list_del_init(&obj_priv->list); |
| 1776 | |
| 1777 | return 0; |
| 1778 | } |
| 1779 | |
| 1780 | static int |
| 1781 | i915_gem_evict_something(struct drm_device *dev) |
| 1782 | { |
| 1783 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1784 | struct drm_gem_object *obj; |
| 1785 | struct drm_i915_gem_object *obj_priv; |
| 1786 | int ret = 0; |
| 1787 | |
| 1788 | for (;;) { |
| 1789 | /* If there's an inactive buffer available now, grab it |
| 1790 | * and be done. |
| 1791 | */ |
| 1792 | if (!list_empty(&dev_priv->mm.inactive_list)) { |
| 1793 | obj_priv = list_first_entry(&dev_priv->mm.inactive_list, |
| 1794 | struct drm_i915_gem_object, |
| 1795 | list); |
| 1796 | obj = obj_priv->obj; |
| 1797 | BUG_ON(obj_priv->pin_count != 0); |
| 1798 | #if WATCH_LRU |
| 1799 | DRM_INFO("%s: evicting %p\n", __func__, obj); |
| 1800 | #endif |
| 1801 | BUG_ON(obj_priv->active); |
| 1802 | |
| 1803 | /* Wait on the rendering and unbind the buffer. */ |
| 1804 | ret = i915_gem_object_unbind(obj); |
| 1805 | break; |
| 1806 | } |
| 1807 | |
| 1808 | /* If we didn't get anything, but the ring is still processing |
| 1809 | * things, wait for one of those things to finish and hopefully |
| 1810 | * leave us a buffer to evict. |
| 1811 | */ |
| 1812 | if (!list_empty(&dev_priv->mm.request_list)) { |
| 1813 | struct drm_i915_gem_request *request; |
| 1814 | |
| 1815 | request = list_first_entry(&dev_priv->mm.request_list, |
| 1816 | struct drm_i915_gem_request, |
| 1817 | list); |
| 1818 | |
| 1819 | ret = i915_wait_request(dev, request->seqno); |
| 1820 | if (ret) |
| 1821 | break; |
| 1822 | |
| 1823 | /* if waiting caused an object to become inactive, |
| 1824 | * then loop around and wait for it. Otherwise, we |
| 1825 | * assume that waiting freed and unbound something, |
| 1826 | * so there should now be some space in the GTT |
| 1827 | */ |
| 1828 | if (!list_empty(&dev_priv->mm.inactive_list)) |
| 1829 | continue; |
| 1830 | break; |
| 1831 | } |
| 1832 | |
| 1833 | /* If we didn't have anything on the request list but there |
| 1834 | * are buffers awaiting a flush, emit one and try again. |
| 1835 | * When we wait on it, those buffers waiting for that flush |
| 1836 | * will get moved to inactive. |
| 1837 | */ |
| 1838 | if (!list_empty(&dev_priv->mm.flushing_list)) { |
| 1839 | obj_priv = list_first_entry(&dev_priv->mm.flushing_list, |
| 1840 | struct drm_i915_gem_object, |
| 1841 | list); |
| 1842 | obj = obj_priv->obj; |
| 1843 | |
| 1844 | i915_gem_flush(dev, |
| 1845 | obj->write_domain, |
| 1846 | obj->write_domain); |
| 1847 | i915_add_request(dev, obj->write_domain); |
| 1848 | |
| 1849 | obj = NULL; |
| 1850 | continue; |
| 1851 | } |
| 1852 | |
| 1853 | DRM_ERROR("inactive empty %d request empty %d " |
| 1854 | "flushing empty %d\n", |
| 1855 | list_empty(&dev_priv->mm.inactive_list), |
| 1856 | list_empty(&dev_priv->mm.request_list), |
| 1857 | list_empty(&dev_priv->mm.flushing_list)); |
| 1858 | /* If we didn't do any of the above, there's nothing to be done |
| 1859 | * and we just can't fit it in. |
| 1860 | */ |
| 1861 | return -ENOMEM; |
| 1862 | } |
| 1863 | return ret; |
| 1864 | } |
| 1865 | |
| 1866 | static int |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 1867 | i915_gem_evict_everything(struct drm_device *dev) |
| 1868 | { |
| 1869 | int ret; |
| 1870 | |
| 1871 | for (;;) { |
| 1872 | ret = i915_gem_evict_something(dev); |
| 1873 | if (ret != 0) |
| 1874 | break; |
| 1875 | } |
Owain Ainsworth | 15c3533 | 2008-12-06 20:42:20 -0800 | [diff] [blame] | 1876 | if (ret == -ENOMEM) |
| 1877 | return 0; |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 1878 | return ret; |
| 1879 | } |
| 1880 | |
| 1881 | static int |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1882 | i915_gem_object_get_pages(struct drm_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1883 | { |
| 1884 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1885 | int page_count, i; |
| 1886 | struct address_space *mapping; |
| 1887 | struct inode *inode; |
| 1888 | struct page *page; |
| 1889 | int ret; |
| 1890 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1891 | if (obj_priv->pages_refcount++ != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1892 | return 0; |
| 1893 | |
| 1894 | /* Get the list of pages out of our struct file. They'll be pinned |
| 1895 | * at this point until we release them. |
| 1896 | */ |
| 1897 | page_count = obj->size / PAGE_SIZE; |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1898 | BUG_ON(obj_priv->pages != NULL); |
| 1899 | obj_priv->pages = drm_calloc(page_count, sizeof(struct page *), |
| 1900 | DRM_MEM_DRIVER); |
| 1901 | if (obj_priv->pages == NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1902 | DRM_ERROR("Faled to allocate page list\n"); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1903 | obj_priv->pages_refcount--; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1904 | return -ENOMEM; |
| 1905 | } |
| 1906 | |
| 1907 | inode = obj->filp->f_path.dentry->d_inode; |
| 1908 | mapping = inode->i_mapping; |
| 1909 | for (i = 0; i < page_count; i++) { |
| 1910 | page = read_mapping_page(mapping, i, NULL); |
| 1911 | if (IS_ERR(page)) { |
| 1912 | ret = PTR_ERR(page); |
| 1913 | DRM_ERROR("read_mapping_page failed: %d\n", ret); |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1914 | i915_gem_object_put_pages(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1915 | return ret; |
| 1916 | } |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 1917 | obj_priv->pages[i] = page; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 1918 | } |
| 1919 | return 0; |
| 1920 | } |
| 1921 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1922 | static void i965_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 1923 | { |
| 1924 | struct drm_gem_object *obj = reg->obj; |
| 1925 | struct drm_device *dev = obj->dev; |
| 1926 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1927 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1928 | int regnum = obj_priv->fence_reg; |
| 1929 | uint64_t val; |
| 1930 | |
| 1931 | val = (uint64_t)((obj_priv->gtt_offset + obj->size - 4096) & |
| 1932 | 0xfffff000) << 32; |
| 1933 | val |= obj_priv->gtt_offset & 0xfffff000; |
| 1934 | val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT; |
| 1935 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 1936 | val |= 1 << I965_FENCE_TILING_Y_SHIFT; |
| 1937 | val |= I965_FENCE_REG_VALID; |
| 1938 | |
| 1939 | I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val); |
| 1940 | } |
| 1941 | |
| 1942 | static void i915_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 1943 | { |
| 1944 | struct drm_gem_object *obj = reg->obj; |
| 1945 | struct drm_device *dev = obj->dev; |
| 1946 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1947 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1948 | int regnum = obj_priv->fence_reg; |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1949 | int tile_width; |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 1950 | uint32_t fence_reg, val; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1951 | uint32_t pitch_val; |
| 1952 | |
| 1953 | if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) || |
| 1954 | (obj_priv->gtt_offset & (obj->size - 1))) { |
Linus Torvalds | f06da26 | 2009-02-09 08:57:29 -0800 | [diff] [blame] | 1955 | 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] | 1956 | __func__, obj_priv->gtt_offset, obj->size); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1957 | return; |
| 1958 | } |
| 1959 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1960 | if (obj_priv->tiling_mode == I915_TILING_Y && |
| 1961 | HAS_128_BYTE_Y_TILING(dev)) |
| 1962 | tile_width = 128; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1963 | else |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1964 | tile_width = 512; |
| 1965 | |
| 1966 | /* Note: pitch better be a power of two tile widths */ |
| 1967 | pitch_val = obj_priv->stride / tile_width; |
| 1968 | pitch_val = ffs(pitch_val) - 1; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1969 | |
| 1970 | val = obj_priv->gtt_offset; |
| 1971 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 1972 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
| 1973 | val |= I915_FENCE_SIZE_BITS(obj->size); |
| 1974 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 1975 | val |= I830_FENCE_REG_VALID; |
| 1976 | |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 1977 | if (regnum < 8) |
| 1978 | fence_reg = FENCE_REG_830_0 + (regnum * 4); |
| 1979 | else |
| 1980 | fence_reg = FENCE_REG_945_8 + ((regnum - 8) * 4); |
| 1981 | I915_WRITE(fence_reg, val); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1982 | } |
| 1983 | |
| 1984 | static void i830_write_fence_reg(struct drm_i915_fence_reg *reg) |
| 1985 | { |
| 1986 | struct drm_gem_object *obj = reg->obj; |
| 1987 | struct drm_device *dev = obj->dev; |
| 1988 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 1989 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 1990 | int regnum = obj_priv->fence_reg; |
| 1991 | uint32_t val; |
| 1992 | uint32_t pitch_val; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 1993 | uint32_t fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1994 | |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 1995 | if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) || |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1996 | (obj_priv->gtt_offset & (obj->size - 1))) { |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 1997 | WARN(1, "%s: object 0x%08x not 512K or size aligned\n", |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 1998 | __func__, obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | pitch_val = (obj_priv->stride / 128) - 1; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 2003 | WARN_ON(pitch_val & ~0x0000000f); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2004 | val = obj_priv->gtt_offset; |
| 2005 | if (obj_priv->tiling_mode == I915_TILING_Y) |
| 2006 | val |= 1 << I830_FENCE_TILING_Y_SHIFT; |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 2007 | fence_size_bits = I830_FENCE_SIZE_BITS(obj->size); |
| 2008 | WARN_ON(fence_size_bits & ~0x00000f00); |
| 2009 | val |= fence_size_bits; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2010 | val |= pitch_val << I830_FENCE_PITCH_SHIFT; |
| 2011 | val |= I830_FENCE_REG_VALID; |
| 2012 | |
| 2013 | I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val); |
| 2014 | |
| 2015 | } |
| 2016 | |
| 2017 | /** |
| 2018 | * i915_gem_object_get_fence_reg - set up a fence reg for an object |
| 2019 | * @obj: object to map through a fence reg |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2020 | * @write: object is about to be written |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2021 | * |
| 2022 | * When mapping objects through the GTT, userspace wants to be able to write |
| 2023 | * to them without having to worry about swizzling if the object is tiled. |
| 2024 | * |
| 2025 | * This function walks the fence regs looking for a free one for @obj, |
| 2026 | * stealing one if it can't find any. |
| 2027 | * |
| 2028 | * It then sets up the reg based on the object's properties: address, pitch |
| 2029 | * and tiling format. |
| 2030 | */ |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2031 | static int |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2032 | i915_gem_object_get_fence_reg(struct drm_gem_object *obj, bool write) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2033 | { |
| 2034 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2035 | struct drm_i915_private *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2036 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2037 | struct drm_i915_fence_reg *reg = NULL; |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2038 | struct drm_i915_gem_object *old_obj_priv = NULL; |
| 2039 | int i, ret, avail; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2040 | |
| 2041 | switch (obj_priv->tiling_mode) { |
| 2042 | case I915_TILING_NONE: |
| 2043 | WARN(1, "allocating a fence for non-tiled object?\n"); |
| 2044 | break; |
| 2045 | case I915_TILING_X: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2046 | if (!obj_priv->stride) |
| 2047 | return -EINVAL; |
| 2048 | WARN((obj_priv->stride & (512 - 1)), |
| 2049 | "object 0x%08x is X tiled but has non-512B pitch\n", |
| 2050 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2051 | break; |
| 2052 | case I915_TILING_Y: |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2053 | if (!obj_priv->stride) |
| 2054 | return -EINVAL; |
| 2055 | WARN((obj_priv->stride & (128 - 1)), |
| 2056 | "object 0x%08x is Y tiled but has non-128B pitch\n", |
| 2057 | obj_priv->gtt_offset); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2058 | break; |
| 2059 | } |
| 2060 | |
| 2061 | /* First try to find a free reg */ |
Chris Wilson | 9b2412f | 2009-02-11 14:26:44 +0000 | [diff] [blame] | 2062 | try_again: |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2063 | avail = 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2064 | for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) { |
| 2065 | reg = &dev_priv->fence_regs[i]; |
| 2066 | if (!reg->obj) |
| 2067 | break; |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2068 | |
| 2069 | old_obj_priv = reg->obj->driver_private; |
| 2070 | if (!old_obj_priv->pin_count) |
| 2071 | avail++; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2072 | } |
| 2073 | |
| 2074 | /* None available, try to steal one or wait for a user to finish */ |
| 2075 | if (i == dev_priv->num_fence_regs) { |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2076 | uint32_t seqno = dev_priv->mm.next_gem_seqno; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2077 | loff_t offset; |
| 2078 | |
Chris Wilson | fc7170b | 2009-02-11 14:26:46 +0000 | [diff] [blame] | 2079 | if (avail == 0) |
| 2080 | return -ENOMEM; |
| 2081 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2082 | for (i = dev_priv->fence_reg_start; |
| 2083 | i < dev_priv->num_fence_regs; i++) { |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2084 | uint32_t this_seqno; |
| 2085 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2086 | reg = &dev_priv->fence_regs[i]; |
| 2087 | old_obj_priv = reg->obj->driver_private; |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2088 | |
| 2089 | if (old_obj_priv->pin_count) |
| 2090 | continue; |
| 2091 | |
| 2092 | /* i915 uses fences for GPU access to tiled buffers */ |
| 2093 | if (IS_I965G(dev) || !old_obj_priv->active) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2094 | break; |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2095 | |
| 2096 | /* find the seqno of the first available fence */ |
| 2097 | this_seqno = old_obj_priv->last_rendering_seqno; |
| 2098 | if (this_seqno != 0 && |
| 2099 | reg->obj->write_domain == 0 && |
| 2100 | i915_seqno_passed(seqno, this_seqno)) |
| 2101 | seqno = this_seqno; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | /* |
| 2105 | * Now things get ugly... we have to wait for one of the |
| 2106 | * objects to finish before trying again. |
| 2107 | */ |
| 2108 | if (i == dev_priv->num_fence_regs) { |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2109 | if (seqno == dev_priv->mm.next_gem_seqno) { |
| 2110 | i915_gem_flush(dev, |
| 2111 | I915_GEM_GPU_DOMAINS, |
| 2112 | I915_GEM_GPU_DOMAINS); |
| 2113 | seqno = i915_add_request(dev, |
| 2114 | I915_GEM_GPU_DOMAINS); |
| 2115 | if (seqno == 0) |
| 2116 | return -ENOMEM; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2117 | } |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2118 | |
| 2119 | ret = i915_wait_request(dev, seqno); |
| 2120 | if (ret) |
| 2121 | return ret; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2122 | goto try_again; |
| 2123 | } |
| 2124 | |
Chris Wilson | d7619c4 | 2009-02-11 14:26:47 +0000 | [diff] [blame] | 2125 | BUG_ON(old_obj_priv->active || |
| 2126 | (reg->obj->write_domain & I915_GEM_GPU_DOMAINS)); |
| 2127 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2128 | /* |
| 2129 | * Zap this virtual mapping so we can set up a fence again |
| 2130 | * for this object next time we need it. |
| 2131 | */ |
| 2132 | offset = ((loff_t) reg->obj->map_list.hash.key) << PAGE_SHIFT; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2133 | if (dev->dev_mapping) |
| 2134 | unmap_mapping_range(dev->dev_mapping, offset, |
| 2135 | reg->obj->size, 1); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2136 | old_obj_priv->fence_reg = I915_FENCE_REG_NONE; |
| 2137 | } |
| 2138 | |
| 2139 | obj_priv->fence_reg = i; |
| 2140 | reg->obj = obj; |
| 2141 | |
| 2142 | if (IS_I965G(dev)) |
| 2143 | i965_write_fence_reg(reg); |
| 2144 | else if (IS_I9XX(dev)) |
| 2145 | i915_write_fence_reg(reg); |
| 2146 | else |
| 2147 | i830_write_fence_reg(reg); |
Eric Anholt | d9ddcb9 | 2009-01-27 10:33:49 -0800 | [diff] [blame] | 2148 | |
| 2149 | return 0; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /** |
| 2153 | * i915_gem_clear_fence_reg - clear out fence register info |
| 2154 | * @obj: object to clear |
| 2155 | * |
| 2156 | * Zeroes out the fence register itself and clears out the associated |
| 2157 | * data structures in dev_priv and obj_priv. |
| 2158 | */ |
| 2159 | static void |
| 2160 | i915_gem_clear_fence_reg(struct drm_gem_object *obj) |
| 2161 | { |
| 2162 | struct drm_device *dev = obj->dev; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2163 | drm_i915_private_t *dev_priv = dev->dev_private; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2164 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2165 | |
| 2166 | if (IS_I965G(dev)) |
| 2167 | I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0); |
Eric Anholt | dc529a4 | 2009-03-10 22:34:49 -0700 | [diff] [blame] | 2168 | else { |
| 2169 | uint32_t fence_reg; |
| 2170 | |
| 2171 | if (obj_priv->fence_reg < 8) |
| 2172 | fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4; |
| 2173 | else |
| 2174 | fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - |
| 2175 | 8) * 4; |
| 2176 | |
| 2177 | I915_WRITE(fence_reg, 0); |
| 2178 | } |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 2179 | |
| 2180 | dev_priv->fence_regs[obj_priv->fence_reg].obj = NULL; |
| 2181 | obj_priv->fence_reg = I915_FENCE_REG_NONE; |
| 2182 | } |
| 2183 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2184 | /** |
| 2185 | * Finds free space in the GTT aperture and binds the object there. |
| 2186 | */ |
| 2187 | static int |
| 2188 | i915_gem_object_bind_to_gtt(struct drm_gem_object *obj, unsigned alignment) |
| 2189 | { |
| 2190 | struct drm_device *dev = obj->dev; |
| 2191 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 2192 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2193 | struct drm_mm_node *free_space; |
| 2194 | int page_count, ret; |
| 2195 | |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 2196 | if (dev_priv->mm.suspended) |
| 2197 | return -EBUSY; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2198 | if (alignment == 0) |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 2199 | alignment = i915_gem_get_gtt_alignment(obj); |
Daniel Vetter | 8d7773a | 2009-03-29 14:09:41 +0200 | [diff] [blame^] | 2200 | if (alignment & (i915_gem_get_gtt_alignment(obj) - 1)) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2201 | DRM_ERROR("Invalid object alignment requested %u\n", alignment); |
| 2202 | return -EINVAL; |
| 2203 | } |
| 2204 | |
| 2205 | search_free: |
| 2206 | free_space = drm_mm_search_free(&dev_priv->mm.gtt_space, |
| 2207 | obj->size, alignment, 0); |
| 2208 | if (free_space != NULL) { |
| 2209 | obj_priv->gtt_space = drm_mm_get_block(free_space, obj->size, |
| 2210 | alignment); |
| 2211 | if (obj_priv->gtt_space != NULL) { |
| 2212 | obj_priv->gtt_space->private = obj; |
| 2213 | obj_priv->gtt_offset = obj_priv->gtt_space->start; |
| 2214 | } |
| 2215 | } |
| 2216 | if (obj_priv->gtt_space == NULL) { |
| 2217 | /* If the gtt is empty and we're still having trouble |
| 2218 | * fitting our object in, we're out of memory. |
| 2219 | */ |
| 2220 | #if WATCH_LRU |
| 2221 | DRM_INFO("%s: GTT full, evicting something\n", __func__); |
| 2222 | #endif |
| 2223 | if (list_empty(&dev_priv->mm.inactive_list) && |
| 2224 | list_empty(&dev_priv->mm.flushing_list) && |
| 2225 | list_empty(&dev_priv->mm.active_list)) { |
| 2226 | DRM_ERROR("GTT full, but LRU list empty\n"); |
| 2227 | return -ENOMEM; |
| 2228 | } |
| 2229 | |
| 2230 | ret = i915_gem_evict_something(dev); |
| 2231 | if (ret != 0) { |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 2232 | if (ret != -ERESTARTSYS) |
| 2233 | DRM_ERROR("Failed to evict a buffer %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2234 | return ret; |
| 2235 | } |
| 2236 | goto search_free; |
| 2237 | } |
| 2238 | |
| 2239 | #if WATCH_BUF |
| 2240 | DRM_INFO("Binding object of size %d at 0x%08x\n", |
| 2241 | obj->size, obj_priv->gtt_offset); |
| 2242 | #endif |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2243 | ret = i915_gem_object_get_pages(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2244 | if (ret) { |
| 2245 | drm_mm_put_block(obj_priv->gtt_space); |
| 2246 | obj_priv->gtt_space = NULL; |
| 2247 | return ret; |
| 2248 | } |
| 2249 | |
| 2250 | page_count = obj->size / PAGE_SIZE; |
| 2251 | /* Create an AGP memory structure pointing at our pages, and bind it |
| 2252 | * into the GTT. |
| 2253 | */ |
| 2254 | obj_priv->agp_mem = drm_agp_bind_pages(dev, |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2255 | obj_priv->pages, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2256 | page_count, |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 2257 | obj_priv->gtt_offset, |
| 2258 | obj_priv->agp_type); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2259 | if (obj_priv->agp_mem == NULL) { |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2260 | i915_gem_object_put_pages(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2261 | drm_mm_put_block(obj_priv->gtt_space); |
| 2262 | obj_priv->gtt_space = NULL; |
| 2263 | return -ENOMEM; |
| 2264 | } |
| 2265 | atomic_inc(&dev->gtt_count); |
| 2266 | atomic_add(obj->size, &dev->gtt_memory); |
| 2267 | |
| 2268 | /* Assert that the object is not currently in any GPU domain. As it |
| 2269 | * wasn't in the GTT, there shouldn't be any way it could have been in |
| 2270 | * a GPU cache |
| 2271 | */ |
| 2272 | BUG_ON(obj->read_domains & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); |
| 2273 | BUG_ON(obj->write_domain & ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); |
| 2274 | |
| 2275 | return 0; |
| 2276 | } |
| 2277 | |
| 2278 | void |
| 2279 | i915_gem_clflush_object(struct drm_gem_object *obj) |
| 2280 | { |
| 2281 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2282 | |
| 2283 | /* If we don't have a page list set up, then we're not pinned |
| 2284 | * to GPU, and we can ignore the cache flush because it'll happen |
| 2285 | * again at bind time. |
| 2286 | */ |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2287 | if (obj_priv->pages == NULL) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2288 | return; |
| 2289 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2290 | drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2291 | } |
| 2292 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2293 | /** Flushes any GPU write domain for the object if it's dirty. */ |
| 2294 | static void |
| 2295 | i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj) |
| 2296 | { |
| 2297 | struct drm_device *dev = obj->dev; |
| 2298 | uint32_t seqno; |
| 2299 | |
| 2300 | if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0) |
| 2301 | return; |
| 2302 | |
| 2303 | /* Queue the GPU write cache flushing we need. */ |
| 2304 | i915_gem_flush(dev, 0, obj->write_domain); |
| 2305 | seqno = i915_add_request(dev, obj->write_domain); |
| 2306 | obj->write_domain = 0; |
| 2307 | i915_gem_object_move_to_active(obj, seqno); |
| 2308 | } |
| 2309 | |
| 2310 | /** Flushes the GTT write domain for the object if it's dirty. */ |
| 2311 | static void |
| 2312 | i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj) |
| 2313 | { |
| 2314 | if (obj->write_domain != I915_GEM_DOMAIN_GTT) |
| 2315 | return; |
| 2316 | |
| 2317 | /* No actual flushing is required for the GTT write domain. Writes |
| 2318 | * to it immediately go to main memory as far as we know, so there's |
| 2319 | * no chipset flush. It also doesn't land in render cache. |
| 2320 | */ |
| 2321 | obj->write_domain = 0; |
| 2322 | } |
| 2323 | |
| 2324 | /** Flushes the CPU write domain for the object if it's dirty. */ |
| 2325 | static void |
| 2326 | i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj) |
| 2327 | { |
| 2328 | struct drm_device *dev = obj->dev; |
| 2329 | |
| 2330 | if (obj->write_domain != I915_GEM_DOMAIN_CPU) |
| 2331 | return; |
| 2332 | |
| 2333 | i915_gem_clflush_object(obj); |
| 2334 | drm_agp_chipset_flush(dev); |
| 2335 | obj->write_domain = 0; |
| 2336 | } |
| 2337 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2338 | /** |
| 2339 | * Moves a single object to the GTT read, and possibly write domain. |
| 2340 | * |
| 2341 | * This function returns when the move is complete, including waiting on |
| 2342 | * flushes to occur. |
| 2343 | */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 2344 | int |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2345 | i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write) |
| 2346 | { |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2347 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2348 | int ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2349 | |
Eric Anholt | 0235439 | 2008-11-26 13:58:13 -0800 | [diff] [blame] | 2350 | /* Not valid to be called on unbound objects. */ |
| 2351 | if (obj_priv->gtt_space == NULL) |
| 2352 | return -EINVAL; |
| 2353 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2354 | i915_gem_object_flush_gpu_write_domain(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2355 | /* Wait on any GPU rendering and flushing to occur. */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2356 | ret = i915_gem_object_wait_rendering(obj); |
| 2357 | if (ret != 0) |
| 2358 | return ret; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2359 | |
| 2360 | /* If we're writing through the GTT domain, then CPU and GPU caches |
| 2361 | * will need to be invalidated at next use. |
| 2362 | */ |
| 2363 | if (write) |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2364 | obj->read_domains &= I915_GEM_DOMAIN_GTT; |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2365 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2366 | i915_gem_object_flush_cpu_write_domain(obj); |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2367 | |
| 2368 | /* It should now be out of any other write domains, and we can update |
| 2369 | * the domain values for our changes. |
| 2370 | */ |
| 2371 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0); |
| 2372 | obj->read_domains |= I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2373 | if (write) { |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2374 | obj->write_domain = I915_GEM_DOMAIN_GTT; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2375 | obj_priv->dirty = 1; |
| 2376 | } |
| 2377 | |
| 2378 | return 0; |
| 2379 | } |
| 2380 | |
| 2381 | /** |
| 2382 | * Moves a single object to the CPU read, and possibly write domain. |
| 2383 | * |
| 2384 | * This function returns when the move is complete, including waiting on |
| 2385 | * flushes to occur. |
| 2386 | */ |
| 2387 | static int |
| 2388 | i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write) |
| 2389 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2390 | int ret; |
| 2391 | |
| 2392 | i915_gem_object_flush_gpu_write_domain(obj); |
| 2393 | /* Wait on any GPU rendering and flushing to occur. */ |
| 2394 | ret = i915_gem_object_wait_rendering(obj); |
| 2395 | if (ret != 0) |
| 2396 | return ret; |
| 2397 | |
| 2398 | i915_gem_object_flush_gtt_write_domain(obj); |
| 2399 | |
| 2400 | /* If we have a partially-valid cache of the object in the CPU, |
| 2401 | * finish invalidating it and free the per-page flags. |
| 2402 | */ |
| 2403 | i915_gem_object_set_to_full_cpu_read_domain(obj); |
| 2404 | |
| 2405 | /* Flush the CPU cache if it's still invalid. */ |
| 2406 | if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) { |
| 2407 | i915_gem_clflush_object(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2408 | |
| 2409 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 2410 | } |
| 2411 | |
| 2412 | /* It should now be out of any other write domains, and we can update |
| 2413 | * the domain values for our changes. |
| 2414 | */ |
| 2415 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 2416 | |
| 2417 | /* If we're writing through the CPU, then the GPU read domains will |
| 2418 | * need to be invalidated at next use. |
| 2419 | */ |
| 2420 | if (write) { |
| 2421 | obj->read_domains &= I915_GEM_DOMAIN_CPU; |
| 2422 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 2423 | } |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2424 | |
| 2425 | return 0; |
| 2426 | } |
| 2427 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2428 | /* |
| 2429 | * Set the next domain for the specified object. This |
| 2430 | * may not actually perform the necessary flushing/invaliding though, |
| 2431 | * as that may want to be batched with other set_domain operations |
| 2432 | * |
| 2433 | * This is (we hope) the only really tricky part of gem. The goal |
| 2434 | * is fairly simple -- track which caches hold bits of the object |
| 2435 | * and make sure they remain coherent. A few concrete examples may |
| 2436 | * help to explain how it works. For shorthand, we use the notation |
| 2437 | * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the |
| 2438 | * a pair of read and write domain masks. |
| 2439 | * |
| 2440 | * Case 1: the batch buffer |
| 2441 | * |
| 2442 | * 1. Allocated |
| 2443 | * 2. Written by CPU |
| 2444 | * 3. Mapped to GTT |
| 2445 | * 4. Read by GPU |
| 2446 | * 5. Unmapped from GTT |
| 2447 | * 6. Freed |
| 2448 | * |
| 2449 | * Let's take these a step at a time |
| 2450 | * |
| 2451 | * 1. Allocated |
| 2452 | * Pages allocated from the kernel may still have |
| 2453 | * cache contents, so we set them to (CPU, CPU) always. |
| 2454 | * 2. Written by CPU (using pwrite) |
| 2455 | * The pwrite function calls set_domain (CPU, CPU) and |
| 2456 | * this function does nothing (as nothing changes) |
| 2457 | * 3. Mapped by GTT |
| 2458 | * This function asserts that the object is not |
| 2459 | * currently in any GPU-based read or write domains |
| 2460 | * 4. Read by GPU |
| 2461 | * i915_gem_execbuffer calls set_domain (COMMAND, 0). |
| 2462 | * As write_domain is zero, this function adds in the |
| 2463 | * current read domains (CPU+COMMAND, 0). |
| 2464 | * flush_domains is set to CPU. |
| 2465 | * invalidate_domains is set to COMMAND |
| 2466 | * clflush is run to get data out of the CPU caches |
| 2467 | * then i915_dev_set_domain calls i915_gem_flush to |
| 2468 | * emit an MI_FLUSH and drm_agp_chipset_flush |
| 2469 | * 5. Unmapped from GTT |
| 2470 | * i915_gem_object_unbind calls set_domain (CPU, CPU) |
| 2471 | * flush_domains and invalidate_domains end up both zero |
| 2472 | * so no flushing/invalidating happens |
| 2473 | * 6. Freed |
| 2474 | * yay, done |
| 2475 | * |
| 2476 | * Case 2: The shared render buffer |
| 2477 | * |
| 2478 | * 1. Allocated |
| 2479 | * 2. Mapped to GTT |
| 2480 | * 3. Read/written by GPU |
| 2481 | * 4. set_domain to (CPU,CPU) |
| 2482 | * 5. Read/written by CPU |
| 2483 | * 6. Read/written by GPU |
| 2484 | * |
| 2485 | * 1. Allocated |
| 2486 | * Same as last example, (CPU, CPU) |
| 2487 | * 2. Mapped to GTT |
| 2488 | * Nothing changes (assertions find that it is not in the GPU) |
| 2489 | * 3. Read/written by GPU |
| 2490 | * execbuffer calls set_domain (RENDER, RENDER) |
| 2491 | * flush_domains gets CPU |
| 2492 | * invalidate_domains gets GPU |
| 2493 | * clflush (obj) |
| 2494 | * MI_FLUSH and drm_agp_chipset_flush |
| 2495 | * 4. set_domain (CPU, CPU) |
| 2496 | * flush_domains gets GPU |
| 2497 | * invalidate_domains gets CPU |
| 2498 | * wait_rendering (obj) to make sure all drawing is complete. |
| 2499 | * This will include an MI_FLUSH to get the data from GPU |
| 2500 | * to memory |
| 2501 | * clflush (obj) to invalidate the CPU cache |
| 2502 | * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?) |
| 2503 | * 5. Read/written by CPU |
| 2504 | * cache lines are loaded and dirtied |
| 2505 | * 6. Read written by GPU |
| 2506 | * Same as last GPU access |
| 2507 | * |
| 2508 | * Case 3: The constant buffer |
| 2509 | * |
| 2510 | * 1. Allocated |
| 2511 | * 2. Written by CPU |
| 2512 | * 3. Read by GPU |
| 2513 | * 4. Updated (written) by CPU again |
| 2514 | * 5. Read by GPU |
| 2515 | * |
| 2516 | * 1. Allocated |
| 2517 | * (CPU, CPU) |
| 2518 | * 2. Written by CPU |
| 2519 | * (CPU, CPU) |
| 2520 | * 3. Read by GPU |
| 2521 | * (CPU+RENDER, 0) |
| 2522 | * flush_domains = CPU |
| 2523 | * invalidate_domains = RENDER |
| 2524 | * clflush (obj) |
| 2525 | * MI_FLUSH |
| 2526 | * drm_agp_chipset_flush |
| 2527 | * 4. Updated (written) by CPU again |
| 2528 | * (CPU, CPU) |
| 2529 | * flush_domains = 0 (no previous write domain) |
| 2530 | * invalidate_domains = 0 (no new read domains) |
| 2531 | * 5. Read by GPU |
| 2532 | * (CPU+RENDER, 0) |
| 2533 | * flush_domains = CPU |
| 2534 | * invalidate_domains = RENDER |
| 2535 | * clflush (obj) |
| 2536 | * MI_FLUSH |
| 2537 | * drm_agp_chipset_flush |
| 2538 | */ |
Keith Packard | c0d9082 | 2008-11-20 23:11:08 -0800 | [diff] [blame] | 2539 | static void |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2540 | i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2541 | { |
| 2542 | struct drm_device *dev = obj->dev; |
| 2543 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2544 | uint32_t invalidate_domains = 0; |
| 2545 | uint32_t flush_domains = 0; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2546 | |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2547 | BUG_ON(obj->pending_read_domains & I915_GEM_DOMAIN_CPU); |
| 2548 | BUG_ON(obj->pending_write_domain == I915_GEM_DOMAIN_CPU); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2549 | |
| 2550 | #if WATCH_BUF |
| 2551 | DRM_INFO("%s: object %p read %08x -> %08x write %08x -> %08x\n", |
| 2552 | __func__, obj, |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2553 | obj->read_domains, obj->pending_read_domains, |
| 2554 | obj->write_domain, obj->pending_write_domain); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2555 | #endif |
| 2556 | /* |
| 2557 | * If the object isn't moving to a new write domain, |
| 2558 | * let the object stay in multiple read domains |
| 2559 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2560 | if (obj->pending_write_domain == 0) |
| 2561 | obj->pending_read_domains |= obj->read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2562 | else |
| 2563 | obj_priv->dirty = 1; |
| 2564 | |
| 2565 | /* |
| 2566 | * Flush the current write domain if |
| 2567 | * the new read domains don't match. Invalidate |
| 2568 | * any read domains which differ from the old |
| 2569 | * write domain |
| 2570 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2571 | if (obj->write_domain && |
| 2572 | obj->write_domain != obj->pending_read_domains) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2573 | flush_domains |= obj->write_domain; |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2574 | invalidate_domains |= |
| 2575 | obj->pending_read_domains & ~obj->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2576 | } |
| 2577 | /* |
| 2578 | * Invalidate any read caches which may have |
| 2579 | * stale data. That is, any new read domains. |
| 2580 | */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2581 | invalidate_domains |= obj->pending_read_domains & ~obj->read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2582 | if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU) { |
| 2583 | #if WATCH_BUF |
| 2584 | DRM_INFO("%s: CPU domain flush %08x invalidate %08x\n", |
| 2585 | __func__, flush_domains, invalidate_domains); |
| 2586 | #endif |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2587 | i915_gem_clflush_object(obj); |
| 2588 | } |
| 2589 | |
Eric Anholt | efbeed9 | 2009-02-19 14:54:51 -0800 | [diff] [blame] | 2590 | /* The actual obj->write_domain will be updated with |
| 2591 | * pending_write_domain after we emit the accumulated flush for all |
| 2592 | * of our domain changes in execbuffers (which clears objects' |
| 2593 | * write_domains). So if we have a current write domain that we |
| 2594 | * aren't changing, set pending_write_domain to that. |
| 2595 | */ |
| 2596 | if (flush_domains == 0 && obj->pending_write_domain == 0) |
| 2597 | obj->pending_write_domain = obj->write_domain; |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 2598 | obj->read_domains = obj->pending_read_domains; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2599 | |
| 2600 | dev->invalidate_domains |= invalidate_domains; |
| 2601 | dev->flush_domains |= flush_domains; |
| 2602 | #if WATCH_BUF |
| 2603 | DRM_INFO("%s: read %08x write %08x invalidate %08x flush %08x\n", |
| 2604 | __func__, |
| 2605 | obj->read_domains, obj->write_domain, |
| 2606 | dev->invalidate_domains, dev->flush_domains); |
| 2607 | #endif |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2608 | } |
| 2609 | |
| 2610 | /** |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2611 | * Moves the object from a partially CPU read to a full one. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2612 | * |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2613 | * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(), |
| 2614 | * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU). |
| 2615 | */ |
| 2616 | static void |
| 2617 | i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj) |
| 2618 | { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2619 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2620 | |
| 2621 | if (!obj_priv->page_cpu_valid) |
| 2622 | return; |
| 2623 | |
| 2624 | /* If we're partially in the CPU read domain, finish moving it in. |
| 2625 | */ |
| 2626 | if (obj->read_domains & I915_GEM_DOMAIN_CPU) { |
| 2627 | int i; |
| 2628 | |
| 2629 | for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) { |
| 2630 | if (obj_priv->page_cpu_valid[i]) |
| 2631 | continue; |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2632 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2633 | } |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2634 | } |
| 2635 | |
| 2636 | /* Free the page_cpu_valid mappings which are now stale, whether |
| 2637 | * or not we've got I915_GEM_DOMAIN_CPU. |
| 2638 | */ |
| 2639 | drm_free(obj_priv->page_cpu_valid, obj->size / PAGE_SIZE, |
| 2640 | DRM_MEM_DRIVER); |
| 2641 | obj_priv->page_cpu_valid = NULL; |
| 2642 | } |
| 2643 | |
| 2644 | /** |
| 2645 | * Set the CPU read domain on a range of the object. |
| 2646 | * |
| 2647 | * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's |
| 2648 | * not entirely valid. The page_cpu_valid member of the object flags which |
| 2649 | * pages have been flushed, and will be respected by |
| 2650 | * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping |
| 2651 | * of the whole object. |
| 2652 | * |
| 2653 | * This function returns when the move is complete, including waiting on |
| 2654 | * flushes to occur. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2655 | */ |
| 2656 | static int |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2657 | i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj, |
| 2658 | uint64_t offset, uint64_t size) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2659 | { |
| 2660 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2661 | int i, ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2662 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2663 | if (offset == 0 && size == obj->size) |
| 2664 | return i915_gem_object_set_to_cpu_domain(obj, 0); |
| 2665 | |
| 2666 | i915_gem_object_flush_gpu_write_domain(obj); |
| 2667 | /* Wait on any GPU rendering and flushing to occur. */ |
| 2668 | ret = i915_gem_object_wait_rendering(obj); |
| 2669 | if (ret != 0) |
| 2670 | return ret; |
| 2671 | i915_gem_object_flush_gtt_write_domain(obj); |
| 2672 | |
| 2673 | /* If we're already fully in the CPU read domain, we're done. */ |
| 2674 | if (obj_priv->page_cpu_valid == NULL && |
| 2675 | (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2676 | return 0; |
| 2677 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2678 | /* Otherwise, create/clear the per-page CPU read domain flag if we're |
| 2679 | * newly adding I915_GEM_DOMAIN_CPU |
| 2680 | */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2681 | if (obj_priv->page_cpu_valid == NULL) { |
| 2682 | obj_priv->page_cpu_valid = drm_calloc(1, obj->size / PAGE_SIZE, |
| 2683 | DRM_MEM_DRIVER); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2684 | if (obj_priv->page_cpu_valid == NULL) |
| 2685 | return -ENOMEM; |
| 2686 | } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) |
| 2687 | memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2688 | |
| 2689 | /* Flush the cache on any pages that are still invalid from the CPU's |
| 2690 | * perspective. |
| 2691 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2692 | for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE; |
| 2693 | i++) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2694 | if (obj_priv->page_cpu_valid[i]) |
| 2695 | continue; |
| 2696 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 2697 | drm_clflush_pages(obj_priv->pages + i, 1); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2698 | |
| 2699 | obj_priv->page_cpu_valid[i] = 1; |
| 2700 | } |
| 2701 | |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2702 | /* It should now be out of any other write domains, and we can update |
| 2703 | * the domain values for our changes. |
| 2704 | */ |
| 2705 | BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0); |
| 2706 | |
| 2707 | obj->read_domains |= I915_GEM_DOMAIN_CPU; |
| 2708 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2709 | return 0; |
| 2710 | } |
| 2711 | |
| 2712 | /** |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2713 | * Pin an object to the GTT and evaluate the relocations landing in it. |
| 2714 | */ |
| 2715 | static int |
| 2716 | i915_gem_object_pin_and_relocate(struct drm_gem_object *obj, |
| 2717 | struct drm_file *file_priv, |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2718 | struct drm_i915_gem_exec_object *entry, |
| 2719 | struct drm_i915_gem_relocation_entry *relocs) |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2720 | { |
| 2721 | struct drm_device *dev = obj->dev; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 2722 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2723 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 2724 | int i, ret; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 2725 | void __iomem *reloc_page; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2726 | |
| 2727 | /* Choose the GTT offset for our buffer and put it there. */ |
| 2728 | ret = i915_gem_object_pin(obj, (uint32_t) entry->alignment); |
| 2729 | if (ret) |
| 2730 | return ret; |
| 2731 | |
| 2732 | entry->offset = obj_priv->gtt_offset; |
| 2733 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2734 | /* Apply the relocations, using the GTT aperture to avoid cache |
| 2735 | * flushing requirements. |
| 2736 | */ |
| 2737 | for (i = 0; i < entry->relocation_count; i++) { |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2738 | struct drm_i915_gem_relocation_entry *reloc= &relocs[i]; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2739 | struct drm_gem_object *target_obj; |
| 2740 | struct drm_i915_gem_object *target_obj_priv; |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 2741 | uint32_t reloc_val, reloc_offset; |
| 2742 | uint32_t __iomem *reloc_entry; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2743 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2744 | target_obj = drm_gem_object_lookup(obj->dev, file_priv, |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2745 | reloc->target_handle); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2746 | if (target_obj == NULL) { |
| 2747 | i915_gem_object_unpin(obj); |
| 2748 | return -EBADF; |
| 2749 | } |
| 2750 | target_obj_priv = target_obj->driver_private; |
| 2751 | |
| 2752 | /* The target buffer should have appeared before us in the |
| 2753 | * exec_object list, so it should have a GTT space bound by now. |
| 2754 | */ |
| 2755 | if (target_obj_priv->gtt_space == NULL) { |
| 2756 | DRM_ERROR("No GTT space found for object %d\n", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2757 | reloc->target_handle); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2758 | drm_gem_object_unreference(target_obj); |
| 2759 | i915_gem_object_unpin(obj); |
| 2760 | return -EINVAL; |
| 2761 | } |
| 2762 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2763 | if (reloc->offset > obj->size - 4) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2764 | DRM_ERROR("Relocation beyond object bounds: " |
| 2765 | "obj %p target %d offset %d size %d.\n", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2766 | obj, reloc->target_handle, |
| 2767 | (int) reloc->offset, (int) obj->size); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2768 | drm_gem_object_unreference(target_obj); |
| 2769 | i915_gem_object_unpin(obj); |
| 2770 | return -EINVAL; |
| 2771 | } |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2772 | if (reloc->offset & 3) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2773 | DRM_ERROR("Relocation not 4-byte aligned: " |
| 2774 | "obj %p target %d offset %d.\n", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2775 | obj, reloc->target_handle, |
| 2776 | (int) reloc->offset); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2777 | drm_gem_object_unreference(target_obj); |
| 2778 | i915_gem_object_unpin(obj); |
| 2779 | return -EINVAL; |
| 2780 | } |
| 2781 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2782 | if (reloc->write_domain & I915_GEM_DOMAIN_CPU || |
| 2783 | reloc->read_domains & I915_GEM_DOMAIN_CPU) { |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2784 | DRM_ERROR("reloc with read/write CPU domains: " |
| 2785 | "obj %p target %d offset %d " |
| 2786 | "read %08x write %08x", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2787 | obj, reloc->target_handle, |
| 2788 | (int) reloc->offset, |
| 2789 | reloc->read_domains, |
| 2790 | reloc->write_domain); |
Chris Wilson | 491152b | 2009-02-11 14:26:32 +0000 | [diff] [blame] | 2791 | drm_gem_object_unreference(target_obj); |
| 2792 | i915_gem_object_unpin(obj); |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 2793 | return -EINVAL; |
| 2794 | } |
| 2795 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2796 | if (reloc->write_domain && target_obj->pending_write_domain && |
| 2797 | reloc->write_domain != target_obj->pending_write_domain) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2798 | DRM_ERROR("Write domain conflict: " |
| 2799 | "obj %p target %d offset %d " |
| 2800 | "new %08x old %08x\n", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2801 | obj, reloc->target_handle, |
| 2802 | (int) reloc->offset, |
| 2803 | reloc->write_domain, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2804 | target_obj->pending_write_domain); |
| 2805 | drm_gem_object_unreference(target_obj); |
| 2806 | i915_gem_object_unpin(obj); |
| 2807 | return -EINVAL; |
| 2808 | } |
| 2809 | |
| 2810 | #if WATCH_RELOC |
| 2811 | DRM_INFO("%s: obj %p offset %08x target %d " |
| 2812 | "read %08x write %08x gtt %08x " |
| 2813 | "presumed %08x delta %08x\n", |
| 2814 | __func__, |
| 2815 | obj, |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2816 | (int) reloc->offset, |
| 2817 | (int) reloc->target_handle, |
| 2818 | (int) reloc->read_domains, |
| 2819 | (int) reloc->write_domain, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2820 | (int) target_obj_priv->gtt_offset, |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2821 | (int) reloc->presumed_offset, |
| 2822 | reloc->delta); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2823 | #endif |
| 2824 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2825 | target_obj->pending_read_domains |= reloc->read_domains; |
| 2826 | target_obj->pending_write_domain |= reloc->write_domain; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2827 | |
| 2828 | /* If the relocation already has the right value in it, no |
| 2829 | * more work needs to be done. |
| 2830 | */ |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2831 | if (target_obj_priv->gtt_offset == reloc->presumed_offset) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2832 | drm_gem_object_unreference(target_obj); |
| 2833 | continue; |
| 2834 | } |
| 2835 | |
Eric Anholt | 2ef7eea | 2008-11-10 10:53:25 -0800 | [diff] [blame] | 2836 | ret = i915_gem_object_set_to_gtt_domain(obj, 1); |
| 2837 | if (ret != 0) { |
| 2838 | drm_gem_object_unreference(target_obj); |
| 2839 | i915_gem_object_unpin(obj); |
| 2840 | return -EINVAL; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2841 | } |
| 2842 | |
| 2843 | /* Map the page containing the relocation we're going to |
| 2844 | * perform. |
| 2845 | */ |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2846 | reloc_offset = obj_priv->gtt_offset + reloc->offset; |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 2847 | reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping, |
| 2848 | (reloc_offset & |
| 2849 | ~(PAGE_SIZE - 1))); |
Eric Anholt | 3043c60 | 2008-10-02 12:24:47 -0700 | [diff] [blame] | 2850 | reloc_entry = (uint32_t __iomem *)(reloc_page + |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 2851 | (reloc_offset & (PAGE_SIZE - 1))); |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2852 | reloc_val = target_obj_priv->gtt_offset + reloc->delta; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2853 | |
| 2854 | #if WATCH_BUF |
| 2855 | DRM_INFO("Applied relocation: %p@0x%08x %08x -> %08x\n", |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2856 | obj, (unsigned int) reloc->offset, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2857 | readl(reloc_entry), reloc_val); |
| 2858 | #endif |
| 2859 | writel(reloc_val, reloc_entry); |
Keith Packard | 0839ccb | 2008-10-30 19:38:48 -0700 | [diff] [blame] | 2860 | io_mapping_unmap_atomic(reloc_page); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2861 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2862 | /* The updated presumed offset for this entry will be |
| 2863 | * copied back out to the user. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2864 | */ |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2865 | reloc->presumed_offset = target_obj_priv->gtt_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2866 | |
| 2867 | drm_gem_object_unreference(target_obj); |
| 2868 | } |
| 2869 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2870 | #if WATCH_BUF |
| 2871 | if (0) |
| 2872 | i915_gem_dump_object(obj, 128, __func__, ~0); |
| 2873 | #endif |
| 2874 | return 0; |
| 2875 | } |
| 2876 | |
| 2877 | /** Dispatch a batchbuffer to the ring |
| 2878 | */ |
| 2879 | static int |
| 2880 | i915_dispatch_gem_execbuffer(struct drm_device *dev, |
| 2881 | struct drm_i915_gem_execbuffer *exec, |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 2882 | struct drm_clip_rect *cliprects, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2883 | uint64_t exec_offset) |
| 2884 | { |
| 2885 | drm_i915_private_t *dev_priv = dev->dev_private; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2886 | int nbox = exec->num_cliprects; |
| 2887 | int i = 0, count; |
| 2888 | uint32_t exec_start, exec_len; |
| 2889 | RING_LOCALS; |
| 2890 | |
| 2891 | exec_start = (uint32_t) exec_offset + exec->batch_start_offset; |
| 2892 | exec_len = (uint32_t) exec->batch_len; |
| 2893 | |
| 2894 | if ((exec_start | exec_len) & 0x7) { |
| 2895 | DRM_ERROR("alignment\n"); |
| 2896 | return -EINVAL; |
| 2897 | } |
| 2898 | |
| 2899 | if (!exec_start) |
| 2900 | return -EINVAL; |
| 2901 | |
| 2902 | count = nbox ? nbox : 1; |
| 2903 | |
| 2904 | for (i = 0; i < count; i++) { |
| 2905 | if (i < nbox) { |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 2906 | int ret = i915_emit_box(dev, cliprects, i, |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 2907 | exec->DR1, exec->DR4); |
| 2908 | if (ret) |
| 2909 | return ret; |
| 2910 | } |
| 2911 | |
| 2912 | if (IS_I830(dev) || IS_845G(dev)) { |
| 2913 | BEGIN_LP_RING(4); |
| 2914 | OUT_RING(MI_BATCH_BUFFER); |
| 2915 | OUT_RING(exec_start | MI_BATCH_NON_SECURE); |
| 2916 | OUT_RING(exec_start + exec_len - 4); |
| 2917 | OUT_RING(0); |
| 2918 | ADVANCE_LP_RING(); |
| 2919 | } else { |
| 2920 | BEGIN_LP_RING(2); |
| 2921 | if (IS_I965G(dev)) { |
| 2922 | OUT_RING(MI_BATCH_BUFFER_START | |
| 2923 | (2 << 6) | |
| 2924 | MI_BATCH_NON_SECURE_I965); |
| 2925 | OUT_RING(exec_start); |
| 2926 | } else { |
| 2927 | OUT_RING(MI_BATCH_BUFFER_START | |
| 2928 | (2 << 6)); |
| 2929 | OUT_RING(exec_start | MI_BATCH_NON_SECURE); |
| 2930 | } |
| 2931 | ADVANCE_LP_RING(); |
| 2932 | } |
| 2933 | } |
| 2934 | |
| 2935 | /* XXX breadcrumb */ |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
| 2939 | /* Throttle our rendering by waiting until the ring has completed our requests |
| 2940 | * emitted over 20 msec ago. |
| 2941 | * |
| 2942 | * This should get us reasonable parallelism between CPU and GPU but also |
| 2943 | * relatively low latency when blocking on a particular request to finish. |
| 2944 | */ |
| 2945 | static int |
| 2946 | i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file_priv) |
| 2947 | { |
| 2948 | struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv; |
| 2949 | int ret = 0; |
| 2950 | uint32_t seqno; |
| 2951 | |
| 2952 | mutex_lock(&dev->struct_mutex); |
| 2953 | seqno = i915_file_priv->mm.last_gem_throttle_seqno; |
| 2954 | i915_file_priv->mm.last_gem_throttle_seqno = |
| 2955 | i915_file_priv->mm.last_gem_seqno; |
| 2956 | if (seqno) |
| 2957 | ret = i915_wait_request(dev, seqno); |
| 2958 | mutex_unlock(&dev->struct_mutex); |
| 2959 | return ret; |
| 2960 | } |
| 2961 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 2962 | static int |
| 2963 | i915_gem_get_relocs_from_user(struct drm_i915_gem_exec_object *exec_list, |
| 2964 | uint32_t buffer_count, |
| 2965 | struct drm_i915_gem_relocation_entry **relocs) |
| 2966 | { |
| 2967 | uint32_t reloc_count = 0, reloc_index = 0, i; |
| 2968 | int ret; |
| 2969 | |
| 2970 | *relocs = NULL; |
| 2971 | for (i = 0; i < buffer_count; i++) { |
| 2972 | if (reloc_count + exec_list[i].relocation_count < reloc_count) |
| 2973 | return -EINVAL; |
| 2974 | reloc_count += exec_list[i].relocation_count; |
| 2975 | } |
| 2976 | |
| 2977 | *relocs = drm_calloc(reloc_count, sizeof(**relocs), DRM_MEM_DRIVER); |
| 2978 | if (*relocs == NULL) |
| 2979 | return -ENOMEM; |
| 2980 | |
| 2981 | for (i = 0; i < buffer_count; i++) { |
| 2982 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 2983 | |
| 2984 | user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr; |
| 2985 | |
| 2986 | ret = copy_from_user(&(*relocs)[reloc_index], |
| 2987 | user_relocs, |
| 2988 | exec_list[i].relocation_count * |
| 2989 | sizeof(**relocs)); |
| 2990 | if (ret != 0) { |
| 2991 | drm_free(*relocs, reloc_count * sizeof(**relocs), |
| 2992 | DRM_MEM_DRIVER); |
| 2993 | *relocs = NULL; |
| 2994 | return ret; |
| 2995 | } |
| 2996 | |
| 2997 | reloc_index += exec_list[i].relocation_count; |
| 2998 | } |
| 2999 | |
| 3000 | return ret; |
| 3001 | } |
| 3002 | |
| 3003 | static int |
| 3004 | i915_gem_put_relocs_to_user(struct drm_i915_gem_exec_object *exec_list, |
| 3005 | uint32_t buffer_count, |
| 3006 | struct drm_i915_gem_relocation_entry *relocs) |
| 3007 | { |
| 3008 | uint32_t reloc_count = 0, i; |
| 3009 | int ret; |
| 3010 | |
| 3011 | for (i = 0; i < buffer_count; i++) { |
| 3012 | struct drm_i915_gem_relocation_entry __user *user_relocs; |
| 3013 | |
| 3014 | user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr; |
| 3015 | |
| 3016 | if (ret == 0) { |
| 3017 | ret = copy_to_user(user_relocs, |
| 3018 | &relocs[reloc_count], |
| 3019 | exec_list[i].relocation_count * |
| 3020 | sizeof(*relocs)); |
| 3021 | } |
| 3022 | |
| 3023 | reloc_count += exec_list[i].relocation_count; |
| 3024 | } |
| 3025 | |
| 3026 | drm_free(relocs, reloc_count * sizeof(*relocs), DRM_MEM_DRIVER); |
| 3027 | |
| 3028 | return ret; |
| 3029 | } |
| 3030 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3031 | int |
| 3032 | i915_gem_execbuffer(struct drm_device *dev, void *data, |
| 3033 | struct drm_file *file_priv) |
| 3034 | { |
| 3035 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3036 | struct drm_i915_file_private *i915_file_priv = file_priv->driver_priv; |
| 3037 | struct drm_i915_gem_execbuffer *args = data; |
| 3038 | struct drm_i915_gem_exec_object *exec_list = NULL; |
| 3039 | struct drm_gem_object **object_list = NULL; |
| 3040 | struct drm_gem_object *batch_obj; |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3041 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3042 | struct drm_clip_rect *cliprects = NULL; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3043 | struct drm_i915_gem_relocation_entry *relocs; |
| 3044 | int ret, ret2, i, pinned = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3045 | uint64_t exec_offset; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3046 | uint32_t seqno, flush_domains, reloc_index; |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3047 | int pin_tries; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3048 | |
| 3049 | #if WATCH_EXEC |
| 3050 | DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n", |
| 3051 | (int) args->buffers_ptr, args->buffer_count, args->batch_len); |
| 3052 | #endif |
| 3053 | |
Eric Anholt | 4f481ed | 2008-09-10 14:22:49 -0700 | [diff] [blame] | 3054 | if (args->buffer_count < 1) { |
| 3055 | DRM_ERROR("execbuf with %d buffers\n", args->buffer_count); |
| 3056 | return -EINVAL; |
| 3057 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3058 | /* Copy in the exec list from userland */ |
| 3059 | exec_list = drm_calloc(sizeof(*exec_list), args->buffer_count, |
| 3060 | DRM_MEM_DRIVER); |
| 3061 | object_list = drm_calloc(sizeof(*object_list), args->buffer_count, |
| 3062 | DRM_MEM_DRIVER); |
| 3063 | if (exec_list == NULL || object_list == NULL) { |
| 3064 | DRM_ERROR("Failed to allocate exec or object list " |
| 3065 | "for %d buffers\n", |
| 3066 | args->buffer_count); |
| 3067 | ret = -ENOMEM; |
| 3068 | goto pre_mutex_err; |
| 3069 | } |
| 3070 | ret = copy_from_user(exec_list, |
| 3071 | (struct drm_i915_relocation_entry __user *) |
| 3072 | (uintptr_t) args->buffers_ptr, |
| 3073 | sizeof(*exec_list) * args->buffer_count); |
| 3074 | if (ret != 0) { |
| 3075 | DRM_ERROR("copy %d exec entries failed %d\n", |
| 3076 | args->buffer_count, ret); |
| 3077 | goto pre_mutex_err; |
| 3078 | } |
| 3079 | |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3080 | if (args->num_cliprects != 0) { |
| 3081 | cliprects = drm_calloc(args->num_cliprects, sizeof(*cliprects), |
| 3082 | DRM_MEM_DRIVER); |
| 3083 | if (cliprects == NULL) |
| 3084 | goto pre_mutex_err; |
| 3085 | |
| 3086 | ret = copy_from_user(cliprects, |
| 3087 | (struct drm_clip_rect __user *) |
| 3088 | (uintptr_t) args->cliprects_ptr, |
| 3089 | sizeof(*cliprects) * args->num_cliprects); |
| 3090 | if (ret != 0) { |
| 3091 | DRM_ERROR("copy %d cliprects failed: %d\n", |
| 3092 | args->num_cliprects, ret); |
| 3093 | goto pre_mutex_err; |
| 3094 | } |
| 3095 | } |
| 3096 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3097 | ret = i915_gem_get_relocs_from_user(exec_list, args->buffer_count, |
| 3098 | &relocs); |
| 3099 | if (ret != 0) |
| 3100 | goto pre_mutex_err; |
| 3101 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3102 | mutex_lock(&dev->struct_mutex); |
| 3103 | |
| 3104 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3105 | |
| 3106 | if (dev_priv->mm.wedged) { |
| 3107 | DRM_ERROR("Execbuf while wedged\n"); |
| 3108 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3109 | ret = -EIO; |
| 3110 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | if (dev_priv->mm.suspended) { |
| 3114 | DRM_ERROR("Execbuf while VT-switched.\n"); |
| 3115 | mutex_unlock(&dev->struct_mutex); |
Chris Wilson | a198bc8 | 2009-02-06 16:55:20 +0000 | [diff] [blame] | 3116 | ret = -EBUSY; |
| 3117 | goto pre_mutex_err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3118 | } |
| 3119 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3120 | /* Look up object handles */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3121 | for (i = 0; i < args->buffer_count; i++) { |
| 3122 | object_list[i] = drm_gem_object_lookup(dev, file_priv, |
| 3123 | exec_list[i].handle); |
| 3124 | if (object_list[i] == NULL) { |
| 3125 | DRM_ERROR("Invalid object handle %d at index %d\n", |
| 3126 | exec_list[i].handle, i); |
| 3127 | ret = -EBADF; |
| 3128 | goto err; |
| 3129 | } |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3130 | |
| 3131 | obj_priv = object_list[i]->driver_private; |
| 3132 | if (obj_priv->in_execbuffer) { |
| 3133 | DRM_ERROR("Object %p appears more than once in object list\n", |
| 3134 | object_list[i]); |
| 3135 | ret = -EBADF; |
| 3136 | goto err; |
| 3137 | } |
| 3138 | obj_priv->in_execbuffer = true; |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3139 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3140 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3141 | /* Pin and relocate */ |
| 3142 | for (pin_tries = 0; ; pin_tries++) { |
| 3143 | ret = 0; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3144 | reloc_index = 0; |
| 3145 | |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3146 | for (i = 0; i < args->buffer_count; i++) { |
| 3147 | object_list[i]->pending_read_domains = 0; |
| 3148 | object_list[i]->pending_write_domain = 0; |
| 3149 | ret = i915_gem_object_pin_and_relocate(object_list[i], |
| 3150 | file_priv, |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3151 | &exec_list[i], |
| 3152 | &relocs[reloc_index]); |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3153 | if (ret) |
| 3154 | break; |
| 3155 | pinned = i + 1; |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3156 | reloc_index += exec_list[i].relocation_count; |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3157 | } |
| 3158 | /* success */ |
| 3159 | if (ret == 0) |
| 3160 | break; |
| 3161 | |
| 3162 | /* error other than GTT full, or we've already tried again */ |
| 3163 | if (ret != -ENOMEM || pin_tries >= 1) { |
Eric Anholt | f1acec9 | 2008-12-19 14:47:48 -0800 | [diff] [blame] | 3164 | if (ret != -ERESTARTSYS) |
| 3165 | DRM_ERROR("Failed to pin buffers %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3166 | goto err; |
| 3167 | } |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3168 | |
| 3169 | /* unpin all of our buffers */ |
| 3170 | for (i = 0; i < pinned; i++) |
| 3171 | i915_gem_object_unpin(object_list[i]); |
Eric Anholt | b117763 | 2008-12-10 10:09:41 -0800 | [diff] [blame] | 3172 | pinned = 0; |
Keith Packard | ac94a96 | 2008-11-20 23:30:27 -0800 | [diff] [blame] | 3173 | |
| 3174 | /* evict everyone we can from the aperture */ |
| 3175 | ret = i915_gem_evict_everything(dev); |
| 3176 | if (ret) |
| 3177 | goto err; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3178 | } |
| 3179 | |
| 3180 | /* Set the pending read domains for the batch buffer to COMMAND */ |
| 3181 | batch_obj = object_list[args->buffer_count-1]; |
| 3182 | batch_obj->pending_read_domains = I915_GEM_DOMAIN_COMMAND; |
| 3183 | batch_obj->pending_write_domain = 0; |
| 3184 | |
| 3185 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3186 | |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3187 | /* Zero the global flush/invalidate flags. These |
| 3188 | * will be modified as new domains are computed |
| 3189 | * for each object |
| 3190 | */ |
| 3191 | dev->invalidate_domains = 0; |
| 3192 | dev->flush_domains = 0; |
| 3193 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3194 | for (i = 0; i < args->buffer_count; i++) { |
| 3195 | struct drm_gem_object *obj = object_list[i]; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3196 | |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3197 | /* Compute new gpu domains and update invalidate/flush */ |
Eric Anholt | 8b0e378 | 2009-02-19 14:40:50 -0800 | [diff] [blame] | 3198 | i915_gem_object_set_to_gpu_domain(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3199 | } |
| 3200 | |
| 3201 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3202 | |
Keith Packard | 646f0f6 | 2008-11-20 23:23:03 -0800 | [diff] [blame] | 3203 | if (dev->invalidate_domains | dev->flush_domains) { |
| 3204 | #if WATCH_EXEC |
| 3205 | DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n", |
| 3206 | __func__, |
| 3207 | dev->invalidate_domains, |
| 3208 | dev->flush_domains); |
| 3209 | #endif |
| 3210 | i915_gem_flush(dev, |
| 3211 | dev->invalidate_domains, |
| 3212 | dev->flush_domains); |
| 3213 | if (dev->flush_domains) |
| 3214 | (void)i915_add_request(dev, dev->flush_domains); |
| 3215 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3216 | |
Eric Anholt | efbeed9 | 2009-02-19 14:54:51 -0800 | [diff] [blame] | 3217 | for (i = 0; i < args->buffer_count; i++) { |
| 3218 | struct drm_gem_object *obj = object_list[i]; |
| 3219 | |
| 3220 | obj->write_domain = obj->pending_write_domain; |
| 3221 | } |
| 3222 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3223 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3224 | |
| 3225 | #if WATCH_COHERENCY |
| 3226 | for (i = 0; i < args->buffer_count; i++) { |
| 3227 | i915_gem_object_check_coherency(object_list[i], |
| 3228 | exec_list[i].handle); |
| 3229 | } |
| 3230 | #endif |
| 3231 | |
| 3232 | exec_offset = exec_list[args->buffer_count - 1].offset; |
| 3233 | |
| 3234 | #if WATCH_EXEC |
| 3235 | i915_gem_dump_object(object_list[args->buffer_count - 1], |
| 3236 | args->batch_len, |
| 3237 | __func__, |
| 3238 | ~0); |
| 3239 | #endif |
| 3240 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3241 | /* Exec the batchbuffer */ |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3242 | ret = i915_dispatch_gem_execbuffer(dev, args, cliprects, exec_offset); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3243 | if (ret) { |
| 3244 | DRM_ERROR("dispatch failed %d\n", ret); |
| 3245 | goto err; |
| 3246 | } |
| 3247 | |
| 3248 | /* |
| 3249 | * Ensure that the commands in the batch buffer are |
| 3250 | * finished before the interrupt fires |
| 3251 | */ |
| 3252 | flush_domains = i915_retire_commands(dev); |
| 3253 | |
| 3254 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3255 | |
| 3256 | /* |
| 3257 | * Get a seqno representing the execution of the current buffer, |
| 3258 | * which we can wait on. We would like to mitigate these interrupts, |
| 3259 | * likely by only creating seqnos occasionally (so that we have |
| 3260 | * *some* interrupts representing completion of buffers that we can |
| 3261 | * wait on when trying to clear up gtt space). |
| 3262 | */ |
| 3263 | seqno = i915_add_request(dev, flush_domains); |
| 3264 | BUG_ON(seqno == 0); |
| 3265 | i915_file_priv->mm.last_gem_seqno = seqno; |
| 3266 | for (i = 0; i < args->buffer_count; i++) { |
| 3267 | struct drm_gem_object *obj = object_list[i]; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3268 | |
Eric Anholt | ce44b0e | 2008-11-06 16:00:31 -0800 | [diff] [blame] | 3269 | i915_gem_object_move_to_active(obj, seqno); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3270 | #if WATCH_LRU |
| 3271 | DRM_INFO("%s: move to exec list %p\n", __func__, obj); |
| 3272 | #endif |
| 3273 | } |
| 3274 | #if WATCH_LRU |
| 3275 | i915_dump_lru(dev, __func__); |
| 3276 | #endif |
| 3277 | |
| 3278 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3279 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3280 | err: |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 3281 | for (i = 0; i < pinned; i++) |
| 3282 | i915_gem_object_unpin(object_list[i]); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3283 | |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3284 | for (i = 0; i < args->buffer_count; i++) { |
| 3285 | if (object_list[i]) { |
| 3286 | obj_priv = object_list[i]->driver_private; |
| 3287 | obj_priv->in_execbuffer = false; |
| 3288 | } |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 3289 | drm_gem_object_unreference(object_list[i]); |
Kristian Høgsberg | b70d11d | 2009-03-03 14:45:57 -0500 | [diff] [blame] | 3290 | } |
Julia Lawall | aad87df | 2008-12-21 16:28:47 +0100 | [diff] [blame] | 3291 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3292 | mutex_unlock(&dev->struct_mutex); |
| 3293 | |
Roland Dreier | a35f2e2 | 2009-02-06 17:48:09 -0800 | [diff] [blame] | 3294 | if (!ret) { |
| 3295 | /* Copy the new buffer offsets back to the user's exec list. */ |
| 3296 | ret = copy_to_user((struct drm_i915_relocation_entry __user *) |
| 3297 | (uintptr_t) args->buffers_ptr, |
| 3298 | exec_list, |
| 3299 | sizeof(*exec_list) * args->buffer_count); |
| 3300 | if (ret) |
| 3301 | DRM_ERROR("failed to copy %d exec entries " |
| 3302 | "back to user (%d)\n", |
| 3303 | args->buffer_count, ret); |
| 3304 | } |
| 3305 | |
Eric Anholt | 40a5f0d | 2009-03-12 11:23:52 -0700 | [diff] [blame] | 3306 | /* Copy the updated relocations out regardless of current error |
| 3307 | * state. Failure to update the relocs would mean that the next |
| 3308 | * time userland calls execbuf, it would do so with presumed offset |
| 3309 | * state that didn't match the actual object state. |
| 3310 | */ |
| 3311 | ret2 = i915_gem_put_relocs_to_user(exec_list, args->buffer_count, |
| 3312 | relocs); |
| 3313 | if (ret2 != 0) { |
| 3314 | DRM_ERROR("Failed to copy relocations back out: %d\n", ret2); |
| 3315 | |
| 3316 | if (ret == 0) |
| 3317 | ret = ret2; |
| 3318 | } |
| 3319 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3320 | pre_mutex_err: |
| 3321 | drm_free(object_list, sizeof(*object_list) * args->buffer_count, |
| 3322 | DRM_MEM_DRIVER); |
| 3323 | drm_free(exec_list, sizeof(*exec_list) * args->buffer_count, |
| 3324 | DRM_MEM_DRIVER); |
Eric Anholt | 201361a | 2009-03-11 12:30:04 -0700 | [diff] [blame] | 3325 | drm_free(cliprects, sizeof(*cliprects) * args->num_cliprects, |
| 3326 | DRM_MEM_DRIVER); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3327 | |
| 3328 | return ret; |
| 3329 | } |
| 3330 | |
| 3331 | int |
| 3332 | i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment) |
| 3333 | { |
| 3334 | struct drm_device *dev = obj->dev; |
| 3335 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 3336 | int ret; |
| 3337 | |
| 3338 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3339 | if (obj_priv->gtt_space == NULL) { |
| 3340 | ret = i915_gem_object_bind_to_gtt(obj, alignment); |
| 3341 | if (ret != 0) { |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 3342 | if (ret != -EBUSY && ret != -ERESTARTSYS) |
Kyle McMartin | 0fce81e | 2009-02-28 15:01:16 -0500 | [diff] [blame] | 3343 | DRM_ERROR("Failure to bind: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3344 | return ret; |
| 3345 | } |
Chris Wilson | 22c344e | 2009-02-11 14:26:45 +0000 | [diff] [blame] | 3346 | } |
| 3347 | /* |
| 3348 | * Pre-965 chips need a fence register set up in order to |
| 3349 | * properly handle tiled surfaces. |
| 3350 | */ |
| 3351 | if (!IS_I965G(dev) && |
| 3352 | obj_priv->fence_reg == I915_FENCE_REG_NONE && |
| 3353 | obj_priv->tiling_mode != I915_TILING_NONE) { |
| 3354 | ret = i915_gem_object_get_fence_reg(obj, true); |
| 3355 | if (ret != 0) { |
| 3356 | if (ret != -EBUSY && ret != -ERESTARTSYS) |
| 3357 | DRM_ERROR("Failure to install fence: %d\n", |
| 3358 | ret); |
| 3359 | return ret; |
| 3360 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3361 | } |
| 3362 | obj_priv->pin_count++; |
| 3363 | |
| 3364 | /* If the object is not active and not pending a flush, |
| 3365 | * remove it from the inactive list |
| 3366 | */ |
| 3367 | if (obj_priv->pin_count == 1) { |
| 3368 | atomic_inc(&dev->pin_count); |
| 3369 | atomic_add(obj->size, &dev->pin_memory); |
| 3370 | if (!obj_priv->active && |
| 3371 | (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | |
| 3372 | I915_GEM_DOMAIN_GTT)) == 0 && |
| 3373 | !list_empty(&obj_priv->list)) |
| 3374 | list_del_init(&obj_priv->list); |
| 3375 | } |
| 3376 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3377 | |
| 3378 | return 0; |
| 3379 | } |
| 3380 | |
| 3381 | void |
| 3382 | i915_gem_object_unpin(struct drm_gem_object *obj) |
| 3383 | { |
| 3384 | struct drm_device *dev = obj->dev; |
| 3385 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3386 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 3387 | |
| 3388 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3389 | obj_priv->pin_count--; |
| 3390 | BUG_ON(obj_priv->pin_count < 0); |
| 3391 | BUG_ON(obj_priv->gtt_space == NULL); |
| 3392 | |
| 3393 | /* If the object is no longer pinned, and is |
| 3394 | * neither active nor being flushed, then stick it on |
| 3395 | * the inactive list |
| 3396 | */ |
| 3397 | if (obj_priv->pin_count == 0) { |
| 3398 | if (!obj_priv->active && |
| 3399 | (obj->write_domain & ~(I915_GEM_DOMAIN_CPU | |
| 3400 | I915_GEM_DOMAIN_GTT)) == 0) |
| 3401 | list_move_tail(&obj_priv->list, |
| 3402 | &dev_priv->mm.inactive_list); |
| 3403 | atomic_dec(&dev->pin_count); |
| 3404 | atomic_sub(obj->size, &dev->pin_memory); |
| 3405 | } |
| 3406 | i915_verify_inactive(dev, __FILE__, __LINE__); |
| 3407 | } |
| 3408 | |
| 3409 | int |
| 3410 | i915_gem_pin_ioctl(struct drm_device *dev, void *data, |
| 3411 | struct drm_file *file_priv) |
| 3412 | { |
| 3413 | struct drm_i915_gem_pin *args = data; |
| 3414 | struct drm_gem_object *obj; |
| 3415 | struct drm_i915_gem_object *obj_priv; |
| 3416 | int ret; |
| 3417 | |
| 3418 | mutex_lock(&dev->struct_mutex); |
| 3419 | |
| 3420 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 3421 | if (obj == NULL) { |
| 3422 | DRM_ERROR("Bad handle in i915_gem_pin_ioctl(): %d\n", |
| 3423 | args->handle); |
| 3424 | mutex_unlock(&dev->struct_mutex); |
| 3425 | return -EBADF; |
| 3426 | } |
| 3427 | obj_priv = obj->driver_private; |
| 3428 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3429 | if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) { |
| 3430 | DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n", |
| 3431 | args->handle); |
Chris Wilson | 96dec61 | 2009-02-08 19:08:04 +0000 | [diff] [blame] | 3432 | drm_gem_object_unreference(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3433 | mutex_unlock(&dev->struct_mutex); |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3434 | return -EINVAL; |
| 3435 | } |
| 3436 | |
| 3437 | obj_priv->user_pin_count++; |
| 3438 | obj_priv->pin_filp = file_priv; |
| 3439 | if (obj_priv->user_pin_count == 1) { |
| 3440 | ret = i915_gem_object_pin(obj, args->alignment); |
| 3441 | if (ret != 0) { |
| 3442 | drm_gem_object_unreference(obj); |
| 3443 | mutex_unlock(&dev->struct_mutex); |
| 3444 | return ret; |
| 3445 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3446 | } |
| 3447 | |
| 3448 | /* XXX - flush the CPU caches for pinned objects |
| 3449 | * as the X server doesn't manage domains yet |
| 3450 | */ |
Eric Anholt | e47c68e | 2008-11-14 13:35:19 -0800 | [diff] [blame] | 3451 | i915_gem_object_flush_cpu_write_domain(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3452 | args->offset = obj_priv->gtt_offset; |
| 3453 | drm_gem_object_unreference(obj); |
| 3454 | mutex_unlock(&dev->struct_mutex); |
| 3455 | |
| 3456 | return 0; |
| 3457 | } |
| 3458 | |
| 3459 | int |
| 3460 | i915_gem_unpin_ioctl(struct drm_device *dev, void *data, |
| 3461 | struct drm_file *file_priv) |
| 3462 | { |
| 3463 | struct drm_i915_gem_pin *args = data; |
| 3464 | struct drm_gem_object *obj; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3465 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3466 | |
| 3467 | mutex_lock(&dev->struct_mutex); |
| 3468 | |
| 3469 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 3470 | if (obj == NULL) { |
| 3471 | DRM_ERROR("Bad handle in i915_gem_unpin_ioctl(): %d\n", |
| 3472 | args->handle); |
| 3473 | mutex_unlock(&dev->struct_mutex); |
| 3474 | return -EBADF; |
| 3475 | } |
| 3476 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3477 | obj_priv = obj->driver_private; |
| 3478 | if (obj_priv->pin_filp != file_priv) { |
| 3479 | DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n", |
| 3480 | args->handle); |
| 3481 | drm_gem_object_unreference(obj); |
| 3482 | mutex_unlock(&dev->struct_mutex); |
| 3483 | return -EINVAL; |
| 3484 | } |
| 3485 | obj_priv->user_pin_count--; |
| 3486 | if (obj_priv->user_pin_count == 0) { |
| 3487 | obj_priv->pin_filp = NULL; |
| 3488 | i915_gem_object_unpin(obj); |
| 3489 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3490 | |
| 3491 | drm_gem_object_unreference(obj); |
| 3492 | mutex_unlock(&dev->struct_mutex); |
| 3493 | return 0; |
| 3494 | } |
| 3495 | |
| 3496 | int |
| 3497 | i915_gem_busy_ioctl(struct drm_device *dev, void *data, |
| 3498 | struct drm_file *file_priv) |
| 3499 | { |
| 3500 | struct drm_i915_gem_busy *args = data; |
| 3501 | struct drm_gem_object *obj; |
| 3502 | struct drm_i915_gem_object *obj_priv; |
| 3503 | |
| 3504 | mutex_lock(&dev->struct_mutex); |
| 3505 | obj = drm_gem_object_lookup(dev, file_priv, args->handle); |
| 3506 | if (obj == NULL) { |
| 3507 | DRM_ERROR("Bad handle in i915_gem_busy_ioctl(): %d\n", |
| 3508 | args->handle); |
| 3509 | mutex_unlock(&dev->struct_mutex); |
| 3510 | return -EBADF; |
| 3511 | } |
| 3512 | |
Eric Anholt | f21289b | 2009-02-18 09:44:56 -0800 | [diff] [blame] | 3513 | /* Update the active list for the hardware's current position. |
| 3514 | * Otherwise this only updates on a delayed timer or when irqs are |
| 3515 | * actually unmasked, and our working set ends up being larger than |
| 3516 | * required. |
| 3517 | */ |
| 3518 | i915_gem_retire_requests(dev); |
| 3519 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3520 | obj_priv = obj->driver_private; |
Eric Anholt | c4de0a5 | 2008-12-14 19:05:04 -0800 | [diff] [blame] | 3521 | /* Don't count being on the flushing list against the object being |
| 3522 | * done. Otherwise, a buffer left on the flushing list but not getting |
| 3523 | * flushed (because nobody's flushing that domain) won't ever return |
| 3524 | * unbusy and get reused by libdrm's bo cache. The other expected |
| 3525 | * consumer of this interface, OpenGL's occlusion queries, also specs |
| 3526 | * that the objects get unbusy "eventually" without any interference. |
| 3527 | */ |
| 3528 | args->busy = obj_priv->active && obj_priv->last_rendering_seqno != 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3529 | |
| 3530 | drm_gem_object_unreference(obj); |
| 3531 | mutex_unlock(&dev->struct_mutex); |
| 3532 | return 0; |
| 3533 | } |
| 3534 | |
| 3535 | int |
| 3536 | i915_gem_throttle_ioctl(struct drm_device *dev, void *data, |
| 3537 | struct drm_file *file_priv) |
| 3538 | { |
| 3539 | return i915_gem_ring_throttle(dev, file_priv); |
| 3540 | } |
| 3541 | |
| 3542 | int i915_gem_init_object(struct drm_gem_object *obj) |
| 3543 | { |
| 3544 | struct drm_i915_gem_object *obj_priv; |
| 3545 | |
| 3546 | obj_priv = drm_calloc(1, sizeof(*obj_priv), DRM_MEM_DRIVER); |
| 3547 | if (obj_priv == NULL) |
| 3548 | return -ENOMEM; |
| 3549 | |
| 3550 | /* |
| 3551 | * We've just allocated pages from the kernel, |
| 3552 | * so they've just been written by the CPU with |
| 3553 | * zeros. They'll need to be clflushed before we |
| 3554 | * use them with the GPU. |
| 3555 | */ |
| 3556 | obj->write_domain = I915_GEM_DOMAIN_CPU; |
| 3557 | obj->read_domains = I915_GEM_DOMAIN_CPU; |
| 3558 | |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 3559 | obj_priv->agp_type = AGP_USER_MEMORY; |
| 3560 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3561 | obj->driver_private = obj_priv; |
| 3562 | obj_priv->obj = obj; |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3563 | obj_priv->fence_reg = I915_FENCE_REG_NONE; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3564 | INIT_LIST_HEAD(&obj_priv->list); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3565 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3566 | return 0; |
| 3567 | } |
| 3568 | |
| 3569 | void i915_gem_free_object(struct drm_gem_object *obj) |
| 3570 | { |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3571 | struct drm_device *dev = obj->dev; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3572 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 3573 | |
| 3574 | while (obj_priv->pin_count > 0) |
| 3575 | i915_gem_object_unpin(obj); |
| 3576 | |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 3577 | if (obj_priv->phys_obj) |
| 3578 | i915_gem_detach_phys_object(dev, obj); |
| 3579 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3580 | i915_gem_object_unbind(obj); |
| 3581 | |
Jesse Barnes | ab00b3e | 2009-02-11 14:01:46 -0800 | [diff] [blame] | 3582 | i915_gem_free_mmap_offset(obj); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3583 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3584 | drm_free(obj_priv->page_cpu_valid, 1, DRM_MEM_DRIVER); |
| 3585 | drm_free(obj->driver_private, 1, DRM_MEM_DRIVER); |
| 3586 | } |
| 3587 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3588 | /** Unbinds all objects that are on the given buffer list. */ |
| 3589 | static int |
| 3590 | i915_gem_evict_from_list(struct drm_device *dev, struct list_head *head) |
| 3591 | { |
| 3592 | struct drm_gem_object *obj; |
| 3593 | struct drm_i915_gem_object *obj_priv; |
| 3594 | int ret; |
| 3595 | |
| 3596 | while (!list_empty(head)) { |
| 3597 | obj_priv = list_first_entry(head, |
| 3598 | struct drm_i915_gem_object, |
| 3599 | list); |
| 3600 | obj = obj_priv->obj; |
| 3601 | |
| 3602 | if (obj_priv->pin_count != 0) { |
| 3603 | DRM_ERROR("Pinned object in unbind list\n"); |
| 3604 | mutex_unlock(&dev->struct_mutex); |
| 3605 | return -EINVAL; |
| 3606 | } |
| 3607 | |
| 3608 | ret = i915_gem_object_unbind(obj); |
| 3609 | if (ret != 0) { |
| 3610 | DRM_ERROR("Error unbinding object in LeaveVT: %d\n", |
| 3611 | ret); |
| 3612 | mutex_unlock(&dev->struct_mutex); |
| 3613 | return ret; |
| 3614 | } |
| 3615 | } |
| 3616 | |
| 3617 | |
| 3618 | return 0; |
| 3619 | } |
| 3620 | |
Jesse Barnes | 5669fca | 2009-02-17 15:13:31 -0800 | [diff] [blame] | 3621 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3622 | i915_gem_idle(struct drm_device *dev) |
| 3623 | { |
| 3624 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3625 | uint32_t seqno, cur_seqno, last_seqno; |
| 3626 | int stuck, ret; |
| 3627 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3628 | mutex_lock(&dev->struct_mutex); |
| 3629 | |
| 3630 | if (dev_priv->mm.suspended || dev_priv->ring.ring_obj == NULL) { |
| 3631 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3632 | return 0; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3633 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3634 | |
| 3635 | /* Hack! Don't let anybody do execbuf while we don't control the chip. |
| 3636 | * We need to replace this with a semaphore, or something. |
| 3637 | */ |
| 3638 | dev_priv->mm.suspended = 1; |
| 3639 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3640 | /* Cancel the retire work handler, wait for it to finish if running |
| 3641 | */ |
| 3642 | mutex_unlock(&dev->struct_mutex); |
| 3643 | cancel_delayed_work_sync(&dev_priv->mm.retire_work); |
| 3644 | mutex_lock(&dev->struct_mutex); |
| 3645 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3646 | i915_kernel_lost_context(dev); |
| 3647 | |
| 3648 | /* Flush the GPU along with all non-CPU write domains |
| 3649 | */ |
| 3650 | i915_gem_flush(dev, ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT), |
| 3651 | ~(I915_GEM_DOMAIN_CPU|I915_GEM_DOMAIN_GTT)); |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 3652 | seqno = i915_add_request(dev, ~I915_GEM_DOMAIN_CPU); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3653 | |
| 3654 | if (seqno == 0) { |
| 3655 | mutex_unlock(&dev->struct_mutex); |
| 3656 | return -ENOMEM; |
| 3657 | } |
| 3658 | |
| 3659 | dev_priv->mm.waiting_gem_seqno = seqno; |
| 3660 | last_seqno = 0; |
| 3661 | stuck = 0; |
| 3662 | for (;;) { |
| 3663 | cur_seqno = i915_get_gem_seqno(dev); |
| 3664 | if (i915_seqno_passed(cur_seqno, seqno)) |
| 3665 | break; |
| 3666 | if (last_seqno == cur_seqno) { |
| 3667 | if (stuck++ > 100) { |
| 3668 | DRM_ERROR("hardware wedged\n"); |
| 3669 | dev_priv->mm.wedged = 1; |
| 3670 | DRM_WAKEUP(&dev_priv->irq_queue); |
| 3671 | break; |
| 3672 | } |
| 3673 | } |
| 3674 | msleep(10); |
| 3675 | last_seqno = cur_seqno; |
| 3676 | } |
| 3677 | dev_priv->mm.waiting_gem_seqno = 0; |
| 3678 | |
| 3679 | i915_gem_retire_requests(dev); |
| 3680 | |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3681 | if (!dev_priv->mm.wedged) { |
| 3682 | /* Active and flushing should now be empty as we've |
| 3683 | * waited for a sequence higher than any pending execbuffer |
| 3684 | */ |
| 3685 | WARN_ON(!list_empty(&dev_priv->mm.active_list)); |
| 3686 | WARN_ON(!list_empty(&dev_priv->mm.flushing_list)); |
| 3687 | /* Request should now be empty as we've also waited |
| 3688 | * for the last request in the list |
| 3689 | */ |
| 3690 | WARN_ON(!list_empty(&dev_priv->mm.request_list)); |
| 3691 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3692 | |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3693 | /* Empty the active and flushing lists to inactive. If there's |
| 3694 | * anything left at this point, it means that we're wedged and |
| 3695 | * nothing good's going to happen by leaving them there. So strip |
| 3696 | * the GPU domains and just stuff them onto inactive. |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3697 | */ |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3698 | while (!list_empty(&dev_priv->mm.active_list)) { |
| 3699 | struct drm_i915_gem_object *obj_priv; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3700 | |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3701 | obj_priv = list_first_entry(&dev_priv->mm.active_list, |
| 3702 | struct drm_i915_gem_object, |
| 3703 | list); |
| 3704 | obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS; |
| 3705 | i915_gem_object_move_to_inactive(obj_priv->obj); |
| 3706 | } |
| 3707 | |
| 3708 | while (!list_empty(&dev_priv->mm.flushing_list)) { |
| 3709 | struct drm_i915_gem_object *obj_priv; |
| 3710 | |
Eric Anholt | 151903d | 2008-12-01 10:23:21 +1000 | [diff] [blame] | 3711 | obj_priv = list_first_entry(&dev_priv->mm.flushing_list, |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3712 | struct drm_i915_gem_object, |
| 3713 | list); |
| 3714 | obj_priv->obj->write_domain &= ~I915_GEM_GPU_DOMAINS; |
| 3715 | i915_gem_object_move_to_inactive(obj_priv->obj); |
| 3716 | } |
| 3717 | |
| 3718 | |
| 3719 | /* Move all inactive buffers out of the GTT. */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3720 | ret = i915_gem_evict_from_list(dev, &dev_priv->mm.inactive_list); |
Eric Anholt | 28dfe52 | 2008-11-13 15:00:55 -0800 | [diff] [blame] | 3721 | WARN_ON(!list_empty(&dev_priv->mm.inactive_list)); |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3722 | if (ret) { |
| 3723 | mutex_unlock(&dev->struct_mutex); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3724 | return ret; |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3725 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3726 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3727 | i915_gem_cleanup_ringbuffer(dev); |
| 3728 | mutex_unlock(&dev->struct_mutex); |
| 3729 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3730 | return 0; |
| 3731 | } |
| 3732 | |
| 3733 | static int |
| 3734 | i915_gem_init_hws(struct drm_device *dev) |
| 3735 | { |
| 3736 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3737 | struct drm_gem_object *obj; |
| 3738 | struct drm_i915_gem_object *obj_priv; |
| 3739 | int ret; |
| 3740 | |
| 3741 | /* If we need a physical address for the status page, it's already |
| 3742 | * initialized at driver load time. |
| 3743 | */ |
| 3744 | if (!I915_NEED_GFX_HWS(dev)) |
| 3745 | return 0; |
| 3746 | |
| 3747 | obj = drm_gem_object_alloc(dev, 4096); |
| 3748 | if (obj == NULL) { |
| 3749 | DRM_ERROR("Failed to allocate status page\n"); |
| 3750 | return -ENOMEM; |
| 3751 | } |
| 3752 | obj_priv = obj->driver_private; |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 3753 | obj_priv->agp_type = AGP_USER_CACHED_MEMORY; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3754 | |
| 3755 | ret = i915_gem_object_pin(obj, 4096); |
| 3756 | if (ret != 0) { |
| 3757 | drm_gem_object_unreference(obj); |
| 3758 | return ret; |
| 3759 | } |
| 3760 | |
| 3761 | dev_priv->status_gfx_addr = obj_priv->gtt_offset; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3762 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3763 | dev_priv->hw_status_page = kmap(obj_priv->pages[0]); |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 3764 | if (dev_priv->hw_status_page == NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3765 | DRM_ERROR("Failed to map status page.\n"); |
| 3766 | memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); |
Chris Wilson | 3eb2ee7 | 2009-02-11 14:26:34 +0000 | [diff] [blame] | 3767 | i915_gem_object_unpin(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3768 | drm_gem_object_unreference(obj); |
| 3769 | return -EINVAL; |
| 3770 | } |
| 3771 | dev_priv->hws_obj = obj; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3772 | memset(dev_priv->hw_status_page, 0, PAGE_SIZE); |
| 3773 | I915_WRITE(HWS_PGA, dev_priv->status_gfx_addr); |
Keith Packard | ba1eb1d | 2008-10-14 19:55:10 -0700 | [diff] [blame] | 3774 | I915_READ(HWS_PGA); /* posting read */ |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3775 | DRM_DEBUG("hws offset: 0x%08x\n", dev_priv->status_gfx_addr); |
| 3776 | |
| 3777 | return 0; |
| 3778 | } |
| 3779 | |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3780 | static void |
| 3781 | i915_gem_cleanup_hws(struct drm_device *dev) |
| 3782 | { |
| 3783 | drm_i915_private_t *dev_priv = dev->dev_private; |
Chris Wilson | bab2d1f | 2009-02-20 17:52:20 +0000 | [diff] [blame] | 3784 | struct drm_gem_object *obj; |
| 3785 | struct drm_i915_gem_object *obj_priv; |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3786 | |
| 3787 | if (dev_priv->hws_obj == NULL) |
| 3788 | return; |
| 3789 | |
Chris Wilson | bab2d1f | 2009-02-20 17:52:20 +0000 | [diff] [blame] | 3790 | obj = dev_priv->hws_obj; |
| 3791 | obj_priv = obj->driver_private; |
| 3792 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 3793 | kunmap(obj_priv->pages[0]); |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3794 | i915_gem_object_unpin(obj); |
| 3795 | drm_gem_object_unreference(obj); |
| 3796 | dev_priv->hws_obj = NULL; |
Chris Wilson | bab2d1f | 2009-02-20 17:52:20 +0000 | [diff] [blame] | 3797 | |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3798 | memset(&dev_priv->hws_map, 0, sizeof(dev_priv->hws_map)); |
| 3799 | dev_priv->hw_status_page = NULL; |
| 3800 | |
| 3801 | /* Write high address into HWS_PGA when disabling. */ |
| 3802 | I915_WRITE(HWS_PGA, 0x1ffff000); |
| 3803 | } |
| 3804 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3805 | int |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3806 | i915_gem_init_ringbuffer(struct drm_device *dev) |
| 3807 | { |
| 3808 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3809 | struct drm_gem_object *obj; |
| 3810 | struct drm_i915_gem_object *obj_priv; |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3811 | drm_i915_ring_buffer_t *ring = &dev_priv->ring; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3812 | int ret; |
Keith Packard | 50aa253 | 2008-10-14 17:20:35 -0700 | [diff] [blame] | 3813 | u32 head; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3814 | |
| 3815 | ret = i915_gem_init_hws(dev); |
| 3816 | if (ret != 0) |
| 3817 | return ret; |
| 3818 | |
| 3819 | obj = drm_gem_object_alloc(dev, 128 * 1024); |
| 3820 | if (obj == NULL) { |
| 3821 | DRM_ERROR("Failed to allocate ringbuffer\n"); |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3822 | i915_gem_cleanup_hws(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3823 | return -ENOMEM; |
| 3824 | } |
| 3825 | obj_priv = obj->driver_private; |
| 3826 | |
| 3827 | ret = i915_gem_object_pin(obj, 4096); |
| 3828 | if (ret != 0) { |
| 3829 | drm_gem_object_unreference(obj); |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3830 | i915_gem_cleanup_hws(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3831 | return ret; |
| 3832 | } |
| 3833 | |
| 3834 | /* Set up the kernel mapping for the ring. */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3835 | ring->Size = obj->size; |
| 3836 | ring->tail_mask = obj->size - 1; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3837 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3838 | ring->map.offset = dev->agp->base + obj_priv->gtt_offset; |
| 3839 | ring->map.size = obj->size; |
| 3840 | ring->map.type = 0; |
| 3841 | ring->map.flags = 0; |
| 3842 | ring->map.mtrr = 0; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3843 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3844 | drm_core_ioremap_wc(&ring->map, dev); |
| 3845 | if (ring->map.handle == NULL) { |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3846 | DRM_ERROR("Failed to map ringbuffer.\n"); |
| 3847 | memset(&dev_priv->ring, 0, sizeof(dev_priv->ring)); |
Chris Wilson | 47ed185 | 2009-02-11 14:26:33 +0000 | [diff] [blame] | 3848 | i915_gem_object_unpin(obj); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3849 | drm_gem_object_unreference(obj); |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3850 | i915_gem_cleanup_hws(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3851 | return -EINVAL; |
| 3852 | } |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3853 | ring->ring_obj = obj; |
| 3854 | ring->virtual_start = ring->map.handle; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3855 | |
| 3856 | /* Stop the ring if it's running. */ |
| 3857 | I915_WRITE(PRB0_CTL, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3858 | I915_WRITE(PRB0_TAIL, 0); |
Keith Packard | 50aa253 | 2008-10-14 17:20:35 -0700 | [diff] [blame] | 3859 | I915_WRITE(PRB0_HEAD, 0); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3860 | |
| 3861 | /* Initialize the ring. */ |
| 3862 | I915_WRITE(PRB0_START, obj_priv->gtt_offset); |
Keith Packard | 50aa253 | 2008-10-14 17:20:35 -0700 | [diff] [blame] | 3863 | head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
| 3864 | |
| 3865 | /* G45 ring initialization fails to reset head to zero */ |
| 3866 | if (head != 0) { |
| 3867 | DRM_ERROR("Ring head not reset to zero " |
| 3868 | "ctl %08x head %08x tail %08x start %08x\n", |
| 3869 | I915_READ(PRB0_CTL), |
| 3870 | I915_READ(PRB0_HEAD), |
| 3871 | I915_READ(PRB0_TAIL), |
| 3872 | I915_READ(PRB0_START)); |
| 3873 | I915_WRITE(PRB0_HEAD, 0); |
| 3874 | |
| 3875 | DRM_ERROR("Ring head forced to zero " |
| 3876 | "ctl %08x head %08x tail %08x start %08x\n", |
| 3877 | I915_READ(PRB0_CTL), |
| 3878 | I915_READ(PRB0_HEAD), |
| 3879 | I915_READ(PRB0_TAIL), |
| 3880 | I915_READ(PRB0_START)); |
| 3881 | } |
| 3882 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3883 | I915_WRITE(PRB0_CTL, |
| 3884 | ((obj->size - 4096) & RING_NR_PAGES) | |
| 3885 | RING_NO_REPORT | |
| 3886 | RING_VALID); |
| 3887 | |
Keith Packard | 50aa253 | 2008-10-14 17:20:35 -0700 | [diff] [blame] | 3888 | head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
| 3889 | |
| 3890 | /* If the head is still not zero, the ring is dead */ |
| 3891 | if (head != 0) { |
| 3892 | DRM_ERROR("Ring initialization failed " |
| 3893 | "ctl %08x head %08x tail %08x start %08x\n", |
| 3894 | I915_READ(PRB0_CTL), |
| 3895 | I915_READ(PRB0_HEAD), |
| 3896 | I915_READ(PRB0_TAIL), |
| 3897 | I915_READ(PRB0_START)); |
| 3898 | return -EIO; |
| 3899 | } |
| 3900 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3901 | /* Update our cache of the ring state */ |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3902 | if (!drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3903 | i915_kernel_lost_context(dev); |
| 3904 | else { |
| 3905 | ring->head = I915_READ(PRB0_HEAD) & HEAD_ADDR; |
| 3906 | ring->tail = I915_READ(PRB0_TAIL) & TAIL_ADDR; |
| 3907 | ring->space = ring->head - (ring->tail + 8); |
| 3908 | if (ring->space < 0) |
| 3909 | ring->space += ring->Size; |
| 3910 | } |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3911 | |
| 3912 | return 0; |
| 3913 | } |
| 3914 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3915 | void |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3916 | i915_gem_cleanup_ringbuffer(struct drm_device *dev) |
| 3917 | { |
| 3918 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3919 | |
| 3920 | if (dev_priv->ring.ring_obj == NULL) |
| 3921 | return; |
| 3922 | |
| 3923 | drm_core_ioremapfree(&dev_priv->ring.map, dev); |
| 3924 | |
| 3925 | i915_gem_object_unpin(dev_priv->ring.ring_obj); |
| 3926 | drm_gem_object_unreference(dev_priv->ring.ring_obj); |
| 3927 | dev_priv->ring.ring_obj = NULL; |
| 3928 | memset(&dev_priv->ring, 0, sizeof(dev_priv->ring)); |
| 3929 | |
Chris Wilson | 85a7bb9 | 2009-02-11 14:52:44 +0000 | [diff] [blame] | 3930 | i915_gem_cleanup_hws(dev); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3931 | } |
| 3932 | |
| 3933 | int |
| 3934 | i915_gem_entervt_ioctl(struct drm_device *dev, void *data, |
| 3935 | struct drm_file *file_priv) |
| 3936 | { |
| 3937 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3938 | int ret; |
| 3939 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3940 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3941 | return 0; |
| 3942 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3943 | if (dev_priv->mm.wedged) { |
| 3944 | DRM_ERROR("Reenabling wedged hardware, good luck\n"); |
| 3945 | dev_priv->mm.wedged = 0; |
| 3946 | } |
| 3947 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3948 | mutex_lock(&dev->struct_mutex); |
Eric Anholt | 9bb2d6f | 2008-12-23 18:42:32 -0800 | [diff] [blame] | 3949 | dev_priv->mm.suspended = 0; |
| 3950 | |
| 3951 | ret = i915_gem_init_ringbuffer(dev); |
| 3952 | if (ret != 0) |
| 3953 | return ret; |
| 3954 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3955 | BUG_ON(!list_empty(&dev_priv->mm.active_list)); |
| 3956 | BUG_ON(!list_empty(&dev_priv->mm.flushing_list)); |
| 3957 | BUG_ON(!list_empty(&dev_priv->mm.inactive_list)); |
| 3958 | BUG_ON(!list_empty(&dev_priv->mm.request_list)); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3959 | mutex_unlock(&dev->struct_mutex); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 3960 | |
| 3961 | drm_irq_install(dev); |
| 3962 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3963 | return 0; |
| 3964 | } |
| 3965 | |
| 3966 | int |
| 3967 | i915_gem_leavevt_ioctl(struct drm_device *dev, void *data, |
| 3968 | struct drm_file *file_priv) |
| 3969 | { |
| 3970 | int ret; |
| 3971 | |
Jesse Barnes | 79e5394 | 2008-11-07 14:24:08 -0800 | [diff] [blame] | 3972 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3973 | return 0; |
| 3974 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3975 | ret = i915_gem_idle(dev); |
Kristian Høgsberg | dbb19d3 | 2008-08-20 11:04:27 -0400 | [diff] [blame] | 3976 | drm_irq_uninstall(dev); |
| 3977 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3978 | return ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3979 | } |
| 3980 | |
| 3981 | void |
| 3982 | i915_gem_lastclose(struct drm_device *dev) |
| 3983 | { |
| 3984 | int ret; |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3985 | |
Eric Anholt | e806b49 | 2009-01-22 09:56:58 -0800 | [diff] [blame] | 3986 | if (drm_core_check_feature(dev, DRIVER_MODESET)) |
| 3987 | return; |
| 3988 | |
Keith Packard | 6dbe277 | 2008-10-14 21:41:13 -0700 | [diff] [blame] | 3989 | ret = i915_gem_idle(dev); |
| 3990 | if (ret) |
| 3991 | DRM_ERROR("failed to idle hardware: %d\n", ret); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | void |
| 3995 | i915_gem_load(struct drm_device *dev) |
| 3996 | { |
| 3997 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 3998 | |
| 3999 | INIT_LIST_HEAD(&dev_priv->mm.active_list); |
| 4000 | INIT_LIST_HEAD(&dev_priv->mm.flushing_list); |
| 4001 | INIT_LIST_HEAD(&dev_priv->mm.inactive_list); |
| 4002 | INIT_LIST_HEAD(&dev_priv->mm.request_list); |
| 4003 | INIT_DELAYED_WORK(&dev_priv->mm.retire_work, |
| 4004 | i915_gem_retire_work_handler); |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4005 | dev_priv->mm.next_gem_seqno = 1; |
| 4006 | |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4007 | /* Old X drivers will take 0-2 for front, back, depth buffers */ |
| 4008 | dev_priv->fence_reg_start = 3; |
| 4009 | |
Jesse Barnes | 0f973f2 | 2009-01-26 17:10:45 -0800 | [diff] [blame] | 4010 | if (IS_I965G(dev) || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) |
Jesse Barnes | de151cf | 2008-11-12 10:03:55 -0800 | [diff] [blame] | 4011 | dev_priv->num_fence_regs = 16; |
| 4012 | else |
| 4013 | dev_priv->num_fence_regs = 8; |
| 4014 | |
Eric Anholt | 673a394 | 2008-07-30 12:06:12 -0700 | [diff] [blame] | 4015 | i915_gem_detect_bit_6_swizzle(dev); |
| 4016 | } |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4017 | |
| 4018 | /* |
| 4019 | * Create a physically contiguous memory object for this object |
| 4020 | * e.g. for cursor + overlay regs |
| 4021 | */ |
| 4022 | int i915_gem_init_phys_object(struct drm_device *dev, |
| 4023 | int id, int size) |
| 4024 | { |
| 4025 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4026 | struct drm_i915_gem_phys_object *phys_obj; |
| 4027 | int ret; |
| 4028 | |
| 4029 | if (dev_priv->mm.phys_objs[id - 1] || !size) |
| 4030 | return 0; |
| 4031 | |
| 4032 | phys_obj = drm_calloc(1, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER); |
| 4033 | if (!phys_obj) |
| 4034 | return -ENOMEM; |
| 4035 | |
| 4036 | phys_obj->id = id; |
| 4037 | |
| 4038 | phys_obj->handle = drm_pci_alloc(dev, size, 0, 0xffffffff); |
| 4039 | if (!phys_obj->handle) { |
| 4040 | ret = -ENOMEM; |
| 4041 | goto kfree_obj; |
| 4042 | } |
| 4043 | #ifdef CONFIG_X86 |
| 4044 | set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4045 | #endif |
| 4046 | |
| 4047 | dev_priv->mm.phys_objs[id - 1] = phys_obj; |
| 4048 | |
| 4049 | return 0; |
| 4050 | kfree_obj: |
| 4051 | drm_free(phys_obj, sizeof(struct drm_i915_gem_phys_object), DRM_MEM_DRIVER); |
| 4052 | return ret; |
| 4053 | } |
| 4054 | |
| 4055 | void i915_gem_free_phys_object(struct drm_device *dev, int id) |
| 4056 | { |
| 4057 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4058 | struct drm_i915_gem_phys_object *phys_obj; |
| 4059 | |
| 4060 | if (!dev_priv->mm.phys_objs[id - 1]) |
| 4061 | return; |
| 4062 | |
| 4063 | phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4064 | if (phys_obj->cur_obj) { |
| 4065 | i915_gem_detach_phys_object(dev, phys_obj->cur_obj); |
| 4066 | } |
| 4067 | |
| 4068 | #ifdef CONFIG_X86 |
| 4069 | set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE); |
| 4070 | #endif |
| 4071 | drm_pci_free(dev, phys_obj->handle); |
| 4072 | kfree(phys_obj); |
| 4073 | dev_priv->mm.phys_objs[id - 1] = NULL; |
| 4074 | } |
| 4075 | |
| 4076 | void i915_gem_free_all_phys_object(struct drm_device *dev) |
| 4077 | { |
| 4078 | int i; |
| 4079 | |
Dave Airlie | 260883c | 2009-01-22 17:58:49 +1000 | [diff] [blame] | 4080 | 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] | 4081 | i915_gem_free_phys_object(dev, i); |
| 4082 | } |
| 4083 | |
| 4084 | void i915_gem_detach_phys_object(struct drm_device *dev, |
| 4085 | struct drm_gem_object *obj) |
| 4086 | { |
| 4087 | struct drm_i915_gem_object *obj_priv; |
| 4088 | int i; |
| 4089 | int ret; |
| 4090 | int page_count; |
| 4091 | |
| 4092 | obj_priv = obj->driver_private; |
| 4093 | if (!obj_priv->phys_obj) |
| 4094 | return; |
| 4095 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4096 | ret = i915_gem_object_get_pages(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4097 | if (ret) |
| 4098 | goto out; |
| 4099 | |
| 4100 | page_count = obj->size / PAGE_SIZE; |
| 4101 | |
| 4102 | for (i = 0; i < page_count; i++) { |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4103 | char *dst = kmap_atomic(obj_priv->pages[i], KM_USER0); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4104 | char *src = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
| 4105 | |
| 4106 | memcpy(dst, src, PAGE_SIZE); |
| 4107 | kunmap_atomic(dst, KM_USER0); |
| 4108 | } |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4109 | drm_clflush_pages(obj_priv->pages, page_count); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4110 | drm_agp_chipset_flush(dev); |
| 4111 | out: |
| 4112 | obj_priv->phys_obj->cur_obj = NULL; |
| 4113 | obj_priv->phys_obj = NULL; |
| 4114 | } |
| 4115 | |
| 4116 | int |
| 4117 | i915_gem_attach_phys_object(struct drm_device *dev, |
| 4118 | struct drm_gem_object *obj, int id) |
| 4119 | { |
| 4120 | drm_i915_private_t *dev_priv = dev->dev_private; |
| 4121 | struct drm_i915_gem_object *obj_priv; |
| 4122 | int ret = 0; |
| 4123 | int page_count; |
| 4124 | int i; |
| 4125 | |
| 4126 | if (id > I915_MAX_PHYS_OBJECT) |
| 4127 | return -EINVAL; |
| 4128 | |
| 4129 | obj_priv = obj->driver_private; |
| 4130 | |
| 4131 | if (obj_priv->phys_obj) { |
| 4132 | if (obj_priv->phys_obj->id == id) |
| 4133 | return 0; |
| 4134 | i915_gem_detach_phys_object(dev, obj); |
| 4135 | } |
| 4136 | |
| 4137 | |
| 4138 | /* create a new object */ |
| 4139 | if (!dev_priv->mm.phys_objs[id - 1]) { |
| 4140 | ret = i915_gem_init_phys_object(dev, id, |
| 4141 | obj->size); |
| 4142 | if (ret) { |
Linus Torvalds | aeb565d | 2009-01-26 10:01:53 -0800 | [diff] [blame] | 4143 | 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] | 4144 | goto out; |
| 4145 | } |
| 4146 | } |
| 4147 | |
| 4148 | /* bind to the object */ |
| 4149 | obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1]; |
| 4150 | obj_priv->phys_obj->cur_obj = obj; |
| 4151 | |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4152 | ret = i915_gem_object_get_pages(obj); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4153 | if (ret) { |
| 4154 | DRM_ERROR("failed to get page list\n"); |
| 4155 | goto out; |
| 4156 | } |
| 4157 | |
| 4158 | page_count = obj->size / PAGE_SIZE; |
| 4159 | |
| 4160 | for (i = 0; i < page_count; i++) { |
Eric Anholt | 856fa19 | 2009-03-19 14:10:50 -0700 | [diff] [blame] | 4161 | char *src = kmap_atomic(obj_priv->pages[i], KM_USER0); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4162 | char *dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE); |
| 4163 | |
| 4164 | memcpy(dst, src, PAGE_SIZE); |
| 4165 | kunmap_atomic(src, KM_USER0); |
| 4166 | } |
| 4167 | |
| 4168 | return 0; |
| 4169 | out: |
| 4170 | return ret; |
| 4171 | } |
| 4172 | |
| 4173 | static int |
| 4174 | i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj, |
| 4175 | struct drm_i915_gem_pwrite *args, |
| 4176 | struct drm_file *file_priv) |
| 4177 | { |
| 4178 | struct drm_i915_gem_object *obj_priv = obj->driver_private; |
| 4179 | void *obj_addr; |
| 4180 | int ret; |
| 4181 | char __user *user_data; |
| 4182 | |
| 4183 | user_data = (char __user *) (uintptr_t) args->data_ptr; |
| 4184 | obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset; |
| 4185 | |
Dave Airlie | e08fb4f | 2009-02-25 14:52:30 +1000 | [diff] [blame] | 4186 | DRM_DEBUG("obj_addr %p, %lld\n", obj_addr, args->size); |
Dave Airlie | 71acb5e | 2008-12-30 20:31:46 +1000 | [diff] [blame] | 4187 | ret = copy_from_user(obj_addr, user_data, args->size); |
| 4188 | if (ret) |
| 4189 | return -EFAULT; |
| 4190 | |
| 4191 | drm_agp_chipset_flush(dev); |
| 4192 | return 0; |
| 4193 | } |