i915_vma.h 12 KB

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