mali_kbase_js_policy_cfs.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /*
  2. *
  3. * (C) COPYRIGHT 2011-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. /*
  16. * Job Scheduler: Completely Fair Policy Implementation
  17. */
  18. #include <mali_kbase.h>
  19. #include <mali_kbase_js.h>
  20. #include <mali_kbase_js_policy_cfs.h>
  21. #include <linux/version.h>
  22. #include <linux/sched/rt.h>
  23. /**
  24. * Define for when dumping is enabled.
  25. * This should not be based on the instrumentation level as whether dumping is enabled for a particular level is down to the integrator.
  26. * However this is being used for now as otherwise the cinstr headers would be needed.
  27. */
  28. #define CINSTR_DUMPING_ENABLED (2 == MALI_INSTRUMENTATION_LEVEL)
  29. /* Fixed point constants used for runtime weight calculations */
  30. #define WEIGHT_FIXEDPOINT_SHIFT 10
  31. #define WEIGHT_TABLE_SIZE 40
  32. #define WEIGHT_0_NICE (WEIGHT_TABLE_SIZE/2)
  33. #define WEIGHT_0_VAL (1 << WEIGHT_FIXEDPOINT_SHIFT)
  34. #define PROCESS_PRIORITY_MIN (-20)
  35. #define PROCESS_PRIORITY_MAX (19)
  36. /* Defines for easy asserts 'is scheduled'/'is queued'/'is neither queued norscheduled' */
  37. #define KBASEP_JS_CHECKFLAG_QUEUED (1u << 0) /**< Check the queued state */
  38. #define KBASEP_JS_CHECKFLAG_SCHEDULED (1u << 1) /**< Check the scheduled state */
  39. #define KBASEP_JS_CHECKFLAG_IS_QUEUED (1u << 2) /**< Expect queued state to be set */
  40. #define KBASEP_JS_CHECKFLAG_IS_SCHEDULED (1u << 3) /**< Expect scheduled state to be set */
  41. enum {
  42. KBASEP_JS_CHECK_NOTQUEUED = KBASEP_JS_CHECKFLAG_QUEUED,
  43. KBASEP_JS_CHECK_NOTSCHEDULED = KBASEP_JS_CHECKFLAG_SCHEDULED,
  44. KBASEP_JS_CHECK_QUEUED = KBASEP_JS_CHECKFLAG_QUEUED | KBASEP_JS_CHECKFLAG_IS_QUEUED,
  45. KBASEP_JS_CHECK_SCHEDULED = KBASEP_JS_CHECKFLAG_SCHEDULED | KBASEP_JS_CHECKFLAG_IS_SCHEDULED
  46. };
  47. typedef u32 kbasep_js_check;
  48. /*
  49. * Private Functions
  50. */
  51. /* Table autogenerated using util built from: base/tools/gen_cfs_weight_of_prio/ */
  52. /* weight = 1.25 */
  53. static const int weight_of_priority[] = {
  54. /* -20 */ 11, 14, 18, 23,
  55. /* -16 */ 29, 36, 45, 56,
  56. /* -12 */ 70, 88, 110, 137,
  57. /* -8 */ 171, 214, 268, 335,
  58. /* -4 */ 419, 524, 655, 819,
  59. /* 0 */ 1024, 1280, 1600, 2000,
  60. /* 4 */ 2500, 3125, 3906, 4883,
  61. /* 8 */ 6104, 7630, 9538, 11923,
  62. /* 12 */ 14904, 18630, 23288, 29110,
  63. /* 16 */ 36388, 45485, 56856, 71070
  64. };
  65. /*
  66. * Note: There is nothing to stop the priority of the ctx containing
  67. * ctx_info changing during or immediately after this function is called
  68. * (because its jsctx_mutex cannot be held during IRQ). Therefore, this
  69. * function should only be seen as a heuristic guide as to the priority weight
  70. * of the context.
  71. */
  72. static u64 priority_weight(struct kbasep_js_policy_cfs_ctx *ctx_info, u64 time_us)
  73. {
  74. u64 time_delta_us;
  75. int priority;
  76. priority = ctx_info->process_priority;
  77. /* Adjust runtime_us using priority weight if required */
  78. if (priority != 0 && time_us != 0) {
  79. int clamped_priority;
  80. /* Clamp values to min..max weights */
  81. if (priority > PROCESS_PRIORITY_MAX)
  82. clamped_priority = PROCESS_PRIORITY_MAX;
  83. else if (priority < PROCESS_PRIORITY_MIN)
  84. clamped_priority = PROCESS_PRIORITY_MIN;
  85. else
  86. clamped_priority = priority;
  87. /* Fixed point multiplication */
  88. time_delta_us = (time_us * weight_of_priority[WEIGHT_0_NICE + clamped_priority]);
  89. /* Remove fraction */
  90. time_delta_us = time_delta_us >> WEIGHT_FIXEDPOINT_SHIFT;
  91. /* Make sure the time always increases */
  92. if (time_delta_us == 0)
  93. time_delta_us++;
  94. } else {
  95. time_delta_us = time_us;
  96. }
  97. return time_delta_us;
  98. }
  99. #if KBASE_TRACE_ENABLE
  100. static int kbasep_js_policy_trace_get_refcnt_nolock(struct kbase_device *kbdev, struct kbase_context *kctx)
  101. {
  102. struct kbasep_js_device_data *js_devdata;
  103. int as_nr;
  104. int refcnt = 0;
  105. js_devdata = &kbdev->js_data;
  106. as_nr = kctx->as_nr;
  107. if (as_nr != KBASEP_AS_NR_INVALID) {
  108. struct kbasep_js_per_as_data *js_per_as_data;
  109. js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
  110. refcnt = js_per_as_data->as_busy_refcount;
  111. }
  112. return refcnt;
  113. }
  114. static inline int kbasep_js_policy_trace_get_refcnt(struct kbase_device *kbdev, struct kbase_context *kctx)
  115. {
  116. unsigned long flags;
  117. struct kbasep_js_device_data *js_devdata;
  118. int refcnt = 0;
  119. js_devdata = &kbdev->js_data;
  120. spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
  121. refcnt = kbasep_js_policy_trace_get_refcnt_nolock(kbdev, kctx);
  122. spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
  123. return refcnt;
  124. }
  125. #else /* KBASE_TRACE_ENABLE */
  126. static inline int kbasep_js_policy_trace_get_refcnt(struct kbase_device *kbdev, struct kbase_context *kctx)
  127. {
  128. CSTD_UNUSED(kbdev);
  129. CSTD_UNUSED(kctx);
  130. return 0;
  131. }
  132. #endif /* KBASE_TRACE_ENABLE */
  133. /*
  134. * Non-private functions
  135. */
  136. int kbasep_js_policy_init(struct kbase_device *kbdev)
  137. {
  138. struct kbasep_js_device_data *js_devdata;
  139. struct kbasep_js_policy_cfs *policy_info;
  140. KBASE_DEBUG_ASSERT(kbdev != NULL);
  141. js_devdata = &kbdev->js_data;
  142. policy_info = &js_devdata->policy.cfs;
  143. atomic64_set(&policy_info->least_runtime_us, KBASEP_JS_RUNTIME_EMPTY);
  144. atomic64_set(&policy_info->rt_least_runtime_us, KBASEP_JS_RUNTIME_EMPTY);
  145. policy_info->head_runtime_us = 0;
  146. return 0;
  147. }
  148. void kbasep_js_policy_term(union kbasep_js_policy *js_policy)
  149. {
  150. CSTD_UNUSED(js_policy);
  151. }
  152. int kbasep_js_policy_init_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
  153. {
  154. struct kbasep_js_device_data *js_devdata;
  155. struct kbasep_js_policy_cfs_ctx *ctx_info;
  156. struct kbasep_js_policy_cfs *policy_info;
  157. int policy;
  158. KBASE_DEBUG_ASSERT(kbdev != NULL);
  159. KBASE_DEBUG_ASSERT(kctx != NULL);
  160. js_devdata = &kbdev->js_data;
  161. policy_info = &kbdev->js_data.policy.cfs;
  162. ctx_info = &kctx->jctx.sched_info.runpool.policy_ctx.cfs;
  163. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_POLICY_INIT_CTX, kctx, NULL, 0u, kbasep_js_policy_trace_get_refcnt(kbdev, kctx));
  164. policy = current->policy;
  165. if (policy == SCHED_FIFO || policy == SCHED_RR) {
  166. ctx_info->process_rt_policy = true;
  167. ctx_info->process_priority = (((MAX_RT_PRIO - 1) - current->rt_priority) / 5) - 20;
  168. } else {
  169. ctx_info->process_rt_policy = false;
  170. ctx_info->process_priority = (current->static_prio - MAX_RT_PRIO) - 20;
  171. }
  172. /* Initial runtime (relative to least-run context runtime)
  173. *
  174. * This uses the Policy Queue's most up-to-date head_runtime_us by using the
  175. * queue mutex to issue memory barriers - also ensure future updates to
  176. * head_runtime_us occur strictly after this context is initialized */
  177. mutex_lock(&js_devdata->queue_mutex);
  178. /* No need to hold the the runpool_irq.lock here, because we're initializing
  179. * the value, and the context is definitely not being updated in the
  180. * runpool at this point. The queue_mutex ensures the memory barrier. */
  181. ctx_info->runtime_us = policy_info->head_runtime_us + priority_weight(ctx_info, (u64) js_devdata->cfs_ctx_runtime_init_slices * (u64) (js_devdata->ctx_timeslice_ns / 1000u));
  182. mutex_unlock(&js_devdata->queue_mutex);
  183. return 0;
  184. }
  185. void kbasep_js_policy_term_ctx(union kbasep_js_policy *js_policy, struct kbase_context *kctx)
  186. {
  187. struct kbasep_js_policy_cfs_ctx *ctx_info;
  188. struct kbasep_js_policy_cfs *policy_info;
  189. struct kbase_device *kbdev;
  190. KBASE_DEBUG_ASSERT(js_policy != NULL);
  191. KBASE_DEBUG_ASSERT(kctx != NULL);
  192. policy_info = &js_policy->cfs;
  193. ctx_info = &kctx->jctx.sched_info.runpool.policy_ctx.cfs;
  194. kbdev = container_of(js_policy, struct kbase_device, js_data.policy);
  195. KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_POLICY_TERM_CTX, kctx, NULL, 0u, kbasep_js_policy_trace_get_refcnt(kbdev, kctx));
  196. /* No work to do */
  197. }
  198. /*
  199. * Job Chain Management
  200. */
  201. void kbasep_js_policy_log_job_result(union kbasep_js_policy *js_policy, struct kbase_jd_atom *katom, u64 time_spent_us)
  202. {
  203. struct kbasep_js_policy_cfs_ctx *ctx_info;
  204. struct kbase_context *parent_ctx;
  205. KBASE_DEBUG_ASSERT(js_policy != NULL);
  206. KBASE_DEBUG_ASSERT(katom != NULL);
  207. CSTD_UNUSED(js_policy);
  208. parent_ctx = katom->kctx;
  209. KBASE_DEBUG_ASSERT(parent_ctx != NULL);
  210. ctx_info = &parent_ctx->jctx.sched_info.runpool.policy_ctx.cfs;
  211. ctx_info->runtime_us += priority_weight(ctx_info, time_spent_us);
  212. katom->time_spent_us += time_spent_us;
  213. }
  214. bool kbasep_js_policy_ctx_has_priority(union kbasep_js_policy *js_policy, struct kbase_context *current_ctx, struct kbase_context *new_ctx)
  215. {
  216. struct kbasep_js_policy_cfs_ctx *current_ctx_info;
  217. struct kbasep_js_policy_cfs_ctx *new_ctx_info;
  218. KBASE_DEBUG_ASSERT(current_ctx != NULL);
  219. KBASE_DEBUG_ASSERT(new_ctx != NULL);
  220. CSTD_UNUSED(js_policy);
  221. current_ctx_info = &current_ctx->jctx.sched_info.runpool.policy_ctx.cfs;
  222. new_ctx_info = &new_ctx->jctx.sched_info.runpool.policy_ctx.cfs;
  223. if (!current_ctx_info->process_rt_policy && new_ctx_info->process_rt_policy)
  224. return true;
  225. if (current_ctx_info->process_rt_policy ==
  226. new_ctx_info->process_rt_policy)
  227. return true;
  228. return false;
  229. }