blob: ea98e6e4262f354284ead306b3fcac03ad95a8cf [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;
53 struct sg_table *pages;
54 void __iomem *iomap;
55 u64 size;
56 u64 display_alignment;
57
Chris Wilson944397f2017-01-09 16:16:11 +000058 u32 fence_size;
59 u32 fence_alignment;
60
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020061 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];
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +020086 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;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100102 struct hlist_node obj_hash;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200103
Chris Wilson8c45cec2017-06-15 09:14:35 +0100104 /** This vma's place in the execbuf reservation list */
105 struct list_head exec_link;
106
107 /** This vma's place in the eviction list */
108 struct list_head evict_link;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200109
110 /**
111 * Used for performing relocations during execbuffer insertion.
112 */
113 struct hlist_node exec_node;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200114 struct drm_i915_gem_exec_object2 *exec_entry;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100115 u32 exec_handle;
116
117 struct i915_gem_context *ctx;
118 struct hlist_node ctx_node;
119 u32 ctx_handle;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200120};
121
122struct i915_vma *
Chris Wilson718659a2017-01-16 15:21:28 +0000123i915_vma_instance(struct drm_i915_gem_object *obj,
124 struct i915_address_space *vm,
125 const struct i915_ggtt_view *view);
126
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200127void i915_vma_unpin_and_release(struct i915_vma **p_vma);
128
129static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
130{
131 return vma->flags & I915_VMA_GGTT;
132}
133
134static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
135{
136 return vma->flags & I915_VMA_CAN_FENCE;
137}
138
139static inline bool i915_vma_is_closed(const struct i915_vma *vma)
140{
141 return vma->flags & I915_VMA_CLOSED;
142}
143
144static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
145{
146 return vma->active;
147}
148
149static inline bool i915_vma_is_active(const struct i915_vma *vma)
150{
151 return i915_vma_get_active(vma);
152}
153
154static inline void i915_vma_set_active(struct i915_vma *vma,
155 unsigned int engine)
156{
157 vma->active |= BIT(engine);
158}
159
160static inline void i915_vma_clear_active(struct i915_vma *vma,
161 unsigned int engine)
162{
163 vma->active &= ~BIT(engine);
164}
165
166static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
167 unsigned int engine)
168{
169 return vma->active & BIT(engine);
170}
171
172static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
173{
174 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
175 GEM_BUG_ON(!vma->node.allocated);
176 GEM_BUG_ON(upper_32_bits(vma->node.start));
177 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
178 return lower_32_bits(vma->node.start);
179}
180
181static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
182{
183 i915_gem_object_get(vma->obj);
184 return vma;
185}
186
187static inline void i915_vma_put(struct i915_vma *vma)
188{
189 i915_gem_object_put(vma->obj);
190}
191
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000192static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
193{
194 return a - b;
195}
196
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200197static inline long
198i915_vma_compare(struct i915_vma *vma,
199 struct i915_address_space *vm,
200 const struct i915_ggtt_view *view)
201{
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000202 ptrdiff_t cmp;
203
Chris Wilson4d1368f2016-11-03 20:08:52 +0000204 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200205
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000206 cmp = ptrdiff(vma->vm, vm);
207 if (cmp)
208 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200209
Chris Wilson992e4182017-01-14 00:28:23 +0000210 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
211 cmp = vma->ggtt_view.type;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200212 if (!view)
Chris Wilson992e4182017-01-14 00:28:23 +0000213 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200214
Chris Wilson992e4182017-01-14 00:28:23 +0000215 cmp -= view->type;
216 if (cmp)
217 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200218
Chris Wilson992e4182017-01-14 00:28:23 +0000219 /* ggtt_view.type also encodes its size so that we both distinguish
220 * different views using it as a "type" and also use a compact (no
221 * accessing of uninitialised padding bytes) memcmp without storing
222 * an extra parameter or adding more code.
Chris Wilson8bab11932017-01-14 00:28:25 +0000223 *
224 * To ensure that the memcmp is valid for all branches of the union,
225 * even though the code looks like it is just comparing one branch,
226 * we assert above that all branches have the same address, and that
227 * each branch has a unique type/size.
Chris Wilson992e4182017-01-14 00:28:23 +0000228 */
229 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
230 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
Chris Wilson8bab11932017-01-14 00:28:25 +0000231 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
232 offsetof(typeof(*view), partial));
233 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200234}
235
236int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
237 u32 flags);
238bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
Chris Wilson782a3e92017-02-13 17:15:46 +0000239bool i915_vma_misplaced(const struct i915_vma *vma,
240 u64 size, u64 alignment, u64 flags);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200241void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
242int __must_check i915_vma_unbind(struct i915_vma *vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100243void i915_vma_unlink_ctx(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200244void i915_vma_close(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200245
246int __i915_vma_do_pin(struct i915_vma *vma,
247 u64 size, u64 alignment, u64 flags);
248static inline int __must_check
249i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
250{
251 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
252 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
253 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
254
255 /* Pin early to prevent the shrinker/eviction logic from destroying
256 * our vma as we insert and bind.
257 */
Chris Wilson03257012017-01-11 21:09:26 +0000258 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
259 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
260 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200261 return 0;
Chris Wilson03257012017-01-11 21:09:26 +0000262 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200263
264 return __i915_vma_do_pin(vma, size, alignment, flags);
265}
266
267static inline int i915_vma_pin_count(const struct i915_vma *vma)
268{
269 return vma->flags & I915_VMA_PIN_MASK;
270}
271
272static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
273{
274 return i915_vma_pin_count(vma);
275}
276
277static inline void __i915_vma_pin(struct i915_vma *vma)
278{
279 vma->flags++;
280 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
281}
282
283static inline void __i915_vma_unpin(struct i915_vma *vma)
284{
285 GEM_BUG_ON(!i915_vma_is_pinned(vma));
286 vma->flags--;
287}
288
289static inline void i915_vma_unpin(struct i915_vma *vma)
290{
291 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
292 __i915_vma_unpin(vma);
293}
294
295/**
296 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
297 * @vma: VMA to iomap
298 *
299 * The passed in VMA has to be pinned in the global GTT mappable region.
300 * An extra pinning of the VMA is acquired for the return iomapping,
301 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
302 * after the iomapping is no longer required.
303 *
304 * Callers must hold the struct_mutex.
305 *
306 * Returns a valid iomapped pointer or ERR_PTR.
307 */
308void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
309#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
310
311/**
312 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
313 * @vma: VMA to unpin
314 *
315 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
316 *
317 * Callers must hold the struct_mutex. This function is only valid to be
318 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
319 */
320static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
321{
Chris Wilson49d73912016-11-29 09:50:08 +0000322 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200323 GEM_BUG_ON(vma->iomap == NULL);
324 i915_vma_unpin(vma);
325}
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{
Chris Wilson49d73912016-11-29 09:50:08 +0000351 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200352 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{
Chris Wilson49d73912016-11-29 09:50:08 +0000370 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200371 if (vma->fence) {
372 GEM_BUG_ON(vma->fence->pin_count <= 0);
373 vma->fence->pin_count--;
374 }
375}
376
377#endif
378