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 static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
39 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
40 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
41 static __must_check int i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj,
43 static __must_check int i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
46 static void i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj);
47 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
49 bool map_and_fenceable);
50 static void i915_gem_clear_fence_reg(struct drm_device *dev,
51 struct drm_i915_fence_reg *reg);
52 static int i915_gem_phys_pwrite(struct drm_device *dev,
53 struct drm_i915_gem_object *obj,
54 struct drm_i915_gem_pwrite *args,
55 struct drm_file *file);
56 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
58 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
59 struct shrink_control *sc);
61 /* some bookkeeping */
62 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
65 dev_priv->mm.object_count++;
66 dev_priv->mm.object_memory += size;
69 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
72 dev_priv->mm.object_count--;
73 dev_priv->mm.object_memory -= size;
77 i915_gem_wait_for_error(struct drm_device *dev)
79 struct drm_i915_private *dev_priv = dev->dev_private;
80 struct completion *x = &dev_priv->error_completion;
84 if (!atomic_read(&dev_priv->mm.wedged))
87 ret = wait_for_completion_interruptible(x);
91 if (atomic_read(&dev_priv->mm.wedged)) {
92 /* GPU is hung, bump the completion count to account for
93 * the token we just consumed so that we never hit zero and
94 * end up waiting upon a subsequent completion event that
97 spin_lock_irqsave(&x->wait.lock, flags);
99 spin_unlock_irqrestore(&x->wait.lock, flags);
104 int i915_mutex_lock_interruptible(struct drm_device *dev)
108 ret = i915_gem_wait_for_error(dev);
112 ret = mutex_lock_interruptible(&dev->struct_mutex);
116 WARN_ON(i915_verify_lists(dev));
121 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
123 return obj->gtt_space && !obj->active && obj->pin_count == 0;
126 void i915_gem_do_init(struct drm_device *dev,
128 unsigned long mappable_end,
131 drm_i915_private_t *dev_priv = dev->dev_private;
133 drm_mm_init(&dev_priv->mm.gtt_space, start, end - start);
135 dev_priv->mm.gtt_start = start;
136 dev_priv->mm.gtt_mappable_end = mappable_end;
137 dev_priv->mm.gtt_end = end;
138 dev_priv->mm.gtt_total = end - start;
139 dev_priv->mm.mappable_gtt_total = min(end, mappable_end) - start;
141 /* Take over this portion of the GTT */
142 intel_gtt_clear_range(start / PAGE_SIZE, (end-start) / PAGE_SIZE);
146 i915_gem_init_ioctl(struct drm_device *dev, void *data,
147 struct drm_file *file)
149 struct drm_i915_gem_init *args = data;
151 if (args->gtt_start >= args->gtt_end ||
152 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
155 mutex_lock(&dev->struct_mutex);
156 i915_gem_do_init(dev, args->gtt_start, args->gtt_end, args->gtt_end);
157 mutex_unlock(&dev->struct_mutex);
163 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
164 struct drm_file *file)
166 struct drm_i915_private *dev_priv = dev->dev_private;
167 struct drm_i915_gem_get_aperture *args = data;
168 struct drm_i915_gem_object *obj;
171 if (!(dev->driver->driver_features & DRIVER_GEM))
175 mutex_lock(&dev->struct_mutex);
176 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
177 pinned += obj->gtt_space->size;
178 mutex_unlock(&dev->struct_mutex);
180 args->aper_size = dev_priv->mm.gtt_total;
181 args->aper_available_size = args->aper_size -pinned;
187 i915_gem_create(struct drm_file *file,
188 struct drm_device *dev,
192 struct drm_i915_gem_object *obj;
196 size = roundup(size, PAGE_SIZE);
198 /* Allocate the new object */
199 obj = i915_gem_alloc_object(dev, size);
203 ret = drm_gem_handle_create(file, &obj->base, &handle);
205 drm_gem_object_release(&obj->base);
206 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
211 /* drop reference from allocate - handle holds it now */
212 drm_gem_object_unreference(&obj->base);
213 trace_i915_gem_object_create(obj);
220 i915_gem_dumb_create(struct drm_file *file,
221 struct drm_device *dev,
222 struct drm_mode_create_dumb *args)
224 /* have to work out size/pitch and return them */
225 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
226 args->size = args->pitch * args->height;
227 return i915_gem_create(file, dev,
228 args->size, &args->handle);
231 int i915_gem_dumb_destroy(struct drm_file *file,
232 struct drm_device *dev,
235 return drm_gem_handle_delete(file, handle);
239 * Creates a new mm object and returns a handle to it.
242 i915_gem_create_ioctl(struct drm_device *dev, void *data,
243 struct drm_file *file)
245 struct drm_i915_gem_create *args = data;
246 return i915_gem_create(file, dev,
247 args->size, &args->handle);
250 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
252 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
254 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
255 obj->tiling_mode != I915_TILING_NONE;
259 slow_shmem_copy(struct page *dst_page,
261 struct page *src_page,
265 char *dst_vaddr, *src_vaddr;
267 dst_vaddr = kmap(dst_page);
268 src_vaddr = kmap(src_page);
270 memcpy(dst_vaddr + dst_offset, src_vaddr + src_offset, length);
277 slow_shmem_bit17_copy(struct page *gpu_page,
279 struct page *cpu_page,
284 char *gpu_vaddr, *cpu_vaddr;
286 /* Use the unswizzled path if this page isn't affected. */
287 if ((page_to_phys(gpu_page) & (1 << 17)) == 0) {
289 return slow_shmem_copy(cpu_page, cpu_offset,
290 gpu_page, gpu_offset, length);
292 return slow_shmem_copy(gpu_page, gpu_offset,
293 cpu_page, cpu_offset, length);
296 gpu_vaddr = kmap(gpu_page);
297 cpu_vaddr = kmap(cpu_page);
299 /* Copy the data, XORing A6 with A17 (1). The user already knows he's
300 * XORing with the other bits (A9 for Y, A9 and A10 for X)
303 int cacheline_end = ALIGN(gpu_offset + 1, 64);
304 int this_length = min(cacheline_end - gpu_offset, length);
305 int swizzled_gpu_offset = gpu_offset ^ 64;
308 memcpy(cpu_vaddr + cpu_offset,
309 gpu_vaddr + swizzled_gpu_offset,
312 memcpy(gpu_vaddr + swizzled_gpu_offset,
313 cpu_vaddr + cpu_offset,
316 cpu_offset += this_length;
317 gpu_offset += this_length;
318 length -= this_length;
326 * This is the fast shmem pread path, which attempts to copy_from_user directly
327 * from the backing pages of the object to the user's address space. On a
328 * fault, it fails so we can fall back to i915_gem_shmem_pwrite_slow().
331 i915_gem_shmem_pread_fast(struct drm_device *dev,
332 struct drm_i915_gem_object *obj,
333 struct drm_i915_gem_pread *args,
334 struct drm_file *file)
336 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
339 char __user *user_data;
340 int page_offset, page_length;
342 user_data = (char __user *) (uintptr_t) args->data_ptr;
345 offset = args->offset;
352 /* Operation in this page
354 * page_offset = offset within page
355 * page_length = bytes to copy for this page
357 page_offset = offset_in_page(offset);
358 page_length = remain;
359 if ((page_offset + remain) > PAGE_SIZE)
360 page_length = PAGE_SIZE - page_offset;
362 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
363 GFP_HIGHUSER | __GFP_RECLAIMABLE);
365 return PTR_ERR(page);
367 vaddr = kmap_atomic(page);
368 ret = __copy_to_user_inatomic(user_data,
371 kunmap_atomic(vaddr);
373 mark_page_accessed(page);
374 page_cache_release(page);
378 remain -= page_length;
379 user_data += page_length;
380 offset += page_length;
387 * This is the fallback shmem pread path, which allocates temporary storage
388 * in kernel space to copy_to_user into outside of the struct_mutex, so we
389 * can copy out of the object's backing pages while holding the struct mutex
390 * and not take page faults.
393 i915_gem_shmem_pread_slow(struct drm_device *dev,
394 struct drm_i915_gem_object *obj,
395 struct drm_i915_gem_pread *args,
396 struct drm_file *file)
398 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
399 struct mm_struct *mm = current->mm;
400 struct page **user_pages;
402 loff_t offset, pinned_pages, i;
403 loff_t first_data_page, last_data_page, num_pages;
404 int shmem_page_offset;
405 int data_page_index, data_page_offset;
408 uint64_t data_ptr = args->data_ptr;
409 int do_bit17_swizzling;
413 /* Pin the user pages containing the data. We can't fault while
414 * holding the struct mutex, yet we want to hold it while
415 * dereferencing the user data.
417 first_data_page = data_ptr / PAGE_SIZE;
418 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
419 num_pages = last_data_page - first_data_page + 1;
421 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
422 if (user_pages == NULL)
425 mutex_unlock(&dev->struct_mutex);
426 down_read(&mm->mmap_sem);
427 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
428 num_pages, 1, 0, user_pages, NULL);
429 up_read(&mm->mmap_sem);
430 mutex_lock(&dev->struct_mutex);
431 if (pinned_pages < num_pages) {
436 ret = i915_gem_object_set_cpu_read_domain_range(obj,
442 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
444 offset = args->offset;
449 /* Operation in this page
451 * shmem_page_offset = offset within page in shmem file
452 * data_page_index = page number in get_user_pages return
453 * data_page_offset = offset with data_page_index page.
454 * page_length = bytes to copy for this page
456 shmem_page_offset = offset_in_page(offset);
457 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
458 data_page_offset = offset_in_page(data_ptr);
460 page_length = remain;
461 if ((shmem_page_offset + page_length) > PAGE_SIZE)
462 page_length = PAGE_SIZE - shmem_page_offset;
463 if ((data_page_offset + page_length) > PAGE_SIZE)
464 page_length = PAGE_SIZE - data_page_offset;
466 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
467 GFP_HIGHUSER | __GFP_RECLAIMABLE);
473 if (do_bit17_swizzling) {
474 slow_shmem_bit17_copy(page,
476 user_pages[data_page_index],
481 slow_shmem_copy(user_pages[data_page_index],
488 mark_page_accessed(page);
489 page_cache_release(page);
491 remain -= page_length;
492 data_ptr += page_length;
493 offset += page_length;
497 for (i = 0; i < pinned_pages; i++) {
498 SetPageDirty(user_pages[i]);
499 mark_page_accessed(user_pages[i]);
500 page_cache_release(user_pages[i]);
502 drm_free_large(user_pages);
508 * Reads data from the object referenced by handle.
510 * On error, the contents of *data are undefined.
513 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
514 struct drm_file *file)
516 struct drm_i915_gem_pread *args = data;
517 struct drm_i915_gem_object *obj;
523 if (!access_ok(VERIFY_WRITE,
524 (char __user *)(uintptr_t)args->data_ptr,
528 ret = fault_in_pages_writeable((char __user *)(uintptr_t)args->data_ptr,
533 ret = i915_mutex_lock_interruptible(dev);
537 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
538 if (&obj->base == NULL) {
543 /* Bounds check source. */
544 if (args->offset > obj->base.size ||
545 args->size > obj->base.size - args->offset) {
550 trace_i915_gem_object_pread(obj, args->offset, args->size);
552 ret = i915_gem_object_set_cpu_read_domain_range(obj,
559 if (!i915_gem_object_needs_bit17_swizzle(obj))
560 ret = i915_gem_shmem_pread_fast(dev, obj, args, file);
562 ret = i915_gem_shmem_pread_slow(dev, obj, args, file);
565 drm_gem_object_unreference(&obj->base);
567 mutex_unlock(&dev->struct_mutex);
571 /* This is the fast write path which cannot handle
572 * page faults in the source data
576 fast_user_write(struct io_mapping *mapping,
577 loff_t page_base, int page_offset,
578 char __user *user_data,
582 unsigned long unwritten;
584 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
585 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
587 io_mapping_unmap_atomic(vaddr_atomic);
591 /* Here's the write path which can sleep for
596 slow_kernel_write(struct io_mapping *mapping,
597 loff_t gtt_base, int gtt_offset,
598 struct page *user_page, int user_offset,
601 char __iomem *dst_vaddr;
604 dst_vaddr = io_mapping_map_wc(mapping, gtt_base);
605 src_vaddr = kmap(user_page);
607 memcpy_toio(dst_vaddr + gtt_offset,
608 src_vaddr + user_offset,
612 io_mapping_unmap(dst_vaddr);
616 * This is the fast pwrite path, where we copy the data directly from the
617 * user into the GTT, uncached.
620 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
621 struct drm_i915_gem_object *obj,
622 struct drm_i915_gem_pwrite *args,
623 struct drm_file *file)
625 drm_i915_private_t *dev_priv = dev->dev_private;
627 loff_t offset, page_base;
628 char __user *user_data;
629 int page_offset, page_length;
631 user_data = (char __user *) (uintptr_t) args->data_ptr;
634 offset = obj->gtt_offset + args->offset;
637 /* Operation in this page
639 * page_base = page offset within aperture
640 * page_offset = offset within page
641 * page_length = bytes to copy for this page
643 page_base = offset & PAGE_MASK;
644 page_offset = offset_in_page(offset);
645 page_length = remain;
646 if ((page_offset + remain) > PAGE_SIZE)
647 page_length = PAGE_SIZE - page_offset;
649 /* If we get a fault while copying data, then (presumably) our
650 * source page isn't available. Return the error and we'll
651 * retry in the slow path.
653 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
654 page_offset, user_data, page_length))
657 remain -= page_length;
658 user_data += page_length;
659 offset += page_length;
666 * This is the fallback GTT pwrite path, which uses get_user_pages to pin
667 * the memory and maps it using kmap_atomic for copying.
669 * This code resulted in x11perf -rgb10text consuming about 10% more CPU
670 * than using i915_gem_gtt_pwrite_fast on a G45 (32-bit).
673 i915_gem_gtt_pwrite_slow(struct drm_device *dev,
674 struct drm_i915_gem_object *obj,
675 struct drm_i915_gem_pwrite *args,
676 struct drm_file *file)
678 drm_i915_private_t *dev_priv = dev->dev_private;
680 loff_t gtt_page_base, offset;
681 loff_t first_data_page, last_data_page, num_pages;
682 loff_t pinned_pages, i;
683 struct page **user_pages;
684 struct mm_struct *mm = current->mm;
685 int gtt_page_offset, data_page_offset, data_page_index, page_length;
687 uint64_t data_ptr = args->data_ptr;
691 /* Pin the user pages containing the data. We can't fault while
692 * holding the struct mutex, and all of the pwrite implementations
693 * want to hold it while dereferencing the user data.
695 first_data_page = data_ptr / PAGE_SIZE;
696 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
697 num_pages = last_data_page - first_data_page + 1;
699 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
700 if (user_pages == NULL)
703 mutex_unlock(&dev->struct_mutex);
704 down_read(&mm->mmap_sem);
705 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
706 num_pages, 0, 0, user_pages, NULL);
707 up_read(&mm->mmap_sem);
708 mutex_lock(&dev->struct_mutex);
709 if (pinned_pages < num_pages) {
711 goto out_unpin_pages;
714 ret = i915_gem_object_set_to_gtt_domain(obj, true);
716 goto out_unpin_pages;
718 ret = i915_gem_object_put_fence(obj);
720 goto out_unpin_pages;
722 offset = obj->gtt_offset + args->offset;
725 /* Operation in this page
727 * gtt_page_base = page offset within aperture
728 * gtt_page_offset = offset within page in aperture
729 * data_page_index = page number in get_user_pages return
730 * data_page_offset = offset with data_page_index page.
731 * page_length = bytes to copy for this page
733 gtt_page_base = offset & PAGE_MASK;
734 gtt_page_offset = offset_in_page(offset);
735 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
736 data_page_offset = offset_in_page(data_ptr);
738 page_length = remain;
739 if ((gtt_page_offset + page_length) > PAGE_SIZE)
740 page_length = PAGE_SIZE - gtt_page_offset;
741 if ((data_page_offset + page_length) > PAGE_SIZE)
742 page_length = PAGE_SIZE - data_page_offset;
744 slow_kernel_write(dev_priv->mm.gtt_mapping,
745 gtt_page_base, gtt_page_offset,
746 user_pages[data_page_index],
750 remain -= page_length;
751 offset += page_length;
752 data_ptr += page_length;
756 for (i = 0; i < pinned_pages; i++)
757 page_cache_release(user_pages[i]);
758 drm_free_large(user_pages);
764 * This is the fast shmem pwrite path, which attempts to directly
765 * copy_from_user into the kmapped pages backing the object.
768 i915_gem_shmem_pwrite_fast(struct drm_device *dev,
769 struct drm_i915_gem_object *obj,
770 struct drm_i915_gem_pwrite *args,
771 struct drm_file *file)
773 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
776 char __user *user_data;
777 int page_offset, page_length;
779 user_data = (char __user *) (uintptr_t) args->data_ptr;
782 offset = args->offset;
790 /* Operation in this page
792 * page_offset = offset within page
793 * page_length = bytes to copy for this page
795 page_offset = offset_in_page(offset);
796 page_length = remain;
797 if ((page_offset + remain) > PAGE_SIZE)
798 page_length = PAGE_SIZE - page_offset;
800 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
801 GFP_HIGHUSER | __GFP_RECLAIMABLE);
803 return PTR_ERR(page);
805 vaddr = kmap_atomic(page, KM_USER0);
806 ret = __copy_from_user_inatomic(vaddr + page_offset,
809 kunmap_atomic(vaddr, KM_USER0);
811 set_page_dirty(page);
812 mark_page_accessed(page);
813 page_cache_release(page);
815 /* If we get a fault while copying data, then (presumably) our
816 * source page isn't available. Return the error and we'll
817 * retry in the slow path.
822 remain -= page_length;
823 user_data += page_length;
824 offset += page_length;
831 * This is the fallback shmem pwrite path, which uses get_user_pages to pin
832 * the memory and maps it using kmap_atomic for copying.
834 * This avoids taking mmap_sem for faulting on the user's address while the
835 * struct_mutex is held.
838 i915_gem_shmem_pwrite_slow(struct drm_device *dev,
839 struct drm_i915_gem_object *obj,
840 struct drm_i915_gem_pwrite *args,
841 struct drm_file *file)
843 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
844 struct mm_struct *mm = current->mm;
845 struct page **user_pages;
847 loff_t offset, pinned_pages, i;
848 loff_t first_data_page, last_data_page, num_pages;
849 int shmem_page_offset;
850 int data_page_index, data_page_offset;
853 uint64_t data_ptr = args->data_ptr;
854 int do_bit17_swizzling;
858 /* Pin the user pages containing the data. We can't fault while
859 * holding the struct mutex, and all of the pwrite implementations
860 * want to hold it while dereferencing the user data.
862 first_data_page = data_ptr / PAGE_SIZE;
863 last_data_page = (data_ptr + args->size - 1) / PAGE_SIZE;
864 num_pages = last_data_page - first_data_page + 1;
866 user_pages = drm_malloc_ab(num_pages, sizeof(struct page *));
867 if (user_pages == NULL)
870 mutex_unlock(&dev->struct_mutex);
871 down_read(&mm->mmap_sem);
872 pinned_pages = get_user_pages(current, mm, (uintptr_t)args->data_ptr,
873 num_pages, 0, 0, user_pages, NULL);
874 up_read(&mm->mmap_sem);
875 mutex_lock(&dev->struct_mutex);
876 if (pinned_pages < num_pages) {
881 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
885 do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
887 offset = args->offset;
893 /* Operation in this page
895 * shmem_page_offset = offset within page in shmem file
896 * data_page_index = page number in get_user_pages return
897 * data_page_offset = offset with data_page_index page.
898 * page_length = bytes to copy for this page
900 shmem_page_offset = offset_in_page(offset);
901 data_page_index = data_ptr / PAGE_SIZE - first_data_page;
902 data_page_offset = offset_in_page(data_ptr);
904 page_length = remain;
905 if ((shmem_page_offset + page_length) > PAGE_SIZE)
906 page_length = PAGE_SIZE - shmem_page_offset;
907 if ((data_page_offset + page_length) > PAGE_SIZE)
908 page_length = PAGE_SIZE - data_page_offset;
910 page = read_cache_page_gfp(mapping, offset >> PAGE_SHIFT,
911 GFP_HIGHUSER | __GFP_RECLAIMABLE);
917 if (do_bit17_swizzling) {
918 slow_shmem_bit17_copy(page,
920 user_pages[data_page_index],
925 slow_shmem_copy(page,
927 user_pages[data_page_index],
932 set_page_dirty(page);
933 mark_page_accessed(page);
934 page_cache_release(page);
936 remain -= page_length;
937 data_ptr += page_length;
938 offset += page_length;
942 for (i = 0; i < pinned_pages; i++)
943 page_cache_release(user_pages[i]);
944 drm_free_large(user_pages);
950 * Writes data to the object referenced by handle.
952 * On error, the contents of the buffer that were to be modified are undefined.
955 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
956 struct drm_file *file)
958 struct drm_i915_gem_pwrite *args = data;
959 struct drm_i915_gem_object *obj;
965 if (!access_ok(VERIFY_READ,
966 (char __user *)(uintptr_t)args->data_ptr,
970 ret = fault_in_pages_readable((char __user *)(uintptr_t)args->data_ptr,
975 ret = i915_mutex_lock_interruptible(dev);
979 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
980 if (&obj->base == NULL) {
985 /* Bounds check destination. */
986 if (args->offset > obj->base.size ||
987 args->size > obj->base.size - args->offset) {
992 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
994 /* We can only do the GTT pwrite on untiled buffers, as otherwise
995 * it would end up going through the fenced access, and we'll get
996 * different detiling behavior between reading and writing.
997 * pread/pwrite currently are reading and writing from the CPU
998 * perspective, requiring manual detiling by the client.
1001 ret = i915_gem_phys_pwrite(dev, obj, args, file);
1002 else if (obj->gtt_space &&
1003 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
1004 ret = i915_gem_object_pin(obj, 0, true);
1008 ret = i915_gem_object_set_to_gtt_domain(obj, true);
1012 ret = i915_gem_object_put_fence(obj);
1016 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
1018 ret = i915_gem_gtt_pwrite_slow(dev, obj, args, file);
1021 i915_gem_object_unpin(obj);
1023 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
1028 if (!i915_gem_object_needs_bit17_swizzle(obj))
1029 ret = i915_gem_shmem_pwrite_fast(dev, obj, args, file);
1031 ret = i915_gem_shmem_pwrite_slow(dev, obj, args, file);
1035 drm_gem_object_unreference(&obj->base);
1037 mutex_unlock(&dev->struct_mutex);
1042 * Called when user space prepares to use an object with the CPU, either
1043 * through the mmap ioctl's mapping or a GTT mapping.
1046 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1047 struct drm_file *file)
1049 struct drm_i915_gem_set_domain *args = data;
1050 struct drm_i915_gem_object *obj;
1051 uint32_t read_domains = args->read_domains;
1052 uint32_t write_domain = args->write_domain;
1055 if (!(dev->driver->driver_features & DRIVER_GEM))
1058 /* Only handle setting domains to types used by the CPU. */
1059 if (write_domain & I915_GEM_GPU_DOMAINS)
1062 if (read_domains & I915_GEM_GPU_DOMAINS)
1065 /* Having something in the write domain implies it's in the read
1066 * domain, and only that read domain. Enforce that in the request.
1068 if (write_domain != 0 && read_domains != write_domain)
1071 ret = i915_mutex_lock_interruptible(dev);
1075 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1076 if (&obj->base == NULL) {
1081 if (read_domains & I915_GEM_DOMAIN_GTT) {
1082 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1084 /* Silently promote "you're not bound, there was nothing to do"
1085 * to success, since the client was just asking us to
1086 * make sure everything was done.
1091 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1094 drm_gem_object_unreference(&obj->base);
1096 mutex_unlock(&dev->struct_mutex);
1101 * Called when user space has done writes to this buffer
1104 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1105 struct drm_file *file)
1107 struct drm_i915_gem_sw_finish *args = data;
1108 struct drm_i915_gem_object *obj;
1111 if (!(dev->driver->driver_features & DRIVER_GEM))
1114 ret = i915_mutex_lock_interruptible(dev);
1118 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1119 if (&obj->base == NULL) {
1124 /* Pinned buffers may be scanout, so flush the cache */
1126 i915_gem_object_flush_cpu_write_domain(obj);
1128 drm_gem_object_unreference(&obj->base);
1130 mutex_unlock(&dev->struct_mutex);
1135 * Maps the contents of an object, returning the address it is mapped
1138 * While the mapping holds a reference on the contents of the object, it doesn't
1139 * imply a ref on the object itself.
1142 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1143 struct drm_file *file)
1145 struct drm_i915_private *dev_priv = dev->dev_private;
1146 struct drm_i915_gem_mmap *args = data;
1147 struct drm_gem_object *obj;
1150 if (!(dev->driver->driver_features & DRIVER_GEM))
1153 obj = drm_gem_object_lookup(dev, file, args->handle);
1157 if (obj->size > dev_priv->mm.gtt_mappable_end) {
1158 drm_gem_object_unreference_unlocked(obj);
1162 down_write(¤t->mm->mmap_sem);
1163 addr = do_mmap(obj->filp, 0, args->size,
1164 PROT_READ | PROT_WRITE, MAP_SHARED,
1166 up_write(¤t->mm->mmap_sem);
1167 drm_gem_object_unreference_unlocked(obj);
1168 if (IS_ERR((void *)addr))
1171 args->addr_ptr = (uint64_t) addr;
1177 * i915_gem_fault - fault a page into the GTT
1178 * vma: VMA in question
1181 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1182 * from userspace. The fault handler takes care of binding the object to
1183 * the GTT (if needed), allocating and programming a fence register (again,
1184 * only if needed based on whether the old reg is still valid or the object
1185 * is tiled) and inserting a new PTE into the faulting process.
1187 * Note that the faulting process may involve evicting existing objects
1188 * from the GTT and/or fence registers to make room. So performance may
1189 * suffer if the GTT working set is large or there are few fence registers
1192 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1194 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1195 struct drm_device *dev = obj->base.dev;
1196 drm_i915_private_t *dev_priv = dev->dev_private;
1197 pgoff_t page_offset;
1200 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1202 /* We don't use vmf->pgoff since that has the fake offset */
1203 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1206 ret = i915_mutex_lock_interruptible(dev);
1210 trace_i915_gem_object_fault(obj, page_offset, true, write);
1212 /* Now bind it into the GTT if needed */
1213 if (!obj->map_and_fenceable) {
1214 ret = i915_gem_object_unbind(obj);
1218 if (!obj->gtt_space) {
1219 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1223 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1228 if (obj->tiling_mode == I915_TILING_NONE)
1229 ret = i915_gem_object_put_fence(obj);
1231 ret = i915_gem_object_get_fence(obj, NULL);
1235 if (i915_gem_object_is_inactive(obj))
1236 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1238 obj->fault_mappable = true;
1240 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1243 /* Finally, remap it using the new GTT offset */
1244 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1246 mutex_unlock(&dev->struct_mutex);
1251 /* Give the error handler a chance to run and move the
1252 * objects off the GPU active list. Next time we service the
1253 * fault, we should be able to transition the page into the
1254 * GTT without touching the GPU (and so avoid further
1255 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1256 * with coherency, just lost writes.
1262 return VM_FAULT_NOPAGE;
1264 return VM_FAULT_OOM;
1266 return VM_FAULT_SIGBUS;
1271 * i915_gem_create_mmap_offset - create a fake mmap offset for an object
1272 * @obj: obj in question
1274 * GEM memory mapping works by handing back to userspace a fake mmap offset
1275 * it can use in a subsequent mmap(2) call. The DRM core code then looks
1276 * up the object based on the offset and sets up the various memory mapping
1279 * This routine allocates and attaches a fake offset for @obj.
1282 i915_gem_create_mmap_offset(struct drm_i915_gem_object *obj)
1284 struct drm_device *dev = obj->base.dev;
1285 struct drm_gem_mm *mm = dev->mm_private;
1286 struct drm_map_list *list;
1287 struct drm_local_map *map;
1290 /* Set the object up for mmap'ing */
1291 list = &obj->base.map_list;
1292 list->map = kzalloc(sizeof(struct drm_map_list), GFP_KERNEL);
1297 map->type = _DRM_GEM;
1298 map->size = obj->base.size;
1301 /* Get a DRM GEM mmap offset allocated... */
1302 list->file_offset_node = drm_mm_search_free(&mm->offset_manager,
1303 obj->base.size / PAGE_SIZE,
1305 if (!list->file_offset_node) {
1306 DRM_ERROR("failed to allocate offset for bo %d\n",
1312 list->file_offset_node = drm_mm_get_block(list->file_offset_node,
1313 obj->base.size / PAGE_SIZE,
1315 if (!list->file_offset_node) {
1320 list->hash.key = list->file_offset_node->start;
1321 ret = drm_ht_insert_item(&mm->offset_hash, &list->hash);
1323 DRM_ERROR("failed to add to map hash\n");
1330 drm_mm_put_block(list->file_offset_node);
1339 * i915_gem_release_mmap - remove physical page mappings
1340 * @obj: obj in question
1342 * Preserve the reservation of the mmapping with the DRM core code, but
1343 * relinquish ownership of the pages back to the system.
1345 * It is vital that we remove the page mapping if we have mapped a tiled
1346 * object through the GTT and then lose the fence register due to
1347 * resource pressure. Similarly if the object has been moved out of the
1348 * aperture, than pages mapped into userspace must be revoked. Removing the
1349 * mapping will then trigger a page fault on the next user access, allowing
1350 * fixup by i915_gem_fault().
1353 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1355 if (!obj->fault_mappable)
1358 if (obj->base.dev->dev_mapping)
1359 unmap_mapping_range(obj->base.dev->dev_mapping,
1360 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1363 obj->fault_mappable = false;
1367 i915_gem_free_mmap_offset(struct drm_i915_gem_object *obj)
1369 struct drm_device *dev = obj->base.dev;
1370 struct drm_gem_mm *mm = dev->mm_private;
1371 struct drm_map_list *list = &obj->base.map_list;
1373 drm_ht_remove_item(&mm->offset_hash, &list->hash);
1374 drm_mm_put_block(list->file_offset_node);
1380 i915_gem_get_gtt_size(struct drm_i915_gem_object *obj)
1382 struct drm_device *dev = obj->base.dev;
1385 if (INTEL_INFO(dev)->gen >= 4 ||
1386 obj->tiling_mode == I915_TILING_NONE)
1387 return obj->base.size;
1389 /* Previous chips need a power-of-two fence region when tiling */
1390 if (INTEL_INFO(dev)->gen == 3)
1395 while (size < obj->base.size)
1402 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1403 * @obj: object to check
1405 * Return the required GTT alignment for an object, taking into account
1406 * potential fence register mapping.
1409 i915_gem_get_gtt_alignment(struct drm_i915_gem_object *obj)
1411 struct drm_device *dev = obj->base.dev;
1414 * Minimum alignment is 4k (GTT page size), but might be greater
1415 * if a fence register is needed for the object.
1417 if (INTEL_INFO(dev)->gen >= 4 ||
1418 obj->tiling_mode == I915_TILING_NONE)
1422 * Previous chips need to be aligned to the size of the smallest
1423 * fence register that can contain the object.
1425 return i915_gem_get_gtt_size(obj);
1429 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1431 * @obj: object to check
1433 * Return the required GTT alignment for an object, only taking into account
1434 * unfenced tiled surface requirements.
1437 i915_gem_get_unfenced_gtt_alignment(struct drm_i915_gem_object *obj)
1439 struct drm_device *dev = obj->base.dev;
1443 * Minimum alignment is 4k (GTT page size) for sane hw.
1445 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1446 obj->tiling_mode == I915_TILING_NONE)
1450 * Older chips need unfenced tiled buffers to be aligned to the left
1451 * edge of an even tile row (where tile rows are counted as if the bo is
1452 * placed in a fenced gtt region).
1456 else if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
1461 return tile_height * obj->stride * 2;
1465 i915_gem_mmap_gtt(struct drm_file *file,
1466 struct drm_device *dev,
1470 struct drm_i915_private *dev_priv = dev->dev_private;
1471 struct drm_i915_gem_object *obj;
1474 if (!(dev->driver->driver_features & DRIVER_GEM))
1477 ret = i915_mutex_lock_interruptible(dev);
1481 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1482 if (&obj->base == NULL) {
1487 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1492 if (obj->madv != I915_MADV_WILLNEED) {
1493 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1498 if (!obj->base.map_list.map) {
1499 ret = i915_gem_create_mmap_offset(obj);
1504 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1507 drm_gem_object_unreference(&obj->base);
1509 mutex_unlock(&dev->struct_mutex);
1514 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1516 * @data: GTT mapping ioctl data
1517 * @file: GEM object info
1519 * Simply returns the fake offset to userspace so it can mmap it.
1520 * The mmap call will end up in drm_gem_mmap(), which will set things
1521 * up so we can get faults in the handler above.
1523 * The fault handler will take care of binding the object into the GTT
1524 * (since it may have been evicted to make room for something), allocating
1525 * a fence register, and mapping the appropriate aperture address into
1529 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1530 struct drm_file *file)
1532 struct drm_i915_gem_mmap_gtt *args = data;
1534 if (!(dev->driver->driver_features & DRIVER_GEM))
1537 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1542 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1546 struct address_space *mapping;
1547 struct inode *inode;
1550 /* Get the list of pages out of our struct file. They'll be pinned
1551 * at this point until we release them.
1553 page_count = obj->base.size / PAGE_SIZE;
1554 BUG_ON(obj->pages != NULL);
1555 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1556 if (obj->pages == NULL)
1559 inode = obj->base.filp->f_path.dentry->d_inode;
1560 mapping = inode->i_mapping;
1561 for (i = 0; i < page_count; i++) {
1562 page = read_cache_page_gfp(mapping, i,
1570 obj->pages[i] = page;
1573 if (obj->tiling_mode != I915_TILING_NONE)
1574 i915_gem_object_do_bit_17_swizzle(obj);
1580 page_cache_release(obj->pages[i]);
1582 drm_free_large(obj->pages);
1584 return PTR_ERR(page);
1588 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1590 int page_count = obj->base.size / PAGE_SIZE;
1593 BUG_ON(obj->madv == __I915_MADV_PURGED);
1595 if (obj->tiling_mode != I915_TILING_NONE)
1596 i915_gem_object_save_bit_17_swizzle(obj);
1598 if (obj->madv == I915_MADV_DONTNEED)
1601 for (i = 0; i < page_count; i++) {
1603 set_page_dirty(obj->pages[i]);
1605 if (obj->madv == I915_MADV_WILLNEED)
1606 mark_page_accessed(obj->pages[i]);
1608 page_cache_release(obj->pages[i]);
1612 drm_free_large(obj->pages);
1617 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1618 struct intel_ring_buffer *ring,
1621 struct drm_device *dev = obj->base.dev;
1622 struct drm_i915_private *dev_priv = dev->dev_private;
1624 BUG_ON(ring == NULL);
1627 /* Add a reference if we're newly entering the active list. */
1629 drm_gem_object_reference(&obj->base);
1633 /* Move from whatever list we were on to the tail of execution. */
1634 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1635 list_move_tail(&obj->ring_list, &ring->active_list);
1637 obj->last_rendering_seqno = seqno;
1638 if (obj->fenced_gpu_access) {
1639 struct drm_i915_fence_reg *reg;
1641 BUG_ON(obj->fence_reg == I915_FENCE_REG_NONE);
1643 obj->last_fenced_seqno = seqno;
1644 obj->last_fenced_ring = ring;
1646 reg = &dev_priv->fence_regs[obj->fence_reg];
1647 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
1652 i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1654 list_del_init(&obj->ring_list);
1655 obj->last_rendering_seqno = 0;
1659 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1661 struct drm_device *dev = obj->base.dev;
1662 drm_i915_private_t *dev_priv = dev->dev_private;
1664 BUG_ON(!obj->active);
1665 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1667 i915_gem_object_move_off_active(obj);
1671 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1673 struct drm_device *dev = obj->base.dev;
1674 struct drm_i915_private *dev_priv = dev->dev_private;
1676 if (obj->pin_count != 0)
1677 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1679 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1681 BUG_ON(!list_empty(&obj->gpu_write_list));
1682 BUG_ON(!obj->active);
1685 i915_gem_object_move_off_active(obj);
1686 obj->fenced_gpu_access = false;
1689 obj->pending_gpu_write = false;
1690 drm_gem_object_unreference(&obj->base);
1692 WARN_ON(i915_verify_lists(dev));
1695 /* Immediately discard the backing storage */
1697 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1699 struct inode *inode;
1701 /* Our goal here is to return as much of the memory as
1702 * is possible back to the system as we are called from OOM.
1703 * To do this we must instruct the shmfs to drop all of its
1704 * backing pages, *now*. Here we mirror the actions taken
1705 * when by shmem_delete_inode() to release the backing store.
1707 inode = obj->base.filp->f_path.dentry->d_inode;
1708 truncate_inode_pages(inode->i_mapping, 0);
1709 if (inode->i_op->truncate_range)
1710 inode->i_op->truncate_range(inode, 0, (loff_t)-1);
1712 obj->madv = __I915_MADV_PURGED;
1716 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1718 return obj->madv == I915_MADV_DONTNEED;
1722 i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1723 uint32_t flush_domains)
1725 struct drm_i915_gem_object *obj, *next;
1727 list_for_each_entry_safe(obj, next,
1728 &ring->gpu_write_list,
1730 if (obj->base.write_domain & flush_domains) {
1731 uint32_t old_write_domain = obj->base.write_domain;
1733 obj->base.write_domain = 0;
1734 list_del_init(&obj->gpu_write_list);
1735 i915_gem_object_move_to_active(obj, ring,
1736 i915_gem_next_request_seqno(ring));
1738 trace_i915_gem_object_change_domain(obj,
1739 obj->base.read_domains,
1746 i915_add_request(struct intel_ring_buffer *ring,
1747 struct drm_file *file,
1748 struct drm_i915_gem_request *request)
1750 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1755 BUG_ON(request == NULL);
1757 ret = ring->add_request(ring, &seqno);
1761 trace_i915_gem_request_add(ring, seqno);
1763 request->seqno = seqno;
1764 request->ring = ring;
1765 request->emitted_jiffies = jiffies;
1766 was_empty = list_empty(&ring->request_list);
1767 list_add_tail(&request->list, &ring->request_list);
1770 struct drm_i915_file_private *file_priv = file->driver_priv;
1772 spin_lock(&file_priv->mm.lock);
1773 request->file_priv = file_priv;
1774 list_add_tail(&request->client_list,
1775 &file_priv->mm.request_list);
1776 spin_unlock(&file_priv->mm.lock);
1779 ring->outstanding_lazy_request = false;
1781 if (!dev_priv->mm.suspended) {
1782 mod_timer(&dev_priv->hangcheck_timer,
1783 jiffies + msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1785 queue_delayed_work(dev_priv->wq,
1786 &dev_priv->mm.retire_work, HZ);
1792 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1794 struct drm_i915_file_private *file_priv = request->file_priv;
1799 spin_lock(&file_priv->mm.lock);
1800 if (request->file_priv) {
1801 list_del(&request->client_list);
1802 request->file_priv = NULL;
1804 spin_unlock(&file_priv->mm.lock);
1807 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1808 struct intel_ring_buffer *ring)
1810 while (!list_empty(&ring->request_list)) {
1811 struct drm_i915_gem_request *request;
1813 request = list_first_entry(&ring->request_list,
1814 struct drm_i915_gem_request,
1817 list_del(&request->list);
1818 i915_gem_request_remove_from_client(request);
1822 while (!list_empty(&ring->active_list)) {
1823 struct drm_i915_gem_object *obj;
1825 obj = list_first_entry(&ring->active_list,
1826 struct drm_i915_gem_object,
1829 obj->base.write_domain = 0;
1830 list_del_init(&obj->gpu_write_list);
1831 i915_gem_object_move_to_inactive(obj);
1835 static void i915_gem_reset_fences(struct drm_device *dev)
1837 struct drm_i915_private *dev_priv = dev->dev_private;
1840 for (i = 0; i < 16; i++) {
1841 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1842 struct drm_i915_gem_object *obj = reg->obj;
1847 if (obj->tiling_mode)
1848 i915_gem_release_mmap(obj);
1850 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1851 reg->obj->fenced_gpu_access = false;
1852 reg->obj->last_fenced_seqno = 0;
1853 reg->obj->last_fenced_ring = NULL;
1854 i915_gem_clear_fence_reg(dev, reg);
1858 void i915_gem_reset(struct drm_device *dev)
1860 struct drm_i915_private *dev_priv = dev->dev_private;
1861 struct drm_i915_gem_object *obj;
1864 for (i = 0; i < I915_NUM_RINGS; i++)
1865 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
1867 /* Remove anything from the flushing lists. The GPU cache is likely
1868 * to be lost on reset along with the data, so simply move the
1869 * lost bo to the inactive list.
1871 while (!list_empty(&dev_priv->mm.flushing_list)) {
1872 obj= list_first_entry(&dev_priv->mm.flushing_list,
1873 struct drm_i915_gem_object,
1876 obj->base.write_domain = 0;
1877 list_del_init(&obj->gpu_write_list);
1878 i915_gem_object_move_to_inactive(obj);
1881 /* Move everything out of the GPU domains to ensure we do any
1882 * necessary invalidation upon reuse.
1884 list_for_each_entry(obj,
1885 &dev_priv->mm.inactive_list,
1888 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1891 /* The fence registers are invalidated so clear them out */
1892 i915_gem_reset_fences(dev);
1896 * This function clears the request list as sequence numbers are passed.
1899 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
1904 if (list_empty(&ring->request_list))
1907 WARN_ON(i915_verify_lists(ring->dev));
1909 seqno = ring->get_seqno(ring);
1911 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
1912 if (seqno >= ring->sync_seqno[i])
1913 ring->sync_seqno[i] = 0;
1915 while (!list_empty(&ring->request_list)) {
1916 struct drm_i915_gem_request *request;
1918 request = list_first_entry(&ring->request_list,
1919 struct drm_i915_gem_request,
1922 if (!i915_seqno_passed(seqno, request->seqno))
1925 trace_i915_gem_request_retire(ring, request->seqno);
1927 list_del(&request->list);
1928 i915_gem_request_remove_from_client(request);
1932 /* Move any buffers on the active list that are no longer referenced
1933 * by the ringbuffer to the flushing/inactive lists as appropriate.
1935 while (!list_empty(&ring->active_list)) {
1936 struct drm_i915_gem_object *obj;
1938 obj= list_first_entry(&ring->active_list,
1939 struct drm_i915_gem_object,
1942 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1945 if (obj->base.write_domain != 0)
1946 i915_gem_object_move_to_flushing(obj);
1948 i915_gem_object_move_to_inactive(obj);
1951 if (unlikely(ring->trace_irq_seqno &&
1952 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
1953 ring->irq_put(ring);
1954 ring->trace_irq_seqno = 0;
1957 WARN_ON(i915_verify_lists(ring->dev));
1961 i915_gem_retire_requests(struct drm_device *dev)
1963 drm_i915_private_t *dev_priv = dev->dev_private;
1966 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1967 struct drm_i915_gem_object *obj, *next;
1969 /* We must be careful that during unbind() we do not
1970 * accidentally infinitely recurse into retire requests.
1972 * retire -> free -> unbind -> wait -> retire_ring
1974 list_for_each_entry_safe(obj, next,
1975 &dev_priv->mm.deferred_free_list,
1977 i915_gem_free_object_tail(obj);
1980 for (i = 0; i < I915_NUM_RINGS; i++)
1981 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
1985 i915_gem_retire_work_handler(struct work_struct *work)
1987 drm_i915_private_t *dev_priv;
1988 struct drm_device *dev;
1992 dev_priv = container_of(work, drm_i915_private_t,
1993 mm.retire_work.work);
1994 dev = dev_priv->dev;
1996 /* Come back later if the device is busy... */
1997 if (!mutex_trylock(&dev->struct_mutex)) {
1998 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2002 i915_gem_retire_requests(dev);
2004 /* Send a periodic flush down the ring so we don't hold onto GEM
2005 * objects indefinitely.
2008 for (i = 0; i < I915_NUM_RINGS; i++) {
2009 struct intel_ring_buffer *ring = &dev_priv->ring[i];
2011 if (!list_empty(&ring->gpu_write_list)) {
2012 struct drm_i915_gem_request *request;
2015 ret = i915_gem_flush_ring(ring,
2016 0, I915_GEM_GPU_DOMAINS);
2017 request = kzalloc(sizeof(*request), GFP_KERNEL);
2018 if (ret || request == NULL ||
2019 i915_add_request(ring, NULL, request))
2023 idle &= list_empty(&ring->request_list);
2026 if (!dev_priv->mm.suspended && !idle)
2027 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
2029 mutex_unlock(&dev->struct_mutex);
2033 * Waits for a sequence number to be signaled, and cleans up the
2034 * request and object lists appropriately for that event.
2037 i915_wait_request(struct intel_ring_buffer *ring,
2040 drm_i915_private_t *dev_priv = ring->dev->dev_private;
2046 if (atomic_read(&dev_priv->mm.wedged)) {
2047 struct completion *x = &dev_priv->error_completion;
2048 bool recovery_complete;
2049 unsigned long flags;
2051 /* Give the error handler a chance to run. */
2052 spin_lock_irqsave(&x->wait.lock, flags);
2053 recovery_complete = x->done > 0;
2054 spin_unlock_irqrestore(&x->wait.lock, flags);
2056 return recovery_complete ? -EIO : -EAGAIN;
2059 if (seqno == ring->outstanding_lazy_request) {
2060 struct drm_i915_gem_request *request;
2062 request = kzalloc(sizeof(*request), GFP_KERNEL);
2063 if (request == NULL)
2066 ret = i915_add_request(ring, NULL, request);
2072 seqno = request->seqno;
2075 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
2076 if (HAS_PCH_SPLIT(ring->dev))
2077 ier = I915_READ(DEIER) | I915_READ(GTIER);
2079 ier = I915_READ(IER);
2081 DRM_ERROR("something (likely vbetool) disabled "
2082 "interrupts, re-enabling\n");
2083 i915_driver_irq_preinstall(ring->dev);
2084 i915_driver_irq_postinstall(ring->dev);
2087 trace_i915_gem_request_wait_begin(ring, seqno);
2089 ring->waiting_seqno = seqno;
2090 if (ring->irq_get(ring)) {
2091 if (dev_priv->mm.interruptible)
2092 ret = wait_event_interruptible(ring->irq_queue,
2093 i915_seqno_passed(ring->get_seqno(ring), seqno)
2094 || atomic_read(&dev_priv->mm.wedged));
2096 wait_event(ring->irq_queue,
2097 i915_seqno_passed(ring->get_seqno(ring), seqno)
2098 || atomic_read(&dev_priv->mm.wedged));
2100 ring->irq_put(ring);
2101 } else if (wait_for(i915_seqno_passed(ring->get_seqno(ring),
2103 atomic_read(&dev_priv->mm.wedged), 3000))
2105 ring->waiting_seqno = 0;
2107 trace_i915_gem_request_wait_end(ring, seqno);
2109 if (atomic_read(&dev_priv->mm.wedged))
2112 if (ret && ret != -ERESTARTSYS)
2113 DRM_ERROR("%s returns %d (awaiting %d at %d, next %d)\n",
2114 __func__, ret, seqno, ring->get_seqno(ring),
2115 dev_priv->next_seqno);
2117 /* Directly dispatch request retiring. While we have the work queue
2118 * to handle this, the waiter on a request often wants an associated
2119 * buffer to have made it to the inactive list, and we would need
2120 * a separate wait queue to handle that.
2123 i915_gem_retire_requests_ring(ring);
2129 * Ensures that all rendering to the object has completed and the object is
2130 * safe to unbind from the GTT or access from the CPU.
2133 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
2137 /* This function only exists to support waiting for existing rendering,
2138 * not for emitting required flushes.
2140 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
2142 /* If there is rendering queued on the buffer being evicted, wait for
2146 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno);
2154 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2156 u32 old_write_domain, old_read_domains;
2158 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2161 /* Act a barrier for all accesses through the GTT */
2164 /* Force a pagefault for domain tracking on next user access */
2165 i915_gem_release_mmap(obj);
2167 old_read_domains = obj->base.read_domains;
2168 old_write_domain = obj->base.write_domain;
2170 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2171 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2173 trace_i915_gem_object_change_domain(obj,
2179 * Unbinds an object from the GTT aperture.
2182 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2186 if (obj->gtt_space == NULL)
2189 if (obj->pin_count != 0) {
2190 DRM_ERROR("Attempting to unbind pinned buffer\n");
2194 ret = i915_gem_object_finish_gpu(obj);
2195 if (ret == -ERESTARTSYS)
2197 /* Continue on if we fail due to EIO, the GPU is hung so we
2198 * should be safe and we need to cleanup or else we might
2199 * cause memory corruption through use-after-free.
2202 i915_gem_object_finish_gtt(obj);
2204 /* Move the object to the CPU domain to ensure that
2205 * any possible CPU writes while it's not in the GTT
2206 * are flushed when we go to remap it.
2209 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2210 if (ret == -ERESTARTSYS)
2213 /* In the event of a disaster, abandon all caches and
2214 * hope for the best.
2216 i915_gem_clflush_object(obj);
2217 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2220 /* release the fence reg _after_ flushing */
2221 ret = i915_gem_object_put_fence(obj);
2222 if (ret == -ERESTARTSYS)
2225 trace_i915_gem_object_unbind(obj);
2227 i915_gem_gtt_unbind_object(obj);
2228 i915_gem_object_put_pages_gtt(obj);
2230 list_del_init(&obj->gtt_list);
2231 list_del_init(&obj->mm_list);
2232 /* Avoid an unnecessary call to unbind on rebind. */
2233 obj->map_and_fenceable = true;
2235 drm_mm_put_block(obj->gtt_space);
2236 obj->gtt_space = NULL;
2237 obj->gtt_offset = 0;
2239 if (i915_gem_object_is_purgeable(obj))
2240 i915_gem_object_truncate(obj);
2246 i915_gem_flush_ring(struct intel_ring_buffer *ring,
2247 uint32_t invalidate_domains,
2248 uint32_t flush_domains)
2252 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2255 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2257 ret = ring->flush(ring, invalidate_domains, flush_domains);
2261 if (flush_domains & I915_GEM_GPU_DOMAINS)
2262 i915_gem_process_flushing_list(ring, flush_domains);
2267 static int i915_ring_idle(struct intel_ring_buffer *ring)
2271 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2274 if (!list_empty(&ring->gpu_write_list)) {
2275 ret = i915_gem_flush_ring(ring,
2276 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2281 return i915_wait_request(ring, i915_gem_next_request_seqno(ring));
2285 i915_gpu_idle(struct drm_device *dev)
2287 drm_i915_private_t *dev_priv = dev->dev_private;
2291 lists_empty = (list_empty(&dev_priv->mm.flushing_list) &&
2292 list_empty(&dev_priv->mm.active_list));
2296 /* Flush everything onto the inactive list. */
2297 for (i = 0; i < I915_NUM_RINGS; i++) {
2298 ret = i915_ring_idle(&dev_priv->ring[i]);
2306 static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2307 struct intel_ring_buffer *pipelined)
2309 struct drm_device *dev = obj->base.dev;
2310 drm_i915_private_t *dev_priv = dev->dev_private;
2311 u32 size = obj->gtt_space->size;
2312 int regnum = obj->fence_reg;
2315 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2317 val |= obj->gtt_offset & 0xfffff000;
2318 val |= (uint64_t)((obj->stride / 128) - 1) <<
2319 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2321 if (obj->tiling_mode == I915_TILING_Y)
2322 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2323 val |= I965_FENCE_REG_VALID;
2326 int ret = intel_ring_begin(pipelined, 6);
2330 intel_ring_emit(pipelined, MI_NOOP);
2331 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2332 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2333 intel_ring_emit(pipelined, (u32)val);
2334 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2335 intel_ring_emit(pipelined, (u32)(val >> 32));
2336 intel_ring_advance(pipelined);
2338 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2343 static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2344 struct intel_ring_buffer *pipelined)
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;
2361 int ret = intel_ring_begin(pipelined, 6);
2365 intel_ring_emit(pipelined, MI_NOOP);
2366 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2367 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2368 intel_ring_emit(pipelined, (u32)val);
2369 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2370 intel_ring_emit(pipelined, (u32)(val >> 32));
2371 intel_ring_advance(pipelined);
2373 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2378 static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2379 struct intel_ring_buffer *pipelined)
2381 struct drm_device *dev = obj->base.dev;
2382 drm_i915_private_t *dev_priv = dev->dev_private;
2383 u32 size = obj->gtt_space->size;
2384 u32 fence_reg, val, pitch_val;
2387 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2388 (size & -size) != size ||
2389 (obj->gtt_offset & (size - 1)),
2390 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2391 obj->gtt_offset, obj->map_and_fenceable, size))
2394 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2399 /* Note: pitch better be a power of two tile widths */
2400 pitch_val = obj->stride / tile_width;
2401 pitch_val = ffs(pitch_val) - 1;
2403 val = obj->gtt_offset;
2404 if (obj->tiling_mode == I915_TILING_Y)
2405 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2406 val |= I915_FENCE_SIZE_BITS(size);
2407 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2408 val |= I830_FENCE_REG_VALID;
2410 fence_reg = obj->fence_reg;
2412 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2414 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2417 int ret = intel_ring_begin(pipelined, 4);
2421 intel_ring_emit(pipelined, MI_NOOP);
2422 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2423 intel_ring_emit(pipelined, fence_reg);
2424 intel_ring_emit(pipelined, val);
2425 intel_ring_advance(pipelined);
2427 I915_WRITE(fence_reg, val);
2432 static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2433 struct intel_ring_buffer *pipelined)
2435 struct drm_device *dev = obj->base.dev;
2436 drm_i915_private_t *dev_priv = dev->dev_private;
2437 u32 size = obj->gtt_space->size;
2438 int regnum = obj->fence_reg;
2442 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2443 (size & -size) != size ||
2444 (obj->gtt_offset & (size - 1)),
2445 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2446 obj->gtt_offset, size))
2449 pitch_val = obj->stride / 128;
2450 pitch_val = ffs(pitch_val) - 1;
2452 val = obj->gtt_offset;
2453 if (obj->tiling_mode == I915_TILING_Y)
2454 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2455 val |= I830_FENCE_SIZE_BITS(size);
2456 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2457 val |= I830_FENCE_REG_VALID;
2460 int ret = intel_ring_begin(pipelined, 4);
2464 intel_ring_emit(pipelined, MI_NOOP);
2465 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2466 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2467 intel_ring_emit(pipelined, val);
2468 intel_ring_advance(pipelined);
2470 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2475 static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2477 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2481 i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
2482 struct intel_ring_buffer *pipelined)
2486 if (obj->fenced_gpu_access) {
2487 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
2488 ret = i915_gem_flush_ring(obj->last_fenced_ring,
2489 0, obj->base.write_domain);
2494 obj->fenced_gpu_access = false;
2497 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2498 if (!ring_passed_seqno(obj->last_fenced_ring,
2499 obj->last_fenced_seqno)) {
2500 ret = i915_wait_request(obj->last_fenced_ring,
2501 obj->last_fenced_seqno);
2506 obj->last_fenced_seqno = 0;
2507 obj->last_fenced_ring = NULL;
2510 /* Ensure that all CPU reads are completed before installing a fence
2511 * and all writes before removing the fence.
2513 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2520 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2524 if (obj->tiling_mode)
2525 i915_gem_release_mmap(obj);
2527 ret = i915_gem_object_flush_fence(obj, NULL);
2531 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2532 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2533 i915_gem_clear_fence_reg(obj->base.dev,
2534 &dev_priv->fence_regs[obj->fence_reg]);
2536 obj->fence_reg = I915_FENCE_REG_NONE;
2542 static struct drm_i915_fence_reg *
2543 i915_find_fence_reg(struct drm_device *dev,
2544 struct intel_ring_buffer *pipelined)
2546 struct drm_i915_private *dev_priv = dev->dev_private;
2547 struct drm_i915_fence_reg *reg, *first, *avail;
2550 /* First try to find a free reg */
2552 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2553 reg = &dev_priv->fence_regs[i];
2557 if (!reg->obj->pin_count)
2564 /* None available, try to steal one or wait for a user to finish */
2565 avail = first = NULL;
2566 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2567 if (reg->obj->pin_count)
2574 !reg->obj->last_fenced_ring ||
2575 reg->obj->last_fenced_ring == pipelined) {
2588 * i915_gem_object_get_fence - set up a fence reg for an object
2589 * @obj: object to map through a fence reg
2590 * @pipelined: ring on which to queue the change, or NULL for CPU access
2591 * @interruptible: must we wait uninterruptibly for the register to retire?
2593 * When mapping objects through the GTT, userspace wants to be able to write
2594 * to them without having to worry about swizzling if the object is tiled.
2596 * This function walks the fence regs looking for a free one for @obj,
2597 * stealing one if it can't find any.
2599 * It then sets up the reg based on the object's properties: address, pitch
2600 * and tiling format.
2603 i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2604 struct intel_ring_buffer *pipelined)
2606 struct drm_device *dev = obj->base.dev;
2607 struct drm_i915_private *dev_priv = dev->dev_private;
2608 struct drm_i915_fence_reg *reg;
2611 /* XXX disable pipelining. There are bugs. Shocking. */
2614 /* Just update our place in the LRU if our fence is getting reused. */
2615 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2616 reg = &dev_priv->fence_regs[obj->fence_reg];
2617 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2619 if (obj->tiling_changed) {
2620 ret = i915_gem_object_flush_fence(obj, pipelined);
2624 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2629 i915_gem_next_request_seqno(pipelined);
2630 obj->last_fenced_seqno = reg->setup_seqno;
2631 obj->last_fenced_ring = pipelined;
2638 if (reg->setup_seqno) {
2639 if (!ring_passed_seqno(obj->last_fenced_ring,
2640 reg->setup_seqno)) {
2641 ret = i915_wait_request(obj->last_fenced_ring,
2647 reg->setup_seqno = 0;
2649 } else if (obj->last_fenced_ring &&
2650 obj->last_fenced_ring != pipelined) {
2651 ret = i915_gem_object_flush_fence(obj, pipelined);
2659 reg = i915_find_fence_reg(dev, pipelined);
2663 ret = i915_gem_object_flush_fence(obj, pipelined);
2668 struct drm_i915_gem_object *old = reg->obj;
2670 drm_gem_object_reference(&old->base);
2672 if (old->tiling_mode)
2673 i915_gem_release_mmap(old);
2675 ret = i915_gem_object_flush_fence(old, pipelined);
2677 drm_gem_object_unreference(&old->base);
2681 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2684 old->fence_reg = I915_FENCE_REG_NONE;
2685 old->last_fenced_ring = pipelined;
2686 old->last_fenced_seqno =
2687 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2689 drm_gem_object_unreference(&old->base);
2690 } else if (obj->last_fenced_seqno == 0)
2694 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2695 obj->fence_reg = reg - dev_priv->fence_regs;
2696 obj->last_fenced_ring = pipelined;
2699 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2700 obj->last_fenced_seqno = reg->setup_seqno;
2703 obj->tiling_changed = false;
2704 switch (INTEL_INFO(dev)->gen) {
2707 ret = sandybridge_write_fence_reg(obj, pipelined);
2711 ret = i965_write_fence_reg(obj, pipelined);
2714 ret = i915_write_fence_reg(obj, pipelined);
2717 ret = i830_write_fence_reg(obj, pipelined);
2725 * i915_gem_clear_fence_reg - clear out fence register info
2726 * @obj: object to clear
2728 * Zeroes out the fence register itself and clears out the associated
2729 * data structures in dev_priv and obj.
2732 i915_gem_clear_fence_reg(struct drm_device *dev,
2733 struct drm_i915_fence_reg *reg)
2735 drm_i915_private_t *dev_priv = dev->dev_private;
2736 uint32_t fence_reg = reg - dev_priv->fence_regs;
2738 switch (INTEL_INFO(dev)->gen) {
2741 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
2745 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
2749 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2752 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2754 I915_WRITE(fence_reg, 0);
2758 list_del_init(®->lru_list);
2760 reg->setup_seqno = 0;
2764 * Finds free space in the GTT aperture and binds the object there.
2767 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2769 bool map_and_fenceable)
2771 struct drm_device *dev = obj->base.dev;
2772 drm_i915_private_t *dev_priv = dev->dev_private;
2773 struct drm_mm_node *free_space;
2774 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2775 u32 size, fence_size, fence_alignment, unfenced_alignment;
2776 bool mappable, fenceable;
2779 if (obj->madv != I915_MADV_WILLNEED) {
2780 DRM_ERROR("Attempting to bind a purgeable object\n");
2784 fence_size = i915_gem_get_gtt_size(obj);
2785 fence_alignment = i915_gem_get_gtt_alignment(obj);
2786 unfenced_alignment = i915_gem_get_unfenced_gtt_alignment(obj);
2789 alignment = map_and_fenceable ? fence_alignment :
2791 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2792 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2796 size = map_and_fenceable ? fence_size : obj->base.size;
2798 /* If the object is bigger than the entire aperture, reject it early
2799 * before evicting everything in a vain attempt to find space.
2801 if (obj->base.size >
2802 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2803 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2808 if (map_and_fenceable)
2810 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2812 dev_priv->mm.gtt_mappable_end,
2815 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2816 size, alignment, 0);
2818 if (free_space != NULL) {
2819 if (map_and_fenceable)
2821 drm_mm_get_block_range_generic(free_space,
2823 dev_priv->mm.gtt_mappable_end,
2827 drm_mm_get_block(free_space, size, alignment);
2829 if (obj->gtt_space == NULL) {
2830 /* If the gtt is empty and we're still having trouble
2831 * fitting our object in, we're out of memory.
2833 ret = i915_gem_evict_something(dev, size, alignment,
2841 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2843 drm_mm_put_block(obj->gtt_space);
2844 obj->gtt_space = NULL;
2846 if (ret == -ENOMEM) {
2847 /* first try to reclaim some memory by clearing the GTT */
2848 ret = i915_gem_evict_everything(dev, false);
2850 /* now try to shrink everyone else */
2865 ret = i915_gem_gtt_bind_object(obj);
2867 i915_gem_object_put_pages_gtt(obj);
2868 drm_mm_put_block(obj->gtt_space);
2869 obj->gtt_space = NULL;
2871 if (i915_gem_evict_everything(dev, false))
2877 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2878 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2880 /* Assert that the object is not currently in any GPU domain. As it
2881 * wasn't in the GTT, there shouldn't be any way it could have been in
2884 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2885 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2887 obj->gtt_offset = obj->gtt_space->start;
2890 obj->gtt_space->size == fence_size &&
2891 (obj->gtt_space->start & (fence_alignment -1)) == 0;
2894 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2896 obj->map_and_fenceable = mappable && fenceable;
2898 trace_i915_gem_object_bind(obj, map_and_fenceable);
2903 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2905 /* If we don't have a page list set up, then we're not pinned
2906 * to GPU, and we can ignore the cache flush because it'll happen
2907 * again at bind time.
2909 if (obj->pages == NULL)
2912 /* If the GPU is snooping the contents of the CPU cache,
2913 * we do not need to manually clear the CPU cache lines. However,
2914 * the caches are only snooped when the render cache is
2915 * flushed/invalidated. As we always have to emit invalidations
2916 * and flushes when moving into and out of the RENDER domain, correct
2917 * snooping behaviour occurs naturally as the result of our domain
2920 if (obj->cache_level != I915_CACHE_NONE)
2923 trace_i915_gem_object_clflush(obj);
2925 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2928 /** Flushes any GPU write domain for the object if it's dirty. */
2930 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2932 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2935 /* Queue the GPU write cache flushing we need. */
2936 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
2939 /** Flushes the GTT write domain for the object if it's dirty. */
2941 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2943 uint32_t old_write_domain;
2945 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2948 /* No actual flushing is required for the GTT write domain. Writes
2949 * to it immediately go to main memory as far as we know, so there's
2950 * no chipset flush. It also doesn't land in render cache.
2952 * However, we do have to enforce the order so that all writes through
2953 * the GTT land before any writes to the device, such as updates to
2958 old_write_domain = obj->base.write_domain;
2959 obj->base.write_domain = 0;
2961 trace_i915_gem_object_change_domain(obj,
2962 obj->base.read_domains,
2966 /** Flushes the CPU write domain for the object if it's dirty. */
2968 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2970 uint32_t old_write_domain;
2972 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2975 i915_gem_clflush_object(obj);
2976 intel_gtt_chipset_flush();
2977 old_write_domain = obj->base.write_domain;
2978 obj->base.write_domain = 0;
2980 trace_i915_gem_object_change_domain(obj,
2981 obj->base.read_domains,
2986 * Moves a single object to the GTT read, and possibly write domain.
2988 * This function returns when the move is complete, including waiting on
2992 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2994 uint32_t old_write_domain, old_read_domains;
2997 /* Not valid to be called on unbound objects. */
2998 if (obj->gtt_space == NULL)
3001 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3004 ret = i915_gem_object_flush_gpu_write_domain(obj);
3008 if (obj->pending_gpu_write || write) {
3009 ret = i915_gem_object_wait_rendering(obj);
3014 i915_gem_object_flush_cpu_write_domain(obj);
3016 old_write_domain = obj->base.write_domain;
3017 old_read_domains = obj->base.read_domains;
3019 /* It should now be out of any other write domains, and we can update
3020 * the domain values for our changes.
3022 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3023 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3025 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3026 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3030 trace_i915_gem_object_change_domain(obj,
3037 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3038 enum i915_cache_level cache_level)
3042 if (obj->cache_level == cache_level)
3045 if (obj->pin_count) {
3046 DRM_DEBUG("can not change the cache level of pinned objects\n");
3050 if (obj->gtt_space) {
3051 ret = i915_gem_object_finish_gpu(obj);
3055 i915_gem_object_finish_gtt(obj);
3057 /* Before SandyBridge, you could not use tiling or fence
3058 * registers with snooped memory, so relinquish any fences
3059 * currently pointing to our region in the aperture.
3061 if (INTEL_INFO(obj->base.dev)->gen < 6) {
3062 ret = i915_gem_object_put_fence(obj);
3067 i915_gem_gtt_rebind_object(obj, cache_level);
3070 if (cache_level == I915_CACHE_NONE) {
3071 u32 old_read_domains, old_write_domain;
3073 /* If we're coming from LLC cached, then we haven't
3074 * actually been tracking whether the data is in the
3075 * CPU cache or not, since we only allow one bit set
3076 * in obj->write_domain and have been skipping the clflushes.
3077 * Just set it to the CPU cache for now.
3079 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3080 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3082 old_read_domains = obj->base.read_domains;
3083 old_write_domain = obj->base.write_domain;
3085 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3086 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3088 trace_i915_gem_object_change_domain(obj,
3093 obj->cache_level = cache_level;
3098 * Prepare buffer for display plane (scanout, cursors, etc).
3099 * Can be called from an uninterruptible phase (modesetting) and allows
3100 * any flushes to be pipelined (for pageflips).
3102 * For the display plane, we want to be in the GTT but out of any write
3103 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
3104 * ability to pipeline the waits, pinning and any additional subtleties
3105 * that may differentiate the display plane from ordinary buffers.
3108 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3110 struct intel_ring_buffer *pipelined)
3112 u32 old_read_domains, old_write_domain;
3115 ret = i915_gem_object_flush_gpu_write_domain(obj);
3119 if (pipelined != obj->ring) {
3120 ret = i915_gem_object_wait_rendering(obj);
3125 /* The display engine is not coherent with the LLC cache on gen6. As
3126 * a result, we make sure that the pinning that is about to occur is
3127 * done with uncached PTEs. This is lowest common denominator for all
3130 * However for gen6+, we could do better by using the GFDT bit instead
3131 * of uncaching, which would allow us to flush all the LLC-cached data
3132 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3134 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3138 /* As the user may map the buffer once pinned in the display plane
3139 * (e.g. libkms for the bootup splash), we have to ensure that we
3140 * always use map_and_fenceable for all scanout buffers.
3142 ret = i915_gem_object_pin(obj, alignment, true);
3146 i915_gem_object_flush_cpu_write_domain(obj);
3148 old_write_domain = obj->base.write_domain;
3149 old_read_domains = obj->base.read_domains;
3151 /* It should now be out of any other write domains, and we can update
3152 * the domain values for our changes.
3154 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3155 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3157 trace_i915_gem_object_change_domain(obj,
3165 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3169 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3172 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3173 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
3178 /* Ensure that we invalidate the GPU's caches and TLBs. */
3179 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3181 return i915_gem_object_wait_rendering(obj);
3185 * Moves a single object to the CPU read, and possibly write domain.
3187 * This function returns when the move is complete, including waiting on
3191 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3193 uint32_t old_write_domain, old_read_domains;
3196 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3199 ret = i915_gem_object_flush_gpu_write_domain(obj);
3203 ret = i915_gem_object_wait_rendering(obj);
3207 i915_gem_object_flush_gtt_write_domain(obj);
3209 /* If we have a partially-valid cache of the object in the CPU,
3210 * finish invalidating it and free the per-page flags.
3212 i915_gem_object_set_to_full_cpu_read_domain(obj);
3214 old_write_domain = obj->base.write_domain;
3215 old_read_domains = obj->base.read_domains;
3217 /* Flush the CPU cache if it's still invalid. */
3218 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3219 i915_gem_clflush_object(obj);
3221 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3224 /* It should now be out of any other write domains, and we can update
3225 * the domain values for our changes.
3227 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3229 /* If we're writing through the CPU, then the GPU read domains will
3230 * need to be invalidated at next use.
3233 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3234 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3237 trace_i915_gem_object_change_domain(obj,
3245 * Moves the object from a partially CPU read to a full one.
3247 * Note that this only resolves i915_gem_object_set_cpu_read_domain_range(),
3248 * and doesn't handle transitioning from !(read_domains & I915_GEM_DOMAIN_CPU).
3251 i915_gem_object_set_to_full_cpu_read_domain(struct drm_i915_gem_object *obj)
3253 if (!obj->page_cpu_valid)
3256 /* If we're partially in the CPU read domain, finish moving it in.
3258 if (obj->base.read_domains & I915_GEM_DOMAIN_CPU) {
3261 for (i = 0; i <= (obj->base.size - 1) / PAGE_SIZE; i++) {
3262 if (obj->page_cpu_valid[i])
3264 drm_clflush_pages(obj->pages + i, 1);
3268 /* Free the page_cpu_valid mappings which are now stale, whether
3269 * or not we've got I915_GEM_DOMAIN_CPU.
3271 kfree(obj->page_cpu_valid);
3272 obj->page_cpu_valid = NULL;
3276 * Set the CPU read domain on a range of the object.
3278 * The object ends up with I915_GEM_DOMAIN_CPU in its read flags although it's
3279 * not entirely valid. The page_cpu_valid member of the object flags which
3280 * pages have been flushed, and will be respected by
3281 * i915_gem_object_set_to_cpu_domain() if it's called on to get a valid mapping
3282 * of the whole object.
3284 * This function returns when the move is complete, including waiting on
3288 i915_gem_object_set_cpu_read_domain_range(struct drm_i915_gem_object *obj,
3289 uint64_t offset, uint64_t size)
3291 uint32_t old_read_domains;
3294 if (offset == 0 && size == obj->base.size)
3295 return i915_gem_object_set_to_cpu_domain(obj, 0);
3297 ret = i915_gem_object_flush_gpu_write_domain(obj);
3301 ret = i915_gem_object_wait_rendering(obj);
3305 i915_gem_object_flush_gtt_write_domain(obj);
3307 /* If we're already fully in the CPU read domain, we're done. */
3308 if (obj->page_cpu_valid == NULL &&
3309 (obj->base.read_domains & I915_GEM_DOMAIN_CPU) != 0)
3312 /* Otherwise, create/clear the per-page CPU read domain flag if we're
3313 * newly adding I915_GEM_DOMAIN_CPU
3315 if (obj->page_cpu_valid == NULL) {
3316 obj->page_cpu_valid = kzalloc(obj->base.size / PAGE_SIZE,
3318 if (obj->page_cpu_valid == NULL)
3320 } else if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0)
3321 memset(obj->page_cpu_valid, 0, obj->base.size / PAGE_SIZE);
3323 /* Flush the cache on any pages that are still invalid from the CPU's
3326 for (i = offset / PAGE_SIZE; i <= (offset + size - 1) / PAGE_SIZE;
3328 if (obj->page_cpu_valid[i])
3331 drm_clflush_pages(obj->pages + i, 1);
3333 obj->page_cpu_valid[i] = 1;
3336 /* It should now be out of any other write domains, and we can update
3337 * the domain values for our changes.
3339 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3341 old_read_domains = obj->base.read_domains;
3342 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3344 trace_i915_gem_object_change_domain(obj,
3346 obj->base.write_domain);
3351 /* Throttle our rendering by waiting until the ring has completed our requests
3352 * emitted over 20 msec ago.
3354 * Note that if we were to use the current jiffies each time around the loop,
3355 * we wouldn't escape the function with any frames outstanding if the time to
3356 * render a frame was over 20ms.
3358 * This should get us reasonable parallelism between CPU and GPU but also
3359 * relatively low latency when blocking on a particular request to finish.
3362 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3364 struct drm_i915_private *dev_priv = dev->dev_private;
3365 struct drm_i915_file_private *file_priv = file->driver_priv;
3366 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3367 struct drm_i915_gem_request *request;
3368 struct intel_ring_buffer *ring = NULL;
3372 if (atomic_read(&dev_priv->mm.wedged))
3375 spin_lock(&file_priv->mm.lock);
3376 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3377 if (time_after_eq(request->emitted_jiffies, recent_enough))
3380 ring = request->ring;
3381 seqno = request->seqno;
3383 spin_unlock(&file_priv->mm.lock);
3389 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3390 /* And wait for the seqno passing without holding any locks and
3391 * causing extra latency for others. This is safe as the irq
3392 * generation is designed to be run atomically and so is
3395 if (ring->irq_get(ring)) {
3396 ret = wait_event_interruptible(ring->irq_queue,
3397 i915_seqno_passed(ring->get_seqno(ring), seqno)
3398 || atomic_read(&dev_priv->mm.wedged));
3399 ring->irq_put(ring);
3401 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3407 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3413 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3415 bool map_and_fenceable)
3417 struct drm_device *dev = obj->base.dev;
3418 struct drm_i915_private *dev_priv = dev->dev_private;
3421 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
3422 WARN_ON(i915_verify_lists(dev));
3424 if (obj->gtt_space != NULL) {
3425 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3426 (map_and_fenceable && !obj->map_and_fenceable)) {
3427 WARN(obj->pin_count,
3428 "bo is already pinned with incorrect alignment:"
3429 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3430 " obj->map_and_fenceable=%d\n",
3431 obj->gtt_offset, alignment,
3433 obj->map_and_fenceable);
3434 ret = i915_gem_object_unbind(obj);
3440 if (obj->gtt_space == NULL) {
3441 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3447 if (obj->pin_count++ == 0) {
3449 list_move_tail(&obj->mm_list,
3450 &dev_priv->mm.pinned_list);
3452 obj->pin_mappable |= map_and_fenceable;
3454 WARN_ON(i915_verify_lists(dev));
3459 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3461 struct drm_device *dev = obj->base.dev;
3462 drm_i915_private_t *dev_priv = dev->dev_private;
3464 WARN_ON(i915_verify_lists(dev));
3465 BUG_ON(obj->pin_count == 0);
3466 BUG_ON(obj->gtt_space == NULL);
3468 if (--obj->pin_count == 0) {
3470 list_move_tail(&obj->mm_list,
3471 &dev_priv->mm.inactive_list);
3472 obj->pin_mappable = false;
3474 WARN_ON(i915_verify_lists(dev));
3478 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3479 struct drm_file *file)
3481 struct drm_i915_gem_pin *args = data;
3482 struct drm_i915_gem_object *obj;
3485 ret = i915_mutex_lock_interruptible(dev);
3489 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3490 if (&obj->base == NULL) {
3495 if (obj->madv != I915_MADV_WILLNEED) {
3496 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3501 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3502 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3508 obj->user_pin_count++;
3509 obj->pin_filp = file;
3510 if (obj->user_pin_count == 1) {
3511 ret = i915_gem_object_pin(obj, args->alignment, true);
3516 /* XXX - flush the CPU caches for pinned objects
3517 * as the X server doesn't manage domains yet
3519 i915_gem_object_flush_cpu_write_domain(obj);
3520 args->offset = obj->gtt_offset;
3522 drm_gem_object_unreference(&obj->base);
3524 mutex_unlock(&dev->struct_mutex);
3529 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3530 struct drm_file *file)
3532 struct drm_i915_gem_pin *args = data;
3533 struct drm_i915_gem_object *obj;
3536 ret = i915_mutex_lock_interruptible(dev);
3540 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3541 if (&obj->base == NULL) {
3546 if (obj->pin_filp != file) {
3547 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3552 obj->user_pin_count--;
3553 if (obj->user_pin_count == 0) {
3554 obj->pin_filp = NULL;
3555 i915_gem_object_unpin(obj);
3559 drm_gem_object_unreference(&obj->base);
3561 mutex_unlock(&dev->struct_mutex);
3566 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3567 struct drm_file *file)
3569 struct drm_i915_gem_busy *args = data;
3570 struct drm_i915_gem_object *obj;
3573 ret = i915_mutex_lock_interruptible(dev);
3577 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3578 if (&obj->base == NULL) {
3583 /* Count all active objects as busy, even if they are currently not used
3584 * by the gpu. Users of this interface expect objects to eventually
3585 * become non-busy without any further actions, therefore emit any
3586 * necessary flushes here.
3588 args->busy = obj->active;
3590 /* Unconditionally flush objects, even when the gpu still uses this
3591 * object. Userspace calling this function indicates that it wants to
3592 * use this buffer rather sooner than later, so issuing the required
3593 * flush earlier is beneficial.
3595 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3596 ret = i915_gem_flush_ring(obj->ring,
3597 0, obj->base.write_domain);
3598 } else if (obj->ring->outstanding_lazy_request ==
3599 obj->last_rendering_seqno) {
3600 struct drm_i915_gem_request *request;
3602 /* This ring is not being cleared by active usage,
3603 * so emit a request to do so.
3605 request = kzalloc(sizeof(*request), GFP_KERNEL);
3607 ret = i915_add_request(obj->ring, NULL,request);
3612 /* Update the active list for the hardware's current position.
3613 * Otherwise this only updates on a delayed timer or when irqs
3614 * are actually unmasked, and our working set ends up being
3615 * larger than required.
3617 i915_gem_retire_requests_ring(obj->ring);
3619 args->busy = obj->active;
3622 drm_gem_object_unreference(&obj->base);
3624 mutex_unlock(&dev->struct_mutex);
3629 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3630 struct drm_file *file_priv)
3632 return i915_gem_ring_throttle(dev, file_priv);
3636 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3637 struct drm_file *file_priv)
3639 struct drm_i915_gem_madvise *args = data;
3640 struct drm_i915_gem_object *obj;
3643 switch (args->madv) {
3644 case I915_MADV_DONTNEED:
3645 case I915_MADV_WILLNEED:
3651 ret = i915_mutex_lock_interruptible(dev);
3655 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3656 if (&obj->base == NULL) {
3661 if (obj->pin_count) {
3666 if (obj->madv != __I915_MADV_PURGED)
3667 obj->madv = args->madv;
3669 /* if the object is no longer bound, discard its backing storage */
3670 if (i915_gem_object_is_purgeable(obj) &&
3671 obj->gtt_space == NULL)
3672 i915_gem_object_truncate(obj);
3674 args->retained = obj->madv != __I915_MADV_PURGED;
3677 drm_gem_object_unreference(&obj->base);
3679 mutex_unlock(&dev->struct_mutex);
3683 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3686 struct drm_i915_private *dev_priv = dev->dev_private;
3687 struct drm_i915_gem_object *obj;
3689 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3693 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3698 i915_gem_info_add_obj(dev_priv, size);
3700 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3701 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3704 /* On Gen6, we can have the GPU use the LLC (the CPU
3705 * cache) for about a 10% performance improvement
3706 * compared to uncached. Graphics requests other than
3707 * display scanout are coherent with the CPU in
3708 * accessing this cache. This means in this mode we
3709 * don't need to clflush on the CPU side, and on the
3710 * GPU side we only need to flush internal caches to
3711 * get data visible to the CPU.
3713 * However, we maintain the display planes as UC, and so
3714 * need to rebind when first used as such.
3716 obj->cache_level = I915_CACHE_LLC;
3718 obj->cache_level = I915_CACHE_NONE;
3720 obj->base.driver_private = NULL;
3721 obj->fence_reg = I915_FENCE_REG_NONE;
3722 INIT_LIST_HEAD(&obj->mm_list);
3723 INIT_LIST_HEAD(&obj->gtt_list);
3724 INIT_LIST_HEAD(&obj->ring_list);
3725 INIT_LIST_HEAD(&obj->exec_list);
3726 INIT_LIST_HEAD(&obj->gpu_write_list);
3727 obj->madv = I915_MADV_WILLNEED;
3728 /* Avoid an unnecessary call to unbind on the first bind. */
3729 obj->map_and_fenceable = true;
3734 int i915_gem_init_object(struct drm_gem_object *obj)
3741 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3743 struct drm_device *dev = obj->base.dev;
3744 drm_i915_private_t *dev_priv = dev->dev_private;
3747 ret = i915_gem_object_unbind(obj);
3748 if (ret == -ERESTARTSYS) {
3749 list_move(&obj->mm_list,
3750 &dev_priv->mm.deferred_free_list);
3754 trace_i915_gem_object_destroy(obj);
3756 if (obj->base.map_list.map)
3757 i915_gem_free_mmap_offset(obj);
3759 drm_gem_object_release(&obj->base);
3760 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3762 kfree(obj->page_cpu_valid);
3767 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3769 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3770 struct drm_device *dev = obj->base.dev;
3772 while (obj->pin_count > 0)
3773 i915_gem_object_unpin(obj);
3776 i915_gem_detach_phys_object(dev, obj);
3778 i915_gem_free_object_tail(obj);
3782 i915_gem_idle(struct drm_device *dev)
3784 drm_i915_private_t *dev_priv = dev->dev_private;
3787 mutex_lock(&dev->struct_mutex);
3789 if (dev_priv->mm.suspended) {
3790 mutex_unlock(&dev->struct_mutex);
3794 ret = i915_gpu_idle(dev);
3796 mutex_unlock(&dev->struct_mutex);
3800 /* Under UMS, be paranoid and evict. */
3801 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3802 ret = i915_gem_evict_inactive(dev, false);
3804 mutex_unlock(&dev->struct_mutex);
3809 i915_gem_reset_fences(dev);
3811 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3812 * We need to replace this with a semaphore, or something.
3813 * And not confound mm.suspended!
3815 dev_priv->mm.suspended = 1;
3816 del_timer_sync(&dev_priv->hangcheck_timer);
3818 i915_kernel_lost_context(dev);
3819 i915_gem_cleanup_ringbuffer(dev);
3821 mutex_unlock(&dev->struct_mutex);
3823 /* Cancel the retire work handler, which should be idle now. */
3824 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3830 i915_gem_init_ringbuffer(struct drm_device *dev)
3832 drm_i915_private_t *dev_priv = dev->dev_private;
3835 ret = intel_init_render_ring_buffer(dev);
3840 ret = intel_init_bsd_ring_buffer(dev);
3842 goto cleanup_render_ring;
3846 ret = intel_init_blt_ring_buffer(dev);
3848 goto cleanup_bsd_ring;
3851 dev_priv->next_seqno = 1;
3856 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3857 cleanup_render_ring:
3858 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3863 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3865 drm_i915_private_t *dev_priv = dev->dev_private;
3868 for (i = 0; i < I915_NUM_RINGS; i++)
3869 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
3873 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3874 struct drm_file *file_priv)
3876 drm_i915_private_t *dev_priv = dev->dev_private;
3879 if (drm_core_check_feature(dev, DRIVER_MODESET))
3882 if (atomic_read(&dev_priv->mm.wedged)) {
3883 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3884 atomic_set(&dev_priv->mm.wedged, 0);
3887 mutex_lock(&dev->struct_mutex);
3888 dev_priv->mm.suspended = 0;
3890 ret = i915_gem_init_ringbuffer(dev);
3892 mutex_unlock(&dev->struct_mutex);
3896 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3897 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3898 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3899 for (i = 0; i < I915_NUM_RINGS; i++) {
3900 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3901 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3903 mutex_unlock(&dev->struct_mutex);
3905 ret = drm_irq_install(dev);
3907 goto cleanup_ringbuffer;
3912 mutex_lock(&dev->struct_mutex);
3913 i915_gem_cleanup_ringbuffer(dev);
3914 dev_priv->mm.suspended = 1;
3915 mutex_unlock(&dev->struct_mutex);
3921 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3922 struct drm_file *file_priv)
3924 if (drm_core_check_feature(dev, DRIVER_MODESET))
3927 drm_irq_uninstall(dev);
3928 return i915_gem_idle(dev);
3932 i915_gem_lastclose(struct drm_device *dev)
3936 if (drm_core_check_feature(dev, DRIVER_MODESET))
3939 ret = i915_gem_idle(dev);
3941 DRM_ERROR("failed to idle hardware: %d\n", ret);
3945 init_ring_lists(struct intel_ring_buffer *ring)
3947 INIT_LIST_HEAD(&ring->active_list);
3948 INIT_LIST_HEAD(&ring->request_list);
3949 INIT_LIST_HEAD(&ring->gpu_write_list);
3953 i915_gem_load(struct drm_device *dev)
3956 drm_i915_private_t *dev_priv = dev->dev_private;
3958 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3959 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3960 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3961 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3962 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3963 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
3964 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3965 for (i = 0; i < I915_NUM_RINGS; i++)
3966 init_ring_lists(&dev_priv->ring[i]);
3967 for (i = 0; i < 16; i++)
3968 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3969 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3970 i915_gem_retire_work_handler);
3971 init_completion(&dev_priv->error_completion);
3973 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3975 u32 tmp = I915_READ(MI_ARB_STATE);
3976 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3977 /* arb state is a masked write, so set bit + bit in mask */
3978 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3979 I915_WRITE(MI_ARB_STATE, tmp);
3983 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3985 /* Old X drivers will take 0-2 for front, back, depth buffers */
3986 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3987 dev_priv->fence_reg_start = 3;
3989 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3990 dev_priv->num_fence_regs = 16;
3992 dev_priv->num_fence_regs = 8;
3994 /* Initialize fence registers to zero */
3995 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3996 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
3999 i915_gem_detect_bit_6_swizzle(dev);
4000 init_waitqueue_head(&dev_priv->pending_flip_queue);
4002 dev_priv->mm.interruptible = true;
4004 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4005 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4006 register_shrinker(&dev_priv->mm.inactive_shrinker);
4010 * Create a physically contiguous memory object for this object
4011 * e.g. for cursor + overlay regs
4013 static int i915_gem_init_phys_object(struct drm_device *dev,
4014 int id, int size, int align)
4016 drm_i915_private_t *dev_priv = dev->dev_private;
4017 struct drm_i915_gem_phys_object *phys_obj;
4020 if (dev_priv->mm.phys_objs[id - 1] || !size)
4023 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4029 phys_obj->handle = drm_pci_alloc(dev, size, align);
4030 if (!phys_obj->handle) {
4035 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4038 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4046 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4048 drm_i915_private_t *dev_priv = dev->dev_private;
4049 struct drm_i915_gem_phys_object *phys_obj;
4051 if (!dev_priv->mm.phys_objs[id - 1])
4054 phys_obj = dev_priv->mm.phys_objs[id - 1];
4055 if (phys_obj->cur_obj) {
4056 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4060 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4062 drm_pci_free(dev, phys_obj->handle);
4064 dev_priv->mm.phys_objs[id - 1] = NULL;
4067 void i915_gem_free_all_phys_object(struct drm_device *dev)
4071 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4072 i915_gem_free_phys_object(dev, i);
4075 void i915_gem_detach_phys_object(struct drm_device *dev,
4076 struct drm_i915_gem_object *obj)
4078 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4085 vaddr = obj->phys_obj->handle->vaddr;
4087 page_count = obj->base.size / PAGE_SIZE;
4088 for (i = 0; i < page_count; i++) {
4089 struct page *page = read_cache_page_gfp(mapping, i,
4090 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4091 if (!IS_ERR(page)) {
4092 char *dst = kmap_atomic(page);
4093 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4096 drm_clflush_pages(&page, 1);
4098 set_page_dirty(page);
4099 mark_page_accessed(page);
4100 page_cache_release(page);
4103 intel_gtt_chipset_flush();
4105 obj->phys_obj->cur_obj = NULL;
4106 obj->phys_obj = NULL;
4110 i915_gem_attach_phys_object(struct drm_device *dev,
4111 struct drm_i915_gem_object *obj,
4115 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
4116 drm_i915_private_t *dev_priv = dev->dev_private;
4121 if (id > I915_MAX_PHYS_OBJECT)
4124 if (obj->phys_obj) {
4125 if (obj->phys_obj->id == id)
4127 i915_gem_detach_phys_object(dev, obj);
4130 /* create a new object */
4131 if (!dev_priv->mm.phys_objs[id - 1]) {
4132 ret = i915_gem_init_phys_object(dev, id,
4133 obj->base.size, align);
4135 DRM_ERROR("failed to init phys object %d size: %zu\n",
4136 id, obj->base.size);
4141 /* bind to the object */
4142 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4143 obj->phys_obj->cur_obj = obj;
4145 page_count = obj->base.size / PAGE_SIZE;
4147 for (i = 0; i < page_count; i++) {
4151 page = read_cache_page_gfp(mapping, i,
4152 GFP_HIGHUSER | __GFP_RECLAIMABLE);
4154 return PTR_ERR(page);
4156 src = kmap_atomic(page);
4157 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4158 memcpy(dst, src, PAGE_SIZE);
4161 mark_page_accessed(page);
4162 page_cache_release(page);
4169 i915_gem_phys_pwrite(struct drm_device *dev,
4170 struct drm_i915_gem_object *obj,
4171 struct drm_i915_gem_pwrite *args,
4172 struct drm_file *file_priv)
4174 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4175 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
4177 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4178 unsigned long unwritten;
4180 /* The physical object once assigned is fixed for the lifetime
4181 * of the obj, so we can safely drop the lock and continue
4184 mutex_unlock(&dev->struct_mutex);
4185 unwritten = copy_from_user(vaddr, user_data, args->size);
4186 mutex_lock(&dev->struct_mutex);
4191 intel_gtt_chipset_flush();
4195 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4197 struct drm_i915_file_private *file_priv = file->driver_priv;
4199 /* Clean up our request list when the client is going away, so that
4200 * later retire_requests won't dereference our soon-to-be-gone
4203 spin_lock(&file_priv->mm.lock);
4204 while (!list_empty(&file_priv->mm.request_list)) {
4205 struct drm_i915_gem_request *request;
4207 request = list_first_entry(&file_priv->mm.request_list,
4208 struct drm_i915_gem_request,
4210 list_del(&request->client_list);
4211 request->file_priv = NULL;
4213 spin_unlock(&file_priv->mm.lock);
4217 i915_gpu_is_active(struct drm_device *dev)
4219 drm_i915_private_t *dev_priv = dev->dev_private;
4222 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4223 list_empty(&dev_priv->mm.active_list);
4225 return !lists_empty;
4229 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4231 struct drm_i915_private *dev_priv =
4232 container_of(shrinker,
4233 struct drm_i915_private,
4234 mm.inactive_shrinker);
4235 struct drm_device *dev = dev_priv->dev;
4236 struct drm_i915_gem_object *obj, *next;
4237 int nr_to_scan = sc->nr_to_scan;
4240 if (!mutex_trylock(&dev->struct_mutex))
4243 /* "fast-path" to count number of available objects */
4244 if (nr_to_scan == 0) {
4246 list_for_each_entry(obj,
4247 &dev_priv->mm.inactive_list,
4250 mutex_unlock(&dev->struct_mutex);
4251 return cnt / 100 * sysctl_vfs_cache_pressure;
4255 /* first scan for clean buffers */
4256 i915_gem_retire_requests(dev);
4258 list_for_each_entry_safe(obj, next,
4259 &dev_priv->mm.inactive_list,
4261 if (i915_gem_object_is_purgeable(obj)) {
4262 if (i915_gem_object_unbind(obj) == 0 &&
4268 /* second pass, evict/count anything still on the inactive list */
4270 list_for_each_entry_safe(obj, next,
4271 &dev_priv->mm.inactive_list,
4274 i915_gem_object_unbind(obj) == 0)
4280 if (nr_to_scan && i915_gpu_is_active(dev)) {
4282 * We are desperate for pages, so as a last resort, wait
4283 * for the GPU to finish and discard whatever we can.
4284 * This has a dramatic impact to reduce the number of
4285 * OOM-killer events whilst running the GPU aggressively.
4287 if (i915_gpu_idle(dev) == 0)
4290 mutex_unlock(&dev->struct_mutex);
4291 return cnt / 100 * sysctl_vfs_cache_pressure;