3 * (C) COPYRIGHT 2012-2015 ARM Limited. All rights reserved.
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
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.
19 #include <mali_kbase.h>
20 #include <mali_kbase_config.h>
23 * Private functions follow
27 * @brief Check whether a ctx has a certain attribute, and if so, retain that
28 * attribute on the runpool.
32 * - runpool_irq spinlock
33 * - ctx is scheduled on the runpool
35 * @return true indicates a change in ctx attributes state of the runpool.
36 * In this state, the scheduler might be able to submit more jobs than
37 * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
38 * or similar is called sometime later.
39 * @return false indicates no change in ctx attributes state of the runpool.
41 static bool kbasep_js_ctx_attr_runpool_retain_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
43 struct kbasep_js_device_data *js_devdata;
44 struct kbasep_js_kctx_info *js_kctx_info;
45 bool runpool_state_changed = false;
47 KBASE_DEBUG_ASSERT(kbdev != NULL);
48 KBASE_DEBUG_ASSERT(kctx != NULL);
49 KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
50 js_devdata = &kbdev->js_data;
51 js_kctx_info = &kctx->jctx.sched_info;
53 lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
54 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
56 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != false);
58 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != false) {
59 KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] < S8_MAX);
60 ++(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
62 if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 1) {
63 /* First refcount indicates a state change */
64 runpool_state_changed = true;
65 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_RUNPOOL, kctx, NULL, 0u, attribute);
69 return runpool_state_changed;
73 * @brief Check whether a ctx has a certain attribute, and if so, release that
74 * attribute on the runpool.
78 * - runpool_irq spinlock
79 * - ctx is scheduled on the runpool
81 * @return true indicates a change in ctx attributes state of the runpool.
82 * In this state, the scheduler might be able to submit more jobs than
83 * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
84 * or similar is called sometime later.
85 * @return false indicates no change in ctx attributes state of the runpool.
87 static bool kbasep_js_ctx_attr_runpool_release_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
89 struct kbasep_js_device_data *js_devdata;
90 struct kbasep_js_kctx_info *js_kctx_info;
91 bool runpool_state_changed = false;
93 KBASE_DEBUG_ASSERT(kbdev != NULL);
94 KBASE_DEBUG_ASSERT(kctx != NULL);
95 KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
96 js_devdata = &kbdev->js_data;
97 js_kctx_info = &kctx->jctx.sched_info;
99 lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
100 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
101 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != false);
103 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != false) {
104 KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] > 0);
105 --(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
107 if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 0) {
108 /* Last de-refcount indicates a state change */
109 runpool_state_changed = true;
110 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_RUNPOOL, kctx, NULL, 0u, attribute);
114 return runpool_state_changed;
118 * @brief Retain a certain attribute on a ctx, also retaining it on the runpool
119 * if the context is scheduled.
123 * - If the context is scheduled, then runpool_irq spinlock must also be held
125 * @return true indicates a change in ctx attributes state of the runpool.
126 * This may allow the scheduler to submit more jobs than previously.
127 * @return false indicates no change in ctx attributes state of the runpool.
129 static bool kbasep_js_ctx_attr_ctx_retain_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
131 struct kbasep_js_kctx_info *js_kctx_info;
132 bool runpool_state_changed = false;
134 KBASE_DEBUG_ASSERT(kbdev != NULL);
135 KBASE_DEBUG_ASSERT(kctx != NULL);
136 KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
137 js_kctx_info = &kctx->jctx.sched_info;
139 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
140 lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
141 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] < U32_MAX);
143 ++(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
145 if (js_kctx_info->ctx.is_scheduled != false && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
146 /* Only ref-count the attribute on the runpool for the first time this contexts sees this attribute */
147 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_CTX, kctx, NULL, 0u, attribute);
148 runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, attribute);
151 return runpool_state_changed;
155 * @brief Release a certain attribute on a ctx, also releasing it from the runpool
156 * if the context is scheduled.
160 * - If the context is scheduled, then runpool_irq spinlock must also be held
162 * @return true indicates a change in ctx attributes state of the runpool.
163 * This may allow the scheduler to submit more jobs than previously.
164 * @return false indicates no change in ctx attributes state of the runpool.
166 static bool kbasep_js_ctx_attr_ctx_release_attr(struct kbase_device *kbdev, struct kbase_context *kctx, enum kbasep_js_ctx_attr attribute)
168 struct kbasep_js_kctx_info *js_kctx_info;
169 bool runpool_state_changed = false;
171 KBASE_DEBUG_ASSERT(kbdev != NULL);
172 KBASE_DEBUG_ASSERT(kctx != NULL);
173 KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
174 js_kctx_info = &kctx->jctx.sched_info;
176 lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
177 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] > 0);
179 if (js_kctx_info->ctx.is_scheduled != false && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
180 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
181 /* Only de-ref-count the attribute on the runpool when this is the last ctx-reference to it */
182 runpool_state_changed = kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, attribute);
183 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_CTX, kctx, NULL, 0u, attribute);
186 /* De-ref must happen afterwards, because kbasep_js_ctx_attr_runpool_release() needs to check it too */
187 --(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
189 return runpool_state_changed;
193 * More commonly used public functions
196 void kbasep_js_ctx_attr_set_initial_attrs(struct kbase_device *kbdev, struct kbase_context *kctx)
198 struct kbasep_js_kctx_info *js_kctx_info;
199 bool runpool_state_changed = false;
201 KBASE_DEBUG_ASSERT(kbdev != NULL);
202 KBASE_DEBUG_ASSERT(kctx != NULL);
203 js_kctx_info = &kctx->jctx.sched_info;
205 if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) != false) {
206 /* This context never submits, so don't track any scheduling attributes */
210 /* Transfer attributes held in the context flags for contexts that have submit enabled */
212 /* ... More attributes can be added here ... */
214 /* The context should not have been scheduled yet, so ASSERT if this caused
215 * runpool state changes (note that other threads *can't* affect the value
216 * of runpool_state_changed, due to how it's calculated) */
217 KBASE_DEBUG_ASSERT(runpool_state_changed == false);
218 CSTD_UNUSED(runpool_state_changed);
221 void kbasep_js_ctx_attr_runpool_retain_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
223 bool runpool_state_changed;
226 /* Retain any existing attributes */
227 for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
228 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (enum kbasep_js_ctx_attr) i) != false) {
229 /* The context is being scheduled in, so update the runpool with the new attributes */
230 runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, (enum kbasep_js_ctx_attr) i);
232 /* We don't need to know about state changed, because retaining a
233 * context occurs on scheduling it, and that itself will also try
234 * to run new atoms */
235 CSTD_UNUSED(runpool_state_changed);
240 bool kbasep_js_ctx_attr_runpool_release_ctx(struct kbase_device *kbdev, struct kbase_context *kctx)
242 bool runpool_state_changed = false;
245 /* Release any existing attributes */
246 for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
247 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (enum kbasep_js_ctx_attr) i) != false) {
248 /* The context is being scheduled out, so update the runpool on the removed attributes */
249 runpool_state_changed |= kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, (enum kbasep_js_ctx_attr) i);
253 return runpool_state_changed;
256 void kbasep_js_ctx_attr_ctx_retain_atom(struct kbase_device *kbdev, struct kbase_context *kctx, struct kbase_jd_atom *katom)
258 bool runpool_state_changed = false;
259 base_jd_core_req core_req;
261 KBASE_DEBUG_ASSERT(katom);
262 core_req = katom->core_req;
264 if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
265 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
267 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
269 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) {
270 /* Atom that can run on slot1 or slot2, and can use all cores */
271 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
274 /* We don't need to know about state changed, because retaining an
275 * atom occurs on adding it, and that itself will also try to run
277 CSTD_UNUSED(runpool_state_changed);
280 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)
282 bool runpool_state_changed = false;
283 base_jd_core_req core_req;
285 KBASE_DEBUG_ASSERT(katom_retained_state);
286 core_req = katom_retained_state->core_req;
288 /* No-op for invalid atoms */
289 if (kbasep_js_atom_retained_state_is_valid(katom_retained_state) == false)
292 if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
293 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
295 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
297 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) {
298 /* Atom that can run on slot1 or slot2, and can use all cores */
299 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
302 return runpool_state_changed;