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>
29 #include <drm/drm_vma_manager.h>
30 #include <drm/i915_drm.h>
32 #include "i915_vgpu.h"
33 #include "i915_trace.h"
34 #include "intel_drv.h"
35 #include "intel_frontbuffer.h"
36 #include "intel_mocs.h"
37 #include <linux/dma-fence-array.h>
38 #include <linux/reservation.h>
39 #include <linux/shmem_fs.h>
40 #include <linux/slab.h>
41 #include <linux/stop_machine.h>
42 #include <linux/swap.h>
43 #include <linux/pci.h>
44 #include <linux/dma-buf.h>
46 static void i915_gem_flush_free_objects(struct drm_i915_private
*i915
);
47 static void i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object
*obj
);
48 static void i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object
*obj
);
50 static bool cpu_cache_is_coherent(struct drm_device
*dev
,
51 enum i915_cache_level level
)
53 return HAS_LLC(to_i915(dev
)) || level
!= I915_CACHE_NONE
;
56 static bool cpu_write_needs_clflush(struct drm_i915_gem_object
*obj
)
58 if (obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
)
61 if (!cpu_cache_is_coherent(obj
->base
.dev
, obj
->cache_level
))
64 return obj
->pin_display
;
68 insert_mappable_node(struct i915_ggtt
*ggtt
,
69 struct drm_mm_node
*node
, u32 size
)
71 memset(node
, 0, sizeof(*node
));
72 return drm_mm_insert_node_in_range_generic(&ggtt
->base
.mm
, node
,
74 I915_COLOR_UNEVICTABLE
,
75 0, ggtt
->mappable_end
,
76 DRM_MM_SEARCH_DEFAULT
,
77 DRM_MM_CREATE_DEFAULT
);
81 remove_mappable_node(struct drm_mm_node
*node
)
83 drm_mm_remove_node(node
);
86 /* some bookkeeping */
87 static void i915_gem_info_add_obj(struct drm_i915_private
*dev_priv
,
90 spin_lock(&dev_priv
->mm
.object_stat_lock
);
91 dev_priv
->mm
.object_count
++;
92 dev_priv
->mm
.object_memory
+= size
;
93 spin_unlock(&dev_priv
->mm
.object_stat_lock
);
96 static void i915_gem_info_remove_obj(struct drm_i915_private
*dev_priv
,
99 spin_lock(&dev_priv
->mm
.object_stat_lock
);
100 dev_priv
->mm
.object_count
--;
101 dev_priv
->mm
.object_memory
-= size
;
102 spin_unlock(&dev_priv
->mm
.object_stat_lock
);
106 i915_gem_wait_for_error(struct i915_gpu_error
*error
)
112 if (!i915_reset_in_progress(error
))
116 * Only wait 10 seconds for the gpu reset to complete to avoid hanging
117 * userspace. If it takes that long something really bad is going on and
118 * we should simply try to bail out and fail as gracefully as possible.
120 ret
= wait_event_interruptible_timeout(error
->reset_queue
,
121 !i915_reset_in_progress(error
),
124 DRM_ERROR("Timed out waiting for the gpu reset to complete\n");
126 } else if (ret
< 0) {
133 int i915_mutex_lock_interruptible(struct drm_device
*dev
)
135 struct drm_i915_private
*dev_priv
= to_i915(dev
);
138 ret
= i915_gem_wait_for_error(&dev_priv
->gpu_error
);
142 ret
= mutex_lock_interruptible(&dev
->struct_mutex
);
150 i915_gem_get_aperture_ioctl(struct drm_device
*dev
, void *data
,
151 struct drm_file
*file
)
153 struct drm_i915_private
*dev_priv
= to_i915(dev
);
154 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
155 struct drm_i915_gem_get_aperture
*args
= data
;
156 struct i915_vma
*vma
;
160 mutex_lock(&dev
->struct_mutex
);
161 list_for_each_entry(vma
, &ggtt
->base
.active_list
, vm_link
)
162 if (i915_vma_is_pinned(vma
))
163 pinned
+= vma
->node
.size
;
164 list_for_each_entry(vma
, &ggtt
->base
.inactive_list
, vm_link
)
165 if (i915_vma_is_pinned(vma
))
166 pinned
+= vma
->node
.size
;
167 mutex_unlock(&dev
->struct_mutex
);
169 args
->aper_size
= ggtt
->base
.total
;
170 args
->aper_available_size
= args
->aper_size
- pinned
;
175 static struct sg_table
*
176 i915_gem_object_get_pages_phys(struct drm_i915_gem_object
*obj
)
178 struct address_space
*mapping
= obj
->base
.filp
->f_mapping
;
179 drm_dma_handle_t
*phys
;
181 struct scatterlist
*sg
;
185 if (WARN_ON(i915_gem_object_needs_bit17_swizzle(obj
)))
186 return ERR_PTR(-EINVAL
);
188 /* Always aligning to the object size, allows a single allocation
189 * to handle all possible callers, and given typical object sizes,
190 * the alignment of the buddy allocation will naturally match.
192 phys
= drm_pci_alloc(obj
->base
.dev
,
194 roundup_pow_of_two(obj
->base
.size
));
196 return ERR_PTR(-ENOMEM
);
199 for (i
= 0; i
< obj
->base
.size
/ PAGE_SIZE
; i
++) {
203 page
= shmem_read_mapping_page(mapping
, i
);
209 src
= kmap_atomic(page
);
210 memcpy(vaddr
, src
, PAGE_SIZE
);
211 drm_clflush_virt_range(vaddr
, PAGE_SIZE
);
218 i915_gem_chipset_flush(to_i915(obj
->base
.dev
));
220 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
222 st
= ERR_PTR(-ENOMEM
);
226 if (sg_alloc_table(st
, 1, GFP_KERNEL
)) {
228 st
= ERR_PTR(-ENOMEM
);
234 sg
->length
= obj
->base
.size
;
236 sg_dma_address(sg
) = phys
->busaddr
;
237 sg_dma_len(sg
) = obj
->base
.size
;
239 obj
->phys_handle
= phys
;
243 drm_pci_free(obj
->base
.dev
, phys
);
248 __i915_gem_object_release_shmem(struct drm_i915_gem_object
*obj
,
249 struct sg_table
*pages
,
252 GEM_BUG_ON(obj
->mm
.madv
== __I915_MADV_PURGED
);
254 if (obj
->mm
.madv
== I915_MADV_DONTNEED
)
255 obj
->mm
.dirty
= false;
258 (obj
->base
.read_domains
& I915_GEM_DOMAIN_CPU
) == 0 &&
259 !cpu_cache_is_coherent(obj
->base
.dev
, obj
->cache_level
))
260 drm_clflush_sg(pages
);
262 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
263 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
267 i915_gem_object_put_pages_phys(struct drm_i915_gem_object
*obj
,
268 struct sg_table
*pages
)
270 __i915_gem_object_release_shmem(obj
, pages
, false);
273 struct address_space
*mapping
= obj
->base
.filp
->f_mapping
;
274 char *vaddr
= obj
->phys_handle
->vaddr
;
277 for (i
= 0; i
< obj
->base
.size
/ PAGE_SIZE
; i
++) {
281 page
= shmem_read_mapping_page(mapping
, i
);
285 dst
= kmap_atomic(page
);
286 drm_clflush_virt_range(vaddr
, PAGE_SIZE
);
287 memcpy(dst
, vaddr
, PAGE_SIZE
);
290 set_page_dirty(page
);
291 if (obj
->mm
.madv
== I915_MADV_WILLNEED
)
292 mark_page_accessed(page
);
296 obj
->mm
.dirty
= false;
299 sg_free_table(pages
);
302 drm_pci_free(obj
->base
.dev
, obj
->phys_handle
);
306 i915_gem_object_release_phys(struct drm_i915_gem_object
*obj
)
308 i915_gem_object_unpin_pages(obj
);
311 static const struct drm_i915_gem_object_ops i915_gem_phys_ops
= {
312 .get_pages
= i915_gem_object_get_pages_phys
,
313 .put_pages
= i915_gem_object_put_pages_phys
,
314 .release
= i915_gem_object_release_phys
,
317 int i915_gem_object_unbind(struct drm_i915_gem_object
*obj
)
319 struct i915_vma
*vma
;
320 LIST_HEAD(still_in_list
);
323 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
325 /* Closed vma are removed from the obj->vma_list - but they may
326 * still have an active binding on the object. To remove those we
327 * must wait for all rendering to complete to the object (as unbinding
328 * must anyway), and retire the requests.
330 ret
= i915_gem_object_wait(obj
,
331 I915_WAIT_INTERRUPTIBLE
|
334 MAX_SCHEDULE_TIMEOUT
,
339 i915_gem_retire_requests(to_i915(obj
->base
.dev
));
341 while ((vma
= list_first_entry_or_null(&obj
->vma_list
,
344 list_move_tail(&vma
->obj_link
, &still_in_list
);
345 ret
= i915_vma_unbind(vma
);
349 list_splice(&still_in_list
, &obj
->vma_list
);
355 i915_gem_object_wait_fence(struct dma_fence
*fence
,
358 struct intel_rps_client
*rps
)
360 struct drm_i915_gem_request
*rq
;
362 BUILD_BUG_ON(I915_WAIT_INTERRUPTIBLE
!= 0x1);
364 if (test_bit(DMA_FENCE_FLAG_SIGNALED_BIT
, &fence
->flags
))
367 if (!dma_fence_is_i915(fence
))
368 return dma_fence_wait_timeout(fence
,
369 flags
& I915_WAIT_INTERRUPTIBLE
,
372 rq
= to_request(fence
);
373 if (i915_gem_request_completed(rq
))
376 /* This client is about to stall waiting for the GPU. In many cases
377 * this is undesirable and limits the throughput of the system, as
378 * many clients cannot continue processing user input/output whilst
379 * blocked. RPS autotuning may take tens of milliseconds to respond
380 * to the GPU load and thus incurs additional latency for the client.
381 * We can circumvent that by promoting the GPU frequency to maximum
382 * before we wait. This makes the GPU throttle up much more quickly
383 * (good for benchmarks and user experience, e.g. window animations),
384 * but at a cost of spending more power processing the workload
385 * (bad for battery). Not all clients even want their results
386 * immediately and for them we should just let the GPU select its own
387 * frequency to maximise efficiency. To prevent a single client from
388 * forcing the clocks too high for the whole system, we only allow
389 * each client to waitboost once in a busy period.
392 if (INTEL_GEN(rq
->i915
) >= 6)
393 gen6_rps_boost(rq
->i915
, rps
, rq
->emitted_jiffies
);
398 timeout
= i915_wait_request(rq
, flags
, timeout
);
401 if (flags
& I915_WAIT_LOCKED
&& i915_gem_request_completed(rq
))
402 i915_gem_request_retire_upto(rq
);
404 if (rps
&& rq
->global_seqno
== intel_engine_last_submit(rq
->engine
)) {
405 /* The GPU is now idle and this client has stalled.
406 * Since no other client has submitted a request in the
407 * meantime, assume that this client is the only one
408 * supplying work to the GPU but is unable to keep that
409 * work supplied because it is waiting. Since the GPU is
410 * then never kept fully busy, RPS autoclocking will
411 * keep the clocks relatively low, causing further delays.
412 * Compensate by giving the synchronous client credit for
413 * a waitboost next time.
415 spin_lock(&rq
->i915
->rps
.client_lock
);
416 list_del_init(&rps
->link
);
417 spin_unlock(&rq
->i915
->rps
.client_lock
);
424 i915_gem_object_wait_reservation(struct reservation_object
*resv
,
427 struct intel_rps_client
*rps
)
429 struct dma_fence
*excl
;
431 if (flags
& I915_WAIT_ALL
) {
432 struct dma_fence
**shared
;
433 unsigned int count
, i
;
436 ret
= reservation_object_get_fences_rcu(resv
,
437 &excl
, &count
, &shared
);
441 for (i
= 0; i
< count
; i
++) {
442 timeout
= i915_gem_object_wait_fence(shared
[i
],
448 dma_fence_put(shared
[i
]);
451 for (; i
< count
; i
++)
452 dma_fence_put(shared
[i
]);
455 excl
= reservation_object_get_excl_rcu(resv
);
458 if (excl
&& timeout
> 0)
459 timeout
= i915_gem_object_wait_fence(excl
, flags
, timeout
, rps
);
466 static void __fence_set_priority(struct dma_fence
*fence
, int prio
)
468 struct drm_i915_gem_request
*rq
;
469 struct intel_engine_cs
*engine
;
471 if (!dma_fence_is_i915(fence
))
474 rq
= to_request(fence
);
476 if (!engine
->schedule
)
479 engine
->schedule(rq
, prio
);
482 static void fence_set_priority(struct dma_fence
*fence
, int prio
)
484 /* Recurse once into a fence-array */
485 if (dma_fence_is_array(fence
)) {
486 struct dma_fence_array
*array
= to_dma_fence_array(fence
);
489 for (i
= 0; i
< array
->num_fences
; i
++)
490 __fence_set_priority(array
->fences
[i
], prio
);
492 __fence_set_priority(fence
, prio
);
497 i915_gem_object_wait_priority(struct drm_i915_gem_object
*obj
,
501 struct dma_fence
*excl
;
503 if (flags
& I915_WAIT_ALL
) {
504 struct dma_fence
**shared
;
505 unsigned int count
, i
;
508 ret
= reservation_object_get_fences_rcu(obj
->resv
,
509 &excl
, &count
, &shared
);
513 for (i
= 0; i
< count
; i
++) {
514 fence_set_priority(shared
[i
], prio
);
515 dma_fence_put(shared
[i
]);
520 excl
= reservation_object_get_excl_rcu(obj
->resv
);
524 fence_set_priority(excl
, prio
);
531 * Waits for rendering to the object to be completed
532 * @obj: i915 gem object
533 * @flags: how to wait (under a lock, for all rendering or just for writes etc)
534 * @timeout: how long to wait
535 * @rps: client (user process) to charge for any waitboosting
538 i915_gem_object_wait(struct drm_i915_gem_object
*obj
,
541 struct intel_rps_client
*rps
)
544 #if IS_ENABLED(CONFIG_LOCKDEP)
545 GEM_BUG_ON(debug_locks
&&
546 !!lockdep_is_held(&obj
->base
.dev
->struct_mutex
) !=
547 !!(flags
& I915_WAIT_LOCKED
));
549 GEM_BUG_ON(timeout
< 0);
551 timeout
= i915_gem_object_wait_reservation(obj
->resv
,
554 return timeout
< 0 ? timeout
: 0;
557 static struct intel_rps_client
*to_rps_client(struct drm_file
*file
)
559 struct drm_i915_file_private
*fpriv
= file
->driver_priv
;
565 i915_gem_object_attach_phys(struct drm_i915_gem_object
*obj
,
570 if (align
> obj
->base
.size
)
573 if (obj
->ops
== &i915_gem_phys_ops
)
576 if (obj
->mm
.madv
!= I915_MADV_WILLNEED
)
579 if (obj
->base
.filp
== NULL
)
582 ret
= i915_gem_object_unbind(obj
);
586 __i915_gem_object_put_pages(obj
, I915_MM_NORMAL
);
590 obj
->ops
= &i915_gem_phys_ops
;
592 return i915_gem_object_pin_pages(obj
);
596 i915_gem_phys_pwrite(struct drm_i915_gem_object
*obj
,
597 struct drm_i915_gem_pwrite
*args
,
598 struct drm_file
*file
)
600 void *vaddr
= obj
->phys_handle
->vaddr
+ args
->offset
;
601 char __user
*user_data
= u64_to_user_ptr(args
->data_ptr
);
603 /* We manually control the domain here and pretend that it
604 * remains coherent i.e. in the GTT domain, like shmem_pwrite.
606 intel_fb_obj_invalidate(obj
, ORIGIN_CPU
);
607 if (copy_from_user(vaddr
, user_data
, args
->size
))
610 drm_clflush_virt_range(vaddr
, args
->size
);
611 i915_gem_chipset_flush(to_i915(obj
->base
.dev
));
613 intel_fb_obj_flush(obj
, false, ORIGIN_CPU
);
617 void *i915_gem_object_alloc(struct drm_i915_private
*dev_priv
)
619 return kmem_cache_zalloc(dev_priv
->objects
, GFP_KERNEL
);
622 void i915_gem_object_free(struct drm_i915_gem_object
*obj
)
624 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
625 kmem_cache_free(dev_priv
->objects
, obj
);
629 i915_gem_create(struct drm_file
*file
,
630 struct drm_i915_private
*dev_priv
,
634 struct drm_i915_gem_object
*obj
;
638 size
= roundup(size
, PAGE_SIZE
);
642 /* Allocate the new object */
643 obj
= i915_gem_object_create(dev_priv
, size
);
647 ret
= drm_gem_handle_create(file
, &obj
->base
, &handle
);
648 /* drop reference from allocate - handle holds it now */
649 i915_gem_object_put(obj
);
658 i915_gem_dumb_create(struct drm_file
*file
,
659 struct drm_device
*dev
,
660 struct drm_mode_create_dumb
*args
)
662 /* have to work out size/pitch and return them */
663 args
->pitch
= ALIGN(args
->width
* DIV_ROUND_UP(args
->bpp
, 8), 64);
664 args
->size
= args
->pitch
* args
->height
;
665 return i915_gem_create(file
, to_i915(dev
),
666 args
->size
, &args
->handle
);
670 * Creates a new mm object and returns a handle to it.
671 * @dev: drm device pointer
672 * @data: ioctl data blob
673 * @file: drm file pointer
676 i915_gem_create_ioctl(struct drm_device
*dev
, void *data
,
677 struct drm_file
*file
)
679 struct drm_i915_private
*dev_priv
= to_i915(dev
);
680 struct drm_i915_gem_create
*args
= data
;
682 i915_gem_flush_free_objects(dev_priv
);
684 return i915_gem_create(file
, dev_priv
,
685 args
->size
, &args
->handle
);
689 __copy_to_user_swizzled(char __user
*cpu_vaddr
,
690 const char *gpu_vaddr
, int gpu_offset
,
693 int ret
, cpu_offset
= 0;
696 int cacheline_end
= ALIGN(gpu_offset
+ 1, 64);
697 int this_length
= min(cacheline_end
- gpu_offset
, length
);
698 int swizzled_gpu_offset
= gpu_offset
^ 64;
700 ret
= __copy_to_user(cpu_vaddr
+ cpu_offset
,
701 gpu_vaddr
+ swizzled_gpu_offset
,
706 cpu_offset
+= this_length
;
707 gpu_offset
+= this_length
;
708 length
-= this_length
;
715 __copy_from_user_swizzled(char *gpu_vaddr
, int gpu_offset
,
716 const char __user
*cpu_vaddr
,
719 int ret
, cpu_offset
= 0;
722 int cacheline_end
= ALIGN(gpu_offset
+ 1, 64);
723 int this_length
= min(cacheline_end
- gpu_offset
, length
);
724 int swizzled_gpu_offset
= gpu_offset
^ 64;
726 ret
= __copy_from_user(gpu_vaddr
+ swizzled_gpu_offset
,
727 cpu_vaddr
+ cpu_offset
,
732 cpu_offset
+= this_length
;
733 gpu_offset
+= this_length
;
734 length
-= this_length
;
741 * Pins the specified object's pages and synchronizes the object with
742 * GPU accesses. Sets needs_clflush to non-zero if the caller should
743 * flush the object from the CPU cache.
745 int i915_gem_obj_prepare_shmem_read(struct drm_i915_gem_object
*obj
,
746 unsigned int *needs_clflush
)
750 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
753 if (!i915_gem_object_has_struct_page(obj
))
756 ret
= i915_gem_object_wait(obj
,
757 I915_WAIT_INTERRUPTIBLE
|
759 MAX_SCHEDULE_TIMEOUT
,
764 ret
= i915_gem_object_pin_pages(obj
);
768 i915_gem_object_flush_gtt_write_domain(obj
);
770 /* If we're not in the cpu read domain, set ourself into the gtt
771 * read domain and manually flush cachelines (if required). This
772 * optimizes for the case when the gpu will dirty the data
773 * anyway again before the next pread happens.
775 if (!(obj
->base
.read_domains
& I915_GEM_DOMAIN_CPU
))
776 *needs_clflush
= !cpu_cache_is_coherent(obj
->base
.dev
,
779 if (*needs_clflush
&& !static_cpu_has(X86_FEATURE_CLFLUSH
)) {
780 ret
= i915_gem_object_set_to_cpu_domain(obj
, false);
787 /* return with the pages pinned */
791 i915_gem_object_unpin_pages(obj
);
795 int i915_gem_obj_prepare_shmem_write(struct drm_i915_gem_object
*obj
,
796 unsigned int *needs_clflush
)
800 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
803 if (!i915_gem_object_has_struct_page(obj
))
806 ret
= i915_gem_object_wait(obj
,
807 I915_WAIT_INTERRUPTIBLE
|
810 MAX_SCHEDULE_TIMEOUT
,
815 ret
= i915_gem_object_pin_pages(obj
);
819 i915_gem_object_flush_gtt_write_domain(obj
);
821 /* If we're not in the cpu write domain, set ourself into the
822 * gtt write domain and manually flush cachelines (as required).
823 * This optimizes for the case when the gpu will use the data
824 * right away and we therefore have to clflush anyway.
826 if (obj
->base
.write_domain
!= I915_GEM_DOMAIN_CPU
)
827 *needs_clflush
|= cpu_write_needs_clflush(obj
) << 1;
829 /* Same trick applies to invalidate partially written cachelines read
832 if (!(obj
->base
.read_domains
& I915_GEM_DOMAIN_CPU
))
833 *needs_clflush
|= !cpu_cache_is_coherent(obj
->base
.dev
,
836 if (*needs_clflush
&& !static_cpu_has(X86_FEATURE_CLFLUSH
)) {
837 ret
= i915_gem_object_set_to_cpu_domain(obj
, true);
844 if ((*needs_clflush
& CLFLUSH_AFTER
) == 0)
845 obj
->cache_dirty
= true;
847 intel_fb_obj_invalidate(obj
, ORIGIN_CPU
);
848 obj
->mm
.dirty
= true;
849 /* return with the pages pinned */
853 i915_gem_object_unpin_pages(obj
);
858 shmem_clflush_swizzled_range(char *addr
, unsigned long length
,
861 if (unlikely(swizzled
)) {
862 unsigned long start
= (unsigned long) addr
;
863 unsigned long end
= (unsigned long) addr
+ length
;
865 /* For swizzling simply ensure that we always flush both
866 * channels. Lame, but simple and it works. Swizzled
867 * pwrite/pread is far from a hotpath - current userspace
868 * doesn't use it at all. */
869 start
= round_down(start
, 128);
870 end
= round_up(end
, 128);
872 drm_clflush_virt_range((void *)start
, end
- start
);
874 drm_clflush_virt_range(addr
, length
);
879 /* Only difference to the fast-path function is that this can handle bit17
880 * and uses non-atomic copy and kmap functions. */
882 shmem_pread_slow(struct page
*page
, int offset
, int length
,
883 char __user
*user_data
,
884 bool page_do_bit17_swizzling
, bool needs_clflush
)
891 shmem_clflush_swizzled_range(vaddr
+ offset
, length
,
892 page_do_bit17_swizzling
);
894 if (page_do_bit17_swizzling
)
895 ret
= __copy_to_user_swizzled(user_data
, vaddr
, offset
, length
);
897 ret
= __copy_to_user(user_data
, vaddr
+ offset
, length
);
900 return ret
? - EFAULT
: 0;
904 shmem_pread(struct page
*page
, int offset
, int length
, char __user
*user_data
,
905 bool page_do_bit17_swizzling
, bool needs_clflush
)
910 if (!page_do_bit17_swizzling
) {
911 char *vaddr
= kmap_atomic(page
);
914 drm_clflush_virt_range(vaddr
+ offset
, length
);
915 ret
= __copy_to_user_inatomic(user_data
, vaddr
+ offset
, length
);
916 kunmap_atomic(vaddr
);
921 return shmem_pread_slow(page
, offset
, length
, user_data
,
922 page_do_bit17_swizzling
, needs_clflush
);
926 i915_gem_shmem_pread(struct drm_i915_gem_object
*obj
,
927 struct drm_i915_gem_pread
*args
)
929 char __user
*user_data
;
931 unsigned int obj_do_bit17_swizzling
;
932 unsigned int needs_clflush
;
933 unsigned int idx
, offset
;
936 obj_do_bit17_swizzling
= 0;
937 if (i915_gem_object_needs_bit17_swizzle(obj
))
938 obj_do_bit17_swizzling
= BIT(17);
940 ret
= mutex_lock_interruptible(&obj
->base
.dev
->struct_mutex
);
944 ret
= i915_gem_obj_prepare_shmem_read(obj
, &needs_clflush
);
945 mutex_unlock(&obj
->base
.dev
->struct_mutex
);
950 user_data
= u64_to_user_ptr(args
->data_ptr
);
951 offset
= offset_in_page(args
->offset
);
952 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
953 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
957 if (offset
+ length
> PAGE_SIZE
)
958 length
= PAGE_SIZE
- offset
;
960 ret
= shmem_pread(page
, offset
, length
, user_data
,
961 page_to_phys(page
) & obj_do_bit17_swizzling
,
971 i915_gem_obj_finish_shmem_access(obj
);
976 gtt_user_read(struct io_mapping
*mapping
,
977 loff_t base
, int offset
,
978 char __user
*user_data
, int length
)
981 unsigned long unwritten
;
983 /* We can use the cpu mem copy function because this is X86. */
984 vaddr
= (void __force
*)io_mapping_map_atomic_wc(mapping
, base
);
985 unwritten
= __copy_to_user_inatomic(user_data
, vaddr
+ offset
, length
);
986 io_mapping_unmap_atomic(vaddr
);
988 vaddr
= (void __force
*)
989 io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
990 unwritten
= copy_to_user(user_data
, vaddr
+ offset
, length
);
991 io_mapping_unmap(vaddr
);
997 i915_gem_gtt_pread(struct drm_i915_gem_object
*obj
,
998 const struct drm_i915_gem_pread
*args
)
1000 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
1001 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
1002 struct drm_mm_node node
;
1003 struct i915_vma
*vma
;
1004 void __user
*user_data
;
1008 ret
= mutex_lock_interruptible(&i915
->drm
.struct_mutex
);
1012 intel_runtime_pm_get(i915
);
1013 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
1014 PIN_MAPPABLE
| PIN_NONBLOCK
);
1016 node
.start
= i915_ggtt_offset(vma
);
1017 node
.allocated
= false;
1018 ret
= i915_vma_put_fence(vma
);
1020 i915_vma_unpin(vma
);
1025 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
1028 GEM_BUG_ON(!node
.allocated
);
1031 ret
= i915_gem_object_set_to_gtt_domain(obj
, false);
1035 mutex_unlock(&i915
->drm
.struct_mutex
);
1037 user_data
= u64_to_user_ptr(args
->data_ptr
);
1038 remain
= args
->size
;
1039 offset
= args
->offset
;
1041 while (remain
> 0) {
1042 /* Operation in this page
1044 * page_base = page offset within aperture
1045 * page_offset = offset within page
1046 * page_length = bytes to copy for this page
1048 u32 page_base
= node
.start
;
1049 unsigned page_offset
= offset_in_page(offset
);
1050 unsigned page_length
= PAGE_SIZE
- page_offset
;
1051 page_length
= remain
< page_length
? remain
: page_length
;
1052 if (node
.allocated
) {
1054 ggtt
->base
.insert_page(&ggtt
->base
,
1055 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
1056 node
.start
, I915_CACHE_NONE
, 0);
1059 page_base
+= offset
& PAGE_MASK
;
1062 if (gtt_user_read(&ggtt
->mappable
, page_base
, page_offset
,
1063 user_data
, page_length
)) {
1068 remain
-= page_length
;
1069 user_data
+= page_length
;
1070 offset
+= page_length
;
1073 mutex_lock(&i915
->drm
.struct_mutex
);
1075 if (node
.allocated
) {
1077 ggtt
->base
.clear_range(&ggtt
->base
,
1078 node
.start
, node
.size
);
1079 remove_mappable_node(&node
);
1081 i915_vma_unpin(vma
);
1084 intel_runtime_pm_put(i915
);
1085 mutex_unlock(&i915
->drm
.struct_mutex
);
1091 * Reads data from the object referenced by handle.
1092 * @dev: drm device pointer
1093 * @data: ioctl data blob
1094 * @file: drm file pointer
1096 * On error, the contents of *data are undefined.
1099 i915_gem_pread_ioctl(struct drm_device
*dev
, void *data
,
1100 struct drm_file
*file
)
1102 struct drm_i915_gem_pread
*args
= data
;
1103 struct drm_i915_gem_object
*obj
;
1106 if (args
->size
== 0)
1109 if (!access_ok(VERIFY_WRITE
,
1110 u64_to_user_ptr(args
->data_ptr
),
1114 obj
= i915_gem_object_lookup(file
, args
->handle
);
1118 /* Bounds check source. */
1119 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
1124 trace_i915_gem_object_pread(obj
, args
->offset
, args
->size
);
1126 ret
= i915_gem_object_wait(obj
,
1127 I915_WAIT_INTERRUPTIBLE
,
1128 MAX_SCHEDULE_TIMEOUT
,
1129 to_rps_client(file
));
1133 ret
= i915_gem_object_pin_pages(obj
);
1137 ret
= i915_gem_shmem_pread(obj
, args
);
1138 if (ret
== -EFAULT
|| ret
== -ENODEV
)
1139 ret
= i915_gem_gtt_pread(obj
, args
);
1141 i915_gem_object_unpin_pages(obj
);
1143 i915_gem_object_put(obj
);
1147 /* This is the fast write path which cannot handle
1148 * page faults in the source data
1152 ggtt_write(struct io_mapping
*mapping
,
1153 loff_t base
, int offset
,
1154 char __user
*user_data
, int length
)
1157 unsigned long unwritten
;
1159 /* We can use the cpu mem copy function because this is X86. */
1160 vaddr
= (void __force
*)io_mapping_map_atomic_wc(mapping
, base
);
1161 unwritten
= __copy_from_user_inatomic_nocache(vaddr
+ offset
,
1163 io_mapping_unmap_atomic(vaddr
);
1165 vaddr
= (void __force
*)
1166 io_mapping_map_wc(mapping
, base
, PAGE_SIZE
);
1167 unwritten
= copy_from_user(vaddr
+ offset
, user_data
, length
);
1168 io_mapping_unmap(vaddr
);
1175 * This is the fast pwrite path, where we copy the data directly from the
1176 * user into the GTT, uncached.
1177 * @obj: i915 GEM object
1178 * @args: pwrite arguments structure
1181 i915_gem_gtt_pwrite_fast(struct drm_i915_gem_object
*obj
,
1182 const struct drm_i915_gem_pwrite
*args
)
1184 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
1185 struct i915_ggtt
*ggtt
= &i915
->ggtt
;
1186 struct drm_mm_node node
;
1187 struct i915_vma
*vma
;
1189 void __user
*user_data
;
1192 ret
= mutex_lock_interruptible(&i915
->drm
.struct_mutex
);
1196 intel_runtime_pm_get(i915
);
1197 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0,
1198 PIN_MAPPABLE
| PIN_NONBLOCK
);
1200 node
.start
= i915_ggtt_offset(vma
);
1201 node
.allocated
= false;
1202 ret
= i915_vma_put_fence(vma
);
1204 i915_vma_unpin(vma
);
1209 ret
= insert_mappable_node(ggtt
, &node
, PAGE_SIZE
);
1212 GEM_BUG_ON(!node
.allocated
);
1215 ret
= i915_gem_object_set_to_gtt_domain(obj
, true);
1219 mutex_unlock(&i915
->drm
.struct_mutex
);
1221 intel_fb_obj_invalidate(obj
, ORIGIN_CPU
);
1223 user_data
= u64_to_user_ptr(args
->data_ptr
);
1224 offset
= args
->offset
;
1225 remain
= args
->size
;
1227 /* Operation in this page
1229 * page_base = page offset within aperture
1230 * page_offset = offset within page
1231 * page_length = bytes to copy for this page
1233 u32 page_base
= node
.start
;
1234 unsigned int page_offset
= offset_in_page(offset
);
1235 unsigned int page_length
= PAGE_SIZE
- page_offset
;
1236 page_length
= remain
< page_length
? remain
: page_length
;
1237 if (node
.allocated
) {
1238 wmb(); /* flush the write before we modify the GGTT */
1239 ggtt
->base
.insert_page(&ggtt
->base
,
1240 i915_gem_object_get_dma_address(obj
, offset
>> PAGE_SHIFT
),
1241 node
.start
, I915_CACHE_NONE
, 0);
1242 wmb(); /* flush modifications to the GGTT (insert_page) */
1244 page_base
+= offset
& PAGE_MASK
;
1246 /* If we get a fault while copying data, then (presumably) our
1247 * source page isn't available. Return the error and we'll
1248 * retry in the slow path.
1249 * If the object is non-shmem backed, we retry again with the
1250 * path that handles page fault.
1252 if (ggtt_write(&ggtt
->mappable
, page_base
, page_offset
,
1253 user_data
, page_length
)) {
1258 remain
-= page_length
;
1259 user_data
+= page_length
;
1260 offset
+= page_length
;
1262 intel_fb_obj_flush(obj
, false, ORIGIN_CPU
);
1264 mutex_lock(&i915
->drm
.struct_mutex
);
1266 if (node
.allocated
) {
1268 ggtt
->base
.clear_range(&ggtt
->base
,
1269 node
.start
, node
.size
);
1270 remove_mappable_node(&node
);
1272 i915_vma_unpin(vma
);
1275 intel_runtime_pm_put(i915
);
1276 mutex_unlock(&i915
->drm
.struct_mutex
);
1281 shmem_pwrite_slow(struct page
*page
, int offset
, int length
,
1282 char __user
*user_data
,
1283 bool page_do_bit17_swizzling
,
1284 bool needs_clflush_before
,
1285 bool needs_clflush_after
)
1291 if (unlikely(needs_clflush_before
|| page_do_bit17_swizzling
))
1292 shmem_clflush_swizzled_range(vaddr
+ offset
, length
,
1293 page_do_bit17_swizzling
);
1294 if (page_do_bit17_swizzling
)
1295 ret
= __copy_from_user_swizzled(vaddr
, offset
, user_data
,
1298 ret
= __copy_from_user(vaddr
+ offset
, user_data
, length
);
1299 if (needs_clflush_after
)
1300 shmem_clflush_swizzled_range(vaddr
+ offset
, length
,
1301 page_do_bit17_swizzling
);
1304 return ret
? -EFAULT
: 0;
1307 /* Per-page copy function for the shmem pwrite fastpath.
1308 * Flushes invalid cachelines before writing to the target if
1309 * needs_clflush_before is set and flushes out any written cachelines after
1310 * writing if needs_clflush is set.
1313 shmem_pwrite(struct page
*page
, int offset
, int len
, char __user
*user_data
,
1314 bool page_do_bit17_swizzling
,
1315 bool needs_clflush_before
,
1316 bool needs_clflush_after
)
1321 if (!page_do_bit17_swizzling
) {
1322 char *vaddr
= kmap_atomic(page
);
1324 if (needs_clflush_before
)
1325 drm_clflush_virt_range(vaddr
+ offset
, len
);
1326 ret
= __copy_from_user_inatomic(vaddr
+ offset
, user_data
, len
);
1327 if (needs_clflush_after
)
1328 drm_clflush_virt_range(vaddr
+ offset
, len
);
1330 kunmap_atomic(vaddr
);
1335 return shmem_pwrite_slow(page
, offset
, len
, user_data
,
1336 page_do_bit17_swizzling
,
1337 needs_clflush_before
,
1338 needs_clflush_after
);
1342 i915_gem_shmem_pwrite(struct drm_i915_gem_object
*obj
,
1343 const struct drm_i915_gem_pwrite
*args
)
1345 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
1346 void __user
*user_data
;
1348 unsigned int obj_do_bit17_swizzling
;
1349 unsigned int partial_cacheline_write
;
1350 unsigned int needs_clflush
;
1351 unsigned int offset
, idx
;
1354 ret
= mutex_lock_interruptible(&i915
->drm
.struct_mutex
);
1358 ret
= i915_gem_obj_prepare_shmem_write(obj
, &needs_clflush
);
1359 mutex_unlock(&i915
->drm
.struct_mutex
);
1363 obj_do_bit17_swizzling
= 0;
1364 if (i915_gem_object_needs_bit17_swizzle(obj
))
1365 obj_do_bit17_swizzling
= BIT(17);
1367 /* If we don't overwrite a cacheline completely we need to be
1368 * careful to have up-to-date data by first clflushing. Don't
1369 * overcomplicate things and flush the entire patch.
1371 partial_cacheline_write
= 0;
1372 if (needs_clflush
& CLFLUSH_BEFORE
)
1373 partial_cacheline_write
= boot_cpu_data
.x86_clflush_size
- 1;
1375 user_data
= u64_to_user_ptr(args
->data_ptr
);
1376 remain
= args
->size
;
1377 offset
= offset_in_page(args
->offset
);
1378 for (idx
= args
->offset
>> PAGE_SHIFT
; remain
; idx
++) {
1379 struct page
*page
= i915_gem_object_get_page(obj
, idx
);
1383 if (offset
+ length
> PAGE_SIZE
)
1384 length
= PAGE_SIZE
- offset
;
1386 ret
= shmem_pwrite(page
, offset
, length
, user_data
,
1387 page_to_phys(page
) & obj_do_bit17_swizzling
,
1388 (offset
| length
) & partial_cacheline_write
,
1389 needs_clflush
& CLFLUSH_AFTER
);
1394 user_data
+= length
;
1398 intel_fb_obj_flush(obj
, false, ORIGIN_CPU
);
1399 i915_gem_obj_finish_shmem_access(obj
);
1404 * Writes data to the object referenced by handle.
1406 * @data: ioctl data blob
1409 * On error, the contents of the buffer that were to be modified are undefined.
1412 i915_gem_pwrite_ioctl(struct drm_device
*dev
, void *data
,
1413 struct drm_file
*file
)
1415 struct drm_i915_gem_pwrite
*args
= data
;
1416 struct drm_i915_gem_object
*obj
;
1419 if (args
->size
== 0)
1422 if (!access_ok(VERIFY_READ
,
1423 u64_to_user_ptr(args
->data_ptr
),
1427 obj
= i915_gem_object_lookup(file
, args
->handle
);
1431 /* Bounds check destination. */
1432 if (range_overflows_t(u64
, args
->offset
, args
->size
, obj
->base
.size
)) {
1437 trace_i915_gem_object_pwrite(obj
, args
->offset
, args
->size
);
1439 ret
= i915_gem_object_wait(obj
,
1440 I915_WAIT_INTERRUPTIBLE
|
1442 MAX_SCHEDULE_TIMEOUT
,
1443 to_rps_client(file
));
1447 ret
= i915_gem_object_pin_pages(obj
);
1452 /* We can only do the GTT pwrite on untiled buffers, as otherwise
1453 * it would end up going through the fenced access, and we'll get
1454 * different detiling behavior between reading and writing.
1455 * pread/pwrite currently are reading and writing from the CPU
1456 * perspective, requiring manual detiling by the client.
1458 if (!i915_gem_object_has_struct_page(obj
) ||
1459 cpu_write_needs_clflush(obj
))
1460 /* Note that the gtt paths might fail with non-page-backed user
1461 * pointers (e.g. gtt mappings when moving data between
1462 * textures). Fallback to the shmem path in that case.
1464 ret
= i915_gem_gtt_pwrite_fast(obj
, args
);
1466 if (ret
== -EFAULT
|| ret
== -ENOSPC
) {
1467 if (obj
->phys_handle
)
1468 ret
= i915_gem_phys_pwrite(obj
, args
, file
);
1470 ret
= i915_gem_shmem_pwrite(obj
, args
);
1473 i915_gem_object_unpin_pages(obj
);
1475 i915_gem_object_put(obj
);
1479 static inline enum fb_op_origin
1480 write_origin(struct drm_i915_gem_object
*obj
, unsigned domain
)
1482 return (domain
== I915_GEM_DOMAIN_GTT
?
1483 obj
->frontbuffer_ggtt_origin
: ORIGIN_CPU
);
1486 static void i915_gem_object_bump_inactive_ggtt(struct drm_i915_gem_object
*obj
)
1488 struct drm_i915_private
*i915
;
1489 struct list_head
*list
;
1490 struct i915_vma
*vma
;
1492 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
) {
1493 if (!i915_vma_is_ggtt(vma
))
1496 if (i915_vma_is_active(vma
))
1499 if (!drm_mm_node_allocated(&vma
->node
))
1502 list_move_tail(&vma
->vm_link
, &vma
->vm
->inactive_list
);
1505 i915
= to_i915(obj
->base
.dev
);
1506 list
= obj
->bind_count
? &i915
->mm
.bound_list
: &i915
->mm
.unbound_list
;
1507 list_move_tail(&obj
->global_link
, list
);
1511 * Called when user space prepares to use an object with the CPU, either
1512 * through the mmap ioctl's mapping or a GTT mapping.
1514 * @data: ioctl data blob
1518 i915_gem_set_domain_ioctl(struct drm_device
*dev
, void *data
,
1519 struct drm_file
*file
)
1521 struct drm_i915_gem_set_domain
*args
= data
;
1522 struct drm_i915_gem_object
*obj
;
1523 uint32_t read_domains
= args
->read_domains
;
1524 uint32_t write_domain
= args
->write_domain
;
1527 /* Only handle setting domains to types used by the CPU. */
1528 if ((write_domain
| read_domains
) & I915_GEM_GPU_DOMAINS
)
1531 /* Having something in the write domain implies it's in the read
1532 * domain, and only that read domain. Enforce that in the request.
1534 if (write_domain
!= 0 && read_domains
!= write_domain
)
1537 obj
= i915_gem_object_lookup(file
, args
->handle
);
1541 /* Try to flush the object off the GPU without holding the lock.
1542 * We will repeat the flush holding the lock in the normal manner
1543 * to catch cases where we are gazumped.
1545 err
= i915_gem_object_wait(obj
,
1546 I915_WAIT_INTERRUPTIBLE
|
1547 (write_domain
? I915_WAIT_ALL
: 0),
1548 MAX_SCHEDULE_TIMEOUT
,
1549 to_rps_client(file
));
1553 /* Flush and acquire obj->pages so that we are coherent through
1554 * direct access in memory with previous cached writes through
1555 * shmemfs and that our cache domain tracking remains valid.
1556 * For example, if the obj->filp was moved to swap without us
1557 * being notified and releasing the pages, we would mistakenly
1558 * continue to assume that the obj remained out of the CPU cached
1561 err
= i915_gem_object_pin_pages(obj
);
1565 err
= i915_mutex_lock_interruptible(dev
);
1569 if (read_domains
& I915_GEM_DOMAIN_GTT
)
1570 err
= i915_gem_object_set_to_gtt_domain(obj
, write_domain
!= 0);
1572 err
= i915_gem_object_set_to_cpu_domain(obj
, write_domain
!= 0);
1574 /* And bump the LRU for this access */
1575 i915_gem_object_bump_inactive_ggtt(obj
);
1577 mutex_unlock(&dev
->struct_mutex
);
1579 if (write_domain
!= 0)
1580 intel_fb_obj_invalidate(obj
, write_origin(obj
, write_domain
));
1583 i915_gem_object_unpin_pages(obj
);
1585 i915_gem_object_put(obj
);
1590 * Called when user space has done writes to this buffer
1592 * @data: ioctl data blob
1596 i915_gem_sw_finish_ioctl(struct drm_device
*dev
, void *data
,
1597 struct drm_file
*file
)
1599 struct drm_i915_gem_sw_finish
*args
= data
;
1600 struct drm_i915_gem_object
*obj
;
1603 obj
= i915_gem_object_lookup(file
, args
->handle
);
1607 /* Pinned buffers may be scanout, so flush the cache */
1608 if (READ_ONCE(obj
->pin_display
)) {
1609 err
= i915_mutex_lock_interruptible(dev
);
1611 i915_gem_object_flush_cpu_write_domain(obj
);
1612 mutex_unlock(&dev
->struct_mutex
);
1616 i915_gem_object_put(obj
);
1621 * i915_gem_mmap_ioctl - Maps the contents of an object, returning the address
1624 * @data: ioctl data blob
1627 * While the mapping holds a reference on the contents of the object, it doesn't
1628 * imply a ref on the object itself.
1632 * DRM driver writers who look a this function as an example for how to do GEM
1633 * mmap support, please don't implement mmap support like here. The modern way
1634 * to implement DRM mmap support is with an mmap offset ioctl (like
1635 * i915_gem_mmap_gtt) and then using the mmap syscall on the DRM fd directly.
1636 * That way debug tooling like valgrind will understand what's going on, hiding
1637 * the mmap call in a driver private ioctl will break that. The i915 driver only
1638 * does cpu mmaps this way because we didn't know better.
1641 i915_gem_mmap_ioctl(struct drm_device
*dev
, void *data
,
1642 struct drm_file
*file
)
1644 struct drm_i915_gem_mmap
*args
= data
;
1645 struct drm_i915_gem_object
*obj
;
1648 if (args
->flags
& ~(I915_MMAP_WC
))
1651 if (args
->flags
& I915_MMAP_WC
&& !boot_cpu_has(X86_FEATURE_PAT
))
1654 obj
= i915_gem_object_lookup(file
, args
->handle
);
1658 /* prime objects have no backing filp to GEM mmap
1661 if (!obj
->base
.filp
) {
1662 i915_gem_object_put(obj
);
1666 addr
= vm_mmap(obj
->base
.filp
, 0, args
->size
,
1667 PROT_READ
| PROT_WRITE
, MAP_SHARED
,
1669 if (args
->flags
& I915_MMAP_WC
) {
1670 struct mm_struct
*mm
= current
->mm
;
1671 struct vm_area_struct
*vma
;
1673 if (down_write_killable(&mm
->mmap_sem
)) {
1674 i915_gem_object_put(obj
);
1677 vma
= find_vma(mm
, addr
);
1680 pgprot_writecombine(vm_get_page_prot(vma
->vm_flags
));
1683 up_write(&mm
->mmap_sem
);
1685 /* This may race, but that's ok, it only gets set */
1686 WRITE_ONCE(obj
->frontbuffer_ggtt_origin
, ORIGIN_CPU
);
1688 i915_gem_object_put(obj
);
1689 if (IS_ERR((void *)addr
))
1692 args
->addr_ptr
= (uint64_t) addr
;
1697 static unsigned int tile_row_pages(struct drm_i915_gem_object
*obj
)
1699 return i915_gem_object_get_tile_row_size(obj
) >> PAGE_SHIFT
;
1703 * i915_gem_mmap_gtt_version - report the current feature set for GTT mmaps
1705 * A history of the GTT mmap interface:
1707 * 0 - Everything had to fit into the GTT. Both parties of a memcpy had to
1708 * aligned and suitable for fencing, and still fit into the available
1709 * mappable space left by the pinned display objects. A classic problem
1710 * we called the page-fault-of-doom where we would ping-pong between
1711 * two objects that could not fit inside the GTT and so the memcpy
1712 * would page one object in at the expense of the other between every
1715 * 1 - Objects can be any size, and have any compatible fencing (X Y, or none
1716 * as set via i915_gem_set_tiling() [DRM_I915_GEM_SET_TILING]). If the
1717 * object is too large for the available space (or simply too large
1718 * for the mappable aperture!), a view is created instead and faulted
1719 * into userspace. (This view is aligned and sized appropriately for
1724 * * snoopable objects cannot be accessed via the GTT. It can cause machine
1725 * hangs on some architectures, corruption on others. An attempt to service
1726 * a GTT page fault from a snoopable object will generate a SIGBUS.
1728 * * the object must be able to fit into RAM (physical memory, though no
1729 * limited to the mappable aperture).
1734 * * a new GTT page fault will synchronize rendering from the GPU and flush
1735 * all data to system memory. Subsequent access will not be synchronized.
1737 * * all mappings are revoked on runtime device suspend.
1739 * * there are only 8, 16 or 32 fence registers to share between all users
1740 * (older machines require fence register for display and blitter access
1741 * as well). Contention of the fence registers will cause the previous users
1742 * to be unmapped and any new access will generate new page faults.
1744 * * running out of memory while servicing a fault may generate a SIGBUS,
1745 * rather than the expected SIGSEGV.
1747 int i915_gem_mmap_gtt_version(void)
1752 static inline struct i915_ggtt_view
1753 compute_partial_view(struct drm_i915_gem_object
*obj
,
1754 pgoff_t page_offset
,
1757 struct i915_ggtt_view view
;
1759 if (i915_gem_object_is_tiled(obj
))
1760 chunk
= roundup(chunk
, tile_row_pages(obj
));
1762 view
.type
= I915_GGTT_VIEW_PARTIAL
;
1763 view
.partial
.offset
= rounddown(page_offset
, chunk
);
1765 min_t(unsigned int, chunk
,
1766 (obj
->base
.size
>> PAGE_SHIFT
) - view
.partial
.offset
);
1768 /* If the partial covers the entire object, just create a normal VMA. */
1769 if (chunk
>= obj
->base
.size
>> PAGE_SHIFT
)
1770 view
.type
= I915_GGTT_VIEW_NORMAL
;
1776 * i915_gem_fault - fault a page into the GTT
1777 * @area: CPU VMA in question
1780 * The fault handler is set up by drm_gem_mmap() when a object is GTT mapped
1781 * from userspace. The fault handler takes care of binding the object to
1782 * the GTT (if needed), allocating and programming a fence register (again,
1783 * only if needed based on whether the old reg is still valid or the object
1784 * is tiled) and inserting a new PTE into the faulting process.
1786 * Note that the faulting process may involve evicting existing objects
1787 * from the GTT and/or fence registers to make room. So performance may
1788 * suffer if the GTT working set is large or there are few fence registers
1791 * The current feature set supported by i915_gem_fault() and thus GTT mmaps
1792 * is exposed via I915_PARAM_MMAP_GTT_VERSION (see i915_gem_mmap_gtt_version).
1794 int i915_gem_fault(struct vm_area_struct
*area
, struct vm_fault
*vmf
)
1796 #define MIN_CHUNK_PAGES ((1 << 20) >> PAGE_SHIFT) /* 1 MiB */
1797 struct drm_i915_gem_object
*obj
= to_intel_bo(area
->vm_private_data
);
1798 struct drm_device
*dev
= obj
->base
.dev
;
1799 struct drm_i915_private
*dev_priv
= to_i915(dev
);
1800 struct i915_ggtt
*ggtt
= &dev_priv
->ggtt
;
1801 bool write
= !!(vmf
->flags
& FAULT_FLAG_WRITE
);
1802 struct i915_vma
*vma
;
1803 pgoff_t page_offset
;
1807 /* We don't use vmf->pgoff since that has the fake offset */
1808 page_offset
= (vmf
->address
- area
->vm_start
) >> PAGE_SHIFT
;
1810 trace_i915_gem_object_fault(obj
, page_offset
, true, write
);
1812 /* Try to flush the object off the GPU first without holding the lock.
1813 * Upon acquiring the lock, we will perform our sanity checks and then
1814 * repeat the flush holding the lock in the normal manner to catch cases
1815 * where we are gazumped.
1817 ret
= i915_gem_object_wait(obj
,
1818 I915_WAIT_INTERRUPTIBLE
,
1819 MAX_SCHEDULE_TIMEOUT
,
1824 ret
= i915_gem_object_pin_pages(obj
);
1828 intel_runtime_pm_get(dev_priv
);
1830 ret
= i915_mutex_lock_interruptible(dev
);
1834 /* Access to snoopable pages through the GTT is incoherent. */
1835 if (obj
->cache_level
!= I915_CACHE_NONE
&& !HAS_LLC(dev_priv
)) {
1840 /* If the object is smaller than a couple of partial vma, it is
1841 * not worth only creating a single partial vma - we may as well
1842 * clear enough space for the full object.
1844 flags
= PIN_MAPPABLE
;
1845 if (obj
->base
.size
> 2 * MIN_CHUNK_PAGES
<< PAGE_SHIFT
)
1846 flags
|= PIN_NONBLOCK
| PIN_NONFAULT
;
1848 /* Now pin it into the GTT as needed */
1849 vma
= i915_gem_object_ggtt_pin(obj
, NULL
, 0, 0, flags
);
1851 /* Use a partial view if it is bigger than available space */
1852 struct i915_ggtt_view view
=
1853 compute_partial_view(obj
, page_offset
, MIN_CHUNK_PAGES
);
1855 /* Userspace is now writing through an untracked VMA, abandon
1856 * all hope that the hardware is able to track future writes.
1858 obj
->frontbuffer_ggtt_origin
= ORIGIN_CPU
;
1860 vma
= i915_gem_object_ggtt_pin(obj
, &view
, 0, 0, PIN_MAPPABLE
);
1867 ret
= i915_gem_object_set_to_gtt_domain(obj
, write
);
1871 ret
= i915_vma_get_fence(vma
);
1875 /* Mark as being mmapped into userspace for later revocation */
1876 assert_rpm_wakelock_held(dev_priv
);
1877 if (list_empty(&obj
->userfault_link
))
1878 list_add(&obj
->userfault_link
, &dev_priv
->mm
.userfault_list
);
1880 /* Finally, remap it using the new GTT offset */
1881 ret
= remap_io_mapping(area
,
1882 area
->vm_start
+ (vma
->ggtt_view
.partial
.offset
<< PAGE_SHIFT
),
1883 (ggtt
->mappable_base
+ vma
->node
.start
) >> PAGE_SHIFT
,
1884 min_t(u64
, vma
->size
, area
->vm_end
- area
->vm_start
),
1888 __i915_vma_unpin(vma
);
1890 mutex_unlock(&dev
->struct_mutex
);
1892 intel_runtime_pm_put(dev_priv
);
1893 i915_gem_object_unpin_pages(obj
);
1898 * We eat errors when the gpu is terminally wedged to avoid
1899 * userspace unduly crashing (gl has no provisions for mmaps to
1900 * fail). But any other -EIO isn't ours (e.g. swap in failure)
1901 * and so needs to be reported.
1903 if (!i915_terminally_wedged(&dev_priv
->gpu_error
)) {
1904 ret
= VM_FAULT_SIGBUS
;
1909 * EAGAIN means the gpu is hung and we'll wait for the error
1910 * handler to reset everything when re-faulting in
1911 * i915_mutex_lock_interruptible.
1918 * EBUSY is ok: this just means that another thread
1919 * already did the job.
1921 ret
= VM_FAULT_NOPAGE
;
1928 ret
= VM_FAULT_SIGBUS
;
1931 WARN_ONCE(ret
, "unhandled error in i915_gem_fault: %i\n", ret
);
1932 ret
= VM_FAULT_SIGBUS
;
1939 * i915_gem_release_mmap - remove physical page mappings
1940 * @obj: obj in question
1942 * Preserve the reservation of the mmapping with the DRM core code, but
1943 * relinquish ownership of the pages back to the system.
1945 * It is vital that we remove the page mapping if we have mapped a tiled
1946 * object through the GTT and then lose the fence register due to
1947 * resource pressure. Similarly if the object has been moved out of the
1948 * aperture, than pages mapped into userspace must be revoked. Removing the
1949 * mapping will then trigger a page fault on the next user access, allowing
1950 * fixup by i915_gem_fault().
1953 i915_gem_release_mmap(struct drm_i915_gem_object
*obj
)
1955 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
1957 /* Serialisation between user GTT access and our code depends upon
1958 * revoking the CPU's PTE whilst the mutex is held. The next user
1959 * pagefault then has to wait until we release the mutex.
1961 * Note that RPM complicates somewhat by adding an additional
1962 * requirement that operations to the GGTT be made holding the RPM
1965 lockdep_assert_held(&i915
->drm
.struct_mutex
);
1966 intel_runtime_pm_get(i915
);
1968 if (list_empty(&obj
->userfault_link
))
1971 list_del_init(&obj
->userfault_link
);
1972 drm_vma_node_unmap(&obj
->base
.vma_node
,
1973 obj
->base
.dev
->anon_inode
->i_mapping
);
1975 /* Ensure that the CPU's PTE are revoked and there are not outstanding
1976 * memory transactions from userspace before we return. The TLB
1977 * flushing implied above by changing the PTE above *should* be
1978 * sufficient, an extra barrier here just provides us with a bit
1979 * of paranoid documentation about our requirement to serialise
1980 * memory writes before touching registers / GSM.
1985 intel_runtime_pm_put(i915
);
1988 void i915_gem_runtime_suspend(struct drm_i915_private
*dev_priv
)
1990 struct drm_i915_gem_object
*obj
, *on
;
1994 * Only called during RPM suspend. All users of the userfault_list
1995 * must be holding an RPM wakeref to ensure that this can not
1996 * run concurrently with themselves (and use the struct_mutex for
1997 * protection between themselves).
2000 list_for_each_entry_safe(obj
, on
,
2001 &dev_priv
->mm
.userfault_list
, userfault_link
) {
2002 list_del_init(&obj
->userfault_link
);
2003 drm_vma_node_unmap(&obj
->base
.vma_node
,
2004 obj
->base
.dev
->anon_inode
->i_mapping
);
2007 /* The fence will be lost when the device powers down. If any were
2008 * in use by hardware (i.e. they are pinned), we should not be powering
2009 * down! All other fences will be reacquired by the user upon waking.
2011 for (i
= 0; i
< dev_priv
->num_fence_regs
; i
++) {
2012 struct drm_i915_fence_reg
*reg
= &dev_priv
->fence_regs
[i
];
2014 if (WARN_ON(reg
->pin_count
))
2020 GEM_BUG_ON(!list_empty(®
->vma
->obj
->userfault_link
));
2025 static int i915_gem_object_create_mmap_offset(struct drm_i915_gem_object
*obj
)
2027 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
2030 err
= drm_gem_create_mmap_offset(&obj
->base
);
2034 /* Attempt to reap some mmap space from dead objects */
2036 err
= i915_gem_wait_for_idle(dev_priv
, I915_WAIT_INTERRUPTIBLE
);
2040 i915_gem_drain_freed_objects(dev_priv
);
2041 err
= drm_gem_create_mmap_offset(&obj
->base
);
2045 } while (flush_delayed_work(&dev_priv
->gt
.retire_work
));
2050 static void i915_gem_object_free_mmap_offset(struct drm_i915_gem_object
*obj
)
2052 drm_gem_free_mmap_offset(&obj
->base
);
2056 i915_gem_mmap_gtt(struct drm_file
*file
,
2057 struct drm_device
*dev
,
2061 struct drm_i915_gem_object
*obj
;
2064 obj
= i915_gem_object_lookup(file
, handle
);
2068 ret
= i915_gem_object_create_mmap_offset(obj
);
2070 *offset
= drm_vma_node_offset_addr(&obj
->base
.vma_node
);
2072 i915_gem_object_put(obj
);
2077 * i915_gem_mmap_gtt_ioctl - prepare an object for GTT mmap'ing
2079 * @data: GTT mapping ioctl data
2080 * @file: GEM object info
2082 * Simply returns the fake offset to userspace so it can mmap it.
2083 * The mmap call will end up in drm_gem_mmap(), which will set things
2084 * up so we can get faults in the handler above.
2086 * The fault handler will take care of binding the object into the GTT
2087 * (since it may have been evicted to make room for something), allocating
2088 * a fence register, and mapping the appropriate aperture address into
2092 i915_gem_mmap_gtt_ioctl(struct drm_device
*dev
, void *data
,
2093 struct drm_file
*file
)
2095 struct drm_i915_gem_mmap_gtt
*args
= data
;
2097 return i915_gem_mmap_gtt(file
, dev
, args
->handle
, &args
->offset
);
2100 /* Immediately discard the backing storage */
2102 i915_gem_object_truncate(struct drm_i915_gem_object
*obj
)
2104 i915_gem_object_free_mmap_offset(obj
);
2106 if (obj
->base
.filp
== NULL
)
2109 /* Our goal here is to return as much of the memory as
2110 * is possible back to the system as we are called from OOM.
2111 * To do this we must instruct the shmfs to drop all of its
2112 * backing pages, *now*.
2114 shmem_truncate_range(file_inode(obj
->base
.filp
), 0, (loff_t
)-1);
2115 obj
->mm
.madv
= __I915_MADV_PURGED
;
2118 /* Try to discard unwanted pages */
2119 void __i915_gem_object_invalidate(struct drm_i915_gem_object
*obj
)
2121 struct address_space
*mapping
;
2123 lockdep_assert_held(&obj
->mm
.lock
);
2124 GEM_BUG_ON(obj
->mm
.pages
);
2126 switch (obj
->mm
.madv
) {
2127 case I915_MADV_DONTNEED
:
2128 i915_gem_object_truncate(obj
);
2129 case __I915_MADV_PURGED
:
2133 if (obj
->base
.filp
== NULL
)
2136 mapping
= obj
->base
.filp
->f_mapping
,
2137 invalidate_mapping_pages(mapping
, 0, (loff_t
)-1);
2141 i915_gem_object_put_pages_gtt(struct drm_i915_gem_object
*obj
,
2142 struct sg_table
*pages
)
2144 struct sgt_iter sgt_iter
;
2147 __i915_gem_object_release_shmem(obj
, pages
, true);
2149 i915_gem_gtt_finish_pages(obj
, pages
);
2151 if (i915_gem_object_needs_bit17_swizzle(obj
))
2152 i915_gem_object_save_bit_17_swizzle(obj
, pages
);
2154 for_each_sgt_page(page
, sgt_iter
, pages
) {
2156 set_page_dirty(page
);
2158 if (obj
->mm
.madv
== I915_MADV_WILLNEED
)
2159 mark_page_accessed(page
);
2163 obj
->mm
.dirty
= false;
2165 sg_free_table(pages
);
2169 static void __i915_gem_object_reset_page_iter(struct drm_i915_gem_object
*obj
)
2171 struct radix_tree_iter iter
;
2174 radix_tree_for_each_slot(slot
, &obj
->mm
.get_page
.radix
, &iter
, 0)
2175 radix_tree_delete(&obj
->mm
.get_page
.radix
, iter
.index
);
2178 void __i915_gem_object_put_pages(struct drm_i915_gem_object
*obj
,
2179 enum i915_mm_subclass subclass
)
2181 struct sg_table
*pages
;
2183 if (i915_gem_object_has_pinned_pages(obj
))
2186 GEM_BUG_ON(obj
->bind_count
);
2187 if (!READ_ONCE(obj
->mm
.pages
))
2190 /* May be called by shrinker from within get_pages() (on another bo) */
2191 mutex_lock_nested(&obj
->mm
.lock
, subclass
);
2192 if (unlikely(atomic_read(&obj
->mm
.pages_pin_count
)))
2195 /* ->put_pages might need to allocate memory for the bit17 swizzle
2196 * array, hence protect them from being reaped by removing them from gtt
2198 pages
= fetch_and_zero(&obj
->mm
.pages
);
2201 if (obj
->mm
.mapping
) {
2204 ptr
= ptr_mask_bits(obj
->mm
.mapping
);
2205 if (is_vmalloc_addr(ptr
))
2208 kunmap(kmap_to_page(ptr
));
2210 obj
->mm
.mapping
= NULL
;
2213 __i915_gem_object_reset_page_iter(obj
);
2215 obj
->ops
->put_pages(obj
, pages
);
2217 mutex_unlock(&obj
->mm
.lock
);
2220 static void i915_sg_trim(struct sg_table
*orig_st
)
2222 struct sg_table new_st
;
2223 struct scatterlist
*sg
, *new_sg
;
2226 if (orig_st
->nents
== orig_st
->orig_nents
)
2229 if (sg_alloc_table(&new_st
, orig_st
->nents
, GFP_KERNEL
| __GFP_NOWARN
))
2232 new_sg
= new_st
.sgl
;
2233 for_each_sg(orig_st
->sgl
, sg
, orig_st
->nents
, i
) {
2234 sg_set_page(new_sg
, sg_page(sg
), sg
->length
, 0);
2235 /* called before being DMA mapped, no need to copy sg->dma_* */
2236 new_sg
= sg_next(new_sg
);
2238 GEM_BUG_ON(new_sg
); /* Should walk exactly nents and hit the end */
2240 sg_free_table(orig_st
);
2245 static struct sg_table
*
2246 i915_gem_object_get_pages_gtt(struct drm_i915_gem_object
*obj
)
2248 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
2249 const unsigned long page_count
= obj
->base
.size
/ PAGE_SIZE
;
2251 struct address_space
*mapping
;
2252 struct sg_table
*st
;
2253 struct scatterlist
*sg
;
2254 struct sgt_iter sgt_iter
;
2256 unsigned long last_pfn
= 0; /* suppress gcc warning */
2257 unsigned int max_segment
;
2261 /* Assert that the object is not currently in any GPU domain. As it
2262 * wasn't in the GTT, there shouldn't be any way it could have been in
2265 GEM_BUG_ON(obj
->base
.read_domains
& I915_GEM_GPU_DOMAINS
);
2266 GEM_BUG_ON(obj
->base
.write_domain
& I915_GEM_GPU_DOMAINS
);
2268 max_segment
= swiotlb_max_segment();
2270 max_segment
= rounddown(UINT_MAX
, PAGE_SIZE
);
2272 st
= kmalloc(sizeof(*st
), GFP_KERNEL
);
2274 return ERR_PTR(-ENOMEM
);
2277 if (sg_alloc_table(st
, page_count
, GFP_KERNEL
)) {
2279 return ERR_PTR(-ENOMEM
);
2282 /* Get the list of pages out of our struct file. They'll be pinned
2283 * at this point until we release them.
2285 * Fail silently without starting the shrinker
2287 mapping
= obj
->base
.filp
->f_mapping
;
2288 gfp
= mapping_gfp_constraint(mapping
, ~(__GFP_IO
| __GFP_RECLAIM
));
2289 gfp
|= __GFP_NORETRY
| __GFP_NOWARN
;
2292 for (i
= 0; i
< page_count
; i
++) {
2293 page
= shmem_read_mapping_page_gfp(mapping
, i
, gfp
);
2295 i915_gem_shrink(dev_priv
,
2298 I915_SHRINK_UNBOUND
|
2299 I915_SHRINK_PURGEABLE
);
2300 page
= shmem_read_mapping_page_gfp(mapping
, i
, gfp
);
2303 /* We've tried hard to allocate the memory by reaping
2304 * our own buffer, now let the real VM do its job and
2305 * go down in flames if truly OOM.
2307 page
= shmem_read_mapping_page(mapping
, i
);
2309 ret
= PTR_ERR(page
);
2314 sg
->length
>= max_segment
||
2315 page_to_pfn(page
) != last_pfn
+ 1) {
2319 sg_set_page(sg
, page
, PAGE_SIZE
, 0);
2321 sg
->length
+= PAGE_SIZE
;
2323 last_pfn
= page_to_pfn(page
);
2325 /* Check that the i965g/gm workaround works. */
2326 WARN_ON((gfp
& __GFP_DMA32
) && (last_pfn
>= 0x00100000UL
));
2328 if (sg
) /* loop terminated early; short sg table */
2331 /* Trim unused sg entries to avoid wasting memory. */
2334 ret
= i915_gem_gtt_prepare_pages(obj
, st
);
2336 /* DMA remapping failed? One possible cause is that
2337 * it could not reserve enough large entries, asking
2338 * for PAGE_SIZE chunks instead may be helpful.
2340 if (max_segment
> PAGE_SIZE
) {
2341 for_each_sgt_page(page
, sgt_iter
, st
)
2345 max_segment
= PAGE_SIZE
;
2348 dev_warn(&dev_priv
->drm
.pdev
->dev
,
2349 "Failed to DMA remap %lu pages\n",
2355 if (i915_gem_object_needs_bit17_swizzle(obj
))
2356 i915_gem_object_do_bit_17_swizzle(obj
, st
);
2363 for_each_sgt_page(page
, sgt_iter
, st
)
2368 /* shmemfs first checks if there is enough memory to allocate the page
2369 * and reports ENOSPC should there be insufficient, along with the usual
2370 * ENOMEM for a genuine allocation failure.
2372 * We use ENOSPC in our driver to mean that we have run out of aperture
2373 * space and so want to translate the error from shmemfs back to our
2374 * usual understanding of ENOMEM.
2379 return ERR_PTR(ret
);
2382 void __i915_gem_object_set_pages(struct drm_i915_gem_object
*obj
,
2383 struct sg_table
*pages
)
2385 lockdep_assert_held(&obj
->mm
.lock
);
2387 obj
->mm
.get_page
.sg_pos
= pages
->sgl
;
2388 obj
->mm
.get_page
.sg_idx
= 0;
2390 obj
->mm
.pages
= pages
;
2392 if (i915_gem_object_is_tiled(obj
) &&
2393 to_i915(obj
->base
.dev
)->quirks
& QUIRK_PIN_SWIZZLED_PAGES
) {
2394 GEM_BUG_ON(obj
->mm
.quirked
);
2395 __i915_gem_object_pin_pages(obj
);
2396 obj
->mm
.quirked
= true;
2400 static int ____i915_gem_object_get_pages(struct drm_i915_gem_object
*obj
)
2402 struct sg_table
*pages
;
2404 GEM_BUG_ON(i915_gem_object_has_pinned_pages(obj
));
2406 if (unlikely(obj
->mm
.madv
!= I915_MADV_WILLNEED
)) {
2407 DRM_DEBUG("Attempting to obtain a purgeable object\n");
2411 pages
= obj
->ops
->get_pages(obj
);
2412 if (unlikely(IS_ERR(pages
)))
2413 return PTR_ERR(pages
);
2415 __i915_gem_object_set_pages(obj
, pages
);
2419 /* Ensure that the associated pages are gathered from the backing storage
2420 * and pinned into our object. i915_gem_object_pin_pages() may be called
2421 * multiple times before they are released by a single call to
2422 * i915_gem_object_unpin_pages() - once the pages are no longer referenced
2423 * either as a result of memory pressure (reaping pages under the shrinker)
2424 * or as the object is itself released.
2426 int __i915_gem_object_get_pages(struct drm_i915_gem_object
*obj
)
2430 err
= mutex_lock_interruptible(&obj
->mm
.lock
);
2434 if (unlikely(!obj
->mm
.pages
)) {
2435 err
= ____i915_gem_object_get_pages(obj
);
2439 smp_mb__before_atomic();
2441 atomic_inc(&obj
->mm
.pages_pin_count
);
2444 mutex_unlock(&obj
->mm
.lock
);
2448 /* The 'mapping' part of i915_gem_object_pin_map() below */
2449 static void *i915_gem_object_map(const struct drm_i915_gem_object
*obj
,
2450 enum i915_map_type type
)
2452 unsigned long n_pages
= obj
->base
.size
>> PAGE_SHIFT
;
2453 struct sg_table
*sgt
= obj
->mm
.pages
;
2454 struct sgt_iter sgt_iter
;
2456 struct page
*stack_pages
[32];
2457 struct page
**pages
= stack_pages
;
2458 unsigned long i
= 0;
2462 /* A single page can always be kmapped */
2463 if (n_pages
== 1 && type
== I915_MAP_WB
)
2464 return kmap(sg_page(sgt
->sgl
));
2466 if (n_pages
> ARRAY_SIZE(stack_pages
)) {
2467 /* Too big for stack -- allocate temporary array instead */
2468 pages
= drm_malloc_gfp(n_pages
, sizeof(*pages
), GFP_TEMPORARY
);
2473 for_each_sgt_page(page
, sgt_iter
, sgt
)
2476 /* Check that we have the expected number of pages */
2477 GEM_BUG_ON(i
!= n_pages
);
2481 pgprot
= PAGE_KERNEL
;
2484 pgprot
= pgprot_writecombine(PAGE_KERNEL_IO
);
2487 addr
= vmap(pages
, n_pages
, 0, pgprot
);
2489 if (pages
!= stack_pages
)
2490 drm_free_large(pages
);
2495 /* get, pin, and map the pages of the object into kernel space */
2496 void *i915_gem_object_pin_map(struct drm_i915_gem_object
*obj
,
2497 enum i915_map_type type
)
2499 enum i915_map_type has_type
;
2504 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj
));
2506 ret
= mutex_lock_interruptible(&obj
->mm
.lock
);
2508 return ERR_PTR(ret
);
2511 if (!atomic_inc_not_zero(&obj
->mm
.pages_pin_count
)) {
2512 if (unlikely(!obj
->mm
.pages
)) {
2513 ret
= ____i915_gem_object_get_pages(obj
);
2517 smp_mb__before_atomic();
2519 atomic_inc(&obj
->mm
.pages_pin_count
);
2522 GEM_BUG_ON(!obj
->mm
.pages
);
2524 ptr
= ptr_unpack_bits(obj
->mm
.mapping
, has_type
);
2525 if (ptr
&& has_type
!= type
) {
2531 if (is_vmalloc_addr(ptr
))
2534 kunmap(kmap_to_page(ptr
));
2536 ptr
= obj
->mm
.mapping
= NULL
;
2540 ptr
= i915_gem_object_map(obj
, type
);
2546 obj
->mm
.mapping
= ptr_pack_bits(ptr
, type
);
2550 mutex_unlock(&obj
->mm
.lock
);
2554 atomic_dec(&obj
->mm
.pages_pin_count
);
2560 static bool ban_context(const struct i915_gem_context
*ctx
)
2562 return (i915_gem_context_is_bannable(ctx
) &&
2563 ctx
->ban_score
>= CONTEXT_SCORE_BAN_THRESHOLD
);
2566 static void i915_gem_context_mark_guilty(struct i915_gem_context
*ctx
)
2568 ctx
->guilty_count
++;
2569 ctx
->ban_score
+= CONTEXT_SCORE_GUILTY
;
2570 if (ban_context(ctx
))
2571 i915_gem_context_set_banned(ctx
);
2573 DRM_DEBUG_DRIVER("context %s marked guilty (score %d) banned? %s\n",
2574 ctx
->name
, ctx
->ban_score
,
2575 yesno(i915_gem_context_is_banned(ctx
)));
2577 if (!i915_gem_context_is_banned(ctx
) || IS_ERR_OR_NULL(ctx
->file_priv
))
2580 ctx
->file_priv
->context_bans
++;
2581 DRM_DEBUG_DRIVER("client %s has had %d context banned\n",
2582 ctx
->name
, ctx
->file_priv
->context_bans
);
2585 static void i915_gem_context_mark_innocent(struct i915_gem_context
*ctx
)
2587 ctx
->active_count
++;
2590 struct drm_i915_gem_request
*
2591 i915_gem_find_active_request(struct intel_engine_cs
*engine
)
2593 struct drm_i915_gem_request
*request
;
2595 /* We are called by the error capture and reset at a random
2596 * point in time. In particular, note that neither is crucially
2597 * ordered with an interrupt. After a hang, the GPU is dead and we
2598 * assume that no more writes can happen (we waited long enough for
2599 * all writes that were in transaction to be flushed) - adding an
2600 * extra delay for a recent interrupt is pointless. Hence, we do
2601 * not need an engine->irq_seqno_barrier() before the seqno reads.
2603 list_for_each_entry(request
, &engine
->timeline
->requests
, link
) {
2604 if (__i915_gem_request_completed(request
))
2607 GEM_BUG_ON(request
->engine
!= engine
);
2614 static bool engine_stalled(struct intel_engine_cs
*engine
)
2616 if (!engine
->hangcheck
.stalled
)
2619 /* Check for possible seqno movement after hang declaration */
2620 if (engine
->hangcheck
.seqno
!= intel_engine_get_seqno(engine
)) {
2621 DRM_DEBUG_DRIVER("%s pardoned\n", engine
->name
);
2628 int i915_gem_reset_prepare(struct drm_i915_private
*dev_priv
)
2630 struct intel_engine_cs
*engine
;
2631 enum intel_engine_id id
;
2634 /* Ensure irq handler finishes, and not run again. */
2635 for_each_engine(engine
, dev_priv
, id
) {
2636 struct drm_i915_gem_request
*request
;
2638 tasklet_kill(&engine
->irq_tasklet
);
2640 if (engine_stalled(engine
)) {
2641 request
= i915_gem_find_active_request(engine
);
2642 if (request
&& request
->fence
.error
== -EIO
)
2643 err
= -EIO
; /* Previous reset failed! */
2647 i915_gem_revoke_fences(dev_priv
);
2652 static void skip_request(struct drm_i915_gem_request
*request
)
2654 void *vaddr
= request
->ring
->vaddr
;
2657 /* As this request likely depends on state from the lost
2658 * context, clear out all the user operations leaving the
2659 * breadcrumb at the end (so we get the fence notifications).
2661 head
= request
->head
;
2662 if (request
->postfix
< head
) {
2663 memset(vaddr
+ head
, 0, request
->ring
->size
- head
);
2666 memset(vaddr
+ head
, 0, request
->postfix
- head
);
2668 dma_fence_set_error(&request
->fence
, -EIO
);
2671 static void engine_skip_context(struct drm_i915_gem_request
*request
)
2673 struct intel_engine_cs
*engine
= request
->engine
;
2674 struct i915_gem_context
*hung_ctx
= request
->ctx
;
2675 struct intel_timeline
*timeline
;
2676 unsigned long flags
;
2678 timeline
= i915_gem_context_lookup_timeline(hung_ctx
, engine
);
2680 spin_lock_irqsave(&engine
->timeline
->lock
, flags
);
2681 spin_lock(&timeline
->lock
);
2683 list_for_each_entry_continue(request
, &engine
->timeline
->requests
, link
)
2684 if (request
->ctx
== hung_ctx
)
2685 skip_request(request
);
2687 list_for_each_entry(request
, &timeline
->requests
, link
)
2688 skip_request(request
);
2690 spin_unlock(&timeline
->lock
);
2691 spin_unlock_irqrestore(&engine
->timeline
->lock
, flags
);
2694 /* Returns true if the request was guilty of hang */
2695 static bool i915_gem_reset_request(struct drm_i915_gem_request
*request
)
2697 /* Read once and return the resolution */
2698 const bool guilty
= engine_stalled(request
->engine
);
2700 /* The guilty request will get skipped on a hung engine.
2702 * Users of client default contexts do not rely on logical
2703 * state preserved between batches so it is safe to execute
2704 * queued requests following the hang. Non default contexts
2705 * rely on preserved state, so skipping a batch loses the
2706 * evolution of the state and it needs to be considered corrupted.
2707 * Executing more queued batches on top of corrupted state is
2708 * risky. But we take the risk by trying to advance through
2709 * the queued requests in order to make the client behaviour
2710 * more predictable around resets, by not throwing away random
2711 * amount of batches it has prepared for execution. Sophisticated
2712 * clients can use gem_reset_stats_ioctl and dma fence status
2713 * (exported via sync_file info ioctl on explicit fences) to observe
2714 * when it loses the context state and should rebuild accordingly.
2716 * The context ban, and ultimately the client ban, mechanism are safety
2717 * valves if client submission ends up resulting in nothing more than
2722 i915_gem_context_mark_guilty(request
->ctx
);
2723 skip_request(request
);
2725 i915_gem_context_mark_innocent(request
->ctx
);
2726 dma_fence_set_error(&request
->fence
, -EAGAIN
);
2732 static void i915_gem_reset_engine(struct intel_engine_cs
*engine
)
2734 struct drm_i915_gem_request
*request
;
2736 if (engine
->irq_seqno_barrier
)
2737 engine
->irq_seqno_barrier(engine
);
2739 request
= i915_gem_find_active_request(engine
);
2743 if (!i915_gem_reset_request(request
))
2746 DRM_DEBUG_DRIVER("resetting %s to restart from tail of request 0x%x\n",
2747 engine
->name
, request
->global_seqno
);
2749 /* Setup the CS to resume from the breadcrumb of the hung request */
2750 engine
->reset_hw(engine
, request
);
2752 /* If this context is now banned, skip all of its pending requests. */
2753 if (i915_gem_context_is_banned(request
->ctx
))
2754 engine_skip_context(request
);
2757 void i915_gem_reset_finish(struct drm_i915_private
*dev_priv
)
2759 struct intel_engine_cs
*engine
;
2760 enum intel_engine_id id
;
2762 lockdep_assert_held(&dev_priv
->drm
.struct_mutex
);
2764 i915_gem_retire_requests(dev_priv
);
2766 for_each_engine(engine
, dev_priv
, id
)
2767 i915_gem_reset_engine(engine
);
2769 i915_gem_restore_fences(dev_priv
);
2771 if (dev_priv
->gt
.awake
) {
2772 intel_sanitize_gt_powersave(dev_priv
);
2773 intel_enable_gt_powersave(dev_priv
);
2774 if (INTEL_GEN(dev_priv
) >= 6)
2775 gen6_rps_busy(dev_priv
);
2779 static void nop_submit_request(struct drm_i915_gem_request
*request
)
2781 dma_fence_set_error(&request
->fence
, -EIO
);
2782 i915_gem_request_submit(request
);
2783 intel_engine_init_global_seqno(request
->engine
, request
->global_seqno
);
2786 static void engine_set_wedged(struct intel_engine_cs
*engine
)
2788 struct drm_i915_gem_request
*request
;
2789 unsigned long flags
;
2791 /* We need to be sure that no thread is running the old callback as
2792 * we install the nop handler (otherwise we would submit a request
2793 * to hardware that will never complete). In order to prevent this
2794 * race, we wait until the machine is idle before making the swap
2795 * (using stop_machine()).
2797 engine
->submit_request
= nop_submit_request
;
2799 /* Mark all executing requests as skipped */
2800 spin_lock_irqsave(&engine
->timeline
->lock
, flags
);
2801 list_for_each_entry(request
, &engine
->timeline
->requests
, link
)
2802 dma_fence_set_error(&request
->fence
, -EIO
);
2803 spin_unlock_irqrestore(&engine
->timeline
->lock
, flags
);
2805 /* Mark all pending requests as complete so that any concurrent
2806 * (lockless) lookup doesn't try and wait upon the request as we
2809 intel_engine_init_global_seqno(engine
,
2810 intel_engine_last_submit(engine
));
2813 * Clear the execlists queue up before freeing the requests, as those
2814 * are the ones that keep the context and ringbuffer backing objects
2818 if (i915
.enable_execlists
) {
2819 unsigned long flags
;
2821 spin_lock_irqsave(&engine
->timeline
->lock
, flags
);
2823 i915_gem_request_put(engine
->execlist_port
[0].request
);
2824 i915_gem_request_put(engine
->execlist_port
[1].request
);
2825 memset(engine
->execlist_port
, 0, sizeof(engine
->execlist_port
));
2826 engine
->execlist_queue
= RB_ROOT
;
2827 engine
->execlist_first
= NULL
;
2829 spin_unlock_irqrestore(&engine
->timeline
->lock
, flags
);
2833 static int __i915_gem_set_wedged_BKL(void *data
)
2835 struct drm_i915_private
*i915
= data
;
2836 struct intel_engine_cs
*engine
;
2837 enum intel_engine_id id
;
2839 for_each_engine(engine
, i915
, id
)
2840 engine_set_wedged(engine
);
2845 void i915_gem_set_wedged(struct drm_i915_private
*dev_priv
)
2847 lockdep_assert_held(&dev_priv
->drm
.struct_mutex
);
2848 set_bit(I915_WEDGED
, &dev_priv
->gpu_error
.flags
);
2850 stop_machine(__i915_gem_set_wedged_BKL
, dev_priv
, NULL
);
2852 i915_gem_context_lost(dev_priv
);
2853 i915_gem_retire_requests(dev_priv
);
2855 mod_delayed_work(dev_priv
->wq
, &dev_priv
->gt
.idle_work
, 0);
2859 i915_gem_retire_work_handler(struct work_struct
*work
)
2861 struct drm_i915_private
*dev_priv
=
2862 container_of(work
, typeof(*dev_priv
), gt
.retire_work
.work
);
2863 struct drm_device
*dev
= &dev_priv
->drm
;
2865 /* Come back later if the device is busy... */
2866 if (mutex_trylock(&dev
->struct_mutex
)) {
2867 i915_gem_retire_requests(dev_priv
);
2868 mutex_unlock(&dev
->struct_mutex
);
2871 /* Keep the retire handler running until we are finally idle.
2872 * We do not need to do this test under locking as in the worst-case
2873 * we queue the retire worker once too often.
2875 if (READ_ONCE(dev_priv
->gt
.awake
)) {
2876 i915_queue_hangcheck(dev_priv
);
2877 queue_delayed_work(dev_priv
->wq
,
2878 &dev_priv
->gt
.retire_work
,
2879 round_jiffies_up_relative(HZ
));
2884 i915_gem_idle_work_handler(struct work_struct
*work
)
2886 struct drm_i915_private
*dev_priv
=
2887 container_of(work
, typeof(*dev_priv
), gt
.idle_work
.work
);
2888 struct drm_device
*dev
= &dev_priv
->drm
;
2889 struct intel_engine_cs
*engine
;
2890 enum intel_engine_id id
;
2891 bool rearm_hangcheck
;
2893 if (!READ_ONCE(dev_priv
->gt
.awake
))
2897 * Wait for last execlists context complete, but bail out in case a
2898 * new request is submitted.
2900 wait_for(READ_ONCE(dev_priv
->gt
.active_requests
) ||
2901 intel_execlists_idle(dev_priv
), 10);
2903 if (READ_ONCE(dev_priv
->gt
.active_requests
))
2907 cancel_delayed_work_sync(&dev_priv
->gpu_error
.hangcheck_work
);
2909 if (!mutex_trylock(&dev
->struct_mutex
)) {
2910 /* Currently busy, come back later */
2911 mod_delayed_work(dev_priv
->wq
,
2912 &dev_priv
->gt
.idle_work
,
2913 msecs_to_jiffies(50));
2918 * New request retired after this work handler started, extend active
2919 * period until next instance of the work.
2921 if (work_pending(work
))
2924 if (dev_priv
->gt
.active_requests
)
2927 if (wait_for(intel_execlists_idle(dev_priv
), 10))
2928 DRM_ERROR("Timeout waiting for engines to idle\n");
2930 for_each_engine(engine
, dev_priv
, id
)
2931 i915_gem_batch_pool_fini(&engine
->batch_pool
);
2933 GEM_BUG_ON(!dev_priv
->gt
.awake
);
2934 dev_priv
->gt
.awake
= false;
2935 rearm_hangcheck
= false;
2937 if (INTEL_GEN(dev_priv
) >= 6)
2938 gen6_rps_idle(dev_priv
);
2939 intel_runtime_pm_put(dev_priv
);
2941 mutex_unlock(&dev
->struct_mutex
);
2944 if (rearm_hangcheck
) {
2945 GEM_BUG_ON(!dev_priv
->gt
.awake
);
2946 i915_queue_hangcheck(dev_priv
);
2950 void i915_gem_close_object(struct drm_gem_object
*gem
, struct drm_file
*file
)
2952 struct drm_i915_gem_object
*obj
= to_intel_bo(gem
);
2953 struct drm_i915_file_private
*fpriv
= file
->driver_priv
;
2954 struct i915_vma
*vma
, *vn
;
2956 mutex_lock(&obj
->base
.dev
->struct_mutex
);
2957 list_for_each_entry_safe(vma
, vn
, &obj
->vma_list
, obj_link
)
2958 if (vma
->vm
->file
== fpriv
)
2959 i915_vma_close(vma
);
2961 if (i915_gem_object_is_active(obj
) &&
2962 !i915_gem_object_has_active_reference(obj
)) {
2963 i915_gem_object_set_active_reference(obj
);
2964 i915_gem_object_get(obj
);
2966 mutex_unlock(&obj
->base
.dev
->struct_mutex
);
2969 static unsigned long to_wait_timeout(s64 timeout_ns
)
2972 return MAX_SCHEDULE_TIMEOUT
;
2974 if (timeout_ns
== 0)
2977 return nsecs_to_jiffies_timeout(timeout_ns
);
2981 * i915_gem_wait_ioctl - implements DRM_IOCTL_I915_GEM_WAIT
2982 * @dev: drm device pointer
2983 * @data: ioctl data blob
2984 * @file: drm file pointer
2986 * Returns 0 if successful, else an error is returned with the remaining time in
2987 * the timeout parameter.
2988 * -ETIME: object is still busy after timeout
2989 * -ERESTARTSYS: signal interrupted the wait
2990 * -ENONENT: object doesn't exist
2991 * Also possible, but rare:
2992 * -EAGAIN: GPU wedged
2994 * -ENODEV: Internal IRQ fail
2995 * -E?: The add request failed
2997 * The wait ioctl with a timeout of 0 reimplements the busy ioctl. With any
2998 * non-zero timeout parameter the wait ioctl will wait for the given number of
2999 * nanoseconds on an object becoming unbusy. Since the wait itself does so
3000 * without holding struct_mutex the object may become re-busied before this
3001 * function completes. A similar but shorter * race condition exists in the busy
3005 i915_gem_wait_ioctl(struct drm_device
*dev
, void *data
, struct drm_file
*file
)
3007 struct drm_i915_gem_wait
*args
= data
;
3008 struct drm_i915_gem_object
*obj
;
3012 if (args
->flags
!= 0)
3015 obj
= i915_gem_object_lookup(file
, args
->bo_handle
);
3019 start
= ktime_get();
3021 ret
= i915_gem_object_wait(obj
,
3022 I915_WAIT_INTERRUPTIBLE
| I915_WAIT_ALL
,
3023 to_wait_timeout(args
->timeout_ns
),
3024 to_rps_client(file
));
3026 if (args
->timeout_ns
> 0) {
3027 args
->timeout_ns
-= ktime_to_ns(ktime_sub(ktime_get(), start
));
3028 if (args
->timeout_ns
< 0)
3029 args
->timeout_ns
= 0;
3032 i915_gem_object_put(obj
);
3036 static int wait_for_timeline(struct i915_gem_timeline
*tl
, unsigned int flags
)
3040 for (i
= 0; i
< ARRAY_SIZE(tl
->engine
); i
++) {
3041 ret
= i915_gem_active_wait(&tl
->engine
[i
].last_request
, flags
);
3049 int i915_gem_wait_for_idle(struct drm_i915_private
*i915
, unsigned int flags
)
3053 if (flags
& I915_WAIT_LOCKED
) {
3054 struct i915_gem_timeline
*tl
;
3056 lockdep_assert_held(&i915
->drm
.struct_mutex
);
3058 list_for_each_entry(tl
, &i915
->gt
.timelines
, link
) {
3059 ret
= wait_for_timeline(tl
, flags
);
3064 ret
= wait_for_timeline(&i915
->gt
.global_timeline
, flags
);
3072 void i915_gem_clflush_object(struct drm_i915_gem_object
*obj
,
3075 /* If we don't have a page list set up, then we're not pinned
3076 * to GPU, and we can ignore the cache flush because it'll happen
3077 * again at bind time.
3083 * Stolen memory is always coherent with the GPU as it is explicitly
3084 * marked as wc by the system, or the system is cache-coherent.
3086 if (obj
->stolen
|| obj
->phys_handle
)
3089 /* If the GPU is snooping the contents of the CPU cache,
3090 * we do not need to manually clear the CPU cache lines. However,
3091 * the caches are only snooped when the render cache is
3092 * flushed/invalidated. As we always have to emit invalidations
3093 * and flushes when moving into and out of the RENDER domain, correct
3094 * snooping behaviour occurs naturally as the result of our domain
3097 if (!force
&& cpu_cache_is_coherent(obj
->base
.dev
, obj
->cache_level
)) {
3098 obj
->cache_dirty
= true;
3102 trace_i915_gem_object_clflush(obj
);
3103 drm_clflush_sg(obj
->mm
.pages
);
3104 obj
->cache_dirty
= false;
3107 /** Flushes the GTT write domain for the object if it's dirty. */
3109 i915_gem_object_flush_gtt_write_domain(struct drm_i915_gem_object
*obj
)
3111 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
3113 if (obj
->base
.write_domain
!= I915_GEM_DOMAIN_GTT
)
3116 /* No actual flushing is required for the GTT write domain. Writes
3117 * to it "immediately" go to main memory as far as we know, so there's
3118 * no chipset flush. It also doesn't land in render cache.
3120 * However, we do have to enforce the order so that all writes through
3121 * the GTT land before any writes to the device, such as updates to
3124 * We also have to wait a bit for the writes to land from the GTT.
3125 * An uncached read (i.e. mmio) seems to be ideal for the round-trip
3126 * timing. This issue has only been observed when switching quickly
3127 * between GTT writes and CPU reads from inside the kernel on recent hw,
3128 * and it appears to only affect discrete GTT blocks (i.e. on LLC
3129 * system agents we cannot reproduce this behaviour).
3132 if (INTEL_GEN(dev_priv
) >= 6 && !HAS_LLC(dev_priv
))
3133 POSTING_READ(RING_ACTHD(dev_priv
->engine
[RCS
]->mmio_base
));
3135 intel_fb_obj_flush(obj
, false, write_origin(obj
, I915_GEM_DOMAIN_GTT
));
3137 obj
->base
.write_domain
= 0;
3138 trace_i915_gem_object_change_domain(obj
,
3139 obj
->base
.read_domains
,
3140 I915_GEM_DOMAIN_GTT
);
3143 /** Flushes the CPU write domain for the object if it's dirty. */
3145 i915_gem_object_flush_cpu_write_domain(struct drm_i915_gem_object
*obj
)
3147 if (obj
->base
.write_domain
!= I915_GEM_DOMAIN_CPU
)
3150 i915_gem_clflush_object(obj
, obj
->pin_display
);
3151 intel_fb_obj_flush(obj
, false, ORIGIN_CPU
);
3153 obj
->base
.write_domain
= 0;
3154 trace_i915_gem_object_change_domain(obj
,
3155 obj
->base
.read_domains
,
3156 I915_GEM_DOMAIN_CPU
);
3160 * Moves a single object to the GTT read, and possibly write domain.
3161 * @obj: object to act on
3162 * @write: ask for write access or read only
3164 * This function returns when the move is complete, including waiting on
3168 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object
*obj
, bool write
)
3170 uint32_t old_write_domain
, old_read_domains
;
3173 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
3175 ret
= i915_gem_object_wait(obj
,
3176 I915_WAIT_INTERRUPTIBLE
|
3178 (write
? I915_WAIT_ALL
: 0),
3179 MAX_SCHEDULE_TIMEOUT
,
3184 if (obj
->base
.write_domain
== I915_GEM_DOMAIN_GTT
)
3187 /* Flush and acquire obj->pages so that we are coherent through
3188 * direct access in memory with previous cached writes through
3189 * shmemfs and that our cache domain tracking remains valid.
3190 * For example, if the obj->filp was moved to swap without us
3191 * being notified and releasing the pages, we would mistakenly
3192 * continue to assume that the obj remained out of the CPU cached
3195 ret
= i915_gem_object_pin_pages(obj
);
3199 i915_gem_object_flush_cpu_write_domain(obj
);
3201 /* Serialise direct access to this object with the barriers for
3202 * coherent writes from the GPU, by effectively invalidating the
3203 * GTT domain upon first access.
3205 if ((obj
->base
.read_domains
& I915_GEM_DOMAIN_GTT
) == 0)
3208 old_write_domain
= obj
->base
.write_domain
;
3209 old_read_domains
= obj
->base
.read_domains
;
3211 /* It should now be out of any other write domains, and we can update
3212 * the domain values for our changes.
3214 GEM_BUG_ON((obj
->base
.write_domain
& ~I915_GEM_DOMAIN_GTT
) != 0);
3215 obj
->base
.read_domains
|= I915_GEM_DOMAIN_GTT
;
3217 obj
->base
.read_domains
= I915_GEM_DOMAIN_GTT
;
3218 obj
->base
.write_domain
= I915_GEM_DOMAIN_GTT
;
3219 obj
->mm
.dirty
= true;
3222 trace_i915_gem_object_change_domain(obj
,
3226 i915_gem_object_unpin_pages(obj
);
3231 * Changes the cache-level of an object across all VMA.
3232 * @obj: object to act on
3233 * @cache_level: new cache level to set for the object
3235 * After this function returns, the object will be in the new cache-level
3236 * across all GTT and the contents of the backing storage will be coherent,
3237 * with respect to the new cache-level. In order to keep the backing storage
3238 * coherent for all users, we only allow a single cache level to be set
3239 * globally on the object and prevent it from being changed whilst the
3240 * hardware is reading from the object. That is if the object is currently
3241 * on the scanout it will be set to uncached (or equivalent display
3242 * cache coherency) and all non-MOCS GPU access will also be uncached so
3243 * that all direct access to the scanout remains coherent.
3245 int i915_gem_object_set_cache_level(struct drm_i915_gem_object
*obj
,
3246 enum i915_cache_level cache_level
)
3248 struct i915_vma
*vma
;
3251 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
3253 if (obj
->cache_level
== cache_level
)
3256 /* Inspect the list of currently bound VMA and unbind any that would
3257 * be invalid given the new cache-level. This is principally to
3258 * catch the issue of the CS prefetch crossing page boundaries and
3259 * reading an invalid PTE on older architectures.
3262 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
) {
3263 if (!drm_mm_node_allocated(&vma
->node
))
3266 if (i915_vma_is_pinned(vma
)) {
3267 DRM_DEBUG("can not change the cache level of pinned objects\n");
3271 if (i915_gem_valid_gtt_space(vma
, cache_level
))
3274 ret
= i915_vma_unbind(vma
);
3278 /* As unbinding may affect other elements in the
3279 * obj->vma_list (due to side-effects from retiring
3280 * an active vma), play safe and restart the iterator.
3285 /* We can reuse the existing drm_mm nodes but need to change the
3286 * cache-level on the PTE. We could simply unbind them all and
3287 * rebind with the correct cache-level on next use. However since
3288 * we already have a valid slot, dma mapping, pages etc, we may as
3289 * rewrite the PTE in the belief that doing so tramples upon less
3290 * state and so involves less work.
3292 if (obj
->bind_count
) {
3293 /* Before we change the PTE, the GPU must not be accessing it.
3294 * If we wait upon the object, we know that all the bound
3295 * VMA are no longer active.
3297 ret
= i915_gem_object_wait(obj
,
3298 I915_WAIT_INTERRUPTIBLE
|
3301 MAX_SCHEDULE_TIMEOUT
,
3306 if (!HAS_LLC(to_i915(obj
->base
.dev
)) &&
3307 cache_level
!= I915_CACHE_NONE
) {
3308 /* Access to snoopable pages through the GTT is
3309 * incoherent and on some machines causes a hard
3310 * lockup. Relinquish the CPU mmaping to force
3311 * userspace to refault in the pages and we can
3312 * then double check if the GTT mapping is still
3313 * valid for that pointer access.
3315 i915_gem_release_mmap(obj
);
3317 /* As we no longer need a fence for GTT access,
3318 * we can relinquish it now (and so prevent having
3319 * to steal a fence from someone else on the next
3320 * fence request). Note GPU activity would have
3321 * dropped the fence as all snoopable access is
3322 * supposed to be linear.
3324 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
) {
3325 ret
= i915_vma_put_fence(vma
);
3330 /* We either have incoherent backing store and
3331 * so no GTT access or the architecture is fully
3332 * coherent. In such cases, existing GTT mmaps
3333 * ignore the cache bit in the PTE and we can
3334 * rewrite it without confusing the GPU or having
3335 * to force userspace to fault back in its mmaps.
3339 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
) {
3340 if (!drm_mm_node_allocated(&vma
->node
))
3343 ret
= i915_vma_bind(vma
, cache_level
, PIN_UPDATE
);
3349 if (obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
&&
3350 cpu_cache_is_coherent(obj
->base
.dev
, obj
->cache_level
))
3351 obj
->cache_dirty
= true;
3353 list_for_each_entry(vma
, &obj
->vma_list
, obj_link
)
3354 vma
->node
.color
= cache_level
;
3355 obj
->cache_level
= cache_level
;
3360 int i915_gem_get_caching_ioctl(struct drm_device
*dev
, void *data
,
3361 struct drm_file
*file
)
3363 struct drm_i915_gem_caching
*args
= data
;
3364 struct drm_i915_gem_object
*obj
;
3368 obj
= i915_gem_object_lookup_rcu(file
, args
->handle
);
3374 switch (obj
->cache_level
) {
3375 case I915_CACHE_LLC
:
3376 case I915_CACHE_L3_LLC
:
3377 args
->caching
= I915_CACHING_CACHED
;
3381 args
->caching
= I915_CACHING_DISPLAY
;
3385 args
->caching
= I915_CACHING_NONE
;
3393 int i915_gem_set_caching_ioctl(struct drm_device
*dev
, void *data
,
3394 struct drm_file
*file
)
3396 struct drm_i915_private
*i915
= to_i915(dev
);
3397 struct drm_i915_gem_caching
*args
= data
;
3398 struct drm_i915_gem_object
*obj
;
3399 enum i915_cache_level level
;
3402 switch (args
->caching
) {
3403 case I915_CACHING_NONE
:
3404 level
= I915_CACHE_NONE
;
3406 case I915_CACHING_CACHED
:
3408 * Due to a HW issue on BXT A stepping, GPU stores via a
3409 * snooped mapping may leave stale data in a corresponding CPU
3410 * cacheline, whereas normally such cachelines would get
3413 if (!HAS_LLC(i915
) && !HAS_SNOOP(i915
))
3416 level
= I915_CACHE_LLC
;
3418 case I915_CACHING_DISPLAY
:
3419 level
= HAS_WT(i915
) ? I915_CACHE_WT
: I915_CACHE_NONE
;
3425 obj
= i915_gem_object_lookup(file
, args
->handle
);
3429 if (obj
->cache_level
== level
)
3432 ret
= i915_gem_object_wait(obj
,
3433 I915_WAIT_INTERRUPTIBLE
,
3434 MAX_SCHEDULE_TIMEOUT
,
3435 to_rps_client(file
));
3439 ret
= i915_mutex_lock_interruptible(dev
);
3443 ret
= i915_gem_object_set_cache_level(obj
, level
);
3444 mutex_unlock(&dev
->struct_mutex
);
3447 i915_gem_object_put(obj
);
3452 * Prepare buffer for display plane (scanout, cursors, etc).
3453 * Can be called from an uninterruptible phase (modesetting) and allows
3454 * any flushes to be pipelined (for pageflips).
3457 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object
*obj
,
3459 const struct i915_ggtt_view
*view
)
3461 struct i915_vma
*vma
;
3462 u32 old_read_domains
, old_write_domain
;
3465 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
3467 /* Mark the pin_display early so that we account for the
3468 * display coherency whilst setting up the cache domains.
3472 /* The display engine is not coherent with the LLC cache on gen6. As
3473 * a result, we make sure that the pinning that is about to occur is
3474 * done with uncached PTEs. This is lowest common denominator for all
3477 * However for gen6+, we could do better by using the GFDT bit instead
3478 * of uncaching, which would allow us to flush all the LLC-cached data
3479 * with that bit in the PTE to main memory with just one PIPE_CONTROL.
3481 ret
= i915_gem_object_set_cache_level(obj
,
3482 HAS_WT(to_i915(obj
->base
.dev
)) ?
3483 I915_CACHE_WT
: I915_CACHE_NONE
);
3486 goto err_unpin_display
;
3489 /* As the user may map the buffer once pinned in the display plane
3490 * (e.g. libkms for the bootup splash), we have to ensure that we
3491 * always use map_and_fenceable for all scanout buffers. However,
3492 * it may simply be too big to fit into mappable, in which case
3493 * put it anyway and hope that userspace can cope (but always first
3494 * try to preserve the existing ABI).
3496 vma
= ERR_PTR(-ENOSPC
);
3497 if (!view
|| view
->type
== I915_GGTT_VIEW_NORMAL
)
3498 vma
= i915_gem_object_ggtt_pin(obj
, view
, 0, alignment
,
3499 PIN_MAPPABLE
| PIN_NONBLOCK
);
3501 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
3504 /* Valleyview is definitely limited to scanning out the first
3505 * 512MiB. Lets presume this behaviour was inherited from the
3506 * g4x display engine and that all earlier gen are similarly
3507 * limited. Testing suggests that it is a little more
3508 * complicated than this. For example, Cherryview appears quite
3509 * happy to scanout from anywhere within its global aperture.
3512 if (HAS_GMCH_DISPLAY(i915
))
3513 flags
= PIN_MAPPABLE
;
3514 vma
= i915_gem_object_ggtt_pin(obj
, view
, 0, alignment
, flags
);
3517 goto err_unpin_display
;
3519 vma
->display_alignment
= max_t(u64
, vma
->display_alignment
, alignment
);
3521 /* Treat this as an end-of-frame, like intel_user_framebuffer_dirty() */
3522 if (obj
->cache_dirty
) {
3523 i915_gem_clflush_object(obj
, true);
3524 intel_fb_obj_flush(obj
, false, ORIGIN_DIRTYFB
);
3527 old_write_domain
= obj
->base
.write_domain
;
3528 old_read_domains
= obj
->base
.read_domains
;
3530 /* It should now be out of any other write domains, and we can update
3531 * the domain values for our changes.
3533 obj
->base
.write_domain
= 0;
3534 obj
->base
.read_domains
|= I915_GEM_DOMAIN_GTT
;
3536 trace_i915_gem_object_change_domain(obj
,
3548 i915_gem_object_unpin_from_display_plane(struct i915_vma
*vma
)
3550 lockdep_assert_held(&vma
->vm
->i915
->drm
.struct_mutex
);
3552 if (WARN_ON(vma
->obj
->pin_display
== 0))
3555 if (--vma
->obj
->pin_display
== 0)
3556 vma
->display_alignment
= I915_GTT_MIN_ALIGNMENT
;
3558 /* Bump the LRU to try and avoid premature eviction whilst flipping */
3559 i915_gem_object_bump_inactive_ggtt(vma
->obj
);
3561 i915_vma_unpin(vma
);
3565 * Moves a single object to the CPU read, and possibly write domain.
3566 * @obj: object to act on
3567 * @write: requesting write or read-only access
3569 * This function returns when the move is complete, including waiting on
3573 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object
*obj
, bool write
)
3575 uint32_t old_write_domain
, old_read_domains
;
3578 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
3580 ret
= i915_gem_object_wait(obj
,
3581 I915_WAIT_INTERRUPTIBLE
|
3583 (write
? I915_WAIT_ALL
: 0),
3584 MAX_SCHEDULE_TIMEOUT
,
3589 if (obj
->base
.write_domain
== I915_GEM_DOMAIN_CPU
)
3592 i915_gem_object_flush_gtt_write_domain(obj
);
3594 old_write_domain
= obj
->base
.write_domain
;
3595 old_read_domains
= obj
->base
.read_domains
;
3597 /* Flush the CPU cache if it's still invalid. */
3598 if ((obj
->base
.read_domains
& I915_GEM_DOMAIN_CPU
) == 0) {
3599 i915_gem_clflush_object(obj
, false);
3601 obj
->base
.read_domains
|= I915_GEM_DOMAIN_CPU
;
3604 /* It should now be out of any other write domains, and we can update
3605 * the domain values for our changes.
3607 GEM_BUG_ON((obj
->base
.write_domain
& ~I915_GEM_DOMAIN_CPU
) != 0);
3609 /* If we're writing through the CPU, then the GPU read domains will
3610 * need to be invalidated at next use.
3613 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
3614 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
3617 trace_i915_gem_object_change_domain(obj
,
3624 /* Throttle our rendering by waiting until the ring has completed our requests
3625 * emitted over 20 msec ago.
3627 * Note that if we were to use the current jiffies each time around the loop,
3628 * we wouldn't escape the function with any frames outstanding if the time to
3629 * render a frame was over 20ms.
3631 * This should get us reasonable parallelism between CPU and GPU but also
3632 * relatively low latency when blocking on a particular request to finish.
3635 i915_gem_ring_throttle(struct drm_device
*dev
, struct drm_file
*file
)
3637 struct drm_i915_private
*dev_priv
= to_i915(dev
);
3638 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
3639 unsigned long recent_enough
= jiffies
- DRM_I915_THROTTLE_JIFFIES
;
3640 struct drm_i915_gem_request
*request
, *target
= NULL
;
3643 /* ABI: return -EIO if already wedged */
3644 if (i915_terminally_wedged(&dev_priv
->gpu_error
))
3647 spin_lock(&file_priv
->mm
.lock
);
3648 list_for_each_entry(request
, &file_priv
->mm
.request_list
, client_list
) {
3649 if (time_after_eq(request
->emitted_jiffies
, recent_enough
))
3653 * Note that the request might not have been submitted yet.
3654 * In which case emitted_jiffies will be zero.
3656 if (!request
->emitted_jiffies
)
3662 i915_gem_request_get(target
);
3663 spin_unlock(&file_priv
->mm
.lock
);
3668 ret
= i915_wait_request(target
,
3669 I915_WAIT_INTERRUPTIBLE
,
3670 MAX_SCHEDULE_TIMEOUT
);
3671 i915_gem_request_put(target
);
3673 return ret
< 0 ? ret
: 0;
3677 i915_gem_object_ggtt_pin(struct drm_i915_gem_object
*obj
,
3678 const struct i915_ggtt_view
*view
,
3683 struct drm_i915_private
*dev_priv
= to_i915(obj
->base
.dev
);
3684 struct i915_address_space
*vm
= &dev_priv
->ggtt
.base
;
3685 struct i915_vma
*vma
;
3688 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
3690 vma
= i915_vma_instance(obj
, vm
, view
);
3691 if (unlikely(IS_ERR(vma
)))
3694 if (i915_vma_misplaced(vma
, size
, alignment
, flags
)) {
3695 if (flags
& PIN_NONBLOCK
&&
3696 (i915_vma_is_pinned(vma
) || i915_vma_is_active(vma
)))
3697 return ERR_PTR(-ENOSPC
);
3699 if (flags
& PIN_MAPPABLE
) {
3700 /* If the required space is larger than the available
3701 * aperture, we will not able to find a slot for the
3702 * object and unbinding the object now will be in
3703 * vain. Worse, doing so may cause us to ping-pong
3704 * the object in and out of the Global GTT and
3705 * waste a lot of cycles under the mutex.
3707 if (vma
->fence_size
> dev_priv
->ggtt
.mappable_end
)
3708 return ERR_PTR(-E2BIG
);
3710 /* If NONBLOCK is set the caller is optimistically
3711 * trying to cache the full object within the mappable
3712 * aperture, and *must* have a fallback in place for
3713 * situations where we cannot bind the object. We
3714 * can be a little more lax here and use the fallback
3715 * more often to avoid costly migrations of ourselves
3716 * and other objects within the aperture.
3718 * Half-the-aperture is used as a simple heuristic.
3719 * More interesting would to do search for a free
3720 * block prior to making the commitment to unbind.
3721 * That caters for the self-harm case, and with a
3722 * little more heuristics (e.g. NOFAULT, NOEVICT)
3723 * we could try to minimise harm to others.
3725 if (flags
& PIN_NONBLOCK
&&
3726 vma
->fence_size
> dev_priv
->ggtt
.mappable_end
/ 2)
3727 return ERR_PTR(-ENOSPC
);
3730 WARN(i915_vma_is_pinned(vma
),
3731 "bo is already pinned in ggtt with incorrect alignment:"
3732 " offset=%08x, req.alignment=%llx,"
3733 " req.map_and_fenceable=%d, vma->map_and_fenceable=%d\n",
3734 i915_ggtt_offset(vma
), alignment
,
3735 !!(flags
& PIN_MAPPABLE
),
3736 i915_vma_is_map_and_fenceable(vma
));
3737 ret
= i915_vma_unbind(vma
);
3739 return ERR_PTR(ret
);
3742 ret
= i915_vma_pin(vma
, size
, alignment
, flags
| PIN_GLOBAL
);
3744 return ERR_PTR(ret
);
3749 static __always_inline
unsigned int __busy_read_flag(unsigned int id
)
3751 /* Note that we could alias engines in the execbuf API, but
3752 * that would be very unwise as it prevents userspace from
3753 * fine control over engine selection. Ahem.
3755 * This should be something like EXEC_MAX_ENGINE instead of
3758 BUILD_BUG_ON(I915_NUM_ENGINES
> 16);
3759 return 0x10000 << id
;
3762 static __always_inline
unsigned int __busy_write_id(unsigned int id
)
3764 /* The uABI guarantees an active writer is also amongst the read
3765 * engines. This would be true if we accessed the activity tracking
3766 * under the lock, but as we perform the lookup of the object and
3767 * its activity locklessly we can not guarantee that the last_write
3768 * being active implies that we have set the same engine flag from
3769 * last_read - hence we always set both read and write busy for
3772 return id
| __busy_read_flag(id
);
3775 static __always_inline
unsigned int
3776 __busy_set_if_active(const struct dma_fence
*fence
,
3777 unsigned int (*flag
)(unsigned int id
))
3779 struct drm_i915_gem_request
*rq
;
3781 /* We have to check the current hw status of the fence as the uABI
3782 * guarantees forward progress. We could rely on the idle worker
3783 * to eventually flush us, but to minimise latency just ask the
3786 * Note we only report on the status of native fences.
3788 if (!dma_fence_is_i915(fence
))
3791 /* opencode to_request() in order to avoid const warnings */
3792 rq
= container_of(fence
, struct drm_i915_gem_request
, fence
);
3793 if (i915_gem_request_completed(rq
))
3796 return flag(rq
->engine
->exec_id
);
3799 static __always_inline
unsigned int
3800 busy_check_reader(const struct dma_fence
*fence
)
3802 return __busy_set_if_active(fence
, __busy_read_flag
);
3805 static __always_inline
unsigned int
3806 busy_check_writer(const struct dma_fence
*fence
)
3811 return __busy_set_if_active(fence
, __busy_write_id
);
3815 i915_gem_busy_ioctl(struct drm_device
*dev
, void *data
,
3816 struct drm_file
*file
)
3818 struct drm_i915_gem_busy
*args
= data
;
3819 struct drm_i915_gem_object
*obj
;
3820 struct reservation_object_list
*list
;
3826 obj
= i915_gem_object_lookup_rcu(file
, args
->handle
);
3830 /* A discrepancy here is that we do not report the status of
3831 * non-i915 fences, i.e. even though we may report the object as idle,
3832 * a call to set-domain may still stall waiting for foreign rendering.
3833 * This also means that wait-ioctl may report an object as busy,
3834 * where busy-ioctl considers it idle.
3836 * We trade the ability to warn of foreign fences to report on which
3837 * i915 engines are active for the object.
3839 * Alternatively, we can trade that extra information on read/write
3842 * !reservation_object_test_signaled_rcu(obj->resv, true);
3843 * to report the overall busyness. This is what the wait-ioctl does.
3847 seq
= raw_read_seqcount(&obj
->resv
->seq
);
3849 /* Translate the exclusive fence to the READ *and* WRITE engine */
3850 args
->busy
= busy_check_writer(rcu_dereference(obj
->resv
->fence_excl
));
3852 /* Translate shared fences to READ set of engines */
3853 list
= rcu_dereference(obj
->resv
->fence
);
3855 unsigned int shared_count
= list
->shared_count
, i
;
3857 for (i
= 0; i
< shared_count
; ++i
) {
3858 struct dma_fence
*fence
=
3859 rcu_dereference(list
->shared
[i
]);
3861 args
->busy
|= busy_check_reader(fence
);
3865 if (args
->busy
&& read_seqcount_retry(&obj
->resv
->seq
, seq
))
3875 i915_gem_throttle_ioctl(struct drm_device
*dev
, void *data
,
3876 struct drm_file
*file_priv
)
3878 return i915_gem_ring_throttle(dev
, file_priv
);
3882 i915_gem_madvise_ioctl(struct drm_device
*dev
, void *data
,
3883 struct drm_file
*file_priv
)
3885 struct drm_i915_private
*dev_priv
= to_i915(dev
);
3886 struct drm_i915_gem_madvise
*args
= data
;
3887 struct drm_i915_gem_object
*obj
;
3890 switch (args
->madv
) {
3891 case I915_MADV_DONTNEED
:
3892 case I915_MADV_WILLNEED
:
3898 obj
= i915_gem_object_lookup(file_priv
, args
->handle
);
3902 err
= mutex_lock_interruptible(&obj
->mm
.lock
);
3906 if (obj
->mm
.pages
&&
3907 i915_gem_object_is_tiled(obj
) &&
3908 dev_priv
->quirks
& QUIRK_PIN_SWIZZLED_PAGES
) {
3909 if (obj
->mm
.madv
== I915_MADV_WILLNEED
) {
3910 GEM_BUG_ON(!obj
->mm
.quirked
);
3911 __i915_gem_object_unpin_pages(obj
);
3912 obj
->mm
.quirked
= false;
3914 if (args
->madv
== I915_MADV_WILLNEED
) {
3915 GEM_BUG_ON(obj
->mm
.quirked
);
3916 __i915_gem_object_pin_pages(obj
);
3917 obj
->mm
.quirked
= true;
3921 if (obj
->mm
.madv
!= __I915_MADV_PURGED
)
3922 obj
->mm
.madv
= args
->madv
;
3924 /* if the object is no longer attached, discard its backing storage */
3925 if (obj
->mm
.madv
== I915_MADV_DONTNEED
&& !obj
->mm
.pages
)
3926 i915_gem_object_truncate(obj
);
3928 args
->retained
= obj
->mm
.madv
!= __I915_MADV_PURGED
;
3929 mutex_unlock(&obj
->mm
.lock
);
3932 i915_gem_object_put(obj
);
3937 frontbuffer_retire(struct i915_gem_active
*active
,
3938 struct drm_i915_gem_request
*request
)
3940 struct drm_i915_gem_object
*obj
=
3941 container_of(active
, typeof(*obj
), frontbuffer_write
);
3943 intel_fb_obj_flush(obj
, true, ORIGIN_CS
);
3946 void i915_gem_object_init(struct drm_i915_gem_object
*obj
,
3947 const struct drm_i915_gem_object_ops
*ops
)
3949 mutex_init(&obj
->mm
.lock
);
3951 INIT_LIST_HEAD(&obj
->global_link
);
3952 INIT_LIST_HEAD(&obj
->userfault_link
);
3953 INIT_LIST_HEAD(&obj
->obj_exec_link
);
3954 INIT_LIST_HEAD(&obj
->vma_list
);
3955 INIT_LIST_HEAD(&obj
->batch_pool_link
);
3959 reservation_object_init(&obj
->__builtin_resv
);
3960 obj
->resv
= &obj
->__builtin_resv
;
3962 obj
->frontbuffer_ggtt_origin
= ORIGIN_GTT
;
3963 init_request_active(&obj
->frontbuffer_write
, frontbuffer_retire
);
3965 obj
->mm
.madv
= I915_MADV_WILLNEED
;
3966 INIT_RADIX_TREE(&obj
->mm
.get_page
.radix
, GFP_KERNEL
| __GFP_NOWARN
);
3967 mutex_init(&obj
->mm
.get_page
.lock
);
3969 i915_gem_info_add_obj(to_i915(obj
->base
.dev
), obj
->base
.size
);
3972 static const struct drm_i915_gem_object_ops i915_gem_object_ops
= {
3973 .flags
= I915_GEM_OBJECT_HAS_STRUCT_PAGE
|
3974 I915_GEM_OBJECT_IS_SHRINKABLE
,
3975 .get_pages
= i915_gem_object_get_pages_gtt
,
3976 .put_pages
= i915_gem_object_put_pages_gtt
,
3979 struct drm_i915_gem_object
*
3980 i915_gem_object_create(struct drm_i915_private
*dev_priv
, u64 size
)
3982 struct drm_i915_gem_object
*obj
;
3983 struct address_space
*mapping
;
3987 /* There is a prevalence of the assumption that we fit the object's
3988 * page count inside a 32bit _signed_ variable. Let's document this and
3989 * catch if we ever need to fix it. In the meantime, if you do spot
3990 * such a local variable, please consider fixing!
3992 if (WARN_ON(size
>> PAGE_SHIFT
> INT_MAX
))
3993 return ERR_PTR(-E2BIG
);
3995 if (overflows_type(size
, obj
->base
.size
))
3996 return ERR_PTR(-E2BIG
);
3998 obj
= i915_gem_object_alloc(dev_priv
);
4000 return ERR_PTR(-ENOMEM
);
4002 ret
= drm_gem_object_init(&dev_priv
->drm
, &obj
->base
, size
);
4006 mask
= GFP_HIGHUSER
| __GFP_RECLAIMABLE
;
4007 if (IS_I965GM(dev_priv
) || IS_I965G(dev_priv
)) {
4008 /* 965gm cannot relocate objects above 4GiB. */
4009 mask
&= ~__GFP_HIGHMEM
;
4010 mask
|= __GFP_DMA32
;
4013 mapping
= obj
->base
.filp
->f_mapping
;
4014 mapping_set_gfp_mask(mapping
, mask
);
4016 i915_gem_object_init(obj
, &i915_gem_object_ops
);
4018 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
4019 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
4021 if (HAS_LLC(dev_priv
)) {
4022 /* On some devices, we can have the GPU use the LLC (the CPU
4023 * cache) for about a 10% performance improvement
4024 * compared to uncached. Graphics requests other than
4025 * display scanout are coherent with the CPU in
4026 * accessing this cache. This means in this mode we
4027 * don't need to clflush on the CPU side, and on the
4028 * GPU side we only need to flush internal caches to
4029 * get data visible to the CPU.
4031 * However, we maintain the display planes as UC, and so
4032 * need to rebind when first used as such.
4034 obj
->cache_level
= I915_CACHE_LLC
;
4036 obj
->cache_level
= I915_CACHE_NONE
;
4038 trace_i915_gem_object_create(obj
);
4043 i915_gem_object_free(obj
);
4044 return ERR_PTR(ret
);
4047 static bool discard_backing_storage(struct drm_i915_gem_object
*obj
)
4049 /* If we are the last user of the backing storage (be it shmemfs
4050 * pages or stolen etc), we know that the pages are going to be
4051 * immediately released. In this case, we can then skip copying
4052 * back the contents from the GPU.
4055 if (obj
->mm
.madv
!= I915_MADV_WILLNEED
)
4058 if (obj
->base
.filp
== NULL
)
4061 /* At first glance, this looks racy, but then again so would be
4062 * userspace racing mmap against close. However, the first external
4063 * reference to the filp can only be obtained through the
4064 * i915_gem_mmap_ioctl() which safeguards us against the user
4065 * acquiring such a reference whilst we are in the middle of
4066 * freeing the object.
4068 return atomic_long_read(&obj
->base
.filp
->f_count
) == 1;
4071 static void __i915_gem_free_objects(struct drm_i915_private
*i915
,
4072 struct llist_node
*freed
)
4074 struct drm_i915_gem_object
*obj
, *on
;
4076 mutex_lock(&i915
->drm
.struct_mutex
);
4077 intel_runtime_pm_get(i915
);
4078 llist_for_each_entry(obj
, freed
, freed
) {
4079 struct i915_vma
*vma
, *vn
;
4081 trace_i915_gem_object_destroy(obj
);
4083 GEM_BUG_ON(i915_gem_object_is_active(obj
));
4084 list_for_each_entry_safe(vma
, vn
,
4085 &obj
->vma_list
, obj_link
) {
4086 GEM_BUG_ON(!i915_vma_is_ggtt(vma
));
4087 GEM_BUG_ON(i915_vma_is_active(vma
));
4088 vma
->flags
&= ~I915_VMA_PIN_MASK
;
4089 i915_vma_close(vma
);
4091 GEM_BUG_ON(!list_empty(&obj
->vma_list
));
4092 GEM_BUG_ON(!RB_EMPTY_ROOT(&obj
->vma_tree
));
4094 list_del(&obj
->global_link
);
4096 intel_runtime_pm_put(i915
);
4097 mutex_unlock(&i915
->drm
.struct_mutex
);
4099 llist_for_each_entry_safe(obj
, on
, freed
, freed
) {
4100 GEM_BUG_ON(obj
->bind_count
);
4101 GEM_BUG_ON(atomic_read(&obj
->frontbuffer_bits
));
4103 if (obj
->ops
->release
)
4104 obj
->ops
->release(obj
);
4106 if (WARN_ON(i915_gem_object_has_pinned_pages(obj
)))
4107 atomic_set(&obj
->mm
.pages_pin_count
, 0);
4108 __i915_gem_object_put_pages(obj
, I915_MM_NORMAL
);
4109 GEM_BUG_ON(obj
->mm
.pages
);
4111 if (obj
->base
.import_attach
)
4112 drm_prime_gem_destroy(&obj
->base
, NULL
);
4114 reservation_object_fini(&obj
->__builtin_resv
);
4115 drm_gem_object_release(&obj
->base
);
4116 i915_gem_info_remove_obj(i915
, obj
->base
.size
);
4119 i915_gem_object_free(obj
);
4123 static void i915_gem_flush_free_objects(struct drm_i915_private
*i915
)
4125 struct llist_node
*freed
;
4127 freed
= llist_del_all(&i915
->mm
.free_list
);
4128 if (unlikely(freed
))
4129 __i915_gem_free_objects(i915
, freed
);
4132 static void __i915_gem_free_work(struct work_struct
*work
)
4134 struct drm_i915_private
*i915
=
4135 container_of(work
, struct drm_i915_private
, mm
.free_work
);
4136 struct llist_node
*freed
;
4138 /* All file-owned VMA should have been released by this point through
4139 * i915_gem_close_object(), or earlier by i915_gem_context_close().
4140 * However, the object may also be bound into the global GTT (e.g.
4141 * older GPUs without per-process support, or for direct access through
4142 * the GTT either for the user or for scanout). Those VMA still need to
4146 while ((freed
= llist_del_all(&i915
->mm
.free_list
)))
4147 __i915_gem_free_objects(i915
, freed
);
4150 static void __i915_gem_free_object_rcu(struct rcu_head
*head
)
4152 struct drm_i915_gem_object
*obj
=
4153 container_of(head
, typeof(*obj
), rcu
);
4154 struct drm_i915_private
*i915
= to_i915(obj
->base
.dev
);
4156 /* We can't simply use call_rcu() from i915_gem_free_object()
4157 * as we need to block whilst unbinding, and the call_rcu
4158 * task may be called from softirq context. So we take a
4159 * detour through a worker.
4161 if (llist_add(&obj
->freed
, &i915
->mm
.free_list
))
4162 schedule_work(&i915
->mm
.free_work
);
4165 void i915_gem_free_object(struct drm_gem_object
*gem_obj
)
4167 struct drm_i915_gem_object
*obj
= to_intel_bo(gem_obj
);
4169 if (obj
->mm
.quirked
)
4170 __i915_gem_object_unpin_pages(obj
);
4172 if (discard_backing_storage(obj
))
4173 obj
->mm
.madv
= I915_MADV_DONTNEED
;
4175 /* Before we free the object, make sure any pure RCU-only
4176 * read-side critical sections are complete, e.g.
4177 * i915_gem_busy_ioctl(). For the corresponding synchronized
4178 * lookup see i915_gem_object_lookup_rcu().
4180 call_rcu(&obj
->rcu
, __i915_gem_free_object_rcu
);
4183 void __i915_gem_object_release_unless_active(struct drm_i915_gem_object
*obj
)
4185 lockdep_assert_held(&obj
->base
.dev
->struct_mutex
);
4187 GEM_BUG_ON(i915_gem_object_has_active_reference(obj
));
4188 if (i915_gem_object_is_active(obj
))
4189 i915_gem_object_set_active_reference(obj
);
4191 i915_gem_object_put(obj
);
4194 static void assert_kernel_context_is_current(struct drm_i915_private
*dev_priv
)
4196 struct intel_engine_cs
*engine
;
4197 enum intel_engine_id id
;
4199 for_each_engine(engine
, dev_priv
, id
)
4200 GEM_BUG_ON(engine
->last_retired_context
&&
4201 !i915_gem_context_is_kernel(engine
->last_retired_context
));
4204 int i915_gem_suspend(struct drm_i915_private
*dev_priv
)
4206 struct drm_device
*dev
= &dev_priv
->drm
;
4209 intel_suspend_gt_powersave(dev_priv
);
4211 mutex_lock(&dev
->struct_mutex
);
4213 /* We have to flush all the executing contexts to main memory so
4214 * that they can saved in the hibernation image. To ensure the last
4215 * context image is coherent, we have to switch away from it. That
4216 * leaves the dev_priv->kernel_context still active when
4217 * we actually suspend, and its image in memory may not match the GPU
4218 * state. Fortunately, the kernel_context is disposable and we do
4219 * not rely on its state.
4221 ret
= i915_gem_switch_to_kernel_context(dev_priv
);
4225 ret
= i915_gem_wait_for_idle(dev_priv
,
4226 I915_WAIT_INTERRUPTIBLE
|
4231 i915_gem_retire_requests(dev_priv
);
4232 GEM_BUG_ON(dev_priv
->gt
.active_requests
);
4234 assert_kernel_context_is_current(dev_priv
);
4235 i915_gem_context_lost(dev_priv
);
4236 mutex_unlock(&dev
->struct_mutex
);
4238 cancel_delayed_work_sync(&dev_priv
->gpu_error
.hangcheck_work
);
4239 cancel_delayed_work_sync(&dev_priv
->gt
.retire_work
);
4241 /* As the idle_work is rearming if it detects a race, play safe and
4242 * repeat the flush until it is definitely idle.
4244 while (flush_delayed_work(&dev_priv
->gt
.idle_work
))
4247 i915_gem_drain_freed_objects(dev_priv
);
4249 /* Assert that we sucessfully flushed all the work and
4250 * reset the GPU back to its idle, low power state.
4252 WARN_ON(dev_priv
->gt
.awake
);
4253 WARN_ON(!intel_execlists_idle(dev_priv
));
4256 * Neither the BIOS, ourselves or any other kernel
4257 * expects the system to be in execlists mode on startup,
4258 * so we need to reset the GPU back to legacy mode. And the only
4259 * known way to disable logical contexts is through a GPU reset.
4261 * So in order to leave the system in a known default configuration,
4262 * always reset the GPU upon unload and suspend. Afterwards we then
4263 * clean up the GEM state tracking, flushing off the requests and
4264 * leaving the system in a known idle state.
4266 * Note that is of the upmost importance that the GPU is idle and
4267 * all stray writes are flushed *before* we dismantle the backing
4268 * storage for the pinned objects.
4270 * However, since we are uncertain that resetting the GPU on older
4271 * machines is a good idea, we don't - just in case it leaves the
4272 * machine in an unusable condition.
4274 if (HAS_HW_CONTEXTS(dev_priv
)) {
4275 int reset
= intel_gpu_reset(dev_priv
, ALL_ENGINES
);
4276 WARN_ON(reset
&& reset
!= -ENODEV
);
4282 mutex_unlock(&dev
->struct_mutex
);
4286 void i915_gem_resume(struct drm_i915_private
*dev_priv
)
4288 struct drm_device
*dev
= &dev_priv
->drm
;
4290 WARN_ON(dev_priv
->gt
.awake
);
4292 mutex_lock(&dev
->struct_mutex
);
4293 i915_gem_restore_gtt_mappings(dev_priv
);
4295 /* As we didn't flush the kernel context before suspend, we cannot
4296 * guarantee that the context image is complete. So let's just reset
4297 * it and start again.
4299 dev_priv
->gt
.resume(dev_priv
);
4301 mutex_unlock(&dev
->struct_mutex
);
4304 void i915_gem_init_swizzling(struct drm_i915_private
*dev_priv
)
4306 if (INTEL_GEN(dev_priv
) < 5 ||
4307 dev_priv
->mm
.bit_6_swizzle_x
== I915_BIT_6_SWIZZLE_NONE
)
4310 I915_WRITE(DISP_ARB_CTL
, I915_READ(DISP_ARB_CTL
) |
4311 DISP_TILE_SURFACE_SWIZZLING
);
4313 if (IS_GEN5(dev_priv
))
4316 I915_WRITE(TILECTL
, I915_READ(TILECTL
) | TILECTL_SWZCTL
);
4317 if (IS_GEN6(dev_priv
))
4318 I915_WRITE(ARB_MODE
, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_SNB
));
4319 else if (IS_GEN7(dev_priv
))
4320 I915_WRITE(ARB_MODE
, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_IVB
));
4321 else if (IS_GEN8(dev_priv
))
4322 I915_WRITE(GAMTARBMODE
, _MASKED_BIT_ENABLE(ARB_MODE_SWIZZLE_BDW
));
4327 static void init_unused_ring(struct drm_i915_private
*dev_priv
, u32 base
)
4329 I915_WRITE(RING_CTL(base
), 0);
4330 I915_WRITE(RING_HEAD(base
), 0);
4331 I915_WRITE(RING_TAIL(base
), 0);
4332 I915_WRITE(RING_START(base
), 0);
4335 static void init_unused_rings(struct drm_i915_private
*dev_priv
)
4337 if (IS_I830(dev_priv
)) {
4338 init_unused_ring(dev_priv
, PRB1_BASE
);
4339 init_unused_ring(dev_priv
, SRB0_BASE
);
4340 init_unused_ring(dev_priv
, SRB1_BASE
);
4341 init_unused_ring(dev_priv
, SRB2_BASE
);
4342 init_unused_ring(dev_priv
, SRB3_BASE
);
4343 } else if (IS_GEN2(dev_priv
)) {
4344 init_unused_ring(dev_priv
, SRB0_BASE
);
4345 init_unused_ring(dev_priv
, SRB1_BASE
);
4346 } else if (IS_GEN3(dev_priv
)) {
4347 init_unused_ring(dev_priv
, PRB1_BASE
);
4348 init_unused_ring(dev_priv
, PRB2_BASE
);
4353 i915_gem_init_hw(struct drm_i915_private
*dev_priv
)
4355 struct intel_engine_cs
*engine
;
4356 enum intel_engine_id id
;
4359 dev_priv
->gt
.last_init_time
= ktime_get();
4361 /* Double layer security blanket, see i915_gem_init() */
4362 intel_uncore_forcewake_get(dev_priv
, FORCEWAKE_ALL
);
4364 if (HAS_EDRAM(dev_priv
) && INTEL_GEN(dev_priv
) < 9)
4365 I915_WRITE(HSW_IDICR
, I915_READ(HSW_IDICR
) | IDIHASHMSK(0xf));
4367 if (IS_HASWELL(dev_priv
))
4368 I915_WRITE(MI_PREDICATE_RESULT_2
, IS_HSW_GT3(dev_priv
) ?
4369 LOWER_SLICE_ENABLED
: LOWER_SLICE_DISABLED
);
4371 if (HAS_PCH_NOP(dev_priv
)) {
4372 if (IS_IVYBRIDGE(dev_priv
)) {
4373 u32 temp
= I915_READ(GEN7_MSG_CTL
);
4374 temp
&= ~(WAIT_FOR_PCH_FLR_ACK
| WAIT_FOR_PCH_RESET_ACK
);
4375 I915_WRITE(GEN7_MSG_CTL
, temp
);
4376 } else if (INTEL_GEN(dev_priv
) >= 7) {
4377 u32 temp
= I915_READ(HSW_NDE_RSTWRN_OPT
);
4378 temp
&= ~RESET_PCH_HANDSHAKE_ENABLE
;
4379 I915_WRITE(HSW_NDE_RSTWRN_OPT
, temp
);
4383 i915_gem_init_swizzling(dev_priv
);
4386 * At least 830 can leave some of the unused rings
4387 * "active" (ie. head != tail) after resume which
4388 * will prevent c3 entry. Makes sure all unused rings
4391 init_unused_rings(dev_priv
);
4393 BUG_ON(!dev_priv
->kernel_context
);
4395 ret
= i915_ppgtt_init_hw(dev_priv
);
4397 DRM_ERROR("PPGTT enable HW failed %d\n", ret
);
4401 /* Need to do basic initialisation of all rings first: */
4402 for_each_engine(engine
, dev_priv
, id
) {
4403 ret
= engine
->init_hw(engine
);
4408 intel_mocs_init_l3cc_table(dev_priv
);
4410 /* We can't enable contexts until all firmware is loaded */
4411 ret
= intel_guc_setup(dev_priv
);
4416 intel_uncore_forcewake_put(dev_priv
, FORCEWAKE_ALL
);
4420 bool intel_sanitize_semaphores(struct drm_i915_private
*dev_priv
, int value
)
4422 if (INTEL_INFO(dev_priv
)->gen
< 6)
4425 /* TODO: make semaphores and Execlists play nicely together */
4426 if (i915
.enable_execlists
)
4432 #ifdef CONFIG_INTEL_IOMMU
4433 /* Enable semaphores on SNB when IO remapping is off */
4434 if (INTEL_INFO(dev_priv
)->gen
== 6 && intel_iommu_gfx_mapped
)
4441 int i915_gem_init(struct drm_i915_private
*dev_priv
)
4445 mutex_lock(&dev_priv
->drm
.struct_mutex
);
4447 if (!i915
.enable_execlists
) {
4448 dev_priv
->gt
.resume
= intel_legacy_submission_resume
;
4449 dev_priv
->gt
.cleanup_engine
= intel_engine_cleanup
;
4451 dev_priv
->gt
.resume
= intel_lr_context_resume
;
4452 dev_priv
->gt
.cleanup_engine
= intel_logical_ring_cleanup
;
4455 /* This is just a security blanket to placate dragons.
4456 * On some systems, we very sporadically observe that the first TLBs
4457 * used by the CS may be stale, despite us poking the TLB reset. If
4458 * we hold the forcewake during initialisation these problems
4459 * just magically go away.
4461 intel_uncore_forcewake_get(dev_priv
, FORCEWAKE_ALL
);
4463 i915_gem_init_userptr(dev_priv
);
4465 ret
= i915_gem_init_ggtt(dev_priv
);
4469 ret
= i915_gem_context_init(dev_priv
);
4473 ret
= intel_engines_init(dev_priv
);
4477 ret
= i915_gem_init_hw(dev_priv
);
4479 /* Allow engine initialisation to fail by marking the GPU as
4480 * wedged. But we only want to do this where the GPU is angry,
4481 * for all other failure, such as an allocation failure, bail.
4483 DRM_ERROR("Failed to initialize GPU, declaring it wedged\n");
4484 i915_gem_set_wedged(dev_priv
);
4489 intel_uncore_forcewake_put(dev_priv
, FORCEWAKE_ALL
);
4490 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
4496 i915_gem_cleanup_engines(struct drm_i915_private
*dev_priv
)
4498 struct intel_engine_cs
*engine
;
4499 enum intel_engine_id id
;
4501 for_each_engine(engine
, dev_priv
, id
)
4502 dev_priv
->gt
.cleanup_engine(engine
);
4506 i915_gem_load_init_fences(struct drm_i915_private
*dev_priv
)
4510 if (INTEL_INFO(dev_priv
)->gen
>= 7 && !IS_VALLEYVIEW(dev_priv
) &&
4511 !IS_CHERRYVIEW(dev_priv
))
4512 dev_priv
->num_fence_regs
= 32;
4513 else if (INTEL_INFO(dev_priv
)->gen
>= 4 ||
4514 IS_I945G(dev_priv
) || IS_I945GM(dev_priv
) ||
4515 IS_G33(dev_priv
) || IS_PINEVIEW(dev_priv
))
4516 dev_priv
->num_fence_regs
= 16;
4518 dev_priv
->num_fence_regs
= 8;
4520 if (intel_vgpu_active(dev_priv
))
4521 dev_priv
->num_fence_regs
=
4522 I915_READ(vgtif_reg(avail_rs
.fence_num
));
4524 /* Initialize fence registers to zero */
4525 for (i
= 0; i
< dev_priv
->num_fence_regs
; i
++) {
4526 struct drm_i915_fence_reg
*fence
= &dev_priv
->fence_regs
[i
];
4528 fence
->i915
= dev_priv
;
4530 list_add_tail(&fence
->link
, &dev_priv
->mm
.fence_list
);
4532 i915_gem_restore_fences(dev_priv
);
4534 i915_gem_detect_bit_6_swizzle(dev_priv
);
4538 i915_gem_load_init(struct drm_i915_private
*dev_priv
)
4542 dev_priv
->objects
= KMEM_CACHE(drm_i915_gem_object
, SLAB_HWCACHE_ALIGN
);
4543 if (!dev_priv
->objects
)
4546 dev_priv
->vmas
= KMEM_CACHE(i915_vma
, SLAB_HWCACHE_ALIGN
);
4547 if (!dev_priv
->vmas
)
4550 dev_priv
->requests
= KMEM_CACHE(drm_i915_gem_request
,
4551 SLAB_HWCACHE_ALIGN
|
4552 SLAB_RECLAIM_ACCOUNT
|
4553 SLAB_DESTROY_BY_RCU
);
4554 if (!dev_priv
->requests
)
4557 dev_priv
->dependencies
= KMEM_CACHE(i915_dependency
,
4558 SLAB_HWCACHE_ALIGN
|
4559 SLAB_RECLAIM_ACCOUNT
);
4560 if (!dev_priv
->dependencies
)
4563 mutex_lock(&dev_priv
->drm
.struct_mutex
);
4564 INIT_LIST_HEAD(&dev_priv
->gt
.timelines
);
4565 err
= i915_gem_timeline_init__global(dev_priv
);
4566 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
4568 goto err_dependencies
;
4570 INIT_LIST_HEAD(&dev_priv
->context_list
);
4571 INIT_WORK(&dev_priv
->mm
.free_work
, __i915_gem_free_work
);
4572 init_llist_head(&dev_priv
->mm
.free_list
);
4573 INIT_LIST_HEAD(&dev_priv
->mm
.unbound_list
);
4574 INIT_LIST_HEAD(&dev_priv
->mm
.bound_list
);
4575 INIT_LIST_HEAD(&dev_priv
->mm
.fence_list
);
4576 INIT_LIST_HEAD(&dev_priv
->mm
.userfault_list
);
4577 INIT_DELAYED_WORK(&dev_priv
->gt
.retire_work
,
4578 i915_gem_retire_work_handler
);
4579 INIT_DELAYED_WORK(&dev_priv
->gt
.idle_work
,
4580 i915_gem_idle_work_handler
);
4581 init_waitqueue_head(&dev_priv
->gpu_error
.wait_queue
);
4582 init_waitqueue_head(&dev_priv
->gpu_error
.reset_queue
);
4584 dev_priv
->relative_constants_mode
= I915_EXEC_CONSTANTS_REL_GENERAL
;
4586 init_waitqueue_head(&dev_priv
->pending_flip_queue
);
4588 dev_priv
->mm
.interruptible
= true;
4590 atomic_set(&dev_priv
->mm
.bsd_engine_dispatch_index
, 0);
4592 spin_lock_init(&dev_priv
->fb_tracking
.lock
);
4597 kmem_cache_destroy(dev_priv
->dependencies
);
4599 kmem_cache_destroy(dev_priv
->requests
);
4601 kmem_cache_destroy(dev_priv
->vmas
);
4603 kmem_cache_destroy(dev_priv
->objects
);
4608 void i915_gem_load_cleanup(struct drm_i915_private
*dev_priv
)
4610 WARN_ON(!llist_empty(&dev_priv
->mm
.free_list
));
4612 mutex_lock(&dev_priv
->drm
.struct_mutex
);
4613 i915_gem_timeline_fini(&dev_priv
->gt
.global_timeline
);
4614 WARN_ON(!list_empty(&dev_priv
->gt
.timelines
));
4615 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
4617 kmem_cache_destroy(dev_priv
->dependencies
);
4618 kmem_cache_destroy(dev_priv
->requests
);
4619 kmem_cache_destroy(dev_priv
->vmas
);
4620 kmem_cache_destroy(dev_priv
->objects
);
4622 /* And ensure that our DESTROY_BY_RCU slabs are truly destroyed */
4626 int i915_gem_freeze(struct drm_i915_private
*dev_priv
)
4628 intel_runtime_pm_get(dev_priv
);
4630 mutex_lock(&dev_priv
->drm
.struct_mutex
);
4631 i915_gem_shrink_all(dev_priv
);
4632 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
4634 intel_runtime_pm_put(dev_priv
);
4639 int i915_gem_freeze_late(struct drm_i915_private
*dev_priv
)
4641 struct drm_i915_gem_object
*obj
;
4642 struct list_head
*phases
[] = {
4643 &dev_priv
->mm
.unbound_list
,
4644 &dev_priv
->mm
.bound_list
,
4648 /* Called just before we write the hibernation image.
4650 * We need to update the domain tracking to reflect that the CPU
4651 * will be accessing all the pages to create and restore from the
4652 * hibernation, and so upon restoration those pages will be in the
4655 * To make sure the hibernation image contains the latest state,
4656 * we update that state just before writing out the image.
4658 * To try and reduce the hibernation image, we manually shrink
4659 * the objects as well.
4662 mutex_lock(&dev_priv
->drm
.struct_mutex
);
4663 i915_gem_shrink(dev_priv
, -1UL, I915_SHRINK_UNBOUND
);
4665 for (p
= phases
; *p
; p
++) {
4666 list_for_each_entry(obj
, *p
, global_link
) {
4667 obj
->base
.read_domains
= I915_GEM_DOMAIN_CPU
;
4668 obj
->base
.write_domain
= I915_GEM_DOMAIN_CPU
;
4671 mutex_unlock(&dev_priv
->drm
.struct_mutex
);
4676 void i915_gem_release(struct drm_device
*dev
, struct drm_file
*file
)
4678 struct drm_i915_file_private
*file_priv
= file
->driver_priv
;
4679 struct drm_i915_gem_request
*request
;
4681 /* Clean up our request list when the client is going away, so that
4682 * later retire_requests won't dereference our soon-to-be-gone
4685 spin_lock(&file_priv
->mm
.lock
);
4686 list_for_each_entry(request
, &file_priv
->mm
.request_list
, client_list
)
4687 request
->file_priv
= NULL
;
4688 spin_unlock(&file_priv
->mm
.lock
);
4690 if (!list_empty(&file_priv
->rps
.link
)) {
4691 spin_lock(&to_i915(dev
)->rps
.client_lock
);
4692 list_del(&file_priv
->rps
.link
);
4693 spin_unlock(&to_i915(dev
)->rps
.client_lock
);
4697 int i915_gem_open(struct drm_device
*dev
, struct drm_file
*file
)
4699 struct drm_i915_file_private
*file_priv
;
4704 file_priv
= kzalloc(sizeof(*file_priv
), GFP_KERNEL
);
4708 file
->driver_priv
= file_priv
;
4709 file_priv
->dev_priv
= to_i915(dev
);
4710 file_priv
->file
= file
;
4711 INIT_LIST_HEAD(&file_priv
->rps
.link
);
4713 spin_lock_init(&file_priv
->mm
.lock
);
4714 INIT_LIST_HEAD(&file_priv
->mm
.request_list
);
4716 file_priv
->bsd_engine
= -1;
4718 ret
= i915_gem_context_open(dev
, file
);
4726 * i915_gem_track_fb - update frontbuffer tracking
4727 * @old: current GEM buffer for the frontbuffer slots
4728 * @new: new GEM buffer for the frontbuffer slots
4729 * @frontbuffer_bits: bitmask of frontbuffer slots
4731 * This updates the frontbuffer tracking bits @frontbuffer_bits by clearing them
4732 * from @old and setting them in @new. Both @old and @new can be NULL.
4734 void i915_gem_track_fb(struct drm_i915_gem_object
*old
,
4735 struct drm_i915_gem_object
*new,
4736 unsigned frontbuffer_bits
)
4738 /* Control of individual bits within the mask are guarded by
4739 * the owning plane->mutex, i.e. we can never see concurrent
4740 * manipulation of individual bits. But since the bitfield as a whole
4741 * is updated using RMW, we need to use atomics in order to update
4744 BUILD_BUG_ON(INTEL_FRONTBUFFER_BITS_PER_PIPE
* I915_MAX_PIPES
>
4745 sizeof(atomic_t
) * BITS_PER_BYTE
);
4748 WARN_ON(!(atomic_read(&old
->frontbuffer_bits
) & frontbuffer_bits
));
4749 atomic_andnot(frontbuffer_bits
, &old
->frontbuffer_bits
);
4753 WARN_ON(atomic_read(&new->frontbuffer_bits
) & frontbuffer_bits
);
4754 atomic_or(frontbuffer_bits
, &new->frontbuffer_bits
);
4758 /* Allocate a new GEM object and fill it with the supplied data */
4759 struct drm_i915_gem_object
*
4760 i915_gem_object_create_from_data(struct drm_i915_private
*dev_priv
,
4761 const void *data
, size_t size
)
4763 struct drm_i915_gem_object
*obj
;
4764 struct sg_table
*sg
;
4768 obj
= i915_gem_object_create(dev_priv
, round_up(size
, PAGE_SIZE
));
4772 ret
= i915_gem_object_set_to_cpu_domain(obj
, true);
4776 ret
= i915_gem_object_pin_pages(obj
);
4781 bytes
= sg_copy_from_buffer(sg
->sgl
, sg
->nents
, (void *)data
, size
);
4782 obj
->mm
.dirty
= true; /* Backing store is now out of date */
4783 i915_gem_object_unpin_pages(obj
);
4785 if (WARN_ON(bytes
!= size
)) {
4786 DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes
, size
);
4794 i915_gem_object_put(obj
);
4795 return ERR_PTR(ret
);
4798 struct scatterlist
*
4799 i915_gem_object_get_sg(struct drm_i915_gem_object
*obj
,
4801 unsigned int *offset
)
4803 struct i915_gem_object_page_iter
*iter
= &obj
->mm
.get_page
;
4804 struct scatterlist
*sg
;
4805 unsigned int idx
, count
;
4808 GEM_BUG_ON(n
>= obj
->base
.size
>> PAGE_SHIFT
);
4809 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj
));
4811 /* As we iterate forward through the sg, we record each entry in a
4812 * radixtree for quick repeated (backwards) lookups. If we have seen
4813 * this index previously, we will have an entry for it.
4815 * Initial lookup is O(N), but this is amortized to O(1) for
4816 * sequential page access (where each new request is consecutive
4817 * to the previous one). Repeated lookups are O(lg(obj->base.size)),
4818 * i.e. O(1) with a large constant!
4820 if (n
< READ_ONCE(iter
->sg_idx
))
4823 mutex_lock(&iter
->lock
);
4825 /* We prefer to reuse the last sg so that repeated lookup of this
4826 * (or the subsequent) sg are fast - comparing against the last
4827 * sg is faster than going through the radixtree.
4832 count
= __sg_page_count(sg
);
4834 while (idx
+ count
<= n
) {
4835 unsigned long exception
, i
;
4838 /* If we cannot allocate and insert this entry, or the
4839 * individual pages from this range, cancel updating the
4840 * sg_idx so that on this lookup we are forced to linearly
4841 * scan onwards, but on future lookups we will try the
4842 * insertion again (in which case we need to be careful of
4843 * the error return reporting that we have already inserted
4846 ret
= radix_tree_insert(&iter
->radix
, idx
, sg
);
4847 if (ret
&& ret
!= -EEXIST
)
4851 RADIX_TREE_EXCEPTIONAL_ENTRY
|
4852 idx
<< RADIX_TREE_EXCEPTIONAL_SHIFT
;
4853 for (i
= 1; i
< count
; i
++) {
4854 ret
= radix_tree_insert(&iter
->radix
, idx
+ i
,
4856 if (ret
&& ret
!= -EEXIST
)
4861 sg
= ____sg_next(sg
);
4862 count
= __sg_page_count(sg
);
4869 mutex_unlock(&iter
->lock
);
4871 if (unlikely(n
< idx
)) /* insertion completed by another thread */
4874 /* In case we failed to insert the entry into the radixtree, we need
4875 * to look beyond the current sg.
4877 while (idx
+ count
<= n
) {
4879 sg
= ____sg_next(sg
);
4880 count
= __sg_page_count(sg
);
4889 sg
= radix_tree_lookup(&iter
->radix
, n
);
4892 /* If this index is in the middle of multi-page sg entry,
4893 * the radixtree will contain an exceptional entry that points
4894 * to the start of that range. We will return the pointer to
4895 * the base page and the offset of this page within the
4899 if (unlikely(radix_tree_exception(sg
))) {
4900 unsigned long base
=
4901 (unsigned long)sg
>> RADIX_TREE_EXCEPTIONAL_SHIFT
;
4903 sg
= radix_tree_lookup(&iter
->radix
, base
);
4915 i915_gem_object_get_page(struct drm_i915_gem_object
*obj
, unsigned int n
)
4917 struct scatterlist
*sg
;
4918 unsigned int offset
;
4920 GEM_BUG_ON(!i915_gem_object_has_struct_page(obj
));
4922 sg
= i915_gem_object_get_sg(obj
, n
, &offset
);
4923 return nth_page(sg_page(sg
), offset
);
4926 /* Like i915_gem_object_get_page(), but mark the returned page dirty */
4928 i915_gem_object_get_dirty_page(struct drm_i915_gem_object
*obj
,
4933 page
= i915_gem_object_get_page(obj
, n
);
4935 set_page_dirty(page
);
4941 i915_gem_object_get_dma_address(struct drm_i915_gem_object
*obj
,
4944 struct scatterlist
*sg
;
4945 unsigned int offset
;
4947 sg
= i915_gem_object_get_sg(obj
, n
, &offset
);
4948 return sg_dma_address(sg
) + (offset
<< PAGE_SHIFT
);