]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/gpu/drm/i915/i915_vma.h
11ce83a8adf03bf565e1b157a5b3e1e27a800134
[karo-tx-linux.git] / drivers / gpu / drm / i915 / i915_vma.h
1 /*
2  * Copyright © 2016 Intel Corporation
3  *
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:
10  *
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
13  * Software.
14  *
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
21  * IN THE SOFTWARE.
22  *
23  */
24
25 #ifndef __I915_VMA_H__
26 #define __I915_VMA_H__
27
28 #include <linux/io-mapping.h>
29
30 #include <drm/drm_mm.h>
31
32 #include "i915_gem_gtt.h"
33 #include "i915_gem_fence_reg.h"
34 #include "i915_gem_object.h"
35 #include "i915_gem_request.h"
36
37
38 enum i915_cache_level;
39
40 /**
41  * A VMA represents a GEM BO that is bound into an address space. Therefore, a
42  * VMA's presence cannot be guaranteed before binding, or after unbinding the
43  * object into/from the address space.
44  *
45  * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
46  * will always be <= an objects lifetime. So object refcounting should cover us.
47  */
48 struct i915_vma {
49         struct drm_mm_node node;
50         struct drm_i915_gem_object *obj;
51         struct i915_address_space *vm;
52         struct drm_i915_fence_reg *fence;
53         struct sg_table *pages;
54         void __iomem *iomap;
55         u64 size;
56         u64 display_alignment;
57
58         u32 fence_size;
59         u32 fence_alignment;
60
61         unsigned int flags;
62         /**
63          * How many users have pinned this object in GTT space. The following
64          * users can each hold at most one reference: pwrite/pread, execbuffer
65          * (objects are not allowed multiple times for the same batchbuffer),
66          * and the framebuffer code. When switching/pageflipping, the
67          * framebuffer code has at most two buffers pinned per crtc.
68          *
69          * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
70          * bits with absolutely no headroom. So use 4 bits.
71          */
72 #define I915_VMA_PIN_MASK 0xf
73 #define I915_VMA_PIN_OVERFLOW   BIT(5)
74
75         /** Flags and address space this VMA is bound to */
76 #define I915_VMA_GLOBAL_BIND    BIT(6)
77 #define I915_VMA_LOCAL_BIND     BIT(7)
78 #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
79
80 #define I915_VMA_GGTT           BIT(8)
81 #define I915_VMA_CAN_FENCE      BIT(9)
82 #define I915_VMA_CLOSED         BIT(10)
83
84         unsigned int active;
85         struct i915_gem_active last_read[I915_NUM_ENGINES];
86         struct i915_gem_active last_fence;
87
88         /**
89          * Support different GGTT views into the same object.
90          * This means there can be multiple VMA mappings per object and per VM.
91          * i915_ggtt_view_type is used to distinguish between those entries.
92          * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
93          * assumed in GEM functions which take no ggtt view parameter.
94          */
95         struct i915_ggtt_view ggtt_view;
96
97         /** This object's place on the active/inactive lists */
98         struct list_head vm_link;
99
100         struct list_head obj_link; /* Link in the object's VMA list */
101         struct rb_node obj_node;
102
103         /** This vma's place in the execbuf reservation list */
104         struct list_head exec_link;
105
106         /** This vma's place in the eviction list */
107         struct list_head evict_link;
108
109         /**
110          * Used for performing relocations during execbuffer insertion.
111          */
112         struct hlist_node exec_node;
113         unsigned long exec_handle;
114         struct drm_i915_gem_exec_object2 *exec_entry;
115 };
116
117 struct i915_vma *
118 i915_vma_instance(struct drm_i915_gem_object *obj,
119                   struct i915_address_space *vm,
120                   const struct i915_ggtt_view *view);
121
122 void i915_vma_unpin_and_release(struct i915_vma **p_vma);
123
124 static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
125 {
126         return vma->flags & I915_VMA_GGTT;
127 }
128
129 static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
130 {
131         return vma->flags & I915_VMA_CAN_FENCE;
132 }
133
134 static inline bool i915_vma_is_closed(const struct i915_vma *vma)
135 {
136         return vma->flags & I915_VMA_CLOSED;
137 }
138
139 static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
140 {
141         return vma->active;
142 }
143
144 static inline bool i915_vma_is_active(const struct i915_vma *vma)
145 {
146         return i915_vma_get_active(vma);
147 }
148
149 static inline void i915_vma_set_active(struct i915_vma *vma,
150                                        unsigned int engine)
151 {
152         vma->active |= BIT(engine);
153 }
154
155 static inline void i915_vma_clear_active(struct i915_vma *vma,
156                                          unsigned int engine)
157 {
158         vma->active &= ~BIT(engine);
159 }
160
161 static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
162                                               unsigned int engine)
163 {
164         return vma->active & BIT(engine);
165 }
166
167 static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
168 {
169         GEM_BUG_ON(!i915_vma_is_ggtt(vma));
170         GEM_BUG_ON(!vma->node.allocated);
171         GEM_BUG_ON(upper_32_bits(vma->node.start));
172         GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
173         return lower_32_bits(vma->node.start);
174 }
175
176 static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
177 {
178         i915_gem_object_get(vma->obj);
179         return vma;
180 }
181
182 static inline void i915_vma_put(struct i915_vma *vma)
183 {
184         i915_gem_object_put(vma->obj);
185 }
186
187 static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
188 {
189         return a - b;
190 }
191
192 static inline long
193 i915_vma_compare(struct i915_vma *vma,
194                  struct i915_address_space *vm,
195                  const struct i915_ggtt_view *view)
196 {
197         ptrdiff_t cmp;
198
199         GEM_BUG_ON(view && !i915_is_ggtt(vm));
200
201         cmp = ptrdiff(vma->vm, vm);
202         if (cmp)
203                 return cmp;
204
205         BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
206         cmp = vma->ggtt_view.type;
207         if (!view)
208                 return cmp;
209
210         cmp -= view->type;
211         if (cmp)
212                 return cmp;
213
214         /* ggtt_view.type also encodes its size so that we both distinguish
215          * different views using it as a "type" and also use a compact (no
216          * accessing of uninitialised padding bytes) memcmp without storing
217          * an extra parameter or adding more code.
218          *
219          * To ensure that the memcmp is valid for all branches of the union,
220          * even though the code looks like it is just comparing one branch,
221          * we assert above that all branches have the same address, and that
222          * each branch has a unique type/size.
223          */
224         BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
225         BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
226         BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
227                      offsetof(typeof(*view), partial));
228         return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
229 }
230
231 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
232                   u32 flags);
233 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
234 bool i915_vma_misplaced(const struct i915_vma *vma,
235                         u64 size, u64 alignment, u64 flags);
236 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
237 int __must_check i915_vma_unbind(struct i915_vma *vma);
238 void i915_vma_close(struct i915_vma *vma);
239
240 int __i915_vma_do_pin(struct i915_vma *vma,
241                       u64 size, u64 alignment, u64 flags);
242 static inline int __must_check
243 i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
244 {
245         BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
246         BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
247         BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
248
249         /* Pin early to prevent the shrinker/eviction logic from destroying
250          * our vma as we insert and bind.
251          */
252         if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
253                 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
254                 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
255                 return 0;
256         }
257
258         return __i915_vma_do_pin(vma, size, alignment, flags);
259 }
260
261 static inline int i915_vma_pin_count(const struct i915_vma *vma)
262 {
263         return vma->flags & I915_VMA_PIN_MASK;
264 }
265
266 static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
267 {
268         return i915_vma_pin_count(vma);
269 }
270
271 static inline void __i915_vma_pin(struct i915_vma *vma)
272 {
273         vma->flags++;
274         GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
275 }
276
277 static inline void __i915_vma_unpin(struct i915_vma *vma)
278 {
279         GEM_BUG_ON(!i915_vma_is_pinned(vma));
280         vma->flags--;
281 }
282
283 static inline void i915_vma_unpin(struct i915_vma *vma)
284 {
285         GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
286         __i915_vma_unpin(vma);
287 }
288
289 /**
290  * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
291  * @vma: VMA to iomap
292  *
293  * The passed in VMA has to be pinned in the global GTT mappable region.
294  * An extra pinning of the VMA is acquired for the return iomapping,
295  * the caller must call i915_vma_unpin_iomap to relinquish the pinning
296  * after the iomapping is no longer required.
297  *
298  * Callers must hold the struct_mutex.
299  *
300  * Returns a valid iomapped pointer or ERR_PTR.
301  */
302 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
303 #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
304
305 /**
306  * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
307  * @vma: VMA to unpin
308  *
309  * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
310  *
311  * Callers must hold the struct_mutex. This function is only valid to be
312  * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
313  */
314 static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
315 {
316         lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
317         GEM_BUG_ON(vma->iomap == NULL);
318         i915_vma_unpin(vma);
319 }
320
321 static inline struct page *i915_vma_first_page(struct i915_vma *vma)
322 {
323         GEM_BUG_ON(!vma->pages);
324         return sg_page(vma->pages->sgl);
325 }
326
327 /**
328  * i915_vma_pin_fence - pin fencing state
329  * @vma: vma to pin fencing for
330  *
331  * This pins the fencing state (whether tiled or untiled) to make sure the
332  * vma (and its object) is ready to be used as a scanout target. Fencing
333  * status must be synchronize first by calling i915_vma_get_fence():
334  *
335  * The resulting fence pin reference must be released again with
336  * i915_vma_unpin_fence().
337  *
338  * Returns:
339  *
340  * True if the vma has a fence, false otherwise.
341  */
342 static inline bool
343 i915_vma_pin_fence(struct i915_vma *vma)
344 {
345         lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
346         if (vma->fence) {
347                 vma->fence->pin_count++;
348                 return true;
349         } else
350                 return false;
351 }
352
353 /**
354  * i915_vma_unpin_fence - unpin fencing state
355  * @vma: vma to unpin fencing for
356  *
357  * This releases the fence pin reference acquired through
358  * i915_vma_pin_fence. It will handle both objects with and without an
359  * attached fence correctly, callers do not need to distinguish this.
360  */
361 static inline void
362 i915_vma_unpin_fence(struct i915_vma *vma)
363 {
364         lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
365         if (vma->fence) {
366                 GEM_BUG_ON(vma->fence->pin_count <= 0);
367                 vma->fence->pin_count--;
368         }
369 }
370
371 #endif
372