3 * (C) COPYRIGHT 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.
21 * @file mali_kbase_js.h
28 #include <malisw/mali_malisw.h>
30 #include "mali_kbase_js_defs.h"
31 #include "mali_kbase_js_policy.h"
32 #include "mali_kbase_defs.h"
34 #include "mali_kbase_js_ctx_attr.h"
37 * @addtogroup base_api
42 * @addtogroup base_kbase_api
47 * @addtogroup kbase_js Job Scheduler Internal APIs
50 * These APIs are Internal to KBase and are available for use by the
51 * @ref kbase_js_policy "Job Scheduler Policy APIs"
55 * @brief Initialize the Job Scheduler
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.
61 mali_error kbasep_js_devdata_init(kbase_device * const kbdev);
64 * @brief Halt the Job Scheduler.
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
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.
74 * It is a Programming Error to call this whilst there are still kbase_context
75 * structures registered with this scheduler.
78 void kbasep_js_devdata_halt(kbase_device *kbdev);
81 * @brief Terminate the Job Scheduler
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
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.
91 * It is a Programming Error to call this whilst there are still kbase_context
92 * structures registered with this scheduler.
94 void kbasep_js_devdata_term(kbase_device *kbdev);
97 * @brief Initialize the Scheduling Component of a kbase_context on the Job Scheduler.
99 * This effectively registers a kbase_context with a Job Scheduler.
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().
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.
107 mali_error kbasep_js_kctx_init(kbase_context * const kctx);
110 * @brief Terminate the Scheduling Component of a kbase_context on the Job Scheduler
112 * This effectively de-registers a kbase_context from its Job Scheduler
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
118 * For this to work, the kbase_context must be zero intitialized before passing
119 * to the kbase_js_init() function.
121 * It is a Programming Error to call this whilst there are still jobs
122 * registered with this context.
124 void kbasep_js_kctx_term(kbase_context *kctx);
127 * @brief Add a job chain to the Job Scheduler, and take necessary actions to
128 * schedule the context/run the job.
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)
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
137 * - Otherwise, and if this is the first job on the context, it enqueues it on
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.
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.
151 * It is a programming error to have more than U32_MAX jobs in flight at a time.
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).
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.
167 mali_bool kbasep_js_add_job(kbase_context *kctx, kbase_jd_atom *atom);
170 * @brief Remove a job chain from the Job Scheduler, except for its 'retained state'.
172 * Completely removing a job requires several calls:
173 * - kbasep_js_copy_atom_retained_state(), to capture the 'retained state' of
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.
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.
181 * In the case of cancelling atoms, it is easier to call kbasep_js_remove_cancelled_job(), which handles all the necessary actions.
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()
190 * Do not use this for removing jobs being killed by kbase_jd_cancel() - use
191 * kbasep_js_remove_cancelled_job() instead.
193 * The following locking conditions are made on the caller:
194 * - it must hold kbasep_js_kctx_info::ctx::jsctx_mutex.
197 void kbasep_js_remove_job(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *atom);
200 * @brief Completely remove a job chain from the Job Scheduler, in the case
201 * where the job chain was cancelled.
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.
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()
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)
222 void kbasep_js_remove_cancelled_job(kbase_device *kbdev, kbase_context *kctx, kbase_jd_atom *katom);
225 * @brief Refcount a context as being busy, preventing it from being scheduled
228 * @note This function can safely be called from IRQ context.
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.
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).
237 mali_bool kbasep_js_runpool_retain_ctx(kbase_device *kbdev, kbase_context *kctx);
240 * @brief Refcount a context as being busy, preventing it from being scheduled
243 * @note This function can safely be called from IRQ context.
245 * The following locks must be held by the caller:
246 * - kbasep_js_device_data::runpool_irq::lock
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).
251 mali_bool kbasep_js_runpool_retain_ctx_nolock(kbase_device *kbdev, kbase_context *kctx);
254 * @brief Lookup a context in the Run Pool based upon its current address space
255 * and ensure that is stays scheduled in.
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.
261 * @note This function can safely be called from IRQ context.
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.
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
270 kbase_context *kbasep_js_runpool_lookup_ctx(kbase_device *kbdev, int as_nr);
273 * @brief Handling the requeuing/killing of a context that was evicted from the
274 * policy queue or runpool.
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
280 * - Otherwise, it is not added
282 * In addition, if the context is dying the jobs are killed asynchronously.
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.
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)
296 void kbasep_js_runpool_requeue_or_kill_ctx(kbase_device *kbdev, kbase_context *kctx, mali_bool has_pm_ref);
299 * @brief Release a refcount of a context being busy, allowing it to be
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
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
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()).
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.
330 * Unlike retaining a context in the runpool, this function \b cannot be called
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.
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)
347 void kbasep_js_runpool_release_ctx(kbase_device *kbdev, kbase_context *kctx);
350 * @brief Variant of kbasep_js_runpool_release_ctx() that handles additional
351 * actions from completing an atom.
353 * This is usually called as part of completing an atom and releasing the
354 * refcount on the context held by the atom.
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.
362 * The locking conditions of this function are the same as those for
363 * kbasep_js_runpool_release_ctx()
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);
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.
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.
378 * The following locks must be held by the caller:
379 * - kbasep_js_device_data::runpool_irq::lock
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.
389 mali_bool kbasep_js_try_run_next_job_on_slot_irq_nolock(kbase_device *kbdev, int js, s8 *submit_count);
392 * @brief Try to submit the next job on a particular slot, outside of IRQ context
394 * This obtains the Job Slot lock for the duration of the call only.
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).
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.
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
410 * This must only be called whilst the GPU is powered - for example, when
411 * kbdev->jsdata.nr_user_contexts_running > 0.
413 * @note The caller \em might be holding one of the
414 * kbasep_js_kctx_info::ctx::jsctx_mutex locks.
417 void kbasep_js_try_run_next_job_on_slot_nolock(kbase_device *kbdev, int js);
420 * @brief Try to submit the next job for each slot in the system, outside of IRQ context
422 * This will internally call kbasep_js_try_run_next_job_on_slot_nolock(), so similar
423 * locking conditions on the caller are required.
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
429 * @note The caller \em might be holding one of the
430 * kbasep_js_kctx_info::ctx::jsctx_mutex locks.
433 void kbasep_js_try_run_next_job_nolock(kbase_device *kbdev);
436 * @brief Try to schedule the next context onto the Run Pool
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.
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
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.
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)
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.
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.
476 void kbasep_js_try_schedule_head_ctx(kbase_device *kbdev);
479 * @brief Schedule in a privileged context
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).
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.
498 void kbasep_js_schedule_privileged_ctx(kbase_device *kbdev, kbase_context *kctx);
501 * @brief Release a privileged context, allowing it to be scheduled out.
503 * See kbasep_js_runpool_release_ctx for potential side effects.
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)
514 void kbasep_js_release_privileged_ctx(kbase_device *kbdev, kbase_context *kctx);
517 * @brief Handle the Job Scheduler component for the IRQ of a job finishing
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)
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.
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).
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.
541 * The following locking conditions are made on the caller:
542 * - it must hold kbasep_js_device_data::runpoool_irq::lock
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);
549 * @brief Try to submit the next job on each slot
551 * The following locks may be used:
552 * - kbasep_js_device_data::runpool_mutex
553 * - kbasep_js_device_data::runpool_irq::lock
555 void kbase_js_try_run_jobs(kbase_device *kbdev);
558 * @brief Try to submit the next job on a specfic slot
560 * The following locking conditions are made on the caller:
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)
568 void kbase_js_try_run_jobs_on_slot(kbase_device *kbdev, int js);
571 * @brief Handle releasing cores for power management and affinity management,
572 * ensuring that cores are powered down and affinity tracking is updated.
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)
577 * This function enters at the following @ref kbase_atom_coreref_state states:
578 * - NO_CORES_REQUESTED
579 * - WAITING_FOR_REQUESTED_CORES
583 * It transitions the above states back to NO_CORES_REQUESTED by the end of the
584 * function call (possibly via intermediate states).
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() ).
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.
593 void kbasep_js_job_check_deref_cores(kbase_device *kbdev, struct kbase_jd_atom *katom);
596 * @brief Suspend the job scheduler during a Power Management Suspend event.
598 * Causes all contexts to be removed from the runpool, and prevents any
599 * contexts from (re)entering the runpool.
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.
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.
607 * The caller must then wait for all Power Mangement active refcount to become
608 * zero before completing the suspend.
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).
616 void kbasep_js_suspend(kbase_device *kbdev);
619 * @brief Resume the Job Scheduler after a Power Management Resume event.
621 * This restores the actions from kbasep_js_suspend():
622 * - Schedules contexts back into the runpool
623 * - Resumes running atoms on the GPU
625 void kbasep_js_resume(kbase_device *kbdev);
633 * @brief Check that a context is allowed to submit jobs on this policy
635 * The purpose of this abstraction is to hide the underlying data size, and wrap up
636 * the long repeated line of code.
638 * As with any mali_bool, never test the return value with MALI_TRUE.
640 * The caller must hold kbasep_js_device_data::runpool_irq::lock.
642 static INLINE mali_bool kbasep_js_is_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
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);
650 test_bit = (u16) (1u << kctx->as_nr);
652 return (mali_bool) (js_devdata->runpool_irq.submit_allowed & test_bit);
656 * @brief Allow a context to submit jobs on this policy
658 * The purpose of this abstraction is to hide the underlying data size, and wrap up
659 * the long repeated line of code.
661 * The caller must hold kbasep_js_device_data::runpool_irq::lock.
663 static INLINE void kbasep_js_set_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
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);
671 set_bit = (u16) (1u << kctx->as_nr);
673 KBASE_DEBUG_PRINT_INFO(KBASE_JM, "JS: Setting Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
675 js_devdata->runpool_irq.submit_allowed |= set_bit;
679 * @brief Prevent a context from submitting more jobs on this policy
681 * The purpose of this abstraction is to hide the underlying data size, and wrap up
682 * the long repeated line of code.
684 * The caller must hold kbasep_js_device_data::runpool_irq::lock.
686 static INLINE void kbasep_js_clear_submit_allowed(kbasep_js_device_data *js_devdata, kbase_context *kctx)
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);
695 clear_bit = (u16) (1u << kctx->as_nr);
696 clear_mask = ~clear_bit;
698 KBASE_DEBUG_PRINT_INFO(KBASE_JM, "JS: Clearing Submit Allowed on %p (as=%d)", kctx, kctx->as_nr);
700 js_devdata->runpool_irq.submit_allowed &= clear_mask;
704 * @brief Manage the 'retry_submit_on_slot' part of a kbase_jd_atom
706 static INLINE void kbasep_js_clear_job_retry_submit(kbase_jd_atom *atom)
708 atom->retry_submit_on_slot = KBASEP_JS_RETRY_SUBMIT_SLOT_INVALID;
712 * Mark a slot as requiring resubmission by carrying that information on a
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.
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.
724 static INLINE void kbasep_js_set_job_retry_submit_slot(kbase_jd_atom *atom, int js)
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);
730 atom->retry_submit_on_slot = js;
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()
738 static INLINE void kbasep_js_atom_retained_state_init_invalid(kbasep_js_atom_retained_state *retained_state)
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;
746 * Copy atom state that can be made available after jd_done_nolock() is called
749 static INLINE void kbasep_js_atom_retained_state_copy(kbasep_js_atom_retained_state *retained_state, const kbase_jd_atom *katom)
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;
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.
760 * Reasons for an atom not finishing include:
761 * - Being soft-stopped (and so, the atom should be resubmitted sometime later)
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
767 static INLINE mali_bool kbasep_js_has_atom_finished(const kbasep_js_atom_retained_state *katom_retained_state)
769 return (mali_bool) (katom_retained_state->event_code != BASE_JD_EVENT_STOPPED && katom_retained_state->event_code != BASE_JD_EVENT_REMOVED_FROM_NEXT);
773 * @brief Determine whether a kbasep_js_atom_retained_state is valid
775 * An invalid kbasep_js_atom_retained_state is allowed, and indicates that the
776 * code should just ignore it.
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
782 static INLINE mali_bool kbasep_js_atom_retained_state_is_valid(const kbasep_js_atom_retained_state *katom_retained_state)
784 return (mali_bool) (katom_retained_state->core_req != KBASEP_JS_ATOM_RETAINED_STATE_CORE_REQ_INVALID);
787 static INLINE mali_bool kbasep_js_get_atom_retry_submit_slot(const kbasep_js_atom_retained_state *katom_retained_state, int *res)
789 int js = katom_retained_state->retry_submit_on_slot;
791 return (mali_bool) (js >= 0);
794 #if KBASE_DEBUG_DISABLE_ASSERTS == 0
796 * Debug Check the refcount of a context. Only use within ASSERTs
798 * Obtains kbasep_js_device_data::runpool_irq::lock
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.
804 static INLINE int kbasep_js_debug_check_ctx_refcount(kbase_device *kbdev, kbase_context *kctx)
807 kbasep_js_device_data *js_devdata;
811 KBASE_DEBUG_ASSERT(kbdev != NULL);
812 KBASE_DEBUG_ASSERT(kctx != NULL);
813 js_devdata = &kbdev->js_data;
815 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
817 if (as_nr != KBASEP_AS_NR_INVALID)
818 result = js_devdata->runpool_irq.per_as_data[as_nr].as_busy_refcount;
820 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
824 #endif /* KBASE_DEBUG_DISABLE_ASSERTS == 0 */
827 * @brief Variant of kbasep_js_runpool_lookup_ctx() that can be used when the
828 * context is guarenteed to be already previously retained.
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).
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.
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
842 static INLINE kbase_context *kbasep_js_runpool_lookup_ctx_noretain(kbase_device *kbdev, int as_nr)
845 kbasep_js_device_data *js_devdata;
846 kbase_context *found_kctx;
847 kbasep_js_per_as_data *js_per_as_data;
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];
854 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
856 found_kctx = js_per_as_data->kctx;
857 KBASE_DEBUG_ASSERT(found_kctx == NULL || js_per_as_data->as_busy_refcount > 0);
859 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
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).
872 static INLINE u32 kbasep_js_convert_us_to_gpu_ticks_min_freq(kbase_device *kbdev, u32 us)
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);
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).
887 static INLINE u32 kbasep_js_convert_us_to_gpu_ticks_max_freq(kbase_device *kbdev, u32 us)
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);
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
903 static INLINE u32 kbasep_js_convert_gpu_ticks_to_us_min_freq(kbase_device *kbdev, u32 ticks)
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;
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).
918 static INLINE u32 kbasep_js_convert_gpu_ticks_to_us_max_freq(kbase_device *kbdev, u32 ticks)
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;
925 /** @} *//* end group kbase_js */
926 /** @} *//* end group base_kbase_api */
927 /** @} *//* end group base_api */
929 #endif /* _KBASE_JS_H_ */