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 <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_pm.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_gt_requests.h"
52 #include "gt/intel_mocs.h"
53 #include "gt/intel_reset.h"
54 #include "gt/intel_renderstate.h"
55 #include "gt/intel_rps.h"
56 #include "gt/intel_workarounds.h"
59 #include "i915_scatterlist.h"
60 #include "i915_trace.h"
61 #include "i915_vgpu.h"
66 insert_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node, u32 size)
70 err = mutex_lock_interruptible(&ggtt->vm.mutex);
74 memset(node, 0, sizeof(*node));
75 err = drm_mm_insert_node_in_range(&ggtt->vm.mm, node,
76 size, 0, I915_COLOR_UNEVICTABLE,
77 0, ggtt->mappable_end,
80 mutex_unlock(&ggtt->vm.mutex);
86 remove_mappable_node(struct i915_ggtt *ggtt, struct drm_mm_node *node)
88 mutex_lock(&ggtt->vm.mutex);
89 drm_mm_remove_node(node);
90 mutex_unlock(&ggtt->vm.mutex);
94 i915_gem_get_aperture_ioctl(struct drm_device *dev, void *data,
95 struct drm_file *file)
97 struct i915_ggtt *ggtt = &to_i915(dev)->ggtt;
98 struct drm_i915_gem_get_aperture *args = data;
102 if (mutex_lock_interruptible(&ggtt->vm.mutex))
105 pinned = ggtt->vm.reserved;
106 list_for_each_entry(vma, &ggtt->vm.bound_list, vm_link)
107 if (i915_vma_is_pinned(vma))
108 pinned += vma->node.size;
110 mutex_unlock(&ggtt->vm.mutex);
112 args->aper_size = ggtt->vm.total;
113 args->aper_available_size = args->aper_size - pinned;
118 int i915_gem_object_unbind(struct drm_i915_gem_object *obj,
121 struct i915_vma *vma;
122 LIST_HEAD(still_in_list);
125 spin_lock(&obj->vma.lock);
126 while (!ret && (vma = list_first_entry_or_null(&obj->vma.list,
129 struct i915_address_space *vm = vma->vm;
132 if (!i915_vm_tryopen(vm))
135 list_move_tail(&vma->obj_link, &still_in_list);
136 spin_unlock(&obj->vma.lock);
138 if (flags & I915_GEM_OBJECT_UNBIND_ACTIVE ||
139 !i915_vma_is_active(vma))
140 ret = i915_vma_unbind(vma);
143 spin_lock(&obj->vma.lock);
145 list_splice(&still_in_list, &obj->vma.list);
146 spin_unlock(&obj->vma.lock);
152 i915_gem_phys_pwrite(struct drm_i915_gem_object *obj,
153 struct drm_i915_gem_pwrite *args,
154 struct drm_file *file)
156 void *vaddr = obj->phys_handle->vaddr + args->offset;
157 char __user *user_data = u64_to_user_ptr(args->data_ptr);
160 * We manually control the domain here and pretend that it
161 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
163 intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);
165 if (copy_from_user(vaddr, user_data, args->size))
168 drm_clflush_virt_range(vaddr, args->size);
169 intel_gt_chipset_flush(&to_i915(obj->base.dev)->gt);
171 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
176 i915_gem_create(struct drm_file *file,
177 struct drm_i915_private *dev_priv,
181 struct drm_i915_gem_object *obj;
186 size = round_up(*size_p, PAGE_SIZE);
190 /* Allocate the new object */
191 obj = i915_gem_object_create_shmem(dev_priv, size);
195 ret = drm_gem_handle_create(file, &obj->base, &handle);
196 /* drop reference from allocate - handle holds it now */
197 i915_gem_object_put(obj);
207 i915_gem_dumb_create(struct drm_file *file,
208 struct drm_device *dev,
209 struct drm_mode_create_dumb *args)
211 int cpp = DIV_ROUND_UP(args->bpp, 8);
216 format = DRM_FORMAT_C8;
219 format = DRM_FORMAT_RGB565;
222 format = DRM_FORMAT_XRGB8888;
228 /* have to work out size/pitch and return them */
229 args->pitch = ALIGN(args->width * cpp, 64);
231 /* align stride to page size so that we can remap */
232 if (args->pitch > intel_plane_fb_max_stride(to_i915(dev), format,
233 DRM_FORMAT_MOD_LINEAR))
234 args->pitch = ALIGN(args->pitch, 4096);
236 args->size = args->pitch * args->height;
237 return i915_gem_create(file, to_i915(dev),
238 &args->size, &args->handle);
242 * Creates a new mm object and returns a handle to it.
243 * @dev: drm device pointer
244 * @data: ioctl data blob
245 * @file: drm file pointer
248 i915_gem_create_ioctl(struct drm_device *dev, void *data,
249 struct drm_file *file)
251 struct drm_i915_private *dev_priv = to_i915(dev);
252 struct drm_i915_gem_create *args = data;
254 i915_gem_flush_free_objects(dev_priv);
256 return i915_gem_create(file, dev_priv,
257 &args->size, &args->handle);
261 shmem_pread(struct page *page, int offset, int len, char __user *user_data,
270 drm_clflush_virt_range(vaddr + offset, len);
272 ret = __copy_to_user(user_data, vaddr + offset, len);
276 return ret ? -EFAULT : 0;
280 i915_gem_shmem_pread(struct drm_i915_gem_object *obj,
281 struct drm_i915_gem_pread *args)
283 unsigned int needs_clflush;
284 unsigned int idx, offset;
285 struct dma_fence *fence;
286 char __user *user_data;
290 ret = i915_gem_object_prepare_read(obj, &needs_clflush);
294 fence = i915_gem_object_lock_fence(obj);
295 i915_gem_object_finish_access(obj);
300 user_data = u64_to_user_ptr(args->data_ptr);
301 offset = offset_in_page(args->offset);
302 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
303 struct page *page = i915_gem_object_get_page(obj, idx);
304 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
306 ret = shmem_pread(page, offset, length, user_data,
316 i915_gem_object_unlock_fence(obj, fence);
321 gtt_user_read(struct io_mapping *mapping,
322 loff_t base, int offset,
323 char __user *user_data, int length)
326 unsigned long unwritten;
328 /* We can use the cpu mem copy function because this is X86. */
329 vaddr = io_mapping_map_atomic_wc(mapping, base);
330 unwritten = __copy_to_user_inatomic(user_data,
331 (void __force *)vaddr + offset,
333 io_mapping_unmap_atomic(vaddr);
335 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
336 unwritten = copy_to_user(user_data,
337 (void __force *)vaddr + offset,
339 io_mapping_unmap(vaddr);
345 i915_gem_gtt_pread(struct drm_i915_gem_object *obj,
346 const struct drm_i915_gem_pread *args)
348 struct drm_i915_private *i915 = to_i915(obj->base.dev);
349 struct i915_ggtt *ggtt = &i915->ggtt;
350 intel_wakeref_t wakeref;
351 struct drm_mm_node node;
352 struct dma_fence *fence;
353 void __user *user_data;
354 struct i915_vma *vma;
358 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
359 vma = ERR_PTR(-ENODEV);
360 if (!i915_gem_object_is_tiled(obj))
361 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
363 PIN_NONBLOCK /* NOWARN */ |
366 node.start = i915_ggtt_offset(vma);
369 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
372 GEM_BUG_ON(!drm_mm_node_allocated(&node));
375 ret = i915_gem_object_lock_interruptible(obj);
379 ret = i915_gem_object_set_to_gtt_domain(obj, false);
381 i915_gem_object_unlock(obj);
385 fence = i915_gem_object_lock_fence(obj);
386 i915_gem_object_unlock(obj);
392 user_data = u64_to_user_ptr(args->data_ptr);
394 offset = args->offset;
397 /* Operation in this page
399 * page_base = page offset within aperture
400 * page_offset = offset within page
401 * page_length = bytes to copy for this page
403 u32 page_base = node.start;
404 unsigned page_offset = offset_in_page(offset);
405 unsigned page_length = PAGE_SIZE - page_offset;
406 page_length = remain < page_length ? remain : page_length;
407 if (drm_mm_node_allocated(&node)) {
408 ggtt->vm.insert_page(&ggtt->vm,
409 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
410 node.start, I915_CACHE_NONE, 0);
412 page_base += offset & PAGE_MASK;
415 if (gtt_user_read(&ggtt->iomap, page_base, page_offset,
416 user_data, page_length)) {
421 remain -= page_length;
422 user_data += page_length;
423 offset += page_length;
426 i915_gem_object_unlock_fence(obj, fence);
428 if (drm_mm_node_allocated(&node)) {
429 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
430 remove_mappable_node(ggtt, &node);
435 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
440 * Reads data from the object referenced by handle.
441 * @dev: drm device pointer
442 * @data: ioctl data blob
443 * @file: drm file pointer
445 * On error, the contents of *data are undefined.
448 i915_gem_pread_ioctl(struct drm_device *dev, void *data,
449 struct drm_file *file)
451 struct drm_i915_gem_pread *args = data;
452 struct drm_i915_gem_object *obj;
458 if (!access_ok(u64_to_user_ptr(args->data_ptr),
462 obj = i915_gem_object_lookup(file, args->handle);
466 /* Bounds check source. */
467 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
472 trace_i915_gem_object_pread(obj, args->offset, args->size);
474 ret = i915_gem_object_wait(obj,
475 I915_WAIT_INTERRUPTIBLE,
476 MAX_SCHEDULE_TIMEOUT);
480 ret = i915_gem_object_pin_pages(obj);
484 ret = i915_gem_shmem_pread(obj, args);
485 if (ret == -EFAULT || ret == -ENODEV)
486 ret = i915_gem_gtt_pread(obj, args);
488 i915_gem_object_unpin_pages(obj);
490 i915_gem_object_put(obj);
494 /* This is the fast write path which cannot handle
495 * page faults in the source data
499 ggtt_write(struct io_mapping *mapping,
500 loff_t base, int offset,
501 char __user *user_data, int length)
504 unsigned long unwritten;
506 /* We can use the cpu mem copy function because this is X86. */
507 vaddr = io_mapping_map_atomic_wc(mapping, base);
508 unwritten = __copy_from_user_inatomic_nocache((void __force *)vaddr + offset,
510 io_mapping_unmap_atomic(vaddr);
512 vaddr = io_mapping_map_wc(mapping, base, PAGE_SIZE);
513 unwritten = copy_from_user((void __force *)vaddr + offset,
515 io_mapping_unmap(vaddr);
522 * This is the fast pwrite path, where we copy the data directly from the
523 * user into the GTT, uncached.
524 * @obj: i915 GEM object
525 * @args: pwrite arguments structure
528 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object *obj,
529 const struct drm_i915_gem_pwrite *args)
531 struct drm_i915_private *i915 = to_i915(obj->base.dev);
532 struct i915_ggtt *ggtt = &i915->ggtt;
533 struct intel_runtime_pm *rpm = &i915->runtime_pm;
534 intel_wakeref_t wakeref;
535 struct drm_mm_node node;
536 struct dma_fence *fence;
537 struct i915_vma *vma;
539 void __user *user_data;
542 if (i915_gem_object_has_struct_page(obj)) {
544 * Avoid waking the device up if we can fallback, as
545 * waking/resuming is very slow (worst-case 10-100 ms
546 * depending on PCI sleeps and our own resume time).
547 * This easily dwarfs any performance advantage from
548 * using the cache bypass of indirect GGTT access.
550 wakeref = intel_runtime_pm_get_if_in_use(rpm);
554 /* No backing pages, no fallback, we must force GGTT access */
555 wakeref = intel_runtime_pm_get(rpm);
558 vma = ERR_PTR(-ENODEV);
559 if (!i915_gem_object_is_tiled(obj))
560 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0,
562 PIN_NONBLOCK /* NOWARN */ |
565 node.start = i915_ggtt_offset(vma);
568 ret = insert_mappable_node(ggtt, &node, PAGE_SIZE);
571 GEM_BUG_ON(!drm_mm_node_allocated(&node));
574 ret = i915_gem_object_lock_interruptible(obj);
578 ret = i915_gem_object_set_to_gtt_domain(obj, true);
580 i915_gem_object_unlock(obj);
584 fence = i915_gem_object_lock_fence(obj);
585 i915_gem_object_unlock(obj);
591 intel_frontbuffer_invalidate(obj->frontbuffer, ORIGIN_CPU);
593 user_data = u64_to_user_ptr(args->data_ptr);
594 offset = args->offset;
597 /* Operation in this page
599 * page_base = page offset within aperture
600 * page_offset = offset within page
601 * page_length = bytes to copy for this page
603 u32 page_base = node.start;
604 unsigned int page_offset = offset_in_page(offset);
605 unsigned int page_length = PAGE_SIZE - page_offset;
606 page_length = remain < page_length ? remain : page_length;
607 if (drm_mm_node_allocated(&node)) {
608 /* flush the write before we modify the GGTT */
609 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
610 ggtt->vm.insert_page(&ggtt->vm,
611 i915_gem_object_get_dma_address(obj, offset >> PAGE_SHIFT),
612 node.start, I915_CACHE_NONE, 0);
613 wmb(); /* flush modifications to the GGTT (insert_page) */
615 page_base += offset & PAGE_MASK;
617 /* If we get a fault while copying data, then (presumably) our
618 * source page isn't available. Return the error and we'll
619 * retry in the slow path.
620 * If the object is non-shmem backed, we retry again with the
621 * path that handles page fault.
623 if (ggtt_write(&ggtt->iomap, page_base, page_offset,
624 user_data, page_length)) {
629 remain -= page_length;
630 user_data += page_length;
631 offset += page_length;
633 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
635 i915_gem_object_unlock_fence(obj, fence);
637 intel_gt_flush_ggtt_writes(ggtt->vm.gt);
638 if (drm_mm_node_allocated(&node)) {
639 ggtt->vm.clear_range(&ggtt->vm, node.start, node.size);
640 remove_mappable_node(ggtt, &node);
645 intel_runtime_pm_put(rpm, wakeref);
649 /* Per-page copy function for the shmem pwrite fastpath.
650 * Flushes invalid cachelines before writing to the target if
651 * needs_clflush_before is set and flushes out any written cachelines after
652 * writing if needs_clflush is set.
655 shmem_pwrite(struct page *page, int offset, int len, char __user *user_data,
656 bool needs_clflush_before,
657 bool needs_clflush_after)
664 if (needs_clflush_before)
665 drm_clflush_virt_range(vaddr + offset, len);
667 ret = __copy_from_user(vaddr + offset, user_data, len);
668 if (!ret && needs_clflush_after)
669 drm_clflush_virt_range(vaddr + offset, len);
673 return ret ? -EFAULT : 0;
677 i915_gem_shmem_pwrite(struct drm_i915_gem_object *obj,
678 const struct drm_i915_gem_pwrite *args)
680 unsigned int partial_cacheline_write;
681 unsigned int needs_clflush;
682 unsigned int offset, idx;
683 struct dma_fence *fence;
684 void __user *user_data;
688 ret = i915_gem_object_prepare_write(obj, &needs_clflush);
692 fence = i915_gem_object_lock_fence(obj);
693 i915_gem_object_finish_access(obj);
697 /* If we don't overwrite a cacheline completely we need to be
698 * careful to have up-to-date data by first clflushing. Don't
699 * overcomplicate things and flush the entire patch.
701 partial_cacheline_write = 0;
702 if (needs_clflush & CLFLUSH_BEFORE)
703 partial_cacheline_write = boot_cpu_data.x86_clflush_size - 1;
705 user_data = u64_to_user_ptr(args->data_ptr);
707 offset = offset_in_page(args->offset);
708 for (idx = args->offset >> PAGE_SHIFT; remain; idx++) {
709 struct page *page = i915_gem_object_get_page(obj, idx);
710 unsigned int length = min_t(u64, remain, PAGE_SIZE - offset);
712 ret = shmem_pwrite(page, offset, length, user_data,
713 (offset | length) & partial_cacheline_write,
714 needs_clflush & CLFLUSH_AFTER);
723 intel_frontbuffer_flush(obj->frontbuffer, ORIGIN_CPU);
724 i915_gem_object_unlock_fence(obj, fence);
730 * Writes data to the object referenced by handle.
732 * @data: ioctl data blob
735 * On error, the contents of the buffer that were to be modified are undefined.
738 i915_gem_pwrite_ioctl(struct drm_device *dev, void *data,
739 struct drm_file *file)
741 struct drm_i915_gem_pwrite *args = data;
742 struct drm_i915_gem_object *obj;
748 if (!access_ok(u64_to_user_ptr(args->data_ptr), args->size))
751 obj = i915_gem_object_lookup(file, args->handle);
755 /* Bounds check destination. */
756 if (range_overflows_t(u64, args->offset, args->size, obj->base.size)) {
761 /* Writes not allowed into this read-only object */
762 if (i915_gem_object_is_readonly(obj)) {
767 trace_i915_gem_object_pwrite(obj, args->offset, args->size);
770 if (obj->ops->pwrite)
771 ret = obj->ops->pwrite(obj, args);
775 ret = i915_gem_object_wait(obj,
776 I915_WAIT_INTERRUPTIBLE |
778 MAX_SCHEDULE_TIMEOUT);
782 ret = i915_gem_object_pin_pages(obj);
787 /* We can only do the GTT pwrite on untiled buffers, as otherwise
788 * it would end up going through the fenced access, and we'll get
789 * different detiling behavior between reading and writing.
790 * pread/pwrite currently are reading and writing from the CPU
791 * perspective, requiring manual detiling by the client.
793 if (!i915_gem_object_has_struct_page(obj) ||
794 cpu_write_needs_clflush(obj))
795 /* Note that the gtt paths might fail with non-page-backed user
796 * pointers (e.g. gtt mappings when moving data between
797 * textures). Fallback to the shmem path in that case.
799 ret = i915_gem_gtt_pwrite_fast(obj, args);
801 if (ret == -EFAULT || ret == -ENOSPC) {
802 if (obj->phys_handle)
803 ret = i915_gem_phys_pwrite(obj, args, file);
805 ret = i915_gem_shmem_pwrite(obj, args);
808 i915_gem_object_unpin_pages(obj);
810 i915_gem_object_put(obj);
815 * Called when user space has done writes to this buffer
817 * @data: ioctl data blob
821 i915_gem_sw_finish_ioctl(struct drm_device *dev, void *data,
822 struct drm_file *file)
824 struct drm_i915_gem_sw_finish *args = data;
825 struct drm_i915_gem_object *obj;
827 obj = i915_gem_object_lookup(file, args->handle);
832 * Proxy objects are barred from CPU access, so there is no
833 * need to ban sw_finish as it is a nop.
836 /* Pinned buffers may be scanout, so flush the cache */
837 i915_gem_object_flush_if_display(obj);
838 i915_gem_object_put(obj);
843 void i915_gem_runtime_suspend(struct drm_i915_private *i915)
845 struct drm_i915_gem_object *obj, *on;
849 * Only called during RPM suspend. All users of the userfault_list
850 * must be holding an RPM wakeref to ensure that this can not
851 * run concurrently with themselves (and use the struct_mutex for
852 * protection between themselves).
855 list_for_each_entry_safe(obj, on,
856 &i915->ggtt.userfault_list, userfault_link)
857 __i915_gem_object_release_mmap(obj);
860 * The fence will be lost when the device powers down. If any were
861 * in use by hardware (i.e. they are pinned), we should not be powering
862 * down! All other fences will be reacquired by the user upon waking.
864 for (i = 0; i < i915->ggtt.num_fences; i++) {
865 struct i915_fence_reg *reg = &i915->ggtt.fence_regs[i];
868 * Ideally we want to assert that the fence register is not
869 * live at this point (i.e. that no piece of code will be
870 * trying to write through fence + GTT, as that both violates
871 * our tracking of activity and associated locking/barriers,
872 * but also is illegal given that the hw is powered down).
874 * Previously we used reg->pin_count as a "liveness" indicator.
875 * That is not sufficient, and we need a more fine-grained
876 * tool if we want to have a sanity check here.
882 GEM_BUG_ON(i915_vma_has_userfault(reg->vma));
888 i915_gem_object_ggtt_pin(struct drm_i915_gem_object *obj,
889 const struct i915_ggtt_view *view,
894 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
895 struct i915_address_space *vm = &dev_priv->ggtt.vm;
897 return i915_gem_object_pin(obj, vm, view, size, alignment,
902 i915_gem_object_pin(struct drm_i915_gem_object *obj,
903 struct i915_address_space *vm,
904 const struct i915_ggtt_view *view,
909 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
910 struct i915_vma *vma;
913 if (i915_gem_object_never_bind_ggtt(obj))
914 return ERR_PTR(-ENODEV);
916 if (flags & PIN_MAPPABLE &&
917 (!view || view->type == I915_GGTT_VIEW_NORMAL)) {
918 /* If the required space is larger than the available
919 * aperture, we will not able to find a slot for the
920 * object and unbinding the object now will be in
921 * vain. Worse, doing so may cause us to ping-pong
922 * the object in and out of the Global GTT and
923 * waste a lot of cycles under the mutex.
925 if (obj->base.size > dev_priv->ggtt.mappable_end)
926 return ERR_PTR(-E2BIG);
928 /* If NONBLOCK is set the caller is optimistically
929 * trying to cache the full object within the mappable
930 * aperture, and *must* have a fallback in place for
931 * situations where we cannot bind the object. We
932 * can be a little more lax here and use the fallback
933 * more often to avoid costly migrations of ourselves
934 * and other objects within the aperture.
936 * Half-the-aperture is used as a simple heuristic.
937 * More interesting would to do search for a free
938 * block prior to making the commitment to unbind.
939 * That caters for the self-harm case, and with a
940 * little more heuristics (e.g. NOFAULT, NOEVICT)
941 * we could try to minimise harm to others.
943 if (flags & PIN_NONBLOCK &&
944 obj->base.size > dev_priv->ggtt.mappable_end / 2)
945 return ERR_PTR(-ENOSPC);
948 vma = i915_vma_instance(obj, vm, view);
952 if (i915_vma_misplaced(vma, size, alignment, flags)) {
953 if (flags & PIN_NONBLOCK) {
954 if (i915_vma_is_pinned(vma) || i915_vma_is_active(vma))
955 return ERR_PTR(-ENOSPC);
957 if (flags & PIN_MAPPABLE &&
958 vma->fence_size > dev_priv->ggtt.mappable_end / 2)
959 return ERR_PTR(-ENOSPC);
962 ret = i915_vma_unbind(vma);
967 if (vma->fence && !i915_gem_object_is_tiled(obj)) {
968 mutex_lock(&vma->vm->mutex);
969 ret = i915_vma_revoke_fence(vma);
970 mutex_unlock(&vma->vm->mutex);
975 ret = i915_vma_pin(vma, size, alignment, flags);
983 i915_gem_madvise_ioctl(struct drm_device *dev, void *data,
984 struct drm_file *file_priv)
986 struct drm_i915_private *i915 = to_i915(dev);
987 struct drm_i915_gem_madvise *args = data;
988 struct drm_i915_gem_object *obj;
991 switch (args->madv) {
992 case I915_MADV_DONTNEED:
993 case I915_MADV_WILLNEED:
999 obj = i915_gem_object_lookup(file_priv, args->handle);
1003 err = mutex_lock_interruptible(&obj->mm.lock);
1007 if (i915_gem_object_has_pages(obj) &&
1008 i915_gem_object_is_tiled(obj) &&
1009 i915->quirks & QUIRK_PIN_SWIZZLED_PAGES) {
1010 if (obj->mm.madv == I915_MADV_WILLNEED) {
1011 GEM_BUG_ON(!obj->mm.quirked);
1012 __i915_gem_object_unpin_pages(obj);
1013 obj->mm.quirked = false;
1015 if (args->madv == I915_MADV_WILLNEED) {
1016 GEM_BUG_ON(obj->mm.quirked);
1017 __i915_gem_object_pin_pages(obj);
1018 obj->mm.quirked = true;
1022 if (obj->mm.madv != __I915_MADV_PURGED)
1023 obj->mm.madv = args->madv;
1025 if (i915_gem_object_has_pages(obj)) {
1026 struct list_head *list;
1028 if (i915_gem_object_is_shrinkable(obj)) {
1029 unsigned long flags;
1031 spin_lock_irqsave(&i915->mm.obj_lock, flags);
1033 if (obj->mm.madv != I915_MADV_WILLNEED)
1034 list = &i915->mm.purge_list;
1036 list = &i915->mm.shrink_list;
1037 list_move_tail(&obj->mm.link, list);
1039 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
1043 /* if the object is no longer attached, discard its backing storage */
1044 if (obj->mm.madv == I915_MADV_DONTNEED &&
1045 !i915_gem_object_has_pages(obj))
1046 i915_gem_object_truncate(obj);
1048 args->retained = obj->mm.madv != __I915_MADV_PURGED;
1049 mutex_unlock(&obj->mm.lock);
1052 i915_gem_object_put(obj);
1056 static int __intel_engines_record_defaults(struct intel_gt *gt)
1058 struct i915_request *requests[I915_NUM_ENGINES] = {};
1059 struct intel_engine_cs *engine;
1060 enum intel_engine_id id;
1064 * As we reset the gpu during very early sanitisation, the current
1065 * register state on the GPU should reflect its defaults values.
1066 * We load a context onto the hw (with restore-inhibit), then switch
1067 * over to a second context to save that default register state. We
1068 * can then prime every new context with that state so they all start
1069 * from the same default HW values.
1072 for_each_engine(engine, gt, id) {
1073 struct intel_context *ce;
1074 struct i915_request *rq;
1076 /* We must be able to switch to something! */
1077 GEM_BUG_ON(!engine->kernel_context);
1078 engine->serial++; /* force the kernel context switch */
1080 ce = intel_context_create(engine->kernel_context->gem_context,
1087 rq = intel_context_create_request(ce);
1090 intel_context_put(ce);
1094 err = intel_engine_emit_ctx_wa(rq);
1098 err = intel_renderstate_emit(rq);
1103 requests[id] = i915_request_get(rq);
1104 i915_request_add(rq);
1109 /* Flush the default context image to memory, and enable powersaving. */
1110 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
1115 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1116 struct i915_request *rq;
1117 struct i915_vma *state;
1124 /* We want to be able to unbind the state from the GGTT */
1125 GEM_BUG_ON(intel_context_is_pinned(rq->hw_context));
1127 state = rq->hw_context->state;
1132 * As we will hold a reference to the logical state, it will
1133 * not be torn down with the context, and importantly the
1134 * object will hold onto its vma (making it possible for a
1135 * stray GTT write to corrupt our defaults). Unmap the vma
1136 * from the GTT to prevent such accidents and reclaim the
1139 err = i915_vma_unbind(state);
1143 i915_gem_object_lock(state->obj);
1144 err = i915_gem_object_set_to_cpu_domain(state->obj, false);
1145 i915_gem_object_unlock(state->obj);
1149 i915_gem_object_set_cache_coherency(state->obj, I915_CACHE_LLC);
1151 /* Check we can acquire the image of the context state */
1152 vaddr = i915_gem_object_pin_map(state->obj, I915_MAP_FORCE_WB);
1153 if (IS_ERR(vaddr)) {
1154 err = PTR_ERR(vaddr);
1158 rq->engine->default_state = i915_gem_object_get(state->obj);
1159 i915_gem_object_unpin_map(state->obj);
1164 * If we have to abandon now, we expect the engines to be idle
1165 * and ready to be torn-down. The quickest way we can accomplish
1166 * this is by declaring ourselves wedged.
1169 intel_gt_set_wedged(gt);
1171 for (id = 0; id < ARRAY_SIZE(requests); id++) {
1172 struct intel_context *ce;
1173 struct i915_request *rq;
1179 ce = rq->hw_context;
1180 i915_request_put(rq);
1181 intel_context_put(ce);
1186 static int intel_engines_verify_workarounds(struct intel_gt *gt)
1188 struct intel_engine_cs *engine;
1189 enum intel_engine_id id;
1192 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
1195 for_each_engine(engine, gt, id) {
1196 if (intel_engine_verify_workarounds(engine, "load"))
1203 int i915_gem_init(struct drm_i915_private *dev_priv)
1207 /* We need to fallback to 4K pages if host doesn't support huge gtt. */
1208 if (intel_vgpu_active(dev_priv) && !intel_vgpu_has_huge_gtt(dev_priv))
1209 mkwrite_device_info(dev_priv)->page_sizes =
1210 I915_GTT_PAGE_SIZE_4K;
1212 intel_timelines_init(dev_priv);
1214 ret = i915_gem_init_userptr(dev_priv);
1218 intel_uc_fetch_firmwares(&dev_priv->gt.uc);
1219 intel_wopcm_init(&dev_priv->wopcm);
1221 /* This is just a security blanket to placate dragons.
1222 * On some systems, we very sporadically observe that the first TLBs
1223 * used by the CS may be stale, despite us poking the TLB reset. If
1224 * we hold the forcewake during initialisation these problems
1225 * just magically go away.
1227 intel_uncore_forcewake_get(&dev_priv->uncore, FORCEWAKE_ALL);
1229 ret = i915_init_ggtt(dev_priv);
1231 GEM_BUG_ON(ret == -EIO);
1235 intel_gt_init(&dev_priv->gt);
1237 ret = intel_engines_setup(&dev_priv->gt);
1239 GEM_BUG_ON(ret == -EIO);
1243 ret = i915_gem_init_contexts(dev_priv);
1245 GEM_BUG_ON(ret == -EIO);
1249 ret = intel_engines_init(&dev_priv->gt);
1251 GEM_BUG_ON(ret == -EIO);
1255 intel_uc_init(&dev_priv->gt.uc);
1257 ret = intel_gt_init_hw(&dev_priv->gt);
1261 /* Only when the HW is re-initialised, can we replay the requests */
1262 ret = intel_gt_resume(&dev_priv->gt);
1267 * Despite its name intel_init_clock_gating applies both display
1268 * clock gating workarounds; GT mmio workarounds and the occasional
1269 * GT power context workaround. Worse, sometimes it includes a context
1270 * register workaround which we need to apply before we record the
1271 * default HW state for all contexts.
1273 * FIXME: break up the workarounds and apply them at the right time!
1275 intel_init_clock_gating(dev_priv);
1277 ret = intel_engines_verify_workarounds(&dev_priv->gt);
1281 ret = __intel_engines_record_defaults(&dev_priv->gt);
1285 ret = i915_inject_probe_error(dev_priv, -ENODEV);
1289 ret = i915_inject_probe_error(dev_priv, -EIO);
1293 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1298 * Unwinding is complicated by that we want to handle -EIO to mean
1299 * disable GPU submission but keep KMS alive. We want to mark the
1300 * HW as irrevisibly wedged, but keep enough state around that the
1301 * driver doesn't explode during runtime.
1304 intel_gt_set_wedged_on_init(&dev_priv->gt);
1305 i915_gem_suspend(dev_priv);
1306 i915_gem_suspend_late(dev_priv);
1308 i915_gem_drain_workqueue(dev_priv);
1310 intel_uc_fini_hw(&dev_priv->gt.uc);
1313 intel_uc_fini(&dev_priv->gt.uc);
1314 intel_engines_cleanup(&dev_priv->gt);
1318 i915_gem_driver_release__contexts(dev_priv);
1320 intel_gt_driver_release(&dev_priv->gt);
1322 intel_uncore_forcewake_put(&dev_priv->uncore, FORCEWAKE_ALL);
1325 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1326 i915_gem_cleanup_userptr(dev_priv);
1327 intel_timelines_fini(dev_priv);
1332 * Allow engines or uC initialisation to fail by marking the GPU
1333 * as wedged. But we only want to do this when the GPU is angry,
1334 * for all other failure, such as an allocation failure, bail.
1336 if (!intel_gt_is_wedged(&dev_priv->gt)) {
1337 i915_probe_error(dev_priv,
1338 "Failed to initialize GPU, declaring it wedged!\n");
1339 intel_gt_set_wedged(&dev_priv->gt);
1342 /* Minimal basic recovery for KMS */
1343 ret = i915_ggtt_enable_hw(dev_priv);
1344 i915_gem_restore_gtt_mappings(dev_priv);
1345 i915_gem_restore_fences(&dev_priv->ggtt);
1346 intel_init_clock_gating(dev_priv);
1349 i915_gem_drain_freed_objects(dev_priv);
1353 void i915_gem_driver_register(struct drm_i915_private *i915)
1355 i915_gem_driver_register__shrinker(i915);
1357 intel_engines_driver_register(i915);
1360 void i915_gem_driver_unregister(struct drm_i915_private *i915)
1362 i915_gem_driver_unregister__shrinker(i915);
1365 void i915_gem_driver_remove(struct drm_i915_private *dev_priv)
1367 intel_wakeref_auto_fini(&dev_priv->ggtt.userfault_wakeref);
1369 i915_gem_suspend_late(dev_priv);
1370 intel_gt_driver_remove(&dev_priv->gt);
1372 /* Flush any outstanding unpin_work. */
1373 i915_gem_drain_workqueue(dev_priv);
1375 intel_uc_fini_hw(&dev_priv->gt.uc);
1376 intel_uc_fini(&dev_priv->gt.uc);
1378 i915_gem_drain_freed_objects(dev_priv);
1381 void i915_gem_driver_release(struct drm_i915_private *dev_priv)
1383 intel_engines_cleanup(&dev_priv->gt);
1384 i915_gem_driver_release__contexts(dev_priv);
1385 intel_gt_driver_release(&dev_priv->gt);
1387 intel_wa_list_free(&dev_priv->gt_wa_list);
1389 intel_uc_cleanup_firmwares(&dev_priv->gt.uc);
1390 i915_gem_cleanup_userptr(dev_priv);
1391 intel_timelines_fini(dev_priv);
1393 i915_gem_drain_freed_objects(dev_priv);
1395 WARN_ON(!list_empty(&dev_priv->gem.contexts.list));
1398 static void i915_gem_init__mm(struct drm_i915_private *i915)
1400 spin_lock_init(&i915->mm.obj_lock);
1402 init_llist_head(&i915->mm.free_list);
1404 INIT_LIST_HEAD(&i915->mm.purge_list);
1405 INIT_LIST_HEAD(&i915->mm.shrink_list);
1407 i915_gem_init__objects(i915);
1410 void i915_gem_init_early(struct drm_i915_private *dev_priv)
1412 i915_gem_init__mm(dev_priv);
1414 spin_lock_init(&dev_priv->fb_tracking.lock);
1417 void i915_gem_cleanup_early(struct drm_i915_private *dev_priv)
1419 i915_gem_drain_freed_objects(dev_priv);
1420 GEM_BUG_ON(!llist_empty(&dev_priv->mm.free_list));
1421 GEM_BUG_ON(atomic_read(&dev_priv->mm.free_count));
1422 WARN_ON(dev_priv->mm.shrink_count);
1425 int i915_gem_freeze(struct drm_i915_private *dev_priv)
1427 /* Discard all purgeable objects, let userspace recover those as
1428 * required after resuming.
1430 i915_gem_shrink_all(dev_priv);
1435 int i915_gem_freeze_late(struct drm_i915_private *i915)
1437 struct drm_i915_gem_object *obj;
1438 intel_wakeref_t wakeref;
1441 * Called just before we write the hibernation image.
1443 * We need to update the domain tracking to reflect that the CPU
1444 * will be accessing all the pages to create and restore from the
1445 * hibernation, and so upon restoration those pages will be in the
1448 * To make sure the hibernation image contains the latest state,
1449 * we update that state just before writing out the image.
1451 * To try and reduce the hibernation image, we manually shrink
1452 * the objects as well, see i915_gem_freeze()
1455 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1457 i915_gem_shrink(i915, -1UL, NULL, ~0);
1458 i915_gem_drain_freed_objects(i915);
1460 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link) {
1461 i915_gem_object_lock(obj);
1462 WARN_ON(i915_gem_object_set_to_cpu_domain(obj, true));
1463 i915_gem_object_unlock(obj);
1466 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1471 void i915_gem_release(struct drm_device *dev, struct drm_file *file)
1473 struct drm_i915_file_private *file_priv = file->driver_priv;
1474 struct i915_request *request;
1476 /* Clean up our request list when the client is going away, so that
1477 * later retire_requests won't dereference our soon-to-be-gone
1480 spin_lock(&file_priv->mm.lock);
1481 list_for_each_entry(request, &file_priv->mm.request_list, client_link)
1482 request->file_priv = NULL;
1483 spin_unlock(&file_priv->mm.lock);
1486 int i915_gem_open(struct drm_i915_private *i915, struct drm_file *file)
1488 struct drm_i915_file_private *file_priv;
1493 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
1497 file->driver_priv = file_priv;
1498 file_priv->dev_priv = i915;
1499 file_priv->file = file;
1501 spin_lock_init(&file_priv->mm.lock);
1502 INIT_LIST_HEAD(&file_priv->mm.request_list);
1504 file_priv->bsd_engine = -1;
1505 file_priv->hang_timestamp = jiffies;
1507 ret = i915_gem_context_open(i915, file);
1514 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1515 #include "selftests/mock_gem_device.c"
1516 #include "selftests/i915_gem.c"