MALI: rockchip: upgrade midgard DDK to r11p0-00rel0
[firefly-linux-kernel-4.4.55.git] / drivers / gpu / arm / midgard / mali_kbase_jd.c
1 /*
2  *
3  * (C) COPYRIGHT 2010-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 #if defined(CONFIG_DMA_SHARED_BUFFER)
21 #include <linux/dma-buf.h>
22 #endif                          /* defined(CONFIG_DMA_SHARED_BUFFER) */
23 #ifdef CONFIG_COMPAT
24 #include <linux/compat.h>
25 #endif
26 #include <mali_kbase.h>
27 #include <mali_kbase_uku.h>
28 #include <linux/random.h>
29 #include <linux/version.h>
30 #include <linux/ratelimit.h>
31 #include <linux/pagemap.h>
32
33 #include <mali_kbase_jm.h>
34 #include <mali_kbase_hwaccess_jm.h>
35 #include <mali_kbase_tlstream.h>
36
37 #define beenthere(kctx, f, a...)  dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
38
39 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
40 /* random32 was renamed to prandom_u32 in 3.8 */
41 #define prandom_u32 random32
42 #endif
43
44 /* Return whether katom will run on the GPU or not. Currently only soft jobs and
45  * dependency-only atoms do not run on the GPU */
46 #define IS_GPU_ATOM(katom) (!((katom->core_req & BASE_JD_REQ_SOFT_JOB) ||  \
47                         ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) ==    \
48                                                         BASE_JD_REQ_DEP)))
49 /*
50  * This is the kernel side of the API. Only entry points are:
51  * - kbase_jd_submit(): Called from userspace to submit a single bag
52  * - kbase_jd_done(): Called from interrupt context to track the
53  *   completion of a job.
54  * Callouts:
55  * - to the job manager (enqueue a job)
56  * - to the event subsystem (signals the completion/failure of bag/job-chains).
57  */
58
59 static void __user *
60 get_compat_pointer(struct kbase_context *kctx, const union kbase_pointer *p)
61 {
62 #ifdef CONFIG_COMPAT
63         if (kctx->is_compat)
64                 return compat_ptr(p->compat_value);
65 #endif
66         return p->value;
67 }
68
69 /* Runs an atom, either by handing to the JS or by immediately running it in the case of soft-jobs
70  *
71  * Returns whether the JS needs a reschedule.
72  *
73  * Note that the caller must also check the atom status and
74  * if it is KBASE_JD_ATOM_STATE_COMPLETED must call jd_done_nolock
75  */
76 static int jd_run_atom(struct kbase_jd_atom *katom)
77 {
78         struct kbase_context *kctx = katom->kctx;
79
80         KBASE_DEBUG_ASSERT(katom->status != KBASE_JD_ATOM_STATE_UNUSED);
81
82         if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) == BASE_JD_REQ_DEP) {
83                 /* Dependency only atom */
84                 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
85                 return 0;
86         } else if (katom->core_req & BASE_JD_REQ_SOFT_JOB) {
87                 /* Soft-job */
88                 if (katom->will_fail_event_code) {
89                         katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
90                         return 0;
91                 }
92                 if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE)
93                                                   == BASE_JD_REQ_SOFT_REPLAY) {
94                         if (!kbase_replay_process(katom))
95                                 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
96                 } else if (kbase_process_soft_job(katom) == 0) {
97                         kbase_finish_soft_job(katom);
98                         katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
99                 }
100                 return 0;
101         }
102
103         katom->status = KBASE_JD_ATOM_STATE_IN_JS;
104         /* Queue an action about whether we should try scheduling a context */
105         return kbasep_js_add_job(kctx, katom);
106 }
107
108 #ifdef CONFIG_KDS
109
110 /* Add the katom to the kds waiting list.
111  * Atoms must be added to the waiting list after a successful call to kds_async_waitall.
112  * The caller must hold the kbase_jd_context.lock */
113
114 static void kbase_jd_kds_waiters_add(struct kbase_jd_atom *katom)
115 {
116         struct kbase_context *kctx;
117
118         KBASE_DEBUG_ASSERT(katom);
119
120         kctx = katom->kctx;
121
122         list_add_tail(&katom->node, &kctx->waiting_kds_resource);
123 }
124
125 /* Remove the katom from the kds waiting list.
126  * Atoms must be removed from the waiting list before a call to kds_resource_set_release_sync.
127  * The supplied katom must first have been added to the list with a call to kbase_jd_kds_waiters_add.
128  * The caller must hold the kbase_jd_context.lock */
129
130 static void kbase_jd_kds_waiters_remove(struct kbase_jd_atom *katom)
131 {
132         KBASE_DEBUG_ASSERT(katom);
133         list_del(&katom->node);
134 }
135
136 static void kds_dep_clear(void *callback_parameter, void *callback_extra_parameter)
137 {
138         struct kbase_jd_atom *katom;
139         struct kbase_jd_context *ctx;
140         struct kbase_device *kbdev;
141
142         katom = (struct kbase_jd_atom *)callback_parameter;
143         KBASE_DEBUG_ASSERT(katom);
144         ctx = &katom->kctx->jctx;
145         kbdev = katom->kctx->kbdev;
146         KBASE_DEBUG_ASSERT(kbdev);
147
148         mutex_lock(&ctx->lock);
149
150         /* KDS resource has already been satisfied (e.g. due to zapping) */
151         if (katom->kds_dep_satisfied)
152                 goto out;
153
154         /* This atom's KDS dependency has now been met */
155         katom->kds_dep_satisfied = true;
156
157         /* Check whether the atom's other dependencies were already met. If
158          * katom is a GPU atom then the job scheduler may be able to represent
159          * the dependencies, hence we may attempt to submit it before they are
160          * met. Other atoms must have had both dependencies resolved */
161         if (IS_GPU_ATOM(katom) ||
162                         (!kbase_jd_katom_dep_atom(&katom->dep[0]) &&
163                         !kbase_jd_katom_dep_atom(&katom->dep[1]))) {
164                 /* katom dep complete, attempt to run it */
165                 bool resched = false;
166
167                 resched = jd_run_atom(katom);
168
169                 if (katom->status == KBASE_JD_ATOM_STATE_COMPLETED) {
170                         /* The atom has already finished */
171                         resched |= jd_done_nolock(katom, NULL);
172                 }
173
174                 if (resched)
175                         kbase_js_sched_all(kbdev);
176         }
177  out:
178         mutex_unlock(&ctx->lock);
179 }
180
181 static void kbase_cancel_kds_wait_job(struct kbase_jd_atom *katom)
182 {
183         KBASE_DEBUG_ASSERT(katom);
184
185         /* Prevent job_done_nolock from being called twice on an atom when
186          *  there is a race between job completion and cancellation */
187
188         if (katom->status == KBASE_JD_ATOM_STATE_QUEUED) {
189                 /* Wait was cancelled - zap the atom */
190                 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
191                 if (jd_done_nolock(katom, NULL))
192                         kbase_js_sched_all(katom->kctx->kbdev);
193         }
194 }
195 #endif                          /* CONFIG_KDS */
196
197 void kbase_jd_free_external_resources(struct kbase_jd_atom *katom)
198 {
199 #ifdef CONFIG_KDS
200         if (katom->kds_rset) {
201                 struct kbase_jd_context *jctx = &katom->kctx->jctx;
202
203                 /*
204                  * As the atom is no longer waiting, remove it from
205                  * the waiting list.
206                  */
207
208                 mutex_lock(&jctx->lock);
209                 kbase_jd_kds_waiters_remove(katom);
210                 mutex_unlock(&jctx->lock);
211
212                 /* Release the kds resource or cancel if zapping */
213                 kds_resource_set_release_sync(&katom->kds_rset);
214         }
215 #endif                          /* CONFIG_KDS */
216 }
217
218 static void kbase_jd_post_external_resources(struct kbase_jd_atom *katom)
219 {
220         KBASE_DEBUG_ASSERT(katom);
221         KBASE_DEBUG_ASSERT(katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES);
222
223 #ifdef CONFIG_KDS
224         /* Prevent the KDS resource from triggering the atom in case of zapping */
225         if (katom->kds_rset)
226                 katom->kds_dep_satisfied = true;
227 #endif                          /* CONFIG_KDS */
228
229         kbase_gpu_vm_lock(katom->kctx);
230         /* only roll back if extres is non-NULL */
231         if (katom->extres) {
232                 u32 res_no;
233
234                 res_no = katom->nr_extres;
235                 while (res_no-- > 0) {
236                         struct kbase_mem_phy_alloc *alloc = katom->extres[res_no].alloc;
237                         struct kbase_va_region *reg;
238
239                         reg = kbase_region_tracker_find_region_base_address(
240                                         katom->kctx,
241                                         katom->extres[res_no].gpu_address);
242                         kbase_unmap_external_resource(katom->kctx, reg, alloc);
243                 }
244                 kfree(katom->extres);
245                 katom->extres = NULL;
246         }
247         kbase_gpu_vm_unlock(katom->kctx);
248 }
249
250 /*
251  * Set up external resources needed by this job.
252  *
253  * jctx.lock must be held when this is called.
254  */
255
256 static int kbase_jd_pre_external_resources(struct kbase_jd_atom *katom, const struct base_jd_atom_v2 *user_atom)
257 {
258         int err_ret_val = -EINVAL;
259         u32 res_no;
260 #ifdef CONFIG_KDS
261         u32 kds_res_count = 0;
262         struct kds_resource **kds_resources = NULL;
263         unsigned long *kds_access_bitmap = NULL;
264 #endif                          /* CONFIG_KDS */
265         struct base_external_resource *input_extres;
266
267         KBASE_DEBUG_ASSERT(katom);
268         KBASE_DEBUG_ASSERT(katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES);
269
270         /* no resources encoded, early out */
271         if (!katom->nr_extres)
272                 return -EINVAL;
273
274         katom->extres = kmalloc_array(katom->nr_extres, sizeof(*katom->extres), GFP_KERNEL);
275         if (NULL == katom->extres) {
276                 err_ret_val = -ENOMEM;
277                 goto early_err_out;
278         }
279
280         /* copy user buffer to the end of our real buffer.
281          * Make sure the struct sizes haven't changed in a way
282          * we don't support */
283         BUILD_BUG_ON(sizeof(*input_extres) > sizeof(*katom->extres));
284         input_extres = (struct base_external_resource *)
285                         (((unsigned char *)katom->extres) +
286                         (sizeof(*katom->extres) - sizeof(*input_extres)) *
287                         katom->nr_extres);
288
289         if (copy_from_user(input_extres,
290                         get_compat_pointer(katom->kctx, &user_atom->extres_list),
291                         sizeof(*input_extres) * katom->nr_extres) != 0) {
292                 err_ret_val = -EINVAL;
293                 goto early_err_out;
294         }
295 #ifdef CONFIG_KDS
296         /* assume we have to wait for all */
297         KBASE_DEBUG_ASSERT(0 != katom->nr_extres);
298         kds_resources = kmalloc_array(katom->nr_extres, sizeof(struct kds_resource *), GFP_KERNEL);
299
300         if (NULL == kds_resources) {
301                 err_ret_val = -ENOMEM;
302                 goto early_err_out;
303         }
304
305         KBASE_DEBUG_ASSERT(0 != katom->nr_extres);
306         kds_access_bitmap = kzalloc(sizeof(unsigned long) * ((katom->nr_extres + BITS_PER_LONG - 1) / BITS_PER_LONG), GFP_KERNEL);
307
308         if (NULL == kds_access_bitmap) {
309                 err_ret_val = -ENOMEM;
310                 goto early_err_out;
311         }
312 #endif                          /* CONFIG_KDS */
313
314         /* Take the processes mmap lock */
315         down_read(&current->mm->mmap_sem);
316
317         /* need to keep the GPU VM locked while we set up UMM buffers */
318         kbase_gpu_vm_lock(katom->kctx);
319         for (res_no = 0; res_no < katom->nr_extres; res_no++) {
320                 struct base_external_resource *res;
321                 struct kbase_va_region *reg;
322                 struct kbase_mem_phy_alloc *alloc;
323                 bool exclusive;
324
325                 res = &input_extres[res_no];
326                 exclusive = (res->ext_resource & BASE_EXT_RES_ACCESS_EXCLUSIVE)
327                                 ? true : false;
328                 reg = kbase_region_tracker_find_region_enclosing_address(
329                                 katom->kctx,
330                                 res->ext_resource & ~BASE_EXT_RES_ACCESS_EXCLUSIVE);
331                 /* did we find a matching region object? */
332                 if (NULL == reg || (reg->flags & KBASE_REG_FREE)) {
333                         /* roll back */
334                         goto failed_loop;
335                 }
336
337                 if (!(katom->core_req & BASE_JD_REQ_SOFT_JOB) &&
338                                 (reg->flags & KBASE_REG_SECURE)) {
339                         katom->atom_flags |= KBASE_KATOM_FLAG_SECURE;
340                 }
341
342                 alloc = kbase_map_external_resource(katom->kctx, reg,
343                                 current->mm
344 #ifdef CONFIG_KDS
345                                 , &kds_res_count, kds_resources,
346                                 kds_access_bitmap, exclusive
347 #endif
348                                 );
349                 if (!alloc) {
350                         err_ret_val = -EINVAL;
351                         goto failed_loop;
352                 }
353
354                 /* finish with updating out array with the data we found */
355                 /* NOTE: It is important that this is the last thing we do (or
356                  * at least not before the first write) as we overwrite elements
357                  * as we loop and could be overwriting ourself, so no writes
358                  * until the last read for an element.
359                  * */
360                 katom->extres[res_no].gpu_address = reg->start_pfn << PAGE_SHIFT; /* save the start_pfn (as an address, not pfn) to use fast lookup later */
361                 katom->extres[res_no].alloc = alloc;
362         }
363         /* successfully parsed the extres array */
364         /* drop the vm lock before we call into kds */
365         kbase_gpu_vm_unlock(katom->kctx);
366
367         /* Release the processes mmap lock */
368         up_read(&current->mm->mmap_sem);
369
370 #ifdef CONFIG_KDS
371         if (kds_res_count) {
372                 int wait_failed;
373
374                 /* We have resources to wait for with kds */
375                 katom->kds_dep_satisfied = false;
376
377                 wait_failed = kds_async_waitall(&katom->kds_rset,
378                                 &katom->kctx->jctx.kds_cb, katom, NULL,
379                                 kds_res_count, kds_access_bitmap,
380                                 kds_resources);
381
382                 if (wait_failed)
383                         goto failed_kds_setup;
384                 else
385                         kbase_jd_kds_waiters_add(katom);
386         } else {
387                 /* Nothing to wait for, so kds dep met */
388                 katom->kds_dep_satisfied = true;
389         }
390         kfree(kds_resources);
391         kfree(kds_access_bitmap);
392 #endif                          /* CONFIG_KDS */
393
394         /* all done OK */
395         return 0;
396
397 /* error handling section */
398
399 #ifdef CONFIG_KDS
400  failed_kds_setup:
401         /* Lock the processes mmap lock */
402         down_read(&current->mm->mmap_sem);
403
404         /* lock before we unmap */
405         kbase_gpu_vm_lock(katom->kctx);
406 #endif                          /* CONFIG_KDS */
407
408  failed_loop:
409         /* undo the loop work */
410         while (res_no-- > 0) {
411                 struct kbase_mem_phy_alloc *alloc = katom->extres[res_no].alloc;
412
413                 kbase_unmap_external_resource(katom->kctx, NULL, alloc);
414         }
415         kbase_gpu_vm_unlock(katom->kctx);
416
417         /* Release the processes mmap lock */
418         up_read(&current->mm->mmap_sem);
419
420  early_err_out:
421         kfree(katom->extres);
422         katom->extres = NULL;
423 #ifdef CONFIG_KDS
424         kfree(kds_resources);
425         kfree(kds_access_bitmap);
426 #endif                          /* CONFIG_KDS */
427         return err_ret_val;
428 }
429
430 static inline void jd_resolve_dep(struct list_head *out_list,
431                                         struct kbase_jd_atom *katom,
432                                         u8 d)
433 {
434         u8 other_d = !d;
435
436         while (!list_empty(&katom->dep_head[d])) {
437                 struct kbase_jd_atom *dep_atom;
438                 u8 dep_type;
439
440                 dep_atom = list_entry(katom->dep_head[d].next,
441                                 struct kbase_jd_atom, dep_item[d]);
442
443                 list_del(katom->dep_head[d].next);
444
445                 dep_type = kbase_jd_katom_dep_type(&dep_atom->dep[d]);
446                 kbase_jd_katom_dep_clear(&dep_atom->dep[d]);
447
448                 if (katom->event_code != BASE_JD_EVENT_DONE &&
449                         (dep_type != BASE_JD_DEP_TYPE_ORDER)) {
450 #ifdef CONFIG_KDS
451                         if (!dep_atom->kds_dep_satisfied) {
452                                 /* Just set kds_dep_satisfied to true. If the callback happens after this then it will early out and
453                                  * do nothing. If the callback doesn't happen then kbase_jd_post_external_resources will clean up
454                                  */
455                                 dep_atom->kds_dep_satisfied = true;
456                         }
457 #endif
458
459                         dep_atom->event_code = katom->event_code;
460                         KBASE_DEBUG_ASSERT(dep_atom->status !=
461                                                 KBASE_JD_ATOM_STATE_UNUSED);
462
463                         if ((dep_atom->core_req & BASE_JD_REQ_SOFT_REPLAY)
464                                         != BASE_JD_REQ_SOFT_REPLAY) {
465                                 dep_atom->will_fail_event_code =
466                                         dep_atom->event_code;
467                         } else {
468                                 dep_atom->status =
469                                         KBASE_JD_ATOM_STATE_COMPLETED;
470                         }
471                 }
472                 if (!kbase_jd_katom_dep_atom(&dep_atom->dep[other_d])) {
473 #ifdef CONFIG_KDS
474                         if (dep_atom->kds_dep_satisfied)
475 #endif
476                                 list_add_tail(&dep_atom->dep_item[0], out_list);
477                 }
478         }
479 }
480
481 KBASE_EXPORT_TEST_API(jd_resolve_dep);
482
483 #if MALI_CUSTOMER_RELEASE == 0
484 static void jd_force_failure(struct kbase_device *kbdev, struct kbase_jd_atom *katom)
485 {
486         kbdev->force_replay_count++;
487
488         if (kbdev->force_replay_count >= kbdev->force_replay_limit) {
489                 kbdev->force_replay_count = 0;
490                 katom->event_code = BASE_JD_EVENT_FORCE_REPLAY;
491
492                 if (kbdev->force_replay_random)
493                         kbdev->force_replay_limit =
494                            (prandom_u32() % KBASEP_FORCE_REPLAY_RANDOM_LIMIT) + 1;
495
496                 dev_info(kbdev->dev, "force_replay : promoting to error\n");
497         }
498 }
499
500 /** Test to see if atom should be forced to fail.
501  *
502  * This function will check if an atom has a replay job as a dependent. If so
503  * then it will be considered for forced failure. */
504 static void jd_check_force_failure(struct kbase_jd_atom *katom)
505 {
506         struct kbase_context *kctx = katom->kctx;
507         struct kbase_device *kbdev = kctx->kbdev;
508         int i;
509
510         if ((kbdev->force_replay_limit == KBASEP_FORCE_REPLAY_DISABLED) ||
511             (katom->core_req & BASEP_JD_REQ_EVENT_NEVER))
512                 return;
513
514         for (i = 1; i < BASE_JD_ATOM_COUNT; i++) {
515                 if (kbase_jd_katom_dep_atom(&kctx->jctx.atoms[i].dep[0]) == katom ||
516                     kbase_jd_katom_dep_atom(&kctx->jctx.atoms[i].dep[1]) == katom) {
517                         struct kbase_jd_atom *dep_atom = &kctx->jctx.atoms[i];
518
519                         if ((dep_atom->core_req & BASEP_JD_REQ_ATOM_TYPE) ==
520                                                      BASE_JD_REQ_SOFT_REPLAY &&
521                             (dep_atom->core_req & kbdev->force_replay_core_req)
522                                              == kbdev->force_replay_core_req) {
523                                 jd_force_failure(kbdev, katom);
524                                 return;
525                         }
526                 }
527         }
528 }
529 #endif
530
531 /*
532  * Perform the necessary handling of an atom that has finished running
533  * on the GPU.
534  *
535  * Note that if this is a soft-job that has had kbase_prepare_soft_job called on it then the caller
536  * is responsible for calling kbase_finish_soft_job *before* calling this function.
537  *
538  * The caller must hold the kbase_jd_context.lock.
539  */
540 bool jd_done_nolock(struct kbase_jd_atom *katom,
541                 struct list_head *completed_jobs_ctx)
542 {
543         struct kbase_context *kctx = katom->kctx;
544         struct kbase_device *kbdev = kctx->kbdev;
545         struct list_head completed_jobs;
546         struct list_head runnable_jobs;
547         bool need_to_try_schedule_context = false;
548         int i;
549
550         INIT_LIST_HEAD(&completed_jobs);
551         INIT_LIST_HEAD(&runnable_jobs);
552
553         KBASE_DEBUG_ASSERT(katom->status != KBASE_JD_ATOM_STATE_UNUSED);
554
555 #if MALI_CUSTOMER_RELEASE == 0
556         jd_check_force_failure(katom);
557 #endif
558
559         /* This is needed in case an atom is failed due to being invalid, this
560          * can happen *before* the jobs that the atom depends on have completed */
561         for (i = 0; i < 2; i++) {
562                 if (kbase_jd_katom_dep_atom(&katom->dep[i])) {
563                         list_del(&katom->dep_item[i]);
564                         kbase_jd_katom_dep_clear(&katom->dep[i]);
565                 }
566         }
567
568         /* With PRLAM-10817 or PRLAM-10959 the last tile of a fragment job being soft-stopped can fail with
569          * BASE_JD_EVENT_TILE_RANGE_FAULT.
570          *
571          * So here if the fragment job failed with TILE_RANGE_FAULT and it has been soft-stopped, then we promote the
572          * error code to BASE_JD_EVENT_DONE
573          */
574
575         if ((kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10817) || kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10959)) &&
576                   katom->event_code == BASE_JD_EVENT_TILE_RANGE_FAULT) {
577                 if ((katom->core_req & BASE_JD_REQ_FS) && (katom->atom_flags & KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED)) {
578                         /* Promote the failure to job done */
579                         katom->event_code = BASE_JD_EVENT_DONE;
580                         katom->atom_flags = katom->atom_flags & (~KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED);
581                 }
582         }
583
584         katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
585         list_add_tail(&katom->dep_item[0], &completed_jobs);
586
587         while (!list_empty(&completed_jobs)) {
588                 katom = list_entry(completed_jobs.prev, struct kbase_jd_atom, dep_item[0]);
589                 list_del(completed_jobs.prev);
590
591                 KBASE_DEBUG_ASSERT(katom->status == KBASE_JD_ATOM_STATE_COMPLETED);
592
593                 for (i = 0; i < 2; i++)
594                         jd_resolve_dep(&runnable_jobs, katom, i);
595
596                 if (katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES)
597                         kbase_jd_post_external_resources(katom);
598
599                 while (!list_empty(&runnable_jobs)) {
600                         struct kbase_jd_atom *node;
601
602                         node = list_entry(runnable_jobs.next,
603                                         struct kbase_jd_atom, dep_item[0]);
604
605                         list_del(runnable_jobs.next);
606
607                         KBASE_DEBUG_ASSERT(node->status != KBASE_JD_ATOM_STATE_UNUSED);
608
609                         if (node->status != KBASE_JD_ATOM_STATE_COMPLETED &&
610                                         !kctx->jctx.sched_info.ctx.is_dying) {
611                                 need_to_try_schedule_context |= jd_run_atom(node);
612                         } else {
613                                 node->event_code = katom->event_code;
614
615                                 if ((node->core_req & BASEP_JD_REQ_ATOM_TYPE)
616                                                   == BASE_JD_REQ_SOFT_REPLAY) {
617                                         if (kbase_replay_process(node))
618                                                 /* Don't complete this atom */
619                                                 continue;
620                                 } else if (node->core_req &
621                                                         BASE_JD_REQ_SOFT_JOB) {
622                                         /* If this is a fence wait then remove it from the list of sync waiters. */
623                                         if (BASE_JD_REQ_SOFT_FENCE_WAIT == node->core_req)
624                                                 list_del(&node->dep_item[0]);
625
626                                         kbase_finish_soft_job(node);
627                                 }
628                                 node->status = KBASE_JD_ATOM_STATE_COMPLETED;
629                         }
630
631                         if (node->status == KBASE_JD_ATOM_STATE_COMPLETED)
632                                 list_add_tail(&node->dep_item[0], &completed_jobs);
633                 }
634
635                 /* Completing an atom might have freed up space
636                  * in the ringbuffer, but only on that slot. */
637                 jsctx_ll_flush_to_rb(kctx,
638                                 katom->sched_priority,
639                                 katom->slot_nr);
640
641                 /* Register a completed job as a disjoint event when the GPU
642                  * is in a disjoint state (ie. being reset or replaying jobs).
643                  */
644                 kbase_disjoint_event_potential(kctx->kbdev);
645                 if (completed_jobs_ctx)
646                         list_add_tail(&katom->dep_item[0], completed_jobs_ctx);
647                 else
648                         kbase_event_post(kctx, katom);
649
650                 /* Decrement and check the TOTAL number of jobs. This includes
651                  * those not tracked by the scheduler: 'not ready to run' and
652                  * 'dependency-only' jobs. */
653                 if (--kctx->jctx.job_nr == 0)
654                         wake_up(&kctx->jctx.zero_jobs_wait);    /* All events are safely queued now, and we can signal any waiter
655                                                                  * that we've got no more jobs (so we can be safely terminated) */
656         }
657
658         return need_to_try_schedule_context;
659 }
660
661 KBASE_EXPORT_TEST_API(jd_done_nolock);
662
663 #ifdef CONFIG_GPU_TRACEPOINTS
664 enum {
665         CORE_REQ_DEP_ONLY,
666         CORE_REQ_SOFT,
667         CORE_REQ_COMPUTE,
668         CORE_REQ_FRAGMENT,
669         CORE_REQ_VERTEX,
670         CORE_REQ_TILER,
671         CORE_REQ_FRAGMENT_VERTEX,
672         CORE_REQ_FRAGMENT_VERTEX_TILER,
673         CORE_REQ_FRAGMENT_TILER,
674         CORE_REQ_VERTEX_TILER,
675         CORE_REQ_UNKNOWN
676 };
677 static const char * const core_req_strings[] = {
678         "Dependency Only Job",
679         "Soft Job",
680         "Compute Shader Job",
681         "Fragment Shader Job",
682         "Vertex/Geometry Shader Job",
683         "Tiler Job",
684         "Fragment Shader + Vertex/Geometry Shader Job",
685         "Fragment Shader + Vertex/Geometry Shader Job + Tiler Job",
686         "Fragment Shader + Tiler Job",
687         "Vertex/Geometry Shader Job + Tiler Job",
688         "Unknown Job"
689 };
690 static const char *kbasep_map_core_reqs_to_string(base_jd_core_req core_req)
691 {
692         if (core_req & BASE_JD_REQ_SOFT_JOB)
693                 return core_req_strings[CORE_REQ_SOFT];
694         if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
695                 return core_req_strings[CORE_REQ_COMPUTE];
696         switch (core_req & (BASE_JD_REQ_FS | BASE_JD_REQ_CS | BASE_JD_REQ_T)) {
697         case BASE_JD_REQ_DEP:
698                 return core_req_strings[CORE_REQ_DEP_ONLY];
699         case BASE_JD_REQ_FS:
700                 return core_req_strings[CORE_REQ_FRAGMENT];
701         case BASE_JD_REQ_CS:
702                 return core_req_strings[CORE_REQ_VERTEX];
703         case BASE_JD_REQ_T:
704                 return core_req_strings[CORE_REQ_TILER];
705         case (BASE_JD_REQ_FS | BASE_JD_REQ_CS):
706                 return core_req_strings[CORE_REQ_FRAGMENT_VERTEX];
707         case (BASE_JD_REQ_FS | BASE_JD_REQ_T):
708                 return core_req_strings[CORE_REQ_FRAGMENT_TILER];
709         case (BASE_JD_REQ_CS | BASE_JD_REQ_T):
710                 return core_req_strings[CORE_REQ_VERTEX_TILER];
711         case (BASE_JD_REQ_FS | BASE_JD_REQ_CS | BASE_JD_REQ_T):
712                 return core_req_strings[CORE_REQ_FRAGMENT_VERTEX_TILER];
713         }
714         return core_req_strings[CORE_REQ_UNKNOWN];
715 }
716 #endif
717
718 bool jd_submit_atom(struct kbase_context *kctx,
719                          const struct base_jd_atom_v2 *user_atom,
720                          struct kbase_jd_atom *katom)
721 {
722         struct kbase_jd_context *jctx = &kctx->jctx;
723         base_jd_core_req core_req;
724         int queued = 0;
725         int i;
726         int sched_prio;
727         bool ret;
728         bool will_fail = false;
729
730         /* Update the TOTAL number of jobs. This includes those not tracked by
731          * the scheduler: 'not ready to run' and 'dependency-only' jobs. */
732         jctx->job_nr++;
733
734         core_req = user_atom->core_req;
735
736         katom->start_timestamp.tv64 = 0;
737         katom->time_spent_us = 0;
738         katom->udata = user_atom->udata;
739         katom->kctx = kctx;
740         katom->nr_extres = user_atom->nr_extres;
741         katom->extres = NULL;
742         katom->device_nr = user_atom->device_nr;
743         katom->affinity = 0;
744         katom->jc = user_atom->jc;
745         katom->coreref_state = KBASE_ATOM_COREREF_STATE_NO_CORES_REQUESTED;
746         katom->core_req = core_req;
747         katom->atom_flags = 0;
748         katom->retry_count = 0;
749         katom->need_cache_flush_cores_retained = 0;
750         katom->x_pre_dep = NULL;
751         katom->x_post_dep = NULL;
752         katom->will_fail_event_code = 0;
753 #ifdef CONFIG_KDS
754         /* Start by assuming that the KDS dependencies are satisfied,
755          * kbase_jd_pre_external_resources will correct this if there are dependencies */
756         katom->kds_dep_satisfied = true;
757         katom->kds_rset = NULL;
758 #endif                          /* CONFIG_KDS */
759
760         /* Don't do anything if there is a mess up with dependencies.
761            This is done in a separate cycle to check both the dependencies at ones, otherwise
762            it will be extra complexity to deal with 1st dependency ( just added to the list )
763            if only the 2nd one has invalid config.
764          */
765         for (i = 0; i < 2; i++) {
766                 int dep_atom_number = user_atom->pre_dep[i].atom_id;
767                 base_jd_dep_type dep_atom_type = user_atom->pre_dep[i].dependency_type;
768
769                 if (dep_atom_number) {
770                         if (dep_atom_type != BASE_JD_DEP_TYPE_ORDER &&
771                                         dep_atom_type != BASE_JD_DEP_TYPE_DATA) {
772                                 katom->event_code = BASE_JD_EVENT_JOB_CONFIG_FAULT;
773                                 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
774
775                                 /* Wrong dependency setup. Atom will be sent
776                                  * back to user space. Do not record any
777                                  * dependencies. */
778                                 kbase_tlstream_tl_new_atom(
779                                                 katom,
780                                                 kbase_jd_atom_id(kctx, katom));
781                                 kbase_tlstream_tl_ret_atom_ctx(
782                                                 katom, kctx);
783
784                                 ret = jd_done_nolock(katom, NULL);
785                                 goto out;
786                         }
787                 }
788         }
789
790         /* Add dependencies */
791         for (i = 0; i < 2; i++) {
792                 int dep_atom_number = user_atom->pre_dep[i].atom_id;
793                 base_jd_dep_type dep_atom_type;
794                 struct kbase_jd_atom *dep_atom = &jctx->atoms[dep_atom_number];
795
796                 dep_atom_type = user_atom->pre_dep[i].dependency_type;
797                 kbase_jd_katom_dep_clear(&katom->dep[i]);
798
799                 if (!dep_atom_number)
800                         continue;
801
802                 if (dep_atom->status == KBASE_JD_ATOM_STATE_UNUSED ||
803                                 dep_atom->status == KBASE_JD_ATOM_STATE_COMPLETED) {
804
805                         if (dep_atom->event_code == BASE_JD_EVENT_DONE)
806                                 continue;
807                         /* don't stop this atom if it has an order dependency
808                          * only to the failed one, try to submit it through
809                          * the normal path
810                          */
811                         if (dep_atom_type == BASE_JD_DEP_TYPE_ORDER &&
812                                         dep_atom->event_code > BASE_JD_EVENT_ACTIVE) {
813                                 continue;
814                         }
815
816                         /* Atom has completed, propagate the error code if any */
817                         katom->event_code = dep_atom->event_code;
818                         katom->status = KBASE_JD_ATOM_STATE_QUEUED;
819
820                         /* This atom is going through soft replay or
821                          * will be sent back to user space. Do not record any
822                          * dependencies. */
823                         kbase_tlstream_tl_new_atom(
824                                         katom,
825                                         kbase_jd_atom_id(kctx, katom));
826                         kbase_tlstream_tl_ret_atom_ctx(katom, kctx);
827
828                         if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE)
829                                          == BASE_JD_REQ_SOFT_REPLAY) {
830                                 if (kbase_replay_process(katom)) {
831                                         ret = false;
832                                         goto out;
833                                 }
834                         }
835                         will_fail = true;
836
837                 } else {
838                         /* Atom is in progress, add this atom to the list */
839                         list_add_tail(&katom->dep_item[i], &dep_atom->dep_head[i]);
840                         kbase_jd_katom_dep_set(&katom->dep[i], dep_atom, dep_atom_type);
841                         queued = 1;
842                 }
843         }
844
845         if (will_fail) {
846                 if (!queued) {
847                         ret = jd_done_nolock(katom, NULL);
848
849                         goto out;
850                 } else {
851                         katom->will_fail_event_code = katom->event_code;
852                         ret = false;
853
854                         goto out;
855                 }
856         } else {
857                 /* These must occur after the above loop to ensure that an atom
858                  * that depends on a previous atom with the same number behaves
859                  * as expected */
860                 katom->event_code = BASE_JD_EVENT_DONE;
861                 katom->status = KBASE_JD_ATOM_STATE_QUEUED;
862         }
863
864         /* Create a new atom recording all dependencies it was set up with. */
865         kbase_tlstream_tl_new_atom(
866                         katom,
867                         kbase_jd_atom_id(kctx, katom));
868         kbase_tlstream_tl_ret_atom_ctx(katom, kctx);
869         for (i = 0; i < 2; i++)
870                 if (BASE_JD_DEP_TYPE_INVALID != kbase_jd_katom_dep_type(
871                                         &katom->dep[i]))
872                         kbase_tlstream_tl_dep_atom_atom(
873                                         (void *)kbase_jd_katom_dep_atom(
874                                                 &katom->dep[i]),
875                                         (void *)katom);
876
877         /* Reject atoms with job chain = NULL, as these cause issues with soft-stop */
878         if (!katom->jc && (katom->core_req & BASEP_JD_REQ_ATOM_TYPE) != BASE_JD_REQ_DEP) {
879                 dev_warn(kctx->kbdev->dev, "Rejecting atom with jc = NULL");
880                 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
881                 ret = jd_done_nolock(katom, NULL);
882                 goto out;
883         }
884
885         /* Reject atoms with an invalid device_nr */
886         if ((katom->core_req & BASE_JD_REQ_SPECIFIC_COHERENT_GROUP) &&
887             (katom->device_nr >= kctx->kbdev->gpu_props.num_core_groups)) {
888                 dev_warn(kctx->kbdev->dev,
889                                 "Rejecting atom with invalid device_nr %d",
890                                 katom->device_nr);
891                 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
892                 ret = jd_done_nolock(katom, NULL);
893                 goto out;
894         }
895
896         /* For invalid priority, be most lenient and choose the default */
897         sched_prio = kbasep_js_atom_prio_to_sched_prio(user_atom->prio);
898         if (sched_prio == KBASE_JS_ATOM_SCHED_PRIO_INVALID)
899                 sched_prio = KBASE_JS_ATOM_SCHED_PRIO_DEFAULT;
900         katom->sched_priority = sched_prio;
901
902         if (katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES) {
903                 /* handle what we need to do to access the external resources */
904                 if (kbase_jd_pre_external_resources(katom, user_atom) != 0) {
905                         /* setup failed (no access, bad resource, unknown resource types, etc.) */
906                         katom->event_code = BASE_JD_EVENT_JOB_INVALID;
907                         ret = jd_done_nolock(katom, NULL);
908                         goto out;
909                 }
910         }
911
912         /* Validate the atom. Function will return error if the atom is
913          * malformed.
914          *
915          * Soft-jobs never enter the job scheduler but have their own initialize method.
916          *
917          * If either fail then we immediately complete the atom with an error.
918          */
919         if ((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0) {
920                 if (!kbase_js_is_atom_valid(kctx->kbdev, katom)) {
921                         katom->event_code = BASE_JD_EVENT_JOB_INVALID;
922                         ret = jd_done_nolock(katom, NULL);
923                         goto out;
924                 }
925         } else {
926                 /* Soft-job */
927                 if (kbase_prepare_soft_job(katom) != 0) {
928                         katom->event_code = BASE_JD_EVENT_JOB_INVALID;
929                         ret = jd_done_nolock(katom, NULL);
930                         goto out;
931                 }
932         }
933
934 #ifdef CONFIG_GPU_TRACEPOINTS
935         katom->work_id = atomic_inc_return(&jctx->work_id);
936         trace_gpu_job_enqueue((u32)kctx->id, katom->work_id,
937                         kbasep_map_core_reqs_to_string(katom->core_req));
938 #endif
939
940         if (queued && !IS_GPU_ATOM(katom)) {
941                 ret = false;
942                 goto out;
943         }
944 #ifdef CONFIG_KDS
945         if (!katom->kds_dep_satisfied) {
946                 /* Queue atom due to KDS dependency */
947                 ret = false;
948                 goto out;
949         }
950 #endif                          /* CONFIG_KDS */
951
952         if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE)
953                                                   == BASE_JD_REQ_SOFT_REPLAY) {
954                 if (kbase_replay_process(katom))
955                         ret = false;
956                 else
957                         ret = jd_done_nolock(katom, NULL);
958
959                 goto out;
960         } else if (katom->core_req & BASE_JD_REQ_SOFT_JOB) {
961                 if (kbase_process_soft_job(katom) == 0) {
962                         kbase_finish_soft_job(katom);
963                         ret = jd_done_nolock(katom, NULL);
964                         goto out;
965                 }
966
967                 ret = false;
968         } else if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) != BASE_JD_REQ_DEP) {
969                 katom->status = KBASE_JD_ATOM_STATE_IN_JS;
970                 ret = kbasep_js_add_job(kctx, katom);
971                 /* If job was cancelled then resolve immediately */
972                 if (katom->event_code == BASE_JD_EVENT_JOB_CANCELLED)
973                         ret = jd_done_nolock(katom, NULL);
974         } else {
975                 /* This is a pure dependency. Resolve it immediately */
976                 ret = jd_done_nolock(katom, NULL);
977         }
978
979  out:
980         return ret;
981 }
982
983 #ifdef BASE_LEGACY_UK6_SUPPORT
984 int kbase_jd_submit(struct kbase_context *kctx,
985                 const struct kbase_uk_job_submit *submit_data,
986                 int uk6_atom)
987 #else
988 int kbase_jd_submit(struct kbase_context *kctx,
989                 const struct kbase_uk_job_submit *submit_data)
990 #endif /* BASE_LEGACY_UK6_SUPPORT */
991 {
992         struct kbase_jd_context *jctx = &kctx->jctx;
993         int err = 0;
994         int i;
995         bool need_to_try_schedule_context = false;
996         struct kbase_device *kbdev;
997         void __user *user_addr;
998         u32 latest_flush;
999
1000         /*
1001          * kbase_jd_submit isn't expected to fail and so all errors with the jobs
1002          * are reported by immediately falling them (through event system)
1003          */
1004         kbdev = kctx->kbdev;
1005
1006         beenthere(kctx, "%s", "Enter");
1007
1008         if ((kctx->jctx.sched_info.ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) != 0) {
1009                 dev_err(kbdev->dev, "Attempt to submit to a context that has SUBMIT_DISABLED set on it");
1010                 return -EINVAL;
1011         }
1012
1013 #ifdef BASE_LEGACY_UK6_SUPPORT
1014         if ((uk6_atom && submit_data->stride !=
1015                         sizeof(struct base_jd_atom_v2_uk6)) ||
1016                         submit_data->stride != sizeof(base_jd_atom_v2)) {
1017 #else
1018         if (submit_data->stride != sizeof(base_jd_atom_v2)) {
1019 #endif /* BASE_LEGACY_UK6_SUPPORT */
1020                 dev_err(kbdev->dev, "Stride passed to job_submit doesn't match kernel");
1021                 return -EINVAL;
1022         }
1023
1024         user_addr = get_compat_pointer(kctx, &submit_data->addr);
1025
1026         KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx, atomic_add_return(submit_data->nr_atoms, &kctx->timeline.jd_atoms_in_flight));
1027
1028         /* All atoms submitted in this call have the same flush ID */
1029         latest_flush = kbase_backend_get_current_flush_id(kbdev);
1030
1031         for (i = 0; i < submit_data->nr_atoms; i++) {
1032                 struct base_jd_atom_v2 user_atom;
1033                 struct kbase_jd_atom *katom;
1034
1035 #ifdef BASE_LEGACY_UK6_SUPPORT
1036                 if (uk6_atom) {
1037                         struct base_jd_atom_v2_uk6 user_atom_v6;
1038                         base_jd_dep_type dep_types[2] = {BASE_JD_DEP_TYPE_DATA, BASE_JD_DEP_TYPE_DATA};
1039
1040                         if (copy_from_user(&user_atom_v6, user_addr,
1041                                         sizeof(user_atom_v6))) {
1042                                 err = -EINVAL;
1043                                 KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx,
1044                                         atomic_sub_return(
1045                                         submit_data->nr_atoms - i,
1046                                         &kctx->timeline.jd_atoms_in_flight));
1047                                 break;
1048                         }
1049                         /* Convert from UK6 atom format to UK7 format */
1050                         user_atom.jc = user_atom_v6.jc;
1051                         user_atom.udata = user_atom_v6.udata;
1052                         user_atom.extres_list = user_atom_v6.extres_list;
1053                         user_atom.nr_extres = user_atom_v6.nr_extres;
1054                         user_atom.core_req = user_atom_v6.core_req;
1055
1056                         /* atom number 0 is used for no dependency atoms */
1057                         if (!user_atom_v6.pre_dep[0])
1058                                 dep_types[0] = BASE_JD_DEP_TYPE_INVALID;
1059
1060                         base_jd_atom_dep_set(&user_atom.pre_dep[0],
1061                                         user_atom_v6.pre_dep[0],
1062                                         dep_types[0]);
1063
1064                         /* atom number 0 is used for no dependency atoms */
1065                         if (!user_atom_v6.pre_dep[1])
1066                                 dep_types[1] = BASE_JD_DEP_TYPE_INVALID;
1067
1068                         base_jd_atom_dep_set(&user_atom.pre_dep[1],
1069                                         user_atom_v6.pre_dep[1],
1070                                         dep_types[1]);
1071
1072                         user_atom.atom_number = user_atom_v6.atom_number;
1073                         user_atom.prio = user_atom_v6.prio;
1074                         user_atom.device_nr = user_atom_v6.device_nr;
1075                 } else {
1076 #endif /* BASE_LEGACY_UK6_SUPPORT */
1077                 if (copy_from_user(&user_atom, user_addr, sizeof(user_atom)) != 0) {
1078                         err = -EINVAL;
1079                         KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx, atomic_sub_return(submit_data->nr_atoms - i, &kctx->timeline.jd_atoms_in_flight));
1080                         break;
1081                 }
1082 #ifdef BASE_LEGACY_UK6_SUPPORT
1083                 }
1084 #endif /* BASE_LEGACY_UK6_SUPPORT */
1085
1086                 user_addr = (void __user *)((uintptr_t) user_addr + submit_data->stride);
1087
1088                 mutex_lock(&jctx->lock);
1089 #ifndef compiletime_assert
1090 #define compiletime_assert_defined
1091 #define compiletime_assert(x, msg) do { switch (0) { case 0: case (x):; } } \
1092 while (false)
1093 #endif
1094                 compiletime_assert((1 << (8*sizeof(user_atom.atom_number))) ==
1095                                         BASE_JD_ATOM_COUNT,
1096                         "BASE_JD_ATOM_COUNT and base_atom_id type out of sync");
1097                 compiletime_assert(sizeof(user_atom.pre_dep[0].atom_id) ==
1098                                         sizeof(user_atom.atom_number),
1099                         "BASE_JD_ATOM_COUNT and base_atom_id type out of sync");
1100 #ifdef compiletime_assert_defined
1101 #undef compiletime_assert
1102 #undef compiletime_assert_defined
1103 #endif
1104                 katom = &jctx->atoms[user_atom.atom_number];
1105
1106                 /* Record the flush ID for the cache flush optimisation */
1107                 katom->flush_id = latest_flush;
1108
1109                 while (katom->status != KBASE_JD_ATOM_STATE_UNUSED) {
1110                         /* Atom number is already in use, wait for the atom to
1111                          * complete
1112                          */
1113                         mutex_unlock(&jctx->lock);
1114
1115                         /* This thread will wait for the atom to complete. Due
1116                          * to thread scheduling we are not sure that the other
1117                          * thread that owns the atom will also schedule the
1118                          * context, so we force the scheduler to be active and
1119                          * hence eventually schedule this context at some point
1120                          * later.
1121                          */
1122                         kbase_js_sched_all(kbdev);
1123
1124                         if (wait_event_killable(katom->completed,
1125                                         katom->status ==
1126                                         KBASE_JD_ATOM_STATE_UNUSED) != 0) {
1127                                 /* We're being killed so the result code
1128                                  * doesn't really matter
1129                                  */
1130                                 return 0;
1131                         }
1132                         mutex_lock(&jctx->lock);
1133                 }
1134
1135                 need_to_try_schedule_context |=
1136                                        jd_submit_atom(kctx, &user_atom, katom);
1137
1138                 /* Register a completed job as a disjoint event when the GPU is in a disjoint state
1139                  * (ie. being reset or replaying jobs).
1140                  */
1141                 kbase_disjoint_event_potential(kbdev);
1142
1143                 mutex_unlock(&jctx->lock);
1144         }
1145
1146         if (need_to_try_schedule_context)
1147                 kbase_js_sched_all(kbdev);
1148
1149         return err;
1150 }
1151
1152 KBASE_EXPORT_TEST_API(kbase_jd_submit);
1153
1154 void kbase_jd_done_worker(struct work_struct *data)
1155 {
1156         struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom, work);
1157         struct kbase_jd_context *jctx;
1158         struct kbase_context *kctx;
1159         struct kbasep_js_kctx_info *js_kctx_info;
1160         union kbasep_js_policy *js_policy;
1161         struct kbase_device *kbdev;
1162         struct kbasep_js_device_data *js_devdata;
1163         u64 cache_jc = katom->jc;
1164         struct kbasep_js_atom_retained_state katom_retained_state;
1165         bool schedule = false;
1166         bool context_idle;
1167         base_jd_core_req core_req = katom->core_req;
1168         u64 affinity = katom->affinity;
1169         enum kbase_atom_coreref_state coreref_state = katom->coreref_state;
1170
1171         /* Soft jobs should never reach this function */
1172         KBASE_DEBUG_ASSERT((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0);
1173
1174         kctx = katom->kctx;
1175         jctx = &kctx->jctx;
1176         kbdev = kctx->kbdev;
1177         js_kctx_info = &kctx->jctx.sched_info;
1178         js_devdata = &kbdev->js_data;
1179         js_policy = &kbdev->js_data.policy;
1180
1181         KBASE_TRACE_ADD(kbdev, JD_DONE_WORKER, kctx, katom, katom->jc, 0);
1182
1183         kbase_backend_complete_wq(kbdev, katom);
1184
1185         /*
1186          * Begin transaction on JD context and JS context
1187          */
1188         mutex_lock(&jctx->lock);
1189         mutex_lock(&js_devdata->queue_mutex);
1190         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1191
1192         /* This worker only gets called on contexts that are scheduled *in*. This is
1193          * because it only happens in response to an IRQ from a job that was
1194          * running.
1195          */
1196         KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
1197
1198         if (katom->event_code == BASE_JD_EVENT_STOPPED) {
1199                 /* Atom has been promoted to stopped */
1200                 unsigned long flags;
1201
1202                 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1203                 mutex_unlock(&js_devdata->queue_mutex);
1204
1205                 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1206
1207                 katom->status = KBASE_JD_ATOM_STATE_IN_JS;
1208                 kbase_js_unpull(kctx, katom);
1209
1210                 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1211                 mutex_unlock(&jctx->lock);
1212
1213                 return;
1214         }
1215
1216         if (katom->event_code != BASE_JD_EVENT_DONE)
1217                 dev_err(kbdev->dev,
1218                         "t6xx: GPU fault 0x%02lx from job slot %d\n",
1219                                         (unsigned long)katom->event_code,
1220                                                                 katom->slot_nr);
1221
1222         if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
1223                 kbase_as_poking_timer_release_atom(kbdev, kctx, katom);
1224
1225         /* Retain state before the katom disappears */
1226         kbasep_js_atom_retained_state_copy(&katom_retained_state, katom);
1227
1228         if (!kbasep_js_has_atom_finished(&katom_retained_state)) {
1229                 mutex_lock(&js_devdata->runpool_mutex);
1230                 kbasep_js_clear_job_retry_submit(katom);
1231                 /* An atom that has been hard-stopped might have previously
1232                  * been soft-stopped and has just finished before the hard-stop
1233                  * occurred. For this reason, clear the hard-stopped flag */
1234                 katom->atom_flags &= ~(KBASE_KATOM_FLAG_BEEN_HARD_STOPPED);
1235                 mutex_unlock(&js_devdata->runpool_mutex);
1236         }
1237
1238         if (kbasep_js_has_atom_finished(&katom_retained_state))
1239                 schedule = true;
1240
1241         context_idle = kbase_js_complete_atom_wq(kctx, katom);
1242
1243         KBASE_DEBUG_ASSERT(kbasep_js_has_atom_finished(&katom_retained_state));
1244
1245         kbasep_js_remove_job(kbdev, kctx, katom);
1246         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1247         mutex_unlock(&js_devdata->queue_mutex);
1248         katom->atom_flags &= ~KBASE_KATOM_FLAG_HOLDING_CTX_REF;
1249         /* jd_done_nolock() requires the jsctx_mutex lock to be dropped */
1250         schedule |= jd_done_nolock(katom, &kctx->completed_jobs);
1251
1252         /* katom may have been freed now, do not use! */
1253
1254         if (context_idle) {
1255                 unsigned long flags;
1256
1257                 mutex_lock(&js_devdata->queue_mutex);
1258                 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1259
1260                 /* If kbase_sched() has scheduled this context back in then
1261                  * ctx_active will have been set after we marked it as inactive,
1262                  * and another pm reference will have been taken, so drop our
1263                  * reference. But do not call kbase_jm_idle_ctx(), as the
1264                  * context is active and fast-starting is allowed.
1265                  *
1266                  * If an atom has been fast-started then kctx->atoms_pulled will
1267                  * be non-zero but ctx_active will still be false (as the
1268                  * previous pm reference has been inherited). Do NOT drop our
1269                  * reference, as it has been re-used, and leave the context as
1270                  * active.
1271                  *
1272                  * If no new atoms have been started then ctx_active will still
1273                  * be false and atoms_pulled will be zero, so drop the reference
1274                  * and call kbase_jm_idle_ctx().
1275                  *
1276                  * As the checks are done under both the queue_mutex and
1277                  * runpool_irq.lock is should be impossible for this to race
1278                  * with the scheduler code.
1279                  */
1280                 if (kctx->ctx_active || !atomic_read(&kctx->atoms_pulled)) {
1281                         /* Calling kbase_jm_idle_ctx() here will ensure that
1282                          * atoms are not fast-started when we drop the
1283                          * runpool_irq.lock. This is not performed if ctx_active
1284                          * is set as in that case another pm reference has been
1285                          * taken and a fast-start would be valid.
1286                          */
1287                         if (!kctx->ctx_active)
1288                                 kbase_jm_idle_ctx(kbdev, kctx);
1289                         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
1290                                         flags);
1291
1292                         kbase_pm_context_idle(kbdev);
1293                 } else {
1294                         kctx->ctx_active = true;
1295                         spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
1296                                         flags);
1297                 }
1298                 mutex_unlock(&js_devdata->queue_mutex);
1299         }
1300
1301         /*
1302          * Transaction complete
1303          */
1304         mutex_unlock(&jctx->lock);
1305
1306         /* Job is now no longer running, so can now safely release the context
1307          * reference, and handle any actions that were logged against the atom's retained state */
1308
1309         kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx, &katom_retained_state);
1310
1311         if (schedule)
1312                 kbase_js_sched_all(kbdev);
1313
1314         if (!atomic_dec_return(&kctx->work_count)) {
1315                 /* If worker now idle then post all events that jd_done_nolock()
1316                  * has queued */
1317                 mutex_lock(&jctx->lock);
1318                 while (!list_empty(&kctx->completed_jobs)) {
1319                         struct kbase_jd_atom *atom = list_entry(
1320                                         kctx->completed_jobs.next,
1321                                         struct kbase_jd_atom, dep_item[0]);
1322                         list_del(kctx->completed_jobs.next);
1323
1324                         kbase_event_post(kctx, atom);
1325                 }
1326                 mutex_unlock(&jctx->lock);
1327         }
1328
1329         kbase_backend_complete_wq_post_sched(kbdev, core_req, affinity,
1330                         coreref_state);
1331
1332         KBASE_TRACE_ADD(kbdev, JD_DONE_WORKER_END, kctx, NULL, cache_jc, 0);
1333 }
1334
1335 /**
1336  * jd_cancel_worker - Work queue job cancel function.
1337  * @data: a &struct work_struct
1338  *
1339  * Only called as part of 'Zapping' a context (which occurs on termination).
1340  * Operates serially with the kbase_jd_done_worker() on the work queue.
1341  *
1342  * This can only be called on contexts that aren't scheduled.
1343  *
1344  * We don't need to release most of the resources that would occur on
1345  * kbase_jd_done() or kbase_jd_done_worker(), because the atoms here must not be
1346  * running (by virtue of only being called on contexts that aren't
1347  * scheduled).
1348  */
1349 static void jd_cancel_worker(struct work_struct *data)
1350 {
1351         struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom, work);
1352         struct kbase_jd_context *jctx;
1353         struct kbase_context *kctx;
1354         struct kbasep_js_kctx_info *js_kctx_info;
1355         bool need_to_try_schedule_context;
1356         bool attr_state_changed;
1357         struct kbase_device *kbdev;
1358
1359         /* Soft jobs should never reach this function */
1360         KBASE_DEBUG_ASSERT((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0);
1361
1362         kctx = katom->kctx;
1363         kbdev = kctx->kbdev;
1364         jctx = &kctx->jctx;
1365         js_kctx_info = &kctx->jctx.sched_info;
1366
1367         KBASE_TRACE_ADD(kbdev, JD_CANCEL_WORKER, kctx, katom, katom->jc, 0);
1368
1369         /* This only gets called on contexts that are scheduled out. Hence, we must
1370          * make sure we don't de-ref the number of running jobs (there aren't
1371          * any), nor must we try to schedule out the context (it's already
1372          * scheduled out).
1373          */
1374         KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1375
1376         /* Scheduler: Remove the job from the system */
1377         mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1378         attr_state_changed = kbasep_js_remove_cancelled_job(kbdev, kctx, katom);
1379         mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1380
1381         mutex_lock(&jctx->lock);
1382
1383         need_to_try_schedule_context = jd_done_nolock(katom, NULL);
1384         /* Because we're zapping, we're not adding any more jobs to this ctx, so no need to
1385          * schedule the context. There's also no need for the jsctx_mutex to have been taken
1386          * around this too. */
1387         KBASE_DEBUG_ASSERT(!need_to_try_schedule_context);
1388
1389         /* katom may have been freed now, do not use! */
1390         mutex_unlock(&jctx->lock);
1391
1392         if (attr_state_changed)
1393                 kbase_js_sched_all(kbdev);
1394 }
1395
1396 /**
1397  * kbase_jd_done - Complete a job that has been removed from the Hardware
1398  * @katom: atom which has been completed
1399  * @slot_nr: slot the atom was on
1400  * @end_timestamp: completion time
1401  * @done_code: completion code
1402  *
1403  * This must be used whenever a job has been removed from the Hardware, e.g.:
1404  * An IRQ indicates that the job finished (for both error and 'done' codes), or
1405  * the job was evicted from the JS_HEAD_NEXT registers during a Soft/Hard stop.
1406  *
1407  * Some work is carried out immediately, and the rest is deferred onto a
1408  * workqueue
1409  *
1410  * Context:
1411  *   This can be called safely from atomic context.
1412  *   The caller must hold kbasep_js_device_data.runpool_irq.lock
1413  */
1414 void kbase_jd_done(struct kbase_jd_atom *katom, int slot_nr,
1415                 ktime_t *end_timestamp, kbasep_js_atom_done_code done_code)
1416 {
1417         struct kbase_context *kctx;
1418         struct kbase_device *kbdev;
1419
1420         KBASE_DEBUG_ASSERT(katom);
1421         kctx = katom->kctx;
1422         KBASE_DEBUG_ASSERT(kctx);
1423         kbdev = kctx->kbdev;
1424         KBASE_DEBUG_ASSERT(kbdev);
1425
1426         if (done_code & KBASE_JS_ATOM_DONE_EVICTED_FROM_NEXT)
1427                 katom->event_code = BASE_JD_EVENT_REMOVED_FROM_NEXT;
1428
1429         KBASE_TRACE_ADD(kbdev, JD_DONE, kctx, katom, katom->jc, 0);
1430
1431         kbase_job_check_leave_disjoint(kbdev, katom);
1432
1433         katom->slot_nr = slot_nr;
1434
1435         atomic_inc(&kctx->work_count);
1436
1437 #ifdef CONFIG_DEBUG_FS
1438         /* a failed job happened and is waiting for dumping*/
1439         if (!katom->will_fail_event_code &&
1440                         kbase_debug_job_fault_process(katom, katom->event_code))
1441                 return;
1442 #endif
1443
1444         WARN_ON(work_pending(&katom->work));
1445         KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
1446         INIT_WORK(&katom->work, kbase_jd_done_worker);
1447         queue_work(kctx->jctx.job_done_wq, &katom->work);
1448 }
1449
1450 KBASE_EXPORT_TEST_API(kbase_jd_done);
1451
1452 void kbase_jd_cancel(struct kbase_device *kbdev, struct kbase_jd_atom *katom)
1453 {
1454         struct kbase_context *kctx;
1455         struct kbasep_js_kctx_info *js_kctx_info;
1456
1457         KBASE_DEBUG_ASSERT(NULL != kbdev);
1458         KBASE_DEBUG_ASSERT(NULL != katom);
1459         kctx = katom->kctx;
1460         KBASE_DEBUG_ASSERT(NULL != kctx);
1461
1462         js_kctx_info = &kctx->jctx.sched_info;
1463
1464         KBASE_TRACE_ADD(kbdev, JD_CANCEL, kctx, katom, katom->jc, 0);
1465
1466         /* This should only be done from a context that is not scheduled */
1467         KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1468
1469         WARN_ON(work_pending(&katom->work));
1470
1471         katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
1472
1473         KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
1474         INIT_WORK(&katom->work, jd_cancel_worker);
1475         queue_work(kctx->jctx.job_done_wq, &katom->work);
1476 }
1477
1478
1479 void kbase_jd_zap_context(struct kbase_context *kctx)
1480 {
1481         struct kbase_jd_atom *katom;
1482         struct list_head *entry, *tmp;
1483         struct kbase_device *kbdev;
1484
1485         KBASE_DEBUG_ASSERT(kctx);
1486
1487         kbdev = kctx->kbdev;
1488
1489         KBASE_TRACE_ADD(kbdev, JD_ZAP_CONTEXT, kctx, NULL, 0u, 0u);
1490
1491         kbase_js_zap_context(kctx);
1492
1493         mutex_lock(&kctx->jctx.lock);
1494
1495         /*
1496          * While holding the struct kbase_jd_context lock clean up jobs which are known to kbase but are
1497          * queued outside the job scheduler.
1498          */
1499
1500         hrtimer_cancel(&kctx->soft_event_timeout);
1501         list_for_each_safe(entry, tmp, &kctx->waiting_soft_jobs) {
1502                 katom = list_entry(entry, struct kbase_jd_atom, dep_item[0]);
1503                 kbase_cancel_soft_job(katom);
1504         }
1505
1506
1507 #ifdef CONFIG_KDS
1508
1509         /* For each job waiting on a kds resource, cancel the wait and force the job to
1510          * complete early, this is done so that we don't leave jobs outstanding waiting
1511          * on kds resources which may never be released when contexts are zapped, resulting
1512          * in a hang.
1513          *
1514          * Note that we can safely iterate over the list as the struct kbase_jd_context lock is held,
1515          * this prevents items being removed when calling job_done_nolock in kbase_cancel_kds_wait_job.
1516          */
1517
1518         list_for_each(entry, &kctx->waiting_kds_resource) {
1519                 katom = list_entry(entry, struct kbase_jd_atom, node);
1520
1521                 kbase_cancel_kds_wait_job(katom);
1522         }
1523 #endif
1524
1525         mutex_unlock(&kctx->jctx.lock);
1526
1527         kbase_jm_wait_for_zero_jobs(kctx);
1528 }
1529
1530 KBASE_EXPORT_TEST_API(kbase_jd_zap_context);
1531
1532 int kbase_jd_init(struct kbase_context *kctx)
1533 {
1534         int i;
1535         int mali_err = 0;
1536 #ifdef CONFIG_KDS
1537         int err;
1538 #endif                          /* CONFIG_KDS */
1539
1540         KBASE_DEBUG_ASSERT(kctx);
1541
1542         kctx->jctx.job_done_wq = alloc_workqueue("mali_jd", 0, 1);
1543         if (NULL == kctx->jctx.job_done_wq) {
1544                 mali_err = -ENOMEM;
1545                 goto out1;
1546         }
1547
1548         for (i = 0; i < BASE_JD_ATOM_COUNT; i++) {
1549                 init_waitqueue_head(&kctx->jctx.atoms[i].completed);
1550
1551                 INIT_LIST_HEAD(&kctx->jctx.atoms[i].dep_head[0]);
1552                 INIT_LIST_HEAD(&kctx->jctx.atoms[i].dep_head[1]);
1553
1554                 /* Catch userspace attempting to use an atom which doesn't exist as a pre-dependency */
1555                 kctx->jctx.atoms[i].event_code = BASE_JD_EVENT_JOB_INVALID;
1556                 kctx->jctx.atoms[i].status = KBASE_JD_ATOM_STATE_UNUSED;
1557         }
1558
1559         mutex_init(&kctx->jctx.lock);
1560
1561         init_waitqueue_head(&kctx->jctx.zero_jobs_wait);
1562
1563         spin_lock_init(&kctx->jctx.tb_lock);
1564
1565 #ifdef CONFIG_KDS
1566         err = kds_callback_init(&kctx->jctx.kds_cb, 0, kds_dep_clear);
1567         if (0 != err) {
1568                 mali_err = -EINVAL;
1569                 goto out2;
1570         }
1571 #endif                          /* CONFIG_KDS */
1572
1573         kctx->jctx.job_nr = 0;
1574         INIT_LIST_HEAD(&kctx->completed_jobs);
1575         atomic_set(&kctx->work_count, 0);
1576
1577         return 0;
1578
1579 #ifdef CONFIG_KDS
1580  out2:
1581         destroy_workqueue(kctx->jctx.job_done_wq);
1582 #endif                          /* CONFIG_KDS */
1583  out1:
1584         return mali_err;
1585 }
1586
1587 KBASE_EXPORT_TEST_API(kbase_jd_init);
1588
1589 void kbase_jd_exit(struct kbase_context *kctx)
1590 {
1591         KBASE_DEBUG_ASSERT(kctx);
1592
1593 #ifdef CONFIG_KDS
1594         kds_callback_term(&kctx->jctx.kds_cb);
1595 #endif                          /* CONFIG_KDS */
1596         /* Work queue is emptied by this */
1597         destroy_workqueue(kctx->jctx.job_done_wq);
1598 }
1599
1600 KBASE_EXPORT_TEST_API(kbase_jd_exit);