3 * (C) COPYRIGHT 2010-2015 ARM Limited. All rights reserved.
5 * This program is free software and is provided to you under the terms of the
6 * GNU General Public License version 2 as published by the Free Software
7 * Foundation, and any use by you of this program is subject to the terms
10 * A copy of the licence is included with the program, and can also be obtained
11 * from Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
12 * Boston, MA 02110-1301, USA.
20 #if defined(CONFIG_DMA_SHARED_BUFFER)
21 #include <linux/dma-buf.h>
22 #endif /* defined(CONFIG_DMA_SHARED_BUFFER) */
24 #include <linux/compat.h>
26 #include <mali_kbase.h>
27 #include <mali_kbase_uku.h>
29 #include <linux/ump.h>
30 #endif /* CONFIG_UMP */
31 #include <linux/random.h>
32 #include <linux/version.h>
33 #include <linux/ratelimit.h>
34 #include <linux/pagemap.h>
36 #include <mali_kbase_jm.h>
37 #include <mali_kbase_hwaccess_jm.h>
39 #if defined(CONFIG_MALI_MIPE_ENABLED)
40 #include <mali_kbase_tlstream.h>
43 #define beenthere(kctx, f, a...) dev_dbg(kctx->kbdev->dev, "%s:" f, __func__, ##a)
45 #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 8, 0)
46 /* random32 was renamed to prandom_u32 in 3.8 */
47 #define prandom_u32 random32
50 /* Return whether katom will run on the GPU or not. Currently only soft jobs and
51 * dependency-only atoms do not run on the GPU */
52 #define IS_GPU_ATOM(katom) (!((katom->core_req & BASE_JD_REQ_SOFT_JOB) || \
53 ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) == \
56 * This is the kernel side of the API. Only entry points are:
57 * - kbase_jd_submit(): Called from userspace to submit a single bag
58 * - kbase_jd_done(): Called from interrupt context to track the
59 * completion of a job.
61 * - to the job manager (enqueue a job)
62 * - to the event subsystem (signals the completion/failure of bag/job-chains).
66 get_compat_pointer(struct kbase_context *kctx, const union kbase_pointer *p)
70 return compat_ptr(p->compat_value);
75 /* Runs an atom, either by handing to the JS or by immediately running it in the case of soft-jobs
77 * Returns whether the JS needs a reschedule.
79 * Note that the caller must also check the atom status and
80 * if it is KBASE_JD_ATOM_STATE_COMPLETED must call jd_done_nolock
82 static int jd_run_atom(struct kbase_jd_atom *katom)
84 struct kbase_context *kctx = katom->kctx;
86 KBASE_DEBUG_ASSERT(katom->status != KBASE_JD_ATOM_STATE_UNUSED);
88 if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) == BASE_JD_REQ_DEP) {
89 /* Dependency only atom */
90 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
92 } else if (katom->core_req & BASE_JD_REQ_SOFT_JOB) {
94 if ((katom->core_req & BASEP_JD_REQ_ATOM_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;
102 /* The job has not completed */
103 list_add_tail(&katom->dep_item[0], &kctx->waiting_soft_jobs);
108 katom->status = KBASE_JD_ATOM_STATE_IN_JS;
109 /* Queue an action about whether we should try scheduling a context */
110 return kbasep_js_add_job(kctx, katom);
115 /* Add the katom to the kds waiting list.
116 * Atoms must be added to the waiting list after a successful call to kds_async_waitall.
117 * The caller must hold the kbase_jd_context.lock */
119 static void kbase_jd_kds_waiters_add(struct kbase_jd_atom *katom)
121 struct kbase_context *kctx;
123 KBASE_DEBUG_ASSERT(katom);
127 list_add_tail(&katom->node, &kctx->waiting_kds_resource);
130 /* Remove the katom from the kds waiting list.
131 * Atoms must be removed from the waiting list before a call to kds_resource_set_release_sync.
132 * The supplied katom must first have been added to the list with a call to kbase_jd_kds_waiters_add.
133 * The caller must hold the kbase_jd_context.lock */
135 static void kbase_jd_kds_waiters_remove(struct kbase_jd_atom *katom)
137 KBASE_DEBUG_ASSERT(katom);
138 list_del(&katom->node);
141 static void kds_dep_clear(void *callback_parameter, void *callback_extra_parameter)
143 struct kbase_jd_atom *katom;
144 struct kbase_jd_context *ctx;
145 struct kbase_device *kbdev;
147 katom = (struct kbase_jd_atom *)callback_parameter;
148 KBASE_DEBUG_ASSERT(katom);
149 ctx = &katom->kctx->jctx;
150 kbdev = katom->kctx->kbdev;
151 KBASE_DEBUG_ASSERT(kbdev);
153 mutex_lock(&ctx->lock);
155 /* KDS resource has already been satisfied (e.g. due to zapping) */
156 if (katom->kds_dep_satisfied)
159 /* This atom's KDS dependency has now been met */
160 katom->kds_dep_satisfied = true;
162 /* Check whether the atom's other dependencies were already met. If
163 * katom is a GPU atom then the job scheduler may be able to represent
164 * the dependencies, hence we may attempt to submit it before they are
165 * met. Other atoms must have had both dependencies resolved */
166 if (IS_GPU_ATOM(katom) ||
167 (!kbase_jd_katom_dep_atom(&katom->dep[0]) &&
168 !kbase_jd_katom_dep_atom(&katom->dep[1]))) {
169 /* katom dep complete, attempt to run it */
170 bool resched = false;
172 resched = jd_run_atom(katom);
174 if (katom->status == KBASE_JD_ATOM_STATE_COMPLETED) {
175 /* The atom has already finished */
176 resched |= jd_done_nolock(katom, NULL);
180 kbase_js_sched_all(kbdev);
183 mutex_unlock(&ctx->lock);
186 static void kbase_cancel_kds_wait_job(struct kbase_jd_atom *katom)
188 KBASE_DEBUG_ASSERT(katom);
190 /* Prevent job_done_nolock from being called twice on an atom when
191 * there is a race between job completion and cancellation */
193 if (katom->status == KBASE_JD_ATOM_STATE_QUEUED) {
194 /* Wait was cancelled - zap the atom */
195 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
196 if (jd_done_nolock(katom, NULL))
197 kbase_js_sched_all(katom->kctx->kbdev);
200 #endif /* CONFIG_KDS */
202 static int kbase_jd_user_buf_map(struct kbase_context *kctx,
203 struct kbase_va_region *reg)
206 struct kbase_mem_phy_alloc *alloc;
211 unsigned long address;
212 struct task_struct *owner;
214 unsigned long offset;
215 unsigned long local_size;
217 alloc = reg->gpu_alloc;
218 pa = kbase_get_gpu_phy_pages(reg);
219 address = alloc->imported.user_buf.address;
220 owner = alloc->imported.user_buf.owner;
222 KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
224 pages = alloc->imported.user_buf.pages;
226 down_read(&owner->mm->mmap_sem);
227 pinned_pages = get_user_pages(owner, owner->mm,
229 alloc->imported.user_buf.nr_pages,
230 reg->flags & KBASE_REG_GPU_WR,
232 up_read(&owner->mm->mmap_sem);
234 if (pinned_pages <= 0)
237 if (pinned_pages != alloc->imported.user_buf.nr_pages) {
238 for (i = 0; i < pinned_pages; i++)
243 dev = kctx->kbdev->dev;
244 offset = address & ~PAGE_MASK;
245 local_size = alloc->imported.user_buf.size;
247 for (i = 0; i < pinned_pages; i++) {
251 min = MIN(PAGE_SIZE - offset, local_size);
252 dma_addr = dma_map_page(dev, pages[i],
255 if (dma_mapping_error(dev, dma_addr))
258 alloc->imported.user_buf.dma_addrs[i] = dma_addr;
259 pa[i] = page_to_phys(pages[i]);
265 alloc->nents = pinned_pages;
267 err = kbase_mmu_insert_pages(kctx, reg->start_pfn, pa,
268 kbase_reg_current_backed_size(reg),
277 dma_unmap_page(kctx->kbdev->dev,
278 alloc->imported.user_buf.dma_addrs[i],
279 PAGE_SIZE, DMA_BIDIRECTIONAL);
287 static void kbase_jd_user_buf_unmap(struct kbase_context *kctx,
288 struct kbase_mem_phy_alloc *alloc, bool writeable)
292 unsigned long size = alloc->imported.user_buf.size;
294 KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_USER_BUF);
295 pages = alloc->imported.user_buf.pages;
296 for (i = 0; i < alloc->imported.user_buf.nr_pages; i++) {
297 unsigned long local_size;
298 dma_addr_t dma_addr = alloc->imported.user_buf.dma_addrs[i];
300 local_size = MIN(size, PAGE_SIZE - (dma_addr & ~PAGE_MASK));
301 dma_unmap_page(kctx->kbdev->dev, dma_addr, local_size,
304 set_page_dirty_lock(pages[i]);
313 /* not to use sg_dma_len. */
314 #define MALI_SG_DMA_LEN(sg) ((sg)->length)
316 #ifdef CONFIG_DMA_SHARED_BUFFER
317 static int kbase_jd_umm_map(struct kbase_context *kctx, struct kbase_va_region *reg)
319 struct sg_table *sgt; /* scatterlist_table */
320 struct scatterlist *s;
325 struct kbase_mem_phy_alloc *alloc;
327 alloc = reg->gpu_alloc;
329 KBASE_DEBUG_ASSERT(alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM);
330 KBASE_DEBUG_ASSERT(NULL == alloc->imported.umm.sgt);
331 sgt = dma_buf_map_attachment(alloc->imported.umm.dma_attachment, DMA_BIDIRECTIONAL);
333 if (IS_ERR_OR_NULL(sgt))
337 alloc->imported.umm.sgt = sgt;
339 pa = kbase_get_gpu_phy_pages(reg);
340 KBASE_DEBUG_ASSERT(pa);
342 for_each_sg(sgt->sgl, s, sgt->nents, i) {
344 /* size_t pages = PFN_UP(sg_dma_len(s)); */
345 size_t pages = PFN_UP(MALI_SG_DMA_LEN(s));
347 WARN_ONCE(MALI_SG_DMA_LEN(s) & (PAGE_SIZE-1),
348 "MALI_SG_DMA_LEN(s)=%u is not a multiple of PAGE_SIZE\n",
351 WARN_ONCE(sg_dma_len(s) & (PAGE_SIZE-1),
352 "sg_dma_len(s)=%u is not a multiple of PAGE_SIZE\n",
356 WARN_ONCE(sg_dma_address(s) & (PAGE_SIZE-1),
357 "sg_dma_address(s)=%llx is not aligned to PAGE_SIZE\n",
358 (unsigned long long) sg_dma_address(s));
360 for (j = 0; (j < pages) && (count < reg->nr_pages); j++, count++)
361 *pa++ = sg_dma_address(s) + (j << PAGE_SHIFT);
364 "sg list from dma_buf_map_attachment > dma_buf->size=%zu\n",
365 alloc->imported.umm.dma_buf->size);
368 if (WARN_ONCE(count < reg->nr_pages,
369 "sg list from dma_buf_map_attachment < dma_buf->size=%zu, count : %lu, reg->nr_pages : %lu. \n",
370 alloc->imported.umm.dma_buf->size,
377 /* Update nents as we now have pages to map */
378 alloc->nents = count;
380 err = kbase_mmu_insert_pages(kctx, reg->start_pfn, kbase_get_gpu_phy_pages(reg), kbase_reg_current_backed_size(reg), reg->flags | KBASE_REG_GPU_WR | KBASE_REG_GPU_RD);
384 dma_buf_unmap_attachment(alloc->imported.umm.dma_attachment, alloc->imported.umm.sgt, DMA_BIDIRECTIONAL);
385 alloc->imported.umm.sgt = NULL;
391 static void kbase_jd_umm_unmap(struct kbase_context *kctx, struct kbase_mem_phy_alloc *alloc)
393 KBASE_DEBUG_ASSERT(kctx);
394 KBASE_DEBUG_ASSERT(alloc);
395 KBASE_DEBUG_ASSERT(alloc->imported.umm.dma_attachment);
396 KBASE_DEBUG_ASSERT(alloc->imported.umm.sgt);
397 dma_buf_unmap_attachment(alloc->imported.umm.dma_attachment,
398 alloc->imported.umm.sgt, DMA_BIDIRECTIONAL);
399 alloc->imported.umm.sgt = NULL;
402 #endif /* CONFIG_DMA_SHARED_BUFFER */
404 void kbase_jd_free_external_resources(struct kbase_jd_atom *katom)
407 if (katom->kds_rset) {
408 struct kbase_jd_context *jctx = &katom->kctx->jctx;
411 * As the atom is no longer waiting, remove it from
415 mutex_lock(&jctx->lock);
416 kbase_jd_kds_waiters_remove(katom);
417 mutex_unlock(&jctx->lock);
419 /* Release the kds resource or cancel if zapping */
420 kds_resource_set_release_sync(&katom->kds_rset);
422 #endif /* CONFIG_KDS */
425 static void kbase_jd_post_external_resources(struct kbase_jd_atom *katom)
427 KBASE_DEBUG_ASSERT(katom);
428 KBASE_DEBUG_ASSERT(katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES);
431 /* Prevent the KDS resource from triggering the atom in case of zapping */
433 katom->kds_dep_satisfied = true;
434 #endif /* CONFIG_KDS */
436 kbase_gpu_vm_lock(katom->kctx);
437 /* only roll back if extres is non-NULL */
441 res_no = katom->nr_extres;
442 while (res_no-- > 0) {
443 struct kbase_mem_phy_alloc *alloc = katom->extres[res_no].alloc;
445 switch (alloc->type) {
446 #ifdef CONFIG_DMA_SHARED_BUFFER
447 case KBASE_MEM_TYPE_IMPORTED_UMM: {
448 alloc->imported.umm.current_mapping_usage_count--;
450 if (0 == alloc->imported.umm.current_mapping_usage_count) {
451 struct kbase_va_region *reg;
453 reg = kbase_region_tracker_find_region_base_address(
455 katom->extres[res_no].gpu_address);
457 if (reg && reg->gpu_alloc == alloc)
458 kbase_mmu_teardown_pages(
461 kbase_reg_current_backed_size(reg));
463 kbase_jd_umm_unmap(katom->kctx, alloc);
467 #endif /* CONFIG_DMA_SHARED_BUFFER */
468 case KBASE_MEM_TYPE_IMPORTED_USER_BUF: {
469 alloc->imported.user_buf.current_mapping_usage_count--;
471 if (0 == alloc->imported.user_buf.current_mapping_usage_count) {
472 struct kbase_va_region *reg;
474 reg = kbase_region_tracker_find_region_base_address(
476 katom->extres[res_no].gpu_address);
478 if (reg && reg->gpu_alloc == alloc)
479 kbase_mmu_teardown_pages(
482 kbase_reg_current_backed_size(reg));
484 kbase_jd_user_buf_unmap(katom->kctx,
486 reg->flags & KBASE_REG_GPU_WR);
493 kbase_mem_phy_alloc_put(katom->extres[res_no].alloc);
495 kfree(katom->extres);
496 katom->extres = NULL;
498 kbase_gpu_vm_unlock(katom->kctx);
501 #if (defined(CONFIG_KDS) && defined(CONFIG_UMP)) || defined(CONFIG_DMA_SHARED_BUFFER_USES_KDS)
502 static void add_kds_resource(struct kds_resource *kds_res, struct kds_resource **kds_resources, u32 *kds_res_count, unsigned long *kds_access_bitmap, bool exclusive)
506 for (i = 0; i < *kds_res_count; i++) {
507 /* Duplicate resource, ignore */
508 if (kds_resources[i] == kds_res)
512 kds_resources[*kds_res_count] = kds_res;
514 set_bit(*kds_res_count, kds_access_bitmap);
520 * Set up external resources needed by this job.
522 * jctx.lock must be held when this is called.
525 static int kbase_jd_pre_external_resources(struct kbase_jd_atom *katom, const struct base_jd_atom_v2 *user_atom)
527 int err_ret_val = -EINVAL;
530 u32 kds_res_count = 0;
531 struct kds_resource **kds_resources = NULL;
532 unsigned long *kds_access_bitmap = NULL;
533 #endif /* CONFIG_KDS */
534 struct base_external_resource *input_extres;
536 KBASE_DEBUG_ASSERT(katom);
537 KBASE_DEBUG_ASSERT(katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES);
539 /* no resources encoded, early out */
540 if (!katom->nr_extres)
543 katom->extres = kmalloc_array(katom->nr_extres, sizeof(*katom->extres), GFP_KERNEL);
544 if (NULL == katom->extres) {
545 err_ret_val = -ENOMEM;
549 /* copy user buffer to the end of our real buffer.
550 * Make sure the struct sizes haven't changed in a way
551 * we don't support */
552 BUILD_BUG_ON(sizeof(*input_extres) > sizeof(*katom->extres));
553 input_extres = (struct base_external_resource *)
554 (((unsigned char *)katom->extres) +
555 (sizeof(*katom->extres) - sizeof(*input_extres)) *
558 if (copy_from_user(input_extres,
559 get_compat_pointer(katom->kctx, &user_atom->extres_list),
560 sizeof(*input_extres) * katom->nr_extres) != 0) {
561 err_ret_val = -EINVAL;
565 /* assume we have to wait for all */
566 KBASE_DEBUG_ASSERT(0 != katom->nr_extres);
567 kds_resources = kmalloc_array(katom->nr_extres, sizeof(struct kds_resource *), GFP_KERNEL);
569 if (NULL == kds_resources) {
570 err_ret_val = -ENOMEM;
574 KBASE_DEBUG_ASSERT(0 != katom->nr_extres);
575 kds_access_bitmap = kzalloc(sizeof(unsigned long) * ((katom->nr_extres + BITS_PER_LONG - 1) / BITS_PER_LONG), GFP_KERNEL);
577 if (NULL == kds_access_bitmap) {
578 err_ret_val = -ENOMEM;
581 #endif /* CONFIG_KDS */
583 /* need to keep the GPU VM locked while we set up UMM buffers */
584 kbase_gpu_vm_lock(katom->kctx);
585 for (res_no = 0; res_no < katom->nr_extres; res_no++) {
586 struct base_external_resource *res;
587 struct kbase_va_region *reg;
589 res = &input_extres[res_no];
590 reg = kbase_region_tracker_find_region_enclosing_address(
592 res->ext_resource & ~BASE_EXT_RES_ACCESS_EXCLUSIVE);
593 /* did we find a matching region object? */
594 if (NULL == reg || (reg->flags & KBASE_REG_FREE)) {
599 if (!(katom->core_req & BASE_JD_REQ_SOFT_JOB) &&
600 (reg->flags & KBASE_REG_SECURE)) {
601 katom->atom_flags |= KBASE_KATOM_FLAG_SECURE;
602 if ((katom->core_req & BASE_JD_REQ_FS) == 0) {
603 WARN_RATELIMIT(1, "Secure non-fragment jobs not supported");
608 /* decide what needs to happen for this resource */
609 switch (reg->gpu_alloc->type) {
610 case BASE_MEM_IMPORT_TYPE_USER_BUFFER: {
611 reg->gpu_alloc->imported.user_buf.current_mapping_usage_count++;
612 if (1 == reg->gpu_alloc->imported.user_buf.current_mapping_usage_count) {
613 /* use a local variable to not pollute
614 * err_ret_val with a potential success
615 * value as some other gotos depend on
616 * the default error code stored in
620 tmp = kbase_jd_user_buf_map(katom->kctx,
623 /* failed to map this buffer,
626 reg->gpu_alloc->imported.user_buf.current_mapping_usage_count--;
632 case BASE_MEM_IMPORT_TYPE_UMP: {
633 #if defined(CONFIG_KDS) && defined(CONFIG_UMP)
634 struct kds_resource *kds_res;
636 kds_res = ump_dd_kds_resource_get(reg->gpu_alloc->imported.ump_handle);
638 add_kds_resource(kds_res, kds_resources, &kds_res_count,
640 res->ext_resource & BASE_EXT_RES_ACCESS_EXCLUSIVE);
641 #endif /*defined(CONFIG_KDS) && defined(CONFIG_UMP) */
644 #ifdef CONFIG_DMA_SHARED_BUFFER
645 case BASE_MEM_IMPORT_TYPE_UMM: {
646 #ifdef CONFIG_DMA_SHARED_BUFFER_USES_KDS
647 struct kds_resource *kds_res;
649 kds_res = get_dma_buf_kds_resource(reg->gpu_alloc->imported.umm.dma_buf);
651 add_kds_resource(kds_res, kds_resources, &kds_res_count, kds_access_bitmap, res->ext_resource & BASE_EXT_RES_ACCESS_EXCLUSIVE);
653 reg->gpu_alloc->imported.umm.current_mapping_usage_count++;
654 if (1 == reg->gpu_alloc->imported.umm.current_mapping_usage_count) {
655 /* use a local variable to not pollute err_ret_val
656 * with a potential success value as some other gotos depend
657 * on the default error code stored in err_ret_val */
660 tmp = kbase_jd_umm_map(katom->kctx, reg);
662 /* failed to map this buffer, roll back */
664 reg->gpu_alloc->imported.umm.current_mapping_usage_count--;
675 /* finish with updating out array with the data we found */
676 /* NOTE: It is important that this is the last thing we do (or
677 * at least not before the first write) as we overwrite elements
678 * as we loop and could be overwriting ourself, so no writes
679 * until the last read for an element.
681 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 */
682 katom->extres[res_no].alloc = kbase_mem_phy_alloc_get(reg->gpu_alloc);
684 /* successfully parsed the extres array */
685 /* drop the vm lock before we call into kds */
686 kbase_gpu_vm_unlock(katom->kctx);
692 /* We have resources to wait for with kds */
693 katom->kds_dep_satisfied = false;
695 wait_failed = kds_async_waitall(&katom->kds_rset,
696 &katom->kctx->jctx.kds_cb, katom, NULL,
697 kds_res_count, kds_access_bitmap,
701 goto failed_kds_setup;
703 kbase_jd_kds_waiters_add(katom);
705 /* Nothing to wait for, so kds dep met */
706 katom->kds_dep_satisfied = true;
708 kfree(kds_resources);
709 kfree(kds_access_bitmap);
710 #endif /* CONFIG_KDS */
715 /* error handling section */
720 /* lock before we unmap */
721 kbase_gpu_vm_lock(katom->kctx);
722 #endif /* CONFIG_KDS */
725 /* undo the loop work */
726 while (res_no-- > 0) {
727 struct kbase_mem_phy_alloc *alloc = katom->extres[res_no].alloc;
728 #ifdef CONFIG_DMA_SHARED_BUFFER
729 if (alloc->type == KBASE_MEM_TYPE_IMPORTED_UMM) {
730 alloc->imported.umm.current_mapping_usage_count--;
732 if (0 == alloc->imported.umm.current_mapping_usage_count) {
733 struct kbase_va_region *reg;
735 reg = kbase_region_tracker_find_region_base_address(
737 katom->extres[res_no].gpu_address);
739 if (reg && reg->gpu_alloc == alloc)
740 kbase_mmu_teardown_pages(katom->kctx,
742 kbase_reg_current_backed_size(reg));
744 kbase_jd_umm_unmap(katom->kctx, alloc);
747 #endif /* CONFIG_DMA_SHARED_BUFFER */
748 kbase_mem_phy_alloc_put(alloc);
750 kbase_gpu_vm_unlock(katom->kctx);
753 kfree(katom->extres);
754 katom->extres = NULL;
756 kfree(kds_resources);
757 kfree(kds_access_bitmap);
758 #endif /* CONFIG_KDS */
762 static inline void jd_resolve_dep(struct list_head *out_list,
763 struct kbase_jd_atom *katom,
769 while (!list_empty(&katom->dep_head[d])) {
770 struct kbase_jd_atom *dep_atom;
773 dep_atom = list_entry(katom->dep_head[d].next,
774 struct kbase_jd_atom, dep_item[d]);
776 list_del(katom->dep_head[d].next);
778 dep_type = kbase_jd_katom_dep_type(&dep_atom->dep[d]);
779 kbase_jd_katom_dep_clear(&dep_atom->dep[d]);
781 if (katom->event_code != BASE_JD_EVENT_DONE &&
782 (dep_type != BASE_JD_DEP_TYPE_ORDER || ctx_is_dying)) {
783 /* Atom failed, so remove the other dependencies and immediately fail the atom */
784 if (kbase_jd_katom_dep_atom(&dep_atom->dep[other_d])) {
785 list_del(&dep_atom->dep_item[other_d]);
786 kbase_jd_katom_dep_clear(&dep_atom->dep[other_d]);
789 if (!dep_atom->kds_dep_satisfied) {
790 /* Just set kds_dep_satisfied to true. If the callback happens after this then it will early out and
791 * do nothing. If the callback doesn't happen then kbase_jd_post_external_resources will clean up
793 dep_atom->kds_dep_satisfied = true;
797 dep_atom->event_code = katom->event_code;
798 KBASE_DEBUG_ASSERT(dep_atom->status !=
799 KBASE_JD_ATOM_STATE_UNUSED);
800 dep_atom->status = KBASE_JD_ATOM_STATE_COMPLETED;
802 list_add_tail(&dep_atom->dep_item[0], out_list);
803 } else if (!kbase_jd_katom_dep_atom(&dep_atom->dep[other_d])) {
805 if (dep_atom->kds_dep_satisfied)
807 list_add_tail(&dep_atom->dep_item[0], out_list);
812 KBASE_EXPORT_TEST_API(jd_resolve_dep);
814 #if MALI_CUSTOMER_RELEASE == 0
815 static void jd_force_failure(struct kbase_device *kbdev, struct kbase_jd_atom *katom)
817 kbdev->force_replay_count++;
819 if (kbdev->force_replay_count >= kbdev->force_replay_limit) {
820 kbdev->force_replay_count = 0;
821 katom->event_code = BASE_JD_EVENT_FORCE_REPLAY;
823 if (kbdev->force_replay_random)
824 kbdev->force_replay_limit =
825 (prandom_u32() % KBASEP_FORCE_REPLAY_RANDOM_LIMIT) + 1;
827 dev_info(kbdev->dev, "force_replay : promoting to error\n");
831 /** Test to see if atom should be forced to fail.
833 * This function will check if an atom has a replay job as a dependent. If so
834 * then it will be considered for forced failure. */
835 static void jd_check_force_failure(struct kbase_jd_atom *katom)
837 struct kbase_context *kctx = katom->kctx;
838 struct kbase_device *kbdev = kctx->kbdev;
841 if ((kbdev->force_replay_limit == KBASEP_FORCE_REPLAY_DISABLED) ||
842 (katom->core_req & BASEP_JD_REQ_EVENT_NEVER))
845 for (i = 1; i < BASE_JD_ATOM_COUNT; i++) {
846 if (kbase_jd_katom_dep_atom(&kctx->jctx.atoms[i].dep[0]) == katom ||
847 kbase_jd_katom_dep_atom(&kctx->jctx.atoms[i].dep[1]) == katom) {
848 struct kbase_jd_atom *dep_atom = &kctx->jctx.atoms[i];
850 if ((dep_atom->core_req & BASEP_JD_REQ_ATOM_TYPE) ==
851 BASE_JD_REQ_SOFT_REPLAY &&
852 (dep_atom->core_req & kbdev->force_replay_core_req)
853 == kbdev->force_replay_core_req) {
854 jd_force_failure(kbdev, katom);
863 * Perform the necessary handling of an atom that has finished running
866 * Note that if this is a soft-job that has had kbase_prepare_soft_job called on it then the caller
867 * is responsible for calling kbase_finish_soft_job *before* calling this function.
869 * The caller must hold the kbase_jd_context.lock.
871 bool jd_done_nolock(struct kbase_jd_atom *katom,
872 struct list_head *completed_jobs_ctx)
874 struct kbase_context *kctx = katom->kctx;
875 struct kbase_device *kbdev = kctx->kbdev;
876 struct kbasep_js_kctx_info *js_kctx_info = &kctx->jctx.sched_info;
877 struct list_head completed_jobs;
878 struct list_head runnable_jobs;
879 bool need_to_try_schedule_context = false;
882 INIT_LIST_HEAD(&completed_jobs);
883 INIT_LIST_HEAD(&runnable_jobs);
885 KBASE_DEBUG_ASSERT(katom->status != KBASE_JD_ATOM_STATE_UNUSED);
887 #if MALI_CUSTOMER_RELEASE == 0
888 jd_check_force_failure(katom);
892 /* This is needed in case an atom is failed due to being invalid, this
893 * can happen *before* the jobs that the atom depends on have completed */
894 for (i = 0; i < 2; i++) {
895 if (kbase_jd_katom_dep_atom(&katom->dep[i])) {
896 list_del(&katom->dep_item[i]);
897 kbase_jd_katom_dep_clear(&katom->dep[i]);
901 /* With PRLAM-10817 or PRLAM-10959 the last tile of a fragment job being soft-stopped can fail with
902 * BASE_JD_EVENT_TILE_RANGE_FAULT.
904 * So here if the fragment job failed with TILE_RANGE_FAULT and it has been soft-stopped, then we promote the
905 * error code to BASE_JD_EVENT_DONE
908 if ((kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10817) || kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_10959)) &&
909 katom->event_code == BASE_JD_EVENT_TILE_RANGE_FAULT) {
910 if ((katom->core_req & BASE_JD_REQ_FS) && (katom->atom_flags & KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED)) {
911 /* Promote the failure to job done */
912 katom->event_code = BASE_JD_EVENT_DONE;
913 katom->atom_flags = katom->atom_flags & (~KBASE_KATOM_FLAG_BEEN_SOFT_STOPPPED);
917 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
918 list_add_tail(&katom->dep_item[0], &completed_jobs);
920 while (!list_empty(&completed_jobs)) {
921 katom = list_entry(completed_jobs.prev, struct kbase_jd_atom, dep_item[0]);
922 list_del(completed_jobs.prev);
924 KBASE_DEBUG_ASSERT(katom->status == KBASE_JD_ATOM_STATE_COMPLETED);
926 for (i = 0; i < 2; i++)
927 jd_resolve_dep(&runnable_jobs, katom, i,
928 js_kctx_info->ctx.is_dying);
930 if (katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES)
931 kbase_jd_post_external_resources(katom);
933 while (!list_empty(&runnable_jobs)) {
934 struct kbase_jd_atom *node;
936 node = list_entry(runnable_jobs.next,
937 struct kbase_jd_atom, dep_item[0]);
939 list_del(runnable_jobs.next);
941 KBASE_DEBUG_ASSERT(node->status != KBASE_JD_ATOM_STATE_UNUSED);
943 if (node->status != KBASE_JD_ATOM_STATE_COMPLETED) {
944 need_to_try_schedule_context |= jd_run_atom(node);
946 node->event_code = katom->event_code;
948 if ((node->core_req & BASEP_JD_REQ_ATOM_TYPE)
949 == BASE_JD_REQ_SOFT_REPLAY) {
950 if (kbase_replay_process(node))
951 /* Don't complete this atom */
953 } else if (node->core_req &
954 BASE_JD_REQ_SOFT_JOB) {
955 /* If this is a fence wait then remove it from the list of sync waiters. */
956 if (BASE_JD_REQ_SOFT_FENCE_WAIT == node->core_req)
957 list_del(&node->dep_item[0]);
959 kbase_finish_soft_job(node);
961 node->status = KBASE_JD_ATOM_STATE_COMPLETED;
964 if (node->status == KBASE_JD_ATOM_STATE_COMPLETED)
965 list_add_tail(&node->dep_item[0], &completed_jobs);
968 /* Register a completed job as a disjoint event when the GPU
969 * is in a disjoint state (ie. being reset or replaying jobs).
971 kbase_disjoint_event_potential(kctx->kbdev);
972 if (completed_jobs_ctx)
973 list_add_tail(&katom->dep_item[0], completed_jobs_ctx);
975 kbase_event_post(kctx, katom);
977 /* Decrement and check the TOTAL number of jobs. This includes
978 * those not tracked by the scheduler: 'not ready to run' and
979 * 'dependency-only' jobs. */
980 if (--kctx->jctx.job_nr == 0)
981 wake_up(&kctx->jctx.zero_jobs_wait); /* All events are safely queued now, and we can signal any waiter
982 * that we've got no more jobs (so we can be safely terminated) */
985 return need_to_try_schedule_context;
988 KBASE_EXPORT_TEST_API(jd_done_nolock);
990 #ifdef CONFIG_GPU_TRACEPOINTS
998 CORE_REQ_FRAGMENT_VERTEX,
999 CORE_REQ_FRAGMENT_VERTEX_TILER,
1000 CORE_REQ_FRAGMENT_TILER,
1001 CORE_REQ_VERTEX_TILER,
1004 static const char * const core_req_strings[] = {
1005 "Dependency Only Job",
1007 "Compute Shader Job",
1008 "Fragment Shader Job",
1009 "Vertex/Geometry Shader Job",
1011 "Fragment Shader + Vertex/Geometry Shader Job",
1012 "Fragment Shader + Vertex/Geometry Shader Job + Tiler Job",
1013 "Fragment Shader + Tiler Job",
1014 "Vertex/Geometry Shader Job + Tiler Job",
1017 static const char *kbasep_map_core_reqs_to_string(base_jd_core_req core_req)
1019 if (core_req & BASE_JD_REQ_SOFT_JOB)
1020 return core_req_strings[CORE_REQ_SOFT];
1021 if (core_req & BASE_JD_REQ_ONLY_COMPUTE)
1022 return core_req_strings[CORE_REQ_COMPUTE];
1023 switch (core_req & (BASE_JD_REQ_FS | BASE_JD_REQ_CS | BASE_JD_REQ_T)) {
1024 case BASE_JD_REQ_DEP:
1025 return core_req_strings[CORE_REQ_DEP_ONLY];
1026 case BASE_JD_REQ_FS:
1027 return core_req_strings[CORE_REQ_FRAGMENT];
1028 case BASE_JD_REQ_CS:
1029 return core_req_strings[CORE_REQ_VERTEX];
1031 return core_req_strings[CORE_REQ_TILER];
1032 case (BASE_JD_REQ_FS | BASE_JD_REQ_CS):
1033 return core_req_strings[CORE_REQ_FRAGMENT_VERTEX];
1034 case (BASE_JD_REQ_FS | BASE_JD_REQ_T):
1035 return core_req_strings[CORE_REQ_FRAGMENT_TILER];
1036 case (BASE_JD_REQ_CS | BASE_JD_REQ_T):
1037 return core_req_strings[CORE_REQ_VERTEX_TILER];
1038 case (BASE_JD_REQ_FS | BASE_JD_REQ_CS | BASE_JD_REQ_T):
1039 return core_req_strings[CORE_REQ_FRAGMENT_VERTEX_TILER];
1041 return core_req_strings[CORE_REQ_UNKNOWN];
1045 bool jd_submit_atom(struct kbase_context *kctx,
1046 const struct base_jd_atom_v2 *user_atom,
1047 struct kbase_jd_atom *katom)
1049 struct kbase_jd_context *jctx = &kctx->jctx;
1050 base_jd_core_req core_req;
1056 /* Update the TOTAL number of jobs. This includes those not tracked by
1057 * the scheduler: 'not ready to run' and 'dependency-only' jobs. */
1060 core_req = user_atom->core_req;
1062 katom->start_timestamp.tv64 = 0;
1063 katom->time_spent_us = 0;
1064 katom->udata = user_atom->udata;
1066 katom->nr_extres = user_atom->nr_extres;
1067 katom->extres = NULL;
1068 katom->device_nr = user_atom->device_nr;
1069 katom->affinity = 0;
1070 katom->jc = user_atom->jc;
1071 katom->coreref_state = KBASE_ATOM_COREREF_STATE_NO_CORES_REQUESTED;
1072 katom->core_req = core_req;
1073 katom->atom_flags = 0;
1074 katom->retry_count = 0;
1075 katom->need_cache_flush_cores_retained = 0;
1076 katom->x_pre_dep = NULL;
1077 katom->x_post_dep = NULL;
1079 /* Start by assuming that the KDS dependencies are satisfied,
1080 * kbase_jd_pre_external_resources will correct this if there are dependencies */
1081 katom->kds_dep_satisfied = true;
1082 katom->kds_rset = NULL;
1083 #endif /* CONFIG_KDS */
1085 /* Don't do anything if there is a mess up with dependencies.
1086 This is done in a separate cycle to check both the dependencies at ones, otherwise
1087 it will be extra complexity to deal with 1st dependency ( just added to the list )
1088 if only the 2nd one has invalid config.
1090 for (i = 0; i < 2; i++) {
1091 int dep_atom_number = user_atom->pre_dep[i].atom_id;
1092 base_jd_dep_type dep_atom_type = user_atom->pre_dep[i].dependency_type;
1094 if (dep_atom_number) {
1095 if (dep_atom_type != BASE_JD_DEP_TYPE_ORDER &&
1096 dep_atom_type != BASE_JD_DEP_TYPE_DATA) {
1097 katom->event_code = BASE_JD_EVENT_JOB_CONFIG_FAULT;
1098 katom->status = KBASE_JD_ATOM_STATE_COMPLETED;
1099 #if defined(CONFIG_MALI_MIPE_ENABLED)
1100 /* Wrong dependency setup. Atom will be sent
1101 * back to user space. Do not record any
1103 kbase_tlstream_tl_new_atom(
1105 kbase_jd_atom_id(kctx, katom));
1106 kbase_tlstream_tl_ret_atom_ctx(
1109 ret = jd_done_nolock(katom, NULL);
1115 /* Add dependencies */
1116 for (i = 0; i < 2; i++) {
1117 int dep_atom_number = user_atom->pre_dep[i].atom_id;
1118 base_jd_dep_type dep_atom_type;
1119 struct kbase_jd_atom *dep_atom = &jctx->atoms[dep_atom_number];
1121 dep_atom_type = user_atom->pre_dep[i].dependency_type;
1122 kbase_jd_katom_dep_clear(&katom->dep[i]);
1124 if (!dep_atom_number)
1127 if (dep_atom->status == KBASE_JD_ATOM_STATE_UNUSED ||
1128 dep_atom->status == KBASE_JD_ATOM_STATE_COMPLETED) {
1130 if (dep_atom->event_code == BASE_JD_EVENT_DONE)
1132 /* don't stop this atom if it has an order dependency
1133 * only to the failed one, try to submit it through
1136 if (dep_atom_type == BASE_JD_DEP_TYPE_ORDER &&
1137 dep_atom->event_code > BASE_JD_EVENT_ACTIVE) {
1141 if (i == 1 && kbase_jd_katom_dep_atom(&katom->dep[0])) {
1142 /* Remove the previous dependency */
1143 list_del(&katom->dep_item[0]);
1144 kbase_jd_katom_dep_clear(&katom->dep[0]);
1147 /* Atom has completed, propagate the error code if any */
1148 katom->event_code = dep_atom->event_code;
1149 katom->status = KBASE_JD_ATOM_STATE_QUEUED;
1150 #if defined(CONFIG_MALI_MIPE_ENABLED)
1151 /* This atom is going through soft replay or
1152 * will be sent back to user space. Do not record any
1154 kbase_tlstream_tl_new_atom(
1156 kbase_jd_atom_id(kctx, katom));
1157 kbase_tlstream_tl_ret_atom_ctx(katom, kctx);
1159 if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE)
1160 == BASE_JD_REQ_SOFT_REPLAY) {
1161 if (kbase_replay_process(katom)) {
1166 ret = jd_done_nolock(katom, NULL);
1170 /* Atom is in progress, add this atom to the list */
1171 list_add_tail(&katom->dep_item[i], &dep_atom->dep_head[i]);
1172 kbase_jd_katom_dep_set(&katom->dep[i], dep_atom, dep_atom_type);
1177 /* These must occur after the above loop to ensure that an atom that
1178 * depends on a previous atom with the same number behaves as expected */
1179 katom->event_code = BASE_JD_EVENT_DONE;
1180 katom->status = KBASE_JD_ATOM_STATE_QUEUED;
1182 #if defined(CONFIG_MALI_MIPE_ENABLED)
1183 /* Create a new atom recording all dependencies it was set up with. */
1184 kbase_tlstream_tl_new_atom(
1186 kbase_jd_atom_id(kctx, katom));
1187 kbase_tlstream_tl_ret_atom_ctx(katom, kctx);
1188 for (i = 0; i < 2; i++)
1189 if (BASE_JD_DEP_TYPE_INVALID != kbase_jd_katom_dep_type(
1191 kbase_tlstream_tl_dep_atom_atom(
1192 (void *)kbase_jd_katom_dep_atom(
1197 /* Reject atoms with job chain = NULL, as these cause issues with soft-stop */
1198 if (!katom->jc && (katom->core_req & BASEP_JD_REQ_ATOM_TYPE) != BASE_JD_REQ_DEP) {
1199 dev_warn(kctx->kbdev->dev, "Rejecting atom with jc = NULL");
1200 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1201 ret = jd_done_nolock(katom, NULL);
1205 /* Reject atoms with an invalid device_nr */
1206 if ((katom->core_req & BASE_JD_REQ_SPECIFIC_COHERENT_GROUP) &&
1207 (katom->device_nr >= kctx->kbdev->gpu_props.num_core_groups)) {
1208 dev_warn(kctx->kbdev->dev,
1209 "Rejecting atom with invalid device_nr %d",
1211 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1212 ret = jd_done_nolock(katom, NULL);
1216 /* For invalid priority, be most lenient and choose the default */
1217 sched_prio = kbasep_js_atom_prio_to_sched_prio(user_atom->prio);
1218 if (sched_prio == KBASE_JS_ATOM_SCHED_PRIO_INVALID)
1219 sched_prio = KBASE_JS_ATOM_SCHED_PRIO_DEFAULT;
1220 katom->sched_priority = sched_prio;
1222 if (katom->core_req & BASE_JD_REQ_EXTERNAL_RESOURCES) {
1223 /* handle what we need to do to access the external resources */
1224 if (kbase_jd_pre_external_resources(katom, user_atom) != 0) {
1225 /* setup failed (no access, bad resource, unknown resource types, etc.) */
1226 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1227 ret = jd_done_nolock(katom, NULL);
1232 /* Validate the atom. Function will return error if the atom is
1235 * Soft-jobs never enter the job scheduler but have their own initialize method.
1237 * If either fail then we immediately complete the atom with an error.
1239 if ((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0) {
1240 if (!kbase_js_is_atom_valid(kctx->kbdev, katom)) {
1241 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1242 ret = jd_done_nolock(katom, NULL);
1247 if (kbase_prepare_soft_job(katom) != 0) {
1248 katom->event_code = BASE_JD_EVENT_JOB_INVALID;
1249 ret = jd_done_nolock(katom, NULL);
1254 #ifdef CONFIG_GPU_TRACEPOINTS
1255 katom->work_id = atomic_inc_return(&jctx->work_id);
1256 trace_gpu_job_enqueue((u32)kctx->id, katom->work_id,
1257 kbasep_map_core_reqs_to_string(katom->core_req));
1260 if (queued && !IS_GPU_ATOM(katom)) {
1265 if (!katom->kds_dep_satisfied) {
1266 /* Queue atom due to KDS dependency */
1270 #endif /* CONFIG_KDS */
1272 if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE)
1273 == BASE_JD_REQ_SOFT_REPLAY) {
1274 if (kbase_replay_process(katom))
1277 ret = jd_done_nolock(katom, NULL);
1280 } else if (katom->core_req & BASE_JD_REQ_SOFT_JOB) {
1281 if (kbase_process_soft_job(katom) == 0) {
1282 kbase_finish_soft_job(katom);
1283 ret = jd_done_nolock(katom, NULL);
1286 /* The job has not yet completed */
1287 list_add_tail(&katom->dep_item[0], &kctx->waiting_soft_jobs);
1289 } else if ((katom->core_req & BASEP_JD_REQ_ATOM_TYPE) != BASE_JD_REQ_DEP) {
1290 katom->status = KBASE_JD_ATOM_STATE_IN_JS;
1291 ret = kbasep_js_add_job(kctx, katom);
1292 /* If job was cancelled then resolve immediately */
1293 if (katom->event_code == BASE_JD_EVENT_JOB_CANCELLED)
1294 ret = jd_done_nolock(katom, NULL);
1296 /* This is a pure dependency. Resolve it immediately */
1297 ret = jd_done_nolock(katom, NULL);
1304 #ifdef BASE_LEGACY_UK6_SUPPORT
1305 int kbase_jd_submit(struct kbase_context *kctx,
1306 const struct kbase_uk_job_submit *submit_data,
1309 int kbase_jd_submit(struct kbase_context *kctx,
1310 const struct kbase_uk_job_submit *submit_data)
1311 #endif /* BASE_LEGACY_UK6_SUPPORT */
1313 struct kbase_jd_context *jctx = &kctx->jctx;
1316 bool need_to_try_schedule_context = false;
1317 struct kbase_device *kbdev;
1318 void __user *user_addr;
1322 * kbase_jd_submit isn't expected to fail and so all errors with the jobs
1323 * are reported by immediately falling them (through event system)
1325 kbdev = kctx->kbdev;
1327 beenthere(kctx, "%s", "Enter");
1329 if ((kctx->jctx.sched_info.ctx.flags & KBASE_CTX_FLAG_SUBMIT_DISABLED) != 0) {
1330 dev_err(kbdev->dev, "Attempt to submit to a context that has SUBMIT_DISABLED set on it");
1334 #ifdef BASE_LEGACY_UK6_SUPPORT
1335 if ((uk6_atom && submit_data->stride !=
1336 sizeof(struct base_jd_atom_v2_uk6)) ||
1337 submit_data->stride != sizeof(base_jd_atom_v2)) {
1339 if (submit_data->stride != sizeof(base_jd_atom_v2)) {
1340 #endif /* BASE_LEGACY_UK6_SUPPORT */
1341 dev_err(kbdev->dev, "Stride passed to job_submit doesn't match kernel");
1345 user_addr = get_compat_pointer(kctx, &submit_data->addr);
1347 KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx, atomic_add_return(submit_data->nr_atoms, &kctx->timeline.jd_atoms_in_flight));
1349 /* All atoms submitted in this call have the same flush ID */
1350 latest_flush = kbase_backend_get_current_flush_id(kbdev);
1352 for (i = 0; i < submit_data->nr_atoms; i++) {
1353 struct base_jd_atom_v2 user_atom;
1354 struct kbase_jd_atom *katom;
1356 #ifdef BASE_LEGACY_UK6_SUPPORT
1358 struct base_jd_atom_v2_uk6 user_atom_v6;
1359 base_jd_dep_type dep_types[2] = {BASE_JD_DEP_TYPE_DATA, BASE_JD_DEP_TYPE_DATA};
1361 if (copy_from_user(&user_atom_v6, user_addr,
1362 sizeof(user_atom_v6))) {
1364 KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx,
1366 submit_data->nr_atoms - i,
1367 &kctx->timeline.jd_atoms_in_flight));
1370 /* Convert from UK6 atom format to UK7 format */
1371 user_atom.jc = user_atom_v6.jc;
1372 user_atom.udata = user_atom_v6.udata;
1373 user_atom.extres_list = user_atom_v6.extres_list;
1374 user_atom.nr_extres = user_atom_v6.nr_extres;
1375 user_atom.core_req = user_atom_v6.core_req;
1377 /* atom number 0 is used for no dependency atoms */
1378 if (!user_atom_v6.pre_dep[0])
1379 dep_types[0] = BASE_JD_DEP_TYPE_INVALID;
1381 base_jd_atom_dep_set(&user_atom.pre_dep[0],
1382 user_atom_v6.pre_dep[0],
1385 /* atom number 0 is used for no dependency atoms */
1386 if (!user_atom_v6.pre_dep[1])
1387 dep_types[1] = BASE_JD_DEP_TYPE_INVALID;
1389 base_jd_atom_dep_set(&user_atom.pre_dep[1],
1390 user_atom_v6.pre_dep[1],
1393 user_atom.atom_number = user_atom_v6.atom_number;
1394 user_atom.prio = user_atom_v6.prio;
1395 user_atom.device_nr = user_atom_v6.device_nr;
1397 #endif /* BASE_LEGACY_UK6_SUPPORT */
1398 if (copy_from_user(&user_atom, user_addr, sizeof(user_atom)) != 0) {
1400 KBASE_TIMELINE_ATOMS_IN_FLIGHT(kctx, atomic_sub_return(submit_data->nr_atoms - i, &kctx->timeline.jd_atoms_in_flight));
1403 #ifdef BASE_LEGACY_UK6_SUPPORT
1405 #endif /* BASE_LEGACY_UK6_SUPPORT */
1407 user_addr = (void __user *)((uintptr_t) user_addr + submit_data->stride);
1409 mutex_lock(&jctx->lock);
1410 #ifndef compiletime_assert
1411 #define compiletime_assert_defined
1412 #define compiletime_assert(x, msg) do { switch (0) { case 0: case (x):; } } \
1415 compiletime_assert((1 << (8*sizeof(user_atom.atom_number))) ==
1417 "BASE_JD_ATOM_COUNT and base_atom_id type out of sync");
1418 compiletime_assert(sizeof(user_atom.pre_dep[0].atom_id) ==
1419 sizeof(user_atom.atom_number),
1420 "BASE_JD_ATOM_COUNT and base_atom_id type out of sync");
1421 #ifdef compiletime_assert_defined
1422 #undef compiletime_assert
1423 #undef compiletime_assert_defined
1425 katom = &jctx->atoms[user_atom.atom_number];
1427 /* Record the flush ID for the cache flush optimisation */
1428 katom->flush_id = latest_flush;
1430 while (katom->status != KBASE_JD_ATOM_STATE_UNUSED) {
1431 /* Atom number is already in use, wait for the atom to
1434 mutex_unlock(&jctx->lock);
1436 /* This thread will wait for the atom to complete. Due
1437 * to thread scheduling we are not sure that the other
1438 * thread that owns the atom will also schedule the
1439 * context, so we force the scheduler to be active and
1440 * hence eventually schedule this context at some point
1443 kbase_js_sched_all(kbdev);
1445 if (wait_event_killable(katom->completed,
1447 KBASE_JD_ATOM_STATE_UNUSED) != 0) {
1448 /* We're being killed so the result code
1449 * doesn't really matter
1453 mutex_lock(&jctx->lock);
1456 need_to_try_schedule_context |=
1457 jd_submit_atom(kctx, &user_atom, katom);
1459 /* Register a completed job as a disjoint event when the GPU is in a disjoint state
1460 * (ie. being reset or replaying jobs).
1462 kbase_disjoint_event_potential(kbdev);
1464 mutex_unlock(&jctx->lock);
1467 if (need_to_try_schedule_context)
1468 kbase_js_sched_all(kbdev);
1473 KBASE_EXPORT_TEST_API(kbase_jd_submit);
1475 void kbase_jd_done_worker(struct work_struct *data)
1477 struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom, work);
1478 struct kbase_jd_context *jctx;
1479 struct kbase_context *kctx;
1480 struct kbasep_js_kctx_info *js_kctx_info;
1481 union kbasep_js_policy *js_policy;
1482 struct kbase_device *kbdev;
1483 struct kbasep_js_device_data *js_devdata;
1484 u64 cache_jc = katom->jc;
1485 struct kbasep_js_atom_retained_state katom_retained_state;
1486 bool schedule = false;
1488 base_jd_core_req core_req = katom->core_req;
1489 u64 affinity = katom->affinity;
1490 enum kbase_atom_coreref_state coreref_state = katom->coreref_state;
1492 /* Soft jobs should never reach this function */
1493 KBASE_DEBUG_ASSERT((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0);
1497 kbdev = kctx->kbdev;
1498 js_kctx_info = &kctx->jctx.sched_info;
1499 js_devdata = &kbdev->js_data;
1500 js_policy = &kbdev->js_data.policy;
1502 KBASE_TRACE_ADD(kbdev, JD_DONE_WORKER, kctx, katom, katom->jc, 0);
1504 kbase_backend_complete_wq(kbdev, katom);
1507 * Begin transaction on JD context and JS context
1509 mutex_lock(&jctx->lock);
1510 mutex_lock(&js_devdata->queue_mutex);
1511 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1513 /* This worker only gets called on contexts that are scheduled *in*. This is
1514 * because it only happens in response to an IRQ from a job that was
1517 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
1519 if (katom->event_code == BASE_JD_EVENT_STOPPED) {
1520 /* Atom has been promoted to stopped */
1521 unsigned long flags;
1523 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1524 mutex_unlock(&js_devdata->queue_mutex);
1525 mutex_unlock(&jctx->lock);
1527 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1529 katom->status = KBASE_JD_ATOM_STATE_IN_JS;
1530 kbase_js_unpull(kctx, katom);
1532 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock, flags);
1537 if (katom->event_code != BASE_JD_EVENT_DONE)
1539 "t6xx: GPU fault 0x%02lx from job slot %d\n",
1540 (unsigned long)katom->event_code,
1543 if (kbase_hw_has_issue(kbdev, BASE_HW_ISSUE_8316))
1544 kbase_as_poking_timer_release_atom(kbdev, kctx, katom);
1546 /* Retain state before the katom disappears */
1547 kbasep_js_atom_retained_state_copy(&katom_retained_state, katom);
1549 if (!kbasep_js_has_atom_finished(&katom_retained_state)) {
1550 mutex_lock(&js_devdata->runpool_mutex);
1551 kbasep_js_clear_job_retry_submit(katom);
1552 /* An atom that has been hard-stopped might have previously
1553 * been soft-stopped and has just finished before the hard-stop
1554 * occurred. For this reason, clear the hard-stopped flag */
1555 katom->atom_flags &= ~(KBASE_KATOM_FLAG_BEEN_HARD_STOPPED);
1556 mutex_unlock(&js_devdata->runpool_mutex);
1559 if (kbasep_js_has_atom_finished(&katom_retained_state))
1562 context_idle = kbase_js_complete_atom_wq(kctx, katom);
1564 KBASE_DEBUG_ASSERT(kbasep_js_has_atom_finished(&katom_retained_state));
1566 kbasep_js_remove_job(kbdev, kctx, katom);
1567 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1568 mutex_unlock(&js_devdata->queue_mutex);
1569 katom->atom_flags &= ~KBASE_KATOM_FLAG_HOLDING_CTX_REF;
1570 /* jd_done_nolock() requires the jsctx_mutex lock to be dropped */
1571 schedule |= jd_done_nolock(katom, &kctx->completed_jobs);
1573 /* katom may have been freed now, do not use! */
1576 unsigned long flags;
1578 mutex_lock(&js_devdata->queue_mutex);
1579 spin_lock_irqsave(&js_devdata->runpool_irq.lock, flags);
1581 /* If kbase_sched() has scheduled this context back in then
1582 * ctx_active will have been set after we marked it as inactive,
1583 * and another pm reference will have been taken, so drop our
1584 * reference. But do not call kbase_jm_idle_ctx(), as the
1585 * context is active and fast-starting is allowed.
1587 * If an atom has been fast-started then kctx->atoms_pulled will
1588 * be non-zero but ctx_active will still be false (as the
1589 * previous pm reference has been inherited). Do NOT drop our
1590 * reference, as it has been re-used, and leave the context as
1593 * If no new atoms have been started then ctx_active will still
1594 * be false and atoms_pulled will be zero, so drop the reference
1595 * and call kbase_jm_idle_ctx().
1597 * As the checks are done under both the queue_mutex and
1598 * runpool_irq.lock is should be impossible for this to race
1599 * with the scheduler code.
1601 if (kctx->ctx_active || !atomic_read(&kctx->atoms_pulled)) {
1602 /* Calling kbase_jm_idle_ctx() here will ensure that
1603 * atoms are not fast-started when we drop the
1604 * runpool_irq.lock. This is not performed if ctx_active
1605 * is set as in that case another pm reference has been
1606 * taken and a fast-start would be valid.
1608 if (!kctx->ctx_active)
1609 kbase_jm_idle_ctx(kbdev, kctx);
1610 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
1613 kbase_pm_context_idle(kbdev);
1615 kctx->ctx_active = true;
1616 spin_unlock_irqrestore(&js_devdata->runpool_irq.lock,
1619 mutex_unlock(&js_devdata->queue_mutex);
1623 * Transaction complete
1625 mutex_unlock(&jctx->lock);
1627 /* Job is now no longer running, so can now safely release the context
1628 * reference, and handle any actions that were logged against the atom's retained state */
1630 kbasep_js_runpool_release_ctx_and_katom_retained_state(kbdev, kctx, &katom_retained_state);
1633 kbase_js_sched_all(kbdev);
1635 if (!atomic_dec_return(&kctx->work_count)) {
1636 /* If worker now idle then post all events that jd_done_nolock()
1638 mutex_lock(&jctx->lock);
1639 while (!list_empty(&kctx->completed_jobs)) {
1640 struct kbase_jd_atom *atom = list_entry(
1641 kctx->completed_jobs.next,
1642 struct kbase_jd_atom, dep_item[0]);
1643 list_del(kctx->completed_jobs.next);
1645 kbase_event_post(kctx, atom);
1647 mutex_unlock(&jctx->lock);
1650 kbase_backend_complete_wq_post_sched(kbdev, core_req, affinity,
1653 KBASE_TRACE_ADD(kbdev, JD_DONE_WORKER_END, kctx, NULL, cache_jc, 0);
1657 * jd_cancel_worker - Work queue job cancel function.
1658 * @data: a &struct work_struct
1660 * Only called as part of 'Zapping' a context (which occurs on termination).
1661 * Operates serially with the kbase_jd_done_worker() on the work queue.
1663 * This can only be called on contexts that aren't scheduled.
1665 * We don't need to release most of the resources that would occur on
1666 * kbase_jd_done() or kbase_jd_done_worker(), because the atoms here must not be
1667 * running (by virtue of only being called on contexts that aren't
1670 static void jd_cancel_worker(struct work_struct *data)
1672 struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom, work);
1673 struct kbase_jd_context *jctx;
1674 struct kbase_context *kctx;
1675 struct kbasep_js_kctx_info *js_kctx_info;
1676 bool need_to_try_schedule_context;
1677 bool attr_state_changed;
1678 struct kbase_device *kbdev;
1680 /* Soft jobs should never reach this function */
1681 KBASE_DEBUG_ASSERT((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0);
1684 kbdev = kctx->kbdev;
1686 js_kctx_info = &kctx->jctx.sched_info;
1688 KBASE_TRACE_ADD(kbdev, JD_CANCEL_WORKER, kctx, katom, katom->jc, 0);
1690 /* This only gets called on contexts that are scheduled out. Hence, we must
1691 * make sure we don't de-ref the number of running jobs (there aren't
1692 * any), nor must we try to schedule out the context (it's already
1695 KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1697 /* Scheduler: Remove the job from the system */
1698 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1699 attr_state_changed = kbasep_js_remove_cancelled_job(kbdev, kctx, katom);
1700 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1702 mutex_lock(&jctx->lock);
1704 need_to_try_schedule_context = jd_done_nolock(katom, NULL);
1705 /* Because we're zapping, we're not adding any more jobs to this ctx, so no need to
1706 * schedule the context. There's also no need for the jsctx_mutex to have been taken
1707 * around this too. */
1708 KBASE_DEBUG_ASSERT(!need_to_try_schedule_context);
1710 /* katom may have been freed now, do not use! */
1711 mutex_unlock(&jctx->lock);
1713 if (attr_state_changed)
1714 kbase_js_sched_all(kbdev);
1718 * jd_evict_worker - Work queue job evict function
1719 * @data: a &struct work_struct
1721 * Only called as part of evicting failed jobs. This is only called on jobs that
1722 * were never submitted to HW Access. Jobs that were submitted are handled
1723 * through kbase_jd_done_worker().
1724 * Operates serially with the kbase_jd_done_worker() on the work queue.
1726 * We don't need to release most of the resources that would occur on
1727 * kbase_jd_done() or kbase_jd_done_worker(), because the atoms here must not be
1728 * running (by virtue of having not been submitted to HW Access).
1730 static void jd_evict_worker(struct work_struct *data)
1732 struct kbase_jd_atom *katom = container_of(data, struct kbase_jd_atom,
1734 struct kbase_jd_context *jctx;
1735 struct kbase_context *kctx;
1736 struct kbasep_js_kctx_info *js_kctx_info;
1737 struct kbase_device *kbdev;
1739 /* Soft jobs should never reach this function */
1740 KBASE_DEBUG_ASSERT((katom->core_req & BASE_JD_REQ_SOFT_JOB) == 0);
1743 kbdev = kctx->kbdev;
1745 js_kctx_info = &kctx->jctx.sched_info;
1747 KBASE_TRACE_ADD(kbdev, JD_CANCEL_WORKER, kctx, katom, katom->jc, 0);
1749 /* Scheduler: Remove the job from the system */
1750 mutex_lock(&js_kctx_info->ctx.jsctx_mutex);
1751 kbasep_js_remove_cancelled_job(kbdev, kctx, katom);
1752 mutex_unlock(&js_kctx_info->ctx.jsctx_mutex);
1754 mutex_lock(&jctx->lock);
1755 jd_done_nolock(katom, NULL);
1756 /* katom may have been freed now, do not use! */
1757 mutex_unlock(&jctx->lock);
1759 kbase_js_sched_all(kbdev);
1763 * kbase_jd_done - Complete a job that has been removed from the Hardware
1764 * @katom: atom which has been completed
1765 * @slot_nr: slot the atom was on
1766 * @end_timestamp: completion time
1767 * @done_code: completion code
1769 * This must be used whenever a job has been removed from the Hardware, e.g.:
1770 * An IRQ indicates that the job finished (for both error and 'done' codes), or
1771 * the job was evicted from the JS_HEAD_NEXT registers during a Soft/Hard stop.
1773 * Some work is carried out immediately, and the rest is deferred onto a
1777 * This can be called safely from atomic context.
1778 * The caller must hold kbasep_js_device_data.runpool_irq.lock
1780 void kbase_jd_done(struct kbase_jd_atom *katom, int slot_nr,
1781 ktime_t *end_timestamp, kbasep_js_atom_done_code done_code)
1783 struct kbase_context *kctx;
1784 struct kbase_device *kbdev;
1786 KBASE_DEBUG_ASSERT(katom);
1788 KBASE_DEBUG_ASSERT(kctx);
1789 kbdev = kctx->kbdev;
1790 KBASE_DEBUG_ASSERT(kbdev);
1792 if (done_code & KBASE_JS_ATOM_DONE_EVICTED_FROM_NEXT)
1793 katom->event_code = BASE_JD_EVENT_REMOVED_FROM_NEXT;
1795 KBASE_TRACE_ADD(kbdev, JD_DONE, kctx, katom, katom->jc, 0);
1797 kbase_job_check_leave_disjoint(kbdev, katom);
1799 katom->slot_nr = slot_nr;
1801 atomic_inc(&kctx->work_count);
1803 #ifdef CONFIG_DEBUG_FS
1804 /* a failed job happened and is waiting for dumping*/
1805 if (kbase_debug_job_fault_process(katom, katom->event_code))
1809 WARN_ON(work_pending(&katom->work));
1810 KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
1811 INIT_WORK(&katom->work, kbase_jd_done_worker);
1812 queue_work(kctx->jctx.job_done_wq, &katom->work);
1815 KBASE_EXPORT_TEST_API(kbase_jd_done);
1817 void kbase_jd_cancel(struct kbase_device *kbdev, struct kbase_jd_atom *katom)
1819 struct kbase_context *kctx;
1820 struct kbasep_js_kctx_info *js_kctx_info;
1822 KBASE_DEBUG_ASSERT(NULL != kbdev);
1823 KBASE_DEBUG_ASSERT(NULL != katom);
1825 KBASE_DEBUG_ASSERT(NULL != kctx);
1827 js_kctx_info = &kctx->jctx.sched_info;
1829 KBASE_TRACE_ADD(kbdev, JD_CANCEL, kctx, katom, katom->jc, 0);
1831 /* This should only be done from a context that is not scheduled */
1832 KBASE_DEBUG_ASSERT(!js_kctx_info->ctx.is_scheduled);
1834 WARN_ON(work_pending(&katom->work));
1836 katom->event_code = BASE_JD_EVENT_JOB_CANCELLED;
1838 KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
1839 INIT_WORK(&katom->work, jd_cancel_worker);
1840 queue_work(kctx->jctx.job_done_wq, &katom->work);
1843 void kbase_jd_evict(struct kbase_device *kbdev, struct kbase_jd_atom *katom)
1845 struct kbase_context *kctx;
1846 struct kbasep_js_kctx_info *js_kctx_info;
1848 KBASE_DEBUG_ASSERT(NULL != kbdev);
1849 KBASE_DEBUG_ASSERT(NULL != katom);
1851 KBASE_DEBUG_ASSERT(NULL != kctx);
1853 js_kctx_info = &kctx->jctx.sched_info;
1855 KBASE_TRACE_ADD(kbdev, JD_CANCEL, kctx, katom, katom->jc, 0);
1857 /* This should only be done from a context that is currently scheduled
1859 KBASE_DEBUG_ASSERT(js_kctx_info->ctx.is_scheduled);
1861 WARN_ON(work_pending(&katom->work));
1863 KBASE_DEBUG_ASSERT(0 == object_is_on_stack(&katom->work));
1864 INIT_WORK(&katom->work, jd_evict_worker);
1865 queue_work(kctx->jctx.job_done_wq, &katom->work);
1868 void kbase_jd_zap_context(struct kbase_context *kctx)
1870 struct kbase_jd_atom *katom;
1871 struct list_head *entry, *tmp;
1872 struct kbase_device *kbdev;
1874 KBASE_DEBUG_ASSERT(kctx);
1876 kbdev = kctx->kbdev;
1878 KBASE_TRACE_ADD(kbdev, JD_ZAP_CONTEXT, kctx, NULL, 0u, 0u);
1880 kbase_js_zap_context(kctx);
1882 mutex_lock(&kctx->jctx.lock);
1885 * While holding the struct kbase_jd_context lock clean up jobs which are known to kbase but are
1886 * queued outside the job scheduler.
1889 list_for_each_safe(entry, tmp, &kctx->waiting_soft_jobs) {
1890 katom = list_entry(entry, struct kbase_jd_atom, dep_item[0]);
1891 kbase_cancel_soft_job(katom);
1897 /* For each job waiting on a kds resource, cancel the wait and force the job to
1898 * complete early, this is done so that we don't leave jobs outstanding waiting
1899 * on kds resources which may never be released when contexts are zapped, resulting
1902 * Note that we can safely iterate over the list as the struct kbase_jd_context lock is held,
1903 * this prevents items being removed when calling job_done_nolock in kbase_cancel_kds_wait_job.
1906 list_for_each(entry, &kctx->waiting_kds_resource) {
1907 katom = list_entry(entry, struct kbase_jd_atom, node);
1909 kbase_cancel_kds_wait_job(katom);
1913 mutex_unlock(&kctx->jctx.lock);
1915 kbase_jm_wait_for_zero_jobs(kctx);
1918 KBASE_EXPORT_TEST_API(kbase_jd_zap_context);
1920 int kbase_jd_init(struct kbase_context *kctx)
1926 #endif /* CONFIG_KDS */
1928 KBASE_DEBUG_ASSERT(kctx);
1930 kctx->jctx.job_done_wq = alloc_workqueue("mali_jd", 0, 1);
1931 if (NULL == kctx->jctx.job_done_wq) {
1936 for (i = 0; i < BASE_JD_ATOM_COUNT; i++) {
1937 init_waitqueue_head(&kctx->jctx.atoms[i].completed);
1939 INIT_LIST_HEAD(&kctx->jctx.atoms[i].dep_head[0]);
1940 INIT_LIST_HEAD(&kctx->jctx.atoms[i].dep_head[1]);
1942 /* Catch userspace attempting to use an atom which doesn't exist as a pre-dependency */
1943 kctx->jctx.atoms[i].event_code = BASE_JD_EVENT_JOB_INVALID;
1944 kctx->jctx.atoms[i].status = KBASE_JD_ATOM_STATE_UNUSED;
1947 mutex_init(&kctx->jctx.lock);
1949 init_waitqueue_head(&kctx->jctx.zero_jobs_wait);
1951 spin_lock_init(&kctx->jctx.tb_lock);
1954 err = kds_callback_init(&kctx->jctx.kds_cb, 0, kds_dep_clear);
1959 #endif /* CONFIG_KDS */
1961 kctx->jctx.job_nr = 0;
1962 INIT_LIST_HEAD(&kctx->completed_jobs);
1963 atomic_set(&kctx->work_count, 0);
1969 destroy_workqueue(kctx->jctx.job_done_wq);
1970 #endif /* CONFIG_KDS */
1975 KBASE_EXPORT_TEST_API(kbase_jd_init);
1977 void kbase_jd_exit(struct kbase_context *kctx)
1979 KBASE_DEBUG_ASSERT(kctx);
1982 kds_callback_term(&kctx->jctx.kds_cb);
1983 #endif /* CONFIG_KDS */
1984 /* Work queue is emptied by this */
1985 destroy_workqueue(kctx->jctx.job_done_wq);
1988 KBASE_EXPORT_TEST_API(kbase_jd_exit);