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 <drm/i915_drm.h>
30 #include <linux/dma-fence-array.h>
31 #include <linux/kthread.h>
32 #include <linux/dma-resv.h>
33 #include <linux/shmem_fs.h>
34 #include <linux/slab.h>
35 #include <linux/stop_machine.h>
36 #include <linux/swap.h>
37 #include <linux/pci.h>
38 #include <linux/dma-buf.h>
39 #include <linux/mman.h>
41 #include "display/intel_display.h"
42 #include "display/intel_frontbuffer.h"
44 #include "gem/i915_gem_clflush.h"
45 #include "gem/i915_gem_context.h"
46 #include "gem/i915_gem_ioctls.h"
47 #include "gem/i915_gem_mman.h"
48 #include "gem/i915_gem_region.h"
49 #include "gt/intel_engine_user.h"
50 #include "gt/intel_gt.h"
51 #include "gt/intel_gt_pm.h"
52 #include "gt/intel_workarounds.h"
55 #include "i915_trace.h"
56 #include "i915_vgpu.h"
61 insert_mappable_node(struct i915_ggtt
*ggtt
, struct drm_mm_node
*node
, u32 size
)
65 err
= mutex_lock_interruptible(&ggtt
->vm
.mutex
);
69 memset(node
, 0, sizeof(*node
));
70 err
= drm_mm_insert_node_in_range(&ggtt
->vm
.mm
, node
,
71 size
, 0, I915_COLOR_UNEVICTABLE
,
72 0, ggtt
->mappable_end
,
75 mutex_unlock(&ggtt
->vm
.mutex
);
81 remove_mappable_node(struct i915_ggtt
*ggtt
, struct drm_mm_node
*node
)
83 mutex_lock(&ggtt
->vm
.mutex
);
84 drm_mm_remove_node(node
);
85 mutex_unlock(&ggtt
->vm
.mutex
);
89 i915_gem_get_aperture_ioctl(struct drm_device
*dev
, void *data
,
90 struct drm_file
*file
)
92 struct i915_ggtt
*ggtt
= &to_i915(dev
)->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 (!atomic_read(&obj
->bind_count
))
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
))
146 if (!i915_vm_tryopen(vm
))
149 /* Prevent vma being freed by i915_vma_parked as we unbind */
150 vma
= __i915_vma_get(vma
);
151 spin_unlock(&obj
->vma
.lock
);
155 if (flags
& I915_GEM_OBJECT_UNBIND_ACTIVE
||
156 !i915_vma_is_active(vma
))
157 ret
= i915_vma_unbind(vma
);
163 spin_lock(&obj
->vma
.lock
);
165 list_splice_init(&still_in_list
, &obj
->vma
.list
);
166 spin_unlock(&obj
->vma
.lock
);
168 if (ret
== -EAGAIN
&& flags
& I915_GEM_OBJECT_UNBIND_BARRIER
) {
169 rcu_barrier(); /* flush the i915_vm_release() */
173 intel_runtime_pm_put(rpm
, wakeref
);
179 i915_gem_phys_pwrite(struct drm_i915_gem_object
*obj
,
180 struct drm_i915_gem_pwrite
*args
,
181 struct drm_file
*file
)
183 void *vaddr
= obj
->phys_handle
->vaddr
+ args
->offset
;
184 char __user
*user_data
= u64_to_user_ptr(args
->data_ptr
);
187 * We manually control the domain here and pretend that it
188 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
190 i915_gem_object_invalidate_frontbuffer(obj
, ORIGIN_CPU
);
192 if (copy_from_user(vaddr
, user_data
, args
->size
))
195 drm_clflush_virt_range(vaddr
, args
->size
);
196 intel_gt_chipset_flush(&to_i915(obj
->base
.dev
)->gt
);
198 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
203 i915_gem_create(struct drm_file
*file
,
204 struct intel_memory_region
*mr
,
208 struct drm_i915_gem_object
*obj
;
213 GEM_BUG_ON(!is_power_of_2(mr
->min_page_size
));
214 size
= round_up(*size_p
, mr
->min_page_size
);
218 /* For most of the ABI (e.g. mmap) we think in system pages */
219 GEM_BUG_ON(!IS_ALIGNED(size
, PAGE_SIZE
));
221 /* Allocate the new object */
222 obj
= i915_gem_object_create_region(mr
, size
, 0);
226 ret
= drm_gem_handle_create(file
, &obj
->base
, &handle
);
227 /* drop reference from allocate - handle holds it now */
228 i915_gem_object_put(obj
);
238 i915_gem_dumb_create(struct drm_file
*file
,
239 struct drm_device
*dev
,
240 struct drm_mode_create_dumb
*args
)
242 enum intel_memory_type mem_type
;
243 int cpp
= DIV_ROUND_UP(args
->bpp
, 8);
248 format
= DRM_FORMAT_C8
;
251 format
= DRM_FORMAT_RGB565
;
254 format
= DRM_FORMAT_XRGB8888
;
260 /* have to work out size/pitch and return them */
261 args
->pitch
= ALIGN(args
->width
* cpp
, 64);
263 /* align stride to page size so that we can remap */
264 if (args
->pitch
> intel_plane_fb_max_stride(to_i915(dev
), format
,
265 DRM_FORMAT_MOD_LINEAR
))
266 args
->pitch
= ALIGN(args
->pitch
, 4096);
268 args
->size
= args
->pitch
* args
->height
;
270 mem_type
= INTEL_MEMORY_SYSTEM
;
271 if (HAS_LMEM(to_i915(dev
)))
272 mem_type
= INTEL_MEMORY_LOCAL
;
274 return i915_gem_create(file
,
275 intel_memory_region_by_type(to_i915(dev
),
277 &args
->size
, &args
->handle
);
281 * Creates a new mm object and returns a handle to it.
282 * @dev: drm device pointer
283 * @data: ioctl data blob
284 * @file: drm file pointer
287 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
288 struct drm_file
*file
)
290 struct drm_i915_private
*i915
= to_i915(dev
);
291 struct drm_i915_gem_create
*args
= data
;
293 i915_gem_flush_free_objects(i915
);
295 return i915_gem_create(file
,
296 intel_memory_region_by_type(i915
,
297 INTEL_MEMORY_SYSTEM
),
298 &args
->size
, &args
->handle
);
302 shmem_pread(struct page
*page
, int offset
, int len
, char __user
*user_data
,
311 drm_clflush_virt_range(vaddr
+ offset
, len
);
313 ret
= __copy_to_user(user_data
, vaddr
+ offset
, len
);
317 return ret
? -EFAULT
: 0;
321 i915_gem_shmem_pread(struct drm_i915_gem_object
*obj
,
322 struct drm_i915_gem_pread
*args
)
324 unsigned int needs_clflush
;
325 unsigned int idx
, offset
;
326 struct dma_fence
*fence
;
327 char __user
*user_data
;
331 ret
= i915_gem_object_prepare_read(obj
, &needs_clflush
);
335 fence
= i915_gem_object_lock_fence(obj
);
336 i915_gem_object_finish_access(obj
);
341 user_data
= u64_to_user_ptr(args
->data_ptr
);
342 offset
= offset_in_page(args
->offset
);
343 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
344 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
345 unsigned int length
= min_t(u64
, remain
, PAGE_SIZE
- offset
);
347 ret
= shmem_pread(page
, offset
, length
, user_data
,
357 i915_gem_object_unlock_fence(obj
, fence
);
362 gtt_user_read(struct io_mapping
*mapping
,
363 loff_t base
, int offset
,
364 char __user
*user_data
, int length
)
367 unsigned long unwritten
;
369 /* We can use the cpu mem copy function because this is X86. */
370 vaddr
= io_mapping_map_atomic_wc(mapping
, base
);
371 unwritten
= __copy_to_user_inatomic(user_data
,
372 (void __force
*)vaddr
+ offset
,
374 io_mapping_unmap_atomic(vaddr
);
376 vaddr
= io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
377 unwritten
= copy_to_user(user_data
,
378 (void __force
*)vaddr
+ offset
,
380 io_mapping_unmap(vaddr
);
386 i915_gem_gtt_pread(struct drm_i915_gem_object
*obj
,
387 const struct drm_i915_gem_pread
*args
)
389 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
390 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
391 intel_wakeref_t wakeref
;
392 struct drm_mm_node node
;
393 struct dma_fence
*fence
;
394 void __user
*user_data
;
395 struct i915_vma
*vma
;
399 wakeref
= intel_runtime_pm_get(&i915
->runtime_pm
);
400 vma
= ERR_PTR(-ENODEV
);
401 if (!i915_gem_object_is_tiled(obj
))
402 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
404 PIN_NONBLOCK
/* NOWARN */ |
407 node
.start
= i915_ggtt_offset(vma
);
410 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
413 GEM_BUG_ON(!drm_mm_node_allocated(&node
));
416 ret
= i915_gem_object_lock_interruptible(obj
);
420 ret
= i915_gem_object_set_to_gtt_domain(obj
, false);
422 i915_gem_object_unlock(obj
);
426 fence
= i915_gem_object_lock_fence(obj
);
427 i915_gem_object_unlock(obj
);
433 user_data
= u64_to_user_ptr(args
->data_ptr
);
435 offset
= args
->offset
;
438 /* Operation in this page
440 * page_base = page offset within aperture
441 * page_offset = offset within page
442 * page_length = bytes to copy for this page
444 u32 page_base
= node
.start
;
445 unsigned page_offset
= offset_in_page(offset
);
446 unsigned page_length
= PAGE_SIZE
- page_offset
;
447 page_length
= remain
< page_length
? remain
: page_length
;
448 if (drm_mm_node_allocated(&node
)) {
449 ggtt
->vm
.insert_page(&ggtt
->vm
,
450 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
451 node
.start
, I915_CACHE_NONE
, 0);
453 page_base
+= offset
& PAGE_MASK
;
456 if (gtt_user_read(&ggtt
->iomap
, page_base
, page_offset
,
457 user_data
, page_length
)) {
462 remain
-= page_length
;
463 user_data
+= page_length
;
464 offset
+= page_length
;
467 i915_gem_object_unlock_fence(obj
, fence
);
469 if (drm_mm_node_allocated(&node
)) {
470 ggtt
->vm
.clear_range(&ggtt
->vm
, node
.start
, node
.size
);
471 remove_mappable_node(ggtt
, &node
);
476 intel_runtime_pm_put(&i915
->runtime_pm
, wakeref
);
481 * Reads data from the object referenced by handle.
482 * @dev: drm device pointer
483 * @data: ioctl data blob
484 * @file: drm file pointer
486 * On error, the contents of *data are undefined.
489 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
490 struct drm_file
*file
)
492 struct drm_i915_gem_pread
*args
= data
;
493 struct drm_i915_gem_object
*obj
;
499 if (!access_ok(u64_to_user_ptr(args
->data_ptr
),
503 obj
= i915_gem_object_lookup(file
, args
->handle
);
507 /* Bounds check source. */
508 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
513 trace_i915_gem_object_pread(obj
, args
->offset
, args
->size
);
515 ret
= i915_gem_object_wait(obj
,
516 I915_WAIT_INTERRUPTIBLE
,
517 MAX_SCHEDULE_TIMEOUT
);
521 ret
= i915_gem_object_pin_pages(obj
);
525 ret
= i915_gem_shmem_pread(obj
, args
);
526 if (ret
== -EFAULT
|| ret
== -ENODEV
)
527 ret
= i915_gem_gtt_pread(obj
, args
);
529 i915_gem_object_unpin_pages(obj
);
531 i915_gem_object_put(obj
);
535 /* This is the fast write path which cannot handle
536 * page faults in the source data
540 ggtt_write(struct io_mapping
*mapping
,
541 loff_t base
, int offset
,
542 char __user
*user_data
, int length
)
545 unsigned long unwritten
;
547 /* We can use the cpu mem copy function because this is X86. */
548 vaddr
= io_mapping_map_atomic_wc(mapping
, base
);
549 unwritten
= __copy_from_user_inatomic_nocache((void __force
*)vaddr
+ offset
,
551 io_mapping_unmap_atomic(vaddr
);
553 vaddr
= io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
554 unwritten
= copy_from_user((void __force
*)vaddr
+ offset
,
556 io_mapping_unmap(vaddr
);
563 * This is the fast pwrite path, where we copy the data directly from the
564 * user into the GTT, uncached.
565 * @obj: i915 GEM object
566 * @args: pwrite arguments structure
569 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object
*obj
,
570 const struct drm_i915_gem_pwrite
*args
)
572 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
573 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
574 struct intel_runtime_pm
*rpm
= &i915
->runtime_pm
;
575 intel_wakeref_t wakeref
;
576 struct drm_mm_node node
;
577 struct dma_fence
*fence
;
578 struct i915_vma
*vma
;
580 void __user
*user_data
;
583 if (i915_gem_object_has_struct_page(obj
)) {
585 * Avoid waking the device up if we can fallback, as
586 * waking/resuming is very slow (worst-case 10-100 ms
587 * depending on PCI sleeps and our own resume time).
588 * This easily dwarfs any performance advantage from
589 * using the cache bypass of indirect GGTT access.
591 wakeref
= intel_runtime_pm_get_if_in_use(rpm
);
595 /* No backing pages, no fallback, we must force GGTT access */
596 wakeref
= intel_runtime_pm_get(rpm
);
599 vma
= ERR_PTR(-ENODEV
);
600 if (!i915_gem_object_is_tiled(obj
))
601 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
603 PIN_NONBLOCK
/* NOWARN */ |
606 node
.start
= i915_ggtt_offset(vma
);
609 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
612 GEM_BUG_ON(!drm_mm_node_allocated(&node
));
615 ret
= i915_gem_object_lock_interruptible(obj
);
619 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
621 i915_gem_object_unlock(obj
);
625 fence
= i915_gem_object_lock_fence(obj
);
626 i915_gem_object_unlock(obj
);
632 i915_gem_object_invalidate_frontbuffer(obj
, ORIGIN_CPU
);
634 user_data
= u64_to_user_ptr(args
->data_ptr
);
635 offset
= args
->offset
;
638 /* Operation in this page
640 * page_base = page offset within aperture
641 * page_offset = offset within page
642 * page_length = bytes to copy for this page
644 u32 page_base
= node
.start
;
645 unsigned int page_offset
= offset_in_page(offset
);
646 unsigned int page_length
= PAGE_SIZE
- page_offset
;
647 page_length
= remain
< page_length
? remain
: page_length
;
648 if (drm_mm_node_allocated(&node
)) {
649 /* flush the write before we modify the GGTT */
650 intel_gt_flush_ggtt_writes(ggtt
->vm
.gt
);
651 ggtt
->vm
.insert_page(&ggtt
->vm
,
652 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
653 node
.start
, I915_CACHE_NONE
, 0);
654 wmb(); /* flush modifications to the GGTT (insert_page) */
656 page_base
+= offset
& PAGE_MASK
;
658 /* If we get a fault while copying data, then (presumably) our
659 * source page isn't available. Return the error and we'll
660 * retry in the slow path.
661 * If the object is non-shmem backed, we retry again with the
662 * path that handles page fault.
664 if (ggtt_write(&ggtt
->iomap
, page_base
, page_offset
,
665 user_data
, page_length
)) {
670 remain
-= page_length
;
671 user_data
+= page_length
;
672 offset
+= page_length
;
675 intel_gt_flush_ggtt_writes(ggtt
->vm
.gt
);
676 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
678 i915_gem_object_unlock_fence(obj
, fence
);
680 if (drm_mm_node_allocated(&node
)) {
681 ggtt
->vm
.clear_range(&ggtt
->vm
, node
.start
, node
.size
);
682 remove_mappable_node(ggtt
, &node
);
687 intel_runtime_pm_put(rpm
, wakeref
);
691 /* Per-page copy function for the shmem pwrite fastpath.
692 * Flushes invalid cachelines before writing to the target if
693 * needs_clflush_before is set and flushes out any written cachelines after
694 * writing if needs_clflush is set.
697 shmem_pwrite(struct page
*page
, int offset
, int len
, char __user
*user_data
,
698 bool needs_clflush_before
,
699 bool needs_clflush_after
)
706 if (needs_clflush_before
)
707 drm_clflush_virt_range(vaddr
+ offset
, len
);
709 ret
= __copy_from_user(vaddr
+ offset
, user_data
, len
);
710 if (!ret
&& needs_clflush_after
)
711 drm_clflush_virt_range(vaddr
+ offset
, len
);
715 return ret
? -EFAULT
: 0;
719 i915_gem_shmem_pwrite(struct drm_i915_gem_object
*obj
,
720 const struct drm_i915_gem_pwrite
*args
)
722 unsigned int partial_cacheline_write
;
723 unsigned int needs_clflush
;
724 unsigned int offset
, idx
;
725 struct dma_fence
*fence
;
726 void __user
*user_data
;
730 ret
= i915_gem_object_prepare_write(obj
, &needs_clflush
);
734 fence
= i915_gem_object_lock_fence(obj
);
735 i915_gem_object_finish_access(obj
);
739 /* If we don't overwrite a cacheline completely we need to be
740 * careful to have up-to-date data by first clflushing. Don't
741 * overcomplicate things and flush the entire patch.
743 partial_cacheline_write
= 0;
744 if (needs_clflush
& CLFLUSH_BEFORE
)
745 partial_cacheline_write
= boot_cpu_data
.x86_clflush_size
- 1;
747 user_data
= u64_to_user_ptr(args
->data_ptr
);
749 offset
= offset_in_page(args
->offset
);
750 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
751 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
752 unsigned int length
= min_t(u64
, remain
, PAGE_SIZE
- offset
);
754 ret
= shmem_pwrite(page
, offset
, length
, user_data
,
755 (offset
| length
) & partial_cacheline_write
,
756 needs_clflush
& CLFLUSH_AFTER
);
765 i915_gem_object_flush_frontbuffer(obj
, ORIGIN_CPU
);
766 i915_gem_object_unlock_fence(obj
, fence
);
772 * Writes data to the object referenced by handle.
774 * @data: ioctl data blob
777 * On error, the contents of the buffer that were to be modified are undefined.
780 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
781 struct drm_file
*file
)
783 struct drm_i915_gem_pwrite
*args
= data
;
784 struct drm_i915_gem_object
*obj
;
790 if (!access_ok(u64_to_user_ptr(args
->data_ptr
), args
->size
))
793 obj
= i915_gem_object_lookup(file
, args
->handle
);
797 /* Bounds check destination. */
798 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
803 /* Writes not allowed into this read-only object */
804 if (i915_gem_object_is_readonly(obj
)) {
809 trace_i915_gem_object_pwrite(obj
, args
->offset
, args
->size
);
812 if (obj
->ops
->pwrite
)
813 ret
= obj
->ops
->pwrite(obj
, args
);
817 ret
= i915_gem_object_wait(obj
,
818 I915_WAIT_INTERRUPTIBLE
|
820 MAX_SCHEDULE_TIMEOUT
);
824 ret
= i915_gem_object_pin_pages(obj
);
829 /* We can only do the GTT pwrite on untiled buffers, as otherwise
830 * it would end up going through the fenced access, and we'll get
831 * different detiling behavior between reading and writing.
832 * pread/pwrite currently are reading and writing from the CPU
833 * perspective, requiring manual detiling by the client.
835 if (!i915_gem_object_has_struct_page(obj
) ||
836 cpu_write_needs_clflush(obj
))
837 /* Note that the gtt paths might fail with non-page-backed user
838 * pointers (e.g. gtt mappings when moving data between
839 * textures). Fallback to the shmem path in that case.
841 ret
= i915_gem_gtt_pwrite_fast(obj
, args
);
843 if (ret
== -EFAULT
|| ret
== -ENOSPC
) {
844 if (obj
->phys_handle
)
845 ret
= i915_gem_phys_pwrite(obj
, args
, file
);
847 ret
= i915_gem_shmem_pwrite(obj
, args
);
850 i915_gem_object_unpin_pages(obj
);
852 i915_gem_object_put(obj
);
857 * Called when user space has done writes to this buffer
859 * @data: ioctl data blob
863 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
864 struct drm_file
*file
)
866 struct drm_i915_gem_sw_finish
*args
= data
;
867 struct drm_i915_gem_object
*obj
;
869 obj
= i915_gem_object_lookup(file
, args
->handle
);
874 * Proxy objects are barred from CPU access, so there is no
875 * need to ban sw_finish as it is a nop.
878 /* Pinned buffers may be scanout, so flush the cache */
879 i915_gem_object_flush_if_display(obj
);
880 i915_gem_object_put(obj
);
885 void i915_gem_runtime_suspend(struct drm_i915_private
*i915
)
887 struct drm_i915_gem_object
*obj
, *on
;
891 * Only called during RPM suspend. All users of the userfault_list
892 * must be holding an RPM wakeref to ensure that this can not
893 * run concurrently with themselves (and use the struct_mutex for
894 * protection between themselves).
897 list_for_each_entry_safe(obj
, on
,
898 &i915
->ggtt
.userfault_list
, userfault_link
)
899 __i915_gem_object_release_mmap_gtt(obj
);
902 * The fence will be lost when the device powers down. If any were
903 * in use by hardware (i.e. they are pinned), we should not be powering
904 * down! All other fences will be reacquired by the user upon waking.
906 for (i
= 0; i
< i915
->ggtt
.num_fences
; i
++) {
907 struct i915_fence_reg
*reg
= &i915
->ggtt
.fence_regs
[i
];
910 * Ideally we want to assert that the fence register is not
911 * live at this point (i.e. that no piece of code will be
912 * trying to write through fence + GTT, as that both violates
913 * our tracking of activity and associated locking/barriers,
914 * but also is illegal given that the hw is powered down).
916 * Previously we used reg->pin_count as a "liveness" indicator.
917 * That is not sufficient, and we need a more fine-grained
918 * tool if we want to have a sanity check here.
924 GEM_BUG_ON(i915_vma_has_userfault(reg
->vma
));
930 i915_gem_object_ggtt_pin(struct drm_i915_gem_object
*obj
,
931 const struct i915_ggtt_view
*view
,
936 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
937 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
938 struct i915_vma
*vma
;
941 if (i915_gem_object_never_bind_ggtt(obj
))
942 return ERR_PTR(-ENODEV
);
944 if (flags
& PIN_MAPPABLE
&&
945 (!view
|| view
->type
== I915_GGTT_VIEW_NORMAL
)) {
947 * If the required space is larger than the available
948 * aperture, we will not able to find a slot for the
949 * object and unbinding the object now will be in
950 * vain. Worse, doing so may cause us to ping-pong
951 * the object in and out of the Global GTT and
952 * waste a lot of cycles under the mutex.
954 if (obj
->base
.size
> ggtt
->mappable_end
)
955 return ERR_PTR(-E2BIG
);
958 * If NONBLOCK is set the caller is optimistically
959 * trying to cache the full object within the mappable
960 * aperture, and *must* have a fallback in place for
961 * situations where we cannot bind the object. We
962 * can be a little more lax here and use the fallback
963 * more often to avoid costly migrations of ourselves
964 * and other objects within the aperture.
966 * Half-the-aperture is used as a simple heuristic.
967 * More interesting would to do search for a free
968 * block prior to making the commitment to unbind.
969 * That caters for the self-harm case, and with a
970 * little more heuristics (e.g. NOFAULT, NOEVICT)
971 * we could try to minimise harm to others.
973 if (flags
& PIN_NONBLOCK
&&
974 obj
->base
.size
> ggtt
->mappable_end
/ 2)
975 return ERR_PTR(-ENOSPC
);
978 vma
= i915_vma_instance(obj
, &ggtt
->vm
, view
);
982 if (i915_vma_misplaced(vma
, size
, alignment
, flags
)) {
983 if (flags
& PIN_NONBLOCK
) {
984 if (i915_vma_is_pinned(vma
) || i915_vma_is_active(vma
))
985 return ERR_PTR(-ENOSPC
);
987 if (flags
& PIN_MAPPABLE
&&
988 vma
->fence_size
> ggtt
->mappable_end
/ 2)
989 return ERR_PTR(-ENOSPC
);
992 ret
= i915_vma_unbind(vma
);
997 if (vma
->fence
&& !i915_gem_object_is_tiled(obj
)) {
998 mutex_lock(&ggtt
->vm
.mutex
);
999 ret
= i915_vma_revoke_fence(vma
);
1000 mutex_unlock(&ggtt
->vm
.mutex
);
1002 return ERR_PTR(ret
);
1005 ret
= i915_vma_pin(vma
, size
, alignment
, flags
| PIN_GLOBAL
);
1007 return ERR_PTR(ret
);
1013 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
1014 struct drm_file
*file_priv
)
1016 struct drm_i915_private
*i915
= to_i915(dev
);
1017 struct drm_i915_gem_madvise
*args
= data
;
1018 struct drm_i915_gem_object
*obj
;
1021 switch (args
->madv
) {
1022 case I915_MADV_DONTNEED
:
1023 case I915_MADV_WILLNEED
:
1029 obj
= i915_gem_object_lookup(file_priv
, args
->handle
);
1033 err
= mutex_lock_interruptible(&obj
->mm
.lock
);
1037 if (i915_gem_object_has_pages(obj
) &&
1038 i915_gem_object_is_tiled(obj
) &&
1039 i915
->quirks
& QUIRK_PIN_SWIZZLED_PAGES
) {
1040 if (obj
->mm
.madv
== I915_MADV_WILLNEED
) {
1041 GEM_BUG_ON(!obj
->mm
.quirked
);
1042 __i915_gem_object_unpin_pages(obj
);
1043 obj
->mm
.quirked
= false;
1045 if (args
->madv
== I915_MADV_WILLNEED
) {
1046 GEM_BUG_ON(obj
->mm
.quirked
);
1047 __i915_gem_object_pin_pages(obj
);
1048 obj
->mm
.quirked
= true;
1052 if (obj
->mm
.madv
!= __I915_MADV_PURGED
)
1053 obj
->mm
.madv
= args
->madv
;
1055 if (i915_gem_object_has_pages(obj
)) {
1056 struct list_head
*list
;
1058 if (i915_gem_object_is_shrinkable(obj
)) {
1059 unsigned long flags
;
1061 spin_lock_irqsave(&i915
->mm
.obj_lock
, flags
);
1063 if (obj
->mm
.madv
!= I915_MADV_WILLNEED
)
1064 list
= &i915
->mm
.purge_list
;
1066 list
= &i915
->mm
.shrink_list
;
1067 list_move_tail(&obj
->mm
.link
, list
);
1069 spin_unlock_irqrestore(&i915
->mm
.obj_lock
, flags
);
1073 /* if the object is no longer attached, discard its backing storage */
1074 if (obj
->mm
.madv
== I915_MADV_DONTNEED
&&
1075 !i915_gem_object_has_pages(obj
))
1076 i915_gem_object_truncate(obj
);
1078 args
->retained
= obj
->mm
.madv
!= __I915_MADV_PURGED
;
1079 mutex_unlock(&obj
->mm
.lock
);
1082 i915_gem_object_put(obj
);
1086 int i915_gem_init(struct drm_i915_private
*dev_priv
)
1090 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1091 if (intel_vgpu_active(dev_priv
) && !intel_vgpu_has_huge_gtt(dev_priv
))
1092 mkwrite_device_info(dev_priv
)->page_sizes
=
1093 I915_GTT_PAGE_SIZE_4K
;
1095 ret
= i915_gem_init_userptr(dev_priv
);
1099 intel_uc_fetch_firmwares(&dev_priv
->gt
.uc
);
1100 intel_wopcm_init(&dev_priv
->wopcm
);
1102 ret
= i915_init_ggtt(dev_priv
);
1104 GEM_BUG_ON(ret
== -EIO
);
1109 * Despite its name intel_init_clock_gating applies both display
1110 * clock gating workarounds; GT mmio workarounds and the occasional
1111 * GT power context workaround. Worse, sometimes it includes a context
1112 * register workaround which we need to apply before we record the
1113 * default HW state for all contexts.
1115 * FIXME: break up the workarounds and apply them at the right time!
1117 intel_init_clock_gating(dev_priv
);
1119 ret
= intel_gt_init(&dev_priv
->gt
);
1126 * Unwinding is complicated by that we want to handle -EIO to mean
1127 * disable GPU submission but keep KMS alive. We want to mark the
1128 * HW as irrevisibly wedged, but keep enough state around that the
1129 * driver doesn't explode during runtime.
1132 i915_gem_drain_workqueue(dev_priv
);
1135 intel_uc_cleanup_firmwares(&dev_priv
->gt
.uc
);
1136 i915_gem_cleanup_userptr(dev_priv
);
1141 * Allow engines or uC initialisation to fail by marking the GPU
1142 * as wedged. But we only want to do this when the GPU is angry,
1143 * for all other failure, such as an allocation failure, bail.
1145 if (!intel_gt_is_wedged(&dev_priv
->gt
)) {
1146 i915_probe_error(dev_priv
,
1147 "Failed to initialize GPU, declaring it wedged!\n");
1148 intel_gt_set_wedged(&dev_priv
->gt
);
1151 /* Minimal basic recovery for KMS */
1152 ret
= i915_ggtt_enable_hw(dev_priv
);
1153 i915_gem_restore_gtt_mappings(dev_priv
);
1154 i915_gem_restore_fences(&dev_priv
->ggtt
);
1155 intel_init_clock_gating(dev_priv
);
1158 i915_gem_drain_freed_objects(dev_priv
);
1162 void i915_gem_driver_register(struct drm_i915_private
*i915
)
1164 i915_gem_driver_register__shrinker(i915
);
1166 intel_engines_driver_register(i915
);
1169 void i915_gem_driver_unregister(struct drm_i915_private
*i915
)
1171 i915_gem_driver_unregister__shrinker(i915
);
1174 void i915_gem_driver_remove(struct drm_i915_private
*dev_priv
)
1176 intel_wakeref_auto_fini(&dev_priv
->ggtt
.userfault_wakeref
);
1178 i915_gem_suspend_late(dev_priv
);
1179 intel_gt_driver_remove(&dev_priv
->gt
);
1180 dev_priv
->uabi_engines
= RB_ROOT
;
1182 /* Flush any outstanding unpin_work. */
1183 i915_gem_drain_workqueue(dev_priv
);
1185 i915_gem_drain_freed_objects(dev_priv
);
1188 void i915_gem_driver_release(struct drm_i915_private
*dev_priv
)
1190 i915_gem_driver_release__contexts(dev_priv
);
1192 intel_gt_driver_release(&dev_priv
->gt
);
1194 intel_wa_list_free(&dev_priv
->gt_wa_list
);
1196 intel_uc_cleanup_firmwares(&dev_priv
->gt
.uc
);
1197 i915_gem_cleanup_userptr(dev_priv
);
1199 i915_gem_drain_freed_objects(dev_priv
);
1201 WARN_ON(!list_empty(&dev_priv
->gem
.contexts
.list
));
1204 static void i915_gem_init__mm(struct drm_i915_private
*i915
)
1206 spin_lock_init(&i915
->mm
.obj_lock
);
1208 init_llist_head(&i915
->mm
.free_list
);
1210 INIT_LIST_HEAD(&i915
->mm
.purge_list
);
1211 INIT_LIST_HEAD(&i915
->mm
.shrink_list
);
1213 i915_gem_init__objects(i915
);
1216 void i915_gem_init_early(struct drm_i915_private
*dev_priv
)
1218 i915_gem_init__mm(dev_priv
);
1219 i915_gem_init__contexts(dev_priv
);
1221 spin_lock_init(&dev_priv
->fb_tracking
.lock
);
1224 void i915_gem_cleanup_early(struct drm_i915_private
*dev_priv
)
1226 i915_gem_drain_freed_objects(dev_priv
);
1227 GEM_BUG_ON(!llist_empty(&dev_priv
->mm
.free_list
));
1228 GEM_BUG_ON(atomic_read(&dev_priv
->mm
.free_count
));
1229 WARN_ON(dev_priv
->mm
.shrink_count
);
1232 int i915_gem_freeze(struct drm_i915_private
*dev_priv
)
1234 /* Discard all purgeable objects, let userspace recover those as
1235 * required after resuming.
1237 i915_gem_shrink_all(dev_priv
);
1242 int i915_gem_freeze_late(struct drm_i915_private
*i915
)
1244 struct drm_i915_gem_object
*obj
;
1245 intel_wakeref_t wakeref
;
1248 * Called just before we write the hibernation image.
1250 * We need to update the domain tracking to reflect that the CPU
1251 * will be accessing all the pages to create and restore from the
1252 * hibernation, and so upon restoration those pages will be in the
1255 * To make sure the hibernation image contains the latest state,
1256 * we update that state just before writing out the image.
1258 * To try and reduce the hibernation image, we manually shrink
1259 * the objects as well, see i915_gem_freeze()
1262 wakeref
= intel_runtime_pm_get(&i915
->runtime_pm
);
1264 i915_gem_shrink(i915
, -1UL, NULL
, ~0);
1265 i915_gem_drain_freed_objects(i915
);
1267 list_for_each_entry(obj
, &i915
->mm
.shrink_list
, mm
.link
) {
1268 i915_gem_object_lock(obj
);
1269 WARN_ON(i915_gem_object_set_to_cpu_domain(obj
, true));
1270 i915_gem_object_unlock(obj
);
1273 intel_runtime_pm_put(&i915
->runtime_pm
, wakeref
);
1278 void i915_gem_release(struct drm_device
*dev
, struct drm_file
*file
)
1280 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
1281 struct i915_request
*request
;
1283 /* Clean up our request list when the client is going away, so that
1284 * later retire_requests won't dereference our soon-to-be-gone
1287 spin_lock(&file_priv
->mm
.lock
);
1288 list_for_each_entry(request
, &file_priv
->mm
.request_list
, client_link
)
1289 request
->file_priv
= NULL
;
1290 spin_unlock(&file_priv
->mm
.lock
);
1293 int i915_gem_open(struct drm_i915_private
*i915
, struct drm_file
*file
)
1295 struct drm_i915_file_private
*file_priv
;
1300 file_priv
= kzalloc(sizeof(*file_priv
), GFP_KERNEL
);
1304 file
->driver_priv
= file_priv
;
1305 file_priv
->dev_priv
= i915
;
1306 file_priv
->file
= file
;
1308 spin_lock_init(&file_priv
->mm
.lock
);
1309 INIT_LIST_HEAD(&file_priv
->mm
.request_list
);
1311 file_priv
->bsd_engine
= -1;
1312 file_priv
->hang_timestamp
= jiffies
;
1314 ret
= i915_gem_context_open(i915
, file
);
1321 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1322 #include "selftests/mock_gem_device.c"
1323 #include "selftests/i915_gem.c"