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 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_create(struct drm_file
*file
,
184 struct intel_memory_region
*mr
,
188 struct drm_i915_gem_object
*obj
;
193 GEM_BUG_ON(!is_power_of_2(mr
->min_page_size
));
194 size
= round_up(*size_p
, mr
->min_page_size
);
198 /* For most of the ABI (e.g. mmap) we think in system pages */
199 GEM_BUG_ON(!IS_ALIGNED(size
, PAGE_SIZE
));
201 /* Allocate the new object */
202 obj
= i915_gem_object_create_region(mr
, size
, 0);
206 ret
= drm_gem_handle_create(file
, &obj
->base
, &handle
);
207 /* drop reference from allocate - handle holds it now */
208 i915_gem_object_put(obj
);
218 i915_gem_dumb_create(struct drm_file
*file
,
219 struct drm_device
*dev
,
220 struct drm_mode_create_dumb
*args
)
222 enum intel_memory_type mem_type
;
223 int cpp
= DIV_ROUND_UP(args
->bpp
, 8);
228 format
= DRM_FORMAT_C8
;
231 format
= DRM_FORMAT_RGB565
;
234 format
= DRM_FORMAT_XRGB8888
;
240 /* have to work out size/pitch and return them */
241 args
->pitch
= ALIGN(args
->width
* cpp
, 64);
243 /* align stride to page size so that we can remap */
244 if (args
->pitch
> intel_plane_fb_max_stride(to_i915(dev
), format
,
245 DRM_FORMAT_MOD_LINEAR
))
246 args
->pitch
= ALIGN(args
->pitch
, 4096);
248 if (args
->pitch
< args
->width
)
251 args
->size
= mul_u32_u32(args
->pitch
, args
->height
);
253 mem_type
= INTEL_MEMORY_SYSTEM
;
254 if (HAS_LMEM(to_i915(dev
)))
255 mem_type
= INTEL_MEMORY_LOCAL
;
257 return i915_gem_create(file
,
258 intel_memory_region_by_type(to_i915(dev
),
260 &args
->size
, &args
->handle
);
264 * Creates a new mm object and returns a handle to it.
265 * @dev: drm device pointer
266 * @data: ioctl data blob
267 * @file: drm file pointer
270 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
271 struct drm_file
*file
)
273 struct drm_i915_private
*i915
= to_i915(dev
);
274 struct drm_i915_gem_create
*args
= data
;
276 i915_gem_flush_free_objects(i915
);
278 return i915_gem_create(file
,
279 intel_memory_region_by_type(i915
,
280 INTEL_MEMORY_SYSTEM
),
281 &args
->size
, &args
->handle
);
285 shmem_pread(struct page
*page
, int offset
, int len
, char __user
*user_data
,
294 drm_clflush_virt_range(vaddr
+ offset
, len
);
296 ret
= __copy_to_user(user_data
, vaddr
+ offset
, len
);
300 return ret
? -EFAULT
: 0;
304 i915_gem_shmem_pread(struct drm_i915_gem_object
*obj
,
305 struct drm_i915_gem_pread
*args
)
307 unsigned int needs_clflush
;
308 unsigned int idx
, offset
;
309 struct dma_fence
*fence
;
310 char __user
*user_data
;
314 ret
= i915_gem_object_lock_interruptible(obj
, NULL
);
318 ret
= i915_gem_object_prepare_read(obj
, &needs_clflush
);
320 i915_gem_object_unlock(obj
);
324 fence
= i915_gem_object_lock_fence(obj
);
325 i915_gem_object_finish_access(obj
);
326 i915_gem_object_unlock(obj
);
332 user_data
= u64_to_user_ptr(args
->data_ptr
);
333 offset
= offset_in_page(args
->offset
);
334 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
335 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
336 unsigned int length
= min_t(u64
, remain
, PAGE_SIZE
- offset
);
338 ret
= shmem_pread(page
, offset
, length
, user_data
,
348 i915_gem_object_unlock_fence(obj
, fence
);
353 gtt_user_read(struct io_mapping
*mapping
,
354 loff_t base
, int offset
,
355 char __user
*user_data
, int length
)
358 unsigned long unwritten
;
360 /* We can use the cpu mem copy function because this is X86. */
361 vaddr
= io_mapping_map_atomic_wc(mapping
, base
);
362 unwritten
= __copy_to_user_inatomic(user_data
,
363 (void __force
*)vaddr
+ offset
,
365 io_mapping_unmap_atomic(vaddr
);
367 vaddr
= io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
368 unwritten
= copy_to_user(user_data
,
369 (void __force
*)vaddr
+ offset
,
371 io_mapping_unmap(vaddr
);
377 i915_gem_gtt_pread(struct drm_i915_gem_object
*obj
,
378 const struct drm_i915_gem_pread
*args
)
380 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
381 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
382 intel_wakeref_t wakeref
;
383 struct drm_mm_node node
;
384 struct dma_fence
*fence
;
385 void __user
*user_data
;
386 struct i915_vma
*vma
;
390 wakeref
= intel_runtime_pm_get(&i915
->runtime_pm
);
391 vma
= ERR_PTR(-ENODEV
);
392 if (!i915_gem_object_is_tiled(obj
))
393 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
395 PIN_NONBLOCK
/* NOWARN */ |
398 node
.start
= i915_ggtt_offset(vma
);
401 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
404 GEM_BUG_ON(!drm_mm_node_allocated(&node
));
407 ret
= i915_gem_object_lock_interruptible(obj
, NULL
);
411 ret
= i915_gem_object_set_to_gtt_domain(obj
, false);
413 i915_gem_object_unlock(obj
);
417 fence
= i915_gem_object_lock_fence(obj
);
418 i915_gem_object_unlock(obj
);
424 user_data
= u64_to_user_ptr(args
->data_ptr
);
426 offset
= args
->offset
;
429 /* Operation in this page
431 * page_base = page offset within aperture
432 * page_offset = offset within page
433 * page_length = bytes to copy for this page
435 u32 page_base
= node
.start
;
436 unsigned page_offset
= offset_in_page(offset
);
437 unsigned page_length
= PAGE_SIZE
- page_offset
;
438 page_length
= remain
< page_length
? remain
: page_length
;
439 if (drm_mm_node_allocated(&node
)) {
440 ggtt
->vm
.insert_page(&ggtt
->vm
,
441 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
442 node
.start
, I915_CACHE_NONE
, 0);
444 page_base
+= offset
& PAGE_MASK
;
447 if (gtt_user_read(&ggtt
->iomap
, page_base
, page_offset
,
448 user_data
, page_length
)) {
453 remain
-= page_length
;
454 user_data
+= page_length
;
455 offset
+= page_length
;
458 i915_gem_object_unlock_fence(obj
, fence
);
460 if (drm_mm_node_allocated(&node
)) {
461 ggtt
->vm
.clear_range(&ggtt
->vm
, node
.start
, node
.size
);
462 remove_mappable_node(ggtt
, &node
);
467 intel_runtime_pm_put(&i915
->runtime_pm
, wakeref
);
472 * Reads data from the object referenced by handle.
473 * @dev: drm device pointer
474 * @data: ioctl data blob
475 * @file: drm file pointer
477 * On error, the contents of *data are undefined.
480 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
481 struct drm_file
*file
)
483 struct drm_i915_gem_pread
*args
= data
;
484 struct drm_i915_gem_object
*obj
;
490 if (!access_ok(u64_to_user_ptr(args
->data_ptr
),
494 obj
= i915_gem_object_lookup(file
, args
->handle
);
498 /* Bounds check source. */
499 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
504 trace_i915_gem_object_pread(obj
, args
->offset
, args
->size
);
508 ret
= obj
->ops
->pread(obj
, args
);
512 ret
= i915_gem_object_wait(obj
,
513 I915_WAIT_INTERRUPTIBLE
,
514 MAX_SCHEDULE_TIMEOUT
);
518 ret
= i915_gem_object_pin_pages(obj
);
522 ret
= i915_gem_shmem_pread(obj
, args
);
523 if (ret
== -EFAULT
|| ret
== -ENODEV
)
524 ret
= i915_gem_gtt_pread(obj
, args
);
526 i915_gem_object_unpin_pages(obj
);
528 i915_gem_object_put(obj
);
532 /* This is the fast write path which cannot handle
533 * page faults in the source data
537 ggtt_write(struct io_mapping
*mapping
,
538 loff_t base
, int offset
,
539 char __user
*user_data
, int length
)
542 unsigned long unwritten
;
544 /* We can use the cpu mem copy function because this is X86. */
545 vaddr
= io_mapping_map_atomic_wc(mapping
, base
);
546 unwritten
= __copy_from_user_inatomic_nocache((void __force
*)vaddr
+ offset
,
548 io_mapping_unmap_atomic(vaddr
);
550 vaddr
= io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
551 unwritten
= copy_from_user((void __force
*)vaddr
+ offset
,
553 io_mapping_unmap(vaddr
);
560 * This is the fast pwrite path, where we copy the data directly from the
561 * user into the GTT, uncached.
562 * @obj: i915 GEM object
563 * @args: pwrite arguments structure
566 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object
*obj
,
567 const struct drm_i915_gem_pwrite
*args
)
569 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
570 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
571 struct intel_runtime_pm
*rpm
= &i915
->runtime_pm
;
572 intel_wakeref_t wakeref
;
573 struct drm_mm_node node
;
574 struct dma_fence
*fence
;
575 struct i915_vma
*vma
;
577 void __user
*user_data
;
580 if (i915_gem_object_has_struct_page(obj
)) {
582 * Avoid waking the device up if we can fallback, as
583 * waking/resuming is very slow (worst-case 10-100 ms
584 * depending on PCI sleeps and our own resume time).
585 * This easily dwarfs any performance advantage from
586 * using the cache bypass of indirect GGTT access.
588 wakeref
= intel_runtime_pm_get_if_in_use(rpm
);
592 /* No backing pages, no fallback, we must force GGTT access */
593 wakeref
= intel_runtime_pm_get(rpm
);
596 vma
= ERR_PTR(-ENODEV
);
597 if (!i915_gem_object_is_tiled(obj
))
598 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
600 PIN_NONBLOCK
/* NOWARN */ |
603 node
.start
= i915_ggtt_offset(vma
);
606 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
609 GEM_BUG_ON(!drm_mm_node_allocated(&node
));
612 ret
= i915_gem_object_lock_interruptible(obj
, NULL
);
616 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
618 i915_gem_object_unlock(obj
);
622 fence
= i915_gem_object_lock_fence(obj
);
623 i915_gem_object_unlock(obj
);
629 i915_gem_object_invalidate_frontbuffer(obj
, ORIGIN_CPU
);
631 user_data
= u64_to_user_ptr(args
->data_ptr
);
632 offset
= args
->offset
;
635 /* Operation in this page
637 * page_base = page offset within aperture
638 * page_offset = offset within page
639 * page_length = bytes to copy for this page
641 u32 page_base
= node
.start
;
642 unsigned int page_offset
= offset_in_page(offset
);
643 unsigned int page_length
= PAGE_SIZE
- page_offset
;
644 page_length
= remain
< page_length
? remain
: page_length
;
645 if (drm_mm_node_allocated(&node
)) {
646 /* flush the write before we modify the GGTT */
647 intel_gt_flush_ggtt_writes(ggtt
->vm
.gt
);
648 ggtt
->vm
.insert_page(&ggtt
->vm
,
649 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
650 node
.start
, I915_CACHE_NONE
, 0);
651 wmb(); /* flush modifications to the GGTT (insert_page) */
653 page_base
+= offset
& PAGE_MASK
;
655 /* If we get a fault while copying data, then (presumably) our
656 * source page isn't available. Return the error and we'll
657 * retry in the slow path.
658 * If the object is non-shmem backed, we retry again with the
659 * path that handles page fault.
661 if (ggtt_write(&ggtt
->iomap
, page_base
, page_offset
,
662 user_data
, page_length
)) {
667 remain
-= page_length
;
668 user_data
+= page_length
;
669 offset
+= page_length
;
672 intel_gt_flush_ggtt_writes(ggtt
->vm
.gt
);
673 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
675 i915_gem_object_unlock_fence(obj
, fence
);
677 if (drm_mm_node_allocated(&node
)) {
678 ggtt
->vm
.clear_range(&ggtt
->vm
, node
.start
, node
.size
);
679 remove_mappable_node(ggtt
, &node
);
684 intel_runtime_pm_put(rpm
, wakeref
);
688 /* Per-page copy function for the shmem pwrite fastpath.
689 * Flushes invalid cachelines before writing to the target if
690 * needs_clflush_before is set and flushes out any written cachelines after
691 * writing if needs_clflush is set.
694 shmem_pwrite(struct page
*page
, int offset
, int len
, char __user
*user_data
,
695 bool needs_clflush_before
,
696 bool needs_clflush_after
)
703 if (needs_clflush_before
)
704 drm_clflush_virt_range(vaddr
+ offset
, len
);
706 ret
= __copy_from_user(vaddr
+ offset
, user_data
, len
);
707 if (!ret
&& needs_clflush_after
)
708 drm_clflush_virt_range(vaddr
+ offset
, len
);
712 return ret
? -EFAULT
: 0;
716 i915_gem_shmem_pwrite(struct drm_i915_gem_object
*obj
,
717 const struct drm_i915_gem_pwrite
*args
)
719 unsigned int partial_cacheline_write
;
720 unsigned int needs_clflush
;
721 unsigned int offset
, idx
;
722 struct dma_fence
*fence
;
723 void __user
*user_data
;
727 ret
= i915_gem_object_lock_interruptible(obj
, NULL
);
731 ret
= i915_gem_object_prepare_write(obj
, &needs_clflush
);
733 i915_gem_object_unlock(obj
);
737 fence
= i915_gem_object_lock_fence(obj
);
738 i915_gem_object_finish_access(obj
);
739 i915_gem_object_unlock(obj
);
744 /* If we don't overwrite a cacheline completely we need to be
745 * careful to have up-to-date data by first clflushing. Don't
746 * overcomplicate things and flush the entire patch.
748 partial_cacheline_write
= 0;
749 if (needs_clflush
& CLFLUSH_BEFORE
)
750 partial_cacheline_write
= boot_cpu_data
.x86_clflush_size
- 1;
752 user_data
= u64_to_user_ptr(args
->data_ptr
);
754 offset
= offset_in_page(args
->offset
);
755 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
756 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
757 unsigned int length
= min_t(u64
, remain
, PAGE_SIZE
- offset
);
759 ret
= shmem_pwrite(page
, offset
, length
, user_data
,
760 (offset
| length
) & partial_cacheline_write
,
761 needs_clflush
& CLFLUSH_AFTER
);
770 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
771 i915_gem_object_unlock_fence(obj
, fence
);
777 * Writes data to the object referenced by handle.
779 * @data: ioctl data blob
782 * On error, the contents of the buffer that were to be modified are undefined.
785 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
786 struct drm_file
*file
)
788 struct drm_i915_gem_pwrite
*args
= data
;
789 struct drm_i915_gem_object
*obj
;
795 if (!access_ok(u64_to_user_ptr(args
->data_ptr
), args
->size
))
798 obj
= i915_gem_object_lookup(file
, args
->handle
);
802 /* Bounds check destination. */
803 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
808 /* Writes not allowed into this read-only object */
809 if (i915_gem_object_is_readonly(obj
)) {
814 trace_i915_gem_object_pwrite(obj
, args
->offset
, args
->size
);
817 if (obj
->ops
->pwrite
)
818 ret
= obj
->ops
->pwrite(obj
, args
);
822 ret
= i915_gem_object_wait(obj
,
823 I915_WAIT_INTERRUPTIBLE
|
825 MAX_SCHEDULE_TIMEOUT
);
829 ret
= i915_gem_object_pin_pages(obj
);
834 /* We can only do the GTT pwrite on untiled buffers, as otherwise
835 * it would end up going through the fenced access, and we'll get
836 * different detiling behavior between reading and writing.
837 * pread/pwrite currently are reading and writing from the CPU
838 * perspective, requiring manual detiling by the client.
840 if (!i915_gem_object_has_struct_page(obj
) ||
841 cpu_write_needs_clflush(obj
))
842 /* Note that the gtt paths might fail with non-page-backed user
843 * pointers (e.g. gtt mappings when moving data between
844 * textures). Fallback to the shmem path in that case.
846 ret
= i915_gem_gtt_pwrite_fast(obj
, args
);
848 if (ret
== -EFAULT
|| ret
== -ENOSPC
) {
849 if (i915_gem_object_has_struct_page(obj
))
850 ret
= i915_gem_shmem_pwrite(obj
, args
);
853 i915_gem_object_unpin_pages(obj
);
855 i915_gem_object_put(obj
);
860 * Called when user space has done writes to this buffer
862 * @data: ioctl data blob
866 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
867 struct drm_file
*file
)
869 struct drm_i915_gem_sw_finish
*args
= data
;
870 struct drm_i915_gem_object
*obj
;
872 obj
= i915_gem_object_lookup(file
, args
->handle
);
877 * Proxy objects are barred from CPU access, so there is no
878 * need to ban sw_finish as it is a nop.
881 /* Pinned buffers may be scanout, so flush the cache */
882 i915_gem_object_flush_if_display(obj
);
883 i915_gem_object_put(obj
);
888 void i915_gem_runtime_suspend(struct drm_i915_private
*i915
)
890 struct drm_i915_gem_object
*obj
, *on
;
894 * Only called during RPM suspend. All users of the userfault_list
895 * must be holding an RPM wakeref to ensure that this can not
896 * run concurrently with themselves (and use the struct_mutex for
897 * protection between themselves).
900 list_for_each_entry_safe(obj
, on
,
901 &i915
->ggtt
.userfault_list
, userfault_link
)
902 __i915_gem_object_release_mmap_gtt(obj
);
905 * The fence will be lost when the device powers down. If any were
906 * in use by hardware (i.e. they are pinned), we should not be powering
907 * down! All other fences will be reacquired by the user upon waking.
909 for (i
= 0; i
< i915
->ggtt
.num_fences
; i
++) {
910 struct i915_fence_reg
*reg
= &i915
->ggtt
.fence_regs
[i
];
913 * Ideally we want to assert that the fence register is not
914 * live at this point (i.e. that no piece of code will be
915 * trying to write through fence + GTT, as that both violates
916 * our tracking of activity and associated locking/barriers,
917 * but also is illegal given that the hw is powered down).
919 * Previously we used reg->pin_count as a "liveness" indicator.
920 * That is not sufficient, and we need a more fine-grained
921 * tool if we want to have a sanity check here.
927 GEM_BUG_ON(i915_vma_has_userfault(reg
->vma
));
932 static void discard_ggtt_vma(struct i915_vma
*vma
)
934 struct drm_i915_gem_object
*obj
= vma
->obj
;
936 spin_lock(&obj
->vma
.lock
);
937 if (!RB_EMPTY_NODE(&vma
->obj_node
)) {
938 rb_erase(&vma
->obj_node
, &obj
->vma
.tree
);
939 RB_CLEAR_NODE(&vma
->obj_node
);
941 spin_unlock(&obj
->vma
.lock
);
945 i915_gem_object_ggtt_pin_ww(struct drm_i915_gem_object
*obj
,
946 struct i915_gem_ww_ctx
*ww
,
947 const struct i915_ggtt_view
*view
,
948 u64 size
, u64 alignment
, u64 flags
)
950 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
951 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
952 struct i915_vma
*vma
;
955 if (flags
& PIN_MAPPABLE
&&
956 (!view
|| view
->type
== I915_GGTT_VIEW_NORMAL
)) {
958 * If the required space is larger than the available
959 * aperture, we will not able to find a slot for the
960 * object and unbinding the object now will be in
961 * vain. Worse, doing so may cause us to ping-pong
962 * the object in and out of the Global GTT and
963 * waste a lot of cycles under the mutex.
965 if (obj
->base
.size
> ggtt
->mappable_end
)
966 return ERR_PTR(-E2BIG
);
969 * If NONBLOCK is set the caller is optimistically
970 * trying to cache the full object within the mappable
971 * aperture, and *must* have a fallback in place for
972 * situations where we cannot bind the object. We
973 * can be a little more lax here and use the fallback
974 * more often to avoid costly migrations of ourselves
975 * and other objects within the aperture.
977 * Half-the-aperture is used as a simple heuristic.
978 * More interesting would to do search for a free
979 * block prior to making the commitment to unbind.
980 * That caters for the self-harm case, and with a
981 * little more heuristics (e.g. NOFAULT, NOEVICT)
982 * we could try to minimise harm to others.
984 if (flags
& PIN_NONBLOCK
&&
985 obj
->base
.size
> ggtt
->mappable_end
/ 2)
986 return ERR_PTR(-ENOSPC
);
990 vma
= i915_vma_instance(obj
, &ggtt
->vm
, view
);
994 if (i915_vma_misplaced(vma
, size
, alignment
, flags
)) {
995 if (flags
& PIN_NONBLOCK
) {
996 if (i915_vma_is_pinned(vma
) || i915_vma_is_active(vma
))
997 return ERR_PTR(-ENOSPC
);
999 if (flags
& PIN_MAPPABLE
&&
1000 vma
->fence_size
> ggtt
->mappable_end
/ 2)
1001 return ERR_PTR(-ENOSPC
);
1004 if (i915_vma_is_pinned(vma
) || i915_vma_is_active(vma
)) {
1005 discard_ggtt_vma(vma
);
1009 ret
= i915_vma_unbind(vma
);
1011 return ERR_PTR(ret
);
1014 ret
= i915_vma_pin_ww(vma
, ww
, size
, alignment
, flags
| PIN_GLOBAL
);
1016 return ERR_PTR(ret
);
1018 if (vma
->fence
&& !i915_gem_object_is_tiled(obj
)) {
1019 mutex_lock(&ggtt
->vm
.mutex
);
1020 i915_vma_revoke_fence(vma
);
1021 mutex_unlock(&ggtt
->vm
.mutex
);
1024 ret
= i915_vma_wait_for_bind(vma
);
1026 i915_vma_unpin(vma
);
1027 return ERR_PTR(ret
);
1034 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
1035 struct drm_file
*file_priv
)
1037 struct drm_i915_private
*i915
= to_i915(dev
);
1038 struct drm_i915_gem_madvise
*args
= data
;
1039 struct drm_i915_gem_object
*obj
;
1042 switch (args
->madv
) {
1043 case I915_MADV_DONTNEED
:
1044 case I915_MADV_WILLNEED
:
1050 obj
= i915_gem_object_lookup(file_priv
, args
->handle
);
1054 err
= mutex_lock_interruptible(&obj
->mm
.lock
);
1058 if (i915_gem_object_has_pages(obj
) &&
1059 i915_gem_object_is_tiled(obj
) &&
1060 i915
->quirks
& QUIRK_PIN_SWIZZLED_PAGES
) {
1061 if (obj
->mm
.madv
== I915_MADV_WILLNEED
) {
1062 GEM_BUG_ON(!obj
->mm
.quirked
);
1063 __i915_gem_object_unpin_pages(obj
);
1064 obj
->mm
.quirked
= false;
1066 if (args
->madv
== I915_MADV_WILLNEED
) {
1067 GEM_BUG_ON(obj
->mm
.quirked
);
1068 __i915_gem_object_pin_pages(obj
);
1069 obj
->mm
.quirked
= true;
1073 if (obj
->mm
.madv
!= __I915_MADV_PURGED
)
1074 obj
->mm
.madv
= args
->madv
;
1076 if (i915_gem_object_has_pages(obj
)) {
1077 struct list_head
*list
;
1079 if (i915_gem_object_is_shrinkable(obj
)) {
1080 unsigned long flags
;
1082 spin_lock_irqsave(&i915
->mm
.obj_lock
, flags
);
1084 if (obj
->mm
.madv
!= I915_MADV_WILLNEED
)
1085 list
= &i915
->mm
.purge_list
;
1087 list
= &i915
->mm
.shrink_list
;
1088 list_move_tail(&obj
->mm
.link
, list
);
1090 spin_unlock_irqrestore(&i915
->mm
.obj_lock
, flags
);
1094 /* if the object is no longer attached, discard its backing storage */
1095 if (obj
->mm
.madv
== I915_MADV_DONTNEED
&&
1096 !i915_gem_object_has_pages(obj
))
1097 i915_gem_object_truncate(obj
);
1099 args
->retained
= obj
->mm
.madv
!= __I915_MADV_PURGED
;
1100 mutex_unlock(&obj
->mm
.lock
);
1103 i915_gem_object_put(obj
);
1107 int i915_gem_init(struct drm_i915_private
*dev_priv
)
1111 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1112 if (intel_vgpu_active(dev_priv
) && !intel_vgpu_has_huge_gtt(dev_priv
))
1113 mkwrite_device_info(dev_priv
)->page_sizes
=
1114 I915_GTT_PAGE_SIZE_4K
;
1116 ret
= i915_gem_init_userptr(dev_priv
);
1120 intel_uc_fetch_firmwares(&dev_priv
->gt
.uc
);
1121 intel_wopcm_init(&dev_priv
->wopcm
);
1123 ret
= i915_init_ggtt(dev_priv
);
1125 GEM_BUG_ON(ret
== -EIO
);
1130 * Despite its name intel_init_clock_gating applies both display
1131 * clock gating workarounds; GT mmio workarounds and the occasional
1132 * GT power context workaround. Worse, sometimes it includes a context
1133 * register workaround which we need to apply before we record the
1134 * default HW state for all contexts.
1136 * FIXME: break up the workarounds and apply them at the right time!
1138 intel_init_clock_gating(dev_priv
);
1140 ret
= intel_gt_init(&dev_priv
->gt
);
1147 * Unwinding is complicated by that we want to handle -EIO to mean
1148 * disable GPU submission but keep KMS alive. We want to mark the
1149 * HW as irrevisibly wedged, but keep enough state around that the
1150 * driver doesn't explode during runtime.
1153 i915_gem_drain_workqueue(dev_priv
);
1156 intel_uc_cleanup_firmwares(&dev_priv
->gt
.uc
);
1157 i915_gem_cleanup_userptr(dev_priv
);
1162 * Allow engines or uC initialisation to fail by marking the GPU
1163 * as wedged. But we only want to do this when the GPU is angry,
1164 * for all other failure, such as an allocation failure, bail.
1166 if (!intel_gt_is_wedged(&dev_priv
->gt
)) {
1167 i915_probe_error(dev_priv
,
1168 "Failed to initialize GPU, declaring it wedged!\n");
1169 intel_gt_set_wedged(&dev_priv
->gt
);
1172 /* Minimal basic recovery for KMS */
1173 ret
= i915_ggtt_enable_hw(dev_priv
);
1174 i915_ggtt_resume(&dev_priv
->ggtt
);
1175 intel_init_clock_gating(dev_priv
);
1178 i915_gem_drain_freed_objects(dev_priv
);
1182 void i915_gem_driver_register(struct drm_i915_private
*i915
)
1184 i915_gem_driver_register__shrinker(i915
);
1186 intel_engines_driver_register(i915
);
1189 void i915_gem_driver_unregister(struct drm_i915_private
*i915
)
1191 i915_gem_driver_unregister__shrinker(i915
);
1194 void i915_gem_driver_remove(struct drm_i915_private
*dev_priv
)
1196 intel_wakeref_auto_fini(&dev_priv
->ggtt
.userfault_wakeref
);
1198 i915_gem_suspend_late(dev_priv
);
1199 intel_gt_driver_remove(&dev_priv
->gt
);
1200 dev_priv
->uabi_engines
= RB_ROOT
;
1202 /* Flush any outstanding unpin_work. */
1203 i915_gem_drain_workqueue(dev_priv
);
1205 i915_gem_drain_freed_objects(dev_priv
);
1208 void i915_gem_driver_release(struct drm_i915_private
*dev_priv
)
1210 i915_gem_driver_release__contexts(dev_priv
);
1212 intel_gt_driver_release(&dev_priv
->gt
);
1214 intel_wa_list_free(&dev_priv
->gt_wa_list
);
1216 intel_uc_cleanup_firmwares(&dev_priv
->gt
.uc
);
1217 i915_gem_cleanup_userptr(dev_priv
);
1219 i915_gem_drain_freed_objects(dev_priv
);
1221 drm_WARN_ON(&dev_priv
->drm
, !list_empty(&dev_priv
->gem
.contexts
.list
));
1224 static void i915_gem_init__mm(struct drm_i915_private
*i915
)
1226 spin_lock_init(&i915
->mm
.obj_lock
);
1228 init_llist_head(&i915
->mm
.free_list
);
1230 INIT_LIST_HEAD(&i915
->mm
.purge_list
);
1231 INIT_LIST_HEAD(&i915
->mm
.shrink_list
);
1233 i915_gem_init__objects(i915
);
1236 void i915_gem_init_early(struct drm_i915_private
*dev_priv
)
1238 i915_gem_init__mm(dev_priv
);
1239 i915_gem_init__contexts(dev_priv
);
1241 spin_lock_init(&dev_priv
->fb_tracking
.lock
);
1244 void i915_gem_cleanup_early(struct drm_i915_private
*dev_priv
)
1246 i915_gem_drain_freed_objects(dev_priv
);
1247 GEM_BUG_ON(!llist_empty(&dev_priv
->mm
.free_list
));
1248 GEM_BUG_ON(atomic_read(&dev_priv
->mm
.free_count
));
1249 drm_WARN_ON(&dev_priv
->drm
, dev_priv
->mm
.shrink_count
);
1252 int i915_gem_freeze(struct drm_i915_private
*dev_priv
)
1254 /* Discard all purgeable objects, let userspace recover those as
1255 * required after resuming.
1257 i915_gem_shrink_all(dev_priv
);
1262 int i915_gem_freeze_late(struct drm_i915_private
*i915
)
1264 struct drm_i915_gem_object
*obj
;
1265 intel_wakeref_t wakeref
;
1268 * Called just before we write the hibernation image.
1270 * We need to update the domain tracking to reflect that the CPU
1271 * will be accessing all the pages to create and restore from the
1272 * hibernation, and so upon restoration those pages will be in the
1275 * To make sure the hibernation image contains the latest state,
1276 * we update that state just before writing out the image.
1278 * To try and reduce the hibernation image, we manually shrink
1279 * the objects as well, see i915_gem_freeze()
1282 wakeref
= intel_runtime_pm_get(&i915
->runtime_pm
);
1284 i915_gem_shrink(i915
, -1UL, NULL
, ~0);
1285 i915_gem_drain_freed_objects(i915
);
1287 list_for_each_entry(obj
, &i915
->mm
.shrink_list
, mm
.link
) {
1288 i915_gem_object_lock(obj
, NULL
);
1289 drm_WARN_ON(&i915
->drm
,
1290 i915_gem_object_set_to_cpu_domain(obj
, true));
1291 i915_gem_object_unlock(obj
);
1294 intel_runtime_pm_put(&i915
->runtime_pm
, wakeref
);
1299 int i915_gem_open(struct drm_i915_private
*i915
, struct drm_file
*file
)
1301 struct drm_i915_file_private
*file_priv
;
1306 file_priv
= kzalloc(sizeof(*file_priv
), GFP_KERNEL
);
1310 file
->driver_priv
= file_priv
;
1311 file_priv
->dev_priv
= i915
;
1312 file_priv
->file
= file
;
1314 file_priv
->bsd_engine
= -1;
1315 file_priv
->hang_timestamp
= jiffies
;
1317 ret
= i915_gem_context_open(i915
, file
);
1324 void i915_gem_ww_ctx_init(struct i915_gem_ww_ctx
*ww
, bool intr
)
1326 ww_acquire_init(&ww
->ctx
, &reservation_ww_class
);
1327 INIT_LIST_HEAD(&ww
->obj_list
);
1329 ww
->contended
= NULL
;
1332 static void i915_gem_ww_ctx_unlock_all(struct i915_gem_ww_ctx
*ww
)
1334 struct drm_i915_gem_object
*obj
;
1336 while ((obj
= list_first_entry_or_null(&ww
->obj_list
, struct drm_i915_gem_object
, obj_link
))) {
1337 list_del(&obj
->obj_link
);
1338 i915_gem_object_unlock(obj
);
1342 void i915_gem_ww_unlock_single(struct drm_i915_gem_object
*obj
)
1344 list_del(&obj
->obj_link
);
1345 i915_gem_object_unlock(obj
);
1348 void i915_gem_ww_ctx_fini(struct i915_gem_ww_ctx
*ww
)
1350 i915_gem_ww_ctx_unlock_all(ww
);
1351 WARN_ON(ww
->contended
);
1352 ww_acquire_fini(&ww
->ctx
);
1355 int __must_check
i915_gem_ww_ctx_backoff(struct i915_gem_ww_ctx
*ww
)
1359 if (WARN_ON(!ww
->contended
))
1362 i915_gem_ww_ctx_unlock_all(ww
);
1364 ret
= dma_resv_lock_slow_interruptible(ww
->contended
->base
.resv
, &ww
->ctx
);
1366 dma_resv_lock_slow(ww
->contended
->base
.resv
, &ww
->ctx
);
1369 list_add_tail(&ww
->contended
->obj_link
, &ww
->obj_list
);
1371 ww
->contended
= NULL
;
1376 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1377 #include "selftests/mock_gem_device.c"
1378 #include "selftests/i915_gem.c"