2 * Copyright © 2008 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24 * Eric Anholt <eric@anholt.net>
32 #include "i915_trace.h"
33 #include "intel_drv.h"
34 #include <linux/shmem_fs.h>
35 #include <linux/slab.h>
36 #include <linux/swap.h>
37 #include <linux/pci.h>
39 static __must_check int i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj);
40 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj);
41 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj);
42 static __must_check int i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
44 bool map_and_fenceable);
45 static void i915_gem_clear_fence_reg(struct drm_device *dev,
46 struct drm_i915_fence_reg *reg);
47 static int i915_gem_phys_pwrite(struct drm_device *dev,
48 struct drm_i915_gem_object *obj,
49 struct drm_i915_gem_pwrite *args,
50 struct drm_file *file);
51 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj);
53 static int i915_gem_inactive_shrink(struct shrinker *shrinker,
54 struct shrink_control *sc);
55 static void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
57 /* some bookkeeping */
58 static void i915_gem_info_add_obj(struct drm_i915_private *dev_priv,
61 dev_priv->mm.object_count++;
62 dev_priv->mm.object_memory += size;
65 static void i915_gem_info_remove_obj(struct drm_i915_private *dev_priv,
68 dev_priv->mm.object_count--;
69 dev_priv->mm.object_memory -= size;
73 i915_gem_wait_for_error(struct drm_device *dev)
75 struct drm_i915_private *dev_priv = dev->dev_private;
76 struct completion *x = &dev_priv->error_completion;
80 if (!atomic_read(&dev_priv->mm.wedged))
83 ret = wait_for_completion_interruptible(x);
87 if (atomic_read(&dev_priv->mm.wedged)) {
88 /* GPU is hung, bump the completion count to account for
89 * the token we just consumed so that we never hit zero and
90 * end up waiting upon a subsequent completion event that
93 spin_lock_irqsave(&x->wait.lock, flags);
95 spin_unlock_irqrestore(&x->wait.lock, flags);
100 int i915_mutex_lock_interruptible(struct drm_device *dev)
104 ret = i915_gem_wait_for_error(dev);
108 ret = mutex_lock_interruptible(&dev->struct_mutex);
112 WARN_ON(i915_verify_lists(dev));
117 i915_gem_object_is_inactive(struct drm_i915_gem_object *obj)
119 return obj->gtt_space && !obj->active && obj->pin_count == 0;
123 i915_gem_init_ioctl(struct drm_device *dev, void *data,
124 struct drm_file *file)
126 struct drm_i915_gem_init *args = data;
128 if (args->gtt_start >= args->gtt_end ||
129 (args->gtt_end | args->gtt_start) & (PAGE_SIZE - 1))
132 /* GEM with user mode setting was never supported on ilk and later. */
133 if (INTEL_INFO(dev)->gen >= 5)
136 mutex_lock(&dev->struct_mutex);
137 i915_gem_init_global_gtt(dev, args->gtt_start,
138 args->gtt_end, args->gtt_end);
139 mutex_unlock(&dev->struct_mutex);
145 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
146 struct drm_file *file)
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 struct drm_i915_gem_get_aperture *args = data;
150 struct drm_i915_gem_object *obj;
153 if (!(dev->driver->driver_features & DRIVER_GEM))
157 mutex_lock(&dev->struct_mutex);
158 list_for_each_entry(obj, &dev_priv->mm.pinned_list, mm_list)
159 pinned += obj->gtt_space->size;
160 mutex_unlock(&dev->struct_mutex);
162 args->aper_size = dev_priv->mm.gtt_total;
163 args->aper_available_size = args->aper_size - pinned;
169 i915_gem_create(struct drm_file *file,
170 struct drm_device *dev,
174 struct drm_i915_gem_object *obj;
178 size = roundup(size, PAGE_SIZE);
182 /* Allocate the new object */
183 obj = i915_gem_alloc_object(dev, size);
187 ret = drm_gem_handle_create(file, &obj->base, &handle);
189 drm_gem_object_release(&obj->base);
190 i915_gem_info_remove_obj(dev->dev_private, obj->base.size);
195 /* drop reference from allocate - handle holds it now */
196 drm_gem_object_unreference(&obj->base);
197 trace_i915_gem_object_create(obj);
204 i915_gem_dumb_create(struct drm_file *file,
205 struct drm_device *dev,
206 struct drm_mode_create_dumb *args)
208 /* have to work out size/pitch and return them */
209 args->pitch = ALIGN(args->width * ((args->bpp + 7) / 8), 64);
210 args->size = args->pitch * args->height;
211 return i915_gem_create(file, dev,
212 args->size, &args->handle);
215 int i915_gem_dumb_destroy(struct drm_file *file,
216 struct drm_device *dev,
219 return drm_gem_handle_delete(file, handle);
223 * Creates a new mm object and returns a handle to it.
226 i915_gem_create_ioctl(struct drm_device *dev, void *data,
227 struct drm_file *file)
229 struct drm_i915_gem_create *args = data;
230 return i915_gem_create(file, dev,
231 args->size, &args->handle);
234 static int i915_gem_object_needs_bit17_swizzle(struct drm_i915_gem_object *obj)
236 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
238 return dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_9_10_17 &&
239 obj->tiling_mode != I915_TILING_NONE;
243 __copy_to_user_swizzled(char __user *cpu_vaddr,
244 const char *gpu_vaddr, int gpu_offset,
247 int ret, cpu_offset = 0;
250 int cacheline_end = ALIGN(gpu_offset + 1, 64);
251 int this_length = min(cacheline_end - gpu_offset, length);
252 int swizzled_gpu_offset = gpu_offset ^ 64;
254 ret = __copy_to_user(cpu_vaddr + cpu_offset,
255 gpu_vaddr + swizzled_gpu_offset,
260 cpu_offset += this_length;
261 gpu_offset += this_length;
262 length -= this_length;
269 __copy_from_user_swizzled(char __user *gpu_vaddr, int gpu_offset,
270 const char *cpu_vaddr,
273 int ret, cpu_offset = 0;
276 int cacheline_end = ALIGN(gpu_offset + 1, 64);
277 int this_length = min(cacheline_end - gpu_offset, length);
278 int swizzled_gpu_offset = gpu_offset ^ 64;
280 ret = __copy_from_user(gpu_vaddr + swizzled_gpu_offset,
281 cpu_vaddr + cpu_offset,
286 cpu_offset += this_length;
287 gpu_offset += this_length;
288 length -= this_length;
294 /* Per-page copy function for the shmem pread fastpath.
295 * Flushes invalid cachelines before reading the target if
296 * needs_clflush is set. */
298 shmem_pread_fast(struct page *page, int shmem_page_offset, int page_length,
299 char __user *user_data,
300 bool page_do_bit17_swizzling, bool needs_clflush)
305 if (unlikely(page_do_bit17_swizzling))
308 vaddr = kmap_atomic(page);
310 drm_clflush_virt_range(vaddr + shmem_page_offset,
312 ret = __copy_to_user_inatomic(user_data,
313 vaddr + shmem_page_offset,
315 kunmap_atomic(vaddr);
321 shmem_clflush_swizzled_range(char *addr, unsigned long length,
324 if (unlikely(swizzled)) {
325 unsigned long start = (unsigned long) addr;
326 unsigned long end = (unsigned long) addr + length;
328 /* For swizzling simply ensure that we always flush both
329 * channels. Lame, but simple and it works. Swizzled
330 * pwrite/pread is far from a hotpath - current userspace
331 * doesn't use it at all. */
332 start = round_down(start, 128);
333 end = round_up(end, 128);
335 drm_clflush_virt_range((void *)start, end - start);
337 drm_clflush_virt_range(addr, length);
342 /* Only difference to the fast-path function is that this can handle bit17
343 * and uses non-atomic copy and kmap functions. */
345 shmem_pread_slow(struct page *page, int shmem_page_offset, int page_length,
346 char __user *user_data,
347 bool page_do_bit17_swizzling, bool needs_clflush)
354 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
356 page_do_bit17_swizzling);
358 if (page_do_bit17_swizzling)
359 ret = __copy_to_user_swizzled(user_data,
360 vaddr, shmem_page_offset,
363 ret = __copy_to_user(user_data,
364 vaddr + shmem_page_offset,
372 i915_gem_shmem_pread(struct drm_device *dev,
373 struct drm_i915_gem_object *obj,
374 struct drm_i915_gem_pread *args,
375 struct drm_file *file)
377 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
378 char __user *user_data;
381 int shmem_page_offset, page_length, ret = 0;
382 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
383 int hit_slowpath = 0;
385 int needs_clflush = 0;
388 user_data = (char __user *) (uintptr_t) args->data_ptr;
391 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
393 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)) {
394 /* If we're not in the cpu read domain, set ourself into the gtt
395 * read domain and manually flush cachelines (if required). This
396 * optimizes for the case when the gpu will dirty the data
397 * anyway again before the next pread happens. */
398 if (obj->cache_level == I915_CACHE_NONE)
400 ret = i915_gem_object_set_to_gtt_domain(obj, false);
405 offset = args->offset;
410 /* Operation in this page
412 * shmem_page_offset = offset within page in shmem file
413 * page_length = bytes to copy for this page
415 shmem_page_offset = offset_in_page(offset);
416 page_length = remain;
417 if ((shmem_page_offset + page_length) > PAGE_SIZE)
418 page_length = PAGE_SIZE - shmem_page_offset;
421 page = obj->pages[offset >> PAGE_SHIFT];
424 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
432 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
433 (page_to_phys(page) & (1 << 17)) != 0;
435 ret = shmem_pread_fast(page, shmem_page_offset, page_length,
436 user_data, page_do_bit17_swizzling,
442 page_cache_get(page);
443 mutex_unlock(&dev->struct_mutex);
446 ret = fault_in_multipages_writeable(user_data, remain);
447 /* Userspace is tricking us, but we've already clobbered
448 * its pages with the prefault and promised to write the
449 * data up to the first fault. Hence ignore any errors
450 * and just continue. */
455 ret = shmem_pread_slow(page, shmem_page_offset, page_length,
456 user_data, page_do_bit17_swizzling,
459 mutex_lock(&dev->struct_mutex);
460 page_cache_release(page);
462 mark_page_accessed(page);
464 page_cache_release(page);
471 remain -= page_length;
472 user_data += page_length;
473 offset += page_length;
478 /* Fixup: Kill any reinstated backing storage pages */
479 if (obj->madv == __I915_MADV_PURGED)
480 i915_gem_object_truncate(obj);
487 * Reads data from the object referenced by handle.
489 * On error, the contents of *data are undefined.
492 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
493 struct drm_file *file)
495 struct drm_i915_gem_pread *args = data;
496 struct drm_i915_gem_object *obj;
502 if (!access_ok(VERIFY_WRITE,
503 (char __user *)(uintptr_t)args->data_ptr,
507 ret = i915_mutex_lock_interruptible(dev);
511 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
512 if (&obj->base == NULL) {
517 /* Bounds check source. */
518 if (args->offset > obj->base.size ||
519 args->size > obj->base.size - args->offset) {
524 trace_i915_gem_object_pread(obj, args->offset, args->size);
526 ret = i915_gem_shmem_pread(dev, obj, args, file);
529 drm_gem_object_unreference(&obj->base);
531 mutex_unlock(&dev->struct_mutex);
535 /* This is the fast write path which cannot handle
536 * page faults in the source data
540 fast_user_write(struct io_mapping *mapping,
541 loff_t page_base, int page_offset,
542 char __user *user_data,
546 unsigned long unwritten;
548 vaddr_atomic = io_mapping_map_atomic_wc(mapping, page_base);
549 unwritten = __copy_from_user_inatomic_nocache(vaddr_atomic + page_offset,
551 io_mapping_unmap_atomic(vaddr_atomic);
556 * This is the fast pwrite path, where we copy the data directly from the
557 * user into the GTT, uncached.
560 i915_gem_gtt_pwrite_fast(struct drm_device *dev,
561 struct drm_i915_gem_object *obj,
562 struct drm_i915_gem_pwrite *args,
563 struct drm_file *file)
565 drm_i915_private_t *dev_priv = dev->dev_private;
567 loff_t offset, page_base;
568 char __user *user_data;
569 int page_offset, page_length, ret;
571 ret = i915_gem_object_pin(obj, 0, true);
575 ret = i915_gem_object_set_to_gtt_domain(obj, true);
579 ret = i915_gem_object_put_fence(obj);
583 user_data = (char __user *) (uintptr_t) args->data_ptr;
586 offset = obj->gtt_offset + args->offset;
589 /* Operation in this page
591 * page_base = page offset within aperture
592 * page_offset = offset within page
593 * page_length = bytes to copy for this page
595 page_base = offset & PAGE_MASK;
596 page_offset = offset_in_page(offset);
597 page_length = remain;
598 if ((page_offset + remain) > PAGE_SIZE)
599 page_length = PAGE_SIZE - page_offset;
601 /* If we get a fault while copying data, then (presumably) our
602 * source page isn't available. Return the error and we'll
603 * retry in the slow path.
605 if (fast_user_write(dev_priv->mm.gtt_mapping, page_base,
606 page_offset, user_data, page_length)) {
611 remain -= page_length;
612 user_data += page_length;
613 offset += page_length;
617 i915_gem_object_unpin(obj);
622 /* Per-page copy function for the shmem pwrite fastpath.
623 * Flushes invalid cachelines before writing to the target if
624 * needs_clflush_before is set and flushes out any written cachelines after
625 * writing if needs_clflush is set. */
627 shmem_pwrite_fast(struct page *page, int shmem_page_offset, int page_length,
628 char __user *user_data,
629 bool page_do_bit17_swizzling,
630 bool needs_clflush_before,
631 bool needs_clflush_after)
636 if (unlikely(page_do_bit17_swizzling))
639 vaddr = kmap_atomic(page);
640 if (needs_clflush_before)
641 drm_clflush_virt_range(vaddr + shmem_page_offset,
643 ret = __copy_from_user_inatomic_nocache(vaddr + shmem_page_offset,
646 if (needs_clflush_after)
647 drm_clflush_virt_range(vaddr + shmem_page_offset,
649 kunmap_atomic(vaddr);
654 /* Only difference to the fast-path function is that this can handle bit17
655 * and uses non-atomic copy and kmap functions. */
657 shmem_pwrite_slow(struct page *page, int shmem_page_offset, int page_length,
658 char __user *user_data,
659 bool page_do_bit17_swizzling,
660 bool needs_clflush_before,
661 bool needs_clflush_after)
667 if (unlikely(needs_clflush_before || page_do_bit17_swizzling))
668 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
670 page_do_bit17_swizzling);
671 if (page_do_bit17_swizzling)
672 ret = __copy_from_user_swizzled(vaddr, shmem_page_offset,
676 ret = __copy_from_user(vaddr + shmem_page_offset,
679 if (needs_clflush_after)
680 shmem_clflush_swizzled_range(vaddr + shmem_page_offset,
682 page_do_bit17_swizzling);
689 i915_gem_shmem_pwrite(struct drm_device *dev,
690 struct drm_i915_gem_object *obj,
691 struct drm_i915_gem_pwrite *args,
692 struct drm_file *file)
694 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
697 char __user *user_data;
698 int shmem_page_offset, page_length, ret = 0;
699 int obj_do_bit17_swizzling, page_do_bit17_swizzling;
700 int hit_slowpath = 0;
701 int needs_clflush_after = 0;
702 int needs_clflush_before = 0;
705 user_data = (char __user *) (uintptr_t) args->data_ptr;
708 obj_do_bit17_swizzling = i915_gem_object_needs_bit17_swizzle(obj);
710 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
711 /* If we're not in the cpu write domain, set ourself into the gtt
712 * write domain and manually flush cachelines (if required). This
713 * optimizes for the case when the gpu will use the data
714 * right away and we therefore have to clflush anyway. */
715 if (obj->cache_level == I915_CACHE_NONE)
716 needs_clflush_after = 1;
717 ret = i915_gem_object_set_to_gtt_domain(obj, true);
721 /* Same trick applies for invalidate partially written cachelines before
723 if (!(obj->base.read_domains & I915_GEM_DOMAIN_CPU)
724 && obj->cache_level == I915_CACHE_NONE)
725 needs_clflush_before = 1;
727 offset = args->offset;
732 int partial_cacheline_write;
734 /* Operation in this page
736 * shmem_page_offset = offset within page in shmem file
737 * page_length = bytes to copy for this page
739 shmem_page_offset = offset_in_page(offset);
741 page_length = remain;
742 if ((shmem_page_offset + page_length) > PAGE_SIZE)
743 page_length = PAGE_SIZE - shmem_page_offset;
745 /* If we don't overwrite a cacheline completely we need to be
746 * careful to have up-to-date data by first clflushing. Don't
747 * overcomplicate things and flush the entire patch. */
748 partial_cacheline_write = needs_clflush_before &&
749 ((shmem_page_offset | page_length)
750 & (boot_cpu_data.x86_clflush_size - 1));
753 page = obj->pages[offset >> PAGE_SHIFT];
756 page = shmem_read_mapping_page(mapping, offset >> PAGE_SHIFT);
764 page_do_bit17_swizzling = obj_do_bit17_swizzling &&
765 (page_to_phys(page) & (1 << 17)) != 0;
767 ret = shmem_pwrite_fast(page, shmem_page_offset, page_length,
768 user_data, page_do_bit17_swizzling,
769 partial_cacheline_write,
770 needs_clflush_after);
775 page_cache_get(page);
776 mutex_unlock(&dev->struct_mutex);
778 ret = shmem_pwrite_slow(page, shmem_page_offset, page_length,
779 user_data, page_do_bit17_swizzling,
780 partial_cacheline_write,
781 needs_clflush_after);
783 mutex_lock(&dev->struct_mutex);
784 page_cache_release(page);
786 set_page_dirty(page);
787 mark_page_accessed(page);
789 page_cache_release(page);
796 remain -= page_length;
797 user_data += page_length;
798 offset += page_length;
803 /* Fixup: Kill any reinstated backing storage pages */
804 if (obj->madv == __I915_MADV_PURGED)
805 i915_gem_object_truncate(obj);
806 /* and flush dirty cachelines in case the object isn't in the cpu write
808 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
809 i915_gem_clflush_object(obj);
810 intel_gtt_chipset_flush();
814 if (needs_clflush_after)
815 intel_gtt_chipset_flush();
821 * Writes data to the object referenced by handle.
823 * On error, the contents of the buffer that were to be modified are undefined.
826 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
827 struct drm_file *file)
829 struct drm_i915_gem_pwrite *args = data;
830 struct drm_i915_gem_object *obj;
836 if (!access_ok(VERIFY_READ,
837 (char __user *)(uintptr_t)args->data_ptr,
841 ret = fault_in_multipages_readable((char __user *)(uintptr_t)args->data_ptr,
846 ret = i915_mutex_lock_interruptible(dev);
850 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
851 if (&obj->base == NULL) {
856 /* Bounds check destination. */
857 if (args->offset > obj->base.size ||
858 args->size > obj->base.size - args->offset) {
863 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
866 /* We can only do the GTT pwrite on untiled buffers, as otherwise
867 * it would end up going through the fenced access, and we'll get
868 * different detiling behavior between reading and writing.
869 * pread/pwrite currently are reading and writing from the CPU
870 * perspective, requiring manual detiling by the client.
873 ret = i915_gem_phys_pwrite(dev, obj, args, file);
877 if (obj->gtt_space &&
878 obj->cache_level == I915_CACHE_NONE &&
879 obj->map_and_fenceable &&
880 obj->base.write_domain != I915_GEM_DOMAIN_CPU) {
881 ret = i915_gem_gtt_pwrite_fast(dev, obj, args, file);
882 /* Note that the gtt paths might fail with non-page-backed user
883 * pointers (e.g. gtt mappings when moving data between
884 * textures). Fallback to the shmem path in that case. */
888 ret = i915_gem_shmem_pwrite(dev, obj, args, file);
891 drm_gem_object_unreference(&obj->base);
893 mutex_unlock(&dev->struct_mutex);
898 * Called when user space prepares to use an object with the CPU, either
899 * through the mmap ioctl's mapping or a GTT mapping.
902 i915_gem_set_domain_ioctl(struct drm_device *dev, void *data,
903 struct drm_file *file)
905 struct drm_i915_gem_set_domain *args = data;
906 struct drm_i915_gem_object *obj;
907 uint32_t read_domains = args->read_domains;
908 uint32_t write_domain = args->write_domain;
911 if (!(dev->driver->driver_features & DRIVER_GEM))
914 /* Only handle setting domains to types used by the CPU. */
915 if (write_domain & I915_GEM_GPU_DOMAINS)
918 if (read_domains & I915_GEM_GPU_DOMAINS)
921 /* Having something in the write domain implies it's in the read
922 * domain, and only that read domain. Enforce that in the request.
924 if (write_domain != 0 && read_domains != write_domain)
927 ret = i915_mutex_lock_interruptible(dev);
931 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
932 if (&obj->base == NULL) {
937 if (read_domains & I915_GEM_DOMAIN_GTT) {
938 ret = i915_gem_object_set_to_gtt_domain(obj, write_domain != 0);
940 /* Silently promote "you're not bound, there was nothing to do"
941 * to success, since the client was just asking us to
942 * make sure everything was done.
947 ret = i915_gem_object_set_to_cpu_domain(obj, write_domain != 0);
950 drm_gem_object_unreference(&obj->base);
952 mutex_unlock(&dev->struct_mutex);
957 * Called when user space has done writes to this buffer
960 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
961 struct drm_file *file)
963 struct drm_i915_gem_sw_finish *args = data;
964 struct drm_i915_gem_object *obj;
967 if (!(dev->driver->driver_features & DRIVER_GEM))
970 ret = i915_mutex_lock_interruptible(dev);
974 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
975 if (&obj->base == NULL) {
980 /* Pinned buffers may be scanout, so flush the cache */
982 i915_gem_object_flush_cpu_write_domain(obj);
984 drm_gem_object_unreference(&obj->base);
986 mutex_unlock(&dev->struct_mutex);
991 * Maps the contents of an object, returning the address it is mapped
994 * While the mapping holds a reference on the contents of the object, it doesn't
995 * imply a ref on the object itself.
998 i915_gem_mmap_ioctl(struct drm_device *dev, void *data,
999 struct drm_file *file)
1001 struct drm_i915_gem_mmap *args = data;
1002 struct drm_gem_object *obj;
1005 if (!(dev->driver->driver_features & DRIVER_GEM))
1008 obj = drm_gem_object_lookup(dev, file, args->handle);
1012 down_write(¤t->mm->mmap_sem);
1013 addr = do_mmap(obj->filp, 0, args->size,
1014 PROT_READ | PROT_WRITE, MAP_SHARED,
1016 up_write(¤t->mm->mmap_sem);
1017 drm_gem_object_unreference_unlocked(obj);
1018 if (IS_ERR((void *)addr))
1021 args->addr_ptr = (uint64_t) addr;
1027 * i915_gem_fault - fault a page into the GTT
1028 * vma: VMA in question
1031 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1032 * from userspace. The fault handler takes care of binding the object to
1033 * the GTT (if needed), allocating and programming a fence register (again,
1034 * only if needed based on whether the old reg is still valid or the object
1035 * is tiled) and inserting a new PTE into the faulting process.
1037 * Note that the faulting process may involve evicting existing objects
1038 * from the GTT and/or fence registers to make room. So performance may
1039 * suffer if the GTT working set is large or there are few fence registers
1042 int i915_gem_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
1044 struct drm_i915_gem_object *obj = to_intel_bo(vma->vm_private_data);
1045 struct drm_device *dev = obj->base.dev;
1046 drm_i915_private_t *dev_priv = dev->dev_private;
1047 pgoff_t page_offset;
1050 bool write = !!(vmf->flags & FAULT_FLAG_WRITE);
1052 /* We don't use vmf->pgoff since that has the fake offset */
1053 page_offset = ((unsigned long)vmf->virtual_address - vma->vm_start) >>
1056 ret = i915_mutex_lock_interruptible(dev);
1060 trace_i915_gem_object_fault(obj, page_offset, true, write);
1062 /* Now bind it into the GTT if needed */
1063 if (!obj->map_and_fenceable) {
1064 ret = i915_gem_object_unbind(obj);
1068 if (!obj->gtt_space) {
1069 ret = i915_gem_object_bind_to_gtt(obj, 0, true);
1073 ret = i915_gem_object_set_to_gtt_domain(obj, write);
1078 if (!obj->has_global_gtt_mapping)
1079 i915_gem_gtt_bind_object(obj, obj->cache_level);
1081 if (obj->tiling_mode == I915_TILING_NONE)
1082 ret = i915_gem_object_put_fence(obj);
1084 ret = i915_gem_object_get_fence(obj, NULL);
1088 if (i915_gem_object_is_inactive(obj))
1089 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1091 obj->fault_mappable = true;
1093 pfn = ((dev->agp->base + obj->gtt_offset) >> PAGE_SHIFT) +
1096 /* Finally, remap it using the new GTT offset */
1097 ret = vm_insert_pfn(vma, (unsigned long)vmf->virtual_address, pfn);
1099 mutex_unlock(&dev->struct_mutex);
1104 /* Give the error handler a chance to run and move the
1105 * objects off the GPU active list. Next time we service the
1106 * fault, we should be able to transition the page into the
1107 * GTT without touching the GPU (and so avoid further
1108 * EIO/EGAIN). If the GPU is wedged, then there is no issue
1109 * with coherency, just lost writes.
1115 return VM_FAULT_NOPAGE;
1117 return VM_FAULT_OOM;
1119 return VM_FAULT_SIGBUS;
1124 * i915_gem_release_mmap - remove physical page mappings
1125 * @obj: obj in question
1127 * Preserve the reservation of the mmapping with the DRM core code, but
1128 * relinquish ownership of the pages back to the system.
1130 * It is vital that we remove the page mapping if we have mapped a tiled
1131 * object through the GTT and then lose the fence register due to
1132 * resource pressure. Similarly if the object has been moved out of the
1133 * aperture, than pages mapped into userspace must be revoked. Removing the
1134 * mapping will then trigger a page fault on the next user access, allowing
1135 * fixup by i915_gem_fault().
1138 i915_gem_release_mmap(struct drm_i915_gem_object *obj)
1140 if (!obj->fault_mappable)
1143 if (obj->base.dev->dev_mapping)
1144 unmap_mapping_range(obj->base.dev->dev_mapping,
1145 (loff_t)obj->base.map_list.hash.key<<PAGE_SHIFT,
1148 obj->fault_mappable = false;
1152 i915_gem_get_gtt_size(struct drm_device *dev, uint32_t size, int tiling_mode)
1156 if (INTEL_INFO(dev)->gen >= 4 ||
1157 tiling_mode == I915_TILING_NONE)
1160 /* Previous chips need a power-of-two fence region when tiling */
1161 if (INTEL_INFO(dev)->gen == 3)
1162 gtt_size = 1024*1024;
1164 gtt_size = 512*1024;
1166 while (gtt_size < size)
1173 * i915_gem_get_gtt_alignment - return required GTT alignment for an object
1174 * @obj: object to check
1176 * Return the required GTT alignment for an object, taking into account
1177 * potential fence register mapping.
1180 i915_gem_get_gtt_alignment(struct drm_device *dev,
1185 * Minimum alignment is 4k (GTT page size), but might be greater
1186 * if a fence register is needed for the object.
1188 if (INTEL_INFO(dev)->gen >= 4 ||
1189 tiling_mode == I915_TILING_NONE)
1193 * Previous chips need to be aligned to the size of the smallest
1194 * fence register that can contain the object.
1196 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1200 * i915_gem_get_unfenced_gtt_alignment - return required GTT alignment for an
1203 * @size: size of the object
1204 * @tiling_mode: tiling mode of the object
1206 * Return the required GTT alignment for an object, only taking into account
1207 * unfenced tiled surface requirements.
1210 i915_gem_get_unfenced_gtt_alignment(struct drm_device *dev,
1215 * Minimum alignment is 4k (GTT page size) for sane hw.
1217 if (INTEL_INFO(dev)->gen >= 4 || IS_G33(dev) ||
1218 tiling_mode == I915_TILING_NONE)
1221 /* Previous hardware however needs to be aligned to a power-of-two
1222 * tile height. The simplest method for determining this is to reuse
1223 * the power-of-tile object size.
1225 return i915_gem_get_gtt_size(dev, size, tiling_mode);
1229 i915_gem_mmap_gtt(struct drm_file *file,
1230 struct drm_device *dev,
1234 struct drm_i915_private *dev_priv = dev->dev_private;
1235 struct drm_i915_gem_object *obj;
1238 if (!(dev->driver->driver_features & DRIVER_GEM))
1241 ret = i915_mutex_lock_interruptible(dev);
1245 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
1246 if (&obj->base == NULL) {
1251 if (obj->base.size > dev_priv->mm.gtt_mappable_end) {
1256 if (obj->madv != I915_MADV_WILLNEED) {
1257 DRM_ERROR("Attempting to mmap a purgeable buffer\n");
1262 if (!obj->base.map_list.map) {
1263 ret = drm_gem_create_mmap_offset(&obj->base);
1268 *offset = (u64)obj->base.map_list.hash.key << PAGE_SHIFT;
1271 drm_gem_object_unreference(&obj->base);
1273 mutex_unlock(&dev->struct_mutex);
1278 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
1280 * @data: GTT mapping ioctl data
1281 * @file: GEM object info
1283 * Simply returns the fake offset to userspace so it can mmap it.
1284 * The mmap call will end up in drm_gem_mmap(), which will set things
1285 * up so we can get faults in the handler above.
1287 * The fault handler will take care of binding the object into the GTT
1288 * (since it may have been evicted to make room for something), allocating
1289 * a fence register, and mapping the appropriate aperture address into
1293 i915_gem_mmap_gtt_ioctl(struct drm_device *dev, void *data,
1294 struct drm_file *file)
1296 struct drm_i915_gem_mmap_gtt *args = data;
1298 if (!(dev->driver->driver_features & DRIVER_GEM))
1301 return i915_gem_mmap_gtt(file, dev, args->handle, &args->offset);
1306 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object *obj,
1310 struct address_space *mapping;
1311 struct inode *inode;
1314 /* Get the list of pages out of our struct file. They'll be pinned
1315 * at this point until we release them.
1317 page_count = obj->base.size / PAGE_SIZE;
1318 BUG_ON(obj->pages != NULL);
1319 obj->pages = drm_malloc_ab(page_count, sizeof(struct page *));
1320 if (obj->pages == NULL)
1323 inode = obj->base.filp->f_path.dentry->d_inode;
1324 mapping = inode->i_mapping;
1325 gfpmask |= mapping_gfp_mask(mapping);
1327 for (i = 0; i < page_count; i++) {
1328 page = shmem_read_mapping_page_gfp(mapping, i, gfpmask);
1332 obj->pages[i] = page;
1335 if (i915_gem_object_needs_bit17_swizzle(obj))
1336 i915_gem_object_do_bit_17_swizzle(obj);
1342 page_cache_release(obj->pages[i]);
1344 drm_free_large(obj->pages);
1346 return PTR_ERR(page);
1350 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object *obj)
1352 int page_count = obj->base.size / PAGE_SIZE;
1355 BUG_ON(obj->madv == __I915_MADV_PURGED);
1357 if (i915_gem_object_needs_bit17_swizzle(obj))
1358 i915_gem_object_save_bit_17_swizzle(obj);
1360 if (obj->madv == I915_MADV_DONTNEED)
1363 for (i = 0; i < page_count; i++) {
1365 set_page_dirty(obj->pages[i]);
1367 if (obj->madv == I915_MADV_WILLNEED)
1368 mark_page_accessed(obj->pages[i]);
1370 page_cache_release(obj->pages[i]);
1374 drm_free_large(obj->pages);
1379 i915_gem_object_move_to_active(struct drm_i915_gem_object *obj,
1380 struct intel_ring_buffer *ring,
1383 struct drm_device *dev = obj->base.dev;
1384 struct drm_i915_private *dev_priv = dev->dev_private;
1386 BUG_ON(ring == NULL);
1389 /* Add a reference if we're newly entering the active list. */
1391 drm_gem_object_reference(&obj->base);
1395 /* Move from whatever list we were on to the tail of execution. */
1396 list_move_tail(&obj->mm_list, &dev_priv->mm.active_list);
1397 list_move_tail(&obj->ring_list, &ring->active_list);
1399 obj->last_rendering_seqno = seqno;
1401 if (obj->fenced_gpu_access) {
1402 obj->last_fenced_seqno = seqno;
1403 obj->last_fenced_ring = ring;
1405 /* Bump MRU to take account of the delayed flush */
1406 if (obj->fence_reg != I915_FENCE_REG_NONE) {
1407 struct drm_i915_fence_reg *reg;
1409 reg = &dev_priv->fence_regs[obj->fence_reg];
1410 list_move_tail(®->lru_list,
1411 &dev_priv->mm.fence_list);
1417 i915_gem_object_move_off_active(struct drm_i915_gem_object *obj)
1419 list_del_init(&obj->ring_list);
1420 obj->last_rendering_seqno = 0;
1424 i915_gem_object_move_to_flushing(struct drm_i915_gem_object *obj)
1426 struct drm_device *dev = obj->base.dev;
1427 drm_i915_private_t *dev_priv = dev->dev_private;
1429 BUG_ON(!obj->active);
1430 list_move_tail(&obj->mm_list, &dev_priv->mm.flushing_list);
1432 i915_gem_object_move_off_active(obj);
1436 i915_gem_object_move_to_inactive(struct drm_i915_gem_object *obj)
1438 struct drm_device *dev = obj->base.dev;
1439 struct drm_i915_private *dev_priv = dev->dev_private;
1441 if (obj->pin_count != 0)
1442 list_move_tail(&obj->mm_list, &dev_priv->mm.pinned_list);
1444 list_move_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
1446 BUG_ON(!list_empty(&obj->gpu_write_list));
1447 BUG_ON(!obj->active);
1450 i915_gem_object_move_off_active(obj);
1451 obj->fenced_gpu_access = false;
1454 obj->pending_gpu_write = false;
1455 drm_gem_object_unreference(&obj->base);
1457 WARN_ON(i915_verify_lists(dev));
1460 /* Immediately discard the backing storage */
1462 i915_gem_object_truncate(struct drm_i915_gem_object *obj)
1464 struct inode *inode;
1466 /* Our goal here is to return as much of the memory as
1467 * is possible back to the system as we are called from OOM.
1468 * To do this we must instruct the shmfs to drop all of its
1469 * backing pages, *now*.
1471 inode = obj->base.filp->f_path.dentry->d_inode;
1472 shmem_truncate_range(inode, 0, (loff_t)-1);
1474 if (obj->base.map_list.map)
1475 drm_gem_free_mmap_offset(&obj->base);
1477 obj->madv = __I915_MADV_PURGED;
1481 i915_gem_object_is_purgeable(struct drm_i915_gem_object *obj)
1483 return obj->madv == I915_MADV_DONTNEED;
1487 i915_gem_process_flushing_list(struct intel_ring_buffer *ring,
1488 uint32_t flush_domains)
1490 struct drm_i915_gem_object *obj, *next;
1492 list_for_each_entry_safe(obj, next,
1493 &ring->gpu_write_list,
1495 if (obj->base.write_domain & flush_domains) {
1496 uint32_t old_write_domain = obj->base.write_domain;
1498 obj->base.write_domain = 0;
1499 list_del_init(&obj->gpu_write_list);
1500 i915_gem_object_move_to_active(obj, ring,
1501 i915_gem_next_request_seqno(ring));
1503 trace_i915_gem_object_change_domain(obj,
1504 obj->base.read_domains,
1511 i915_gem_get_seqno(struct drm_device *dev)
1513 drm_i915_private_t *dev_priv = dev->dev_private;
1514 u32 seqno = dev_priv->next_seqno;
1516 /* reserve 0 for non-seqno */
1517 if (++dev_priv->next_seqno == 0)
1518 dev_priv->next_seqno = 1;
1524 i915_gem_next_request_seqno(struct intel_ring_buffer *ring)
1526 if (ring->outstanding_lazy_request == 0)
1527 ring->outstanding_lazy_request = i915_gem_get_seqno(ring->dev);
1529 return ring->outstanding_lazy_request;
1533 i915_add_request(struct intel_ring_buffer *ring,
1534 struct drm_file *file,
1535 struct drm_i915_gem_request *request)
1537 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1539 u32 request_ring_position;
1543 BUG_ON(request == NULL);
1544 seqno = i915_gem_next_request_seqno(ring);
1546 /* Record the position of the start of the request so that
1547 * should we detect the updated seqno part-way through the
1548 * GPU processing the request, we never over-estimate the
1549 * position of the head.
1551 request_ring_position = intel_ring_get_tail(ring);
1553 ret = ring->add_request(ring, &seqno);
1557 trace_i915_gem_request_add(ring, seqno);
1559 request->seqno = seqno;
1560 request->ring = ring;
1561 request->tail = request_ring_position;
1562 request->emitted_jiffies = jiffies;
1563 was_empty = list_empty(&ring->request_list);
1564 list_add_tail(&request->list, &ring->request_list);
1567 struct drm_i915_file_private *file_priv = file->driver_priv;
1569 spin_lock(&file_priv->mm.lock);
1570 request->file_priv = file_priv;
1571 list_add_tail(&request->client_list,
1572 &file_priv->mm.request_list);
1573 spin_unlock(&file_priv->mm.lock);
1576 ring->outstanding_lazy_request = 0;
1578 if (!dev_priv->mm.suspended) {
1579 if (i915_enable_hangcheck) {
1580 mod_timer(&dev_priv->hangcheck_timer,
1582 msecs_to_jiffies(DRM_I915_HANGCHECK_PERIOD));
1585 queue_delayed_work(dev_priv->wq,
1586 &dev_priv->mm.retire_work, HZ);
1592 i915_gem_request_remove_from_client(struct drm_i915_gem_request *request)
1594 struct drm_i915_file_private *file_priv = request->file_priv;
1599 spin_lock(&file_priv->mm.lock);
1600 if (request->file_priv) {
1601 list_del(&request->client_list);
1602 request->file_priv = NULL;
1604 spin_unlock(&file_priv->mm.lock);
1607 static void i915_gem_reset_ring_lists(struct drm_i915_private *dev_priv,
1608 struct intel_ring_buffer *ring)
1610 while (!list_empty(&ring->request_list)) {
1611 struct drm_i915_gem_request *request;
1613 request = list_first_entry(&ring->request_list,
1614 struct drm_i915_gem_request,
1617 list_del(&request->list);
1618 i915_gem_request_remove_from_client(request);
1622 while (!list_empty(&ring->active_list)) {
1623 struct drm_i915_gem_object *obj;
1625 obj = list_first_entry(&ring->active_list,
1626 struct drm_i915_gem_object,
1629 obj->base.write_domain = 0;
1630 list_del_init(&obj->gpu_write_list);
1631 i915_gem_object_move_to_inactive(obj);
1635 static void i915_gem_reset_fences(struct drm_device *dev)
1637 struct drm_i915_private *dev_priv = dev->dev_private;
1640 for (i = 0; i < dev_priv->num_fence_regs; i++) {
1641 struct drm_i915_fence_reg *reg = &dev_priv->fence_regs[i];
1642 struct drm_i915_gem_object *obj = reg->obj;
1647 if (obj->tiling_mode)
1648 i915_gem_release_mmap(obj);
1650 reg->obj->fence_reg = I915_FENCE_REG_NONE;
1651 reg->obj->fenced_gpu_access = false;
1652 reg->obj->last_fenced_seqno = 0;
1653 reg->obj->last_fenced_ring = NULL;
1654 i915_gem_clear_fence_reg(dev, reg);
1658 void i915_gem_reset(struct drm_device *dev)
1660 struct drm_i915_private *dev_priv = dev->dev_private;
1661 struct drm_i915_gem_object *obj;
1664 for (i = 0; i < I915_NUM_RINGS; i++)
1665 i915_gem_reset_ring_lists(dev_priv, &dev_priv->ring[i]);
1667 /* Remove anything from the flushing lists. The GPU cache is likely
1668 * to be lost on reset along with the data, so simply move the
1669 * lost bo to the inactive list.
1671 while (!list_empty(&dev_priv->mm.flushing_list)) {
1672 obj = list_first_entry(&dev_priv->mm.flushing_list,
1673 struct drm_i915_gem_object,
1676 obj->base.write_domain = 0;
1677 list_del_init(&obj->gpu_write_list);
1678 i915_gem_object_move_to_inactive(obj);
1681 /* Move everything out of the GPU domains to ensure we do any
1682 * necessary invalidation upon reuse.
1684 list_for_each_entry(obj,
1685 &dev_priv->mm.inactive_list,
1688 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
1691 /* The fence registers are invalidated so clear them out */
1692 i915_gem_reset_fences(dev);
1696 * This function clears the request list as sequence numbers are passed.
1699 i915_gem_retire_requests_ring(struct intel_ring_buffer *ring)
1704 if (list_empty(&ring->request_list))
1707 WARN_ON(i915_verify_lists(ring->dev));
1709 seqno = ring->get_seqno(ring);
1711 for (i = 0; i < ARRAY_SIZE(ring->sync_seqno); i++)
1712 if (seqno >= ring->sync_seqno[i])
1713 ring->sync_seqno[i] = 0;
1715 while (!list_empty(&ring->request_list)) {
1716 struct drm_i915_gem_request *request;
1718 request = list_first_entry(&ring->request_list,
1719 struct drm_i915_gem_request,
1722 if (!i915_seqno_passed(seqno, request->seqno))
1725 trace_i915_gem_request_retire(ring, request->seqno);
1726 /* We know the GPU must have read the request to have
1727 * sent us the seqno + interrupt, so use the position
1728 * of tail of the request to update the last known position
1731 ring->last_retired_head = request->tail;
1733 list_del(&request->list);
1734 i915_gem_request_remove_from_client(request);
1738 /* Move any buffers on the active list that are no longer referenced
1739 * by the ringbuffer to the flushing/inactive lists as appropriate.
1741 while (!list_empty(&ring->active_list)) {
1742 struct drm_i915_gem_object *obj;
1744 obj = list_first_entry(&ring->active_list,
1745 struct drm_i915_gem_object,
1748 if (!i915_seqno_passed(seqno, obj->last_rendering_seqno))
1751 if (obj->base.write_domain != 0)
1752 i915_gem_object_move_to_flushing(obj);
1754 i915_gem_object_move_to_inactive(obj);
1757 if (unlikely(ring->trace_irq_seqno &&
1758 i915_seqno_passed(seqno, ring->trace_irq_seqno))) {
1759 ring->irq_put(ring);
1760 ring->trace_irq_seqno = 0;
1763 WARN_ON(i915_verify_lists(ring->dev));
1767 i915_gem_retire_requests(struct drm_device *dev)
1769 drm_i915_private_t *dev_priv = dev->dev_private;
1772 if (!list_empty(&dev_priv->mm.deferred_free_list)) {
1773 struct drm_i915_gem_object *obj, *next;
1775 /* We must be careful that during unbind() we do not
1776 * accidentally infinitely recurse into retire requests.
1778 * retire -> free -> unbind -> wait -> retire_ring
1780 list_for_each_entry_safe(obj, next,
1781 &dev_priv->mm.deferred_free_list,
1783 i915_gem_free_object_tail(obj);
1786 for (i = 0; i < I915_NUM_RINGS; i++)
1787 i915_gem_retire_requests_ring(&dev_priv->ring[i]);
1791 i915_gem_retire_work_handler(struct work_struct *work)
1793 drm_i915_private_t *dev_priv;
1794 struct drm_device *dev;
1798 dev_priv = container_of(work, drm_i915_private_t,
1799 mm.retire_work.work);
1800 dev = dev_priv->dev;
1802 /* Come back later if the device is busy... */
1803 if (!mutex_trylock(&dev->struct_mutex)) {
1804 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1808 i915_gem_retire_requests(dev);
1810 /* Send a periodic flush down the ring so we don't hold onto GEM
1811 * objects indefinitely.
1814 for (i = 0; i < I915_NUM_RINGS; i++) {
1815 struct intel_ring_buffer *ring = &dev_priv->ring[i];
1817 if (!list_empty(&ring->gpu_write_list)) {
1818 struct drm_i915_gem_request *request;
1821 ret = i915_gem_flush_ring(ring,
1822 0, I915_GEM_GPU_DOMAINS);
1823 request = kzalloc(sizeof(*request), GFP_KERNEL);
1824 if (ret || request == NULL ||
1825 i915_add_request(ring, NULL, request))
1829 idle &= list_empty(&ring->request_list);
1832 if (!dev_priv->mm.suspended && !idle)
1833 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, HZ);
1835 mutex_unlock(&dev->struct_mutex);
1839 * Waits for a sequence number to be signaled, and cleans up the
1840 * request and object lists appropriately for that event.
1843 i915_wait_request(struct intel_ring_buffer *ring,
1847 drm_i915_private_t *dev_priv = ring->dev->dev_private;
1853 if (atomic_read(&dev_priv->mm.wedged)) {
1854 struct completion *x = &dev_priv->error_completion;
1855 bool recovery_complete;
1856 unsigned long flags;
1858 /* Give the error handler a chance to run. */
1859 spin_lock_irqsave(&x->wait.lock, flags);
1860 recovery_complete = x->done > 0;
1861 spin_unlock_irqrestore(&x->wait.lock, flags);
1863 return recovery_complete ? -EIO : -EAGAIN;
1866 if (seqno == ring->outstanding_lazy_request) {
1867 struct drm_i915_gem_request *request;
1869 request = kzalloc(sizeof(*request), GFP_KERNEL);
1870 if (request == NULL)
1873 ret = i915_add_request(ring, NULL, request);
1879 seqno = request->seqno;
1882 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
1883 if (HAS_PCH_SPLIT(ring->dev))
1884 ier = I915_READ(DEIER) | I915_READ(GTIER);
1885 else if (IS_VALLEYVIEW(ring->dev))
1886 ier = I915_READ(GTIER) | I915_READ(VLV_IER);
1888 ier = I915_READ(IER);
1890 DRM_ERROR("something (likely vbetool) disabled "
1891 "interrupts, re-enabling\n");
1892 ring->dev->driver->irq_preinstall(ring->dev);
1893 ring->dev->driver->irq_postinstall(ring->dev);
1896 trace_i915_gem_request_wait_begin(ring, seqno);
1898 ring->waiting_seqno = seqno;
1899 if (ring->irq_get(ring)) {
1900 if (dev_priv->mm.interruptible)
1901 ret = wait_event_interruptible(ring->irq_queue,
1902 i915_seqno_passed(ring->get_seqno(ring), seqno)
1903 || atomic_read(&dev_priv->mm.wedged));
1905 wait_event(ring->irq_queue,
1906 i915_seqno_passed(ring->get_seqno(ring), seqno)
1907 || atomic_read(&dev_priv->mm.wedged));
1909 ring->irq_put(ring);
1910 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
1912 atomic_read(&dev_priv->mm.wedged), 3000))
1914 ring->waiting_seqno = 0;
1916 trace_i915_gem_request_wait_end(ring, seqno);
1918 if (atomic_read(&dev_priv->mm.wedged))
1921 /* Directly dispatch request retiring. While we have the work queue
1922 * to handle this, the waiter on a request often wants an associated
1923 * buffer to have made it to the inactive list, and we would need
1924 * a separate wait queue to handle that.
1926 if (ret == 0 && do_retire)
1927 i915_gem_retire_requests_ring(ring);
1933 * Ensures that all rendering to the object has completed and the object is
1934 * safe to unbind from the GTT or access from the CPU.
1937 i915_gem_object_wait_rendering(struct drm_i915_gem_object *obj)
1941 /* This function only exists to support waiting for existing rendering,
1942 * not for emitting required flushes.
1944 BUG_ON((obj->base.write_domain & I915_GEM_GPU_DOMAINS) != 0);
1946 /* If there is rendering queued on the buffer being evicted, wait for
1950 ret = i915_wait_request(obj->ring, obj->last_rendering_seqno,
1959 static void i915_gem_object_finish_gtt(struct drm_i915_gem_object *obj)
1961 u32 old_write_domain, old_read_domains;
1963 /* Act a barrier for all accesses through the GTT */
1966 /* Force a pagefault for domain tracking on next user access */
1967 i915_gem_release_mmap(obj);
1969 if ((obj->base.read_domains & I915_GEM_DOMAIN_GTT) == 0)
1972 old_read_domains = obj->base.read_domains;
1973 old_write_domain = obj->base.write_domain;
1975 obj->base.read_domains &= ~I915_GEM_DOMAIN_GTT;
1976 obj->base.write_domain &= ~I915_GEM_DOMAIN_GTT;
1978 trace_i915_gem_object_change_domain(obj,
1984 * Unbinds an object from the GTT aperture.
1987 i915_gem_object_unbind(struct drm_i915_gem_object *obj)
1989 drm_i915_private_t *dev_priv = obj->base.dev->dev_private;
1992 if (obj->gtt_space == NULL)
1995 if (obj->pin_count != 0) {
1996 DRM_ERROR("Attempting to unbind pinned buffer\n");
2000 ret = i915_gem_object_finish_gpu(obj);
2001 if (ret == -ERESTARTSYS)
2003 /* Continue on if we fail due to EIO, the GPU is hung so we
2004 * should be safe and we need to cleanup or else we might
2005 * cause memory corruption through use-after-free.
2008 i915_gem_object_finish_gtt(obj);
2010 /* Move the object to the CPU domain to ensure that
2011 * any possible CPU writes while it's not in the GTT
2012 * are flushed when we go to remap it.
2015 ret = i915_gem_object_set_to_cpu_domain(obj, 1);
2016 if (ret == -ERESTARTSYS)
2019 /* In the event of a disaster, abandon all caches and
2020 * hope for the best.
2022 i915_gem_clflush_object(obj);
2023 obj->base.read_domains = obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2026 /* release the fence reg _after_ flushing */
2027 ret = i915_gem_object_put_fence(obj);
2028 if (ret == -ERESTARTSYS)
2031 trace_i915_gem_object_unbind(obj);
2033 if (obj->has_global_gtt_mapping)
2034 i915_gem_gtt_unbind_object(obj);
2035 if (obj->has_aliasing_ppgtt_mapping) {
2036 i915_ppgtt_unbind_object(dev_priv->mm.aliasing_ppgtt, obj);
2037 obj->has_aliasing_ppgtt_mapping = 0;
2039 i915_gem_gtt_finish_object(obj);
2041 i915_gem_object_put_pages_gtt(obj);
2043 list_del_init(&obj->gtt_list);
2044 list_del_init(&obj->mm_list);
2045 /* Avoid an unnecessary call to unbind on rebind. */
2046 obj->map_and_fenceable = true;
2048 drm_mm_put_block(obj->gtt_space);
2049 obj->gtt_space = NULL;
2050 obj->gtt_offset = 0;
2052 if (i915_gem_object_is_purgeable(obj))
2053 i915_gem_object_truncate(obj);
2059 i915_gem_flush_ring(struct intel_ring_buffer *ring,
2060 uint32_t invalidate_domains,
2061 uint32_t flush_domains)
2065 if (((invalidate_domains | flush_domains) & I915_GEM_GPU_DOMAINS) == 0)
2068 trace_i915_gem_ring_flush(ring, invalidate_domains, flush_domains);
2070 ret = ring->flush(ring, invalidate_domains, flush_domains);
2074 if (flush_domains & I915_GEM_GPU_DOMAINS)
2075 i915_gem_process_flushing_list(ring, flush_domains);
2080 static int i915_ring_idle(struct intel_ring_buffer *ring, bool do_retire)
2084 if (list_empty(&ring->gpu_write_list) && list_empty(&ring->active_list))
2087 if (!list_empty(&ring->gpu_write_list)) {
2088 ret = i915_gem_flush_ring(ring,
2089 I915_GEM_GPU_DOMAINS, I915_GEM_GPU_DOMAINS);
2094 return i915_wait_request(ring, i915_gem_next_request_seqno(ring),
2098 int i915_gpu_idle(struct drm_device *dev, bool do_retire)
2100 drm_i915_private_t *dev_priv = dev->dev_private;
2103 /* Flush everything onto the inactive list. */
2104 for (i = 0; i < I915_NUM_RINGS; i++) {
2105 ret = i915_ring_idle(&dev_priv->ring[i], do_retire);
2113 static int sandybridge_write_fence_reg(struct drm_i915_gem_object *obj,
2114 struct intel_ring_buffer *pipelined)
2116 struct drm_device *dev = obj->base.dev;
2117 drm_i915_private_t *dev_priv = dev->dev_private;
2118 u32 size = obj->gtt_space->size;
2119 int regnum = obj->fence_reg;
2122 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2124 val |= obj->gtt_offset & 0xfffff000;
2125 val |= (uint64_t)((obj->stride / 128) - 1) <<
2126 SANDYBRIDGE_FENCE_PITCH_SHIFT;
2128 if (obj->tiling_mode == I915_TILING_Y)
2129 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2130 val |= I965_FENCE_REG_VALID;
2133 int ret = intel_ring_begin(pipelined, 6);
2137 intel_ring_emit(pipelined, MI_NOOP);
2138 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2139 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8);
2140 intel_ring_emit(pipelined, (u32)val);
2141 intel_ring_emit(pipelined, FENCE_REG_SANDYBRIDGE_0 + regnum*8 + 4);
2142 intel_ring_emit(pipelined, (u32)(val >> 32));
2143 intel_ring_advance(pipelined);
2145 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + regnum * 8, val);
2150 static int i965_write_fence_reg(struct drm_i915_gem_object *obj,
2151 struct intel_ring_buffer *pipelined)
2153 struct drm_device *dev = obj->base.dev;
2154 drm_i915_private_t *dev_priv = dev->dev_private;
2155 u32 size = obj->gtt_space->size;
2156 int regnum = obj->fence_reg;
2159 val = (uint64_t)((obj->gtt_offset + size - 4096) &
2161 val |= obj->gtt_offset & 0xfffff000;
2162 val |= ((obj->stride / 128) - 1) << I965_FENCE_PITCH_SHIFT;
2163 if (obj->tiling_mode == I915_TILING_Y)
2164 val |= 1 << I965_FENCE_TILING_Y_SHIFT;
2165 val |= I965_FENCE_REG_VALID;
2168 int ret = intel_ring_begin(pipelined, 6);
2172 intel_ring_emit(pipelined, MI_NOOP);
2173 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(2));
2174 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8);
2175 intel_ring_emit(pipelined, (u32)val);
2176 intel_ring_emit(pipelined, FENCE_REG_965_0 + regnum*8 + 4);
2177 intel_ring_emit(pipelined, (u32)(val >> 32));
2178 intel_ring_advance(pipelined);
2180 I915_WRITE64(FENCE_REG_965_0 + regnum * 8, val);
2185 static int i915_write_fence_reg(struct drm_i915_gem_object *obj,
2186 struct intel_ring_buffer *pipelined)
2188 struct drm_device *dev = obj->base.dev;
2189 drm_i915_private_t *dev_priv = dev->dev_private;
2190 u32 size = obj->gtt_space->size;
2191 u32 fence_reg, val, pitch_val;
2194 if (WARN((obj->gtt_offset & ~I915_FENCE_START_MASK) ||
2195 (size & -size) != size ||
2196 (obj->gtt_offset & (size - 1)),
2197 "object 0x%08x [fenceable? %d] not 1M or pot-size (0x%08x) aligned\n",
2198 obj->gtt_offset, obj->map_and_fenceable, size))
2201 if (obj->tiling_mode == I915_TILING_Y && HAS_128_BYTE_Y_TILING(dev))
2206 /* Note: pitch better be a power of two tile widths */
2207 pitch_val = obj->stride / tile_width;
2208 pitch_val = ffs(pitch_val) - 1;
2210 val = obj->gtt_offset;
2211 if (obj->tiling_mode == I915_TILING_Y)
2212 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2213 val |= I915_FENCE_SIZE_BITS(size);
2214 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2215 val |= I830_FENCE_REG_VALID;
2217 fence_reg = obj->fence_reg;
2219 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2221 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2224 int ret = intel_ring_begin(pipelined, 4);
2228 intel_ring_emit(pipelined, MI_NOOP);
2229 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2230 intel_ring_emit(pipelined, fence_reg);
2231 intel_ring_emit(pipelined, val);
2232 intel_ring_advance(pipelined);
2234 I915_WRITE(fence_reg, val);
2239 static int i830_write_fence_reg(struct drm_i915_gem_object *obj,
2240 struct intel_ring_buffer *pipelined)
2242 struct drm_device *dev = obj->base.dev;
2243 drm_i915_private_t *dev_priv = dev->dev_private;
2244 u32 size = obj->gtt_space->size;
2245 int regnum = obj->fence_reg;
2249 if (WARN((obj->gtt_offset & ~I830_FENCE_START_MASK) ||
2250 (size & -size) != size ||
2251 (obj->gtt_offset & (size - 1)),
2252 "object 0x%08x not 512K or pot-size 0x%08x aligned\n",
2253 obj->gtt_offset, size))
2256 pitch_val = obj->stride / 128;
2257 pitch_val = ffs(pitch_val) - 1;
2259 val = obj->gtt_offset;
2260 if (obj->tiling_mode == I915_TILING_Y)
2261 val |= 1 << I830_FENCE_TILING_Y_SHIFT;
2262 val |= I830_FENCE_SIZE_BITS(size);
2263 val |= pitch_val << I830_FENCE_PITCH_SHIFT;
2264 val |= I830_FENCE_REG_VALID;
2267 int ret = intel_ring_begin(pipelined, 4);
2271 intel_ring_emit(pipelined, MI_NOOP);
2272 intel_ring_emit(pipelined, MI_LOAD_REGISTER_IMM(1));
2273 intel_ring_emit(pipelined, FENCE_REG_830_0 + regnum*4);
2274 intel_ring_emit(pipelined, val);
2275 intel_ring_advance(pipelined);
2277 I915_WRITE(FENCE_REG_830_0 + regnum * 4, val);
2282 static bool ring_passed_seqno(struct intel_ring_buffer *ring, u32 seqno)
2284 return i915_seqno_passed(ring->get_seqno(ring), seqno);
2288 i915_gem_object_flush_fence(struct drm_i915_gem_object *obj,
2289 struct intel_ring_buffer *pipelined)
2293 if (obj->fenced_gpu_access) {
2294 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
2295 ret = i915_gem_flush_ring(obj->last_fenced_ring,
2296 0, obj->base.write_domain);
2301 obj->fenced_gpu_access = false;
2304 if (obj->last_fenced_seqno && pipelined != obj->last_fenced_ring) {
2305 if (!ring_passed_seqno(obj->last_fenced_ring,
2306 obj->last_fenced_seqno)) {
2307 ret = i915_wait_request(obj->last_fenced_ring,
2308 obj->last_fenced_seqno,
2314 obj->last_fenced_seqno = 0;
2315 obj->last_fenced_ring = NULL;
2318 /* Ensure that all CPU reads are completed before installing a fence
2319 * and all writes before removing the fence.
2321 if (obj->base.read_domains & I915_GEM_DOMAIN_GTT)
2328 i915_gem_object_put_fence(struct drm_i915_gem_object *obj)
2332 if (obj->tiling_mode)
2333 i915_gem_release_mmap(obj);
2335 ret = i915_gem_object_flush_fence(obj, NULL);
2339 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2340 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2342 WARN_ON(dev_priv->fence_regs[obj->fence_reg].pin_count);
2343 i915_gem_clear_fence_reg(obj->base.dev,
2344 &dev_priv->fence_regs[obj->fence_reg]);
2346 obj->fence_reg = I915_FENCE_REG_NONE;
2352 static struct drm_i915_fence_reg *
2353 i915_find_fence_reg(struct drm_device *dev,
2354 struct intel_ring_buffer *pipelined)
2356 struct drm_i915_private *dev_priv = dev->dev_private;
2357 struct drm_i915_fence_reg *reg, *first, *avail;
2360 /* First try to find a free reg */
2362 for (i = dev_priv->fence_reg_start; i < dev_priv->num_fence_regs; i++) {
2363 reg = &dev_priv->fence_regs[i];
2367 if (!reg->pin_count)
2374 /* None available, try to steal one or wait for a user to finish */
2375 avail = first = NULL;
2376 list_for_each_entry(reg, &dev_priv->mm.fence_list, lru_list) {
2384 !reg->obj->last_fenced_ring ||
2385 reg->obj->last_fenced_ring == pipelined) {
2398 * i915_gem_object_get_fence - set up a fence reg for an object
2399 * @obj: object to map through a fence reg
2400 * @pipelined: ring on which to queue the change, or NULL for CPU access
2401 * @interruptible: must we wait uninterruptibly for the register to retire?
2403 * When mapping objects through the GTT, userspace wants to be able to write
2404 * to them without having to worry about swizzling if the object is tiled.
2406 * This function walks the fence regs looking for a free one for @obj,
2407 * stealing one if it can't find any.
2409 * It then sets up the reg based on the object's properties: address, pitch
2410 * and tiling format.
2413 i915_gem_object_get_fence(struct drm_i915_gem_object *obj,
2414 struct intel_ring_buffer *pipelined)
2416 struct drm_device *dev = obj->base.dev;
2417 struct drm_i915_private *dev_priv = dev->dev_private;
2418 struct drm_i915_fence_reg *reg;
2421 /* XXX disable pipelining. There are bugs. Shocking. */
2424 /* Just update our place in the LRU if our fence is getting reused. */
2425 if (obj->fence_reg != I915_FENCE_REG_NONE) {
2426 reg = &dev_priv->fence_regs[obj->fence_reg];
2427 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2429 if (obj->tiling_changed) {
2430 ret = i915_gem_object_flush_fence(obj, pipelined);
2434 if (!obj->fenced_gpu_access && !obj->last_fenced_seqno)
2439 i915_gem_next_request_seqno(pipelined);
2440 obj->last_fenced_seqno = reg->setup_seqno;
2441 obj->last_fenced_ring = pipelined;
2448 if (reg->setup_seqno) {
2449 if (!ring_passed_seqno(obj->last_fenced_ring,
2450 reg->setup_seqno)) {
2451 ret = i915_wait_request(obj->last_fenced_ring,
2458 reg->setup_seqno = 0;
2460 } else if (obj->last_fenced_ring &&
2461 obj->last_fenced_ring != pipelined) {
2462 ret = i915_gem_object_flush_fence(obj, pipelined);
2470 reg = i915_find_fence_reg(dev, pipelined);
2474 ret = i915_gem_object_flush_fence(obj, pipelined);
2479 struct drm_i915_gem_object *old = reg->obj;
2481 drm_gem_object_reference(&old->base);
2483 if (old->tiling_mode)
2484 i915_gem_release_mmap(old);
2486 ret = i915_gem_object_flush_fence(old, pipelined);
2488 drm_gem_object_unreference(&old->base);
2492 if (old->last_fenced_seqno == 0 && obj->last_fenced_seqno == 0)
2495 old->fence_reg = I915_FENCE_REG_NONE;
2496 old->last_fenced_ring = pipelined;
2497 old->last_fenced_seqno =
2498 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2500 drm_gem_object_unreference(&old->base);
2501 } else if (obj->last_fenced_seqno == 0)
2505 list_move_tail(®->lru_list, &dev_priv->mm.fence_list);
2506 obj->fence_reg = reg - dev_priv->fence_regs;
2507 obj->last_fenced_ring = pipelined;
2510 pipelined ? i915_gem_next_request_seqno(pipelined) : 0;
2511 obj->last_fenced_seqno = reg->setup_seqno;
2514 obj->tiling_changed = false;
2515 switch (INTEL_INFO(dev)->gen) {
2518 ret = sandybridge_write_fence_reg(obj, pipelined);
2522 ret = i965_write_fence_reg(obj, pipelined);
2525 ret = i915_write_fence_reg(obj, pipelined);
2528 ret = i830_write_fence_reg(obj, pipelined);
2536 * i915_gem_clear_fence_reg - clear out fence register info
2537 * @obj: object to clear
2539 * Zeroes out the fence register itself and clears out the associated
2540 * data structures in dev_priv and obj.
2543 i915_gem_clear_fence_reg(struct drm_device *dev,
2544 struct drm_i915_fence_reg *reg)
2546 drm_i915_private_t *dev_priv = dev->dev_private;
2547 uint32_t fence_reg = reg - dev_priv->fence_regs;
2549 switch (INTEL_INFO(dev)->gen) {
2552 I915_WRITE64(FENCE_REG_SANDYBRIDGE_0 + fence_reg*8, 0);
2556 I915_WRITE64(FENCE_REG_965_0 + fence_reg*8, 0);
2560 fence_reg = FENCE_REG_945_8 + (fence_reg - 8) * 4;
2563 fence_reg = FENCE_REG_830_0 + fence_reg * 4;
2565 I915_WRITE(fence_reg, 0);
2569 list_del_init(®->lru_list);
2571 reg->setup_seqno = 0;
2576 * Finds free space in the GTT aperture and binds the object there.
2579 i915_gem_object_bind_to_gtt(struct drm_i915_gem_object *obj,
2581 bool map_and_fenceable)
2583 struct drm_device *dev = obj->base.dev;
2584 drm_i915_private_t *dev_priv = dev->dev_private;
2585 struct drm_mm_node *free_space;
2586 gfp_t gfpmask = __GFP_NORETRY | __GFP_NOWARN;
2587 u32 size, fence_size, fence_alignment, unfenced_alignment;
2588 bool mappable, fenceable;
2591 if (obj->madv != I915_MADV_WILLNEED) {
2592 DRM_ERROR("Attempting to bind a purgeable object\n");
2596 fence_size = i915_gem_get_gtt_size(dev,
2599 fence_alignment = i915_gem_get_gtt_alignment(dev,
2602 unfenced_alignment =
2603 i915_gem_get_unfenced_gtt_alignment(dev,
2608 alignment = map_and_fenceable ? fence_alignment :
2610 if (map_and_fenceable && alignment & (fence_alignment - 1)) {
2611 DRM_ERROR("Invalid object alignment requested %u\n", alignment);
2615 size = map_and_fenceable ? fence_size : obj->base.size;
2617 /* If the object is bigger than the entire aperture, reject it early
2618 * before evicting everything in a vain attempt to find space.
2620 if (obj->base.size >
2621 (map_and_fenceable ? dev_priv->mm.gtt_mappable_end : dev_priv->mm.gtt_total)) {
2622 DRM_ERROR("Attempting to bind an object larger than the aperture\n");
2627 if (map_and_fenceable)
2629 drm_mm_search_free_in_range(&dev_priv->mm.gtt_space,
2631 dev_priv->mm.gtt_mappable_end,
2634 free_space = drm_mm_search_free(&dev_priv->mm.gtt_space,
2635 size, alignment, 0);
2637 if (free_space != NULL) {
2638 if (map_and_fenceable)
2640 drm_mm_get_block_range_generic(free_space,
2642 dev_priv->mm.gtt_mappable_end,
2646 drm_mm_get_block(free_space, size, alignment);
2648 if (obj->gtt_space == NULL) {
2649 /* If the gtt is empty and we're still having trouble
2650 * fitting our object in, we're out of memory.
2652 ret = i915_gem_evict_something(dev, size, alignment,
2660 ret = i915_gem_object_get_pages_gtt(obj, gfpmask);
2662 drm_mm_put_block(obj->gtt_space);
2663 obj->gtt_space = NULL;
2665 if (ret == -ENOMEM) {
2666 /* first try to reclaim some memory by clearing the GTT */
2667 ret = i915_gem_evict_everything(dev, false);
2669 /* now try to shrink everyone else */
2684 ret = i915_gem_gtt_prepare_object(obj);
2686 i915_gem_object_put_pages_gtt(obj);
2687 drm_mm_put_block(obj->gtt_space);
2688 obj->gtt_space = NULL;
2690 if (i915_gem_evict_everything(dev, false))
2696 if (!dev_priv->mm.aliasing_ppgtt)
2697 i915_gem_gtt_bind_object(obj, obj->cache_level);
2699 list_add_tail(&obj->gtt_list, &dev_priv->mm.gtt_list);
2700 list_add_tail(&obj->mm_list, &dev_priv->mm.inactive_list);
2702 /* Assert that the object is not currently in any GPU domain. As it
2703 * wasn't in the GTT, there shouldn't be any way it could have been in
2706 BUG_ON(obj->base.read_domains & I915_GEM_GPU_DOMAINS);
2707 BUG_ON(obj->base.write_domain & I915_GEM_GPU_DOMAINS);
2709 obj->gtt_offset = obj->gtt_space->start;
2712 obj->gtt_space->size == fence_size &&
2713 (obj->gtt_space->start & (fence_alignment - 1)) == 0;
2716 obj->gtt_offset + obj->base.size <= dev_priv->mm.gtt_mappable_end;
2718 obj->map_and_fenceable = mappable && fenceable;
2720 trace_i915_gem_object_bind(obj, map_and_fenceable);
2725 i915_gem_clflush_object(struct drm_i915_gem_object *obj)
2727 /* If we don't have a page list set up, then we're not pinned
2728 * to GPU, and we can ignore the cache flush because it'll happen
2729 * again at bind time.
2731 if (obj->pages == NULL)
2734 /* If the GPU is snooping the contents of the CPU cache,
2735 * we do not need to manually clear the CPU cache lines. However,
2736 * the caches are only snooped when the render cache is
2737 * flushed/invalidated. As we always have to emit invalidations
2738 * and flushes when moving into and out of the RENDER domain, correct
2739 * snooping behaviour occurs naturally as the result of our domain
2742 if (obj->cache_level != I915_CACHE_NONE)
2745 trace_i915_gem_object_clflush(obj);
2747 drm_clflush_pages(obj->pages, obj->base.size / PAGE_SIZE);
2750 /** Flushes any GPU write domain for the object if it's dirty. */
2752 i915_gem_object_flush_gpu_write_domain(struct drm_i915_gem_object *obj)
2754 if ((obj->base.write_domain & I915_GEM_GPU_DOMAINS) == 0)
2757 /* Queue the GPU write cache flushing we need. */
2758 return i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
2761 /** Flushes the GTT write domain for the object if it's dirty. */
2763 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object *obj)
2765 uint32_t old_write_domain;
2767 if (obj->base.write_domain != I915_GEM_DOMAIN_GTT)
2770 /* No actual flushing is required for the GTT write domain. Writes
2771 * to it immediately go to main memory as far as we know, so there's
2772 * no chipset flush. It also doesn't land in render cache.
2774 * However, we do have to enforce the order so that all writes through
2775 * the GTT land before any writes to the device, such as updates to
2780 old_write_domain = obj->base.write_domain;
2781 obj->base.write_domain = 0;
2783 trace_i915_gem_object_change_domain(obj,
2784 obj->base.read_domains,
2788 /** Flushes the CPU write domain for the object if it's dirty. */
2790 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object *obj)
2792 uint32_t old_write_domain;
2794 if (obj->base.write_domain != I915_GEM_DOMAIN_CPU)
2797 i915_gem_clflush_object(obj);
2798 intel_gtt_chipset_flush();
2799 old_write_domain = obj->base.write_domain;
2800 obj->base.write_domain = 0;
2802 trace_i915_gem_object_change_domain(obj,
2803 obj->base.read_domains,
2808 * Moves a single object to the GTT read, and possibly write domain.
2810 * This function returns when the move is complete, including waiting on
2814 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write)
2816 uint32_t old_write_domain, old_read_domains;
2819 /* Not valid to be called on unbound objects. */
2820 if (obj->gtt_space == NULL)
2823 if (obj->base.write_domain == I915_GEM_DOMAIN_GTT)
2826 ret = i915_gem_object_flush_gpu_write_domain(obj);
2830 if (obj->pending_gpu_write || write) {
2831 ret = i915_gem_object_wait_rendering(obj);
2836 i915_gem_object_flush_cpu_write_domain(obj);
2838 old_write_domain = obj->base.write_domain;
2839 old_read_domains = obj->base.read_domains;
2841 /* It should now be out of any other write domains, and we can update
2842 * the domain values for our changes.
2844 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2845 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2847 obj->base.read_domains = I915_GEM_DOMAIN_GTT;
2848 obj->base.write_domain = I915_GEM_DOMAIN_GTT;
2852 trace_i915_gem_object_change_domain(obj,
2859 int i915_gem_object_set_cache_level(struct drm_i915_gem_object *obj,
2860 enum i915_cache_level cache_level)
2862 struct drm_device *dev = obj->base.dev;
2863 drm_i915_private_t *dev_priv = dev->dev_private;
2866 if (obj->cache_level == cache_level)
2869 if (obj->pin_count) {
2870 DRM_DEBUG("can not change the cache level of pinned objects\n");
2874 if (obj->gtt_space) {
2875 ret = i915_gem_object_finish_gpu(obj);
2879 i915_gem_object_finish_gtt(obj);
2881 /* Before SandyBridge, you could not use tiling or fence
2882 * registers with snooped memory, so relinquish any fences
2883 * currently pointing to our region in the aperture.
2885 if (INTEL_INFO(obj->base.dev)->gen < 6) {
2886 ret = i915_gem_object_put_fence(obj);
2891 if (obj->has_global_gtt_mapping)
2892 i915_gem_gtt_bind_object(obj, cache_level);
2893 if (obj->has_aliasing_ppgtt_mapping)
2894 i915_ppgtt_bind_object(dev_priv->mm.aliasing_ppgtt,
2898 if (cache_level == I915_CACHE_NONE) {
2899 u32 old_read_domains, old_write_domain;
2901 /* If we're coming from LLC cached, then we haven't
2902 * actually been tracking whether the data is in the
2903 * CPU cache or not, since we only allow one bit set
2904 * in obj->write_domain and have been skipping the clflushes.
2905 * Just set it to the CPU cache for now.
2907 WARN_ON(obj->base.write_domain & ~I915_GEM_DOMAIN_CPU);
2908 WARN_ON(obj->base.read_domains & ~I915_GEM_DOMAIN_CPU);
2910 old_read_domains = obj->base.read_domains;
2911 old_write_domain = obj->base.write_domain;
2913 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
2914 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
2916 trace_i915_gem_object_change_domain(obj,
2921 obj->cache_level = cache_level;
2926 * Prepare buffer for display plane (scanout, cursors, etc).
2927 * Can be called from an uninterruptible phase (modesetting) and allows
2928 * any flushes to be pipelined (for pageflips).
2930 * For the display plane, we want to be in the GTT but out of any write
2931 * domains. So in many ways this looks like set_to_gtt_domain() apart from the
2932 * ability to pipeline the waits, pinning and any additional subtleties
2933 * that may differentiate the display plane from ordinary buffers.
2936 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
2938 struct intel_ring_buffer *pipelined)
2940 u32 old_read_domains, old_write_domain;
2943 ret = i915_gem_object_flush_gpu_write_domain(obj);
2947 if (pipelined != obj->ring) {
2948 ret = i915_gem_object_wait_rendering(obj);
2949 if (ret == -ERESTARTSYS)
2953 /* The display engine is not coherent with the LLC cache on gen6. As
2954 * a result, we make sure that the pinning that is about to occur is
2955 * done with uncached PTEs. This is lowest common denominator for all
2958 * However for gen6+, we could do better by using the GFDT bit instead
2959 * of uncaching, which would allow us to flush all the LLC-cached data
2960 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
2962 ret = i915_gem_object_set_cache_level(obj, I915_CACHE_NONE);
2966 /* As the user may map the buffer once pinned in the display plane
2967 * (e.g. libkms for the bootup splash), we have to ensure that we
2968 * always use map_and_fenceable for all scanout buffers.
2970 ret = i915_gem_object_pin(obj, alignment, true);
2974 i915_gem_object_flush_cpu_write_domain(obj);
2976 old_write_domain = obj->base.write_domain;
2977 old_read_domains = obj->base.read_domains;
2979 /* It should now be out of any other write domains, and we can update
2980 * the domain values for our changes.
2982 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_GTT) != 0);
2983 obj->base.read_domains |= I915_GEM_DOMAIN_GTT;
2985 trace_i915_gem_object_change_domain(obj,
2993 i915_gem_object_finish_gpu(struct drm_i915_gem_object *obj)
2997 if ((obj->base.read_domains & I915_GEM_GPU_DOMAINS) == 0)
3000 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3001 ret = i915_gem_flush_ring(obj->ring, 0, obj->base.write_domain);
3006 ret = i915_gem_object_wait_rendering(obj);
3010 /* Ensure that we invalidate the GPU's caches and TLBs. */
3011 obj->base.read_domains &= ~I915_GEM_GPU_DOMAINS;
3016 * Moves a single object to the CPU read, and possibly write domain.
3018 * This function returns when the move is complete, including waiting on
3022 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write)
3024 uint32_t old_write_domain, old_read_domains;
3027 if (obj->base.write_domain == I915_GEM_DOMAIN_CPU)
3030 ret = i915_gem_object_flush_gpu_write_domain(obj);
3034 ret = i915_gem_object_wait_rendering(obj);
3038 i915_gem_object_flush_gtt_write_domain(obj);
3040 old_write_domain = obj->base.write_domain;
3041 old_read_domains = obj->base.read_domains;
3043 /* Flush the CPU cache if it's still invalid. */
3044 if ((obj->base.read_domains & I915_GEM_DOMAIN_CPU) == 0) {
3045 i915_gem_clflush_object(obj);
3047 obj->base.read_domains |= I915_GEM_DOMAIN_CPU;
3050 /* It should now be out of any other write domains, and we can update
3051 * the domain values for our changes.
3053 BUG_ON((obj->base.write_domain & ~I915_GEM_DOMAIN_CPU) != 0);
3055 /* If we're writing through the CPU, then the GPU read domains will
3056 * need to be invalidated at next use.
3059 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3060 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3063 trace_i915_gem_object_change_domain(obj,
3070 /* Throttle our rendering by waiting until the ring has completed our requests
3071 * emitted over 20 msec ago.
3073 * Note that if we were to use the current jiffies each time around the loop,
3074 * we wouldn't escape the function with any frames outstanding if the time to
3075 * render a frame was over 20ms.
3077 * This should get us reasonable parallelism between CPU and GPU but also
3078 * relatively low latency when blocking on a particular request to finish.
3081 i915_gem_ring_throttle(struct drm_device *dev, struct drm_file *file)
3083 struct drm_i915_private *dev_priv = dev->dev_private;
3084 struct drm_i915_file_private *file_priv = file->driver_priv;
3085 unsigned long recent_enough = jiffies - msecs_to_jiffies(20);
3086 struct drm_i915_gem_request *request;
3087 struct intel_ring_buffer *ring = NULL;
3091 if (atomic_read(&dev_priv->mm.wedged))
3094 spin_lock(&file_priv->mm.lock);
3095 list_for_each_entry(request, &file_priv->mm.request_list, client_list) {
3096 if (time_after_eq(request->emitted_jiffies, recent_enough))
3099 ring = request->ring;
3100 seqno = request->seqno;
3102 spin_unlock(&file_priv->mm.lock);
3108 if (!i915_seqno_passed(ring->get_seqno(ring), seqno)) {
3109 /* And wait for the seqno passing without holding any locks and
3110 * causing extra latency for others. This is safe as the irq
3111 * generation is designed to be run atomically and so is
3114 if (ring->irq_get(ring)) {
3115 ret = wait_event_interruptible(ring->irq_queue,
3116 i915_seqno_passed(ring->get_seqno(ring), seqno)
3117 || atomic_read(&dev_priv->mm.wedged));
3118 ring->irq_put(ring);
3120 if (ret == 0 && atomic_read(&dev_priv->mm.wedged))
3122 } else if (wait_for_atomic(i915_seqno_passed(ring->get_seqno(ring),
3124 atomic_read(&dev_priv->mm.wedged), 3000)) {
3130 queue_delayed_work(dev_priv->wq, &dev_priv->mm.retire_work, 0);
3136 i915_gem_object_pin(struct drm_i915_gem_object *obj,
3138 bool map_and_fenceable)
3140 struct drm_device *dev = obj->base.dev;
3141 struct drm_i915_private *dev_priv = dev->dev_private;
3144 BUG_ON(obj->pin_count == DRM_I915_GEM_OBJECT_MAX_PIN_COUNT);
3145 WARN_ON(i915_verify_lists(dev));
3147 if (obj->gtt_space != NULL) {
3148 if ((alignment && obj->gtt_offset & (alignment - 1)) ||
3149 (map_and_fenceable && !obj->map_and_fenceable)) {
3150 WARN(obj->pin_count,
3151 "bo is already pinned with incorrect alignment:"
3152 " offset=%x, req.alignment=%x, req.map_and_fenceable=%d,"
3153 " obj->map_and_fenceable=%d\n",
3154 obj->gtt_offset, alignment,
3156 obj->map_and_fenceable);
3157 ret = i915_gem_object_unbind(obj);
3163 if (obj->gtt_space == NULL) {
3164 ret = i915_gem_object_bind_to_gtt(obj, alignment,
3170 if (!obj->has_global_gtt_mapping && map_and_fenceable)
3171 i915_gem_gtt_bind_object(obj, obj->cache_level);
3173 if (obj->pin_count++ == 0) {
3175 list_move_tail(&obj->mm_list,
3176 &dev_priv->mm.pinned_list);
3178 obj->pin_mappable |= map_and_fenceable;
3180 WARN_ON(i915_verify_lists(dev));
3185 i915_gem_object_unpin(struct drm_i915_gem_object *obj)
3187 struct drm_device *dev = obj->base.dev;
3188 drm_i915_private_t *dev_priv = dev->dev_private;
3190 WARN_ON(i915_verify_lists(dev));
3191 BUG_ON(obj->pin_count == 0);
3192 BUG_ON(obj->gtt_space == NULL);
3194 if (--obj->pin_count == 0) {
3196 list_move_tail(&obj->mm_list,
3197 &dev_priv->mm.inactive_list);
3198 obj->pin_mappable = false;
3200 WARN_ON(i915_verify_lists(dev));
3204 i915_gem_pin_ioctl(struct drm_device *dev, void *data,
3205 struct drm_file *file)
3207 struct drm_i915_gem_pin *args = data;
3208 struct drm_i915_gem_object *obj;
3211 ret = i915_mutex_lock_interruptible(dev);
3215 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3216 if (&obj->base == NULL) {
3221 if (obj->madv != I915_MADV_WILLNEED) {
3222 DRM_ERROR("Attempting to pin a purgeable buffer\n");
3227 if (obj->pin_filp != NULL && obj->pin_filp != file) {
3228 DRM_ERROR("Already pinned in i915_gem_pin_ioctl(): %d\n",
3234 obj->user_pin_count++;
3235 obj->pin_filp = file;
3236 if (obj->user_pin_count == 1) {
3237 ret = i915_gem_object_pin(obj, args->alignment, true);
3242 /* XXX - flush the CPU caches for pinned objects
3243 * as the X server doesn't manage domains yet
3245 i915_gem_object_flush_cpu_write_domain(obj);
3246 args->offset = obj->gtt_offset;
3248 drm_gem_object_unreference(&obj->base);
3250 mutex_unlock(&dev->struct_mutex);
3255 i915_gem_unpin_ioctl(struct drm_device *dev, void *data,
3256 struct drm_file *file)
3258 struct drm_i915_gem_pin *args = data;
3259 struct drm_i915_gem_object *obj;
3262 ret = i915_mutex_lock_interruptible(dev);
3266 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3267 if (&obj->base == NULL) {
3272 if (obj->pin_filp != file) {
3273 DRM_ERROR("Not pinned by caller in i915_gem_pin_ioctl(): %d\n",
3278 obj->user_pin_count--;
3279 if (obj->user_pin_count == 0) {
3280 obj->pin_filp = NULL;
3281 i915_gem_object_unpin(obj);
3285 drm_gem_object_unreference(&obj->base);
3287 mutex_unlock(&dev->struct_mutex);
3292 i915_gem_busy_ioctl(struct drm_device *dev, void *data,
3293 struct drm_file *file)
3295 struct drm_i915_gem_busy *args = data;
3296 struct drm_i915_gem_object *obj;
3299 ret = i915_mutex_lock_interruptible(dev);
3303 obj = to_intel_bo(drm_gem_object_lookup(dev, file, args->handle));
3304 if (&obj->base == NULL) {
3309 /* Count all active objects as busy, even if they are currently not used
3310 * by the gpu. Users of this interface expect objects to eventually
3311 * become non-busy without any further actions, therefore emit any
3312 * necessary flushes here.
3314 args->busy = obj->active;
3316 /* Unconditionally flush objects, even when the gpu still uses this
3317 * object. Userspace calling this function indicates that it wants to
3318 * use this buffer rather sooner than later, so issuing the required
3319 * flush earlier is beneficial.
3321 if (obj->base.write_domain & I915_GEM_GPU_DOMAINS) {
3322 ret = i915_gem_flush_ring(obj->ring,
3323 0, obj->base.write_domain);
3324 } else if (obj->ring->outstanding_lazy_request ==
3325 obj->last_rendering_seqno) {
3326 struct drm_i915_gem_request *request;
3328 /* This ring is not being cleared by active usage,
3329 * so emit a request to do so.
3331 request = kzalloc(sizeof(*request), GFP_KERNEL);
3333 ret = i915_add_request(obj->ring, NULL, request);
3340 /* Update the active list for the hardware's current position.
3341 * Otherwise this only updates on a delayed timer or when irqs
3342 * are actually unmasked, and our working set ends up being
3343 * larger than required.
3345 i915_gem_retire_requests_ring(obj->ring);
3347 args->busy = obj->active;
3350 drm_gem_object_unreference(&obj->base);
3352 mutex_unlock(&dev->struct_mutex);
3357 i915_gem_throttle_ioctl(struct drm_device *dev, void *data,
3358 struct drm_file *file_priv)
3360 return i915_gem_ring_throttle(dev, file_priv);
3364 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
3365 struct drm_file *file_priv)
3367 struct drm_i915_gem_madvise *args = data;
3368 struct drm_i915_gem_object *obj;
3371 switch (args->madv) {
3372 case I915_MADV_DONTNEED:
3373 case I915_MADV_WILLNEED:
3379 ret = i915_mutex_lock_interruptible(dev);
3383 obj = to_intel_bo(drm_gem_object_lookup(dev, file_priv, args->handle));
3384 if (&obj->base == NULL) {
3389 if (obj->pin_count) {
3394 if (obj->madv != __I915_MADV_PURGED)
3395 obj->madv = args->madv;
3397 /* if the object is no longer bound, discard its backing storage */
3398 if (i915_gem_object_is_purgeable(obj) &&
3399 obj->gtt_space == NULL)
3400 i915_gem_object_truncate(obj);
3402 args->retained = obj->madv != __I915_MADV_PURGED;
3405 drm_gem_object_unreference(&obj->base);
3407 mutex_unlock(&dev->struct_mutex);
3411 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
3414 struct drm_i915_private *dev_priv = dev->dev_private;
3415 struct drm_i915_gem_object *obj;
3416 struct address_space *mapping;
3418 obj = kzalloc(sizeof(*obj), GFP_KERNEL);
3422 if (drm_gem_object_init(dev, &obj->base, size) != 0) {
3427 mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3428 mapping_set_gfp_mask(mapping, GFP_HIGHUSER | __GFP_RECLAIMABLE);
3430 i915_gem_info_add_obj(dev_priv, size);
3432 obj->base.write_domain = I915_GEM_DOMAIN_CPU;
3433 obj->base.read_domains = I915_GEM_DOMAIN_CPU;
3436 /* On some devices, we can have the GPU use the LLC (the CPU
3437 * cache) for about a 10% performance improvement
3438 * compared to uncached. Graphics requests other than
3439 * display scanout are coherent with the CPU in
3440 * accessing this cache. This means in this mode we
3441 * don't need to clflush on the CPU side, and on the
3442 * GPU side we only need to flush internal caches to
3443 * get data visible to the CPU.
3445 * However, we maintain the display planes as UC, and so
3446 * need to rebind when first used as such.
3448 obj->cache_level = I915_CACHE_LLC;
3450 obj->cache_level = I915_CACHE_NONE;
3452 obj->base.driver_private = NULL;
3453 obj->fence_reg = I915_FENCE_REG_NONE;
3454 INIT_LIST_HEAD(&obj->mm_list);
3455 INIT_LIST_HEAD(&obj->gtt_list);
3456 INIT_LIST_HEAD(&obj->ring_list);
3457 INIT_LIST_HEAD(&obj->exec_list);
3458 INIT_LIST_HEAD(&obj->gpu_write_list);
3459 obj->madv = I915_MADV_WILLNEED;
3460 /* Avoid an unnecessary call to unbind on the first bind. */
3461 obj->map_and_fenceable = true;
3466 int i915_gem_init_object(struct drm_gem_object *obj)
3473 static void i915_gem_free_object_tail(struct drm_i915_gem_object *obj)
3475 struct drm_device *dev = obj->base.dev;
3476 drm_i915_private_t *dev_priv = dev->dev_private;
3479 ret = i915_gem_object_unbind(obj);
3480 if (ret == -ERESTARTSYS) {
3481 list_move(&obj->mm_list,
3482 &dev_priv->mm.deferred_free_list);
3486 trace_i915_gem_object_destroy(obj);
3488 if (obj->base.map_list.map)
3489 drm_gem_free_mmap_offset(&obj->base);
3491 drm_gem_object_release(&obj->base);
3492 i915_gem_info_remove_obj(dev_priv, obj->base.size);
3498 void i915_gem_free_object(struct drm_gem_object *gem_obj)
3500 struct drm_i915_gem_object *obj = to_intel_bo(gem_obj);
3501 struct drm_device *dev = obj->base.dev;
3503 while (obj->pin_count > 0)
3504 i915_gem_object_unpin(obj);
3507 i915_gem_detach_phys_object(dev, obj);
3509 i915_gem_free_object_tail(obj);
3513 i915_gem_idle(struct drm_device *dev)
3515 drm_i915_private_t *dev_priv = dev->dev_private;
3518 mutex_lock(&dev->struct_mutex);
3520 if (dev_priv->mm.suspended) {
3521 mutex_unlock(&dev->struct_mutex);
3525 ret = i915_gpu_idle(dev, true);
3527 mutex_unlock(&dev->struct_mutex);
3531 /* Under UMS, be paranoid and evict. */
3532 if (!drm_core_check_feature(dev, DRIVER_MODESET)) {
3533 ret = i915_gem_evict_inactive(dev, false);
3535 mutex_unlock(&dev->struct_mutex);
3540 i915_gem_reset_fences(dev);
3542 /* Hack! Don't let anybody do execbuf while we don't control the chip.
3543 * We need to replace this with a semaphore, or something.
3544 * And not confound mm.suspended!
3546 dev_priv->mm.suspended = 1;
3547 del_timer_sync(&dev_priv->hangcheck_timer);
3549 i915_kernel_lost_context(dev);
3550 i915_gem_cleanup_ringbuffer(dev);
3552 mutex_unlock(&dev->struct_mutex);
3554 /* Cancel the retire work handler, which should be idle now. */
3555 cancel_delayed_work_sync(&dev_priv->mm.retire_work);
3560 void i915_gem_init_swizzling(struct drm_device *dev)
3562 drm_i915_private_t *dev_priv = dev->dev_private;
3564 if (INTEL_INFO(dev)->gen < 5 ||
3565 dev_priv->mm.bit_6_swizzle_x == I915_BIT_6_SWIZZLE_NONE)
3568 I915_WRITE(DISP_ARB_CTL, I915_READ(DISP_ARB_CTL) |
3569 DISP_TILE_SURFACE_SWIZZLING);
3574 I915_WRITE(TILECTL, I915_READ(TILECTL) | TILECTL_SWZCTL);
3576 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_SNB));
3578 I915_WRITE(ARB_MODE, ARB_MODE_ENABLE(ARB_MODE_SWIZZLE_IVB));
3581 void i915_gem_init_ppgtt(struct drm_device *dev)
3583 drm_i915_private_t *dev_priv = dev->dev_private;
3585 struct intel_ring_buffer *ring;
3586 struct i915_hw_ppgtt *ppgtt = dev_priv->mm.aliasing_ppgtt;
3587 uint32_t __iomem *pd_addr;
3591 if (!dev_priv->mm.aliasing_ppgtt)
3595 pd_addr = dev_priv->mm.gtt->gtt + ppgtt->pd_offset/sizeof(uint32_t);
3596 for (i = 0; i < ppgtt->num_pd_entries; i++) {
3599 if (dev_priv->mm.gtt->needs_dmar)
3600 pt_addr = ppgtt->pt_dma_addr[i];
3602 pt_addr = page_to_phys(ppgtt->pt_pages[i]);
3604 pd_entry = GEN6_PDE_ADDR_ENCODE(pt_addr);
3605 pd_entry |= GEN6_PDE_VALID;
3607 writel(pd_entry, pd_addr + i);
3611 pd_offset = ppgtt->pd_offset;
3612 pd_offset /= 64; /* in cachelines, */
3615 if (INTEL_INFO(dev)->gen == 6) {
3616 uint32_t ecochk = I915_READ(GAM_ECOCHK);
3617 I915_WRITE(GAM_ECOCHK, ecochk | ECOCHK_SNB_BIT |
3618 ECOCHK_PPGTT_CACHE64B);
3619 I915_WRITE(GFX_MODE, GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3620 } else if (INTEL_INFO(dev)->gen >= 7) {
3621 I915_WRITE(GAM_ECOCHK, ECOCHK_PPGTT_CACHE64B);
3622 /* GFX_MODE is per-ring on gen7+ */
3625 for (i = 0; i < I915_NUM_RINGS; i++) {
3626 ring = &dev_priv->ring[i];
3628 if (INTEL_INFO(dev)->gen >= 7)
3629 I915_WRITE(RING_MODE_GEN7(ring),
3630 GFX_MODE_ENABLE(GFX_PPGTT_ENABLE));
3632 I915_WRITE(RING_PP_DIR_DCLV(ring), PP_DIR_DCLV_2G);
3633 I915_WRITE(RING_PP_DIR_BASE(ring), pd_offset);
3638 i915_gem_init_hw(struct drm_device *dev)
3640 drm_i915_private_t *dev_priv = dev->dev_private;
3643 i915_gem_init_swizzling(dev);
3645 ret = intel_init_render_ring_buffer(dev);
3650 ret = intel_init_bsd_ring_buffer(dev);
3652 goto cleanup_render_ring;
3656 ret = intel_init_blt_ring_buffer(dev);
3658 goto cleanup_bsd_ring;
3661 dev_priv->next_seqno = 1;
3663 i915_gem_init_ppgtt(dev);
3668 intel_cleanup_ring_buffer(&dev_priv->ring[VCS]);
3669 cleanup_render_ring:
3670 intel_cleanup_ring_buffer(&dev_priv->ring[RCS]);
3675 i915_gem_cleanup_ringbuffer(struct drm_device *dev)
3677 drm_i915_private_t *dev_priv = dev->dev_private;
3680 for (i = 0; i < I915_NUM_RINGS; i++)
3681 intel_cleanup_ring_buffer(&dev_priv->ring[i]);
3685 i915_gem_entervt_ioctl(struct drm_device *dev, void *data,
3686 struct drm_file *file_priv)
3688 drm_i915_private_t *dev_priv = dev->dev_private;
3691 if (drm_core_check_feature(dev, DRIVER_MODESET))
3694 if (atomic_read(&dev_priv->mm.wedged)) {
3695 DRM_ERROR("Reenabling wedged hardware, good luck\n");
3696 atomic_set(&dev_priv->mm.wedged, 0);
3699 mutex_lock(&dev->struct_mutex);
3700 dev_priv->mm.suspended = 0;
3702 ret = i915_gem_init_hw(dev);
3704 mutex_unlock(&dev->struct_mutex);
3708 BUG_ON(!list_empty(&dev_priv->mm.active_list));
3709 BUG_ON(!list_empty(&dev_priv->mm.flushing_list));
3710 BUG_ON(!list_empty(&dev_priv->mm.inactive_list));
3711 for (i = 0; i < I915_NUM_RINGS; i++) {
3712 BUG_ON(!list_empty(&dev_priv->ring[i].active_list));
3713 BUG_ON(!list_empty(&dev_priv->ring[i].request_list));
3715 mutex_unlock(&dev->struct_mutex);
3717 ret = drm_irq_install(dev);
3719 goto cleanup_ringbuffer;
3724 mutex_lock(&dev->struct_mutex);
3725 i915_gem_cleanup_ringbuffer(dev);
3726 dev_priv->mm.suspended = 1;
3727 mutex_unlock(&dev->struct_mutex);
3733 i915_gem_leavevt_ioctl(struct drm_device *dev, void *data,
3734 struct drm_file *file_priv)
3736 if (drm_core_check_feature(dev, DRIVER_MODESET))
3739 drm_irq_uninstall(dev);
3740 return i915_gem_idle(dev);
3744 i915_gem_lastclose(struct drm_device *dev)
3748 if (drm_core_check_feature(dev, DRIVER_MODESET))
3751 ret = i915_gem_idle(dev);
3753 DRM_ERROR("failed to idle hardware: %d\n", ret);
3757 init_ring_lists(struct intel_ring_buffer *ring)
3759 INIT_LIST_HEAD(&ring->active_list);
3760 INIT_LIST_HEAD(&ring->request_list);
3761 INIT_LIST_HEAD(&ring->gpu_write_list);
3765 i915_gem_load(struct drm_device *dev)
3768 drm_i915_private_t *dev_priv = dev->dev_private;
3770 INIT_LIST_HEAD(&dev_priv->mm.active_list);
3771 INIT_LIST_HEAD(&dev_priv->mm.flushing_list);
3772 INIT_LIST_HEAD(&dev_priv->mm.inactive_list);
3773 INIT_LIST_HEAD(&dev_priv->mm.pinned_list);
3774 INIT_LIST_HEAD(&dev_priv->mm.fence_list);
3775 INIT_LIST_HEAD(&dev_priv->mm.deferred_free_list);
3776 INIT_LIST_HEAD(&dev_priv->mm.gtt_list);
3777 for (i = 0; i < I915_NUM_RINGS; i++)
3778 init_ring_lists(&dev_priv->ring[i]);
3779 for (i = 0; i < I915_MAX_NUM_FENCES; i++)
3780 INIT_LIST_HEAD(&dev_priv->fence_regs[i].lru_list);
3781 INIT_DELAYED_WORK(&dev_priv->mm.retire_work,
3782 i915_gem_retire_work_handler);
3783 init_completion(&dev_priv->error_completion);
3785 /* On GEN3 we really need to make sure the ARB C3 LP bit is set */
3787 u32 tmp = I915_READ(MI_ARB_STATE);
3788 if (!(tmp & MI_ARB_C3_LP_WRITE_ENABLE)) {
3789 /* arb state is a masked write, so set bit + bit in mask */
3790 tmp = MI_ARB_C3_LP_WRITE_ENABLE | (MI_ARB_C3_LP_WRITE_ENABLE << MI_ARB_MASK_SHIFT);
3791 I915_WRITE(MI_ARB_STATE, tmp);
3795 dev_priv->relative_constants_mode = I915_EXEC_CONSTANTS_REL_GENERAL;
3797 /* Old X drivers will take 0-2 for front, back, depth buffers */
3798 if (!drm_core_check_feature(dev, DRIVER_MODESET))
3799 dev_priv->fence_reg_start = 3;
3801 if (INTEL_INFO(dev)->gen >= 4 || IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev))
3802 dev_priv->num_fence_regs = 16;
3804 dev_priv->num_fence_regs = 8;
3806 /* Initialize fence registers to zero */
3807 for (i = 0; i < dev_priv->num_fence_regs; i++) {
3808 i915_gem_clear_fence_reg(dev, &dev_priv->fence_regs[i]);
3811 i915_gem_detect_bit_6_swizzle(dev);
3812 init_waitqueue_head(&dev_priv->pending_flip_queue);
3814 dev_priv->mm.interruptible = true;
3816 dev_priv->mm.inactive_shrinker.shrink = i915_gem_inactive_shrink;
3817 dev_priv->mm.inactive_shrinker.seeks = DEFAULT_SEEKS;
3818 register_shrinker(&dev_priv->mm.inactive_shrinker);
3822 * Create a physically contiguous memory object for this object
3823 * e.g. for cursor + overlay regs
3825 static int i915_gem_init_phys_object(struct drm_device *dev,
3826 int id, int size, int align)
3828 drm_i915_private_t *dev_priv = dev->dev_private;
3829 struct drm_i915_gem_phys_object *phys_obj;
3832 if (dev_priv->mm.phys_objs[id - 1] || !size)
3835 phys_obj = kzalloc(sizeof(struct drm_i915_gem_phys_object), GFP_KERNEL);
3841 phys_obj->handle = drm_pci_alloc(dev, size, align);
3842 if (!phys_obj->handle) {
3847 set_memory_wc((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3850 dev_priv->mm.phys_objs[id - 1] = phys_obj;
3858 static void i915_gem_free_phys_object(struct drm_device *dev, int id)
3860 drm_i915_private_t *dev_priv = dev->dev_private;
3861 struct drm_i915_gem_phys_object *phys_obj;
3863 if (!dev_priv->mm.phys_objs[id - 1])
3866 phys_obj = dev_priv->mm.phys_objs[id - 1];
3867 if (phys_obj->cur_obj) {
3868 i915_gem_detach_phys_object(dev, phys_obj->cur_obj);
3872 set_memory_wb((unsigned long)phys_obj->handle->vaddr, phys_obj->handle->size / PAGE_SIZE);
3874 drm_pci_free(dev, phys_obj->handle);
3876 dev_priv->mm.phys_objs[id - 1] = NULL;
3879 void i915_gem_free_all_phys_object(struct drm_device *dev)
3883 for (i = I915_GEM_PHYS_CURSOR_0; i <= I915_MAX_PHYS_OBJECT; i++)
3884 i915_gem_free_phys_object(dev, i);
3887 void i915_gem_detach_phys_object(struct drm_device *dev,
3888 struct drm_i915_gem_object *obj)
3890 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3897 vaddr = obj->phys_obj->handle->vaddr;
3899 page_count = obj->base.size / PAGE_SIZE;
3900 for (i = 0; i < page_count; i++) {
3901 struct page *page = shmem_read_mapping_page(mapping, i);
3902 if (!IS_ERR(page)) {
3903 char *dst = kmap_atomic(page);
3904 memcpy(dst, vaddr + i*PAGE_SIZE, PAGE_SIZE);
3907 drm_clflush_pages(&page, 1);
3909 set_page_dirty(page);
3910 mark_page_accessed(page);
3911 page_cache_release(page);
3914 intel_gtt_chipset_flush();
3916 obj->phys_obj->cur_obj = NULL;
3917 obj->phys_obj = NULL;
3921 i915_gem_attach_phys_object(struct drm_device *dev,
3922 struct drm_i915_gem_object *obj,
3926 struct address_space *mapping = obj->base.filp->f_path.dentry->d_inode->i_mapping;
3927 drm_i915_private_t *dev_priv = dev->dev_private;
3932 if (id > I915_MAX_PHYS_OBJECT)
3935 if (obj->phys_obj) {
3936 if (obj->phys_obj->id == id)
3938 i915_gem_detach_phys_object(dev, obj);
3941 /* create a new object */
3942 if (!dev_priv->mm.phys_objs[id - 1]) {
3943 ret = i915_gem_init_phys_object(dev, id,
3944 obj->base.size, align);
3946 DRM_ERROR("failed to init phys object %d size: %zu\n",
3947 id, obj->base.size);
3952 /* bind to the object */
3953 obj->phys_obj = dev_priv->mm.phys_objs[id - 1];
3954 obj->phys_obj->cur_obj = obj;
3956 page_count = obj->base.size / PAGE_SIZE;
3958 for (i = 0; i < page_count; i++) {
3962 page = shmem_read_mapping_page(mapping, i);
3964 return PTR_ERR(page);
3966 src = kmap_atomic(page);
3967 dst = obj->phys_obj->handle->vaddr + (i * PAGE_SIZE);
3968 memcpy(dst, src, PAGE_SIZE);
3971 mark_page_accessed(page);
3972 page_cache_release(page);
3979 i915_gem_phys_pwrite(struct drm_device *dev,
3980 struct drm_i915_gem_object *obj,
3981 struct drm_i915_gem_pwrite *args,
3982 struct drm_file *file_priv)
3984 void *vaddr = obj->phys_obj->handle->vaddr + args->offset;
3985 char __user *user_data = (char __user *) (uintptr_t) args->data_ptr;
3987 if (__copy_from_user_inatomic_nocache(vaddr, user_data, args->size)) {
3988 unsigned long unwritten;
3990 /* The physical object once assigned is fixed for the lifetime
3991 * of the obj, so we can safely drop the lock and continue
3994 mutex_unlock(&dev->struct_mutex);
3995 unwritten = copy_from_user(vaddr, user_data, args->size);
3996 mutex_lock(&dev->struct_mutex);
4001 intel_gtt_chipset_flush();
4005 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
4007 struct drm_i915_file_private *file_priv = file->driver_priv;
4009 /* Clean up our request list when the client is going away, so that
4010 * later retire_requests won't dereference our soon-to-be-gone
4013 spin_lock(&file_priv->mm.lock);
4014 while (!list_empty(&file_priv->mm.request_list)) {
4015 struct drm_i915_gem_request *request;
4017 request = list_first_entry(&file_priv->mm.request_list,
4018 struct drm_i915_gem_request,
4020 list_del(&request->client_list);
4021 request->file_priv = NULL;
4023 spin_unlock(&file_priv->mm.lock);
4027 i915_gpu_is_active(struct drm_device *dev)
4029 drm_i915_private_t *dev_priv = dev->dev_private;
4032 lists_empty = list_empty(&dev_priv->mm.flushing_list) &&
4033 list_empty(&dev_priv->mm.active_list);
4035 return !lists_empty;
4039 i915_gem_inactive_shrink(struct shrinker *shrinker, struct shrink_control *sc)
4041 struct drm_i915_private *dev_priv =
4042 container_of(shrinker,
4043 struct drm_i915_private,
4044 mm.inactive_shrinker);
4045 struct drm_device *dev = dev_priv->dev;
4046 struct drm_i915_gem_object *obj, *next;
4047 int nr_to_scan = sc->nr_to_scan;
4050 if (!mutex_trylock(&dev->struct_mutex))
4053 /* "fast-path" to count number of available objects */
4054 if (nr_to_scan == 0) {
4056 list_for_each_entry(obj,
4057 &dev_priv->mm.inactive_list,
4060 mutex_unlock(&dev->struct_mutex);
4061 return cnt / 100 * sysctl_vfs_cache_pressure;
4065 /* first scan for clean buffers */
4066 i915_gem_retire_requests(dev);
4068 list_for_each_entry_safe(obj, next,
4069 &dev_priv->mm.inactive_list,
4071 if (i915_gem_object_is_purgeable(obj)) {
4072 if (i915_gem_object_unbind(obj) == 0 &&
4078 /* second pass, evict/count anything still on the inactive list */
4080 list_for_each_entry_safe(obj, next,
4081 &dev_priv->mm.inactive_list,
4084 i915_gem_object_unbind(obj) == 0)
4090 if (nr_to_scan && i915_gpu_is_active(dev)) {
4092 * We are desperate for pages, so as a last resort, wait
4093 * for the GPU to finish and discard whatever we can.
4094 * This has a dramatic impact to reduce the number of
4095 * OOM-killer events whilst running the GPU aggressively.
4097 if (i915_gpu_idle(dev, true) == 0)
4100 mutex_unlock(&dev->struct_mutex);
4101 return cnt / 100 * sysctl_vfs_cache_pressure;