xref: /openbmc/linux/drivers/gpu/drm/i915/i915_vma.h (revision ba61bb17496d1664bf7c5c2fd650d5fd78bd0a92)
1 /*
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 
36 #include "i915_request.h"
37 
38 enum 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  */
48 struct i915_vma {
49 	struct drm_mm_node node;
50 	struct drm_i915_gem_object *obj;
51 	struct i915_address_space *vm;
52 	const struct i915_vma_ops *ops;
53 	struct drm_i915_fence_reg *fence;
54 	struct reservation_object *resv; /** Alias of obj->resv */
55 	struct sg_table *pages;
56 	void __iomem *iomap;
57 	void *private; /* owned by creator */
58 	u64 size;
59 	u64 display_alignment;
60 	struct i915_page_sizes page_sizes;
61 
62 	u32 fence_size;
63 	u32 fence_alignment;
64 
65 	/**
66 	 * Count of the number of times this vma has been opened by different
67 	 * handles (but same file) for execbuf, i.e. the number of aliases
68 	 * that exist in the ctx->handle_vmas LUT for this vma.
69 	 */
70 	unsigned int open_count;
71 	unsigned long flags;
72 	/**
73 	 * How many users have pinned this object in GTT space. The following
74 	 * users can each hold at most one reference: pwrite/pread, execbuffer
75 	 * (objects are not allowed multiple times for the same batchbuffer),
76 	 * and the framebuffer code. When switching/pageflipping, the
77 	 * framebuffer code has at most two buffers pinned per crtc.
78 	 *
79 	 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
80 	 * bits with absolutely no headroom. So use 4 bits.
81 	 */
82 #define I915_VMA_PIN_MASK 0xf
83 #define I915_VMA_PIN_OVERFLOW	BIT(5)
84 
85 	/** Flags and address space this VMA is bound to */
86 #define I915_VMA_GLOBAL_BIND	BIT(6)
87 #define I915_VMA_LOCAL_BIND	BIT(7)
88 #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
89 
90 #define I915_VMA_GGTT		BIT(8)
91 #define I915_VMA_CAN_FENCE	BIT(9)
92 #define I915_VMA_CLOSED		BIT(10)
93 #define I915_VMA_USERFAULT_BIT	11
94 #define I915_VMA_USERFAULT	BIT(I915_VMA_USERFAULT_BIT)
95 #define I915_VMA_GGTT_WRITE	BIT(12)
96 
97 	unsigned int active;
98 	struct i915_gem_active last_read[I915_NUM_ENGINES];
99 	struct i915_gem_active last_fence;
100 
101 	/**
102 	 * Support different GGTT views into the same object.
103 	 * This means there can be multiple VMA mappings per object and per VM.
104 	 * i915_ggtt_view_type is used to distinguish between those entries.
105 	 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
106 	 * assumed in GEM functions which take no ggtt view parameter.
107 	 */
108 	struct i915_ggtt_view ggtt_view;
109 
110 	/** This object's place on the active/inactive lists */
111 	struct list_head vm_link;
112 
113 	struct list_head obj_link; /* Link in the object's VMA list */
114 	struct rb_node obj_node;
115 	struct hlist_node obj_hash;
116 
117 	/** This vma's place in the execbuf reservation list */
118 	struct list_head exec_link;
119 	struct list_head reloc_link;
120 
121 	/** This vma's place in the eviction list */
122 	struct list_head evict_link;
123 
124 	struct list_head closed_link;
125 
126 	/**
127 	 * Used for performing relocations during execbuffer insertion.
128 	 */
129 	unsigned int *exec_flags;
130 	struct hlist_node exec_node;
131 	u32 exec_handle;
132 };
133 
134 struct i915_vma *
135 i915_vma_instance(struct drm_i915_gem_object *obj,
136 		  struct i915_address_space *vm,
137 		  const struct i915_ggtt_view *view);
138 
139 void i915_vma_unpin_and_release(struct i915_vma **p_vma);
140 
141 static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
142 {
143 	return vma->flags & I915_VMA_GGTT;
144 }
145 
146 static inline bool i915_vma_has_ggtt_write(const struct i915_vma *vma)
147 {
148 	return vma->flags & I915_VMA_GGTT_WRITE;
149 }
150 
151 static inline void i915_vma_set_ggtt_write(struct i915_vma *vma)
152 {
153 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
154 	vma->flags |= I915_VMA_GGTT_WRITE;
155 }
156 
157 static inline void i915_vma_unset_ggtt_write(struct i915_vma *vma)
158 {
159 	vma->flags &= ~I915_VMA_GGTT_WRITE;
160 }
161 
162 void i915_vma_flush_writes(struct i915_vma *vma);
163 
164 static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
165 {
166 	return vma->flags & I915_VMA_CAN_FENCE;
167 }
168 
169 static inline bool i915_vma_is_closed(const struct i915_vma *vma)
170 {
171 	return vma->flags & I915_VMA_CLOSED;
172 }
173 
174 static inline bool i915_vma_set_userfault(struct i915_vma *vma)
175 {
176 	GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
177 	return __test_and_set_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
178 }
179 
180 static inline void i915_vma_unset_userfault(struct i915_vma *vma)
181 {
182 	return __clear_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
183 }
184 
185 static inline bool i915_vma_has_userfault(const struct i915_vma *vma)
186 {
187 	return test_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
188 }
189 
190 static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
191 {
192 	return vma->active;
193 }
194 
195 static inline bool i915_vma_is_active(const struct i915_vma *vma)
196 {
197 	return i915_vma_get_active(vma);
198 }
199 
200 static inline void i915_vma_set_active(struct i915_vma *vma,
201 				       unsigned int engine)
202 {
203 	vma->active |= BIT(engine);
204 }
205 
206 static inline void i915_vma_clear_active(struct i915_vma *vma,
207 					 unsigned int engine)
208 {
209 	vma->active &= ~BIT(engine);
210 }
211 
212 static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
213 					      unsigned int engine)
214 {
215 	return vma->active & BIT(engine);
216 }
217 
218 static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
219 {
220 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
221 	GEM_BUG_ON(!vma->node.allocated);
222 	GEM_BUG_ON(upper_32_bits(vma->node.start));
223 	GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
224 	return lower_32_bits(vma->node.start);
225 }
226 
227 static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
228 {
229 	i915_gem_object_get(vma->obj);
230 	return vma;
231 }
232 
233 static inline void i915_vma_put(struct i915_vma *vma)
234 {
235 	i915_gem_object_put(vma->obj);
236 }
237 
238 static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
239 {
240 	return a - b;
241 }
242 
243 static inline long
244 i915_vma_compare(struct i915_vma *vma,
245 		 struct i915_address_space *vm,
246 		 const struct i915_ggtt_view *view)
247 {
248 	ptrdiff_t cmp;
249 
250 	GEM_BUG_ON(view && !i915_is_ggtt(vm));
251 
252 	cmp = ptrdiff(vma->vm, vm);
253 	if (cmp)
254 		return cmp;
255 
256 	BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
257 	cmp = vma->ggtt_view.type;
258 	if (!view)
259 		return cmp;
260 
261 	cmp -= view->type;
262 	if (cmp)
263 		return cmp;
264 
265 	/* ggtt_view.type also encodes its size so that we both distinguish
266 	 * different views using it as a "type" and also use a compact (no
267 	 * accessing of uninitialised padding bytes) memcmp without storing
268 	 * an extra parameter or adding more code.
269 	 *
270 	 * To ensure that the memcmp is valid for all branches of the union,
271 	 * even though the code looks like it is just comparing one branch,
272 	 * we assert above that all branches have the same address, and that
273 	 * each branch has a unique type/size.
274 	 */
275 	BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
276 	BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
277 	BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
278 		     offsetof(typeof(*view), partial));
279 	return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
280 }
281 
282 int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
283 		  u32 flags);
284 bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
285 bool i915_vma_misplaced(const struct i915_vma *vma,
286 			u64 size, u64 alignment, u64 flags);
287 void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
288 void i915_vma_revoke_mmap(struct i915_vma *vma);
289 int __must_check i915_vma_unbind(struct i915_vma *vma);
290 void i915_vma_unlink_ctx(struct i915_vma *vma);
291 void i915_vma_close(struct i915_vma *vma);
292 void i915_vma_reopen(struct i915_vma *vma);
293 void i915_vma_destroy(struct i915_vma *vma);
294 
295 int __i915_vma_do_pin(struct i915_vma *vma,
296 		      u64 size, u64 alignment, u64 flags);
297 static inline int __must_check
298 i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
299 {
300 	BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
301 	BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
302 	BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
303 
304 	/* Pin early to prevent the shrinker/eviction logic from destroying
305 	 * our vma as we insert and bind.
306 	 */
307 	if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
308 		GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
309 		GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
310 		return 0;
311 	}
312 
313 	return __i915_vma_do_pin(vma, size, alignment, flags);
314 }
315 
316 static inline int i915_vma_pin_count(const struct i915_vma *vma)
317 {
318 	return vma->flags & I915_VMA_PIN_MASK;
319 }
320 
321 static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
322 {
323 	return i915_vma_pin_count(vma);
324 }
325 
326 static inline void __i915_vma_pin(struct i915_vma *vma)
327 {
328 	vma->flags++;
329 	GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
330 }
331 
332 static inline void __i915_vma_unpin(struct i915_vma *vma)
333 {
334 	vma->flags--;
335 }
336 
337 static inline void i915_vma_unpin(struct i915_vma *vma)
338 {
339 	GEM_BUG_ON(!i915_vma_is_pinned(vma));
340 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
341 	__i915_vma_unpin(vma);
342 }
343 
344 static inline bool i915_vma_is_bound(const struct i915_vma *vma,
345 				     unsigned int where)
346 {
347 	return vma->flags & where;
348 }
349 
350 /**
351  * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
352  * @vma: VMA to iomap
353  *
354  * The passed in VMA has to be pinned in the global GTT mappable region.
355  * An extra pinning of the VMA is acquired for the return iomapping,
356  * the caller must call i915_vma_unpin_iomap to relinquish the pinning
357  * after the iomapping is no longer required.
358  *
359  * Callers must hold the struct_mutex.
360  *
361  * Returns a valid iomapped pointer or ERR_PTR.
362  */
363 void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
364 #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
365 
366 /**
367  * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
368  * @vma: VMA to unpin
369  *
370  * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
371  *
372  * Callers must hold the struct_mutex. This function is only valid to be
373  * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
374  */
375 void i915_vma_unpin_iomap(struct i915_vma *vma);
376 
377 static inline struct page *i915_vma_first_page(struct i915_vma *vma)
378 {
379 	GEM_BUG_ON(!vma->pages);
380 	return sg_page(vma->pages->sgl);
381 }
382 
383 /**
384  * i915_vma_pin_fence - pin fencing state
385  * @vma: vma to pin fencing for
386  *
387  * This pins the fencing state (whether tiled or untiled) to make sure the
388  * vma (and its object) is ready to be used as a scanout target. Fencing
389  * status must be synchronize first by calling i915_vma_get_fence():
390  *
391  * The resulting fence pin reference must be released again with
392  * i915_vma_unpin_fence().
393  *
394  * Returns:
395  *
396  * True if the vma has a fence, false otherwise.
397  */
398 int i915_vma_pin_fence(struct i915_vma *vma);
399 int __must_check i915_vma_put_fence(struct i915_vma *vma);
400 
401 static inline void __i915_vma_unpin_fence(struct i915_vma *vma)
402 {
403 	GEM_BUG_ON(vma->fence->pin_count <= 0);
404 	vma->fence->pin_count--;
405 }
406 
407 /**
408  * i915_vma_unpin_fence - unpin fencing state
409  * @vma: vma to unpin fencing for
410  *
411  * This releases the fence pin reference acquired through
412  * i915_vma_pin_fence. It will handle both objects with and without an
413  * attached fence correctly, callers do not need to distinguish this.
414  */
415 static inline void
416 i915_vma_unpin_fence(struct i915_vma *vma)
417 {
418 	/* lockdep_assert_held(&vma->vm->i915->drm.struct_mutex); */
419 	if (vma->fence)
420 		__i915_vma_unpin_fence(vma);
421 }
422 
423 void i915_vma_parked(struct drm_i915_private *i915);
424 
425 #define for_each_until(cond) if (cond) break; else
426 
427 /**
428  * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object.
429  * @V: the #i915_vma iterator
430  * @OBJ: the #drm_i915_gem_object
431  *
432  * GGTT VMA are placed at the being of the object's vma_list, see
433  * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA,
434  * or the list is empty ofc.
435  */
436 #define for_each_ggtt_vma(V, OBJ) \
437 	list_for_each_entry(V, &(OBJ)->vma_list, obj_link)		\
438 		for_each_until(!i915_vma_is_ggtt(V))
439 
440 #endif
441