5ac4a7fcedd8c23405218945274e0a55a2bc875d
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / t6xx / kbase / src / common / mali_kbase_js.h
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
20 /**
21  * @file mali_kbase_js.h
22  * Job Scheduler APIs.
23  */
24
25 #ifndef _KBASE_JS_H_
26 #define _KBASE_JS_H_
27
28 #include <malisw/mali_malisw.h>
29
30 #include "mali_kbase_js_defs.h"
31 #include "mali_kbase_js_policy.h"
32 #include "mali_kbase_defs.h"
33
34 #include "mali_kbase_js_ctx_attr.h"
35
36 /**
37  * @addtogroup base_api
38  * @{
39  */
40
41 /**
42  * @addtogroup base_kbase_api
43  * @{
44  */
45
46 /**
47  * @addtogroup kbase_js Job Scheduler Internal APIs
48  * @{
49  *
50  * These APIs are Internal to KBase and are available for use by the
51  * @ref kbase_js_policy "Job Scheduler Policy APIs"
52  */
53
54 /**
55  * @brief Initialize the Job Scheduler
56  *
57  * The kbasep_js_device_data sub-structure of \a kbdev must be zero
58  * initialized before passing to the kbasep_js_devdata_init() function. This is
59  * to give efficient error path code.
60  */
61 mali_error kbasep_js_devdata_init(kbase_device * const kbdev);
62
63 /**
64  * @brief Halt the Job Scheduler.
65  *
66  * It is safe to call this on \a kbdev even if it the kbasep_js_device_data
67  * sub-structure was never initialized/failed initialization, to give efficient
68  * error-path code.
69  *
70  * For this to work, the kbasep_js_device_data sub-structure of \a kbdev must
71  * be zero initialized before passing to the kbasep_js_devdata_init()
72  * function. This is to give efficient error path code.
73  *
74  * It is a Programming Error to call this whilst there are still kbase_context
75  * structures registered with this scheduler.
76  *
77  */
78 void kbasep_js_devdata_halt(kbase_device *kbdev);
79
80 /**
81  * @brief Terminate the Job Scheduler
82  *
83  * It is safe to call this on \a kbdev even if it the kbasep_js_device_data
84  * sub-structure was never initialized/failed initialization, to give efficient
85  * error-path code.
86  *
87  * For this to work, the kbasep_js_device_data sub-structure of \a kbdev must
88  * be zero initialized before passing to the kbasep_js_devdata_init()
89  * function. This is to give efficient error path code.
90  *
91  * It is a Programming Error to call this whilst there are still kbase_context
92  * structures registered with this scheduler.
93  */
94 void kbasep_js_devdata_term(kbase_device *kbdev);
95
96 /**
97  * @brief Initialize the Scheduling Component of a kbase_context on the Job Scheduler.
98  *
99  * This effectively registers a kbase_context with a Job Scheduler.
100  *
101  * It does not register any jobs owned by the kbase_context with the scheduler.
102  * Those must be separately registered by kbasep_js_add_job().
103  *
104  * The kbase_context must be zero intitialized before passing to the
105  * kbase_js_init() function. This is to give efficient error path code.
106  */
107 mali_error kbasep_js_kctx_init(kbase_context * const kctx);
108
109 /**
110  * @brief Terminate the Scheduling Component of a kbase_context on the Job Scheduler
111  *
112  * This effectively de-registers a kbase_context from its Job Scheduler
113  *
114  * It is safe to call this on a kbase_context that has never had or failed
115  * initialization of its jctx.sched_info member, to give efficient error-path
116  * code.
117  *
118  * For this to work, the kbase_context must be zero intitialized before passing
119  * to the kbase_js_init() function.
120  *
121  * It is a Programming Error to call this whilst there are still jobs
122  * registered with this context.
123  */
124 void kbasep_js_kctx_term(kbase_context *kctx);
125
126 /**
127  * @brief Add a job chain to the Job Scheduler, and take necessary actions to
128  * schedule the context/run the job.
129  *
130  * This atomically does the following:
131  * - Update the numbers of jobs information
132  * - Add the job to the run pool if necessary (part of init_job)
133  *
134  * Once this is done, then an appropriate action is taken:
135  * - If the ctx is scheduled, it attempts to start the next job (which might be
136  * this added job)
137  * - Otherwise, and if this is the first job on the context, it enqueues it on
138  * the Policy Queue
139  *
140  * The Policy's Queue can be updated by this in the following ways:
141  * - In the above case that this is the first job on the context
142  * - If the job is high priority and the context is not scheduled, then it
143  * could cause the Policy to schedule out a low-priority context, allowing
144  * this context to be scheduled in.
145  *
146  * If the context is already scheduled on the RunPool, then adding a job to it
147  * is guarenteed not to update the Policy Queue. And so, the caller is
148  * guarenteed to not need to try scheduling a context from the Run Pool - it
149  * can safely assert that the result is MALI_FALSE.
150  *
151  * It is a programming error to have more than U32_MAX jobs in flight at a time.
152  *
153  * The following locking conditions are made on the caller:
154  * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
155  * - it must \em not hold kbasep_js_device_data::runpool_irq::lock (as this will be
156  * obtained internally)
157  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
158  * obtained internally)
159  * - it must \em not hold kbasep_jd_device_data::queue_mutex (again, it's used internally).
160  *
161  * @return MALI_TRUE indicates that the Policy Queue was updated, and so the
162  * caller will need to try scheduling a context onto the Run Pool.
163  * @return MALI_FALSE indicates that no updates were made to the Policy Queue,
164  * so no further action is required from the caller. This is \b always returned
165  * when the context is currently scheduled.
166  */
167 mali_bool kbasep_js_add_job(kbase_context *kctx, kbase_jd_atom *atom);
168
169 /**
170  * @brief Remove a job chain from the Job Scheduler, except for its 'retained state'.
171  *
172  * Completely removing a job requires several calls:
173  * - kbasep_js_copy_atom_retained_state(), to capture the 'retained state' of
174  *   the atom
175  * - kbasep_js_remove_job(), to partially remove the atom from the Job Scheduler
176  * - kbasep_js_runpool_release_ctx_and_katom_retained_state(), to release the
177  *   remaining state held as part of the job having been run.
178  *
179  * In the common case of atoms completing normally, this set of actions is more optimal for spinlock purposes than having kbasep_js_remove_job() handle all of the actions.
180  *
181  * In the case of cancelling atoms, it is easier to call kbasep_js_remove_cancelled_job(), which handles all the necessary actions.
182  *
183  * It is a programming error to call this when:
184  * - \a atom is not a job belonging to kctx.
185  * - \a atom has already been removed from the Job Scheduler.
186  * - \a atom is still in the runpool:
187  *  - it has not been removed with kbasep_js_policy_dequeue_job()
188  *  - or, it has not been removed with kbasep_js_policy_dequeue_job_irq()
189  *
190  * Do not use this for removing jobs being killed by kbase_jd_cancel() - use
191  * kbasep_js_remove_cancelled_job() instead.
192  *
193  * The following locking conditions are made on the caller:
194  * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
195  *
196  */
197 void kbasep_js_remove_job(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *atom);
198
199 /**
200  * @brief Completely remove a job chain from the Job Scheduler, in the case
201  * where the job chain was cancelled.
202  *
203  * This is a variant of kbasep_js_remove_job() that takes care of removing all
204  * of the retained state too. This is generally useful for cancelled atoms,
205  * which need not be handled in an optimal way.
206  *
207  * It is a programming error to call this when:
208  * - \a atom is not a job belonging to kctx.
209  * - \a atom has already been removed from the Job Scheduler.
210  * - \a atom is still in the runpool:
211  *  - it is not being killed with kbasep_jd_cancel()
212  *  - or, it has not been removed with kbasep_js_policy_dequeue_job()
213  *  - or, it has not been removed with kbasep_js_policy_dequeue_job_irq()
214  *
215  * The following locking conditions are made on the caller:
216  * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
217  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, (as this will be
218  * obtained internally)
219  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this could be
220  * obtained internally)
221  */
222 void kbasep_js_remove_cancelled_job(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *katom);
223
224 /**
225  * @brief Refcount a context as being busy, preventing it from being scheduled
226  * out.
227  *
228  * @note This function can safely be called from IRQ context.
229  *
230  * The following locking conditions are made on the caller:
231  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
232  * it will be used internally.
233  *
234  * @return value != MALI_FALSE if the retain succeeded, and the context will not be scheduled out.
235  * @return MALI_FALSE if the retain failed (because the context is being/has been scheduled out).
236  */
237 mali_bool kbasep_js_runpool_retain_ctx(kbase_device *kbdev, kbase_context *kctx);
238
239 /**
240  * @brief Refcount a context as being busy, preventing it from being scheduled
241  * out.
242  *
243  * @note This function can safely be called from IRQ context.
244  *
245  * The following locks must be held by the caller:
246  * - kbasep_js_device_data::runpool_irq::lock
247  *
248  * @return value != MALI_FALSE if the retain succeeded, and the context will not be scheduled out.
249  * @return MALI_FALSE if the retain failed (because the context is being/has been scheduled out).
250  */
251 mali_bool kbasep_js_runpool_retain_ctx_nolock(kbase_device *kbdev, kbase_context *kctx);
252
253 /**
254  * @brief Lookup a context in the Run Pool based upon its current address space
255  * and ensure that is stays scheduled in.
256  *
257  * The context is refcounted as being busy to prevent it from scheduling
258  * out. It must be released with kbasep_js_runpool_release_ctx() when it is no
259  * longer required to stay scheduled in.
260  *
261  * @note This function can safely be called from IRQ context.
262  *
263  * The following locking conditions are made on the caller:
264  * - it must \em not hold the kbasep_js_device_data::runpoool_irq::lock, because
265  * it will be used internally.
266  *
267  * @return a valid kbase_context on success, which has been refcounted as being busy.
268  * @return NULL on failure, indicating that no context was found in \a as_nr
269  */
270 kbase_context *kbasep_js_runpool_lookup_ctx(kbase_device *kbdev, int as_nr);
271
272 /**
273  * @brief Handling the requeuing/killing of a context that was evicted from the
274  * policy queue or runpool.
275  *
276  * This should be used whenever handing off a context that has been evicted
277  * from the policy queue or the runpool:
278  * - If the context is not dying and has jobs, it gets re-added to the policy
279  * queue
280  * - Otherwise, it is not added
281  *
282  * In addition, if the context is dying the jobs are killed asynchronously.
283  *
284  * In all cases, the Power Manager active reference is released
285  * (kbase_pm_context_idle()) whenever the has_pm_ref parameter is true.  \a
286  * has_pm_ref must be set to false whenever the context was not previously in
287  * the runpool and does not hold a Power Manager active refcount. Note that
288  * contexts in a rollback of kbasep_js_try_schedule_head_ctx() might have an
289  * active refcount even though they weren't in the runpool.
290  *
291  * The following locking conditions are made on the caller:
292  * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
293  * - it must \em not hold kbasep_jd_device_data::queue_mutex (as this will be
294  * obtained internally)
295  */
296 void kbasep_js_runpool_requeue_or_kill_ctx(kbase_device *kbdev, kbase_context *kctx, mali_bool has_pm_ref);
297
298 /**
299  * @brief Release a refcount of a context being busy, allowing it to be
300  * scheduled out.
301  *
302  * When the refcount reaches zero and the context \em might be scheduled out
303  * (depending on whether the Scheudling Policy has deemed it so, or if it has run
304  * out of jobs).
305  *
306  * If the context does get scheduled out, then The following actions will be
307  * taken as part of deschduling a context:
308  * - For the context being descheduled:
309  *  - If the context is in the processing of dying (all the jobs are being
310  * removed from it), then descheduling also kills off any jobs remaining in the
311  * context.
312  *  - If the context is not dying, and any jobs remain after descheduling the
313  * context then it is re-enqueued to the Policy's Queue.
314  *  - Otherwise, the context is still known to the scheduler, but remains absent
315  * from the Policy Queue until a job is next added to it.
316  *  - In all descheduling cases, the Power Manager active reference (obtained
317  * during kbasep_js_try_schedule_head_ctx()) is released (kbase_pm_context_idle()).
318  *
319  * Whilst the context is being descheduled, this also handles actions that
320  * cause more atoms to be run:
321  * - Attempt submitting atoms when the Context Attributes on the Runpool have
322  * changed. This is because the context being scheduled out could mean that
323  * there are more opportunities to run atoms.
324  * - Attempt submitting to a slot that was previously blocked due to affinity
325  * restrictions. This is usually only necessary when releasing a context
326  * happens as part of completing a previous job, but is harmless nonetheless.
327  * - Attempt scheduling in a new context (if one is available), and if necessary,
328  * running a job from that new context.
329  *
330  * Unlike retaining a context in the runpool, this function \b cannot be called
331  * from IRQ context.
332  *
333  * It is a programming error to call this on a \a kctx that is not currently
334  * scheduled, or that already has a zero refcount.
335  *
336  * The following locking conditions are made on the caller:
337  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
338  * it will be used internally.
339  * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
340  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
341  * obtained internally)
342  * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
343  * - it must \em not hold kbasep_jd_device_data::queue_mutex (as this will be
344  * obtained internally)
345  *
346  */
347 void kbasep_js_runpool_release_ctx(kbase_device *kbdev, kbase_context *kctx);
348
349 /**
350  * @brief Variant of kbasep_js_runpool_release_ctx() that handles additional
351  * actions from completing an atom.
352  *
353  * This is usually called as part of completing an atom and releasing the
354  * refcount on the context held by the atom.
355  *
356  * Therefore, the extra actions carried out are part of handling actions queued
357  * on a completed atom, namely:
358  * - Releasing the atom's context attributes
359  * - Retrying the submission on a particular slot, because we couldn't submit
360  * on that slot from an IRQ handler.
361  *
362  * The locking conditions of this function are the same as those for
363  * kbasep_js_runpool_release_ctx()
364  */
365 void kbasep_js_runpool_release_ctx_and_katom_retained_state(kbase_device *kbdev, kbase_context *kctx, kbasep_js_atom_retained_state *katom_retained_state);
366
367 /**
368  * @brief Try to submit the next job on a \b particular slot whilst in IRQ
369  * context, and whilst the caller already holds the runpool IRQ spinlock.
370  *
371  * \a *submit_count will be checked against
372  * KBASE_JS_MAX_JOB_SUBMIT_PER_SLOT_PER_IRQ to see whether too many jobs have
373  * been submitted. This is to prevent the IRQ handler looping over lots of GPU
374  * NULL jobs, which may complete whilst the IRQ handler is still processing. \a
375  * submit_count itself should point to kbase_device::slot_submit_count_irq[ \a js ],
376  * which is initialized to zero on entry to the IRQ handler.
377  *
378  * The following locks must be held by the caller:
379  * - kbasep_js_device_data::runpool_irq::lock
380  *
381  * @return truthful (i.e. != MALI_FALSE) if too many jobs were submitted from
382  * IRQ. Therefore, this indicates that submission should be retried from a
383  * work-queue, by using
384  * kbasep_js_try_run_next_job_on_slot_nolock()/kbase_js_try_run_jobs_on_slot().
385  * @return MALI_FALSE if submission had no problems: the GPU is either already
386  * full of jobs in the HEAD and NEXT registers, or we were able to get enough
387  * jobs from the Run Pool to fill the GPU's HEAD and NEXT registers.
388  */
389 mali_bool kbasep_js_try_run_next_job_on_slot_irq_nolock(kbase_device *kbdev, int js, s8 *submit_count);
390
391 /**
392  * @brief Try to submit the next job on a particular slot, outside of IRQ context
393  *
394  * This obtains the Job Slot lock for the duration of the call only.
395  *
396  * Unlike kbasep_js_try_run_next_job_on_slot_irq_nolock(), there is no limit on
397  * submission, because eventually IRQ_THROTTLE will kick in to prevent us
398  * getting stuck in a loop of submitting GPU NULL jobs. This is because the IRQ
399  * handler will be delayed, and so this function will eventually fill up the
400  * space in our software 'submitted' slot (kbase_jm_slot::submitted).
401  *
402  * In addition, there's no return value - we'll run the maintenence functions
403  * on the Policy's Run Pool, but if there's nothing there after that, then the
404  * Run Pool is truely empty, and so no more action need be taken.
405  *
406  * The following locking conditions are made on the caller:
407  * - it must hold kbasep_js_device_data::runpool_mutex
408  * - it must hold kbasep_js_device_data::runpool_irq::lock
409  *
410  * This must only be called whilst the GPU is powered - for example, when
411  * kbdev->jsdata.nr_user_contexts_running > 0.
412  *
413  * @note The caller \em might be holding one of the
414  * kbasep_js_kctx_info::ctx::jsctx_mutex locks.
415  *
416  */
417 void kbasep_js_try_run_next_job_on_slot_nolock(kbase_device *kbdev, int js);
418
419 /**
420  * @brief Try to submit the next job for each slot in the system, outside of IRQ context
421  *
422  * This will internally call kbasep_js_try_run_next_job_on_slot_nolock(), so similar
423  * locking conditions on the caller are required.
424  *
425  * The following locking conditions are made on the caller:
426  * - it must hold kbasep_js_device_data::runpool_mutex
427  * - it must hold kbasep_js_device_data::runpool_irq::lock
428  *
429  * @note The caller \em might be holding one of the
430  * kbasep_js_kctx_info::ctx::jsctx_mutex locks.
431  *
432  */
433 void kbasep_js_try_run_next_job_nolock(kbase_device *kbdev);
434
435 /**
436  * @brief Try to schedule the next context onto the Run Pool
437  *
438  * This checks whether there's space in the Run Pool to accommodate a new
439  * context. If so, it attempts to dequeue a context from the Policy Queue, and
440  * submit this to the Run Pool.
441  *
442  * If the scheduling succeeds, then it also makes a call to
443  * kbasep_js_try_run_next_job_nolock(), in case the new context has jobs
444  * matching the job slot requirements, but no other currently scheduled context
445  * has such jobs.
446  *
447  * Whilst attempting to obtain a context from the policy queue, or add a
448  * context to the runpool, this function takes a Power Manager active
449  * reference. If for any reason a context cannot be added to the runpool, any
450  * reference obtained is released once the context is safely back in the policy
451  * queue. If no context was available on the policy queue, any reference
452  * obtained is released too.
453  *
454  * Only if the context gets placed in the runpool does the Power Manager active
455  * reference stay held (and is effectively now owned by the
456  * context/runpool). It is only released once the context is removed
457  * completely, or added back to the policy queue
458  * (e.g. kbasep_js_runpool_release_ctx(),
459  * kbasep_js_runpool_requeue_or_kill_ctx(), etc)
460  *
461  * If any of these actions fail (Run Pool Full, Policy Queue empty, can't get
462  * PM active reference due to a suspend, etc) then any actions taken are rolled
463  * back and the function just returns normally.
464  *
465  * The following locking conditions are made on the caller:
466  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
467  * it will be used internally.
468  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
469  * obtained internally)
470  * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
471  * - it must \em not hold kbasep_jd_device_data::queue_mutex (again, it's used internally).
472  * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex, because it will
473  * be used internally.
474  *
475  */
476 void kbasep_js_try_schedule_head_ctx(kbase_device *kbdev);
477
478 /**
479  * @brief Schedule in a privileged context
480  *
481  * This schedules a context in regardless of the context priority.
482  * If the runpool is full, a context will be forced out of the runpool and the function will wait
483  * for the new context to be scheduled in.
484  * The context will be kept scheduled in (and the corresponding address space reserved) until
485  * kbasep_js_release_privileged_ctx is called).
486  *
487  * The following locking conditions are made on the caller:
488  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
489  * it will be used internally.
490  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
491  * obtained internally)
492  * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
493  * - it must \em not hold kbasep_jd_device_data::queue_mutex (again, it's used internally).
494  * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex, because it will
495  * be used internally.
496  *
497  */
498 void kbasep_js_schedule_privileged_ctx(kbase_device *kbdev, kbase_context *kctx);
499
500 /**
501  * @brief Release a privileged context, allowing it to be scheduled out.
502  *
503  * See kbasep_js_runpool_release_ctx for potential side effects.
504  *
505  * The following locking conditions are made on the caller:
506  * - it must \em not hold the kbasep_js_device_data::runpool_irq::lock, because
507  * it will be used internally.
508  * - it must \em not hold kbasep_js_kctx_info::ctx::jsctx_mutex.
509  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
510  * obtained internally)
511  * - it must \em not hold the kbase_device::as[n].transaction_mutex (as this will be obtained internally)
512  *
513  */
514 void kbasep_js_release_privileged_ctx(kbase_device *kbdev, kbase_context *kctx);
515
516 /**
517  * @brief Handle the Job Scheduler component for the IRQ of a job finishing
518  *
519  * This does the following:
520  * -# Releases resources held by the atom
521  * -# if \a end_timestamp != NULL, updates the runpool's notion of time spent by a running ctx
522  * -# determines whether a context should be marked for scheduling out
523  * -# examines done_code to determine whether to submit the next job on the slot
524  * (picking from all ctxs in the runpool)
525  *
526  * In addition, if submission didn't happen (the submit-from-IRQ function
527  * failed or done_code didn't specify to start new jobs), then this sets a
528  * message on katom that submission needs to be retried from the worker thread.
529  *
530  * Normally, the time calculated from end_timestamp is rounded up to the
531  * minimum time precision. Therefore, to ensure the job is recorded as not
532  * spending any time, then set end_timestamp to NULL. For example, this is necessary when
533  * evicting jobs from JSn_HEAD_NEXT (because they didn't actually run).
534  *
535  * NOTE: It's possible to move the steps (2) and (3) (inc calculating job's time
536  * used) into the worker (outside of IRQ context), but this may allow a context
537  * to use up to twice as much timeslice as is allowed by the policy. For
538  * policies that order by time spent, this is not a problem for overall
539  * 'fairness', but can still increase latency between contexts.
540  *
541  * The following locking conditions are made on the caller:
542  * - it must hold kbasep_js_device_data::runpoool_irq::lock
543  */
544 void kbasep_js_job_done_slot_irq(kbase_jd_atom *katom, int slot_nr,
545                                  ktime_t *end_timestamp,
546                                  kbasep_js_atom_done_code done_code);
547
548 /**
549  * @brief Try to submit the next job on each slot
550  *
551  * The following locks may be used:
552  * - kbasep_js_device_data::runpool_mutex
553  * - kbasep_js_device_data::runpool_irq::lock
554  */
555 void kbase_js_try_run_jobs(kbase_device *kbdev);
556
557 /**
558  * @brief Try to submit the next job on a specfic slot
559  *
560  * The following locking conditions are made on the caller:
561  *
562  * - it must \em not hold kbasep_js_device_data::runpool_mutex (as this will be
563  * obtained internally)
564  * - it must \em not hold kbasep_js_device_data::runpool_irq::lock (as this
565  * will be obtained internally)
566  *
567  */
568 void kbase_js_try_run_jobs_on_slot(kbase_device *kbdev, int js);
569
570 /**
571  * @brief Handle releasing cores for power management and affinity management,
572  * ensuring that cores are powered down and affinity tracking is updated.
573  *
574  * This must only be called on an atom that is not currently running, and has
575  * not been re-queued onto the context (and so does not need locking)
576  *
577  * This function enters at the following @ref kbase_atom_coreref_state states:
578  * - NO_CORES_REQUESTED
579  * - WAITING_FOR_REQUESTED_CORES
580  * - RECHECK_AFFINITY
581  * - READY
582  *
583  * It transitions the above states back to NO_CORES_REQUESTED by the end of the
584  * function call (possibly via intermediate states).
585  *
586  * No locks need be held by the caller, since this takes the necessary Power
587  * Management locks itself. The runpool_irq.lock is not taken (the work that
588  * requires it is handled by kbase_js_affinity_submit_to_blocked_slots() ).
589  *
590  * @note The corresponding kbasep_js_job_check_ref_cores() is private to the
591  * Job Scheduler, and is called automatically when running the next job.
592  */
593 void kbasep_js_job_check_deref_cores(kbase_device *kbdev, struct kbase_jd_atom *katom);
594
595 /**
596  * @brief Suspend the job scheduler during a Power Management Suspend event.
597  *
598  * Causes all contexts to be removed from the runpool, and prevents any
599  * contexts from (re)entering the runpool.
600  *
601  * This does not handle suspending the one privileged context: the caller must
602  * instead do this by by suspending the GPU HW Counter Instrumentation.
603  *
604  * This will eventually cause all Power Management active references held by
605  * contexts on the runpool to be released, without running any more atoms.
606  *
607  * The caller must then wait for all Power Mangement active refcount to become
608  * zero before completing the suspend.
609  *
610  * The emptying mechanism may take some time to complete, since it can wait for
611  * jobs to complete naturally instead of forcing them to end quickly. However,
612  * this is bounded by the Job Scheduling Policy's Job Timeouts. Hence, this
613  * function is guaranteed to complete in a finite time whenever the Job
614  * Scheduling Policy implements Job Timeouts (such as those done by CFS).
615  */
616 void kbasep_js_suspend(kbase_device *kbdev);
617
618 /**
619  * @brief Resume the Job Scheduler after a Power Management Resume event.
620  *
621  * This restores the actions from kbasep_js_suspend():
622  * - Schedules contexts back into the runpool
623  * - Resumes running atoms on the GPU
624  */
625 void kbasep_js_resume(kbase_device *kbdev);
626
627
628 /*
629  * Helpers follow
630  */
631
632 /**
633  * @brief Check that a context is allowed to submit jobs on this policy
634  *
635  * The purpose of this abstraction is to hide the underlying data size, and wrap up
636  * the long repeated line of code.
637  *
638  * As with any mali_bool, never test the return value with MALI_TRUE.
639  *
640  * The caller must hold kbasep_js_device_data::runpool_irq::lock.
641  */
642 static INLINE mali_bool kbasep_js_is_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
643 {
644         u16 test_bit;
645
646         /* Ensure context really is scheduled in */
647         KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
648         KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled != MALI_FALSE);
649
650         test_bit = (u16) (1u << kctx->as_nr);
651
652         return (mali_bool) (js_devdata->runpool_irq.submit_allowed & test_bit);
653 }
654
655 /**
656  * @brief Allow a context to submit jobs on this policy
657  *
658  * The purpose of this abstraction is to hide the underlying data size, and wrap up
659  * the long repeated line of code.
660  *
661  * The caller must hold kbasep_js_device_data::runpool_irq::lock.
662  */
663 static INLINE void kbasep_js_set_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
664 {
665         u16 set_bit;
666
667         /* Ensure context really is scheduled in */
668         KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
669         KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled != MALI_FALSE);
670
671         set_bit = (u16) (1u << kctx->as_nr);
672
673         KBASE_DEBUG_PRINT_INFO(KBASE_JM, "JS: Setting Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
674
675         js_devdata->runpool_irq.submit_allowed |= set_bit;
676 }
677
678 /**
679  * @brief Prevent a context from submitting more jobs on this policy
680  *
681  * The purpose of this abstraction is to hide the underlying data size, and wrap up
682  * the long repeated line of code.
683  *
684  * The caller must hold kbasep_js_device_data::runpool_irq::lock.
685  */
686 static INLINE void kbasep_js_clear_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
687 {
688         u16 clear_bit;
689         u16 clear_mask;
690
691         /* Ensure context really is scheduled in */
692         KBASE_DEBUG_ASSERT(kctx->as_nr != KBASEP_AS_NR_INVALID);
693         KBASE_DEBUG_ASSERT(kctx->jctx.sched_info.ctx.is_scheduled != MALI_FALSE);
694
695         clear_bit = (u16) (1u << kctx->as_nr);
696         clear_mask = ~clear_bit;
697
698         KBASE_DEBUG_PRINT_INFO(KBASE_JM, "JS: Clearing Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
699
700         js_devdata->runpool_irq.submit_allowed &= clear_mask;
701 }
702
703 /**
704  * @brief Manage the 'retry_submit_on_slot' part of a kbase_jd_atom
705  */
706 static INLINE void kbasep_js_clear_job_retry_submit(kbase_jd_atom *atom)
707 {
708         atom->retry_submit_on_slot = KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID;
709 }
710
711 /**
712  * Mark a slot as requiring resubmission by carrying that information on a
713  * completing atom.
714  *
715  * @note This can ASSERT in debug builds if the submit slot has been set to
716  * something other than the current value for @a js. This is because you might
717  * be unintentionally stopping more jobs being submitted on the old submit
718  * slot, and that might cause a scheduling-hang.
719  *
720  * @note If you can guarantee that the atoms for the original slot will be
721  * submitted on some other slot, then call kbasep_js_clear_job_retry_submit()
722  * first to silence the ASSERT.
723  */
724 static INLINE void kbasep_js_set_job_retry_submit_slot(kbase_jd_atom *atom, int js)
725 {
726         KBASE_DEBUG_ASSERT(0 <= js && js <= BASE_JM_MAX_NR_SLOTS);
727         KBASE_DEBUG_ASSERT(atom->retry_submit_on_slot == KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID
728                            || atom->retry_submit_on_slot == js);
729
730         atom->retry_submit_on_slot = js;
731 }
732
733 /**
734  * Create an initial 'invalid' atom retained state, that requires no
735  * atom-related work to be done on releasing with
736  * kbasep_js_runpool_release_ctx_and_katom_retained_state()
737  */
738 static INLINE void kbasep_js_atom_retained_state_init_invalid(kbasep_js_atom_retained_state *retained_state)
739 {
740         retained_state->event_code = BASE_JD_EVENT_NOT_STARTED;
741         retained_state->core_req = KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID;
742         retained_state->retry_submit_on_slot = KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID;
743 }
744
745 /**
746  * Copy atom state that can be made available after jd_done_nolock() is called
747  * on that atom.
748  */
749 static INLINE void kbasep_js_atom_retained_state_copy(kbasep_js_atom_retained_state *retained_state, const kbase_jd_atom *katom)
750 {
751         retained_state->event_code = katom->event_code;
752         retained_state->core_req = katom->core_req;
753         retained_state->retry_submit_on_slot = katom->retry_submit_on_slot;
754 }
755
756 /**
757  * @brief Determine whether an atom has finished (given its retained state),
758  * and so should be given back to userspace/removed from the system.
759  *
760  * Reasons for an atom not finishing include:
761  * - Being soft-stopped (and so, the atom should be resubmitted sometime later)
762  *
763  * @param[in] katom_retained_state the retained state of the atom to check
764  * @return    MALI_FALSE if the atom has not finished
765  * @return    !=MALI_FALSE if the atom has finished
766  */
767 static INLINE mali_bool kbasep_js_has_atom_finished(const kbasep_js_atom_retained_state *katom_retained_state)
768 {
769         return (mali_bool) (katom_retained_state->event_code != BASE_JD_EVENT_STOPPED && katom_retained_state->event_code != BASE_JD_EVENT_REMOVED_FROM_NEXT);
770 }
771
772 /**
773  * @brief Determine whether a kbasep_js_atom_retained_state is valid
774  *
775  * An invalid kbasep_js_atom_retained_state is allowed, and indicates that the
776  * code should just ignore it.
777  *
778  * @param[in] katom_retained_state the atom's retained state to check
779  * @return    MALI_FALSE if the retained state is invalid, and can be ignored
780  * @return    !=MALI_FALSE if the retained state is valid
781  */
782 static INLINE mali_bool kbasep_js_atom_retained_state_is_valid(const kbasep_js_atom_retained_state *katom_retained_state)
783 {
784         return (mali_bool) (katom_retained_state->core_req != KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID);
785 }
786
787 static INLINE mali_bool kbasep_js_get_atom_retry_submit_slot(const kbasep_js_atom_retained_state *katom_retained_state, int *res)
788 {
789         int js = katom_retained_state->retry_submit_on_slot;
790         *res = js;
791         return (mali_bool) (js >= 0);
792 }
793
794 #if KBASE_DEBUG_DISABLE_ASSERTS == 0
795 /**
796  * Debug Check the refcount of a context. Only use within ASSERTs
797  *
798  * Obtains kbasep_js_device_data::runpool_irq::lock
799  *
800  * @return negative value if the context is not scheduled in
801  * @return current refcount of the context if it is scheduled in. The refcount
802  * is not guarenteed to be kept constant.
803  */
804 static INLINE int kbasep_js_debug_check_ctx_refcount(kbase_device *kbdev, kbase_context *kctx)
805 {
806         unsigned long flags;
807         kbasep_js_device_data *js_devdata;
808         int result = -1;
809         int as_nr;
810
811         KBASE_DEBUG_ASSERT(kbdev != NULL);
812         KBASE_DEBUG_ASSERT(kctx != NULL);
813         js_devdata = &kbdev->js_data;
814
815         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
816         as_nr = kctx->as_nr;
817         if (as_nr != KBASEP_AS_NR_INVALID)
818                 result = js_devdata->runpool_irq.per_as_data[as_nr].as_busy_refcount;
819
820         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
821
822         return result;
823 }
824 #endif                          /* KBASE_DEBUG_DISABLE_ASSERTS == 0 */
825
826 /**
827  * @brief Variant of kbasep_js_runpool_lookup_ctx() that can be used when the
828  * context is guarenteed to be already previously retained.
829  *
830  * It is a programming error to supply the \a as_nr of a context that has not
831  * been previously retained/has a busy refcount of zero. The only exception is
832  * when there is no ctx in \a as_nr (NULL returned).
833  *
834  * The following locking conditions are made on the caller:
835  * - it must \em not hold the kbasep_js_device_data::runpoool_irq::lock, because
836  * it will be used internally.
837  *
838  * @return a valid kbase_context on success, with a refcount that is guarenteed
839  * to be non-zero and unmodified by this function.
840  * @return NULL on failure, indicating that no context was found in \a as_nr
841  */
842 static INLINE kbase_context *kbasep_js_runpool_lookup_ctx_noretain(kbase_device *kbdev, int as_nr)
843 {
844         unsigned long flags;
845         kbasep_js_device_data *js_devdata;
846         kbase_context *found_kctx;
847         kbasep_js_per_as_data *js_per_as_data;
848
849         KBASE_DEBUG_ASSERT(kbdev != NULL);
850         KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
851         js_devdata = &kbdev->js_data;
852         js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
853
854         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
855
856         found_kctx = js_per_as_data->kctx;
857         KBASE_DEBUG_ASSERT(found_kctx == NULL || js_per_as_data->as_busy_refcount > 0);
858
859         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
860
861         return found_kctx;
862 }
863
864 /**
865  * This will provide a conversion from time (us) to ticks of the gpu clock
866  * based on the minimum available gpu frequency.
867  * This is usually good to compute best/worst case (where the use of current
868  * frequency is not valid due to DVFS).
869  * e.g.: when you need the number of cycles to guarantee you won't wait for
870  * longer than 'us' time (you might have a shorter wait).
871  */
872 static INLINE u32 kbasep_js_convert_us_to_gpu_ticks_min_freq(kbase_device *kbdev, u32 us)
873 {
874         u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_min;
875         KBASE_DEBUG_ASSERT(0 != gpu_freq);
876         return us * (gpu_freq / 1000);
877 }
878
879 /**
880  * This will provide a conversion from time (us) to ticks of the gpu clock
881  * based on the maximum available gpu frequency.
882  * This is usually good to compute best/worst case (where the use of current
883  * frequency is not valid due to DVFS).
884  * e.g.: When you need the number of cycles to guarantee you'll wait at least
885  * 'us' amount of time (but you might wait longer).
886  */
887 static INLINE u32 kbasep_js_convert_us_to_gpu_ticks_max_freq(kbase_device *kbdev, u32 us)
888 {
889         u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_max;
890         KBASE_DEBUG_ASSERT(0 != gpu_freq);
891         return us * (u32) (gpu_freq / 1000);
892 }
893
894 /**
895  * This will provide a conversion from ticks of the gpu clock to time (us)
896  * based on the minimum available gpu frequency.
897  * This is usually good to compute best/worst case (where the use of current
898  * frequency is not valid due to DVFS).
899  * e.g.: When you need to know the worst-case wait that 'ticks' cycles will
900  * take (you guarantee that you won't wait any longer than this, but it may
901  * be shorter).
902  */
903 static INLINE u32 kbasep_js_convert_gpu_ticks_to_us_min_freq(kbase_device *kbdev, u32 ticks)
904 {
905         u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_min;
906         KBASE_DEBUG_ASSERT(0 != gpu_freq);
907         return ticks / gpu_freq * 1000;
908 }
909
910 /**
911  * This will provide a conversion from ticks of the gpu clock to time (us)
912  * based on the maximum available gpu frequency.
913  * This is usually good to compute best/worst case (where the use of current
914  * frequency is not valid due to DVFS).
915  * e.g.: When you need to know the best-case wait for 'tick' cycles (you
916  * guarantee to be waiting for at least this long, but it may be longer).
917  */
918 static INLINE u32 kbasep_js_convert_gpu_ticks_to_us_max_freq(kbase_device *kbdev, u32 ticks)
919 {
920         u32 gpu_freq = kbdev->gpu_props.props.core_props.gpu_freq_khz_max;
921         KBASE_DEBUG_ASSERT(0 != gpu_freq);
922         return ticks / gpu_freq * 1000;
923 }
924
925           /** @} *//* end group kbase_js */
926           /** @} *//* end group base_kbase_api */
927           /** @} *//* end group base_api */
928
929 #endif                          /* _KBASE_JS_H_ */