3 * (C) COPYRIGHT 2011-2016 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 * Job Scheduler Implementation
23 #include <mali_kbase.h>
24 #include <mali_kbase_js.h>
25 #if defined(CONFIG_MALI_GATOR_SUPPORT)
26 #include <mali_kbase_gator.h>
28 #include <mali_kbase_tlstream.h>
29 #include <mali_kbase_hw.h>
31 #include <mali_kbase_defs.h>
32 #include <mali_kbase_config_defaults.h>
34 #include "mali_kbase_jm.h"
35 #include "mali_kbase_hwaccess_jm.h"
41 /* Bitpattern indicating the result of releasing a context */
43 /* The context was descheduled - caller should try scheduling in a new
44 * one to keep the runpool full */
45 KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED = (1u << 0),
46 /* Ctx attributes were changed - caller should try scheduling all
48 KBASEP_JS_RELEASE_RESULT_SCHED_ALL = (1u << 1)
51 typedef u32 kbasep_js_release_result;
53 const int kbasep_js_atom_priority_to_relative[BASE_JD_NR_PRIO_LEVELS] = {
54 KBASE_JS_ATOM_SCHED_PRIO_MED, /* BASE_JD_PRIO_MEDIUM */
55 KBASE_JS_ATOM_SCHED_PRIO_HIGH, /* BASE_JD_PRIO_HIGH */
56 KBASE_JS_ATOM_SCHED_PRIO_LOW /* BASE_JD_PRIO_LOW */
60 kbasep_js_relative_priority_to_atom[KBASE_JS_ATOM_SCHED_PRIO_COUNT] = {
61 BASE_JD_PRIO_HIGH, /* KBASE_JS_ATOM_SCHED_PRIO_HIGH */
62 BASE_JD_PRIO_MEDIUM, /* KBASE_JS_ATOM_SCHED_PRIO_MED */
63 BASE_JD_PRIO_LOW /* KBASE_JS_ATOM_SCHED_PRIO_LOW */
68 * Private function prototypes
70 static kbasep_js_release_result kbasep_js_runpool_release_ctx_internal(
71 struct kbase_device *kbdev, struct kbase_context *kctx,
72 struct kbasep_js_atom_retained_state *katom_retained_state);
74 static int kbase_js_get_slot(struct kbase_device *kbdev,
75 struct kbase_jd_atom *katom);
77 static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
78 kbasep_js_policy_ctx_job_cb callback);
80 /* Helper for trace subcodes */
81 #if KBASE_TRACE_ENABLE
82 static int kbasep_js_trace_get_refcnt(struct kbase_device *kbdev,
83 struct kbase_context *kctx)
86 struct kbasep_js_device_data *js_devdata;
90 js_devdata = &kbdev->js_data;
92 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
94 if (as_nr != KBASEP_AS_NR_INVALID) {
95 struct kbasep_js_per_as_data *js_per_as_data;
97 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
99 refcnt = js_per_as_data->as_busy_refcount;
101 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
106 static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
107 struct kbase_context *kctx)
109 struct kbasep_js_device_data *js_devdata;
113 js_devdata = &kbdev->js_data;
116 if (as_nr != KBASEP_AS_NR_INVALID) {
117 struct kbasep_js_per_as_data *js_per_as_data;
119 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
121 refcnt = js_per_as_data->as_busy_refcount;
126 #else /* KBASE_TRACE_ENABLE */
127 static int kbasep_js_trace_get_refcnt(struct kbase_device *kbdev,
128 struct kbase_context *kctx)
134 static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
135 struct kbase_context *kctx)
141 #endif /* KBASE_TRACE_ENABLE */
147 JS_DEVDATA_INIT_NONE = 0,
148 JS_DEVDATA_INIT_CONSTANTS = (1 << 0),
149 JS_DEVDATA_INIT_POLICY = (1 << 1),
150 JS_DEVDATA_INIT_ALL = ((1 << 2) - 1)
154 JS_KCTX_INIT_NONE = 0,
155 JS_KCTX_INIT_CONSTANTS = (1 << 0),
156 JS_KCTX_INIT_POLICY = (1 << 1),
157 JS_KCTX_INIT_ALL = ((1 << 2) - 1)
165 * core_reqs_from_jsn_features - Convert JSn_FEATURES to core requirements
166 * @features: JSn_FEATURE register value
168 * Given a JSn_FEATURE register value returns the core requirements that match
170 * Return: Core requirement bit mask
172 static base_jd_core_req core_reqs_from_jsn_features(u16 features)
174 base_jd_core_req core_req = 0u;
176 if ((features & JS_FEATURE_SET_VALUE_JOB) != 0)
177 core_req |= BASE_JD_REQ_V;
179 if ((features & JS_FEATURE_CACHE_FLUSH_JOB) != 0)
180 core_req |= BASE_JD_REQ_CF;
182 if ((features & JS_FEATURE_COMPUTE_JOB) != 0)
183 core_req |= BASE_JD_REQ_CS;
185 if ((features & JS_FEATURE_TILER_JOB) != 0)
186 core_req |= BASE_JD_REQ_T;
188 if ((features & JS_FEATURE_FRAGMENT_JOB) != 0)
189 core_req |= BASE_JD_REQ_FS;
194 static void kbase_js_sync_timers(struct kbase_device *kbdev)
196 mutex_lock(&kbdev->js_data.runpool_mutex);
197 kbase_backend_ctx_count_changed(kbdev);
198 mutex_unlock(&kbdev->js_data.runpool_mutex);
201 /* Hold the kbasep_js_device_data::runpool_irq::lock for this */
202 bool kbasep_js_runpool_retain_ctx_nolock(struct kbase_device *kbdev,
203 struct kbase_context *kctx)
205 struct kbasep_js_device_data *js_devdata;
206 struct kbasep_js_per_as_data *js_per_as_data;
210 KBASE_DEBUG_ASSERT(kbdev != NULL);
211 KBASE_DEBUG_ASSERT(kctx != NULL);
212 js_devdata = &kbdev->js_data;
215 if (as_nr != KBASEP_AS_NR_INVALID) {
218 KBASE_DEBUG_ASSERT(as_nr >= 0);
219 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
221 KBASE_DEBUG_ASSERT(js_per_as_data->kctx != NULL);
223 new_refcnt = ++(js_per_as_data->as_busy_refcount);
225 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RETAIN_CTX_NOLOCK, kctx,
226 NULL, 0u, new_refcnt);
234 * jsctx_rb_none_to_pull_prio(): - Check if there are no pullable atoms
235 * @kctx: Pointer to kbase context with ring buffer.
236 * @js: Job slot id to check.
237 * @prio: Priority to check.
239 * Return true if there are no atoms to pull. There may be running atoms in the
240 * ring buffer even if there are no atoms to pull. It is also possible for the
241 * ring buffer to be full (with running atoms) when this functions returns
244 * Return: true if there are no atoms to pull, false otherwise.
247 jsctx_rb_none_to_pull_prio(struct kbase_context *kctx, int js, int prio)
249 struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
251 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
253 return RB_EMPTY_ROOT(&rb->runnable_tree);
257 * jsctx_rb_none_to_pull(): - Check if all priority ring buffers have no
259 * @kctx: Pointer to kbase context with ring buffer.
260 * @js: Job slot id to check.
262 * Caller must hold runpool_irq.lock
264 * Return: true if the ring buffers for all priorities have no pullable atoms,
268 jsctx_rb_none_to_pull(struct kbase_context *kctx, int js)
272 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
274 for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
275 if (!jsctx_rb_none_to_pull_prio(kctx, js, prio))
283 * jsctx_queue_foreach_prio(): - Execute callback for each entry in the queue.
284 * @kctx: Pointer to kbase context with the queue.
285 * @js: Job slot id to iterate.
286 * @prio: Priority id to iterate.
287 * @callback: Function pointer to callback.
289 * Iterate over a queue and invoke @callback for each entry in the queue, and
290 * remove the entry from the queue.
292 * If entries are added to the queue while this is running those entries may, or
293 * may not be covered. To ensure that all entries in the buffer have been
294 * enumerated when this function returns jsctx->lock must be held when calling
297 * The HW access lock, js_data.runpool_irq.lock, must always be held when
298 * calling this function.
301 jsctx_queue_foreach_prio(struct kbase_context *kctx, int js, int prio,
302 kbasep_js_policy_ctx_job_cb callback)
304 struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
306 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
308 while (!RB_EMPTY_ROOT(&queue->runnable_tree)) {
309 struct rb_node *node = rb_first(&queue->runnable_tree);
310 struct kbase_jd_atom *entry = rb_entry(node,
311 struct kbase_jd_atom, runnable_tree_node);
313 rb_erase(node, &queue->runnable_tree);
314 callback(kctx->kbdev, entry);
317 while (!list_empty(&queue->x_dep_head)) {
318 struct kbase_jd_atom *entry = list_entry(queue->x_dep_head.next,
319 struct kbase_jd_atom, queue);
321 list_del(queue->x_dep_head.next);
323 callback(kctx->kbdev, entry);
328 * jsctx_queue_foreach(): - Execute callback for each entry in every queue
329 * @kctx: Pointer to kbase context with queue.
330 * @js: Job slot id to iterate.
331 * @callback: Function pointer to callback.
333 * Iterate over all the different priorities, and for each call
334 * jsctx_queue_foreach_prio() to iterate over the queue and invoke @callback
335 * for each entry, and remove the entry from the queue.
338 jsctx_queue_foreach(struct kbase_context *kctx, int js,
339 kbasep_js_policy_ctx_job_cb callback)
343 for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++)
344 jsctx_queue_foreach_prio(kctx, js, prio, callback);
348 * jsctx_rb_peek_prio(): - Check buffer and get next atom
349 * @kctx: Pointer to kbase context with ring buffer.
350 * @js: Job slot id to check.
351 * @prio: Priority id to check.
353 * Check the ring buffer for the specified @js and @prio and return a pointer to
354 * the next atom, unless the ring buffer is empty.
356 * Return: Pointer to next atom in buffer, or NULL if there is no atom.
358 static inline struct kbase_jd_atom *
359 jsctx_rb_peek_prio(struct kbase_context *kctx, int js, int prio)
361 struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
362 struct rb_node *node;
364 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
366 node = rb_first(&rb->runnable_tree);
370 return rb_entry(node, struct kbase_jd_atom, runnable_tree_node);
374 * jsctx_rb_peek(): - Check all priority buffers and get next atom
375 * @kctx: Pointer to kbase context with ring buffer.
376 * @js: Job slot id to check.
378 * Check the ring buffers for all priorities, starting from
379 * KBASE_JS_ATOM_SCHED_PRIO_HIGH, for the specified @js and @prio and return a
380 * pointer to the next atom, unless all the priority's ring buffers are empty.
382 * Caller must hold the runpool_irq.lock.
384 * Return: Pointer to next atom in buffer, or NULL if there is no atom.
386 static inline struct kbase_jd_atom *
387 jsctx_rb_peek(struct kbase_context *kctx, int js)
391 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
393 for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
394 struct kbase_jd_atom *katom;
396 katom = jsctx_rb_peek_prio(kctx, js, prio);
405 * jsctx_rb_pull(): - Mark atom in list as running
406 * @kctx: Pointer to kbase context with ring buffer.
407 * @katom: Pointer to katom to pull.
409 * Mark an atom previously obtained from jsctx_rb_peek() as running.
411 * @katom must currently be at the head of the ring buffer.
414 jsctx_rb_pull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
416 int prio = katom->sched_priority;
417 int js = katom->slot_nr;
418 struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
420 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
422 /* Atoms must be pulled in the correct order. */
423 WARN_ON(katom != jsctx_rb_peek_prio(kctx, js, prio));
425 rb_erase(&katom->runnable_tree_node, &rb->runnable_tree);
428 #define LESS_THAN_WRAP(a, b) ((s32)(a - b) < 0)
431 jsctx_tree_add(struct kbase_context *kctx, struct kbase_jd_atom *katom)
433 int prio = katom->sched_priority;
434 int js = katom->slot_nr;
435 struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
436 struct rb_node **new = &(queue->runnable_tree.rb_node), *parent = NULL;
438 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
441 struct kbase_jd_atom *entry = container_of(*new,
442 struct kbase_jd_atom, runnable_tree_node);
445 if (LESS_THAN_WRAP(katom->age, entry->age))
446 new = &((*new)->rb_left);
448 new = &((*new)->rb_right);
451 /* Add new node and rebalance tree. */
452 rb_link_node(&katom->runnable_tree_node, parent, new);
453 rb_insert_color(&katom->runnable_tree_node, &queue->runnable_tree);
457 * jsctx_rb_unpull(): - Undo marking of atom in list as running
458 * @kctx: Pointer to kbase context with ring buffer.
459 * @katom: Pointer to katom to unpull.
461 * Undo jsctx_rb_pull() and put @katom back in the queue.
463 * jsctx_rb_unpull() must be called on atoms in the same order the atoms were
467 jsctx_rb_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
469 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
471 jsctx_tree_add(kctx, katom);
474 static bool kbase_js_ctx_pullable(struct kbase_context *kctx,
477 static bool kbase_js_ctx_list_add_pullable_nolock(struct kbase_device *kbdev,
478 struct kbase_context *kctx,
480 static bool kbase_js_ctx_list_add_unpullable_nolock(struct kbase_device *kbdev,
481 struct kbase_context *kctx,
485 * Functions private to KBase ('Protected' functions)
487 int kbasep_js_devdata_init(struct kbase_device * const kbdev)
489 struct kbasep_js_device_data *jsdd;
494 KBASE_DEBUG_ASSERT(kbdev != NULL);
496 jsdd = &kbdev->js_data;
498 KBASE_DEBUG_ASSERT(jsdd->init_status == JS_DEVDATA_INIT_NONE);
500 /* These two must be recalculated if nr_hw_address_spaces changes
501 * (e.g. for HW workarounds) */
502 as_present = (1U << kbdev->nr_hw_address_spaces) - 1;
503 kbdev->nr_user_address_spaces = kbdev->nr_hw_address_spaces;
504 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987)) {
507 use_workaround = DEFAULT_SECURE_BUT_LOSS_OF_PERFORMANCE;
508 if (use_workaround) {
509 dev_dbg(kbdev->dev, "GPU has HW ISSUE 8987, and driver configured for security workaround: 1 address space only");
510 kbdev->nr_user_address_spaces = 1;
513 #ifdef CONFIG_MALI_DEBUG
514 /* Soft-stop will be disabled on a single context by default unless
515 * softstop_always is set */
516 jsdd->softstop_always = false;
517 #endif /* CONFIG_MALI_DEBUG */
518 jsdd->nr_all_contexts_running = 0;
519 jsdd->nr_user_contexts_running = 0;
520 jsdd->nr_contexts_pullable = 0;
521 atomic_set(&jsdd->nr_contexts_runnable, 0);
522 /* All ASs initially free */
523 jsdd->as_free = as_present;
524 /* No ctx allowed to submit */
525 jsdd->runpool_irq.submit_allowed = 0u;
526 memset(jsdd->runpool_irq.ctx_attr_ref_count, 0,
527 sizeof(jsdd->runpool_irq.ctx_attr_ref_count));
528 memset(jsdd->runpool_irq.slot_affinities, 0,
529 sizeof(jsdd->runpool_irq.slot_affinities));
530 memset(jsdd->runpool_irq.slot_affinity_refcount, 0,
531 sizeof(jsdd->runpool_irq.slot_affinity_refcount));
532 INIT_LIST_HEAD(&jsdd->suspended_soft_jobs_list);
534 /* Config attributes */
535 jsdd->scheduling_period_ns = DEFAULT_JS_SCHEDULING_PERIOD_NS;
536 jsdd->soft_stop_ticks = DEFAULT_JS_SOFT_STOP_TICKS;
537 jsdd->soft_stop_ticks_cl = DEFAULT_JS_SOFT_STOP_TICKS_CL;
538 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
539 jsdd->hard_stop_ticks_ss = DEFAULT_JS_HARD_STOP_TICKS_SS_8408;
541 jsdd->hard_stop_ticks_ss = DEFAULT_JS_HARD_STOP_TICKS_SS;
542 jsdd->hard_stop_ticks_cl = DEFAULT_JS_HARD_STOP_TICKS_CL;
543 jsdd->hard_stop_ticks_dumping = DEFAULT_JS_HARD_STOP_TICKS_DUMPING;
544 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8408))
545 jsdd->gpu_reset_ticks_ss = DEFAULT_JS_RESET_TICKS_SS_8408;
547 jsdd->gpu_reset_ticks_ss = DEFAULT_JS_RESET_TICKS_SS;
548 jsdd->gpu_reset_ticks_cl = DEFAULT_JS_RESET_TICKS_CL;
549 jsdd->gpu_reset_ticks_dumping = DEFAULT_JS_RESET_TICKS_DUMPING;
550 jsdd->ctx_timeslice_ns = DEFAULT_JS_CTX_TIMESLICE_NS;
551 jsdd->cfs_ctx_runtime_init_slices =
552 DEFAULT_JS_CFS_CTX_RUNTIME_INIT_SLICES;
553 jsdd->cfs_ctx_runtime_min_slices =
554 DEFAULT_JS_CFS_CTX_RUNTIME_MIN_SLICES;
555 atomic_set(&jsdd->soft_job_timeout_ms, DEFAULT_JS_SOFT_JOB_TIMEOUT);
557 dev_dbg(kbdev->dev, "JS Config Attribs: ");
558 dev_dbg(kbdev->dev, "\tscheduling_period_ns:%u",
559 jsdd->scheduling_period_ns);
560 dev_dbg(kbdev->dev, "\tsoft_stop_ticks:%u",
561 jsdd->soft_stop_ticks);
562 dev_dbg(kbdev->dev, "\tsoft_stop_ticks_cl:%u",
563 jsdd->soft_stop_ticks_cl);
564 dev_dbg(kbdev->dev, "\thard_stop_ticks_ss:%u",
565 jsdd->hard_stop_ticks_ss);
566 dev_dbg(kbdev->dev, "\thard_stop_ticks_cl:%u",
567 jsdd->hard_stop_ticks_cl);
568 dev_dbg(kbdev->dev, "\thard_stop_ticks_dumping:%u",
569 jsdd->hard_stop_ticks_dumping);
570 dev_dbg(kbdev->dev, "\tgpu_reset_ticks_ss:%u",
571 jsdd->gpu_reset_ticks_ss);
572 dev_dbg(kbdev->dev, "\tgpu_reset_ticks_cl:%u",
573 jsdd->gpu_reset_ticks_cl);
574 dev_dbg(kbdev->dev, "\tgpu_reset_ticks_dumping:%u",
575 jsdd->gpu_reset_ticks_dumping);
576 dev_dbg(kbdev->dev, "\tctx_timeslice_ns:%u",
577 jsdd->ctx_timeslice_ns);
578 dev_dbg(kbdev->dev, "\tcfs_ctx_runtime_init_slices:%u",
579 jsdd->cfs_ctx_runtime_init_slices);
580 dev_dbg(kbdev->dev, "\tcfs_ctx_runtime_min_slices:%u",
581 jsdd->cfs_ctx_runtime_min_slices);
582 dev_dbg(kbdev->dev, "\tsoft_job_timeout:%i",
583 atomic_read(&jsdd->soft_job_timeout_ms));
585 if (!(jsdd->soft_stop_ticks < jsdd->hard_stop_ticks_ss &&
586 jsdd->hard_stop_ticks_ss < jsdd->gpu_reset_ticks_ss &&
587 jsdd->soft_stop_ticks < jsdd->hard_stop_ticks_dumping &&
588 jsdd->hard_stop_ticks_dumping <
589 jsdd->gpu_reset_ticks_dumping)) {
590 dev_err(kbdev->dev, "Job scheduler timeouts invalid; soft/hard/reset tick counts should be in increasing order\n");
594 #if KBASE_DISABLE_SCHEDULING_SOFT_STOPS
595 dev_dbg(kbdev->dev, "Job Scheduling Policy Soft-stops disabled, ignoring value for soft_stop_ticks==%u at %uns per tick. Other soft-stops may still occur.",
596 jsdd->soft_stop_ticks,
597 jsdd->scheduling_period_ns);
599 #if KBASE_DISABLE_SCHEDULING_HARD_STOPS
600 dev_dbg(kbdev->dev, "Job Scheduling Policy Hard-stops disabled, ignoring values for hard_stop_ticks_ss==%d and hard_stop_ticks_dumping==%u at %uns per tick. Other hard-stops may still occur.",
601 jsdd->hard_stop_ticks_ss,
602 jsdd->hard_stop_ticks_dumping,
603 jsdd->scheduling_period_ns);
605 #if KBASE_DISABLE_SCHEDULING_SOFT_STOPS && KBASE_DISABLE_SCHEDULING_HARD_STOPS
606 dev_dbg(kbdev->dev, "Note: The JS policy's tick timer (if coded) will still be run, but do nothing.");
609 /* setup the number of irq throttle cycles base on given time */
611 int time_us = kbdev->gpu_props.irq_throttle_time_us;
612 int cycles = kbasep_js_convert_us_to_gpu_ticks_max_freq(kbdev,
615 atomic_set(&kbdev->irq_throttle_cycles, cycles);
618 /* Clear the AS data, including setting NULL pointers */
619 memset(&jsdd->runpool_irq.per_as_data[0], 0,
620 sizeof(jsdd->runpool_irq.per_as_data));
622 for (i = 0; i < kbdev->gpu_props.num_job_slots; ++i)
623 jsdd->js_reqs[i] = core_reqs_from_jsn_features(
624 kbdev->gpu_props.props.raw_props.js_features[i]);
626 jsdd->init_status |= JS_DEVDATA_INIT_CONSTANTS;
628 /* On error, we could continue on: providing none of the below resources
629 * rely on the ones above */
631 mutex_init(&jsdd->runpool_mutex);
632 mutex_init(&jsdd->queue_mutex);
633 spin_lock_init(&jsdd->runpool_irq.lock);
634 sema_init(&jsdd->schedule_sem, 1);
636 err = kbasep_js_policy_init(kbdev);
638 jsdd->init_status |= JS_DEVDATA_INIT_POLICY;
640 for (i = 0; i < kbdev->gpu_props.num_job_slots; ++i) {
641 INIT_LIST_HEAD(&jsdd->ctx_list_pullable[i]);
642 INIT_LIST_HEAD(&jsdd->ctx_list_unpullable[i]);
645 /* On error, do no cleanup; this will be handled by the caller(s), since
646 * we've designed this resource to be safe to terminate on init-fail */
647 if (jsdd->init_status != JS_DEVDATA_INIT_ALL)
653 void kbasep_js_devdata_halt(struct kbase_device *kbdev)
658 void kbasep_js_devdata_term(struct kbase_device *kbdev)
660 struct kbasep_js_device_data *js_devdata;
662 KBASE_DEBUG_ASSERT(kbdev != NULL);
664 js_devdata = &kbdev->js_data;
666 if ((js_devdata->init_status & JS_DEVDATA_INIT_CONSTANTS)) {
667 s8 zero_ctx_attr_ref_count[KBASEP_JS_CTX_ATTR_COUNT] = { 0, };
668 /* The caller must de-register all contexts before calling this
670 KBASE_DEBUG_ASSERT(js_devdata->nr_all_contexts_running == 0);
671 KBASE_DEBUG_ASSERT(memcmp(
672 js_devdata->runpool_irq.ctx_attr_ref_count,
673 zero_ctx_attr_ref_count,
674 sizeof(zero_ctx_attr_ref_count)) == 0);
675 CSTD_UNUSED(zero_ctx_attr_ref_count);
677 if ((js_devdata->init_status & JS_DEVDATA_INIT_POLICY))
678 kbasep_js_policy_term(&js_devdata->policy);
680 js_devdata->init_status = JS_DEVDATA_INIT_NONE;
683 int kbasep_js_kctx_init(struct kbase_context * const kctx)
685 struct kbase_device *kbdev;
686 struct kbasep_js_kctx_info *js_kctx_info;
690 KBASE_DEBUG_ASSERT(kctx != NULL);
693 KBASE_DEBUG_ASSERT(kbdev != NULL);
695 for (i = 0; i < BASE_JM_MAX_NR_SLOTS; ++i)
696 INIT_LIST_HEAD(&kctx->jctx.sched_info.ctx.ctx_list_entry[i]);
698 js_kctx_info = &kctx->jctx.sched_info;
699 KBASE_DEBUG_ASSERT(js_kctx_info->init_status == JS_KCTX_INIT_NONE);
701 js_kctx_info->ctx.nr_jobs = 0;
702 js_kctx_info->ctx.is_scheduled = false;
703 js_kctx_info->ctx.is_dying = false;
704 memset(js_kctx_info->ctx.ctx_attr_ref_count, 0,
705 sizeof(js_kctx_info->ctx.ctx_attr_ref_count));
707 /* Initially, the context is disabled from submission until the create
709 js_kctx_info->ctx.flags = KBASE_CTX_FLAG_SUBMIT_DISABLED;
711 js_kctx_info->init_status |= JS_KCTX_INIT_CONSTANTS;
713 /* On error, we could continue on: providing none of the below resources
714 * rely on the ones above */
715 mutex_init(&js_kctx_info->ctx.jsctx_mutex);
717 init_waitqueue_head(&js_kctx_info->ctx.is_scheduled_wait);
719 err = kbasep_js_policy_init_ctx(kbdev, kctx);
721 js_kctx_info->init_status |= JS_KCTX_INIT_POLICY;
723 /* On error, do no cleanup; this will be handled by the caller(s), since
724 * we've designed this resource to be safe to terminate on init-fail */
725 if (js_kctx_info->init_status != JS_KCTX_INIT_ALL)
728 for (i = 0; i < KBASE_JS_ATOM_SCHED_PRIO_COUNT; i++) {
729 for (j = 0; j < BASE_JM_MAX_NR_SLOTS; j++) {
730 INIT_LIST_HEAD(&kctx->jsctx_queue[i][j].x_dep_head);
731 kctx->jsctx_queue[i][j].runnable_tree = RB_ROOT;
738 void kbasep_js_kctx_term(struct kbase_context *kctx)
740 struct kbase_device *kbdev;
741 struct kbasep_js_kctx_info *js_kctx_info;
742 union kbasep_js_policy *js_policy;
744 bool update_ctx_count = false;
746 KBASE_DEBUG_ASSERT(kctx != NULL);
749 KBASE_DEBUG_ASSERT(kbdev != NULL);
751 js_policy = &kbdev->js_data.policy;
752 js_kctx_info = &kctx->jctx.sched_info;
754 if ((js_kctx_info->init_status & JS_KCTX_INIT_CONSTANTS)) {
755 /* The caller must de-register all jobs before calling this */
756 KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
757 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs == 0);
760 mutex_lock(&kbdev->js_data.queue_mutex);
761 mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
763 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
764 list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
766 if (kctx->ctx_runnable_ref) {
767 WARN_ON(atomic_read(&kbdev->js_data.nr_contexts_runnable) <= 0);
768 atomic_dec(&kbdev->js_data.nr_contexts_runnable);
769 update_ctx_count = true;
770 kctx->ctx_runnable_ref = false;
773 mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
774 mutex_unlock(&kbdev->js_data.queue_mutex);
776 if ((js_kctx_info->init_status & JS_KCTX_INIT_POLICY))
777 kbasep_js_policy_term_ctx(js_policy, kctx);
779 js_kctx_info->init_status = JS_KCTX_INIT_NONE;
781 if (update_ctx_count) {
782 mutex_lock(&kbdev->js_data.runpool_mutex);
783 kbase_backend_ctx_count_changed(kbdev);
784 mutex_unlock(&kbdev->js_data.runpool_mutex);
789 * kbase_js_ctx_list_add_pullable_nolock - Variant of
790 * kbase_jd_ctx_list_add_pullable()
791 * where the caller must hold
793 * @kbdev: Device pointer
794 * @kctx: Context to add to queue
795 * @js: Job slot to use
797 * Caller must hold runpool_irq.lock
799 * Return: true if caller should call kbase_backend_ctx_count_changed()
801 static bool kbase_js_ctx_list_add_pullable_nolock(struct kbase_device *kbdev,
802 struct kbase_context *kctx,
807 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
809 if (!list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
810 list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
812 list_add_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
813 &kbdev->js_data.ctx_list_pullable[js]);
815 if (!kctx->slots_pullable) {
816 kbdev->js_data.nr_contexts_pullable++;
818 if (!atomic_read(&kctx->atoms_pulled)) {
819 WARN_ON(kctx->ctx_runnable_ref);
820 kctx->ctx_runnable_ref = true;
821 atomic_inc(&kbdev->js_data.nr_contexts_runnable);
824 kctx->slots_pullable |= (1 << js);
830 * kbase_js_ctx_list_add_pullable_head_nolock - Variant of
831 * kbase_js_ctx_list_add_pullable_head()
832 * where the caller must hold
834 * @kbdev: Device pointer
835 * @kctx: Context to add to queue
836 * @js: Job slot to use
838 * Caller must hold runpool_irq.lock
840 * Return: true if caller should call kbase_backend_ctx_count_changed()
842 static bool kbase_js_ctx_list_add_pullable_head_nolock(
843 struct kbase_device *kbdev, struct kbase_context *kctx, int js)
847 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
849 if (!list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
850 list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
852 list_add(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
853 &kbdev->js_data.ctx_list_pullable[js]);
855 if (!kctx->slots_pullable) {
856 kbdev->js_data.nr_contexts_pullable++;
858 if (!atomic_read(&kctx->atoms_pulled)) {
859 WARN_ON(kctx->ctx_runnable_ref);
860 kctx->ctx_runnable_ref = true;
861 atomic_inc(&kbdev->js_data.nr_contexts_runnable);
864 kctx->slots_pullable |= (1 << js);
870 * kbase_js_ctx_list_add_pullable_head - Add context to the head of the
871 * per-slot pullable context queue
872 * @kbdev: Device pointer
873 * @kctx: Context to add to queue
874 * @js: Job slot to use
876 * If the context is on either the pullable or unpullable queues, then it is
877 * removed before being added to the head.
879 * This function should be used when a context has been scheduled, but no jobs
880 * can currently be pulled from it.
882 * Return: true if caller should call kbase_backend_ctx_count_changed()
884 static bool kbase_js_ctx_list_add_pullable_head(struct kbase_device *kbdev,
885 struct kbase_context *kctx,
891 spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
892 ret = kbase_js_ctx_list_add_pullable_head_nolock(kbdev, kctx, js);
893 spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
899 * kbase_js_ctx_list_add_unpullable_nolock - Add context to the tail of the
900 * per-slot unpullable context queue
901 * @kbdev: Device pointer
902 * @kctx: Context to add to queue
903 * @js: Job slot to use
905 * The context must already be on the per-slot pullable queue. It will be
906 * removed from the pullable queue before being added to the unpullable queue.
908 * This function should be used when a context has been pulled from, and there
909 * are no jobs remaining on the specified slot.
911 * Caller must hold runpool_irq.lock
913 * Return: true if caller should call kbase_backend_ctx_count_changed()
915 static bool kbase_js_ctx_list_add_unpullable_nolock(struct kbase_device *kbdev,
916 struct kbase_context *kctx,
921 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
923 list_move_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
924 &kbdev->js_data.ctx_list_unpullable[js]);
926 if (kctx->slots_pullable == (1 << js)) {
927 kbdev->js_data.nr_contexts_pullable--;
929 if (!atomic_read(&kctx->atoms_pulled)) {
930 WARN_ON(!kctx->ctx_runnable_ref);
931 kctx->ctx_runnable_ref = false;
932 atomic_dec(&kbdev->js_data.nr_contexts_runnable);
935 kctx->slots_pullable &= ~(1 << js);
941 * kbase_js_ctx_list_remove_nolock - Remove context from the per-slot pullable
942 * or unpullable context queues
943 * @kbdev: Device pointer
944 * @kctx: Context to remove from queue
945 * @js: Job slot to use
947 * The context must already be on one of the queues.
949 * This function should be used when a context has no jobs on the GPU, and no
950 * jobs remaining for the specified slot.
952 * Caller must hold runpool_irq.lock
954 * Return: true if caller should call kbase_backend_ctx_count_changed()
956 static bool kbase_js_ctx_list_remove_nolock(struct kbase_device *kbdev,
957 struct kbase_context *kctx,
962 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
964 WARN_ON(list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]));
966 list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
968 if (kctx->slots_pullable == (1 << js)) {
969 kbdev->js_data.nr_contexts_pullable--;
971 if (!atomic_read(&kctx->atoms_pulled)) {
972 WARN_ON(!kctx->ctx_runnable_ref);
973 kctx->ctx_runnable_ref = false;
974 atomic_dec(&kbdev->js_data.nr_contexts_runnable);
977 kctx->slots_pullable &= ~(1 << js);
983 * kbase_js_ctx_list_pop_head_nolock - Variant of kbase_js_ctx_list_pop_head()
984 * where the caller must hold
986 * @kbdev: Device pointer
987 * @js: Job slot to use
989 * Caller must hold runpool_irq.lock
991 * Return: Context to use for specified slot.
992 * NULL if no contexts present for specified slot
994 static struct kbase_context *kbase_js_ctx_list_pop_head_nolock(
995 struct kbase_device *kbdev,
998 struct kbase_context *kctx;
1000 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1002 if (list_empty(&kbdev->js_data.ctx_list_pullable[js]))
1005 kctx = list_entry(kbdev->js_data.ctx_list_pullable[js].next,
1006 struct kbase_context,
1007 jctx.sched_info.ctx.ctx_list_entry[js]);
1009 list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
1015 * kbase_js_ctx_list_pop_head - Pop the head context off the per-slot pullable
1017 * @kbdev: Device pointer
1018 * @js: Job slot to use
1020 * Return: Context to use for specified slot.
1021 * NULL if no contexts present for specified slot
1023 static struct kbase_context *kbase_js_ctx_list_pop_head(
1024 struct kbase_device *kbdev, int js)
1026 struct kbase_context *kctx;
1027 unsigned long flags;
1029 spin_lock_irqsave(&kbdev->js_data.runpool_irq.lock, flags);
1030 kctx = kbase_js_ctx_list_pop_head_nolock(kbdev, js);
1031 spin_unlock_irqrestore(&kbdev->js_data.runpool_irq.lock, flags);
1037 * kbase_js_ctx_pullable - Return if a context can be pulled from on the
1039 * @kctx: Context pointer
1040 * @js: Job slot to use
1041 * @is_scheduled: true if the context is currently scheduled
1043 * Caller must hold runpool_irq.lock
1045 * Return: true if context can be pulled from on specified slot
1048 static bool kbase_js_ctx_pullable(struct kbase_context *kctx, int js,
1051 struct kbasep_js_device_data *js_devdata;
1052 struct kbase_jd_atom *katom;
1054 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
1056 js_devdata = &kctx->kbdev->js_data;
1059 if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
1062 katom = jsctx_rb_peek(kctx, js);
1064 return false; /* No pullable atoms */
1065 if (atomic_read(&katom->blocked))
1066 return false; /* next atom blocked */
1067 if (katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED) {
1068 if (katom->x_pre_dep->gpu_rb_state ==
1069 KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB ||
1070 katom->x_pre_dep->will_fail_event_code)
1072 if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
1073 kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
1080 static bool kbase_js_dep_validate(struct kbase_context *kctx,
1081 struct kbase_jd_atom *katom)
1083 struct kbase_device *kbdev = kctx->kbdev;
1085 bool has_dep = false, has_x_dep = false;
1086 int js = kbase_js_get_slot(kbdev, katom);
1087 int prio = katom->sched_priority;
1090 for (i = 0; i < 2; i++) {
1091 struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
1094 int dep_js = kbase_js_get_slot(kbdev, dep_atom);
1095 int dep_prio = dep_atom->sched_priority;
1097 /* Dependent atom must already have been submitted */
1098 if (!(dep_atom->atom_flags &
1099 KBASE_KATOM_FLAG_JSCTX_IN_TREE)) {
1104 /* Dependencies with different priorities can't
1105 be represented in the ringbuffer */
1106 if (prio != dep_prio) {
1112 /* Only one same-slot dependency can be
1113 * represented in the ringbuffer */
1118 /* Each dependee atom can only have one
1119 * same-slot dependency */
1120 if (dep_atom->post_dep) {
1126 /* Only one cross-slot dependency can be
1127 * represented in the ringbuffer */
1132 /* Each dependee atom can only have one
1133 * cross-slot dependency */
1134 if (dep_atom->x_post_dep) {
1138 /* The dependee atom can not already be in the
1139 * HW access ringbuffer */
1140 if (dep_atom->gpu_rb_state !=
1141 KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB) {
1145 /* The dependee atom can not already have
1147 if (dep_atom->status !=
1148 KBASE_JD_ATOM_STATE_IN_JS) {
1152 /* Cross-slot dependencies must not violate
1153 * PRLAM-8987 affinity restrictions */
1154 if (kbase_hw_has_issue(kbdev,
1155 BASE_HW_ISSUE_8987) &&
1156 (js == 2 || dep_js == 2)) {
1163 /* Dependency can be represented in ringbuffers */
1167 /* If dependencies can be represented by ringbuffer then clear them from
1170 for (i = 0; i < 2; i++) {
1171 struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
1174 int dep_js = kbase_js_get_slot(kbdev, dep_atom);
1176 if ((js != dep_js) &&
1177 (dep_atom->status !=
1178 KBASE_JD_ATOM_STATE_COMPLETED)
1179 && (dep_atom->status !=
1180 KBASE_JD_ATOM_STATE_HW_COMPLETED)
1181 && (dep_atom->status !=
1182 KBASE_JD_ATOM_STATE_UNUSED)) {
1184 katom->atom_flags |=
1185 KBASE_KATOM_FLAG_X_DEP_BLOCKED;
1186 katom->x_pre_dep = dep_atom;
1187 dep_atom->x_post_dep = katom;
1188 if (kbase_jd_katom_dep_type(
1190 BASE_JD_DEP_TYPE_DATA)
1191 katom->atom_flags |=
1192 KBASE_KATOM_FLAG_FAIL_BLOCKER;
1194 if ((kbase_jd_katom_dep_type(&katom->dep[i])
1195 == BASE_JD_DEP_TYPE_DATA) &&
1197 katom->pre_dep = dep_atom;
1198 dep_atom->post_dep = katom;
1201 list_del(&katom->dep_item[i]);
1202 kbase_jd_katom_dep_clear(&katom->dep[i]);
1210 bool kbasep_js_add_job(struct kbase_context *kctx,
1211 struct kbase_jd_atom *atom)
1213 unsigned long flags;
1214 struct kbasep_js_kctx_info *js_kctx_info;
1215 struct kbase_device *kbdev;
1216 struct kbasep_js_device_data *js_devdata;
1217 union kbasep_js_policy *js_policy;
1219 bool enqueue_required = false;
1220 bool timer_sync = false;
1222 KBASE_DEBUG_ASSERT(kctx != NULL);
1223 KBASE_DEBUG_ASSERT(atom != NULL);
1224 lockdep_assert_held(&kctx->jctx.lock);
1226 kbdev = kctx->kbdev;
1227 js_devdata = &kbdev->js_data;
1228 js_policy = &kbdev->js_data.policy;
1229 js_kctx_info = &kctx->jctx.sched_info;
1231 mutex_lock(&js_devdata->queue_mutex);
1232 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1235 * Begin Runpool transaction
1237 mutex_lock(&js_devdata->runpool_mutex);
1239 /* Refcount ctx.nr_jobs */
1240 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs < U32_MAX);
1241 ++(js_kctx_info->ctx.nr_jobs);
1243 /* Setup any scheduling information */
1244 kbasep_js_clear_job_retry_submit(atom);
1246 /* Lock for state available during IRQ */
1247 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1249 if (!kbase_js_dep_validate(kctx, atom)) {
1250 /* Dependencies could not be represented */
1251 --(js_kctx_info->ctx.nr_jobs);
1253 /* Setting atom status back to queued as it still has unresolved
1255 atom->status = KBASE_JD_ATOM_STATE_QUEUED;
1257 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1258 mutex_unlock(&js_devdata->runpool_mutex);
1263 KBASE_TIMELINE_ATOM_READY(kctx, kbase_jd_atom_id(kctx, atom));
1265 enqueue_required = kbase_js_dep_resolved_submit(kctx, atom);
1267 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_ADD_JOB, kctx, atom, atom->jc,
1268 kbasep_js_trace_get_refcnt_nolock(kbdev, kctx));
1270 /* Context Attribute Refcounting */
1271 kbasep_js_ctx_attr_ctx_retain_atom(kbdev, kctx, atom);
1273 if (enqueue_required) {
1274 if (kbase_js_ctx_pullable(kctx, atom->slot_nr, false))
1275 timer_sync = kbase_js_ctx_list_add_pullable_nolock(
1276 kbdev, kctx, atom->slot_nr);
1278 timer_sync = kbase_js_ctx_list_add_unpullable_nolock(
1279 kbdev, kctx, atom->slot_nr);
1281 /* If this context is active and the atom is the first on its slot,
1282 * kick the job manager to attempt to fast-start the atom */
1283 if (enqueue_required && kctx == kbdev->hwaccess.active_kctx)
1284 kbase_jm_try_kick(kbdev, 1 << atom->slot_nr);
1286 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1288 kbase_backend_ctx_count_changed(kbdev);
1289 mutex_unlock(&js_devdata->runpool_mutex);
1290 /* End runpool transaction */
1292 if (!js_kctx_info->ctx.is_scheduled) {
1293 if (js_kctx_info->ctx.is_dying) {
1294 /* A job got added while/after kbase_job_zap_context()
1295 * was called on a non-scheduled context (e.g. KDS
1296 * dependency resolved). Kill that job by killing the
1298 kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx,
1300 } else if (js_kctx_info->ctx.nr_jobs == 1) {
1301 /* Handle Refcount going from 0 to 1: schedule the
1302 * context on the Policy Queue */
1303 KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1304 dev_dbg(kbdev->dev, "JS: Enqueue Context %p", kctx);
1306 /* Policy Queue was updated - caller must try to
1307 * schedule the head context */
1308 WARN_ON(!enqueue_required);
1312 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1314 mutex_unlock(&js_devdata->queue_mutex);
1316 return enqueue_required;
1319 void kbasep_js_remove_job(struct kbase_device *kbdev,
1320 struct kbase_context *kctx, struct kbase_jd_atom *atom)
1322 struct kbasep_js_kctx_info *js_kctx_info;
1323 struct kbasep_js_device_data *js_devdata;
1324 union kbasep_js_policy *js_policy;
1326 KBASE_DEBUG_ASSERT(kbdev != NULL);
1327 KBASE_DEBUG_ASSERT(kctx != NULL);
1328 KBASE_DEBUG_ASSERT(atom != NULL);
1330 js_devdata = &kbdev->js_data;
1331 js_policy = &kbdev->js_data.policy;
1332 js_kctx_info = &kctx->jctx.sched_info;
1334 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_REMOVE_JOB, kctx, atom, atom->jc,
1335 kbasep_js_trace_get_refcnt(kbdev, kctx));
1337 /* De-refcount ctx.nr_jobs */
1338 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs > 0);
1339 --(js_kctx_info->ctx.nr_jobs);
1342 bool kbasep_js_remove_cancelled_job(struct kbase_device *kbdev,
1343 struct kbase_context *kctx, struct kbase_jd_atom *katom)
1345 unsigned long flags;
1346 struct kbasep_js_atom_retained_state katom_retained_state;
1347 struct kbasep_js_device_data *js_devdata;
1348 bool attr_state_changed;
1350 KBASE_DEBUG_ASSERT(kbdev != NULL);
1351 KBASE_DEBUG_ASSERT(kctx != NULL);
1352 KBASE_DEBUG_ASSERT(katom != NULL);
1354 js_devdata = &kbdev->js_data;
1356 kbasep_js_atom_retained_state_copy(&katom_retained_state, katom);
1357 kbasep_js_remove_job(kbdev, kctx, katom);
1359 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1361 /* The atom has 'finished' (will not be re-run), so no need to call
1362 * kbasep_js_has_atom_finished().
1364 * This is because it returns false for soft-stopped atoms, but we
1365 * want to override that, because we're cancelling an atom regardless of
1366 * whether it was soft-stopped or not */
1367 attr_state_changed = kbasep_js_ctx_attr_ctx_release_atom(kbdev, kctx,
1368 &katom_retained_state);
1370 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1372 return attr_state_changed;
1375 bool kbasep_js_runpool_retain_ctx(struct kbase_device *kbdev,
1376 struct kbase_context *kctx)
1378 unsigned long flags;
1379 struct kbasep_js_device_data *js_devdata;
1382 KBASE_DEBUG_ASSERT(kbdev != NULL);
1383 js_devdata = &kbdev->js_data;
1385 /* KBASE_TRACE_ADD_REFCOUNT( kbdev, JS_RETAIN_CTX, kctx, NULL, 0,
1386 kbasep_js_trace_get_refcnt(kbdev, kctx)); */
1387 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1388 result = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
1389 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1394 struct kbase_context *kbasep_js_runpool_lookup_ctx(struct kbase_device *kbdev,
1397 unsigned long flags;
1398 struct kbasep_js_device_data *js_devdata;
1399 struct kbase_context *found_kctx = NULL;
1400 struct kbasep_js_per_as_data *js_per_as_data;
1402 KBASE_DEBUG_ASSERT(kbdev != NULL);
1403 KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
1404 js_devdata = &kbdev->js_data;
1405 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
1407 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1409 found_kctx = js_per_as_data->kctx;
1411 if (found_kctx != NULL)
1412 ++(js_per_as_data->as_busy_refcount);
1414 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1419 struct kbase_context *kbasep_js_runpool_lookup_ctx_nolock(
1420 struct kbase_device *kbdev, int as_nr)
1422 struct kbasep_js_device_data *js_devdata;
1423 struct kbase_context *found_kctx = NULL;
1424 struct kbasep_js_per_as_data *js_per_as_data;
1426 KBASE_DEBUG_ASSERT(kbdev != NULL);
1427 KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
1429 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1431 js_devdata = &kbdev->js_data;
1432 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
1434 found_kctx = js_per_as_data->kctx;
1436 if (found_kctx != NULL)
1437 ++(js_per_as_data->as_busy_refcount);
1443 * kbasep_js_release_result - Try running more jobs after releasing a context
1446 * @kbdev: The kbase_device to operate on
1447 * @kctx: The kbase_context to operate on
1448 * @katom_retained_state: Retained state from the atom
1449 * @runpool_ctx_attr_change: True if the runpool context attributes have changed
1451 * This collates a set of actions that must happen whilst
1452 * kbasep_js_device_data.runpool_irq.lock is held.
1454 * This includes running more jobs when:
1455 * - The previously released kctx caused a ctx attribute change,
1456 * - The released atom caused a ctx attribute change,
1457 * - Slots were previously blocked due to affinity restrictions,
1458 * - Submission during IRQ handling failed.
1460 * Return: %KBASEP_JS_RELEASE_RESULT_SCHED_ALL if context attributes were
1461 * changed. The caller should try scheduling all contexts
1463 static kbasep_js_release_result kbasep_js_run_jobs_after_ctx_and_atom_release(
1464 struct kbase_device *kbdev,
1465 struct kbase_context *kctx,
1466 struct kbasep_js_atom_retained_state *katom_retained_state,
1467 bool runpool_ctx_attr_change)
1469 struct kbasep_js_device_data *js_devdata;
1470 kbasep_js_release_result result = 0;
1472 KBASE_DEBUG_ASSERT(kbdev != NULL);
1473 KBASE_DEBUG_ASSERT(kctx != NULL);
1474 KBASE_DEBUG_ASSERT(katom_retained_state != NULL);
1475 js_devdata = &kbdev->js_data;
1477 lockdep_assert_held(&kctx->jctx.sched_info.ctx.jsctx_mutex);
1478 lockdep_assert_held(&js_devdata->runpool_mutex);
1479 lockdep_assert_held(&js_devdata->runpool_irq.lock);
1481 if (js_devdata->nr_user_contexts_running != 0) {
1482 bool retry_submit = false;
1483 int retry_jobslot = 0;
1485 if (katom_retained_state)
1486 retry_submit = kbasep_js_get_atom_retry_submit_slot(
1487 katom_retained_state, &retry_jobslot);
1489 if (runpool_ctx_attr_change || retry_submit) {
1490 /* A change in runpool ctx attributes might mean we can
1491 * run more jobs than before */
1492 result = KBASEP_JS_RELEASE_RESULT_SCHED_ALL;
1494 KBASE_TRACE_ADD_SLOT(kbdev, JD_DONE_TRY_RUN_NEXT_JOB,
1495 kctx, NULL, 0u, retry_jobslot);
1502 * Internal function to release the reference on a ctx and an atom's "retained
1503 * state", only taking the runpool and as transaction mutexes
1505 * This also starts more jobs running in the case of an ctx-attribute state
1508 * This does none of the followup actions for scheduling:
1509 * - It does not schedule in a new context
1510 * - It does not requeue or handle dying contexts
1512 * For those tasks, just call kbasep_js_runpool_release_ctx() instead
1515 * - Context is scheduled in, and kctx->as_nr matches kctx_as_nr
1516 * - Context has a non-zero refcount
1517 * - Caller holds js_kctx_info->ctx.jsctx_mutex
1518 * - Caller holds js_devdata->runpool_mutex
1520 static kbasep_js_release_result kbasep_js_runpool_release_ctx_internal(
1521 struct kbase_device *kbdev,
1522 struct kbase_context *kctx,
1523 struct kbasep_js_atom_retained_state *katom_retained_state)
1525 unsigned long flags;
1526 struct kbasep_js_device_data *js_devdata;
1527 struct kbasep_js_kctx_info *js_kctx_info;
1528 union kbasep_js_policy *js_policy;
1529 struct kbasep_js_per_as_data *js_per_as_data;
1531 kbasep_js_release_result release_result = 0u;
1532 bool runpool_ctx_attr_change = false;
1534 struct kbase_as *current_as;
1537 KBASE_DEBUG_ASSERT(kbdev != NULL);
1538 KBASE_DEBUG_ASSERT(kctx != NULL);
1539 js_kctx_info = &kctx->jctx.sched_info;
1540 js_devdata = &kbdev->js_data;
1541 js_policy = &kbdev->js_data.policy;
1543 /* Ensure context really is scheduled in */
1544 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
1546 /* kctx->as_nr and js_per_as_data are only read from here. The caller's
1547 * js_ctx_mutex provides a barrier that ensures they are up-to-date.
1549 * They will not change whilst we're reading them, because the refcount
1550 * is non-zero (and we ASSERT on that last fact).
1552 kctx_as_nr = kctx->as_nr;
1553 KBASE_DEBUG_ASSERT(kctx_as_nr != KBASEP_AS_NR_INVALID);
1554 js_per_as_data = &js_devdata->runpool_irq.per_as_data[kctx_as_nr];
1555 KBASE_DEBUG_ASSERT(js_per_as_data->as_busy_refcount > 0);
1558 * Transaction begins on AS and runpool_irq
1560 * Assert about out calling contract
1562 current_as = &kbdev->as[kctx_as_nr];
1563 mutex_lock(&kbdev->pm.lock);
1564 mutex_lock(¤t_as->transaction_mutex);
1565 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1566 KBASE_DEBUG_ASSERT(kctx_as_nr == kctx->as_nr);
1567 KBASE_DEBUG_ASSERT(js_per_as_data->as_busy_refcount > 0);
1569 /* Update refcount */
1570 new_ref_count = --(js_per_as_data->as_busy_refcount);
1572 /* Release the atom if it finished (i.e. wasn't soft-stopped) */
1573 if (kbasep_js_has_atom_finished(katom_retained_state))
1574 runpool_ctx_attr_change |= kbasep_js_ctx_attr_ctx_release_atom(
1575 kbdev, kctx, katom_retained_state);
1577 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RELEASE_CTX, kctx, NULL, 0u,
1580 if (new_ref_count == 1 && kctx->jctx.sched_info.ctx.flags &
1581 KBASE_CTX_FLAG_PRIVILEGED &&
1582 !kbase_pm_is_suspending(kbdev)) {
1583 /* Context is kept scheduled into an address space even when
1584 * there are no jobs, in this case we have to handle the
1585 * situation where all jobs have been evicted from the GPU and
1586 * submission is disabled.
1588 * At this point we re-enable submission to allow further jobs
1591 kbasep_js_set_submit_allowed(js_devdata, kctx);
1594 /* Make a set of checks to see if the context should be scheduled out */
1595 if (new_ref_count == 0 &&
1596 (!kbasep_js_is_submit_allowed(js_devdata, kctx) ||
1597 kbdev->pm.suspending)) {
1598 /* Last reference, and we've been told to remove this context
1599 * from the Run Pool */
1600 dev_dbg(kbdev->dev, "JS: RunPool Remove Context %p because as_busy_refcount=%d, jobs=%d, allowed=%d",
1601 kctx, new_ref_count, js_kctx_info->ctx.nr_jobs,
1602 kbasep_js_is_submit_allowed(js_devdata, kctx));
1604 #if defined(CONFIG_MALI_GATOR_SUPPORT)
1605 kbase_trace_mali_mmu_as_released(kctx->as_nr);
1607 kbase_tlstream_tl_nret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
1609 kbase_backend_release_ctx_irq(kbdev, kctx);
1611 if (kbdev->hwaccess.active_kctx == kctx)
1612 kbdev->hwaccess.active_kctx = NULL;
1614 /* Ctx Attribute handling
1616 * Releasing atoms attributes must either happen before this, or
1617 * after 'is_scheduled' is changed, otherwise we double-decount
1619 runpool_ctx_attr_change |=
1620 kbasep_js_ctx_attr_runpool_release_ctx(kbdev, kctx);
1622 /* Releasing the context and katom retained state can allow
1623 * more jobs to run */
1625 kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev,
1626 kctx, katom_retained_state,
1627 runpool_ctx_attr_change);
1630 * Transaction ends on AS and runpool_irq:
1632 * By this point, the AS-related data is now clear and ready
1635 * Since releases only occur once for each previous successful
1636 * retain, and no more retains are allowed on this context, no
1637 * other thread will be operating in this
1638 * code whilst we are
1640 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1642 kbase_backend_release_ctx_noirq(kbdev, kctx);
1644 mutex_unlock(¤t_as->transaction_mutex);
1645 mutex_unlock(&kbdev->pm.lock);
1647 /* Note: Don't reuse kctx_as_nr now */
1649 /* Synchronize with any policy timers */
1650 kbase_backend_ctx_count_changed(kbdev);
1652 /* update book-keeping info */
1653 js_kctx_info->ctx.is_scheduled = false;
1654 /* Signal any waiter that the context is not scheduled, so is
1655 * safe for termination - once the jsctx_mutex is also dropped,
1656 * and jobs have finished. */
1657 wake_up(&js_kctx_info->ctx.is_scheduled_wait);
1659 /* Queue an action to occur after we've dropped the lock */
1660 release_result |= KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED |
1661 KBASEP_JS_RELEASE_RESULT_SCHED_ALL;
1663 kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev, kctx,
1664 katom_retained_state, runpool_ctx_attr_change);
1666 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1667 mutex_unlock(¤t_as->transaction_mutex);
1668 mutex_unlock(&kbdev->pm.lock);
1671 return release_result;
1674 void kbasep_js_runpool_release_ctx_nolock(struct kbase_device *kbdev,
1675 struct kbase_context *kctx)
1677 struct kbasep_js_atom_retained_state katom_retained_state;
1679 /* Setup a dummy katom_retained_state */
1680 kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
1682 kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1683 &katom_retained_state);
1686 void kbasep_js_runpool_requeue_or_kill_ctx(struct kbase_device *kbdev,
1687 struct kbase_context *kctx, bool has_pm_ref)
1689 struct kbasep_js_device_data *js_devdata;
1690 union kbasep_js_policy *js_policy;
1691 struct kbasep_js_kctx_info *js_kctx_info;
1693 KBASE_DEBUG_ASSERT(kbdev != NULL);
1694 KBASE_DEBUG_ASSERT(kctx != NULL);
1695 js_kctx_info = &kctx->jctx.sched_info;
1696 js_policy = &kbdev->js_data.policy;
1697 js_devdata = &kbdev->js_data;
1699 /* This is called if and only if you've you've detached the context from
1700 * the Runpool or the Policy Queue, and not added it back to the Runpool
1702 KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1704 if (js_kctx_info->ctx.is_dying) {
1705 /* Dying: don't requeue, but kill all jobs on the context. This
1706 * happens asynchronously */
1708 "JS: ** Killing Context %p on RunPool Remove **", kctx);
1709 kbase_js_foreach_ctx_job(kctx, &kbase_jd_cancel);
1713 void kbasep_js_runpool_release_ctx_and_katom_retained_state(
1714 struct kbase_device *kbdev, struct kbase_context *kctx,
1715 struct kbasep_js_atom_retained_state *katom_retained_state)
1717 struct kbasep_js_device_data *js_devdata;
1718 struct kbasep_js_kctx_info *js_kctx_info;
1719 kbasep_js_release_result release_result;
1721 KBASE_DEBUG_ASSERT(kbdev != NULL);
1722 KBASE_DEBUG_ASSERT(kctx != NULL);
1723 js_kctx_info = &kctx->jctx.sched_info;
1724 js_devdata = &kbdev->js_data;
1726 mutex_lock(&js_devdata->queue_mutex);
1727 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1728 mutex_lock(&js_devdata->runpool_mutex);
1730 release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1731 katom_retained_state);
1733 /* Drop the runpool mutex to allow requeing kctx */
1734 mutex_unlock(&js_devdata->runpool_mutex);
1736 if ((release_result & KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED) != 0u)
1737 kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, true);
1739 /* Drop the jsctx_mutex to allow scheduling in a new context */
1741 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1742 mutex_unlock(&js_devdata->queue_mutex);
1744 if (release_result & KBASEP_JS_RELEASE_RESULT_SCHED_ALL)
1745 kbase_js_sched_all(kbdev);
1748 void kbasep_js_runpool_release_ctx(struct kbase_device *kbdev,
1749 struct kbase_context *kctx)
1751 struct kbasep_js_atom_retained_state katom_retained_state;
1753 kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
1755 kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
1756 &katom_retained_state);
1759 /* Variant of kbasep_js_runpool_release_ctx() that doesn't call into
1760 * kbase_js_sched_all() */
1761 static void kbasep_js_runpool_release_ctx_no_schedule(
1762 struct kbase_device *kbdev, struct kbase_context *kctx)
1764 struct kbasep_js_device_data *js_devdata;
1765 struct kbasep_js_kctx_info *js_kctx_info;
1766 kbasep_js_release_result release_result;
1767 struct kbasep_js_atom_retained_state katom_retained_state_struct;
1768 struct kbasep_js_atom_retained_state *katom_retained_state =
1769 &katom_retained_state_struct;
1771 KBASE_DEBUG_ASSERT(kbdev != NULL);
1772 KBASE_DEBUG_ASSERT(kctx != NULL);
1773 js_kctx_info = &kctx->jctx.sched_info;
1774 js_devdata = &kbdev->js_data;
1775 kbasep_js_atom_retained_state_init_invalid(katom_retained_state);
1777 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1778 mutex_lock(&js_devdata->runpool_mutex);
1780 release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1781 katom_retained_state);
1783 /* Drop the runpool mutex to allow requeing kctx */
1784 mutex_unlock(&js_devdata->runpool_mutex);
1785 if ((release_result & KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED) != 0u)
1786 kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, true);
1788 /* Drop the jsctx_mutex to allow scheduling in a new context */
1789 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1791 /* NOTE: could return release_result if the caller would like to know
1792 * whether it should schedule a new context, but currently no callers do
1796 void kbase_js_set_timeouts(struct kbase_device *kbdev)
1798 lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1800 kbase_backend_timeouts_changed(kbdev);
1803 static bool kbasep_js_schedule_ctx(struct kbase_device *kbdev,
1804 struct kbase_context *kctx)
1806 struct kbasep_js_device_data *js_devdata;
1807 struct kbasep_js_kctx_info *js_kctx_info;
1808 union kbasep_js_policy *js_policy;
1809 struct kbase_as *new_address_space = NULL;
1810 unsigned long flags;
1811 bool kctx_suspended = false;
1814 js_devdata = &kbdev->js_data;
1815 js_policy = &kbdev->js_data.policy;
1816 js_kctx_info = &kctx->jctx.sched_info;
1818 /* Pick available address space for this context */
1819 as_nr = kbase_backend_find_free_address_space(kbdev, kctx);
1821 if (as_nr == KBASEP_AS_NR_INVALID)
1822 return false; /* No address spaces currently available */
1824 new_address_space = &kbdev->as[as_nr];
1827 * Atomic transaction on the Context and Run Pool begins
1829 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1830 mutex_lock(&js_devdata->runpool_mutex);
1832 /* Check to see if context is dying due to kbase_job_zap_context() */
1833 if (js_kctx_info->ctx.is_dying) {
1834 /* Roll back the transaction so far and return */
1835 kbase_backend_release_free_address_space(kbdev, as_nr);
1837 mutex_unlock(&js_devdata->runpool_mutex);
1838 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1843 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_TRY_SCHEDULE_HEAD_CTX, kctx, NULL,
1845 kbasep_js_trace_get_refcnt(kbdev, kctx));
1847 js_kctx_info->ctx.is_scheduled = true;
1849 mutex_lock(&new_address_space->transaction_mutex);
1850 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1852 /* Assign context to previously chosen address space */
1853 if (!kbase_backend_use_ctx(kbdev, kctx, as_nr)) {
1854 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1855 mutex_unlock(&new_address_space->transaction_mutex);
1856 /* Roll back the transaction so far and return */
1857 js_kctx_info->ctx.is_scheduled = false;
1859 kbase_backend_release_free_address_space(kbdev, as_nr);
1861 mutex_unlock(&js_devdata->runpool_mutex);
1863 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1867 kbdev->hwaccess.active_kctx = kctx;
1869 #if defined(CONFIG_MALI_GATOR_SUPPORT)
1870 kbase_trace_mali_mmu_as_in_use(kctx->as_nr);
1872 kbase_tlstream_tl_ret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
1874 /* Cause any future waiter-on-termination to wait until the context is
1876 wake_up(&js_kctx_info->ctx.is_scheduled_wait);
1878 /* Re-check for suspending: a suspend could've occurred, and all the
1879 * contexts could've been removed from the runpool before we took this
1880 * lock. In this case, we don't want to allow this context to run jobs,
1881 * we just want it out immediately.
1883 * The DMB required to read the suspend flag was issued recently as part
1884 * of the runpool_irq locking. If a suspend occurs *after* that lock was
1885 * taken (i.e. this condition doesn't execute), then the
1886 * kbasep_js_suspend() code will cleanup this context instead (by virtue
1887 * of it being called strictly after the suspend flag is set, and will
1888 * wait for this lock to drop) */
1889 if (kbase_pm_is_suspending(kbdev)) {
1890 /* Cause it to leave at some later point */
1893 retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
1894 KBASE_DEBUG_ASSERT(retained);
1896 kbasep_js_clear_submit_allowed(js_devdata, kctx);
1897 kctx_suspended = true;
1900 /* Transaction complete */
1901 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1902 mutex_unlock(&new_address_space->transaction_mutex);
1904 /* Synchronize with any policy timers */
1905 kbase_backend_ctx_count_changed(kbdev);
1907 mutex_unlock(&js_devdata->runpool_mutex);
1908 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1909 /* Note: after this point, the context could potentially get scheduled
1910 * out immediately */
1912 if (kctx_suspended) {
1913 /* Finishing forcing out the context due to a suspend. Use a
1914 * variant of kbasep_js_runpool_release_ctx() that doesn't
1915 * schedule a new context, to prevent a risk of recursion back
1916 * into this function */
1917 kbasep_js_runpool_release_ctx_no_schedule(kbdev, kctx);
1923 static bool kbase_js_use_ctx(struct kbase_device *kbdev,
1924 struct kbase_context *kctx)
1926 struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
1927 unsigned long flags;
1929 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1930 if (kbase_backend_use_ctx_sched(kbdev, kctx)) {
1931 /* Context already has ASID - mark as active */
1932 kbdev->hwaccess.active_kctx = kctx;
1934 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1935 return true; /* Context already scheduled */
1937 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1939 return kbasep_js_schedule_ctx(kbdev, kctx);
1942 void kbasep_js_schedule_privileged_ctx(struct kbase_device *kbdev,
1943 struct kbase_context *kctx)
1945 struct kbasep_js_kctx_info *js_kctx_info;
1946 struct kbasep_js_device_data *js_devdata;
1949 KBASE_DEBUG_ASSERT(kbdev != NULL);
1950 KBASE_DEBUG_ASSERT(kctx != NULL);
1952 js_devdata = &kbdev->js_data;
1953 js_kctx_info = &kctx->jctx.sched_info;
1955 /* This must never be attempted whilst suspending - i.e. it should only
1956 * happen in response to a syscall from a user-space thread */
1957 BUG_ON(kbase_pm_is_suspending(kbdev));
1959 mutex_lock(&js_devdata->queue_mutex);
1960 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1962 /* Mark the context as privileged */
1963 js_kctx_info->ctx.flags |= KBASE_CTX_FLAG_PRIVILEGED;
1965 is_scheduled = js_kctx_info->ctx.is_scheduled;
1966 if (!is_scheduled) {
1967 /* Add the context to the pullable list */
1968 if (kbase_js_ctx_list_add_pullable_head(kbdev, kctx, 0))
1969 kbase_js_sync_timers(kbdev);
1971 /* Fast-starting requires the jsctx_mutex to be dropped,
1972 * because it works on multiple ctxs */
1973 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1974 mutex_unlock(&js_devdata->queue_mutex);
1976 /* Try to schedule the context in */
1977 kbase_js_sched_all(kbdev);
1979 /* Wait for the context to be scheduled in */
1980 wait_event(kctx->jctx.sched_info.ctx.is_scheduled_wait,
1981 kctx->jctx.sched_info.ctx.is_scheduled);
1983 /* Already scheduled in - We need to retain it to keep the
1984 * corresponding address space */
1985 kbasep_js_runpool_retain_ctx(kbdev, kctx);
1986 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1987 mutex_unlock(&js_devdata->queue_mutex);
1990 KBASE_EXPORT_TEST_API(kbasep_js_schedule_privileged_ctx);
1992 void kbasep_js_release_privileged_ctx(struct kbase_device *kbdev,
1993 struct kbase_context *kctx)
1995 struct kbasep_js_kctx_info *js_kctx_info;
1997 KBASE_DEBUG_ASSERT(kctx != NULL);
1998 js_kctx_info = &kctx->jctx.sched_info;
2000 /* We don't need to use the address space anymore */
2001 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
2002 js_kctx_info->ctx.flags &= (~KBASE_CTX_FLAG_PRIVILEGED);
2003 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2005 /* Release the context - it will be scheduled out */
2006 kbasep_js_runpool_release_ctx(kbdev, kctx);
2008 kbase_js_sched_all(kbdev);
2010 KBASE_EXPORT_TEST_API(kbasep_js_release_privileged_ctx);
2012 void kbasep_js_suspend(struct kbase_device *kbdev)
2014 unsigned long flags;
2015 struct kbasep_js_device_data *js_devdata;
2018 int nr_privileged_ctx = 0;
2020 KBASE_DEBUG_ASSERT(kbdev);
2021 KBASE_DEBUG_ASSERT(kbase_pm_is_suspending(kbdev));
2022 js_devdata = &kbdev->js_data;
2024 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2026 /* Prevent all contexts from submitting */
2027 js_devdata->runpool_irq.submit_allowed = 0;
2029 /* Retain each of the contexts, so we can cause it to leave even if it
2030 * had no refcount to begin with */
2031 for (i = BASE_MAX_NR_AS - 1; i >= 0; --i) {
2032 struct kbasep_js_per_as_data *js_per_as_data =
2033 &js_devdata->runpool_irq.per_as_data[i];
2034 struct kbase_context *kctx = js_per_as_data->kctx;
2036 retained = retained << 1;
2039 ++(js_per_as_data->as_busy_refcount);
2041 /* We can only cope with up to 1 privileged context -
2042 * the instrumented context. It'll be suspended by
2043 * disabling instrumentation */
2044 if (kctx->jctx.sched_info.ctx.flags &
2045 KBASE_CTX_FLAG_PRIVILEGED) {
2046 ++nr_privileged_ctx;
2047 WARN_ON(nr_privileged_ctx != 1);
2051 CSTD_UNUSED(nr_privileged_ctx);
2052 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2054 /* De-ref the previous retain to ensure each context gets pulled out
2055 * sometime later. */
2058 ++i, retained = retained >> 1) {
2059 struct kbasep_js_per_as_data *js_per_as_data =
2060 &js_devdata->runpool_irq.per_as_data[i];
2061 struct kbase_context *kctx = js_per_as_data->kctx;
2064 kbasep_js_runpool_release_ctx(kbdev, kctx);
2067 /* Caller must wait for all Power Manager active references to be
2071 void kbasep_js_resume(struct kbase_device *kbdev)
2073 struct kbasep_js_device_data *js_devdata;
2076 KBASE_DEBUG_ASSERT(kbdev);
2077 js_devdata = &kbdev->js_data;
2078 KBASE_DEBUG_ASSERT(!kbase_pm_is_suspending(kbdev));
2080 mutex_lock(&js_devdata->queue_mutex);
2081 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2082 struct kbase_context *kctx, *n;
2084 list_for_each_entry_safe(kctx, n,
2085 &kbdev->js_data.ctx_list_unpullable[js],
2086 jctx.sched_info.ctx.ctx_list_entry[js]) {
2087 struct kbasep_js_kctx_info *js_kctx_info;
2088 unsigned long flags;
2089 bool timer_sync = false;
2091 js_kctx_info = &kctx->jctx.sched_info;
2093 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
2094 mutex_lock(&js_devdata->runpool_mutex);
2095 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2097 if (!js_kctx_info->ctx.is_scheduled &&
2098 kbase_js_ctx_pullable(kctx, js, false))
2100 kbase_js_ctx_list_add_pullable_nolock(
2103 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
2106 kbase_backend_ctx_count_changed(kbdev);
2107 mutex_unlock(&js_devdata->runpool_mutex);
2108 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2111 mutex_unlock(&js_devdata->queue_mutex);
2113 /* Restart atom processing */
2114 kbase_js_sched_all(kbdev);
2116 /* JS Resume complete */
2119 bool kbase_js_is_atom_valid(struct kbase_device *kbdev,
2120 struct kbase_jd_atom *katom)
2122 if ((katom->core_req & BASE_JD_REQ_FS) &&
2123 (katom->core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE |
2127 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987) &&
2128 (katom->core_req & BASE_JD_REQ_ONLY_COMPUTE) &&
2129 (katom->core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_T)))
2135 static int kbase_js_get_slot(struct kbase_device *kbdev,
2136 struct kbase_jd_atom *katom)
2138 if (katom->core_req & BASE_JD_REQ_FS)
2141 if (katom->core_req & BASE_JD_REQ_ONLY_COMPUTE) {
2142 if (katom->device_nr == 1 &&
2143 kbdev->gpu_props.num_core_groups == 2)
2145 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987))
2152 bool kbase_js_dep_resolved_submit(struct kbase_context *kctx,
2153 struct kbase_jd_atom *katom)
2155 bool enqueue_required;
2157 katom->slot_nr = kbase_js_get_slot(kctx->kbdev, katom);
2159 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2160 lockdep_assert_held(&kctx->jctx.lock);
2162 /* If slot will transition from unpullable to pullable then add to
2164 if (jsctx_rb_none_to_pull(kctx, katom->slot_nr)) {
2165 enqueue_required = true;
2167 enqueue_required = false;
2169 /* Check if there are lower priority jobs to soft stop */
2170 kbase_job_slot_ctx_priority_check_locked(kctx, katom);
2172 if ((katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED) ||
2173 (katom->pre_dep && (katom->pre_dep->atom_flags &
2174 KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST))) {
2175 int prio = katom->sched_priority;
2176 int js = katom->slot_nr;
2177 struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
2179 list_add_tail(&katom->queue, &queue->x_dep_head);
2180 katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST;
2181 enqueue_required = false;
2183 /* Add atom to ring buffer. */
2184 jsctx_tree_add(kctx, katom);
2185 katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_IN_TREE;
2188 return enqueue_required;
2192 * kbase_js_move_to_tree - Move atom (and any dependent atoms) to the
2193 * runnable_tree, ready for execution
2194 * @katom: Atom to submit
2196 * It is assumed that @katom does not have KBASE_KATOM_FLAG_X_DEP_BLOCKED set,
2197 * but is still present in the x_dep list. If @katom has a same-slot dependent
2198 * atom then that atom (and any dependents) will also be moved.
2200 static void kbase_js_move_to_tree(struct kbase_jd_atom *katom)
2202 lockdep_assert_held(&katom->kctx->kbdev->js_data.runpool_irq.lock);
2205 WARN_ON(!(katom->atom_flags &
2206 KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST));
2208 if (!(katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED)) {
2209 list_del(&katom->queue);
2210 katom->atom_flags &=
2211 ~KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST;
2212 jsctx_tree_add(katom->kctx, katom);
2213 katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_IN_TREE;
2218 katom = katom->post_dep;
2224 * kbase_js_evict_deps - Evict dependencies of a failed atom.
2225 * @kctx: Context pointer
2226 * @katom: Pointer to the atom that has failed.
2227 * @js: The job slot the katom was run on.
2228 * @prio: Priority of the katom.
2230 * Remove all post dependencies of an atom from the context ringbuffers.
2232 * The original atom's event_code will be propogated to all dependent atoms.
2234 * Context: Caller must hold the HW access lock
2236 static void kbase_js_evict_deps(struct kbase_context *kctx,
2237 struct kbase_jd_atom *katom, int js, int prio)
2239 struct kbase_jd_atom *x_dep = katom->x_post_dep;
2240 struct kbase_jd_atom *next_katom = katom->post_dep;
2242 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2245 KBASE_DEBUG_ASSERT(next_katom->status !=
2246 KBASE_JD_ATOM_STATE_HW_COMPLETED);
2247 next_katom->will_fail_event_code = katom->event_code;
2251 /* Has cross slot depenency. */
2252 if (x_dep && (x_dep->atom_flags & (KBASE_KATOM_FLAG_JSCTX_IN_TREE |
2253 KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST))) {
2254 /* Remove dependency.*/
2255 x_dep->atom_flags &= ~KBASE_KATOM_FLAG_X_DEP_BLOCKED;
2257 /* Fail if it had a data dependency. */
2258 if (x_dep->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) {
2259 x_dep->will_fail_event_code = katom->event_code;
2261 if (x_dep->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST)
2262 kbase_js_move_to_tree(x_dep);
2266 struct kbase_jd_atom *kbase_js_pull(struct kbase_context *kctx, int js)
2268 struct kbase_jd_atom *katom;
2269 struct kbasep_js_device_data *js_devdata;
2272 KBASE_DEBUG_ASSERT(kctx);
2274 js_devdata = &kctx->kbdev->js_data;
2275 lockdep_assert_held(&js_devdata->runpool_irq.lock);
2277 if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
2279 if (kbase_pm_is_suspending(kctx->kbdev))
2282 katom = jsctx_rb_peek(kctx, js);
2286 if (atomic_read(&katom->blocked))
2289 /* Due to ordering restrictions when unpulling atoms on failure, we do
2290 * not allow multiple runs of fail-dep atoms from the same context to be
2291 * present on the same slot */
2292 if (katom->pre_dep && atomic_read(&kctx->atoms_pulled_slot[js])) {
2293 struct kbase_jd_atom *prev_atom =
2294 kbase_backend_inspect_tail(kctx->kbdev, js);
2296 if (prev_atom && prev_atom->kctx != kctx)
2300 if (katom->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED) {
2301 if (katom->x_pre_dep->gpu_rb_state ==
2302 KBASE_ATOM_GPU_RB_NOT_IN_SLOT_RB ||
2303 katom->x_pre_dep->will_fail_event_code)
2305 if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
2306 kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
2310 kctx->pulled = true;
2311 pulled = atomic_inc_return(&kctx->atoms_pulled);
2312 if (pulled == 1 && !kctx->slots_pullable) {
2313 WARN_ON(kctx->ctx_runnable_ref);
2314 kctx->ctx_runnable_ref = true;
2315 atomic_inc(&kctx->kbdev->js_data.nr_contexts_runnable);
2317 atomic_inc(&kctx->atoms_pulled_slot[katom->slot_nr]);
2318 jsctx_rb_pull(kctx, katom);
2320 kbasep_js_runpool_retain_ctx_nolock(kctx->kbdev, kctx);
2321 katom->atom_flags |= KBASE_KATOM_FLAG_HOLDING_CTX_REF;
2323 katom->sched_info.cfs.ticks = 0;
2329 static void js_return_worker(struct work_struct *data)
2331 struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom,
2333 struct kbase_context *kctx = katom->kctx;
2334 struct kbase_device *kbdev = kctx->kbdev;
2335 struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
2336 struct kbasep_js_kctx_info *js_kctx_info = &kctx->jctx.sched_info;
2337 struct kbasep_js_atom_retained_state retained_state;
2338 int js = katom->slot_nr;
2339 bool timer_sync = false;
2340 bool context_idle = false;
2341 unsigned long flags;
2342 base_jd_core_req core_req = katom->core_req;
2343 u64 affinity = katom->affinity;
2344 enum kbase_atom_coreref_state coreref_state = katom->coreref_state;
2346 kbase_tlstream_aux_job_softstop_ex(katom);
2348 kbase_backend_complete_wq(kbdev, katom);
2350 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
2351 kbase_as_poking_timer_release_atom(kbdev, kctx, katom);
2353 kbasep_js_atom_retained_state_copy(&retained_state, katom);
2355 mutex_lock(&js_devdata->queue_mutex);
2356 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
2358 atomic_dec(&kctx->atoms_pulled);
2359 atomic_dec(&kctx->atoms_pulled_slot[js]);
2361 atomic_dec(&katom->blocked);
2363 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2365 if (!atomic_read(&kctx->atoms_pulled_slot[js]) &&
2366 jsctx_rb_none_to_pull(kctx, js))
2367 timer_sync |= kbase_js_ctx_list_remove_nolock(kbdev, kctx, js);
2369 if (!atomic_read(&kctx->atoms_pulled)) {
2370 if (!kctx->slots_pullable) {
2371 WARN_ON(!kctx->ctx_runnable_ref);
2372 kctx->ctx_runnable_ref = false;
2373 atomic_dec(&kbdev->js_data.nr_contexts_runnable);
2377 if (kctx->as_nr != KBASEP_AS_NR_INVALID &&
2378 !js_kctx_info->ctx.is_dying) {
2379 int num_slots = kbdev->gpu_props.num_job_slots;
2382 if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
2383 kbasep_js_set_submit_allowed(js_devdata, kctx);
2385 for (slot = 0; slot < num_slots; slot++) {
2386 if (kbase_js_ctx_pullable(kctx, slot, true))
2388 kbase_js_ctx_list_add_pullable_nolock(
2393 kbase_jm_idle_ctx(kbdev, kctx);
2395 context_idle = true;
2398 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2401 WARN_ON(!kctx->ctx_active);
2402 kctx->ctx_active = false;
2403 kbase_pm_context_idle(kbdev);
2407 kbase_js_sync_timers(kbdev);
2409 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2410 mutex_unlock(&js_devdata->queue_mutex);
2412 katom->atom_flags &= ~KBASE_KATOM_FLAG_HOLDING_CTX_REF;
2413 kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
2416 kbase_js_sched_all(kbdev);
2418 kbase_backend_complete_wq_post_sched(kbdev, core_req, affinity,
2422 void kbase_js_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
2424 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2426 jsctx_rb_unpull(kctx, katom);
2428 WARN_ON(work_pending(&katom->work));
2430 /* Block re-submission until workqueue has run */
2431 atomic_inc(&katom->blocked);
2433 kbase_job_check_leave_disjoint(kctx->kbdev, katom);
2435 KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
2436 INIT_WORK(&katom->work, js_return_worker);
2437 queue_work(kctx->jctx.job_done_wq, &katom->work);
2440 bool kbase_js_complete_atom_wq(struct kbase_context *kctx,
2441 struct kbase_jd_atom *katom)
2443 struct kbasep_js_kctx_info *js_kctx_info;
2444 struct kbasep_js_device_data *js_devdata;
2445 struct kbase_device *kbdev;
2446 unsigned long flags;
2447 bool timer_sync = false;
2449 bool context_idle = false;
2451 kbdev = kctx->kbdev;
2452 atom_slot = katom->slot_nr;
2454 js_kctx_info = &kctx->jctx.sched_info;
2455 js_devdata = &kbdev->js_data;
2457 lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
2459 mutex_lock(&js_devdata->runpool_mutex);
2460 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2462 if (katom->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_TREE) {
2463 context_idle = !atomic_dec_return(&kctx->atoms_pulled);
2464 atomic_dec(&kctx->atoms_pulled_slot[atom_slot]);
2466 if (!atomic_read(&kctx->atoms_pulled) &&
2467 !kctx->slots_pullable) {
2468 WARN_ON(!kctx->ctx_runnable_ref);
2469 kctx->ctx_runnable_ref = false;
2470 atomic_dec(&kbdev->js_data.nr_contexts_runnable);
2474 WARN_ON(!(katom->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_TREE));
2476 if (!atomic_read(&kctx->atoms_pulled_slot[atom_slot]) &&
2477 jsctx_rb_none_to_pull(kctx, atom_slot)) {
2479 &kctx->jctx.sched_info.ctx.ctx_list_entry[atom_slot]))
2480 timer_sync |= kbase_js_ctx_list_remove_nolock(
2481 kctx->kbdev, kctx, atom_slot);
2485 * If submission is disabled on this context (most likely due to an
2486 * atom failure) and there are now no atoms left in the system then
2487 * re-enable submission so that context can be scheduled again.
2489 if (!kbasep_js_is_submit_allowed(js_devdata, kctx) &&
2490 !atomic_read(&kctx->atoms_pulled) &&
2491 !js_kctx_info->ctx.is_dying) {
2494 kbasep_js_set_submit_allowed(js_devdata, kctx);
2496 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2497 if (kbase_js_ctx_pullable(kctx, js, true))
2499 kbase_js_ctx_list_add_pullable_nolock(
2502 } else if (katom->x_post_dep &&
2503 kbasep_js_is_submit_allowed(js_devdata, kctx)) {
2506 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2507 if (kbase_js_ctx_pullable(kctx, js, true))
2509 kbase_js_ctx_list_add_pullable_nolock(
2514 /* Mark context as inactive. The pm reference will be dropped later in
2518 kctx->ctx_active = false;
2520 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2522 kbase_backend_ctx_count_changed(kbdev);
2523 mutex_unlock(&js_devdata->runpool_mutex);
2525 return context_idle;
2528 void kbase_js_complete_atom(struct kbase_jd_atom *katom, ktime_t *end_timestamp)
2530 u64 microseconds_spent = 0;
2531 struct kbase_device *kbdev;
2532 struct kbase_context *kctx = katom->kctx;
2533 union kbasep_js_policy *js_policy;
2534 struct kbase_jd_atom *x_dep = katom->x_post_dep;
2536 kbdev = kctx->kbdev;
2538 js_policy = &kbdev->js_data.policy;
2540 lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2542 if (katom->will_fail_event_code)
2543 katom->event_code = katom->will_fail_event_code;
2545 katom->status = KBASE_JD_ATOM_STATE_HW_COMPLETED;
2547 if (katom->event_code != BASE_JD_EVENT_DONE) {
2548 kbase_js_evict_deps(kctx, katom, katom->slot_nr,
2549 katom->sched_priority);
2552 #if defined(CONFIG_MALI_GATOR_SUPPORT)
2553 kbase_trace_mali_job_slots_event(GATOR_MAKE_EVENT(GATOR_JOB_SLOT_STOP,
2554 katom->slot_nr), NULL, 0);
2557 /* Calculate the job's time used */
2558 if (end_timestamp != NULL) {
2559 /* Only calculating it for jobs that really run on the HW (e.g.
2560 * removed from next jobs never actually ran, so really did take
2562 ktime_t tick_diff = ktime_sub(*end_timestamp,
2563 katom->start_timestamp);
2565 microseconds_spent = ktime_to_ns(tick_diff);
2567 do_div(microseconds_spent, 1000);
2569 /* Round up time spent to the minimum timer resolution */
2570 if (microseconds_spent < KBASEP_JS_TICK_RESOLUTION_US)
2571 microseconds_spent = KBASEP_JS_TICK_RESOLUTION_US;
2574 /* Log the result of the job (completion status, and time spent). */
2575 kbasep_js_policy_log_job_result(js_policy, katom, microseconds_spent);
2577 kbase_jd_done(katom, katom->slot_nr, end_timestamp, 0);
2579 /* Unblock cross dependency if present */
2580 if (x_dep && (katom->event_code == BASE_JD_EVENT_DONE ||
2581 !(x_dep->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER)) &&
2582 (x_dep->atom_flags & KBASE_KATOM_FLAG_X_DEP_BLOCKED)) {
2583 bool was_pullable = kbase_js_ctx_pullable(kctx, x_dep->slot_nr,
2585 x_dep->atom_flags &= ~KBASE_KATOM_FLAG_X_DEP_BLOCKED;
2586 kbase_js_move_to_tree(x_dep);
2587 if (!was_pullable && kbase_js_ctx_pullable(kctx, x_dep->slot_nr,
2589 kbase_js_ctx_list_add_pullable_nolock(kbdev, kctx,
2594 void kbase_js_sched(struct kbase_device *kbdev, int js_mask)
2596 struct kbasep_js_device_data *js_devdata;
2597 bool timer_sync = false;
2599 js_devdata = &kbdev->js_data;
2601 down(&js_devdata->schedule_sem);
2602 mutex_lock(&js_devdata->queue_mutex);
2607 js = ffs(js_mask) - 1;
2610 struct kbase_context *kctx;
2611 unsigned long flags;
2612 bool context_idle = false;
2614 kctx = kbase_js_ctx_list_pop_head(kbdev, js);
2617 js_mask &= ~(1 << js);
2618 break; /* No contexts on pullable list */
2621 if (!kctx->ctx_active) {
2622 context_idle = true;
2624 if (kbase_pm_context_active_handle_suspend(
2626 KBASE_PM_SUSPEND_HANDLER_DONT_INCREASE)) {
2627 /* Suspend pending - return context to
2628 * queue and stop scheduling */
2630 &kctx->jctx.sched_info.ctx.jsctx_mutex);
2631 if (kbase_js_ctx_list_add_pullable_head(
2632 kctx->kbdev, kctx, js))
2633 kbase_js_sync_timers(kbdev);
2635 &kctx->jctx.sched_info.ctx.jsctx_mutex);
2636 mutex_unlock(&js_devdata->queue_mutex);
2637 up(&js_devdata->schedule_sem);
2640 kctx->ctx_active = true;
2643 if (!kbase_js_use_ctx(kbdev, kctx)) {
2645 &kctx->jctx.sched_info.ctx.jsctx_mutex);
2646 /* Context can not be used at this time */
2647 spin_lock_irqsave(&js_devdata->runpool_irq.lock,
2649 if (kbase_js_ctx_pullable(kctx, js, false)
2650 || (kctx->jctx.sched_info.ctx.flags &
2651 KBASE_CTX_FLAG_PRIVILEGED))
2653 kbase_js_ctx_list_add_pullable_head_nolock(
2654 kctx->kbdev, kctx, js);
2657 kbase_js_ctx_list_add_unpullable_nolock(
2658 kctx->kbdev, kctx, js);
2659 spin_unlock_irqrestore(
2660 &js_devdata->runpool_irq.lock, flags);
2662 &kctx->jctx.sched_info.ctx.jsctx_mutex);
2664 WARN_ON(!kctx->ctx_active);
2665 kctx->ctx_active = false;
2666 kbase_pm_context_idle(kbdev);
2669 /* No more jobs can be submitted on this slot */
2670 js_mask &= ~(1 << js);
2673 mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
2674 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2676 kctx->pulled = false;
2678 if (!kbase_jm_kick(kbdev, 1 << js))
2679 /* No more jobs can be submitted on this slot */
2680 js_mask &= ~(1 << js);
2682 if (!kctx->pulled) {
2683 /* Failed to pull jobs - push to head of list */
2684 if (kbase_js_ctx_pullable(kctx, js, true))
2686 kbase_js_ctx_list_add_pullable_head_nolock(
2691 kbase_js_ctx_list_add_unpullable_nolock(
2696 kbase_jm_idle_ctx(kbdev, kctx);
2697 spin_unlock_irqrestore(
2698 &js_devdata->runpool_irq.lock,
2700 WARN_ON(!kctx->ctx_active);
2701 kctx->ctx_active = false;
2702 kbase_pm_context_idle(kbdev);
2704 spin_unlock_irqrestore(
2705 &js_devdata->runpool_irq.lock,
2709 &kctx->jctx.sched_info.ctx.jsctx_mutex);
2711 js_mask &= ~(1 << js);
2712 break; /* Could not run atoms on this slot */
2715 /* Push to back of list */
2716 if (kbase_js_ctx_pullable(kctx, js, true))
2718 kbase_js_ctx_list_add_pullable_nolock(
2719 kctx->kbdev, kctx, js);
2722 kbase_js_ctx_list_add_unpullable_nolock(
2723 kctx->kbdev, kctx, js);
2724 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
2726 mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
2731 kbase_js_sync_timers(kbdev);
2733 mutex_unlock(&js_devdata->queue_mutex);
2734 up(&js_devdata->schedule_sem);
2737 void kbase_js_zap_context(struct kbase_context *kctx)
2739 struct kbase_device *kbdev = kctx->kbdev;
2740 struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
2741 struct kbasep_js_kctx_info *js_kctx_info = &kctx->jctx.sched_info;
2745 * Critical assumption: No more submission is possible outside of the
2746 * workqueue. This is because the OS *must* prevent U/K calls (IOCTLs)
2747 * whilst the struct kbase_context is terminating.
2750 /* First, atomically do the following:
2751 * - mark the context as dying
2752 * - try to evict it from the policy queue */
2753 mutex_lock(&kctx->jctx.lock);
2754 mutex_lock(&js_devdata->queue_mutex);
2755 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
2756 js_kctx_info->ctx.is_dying = true;
2758 dev_dbg(kbdev->dev, "Zap: Try Evict Ctx %p", kctx);
2761 * At this point we know:
2762 * - If eviction succeeded, it was in the policy queue, but now no
2764 * - We must cancel the jobs here. No Power Manager active reference to
2766 * - This happens asynchronously - kbase_jd_zap_context() will wait for
2767 * those jobs to be killed.
2768 * - If eviction failed, then it wasn't in the policy queue. It is one
2770 * - a. it didn't have any jobs, and so is not in the Policy Queue or
2771 * the Run Pool (not scheduled)
2772 * - Hence, no more work required to cancel jobs. No Power Manager
2773 * active reference to release.
2774 * - b. it was in the middle of a scheduling transaction (and thus must
2775 * have at least 1 job). This can happen from a syscall or a
2776 * kernel thread. We still hold the jsctx_mutex, and so the thread
2777 * must be waiting inside kbasep_js_try_schedule_head_ctx(),
2778 * before checking whether the runpool is full. That thread will
2779 * continue after we drop the mutex, and will notice the context
2780 * is dying. It will rollback the transaction, killing all jobs at
2781 * the same time. kbase_jd_zap_context() will wait for those jobs
2783 * - Hence, no more work required to cancel jobs, or to release the
2784 * Power Manager active reference.
2785 * - c. it is scheduled, and may or may not be running jobs
2786 * - We must cause it to leave the runpool by stopping it from
2787 * submitting any more jobs. When it finally does leave,
2788 * kbasep_js_runpool_requeue_or_kill_ctx() will kill all remaining jobs
2789 * (because it is dying), release the Power Manager active reference,
2790 * and will not requeue the context in the policy queue.
2791 * kbase_jd_zap_context() will wait for those jobs to be killed.
2792 * - Hence, work required just to make it leave the runpool. Cancelling
2793 * jobs and releasing the Power manager active reference will be
2794 * handled when it leaves the runpool.
2796 if (!js_kctx_info->ctx.is_scheduled) {
2797 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2799 &kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
2801 &kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
2804 /* The following events require us to kill off remaining jobs
2805 * and update PM book-keeping:
2806 * - we evicted it correctly (it must have jobs to be in the
2809 * These events need no action, but take this path anyway:
2810 * - Case a: it didn't have any jobs, and was never in the Queue
2811 * - Case b: scheduling transaction will be partially rolled-
2812 * back (this already cancels the jobs)
2815 KBASE_TRACE_ADD(kbdev, JM_ZAP_NON_SCHEDULED, kctx, NULL, 0u,
2816 js_kctx_info->ctx.is_scheduled);
2818 dev_dbg(kbdev->dev, "Zap: Ctx %p scheduled=0", kctx);
2820 /* Only cancel jobs when we evicted from the policy
2821 * queue. No Power Manager active reference was held.
2823 * Having is_dying set ensures that this kills, and
2824 * doesn't requeue */
2825 kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, false);
2827 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2828 mutex_unlock(&js_devdata->queue_mutex);
2829 mutex_unlock(&kctx->jctx.lock);
2831 unsigned long flags;
2834 /* Case c: didn't evict, but it is scheduled - it's in the Run
2836 KBASE_TRACE_ADD(kbdev, JM_ZAP_SCHEDULED, kctx, NULL, 0u,
2837 js_kctx_info->ctx.is_scheduled);
2838 dev_dbg(kbdev->dev, "Zap: Ctx %p is in RunPool", kctx);
2840 /* Disable the ctx from submitting any more jobs */
2841 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2843 kbasep_js_clear_submit_allowed(js_devdata, kctx);
2845 /* Retain and (later) release the context whilst it is is now
2846 * disallowed from submitting jobs - ensures that someone
2847 * somewhere will be removing the context later on */
2848 was_retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
2850 /* Since it's scheduled and we have the jsctx_mutex, it must be
2851 * retained successfully */
2852 KBASE_DEBUG_ASSERT(was_retained);
2854 dev_dbg(kbdev->dev, "Zap: Ctx %p Kill Any Running jobs", kctx);
2856 /* Cancel any remaining running jobs for this kctx - if any.
2857 * Submit is disallowed which takes effect immediately, so no
2858 * more new jobs will appear after we do this. */
2859 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
2860 kbase_job_slot_hardstop(kctx, js, NULL);
2862 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2863 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2864 mutex_unlock(&js_devdata->queue_mutex);
2865 mutex_unlock(&kctx->jctx.lock);
2867 dev_dbg(kbdev->dev, "Zap: Ctx %p Release (may or may not schedule out immediately)",
2870 kbasep_js_runpool_release_ctx(kbdev, kctx);
2873 KBASE_TRACE_ADD(kbdev, JM_ZAP_DONE, kctx, NULL, 0u, 0u);
2875 /* After this, you must wait on both the
2876 * kbase_jd_context::zero_jobs_wait and the
2877 * kbasep_js_kctx_info::ctx::is_scheduled_waitq - to wait for the jobs
2878 * to be destroyed, and the context to be de-scheduled (if it was on the
2881 * kbase_jd_zap_context() will do this. */
2884 static inline int trace_get_refcnt(struct kbase_device *kbdev,
2885 struct kbase_context *kctx)
2887 struct kbasep_js_device_data *js_devdata;
2891 js_devdata = &kbdev->js_data;
2893 as_nr = kctx->as_nr;
2894 if (as_nr != KBASEP_AS_NR_INVALID) {
2895 struct kbasep_js_per_as_data *js_per_as_data;
2897 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
2899 refcnt = js_per_as_data->as_busy_refcount;
2906 * kbase_js_foreach_ctx_job(): - Call a function on all jobs in context
2907 * @kctx: Pointer to context.
2908 * @callback: Pointer to function to call for each job.
2910 * Call a function on all jobs belonging to a non-queued, non-running
2911 * context, and detach the jobs from the context as it goes.
2913 * Due to the locks that might be held at the time of the call, the callback
2914 * may need to defer work on a workqueue to complete its actions (e.g. when
2917 * Atoms will be removed from the queue, so this must only be called when
2918 * cancelling jobs (which occurs as part of context destruction).
2920 * The locking conditions on the caller are as follows:
2921 * - it will be holding kbasep_js_kctx_info::ctx::jsctx_mutex.
2923 static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
2924 kbasep_js_policy_ctx_job_cb callback)
2926 struct kbase_device *kbdev;
2927 struct kbasep_js_device_data *js_devdata;
2928 unsigned long flags;
2931 kbdev = kctx->kbdev;
2932 js_devdata = &kbdev->js_data;
2934 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2936 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_POLICY_FOREACH_CTX_JOBS, kctx, NULL,
2937 0u, trace_get_refcnt(kbdev, kctx));
2939 /* Invoke callback on jobs on each slot in turn */
2940 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++)
2941 jsctx_queue_foreach(kctx, js, callback);
2943 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);