2 * Copyright © 2008 Intel Corporation
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:
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
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
24 * Eric Anholt <eric@anholt.net>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37 #include <linux/intel-gtt.h>
39 static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv);
40 static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv);
42 static int i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
44 static void i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj);
45 static void i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj);
46 static int i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj,
48 static int i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
51 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj);
52 static int i915_gem_object_wait_rendering(struct drm_gem_object *obj,
54 static int i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
58 static void i915_gem_clear_fence_reg(struct drm_gem_object *obj);
59 static int i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
60 struct drm_i915_gem_pwrite *args,
61 struct drm_file *file_priv);
62 static void i915_gem_free_object_tail(struct drm_gem_object *obj);
64 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
69 /* some bookkeeping */
70 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
73 dev_priv->mm.object_count++;
74 dev_priv->mm.object_memory += size;
77 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
80 dev_priv->mm.object_count--;
81 dev_priv->mm.object_memory -= size;
84 static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
85 struct drm_i915_gem_object *obj)
87 dev_priv->mm.gtt_count++;
88 dev_priv->mm.gtt_memory += obj->gtt_space->size;
89 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
90 dev_priv->mm.mappable_gtt_used +=
91 min_t(size_t, obj->gtt_space->size,
92 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
96 static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
97 struct drm_i915_gem_object *obj)
99 dev_priv->mm.gtt_count--;
100 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
101 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
102 dev_priv->mm.mappable_gtt_used -=
103 min_t(size_t, obj->gtt_space->size,
104 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
109 * Update the mappable working set counters. Call _only_ when there is a change
110 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
111 * @mappable: new state the changed mappable flag (either pin_ or fault_).
114 i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
115 struct drm_i915_gem_object *obj,
119 if (obj->pin_mappable && obj->fault_mappable)
120 /* Combined state was already mappable. */
122 dev_priv->mm.gtt_mappable_count++;
123 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
125 if (obj->pin_mappable || obj->fault_mappable)
126 /* Combined state still mappable. */
128 dev_priv->mm.gtt_mappable_count--;
129 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
133 static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
134 struct drm_i915_gem_object *obj,
137 dev_priv->mm.pin_count++;
138 dev_priv->mm.pin_memory += obj->gtt_space->size;
140 obj->pin_mappable = true;
141 i915_gem_info_update_mappable(dev_priv, obj, true);
145 static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
146 struct drm_i915_gem_object *obj)
148 dev_priv->mm.pin_count--;
149 dev_priv->mm.pin_memory -= obj->gtt_space->size;
150 if (obj->pin_mappable) {
151 obj->pin_mappable = false;
152 i915_gem_info_update_mappable(dev_priv, obj, false);
157 i915_gem_check_is_wedged(struct drm_device *dev)
159 struct drm_i915_private *dev_priv = dev->dev_private;
160 struct completion *x = &dev_priv->error_completion;
164 if (!atomic_read(&dev_priv->mm.wedged))
167 ret = wait_for_completion_interruptible(x);
171 /* Success, we reset the GPU! */
172 if (!atomic_read(&dev_priv->mm.wedged))
175 /* GPU is hung, bump the completion count to account for
176 * the token we just consumed so that we never hit zero and
177 * end up waiting upon a subsequent completion event that
180 spin_lock_irqsave(&x->wait.lock, flags);
182 spin_unlock_irqrestore(&x->wait.lock, flags);
186 static int i915_mutex_lock_interruptible(struct drm_device *dev)
188 struct drm_i915_private *dev_priv = dev->dev_private;
191 ret = i915_gem_check_is_wedged(dev);
195 ret = mutex_lock_interruptible(&dev->struct_mutex);
199 if (atomic_read(&dev_priv->mm.wedged)) {
200 mutex_unlock(&dev->struct_mutex);
204 WARN_ON(i915_verify_lists(dev));
209 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj_priv)
211 return obj_priv->gtt_space &&
213 obj_priv->pin_count == 0;
216 int i915_gem_do_init(struct drm_device *dev,
218 unsigned long mappable_end,
221 drm_i915_private_t *dev_priv = dev->dev_private;
224 (start & (PAGE_SIZE - 1)) != 0 ||
225 (end & (PAGE_SIZE - 1)) != 0) {
229 drm_mm_init(&dev_priv->mm.gtt_space, start,
232 dev_priv->mm.gtt_total = end - start;
233 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
234 dev_priv->mm.gtt_mappable_end = mappable_end;
240 i915_gem_init_ioctl(struct drm_device *dev, void *data,
241 struct drm_file *file_priv)
243 struct drm_i915_gem_init *args = data;
246 mutex_lock(&dev->struct_mutex);
247 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
248 mutex_unlock(&dev->struct_mutex);
254 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
255 struct drm_file *file_priv)
257 struct drm_i915_private *dev_priv = dev->dev_private;
258 struct drm_i915_gem_get_aperture *args = data;
260 if (!(dev->driver->driver_features & DRIVER_GEM))
263 mutex_lock(&dev->struct_mutex);
264 args->aper_size = dev_priv->mm.gtt_total;
265 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
266 mutex_unlock(&dev->struct_mutex);
273 * Creates a new mm object and returns a handle to it.
276 i915_gem_create_ioctl(struct drm_device *dev, void *data,
277 struct drm_file *file_priv)
279 struct drm_i915_gem_create *args = data;
280 struct drm_gem_object *obj;
284 args->size = roundup(args->size, PAGE_SIZE);
286 /* Allocate the new object */
287 obj = i915_gem_alloc_object(dev, args->size);
291 ret = drm_gem_handle_create(file_priv, obj, &handle);
293 drm_gem_object_release(obj);
294 i915_gem_info_remove_obj(dev->dev_private, obj->size);
299 /* drop reference from allocate - handle holds it now */
300 drm_gem_object_unreference(obj);
301 trace_i915_gem_object_create(obj);
303 args->handle = handle;
307 static int i915_gem_object_needs_bit17_swizzle(struct drm_gem_object *obj)
309 drm_i915_private_t *dev_priv = obj->dev->dev_private;
310 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
312 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
313 obj_priv->tiling_mode != I915_TILING_NONE;
317 slow_shmem_copy(struct page *dst_page,
319 struct page *src_page,
323 char *dst_vaddr, *src_vaddr;
325 dst_vaddr = kmap(dst_page);
326 src_vaddr = kmap(src_page);
328 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
335 slow_shmem_bit17_copy(struct page *gpu_page,
337 struct page *cpu_page,
342 char *gpu_vaddr, *cpu_vaddr;
344 /* Use the unswizzled path if this page isn't affected. */
345 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
347 return slow_shmem_copy(cpu_page, cpu_offset,
348 gpu_page, gpu_offset, length);
350 return slow_shmem_copy(gpu_page, gpu_offset,
351 cpu_page, cpu_offset, length);
354 gpu_vaddr = kmap(gpu_page);
355 cpu_vaddr = kmap(cpu_page);
357 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
358 * XORing with the other bits (A9 for Y, A9 and A10 for X)
361 int cacheline_end = ALIGN(gpu_offset + 1, 64);
362 int this_length = min(cacheline_end - gpu_offset, length);
363 int swizzled_gpu_offset = gpu_offset ^ 64;
366 memcpy(cpu_vaddr + cpu_offset,
367 gpu_vaddr + swizzled_gpu_offset,
370 memcpy(gpu_vaddr + swizzled_gpu_offset,
371 cpu_vaddr + cpu_offset,
374 cpu_offset += this_length;
375 gpu_offset += this_length;
376 length -= this_length;
384 * This is the fast shmem pread path, which attempts to copy_from_user directly
385 * from the backing pages of the object to the user's address space. On a
386 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
389 i915_gem_shmem_pread_fast(struct drm_device *dev, struct drm_gem_object *obj,
390 struct drm_i915_gem_pread *args,
391 struct drm_file *file_priv)
393 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
394 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
397 char __user *user_data;
398 int page_offset, page_length;
400 user_data = (char __user *) (uintptr_t) args->data_ptr;
403 obj_priv = to_intel_bo(obj);
404 offset = args->offset;
411 /* Operation in this page
413 * page_offset = offset within page
414 * page_length = bytes to copy for this page
416 page_offset = offset & (PAGE_SIZE-1);
417 page_length = remain;
418 if ((page_offset + remain) > PAGE_SIZE)
419 page_length = PAGE_SIZE - page_offset;
421 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
422 GFP_HIGHUSER | __GFP_RECLAIMABLE);
424 return PTR_ERR(page);
426 vaddr = kmap_atomic(page);
427 ret = __copy_to_user_inatomic(user_data,
430 kunmap_atomic(vaddr);
432 mark_page_accessed(page);
433 page_cache_release(page);
437 remain -= page_length;
438 user_data += page_length;
439 offset += page_length;
446 * This is the fallback shmem pread path, which allocates temporary storage
447 * in kernel space to copy_to_user into outside of the struct_mutex, so we
448 * can copy out of the object's backing pages while holding the struct mutex
449 * and not take page faults.
452 i915_gem_shmem_pread_slow(struct drm_device *dev, struct drm_gem_object *obj,
453 struct drm_i915_gem_pread *args,
454 struct drm_file *file_priv)
456 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
457 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
458 struct mm_struct *mm = current->mm;
459 struct page **user_pages;
461 loff_t offset, pinned_pages, i;
462 loff_t first_data_page, last_data_page, num_pages;
463 int shmem_page_offset;
464 int data_page_index, data_page_offset;
467 uint64_t data_ptr = args->data_ptr;
468 int do_bit17_swizzling;
472 /* Pin the user pages containing the data. We can't fault while
473 * holding the struct mutex, yet we want to hold it while
474 * dereferencing the user data.
476 first_data_page = data_ptr / PAGE_SIZE;
477 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
478 num_pages = last_data_page - first_data_page + 1;
480 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
481 if (user_pages == NULL)
484 mutex_unlock(&dev->struct_mutex);
485 down_read(&mm->mmap_sem);
486 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
487 num_pages, 1, 0, user_pages, NULL);
488 up_read(&mm->mmap_sem);
489 mutex_lock(&dev->struct_mutex);
490 if (pinned_pages < num_pages) {
495 ret = i915_gem_object_set_cpu_read_domain_range(obj,
501 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
503 obj_priv = to_intel_bo(obj);
504 offset = args->offset;
509 /* Operation in this page
511 * shmem_page_offset = offset within page in shmem file
512 * data_page_index = page number in get_user_pages return
513 * data_page_offset = offset with data_page_index page.
514 * page_length = bytes to copy for this page
516 shmem_page_offset = offset & ~PAGE_MASK;
517 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
518 data_page_offset = data_ptr & ~PAGE_MASK;
520 page_length = remain;
521 if ((shmem_page_offset + page_length) > PAGE_SIZE)
522 page_length = PAGE_SIZE - shmem_page_offset;
523 if ((data_page_offset + page_length) > PAGE_SIZE)
524 page_length = PAGE_SIZE - data_page_offset;
526 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
527 GFP_HIGHUSER | __GFP_RECLAIMABLE);
529 return PTR_ERR(page);
531 if (do_bit17_swizzling) {
532 slow_shmem_bit17_copy(page,
534 user_pages[data_page_index],
539 slow_shmem_copy(user_pages[data_page_index],
546 mark_page_accessed(page);
547 page_cache_release(page);
549 remain -= page_length;
550 data_ptr += page_length;
551 offset += page_length;
555 for (i = 0; i < pinned_pages; i++) {
556 SetPageDirty(user_pages[i]);
557 mark_page_accessed(user_pages[i]);
558 page_cache_release(user_pages[i]);
560 drm_free_large(user_pages);
566 * Reads data from the object referenced by handle.
568 * On error, the contents of *data are undefined.
571 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
572 struct drm_file *file_priv)
574 struct drm_i915_gem_pread *args = data;
575 struct drm_gem_object *obj;
576 struct drm_i915_gem_object *obj_priv;
579 ret = i915_mutex_lock_interruptible(dev);
583 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
588 obj_priv = to_intel_bo(obj);
590 /* Bounds check source. */
591 if (args->offset > obj->size || args->size > obj->size - args->offset) {
599 if (!access_ok(VERIFY_WRITE,
600 (char __user *)(uintptr_t)args->data_ptr,
606 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
613 ret = i915_gem_object_set_cpu_read_domain_range(obj,
620 if (!i915_gem_object_needs_bit17_swizzle(obj))
621 ret = i915_gem_shmem_pread_fast(dev, obj, args, file_priv);
623 ret = i915_gem_shmem_pread_slow(dev, obj, args, file_priv);
626 drm_gem_object_unreference(obj);
628 mutex_unlock(&dev->struct_mutex);
632 /* This is the fast write path which cannot handle
633 * page faults in the source data
637 fast_user_write(struct io_mapping *mapping,
638 loff_t page_base, int page_offset,
639 char __user *user_data,
643 unsigned long unwritten;
645 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
646 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
648 io_mapping_unmap_atomic(vaddr_atomic);
652 /* Here's the write path which can sleep for
657 slow_kernel_write(struct io_mapping *mapping,
658 loff_t gtt_base, int gtt_offset,
659 struct page *user_page, int user_offset,
662 char __iomem *dst_vaddr;
665 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
666 src_vaddr = kmap(user_page);
668 memcpy_toio(dst_vaddr + gtt_offset,
669 src_vaddr + user_offset,
673 io_mapping_unmap(dst_vaddr);
677 * This is the fast pwrite path, where we copy the data directly from the
678 * user into the GTT, uncached.
681 i915_gem_gtt_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
682 struct drm_i915_gem_pwrite *args,
683 struct drm_file *file_priv)
685 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
686 drm_i915_private_t *dev_priv = dev->dev_private;
688 loff_t offset, page_base;
689 char __user *user_data;
690 int page_offset, page_length;
692 user_data = (char __user *) (uintptr_t) args->data_ptr;
695 obj_priv = to_intel_bo(obj);
696 offset = obj_priv->gtt_offset + args->offset;
699 /* Operation in this page
701 * page_base = page offset within aperture
702 * page_offset = offset within page
703 * page_length = bytes to copy for this page
705 page_base = (offset & ~(PAGE_SIZE-1));
706 page_offset = offset & (PAGE_SIZE-1);
707 page_length = remain;
708 if ((page_offset + remain) > PAGE_SIZE)
709 page_length = PAGE_SIZE - page_offset;
711 /* If we get a fault while copying data, then (presumably) our
712 * source page isn't available. Return the error and we'll
713 * retry in the slow path.
715 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
716 page_offset, user_data, page_length))
720 remain -= page_length;
721 user_data += page_length;
722 offset += page_length;
729 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
730 * the memory and maps it using kmap_atomic for copying.
732 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
733 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
736 i915_gem_gtt_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
737 struct drm_i915_gem_pwrite *args,
738 struct drm_file *file_priv)
740 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
741 drm_i915_private_t *dev_priv = dev->dev_private;
743 loff_t gtt_page_base, offset;
744 loff_t first_data_page, last_data_page, num_pages;
745 loff_t pinned_pages, i;
746 struct page **user_pages;
747 struct mm_struct *mm = current->mm;
748 int gtt_page_offset, data_page_offset, data_page_index, page_length;
750 uint64_t data_ptr = args->data_ptr;
754 /* Pin the user pages containing the data. We can't fault while
755 * holding the struct mutex, and all of the pwrite implementations
756 * want to hold it while dereferencing the user data.
758 first_data_page = data_ptr / PAGE_SIZE;
759 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
760 num_pages = last_data_page - first_data_page + 1;
762 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
763 if (user_pages == NULL)
766 mutex_unlock(&dev->struct_mutex);
767 down_read(&mm->mmap_sem);
768 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
769 num_pages, 0, 0, user_pages, NULL);
770 up_read(&mm->mmap_sem);
771 mutex_lock(&dev->struct_mutex);
772 if (pinned_pages < num_pages) {
774 goto out_unpin_pages;
777 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
779 goto out_unpin_pages;
781 obj_priv = to_intel_bo(obj);
782 offset = obj_priv->gtt_offset + args->offset;
785 /* Operation in this page
787 * gtt_page_base = page offset within aperture
788 * gtt_page_offset = offset within page in aperture
789 * data_page_index = page number in get_user_pages return
790 * data_page_offset = offset with data_page_index page.
791 * page_length = bytes to copy for this page
793 gtt_page_base = offset & PAGE_MASK;
794 gtt_page_offset = offset & ~PAGE_MASK;
795 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
796 data_page_offset = data_ptr & ~PAGE_MASK;
798 page_length = remain;
799 if ((gtt_page_offset + page_length) > PAGE_SIZE)
800 page_length = PAGE_SIZE - gtt_page_offset;
801 if ((data_page_offset + page_length) > PAGE_SIZE)
802 page_length = PAGE_SIZE - data_page_offset;
804 slow_kernel_write(dev_priv->mm.gtt_mapping,
805 gtt_page_base, gtt_page_offset,
806 user_pages[data_page_index],
810 remain -= page_length;
811 offset += page_length;
812 data_ptr += page_length;
816 for (i = 0; i < pinned_pages; i++)
817 page_cache_release(user_pages[i]);
818 drm_free_large(user_pages);
824 * This is the fast shmem pwrite path, which attempts to directly
825 * copy_from_user into the kmapped pages backing the object.
828 i915_gem_shmem_pwrite_fast(struct drm_device *dev, struct drm_gem_object *obj,
829 struct drm_i915_gem_pwrite *args,
830 struct drm_file *file_priv)
832 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
833 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
836 char __user *user_data;
837 int page_offset, page_length;
839 user_data = (char __user *) (uintptr_t) args->data_ptr;
842 obj_priv = to_intel_bo(obj);
843 offset = args->offset;
851 /* Operation in this page
853 * page_offset = offset within page
854 * page_length = bytes to copy for this page
856 page_offset = offset & (PAGE_SIZE-1);
857 page_length = remain;
858 if ((page_offset + remain) > PAGE_SIZE)
859 page_length = PAGE_SIZE - page_offset;
861 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
862 GFP_HIGHUSER | __GFP_RECLAIMABLE);
864 return PTR_ERR(page);
866 vaddr = kmap_atomic(page, KM_USER0);
867 ret = __copy_from_user_inatomic(vaddr + page_offset,
870 kunmap_atomic(vaddr, KM_USER0);
872 set_page_dirty(page);
873 mark_page_accessed(page);
874 page_cache_release(page);
876 /* If we get a fault while copying data, then (presumably) our
877 * source page isn't available. Return the error and we'll
878 * retry in the slow path.
883 remain -= page_length;
884 user_data += page_length;
885 offset += page_length;
892 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
893 * the memory and maps it using kmap_atomic for copying.
895 * This avoids taking mmap_sem for faulting on the user's address while the
896 * struct_mutex is held.
899 i915_gem_shmem_pwrite_slow(struct drm_device *dev, struct drm_gem_object *obj,
900 struct drm_i915_gem_pwrite *args,
901 struct drm_file *file_priv)
903 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
904 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
905 struct mm_struct *mm = current->mm;
906 struct page **user_pages;
908 loff_t offset, pinned_pages, i;
909 loff_t first_data_page, last_data_page, num_pages;
910 int shmem_page_offset;
911 int data_page_index, data_page_offset;
914 uint64_t data_ptr = args->data_ptr;
915 int do_bit17_swizzling;
919 /* Pin the user pages containing the data. We can't fault while
920 * holding the struct mutex, and all of the pwrite implementations
921 * want to hold it while dereferencing the user data.
923 first_data_page = data_ptr / PAGE_SIZE;
924 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
925 num_pages = last_data_page - first_data_page + 1;
927 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
928 if (user_pages == NULL)
931 mutex_unlock(&dev->struct_mutex);
932 down_read(&mm->mmap_sem);
933 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
934 num_pages, 0, 0, user_pages, NULL);
935 up_read(&mm->mmap_sem);
936 mutex_lock(&dev->struct_mutex);
937 if (pinned_pages < num_pages) {
942 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
946 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
948 obj_priv = to_intel_bo(obj);
949 offset = args->offset;
955 /* Operation in this page
957 * shmem_page_offset = offset within page in shmem file
958 * data_page_index = page number in get_user_pages return
959 * data_page_offset = offset with data_page_index page.
960 * page_length = bytes to copy for this page
962 shmem_page_offset = offset & ~PAGE_MASK;
963 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
964 data_page_offset = data_ptr & ~PAGE_MASK;
966 page_length = remain;
967 if ((shmem_page_offset + page_length) > PAGE_SIZE)
968 page_length = PAGE_SIZE - shmem_page_offset;
969 if ((data_page_offset + page_length) > PAGE_SIZE)
970 page_length = PAGE_SIZE - data_page_offset;
972 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
973 GFP_HIGHUSER | __GFP_RECLAIMABLE);
979 if (do_bit17_swizzling) {
980 slow_shmem_bit17_copy(page,
982 user_pages[data_page_index],
987 slow_shmem_copy(page,
989 user_pages[data_page_index],
994 set_page_dirty(page);
995 mark_page_accessed(page);
996 page_cache_release(page);
998 remain -= page_length;
999 data_ptr += page_length;
1000 offset += page_length;
1004 for (i = 0; i < pinned_pages; i++)
1005 page_cache_release(user_pages[i]);
1006 drm_free_large(user_pages);
1012 * Writes data to the object referenced by handle.
1014 * On error, the contents of the buffer that were to be modified are undefined.
1017 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1018 struct drm_file *file)
1020 struct drm_i915_gem_pwrite *args = data;
1021 struct drm_gem_object *obj;
1022 struct drm_i915_gem_object *obj_priv;
1025 ret = i915_mutex_lock_interruptible(dev);
1029 obj = drm_gem_object_lookup(dev, file, args->handle);
1034 obj_priv = to_intel_bo(obj);
1037 /* Bounds check destination. */
1038 if (args->offset > obj->size || args->size > obj->size - args->offset) {
1043 if (args->size == 0)
1046 if (!access_ok(VERIFY_READ,
1047 (char __user *)(uintptr_t)args->data_ptr,
1053 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1060 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1061 * it would end up going through the fenced access, and we'll get
1062 * different detiling behavior between reading and writing.
1063 * pread/pwrite currently are reading and writing from the CPU
1064 * perspective, requiring manual detiling by the client.
1066 if (obj_priv->phys_obj)
1067 ret = i915_gem_phys_pwrite(dev, obj, args, file);
1068 else if (obj_priv->tiling_mode == I915_TILING_NONE &&
1069 obj_priv->gtt_space &&
1070 obj->write_domain != I915_GEM_DOMAIN_CPU) {
1071 ret = i915_gem_object_pin(obj, 0, true, false);
1075 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1079 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1081 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1084 i915_gem_object_unpin(obj);
1086 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1091 if (!i915_gem_object_needs_bit17_swizzle(obj))
1092 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1094 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1098 drm_gem_object_unreference(obj);
1100 mutex_unlock(&dev->struct_mutex);
1105 * Called when user space prepares to use an object with the CPU, either
1106 * through the mmap ioctl's mapping or a GTT mapping.
1109 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1110 struct drm_file *file_priv)
1112 struct drm_i915_private *dev_priv = dev->dev_private;
1113 struct drm_i915_gem_set_domain *args = data;
1114 struct drm_gem_object *obj;
1115 struct drm_i915_gem_object *obj_priv;
1116 uint32_t read_domains = args->read_domains;
1117 uint32_t write_domain = args->write_domain;
1120 if (!(dev->driver->driver_features & DRIVER_GEM))
1123 /* Only handle setting domains to types used by the CPU. */
1124 if (write_domain & I915_GEM_GPU_DOMAINS)
1127 if (read_domains & I915_GEM_GPU_DOMAINS)
1130 /* Having something in the write domain implies it's in the read
1131 * domain, and only that read domain. Enforce that in the request.
1133 if (write_domain != 0 && read_domains != write_domain)
1136 ret = i915_mutex_lock_interruptible(dev);
1140 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1145 obj_priv = to_intel_bo(obj);
1147 intel_mark_busy(dev, obj);
1149 if (read_domains & I915_GEM_DOMAIN_GTT) {
1150 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1152 /* Update the LRU on the fence for the CPU access that's
1155 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1156 struct drm_i915_fence_reg *reg =
1157 &dev_priv->fence_regs[obj_priv->fence_reg];
1158 list_move_tail(®->lru_list,
1159 &dev_priv->mm.fence_list);
1162 /* Silently promote "you're not bound, there was nothing to do"
1163 * to success, since the client was just asking us to
1164 * make sure everything was done.
1169 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1172 /* Maintain LRU order of "inactive" objects */
1173 if (ret == 0 && i915_gem_object_is_inactive(obj_priv))
1174 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1176 drm_gem_object_unreference(obj);
1178 mutex_unlock(&dev->struct_mutex);
1183 * Called when user space has done writes to this buffer
1186 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1187 struct drm_file *file_priv)
1189 struct drm_i915_gem_sw_finish *args = data;
1190 struct drm_gem_object *obj;
1193 if (!(dev->driver->driver_features & DRIVER_GEM))
1196 ret = i915_mutex_lock_interruptible(dev);
1200 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1206 /* Pinned buffers may be scanout, so flush the cache */
1207 if (to_intel_bo(obj)->pin_count)
1208 i915_gem_object_flush_cpu_write_domain(obj);
1210 drm_gem_object_unreference(obj);
1212 mutex_unlock(&dev->struct_mutex);
1217 * Maps the contents of an object, returning the address it is mapped
1220 * While the mapping holds a reference on the contents of the object, it doesn't
1221 * imply a ref on the object itself.
1224 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1225 struct drm_file *file_priv)
1227 struct drm_i915_private *dev_priv = dev->dev_private;
1228 struct drm_i915_gem_mmap *args = data;
1229 struct drm_gem_object *obj;
1233 if (!(dev->driver->driver_features & DRIVER_GEM))
1236 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1240 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1241 drm_gem_object_unreference_unlocked(obj);
1245 offset = args->offset;
1247 down_write(¤t->mm->mmap_sem);
1248 addr = do_mmap(obj->filp, 0, args->size,
1249 PROT_READ | PROT_WRITE, MAP_SHARED,
1251 up_write(¤t->mm->mmap_sem);
1252 drm_gem_object_unreference_unlocked(obj);
1253 if (IS_ERR((void *)addr))
1256 args->addr_ptr = (uint64_t) addr;
1262 * i915_gem_fault - fault a page into the GTT
1263 * vma: VMA in question
1266 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1267 * from userspace. The fault handler takes care of binding the object to
1268 * the GTT (if needed), allocating and programming a fence register (again,
1269 * only if needed based on whether the old reg is still valid or the object
1270 * is tiled) and inserting a new PTE into the faulting process.
1272 * Note that the faulting process may involve evicting existing objects
1273 * from the GTT and/or fence registers to make room. So performance may
1274 * suffer if the GTT working set is large or there are few fence registers
1277 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1279 struct drm_gem_object *obj = vma->vm_private_data;
1280 struct drm_device *dev = obj->dev;
1281 drm_i915_private_t *dev_priv = dev->dev_private;
1282 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1283 pgoff_t page_offset;
1286 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1288 /* We don't use vmf->pgoff since that has the fake offset */
1289 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1292 /* Now bind it into the GTT if needed */
1293 mutex_lock(&dev->struct_mutex);
1294 BUG_ON(obj_priv->pin_count && !obj_priv->pin_mappable);
1296 if (obj_priv->gtt_space) {
1297 if (!obj_priv->mappable ||
1298 (obj_priv->tiling_mode && !obj_priv->fenceable)) {
1299 ret = i915_gem_object_unbind(obj);
1305 if (!obj_priv->gtt_space) {
1306 ret = i915_gem_object_bind_to_gtt(obj, 0,
1307 true, obj_priv->tiling_mode);
1312 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1316 if (!obj_priv->fault_mappable) {
1317 obj_priv->fault_mappable = true;
1318 i915_gem_info_update_mappable(dev_priv, obj_priv, true);
1321 /* Need a new fence register? */
1322 if (obj_priv->tiling_mode != I915_TILING_NONE) {
1323 ret = i915_gem_object_get_fence_reg(obj, true);
1328 if (i915_gem_object_is_inactive(obj_priv))
1329 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1331 pfn = ((dev->agp->base + obj_priv->gtt_offset) >> PAGE_SHIFT) +
1334 /* Finally, remap it using the new GTT offset */
1335 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1337 mutex_unlock(&dev->struct_mutex);
1342 return VM_FAULT_NOPAGE;
1345 return VM_FAULT_OOM;
1347 return VM_FAULT_SIGBUS;
1352 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1353 * @obj: obj in question
1355 * GEM memory mapping works by handing back to userspace a fake mmap offset
1356 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1357 * up the object based on the offset and sets up the various memory mapping
1360 * This routine allocates and attaches a fake offset for @obj.
1363 i915_gem_create_mmap_offset(struct drm_gem_object *obj)
1365 struct drm_device *dev = obj->dev;
1366 struct drm_gem_mm *mm = dev->mm_private;
1367 struct drm_map_list *list;
1368 struct drm_local_map *map;
1371 /* Set the object up for mmap'ing */
1372 list = &obj->map_list;
1373 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1378 map->type = _DRM_GEM;
1379 map->size = obj->size;
1382 /* Get a DRM GEM mmap offset allocated... */
1383 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1384 obj->size / PAGE_SIZE, 0, 0);
1385 if (!list->file_offset_node) {
1386 DRM_ERROR("failed to allocate offset for bo %d\n", obj->name);
1391 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1392 obj->size / PAGE_SIZE, 0);
1393 if (!list->file_offset_node) {
1398 list->hash.key = list->file_offset_node->start;
1399 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1401 DRM_ERROR("failed to add to map hash\n");
1408 drm_mm_put_block(list->file_offset_node);
1417 * i915_gem_release_mmap - remove physical page mappings
1418 * @obj: obj in question
1420 * Preserve the reservation of the mmapping with the DRM core code, but
1421 * relinquish ownership of the pages back to the system.
1423 * It is vital that we remove the page mapping if we have mapped a tiled
1424 * object through the GTT and then lose the fence register due to
1425 * resource pressure. Similarly if the object has been moved out of the
1426 * aperture, than pages mapped into userspace must be revoked. Removing the
1427 * mapping will then trigger a page fault on the next user access, allowing
1428 * fixup by i915_gem_fault().
1431 i915_gem_release_mmap(struct drm_gem_object *obj)
1433 struct drm_device *dev = obj->dev;
1434 struct drm_i915_private *dev_priv = dev->dev_private;
1435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1437 if (unlikely(obj->map_list.map && dev->dev_mapping))
1438 unmap_mapping_range(dev->dev_mapping,
1439 (loff_t)obj->map_list.hash.key<<PAGE_SHIFT,
1442 if (obj_priv->fault_mappable) {
1443 obj_priv->fault_mappable = false;
1444 i915_gem_info_update_mappable(dev_priv, obj_priv, false);
1449 i915_gem_free_mmap_offset(struct drm_gem_object *obj)
1451 struct drm_device *dev = obj->dev;
1452 struct drm_gem_mm *mm = dev->mm_private;
1453 struct drm_map_list *list = &obj->map_list;
1455 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1456 drm_mm_put_block(list->file_offset_node);
1462 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1463 * @obj: object to check
1465 * Return the required GTT alignment for an object, taking into account
1466 * potential fence register mapping if needed.
1469 i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj_priv)
1471 struct drm_device *dev = obj_priv->base.dev;
1474 * Minimum alignment is 4k (GTT page size), but might be greater
1475 * if a fence register is needed for the object.
1477 if (INTEL_INFO(dev)->gen >= 4 ||
1478 obj_priv->tiling_mode == I915_TILING_NONE)
1482 * Previous chips need to be aligned to the size of the smallest
1483 * fence register that can contain the object.
1485 return i915_gem_get_gtt_size(obj_priv);
1489 i915_gem_get_gtt_size(struct drm_i915_gem_object *obj_priv)
1491 struct drm_device *dev = obj_priv->base.dev;
1495 * Minimum alignment is 4k (GTT page size), but might be greater
1496 * if a fence register is needed for the object.
1498 if (INTEL_INFO(dev)->gen >= 4)
1499 return obj_priv->base.size;
1502 * Previous chips need to be aligned to the size of the smallest
1503 * fence register that can contain the object.
1505 if (INTEL_INFO(dev)->gen == 3)
1510 while (size < obj_priv->base.size)
1517 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1519 * @data: GTT mapping ioctl data
1520 * @file_priv: GEM object info
1522 * Simply returns the fake offset to userspace so it can mmap it.
1523 * The mmap call will end up in drm_gem_mmap(), which will set things
1524 * up so we can get faults in the handler above.
1526 * The fault handler will take care of binding the object into the GTT
1527 * (since it may have been evicted to make room for something), allocating
1528 * a fence register, and mapping the appropriate aperture address into
1532 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1533 struct drm_file *file_priv)
1535 struct drm_i915_private *dev_priv = dev->dev_private;
1536 struct drm_i915_gem_mmap_gtt *args = data;
1537 struct drm_gem_object *obj;
1538 struct drm_i915_gem_object *obj_priv;
1541 if (!(dev->driver->driver_features & DRIVER_GEM))
1544 ret = i915_mutex_lock_interruptible(dev);
1548 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
1553 obj_priv = to_intel_bo(obj);
1555 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1560 if (obj_priv->madv != I915_MADV_WILLNEED) {
1561 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1566 if (!obj->map_list.map) {
1567 ret = i915_gem_create_mmap_offset(obj);
1572 args->offset = (u64)obj->map_list.hash.key << PAGE_SHIFT;
1575 drm_gem_object_unreference(obj);
1577 mutex_unlock(&dev->struct_mutex);
1582 i915_gem_object_get_pages_gtt(struct drm_gem_object *obj,
1585 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1587 struct address_space *mapping;
1588 struct inode *inode;
1591 /* Get the list of pages out of our struct file. They'll be pinned
1592 * at this point until we release them.
1594 page_count = obj->size / PAGE_SIZE;
1595 BUG_ON(obj_priv->pages != NULL);
1596 obj_priv->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1597 if (obj_priv->pages == NULL)
1600 inode = obj->filp->f_path.dentry->d_inode;
1601 mapping = inode->i_mapping;
1602 for (i = 0; i < page_count; i++) {
1603 page = read_cache_page_gfp(mapping, i,
1611 obj_priv->pages[i] = page;
1614 if (obj_priv->tiling_mode != I915_TILING_NONE)
1615 i915_gem_object_do_bit_17_swizzle(obj);
1621 page_cache_release(obj_priv->pages[i]);
1623 drm_free_large(obj_priv->pages);
1624 obj_priv->pages = NULL;
1625 return PTR_ERR(page);
1629 i915_gem_object_put_pages_gtt(struct drm_gem_object *obj)
1631 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1632 int page_count = obj->size / PAGE_SIZE;
1635 BUG_ON(obj_priv->madv == __I915_MADV_PURGED);
1637 if (obj_priv->tiling_mode != I915_TILING_NONE)
1638 i915_gem_object_save_bit_17_swizzle(obj);
1640 if (obj_priv->madv == I915_MADV_DONTNEED)
1641 obj_priv->dirty = 0;
1643 for (i = 0; i < page_count; i++) {
1644 if (obj_priv->dirty)
1645 set_page_dirty(obj_priv->pages[i]);
1647 if (obj_priv->madv == I915_MADV_WILLNEED)
1648 mark_page_accessed(obj_priv->pages[i]);
1650 page_cache_release(obj_priv->pages[i]);
1652 obj_priv->dirty = 0;
1654 drm_free_large(obj_priv->pages);
1655 obj_priv->pages = NULL;
1659 i915_gem_next_request_seqno(struct drm_device *dev,
1660 struct intel_ring_buffer *ring)
1662 drm_i915_private_t *dev_priv = dev->dev_private;
1664 ring->outstanding_lazy_request = true;
1665 return dev_priv->next_seqno;
1669 i915_gem_object_move_to_active(struct drm_gem_object *obj,
1670 struct intel_ring_buffer *ring)
1672 struct drm_device *dev = obj->dev;
1673 struct drm_i915_private *dev_priv = dev->dev_private;
1674 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1675 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1677 BUG_ON(ring == NULL);
1678 obj_priv->ring = ring;
1680 /* Add a reference if we're newly entering the active list. */
1681 if (!obj_priv->active) {
1682 drm_gem_object_reference(obj);
1683 obj_priv->active = 1;
1686 /* Move from whatever list we were on to the tail of execution. */
1687 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.active_list);
1688 list_move_tail(&obj_priv->ring_list, &ring->active_list);
1689 obj_priv->last_rendering_seqno = seqno;
1693 i915_gem_object_move_to_flushing(struct drm_gem_object *obj)
1695 struct drm_device *dev = obj->dev;
1696 drm_i915_private_t *dev_priv = dev->dev_private;
1697 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1699 BUG_ON(!obj_priv->active);
1700 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.flushing_list);
1701 list_del_init(&obj_priv->ring_list);
1702 obj_priv->last_rendering_seqno = 0;
1705 /* Immediately discard the backing storage */
1707 i915_gem_object_truncate(struct drm_gem_object *obj)
1709 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1710 struct inode *inode;
1712 /* Our goal here is to return as much of the memory as
1713 * is possible back to the system as we are called from OOM.
1714 * To do this we must instruct the shmfs to drop all of its
1715 * backing pages, *now*. Here we mirror the actions taken
1716 * when by shmem_delete_inode() to release the backing store.
1718 inode = obj->filp->f_path.dentry->d_inode;
1719 truncate_inode_pages(inode->i_mapping, 0);
1720 if (inode->i_op->truncate_range)
1721 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
1723 obj_priv->madv = __I915_MADV_PURGED;
1727 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj_priv)
1729 return obj_priv->madv == I915_MADV_DONTNEED;
1733 i915_gem_object_move_to_inactive(struct drm_gem_object *obj)
1735 struct drm_device *dev = obj->dev;
1736 drm_i915_private_t *dev_priv = dev->dev_private;
1737 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
1739 if (obj_priv->pin_count != 0)
1740 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.pinned_list);
1742 list_move_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
1743 list_del_init(&obj_priv->ring_list);
1745 BUG_ON(!list_empty(&obj_priv->gpu_write_list));
1747 obj_priv->last_rendering_seqno = 0;
1748 obj_priv->ring = NULL;
1749 if (obj_priv->active) {
1750 obj_priv->active = 0;
1751 drm_gem_object_unreference(obj);
1753 WARN_ON(i915_verify_lists(dev));
1757 i915_gem_process_flushing_list(struct drm_device *dev,
1758 uint32_t flush_domains,
1759 struct intel_ring_buffer *ring)
1761 drm_i915_private_t *dev_priv = dev->dev_private;
1762 struct drm_i915_gem_object *obj_priv, *next;
1764 list_for_each_entry_safe(obj_priv, next,
1765 &ring->gpu_write_list,
1767 struct drm_gem_object *obj = &obj_priv->base;
1769 if (obj->write_domain & flush_domains) {
1770 uint32_t old_write_domain = obj->write_domain;
1772 obj->write_domain = 0;
1773 list_del_init(&obj_priv->gpu_write_list);
1774 i915_gem_object_move_to_active(obj, ring);
1776 /* update the fence lru list */
1777 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
1778 struct drm_i915_fence_reg *reg =
1779 &dev_priv->fence_regs[obj_priv->fence_reg];
1780 list_move_tail(®->lru_list,
1781 &dev_priv->mm.fence_list);
1784 trace_i915_gem_object_change_domain(obj,
1792 i915_add_request(struct drm_device *dev,
1793 struct drm_file *file,
1794 struct drm_i915_gem_request *request,
1795 struct intel_ring_buffer *ring)
1797 drm_i915_private_t *dev_priv = dev->dev_private;
1798 struct drm_i915_file_private *file_priv = NULL;
1803 BUG_ON(request == NULL);
1806 file_priv = file->driver_priv;
1808 ret = ring->add_request(ring, &seqno);
1812 ring->outstanding_lazy_request = false;
1814 request->seqno = seqno;
1815 request->ring = ring;
1816 request->emitted_jiffies = jiffies;
1817 was_empty = list_empty(&ring->request_list);
1818 list_add_tail(&request->list, &ring->request_list);
1821 spin_lock(&file_priv->mm.lock);
1822 request->file_priv = file_priv;
1823 list_add_tail(&request->client_list,
1824 &file_priv->mm.request_list);
1825 spin_unlock(&file_priv->mm.lock);
1828 if (!dev_priv->mm.suspended) {
1829 mod_timer(&dev_priv->hangcheck_timer,
1830 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1832 queue_delayed_work(dev_priv->wq,
1833 &dev_priv->mm.retire_work, HZ);
1839 * Command execution barrier
1841 * Ensures that all commands in the ring are finished
1842 * before signalling the CPU
1845 i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
1847 uint32_t flush_domains = 0;
1849 /* The sampler always gets flushed on i965 (sigh) */
1850 if (INTEL_INFO(dev)->gen >= 4)
1851 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1853 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
1857 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1859 struct drm_i915_file_private *file_priv = request->file_priv;
1864 spin_lock(&file_priv->mm.lock);
1865 list_del(&request->client_list);
1866 request->file_priv = NULL;
1867 spin_unlock(&file_priv->mm.lock);
1870 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1871 struct intel_ring_buffer *ring)
1873 while (!list_empty(&ring->request_list)) {
1874 struct drm_i915_gem_request *request;
1876 request = list_first_entry(&ring->request_list,
1877 struct drm_i915_gem_request,
1880 list_del(&request->list);
1881 i915_gem_request_remove_from_client(request);
1885 while (!list_empty(&ring->active_list)) {
1886 struct drm_i915_gem_object *obj_priv;
1888 obj_priv = list_first_entry(&ring->active_list,
1889 struct drm_i915_gem_object,
1892 obj_priv->base.write_domain = 0;
1893 list_del_init(&obj_priv->gpu_write_list);
1894 i915_gem_object_move_to_inactive(&obj_priv->base);
1898 void i915_gem_reset(struct drm_device *dev)
1900 struct drm_i915_private *dev_priv = dev->dev_private;
1901 struct drm_i915_gem_object *obj_priv;
1904 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1905 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1906 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
1908 /* Remove anything from the flushing lists. The GPU cache is likely
1909 * to be lost on reset along with the data, so simply move the
1910 * lost bo to the inactive list.
1912 while (!list_empty(&dev_priv->mm.flushing_list)) {
1913 obj_priv = list_first_entry(&dev_priv->mm.flushing_list,
1914 struct drm_i915_gem_object,
1917 obj_priv->base.write_domain = 0;
1918 list_del_init(&obj_priv->gpu_write_list);
1919 i915_gem_object_move_to_inactive(&obj_priv->base);
1922 /* Move everything out of the GPU domains to ensure we do any
1923 * necessary invalidation upon reuse.
1925 list_for_each_entry(obj_priv,
1926 &dev_priv->mm.inactive_list,
1929 obj_priv->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1932 /* The fence registers are invalidated so clear them out */
1933 for (i = 0; i < 16; i++) {
1934 struct drm_i915_fence_reg *reg;
1936 reg = &dev_priv->fence_regs[i];
1940 i915_gem_clear_fence_reg(reg->obj);
1945 * This function clears the request list as sequence numbers are passed.
1948 i915_gem_retire_requests_ring(struct drm_device *dev,
1949 struct intel_ring_buffer *ring)
1951 drm_i915_private_t *dev_priv = dev->dev_private;
1954 if (!ring->status_page.page_addr ||
1955 list_empty(&ring->request_list))
1958 WARN_ON(i915_verify_lists(dev));
1960 seqno = ring->get_seqno(ring);
1961 while (!list_empty(&ring->request_list)) {
1962 struct drm_i915_gem_request *request;
1964 request = list_first_entry(&ring->request_list,
1965 struct drm_i915_gem_request,
1968 if (!i915_seqno_passed(seqno, request->seqno))
1971 trace_i915_gem_request_retire(dev, request->seqno);
1973 list_del(&request->list);
1974 i915_gem_request_remove_from_client(request);
1978 /* Move any buffers on the active list that are no longer referenced
1979 * by the ringbuffer to the flushing/inactive lists as appropriate.
1981 while (!list_empty(&ring->active_list)) {
1982 struct drm_gem_object *obj;
1983 struct drm_i915_gem_object *obj_priv;
1985 obj_priv = list_first_entry(&ring->active_list,
1986 struct drm_i915_gem_object,
1989 if (!i915_seqno_passed(seqno, obj_priv->last_rendering_seqno))
1992 obj = &obj_priv->base;
1993 if (obj->write_domain != 0)
1994 i915_gem_object_move_to_flushing(obj);
1996 i915_gem_object_move_to_inactive(obj);
1999 if (unlikely (dev_priv->trace_irq_seqno &&
2000 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
2001 ring->user_irq_put(ring);
2002 dev_priv->trace_irq_seqno = 0;
2005 WARN_ON(i915_verify_lists(dev));
2009 i915_gem_retire_requests(struct drm_device *dev)
2011 drm_i915_private_t *dev_priv = dev->dev_private;
2013 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2014 struct drm_i915_gem_object *obj_priv, *tmp;
2016 /* We must be careful that during unbind() we do not
2017 * accidentally infinitely recurse into retire requests.
2019 * retire -> free -> unbind -> wait -> retire_ring
2021 list_for_each_entry_safe(obj_priv, tmp,
2022 &dev_priv->mm.deferred_free_list,
2024 i915_gem_free_object_tail(&obj_priv->base);
2027 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
2028 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
2029 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
2033 i915_gem_retire_work_handler(struct work_struct *work)
2035 drm_i915_private_t *dev_priv;
2036 struct drm_device *dev;
2038 dev_priv = container_of(work, drm_i915_private_t,
2039 mm.retire_work.work);
2040 dev = dev_priv->dev;
2042 /* Come back later if the device is busy... */
2043 if (!mutex_trylock(&dev->struct_mutex)) {
2044 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2048 i915_gem_retire_requests(dev);
2050 if (!dev_priv->mm.suspended &&
2051 (!list_empty(&dev_priv->render_ring.request_list) ||
2052 !list_empty(&dev_priv->bsd_ring.request_list) ||
2053 !list_empty(&dev_priv->blt_ring.request_list)))
2054 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2055 mutex_unlock(&dev->struct_mutex);
2059 i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
2060 bool interruptible, struct intel_ring_buffer *ring)
2062 drm_i915_private_t *dev_priv = dev->dev_private;
2068 if (atomic_read(&dev_priv->mm.wedged))
2071 if (ring->outstanding_lazy_request) {
2072 struct drm_i915_gem_request *request;
2074 request = kzalloc(sizeof(*request), GFP_KERNEL);
2075 if (request == NULL)
2078 ret = i915_add_request(dev, NULL, request, ring);
2084 seqno = request->seqno;
2086 BUG_ON(seqno == dev_priv->next_seqno);
2088 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
2089 if (HAS_PCH_SPLIT(dev))
2090 ier = I915_READ(DEIER) | I915_READ(GTIER);
2092 ier = I915_READ(IER);
2094 DRM_ERROR("something (likely vbetool) disabled "
2095 "interrupts, re-enabling\n");
2096 i915_driver_irq_preinstall(dev);
2097 i915_driver_irq_postinstall(dev);
2100 trace_i915_gem_request_wait_begin(dev, seqno);
2102 ring->waiting_seqno = seqno;
2103 ring->user_irq_get(ring);
2105 ret = wait_event_interruptible(ring->irq_queue,
2106 i915_seqno_passed(ring->get_seqno(ring), seqno)
2107 || atomic_read(&dev_priv->mm.wedged));
2109 wait_event(ring->irq_queue,
2110 i915_seqno_passed(ring->get_seqno(ring), seqno)
2111 || atomic_read(&dev_priv->mm.wedged));
2113 ring->user_irq_put(ring);
2114 ring->waiting_seqno = 0;
2116 trace_i915_gem_request_wait_end(dev, seqno);
2118 if (atomic_read(&dev_priv->mm.wedged))
2121 if (ret && ret != -ERESTARTSYS)
2122 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2123 __func__, ret, seqno, ring->get_seqno(ring),
2124 dev_priv->next_seqno);
2126 /* Directly dispatch request retiring. While we have the work queue
2127 * to handle this, the waiter on a request often wants an associated
2128 * buffer to have made it to the inactive list, and we would need
2129 * a separate wait queue to handle that.
2132 i915_gem_retire_requests_ring(dev, ring);
2138 * Waits for a sequence number to be signaled, and cleans up the
2139 * request and object lists appropriately for that event.
2142 i915_wait_request(struct drm_device *dev, uint32_t seqno,
2143 struct intel_ring_buffer *ring)
2145 return i915_do_wait_request(dev, seqno, 1, ring);
2149 i915_gem_flush_ring(struct drm_device *dev,
2150 struct drm_file *file_priv,
2151 struct intel_ring_buffer *ring,
2152 uint32_t invalidate_domains,
2153 uint32_t flush_domains)
2155 ring->flush(ring, invalidate_domains, flush_domains);
2156 i915_gem_process_flushing_list(dev, flush_domains, ring);
2160 i915_gem_flush(struct drm_device *dev,
2161 struct drm_file *file_priv,
2162 uint32_t invalidate_domains,
2163 uint32_t flush_domains,
2164 uint32_t flush_rings)
2166 drm_i915_private_t *dev_priv = dev->dev_private;
2168 if (flush_domains & I915_GEM_DOMAIN_CPU)
2169 drm_agp_chipset_flush(dev);
2171 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2172 if (flush_rings & RING_RENDER)
2173 i915_gem_flush_ring(dev, file_priv,
2174 &dev_priv->render_ring,
2175 invalidate_domains, flush_domains);
2176 if (flush_rings & RING_BSD)
2177 i915_gem_flush_ring(dev, file_priv,
2178 &dev_priv->bsd_ring,
2179 invalidate_domains, flush_domains);
2180 if (flush_rings & RING_BLT)
2181 i915_gem_flush_ring(dev, file_priv,
2182 &dev_priv->blt_ring,
2183 invalidate_domains, flush_domains);
2188 * Ensures that all rendering to the object has completed and the object is
2189 * safe to unbind from the GTT or access from the CPU.
2192 i915_gem_object_wait_rendering(struct drm_gem_object *obj,
2195 struct drm_device *dev = obj->dev;
2196 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2199 /* This function only exists to support waiting for existing rendering,
2200 * not for emitting required flushes.
2202 BUG_ON((obj->write_domain & I915_GEM_GPU_DOMAINS) != 0);
2204 /* If there is rendering queued on the buffer being evicted, wait for
2207 if (obj_priv->active) {
2208 ret = i915_do_wait_request(dev,
2209 obj_priv->last_rendering_seqno,
2220 * Unbinds an object from the GTT aperture.
2223 i915_gem_object_unbind(struct drm_gem_object *obj)
2225 struct drm_device *dev = obj->dev;
2226 struct drm_i915_private *dev_priv = dev->dev_private;
2227 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2230 if (obj_priv->gtt_space == NULL)
2233 if (obj_priv->pin_count != 0) {
2234 DRM_ERROR("Attempting to unbind pinned buffer\n");
2238 /* blow away mappings if mapped through GTT */
2239 i915_gem_release_mmap(obj);
2241 /* Move the object to the CPU domain to ensure that
2242 * any possible CPU writes while it's not in the GTT
2243 * are flushed when we go to remap it. This will
2244 * also ensure that all pending GPU writes are finished
2247 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2248 if (ret == -ERESTARTSYS)
2250 /* Continue on if we fail due to EIO, the GPU is hung so we
2251 * should be safe and we need to cleanup or else we might
2252 * cause memory corruption through use-after-free.
2255 i915_gem_clflush_object(obj);
2256 obj->read_domains = obj->write_domain = I915_GEM_DOMAIN_CPU;
2259 /* release the fence reg _after_ flushing */
2260 if (obj_priv->fence_reg != I915_FENCE_REG_NONE)
2261 i915_gem_clear_fence_reg(obj);
2263 drm_unbind_agp(obj_priv->agp_mem);
2264 drm_free_agp(obj_priv->agp_mem, obj->size / PAGE_SIZE);
2266 i915_gem_object_put_pages_gtt(obj);
2268 i915_gem_info_remove_gtt(dev_priv, obj_priv);
2269 list_del_init(&obj_priv->mm_list);
2270 obj_priv->fenceable = true;
2271 obj_priv->mappable = true;
2273 drm_mm_put_block(obj_priv->gtt_space);
2274 obj_priv->gtt_space = NULL;
2275 obj_priv->gtt_offset = 0;
2277 if (i915_gem_object_is_purgeable(obj_priv))
2278 i915_gem_object_truncate(obj);
2280 trace_i915_gem_object_unbind(obj);
2285 static int i915_ring_idle(struct drm_device *dev,
2286 struct intel_ring_buffer *ring)
2288 if (list_empty(&ring->gpu_write_list))
2291 i915_gem_flush_ring(dev, NULL, ring,
2292 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2293 return i915_wait_request(dev,
2294 i915_gem_next_request_seqno(dev, ring),
2299 i915_gpu_idle(struct drm_device *dev)
2301 drm_i915_private_t *dev_priv = dev->dev_private;
2305 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2306 list_empty(&dev_priv->render_ring.active_list) &&
2307 list_empty(&dev_priv->bsd_ring.active_list) &&
2308 list_empty(&dev_priv->blt_ring.active_list));
2312 /* Flush everything onto the inactive list. */
2313 ret = i915_ring_idle(dev, &dev_priv->render_ring);
2317 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2321 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2328 static void sandybridge_write_fence_reg(struct drm_gem_object *obj)
2330 struct drm_device *dev = obj->dev;
2331 drm_i915_private_t *dev_priv = dev->dev_private;
2332 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2333 u32 size = i915_gem_get_gtt_size(obj_priv);
2334 int regnum = obj_priv->fence_reg;
2337 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
2339 val |= obj_priv->gtt_offset & 0xfffff000;
2340 val |= (uint64_t)((obj_priv->stride / 128) - 1) <<
2341 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2343 if (obj_priv->tiling_mode == I915_TILING_Y)
2344 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2345 val |= I965_FENCE_REG_VALID;
2347 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2350 static void i965_write_fence_reg(struct drm_gem_object *obj)
2352 struct drm_device *dev = obj->dev;
2353 drm_i915_private_t *dev_priv = dev->dev_private;
2354 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2355 u32 size = i915_gem_get_gtt_size(obj_priv);
2356 int regnum = obj_priv->fence_reg;
2359 val = (uint64_t)((obj_priv->gtt_offset + size - 4096) &
2361 val |= obj_priv->gtt_offset & 0xfffff000;
2362 val |= ((obj_priv->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2363 if (obj_priv->tiling_mode == I915_TILING_Y)
2364 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2365 val |= I965_FENCE_REG_VALID;
2367 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2370 static void i915_write_fence_reg(struct drm_gem_object *obj)
2372 struct drm_device *dev = obj->dev;
2373 drm_i915_private_t *dev_priv = dev->dev_private;
2374 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2375 u32 size = i915_gem_get_gtt_size(obj_priv);
2376 uint32_t fence_reg, val, pitch_val;
2379 if ((obj_priv->gtt_offset & ~I915_FENCE_START_MASK) ||
2380 (obj_priv->gtt_offset & (size - 1))) {
2381 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2382 __func__, obj_priv->gtt_offset, obj_priv->fenceable, size,
2383 obj_priv->gtt_space->start, obj_priv->gtt_space->size);
2387 if (obj_priv->tiling_mode == I915_TILING_Y &&
2388 HAS_128_BYTE_Y_TILING(dev))
2393 /* Note: pitch better be a power of two tile widths */
2394 pitch_val = obj_priv->stride / tile_width;
2395 pitch_val = ffs(pitch_val) - 1;
2397 if (obj_priv->tiling_mode == I915_TILING_Y &&
2398 HAS_128_BYTE_Y_TILING(dev))
2399 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2401 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2403 val = obj_priv->gtt_offset;
2404 if (obj_priv->tiling_mode == I915_TILING_Y)
2405 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2406 val |= I915_FENCE_SIZE_BITS(size);
2407 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2408 val |= I830_FENCE_REG_VALID;
2410 fence_reg = obj_priv->fence_reg;
2412 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2414 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2415 I915_WRITE(fence_reg, val);
2418 static void i830_write_fence_reg(struct drm_gem_object *obj)
2420 struct drm_device *dev = obj->dev;
2421 drm_i915_private_t *dev_priv = dev->dev_private;
2422 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2423 u32 size = i915_gem_get_gtt_size(obj_priv);
2424 int regnum = obj_priv->fence_reg;
2427 uint32_t fence_size_bits;
2429 if ((obj_priv->gtt_offset & ~I830_FENCE_START_MASK) ||
2430 (obj_priv->gtt_offset & (obj->size - 1))) {
2431 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2432 __func__, obj_priv->gtt_offset);
2436 pitch_val = obj_priv->stride / 128;
2437 pitch_val = ffs(pitch_val) - 1;
2438 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2440 val = obj_priv->gtt_offset;
2441 if (obj_priv->tiling_mode == I915_TILING_Y)
2442 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2443 fence_size_bits = I830_FENCE_SIZE_BITS(size);
2444 WARN_ON(fence_size_bits & ~0x00000f00);
2445 val |= fence_size_bits;
2446 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2447 val |= I830_FENCE_REG_VALID;
2449 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2452 static int i915_find_fence_reg(struct drm_device *dev,
2455 struct drm_i915_private *dev_priv = dev->dev_private;
2456 struct drm_i915_fence_reg *reg;
2457 struct drm_i915_gem_object *obj_priv = NULL;
2460 /* First try to find a free reg */
2462 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2463 reg = &dev_priv->fence_regs[i];
2467 obj_priv = to_intel_bo(reg->obj);
2468 if (!obj_priv->pin_count)
2475 /* None available, try to steal one or wait for a user to finish */
2476 avail = I915_FENCE_REG_NONE;
2477 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2479 obj_priv = to_intel_bo(reg->obj);
2480 if (obj_priv->pin_count)
2484 avail = obj_priv->fence_reg;
2488 BUG_ON(avail == I915_FENCE_REG_NONE);
2490 /* We only have a reference on obj from the active list. put_fence_reg
2491 * might drop that one, causing a use-after-free in it. So hold a
2492 * private reference to obj like the other callers of put_fence_reg
2493 * (set_tiling ioctl) do. */
2494 drm_gem_object_reference(&obj_priv->base);
2495 ret = i915_gem_object_put_fence_reg(&obj_priv->base, interruptible);
2496 drm_gem_object_unreference(&obj_priv->base);
2504 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2505 * @obj: object to map through a fence reg
2507 * When mapping objects through the GTT, userspace wants to be able to write
2508 * to them without having to worry about swizzling if the object is tiled.
2510 * This function walks the fence regs looking for a free one for @obj,
2511 * stealing one if it can't find any.
2513 * It then sets up the reg based on the object's properties: address, pitch
2514 * and tiling format.
2517 i915_gem_object_get_fence_reg(struct drm_gem_object *obj,
2520 struct drm_device *dev = obj->dev;
2521 struct drm_i915_private *dev_priv = dev->dev_private;
2522 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2523 struct drm_i915_fence_reg *reg = NULL;
2526 /* Just update our place in the LRU if our fence is getting used. */
2527 if (obj_priv->fence_reg != I915_FENCE_REG_NONE) {
2528 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2529 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2533 switch (obj_priv->tiling_mode) {
2534 case I915_TILING_NONE:
2535 WARN(1, "allocating a fence for non-tiled object?\n");
2538 if (!obj_priv->stride)
2540 WARN((obj_priv->stride & (512 - 1)),
2541 "object 0x%08x is X tiled but has non-512B pitch\n",
2542 obj_priv->gtt_offset);
2545 if (!obj_priv->stride)
2547 WARN((obj_priv->stride & (128 - 1)),
2548 "object 0x%08x is Y tiled but has non-128B pitch\n",
2549 obj_priv->gtt_offset);
2553 ret = i915_find_fence_reg(dev, interruptible);
2557 obj_priv->fence_reg = ret;
2558 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2559 list_add_tail(®->lru_list, &dev_priv->mm.fence_list);
2563 switch (INTEL_INFO(dev)->gen) {
2565 sandybridge_write_fence_reg(obj);
2569 i965_write_fence_reg(obj);
2572 i915_write_fence_reg(obj);
2575 i830_write_fence_reg(obj);
2579 trace_i915_gem_object_get_fence(obj,
2580 obj_priv->fence_reg,
2581 obj_priv->tiling_mode);
2587 * i915_gem_clear_fence_reg - clear out fence register info
2588 * @obj: object to clear
2590 * Zeroes out the fence register itself and clears out the associated
2591 * data structures in dev_priv and obj_priv.
2594 i915_gem_clear_fence_reg(struct drm_gem_object *obj)
2596 struct drm_device *dev = obj->dev;
2597 drm_i915_private_t *dev_priv = dev->dev_private;
2598 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2599 struct drm_i915_fence_reg *reg =
2600 &dev_priv->fence_regs[obj_priv->fence_reg];
2603 switch (INTEL_INFO(dev)->gen) {
2605 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2606 (obj_priv->fence_reg * 8), 0);
2610 I915_WRITE64(FENCE_REG_965_0 + (obj_priv->fence_reg * 8), 0);
2613 if (obj_priv->fence_reg >= 8)
2614 fence_reg = FENCE_REG_945_8 + (obj_priv->fence_reg - 8) * 4;
2617 fence_reg = FENCE_REG_830_0 + obj_priv->fence_reg * 4;
2619 I915_WRITE(fence_reg, 0);
2624 obj_priv->fence_reg = I915_FENCE_REG_NONE;
2625 list_del_init(®->lru_list);
2629 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2630 * to the buffer to finish, and then resets the fence register.
2631 * @obj: tiled object holding a fence register.
2632 * @bool: whether the wait upon the fence is interruptible
2634 * Zeroes out the fence register itself and clears out the associated
2635 * data structures in dev_priv and obj_priv.
2638 i915_gem_object_put_fence_reg(struct drm_gem_object *obj,
2641 struct drm_device *dev = obj->dev;
2642 struct drm_i915_private *dev_priv = dev->dev_private;
2643 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2644 struct drm_i915_fence_reg *reg;
2646 if (obj_priv->fence_reg == I915_FENCE_REG_NONE)
2649 /* If we've changed tiling, GTT-mappings of the object
2650 * need to re-fault to ensure that the correct fence register
2651 * setup is in place.
2653 i915_gem_release_mmap(obj);
2655 /* On the i915, GPU access to tiled buffers is via a fence,
2656 * therefore we must wait for any outstanding access to complete
2657 * before clearing the fence.
2659 reg = &dev_priv->fence_regs[obj_priv->fence_reg];
2663 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2667 ret = i915_gem_object_wait_rendering(obj, interruptible);
2674 i915_gem_object_flush_gtt_write_domain(obj);
2675 i915_gem_clear_fence_reg(obj);
2681 * Finds free space in the GTT aperture and binds the object there.
2684 i915_gem_object_bind_to_gtt(struct drm_gem_object *obj,
2689 struct drm_device *dev = obj->dev;
2690 drm_i915_private_t *dev_priv = dev->dev_private;
2691 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2692 struct drm_mm_node *free_space;
2693 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2694 u32 size, fence_size, fence_alignment;
2697 if (obj_priv->madv != I915_MADV_WILLNEED) {
2698 DRM_ERROR("Attempting to bind a purgeable object\n");
2702 fence_size = i915_gem_get_gtt_size(obj_priv);
2703 fence_alignment = i915_gem_get_gtt_alignment(obj_priv);
2706 alignment = need_fence ? fence_alignment : 4096;
2707 if (need_fence && alignment & (fence_alignment - 1)) {
2708 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2712 size = need_fence ? fence_size : obj->size;
2714 /* If the object is bigger than the entire aperture, reject it early
2715 * before evicting everything in a vain attempt to find space.
2718 (mappable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2719 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2726 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2728 dev_priv->mm.gtt_mappable_end,
2731 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2732 size, alignment, 0);
2734 if (free_space != NULL) {
2736 obj_priv->gtt_space =
2737 drm_mm_get_block_range_generic(free_space,
2739 dev_priv->mm.gtt_mappable_end,
2742 obj_priv->gtt_space =
2743 drm_mm_get_block(free_space, size, alignment);
2745 if (obj_priv->gtt_space == NULL) {
2746 /* If the gtt is empty and we're still having trouble
2747 * fitting our object in, we're out of memory.
2749 ret = i915_gem_evict_something(dev, size, alignment, mappable);
2756 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2758 drm_mm_put_block(obj_priv->gtt_space);
2759 obj_priv->gtt_space = NULL;
2761 if (ret == -ENOMEM) {
2762 /* first try to clear up some space from the GTT */
2763 ret = i915_gem_evict_something(dev, size,
2764 alignment, mappable);
2766 /* now try to shrink everyone else */
2781 /* Create an AGP memory structure pointing at our pages, and bind it
2784 obj_priv->agp_mem = drm_agp_bind_pages(dev,
2786 obj->size >> PAGE_SHIFT,
2787 obj_priv->gtt_space->start,
2788 obj_priv->agp_type);
2789 if (obj_priv->agp_mem == NULL) {
2790 i915_gem_object_put_pages_gtt(obj);
2791 drm_mm_put_block(obj_priv->gtt_space);
2792 obj_priv->gtt_space = NULL;
2794 ret = i915_gem_evict_something(dev, size,
2795 alignment, mappable);
2802 obj_priv->gtt_offset = obj_priv->gtt_space->start;
2804 /* keep track of bounds object by adding it to the inactive list */
2805 list_add_tail(&obj_priv->mm_list, &dev_priv->mm.inactive_list);
2806 i915_gem_info_add_gtt(dev_priv, obj_priv);
2808 /* Assert that the object is not currently in any GPU domain. As it
2809 * wasn't in the GTT, there shouldn't be any way it could have been in
2812 BUG_ON(obj->read_domains & I915_GEM_GPU_DOMAINS);
2813 BUG_ON(obj->write_domain & I915_GEM_GPU_DOMAINS);
2815 trace_i915_gem_object_bind(obj, obj_priv->gtt_offset, mappable);
2817 obj_priv->fenceable =
2818 obj_priv->gtt_space->size == fence_size &&
2819 (obj_priv->gtt_space->start & (fence_alignment -1)) == 0;
2821 obj_priv->mappable =
2822 obj_priv->gtt_offset + obj->size <= dev_priv->mm.gtt_mappable_end;
2828 i915_gem_clflush_object(struct drm_gem_object *obj)
2830 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2832 /* If we don't have a page list set up, then we're not pinned
2833 * to GPU, and we can ignore the cache flush because it'll happen
2834 * again at bind time.
2836 if (obj_priv->pages == NULL)
2839 trace_i915_gem_object_clflush(obj);
2841 drm_clflush_pages(obj_priv->pages, obj->size / PAGE_SIZE);
2844 /** Flushes any GPU write domain for the object if it's dirty. */
2846 i915_gem_object_flush_gpu_write_domain(struct drm_gem_object *obj,
2849 struct drm_device *dev = obj->dev;
2851 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2854 /* Queue the GPU write cache flushing we need. */
2855 i915_gem_flush_ring(dev, NULL,
2856 to_intel_bo(obj)->ring,
2857 0, obj->write_domain);
2858 BUG_ON(obj->write_domain);
2863 return i915_gem_object_wait_rendering(obj, true);
2866 /** Flushes the GTT write domain for the object if it's dirty. */
2868 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2870 uint32_t old_write_domain;
2872 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2875 /* No actual flushing is required for the GTT write domain. Writes
2876 * to it immediately go to main memory as far as we know, so there's
2877 * no chipset flush. It also doesn't land in render cache.
2879 i915_gem_release_mmap(obj);
2881 old_write_domain = obj->write_domain;
2882 obj->write_domain = 0;
2884 trace_i915_gem_object_change_domain(obj,
2889 /** Flushes the CPU write domain for the object if it's dirty. */
2891 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2893 struct drm_device *dev = obj->dev;
2894 uint32_t old_write_domain;
2896 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2899 i915_gem_clflush_object(obj);
2900 drm_agp_chipset_flush(dev);
2901 old_write_domain = obj->write_domain;
2902 obj->write_domain = 0;
2904 trace_i915_gem_object_change_domain(obj,
2910 * Moves a single object to the GTT read, and possibly write domain.
2912 * This function returns when the move is complete, including waiting on
2916 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2918 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2919 uint32_t old_write_domain, old_read_domains;
2922 /* Not valid to be called on unbound objects. */
2923 if (obj_priv->gtt_space == NULL)
2926 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
2930 i915_gem_object_flush_cpu_write_domain(obj);
2933 ret = i915_gem_object_wait_rendering(obj, true);
2938 old_write_domain = obj->write_domain;
2939 old_read_domains = obj->read_domains;
2941 /* It should now be out of any other write domains, and we can update
2942 * the domain values for our changes.
2944 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2945 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2947 obj->read_domains = I915_GEM_DOMAIN_GTT;
2948 obj->write_domain = I915_GEM_DOMAIN_GTT;
2949 obj_priv->dirty = 1;
2952 trace_i915_gem_object_change_domain(obj,
2960 * Prepare buffer for display plane. Use uninterruptible for possible flush
2961 * wait, as in modesetting process we're not supposed to be interrupted.
2964 i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2967 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2968 uint32_t old_read_domains;
2971 /* Not valid to be called on unbound objects. */
2972 if (obj_priv->gtt_space == NULL)
2975 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2979 /* Currently, we are always called from an non-interruptible context. */
2981 ret = i915_gem_object_wait_rendering(obj, false);
2986 i915_gem_object_flush_cpu_write_domain(obj);
2988 old_read_domains = obj->read_domains;
2989 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2991 trace_i915_gem_object_change_domain(obj,
2999 * Moves a single object to the CPU read, and possibly write domain.
3001 * This function returns when the move is complete, including waiting on
3005 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3007 uint32_t old_write_domain, old_read_domains;
3010 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3014 i915_gem_object_flush_gtt_write_domain(obj);
3016 /* If we have a partially-valid cache of the object in the CPU,
3017 * finish invalidating it and free the per-page flags.
3019 i915_gem_object_set_to_full_cpu_read_domain(obj);
3022 ret = i915_gem_object_wait_rendering(obj, true);
3027 old_write_domain = obj->write_domain;
3028 old_read_domains = obj->read_domains;
3030 /* Flush the CPU cache if it's still invalid. */
3031 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3032 i915_gem_clflush_object(obj);
3034 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3037 /* It should now be out of any other write domains, and we can update
3038 * the domain values for our changes.
3040 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3042 /* If we're writing through the CPU, then the GPU read domains will
3043 * need to be invalidated at next use.
3046 obj->read_domains = I915_GEM_DOMAIN_CPU;
3047 obj->write_domain = I915_GEM_DOMAIN_CPU;
3050 trace_i915_gem_object_change_domain(obj,
3058 * Set the next domain for the specified object. This
3059 * may not actually perform the necessary flushing/invaliding though,
3060 * as that may want to be batched with other set_domain operations
3062 * This is (we hope) the only really tricky part of gem. The goal
3063 * is fairly simple -- track which caches hold bits of the object
3064 * and make sure they remain coherent. A few concrete examples may
3065 * help to explain how it works. For shorthand, we use the notation
3066 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3067 * a pair of read and write domain masks.
3069 * Case 1: the batch buffer
3075 * 5. Unmapped from GTT
3078 * Let's take these a step at a time
3081 * Pages allocated from the kernel may still have
3082 * cache contents, so we set them to (CPU, CPU) always.
3083 * 2. Written by CPU (using pwrite)
3084 * The pwrite function calls set_domain (CPU, CPU) and
3085 * this function does nothing (as nothing changes)
3087 * This function asserts that the object is not
3088 * currently in any GPU-based read or write domains
3090 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3091 * As write_domain is zero, this function adds in the
3092 * current read domains (CPU+COMMAND, 0).
3093 * flush_domains is set to CPU.
3094 * invalidate_domains is set to COMMAND
3095 * clflush is run to get data out of the CPU caches
3096 * then i915_dev_set_domain calls i915_gem_flush to
3097 * emit an MI_FLUSH and drm_agp_chipset_flush
3098 * 5. Unmapped from GTT
3099 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3100 * flush_domains and invalidate_domains end up both zero
3101 * so no flushing/invalidating happens
3105 * Case 2: The shared render buffer
3109 * 3. Read/written by GPU
3110 * 4. set_domain to (CPU,CPU)
3111 * 5. Read/written by CPU
3112 * 6. Read/written by GPU
3115 * Same as last example, (CPU, CPU)
3117 * Nothing changes (assertions find that it is not in the GPU)
3118 * 3. Read/written by GPU
3119 * execbuffer calls set_domain (RENDER, RENDER)
3120 * flush_domains gets CPU
3121 * invalidate_domains gets GPU
3123 * MI_FLUSH and drm_agp_chipset_flush
3124 * 4. set_domain (CPU, CPU)
3125 * flush_domains gets GPU
3126 * invalidate_domains gets CPU
3127 * wait_rendering (obj) to make sure all drawing is complete.
3128 * This will include an MI_FLUSH to get the data from GPU
3130 * clflush (obj) to invalidate the CPU cache
3131 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3132 * 5. Read/written by CPU
3133 * cache lines are loaded and dirtied
3134 * 6. Read written by GPU
3135 * Same as last GPU access
3137 * Case 3: The constant buffer
3142 * 4. Updated (written) by CPU again
3151 * flush_domains = CPU
3152 * invalidate_domains = RENDER
3155 * drm_agp_chipset_flush
3156 * 4. Updated (written) by CPU again
3158 * flush_domains = 0 (no previous write domain)
3159 * invalidate_domains = 0 (no new read domains)
3162 * flush_domains = CPU
3163 * invalidate_domains = RENDER
3166 * drm_agp_chipset_flush
3169 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3170 struct intel_ring_buffer *ring)
3172 struct drm_device *dev = obj->dev;
3173 struct drm_i915_private *dev_priv = dev->dev_private;
3174 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3175 uint32_t invalidate_domains = 0;
3176 uint32_t flush_domains = 0;
3179 * If the object isn't moving to a new write domain,
3180 * let the object stay in multiple read domains
3182 if (obj->pending_write_domain == 0)
3183 obj->pending_read_domains |= obj->read_domains;
3186 * Flush the current write domain if
3187 * the new read domains don't match. Invalidate
3188 * any read domains which differ from the old
3191 if (obj->write_domain &&
3192 obj->write_domain != obj->pending_read_domains) {
3193 flush_domains |= obj->write_domain;
3194 invalidate_domains |=
3195 obj->pending_read_domains & ~obj->write_domain;
3198 * Invalidate any read caches which may have
3199 * stale data. That is, any new read domains.
3201 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
3202 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
3203 i915_gem_clflush_object(obj);
3205 /* blow away mappings if mapped through GTT */
3206 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3207 i915_gem_release_mmap(obj);
3209 /* The actual obj->write_domain will be updated with
3210 * pending_write_domain after we emit the accumulated flush for all
3211 * of our domain changes in execbuffers (which clears objects'
3212 * write_domains). So if we have a current write domain that we
3213 * aren't changing, set pending_write_domain to that.
3215 if (flush_domains == 0 && obj->pending_write_domain == 0)
3216 obj->pending_write_domain = obj->write_domain;
3218 dev->invalidate_domains |= invalidate_domains;
3219 dev->flush_domains |= flush_domains;
3220 if (flush_domains & I915_GEM_GPU_DOMAINS)
3221 dev_priv->mm.flush_rings |= obj_priv->ring->id;
3222 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3223 dev_priv->mm.flush_rings |= ring->id;
3227 * Moves the object from a partially CPU read to a full one.
3229 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3230 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3233 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3235 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3237 if (!obj_priv->page_cpu_valid)
3240 /* If we're partially in the CPU read domain, finish moving it in.
3242 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3245 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3246 if (obj_priv->page_cpu_valid[i])
3248 drm_clflush_pages(obj_priv->pages + i, 1);
3252 /* Free the page_cpu_valid mappings which are now stale, whether
3253 * or not we've got I915_GEM_DOMAIN_CPU.
3255 kfree(obj_priv->page_cpu_valid);
3256 obj_priv->page_cpu_valid = NULL;
3260 * Set the CPU read domain on a range of the object.
3262 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3263 * not entirely valid. The page_cpu_valid member of the object flags which
3264 * pages have been flushed, and will be respected by
3265 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3266 * of the whole object.
3268 * This function returns when the move is complete, including waiting on
3272 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3273 uint64_t offset, uint64_t size)
3275 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3276 uint32_t old_read_domains;
3279 if (offset == 0 && size == obj->size)
3280 return i915_gem_object_set_to_cpu_domain(obj, 0);
3282 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3285 i915_gem_object_flush_gtt_write_domain(obj);
3287 /* If we're already fully in the CPU read domain, we're done. */
3288 if (obj_priv->page_cpu_valid == NULL &&
3289 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
3292 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3293 * newly adding I915_GEM_DOMAIN_CPU
3295 if (obj_priv->page_cpu_valid == NULL) {
3296 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3298 if (obj_priv->page_cpu_valid == NULL)
3300 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3301 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
3303 /* Flush the cache on any pages that are still invalid from the CPU's
3306 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3308 if (obj_priv->page_cpu_valid[i])
3311 drm_clflush_pages(obj_priv->pages + i, 1);
3313 obj_priv->page_cpu_valid[i] = 1;
3316 /* It should now be out of any other write domains, and we can update
3317 * the domain values for our changes.
3319 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3321 old_read_domains = obj->read_domains;
3322 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3324 trace_i915_gem_object_change_domain(obj,
3332 * Pin an object to the GTT and evaluate the relocations landing in it.
3335 i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3336 struct drm_file *file_priv,
3337 struct drm_i915_gem_exec_object2 *entry)
3339 struct drm_device *dev = obj->base.dev;
3340 drm_i915_private_t *dev_priv = dev->dev_private;
3341 struct drm_i915_gem_relocation_entry __user *user_relocs;
3342 struct drm_gem_object *target_obj = NULL;
3343 uint32_t target_handle = 0;
3346 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3347 for (i = 0; i < entry->relocation_count; i++) {
3348 struct drm_i915_gem_relocation_entry reloc;
3349 uint32_t target_offset;
3351 if (__copy_from_user_inatomic(&reloc,
3358 if (reloc.target_handle != target_handle) {
3359 drm_gem_object_unreference(target_obj);
3361 target_obj = drm_gem_object_lookup(dev, file_priv,
3362 reloc.target_handle);
3363 if (target_obj == NULL) {
3368 target_handle = reloc.target_handle;
3370 target_offset = to_intel_bo(target_obj)->gtt_offset;
3373 DRM_INFO("%s: obj %p offset %08x target %d "
3374 "read %08x write %08x gtt %08x "
3375 "presumed %08x delta %08x\n",
3379 (int) reloc.target_handle,
3380 (int) reloc.read_domains,
3381 (int) reloc.write_domain,
3382 (int) target_offset,
3383 (int) reloc.presumed_offset,
3387 /* The target buffer should have appeared before us in the
3388 * exec_object list, so it should have a GTT space bound by now.
3390 if (target_offset == 0) {
3391 DRM_ERROR("No GTT space found for object %d\n",
3392 reloc.target_handle);
3397 /* Validate that the target is in a valid r/w GPU domain */
3398 if (reloc.write_domain & (reloc.write_domain - 1)) {
3399 DRM_ERROR("reloc with multiple write domains: "
3400 "obj %p target %d offset %d "
3401 "read %08x write %08x",
3402 obj, reloc.target_handle,
3405 reloc.write_domain);
3409 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3410 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
3411 DRM_ERROR("reloc with read/write CPU domains: "
3412 "obj %p target %d offset %d "
3413 "read %08x write %08x",
3414 obj, reloc.target_handle,
3417 reloc.write_domain);
3421 if (reloc.write_domain && target_obj->pending_write_domain &&
3422 reloc.write_domain != target_obj->pending_write_domain) {
3423 DRM_ERROR("Write domain conflict: "
3424 "obj %p target %d offset %d "
3425 "new %08x old %08x\n",
3426 obj, reloc.target_handle,
3429 target_obj->pending_write_domain);
3434 target_obj->pending_read_domains |= reloc.read_domains;
3435 target_obj->pending_write_domain |= reloc.write_domain;
3437 /* If the relocation already has the right value in it, no
3438 * more work needs to be done.
3440 if (target_offset == reloc.presumed_offset)
3443 /* Check that the relocation address is valid... */
3444 if (reloc.offset > obj->base.size - 4) {
3445 DRM_ERROR("Relocation beyond object bounds: "
3446 "obj %p target %d offset %d size %d.\n",
3447 obj, reloc.target_handle,
3448 (int) reloc.offset, (int) obj->base.size);
3452 if (reloc.offset & 3) {
3453 DRM_ERROR("Relocation not 4-byte aligned: "
3454 "obj %p target %d offset %d.\n",
3455 obj, reloc.target_handle,
3456 (int) reloc.offset);
3461 /* and points to somewhere within the target object. */
3462 if (reloc.delta >= target_obj->size) {
3463 DRM_ERROR("Relocation beyond target object bounds: "
3464 "obj %p target %d delta %d size %d.\n",
3465 obj, reloc.target_handle,
3466 (int) reloc.delta, (int) target_obj->size);
3471 reloc.delta += target_offset;
3472 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3473 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3476 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
3477 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
3478 kunmap_atomic(vaddr);
3480 uint32_t __iomem *reloc_entry;
3481 void __iomem *reloc_page;
3483 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3487 /* Map the page containing the relocation we're going to perform. */
3488 reloc.offset += obj->gtt_offset;
3489 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3490 reloc.offset & PAGE_MASK);
3491 reloc_entry = (uint32_t __iomem *)
3492 (reloc_page + (reloc.offset & ~PAGE_MASK));
3493 iowrite32(reloc.delta, reloc_entry);
3494 io_mapping_unmap_atomic(reloc_page);
3497 /* and update the user's relocation entry */
3498 reloc.presumed_offset = target_offset;
3499 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3500 &reloc.presumed_offset,
3501 sizeof(reloc.presumed_offset))) {
3507 drm_gem_object_unreference(target_obj);
3512 i915_gem_execbuffer_pin(struct drm_device *dev,
3513 struct drm_file *file,
3514 struct drm_gem_object **object_list,
3515 struct drm_i915_gem_exec_object2 *exec_list,
3518 struct drm_i915_private *dev_priv = dev->dev_private;
3521 /* attempt to pin all of the buffers into the GTT */
3522 for (retry = 0; retry < 2; retry++) {
3524 for (i = 0; i < count; i++) {
3525 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3526 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3528 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3529 obj->tiling_mode != I915_TILING_NONE;
3531 /* g33/pnv can't fence buffers in the unmappable part */
3532 bool need_mappable =
3533 entry->relocation_count ? true : need_fence;
3535 /* Check fence reg constraints and rebind if necessary */
3536 if ((need_fence && !obj->fenceable) ||
3537 (need_mappable && !obj->mappable)) {
3538 ret = i915_gem_object_unbind(&obj->base);
3543 ret = i915_gem_object_pin(&obj->base,
3551 * Pre-965 chips need a fence register set up in order
3552 * to properly handle blits to/from tiled surfaces.
3555 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3557 i915_gem_object_unpin(&obj->base);
3561 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3564 entry->offset = obj->gtt_offset;
3568 i915_gem_object_unpin(object_list[i]);
3573 if (ret != -ENOSPC || retry)
3576 ret = i915_gem_evict_everything(dev);
3584 /* Throttle our rendering by waiting until the ring has completed our requests
3585 * emitted over 20 msec ago.
3587 * Note that if we were to use the current jiffies each time around the loop,
3588 * we wouldn't escape the function with any frames outstanding if the time to
3589 * render a frame was over 20ms.
3591 * This should get us reasonable parallelism between CPU and GPU but also
3592 * relatively low latency when blocking on a particular request to finish.
3595 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3597 struct drm_i915_private *dev_priv = dev->dev_private;
3598 struct drm_i915_file_private *file_priv = file->driver_priv;
3599 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3600 struct drm_i915_gem_request *request;
3601 struct intel_ring_buffer *ring = NULL;
3605 spin_lock(&file_priv->mm.lock);
3606 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3607 if (time_after_eq(request->emitted_jiffies, recent_enough))
3610 ring = request->ring;
3611 seqno = request->seqno;
3613 spin_unlock(&file_priv->mm.lock);
3619 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3620 /* And wait for the seqno passing without holding any locks and
3621 * causing extra latency for others. This is safe as the irq
3622 * generation is designed to be run atomically and so is
3625 ring->user_irq_get(ring);
3626 ret = wait_event_interruptible(ring->irq_queue,
3627 i915_seqno_passed(ring->get_seqno(ring), seqno)
3628 || atomic_read(&dev_priv->mm.wedged));
3629 ring->user_irq_put(ring);
3631 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3636 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3642 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3643 uint64_t exec_offset)
3645 uint32_t exec_start, exec_len;
3647 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3648 exec_len = (uint32_t) exec->batch_len;
3650 if ((exec_start | exec_len) & 0x7)
3660 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3665 for (i = 0; i < count; i++) {
3666 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3667 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
3669 if (!access_ok(VERIFY_READ, ptr, length))
3672 /* we may also need to update the presumed offsets */
3673 if (!access_ok(VERIFY_WRITE, ptr, length))
3676 if (fault_in_pages_readable(ptr, length))
3684 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3685 struct drm_file *file,
3686 struct drm_i915_gem_execbuffer2 *args,
3687 struct drm_i915_gem_exec_object2 *exec_list)
3689 drm_i915_private_t *dev_priv = dev->dev_private;
3690 struct drm_gem_object **object_list = NULL;
3691 struct drm_gem_object *batch_obj;
3692 struct drm_clip_rect *cliprects = NULL;
3693 struct drm_i915_gem_request *request = NULL;
3695 uint64_t exec_offset;
3697 struct intel_ring_buffer *ring = NULL;
3699 ret = i915_gem_check_is_wedged(dev);
3703 ret = validate_exec_list(exec_list, args->buffer_count);
3708 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3709 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3711 switch (args->flags & I915_EXEC_RING_MASK) {
3712 case I915_EXEC_DEFAULT:
3713 case I915_EXEC_RENDER:
3714 ring = &dev_priv->render_ring;
3717 if (!HAS_BSD(dev)) {
3718 DRM_ERROR("execbuf with invalid ring (BSD)\n");
3721 ring = &dev_priv->bsd_ring;
3724 if (!HAS_BLT(dev)) {
3725 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3728 ring = &dev_priv->blt_ring;
3731 DRM_ERROR("execbuf with unknown ring: %d\n",
3732 (int)(args->flags & I915_EXEC_RING_MASK));
3736 if (args->buffer_count < 1) {
3737 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3740 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
3741 if (object_list == NULL) {
3742 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3743 args->buffer_count);
3748 if (args->num_cliprects != 0) {
3749 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3751 if (cliprects == NULL) {
3756 ret = copy_from_user(cliprects,
3757 (struct drm_clip_rect __user *)
3758 (uintptr_t) args->cliprects_ptr,
3759 sizeof(*cliprects) * args->num_cliprects);
3761 DRM_ERROR("copy %d cliprects failed: %d\n",
3762 args->num_cliprects, ret);
3768 request = kzalloc(sizeof(*request), GFP_KERNEL);
3769 if (request == NULL) {
3774 ret = i915_mutex_lock_interruptible(dev);
3778 if (dev_priv->mm.suspended) {
3779 mutex_unlock(&dev->struct_mutex);
3784 /* Look up object handles */
3785 for (i = 0; i < args->buffer_count; i++) {
3786 struct drm_i915_gem_object *obj_priv;
3788 object_list[i] = drm_gem_object_lookup(dev, file,
3789 exec_list[i].handle);
3790 if (object_list[i] == NULL) {
3791 DRM_ERROR("Invalid object handle %d at index %d\n",
3792 exec_list[i].handle, i);
3793 /* prevent error path from reading uninitialized data */
3794 args->buffer_count = i + 1;
3799 obj_priv = to_intel_bo(object_list[i]);
3800 if (obj_priv->in_execbuffer) {
3801 DRM_ERROR("Object %p appears more than once in object list\n",
3803 /* prevent error path from reading uninitialized data */
3804 args->buffer_count = i + 1;
3808 obj_priv->in_execbuffer = true;
3811 /* Move the objects en-masse into the GTT, evicting if necessary. */
3812 ret = i915_gem_execbuffer_pin(dev, file,
3813 object_list, exec_list,
3814 args->buffer_count);
3818 /* The objects are in their final locations, apply the relocations. */
3819 for (i = 0; i < args->buffer_count; i++) {
3820 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3821 obj->base.pending_read_domains = 0;
3822 obj->base.pending_write_domain = 0;
3823 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
3828 /* Set the pending read domains for the batch buffer to COMMAND */
3829 batch_obj = object_list[args->buffer_count-1];
3830 if (batch_obj->pending_write_domain) {
3831 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3835 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
3837 /* Sanity check the batch buffer */
3838 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3839 ret = i915_gem_check_execbuffer(args, exec_offset);
3841 DRM_ERROR("execbuf with invalid offset/length\n");
3845 /* Zero the global flush/invalidate flags. These
3846 * will be modified as new domains are computed
3849 dev->invalidate_domains = 0;
3850 dev->flush_domains = 0;
3851 dev_priv->mm.flush_rings = 0;
3852 for (i = 0; i < args->buffer_count; i++)
3853 i915_gem_object_set_to_gpu_domain(object_list[i], ring);
3855 if (dev->invalidate_domains | dev->flush_domains) {
3857 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3859 dev->invalidate_domains,
3860 dev->flush_domains);
3862 i915_gem_flush(dev, file,
3863 dev->invalidate_domains,
3865 dev_priv->mm.flush_rings);
3869 for (i = 0; i < args->buffer_count; i++) {
3870 i915_gem_object_check_coherency(object_list[i],
3871 exec_list[i].handle);
3876 i915_gem_dump_object(batch_obj,
3882 /* Check for any pending flips. As we only maintain a flip queue depth
3883 * of 1, we can simply insert a WAIT for the next display flip prior
3884 * to executing the batch and avoid stalling the CPU.
3887 for (i = 0; i < args->buffer_count; i++) {
3888 if (object_list[i]->write_domain)
3889 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3892 int plane, flip_mask;
3894 for (plane = 0; flips >> plane; plane++) {
3895 if (((flips >> plane) & 1) == 0)
3899 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3901 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3903 ret = intel_ring_begin(ring, 2);
3907 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3908 intel_ring_emit(ring, MI_NOOP);
3909 intel_ring_advance(ring);
3913 /* Exec the batchbuffer */
3914 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
3916 DRM_ERROR("dispatch failed %d\n", ret);
3920 for (i = 0; i < args->buffer_count; i++) {
3921 struct drm_gem_object *obj = object_list[i];
3923 obj->read_domains = obj->pending_read_domains;
3924 obj->write_domain = obj->pending_write_domain;
3926 i915_gem_object_move_to_active(obj, ring);
3927 if (obj->write_domain) {
3928 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3929 obj_priv->dirty = 1;
3930 list_move_tail(&obj_priv->gpu_write_list,
3931 &ring->gpu_write_list);
3932 intel_mark_busy(dev, obj);
3935 trace_i915_gem_object_change_domain(obj,
3941 * Ensure that the commands in the batch buffer are
3942 * finished before the interrupt fires
3944 i915_retire_commands(dev, ring);
3946 if (i915_add_request(dev, file, request, ring))
3947 ring->outstanding_lazy_request = true;
3952 for (i = 0; i < args->buffer_count; i++) {
3953 if (object_list[i] == NULL)
3956 to_intel_bo(object_list[i])->in_execbuffer = false;
3957 drm_gem_object_unreference(object_list[i]);
3960 mutex_unlock(&dev->struct_mutex);
3963 drm_free_large(object_list);
3971 * Legacy execbuffer just creates an exec2 list from the original exec object
3972 * list array and passes it to the real function.
3975 i915_gem_execbuffer(struct drm_device *dev, void *data,
3976 struct drm_file *file_priv)
3978 struct drm_i915_gem_execbuffer *args = data;
3979 struct drm_i915_gem_execbuffer2 exec2;
3980 struct drm_i915_gem_exec_object *exec_list = NULL;
3981 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3985 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3986 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3989 if (args->buffer_count < 1) {
3990 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3994 /* Copy in the exec list from userland */
3995 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
3996 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
3997 if (exec_list == NULL || exec2_list == NULL) {
3998 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
3999 args->buffer_count);
4000 drm_free_large(exec_list);
4001 drm_free_large(exec2_list);
4004 ret = copy_from_user(exec_list,
4005 (struct drm_i915_relocation_entry __user *)
4006 (uintptr_t) args->buffers_ptr,
4007 sizeof(*exec_list) * args->buffer_count);
4009 DRM_ERROR("copy %d exec entries failed %d\n",
4010 args->buffer_count, ret);
4011 drm_free_large(exec_list);
4012 drm_free_large(exec2_list);
4016 for (i = 0; i < args->buffer_count; i++) {
4017 exec2_list[i].handle = exec_list[i].handle;
4018 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4019 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4020 exec2_list[i].alignment = exec_list[i].alignment;
4021 exec2_list[i].offset = exec_list[i].offset;
4022 if (INTEL_INFO(dev)->gen < 4)
4023 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4025 exec2_list[i].flags = 0;
4028 exec2.buffers_ptr = args->buffers_ptr;
4029 exec2.buffer_count = args->buffer_count;
4030 exec2.batch_start_offset = args->batch_start_offset;
4031 exec2.batch_len = args->batch_len;
4032 exec2.DR1 = args->DR1;
4033 exec2.DR4 = args->DR4;
4034 exec2.num_cliprects = args->num_cliprects;
4035 exec2.cliprects_ptr = args->cliprects_ptr;
4036 exec2.flags = I915_EXEC_RENDER;
4038 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4040 /* Copy the new buffer offsets back to the user's exec list. */
4041 for (i = 0; i < args->buffer_count; i++)
4042 exec_list[i].offset = exec2_list[i].offset;
4043 /* ... and back out to userspace */
4044 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4045 (uintptr_t) args->buffers_ptr,
4047 sizeof(*exec_list) * args->buffer_count);
4050 DRM_ERROR("failed to copy %d exec entries "
4051 "back to user (%d)\n",
4052 args->buffer_count, ret);
4056 drm_free_large(exec_list);
4057 drm_free_large(exec2_list);
4062 i915_gem_execbuffer2(struct drm_device *dev, void *data,
4063 struct drm_file *file_priv)
4065 struct drm_i915_gem_execbuffer2 *args = data;
4066 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4070 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4071 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4074 if (args->buffer_count < 1) {
4075 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4079 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4080 if (exec2_list == NULL) {
4081 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4082 args->buffer_count);
4085 ret = copy_from_user(exec2_list,
4086 (struct drm_i915_relocation_entry __user *)
4087 (uintptr_t) args->buffers_ptr,
4088 sizeof(*exec2_list) * args->buffer_count);
4090 DRM_ERROR("copy %d exec entries failed %d\n",
4091 args->buffer_count, ret);
4092 drm_free_large(exec2_list);
4096 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4098 /* Copy the new buffer offsets back to the user's exec list. */
4099 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4100 (uintptr_t) args->buffers_ptr,
4102 sizeof(*exec2_list) * args->buffer_count);
4105 DRM_ERROR("failed to copy %d exec entries "
4106 "back to user (%d)\n",
4107 args->buffer_count, ret);
4111 drm_free_large(exec2_list);
4116 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
4117 bool mappable, bool need_fence)
4119 struct drm_device *dev = obj->dev;
4120 struct drm_i915_private *dev_priv = dev->dev_private;
4121 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4124 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4125 WARN_ON(i915_verify_lists(dev));
4127 if (obj_priv->gtt_space != NULL) {
4128 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
4129 (need_fence && !obj_priv->fenceable) ||
4130 (mappable && !obj_priv->mappable)) {
4131 WARN(obj_priv->pin_count,
4132 "bo is already pinned with incorrect alignment:"
4133 " offset=%x, req.alignment=%x, need_fence=%d, fenceable=%d, mappable=%d, cpu_accessible=%d\n",
4134 obj_priv->gtt_offset, alignment,
4135 need_fence, obj_priv->fenceable,
4136 mappable, obj_priv->mappable);
4137 ret = i915_gem_object_unbind(obj);
4143 if (obj_priv->gtt_space == NULL) {
4144 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4145 mappable, need_fence);
4150 if (obj_priv->pin_count++ == 0) {
4151 i915_gem_info_add_pin(dev_priv, obj_priv, mappable);
4152 if (!obj_priv->active)
4153 list_move_tail(&obj_priv->mm_list,
4154 &dev_priv->mm.pinned_list);
4156 BUG_ON(!obj_priv->pin_mappable && mappable);
4158 WARN_ON(i915_verify_lists(dev));
4163 i915_gem_object_unpin(struct drm_gem_object *obj)
4165 struct drm_device *dev = obj->dev;
4166 drm_i915_private_t *dev_priv = dev->dev_private;
4167 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4169 WARN_ON(i915_verify_lists(dev));
4170 BUG_ON(obj_priv->pin_count == 0);
4171 BUG_ON(obj_priv->gtt_space == NULL);
4173 if (--obj_priv->pin_count == 0) {
4174 if (!obj_priv->active)
4175 list_move_tail(&obj_priv->mm_list,
4176 &dev_priv->mm.inactive_list);
4177 i915_gem_info_remove_pin(dev_priv, obj_priv);
4179 WARN_ON(i915_verify_lists(dev));
4183 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4184 struct drm_file *file_priv)
4186 struct drm_i915_gem_pin *args = data;
4187 struct drm_gem_object *obj;
4188 struct drm_i915_gem_object *obj_priv;
4191 ret = i915_mutex_lock_interruptible(dev);
4195 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4200 obj_priv = to_intel_bo(obj);
4202 if (obj_priv->madv != I915_MADV_WILLNEED) {
4203 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4208 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4209 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4215 obj_priv->user_pin_count++;
4216 obj_priv->pin_filp = file_priv;
4217 if (obj_priv->user_pin_count == 1) {
4218 ret = i915_gem_object_pin(obj, args->alignment,
4219 true, obj_priv->tiling_mode);
4224 /* XXX - flush the CPU caches for pinned objects
4225 * as the X server doesn't manage domains yet
4227 i915_gem_object_flush_cpu_write_domain(obj);
4228 args->offset = obj_priv->gtt_offset;
4230 drm_gem_object_unreference(obj);
4232 mutex_unlock(&dev->struct_mutex);
4237 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4238 struct drm_file *file_priv)
4240 struct drm_i915_gem_pin *args = data;
4241 struct drm_gem_object *obj;
4242 struct drm_i915_gem_object *obj_priv;
4245 ret = i915_mutex_lock_interruptible(dev);
4249 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4254 obj_priv = to_intel_bo(obj);
4256 if (obj_priv->pin_filp != file_priv) {
4257 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4262 obj_priv->user_pin_count--;
4263 if (obj_priv->user_pin_count == 0) {
4264 obj_priv->pin_filp = NULL;
4265 i915_gem_object_unpin(obj);
4269 drm_gem_object_unreference(obj);
4271 mutex_unlock(&dev->struct_mutex);
4276 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4277 struct drm_file *file_priv)
4279 struct drm_i915_gem_busy *args = data;
4280 struct drm_gem_object *obj;
4281 struct drm_i915_gem_object *obj_priv;
4284 ret = i915_mutex_lock_interruptible(dev);
4288 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4293 obj_priv = to_intel_bo(obj);
4295 /* Count all active objects as busy, even if they are currently not used
4296 * by the gpu. Users of this interface expect objects to eventually
4297 * become non-busy without any further actions, therefore emit any
4298 * necessary flushes here.
4300 args->busy = obj_priv->active;
4302 /* Unconditionally flush objects, even when the gpu still uses this
4303 * object. Userspace calling this function indicates that it wants to
4304 * use this buffer rather sooner than later, so issuing the required
4305 * flush earlier is beneficial.
4307 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4308 i915_gem_flush_ring(dev, file_priv,
4310 0, obj->write_domain);
4312 /* Update the active list for the hardware's current position.
4313 * Otherwise this only updates on a delayed timer or when irqs
4314 * are actually unmasked, and our working set ends up being
4315 * larger than required.
4317 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4319 args->busy = obj_priv->active;
4322 drm_gem_object_unreference(obj);
4324 mutex_unlock(&dev->struct_mutex);
4329 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4330 struct drm_file *file_priv)
4332 return i915_gem_ring_throttle(dev, file_priv);
4336 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4337 struct drm_file *file_priv)
4339 struct drm_i915_gem_madvise *args = data;
4340 struct drm_gem_object *obj;
4341 struct drm_i915_gem_object *obj_priv;
4344 switch (args->madv) {
4345 case I915_MADV_DONTNEED:
4346 case I915_MADV_WILLNEED:
4352 ret = i915_mutex_lock_interruptible(dev);
4356 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4361 obj_priv = to_intel_bo(obj);
4363 if (obj_priv->pin_count) {
4368 if (obj_priv->madv != __I915_MADV_PURGED)
4369 obj_priv->madv = args->madv;
4371 /* if the object is no longer bound, discard its backing storage */
4372 if (i915_gem_object_is_purgeable(obj_priv) &&
4373 obj_priv->gtt_space == NULL)
4374 i915_gem_object_truncate(obj);
4376 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4379 drm_gem_object_unreference(obj);
4381 mutex_unlock(&dev->struct_mutex);
4385 struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4388 struct drm_i915_private *dev_priv = dev->dev_private;
4389 struct drm_i915_gem_object *obj;
4391 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4395 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4400 i915_gem_info_add_obj(dev_priv, size);
4402 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4403 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4405 obj->agp_type = AGP_USER_MEMORY;
4406 obj->base.driver_private = NULL;
4407 obj->fence_reg = I915_FENCE_REG_NONE;
4408 INIT_LIST_HEAD(&obj->mm_list);
4409 INIT_LIST_HEAD(&obj->ring_list);
4410 INIT_LIST_HEAD(&obj->gpu_write_list);
4411 obj->madv = I915_MADV_WILLNEED;
4412 obj->fenceable = true;
4413 obj->mappable = true;
4418 int i915_gem_init_object(struct drm_gem_object *obj)
4425 static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4427 struct drm_device *dev = obj->dev;
4428 drm_i915_private_t *dev_priv = dev->dev_private;
4429 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4432 ret = i915_gem_object_unbind(obj);
4433 if (ret == -ERESTARTSYS) {
4434 list_move(&obj_priv->mm_list,
4435 &dev_priv->mm.deferred_free_list);
4439 if (obj->map_list.map)
4440 i915_gem_free_mmap_offset(obj);
4442 drm_gem_object_release(obj);
4443 i915_gem_info_remove_obj(dev_priv, obj->size);
4445 kfree(obj_priv->page_cpu_valid);
4446 kfree(obj_priv->bit_17);
4450 void i915_gem_free_object(struct drm_gem_object *obj)
4452 struct drm_device *dev = obj->dev;
4453 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4455 trace_i915_gem_object_destroy(obj);
4457 while (obj_priv->pin_count > 0)
4458 i915_gem_object_unpin(obj);
4460 if (obj_priv->phys_obj)
4461 i915_gem_detach_phys_object(dev, obj);
4463 i915_gem_free_object_tail(obj);
4467 i915_gem_idle(struct drm_device *dev)
4469 drm_i915_private_t *dev_priv = dev->dev_private;
4472 mutex_lock(&dev->struct_mutex);
4474 if (dev_priv->mm.suspended) {
4475 mutex_unlock(&dev->struct_mutex);
4479 ret = i915_gpu_idle(dev);
4481 mutex_unlock(&dev->struct_mutex);
4485 /* Under UMS, be paranoid and evict. */
4486 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4487 ret = i915_gem_evict_inactive(dev);
4489 mutex_unlock(&dev->struct_mutex);
4494 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4495 * We need to replace this with a semaphore, or something.
4496 * And not confound mm.suspended!
4498 dev_priv->mm.suspended = 1;
4499 del_timer_sync(&dev_priv->hangcheck_timer);
4501 i915_kernel_lost_context(dev);
4502 i915_gem_cleanup_ringbuffer(dev);
4504 mutex_unlock(&dev->struct_mutex);
4506 /* Cancel the retire work handler, which should be idle now. */
4507 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4513 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4514 * over cache flushing.
4517 i915_gem_init_pipe_control(struct drm_device *dev)
4519 drm_i915_private_t *dev_priv = dev->dev_private;
4520 struct drm_gem_object *obj;
4521 struct drm_i915_gem_object *obj_priv;
4524 obj = i915_gem_alloc_object(dev, 4096);
4526 DRM_ERROR("Failed to allocate seqno page\n");
4530 obj_priv = to_intel_bo(obj);
4531 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4533 ret = i915_gem_object_pin(obj, 4096, true, false);
4537 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4538 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4539 if (dev_priv->seqno_page == NULL)
4542 dev_priv->seqno_obj = obj;
4543 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4548 i915_gem_object_unpin(obj);
4550 drm_gem_object_unreference(obj);
4557 i915_gem_cleanup_pipe_control(struct drm_device *dev)
4559 drm_i915_private_t *dev_priv = dev->dev_private;
4560 struct drm_gem_object *obj;
4561 struct drm_i915_gem_object *obj_priv;
4563 obj = dev_priv->seqno_obj;
4564 obj_priv = to_intel_bo(obj);
4565 kunmap(obj_priv->pages[0]);
4566 i915_gem_object_unpin(obj);
4567 drm_gem_object_unreference(obj);
4568 dev_priv->seqno_obj = NULL;
4570 dev_priv->seqno_page = NULL;
4574 i915_gem_init_ringbuffer(struct drm_device *dev)
4576 drm_i915_private_t *dev_priv = dev->dev_private;
4579 if (HAS_PIPE_CONTROL(dev)) {
4580 ret = i915_gem_init_pipe_control(dev);
4585 ret = intel_init_render_ring_buffer(dev);
4587 goto cleanup_pipe_control;
4590 ret = intel_init_bsd_ring_buffer(dev);
4592 goto cleanup_render_ring;
4596 ret = intel_init_blt_ring_buffer(dev);
4598 goto cleanup_bsd_ring;
4601 dev_priv->next_seqno = 1;
4606 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4607 cleanup_render_ring:
4608 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4609 cleanup_pipe_control:
4610 if (HAS_PIPE_CONTROL(dev))
4611 i915_gem_cleanup_pipe_control(dev);
4616 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4618 drm_i915_private_t *dev_priv = dev->dev_private;
4620 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4621 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4622 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
4623 if (HAS_PIPE_CONTROL(dev))
4624 i915_gem_cleanup_pipe_control(dev);
4628 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4629 struct drm_file *file_priv)
4631 drm_i915_private_t *dev_priv = dev->dev_private;
4634 if (drm_core_check_feature(dev, DRIVER_MODESET))
4637 if (atomic_read(&dev_priv->mm.wedged)) {
4638 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4639 atomic_set(&dev_priv->mm.wedged, 0);
4642 mutex_lock(&dev->struct_mutex);
4643 dev_priv->mm.suspended = 0;
4645 ret = i915_gem_init_ringbuffer(dev);
4647 mutex_unlock(&dev->struct_mutex);
4651 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4652 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
4653 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
4654 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
4655 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4656 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4657 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
4658 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
4659 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
4660 mutex_unlock(&dev->struct_mutex);
4662 ret = drm_irq_install(dev);
4664 goto cleanup_ringbuffer;
4669 mutex_lock(&dev->struct_mutex);
4670 i915_gem_cleanup_ringbuffer(dev);
4671 dev_priv->mm.suspended = 1;
4672 mutex_unlock(&dev->struct_mutex);
4678 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4679 struct drm_file *file_priv)
4681 if (drm_core_check_feature(dev, DRIVER_MODESET))
4684 drm_irq_uninstall(dev);
4685 return i915_gem_idle(dev);
4689 i915_gem_lastclose(struct drm_device *dev)
4693 if (drm_core_check_feature(dev, DRIVER_MODESET))
4696 ret = i915_gem_idle(dev);
4698 DRM_ERROR("failed to idle hardware: %d\n", ret);
4702 init_ring_lists(struct intel_ring_buffer *ring)
4704 INIT_LIST_HEAD(&ring->active_list);
4705 INIT_LIST_HEAD(&ring->request_list);
4706 INIT_LIST_HEAD(&ring->gpu_write_list);
4710 i915_gem_load(struct drm_device *dev)
4713 drm_i915_private_t *dev_priv = dev->dev_private;
4715 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4716 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4717 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4718 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
4719 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4720 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
4721 init_ring_lists(&dev_priv->render_ring);
4722 init_ring_lists(&dev_priv->bsd_ring);
4723 init_ring_lists(&dev_priv->blt_ring);
4724 for (i = 0; i < 16; i++)
4725 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4726 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4727 i915_gem_retire_work_handler);
4728 init_completion(&dev_priv->error_completion);
4730 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4732 u32 tmp = I915_READ(MI_ARB_STATE);
4733 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4734 /* arb state is a masked write, so set bit + bit in mask */
4735 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4736 I915_WRITE(MI_ARB_STATE, tmp);
4740 /* Old X drivers will take 0-2 for front, back, depth buffers */
4741 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4742 dev_priv->fence_reg_start = 3;
4744 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4745 dev_priv->num_fence_regs = 16;
4747 dev_priv->num_fence_regs = 8;
4749 /* Initialize fence registers to zero */
4750 switch (INTEL_INFO(dev)->gen) {
4752 for (i = 0; i < 16; i++)
4753 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4757 for (i = 0; i < 16; i++)
4758 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4761 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4762 for (i = 0; i < 8; i++)
4763 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4765 for (i = 0; i < 8; i++)
4766 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4769 i915_gem_detect_bit_6_swizzle(dev);
4770 init_waitqueue_head(&dev_priv->pending_flip_queue);
4772 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4773 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4774 register_shrinker(&dev_priv->mm.inactive_shrinker);
4778 * Create a physically contiguous memory object for this object
4779 * e.g. for cursor + overlay regs
4781 static int i915_gem_init_phys_object(struct drm_device *dev,
4782 int id, int size, int align)
4784 drm_i915_private_t *dev_priv = dev->dev_private;
4785 struct drm_i915_gem_phys_object *phys_obj;
4788 if (dev_priv->mm.phys_objs[id - 1] || !size)
4791 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4797 phys_obj->handle = drm_pci_alloc(dev, size, align);
4798 if (!phys_obj->handle) {
4803 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4806 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4814 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4816 drm_i915_private_t *dev_priv = dev->dev_private;
4817 struct drm_i915_gem_phys_object *phys_obj;
4819 if (!dev_priv->mm.phys_objs[id - 1])
4822 phys_obj = dev_priv->mm.phys_objs[id - 1];
4823 if (phys_obj->cur_obj) {
4824 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4828 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4830 drm_pci_free(dev, phys_obj->handle);
4832 dev_priv->mm.phys_objs[id - 1] = NULL;
4835 void i915_gem_free_all_phys_object(struct drm_device *dev)
4839 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4840 i915_gem_free_phys_object(dev, i);
4843 void i915_gem_detach_phys_object(struct drm_device *dev,
4844 struct drm_gem_object *obj)
4846 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4847 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4852 if (!obj_priv->phys_obj)
4854 vaddr = obj_priv->phys_obj->handle->vaddr;
4856 page_count = obj->size / PAGE_SIZE;
4858 for (i = 0; i < page_count; i++) {
4859 struct page *page = read_cache_page_gfp(mapping, i,
4860 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4861 if (!IS_ERR(page)) {
4862 char *dst = kmap_atomic(page);
4863 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4866 drm_clflush_pages(&page, 1);
4868 set_page_dirty(page);
4869 mark_page_accessed(page);
4870 page_cache_release(page);
4873 drm_agp_chipset_flush(dev);
4875 obj_priv->phys_obj->cur_obj = NULL;
4876 obj_priv->phys_obj = NULL;
4880 i915_gem_attach_phys_object(struct drm_device *dev,
4881 struct drm_gem_object *obj,
4885 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4886 drm_i915_private_t *dev_priv = dev->dev_private;
4887 struct drm_i915_gem_object *obj_priv;
4892 if (id > I915_MAX_PHYS_OBJECT)
4895 obj_priv = to_intel_bo(obj);
4897 if (obj_priv->phys_obj) {
4898 if (obj_priv->phys_obj->id == id)
4900 i915_gem_detach_phys_object(dev, obj);
4903 /* create a new object */
4904 if (!dev_priv->mm.phys_objs[id - 1]) {
4905 ret = i915_gem_init_phys_object(dev, id,
4908 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
4913 /* bind to the object */
4914 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4915 obj_priv->phys_obj->cur_obj = obj;
4917 page_count = obj->size / PAGE_SIZE;
4919 for (i = 0; i < page_count; i++) {
4923 page = read_cache_page_gfp(mapping, i,
4924 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4926 return PTR_ERR(page);
4928 src = kmap_atomic(obj_priv->pages[i]);
4929 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4930 memcpy(dst, src, PAGE_SIZE);
4933 mark_page_accessed(page);
4934 page_cache_release(page);
4941 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4942 struct drm_i915_gem_pwrite *args,
4943 struct drm_file *file_priv)
4945 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4948 char __user *user_data;
4950 user_data = (char __user *) (uintptr_t) args->data_ptr;
4951 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4953 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
4954 ret = copy_from_user(obj_addr, user_data, args->size);
4958 drm_agp_chipset_flush(dev);
4962 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4964 struct drm_i915_file_private *file_priv = file->driver_priv;
4966 /* Clean up our request list when the client is going away, so that
4967 * later retire_requests won't dereference our soon-to-be-gone
4970 spin_lock(&file_priv->mm.lock);
4971 while (!list_empty(&file_priv->mm.request_list)) {
4972 struct drm_i915_gem_request *request;
4974 request = list_first_entry(&file_priv->mm.request_list,
4975 struct drm_i915_gem_request,
4977 list_del(&request->client_list);
4978 request->file_priv = NULL;
4980 spin_unlock(&file_priv->mm.lock);
4984 i915_gpu_is_active(struct drm_device *dev)
4986 drm_i915_private_t *dev_priv = dev->dev_private;
4989 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4990 list_empty(&dev_priv->mm.active_list);
4992 return !lists_empty;
4996 i915_gem_inactive_shrink(struct shrinker *shrinker,
5000 struct drm_i915_private *dev_priv =
5001 container_of(shrinker,
5002 struct drm_i915_private,
5003 mm.inactive_shrinker);
5004 struct drm_device *dev = dev_priv->dev;
5005 struct drm_i915_gem_object *obj, *next;
5008 if (!mutex_trylock(&dev->struct_mutex))
5011 /* "fast-path" to count number of available objects */
5012 if (nr_to_scan == 0) {
5014 list_for_each_entry(obj,
5015 &dev_priv->mm.inactive_list,
5018 mutex_unlock(&dev->struct_mutex);
5019 return cnt / 100 * sysctl_vfs_cache_pressure;
5023 /* first scan for clean buffers */
5024 i915_gem_retire_requests(dev);
5026 list_for_each_entry_safe(obj, next,
5027 &dev_priv->mm.inactive_list,
5029 if (i915_gem_object_is_purgeable(obj)) {
5030 i915_gem_object_unbind(&obj->base);
5031 if (--nr_to_scan == 0)
5036 /* second pass, evict/count anything still on the inactive list */
5038 list_for_each_entry_safe(obj, next,
5039 &dev_priv->mm.inactive_list,
5042 i915_gem_object_unbind(&obj->base);
5048 if (nr_to_scan && i915_gpu_is_active(dev)) {
5050 * We are desperate for pages, so as a last resort, wait
5051 * for the GPU to finish and discard whatever we can.
5052 * This has a dramatic impact to reduce the number of
5053 * OOM-killer events whilst running the GPU aggressively.
5055 if (i915_gpu_idle(dev) == 0)
5058 mutex_unlock(&dev->struct_mutex);
5059 return cnt / 100 * sysctl_vfs_cache_pressure;