i915_vma.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. #ifndef __I915_VMA_H__
  25. #define __I915_VMA_H__
  26. #include <linux/io-mapping.h>
  27. #include <drm/drm_mm.h>
  28. #include "i915_gem_gtt.h"
  29. #include "i915_gem_fence_reg.h"
  30. #include "i915_gem_object.h"
  31. #include "i915_gem_request.h"
  32. enum i915_cache_level;
  33. /**
  34. * A VMA represents a GEM BO that is bound into an address space. Therefore, a
  35. * VMA's presence cannot be guaranteed before binding, or after unbinding the
  36. * object into/from the address space.
  37. *
  38. * To make things as simple as possible (ie. no refcounting), a VMA's lifetime
  39. * will always be <= an objects lifetime. So object refcounting should cover us.
  40. */
  41. struct i915_vma {
  42. struct drm_mm_node node;
  43. struct drm_i915_gem_object *obj;
  44. struct i915_address_space *vm;
  45. struct drm_i915_fence_reg *fence;
  46. struct reservation_object *resv; /** Alias of obj->resv */
  47. struct sg_table *pages;
  48. void __iomem *iomap;
  49. u64 size;
  50. u64 display_alignment;
  51. u32 fence_size;
  52. u32 fence_alignment;
  53. /**
  54. * Count of the number of times this vma has been opened by different
  55. * handles (but same file) for execbuf, i.e. the number of aliases
  56. * that exist in the ctx->handle_vmas LUT for this vma.
  57. */
  58. unsigned int open_count;
  59. unsigned int flags;
  60. /**
  61. * How many users have pinned this object in GTT space. The following
  62. * users can each hold at most one reference: pwrite/pread, execbuffer
  63. * (objects are not allowed multiple times for the same batchbuffer),
  64. * and the framebuffer code. When switching/pageflipping, the
  65. * framebuffer code has at most two buffers pinned per crtc.
  66. *
  67. * In the worst case this is 1 + 1 + 1 + 2*2 = 7. That would fit into 3
  68. * bits with absolutely no headroom. So use 4 bits.
  69. */
  70. #define I915_VMA_PIN_MASK 0xf
  71. #define I915_VMA_PIN_OVERFLOW BIT(5)
  72. /** Flags and address space this VMA is bound to */
  73. #define I915_VMA_GLOBAL_BIND BIT(6)
  74. #define I915_VMA_LOCAL_BIND BIT(7)
  75. #define I915_VMA_BIND_MASK (I915_VMA_GLOBAL_BIND | I915_VMA_LOCAL_BIND | I915_VMA_PIN_OVERFLOW)
  76. #define I915_VMA_GGTT BIT(8)
  77. #define I915_VMA_CAN_FENCE BIT(9)
  78. #define I915_VMA_CLOSED BIT(10)
  79. unsigned int active;
  80. struct i915_gem_active last_read[I915_NUM_ENGINES];
  81. struct i915_gem_active last_fence;
  82. /**
  83. * Support different GGTT views into the same object.
  84. * This means there can be multiple VMA mappings per object and per VM.
  85. * i915_ggtt_view_type is used to distinguish between those entries.
  86. * The default one of zero (I915_GGTT_VIEW_NORMAL) is default and also
  87. * assumed in GEM functions which take no ggtt view parameter.
  88. */
  89. struct i915_ggtt_view ggtt_view;
  90. /** This object's place on the active/inactive lists */
  91. struct list_head vm_link;
  92. struct list_head obj_link; /* Link in the object's VMA list */
  93. struct rb_node obj_node;
  94. struct hlist_node obj_hash;
  95. /** This vma's place in the execbuf reservation list */
  96. struct list_head exec_link;
  97. struct list_head reloc_link;
  98. /** This vma's place in the eviction list */
  99. struct list_head evict_link;
  100. /**
  101. * Used for performing relocations during execbuffer insertion.
  102. */
  103. unsigned int *exec_flags;
  104. struct hlist_node exec_node;
  105. u32 exec_handle;
  106. };
  107. struct i915_vma *
  108. i915_vma_instance(struct drm_i915_gem_object *obj,
  109. struct i915_address_space *vm,
  110. const struct i915_ggtt_view *view);
  111. void i915_vma_unpin_and_release(struct i915_vma **p_vma);
  112. static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
  113. {
  114. return vma->flags & I915_VMA_GGTT;
  115. }
  116. static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
  117. {
  118. return vma->flags & I915_VMA_CAN_FENCE;
  119. }
  120. static inline bool i915_vma_is_closed(const struct i915_vma *vma)
  121. {
  122. return vma->flags & I915_VMA_CLOSED;
  123. }
  124. static inline unsigned int i915_vma_get_active(const struct i915_vma *vma)
  125. {
  126. return vma->active;
  127. }
  128. static inline bool i915_vma_is_active(const struct i915_vma *vma)
  129. {
  130. return i915_vma_get_active(vma);
  131. }
  132. static inline void i915_vma_set_active(struct i915_vma *vma,
  133. unsigned int engine)
  134. {
  135. vma->active |= BIT(engine);
  136. }
  137. static inline void i915_vma_clear_active(struct i915_vma *vma,
  138. unsigned int engine)
  139. {
  140. vma->active &= ~BIT(engine);
  141. }
  142. static inline bool i915_vma_has_active_engine(const struct i915_vma *vma,
  143. unsigned int engine)
  144. {
  145. return vma->active & BIT(engine);
  146. }
  147. static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
  148. {
  149. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  150. GEM_BUG_ON(!vma->node.allocated);
  151. GEM_BUG_ON(upper_32_bits(vma->node.start));
  152. GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
  153. return lower_32_bits(vma->node.start);
  154. }
  155. static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
  156. {
  157. i915_gem_object_get(vma->obj);
  158. return vma;
  159. }
  160. static inline void i915_vma_put(struct i915_vma *vma)
  161. {
  162. i915_gem_object_put(vma->obj);
  163. }
  164. static __always_inline ptrdiff_t ptrdiff(const void *a, const void *b)
  165. {
  166. return a - b;
  167. }
  168. static inline long
  169. i915_vma_compare(struct i915_vma *vma,
  170. struct i915_address_space *vm,
  171. const struct i915_ggtt_view *view)
  172. {
  173. ptrdiff_t cmp;
  174. GEM_BUG_ON(view && !i915_is_ggtt(vm));
  175. cmp = ptrdiff(vma->vm, vm);
  176. if (cmp)
  177. return cmp;
  178. BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL != 0);
  179. cmp = vma->ggtt_view.type;
  180. if (!view)
  181. return cmp;
  182. cmp -= view->type;
  183. if (cmp)
  184. return cmp;
  185. /* ggtt_view.type also encodes its size so that we both distinguish
  186. * different views using it as a "type" and also use a compact (no
  187. * accessing of uninitialised padding bytes) memcmp without storing
  188. * an extra parameter or adding more code.
  189. *
  190. * To ensure that the memcmp is valid for all branches of the union,
  191. * even though the code looks like it is just comparing one branch,
  192. * we assert above that all branches have the same address, and that
  193. * each branch has a unique type/size.
  194. */
  195. BUILD_BUG_ON(I915_GGTT_VIEW_NORMAL >= I915_GGTT_VIEW_PARTIAL);
  196. BUILD_BUG_ON(I915_GGTT_VIEW_PARTIAL >= I915_GGTT_VIEW_ROTATED);
  197. BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
  198. offsetof(typeof(*view), partial));
  199. return memcmp(&vma->ggtt_view.partial, &view->partial, view->type);
  200. }
  201. int i915_vma_bind(struct i915_vma *vma, enum i915_cache_level cache_level,
  202. u32 flags);
  203. bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long cache_level);
  204. bool i915_vma_misplaced(const struct i915_vma *vma,
  205. u64 size, u64 alignment, u64 flags);
  206. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
  207. int __must_check i915_vma_unbind(struct i915_vma *vma);
  208. void i915_vma_unlink_ctx(struct i915_vma *vma);
  209. void i915_vma_close(struct i915_vma *vma);
  210. int __i915_vma_do_pin(struct i915_vma *vma,
  211. u64 size, u64 alignment, u64 flags);
  212. static inline int __must_check
  213. i915_vma_pin(struct i915_vma *vma, u64 size, u64 alignment, u64 flags)
  214. {
  215. BUILD_BUG_ON(PIN_MBZ != I915_VMA_PIN_OVERFLOW);
  216. BUILD_BUG_ON(PIN_GLOBAL != I915_VMA_GLOBAL_BIND);
  217. BUILD_BUG_ON(PIN_USER != I915_VMA_LOCAL_BIND);
  218. /* Pin early to prevent the shrinker/eviction logic from destroying
  219. * our vma as we insert and bind.
  220. */
  221. if (likely(((++vma->flags ^ flags) & I915_VMA_BIND_MASK) == 0)) {
  222. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  223. GEM_BUG_ON(i915_vma_misplaced(vma, size, alignment, flags));
  224. return 0;
  225. }
  226. return __i915_vma_do_pin(vma, size, alignment, flags);
  227. }
  228. static inline int i915_vma_pin_count(const struct i915_vma *vma)
  229. {
  230. return vma->flags & I915_VMA_PIN_MASK;
  231. }
  232. static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
  233. {
  234. return i915_vma_pin_count(vma);
  235. }
  236. static inline void __i915_vma_pin(struct i915_vma *vma)
  237. {
  238. vma->flags++;
  239. GEM_BUG_ON(vma->flags & I915_VMA_PIN_OVERFLOW);
  240. }
  241. static inline void __i915_vma_unpin(struct i915_vma *vma)
  242. {
  243. vma->flags--;
  244. }
  245. static inline void i915_vma_unpin(struct i915_vma *vma)
  246. {
  247. GEM_BUG_ON(!i915_vma_is_pinned(vma));
  248. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  249. __i915_vma_unpin(vma);
  250. }
  251. /**
  252. * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
  253. * @vma: VMA to iomap
  254. *
  255. * The passed in VMA has to be pinned in the global GTT mappable region.
  256. * An extra pinning of the VMA is acquired for the return iomapping,
  257. * the caller must call i915_vma_unpin_iomap to relinquish the pinning
  258. * after the iomapping is no longer required.
  259. *
  260. * Callers must hold the struct_mutex.
  261. *
  262. * Returns a valid iomapped pointer or ERR_PTR.
  263. */
  264. void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
  265. #define IO_ERR_PTR(x) ((void __iomem *)ERR_PTR(x))
  266. /**
  267. * i915_vma_unpin_iomap - unpins the mapping returned from i915_vma_iomap
  268. * @vma: VMA to unpin
  269. *
  270. * Unpins the previously iomapped VMA from i915_vma_pin_iomap().
  271. *
  272. * Callers must hold the struct_mutex. This function is only valid to be
  273. * called on a VMA previously iomapped by the caller with i915_vma_pin_iomap().
  274. */
  275. static inline void i915_vma_unpin_iomap(struct i915_vma *vma)
  276. {
  277. lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
  278. GEM_BUG_ON(vma->iomap == NULL);
  279. i915_vma_unpin(vma);
  280. }
  281. static inline struct page *i915_vma_first_page(struct i915_vma *vma)
  282. {
  283. GEM_BUG_ON(!vma->pages);
  284. return sg_page(vma->pages->sgl);
  285. }
  286. /**
  287. * i915_vma_pin_fence - pin fencing state
  288. * @vma: vma to pin fencing for
  289. *
  290. * This pins the fencing state (whether tiled or untiled) to make sure the
  291. * vma (and its object) is ready to be used as a scanout target. Fencing
  292. * status must be synchronize first by calling i915_vma_get_fence():
  293. *
  294. * The resulting fence pin reference must be released again with
  295. * i915_vma_unpin_fence().
  296. *
  297. * Returns:
  298. *
  299. * True if the vma has a fence, false otherwise.
  300. */
  301. static inline bool
  302. i915_vma_pin_fence(struct i915_vma *vma)
  303. {
  304. lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
  305. if (vma->fence) {
  306. vma->fence->pin_count++;
  307. return true;
  308. } else
  309. return false;
  310. }
  311. /**
  312. * i915_vma_unpin_fence - unpin fencing state
  313. * @vma: vma to unpin fencing for
  314. *
  315. * This releases the fence pin reference acquired through
  316. * i915_vma_pin_fence. It will handle both objects with and without an
  317. * attached fence correctly, callers do not need to distinguish this.
  318. */
  319. static inline void
  320. i915_vma_unpin_fence(struct i915_vma *vma)
  321. {
  322. lockdep_assert_held(&vma->obj->base.dev->struct_mutex);
  323. if (vma->fence) {
  324. GEM_BUG_ON(vma->fence->pin_count <= 0);
  325. vma->fence->pin_count--;
  326. }
  327. }
  328. #endif