drm/i915/gem: Implement object migration
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gem / i915_gem_object.h
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6
7 #ifndef __I915_GEM_OBJECT_H__
8 #define __I915_GEM_OBJECT_H__
9
10 #include <drm/drm_gem.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_device.h>
13
14 #include "display/intel_frontbuffer.h"
15 #include "i915_gem_object_types.h"
16 #include "i915_gem_gtt.h"
17 #include "i915_gem_ww.h"
18 #include "i915_vma_types.h"
19
20 enum intel_region_id;
21
22 /*
23  * XXX: There is a prevalence of the assumption that we fit the
24  * object's page count inside a 32bit _signed_ variable. Let's document
25  * this and catch if we ever need to fix it. In the meantime, if you do
26  * spot such a local variable, please consider fixing!
27  *
28  * Aside from our own locals (for which we have no excuse!):
29  * - sg_table embeds unsigned int for num_pages
30  * - get_user_pages*() mixed ints with longs
31  */
32 #define GEM_CHECK_SIZE_OVERFLOW(sz) \
33         GEM_WARN_ON((sz) >> PAGE_SHIFT > INT_MAX)
34
35 static inline bool i915_gem_object_size_2big(u64 size)
36 {
37         struct drm_i915_gem_object *obj;
38
39         if (GEM_CHECK_SIZE_OVERFLOW(size))
40                 return true;
41
42         if (overflows_type(size, obj->base.size))
43                 return true;
44
45         return false;
46 }
47
48 void i915_gem_init__objects(struct drm_i915_private *i915);
49
50 struct drm_i915_gem_object *i915_gem_object_alloc(void);
51 void i915_gem_object_free(struct drm_i915_gem_object *obj);
52
53 void i915_gem_object_init(struct drm_i915_gem_object *obj,
54                           const struct drm_i915_gem_object_ops *ops,
55                           struct lock_class_key *key,
56                           unsigned alloc_flags);
57 struct drm_i915_gem_object *
58 i915_gem_object_create_shmem(struct drm_i915_private *i915,
59                              resource_size_t size);
60 struct drm_i915_gem_object *
61 i915_gem_object_create_shmem_from_data(struct drm_i915_private *i915,
62                                        const void *data, resource_size_t size);
63
64 extern const struct drm_i915_gem_object_ops i915_gem_shmem_ops;
65
66 void __i915_gem_object_release_shmem(struct drm_i915_gem_object *obj,
67                                      struct sg_table *pages,
68                                      bool needs_clflush);
69
70 int i915_gem_object_pwrite_phys(struct drm_i915_gem_object *obj,
71                                 const struct drm_i915_gem_pwrite *args);
72 int i915_gem_object_pread_phys(struct drm_i915_gem_object *obj,
73                                const struct drm_i915_gem_pread *args);
74
75 int i915_gem_object_attach_phys(struct drm_i915_gem_object *obj, int align);
76 void i915_gem_object_put_pages_shmem(struct drm_i915_gem_object *obj,
77                                      struct sg_table *pages);
78 void i915_gem_object_put_pages_phys(struct drm_i915_gem_object *obj,
79                                     struct sg_table *pages);
80
81 void i915_gem_flush_free_objects(struct drm_i915_private *i915);
82
83 struct sg_table *
84 __i915_gem_object_unset_pages(struct drm_i915_gem_object *obj);
85 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
86
87 /**
88  * i915_gem_object_lookup_rcu - look up a temporary GEM object from its handle
89  * @filp: DRM file private date
90  * @handle: userspace handle
91  *
92  * Returns:
93  *
94  * A pointer to the object named by the handle if such exists on @filp, NULL
95  * otherwise. This object is only valid whilst under the RCU read lock, and
96  * note carefully the object may be in the process of being destroyed.
97  */
98 static inline struct drm_i915_gem_object *
99 i915_gem_object_lookup_rcu(struct drm_file *file, u32 handle)
100 {
101 #ifdef CONFIG_LOCKDEP
102         WARN_ON(debug_locks && !lock_is_held(&rcu_lock_map));
103 #endif
104         return idr_find(&file->object_idr, handle);
105 }
106
107 static inline struct drm_i915_gem_object *
108 i915_gem_object_get_rcu(struct drm_i915_gem_object *obj)
109 {
110         if (obj && !kref_get_unless_zero(&obj->base.refcount))
111                 obj = NULL;
112
113         return obj;
114 }
115
116 static inline struct drm_i915_gem_object *
117 i915_gem_object_lookup(struct drm_file *file, u32 handle)
118 {
119         struct drm_i915_gem_object *obj;
120
121         rcu_read_lock();
122         obj = i915_gem_object_lookup_rcu(file, handle);
123         obj = i915_gem_object_get_rcu(obj);
124         rcu_read_unlock();
125
126         return obj;
127 }
128
129 __deprecated
130 struct drm_gem_object *
131 drm_gem_object_lookup(struct drm_file *file, u32 handle);
132
133 __attribute__((nonnull))
134 static inline struct drm_i915_gem_object *
135 i915_gem_object_get(struct drm_i915_gem_object *obj)
136 {
137         drm_gem_object_get(&obj->base);
138         return obj;
139 }
140
141 __attribute__((nonnull))
142 static inline void
143 i915_gem_object_put(struct drm_i915_gem_object *obj)
144 {
145         __drm_gem_object_put(&obj->base);
146 }
147
148 #define assert_object_held(obj) dma_resv_assert_held((obj)->base.resv)
149
150 /*
151  * If more than one potential simultaneous locker, assert held.
152  */
153 static inline void assert_object_held_shared(const struct drm_i915_gem_object *obj)
154 {
155         /*
156          * Note mm list lookup is protected by
157          * kref_get_unless_zero().
158          */
159         if (IS_ENABLED(CONFIG_LOCKDEP) &&
160             kref_read(&obj->base.refcount) > 0)
161                 assert_object_held(obj);
162 }
163
164 static inline int __i915_gem_object_lock(struct drm_i915_gem_object *obj,
165                                          struct i915_gem_ww_ctx *ww,
166                                          bool intr)
167 {
168         int ret;
169
170         if (intr)
171                 ret = dma_resv_lock_interruptible(obj->base.resv, ww ? &ww->ctx : NULL);
172         else
173                 ret = dma_resv_lock(obj->base.resv, ww ? &ww->ctx : NULL);
174
175         if (!ret && ww) {
176                 i915_gem_object_get(obj);
177                 list_add_tail(&obj->obj_link, &ww->obj_list);
178         }
179         if (ret == -EALREADY)
180                 ret = 0;
181
182         if (ret == -EDEADLK) {
183                 i915_gem_object_get(obj);
184                 ww->contended = obj;
185         }
186
187         return ret;
188 }
189
190 static inline int i915_gem_object_lock(struct drm_i915_gem_object *obj,
191                                        struct i915_gem_ww_ctx *ww)
192 {
193         return __i915_gem_object_lock(obj, ww, ww && ww->intr);
194 }
195
196 static inline int i915_gem_object_lock_interruptible(struct drm_i915_gem_object *obj,
197                                                      struct i915_gem_ww_ctx *ww)
198 {
199         WARN_ON(ww && !ww->intr);
200         return __i915_gem_object_lock(obj, ww, true);
201 }
202
203 static inline bool i915_gem_object_trylock(struct drm_i915_gem_object *obj)
204 {
205         return dma_resv_trylock(obj->base.resv);
206 }
207
208 static inline void i915_gem_object_unlock(struct drm_i915_gem_object *obj)
209 {
210         if (obj->ops->adjust_lru)
211                 obj->ops->adjust_lru(obj);
212
213         dma_resv_unlock(obj->base.resv);
214 }
215
216 static inline void
217 i915_gem_object_set_readonly(struct drm_i915_gem_object *obj)
218 {
219         obj->flags |= I915_BO_READONLY;
220 }
221
222 static inline bool
223 i915_gem_object_is_readonly(const struct drm_i915_gem_object *obj)
224 {
225         return obj->flags & I915_BO_READONLY;
226 }
227
228 static inline bool
229 i915_gem_object_is_contiguous(const struct drm_i915_gem_object *obj)
230 {
231         return obj->flags & I915_BO_ALLOC_CONTIGUOUS;
232 }
233
234 static inline bool
235 i915_gem_object_is_volatile(const struct drm_i915_gem_object *obj)
236 {
237         return obj->flags & I915_BO_ALLOC_VOLATILE;
238 }
239
240 static inline void
241 i915_gem_object_set_volatile(struct drm_i915_gem_object *obj)
242 {
243         obj->flags |= I915_BO_ALLOC_VOLATILE;
244 }
245
246 static inline bool
247 i915_gem_object_has_tiling_quirk(struct drm_i915_gem_object *obj)
248 {
249         return test_bit(I915_TILING_QUIRK_BIT, &obj->flags);
250 }
251
252 static inline void
253 i915_gem_object_set_tiling_quirk(struct drm_i915_gem_object *obj)
254 {
255         set_bit(I915_TILING_QUIRK_BIT, &obj->flags);
256 }
257
258 static inline void
259 i915_gem_object_clear_tiling_quirk(struct drm_i915_gem_object *obj)
260 {
261         clear_bit(I915_TILING_QUIRK_BIT, &obj->flags);
262 }
263
264 static inline bool
265 i915_gem_object_type_has(const struct drm_i915_gem_object *obj,
266                          unsigned long flags)
267 {
268         return obj->ops->flags & flags;
269 }
270
271 bool i915_gem_object_has_struct_page(const struct drm_i915_gem_object *obj);
272
273 bool i915_gem_object_has_iomem(const struct drm_i915_gem_object *obj);
274
275 static inline bool
276 i915_gem_object_is_shrinkable(const struct drm_i915_gem_object *obj)
277 {
278         return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_SHRINKABLE);
279 }
280
281 static inline bool
282 i915_gem_object_is_proxy(const struct drm_i915_gem_object *obj)
283 {
284         return i915_gem_object_type_has(obj, I915_GEM_OBJECT_IS_PROXY);
285 }
286
287 static inline bool
288 i915_gem_object_never_mmap(const struct drm_i915_gem_object *obj)
289 {
290         return i915_gem_object_type_has(obj, I915_GEM_OBJECT_NO_MMAP);
291 }
292
293 static inline bool
294 i915_gem_object_is_framebuffer(const struct drm_i915_gem_object *obj)
295 {
296         return READ_ONCE(obj->frontbuffer);
297 }
298
299 static inline unsigned int
300 i915_gem_object_get_tiling(const struct drm_i915_gem_object *obj)
301 {
302         return obj->tiling_and_stride & TILING_MASK;
303 }
304
305 static inline bool
306 i915_gem_object_is_tiled(const struct drm_i915_gem_object *obj)
307 {
308         return i915_gem_object_get_tiling(obj) != I915_TILING_NONE;
309 }
310
311 static inline unsigned int
312 i915_gem_object_get_stride(const struct drm_i915_gem_object *obj)
313 {
314         return obj->tiling_and_stride & STRIDE_MASK;
315 }
316
317 static inline unsigned int
318 i915_gem_tile_height(unsigned int tiling)
319 {
320         GEM_BUG_ON(!tiling);
321         return tiling == I915_TILING_Y ? 32 : 8;
322 }
323
324 static inline unsigned int
325 i915_gem_object_get_tile_height(const struct drm_i915_gem_object *obj)
326 {
327         return i915_gem_tile_height(i915_gem_object_get_tiling(obj));
328 }
329
330 static inline unsigned int
331 i915_gem_object_get_tile_row_size(const struct drm_i915_gem_object *obj)
332 {
333         return (i915_gem_object_get_stride(obj) *
334                 i915_gem_object_get_tile_height(obj));
335 }
336
337 int i915_gem_object_set_tiling(struct drm_i915_gem_object *obj,
338                                unsigned int tiling, unsigned int stride);
339
340 struct scatterlist *
341 __i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
342                          struct i915_gem_object_page_iter *iter,
343                          unsigned int n,
344                          unsigned int *offset, bool allow_alloc, bool dma);
345
346 static inline struct scatterlist *
347 i915_gem_object_get_sg(struct drm_i915_gem_object *obj,
348                        unsigned int n,
349                        unsigned int *offset, bool allow_alloc)
350 {
351         return __i915_gem_object_get_sg(obj, &obj->mm.get_page, n, offset, allow_alloc, false);
352 }
353
354 static inline struct scatterlist *
355 i915_gem_object_get_sg_dma(struct drm_i915_gem_object *obj,
356                            unsigned int n,
357                            unsigned int *offset, bool allow_alloc)
358 {
359         return __i915_gem_object_get_sg(obj, &obj->mm.get_dma_page, n, offset, allow_alloc, true);
360 }
361
362 struct page *
363 i915_gem_object_get_page(struct drm_i915_gem_object *obj,
364                          unsigned int n);
365
366 struct page *
367 i915_gem_object_get_dirty_page(struct drm_i915_gem_object *obj,
368                                unsigned int n);
369
370 dma_addr_t
371 i915_gem_object_get_dma_address_len(struct drm_i915_gem_object *obj,
372                                     unsigned long n,
373                                     unsigned int *len);
374
375 dma_addr_t
376 i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
377                                 unsigned long n);
378
379 void __i915_gem_object_set_pages(struct drm_i915_gem_object *obj,
380                                  struct sg_table *pages,
381                                  unsigned int sg_page_sizes);
382
383 int ____i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
384 int __i915_gem_object_get_pages(struct drm_i915_gem_object *obj);
385
386 static inline int __must_check
387 i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
388 {
389         assert_object_held(obj);
390
391         if (atomic_inc_not_zero(&obj->mm.pages_pin_count))
392                 return 0;
393
394         return __i915_gem_object_get_pages(obj);
395 }
396
397 int i915_gem_object_pin_pages_unlocked(struct drm_i915_gem_object *obj);
398
399 static inline bool
400 i915_gem_object_has_pages(struct drm_i915_gem_object *obj)
401 {
402         return !IS_ERR_OR_NULL(READ_ONCE(obj->mm.pages));
403 }
404
405 static inline void
406 __i915_gem_object_pin_pages(struct drm_i915_gem_object *obj)
407 {
408         GEM_BUG_ON(!i915_gem_object_has_pages(obj));
409
410         atomic_inc(&obj->mm.pages_pin_count);
411 }
412
413 static inline bool
414 i915_gem_object_has_pinned_pages(struct drm_i915_gem_object *obj)
415 {
416         return atomic_read(&obj->mm.pages_pin_count);
417 }
418
419 static inline void
420 __i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
421 {
422         GEM_BUG_ON(!i915_gem_object_has_pages(obj));
423         GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
424
425         atomic_dec(&obj->mm.pages_pin_count);
426 }
427
428 static inline void
429 i915_gem_object_unpin_pages(struct drm_i915_gem_object *obj)
430 {
431         __i915_gem_object_unpin_pages(obj);
432 }
433
434 int __i915_gem_object_put_pages(struct drm_i915_gem_object *obj);
435 void i915_gem_object_truncate(struct drm_i915_gem_object *obj);
436 void i915_gem_object_writeback(struct drm_i915_gem_object *obj);
437
438 /**
439  * i915_gem_object_pin_map - return a contiguous mapping of the entire object
440  * @obj: the object to map into kernel address space
441  * @type: the type of mapping, used to select pgprot_t
442  *
443  * Calls i915_gem_object_pin_pages() to prevent reaping of the object's
444  * pages and then returns a contiguous mapping of the backing storage into
445  * the kernel address space. Based on the @type of mapping, the PTE will be
446  * set to either WriteBack or WriteCombine (via pgprot_t).
447  *
448  * The caller is responsible for calling i915_gem_object_unpin_map() when the
449  * mapping is no longer required.
450  *
451  * Returns the pointer through which to access the mapped object, or an
452  * ERR_PTR() on error.
453  */
454 void *__must_check i915_gem_object_pin_map(struct drm_i915_gem_object *obj,
455                                            enum i915_map_type type);
456
457 void *__must_check i915_gem_object_pin_map_unlocked(struct drm_i915_gem_object *obj,
458                                                     enum i915_map_type type);
459
460 void __i915_gem_object_flush_map(struct drm_i915_gem_object *obj,
461                                  unsigned long offset,
462                                  unsigned long size);
463 static inline void i915_gem_object_flush_map(struct drm_i915_gem_object *obj)
464 {
465         __i915_gem_object_flush_map(obj, 0, obj->base.size);
466 }
467
468 /**
469  * i915_gem_object_unpin_map - releases an earlier mapping
470  * @obj: the object to unmap
471  *
472  * After pinning the object and mapping its pages, once you are finished
473  * with your access, call i915_gem_object_unpin_map() to release the pin
474  * upon the mapping. Once the pin count reaches zero, that mapping may be
475  * removed.
476  */
477 static inline void i915_gem_object_unpin_map(struct drm_i915_gem_object *obj)
478 {
479         i915_gem_object_unpin_pages(obj);
480 }
481
482 void __i915_gem_object_release_map(struct drm_i915_gem_object *obj);
483
484 int i915_gem_object_prepare_read(struct drm_i915_gem_object *obj,
485                                  unsigned int *needs_clflush);
486 int i915_gem_object_prepare_write(struct drm_i915_gem_object *obj,
487                                   unsigned int *needs_clflush);
488 #define CLFLUSH_BEFORE  BIT(0)
489 #define CLFLUSH_AFTER   BIT(1)
490 #define CLFLUSH_FLAGS   (CLFLUSH_BEFORE | CLFLUSH_AFTER)
491
492 static inline void
493 i915_gem_object_finish_access(struct drm_i915_gem_object *obj)
494 {
495         i915_gem_object_unpin_pages(obj);
496 }
497
498 static inline struct intel_engine_cs *
499 i915_gem_object_last_write_engine(struct drm_i915_gem_object *obj)
500 {
501         struct intel_engine_cs *engine = NULL;
502         struct dma_fence *fence;
503
504         rcu_read_lock();
505         fence = dma_resv_get_excl_unlocked(obj->base.resv);
506         rcu_read_unlock();
507
508         if (fence && dma_fence_is_i915(fence) && !dma_fence_is_signaled(fence))
509                 engine = to_request(fence)->engine;
510         dma_fence_put(fence);
511
512         return engine;
513 }
514
515 void i915_gem_object_set_cache_coherency(struct drm_i915_gem_object *obj,
516                                          unsigned int cache_level);
517 void i915_gem_object_flush_if_display(struct drm_i915_gem_object *obj);
518 void i915_gem_object_flush_if_display_locked(struct drm_i915_gem_object *obj);
519
520 int __must_check
521 i915_gem_object_set_to_wc_domain(struct drm_i915_gem_object *obj, bool write);
522 int __must_check
523 i915_gem_object_set_to_gtt_domain(struct drm_i915_gem_object *obj, bool write);
524 int __must_check
525 i915_gem_object_set_to_cpu_domain(struct drm_i915_gem_object *obj, bool write);
526 struct i915_vma * __must_check
527 i915_gem_object_pin_to_display_plane(struct drm_i915_gem_object *obj,
528                                      struct i915_gem_ww_ctx *ww,
529                                      u32 alignment,
530                                      const struct i915_ggtt_view *view,
531                                      unsigned int flags);
532
533 void i915_gem_object_make_unshrinkable(struct drm_i915_gem_object *obj);
534 void i915_gem_object_make_shrinkable(struct drm_i915_gem_object *obj);
535 void i915_gem_object_make_purgeable(struct drm_i915_gem_object *obj);
536
537 static inline bool cpu_write_needs_clflush(struct drm_i915_gem_object *obj)
538 {
539         if (obj->cache_dirty)
540                 return false;
541
542         if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_WRITE))
543                 return true;
544
545         /* Currently in use by HW (display engine)? Keep flushed. */
546         return i915_gem_object_is_framebuffer(obj);
547 }
548
549 static inline void __start_cpu_write(struct drm_i915_gem_object *obj)
550 {
551         obj->read_domains = I915_GEM_DOMAIN_CPU;
552         obj->write_domain = I915_GEM_DOMAIN_CPU;
553         if (cpu_write_needs_clflush(obj))
554                 obj->cache_dirty = true;
555 }
556
557 void i915_gem_fence_wait_priority(struct dma_fence *fence,
558                                   const struct i915_sched_attr *attr);
559
560 int i915_gem_object_wait(struct drm_i915_gem_object *obj,
561                          unsigned int flags,
562                          long timeout);
563 int i915_gem_object_wait_priority(struct drm_i915_gem_object *obj,
564                                   unsigned int flags,
565                                   const struct i915_sched_attr *attr);
566
567 void __i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
568                                          enum fb_op_origin origin);
569 void __i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
570                                               enum fb_op_origin origin);
571
572 static inline void
573 i915_gem_object_flush_frontbuffer(struct drm_i915_gem_object *obj,
574                                   enum fb_op_origin origin)
575 {
576         if (unlikely(rcu_access_pointer(obj->frontbuffer)))
577                 __i915_gem_object_flush_frontbuffer(obj, origin);
578 }
579
580 static inline void
581 i915_gem_object_invalidate_frontbuffer(struct drm_i915_gem_object *obj,
582                                        enum fb_op_origin origin)
583 {
584         if (unlikely(rcu_access_pointer(obj->frontbuffer)))
585                 __i915_gem_object_invalidate_frontbuffer(obj, origin);
586 }
587
588 int i915_gem_object_read_from_page(struct drm_i915_gem_object *obj, u64 offset, void *dst, int size);
589
590 bool i915_gem_object_is_shmem(const struct drm_i915_gem_object *obj);
591
592 void __i915_gem_free_object_rcu(struct rcu_head *head);
593
594 void __i915_gem_free_object(struct drm_i915_gem_object *obj);
595
596 bool i915_gem_object_evictable(struct drm_i915_gem_object *obj);
597
598 bool i915_gem_object_migratable(struct drm_i915_gem_object *obj);
599
600 bool i915_gem_object_validates_to_lmem(struct drm_i915_gem_object *obj);
601
602 int i915_gem_object_migrate(struct drm_i915_gem_object *obj,
603                             struct i915_gem_ww_ctx *ww,
604                             enum intel_region_id id);
605
606 bool i915_gem_object_can_migrate(struct drm_i915_gem_object *obj,
607                                  enum intel_region_id id);
608
609 int i915_gem_object_wait_migration(struct drm_i915_gem_object *obj,
610                                    unsigned int flags);
611
612 #ifdef CONFIG_MMU_NOTIFIER
613 static inline bool
614 i915_gem_object_is_userptr(struct drm_i915_gem_object *obj)
615 {
616         return obj->userptr.notifier.mm;
617 }
618
619 int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj);
620 int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj);
621 int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj);
622 #else
623 static inline bool i915_gem_object_is_userptr(struct drm_i915_gem_object *obj) { return false; }
624
625 static inline int i915_gem_object_userptr_submit_init(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
626 static inline int i915_gem_object_userptr_submit_done(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
627 static inline int i915_gem_object_userptr_validate(struct drm_i915_gem_object *obj) { GEM_BUG_ON(1); return -ENODEV; }
628
629 #endif
630
631 #endif