]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/gpu/drm/i915/gem/i915_gem_object_types.h
Merge tag 'usercopy-v5.4-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
[linux.git] / drivers / gpu / drm / i915 / gem / i915_gem_object_types.h
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2016 Intel Corporation
5  */
6
7 #ifndef __I915_GEM_OBJECT_TYPES_H__
8 #define __I915_GEM_OBJECT_TYPES_H__
9
10 #include <drm/drm_gem.h>
11
12 #include "i915_active.h"
13 #include "i915_selftest.h"
14
15 struct drm_i915_gem_object;
16 struct intel_fronbuffer;
17
18 /*
19  * struct i915_lut_handle tracks the fast lookups from handle to vma used
20  * for execbuf. Although we use a radixtree for that mapping, in order to
21  * remove them as the object or context is closed, we need a secondary list
22  * and a translation entry (i915_lut_handle).
23  */
24 struct i915_lut_handle {
25         struct list_head obj_link;
26         struct i915_gem_context *ctx;
27         u32 handle;
28 };
29
30 struct drm_i915_gem_object_ops {
31         unsigned int flags;
32 #define I915_GEM_OBJECT_HAS_STRUCT_PAGE BIT(0)
33 #define I915_GEM_OBJECT_IS_SHRINKABLE   BIT(1)
34 #define I915_GEM_OBJECT_IS_PROXY        BIT(2)
35 #define I915_GEM_OBJECT_ASYNC_CANCEL    BIT(3)
36
37         /* Interface between the GEM object and its backing storage.
38          * get_pages() is called once prior to the use of the associated set
39          * of pages before to binding them into the GTT, and put_pages() is
40          * called after we no longer need them. As we expect there to be
41          * associated cost with migrating pages between the backing storage
42          * and making them available for the GPU (e.g. clflush), we may hold
43          * onto the pages after they are no longer referenced by the GPU
44          * in case they may be used again shortly (for example migrating the
45          * pages to a different memory domain within the GTT). put_pages()
46          * will therefore most likely be called when the object itself is
47          * being released or under memory pressure (where we attempt to
48          * reap pages for the shrinker).
49          */
50         int (*get_pages)(struct drm_i915_gem_object *obj);
51         void (*put_pages)(struct drm_i915_gem_object *obj,
52                           struct sg_table *pages);
53         void (*truncate)(struct drm_i915_gem_object *obj);
54         void (*writeback)(struct drm_i915_gem_object *obj);
55
56         int (*pwrite)(struct drm_i915_gem_object *obj,
57                       const struct drm_i915_gem_pwrite *arg);
58
59         int (*dmabuf_export)(struct drm_i915_gem_object *obj);
60         void (*release)(struct drm_i915_gem_object *obj);
61 };
62
63 struct drm_i915_gem_object {
64         struct drm_gem_object base;
65
66         const struct drm_i915_gem_object_ops *ops;
67
68         struct {
69                 /**
70                  * @vma.lock: protect the list/tree of vmas
71                  */
72                 spinlock_t lock;
73
74                 /**
75                  * @vma.list: List of VMAs backed by this object
76                  *
77                  * The VMA on this list are ordered by type, all GGTT vma are
78                  * placed at the head and all ppGTT vma are placed at the tail.
79                  * The different types of GGTT vma are unordered between
80                  * themselves, use the @vma.tree (which has a defined order
81                  * between all VMA) to quickly find an exact match.
82                  */
83                 struct list_head list;
84
85                 /**
86                  * @vma.tree: Ordered tree of VMAs backed by this object
87                  *
88                  * All VMA created for this object are placed in the @vma.tree
89                  * for fast retrieval via a binary search in
90                  * i915_vma_instance(). They are also added to @vma.list for
91                  * easy iteration.
92                  */
93                 struct rb_root tree;
94         } vma;
95
96         /**
97          * @lut_list: List of vma lookup entries in use for this object.
98          *
99          * If this object is closed, we need to remove all of its VMA from
100          * the fast lookup index in associated contexts; @lut_list provides
101          * this translation from object to context->handles_vma.
102          */
103         struct list_head lut_list;
104
105         /** Stolen memory for this object, instead of being backed by shmem. */
106         struct drm_mm_node *stolen;
107         union {
108                 struct rcu_head rcu;
109                 struct llist_node freed;
110         };
111
112         /**
113          * Whether the object is currently in the GGTT mmap.
114          */
115         unsigned int userfault_count;
116         struct list_head userfault_link;
117
118         I915_SELFTEST_DECLARE(struct list_head st_link);
119
120         /*
121          * Is the object to be mapped as read-only to the GPU
122          * Only honoured if hardware has relevant pte bit
123          */
124         unsigned int cache_level:3;
125         unsigned int cache_coherent:2;
126 #define I915_BO_CACHE_COHERENT_FOR_READ BIT(0)
127 #define I915_BO_CACHE_COHERENT_FOR_WRITE BIT(1)
128         unsigned int cache_dirty:1;
129
130         /**
131          * @read_domains: Read memory domains.
132          *
133          * These monitor which caches contain read/write data related to the
134          * object. When transitioning from one set of domains to another,
135          * the driver is called to ensure that caches are suitably flushed and
136          * invalidated.
137          */
138         u16 read_domains;
139
140         /**
141          * @write_domain: Corresponding unique write memory domain.
142          */
143         u16 write_domain;
144
145         struct intel_frontbuffer *frontbuffer;
146
147         /** Current tiling stride for the object, if it's tiled. */
148         unsigned int tiling_and_stride;
149 #define FENCE_MINIMUM_STRIDE 128 /* See i915_tiling_ok() */
150 #define TILING_MASK (FENCE_MINIMUM_STRIDE - 1)
151 #define STRIDE_MASK (~TILING_MASK)
152
153         /** Count of VMA actually bound by this object */
154         atomic_t bind_count;
155         /** Count of how many global VMA are currently pinned for use by HW */
156         unsigned int pin_global;
157
158         struct {
159                 struct mutex lock; /* protects the pages and their use */
160                 atomic_t pages_pin_count;
161
162                 struct sg_table *pages;
163                 void *mapping;
164
165                 /* TODO: whack some of this into the error state */
166                 struct i915_page_sizes {
167                         /**
168                          * The sg mask of the pages sg_table. i.e the mask of
169                          * of the lengths for each sg entry.
170                          */
171                         unsigned int phys;
172
173                         /**
174                          * The gtt page sizes we are allowed to use given the
175                          * sg mask and the supported page sizes. This will
176                          * express the smallest unit we can use for the whole
177                          * object, as well as the larger sizes we may be able
178                          * to use opportunistically.
179                          */
180                         unsigned int sg;
181
182                         /**
183                          * The actual gtt page size usage. Since we can have
184                          * multiple vma associated with this object we need to
185                          * prevent any trampling of state, hence a copy of this
186                          * struct also lives in each vma, therefore the gtt
187                          * value here should only be read/write through the vma.
188                          */
189                         unsigned int gtt;
190                 } page_sizes;
191
192                 I915_SELFTEST_DECLARE(unsigned int page_mask);
193
194                 struct i915_gem_object_page_iter {
195                         struct scatterlist *sg_pos;
196                         unsigned int sg_idx; /* in pages, but 32bit eek! */
197
198                         struct radix_tree_root radix;
199                         struct mutex lock; /* protects this cache */
200                 } get_page;
201
202                 /**
203                  * Element within i915->mm.unbound_list or i915->mm.bound_list,
204                  * locked by i915->mm.obj_lock.
205                  */
206                 struct list_head link;
207
208                 /**
209                  * Advice: are the backing pages purgeable?
210                  */
211                 unsigned int madv:2;
212
213                 /**
214                  * This is set if the object has been written to since the
215                  * pages were last acquired.
216                  */
217                 bool dirty:1;
218
219                 /**
220                  * This is set if the object has been pinned due to unknown
221                  * swizzling.
222                  */
223                 bool quirked:1;
224         } mm;
225
226         /** Record of address bit 17 of each page at last unbind. */
227         unsigned long *bit_17;
228
229         union {
230                 struct i915_gem_userptr {
231                         uintptr_t ptr;
232
233                         struct i915_mm_struct *mm;
234                         struct i915_mmu_object *mmu_object;
235                         struct work_struct *work;
236                 } userptr;
237
238                 unsigned long scratch;
239
240                 void *gvt_info;
241         };
242
243         /** for phys allocated objects */
244         struct drm_dma_handle *phys_handle;
245 };
246
247 static inline struct drm_i915_gem_object *
248 to_intel_bo(struct drm_gem_object *gem)
249 {
250         /* Assert that to_intel_bo(NULL) == NULL */
251         BUILD_BUG_ON(offsetof(struct drm_i915_gem_object, base));
252
253         return container_of(gem, struct drm_i915_gem_object, base);
254 }
255
256 #endif