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