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