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