xref: /dragonfly/sys/dev/drm/i915/i915_vma.h (revision 3f2dd94a)
14be47400SFrançois Tigeot /*
24be47400SFrançois Tigeot  * Copyright © 2016 Intel Corporation
34be47400SFrançois Tigeot  *
44be47400SFrançois Tigeot  * Permission is hereby granted, free of charge, to any person obtaining a
54be47400SFrançois Tigeot  * copy of this software and associated documentation files (the "Software"),
64be47400SFrançois Tigeot  * to deal in the Software without restriction, including without limitation
74be47400SFrançois Tigeot  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
84be47400SFrançois Tigeot  * and/or sell copies of the Software, and to permit persons to whom the
94be47400SFrançois Tigeot  * Software is furnished to do so, subject to the following conditions:
104be47400SFrançois Tigeot  *
114be47400SFrançois Tigeot  * The above copyright notice and this permission notice (including the next
124be47400SFrançois Tigeot  * paragraph) shall be included in all copies or substantial portions of the
134be47400SFrançois Tigeot  * Software.
144be47400SFrançois Tigeot  *
154be47400SFrançois Tigeot  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
164be47400SFrançois Tigeot  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
174be47400SFrançois Tigeot  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
184be47400SFrançois Tigeot  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
194be47400SFrançois Tigeot  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
204be47400SFrançois Tigeot  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
214be47400SFrançois Tigeot  * IN THE SOFTWARE.
224be47400SFrançois Tigeot  *
234be47400SFrançois Tigeot  */
244be47400SFrançois Tigeot 
254be47400SFrançois Tigeot #ifndef __I915_VMA_H__
264be47400SFrançois Tigeot #define __I915_VMA_H__
274be47400SFrançois Tigeot 
284be47400SFrançois Tigeot #include <linux/io-mapping.h>
294be47400SFrançois Tigeot 
304be47400SFrançois Tigeot #include <drm/drm_mm.h>
314be47400SFrançois Tigeot 
324be47400SFrançois Tigeot #include "i915_gem_gtt.h"
334be47400SFrançois Tigeot #include "i915_gem_fence_reg.h"
344be47400SFrançois Tigeot #include "i915_gem_object.h"
354be47400SFrançois Tigeot #include "i915_gem_request.h"
364be47400SFrançois Tigeot 
374be47400SFrançois Tigeot 
384be47400SFrançois Tigeot enum i915_cache_level;
394be47400SFrançois Tigeot 
404be47400SFrançois Tigeot /**
414be47400SFrançois Tigeot  * A VMA represents a GEM BO that is bound into an address space. Therefore, a
424be47400SFrançois Tigeot  * VMA's presence cannot be guaranteed before binding, or after unbinding the
434be47400SFrançois Tigeot  * object into/from the address space.
444be47400SFrançois Tigeot  *
454be47400SFrançois Tigeot  * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
464be47400SFrançois Tigeot  * will always be <= an objects lifetime. So object refcounting should cover us.
474be47400SFrançois Tigeot  */
484be47400SFrançois Tigeot struct i915_vma {
494be47400SFrançois Tigeot 	struct drm_mm_node node;
504be47400SFrançois Tigeot 	struct drm_i915_gem_object *obj;
514be47400SFrançois Tigeot 	struct i915_address_space *vm;
524be47400SFrançois Tigeot 	struct drm_i915_fence_reg *fence;
53*3f2dd94aSFrançois Tigeot 	struct reservation_object *resv; /** Alias of obj->resv */
544be47400SFrançois Tigeot 	struct sg_table *pages;
554be47400SFrançois Tigeot 	void __iomem *iomap;
564be47400SFrançois Tigeot 	u64 size;
574be47400SFrançois Tigeot 	u64 display_alignment;
58*3f2dd94aSFrançois Tigeot 	struct i915_page_sizes page_sizes;
594be47400SFrançois Tigeot 
60a85cb24fSFrançois Tigeot 	u32 fence_size;
61a85cb24fSFrançois Tigeot 	u32 fence_alignment;
62a85cb24fSFrançois Tigeot 
63*3f2dd94aSFrançois Tigeot 	/**
64*3f2dd94aSFrançois Tigeot 	 * Count of the number of times this vma has been opened by different
65*3f2dd94aSFrançois Tigeot 	 * handles (but same file) for execbuf, i.e. the number of aliases
66*3f2dd94aSFrançois Tigeot 	 * that exist in the ctx->handle_vmas LUT for this vma.
67*3f2dd94aSFrançois Tigeot 	 */
68*3f2dd94aSFrançois Tigeot 	unsigned int open_count;
69*3f2dd94aSFrançois Tigeot 	unsigned long flags;
704be47400SFrançois Tigeot 	/**
714be47400SFrançois Tigeot 	 * How many users have pinned this object in GTT space. The following
724be47400SFrançois Tigeot 	 * users can each hold at most one reference: pwrite/pread, execbuffer
734be47400SFrançois Tigeot 	 * (objects are not allowed multiple times for the same batchbuffer),
744be47400SFrançois Tigeot 	 * and the framebuffer code. When switching/pageflipping, the
754be47400SFrançois Tigeot 	 * framebuffer code has at most two buffers pinned per crtc.
764be47400SFrançois Tigeot 	 *
774be47400SFrançois Tigeot 	 * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
784be47400SFrançois Tigeot 	 * bits with absolutely no headroom. So use 4 bits.
794be47400SFrançois Tigeot 	 */
804be47400SFrançois Tigeot #define I915_VMA_PIN_MASK 0xf
814be47400SFrançois Tigeot #define I915_VMA_PIN_OVERFLOW	BIT(5)
824be47400SFrançois Tigeot 
834be47400SFrançois Tigeot 	/** Flags and address space this VMA is bound to */
844be47400SFrançois Tigeot #define I915_VMA_GLOBAL_BIND	BIT(6)
854be47400SFrançois Tigeot #define I915_VMA_LOCAL_BIND	BIT(7)
864be47400SFrançois Tigeot #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
874be47400SFrançois Tigeot 
884be47400SFrançois Tigeot #define I915_VMA_GGTT		BIT(8)
894be47400SFrançois Tigeot #define I915_VMA_CAN_FENCE	BIT(9)
904be47400SFrançois Tigeot #define I915_VMA_CLOSED		BIT(10)
91*3f2dd94aSFrançois Tigeot #define I915_VMA_USERFAULT_BIT	11
92*3f2dd94aSFrançois Tigeot #define I915_VMA_USERFAULT	BIT(I915_VMA_USERFAULT_BIT)
934be47400SFrançois Tigeot 
944be47400SFrançois Tigeot 	unsigned int active;
954be47400SFrançois Tigeot 	struct i915_gem_active last_read[I915_NUM_ENGINES];
964be47400SFrançois Tigeot 	struct i915_gem_active last_fence;
974be47400SFrançois Tigeot 
984be47400SFrançois Tigeot 	/**
994be47400SFrançois Tigeot 	 * Support different GGTT views into the same object.
1004be47400SFrançois Tigeot 	 * This means there can be multiple VMA mappings per object and per VM.
1014be47400SFrançois Tigeot 	 * i915_ggtt_view_type is used to distinguish between those entries.
1024be47400SFrançois Tigeot 	 * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
1034be47400SFrançois Tigeot 	 * assumed in GEM functions which take no ggtt view parameter.
1044be47400SFrançois Tigeot 	 */
1054be47400SFrançois Tigeot 	struct i915_ggtt_view ggtt_view;
1064be47400SFrançois Tigeot 
1074be47400SFrançois Tigeot 	/** This object's place on the active/inactive lists */
1084be47400SFrançois Tigeot 	struct list_head vm_link;
1094be47400SFrançois Tigeot 
1104be47400SFrançois Tigeot 	struct list_head obj_link; /* Link in the object's VMA list */
1114be47400SFrançois Tigeot 	struct rb_node obj_node;
112*3f2dd94aSFrançois Tigeot 	struct hlist_node obj_hash;
1134be47400SFrançois Tigeot 
114*3f2dd94aSFrançois Tigeot 	/** This vma's place in the execbuf reservation list */
115*3f2dd94aSFrançois Tigeot 	struct list_head exec_link;
116*3f2dd94aSFrançois Tigeot 	struct list_head reloc_link;
117*3f2dd94aSFrançois Tigeot 
118*3f2dd94aSFrançois Tigeot 	/** This vma's place in the eviction list */
119*3f2dd94aSFrançois Tigeot 	struct list_head evict_link;
1204be47400SFrançois Tigeot 
1214be47400SFrançois Tigeot 	/**
1224be47400SFrançois Tigeot 	 * Used for performing relocations during execbuffer insertion.
1234be47400SFrançois Tigeot 	 */
124*3f2dd94aSFrançois Tigeot 	unsigned int *exec_flags;
1254be47400SFrançois Tigeot 	struct hlist_node exec_node;
126*3f2dd94aSFrançois Tigeot 	u32 exec_handle;
1274be47400SFrançois Tigeot };
1284be47400SFrançois Tigeot 
1294be47400SFrançois Tigeot struct i915_vma *
130a85cb24fSFrançois Tigeot i915_vma_instance(struct drm_i915_gem_object *obj,
1314be47400SFrançois Tigeot 		  struct i915_address_space *vm,
1324be47400SFrançois Tigeot 		  const struct i915_ggtt_view *view);
1334be47400SFrançois Tigeot 
1344be47400SFrançois Tigeot void i915_vma_unpin_and_release(struct i915_vma **p_vma);
1354be47400SFrançois Tigeot 
i915_vma_is_ggtt(const struct i915_vma * vma)1364be47400SFrançois Tigeot static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
1374be47400SFrançois Tigeot {
1384be47400SFrançois Tigeot 	return vma->flags & I915_VMA_GGTT;
1394be47400SFrançois Tigeot }
1404be47400SFrançois Tigeot 
i915_vma_is_map_and_fenceable(const struct i915_vma * vma)1414be47400SFrançois Tigeot static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
1424be47400SFrançois Tigeot {
1434be47400SFrançois Tigeot 	return vma->flags & I915_VMA_CAN_FENCE;
1444be47400SFrançois Tigeot }
1454be47400SFrançois Tigeot 
i915_vma_is_closed(const struct i915_vma * vma)1464be47400SFrançois Tigeot static inline bool i915_vma_is_closed(const struct i915_vma *vma)
1474be47400SFrançois Tigeot {
1484be47400SFrançois Tigeot 	return vma->flags & I915_VMA_CLOSED;
1494be47400SFrançois Tigeot }
1504be47400SFrançois Tigeot 
i915_vma_set_userfault(struct i915_vma * vma)151*3f2dd94aSFrançois Tigeot static inline bool i915_vma_set_userfault(struct i915_vma *vma)
152*3f2dd94aSFrançois Tigeot {
153*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
154*3f2dd94aSFrançois Tigeot 	return __test_and_set_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
155*3f2dd94aSFrançois Tigeot }
156*3f2dd94aSFrançois Tigeot 
i915_vma_unset_userfault(struct i915_vma * vma)157*3f2dd94aSFrançois Tigeot static inline void i915_vma_unset_userfault(struct i915_vma *vma)
158*3f2dd94aSFrançois Tigeot {
159*3f2dd94aSFrançois Tigeot 	return __clear_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
160*3f2dd94aSFrançois Tigeot }
161*3f2dd94aSFrançois Tigeot 
i915_vma_has_userfault(const struct i915_vma * vma)162*3f2dd94aSFrançois Tigeot static inline bool i915_vma_has_userfault(const struct i915_vma *vma)
163*3f2dd94aSFrançois Tigeot {
164*3f2dd94aSFrançois Tigeot 	return test_bit(I915_VMA_USERFAULT_BIT, &vma->flags);
165*3f2dd94aSFrançois Tigeot }
166*3f2dd94aSFrançois Tigeot 
i915_vma_get_active(const struct i915_vma * vma)1674be47400SFrançois Tigeot static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
1684be47400SFrançois Tigeot {
1694be47400SFrançois Tigeot 	return vma->active;
1704be47400SFrançois Tigeot }
1714be47400SFrançois Tigeot 
i915_vma_is_active(const struct i915_vma * vma)1724be47400SFrançois Tigeot static inline bool i915_vma_is_active(const struct i915_vma *vma)
1734be47400SFrançois Tigeot {
1744be47400SFrançois Tigeot 	return i915_vma_get_active(vma);
1754be47400SFrançois Tigeot }
1764be47400SFrançois Tigeot 
i915_vma_set_active(struct i915_vma * vma,unsigned int engine)1774be47400SFrançois Tigeot static inline void i915_vma_set_active(struct i915_vma *vma,
1784be47400SFrançois Tigeot 				       unsigned int engine)
1794be47400SFrançois Tigeot {
1804be47400SFrançois Tigeot 	vma->active |= BIT(engine);
1814be47400SFrançois Tigeot }
1824be47400SFrançois Tigeot 
i915_vma_clear_active(struct i915_vma * vma,unsigned int engine)1834be47400SFrançois Tigeot static inline void i915_vma_clear_active(struct i915_vma *vma,
1844be47400SFrançois Tigeot 					 unsigned int engine)
1854be47400SFrançois Tigeot {
1864be47400SFrançois Tigeot 	vma->active &= ~BIT(engine);
1874be47400SFrançois Tigeot }
1884be47400SFrançois Tigeot 
i915_vma_has_active_engine(const struct i915_vma * vma,unsigned int engine)1894be47400SFrançois Tigeot static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
1904be47400SFrançois Tigeot 					      unsigned int engine)
1914be47400SFrançois Tigeot {
1924be47400SFrançois Tigeot 	return vma->active & BIT(engine);
1934be47400SFrançois Tigeot }
1944be47400SFrançois Tigeot 
i915_ggtt_offset(const struct i915_vma * vma)1954be47400SFrançois Tigeot static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
1964be47400SFrançois Tigeot {
1974be47400SFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_ggtt(vma));
1984be47400SFrançois Tigeot 	GEM_BUG_ON(!vma->node.allocated);
1994be47400SFrançois Tigeot 	GEM_BUG_ON(upper_32_bits(vma->node.start));
2004be47400SFrançois Tigeot 	GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
2014be47400SFrançois Tigeot 	return lower_32_bits(vma->node.start);
2024be47400SFrançois Tigeot }
2034be47400SFrançois Tigeot 
i915_vma_get(struct i915_vma * vma)2044be47400SFrançois Tigeot static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
2054be47400SFrançois Tigeot {
2064be47400SFrançois Tigeot 	i915_gem_object_get(vma->obj);
2074be47400SFrançois Tigeot 	return vma;
2084be47400SFrançois Tigeot }
2094be47400SFrançois Tigeot 
i915_vma_put(struct i915_vma * vma)2104be47400SFrançois Tigeot static inline void i915_vma_put(struct i915_vma *vma)
2114be47400SFrançois Tigeot {
2124be47400SFrançois Tigeot 	i915_gem_object_put(vma->obj);
2134be47400SFrançois Tigeot }
2144be47400SFrançois Tigeot 
ptrdiff(const void * a,const void * b)215a85cb24fSFrançois Tigeot static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
216a85cb24fSFrançois Tigeot {
217a85cb24fSFrançois Tigeot 	return a - b;
218a85cb24fSFrançois Tigeot }
219a85cb24fSFrançois Tigeot 
2204be47400SFrançois Tigeot static inline long
i915_vma_compare(struct i915_vma * vma,struct i915_address_space * vm,const struct i915_ggtt_view * view)2214be47400SFrançois Tigeot i915_vma_compare(struct i915_vma *vma,
2224be47400SFrançois Tigeot 		 struct i915_address_space *vm,
2234be47400SFrançois Tigeot 		 const struct i915_ggtt_view *view)
2244be47400SFrançois Tigeot {
225a85cb24fSFrançois Tigeot 	ptrdiff_t cmp;
226a85cb24fSFrançois Tigeot 
2274be47400SFrançois Tigeot 	GEM_BUG_ON(view && !i915_is_ggtt(vm));
2284be47400SFrançois Tigeot 
229a85cb24fSFrançois Tigeot 	cmp = ptrdiff(vma->vm, vm);
230a85cb24fSFrançois Tigeot 	if (cmp)
231a85cb24fSFrançois Tigeot 		return cmp;
2324be47400SFrançois Tigeot 
233a85cb24fSFrançois Tigeot 	BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
234a85cb24fSFrançois Tigeot 	cmp = vma->ggtt_view.type;
2354be47400SFrançois Tigeot 	if (!view)
236a85cb24fSFrançois Tigeot 		return cmp;
2374be47400SFrançois Tigeot 
238a85cb24fSFrançois Tigeot 	cmp -= view->type;
239a85cb24fSFrançois Tigeot 	if (cmp)
240a85cb24fSFrançois Tigeot 		return cmp;
2414be47400SFrançois Tigeot 
242a85cb24fSFrançois Tigeot 	/* ggtt_view.type also encodes its size so that we both distinguish
243a85cb24fSFrançois Tigeot 	 * different views using it as a "type" and also use a compact (no
244a85cb24fSFrançois Tigeot 	 * accessing of uninitialised padding bytes) memcmp without storing
245a85cb24fSFrançois Tigeot 	 * an extra parameter or adding more code.
246a85cb24fSFrançois Tigeot 	 *
247a85cb24fSFrançois Tigeot 	 * To ensure that the memcmp is valid for all branches of the union,
248a85cb24fSFrançois Tigeot 	 * even though the code looks like it is just comparing one branch,
249a85cb24fSFrançois Tigeot 	 * we assert above that all branches have the same address, and that
250a85cb24fSFrançois Tigeot 	 * each branch has a unique type/size.
251a85cb24fSFrançois Tigeot 	 */
252a85cb24fSFrançois Tigeot 	BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
253a85cb24fSFrançois Tigeot 	BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
254a85cb24fSFrançois Tigeot 	BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
255a85cb24fSFrançois Tigeot 		     offsetof(typeof(*view), partial));
256a85cb24fSFrançois Tigeot 	return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
2574be47400SFrançois Tigeot }
2584be47400SFrançois Tigeot 
2594be47400SFrançois Tigeot int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
2604be47400SFrançois Tigeot 		  u32 flags);
2614be47400SFrançois Tigeot bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
262a85cb24fSFrançois Tigeot bool i915_vma_misplaced(const struct i915_vma *vma,
263a85cb24fSFrançois Tigeot 			u64 size, u64 alignment, u64 flags);
2644be47400SFrançois Tigeot void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
265*3f2dd94aSFrançois Tigeot void i915_vma_revoke_mmap(struct i915_vma *vma);
2664be47400SFrançois Tigeot int __must_check i915_vma_unbind(struct i915_vma *vma);
267*3f2dd94aSFrançois Tigeot void i915_vma_unlink_ctx(struct i915_vma *vma);
2684be47400SFrançois Tigeot void i915_vma_close(struct i915_vma *vma);
2694be47400SFrançois Tigeot 
2704be47400SFrançois Tigeot int __i915_vma_do_pin(struct i915_vma *vma,
2714be47400SFrançois Tigeot 		      u64 size, u64 alignment, u64 flags);
2724be47400SFrançois Tigeot static inline int __must_check
i915_vma_pin(struct i915_vma * vma,u64 size,u64 alignment,u64 flags)2734be47400SFrançois Tigeot i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
2744be47400SFrançois Tigeot {
2754be47400SFrançois Tigeot 	BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
2764be47400SFrançois Tigeot 	BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
2774be47400SFrançois Tigeot 	BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
2784be47400SFrançois Tigeot 
2794be47400SFrançois Tigeot 	/* Pin early to prevent the shrinker/eviction logic from destroying
2804be47400SFrançois Tigeot 	 * our vma as we insert and bind.
2814be47400SFrançois Tigeot 	 */
282a85cb24fSFrançois Tigeot 	if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
283a85cb24fSFrançois Tigeot 		GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
284a85cb24fSFrançois Tigeot 		GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
2854be47400SFrançois Tigeot 		return 0;
286a85cb24fSFrançois Tigeot 	}
2874be47400SFrançois Tigeot 
2884be47400SFrançois Tigeot 	return __i915_vma_do_pin(vma, size, alignment, flags);
2894be47400SFrançois Tigeot }
2904be47400SFrançois Tigeot 
i915_vma_pin_count(const struct i915_vma * vma)2914be47400SFrançois Tigeot static inline int i915_vma_pin_count(const struct i915_vma *vma)
2924be47400SFrançois Tigeot {
2934be47400SFrançois Tigeot 	return vma->flags & I915_VMA_PIN_MASK;
2944be47400SFrançois Tigeot }
2954be47400SFrançois Tigeot 
i915_vma_is_pinned(const struct i915_vma * vma)2964be47400SFrançois Tigeot static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
2974be47400SFrançois Tigeot {
2984be47400SFrançois Tigeot 	return i915_vma_pin_count(vma);
2994be47400SFrançois Tigeot }
3004be47400SFrançois Tigeot 
__i915_vma_pin(struct i915_vma * vma)3014be47400SFrançois Tigeot static inline void __i915_vma_pin(struct i915_vma *vma)
3024be47400SFrançois Tigeot {
3034be47400SFrançois Tigeot 	vma->flags++;
3044be47400SFrançois Tigeot 	GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
3054be47400SFrançois Tigeot }
3064be47400SFrançois Tigeot 
__i915_vma_unpin(struct i915_vma * vma)3074be47400SFrançois Tigeot static inline void __i915_vma_unpin(struct i915_vma *vma)
3084be47400SFrançois Tigeot {
3094be47400SFrançois Tigeot 	vma->flags--;
3104be47400SFrançois Tigeot }
3114be47400SFrançois Tigeot 
i915_vma_unpin(struct i915_vma * vma)3124be47400SFrançois Tigeot static inline void i915_vma_unpin(struct i915_vma *vma)
3134be47400SFrançois Tigeot {
314*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(!i915_vma_is_pinned(vma));
3154be47400SFrançois Tigeot 	GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
3164be47400SFrançois Tigeot 	__i915_vma_unpin(vma);
3174be47400SFrançois Tigeot }
3184be47400SFrançois Tigeot 
3194be47400SFrançois Tigeot /**
3204be47400SFrançois Tigeot  * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
3214be47400SFrançois Tigeot  * @vma: VMA to iomap
3224be47400SFrançois Tigeot  *
3234be47400SFrançois Tigeot  * The passed in VMA has to be pinned in the global GTT mappable region.
3244be47400SFrançois Tigeot  * An extra pinning of the VMA is acquired for the return iomapping,
3254be47400SFrançois Tigeot  * the caller must call i915_vma_unpin_iomap to relinquish the pinning
3264be47400SFrançois Tigeot  * after the iomapping is no longer required.
3274be47400SFrançois Tigeot  *
3284be47400SFrançois Tigeot  * Callers must hold the struct_mutex.
3294be47400SFrançois Tigeot  *
3304be47400SFrançois Tigeot  * Returns a valid iomapped pointer or ERR_PTR.
3314be47400SFrançois Tigeot  */
3324be47400SFrançois Tigeot void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
3334be47400SFrançois Tigeot #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
3344be47400SFrançois Tigeot 
3354be47400SFrançois Tigeot /**
3364be47400SFrançois Tigeot  * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
3374be47400SFrançois Tigeot  * @vma: VMA to unpin
3384be47400SFrançois Tigeot  *
3394be47400SFrançois Tigeot  * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
3404be47400SFrançois Tigeot  *
3414be47400SFrançois Tigeot  * Callers must hold the struct_mutex. This function is only valid to be
3424be47400SFrançois Tigeot  * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
3434be47400SFrançois Tigeot  */
344*3f2dd94aSFrançois Tigeot void i915_vma_unpin_iomap(struct i915_vma *vma);
3454be47400SFrançois Tigeot 
i915_vma_first_page(struct i915_vma * vma)3464be47400SFrançois Tigeot static inline struct page *i915_vma_first_page(struct i915_vma *vma)
3474be47400SFrançois Tigeot {
3484be47400SFrançois Tigeot 	GEM_BUG_ON(!vma->pages);
3494be47400SFrançois Tigeot 	return sg_page(vma->pages->sgl);
3504be47400SFrançois Tigeot }
3514be47400SFrançois Tigeot 
3524be47400SFrançois Tigeot /**
3534be47400SFrançois Tigeot  * i915_vma_pin_fence - pin fencing state
3544be47400SFrançois Tigeot  * @vma: vma to pin fencing for
3554be47400SFrançois Tigeot  *
3564be47400SFrançois Tigeot  * This pins the fencing state (whether tiled or untiled) to make sure the
3574be47400SFrançois Tigeot  * vma (and its object) is ready to be used as a scanout target. Fencing
3584be47400SFrançois Tigeot  * status must be synchronize first by calling i915_vma_get_fence():
3594be47400SFrançois Tigeot  *
3604be47400SFrançois Tigeot  * The resulting fence pin reference must be released again with
3614be47400SFrançois Tigeot  * i915_vma_unpin_fence().
3624be47400SFrançois Tigeot  *
3634be47400SFrançois Tigeot  * Returns:
3644be47400SFrançois Tigeot  *
3654be47400SFrançois Tigeot  * True if the vma has a fence, false otherwise.
3664be47400SFrançois Tigeot  */
367*3f2dd94aSFrançois Tigeot int i915_vma_pin_fence(struct i915_vma *vma);
368*3f2dd94aSFrançois Tigeot int __must_check i915_vma_put_fence(struct i915_vma *vma);
369*3f2dd94aSFrançois Tigeot 
__i915_vma_unpin_fence(struct i915_vma * vma)370*3f2dd94aSFrançois Tigeot static inline void __i915_vma_unpin_fence(struct i915_vma *vma)
3714be47400SFrançois Tigeot {
372*3f2dd94aSFrançois Tigeot 	GEM_BUG_ON(vma->fence->pin_count <= 0);
373*3f2dd94aSFrançois Tigeot 	vma->fence->pin_count--;
3744be47400SFrançois Tigeot }
3754be47400SFrançois Tigeot 
3764be47400SFrançois Tigeot /**
3774be47400SFrançois Tigeot  * i915_vma_unpin_fence - unpin fencing state
3784be47400SFrançois Tigeot  * @vma: vma to unpin fencing for
3794be47400SFrançois Tigeot  *
3804be47400SFrançois Tigeot  * This releases the fence pin reference acquired through
3814be47400SFrançois Tigeot  * i915_vma_pin_fence. It will handle both objects with and without an
3824be47400SFrançois Tigeot  * attached fence correctly, callers do not need to distinguish this.
3834be47400SFrançois Tigeot  */
3844be47400SFrançois Tigeot static inline void
i915_vma_unpin_fence(struct i915_vma * vma)3854be47400SFrançois Tigeot i915_vma_unpin_fence(struct i915_vma *vma)
3864be47400SFrançois Tigeot {
387a85cb24fSFrançois Tigeot 	lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
388*3f2dd94aSFrançois Tigeot 	if (vma->fence)
389*3f2dd94aSFrançois Tigeot 		__i915_vma_unpin_fence(vma);
3904be47400SFrançois Tigeot }
3914be47400SFrançois Tigeot 
3924be47400SFrançois Tigeot #endif
3934be47400SFrançois Tigeot 
394