blob: 04d7a5da70fd8621b78cd81f908fd5c5b24fa4bd [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;
Chris Wilson2889caa2017-06-16 15:05:19 +0100106 struct list_head reloc_link;
Chris Wilson8c45cec2017-06-15 09:14:35 +0100107
108 /** This vma's place in the eviction list */
109 struct list_head evict_link;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200110
111 /**
112 * Used for performing relocations during execbuffer insertion.
113 */
114 struct hlist_node exec_node;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200115 struct drm_i915_gem_exec_object2 *exec_entry;
Chris Wilson4ff4b442017-06-16 15:05:16 +0100116 u32 exec_handle;
117
118 struct i915_gem_context *ctx;
119 struct hlist_node ctx_node;
120 u32 ctx_handle;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200121};
122
123struct i915_vma *
Chris Wilson718659a2017-01-16 15:21:28 +0000124i915_vma_instance(struct drm_i915_gem_object *obj,
125 struct i915_address_space *vm,
126 const struct i915_ggtt_view *view);
127
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200128void i915_vma_unpin_and_release(struct i915_vma **p_vma);
129
130static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
131{
132 return vma->flags & I915_VMA_GGTT;
133}
134
135static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
136{
137 return vma->flags & I915_VMA_CAN_FENCE;
138}
139
140static inline bool i915_vma_is_closed(const struct i915_vma *vma)
141{
142 return vma->flags & I915_VMA_CLOSED;
143}
144
145static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
146{
147 return vma->active;
148}
149
150static inline bool i915_vma_is_active(const struct i915_vma *vma)
151{
152 return i915_vma_get_active(vma);
153}
154
155static inline void i915_vma_set_active(struct i915_vma *vma,
156 unsigned int engine)
157{
158 vma->active |= BIT(engine);
159}
160
161static inline void i915_vma_clear_active(struct i915_vma *vma,
162 unsigned int engine)
163{
164 vma->active &= ~BIT(engine);
165}
166
167static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
168 unsigned int engine)
169{
170 return vma->active & BIT(engine);
171}
172
173static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
174{
175 GEM_BUG_ON(!i915_vma_is_ggtt(vma));
176 GEM_BUG_ON(!vma->node.allocated);
177 GEM_BUG_ON(upper_32_bits(vma->node.start));
178 GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
179 return lower_32_bits(vma->node.start);
180}
181
182static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
183{
184 i915_gem_object_get(vma->obj);
185 return vma;
186}
187
188static inline void i915_vma_put(struct i915_vma *vma)
189{
190 i915_gem_object_put(vma->obj);
191}
192
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000193static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
194{
195 return a - b;
196}
197
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200198static inline long
199i915_vma_compare(struct i915_vma *vma,
200 struct i915_address_space *vm,
201 const struct i915_ggtt_view *view)
202{
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000203 ptrdiff_t cmp;
204
Chris Wilson4d1368f2016-11-03 20:08:52 +0000205 GEM_BUG_ON(view && !i915_is_ggtt(vm));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200206
Tvrtko Ursulin2bf0d262016-12-13 14:37:27 +0000207 cmp = ptrdiff(vma->vm, vm);
208 if (cmp)
209 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200210
Chris Wilson992e4182017-01-14 00:28:23 +0000211 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
212 cmp = vma->ggtt_view.type;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200213 if (!view)
Chris Wilson992e4182017-01-14 00:28:23 +0000214 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200215
Chris Wilson992e4182017-01-14 00:28:23 +0000216 cmp -= view->type;
217 if (cmp)
218 return cmp;
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200219
Chris Wilson992e4182017-01-14 00:28:23 +0000220 /* ggtt_view.type also encodes its size so that we both distinguish
221 * different views using it as a "type" and also use a compact (no
222 * accessing of uninitialised padding bytes) memcmp without storing
223 * an extra parameter or adding more code.
Chris Wilson8bab11932017-01-14 00:28:25 +0000224 *
225 * To ensure that the memcmp is valid for all branches of the union,
226 * even though the code looks like it is just comparing one branch,
227 * we assert above that all branches have the same address, and that
228 * each branch has a unique type/size.
Chris Wilson992e4182017-01-14 00:28:23 +0000229 */
230 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
231 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
Chris Wilson8bab11932017-01-14 00:28:25 +0000232 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
233 offsetof(typeof(*view), partial));
234 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200235}
236
237int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
238 u32 flags);
239bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
Chris Wilson782a3e92017-02-13 17:15:46 +0000240bool i915_vma_misplaced(const struct i915_vma *vma,
241 u64 size, u64 alignment, u64 flags);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200242void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
243int __must_check i915_vma_unbind(struct i915_vma *vma);
Chris Wilson4ff4b442017-06-16 15:05:16 +0100244void i915_vma_unlink_ctx(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200245void i915_vma_close(struct i915_vma *vma);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200246
247int __i915_vma_do_pin(struct i915_vma *vma,
248 u64 size, u64 alignment, u64 flags);
249static inline int __must_check
250i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
251{
252 BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
253 BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
254 BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
255
256 /* Pin early to prevent the shrinker/eviction logic from destroying
257 * our vma as we insert and bind.
258 */
Chris Wilson03257012017-01-11 21:09:26 +0000259 if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
260 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
261 GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200262 return 0;
Chris Wilson03257012017-01-11 21:09:26 +0000263 }
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200264
265 return __i915_vma_do_pin(vma, size, alignment, flags);
266}
267
268static inline int i915_vma_pin_count(const struct i915_vma *vma)
269{
270 return vma->flags & I915_VMA_PIN_MASK;
271}
272
273static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
274{
275 return i915_vma_pin_count(vma);
276}
277
278static inline void __i915_vma_pin(struct i915_vma *vma)
279{
280 vma->flags++;
281 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
282}
283
284static inline void __i915_vma_unpin(struct i915_vma *vma)
285{
286 GEM_BUG_ON(!i915_vma_is_pinned(vma));
287 vma->flags--;
288}
289
290static inline void i915_vma_unpin(struct i915_vma *vma)
291{
292 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
293 __i915_vma_unpin(vma);
294}
295
296/**
297 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
298 * @vma: VMA to iomap
299 *
300 * The passed in VMA has to be pinned in the global GTT mappable region.
301 * An extra pinning of the VMA is acquired for the return iomapping,
302 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
303 * after the iomapping is no longer required.
304 *
305 * Callers must hold the struct_mutex.
306 *
307 * Returns a valid iomapped pointer or ERR_PTR.
308 */
309void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
310#define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
311
312/**
313 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
314 * @vma: VMA to unpin
315 *
316 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
317 *
318 * Callers must hold the struct_mutex. This function is only valid to be
319 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
320 */
321static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
322{
Chris Wilson49d73912016-11-29 09:50:08 +0000323 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200324 GEM_BUG_ON(vma->iomap == NULL);
325 i915_vma_unpin(vma);
326}
327
328static inline struct page *i915_vma_first_page(struct i915_vma *vma)
329{
330 GEM_BUG_ON(!vma->pages);
331 return sg_page(vma->pages->sgl);
332}
333
334/**
335 * i915_vma_pin_fence - pin fencing state
336 * @vma: vma to pin fencing for
337 *
338 * This pins the fencing state (whether tiled or untiled) to make sure the
339 * vma (and its object) is ready to be used as a scanout target. Fencing
340 * status must be synchronize first by calling i915_vma_get_fence():
341 *
342 * The resulting fence pin reference must be released again with
343 * i915_vma_unpin_fence().
344 *
345 * Returns:
346 *
347 * True if the vma has a fence, false otherwise.
348 */
349static inline bool
350i915_vma_pin_fence(struct i915_vma *vma)
351{
Chris Wilson49d73912016-11-29 09:50:08 +0000352 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200353 if (vma->fence) {
354 vma->fence->pin_count++;
355 return true;
356 } else
357 return false;
358}
359
360/**
361 * i915_vma_unpin_fence - unpin fencing state
362 * @vma: vma to unpin fencing for
363 *
364 * This releases the fence pin reference acquired through
365 * i915_vma_pin_fence. It will handle both objects with and without an
366 * attached fence correctly, callers do not need to distinguish this.
367 */
368static inline void
369i915_vma_unpin_fence(struct i915_vma *vma)
370{
Chris Wilson49d73912016-11-29 09:50:08 +0000371 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
Joonas Lahtinenb42fe9c2016-11-11 12:43:54 +0200372 if (vma->fence) {
373 GEM_BUG_ON(vma->fence->pin_count <= 0);
374 vma->fence->pin_count--;
375 }
376}
377
378#endif
379