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>
29 #include <linux/rbtree.h>
31 #include <drm/drm_mm.h>
33 #include "i915_gem_gtt.h"
34 #include "i915_gem_fence_reg.h"
35 #include "i915_gem_object.h"
37 #include "i915_request.h"
39 enum i915_cache_level
;
42 * A VMA represents a GEM BO that is bound into an address space. Therefore, a
43 * VMA's presence cannot be guaranteed before binding, or after unbinding the
44 * object into/from the address space.
46 * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
47 * will always be <= an objects lifetime. So object refcounting should cover us.
50 struct drm_mm_node node
;
51 struct drm_i915_gem_object
*obj
;
52 struct i915_address_space
*vm
;
53 const struct i915_vma_ops
*ops
;
54 struct drm_i915_fence_reg
*fence
;
55 struct reservation_object
*resv
; /** Alias of obj->resv */
56 struct sg_table
*pages
;
58 void *private; /* owned by creator */
60 u64 display_alignment
;
61 struct i915_page_sizes page_sizes
;
67 * Count of the number of times this vma has been opened by different
68 * handles (but same file) for execbuf, i.e. the number of aliases
69 * that exist in the ctx->handle_vmas LUT for this vma.
71 unsigned int open_count
;
74 * How many users have pinned this object in GTT space. The following
75 * users can each hold at most one reference: pwrite/pread, execbuffer
76 * (objects are not allowed multiple times for the same batchbuffer),
77 * and the framebuffer code. When switching/pageflipping, the
78 * framebuffer code has at most two buffers pinned per crtc.
80 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
81 * bits with absolutely no headroom. So use 4 bits.
83 #define I915_VMA_PIN_MASK 0xf
84 #define I915_VMA_PIN_OVERFLOW BIT(5)
86 /** Flags and address space this VMA is bound to */
87 #define I915_VMA_GLOBAL_BIND BIT(6)
88 #define I915_VMA_LOCAL_BIND BIT(7)
89 #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
91 #define I915_VMA_GGTT BIT(8)
92 #define I915_VMA_CAN_FENCE BIT(9)
93 #define I915_VMA_CLOSED BIT(10)
94 #define I915_VMA_USERFAULT_BIT 11
95 #define I915_VMA_USERFAULT BIT(I915_VMA_USERFAULT_BIT)
96 #define I915_VMA_GGTT_WRITE BIT(12)
98 unsigned int active_count
;
99 struct rb_root active
;
100 struct i915_gem_active last_active
;
101 struct i915_gem_active last_fence
;
104 * Support different GGTT views into the same object.
105 * This means there can be multiple VMA mappings per object and per VM.
106 * i915_ggtt_view_type is used to distinguish between those entries.
107 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
108 * assumed in GEM functions which take no ggtt view parameter.
110 struct i915_ggtt_view ggtt_view
;
112 /** This object's place on the active/inactive lists */
113 struct list_head vm_link
;
115 struct list_head obj_link
; /* Link in the object's VMA list */
116 struct rb_node obj_node
;
117 struct hlist_node obj_hash
;
119 /** This vma's place in the execbuf reservation list */
120 struct list_head exec_link
;
121 struct list_head reloc_link
;
123 /** This vma's place in the eviction list */
124 struct list_head evict_link
;
126 struct list_head closed_link
;
129 * Used for performing relocations during execbuffer insertion.
131 unsigned int *exec_flags
;
132 struct hlist_node exec_node
;
137 i915_vma_instance(struct drm_i915_gem_object
*obj
,
138 struct i915_address_space
*vm
,
139 const struct i915_ggtt_view
*view
);
141 void i915_vma_unpin_and_release(struct i915_vma
**p_vma
);
143 static inline bool i915_vma_is_active(struct i915_vma
*vma
)
145 return vma
->active_count
;
148 int __must_check
i915_vma_move_to_active(struct i915_vma
*vma
,
149 struct i915_request
*rq
,
152 static inline bool i915_vma_is_ggtt(const struct i915_vma
*vma
)
154 return vma
->flags
& I915_VMA_GGTT
;
157 static inline bool i915_vma_has_ggtt_write(const struct i915_vma
*vma
)
159 return vma
->flags
& I915_VMA_GGTT_WRITE
;
162 static inline void i915_vma_set_ggtt_write(struct i915_vma
*vma
)
164 GEM_BUG_ON(!i915_vma_is_ggtt(vma
));
165 vma
->flags
|= I915_VMA_GGTT_WRITE
;
168 static inline void i915_vma_unset_ggtt_write(struct i915_vma
*vma
)
170 vma
->flags
&= ~I915_VMA_GGTT_WRITE
;
173 void i915_vma_flush_writes(struct i915_vma
*vma
);
175 static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma
*vma
)
177 return vma
->flags
& I915_VMA_CAN_FENCE
;
180 static inline bool i915_vma_is_closed(const struct i915_vma
*vma
)
182 return vma
->flags
& I915_VMA_CLOSED
;
185 static inline bool i915_vma_set_userfault(struct i915_vma
*vma
)
187 GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma
));
188 return __test_and_set_bit(I915_VMA_USERFAULT_BIT
, &vma
->flags
);
191 static inline void i915_vma_unset_userfault(struct i915_vma
*vma
)
193 return __clear_bit(I915_VMA_USERFAULT_BIT
, &vma
->flags
);
196 static inline bool i915_vma_has_userfault(const struct i915_vma
*vma
)
198 return test_bit(I915_VMA_USERFAULT_BIT
, &vma
->flags
);
201 static inline u32
i915_ggtt_offset(const struct i915_vma
*vma
)
203 GEM_BUG_ON(!i915_vma_is_ggtt(vma
));
204 GEM_BUG_ON(!vma
->node
.allocated
);
205 GEM_BUG_ON(upper_32_bits(vma
->node
.start
));
206 GEM_BUG_ON(upper_32_bits(vma
->node
.start
+ vma
->node
.size
- 1));
207 return lower_32_bits(vma
->node
.start
);
210 static inline struct i915_vma
*i915_vma_get(struct i915_vma
*vma
)
212 i915_gem_object_get(vma
->obj
);
216 static inline void i915_vma_put(struct i915_vma
*vma
)
218 i915_gem_object_put(vma
->obj
);
221 static __always_inline
ptrdiff_t ptrdiff(const void *a
, const void *b
)
227 i915_vma_compare(struct i915_vma
*vma
,
228 struct i915_address_space
*vm
,
229 const struct i915_ggtt_view
*view
)
233 GEM_BUG_ON(view
&& !i915_is_ggtt(vm
));
235 cmp
= ptrdiff(vma
->vm
, vm
);
239 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL
!= 0);
240 cmp
= vma
->ggtt_view
.type
;
248 /* ggtt_view.type also encodes its size so that we both distinguish
249 * different views using it as a "type" and also use a compact (no
250 * accessing of uninitialised padding bytes) memcmp without storing
251 * an extra parameter or adding more code.
253 * To ensure that the memcmp is valid for all branches of the union,
254 * even though the code looks like it is just comparing one branch,
255 * we assert above that all branches have the same address, and that
256 * each branch has a unique type/size.
258 BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL
>= I915_GGTT_VIEW_PARTIAL
);
259 BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL
>= I915_GGTT_VIEW_ROTATED
);
260 BUILD_BUG_ON(offsetof(typeof(*view
), rotated
) !=
261 offsetof(typeof(*view
), partial
));
262 return memcmp(&vma
->ggtt_view
.partial
, &view
->partial
, view
->type
);
265 int i915_vma_bind(struct i915_vma
*vma
, enum i915_cache_level cache_level
,
267 bool i915_gem_valid_gtt_space(struct i915_vma
*vma
, unsigned long cache_level
);
268 bool i915_vma_misplaced(const struct i915_vma
*vma
,
269 u64 size
, u64 alignment
, u64 flags
);
270 void __i915_vma_set_map_and_fenceable(struct i915_vma
*vma
);
271 void i915_vma_revoke_mmap(struct i915_vma
*vma
);
272 int __must_check
i915_vma_unbind(struct i915_vma
*vma
);
273 void i915_vma_unlink_ctx(struct i915_vma
*vma
);
274 void i915_vma_close(struct i915_vma
*vma
);
275 void i915_vma_reopen(struct i915_vma
*vma
);
276 void i915_vma_destroy(struct i915_vma
*vma
);
278 int __i915_vma_do_pin(struct i915_vma
*vma
,
279 u64 size
, u64 alignment
, u64 flags
);
280 static inline int __must_check
281 i915_vma_pin(struct i915_vma
*vma
, u64 size
, u64 alignment
, u64 flags
)
283 BUILD_BUG_ON(PIN_MBZ
!= I915_VMA_PIN_OVERFLOW
);
284 BUILD_BUG_ON(PIN_GLOBAL
!= I915_VMA_GLOBAL_BIND
);
285 BUILD_BUG_ON(PIN_USER
!= I915_VMA_LOCAL_BIND
);
287 /* Pin early to prevent the shrinker/eviction logic from destroying
288 * our vma as we insert and bind.
290 if (likely(((++vma
->flags
^ flags
) & I915_VMA_BIND_MASK
) == 0)) {
291 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
292 GEM_BUG_ON(i915_vma_misplaced(vma
, size
, alignment
, flags
));
296 return __i915_vma_do_pin(vma
, size
, alignment
, flags
);
299 static inline int i915_vma_pin_count(const struct i915_vma
*vma
)
301 return vma
->flags
& I915_VMA_PIN_MASK
;
304 static inline bool i915_vma_is_pinned(const struct i915_vma
*vma
)
306 return i915_vma_pin_count(vma
);
309 static inline void __i915_vma_pin(struct i915_vma
*vma
)
312 GEM_BUG_ON(vma
->flags
& I915_VMA_PIN_OVERFLOW
);
315 static inline void __i915_vma_unpin(struct i915_vma
*vma
)
320 static inline void i915_vma_unpin(struct i915_vma
*vma
)
322 GEM_BUG_ON(!i915_vma_is_pinned(vma
));
323 GEM_BUG_ON(!drm_mm_node_allocated(&vma
->node
));
324 __i915_vma_unpin(vma
);
327 static inline bool i915_vma_is_bound(const struct i915_vma
*vma
,
330 return vma
->flags
& where
;
334 * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
337 * The passed in VMA has to be pinned in the global GTT mappable region.
338 * An extra pinning of the VMA is acquired for the return iomapping,
339 * the caller must call i915_vma_unpin_iomap to relinquish the pinning
340 * after the iomapping is no longer required.
342 * Callers must hold the struct_mutex.
344 * Returns a valid iomapped pointer or ERR_PTR.
346 void __iomem
*i915_vma_pin_iomap(struct i915_vma
*vma
);
347 #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
350 * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
353 * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
355 * Callers must hold the struct_mutex. This function is only valid to be
356 * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
358 void i915_vma_unpin_iomap(struct i915_vma
*vma
);
360 static inline struct page
*i915_vma_first_page(struct i915_vma
*vma
)
362 GEM_BUG_ON(!vma
->pages
);
363 return sg_page(vma
->pages
->sgl
);
367 * i915_vma_pin_fence - pin fencing state
368 * @vma: vma to pin fencing for
370 * This pins the fencing state (whether tiled or untiled) to make sure the
371 * vma (and its object) is ready to be used as a scanout target. Fencing
372 * status must be synchronize first by calling i915_vma_get_fence():
374 * The resulting fence pin reference must be released again with
375 * i915_vma_unpin_fence().
379 * True if the vma has a fence, false otherwise.
381 int i915_vma_pin_fence(struct i915_vma
*vma
);
382 int __must_check
i915_vma_put_fence(struct i915_vma
*vma
);
384 static inline void __i915_vma_unpin_fence(struct i915_vma
*vma
)
386 GEM_BUG_ON(vma
->fence
->pin_count
<= 0);
387 vma
->fence
->pin_count
--;
391 * i915_vma_unpin_fence - unpin fencing state
392 * @vma: vma to unpin fencing for
394 * This releases the fence pin reference acquired through
395 * i915_vma_pin_fence. It will handle both objects with and without an
396 * attached fence correctly, callers do not need to distinguish this.
399 i915_vma_unpin_fence(struct i915_vma
*vma
)
401 /* lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); */
403 __i915_vma_unpin_fence(vma
);
406 void i915_vma_parked(struct drm_i915_private
*i915
);
408 #define for_each_until(cond) if (cond) break; else
411 * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object.
412 * @V: the #i915_vma iterator
413 * @OBJ: the #drm_i915_gem_object
415 * GGTT VMA are placed at the being of the object's vma_list, see
416 * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA,
417 * or the list is empty ofc.
419 #define for_each_ggtt_vma(V, OBJ) \
420 list_for_each_entry(V, &(OBJ)->vma_list, obj_link) \
421 for_each_until(!i915_vma_is_ggtt(V))