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>
29 #include <drm/drm_vma_manager.h>
30 #include <drm/i915_drm.h>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/shmem_fs.h>
35 #include <linux/slab.h>
36 #include <linux/swap.h>
37 #include <linux/pci.h>
38 #include <linux/dma-buf.h>
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
42 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
44 bool map_and_fenceable,
46 static int i915_gem_phys_pwrite(struct drm_device *dev,
47 struct drm_i915_gem_object *obj,
48 struct drm_i915_gem_pwrite *args,
49 struct drm_file *file);
51 static void i915_gem_write_fence(struct drm_device *dev, int reg,
52 struct drm_i915_gem_object *obj);
53 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
54 struct drm_i915_fence_reg *fence,
57 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
58 struct shrink_control *sc);
59 static long i915_gem_purge(struct drm_i915_private *dev_priv, long target);
60 static void i915_gem_shrink_all(struct drm_i915_private *dev_priv);
61 static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
63 static inline void i915_gem_object_fence_lost(struct drm_i915_gem_object *obj)
66 i915_gem_release_mmap(obj);
68 /* As we do not have an associated fence register, we will force
69 * a tiling change if we ever need to acquire one.
71 obj->fence_dirty = false;
72 obj->fence_reg = I915_FENCE_REG_NONE;
75 /* some bookkeeping */
76 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
79 spin_lock(&dev_priv->mm.object_stat_lock);
80 dev_priv->mm.object_count++;
81 dev_priv->mm.object_memory += size;
82 spin_unlock(&dev_priv->mm.object_stat_lock);
85 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
88 spin_lock(&dev_priv->mm.object_stat_lock);
89 dev_priv->mm.object_count--;
90 dev_priv->mm.object_memory -= size;
91 spin_unlock(&dev_priv->mm.object_stat_lock);
95 i915_gem_wait_for_error(struct i915_gpu_error *error)
99 #define EXIT_COND (!i915_reset_in_progress(error) || \
100 i915_terminally_wedged(error))
105 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
106 * userspace. If it takes that long something really bad is going on and
107 * we should simply try to bail out and fail as gracefully as possible.
109 ret = wait_event_interruptible_timeout(error->reset_queue,
113 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
115 } else if (ret < 0) {
123 int i915_mutex_lock_interruptible(struct drm_device *dev)
125 struct drm_i915_private *dev_priv = dev->dev_private;
128 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
132 ret = mutex_lock_interruptible(&dev->struct_mutex);
136 WARN_ON(i915_verify_lists(dev));
141 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
143 return i915_gem_obj_ggtt_bound(obj) && !obj->active;
147 i915_gem_init_ioctl(struct drm_device *dev, void *data,
148 struct drm_file *file)
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 struct drm_i915_gem_init *args = data;
153 if (drm_core_check_feature(dev, DRIVER_MODESET))
156 if (args->gtt_start >= args->gtt_end ||
157 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
160 /* GEM with user mode setting was never supported on ilk and later. */
161 if (INTEL_INFO(dev)->gen >= 5)
164 mutex_lock(&dev->struct_mutex);
165 i915_gem_setup_global_gtt(dev, args->gtt_start, args->gtt_end,
167 dev_priv->gtt.mappable_end = args->gtt_end;
168 mutex_unlock(&dev->struct_mutex);
174 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
175 struct drm_file *file)
177 struct drm_i915_private *dev_priv = dev->dev_private;
178 struct drm_i915_gem_get_aperture *args = data;
179 struct drm_i915_gem_object *obj;
183 mutex_lock(&dev->struct_mutex);
184 list_for_each_entry(obj, &dev_priv->mm.bound_list, global_list)
186 pinned += i915_gem_obj_ggtt_size(obj);
187 mutex_unlock(&dev->struct_mutex);
189 args->aper_size = dev_priv->gtt.base.total;
190 args->aper_available_size = args->aper_size - pinned;
195 void *i915_gem_object_alloc(struct drm_device *dev)
197 struct drm_i915_private *dev_priv = dev->dev_private;
198 return kmem_cache_alloc(dev_priv->slab, GFP_KERNEL | __GFP_ZERO);
201 void i915_gem_object_free(struct drm_i915_gem_object *obj)
203 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
204 kmem_cache_free(dev_priv->slab, obj);
208 i915_gem_create(struct drm_file *file,
209 struct drm_device *dev,
213 struct drm_i915_gem_object *obj;
217 size = roundup(size, PAGE_SIZE);
221 /* Allocate the new object */
222 obj = i915_gem_alloc_object(dev, size);
226 ret = drm_gem_handle_create(file, &obj->base, &handle);
227 /* drop reference from allocate - handle holds it now */
228 drm_gem_object_unreference_unlocked(&obj->base);
237 i915_gem_dumb_create(struct drm_file *file,
238 struct drm_device *dev,
239 struct drm_mode_create_dumb *args)
241 /* have to work out size/pitch and return them */
242 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
243 args->size = args->pitch * args->height;
244 return i915_gem_create(file, dev,
245 args->size, &args->handle);
249 * Creates a new mm object and returns a handle to it.
252 i915_gem_create_ioctl(struct drm_device *dev, void *data,
253 struct drm_file *file)
255 struct drm_i915_gem_create *args = data;
257 return i915_gem_create(file, dev,
258 args->size, &args->handle);
262 __copy_to_user_swizzled(char __user *cpu_vaddr,
263 const char *gpu_vaddr, int gpu_offset,
266 int ret, cpu_offset = 0;
269 int cacheline_end = ALIGN(gpu_offset + 1, 64);
270 int this_length = min(cacheline_end - gpu_offset, length);
271 int swizzled_gpu_offset = gpu_offset ^ 64;
273 ret = __copy_to_user(cpu_vaddr + cpu_offset,
274 gpu_vaddr + swizzled_gpu_offset,
279 cpu_offset += this_length;
280 gpu_offset += this_length;
281 length -= this_length;
288 __copy_from_user_swizzled(char *gpu_vaddr, int gpu_offset,
289 const char __user *cpu_vaddr,
292 int ret, cpu_offset = 0;
295 int cacheline_end = ALIGN(gpu_offset + 1, 64);
296 int this_length = min(cacheline_end - gpu_offset, length);
297 int swizzled_gpu_offset = gpu_offset ^ 64;
299 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
300 cpu_vaddr + cpu_offset,
305 cpu_offset += this_length;
306 gpu_offset += this_length;
307 length -= this_length;
313 /* Per-page copy function for the shmem pread fastpath.
314 * Flushes invalid cachelines before reading the target if
315 * needs_clflush is set. */
317 shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
318 char __user *user_data,
319 bool page_do_bit17_swizzling, bool needs_clflush)
324 if (unlikely(page_do_bit17_swizzling))
327 vaddr = kmap_atomic(page);
329 drm_clflush_virt_range(vaddr + shmem_page_offset,
331 ret = __copy_to_user_inatomic(user_data,
332 vaddr + shmem_page_offset,
334 kunmap_atomic(vaddr);
336 return ret ? -EFAULT : 0;
340 shmem_clflush_swizzled_range(char *addr, unsigned long length,
343 if (unlikely(swizzled)) {
344 unsigned long start = (unsigned long) addr;
345 unsigned long end = (unsigned long) addr + length;
347 /* For swizzling simply ensure that we always flush both
348 * channels. Lame, but simple and it works. Swizzled
349 * pwrite/pread is far from a hotpath - current userspace
350 * doesn't use it at all. */
351 start = round_down(start, 128);
352 end = round_up(end, 128);
354 drm_clflush_virt_range((void *)start, end - start);
356 drm_clflush_virt_range(addr, length);
361 /* Only difference to the fast-path function is that this can handle bit17
362 * and uses non-atomic copy and kmap functions. */
364 shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
365 char __user *user_data,
366 bool page_do_bit17_swizzling, bool needs_clflush)
373 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
375 page_do_bit17_swizzling);
377 if (page_do_bit17_swizzling)
378 ret = __copy_to_user_swizzled(user_data,
379 vaddr, shmem_page_offset,
382 ret = __copy_to_user(user_data,
383 vaddr + shmem_page_offset,
387 return ret ? - EFAULT : 0;
391 i915_gem_shmem_pread(struct drm_device *dev,
392 struct drm_i915_gem_object *obj,
393 struct drm_i915_gem_pread *args,
394 struct drm_file *file)
396 char __user *user_data;
399 int shmem_page_offset, page_length, ret = 0;
400 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
402 int needs_clflush = 0;
403 struct sg_page_iter sg_iter;
405 user_data = to_user_ptr(args->data_ptr);
408 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
410 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
411 /* If we're not in the cpu read domain, set ourself into the gtt
412 * read domain and manually flush cachelines (if required). This
413 * optimizes for the case when the gpu will dirty the data
414 * anyway again before the next pread happens. */
415 if (obj->cache_level == I915_CACHE_NONE)
417 if (i915_gem_obj_ggtt_bound(obj)) {
418 ret = i915_gem_object_set_to_gtt_domain(obj, false);
424 ret = i915_gem_object_get_pages(obj);
428 i915_gem_object_pin_pages(obj);
430 offset = args->offset;
432 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
433 offset >> PAGE_SHIFT) {
434 struct page *page = sg_page_iter_page(&sg_iter);
439 /* Operation in this page
441 * shmem_page_offset = offset within page in shmem file
442 * page_length = bytes to copy for this page
444 shmem_page_offset = offset_in_page(offset);
445 page_length = remain;
446 if ((shmem_page_offset + page_length) > PAGE_SIZE)
447 page_length = PAGE_SIZE - shmem_page_offset;
449 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
450 (page_to_phys(page) & (1 << 17)) != 0;
452 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
453 user_data, page_do_bit17_swizzling,
458 mutex_unlock(&dev->struct_mutex);
460 if (likely(!i915_prefault_disable) && !prefaulted) {
461 ret = fault_in_multipages_writeable(user_data, remain);
462 /* Userspace is tricking us, but we've already clobbered
463 * its pages with the prefault and promised to write the
464 * data up to the first fault. Hence ignore any errors
465 * and just continue. */
470 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
471 user_data, page_do_bit17_swizzling,
474 mutex_lock(&dev->struct_mutex);
477 mark_page_accessed(page);
482 remain -= page_length;
483 user_data += page_length;
484 offset += page_length;
488 i915_gem_object_unpin_pages(obj);
494 * Reads data from the object referenced by handle.
496 * On error, the contents of *data are undefined.
499 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
500 struct drm_file *file)
502 struct drm_i915_gem_pread *args = data;
503 struct drm_i915_gem_object *obj;
509 if (!access_ok(VERIFY_WRITE,
510 to_user_ptr(args->data_ptr),
514 ret = i915_mutex_lock_interruptible(dev);
518 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
519 if (&obj->base == NULL) {
524 /* Bounds check source. */
525 if (args->offset > obj->base.size ||
526 args->size > obj->base.size - args->offset) {
531 /* prime objects have no backing filp to GEM pread/pwrite
534 if (!obj->base.filp) {
539 trace_i915_gem_object_pread(obj, args->offset, args->size);
541 ret = i915_gem_shmem_pread(dev, obj, args, file);
544 drm_gem_object_unreference(&obj->base);
546 mutex_unlock(&dev->struct_mutex);
550 /* This is the fast write path which cannot handle
551 * page faults in the source data
555 fast_user_write(struct io_mapping *mapping,
556 loff_t page_base, int page_offset,
557 char __user *user_data,
560 void __iomem *vaddr_atomic;
562 unsigned long unwritten;
564 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
565 /* We can use the cpu mem copy function because this is X86. */
566 vaddr = (void __force*)vaddr_atomic + page_offset;
567 unwritten = __copy_from_user_inatomic_nocache(vaddr,
569 io_mapping_unmap_atomic(vaddr_atomic);
574 * This is the fast pwrite path, where we copy the data directly from the
575 * user into the GTT, uncached.
578 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
579 struct drm_i915_gem_object *obj,
580 struct drm_i915_gem_pwrite *args,
581 struct drm_file *file)
583 drm_i915_private_t *dev_priv = dev->dev_private;
585 loff_t offset, page_base;
586 char __user *user_data;
587 int page_offset, page_length, ret;
589 ret = i915_gem_object_pin(obj, 0, true, true);
593 ret = i915_gem_object_set_to_gtt_domain(obj, true);
597 ret = i915_gem_object_put_fence(obj);
601 user_data = to_user_ptr(args->data_ptr);
604 offset = i915_gem_obj_ggtt_offset(obj) + args->offset;
607 /* Operation in this page
609 * page_base = page offset within aperture
610 * page_offset = offset within page
611 * page_length = bytes to copy for this page
613 page_base = offset & PAGE_MASK;
614 page_offset = offset_in_page(offset);
615 page_length = remain;
616 if ((page_offset + remain) > PAGE_SIZE)
617 page_length = PAGE_SIZE - page_offset;
619 /* If we get a fault while copying data, then (presumably) our
620 * source page isn't available. Return the error and we'll
621 * retry in the slow path.
623 if (fast_user_write(dev_priv->gtt.mappable, page_base,
624 page_offset, user_data, page_length)) {
629 remain -= page_length;
630 user_data += page_length;
631 offset += page_length;
635 i915_gem_object_unpin(obj);
640 /* Per-page copy function for the shmem pwrite fastpath.
641 * Flushes invalid cachelines before writing to the target if
642 * needs_clflush_before is set and flushes out any written cachelines after
643 * writing if needs_clflush is set. */
645 shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
646 char __user *user_data,
647 bool page_do_bit17_swizzling,
648 bool needs_clflush_before,
649 bool needs_clflush_after)
654 if (unlikely(page_do_bit17_swizzling))
657 vaddr = kmap_atomic(page);
658 if (needs_clflush_before)
659 drm_clflush_virt_range(vaddr + shmem_page_offset,
661 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
664 if (needs_clflush_after)
665 drm_clflush_virt_range(vaddr + shmem_page_offset,
667 kunmap_atomic(vaddr);
669 return ret ? -EFAULT : 0;
672 /* Only difference to the fast-path function is that this can handle bit17
673 * and uses non-atomic copy and kmap functions. */
675 shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
676 char __user *user_data,
677 bool page_do_bit17_swizzling,
678 bool needs_clflush_before,
679 bool needs_clflush_after)
685 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
686 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
688 page_do_bit17_swizzling);
689 if (page_do_bit17_swizzling)
690 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
694 ret = __copy_from_user(vaddr + shmem_page_offset,
697 if (needs_clflush_after)
698 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
700 page_do_bit17_swizzling);
703 return ret ? -EFAULT : 0;
707 i915_gem_shmem_pwrite(struct drm_device *dev,
708 struct drm_i915_gem_object *obj,
709 struct drm_i915_gem_pwrite *args,
710 struct drm_file *file)
714 char __user *user_data;
715 int shmem_page_offset, page_length, ret = 0;
716 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
717 int hit_slowpath = 0;
718 int needs_clflush_after = 0;
719 int needs_clflush_before = 0;
720 struct sg_page_iter sg_iter;
722 user_data = to_user_ptr(args->data_ptr);
725 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
727 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
728 /* If we're not in the cpu write domain, set ourself into the gtt
729 * write domain and manually flush cachelines (if required). This
730 * optimizes for the case when the gpu will use the data
731 * right away and we therefore have to clflush anyway. */
732 if (obj->cache_level == I915_CACHE_NONE)
733 needs_clflush_after = 1;
734 if (i915_gem_obj_ggtt_bound(obj)) {
735 ret = i915_gem_object_set_to_gtt_domain(obj, true);
740 /* Same trick applies for invalidate partially written cachelines before
742 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
743 && obj->cache_level == I915_CACHE_NONE)
744 needs_clflush_before = 1;
746 ret = i915_gem_object_get_pages(obj);
750 i915_gem_object_pin_pages(obj);
752 offset = args->offset;
755 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents,
756 offset >> PAGE_SHIFT) {
757 struct page *page = sg_page_iter_page(&sg_iter);
758 int partial_cacheline_write;
763 /* Operation in this page
765 * shmem_page_offset = offset within page in shmem file
766 * page_length = bytes to copy for this page
768 shmem_page_offset = offset_in_page(offset);
770 page_length = remain;
771 if ((shmem_page_offset + page_length) > PAGE_SIZE)
772 page_length = PAGE_SIZE - shmem_page_offset;
774 /* If we don't overwrite a cacheline completely we need to be
775 * careful to have up-to-date data by first clflushing. Don't
776 * overcomplicate things and flush the entire patch. */
777 partial_cacheline_write = needs_clflush_before &&
778 ((shmem_page_offset | page_length)
779 & (boot_cpu_data.x86_clflush_size - 1));
781 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
782 (page_to_phys(page) & (1 << 17)) != 0;
784 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
785 user_data, page_do_bit17_swizzling,
786 partial_cacheline_write,
787 needs_clflush_after);
792 mutex_unlock(&dev->struct_mutex);
793 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
794 user_data, page_do_bit17_swizzling,
795 partial_cacheline_write,
796 needs_clflush_after);
798 mutex_lock(&dev->struct_mutex);
801 set_page_dirty(page);
802 mark_page_accessed(page);
807 remain -= page_length;
808 user_data += page_length;
809 offset += page_length;
813 i915_gem_object_unpin_pages(obj);
817 * Fixup: Flush cpu caches in case we didn't flush the dirty
818 * cachelines in-line while writing and the object moved
819 * out of the cpu write domain while we've dropped the lock.
821 if (!needs_clflush_after &&
822 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
823 i915_gem_clflush_object(obj);
824 i915_gem_chipset_flush(dev);
828 if (needs_clflush_after)
829 i915_gem_chipset_flush(dev);
835 * Writes data to the object referenced by handle.
837 * On error, the contents of the buffer that were to be modified are undefined.
840 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
841 struct drm_file *file)
843 struct drm_i915_gem_pwrite *args = data;
844 struct drm_i915_gem_object *obj;
850 if (!access_ok(VERIFY_READ,
851 to_user_ptr(args->data_ptr),
855 if (likely(!i915_prefault_disable)) {
856 ret = fault_in_multipages_readable(to_user_ptr(args->data_ptr),
862 ret = i915_mutex_lock_interruptible(dev);
866 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
867 if (&obj->base == NULL) {
872 /* Bounds check destination. */
873 if (args->offset > obj->base.size ||
874 args->size > obj->base.size - args->offset) {
879 /* prime objects have no backing filp to GEM pread/pwrite
882 if (!obj->base.filp) {
887 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
890 /* We can only do the GTT pwrite on untiled buffers, as otherwise
891 * it would end up going through the fenced access, and we'll get
892 * different detiling behavior between reading and writing.
893 * pread/pwrite currently are reading and writing from the CPU
894 * perspective, requiring manual detiling by the client.
897 ret = i915_gem_phys_pwrite(dev, obj, args, file);
901 if (obj->cache_level == I915_CACHE_NONE &&
902 obj->tiling_mode == I915_TILING_NONE &&
903 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
904 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
905 /* Note that the gtt paths might fail with non-page-backed user
906 * pointers (e.g. gtt mappings when moving data between
907 * textures). Fallback to the shmem path in that case. */
910 if (ret == -EFAULT || ret == -ENOSPC)
911 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
914 drm_gem_object_unreference(&obj->base);
916 mutex_unlock(&dev->struct_mutex);
921 i915_gem_check_wedge(struct i915_gpu_error *error,
924 if (i915_reset_in_progress(error)) {
925 /* Non-interruptible callers can't handle -EAGAIN, hence return
926 * -EIO unconditionally for these. */
930 /* Recovery complete, but the reset failed ... */
931 if (i915_terminally_wedged(error))
941 * Compare seqno against outstanding lazy request. Emit a request if they are
945 i915_gem_check_olr(struct intel_ring_buffer *ring, u32 seqno)
949 BUG_ON(!mutex_is_locked(&ring->dev->struct_mutex));
952 if (seqno == ring->outstanding_lazy_request)
953 ret = i915_add_request(ring, NULL);
959 * __wait_seqno - wait until execution of seqno has finished
960 * @ring: the ring expected to report seqno
962 * @reset_counter: reset sequence associated with the given seqno
963 * @interruptible: do an interruptible wait (normally yes)
964 * @timeout: in - how long to wait (NULL forever); out - how much time remaining
966 * Note: It is of utmost importance that the passed in seqno and reset_counter
967 * values have been read by the caller in an smp safe manner. Where read-side
968 * locks are involved, it is sufficient to read the reset_counter before
969 * unlocking the lock that protects the seqno. For lockless tricks, the
970 * reset_counter _must_ be read before, and an appropriate smp_rmb must be
973 * Returns 0 if the seqno was found within the alloted time. Else returns the
974 * errno with remaining time filled in timeout argument.
976 static int __wait_seqno(struct intel_ring_buffer *ring, u32 seqno,
977 unsigned reset_counter,
978 bool interruptible, struct timespec *timeout)
980 drm_i915_private_t *dev_priv = ring->dev->dev_private;
981 struct timespec before, now, wait_time={1,0};
982 unsigned long timeout_jiffies;
984 bool wait_forever = true;
987 if (i915_seqno_passed(ring->get_seqno(ring, true), seqno))
990 trace_i915_gem_request_wait_begin(ring, seqno);
992 if (timeout != NULL) {
993 wait_time = *timeout;
994 wait_forever = false;
997 timeout_jiffies = timespec_to_jiffies_timeout(&wait_time);
999 if (WARN_ON(!ring->irq_get(ring)))
1002 /* Record current time in case interrupted by signal, or wedged * */
1003 getrawmonotonic(&before);
1006 (i915_seqno_passed(ring->get_seqno(ring, false), seqno) || \
1007 i915_reset_in_progress(&dev_priv->gpu_error) || \
1008 reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1011 end = wait_event_interruptible_timeout(ring->irq_queue,
1015 end = wait_event_timeout(ring->irq_queue, EXIT_COND,
1018 /* We need to check whether any gpu reset happened in between
1019 * the caller grabbing the seqno and now ... */
1020 if (reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
1023 /* ... but upgrade the -EGAIN to an -EIO if the gpu is truely
1025 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1028 } while (end == 0 && wait_forever);
1030 getrawmonotonic(&now);
1032 ring->irq_put(ring);
1033 trace_i915_gem_request_wait_end(ring, seqno);
1037 struct timespec sleep_time = timespec_sub(now, before);
1038 *timeout = timespec_sub(*timeout, sleep_time);
1039 if (!timespec_valid(timeout)) /* i.e. negative time remains */
1040 set_normalized_timespec(timeout, 0, 0);
1045 case -EAGAIN: /* Wedged */
1046 case -ERESTARTSYS: /* Signal */
1048 case 0: /* Timeout */
1050 default: /* Completed */
1051 WARN_ON(end < 0); /* We're not aware of other errors */
1057 * Waits for a sequence number to be signaled, and cleans up the
1058 * request and object lists appropriately for that event.
1061 i915_wait_seqno(struct intel_ring_buffer *ring, uint32_t seqno)
1063 struct drm_device *dev = ring->dev;
1064 struct drm_i915_private *dev_priv = dev->dev_private;
1065 bool interruptible = dev_priv->mm.interruptible;
1068 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1071 ret = i915_gem_check_wedge(&dev_priv->gpu_error, interruptible);
1075 ret = i915_gem_check_olr(ring, seqno);
1079 return __wait_seqno(ring, seqno,
1080 atomic_read(&dev_priv->gpu_error.reset_counter),
1081 interruptible, NULL);
1085 i915_gem_object_wait_rendering__tail(struct drm_i915_gem_object *obj,
1086 struct intel_ring_buffer *ring)
1088 i915_gem_retire_requests_ring(ring);
1090 /* Manually manage the write flush as we may have not yet
1091 * retired the buffer.
1093 * Note that the last_write_seqno is always the earlier of
1094 * the two (read/write) seqno, so if we haved successfully waited,
1095 * we know we have passed the last write.
1097 obj->last_write_seqno = 0;
1098 obj->base.write_domain &= ~I915_GEM_GPU_DOMAINS;
1104 * Ensures that all rendering to the object has completed and the object is
1105 * safe to unbind from the GTT or access from the CPU.
1107 static __must_check int
1108 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj,
1111 struct intel_ring_buffer *ring = obj->ring;
1115 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1119 ret = i915_wait_seqno(ring, seqno);
1123 return i915_gem_object_wait_rendering__tail(obj, ring);
1126 /* A nonblocking variant of the above wait. This is a highly dangerous routine
1127 * as the object state may change during this call.
1129 static __must_check int
1130 i915_gem_object_wait_rendering__nonblocking(struct drm_i915_gem_object *obj,
1133 struct drm_device *dev = obj->base.dev;
1134 struct drm_i915_private *dev_priv = dev->dev_private;
1135 struct intel_ring_buffer *ring = obj->ring;
1136 unsigned reset_counter;
1140 BUG_ON(!mutex_is_locked(&dev->struct_mutex));
1141 BUG_ON(!dev_priv->mm.interruptible);
1143 seqno = readonly ? obj->last_write_seqno : obj->last_read_seqno;
1147 ret = i915_gem_check_wedge(&dev_priv->gpu_error, true);
1151 ret = i915_gem_check_olr(ring, seqno);
1155 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
1156 mutex_unlock(&dev->struct_mutex);
1157 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
1158 mutex_lock(&dev->struct_mutex);
1162 return i915_gem_object_wait_rendering__tail(obj, ring);
1166 * Called when user space prepares to use an object with the CPU, either
1167 * through the mmap ioctl's mapping or a GTT mapping.
1170 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
1171 struct drm_file *file)
1173 struct drm_i915_gem_set_domain *args = data;
1174 struct drm_i915_gem_object *obj;
1175 uint32_t read_domains = args->read_domains;
1176 uint32_t write_domain = args->write_domain;
1179 /* Only handle setting domains to types used by the CPU. */
1180 if (write_domain & I915_GEM_GPU_DOMAINS)
1183 if (read_domains & I915_GEM_GPU_DOMAINS)
1186 /* Having something in the write domain implies it's in the read
1187 * domain, and only that read domain. Enforce that in the request.
1189 if (write_domain != 0 && read_domains != write_domain)
1192 ret = i915_mutex_lock_interruptible(dev);
1196 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1197 if (&obj->base == NULL) {
1202 /* Try to flush the object off the GPU without holding the lock.
1203 * We will repeat the flush holding the lock in the normal manner
1204 * to catch cases where we are gazumped.
1206 ret = i915_gem_object_wait_rendering__nonblocking(obj, !write_domain);
1210 if (read_domains & I915_GEM_DOMAIN_GTT) {
1211 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
1213 /* Silently promote "you're not bound, there was nothing to do"
1214 * to success, since the client was just asking us to
1215 * make sure everything was done.
1220 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
1224 drm_gem_object_unreference(&obj->base);
1226 mutex_unlock(&dev->struct_mutex);
1231 * Called when user space has done writes to this buffer
1234 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
1235 struct drm_file *file)
1237 struct drm_i915_gem_sw_finish *args = data;
1238 struct drm_i915_gem_object *obj;
1241 ret = i915_mutex_lock_interruptible(dev);
1245 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
1246 if (&obj->base == NULL) {
1251 /* Pinned buffers may be scanout, so flush the cache */
1253 i915_gem_object_flush_cpu_write_domain(obj);
1255 drm_gem_object_unreference(&obj->base);
1257 mutex_unlock(&dev->struct_mutex);
1262 * Maps the contents of an object, returning the address it is mapped
1265 * While the mapping holds a reference on the contents of the object, it doesn't
1266 * imply a ref on the object itself.
1269 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
1270 struct drm_file *file)
1272 struct drm_i915_gem_mmap *args = data;
1273 struct drm_gem_object *obj;
1276 obj = drm_gem_object_lookup(dev, file, args->handle);
1280 /* prime objects have no backing filp to GEM mmap
1284 drm_gem_object_unreference_unlocked(obj);
1288 addr = vm_mmap(obj->filp, 0, args->size,
1289 PROT_READ | PROT_WRITE, MAP_SHARED,
1291 drm_gem_object_unreference_unlocked(obj);
1292 if (IS_ERR((void *)addr))
1295 args->addr_ptr = (uint64_t) addr;
1301 * i915_gem_fault - fault a page into the GTT
1302 * vma: VMA in question
1305 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1306 * from userspace. The fault handler takes care of binding the object to
1307 * the GTT (if needed), allocating and programming a fence register (again,
1308 * only if needed based on whether the old reg is still valid or the object
1309 * is tiled) and inserting a new PTE into the faulting process.
1311 * Note that the faulting process may involve evicting existing objects
1312 * from the GTT and/or fence registers to make room. So performance may
1313 * suffer if the GTT working set is large or there are few fence registers
1316 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1318 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1319 struct drm_device *dev = obj->base.dev;
1320 drm_i915_private_t *dev_priv = dev->dev_private;
1321 pgoff_t page_offset;
1324 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1326 /* We don't use vmf->pgoff since that has the fake offset */
1327 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1330 ret = i915_mutex_lock_interruptible(dev);
1334 trace_i915_gem_object_fault(obj, page_offset, true, write);
1336 /* Access to snoopable pages through the GTT is incoherent. */
1337 if (obj->cache_level != I915_CACHE_NONE && !HAS_LLC(dev)) {
1342 /* Now bind it into the GTT if needed */
1343 ret = i915_gem_object_pin(obj, 0, true, false);
1347 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1351 ret = i915_gem_object_get_fence(obj);
1355 obj->fault_mappable = true;
1357 pfn = dev_priv->gtt.mappable_base + i915_gem_obj_ggtt_offset(obj);
1361 /* Finally, remap it using the new GTT offset */
1362 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1364 i915_gem_object_unpin(obj);
1366 mutex_unlock(&dev->struct_mutex);
1370 /* If this -EIO is due to a gpu hang, give the reset code a
1371 * chance to clean up the mess. Otherwise return the proper
1373 if (i915_terminally_wedged(&dev_priv->gpu_error))
1374 return VM_FAULT_SIGBUS;
1376 /* Give the error handler a chance to run and move the
1377 * objects off the GPU active list. Next time we service the
1378 * fault, we should be able to transition the page into the
1379 * GTT without touching the GPU (and so avoid further
1380 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1381 * with coherency, just lost writes.
1389 * EBUSY is ok: this just means that another thread
1390 * already did the job.
1392 return VM_FAULT_NOPAGE;
1394 return VM_FAULT_OOM;
1396 return VM_FAULT_SIGBUS;
1398 WARN_ONCE(ret, "unhandled error in i915_gem_fault: %i\n", ret);
1399 return VM_FAULT_SIGBUS;
1404 * i915_gem_release_mmap - remove physical page mappings
1405 * @obj: obj in question
1407 * Preserve the reservation of the mmapping with the DRM core code, but
1408 * relinquish ownership of the pages back to the system.
1410 * It is vital that we remove the page mapping if we have mapped a tiled
1411 * object through the GTT and then lose the fence register due to
1412 * resource pressure. Similarly if the object has been moved out of the
1413 * aperture, than pages mapped into userspace must be revoked. Removing the
1414 * mapping will then trigger a page fault on the next user access, allowing
1415 * fixup by i915_gem_fault().
1418 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1420 if (!obj->fault_mappable)
1423 drm_vma_node_unmap(&obj->base.vma_node, obj->base.dev->dev_mapping);
1424 obj->fault_mappable = false;
1428 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1432 if (INTEL_INFO(dev)->gen >= 4 ||
1433 tiling_mode == I915_TILING_NONE)
1436 /* Previous chips need a power-of-two fence region when tiling */
1437 if (INTEL_INFO(dev)->gen == 3)
1438 gtt_size = 1024*1024;
1440 gtt_size = 512*1024;
1442 while (gtt_size < size)
1449 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1450 * @obj: object to check
1452 * Return the required GTT alignment for an object, taking into account
1453 * potential fence register mapping.
1456 i915_gem_get_gtt_alignment(struct drm_device *dev, uint32_t size,
1457 int tiling_mode, bool fenced)
1460 * Minimum alignment is 4k (GTT page size), but might be greater
1461 * if a fence register is needed for the object.
1463 if (INTEL_INFO(dev)->gen >= 4 || (!fenced && IS_G33(dev)) ||
1464 tiling_mode == I915_TILING_NONE)
1468 * Previous chips need to be aligned to the size of the smallest
1469 * fence register that can contain the object.
1471 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1474 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object *obj)
1476 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1479 if (drm_vma_node_has_offset(&obj->base.vma_node))
1482 dev_priv->mm.shrinker_no_lock_stealing = true;
1484 ret = drm_gem_create_mmap_offset(&obj->base);
1488 /* Badly fragmented mmap space? The only way we can recover
1489 * space is by destroying unwanted objects. We can't randomly release
1490 * mmap_offsets as userspace expects them to be persistent for the
1491 * lifetime of the objects. The closest we can is to release the
1492 * offsets on purgeable objects by truncating it and marking it purged,
1493 * which prevents userspace from ever using that object again.
1495 i915_gem_purge(dev_priv, obj->base.size >> PAGE_SHIFT);
1496 ret = drm_gem_create_mmap_offset(&obj->base);
1500 i915_gem_shrink_all(dev_priv);
1501 ret = drm_gem_create_mmap_offset(&obj->base);
1503 dev_priv->mm.shrinker_no_lock_stealing = false;
1508 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object *obj)
1510 drm_gem_free_mmap_offset(&obj->base);
1514 i915_gem_mmap_gtt(struct drm_file *file,
1515 struct drm_device *dev,
1519 struct drm_i915_private *dev_priv = dev->dev_private;
1520 struct drm_i915_gem_object *obj;
1523 ret = i915_mutex_lock_interruptible(dev);
1527 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1528 if (&obj->base == NULL) {
1533 if (obj->base.size > dev_priv->gtt.mappable_end) {
1538 if (obj->madv != I915_MADV_WILLNEED) {
1539 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1544 ret = i915_gem_object_create_mmap_offset(obj);
1548 *offset = drm_vma_node_offset_addr(&obj->base.vma_node);
1551 drm_gem_object_unreference(&obj->base);
1553 mutex_unlock(&dev->struct_mutex);
1558 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1560 * @data: GTT mapping ioctl data
1561 * @file: GEM object info
1563 * Simply returns the fake offset to userspace so it can mmap it.
1564 * The mmap call will end up in drm_gem_mmap(), which will set things
1565 * up so we can get faults in the handler above.
1567 * The fault handler will take care of binding the object into the GTT
1568 * (since it may have been evicted to make room for something), allocating
1569 * a fence register, and mapping the appropriate aperture address into
1573 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1574 struct drm_file *file)
1576 struct drm_i915_gem_mmap_gtt *args = data;
1578 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1581 /* Immediately discard the backing storage */
1583 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1585 struct inode *inode;
1587 i915_gem_object_free_mmap_offset(obj);
1589 if (obj->base.filp == NULL)
1592 /* Our goal here is to return as much of the memory as
1593 * is possible back to the system as we are called from OOM.
1594 * To do this we must instruct the shmfs to drop all of its
1595 * backing pages, *now*.
1597 inode = file_inode(obj->base.filp);
1598 shmem_truncate_range(inode, 0, (loff_t)-1);
1600 obj->madv = __I915_MADV_PURGED;
1604 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1606 return obj->madv == I915_MADV_DONTNEED;
1610 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1612 struct sg_page_iter sg_iter;
1615 BUG_ON(obj->madv == __I915_MADV_PURGED);
1617 ret = i915_gem_object_set_to_cpu_domain(obj, true);
1619 /* In the event of a disaster, abandon all caches and
1620 * hope for the best.
1622 WARN_ON(ret != -EIO);
1623 i915_gem_clflush_object(obj);
1624 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
1627 if (i915_gem_object_needs_bit17_swizzle(obj))
1628 i915_gem_object_save_bit_17_swizzle(obj);
1630 if (obj->madv == I915_MADV_DONTNEED)
1633 for_each_sg_page(obj->pages->sgl, &sg_iter, obj->pages->nents, 0) {
1634 struct page *page = sg_page_iter_page(&sg_iter);
1637 set_page_dirty(page);
1639 if (obj->madv == I915_MADV_WILLNEED)
1640 mark_page_accessed(page);
1642 page_cache_release(page);
1646 sg_free_table(obj->pages);
1651 i915_gem_object_put_pages(struct drm_i915_gem_object *obj)
1653 const struct drm_i915_gem_object_ops *ops = obj->ops;
1655 if (obj->pages == NULL)
1658 BUG_ON(i915_gem_obj_ggtt_bound(obj));
1660 if (obj->pages_pin_count)
1663 /* ->put_pages might need to allocate memory for the bit17 swizzle
1664 * array, hence protect them from being reaped by removing them from gtt
1666 list_del(&obj->global_list);
1668 ops->put_pages(obj);
1671 if (i915_gem_object_is_purgeable(obj))
1672 i915_gem_object_truncate(obj);
1678 __i915_gem_shrink(struct drm_i915_private *dev_priv, long target,
1679 bool purgeable_only)
1681 struct drm_i915_gem_object *obj, *next;
1682 struct i915_address_space *vm = &dev_priv->gtt.base;
1685 list_for_each_entry_safe(obj, next,
1686 &dev_priv->mm.unbound_list,
1688 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
1689 i915_gem_object_put_pages(obj) == 0) {
1690 count += obj->base.size >> PAGE_SHIFT;
1691 if (count >= target)
1696 list_for_each_entry_safe(obj, next, &vm->inactive_list, mm_list) {
1697 if ((i915_gem_object_is_purgeable(obj) || !purgeable_only) &&
1698 i915_gem_object_unbind(obj) == 0 &&
1699 i915_gem_object_put_pages(obj) == 0) {
1700 count += obj->base.size >> PAGE_SHIFT;
1701 if (count >= target)
1710 i915_gem_purge(struct drm_i915_private *dev_priv, long target)
1712 return __i915_gem_shrink(dev_priv, target, true);
1716 i915_gem_shrink_all(struct drm_i915_private *dev_priv)
1718 struct drm_i915_gem_object *obj, *next;
1720 i915_gem_evict_everything(dev_priv->dev);
1722 list_for_each_entry_safe(obj, next, &dev_priv->mm.unbound_list,
1724 i915_gem_object_put_pages(obj);
1728 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj)
1730 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1732 struct address_space *mapping;
1733 struct sg_table *st;
1734 struct scatterlist *sg;
1735 struct sg_page_iter sg_iter;
1737 unsigned long last_pfn = 0; /* suppress gcc warning */
1740 /* Assert that the object is not currently in any GPU domain. As it
1741 * wasn't in the GTT, there shouldn't be any way it could have been in
1744 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
1745 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
1747 st = kmalloc(sizeof(*st), GFP_KERNEL);
1751 page_count = obj->base.size / PAGE_SIZE;
1752 if (sg_alloc_table(st, page_count, GFP_KERNEL)) {
1758 /* Get the list of pages out of our struct file. They'll be pinned
1759 * at this point until we release them.
1761 * Fail silently without starting the shrinker
1763 mapping = file_inode(obj->base.filp)->i_mapping;
1764 gfp = mapping_gfp_mask(mapping);
1765 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1766 gfp &= ~(__GFP_IO | __GFP_WAIT);
1769 for (i = 0; i < page_count; i++) {
1770 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1772 i915_gem_purge(dev_priv, page_count);
1773 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1776 /* We've tried hard to allocate the memory by reaping
1777 * our own buffer, now let the real VM do its job and
1778 * go down in flames if truly OOM.
1780 gfp &= ~(__GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD);
1781 gfp |= __GFP_IO | __GFP_WAIT;
1783 i915_gem_shrink_all(dev_priv);
1784 page = shmem_read_mapping_page_gfp(mapping, i, gfp);
1788 gfp |= __GFP_NORETRY | __GFP_NOWARN | __GFP_NO_KSWAPD;
1789 gfp &= ~(__GFP_IO | __GFP_WAIT);
1791 #ifdef CONFIG_SWIOTLB
1792 if (swiotlb_nr_tbl()) {
1794 sg_set_page(sg, page, PAGE_SIZE, 0);
1799 if (!i || page_to_pfn(page) != last_pfn + 1) {
1803 sg_set_page(sg, page, PAGE_SIZE, 0);
1805 sg->length += PAGE_SIZE;
1807 last_pfn = page_to_pfn(page);
1809 #ifdef CONFIG_SWIOTLB
1810 if (!swiotlb_nr_tbl())
1815 if (i915_gem_object_needs_bit17_swizzle(obj))
1816 i915_gem_object_do_bit_17_swizzle(obj);
1822 for_each_sg_page(st->sgl, &sg_iter, st->nents, 0)
1823 page_cache_release(sg_page_iter_page(&sg_iter));
1826 return PTR_ERR(page);
1829 /* Ensure that the associated pages are gathered from the backing storage
1830 * and pinned into our object. i915_gem_object_get_pages() may be called
1831 * multiple times before they are released by a single call to
1832 * i915_gem_object_put_pages() - once the pages are no longer referenced
1833 * either as a result of memory pressure (reaping pages under the shrinker)
1834 * or as the object is itself released.
1837 i915_gem_object_get_pages(struct drm_i915_gem_object *obj)
1839 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
1840 const struct drm_i915_gem_object_ops *ops = obj->ops;
1846 if (obj->madv != I915_MADV_WILLNEED) {
1847 DRM_ERROR("Attempting to obtain a purgeable object\n");
1851 BUG_ON(obj->pages_pin_count);
1853 ret = ops->get_pages(obj);
1857 list_add_tail(&obj->global_list, &dev_priv->mm.unbound_list);
1862 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1863 struct intel_ring_buffer *ring)
1865 struct drm_device *dev = obj->base.dev;
1866 struct drm_i915_private *dev_priv = dev->dev_private;
1867 struct i915_address_space *vm = &dev_priv->gtt.base;
1868 u32 seqno = intel_ring_get_seqno(ring);
1870 BUG_ON(ring == NULL);
1871 if (obj->ring != ring && obj->last_write_seqno) {
1872 /* Keep the seqno relative to the current ring */
1873 obj->last_write_seqno = seqno;
1877 /* Add a reference if we're newly entering the active list. */
1879 drm_gem_object_reference(&obj->base);
1883 /* Move from whatever list we were on to the tail of execution. */
1884 list_move_tail(&obj->mm_list, &vm->active_list);
1885 list_move_tail(&obj->ring_list, &ring->active_list);
1887 obj->last_read_seqno = seqno;
1889 if (obj->fenced_gpu_access) {
1890 obj->last_fenced_seqno = seqno;
1892 /* Bump MRU to take account of the delayed flush */
1893 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1894 struct drm_i915_fence_reg *reg;
1896 reg = &dev_priv->fence_regs[obj->fence_reg];
1897 list_move_tail(®->lru_list,
1898 &dev_priv->mm.fence_list);
1904 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1906 struct drm_device *dev = obj->base.dev;
1907 struct drm_i915_private *dev_priv = dev->dev_private;
1908 struct i915_address_space *vm = &dev_priv->gtt.base;
1910 BUG_ON(obj->base.write_domain & ~I915_GEM_GPU_DOMAINS);
1911 BUG_ON(!obj->active);
1913 list_move_tail(&obj->mm_list, &vm->inactive_list);
1915 list_del_init(&obj->ring_list);
1918 obj->last_read_seqno = 0;
1919 obj->last_write_seqno = 0;
1920 obj->base.write_domain = 0;
1922 obj->last_fenced_seqno = 0;
1923 obj->fenced_gpu_access = false;
1926 drm_gem_object_unreference(&obj->base);
1928 WARN_ON(i915_verify_lists(dev));
1932 i915_gem_init_seqno(struct drm_device *dev, u32 seqno)
1934 struct drm_i915_private *dev_priv = dev->dev_private;
1935 struct intel_ring_buffer *ring;
1938 /* Carefully retire all requests without writing to the rings */
1939 for_each_ring(ring, dev_priv, i) {
1940 ret = intel_ring_idle(ring);
1944 i915_gem_retire_requests(dev);
1946 /* Finally reset hw state */
1947 for_each_ring(ring, dev_priv, i) {
1948 intel_ring_init_seqno(ring, seqno);
1950 for (j = 0; j < ARRAY_SIZE(ring->sync_seqno); j++)
1951 ring->sync_seqno[j] = 0;
1957 int i915_gem_set_seqno(struct drm_device *dev, u32 seqno)
1959 struct drm_i915_private *dev_priv = dev->dev_private;
1965 /* HWS page needs to be set less than what we
1966 * will inject to ring
1968 ret = i915_gem_init_seqno(dev, seqno - 1);
1972 /* Carefully set the last_seqno value so that wrap
1973 * detection still works
1975 dev_priv->next_seqno = seqno;
1976 dev_priv->last_seqno = seqno - 1;
1977 if (dev_priv->last_seqno == 0)
1978 dev_priv->last_seqno--;
1984 i915_gem_get_seqno(struct drm_device *dev, u32 *seqno)
1986 struct drm_i915_private *dev_priv = dev->dev_private;
1988 /* reserve 0 for non-seqno */
1989 if (dev_priv->next_seqno == 0) {
1990 int ret = i915_gem_init_seqno(dev, 0);
1994 dev_priv->next_seqno = 1;
1997 *seqno = dev_priv->last_seqno = dev_priv->next_seqno++;
2001 int __i915_add_request(struct intel_ring_buffer *ring,
2002 struct drm_file *file,
2003 struct drm_i915_gem_object *obj,
2006 drm_i915_private_t *dev_priv = ring->dev->dev_private;
2007 struct drm_i915_gem_request *request;
2008 u32 request_ring_position, request_start;
2012 request_start = intel_ring_get_tail(ring);
2014 * Emit any outstanding flushes - execbuf can fail to emit the flush
2015 * after having emitted the batchbuffer command. Hence we need to fix
2016 * things up similar to emitting the lazy request. The difference here
2017 * is that the flush _must_ happen before the next request, no matter
2020 ret = intel_ring_flush_all_caches(ring);
2024 request = kmalloc(sizeof(*request), GFP_KERNEL);
2025 if (request == NULL)
2029 /* Record the position of the start of the request so that
2030 * should we detect the updated seqno part-way through the
2031 * GPU processing the request, we never over-estimate the
2032 * position of the head.
2034 request_ring_position = intel_ring_get_tail(ring);
2036 ret = ring->add_request(ring);
2042 request->seqno = intel_ring_get_seqno(ring);
2043 request->ring = ring;
2044 request->head = request_start;
2045 request->tail = request_ring_position;
2046 request->ctx = ring->last_context;
2047 request->batch_obj = obj;
2049 /* Whilst this request exists, batch_obj will be on the
2050 * active_list, and so will hold the active reference. Only when this
2051 * request is retired will the the batch_obj be moved onto the
2052 * inactive_list and lose its active reference. Hence we do not need
2053 * to explicitly hold another reference here.
2057 i915_gem_context_reference(request->ctx);
2059 request->emitted_jiffies = jiffies;
2060 was_empty = list_empty(&ring->request_list);
2061 list_add_tail(&request->list, &ring->request_list);
2062 request->file_priv = NULL;
2065 struct drm_i915_file_private *file_priv = file->driver_priv;
2067 spin_lock(&file_priv->mm.lock);
2068 request->file_priv = file_priv;
2069 list_add_tail(&request->client_list,
2070 &file_priv->mm.request_list);
2071 spin_unlock(&file_priv->mm.lock);
2074 trace_i915_gem_request_add(ring, request->seqno);
2075 ring->outstanding_lazy_request = 0;
2077 if (!dev_priv->ums.mm_suspended) {
2078 i915_queue_hangcheck(ring->dev);
2081 queue_delayed_work(dev_priv->wq,
2082 &dev_priv->mm.retire_work,
2083 round_jiffies_up_relative(HZ));
2084 intel_mark_busy(dev_priv->dev);
2089 *out_seqno = request->seqno;
2094 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
2096 struct drm_i915_file_private *file_priv = request->file_priv;
2101 spin_lock(&file_priv->mm.lock);
2102 if (request->file_priv) {
2103 list_del(&request->client_list);
2104 request->file_priv = NULL;
2106 spin_unlock(&file_priv->mm.lock);
2109 static bool i915_head_inside_object(u32 acthd, struct drm_i915_gem_object *obj)
2111 if (acthd >= i915_gem_obj_ggtt_offset(obj) &&
2112 acthd < i915_gem_obj_ggtt_offset(obj) + obj->base.size)
2118 static bool i915_head_inside_request(const u32 acthd_unmasked,
2119 const u32 request_start,
2120 const u32 request_end)
2122 const u32 acthd = acthd_unmasked & HEAD_ADDR;
2124 if (request_start < request_end) {
2125 if (acthd >= request_start && acthd < request_end)
2127 } else if (request_start > request_end) {
2128 if (acthd >= request_start || acthd < request_end)
2135 static bool i915_request_guilty(struct drm_i915_gem_request *request,
2136 const u32 acthd, bool *inside)
2138 /* There is a possibility that unmasked head address
2139 * pointing inside the ring, matches the batch_obj address range.
2140 * However this is extremely unlikely.
2143 if (request->batch_obj) {
2144 if (i915_head_inside_object(acthd, request->batch_obj)) {
2150 if (i915_head_inside_request(acthd, request->head, request->tail)) {
2158 static void i915_set_reset_status(struct intel_ring_buffer *ring,
2159 struct drm_i915_gem_request *request,
2162 struct i915_ctx_hang_stats *hs = NULL;
2163 bool inside, guilty;
2165 /* Innocent until proven guilty */
2168 if (ring->hangcheck.action != wait &&
2169 i915_request_guilty(request, acthd, &inside)) {
2170 DRM_ERROR("%s hung %s bo (0x%lx ctx %d) at 0x%x\n",
2172 inside ? "inside" : "flushing",
2173 request->batch_obj ?
2174 i915_gem_obj_ggtt_offset(request->batch_obj) : 0,
2175 request->ctx ? request->ctx->id : 0,
2181 /* If contexts are disabled or this is the default context, use
2182 * file_priv->reset_state
2184 if (request->ctx && request->ctx->id != DEFAULT_CONTEXT_ID)
2185 hs = &request->ctx->hang_stats;
2186 else if (request->file_priv)
2187 hs = &request->file_priv->hang_stats;
2193 hs->batch_pending++;
2197 static void i915_gem_free_request(struct drm_i915_gem_request *request)
2199 list_del(&request->list);
2200 i915_gem_request_remove_from_client(request);
2203 i915_gem_context_unreference(request->ctx);
2208 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
2209 struct intel_ring_buffer *ring)
2211 u32 completed_seqno;
2214 acthd = intel_ring_get_active_head(ring);
2215 completed_seqno = ring->get_seqno(ring, false);
2217 while (!list_empty(&ring->request_list)) {
2218 struct drm_i915_gem_request *request;
2220 request = list_first_entry(&ring->request_list,
2221 struct drm_i915_gem_request,
2224 if (request->seqno > completed_seqno)
2225 i915_set_reset_status(ring, request, acthd);
2227 i915_gem_free_request(request);
2230 while (!list_empty(&ring->active_list)) {
2231 struct drm_i915_gem_object *obj;
2233 obj = list_first_entry(&ring->active_list,
2234 struct drm_i915_gem_object,
2237 i915_gem_object_move_to_inactive(obj);
2241 void i915_gem_restore_fences(struct drm_device *dev)
2243 struct drm_i915_private *dev_priv = dev->dev_private;
2246 for (i = 0; i < dev_priv->num_fence_regs; i++) {
2247 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
2250 * Commit delayed tiling changes if we have an object still
2251 * attached to the fence, otherwise just clear the fence.
2254 i915_gem_object_update_fence(reg->obj, reg,
2255 reg->obj->tiling_mode);
2257 i915_gem_write_fence(dev, i, NULL);
2262 void i915_gem_reset(struct drm_device *dev)
2264 struct drm_i915_private *dev_priv = dev->dev_private;
2265 struct i915_address_space *vm = &dev_priv->gtt.base;
2266 struct drm_i915_gem_object *obj;
2267 struct intel_ring_buffer *ring;
2270 for_each_ring(ring, dev_priv, i)
2271 i915_gem_reset_ring_lists(dev_priv, ring);
2273 /* Move everything out of the GPU domains to ensure we do any
2274 * necessary invalidation upon reuse.
2276 list_for_each_entry(obj, &vm->inactive_list, mm_list)
2277 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
2279 i915_gem_restore_fences(dev);
2283 * This function clears the request list as sequence numbers are passed.
2286 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
2290 if (list_empty(&ring->request_list))
2293 WARN_ON(i915_verify_lists(ring->dev));
2295 seqno = ring->get_seqno(ring, true);
2297 while (!list_empty(&ring->request_list)) {
2298 struct drm_i915_gem_request *request;
2300 request = list_first_entry(&ring->request_list,
2301 struct drm_i915_gem_request,
2304 if (!i915_seqno_passed(seqno, request->seqno))
2307 trace_i915_gem_request_retire(ring, request->seqno);
2308 /* We know the GPU must have read the request to have
2309 * sent us the seqno + interrupt, so use the position
2310 * of tail of the request to update the last known position
2313 ring->last_retired_head = request->tail;
2315 i915_gem_free_request(request);
2318 /* Move any buffers on the active list that are no longer referenced
2319 * by the ringbuffer to the flushing/inactive lists as appropriate.
2321 while (!list_empty(&ring->active_list)) {
2322 struct drm_i915_gem_object *obj;
2324 obj = list_first_entry(&ring->active_list,
2325 struct drm_i915_gem_object,
2328 if (!i915_seqno_passed(seqno, obj->last_read_seqno))
2331 i915_gem_object_move_to_inactive(obj);
2334 if (unlikely(ring->trace_irq_seqno &&
2335 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
2336 ring->irq_put(ring);
2337 ring->trace_irq_seqno = 0;
2340 WARN_ON(i915_verify_lists(ring->dev));
2344 i915_gem_retire_requests(struct drm_device *dev)
2346 drm_i915_private_t *dev_priv = dev->dev_private;
2347 struct intel_ring_buffer *ring;
2350 for_each_ring(ring, dev_priv, i)
2351 i915_gem_retire_requests_ring(ring);
2355 i915_gem_retire_work_handler(struct work_struct *work)
2357 drm_i915_private_t *dev_priv;
2358 struct drm_device *dev;
2359 struct intel_ring_buffer *ring;
2363 dev_priv = container_of(work, drm_i915_private_t,
2364 mm.retire_work.work);
2365 dev = dev_priv->dev;
2367 /* Come back later if the device is busy... */
2368 if (!mutex_trylock(&dev->struct_mutex)) {
2369 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2370 round_jiffies_up_relative(HZ));
2374 i915_gem_retire_requests(dev);
2376 /* Send a periodic flush down the ring so we don't hold onto GEM
2377 * objects indefinitely.
2380 for_each_ring(ring, dev_priv, i) {
2381 if (ring->gpu_caches_dirty)
2382 i915_add_request(ring, NULL);
2384 idle &= list_empty(&ring->request_list);
2387 if (!dev_priv->ums.mm_suspended && !idle)
2388 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work,
2389 round_jiffies_up_relative(HZ));
2391 intel_mark_idle(dev);
2393 mutex_unlock(&dev->struct_mutex);
2397 * Ensures that an object will eventually get non-busy by flushing any required
2398 * write domains, emitting any outstanding lazy request and retiring and
2399 * completed requests.
2402 i915_gem_object_flush_active(struct drm_i915_gem_object *obj)
2407 ret = i915_gem_check_olr(obj->ring, obj->last_read_seqno);
2411 i915_gem_retire_requests_ring(obj->ring);
2418 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2419 * @DRM_IOCTL_ARGS: standard ioctl arguments
2421 * Returns 0 if successful, else an error is returned with the remaining time in
2422 * the timeout parameter.
2423 * -ETIME: object is still busy after timeout
2424 * -ERESTARTSYS: signal interrupted the wait
2425 * -ENONENT: object doesn't exist
2426 * Also possible, but rare:
2427 * -EAGAIN: GPU wedged
2429 * -ENODEV: Internal IRQ fail
2430 * -E?: The add request failed
2432 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2433 * non-zero timeout parameter the wait ioctl will wait for the given number of
2434 * nanoseconds on an object becoming unbusy. Since the wait itself does so
2435 * without holding struct_mutex the object may become re-busied before this
2436 * function completes. A similar but shorter * race condition exists in the busy
2440 i915_gem_wait_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
2442 drm_i915_private_t *dev_priv = dev->dev_private;
2443 struct drm_i915_gem_wait *args = data;
2444 struct drm_i915_gem_object *obj;
2445 struct intel_ring_buffer *ring = NULL;
2446 struct timespec timeout_stack, *timeout = NULL;
2447 unsigned reset_counter;
2451 if (args->timeout_ns >= 0) {
2452 timeout_stack = ns_to_timespec(args->timeout_ns);
2453 timeout = &timeout_stack;
2456 ret = i915_mutex_lock_interruptible(dev);
2460 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->bo_handle));
2461 if (&obj->base == NULL) {
2462 mutex_unlock(&dev->struct_mutex);
2466 /* Need to make sure the object gets inactive eventually. */
2467 ret = i915_gem_object_flush_active(obj);
2472 seqno = obj->last_read_seqno;
2479 /* Do this after OLR check to make sure we make forward progress polling
2480 * on this IOCTL with a 0 timeout (like busy ioctl)
2482 if (!args->timeout_ns) {
2487 drm_gem_object_unreference(&obj->base);
2488 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
2489 mutex_unlock(&dev->struct_mutex);
2491 ret = __wait_seqno(ring, seqno, reset_counter, true, timeout);
2493 args->timeout_ns = timespec_to_ns(timeout);
2497 drm_gem_object_unreference(&obj->base);
2498 mutex_unlock(&dev->struct_mutex);
2503 * i915_gem_object_sync - sync an object to a ring.
2505 * @obj: object which may be in use on another ring.
2506 * @to: ring we wish to use the object on. May be NULL.
2508 * This code is meant to abstract object synchronization with the GPU.
2509 * Calling with NULL implies synchronizing the object with the CPU
2510 * rather than a particular GPU ring.
2512 * Returns 0 if successful, else propagates up the lower layer error.
2515 i915_gem_object_sync(struct drm_i915_gem_object *obj,
2516 struct intel_ring_buffer *to)
2518 struct intel_ring_buffer *from = obj->ring;
2522 if (from == NULL || to == from)
2525 if (to == NULL || !i915_semaphore_is_enabled(obj->base.dev))
2526 return i915_gem_object_wait_rendering(obj, false);
2528 idx = intel_ring_sync_index(from, to);
2530 seqno = obj->last_read_seqno;
2531 if (seqno <= from->sync_seqno[idx])
2534 ret = i915_gem_check_olr(obj->ring, seqno);
2538 ret = to->sync_to(to, from, seqno);
2540 /* We use last_read_seqno because sync_to()
2541 * might have just caused seqno wrap under
2544 from->sync_seqno[idx] = obj->last_read_seqno;
2549 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
2551 u32 old_write_domain, old_read_domains;
2553 /* Force a pagefault for domain tracking on next user access */
2554 i915_gem_release_mmap(obj);
2556 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
2559 /* Wait for any direct GTT access to complete */
2562 old_read_domains = obj->base.read_domains;
2563 old_write_domain = obj->base.write_domain;
2565 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
2566 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
2568 trace_i915_gem_object_change_domain(obj,
2574 * Unbinds an object from the GTT aperture.
2577 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
2579 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
2580 struct i915_vma *vma;
2583 if (!i915_gem_obj_ggtt_bound(obj))
2589 BUG_ON(obj->pages == NULL);
2591 ret = i915_gem_object_finish_gpu(obj);
2594 /* Continue on if we fail due to EIO, the GPU is hung so we
2595 * should be safe and we need to cleanup or else we might
2596 * cause memory corruption through use-after-free.
2599 i915_gem_object_finish_gtt(obj);
2601 /* release the fence reg _after_ flushing */
2602 ret = i915_gem_object_put_fence(obj);
2606 trace_i915_gem_object_unbind(obj);
2608 if (obj->has_global_gtt_mapping)
2609 i915_gem_gtt_unbind_object(obj);
2610 if (obj->has_aliasing_ppgtt_mapping) {
2611 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2612 obj->has_aliasing_ppgtt_mapping = 0;
2614 i915_gem_gtt_finish_object(obj);
2615 i915_gem_object_unpin_pages(obj);
2617 list_del(&obj->mm_list);
2618 /* Avoid an unnecessary call to unbind on rebind. */
2619 obj->map_and_fenceable = true;
2621 vma = __i915_gem_obj_to_vma(obj);
2622 list_del(&vma->vma_link);
2623 drm_mm_remove_node(&vma->node);
2624 i915_gem_vma_destroy(vma);
2626 /* Since the unbound list is global, only move to that list if
2627 * no more VMAs exist.
2628 * NB: Until we have real VMAs there will only ever be one */
2629 WARN_ON(!list_empty(&obj->vma_list));
2630 if (list_empty(&obj->vma_list))
2631 list_move_tail(&obj->global_list, &dev_priv->mm.unbound_list);
2636 int i915_gpu_idle(struct drm_device *dev)
2638 drm_i915_private_t *dev_priv = dev->dev_private;
2639 struct intel_ring_buffer *ring;
2642 /* Flush everything onto the inactive list. */
2643 for_each_ring(ring, dev_priv, i) {
2644 ret = i915_switch_context(ring, NULL, DEFAULT_CONTEXT_ID);
2648 ret = intel_ring_idle(ring);
2656 static void i965_write_fence_reg(struct drm_device *dev, int reg,
2657 struct drm_i915_gem_object *obj)
2659 drm_i915_private_t *dev_priv = dev->dev_private;
2661 int fence_pitch_shift;
2663 if (INTEL_INFO(dev)->gen >= 6) {
2664 fence_reg = FENCE_REG_SANDYBRIDGE_0;
2665 fence_pitch_shift = SANDYBRIDGE_FENCE_PITCH_SHIFT;
2667 fence_reg = FENCE_REG_965_0;
2668 fence_pitch_shift = I965_FENCE_PITCH_SHIFT;
2671 fence_reg += reg * 8;
2673 /* To w/a incoherency with non-atomic 64-bit register updates,
2674 * we split the 64-bit update into two 32-bit writes. In order
2675 * for a partial fence not to be evaluated between writes, we
2676 * precede the update with write to turn off the fence register,
2677 * and only enable the fence as the last step.
2679 * For extra levels of paranoia, we make sure each step lands
2680 * before applying the next step.
2682 I915_WRITE(fence_reg, 0);
2683 POSTING_READ(fence_reg);
2686 u32 size = i915_gem_obj_ggtt_size(obj);
2689 val = (uint64_t)((i915_gem_obj_ggtt_offset(obj) + size - 4096) &
2691 val |= i915_gem_obj_ggtt_offset(obj) & 0xfffff000;
2692 val |= (uint64_t)((obj->stride / 128) - 1) << fence_pitch_shift;
2693 if (obj->tiling_mode == I915_TILING_Y)
2694 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2695 val |= I965_FENCE_REG_VALID;
2697 I915_WRITE(fence_reg + 4, val >> 32);
2698 POSTING_READ(fence_reg + 4);
2700 I915_WRITE(fence_reg + 0, val);
2701 POSTING_READ(fence_reg);
2703 I915_WRITE(fence_reg + 4, 0);
2704 POSTING_READ(fence_reg + 4);
2708 static void i915_write_fence_reg(struct drm_device *dev, int reg,
2709 struct drm_i915_gem_object *obj)
2711 drm_i915_private_t *dev_priv = dev->dev_private;
2715 u32 size = i915_gem_obj_ggtt_size(obj);
2719 WARN((i915_gem_obj_ggtt_offset(obj) & ~I915_FENCE_START_MASK) ||
2720 (size & -size) != size ||
2721 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2722 "object 0x%08lx [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2723 i915_gem_obj_ggtt_offset(obj), obj->map_and_fenceable, size);
2725 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2730 /* Note: pitch better be a power of two tile widths */
2731 pitch_val = obj->stride / tile_width;
2732 pitch_val = ffs(pitch_val) - 1;
2734 val = i915_gem_obj_ggtt_offset(obj);
2735 if (obj->tiling_mode == I915_TILING_Y)
2736 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2737 val |= I915_FENCE_SIZE_BITS(size);
2738 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2739 val |= I830_FENCE_REG_VALID;
2744 reg = FENCE_REG_830_0 + reg * 4;
2746 reg = FENCE_REG_945_8 + (reg - 8) * 4;
2748 I915_WRITE(reg, val);
2752 static void i830_write_fence_reg(struct drm_device *dev, int reg,
2753 struct drm_i915_gem_object *obj)
2755 drm_i915_private_t *dev_priv = dev->dev_private;
2759 u32 size = i915_gem_obj_ggtt_size(obj);
2762 WARN((i915_gem_obj_ggtt_offset(obj) & ~I830_FENCE_START_MASK) ||
2763 (size & -size) != size ||
2764 (i915_gem_obj_ggtt_offset(obj) & (size - 1)),
2765 "object 0x%08lx not 512K or pot-size 0x%08x aligned\n",
2766 i915_gem_obj_ggtt_offset(obj), size);
2768 pitch_val = obj->stride / 128;
2769 pitch_val = ffs(pitch_val) - 1;
2771 val = i915_gem_obj_ggtt_offset(obj);
2772 if (obj->tiling_mode == I915_TILING_Y)
2773 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2774 val |= I830_FENCE_SIZE_BITS(size);
2775 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2776 val |= I830_FENCE_REG_VALID;
2780 I915_WRITE(FENCE_REG_830_0 + reg * 4, val);
2781 POSTING_READ(FENCE_REG_830_0 + reg * 4);
2784 inline static bool i915_gem_object_needs_mb(struct drm_i915_gem_object *obj)
2786 return obj && obj->base.read_domains & I915_GEM_DOMAIN_GTT;
2789 static void i915_gem_write_fence(struct drm_device *dev, int reg,
2790 struct drm_i915_gem_object *obj)
2792 struct drm_i915_private *dev_priv = dev->dev_private;
2794 /* Ensure that all CPU reads are completed before installing a fence
2795 * and all writes before removing the fence.
2797 if (i915_gem_object_needs_mb(dev_priv->fence_regs[reg].obj))
2800 WARN(obj && (!obj->stride || !obj->tiling_mode),
2801 "bogus fence setup with stride: 0x%x, tiling mode: %i\n",
2802 obj->stride, obj->tiling_mode);
2804 switch (INTEL_INFO(dev)->gen) {
2808 case 4: i965_write_fence_reg(dev, reg, obj); break;
2809 case 3: i915_write_fence_reg(dev, reg, obj); break;
2810 case 2: i830_write_fence_reg(dev, reg, obj); break;
2814 /* And similarly be paranoid that no direct access to this region
2815 * is reordered to before the fence is installed.
2817 if (i915_gem_object_needs_mb(obj))
2821 static inline int fence_number(struct drm_i915_private *dev_priv,
2822 struct drm_i915_fence_reg *fence)
2824 return fence - dev_priv->fence_regs;
2827 static void i915_gem_object_update_fence(struct drm_i915_gem_object *obj,
2828 struct drm_i915_fence_reg *fence,
2831 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2832 int reg = fence_number(dev_priv, fence);
2834 i915_gem_write_fence(obj->base.dev, reg, enable ? obj : NULL);
2837 obj->fence_reg = reg;
2839 list_move_tail(&fence->lru_list, &dev_priv->mm.fence_list);
2841 obj->fence_reg = I915_FENCE_REG_NONE;
2843 list_del_init(&fence->lru_list);
2845 obj->fence_dirty = false;
2849 i915_gem_object_wait_fence(struct drm_i915_gem_object *obj)
2851 if (obj->last_fenced_seqno) {
2852 int ret = i915_wait_seqno(obj->ring, obj->last_fenced_seqno);
2856 obj->last_fenced_seqno = 0;
2859 obj->fenced_gpu_access = false;
2864 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2866 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2867 struct drm_i915_fence_reg *fence;
2870 ret = i915_gem_object_wait_fence(obj);
2874 if (obj->fence_reg == I915_FENCE_REG_NONE)
2877 fence = &dev_priv->fence_regs[obj->fence_reg];
2879 i915_gem_object_fence_lost(obj);
2880 i915_gem_object_update_fence(obj, fence, false);
2885 static struct drm_i915_fence_reg *
2886 i915_find_fence_reg(struct drm_device *dev)
2888 struct drm_i915_private *dev_priv = dev->dev_private;
2889 struct drm_i915_fence_reg *reg, *avail;
2892 /* First try to find a free reg */
2894 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2895 reg = &dev_priv->fence_regs[i];
2899 if (!reg->pin_count)
2906 /* None available, try to steal one or wait for a user to finish */
2907 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2918 * i915_gem_object_get_fence - set up fencing for an object
2919 * @obj: object to map through a fence reg
2921 * When mapping objects through the GTT, userspace wants to be able to write
2922 * to them without having to worry about swizzling if the object is tiled.
2923 * This function walks the fence regs looking for a free one for @obj,
2924 * stealing one if it can't find any.
2926 * It then sets up the reg based on the object's properties: address, pitch
2927 * and tiling format.
2929 * For an untiled surface, this removes any existing fence.
2932 i915_gem_object_get_fence(struct drm_i915_gem_object *obj)
2934 struct drm_device *dev = obj->base.dev;
2935 struct drm_i915_private *dev_priv = dev->dev_private;
2936 bool enable = obj->tiling_mode != I915_TILING_NONE;
2937 struct drm_i915_fence_reg *reg;
2940 /* Have we updated the tiling parameters upon the object and so
2941 * will need to serialise the write to the associated fence register?
2943 if (obj->fence_dirty) {
2944 ret = i915_gem_object_wait_fence(obj);
2949 /* Just update our place in the LRU if our fence is getting reused. */
2950 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2951 reg = &dev_priv->fence_regs[obj->fence_reg];
2952 if (!obj->fence_dirty) {
2953 list_move_tail(®->lru_list,
2954 &dev_priv->mm.fence_list);
2957 } else if (enable) {
2958 reg = i915_find_fence_reg(dev);
2963 struct drm_i915_gem_object *old = reg->obj;
2965 ret = i915_gem_object_wait_fence(old);
2969 i915_gem_object_fence_lost(old);
2974 i915_gem_object_update_fence(obj, reg, enable);
2979 static bool i915_gem_valid_gtt_space(struct drm_device *dev,
2980 struct drm_mm_node *gtt_space,
2981 unsigned long cache_level)
2983 struct drm_mm_node *other;
2985 /* On non-LLC machines we have to be careful when putting differing
2986 * types of snoopable memory together to avoid the prefetcher
2987 * crossing memory domains and dying.
2992 if (!drm_mm_node_allocated(gtt_space))
2995 if (list_empty(>t_space->node_list))
2998 other = list_entry(gtt_space->node_list.prev, struct drm_mm_node, node_list);
2999 if (other->allocated && !other->hole_follows && other->color != cache_level)
3002 other = list_entry(gtt_space->node_list.next, struct drm_mm_node, node_list);
3003 if (other->allocated && !gtt_space->hole_follows && other->color != cache_level)
3009 static void i915_gem_verify_gtt(struct drm_device *dev)
3012 struct drm_i915_private *dev_priv = dev->dev_private;
3013 struct drm_i915_gem_object *obj;
3016 list_for_each_entry(obj, &dev_priv->mm.gtt_list, global_list) {
3017 if (obj->gtt_space == NULL) {
3018 printk(KERN_ERR "object found on GTT list with no space reserved\n");
3023 if (obj->cache_level != obj->gtt_space->color) {
3024 printk(KERN_ERR "object reserved space [%08lx, %08lx] with wrong color, cache_level=%x, color=%lx\n",
3025 i915_gem_obj_ggtt_offset(obj),
3026 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
3028 obj->gtt_space->color);
3033 if (!i915_gem_valid_gtt_space(dev,
3035 obj->cache_level)) {
3036 printk(KERN_ERR "invalid GTT space found at [%08lx, %08lx] - color=%x\n",
3037 i915_gem_obj_ggtt_offset(obj),
3038 i915_gem_obj_ggtt_offset(obj) + i915_gem_obj_ggtt_size(obj),
3050 * Finds free space in the GTT aperture and binds the object there.
3053 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
3055 bool map_and_fenceable,
3058 struct drm_device *dev = obj->base.dev;
3059 drm_i915_private_t *dev_priv = dev->dev_private;
3060 struct i915_address_space *vm = &dev_priv->gtt.base;
3061 u32 size, fence_size, fence_alignment, unfenced_alignment;
3062 bool mappable, fenceable;
3063 size_t gtt_max = map_and_fenceable ?
3064 dev_priv->gtt.mappable_end : dev_priv->gtt.base.total;
3065 struct i915_vma *vma;
3068 if (WARN_ON(!list_empty(&obj->vma_list)))
3071 fence_size = i915_gem_get_gtt_size(dev,
3074 fence_alignment = i915_gem_get_gtt_alignment(dev,
3076 obj->tiling_mode, true);
3077 unfenced_alignment =
3078 i915_gem_get_gtt_alignment(dev,
3080 obj->tiling_mode, false);
3083 alignment = map_and_fenceable ? fence_alignment :
3085 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
3086 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
3090 size = map_and_fenceable ? fence_size : obj->base.size;
3092 /* If the object is bigger than the entire aperture, reject it early
3093 * before evicting everything in a vain attempt to find space.
3095 if (obj->base.size > gtt_max) {
3096 DRM_ERROR("Attempting to bind an object larger than the aperture: object=%zd > %s aperture=%zu\n",
3098 map_and_fenceable ? "mappable" : "total",
3103 ret = i915_gem_object_get_pages(obj);
3107 i915_gem_object_pin_pages(obj);
3109 vma = i915_gem_vma_create(obj, &dev_priv->gtt.base);
3116 ret = drm_mm_insert_node_in_range_generic(&dev_priv->gtt.base.mm,
3119 obj->cache_level, 0, gtt_max,
3120 DRM_MM_SEARCH_DEFAULT);
3122 ret = i915_gem_evict_something(dev, size, alignment,
3131 if (WARN_ON(!i915_gem_valid_gtt_space(dev, &vma->node,
3132 obj->cache_level))) {
3134 goto err_remove_node;
3137 ret = i915_gem_gtt_prepare_object(obj);
3139 goto err_remove_node;
3141 list_move_tail(&obj->global_list, &dev_priv->mm.bound_list);
3142 list_add_tail(&obj->mm_list, &vm->inactive_list);
3143 list_add(&vma->vma_link, &obj->vma_list);
3146 i915_gem_obj_ggtt_size(obj) == fence_size &&
3147 (i915_gem_obj_ggtt_offset(obj) & (fence_alignment - 1)) == 0;
3149 mappable = i915_gem_obj_ggtt_offset(obj) + obj->base.size <=
3150 dev_priv->gtt.mappable_end;
3152 obj->map_and_fenceable = mappable && fenceable;
3154 trace_i915_gem_object_bind(obj, map_and_fenceable);
3155 i915_gem_verify_gtt(dev);
3159 drm_mm_remove_node(&vma->node);
3161 i915_gem_vma_destroy(vma);
3163 i915_gem_object_unpin_pages(obj);
3168 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
3170 /* If we don't have a page list set up, then we're not pinned
3171 * to GPU, and we can ignore the cache flush because it'll happen
3172 * again at bind time.
3174 if (obj->pages == NULL)
3178 * Stolen memory is always coherent with the GPU as it is explicitly
3179 * marked as wc by the system, or the system is cache-coherent.
3184 /* If the GPU is snooping the contents of the CPU cache,
3185 * we do not need to manually clear the CPU cache lines. However,
3186 * the caches are only snooped when the render cache is
3187 * flushed/invalidated. As we always have to emit invalidations
3188 * and flushes when moving into and out of the RENDER domain, correct
3189 * snooping behaviour occurs naturally as the result of our domain
3192 if (obj->cache_level != I915_CACHE_NONE)
3195 trace_i915_gem_object_clflush(obj);
3197 drm_clflush_sg(obj->pages);
3200 /** Flushes the GTT write domain for the object if it's dirty. */
3202 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
3204 uint32_t old_write_domain;
3206 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
3209 /* No actual flushing is required for the GTT write domain. Writes
3210 * to it immediately go to main memory as far as we know, so there's
3211 * no chipset flush. It also doesn't land in render cache.
3213 * However, we do have to enforce the order so that all writes through
3214 * the GTT land before any writes to the device, such as updates to
3219 old_write_domain = obj->base.write_domain;
3220 obj->base.write_domain = 0;
3222 trace_i915_gem_object_change_domain(obj,
3223 obj->base.read_domains,
3227 /** Flushes the CPU write domain for the object if it's dirty. */
3229 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
3231 uint32_t old_write_domain;
3233 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
3236 i915_gem_clflush_object(obj);
3237 i915_gem_chipset_flush(obj->base.dev);
3238 old_write_domain = obj->base.write_domain;
3239 obj->base.write_domain = 0;
3241 trace_i915_gem_object_change_domain(obj,
3242 obj->base.read_domains,
3247 * Moves a single object to the GTT read, and possibly write domain.
3249 * This function returns when the move is complete, including waiting on
3253 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
3255 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
3256 uint32_t old_write_domain, old_read_domains;
3259 /* Not valid to be called on unbound objects. */
3260 if (!i915_gem_obj_ggtt_bound(obj))
3263 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
3266 ret = i915_gem_object_wait_rendering(obj, !write);
3270 i915_gem_object_flush_cpu_write_domain(obj);
3272 /* Serialise direct access to this object with the barriers for
3273 * coherent writes from the GPU, by effectively invalidating the
3274 * GTT domain upon first access.
3276 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
3279 old_write_domain = obj->base.write_domain;
3280 old_read_domains = obj->base.read_domains;
3282 /* It should now be out of any other write domains, and we can update
3283 * the domain values for our changes.
3285 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
3286 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3288 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
3289 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
3293 trace_i915_gem_object_change_domain(obj,
3297 /* And bump the LRU for this access */
3298 if (i915_gem_object_is_inactive(obj))
3299 list_move_tail(&obj->mm_list,
3300 &dev_priv->gtt.base.inactive_list);
3305 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
3306 enum i915_cache_level cache_level)
3308 struct drm_device *dev = obj->base.dev;
3309 drm_i915_private_t *dev_priv = dev->dev_private;
3310 struct i915_vma *vma = __i915_gem_obj_to_vma(obj);
3313 if (obj->cache_level == cache_level)
3316 if (obj->pin_count) {
3317 DRM_DEBUG("can not change the cache level of pinned objects\n");
3321 if (vma && !i915_gem_valid_gtt_space(dev, &vma->node, cache_level)) {
3322 ret = i915_gem_object_unbind(obj);
3327 if (i915_gem_obj_ggtt_bound(obj)) {
3328 ret = i915_gem_object_finish_gpu(obj);
3332 i915_gem_object_finish_gtt(obj);
3334 /* Before SandyBridge, you could not use tiling or fence
3335 * registers with snooped memory, so relinquish any fences
3336 * currently pointing to our region in the aperture.
3338 if (INTEL_INFO(dev)->gen < 6) {
3339 ret = i915_gem_object_put_fence(obj);
3344 if (obj->has_global_gtt_mapping)
3345 i915_gem_gtt_bind_object(obj, cache_level);
3346 if (obj->has_aliasing_ppgtt_mapping)
3347 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
3350 i915_gem_obj_ggtt_set_color(obj, cache_level);
3353 if (cache_level == I915_CACHE_NONE) {
3354 u32 old_read_domains, old_write_domain;
3356 /* If we're coming from LLC cached, then we haven't
3357 * actually been tracking whether the data is in the
3358 * CPU cache or not, since we only allow one bit set
3359 * in obj->write_domain and have been skipping the clflushes.
3360 * Just set it to the CPU cache for now.
3362 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
3363 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
3365 old_read_domains = obj->base.read_domains;
3366 old_write_domain = obj->base.write_domain;
3368 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3369 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3371 trace_i915_gem_object_change_domain(obj,
3376 obj->cache_level = cache_level;
3377 i915_gem_verify_gtt(dev);
3381 int i915_gem_get_caching_ioctl(struct drm_device *dev, void *data,
3382 struct drm_file *file)
3384 struct drm_i915_gem_caching *args = data;
3385 struct drm_i915_gem_object *obj;
3388 ret = i915_mutex_lock_interruptible(dev);
3392 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3393 if (&obj->base == NULL) {
3398 args->caching = obj->cache_level != I915_CACHE_NONE;
3400 drm_gem_object_unreference(&obj->base);
3402 mutex_unlock(&dev->struct_mutex);
3406 int i915_gem_set_caching_ioctl(struct drm_device *dev, void *data,
3407 struct drm_file *file)
3409 struct drm_i915_gem_caching *args = data;
3410 struct drm_i915_gem_object *obj;
3411 enum i915_cache_level level;
3414 switch (args->caching) {
3415 case I915_CACHING_NONE:
3416 level = I915_CACHE_NONE;
3418 case I915_CACHING_CACHED:
3419 level = I915_CACHE_LLC;
3425 ret = i915_mutex_lock_interruptible(dev);
3429 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3430 if (&obj->base == NULL) {
3435 ret = i915_gem_object_set_cache_level(obj, level);
3437 drm_gem_object_unreference(&obj->base);
3439 mutex_unlock(&dev->struct_mutex);
3444 * Prepare buffer for display plane (scanout, cursors, etc).
3445 * Can be called from an uninterruptible phase (modesetting) and allows
3446 * any flushes to be pipelined (for pageflips).
3449 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
3451 struct intel_ring_buffer *pipelined)
3453 u32 old_read_domains, old_write_domain;
3456 if (pipelined != obj->ring) {
3457 ret = i915_gem_object_sync(obj, pipelined);
3462 /* The display engine is not coherent with the LLC cache on gen6. As
3463 * a result, we make sure that the pinning that is about to occur is
3464 * done with uncached PTEs. This is lowest common denominator for all
3467 * However for gen6+, we could do better by using the GFDT bit instead
3468 * of uncaching, which would allow us to flush all the LLC-cached data
3469 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3471 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
3475 /* As the user may map the buffer once pinned in the display plane
3476 * (e.g. libkms for the bootup splash), we have to ensure that we
3477 * always use map_and_fenceable for all scanout buffers.
3479 ret = i915_gem_object_pin(obj, alignment, true, false);
3483 i915_gem_object_flush_cpu_write_domain(obj);
3485 old_write_domain = obj->base.write_domain;
3486 old_read_domains = obj->base.read_domains;
3488 /* It should now be out of any other write domains, and we can update
3489 * the domain values for our changes.
3491 obj->base.write_domain = 0;
3492 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
3494 trace_i915_gem_object_change_domain(obj,
3502 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
3506 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3509 ret = i915_gem_object_wait_rendering(obj, false);
3513 /* Ensure that we invalidate the GPU's caches and TLBs. */
3514 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3519 * Moves a single object to the CPU read, and possibly write domain.
3521 * This function returns when the move is complete, including waiting on
3525 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3527 uint32_t old_write_domain, old_read_domains;
3530 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3533 ret = i915_gem_object_wait_rendering(obj, !write);
3537 i915_gem_object_flush_gtt_write_domain(obj);
3539 old_write_domain = obj->base.write_domain;
3540 old_read_domains = obj->base.read_domains;
3542 /* Flush the CPU cache if it's still invalid. */
3543 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3544 i915_gem_clflush_object(obj);
3546 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3549 /* It should now be out of any other write domains, and we can update
3550 * the domain values for our changes.
3552 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3554 /* If we're writing through the CPU, then the GPU read domains will
3555 * need to be invalidated at next use.
3558 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3559 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3562 trace_i915_gem_object_change_domain(obj,
3569 /* Throttle our rendering by waiting until the ring has completed our requests
3570 * emitted over 20 msec ago.
3572 * Note that if we were to use the current jiffies each time around the loop,
3573 * we wouldn't escape the function with any frames outstanding if the time to
3574 * render a frame was over 20ms.
3576 * This should get us reasonable parallelism between CPU and GPU but also
3577 * relatively low latency when blocking on a particular request to finish.
3580 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3582 struct drm_i915_private *dev_priv = dev->dev_private;
3583 struct drm_i915_file_private *file_priv = file->driver_priv;
3584 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3585 struct drm_i915_gem_request *request;
3586 struct intel_ring_buffer *ring = NULL;
3587 unsigned reset_counter;
3591 ret = i915_gem_wait_for_error(&dev_priv->gpu_error);
3595 ret = i915_gem_check_wedge(&dev_priv->gpu_error, false);
3599 spin_lock(&file_priv->mm.lock);
3600 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3601 if (time_after_eq(request->emitted_jiffies, recent_enough))
3604 ring = request->ring;
3605 seqno = request->seqno;
3607 reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
3608 spin_unlock(&file_priv->mm.lock);
3613 ret = __wait_seqno(ring, seqno, reset_counter, true, NULL);
3615 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3621 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3623 bool map_and_fenceable,
3628 if (WARN_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT))
3631 if (i915_gem_obj_ggtt_bound(obj)) {
3632 if ((alignment && i915_gem_obj_ggtt_offset(obj) & (alignment - 1)) ||
3633 (map_and_fenceable && !obj->map_and_fenceable)) {
3634 WARN(obj->pin_count,
3635 "bo is already pinned with incorrect alignment:"
3636 " offset=%lx, req.alignment=%x, req.map_and_fenceable=%d,"
3637 " obj->map_and_fenceable=%d\n",
3638 i915_gem_obj_ggtt_offset(obj), alignment,
3640 obj->map_and_fenceable);
3641 ret = i915_gem_object_unbind(obj);
3647 if (!i915_gem_obj_ggtt_bound(obj)) {
3648 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3650 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3656 if (!dev_priv->mm.aliasing_ppgtt)
3657 i915_gem_gtt_bind_object(obj, obj->cache_level);
3660 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3661 i915_gem_gtt_bind_object(obj, obj->cache_level);
3664 obj->pin_mappable |= map_and_fenceable;
3670 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3672 BUG_ON(obj->pin_count == 0);
3673 BUG_ON(!i915_gem_obj_ggtt_bound(obj));
3675 if (--obj->pin_count == 0)
3676 obj->pin_mappable = false;
3680 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3681 struct drm_file *file)
3683 struct drm_i915_gem_pin *args = data;
3684 struct drm_i915_gem_object *obj;
3687 ret = i915_mutex_lock_interruptible(dev);
3691 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3692 if (&obj->base == NULL) {
3697 if (obj->madv != I915_MADV_WILLNEED) {
3698 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3703 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3704 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3710 if (obj->user_pin_count == 0) {
3711 ret = i915_gem_object_pin(obj, args->alignment, true, false);
3716 obj->user_pin_count++;
3717 obj->pin_filp = file;
3719 /* XXX - flush the CPU caches for pinned objects
3720 * as the X server doesn't manage domains yet
3722 i915_gem_object_flush_cpu_write_domain(obj);
3723 args->offset = i915_gem_obj_ggtt_offset(obj);
3725 drm_gem_object_unreference(&obj->base);
3727 mutex_unlock(&dev->struct_mutex);
3732 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3733 struct drm_file *file)
3735 struct drm_i915_gem_pin *args = data;
3736 struct drm_i915_gem_object *obj;
3739 ret = i915_mutex_lock_interruptible(dev);
3743 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3744 if (&obj->base == NULL) {
3749 if (obj->pin_filp != file) {
3750 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3755 obj->user_pin_count--;
3756 if (obj->user_pin_count == 0) {
3757 obj->pin_filp = NULL;
3758 i915_gem_object_unpin(obj);
3762 drm_gem_object_unreference(&obj->base);
3764 mutex_unlock(&dev->struct_mutex);
3769 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3770 struct drm_file *file)
3772 struct drm_i915_gem_busy *args = data;
3773 struct drm_i915_gem_object *obj;
3776 ret = i915_mutex_lock_interruptible(dev);
3780 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3781 if (&obj->base == NULL) {
3786 /* Count all active objects as busy, even if they are currently not used
3787 * by the gpu. Users of this interface expect objects to eventually
3788 * become non-busy without any further actions, therefore emit any
3789 * necessary flushes here.
3791 ret = i915_gem_object_flush_active(obj);
3793 args->busy = obj->active;
3795 BUILD_BUG_ON(I915_NUM_RINGS > 16);
3796 args->busy |= intel_ring_flag(obj->ring) << 16;
3799 drm_gem_object_unreference(&obj->base);
3801 mutex_unlock(&dev->struct_mutex);
3806 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3807 struct drm_file *file_priv)
3809 return i915_gem_ring_throttle(dev, file_priv);
3813 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3814 struct drm_file *file_priv)
3816 struct drm_i915_gem_madvise *args = data;
3817 struct drm_i915_gem_object *obj;
3820 switch (args->madv) {
3821 case I915_MADV_DONTNEED:
3822 case I915_MADV_WILLNEED:
3828 ret = i915_mutex_lock_interruptible(dev);
3832 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3833 if (&obj->base == NULL) {
3838 if (obj->pin_count) {
3843 if (obj->madv != __I915_MADV_PURGED)
3844 obj->madv = args->madv;
3846 /* if the object is no longer attached, discard its backing storage */
3847 if (i915_gem_object_is_purgeable(obj) && obj->pages == NULL)
3848 i915_gem_object_truncate(obj);
3850 args->retained = obj->madv != __I915_MADV_PURGED;
3853 drm_gem_object_unreference(&obj->base);
3855 mutex_unlock(&dev->struct_mutex);
3859 void i915_gem_object_init(struct drm_i915_gem_object *obj,
3860 const struct drm_i915_gem_object_ops *ops)
3862 INIT_LIST_HEAD(&obj->mm_list);
3863 INIT_LIST_HEAD(&obj->global_list);
3864 INIT_LIST_HEAD(&obj->ring_list);
3865 INIT_LIST_HEAD(&obj->exec_list);
3866 INIT_LIST_HEAD(&obj->vma_list);
3870 obj->fence_reg = I915_FENCE_REG_NONE;
3871 obj->madv = I915_MADV_WILLNEED;
3872 /* Avoid an unnecessary call to unbind on the first bind. */
3873 obj->map_and_fenceable = true;
3875 i915_gem_info_add_obj(obj->base.dev->dev_private, obj->base.size);
3878 static const struct drm_i915_gem_object_ops i915_gem_object_ops = {
3879 .get_pages = i915_gem_object_get_pages_gtt,
3880 .put_pages = i915_gem_object_put_pages_gtt,
3883 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3886 struct drm_i915_gem_object *obj;
3887 struct address_space *mapping;
3890 obj = i915_gem_object_alloc(dev);
3894 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3895 i915_gem_object_free(obj);
3899 mask = GFP_HIGHUSER | __GFP_RECLAIMABLE;
3900 if (IS_CRESTLINE(dev) || IS_BROADWATER(dev)) {
3901 /* 965gm cannot relocate objects above 4GiB. */
3902 mask &= ~__GFP_HIGHMEM;
3903 mask |= __GFP_DMA32;
3906 mapping = file_inode(obj->base.filp)->i_mapping;
3907 mapping_set_gfp_mask(mapping, mask);
3909 i915_gem_object_init(obj, &i915_gem_object_ops);
3911 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3912 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3915 /* On some devices, we can have the GPU use the LLC (the CPU
3916 * cache) for about a 10% performance improvement
3917 * compared to uncached. Graphics requests other than
3918 * display scanout are coherent with the CPU in
3919 * accessing this cache. This means in this mode we
3920 * don't need to clflush on the CPU side, and on the
3921 * GPU side we only need to flush internal caches to
3922 * get data visible to the CPU.
3924 * However, we maintain the display planes as UC, and so
3925 * need to rebind when first used as such.
3927 obj->cache_level = I915_CACHE_LLC;
3929 obj->cache_level = I915_CACHE_NONE;
3931 trace_i915_gem_object_create(obj);
3936 int i915_gem_init_object(struct drm_gem_object *obj)
3943 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3945 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3946 struct drm_device *dev = obj->base.dev;
3947 drm_i915_private_t *dev_priv = dev->dev_private;
3949 trace_i915_gem_object_destroy(obj);
3952 i915_gem_detach_phys_object(dev, obj);
3955 if (WARN_ON(i915_gem_object_unbind(obj) == -ERESTARTSYS)) {
3956 bool was_interruptible;
3958 was_interruptible = dev_priv->mm.interruptible;
3959 dev_priv->mm.interruptible = false;
3961 WARN_ON(i915_gem_object_unbind(obj));
3963 dev_priv->mm.interruptible = was_interruptible;
3966 /* Stolen objects don't hold a ref, but do hold pin count. Fix that up
3967 * before progressing. */
3969 i915_gem_object_unpin_pages(obj);
3971 if (WARN_ON(obj->pages_pin_count))
3972 obj->pages_pin_count = 0;
3973 i915_gem_object_put_pages(obj);
3974 i915_gem_object_free_mmap_offset(obj);
3975 i915_gem_object_release_stolen(obj);
3979 if (obj->base.import_attach)
3980 drm_prime_gem_destroy(&obj->base, NULL);
3982 drm_gem_object_release(&obj->base);
3983 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3986 i915_gem_object_free(obj);
3989 struct i915_vma *i915_gem_vma_create(struct drm_i915_gem_object *obj,
3990 struct i915_address_space *vm)
3992 struct i915_vma *vma = kzalloc(sizeof(*vma), GFP_KERNEL);
3994 return ERR_PTR(-ENOMEM);
3996 INIT_LIST_HEAD(&vma->vma_link);
4003 void i915_gem_vma_destroy(struct i915_vma *vma)
4005 WARN_ON(vma->node.allocated);
4010 i915_gem_idle(struct drm_device *dev)
4012 drm_i915_private_t *dev_priv = dev->dev_private;
4015 if (dev_priv->ums.mm_suspended) {
4016 mutex_unlock(&dev->struct_mutex);
4020 ret = i915_gpu_idle(dev);
4022 mutex_unlock(&dev->struct_mutex);
4025 i915_gem_retire_requests(dev);
4027 /* Under UMS, be paranoid and evict. */
4028 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4029 i915_gem_evict_everything(dev);
4031 del_timer_sync(&dev_priv->gpu_error.hangcheck_timer);
4033 i915_kernel_lost_context(dev);
4034 i915_gem_cleanup_ringbuffer(dev);
4036 /* Cancel the retire work handler, which should be idle now. */
4037 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
4042 void i915_gem_l3_remap(struct drm_device *dev)
4044 drm_i915_private_t *dev_priv = dev->dev_private;
4048 if (!HAS_L3_GPU_CACHE(dev))
4051 if (!dev_priv->l3_parity.remap_info)
4054 misccpctl = I915_READ(GEN7_MISCCPCTL);
4055 I915_WRITE(GEN7_MISCCPCTL, misccpctl & ~GEN7_DOP_CLOCK_GATE_ENABLE);
4056 POSTING_READ(GEN7_MISCCPCTL);
4058 for (i = 0; i < GEN7_L3LOG_SIZE; i += 4) {
4059 u32 remap = I915_READ(GEN7_L3LOG_BASE + i);
4060 if (remap && remap != dev_priv->l3_parity.remap_info[i/4])
4061 DRM_DEBUG("0x%x was already programmed to %x\n",
4062 GEN7_L3LOG_BASE + i, remap);
4063 if (remap && !dev_priv->l3_parity.remap_info[i/4])
4064 DRM_DEBUG_DRIVER("Clearing remapped register\n");
4065 I915_WRITE(GEN7_L3LOG_BASE + i, dev_priv->l3_parity.remap_info[i/4]);
4068 /* Make sure all the writes land before disabling dop clock gating */
4069 POSTING_READ(GEN7_L3LOG_BASE);
4071 I915_WRITE(GEN7_MISCCPCTL, misccpctl);
4074 void i915_gem_init_swizzling(struct drm_device *dev)
4076 drm_i915_private_t *dev_priv = dev->dev_private;
4078 if (INTEL_INFO(dev)->gen < 5 ||
4079 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
4082 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
4083 DISP_TILE_SURFACE_SWIZZLING);
4088 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
4090 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB));
4091 else if (IS_GEN7(dev))
4092 I915_WRITE(ARB_MODE, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB));
4098 intel_enable_blt(struct drm_device *dev)
4103 /* The blitter was dysfunctional on early prototypes */
4104 if (IS_GEN6(dev) && dev->pdev->revision < 8) {
4105 DRM_INFO("BLT not supported on this pre-production hardware;"
4106 " graphics performance will be degraded.\n");
4113 static int i915_gem_init_rings(struct drm_device *dev)
4115 struct drm_i915_private *dev_priv = dev->dev_private;
4118 ret = intel_init_render_ring_buffer(dev);
4123 ret = intel_init_bsd_ring_buffer(dev);
4125 goto cleanup_render_ring;
4128 if (intel_enable_blt(dev)) {
4129 ret = intel_init_blt_ring_buffer(dev);
4131 goto cleanup_bsd_ring;
4134 if (HAS_VEBOX(dev)) {
4135 ret = intel_init_vebox_ring_buffer(dev);
4137 goto cleanup_blt_ring;
4141 ret = i915_gem_set_seqno(dev, ((u32)~0 - 0x1000));
4143 goto cleanup_vebox_ring;
4148 intel_cleanup_ring_buffer(&dev_priv->ring[VECS]);
4150 intel_cleanup_ring_buffer(&dev_priv->ring[BCS]);
4152 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
4153 cleanup_render_ring:
4154 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
4160 i915_gem_init_hw(struct drm_device *dev)
4162 drm_i915_private_t *dev_priv = dev->dev_private;
4165 if (INTEL_INFO(dev)->gen < 6 && !intel_enable_gtt())
4168 if (dev_priv->ellc_size)
4169 I915_WRITE(HSW_IDICR, I915_READ(HSW_IDICR) | IDIHASHMSK(0xf));
4171 if (HAS_PCH_NOP(dev)) {
4172 u32 temp = I915_READ(GEN7_MSG_CTL);
4173 temp &= ~(WAIT_FOR_PCH_FLR_ACK | WAIT_FOR_PCH_RESET_ACK);
4174 I915_WRITE(GEN7_MSG_CTL, temp);
4177 i915_gem_l3_remap(dev);
4179 i915_gem_init_swizzling(dev);
4181 ret = i915_gem_init_rings(dev);
4186 * XXX: There was some w/a described somewhere suggesting loading
4187 * contexts before PPGTT.
4189 i915_gem_context_init(dev);
4190 if (dev_priv->mm.aliasing_ppgtt) {
4191 ret = dev_priv->mm.aliasing_ppgtt->enable(dev);
4193 i915_gem_cleanup_aliasing_ppgtt(dev);
4194 DRM_INFO("PPGTT enable failed. This is not fatal, but unexpected\n");
4201 int i915_gem_init(struct drm_device *dev)
4203 struct drm_i915_private *dev_priv = dev->dev_private;
4206 mutex_lock(&dev->struct_mutex);
4208 if (IS_VALLEYVIEW(dev)) {
4209 /* VLVA0 (potential hack), BIOS isn't actually waking us */
4210 I915_WRITE(VLV_GTLC_WAKE_CTRL, 1);
4211 if (wait_for((I915_READ(VLV_GTLC_PW_STATUS) & 1) == 1, 10))
4212 DRM_DEBUG_DRIVER("allow wake ack timed out\n");
4215 i915_gem_init_global_gtt(dev);
4217 ret = i915_gem_init_hw(dev);
4218 mutex_unlock(&dev->struct_mutex);
4220 i915_gem_cleanup_aliasing_ppgtt(dev);
4224 /* Allow hardware batchbuffers unless told otherwise, but not for KMS. */
4225 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4226 dev_priv->dri1.allow_batchbuffer = 1;
4231 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
4233 drm_i915_private_t *dev_priv = dev->dev_private;
4234 struct intel_ring_buffer *ring;
4237 for_each_ring(ring, dev_priv, i)
4238 intel_cleanup_ring_buffer(ring);
4242 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
4243 struct drm_file *file_priv)
4245 struct drm_i915_private *dev_priv = dev->dev_private;
4248 if (drm_core_check_feature(dev, DRIVER_MODESET))
4251 if (i915_reset_in_progress(&dev_priv->gpu_error)) {
4252 DRM_ERROR("Reenabling wedged hardware, good luck\n");
4253 atomic_set(&dev_priv->gpu_error.reset_counter, 0);
4256 mutex_lock(&dev->struct_mutex);
4257 dev_priv->ums.mm_suspended = 0;
4259 ret = i915_gem_init_hw(dev);
4261 mutex_unlock(&dev->struct_mutex);
4265 BUG_ON(!list_empty(&dev_priv->gtt.base.active_list));
4266 mutex_unlock(&dev->struct_mutex);
4268 ret = drm_irq_install(dev);
4270 goto cleanup_ringbuffer;
4275 mutex_lock(&dev->struct_mutex);
4276 i915_gem_cleanup_ringbuffer(dev);
4277 dev_priv->ums.mm_suspended = 1;
4278 mutex_unlock(&dev->struct_mutex);
4284 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
4285 struct drm_file *file_priv)
4287 struct drm_i915_private *dev_priv = dev->dev_private;
4290 if (drm_core_check_feature(dev, DRIVER_MODESET))
4293 drm_irq_uninstall(dev);
4295 mutex_lock(&dev->struct_mutex);
4296 ret = i915_gem_idle(dev);
4298 /* Hack! Don't let anybody do execbuf while we don't control the chip.
4299 * We need to replace this with a semaphore, or something.
4300 * And not confound ums.mm_suspended!
4303 dev_priv->ums.mm_suspended = 1;
4304 mutex_unlock(&dev->struct_mutex);
4310 i915_gem_lastclose(struct drm_device *dev)
4314 if (drm_core_check_feature(dev, DRIVER_MODESET))
4317 mutex_lock(&dev->struct_mutex);
4318 ret = i915_gem_idle(dev);
4320 DRM_ERROR("failed to idle hardware: %d\n", ret);
4321 mutex_unlock(&dev->struct_mutex);
4325 init_ring_lists(struct intel_ring_buffer *ring)
4327 INIT_LIST_HEAD(&ring->active_list);
4328 INIT_LIST_HEAD(&ring->request_list);
4332 i915_gem_load(struct drm_device *dev)
4334 drm_i915_private_t *dev_priv = dev->dev_private;
4338 kmem_cache_create("i915_gem_object",
4339 sizeof(struct drm_i915_gem_object), 0,
4343 INIT_LIST_HEAD(&dev_priv->gtt.base.active_list);
4344 INIT_LIST_HEAD(&dev_priv->gtt.base.inactive_list);
4345 INIT_LIST_HEAD(&dev_priv->mm.unbound_list);
4346 INIT_LIST_HEAD(&dev_priv->mm.bound_list);
4347 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4348 for (i = 0; i < I915_NUM_RINGS; i++)
4349 init_ring_lists(&dev_priv->ring[i]);
4350 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
4351 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
4352 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
4353 i915_gem_retire_work_handler);
4354 init_waitqueue_head(&dev_priv->gpu_error.reset_queue);
4356 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
4358 I915_WRITE(MI_ARB_STATE,
4359 _MASKED_BIT_ENABLE(MI_ARB_C3_LP_WRITE_ENABLE));
4362 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
4364 /* Old X drivers will take 0-2 for front, back, depth buffers */
4365 if (!drm_core_check_feature(dev, DRIVER_MODESET))
4366 dev_priv->fence_reg_start = 3;
4368 if (INTEL_INFO(dev)->gen >= 7 && !IS_VALLEYVIEW(dev))
4369 dev_priv->num_fence_regs = 32;
4370 else if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
4371 dev_priv->num_fence_regs = 16;
4373 dev_priv->num_fence_regs = 8;
4375 /* Initialize fence registers to zero */
4376 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
4377 i915_gem_restore_fences(dev);
4379 i915_gem_detect_bit_6_swizzle(dev);
4380 init_waitqueue_head(&dev_priv->pending_flip_queue);
4382 dev_priv->mm.interruptible = true;
4384 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
4385 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
4386 register_shrinker(&dev_priv->mm.inactive_shrinker);
4390 * Create a physically contiguous memory object for this object
4391 * e.g. for cursor + overlay regs
4393 static int i915_gem_init_phys_object(struct drm_device *dev,
4394 int id, int size, int align)
4396 drm_i915_private_t *dev_priv = dev->dev_private;
4397 struct drm_i915_gem_phys_object *phys_obj;
4400 if (dev_priv->mm.phys_objs[id - 1] || !size)
4403 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
4409 phys_obj->handle = drm_pci_alloc(dev, size, align);
4410 if (!phys_obj->handle) {
4415 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4418 dev_priv->mm.phys_objs[id - 1] = phys_obj;
4426 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
4428 drm_i915_private_t *dev_priv = dev->dev_private;
4429 struct drm_i915_gem_phys_object *phys_obj;
4431 if (!dev_priv->mm.phys_objs[id - 1])
4434 phys_obj = dev_priv->mm.phys_objs[id - 1];
4435 if (phys_obj->cur_obj) {
4436 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
4440 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
4442 drm_pci_free(dev, phys_obj->handle);
4444 dev_priv->mm.phys_objs[id - 1] = NULL;
4447 void i915_gem_free_all_phys_object(struct drm_device *dev)
4451 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
4452 i915_gem_free_phys_object(dev, i);
4455 void i915_gem_detach_phys_object(struct drm_device *dev,
4456 struct drm_i915_gem_object *obj)
4458 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
4465 vaddr = obj->phys_obj->handle->vaddr;
4467 page_count = obj->base.size / PAGE_SIZE;
4468 for (i = 0; i < page_count; i++) {
4469 struct page *page = shmem_read_mapping_page(mapping, i);
4470 if (!IS_ERR(page)) {
4471 char *dst = kmap_atomic(page);
4472 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
4475 drm_clflush_pages(&page, 1);
4477 set_page_dirty(page);
4478 mark_page_accessed(page);
4479 page_cache_release(page);
4482 i915_gem_chipset_flush(dev);
4484 obj->phys_obj->cur_obj = NULL;
4485 obj->phys_obj = NULL;
4489 i915_gem_attach_phys_object(struct drm_device *dev,
4490 struct drm_i915_gem_object *obj,
4494 struct address_space *mapping = file_inode(obj->base.filp)->i_mapping;
4495 drm_i915_private_t *dev_priv = dev->dev_private;
4500 if (id > I915_MAX_PHYS_OBJECT)
4503 if (obj->phys_obj) {
4504 if (obj->phys_obj->id == id)
4506 i915_gem_detach_phys_object(dev, obj);
4509 /* create a new object */
4510 if (!dev_priv->mm.phys_objs[id - 1]) {
4511 ret = i915_gem_init_phys_object(dev, id,
4512 obj->base.size, align);
4514 DRM_ERROR("failed to init phys object %d size: %zu\n",
4515 id, obj->base.size);
4520 /* bind to the object */
4521 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
4522 obj->phys_obj->cur_obj = obj;
4524 page_count = obj->base.size / PAGE_SIZE;
4526 for (i = 0; i < page_count; i++) {
4530 page = shmem_read_mapping_page(mapping, i);
4532 return PTR_ERR(page);
4534 src = kmap_atomic(page);
4535 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
4536 memcpy(dst, src, PAGE_SIZE);
4539 mark_page_accessed(page);
4540 page_cache_release(page);
4547 i915_gem_phys_pwrite(struct drm_device *dev,
4548 struct drm_i915_gem_object *obj,
4549 struct drm_i915_gem_pwrite *args,
4550 struct drm_file *file_priv)
4552 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
4553 char __user *user_data = to_user_ptr(args->data_ptr);
4555 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
4556 unsigned long unwritten;
4558 /* The physical object once assigned is fixed for the lifetime
4559 * of the obj, so we can safely drop the lock and continue
4562 mutex_unlock(&dev->struct_mutex);
4563 unwritten = copy_from_user(vaddr, user_data, args->size);
4564 mutex_lock(&dev->struct_mutex);
4569 i915_gem_chipset_flush(dev);
4573 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4575 struct drm_i915_file_private *file_priv = file->driver_priv;
4577 /* Clean up our request list when the client is going away, so that
4578 * later retire_requests won't dereference our soon-to-be-gone
4581 spin_lock(&file_priv->mm.lock);
4582 while (!list_empty(&file_priv->mm.request_list)) {
4583 struct drm_i915_gem_request *request;
4585 request = list_first_entry(&file_priv->mm.request_list,
4586 struct drm_i915_gem_request,
4588 list_del(&request->client_list);
4589 request->file_priv = NULL;
4591 spin_unlock(&file_priv->mm.lock);
4594 static bool mutex_is_locked_by(struct mutex *mutex, struct task_struct *task)
4596 if (!mutex_is_locked(mutex))
4599 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_MUTEXES)
4600 return mutex->owner == task;
4602 /* Since UP may be pre-empted, we cannot assume that we own the lock */
4608 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4610 struct drm_i915_private *dev_priv =
4611 container_of(shrinker,
4612 struct drm_i915_private,
4613 mm.inactive_shrinker);
4614 struct drm_device *dev = dev_priv->dev;
4615 struct i915_address_space *vm = &dev_priv->gtt.base;
4616 struct drm_i915_gem_object *obj;
4617 int nr_to_scan = sc->nr_to_scan;
4621 if (!mutex_trylock(&dev->struct_mutex)) {
4622 if (!mutex_is_locked_by(&dev->struct_mutex, current))
4625 if (dev_priv->mm.shrinker_no_lock_stealing)
4632 nr_to_scan -= i915_gem_purge(dev_priv, nr_to_scan);
4634 nr_to_scan -= __i915_gem_shrink(dev_priv, nr_to_scan,
4637 i915_gem_shrink_all(dev_priv);
4641 list_for_each_entry(obj, &dev_priv->mm.unbound_list, global_list)
4642 if (obj->pages_pin_count == 0)
4643 cnt += obj->base.size >> PAGE_SHIFT;
4644 list_for_each_entry(obj, &vm->inactive_list, mm_list)
4645 if (obj->pin_count == 0 && obj->pages_pin_count == 0)
4646 cnt += obj->base.size >> PAGE_SHIFT;
4649 mutex_unlock(&dev->struct_mutex);