i915_vma.h 13 KB

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