MALI: rockchip: upgrade midgard DDK to r13p0-00rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_js.c
1 /*
2  *
3  * (C) COPYRIGHT 2011-2016 ARM Limited. All rights reserved.
4  *
5  * This program is free software and is provided to you under the terms of the
6  * GNU General Public License version 2 as published by the Free Software
7  * Foundation, and any use by you of this program is subject to the terms
8  * of such GNU licence.
9  *
10  * A copy of the licence is included with the program, and can also be obtained
11  * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12  * Boston, MA  02110-1301, USA.
13  *
14  */
15
16
17
18
19
20 /*
21  * Job Scheduler Implementation
22  */
23 #include <mali_kbase.h>
24 #include <mali_kbase_js.h>
25 #if defined(CONFIG_MALI_GATOR_SUPPORT)
26 #include <mali_kbase_gator.h>
27 #endif
28 #include <mali_kbase_tlstream.h>
29 #include <mali_kbase_hw.h>
30
31 #include <mali_kbase_defs.h>
32 #include <mali_kbase_config_defaults.h>
33
34 #include "mali_kbase_jm.h"
35 #include "mali_kbase_hwaccess_jm.h"
36
37 /*
38  * Private types
39  */
40
41 /* Bitpattern indicating the result of releasing a context */
42 enum {
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
47          * contexts */
48         KBASEP_JS_RELEASE_RESULT_SCHED_ALL = (1u << 1)
49 };
50
51 typedef u32 kbasep_js_release_result;
52
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 */
57 };
58
59 const base_jd_prio
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 */
64 };
65
66
67 /*
68  * Private function prototypes
69  */
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);
73
74 static int kbase_js_get_slot(struct kbase_device *kbdev,
75                                 struct kbase_jd_atom *katom);
76
77 static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
78                 kbasep_js_policy_ctx_job_cb callback);
79
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)
84 {
85         unsigned long flags;
86         struct kbasep_js_device_data *js_devdata;
87         int as_nr;
88         int refcnt = 0;
89
90         js_devdata = &kbdev->js_data;
91
92         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
93         as_nr = kctx->as_nr;
94         if (as_nr != KBASEP_AS_NR_INVALID) {
95                 struct kbasep_js_per_as_data *js_per_as_data;
96
97                 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
98
99                 refcnt = js_per_as_data->as_busy_refcount;
100         }
101         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
102
103         return refcnt;
104 }
105
106 static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
107                                                 struct kbase_context *kctx)
108 {
109         struct kbasep_js_device_data *js_devdata;
110         int as_nr;
111         int refcnt = 0;
112
113         js_devdata = &kbdev->js_data;
114
115         as_nr = kctx->as_nr;
116         if (as_nr != KBASEP_AS_NR_INVALID) {
117                 struct kbasep_js_per_as_data *js_per_as_data;
118
119                 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
120
121                 refcnt = js_per_as_data->as_busy_refcount;
122         }
123
124         return refcnt;
125 }
126 #else                           /* KBASE_TRACE_ENABLE  */
127 static int kbasep_js_trace_get_refcnt(struct kbase_device *kbdev,
128                 struct kbase_context *kctx)
129 {
130         CSTD_UNUSED(kbdev);
131         CSTD_UNUSED(kctx);
132         return 0;
133 }
134 static int kbasep_js_trace_get_refcnt_nolock(struct kbase_device *kbdev,
135                                                 struct kbase_context *kctx)
136 {
137         CSTD_UNUSED(kbdev);
138         CSTD_UNUSED(kctx);
139         return 0;
140 }
141 #endif                          /* KBASE_TRACE_ENABLE  */
142
143 /*
144  * Private types
145  */
146 enum {
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)
151 };
152
153 enum {
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)
158 };
159
160 /*
161  * Private functions
162  */
163
164 /**
165  * core_reqs_from_jsn_features - Convert JSn_FEATURES to core requirements
166  * @features: JSn_FEATURE register value
167  *
168  * Given a JSn_FEATURE register value returns the core requirements that match
169  *
170  * Return: Core requirement bit mask
171  */
172 static base_jd_core_req core_reqs_from_jsn_features(u16 features)
173 {
174         base_jd_core_req core_req = 0u;
175
176         if ((features & JS_FEATURE_SET_VALUE_JOB) != 0)
177                 core_req |= BASE_JD_REQ_V;
178
179         if ((features & JS_FEATURE_CACHE_FLUSH_JOB) != 0)
180                 core_req |= BASE_JD_REQ_CF;
181
182         if ((features & JS_FEATURE_COMPUTE_JOB) != 0)
183                 core_req |= BASE_JD_REQ_CS;
184
185         if ((features & JS_FEATURE_TILER_JOB) != 0)
186                 core_req |= BASE_JD_REQ_T;
187
188         if ((features & JS_FEATURE_FRAGMENT_JOB) != 0)
189                 core_req |= BASE_JD_REQ_FS;
190
191         return core_req;
192 }
193
194 static void kbase_js_sync_timers(struct kbase_device *kbdev)
195 {
196         mutex_lock(&kbdev->js_data.runpool_mutex);
197         kbase_backend_ctx_count_changed(kbdev);
198         mutex_unlock(&kbdev->js_data.runpool_mutex);
199 }
200
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)
204 {
205         struct kbasep_js_device_data *js_devdata;
206         struct kbasep_js_per_as_data *js_per_as_data;
207         bool result = false;
208         int as_nr;
209
210         KBASE_DEBUG_ASSERT(kbdev != NULL);
211         KBASE_DEBUG_ASSERT(kctx != NULL);
212         js_devdata = &kbdev->js_data;
213
214         as_nr = kctx->as_nr;
215         if (as_nr != KBASEP_AS_NR_INVALID) {
216                 int new_refcnt;
217
218                 KBASE_DEBUG_ASSERT(as_nr >= 0);
219                 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
220
221                 KBASE_DEBUG_ASSERT(js_per_as_data->kctx != NULL);
222
223                 new_refcnt = ++(js_per_as_data->as_busy_refcount);
224
225                 KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RETAIN_CTX_NOLOCK, kctx,
226                                 NULL, 0u, new_refcnt);
227                 result = true;
228         }
229
230         return result;
231 }
232
233 /**
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.
238  *
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
242  * true.
243  *
244  * Return: true if there are no atoms to pull, false otherwise.
245  */
246 static inline bool
247 jsctx_rb_none_to_pull_prio(struct kbase_context *kctx, int js, int prio)
248 {
249         struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
250
251         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
252
253         return RB_EMPTY_ROOT(&rb->runnable_tree);
254 }
255
256 /**
257  * jsctx_rb_none_to_pull(): - Check if all priority ring buffers have no
258  * pullable atoms
259  * @kctx: Pointer to kbase context with ring buffer.
260  * @js:   Job slot id to check.
261  *
262  * Caller must hold runpool_irq.lock
263  *
264  * Return: true if the ring buffers for all priorities have no pullable atoms,
265  *         false otherwise.
266  */
267 static inline bool
268 jsctx_rb_none_to_pull(struct kbase_context *kctx, int js)
269 {
270         int prio;
271
272         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
273
274         for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
275                 if (!jsctx_rb_none_to_pull_prio(kctx, js, prio))
276                         return false;
277         }
278
279         return true;
280 }
281
282 /**
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.
288  *
289  * Iterate over a queue and invoke @callback for each entry in the queue, and
290  * remove the entry from the queue.
291  *
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
295  * this function.
296  *
297  * The HW access lock, js_data.runpool_irq.lock, must always be held when
298  * calling this function.
299  */
300 static void
301 jsctx_queue_foreach_prio(struct kbase_context *kctx, int js, int prio,
302                 kbasep_js_policy_ctx_job_cb callback)
303 {
304         struct jsctx_queue *queue = &kctx->jsctx_queue[prio][js];
305
306         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
307
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);
312
313                 rb_erase(node, &queue->runnable_tree);
314                 callback(kctx->kbdev, entry);
315         }
316
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);
320
321                 list_del(queue->x_dep_head.next);
322
323                 callback(kctx->kbdev, entry);
324         }
325 }
326
327 /**
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.
332  *
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.
336  */
337 static inline void
338 jsctx_queue_foreach(struct kbase_context *kctx, int js,
339                 kbasep_js_policy_ctx_job_cb callback)
340 {
341         int prio;
342
343         for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++)
344                 jsctx_queue_foreach_prio(kctx, js, prio, callback);
345 }
346
347 /**
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.
352  *
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.
355  *
356  * Return: Pointer to next atom in buffer, or NULL if there is no atom.
357  */
358 static inline struct kbase_jd_atom *
359 jsctx_rb_peek_prio(struct kbase_context *kctx, int js, int prio)
360 {
361         struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
362         struct rb_node *node;
363
364         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
365
366         node = rb_first(&rb->runnable_tree);
367         if (!node)
368                 return NULL;
369
370         return rb_entry(node, struct kbase_jd_atom, runnable_tree_node);
371 }
372
373 /**
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.
377  *
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.
381  *
382  * Caller must hold the runpool_irq.lock.
383  *
384  * Return: Pointer to next atom in buffer, or NULL if there is no atom.
385  */
386 static inline struct kbase_jd_atom *
387 jsctx_rb_peek(struct kbase_context *kctx, int js)
388 {
389         int prio;
390
391         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
392
393         for (prio = 0; prio < KBASE_JS_ATOM_SCHED_PRIO_COUNT; prio++) {
394                 struct kbase_jd_atom *katom;
395
396                 katom = jsctx_rb_peek_prio(kctx, js, prio);
397                 if (katom)
398                         return katom;
399         }
400
401         return NULL;
402 }
403
404 /**
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.
408  *
409  * Mark an atom previously obtained from jsctx_rb_peek() as running.
410  *
411  * @katom must currently be at the head of the ring buffer.
412  */
413 static inline void
414 jsctx_rb_pull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
415 {
416         int prio = katom->sched_priority;
417         int js = katom->slot_nr;
418         struct jsctx_queue *rb = &kctx->jsctx_queue[prio][js];
419
420         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
421
422         /* Atoms must be pulled in the correct order. */
423         WARN_ON(katom != jsctx_rb_peek_prio(kctx, js, prio));
424
425         rb_erase(&katom->runnable_tree_node, &rb->runnable_tree);
426 }
427
428 #define LESS_THAN_WRAP(a, b) ((s32)(a - b) < 0)
429
430 static void
431 jsctx_tree_add(struct kbase_context *kctx, struct kbase_jd_atom *katom)
432 {
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;
437
438         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
439
440         while (*new) {
441                 struct kbase_jd_atom *entry = container_of(*new,
442                                 struct kbase_jd_atom, runnable_tree_node);
443
444                 parent = *new;
445                 if (LESS_THAN_WRAP(katom->age, entry->age))
446                         new = &((*new)->rb_left);
447                 else
448                         new = &((*new)->rb_right);
449         }
450
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);
454 }
455
456 /**
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.
460  *
461  * Undo jsctx_rb_pull() and put @katom back in the queue.
462  *
463  * jsctx_rb_unpull() must be called on atoms in the same order the atoms were
464  * pulled.
465  */
466 static inline void
467 jsctx_rb_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
468 {
469         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
470
471         jsctx_tree_add(kctx, katom);
472 }
473
474 static bool kbase_js_ctx_pullable(struct kbase_context *kctx,
475                                         int js,
476                                         bool is_scheduled);
477 static bool kbase_js_ctx_list_add_pullable_nolock(struct kbase_device *kbdev,
478                                                 struct kbase_context *kctx,
479                                                 int js);
480 static bool kbase_js_ctx_list_add_unpullable_nolock(struct kbase_device *kbdev,
481                                                 struct kbase_context *kctx,
482                                                 int js);
483
484 /*
485  * Functions private to KBase ('Protected' functions)
486  */
487 int kbasep_js_devdata_init(struct kbase_device * const kbdev)
488 {
489         struct kbasep_js_device_data *jsdd;
490         int err;
491         int i;
492         u16 as_present;
493
494         KBASE_DEBUG_ASSERT(kbdev != NULL);
495
496         jsdd = &kbdev->js_data;
497
498         KBASE_DEBUG_ASSERT(jsdd->init_status == JS_DEVDATA_INIT_NONE);
499
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)) {
505                 bool use_workaround;
506
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;
511                 }
512         }
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);
533
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;
540         else
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;
546         else
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);
556
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));
584
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");
591                 return -EINVAL;
592         }
593
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);
598 #endif
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);
604 #endif
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.");
607 #endif
608
609         /* setup the number of irq throttle cycles base on given time */
610         {
611                 int time_us = kbdev->gpu_props.irq_throttle_time_us;
612                 int cycles = kbasep_js_convert_us_to_gpu_ticks_max_freq(kbdev,
613                                 time_us);
614
615                 atomic_set(&kbdev->irq_throttle_cycles, cycles);
616         }
617
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));
621
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]);
625
626         jsdd->init_status |= JS_DEVDATA_INIT_CONSTANTS;
627
628         /* On error, we could continue on: providing none of the below resources
629          * rely on the ones above */
630
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);
635
636         err = kbasep_js_policy_init(kbdev);
637         if (!err)
638                 jsdd->init_status |= JS_DEVDATA_INIT_POLICY;
639
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]);
643         }
644
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)
648                 return -EINVAL;
649
650         return 0;
651 }
652
653 void kbasep_js_devdata_halt(struct kbase_device *kbdev)
654 {
655         CSTD_UNUSED(kbdev);
656 }
657
658 void kbasep_js_devdata_term(struct kbase_device *kbdev)
659 {
660         struct kbasep_js_device_data *js_devdata;
661
662         KBASE_DEBUG_ASSERT(kbdev != NULL);
663
664         js_devdata = &kbdev->js_data;
665
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
669                  */
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);
676         }
677         if ((js_devdata->init_status & JS_DEVDATA_INIT_POLICY))
678                 kbasep_js_policy_term(&js_devdata->policy);
679
680         js_devdata->init_status = JS_DEVDATA_INIT_NONE;
681 }
682
683 int kbasep_js_kctx_init(struct kbase_context * const kctx)
684 {
685         struct kbase_device *kbdev;
686         struct kbasep_js_kctx_info *js_kctx_info;
687         int err;
688         int i, j;
689
690         KBASE_DEBUG_ASSERT(kctx != NULL);
691
692         kbdev = kctx->kbdev;
693         KBASE_DEBUG_ASSERT(kbdev != NULL);
694
695         for (i = 0; i < BASE_JM_MAX_NR_SLOTS; ++i)
696                 INIT_LIST_HEAD(&kctx->jctx.sched_info.ctx.ctx_list_entry[i]);
697
698         js_kctx_info = &kctx->jctx.sched_info;
699         KBASE_DEBUG_ASSERT(js_kctx_info->init_status == JS_KCTX_INIT_NONE);
700
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));
706
707         /* Initially, the context is disabled from submission until the create
708          * flags are set */
709         js_kctx_info->ctx.flags = KBASE_CTX_FLAG_SUBMIT_DISABLED;
710
711         js_kctx_info->init_status |= JS_KCTX_INIT_CONSTANTS;
712
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);
716
717         init_waitqueue_head(&js_kctx_info->ctx.is_scheduled_wait);
718
719         err = kbasep_js_policy_init_ctx(kbdev, kctx);
720         if (!err)
721                 js_kctx_info->init_status |= JS_KCTX_INIT_POLICY;
722
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)
726                 return -EINVAL;
727
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;
732                 }
733         }
734
735         return 0;
736 }
737
738 void kbasep_js_kctx_term(struct kbase_context *kctx)
739 {
740         struct kbase_device *kbdev;
741         struct kbasep_js_kctx_info *js_kctx_info;
742         union kbasep_js_policy *js_policy;
743         int js;
744         bool update_ctx_count = false;
745
746         KBASE_DEBUG_ASSERT(kctx != NULL);
747
748         kbdev = kctx->kbdev;
749         KBASE_DEBUG_ASSERT(kbdev != NULL);
750
751         js_policy = &kbdev->js_data.policy;
752         js_kctx_info = &kctx->jctx.sched_info;
753
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);
758         }
759
760         mutex_lock(&kbdev->js_data.queue_mutex);
761         mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
762
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]);
765
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;
771         }
772
773         mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
774         mutex_unlock(&kbdev->js_data.queue_mutex);
775
776         if ((js_kctx_info->init_status & JS_KCTX_INIT_POLICY))
777                 kbasep_js_policy_term_ctx(js_policy, kctx);
778
779         js_kctx_info->init_status = JS_KCTX_INIT_NONE;
780
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);
785         }
786 }
787
788 /**
789  * kbase_js_ctx_list_add_pullable_nolock - Variant of
790  *                                         kbase_jd_ctx_list_add_pullable()
791  *                                         where the caller must hold
792  *                                         runpool_irq.lock
793  * @kbdev:  Device pointer
794  * @kctx:   Context to add to queue
795  * @js:     Job slot to use
796  *
797  * Caller must hold runpool_irq.lock
798  *
799  * Return: true if caller should call kbase_backend_ctx_count_changed()
800  */
801 static bool kbase_js_ctx_list_add_pullable_nolock(struct kbase_device *kbdev,
802                                                 struct kbase_context *kctx,
803                                                 int js)
804 {
805         bool ret = false;
806
807         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
808
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]);
811
812         list_add_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
813                                         &kbdev->js_data.ctx_list_pullable[js]);
814
815         if (!kctx->slots_pullable) {
816                 kbdev->js_data.nr_contexts_pullable++;
817                 ret = true;
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);
822                 }
823         }
824         kctx->slots_pullable |= (1 << js);
825
826         return ret;
827 }
828
829 /**
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
833  *                                              runpool_irq.lock
834  * @kbdev:  Device pointer
835  * @kctx:   Context to add to queue
836  * @js:     Job slot to use
837  *
838  * Caller must hold runpool_irq.lock
839  *
840  * Return:  true if caller should call kbase_backend_ctx_count_changed()
841  */
842 static bool kbase_js_ctx_list_add_pullable_head_nolock(
843                 struct kbase_device *kbdev, struct kbase_context *kctx, int js)
844 {
845         bool ret = false;
846
847         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
848
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]);
851
852         list_add(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
853                                         &kbdev->js_data.ctx_list_pullable[js]);
854
855         if (!kctx->slots_pullable) {
856                 kbdev->js_data.nr_contexts_pullable++;
857                 ret = true;
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);
862                 }
863         }
864         kctx->slots_pullable |= (1 << js);
865
866         return ret;
867 }
868
869 /**
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
875  *
876  * If the context is on either the pullable or unpullable queues, then it is
877  * removed before being added to the head.
878  *
879  * This function should be used when a context has been scheduled, but no jobs
880  * can currently be pulled from it.
881  *
882  * Return:  true if caller should call kbase_backend_ctx_count_changed()
883  */
884 static bool kbase_js_ctx_list_add_pullable_head(struct kbase_device *kbdev,
885                                                 struct kbase_context *kctx,
886                                                 int js)
887 {
888         bool ret;
889         unsigned long flags;
890
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);
894
895         return ret;
896 }
897
898 /**
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
904  *
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.
907  *
908  * This function should be used when a context has been pulled from, and there
909  * are no jobs remaining on the specified slot.
910  *
911  * Caller must hold runpool_irq.lock
912  *
913  * Return:  true if caller should call kbase_backend_ctx_count_changed()
914  */
915 static bool kbase_js_ctx_list_add_unpullable_nolock(struct kbase_device *kbdev,
916                                                 struct kbase_context *kctx,
917                                                 int js)
918 {
919         bool ret = false;
920
921         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
922
923         list_move_tail(&kctx->jctx.sched_info.ctx.ctx_list_entry[js],
924                                 &kbdev->js_data.ctx_list_unpullable[js]);
925
926         if (kctx->slots_pullable == (1 << js)) {
927                 kbdev->js_data.nr_contexts_pullable--;
928                 ret = true;
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);
933                 }
934         }
935         kctx->slots_pullable &= ~(1 << js);
936
937         return ret;
938 }
939
940 /**
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
946  *
947  * The context must already be on one of the queues.
948  *
949  * This function should be used when a context has no jobs on the GPU, and no
950  * jobs remaining for the specified slot.
951  *
952  * Caller must hold runpool_irq.lock
953  *
954  * Return:  true if caller should call kbase_backend_ctx_count_changed()
955  */
956 static bool kbase_js_ctx_list_remove_nolock(struct kbase_device *kbdev,
957                                         struct kbase_context *kctx,
958                                         int js)
959 {
960         bool ret = false;
961
962         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
963
964         WARN_ON(list_empty(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]));
965
966         list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
967
968         if (kctx->slots_pullable == (1 << js)) {
969                 kbdev->js_data.nr_contexts_pullable--;
970                 ret = true;
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);
975                 }
976         }
977         kctx->slots_pullable &= ~(1 << js);
978
979         return ret;
980 }
981
982 /**
983  * kbase_js_ctx_list_pop_head_nolock - Variant of kbase_js_ctx_list_pop_head()
984  *                                     where the caller must hold
985  *                                     runpool_irq.lock
986  * @kbdev:  Device pointer
987  * @js:     Job slot to use
988  *
989  * Caller must hold runpool_irq.lock
990  *
991  * Return:  Context to use for specified slot.
992  *          NULL if no contexts present for specified slot
993  */
994 static struct kbase_context *kbase_js_ctx_list_pop_head_nolock(
995                                                 struct kbase_device *kbdev,
996                                                 int js)
997 {
998         struct kbase_context *kctx;
999
1000         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1001
1002         if (list_empty(&kbdev->js_data.ctx_list_pullable[js]))
1003                 return NULL;
1004
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]);
1008
1009         list_del_init(&kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
1010
1011         return kctx;
1012 }
1013
1014 /**
1015  * kbase_js_ctx_list_pop_head - Pop the head context off the per-slot pullable
1016  *                              queue.
1017  * @kbdev:  Device pointer
1018  * @js:     Job slot to use
1019  *
1020  * Return:  Context to use for specified slot.
1021  *          NULL if no contexts present for specified slot
1022  */
1023 static struct kbase_context *kbase_js_ctx_list_pop_head(
1024                 struct kbase_device *kbdev, int js)
1025 {
1026         struct kbase_context *kctx;
1027         unsigned long flags;
1028
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);
1032
1033         return kctx;
1034 }
1035
1036 /**
1037  * kbase_js_ctx_pullable - Return if a context can be pulled from on the
1038  *                         specified slot
1039  * @kctx:          Context pointer
1040  * @js:            Job slot to use
1041  * @is_scheduled:  true if the context is currently scheduled
1042  *
1043  * Caller must hold runpool_irq.lock
1044  *
1045  * Return:         true if context can be pulled from on specified slot
1046  *                 false otherwise
1047  */
1048 static bool kbase_js_ctx_pullable(struct kbase_context *kctx, int js,
1049                                         bool is_scheduled)
1050 {
1051         struct kbasep_js_device_data *js_devdata;
1052         struct kbase_jd_atom *katom;
1053
1054         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
1055
1056         js_devdata = &kctx->kbdev->js_data;
1057
1058         if (is_scheduled) {
1059                 if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
1060                         return false;
1061         }
1062         katom = jsctx_rb_peek(kctx, js);
1063         if (!katom)
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)
1071                         return false;
1072                 if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
1073                                 kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
1074                         return false;
1075         }
1076
1077         return true;
1078 }
1079
1080 static bool kbase_js_dep_validate(struct kbase_context *kctx,
1081                                 struct kbase_jd_atom *katom)
1082 {
1083         struct kbase_device *kbdev = kctx->kbdev;
1084         bool ret = true;
1085         bool has_dep = false, has_x_dep = false;
1086         int js = kbase_js_get_slot(kbdev, katom);
1087         int prio = katom->sched_priority;
1088         int i;
1089
1090         for (i = 0; i < 2; i++) {
1091                 struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
1092
1093                 if (dep_atom) {
1094                         int dep_js = kbase_js_get_slot(kbdev, dep_atom);
1095                         int dep_prio = dep_atom->sched_priority;
1096
1097                         /* Dependent atom must already have been submitted */
1098                         if (!(dep_atom->atom_flags &
1099                                         KBASE_KATOM_FLAG_JSCTX_IN_TREE)) {
1100                                 ret = false;
1101                                 break;
1102                         }
1103
1104                         /* Dependencies with different priorities can't
1105                           be represented in the ringbuffer */
1106                         if (prio != dep_prio) {
1107                                 ret = false;
1108                                 break;
1109                         }
1110
1111                         if (js == dep_js) {
1112                                 /* Only one same-slot dependency can be
1113                                  * represented in the ringbuffer */
1114                                 if (has_dep) {
1115                                         ret = false;
1116                                         break;
1117                                 }
1118                                 /* Each dependee atom can only have one
1119                                  * same-slot dependency */
1120                                 if (dep_atom->post_dep) {
1121                                         ret = false;
1122                                         break;
1123                                 }
1124                                 has_dep = true;
1125                         } else {
1126                                 /* Only one cross-slot dependency can be
1127                                  * represented in the ringbuffer */
1128                                 if (has_x_dep) {
1129                                         ret = false;
1130                                         break;
1131                                 }
1132                                 /* Each dependee atom can only have one
1133                                  * cross-slot dependency */
1134                                 if (dep_atom->x_post_dep) {
1135                                         ret = false;
1136                                         break;
1137                                 }
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) {
1142                                         ret = false;
1143                                         break;
1144                                 }
1145                                 /* The dependee atom can not already have
1146                                  * completed */
1147                                 if (dep_atom->status !=
1148                                                 KBASE_JD_ATOM_STATE_IN_JS) {
1149                                         ret = false;
1150                                         break;
1151                                 }
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)) {
1157                                         ret = false;
1158                                         break;
1159                                 }
1160                                 has_x_dep = true;
1161                         }
1162
1163                         /* Dependency can be represented in ringbuffers */
1164                 }
1165         }
1166
1167         /* If dependencies can be represented by ringbuffer then clear them from
1168          * atom structure */
1169         if (ret) {
1170                 for (i = 0; i < 2; i++) {
1171                         struct kbase_jd_atom *dep_atom = katom->dep[i].atom;
1172
1173                         if (dep_atom) {
1174                                 int dep_js = kbase_js_get_slot(kbdev, dep_atom);
1175
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)) {
1183
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(
1189                                                         &katom->dep[i]) ==
1190                                                         BASE_JD_DEP_TYPE_DATA)
1191                                                 katom->atom_flags |=
1192                                                 KBASE_KATOM_FLAG_FAIL_BLOCKER;
1193                                 }
1194                                 if ((kbase_jd_katom_dep_type(&katom->dep[i])
1195                                                 == BASE_JD_DEP_TYPE_DATA) &&
1196                                                 (js == dep_js)) {
1197                                         katom->pre_dep = dep_atom;
1198                                         dep_atom->post_dep = katom;
1199                                 }
1200
1201                                 list_del(&katom->dep_item[i]);
1202                                 kbase_jd_katom_dep_clear(&katom->dep[i]);
1203                         }
1204                 }
1205         }
1206
1207         return ret;
1208 }
1209
1210 bool kbasep_js_add_job(struct kbase_context *kctx,
1211                 struct kbase_jd_atom *atom)
1212 {
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;
1218
1219         bool enqueue_required = false;
1220         bool timer_sync = false;
1221
1222         KBASE_DEBUG_ASSERT(kctx != NULL);
1223         KBASE_DEBUG_ASSERT(atom != NULL);
1224         lockdep_assert_held(&kctx->jctx.lock);
1225
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;
1230
1231         mutex_lock(&js_devdata->queue_mutex);
1232         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1233
1234         /*
1235          * Begin Runpool transaction
1236          */
1237         mutex_lock(&js_devdata->runpool_mutex);
1238
1239         /* Refcount ctx.nr_jobs */
1240         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs < U32_MAX);
1241         ++(js_kctx_info->ctx.nr_jobs);
1242
1243         /* Setup any scheduling information */
1244         kbasep_js_clear_job_retry_submit(atom);
1245
1246         /* Lock for state available during IRQ */
1247         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1248
1249         if (!kbase_js_dep_validate(kctx, atom)) {
1250                 /* Dependencies could not be represented */
1251                 --(js_kctx_info->ctx.nr_jobs);
1252
1253                 /* Setting atom status back to queued as it still has unresolved
1254                  * dependencies */
1255                 atom->status = KBASE_JD_ATOM_STATE_QUEUED;
1256
1257                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1258                 mutex_unlock(&js_devdata->runpool_mutex);
1259
1260                 goto out_unlock;
1261         }
1262
1263         KBASE_TIMELINE_ATOM_READY(kctx, kbase_jd_atom_id(kctx, atom));
1264
1265         enqueue_required = kbase_js_dep_resolved_submit(kctx, atom);
1266
1267         KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_ADD_JOB, kctx, atom, atom->jc,
1268                                 kbasep_js_trace_get_refcnt_nolock(kbdev, kctx));
1269
1270         /* Context Attribute Refcounting */
1271         kbasep_js_ctx_attr_ctx_retain_atom(kbdev, kctx, atom);
1272
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);
1277                 else
1278                         timer_sync = kbase_js_ctx_list_add_unpullable_nolock(
1279                                         kbdev, kctx, atom->slot_nr);
1280         }
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);
1285
1286         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1287         if (timer_sync)
1288                 kbase_backend_ctx_count_changed(kbdev);
1289         mutex_unlock(&js_devdata->runpool_mutex);
1290         /* End runpool transaction */
1291
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
1297                          * context. */
1298                         kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx,
1299                                         false);
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);
1305
1306                         /* Policy Queue was updated - caller must try to
1307                          * schedule the head context */
1308                         WARN_ON(!enqueue_required);
1309                 }
1310         }
1311 out_unlock:
1312         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1313
1314         mutex_unlock(&js_devdata->queue_mutex);
1315
1316         return enqueue_required;
1317 }
1318
1319 void kbasep_js_remove_job(struct kbase_device *kbdev,
1320                 struct kbase_context *kctx, struct kbase_jd_atom *atom)
1321 {
1322         struct kbasep_js_kctx_info *js_kctx_info;
1323         struct kbasep_js_device_data *js_devdata;
1324         union kbasep_js_policy *js_policy;
1325
1326         KBASE_DEBUG_ASSERT(kbdev != NULL);
1327         KBASE_DEBUG_ASSERT(kctx != NULL);
1328         KBASE_DEBUG_ASSERT(atom != NULL);
1329
1330         js_devdata = &kbdev->js_data;
1331         js_policy = &kbdev->js_data.policy;
1332         js_kctx_info = &kctx->jctx.sched_info;
1333
1334         KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_REMOVE_JOB, kctx, atom, atom->jc,
1335                         kbasep_js_trace_get_refcnt(kbdev, kctx));
1336
1337         /* De-refcount ctx.nr_jobs */
1338         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.nr_jobs > 0);
1339         --(js_kctx_info->ctx.nr_jobs);
1340 }
1341
1342 bool kbasep_js_remove_cancelled_job(struct kbase_device *kbdev,
1343                 struct kbase_context *kctx, struct kbase_jd_atom *katom)
1344 {
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;
1349
1350         KBASE_DEBUG_ASSERT(kbdev != NULL);
1351         KBASE_DEBUG_ASSERT(kctx != NULL);
1352         KBASE_DEBUG_ASSERT(katom != NULL);
1353
1354         js_devdata = &kbdev->js_data;
1355
1356         kbasep_js_atom_retained_state_copy(&katom_retained_state, katom);
1357         kbasep_js_remove_job(kbdev, kctx, katom);
1358
1359         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1360
1361         /* The atom has 'finished' (will not be re-run), so no need to call
1362          * kbasep_js_has_atom_finished().
1363          *
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);
1369
1370         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1371
1372         return attr_state_changed;
1373 }
1374
1375 bool kbasep_js_runpool_retain_ctx(struct kbase_device *kbdev,
1376                 struct kbase_context *kctx)
1377 {
1378         unsigned long flags;
1379         struct kbasep_js_device_data *js_devdata;
1380         bool result;
1381
1382         KBASE_DEBUG_ASSERT(kbdev != NULL);
1383         js_devdata = &kbdev->js_data;
1384
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);
1390
1391         return result;
1392 }
1393
1394 struct kbase_context *kbasep_js_runpool_lookup_ctx(struct kbase_device *kbdev,
1395                 int as_nr)
1396 {
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;
1401
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];
1406
1407         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1408
1409         found_kctx = js_per_as_data->kctx;
1410
1411         if (found_kctx != NULL)
1412                 ++(js_per_as_data->as_busy_refcount);
1413
1414         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1415
1416         return found_kctx;
1417 }
1418
1419 struct kbase_context *kbasep_js_runpool_lookup_ctx_nolock(
1420                 struct kbase_device *kbdev, int as_nr)
1421 {
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;
1425
1426         KBASE_DEBUG_ASSERT(kbdev != NULL);
1427         KBASE_DEBUG_ASSERT(0 <= as_nr && as_nr < BASE_MAX_NR_AS);
1428
1429         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1430
1431         js_devdata = &kbdev->js_data;
1432         js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
1433
1434         found_kctx = js_per_as_data->kctx;
1435
1436         if (found_kctx != NULL)
1437                 ++(js_per_as_data->as_busy_refcount);
1438
1439         return found_kctx;
1440 }
1441
1442 /**
1443  * kbasep_js_release_result - Try running more jobs after releasing a context
1444  *                            and/or atom
1445  *
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
1450  *
1451  * This collates a set of actions that must happen whilst
1452  * kbasep_js_device_data.runpool_irq.lock is held.
1453  *
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.
1459  *
1460  * Return: %KBASEP_JS_RELEASE_RESULT_SCHED_ALL if context attributes were
1461  *         changed. The caller should try scheduling all contexts
1462  */
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)
1468 {
1469         struct kbasep_js_device_data *js_devdata;
1470         kbasep_js_release_result result = 0;
1471
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;
1476
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);
1480
1481         if (js_devdata->nr_user_contexts_running != 0) {
1482                 bool retry_submit = false;
1483                 int retry_jobslot = 0;
1484
1485                 if (katom_retained_state)
1486                         retry_submit = kbasep_js_get_atom_retry_submit_slot(
1487                                         katom_retained_state, &retry_jobslot);
1488
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;
1493
1494                         KBASE_TRACE_ADD_SLOT(kbdev, JD_DONE_TRY_RUN_NEXT_JOB,
1495                                                 kctx, NULL, 0u, retry_jobslot);
1496                 }
1497         }
1498         return result;
1499 }
1500
1501 /*
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
1504  *
1505  * This also starts more jobs running in the case of an ctx-attribute state
1506  * change
1507  *
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
1511  *
1512  * For those tasks, just call kbasep_js_runpool_release_ctx() instead
1513  *
1514  * Requires:
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
1519  */
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)
1524 {
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;
1530
1531         kbasep_js_release_result release_result = 0u;
1532         bool runpool_ctx_attr_change = false;
1533         int kctx_as_nr;
1534         struct kbase_as *current_as;
1535         int new_ref_count;
1536
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;
1542
1543         /* Ensure context really is scheduled in */
1544         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
1545
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.
1548          *
1549          * They will not change whilst we're reading them, because the refcount
1550          * is non-zero (and we ASSERT on that last fact).
1551          */
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);
1556
1557         /*
1558          * Transaction begins on AS and runpool_irq
1559          *
1560          * Assert about out calling contract
1561          */
1562         current_as = &kbdev->as[kctx_as_nr];
1563         mutex_lock(&kbdev->pm.lock);
1564         mutex_lock(&current_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);
1568
1569         /* Update refcount */
1570         new_ref_count = --(js_per_as_data->as_busy_refcount);
1571
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);
1576
1577         KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_RELEASE_CTX, kctx, NULL, 0u,
1578                         new_ref_count);
1579
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.
1587                  *
1588                  * At this point we re-enable submission to allow further jobs
1589                  * to be executed
1590                  */
1591                 kbasep_js_set_submit_allowed(js_devdata, kctx);
1592         }
1593
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));
1603
1604 #if defined(CONFIG_MALI_GATOR_SUPPORT)
1605                 kbase_trace_mali_mmu_as_released(kctx->as_nr);
1606 #endif
1607                 kbase_tlstream_tl_nret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
1608
1609                 kbase_backend_release_ctx_irq(kbdev, kctx);
1610
1611                 if (kbdev->hwaccess.active_kctx == kctx)
1612                         kbdev->hwaccess.active_kctx = NULL;
1613
1614                 /* Ctx Attribute handling
1615                  *
1616                  * Releasing atoms attributes must either happen before this, or
1617                  * after 'is_scheduled' is changed, otherwise we double-decount
1618                  * the attributes */
1619                 runpool_ctx_attr_change |=
1620                         kbasep_js_ctx_attr_runpool_release_ctx(kbdev, kctx);
1621
1622                 /* Releasing the context and katom retained state can allow
1623                  * more jobs to run */
1624                 release_result |=
1625                         kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev,
1626                                                 kctx, katom_retained_state,
1627                                                 runpool_ctx_attr_change);
1628
1629                 /*
1630                  * Transaction ends on AS and runpool_irq:
1631                  *
1632                  * By this point, the AS-related data is now clear and ready
1633                  * for re-use.
1634                  *
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
1639                  */
1640                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1641
1642                 kbase_backend_release_ctx_noirq(kbdev, kctx);
1643
1644                 mutex_unlock(&current_as->transaction_mutex);
1645                 mutex_unlock(&kbdev->pm.lock);
1646
1647                 /* Note: Don't reuse kctx_as_nr now */
1648
1649                 /* Synchronize with any policy timers */
1650                 kbase_backend_ctx_count_changed(kbdev);
1651
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);
1658
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;
1662         } else {
1663                 kbasep_js_run_jobs_after_ctx_and_atom_release(kbdev, kctx,
1664                                 katom_retained_state, runpool_ctx_attr_change);
1665
1666                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1667                 mutex_unlock(&current_as->transaction_mutex);
1668                 mutex_unlock(&kbdev->pm.lock);
1669         }
1670
1671         return release_result;
1672 }
1673
1674 void kbasep_js_runpool_release_ctx_nolock(struct kbase_device *kbdev,
1675                                                 struct kbase_context *kctx)
1676 {
1677         struct kbasep_js_atom_retained_state katom_retained_state;
1678
1679         /* Setup a dummy katom_retained_state */
1680         kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
1681
1682         kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1683                                                         &katom_retained_state);
1684 }
1685
1686 void kbasep_js_runpool_requeue_or_kill_ctx(struct kbase_device *kbdev,
1687                 struct kbase_context *kctx, bool has_pm_ref)
1688 {
1689         struct kbasep_js_device_data *js_devdata;
1690         union kbasep_js_policy *js_policy;
1691         struct kbasep_js_kctx_info *js_kctx_info;
1692
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;
1698
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
1701          */
1702         KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1703
1704         if (js_kctx_info->ctx.is_dying) {
1705                 /* Dying: don't requeue, but kill all jobs on the context. This
1706                  * happens asynchronously */
1707                 dev_dbg(kbdev->dev,
1708                         "JS: ** Killing Context %p on RunPool Remove **", kctx);
1709                 kbase_js_foreach_ctx_job(kctx, &kbase_jd_cancel);
1710         }
1711 }
1712
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)
1716 {
1717         struct kbasep_js_device_data *js_devdata;
1718         struct kbasep_js_kctx_info *js_kctx_info;
1719         kbasep_js_release_result release_result;
1720
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;
1725
1726         mutex_lock(&js_devdata->queue_mutex);
1727         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1728         mutex_lock(&js_devdata->runpool_mutex);
1729
1730         release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1731                         katom_retained_state);
1732
1733         /* Drop the runpool mutex to allow requeing kctx */
1734         mutex_unlock(&js_devdata->runpool_mutex);
1735
1736         if ((release_result & KBASEP_JS_RELEASE_RESULT_WAS_DESCHEDULED) != 0u)
1737                 kbasep_js_runpool_requeue_or_kill_ctx(kbdev, kctx, true);
1738
1739         /* Drop the jsctx_mutex to allow scheduling in a new context */
1740
1741         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1742         mutex_unlock(&js_devdata->queue_mutex);
1743
1744         if (release_result & KBASEP_JS_RELEASE_RESULT_SCHED_ALL)
1745                 kbase_js_sched_all(kbdev);
1746 }
1747
1748 void kbasep_js_runpool_release_ctx(struct kbase_device *kbdev,
1749                 struct kbase_context *kctx)
1750 {
1751         struct kbasep_js_atom_retained_state katom_retained_state;
1752
1753         kbasep_js_atom_retained_state_init_invalid(&katom_retained_state);
1754
1755         kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
1756                         &katom_retained_state);
1757 }
1758
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)
1763 {
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;
1770
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);
1776
1777         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1778         mutex_lock(&js_devdata->runpool_mutex);
1779
1780         release_result = kbasep_js_runpool_release_ctx_internal(kbdev, kctx,
1781                         katom_retained_state);
1782
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);
1787
1788         /* Drop the jsctx_mutex to allow scheduling in a new context */
1789         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1790
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
1793          */
1794 }
1795
1796 void kbase_js_set_timeouts(struct kbase_device *kbdev)
1797 {
1798         lockdep_assert_held(&kbdev->js_data.runpool_irq.lock);
1799
1800         kbase_backend_timeouts_changed(kbdev);
1801 }
1802
1803 static bool kbasep_js_schedule_ctx(struct kbase_device *kbdev,
1804                                         struct kbase_context *kctx)
1805 {
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;
1812         int as_nr;
1813
1814         js_devdata = &kbdev->js_data;
1815         js_policy = &kbdev->js_data.policy;
1816         js_kctx_info = &kctx->jctx.sched_info;
1817
1818         /* Pick available address space for this context */
1819         as_nr = kbase_backend_find_free_address_space(kbdev, kctx);
1820
1821         if (as_nr == KBASEP_AS_NR_INVALID)
1822                 return false; /* No address spaces currently available */
1823
1824         new_address_space = &kbdev->as[as_nr];
1825
1826         /*
1827          * Atomic transaction on the Context and Run Pool begins
1828          */
1829         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1830         mutex_lock(&js_devdata->runpool_mutex);
1831
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);
1836
1837                 mutex_unlock(&js_devdata->runpool_mutex);
1838                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1839
1840                 return false;
1841         }
1842
1843         KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_TRY_SCHEDULE_HEAD_CTX, kctx, NULL,
1844                                 0u,
1845                                 kbasep_js_trace_get_refcnt(kbdev, kctx));
1846
1847         js_kctx_info->ctx.is_scheduled = true;
1848
1849         mutex_lock(&new_address_space->transaction_mutex);
1850         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1851
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;
1858
1859                 kbase_backend_release_free_address_space(kbdev, as_nr);
1860
1861                 mutex_unlock(&js_devdata->runpool_mutex);
1862
1863                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1864                 return false;
1865         }
1866
1867         kbdev->hwaccess.active_kctx = kctx;
1868
1869 #if defined(CONFIG_MALI_GATOR_SUPPORT)
1870         kbase_trace_mali_mmu_as_in_use(kctx->as_nr);
1871 #endif
1872         kbase_tlstream_tl_ret_as_ctx(&kbdev->as[kctx->as_nr], kctx);
1873
1874         /* Cause any future waiter-on-termination to wait until the context is
1875          * descheduled */
1876         wake_up(&js_kctx_info->ctx.is_scheduled_wait);
1877
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.
1882          *
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 */
1891                 bool retained;
1892
1893                 retained = kbasep_js_runpool_retain_ctx_nolock(kbdev, kctx);
1894                 KBASE_DEBUG_ASSERT(retained);
1895
1896                 kbasep_js_clear_submit_allowed(js_devdata, kctx);
1897                 kctx_suspended = true;
1898         }
1899
1900         /* Transaction complete */
1901         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1902         mutex_unlock(&new_address_space->transaction_mutex);
1903
1904         /* Synchronize with any policy timers */
1905         kbase_backend_ctx_count_changed(kbdev);
1906
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 */
1911
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);
1918                 return false;
1919         }
1920         return true;
1921 }
1922
1923 static bool kbase_js_use_ctx(struct kbase_device *kbdev,
1924                                 struct kbase_context *kctx)
1925 {
1926         struct kbasep_js_device_data *js_devdata = &kbdev->js_data;
1927         unsigned long flags;
1928
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;
1933
1934                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1935                 return true; /* Context already scheduled */
1936         }
1937         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1938
1939         return kbasep_js_schedule_ctx(kbdev, kctx);
1940 }
1941
1942 void kbasep_js_schedule_privileged_ctx(struct kbase_device *kbdev,
1943                 struct kbase_context *kctx)
1944 {
1945         struct kbasep_js_kctx_info *js_kctx_info;
1946         struct kbasep_js_device_data *js_devdata;
1947         bool is_scheduled;
1948
1949         KBASE_DEBUG_ASSERT(kbdev != NULL);
1950         KBASE_DEBUG_ASSERT(kctx != NULL);
1951
1952         js_devdata = &kbdev->js_data;
1953         js_kctx_info = &kctx->jctx.sched_info;
1954
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));
1958
1959         mutex_lock(&js_devdata->queue_mutex);
1960         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1961
1962         /* Mark the context as privileged */
1963         js_kctx_info->ctx.flags |= KBASE_CTX_FLAG_PRIVILEGED;
1964
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);
1970
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);
1975
1976                 /* Try to schedule the context in */
1977                 kbase_js_sched_all(kbdev);
1978
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);
1982         } else {
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);
1988         }
1989 }
1990 KBASE_EXPORT_TEST_API(kbasep_js_schedule_privileged_ctx);
1991
1992 void kbasep_js_release_privileged_ctx(struct kbase_device *kbdev,
1993                 struct kbase_context *kctx)
1994 {
1995         struct kbasep_js_kctx_info *js_kctx_info;
1996
1997         KBASE_DEBUG_ASSERT(kctx != NULL);
1998         js_kctx_info = &kctx->jctx.sched_info;
1999
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);
2004
2005         /* Release the context - it will be scheduled out */
2006         kbasep_js_runpool_release_ctx(kbdev, kctx);
2007
2008         kbase_js_sched_all(kbdev);
2009 }
2010 KBASE_EXPORT_TEST_API(kbasep_js_release_privileged_ctx);
2011
2012 void kbasep_js_suspend(struct kbase_device *kbdev)
2013 {
2014         unsigned long flags;
2015         struct kbasep_js_device_data *js_devdata;
2016         int i;
2017         u16 retained = 0u;
2018         int nr_privileged_ctx = 0;
2019
2020         KBASE_DEBUG_ASSERT(kbdev);
2021         KBASE_DEBUG_ASSERT(kbase_pm_is_suspending(kbdev));
2022         js_devdata = &kbdev->js_data;
2023
2024         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2025
2026         /* Prevent all contexts from submitting */
2027         js_devdata->runpool_irq.submit_allowed = 0;
2028
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;
2035
2036                 retained = retained << 1;
2037
2038                 if (kctx) {
2039                         ++(js_per_as_data->as_busy_refcount);
2040                         retained |= 1u;
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);
2048                         }
2049                 }
2050         }
2051         CSTD_UNUSED(nr_privileged_ctx);
2052         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2053
2054         /* De-ref the previous retain to ensure each context gets pulled out
2055          * sometime later. */
2056         for (i = 0;
2057                  i < BASE_MAX_NR_AS;
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;
2062
2063                 if (retained & 1u)
2064                         kbasep_js_runpool_release_ctx(kbdev, kctx);
2065         }
2066
2067         /* Caller must wait for all Power Manager active references to be
2068          * dropped */
2069 }
2070
2071 void kbasep_js_resume(struct kbase_device *kbdev)
2072 {
2073         struct kbasep_js_device_data *js_devdata;
2074         int js;
2075
2076         KBASE_DEBUG_ASSERT(kbdev);
2077         js_devdata = &kbdev->js_data;
2078         KBASE_DEBUG_ASSERT(!kbase_pm_is_suspending(kbdev));
2079
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;
2083
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;
2090
2091                         js_kctx_info = &kctx->jctx.sched_info;
2092
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);
2096
2097                         if (!js_kctx_info->ctx.is_scheduled &&
2098                                 kbase_js_ctx_pullable(kctx, js, false))
2099                                 timer_sync =
2100                                         kbase_js_ctx_list_add_pullable_nolock(
2101                                                         kbdev, kctx, js);
2102
2103                         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
2104                                                                         flags);
2105                         if (timer_sync)
2106                                 kbase_backend_ctx_count_changed(kbdev);
2107                         mutex_unlock(&js_devdata->runpool_mutex);
2108                         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2109                 }
2110         }
2111         mutex_unlock(&js_devdata->queue_mutex);
2112
2113         /* Restart atom processing */
2114         kbase_js_sched_all(kbdev);
2115
2116         /* JS Resume complete */
2117 }
2118
2119 bool kbase_js_is_atom_valid(struct kbase_device *kbdev,
2120                                 struct kbase_jd_atom *katom)
2121 {
2122         if ((katom->core_req & BASE_JD_REQ_FS) &&
2123             (katom->core_req & (BASE_JD_REQ_CS | BASE_JD_REQ_ONLY_COMPUTE |
2124                                                                 BASE_JD_REQ_T)))
2125                 return false;
2126
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)))
2130                 return false;
2131
2132         return true;
2133 }
2134
2135 static int kbase_js_get_slot(struct kbase_device *kbdev,
2136                                 struct kbase_jd_atom *katom)
2137 {
2138         if (katom->core_req & BASE_JD_REQ_FS)
2139                 return 0;
2140
2141         if (katom->core_req & BASE_JD_REQ_ONLY_COMPUTE) {
2142                 if (katom->device_nr == 1 &&
2143                                 kbdev->gpu_props.num_core_groups == 2)
2144                         return 2;
2145                 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8987))
2146                         return 2;
2147         }
2148
2149         return 1;
2150 }
2151
2152 bool kbase_js_dep_resolved_submit(struct kbase_context *kctx,
2153                                         struct kbase_jd_atom *katom)
2154 {
2155         bool enqueue_required;
2156
2157         katom->slot_nr = kbase_js_get_slot(kctx->kbdev, katom);
2158
2159         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2160         lockdep_assert_held(&kctx->jctx.lock);
2161
2162         /* If slot will transition from unpullable to pullable then add to
2163          * pullable list */
2164         if (jsctx_rb_none_to_pull(kctx, katom->slot_nr)) {
2165                 enqueue_required = true;
2166         } else {
2167                 enqueue_required = false;
2168         }
2169         /* Check if there are lower priority jobs to soft stop */
2170         kbase_job_slot_ctx_priority_check_locked(kctx, katom);
2171
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];
2178
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;
2182         } else {
2183                 /* Add atom to ring buffer. */
2184                 jsctx_tree_add(kctx, katom);
2185                 katom->atom_flags |= KBASE_KATOM_FLAG_JSCTX_IN_TREE;
2186         }
2187
2188         return enqueue_required;
2189 }
2190
2191 /**
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
2195  *
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.
2199  */
2200 static void kbase_js_move_to_tree(struct kbase_jd_atom *katom)
2201 {
2202         lockdep_assert_held(&katom->kctx->kbdev->js_data.runpool_irq.lock);
2203
2204         while (katom) {
2205                 WARN_ON(!(katom->atom_flags &
2206                                 KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST));
2207
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;
2214                 } else {
2215                         break;
2216                 }
2217
2218                 katom = katom->post_dep;
2219         }
2220 }
2221
2222
2223 /**
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.
2229  *
2230  * Remove all post dependencies of an atom from the context ringbuffers.
2231  *
2232  * The original atom's event_code will be propogated to all dependent atoms.
2233  *
2234  * Context: Caller must hold the HW access lock
2235  */
2236 static void kbase_js_evict_deps(struct kbase_context *kctx,
2237                                 struct kbase_jd_atom *katom, int js, int prio)
2238 {
2239         struct kbase_jd_atom *x_dep = katom->x_post_dep;
2240         struct kbase_jd_atom *next_katom = katom->post_dep;
2241
2242         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2243
2244         if (next_katom) {
2245                 KBASE_DEBUG_ASSERT(next_katom->status !=
2246                                 KBASE_JD_ATOM_STATE_HW_COMPLETED);
2247                 next_katom->will_fail_event_code = katom->event_code;
2248
2249         }
2250
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;
2256
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;
2260                 }
2261                 if (x_dep->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_X_DEP_LIST)
2262                         kbase_js_move_to_tree(x_dep);
2263         }
2264 }
2265
2266 struct kbase_jd_atom *kbase_js_pull(struct kbase_context *kctx, int js)
2267 {
2268         struct kbase_jd_atom *katom;
2269         struct kbasep_js_device_data *js_devdata;
2270         int pulled;
2271
2272         KBASE_DEBUG_ASSERT(kctx);
2273
2274         js_devdata = &kctx->kbdev->js_data;
2275         lockdep_assert_held(&js_devdata->runpool_irq.lock);
2276
2277         if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
2278                 return NULL;
2279         if (kbase_pm_is_suspending(kctx->kbdev))
2280                 return NULL;
2281
2282         katom = jsctx_rb_peek(kctx, js);
2283         if (!katom)
2284                 return NULL;
2285
2286         if (atomic_read(&katom->blocked))
2287                 return NULL;
2288
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);
2295
2296                 if (prev_atom && prev_atom->kctx != kctx)
2297                         return NULL;
2298         }
2299
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)
2304                         return NULL;
2305                 if ((katom->atom_flags & KBASE_KATOM_FLAG_FAIL_BLOCKER) &&
2306                                 kbase_backend_nr_atoms_on_slot(kctx->kbdev, js))
2307                         return NULL;
2308         }
2309
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);
2316         }
2317         atomic_inc(&kctx->atoms_pulled_slot[katom->slot_nr]);
2318         jsctx_rb_pull(kctx, katom);
2319
2320         kbasep_js_runpool_retain_ctx_nolock(kctx->kbdev, kctx);
2321         katom->atom_flags |= KBASE_KATOM_FLAG_HOLDING_CTX_REF;
2322
2323         katom->sched_info.cfs.ticks = 0;
2324
2325         return katom;
2326 }
2327
2328
2329 static void js_return_worker(struct work_struct *data)
2330 {
2331         struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom,
2332                                                                         work);
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;
2345
2346         kbase_tlstream_aux_job_softstop_ex(katom);
2347
2348         kbase_backend_complete_wq(kbdev, katom);
2349
2350         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
2351                 kbase_as_poking_timer_release_atom(kbdev, kctx, katom);
2352
2353         kbasep_js_atom_retained_state_copy(&retained_state, katom);
2354
2355         mutex_lock(&js_devdata->queue_mutex);
2356         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
2357
2358         atomic_dec(&kctx->atoms_pulled);
2359         atomic_dec(&kctx->atoms_pulled_slot[js]);
2360
2361         atomic_dec(&katom->blocked);
2362
2363         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2364
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);
2368
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);
2374                         timer_sync = true;
2375                 }
2376
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;
2380                         int slot;
2381
2382                         if (!kbasep_js_is_submit_allowed(js_devdata, kctx))
2383                                 kbasep_js_set_submit_allowed(js_devdata, kctx);
2384
2385                         for (slot = 0; slot < num_slots; slot++) {
2386                                 if (kbase_js_ctx_pullable(kctx, slot, true))
2387                                         timer_sync |=
2388                                         kbase_js_ctx_list_add_pullable_nolock(
2389                                                         kbdev, kctx, slot);
2390                         }
2391                 }
2392
2393                 kbase_jm_idle_ctx(kbdev, kctx);
2394
2395                 context_idle = true;
2396         }
2397
2398         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2399
2400         if (context_idle) {
2401                 WARN_ON(!kctx->ctx_active);
2402                 kctx->ctx_active = false;
2403                 kbase_pm_context_idle(kbdev);
2404         }
2405
2406         if (timer_sync)
2407                 kbase_js_sync_timers(kbdev);
2408
2409         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2410         mutex_unlock(&js_devdata->queue_mutex);
2411
2412         katom->atom_flags &= ~KBASE_KATOM_FLAG_HOLDING_CTX_REF;
2413         kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx,
2414                                                         &retained_state);
2415
2416         kbase_js_sched_all(kbdev);
2417
2418         kbase_backend_complete_wq_post_sched(kbdev, core_req, affinity,
2419                         coreref_state);
2420 }
2421
2422 void kbase_js_unpull(struct kbase_context *kctx, struct kbase_jd_atom *katom)
2423 {
2424         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2425
2426         jsctx_rb_unpull(kctx, katom);
2427
2428         WARN_ON(work_pending(&katom->work));
2429
2430         /* Block re-submission until workqueue has run */
2431         atomic_inc(&katom->blocked);
2432
2433         kbase_job_check_leave_disjoint(kctx->kbdev, katom);
2434
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);
2438 }
2439
2440 bool kbase_js_complete_atom_wq(struct kbase_context *kctx,
2441                                                 struct kbase_jd_atom *katom)
2442 {
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;
2448         int atom_slot;
2449         bool context_idle = false;
2450
2451         kbdev = kctx->kbdev;
2452         atom_slot = katom->slot_nr;
2453
2454         js_kctx_info = &kctx->jctx.sched_info;
2455         js_devdata = &kbdev->js_data;
2456
2457         lockdep_assert_held(&js_kctx_info->ctx.jsctx_mutex);
2458
2459         mutex_lock(&js_devdata->runpool_mutex);
2460         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2461
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]);
2465
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);
2471                         timer_sync = true;
2472                 }
2473         }
2474         WARN_ON(!(katom->atom_flags & KBASE_KATOM_FLAG_JSCTX_IN_TREE));
2475
2476         if (!atomic_read(&kctx->atoms_pulled_slot[atom_slot]) &&
2477                         jsctx_rb_none_to_pull(kctx, atom_slot)) {
2478                 if (!list_empty(
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);
2482         }
2483
2484         /*
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.
2488          */
2489         if (!kbasep_js_is_submit_allowed(js_devdata, kctx) &&
2490                                         !atomic_read(&kctx->atoms_pulled) &&
2491                                         !js_kctx_info->ctx.is_dying) {
2492                 int js;
2493
2494                 kbasep_js_set_submit_allowed(js_devdata, kctx);
2495
2496                 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2497                         if (kbase_js_ctx_pullable(kctx, js, true))
2498                                 timer_sync |=
2499                                         kbase_js_ctx_list_add_pullable_nolock(
2500                                                         kbdev, kctx, js);
2501                 }
2502         } else if (katom->x_post_dep &&
2503                         kbasep_js_is_submit_allowed(js_devdata, kctx)) {
2504                 int js;
2505
2506                 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2507                         if (kbase_js_ctx_pullable(kctx, js, true))
2508                                 timer_sync |=
2509                                         kbase_js_ctx_list_add_pullable_nolock(
2510                                                         kbdev, kctx, js);
2511                 }
2512         }
2513
2514         /* Mark context as inactive. The pm reference will be dropped later in
2515          * jd_done_worker().
2516          */
2517         if (context_idle)
2518                 kctx->ctx_active = false;
2519
2520         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2521         if (timer_sync)
2522                 kbase_backend_ctx_count_changed(kbdev);
2523         mutex_unlock(&js_devdata->runpool_mutex);
2524
2525         return context_idle;
2526 }
2527
2528 void kbase_js_complete_atom(struct kbase_jd_atom *katom, ktime_t *end_timestamp)
2529 {
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;
2535
2536         kbdev = kctx->kbdev;
2537
2538         js_policy = &kbdev->js_data.policy;
2539
2540         lockdep_assert_held(&kctx->kbdev->js_data.runpool_irq.lock);
2541
2542         if (katom->will_fail_event_code)
2543                 katom->event_code = katom->will_fail_event_code;
2544
2545         katom->status = KBASE_JD_ATOM_STATE_HW_COMPLETED;
2546
2547         if (katom->event_code != BASE_JD_EVENT_DONE) {
2548                 kbase_js_evict_deps(kctx, katom, katom->slot_nr,
2549                                 katom->sched_priority);
2550         }
2551
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);
2555 #endif
2556
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
2561                  * zero time) */
2562                 ktime_t tick_diff = ktime_sub(*end_timestamp,
2563                                                         katom->start_timestamp);
2564
2565                 microseconds_spent = ktime_to_ns(tick_diff);
2566
2567                 do_div(microseconds_spent, 1000);
2568
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;
2572         }
2573
2574         /* Log the result of the job (completion status, and time spent). */
2575         kbasep_js_policy_log_job_result(js_policy, katom, microseconds_spent);
2576
2577         kbase_jd_done(katom, katom->slot_nr, end_timestamp, 0);
2578
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,
2584                                 false);
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,
2588                                 false))
2589                         kbase_js_ctx_list_add_pullable_nolock(kbdev, kctx,
2590                                         x_dep->slot_nr);
2591         }
2592 }
2593
2594 void kbase_js_sched(struct kbase_device *kbdev, int js_mask)
2595 {
2596         struct kbasep_js_device_data *js_devdata;
2597         bool timer_sync = false;
2598
2599         js_devdata = &kbdev->js_data;
2600
2601         down(&js_devdata->schedule_sem);
2602         mutex_lock(&js_devdata->queue_mutex);
2603
2604         while (js_mask) {
2605                 int js;
2606
2607                 js = ffs(js_mask) - 1;
2608
2609                 while (1) {
2610                         struct kbase_context *kctx;
2611                         unsigned long flags;
2612                         bool context_idle = false;
2613
2614                         kctx = kbase_js_ctx_list_pop_head(kbdev, js);
2615
2616                         if (!kctx) {
2617                                 js_mask &= ~(1 << js);
2618                                 break; /* No contexts on pullable list */
2619                         }
2620
2621                         if (!kctx->ctx_active) {
2622                                 context_idle = true;
2623
2624                                 if (kbase_pm_context_active_handle_suspend(
2625                                                                         kbdev,
2626                                       KBASE_PM_SUSPEND_HANDLER_DONT_INCREASE)) {
2627                                         /* Suspend pending - return context to
2628                                          * queue and stop scheduling */
2629                                         mutex_lock(
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);
2634                                         mutex_unlock(
2635                                         &kctx->jctx.sched_info.ctx.jsctx_mutex);
2636                                         mutex_unlock(&js_devdata->queue_mutex);
2637                                         up(&js_devdata->schedule_sem);
2638                                         return;
2639                                 }
2640                                 kctx->ctx_active = true;
2641                         }
2642
2643                         if (!kbase_js_use_ctx(kbdev, kctx)) {
2644                                 mutex_lock(
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,
2648                                                                         flags);
2649                                 if (kbase_js_ctx_pullable(kctx, js, false)
2650                                         || (kctx->jctx.sched_info.ctx.flags &
2651                                                 KBASE_CTX_FLAG_PRIVILEGED))
2652                                         timer_sync |=
2653                                         kbase_js_ctx_list_add_pullable_head_nolock(
2654                                                         kctx->kbdev, kctx, js);
2655                                 else
2656                                         timer_sync |=
2657                                         kbase_js_ctx_list_add_unpullable_nolock(
2658                                                         kctx->kbdev, kctx, js);
2659                                 spin_unlock_irqrestore(
2660                                         &js_devdata->runpool_irq.lock, flags);
2661                                 mutex_unlock(
2662                                         &kctx->jctx.sched_info.ctx.jsctx_mutex);
2663                                 if (context_idle) {
2664                                         WARN_ON(!kctx->ctx_active);
2665                                         kctx->ctx_active = false;
2666                                         kbase_pm_context_idle(kbdev);
2667                                 }
2668
2669                                 /* No more jobs can be submitted on this slot */
2670                                 js_mask &= ~(1 << js);
2671                                 break;
2672                         }
2673                         mutex_lock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
2674                         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2675
2676                         kctx->pulled = false;
2677
2678                         if (!kbase_jm_kick(kbdev, 1 << js))
2679                                 /* No more jobs can be submitted on this slot */
2680                                 js_mask &= ~(1 << js);
2681
2682                         if (!kctx->pulled) {
2683                                 /* Failed to pull jobs - push to head of list */
2684                                 if (kbase_js_ctx_pullable(kctx, js, true))
2685                                         timer_sync |=
2686                                         kbase_js_ctx_list_add_pullable_head_nolock(
2687                                                                 kctx->kbdev,
2688                                                                 kctx, js);
2689                                 else
2690                                         timer_sync |=
2691                                         kbase_js_ctx_list_add_unpullable_nolock(
2692                                                                 kctx->kbdev,
2693                                                                 kctx, js);
2694
2695                                 if (context_idle) {
2696                                         kbase_jm_idle_ctx(kbdev, kctx);
2697                                         spin_unlock_irqrestore(
2698                                                 &js_devdata->runpool_irq.lock,
2699                                                                         flags);
2700                                         WARN_ON(!kctx->ctx_active);
2701                                         kctx->ctx_active = false;
2702                                         kbase_pm_context_idle(kbdev);
2703                                 } else {
2704                                         spin_unlock_irqrestore(
2705                                                 &js_devdata->runpool_irq.lock,
2706                                                                         flags);
2707                                 }
2708                                 mutex_unlock(
2709                                         &kctx->jctx.sched_info.ctx.jsctx_mutex);
2710
2711                                 js_mask &= ~(1 << js);
2712                                 break; /* Could not run atoms on this slot */
2713                         }
2714
2715                         /* Push to back of list */
2716                         if (kbase_js_ctx_pullable(kctx, js, true))
2717                                 timer_sync |=
2718                                         kbase_js_ctx_list_add_pullable_nolock(
2719                                                         kctx->kbdev, kctx, js);
2720                         else
2721                                 timer_sync |=
2722                                         kbase_js_ctx_list_add_unpullable_nolock(
2723                                                         kctx->kbdev, kctx, js);
2724                         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
2725                                                                         flags);
2726                         mutex_unlock(&kctx->jctx.sched_info.ctx.jsctx_mutex);
2727                 }
2728         }
2729
2730         if (timer_sync)
2731                 kbase_js_sync_timers(kbdev);
2732
2733         mutex_unlock(&js_devdata->queue_mutex);
2734         up(&js_devdata->schedule_sem);
2735 }
2736
2737 void kbase_js_zap_context(struct kbase_context *kctx)
2738 {
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;
2742         int js;
2743
2744         /*
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.
2748          */
2749
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;
2757
2758         dev_dbg(kbdev->dev, "Zap: Try Evict Ctx %p", kctx);
2759
2760         /*
2761          * At this point we know:
2762          * - If eviction succeeded, it was in the policy queue, but now no
2763          *   longer is
2764          *  - We must cancel the jobs here. No Power Manager active reference to
2765          *    release.
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
2769          *   of the following:
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
2782          *       to be killed.
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.
2795          */
2796         if (!js_kctx_info->ctx.is_scheduled) {
2797                 for (js = 0; js < kbdev->gpu_props.num_job_slots; js++) {
2798                         if (!list_empty(
2799                                 &kctx->jctx.sched_info.ctx.ctx_list_entry[js]))
2800                                 list_del_init(
2801                                 &kctx->jctx.sched_info.ctx.ctx_list_entry[js]);
2802                 }
2803
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
2807                  *   Policy Queue)
2808                  *
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)
2813                  */
2814
2815                 KBASE_TRACE_ADD(kbdev, JM_ZAP_NON_SCHEDULED, kctx, NULL, 0u,
2816                                                 js_kctx_info->ctx.is_scheduled);
2817
2818                 dev_dbg(kbdev->dev, "Zap: Ctx %p scheduled=0", kctx);
2819
2820                 /* Only cancel jobs when we evicted from the policy
2821                  * queue. No Power Manager active reference was held.
2822                  *
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);
2826
2827                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
2828                 mutex_unlock(&js_devdata->queue_mutex);
2829                 mutex_unlock(&kctx->jctx.lock);
2830         } else {
2831                 unsigned long flags;
2832                 bool was_retained;
2833
2834                 /* Case c: didn't evict, but it is scheduled - it's in the Run
2835                  * Pool */
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);
2839
2840                 /* Disable the ctx from submitting any more jobs */
2841                 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2842
2843                 kbasep_js_clear_submit_allowed(js_devdata, kctx);
2844
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);
2849
2850                 /* Since it's scheduled and we have the jsctx_mutex, it must be
2851                  * retained successfully */
2852                 KBASE_DEBUG_ASSERT(was_retained);
2853
2854                 dev_dbg(kbdev->dev, "Zap: Ctx %p Kill Any Running jobs", kctx);
2855
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);
2861
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);
2866
2867                 dev_dbg(kbdev->dev, "Zap: Ctx %p Release (may or may not schedule out immediately)",
2868                                                                         kctx);
2869
2870                 kbasep_js_runpool_release_ctx(kbdev, kctx);
2871         }
2872
2873         KBASE_TRACE_ADD(kbdev, JM_ZAP_DONE, kctx, NULL, 0u, 0u);
2874
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
2879          * runpool).
2880          *
2881          * kbase_jd_zap_context() will do this. */
2882 }
2883
2884 static inline int trace_get_refcnt(struct kbase_device *kbdev,
2885                                         struct kbase_context *kctx)
2886 {
2887         struct kbasep_js_device_data *js_devdata;
2888         int as_nr;
2889         int refcnt = 0;
2890
2891         js_devdata = &kbdev->js_data;
2892
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;
2896
2897                 js_per_as_data = &js_devdata->runpool_irq.per_as_data[as_nr];
2898
2899                 refcnt = js_per_as_data->as_busy_refcount;
2900         }
2901
2902         return refcnt;
2903 }
2904
2905 /**
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.
2909  *
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.
2912  *
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
2915  * cancelling jobs)
2916  *
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).
2919  *
2920  * The locking conditions on the caller are as follows:
2921  * - it will be holding kbasep_js_kctx_info::ctx::jsctx_mutex.
2922  */
2923 static void kbase_js_foreach_ctx_job(struct kbase_context *kctx,
2924                 kbasep_js_policy_ctx_job_cb callback)
2925 {
2926         struct kbase_device *kbdev;
2927         struct kbasep_js_device_data *js_devdata;
2928         unsigned long flags;
2929         u32 js;
2930
2931         kbdev = kctx->kbdev;
2932         js_devdata = &kbdev->js_data;
2933
2934         spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
2935
2936         KBASE_TRACE_ADD_REFCOUNT(kbdev, JS_POLICY_FOREACH_CTX_JOBS, kctx, NULL,
2937                                         0u, trace_get_refcnt(kbdev, kctx));
2938
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);
2942
2943         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
2944 }