intel_context.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /* SPDX-License-Identifier: MIT */
  2. /*
  3. * Copyright © 2019 Intel Corporation
  4. */
  5. #ifndef __INTEL_CONTEXT_H__
  6. #define __INTEL_CONTEXT_H__
  7. #include <linux/bitops.h>
  8. #include <linux/lockdep.h>
  9. #include <linux/types.h>
  10. #include "i915_active.h"
  11. #include "i915_drv.h"
  12. #include "intel_context_types.h"
  13. #include "intel_engine_types.h"
  14. #include "intel_gt_pm.h"
  15. #include "intel_ring_types.h"
  16. #include "intel_timeline_types.h"
  17. #include "i915_trace.h"
  18. #define CE_TRACE(ce, fmt, ...) do { \
  19. const struct intel_context *ce__ = (ce); \
  20. ENGINE_TRACE(ce__->engine, "context:%llx " fmt, \
  21. ce__->timeline->fence_context, \
  22. ##__VA_ARGS__); \
  23. } while (0)
  24. #define INTEL_CONTEXT_BANNED_PREEMPT_TIMEOUT_MS (1)
  25. struct i915_gem_ww_ctx;
  26. void intel_context_init(struct intel_context *ce,
  27. struct intel_engine_cs *engine);
  28. void intel_context_fini(struct intel_context *ce);
  29. void i915_context_module_exit(void);
  30. int i915_context_module_init(void);
  31. struct intel_context *
  32. intel_context_create(struct intel_engine_cs *engine);
  33. int intel_context_alloc_state(struct intel_context *ce);
  34. void intel_context_free(struct intel_context *ce);
  35. int intel_context_reconfigure_sseu(struct intel_context *ce,
  36. const struct intel_sseu sseu);
  37. #define PARENT_SCRATCH_SIZE PAGE_SIZE
  38. static inline bool intel_context_is_child(struct intel_context *ce)
  39. {
  40. return !!ce->parallel.parent;
  41. }
  42. static inline bool intel_context_is_parent(struct intel_context *ce)
  43. {
  44. return !!ce->parallel.number_children;
  45. }
  46. static inline bool intel_context_is_pinned(struct intel_context *ce);
  47. static inline struct intel_context *
  48. intel_context_to_parent(struct intel_context *ce)
  49. {
  50. if (intel_context_is_child(ce)) {
  51. /*
  52. * The parent holds ref count to the child so it is always safe
  53. * for the parent to access the child, but the child has a
  54. * pointer to the parent without a ref. To ensure this is safe
  55. * the child should only access the parent pointer while the
  56. * parent is pinned.
  57. */
  58. GEM_BUG_ON(!intel_context_is_pinned(ce->parallel.parent));
  59. return ce->parallel.parent;
  60. } else {
  61. return ce;
  62. }
  63. }
  64. static inline bool intel_context_is_parallel(struct intel_context *ce)
  65. {
  66. return intel_context_is_child(ce) || intel_context_is_parent(ce);
  67. }
  68. void intel_context_bind_parent_child(struct intel_context *parent,
  69. struct intel_context *child);
  70. #define for_each_child(parent, ce)\
  71. list_for_each_entry(ce, &(parent)->parallel.child_list,\
  72. parallel.child_link)
  73. #define for_each_child_safe(parent, ce, cn)\
  74. list_for_each_entry_safe(ce, cn, &(parent)->parallel.child_list,\
  75. parallel.child_link)
  76. /**
  77. * intel_context_lock_pinned - Stablises the 'pinned' status of the HW context
  78. * @ce: the context
  79. *
  80. * Acquire a lock on the pinned status of the HW context, such that the context
  81. * can neither be bound to the GPU or unbound whilst the lock is held, i.e.
  82. * intel_context_is_pinned() remains stable.
  83. */
  84. static inline int intel_context_lock_pinned(struct intel_context *ce)
  85. __acquires(ce->pin_mutex)
  86. {
  87. return mutex_lock_interruptible(&ce->pin_mutex);
  88. }
  89. /**
  90. * intel_context_is_pinned - Reports the 'pinned' status
  91. * @ce: the context
  92. *
  93. * While in use by the GPU, the context, along with its ring and page
  94. * tables is pinned into memory and the GTT.
  95. *
  96. * Returns: true if the context is currently pinned for use by the GPU.
  97. */
  98. static inline bool
  99. intel_context_is_pinned(struct intel_context *ce)
  100. {
  101. return atomic_read(&ce->pin_count);
  102. }
  103. static inline void intel_context_cancel_request(struct intel_context *ce,
  104. struct i915_request *rq)
  105. {
  106. GEM_BUG_ON(!ce->ops->cancel_request);
  107. return ce->ops->cancel_request(ce, rq);
  108. }
  109. /**
  110. * intel_context_unlock_pinned - Releases the earlier locking of 'pinned' status
  111. * @ce: the context
  112. *
  113. * Releases the lock earlier acquired by intel_context_unlock_pinned().
  114. */
  115. static inline void intel_context_unlock_pinned(struct intel_context *ce)
  116. __releases(ce->pin_mutex)
  117. {
  118. mutex_unlock(&ce->pin_mutex);
  119. }
  120. int __intel_context_do_pin(struct intel_context *ce);
  121. int __intel_context_do_pin_ww(struct intel_context *ce,
  122. struct i915_gem_ww_ctx *ww);
  123. static inline bool intel_context_pin_if_active(struct intel_context *ce)
  124. {
  125. return atomic_inc_not_zero(&ce->pin_count);
  126. }
  127. static inline int intel_context_pin(struct intel_context *ce)
  128. {
  129. if (likely(intel_context_pin_if_active(ce)))
  130. return 0;
  131. return __intel_context_do_pin(ce);
  132. }
  133. static inline int intel_context_pin_ww(struct intel_context *ce,
  134. struct i915_gem_ww_ctx *ww)
  135. {
  136. if (likely(intel_context_pin_if_active(ce)))
  137. return 0;
  138. return __intel_context_do_pin_ww(ce, ww);
  139. }
  140. static inline void __intel_context_pin(struct intel_context *ce)
  141. {
  142. GEM_BUG_ON(!intel_context_is_pinned(ce));
  143. atomic_inc(&ce->pin_count);
  144. }
  145. void __intel_context_do_unpin(struct intel_context *ce, int sub);
  146. static inline void intel_context_sched_disable_unpin(struct intel_context *ce)
  147. {
  148. __intel_context_do_unpin(ce, 2);
  149. }
  150. static inline void intel_context_unpin(struct intel_context *ce)
  151. {
  152. if (!ce->ops->sched_disable) {
  153. __intel_context_do_unpin(ce, 1);
  154. } else {
  155. /*
  156. * Move ownership of this pin to the scheduling disable which is
  157. * an async operation. When that operation completes the above
  158. * intel_context_sched_disable_unpin is called potentially
  159. * unpinning the context.
  160. */
  161. while (!atomic_add_unless(&ce->pin_count, -1, 1)) {
  162. if (atomic_cmpxchg(&ce->pin_count, 1, 2) == 1) {
  163. ce->ops->sched_disable(ce);
  164. break;
  165. }
  166. }
  167. }
  168. }
  169. void intel_context_enter_engine(struct intel_context *ce);
  170. void intel_context_exit_engine(struct intel_context *ce);
  171. static inline void intel_context_enter(struct intel_context *ce)
  172. {
  173. lockdep_assert_held(&ce->timeline->mutex);
  174. if (ce->active_count++)
  175. return;
  176. ce->ops->enter(ce);
  177. ce->wakeref = intel_gt_pm_get(ce->vm->gt);
  178. }
  179. static inline void intel_context_mark_active(struct intel_context *ce)
  180. {
  181. lockdep_assert(lockdep_is_held(&ce->timeline->mutex) ||
  182. test_bit(CONTEXT_IS_PARKING, &ce->flags));
  183. ++ce->active_count;
  184. }
  185. static inline void intel_context_exit(struct intel_context *ce)
  186. {
  187. lockdep_assert_held(&ce->timeline->mutex);
  188. GEM_BUG_ON(!ce->active_count);
  189. if (--ce->active_count)
  190. return;
  191. intel_gt_pm_put_async(ce->vm->gt, ce->wakeref);
  192. ce->ops->exit(ce);
  193. }
  194. static inline struct intel_context *intel_context_get(struct intel_context *ce)
  195. {
  196. kref_get(&ce->ref);
  197. return ce;
  198. }
  199. static inline void intel_context_put(struct intel_context *ce)
  200. {
  201. kref_put(&ce->ref, ce->ops->destroy);
  202. }
  203. static inline struct intel_timeline *__must_check
  204. intel_context_timeline_lock(struct intel_context *ce)
  205. __acquires(&ce->timeline->mutex)
  206. {
  207. struct intel_timeline *tl = ce->timeline;
  208. int err;
  209. if (intel_context_is_parent(ce))
  210. err = mutex_lock_interruptible_nested(&tl->mutex, 0);
  211. else if (intel_context_is_child(ce))
  212. err = mutex_lock_interruptible_nested(&tl->mutex,
  213. ce->parallel.child_index + 1);
  214. else
  215. err = mutex_lock_interruptible(&tl->mutex);
  216. if (err)
  217. return ERR_PTR(err);
  218. return tl;
  219. }
  220. static inline void intel_context_timeline_unlock(struct intel_timeline *tl)
  221. __releases(&tl->mutex)
  222. {
  223. mutex_unlock(&tl->mutex);
  224. }
  225. int intel_context_prepare_remote_request(struct intel_context *ce,
  226. struct i915_request *rq);
  227. struct i915_request *intel_context_create_request(struct intel_context *ce);
  228. struct i915_request *intel_context_get_active_request(struct intel_context *ce);
  229. static inline bool intel_context_is_barrier(const struct intel_context *ce)
  230. {
  231. return test_bit(CONTEXT_BARRIER_BIT, &ce->flags);
  232. }
  233. static inline void intel_context_close(struct intel_context *ce)
  234. {
  235. set_bit(CONTEXT_CLOSED_BIT, &ce->flags);
  236. if (ce->ops->close)
  237. ce->ops->close(ce);
  238. }
  239. static inline bool intel_context_is_closed(const struct intel_context *ce)
  240. {
  241. return test_bit(CONTEXT_CLOSED_BIT, &ce->flags);
  242. }
  243. static inline bool intel_context_has_inflight(const struct intel_context *ce)
  244. {
  245. return test_bit(COPS_HAS_INFLIGHT_BIT, &ce->ops->flags);
  246. }
  247. static inline bool intel_context_use_semaphores(const struct intel_context *ce)
  248. {
  249. return test_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
  250. }
  251. static inline void intel_context_set_use_semaphores(struct intel_context *ce)
  252. {
  253. set_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
  254. }
  255. static inline void intel_context_clear_use_semaphores(struct intel_context *ce)
  256. {
  257. clear_bit(CONTEXT_USE_SEMAPHORES, &ce->flags);
  258. }
  259. static inline bool intel_context_is_banned(const struct intel_context *ce)
  260. {
  261. return test_bit(CONTEXT_BANNED, &ce->flags);
  262. }
  263. static inline bool intel_context_set_banned(struct intel_context *ce)
  264. {
  265. return test_and_set_bit(CONTEXT_BANNED, &ce->flags);
  266. }
  267. bool intel_context_ban(struct intel_context *ce, struct i915_request *rq);
  268. static inline bool intel_context_is_schedulable(const struct intel_context *ce)
  269. {
  270. return !test_bit(CONTEXT_EXITING, &ce->flags) &&
  271. !test_bit(CONTEXT_BANNED, &ce->flags);
  272. }
  273. static inline bool intel_context_is_exiting(const struct intel_context *ce)
  274. {
  275. return test_bit(CONTEXT_EXITING, &ce->flags);
  276. }
  277. static inline bool intel_context_set_exiting(struct intel_context *ce)
  278. {
  279. return test_and_set_bit(CONTEXT_EXITING, &ce->flags);
  280. }
  281. bool intel_context_revoke(struct intel_context *ce);
  282. static inline bool
  283. intel_context_force_single_submission(const struct intel_context *ce)
  284. {
  285. return test_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
  286. }
  287. static inline void
  288. intel_context_set_single_submission(struct intel_context *ce)
  289. {
  290. __set_bit(CONTEXT_FORCE_SINGLE_SUBMISSION, &ce->flags);
  291. }
  292. static inline bool
  293. intel_context_nopreempt(const struct intel_context *ce)
  294. {
  295. return test_bit(CONTEXT_NOPREEMPT, &ce->flags);
  296. }
  297. static inline void
  298. intel_context_set_nopreempt(struct intel_context *ce)
  299. {
  300. set_bit(CONTEXT_NOPREEMPT, &ce->flags);
  301. }
  302. static inline void
  303. intel_context_clear_nopreempt(struct intel_context *ce)
  304. {
  305. clear_bit(CONTEXT_NOPREEMPT, &ce->flags);
  306. }
  307. #if IS_ENABLED(CONFIG_DRM_I915_REPLAY_GPU_HANGS_API)
  308. static inline bool intel_context_has_own_state(const struct intel_context *ce)
  309. {
  310. return test_bit(CONTEXT_OWN_STATE, &ce->flags);
  311. }
  312. static inline bool intel_context_set_own_state(struct intel_context *ce)
  313. {
  314. return test_and_set_bit(CONTEXT_OWN_STATE, &ce->flags);
  315. }
  316. #else
  317. static inline bool intel_context_has_own_state(const struct intel_context *ce)
  318. {
  319. return false;
  320. }
  321. static inline bool intel_context_set_own_state(struct intel_context *ce)
  322. {
  323. return true;
  324. }
  325. #endif
  326. u64 intel_context_get_total_runtime_ns(struct intel_context *ce);
  327. u64 intel_context_get_avg_runtime_ns(struct intel_context *ce);
  328. static inline u64 intel_context_clock(void)
  329. {
  330. /* As we mix CS cycles with CPU clocks, use the raw monotonic clock. */
  331. return ktime_get_raw_fast_ns();
  332. }
  333. #endif /* __INTEL_CONTEXT_H__ */