rk: ion: resolve build err
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / t6xx / kbase / src / common / mali_kbase_js_ctx_attr.c
1 /*
2  *
3  * (C) COPYRIGHT 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
17
18
19 #include <kbase/src/common/mali_kbase.h>
20
21 /*
22  * Private functions follow
23  */
24
25 /**
26  * @brief Check whether a ctx has a certain attribute, and if so, retain that
27  * attribute on the runpool.
28  *
29  * Requires:
30  * - jsctx mutex
31  * - runpool_irq spinlock
32  * - ctx is scheduled on the runpool
33  *
34  * @return MALI_TRUE indicates a change in ctx attributes state of the runpool.
35  * In this state, the scheduler might be able to submit more jobs than
36  * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
37  * or similar is called sometime later.
38  * @return MALI_FALSE indicates no change in ctx attributes state of the runpool.
39  */
40 STATIC mali_bool kbasep_js_ctx_attr_runpool_retain_attr(kbase_device *kbdev, kbase_context *kctx, kbasep_js_ctx_attr attribute)
41 {
42         kbasep_js_device_data *js_devdata;
43         kbasep_js_kctx_info *js_kctx_info;
44         mali_bool runpool_state_changed = MALI_FALSE;
45
46         KBASE_DEBUG_ASSERT(kbdev != NULL);
47         KBASE_DEBUG_ASSERT(kctx != NULL);
48         KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
49         js_devdata = &kbdev->js_data;
50         js_kctx_info = &kctx->jctx.sched_info;
51
52         BUG_ON(!mutex_is_locked(&js_kctx_info->ctx.jsctx_mutex));
53         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
54
55         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != MALI_FALSE);
56
57         if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != MALI_FALSE) {
58                 KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] < S8_MAX);
59                 ++(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
60
61                 if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 1) {
62                         /* First refcount indicates a state change */
63                         runpool_state_changed = MALI_TRUE;
64                         KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_RUNPOOL, kctx, NULL, 0u, attribute);
65                 }
66         }
67
68         return runpool_state_changed;
69 }
70
71 /**
72  * @brief Check whether a ctx has a certain attribute, and if so, release that
73  * attribute on the runpool.
74  *
75  * Requires:
76  * - jsctx mutex
77  * - runpool_irq spinlock
78  * - ctx is scheduled on the runpool
79  *
80  * @return MALI_TRUE indicates a change in ctx attributes state of the runpool.
81  * In this state, the scheduler might be able to submit more jobs than
82  * previously, and so the caller should ensure kbasep_js_try_run_next_job_nolock()
83  * or similar is called sometime later.
84  * @return MALI_FALSE indicates no change in ctx attributes state of the runpool.
85  */
86 STATIC mali_bool kbasep_js_ctx_attr_runpool_release_attr(kbase_device *kbdev, kbase_context *kctx, kbasep_js_ctx_attr attribute)
87 {
88         kbasep_js_device_data *js_devdata;
89         kbasep_js_kctx_info *js_kctx_info;
90         mali_bool runpool_state_changed = MALI_FALSE;
91
92         KBASE_DEBUG_ASSERT(kbdev != NULL);
93         KBASE_DEBUG_ASSERT(kctx != NULL);
94         KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
95         js_devdata = &kbdev->js_data;
96         js_kctx_info = &kctx->jctx.sched_info;
97
98         BUG_ON(!mutex_is_locked(&js_kctx_info->ctx.jsctx_mutex));
99         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
100         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled != MALI_FALSE);
101
102         if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, attribute) != MALI_FALSE) {
103                 KBASE_DEBUG_ASSERT(js_devdata->runpool_irq.ctx_attr_ref_count[attribute] > 0);
104                 --(js_devdata->runpool_irq.ctx_attr_ref_count[attribute]);
105
106                 if (js_devdata->runpool_irq.ctx_attr_ref_count[attribute] == 0) {
107                         /* Last de-refcount indicates a state change */
108                         runpool_state_changed = MALI_TRUE;
109                         KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_RUNPOOL, kctx, NULL, 0u, attribute);
110                 }
111         }
112
113         return runpool_state_changed;
114 }
115
116 /**
117  * @brief Retain a certain attribute on a ctx, also retaining it on the runpool
118  * if the context is scheduled.
119  *
120  * Requires:
121  * - jsctx mutex
122  * - If the context is scheduled, then runpool_irq spinlock must also be held
123  *
124  * @return MALI_TRUE indicates a change in ctx attributes state of the runpool.
125  * This may allow the scheduler to submit more jobs than previously.
126  * @return MALI_FALSE indicates no change in ctx attributes state of the runpool.
127  */
128 STATIC mali_bool kbasep_js_ctx_attr_ctx_retain_attr(kbase_device *kbdev, kbase_context *kctx, kbasep_js_ctx_attr attribute)
129 {
130         kbasep_js_kctx_info *js_kctx_info;
131         mali_bool runpool_state_changed = MALI_FALSE;
132
133         KBASE_DEBUG_ASSERT(kbdev != NULL);
134         KBASE_DEBUG_ASSERT(kctx != NULL);
135         KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
136         js_kctx_info = &kctx->jctx.sched_info;
137
138         BUG_ON(!mutex_is_locked(&js_kctx_info->ctx.jsctx_mutex));
139         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] < U32_MAX);
140
141         ++(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
142
143         if (js_kctx_info->ctx.is_scheduled != MALI_FALSE && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
144                 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
145                 /* Only ref-count the attribute on the runpool for the first time this contexts sees this attribute */
146                 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_ON_CTX, kctx, NULL, 0u, attribute);
147                 runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, attribute);
148         }
149
150         return runpool_state_changed;
151 }
152
153 /**
154  * @brief Release a certain attribute on a ctx, also releasign it from the runpool
155  * if the context is scheduled.
156  *
157  * Requires:
158  * - jsctx mutex
159  * - If the context is scheduled, then runpool_irq spinlock must also be held
160  *
161  * @return MALI_TRUE indicates a change in ctx attributes state of the runpool.
162  * This may allow the scheduler to submit more jobs than previously.
163  * @return MALI_FALSE indicates no change in ctx attributes state of the runpool.
164  */
165 STATIC mali_bool kbasep_js_ctx_attr_ctx_release_attr(kbase_device *kbdev, kbase_context *kctx, kbasep_js_ctx_attr attribute)
166 {
167         kbasep_js_kctx_info *js_kctx_info;
168         mali_bool runpool_state_changed = MALI_FALSE;
169
170         KBASE_DEBUG_ASSERT(kbdev != NULL);
171         KBASE_DEBUG_ASSERT(kctx != NULL);
172         KBASE_DEBUG_ASSERT(attribute < KBASEP_JS_CTX_ATTR_COUNT);
173         js_kctx_info = &kctx->jctx.sched_info;
174
175         BUG_ON(!mutex_is_locked(&js_kctx_info->ctx.jsctx_mutex));
176         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.ctx_attr_ref_count[attribute] > 0);
177
178         if (js_kctx_info->ctx.is_scheduled != MALI_FALSE && js_kctx_info->ctx.ctx_attr_ref_count[attribute] == 1) {
179                 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
180                 /* Only de-ref-count the attribute on the runpool when this is the last ctx-reference to it */
181                 runpool_state_changed = kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, attribute);
182                 KBASE_TRACE_ADD(kbdev, JS_CTX_ATTR_NOW_OFF_CTX, kctx, NULL, 0u, attribute);
183         }
184
185         /* De-ref must happen afterwards, because kbasep_js_ctx_attr_runpool_release() needs to check it too */
186         --(js_kctx_info->ctx.ctx_attr_ref_count[attribute]);
187
188         return runpool_state_changed;
189 }
190
191 /*
192  * More commonly used public functions
193  */
194
195 void kbasep_js_ctx_attr_set_initial_attrs(kbase_device *kbdev, kbase_context *kctx)
196 {
197         kbasep_js_kctx_info *js_kctx_info;
198         mali_bool runpool_state_changed = MALI_FALSE;
199
200         KBASE_DEBUG_ASSERT(kbdev != NULL);
201         KBASE_DEBUG_ASSERT(kctx != NULL);
202         js_kctx_info = &kctx->jctx.sched_info;
203
204         if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) != MALI_FALSE) {
205                 /* This context never submits, so don't track any scheduling attributes */
206                 return;
207         }
208
209         /* Transfer attributes held in the context flags for contexts that have submit enabled */
210
211         if ((js_kctx_info->ctx.flags & KBASE_CTX_FLAG_HINT_ONLY_COMPUTE) != MALI_FALSE) {
212                 /* Compute context */
213                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
214         }
215         /* NOTE: Whether this is a non-compute context depends on the jobs being
216          * run, e.g. it might be submitting jobs with BASE_JD_REQ_ONLY_COMPUTE */
217
218         /* ... More attributes can be added here ... */
219
220         /* The context should not have been scheduled yet, so ASSERT if this caused
221          * runpool state changes (note that other threads *can't* affect the value
222          * of runpool_state_changed, due to how it's calculated) */
223         KBASE_DEBUG_ASSERT(runpool_state_changed == MALI_FALSE);
224         CSTD_UNUSED(runpool_state_changed);
225 }
226
227 void kbasep_js_ctx_attr_runpool_retain_ctx(kbase_device *kbdev, kbase_context *kctx)
228 {
229         mali_bool runpool_state_changed;
230         int i;
231
232         /* Retain any existing attributes */
233         for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
234                 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (kbasep_js_ctx_attr) i) != MALI_FALSE) {
235                         /* The context is being scheduled in, so update the runpool with the new attributes */
236                         runpool_state_changed = kbasep_js_ctx_attr_runpool_retain_attr(kbdev, kctx, (kbasep_js_ctx_attr) i);
237
238                         /* We don't need to know about state changed, because retaining a
239                          * context occurs on scheduling it, and that itself will also try
240                          * to run new atoms */
241                         CSTD_UNUSED(runpool_state_changed);
242                 }
243         }
244 }
245
246 mali_bool kbasep_js_ctx_attr_runpool_release_ctx(kbase_device *kbdev, kbase_context *kctx)
247 {
248         mali_bool runpool_state_changed = MALI_FALSE;
249         int i;
250
251         /* Release any existing attributes */
252         for (i = 0; i < KBASEP_JS_CTX_ATTR_COUNT; ++i) {
253                 if (kbasep_js_ctx_attr_is_attr_on_ctx(kctx, (kbasep_js_ctx_attr) i) != MALI_FALSE) {
254                         /* The context is being scheduled out, so update the runpool on the removed attributes */
255                         runpool_state_changed |= kbasep_js_ctx_attr_runpool_release_attr(kbdev, kctx, (kbasep_js_ctx_attr) i);
256                 }
257         }
258
259         return runpool_state_changed;
260 }
261
262 void kbasep_js_ctx_attr_ctx_retain_atom(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *katom)
263 {
264         mali_bool runpool_state_changed = MALI_FALSE;
265         base_jd_core_req core_req;
266
267         KBASE_DEBUG_ASSERT(katom);
268         core_req = katom->core_req;
269
270         if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
271                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
272         else
273                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
274
275         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) {
276                 /* Atom that can run on slot1 or slot2, and can use all cores */
277                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_retain_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
278         }
279
280         /* We don't need to know about state changed, because retaining an
281          * atom occurs on adding it, and that itself will also try to run
282          * new atoms */
283         CSTD_UNUSED(runpool_state_changed);
284 }
285
286 mali_bool kbasep_js_ctx_attr_ctx_release_atom(kbase_device *kbdev, kbase_context *kctx, kbasep_js_atom_retained_state *katom_retained_state)
287 {
288         mali_bool runpool_state_changed = MALI_FALSE;
289         base_jd_core_req core_req;
290
291         KBASE_DEBUG_ASSERT(katom_retained_state);
292         core_req = katom_retained_state->core_req;
293
294         /* No-op for invalid atoms */
295         if (kbasep_js_atom_retained_state_is_valid(katom_retained_state) == MALI_FALSE)
296                 return MALI_FALSE;
297
298         if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
299                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE);
300         else
301                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_NON_COMPUTE);
302
303         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) {
304                 /* Atom that can run on slot1 or slot2, and can use all cores */
305                 runpool_state_changed |= kbasep_js_ctx_attr_ctx_release_attr(kbdev, kctx, KBASEP_JS_CTX_ATTR_COMPUTE_ALL_CORES);
306         }
307
308         return runpool_state_changed;
309 }