2 * Copyright © 2008-2015 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
28 #include <drm/drm_vma_manager.h>
29 #include <linux/dma-fence-array.h>
30 #include <linux/kthread.h>
31 #include <linux/dma-resv.h>
32 #include <linux/shmem_fs.h>
33 #include <linux/slab.h>
34 #include <linux/stop_machine.h>
35 #include <linux/swap.h>
36 #include <linux/pci.h>
37 #include <linux/dma-buf.h>
38 #include <linux/mman.h>
40 #include "display/intel_display.h"
41 #include "display/intel_frontbuffer.h"
43 #include "gem/i915_gem_clflush.h"
44 #include "gem/i915_gem_context.h"
45 #include "gem/i915_gem_ioctls.h"
46 #include "gem/i915_gem_mman.h"
47 #include "gem/i915_gem_region.h"
48 #include "gt/intel_engine_user.h"
49 #include "gt/intel_gt.h"
50 #include "gt/intel_gt_pm.h"
51 #include "gt/intel_workarounds.h"
54 #include "i915_trace.h"
55 #include "i915_vgpu.h"
60 insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
64 err = mutex_lock_interruptible(&ggtt->vm.mutex);
68 memset(node, 0, sizeof(*node));
69 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
70 size, 0, I915_COLOR_UNEVICTABLE,
71 0, ggtt->mappable_end,
74 mutex_unlock(&ggtt->vm.mutex);
80 remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
82 mutex_lock(&ggtt->vm.mutex);
83 drm_mm_remove_node(node);
84 mutex_unlock(&ggtt->vm.mutex);
88 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
89 struct drm_file *file)
91 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
92 struct drm_i915_gem_get_aperture *args = data;
96 if (mutex_lock_interruptible(&ggtt->vm.mutex))
99 pinned = ggtt->vm.reserved;
100 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
101 if (i915_vma_is_pinned(vma))
102 pinned += vma->node.size;
104 mutex_unlock(&ggtt->vm.mutex);
106 args->aper_size = ggtt->vm.total;
107 args->aper_available_size = args->aper_size - pinned;
112 int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
115 struct intel_runtime_pm *rpm = &to_i915(obj->base.dev)->runtime_pm;
116 LIST_HEAD(still_in_list);
117 intel_wakeref_t wakeref;
118 struct i915_vma *vma;
121 if (list_empty(&obj->vma.list))
125 * As some machines use ACPI to handle runtime-resume callbacks, and
126 * ACPI is quite kmalloc happy, we cannot resume beneath the vm->mutex
127 * as they are required by the shrinker. Ergo, we wake the device up
128 * first just in case.
130 wakeref = intel_runtime_pm_get(rpm);
134 spin_lock(&obj->vma.lock);
135 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
138 struct i915_address_space *vm = vma->vm;
140 list_move_tail(&vma->obj_link, &still_in_list);
141 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK))
144 if (flags & I915_GEM_OBJECT_UNBIND_TEST) {
150 if (!i915_vm_tryopen(vm))
153 /* Prevent vma being freed by i915_vma_parked as we unbind */
154 vma = __i915_vma_get(vma);
155 spin_unlock(&obj->vma.lock);
159 if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
160 !i915_vma_is_active(vma))
161 ret = i915_vma_unbind(vma);
167 spin_lock(&obj->vma.lock);
169 list_splice_init(&still_in_list, &obj->vma.list);
170 spin_unlock(&obj->vma.lock);
172 if (ret == -EAGAIN && flags & I915_GEM_OBJECT_UNBIND_BARRIER) {
173 rcu_barrier(); /* flush the i915_vm_release() */
177 intel_runtime_pm_put(rpm, wakeref);
183 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
192 drm_clflush_virt_range(vaddr + offset, len);
194 ret = __copy_to_user(user_data, vaddr + offset, len);
198 return ret ? -EFAULT : 0;
202 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
203 struct drm_i915_gem_pread *args)
205 unsigned int needs_clflush;
206 unsigned int idx, offset;
207 struct dma_fence *fence;
208 char __user *user_data;
212 ret = i915_gem_object_lock_interruptible(obj, NULL);
216 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
218 i915_gem_object_unlock(obj);
222 fence = i915_gem_object_lock_fence(obj);
223 i915_gem_object_finish_access(obj);
224 i915_gem_object_unlock(obj);
230 user_data = u64_to_user_ptr(args->data_ptr);
231 offset = offset_in_page(args->offset);
232 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
233 struct page *page = i915_gem_object_get_page(obj, idx);
234 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
236 ret = shmem_pread(page, offset, length, user_data,
246 i915_gem_object_unlock_fence(obj, fence);
251 gtt_user_read(struct io_mapping *mapping,
252 loff_t base, int offset,
253 char __user *user_data, int length)
256 unsigned long unwritten;
258 /* We can use the cpu mem copy function because this is X86. */
259 vaddr = io_mapping_map_atomic_wc(mapping, base);
260 unwritten = __copy_to_user_inatomic(user_data,
261 (void __force *)vaddr + offset,
263 io_mapping_unmap_atomic(vaddr);
265 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
266 unwritten = copy_to_user(user_data,
267 (void __force *)vaddr + offset,
269 io_mapping_unmap(vaddr);
275 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
276 const struct drm_i915_gem_pread *args)
278 struct drm_i915_private *i915 = to_i915(obj->base.dev);
279 struct i915_ggtt *ggtt = &i915->ggtt;
280 intel_wakeref_t wakeref;
281 struct drm_mm_node node;
282 struct dma_fence *fence;
283 void __user *user_data;
284 struct i915_vma *vma;
288 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
289 vma = ERR_PTR(-ENODEV);
290 if (!i915_gem_object_is_tiled(obj))
291 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
293 PIN_NONBLOCK /* NOWARN */ |
296 node.start = i915_ggtt_offset(vma);
299 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
302 GEM_BUG_ON(!drm_mm_node_allocated(&node));
305 ret = i915_gem_object_lock_interruptible(obj, NULL);
309 ret = i915_gem_object_set_to_gtt_domain(obj, false);
311 i915_gem_object_unlock(obj);
315 fence = i915_gem_object_lock_fence(obj);
316 i915_gem_object_unlock(obj);
322 user_data = u64_to_user_ptr(args->data_ptr);
324 offset = args->offset;
327 /* Operation in this page
329 * page_base = page offset within aperture
330 * page_offset = offset within page
331 * page_length = bytes to copy for this page
333 u32 page_base = node.start;
334 unsigned page_offset = offset_in_page(offset);
335 unsigned page_length = PAGE_SIZE - page_offset;
336 page_length = remain < page_length ? remain : page_length;
337 if (drm_mm_node_allocated(&node)) {
338 ggtt->vm.insert_page(&ggtt->vm,
339 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
340 node.start, I915_CACHE_NONE, 0);
342 page_base += offset & PAGE_MASK;
345 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
346 user_data, page_length)) {
351 remain -= page_length;
352 user_data += page_length;
353 offset += page_length;
356 i915_gem_object_unlock_fence(obj, fence);
358 if (drm_mm_node_allocated(&node)) {
359 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
360 remove_mappable_node(ggtt, &node);
365 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
370 * Reads data from the object referenced by handle.
371 * @dev: drm device pointer
372 * @data: ioctl data blob
373 * @file: drm file pointer
375 * On error, the contents of *data are undefined.
378 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
379 struct drm_file *file)
381 struct drm_i915_gem_pread *args = data;
382 struct drm_i915_gem_object *obj;
388 if (!access_ok(u64_to_user_ptr(args->data_ptr),
392 obj = i915_gem_object_lookup(file, args->handle);
396 /* Bounds check source. */
397 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
402 trace_i915_gem_object_pread(obj, args->offset, args->size);
406 ret = obj->ops->pread(obj, args);
410 ret = i915_gem_object_wait(obj,
411 I915_WAIT_INTERRUPTIBLE,
412 MAX_SCHEDULE_TIMEOUT);
416 ret = i915_gem_object_pin_pages(obj);
420 ret = i915_gem_shmem_pread(obj, args);
421 if (ret == -EFAULT || ret == -ENODEV)
422 ret = i915_gem_gtt_pread(obj, args);
424 i915_gem_object_unpin_pages(obj);
426 i915_gem_object_put(obj);
430 /* This is the fast write path which cannot handle
431 * page faults in the source data
435 ggtt_write(struct io_mapping *mapping,
436 loff_t base, int offset,
437 char __user *user_data, int length)
440 unsigned long unwritten;
442 /* We can use the cpu mem copy function because this is X86. */
443 vaddr = io_mapping_map_atomic_wc(mapping, base);
444 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
446 io_mapping_unmap_atomic(vaddr);
448 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
449 unwritten = copy_from_user((void __force *)vaddr + offset,
451 io_mapping_unmap(vaddr);
458 * This is the fast pwrite path, where we copy the data directly from the
459 * user into the GTT, uncached.
460 * @obj: i915 GEM object
461 * @args: pwrite arguments structure
464 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
465 const struct drm_i915_gem_pwrite *args)
467 struct drm_i915_private *i915 = to_i915(obj->base.dev);
468 struct i915_ggtt *ggtt = &i915->ggtt;
469 struct intel_runtime_pm *rpm = &i915->runtime_pm;
470 intel_wakeref_t wakeref;
471 struct drm_mm_node node;
472 struct dma_fence *fence;
473 struct i915_vma *vma;
475 void __user *user_data;
478 if (i915_gem_object_has_struct_page(obj)) {
480 * Avoid waking the device up if we can fallback, as
481 * waking/resuming is very slow (worst-case 10-100 ms
482 * depending on PCI sleeps and our own resume time).
483 * This easily dwarfs any performance advantage from
484 * using the cache bypass of indirect GGTT access.
486 wakeref = intel_runtime_pm_get_if_in_use(rpm);
490 /* No backing pages, no fallback, we must force GGTT access */
491 wakeref = intel_runtime_pm_get(rpm);
494 vma = ERR_PTR(-ENODEV);
495 if (!i915_gem_object_is_tiled(obj))
496 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
498 PIN_NONBLOCK /* NOWARN */ |
501 node.start = i915_ggtt_offset(vma);
504 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
507 GEM_BUG_ON(!drm_mm_node_allocated(&node));
510 ret = i915_gem_object_lock_interruptible(obj, NULL);
514 ret = i915_gem_object_set_to_gtt_domain(obj, true);
516 i915_gem_object_unlock(obj);
520 fence = i915_gem_object_lock_fence(obj);
521 i915_gem_object_unlock(obj);
527 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
529 user_data = u64_to_user_ptr(args->data_ptr);
530 offset = args->offset;
533 /* Operation in this page
535 * page_base = page offset within aperture
536 * page_offset = offset within page
537 * page_length = bytes to copy for this page
539 u32 page_base = node.start;
540 unsigned int page_offset = offset_in_page(offset);
541 unsigned int page_length = PAGE_SIZE - page_offset;
542 page_length = remain < page_length ? remain : page_length;
543 if (drm_mm_node_allocated(&node)) {
544 /* flush the write before we modify the GGTT */
545 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
546 ggtt->vm.insert_page(&ggtt->vm,
547 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
548 node.start, I915_CACHE_NONE, 0);
549 wmb(); /* flush modifications to the GGTT (insert_page) */
551 page_base += offset & PAGE_MASK;
553 /* If we get a fault while copying data, then (presumably) our
554 * source page isn't available. Return the error and we'll
555 * retry in the slow path.
556 * If the object is non-shmem backed, we retry again with the
557 * path that handles page fault.
559 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
560 user_data, page_length)) {
565 remain -= page_length;
566 user_data += page_length;
567 offset += page_length;
570 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
571 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
573 i915_gem_object_unlock_fence(obj, fence);
575 if (drm_mm_node_allocated(&node)) {
576 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
577 remove_mappable_node(ggtt, &node);
582 intel_runtime_pm_put(rpm, wakeref);
586 /* Per-page copy function for the shmem pwrite fastpath.
587 * Flushes invalid cachelines before writing to the target if
588 * needs_clflush_before is set and flushes out any written cachelines after
589 * writing if needs_clflush is set.
592 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
593 bool needs_clflush_before,
594 bool needs_clflush_after)
601 if (needs_clflush_before)
602 drm_clflush_virt_range(vaddr + offset, len);
604 ret = __copy_from_user(vaddr + offset, user_data, len);
605 if (!ret && needs_clflush_after)
606 drm_clflush_virt_range(vaddr + offset, len);
610 return ret ? -EFAULT : 0;
614 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
615 const struct drm_i915_gem_pwrite *args)
617 unsigned int partial_cacheline_write;
618 unsigned int needs_clflush;
619 unsigned int offset, idx;
620 struct dma_fence *fence;
621 void __user *user_data;
625 ret = i915_gem_object_lock_interruptible(obj, NULL);
629 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
631 i915_gem_object_unlock(obj);
635 fence = i915_gem_object_lock_fence(obj);
636 i915_gem_object_finish_access(obj);
637 i915_gem_object_unlock(obj);
642 /* If we don't overwrite a cacheline completely we need to be
643 * careful to have up-to-date data by first clflushing. Don't
644 * overcomplicate things and flush the entire patch.
646 partial_cacheline_write = 0;
647 if (needs_clflush & CLFLUSH_BEFORE)
648 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
650 user_data = u64_to_user_ptr(args->data_ptr);
652 offset = offset_in_page(args->offset);
653 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
654 struct page *page = i915_gem_object_get_page(obj, idx);
655 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
657 ret = shmem_pwrite(page, offset, length, user_data,
658 (offset | length) & partial_cacheline_write,
659 needs_clflush & CLFLUSH_AFTER);
668 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
669 i915_gem_object_unlock_fence(obj, fence);
675 * Writes data to the object referenced by handle.
677 * @data: ioctl data blob
680 * On error, the contents of the buffer that were to be modified are undefined.
683 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
684 struct drm_file *file)
686 struct drm_i915_gem_pwrite *args = data;
687 struct drm_i915_gem_object *obj;
693 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
696 obj = i915_gem_object_lookup(file, args->handle);
700 /* Bounds check destination. */
701 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
706 /* Writes not allowed into this read-only object */
707 if (i915_gem_object_is_readonly(obj)) {
712 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
715 if (obj->ops->pwrite)
716 ret = obj->ops->pwrite(obj, args);
720 ret = i915_gem_object_wait(obj,
721 I915_WAIT_INTERRUPTIBLE |
723 MAX_SCHEDULE_TIMEOUT);
727 ret = i915_gem_object_pin_pages(obj);
732 /* We can only do the GTT pwrite on untiled buffers, as otherwise
733 * it would end up going through the fenced access, and we'll get
734 * different detiling behavior between reading and writing.
735 * pread/pwrite currently are reading and writing from the CPU
736 * perspective, requiring manual detiling by the client.
738 if (!i915_gem_object_has_struct_page(obj) ||
739 cpu_write_needs_clflush(obj))
740 /* Note that the gtt paths might fail with non-page-backed user
741 * pointers (e.g. gtt mappings when moving data between
742 * textures). Fallback to the shmem path in that case.
744 ret = i915_gem_gtt_pwrite_fast(obj, args);
746 if (ret == -EFAULT || ret == -ENOSPC) {
747 if (i915_gem_object_has_struct_page(obj))
748 ret = i915_gem_shmem_pwrite(obj, args);
751 i915_gem_object_unpin_pages(obj);
753 i915_gem_object_put(obj);
758 * Called when user space has done writes to this buffer
760 * @data: ioctl data blob
764 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
765 struct drm_file *file)
767 struct drm_i915_gem_sw_finish *args = data;
768 struct drm_i915_gem_object *obj;
770 obj = i915_gem_object_lookup(file, args->handle);
775 * Proxy objects are barred from CPU access, so there is no
776 * need to ban sw_finish as it is a nop.
779 /* Pinned buffers may be scanout, so flush the cache */
780 i915_gem_object_flush_if_display(obj);
781 i915_gem_object_put(obj);
786 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
788 struct drm_i915_gem_object *obj, *on;
792 * Only called during RPM suspend. All users of the userfault_list
793 * must be holding an RPM wakeref to ensure that this can not
794 * run concurrently with themselves (and use the struct_mutex for
795 * protection between themselves).
798 list_for_each_entry_safe(obj, on,
799 &i915->ggtt.userfault_list, userfault_link)
800 __i915_gem_object_release_mmap_gtt(obj);
803 * The fence will be lost when the device powers down. If any were
804 * in use by hardware (i.e. they are pinned), we should not be powering
805 * down! All other fences will be reacquired by the user upon waking.
807 for (i = 0; i < i915->ggtt.num_fences; i++) {
808 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
811 * Ideally we want to assert that the fence register is not
812 * live at this point (i.e. that no piece of code will be
813 * trying to write through fence + GTT, as that both violates
814 * our tracking of activity and associated locking/barriers,
815 * but also is illegal given that the hw is powered down).
817 * Previously we used reg->pin_count as a "liveness" indicator.
818 * That is not sufficient, and we need a more fine-grained
819 * tool if we want to have a sanity check here.
825 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
830 static void discard_ggtt_vma(struct i915_vma *vma)
832 struct drm_i915_gem_object *obj = vma->obj;
834 spin_lock(&obj->vma.lock);
835 if (!RB_EMPTY_NODE(&vma->obj_node)) {
836 rb_erase(&vma->obj_node, &obj->vma.tree);
837 RB_CLEAR_NODE(&vma->obj_node);
839 spin_unlock(&obj->vma.lock);
843 i915_gem_object_ggtt_pin_ww(struct drm_i915_gem_object *obj,
844 struct i915_gem_ww_ctx *ww,
845 const struct i915_ggtt_view *view,
846 u64 size, u64 alignment, u64 flags)
848 struct drm_i915_private *i915 = to_i915(obj->base.dev);
849 struct i915_ggtt *ggtt = &i915->ggtt;
850 struct i915_vma *vma;
853 if (flags & PIN_MAPPABLE &&
854 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
856 * If the required space is larger than the available
857 * aperture, we will not able to find a slot for the
858 * object and unbinding the object now will be in
859 * vain. Worse, doing so may cause us to ping-pong
860 * the object in and out of the Global GTT and
861 * waste a lot of cycles under the mutex.
863 if (obj->base.size > ggtt->mappable_end)
864 return ERR_PTR(-E2BIG);
867 * If NONBLOCK is set the caller is optimistically
868 * trying to cache the full object within the mappable
869 * aperture, and *must* have a fallback in place for
870 * situations where we cannot bind the object. We
871 * can be a little more lax here and use the fallback
872 * more often to avoid costly migrations of ourselves
873 * and other objects within the aperture.
875 * Half-the-aperture is used as a simple heuristic.
876 * More interesting would to do search for a free
877 * block prior to making the commitment to unbind.
878 * That caters for the self-harm case, and with a
879 * little more heuristics (e.g. NOFAULT, NOEVICT)
880 * we could try to minimise harm to others.
882 if (flags & PIN_NONBLOCK &&
883 obj->base.size > ggtt->mappable_end / 2)
884 return ERR_PTR(-ENOSPC);
888 vma = i915_vma_instance(obj, &ggtt->vm, view);
892 if (i915_vma_misplaced(vma, size, alignment, flags)) {
893 if (flags & PIN_NONBLOCK) {
894 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
895 return ERR_PTR(-ENOSPC);
897 if (flags & PIN_MAPPABLE &&
898 vma->fence_size > ggtt->mappable_end / 2)
899 return ERR_PTR(-ENOSPC);
902 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) {
903 discard_ggtt_vma(vma);
907 ret = i915_vma_unbind(vma);
912 ret = i915_vma_pin_ww(vma, ww, size, alignment, flags | PIN_GLOBAL);
916 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
917 mutex_lock(&ggtt->vm.mutex);
918 i915_vma_revoke_fence(vma);
919 mutex_unlock(&ggtt->vm.mutex);
922 ret = i915_vma_wait_for_bind(vma);
932 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
933 struct drm_file *file_priv)
935 struct drm_i915_private *i915 = to_i915(dev);
936 struct drm_i915_gem_madvise *args = data;
937 struct drm_i915_gem_object *obj;
940 switch (args->madv) {
941 case I915_MADV_DONTNEED:
942 case I915_MADV_WILLNEED:
948 obj = i915_gem_object_lookup(file_priv, args->handle);
952 err = mutex_lock_interruptible(&obj->mm.lock);
956 if (i915_gem_object_has_pages(obj) &&
957 i915_gem_object_is_tiled(obj) &&
958 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
959 if (obj->mm.madv == I915_MADV_WILLNEED) {
960 GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
961 i915_gem_object_clear_tiling_quirk(obj);
962 i915_gem_object_make_shrinkable(obj);
964 if (args->madv == I915_MADV_WILLNEED) {
965 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
966 i915_gem_object_make_unshrinkable(obj);
967 i915_gem_object_set_tiling_quirk(obj);
971 if (obj->mm.madv != __I915_MADV_PURGED)
972 obj->mm.madv = args->madv;
974 if (i915_gem_object_has_pages(obj)) {
975 struct list_head *list;
977 if (i915_gem_object_is_shrinkable(obj)) {
980 spin_lock_irqsave(&i915->mm.obj_lock, flags);
982 if (obj->mm.madv != I915_MADV_WILLNEED)
983 list = &i915->mm.purge_list;
985 list = &i915->mm.shrink_list;
986 list_move_tail(&obj->mm.link, list);
988 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
992 /* if the object is no longer attached, discard its backing storage */
993 if (obj->mm.madv == I915_MADV_DONTNEED &&
994 !i915_gem_object_has_pages(obj))
995 i915_gem_object_truncate(obj);
997 args->retained = obj->mm.madv != __I915_MADV_PURGED;
998 mutex_unlock(&obj->mm.lock);
1001 i915_gem_object_put(obj);
1005 int i915_gem_init(struct drm_i915_private *dev_priv)
1009 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1010 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1011 mkwrite_device_info(dev_priv)->page_sizes =
1012 I915_GTT_PAGE_SIZE_4K;
1014 ret = i915_gem_init_userptr(dev_priv);
1018 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1019 intel_wopcm_init(&dev_priv->wopcm);
1021 ret = i915_init_ggtt(dev_priv);
1023 GEM_BUG_ON(ret == -EIO);
1028 * Despite its name intel_init_clock_gating applies both display
1029 * clock gating workarounds; GT mmio workarounds and the occasional
1030 * GT power context workaround. Worse, sometimes it includes a context
1031 * register workaround which we need to apply before we record the
1032 * default HW state for all contexts.
1034 * FIXME: break up the workarounds and apply them at the right time!
1036 intel_init_clock_gating(dev_priv);
1038 ret = intel_gt_init(&dev_priv->gt);
1045 * Unwinding is complicated by that we want to handle -EIO to mean
1046 * disable GPU submission but keep KMS alive. We want to mark the
1047 * HW as irrevisibly wedged, but keep enough state around that the
1048 * driver doesn't explode during runtime.
1051 i915_gem_drain_workqueue(dev_priv);
1054 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1055 i915_gem_cleanup_userptr(dev_priv);
1060 * Allow engines or uC initialisation to fail by marking the GPU
1061 * as wedged. But we only want to do this when the GPU is angry,
1062 * for all other failure, such as an allocation failure, bail.
1064 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1065 i915_probe_error(dev_priv,
1066 "Failed to initialize GPU, declaring it wedged!\n");
1067 intel_gt_set_wedged(&dev_priv->gt);
1070 /* Minimal basic recovery for KMS */
1071 ret = i915_ggtt_enable_hw(dev_priv);
1072 i915_ggtt_resume(&dev_priv->ggtt);
1073 intel_init_clock_gating(dev_priv);
1076 i915_gem_drain_freed_objects(dev_priv);
1080 void i915_gem_driver_register(struct drm_i915_private *i915)
1082 i915_gem_driver_register__shrinker(i915);
1084 intel_engines_driver_register(i915);
1087 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1089 i915_gem_driver_unregister__shrinker(i915);
1092 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1094 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1096 i915_gem_suspend_late(dev_priv);
1097 intel_gt_driver_remove(&dev_priv->gt);
1098 dev_priv->uabi_engines = RB_ROOT;
1100 /* Flush any outstanding unpin_work. */
1101 i915_gem_drain_workqueue(dev_priv);
1103 i915_gem_drain_freed_objects(dev_priv);
1106 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1108 intel_gt_driver_release(&dev_priv->gt);
1110 intel_wa_list_free(&dev_priv->gt_wa_list);
1112 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1113 i915_gem_cleanup_userptr(dev_priv);
1115 i915_gem_drain_freed_objects(dev_priv);
1117 drm_WARN_ON(&dev_priv->drm, !list_empty(&dev_priv->gem.contexts.list));
1120 static void i915_gem_init__mm(struct drm_i915_private *i915)
1122 spin_lock_init(&i915->mm.obj_lock);
1124 init_llist_head(&i915->mm.free_list);
1126 INIT_LIST_HEAD(&i915->mm.purge_list);
1127 INIT_LIST_HEAD(&i915->mm.shrink_list);
1129 i915_gem_init__objects(i915);
1132 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1134 i915_gem_init__mm(dev_priv);
1135 i915_gem_init__contexts(dev_priv);
1137 spin_lock_init(&dev_priv->fb_tracking.lock);
1140 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1142 i915_gem_drain_freed_objects(dev_priv);
1143 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1144 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1145 drm_WARN_ON(&dev_priv->drm, dev_priv->mm.shrink_count);
1148 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1150 struct drm_i915_file_private *file_priv;
1155 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1159 file->driver_priv = file_priv;
1160 file_priv->dev_priv = i915;
1161 file_priv->file = file;
1163 file_priv->bsd_engine = -1;
1164 file_priv->hang_timestamp = jiffies;
1166 ret = i915_gem_context_open(i915, file);
1173 void i915_gem_ww_ctx_init(struct i915_gem_ww_ctx *ww, bool intr)
1175 ww_acquire_init(&ww->ctx, &reservation_ww_class);
1176 INIT_LIST_HEAD(&ww->obj_list);
1178 ww->contended = NULL;
1181 static void i915_gem_ww_ctx_unlock_all(struct i915_gem_ww_ctx *ww)
1183 struct drm_i915_gem_object *obj;
1185 while ((obj = list_first_entry_or_null(&ww->obj_list, struct drm_i915_gem_object, obj_link))) {
1186 list_del(&obj->obj_link);
1187 i915_gem_object_unlock(obj);
1191 void i915_gem_ww_unlock_single(struct drm_i915_gem_object *obj)
1193 list_del(&obj->obj_link);
1194 i915_gem_object_unlock(obj);
1197 void i915_gem_ww_ctx_fini(struct i915_gem_ww_ctx *ww)
1199 i915_gem_ww_ctx_unlock_all(ww);
1200 WARN_ON(ww->contended);
1201 ww_acquire_fini(&ww->ctx);
1204 int __must_check i915_gem_ww_ctx_backoff(struct i915_gem_ww_ctx *ww)
1208 if (WARN_ON(!ww->contended))
1211 i915_gem_ww_ctx_unlock_all(ww);
1213 ret = dma_resv_lock_slow_interruptible(ww->contended->base.resv, &ww->ctx);
1215 dma_resv_lock_slow(ww->contended->base.resv, &ww->ctx);
1218 list_add_tail(&ww->contended->obj_link, &ww->obj_list);
1220 ww->contended = NULL;
1225 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1226 #include "selftests/mock_gem_device.c"
1227 #include "selftests/i915_gem.c"