1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright 2019 Linaro, Ltd, Rob Herring <robh@kernel.org> */
3 /* Copyright 2019 Collabora ltd. */
4 #include <linux/delay.h>
5 #include <linux/interrupt.h>
7 #include <linux/iopoll.h>
8 #include <linux/platform_device.h>
9 #include <linux/pm_runtime.h>
10 #include <linux/dma-resv.h>
11 #include <drm/gpu_scheduler.h>
12 #include <drm/panfrost_drm.h>
14 #include "panfrost_device.h"
15 #include "panfrost_devfreq.h"
16 #include "panfrost_job.h"
17 #include "panfrost_features.h"
18 #include "panfrost_issues.h"
19 #include "panfrost_gem.h"
20 #include "panfrost_regs.h"
21 #include "panfrost_gpu.h"
22 #include "panfrost_mmu.h"
24 #define JOB_TIMEOUT_MS 500
26 #define job_write(dev, reg, data) writel(data, dev->iomem + (reg))
27 #define job_read(dev, reg) readl(dev->iomem + (reg))
29 struct panfrost_queue_state {
30 struct drm_gpu_scheduler sched;
35 struct panfrost_job_slot {
36 struct panfrost_queue_state queue[NUM_JOB_SLOTS];
41 static struct panfrost_job *
42 to_panfrost_job(struct drm_sched_job *sched_job)
44 return container_of(sched_job, struct panfrost_job, base);
47 struct panfrost_fence {
48 struct dma_fence base;
49 struct drm_device *dev;
50 /* panfrost seqno for signaled() test */
55 static inline struct panfrost_fence *
56 to_panfrost_fence(struct dma_fence *fence)
58 return (struct panfrost_fence *)fence;
61 static const char *panfrost_fence_get_driver_name(struct dma_fence *fence)
66 static const char *panfrost_fence_get_timeline_name(struct dma_fence *fence)
68 struct panfrost_fence *f = to_panfrost_fence(fence);
72 return "panfrost-js-0";
74 return "panfrost-js-1";
76 return "panfrost-js-2";
82 static const struct dma_fence_ops panfrost_fence_ops = {
83 .get_driver_name = panfrost_fence_get_driver_name,
84 .get_timeline_name = panfrost_fence_get_timeline_name,
87 static struct dma_fence *panfrost_fence_create(struct panfrost_device *pfdev, int js_num)
89 struct panfrost_fence *fence;
90 struct panfrost_job_slot *js = pfdev->js;
92 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
94 return ERR_PTR(-ENOMEM);
96 fence->dev = pfdev->ddev;
97 fence->queue = js_num;
98 fence->seqno = ++js->queue[js_num].emit_seqno;
99 dma_fence_init(&fence->base, &panfrost_fence_ops, &js->job_lock,
100 js->queue[js_num].fence_context, fence->seqno);
105 int panfrost_job_get_slot(struct panfrost_job *job)
107 /* JS0: fragment jobs.
108 * JS1: vertex/tiler jobs
111 if (job->requirements & PANFROST_JD_REQ_FS)
114 /* Not exposed to userspace yet */
116 if (job->requirements & PANFROST_JD_REQ_ONLY_COMPUTE) {
117 if ((job->requirements & PANFROST_JD_REQ_CORE_GRP_MASK) &&
118 (job->pfdev->features.nr_core_groups == 2))
120 if (panfrost_has_hw_issue(job->pfdev, HW_ISSUE_8987))
127 static void panfrost_job_write_affinity(struct panfrost_device *pfdev,
134 * Use all cores for now.
135 * Eventually we may need to support tiler only jobs and h/w with
136 * multiple (2) coherent core groups
138 affinity = pfdev->features.shader_present;
140 job_write(pfdev, JS_AFFINITY_NEXT_LO(js), lower_32_bits(affinity));
141 job_write(pfdev, JS_AFFINITY_NEXT_HI(js), upper_32_bits(affinity));
145 panfrost_get_job_chain_flag(const struct panfrost_job *job)
147 struct panfrost_fence *f = to_panfrost_fence(job->done_fence);
149 if (!panfrost_has_hw_feature(job->pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION))
152 return (f->seqno & 1) ? JS_CONFIG_JOB_CHAIN_FLAG : 0;
155 static struct panfrost_job *
156 panfrost_dequeue_job(struct panfrost_device *pfdev, int slot)
158 struct panfrost_job *job = pfdev->jobs[slot][0];
161 pfdev->jobs[slot][0] = pfdev->jobs[slot][1];
162 pfdev->jobs[slot][1] = NULL;
168 panfrost_enqueue_job(struct panfrost_device *pfdev, int slot,
169 struct panfrost_job *job)
174 if (!pfdev->jobs[slot][0]) {
175 pfdev->jobs[slot][0] = job;
179 WARN_ON(pfdev->jobs[slot][1]);
180 pfdev->jobs[slot][1] = job;
181 WARN_ON(panfrost_get_job_chain_flag(job) ==
182 panfrost_get_job_chain_flag(pfdev->jobs[slot][0]));
186 static void panfrost_job_hw_submit(struct panfrost_job *job, int js)
188 struct panfrost_device *pfdev = job->pfdev;
189 unsigned int subslot;
191 u64 jc_head = job->jc;
194 panfrost_devfreq_record_busy(&pfdev->pfdevfreq);
196 ret = pm_runtime_get_sync(pfdev->dev);
200 if (WARN_ON(job_read(pfdev, JS_COMMAND_NEXT(js)))) {
204 cfg = panfrost_mmu_as_get(pfdev, job->mmu);
206 job_write(pfdev, JS_HEAD_NEXT_LO(js), lower_32_bits(jc_head));
207 job_write(pfdev, JS_HEAD_NEXT_HI(js), upper_32_bits(jc_head));
209 panfrost_job_write_affinity(pfdev, job->requirements, js);
211 /* start MMU, medium priority, cache clean/flush on end, clean/flush on
213 cfg |= JS_CONFIG_THREAD_PRI(8) |
214 JS_CONFIG_START_FLUSH_CLEAN_INVALIDATE |
215 JS_CONFIG_END_FLUSH_CLEAN_INVALIDATE |
216 panfrost_get_job_chain_flag(job);
218 if (panfrost_has_hw_feature(pfdev, HW_FEATURE_FLUSH_REDUCTION))
219 cfg |= JS_CONFIG_ENABLE_FLUSH_REDUCTION;
221 if (panfrost_has_hw_issue(pfdev, HW_ISSUE_10649))
222 cfg |= JS_CONFIG_START_MMU;
224 job_write(pfdev, JS_CONFIG_NEXT(js), cfg);
226 if (panfrost_has_hw_feature(pfdev, HW_FEATURE_FLUSH_REDUCTION))
227 job_write(pfdev, JS_FLUSH_ID_NEXT(js), job->flush_id);
231 spin_lock(&pfdev->js->job_lock);
232 subslot = panfrost_enqueue_job(pfdev, js, job);
233 /* Don't queue the job if a reset is in progress */
234 if (!atomic_read(&pfdev->reset.pending)) {
235 job_write(pfdev, JS_COMMAND_NEXT(js), JS_COMMAND_START);
237 "JS: Submitting atom %p to js[%d][%d] with head=0x%llx AS %d",
238 job, js, subslot, jc_head, cfg & 0xf);
240 spin_unlock(&pfdev->js->job_lock);
243 static int panfrost_acquire_object_fences(struct drm_gem_object **bos,
245 struct drm_sched_job *job)
249 for (i = 0; i < bo_count; i++) {
250 ret = dma_resv_reserve_fences(bos[i]->resv, 1);
254 /* panfrost always uses write mode in its current uapi */
255 ret = drm_sched_job_add_implicit_dependencies(job, bos[i],
264 static void panfrost_attach_object_fences(struct drm_gem_object **bos,
266 struct dma_fence *fence)
270 for (i = 0; i < bo_count; i++)
271 dma_resv_add_fence(bos[i]->resv, fence, DMA_RESV_USAGE_WRITE);
274 int panfrost_job_push(struct panfrost_job *job)
276 struct panfrost_device *pfdev = job->pfdev;
277 struct ww_acquire_ctx acquire_ctx;
280 ret = drm_gem_lock_reservations(job->bos, job->bo_count,
285 mutex_lock(&pfdev->sched_lock);
286 drm_sched_job_arm(&job->base);
288 job->render_done_fence = dma_fence_get(&job->base.s_fence->finished);
290 ret = panfrost_acquire_object_fences(job->bos, job->bo_count,
293 mutex_unlock(&pfdev->sched_lock);
297 kref_get(&job->refcount); /* put by scheduler job completion */
299 drm_sched_entity_push_job(&job->base);
301 mutex_unlock(&pfdev->sched_lock);
303 panfrost_attach_object_fences(job->bos, job->bo_count,
304 job->render_done_fence);
307 drm_gem_unlock_reservations(job->bos, job->bo_count, &acquire_ctx);
312 static void panfrost_job_cleanup(struct kref *ref)
314 struct panfrost_job *job = container_of(ref, struct panfrost_job,
318 dma_fence_put(job->done_fence);
319 dma_fence_put(job->render_done_fence);
322 for (i = 0; i < job->bo_count; i++) {
323 if (!job->mappings[i])
326 atomic_dec(&job->mappings[i]->obj->gpu_usecount);
327 panfrost_gem_mapping_put(job->mappings[i]);
329 kvfree(job->mappings);
333 for (i = 0; i < job->bo_count; i++)
334 drm_gem_object_put(job->bos[i]);
342 void panfrost_job_put(struct panfrost_job *job)
344 kref_put(&job->refcount, panfrost_job_cleanup);
347 static void panfrost_job_free(struct drm_sched_job *sched_job)
349 struct panfrost_job *job = to_panfrost_job(sched_job);
351 drm_sched_job_cleanup(sched_job);
353 panfrost_job_put(job);
356 static struct dma_fence *panfrost_job_run(struct drm_sched_job *sched_job)
358 struct panfrost_job *job = to_panfrost_job(sched_job);
359 struct panfrost_device *pfdev = job->pfdev;
360 int slot = panfrost_job_get_slot(job);
361 struct dma_fence *fence = NULL;
363 if (unlikely(job->base.s_fence->finished.error))
366 /* Nothing to execute: can happen if the job has finished while
367 * we were resetting the GPU.
372 fence = panfrost_fence_create(pfdev, slot);
377 dma_fence_put(job->done_fence);
378 job->done_fence = dma_fence_get(fence);
380 panfrost_job_hw_submit(job, slot);
385 void panfrost_job_enable_interrupts(struct panfrost_device *pfdev)
390 for (j = 0; j < NUM_JOB_SLOTS; j++) {
391 irq_mask |= MK_JS_MASK(j);
394 job_write(pfdev, JOB_INT_CLEAR, irq_mask);
395 job_write(pfdev, JOB_INT_MASK, irq_mask);
398 static void panfrost_job_handle_err(struct panfrost_device *pfdev,
399 struct panfrost_job *job,
402 u32 js_status = job_read(pfdev, JS_STATUS(js));
403 const char *exception_name = panfrost_exception_name(js_status);
404 bool signal_fence = true;
406 if (!panfrost_exception_is_fault(js_status)) {
407 dev_dbg(pfdev->dev, "js event, js=%d, status=%s, head=0x%x, tail=0x%x",
409 job_read(pfdev, JS_HEAD_LO(js)),
410 job_read(pfdev, JS_TAIL_LO(js)));
412 dev_err(pfdev->dev, "js fault, js=%d, status=%s, head=0x%x, tail=0x%x",
414 job_read(pfdev, JS_HEAD_LO(js)),
415 job_read(pfdev, JS_TAIL_LO(js)));
418 if (js_status == DRM_PANFROST_EXCEPTION_STOPPED) {
419 /* Update the job head so we can resume */
420 job->jc = job_read(pfdev, JS_TAIL_LO(js)) |
421 ((u64)job_read(pfdev, JS_TAIL_HI(js)) << 32);
423 /* The job will be resumed, don't signal the fence */
424 signal_fence = false;
425 } else if (js_status == DRM_PANFROST_EXCEPTION_TERMINATED) {
426 /* Job has been hard-stopped, flag it as canceled */
427 dma_fence_set_error(job->done_fence, -ECANCELED);
429 } else if (panfrost_exception_is_fault(js_status)) {
430 /* We might want to provide finer-grained error code based on
431 * the exception type, but unconditionally setting to EINVAL
432 * is good enough for now.
434 dma_fence_set_error(job->done_fence, -EINVAL);
438 panfrost_mmu_as_put(pfdev, job->mmu);
439 panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
442 dma_fence_signal_locked(job->done_fence);
444 pm_runtime_put_autosuspend(pfdev->dev);
446 if (panfrost_exception_needs_reset(pfdev, js_status)) {
447 atomic_set(&pfdev->reset.pending, 1);
448 drm_sched_fault(&pfdev->js->queue[js].sched);
452 static void panfrost_job_handle_done(struct panfrost_device *pfdev,
453 struct panfrost_job *job)
455 /* Set ->jc to 0 to avoid re-submitting an already finished job (can
456 * happen when we receive the DONE interrupt while doing a GPU reset).
459 panfrost_mmu_as_put(pfdev, job->mmu);
460 panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
462 dma_fence_signal_locked(job->done_fence);
463 pm_runtime_put_autosuspend(pfdev->dev);
466 static void panfrost_job_handle_irq(struct panfrost_device *pfdev, u32 status)
468 struct panfrost_job *done[NUM_JOB_SLOTS][2] = {};
469 struct panfrost_job *failed[NUM_JOB_SLOTS] = {};
470 u32 js_state = 0, js_events = 0;
473 /* First we collect all failed/done jobs. */
475 u32 js_state_mask = 0;
477 for (j = 0; j < NUM_JOB_SLOTS; j++) {
478 if (status & MK_JS_MASK(j))
479 js_state_mask |= MK_JS_MASK(j);
481 if (status & JOB_INT_MASK_DONE(j)) {
483 done[j][1] = panfrost_dequeue_job(pfdev, j);
485 done[j][0] = panfrost_dequeue_job(pfdev, j);
488 if (status & JOB_INT_MASK_ERR(j)) {
489 /* Cancel the next submission. Will be submitted
490 * after we're done handling this failure if
491 * there's no reset pending.
493 job_write(pfdev, JS_COMMAND_NEXT(j), JS_COMMAND_NOP);
494 failed[j] = panfrost_dequeue_job(pfdev, j);
498 /* JS_STATE is sampled when JOB_INT_CLEAR is written.
499 * For each BIT(slot) or BIT(slot + 16) bit written to
500 * JOB_INT_CLEAR, the corresponding bits in JS_STATE
501 * (BIT(slot) and BIT(slot + 16)) are updated, but this
502 * is racy. If we only have one job done at the time we
503 * read JOB_INT_RAWSTAT but the second job fails before we
504 * clear the status, we end up with a status containing
505 * only the DONE bit and consider both jobs as DONE since
506 * JS_STATE reports both NEXT and CURRENT as inactive.
507 * To prevent that, let's repeat this clear+read steps
510 job_write(pfdev, JOB_INT_CLEAR, status);
511 js_state &= ~js_state_mask;
512 js_state |= job_read(pfdev, JOB_INT_JS_STATE) & js_state_mask;
514 status = job_read(pfdev, JOB_INT_RAWSTAT);
517 /* Then we handle the dequeued jobs. */
518 for (j = 0; j < NUM_JOB_SLOTS; j++) {
519 if (!(js_events & MK_JS_MASK(j)))
523 panfrost_job_handle_err(pfdev, failed[j], j);
524 } else if (pfdev->jobs[j][0] && !(js_state & MK_JS_MASK(j))) {
525 /* When the current job doesn't fail, the JM dequeues
526 * the next job without waiting for an ACK, this means
527 * we can have 2 jobs dequeued and only catch the
528 * interrupt when the second one is done. If both slots
529 * are inactive, but one job remains in pfdev->jobs[j],
530 * consider it done. Of course that doesn't apply if a
531 * failure happened since we cancelled execution of the
532 * job in _NEXT (see above).
534 if (WARN_ON(!done[j][0]))
535 done[j][0] = panfrost_dequeue_job(pfdev, j);
537 done[j][1] = panfrost_dequeue_job(pfdev, j);
540 for (i = 0; i < ARRAY_SIZE(done[0]) && done[j][i]; i++)
541 panfrost_job_handle_done(pfdev, done[j][i]);
544 /* And finally we requeue jobs that were waiting in the second slot
545 * and have been stopped if we detected a failure on the first slot.
547 for (j = 0; j < NUM_JOB_SLOTS; j++) {
548 if (!(js_events & MK_JS_MASK(j)))
551 if (!failed[j] || !pfdev->jobs[j][0])
554 if (pfdev->jobs[j][0]->jc == 0) {
555 /* The job was cancelled, signal the fence now */
556 struct panfrost_job *canceled = panfrost_dequeue_job(pfdev, j);
558 dma_fence_set_error(canceled->done_fence, -ECANCELED);
559 panfrost_job_handle_done(pfdev, canceled);
560 } else if (!atomic_read(&pfdev->reset.pending)) {
561 /* Requeue the job we removed if no reset is pending */
562 job_write(pfdev, JS_COMMAND_NEXT(j), JS_COMMAND_START);
567 static void panfrost_job_handle_irqs(struct panfrost_device *pfdev)
569 u32 status = job_read(pfdev, JOB_INT_RAWSTAT);
572 pm_runtime_mark_last_busy(pfdev->dev);
574 spin_lock(&pfdev->js->job_lock);
575 panfrost_job_handle_irq(pfdev, status);
576 spin_unlock(&pfdev->js->job_lock);
577 status = job_read(pfdev, JOB_INT_RAWSTAT);
581 static u32 panfrost_active_slots(struct panfrost_device *pfdev,
582 u32 *js_state_mask, u32 js_state)
586 if (!(js_state & *js_state_mask))
589 rawstat = job_read(pfdev, JOB_INT_RAWSTAT);
593 for (i = 0; i < NUM_JOB_SLOTS; i++) {
594 if (rawstat & MK_JS_MASK(i))
595 *js_state_mask &= ~MK_JS_MASK(i);
599 return js_state & *js_state_mask;
603 panfrost_reset(struct panfrost_device *pfdev,
604 struct drm_sched_job *bad)
606 u32 js_state, js_state_mask = 0xffffffff;
611 if (!atomic_read(&pfdev->reset.pending))
614 /* Stop the schedulers.
616 * FIXME: We temporarily get out of the dma_fence_signalling section
617 * because the cleanup path generate lockdep splats when taking locks
618 * to release job resources. We should rework the code to follow this
625 * schedule_work_to_release_later
627 for (i = 0; i < NUM_JOB_SLOTS; i++)
628 drm_sched_stop(&pfdev->js->queue[i].sched, bad);
630 cookie = dma_fence_begin_signalling();
633 drm_sched_increase_karma(bad);
635 /* Mask job interrupts and synchronize to make sure we won't be
636 * interrupted during our reset.
638 job_write(pfdev, JOB_INT_MASK, 0);
639 synchronize_irq(pfdev->js->irq);
641 for (i = 0; i < NUM_JOB_SLOTS; i++) {
642 /* Cancel the next job and soft-stop the running job. */
643 job_write(pfdev, JS_COMMAND_NEXT(i), JS_COMMAND_NOP);
644 job_write(pfdev, JS_COMMAND(i), JS_COMMAND_SOFT_STOP);
647 /* Wait at most 10ms for soft-stops to complete */
648 ret = readl_poll_timeout(pfdev->iomem + JOB_INT_JS_STATE, js_state,
649 !panfrost_active_slots(pfdev, &js_state_mask, js_state),
653 dev_err(pfdev->dev, "Soft-stop failed\n");
655 /* Handle the remaining interrupts before we reset. */
656 panfrost_job_handle_irqs(pfdev);
658 /* Remaining interrupts have been handled, but we might still have
659 * stuck jobs. Let's make sure the PM counters stay balanced by
660 * manually calling pm_runtime_put_noidle() and
661 * panfrost_devfreq_record_idle() for each stuck job.
663 spin_lock(&pfdev->js->job_lock);
664 for (i = 0; i < NUM_JOB_SLOTS; i++) {
665 for (j = 0; j < ARRAY_SIZE(pfdev->jobs[0]) && pfdev->jobs[i][j]; j++) {
666 pm_runtime_put_noidle(pfdev->dev);
667 panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
670 memset(pfdev->jobs, 0, sizeof(pfdev->jobs));
671 spin_unlock(&pfdev->js->job_lock);
673 /* Proceed with reset now. */
674 panfrost_device_reset(pfdev);
676 /* panfrost_device_reset() unmasks job interrupts, but we want to
677 * keep them masked a bit longer.
679 job_write(pfdev, JOB_INT_MASK, 0);
681 /* GPU has been reset, we can clear the reset pending bit. */
682 atomic_set(&pfdev->reset.pending, 0);
684 /* Now resubmit jobs that were previously queued but didn't have a
686 * FIXME: We temporarily get out of the DMA fence signalling section
687 * while resubmitting jobs because the job submission logic will
688 * allocate memory with the GFP_KERNEL flag which can trigger memory
689 * reclaim and exposes a lock ordering issue.
691 dma_fence_end_signalling(cookie);
692 for (i = 0; i < NUM_JOB_SLOTS; i++)
693 drm_sched_resubmit_jobs(&pfdev->js->queue[i].sched);
694 cookie = dma_fence_begin_signalling();
696 /* Restart the schedulers */
697 for (i = 0; i < NUM_JOB_SLOTS; i++)
698 drm_sched_start(&pfdev->js->queue[i].sched, true);
700 /* Re-enable job interrupts now that everything has been restarted. */
701 job_write(pfdev, JOB_INT_MASK,
702 GENMASK(16 + NUM_JOB_SLOTS - 1, 16) |
703 GENMASK(NUM_JOB_SLOTS - 1, 0));
705 dma_fence_end_signalling(cookie);
708 static enum drm_gpu_sched_stat panfrost_job_timedout(struct drm_sched_job
711 struct panfrost_job *job = to_panfrost_job(sched_job);
712 struct panfrost_device *pfdev = job->pfdev;
713 int js = panfrost_job_get_slot(job);
716 * If the GPU managed to complete this jobs fence, the timeout is
717 * spurious. Bail out.
719 if (dma_fence_is_signaled(job->done_fence))
720 return DRM_GPU_SCHED_STAT_NOMINAL;
722 dev_err(pfdev->dev, "gpu sched timeout, js=%d, config=0x%x, status=0x%x, head=0x%x, tail=0x%x, sched_job=%p",
724 job_read(pfdev, JS_CONFIG(js)),
725 job_read(pfdev, JS_STATUS(js)),
726 job_read(pfdev, JS_HEAD_LO(js)),
727 job_read(pfdev, JS_TAIL_LO(js)),
730 atomic_set(&pfdev->reset.pending, 1);
731 panfrost_reset(pfdev, sched_job);
733 return DRM_GPU_SCHED_STAT_NOMINAL;
736 static void panfrost_reset_work(struct work_struct *work)
738 struct panfrost_device *pfdev;
740 pfdev = container_of(work, struct panfrost_device, reset.work);
741 panfrost_reset(pfdev, NULL);
744 static const struct drm_sched_backend_ops panfrost_sched_ops = {
745 .run_job = panfrost_job_run,
746 .timedout_job = panfrost_job_timedout,
747 .free_job = panfrost_job_free
750 static irqreturn_t panfrost_job_irq_handler_thread(int irq, void *data)
752 struct panfrost_device *pfdev = data;
754 panfrost_job_handle_irqs(pfdev);
755 job_write(pfdev, JOB_INT_MASK,
756 GENMASK(16 + NUM_JOB_SLOTS - 1, 16) |
757 GENMASK(NUM_JOB_SLOTS - 1, 0));
761 static irqreturn_t panfrost_job_irq_handler(int irq, void *data)
763 struct panfrost_device *pfdev = data;
764 u32 status = job_read(pfdev, JOB_INT_STAT);
769 job_write(pfdev, JOB_INT_MASK, 0);
770 return IRQ_WAKE_THREAD;
773 int panfrost_job_init(struct panfrost_device *pfdev)
775 struct panfrost_job_slot *js;
776 unsigned int nentries = 2;
779 /* All GPUs have two entries per queue, but without jobchain
780 * disambiguation stopping the right job in the close path is tricky,
781 * so let's just advertise one entry in that case.
783 if (!panfrost_has_hw_feature(pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION))
786 pfdev->js = js = devm_kzalloc(pfdev->dev, sizeof(*js), GFP_KERNEL);
790 INIT_WORK(&pfdev->reset.work, panfrost_reset_work);
791 spin_lock_init(&js->job_lock);
793 js->irq = platform_get_irq_byname(to_platform_device(pfdev->dev), "job");
797 ret = devm_request_threaded_irq(pfdev->dev, js->irq,
798 panfrost_job_irq_handler,
799 panfrost_job_irq_handler_thread,
800 IRQF_SHARED, KBUILD_MODNAME "-job",
803 dev_err(pfdev->dev, "failed to request job irq");
807 pfdev->reset.wq = alloc_ordered_workqueue("panfrost-reset", 0);
808 if (!pfdev->reset.wq)
811 for (j = 0; j < NUM_JOB_SLOTS; j++) {
812 js->queue[j].fence_context = dma_fence_context_alloc(1);
814 ret = drm_sched_init(&js->queue[j].sched,
817 msecs_to_jiffies(JOB_TIMEOUT_MS),
819 NULL, "pan_js", pfdev->dev);
821 dev_err(pfdev->dev, "Failed to create scheduler: %d.", ret);
826 panfrost_job_enable_interrupts(pfdev);
831 for (j--; j >= 0; j--)
832 drm_sched_fini(&js->queue[j].sched);
834 destroy_workqueue(pfdev->reset.wq);
838 void panfrost_job_fini(struct panfrost_device *pfdev)
840 struct panfrost_job_slot *js = pfdev->js;
843 job_write(pfdev, JOB_INT_MASK, 0);
845 for (j = 0; j < NUM_JOB_SLOTS; j++) {
846 drm_sched_fini(&js->queue[j].sched);
849 cancel_work_sync(&pfdev->reset.work);
850 destroy_workqueue(pfdev->reset.wq);
853 int panfrost_job_open(struct panfrost_file_priv *panfrost_priv)
855 struct panfrost_device *pfdev = panfrost_priv->pfdev;
856 struct panfrost_job_slot *js = pfdev->js;
857 struct drm_gpu_scheduler *sched;
860 for (i = 0; i < NUM_JOB_SLOTS; i++) {
861 sched = &js->queue[i].sched;
862 ret = drm_sched_entity_init(&panfrost_priv->sched_entity[i],
863 DRM_SCHED_PRIORITY_NORMAL, &sched,
871 void panfrost_job_close(struct panfrost_file_priv *panfrost_priv)
873 struct panfrost_device *pfdev = panfrost_priv->pfdev;
876 for (i = 0; i < NUM_JOB_SLOTS; i++)
877 drm_sched_entity_destroy(&panfrost_priv->sched_entity[i]);
879 /* Kill in-flight jobs */
880 spin_lock(&pfdev->js->job_lock);
881 for (i = 0; i < NUM_JOB_SLOTS; i++) {
882 struct drm_sched_entity *entity = &panfrost_priv->sched_entity[i];
885 for (j = ARRAY_SIZE(pfdev->jobs[0]) - 1; j >= 0; j--) {
886 struct panfrost_job *job = pfdev->jobs[i][j];
889 if (!job || job->base.entity != entity)
893 /* Try to cancel the job before it starts */
894 job_write(pfdev, JS_COMMAND_NEXT(i), JS_COMMAND_NOP);
895 /* Reset the job head so it doesn't get restarted if
896 * the job in the first slot failed.
901 if (panfrost_has_hw_feature(pfdev, HW_FEATURE_JOBCHAIN_DISAMBIGUATION)) {
902 cmd = panfrost_get_job_chain_flag(job) ?
903 JS_COMMAND_HARD_STOP_1 :
904 JS_COMMAND_HARD_STOP_0;
906 cmd = JS_COMMAND_HARD_STOP;
909 job_write(pfdev, JS_COMMAND(i), cmd);
912 spin_unlock(&pfdev->js->job_lock);
915 int panfrost_job_is_idle(struct panfrost_device *pfdev)
917 struct panfrost_job_slot *js = pfdev->js;
920 for (i = 0; i < NUM_JOB_SLOTS; i++) {
921 /* If there are any jobs in the HW queue, we're not idle */
922 if (atomic_read(&js->queue[i].sched.hw_rq_count))