i915_vma.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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 <linux/rbtree.h>
  28. #include <drm/drm_mm.h>
  29. #include "gt/intel_ggtt_fencing.h"
  30. #include "gem/i915_gem_object.h"
  31. #include "i915_gem_gtt.h"
  32. #include "i915_active.h"
  33. #include "i915_request.h"
  34. #include "i915_vma_resource.h"
  35. #include "i915_vma_types.h"
  36. struct i915_vma *
  37. i915_vma_instance(struct drm_i915_gem_object *obj,
  38. struct i915_address_space *vm,
  39. const struct i915_gtt_view *view);
  40. void i915_vma_unpin_and_release(struct i915_vma **p_vma, unsigned int flags);
  41. #define I915_VMA_RELEASE_MAP BIT(0)
  42. static inline bool i915_vma_is_active(const struct i915_vma *vma)
  43. {
  44. return !i915_active_is_idle(&vma->active);
  45. }
  46. /* do not reserve memory to prevent deadlocks */
  47. #define __EXEC_OBJECT_NO_RESERVE BIT(31)
  48. int __must_check _i915_vma_move_to_active(struct i915_vma *vma,
  49. struct i915_request *rq,
  50. struct dma_fence *fence,
  51. unsigned int flags);
  52. static inline int __must_check
  53. i915_vma_move_to_active(struct i915_vma *vma, struct i915_request *rq,
  54. unsigned int flags)
  55. {
  56. return _i915_vma_move_to_active(vma, rq, &rq->fence, flags);
  57. }
  58. #define __i915_vma_flags(v) ((unsigned long *)&(v)->flags.counter)
  59. static inline bool i915_vma_is_ggtt(const struct i915_vma *vma)
  60. {
  61. return test_bit(I915_VMA_GGTT_BIT, __i915_vma_flags(vma));
  62. }
  63. static inline bool i915_vma_is_dpt(const struct i915_vma *vma)
  64. {
  65. return i915_is_dpt(vma->vm);
  66. }
  67. static inline bool i915_vma_has_ggtt_write(const struct i915_vma *vma)
  68. {
  69. return test_bit(I915_VMA_GGTT_WRITE_BIT, __i915_vma_flags(vma));
  70. }
  71. static inline void i915_vma_set_ggtt_write(struct i915_vma *vma)
  72. {
  73. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  74. set_bit(I915_VMA_GGTT_WRITE_BIT, __i915_vma_flags(vma));
  75. }
  76. static inline bool i915_vma_unset_ggtt_write(struct i915_vma *vma)
  77. {
  78. return test_and_clear_bit(I915_VMA_GGTT_WRITE_BIT,
  79. __i915_vma_flags(vma));
  80. }
  81. void i915_vma_flush_writes(struct i915_vma *vma);
  82. static inline bool i915_vma_is_map_and_fenceable(const struct i915_vma *vma)
  83. {
  84. return test_bit(I915_VMA_CAN_FENCE_BIT, __i915_vma_flags(vma));
  85. }
  86. static inline bool i915_vma_set_userfault(struct i915_vma *vma)
  87. {
  88. GEM_BUG_ON(!i915_vma_is_map_and_fenceable(vma));
  89. return test_and_set_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags(vma));
  90. }
  91. static inline void i915_vma_unset_userfault(struct i915_vma *vma)
  92. {
  93. return clear_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags(vma));
  94. }
  95. static inline bool i915_vma_has_userfault(const struct i915_vma *vma)
  96. {
  97. return test_bit(I915_VMA_USERFAULT_BIT, __i915_vma_flags(vma));
  98. }
  99. static inline bool i915_vma_is_closed(const struct i915_vma *vma)
  100. {
  101. return !list_empty(&vma->closed_link);
  102. }
  103. static inline u32 i915_ggtt_offset(const struct i915_vma *vma)
  104. {
  105. GEM_BUG_ON(!i915_vma_is_ggtt(vma));
  106. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  107. GEM_BUG_ON(upper_32_bits(vma->node.start));
  108. GEM_BUG_ON(upper_32_bits(vma->node.start + vma->node.size - 1));
  109. return lower_32_bits(vma->node.start);
  110. }
  111. static inline u32 i915_ggtt_pin_bias(struct i915_vma *vma)
  112. {
  113. return i915_vm_to_ggtt(vma->vm)->pin_bias;
  114. }
  115. static inline struct i915_vma *i915_vma_get(struct i915_vma *vma)
  116. {
  117. i915_gem_object_get(vma->obj);
  118. return vma;
  119. }
  120. static inline struct i915_vma *i915_vma_tryget(struct i915_vma *vma)
  121. {
  122. if (likely(kref_get_unless_zero(&vma->obj->base.refcount)))
  123. return vma;
  124. return NULL;
  125. }
  126. static inline void i915_vma_put(struct i915_vma *vma)
  127. {
  128. i915_gem_object_put(vma->obj);
  129. }
  130. static inline long
  131. i915_vma_compare(struct i915_vma *vma,
  132. struct i915_address_space *vm,
  133. const struct i915_gtt_view *view)
  134. {
  135. ptrdiff_t cmp;
  136. GEM_BUG_ON(view && !i915_is_ggtt_or_dpt(vm));
  137. cmp = ptrdiff(vma->vm, vm);
  138. if (cmp)
  139. return cmp;
  140. BUILD_BUG_ON(I915_GTT_VIEW_NORMAL != 0);
  141. cmp = vma->gtt_view.type;
  142. if (!view)
  143. return cmp;
  144. cmp -= view->type;
  145. if (cmp)
  146. return cmp;
  147. assert_i915_gem_gtt_types();
  148. /* gtt_view.type also encodes its size so that we both distinguish
  149. * different views using it as a "type" and also use a compact (no
  150. * accessing of uninitialised padding bytes) memcmp without storing
  151. * an extra parameter or adding more code.
  152. *
  153. * To ensure that the memcmp is valid for all branches of the union,
  154. * even though the code looks like it is just comparing one branch,
  155. * we assert above that all branches have the same address, and that
  156. * each branch has a unique type/size.
  157. */
  158. BUILD_BUG_ON(I915_GTT_VIEW_NORMAL >= I915_GTT_VIEW_PARTIAL);
  159. BUILD_BUG_ON(I915_GTT_VIEW_PARTIAL >= I915_GTT_VIEW_ROTATED);
  160. BUILD_BUG_ON(I915_GTT_VIEW_ROTATED >= I915_GTT_VIEW_REMAPPED);
  161. BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
  162. offsetof(typeof(*view), partial));
  163. BUILD_BUG_ON(offsetof(typeof(*view), rotated) !=
  164. offsetof(typeof(*view), remapped));
  165. return memcmp(&vma->gtt_view.partial, &view->partial, view->type);
  166. }
  167. struct i915_vma_work *i915_vma_work(void);
  168. int i915_vma_bind(struct i915_vma *vma,
  169. enum i915_cache_level cache_level,
  170. u32 flags,
  171. struct i915_vma_work *work,
  172. struct i915_vma_resource *vma_res);
  173. bool i915_gem_valid_gtt_space(struct i915_vma *vma, unsigned long color);
  174. bool i915_vma_misplaced(const struct i915_vma *vma,
  175. u64 size, u64 alignment, u64 flags);
  176. void __i915_vma_set_map_and_fenceable(struct i915_vma *vma);
  177. void i915_vma_revoke_mmap(struct i915_vma *vma);
  178. void vma_invalidate_tlb(struct i915_address_space *vm, u32 *tlb);
  179. struct dma_fence *__i915_vma_evict(struct i915_vma *vma, bool async);
  180. int __i915_vma_unbind(struct i915_vma *vma);
  181. int __must_check i915_vma_unbind(struct i915_vma *vma);
  182. int __must_check i915_vma_unbind_async(struct i915_vma *vma, bool trylock_vm);
  183. int __must_check i915_vma_unbind_unlocked(struct i915_vma *vma);
  184. void i915_vma_unlink_ctx(struct i915_vma *vma);
  185. void i915_vma_close(struct i915_vma *vma);
  186. void i915_vma_reopen(struct i915_vma *vma);
  187. void i915_vma_destroy_locked(struct i915_vma *vma);
  188. void i915_vma_destroy(struct i915_vma *vma);
  189. #define assert_vma_held(vma) dma_resv_assert_held((vma)->obj->base.resv)
  190. static inline void i915_vma_lock(struct i915_vma *vma)
  191. {
  192. dma_resv_lock(vma->obj->base.resv, NULL);
  193. }
  194. static inline void i915_vma_unlock(struct i915_vma *vma)
  195. {
  196. dma_resv_unlock(vma->obj->base.resv);
  197. }
  198. int __must_check
  199. i915_vma_pin_ww(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  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. struct i915_gem_ww_ctx ww;
  205. int err;
  206. i915_gem_ww_ctx_init(&ww, true);
  207. retry:
  208. err = i915_gem_object_lock(vma->obj, &ww);
  209. if (!err)
  210. err = i915_vma_pin_ww(vma, &ww, size, alignment, flags);
  211. if (err == -EDEADLK) {
  212. err = i915_gem_ww_ctx_backoff(&ww);
  213. if (!err)
  214. goto retry;
  215. }
  216. i915_gem_ww_ctx_fini(&ww);
  217. return err;
  218. }
  219. int i915_ggtt_pin(struct i915_vma *vma, struct i915_gem_ww_ctx *ww,
  220. u32 align, unsigned int flags);
  221. static inline int i915_vma_pin_count(const struct i915_vma *vma)
  222. {
  223. return atomic_read(&vma->flags) & I915_VMA_PIN_MASK;
  224. }
  225. static inline bool i915_vma_is_pinned(const struct i915_vma *vma)
  226. {
  227. return i915_vma_pin_count(vma);
  228. }
  229. static inline void __i915_vma_pin(struct i915_vma *vma)
  230. {
  231. atomic_inc(&vma->flags);
  232. GEM_BUG_ON(!i915_vma_is_pinned(vma));
  233. }
  234. static inline void __i915_vma_unpin(struct i915_vma *vma)
  235. {
  236. GEM_BUG_ON(!i915_vma_is_pinned(vma));
  237. atomic_dec(&vma->flags);
  238. }
  239. static inline void i915_vma_unpin(struct i915_vma *vma)
  240. {
  241. GEM_BUG_ON(!drm_mm_node_allocated(&vma->node));
  242. __i915_vma_unpin(vma);
  243. }
  244. static inline bool i915_vma_is_bound(const struct i915_vma *vma,
  245. unsigned int where)
  246. {
  247. return atomic_read(&vma->flags) & where;
  248. }
  249. static inline bool i915_node_color_differs(const struct drm_mm_node *node,
  250. unsigned long color)
  251. {
  252. return drm_mm_node_allocated(node) && node->color != color;
  253. }
  254. /**
  255. * i915_vma_pin_iomap - calls ioremap_wc to map the GGTT VMA via the aperture
  256. * @vma: VMA to iomap
  257. *
  258. * The passed in VMA has to be pinned in the global GTT mappable region.
  259. * An extra pinning of the VMA is acquired for the return iomapping,
  260. * the caller must call i915_vma_unpin_iomap to relinquish the pinning
  261. * after the iomapping is no longer required.
  262. *
  263. * Returns a valid iomapped pointer or ERR_PTR.
  264. */
  265. void __iomem *i915_vma_pin_iomap(struct i915_vma *vma);
  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. * This function is only valid to be called on a VMA previously
  273. * iomapped by the caller with i915_vma_pin_iomap().
  274. */
  275. void i915_vma_unpin_iomap(struct i915_vma *vma);
  276. /**
  277. * i915_vma_pin_fence - pin fencing state
  278. * @vma: vma to pin fencing for
  279. *
  280. * This pins the fencing state (whether tiled or untiled) to make sure the
  281. * vma (and its object) is ready to be used as a scanout target. Fencing
  282. * status must be synchronize first by calling i915_vma_get_fence():
  283. *
  284. * The resulting fence pin reference must be released again with
  285. * i915_vma_unpin_fence().
  286. *
  287. * Returns:
  288. *
  289. * True if the vma has a fence, false otherwise.
  290. */
  291. int __must_check i915_vma_pin_fence(struct i915_vma *vma);
  292. void i915_vma_revoke_fence(struct i915_vma *vma);
  293. int __i915_vma_pin_fence(struct i915_vma *vma);
  294. static inline void __i915_vma_unpin_fence(struct i915_vma *vma)
  295. {
  296. GEM_BUG_ON(atomic_read(&vma->fence->pin_count) <= 0);
  297. atomic_dec(&vma->fence->pin_count);
  298. }
  299. /**
  300. * i915_vma_unpin_fence - unpin fencing state
  301. * @vma: vma to unpin fencing for
  302. *
  303. * This releases the fence pin reference acquired through
  304. * i915_vma_pin_fence. It will handle both objects with and without an
  305. * attached fence correctly, callers do not need to distinguish this.
  306. */
  307. static inline void
  308. i915_vma_unpin_fence(struct i915_vma *vma)
  309. {
  310. if (vma->fence)
  311. __i915_vma_unpin_fence(vma);
  312. }
  313. void i915_vma_parked(struct intel_gt *gt);
  314. static inline bool i915_vma_is_scanout(const struct i915_vma *vma)
  315. {
  316. return test_bit(I915_VMA_SCANOUT_BIT, __i915_vma_flags(vma));
  317. }
  318. static inline void i915_vma_mark_scanout(struct i915_vma *vma)
  319. {
  320. set_bit(I915_VMA_SCANOUT_BIT, __i915_vma_flags(vma));
  321. }
  322. static inline void i915_vma_clear_scanout(struct i915_vma *vma)
  323. {
  324. clear_bit(I915_VMA_SCANOUT_BIT, __i915_vma_flags(vma));
  325. }
  326. #define for_each_until(cond) if (cond) break; else
  327. /**
  328. * for_each_ggtt_vma - Iterate over the GGTT VMA belonging to an object.
  329. * @V: the #i915_vma iterator
  330. * @OBJ: the #drm_i915_gem_object
  331. *
  332. * GGTT VMA are placed at the being of the object's vma_list, see
  333. * vma_create(), so we can stop our walk as soon as we see a ppgtt VMA,
  334. * or the list is empty ofc.
  335. */
  336. #define for_each_ggtt_vma(V, OBJ) \
  337. list_for_each_entry(V, &(OBJ)->vma.list, obj_link) \
  338. for_each_until(!i915_vma_is_ggtt(V))
  339. struct i915_vma *i915_vma_make_unshrinkable(struct i915_vma *vma);
  340. void i915_vma_make_shrinkable(struct i915_vma *vma);
  341. void i915_vma_make_purgeable(struct i915_vma *vma);
  342. int i915_vma_wait_for_bind(struct i915_vma *vma);
  343. static inline int i915_vma_sync(struct i915_vma *vma)
  344. {
  345. /* Wait for the asynchronous bindings and pending GPU reads */
  346. return i915_active_wait(&vma->active);
  347. }
  348. /**
  349. * i915_vma_get_current_resource - Get the current resource of the vma
  350. * @vma: The vma to get the current resource from.
  351. *
  352. * It's illegal to call this function if the vma is not bound.
  353. *
  354. * Return: A refcounted pointer to the current vma resource
  355. * of the vma, assuming the vma is bound.
  356. */
  357. static inline struct i915_vma_resource *
  358. i915_vma_get_current_resource(struct i915_vma *vma)
  359. {
  360. return i915_vma_resource_get(vma->resource);
  361. }
  362. #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
  363. void i915_vma_resource_init_from_vma(struct i915_vma_resource *vma_res,
  364. struct i915_vma *vma);
  365. #endif
  366. void i915_vma_module_exit(void);
  367. int i915_vma_module_init(void);
  368. I915_SELFTEST_DECLARE(int i915_vma_get_pages(struct i915_vma *vma));
  369. I915_SELFTEST_DECLARE(void i915_vma_put_pages(struct i915_vma *vma));
  370. #endif