mali_kbase_js_ctx_attr.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. *
  3. * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
  4. *
  5. * This program is free software and is provided to you under the terms of the
  6. * GNU General Public License version 2 as published by the Free Software
  7. * Foundation, and any use by you of this program is subject to the terms
  8. * of such GNU licence.
  9. *
  10. * A copy of the licence is included with the program, and can also be obtained
  11. * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  12. * Boston, MA 02110-1301, USA.
  13. *
  14. */
  15. #include <mali_kbase.h>
  16. #include <mali_kbase_config.h>
  17. /*
  18. * Private functions follow
  19. */
  20. /**
  21. * @brief Check whether a ctx has a certain attribute, and if so, retain that
  22. * attribute on the runpool.
  23. *
  24. * Requires:
  25. * - jsctx mutex
  26. * - runpool_irq spinlock
  27. * - ctx is scheduled on the runpool
  28. *
  29. * @return true indicates a change in ctx attributes state of the runpool.
  30. * In this state, the scheduler might be able to submit more jobs than
  31. * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
  32. * or similar is called sometime later.
  33. * @return false indicates no change in ctx attributes state of the runpool.
  34. */
  35. static bool kbasep_js_ctx_attr_runpool_retain_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
  36. {
  37. struct kbasep_js_device_data *js_devdata;
  38. struct kbasep_js_kctx_info *js_kctx_info;
  39. bool runpool_state_changed = false;
  40. KBASE_DEBUG_ASSERT(kbdev != NULL);
  41. KBASE_DEBUG_ASSERT(kctx != NULL);
  42. KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
  43. js_devdata = &kbdev->js_data;
  44. js_kctx_info = &kctx->jctx.sched_info;
  45. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  46. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  47. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != false);
  48. if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != false) {
  49. KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] < S8_MAX);
  50. ++(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
  51. if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 1) {
  52. /* First refcount indicates a state change */
  53. runpool_state_changed = true;
  54. KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_RUNPOOL, kctx, NULL, 0u, attribute);
  55. }
  56. }
  57. return runpool_state_changed;
  58. }
  59. /**
  60. * @brief Check whether a ctx has a certain attribute, and if so, release that
  61. * attribute on the runpool.
  62. *
  63. * Requires:
  64. * - jsctx mutex
  65. * - runpool_irq spinlock
  66. * - ctx is scheduled on the runpool
  67. *
  68. * @return true indicates a change in ctx attributes state of the runpool.
  69. * In this state, the scheduler might be able to submit more jobs than
  70. * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
  71. * or similar is called sometime later.
  72. * @return false indicates no change in ctx attributes state of the runpool.
  73. */
  74. static bool kbasep_js_ctx_attr_runpool_release_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
  75. {
  76. struct kbasep_js_device_data *js_devdata;
  77. struct kbasep_js_kctx_info *js_kctx_info;
  78. bool runpool_state_changed = false;
  79. KBASE_DEBUG_ASSERT(kbdev != NULL);
  80. KBASE_DEBUG_ASSERT(kctx != NULL);
  81. KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
  82. js_devdata = &kbdev->js_data;
  83. js_kctx_info = &kctx->jctx.sched_info;
  84. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  85. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  86. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != false);
  87. if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != false) {
  88. KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] > 0);
  89. --(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
  90. if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 0) {
  91. /* Last de-refcount indicates a state change */
  92. runpool_state_changed = true;
  93. KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_RUNPOOL, kctx, NULL, 0u, attribute);
  94. }
  95. }
  96. return runpool_state_changed;
  97. }
  98. /**
  99. * @brief Retain a certain attribute on a ctx, also retaining it on the runpool
  100. * if the context is scheduled.
  101. *
  102. * Requires:
  103. * - jsctx mutex
  104. * - If the context is scheduled, then runpool_irq spinlock must also be held
  105. *
  106. * @return true indicates a change in ctx attributes state of the runpool.
  107. * This may allow the scheduler to submit more jobs than previously.
  108. * @return false indicates no change in ctx attributes state of the runpool.
  109. */
  110. static bool kbasep_js_ctx_attr_ctx_retain_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
  111. {
  112. struct kbasep_js_kctx_info *js_kctx_info;
  113. bool runpool_state_changed = false;
  114. KBASE_DEBUG_ASSERT(kbdev != NULL);
  115. KBASE_DEBUG_ASSERT(kctx != NULL);
  116. KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
  117. js_kctx_info = &kctx->jctx.sched_info;
  118. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  119. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  120. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] < U32_MAX);
  121. ++(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
  122. if (js_kctx_info->ctx.is_scheduled != false && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
  123. /* Only ref-count the attribute on the runpool for the first time this contexts sees this attribute */
  124. KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_CTX, kctx, NULL, 0u, attribute);
  125. runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, attribute);
  126. }
  127. return runpool_state_changed;
  128. }
  129. /*
  130. * @brief Release a certain attribute on a ctx, also releasing it from the runpool
  131. * if the context is scheduled.
  132. *
  133. * Requires:
  134. * - jsctx mutex
  135. * - If the context is scheduled, then runpool_irq spinlock must also be held
  136. *
  137. * @return true indicates a change in ctx attributes state of the runpool.
  138. * This may allow the scheduler to submit more jobs than previously.
  139. * @return false indicates no change in ctx attributes state of the runpool.
  140. */
  141. static bool kbasep_js_ctx_attr_ctx_release_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
  142. {
  143. struct kbasep_js_kctx_info *js_kctx_info;
  144. bool runpool_state_changed = false;
  145. KBASE_DEBUG_ASSERT(kbdev != NULL);
  146. KBASE_DEBUG_ASSERT(kctx != NULL);
  147. KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
  148. js_kctx_info = &kctx->jctx.sched_info;
  149. lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
  150. KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] > 0);
  151. if (js_kctx_info->ctx.is_scheduled != false && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
  152. lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
  153. /* Only de-ref-count the attribute on the runpool when this is the last ctx-reference to it */
  154. runpool_state_changed = kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, attribute);
  155. KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_CTX, kctx, NULL, 0u, attribute);
  156. }
  157. /* De-ref must happen afterwards, because kbasep_js_ctx_attr_runpool_release() needs to check it too */
  158. --(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
  159. return runpool_state_changed;
  160. }
  161. /*
  162. * More commonly used public functions
  163. */
  164. void kbasep_js_ctx_attr_set_initial_attrs(struct kbase_device *kbdev, struct kbase_context *kctx)
  165. {
  166. struct kbasep_js_kctx_info *js_kctx_info;
  167. bool runpool_state_changed = false;
  168. KBASE_DEBUG_ASSERT(kbdev != NULL);
  169. KBASE_DEBUG_ASSERT(kctx != NULL);
  170. js_kctx_info = &kctx->jctx.sched_info;
  171. if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) != false) {
  172. /* This context never submits, so don't track any scheduling attributes */
  173. return;
  174. }
  175. /* Transfer attributes held in the context flags for contexts that have submit enabled */
  176. /* ... More attributes can be added here ... */
  177. /* The context should not have been scheduled yet, so ASSERT if this caused
  178. * runpool state changes (note that other threads *can't* affect the value
  179. * of runpool_state_changed, due to how it's calculated) */
  180. KBASE_DEBUG_ASSERT(runpool_state_changed == false);
  181. CSTD_UNUSED(runpool_state_changed);
  182. }
  183. void kbasep_js_ctx_attr_runpool_retain_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
  184. {
  185. bool runpool_state_changed;
  186. int i;
  187. /* Retain any existing attributes */
  188. for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
  189. if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (enum kbasep_js_ctx_attr) i) != false) {
  190. /* The context is being scheduled in, so update the runpool with the new attributes */
  191. runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, (enum kbasep_js_ctx_attr) i);
  192. /* We don't need to know about state changed, because retaining a
  193. * context occurs on scheduling it, and that itself will also try
  194. * to run new atoms */
  195. CSTD_UNUSED(runpool_state_changed);
  196. }
  197. }
  198. }
  199. bool kbasep_js_ctx_attr_runpool_release_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
  200. {
  201. bool runpool_state_changed = false;
  202. int i;
  203. /* Release any existing attributes */
  204. for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
  205. if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (enum kbasep_js_ctx_attr) i) != false) {
  206. /* The context is being scheduled out, so update the runpool on the removed attributes */
  207. runpool_state_changed |= kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, (enum kbasep_js_ctx_attr) i);
  208. }
  209. }
  210. return runpool_state_changed;
  211. }
  212. void kbasep_js_ctx_attr_ctx_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
  213. {
  214. bool runpool_state_changed = false;
  215. base_jd_core_req core_req;
  216. KBASE_DEBUG_ASSERT(katom);
  217. core_req = katom->core_req;
  218. if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
  219. runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
  220. else
  221. runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
  222. if ((core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE | BASE_JD_REQ_T)) != 0 && (core_req & (BASE_JD_REQ_COHERENT_GROUP | BASE_JD_REQ_SPECIFIC_COHERENT_GROUP)) == 0) {
  223. /* Atom that can run on slot1 or slot2, and can use all cores */
  224. runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
  225. }
  226. /* We don't need to know about state changed, because retaining an
  227. * atom occurs on adding it, and that itself will also try to run
  228. * new atoms */
  229. CSTD_UNUSED(runpool_state_changed);
  230. }
  231. bool kbasep_js_ctx_attr_ctx_release_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbasep_js_atom_retained_state *katom_retained_state)
  232. {
  233. bool runpool_state_changed = false;
  234. base_jd_core_req core_req;
  235. KBASE_DEBUG_ASSERT(katom_retained_state);
  236. core_req = katom_retained_state->core_req;
  237. /* No-op for invalid atoms */
  238. if (kbasep_js_atom_retained_state_is_valid(katom_retained_state) == false)
  239. return false;
  240. if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
  241. runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
  242. else
  243. runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
  244. if ((core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE | BASE_JD_REQ_T)) != 0 && (core_req & (BASE_JD_REQ_COHERENT_GROUP | BASE_JD_REQ_SPECIFIC_COHERENT_GROUP)) == 0) {
  245. /* Atom that can run on slot1 or slot2, and can use all cores */
  246. runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
  247. }
  248. return runpool_state_changed;
  249. }