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;
2850 uint32_t old_write_domain;
2852 if ((obj->write_domain & I915_GEM_GPU_DOMAINS) == 0)
2855 /* Queue the GPU write cache flushing we need. */
2856 old_write_domain = obj->write_domain;
2857 i915_gem_flush_ring(dev, NULL,
2858 to_intel_bo(obj)->ring,
2859 0, obj->write_domain);
2860 BUG_ON(obj->write_domain);
2862 trace_i915_gem_object_change_domain(obj,
2869 return i915_gem_object_wait_rendering(obj, true);
2872 /** Flushes the GTT write domain for the object if it's dirty. */
2874 i915_gem_object_flush_gtt_write_domain(struct drm_gem_object *obj)
2876 uint32_t old_write_domain;
2878 if (obj->write_domain != I915_GEM_DOMAIN_GTT)
2881 /* No actual flushing is required for the GTT write domain. Writes
2882 * to it immediately go to main memory as far as we know, so there's
2883 * no chipset flush. It also doesn't land in render cache.
2885 i915_gem_release_mmap(obj);
2887 old_write_domain = obj->write_domain;
2888 obj->write_domain = 0;
2890 trace_i915_gem_object_change_domain(obj,
2895 /** Flushes the CPU write domain for the object if it's dirty. */
2897 i915_gem_object_flush_cpu_write_domain(struct drm_gem_object *obj)
2899 struct drm_device *dev = obj->dev;
2900 uint32_t old_write_domain;
2902 if (obj->write_domain != I915_GEM_DOMAIN_CPU)
2905 i915_gem_clflush_object(obj);
2906 drm_agp_chipset_flush(dev);
2907 old_write_domain = obj->write_domain;
2908 obj->write_domain = 0;
2910 trace_i915_gem_object_change_domain(obj,
2916 * Moves a single object to the GTT read, and possibly write domain.
2918 * This function returns when the move is complete, including waiting on
2922 i915_gem_object_set_to_gtt_domain(struct drm_gem_object *obj, int write)
2924 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2925 uint32_t old_write_domain, old_read_domains;
2928 /* Not valid to be called on unbound objects. */
2929 if (obj_priv->gtt_space == NULL)
2932 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
2936 i915_gem_object_flush_cpu_write_domain(obj);
2939 ret = i915_gem_object_wait_rendering(obj, true);
2944 old_write_domain = obj->write_domain;
2945 old_read_domains = obj->read_domains;
2947 /* It should now be out of any other write domains, and we can update
2948 * the domain values for our changes.
2950 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2951 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2953 obj->read_domains = I915_GEM_DOMAIN_GTT;
2954 obj->write_domain = I915_GEM_DOMAIN_GTT;
2955 obj_priv->dirty = 1;
2958 trace_i915_gem_object_change_domain(obj,
2966 * Prepare buffer for display plane. Use uninterruptible for possible flush
2967 * wait, as in modesetting process we're not supposed to be interrupted.
2970 i915_gem_object_set_to_display_plane(struct drm_gem_object *obj,
2973 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
2974 uint32_t old_read_domains;
2977 /* Not valid to be called on unbound objects. */
2978 if (obj_priv->gtt_space == NULL)
2981 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2985 /* Currently, we are always called from an non-interruptible context. */
2987 ret = i915_gem_object_wait_rendering(obj, false);
2992 i915_gem_object_flush_cpu_write_domain(obj);
2994 old_read_domains = obj->read_domains;
2995 obj->read_domains |= I915_GEM_DOMAIN_GTT;
2997 trace_i915_gem_object_change_domain(obj,
3005 * Moves a single object to the CPU read, and possibly write domain.
3007 * This function returns when the move is complete, including waiting on
3011 i915_gem_object_set_to_cpu_domain(struct drm_gem_object *obj, int write)
3013 uint32_t old_write_domain, old_read_domains;
3016 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3020 i915_gem_object_flush_gtt_write_domain(obj);
3022 /* If we have a partially-valid cache of the object in the CPU,
3023 * finish invalidating it and free the per-page flags.
3025 i915_gem_object_set_to_full_cpu_read_domain(obj);
3028 ret = i915_gem_object_wait_rendering(obj, true);
3033 old_write_domain = obj->write_domain;
3034 old_read_domains = obj->read_domains;
3036 /* Flush the CPU cache if it's still invalid. */
3037 if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3038 i915_gem_clflush_object(obj);
3040 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3043 /* It should now be out of any other write domains, and we can update
3044 * the domain values for our changes.
3046 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3048 /* If we're writing through the CPU, then the GPU read domains will
3049 * need to be invalidated at next use.
3052 obj->read_domains = I915_GEM_DOMAIN_CPU;
3053 obj->write_domain = I915_GEM_DOMAIN_CPU;
3056 trace_i915_gem_object_change_domain(obj,
3064 * Set the next domain for the specified object. This
3065 * may not actually perform the necessary flushing/invaliding though,
3066 * as that may want to be batched with other set_domain operations
3068 * This is (we hope) the only really tricky part of gem. The goal
3069 * is fairly simple -- track which caches hold bits of the object
3070 * and make sure they remain coherent. A few concrete examples may
3071 * help to explain how it works. For shorthand, we use the notation
3072 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3073 * a pair of read and write domain masks.
3075 * Case 1: the batch buffer
3081 * 5. Unmapped from GTT
3084 * Let's take these a step at a time
3087 * Pages allocated from the kernel may still have
3088 * cache contents, so we set them to (CPU, CPU) always.
3089 * 2. Written by CPU (using pwrite)
3090 * The pwrite function calls set_domain (CPU, CPU) and
3091 * this function does nothing (as nothing changes)
3093 * This function asserts that the object is not
3094 * currently in any GPU-based read or write domains
3096 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3097 * As write_domain is zero, this function adds in the
3098 * current read domains (CPU+COMMAND, 0).
3099 * flush_domains is set to CPU.
3100 * invalidate_domains is set to COMMAND
3101 * clflush is run to get data out of the CPU caches
3102 * then i915_dev_set_domain calls i915_gem_flush to
3103 * emit an MI_FLUSH and drm_agp_chipset_flush
3104 * 5. Unmapped from GTT
3105 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3106 * flush_domains and invalidate_domains end up both zero
3107 * so no flushing/invalidating happens
3111 * Case 2: The shared render buffer
3115 * 3. Read/written by GPU
3116 * 4. set_domain to (CPU,CPU)
3117 * 5. Read/written by CPU
3118 * 6. Read/written by GPU
3121 * Same as last example, (CPU, CPU)
3123 * Nothing changes (assertions find that it is not in the GPU)
3124 * 3. Read/written by GPU
3125 * execbuffer calls set_domain (RENDER, RENDER)
3126 * flush_domains gets CPU
3127 * invalidate_domains gets GPU
3129 * MI_FLUSH and drm_agp_chipset_flush
3130 * 4. set_domain (CPU, CPU)
3131 * flush_domains gets GPU
3132 * invalidate_domains gets CPU
3133 * wait_rendering (obj) to make sure all drawing is complete.
3134 * This will include an MI_FLUSH to get the data from GPU
3136 * clflush (obj) to invalidate the CPU cache
3137 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3138 * 5. Read/written by CPU
3139 * cache lines are loaded and dirtied
3140 * 6. Read written by GPU
3141 * Same as last GPU access
3143 * Case 3: The constant buffer
3148 * 4. Updated (written) by CPU again
3157 * flush_domains = CPU
3158 * invalidate_domains = RENDER
3161 * drm_agp_chipset_flush
3162 * 4. Updated (written) by CPU again
3164 * flush_domains = 0 (no previous write domain)
3165 * invalidate_domains = 0 (no new read domains)
3168 * flush_domains = CPU
3169 * invalidate_domains = RENDER
3172 * drm_agp_chipset_flush
3175 i915_gem_object_set_to_gpu_domain(struct drm_gem_object *obj,
3176 struct intel_ring_buffer *ring)
3178 struct drm_device *dev = obj->dev;
3179 struct drm_i915_private *dev_priv = dev->dev_private;
3180 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3181 uint32_t invalidate_domains = 0;
3182 uint32_t flush_domains = 0;
3185 * If the object isn't moving to a new write domain,
3186 * let the object stay in multiple read domains
3188 if (obj->pending_write_domain == 0)
3189 obj->pending_read_domains |= obj->read_domains;
3192 * Flush the current write domain if
3193 * the new read domains don't match. Invalidate
3194 * any read domains which differ from the old
3197 if (obj->write_domain &&
3198 obj->write_domain != obj->pending_read_domains) {
3199 flush_domains |= obj->write_domain;
3200 invalidate_domains |=
3201 obj->pending_read_domains & ~obj->write_domain;
3204 * Invalidate any read caches which may have
3205 * stale data. That is, any new read domains.
3207 invalidate_domains |= obj->pending_read_domains & ~obj->read_domains;
3208 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
3209 i915_gem_clflush_object(obj);
3211 /* blow away mappings if mapped through GTT */
3212 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3213 i915_gem_release_mmap(obj);
3215 /* The actual obj->write_domain will be updated with
3216 * pending_write_domain after we emit the accumulated flush for all
3217 * of our domain changes in execbuffers (which clears objects'
3218 * write_domains). So if we have a current write domain that we
3219 * aren't changing, set pending_write_domain to that.
3221 if (flush_domains == 0 && obj->pending_write_domain == 0)
3222 obj->pending_write_domain = obj->write_domain;
3224 dev->invalidate_domains |= invalidate_domains;
3225 dev->flush_domains |= flush_domains;
3226 if (flush_domains & I915_GEM_GPU_DOMAINS)
3227 dev_priv->mm.flush_rings |= obj_priv->ring->id;
3228 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3229 dev_priv->mm.flush_rings |= ring->id;
3233 * Moves the object from a partially CPU read to a full one.
3235 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3236 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3239 i915_gem_object_set_to_full_cpu_read_domain(struct drm_gem_object *obj)
3241 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3243 if (!obj_priv->page_cpu_valid)
3246 /* If we're partially in the CPU read domain, finish moving it in.
3248 if (obj->read_domains & I915_GEM_DOMAIN_CPU) {
3251 for (i = 0; i <= (obj->size - 1) / PAGE_SIZE; i++) {
3252 if (obj_priv->page_cpu_valid[i])
3254 drm_clflush_pages(obj_priv->pages + i, 1);
3258 /* Free the page_cpu_valid mappings which are now stale, whether
3259 * or not we've got I915_GEM_DOMAIN_CPU.
3261 kfree(obj_priv->page_cpu_valid);
3262 obj_priv->page_cpu_valid = NULL;
3266 * Set the CPU read domain on a range of the object.
3268 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3269 * not entirely valid. The page_cpu_valid member of the object flags which
3270 * pages have been flushed, and will be respected by
3271 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3272 * of the whole object.
3274 * This function returns when the move is complete, including waiting on
3278 i915_gem_object_set_cpu_read_domain_range(struct drm_gem_object *obj,
3279 uint64_t offset, uint64_t size)
3281 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3282 uint32_t old_read_domains;
3285 if (offset == 0 && size == obj->size)
3286 return i915_gem_object_set_to_cpu_domain(obj, 0);
3288 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3291 i915_gem_object_flush_gtt_write_domain(obj);
3293 /* If we're already fully in the CPU read domain, we're done. */
3294 if (obj_priv->page_cpu_valid == NULL &&
3295 (obj->read_domains & I915_GEM_DOMAIN_CPU) != 0)
3298 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3299 * newly adding I915_GEM_DOMAIN_CPU
3301 if (obj_priv->page_cpu_valid == NULL) {
3302 obj_priv->page_cpu_valid = kzalloc(obj->size / PAGE_SIZE,
3304 if (obj_priv->page_cpu_valid == NULL)
3306 } else if ((obj->read_domains & I915_GEM_DOMAIN_CPU) == 0)
3307 memset(obj_priv->page_cpu_valid, 0, obj->size / PAGE_SIZE);
3309 /* Flush the cache on any pages that are still invalid from the CPU's
3312 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3314 if (obj_priv->page_cpu_valid[i])
3317 drm_clflush_pages(obj_priv->pages + i, 1);
3319 obj_priv->page_cpu_valid[i] = 1;
3322 /* It should now be out of any other write domains, and we can update
3323 * the domain values for our changes.
3325 BUG_ON((obj->write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3327 old_read_domains = obj->read_domains;
3328 obj->read_domains |= I915_GEM_DOMAIN_CPU;
3330 trace_i915_gem_object_change_domain(obj,
3338 * Pin an object to the GTT and evaluate the relocations landing in it.
3341 i915_gem_execbuffer_relocate(struct drm_i915_gem_object *obj,
3342 struct drm_file *file_priv,
3343 struct drm_i915_gem_exec_object2 *entry)
3345 struct drm_device *dev = obj->base.dev;
3346 drm_i915_private_t *dev_priv = dev->dev_private;
3347 struct drm_i915_gem_relocation_entry __user *user_relocs;
3348 struct drm_gem_object *target_obj = NULL;
3349 uint32_t target_handle = 0;
3352 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3353 for (i = 0; i < entry->relocation_count; i++) {
3354 struct drm_i915_gem_relocation_entry reloc;
3355 uint32_t target_offset;
3357 if (__copy_from_user_inatomic(&reloc,
3364 if (reloc.target_handle != target_handle) {
3365 drm_gem_object_unreference(target_obj);
3367 target_obj = drm_gem_object_lookup(dev, file_priv,
3368 reloc.target_handle);
3369 if (target_obj == NULL) {
3374 target_handle = reloc.target_handle;
3376 target_offset = to_intel_bo(target_obj)->gtt_offset;
3379 DRM_INFO("%s: obj %p offset %08x target %d "
3380 "read %08x write %08x gtt %08x "
3381 "presumed %08x delta %08x\n",
3385 (int) reloc.target_handle,
3386 (int) reloc.read_domains,
3387 (int) reloc.write_domain,
3388 (int) target_offset,
3389 (int) reloc.presumed_offset,
3393 /* The target buffer should have appeared before us in the
3394 * exec_object list, so it should have a GTT space bound by now.
3396 if (target_offset == 0) {
3397 DRM_ERROR("No GTT space found for object %d\n",
3398 reloc.target_handle);
3403 /* Validate that the target is in a valid r/w GPU domain */
3404 if (reloc.write_domain & (reloc.write_domain - 1)) {
3405 DRM_ERROR("reloc with multiple write domains: "
3406 "obj %p target %d offset %d "
3407 "read %08x write %08x",
3408 obj, reloc.target_handle,
3411 reloc.write_domain);
3415 if (reloc.write_domain & I915_GEM_DOMAIN_CPU ||
3416 reloc.read_domains & I915_GEM_DOMAIN_CPU) {
3417 DRM_ERROR("reloc with read/write CPU domains: "
3418 "obj %p target %d offset %d "
3419 "read %08x write %08x",
3420 obj, reloc.target_handle,
3423 reloc.write_domain);
3427 if (reloc.write_domain && target_obj->pending_write_domain &&
3428 reloc.write_domain != target_obj->pending_write_domain) {
3429 DRM_ERROR("Write domain conflict: "
3430 "obj %p target %d offset %d "
3431 "new %08x old %08x\n",
3432 obj, reloc.target_handle,
3435 target_obj->pending_write_domain);
3440 target_obj->pending_read_domains |= reloc.read_domains;
3441 target_obj->pending_write_domain |= reloc.write_domain;
3443 /* If the relocation already has the right value in it, no
3444 * more work needs to be done.
3446 if (target_offset == reloc.presumed_offset)
3449 /* Check that the relocation address is valid... */
3450 if (reloc.offset > obj->base.size - 4) {
3451 DRM_ERROR("Relocation beyond object bounds: "
3452 "obj %p target %d offset %d size %d.\n",
3453 obj, reloc.target_handle,
3454 (int) reloc.offset, (int) obj->base.size);
3458 if (reloc.offset & 3) {
3459 DRM_ERROR("Relocation not 4-byte aligned: "
3460 "obj %p target %d offset %d.\n",
3461 obj, reloc.target_handle,
3462 (int) reloc.offset);
3467 /* and points to somewhere within the target object. */
3468 if (reloc.delta >= target_obj->size) {
3469 DRM_ERROR("Relocation beyond target object bounds: "
3470 "obj %p target %d delta %d size %d.\n",
3471 obj, reloc.target_handle,
3472 (int) reloc.delta, (int) target_obj->size);
3477 reloc.delta += target_offset;
3478 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3479 uint32_t page_offset = reloc.offset & ~PAGE_MASK;
3482 vaddr = kmap_atomic(obj->pages[reloc.offset >> PAGE_SHIFT]);
3483 *(uint32_t *)(vaddr + page_offset) = reloc.delta;
3484 kunmap_atomic(vaddr);
3486 uint32_t __iomem *reloc_entry;
3487 void __iomem *reloc_page;
3489 ret = i915_gem_object_set_to_gtt_domain(&obj->base, 1);
3493 /* Map the page containing the relocation we're going to perform. */
3494 reloc.offset += obj->gtt_offset;
3495 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3496 reloc.offset & PAGE_MASK);
3497 reloc_entry = (uint32_t __iomem *)
3498 (reloc_page + (reloc.offset & ~PAGE_MASK));
3499 iowrite32(reloc.delta, reloc_entry);
3500 io_mapping_unmap_atomic(reloc_page);
3503 /* and update the user's relocation entry */
3504 reloc.presumed_offset = target_offset;
3505 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3506 &reloc.presumed_offset,
3507 sizeof(reloc.presumed_offset))) {
3513 drm_gem_object_unreference(target_obj);
3518 i915_gem_execbuffer_pin(struct drm_device *dev,
3519 struct drm_file *file,
3520 struct drm_gem_object **object_list,
3521 struct drm_i915_gem_exec_object2 *exec_list,
3524 struct drm_i915_private *dev_priv = dev->dev_private;
3527 /* attempt to pin all of the buffers into the GTT */
3528 for (retry = 0; retry < 2; retry++) {
3530 for (i = 0; i < count; i++) {
3531 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3532 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3534 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3535 obj->tiling_mode != I915_TILING_NONE;
3537 /* g33/pnv can't fence buffers in the unmappable part */
3538 bool need_mappable =
3539 entry->relocation_count ? true : need_fence;
3541 /* Check fence reg constraints and rebind if necessary */
3542 if ((need_fence && !obj->fenceable) ||
3543 (need_mappable && !obj->mappable)) {
3544 ret = i915_gem_object_unbind(&obj->base);
3549 ret = i915_gem_object_pin(&obj->base,
3557 * Pre-965 chips need a fence register set up in order
3558 * to properly handle blits to/from tiled surfaces.
3561 ret = i915_gem_object_get_fence_reg(&obj->base, true);
3563 i915_gem_object_unpin(&obj->base);
3567 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3570 entry->offset = obj->gtt_offset;
3574 i915_gem_object_unpin(object_list[i]);
3579 if (ret != -ENOSPC || retry)
3582 ret = i915_gem_evict_everything(dev);
3590 /* Throttle our rendering by waiting until the ring has completed our requests
3591 * emitted over 20 msec ago.
3593 * Note that if we were to use the current jiffies each time around the loop,
3594 * we wouldn't escape the function with any frames outstanding if the time to
3595 * render a frame was over 20ms.
3597 * This should get us reasonable parallelism between CPU and GPU but also
3598 * relatively low latency when blocking on a particular request to finish.
3601 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3603 struct drm_i915_private *dev_priv = dev->dev_private;
3604 struct drm_i915_file_private *file_priv = file->driver_priv;
3605 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3606 struct drm_i915_gem_request *request;
3607 struct intel_ring_buffer *ring = NULL;
3611 spin_lock(&file_priv->mm.lock);
3612 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3613 if (time_after_eq(request->emitted_jiffies, recent_enough))
3616 ring = request->ring;
3617 seqno = request->seqno;
3619 spin_unlock(&file_priv->mm.lock);
3625 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3626 /* And wait for the seqno passing without holding any locks and
3627 * causing extra latency for others. This is safe as the irq
3628 * generation is designed to be run atomically and so is
3631 ring->user_irq_get(ring);
3632 ret = wait_event_interruptible(ring->irq_queue,
3633 i915_seqno_passed(ring->get_seqno(ring), seqno)
3634 || atomic_read(&dev_priv->mm.wedged));
3635 ring->user_irq_put(ring);
3637 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3642 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3648 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3649 uint64_t exec_offset)
3651 uint32_t exec_start, exec_len;
3653 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3654 exec_len = (uint32_t) exec->batch_len;
3656 if ((exec_start | exec_len) & 0x7)
3666 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3671 for (i = 0; i < count; i++) {
3672 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3673 size_t length = exec[i].relocation_count * sizeof(struct drm_i915_gem_relocation_entry);
3675 if (!access_ok(VERIFY_READ, ptr, length))
3678 /* we may also need to update the presumed offsets */
3679 if (!access_ok(VERIFY_WRITE, ptr, length))
3682 if (fault_in_pages_readable(ptr, length))
3690 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3691 struct drm_file *file,
3692 struct drm_i915_gem_execbuffer2 *args,
3693 struct drm_i915_gem_exec_object2 *exec_list)
3695 drm_i915_private_t *dev_priv = dev->dev_private;
3696 struct drm_gem_object **object_list = NULL;
3697 struct drm_gem_object *batch_obj;
3698 struct drm_clip_rect *cliprects = NULL;
3699 struct drm_i915_gem_request *request = NULL;
3701 uint64_t exec_offset;
3703 struct intel_ring_buffer *ring = NULL;
3705 ret = i915_gem_check_is_wedged(dev);
3709 ret = validate_exec_list(exec_list, args->buffer_count);
3714 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3715 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3717 switch (args->flags & I915_EXEC_RING_MASK) {
3718 case I915_EXEC_DEFAULT:
3719 case I915_EXEC_RENDER:
3720 ring = &dev_priv->render_ring;
3723 if (!HAS_BSD(dev)) {
3724 DRM_ERROR("execbuf with invalid ring (BSD)\n");
3727 ring = &dev_priv->bsd_ring;
3730 if (!HAS_BLT(dev)) {
3731 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3734 ring = &dev_priv->blt_ring;
3737 DRM_ERROR("execbuf with unknown ring: %d\n",
3738 (int)(args->flags & I915_EXEC_RING_MASK));
3742 if (args->buffer_count < 1) {
3743 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3746 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
3747 if (object_list == NULL) {
3748 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3749 args->buffer_count);
3754 if (args->num_cliprects != 0) {
3755 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3757 if (cliprects == NULL) {
3762 ret = copy_from_user(cliprects,
3763 (struct drm_clip_rect __user *)
3764 (uintptr_t) args->cliprects_ptr,
3765 sizeof(*cliprects) * args->num_cliprects);
3767 DRM_ERROR("copy %d cliprects failed: %d\n",
3768 args->num_cliprects, ret);
3774 request = kzalloc(sizeof(*request), GFP_KERNEL);
3775 if (request == NULL) {
3780 ret = i915_mutex_lock_interruptible(dev);
3784 if (dev_priv->mm.suspended) {
3785 mutex_unlock(&dev->struct_mutex);
3790 /* Look up object handles */
3791 for (i = 0; i < args->buffer_count; i++) {
3792 struct drm_i915_gem_object *obj_priv;
3794 object_list[i] = drm_gem_object_lookup(dev, file,
3795 exec_list[i].handle);
3796 if (object_list[i] == NULL) {
3797 DRM_ERROR("Invalid object handle %d at index %d\n",
3798 exec_list[i].handle, i);
3799 /* prevent error path from reading uninitialized data */
3800 args->buffer_count = i + 1;
3805 obj_priv = to_intel_bo(object_list[i]);
3806 if (obj_priv->in_execbuffer) {
3807 DRM_ERROR("Object %p appears more than once in object list\n",
3809 /* prevent error path from reading uninitialized data */
3810 args->buffer_count = i + 1;
3814 obj_priv->in_execbuffer = true;
3817 /* Move the objects en-masse into the GTT, evicting if necessary. */
3818 ret = i915_gem_execbuffer_pin(dev, file,
3819 object_list, exec_list,
3820 args->buffer_count);
3824 /* The objects are in their final locations, apply the relocations. */
3825 for (i = 0; i < args->buffer_count; i++) {
3826 struct drm_i915_gem_object *obj = to_intel_bo(object_list[i]);
3827 obj->base.pending_read_domains = 0;
3828 obj->base.pending_write_domain = 0;
3829 ret = i915_gem_execbuffer_relocate(obj, file, &exec_list[i]);
3834 /* Set the pending read domains for the batch buffer to COMMAND */
3835 batch_obj = object_list[args->buffer_count-1];
3836 if (batch_obj->pending_write_domain) {
3837 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3841 batch_obj->pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
3843 /* Sanity check the batch buffer */
3844 exec_offset = to_intel_bo(batch_obj)->gtt_offset;
3845 ret = i915_gem_check_execbuffer(args, exec_offset);
3847 DRM_ERROR("execbuf with invalid offset/length\n");
3851 /* Zero the global flush/invalidate flags. These
3852 * will be modified as new domains are computed
3855 dev->invalidate_domains = 0;
3856 dev->flush_domains = 0;
3857 dev_priv->mm.flush_rings = 0;
3858 for (i = 0; i < args->buffer_count; i++)
3859 i915_gem_object_set_to_gpu_domain(object_list[i], ring);
3861 if (dev->invalidate_domains | dev->flush_domains) {
3863 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3865 dev->invalidate_domains,
3866 dev->flush_domains);
3868 i915_gem_flush(dev, file,
3869 dev->invalidate_domains,
3871 dev_priv->mm.flush_rings);
3875 for (i = 0; i < args->buffer_count; i++) {
3876 i915_gem_object_check_coherency(object_list[i],
3877 exec_list[i].handle);
3882 i915_gem_dump_object(batch_obj,
3888 /* Check for any pending flips. As we only maintain a flip queue depth
3889 * of 1, we can simply insert a WAIT for the next display flip prior
3890 * to executing the batch and avoid stalling the CPU.
3893 for (i = 0; i < args->buffer_count; i++) {
3894 if (object_list[i]->write_domain)
3895 flips |= atomic_read(&to_intel_bo(object_list[i])->pending_flip);
3898 int plane, flip_mask;
3900 for (plane = 0; flips >> plane; plane++) {
3901 if (((flips >> plane) & 1) == 0)
3905 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
3907 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
3909 ret = intel_ring_begin(ring, 2);
3913 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
3914 intel_ring_emit(ring, MI_NOOP);
3915 intel_ring_advance(ring);
3919 /* Exec the batchbuffer */
3920 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
3922 DRM_ERROR("dispatch failed %d\n", ret);
3926 for (i = 0; i < args->buffer_count; i++) {
3927 struct drm_gem_object *obj = object_list[i];
3929 obj->read_domains = obj->pending_read_domains;
3930 obj->write_domain = obj->pending_write_domain;
3932 i915_gem_object_move_to_active(obj, ring);
3933 if (obj->write_domain) {
3934 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
3935 obj_priv->dirty = 1;
3936 list_move_tail(&obj_priv->gpu_write_list,
3937 &ring->gpu_write_list);
3938 intel_mark_busy(dev, obj);
3941 trace_i915_gem_object_change_domain(obj,
3947 * Ensure that the commands in the batch buffer are
3948 * finished before the interrupt fires
3950 i915_retire_commands(dev, ring);
3952 if (i915_add_request(dev, file, request, ring))
3953 ring->outstanding_lazy_request = true;
3958 for (i = 0; i < args->buffer_count; i++) {
3959 if (object_list[i] == NULL)
3962 to_intel_bo(object_list[i])->in_execbuffer = false;
3963 drm_gem_object_unreference(object_list[i]);
3966 mutex_unlock(&dev->struct_mutex);
3969 drm_free_large(object_list);
3977 * Legacy execbuffer just creates an exec2 list from the original exec object
3978 * list array and passes it to the real function.
3981 i915_gem_execbuffer(struct drm_device *dev, void *data,
3982 struct drm_file *file_priv)
3984 struct drm_i915_gem_execbuffer *args = data;
3985 struct drm_i915_gem_execbuffer2 exec2;
3986 struct drm_i915_gem_exec_object *exec_list = NULL;
3987 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
3991 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3992 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3995 if (args->buffer_count < 1) {
3996 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4000 /* Copy in the exec list from userland */
4001 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4002 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4003 if (exec_list == NULL || exec2_list == NULL) {
4004 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4005 args->buffer_count);
4006 drm_free_large(exec_list);
4007 drm_free_large(exec2_list);
4010 ret = copy_from_user(exec_list,
4011 (struct drm_i915_relocation_entry __user *)
4012 (uintptr_t) args->buffers_ptr,
4013 sizeof(*exec_list) * args->buffer_count);
4015 DRM_ERROR("copy %d exec entries failed %d\n",
4016 args->buffer_count, ret);
4017 drm_free_large(exec_list);
4018 drm_free_large(exec2_list);
4022 for (i = 0; i < args->buffer_count; i++) {
4023 exec2_list[i].handle = exec_list[i].handle;
4024 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4025 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4026 exec2_list[i].alignment = exec_list[i].alignment;
4027 exec2_list[i].offset = exec_list[i].offset;
4028 if (INTEL_INFO(dev)->gen < 4)
4029 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4031 exec2_list[i].flags = 0;
4034 exec2.buffers_ptr = args->buffers_ptr;
4035 exec2.buffer_count = args->buffer_count;
4036 exec2.batch_start_offset = args->batch_start_offset;
4037 exec2.batch_len = args->batch_len;
4038 exec2.DR1 = args->DR1;
4039 exec2.DR4 = args->DR4;
4040 exec2.num_cliprects = args->num_cliprects;
4041 exec2.cliprects_ptr = args->cliprects_ptr;
4042 exec2.flags = I915_EXEC_RENDER;
4044 ret = i915_gem_do_execbuffer(dev, data, file_priv, &exec2, exec2_list);
4046 /* Copy the new buffer offsets back to the user's exec list. */
4047 for (i = 0; i < args->buffer_count; i++)
4048 exec_list[i].offset = exec2_list[i].offset;
4049 /* ... and back out to userspace */
4050 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4051 (uintptr_t) args->buffers_ptr,
4053 sizeof(*exec_list) * args->buffer_count);
4056 DRM_ERROR("failed to copy %d exec entries "
4057 "back to user (%d)\n",
4058 args->buffer_count, ret);
4062 drm_free_large(exec_list);
4063 drm_free_large(exec2_list);
4068 i915_gem_execbuffer2(struct drm_device *dev, void *data,
4069 struct drm_file *file_priv)
4071 struct drm_i915_gem_execbuffer2 *args = data;
4072 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4076 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4077 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4080 if (args->buffer_count < 1) {
4081 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4085 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4086 if (exec2_list == NULL) {
4087 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4088 args->buffer_count);
4091 ret = copy_from_user(exec2_list,
4092 (struct drm_i915_relocation_entry __user *)
4093 (uintptr_t) args->buffers_ptr,
4094 sizeof(*exec2_list) * args->buffer_count);
4096 DRM_ERROR("copy %d exec entries failed %d\n",
4097 args->buffer_count, ret);
4098 drm_free_large(exec2_list);
4102 ret = i915_gem_do_execbuffer(dev, data, file_priv, args, exec2_list);
4104 /* Copy the new buffer offsets back to the user's exec list. */
4105 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4106 (uintptr_t) args->buffers_ptr,
4108 sizeof(*exec2_list) * args->buffer_count);
4111 DRM_ERROR("failed to copy %d exec entries "
4112 "back to user (%d)\n",
4113 args->buffer_count, ret);
4117 drm_free_large(exec2_list);
4122 i915_gem_object_pin(struct drm_gem_object *obj, uint32_t alignment,
4123 bool mappable, bool need_fence)
4125 struct drm_device *dev = obj->dev;
4126 struct drm_i915_private *dev_priv = dev->dev_private;
4127 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4130 BUG_ON(obj_priv->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4131 WARN_ON(i915_verify_lists(dev));
4133 if (obj_priv->gtt_space != NULL) {
4134 if ((alignment && obj_priv->gtt_offset & (alignment - 1)) ||
4135 (need_fence && !obj_priv->fenceable) ||
4136 (mappable && !obj_priv->mappable)) {
4137 WARN(obj_priv->pin_count,
4138 "bo is already pinned with incorrect alignment:"
4139 " offset=%x, req.alignment=%x, need_fence=%d, fenceable=%d, mappable=%d, cpu_accessible=%d\n",
4140 obj_priv->gtt_offset, alignment,
4141 need_fence, obj_priv->fenceable,
4142 mappable, obj_priv->mappable);
4143 ret = i915_gem_object_unbind(obj);
4149 if (obj_priv->gtt_space == NULL) {
4150 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4151 mappable, need_fence);
4156 if (obj_priv->pin_count++ == 0) {
4157 i915_gem_info_add_pin(dev_priv, obj_priv, mappable);
4158 if (!obj_priv->active)
4159 list_move_tail(&obj_priv->mm_list,
4160 &dev_priv->mm.pinned_list);
4162 BUG_ON(!obj_priv->pin_mappable && mappable);
4164 WARN_ON(i915_verify_lists(dev));
4169 i915_gem_object_unpin(struct drm_gem_object *obj)
4171 struct drm_device *dev = obj->dev;
4172 drm_i915_private_t *dev_priv = dev->dev_private;
4173 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4175 WARN_ON(i915_verify_lists(dev));
4176 BUG_ON(obj_priv->pin_count == 0);
4177 BUG_ON(obj_priv->gtt_space == NULL);
4179 if (--obj_priv->pin_count == 0) {
4180 if (!obj_priv->active)
4181 list_move_tail(&obj_priv->mm_list,
4182 &dev_priv->mm.inactive_list);
4183 i915_gem_info_remove_pin(dev_priv, obj_priv);
4185 WARN_ON(i915_verify_lists(dev));
4189 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4190 struct drm_file *file_priv)
4192 struct drm_i915_gem_pin *args = data;
4193 struct drm_gem_object *obj;
4194 struct drm_i915_gem_object *obj_priv;
4197 ret = i915_mutex_lock_interruptible(dev);
4201 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4206 obj_priv = to_intel_bo(obj);
4208 if (obj_priv->madv != I915_MADV_WILLNEED) {
4209 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4214 if (obj_priv->pin_filp != NULL && obj_priv->pin_filp != file_priv) {
4215 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4221 obj_priv->user_pin_count++;
4222 obj_priv->pin_filp = file_priv;
4223 if (obj_priv->user_pin_count == 1) {
4224 ret = i915_gem_object_pin(obj, args->alignment,
4225 true, obj_priv->tiling_mode);
4230 /* XXX - flush the CPU caches for pinned objects
4231 * as the X server doesn't manage domains yet
4233 i915_gem_object_flush_cpu_write_domain(obj);
4234 args->offset = obj_priv->gtt_offset;
4236 drm_gem_object_unreference(obj);
4238 mutex_unlock(&dev->struct_mutex);
4243 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4244 struct drm_file *file_priv)
4246 struct drm_i915_gem_pin *args = data;
4247 struct drm_gem_object *obj;
4248 struct drm_i915_gem_object *obj_priv;
4251 ret = i915_mutex_lock_interruptible(dev);
4255 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4260 obj_priv = to_intel_bo(obj);
4262 if (obj_priv->pin_filp != file_priv) {
4263 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4268 obj_priv->user_pin_count--;
4269 if (obj_priv->user_pin_count == 0) {
4270 obj_priv->pin_filp = NULL;
4271 i915_gem_object_unpin(obj);
4275 drm_gem_object_unreference(obj);
4277 mutex_unlock(&dev->struct_mutex);
4282 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4283 struct drm_file *file_priv)
4285 struct drm_i915_gem_busy *args = data;
4286 struct drm_gem_object *obj;
4287 struct drm_i915_gem_object *obj_priv;
4290 ret = i915_mutex_lock_interruptible(dev);
4294 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4299 obj_priv = to_intel_bo(obj);
4301 /* Count all active objects as busy, even if they are currently not used
4302 * by the gpu. Users of this interface expect objects to eventually
4303 * become non-busy without any further actions, therefore emit any
4304 * necessary flushes here.
4306 args->busy = obj_priv->active;
4308 /* Unconditionally flush objects, even when the gpu still uses this
4309 * object. Userspace calling this function indicates that it wants to
4310 * use this buffer rather sooner than later, so issuing the required
4311 * flush earlier is beneficial.
4313 if (obj->write_domain & I915_GEM_GPU_DOMAINS)
4314 i915_gem_flush_ring(dev, file_priv,
4316 0, obj->write_domain);
4318 /* Update the active list for the hardware's current position.
4319 * Otherwise this only updates on a delayed timer or when irqs
4320 * are actually unmasked, and our working set ends up being
4321 * larger than required.
4323 i915_gem_retire_requests_ring(dev, obj_priv->ring);
4325 args->busy = obj_priv->active;
4328 drm_gem_object_unreference(obj);
4330 mutex_unlock(&dev->struct_mutex);
4335 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4336 struct drm_file *file_priv)
4338 return i915_gem_ring_throttle(dev, file_priv);
4342 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4343 struct drm_file *file_priv)
4345 struct drm_i915_gem_madvise *args = data;
4346 struct drm_gem_object *obj;
4347 struct drm_i915_gem_object *obj_priv;
4350 switch (args->madv) {
4351 case I915_MADV_DONTNEED:
4352 case I915_MADV_WILLNEED:
4358 ret = i915_mutex_lock_interruptible(dev);
4362 obj = drm_gem_object_lookup(dev, file_priv, args->handle);
4367 obj_priv = to_intel_bo(obj);
4369 if (obj_priv->pin_count) {
4374 if (obj_priv->madv != __I915_MADV_PURGED)
4375 obj_priv->madv = args->madv;
4377 /* if the object is no longer bound, discard its backing storage */
4378 if (i915_gem_object_is_purgeable(obj_priv) &&
4379 obj_priv->gtt_space == NULL)
4380 i915_gem_object_truncate(obj);
4382 args->retained = obj_priv->madv != __I915_MADV_PURGED;
4385 drm_gem_object_unreference(obj);
4387 mutex_unlock(&dev->struct_mutex);
4391 struct drm_gem_object * i915_gem_alloc_object(struct drm_device *dev,
4394 struct drm_i915_private *dev_priv = dev->dev_private;
4395 struct drm_i915_gem_object *obj;
4397 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4401 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4406 i915_gem_info_add_obj(dev_priv, size);
4408 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4409 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4411 obj->agp_type = AGP_USER_MEMORY;
4412 obj->base.driver_private = NULL;
4413 obj->fence_reg = I915_FENCE_REG_NONE;
4414 INIT_LIST_HEAD(&obj->mm_list);
4415 INIT_LIST_HEAD(&obj->ring_list);
4416 INIT_LIST_HEAD(&obj->gpu_write_list);
4417 obj->madv = I915_MADV_WILLNEED;
4418 obj->fenceable = true;
4419 obj->mappable = true;
4424 int i915_gem_init_object(struct drm_gem_object *obj)
4431 static void i915_gem_free_object_tail(struct drm_gem_object *obj)
4433 struct drm_device *dev = obj->dev;
4434 drm_i915_private_t *dev_priv = dev->dev_private;
4435 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4438 ret = i915_gem_object_unbind(obj);
4439 if (ret == -ERESTARTSYS) {
4440 list_move(&obj_priv->mm_list,
4441 &dev_priv->mm.deferred_free_list);
4445 if (obj->map_list.map)
4446 i915_gem_free_mmap_offset(obj);
4448 drm_gem_object_release(obj);
4449 i915_gem_info_remove_obj(dev_priv, obj->size);
4451 kfree(obj_priv->page_cpu_valid);
4452 kfree(obj_priv->bit_17);
4456 void i915_gem_free_object(struct drm_gem_object *obj)
4458 struct drm_device *dev = obj->dev;
4459 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4461 trace_i915_gem_object_destroy(obj);
4463 while (obj_priv->pin_count > 0)
4464 i915_gem_object_unpin(obj);
4466 if (obj_priv->phys_obj)
4467 i915_gem_detach_phys_object(dev, obj);
4469 i915_gem_free_object_tail(obj);
4473 i915_gem_idle(struct drm_device *dev)
4475 drm_i915_private_t *dev_priv = dev->dev_private;
4478 mutex_lock(&dev->struct_mutex);
4480 if (dev_priv->mm.suspended) {
4481 mutex_unlock(&dev->struct_mutex);
4485 ret = i915_gpu_idle(dev);
4487 mutex_unlock(&dev->struct_mutex);
4491 /* Under UMS, be paranoid and evict. */
4492 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4493 ret = i915_gem_evict_inactive(dev);
4495 mutex_unlock(&dev->struct_mutex);
4500 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4501 * We need to replace this with a semaphore, or something.
4502 * And not confound mm.suspended!
4504 dev_priv->mm.suspended = 1;
4505 del_timer_sync(&dev_priv->hangcheck_timer);
4507 i915_kernel_lost_context(dev);
4508 i915_gem_cleanup_ringbuffer(dev);
4510 mutex_unlock(&dev->struct_mutex);
4512 /* Cancel the retire work handler, which should be idle now. */
4513 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4519 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4520 * over cache flushing.
4523 i915_gem_init_pipe_control(struct drm_device *dev)
4525 drm_i915_private_t *dev_priv = dev->dev_private;
4526 struct drm_gem_object *obj;
4527 struct drm_i915_gem_object *obj_priv;
4530 obj = i915_gem_alloc_object(dev, 4096);
4532 DRM_ERROR("Failed to allocate seqno page\n");
4536 obj_priv = to_intel_bo(obj);
4537 obj_priv->agp_type = AGP_USER_CACHED_MEMORY;
4539 ret = i915_gem_object_pin(obj, 4096, true, false);
4543 dev_priv->seqno_gfx_addr = obj_priv->gtt_offset;
4544 dev_priv->seqno_page = kmap(obj_priv->pages[0]);
4545 if (dev_priv->seqno_page == NULL)
4548 dev_priv->seqno_obj = obj;
4549 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4554 i915_gem_object_unpin(obj);
4556 drm_gem_object_unreference(obj);
4563 i915_gem_cleanup_pipe_control(struct drm_device *dev)
4565 drm_i915_private_t *dev_priv = dev->dev_private;
4566 struct drm_gem_object *obj;
4567 struct drm_i915_gem_object *obj_priv;
4569 obj = dev_priv->seqno_obj;
4570 obj_priv = to_intel_bo(obj);
4571 kunmap(obj_priv->pages[0]);
4572 i915_gem_object_unpin(obj);
4573 drm_gem_object_unreference(obj);
4574 dev_priv->seqno_obj = NULL;
4576 dev_priv->seqno_page = NULL;
4580 i915_gem_init_ringbuffer(struct drm_device *dev)
4582 drm_i915_private_t *dev_priv = dev->dev_private;
4585 if (HAS_PIPE_CONTROL(dev)) {
4586 ret = i915_gem_init_pipe_control(dev);
4591 ret = intel_init_render_ring_buffer(dev);
4593 goto cleanup_pipe_control;
4596 ret = intel_init_bsd_ring_buffer(dev);
4598 goto cleanup_render_ring;
4602 ret = intel_init_blt_ring_buffer(dev);
4604 goto cleanup_bsd_ring;
4607 dev_priv->next_seqno = 1;
4612 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4613 cleanup_render_ring:
4614 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4615 cleanup_pipe_control:
4616 if (HAS_PIPE_CONTROL(dev))
4617 i915_gem_cleanup_pipe_control(dev);
4622 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4624 drm_i915_private_t *dev_priv = dev->dev_private;
4626 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4627 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4628 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
4629 if (HAS_PIPE_CONTROL(dev))
4630 i915_gem_cleanup_pipe_control(dev);
4634 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4635 struct drm_file *file_priv)
4637 drm_i915_private_t *dev_priv = dev->dev_private;
4640 if (drm_core_check_feature(dev, DRIVER_MODESET))
4643 if (atomic_read(&dev_priv->mm.wedged)) {
4644 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4645 atomic_set(&dev_priv->mm.wedged, 0);
4648 mutex_lock(&dev->struct_mutex);
4649 dev_priv->mm.suspended = 0;
4651 ret = i915_gem_init_ringbuffer(dev);
4653 mutex_unlock(&dev->struct_mutex);
4657 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4658 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
4659 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
4660 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
4661 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4662 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4663 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
4664 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
4665 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
4666 mutex_unlock(&dev->struct_mutex);
4668 ret = drm_irq_install(dev);
4670 goto cleanup_ringbuffer;
4675 mutex_lock(&dev->struct_mutex);
4676 i915_gem_cleanup_ringbuffer(dev);
4677 dev_priv->mm.suspended = 1;
4678 mutex_unlock(&dev->struct_mutex);
4684 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4685 struct drm_file *file_priv)
4687 if (drm_core_check_feature(dev, DRIVER_MODESET))
4690 drm_irq_uninstall(dev);
4691 return i915_gem_idle(dev);
4695 i915_gem_lastclose(struct drm_device *dev)
4699 if (drm_core_check_feature(dev, DRIVER_MODESET))
4702 ret = i915_gem_idle(dev);
4704 DRM_ERROR("failed to idle hardware: %d\n", ret);
4708 init_ring_lists(struct intel_ring_buffer *ring)
4710 INIT_LIST_HEAD(&ring->active_list);
4711 INIT_LIST_HEAD(&ring->request_list);
4712 INIT_LIST_HEAD(&ring->gpu_write_list);
4716 i915_gem_load(struct drm_device *dev)
4719 drm_i915_private_t *dev_priv = dev->dev_private;
4721 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4722 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4723 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4724 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
4725 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4726 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
4727 init_ring_lists(&dev_priv->render_ring);
4728 init_ring_lists(&dev_priv->bsd_ring);
4729 init_ring_lists(&dev_priv->blt_ring);
4730 for (i = 0; i < 16; i++)
4731 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4732 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4733 i915_gem_retire_work_handler);
4734 init_completion(&dev_priv->error_completion);
4736 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4738 u32 tmp = I915_READ(MI_ARB_STATE);
4739 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4740 /* arb state is a masked write, so set bit + bit in mask */
4741 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4742 I915_WRITE(MI_ARB_STATE, tmp);
4746 /* Old X drivers will take 0-2 for front, back, depth buffers */
4747 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4748 dev_priv->fence_reg_start = 3;
4750 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4751 dev_priv->num_fence_regs = 16;
4753 dev_priv->num_fence_regs = 8;
4755 /* Initialize fence registers to zero */
4756 switch (INTEL_INFO(dev)->gen) {
4758 for (i = 0; i < 16; i++)
4759 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4763 for (i = 0; i < 16; i++)
4764 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4767 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4768 for (i = 0; i < 8; i++)
4769 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4771 for (i = 0; i < 8; i++)
4772 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4775 i915_gem_detect_bit_6_swizzle(dev);
4776 init_waitqueue_head(&dev_priv->pending_flip_queue);
4778 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4779 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4780 register_shrinker(&dev_priv->mm.inactive_shrinker);
4784 * Create a physically contiguous memory object for this object
4785 * e.g. for cursor + overlay regs
4787 static int i915_gem_init_phys_object(struct drm_device *dev,
4788 int id, int size, int align)
4790 drm_i915_private_t *dev_priv = dev->dev_private;
4791 struct drm_i915_gem_phys_object *phys_obj;
4794 if (dev_priv->mm.phys_objs[id - 1] || !size)
4797 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4803 phys_obj->handle = drm_pci_alloc(dev, size, align);
4804 if (!phys_obj->handle) {
4809 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4812 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4820 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4822 drm_i915_private_t *dev_priv = dev->dev_private;
4823 struct drm_i915_gem_phys_object *phys_obj;
4825 if (!dev_priv->mm.phys_objs[id - 1])
4828 phys_obj = dev_priv->mm.phys_objs[id - 1];
4829 if (phys_obj->cur_obj) {
4830 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4834 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4836 drm_pci_free(dev, phys_obj->handle);
4838 dev_priv->mm.phys_objs[id - 1] = NULL;
4841 void i915_gem_free_all_phys_object(struct drm_device *dev)
4845 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4846 i915_gem_free_phys_object(dev, i);
4849 void i915_gem_detach_phys_object(struct drm_device *dev,
4850 struct drm_gem_object *obj)
4852 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4853 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4858 if (!obj_priv->phys_obj)
4860 vaddr = obj_priv->phys_obj->handle->vaddr;
4862 page_count = obj->size / PAGE_SIZE;
4864 for (i = 0; i < page_count; i++) {
4865 struct page *page = read_cache_page_gfp(mapping, i,
4866 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4867 if (!IS_ERR(page)) {
4868 char *dst = kmap_atomic(page);
4869 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4872 drm_clflush_pages(&page, 1);
4874 set_page_dirty(page);
4875 mark_page_accessed(page);
4876 page_cache_release(page);
4879 drm_agp_chipset_flush(dev);
4881 obj_priv->phys_obj->cur_obj = NULL;
4882 obj_priv->phys_obj = NULL;
4886 i915_gem_attach_phys_object(struct drm_device *dev,
4887 struct drm_gem_object *obj,
4891 struct address_space *mapping = obj->filp->f_path.dentry->d_inode->i_mapping;
4892 drm_i915_private_t *dev_priv = dev->dev_private;
4893 struct drm_i915_gem_object *obj_priv;
4898 if (id > I915_MAX_PHYS_OBJECT)
4901 obj_priv = to_intel_bo(obj);
4903 if (obj_priv->phys_obj) {
4904 if (obj_priv->phys_obj->id == id)
4906 i915_gem_detach_phys_object(dev, obj);
4909 /* create a new object */
4910 if (!dev_priv->mm.phys_objs[id - 1]) {
4911 ret = i915_gem_init_phys_object(dev, id,
4914 DRM_ERROR("failed to init phys object %d size: %zu\n", id, obj->size);
4919 /* bind to the object */
4920 obj_priv->phys_obj = dev_priv->mm.phys_objs[id - 1];
4921 obj_priv->phys_obj->cur_obj = obj;
4923 page_count = obj->size / PAGE_SIZE;
4925 for (i = 0; i < page_count; i++) {
4929 page = read_cache_page_gfp(mapping, i,
4930 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4932 return PTR_ERR(page);
4934 src = kmap_atomic(obj_priv->pages[i]);
4935 dst = obj_priv->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4936 memcpy(dst, src, PAGE_SIZE);
4939 mark_page_accessed(page);
4940 page_cache_release(page);
4947 i915_gem_phys_pwrite(struct drm_device *dev, struct drm_gem_object *obj,
4948 struct drm_i915_gem_pwrite *args,
4949 struct drm_file *file_priv)
4951 struct drm_i915_gem_object *obj_priv = to_intel_bo(obj);
4954 char __user *user_data;
4956 user_data = (char __user *) (uintptr_t) args->data_ptr;
4957 obj_addr = obj_priv->phys_obj->handle->vaddr + args->offset;
4959 DRM_DEBUG_DRIVER("obj_addr %p, %lld\n", obj_addr, args->size);
4960 ret = copy_from_user(obj_addr, user_data, args->size);
4964 drm_agp_chipset_flush(dev);
4968 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4970 struct drm_i915_file_private *file_priv = file->driver_priv;
4972 /* Clean up our request list when the client is going away, so that
4973 * later retire_requests won't dereference our soon-to-be-gone
4976 spin_lock(&file_priv->mm.lock);
4977 while (!list_empty(&file_priv->mm.request_list)) {
4978 struct drm_i915_gem_request *request;
4980 request = list_first_entry(&file_priv->mm.request_list,
4981 struct drm_i915_gem_request,
4983 list_del(&request->client_list);
4984 request->file_priv = NULL;
4986 spin_unlock(&file_priv->mm.lock);
4990 i915_gpu_is_active(struct drm_device *dev)
4992 drm_i915_private_t *dev_priv = dev->dev_private;
4995 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4996 list_empty(&dev_priv->mm.active_list);
4998 return !lists_empty;
5002 i915_gem_inactive_shrink(struct shrinker *shrinker,
5006 struct drm_i915_private *dev_priv =
5007 container_of(shrinker,
5008 struct drm_i915_private,
5009 mm.inactive_shrinker);
5010 struct drm_device *dev = dev_priv->dev;
5011 struct drm_i915_gem_object *obj, *next;
5014 if (!mutex_trylock(&dev->struct_mutex))
5017 /* "fast-path" to count number of available objects */
5018 if (nr_to_scan == 0) {
5020 list_for_each_entry(obj,
5021 &dev_priv->mm.inactive_list,
5024 mutex_unlock(&dev->struct_mutex);
5025 return cnt / 100 * sysctl_vfs_cache_pressure;
5029 /* first scan for clean buffers */
5030 i915_gem_retire_requests(dev);
5032 list_for_each_entry_safe(obj, next,
5033 &dev_priv->mm.inactive_list,
5035 if (i915_gem_object_is_purgeable(obj)) {
5036 i915_gem_object_unbind(&obj->base);
5037 if (--nr_to_scan == 0)
5042 /* second pass, evict/count anything still on the inactive list */
5044 list_for_each_entry_safe(obj, next,
5045 &dev_priv->mm.inactive_list,
5048 i915_gem_object_unbind(&obj->base);
5054 if (nr_to_scan && i915_gpu_is_active(dev)) {
5056 * We are desperate for pages, so as a last resort, wait
5057 * for the GPU to finish and discard whatever we can.
5058 * This has a dramatic impact to reduce the number of
5059 * OOM-killer events whilst running the GPU aggressively.
5061 if (i915_gpu_idle(dev) == 0)
5064 mutex_unlock(&dev->struct_mutex);
5065 return cnt / 100 * sysctl_vfs_cache_pressure;