2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
38 struct change_domains {
39 uint32_t invalidate_domains;
40 uint32_t flush_domains;
44 static uint32_t i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj);
45 static uint32_t i915_gem_get_gtt_size(struct drm_i915_gem_object *obj);
47 static int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
49 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
50 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
51 static int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
53 static int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
56 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
57 static int i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
59 static int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
61 bool map_and_fenceable);
62 static void i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj);
63 static int i915_gem_phys_pwrite(struct drm_device *dev,
64 struct drm_i915_gem_object *obj,
65 struct drm_i915_gem_pwrite *args,
66 struct drm_file *file);
67 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
69 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
74 /* some bookkeeping */
75 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
78 dev_priv->mm.object_count++;
79 dev_priv->mm.object_memory += size;
82 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
85 dev_priv->mm.object_count--;
86 dev_priv->mm.object_memory -= size;
89 static void i915_gem_info_add_gtt(struct drm_i915_private *dev_priv,
90 struct drm_i915_gem_object *obj)
92 dev_priv->mm.gtt_count++;
93 dev_priv->mm.gtt_memory += obj->gtt_space->size;
94 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
95 dev_priv->mm.mappable_gtt_used +=
96 min_t(size_t, obj->gtt_space->size,
97 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
99 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
102 static void i915_gem_info_remove_gtt(struct drm_i915_private *dev_priv,
103 struct drm_i915_gem_object *obj)
105 dev_priv->mm.gtt_count--;
106 dev_priv->mm.gtt_memory -= obj->gtt_space->size;
107 if (obj->gtt_offset < dev_priv->mm.gtt_mappable_end) {
108 dev_priv->mm.mappable_gtt_used -=
109 min_t(size_t, obj->gtt_space->size,
110 dev_priv->mm.gtt_mappable_end - obj->gtt_offset);
112 list_del_init(&obj->gtt_list);
116 * Update the mappable working set counters. Call _only_ when there is a change
117 * in one of (pin|fault)_mappable and update *_mappable _before_ calling.
118 * @mappable: new state the changed mappable flag (either pin_ or fault_).
121 i915_gem_info_update_mappable(struct drm_i915_private *dev_priv,
122 struct drm_i915_gem_object *obj,
126 if (obj->pin_mappable && obj->fault_mappable)
127 /* Combined state was already mappable. */
129 dev_priv->mm.gtt_mappable_count++;
130 dev_priv->mm.gtt_mappable_memory += obj->gtt_space->size;
132 if (obj->pin_mappable || obj->fault_mappable)
133 /* Combined state still mappable. */
135 dev_priv->mm.gtt_mappable_count--;
136 dev_priv->mm.gtt_mappable_memory -= obj->gtt_space->size;
140 static void i915_gem_info_add_pin(struct drm_i915_private *dev_priv,
141 struct drm_i915_gem_object *obj,
144 dev_priv->mm.pin_count++;
145 dev_priv->mm.pin_memory += obj->gtt_space->size;
147 obj->pin_mappable = true;
148 i915_gem_info_update_mappable(dev_priv, obj, true);
152 static void i915_gem_info_remove_pin(struct drm_i915_private *dev_priv,
153 struct drm_i915_gem_object *obj)
155 dev_priv->mm.pin_count--;
156 dev_priv->mm.pin_memory -= obj->gtt_space->size;
157 if (obj->pin_mappable) {
158 obj->pin_mappable = false;
159 i915_gem_info_update_mappable(dev_priv, obj, false);
164 i915_gem_check_is_wedged(struct drm_device *dev)
166 struct drm_i915_private *dev_priv = dev->dev_private;
167 struct completion *x = &dev_priv->error_completion;
171 if (!atomic_read(&dev_priv->mm.wedged))
174 ret = wait_for_completion_interruptible(x);
178 /* Success, we reset the GPU! */
179 if (!atomic_read(&dev_priv->mm.wedged))
182 /* GPU is hung, bump the completion count to account for
183 * the token we just consumed so that we never hit zero and
184 * end up waiting upon a subsequent completion event that
187 spin_lock_irqsave(&x->wait.lock, flags);
189 spin_unlock_irqrestore(&x->wait.lock, flags);
193 static int i915_mutex_lock_interruptible(struct drm_device *dev)
195 struct drm_i915_private *dev_priv = dev->dev_private;
198 ret = i915_gem_check_is_wedged(dev);
202 ret = mutex_lock_interruptible(&dev->struct_mutex);
206 if (atomic_read(&dev_priv->mm.wedged)) {
207 mutex_unlock(&dev->struct_mutex);
211 WARN_ON(i915_verify_lists(dev));
216 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
218 return obj->gtt_space && !obj->active && obj->pin_count == 0;
221 int i915_gem_do_init(struct drm_device *dev,
223 unsigned long mappable_end,
226 drm_i915_private_t *dev_priv = dev->dev_private;
229 (start & (PAGE_SIZE - 1)) != 0 ||
230 (end & (PAGE_SIZE - 1)) != 0) {
234 drm_mm_init(&dev_priv->mm.gtt_space, start,
237 dev_priv->mm.gtt_total = end - start;
238 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
239 dev_priv->mm.gtt_mappable_end = mappable_end;
245 i915_gem_init_ioctl(struct drm_device *dev, void *data,
246 struct drm_file *file)
248 struct drm_i915_gem_init *args = data;
251 mutex_lock(&dev->struct_mutex);
252 ret = i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
253 mutex_unlock(&dev->struct_mutex);
259 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
260 struct drm_file *file)
262 struct drm_i915_private *dev_priv = dev->dev_private;
263 struct drm_i915_gem_get_aperture *args = data;
265 if (!(dev->driver->driver_features & DRIVER_GEM))
268 mutex_lock(&dev->struct_mutex);
269 args->aper_size = dev_priv->mm.gtt_total;
270 args->aper_available_size = args->aper_size - dev_priv->mm.pin_memory;
271 mutex_unlock(&dev->struct_mutex);
278 * Creates a new mm object and returns a handle to it.
281 i915_gem_create_ioctl(struct drm_device *dev, void *data,
282 struct drm_file *file)
284 struct drm_i915_gem_create *args = data;
285 struct drm_i915_gem_object *obj;
289 args->size = roundup(args->size, PAGE_SIZE);
291 /* Allocate the new object */
292 obj = i915_gem_alloc_object(dev, args->size);
296 ret = drm_gem_handle_create(file, &obj->base, &handle);
298 drm_gem_object_release(&obj->base);
299 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
304 /* drop reference from allocate - handle holds it now */
305 drm_gem_object_unreference(&obj->base);
306 trace_i915_gem_object_create(obj);
308 args->handle = handle;
312 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
314 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
316 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
317 obj->tiling_mode != I915_TILING_NONE;
321 slow_shmem_copy(struct page *dst_page,
323 struct page *src_page,
327 char *dst_vaddr, *src_vaddr;
329 dst_vaddr = kmap(dst_page);
330 src_vaddr = kmap(src_page);
332 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
339 slow_shmem_bit17_copy(struct page *gpu_page,
341 struct page *cpu_page,
346 char *gpu_vaddr, *cpu_vaddr;
348 /* Use the unswizzled path if this page isn't affected. */
349 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
351 return slow_shmem_copy(cpu_page, cpu_offset,
352 gpu_page, gpu_offset, length);
354 return slow_shmem_copy(gpu_page, gpu_offset,
355 cpu_page, cpu_offset, length);
358 gpu_vaddr = kmap(gpu_page);
359 cpu_vaddr = kmap(cpu_page);
361 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
362 * XORing with the other bits (A9 for Y, A9 and A10 for X)
365 int cacheline_end = ALIGN(gpu_offset + 1, 64);
366 int this_length = min(cacheline_end - gpu_offset, length);
367 int swizzled_gpu_offset = gpu_offset ^ 64;
370 memcpy(cpu_vaddr + cpu_offset,
371 gpu_vaddr + swizzled_gpu_offset,
374 memcpy(gpu_vaddr + swizzled_gpu_offset,
375 cpu_vaddr + cpu_offset,
378 cpu_offset += this_length;
379 gpu_offset += this_length;
380 length -= this_length;
388 * This is the fast shmem pread path, which attempts to copy_from_user directly
389 * from the backing pages of the object to the user's address space. On a
390 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
393 i915_gem_shmem_pread_fast(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;
401 char __user *user_data;
402 int page_offset, page_length;
404 user_data = (char __user *) (uintptr_t) args->data_ptr;
407 offset = args->offset;
414 /* Operation in this page
416 * page_offset = offset within page
417 * page_length = bytes to copy for this page
419 page_offset = offset & (PAGE_SIZE-1);
420 page_length = remain;
421 if ((page_offset + remain) > PAGE_SIZE)
422 page_length = PAGE_SIZE - page_offset;
424 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
425 GFP_HIGHUSER | __GFP_RECLAIMABLE);
427 return PTR_ERR(page);
429 vaddr = kmap_atomic(page);
430 ret = __copy_to_user_inatomic(user_data,
433 kunmap_atomic(vaddr);
435 mark_page_accessed(page);
436 page_cache_release(page);
440 remain -= page_length;
441 user_data += page_length;
442 offset += page_length;
449 * This is the fallback shmem pread path, which allocates temporary storage
450 * in kernel space to copy_to_user into outside of the struct_mutex, so we
451 * can copy out of the object's backing pages while holding the struct mutex
452 * and not take page faults.
455 i915_gem_shmem_pread_slow(struct drm_device *dev,
456 struct drm_i915_gem_object *obj,
457 struct drm_i915_gem_pread *args,
458 struct drm_file *file)
460 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
461 struct mm_struct *mm = current->mm;
462 struct page **user_pages;
464 loff_t offset, pinned_pages, i;
465 loff_t first_data_page, last_data_page, num_pages;
466 int shmem_page_offset;
467 int data_page_index, data_page_offset;
470 uint64_t data_ptr = args->data_ptr;
471 int do_bit17_swizzling;
475 /* Pin the user pages containing the data. We can't fault while
476 * holding the struct mutex, yet we want to hold it while
477 * dereferencing the user data.
479 first_data_page = data_ptr / PAGE_SIZE;
480 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
481 num_pages = last_data_page - first_data_page + 1;
483 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
484 if (user_pages == NULL)
487 mutex_unlock(&dev->struct_mutex);
488 down_read(&mm->mmap_sem);
489 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
490 num_pages, 1, 0, user_pages, NULL);
491 up_read(&mm->mmap_sem);
492 mutex_lock(&dev->struct_mutex);
493 if (pinned_pages < num_pages) {
498 ret = i915_gem_object_set_cpu_read_domain_range(obj,
504 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
506 offset = args->offset;
511 /* Operation in this page
513 * shmem_page_offset = offset within page in shmem file
514 * data_page_index = page number in get_user_pages return
515 * data_page_offset = offset with data_page_index page.
516 * page_length = bytes to copy for this page
518 shmem_page_offset = offset & ~PAGE_MASK;
519 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
520 data_page_offset = data_ptr & ~PAGE_MASK;
522 page_length = remain;
523 if ((shmem_page_offset + page_length) > PAGE_SIZE)
524 page_length = PAGE_SIZE - shmem_page_offset;
525 if ((data_page_offset + page_length) > PAGE_SIZE)
526 page_length = PAGE_SIZE - data_page_offset;
528 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
529 GFP_HIGHUSER | __GFP_RECLAIMABLE);
531 return PTR_ERR(page);
533 if (do_bit17_swizzling) {
534 slow_shmem_bit17_copy(page,
536 user_pages[data_page_index],
541 slow_shmem_copy(user_pages[data_page_index],
548 mark_page_accessed(page);
549 page_cache_release(page);
551 remain -= page_length;
552 data_ptr += page_length;
553 offset += page_length;
557 for (i = 0; i < pinned_pages; i++) {
558 SetPageDirty(user_pages[i]);
559 mark_page_accessed(user_pages[i]);
560 page_cache_release(user_pages[i]);
562 drm_free_large(user_pages);
568 * Reads data from the object referenced by handle.
570 * On error, the contents of *data are undefined.
573 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
574 struct drm_file *file)
576 struct drm_i915_gem_pread *args = data;
577 struct drm_i915_gem_object *obj;
583 if (!access_ok(VERIFY_WRITE,
584 (char __user *)(uintptr_t)args->data_ptr,
588 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
593 ret = i915_mutex_lock_interruptible(dev);
597 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
603 /* Bounds check source. */
604 if (args->offset > obj->base.size ||
605 args->size > obj->base.size - args->offset) {
610 ret = i915_gem_object_set_cpu_read_domain_range(obj,
617 if (!i915_gem_object_needs_bit17_swizzle(obj))
618 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
620 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
623 drm_gem_object_unreference(&obj->base);
625 mutex_unlock(&dev->struct_mutex);
629 /* This is the fast write path which cannot handle
630 * page faults in the source data
634 fast_user_write(struct io_mapping *mapping,
635 loff_t page_base, int page_offset,
636 char __user *user_data,
640 unsigned long unwritten;
642 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
643 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
645 io_mapping_unmap_atomic(vaddr_atomic);
649 /* Here's the write path which can sleep for
654 slow_kernel_write(struct io_mapping *mapping,
655 loff_t gtt_base, int gtt_offset,
656 struct page *user_page, int user_offset,
659 char __iomem *dst_vaddr;
662 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
663 src_vaddr = kmap(user_page);
665 memcpy_toio(dst_vaddr + gtt_offset,
666 src_vaddr + user_offset,
670 io_mapping_unmap(dst_vaddr);
674 * This is the fast pwrite path, where we copy the data directly from the
675 * user into the GTT, uncached.
678 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
679 struct drm_i915_gem_object *obj,
680 struct drm_i915_gem_pwrite *args,
681 struct drm_file *file)
683 drm_i915_private_t *dev_priv = dev->dev_private;
685 loff_t offset, page_base;
686 char __user *user_data;
687 int page_offset, page_length;
689 user_data = (char __user *) (uintptr_t) args->data_ptr;
692 offset = obj->gtt_offset + args->offset;
695 /* Operation in this page
697 * page_base = page offset within aperture
698 * page_offset = offset within page
699 * page_length = bytes to copy for this page
701 page_base = (offset & ~(PAGE_SIZE-1));
702 page_offset = offset & (PAGE_SIZE-1);
703 page_length = remain;
704 if ((page_offset + remain) > PAGE_SIZE)
705 page_length = PAGE_SIZE - page_offset;
707 /* If we get a fault while copying data, then (presumably) our
708 * source page isn't available. Return the error and we'll
709 * retry in the slow path.
711 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
712 page_offset, user_data, page_length))
716 remain -= page_length;
717 user_data += page_length;
718 offset += page_length;
725 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
726 * the memory and maps it using kmap_atomic for copying.
728 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
729 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
732 i915_gem_gtt_pwrite_slow(struct drm_device *dev,
733 struct drm_i915_gem_object *obj,
734 struct drm_i915_gem_pwrite *args,
735 struct drm_file *file)
737 drm_i915_private_t *dev_priv = dev->dev_private;
739 loff_t gtt_page_base, offset;
740 loff_t first_data_page, last_data_page, num_pages;
741 loff_t pinned_pages, i;
742 struct page **user_pages;
743 struct mm_struct *mm = current->mm;
744 int gtt_page_offset, data_page_offset, data_page_index, page_length;
746 uint64_t data_ptr = args->data_ptr;
750 /* Pin the user pages containing the data. We can't fault while
751 * holding the struct mutex, and all of the pwrite implementations
752 * want to hold it while dereferencing the user data.
754 first_data_page = data_ptr / PAGE_SIZE;
755 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
756 num_pages = last_data_page - first_data_page + 1;
758 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
759 if (user_pages == NULL)
762 mutex_unlock(&dev->struct_mutex);
763 down_read(&mm->mmap_sem);
764 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
765 num_pages, 0, 0, user_pages, NULL);
766 up_read(&mm->mmap_sem);
767 mutex_lock(&dev->struct_mutex);
768 if (pinned_pages < num_pages) {
770 goto out_unpin_pages;
773 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
775 goto out_unpin_pages;
777 offset = obj->gtt_offset + args->offset;
780 /* Operation in this page
782 * gtt_page_base = page offset within aperture
783 * gtt_page_offset = offset within page in aperture
784 * data_page_index = page number in get_user_pages return
785 * data_page_offset = offset with data_page_index page.
786 * page_length = bytes to copy for this page
788 gtt_page_base = offset & PAGE_MASK;
789 gtt_page_offset = offset & ~PAGE_MASK;
790 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
791 data_page_offset = data_ptr & ~PAGE_MASK;
793 page_length = remain;
794 if ((gtt_page_offset + page_length) > PAGE_SIZE)
795 page_length = PAGE_SIZE - gtt_page_offset;
796 if ((data_page_offset + page_length) > PAGE_SIZE)
797 page_length = PAGE_SIZE - data_page_offset;
799 slow_kernel_write(dev_priv->mm.gtt_mapping,
800 gtt_page_base, gtt_page_offset,
801 user_pages[data_page_index],
805 remain -= page_length;
806 offset += page_length;
807 data_ptr += page_length;
811 for (i = 0; i < pinned_pages; i++)
812 page_cache_release(user_pages[i]);
813 drm_free_large(user_pages);
819 * This is the fast shmem pwrite path, which attempts to directly
820 * copy_from_user into the kmapped pages backing the object.
823 i915_gem_shmem_pwrite_fast(struct drm_device *dev,
824 struct drm_i915_gem_object *obj,
825 struct drm_i915_gem_pwrite *args,
826 struct drm_file *file)
828 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
831 char __user *user_data;
832 int page_offset, page_length;
834 user_data = (char __user *) (uintptr_t) args->data_ptr;
837 offset = args->offset;
845 /* Operation in this page
847 * page_offset = offset within page
848 * page_length = bytes to copy for this page
850 page_offset = offset & (PAGE_SIZE-1);
851 page_length = remain;
852 if ((page_offset + remain) > PAGE_SIZE)
853 page_length = PAGE_SIZE - page_offset;
855 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
856 GFP_HIGHUSER | __GFP_RECLAIMABLE);
858 return PTR_ERR(page);
860 vaddr = kmap_atomic(page, KM_USER0);
861 ret = __copy_from_user_inatomic(vaddr + page_offset,
864 kunmap_atomic(vaddr, KM_USER0);
866 set_page_dirty(page);
867 mark_page_accessed(page);
868 page_cache_release(page);
870 /* If we get a fault while copying data, then (presumably) our
871 * source page isn't available. Return the error and we'll
872 * retry in the slow path.
877 remain -= page_length;
878 user_data += page_length;
879 offset += page_length;
886 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
887 * the memory and maps it using kmap_atomic for copying.
889 * This avoids taking mmap_sem for faulting on the user's address while the
890 * struct_mutex is held.
893 i915_gem_shmem_pwrite_slow(struct drm_device *dev,
894 struct drm_i915_gem_object *obj,
895 struct drm_i915_gem_pwrite *args,
896 struct drm_file *file)
898 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
899 struct mm_struct *mm = current->mm;
900 struct page **user_pages;
902 loff_t offset, pinned_pages, i;
903 loff_t first_data_page, last_data_page, num_pages;
904 int shmem_page_offset;
905 int data_page_index, data_page_offset;
908 uint64_t data_ptr = args->data_ptr;
909 int do_bit17_swizzling;
913 /* Pin the user pages containing the data. We can't fault while
914 * holding the struct mutex, and all of the pwrite implementations
915 * want to hold it while dereferencing the user data.
917 first_data_page = data_ptr / PAGE_SIZE;
918 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
919 num_pages = last_data_page - first_data_page + 1;
921 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
922 if (user_pages == NULL)
925 mutex_unlock(&dev->struct_mutex);
926 down_read(&mm->mmap_sem);
927 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
928 num_pages, 0, 0, user_pages, NULL);
929 up_read(&mm->mmap_sem);
930 mutex_lock(&dev->struct_mutex);
931 if (pinned_pages < num_pages) {
936 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
940 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
942 offset = args->offset;
948 /* Operation in this page
950 * shmem_page_offset = offset within page in shmem file
951 * data_page_index = page number in get_user_pages return
952 * data_page_offset = offset with data_page_index page.
953 * page_length = bytes to copy for this page
955 shmem_page_offset = offset & ~PAGE_MASK;
956 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
957 data_page_offset = data_ptr & ~PAGE_MASK;
959 page_length = remain;
960 if ((shmem_page_offset + page_length) > PAGE_SIZE)
961 page_length = PAGE_SIZE - shmem_page_offset;
962 if ((data_page_offset + page_length) > PAGE_SIZE)
963 page_length = PAGE_SIZE - data_page_offset;
965 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
966 GFP_HIGHUSER | __GFP_RECLAIMABLE);
972 if (do_bit17_swizzling) {
973 slow_shmem_bit17_copy(page,
975 user_pages[data_page_index],
980 slow_shmem_copy(page,
982 user_pages[data_page_index],
987 set_page_dirty(page);
988 mark_page_accessed(page);
989 page_cache_release(page);
991 remain -= page_length;
992 data_ptr += page_length;
993 offset += page_length;
997 for (i = 0; i < pinned_pages; i++)
998 page_cache_release(user_pages[i]);
999 drm_free_large(user_pages);
1005 * Writes data to the object referenced by handle.
1007 * On error, the contents of the buffer that were to be modified are undefined.
1010 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
1011 struct drm_file *file)
1013 struct drm_i915_gem_pwrite *args = data;
1014 struct drm_i915_gem_object *obj;
1017 if (args->size == 0)
1020 if (!access_ok(VERIFY_READ,
1021 (char __user *)(uintptr_t)args->data_ptr,
1025 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
1030 ret = i915_mutex_lock_interruptible(dev);
1034 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1040 /* Bounds check destination. */
1041 if (args->offset > obj->base.size ||
1042 args->size > obj->base.size - args->offset) {
1047 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1048 * it would end up going through the fenced access, and we'll get
1049 * different detiling behavior between reading and writing.
1050 * pread/pwrite currently are reading and writing from the CPU
1051 * perspective, requiring manual detiling by the client.
1054 ret = i915_gem_phys_pwrite(dev, obj, args, file);
1055 else if (obj->tiling_mode == I915_TILING_NONE &&
1057 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1058 ret = i915_gem_object_pin(obj, 0, true);
1062 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
1066 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1068 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1071 i915_gem_object_unpin(obj);
1073 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1078 if (!i915_gem_object_needs_bit17_swizzle(obj))
1079 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1081 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1085 drm_gem_object_unreference(&obj->base);
1087 mutex_unlock(&dev->struct_mutex);
1092 * Called when user space prepares to use an object with the CPU, either
1093 * through the mmap ioctl's mapping or a GTT mapping.
1096 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1097 struct drm_file *file)
1099 struct drm_i915_private *dev_priv = dev->dev_private;
1100 struct drm_i915_gem_set_domain *args = data;
1101 struct drm_i915_gem_object *obj;
1102 uint32_t read_domains = args->read_domains;
1103 uint32_t write_domain = args->write_domain;
1106 if (!(dev->driver->driver_features & DRIVER_GEM))
1109 /* Only handle setting domains to types used by the CPU. */
1110 if (write_domain & I915_GEM_GPU_DOMAINS)
1113 if (read_domains & I915_GEM_GPU_DOMAINS)
1116 /* Having something in the write domain implies it's in the read
1117 * domain, and only that read domain. Enforce that in the request.
1119 if (write_domain != 0 && read_domains != write_domain)
1122 ret = i915_mutex_lock_interruptible(dev);
1126 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1132 intel_mark_busy(dev, obj);
1134 if (read_domains & I915_GEM_DOMAIN_GTT) {
1135 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1137 /* Update the LRU on the fence for the CPU access that's
1140 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1141 struct drm_i915_fence_reg *reg =
1142 &dev_priv->fence_regs[obj->fence_reg];
1143 list_move_tail(®->lru_list,
1144 &dev_priv->mm.fence_list);
1147 /* Silently promote "you're not bound, there was nothing to do"
1148 * to success, since the client was just asking us to
1149 * make sure everything was done.
1154 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1157 /* Maintain LRU order of "inactive" objects */
1158 if (ret == 0 && i915_gem_object_is_inactive(obj))
1159 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1161 drm_gem_object_unreference(&obj->base);
1163 mutex_unlock(&dev->struct_mutex);
1168 * Called when user space has done writes to this buffer
1171 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1172 struct drm_file *file)
1174 struct drm_i915_gem_sw_finish *args = data;
1175 struct drm_i915_gem_object *obj;
1178 if (!(dev->driver->driver_features & DRIVER_GEM))
1181 ret = i915_mutex_lock_interruptible(dev);
1185 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1191 /* Pinned buffers may be scanout, so flush the cache */
1193 i915_gem_object_flush_cpu_write_domain(obj);
1195 drm_gem_object_unreference(&obj->base);
1197 mutex_unlock(&dev->struct_mutex);
1202 * Maps the contents of an object, returning the address it is mapped
1205 * While the mapping holds a reference on the contents of the object, it doesn't
1206 * imply a ref on the object itself.
1209 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1210 struct drm_file *file)
1212 struct drm_i915_private *dev_priv = dev->dev_private;
1213 struct drm_i915_gem_mmap *args = data;
1214 struct drm_gem_object *obj;
1218 if (!(dev->driver->driver_features & DRIVER_GEM))
1221 obj = drm_gem_object_lookup(dev, file, args->handle);
1225 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1226 drm_gem_object_unreference_unlocked(obj);
1230 offset = args->offset;
1232 down_write(¤t->mm->mmap_sem);
1233 addr = do_mmap(obj->filp, 0, args->size,
1234 PROT_READ | PROT_WRITE, MAP_SHARED,
1236 up_write(¤t->mm->mmap_sem);
1237 drm_gem_object_unreference_unlocked(obj);
1238 if (IS_ERR((void *)addr))
1241 args->addr_ptr = (uint64_t) addr;
1247 * i915_gem_fault - fault a page into the GTT
1248 * vma: VMA in question
1251 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1252 * from userspace. The fault handler takes care of binding the object to
1253 * the GTT (if needed), allocating and programming a fence register (again,
1254 * only if needed based on whether the old reg is still valid or the object
1255 * is tiled) and inserting a new PTE into the faulting process.
1257 * Note that the faulting process may involve evicting existing objects
1258 * from the GTT and/or fence registers to make room. So performance may
1259 * suffer if the GTT working set is large or there are few fence registers
1262 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1264 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1265 struct drm_device *dev = obj->base.dev;
1266 drm_i915_private_t *dev_priv = dev->dev_private;
1267 pgoff_t page_offset;
1270 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1272 /* We don't use vmf->pgoff since that has the fake offset */
1273 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1276 /* Now bind it into the GTT if needed */
1277 mutex_lock(&dev->struct_mutex);
1278 BUG_ON(obj->pin_count && !obj->pin_mappable);
1280 if (obj->gtt_space) {
1281 if (!obj->map_and_fenceable) {
1282 ret = i915_gem_object_unbind(obj);
1288 if (!obj->gtt_space) {
1289 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1294 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1298 if (!obj->fault_mappable) {
1299 obj->fault_mappable = true;
1300 i915_gem_info_update_mappable(dev_priv, obj, true);
1303 /* Need a new fence register? */
1304 if (obj->tiling_mode != I915_TILING_NONE) {
1305 ret = i915_gem_object_get_fence_reg(obj, true);
1310 if (i915_gem_object_is_inactive(obj))
1311 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1313 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1316 /* Finally, remap it using the new GTT offset */
1317 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1319 mutex_unlock(&dev->struct_mutex);
1326 return VM_FAULT_NOPAGE;
1328 return VM_FAULT_OOM;
1330 return VM_FAULT_SIGBUS;
1335 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1336 * @obj: obj in question
1338 * GEM memory mapping works by handing back to userspace a fake mmap offset
1339 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1340 * up the object based on the offset and sets up the various memory mapping
1343 * This routine allocates and attaches a fake offset for @obj.
1346 i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
1348 struct drm_device *dev = obj->base.dev;
1349 struct drm_gem_mm *mm = dev->mm_private;
1350 struct drm_map_list *list;
1351 struct drm_local_map *map;
1354 /* Set the object up for mmap'ing */
1355 list = &obj->base.map_list;
1356 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1361 map->type = _DRM_GEM;
1362 map->size = obj->base.size;
1365 /* Get a DRM GEM mmap offset allocated... */
1366 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1367 obj->base.size / PAGE_SIZE,
1369 if (!list->file_offset_node) {
1370 DRM_ERROR("failed to allocate offset for bo %d\n",
1376 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1377 obj->base.size / PAGE_SIZE,
1379 if (!list->file_offset_node) {
1384 list->hash.key = list->file_offset_node->start;
1385 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1387 DRM_ERROR("failed to add to map hash\n");
1394 drm_mm_put_block(list->file_offset_node);
1403 * i915_gem_release_mmap - remove physical page mappings
1404 * @obj: obj in question
1406 * Preserve the reservation of the mmapping with the DRM core code, but
1407 * relinquish ownership of the pages back to the system.
1409 * It is vital that we remove the page mapping if we have mapped a tiled
1410 * object through the GTT and then lose the fence register due to
1411 * resource pressure. Similarly if the object has been moved out of the
1412 * aperture, than pages mapped into userspace must be revoked. Removing the
1413 * mapping will then trigger a page fault on the next user access, allowing
1414 * fixup by i915_gem_fault().
1417 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1419 struct drm_device *dev = obj->base.dev;
1420 struct drm_i915_private *dev_priv = dev->dev_private;
1422 if (unlikely(obj->base.map_list.map && dev->dev_mapping))
1423 unmap_mapping_range(dev->dev_mapping,
1424 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1427 if (obj->fault_mappable) {
1428 obj->fault_mappable = false;
1429 i915_gem_info_update_mappable(dev_priv, obj, false);
1434 i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
1436 struct drm_device *dev = obj->base.dev;
1437 struct drm_gem_mm *mm = dev->mm_private;
1438 struct drm_map_list *list = &obj->base.map_list;
1440 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1441 drm_mm_put_block(list->file_offset_node);
1447 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1448 * @obj: object to check
1450 * Return the required GTT alignment for an object, taking into account
1451 * potential fence register mapping.
1454 i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
1456 struct drm_device *dev = obj->base.dev;
1459 * Minimum alignment is 4k (GTT page size), but might be greater
1460 * if a fence register is needed for the object.
1462 if (INTEL_INFO(dev)->gen >= 4 ||
1463 obj->tiling_mode == I915_TILING_NONE)
1467 * Previous chips need to be aligned to the size of the smallest
1468 * fence register that can contain the object.
1470 return i915_gem_get_gtt_size(obj);
1474 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1476 * @obj: object to check
1478 * Return the required GTT alignment for an object, only taking into account
1479 * unfenced tiled surface requirements.
1482 i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
1484 struct drm_device *dev = obj->base.dev;
1488 * Minimum alignment is 4k (GTT page size) for sane hw.
1490 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1491 obj->tiling_mode == I915_TILING_NONE)
1495 * Older chips need unfenced tiled buffers to be aligned to the left
1496 * edge of an even tile row (where tile rows are counted as if the bo is
1497 * placed in a fenced gtt region).
1500 (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev)))
1505 return tile_height * obj->stride * 2;
1509 i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1511 struct drm_device *dev = obj->base.dev;
1515 * Minimum alignment is 4k (GTT page size), but might be greater
1516 * if a fence register is needed for the object.
1518 if (INTEL_INFO(dev)->gen >= 4)
1519 return obj->base.size;
1522 * Previous chips need to be aligned to the size of the smallest
1523 * fence register that can contain the object.
1525 if (INTEL_INFO(dev)->gen == 3)
1530 while (size < obj->base.size)
1537 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1539 * @data: GTT mapping ioctl data
1540 * @file: GEM object info
1542 * Simply returns the fake offset to userspace so it can mmap it.
1543 * The mmap call will end up in drm_gem_mmap(), which will set things
1544 * up so we can get faults in the handler above.
1546 * The fault handler will take care of binding the object into the GTT
1547 * (since it may have been evicted to make room for something), allocating
1548 * a fence register, and mapping the appropriate aperture address into
1552 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1553 struct drm_file *file)
1555 struct drm_i915_private *dev_priv = dev->dev_private;
1556 struct drm_i915_gem_mmap_gtt *args = data;
1557 struct drm_i915_gem_object *obj;
1560 if (!(dev->driver->driver_features & DRIVER_GEM))
1563 ret = i915_mutex_lock_interruptible(dev);
1567 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1573 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1578 if (obj->madv != I915_MADV_WILLNEED) {
1579 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1584 if (!obj->base.map_list.map) {
1585 ret = i915_gem_create_mmap_offset(obj);
1590 args->offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1593 drm_gem_object_unreference(&obj->base);
1595 mutex_unlock(&dev->struct_mutex);
1600 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1604 struct address_space *mapping;
1605 struct inode *inode;
1608 /* Get the list of pages out of our struct file. They'll be pinned
1609 * at this point until we release them.
1611 page_count = obj->base.size / PAGE_SIZE;
1612 BUG_ON(obj->pages != NULL);
1613 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1614 if (obj->pages == NULL)
1617 inode = obj->base.filp->f_path.dentry->d_inode;
1618 mapping = inode->i_mapping;
1619 for (i = 0; i < page_count; i++) {
1620 page = read_cache_page_gfp(mapping, i,
1628 obj->pages[i] = page;
1631 if (obj->tiling_mode != I915_TILING_NONE)
1632 i915_gem_object_do_bit_17_swizzle(obj);
1638 page_cache_release(obj->pages[i]);
1640 drm_free_large(obj->pages);
1642 return PTR_ERR(page);
1646 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1648 int page_count = obj->base.size / PAGE_SIZE;
1651 BUG_ON(obj->madv == __I915_MADV_PURGED);
1653 if (obj->tiling_mode != I915_TILING_NONE)
1654 i915_gem_object_save_bit_17_swizzle(obj);
1656 if (obj->madv == I915_MADV_DONTNEED)
1659 for (i = 0; i < page_count; i++) {
1661 set_page_dirty(obj->pages[i]);
1663 if (obj->madv == I915_MADV_WILLNEED)
1664 mark_page_accessed(obj->pages[i]);
1666 page_cache_release(obj->pages[i]);
1670 drm_free_large(obj->pages);
1675 i915_gem_next_request_seqno(struct drm_device *dev,
1676 struct intel_ring_buffer *ring)
1678 drm_i915_private_t *dev_priv = dev->dev_private;
1679 return ring->outstanding_lazy_request = dev_priv->next_seqno;
1683 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1684 struct intel_ring_buffer *ring)
1686 struct drm_device *dev = obj->base.dev;
1687 struct drm_i915_private *dev_priv = dev->dev_private;
1688 uint32_t seqno = i915_gem_next_request_seqno(dev, ring);
1690 BUG_ON(ring == NULL);
1693 /* Add a reference if we're newly entering the active list. */
1695 drm_gem_object_reference(&obj->base);
1699 /* Move from whatever list we were on to the tail of execution. */
1700 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1701 list_move_tail(&obj->ring_list, &ring->active_list);
1702 obj->last_rendering_seqno = seqno;
1706 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1708 struct drm_device *dev = obj->base.dev;
1709 drm_i915_private_t *dev_priv = dev->dev_private;
1711 BUG_ON(!obj->active);
1712 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1713 list_del_init(&obj->ring_list);
1714 obj->last_rendering_seqno = 0;
1717 /* Immediately discard the backing storage */
1719 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1721 struct inode *inode;
1723 /* Our goal here is to return as much of the memory as
1724 * is possible back to the system as we are called from OOM.
1725 * To do this we must instruct the shmfs to drop all of its
1726 * backing pages, *now*. Here we mirror the actions taken
1727 * when by shmem_delete_inode() to release the backing store.
1729 inode = obj->base.filp->f_path.dentry->d_inode;
1730 truncate_inode_pages(inode->i_mapping, 0);
1731 if (inode->i_op->truncate_range)
1732 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
1734 obj->madv = __I915_MADV_PURGED;
1738 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1740 return obj->madv == I915_MADV_DONTNEED;
1744 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1746 struct drm_device *dev = obj->base.dev;
1747 drm_i915_private_t *dev_priv = dev->dev_private;
1749 if (obj->pin_count != 0)
1750 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1752 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1753 list_del_init(&obj->ring_list);
1755 BUG_ON(!list_empty(&obj->gpu_write_list));
1757 obj->last_rendering_seqno = 0;
1761 drm_gem_object_unreference(&obj->base);
1763 WARN_ON(i915_verify_lists(dev));
1767 i915_gem_process_flushing_list(struct drm_device *dev,
1768 uint32_t flush_domains,
1769 struct intel_ring_buffer *ring)
1771 drm_i915_private_t *dev_priv = dev->dev_private;
1772 struct drm_i915_gem_object *obj, *next;
1774 list_for_each_entry_safe(obj, next,
1775 &ring->gpu_write_list,
1777 if (obj->base.write_domain & flush_domains) {
1778 uint32_t old_write_domain = obj->base.write_domain;
1780 obj->base.write_domain = 0;
1781 list_del_init(&obj->gpu_write_list);
1782 i915_gem_object_move_to_active(obj, ring);
1784 /* update the fence lru list */
1785 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1786 struct drm_i915_fence_reg *reg =
1787 &dev_priv->fence_regs[obj->fence_reg];
1788 list_move_tail(®->lru_list,
1789 &dev_priv->mm.fence_list);
1792 trace_i915_gem_object_change_domain(obj,
1793 obj->base.read_domains,
1800 i915_add_request(struct drm_device *dev,
1801 struct drm_file *file,
1802 struct drm_i915_gem_request *request,
1803 struct intel_ring_buffer *ring)
1805 drm_i915_private_t *dev_priv = dev->dev_private;
1806 struct drm_i915_file_private *file_priv = NULL;
1811 BUG_ON(request == NULL);
1814 file_priv = file->driver_priv;
1816 ret = ring->add_request(ring, &seqno);
1820 ring->outstanding_lazy_request = false;
1822 request->seqno = seqno;
1823 request->ring = ring;
1824 request->emitted_jiffies = jiffies;
1825 was_empty = list_empty(&ring->request_list);
1826 list_add_tail(&request->list, &ring->request_list);
1829 spin_lock(&file_priv->mm.lock);
1830 request->file_priv = file_priv;
1831 list_add_tail(&request->client_list,
1832 &file_priv->mm.request_list);
1833 spin_unlock(&file_priv->mm.lock);
1836 if (!dev_priv->mm.suspended) {
1837 mod_timer(&dev_priv->hangcheck_timer,
1838 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1840 queue_delayed_work(dev_priv->wq,
1841 &dev_priv->mm.retire_work, HZ);
1847 * Command execution barrier
1849 * Ensures that all commands in the ring are finished
1850 * before signalling the CPU
1853 i915_retire_commands(struct drm_device *dev, struct intel_ring_buffer *ring)
1855 uint32_t flush_domains = 0;
1857 /* The sampler always gets flushed on i965 (sigh) */
1858 if (INTEL_INFO(dev)->gen >= 4)
1859 flush_domains |= I915_GEM_DOMAIN_SAMPLER;
1861 ring->flush(ring, I915_GEM_DOMAIN_COMMAND, flush_domains);
1865 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1867 struct drm_i915_file_private *file_priv = request->file_priv;
1872 spin_lock(&file_priv->mm.lock);
1873 list_del(&request->client_list);
1874 request->file_priv = NULL;
1875 spin_unlock(&file_priv->mm.lock);
1878 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1879 struct intel_ring_buffer *ring)
1881 while (!list_empty(&ring->request_list)) {
1882 struct drm_i915_gem_request *request;
1884 request = list_first_entry(&ring->request_list,
1885 struct drm_i915_gem_request,
1888 list_del(&request->list);
1889 i915_gem_request_remove_from_client(request);
1893 while (!list_empty(&ring->active_list)) {
1894 struct drm_i915_gem_object *obj;
1896 obj = list_first_entry(&ring->active_list,
1897 struct drm_i915_gem_object,
1900 obj->base.write_domain = 0;
1901 list_del_init(&obj->gpu_write_list);
1902 i915_gem_object_move_to_inactive(obj);
1906 void i915_gem_reset(struct drm_device *dev)
1908 struct drm_i915_private *dev_priv = dev->dev_private;
1909 struct drm_i915_gem_object *obj;
1912 i915_gem_reset_ring_lists(dev_priv, &dev_priv->render_ring);
1913 i915_gem_reset_ring_lists(dev_priv, &dev_priv->bsd_ring);
1914 i915_gem_reset_ring_lists(dev_priv, &dev_priv->blt_ring);
1916 /* Remove anything from the flushing lists. The GPU cache is likely
1917 * to be lost on reset along with the data, so simply move the
1918 * lost bo to the inactive list.
1920 while (!list_empty(&dev_priv->mm.flushing_list)) {
1921 obj= list_first_entry(&dev_priv->mm.flushing_list,
1922 struct drm_i915_gem_object,
1925 obj->base.write_domain = 0;
1926 list_del_init(&obj->gpu_write_list);
1927 i915_gem_object_move_to_inactive(obj);
1930 /* Move everything out of the GPU domains to ensure we do any
1931 * necessary invalidation upon reuse.
1933 list_for_each_entry(obj,
1934 &dev_priv->mm.inactive_list,
1937 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1940 /* The fence registers are invalidated so clear them out */
1941 for (i = 0; i < 16; i++) {
1942 struct drm_i915_fence_reg *reg;
1944 reg = &dev_priv->fence_regs[i];
1948 i915_gem_clear_fence_reg(reg->obj);
1953 * This function clears the request list as sequence numbers are passed.
1956 i915_gem_retire_requests_ring(struct drm_device *dev,
1957 struct intel_ring_buffer *ring)
1959 drm_i915_private_t *dev_priv = dev->dev_private;
1962 if (!ring->status_page.page_addr ||
1963 list_empty(&ring->request_list))
1966 WARN_ON(i915_verify_lists(dev));
1968 seqno = ring->get_seqno(ring);
1969 while (!list_empty(&ring->request_list)) {
1970 struct drm_i915_gem_request *request;
1972 request = list_first_entry(&ring->request_list,
1973 struct drm_i915_gem_request,
1976 if (!i915_seqno_passed(seqno, request->seqno))
1979 trace_i915_gem_request_retire(dev, request->seqno);
1981 list_del(&request->list);
1982 i915_gem_request_remove_from_client(request);
1986 /* Move any buffers on the active list that are no longer referenced
1987 * by the ringbuffer to the flushing/inactive lists as appropriate.
1989 while (!list_empty(&ring->active_list)) {
1990 struct drm_i915_gem_object *obj;
1992 obj= list_first_entry(&ring->active_list,
1993 struct drm_i915_gem_object,
1996 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1999 if (obj->base.write_domain != 0)
2000 i915_gem_object_move_to_flushing(obj);
2002 i915_gem_object_move_to_inactive(obj);
2005 if (unlikely (dev_priv->trace_irq_seqno &&
2006 i915_seqno_passed(dev_priv->trace_irq_seqno, seqno))) {
2007 ring->user_irq_put(ring);
2008 dev_priv->trace_irq_seqno = 0;
2011 WARN_ON(i915_verify_lists(dev));
2015 i915_gem_retire_requests(struct drm_device *dev)
2017 drm_i915_private_t *dev_priv = dev->dev_private;
2019 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
2020 struct drm_i915_gem_object *obj, *next;
2022 /* We must be careful that during unbind() we do not
2023 * accidentally infinitely recurse into retire requests.
2025 * retire -> free -> unbind -> wait -> retire_ring
2027 list_for_each_entry_safe(obj, next,
2028 &dev_priv->mm.deferred_free_list,
2030 i915_gem_free_object_tail(obj);
2033 i915_gem_retire_requests_ring(dev, &dev_priv->render_ring);
2034 i915_gem_retire_requests_ring(dev, &dev_priv->bsd_ring);
2035 i915_gem_retire_requests_ring(dev, &dev_priv->blt_ring);
2039 i915_gem_retire_work_handler(struct work_struct *work)
2041 drm_i915_private_t *dev_priv;
2042 struct drm_device *dev;
2044 dev_priv = container_of(work, drm_i915_private_t,
2045 mm.retire_work.work);
2046 dev = dev_priv->dev;
2048 /* Come back later if the device is busy... */
2049 if (!mutex_trylock(&dev->struct_mutex)) {
2050 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2054 i915_gem_retire_requests(dev);
2056 if (!dev_priv->mm.suspended &&
2057 (!list_empty(&dev_priv->render_ring.request_list) ||
2058 !list_empty(&dev_priv->bsd_ring.request_list) ||
2059 !list_empty(&dev_priv->blt_ring.request_list)))
2060 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2061 mutex_unlock(&dev->struct_mutex);
2065 i915_do_wait_request(struct drm_device *dev, uint32_t seqno,
2066 bool interruptible, struct intel_ring_buffer *ring)
2068 drm_i915_private_t *dev_priv = dev->dev_private;
2074 if (atomic_read(&dev_priv->mm.wedged))
2077 if (seqno == ring->outstanding_lazy_request) {
2078 struct drm_i915_gem_request *request;
2080 request = kzalloc(sizeof(*request), GFP_KERNEL);
2081 if (request == NULL)
2084 ret = i915_add_request(dev, NULL, request, ring);
2090 seqno = request->seqno;
2093 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
2094 if (HAS_PCH_SPLIT(dev))
2095 ier = I915_READ(DEIER) | I915_READ(GTIER);
2097 ier = I915_READ(IER);
2099 DRM_ERROR("something (likely vbetool) disabled "
2100 "interrupts, re-enabling\n");
2101 i915_driver_irq_preinstall(dev);
2102 i915_driver_irq_postinstall(dev);
2105 trace_i915_gem_request_wait_begin(dev, seqno);
2107 ring->waiting_seqno = seqno;
2108 ring->user_irq_get(ring);
2110 ret = wait_event_interruptible(ring->irq_queue,
2111 i915_seqno_passed(ring->get_seqno(ring), seqno)
2112 || atomic_read(&dev_priv->mm.wedged));
2114 wait_event(ring->irq_queue,
2115 i915_seqno_passed(ring->get_seqno(ring), seqno)
2116 || atomic_read(&dev_priv->mm.wedged));
2118 ring->user_irq_put(ring);
2119 ring->waiting_seqno = 0;
2121 trace_i915_gem_request_wait_end(dev, seqno);
2123 if (atomic_read(&dev_priv->mm.wedged))
2126 if (ret && ret != -ERESTARTSYS)
2127 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2128 __func__, ret, seqno, ring->get_seqno(ring),
2129 dev_priv->next_seqno);
2131 /* Directly dispatch request retiring. While we have the work queue
2132 * to handle this, the waiter on a request often wants an associated
2133 * buffer to have made it to the inactive list, and we would need
2134 * a separate wait queue to handle that.
2137 i915_gem_retire_requests_ring(dev, ring);
2143 * Waits for a sequence number to be signaled, and cleans up the
2144 * request and object lists appropriately for that event.
2147 i915_wait_request(struct drm_device *dev, uint32_t seqno,
2148 struct intel_ring_buffer *ring)
2150 return i915_do_wait_request(dev, seqno, 1, ring);
2154 i915_gem_flush_ring(struct drm_device *dev,
2155 struct intel_ring_buffer *ring,
2156 uint32_t invalidate_domains,
2157 uint32_t flush_domains)
2159 ring->flush(ring, invalidate_domains, flush_domains);
2160 i915_gem_process_flushing_list(dev, flush_domains, ring);
2164 i915_gem_flush(struct drm_device *dev,
2165 uint32_t invalidate_domains,
2166 uint32_t flush_domains,
2167 uint32_t flush_rings)
2169 drm_i915_private_t *dev_priv = dev->dev_private;
2171 if (flush_domains & I915_GEM_DOMAIN_CPU)
2172 intel_gtt_chipset_flush();
2174 if ((flush_domains | invalidate_domains) & I915_GEM_GPU_DOMAINS) {
2175 if (flush_rings & RING_RENDER)
2176 i915_gem_flush_ring(dev, &dev_priv->render_ring,
2177 invalidate_domains, flush_domains);
2178 if (flush_rings & RING_BSD)
2179 i915_gem_flush_ring(dev, &dev_priv->bsd_ring,
2180 invalidate_domains, flush_domains);
2181 if (flush_rings & RING_BLT)
2182 i915_gem_flush_ring(dev, &dev_priv->blt_ring,
2183 invalidate_domains, flush_domains);
2188 * Ensures that all rendering to the object has completed and the object is
2189 * safe to unbind from the GTT or access from the CPU.
2192 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
2195 struct drm_device *dev = obj->base.dev;
2198 /* This function only exists to support waiting for existing rendering,
2199 * not for emitting required flushes.
2201 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2203 /* If there is rendering queued on the buffer being evicted, wait for
2207 ret = i915_do_wait_request(dev,
2208 obj->last_rendering_seqno,
2219 * Unbinds an object from the GTT aperture.
2222 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2224 struct drm_device *dev = obj->base.dev;
2225 struct drm_i915_private *dev_priv = dev->dev_private;
2228 if (obj->gtt_space == NULL)
2231 if (obj->pin_count != 0) {
2232 DRM_ERROR("Attempting to unbind pinned buffer\n");
2236 /* blow away mappings if mapped through GTT */
2237 i915_gem_release_mmap(obj);
2239 /* Move the object to the CPU domain to ensure that
2240 * any possible CPU writes while it's not in the GTT
2241 * are flushed when we go to remap it. This will
2242 * also ensure that all pending GPU writes are finished
2245 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2246 if (ret == -ERESTARTSYS)
2248 /* Continue on if we fail due to EIO, the GPU is hung so we
2249 * should be safe and we need to cleanup or else we might
2250 * cause memory corruption through use-after-free.
2253 i915_gem_clflush_object(obj);
2254 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2257 /* release the fence reg _after_ flushing */
2258 if (obj->fence_reg != I915_FENCE_REG_NONE)
2259 i915_gem_clear_fence_reg(obj);
2261 i915_gem_gtt_unbind_object(obj);
2263 i915_gem_object_put_pages_gtt(obj);
2265 i915_gem_info_remove_gtt(dev_priv, obj);
2266 list_del_init(&obj->mm_list);
2267 /* Avoid an unnecessary call to unbind on rebind. */
2268 obj->map_and_fenceable = true;
2270 drm_mm_put_block(obj->gtt_space);
2271 obj->gtt_space = NULL;
2272 obj->gtt_offset = 0;
2274 if (i915_gem_object_is_purgeable(obj))
2275 i915_gem_object_truncate(obj);
2277 trace_i915_gem_object_unbind(obj);
2282 static int i915_ring_idle(struct drm_device *dev,
2283 struct intel_ring_buffer *ring)
2285 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2288 i915_gem_flush_ring(dev, ring,
2289 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2290 return i915_wait_request(dev,
2291 i915_gem_next_request_seqno(dev, ring),
2296 i915_gpu_idle(struct drm_device *dev)
2298 drm_i915_private_t *dev_priv = dev->dev_private;
2302 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2303 list_empty(&dev_priv->mm.active_list));
2307 /* Flush everything onto the inactive list. */
2308 ret = i915_ring_idle(dev, &dev_priv->render_ring);
2312 ret = i915_ring_idle(dev, &dev_priv->bsd_ring);
2316 ret = i915_ring_idle(dev, &dev_priv->blt_ring);
2323 static void sandybridge_write_fence_reg(struct drm_i915_gem_object *obj)
2325 struct drm_device *dev = obj->base.dev;
2326 drm_i915_private_t *dev_priv = dev->dev_private;
2327 u32 size = obj->gtt_space->size;
2328 int regnum = obj->fence_reg;
2331 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2333 val |= obj->gtt_offset & 0xfffff000;
2334 val |= (uint64_t)((obj->stride / 128) - 1) <<
2335 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2337 if (obj->tiling_mode == I915_TILING_Y)
2338 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2339 val |= I965_FENCE_REG_VALID;
2341 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (regnum * 8), val);
2344 static void i965_write_fence_reg(struct drm_i915_gem_object *obj)
2346 struct drm_device *dev = obj->base.dev;
2347 drm_i915_private_t *dev_priv = dev->dev_private;
2348 u32 size = obj->gtt_space->size;
2349 int regnum = obj->fence_reg;
2352 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2354 val |= obj->gtt_offset & 0xfffff000;
2355 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2356 if (obj->tiling_mode == I915_TILING_Y)
2357 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2358 val |= I965_FENCE_REG_VALID;
2360 I915_WRITE64(FENCE_REG_965_0 + (regnum * 8), val);
2363 static void i915_write_fence_reg(struct drm_i915_gem_object *obj)
2365 struct drm_device *dev = obj->base.dev;
2366 drm_i915_private_t *dev_priv = dev->dev_private;
2367 u32 size = obj->gtt_space->size;
2368 uint32_t fence_reg, val, pitch_val;
2371 if ((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2372 (obj->gtt_offset & (size - 1))) {
2373 WARN(1, "%s: object 0x%08x [fenceable? %d] not 1M or size (0x%08x) aligned [gtt_space offset=%lx, size=%lx]\n",
2374 __func__, obj->gtt_offset, obj->map_and_fenceable, size,
2375 obj->gtt_space->start, obj->gtt_space->size);
2379 if (obj->tiling_mode == I915_TILING_Y &&
2380 HAS_128_BYTE_Y_TILING(dev))
2385 /* Note: pitch better be a power of two tile widths */
2386 pitch_val = obj->stride / tile_width;
2387 pitch_val = ffs(pitch_val) - 1;
2389 if (obj->tiling_mode == I915_TILING_Y &&
2390 HAS_128_BYTE_Y_TILING(dev))
2391 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2393 WARN_ON(pitch_val > I915_FENCE_MAX_PITCH_VAL);
2395 val = obj->gtt_offset;
2396 if (obj->tiling_mode == I915_TILING_Y)
2397 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2398 val |= I915_FENCE_SIZE_BITS(size);
2399 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2400 val |= I830_FENCE_REG_VALID;
2402 fence_reg = obj->fence_reg;
2404 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2406 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2407 I915_WRITE(fence_reg, val);
2410 static void i830_write_fence_reg(struct drm_i915_gem_object *obj)
2412 struct drm_device *dev = obj->base.dev;
2413 drm_i915_private_t *dev_priv = dev->dev_private;
2414 u32 size = obj->gtt_space->size;
2415 int regnum = obj->fence_reg;
2418 uint32_t fence_size_bits;
2420 if ((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2421 (obj->gtt_offset & (obj->base.size - 1))) {
2422 WARN(1, "%s: object 0x%08x not 512K or size aligned\n",
2423 __func__, obj->gtt_offset);
2427 pitch_val = obj->stride / 128;
2428 pitch_val = ffs(pitch_val) - 1;
2429 WARN_ON(pitch_val > I830_FENCE_MAX_PITCH_VAL);
2431 val = obj->gtt_offset;
2432 if (obj->tiling_mode == I915_TILING_Y)
2433 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2434 fence_size_bits = I830_FENCE_SIZE_BITS(size);
2435 WARN_ON(fence_size_bits & ~0x00000f00);
2436 val |= fence_size_bits;
2437 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2438 val |= I830_FENCE_REG_VALID;
2440 I915_WRITE(FENCE_REG_830_0 + (regnum * 4), val);
2443 static int i915_find_fence_reg(struct drm_device *dev,
2446 struct drm_i915_private *dev_priv = dev->dev_private;
2447 struct drm_i915_fence_reg *reg;
2448 struct drm_i915_gem_object *obj = NULL;
2451 /* First try to find a free reg */
2453 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2454 reg = &dev_priv->fence_regs[i];
2458 if (!reg->obj->pin_count)
2465 /* None available, try to steal one or wait for a user to finish */
2466 avail = I915_FENCE_REG_NONE;
2467 list_for_each_entry(reg, &dev_priv->mm.fence_list,
2474 avail = obj->fence_reg;
2478 BUG_ON(avail == I915_FENCE_REG_NONE);
2480 /* We only have a reference on obj from the active list. put_fence_reg
2481 * might drop that one, causing a use-after-free in it. So hold a
2482 * private reference to obj like the other callers of put_fence_reg
2483 * (set_tiling ioctl) do. */
2484 drm_gem_object_reference(&obj->base);
2485 ret = i915_gem_object_put_fence_reg(obj, interruptible);
2486 drm_gem_object_unreference(&obj->base);
2494 * i915_gem_object_get_fence_reg - set up a fence reg for an object
2495 * @obj: object to map through a fence reg
2497 * When mapping objects through the GTT, userspace wants to be able to write
2498 * to them without having to worry about swizzling if the object is tiled.
2500 * This function walks the fence regs looking for a free one for @obj,
2501 * stealing one if it can't find any.
2503 * It then sets up the reg based on the object's properties: address, pitch
2504 * and tiling format.
2507 i915_gem_object_get_fence_reg(struct drm_i915_gem_object *obj,
2510 struct drm_device *dev = obj->base.dev;
2511 struct drm_i915_private *dev_priv = dev->dev_private;
2512 struct drm_i915_fence_reg *reg = NULL;
2515 /* Just update our place in the LRU if our fence is getting used. */
2516 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2517 reg = &dev_priv->fence_regs[obj->fence_reg];
2518 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2522 switch (obj->tiling_mode) {
2523 case I915_TILING_NONE:
2524 WARN(1, "allocating a fence for non-tiled object?\n");
2529 WARN((obj->stride & (512 - 1)),
2530 "object 0x%08x is X tiled but has non-512B pitch\n",
2536 WARN((obj->stride & (128 - 1)),
2537 "object 0x%08x is Y tiled but has non-128B pitch\n",
2542 ret = i915_find_fence_reg(dev, interruptible);
2546 obj->fence_reg = ret;
2547 reg = &dev_priv->fence_regs[obj->fence_reg];
2548 list_add_tail(®->lru_list, &dev_priv->mm.fence_list);
2552 switch (INTEL_INFO(dev)->gen) {
2554 sandybridge_write_fence_reg(obj);
2558 i965_write_fence_reg(obj);
2561 i915_write_fence_reg(obj);
2564 i830_write_fence_reg(obj);
2568 trace_i915_gem_object_get_fence(obj,
2576 * i915_gem_clear_fence_reg - clear out fence register info
2577 * @obj: object to clear
2579 * Zeroes out the fence register itself and clears out the associated
2580 * data structures in dev_priv and obj.
2583 i915_gem_clear_fence_reg(struct drm_i915_gem_object *obj)
2585 struct drm_device *dev = obj->base.dev;
2586 drm_i915_private_t *dev_priv = dev->dev_private;
2587 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[obj->fence_reg];
2590 switch (INTEL_INFO(dev)->gen) {
2592 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 +
2593 (obj->fence_reg * 8), 0);
2597 I915_WRITE64(FENCE_REG_965_0 + (obj->fence_reg * 8), 0);
2600 if (obj->fence_reg >= 8)
2601 fence_reg = FENCE_REG_945_8 + (obj->fence_reg - 8) * 4;
2604 fence_reg = FENCE_REG_830_0 + obj->fence_reg * 4;
2606 I915_WRITE(fence_reg, 0);
2611 obj->fence_reg = I915_FENCE_REG_NONE;
2612 list_del_init(®->lru_list);
2616 * i915_gem_object_put_fence_reg - waits on outstanding fenced access
2617 * to the buffer to finish, and then resets the fence register.
2618 * @obj: tiled object holding a fence register.
2619 * @bool: whether the wait upon the fence is interruptible
2621 * Zeroes out the fence register itself and clears out the associated
2622 * data structures in dev_priv and obj.
2625 i915_gem_object_put_fence_reg(struct drm_i915_gem_object *obj,
2628 struct drm_device *dev = obj->base.dev;
2629 struct drm_i915_private *dev_priv = dev->dev_private;
2630 struct drm_i915_fence_reg *reg;
2632 if (obj->fence_reg == I915_FENCE_REG_NONE)
2635 /* If we've changed tiling, GTT-mappings of the object
2636 * need to re-fault to ensure that the correct fence register
2637 * setup is in place.
2639 i915_gem_release_mmap(obj);
2641 /* On the i915, GPU access to tiled buffers is via a fence,
2642 * therefore we must wait for any outstanding access to complete
2643 * before clearing the fence.
2645 reg = &dev_priv->fence_regs[obj->fence_reg];
2649 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2653 ret = i915_gem_object_wait_rendering(obj, interruptible);
2660 i915_gem_object_flush_gtt_write_domain(obj);
2661 i915_gem_clear_fence_reg(obj);
2667 * Finds free space in the GTT aperture and binds the object there.
2670 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2672 bool map_and_fenceable)
2674 struct drm_device *dev = obj->base.dev;
2675 drm_i915_private_t *dev_priv = dev->dev_private;
2676 struct drm_mm_node *free_space;
2677 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2678 u32 size, fence_size, fence_alignment, unfenced_alignment;
2679 bool mappable, fenceable;
2682 if (obj->madv != I915_MADV_WILLNEED) {
2683 DRM_ERROR("Attempting to bind a purgeable object\n");
2687 fence_size = i915_gem_get_gtt_size(obj);
2688 fence_alignment = i915_gem_get_gtt_alignment(obj);
2689 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
2692 alignment = map_and_fenceable ? fence_alignment :
2694 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2695 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2699 size = map_and_fenceable ? fence_size : obj->base.size;
2701 /* If the object is bigger than the entire aperture, reject it early
2702 * before evicting everything in a vain attempt to find space.
2704 if (obj->base.size >
2705 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2706 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2711 if (map_and_fenceable)
2713 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2715 dev_priv->mm.gtt_mappable_end,
2718 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2719 size, alignment, 0);
2721 if (free_space != NULL) {
2722 if (map_and_fenceable)
2724 drm_mm_get_block_range_generic(free_space,
2726 dev_priv->mm.gtt_mappable_end,
2730 drm_mm_get_block(free_space, size, alignment);
2732 if (obj->gtt_space == NULL) {
2733 /* If the gtt is empty and we're still having trouble
2734 * fitting our object in, we're out of memory.
2736 ret = i915_gem_evict_something(dev, size, alignment,
2744 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2746 drm_mm_put_block(obj->gtt_space);
2747 obj->gtt_space = NULL;
2749 if (ret == -ENOMEM) {
2750 /* first try to clear up some space from the GTT */
2751 ret = i915_gem_evict_something(dev, size,
2755 /* now try to shrink everyone else */
2770 ret = i915_gem_gtt_bind_object(obj);
2772 i915_gem_object_put_pages_gtt(obj);
2773 drm_mm_put_block(obj->gtt_space);
2774 obj->gtt_space = NULL;
2776 ret = i915_gem_evict_something(dev, size,
2777 alignment, map_and_fenceable);
2784 obj->gtt_offset = obj->gtt_space->start;
2786 /* keep track of bounds object by adding it to the inactive list */
2787 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2788 i915_gem_info_add_gtt(dev_priv, obj);
2790 /* Assert that the object is not currently in any GPU domain. As it
2791 * wasn't in the GTT, there shouldn't be any way it could have been in
2794 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2795 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2797 trace_i915_gem_object_bind(obj, obj->gtt_offset, map_and_fenceable);
2800 obj->gtt_space->size == fence_size &&
2801 (obj->gtt_space->start & (fence_alignment -1)) == 0;
2804 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2806 obj->map_and_fenceable = mappable && fenceable;
2812 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2814 /* If we don't have a page list set up, then we're not pinned
2815 * to GPU, and we can ignore the cache flush because it'll happen
2816 * again at bind time.
2818 if (obj->pages == NULL)
2821 trace_i915_gem_object_clflush(obj);
2823 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2826 /** Flushes any GPU write domain for the object if it's dirty. */
2828 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj,
2831 struct drm_device *dev = obj->base.dev;
2833 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2836 /* Queue the GPU write cache flushing we need. */
2837 i915_gem_flush_ring(dev, obj->ring, 0, obj->base.write_domain);
2838 BUG_ON(obj->base.write_domain);
2843 return i915_gem_object_wait_rendering(obj, true);
2846 /** Flushes the GTT write domain for the object if it's dirty. */
2848 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2850 uint32_t old_write_domain;
2852 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2855 /* No actual flushing is required for the GTT write domain. Writes
2856 * to it immediately go to main memory as far as we know, so there's
2857 * no chipset flush. It also doesn't land in render cache.
2859 i915_gem_release_mmap(obj);
2861 old_write_domain = obj->base.write_domain;
2862 obj->base.write_domain = 0;
2864 trace_i915_gem_object_change_domain(obj,
2865 obj->base.read_domains,
2869 /** Flushes the CPU write domain for the object if it's dirty. */
2871 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2873 uint32_t old_write_domain;
2875 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2878 i915_gem_clflush_object(obj);
2879 intel_gtt_chipset_flush();
2880 old_write_domain = obj->base.write_domain;
2881 obj->base.write_domain = 0;
2883 trace_i915_gem_object_change_domain(obj,
2884 obj->base.read_domains,
2889 * Moves a single object to the GTT read, and possibly write domain.
2891 * This function returns when the move is complete, including waiting on
2895 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, int write)
2897 uint32_t old_write_domain, old_read_domains;
2900 /* Not valid to be called on unbound objects. */
2901 if (obj->gtt_space == NULL)
2904 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
2908 i915_gem_object_flush_cpu_write_domain(obj);
2911 ret = i915_gem_object_wait_rendering(obj, true);
2916 old_write_domain = obj->base.write_domain;
2917 old_read_domains = obj->base.read_domains;
2919 /* It should now be out of any other write domains, and we can update
2920 * the domain values for our changes.
2922 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2923 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2925 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2926 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2930 trace_i915_gem_object_change_domain(obj,
2938 * Prepare buffer for display plane. Use uninterruptible for possible flush
2939 * wait, as in modesetting process we're not supposed to be interrupted.
2942 i915_gem_object_set_to_display_plane(struct drm_i915_gem_object *obj,
2945 uint32_t old_read_domains;
2948 /* Not valid to be called on unbound objects. */
2949 if (obj->gtt_space == NULL)
2952 ret = i915_gem_object_flush_gpu_write_domain(obj, true);
2956 /* Currently, we are always called from an non-interruptible context. */
2958 ret = i915_gem_object_wait_rendering(obj, false);
2963 i915_gem_object_flush_cpu_write_domain(obj);
2965 old_read_domains = obj->base.read_domains;
2966 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2968 trace_i915_gem_object_change_domain(obj,
2970 obj->base.write_domain);
2976 i915_gem_object_flush_gpu(struct drm_i915_gem_object *obj,
2982 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
2983 i915_gem_flush_ring(obj->base.dev, obj->ring,
2984 0, obj->base.write_domain);
2986 return i915_gem_object_wait_rendering(obj, interruptible);
2990 * Moves a single object to the CPU read, and possibly write domain.
2992 * This function returns when the move is complete, including waiting on
2996 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, int write)
2998 uint32_t old_write_domain, old_read_domains;
3001 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3005 i915_gem_object_flush_gtt_write_domain(obj);
3007 /* If we have a partially-valid cache of the object in the CPU,
3008 * finish invalidating it and free the per-page flags.
3010 i915_gem_object_set_to_full_cpu_read_domain(obj);
3013 ret = i915_gem_object_wait_rendering(obj, true);
3018 old_write_domain = obj->base.write_domain;
3019 old_read_domains = obj->base.read_domains;
3021 /* Flush the CPU cache if it's still invalid. */
3022 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3023 i915_gem_clflush_object(obj);
3025 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3028 /* It should now be out of any other write domains, and we can update
3029 * the domain values for our changes.
3031 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3033 /* If we're writing through the CPU, then the GPU read domains will
3034 * need to be invalidated at next use.
3037 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3038 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3041 trace_i915_gem_object_change_domain(obj,
3049 * Set the next domain for the specified object. This
3050 * may not actually perform the necessary flushing/invaliding though,
3051 * as that may want to be batched with other set_domain operations
3053 * This is (we hope) the only really tricky part of gem. The goal
3054 * is fairly simple -- track which caches hold bits of the object
3055 * and make sure they remain coherent. A few concrete examples may
3056 * help to explain how it works. For shorthand, we use the notation
3057 * (read_domains, write_domain), e.g. (CPU, CPU) to indicate the
3058 * a pair of read and write domain masks.
3060 * Case 1: the batch buffer
3066 * 5. Unmapped from GTT
3069 * Let's take these a step at a time
3072 * Pages allocated from the kernel may still have
3073 * cache contents, so we set them to (CPU, CPU) always.
3074 * 2. Written by CPU (using pwrite)
3075 * The pwrite function calls set_domain (CPU, CPU) and
3076 * this function does nothing (as nothing changes)
3078 * This function asserts that the object is not
3079 * currently in any GPU-based read or write domains
3081 * i915_gem_execbuffer calls set_domain (COMMAND, 0).
3082 * As write_domain is zero, this function adds in the
3083 * current read domains (CPU+COMMAND, 0).
3084 * flush_domains is set to CPU.
3085 * invalidate_domains is set to COMMAND
3086 * clflush is run to get data out of the CPU caches
3087 * then i915_dev_set_domain calls i915_gem_flush to
3088 * emit an MI_FLUSH and drm_agp_chipset_flush
3089 * 5. Unmapped from GTT
3090 * i915_gem_object_unbind calls set_domain (CPU, CPU)
3091 * flush_domains and invalidate_domains end up both zero
3092 * so no flushing/invalidating happens
3096 * Case 2: The shared render buffer
3100 * 3. Read/written by GPU
3101 * 4. set_domain to (CPU,CPU)
3102 * 5. Read/written by CPU
3103 * 6. Read/written by GPU
3106 * Same as last example, (CPU, CPU)
3108 * Nothing changes (assertions find that it is not in the GPU)
3109 * 3. Read/written by GPU
3110 * execbuffer calls set_domain (RENDER, RENDER)
3111 * flush_domains gets CPU
3112 * invalidate_domains gets GPU
3114 * MI_FLUSH and drm_agp_chipset_flush
3115 * 4. set_domain (CPU, CPU)
3116 * flush_domains gets GPU
3117 * invalidate_domains gets CPU
3118 * wait_rendering (obj) to make sure all drawing is complete.
3119 * This will include an MI_FLUSH to get the data from GPU
3121 * clflush (obj) to invalidate the CPU cache
3122 * Another MI_FLUSH in i915_gem_flush (eliminate this somehow?)
3123 * 5. Read/written by CPU
3124 * cache lines are loaded and dirtied
3125 * 6. Read written by GPU
3126 * Same as last GPU access
3128 * Case 3: The constant buffer
3133 * 4. Updated (written) by CPU again
3142 * flush_domains = CPU
3143 * invalidate_domains = RENDER
3146 * drm_agp_chipset_flush
3147 * 4. Updated (written) by CPU again
3149 * flush_domains = 0 (no previous write domain)
3150 * invalidate_domains = 0 (no new read domains)
3153 * flush_domains = CPU
3154 * invalidate_domains = RENDER
3157 * drm_agp_chipset_flush
3160 i915_gem_object_set_to_gpu_domain(struct drm_i915_gem_object *obj,
3161 struct intel_ring_buffer *ring,
3162 struct change_domains *cd)
3164 uint32_t invalidate_domains = 0, flush_domains = 0;
3167 * If the object isn't moving to a new write domain,
3168 * let the object stay in multiple read domains
3170 if (obj->base.pending_write_domain == 0)
3171 obj->base.pending_read_domains |= obj->base.read_domains;
3174 * Flush the current write domain if
3175 * the new read domains don't match. Invalidate
3176 * any read domains which differ from the old
3179 if (obj->base.write_domain &&
3180 (obj->base.write_domain != obj->base.pending_read_domains ||
3181 obj->ring != ring)) {
3182 flush_domains |= obj->base.write_domain;
3183 invalidate_domains |=
3184 obj->base.pending_read_domains & ~obj->base.write_domain;
3187 * Invalidate any read caches which may have
3188 * stale data. That is, any new read domains.
3190 invalidate_domains |= obj->base.pending_read_domains & ~obj->base.read_domains;
3191 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_CPU)
3192 i915_gem_clflush_object(obj);
3194 /* blow away mappings if mapped through GTT */
3195 if ((flush_domains | invalidate_domains) & I915_GEM_DOMAIN_GTT)
3196 i915_gem_release_mmap(obj);
3198 /* The actual obj->write_domain will be updated with
3199 * pending_write_domain after we emit the accumulated flush for all
3200 * of our domain changes in execbuffers (which clears objects'
3201 * write_domains). So if we have a current write domain that we
3202 * aren't changing, set pending_write_domain to that.
3204 if (flush_domains == 0 && obj->base.pending_write_domain == 0)
3205 obj->base.pending_write_domain = obj->base.write_domain;
3207 cd->invalidate_domains |= invalidate_domains;
3208 cd->flush_domains |= flush_domains;
3209 if (flush_domains & I915_GEM_GPU_DOMAINS)
3210 cd->flush_rings |= obj->ring->id;
3211 if (invalidate_domains & I915_GEM_GPU_DOMAINS)
3212 cd->flush_rings |= ring->id;
3216 * Moves the object from a partially CPU read to a full one.
3218 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3219 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3222 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3224 if (!obj->page_cpu_valid)
3227 /* If we're partially in the CPU read domain, finish moving it in.
3229 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3232 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3233 if (obj->page_cpu_valid[i])
3235 drm_clflush_pages(obj->pages + i, 1);
3239 /* Free the page_cpu_valid mappings which are now stale, whether
3240 * or not we've got I915_GEM_DOMAIN_CPU.
3242 kfree(obj->page_cpu_valid);
3243 obj->page_cpu_valid = NULL;
3247 * Set the CPU read domain on a range of the object.
3249 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3250 * not entirely valid. The page_cpu_valid member of the object flags which
3251 * pages have been flushed, and will be respected by
3252 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3253 * of the whole object.
3255 * This function returns when the move is complete, including waiting on
3259 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3260 uint64_t offset, uint64_t size)
3262 uint32_t old_read_domains;
3265 if (offset == 0 && size == obj->base.size)
3266 return i915_gem_object_set_to_cpu_domain(obj, 0);
3268 ret = i915_gem_object_flush_gpu_write_domain(obj, false);
3271 i915_gem_object_flush_gtt_write_domain(obj);
3273 /* If we're already fully in the CPU read domain, we're done. */
3274 if (obj->page_cpu_valid == NULL &&
3275 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3278 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3279 * newly adding I915_GEM_DOMAIN_CPU
3281 if (obj->page_cpu_valid == NULL) {
3282 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3284 if (obj->page_cpu_valid == NULL)
3286 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3287 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3289 /* Flush the cache on any pages that are still invalid from the CPU's
3292 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3294 if (obj->page_cpu_valid[i])
3297 drm_clflush_pages(obj->pages + i, 1);
3299 obj->page_cpu_valid[i] = 1;
3302 /* It should now be out of any other write domains, and we can update
3303 * the domain values for our changes.
3305 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3307 old_read_domains = obj->base.read_domains;
3308 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3310 trace_i915_gem_object_change_domain(obj,
3312 obj->base.write_domain);
3318 i915_gem_execbuffer_relocate_entry(struct drm_i915_gem_object *obj,
3319 struct drm_file *file_priv,
3320 struct drm_i915_gem_exec_object2 *entry,
3321 struct drm_i915_gem_relocation_entry *reloc)
3323 struct drm_device *dev = obj->base.dev;
3324 struct drm_gem_object *target_obj;
3325 uint32_t target_offset;
3328 target_obj = drm_gem_object_lookup(dev, file_priv,
3329 reloc->target_handle);
3330 if (target_obj == NULL)
3333 target_offset = to_intel_bo(target_obj)->gtt_offset;
3336 DRM_INFO("%s: obj %p offset %08x target %d "
3337 "read %08x write %08x gtt %08x "
3338 "presumed %08x delta %08x\n",
3341 (int) reloc->offset,
3342 (int) reloc->target_handle,
3343 (int) reloc->read_domains,
3344 (int) reloc->write_domain,
3345 (int) target_offset,
3346 (int) reloc->presumed_offset,
3350 /* The target buffer should have appeared before us in the
3351 * exec_object list, so it should have a GTT space bound by now.
3353 if (target_offset == 0) {
3354 DRM_ERROR("No GTT space found for object %d\n",
3355 reloc->target_handle);
3359 /* Validate that the target is in a valid r/w GPU domain */
3360 if (reloc->write_domain & (reloc->write_domain - 1)) {
3361 DRM_ERROR("reloc with multiple write domains: "
3362 "obj %p target %d offset %d "
3363 "read %08x write %08x",
3364 obj, reloc->target_handle,
3365 (int) reloc->offset,
3366 reloc->read_domains,
3367 reloc->write_domain);
3370 if (reloc->write_domain & I915_GEM_DOMAIN_CPU ||
3371 reloc->read_domains & I915_GEM_DOMAIN_CPU) {
3372 DRM_ERROR("reloc with read/write CPU domains: "
3373 "obj %p target %d offset %d "
3374 "read %08x write %08x",
3375 obj, reloc->target_handle,
3376 (int) reloc->offset,
3377 reloc->read_domains,
3378 reloc->write_domain);
3381 if (reloc->write_domain && target_obj->pending_write_domain &&
3382 reloc->write_domain != target_obj->pending_write_domain) {
3383 DRM_ERROR("Write domain conflict: "
3384 "obj %p target %d offset %d "
3385 "new %08x old %08x\n",
3386 obj, reloc->target_handle,
3387 (int) reloc->offset,
3388 reloc->write_domain,
3389 target_obj->pending_write_domain);
3393 target_obj->pending_read_domains |= reloc->read_domains;
3394 target_obj->pending_write_domain |= reloc->write_domain;
3396 /* If the relocation already has the right value in it, no
3397 * more work needs to be done.
3399 if (target_offset == reloc->presumed_offset)
3402 /* Check that the relocation address is valid... */
3403 if (reloc->offset > obj->base.size - 4) {
3404 DRM_ERROR("Relocation beyond object bounds: "
3405 "obj %p target %d offset %d size %d.\n",
3406 obj, reloc->target_handle,
3407 (int) reloc->offset,
3408 (int) obj->base.size);
3411 if (reloc->offset & 3) {
3412 DRM_ERROR("Relocation not 4-byte aligned: "
3413 "obj %p target %d offset %d.\n",
3414 obj, reloc->target_handle,
3415 (int) reloc->offset);
3419 /* and points to somewhere within the target object. */
3420 if (reloc->delta >= target_obj->size) {
3421 DRM_ERROR("Relocation beyond target object bounds: "
3422 "obj %p target %d delta %d size %d.\n",
3423 obj, reloc->target_handle,
3425 (int) target_obj->size);
3429 reloc->delta += target_offset;
3430 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU) {
3431 uint32_t page_offset = reloc->offset & ~PAGE_MASK;
3434 vaddr = kmap_atomic(obj->pages[reloc->offset >> PAGE_SHIFT]);
3435 *(uint32_t *)(vaddr + page_offset) = reloc->delta;
3436 kunmap_atomic(vaddr);
3438 struct drm_i915_private *dev_priv = dev->dev_private;
3439 uint32_t __iomem *reloc_entry;
3440 void __iomem *reloc_page;
3442 ret = i915_gem_object_set_to_gtt_domain(obj, 1);
3446 /* Map the page containing the relocation we're going to perform. */
3447 reloc->offset += obj->gtt_offset;
3448 reloc_page = io_mapping_map_atomic_wc(dev_priv->mm.gtt_mapping,
3449 reloc->offset & PAGE_MASK);
3450 reloc_entry = (uint32_t __iomem *)
3451 (reloc_page + (reloc->offset & ~PAGE_MASK));
3452 iowrite32(reloc->delta, reloc_entry);
3453 io_mapping_unmap_atomic(reloc_page);
3456 /* and update the user's relocation entry */
3457 reloc->presumed_offset = target_offset;
3462 drm_gem_object_unreference(target_obj);
3467 i915_gem_execbuffer_relocate_object(struct drm_i915_gem_object *obj,
3468 struct drm_file *file_priv,
3469 struct drm_i915_gem_exec_object2 *entry)
3471 struct drm_i915_gem_relocation_entry __user *user_relocs;
3474 user_relocs = (void __user *)(uintptr_t)entry->relocs_ptr;
3475 for (i = 0; i < entry->relocation_count; i++) {
3476 struct drm_i915_gem_relocation_entry reloc;
3478 if (__copy_from_user_inatomic(&reloc,
3483 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &reloc);
3487 if (__copy_to_user_inatomic(&user_relocs[i].presumed_offset,
3488 &reloc.presumed_offset,
3489 sizeof(reloc.presumed_offset)))
3497 i915_gem_execbuffer_relocate_object_slow(struct drm_i915_gem_object *obj,
3498 struct drm_file *file_priv,
3499 struct drm_i915_gem_exec_object2 *entry,
3500 struct drm_i915_gem_relocation_entry *relocs)
3504 for (i = 0; i < entry->relocation_count; i++) {
3505 ret = i915_gem_execbuffer_relocate_entry(obj, file_priv, entry, &relocs[i]);
3514 i915_gem_execbuffer_relocate(struct drm_device *dev,
3515 struct drm_file *file,
3516 struct drm_i915_gem_object **object_list,
3517 struct drm_i915_gem_exec_object2 *exec_list,
3522 for (i = 0; i < count; i++) {
3523 struct drm_i915_gem_object *obj = object_list[i];
3524 obj->base.pending_read_domains = 0;
3525 obj->base.pending_write_domain = 0;
3526 ret = i915_gem_execbuffer_relocate_object(obj, file,
3536 i915_gem_execbuffer_reserve(struct drm_device *dev,
3537 struct drm_file *file,
3538 struct drm_i915_gem_object **object_list,
3539 struct drm_i915_gem_exec_object2 *exec_list,
3542 struct drm_i915_private *dev_priv = dev->dev_private;
3545 /* attempt to pin all of the buffers into the GTT */
3549 for (i = 0; i < count; i++) {
3550 struct drm_i915_gem_exec_object2 *entry = &exec_list[i];
3551 struct drm_i915_gem_object *obj = object_list[i];
3553 entry->flags & EXEC_OBJECT_NEEDS_FENCE &&
3554 obj->tiling_mode != I915_TILING_NONE;
3556 /* g33/pnv can't fence buffers in the unmappable part */
3557 bool need_mappable =
3558 entry->relocation_count ? true : need_fence;
3560 /* Check fence reg constraints and rebind if necessary */
3561 if (need_mappable && !obj->map_and_fenceable) {
3562 ret = i915_gem_object_unbind(obj);
3567 ret = i915_gem_object_pin(obj,
3574 * Pre-965 chips need a fence register set up in order
3575 * to properly handle blits to/from tiled surfaces.
3578 ret = i915_gem_object_get_fence_reg(obj, true);
3580 i915_gem_object_unpin(obj);
3584 dev_priv->fence_regs[obj->fence_reg].gpu = true;
3587 entry->offset = obj->gtt_offset;
3591 i915_gem_object_unpin(object_list[i]);
3593 if (ret != -ENOSPC || retry > 1)
3596 /* First attempt, just clear anything that is purgeable.
3597 * Second attempt, clear the entire GTT.
3599 ret = i915_gem_evict_everything(dev, retry == 0);
3608 i915_gem_execbuffer_relocate_slow(struct drm_device *dev,
3609 struct drm_file *file,
3610 struct drm_i915_gem_object **object_list,
3611 struct drm_i915_gem_exec_object2 *exec_list,
3614 struct drm_i915_gem_relocation_entry *reloc;
3617 for (i = 0; i < count; i++)
3618 object_list[i]->in_execbuffer = false;
3620 mutex_unlock(&dev->struct_mutex);
3623 for (i = 0; i < count; i++)
3624 total += exec_list[i].relocation_count;
3626 reloc = drm_malloc_ab(total, sizeof(*reloc));
3627 if (reloc == NULL) {
3628 mutex_lock(&dev->struct_mutex);
3633 for (i = 0; i < count; i++) {
3634 struct drm_i915_gem_relocation_entry __user *user_relocs;
3636 user_relocs = (void __user *)(uintptr_t)exec_list[i].relocs_ptr;
3638 if (copy_from_user(reloc+total, user_relocs,
3639 exec_list[i].relocation_count *
3642 mutex_lock(&dev->struct_mutex);
3646 total += exec_list[i].relocation_count;
3649 ret = i915_mutex_lock_interruptible(dev);
3651 mutex_lock(&dev->struct_mutex);
3655 ret = i915_gem_execbuffer_reserve(dev, file,
3656 object_list, exec_list,
3662 for (i = 0; i < count; i++) {
3663 struct drm_i915_gem_object *obj = object_list[i];
3664 obj->base.pending_read_domains = 0;
3665 obj->base.pending_write_domain = 0;
3666 ret = i915_gem_execbuffer_relocate_object_slow(obj, file,
3672 total += exec_list[i].relocation_count;
3675 /* Leave the user relocations as are, this is the painfully slow path,
3676 * and we want to avoid the complication of dropping the lock whilst
3677 * having buffers reserved in the aperture and so causing spurious
3678 * ENOSPC for random operations.
3682 drm_free_large(reloc);
3687 i915_gem_execbuffer_move_to_gpu(struct drm_device *dev,
3688 struct drm_file *file,
3689 struct intel_ring_buffer *ring,
3690 struct drm_i915_gem_object **objects,
3693 struct change_domains cd;
3696 cd.invalidate_domains = 0;
3697 cd.flush_domains = 0;
3699 for (i = 0; i < count; i++)
3700 i915_gem_object_set_to_gpu_domain(objects[i], ring, &cd);
3702 if (cd.invalidate_domains | cd.flush_domains) {
3704 DRM_INFO("%s: invalidate_domains %08x flush_domains %08x\n",
3706 cd.invalidate_domains,
3710 cd.invalidate_domains,
3715 for (i = 0; i < count; i++) {
3716 struct drm_i915_gem_object *obj = objects[i];
3717 /* XXX replace with semaphores */
3718 if (obj->ring && ring != obj->ring) {
3719 ret = i915_gem_object_wait_rendering(obj, true);
3728 /* Throttle our rendering by waiting until the ring has completed our requests
3729 * emitted over 20 msec ago.
3731 * Note that if we were to use the current jiffies each time around the loop,
3732 * we wouldn't escape the function with any frames outstanding if the time to
3733 * render a frame was over 20ms.
3735 * This should get us reasonable parallelism between CPU and GPU but also
3736 * relatively low latency when blocking on a particular request to finish.
3739 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3741 struct drm_i915_private *dev_priv = dev->dev_private;
3742 struct drm_i915_file_private *file_priv = file->driver_priv;
3743 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3744 struct drm_i915_gem_request *request;
3745 struct intel_ring_buffer *ring = NULL;
3749 spin_lock(&file_priv->mm.lock);
3750 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3751 if (time_after_eq(request->emitted_jiffies, recent_enough))
3754 ring = request->ring;
3755 seqno = request->seqno;
3757 spin_unlock(&file_priv->mm.lock);
3763 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3764 /* And wait for the seqno passing without holding any locks and
3765 * causing extra latency for others. This is safe as the irq
3766 * generation is designed to be run atomically and so is
3769 ring->user_irq_get(ring);
3770 ret = wait_event_interruptible(ring->irq_queue,
3771 i915_seqno_passed(ring->get_seqno(ring), seqno)
3772 || atomic_read(&dev_priv->mm.wedged));
3773 ring->user_irq_put(ring);
3775 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3780 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3786 i915_gem_check_execbuffer(struct drm_i915_gem_execbuffer2 *exec,
3787 uint64_t exec_offset)
3789 uint32_t exec_start, exec_len;
3791 exec_start = (uint32_t) exec_offset + exec->batch_start_offset;
3792 exec_len = (uint32_t) exec->batch_len;
3794 if ((exec_start | exec_len) & 0x7)
3804 validate_exec_list(struct drm_i915_gem_exec_object2 *exec,
3809 for (i = 0; i < count; i++) {
3810 char __user *ptr = (char __user *)(uintptr_t)exec[i].relocs_ptr;
3811 int length; /* limited by fault_in_pages_readable() */
3813 /* First check for malicious input causing overflow */
3814 if (exec[i].relocation_count >
3815 INT_MAX / sizeof(struct drm_i915_gem_relocation_entry))
3818 length = exec[i].relocation_count *
3819 sizeof(struct drm_i915_gem_relocation_entry);
3820 if (!access_ok(VERIFY_READ, ptr, length))
3823 /* we may also need to update the presumed offsets */
3824 if (!access_ok(VERIFY_WRITE, ptr, length))
3827 if (fault_in_pages_readable(ptr, length))
3835 i915_gem_do_execbuffer(struct drm_device *dev, void *data,
3836 struct drm_file *file,
3837 struct drm_i915_gem_execbuffer2 *args,
3838 struct drm_i915_gem_exec_object2 *exec_list)
3840 drm_i915_private_t *dev_priv = dev->dev_private;
3841 struct drm_i915_gem_object **object_list = NULL;
3842 struct drm_i915_gem_object *batch_obj;
3843 struct drm_clip_rect *cliprects = NULL;
3844 struct drm_i915_gem_request *request = NULL;
3846 uint64_t exec_offset;
3848 struct intel_ring_buffer *ring = NULL;
3850 ret = i915_gem_check_is_wedged(dev);
3854 ret = validate_exec_list(exec_list, args->buffer_count);
3859 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
3860 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
3862 switch (args->flags & I915_EXEC_RING_MASK) {
3863 case I915_EXEC_DEFAULT:
3864 case I915_EXEC_RENDER:
3865 ring = &dev_priv->render_ring;
3868 if (!HAS_BSD(dev)) {
3869 DRM_ERROR("execbuf with invalid ring (BSD)\n");
3872 ring = &dev_priv->bsd_ring;
3875 if (!HAS_BLT(dev)) {
3876 DRM_ERROR("execbuf with invalid ring (BLT)\n");
3879 ring = &dev_priv->blt_ring;
3882 DRM_ERROR("execbuf with unknown ring: %d\n",
3883 (int)(args->flags & I915_EXEC_RING_MASK));
3887 if (args->buffer_count < 1) {
3888 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
3891 object_list = drm_malloc_ab(sizeof(*object_list), args->buffer_count);
3892 if (object_list == NULL) {
3893 DRM_ERROR("Failed to allocate object list for %d buffers\n",
3894 args->buffer_count);
3899 if (args->num_cliprects != 0) {
3900 cliprects = kcalloc(args->num_cliprects, sizeof(*cliprects),
3902 if (cliprects == NULL) {
3907 ret = copy_from_user(cliprects,
3908 (struct drm_clip_rect __user *)
3909 (uintptr_t) args->cliprects_ptr,
3910 sizeof(*cliprects) * args->num_cliprects);
3912 DRM_ERROR("copy %d cliprects failed: %d\n",
3913 args->num_cliprects, ret);
3919 request = kzalloc(sizeof(*request), GFP_KERNEL);
3920 if (request == NULL) {
3925 ret = i915_mutex_lock_interruptible(dev);
3929 if (dev_priv->mm.suspended) {
3930 mutex_unlock(&dev->struct_mutex);
3935 /* Look up object handles */
3936 for (i = 0; i < args->buffer_count; i++) {
3937 struct drm_i915_gem_object *obj;
3939 obj = to_intel_bo (drm_gem_object_lookup(dev, file,
3940 exec_list[i].handle));
3942 DRM_ERROR("Invalid object handle %d at index %d\n",
3943 exec_list[i].handle, i);
3944 /* prevent error path from reading uninitialized data */
3945 args->buffer_count = i;
3949 object_list[i] = obj;
3951 if (obj->in_execbuffer) {
3952 DRM_ERROR("Object %p appears more than once in object list\n",
3954 /* prevent error path from reading uninitialized data */
3955 args->buffer_count = i + 1;
3959 obj->in_execbuffer = true;
3962 /* Move the objects en-masse into the GTT, evicting if necessary. */
3963 ret = i915_gem_execbuffer_reserve(dev, file,
3964 object_list, exec_list,
3965 args->buffer_count);
3969 /* The objects are in their final locations, apply the relocations. */
3970 ret = i915_gem_execbuffer_relocate(dev, file,
3971 object_list, exec_list,
3972 args->buffer_count);
3974 if (ret == -EFAULT) {
3975 ret = i915_gem_execbuffer_relocate_slow(dev, file,
3978 args->buffer_count);
3979 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
3985 /* Set the pending read domains for the batch buffer to COMMAND */
3986 batch_obj = object_list[args->buffer_count-1];
3987 if (batch_obj->base.pending_write_domain) {
3988 DRM_ERROR("Attempting to use self-modifying batch buffer\n");
3992 batch_obj->base.pending_read_domains |= I915_GEM_DOMAIN_COMMAND;
3994 /* Sanity check the batch buffer */
3995 exec_offset = batch_obj->gtt_offset;
3996 ret = i915_gem_check_execbuffer(args, exec_offset);
3998 DRM_ERROR("execbuf with invalid offset/length\n");
4002 ret = i915_gem_execbuffer_move_to_gpu(dev, file, ring,
4003 object_list, args->buffer_count);
4008 for (i = 0; i < args->buffer_count; i++) {
4009 i915_gem_object_check_coherency(object_list[i],
4010 exec_list[i].handle);
4015 i915_gem_dump_object(batch_obj,
4021 /* Check for any pending flips. As we only maintain a flip queue depth
4022 * of 1, we can simply insert a WAIT for the next display flip prior
4023 * to executing the batch and avoid stalling the CPU.
4026 for (i = 0; i < args->buffer_count; i++) {
4027 if (object_list[i]->base.write_domain)
4028 flips |= atomic_read(&object_list[i]->pending_flip);
4031 int plane, flip_mask;
4033 for (plane = 0; flips >> plane; plane++) {
4034 if (((flips >> plane) & 1) == 0)
4038 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
4040 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
4042 ret = intel_ring_begin(ring, 2);
4046 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
4047 intel_ring_emit(ring, MI_NOOP);
4048 intel_ring_advance(ring);
4052 /* Exec the batchbuffer */
4053 ret = ring->dispatch_execbuffer(ring, args, cliprects, exec_offset);
4055 DRM_ERROR("dispatch failed %d\n", ret);
4059 for (i = 0; i < args->buffer_count; i++) {
4060 struct drm_i915_gem_object *obj = object_list[i];
4062 obj->base.read_domains = obj->base.pending_read_domains;
4063 obj->base.write_domain = obj->base.pending_write_domain;
4065 i915_gem_object_move_to_active(obj, ring);
4066 if (obj->base.write_domain) {
4068 list_move_tail(&obj->gpu_write_list,
4069 &ring->gpu_write_list);
4070 intel_mark_busy(dev, obj);
4073 trace_i915_gem_object_change_domain(obj,
4074 obj->base.read_domains,
4075 obj->base.write_domain);
4079 * Ensure that the commands in the batch buffer are
4080 * finished before the interrupt fires
4082 i915_retire_commands(dev, ring);
4084 if (i915_add_request(dev, file, request, ring))
4085 i915_gem_next_request_seqno(dev, ring);
4090 for (i = 0; i < args->buffer_count; i++) {
4091 object_list[i]->in_execbuffer = false;
4092 drm_gem_object_unreference(&object_list[i]->base);
4095 mutex_unlock(&dev->struct_mutex);
4098 drm_free_large(object_list);
4106 * Legacy execbuffer just creates an exec2 list from the original exec object
4107 * list array and passes it to the real function.
4110 i915_gem_execbuffer(struct drm_device *dev, void *data,
4111 struct drm_file *file)
4113 struct drm_i915_gem_execbuffer *args = data;
4114 struct drm_i915_gem_execbuffer2 exec2;
4115 struct drm_i915_gem_exec_object *exec_list = NULL;
4116 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4120 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4121 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4124 if (args->buffer_count < 1) {
4125 DRM_ERROR("execbuf with %d buffers\n", args->buffer_count);
4129 /* Copy in the exec list from userland */
4130 exec_list = drm_malloc_ab(sizeof(*exec_list), args->buffer_count);
4131 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4132 if (exec_list == NULL || exec2_list == NULL) {
4133 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4134 args->buffer_count);
4135 drm_free_large(exec_list);
4136 drm_free_large(exec2_list);
4139 ret = copy_from_user(exec_list,
4140 (struct drm_i915_relocation_entry __user *)
4141 (uintptr_t) args->buffers_ptr,
4142 sizeof(*exec_list) * args->buffer_count);
4144 DRM_ERROR("copy %d exec entries failed %d\n",
4145 args->buffer_count, ret);
4146 drm_free_large(exec_list);
4147 drm_free_large(exec2_list);
4151 for (i = 0; i < args->buffer_count; i++) {
4152 exec2_list[i].handle = exec_list[i].handle;
4153 exec2_list[i].relocation_count = exec_list[i].relocation_count;
4154 exec2_list[i].relocs_ptr = exec_list[i].relocs_ptr;
4155 exec2_list[i].alignment = exec_list[i].alignment;
4156 exec2_list[i].offset = exec_list[i].offset;
4157 if (INTEL_INFO(dev)->gen < 4)
4158 exec2_list[i].flags = EXEC_OBJECT_NEEDS_FENCE;
4160 exec2_list[i].flags = 0;
4163 exec2.buffers_ptr = args->buffers_ptr;
4164 exec2.buffer_count = args->buffer_count;
4165 exec2.batch_start_offset = args->batch_start_offset;
4166 exec2.batch_len = args->batch_len;
4167 exec2.DR1 = args->DR1;
4168 exec2.DR4 = args->DR4;
4169 exec2.num_cliprects = args->num_cliprects;
4170 exec2.cliprects_ptr = args->cliprects_ptr;
4171 exec2.flags = I915_EXEC_RENDER;
4173 ret = i915_gem_do_execbuffer(dev, data, file, &exec2, exec2_list);
4175 /* Copy the new buffer offsets back to the user's exec list. */
4176 for (i = 0; i < args->buffer_count; i++)
4177 exec_list[i].offset = exec2_list[i].offset;
4178 /* ... and back out to userspace */
4179 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4180 (uintptr_t) args->buffers_ptr,
4182 sizeof(*exec_list) * args->buffer_count);
4185 DRM_ERROR("failed to copy %d exec entries "
4186 "back to user (%d)\n",
4187 args->buffer_count, ret);
4191 drm_free_large(exec_list);
4192 drm_free_large(exec2_list);
4197 i915_gem_execbuffer2(struct drm_device *dev, void *data,
4198 struct drm_file *file)
4200 struct drm_i915_gem_execbuffer2 *args = data;
4201 struct drm_i915_gem_exec_object2 *exec2_list = NULL;
4205 DRM_INFO("buffers_ptr %d buffer_count %d len %08x\n",
4206 (int) args->buffers_ptr, args->buffer_count, args->batch_len);
4209 if (args->buffer_count < 1) {
4210 DRM_ERROR("execbuf2 with %d buffers\n", args->buffer_count);
4214 exec2_list = drm_malloc_ab(sizeof(*exec2_list), args->buffer_count);
4215 if (exec2_list == NULL) {
4216 DRM_ERROR("Failed to allocate exec list for %d buffers\n",
4217 args->buffer_count);
4220 ret = copy_from_user(exec2_list,
4221 (struct drm_i915_relocation_entry __user *)
4222 (uintptr_t) args->buffers_ptr,
4223 sizeof(*exec2_list) * args->buffer_count);
4225 DRM_ERROR("copy %d exec entries failed %d\n",
4226 args->buffer_count, ret);
4227 drm_free_large(exec2_list);
4231 ret = i915_gem_do_execbuffer(dev, data, file, args, exec2_list);
4233 /* Copy the new buffer offsets back to the user's exec list. */
4234 ret = copy_to_user((struct drm_i915_relocation_entry __user *)
4235 (uintptr_t) args->buffers_ptr,
4237 sizeof(*exec2_list) * args->buffer_count);
4240 DRM_ERROR("failed to copy %d exec entries "
4241 "back to user (%d)\n",
4242 args->buffer_count, ret);
4246 drm_free_large(exec2_list);
4251 i915_gem_object_pin(struct drm_i915_gem_object *obj,
4253 bool map_and_fenceable)
4255 struct drm_device *dev = obj->base.dev;
4256 struct drm_i915_private *dev_priv = dev->dev_private;
4259 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
4260 BUG_ON(map_and_fenceable && !map_and_fenceable);
4261 WARN_ON(i915_verify_lists(dev));
4263 if (obj->gtt_space != NULL) {
4264 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
4265 (map_and_fenceable && !obj->map_and_fenceable)) {
4266 WARN(obj->pin_count,
4267 "bo is already pinned with incorrect alignment:"
4268 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
4269 " obj->map_and_fenceable=%d\n",
4270 obj->gtt_offset, alignment,
4272 obj->map_and_fenceable);
4273 ret = i915_gem_object_unbind(obj);
4279 if (obj->gtt_space == NULL) {
4280 ret = i915_gem_object_bind_to_gtt(obj, alignment,
4286 if (obj->pin_count++ == 0) {
4287 i915_gem_info_add_pin(dev_priv, obj, map_and_fenceable);
4289 list_move_tail(&obj->mm_list,
4290 &dev_priv->mm.pinned_list);
4292 BUG_ON(!obj->pin_mappable && map_and_fenceable);
4294 WARN_ON(i915_verify_lists(dev));
4299 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
4301 struct drm_device *dev = obj->base.dev;
4302 drm_i915_private_t *dev_priv = dev->dev_private;
4304 WARN_ON(i915_verify_lists(dev));
4305 BUG_ON(obj->pin_count == 0);
4306 BUG_ON(obj->gtt_space == NULL);
4308 if (--obj->pin_count == 0) {
4310 list_move_tail(&obj->mm_list,
4311 &dev_priv->mm.inactive_list);
4312 i915_gem_info_remove_pin(dev_priv, obj);
4314 WARN_ON(i915_verify_lists(dev));
4318 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
4319 struct drm_file *file)
4321 struct drm_i915_gem_pin *args = data;
4322 struct drm_i915_gem_object *obj;
4325 ret = i915_mutex_lock_interruptible(dev);
4329 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4335 if (obj->madv != I915_MADV_WILLNEED) {
4336 DRM_ERROR("Attempting to pin a purgeable buffer\n");
4341 if (obj->pin_filp != NULL && obj->pin_filp != file) {
4342 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
4348 obj->user_pin_count++;
4349 obj->pin_filp = file;
4350 if (obj->user_pin_count == 1) {
4351 ret = i915_gem_object_pin(obj, args->alignment, true);
4356 /* XXX - flush the CPU caches for pinned objects
4357 * as the X server doesn't manage domains yet
4359 i915_gem_object_flush_cpu_write_domain(obj);
4360 args->offset = obj->gtt_offset;
4362 drm_gem_object_unreference(&obj->base);
4364 mutex_unlock(&dev->struct_mutex);
4369 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
4370 struct drm_file *file)
4372 struct drm_i915_gem_pin *args = data;
4373 struct drm_i915_gem_object *obj;
4376 ret = i915_mutex_lock_interruptible(dev);
4380 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4386 if (obj->pin_filp != file) {
4387 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
4392 obj->user_pin_count--;
4393 if (obj->user_pin_count == 0) {
4394 obj->pin_filp = NULL;
4395 i915_gem_object_unpin(obj);
4399 drm_gem_object_unreference(&obj->base);
4401 mutex_unlock(&dev->struct_mutex);
4406 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
4407 struct drm_file *file)
4409 struct drm_i915_gem_busy *args = data;
4410 struct drm_i915_gem_object *obj;
4413 ret = i915_mutex_lock_interruptible(dev);
4417 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
4423 /* Count all active objects as busy, even if they are currently not used
4424 * by the gpu. Users of this interface expect objects to eventually
4425 * become non-busy without any further actions, therefore emit any
4426 * necessary flushes here.
4428 args->busy = obj->active;
4430 /* Unconditionally flush objects, even when the gpu still uses this
4431 * object. Userspace calling this function indicates that it wants to
4432 * use this buffer rather sooner than later, so issuing the required
4433 * flush earlier is beneficial.
4435 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS)
4436 i915_gem_flush_ring(dev, obj->ring,
4437 0, obj->base.write_domain);
4439 /* Update the active list for the hardware's current position.
4440 * Otherwise this only updates on a delayed timer or when irqs
4441 * are actually unmasked, and our working set ends up being
4442 * larger than required.
4444 i915_gem_retire_requests_ring(dev, obj->ring);
4446 args->busy = obj->active;
4449 drm_gem_object_unreference(&obj->base);
4451 mutex_unlock(&dev->struct_mutex);
4456 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
4457 struct drm_file *file_priv)
4459 return i915_gem_ring_throttle(dev, file_priv);
4463 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
4464 struct drm_file *file_priv)
4466 struct drm_i915_gem_madvise *args = data;
4467 struct drm_i915_gem_object *obj;
4470 switch (args->madv) {
4471 case I915_MADV_DONTNEED:
4472 case I915_MADV_WILLNEED:
4478 ret = i915_mutex_lock_interruptible(dev);
4482 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
4488 if (obj->pin_count) {
4493 if (obj->madv != __I915_MADV_PURGED)
4494 obj->madv = args->madv;
4496 /* if the object is no longer bound, discard its backing storage */
4497 if (i915_gem_object_is_purgeable(obj) &&
4498 obj->gtt_space == NULL)
4499 i915_gem_object_truncate(obj);
4501 args->retained = obj->madv != __I915_MADV_PURGED;
4504 drm_gem_object_unreference(&obj->base);
4506 mutex_unlock(&dev->struct_mutex);
4510 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
4513 struct drm_i915_private *dev_priv = dev->dev_private;
4514 struct drm_i915_gem_object *obj;
4516 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
4520 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
4525 i915_gem_info_add_obj(dev_priv, size);
4527 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
4528 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
4530 obj->agp_type = AGP_USER_MEMORY;
4531 obj->base.driver_private = NULL;
4532 obj->fence_reg = I915_FENCE_REG_NONE;
4533 INIT_LIST_HEAD(&obj->mm_list);
4534 INIT_LIST_HEAD(&obj->gtt_list);
4535 INIT_LIST_HEAD(&obj->ring_list);
4536 INIT_LIST_HEAD(&obj->gpu_write_list);
4537 obj->madv = I915_MADV_WILLNEED;
4538 /* Avoid an unnecessary call to unbind on the first bind. */
4539 obj->map_and_fenceable = true;
4544 int i915_gem_init_object(struct drm_gem_object *obj)
4551 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
4553 struct drm_device *dev = obj->base.dev;
4554 drm_i915_private_t *dev_priv = dev->dev_private;
4557 ret = i915_gem_object_unbind(obj);
4558 if (ret == -ERESTARTSYS) {
4559 list_move(&obj->mm_list,
4560 &dev_priv->mm.deferred_free_list);
4564 if (obj->base.map_list.map)
4565 i915_gem_free_mmap_offset(obj);
4567 drm_gem_object_release(&obj->base);
4568 i915_gem_info_remove_obj(dev_priv, obj->base.size);
4570 kfree(obj->page_cpu_valid);
4575 void i915_gem_free_object(struct drm_gem_object *gem_obj)
4577 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
4578 struct drm_device *dev = obj->base.dev;
4580 trace_i915_gem_object_destroy(obj);
4582 while (obj->pin_count > 0)
4583 i915_gem_object_unpin(obj);
4586 i915_gem_detach_phys_object(dev, obj);
4588 i915_gem_free_object_tail(obj);
4592 i915_gem_idle(struct drm_device *dev)
4594 drm_i915_private_t *dev_priv = dev->dev_private;
4597 mutex_lock(&dev->struct_mutex);
4599 if (dev_priv->mm.suspended) {
4600 mutex_unlock(&dev->struct_mutex);
4604 ret = i915_gpu_idle(dev);
4606 mutex_unlock(&dev->struct_mutex);
4610 /* Under UMS, be paranoid and evict. */
4611 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
4612 ret = i915_gem_evict_inactive(dev, false);
4614 mutex_unlock(&dev->struct_mutex);
4619 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4620 * We need to replace this with a semaphore, or something.
4621 * And not confound mm.suspended!
4623 dev_priv->mm.suspended = 1;
4624 del_timer_sync(&dev_priv->hangcheck_timer);
4626 i915_kernel_lost_context(dev);
4627 i915_gem_cleanup_ringbuffer(dev);
4629 mutex_unlock(&dev->struct_mutex);
4631 /* Cancel the retire work handler, which should be idle now. */
4632 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4638 * 965+ support PIPE_CONTROL commands, which provide finer grained control
4639 * over cache flushing.
4642 i915_gem_init_pipe_control(struct drm_device *dev)
4644 drm_i915_private_t *dev_priv = dev->dev_private;
4645 struct drm_i915_gem_object *obj;
4648 obj = i915_gem_alloc_object(dev, 4096);
4650 DRM_ERROR("Failed to allocate seqno page\n");
4654 obj->agp_type = AGP_USER_CACHED_MEMORY;
4656 ret = i915_gem_object_pin(obj, 4096, true);
4660 dev_priv->seqno_gfx_addr = obj->gtt_offset;
4661 dev_priv->seqno_page = kmap(obj->pages[0]);
4662 if (dev_priv->seqno_page == NULL)
4665 dev_priv->seqno_obj = obj;
4666 memset(dev_priv->seqno_page, 0, PAGE_SIZE);
4671 i915_gem_object_unpin(obj);
4673 drm_gem_object_unreference(&obj->base);
4680 i915_gem_cleanup_pipe_control(struct drm_device *dev)
4682 drm_i915_private_t *dev_priv = dev->dev_private;
4683 struct drm_i915_gem_object *obj;
4685 obj = dev_priv->seqno_obj;
4686 kunmap(obj->pages[0]);
4687 i915_gem_object_unpin(obj);
4688 drm_gem_object_unreference(&obj->base);
4689 dev_priv->seqno_obj = NULL;
4691 dev_priv->seqno_page = NULL;
4695 i915_gem_init_ringbuffer(struct drm_device *dev)
4697 drm_i915_private_t *dev_priv = dev->dev_private;
4700 if (HAS_PIPE_CONTROL(dev)) {
4701 ret = i915_gem_init_pipe_control(dev);
4706 ret = intel_init_render_ring_buffer(dev);
4708 goto cleanup_pipe_control;
4711 ret = intel_init_bsd_ring_buffer(dev);
4713 goto cleanup_render_ring;
4717 ret = intel_init_blt_ring_buffer(dev);
4719 goto cleanup_bsd_ring;
4722 dev_priv->next_seqno = 1;
4727 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4728 cleanup_render_ring:
4729 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4730 cleanup_pipe_control:
4731 if (HAS_PIPE_CONTROL(dev))
4732 i915_gem_cleanup_pipe_control(dev);
4737 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4739 drm_i915_private_t *dev_priv = dev->dev_private;
4741 intel_cleanup_ring_buffer(&dev_priv->render_ring);
4742 intel_cleanup_ring_buffer(&dev_priv->bsd_ring);
4743 intel_cleanup_ring_buffer(&dev_priv->blt_ring);
4744 if (HAS_PIPE_CONTROL(dev))
4745 i915_gem_cleanup_pipe_control(dev);
4749 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4750 struct drm_file *file_priv)
4752 drm_i915_private_t *dev_priv = dev->dev_private;
4755 if (drm_core_check_feature(dev, DRIVER_MODESET))
4758 if (atomic_read(&dev_priv->mm.wedged)) {
4759 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4760 atomic_set(&dev_priv->mm.wedged, 0);
4763 mutex_lock(&dev->struct_mutex);
4764 dev_priv->mm.suspended = 0;
4766 ret = i915_gem_init_ringbuffer(dev);
4768 mutex_unlock(&dev->struct_mutex);
4772 BUG_ON(!list_empty(&dev_priv->mm.active_list));
4773 BUG_ON(!list_empty(&dev_priv->render_ring.active_list));
4774 BUG_ON(!list_empty(&dev_priv->bsd_ring.active_list));
4775 BUG_ON(!list_empty(&dev_priv->blt_ring.active_list));
4776 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
4777 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
4778 BUG_ON(!list_empty(&dev_priv->render_ring.request_list));
4779 BUG_ON(!list_empty(&dev_priv->bsd_ring.request_list));
4780 BUG_ON(!list_empty(&dev_priv->blt_ring.request_list));
4781 mutex_unlock(&dev->struct_mutex);
4783 ret = drm_irq_install(dev);
4785 goto cleanup_ringbuffer;
4790 mutex_lock(&dev->struct_mutex);
4791 i915_gem_cleanup_ringbuffer(dev);
4792 dev_priv->mm.suspended = 1;
4793 mutex_unlock(&dev->struct_mutex);
4799 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4800 struct drm_file *file_priv)
4802 if (drm_core_check_feature(dev, DRIVER_MODESET))
4805 drm_irq_uninstall(dev);
4806 return i915_gem_idle(dev);
4810 i915_gem_lastclose(struct drm_device *dev)
4814 if (drm_core_check_feature(dev, DRIVER_MODESET))
4817 ret = i915_gem_idle(dev);
4819 DRM_ERROR("failed to idle hardware: %d\n", ret);
4823 init_ring_lists(struct intel_ring_buffer *ring)
4825 INIT_LIST_HEAD(&ring->active_list);
4826 INIT_LIST_HEAD(&ring->request_list);
4827 INIT_LIST_HEAD(&ring->gpu_write_list);
4831 i915_gem_load(struct drm_device *dev)
4834 drm_i915_private_t *dev_priv = dev->dev_private;
4836 INIT_LIST_HEAD(&dev_priv->mm.active_list);
4837 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
4838 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
4839 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
4840 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4841 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
4842 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
4843 init_ring_lists(&dev_priv->render_ring);
4844 init_ring_lists(&dev_priv->bsd_ring);
4845 init_ring_lists(&dev_priv->blt_ring);
4846 for (i = 0; i < 16; i++)
4847 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4848 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4849 i915_gem_retire_work_handler);
4850 init_completion(&dev_priv->error_completion);
4852 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4854 u32 tmp = I915_READ(MI_ARB_STATE);
4855 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
4856 /* arb state is a masked write, so set bit + bit in mask */
4857 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
4858 I915_WRITE(MI_ARB_STATE, tmp);
4862 /* Old X drivers will take 0-2 for front, back, depth buffers */
4863 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4864 dev_priv->fence_reg_start = 3;
4866 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4867 dev_priv->num_fence_regs = 16;
4869 dev_priv->num_fence_regs = 8;
4871 /* Initialize fence registers to zero */
4872 switch (INTEL_INFO(dev)->gen) {
4874 for (i = 0; i < 16; i++)
4875 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + (i * 8), 0);
4879 for (i = 0; i < 16; i++)
4880 I915_WRITE64(FENCE_REG_965_0 + (i * 8), 0);
4883 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4884 for (i = 0; i < 8; i++)
4885 I915_WRITE(FENCE_REG_945_8 + (i * 4), 0);
4887 for (i = 0; i < 8; i++)
4888 I915_WRITE(FENCE_REG_830_0 + (i * 4), 0);
4891 i915_gem_detect_bit_6_swizzle(dev);
4892 init_waitqueue_head(&dev_priv->pending_flip_queue);
4894 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4895 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4896 register_shrinker(&dev_priv->mm.inactive_shrinker);
4900 * Create a physically contiguous memory object for this object
4901 * e.g. for cursor + overlay regs
4903 static int i915_gem_init_phys_object(struct drm_device *dev,
4904 int id, int size, int align)
4906 drm_i915_private_t *dev_priv = dev->dev_private;
4907 struct drm_i915_gem_phys_object *phys_obj;
4910 if (dev_priv->mm.phys_objs[id - 1] || !size)
4913 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4919 phys_obj->handle = drm_pci_alloc(dev, size, align);
4920 if (!phys_obj->handle) {
4925 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4928 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4936 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4938 drm_i915_private_t *dev_priv = dev->dev_private;
4939 struct drm_i915_gem_phys_object *phys_obj;
4941 if (!dev_priv->mm.phys_objs[id - 1])
4944 phys_obj = dev_priv->mm.phys_objs[id - 1];
4945 if (phys_obj->cur_obj) {
4946 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4950 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4952 drm_pci_free(dev, phys_obj->handle);
4954 dev_priv->mm.phys_objs[id - 1] = NULL;
4957 void i915_gem_free_all_phys_object(struct drm_device *dev)
4961 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4962 i915_gem_free_phys_object(dev, i);
4965 void i915_gem_detach_phys_object(struct drm_device *dev,
4966 struct drm_i915_gem_object *obj)
4968 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4975 vaddr = obj->phys_obj->handle->vaddr;
4977 page_count = obj->base.size / PAGE_SIZE;
4978 for (i = 0; i < page_count; i++) {
4979 struct page *page = read_cache_page_gfp(mapping, i,
4980 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4981 if (!IS_ERR(page)) {
4982 char *dst = kmap_atomic(page);
4983 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4986 drm_clflush_pages(&page, 1);
4988 set_page_dirty(page);
4989 mark_page_accessed(page);
4990 page_cache_release(page);
4993 intel_gtt_chipset_flush();
4995 obj->phys_obj->cur_obj = NULL;
4996 obj->phys_obj = NULL;
5000 i915_gem_attach_phys_object(struct drm_device *dev,
5001 struct drm_i915_gem_object *obj,
5005 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
5006 drm_i915_private_t *dev_priv = dev->dev_private;
5011 if (id > I915_MAX_PHYS_OBJECT)
5014 if (obj->phys_obj) {
5015 if (obj->phys_obj->id == id)
5017 i915_gem_detach_phys_object(dev, obj);
5020 /* create a new object */
5021 if (!dev_priv->mm.phys_objs[id - 1]) {
5022 ret = i915_gem_init_phys_object(dev, id,
5023 obj->base.size, align);
5025 DRM_ERROR("failed to init phys object %d size: %zu\n",
5026 id, obj->base.size);
5031 /* bind to the object */
5032 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
5033 obj->phys_obj->cur_obj = obj;
5035 page_count = obj->base.size / PAGE_SIZE;
5037 for (i = 0; i < page_count; i++) {
5041 page = read_cache_page_gfp(mapping, i,
5042 GFP_HIGHUSER | __GFP_RECLAIMABLE);
5044 return PTR_ERR(page);
5046 src = kmap_atomic(page);
5047 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
5048 memcpy(dst, src, PAGE_SIZE);
5051 mark_page_accessed(page);
5052 page_cache_release(page);
5059 i915_gem_phys_pwrite(struct drm_device *dev,
5060 struct drm_i915_gem_object *obj,
5061 struct drm_i915_gem_pwrite *args,
5062 struct drm_file *file_priv)
5064 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
5065 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
5067 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
5068 unsigned long unwritten;
5070 /* The physical object once assigned is fixed for the lifetime
5071 * of the obj, so we can safely drop the lock and continue
5074 mutex_unlock(&dev->struct_mutex);
5075 unwritten = copy_from_user(vaddr, user_data, args->size);
5076 mutex_lock(&dev->struct_mutex);
5081 intel_gtt_chipset_flush();
5085 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
5087 struct drm_i915_file_private *file_priv = file->driver_priv;
5089 /* Clean up our request list when the client is going away, so that
5090 * later retire_requests won't dereference our soon-to-be-gone
5093 spin_lock(&file_priv->mm.lock);
5094 while (!list_empty(&file_priv->mm.request_list)) {
5095 struct drm_i915_gem_request *request;
5097 request = list_first_entry(&file_priv->mm.request_list,
5098 struct drm_i915_gem_request,
5100 list_del(&request->client_list);
5101 request->file_priv = NULL;
5103 spin_unlock(&file_priv->mm.lock);
5107 i915_gpu_is_active(struct drm_device *dev)
5109 drm_i915_private_t *dev_priv = dev->dev_private;
5112 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
5113 list_empty(&dev_priv->mm.active_list);
5115 return !lists_empty;
5119 i915_gem_inactive_shrink(struct shrinker *shrinker,
5123 struct drm_i915_private *dev_priv =
5124 container_of(shrinker,
5125 struct drm_i915_private,
5126 mm.inactive_shrinker);
5127 struct drm_device *dev = dev_priv->dev;
5128 struct drm_i915_gem_object *obj, *next;
5131 if (!mutex_trylock(&dev->struct_mutex))
5134 /* "fast-path" to count number of available objects */
5135 if (nr_to_scan == 0) {
5137 list_for_each_entry(obj,
5138 &dev_priv->mm.inactive_list,
5141 mutex_unlock(&dev->struct_mutex);
5142 return cnt / 100 * sysctl_vfs_cache_pressure;
5146 /* first scan for clean buffers */
5147 i915_gem_retire_requests(dev);
5149 list_for_each_entry_safe(obj, next,
5150 &dev_priv->mm.inactive_list,
5152 if (i915_gem_object_is_purgeable(obj)) {
5153 i915_gem_object_unbind(obj);
5154 if (--nr_to_scan == 0)
5159 /* second pass, evict/count anything still on the inactive list */
5161 list_for_each_entry_safe(obj, next,
5162 &dev_priv->mm.inactive_list,
5165 i915_gem_object_unbind(obj);
5171 if (nr_to_scan && i915_gpu_is_active(dev)) {
5173 * We are desperate for pages, so as a last resort, wait
5174 * for the GPU to finish and discard whatever we can.
5175 * This has a dramatic impact to reduce the number of
5176 * OOM-killer events whilst running the GPU aggressively.
5178 if (i915_gpu_idle(dev) == 0)
5181 mutex_unlock(&dev->struct_mutex);
5182 return cnt / 100 * sysctl_vfs_cache_pressure;