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 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
184 struct drm_i915_gem_pwrite *args,
185 struct drm_file *file)
187 void *vaddr = sg_page(obj->mm.pages->sgl) + args->offset;
188 char __user *user_data = u64_to_user_ptr(args->data_ptr);
191 * We manually control the domain here and pretend that it
192 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
194 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
196 if (copy_from_user(vaddr, user_data, args->size))
199 drm_clflush_virt_range(vaddr, args->size);
200 intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
202 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
207 i915_gem_create(struct drm_file *file,
208 struct intel_memory_region *mr,
212 struct drm_i915_gem_object *obj;
217 GEM_BUG_ON(!is_power_of_2(mr->min_page_size));
218 size = round_up(*size_p, mr->min_page_size);
222 /* For most of the ABI (e.g. mmap) we think in system pages */
223 GEM_BUG_ON(!IS_ALIGNED(size, PAGE_SIZE));
225 /* Allocate the new object */
226 obj = i915_gem_object_create_region(mr, size, 0);
230 ret = drm_gem_handle_create(file, &obj->base, &handle);
231 /* drop reference from allocate - handle holds it now */
232 i915_gem_object_put(obj);
242 i915_gem_dumb_create(struct drm_file *file,
243 struct drm_device *dev,
244 struct drm_mode_create_dumb *args)
246 enum intel_memory_type mem_type;
247 int cpp = DIV_ROUND_UP(args->bpp, 8);
252 format = DRM_FORMAT_C8;
255 format = DRM_FORMAT_RGB565;
258 format = DRM_FORMAT_XRGB8888;
264 /* have to work out size/pitch and return them */
265 args->pitch = ALIGN(args->width * cpp, 64);
267 /* align stride to page size so that we can remap */
268 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
269 DRM_FORMAT_MOD_LINEAR))
270 args->pitch = ALIGN(args->pitch, 4096);
272 if (args->pitch < args->width)
275 args->size = mul_u32_u32(args->pitch, args->height);
277 mem_type = INTEL_MEMORY_SYSTEM;
278 if (HAS_LMEM(to_i915(dev)))
279 mem_type = INTEL_MEMORY_LOCAL;
281 return i915_gem_create(file,
282 intel_memory_region_by_type(to_i915(dev),
284 &args->size, &args->handle);
288 * Creates a new mm object and returns a handle to it.
289 * @dev: drm device pointer
290 * @data: ioctl data blob
291 * @file: drm file pointer
294 i915_gem_create_ioctl(struct drm_device *dev, void *data,
295 struct drm_file *file)
297 struct drm_i915_private *i915 = to_i915(dev);
298 struct drm_i915_gem_create *args = data;
300 i915_gem_flush_free_objects(i915);
302 return i915_gem_create(file,
303 intel_memory_region_by_type(i915,
304 INTEL_MEMORY_SYSTEM),
305 &args->size, &args->handle);
309 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
318 drm_clflush_virt_range(vaddr + offset, len);
320 ret = __copy_to_user(user_data, vaddr + offset, len);
324 return ret ? -EFAULT : 0;
328 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
329 struct drm_i915_gem_pread *args)
331 unsigned int needs_clflush;
332 unsigned int idx, offset;
333 struct dma_fence *fence;
334 char __user *user_data;
338 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
342 fence = i915_gem_object_lock_fence(obj);
343 i915_gem_object_finish_access(obj);
348 user_data = u64_to_user_ptr(args->data_ptr);
349 offset = offset_in_page(args->offset);
350 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
351 struct page *page = i915_gem_object_get_page(obj, idx);
352 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
354 ret = shmem_pread(page, offset, length, user_data,
364 i915_gem_object_unlock_fence(obj, fence);
369 gtt_user_read(struct io_mapping *mapping,
370 loff_t base, int offset,
371 char __user *user_data, int length)
374 unsigned long unwritten;
376 /* We can use the cpu mem copy function because this is X86. */
377 vaddr = io_mapping_map_atomic_wc(mapping, base);
378 unwritten = __copy_to_user_inatomic(user_data,
379 (void __force *)vaddr + offset,
381 io_mapping_unmap_atomic(vaddr);
383 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
384 unwritten = copy_to_user(user_data,
385 (void __force *)vaddr + offset,
387 io_mapping_unmap(vaddr);
393 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
394 const struct drm_i915_gem_pread *args)
396 struct drm_i915_private *i915 = to_i915(obj->base.dev);
397 struct i915_ggtt *ggtt = &i915->ggtt;
398 intel_wakeref_t wakeref;
399 struct drm_mm_node node;
400 struct dma_fence *fence;
401 void __user *user_data;
402 struct i915_vma *vma;
406 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
407 vma = ERR_PTR(-ENODEV);
408 if (!i915_gem_object_is_tiled(obj))
409 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
411 PIN_NONBLOCK /* NOWARN */ |
414 node.start = i915_ggtt_offset(vma);
417 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
420 GEM_BUG_ON(!drm_mm_node_allocated(&node));
423 ret = i915_gem_object_lock_interruptible(obj);
427 ret = i915_gem_object_set_to_gtt_domain(obj, false);
429 i915_gem_object_unlock(obj);
433 fence = i915_gem_object_lock_fence(obj);
434 i915_gem_object_unlock(obj);
440 user_data = u64_to_user_ptr(args->data_ptr);
442 offset = args->offset;
445 /* Operation in this page
447 * page_base = page offset within aperture
448 * page_offset = offset within page
449 * page_length = bytes to copy for this page
451 u32 page_base = node.start;
452 unsigned page_offset = offset_in_page(offset);
453 unsigned page_length = PAGE_SIZE - page_offset;
454 page_length = remain < page_length ? remain : page_length;
455 if (drm_mm_node_allocated(&node)) {
456 ggtt->vm.insert_page(&ggtt->vm,
457 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
458 node.start, I915_CACHE_NONE, 0);
460 page_base += offset & PAGE_MASK;
463 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
464 user_data, page_length)) {
469 remain -= page_length;
470 user_data += page_length;
471 offset += page_length;
474 i915_gem_object_unlock_fence(obj, fence);
476 if (drm_mm_node_allocated(&node)) {
477 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
478 remove_mappable_node(ggtt, &node);
483 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
488 * Reads data from the object referenced by handle.
489 * @dev: drm device pointer
490 * @data: ioctl data blob
491 * @file: drm file pointer
493 * On error, the contents of *data are undefined.
496 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
497 struct drm_file *file)
499 struct drm_i915_gem_pread *args = data;
500 struct drm_i915_gem_object *obj;
506 if (!access_ok(u64_to_user_ptr(args->data_ptr),
510 obj = i915_gem_object_lookup(file, args->handle);
514 /* Bounds check source. */
515 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
520 trace_i915_gem_object_pread(obj, args->offset, args->size);
522 ret = i915_gem_object_wait(obj,
523 I915_WAIT_INTERRUPTIBLE,
524 MAX_SCHEDULE_TIMEOUT);
528 ret = i915_gem_object_pin_pages(obj);
532 ret = i915_gem_shmem_pread(obj, args);
533 if (ret == -EFAULT || ret == -ENODEV)
534 ret = i915_gem_gtt_pread(obj, args);
536 i915_gem_object_unpin_pages(obj);
538 i915_gem_object_put(obj);
542 /* This is the fast write path which cannot handle
543 * page faults in the source data
547 ggtt_write(struct io_mapping *mapping,
548 loff_t base, int offset,
549 char __user *user_data, int length)
552 unsigned long unwritten;
554 /* We can use the cpu mem copy function because this is X86. */
555 vaddr = io_mapping_map_atomic_wc(mapping, base);
556 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
558 io_mapping_unmap_atomic(vaddr);
560 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
561 unwritten = copy_from_user((void __force *)vaddr + offset,
563 io_mapping_unmap(vaddr);
570 * This is the fast pwrite path, where we copy the data directly from the
571 * user into the GTT, uncached.
572 * @obj: i915 GEM object
573 * @args: pwrite arguments structure
576 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
577 const struct drm_i915_gem_pwrite *args)
579 struct drm_i915_private *i915 = to_i915(obj->base.dev);
580 struct i915_ggtt *ggtt = &i915->ggtt;
581 struct intel_runtime_pm *rpm = &i915->runtime_pm;
582 intel_wakeref_t wakeref;
583 struct drm_mm_node node;
584 struct dma_fence *fence;
585 struct i915_vma *vma;
587 void __user *user_data;
590 if (i915_gem_object_has_struct_page(obj)) {
592 * Avoid waking the device up if we can fallback, as
593 * waking/resuming is very slow (worst-case 10-100 ms
594 * depending on PCI sleeps and our own resume time).
595 * This easily dwarfs any performance advantage from
596 * using the cache bypass of indirect GGTT access.
598 wakeref = intel_runtime_pm_get_if_in_use(rpm);
602 /* No backing pages, no fallback, we must force GGTT access */
603 wakeref = intel_runtime_pm_get(rpm);
606 vma = ERR_PTR(-ENODEV);
607 if (!i915_gem_object_is_tiled(obj))
608 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
610 PIN_NONBLOCK /* NOWARN */ |
613 node.start = i915_ggtt_offset(vma);
616 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
619 GEM_BUG_ON(!drm_mm_node_allocated(&node));
622 ret = i915_gem_object_lock_interruptible(obj);
626 ret = i915_gem_object_set_to_gtt_domain(obj, true);
628 i915_gem_object_unlock(obj);
632 fence = i915_gem_object_lock_fence(obj);
633 i915_gem_object_unlock(obj);
639 i915_gem_object_invalidate_frontbuffer(obj, ORIGIN_CPU);
641 user_data = u64_to_user_ptr(args->data_ptr);
642 offset = args->offset;
645 /* Operation in this page
647 * page_base = page offset within aperture
648 * page_offset = offset within page
649 * page_length = bytes to copy for this page
651 u32 page_base = node.start;
652 unsigned int page_offset = offset_in_page(offset);
653 unsigned int page_length = PAGE_SIZE - page_offset;
654 page_length = remain < page_length ? remain : page_length;
655 if (drm_mm_node_allocated(&node)) {
656 /* flush the write before we modify the GGTT */
657 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
658 ggtt->vm.insert_page(&ggtt->vm,
659 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
660 node.start, I915_CACHE_NONE, 0);
661 wmb(); /* flush modifications to the GGTT (insert_page) */
663 page_base += offset & PAGE_MASK;
665 /* If we get a fault while copying data, then (presumably) our
666 * source page isn't available. Return the error and we'll
667 * retry in the slow path.
668 * If the object is non-shmem backed, we retry again with the
669 * path that handles page fault.
671 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
672 user_data, page_length)) {
677 remain -= page_length;
678 user_data += page_length;
679 offset += page_length;
682 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
683 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
685 i915_gem_object_unlock_fence(obj, fence);
687 if (drm_mm_node_allocated(&node)) {
688 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
689 remove_mappable_node(ggtt, &node);
694 intel_runtime_pm_put(rpm, wakeref);
698 /* Per-page copy function for the shmem pwrite fastpath.
699 * Flushes invalid cachelines before writing to the target if
700 * needs_clflush_before is set and flushes out any written cachelines after
701 * writing if needs_clflush is set.
704 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
705 bool needs_clflush_before,
706 bool needs_clflush_after)
713 if (needs_clflush_before)
714 drm_clflush_virt_range(vaddr + offset, len);
716 ret = __copy_from_user(vaddr + offset, user_data, len);
717 if (!ret && needs_clflush_after)
718 drm_clflush_virt_range(vaddr + offset, len);
722 return ret ? -EFAULT : 0;
726 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
727 const struct drm_i915_gem_pwrite *args)
729 unsigned int partial_cacheline_write;
730 unsigned int needs_clflush;
731 unsigned int offset, idx;
732 struct dma_fence *fence;
733 void __user *user_data;
737 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
741 fence = i915_gem_object_lock_fence(obj);
742 i915_gem_object_finish_access(obj);
746 /* If we don't overwrite a cacheline completely we need to be
747 * careful to have up-to-date data by first clflushing. Don't
748 * overcomplicate things and flush the entire patch.
750 partial_cacheline_write = 0;
751 if (needs_clflush & CLFLUSH_BEFORE)
752 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
754 user_data = u64_to_user_ptr(args->data_ptr);
756 offset = offset_in_page(args->offset);
757 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
758 struct page *page = i915_gem_object_get_page(obj, idx);
759 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
761 ret = shmem_pwrite(page, offset, length, user_data,
762 (offset | length) & partial_cacheline_write,
763 needs_clflush & CLFLUSH_AFTER);
772 i915_gem_object_flush_frontbuffer(obj, ORIGIN_CPU);
773 i915_gem_object_unlock_fence(obj, fence);
779 * Writes data to the object referenced by handle.
781 * @data: ioctl data blob
784 * On error, the contents of the buffer that were to be modified are undefined.
787 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
788 struct drm_file *file)
790 struct drm_i915_gem_pwrite *args = data;
791 struct drm_i915_gem_object *obj;
797 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
800 obj = i915_gem_object_lookup(file, args->handle);
804 /* Bounds check destination. */
805 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
810 /* Writes not allowed into this read-only object */
811 if (i915_gem_object_is_readonly(obj)) {
816 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
819 if (obj->ops->pwrite)
820 ret = obj->ops->pwrite(obj, args);
824 ret = i915_gem_object_wait(obj,
825 I915_WAIT_INTERRUPTIBLE |
827 MAX_SCHEDULE_TIMEOUT);
831 ret = i915_gem_object_pin_pages(obj);
836 /* We can only do the GTT pwrite on untiled buffers, as otherwise
837 * it would end up going through the fenced access, and we'll get
838 * different detiling behavior between reading and writing.
839 * pread/pwrite currently are reading and writing from the CPU
840 * perspective, requiring manual detiling by the client.
842 if (!i915_gem_object_has_struct_page(obj) ||
843 cpu_write_needs_clflush(obj))
844 /* Note that the gtt paths might fail with non-page-backed user
845 * pointers (e.g. gtt mappings when moving data between
846 * textures). Fallback to the shmem path in that case.
848 ret = i915_gem_gtt_pwrite_fast(obj, args);
850 if (ret == -EFAULT || ret == -ENOSPC) {
851 if (i915_gem_object_has_struct_page(obj))
852 ret = i915_gem_shmem_pwrite(obj, args);
854 ret = i915_gem_phys_pwrite(obj, args, file);
857 i915_gem_object_unpin_pages(obj);
859 i915_gem_object_put(obj);
864 * Called when user space has done writes to this buffer
866 * @data: ioctl data blob
870 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
871 struct drm_file *file)
873 struct drm_i915_gem_sw_finish *args = data;
874 struct drm_i915_gem_object *obj;
876 obj = i915_gem_object_lookup(file, args->handle);
881 * Proxy objects are barred from CPU access, so there is no
882 * need to ban sw_finish as it is a nop.
885 /* Pinned buffers may be scanout, so flush the cache */
886 i915_gem_object_flush_if_display(obj);
887 i915_gem_object_put(obj);
892 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
894 struct drm_i915_gem_object *obj, *on;
898 * Only called during RPM suspend. All users of the userfault_list
899 * must be holding an RPM wakeref to ensure that this can not
900 * run concurrently with themselves (and use the struct_mutex for
901 * protection between themselves).
904 list_for_each_entry_safe(obj, on,
905 &i915->ggtt.userfault_list, userfault_link)
906 __i915_gem_object_release_mmap_gtt(obj);
909 * The fence will be lost when the device powers down. If any were
910 * in use by hardware (i.e. they are pinned), we should not be powering
911 * down! All other fences will be reacquired by the user upon waking.
913 for (i = 0; i < i915->ggtt.num_fences; i++) {
914 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
917 * Ideally we want to assert that the fence register is not
918 * live at this point (i.e. that no piece of code will be
919 * trying to write through fence + GTT, as that both violates
920 * our tracking of activity and associated locking/barriers,
921 * but also is illegal given that the hw is powered down).
923 * Previously we used reg->pin_count as a "liveness" indicator.
924 * That is not sufficient, and we need a more fine-grained
925 * tool if we want to have a sanity check here.
931 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
937 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
938 const struct i915_ggtt_view *view,
943 struct drm_i915_private *i915 = to_i915(obj->base.dev);
944 struct i915_ggtt *ggtt = &i915->ggtt;
945 struct i915_vma *vma;
948 if (flags & PIN_MAPPABLE &&
949 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
951 * If the required space is larger than the available
952 * aperture, we will not able to find a slot for the
953 * object and unbinding the object now will be in
954 * vain. Worse, doing so may cause us to ping-pong
955 * the object in and out of the Global GTT and
956 * waste a lot of cycles under the mutex.
958 if (obj->base.size > ggtt->mappable_end)
959 return ERR_PTR(-E2BIG);
962 * If NONBLOCK is set the caller is optimistically
963 * trying to cache the full object within the mappable
964 * aperture, and *must* have a fallback in place for
965 * situations where we cannot bind the object. We
966 * can be a little more lax here and use the fallback
967 * more often to avoid costly migrations of ourselves
968 * and other objects within the aperture.
970 * Half-the-aperture is used as a simple heuristic.
971 * More interesting would to do search for a free
972 * block prior to making the commitment to unbind.
973 * That caters for the self-harm case, and with a
974 * little more heuristics (e.g. NOFAULT, NOEVICT)
975 * we could try to minimise harm to others.
977 if (flags & PIN_NONBLOCK &&
978 obj->base.size > ggtt->mappable_end / 2)
979 return ERR_PTR(-ENOSPC);
982 vma = i915_vma_instance(obj, &ggtt->vm, view);
986 if (i915_vma_misplaced(vma, size, alignment, flags)) {
987 if (flags & PIN_NONBLOCK) {
988 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
989 return ERR_PTR(-ENOSPC);
991 if (flags & PIN_MAPPABLE &&
992 vma->fence_size > ggtt->mappable_end / 2)
993 return ERR_PTR(-ENOSPC);
996 ret = i915_vma_unbind(vma);
1001 ret = i915_vma_pin(vma, size, alignment, flags | PIN_GLOBAL);
1003 return ERR_PTR(ret);
1005 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
1006 mutex_lock(&ggtt->vm.mutex);
1007 i915_vma_revoke_fence(vma);
1008 mutex_unlock(&ggtt->vm.mutex);
1011 ret = i915_vma_wait_for_bind(vma);
1013 i915_vma_unpin(vma);
1014 return ERR_PTR(ret);
1021 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
1022 struct drm_file *file_priv)
1024 struct drm_i915_private *i915 = to_i915(dev);
1025 struct drm_i915_gem_madvise *args = data;
1026 struct drm_i915_gem_object *obj;
1029 switch (args->madv) {
1030 case I915_MADV_DONTNEED:
1031 case I915_MADV_WILLNEED:
1037 obj = i915_gem_object_lookup(file_priv, args->handle);
1041 err = mutex_lock_interruptible(&obj->mm.lock);
1045 if (i915_gem_object_has_pages(obj) &&
1046 i915_gem_object_is_tiled(obj) &&
1047 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1048 if (obj->mm.madv == I915_MADV_WILLNEED) {
1049 GEM_BUG_ON(!obj->mm.quirked);
1050 __i915_gem_object_unpin_pages(obj);
1051 obj->mm.quirked = false;
1053 if (args->madv == I915_MADV_WILLNEED) {
1054 GEM_BUG_ON(obj->mm.quirked);
1055 __i915_gem_object_pin_pages(obj);
1056 obj->mm.quirked = true;
1060 if (obj->mm.madv != __I915_MADV_PURGED)
1061 obj->mm.madv = args->madv;
1063 if (i915_gem_object_has_pages(obj)) {
1064 struct list_head *list;
1066 if (i915_gem_object_is_shrinkable(obj)) {
1067 unsigned long flags;
1069 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1071 if (obj->mm.madv != I915_MADV_WILLNEED)
1072 list = &i915->mm.purge_list;
1074 list = &i915->mm.shrink_list;
1075 list_move_tail(&obj->mm.link, list);
1077 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1081 /* if the object is no longer attached, discard its backing storage */
1082 if (obj->mm.madv == I915_MADV_DONTNEED &&
1083 !i915_gem_object_has_pages(obj))
1084 i915_gem_object_truncate(obj);
1086 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1087 mutex_unlock(&obj->mm.lock);
1090 i915_gem_object_put(obj);
1094 int i915_gem_init(struct drm_i915_private *dev_priv)
1098 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1099 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1100 mkwrite_device_info(dev_priv)->page_sizes =
1101 I915_GTT_PAGE_SIZE_4K;
1103 ret = i915_gem_init_userptr(dev_priv);
1107 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1108 intel_wopcm_init(&dev_priv->wopcm);
1110 ret = i915_init_ggtt(dev_priv);
1112 GEM_BUG_ON(ret == -EIO);
1117 * Despite its name intel_init_clock_gating applies both display
1118 * clock gating workarounds; GT mmio workarounds and the occasional
1119 * GT power context workaround. Worse, sometimes it includes a context
1120 * register workaround which we need to apply before we record the
1121 * default HW state for all contexts.
1123 * FIXME: break up the workarounds and apply them at the right time!
1125 intel_init_clock_gating(dev_priv);
1127 ret = intel_gt_init(&dev_priv->gt);
1134 * Unwinding is complicated by that we want to handle -EIO to mean
1135 * disable GPU submission but keep KMS alive. We want to mark the
1136 * HW as irrevisibly wedged, but keep enough state around that the
1137 * driver doesn't explode during runtime.
1140 i915_gem_drain_workqueue(dev_priv);
1143 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1144 i915_gem_cleanup_userptr(dev_priv);
1149 * Allow engines or uC initialisation to fail by marking the GPU
1150 * as wedged. But we only want to do this when the GPU is angry,
1151 * for all other failure, such as an allocation failure, bail.
1153 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1154 i915_probe_error(dev_priv,
1155 "Failed to initialize GPU, declaring it wedged!\n");
1156 intel_gt_set_wedged(&dev_priv->gt);
1159 /* Minimal basic recovery for KMS */
1160 ret = i915_ggtt_enable_hw(dev_priv);
1161 i915_ggtt_resume(&dev_priv->ggtt);
1162 intel_init_clock_gating(dev_priv);
1165 i915_gem_drain_freed_objects(dev_priv);
1169 void i915_gem_driver_register(struct drm_i915_private *i915)
1171 i915_gem_driver_register__shrinker(i915);
1173 intel_engines_driver_register(i915);
1176 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1178 i915_gem_driver_unregister__shrinker(i915);
1181 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1183 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1185 i915_gem_suspend_late(dev_priv);
1186 intel_gt_driver_remove(&dev_priv->gt);
1187 dev_priv->uabi_engines = RB_ROOT;
1189 /* Flush any outstanding unpin_work. */
1190 i915_gem_drain_workqueue(dev_priv);
1192 i915_gem_drain_freed_objects(dev_priv);
1195 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1197 i915_gem_driver_release__contexts(dev_priv);
1199 intel_gt_driver_release(&dev_priv->gt);
1201 intel_wa_list_free(&dev_priv->gt_wa_list);
1203 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1204 i915_gem_cleanup_userptr(dev_priv);
1206 i915_gem_drain_freed_objects(dev_priv);
1208 drm_WARN_ON(&dev_priv->drm, !list_empty(&dev_priv->gem.contexts.list));
1211 static void i915_gem_init__mm(struct drm_i915_private *i915)
1213 spin_lock_init(&i915->mm.obj_lock);
1215 init_llist_head(&i915->mm.free_list);
1217 INIT_LIST_HEAD(&i915->mm.purge_list);
1218 INIT_LIST_HEAD(&i915->mm.shrink_list);
1220 i915_gem_init__objects(i915);
1223 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1225 i915_gem_init__mm(dev_priv);
1226 i915_gem_init__contexts(dev_priv);
1228 spin_lock_init(&dev_priv->fb_tracking.lock);
1231 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1233 i915_gem_drain_freed_objects(dev_priv);
1234 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1235 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1236 drm_WARN_ON(&dev_priv->drm, dev_priv->mm.shrink_count);
1239 int i915_gem_freeze(struct drm_i915_private *dev_priv)
1241 /* Discard all purgeable objects, let userspace recover those as
1242 * required after resuming.
1244 i915_gem_shrink_all(dev_priv);
1249 int i915_gem_freeze_late(struct drm_i915_private *i915)
1251 struct drm_i915_gem_object *obj;
1252 intel_wakeref_t wakeref;
1255 * Called just before we write the hibernation image.
1257 * We need to update the domain tracking to reflect that the CPU
1258 * will be accessing all the pages to create and restore from the
1259 * hibernation, and so upon restoration those pages will be in the
1262 * To make sure the hibernation image contains the latest state,
1263 * we update that state just before writing out the image.
1265 * To try and reduce the hibernation image, we manually shrink
1266 * the objects as well, see i915_gem_freeze()
1269 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1271 i915_gem_shrink(i915, -1UL, NULL, ~0);
1272 i915_gem_drain_freed_objects(i915);
1274 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1275 i915_gem_object_lock(obj);
1276 drm_WARN_ON(&i915->drm,
1277 i915_gem_object_set_to_cpu_domain(obj, true));
1278 i915_gem_object_unlock(obj);
1281 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1286 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1288 struct drm_i915_file_private *file_priv = file->driver_priv;
1289 struct i915_request *request;
1291 /* Clean up our request list when the client is going away, so that
1292 * later retire_requests won't dereference our soon-to-be-gone
1295 spin_lock(&file_priv->mm.lock);
1296 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1297 request->file_priv = NULL;
1298 spin_unlock(&file_priv->mm.lock);
1301 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1303 struct drm_i915_file_private *file_priv;
1308 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1312 file->driver_priv = file_priv;
1313 file_priv->dev_priv = i915;
1314 file_priv->file = file;
1316 spin_lock_init(&file_priv->mm.lock);
1317 INIT_LIST_HEAD(&file_priv->mm.request_list);
1319 file_priv->bsd_engine = -1;
1320 file_priv->hang_timestamp = jiffies;
1322 ret = i915_gem_context_open(i915, file);
1329 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1330 #include "selftests/mock_gem_device.c"
1331 #include "selftests/i915_gem.c"