blob: e811067c7724f626c93c441586c3b4855210b225 [file] [log] [blame]
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +02001/*
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;
Chris Wilson95ff7c72017-06-16 15:05:25 +010053 struct reservation_object *resv; /** Alias of obj->resv */
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020054 struct sg_table *pages;
55 void __iomem *iomap;
56 u64 size;
57 u64 display_alignment;
58
Chris Wilson944397f2017-01-09 16:16:11 +000059 u32 fence_size;
60 u32 fence_alignment;
61
Chris Wilson3ffff012017-08-22 12:05:17 +010062 /**
63 * Count of the number of times this vma has been opened by different
64 * handles (but same file) for execbuf, i.e. the number of aliases
65 * that exist in the ctx->handle_vmas LUT for this vma.
66 */
67 unsigned int open_count;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020068 unsigned int flags;
69 /**
70 * How many users have pinned this object in GTT space. The following
71 * users can each hold at most one reference: pwrite/pread, execbuffer
72 * (objects are not allowed multiple times for the same batchbuffer),
73 * and the framebuffer code. When switching/pageflipping, the
74 * framebuffer code has at most two buffers pinned per crtc.
75 *
76 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
77 * bits with absolutely no headroom. So use 4 bits.
78 */
79#define I915_VMA_PIN_MASK 0xf
80#define I915_VMA_PIN_OVERFLOW BIT(5)
81
82 /** Flags and address space this VMA is bound to */
83#define I915_VMA_GLOBAL_BIND BIT(6)
84#define I915_VMA_LOCAL_BIND BIT(7)
85#define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
86
87#define I915_VMA_GGTT BIT(8)
88#define I915_VMA_CAN_FENCE BIT(9)
89#define I915_VMA_CLOSED BIT(10)
90
91 unsigned int active;
92 struct i915_gem_active last_read[I915_NUM_ENGINES];
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020093 struct i915_gem_active last_fence;
94
95 /**
96 * Support different GGTT views into the same object.
97 * This means there can be multiple VMA mappings per object and per VM.
98 * i915_ggtt_view_type is used to distinguish between those entries.
99 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
100 * assumed in GEM functions which take no ggtt view parameter.
101 */
102 struct i915_ggtt_view ggtt_view;
103
104 /** This object's place on the active/inactive lists */
105 struct list_head vm_link;
106
107 struct list_head obj_link; /* Link in the object's VMA list */
108 struct rb_node obj_node;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100109 struct hlist_node obj_hash;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200110
Chris Wilson8c45cec2017-06-15 09:14:35 +0100111 /** This vma's place in the execbuf reservation list */
112 struct list_head exec_link;
Chris Wilson2889caa2017-06-16 15:05:19 +0100113 struct list_head reloc_link;
Chris Wilson8c45cec2017-06-15 09:14:35 +0100114
115 /** This vma's place in the eviction list */
116 struct list_head evict_link;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200117
118 /**
119 * Used for performing relocations during execbuffer insertion.
120 */
Chris Wilsonc7c6e462017-08-16 09:52:06 +0100121 unsigned int *exec_flags;
Chris Wilson95ff7c72017-06-16 15:05:25 +0100122 struct hlist_node exec_node;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100123 u32 exec_handle;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200124};
125
126struct i915_vma *
Chris Wilson718659a2017-01-16 15:21:28 +0000127i915_vma_instance(struct drm_i915_gem_object *obj,
128 struct i915_address_space *vm,
129 const struct i915_ggtt_view *view);
130
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200131void i915_vma_unpin_and_release(struct i915_vma **p_vma);
132
133static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
134{
135 return vma->flags & I915_VMA_GGTT;
136}
137
138static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
139{
140 return vma->flags & I915_VMA_CAN_FENCE;
141}
142
143static inline bool i915_vma_is_closed(const struct i915_vma *vma)
144{
145 return vma->flags & I915_VMA_CLOSED;
146}
147
148static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
149{
150 return vma->active;
151}
152
153static inline bool i915_vma_is_active(const struct i915_vma *vma)
154{
155 return i915_vma_get_active(vma);
156}
157
158static inline void i915_vma_set_active(struct i915_vma *vma,
159 unsigned int engine)
160{
161 vma->active |= BIT(engine);
162}
163
164static inline void i915_vma_clear_active(struct i915_vma *vma,
165 unsigned int engine)
166{
167 vma->active &= ~BIT(engine);
168}
169
170static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
171 unsigned int engine)
172{
173 return vma->active & BIT(engine);
174}
175
176static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
177{
178 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
179 GEM_BUG_ON(!vma->node.allocated);
180 GEM_BUG_ON(upper_32_bits(vma->node.start));
181 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
182 return lower_32_bits(vma->node.start);
183}
184
185static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
186{
187 i915_gem_object_get(vma->obj);
188 return vma;
189}
190
191static inline void i915_vma_put(struct i915_vma *vma)
192{
193 i915_gem_object_put(vma->obj);
194}
195
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000196static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
197{
198 return a - b;
199}
200
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200201static inline long
202i915_vma_compare(struct i915_vma *vma,
203 struct i915_address_space *vm,
204 const struct i915_ggtt_view *view)
205{
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000206 ptrdiff_t cmp;
207
Chris Wilson4d1368f2016-11-03 20:08:52 +0000208 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200209
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000210 cmp = ptrdiff(vma->vm, vm);
211 if (cmp)
212 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200213
Chris Wilson992e4182017-01-14 00:28:23 +0000214 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
215 cmp = vma->ggtt_view.type;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200216 if (!view)
Chris Wilson992e4182017-01-14 00:28:23 +0000217 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200218
Chris Wilson992e4182017-01-14 00:28:23 +0000219 cmp -= view->type;
220 if (cmp)
221 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200222
Chris Wilson992e4182017-01-14 00:28:23 +0000223 /* ggtt_view.type also encodes its size so that we both distinguish
224 * different views using it as a "type" and also use a compact (no
225 * accessing of uninitialised padding bytes) memcmp without storing
226 * an extra parameter or adding more code.
Chris Wilson8bab11932017-01-14 00:28:25 +0000227 *
228 * To ensure that the memcmp is valid for all branches of the union,
229 * even though the code looks like it is just comparing one branch,
230 * we assert above that all branches have the same address, and that
231 * each branch has a unique type/size.
Chris Wilson992e4182017-01-14 00:28:23 +0000232 */
233 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
234 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
Chris Wilson8bab11932017-01-14 00:28:25 +0000235 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
236 offsetof(typeof(*view), partial));
237 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200238}
239
240int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
241 u32 flags);
242bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
Chris Wilson782a3e92017-02-13 17:15:46 +0000243bool i915_vma_misplaced(const struct i915_vma *vma,
244 u64 size, u64 alignment, u64 flags);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200245void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
246int __must_check i915_vma_unbind(struct i915_vma *vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100247void i915_vma_unlink_ctx(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200248void i915_vma_close(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200249
250int __i915_vma_do_pin(struct i915_vma *vma,
251 u64 size, u64 alignment, u64 flags);
252static inline int __must_check
253i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
254{
255 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
256 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
257 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
258
259 /* Pin early to prevent the shrinker/eviction logic from destroying
260 * our vma as we insert and bind.
261 */
Chris Wilson03257012017-01-11 21:09:26 +0000262 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
263 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
264 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200265 return 0;
Chris Wilson03257012017-01-11 21:09:26 +0000266 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200267
268 return __i915_vma_do_pin(vma, size, alignment, flags);
269}
270
271static inline int i915_vma_pin_count(const struct i915_vma *vma)
272{
273 return vma->flags & I915_VMA_PIN_MASK;
274}
275
276static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
277{
278 return i915_vma_pin_count(vma);
279}
280
281static inline void __i915_vma_pin(struct i915_vma *vma)
282{
283 vma->flags++;
284 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
285}
286
287static inline void __i915_vma_unpin(struct i915_vma *vma)
288{
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200289 vma->flags--;
290}
291
292static inline void i915_vma_unpin(struct i915_vma *vma)
293{
Chris Wilson67fddd92017-07-21 15:50:34 +0100294 GEM_BUG_ON(!i915_vma_is_pinned(vma));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200295 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
296 __i915_vma_unpin(vma);
297}
298
299/**
300 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
301 * @vma: VMA to iomap
302 *
303 * The passed in VMA has to be pinned in the global GTT mappable region.
304 * An extra pinning of the VMA is acquired for the return iomapping,
305 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
306 * after the iomapping is no longer required.
307 *
308 * Callers must hold the struct_mutex.
309 *
310 * Returns a valid iomapped pointer or ERR_PTR.
311 */
312void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
313#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
314
315/**
316 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
317 * @vma: VMA to unpin
318 *
319 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
320 *
321 * Callers must hold the struct_mutex. This function is only valid to be
322 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
323 */
324static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
325{
Chris Wilson49d73912016-11-29 09:50:08 +0000326 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200327 GEM_BUG_ON(vma->iomap == NULL);
328 i915_vma_unpin(vma);
329}
330
331static inline struct page *i915_vma_first_page(struct i915_vma *vma)
332{
333 GEM_BUG_ON(!vma->pages);
334 return sg_page(vma->pages->sgl);
335}
336
337/**
338 * i915_vma_pin_fence - pin fencing state
339 * @vma: vma to pin fencing for
340 *
341 * This pins the fencing state (whether tiled or untiled) to make sure the
342 * vma (and its object) is ready to be used as a scanout target. Fencing
343 * status must be synchronize first by calling i915_vma_get_fence():
344 *
345 * The resulting fence pin reference must be released again with
346 * i915_vma_unpin_fence().
347 *
348 * Returns:
349 *
350 * True if the vma has a fence, false otherwise.
351 */
352static inline bool
353i915_vma_pin_fence(struct i915_vma *vma)
354{
Chris Wilson49d73912016-11-29 09:50:08 +0000355 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200356 if (vma->fence) {
357 vma->fence->pin_count++;
358 return true;
359 } else
360 return false;
361}
362
363/**
364 * i915_vma_unpin_fence - unpin fencing state
365 * @vma: vma to unpin fencing for
366 *
367 * This releases the fence pin reference acquired through
368 * i915_vma_pin_fence. It will handle both objects with and without an
369 * attached fence correctly, callers do not need to distinguish this.
370 */
371static inline void
372i915_vma_unpin_fence(struct i915_vma *vma)
373{
Chris Wilson49d73912016-11-29 09:50:08 +0000374 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200375 if (vma->fence) {
376 GEM_BUG_ON(vma->fence->pin_count <= 0);
377 vma->fence->pin_count--;
378 }
379}
380
381#endif
382