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/shmem_fs.h>
35 #include <linux/slab.h>
36 #include <linux/swap.h>
37 #include <linux/pci.h>
39 static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
42 static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
44 static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
47 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
48 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
50 bool map_and_fenceable);
51 static void i915_gem_clear_fence_reg(struct drm_device *dev,
52 struct drm_i915_fence_reg *reg);
53 static int i915_gem_phys_pwrite(struct drm_device *dev,
54 struct drm_i915_gem_object *obj,
55 struct drm_i915_gem_pwrite *args,
56 struct drm_file *file);
57 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
59 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
60 struct shrink_control *sc);
62 /* some bookkeeping */
63 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
66 dev_priv->mm.object_count++;
67 dev_priv->mm.object_memory += size;
70 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
73 dev_priv->mm.object_count--;
74 dev_priv->mm.object_memory -= size;
78 i915_gem_wait_for_error(struct drm_device *dev)
80 struct drm_i915_private *dev_priv = dev->dev_private;
81 struct completion *x = &dev_priv->error_completion;
85 if (!atomic_read(&dev_priv->mm.wedged))
88 ret = wait_for_completion_interruptible(x);
92 if (atomic_read(&dev_priv->mm.wedged)) {
93 /* GPU is hung, bump the completion count to account for
94 * the token we just consumed so that we never hit zero and
95 * end up waiting upon a subsequent completion event that
98 spin_lock_irqsave(&x->wait.lock, flags);
100 spin_unlock_irqrestore(&x->wait.lock, flags);
105 int i915_mutex_lock_interruptible(struct drm_device *dev)
109 ret = i915_gem_wait_for_error(dev);
113 ret = mutex_lock_interruptible(&dev->struct_mutex);
117 WARN_ON(i915_verify_lists(dev));
122 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
124 return obj->gtt_space && !obj->active && obj->pin_count == 0;
127 void i915_gem_do_init(struct drm_device *dev,
129 unsigned long mappable_end,
132 drm_i915_private_t *dev_priv = dev->dev_private;
134 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
136 dev_priv->mm.gtt_start = start;
137 dev_priv->mm.gtt_mappable_end = mappable_end;
138 dev_priv->mm.gtt_end = end;
139 dev_priv->mm.gtt_total = end - start;
140 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
142 /* Take over this portion of the GTT */
143 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
147 i915_gem_init_ioctl(struct drm_device *dev, void *data,
148 struct drm_file *file)
150 struct drm_i915_gem_init *args = data;
152 if (args->gtt_start >= args->gtt_end ||
153 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
156 mutex_lock(&dev->struct_mutex);
157 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
158 mutex_unlock(&dev->struct_mutex);
164 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
165 struct drm_file *file)
167 struct drm_i915_private *dev_priv = dev->dev_private;
168 struct drm_i915_gem_get_aperture *args = data;
169 struct drm_i915_gem_object *obj;
172 if (!(dev->driver->driver_features & DRIVER_GEM))
176 mutex_lock(&dev->struct_mutex);
177 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
178 pinned += obj->gtt_space->size;
179 mutex_unlock(&dev->struct_mutex);
181 args->aper_size = dev_priv->mm.gtt_total;
182 args->aper_available_size = args->aper_size - pinned;
188 i915_gem_create(struct drm_file *file,
189 struct drm_device *dev,
193 struct drm_i915_gem_object *obj;
197 size = roundup(size, PAGE_SIZE);
199 /* Allocate the new object */
200 obj = i915_gem_alloc_object(dev, size);
204 ret = drm_gem_handle_create(file, &obj->base, &handle);
206 drm_gem_object_release(&obj->base);
207 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
212 /* drop reference from allocate - handle holds it now */
213 drm_gem_object_unreference(&obj->base);
214 trace_i915_gem_object_create(obj);
221 i915_gem_dumb_create(struct drm_file *file,
222 struct drm_device *dev,
223 struct drm_mode_create_dumb *args)
225 /* have to work out size/pitch and return them */
226 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
227 args->size = args->pitch * args->height;
228 return i915_gem_create(file, dev,
229 args->size, &args->handle);
232 int i915_gem_dumb_destroy(struct drm_file *file,
233 struct drm_device *dev,
236 return drm_gem_handle_delete(file, handle);
240 * Creates a new mm object and returns a handle to it.
243 i915_gem_create_ioctl(struct drm_device *dev, void *data,
244 struct drm_file *file)
246 struct drm_i915_gem_create *args = data;
247 return i915_gem_create(file, dev,
248 args->size, &args->handle);
251 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
253 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
255 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
256 obj->tiling_mode != I915_TILING_NONE;
260 slow_shmem_copy(struct page *dst_page,
262 struct page *src_page,
266 char *dst_vaddr, *src_vaddr;
268 dst_vaddr = kmap(dst_page);
269 src_vaddr = kmap(src_page);
271 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
278 slow_shmem_bit17_copy(struct page *gpu_page,
280 struct page *cpu_page,
285 char *gpu_vaddr, *cpu_vaddr;
287 /* Use the unswizzled path if this page isn't affected. */
288 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
290 return slow_shmem_copy(cpu_page, cpu_offset,
291 gpu_page, gpu_offset, length);
293 return slow_shmem_copy(gpu_page, gpu_offset,
294 cpu_page, cpu_offset, length);
297 gpu_vaddr = kmap(gpu_page);
298 cpu_vaddr = kmap(cpu_page);
300 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
301 * XORing with the other bits (A9 for Y, A9 and A10 for X)
304 int cacheline_end = ALIGN(gpu_offset + 1, 64);
305 int this_length = min(cacheline_end - gpu_offset, length);
306 int swizzled_gpu_offset = gpu_offset ^ 64;
309 memcpy(cpu_vaddr + cpu_offset,
310 gpu_vaddr + swizzled_gpu_offset,
313 memcpy(gpu_vaddr + swizzled_gpu_offset,
314 cpu_vaddr + cpu_offset,
317 cpu_offset += this_length;
318 gpu_offset += this_length;
319 length -= this_length;
327 * This is the fast shmem pread path, which attempts to copy_from_user directly
328 * from the backing pages of the object to the user's address space. On a
329 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
332 i915_gem_shmem_pread_fast(struct drm_device *dev,
333 struct drm_i915_gem_object *obj,
334 struct drm_i915_gem_pread *args,
335 struct drm_file *file)
337 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
340 char __user *user_data;
341 int page_offset, page_length;
343 user_data = (char __user *) (uintptr_t) args->data_ptr;
346 offset = args->offset;
353 /* Operation in this page
355 * page_offset = offset within page
356 * page_length = bytes to copy for this page
358 page_offset = offset_in_page(offset);
359 page_length = remain;
360 if ((page_offset + remain) > PAGE_SIZE)
361 page_length = PAGE_SIZE - page_offset;
363 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
365 return PTR_ERR(page);
367 vaddr = kmap_atomic(page);
368 ret = __copy_to_user_inatomic(user_data,
371 kunmap_atomic(vaddr);
373 mark_page_accessed(page);
374 page_cache_release(page);
378 remain -= page_length;
379 user_data += page_length;
380 offset += page_length;
387 * This is the fallback shmem pread path, which allocates temporary storage
388 * in kernel space to copy_to_user into outside of the struct_mutex, so we
389 * can copy out of the object's backing pages while holding the struct mutex
390 * and not take page faults.
393 i915_gem_shmem_pread_slow(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
395 struct drm_i915_gem_pread *args,
396 struct drm_file *file)
398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
399 struct mm_struct *mm = current->mm;
400 struct page **user_pages;
402 loff_t offset, pinned_pages, i;
403 loff_t first_data_page, last_data_page, num_pages;
404 int shmem_page_offset;
405 int data_page_index, data_page_offset;
408 uint64_t data_ptr = args->data_ptr;
409 int do_bit17_swizzling;
413 /* Pin the user pages containing the data. We can't fault while
414 * holding the struct mutex, yet we want to hold it while
415 * dereferencing the user data.
417 first_data_page = data_ptr / PAGE_SIZE;
418 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
419 num_pages = last_data_page - first_data_page + 1;
421 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
422 if (user_pages == NULL)
425 mutex_unlock(&dev->struct_mutex);
426 down_read(&mm->mmap_sem);
427 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
428 num_pages, 1, 0, user_pages, NULL);
429 up_read(&mm->mmap_sem);
430 mutex_lock(&dev->struct_mutex);
431 if (pinned_pages < num_pages) {
436 ret = i915_gem_object_set_cpu_read_domain_range(obj,
442 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
444 offset = args->offset;
449 /* Operation in this page
451 * shmem_page_offset = offset within page in shmem file
452 * data_page_index = page number in get_user_pages return
453 * data_page_offset = offset with data_page_index page.
454 * page_length = bytes to copy for this page
456 shmem_page_offset = offset_in_page(offset);
457 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
458 data_page_offset = offset_in_page(data_ptr);
460 page_length = remain;
461 if ((shmem_page_offset + page_length) > PAGE_SIZE)
462 page_length = PAGE_SIZE - shmem_page_offset;
463 if ((data_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - data_page_offset;
466 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
472 if (do_bit17_swizzling) {
473 slow_shmem_bit17_copy(page,
475 user_pages[data_page_index],
480 slow_shmem_copy(user_pages[data_page_index],
487 mark_page_accessed(page);
488 page_cache_release(page);
490 remain -= page_length;
491 data_ptr += page_length;
492 offset += page_length;
496 for (i = 0; i < pinned_pages; i++) {
497 SetPageDirty(user_pages[i]);
498 mark_page_accessed(user_pages[i]);
499 page_cache_release(user_pages[i]);
501 drm_free_large(user_pages);
507 * Reads data from the object referenced by handle.
509 * On error, the contents of *data are undefined.
512 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
513 struct drm_file *file)
515 struct drm_i915_gem_pread *args = data;
516 struct drm_i915_gem_object *obj;
522 if (!access_ok(VERIFY_WRITE,
523 (char __user *)(uintptr_t)args->data_ptr,
527 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
532 ret = i915_mutex_lock_interruptible(dev);
536 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
537 if (&obj->base == NULL) {
542 /* Bounds check source. */
543 if (args->offset > obj->base.size ||
544 args->size > obj->base.size - args->offset) {
549 trace_i915_gem_object_pread(obj, args->offset, args->size);
551 ret = i915_gem_object_set_cpu_read_domain_range(obj,
558 if (!i915_gem_object_needs_bit17_swizzle(obj))
559 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
561 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
564 drm_gem_object_unreference(&obj->base);
566 mutex_unlock(&dev->struct_mutex);
570 /* This is the fast write path which cannot handle
571 * page faults in the source data
575 fast_user_write(struct io_mapping *mapping,
576 loff_t page_base, int page_offset,
577 char __user *user_data,
581 unsigned long unwritten;
583 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
584 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
586 io_mapping_unmap_atomic(vaddr_atomic);
590 /* Here's the write path which can sleep for
595 slow_kernel_write(struct io_mapping *mapping,
596 loff_t gtt_base, int gtt_offset,
597 struct page *user_page, int user_offset,
600 char __iomem *dst_vaddr;
603 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
604 src_vaddr = kmap(user_page);
606 memcpy_toio(dst_vaddr + gtt_offset,
607 src_vaddr + user_offset,
611 io_mapping_unmap(dst_vaddr);
615 * This is the fast pwrite path, where we copy the data directly from the
616 * user into the GTT, uncached.
619 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
620 struct drm_i915_gem_object *obj,
621 struct drm_i915_gem_pwrite *args,
622 struct drm_file *file)
624 drm_i915_private_t *dev_priv = dev->dev_private;
626 loff_t offset, page_base;
627 char __user *user_data;
628 int page_offset, page_length;
630 user_data = (char __user *) (uintptr_t) args->data_ptr;
633 offset = obj->gtt_offset + args->offset;
636 /* Operation in this page
638 * page_base = page offset within aperture
639 * page_offset = offset within page
640 * page_length = bytes to copy for this page
642 page_base = offset & PAGE_MASK;
643 page_offset = offset_in_page(offset);
644 page_length = remain;
645 if ((page_offset + remain) > PAGE_SIZE)
646 page_length = PAGE_SIZE - page_offset;
648 /* If we get a fault while copying data, then (presumably) our
649 * source page isn't available. Return the error and we'll
650 * retry in the slow path.
652 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
653 page_offset, user_data, page_length))
656 remain -= page_length;
657 user_data += page_length;
658 offset += page_length;
665 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
666 * the memory and maps it using kmap_atomic for copying.
668 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
669 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
672 i915_gem_gtt_pwrite_slow(struct drm_device *dev,
673 struct drm_i915_gem_object *obj,
674 struct drm_i915_gem_pwrite *args,
675 struct drm_file *file)
677 drm_i915_private_t *dev_priv = dev->dev_private;
679 loff_t gtt_page_base, offset;
680 loff_t first_data_page, last_data_page, num_pages;
681 loff_t pinned_pages, i;
682 struct page **user_pages;
683 struct mm_struct *mm = current->mm;
684 int gtt_page_offset, data_page_offset, data_page_index, page_length;
686 uint64_t data_ptr = args->data_ptr;
690 /* Pin the user pages containing the data. We can't fault while
691 * holding the struct mutex, and all of the pwrite implementations
692 * want to hold it while dereferencing the user data.
694 first_data_page = data_ptr / PAGE_SIZE;
695 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
696 num_pages = last_data_page - first_data_page + 1;
698 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
699 if (user_pages == NULL)
702 mutex_unlock(&dev->struct_mutex);
703 down_read(&mm->mmap_sem);
704 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
705 num_pages, 0, 0, user_pages, NULL);
706 up_read(&mm->mmap_sem);
707 mutex_lock(&dev->struct_mutex);
708 if (pinned_pages < num_pages) {
710 goto out_unpin_pages;
713 ret = i915_gem_object_set_to_gtt_domain(obj, true);
715 goto out_unpin_pages;
717 ret = i915_gem_object_put_fence(obj);
719 goto out_unpin_pages;
721 offset = obj->gtt_offset + args->offset;
724 /* Operation in this page
726 * gtt_page_base = page offset within aperture
727 * gtt_page_offset = offset within page in aperture
728 * data_page_index = page number in get_user_pages return
729 * data_page_offset = offset with data_page_index page.
730 * page_length = bytes to copy for this page
732 gtt_page_base = offset & PAGE_MASK;
733 gtt_page_offset = offset_in_page(offset);
734 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
735 data_page_offset = offset_in_page(data_ptr);
737 page_length = remain;
738 if ((gtt_page_offset + page_length) > PAGE_SIZE)
739 page_length = PAGE_SIZE - gtt_page_offset;
740 if ((data_page_offset + page_length) > PAGE_SIZE)
741 page_length = PAGE_SIZE - data_page_offset;
743 slow_kernel_write(dev_priv->mm.gtt_mapping,
744 gtt_page_base, gtt_page_offset,
745 user_pages[data_page_index],
749 remain -= page_length;
750 offset += page_length;
751 data_ptr += page_length;
755 for (i = 0; i < pinned_pages; i++)
756 page_cache_release(user_pages[i]);
757 drm_free_large(user_pages);
763 * This is the fast shmem pwrite path, which attempts to directly
764 * copy_from_user into the kmapped pages backing the object.
767 i915_gem_shmem_pwrite_fast(struct drm_device *dev,
768 struct drm_i915_gem_object *obj,
769 struct drm_i915_gem_pwrite *args,
770 struct drm_file *file)
772 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
775 char __user *user_data;
776 int page_offset, page_length;
778 user_data = (char __user *) (uintptr_t) args->data_ptr;
781 offset = args->offset;
789 /* Operation in this page
791 * page_offset = offset within page
792 * page_length = bytes to copy for this page
794 page_offset = offset_in_page(offset);
795 page_length = remain;
796 if ((page_offset + remain) > PAGE_SIZE)
797 page_length = PAGE_SIZE - page_offset;
799 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
801 return PTR_ERR(page);
803 vaddr = kmap_atomic(page, KM_USER0);
804 ret = __copy_from_user_inatomic(vaddr + page_offset,
807 kunmap_atomic(vaddr, KM_USER0);
809 set_page_dirty(page);
810 mark_page_accessed(page);
811 page_cache_release(page);
813 /* If we get a fault while copying data, then (presumably) our
814 * source page isn't available. Return the error and we'll
815 * retry in the slow path.
820 remain -= page_length;
821 user_data += page_length;
822 offset += page_length;
829 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
830 * the memory and maps it using kmap_atomic for copying.
832 * This avoids taking mmap_sem for faulting on the user's address while the
833 * struct_mutex is held.
836 i915_gem_shmem_pwrite_slow(struct drm_device *dev,
837 struct drm_i915_gem_object *obj,
838 struct drm_i915_gem_pwrite *args,
839 struct drm_file *file)
841 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
842 struct mm_struct *mm = current->mm;
843 struct page **user_pages;
845 loff_t offset, pinned_pages, i;
846 loff_t first_data_page, last_data_page, num_pages;
847 int shmem_page_offset;
848 int data_page_index, data_page_offset;
851 uint64_t data_ptr = args->data_ptr;
852 int do_bit17_swizzling;
856 /* Pin the user pages containing the data. We can't fault while
857 * holding the struct mutex, and all of the pwrite implementations
858 * want to hold it while dereferencing the user data.
860 first_data_page = data_ptr / PAGE_SIZE;
861 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
862 num_pages = last_data_page - first_data_page + 1;
864 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
865 if (user_pages == NULL)
868 mutex_unlock(&dev->struct_mutex);
869 down_read(&mm->mmap_sem);
870 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
871 num_pages, 0, 0, user_pages, NULL);
872 up_read(&mm->mmap_sem);
873 mutex_lock(&dev->struct_mutex);
874 if (pinned_pages < num_pages) {
879 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
883 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
885 offset = args->offset;
891 /* Operation in this page
893 * shmem_page_offset = offset within page in shmem file
894 * data_page_index = page number in get_user_pages return
895 * data_page_offset = offset with data_page_index page.
896 * page_length = bytes to copy for this page
898 shmem_page_offset = offset_in_page(offset);
899 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
900 data_page_offset = offset_in_page(data_ptr);
902 page_length = remain;
903 if ((shmem_page_offset + page_length) > PAGE_SIZE)
904 page_length = PAGE_SIZE - shmem_page_offset;
905 if ((data_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - data_page_offset;
908 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
914 if (do_bit17_swizzling) {
915 slow_shmem_bit17_copy(page,
917 user_pages[data_page_index],
922 slow_shmem_copy(page,
924 user_pages[data_page_index],
929 set_page_dirty(page);
930 mark_page_accessed(page);
931 page_cache_release(page);
933 remain -= page_length;
934 data_ptr += page_length;
935 offset += page_length;
939 for (i = 0; i < pinned_pages; i++)
940 page_cache_release(user_pages[i]);
941 drm_free_large(user_pages);
947 * Writes data to the object referenced by handle.
949 * On error, the contents of the buffer that were to be modified are undefined.
952 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
953 struct drm_file *file)
955 struct drm_i915_gem_pwrite *args = data;
956 struct drm_i915_gem_object *obj;
962 if (!access_ok(VERIFY_READ,
963 (char __user *)(uintptr_t)args->data_ptr,
967 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
972 ret = i915_mutex_lock_interruptible(dev);
976 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
977 if (&obj->base == NULL) {
982 /* Bounds check destination. */
983 if (args->offset > obj->base.size ||
984 args->size > obj->base.size - args->offset) {
989 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
991 /* We can only do the GTT pwrite on untiled buffers, as otherwise
992 * it would end up going through the fenced access, and we'll get
993 * different detiling behavior between reading and writing.
994 * pread/pwrite currently are reading and writing from the CPU
995 * perspective, requiring manual detiling by the client.
998 ret = i915_gem_phys_pwrite(dev, obj, args, file);
999 else if (obj->gtt_space &&
1000 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1001 ret = i915_gem_object_pin(obj, 0, true);
1005 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1009 ret = i915_gem_object_put_fence(obj);
1013 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1015 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1018 i915_gem_object_unpin(obj);
1020 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1025 if (!i915_gem_object_needs_bit17_swizzle(obj))
1026 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1028 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1032 drm_gem_object_unreference(&obj->base);
1034 mutex_unlock(&dev->struct_mutex);
1039 * Called when user space prepares to use an object with the CPU, either
1040 * through the mmap ioctl's mapping or a GTT mapping.
1043 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1044 struct drm_file *file)
1046 struct drm_i915_gem_set_domain *args = data;
1047 struct drm_i915_gem_object *obj;
1048 uint32_t read_domains = args->read_domains;
1049 uint32_t write_domain = args->write_domain;
1052 if (!(dev->driver->driver_features & DRIVER_GEM))
1055 /* Only handle setting domains to types used by the CPU. */
1056 if (write_domain & I915_GEM_GPU_DOMAINS)
1059 if (read_domains & I915_GEM_GPU_DOMAINS)
1062 /* Having something in the write domain implies it's in the read
1063 * domain, and only that read domain. Enforce that in the request.
1065 if (write_domain != 0 && read_domains != write_domain)
1068 ret = i915_mutex_lock_interruptible(dev);
1072 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1073 if (&obj->base == NULL) {
1078 if (read_domains & I915_GEM_DOMAIN_GTT) {
1079 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1081 /* Silently promote "you're not bound, there was nothing to do"
1082 * to success, since the client was just asking us to
1083 * make sure everything was done.
1088 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1091 drm_gem_object_unreference(&obj->base);
1093 mutex_unlock(&dev->struct_mutex);
1098 * Called when user space has done writes to this buffer
1101 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1102 struct drm_file *file)
1104 struct drm_i915_gem_sw_finish *args = data;
1105 struct drm_i915_gem_object *obj;
1108 if (!(dev->driver->driver_features & DRIVER_GEM))
1111 ret = i915_mutex_lock_interruptible(dev);
1115 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1116 if (&obj->base == NULL) {
1121 /* Pinned buffers may be scanout, so flush the cache */
1123 i915_gem_object_flush_cpu_write_domain(obj);
1125 drm_gem_object_unreference(&obj->base);
1127 mutex_unlock(&dev->struct_mutex);
1132 * Maps the contents of an object, returning the address it is mapped
1135 * While the mapping holds a reference on the contents of the object, it doesn't
1136 * imply a ref on the object itself.
1139 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1140 struct drm_file *file)
1142 struct drm_i915_private *dev_priv = dev->dev_private;
1143 struct drm_i915_gem_mmap *args = data;
1144 struct drm_gem_object *obj;
1147 if (!(dev->driver->driver_features & DRIVER_GEM))
1150 obj = drm_gem_object_lookup(dev, file, args->handle);
1154 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1155 drm_gem_object_unreference_unlocked(obj);
1159 down_write(¤t->mm->mmap_sem);
1160 addr = do_mmap(obj->filp, 0, args->size,
1161 PROT_READ | PROT_WRITE, MAP_SHARED,
1163 up_write(¤t->mm->mmap_sem);
1164 drm_gem_object_unreference_unlocked(obj);
1165 if (IS_ERR((void *)addr))
1168 args->addr_ptr = (uint64_t) addr;
1174 * i915_gem_fault - fault a page into the GTT
1175 * vma: VMA in question
1178 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1179 * from userspace. The fault handler takes care of binding the object to
1180 * the GTT (if needed), allocating and programming a fence register (again,
1181 * only if needed based on whether the old reg is still valid or the object
1182 * is tiled) and inserting a new PTE into the faulting process.
1184 * Note that the faulting process may involve evicting existing objects
1185 * from the GTT and/or fence registers to make room. So performance may
1186 * suffer if the GTT working set is large or there are few fence registers
1189 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1191 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1192 struct drm_device *dev = obj->base.dev;
1193 drm_i915_private_t *dev_priv = dev->dev_private;
1194 pgoff_t page_offset;
1197 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1199 /* We don't use vmf->pgoff since that has the fake offset */
1200 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1203 ret = i915_mutex_lock_interruptible(dev);
1207 trace_i915_gem_object_fault(obj, page_offset, true, write);
1209 /* Now bind it into the GTT if needed */
1210 if (!obj->map_and_fenceable) {
1211 ret = i915_gem_object_unbind(obj);
1215 if (!obj->gtt_space) {
1216 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1220 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1225 if (obj->tiling_mode == I915_TILING_NONE)
1226 ret = i915_gem_object_put_fence(obj);
1228 ret = i915_gem_object_get_fence(obj, NULL);
1232 if (i915_gem_object_is_inactive(obj))
1233 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1235 obj->fault_mappable = true;
1237 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1240 /* Finally, remap it using the new GTT offset */
1241 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1243 mutex_unlock(&dev->struct_mutex);
1248 /* Give the error handler a chance to run and move the
1249 * objects off the GPU active list. Next time we service the
1250 * fault, we should be able to transition the page into the
1251 * GTT without touching the GPU (and so avoid further
1252 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1253 * with coherency, just lost writes.
1259 return VM_FAULT_NOPAGE;
1261 return VM_FAULT_OOM;
1263 return VM_FAULT_SIGBUS;
1268 * i915_gem_release_mmap - remove physical page mappings
1269 * @obj: obj in question
1271 * Preserve the reservation of the mmapping with the DRM core code, but
1272 * relinquish ownership of the pages back to the system.
1274 * It is vital that we remove the page mapping if we have mapped a tiled
1275 * object through the GTT and then lose the fence register due to
1276 * resource pressure. Similarly if the object has been moved out of the
1277 * aperture, than pages mapped into userspace must be revoked. Removing the
1278 * mapping will then trigger a page fault on the next user access, allowing
1279 * fixup by i915_gem_fault().
1282 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1284 if (!obj->fault_mappable)
1287 if (obj->base.dev->dev_mapping)
1288 unmap_mapping_range(obj->base.dev->dev_mapping,
1289 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1292 obj->fault_mappable = false;
1296 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1300 if (INTEL_INFO(dev)->gen >= 4 ||
1301 tiling_mode == I915_TILING_NONE)
1304 /* Previous chips need a power-of-two fence region when tiling */
1305 if (INTEL_INFO(dev)->gen == 3)
1306 gtt_size = 1024*1024;
1308 gtt_size = 512*1024;
1310 while (gtt_size < size)
1317 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1318 * @obj: object to check
1320 * Return the required GTT alignment for an object, taking into account
1321 * potential fence register mapping.
1324 i915_gem_get_gtt_alignment(struct drm_device *dev,
1329 * Minimum alignment is 4k (GTT page size), but might be greater
1330 * if a fence register is needed for the object.
1332 if (INTEL_INFO(dev)->gen >= 4 ||
1333 tiling_mode == I915_TILING_NONE)
1337 * Previous chips need to be aligned to the size of the smallest
1338 * fence register that can contain the object.
1340 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1344 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1347 * @size: size of the object
1348 * @tiling_mode: tiling mode of the object
1350 * Return the required GTT alignment for an object, only taking into account
1351 * unfenced tiled surface requirements.
1354 i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1359 * Minimum alignment is 4k (GTT page size) for sane hw.
1361 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1362 tiling_mode == I915_TILING_NONE)
1365 /* Previous hardware however needs to be aligned to a power-of-two
1366 * tile height. The simplest method for determining this is to reuse
1367 * the power-of-tile object size.
1369 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1373 i915_gem_mmap_gtt(struct drm_file *file,
1374 struct drm_device *dev,
1378 struct drm_i915_private *dev_priv = dev->dev_private;
1379 struct drm_i915_gem_object *obj;
1382 if (!(dev->driver->driver_features & DRIVER_GEM))
1385 ret = i915_mutex_lock_interruptible(dev);
1389 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1390 if (&obj->base == NULL) {
1395 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1400 if (obj->madv != I915_MADV_WILLNEED) {
1401 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1406 if (!obj->base.map_list.map) {
1407 ret = drm_gem_create_mmap_offset(&obj->base);
1412 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1415 drm_gem_object_unreference(&obj->base);
1417 mutex_unlock(&dev->struct_mutex);
1422 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1424 * @data: GTT mapping ioctl data
1425 * @file: GEM object info
1427 * Simply returns the fake offset to userspace so it can mmap it.
1428 * The mmap call will end up in drm_gem_mmap(), which will set things
1429 * up so we can get faults in the handler above.
1431 * The fault handler will take care of binding the object into the GTT
1432 * (since it may have been evicted to make room for something), allocating
1433 * a fence register, and mapping the appropriate aperture address into
1437 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1438 struct drm_file *file)
1440 struct drm_i915_gem_mmap_gtt *args = data;
1442 if (!(dev->driver->driver_features & DRIVER_GEM))
1445 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1450 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1454 struct address_space *mapping;
1455 struct inode *inode;
1458 /* Get the list of pages out of our struct file. They'll be pinned
1459 * at this point until we release them.
1461 page_count = obj->base.size / PAGE_SIZE;
1462 BUG_ON(obj->pages != NULL);
1463 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1464 if (obj->pages == NULL)
1467 inode = obj->base.filp->f_path.dentry->d_inode;
1468 mapping = inode->i_mapping;
1469 gfpmask |= mapping_gfp_mask(mapping);
1471 for (i = 0; i < page_count; i++) {
1472 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
1476 obj->pages[i] = page;
1479 if (obj->tiling_mode != I915_TILING_NONE)
1480 i915_gem_object_do_bit_17_swizzle(obj);
1486 page_cache_release(obj->pages[i]);
1488 drm_free_large(obj->pages);
1490 return PTR_ERR(page);
1494 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1496 int page_count = obj->base.size / PAGE_SIZE;
1499 BUG_ON(obj->madv == __I915_MADV_PURGED);
1501 if (obj->tiling_mode != I915_TILING_NONE)
1502 i915_gem_object_save_bit_17_swizzle(obj);
1504 if (obj->madv == I915_MADV_DONTNEED)
1507 for (i = 0; i < page_count; i++) {
1509 set_page_dirty(obj->pages[i]);
1511 if (obj->madv == I915_MADV_WILLNEED)
1512 mark_page_accessed(obj->pages[i]);
1514 page_cache_release(obj->pages[i]);
1518 drm_free_large(obj->pages);
1523 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1524 struct intel_ring_buffer *ring,
1527 struct drm_device *dev = obj->base.dev;
1528 struct drm_i915_private *dev_priv = dev->dev_private;
1530 BUG_ON(ring == NULL);
1533 /* Add a reference if we're newly entering the active list. */
1535 drm_gem_object_reference(&obj->base);
1539 /* Move from whatever list we were on to the tail of execution. */
1540 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1541 list_move_tail(&obj->ring_list, &ring->active_list);
1543 obj->last_rendering_seqno = seqno;
1544 if (obj->fenced_gpu_access) {
1545 struct drm_i915_fence_reg *reg;
1547 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1549 obj->last_fenced_seqno = seqno;
1550 obj->last_fenced_ring = ring;
1552 reg = &dev_priv->fence_regs[obj->fence_reg];
1553 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
1558 i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1560 list_del_init(&obj->ring_list);
1561 obj->last_rendering_seqno = 0;
1565 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1567 struct drm_device *dev = obj->base.dev;
1568 drm_i915_private_t *dev_priv = dev->dev_private;
1570 BUG_ON(!obj->active);
1571 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1573 i915_gem_object_move_off_active(obj);
1577 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1579 struct drm_device *dev = obj->base.dev;
1580 struct drm_i915_private *dev_priv = dev->dev_private;
1582 if (obj->pin_count != 0)
1583 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1585 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1587 BUG_ON(!list_empty(&obj->gpu_write_list));
1588 BUG_ON(!obj->active);
1591 i915_gem_object_move_off_active(obj);
1592 obj->fenced_gpu_access = false;
1595 obj->pending_gpu_write = false;
1596 drm_gem_object_unreference(&obj->base);
1598 WARN_ON(i915_verify_lists(dev));
1601 /* Immediately discard the backing storage */
1603 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1605 struct inode *inode;
1607 /* Our goal here is to return as much of the memory as
1608 * is possible back to the system as we are called from OOM.
1609 * To do this we must instruct the shmfs to drop all of its
1610 * backing pages, *now*.
1612 inode = obj->base.filp->f_path.dentry->d_inode;
1613 shmem_truncate_range(inode, 0, (loff_t)-1);
1615 obj->madv = __I915_MADV_PURGED;
1619 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1621 return obj->madv == I915_MADV_DONTNEED;
1625 i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1626 uint32_t flush_domains)
1628 struct drm_i915_gem_object *obj, *next;
1630 list_for_each_entry_safe(obj, next,
1631 &ring->gpu_write_list,
1633 if (obj->base.write_domain & flush_domains) {
1634 uint32_t old_write_domain = obj->base.write_domain;
1636 obj->base.write_domain = 0;
1637 list_del_init(&obj->gpu_write_list);
1638 i915_gem_object_move_to_active(obj, ring,
1639 i915_gem_next_request_seqno(ring));
1641 trace_i915_gem_object_change_domain(obj,
1642 obj->base.read_domains,
1649 i915_add_request(struct intel_ring_buffer *ring,
1650 struct drm_file *file,
1651 struct drm_i915_gem_request *request)
1653 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1658 BUG_ON(request == NULL);
1660 ret = ring->add_request(ring, &seqno);
1664 trace_i915_gem_request_add(ring, seqno);
1666 request->seqno = seqno;
1667 request->ring = ring;
1668 request->emitted_jiffies = jiffies;
1669 was_empty = list_empty(&ring->request_list);
1670 list_add_tail(&request->list, &ring->request_list);
1673 struct drm_i915_file_private *file_priv = file->driver_priv;
1675 spin_lock(&file_priv->mm.lock);
1676 request->file_priv = file_priv;
1677 list_add_tail(&request->client_list,
1678 &file_priv->mm.request_list);
1679 spin_unlock(&file_priv->mm.lock);
1682 ring->outstanding_lazy_request = false;
1684 if (!dev_priv->mm.suspended) {
1685 if (i915_enable_hangcheck) {
1686 mod_timer(&dev_priv->hangcheck_timer,
1688 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1691 queue_delayed_work(dev_priv->wq,
1692 &dev_priv->mm.retire_work, HZ);
1698 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1700 struct drm_i915_file_private *file_priv = request->file_priv;
1705 spin_lock(&file_priv->mm.lock);
1706 if (request->file_priv) {
1707 list_del(&request->client_list);
1708 request->file_priv = NULL;
1710 spin_unlock(&file_priv->mm.lock);
1713 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1714 struct intel_ring_buffer *ring)
1716 while (!list_empty(&ring->request_list)) {
1717 struct drm_i915_gem_request *request;
1719 request = list_first_entry(&ring->request_list,
1720 struct drm_i915_gem_request,
1723 list_del(&request->list);
1724 i915_gem_request_remove_from_client(request);
1728 while (!list_empty(&ring->active_list)) {
1729 struct drm_i915_gem_object *obj;
1731 obj = list_first_entry(&ring->active_list,
1732 struct drm_i915_gem_object,
1735 obj->base.write_domain = 0;
1736 list_del_init(&obj->gpu_write_list);
1737 i915_gem_object_move_to_inactive(obj);
1741 static void i915_gem_reset_fences(struct drm_device *dev)
1743 struct drm_i915_private *dev_priv = dev->dev_private;
1746 for (i = 0; i < 16; i++) {
1747 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1748 struct drm_i915_gem_object *obj = reg->obj;
1753 if (obj->tiling_mode)
1754 i915_gem_release_mmap(obj);
1756 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1757 reg->obj->fenced_gpu_access = false;
1758 reg->obj->last_fenced_seqno = 0;
1759 reg->obj->last_fenced_ring = NULL;
1760 i915_gem_clear_fence_reg(dev, reg);
1764 void i915_gem_reset(struct drm_device *dev)
1766 struct drm_i915_private *dev_priv = dev->dev_private;
1767 struct drm_i915_gem_object *obj;
1770 for (i = 0; i < I915_NUM_RINGS; i++)
1771 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
1773 /* Remove anything from the flushing lists. The GPU cache is likely
1774 * to be lost on reset along with the data, so simply move the
1775 * lost bo to the inactive list.
1777 while (!list_empty(&dev_priv->mm.flushing_list)) {
1778 obj = list_first_entry(&dev_priv->mm.flushing_list,
1779 struct drm_i915_gem_object,
1782 obj->base.write_domain = 0;
1783 list_del_init(&obj->gpu_write_list);
1784 i915_gem_object_move_to_inactive(obj);
1787 /* Move everything out of the GPU domains to ensure we do any
1788 * necessary invalidation upon reuse.
1790 list_for_each_entry(obj,
1791 &dev_priv->mm.inactive_list,
1794 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1797 /* The fence registers are invalidated so clear them out */
1798 i915_gem_reset_fences(dev);
1802 * This function clears the request list as sequence numbers are passed.
1805 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
1810 if (list_empty(&ring->request_list))
1813 WARN_ON(i915_verify_lists(ring->dev));
1815 seqno = ring->get_seqno(ring);
1817 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
1818 if (seqno >= ring->sync_seqno[i])
1819 ring->sync_seqno[i] = 0;
1821 while (!list_empty(&ring->request_list)) {
1822 struct drm_i915_gem_request *request;
1824 request = list_first_entry(&ring->request_list,
1825 struct drm_i915_gem_request,
1828 if (!i915_seqno_passed(seqno, request->seqno))
1831 trace_i915_gem_request_retire(ring, request->seqno);
1833 list_del(&request->list);
1834 i915_gem_request_remove_from_client(request);
1838 /* Move any buffers on the active list that are no longer referenced
1839 * by the ringbuffer to the flushing/inactive lists as appropriate.
1841 while (!list_empty(&ring->active_list)) {
1842 struct drm_i915_gem_object *obj;
1844 obj = list_first_entry(&ring->active_list,
1845 struct drm_i915_gem_object,
1848 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1851 if (obj->base.write_domain != 0)
1852 i915_gem_object_move_to_flushing(obj);
1854 i915_gem_object_move_to_inactive(obj);
1857 if (unlikely(ring->trace_irq_seqno &&
1858 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
1859 ring->irq_put(ring);
1860 ring->trace_irq_seqno = 0;
1863 WARN_ON(i915_verify_lists(ring->dev));
1867 i915_gem_retire_requests(struct drm_device *dev)
1869 drm_i915_private_t *dev_priv = dev->dev_private;
1872 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1873 struct drm_i915_gem_object *obj, *next;
1875 /* We must be careful that during unbind() we do not
1876 * accidentally infinitely recurse into retire requests.
1878 * retire -> free -> unbind -> wait -> retire_ring
1880 list_for_each_entry_safe(obj, next,
1881 &dev_priv->mm.deferred_free_list,
1883 i915_gem_free_object_tail(obj);
1886 for (i = 0; i < I915_NUM_RINGS; i++)
1887 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
1891 i915_gem_retire_work_handler(struct work_struct *work)
1893 drm_i915_private_t *dev_priv;
1894 struct drm_device *dev;
1898 dev_priv = container_of(work, drm_i915_private_t,
1899 mm.retire_work.work);
1900 dev = dev_priv->dev;
1902 /* Come back later if the device is busy... */
1903 if (!mutex_trylock(&dev->struct_mutex)) {
1904 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1908 i915_gem_retire_requests(dev);
1910 /* Send a periodic flush down the ring so we don't hold onto GEM
1911 * objects indefinitely.
1914 for (i = 0; i < I915_NUM_RINGS; i++) {
1915 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1917 if (!list_empty(&ring->gpu_write_list)) {
1918 struct drm_i915_gem_request *request;
1921 ret = i915_gem_flush_ring(ring,
1922 0, I915_GEM_GPU_DOMAINS);
1923 request = kzalloc(sizeof(*request), GFP_KERNEL);
1924 if (ret || request == NULL ||
1925 i915_add_request(ring, NULL, request))
1929 idle &= list_empty(&ring->request_list);
1932 if (!dev_priv->mm.suspended && !idle)
1933 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1935 mutex_unlock(&dev->struct_mutex);
1939 * Waits for a sequence number to be signaled, and cleans up the
1940 * request and object lists appropriately for that event.
1943 i915_wait_request(struct intel_ring_buffer *ring,
1946 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1952 if (atomic_read(&dev_priv->mm.wedged)) {
1953 struct completion *x = &dev_priv->error_completion;
1954 bool recovery_complete;
1955 unsigned long flags;
1957 /* Give the error handler a chance to run. */
1958 spin_lock_irqsave(&x->wait.lock, flags);
1959 recovery_complete = x->done > 0;
1960 spin_unlock_irqrestore(&x->wait.lock, flags);
1962 return recovery_complete ? -EIO : -EAGAIN;
1965 if (seqno == ring->outstanding_lazy_request) {
1966 struct drm_i915_gem_request *request;
1968 request = kzalloc(sizeof(*request), GFP_KERNEL);
1969 if (request == NULL)
1972 ret = i915_add_request(ring, NULL, request);
1978 seqno = request->seqno;
1981 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
1982 if (HAS_PCH_SPLIT(ring->dev))
1983 ier = I915_READ(DEIER) | I915_READ(GTIER);
1985 ier = I915_READ(IER);
1987 DRM_ERROR("something (likely vbetool) disabled "
1988 "interrupts, re-enabling\n");
1989 ring->dev->driver->irq_preinstall(ring->dev);
1990 ring->dev->driver->irq_postinstall(ring->dev);
1993 trace_i915_gem_request_wait_begin(ring, seqno);
1995 ring->waiting_seqno = seqno;
1996 if (ring->irq_get(ring)) {
1997 if (dev_priv->mm.interruptible)
1998 ret = wait_event_interruptible(ring->irq_queue,
1999 i915_seqno_passed(ring->get_seqno(ring), seqno)
2000 || atomic_read(&dev_priv->mm.wedged));
2002 wait_event(ring->irq_queue,
2003 i915_seqno_passed(ring->get_seqno(ring), seqno)
2004 || atomic_read(&dev_priv->mm.wedged));
2006 ring->irq_put(ring);
2007 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2009 atomic_read(&dev_priv->mm.wedged), 3000))
2011 ring->waiting_seqno = 0;
2013 trace_i915_gem_request_wait_end(ring, seqno);
2015 if (atomic_read(&dev_priv->mm.wedged))
2018 if (ret && ret != -ERESTARTSYS)
2019 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2020 __func__, ret, seqno, ring->get_seqno(ring),
2021 dev_priv->next_seqno);
2023 /* Directly dispatch request retiring. While we have the work queue
2024 * to handle this, the waiter on a request often wants an associated
2025 * buffer to have made it to the inactive list, and we would need
2026 * a separate wait queue to handle that.
2029 i915_gem_retire_requests_ring(ring);
2035 * Ensures that all rendering to the object has completed and the object is
2036 * safe to unbind from the GTT or access from the CPU.
2039 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
2043 /* This function only exists to support waiting for existing rendering,
2044 * not for emitting required flushes.
2046 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2048 /* If there is rendering queued on the buffer being evicted, wait for
2052 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
2060 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2062 u32 old_write_domain, old_read_domains;
2064 /* Act a barrier for all accesses through the GTT */
2067 /* Force a pagefault for domain tracking on next user access */
2068 i915_gem_release_mmap(obj);
2070 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2073 old_read_domains = obj->base.read_domains;
2074 old_write_domain = obj->base.write_domain;
2076 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2077 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2079 trace_i915_gem_object_change_domain(obj,
2085 * Unbinds an object from the GTT aperture.
2088 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2092 if (obj->gtt_space == NULL)
2095 if (obj->pin_count != 0) {
2096 DRM_ERROR("Attempting to unbind pinned buffer\n");
2100 ret = i915_gem_object_finish_gpu(obj);
2101 if (ret == -ERESTARTSYS)
2103 /* Continue on if we fail due to EIO, the GPU is hung so we
2104 * should be safe and we need to cleanup or else we might
2105 * cause memory corruption through use-after-free.
2108 i915_gem_object_finish_gtt(obj);
2110 /* Move the object to the CPU domain to ensure that
2111 * any possible CPU writes while it's not in the GTT
2112 * are flushed when we go to remap it.
2115 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2116 if (ret == -ERESTARTSYS)
2119 /* In the event of a disaster, abandon all caches and
2120 * hope for the best.
2122 i915_gem_clflush_object(obj);
2123 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2126 /* release the fence reg _after_ flushing */
2127 ret = i915_gem_object_put_fence(obj);
2128 if (ret == -ERESTARTSYS)
2131 trace_i915_gem_object_unbind(obj);
2133 i915_gem_gtt_unbind_object(obj);
2134 i915_gem_object_put_pages_gtt(obj);
2136 list_del_init(&obj->gtt_list);
2137 list_del_init(&obj->mm_list);
2138 /* Avoid an unnecessary call to unbind on rebind. */
2139 obj->map_and_fenceable = true;
2141 drm_mm_put_block(obj->gtt_space);
2142 obj->gtt_space = NULL;
2143 obj->gtt_offset = 0;
2145 if (i915_gem_object_is_purgeable(obj))
2146 i915_gem_object_truncate(obj);
2152 i915_gem_flush_ring(struct intel_ring_buffer *ring,
2153 uint32_t invalidate_domains,
2154 uint32_t flush_domains)
2158 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2161 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2163 ret = ring->flush(ring, invalidate_domains, flush_domains);
2167 if (flush_domains & I915_GEM_GPU_DOMAINS)
2168 i915_gem_process_flushing_list(ring, flush_domains);
2173 static int i915_ring_idle(struct intel_ring_buffer *ring)
2177 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2180 if (!list_empty(&ring->gpu_write_list)) {
2181 ret = i915_gem_flush_ring(ring,
2182 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2187 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
2191 i915_gpu_idle(struct drm_device *dev)
2193 drm_i915_private_t *dev_priv = dev->dev_private;
2197 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2198 list_empty(&dev_priv->mm.active_list));
2202 /* Flush everything onto the inactive list. */
2203 for (i = 0; i < I915_NUM_RINGS; i++) {
2204 ret = i915_ring_idle(&dev_priv->ring[i]);
2212 static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2213 struct intel_ring_buffer *pipelined)
2215 struct drm_device *dev = obj->base.dev;
2216 drm_i915_private_t *dev_priv = dev->dev_private;
2217 u32 size = obj->gtt_space->size;
2218 int regnum = obj->fence_reg;
2221 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2223 val |= obj->gtt_offset & 0xfffff000;
2224 val |= (uint64_t)((obj->stride / 128) - 1) <<
2225 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2227 if (obj->tiling_mode == I915_TILING_Y)
2228 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2229 val |= I965_FENCE_REG_VALID;
2232 int ret = intel_ring_begin(pipelined, 6);
2236 intel_ring_emit(pipelined, MI_NOOP);
2237 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2238 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2239 intel_ring_emit(pipelined, (u32)val);
2240 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2241 intel_ring_emit(pipelined, (u32)(val >> 32));
2242 intel_ring_advance(pipelined);
2244 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2249 static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2250 struct intel_ring_buffer *pipelined)
2252 struct drm_device *dev = obj->base.dev;
2253 drm_i915_private_t *dev_priv = dev->dev_private;
2254 u32 size = obj->gtt_space->size;
2255 int regnum = obj->fence_reg;
2258 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2260 val |= obj->gtt_offset & 0xfffff000;
2261 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2262 if (obj->tiling_mode == I915_TILING_Y)
2263 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2264 val |= I965_FENCE_REG_VALID;
2267 int ret = intel_ring_begin(pipelined, 6);
2271 intel_ring_emit(pipelined, MI_NOOP);
2272 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2273 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2274 intel_ring_emit(pipelined, (u32)val);
2275 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2276 intel_ring_emit(pipelined, (u32)(val >> 32));
2277 intel_ring_advance(pipelined);
2279 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2284 static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2285 struct intel_ring_buffer *pipelined)
2287 struct drm_device *dev = obj->base.dev;
2288 drm_i915_private_t *dev_priv = dev->dev_private;
2289 u32 size = obj->gtt_space->size;
2290 u32 fence_reg, val, pitch_val;
2293 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2294 (size & -size) != size ||
2295 (obj->gtt_offset & (size - 1)),
2296 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2297 obj->gtt_offset, obj->map_and_fenceable, size))
2300 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2305 /* Note: pitch better be a power of two tile widths */
2306 pitch_val = obj->stride / tile_width;
2307 pitch_val = ffs(pitch_val) - 1;
2309 val = obj->gtt_offset;
2310 if (obj->tiling_mode == I915_TILING_Y)
2311 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2312 val |= I915_FENCE_SIZE_BITS(size);
2313 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2314 val |= I830_FENCE_REG_VALID;
2316 fence_reg = obj->fence_reg;
2318 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2320 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2323 int ret = intel_ring_begin(pipelined, 4);
2327 intel_ring_emit(pipelined, MI_NOOP);
2328 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2329 intel_ring_emit(pipelined, fence_reg);
2330 intel_ring_emit(pipelined, val);
2331 intel_ring_advance(pipelined);
2333 I915_WRITE(fence_reg, val);
2338 static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2339 struct intel_ring_buffer *pipelined)
2341 struct drm_device *dev = obj->base.dev;
2342 drm_i915_private_t *dev_priv = dev->dev_private;
2343 u32 size = obj->gtt_space->size;
2344 int regnum = obj->fence_reg;
2348 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2349 (size & -size) != size ||
2350 (obj->gtt_offset & (size - 1)),
2351 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2352 obj->gtt_offset, size))
2355 pitch_val = obj->stride / 128;
2356 pitch_val = ffs(pitch_val) - 1;
2358 val = obj->gtt_offset;
2359 if (obj->tiling_mode == I915_TILING_Y)
2360 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2361 val |= I830_FENCE_SIZE_BITS(size);
2362 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2363 val |= I830_FENCE_REG_VALID;
2366 int ret = intel_ring_begin(pipelined, 4);
2370 intel_ring_emit(pipelined, MI_NOOP);
2371 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2372 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2373 intel_ring_emit(pipelined, val);
2374 intel_ring_advance(pipelined);
2376 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2381 static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2383 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2387 i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
2388 struct intel_ring_buffer *pipelined)
2392 if (obj->fenced_gpu_access) {
2393 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
2394 ret = i915_gem_flush_ring(obj->last_fenced_ring,
2395 0, obj->base.write_domain);
2400 obj->fenced_gpu_access = false;
2403 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2404 if (!ring_passed_seqno(obj->last_fenced_ring,
2405 obj->last_fenced_seqno)) {
2406 ret = i915_wait_request(obj->last_fenced_ring,
2407 obj->last_fenced_seqno);
2412 obj->last_fenced_seqno = 0;
2413 obj->last_fenced_ring = NULL;
2416 /* Ensure that all CPU reads are completed before installing a fence
2417 * and all writes before removing the fence.
2419 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2426 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2430 if (obj->tiling_mode)
2431 i915_gem_release_mmap(obj);
2433 ret = i915_gem_object_flush_fence(obj, NULL);
2437 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2438 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2439 i915_gem_clear_fence_reg(obj->base.dev,
2440 &dev_priv->fence_regs[obj->fence_reg]);
2442 obj->fence_reg = I915_FENCE_REG_NONE;
2448 static struct drm_i915_fence_reg *
2449 i915_find_fence_reg(struct drm_device *dev,
2450 struct intel_ring_buffer *pipelined)
2452 struct drm_i915_private *dev_priv = dev->dev_private;
2453 struct drm_i915_fence_reg *reg, *first, *avail;
2456 /* First try to find a free reg */
2458 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2459 reg = &dev_priv->fence_regs[i];
2463 if (!reg->obj->pin_count)
2470 /* None available, try to steal one or wait for a user to finish */
2471 avail = first = NULL;
2472 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2473 if (reg->obj->pin_count)
2480 !reg->obj->last_fenced_ring ||
2481 reg->obj->last_fenced_ring == pipelined) {
2494 * i915_gem_object_get_fence - set up a fence reg for an object
2495 * @obj: object to map through a fence reg
2496 * @pipelined: ring on which to queue the change, or NULL for CPU access
2497 * @interruptible: must we wait uninterruptibly for the register to retire?
2499 * When mapping objects through the GTT, userspace wants to be able to write
2500 * to them without having to worry about swizzling if the object is tiled.
2502 * This function walks the fence regs looking for a free one for @obj,
2503 * stealing one if it can't find any.
2505 * It then sets up the reg based on the object's properties: address, pitch
2506 * and tiling format.
2509 i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2510 struct intel_ring_buffer *pipelined)
2512 struct drm_device *dev = obj->base.dev;
2513 struct drm_i915_private *dev_priv = dev->dev_private;
2514 struct drm_i915_fence_reg *reg;
2517 /* XXX disable pipelining. There are bugs. Shocking. */
2520 /* Just update our place in the LRU if our fence is getting reused. */
2521 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2522 reg = &dev_priv->fence_regs[obj->fence_reg];
2523 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2525 if (obj->tiling_changed) {
2526 ret = i915_gem_object_flush_fence(obj, pipelined);
2530 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2535 i915_gem_next_request_seqno(pipelined);
2536 obj->last_fenced_seqno = reg->setup_seqno;
2537 obj->last_fenced_ring = pipelined;
2544 if (reg->setup_seqno) {
2545 if (!ring_passed_seqno(obj->last_fenced_ring,
2546 reg->setup_seqno)) {
2547 ret = i915_wait_request(obj->last_fenced_ring,
2553 reg->setup_seqno = 0;
2555 } else if (obj->last_fenced_ring &&
2556 obj->last_fenced_ring != pipelined) {
2557 ret = i915_gem_object_flush_fence(obj, pipelined);
2565 reg = i915_find_fence_reg(dev, pipelined);
2569 ret = i915_gem_object_flush_fence(obj, pipelined);
2574 struct drm_i915_gem_object *old = reg->obj;
2576 drm_gem_object_reference(&old->base);
2578 if (old->tiling_mode)
2579 i915_gem_release_mmap(old);
2581 ret = i915_gem_object_flush_fence(old, pipelined);
2583 drm_gem_object_unreference(&old->base);
2587 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2590 old->fence_reg = I915_FENCE_REG_NONE;
2591 old->last_fenced_ring = pipelined;
2592 old->last_fenced_seqno =
2593 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2595 drm_gem_object_unreference(&old->base);
2596 } else if (obj->last_fenced_seqno == 0)
2600 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2601 obj->fence_reg = reg - dev_priv->fence_regs;
2602 obj->last_fenced_ring = pipelined;
2605 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2606 obj->last_fenced_seqno = reg->setup_seqno;
2609 obj->tiling_changed = false;
2610 switch (INTEL_INFO(dev)->gen) {
2613 ret = sandybridge_write_fence_reg(obj, pipelined);
2617 ret = i965_write_fence_reg(obj, pipelined);
2620 ret = i915_write_fence_reg(obj, pipelined);
2623 ret = i830_write_fence_reg(obj, pipelined);
2631 * i915_gem_clear_fence_reg - clear out fence register info
2632 * @obj: object to clear
2634 * Zeroes out the fence register itself and clears out the associated
2635 * data structures in dev_priv and obj.
2638 i915_gem_clear_fence_reg(struct drm_device *dev,
2639 struct drm_i915_fence_reg *reg)
2641 drm_i915_private_t *dev_priv = dev->dev_private;
2642 uint32_t fence_reg = reg - dev_priv->fence_regs;
2644 switch (INTEL_INFO(dev)->gen) {
2647 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
2651 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
2655 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2658 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2660 I915_WRITE(fence_reg, 0);
2664 list_del_init(®->lru_list);
2666 reg->setup_seqno = 0;
2670 * Finds free space in the GTT aperture and binds the object there.
2673 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2675 bool map_and_fenceable)
2677 struct drm_device *dev = obj->base.dev;
2678 drm_i915_private_t *dev_priv = dev->dev_private;
2679 struct drm_mm_node *free_space;
2680 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2681 u32 size, fence_size, fence_alignment, unfenced_alignment;
2682 bool mappable, fenceable;
2685 if (obj->madv != I915_MADV_WILLNEED) {
2686 DRM_ERROR("Attempting to bind a purgeable object\n");
2690 fence_size = i915_gem_get_gtt_size(dev,
2693 fence_alignment = i915_gem_get_gtt_alignment(dev,
2696 unfenced_alignment =
2697 i915_gem_get_unfenced_gtt_alignment(dev,
2702 alignment = map_and_fenceable ? fence_alignment :
2704 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2705 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2709 size = map_and_fenceable ? fence_size : obj->base.size;
2711 /* If the object is bigger than the entire aperture, reject it early
2712 * before evicting everything in a vain attempt to find space.
2714 if (obj->base.size >
2715 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2716 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2721 if (map_and_fenceable)
2723 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2725 dev_priv->mm.gtt_mappable_end,
2728 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2729 size, alignment, 0);
2731 if (free_space != NULL) {
2732 if (map_and_fenceable)
2734 drm_mm_get_block_range_generic(free_space,
2736 dev_priv->mm.gtt_mappable_end,
2740 drm_mm_get_block(free_space, size, alignment);
2742 if (obj->gtt_space == NULL) {
2743 /* If the gtt is empty and we're still having trouble
2744 * fitting our object in, we're out of memory.
2746 ret = i915_gem_evict_something(dev, size, alignment,
2754 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2756 drm_mm_put_block(obj->gtt_space);
2757 obj->gtt_space = NULL;
2759 if (ret == -ENOMEM) {
2760 /* first try to reclaim some memory by clearing the GTT */
2761 ret = i915_gem_evict_everything(dev, false);
2763 /* now try to shrink everyone else */
2778 ret = i915_gem_gtt_bind_object(obj);
2780 i915_gem_object_put_pages_gtt(obj);
2781 drm_mm_put_block(obj->gtt_space);
2782 obj->gtt_space = NULL;
2784 if (i915_gem_evict_everything(dev, false))
2790 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2791 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2793 /* Assert that the object is not currently in any GPU domain. As it
2794 * wasn't in the GTT, there shouldn't be any way it could have been in
2797 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2798 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2800 obj->gtt_offset = obj->gtt_space->start;
2803 obj->gtt_space->size == fence_size &&
2804 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
2807 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2809 obj->map_and_fenceable = mappable && fenceable;
2811 trace_i915_gem_object_bind(obj, map_and_fenceable);
2816 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2818 /* If we don't have a page list set up, then we're not pinned
2819 * to GPU, and we can ignore the cache flush because it'll happen
2820 * again at bind time.
2822 if (obj->pages == NULL)
2825 /* If the GPU is snooping the contents of the CPU cache,
2826 * we do not need to manually clear the CPU cache lines. However,
2827 * the caches are only snooped when the render cache is
2828 * flushed/invalidated. As we always have to emit invalidations
2829 * and flushes when moving into and out of the RENDER domain, correct
2830 * snooping behaviour occurs naturally as the result of our domain
2833 if (obj->cache_level != I915_CACHE_NONE)
2836 trace_i915_gem_object_clflush(obj);
2838 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2841 /** Flushes any GPU write domain for the object if it's dirty. */
2843 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2845 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2848 /* Queue the GPU write cache flushing we need. */
2849 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
2852 /** Flushes the GTT write domain for the object if it's dirty. */
2854 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2856 uint32_t old_write_domain;
2858 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2861 /* No actual flushing is required for the GTT write domain. Writes
2862 * to it immediately go to main memory as far as we know, so there's
2863 * no chipset flush. It also doesn't land in render cache.
2865 * However, we do have to enforce the order so that all writes through
2866 * the GTT land before any writes to the device, such as updates to
2871 old_write_domain = obj->base.write_domain;
2872 obj->base.write_domain = 0;
2874 trace_i915_gem_object_change_domain(obj,
2875 obj->base.read_domains,
2879 /** Flushes the CPU write domain for the object if it's dirty. */
2881 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2883 uint32_t old_write_domain;
2885 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2888 i915_gem_clflush_object(obj);
2889 intel_gtt_chipset_flush();
2890 old_write_domain = obj->base.write_domain;
2891 obj->base.write_domain = 0;
2893 trace_i915_gem_object_change_domain(obj,
2894 obj->base.read_domains,
2899 * Moves a single object to the GTT read, and possibly write domain.
2901 * This function returns when the move is complete, including waiting on
2905 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2907 uint32_t old_write_domain, old_read_domains;
2910 /* Not valid to be called on unbound objects. */
2911 if (obj->gtt_space == NULL)
2914 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2917 ret = i915_gem_object_flush_gpu_write_domain(obj);
2921 if (obj->pending_gpu_write || write) {
2922 ret = i915_gem_object_wait_rendering(obj);
2927 i915_gem_object_flush_cpu_write_domain(obj);
2929 old_write_domain = obj->base.write_domain;
2930 old_read_domains = obj->base.read_domains;
2932 /* It should now be out of any other write domains, and we can update
2933 * the domain values for our changes.
2935 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2936 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2938 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2939 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2943 trace_i915_gem_object_change_domain(obj,
2950 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2951 enum i915_cache_level cache_level)
2955 if (obj->cache_level == cache_level)
2958 if (obj->pin_count) {
2959 DRM_DEBUG("can not change the cache level of pinned objects\n");
2963 if (obj->gtt_space) {
2964 ret = i915_gem_object_finish_gpu(obj);
2968 i915_gem_object_finish_gtt(obj);
2970 /* Before SandyBridge, you could not use tiling or fence
2971 * registers with snooped memory, so relinquish any fences
2972 * currently pointing to our region in the aperture.
2974 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2975 ret = i915_gem_object_put_fence(obj);
2980 i915_gem_gtt_rebind_object(obj, cache_level);
2983 if (cache_level == I915_CACHE_NONE) {
2984 u32 old_read_domains, old_write_domain;
2986 /* If we're coming from LLC cached, then we haven't
2987 * actually been tracking whether the data is in the
2988 * CPU cache or not, since we only allow one bit set
2989 * in obj->write_domain and have been skipping the clflushes.
2990 * Just set it to the CPU cache for now.
2992 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2993 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2995 old_read_domains = obj->base.read_domains;
2996 old_write_domain = obj->base.write_domain;
2998 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2999 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3001 trace_i915_gem_object_change_domain(obj,
3006 obj->cache_level = cache_level;
3011 * Prepare buffer for display plane (scanout, cursors, etc).
3012 * Can be called from an uninterruptible phase (modesetting) and allows
3013 * any flushes to be pipelined (for pageflips).
3015 * For the display plane, we want to be in the GTT but out of any write
3016 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3017 * ability to pipeline the waits, pinning and any additional subtleties
3018 * that may differentiate the display plane from ordinary buffers.
3021 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3023 struct intel_ring_buffer *pipelined)
3025 u32 old_read_domains, old_write_domain;
3028 ret = i915_gem_object_flush_gpu_write_domain(obj);
3032 if (pipelined != obj->ring) {
3033 ret = i915_gem_object_wait_rendering(obj);
3034 if (ret == -ERESTARTSYS)
3038 /* The display engine is not coherent with the LLC cache on gen6. As
3039 * a result, we make sure that the pinning that is about to occur is
3040 * done with uncached PTEs. This is lowest common denominator for all
3043 * However for gen6+, we could do better by using the GFDT bit instead
3044 * of uncaching, which would allow us to flush all the LLC-cached data
3045 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3047 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3051 /* As the user may map the buffer once pinned in the display plane
3052 * (e.g. libkms for the bootup splash), we have to ensure that we
3053 * always use map_and_fenceable for all scanout buffers.
3055 ret = i915_gem_object_pin(obj, alignment, true);
3059 i915_gem_object_flush_cpu_write_domain(obj);
3061 old_write_domain = obj->base.write_domain;
3062 old_read_domains = obj->base.read_domains;
3064 /* It should now be out of any other write domains, and we can update
3065 * the domain values for our changes.
3067 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3068 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3070 trace_i915_gem_object_change_domain(obj,
3078 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3082 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3085 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3086 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
3091 /* Ensure that we invalidate the GPU's caches and TLBs. */
3092 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3094 return i915_gem_object_wait_rendering(obj);
3098 * Moves a single object to the CPU read, and possibly write domain.
3100 * This function returns when the move is complete, including waiting on
3104 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3106 uint32_t old_write_domain, old_read_domains;
3109 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3112 ret = i915_gem_object_flush_gpu_write_domain(obj);
3116 ret = i915_gem_object_wait_rendering(obj);
3120 i915_gem_object_flush_gtt_write_domain(obj);
3122 /* If we have a partially-valid cache of the object in the CPU,
3123 * finish invalidating it and free the per-page flags.
3125 i915_gem_object_set_to_full_cpu_read_domain(obj);
3127 old_write_domain = obj->base.write_domain;
3128 old_read_domains = obj->base.read_domains;
3130 /* Flush the CPU cache if it's still invalid. */
3131 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3132 i915_gem_clflush_object(obj);
3134 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3137 /* It should now be out of any other write domains, and we can update
3138 * the domain values for our changes.
3140 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3142 /* If we're writing through the CPU, then the GPU read domains will
3143 * need to be invalidated at next use.
3146 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3147 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3150 trace_i915_gem_object_change_domain(obj,
3158 * Moves the object from a partially CPU read to a full one.
3160 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3161 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3164 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3166 if (!obj->page_cpu_valid)
3169 /* If we're partially in the CPU read domain, finish moving it in.
3171 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3174 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3175 if (obj->page_cpu_valid[i])
3177 drm_clflush_pages(obj->pages + i, 1);
3181 /* Free the page_cpu_valid mappings which are now stale, whether
3182 * or not we've got I915_GEM_DOMAIN_CPU.
3184 kfree(obj->page_cpu_valid);
3185 obj->page_cpu_valid = NULL;
3189 * Set the CPU read domain on a range of the object.
3191 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3192 * not entirely valid. The page_cpu_valid member of the object flags which
3193 * pages have been flushed, and will be respected by
3194 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3195 * of the whole object.
3197 * This function returns when the move is complete, including waiting on
3201 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3202 uint64_t offset, uint64_t size)
3204 uint32_t old_read_domains;
3207 if (offset == 0 && size == obj->base.size)
3208 return i915_gem_object_set_to_cpu_domain(obj, 0);
3210 ret = i915_gem_object_flush_gpu_write_domain(obj);
3214 ret = i915_gem_object_wait_rendering(obj);
3218 i915_gem_object_flush_gtt_write_domain(obj);
3220 /* If we're already fully in the CPU read domain, we're done. */
3221 if (obj->page_cpu_valid == NULL &&
3222 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3225 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3226 * newly adding I915_GEM_DOMAIN_CPU
3228 if (obj->page_cpu_valid == NULL) {
3229 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3231 if (obj->page_cpu_valid == NULL)
3233 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3234 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3236 /* Flush the cache on any pages that are still invalid from the CPU's
3239 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3241 if (obj->page_cpu_valid[i])
3244 drm_clflush_pages(obj->pages + i, 1);
3246 obj->page_cpu_valid[i] = 1;
3249 /* It should now be out of any other write domains, and we can update
3250 * the domain values for our changes.
3252 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3254 old_read_domains = obj->base.read_domains;
3255 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3257 trace_i915_gem_object_change_domain(obj,
3259 obj->base.write_domain);
3264 /* Throttle our rendering by waiting until the ring has completed our requests
3265 * emitted over 20 msec ago.
3267 * Note that if we were to use the current jiffies each time around the loop,
3268 * we wouldn't escape the function with any frames outstanding if the time to
3269 * render a frame was over 20ms.
3271 * This should get us reasonable parallelism between CPU and GPU but also
3272 * relatively low latency when blocking on a particular request to finish.
3275 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3277 struct drm_i915_private *dev_priv = dev->dev_private;
3278 struct drm_i915_file_private *file_priv = file->driver_priv;
3279 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3280 struct drm_i915_gem_request *request;
3281 struct intel_ring_buffer *ring = NULL;
3285 if (atomic_read(&dev_priv->mm.wedged))
3288 spin_lock(&file_priv->mm.lock);
3289 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3290 if (time_after_eq(request->emitted_jiffies, recent_enough))
3293 ring = request->ring;
3294 seqno = request->seqno;
3296 spin_unlock(&file_priv->mm.lock);
3302 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3303 /* And wait for the seqno passing without holding any locks and
3304 * causing extra latency for others. This is safe as the irq
3305 * generation is designed to be run atomically and so is
3308 if (ring->irq_get(ring)) {
3309 ret = wait_event_interruptible(ring->irq_queue,
3310 i915_seqno_passed(ring->get_seqno(ring), seqno)
3311 || atomic_read(&dev_priv->mm.wedged));
3312 ring->irq_put(ring);
3314 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3320 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3326 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3328 bool map_and_fenceable)
3330 struct drm_device *dev = obj->base.dev;
3331 struct drm_i915_private *dev_priv = dev->dev_private;
3334 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
3335 WARN_ON(i915_verify_lists(dev));
3337 if (obj->gtt_space != NULL) {
3338 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3339 (map_and_fenceable && !obj->map_and_fenceable)) {
3340 WARN(obj->pin_count,
3341 "bo is already pinned with incorrect alignment:"
3342 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3343 " obj->map_and_fenceable=%d\n",
3344 obj->gtt_offset, alignment,
3346 obj->map_and_fenceable);
3347 ret = i915_gem_object_unbind(obj);
3353 if (obj->gtt_space == NULL) {
3354 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3360 if (obj->pin_count++ == 0) {
3362 list_move_tail(&obj->mm_list,
3363 &dev_priv->mm.pinned_list);
3365 obj->pin_mappable |= map_and_fenceable;
3367 WARN_ON(i915_verify_lists(dev));
3372 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3374 struct drm_device *dev = obj->base.dev;
3375 drm_i915_private_t *dev_priv = dev->dev_private;
3377 WARN_ON(i915_verify_lists(dev));
3378 BUG_ON(obj->pin_count == 0);
3379 BUG_ON(obj->gtt_space == NULL);
3381 if (--obj->pin_count == 0) {
3383 list_move_tail(&obj->mm_list,
3384 &dev_priv->mm.inactive_list);
3385 obj->pin_mappable = false;
3387 WARN_ON(i915_verify_lists(dev));
3391 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3392 struct drm_file *file)
3394 struct drm_i915_gem_pin *args = data;
3395 struct drm_i915_gem_object *obj;
3398 ret = i915_mutex_lock_interruptible(dev);
3402 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3403 if (&obj->base == NULL) {
3408 if (obj->madv != I915_MADV_WILLNEED) {
3409 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3414 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3415 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3421 obj->user_pin_count++;
3422 obj->pin_filp = file;
3423 if (obj->user_pin_count == 1) {
3424 ret = i915_gem_object_pin(obj, args->alignment, true);
3429 /* XXX - flush the CPU caches for pinned objects
3430 * as the X server doesn't manage domains yet
3432 i915_gem_object_flush_cpu_write_domain(obj);
3433 args->offset = obj->gtt_offset;
3435 drm_gem_object_unreference(&obj->base);
3437 mutex_unlock(&dev->struct_mutex);
3442 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3443 struct drm_file *file)
3445 struct drm_i915_gem_pin *args = data;
3446 struct drm_i915_gem_object *obj;
3449 ret = i915_mutex_lock_interruptible(dev);
3453 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3454 if (&obj->base == NULL) {
3459 if (obj->pin_filp != file) {
3460 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3465 obj->user_pin_count--;
3466 if (obj->user_pin_count == 0) {
3467 obj->pin_filp = NULL;
3468 i915_gem_object_unpin(obj);
3472 drm_gem_object_unreference(&obj->base);
3474 mutex_unlock(&dev->struct_mutex);
3479 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3480 struct drm_file *file)
3482 struct drm_i915_gem_busy *args = data;
3483 struct drm_i915_gem_object *obj;
3486 ret = i915_mutex_lock_interruptible(dev);
3490 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3491 if (&obj->base == NULL) {
3496 /* Count all active objects as busy, even if they are currently not used
3497 * by the gpu. Users of this interface expect objects to eventually
3498 * become non-busy without any further actions, therefore emit any
3499 * necessary flushes here.
3501 args->busy = obj->active;
3503 /* Unconditionally flush objects, even when the gpu still uses this
3504 * object. Userspace calling this function indicates that it wants to
3505 * use this buffer rather sooner than later, so issuing the required
3506 * flush earlier is beneficial.
3508 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3509 ret = i915_gem_flush_ring(obj->ring,
3510 0, obj->base.write_domain);
3511 } else if (obj->ring->outstanding_lazy_request ==
3512 obj->last_rendering_seqno) {
3513 struct drm_i915_gem_request *request;
3515 /* This ring is not being cleared by active usage,
3516 * so emit a request to do so.
3518 request = kzalloc(sizeof(*request), GFP_KERNEL);
3520 ret = i915_add_request(obj->ring, NULL, request);
3525 /* Update the active list for the hardware's current position.
3526 * Otherwise this only updates on a delayed timer or when irqs
3527 * are actually unmasked, and our working set ends up being
3528 * larger than required.
3530 i915_gem_retire_requests_ring(obj->ring);
3532 args->busy = obj->active;
3535 drm_gem_object_unreference(&obj->base);
3537 mutex_unlock(&dev->struct_mutex);
3542 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3543 struct drm_file *file_priv)
3545 return i915_gem_ring_throttle(dev, file_priv);
3549 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3550 struct drm_file *file_priv)
3552 struct drm_i915_gem_madvise *args = data;
3553 struct drm_i915_gem_object *obj;
3556 switch (args->madv) {
3557 case I915_MADV_DONTNEED:
3558 case I915_MADV_WILLNEED:
3564 ret = i915_mutex_lock_interruptible(dev);
3568 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3569 if (&obj->base == NULL) {
3574 if (obj->pin_count) {
3579 if (obj->madv != __I915_MADV_PURGED)
3580 obj->madv = args->madv;
3582 /* if the object is no longer bound, discard its backing storage */
3583 if (i915_gem_object_is_purgeable(obj) &&
3584 obj->gtt_space == NULL)
3585 i915_gem_object_truncate(obj);
3587 args->retained = obj->madv != __I915_MADV_PURGED;
3590 drm_gem_object_unreference(&obj->base);
3592 mutex_unlock(&dev->struct_mutex);
3596 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3599 struct drm_i915_private *dev_priv = dev->dev_private;
3600 struct drm_i915_gem_object *obj;
3601 struct address_space *mapping;
3603 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3607 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3612 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3613 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3615 i915_gem_info_add_obj(dev_priv, size);
3617 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3618 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3621 /* On Gen6, we can have the GPU use the LLC (the CPU
3622 * cache) for about a 10% performance improvement
3623 * compared to uncached. Graphics requests other than
3624 * display scanout are coherent with the CPU in
3625 * accessing this cache. This means in this mode we
3626 * don't need to clflush on the CPU side, and on the
3627 * GPU side we only need to flush internal caches to
3628 * get data visible to the CPU.
3630 * However, we maintain the display planes as UC, and so
3631 * need to rebind when first used as such.
3633 obj->cache_level = I915_CACHE_LLC;
3635 obj->cache_level = I915_CACHE_NONE;
3637 obj->base.driver_private = NULL;
3638 obj->fence_reg = I915_FENCE_REG_NONE;
3639 INIT_LIST_HEAD(&obj->mm_list);
3640 INIT_LIST_HEAD(&obj->gtt_list);
3641 INIT_LIST_HEAD(&obj->ring_list);
3642 INIT_LIST_HEAD(&obj->exec_list);
3643 INIT_LIST_HEAD(&obj->gpu_write_list);
3644 obj->madv = I915_MADV_WILLNEED;
3645 /* Avoid an unnecessary call to unbind on the first bind. */
3646 obj->map_and_fenceable = true;
3651 int i915_gem_init_object(struct drm_gem_object *obj)
3658 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3660 struct drm_device *dev = obj->base.dev;
3661 drm_i915_private_t *dev_priv = dev->dev_private;
3664 ret = i915_gem_object_unbind(obj);
3665 if (ret == -ERESTARTSYS) {
3666 list_move(&obj->mm_list,
3667 &dev_priv->mm.deferred_free_list);
3671 trace_i915_gem_object_destroy(obj);
3673 if (obj->base.map_list.map)
3674 drm_gem_free_mmap_offset(&obj->base);
3676 drm_gem_object_release(&obj->base);
3677 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3679 kfree(obj->page_cpu_valid);
3684 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3686 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3687 struct drm_device *dev = obj->base.dev;
3689 while (obj->pin_count > 0)
3690 i915_gem_object_unpin(obj);
3693 i915_gem_detach_phys_object(dev, obj);
3695 i915_gem_free_object_tail(obj);
3699 i915_gem_idle(struct drm_device *dev)
3701 drm_i915_private_t *dev_priv = dev->dev_private;
3704 mutex_lock(&dev->struct_mutex);
3706 if (dev_priv->mm.suspended) {
3707 mutex_unlock(&dev->struct_mutex);
3711 ret = i915_gpu_idle(dev);
3713 mutex_unlock(&dev->struct_mutex);
3717 /* Under UMS, be paranoid and evict. */
3718 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3719 ret = i915_gem_evict_inactive(dev, false);
3721 mutex_unlock(&dev->struct_mutex);
3726 i915_gem_reset_fences(dev);
3728 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3729 * We need to replace this with a semaphore, or something.
3730 * And not confound mm.suspended!
3732 dev_priv->mm.suspended = 1;
3733 del_timer_sync(&dev_priv->hangcheck_timer);
3735 i915_kernel_lost_context(dev);
3736 i915_gem_cleanup_ringbuffer(dev);
3738 mutex_unlock(&dev->struct_mutex);
3740 /* Cancel the retire work handler, which should be idle now. */
3741 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3747 i915_gem_init_ringbuffer(struct drm_device *dev)
3749 drm_i915_private_t *dev_priv = dev->dev_private;
3752 ret = intel_init_render_ring_buffer(dev);
3757 ret = intel_init_bsd_ring_buffer(dev);
3759 goto cleanup_render_ring;
3763 ret = intel_init_blt_ring_buffer(dev);
3765 goto cleanup_bsd_ring;
3768 dev_priv->next_seqno = 1;
3773 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3774 cleanup_render_ring:
3775 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3780 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3782 drm_i915_private_t *dev_priv = dev->dev_private;
3785 for (i = 0; i < I915_NUM_RINGS; i++)
3786 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
3790 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3791 struct drm_file *file_priv)
3793 drm_i915_private_t *dev_priv = dev->dev_private;
3796 if (drm_core_check_feature(dev, DRIVER_MODESET))
3799 if (atomic_read(&dev_priv->mm.wedged)) {
3800 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3801 atomic_set(&dev_priv->mm.wedged, 0);
3804 mutex_lock(&dev->struct_mutex);
3805 dev_priv->mm.suspended = 0;
3807 ret = i915_gem_init_ringbuffer(dev);
3809 mutex_unlock(&dev->struct_mutex);
3813 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3814 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3815 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3816 for (i = 0; i < I915_NUM_RINGS; i++) {
3817 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3818 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3820 mutex_unlock(&dev->struct_mutex);
3822 ret = drm_irq_install(dev);
3824 goto cleanup_ringbuffer;
3829 mutex_lock(&dev->struct_mutex);
3830 i915_gem_cleanup_ringbuffer(dev);
3831 dev_priv->mm.suspended = 1;
3832 mutex_unlock(&dev->struct_mutex);
3838 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3839 struct drm_file *file_priv)
3841 if (drm_core_check_feature(dev, DRIVER_MODESET))
3844 drm_irq_uninstall(dev);
3845 return i915_gem_idle(dev);
3849 i915_gem_lastclose(struct drm_device *dev)
3853 if (drm_core_check_feature(dev, DRIVER_MODESET))
3856 ret = i915_gem_idle(dev);
3858 DRM_ERROR("failed to idle hardware: %d\n", ret);
3862 init_ring_lists(struct intel_ring_buffer *ring)
3864 INIT_LIST_HEAD(&ring->active_list);
3865 INIT_LIST_HEAD(&ring->request_list);
3866 INIT_LIST_HEAD(&ring->gpu_write_list);
3870 i915_gem_load(struct drm_device *dev)
3873 drm_i915_private_t *dev_priv = dev->dev_private;
3875 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3876 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3877 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3878 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3879 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3880 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
3881 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3882 for (i = 0; i < I915_NUM_RINGS; i++)
3883 init_ring_lists(&dev_priv->ring[i]);
3884 for (i = 0; i < 16; i++)
3885 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3886 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3887 i915_gem_retire_work_handler);
3888 init_completion(&dev_priv->error_completion);
3890 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3892 u32 tmp = I915_READ(MI_ARB_STATE);
3893 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3894 /* arb state is a masked write, so set bit + bit in mask */
3895 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3896 I915_WRITE(MI_ARB_STATE, tmp);
3900 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3902 /* Old X drivers will take 0-2 for front, back, depth buffers */
3903 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3904 dev_priv->fence_reg_start = 3;
3906 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3907 dev_priv->num_fence_regs = 16;
3909 dev_priv->num_fence_regs = 8;
3911 /* Initialize fence registers to zero */
3912 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3913 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
3916 i915_gem_detect_bit_6_swizzle(dev);
3917 init_waitqueue_head(&dev_priv->pending_flip_queue);
3919 dev_priv->mm.interruptible = true;
3921 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3922 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3923 register_shrinker(&dev_priv->mm.inactive_shrinker);
3927 * Create a physically contiguous memory object for this object
3928 * e.g. for cursor + overlay regs
3930 static int i915_gem_init_phys_object(struct drm_device *dev,
3931 int id, int size, int align)
3933 drm_i915_private_t *dev_priv = dev->dev_private;
3934 struct drm_i915_gem_phys_object *phys_obj;
3937 if (dev_priv->mm.phys_objs[id - 1] || !size)
3940 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
3946 phys_obj->handle = drm_pci_alloc(dev, size, align);
3947 if (!phys_obj->handle) {
3952 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3955 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3963 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
3965 drm_i915_private_t *dev_priv = dev->dev_private;
3966 struct drm_i915_gem_phys_object *phys_obj;
3968 if (!dev_priv->mm.phys_objs[id - 1])
3971 phys_obj = dev_priv->mm.phys_objs[id - 1];
3972 if (phys_obj->cur_obj) {
3973 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3977 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3979 drm_pci_free(dev, phys_obj->handle);
3981 dev_priv->mm.phys_objs[id - 1] = NULL;
3984 void i915_gem_free_all_phys_object(struct drm_device *dev)
3988 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
3989 i915_gem_free_phys_object(dev, i);
3992 void i915_gem_detach_phys_object(struct drm_device *dev,
3993 struct drm_i915_gem_object *obj)
3995 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4002 vaddr = obj->phys_obj->handle->vaddr;
4004 page_count = obj->base.size / PAGE_SIZE;
4005 for (i = 0; i < page_count; i++) {
4006 struct page *page = shmem_read_mapping_page(mapping, i);
4007 if (!IS_ERR(page)) {
4008 char *dst = kmap_atomic(page);
4009 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4012 drm_clflush_pages(&page, 1);
4014 set_page_dirty(page);
4015 mark_page_accessed(page);
4016 page_cache_release(page);
4019 intel_gtt_chipset_flush();
4021 obj->phys_obj->cur_obj = NULL;
4022 obj->phys_obj = NULL;
4026 i915_gem_attach_phys_object(struct drm_device *dev,
4027 struct drm_i915_gem_object *obj,
4031 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4032 drm_i915_private_t *dev_priv = dev->dev_private;
4037 if (id > I915_MAX_PHYS_OBJECT)
4040 if (obj->phys_obj) {
4041 if (obj->phys_obj->id == id)
4043 i915_gem_detach_phys_object(dev, obj);
4046 /* create a new object */
4047 if (!dev_priv->mm.phys_objs[id - 1]) {
4048 ret = i915_gem_init_phys_object(dev, id,
4049 obj->base.size, align);
4051 DRM_ERROR("failed to init phys object %d size: %zu\n",
4052 id, obj->base.size);
4057 /* bind to the object */
4058 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4059 obj->phys_obj->cur_obj = obj;
4061 page_count = obj->base.size / PAGE_SIZE;
4063 for (i = 0; i < page_count; i++) {
4067 page = shmem_read_mapping_page(mapping, i);
4069 return PTR_ERR(page);
4071 src = kmap_atomic(page);
4072 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4073 memcpy(dst, src, PAGE_SIZE);
4076 mark_page_accessed(page);
4077 page_cache_release(page);
4084 i915_gem_phys_pwrite(struct drm_device *dev,
4085 struct drm_i915_gem_object *obj,
4086 struct drm_i915_gem_pwrite *args,
4087 struct drm_file *file_priv)
4089 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4090 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4092 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4093 unsigned long unwritten;
4095 /* The physical object once assigned is fixed for the lifetime
4096 * of the obj, so we can safely drop the lock and continue
4099 mutex_unlock(&dev->struct_mutex);
4100 unwritten = copy_from_user(vaddr, user_data, args->size);
4101 mutex_lock(&dev->struct_mutex);
4106 intel_gtt_chipset_flush();
4110 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4112 struct drm_i915_file_private *file_priv = file->driver_priv;
4114 /* Clean up our request list when the client is going away, so that
4115 * later retire_requests won't dereference our soon-to-be-gone
4118 spin_lock(&file_priv->mm.lock);
4119 while (!list_empty(&file_priv->mm.request_list)) {
4120 struct drm_i915_gem_request *request;
4122 request = list_first_entry(&file_priv->mm.request_list,
4123 struct drm_i915_gem_request,
4125 list_del(&request->client_list);
4126 request->file_priv = NULL;
4128 spin_unlock(&file_priv->mm.lock);
4132 i915_gpu_is_active(struct drm_device *dev)
4134 drm_i915_private_t *dev_priv = dev->dev_private;
4137 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4138 list_empty(&dev_priv->mm.active_list);
4140 return !lists_empty;
4144 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4146 struct drm_i915_private *dev_priv =
4147 container_of(shrinker,
4148 struct drm_i915_private,
4149 mm.inactive_shrinker);
4150 struct drm_device *dev = dev_priv->dev;
4151 struct drm_i915_gem_object *obj, *next;
4152 int nr_to_scan = sc->nr_to_scan;
4155 if (!mutex_trylock(&dev->struct_mutex))
4158 /* "fast-path" to count number of available objects */
4159 if (nr_to_scan == 0) {
4161 list_for_each_entry(obj,
4162 &dev_priv->mm.inactive_list,
4165 mutex_unlock(&dev->struct_mutex);
4166 return cnt / 100 * sysctl_vfs_cache_pressure;
4170 /* first scan for clean buffers */
4171 i915_gem_retire_requests(dev);
4173 list_for_each_entry_safe(obj, next,
4174 &dev_priv->mm.inactive_list,
4176 if (i915_gem_object_is_purgeable(obj)) {
4177 if (i915_gem_object_unbind(obj) == 0 &&
4183 /* second pass, evict/count anything still on the inactive list */
4185 list_for_each_entry_safe(obj, next,
4186 &dev_priv->mm.inactive_list,
4189 i915_gem_object_unbind(obj) == 0)
4195 if (nr_to_scan && i915_gpu_is_active(dev)) {
4197 * We are desperate for pages, so as a last resort, wait
4198 * for the GPU to finish and discard whatever we can.
4199 * This has a dramatic impact to reduce the number of
4200 * OOM-killer events whilst running the GPU aggressively.
4202 if (i915_gpu_idle(dev) == 0)
4205 mutex_unlock(&dev->struct_mutex);
4206 return cnt / 100 * sysctl_vfs_cache_pressure;