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
24 * Eric Anholt <eric@anholt.net>
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 drm_i915_private *i915 = to_i915(dev);
92 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
93 struct drm_i915_gem_get_aperture *args = data;
97 if (mutex_lock_interruptible(&ggtt->vm.mutex))
100 pinned = ggtt->vm.reserved;
101 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
102 if (i915_vma_is_pinned(vma))
103 pinned += vma->node.size;
105 mutex_unlock(&ggtt->vm.mutex);
107 args->aper_size = ggtt->vm.total;
108 args->aper_available_size = args->aper_size - pinned;
113 int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
116 struct intel_runtime_pm *rpm = &to_i915(obj->base.dev)->runtime_pm;
117 LIST_HEAD(still_in_list);
118 intel_wakeref_t wakeref;
119 struct i915_vma *vma;
122 if (list_empty(&obj->vma.list))
126 * As some machines use ACPI to handle runtime-resume callbacks, and
127 * ACPI is quite kmalloc happy, we cannot resume beneath the vm->mutex
128 * as they are required by the shrinker. Ergo, we wake the device up
129 * first just in case.
131 wakeref = intel_runtime_pm_get(rpm);
135 spin_lock(&obj->vma.lock);
136 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
139 struct i915_address_space *vm = vma->vm;
141 list_move_tail(&vma->obj_link, &still_in_list);
142 if (!i915_vma_is_bound(vma, I915_VMA_BIND_MASK))
145 if (flags & I915_GEM_OBJECT_UNBIND_TEST) {
151 if (!i915_vm_tryopen(vm))
154 /* Prevent vma being freed by i915_vma_parked as we unbind */
155 vma = __i915_vma_get(vma);
156 spin_unlock(&obj->vma.lock);
159 bool vm_trylock = !!(flags & I915_GEM_OBJECT_UNBIND_VM_TRYLOCK);
161 if (flags & I915_GEM_OBJECT_UNBIND_ASYNC) {
162 assert_object_held(vma->obj);
163 ret = i915_vma_unbind_async(vma, vm_trylock);
166 if (ret == -EBUSY && (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
167 !i915_vma_is_active(vma))) {
169 if (mutex_trylock(&vma->vm->mutex)) {
170 ret = __i915_vma_unbind(vma);
171 mutex_unlock(&vma->vm->mutex);
176 ret = i915_vma_unbind(vma);
184 spin_lock(&obj->vma.lock);
186 list_splice_init(&still_in_list, &obj->vma.list);
187 spin_unlock(&obj->vma.lock);
189 if (ret == -EAGAIN && flags & I915_GEM_OBJECT_UNBIND_BARRIER) {
190 rcu_barrier(); /* flush the i915_vm_release() */
194 intel_runtime_pm_put(rpm, wakeref);
200 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
209 drm_clflush_virt_range(vaddr + offset, len);
211 ret = __copy_to_user(user_data, vaddr + offset, len);
215 return ret ? -EFAULT : 0;
219 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
220 struct drm_i915_gem_pread *args)
222 unsigned int needs_clflush;
223 unsigned int idx, offset;
224 char __user *user_data;
228 ret = i915_gem_object_lock_interruptible(obj, NULL);
232 ret = i915_gem_object_pin_pages(obj);
236 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
240 i915_gem_object_finish_access(obj);
241 i915_gem_object_unlock(obj);
244 user_data = u64_to_user_ptr(args->data_ptr);
245 offset = offset_in_page(args->offset);
246 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
247 struct page *page = i915_gem_object_get_page(obj, idx);
248 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
250 ret = shmem_pread(page, offset, length, user_data,
260 i915_gem_object_unpin_pages(obj);
264 i915_gem_object_unpin_pages(obj);
266 i915_gem_object_unlock(obj);
271 gtt_user_read(struct io_mapping *mapping,
272 loff_t base, int offset,
273 char __user *user_data, int length)
276 unsigned long unwritten;
278 /* We can use the cpu mem copy function because this is X86. */
279 vaddr = io_mapping_map_atomic_wc(mapping, base);
280 unwritten = __copy_to_user_inatomic(user_data,
281 (void __force *)vaddr + offset,
283 io_mapping_unmap_atomic(vaddr);
285 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
286 unwritten = copy_to_user(user_data,
287 (void __force *)vaddr + offset,
289 io_mapping_unmap(vaddr);
294 static struct i915_vma *i915_gem_gtt_prepare(struct drm_i915_gem_object *obj,
295 struct drm_mm_node *node,
298 struct drm_i915_private *i915 = to_i915(obj->base.dev);
299 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
300 struct i915_vma *vma;
301 struct i915_gem_ww_ctx ww;
304 i915_gem_ww_ctx_init(&ww, true);
306 vma = ERR_PTR(-ENODEV);
307 ret = i915_gem_object_lock(obj, &ww);
311 ret = i915_gem_object_set_to_gtt_domain(obj, write);
315 if (!i915_gem_object_is_tiled(obj))
316 vma = i915_gem_object_ggtt_pin_ww(obj, &ww, NULL, 0, 0,
318 PIN_NONBLOCK /* NOWARN */ |
320 if (vma == ERR_PTR(-EDEADLK)) {
323 } else if (!IS_ERR(vma)) {
324 node->start = i915_ggtt_offset(vma);
327 ret = insert_mappable_node(ggtt, node, PAGE_SIZE);
330 GEM_BUG_ON(!drm_mm_node_allocated(node));
334 ret = i915_gem_object_pin_pages(obj);
336 if (drm_mm_node_allocated(node)) {
337 ggtt->vm.clear_range(&ggtt->vm, node->start, node->size);
338 remove_mappable_node(ggtt, node);
345 if (ret == -EDEADLK) {
346 ret = i915_gem_ww_ctx_backoff(&ww);
350 i915_gem_ww_ctx_fini(&ww);
352 return ret ? ERR_PTR(ret) : vma;
355 static void i915_gem_gtt_cleanup(struct drm_i915_gem_object *obj,
356 struct drm_mm_node *node,
357 struct i915_vma *vma)
359 struct drm_i915_private *i915 = to_i915(obj->base.dev);
360 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
362 i915_gem_object_unpin_pages(obj);
363 if (drm_mm_node_allocated(node)) {
364 ggtt->vm.clear_range(&ggtt->vm, node->start, node->size);
365 remove_mappable_node(ggtt, node);
372 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
373 const struct drm_i915_gem_pread *args)
375 struct drm_i915_private *i915 = to_i915(obj->base.dev);
376 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
377 intel_wakeref_t wakeref;
378 struct drm_mm_node node;
379 void __user *user_data;
380 struct i915_vma *vma;
384 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
386 vma = i915_gem_gtt_prepare(obj, &node, false);
392 user_data = u64_to_user_ptr(args->data_ptr);
394 offset = args->offset;
397 /* Operation in this page
399 * page_base = page offset within aperture
400 * page_offset = offset within page
401 * page_length = bytes to copy for this page
403 u32 page_base = node.start;
404 unsigned page_offset = offset_in_page(offset);
405 unsigned page_length = PAGE_SIZE - page_offset;
406 page_length = remain < page_length ? remain : page_length;
407 if (drm_mm_node_allocated(&node)) {
408 ggtt->vm.insert_page(&ggtt->vm,
409 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
410 node.start, I915_CACHE_NONE, 0);
412 page_base += offset & PAGE_MASK;
415 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
416 user_data, page_length)) {
421 remain -= page_length;
422 user_data += page_length;
423 offset += page_length;
426 i915_gem_gtt_cleanup(obj, &node, vma);
428 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
433 * Reads data from the object referenced by handle.
434 * @dev: drm device pointer
435 * @data: ioctl data blob
436 * @file: drm file pointer
438 * On error, the contents of *data are undefined.
441 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
442 struct drm_file *file)
444 struct drm_i915_private *i915 = to_i915(dev);
445 struct drm_i915_gem_pread *args = data;
446 struct drm_i915_gem_object *obj;
449 /* PREAD is disallowed for all platforms after TGL-LP. This also
450 * covers all platforms with local memory.
452 if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915))
458 if (!access_ok(u64_to_user_ptr(args->data_ptr),
462 obj = i915_gem_object_lookup(file, args->handle);
466 /* Bounds check source. */
467 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
472 trace_i915_gem_object_pread(obj, args->offset, args->size);
475 ret = obj->ops->pread(obj, args);
479 ret = i915_gem_object_wait(obj,
480 I915_WAIT_INTERRUPTIBLE,
481 MAX_SCHEDULE_TIMEOUT);
485 ret = i915_gem_shmem_pread(obj, args);
486 if (ret == -EFAULT || ret == -ENODEV)
487 ret = i915_gem_gtt_pread(obj, args);
490 i915_gem_object_put(obj);
494 /* This is the fast write path which cannot handle
495 * page faults in the source data
499 ggtt_write(struct io_mapping *mapping,
500 loff_t base, int offset,
501 char __user *user_data, int length)
504 unsigned long unwritten;
506 /* We can use the cpu mem copy function because this is X86. */
507 vaddr = io_mapping_map_atomic_wc(mapping, base);
508 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
510 io_mapping_unmap_atomic(vaddr);
512 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
513 unwritten = copy_from_user((void __force *)vaddr + offset,
515 io_mapping_unmap(vaddr);
522 * This is the fast pwrite path, where we copy the data directly from the
523 * user into the GTT, uncached.
524 * @obj: i915 GEM object
525 * @args: pwrite arguments structure
528 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
529 const struct drm_i915_gem_pwrite *args)
531 struct drm_i915_private *i915 = to_i915(obj->base.dev);
532 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
533 struct intel_runtime_pm *rpm = &i915->runtime_pm;
534 intel_wakeref_t wakeref;
535 struct drm_mm_node node;
536 struct i915_vma *vma;
538 void __user *user_data;
541 if (i915_gem_object_has_struct_page(obj)) {
543 * Avoid waking the device up if we can fallback, as
544 * waking/resuming is very slow (worst-case 10-100 ms
545 * depending on PCI sleeps and our own resume time).
546 * This easily dwarfs any performance advantage from
547 * using the cache bypass of indirect GGTT access.
549 wakeref = intel_runtime_pm_get_if_in_use(rpm);
553 /* No backing pages, no fallback, we must force GGTT access */
554 wakeref = intel_runtime_pm_get(rpm);
557 vma = i915_gem_gtt_prepare(obj, &node, true);
563 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
565 user_data = u64_to_user_ptr(args->data_ptr);
566 offset = args->offset;
569 /* Operation in this page
571 * page_base = page offset within aperture
572 * page_offset = offset within page
573 * page_length = bytes to copy for this page
575 u32 page_base = node.start;
576 unsigned int page_offset = offset_in_page(offset);
577 unsigned int page_length = PAGE_SIZE - page_offset;
578 page_length = remain < page_length ? remain : page_length;
579 if (drm_mm_node_allocated(&node)) {
580 /* flush the write before we modify the GGTT */
581 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
582 ggtt->vm.insert_page(&ggtt->vm,
583 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
584 node.start, I915_CACHE_NONE, 0);
585 wmb(); /* flush modifications to the GGTT (insert_page) */
587 page_base += offset & PAGE_MASK;
589 /* If we get a fault while copying data, then (presumably) our
590 * source page isn't available. Return the error and we'll
591 * retry in the slow path.
592 * If the object is non-shmem backed, we retry again with the
593 * path that handles page fault.
595 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
596 user_data, page_length)) {
601 remain -= page_length;
602 user_data += page_length;
603 offset += page_length;
606 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
607 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
609 i915_gem_gtt_cleanup(obj, &node, vma);
611 intel_runtime_pm_put(rpm, wakeref);
615 /* Per-page copy function for the shmem pwrite fastpath.
616 * Flushes invalid cachelines before writing to the target if
617 * needs_clflush_before is set and flushes out any written cachelines after
618 * writing if needs_clflush is set.
621 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
622 bool needs_clflush_before,
623 bool needs_clflush_after)
630 if (needs_clflush_before)
631 drm_clflush_virt_range(vaddr + offset, len);
633 ret = __copy_from_user(vaddr + offset, user_data, len);
634 if (!ret && needs_clflush_after)
635 drm_clflush_virt_range(vaddr + offset, len);
639 return ret ? -EFAULT : 0;
643 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
644 const struct drm_i915_gem_pwrite *args)
646 unsigned int partial_cacheline_write;
647 unsigned int needs_clflush;
648 unsigned int offset, idx;
649 void __user *user_data;
653 ret = i915_gem_object_lock_interruptible(obj, NULL);
657 ret = i915_gem_object_pin_pages(obj);
661 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
665 i915_gem_object_finish_access(obj);
666 i915_gem_object_unlock(obj);
668 /* If we don't overwrite a cacheline completely we need to be
669 * careful to have up-to-date data by first clflushing. Don't
670 * overcomplicate things and flush the entire patch.
672 partial_cacheline_write = 0;
673 if (needs_clflush & CLFLUSH_BEFORE)
674 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
676 user_data = u64_to_user_ptr(args->data_ptr);
678 offset = offset_in_page(args->offset);
679 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
680 struct page *page = i915_gem_object_get_page(obj, idx);
681 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
683 ret = shmem_pwrite(page, offset, length, user_data,
684 (offset | length) & partial_cacheline_write,
685 needs_clflush & CLFLUSH_AFTER);
694 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
696 i915_gem_object_unpin_pages(obj);
700 i915_gem_object_unpin_pages(obj);
702 i915_gem_object_unlock(obj);
707 * Writes data to the object referenced by handle.
709 * @data: ioctl data blob
712 * On error, the contents of the buffer that were to be modified are undefined.
715 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
716 struct drm_file *file)
718 struct drm_i915_private *i915 = to_i915(dev);
719 struct drm_i915_gem_pwrite *args = data;
720 struct drm_i915_gem_object *obj;
723 /* PWRITE is disallowed for all platforms after TGL-LP. This also
724 * covers all platforms with local memory.
726 if (GRAPHICS_VER(i915) >= 12 && !IS_TIGERLAKE(i915))
732 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
735 obj = i915_gem_object_lookup(file, args->handle);
739 /* Bounds check destination. */
740 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
745 /* Writes not allowed into this read-only object */
746 if (i915_gem_object_is_readonly(obj)) {
751 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
754 if (obj->ops->pwrite)
755 ret = obj->ops->pwrite(obj, args);
759 ret = i915_gem_object_wait(obj,
760 I915_WAIT_INTERRUPTIBLE |
762 MAX_SCHEDULE_TIMEOUT);
767 /* We can only do the GTT pwrite on untiled buffers, as otherwise
768 * it would end up going through the fenced access, and we'll get
769 * different detiling behavior between reading and writing.
770 * pread/pwrite currently are reading and writing from the CPU
771 * perspective, requiring manual detiling by the client.
773 if (!i915_gem_object_has_struct_page(obj) ||
774 i915_gem_cpu_write_needs_clflush(obj))
775 /* Note that the gtt paths might fail with non-page-backed user
776 * pointers (e.g. gtt mappings when moving data between
777 * textures). Fallback to the shmem path in that case.
779 ret = i915_gem_gtt_pwrite_fast(obj, args);
781 if (ret == -EFAULT || ret == -ENOSPC) {
782 if (i915_gem_object_has_struct_page(obj))
783 ret = i915_gem_shmem_pwrite(obj, args);
787 i915_gem_object_put(obj);
792 * Called when user space has done writes to this buffer
794 * @data: ioctl data blob
798 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
799 struct drm_file *file)
801 struct drm_i915_gem_sw_finish *args = data;
802 struct drm_i915_gem_object *obj;
804 obj = i915_gem_object_lookup(file, args->handle);
809 * Proxy objects are barred from CPU access, so there is no
810 * need to ban sw_finish as it is a nop.
813 /* Pinned buffers may be scanout, so flush the cache */
814 i915_gem_object_flush_if_display(obj);
815 i915_gem_object_put(obj);
820 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
822 struct drm_i915_gem_object *obj, *on;
826 * Only called during RPM suspend. All users of the userfault_list
827 * must be holding an RPM wakeref to ensure that this can not
828 * run concurrently with themselves (and use the struct_mutex for
829 * protection between themselves).
832 list_for_each_entry_safe(obj, on,
833 &to_gt(i915)->ggtt->userfault_list, userfault_link)
834 __i915_gem_object_release_mmap_gtt(obj);
837 * The fence will be lost when the device powers down. If any were
838 * in use by hardware (i.e. they are pinned), we should not be powering
839 * down! All other fences will be reacquired by the user upon waking.
841 for (i = 0; i < to_gt(i915)->ggtt->num_fences; i++) {
842 struct i915_fence_reg *reg = &to_gt(i915)->ggtt->fence_regs[i];
845 * Ideally we want to assert that the fence register is not
846 * live at this point (i.e. that no piece of code will be
847 * trying to write through fence + GTT, as that both violates
848 * our tracking of activity and associated locking/barriers,
849 * but also is illegal given that the hw is powered down).
851 * Previously we used reg->pin_count as a "liveness" indicator.
852 * That is not sufficient, and we need a more fine-grained
853 * tool if we want to have a sanity check here.
859 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
864 static void discard_ggtt_vma(struct i915_vma *vma)
866 struct drm_i915_gem_object *obj = vma->obj;
868 spin_lock(&obj->vma.lock);
869 if (!RB_EMPTY_NODE(&vma->obj_node)) {
870 rb_erase(&vma->obj_node, &obj->vma.tree);
871 RB_CLEAR_NODE(&vma->obj_node);
873 spin_unlock(&obj->vma.lock);
877 i915_gem_object_ggtt_pin_ww(struct drm_i915_gem_object *obj,
878 struct i915_gem_ww_ctx *ww,
879 const struct i915_ggtt_view *view,
880 u64 size, u64 alignment, u64 flags)
882 struct drm_i915_private *i915 = to_i915(obj->base.dev);
883 struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
884 struct i915_vma *vma;
889 if (flags & PIN_MAPPABLE &&
890 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
892 * If the required space is larger than the available
893 * aperture, we will not able to find a slot for the
894 * object and unbinding the object now will be in
895 * vain. Worse, doing so may cause us to ping-pong
896 * the object in and out of the Global GTT and
897 * waste a lot of cycles under the mutex.
899 if (obj->base.size > ggtt->mappable_end)
900 return ERR_PTR(-E2BIG);
903 * If NONBLOCK is set the caller is optimistically
904 * trying to cache the full object within the mappable
905 * aperture, and *must* have a fallback in place for
906 * situations where we cannot bind the object. We
907 * can be a little more lax here and use the fallback
908 * more often to avoid costly migrations of ourselves
909 * and other objects within the aperture.
911 * Half-the-aperture is used as a simple heuristic.
912 * More interesting would to do search for a free
913 * block prior to making the commitment to unbind.
914 * That caters for the self-harm case, and with a
915 * little more heuristics (e.g. NOFAULT, NOEVICT)
916 * we could try to minimise harm to others.
918 if (flags & PIN_NONBLOCK &&
919 obj->base.size > ggtt->mappable_end / 2)
920 return ERR_PTR(-ENOSPC);
924 vma = i915_vma_instance(obj, &ggtt->vm, view);
928 if (i915_vma_misplaced(vma, size, alignment, flags)) {
929 if (flags & PIN_NONBLOCK) {
930 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
931 return ERR_PTR(-ENOSPC);
933 if (flags & PIN_MAPPABLE &&
934 vma->fence_size > ggtt->mappable_end / 2)
935 return ERR_PTR(-ENOSPC);
938 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma)) {
939 discard_ggtt_vma(vma);
943 ret = i915_vma_unbind(vma);
948 ret = i915_vma_pin_ww(vma, ww, size, alignment, flags | PIN_GLOBAL);
953 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
954 mutex_lock(&ggtt->vm.mutex);
955 i915_vma_revoke_fence(vma);
956 mutex_unlock(&ggtt->vm.mutex);
959 ret = i915_vma_wait_for_bind(vma);
968 struct i915_vma * __must_check
969 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
970 const struct i915_ggtt_view *view,
971 u64 size, u64 alignment, u64 flags)
973 struct i915_gem_ww_ctx ww;
974 struct i915_vma *ret;
977 for_i915_gem_ww(&ww, err, true) {
978 err = i915_gem_object_lock(obj, &ww);
982 ret = i915_gem_object_ggtt_pin_ww(obj, &ww, view, size,
988 return err ? ERR_PTR(err) : ret;
992 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
993 struct drm_file *file_priv)
995 struct drm_i915_private *i915 = to_i915(dev);
996 struct drm_i915_gem_madvise *args = data;
997 struct drm_i915_gem_object *obj;
1000 switch (args->madv) {
1001 case I915_MADV_DONTNEED:
1002 case I915_MADV_WILLNEED:
1008 obj = i915_gem_object_lookup(file_priv, args->handle);
1012 err = i915_gem_object_lock_interruptible(obj, NULL);
1016 if (i915_gem_object_has_pages(obj) &&
1017 i915_gem_object_is_tiled(obj) &&
1018 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1019 if (obj->mm.madv == I915_MADV_WILLNEED) {
1020 GEM_BUG_ON(!i915_gem_object_has_tiling_quirk(obj));
1021 i915_gem_object_clear_tiling_quirk(obj);
1022 i915_gem_object_make_shrinkable(obj);
1024 if (args->madv == I915_MADV_WILLNEED) {
1025 GEM_BUG_ON(i915_gem_object_has_tiling_quirk(obj));
1026 i915_gem_object_make_unshrinkable(obj);
1027 i915_gem_object_set_tiling_quirk(obj);
1031 if (obj->mm.madv != __I915_MADV_PURGED) {
1032 obj->mm.madv = args->madv;
1033 if (obj->ops->adjust_lru)
1034 obj->ops->adjust_lru(obj);
1037 if (i915_gem_object_has_pages(obj) ||
1038 i915_gem_object_has_self_managed_shrink_list(obj)) {
1039 unsigned long flags;
1041 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1042 if (!list_empty(&obj->mm.link)) {
1043 struct list_head *list;
1045 if (obj->mm.madv != I915_MADV_WILLNEED)
1046 list = &i915->mm.purge_list;
1048 list = &i915->mm.shrink_list;
1049 list_move_tail(&obj->mm.link, list);
1052 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1055 /* if the object is no longer attached, discard its backing storage */
1056 if (obj->mm.madv == I915_MADV_DONTNEED &&
1057 !i915_gem_object_has_pages(obj))
1058 i915_gem_object_truncate(obj);
1060 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1062 i915_gem_object_unlock(obj);
1064 i915_gem_object_put(obj);
1068 int i915_gem_init(struct drm_i915_private *dev_priv)
1072 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1073 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1074 mkwrite_device_info(dev_priv)->page_sizes =
1075 I915_GTT_PAGE_SIZE_4K;
1077 ret = i915_gem_init_userptr(dev_priv);
1081 intel_uc_fetch_firmwares(&to_gt(dev_priv)->uc);
1082 intel_wopcm_init(&dev_priv->wopcm);
1084 ret = i915_init_ggtt(dev_priv);
1086 GEM_BUG_ON(ret == -EIO);
1091 * Despite its name intel_init_clock_gating applies both display
1092 * clock gating workarounds; GT mmio workarounds and the occasional
1093 * GT power context workaround. Worse, sometimes it includes a context
1094 * register workaround which we need to apply before we record the
1095 * default HW state for all contexts.
1097 * FIXME: break up the workarounds and apply them at the right time!
1099 intel_init_clock_gating(dev_priv);
1101 ret = intel_gt_init(to_gt(dev_priv));
1108 * Unwinding is complicated by that we want to handle -EIO to mean
1109 * disable GPU submission but keep KMS alive. We want to mark the
1110 * HW as irrevisibly wedged, but keep enough state around that the
1111 * driver doesn't explode during runtime.
1114 i915_gem_drain_workqueue(dev_priv);
1117 intel_uc_cleanup_firmwares(&to_gt(dev_priv)->uc);
1121 * Allow engines or uC initialisation to fail by marking the GPU
1122 * as wedged. But we only want to do this when the GPU is angry,
1123 * for all other failure, such as an allocation failure, bail.
1125 if (!intel_gt_is_wedged(to_gt(dev_priv))) {
1126 i915_probe_error(dev_priv,
1127 "Failed to initialize GPU, declaring it wedged!\n");
1128 intel_gt_set_wedged(to_gt(dev_priv));
1131 /* Minimal basic recovery for KMS */
1132 ret = i915_ggtt_enable_hw(dev_priv);
1133 i915_ggtt_resume(to_gt(dev_priv)->ggtt);
1134 intel_init_clock_gating(dev_priv);
1137 i915_gem_drain_freed_objects(dev_priv);
1142 void i915_gem_driver_register(struct drm_i915_private *i915)
1144 i915_gem_driver_register__shrinker(i915);
1146 intel_engines_driver_register(i915);
1149 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1151 i915_gem_driver_unregister__shrinker(i915);
1154 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1156 intel_wakeref_auto_fini(&to_gt(dev_priv)->ggtt->userfault_wakeref);
1158 i915_gem_suspend_late(dev_priv);
1159 intel_gt_driver_remove(to_gt(dev_priv));
1160 dev_priv->uabi_engines = RB_ROOT;
1162 /* Flush any outstanding unpin_work. */
1163 i915_gem_drain_workqueue(dev_priv);
1165 i915_gem_drain_freed_objects(dev_priv);
1168 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1170 intel_gt_driver_release(to_gt(dev_priv));
1172 intel_uc_cleanup_firmwares(&to_gt(dev_priv)->uc);
1174 i915_gem_drain_freed_objects(dev_priv);
1176 drm_WARN_ON(&dev_priv->drm, !list_empty(&dev_priv->gem.contexts.list));
1179 static void i915_gem_init__mm(struct drm_i915_private *i915)
1181 spin_lock_init(&i915->mm.obj_lock);
1183 init_llist_head(&i915->mm.free_list);
1185 INIT_LIST_HEAD(&i915->mm.purge_list);
1186 INIT_LIST_HEAD(&i915->mm.shrink_list);
1188 i915_gem_init__objects(i915);
1191 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1193 i915_gem_init__mm(dev_priv);
1194 i915_gem_init__contexts(dev_priv);
1196 spin_lock_init(&dev_priv->fb_tracking.lock);
1199 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1201 i915_gem_drain_freed_objects(dev_priv);
1202 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1203 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1204 drm_WARN_ON(&dev_priv->drm, dev_priv->mm.shrink_count);
1207 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1209 struct drm_i915_file_private *file_priv;
1214 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1218 file->driver_priv = file_priv;
1219 file_priv->dev_priv = i915;
1220 file_priv->file = file;
1222 file_priv->bsd_engine = -1;
1223 file_priv->hang_timestamp = jiffies;
1225 ret = i915_gem_context_open(i915, file);
1232 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1233 #include "selftests/mock_gem_device.c"
1234 #include "selftests/i915_gem.c"