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