i915_vma.h 11 KB

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