blob: 11ff33841caf19627eac9fc8b9d6795f33c8d15b [file] [log] [blame]
Rob Herringf3ba9122018-09-10 14:27:58 -05001// 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>
6#include <linux/io.h>
7#include <linux/platform_device.h>
8#include <linux/pm_runtime.h>
Christian König52791ee2019-08-11 10:06:32 +02009#include <linux/dma-resv.h>
Rob Herringf3ba9122018-09-10 14:27:58 -050010#include <drm/gpu_scheduler.h>
11#include <drm/panfrost_drm.h>
12
13#include "panfrost_device.h"
14#include "panfrost_devfreq.h"
15#include "panfrost_job.h"
16#include "panfrost_features.h"
17#include "panfrost_issues.h"
18#include "panfrost_gem.h"
19#include "panfrost_regs.h"
20#include "panfrost_gpu.h"
21#include "panfrost_mmu.h"
22
Boris Brezillon5bc5cc22020-11-05 16:17:04 +010023#define JOB_TIMEOUT_MS 500
24
Rob Herringf3ba9122018-09-10 14:27:58 -050025#define job_write(dev, reg, data) writel(data, dev->iomem + (reg))
26#define job_read(dev, reg) readl(dev->iomem + (reg))
27
28struct panfrost_queue_state {
29 struct drm_gpu_scheduler sched;
Rob Herringf3ba9122018-09-10 14:27:58 -050030 u64 fence_context;
31 u64 emit_seqno;
32};
33
34struct panfrost_job_slot {
35 struct panfrost_queue_state queue[NUM_JOB_SLOTS];
36 spinlock_t job_lock;
Boris Brezillon1d0cab52021-06-30 08:27:45 +020037 int irq;
Rob Herringf3ba9122018-09-10 14:27:58 -050038};
39
40static struct panfrost_job *
41to_panfrost_job(struct drm_sched_job *sched_job)
42{
43 return container_of(sched_job, struct panfrost_job, base);
44}
45
46struct panfrost_fence {
47 struct dma_fence base;
48 struct drm_device *dev;
49 /* panfrost seqno for signaled() test */
50 u64 seqno;
51 int queue;
52};
53
54static inline struct panfrost_fence *
55to_panfrost_fence(struct dma_fence *fence)
56{
57 return (struct panfrost_fence *)fence;
58}
59
60static const char *panfrost_fence_get_driver_name(struct dma_fence *fence)
61{
62 return "panfrost";
63}
64
65static const char *panfrost_fence_get_timeline_name(struct dma_fence *fence)
66{
67 struct panfrost_fence *f = to_panfrost_fence(fence);
68
69 switch (f->queue) {
70 case 0:
71 return "panfrost-js-0";
72 case 1:
73 return "panfrost-js-1";
74 case 2:
75 return "panfrost-js-2";
76 default:
77 return NULL;
78 }
79}
80
81static const struct dma_fence_ops panfrost_fence_ops = {
82 .get_driver_name = panfrost_fence_get_driver_name,
83 .get_timeline_name = panfrost_fence_get_timeline_name,
84};
85
86static struct dma_fence *panfrost_fence_create(struct panfrost_device *pfdev, int js_num)
87{
88 struct panfrost_fence *fence;
89 struct panfrost_job_slot *js = pfdev->js;
90
91 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
92 if (!fence)
93 return ERR_PTR(-ENOMEM);
94
95 fence->dev = pfdev->ddev;
96 fence->queue = js_num;
97 fence->seqno = ++js->queue[js_num].emit_seqno;
98 dma_fence_init(&fence->base, &panfrost_fence_ops, &js->job_lock,
99 js->queue[js_num].fence_context, fence->seqno);
100
101 return &fence->base;
102}
103
104static int panfrost_job_get_slot(struct panfrost_job *job)
105{
106 /* JS0: fragment jobs.
107 * JS1: vertex/tiler jobs
108 * JS2: compute jobs
109 */
110 if (job->requirements & PANFROST_JD_REQ_FS)
111 return 0;
112
113/* Not exposed to userspace yet */
114#if 0
115 if (job->requirements & PANFROST_JD_REQ_ONLY_COMPUTE) {
116 if ((job->requirements & PANFROST_JD_REQ_CORE_GRP_MASK) &&
117 (job->pfdev->features.nr_core_groups == 2))
118 return 2;
119 if (panfrost_has_hw_issue(job->pfdev, HW_ISSUE_8987))
120 return 2;
121 }
122#endif
123 return 1;
124}
125
126static void panfrost_job_write_affinity(struct panfrost_device *pfdev,
127 u32 requirements,
128 int js)
129{
130 u64 affinity;
131
132 /*
133 * Use all cores for now.
134 * Eventually we may need to support tiler only jobs and h/w with
135 * multiple (2) coherent core groups
136 */
137 affinity = pfdev->features.shader_present;
138
139 job_write(pfdev, JS_AFFINITY_NEXT_LO(js), affinity & 0xFFFFFFFF);
140 job_write(pfdev, JS_AFFINITY_NEXT_HI(js), affinity >> 32);
141}
142
143static void panfrost_job_hw_submit(struct panfrost_job *job, int js)
144{
145 struct panfrost_device *pfdev = job->pfdev;
Rob Herringf3ba9122018-09-10 14:27:58 -0500146 u32 cfg;
147 u64 jc_head = job->jc;
148 int ret;
149
Clément Péron9bfacfc2020-07-10 11:53:59 +0200150 panfrost_devfreq_record_busy(&pfdev->pfdevfreq);
Steven Priceb99773e2020-05-22 16:36:53 +0100151
Rob Herringf3ba9122018-09-10 14:27:58 -0500152 ret = pm_runtime_get_sync(pfdev->dev);
153 if (ret < 0)
154 return;
155
Rob Herring330bec42019-08-26 17:33:11 -0500156 if (WARN_ON(job_read(pfdev, JS_COMMAND_NEXT(js)))) {
Rob Herring330bec42019-08-26 17:33:11 -0500157 return;
158 }
Rob Herringf3ba9122018-09-10 14:27:58 -0500159
Boris Brezillon7fdc48c2021-06-21 15:38:56 +0200160 cfg = panfrost_mmu_as_get(pfdev, job->file_priv->mmu);
Rob Herringf3ba9122018-09-10 14:27:58 -0500161
162 job_write(pfdev, JS_HEAD_NEXT_LO(js), jc_head & 0xFFFFFFFF);
163 job_write(pfdev, JS_HEAD_NEXT_HI(js), jc_head >> 32);
164
165 panfrost_job_write_affinity(pfdev, job->requirements, js);
166
167 /* start MMU, medium priority, cache clean/flush on end, clean/flush on
168 * start */
Rob Herring7282f762019-08-13 09:01:15 -0600169 cfg |= JS_CONFIG_THREAD_PRI(8) |
Rob Herringf3ba9122018-09-10 14:27:58 -0500170 JS_CONFIG_START_FLUSH_CLEAN_INVALIDATE |
171 JS_CONFIG_END_FLUSH_CLEAN_INVALIDATE;
172
173 if (panfrost_has_hw_feature(pfdev, HW_FEATURE_FLUSH_REDUCTION))
174 cfg |= JS_CONFIG_ENABLE_FLUSH_REDUCTION;
175
176 if (panfrost_has_hw_issue(pfdev, HW_ISSUE_10649))
177 cfg |= JS_CONFIG_START_MMU;
178
179 job_write(pfdev, JS_CONFIG_NEXT(js), cfg);
180
181 if (panfrost_has_hw_feature(pfdev, HW_FEATURE_FLUSH_REDUCTION))
182 job_write(pfdev, JS_FLUSH_ID_NEXT(js), job->flush_id);
183
184 /* GO ! */
185 dev_dbg(pfdev->dev, "JS: Submitting atom %p to js[%d] with head=0x%llx",
186 job, js, jc_head);
187
188 job_write(pfdev, JS_COMMAND_NEXT(js), JS_COMMAND_START);
Rob Herringf3ba9122018-09-10 14:27:58 -0500189}
190
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200191static int panfrost_acquire_object_fences(struct drm_gem_object **bos,
192 int bo_count,
193 struct xarray *deps)
Rob Herringf3ba9122018-09-10 14:27:58 -0500194{
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200195 int i, ret;
Rob Herringf3ba9122018-09-10 14:27:58 -0500196
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200197 for (i = 0; i < bo_count; i++) {
Daniel Vetter7601d532021-06-22 18:55:02 +0200198 /* panfrost always uses write mode in its current uapi */
199 ret = drm_gem_fence_array_add_implicit(deps, bos[i], true);
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200200 if (ret)
201 return ret;
202 }
203
204 return 0;
Rob Herringf3ba9122018-09-10 14:27:58 -0500205}
206
207static void panfrost_attach_object_fences(struct drm_gem_object **bos,
208 int bo_count,
209 struct dma_fence *fence)
210{
211 int i;
212
213 for (i = 0; i < bo_count; i++)
Christian König52791ee2019-08-11 10:06:32 +0200214 dma_resv_add_excl_fence(bos[i]->resv, fence);
Rob Herringf3ba9122018-09-10 14:27:58 -0500215}
216
217int panfrost_job_push(struct panfrost_job *job)
218{
219 struct panfrost_device *pfdev = job->pfdev;
220 int slot = panfrost_job_get_slot(job);
221 struct drm_sched_entity *entity = &job->file_priv->sched_entity[slot];
222 struct ww_acquire_ctx acquire_ctx;
223 int ret = 0;
224
Rob Herringf3ba9122018-09-10 14:27:58 -0500225
226 ret = drm_gem_lock_reservations(job->bos, job->bo_count,
227 &acquire_ctx);
Daniel Vetter94dd80f2021-06-22 18:55:00 +0200228 if (ret)
Rob Herringf3ba9122018-09-10 14:27:58 -0500229 return ret;
Daniel Vetter94dd80f2021-06-22 18:55:00 +0200230
231 mutex_lock(&pfdev->sched_lock);
Rob Herringf3ba9122018-09-10 14:27:58 -0500232
233 ret = drm_sched_job_init(&job->base, entity, NULL);
234 if (ret) {
235 mutex_unlock(&pfdev->sched_lock);
236 goto unlock;
237 }
238
239 job->render_done_fence = dma_fence_get(&job->base.s_fence->finished);
240
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200241 ret = panfrost_acquire_object_fences(job->bos, job->bo_count,
242 &job->deps);
243 if (ret) {
244 mutex_unlock(&pfdev->sched_lock);
245 goto unlock;
246 }
Rob Herringf3ba9122018-09-10 14:27:58 -0500247
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200248 kref_get(&job->refcount); /* put by scheduler job completion */
Rob Herringf3ba9122018-09-10 14:27:58 -0500249
250 drm_sched_entity_push_job(&job->base, entity);
251
252 mutex_unlock(&pfdev->sched_lock);
253
254 panfrost_attach_object_fences(job->bos, job->bo_count,
255 job->render_done_fence);
256
257unlock:
258 drm_gem_unlock_reservations(job->bos, job->bo_count, &acquire_ctx);
259
260 return ret;
261}
262
263static void panfrost_job_cleanup(struct kref *ref)
264{
265 struct panfrost_job *job = container_of(ref, struct panfrost_job,
266 refcount);
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200267 struct dma_fence *fence;
268 unsigned long index;
Rob Herringf3ba9122018-09-10 14:27:58 -0500269 unsigned int i;
270
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200271 xa_for_each(&job->deps, index, fence) {
272 dma_fence_put(fence);
Rob Herringf3ba9122018-09-10 14:27:58 -0500273 }
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200274 xa_destroy(&job->deps);
275
Rob Herringf3ba9122018-09-10 14:27:58 -0500276 dma_fence_put(job->done_fence);
277 dma_fence_put(job->render_done_fence);
278
Boris Brezillonbdefca22020-01-15 20:15:54 -0600279 if (job->mappings) {
Boris Brezillon7e0cf7e2019-11-29 14:59:08 +0100280 for (i = 0; i < job->bo_count; i++) {
281 if (!job->mappings[i])
282 break;
283
284 atomic_dec(&job->mappings[i]->obj->gpu_usecount);
Boris Brezillonbdefca22020-01-15 20:15:54 -0600285 panfrost_gem_mapping_put(job->mappings[i]);
Boris Brezillon7e0cf7e2019-11-29 14:59:08 +0100286 }
Boris Brezillonbdefca22020-01-15 20:15:54 -0600287 kvfree(job->mappings);
288 }
289
290 if (job->bos) {
YueHaibingfe154a22020-02-03 15:27:24 +0000291 for (i = 0; i < job->bo_count; i++)
Emil Velikov496d0cc2020-05-15 10:51:07 +0100292 drm_gem_object_put(job->bos[i]);
Boris Brezillonbdefca22020-01-15 20:15:54 -0600293
Rob Herringf3ba9122018-09-10 14:27:58 -0500294 kvfree(job->bos);
295 }
296
297 kfree(job);
298}
299
300void panfrost_job_put(struct panfrost_job *job)
301{
302 kref_put(&job->refcount, panfrost_job_cleanup);
303}
304
305static void panfrost_job_free(struct drm_sched_job *sched_job)
306{
307 struct panfrost_job *job = to_panfrost_job(sched_job);
308
309 drm_sched_job_cleanup(sched_job);
310
311 panfrost_job_put(job);
312}
313
314static struct dma_fence *panfrost_job_dependency(struct drm_sched_job *sched_job,
315 struct drm_sched_entity *s_entity)
316{
317 struct panfrost_job *job = to_panfrost_job(sched_job);
Rob Herringf3ba9122018-09-10 14:27:58 -0500318
Daniel Vetter7d7a0fc2021-06-22 18:55:01 +0200319 if (!xa_empty(&job->deps))
320 return xa_erase(&job->deps, job->last_dep++);
Rob Herringf3ba9122018-09-10 14:27:58 -0500321
322 return NULL;
323}
324
325static struct dma_fence *panfrost_job_run(struct drm_sched_job *sched_job)
326{
327 struct panfrost_job *job = to_panfrost_job(sched_job);
328 struct panfrost_device *pfdev = job->pfdev;
329 int slot = panfrost_job_get_slot(job);
330 struct dma_fence *fence = NULL;
331
332 if (unlikely(job->base.s_fence->finished.error))
333 return NULL;
334
335 pfdev->jobs[slot] = job;
336
337 fence = panfrost_fence_create(pfdev, slot);
338 if (IS_ERR(fence))
Boris Brezillon9f4e9112021-06-30 08:27:38 +0200339 return fence;
Rob Herringf3ba9122018-09-10 14:27:58 -0500340
341 if (job->done_fence)
342 dma_fence_put(job->done_fence);
343 job->done_fence = dma_fence_get(fence);
344
345 panfrost_job_hw_submit(job, slot);
346
347 return fence;
348}
349
350void panfrost_job_enable_interrupts(struct panfrost_device *pfdev)
351{
352 int j;
353 u32 irq_mask = 0;
354
355 for (j = 0; j < NUM_JOB_SLOTS; j++) {
356 irq_mask |= MK_JS_MASK(j);
357 }
358
359 job_write(pfdev, JOB_INT_CLEAR, irq_mask);
360 job_write(pfdev, JOB_INT_MASK, irq_mask);
361}
362
Boris Brezillona11c4712021-06-30 08:27:44 +0200363static void panfrost_reset(struct panfrost_device *pfdev,
364 struct drm_sched_job *bad)
Boris Brezillon1a11a882020-10-02 14:25:06 +0200365{
Boris Brezillona11c4712021-06-30 08:27:44 +0200366 unsigned int i;
367 bool cookie;
Boris Brezillon1a11a882020-10-02 14:25:06 +0200368
Boris Brezillona11c4712021-06-30 08:27:44 +0200369 if (!atomic_read(&pfdev->reset.pending))
370 return;
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100371
Boris Brezillona11c4712021-06-30 08:27:44 +0200372 /* Stop the schedulers.
373 *
374 * FIXME: We temporarily get out of the dma_fence_signalling section
375 * because the cleanup path generate lockdep splats when taking locks
376 * to release job resources. We should rework the code to follow this
377 * pattern:
378 *
379 * try_lock
380 * if (locked)
381 * release
382 * else
383 * schedule_work_to_release_later
384 */
385 for (i = 0; i < NUM_JOB_SLOTS; i++)
386 drm_sched_stop(&pfdev->js->queue[i].sched, bad);
387
388 cookie = dma_fence_begin_signalling();
389
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100390 if (bad)
391 drm_sched_increase_karma(bad);
392
Boris Brezillon1d0cab52021-06-30 08:27:45 +0200393 /* Mask job interrupts and synchronize to make sure we won't be
394 * interrupted during our reset.
395 */
396 job_write(pfdev, JOB_INT_MASK, 0);
397 synchronize_irq(pfdev->js->irq);
398
399 /* Schedulers are stopped and interrupts are masked+flushed, we don't
400 * need to protect the 'evict unfinished jobs' lock with the job_lock.
401 */
Boris Brezillona11c4712021-06-30 08:27:44 +0200402 spin_lock(&pfdev->js->job_lock);
403 for (i = 0; i < NUM_JOB_SLOTS; i++) {
404 if (pfdev->jobs[i]) {
405 pm_runtime_put_noidle(pfdev->dev);
406 panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
407 pfdev->jobs[i] = NULL;
408 }
409 }
410 spin_unlock(&pfdev->js->job_lock);
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100411
Boris Brezillona11c4712021-06-30 08:27:44 +0200412 panfrost_device_reset(pfdev);
413
414 /* GPU has been reset, we can clear the reset pending bit. */
415 atomic_set(&pfdev->reset.pending, 0);
416
417 /* Now resubmit jobs that were previously queued but didn't have a
418 * chance to finish.
419 * FIXME: We temporarily get out of the DMA fence signalling section
420 * while resubmitting jobs because the job submission logic will
421 * allocate memory with the GFP_KERNEL flag which can trigger memory
422 * reclaim and exposes a lock ordering issue.
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100423 */
Boris Brezillona11c4712021-06-30 08:27:44 +0200424 dma_fence_end_signalling(cookie);
425 for (i = 0; i < NUM_JOB_SLOTS; i++)
426 drm_sched_resubmit_jobs(&pfdev->js->queue[i].sched);
427 cookie = dma_fence_begin_signalling();
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100428
Boris Brezillona11c4712021-06-30 08:27:44 +0200429 for (i = 0; i < NUM_JOB_SLOTS; i++)
430 drm_sched_start(&pfdev->js->queue[i].sched, true);
Boris Brezillon1a11a882020-10-02 14:25:06 +0200431
Boris Brezillona11c4712021-06-30 08:27:44 +0200432 dma_fence_end_signalling(cookie);
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100433}
434
Luben Tuikova6a1f032021-01-20 15:09:59 -0500435static enum drm_gpu_sched_stat panfrost_job_timedout(struct drm_sched_job
436 *sched_job)
Rob Herringf3ba9122018-09-10 14:27:58 -0500437{
438 struct panfrost_job *job = to_panfrost_job(sched_job);
439 struct panfrost_device *pfdev = job->pfdev;
440 int js = panfrost_job_get_slot(job);
Rob Herringf3ba9122018-09-10 14:27:58 -0500441
442 /*
443 * If the GPU managed to complete this jobs fence, the timeout is
444 * spurious. Bail out.
445 */
446 if (dma_fence_is_signaled(job->done_fence))
Luben Tuikova6a1f032021-01-20 15:09:59 -0500447 return DRM_GPU_SCHED_STAT_NOMINAL;
Rob Herringf3ba9122018-09-10 14:27:58 -0500448
Rob Herring7282f762019-08-13 09:01:15 -0600449 dev_err(pfdev->dev, "gpu sched timeout, js=%d, config=0x%x, status=0x%x, head=0x%x, tail=0x%x, sched_job=%p",
Rob Herringf3ba9122018-09-10 14:27:58 -0500450 js,
Rob Herring7282f762019-08-13 09:01:15 -0600451 job_read(pfdev, JS_CONFIG(js)),
Rob Herringf3ba9122018-09-10 14:27:58 -0500452 job_read(pfdev, JS_STATUS(js)),
453 job_read(pfdev, JS_HEAD_LO(js)),
454 job_read(pfdev, JS_TAIL_LO(js)),
455 sched_job);
456
Boris Brezillona11c4712021-06-30 08:27:44 +0200457 atomic_set(&pfdev->reset.pending, 1);
458 panfrost_reset(pfdev, sched_job);
Luben Tuikova6a1f032021-01-20 15:09:59 -0500459
460 return DRM_GPU_SCHED_STAT_NOMINAL;
Rob Herringf3ba9122018-09-10 14:27:58 -0500461}
462
463static const struct drm_sched_backend_ops panfrost_sched_ops = {
464 .dependency = panfrost_job_dependency,
465 .run_job = panfrost_job_run,
466 .timedout_job = panfrost_job_timedout,
467 .free_job = panfrost_job_free
468};
469
Boris Brezillon070ce762021-06-30 08:27:43 +0200470static void panfrost_job_handle_irq(struct panfrost_device *pfdev, u32 status)
Rob Herringf3ba9122018-09-10 14:27:58 -0500471{
Rob Herringf3ba9122018-09-10 14:27:58 -0500472 int j;
473
474 dev_dbg(pfdev->dev, "jobslot irq status=%x\n", status);
475
Rob Herringf3ba9122018-09-10 14:27:58 -0500476 for (j = 0; status; j++) {
477 u32 mask = MK_JS_MASK(j);
478
479 if (!(status & mask))
480 continue;
481
482 job_write(pfdev, JOB_INT_CLEAR, mask);
483
484 if (status & JOB_INT_MASK_ERR(j)) {
485 job_write(pfdev, JS_COMMAND_NEXT(j), JS_COMMAND_NOP);
486
487 dev_err(pfdev->dev, "js fault, js=%d, status=%s, head=0x%x, tail=0x%x",
488 j,
Boris Brezillon6ef2f372021-06-30 08:27:40 +0200489 panfrost_exception_name(job_read(pfdev, JS_STATUS(j))),
Rob Herringf3ba9122018-09-10 14:27:58 -0500490 job_read(pfdev, JS_HEAD_LO(j)),
491 job_read(pfdev, JS_TAIL_LO(j)));
Boris Brezillona11c4712021-06-30 08:27:44 +0200492 drm_sched_fault(&pfdev->js->queue[j].sched);
Rob Herringf3ba9122018-09-10 14:27:58 -0500493 }
494
495 if (status & JOB_INT_MASK_DONE(j)) {
Rob Herring330bec42019-08-26 17:33:11 -0500496 struct panfrost_job *job;
Rob Herring7282f762019-08-13 09:01:15 -0600497
Rob Herring330bec42019-08-26 17:33:11 -0500498 job = pfdev->jobs[j];
Boris Brezillon1d0cab52021-06-30 08:27:45 +0200499 /* The only reason this job could be NULL is if the
500 * job IRQ handler is called just after the
501 * in-flight job eviction in the reset path, and
502 * this shouldn't happen because the job IRQ has
503 * been masked and synchronized when this eviction
504 * happens.
505 */
506 WARN_ON(!job);
Rob Herring330bec42019-08-26 17:33:11 -0500507 if (job) {
508 pfdev->jobs[j] = NULL;
509
Boris Brezillon7fdc48c2021-06-21 15:38:56 +0200510 panfrost_mmu_as_put(pfdev, job->file_priv->mmu);
Clément Péron9bfacfc2020-07-10 11:53:59 +0200511 panfrost_devfreq_record_idle(&pfdev->pfdevfreq);
Rob Herring330bec42019-08-26 17:33:11 -0500512
513 dma_fence_signal_locked(job->done_fence);
514 pm_runtime_put_autosuspend(pfdev->dev);
515 }
Rob Herringf3ba9122018-09-10 14:27:58 -0500516 }
517
518 status &= ~mask;
519 }
Boris Brezillon070ce762021-06-30 08:27:43 +0200520}
Rob Herringf3ba9122018-09-10 14:27:58 -0500521
Boris Brezillon070ce762021-06-30 08:27:43 +0200522static irqreturn_t panfrost_job_irq_handler_thread(int irq, void *data)
523{
524 struct panfrost_device *pfdev = data;
525 u32 status = job_read(pfdev, JOB_INT_RAWSTAT);
526
527 while (status) {
528 pm_runtime_mark_last_busy(pfdev->dev);
529
530 spin_lock(&pfdev->js->job_lock);
531 panfrost_job_handle_irq(pfdev, status);
532 spin_unlock(&pfdev->js->job_lock);
533 status = job_read(pfdev, JOB_INT_RAWSTAT);
534 }
535
536 job_write(pfdev, JOB_INT_MASK,
537 GENMASK(16 + NUM_JOB_SLOTS - 1, 16) |
538 GENMASK(NUM_JOB_SLOTS - 1, 0));
Rob Herringf3ba9122018-09-10 14:27:58 -0500539 return IRQ_HANDLED;
540}
541
Boris Brezillon070ce762021-06-30 08:27:43 +0200542static irqreturn_t panfrost_job_irq_handler(int irq, void *data)
543{
544 struct panfrost_device *pfdev = data;
545 u32 status = job_read(pfdev, JOB_INT_STAT);
546
547 if (!status)
548 return IRQ_NONE;
549
550 job_write(pfdev, JOB_INT_MASK, 0);
551 return IRQ_WAKE_THREAD;
552}
553
Boris Brezillona11c4712021-06-30 08:27:44 +0200554static void panfrost_reset_work(struct work_struct *work)
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100555{
556 struct panfrost_device *pfdev = container_of(work,
557 struct panfrost_device,
558 reset.work);
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100559
Boris Brezillona11c4712021-06-30 08:27:44 +0200560 panfrost_reset(pfdev, NULL);
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100561}
562
Rob Herringf3ba9122018-09-10 14:27:58 -0500563int panfrost_job_init(struct panfrost_device *pfdev)
564{
565 struct panfrost_job_slot *js;
Boris Brezillon1d0cab52021-06-30 08:27:45 +0200566 int ret, j;
Rob Herringf3ba9122018-09-10 14:27:58 -0500567
Boris Brezillona11c4712021-06-30 08:27:44 +0200568 INIT_WORK(&pfdev->reset.work, panfrost_reset_work);
Boris Brezillon5bc5cc22020-11-05 16:17:04 +0100569
Rob Herringf3ba9122018-09-10 14:27:58 -0500570 pfdev->js = js = devm_kzalloc(pfdev->dev, sizeof(*js), GFP_KERNEL);
571 if (!js)
572 return -ENOMEM;
573
574 spin_lock_init(&js->job_lock);
575
Boris Brezillon1d0cab52021-06-30 08:27:45 +0200576 js->irq = platform_get_irq_byname(to_platform_device(pfdev->dev), "job");
577 if (js->irq <= 0)
Rob Herringf3ba9122018-09-10 14:27:58 -0500578 return -ENODEV;
579
Boris Brezillon1d0cab52021-06-30 08:27:45 +0200580 ret = devm_request_threaded_irq(pfdev->dev, js->irq,
Boris Brezillon070ce762021-06-30 08:27:43 +0200581 panfrost_job_irq_handler,
582 panfrost_job_irq_handler_thread,
583 IRQF_SHARED, KBUILD_MODNAME "-job",
584 pfdev);
Rob Herringf3ba9122018-09-10 14:27:58 -0500585 if (ret) {
586 dev_err(pfdev->dev, "failed to request job irq");
587 return ret;
588 }
589
Boris Brezillona11c4712021-06-30 08:27:44 +0200590 pfdev->reset.wq = alloc_ordered_workqueue("panfrost-reset", 0);
591 if (!pfdev->reset.wq)
592 return -ENOMEM;
Steven Pricea17d6092020-10-29 17:00:47 +0000593
Boris Brezillona11c4712021-06-30 08:27:44 +0200594 for (j = 0; j < NUM_JOB_SLOTS; j++) {
Rob Herringf3ba9122018-09-10 14:27:58 -0500595 js->queue[j].fence_context = dma_fence_context_alloc(1);
596
597 ret = drm_sched_init(&js->queue[j].sched,
598 &panfrost_sched_ops,
Boris Brezillon78efe212021-06-30 08:27:37 +0200599 1, 0,
Boris Brezillona11c4712021-06-30 08:27:44 +0200600 msecs_to_jiffies(JOB_TIMEOUT_MS),
601 pfdev->reset.wq,
Christian Königf2f12eb2021-02-02 12:40:01 +0100602 NULL, "pan_js");
Rob Herringf3ba9122018-09-10 14:27:58 -0500603 if (ret) {
604 dev_err(pfdev->dev, "Failed to create scheduler: %d.", ret);
605 goto err_sched;
606 }
607 }
608
609 panfrost_job_enable_interrupts(pfdev);
610
611 return 0;
612
613err_sched:
614 for (j--; j >= 0; j--)
615 drm_sched_fini(&js->queue[j].sched);
616
Boris Brezillona11c4712021-06-30 08:27:44 +0200617 destroy_workqueue(pfdev->reset.wq);
Rob Herringf3ba9122018-09-10 14:27:58 -0500618 return ret;
619}
620
621void panfrost_job_fini(struct panfrost_device *pfdev)
622{
623 struct panfrost_job_slot *js = pfdev->js;
624 int j;
625
626 job_write(pfdev, JOB_INT_MASK, 0);
627
Steven Pricea17d6092020-10-29 17:00:47 +0000628 for (j = 0; j < NUM_JOB_SLOTS; j++) {
Rob Herringf3ba9122018-09-10 14:27:58 -0500629 drm_sched_fini(&js->queue[j].sched);
Steven Pricea17d6092020-10-29 17:00:47 +0000630 }
Rob Herringf3ba9122018-09-10 14:27:58 -0500631
Boris Brezillona11c4712021-06-30 08:27:44 +0200632 cancel_work_sync(&pfdev->reset.work);
633 destroy_workqueue(pfdev->reset.wq);
Rob Herringf3ba9122018-09-10 14:27:58 -0500634}
635
636int panfrost_job_open(struct panfrost_file_priv *panfrost_priv)
637{
638 struct panfrost_device *pfdev = panfrost_priv->pfdev;
639 struct panfrost_job_slot *js = pfdev->js;
Nirmoy Dasb3ac1762019-12-05 11:38:00 +0100640 struct drm_gpu_scheduler *sched;
Rob Herringf3ba9122018-09-10 14:27:58 -0500641 int ret, i;
642
643 for (i = 0; i < NUM_JOB_SLOTS; i++) {
Nirmoy Dasb3ac1762019-12-05 11:38:00 +0100644 sched = &js->queue[i].sched;
645 ret = drm_sched_entity_init(&panfrost_priv->sched_entity[i],
646 DRM_SCHED_PRIORITY_NORMAL, &sched,
647 1, NULL);
Rob Herringf3ba9122018-09-10 14:27:58 -0500648 if (WARN_ON(ret))
649 return ret;
650 }
651 return 0;
652}
653
654void panfrost_job_close(struct panfrost_file_priv *panfrost_priv)
655{
656 int i;
657
Steven Pricea17d6092020-10-29 17:00:47 +0000658 for (i = 0; i < NUM_JOB_SLOTS; i++)
Rob Herringf3ba9122018-09-10 14:27:58 -0500659 drm_sched_entity_destroy(&panfrost_priv->sched_entity[i]);
660}
661
662int panfrost_job_is_idle(struct panfrost_device *pfdev)
663{
664 struct panfrost_job_slot *js = pfdev->js;
665 int i;
666
667 for (i = 0; i < NUM_JOB_SLOTS; i++) {
668 /* If there are any jobs in the HW queue, we're not idle */
669 if (atomic_read(&js->queue[i].sched.hw_rq_count))
670 return false;
Rob Herringf3ba9122018-09-10 14:27:58 -0500671 }
672
673 return true;
674}