2 * Copyright © 2016 Intel Corporation
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:
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
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
25 #ifndef __I915_VMA_H__
26 #define __I915_VMA_H__
28 #include <linux/io-mapping.h>
30 #include <drm/drm_mm.h>
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"
38 enum i915_cache_level;
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.
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.
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;
57 u64 display_alignment;
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.
67 unsigned int open_count;
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.
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.
79 #define I915_VMA_PIN_MASK 0xf
80 #define I915_VMA_PIN_OVERFLOW BIT(5)
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)
87 #define I915_VMA_GGTT BIT(8)
88 #define I915_VMA_CAN_FENCE BIT(9)
89 #define I915_VMA_CLOSED BIT(10)
92 struct i915_gem_active last_read[I915_NUM_ENGINES];
93 struct i915_gem_active last_fence;
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.
102 struct i915_ggtt_view ggtt_view;
104 /** This object's place on the active/inactive lists */
105 struct list_head vm_link;
107 struct list_head obj_link; /* Link in the object's VMA list */
108 struct rb_node obj_node;
109 struct hlist_node obj_hash;
111 /** This vma's place in the execbuf reservation list */
112 struct list_head exec_link;
113 struct list_head reloc_link;
115 /** This vma's place in the eviction list */
116 struct list_head evict_link;
119 * Used for performing relocations during execbuffer insertion.
121 unsigned int *exec_flags;
122 struct hlist_node exec_node;
127 i915_vma_instance(struct drm_i915_gem_object *obj,
128 struct i915_address_space *vm,
129 const struct i915_ggtt_view *view);
131 void i915_vma_unpin_and_release(struct i915_vma **p_vma);
133 static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
135 return vma->flags & I915_VMA_GGTT;
138 static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
140 return vma->flags & I915_VMA_CAN_FENCE;
143 static inline bool i915_vma_is_closed(const struct i915_vma *vma)
145 return vma->flags & I915_VMA_CLOSED;
148 static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
153 static inline bool i915_vma_is_active(const struct i915_vma *vma)
155 return i915_vma_get_active(vma);
158 static inline void i915_vma_set_active(struct i915_vma *vma,
161 vma->active |= BIT(engine);
164 static inline void i915_vma_clear_active(struct i915_vma *vma,
167 vma->active &= ~BIT(engine);
170 static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
173 return vma->active & BIT(engine);
176 static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
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);
185 static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
187 i915_gem_object_get(vma->obj);
191 static inline void i915_vma_put(struct i915_vma *vma)
193 i915_gem_object_put(vma->obj);
196 static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
202 i915_vma_compare(struct i915_vma *vma,
203 struct i915_address_space *vm,
204 const struct i915_ggtt_view *view)
208 GEM_BUG_ON(view && !i915_is_ggtt(vm));
210 cmp = ptrdiff(vma->vm, vm);
214 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
215 cmp = vma->ggtt_view.type;
223 /* 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.
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.
233 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
234 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
235 BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
236 offsetof(typeof(*view), partial));
237 return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
240 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
242 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
243 bool i915_vma_misplaced(const struct i915_vma *vma,
244 u64 size, u64 alignment, u64 flags);
245 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
246 int __must_check i915_vma_unbind(struct i915_vma *vma);
247 void i915_vma_unlink_ctx(struct i915_vma *vma);
248 void i915_vma_close(struct i915_vma *vma);
250 int __i915_vma_do_pin(struct i915_vma *vma,
251 u64 size, u64 alignment, u64 flags);
252 static inline int __must_check
253 i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
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);
259 /* Pin early to prevent the shrinker/eviction logic from destroying
260 * our vma as we insert and bind.
262 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));
268 return __i915_vma_do_pin(vma, size, alignment, flags);
271 static inline int i915_vma_pin_count(const struct i915_vma *vma)
273 return vma->flags & I915_VMA_PIN_MASK;
276 static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
278 return i915_vma_pin_count(vma);
281 static inline void __i915_vma_pin(struct i915_vma *vma)
284 GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
287 static inline void __i915_vma_unpin(struct i915_vma *vma)
292 static inline void i915_vma_unpin(struct i915_vma *vma)
294 GEM_BUG_ON(!i915_vma_is_pinned(vma));
295 GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
296 __i915_vma_unpin(vma);
300 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
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.
308 * Callers must hold the struct_mutex.
310 * Returns a valid iomapped pointer or ERR_PTR.
312 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
313 #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
316 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
319 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
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().
324 static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
326 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
327 GEM_BUG_ON(vma->iomap == NULL);
331 static inline struct page *i915_vma_first_page(struct i915_vma *vma)
333 GEM_BUG_ON(!vma->pages);
334 return sg_page(vma->pages->sgl);
338 * i915_vma_pin_fence - pin fencing state
339 * @vma: vma to pin fencing for
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():
345 * The resulting fence pin reference must be released again with
346 * i915_vma_unpin_fence().
350 * True if the vma has a fence, false otherwise.
353 i915_vma_pin_fence(struct i915_vma *vma)
355 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
357 vma->fence->pin_count++;
364 * i915_vma_unpin_fence - unpin fencing state
365 * @vma: vma to unpin fencing for
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.
372 i915_vma_unpin_fence(struct i915_vma *vma)
374 lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
376 GEM_BUG_ON(vma->fence->pin_count <= 0);
377 vma->fence->pin_count--;