i915_vma.h 12 KB

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