1 // SPDX-License-Identifier: GPL-2.0-only OR MIT
2 /* Copyright 2024-2025 Tomeu Vizoso <tomeu@tomeuvizoso.net> */
3 /* Copyright 2025-2026 Arm, Ltd. */
4
5 #include <linux/bitfield.h>
6 #include <linux/genalloc.h>
7 #include <linux/interrupt.h>
8 #include <linux/iopoll.h>
9 #include <linux/platform_device.h>
10 #include <linux/pm_runtime.h>
11
12 #include <drm/drm_file.h>
13 #include <drm/drm_gem.h>
14 #include <drm/drm_gem_dma_helper.h>
15 #include <drm/drm_print.h>
16 #include <drm/ethosu_accel.h>
17
18 #include "ethosu_device.h"
19 #include "ethosu_drv.h"
20 #include "ethosu_gem.h"
21 #include "ethosu_job.h"
22
23 #define JOB_TIMEOUT_MS 500
24
to_ethosu_job(struct drm_sched_job * sched_job)25 static struct ethosu_job *to_ethosu_job(struct drm_sched_job *sched_job)
26 {
27 return container_of(sched_job, struct ethosu_job, base);
28 }
29
ethosu_fence_get_driver_name(struct dma_fence * fence)30 static const char *ethosu_fence_get_driver_name(struct dma_fence *fence)
31 {
32 return "ethosu";
33 }
34
ethosu_fence_get_timeline_name(struct dma_fence * fence)35 static const char *ethosu_fence_get_timeline_name(struct dma_fence *fence)
36 {
37 return "ethosu-npu";
38 }
39
40 static const struct dma_fence_ops ethosu_fence_ops = {
41 .get_driver_name = ethosu_fence_get_driver_name,
42 .get_timeline_name = ethosu_fence_get_timeline_name,
43 };
44
ethosu_job_hw_submit(struct ethosu_device * dev,struct ethosu_job * job)45 static void ethosu_job_hw_submit(struct ethosu_device *dev, struct ethosu_job *job)
46 {
47 struct drm_gem_dma_object *cmd_bo = to_drm_gem_dma_obj(job->cmd_bo);
48 struct ethosu_validated_cmdstream_info *cmd_info = to_ethosu_bo(job->cmd_bo)->info;
49
50 for (int i = 0; i < job->region_cnt; i++) {
51 struct drm_gem_dma_object *bo;
52 int region = job->region_bo_num[i];
53
54 bo = to_drm_gem_dma_obj(job->region_bo[i]);
55 writel_relaxed(lower_32_bits(bo->dma_addr), dev->regs + NPU_REG_BASEP(region));
56 writel_relaxed(upper_32_bits(bo->dma_addr), dev->regs + NPU_REG_BASEP_HI(region));
57 dev_dbg(dev->base.dev, "Region %d base addr = %pad\n", region, &bo->dma_addr);
58 }
59
60 if (job->sram_size) {
61 writel_relaxed(lower_32_bits(dev->sramphys),
62 dev->regs + NPU_REG_BASEP(ETHOSU_SRAM_REGION));
63 writel_relaxed(upper_32_bits(dev->sramphys),
64 dev->regs + NPU_REG_BASEP_HI(ETHOSU_SRAM_REGION));
65 dev_dbg(dev->base.dev, "Region %d base addr = %pad (SRAM)\n",
66 ETHOSU_SRAM_REGION, &dev->sramphys);
67 }
68
69 writel_relaxed(lower_32_bits(cmd_bo->dma_addr), dev->regs + NPU_REG_QBASE);
70 writel_relaxed(upper_32_bits(cmd_bo->dma_addr), dev->regs + NPU_REG_QBASE_HI);
71 writel_relaxed(cmd_info->cmd_size, dev->regs + NPU_REG_QSIZE);
72
73 writel(CMD_TRANSITION_TO_RUN, dev->regs + NPU_REG_CMD);
74
75 dev_dbg(dev->base.dev,
76 "Submitted cmd at %pad to core\n", &cmd_bo->dma_addr);
77 }
78
ethosu_acquire_object_fences(struct ethosu_job * job)79 static int ethosu_acquire_object_fences(struct ethosu_job *job)
80 {
81 int i, ret;
82 struct drm_gem_object **bos = job->region_bo;
83 struct ethosu_validated_cmdstream_info *info = to_ethosu_bo(job->cmd_bo)->info;
84
85 for (i = 0; i < job->region_cnt; i++) {
86 bool is_write;
87
88 if (!bos[i])
89 break;
90
91 ret = dma_resv_reserve_fences(bos[i]->resv, 1);
92 if (ret)
93 return ret;
94
95 is_write = info->output_region[job->region_bo_num[i]];
96 ret = drm_sched_job_add_implicit_dependencies(&job->base, bos[i],
97 is_write);
98 if (ret)
99 return ret;
100 }
101
102 return 0;
103 }
104
ethosu_attach_object_fences(struct ethosu_job * job)105 static void ethosu_attach_object_fences(struct ethosu_job *job)
106 {
107 int i;
108 struct dma_fence *fence = job->inference_done_fence;
109 struct drm_gem_object **bos = job->region_bo;
110 struct ethosu_validated_cmdstream_info *info = to_ethosu_bo(job->cmd_bo)->info;
111
112 for (i = 0; i < job->region_cnt; i++)
113 if (info->output_region[job->region_bo_num[i]])
114 dma_resv_add_fence(bos[i]->resv, fence, DMA_RESV_USAGE_WRITE);
115 }
116
ethosu_job_push(struct ethosu_job * job)117 static int ethosu_job_push(struct ethosu_job *job)
118 {
119 struct ww_acquire_ctx acquire_ctx;
120 int ret;
121
122 ret = drm_gem_lock_reservations(job->region_bo, job->region_cnt, &acquire_ctx);
123 if (ret)
124 return ret;
125
126 ret = ethosu_acquire_object_fences(job);
127 if (ret)
128 goto out;
129
130 ret = pm_runtime_resume_and_get(job->dev->base.dev);
131 if (!ret) {
132 guard(mutex)(&job->dev->sched_lock);
133
134 drm_sched_job_arm(&job->base);
135 job->inference_done_fence = dma_fence_get(&job->base.s_fence->finished);
136 kref_get(&job->refcount); /* put by scheduler job completion */
137 drm_sched_entity_push_job(&job->base);
138 ethosu_attach_object_fences(job);
139 }
140
141 out:
142 drm_gem_unlock_reservations(job->region_bo, job->region_cnt, &acquire_ctx);
143 return ret;
144 }
145
ethosu_job_err_cleanup(struct ethosu_job * job)146 static void ethosu_job_err_cleanup(struct ethosu_job *job)
147 {
148 unsigned int i;
149
150 ethosu_perfmon_put(job->perfmon);
151
152 for (i = 0; i < job->region_cnt; i++)
153 drm_gem_object_put(job->region_bo[i]);
154
155 drm_gem_object_put(job->cmd_bo);
156
157 if (job->done_fence) {
158 if (dma_fence_was_initialized(job->done_fence))
159 dma_fence_put(job->done_fence);
160 else
161 dma_fence_free(job->done_fence);
162 }
163
164 kfree(job);
165 }
166
ethosu_job_cleanup(struct kref * ref)167 static void ethosu_job_cleanup(struct kref *ref)
168 {
169 struct ethosu_job *job = container_of(ref, struct ethosu_job,
170 refcount);
171
172 pm_runtime_put_autosuspend(job->dev->base.dev);
173
174 dma_fence_put(job->inference_done_fence);
175
176 ethosu_job_err_cleanup(job);
177 }
178
ethosu_job_put(struct ethosu_job * job)179 static void ethosu_job_put(struct ethosu_job *job)
180 {
181 kref_put(&job->refcount, ethosu_job_cleanup);
182 }
183
ethosu_job_free(struct drm_sched_job * sched_job)184 static void ethosu_job_free(struct drm_sched_job *sched_job)
185 {
186 struct ethosu_job *job = to_ethosu_job(sched_job);
187
188 drm_sched_job_cleanup(sched_job);
189 ethosu_job_put(job);
190 }
191
192 static void
ethosu_switch_perfmon(struct ethosu_device * ethosu,struct ethosu_job * job)193 ethosu_switch_perfmon(struct ethosu_device *ethosu, struct ethosu_job *job)
194 {
195 struct ethosu_perfmon *perfmon;
196
197 guard(mutex)(ðosu->perfmon_state.lock);
198
199 perfmon = ethosu->global_perfmon;
200 if (!perfmon)
201 perfmon = job->perfmon;
202
203 if (perfmon == ethosu->perfmon_state.active)
204 return;
205
206 ethosu_perfmon_stop_locked(ethosu, ethosu->perfmon_state.active, true);
207
208 if (perfmon)
209 ethosu_perfmon_start(ethosu, perfmon);
210 }
211
ethosu_job_run(struct drm_sched_job * sched_job)212 static struct dma_fence *ethosu_job_run(struct drm_sched_job *sched_job)
213 {
214 struct ethosu_job *job = to_ethosu_job(sched_job);
215 struct ethosu_device *dev = job->dev;
216 struct dma_fence *fence = job->done_fence;
217
218 if (unlikely(job->base.s_fence->finished.error))
219 return NULL;
220
221 dma_fence_init(fence, ðosu_fence_ops, &dev->fence_lock,
222 dev->fence_context, ++dev->emit_seqno);
223 dma_fence_get(fence);
224
225 ethosu_switch_perfmon(dev, job);
226
227 WRITE_ONCE(dev->in_flight_job, job);
228 ethosu_job_hw_submit(dev, job);
229
230 return fence;
231 }
232
ethosu_job_handle_irq(struct ethosu_device * dev)233 static void ethosu_job_handle_irq(struct ethosu_device *dev)
234 {
235 u32 status = readl_relaxed(dev->regs + NPU_REG_STATUS);
236 struct ethosu_job *job;
237
238 if (status & (STATUS_BUS_STATUS | STATUS_CMD_PARSE_ERR)) {
239 dev_err(dev->base.dev, "Error IRQ - %x\n", status);
240 drm_sched_fault(&dev->sched);
241 return;
242 }
243
244 job = READ_ONCE(dev->in_flight_job);
245 if (job) {
246 WRITE_ONCE(dev->in_flight_job, NULL);
247 dma_fence_signal(job->done_fence);
248 }
249 }
250
ethosu_job_irq_handler_thread(int irq,void * data)251 static irqreturn_t ethosu_job_irq_handler_thread(int irq, void *data)
252 {
253 struct ethosu_device *dev = data;
254
255 ethosu_job_handle_irq(dev);
256
257 return IRQ_HANDLED;
258 }
259
ethosu_job_irq_handler(int irq,void * data)260 static irqreturn_t ethosu_job_irq_handler(int irq, void *data)
261 {
262 struct ethosu_device *dev = data;
263 u32 status = readl_relaxed(dev->regs + NPU_REG_STATUS);
264
265 if (!(status & STATUS_IRQ_RAISED))
266 return IRQ_NONE;
267
268 writel_relaxed(CMD_CLEAR_IRQ, dev->regs + NPU_REG_CMD);
269 return IRQ_WAKE_THREAD;
270 }
271
ethosu_job_timedout(struct drm_sched_job * bad)272 static enum drm_gpu_sched_stat ethosu_job_timedout(struct drm_sched_job *bad)
273 {
274 struct ethosu_job *job = to_ethosu_job(bad);
275 struct ethosu_device *dev = job->dev;
276 bool running;
277 u32 *bocmds = to_drm_gem_dma_obj(job->cmd_bo)->vaddr;
278 u32 cmdaddr;
279
280 cmdaddr = readl_relaxed(dev->regs + NPU_REG_QREAD);
281 running = FIELD_GET(STATUS_STATE_RUNNING, readl_relaxed(dev->regs + NPU_REG_STATUS));
282
283 if (running) {
284 int ret;
285 u32 reg;
286
287 ret = readl_relaxed_poll_timeout(dev->regs + NPU_REG_QREAD,
288 reg,
289 reg != cmdaddr,
290 USEC_PER_MSEC, 100 * USEC_PER_MSEC);
291
292 /* If still running and progress is being made, just return */
293 if (!ret)
294 return DRM_GPU_SCHED_STAT_NO_HANG;
295 }
296
297 dev_err(dev->base.dev, "NPU sched timed out: NPU %s, cmdstream offset 0x%x: 0x%x\n",
298 running ? "running" : "stopped",
299 cmdaddr, bocmds[cmdaddr / 4]);
300
301 drm_sched_stop(&dev->sched, bad);
302
303 WRITE_ONCE(dev->in_flight_job, NULL);
304
305 /* Proceed with reset now. */
306 pm_runtime_force_suspend(dev->base.dev);
307 pm_runtime_force_resume(dev->base.dev);
308
309 /* Restart the scheduler */
310 drm_sched_start(&dev->sched, 0);
311
312 return DRM_GPU_SCHED_STAT_RESET;
313 }
314
315 static const struct drm_sched_backend_ops ethosu_sched_ops = {
316 .run_job = ethosu_job_run,
317 .timedout_job = ethosu_job_timedout,
318 .free_job = ethosu_job_free
319 };
320
ethosu_job_init(struct ethosu_device * edev)321 int ethosu_job_init(struct ethosu_device *edev)
322 {
323 struct device *dev = edev->base.dev;
324 struct drm_sched_init_args args = {
325 .ops = ðosu_sched_ops,
326 .num_rqs = DRM_SCHED_PRIORITY_COUNT,
327 .credit_limit = 1,
328 .timeout = msecs_to_jiffies(JOB_TIMEOUT_MS),
329 .name = dev_name(dev),
330 .dev = dev,
331 };
332 int ret;
333
334 spin_lock_init(&edev->fence_lock);
335 ret = devm_mutex_init(dev, &edev->sched_lock);
336 if (ret)
337 return ret;
338
339 edev->irq = platform_get_irq(to_platform_device(dev), 0);
340 if (edev->irq < 0)
341 return edev->irq;
342
343 ret = devm_request_threaded_irq(dev, edev->irq,
344 ethosu_job_irq_handler,
345 ethosu_job_irq_handler_thread,
346 0, KBUILD_MODNAME,
347 edev);
348 if (ret) {
349 dev_err(dev, "failed to request irq\n");
350 return ret;
351 }
352
353 edev->fence_context = dma_fence_context_alloc(1);
354
355 ret = drm_sched_init(&edev->sched, &args);
356 if (ret) {
357 dev_err(dev, "Failed to create scheduler: %d\n", ret);
358 goto err_sched;
359 }
360
361 return 0;
362
363 err_sched:
364 drm_sched_fini(&edev->sched);
365 return ret;
366 }
367
ethosu_job_fini(struct ethosu_device * dev)368 void ethosu_job_fini(struct ethosu_device *dev)
369 {
370 drm_sched_fini(&dev->sched);
371 }
372
ethosu_job_open(struct ethosu_file_priv * ethosu_priv)373 int ethosu_job_open(struct ethosu_file_priv *ethosu_priv)
374 {
375 struct ethosu_device *dev = ethosu_priv->edev;
376 struct drm_gpu_scheduler *sched = &dev->sched;
377
378 return drm_sched_entity_init(ðosu_priv->sched_entity,
379 DRM_SCHED_PRIORITY_NORMAL,
380 &sched, 1, NULL);
381 }
382
ethosu_job_close(struct ethosu_file_priv * ethosu_priv)383 void ethosu_job_close(struct ethosu_file_priv *ethosu_priv)
384 {
385 struct drm_sched_entity *entity = ðosu_priv->sched_entity;
386
387 drm_sched_entity_destroy(entity);
388 }
389
ethosu_ioctl_submit_job(struct drm_device * dev,struct drm_file * file,struct drm_ethosu_job * job,int perfmon_id)390 static int ethosu_ioctl_submit_job(struct drm_device *dev, struct drm_file *file,
391 struct drm_ethosu_job *job,
392 int perfmon_id)
393 {
394 struct ethosu_device *edev = to_ethosu_device(dev);
395 struct ethosu_file_priv *file_priv = file->driver_priv;
396 struct ethosu_job *ejob = NULL;
397 struct ethosu_validated_cmdstream_info *cmd_info;
398 int ret = 0;
399
400 /* BO region 2 is reserved if SRAM is used */
401 if (job->region_bo_handles[ETHOSU_SRAM_REGION] && job->sram_size)
402 return -EINVAL;
403
404 if (edev->npu_info.sram_size < job->sram_size)
405 return -EINVAL;
406
407 ejob = kzalloc_obj(*ejob);
408 if (!ejob)
409 return -ENOMEM;
410
411 kref_init(&ejob->refcount);
412
413 ejob->dev = edev;
414 ejob->sram_size = job->sram_size;
415
416 if (perfmon_id)
417 ejob->perfmon = ethosu_perfmon_find(file_priv, perfmon_id);
418
419 ejob->done_fence = kzalloc_obj(*ejob->done_fence);
420 if (!ejob->done_fence) {
421 ret = -ENOMEM;
422 goto out_put_job;
423 }
424
425 ret = drm_sched_job_init(&ejob->base,
426 &file_priv->sched_entity,
427 1, NULL, file->client_id);
428 if (ret)
429 goto out_put_job;
430
431 ejob->cmd_bo = drm_gem_object_lookup(file, job->cmd_bo);
432 if (!ejob->cmd_bo) {
433 ret = -ENOENT;
434 goto out_cleanup_job;
435 }
436 cmd_info = to_ethosu_bo(ejob->cmd_bo)->info;
437 if (!cmd_info) {
438 ret = -EINVAL;
439 goto out_cleanup_job;
440 }
441
442 for (int i = 0; i < NPU_BASEP_REGION_MAX; i++) {
443 struct drm_gem_object *gem;
444
445 /* Can only omit a BO handle if the region is not used or used for SRAM */
446 if (!job->region_bo_handles[i]) {
447 if (!cmd_info->region_size[i])
448 continue;
449 if (i == ETHOSU_SRAM_REGION) {
450 if (cmd_info->region_size[i] <= ejob->sram_size)
451 continue;
452
453 dev_err(dev->dev,
454 "cmd stream region %d size greater than job SRAM size (%llu > %u)\n",
455 i, cmd_info->region_size[i],
456 ejob->sram_size);
457 ret = -EINVAL;
458 goto out_cleanup_job;
459 }
460 }
461
462 if (job->region_bo_handles[i] && !cmd_info->region_size[i]) {
463 dev_err(dev->dev,
464 "Cmdstream BO handle %d set for unused region %d\n",
465 job->region_bo_handles[i], i);
466 ret = -EINVAL;
467 goto out_cleanup_job;
468 }
469
470 gem = drm_gem_object_lookup(file, job->region_bo_handles[i]);
471 if (!gem) {
472 dev_err(dev->dev,
473 "Invalid BO handle %d for region %d\n",
474 job->region_bo_handles[i], i);
475 ret = -ENOENT;
476 goto out_cleanup_job;
477 }
478
479 ejob->region_bo[ejob->region_cnt] = gem;
480 ejob->region_bo_num[ejob->region_cnt] = i;
481 ejob->region_cnt++;
482
483 if (to_ethosu_bo(gem)->info) {
484 dev_err(dev->dev,
485 "Cmdstream BO handle %d used for region %d\n",
486 job->region_bo_handles[i], i);
487 ret = -EINVAL;
488 goto out_cleanup_job;
489 }
490
491 /* Verify the command stream doesn't have accesses outside the BO */
492 if (cmd_info->region_size[i] > gem->size) {
493 dev_err(dev->dev,
494 "cmd stream region %d size greater than BO size (%llu > %zu)\n",
495 i, cmd_info->region_size[i], gem->size);
496 ret = -EOVERFLOW;
497 goto out_cleanup_job;
498 }
499 }
500 ret = ethosu_job_push(ejob);
501 if (!ret) {
502 ethosu_job_put(ejob);
503 return 0;
504 }
505
506 out_cleanup_job:
507 if (ret)
508 drm_sched_job_cleanup(&ejob->base);
509 out_put_job:
510 ethosu_job_err_cleanup(ejob);
511
512 return ret;
513 }
514
ethosu_ioctl_submit(struct drm_device * dev,void * data,struct drm_file * file)515 int ethosu_ioctl_submit(struct drm_device *dev, void *data, struct drm_file *file)
516 {
517 struct drm_ethosu_submit *args = data;
518 int ret = 0;
519 unsigned int i = 0;
520
521 struct drm_ethosu_job __free(kvfree) *jobs =
522 kvmalloc_objs(*jobs, args->job_count);
523 if (!jobs)
524 return -ENOMEM;
525
526 if (copy_from_user(jobs,
527 (void __user *)(uintptr_t)args->jobs,
528 args->job_count * sizeof(*jobs))) {
529 drm_dbg(dev, "Failed to copy incoming job array\n");
530 return -EFAULT;
531 }
532
533 for (i = 0; i < args->job_count; i++) {
534 ret = ethosu_ioctl_submit_job(dev, file, &jobs[i], args->perfmon_id);
535 if (ret)
536 return ret;
537 }
538
539 return 0;
540 }
541