1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020-2026 Intel Corporation
4 */
5
6 #include <drm/drm_file.h>
7
8 #include <linux/bitfield.h>
9 #include <linux/highmem.h>
10 #include <linux/pci.h>
11 #include <linux/pm_runtime.h>
12 #include <linux/module.h>
13 #include <uapi/drm/ivpu_accel.h>
14
15 #include "ivpu_drv.h"
16 #include "ivpu_fw.h"
17 #include "ivpu_hw.h"
18 #include "ivpu_ipc.h"
19 #include "ivpu_job.h"
20 #include "ivpu_jsm_msg.h"
21 #include "ivpu_mmu.h"
22 #include "ivpu_pm.h"
23 #include "ivpu_trace.h"
24 #include "vpu_boot_api.h"
25
26 #define CMD_BUF_IDX 0
27 #define JOB_MAX_BUFFER_COUNT 65535
28
ivpu_cmdq_ring_db(struct ivpu_device * vdev,struct ivpu_cmdq * cmdq)29 static void ivpu_cmdq_ring_db(struct ivpu_device *vdev, struct ivpu_cmdq *cmdq)
30 {
31 ivpu_hw_db_set(vdev, cmdq->db_id);
32 }
33
ivpu_preemption_buffers_create(struct ivpu_device * vdev,struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)34 static int ivpu_preemption_buffers_create(struct ivpu_device *vdev,
35 struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
36 {
37 if (ivpu_fw_preempt_buf_size(vdev) == 0)
38 return 0;
39
40 cmdq->primary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &vdev->hw->ranges.user,
41 vdev->fw->primary_preempt_buf_size,
42 DRM_IVPU_BO_WC);
43 if (!cmdq->primary_preempt_buf) {
44 ivpu_err(vdev, "Failed to create primary preemption buffer\n");
45 return -ENOMEM;
46 }
47
48 cmdq->secondary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &vdev->hw->ranges.dma,
49 vdev->fw->secondary_preempt_buf_size,
50 DRM_IVPU_BO_WC);
51 if (!cmdq->secondary_preempt_buf) {
52 ivpu_err(vdev, "Failed to create secondary preemption buffer\n");
53 goto err_free_primary;
54 }
55
56 return 0;
57
58 err_free_primary:
59 ivpu_bo_free(cmdq->primary_preempt_buf);
60 cmdq->primary_preempt_buf = NULL;
61 return -ENOMEM;
62 }
63
ivpu_preemption_buffers_free(struct ivpu_device * vdev,struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)64 static void ivpu_preemption_buffers_free(struct ivpu_device *vdev,
65 struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
66 {
67 if (cmdq->primary_preempt_buf)
68 ivpu_bo_free(cmdq->primary_preempt_buf);
69 if (cmdq->secondary_preempt_buf)
70 ivpu_bo_free(cmdq->secondary_preempt_buf);
71 }
72
ivpu_preemption_job_init(struct ivpu_device * vdev,struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq,struct ivpu_job * job)73 static int ivpu_preemption_job_init(struct ivpu_device *vdev, struct ivpu_file_priv *file_priv,
74 struct ivpu_cmdq *cmdq, struct ivpu_job *job)
75 {
76 int ret;
77
78 /* Use preemption buffer provided by the user space */
79 if (job->primary_preempt_buf)
80 return 0;
81
82 if (!cmdq->primary_preempt_buf) {
83 /* Allocate per command queue preemption buffers */
84 ret = ivpu_preemption_buffers_create(vdev, file_priv, cmdq);
85 if (ret)
86 return ret;
87 }
88
89 /* Use preemption buffers allocated by the kernel */
90 job->primary_preempt_buf = cmdq->primary_preempt_buf;
91 job->secondary_preempt_buf = cmdq->secondary_preempt_buf;
92
93 return 0;
94 }
95
ivpu_cmdq_alloc(struct ivpu_file_priv * file_priv)96 static struct ivpu_cmdq *ivpu_cmdq_alloc(struct ivpu_file_priv *file_priv)
97 {
98 struct ivpu_device *vdev = file_priv->vdev;
99 struct ivpu_cmdq *cmdq;
100
101 cmdq = kzalloc_obj(*cmdq);
102 if (!cmdq)
103 return NULL;
104
105 cmdq->mem = ivpu_bo_create_global(vdev, SZ_4K, DRM_IVPU_BO_WC | DRM_IVPU_BO_MAPPABLE);
106 if (!cmdq->mem)
107 goto err_free_cmdq;
108
109 return cmdq;
110
111 err_free_cmdq:
112 kfree(cmdq);
113 return NULL;
114 }
115
116 /**
117 * ivpu_cmdq_get_entry_count - Calculate the number of entries in the command queue.
118 * @cmdq: Pointer to the command queue structure.
119 *
120 * Returns the number of entries that can fit in the command queue memory.
121 */
ivpu_cmdq_get_entry_count(struct ivpu_cmdq * cmdq)122 static inline u32 ivpu_cmdq_get_entry_count(struct ivpu_cmdq *cmdq)
123 {
124 size_t size = ivpu_bo_size(cmdq->mem) - sizeof(struct vpu_job_queue_header);
125
126 return size / sizeof(struct vpu_job_queue_entry);
127 }
128
129 /**
130 * ivpu_cmdq_get_flags - Get command queue flags based on input flags and test mode.
131 * @vdev: Pointer to the ivpu device structure.
132 * @flags: Input flags to determine the command queue flags.
133 *
134 * Returns the calculated command queue flags, considering both the input flags
135 * and the current test mode settings.
136 */
ivpu_cmdq_get_flags(struct ivpu_device * vdev,u32 flags)137 static u32 ivpu_cmdq_get_flags(struct ivpu_device *vdev, u32 flags)
138 {
139 u32 cmdq_flags = 0;
140
141 if ((flags & DRM_IVPU_CMDQ_FLAG_TURBO) && (ivpu_hw_ip_gen(vdev) >= IVPU_HW_IP_40XX))
142 cmdq_flags |= VPU_JOB_QUEUE_FLAGS_TURBO_MODE;
143
144 /* Test mode can override the TURBO flag coming from the application */
145 if (ivpu_test_mode & IVPU_TEST_MODE_TURBO_ENABLE)
146 cmdq_flags |= VPU_JOB_QUEUE_FLAGS_TURBO_MODE;
147 if (ivpu_test_mode & IVPU_TEST_MODE_TURBO_DISABLE)
148 cmdq_flags &= ~VPU_JOB_QUEUE_FLAGS_TURBO_MODE;
149
150 return cmdq_flags;
151 }
152
ivpu_cmdq_free(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)153 static void ivpu_cmdq_free(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
154 {
155 ivpu_preemption_buffers_free(file_priv->vdev, file_priv, cmdq);
156 ivpu_bo_free(cmdq->mem);
157 kfree(cmdq);
158 }
159
ivpu_cmdq_create(struct ivpu_file_priv * file_priv,u8 priority,u32 flags)160 static struct ivpu_cmdq *ivpu_cmdq_create(struct ivpu_file_priv *file_priv, u8 priority, u32 flags)
161 {
162 struct ivpu_device *vdev = file_priv->vdev;
163 struct ivpu_cmdq *cmdq = NULL;
164 int ret;
165
166 lockdep_assert_held(&file_priv->lock);
167
168 cmdq = ivpu_cmdq_alloc(file_priv);
169 if (!cmdq) {
170 ivpu_err(vdev, "Failed to allocate command queue\n");
171 return NULL;
172 }
173 ret = xa_alloc_cyclic(&file_priv->cmdq_xa, &cmdq->id, cmdq, file_priv->cmdq_limit,
174 &file_priv->cmdq_id_next, GFP_KERNEL);
175 if (ret < 0) {
176 ivpu_dbg(vdev, IOCTL, "Failed to allocate command queue ID: %d\n", ret);
177 goto err_free_cmdq;
178 }
179
180 cmdq->entry_count = ivpu_cmdq_get_entry_count(cmdq);
181 cmdq->priority = priority;
182
183 cmdq->jobq = (struct vpu_job_queue *)ivpu_bo_vaddr(cmdq->mem);
184 cmdq->jobq->header.engine_idx = VPU_ENGINE_COMPUTE;
185 cmdq->jobq->header.flags = ivpu_cmdq_get_flags(vdev, flags);
186
187 ivpu_dbg(vdev, JOB, "Command queue %d created, ctx %d, flags 0x%08x\n",
188 cmdq->id, file_priv->ctx.id, cmdq->jobq->header.flags);
189 return cmdq;
190
191 err_free_cmdq:
192 ivpu_cmdq_free(file_priv, cmdq);
193 return NULL;
194 }
195
ivpu_hws_cmdq_init(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq,u16 engine,u8 priority)196 static int ivpu_hws_cmdq_init(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq, u16 engine,
197 u8 priority)
198 {
199 struct ivpu_device *vdev = file_priv->vdev;
200 int ret;
201
202 ret = ivpu_jsm_hws_create_cmdq(vdev, file_priv->ctx.id, file_priv->ctx.id, cmdq->id,
203 task_pid_nr(current), engine,
204 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
205 if (ret)
206 return ret;
207
208 ret = ivpu_jsm_hws_set_context_sched_properties(vdev, file_priv->ctx.id, cmdq->id,
209 priority);
210 if (ret)
211 ivpu_jsm_hws_destroy_cmdq(vdev, file_priv->ctx.id, cmdq->id);
212
213 return ret;
214 }
215
ivpu_register_db(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)216 static int ivpu_register_db(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
217 {
218 struct ivpu_user_limits *limits = file_priv->user_limits;
219 struct ivpu_device *vdev = file_priv->vdev;
220 int ret;
221
222 if (atomic_inc_return(&limits->db_count) > limits->max_db_count) {
223 ivpu_dbg(vdev, IOCTL, "Maximum number of %u doorbells for uid %u reached\n",
224 limits->max_db_count, limits->uid);
225 ret = -EBUSY;
226 goto err_dec_db_count;
227 }
228
229 ret = xa_alloc_cyclic(&vdev->db_xa, &cmdq->db_id, NULL, vdev->db_limit, &vdev->db_next,
230 GFP_KERNEL);
231 if (ret < 0) {
232 ivpu_dbg(vdev, IOCTL, "Failed to allocate doorbell ID: %d\n", ret);
233 goto err_dec_db_count;
234 }
235
236 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
237 ret = ivpu_jsm_hws_register_db(vdev, file_priv->ctx.id, cmdq->id, cmdq->db_id,
238 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
239 else
240 ret = ivpu_jsm_register_db(vdev, file_priv->ctx.id, cmdq->db_id,
241 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
242 if (ret) {
243 xa_erase(&vdev->db_xa, cmdq->db_id);
244 cmdq->db_id = 0;
245 goto err_dec_db_count;
246 }
247
248 ivpu_dbg(vdev, JOB, "DB %d registered to cmdq %d ctx %d priority %d\n",
249 cmdq->db_id, cmdq->id, file_priv->ctx.id, cmdq->priority);
250 return 0;
251
252 err_dec_db_count:
253 atomic_dec(&limits->db_count);
254 return ret;
255 }
256
ivpu_cmdq_jobq_reset(struct ivpu_device * vdev,struct vpu_job_queue * jobq)257 static void ivpu_cmdq_jobq_reset(struct ivpu_device *vdev, struct vpu_job_queue *jobq)
258 {
259 jobq->header.head = 0;
260 jobq->header.tail = 0;
261
262 wmb(); /* Flush WC buffer for jobq->header */
263 }
264
ivpu_cmdq_register(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)265 static int ivpu_cmdq_register(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
266 {
267 struct ivpu_device *vdev = file_priv->vdev;
268 int ret;
269
270 lockdep_assert_held(&file_priv->lock);
271
272 if (cmdq->db_id)
273 return 0;
274
275 ivpu_cmdq_jobq_reset(vdev, cmdq->jobq);
276
277 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) {
278 ret = ivpu_hws_cmdq_init(file_priv, cmdq, VPU_ENGINE_COMPUTE, cmdq->priority);
279 if (ret)
280 return ret;
281 }
282
283 ret = ivpu_register_db(file_priv, cmdq);
284 if (ret && vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
285 ivpu_jsm_hws_destroy_cmdq(vdev, file_priv->ctx.id, cmdq->id);
286
287 return ret;
288 }
289
ivpu_cmdq_unregister(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)290 static int ivpu_cmdq_unregister(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
291 {
292 struct ivpu_device *vdev = file_priv->vdev;
293 int ret;
294
295 lockdep_assert_held(&file_priv->lock);
296
297 if (!cmdq->db_id)
298 return 0;
299
300 ret = ivpu_jsm_unregister_db(vdev, cmdq->db_id);
301 if (!ret)
302 ivpu_dbg(vdev, JOB, "DB %d unregistered\n", cmdq->db_id);
303
304 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) {
305 ret = ivpu_jsm_hws_destroy_cmdq(vdev, file_priv->ctx.id, cmdq->id);
306 if (!ret)
307 ivpu_dbg(vdev, JOB, "Command queue %d destroyed, ctx %d\n",
308 cmdq->id, file_priv->ctx.id);
309 }
310
311 xa_erase(&file_priv->vdev->db_xa, cmdq->db_id);
312 atomic_dec(&file_priv->user_limits->db_count);
313 cmdq->db_id = 0;
314
315 return 0;
316 }
317
ivpu_job_to_jsm_priority(u8 priority)318 static inline u8 ivpu_job_to_jsm_priority(u8 priority)
319 {
320 if (priority == DRM_IVPU_JOB_PRIORITY_DEFAULT)
321 return VPU_JOB_SCHEDULING_PRIORITY_BAND_NORMAL;
322
323 return priority - 1;
324 }
325
ivpu_cmdq_destroy(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)326 static void ivpu_cmdq_destroy(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
327 {
328 lockdep_assert_held(&file_priv->lock);
329 ivpu_cmdq_unregister(file_priv, cmdq);
330 xa_erase(&file_priv->cmdq_xa, cmdq->id);
331 ivpu_cmdq_free(file_priv, cmdq);
332 }
333
ivpu_cmdq_acquire_legacy(struct ivpu_file_priv * file_priv,u8 priority)334 static struct ivpu_cmdq *ivpu_cmdq_acquire_legacy(struct ivpu_file_priv *file_priv, u8 priority)
335 {
336 struct ivpu_cmdq *cmdq;
337 unsigned long id;
338
339 lockdep_assert_held(&file_priv->lock);
340
341 xa_for_each(&file_priv->cmdq_xa, id, cmdq)
342 if (cmdq->is_legacy && cmdq->priority == priority)
343 break;
344
345 if (!cmdq) {
346 cmdq = ivpu_cmdq_create(file_priv, priority, 0);
347 if (!cmdq)
348 return NULL;
349 cmdq->is_legacy = true;
350 }
351
352 return cmdq;
353 }
354
ivpu_cmdq_acquire(struct ivpu_file_priv * file_priv,u32 cmdq_id)355 static struct ivpu_cmdq *ivpu_cmdq_acquire(struct ivpu_file_priv *file_priv, u32 cmdq_id)
356 {
357 struct ivpu_device *vdev = file_priv->vdev;
358 struct ivpu_cmdq *cmdq;
359
360 lockdep_assert_held(&file_priv->lock);
361
362 cmdq = xa_load(&file_priv->cmdq_xa, cmdq_id);
363 if (!cmdq) {
364 ivpu_dbg(vdev, IOCTL, "Failed to find command queue with ID: %u\n", cmdq_id);
365 return NULL;
366 }
367
368 return cmdq;
369 }
370
ivpu_cmdq_release_all_locked(struct ivpu_file_priv * file_priv)371 void ivpu_cmdq_release_all_locked(struct ivpu_file_priv *file_priv)
372 {
373 struct ivpu_cmdq *cmdq;
374 unsigned long cmdq_id;
375
376 lockdep_assert_held(&file_priv->lock);
377
378 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq)
379 ivpu_cmdq_destroy(file_priv, cmdq);
380 }
381
382 /*
383 * Mark the doorbell as unregistered
384 * This function needs to be called when the VPU hardware is restarted
385 * and FW loses job queue state. The next time job queue is used it
386 * will be registered again.
387 */
ivpu_cmdq_reset(struct ivpu_file_priv * file_priv)388 static void ivpu_cmdq_reset(struct ivpu_file_priv *file_priv)
389 {
390 struct ivpu_cmdq *cmdq;
391 unsigned long cmdq_id;
392
393 mutex_lock(&file_priv->lock);
394
395 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq) {
396 if (cmdq->db_id) {
397 xa_erase(&file_priv->vdev->db_xa, cmdq->db_id);
398 atomic_dec(&file_priv->user_limits->db_count);
399 cmdq->db_id = 0;
400 }
401 }
402
403 mutex_unlock(&file_priv->lock);
404 }
405
ivpu_cmdq_reset_all_contexts(struct ivpu_device * vdev)406 void ivpu_cmdq_reset_all_contexts(struct ivpu_device *vdev)
407 {
408 struct ivpu_file_priv *file_priv;
409 unsigned long ctx_id;
410
411 mutex_lock(&vdev->context_list_lock);
412
413 xa_for_each(&vdev->context_xa, ctx_id, file_priv)
414 ivpu_cmdq_reset(file_priv);
415
416 mutex_unlock(&vdev->context_list_lock);
417 }
418
ivpu_context_abort_locked(struct ivpu_file_priv * file_priv)419 void ivpu_context_abort_locked(struct ivpu_file_priv *file_priv)
420 {
421 struct ivpu_device *vdev = file_priv->vdev;
422 struct ivpu_cmdq *cmdq;
423 unsigned long cmdq_id;
424
425 lockdep_assert_held(&file_priv->lock);
426 ivpu_dbg(vdev, JOB, "Context ID: %u abort\n", file_priv->ctx.id);
427
428 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq)
429 ivpu_cmdq_unregister(file_priv, cmdq);
430
431 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_OS)
432 ivpu_jsm_context_release(vdev, file_priv->ctx.id);
433
434 ivpu_mmu_disable_ssid_events(vdev, file_priv->ctx.id);
435
436 file_priv->aborted = true;
437 }
438
ivpu_cmdq_push_job(struct ivpu_cmdq * cmdq,struct ivpu_job * job)439 static int ivpu_cmdq_push_job(struct ivpu_cmdq *cmdq, struct ivpu_job *job)
440 {
441 struct ivpu_device *vdev = job->vdev;
442 struct vpu_job_queue_header *header = &cmdq->jobq->header;
443 struct vpu_job_queue_entry *entry;
444 u32 tail = READ_ONCE(header->tail);
445 u32 next_entry = (tail + 1) % cmdq->entry_count;
446
447 /* Check if there is space left in job queue */
448 if (next_entry == header->head) {
449 ivpu_dbg(vdev, JOB, "Job queue full: ctx %d cmdq %d db %d head %d tail %d\n",
450 job->file_priv->ctx.id, cmdq->id, cmdq->db_id, header->head, tail);
451 return -EBUSY;
452 }
453
454 entry = &cmdq->jobq->slot[tail].job;
455 entry->batch_buf_addr = job->cmd_buf_vpu_addr;
456 entry->job_id = job->job_id;
457 entry->flags = 0;
458 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_SUBMISSION))
459 entry->flags = VPU_JOB_FLAGS_NULL_SUBMISSION_MASK;
460
461 if (job->primary_preempt_buf) {
462 entry->primary_preempt_buf_addr = job->primary_preempt_buf->vpu_addr;
463 entry->primary_preempt_buf_size = ivpu_bo_size(job->primary_preempt_buf);
464 }
465
466 if (job->secondary_preempt_buf) {
467 entry->secondary_preempt_buf_addr = job->secondary_preempt_buf->vpu_addr;
468 entry->secondary_preempt_buf_size = ivpu_bo_size(job->secondary_preempt_buf);
469 }
470
471 wmb(); /* Ensure that tail is updated after filling entry */
472 header->tail = next_entry;
473 wmb(); /* Flush WC buffer for jobq header */
474
475 return 0;
476 }
477
478 struct ivpu_fence {
479 struct dma_fence base;
480 spinlock_t lock; /* protects base */
481 struct ivpu_device *vdev;
482 };
483
to_vpu_fence(struct dma_fence * fence)484 static inline struct ivpu_fence *to_vpu_fence(struct dma_fence *fence)
485 {
486 return container_of(fence, struct ivpu_fence, base);
487 }
488
ivpu_fence_get_driver_name(struct dma_fence * fence)489 static const char *ivpu_fence_get_driver_name(struct dma_fence *fence)
490 {
491 return DRIVER_NAME;
492 }
493
ivpu_fence_get_timeline_name(struct dma_fence * fence)494 static const char *ivpu_fence_get_timeline_name(struct dma_fence *fence)
495 {
496 struct ivpu_fence *ivpu_fence = to_vpu_fence(fence);
497
498 return dev_name(ivpu_fence->vdev->drm.dev);
499 }
500
501 static const struct dma_fence_ops ivpu_fence_ops = {
502 .get_driver_name = ivpu_fence_get_driver_name,
503 .get_timeline_name = ivpu_fence_get_timeline_name,
504 };
505
ivpu_fence_create(struct ivpu_device * vdev)506 static struct dma_fence *ivpu_fence_create(struct ivpu_device *vdev)
507 {
508 struct ivpu_fence *fence;
509
510 fence = kzalloc_obj(*fence);
511 if (!fence)
512 return NULL;
513
514 fence->vdev = vdev;
515 spin_lock_init(&fence->lock);
516 dma_fence_init(&fence->base, &ivpu_fence_ops, &fence->lock, dma_fence_context_alloc(1), 1);
517
518 return &fence->base;
519 }
520
ivpu_job_destroy(struct ivpu_job * job)521 static void ivpu_job_destroy(struct ivpu_job *job)
522 {
523 struct ivpu_device *vdev = job->vdev;
524 u32 i;
525
526 ivpu_dbg(vdev, JOB, "Job destroyed: id %3u ctx %2d cmdq_id %u engine %d",
527 job->job_id, job->file_priv->ctx.id, job->cmdq_id, job->engine_idx);
528
529 for (i = 0; i < job->bo_count; i++)
530 if (job->bos[i])
531 drm_gem_object_put(&job->bos[i]->base.base);
532
533 dma_fence_put(job->done_fence);
534 ivpu_file_priv_put(&job->file_priv);
535 kfree(job);
536 }
537
ivpu_job_destroy_work_fn(struct work_struct * work)538 void ivpu_job_destroy_work_fn(struct work_struct *work)
539 {
540 struct ivpu_device *vdev = container_of(work, struct ivpu_device, job_destroy_work);
541 struct ivpu_job *job, *tmp;
542 struct llist_node *list;
543
544 list = llist_del_all(&vdev->job_destroy_list);
545
546 llist_for_each_entry_safe(job, tmp, list, destroy_node) {
547 ivpu_job_destroy(job);
548 ivpu_rpm_put(vdev);
549 }
550 }
551
552 static struct ivpu_job *
ivpu_job_create(struct ivpu_file_priv * file_priv,u32 engine_idx,u32 bo_count)553 ivpu_job_create(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
554 {
555 struct ivpu_device *vdev = file_priv->vdev;
556 struct ivpu_job *job;
557
558 job = kzalloc_flex(*job, bos, bo_count);
559 if (!job)
560 return NULL;
561
562 job->vdev = vdev;
563 job->engine_idx = engine_idx;
564 job->bo_count = bo_count;
565 job->done_fence = ivpu_fence_create(vdev);
566 if (!job->done_fence) {
567 ivpu_err(vdev, "Failed to create a fence\n");
568 goto err_free_job;
569 }
570
571 job->file_priv = ivpu_file_priv_get(file_priv);
572
573 trace_job("create", job);
574 ivpu_dbg(vdev, JOB, "Job created: ctx %2d engine %d", file_priv->ctx.id, job->engine_idx);
575 return job;
576
577 err_free_job:
578 kfree(job);
579 return NULL;
580 }
581
ivpu_job_remove_from_submitted_jobs(struct ivpu_device * vdev,u32 job_id)582 static struct ivpu_job *ivpu_job_remove_from_submitted_jobs(struct ivpu_device *vdev, u32 job_id)
583 {
584 struct ivpu_job *job;
585
586 lockdep_assert_held(&vdev->submitted_jobs_lock);
587
588 job = xa_erase(&vdev->submitted_jobs_xa, job_id);
589 if (xa_empty(&vdev->submitted_jobs_xa) && job) {
590 vdev->busy_time = ktime_add(ktime_sub(ktime_get(), vdev->busy_start_ts),
591 vdev->busy_time);
592 }
593
594 return job;
595 }
596
ivpu_job_handle_engine_error(struct ivpu_device * vdev,u32 job_id,u32 job_status)597 bool ivpu_job_handle_engine_error(struct ivpu_device *vdev, u32 job_id, u32 job_status)
598 {
599 lockdep_assert_held(&vdev->submitted_jobs_lock);
600
601 switch (job_status) {
602 case VPU_JSM_STATUS_PROCESSING_ERR:
603 case VPU_JSM_STATUS_ENGINE_RESET_REQUIRED_MIN ... VPU_JSM_STATUS_ENGINE_RESET_REQUIRED_MAX:
604 {
605 struct ivpu_job *job = xa_load(&vdev->submitted_jobs_xa, job_id);
606
607 if (!job)
608 return false;
609
610 /* Trigger an engine reset */
611 guard(mutex)(&job->file_priv->lock);
612
613 job->job_status = job_status;
614
615 if (job->file_priv->has_mmu_faults)
616 return false;
617
618 /*
619 * Mark context as faulty and defer destruction of the job to jobs abort thread
620 * handler to synchronize between both faults and jobs returning context violation
621 * status and ensure both are handled in the same way
622 */
623 job->file_priv->has_mmu_faults = true;
624 queue_work(system_percpu_wq, &vdev->context_abort_work);
625 return true;
626 }
627 default:
628 /* Complete job with error status, engine reset not required */
629 break;
630 }
631
632 return false;
633 }
634
ivpu_job_signal(struct ivpu_device * vdev,u32 job_id,u32 job_status)635 static struct ivpu_job *ivpu_job_signal(struct ivpu_device *vdev, u32 job_id, u32 job_status)
636 {
637 struct ivpu_job *job;
638
639 lockdep_assert_held(&vdev->submitted_jobs_lock);
640
641 job = xa_load(&vdev->submitted_jobs_xa, job_id);
642 if (!job)
643 return NULL;
644
645 ivpu_job_remove_from_submitted_jobs(vdev, job_id);
646
647 if (job->job_status == VPU_JSM_STATUS_SUCCESS) {
648 if (job->file_priv->has_mmu_faults)
649 job->job_status = DRM_IVPU_JOB_STATUS_ABORTED;
650 else
651 job->job_status = job_status;
652 }
653
654 job->bos[CMD_BUF_IDX]->job_status = job->job_status;
655 dma_fence_signal(job->done_fence);
656
657 trace_job("done", job);
658 ivpu_dbg(vdev, JOB, "Job complete: id %3u ctx %2d cmdq_id %u engine %d status 0x%x\n",
659 job->job_id, job->file_priv->ctx.id, job->cmdq_id, job->engine_idx,
660 job->job_status);
661
662 ivpu_stop_job_timeout_detection(vdev);
663
664 if (!xa_empty(&vdev->submitted_jobs_xa))
665 ivpu_start_job_timeout_detection(vdev);
666
667 return job;
668 }
669
ivpu_job_signal_and_destroy(struct ivpu_device * vdev,u32 job_id,u32 job_status)670 static int ivpu_job_signal_and_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
671 {
672 struct ivpu_job *job = ivpu_job_signal(vdev, job_id, job_status);
673
674 if (!job)
675 return -ENOENT;
676
677 ivpu_job_destroy(job);
678 ivpu_rpm_put(vdev);
679
680 return 0;
681 }
682
ivpu_job_signal_and_defer_destroy(struct ivpu_device * vdev,u32 job_id,u32 job_status)683 static int ivpu_job_signal_and_defer_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
684 {
685 struct ivpu_job *job = ivpu_job_signal(vdev, job_id, job_status);
686
687 if (!job)
688 return -ENOENT;
689
690 llist_add(&job->destroy_node, &vdev->job_destroy_list);
691 queue_work(vdev->job_destroy_wq, &vdev->job_destroy_work);
692
693 return 0;
694 }
695
ivpu_jobs_abort_all(struct ivpu_device * vdev)696 void ivpu_jobs_abort_all(struct ivpu_device *vdev)
697 {
698 struct ivpu_job *job;
699 unsigned long id;
700
701 mutex_lock(&vdev->submitted_jobs_lock);
702
703 xa_for_each(&vdev->submitted_jobs_xa, id, job)
704 ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
705
706 mutex_unlock(&vdev->submitted_jobs_lock);
707 }
708
ivpu_cmdq_abort_all_jobs(struct ivpu_device * vdev,u32 ctx_id,u32 cmdq_id)709 void ivpu_cmdq_abort_all_jobs(struct ivpu_device *vdev, u32 ctx_id, u32 cmdq_id)
710 {
711 struct ivpu_job *job;
712 unsigned long id;
713
714 mutex_lock(&vdev->submitted_jobs_lock);
715
716 xa_for_each(&vdev->submitted_jobs_xa, id, job)
717 if (job->file_priv->ctx.id == ctx_id && job->cmdq_id == cmdq_id)
718 ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
719
720 mutex_unlock(&vdev->submitted_jobs_lock);
721 }
722
ivpu_job_submit(struct ivpu_job * job,u8 priority,u32 cmdq_id)723 static int ivpu_job_submit(struct ivpu_job *job, u8 priority, u32 cmdq_id)
724 {
725 struct ivpu_file_priv *file_priv = job->file_priv;
726 struct ivpu_device *vdev = job->vdev;
727 struct ivpu_cmdq *cmdq;
728 bool flushed = false;
729 bool is_first_job;
730 int ret;
731
732 ret = ivpu_rpm_get(vdev);
733 if (ret < 0)
734 return ret;
735
736 retry:
737 mutex_lock(&vdev->submitted_jobs_lock);
738 mutex_lock(&file_priv->lock);
739
740 if (cmdq_id == 0)
741 cmdq = ivpu_cmdq_acquire_legacy(file_priv, priority);
742 else
743 cmdq = ivpu_cmdq_acquire(file_priv, cmdq_id);
744 if (!cmdq) {
745 ret = -EINVAL;
746 goto err_unlock;
747 }
748
749 ret = ivpu_cmdq_register(file_priv, cmdq);
750 if (ret == -EBUSY && !flushed) {
751 /* Doorbell may be held by jobs pending deferred cleanup */
752 mutex_unlock(&file_priv->lock);
753 mutex_unlock(&vdev->submitted_jobs_lock);
754 flush_work(&vdev->job_destroy_work);
755 flushed = true;
756 goto retry;
757 }
758 if (ret) {
759 ivpu_err(vdev, "Failed to register command queue: %d\n", ret);
760 goto err_unlock;
761 }
762
763 ret = ivpu_preemption_job_init(vdev, file_priv, cmdq, job);
764 if (ret) {
765 ivpu_err(vdev, "Failed to initialize preemption buffers for job %d: %d\n",
766 job->job_id, ret);
767 goto err_unlock;
768 }
769
770 job->cmdq_id = cmdq->id;
771
772 is_first_job = xa_empty(&vdev->submitted_jobs_xa);
773 ret = xa_alloc_cyclic(&vdev->submitted_jobs_xa, &job->job_id, job, file_priv->job_limit,
774 &file_priv->job_id_next, GFP_KERNEL);
775 if (ret < 0) {
776 ivpu_dbg(vdev, JOB, "Too many active jobs in ctx %d\n",
777 file_priv->ctx.id);
778 ret = -EBUSY;
779 goto err_unlock;
780 }
781
782 ret = ivpu_cmdq_push_job(cmdq, job);
783 if (ret)
784 goto err_erase_xa;
785
786 ivpu_start_job_timeout_detection(vdev);
787
788 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
789 cmdq->jobq->header.head = cmdq->jobq->header.tail;
790 wmb(); /* Flush WC buffer for jobq header */
791 } else {
792 ivpu_cmdq_ring_db(vdev, cmdq);
793 if (is_first_job)
794 vdev->busy_start_ts = ktime_get();
795 }
796
797 trace_job("submit", job);
798 ivpu_dbg(vdev, JOB, "Job submitted: id %3u ctx %2d cmdq_id %u engine %d prio %d addr 0x%llx next %d\n",
799 job->job_id, file_priv->ctx.id, cmdq->id, job->engine_idx, cmdq->priority,
800 job->cmd_buf_vpu_addr, cmdq->jobq->header.tail);
801
802 mutex_unlock(&file_priv->lock);
803
804 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
805 ivpu_job_signal_and_destroy(vdev, job->job_id, VPU_JSM_STATUS_SUCCESS);
806 }
807
808 mutex_unlock(&vdev->submitted_jobs_lock);
809
810 return 0;
811
812 err_erase_xa:
813 xa_erase(&vdev->submitted_jobs_xa, job->job_id);
814 err_unlock:
815 mutex_unlock(&file_priv->lock);
816 mutex_unlock(&vdev->submitted_jobs_lock);
817 ivpu_rpm_put(vdev);
818 return ret;
819 }
820
821 static int
ivpu_job_prepare_bos_for_submit(struct drm_file * file,struct ivpu_job * job,u32 * buf_handles,u32 buf_count,u32 commands_offset,u32 preempt_buffer_index)822 ivpu_job_prepare_bos_for_submit(struct drm_file *file, struct ivpu_job *job, u32 *buf_handles,
823 u32 buf_count, u32 commands_offset, u32 preempt_buffer_index)
824 {
825 struct ivpu_file_priv *file_priv = job->file_priv;
826 struct ivpu_device *vdev = file_priv->vdev;
827 struct ww_acquire_ctx acquire_ctx;
828 enum dma_resv_usage usage;
829 struct ivpu_bo *bo;
830 int ret;
831 u32 i;
832
833 for (i = 0; i < buf_count; i++) {
834 struct drm_gem_object *obj = drm_gem_object_lookup(file, buf_handles[i]);
835
836 if (!obj) {
837 ivpu_dbg(vdev, IOCTL, "Failed to lookup GEM object with handle %u\n",
838 buf_handles[i]);
839 return -ENOENT;
840 }
841
842 job->bos[i] = to_ivpu_bo(obj);
843
844 ret = ivpu_bo_bind(job->bos[i]);
845 if (ret)
846 return ret;
847 }
848
849 bo = job->bos[CMD_BUF_IDX];
850 if (!dma_resv_test_signaled(bo->base.base.resv, DMA_RESV_USAGE_READ)) {
851 ivpu_dbg(vdev, IOCTL, "Buffer is already in use by another job\n");
852 return -EBUSY;
853 }
854
855 if (commands_offset >= ivpu_bo_size(bo)) {
856 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u for buffer size %zu\n",
857 commands_offset, ivpu_bo_size(bo));
858 return -EINVAL;
859 }
860
861 job->cmd_buf_vpu_addr = bo->vpu_addr + commands_offset;
862
863 if (preempt_buffer_index) {
864 struct ivpu_bo *preempt_bo = job->bos[preempt_buffer_index];
865
866 if (ivpu_bo_size(preempt_bo) < ivpu_fw_preempt_buf_size(vdev)) {
867 ivpu_dbg(vdev, IOCTL, "Preemption buffer is too small\n");
868 return -EINVAL;
869 }
870 if (ivpu_bo_is_mappable(preempt_bo)) {
871 ivpu_dbg(vdev, IOCTL, "Preemption buffer cannot be mappable\n");
872 return -EINVAL;
873 }
874 job->primary_preempt_buf = preempt_bo;
875 }
876
877 ret = drm_gem_lock_reservations((struct drm_gem_object **)job->bos, buf_count,
878 &acquire_ctx);
879 if (ret) {
880 ivpu_warn_ratelimited(vdev, "Failed to lock reservations: %d\n", ret);
881 return ret;
882 }
883
884 for (i = 0; i < buf_count; i++) {
885 ret = dma_resv_reserve_fences(job->bos[i]->base.base.resv, 1);
886 if (ret) {
887 ivpu_warn_ratelimited(vdev, "Failed to reserve fences: %d\n", ret);
888 goto unlock_reservations;
889 }
890 }
891
892 for (i = 0; i < buf_count; i++) {
893 usage = (i == CMD_BUF_IDX) ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_BOOKKEEP;
894 dma_resv_add_fence(job->bos[i]->base.base.resv, job->done_fence, usage);
895 }
896
897 unlock_reservations:
898 drm_gem_unlock_reservations((struct drm_gem_object **)job->bos, buf_count, &acquire_ctx);
899
900 wmb(); /* Flush write combining buffers */
901
902 return ret;
903 }
904
ivpu_submit(struct drm_file * file,struct ivpu_file_priv * file_priv,u32 cmdq_id,u32 buffer_count,u32 engine,void __user * buffers_ptr,u32 cmds_offset,u32 preempt_buffer_index,u8 priority)905 static int ivpu_submit(struct drm_file *file, struct ivpu_file_priv *file_priv, u32 cmdq_id,
906 u32 buffer_count, u32 engine, void __user *buffers_ptr, u32 cmds_offset,
907 u32 preempt_buffer_index, u8 priority)
908 {
909 struct ivpu_device *vdev = file_priv->vdev;
910 struct ivpu_job *job;
911 u32 *buf_handles;
912 int idx, ret;
913
914 buf_handles = kcalloc(buffer_count, sizeof(u32), GFP_KERNEL);
915 if (!buf_handles)
916 return -ENOMEM;
917
918 ret = copy_from_user(buf_handles, buffers_ptr, buffer_count * sizeof(u32));
919 if (ret) {
920 ret = -EFAULT;
921 goto err_free_handles;
922 }
923
924 if (!drm_dev_enter(&vdev->drm, &idx)) {
925 ret = -ENODEV;
926 goto err_free_handles;
927 }
928
929 ivpu_dbg(vdev, JOB, "Submit ioctl: ctx %u cmdq_id %u buf_count %u\n",
930 file_priv->ctx.id, cmdq_id, buffer_count);
931
932 job = ivpu_job_create(file_priv, engine, buffer_count);
933 if (!job) {
934 ret = -ENOMEM;
935 goto err_exit_dev;
936 }
937
938 ret = ivpu_job_prepare_bos_for_submit(file, job, buf_handles, buffer_count, cmds_offset,
939 preempt_buffer_index);
940 if (ret)
941 goto err_destroy_job;
942
943 down_read(&vdev->pm->reset_lock);
944 ret = ivpu_job_submit(job, priority, cmdq_id);
945 up_read(&vdev->pm->reset_lock);
946 if (ret)
947 goto err_signal_fence;
948
949 drm_dev_exit(idx);
950 kfree(buf_handles);
951 return ret;
952
953 err_signal_fence:
954 dma_fence_signal(job->done_fence);
955 err_destroy_job:
956 ivpu_job_destroy(job);
957 err_exit_dev:
958 drm_dev_exit(idx);
959 err_free_handles:
960 kfree(buf_handles);
961 return ret;
962 }
963
ivpu_submit_ioctl(struct drm_device * dev,void * data,struct drm_file * file)964 int ivpu_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
965 {
966 struct ivpu_file_priv *file_priv = file->driver_priv;
967 struct ivpu_device *vdev = file_priv->vdev;
968 struct drm_ivpu_submit *args = data;
969 u8 priority;
970
971 if (args->engine != DRM_IVPU_ENGINE_COMPUTE) {
972 ivpu_dbg(vdev, IOCTL, "Invalid engine %d\n", args->engine);
973 return -EINVAL;
974 }
975
976 if (args->priority > DRM_IVPU_JOB_PRIORITY_REALTIME) {
977 ivpu_dbg(vdev, IOCTL, "Invalid priority %d\n", args->priority);
978 return -EINVAL;
979 }
980
981 if (args->buffer_count == 0 || args->buffer_count > JOB_MAX_BUFFER_COUNT) {
982 ivpu_dbg(vdev, IOCTL, "Invalid buffer count %u\n", args->buffer_count);
983 return -EINVAL;
984 }
985
986 if (!IS_ALIGNED(args->commands_offset, 8)) {
987 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u\n", args->commands_offset);
988 return -EINVAL;
989 }
990
991 if (!file_priv->ctx.id) {
992 ivpu_dbg(vdev, IOCTL, "Context not initialized\n");
993 return -EINVAL;
994 }
995
996 if (file_priv->has_mmu_faults) {
997 ivpu_dbg(vdev, IOCTL, "Context %u has MMU faults\n", file_priv->ctx.id);
998 return -EBADFD;
999 }
1000
1001 priority = ivpu_job_to_jsm_priority(args->priority);
1002
1003 return ivpu_submit(file, file_priv, 0, args->buffer_count, args->engine,
1004 (void __user *)args->buffers_ptr, args->commands_offset, 0, priority);
1005 }
1006
ivpu_cmdq_submit_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1007 int ivpu_cmdq_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1008 {
1009 struct ivpu_file_priv *file_priv = file->driver_priv;
1010 struct ivpu_device *vdev = file_priv->vdev;
1011 struct drm_ivpu_cmdq_submit *args = data;
1012
1013 if (!ivpu_is_capable(file_priv->vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1014 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1015 return -ENODEV;
1016 }
1017
1018 if (args->cmdq_id < IVPU_CMDQ_MIN_ID || args->cmdq_id > IVPU_CMDQ_MAX_ID) {
1019 ivpu_dbg(vdev, IOCTL, "Invalid command queue ID %u\n", args->cmdq_id);
1020 return -EINVAL;
1021 }
1022
1023 if (args->buffer_count == 0 || args->buffer_count > JOB_MAX_BUFFER_COUNT) {
1024 ivpu_dbg(vdev, IOCTL, "Invalid buffer count %u\n", args->buffer_count);
1025 return -EINVAL;
1026 }
1027
1028 if (args->preempt_buffer_index >= args->buffer_count) {
1029 ivpu_dbg(vdev, IOCTL, "Invalid preemption buffer index %u\n",
1030 args->preempt_buffer_index);
1031 return -EINVAL;
1032 }
1033
1034 if (!IS_ALIGNED(args->commands_offset, 8)) {
1035 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u\n", args->commands_offset);
1036 return -EINVAL;
1037 }
1038
1039 if (!file_priv->ctx.id) {
1040 ivpu_dbg(vdev, IOCTL, "Context not initialized\n");
1041 return -EINVAL;
1042 }
1043
1044 if (file_priv->has_mmu_faults) {
1045 ivpu_dbg(vdev, IOCTL, "Context %u has MMU faults\n", file_priv->ctx.id);
1046 return -EBADFD;
1047 }
1048
1049 return ivpu_submit(file, file_priv, args->cmdq_id, args->buffer_count, VPU_ENGINE_COMPUTE,
1050 (void __user *)args->buffers_ptr, args->commands_offset,
1051 args->preempt_buffer_index, 0);
1052 }
1053
ivpu_cmdq_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1054 int ivpu_cmdq_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1055 {
1056 struct ivpu_file_priv *file_priv = file->driver_priv;
1057 struct ivpu_device *vdev = file_priv->vdev;
1058 struct drm_ivpu_cmdq_create *args = data;
1059 struct ivpu_cmdq *cmdq;
1060 int ret;
1061
1062 if (!ivpu_is_capable(vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1063 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1064 return -ENODEV;
1065 }
1066
1067 if (args->priority > DRM_IVPU_JOB_PRIORITY_REALTIME) {
1068 ivpu_dbg(vdev, IOCTL, "Invalid priority %d\n", args->priority);
1069 return -EINVAL;
1070 }
1071
1072 ret = ivpu_rpm_get(vdev);
1073 if (ret < 0)
1074 return ret;
1075
1076 mutex_lock(&file_priv->lock);
1077
1078 cmdq = ivpu_cmdq_create(file_priv, ivpu_job_to_jsm_priority(args->priority), args->flags);
1079 if (cmdq)
1080 args->cmdq_id = cmdq->id;
1081
1082 mutex_unlock(&file_priv->lock);
1083
1084 ivpu_rpm_put(vdev);
1085
1086 return cmdq ? 0 : -ENOMEM;
1087 }
1088
ivpu_cmdq_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1089 int ivpu_cmdq_destroy_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1090 {
1091 struct ivpu_file_priv *file_priv = file->driver_priv;
1092 struct ivpu_device *vdev = file_priv->vdev;
1093 struct drm_ivpu_cmdq_destroy *args = data;
1094 struct ivpu_cmdq *cmdq;
1095 u32 cmdq_id = 0;
1096 int ret;
1097
1098 if (!ivpu_is_capable(vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1099 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1100 return -ENODEV;
1101 }
1102
1103 ret = ivpu_rpm_get(vdev);
1104 if (ret < 0)
1105 return ret;
1106
1107 mutex_lock(&file_priv->lock);
1108
1109 cmdq = xa_load(&file_priv->cmdq_xa, args->cmdq_id);
1110 if (!cmdq || cmdq->is_legacy) {
1111 ret = -ENOENT;
1112 } else {
1113 cmdq_id = cmdq->id;
1114 ivpu_cmdq_destroy(file_priv, cmdq);
1115 ret = 0;
1116 }
1117
1118 mutex_unlock(&file_priv->lock);
1119
1120 /* Abort any pending jobs only if cmdq was destroyed */
1121 if (!ret)
1122 ivpu_cmdq_abort_all_jobs(vdev, file_priv->ctx.id, cmdq_id);
1123
1124 ivpu_rpm_put(vdev);
1125
1126 return ret;
1127 }
1128
1129 static void
ivpu_job_done_callback(struct ivpu_device * vdev,struct ivpu_ipc_hdr * ipc_hdr,struct vpu_jsm_msg * jsm_msg)1130 ivpu_job_done_callback(struct ivpu_device *vdev, struct ivpu_ipc_hdr *ipc_hdr,
1131 struct vpu_jsm_msg *jsm_msg)
1132 {
1133 struct vpu_ipc_msg_payload_job_done *payload;
1134
1135 if (!jsm_msg) {
1136 ivpu_err(vdev, "IPC message has no JSM payload\n");
1137 return;
1138 }
1139
1140 if (jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
1141 ivpu_err(vdev, "Invalid JSM message result: %d\n", jsm_msg->result);
1142 return;
1143 }
1144
1145 payload = (struct vpu_ipc_msg_payload_job_done *)&jsm_msg->payload;
1146
1147 mutex_lock(&vdev->submitted_jobs_lock);
1148 if (!ivpu_job_handle_engine_error(vdev, payload->job_id, payload->job_status))
1149 /* No engine error, complete the job normally */
1150 ivpu_job_signal_and_defer_destroy(vdev, payload->job_id, payload->job_status);
1151 mutex_unlock(&vdev->submitted_jobs_lock);
1152 }
1153
ivpu_job_done_consumer_init(struct ivpu_device * vdev)1154 void ivpu_job_done_consumer_init(struct ivpu_device *vdev)
1155 {
1156 ivpu_ipc_consumer_add(vdev, &vdev->job_done_consumer,
1157 VPU_IPC_CHAN_JOB_RET, ivpu_job_done_callback);
1158 }
1159
ivpu_job_done_consumer_fini(struct ivpu_device * vdev)1160 void ivpu_job_done_consumer_fini(struct ivpu_device *vdev)
1161 {
1162 ivpu_ipc_consumer_del(vdev, &vdev->job_done_consumer);
1163 }
1164
reset_engine_and_mark_faulty_contexts(struct ivpu_device * vdev)1165 static int reset_engine_and_mark_faulty_contexts(struct ivpu_device *vdev)
1166 {
1167 u32 num_impacted_contexts;
1168 struct vpu_jsm_msg resp;
1169 int ret;
1170 u32 i;
1171
1172 ret = ivpu_jsm_reset_engine(vdev, 0, &resp);
1173 if (ret)
1174 return ret;
1175
1176 /*
1177 * If job timeout is detected, read guilty context from engine reset, for other reasons
1178 * faulty context is already known
1179 */
1180 if (atomic_cmpxchg(&vdev->job_timeout_detected, 1, 0) == 0)
1181 return 0;
1182
1183 num_impacted_contexts = resp.payload.engine_reset_done.num_impacted_contexts;
1184
1185 ivpu_warn_ratelimited(vdev, "Engine reset performed, impacted contexts: %u\n",
1186 num_impacted_contexts);
1187
1188 if (!in_range(num_impacted_contexts, 1, VPU_MAX_ENGINE_RESET_IMPACTED_CONTEXTS - 1)) {
1189 ivpu_pm_trigger_recovery(vdev, "Cannot determine guilty contexts");
1190 return -EIO;
1191 }
1192
1193 /* No faults detected, NPU likely got stuck. Mark returned contexts as guilty */
1194 guard(mutex)(&vdev->context_list_lock);
1195
1196 for (i = 0; i < num_impacted_contexts; i++) {
1197 u32 ssid = resp.payload.engine_reset_done.impacted_contexts[i].host_ssid;
1198 struct ivpu_file_priv *file_priv = xa_load(&vdev->context_xa, ssid);
1199
1200 if (file_priv) {
1201 mutex_lock(&file_priv->lock);
1202 file_priv->has_mmu_faults = true;
1203 mutex_unlock(&file_priv->lock);
1204 }
1205 }
1206
1207 return 0;
1208 }
1209
ivpu_context_abort_work_fn(struct work_struct * work)1210 void ivpu_context_abort_work_fn(struct work_struct *work)
1211 {
1212 struct ivpu_device *vdev = container_of(work, struct ivpu_device, context_abort_work);
1213 struct ivpu_file_priv *file_priv;
1214 struct ivpu_job *job;
1215 unsigned long ctx_id;
1216 unsigned long id;
1217
1218 if (drm_WARN_ON(&vdev->drm, pm_runtime_get_if_active(vdev->drm.dev) <= 0))
1219 return;
1220
1221 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
1222 if (reset_engine_and_mark_faulty_contexts(vdev))
1223 goto runtime_put;
1224
1225 mutex_lock(&vdev->context_list_lock);
1226 xa_for_each(&vdev->context_xa, ctx_id, file_priv) {
1227 if (!file_priv->has_mmu_faults || file_priv->aborted)
1228 continue;
1229
1230 mutex_lock(&file_priv->lock);
1231 ivpu_context_abort_locked(file_priv);
1232 mutex_unlock(&file_priv->lock);
1233 }
1234 mutex_unlock(&vdev->context_list_lock);
1235
1236 /*
1237 * We will not receive new MMU event interrupts until existing events are discarded
1238 * however, we want to discard these events only after aborting the faulty context
1239 * to avoid generating new faults from that context
1240 */
1241 ivpu_mmu_discard_events(vdev);
1242
1243 if (vdev->fw->sched_mode != VPU_SCHEDULING_MODE_HW)
1244 goto runtime_put;
1245
1246 if (ivpu_jsm_hws_resume_engine(vdev, 0))
1247 goto runtime_put;
1248 /*
1249 * In hardware scheduling mode NPU already has stopped processing jobs
1250 * and won't send us any further notifications, thus we have to free job related resources
1251 * and notify userspace
1252 */
1253 mutex_lock(&vdev->submitted_jobs_lock);
1254 xa_for_each(&vdev->submitted_jobs_xa, id, job)
1255 if (job->file_priv->aborted)
1256 ivpu_job_signal_and_destroy(vdev, job->job_id, DRM_IVPU_JOB_STATUS_ABORTED);
1257 mutex_unlock(&vdev->submitted_jobs_lock);
1258
1259 runtime_put:
1260 pm_runtime_put_autosuspend(vdev->drm.dev);
1261 }
1262