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 atomic_set(&vdev->faults_detected, 1);
625 queue_work(system_percpu_wq, &vdev->context_abort_work);
626 return true;
627 }
628 default:
629 /* Complete job with error status, engine reset not required */
630 break;
631 }
632
633 return false;
634 }
635
ivpu_job_signal(struct ivpu_device * vdev,u32 job_id,u32 job_status)636 static struct ivpu_job *ivpu_job_signal(struct ivpu_device *vdev, u32 job_id, u32 job_status)
637 {
638 struct ivpu_job *job;
639
640 lockdep_assert_held(&vdev->submitted_jobs_lock);
641
642 job = xa_load(&vdev->submitted_jobs_xa, job_id);
643 if (!job)
644 return NULL;
645
646 ivpu_job_remove_from_submitted_jobs(vdev, job_id);
647
648 if (job->job_status == VPU_JSM_STATUS_SUCCESS) {
649 if (job->file_priv->has_mmu_faults)
650 job->job_status = DRM_IVPU_JOB_STATUS_ABORTED;
651 else
652 job->job_status = job_status;
653 }
654
655 job->bos[CMD_BUF_IDX]->job_status = job->job_status;
656 dma_fence_signal(job->done_fence);
657
658 trace_job("done", job);
659 ivpu_dbg(vdev, JOB, "Job complete: id %3u ctx %2d cmdq_id %u engine %d status 0x%x\n",
660 job->job_id, job->file_priv->ctx.id, job->cmdq_id, job->engine_idx,
661 job->job_status);
662
663 ivpu_stop_job_timeout_detection(vdev);
664
665 if (!xa_empty(&vdev->submitted_jobs_xa))
666 ivpu_start_job_timeout_detection(vdev);
667
668 return job;
669 }
670
ivpu_job_signal_and_destroy(struct ivpu_device * vdev,u32 job_id,u32 job_status)671 static int ivpu_job_signal_and_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
672 {
673 struct ivpu_job *job = ivpu_job_signal(vdev, job_id, job_status);
674
675 if (!job)
676 return -ENOENT;
677
678 ivpu_job_destroy(job);
679 ivpu_rpm_put(vdev);
680
681 return 0;
682 }
683
ivpu_job_signal_and_defer_destroy(struct ivpu_device * vdev,u32 job_id,u32 job_status)684 static int ivpu_job_signal_and_defer_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
685 {
686 struct ivpu_job *job = ivpu_job_signal(vdev, job_id, job_status);
687
688 if (!job)
689 return -ENOENT;
690
691 llist_add(&job->destroy_node, &vdev->job_destroy_list);
692 queue_work(vdev->job_destroy_wq, &vdev->job_destroy_work);
693
694 return 0;
695 }
696
ivpu_jobs_abort_all(struct ivpu_device * vdev)697 void ivpu_jobs_abort_all(struct ivpu_device *vdev)
698 {
699 struct ivpu_job *job;
700 unsigned long id;
701
702 mutex_lock(&vdev->submitted_jobs_lock);
703
704 xa_for_each(&vdev->submitted_jobs_xa, id, job)
705 ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
706
707 mutex_unlock(&vdev->submitted_jobs_lock);
708 }
709
ivpu_cmdq_abort_all_jobs(struct ivpu_device * vdev,u32 ctx_id,u32 cmdq_id)710 void ivpu_cmdq_abort_all_jobs(struct ivpu_device *vdev, u32 ctx_id, u32 cmdq_id)
711 {
712 struct ivpu_job *job;
713 unsigned long id;
714
715 mutex_lock(&vdev->submitted_jobs_lock);
716
717 xa_for_each(&vdev->submitted_jobs_xa, id, job)
718 if (job->file_priv->ctx.id == ctx_id && job->cmdq_id == cmdq_id)
719 ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
720
721 mutex_unlock(&vdev->submitted_jobs_lock);
722 }
723
ivpu_job_submit(struct ivpu_job * job,u8 priority,u32 cmdq_id)724 static int ivpu_job_submit(struct ivpu_job *job, u8 priority, u32 cmdq_id)
725 {
726 struct ivpu_file_priv *file_priv = job->file_priv;
727 struct ivpu_device *vdev = job->vdev;
728 struct ivpu_cmdq *cmdq;
729 bool flushed = false;
730 bool is_first_job;
731 int ret;
732
733 ret = ivpu_rpm_get(vdev);
734 if (ret < 0)
735 return ret;
736
737 retry:
738 mutex_lock(&vdev->submitted_jobs_lock);
739 mutex_lock(&file_priv->lock);
740
741 if (cmdq_id == 0)
742 cmdq = ivpu_cmdq_acquire_legacy(file_priv, priority);
743 else
744 cmdq = ivpu_cmdq_acquire(file_priv, cmdq_id);
745 if (!cmdq) {
746 ret = -EINVAL;
747 goto err_unlock;
748 }
749
750 ret = ivpu_cmdq_register(file_priv, cmdq);
751 if (ret == -EBUSY && !flushed) {
752 /* Doorbell may be held by jobs pending deferred cleanup */
753 mutex_unlock(&file_priv->lock);
754 mutex_unlock(&vdev->submitted_jobs_lock);
755 flush_work(&vdev->job_destroy_work);
756 flushed = true;
757 goto retry;
758 }
759 if (ret) {
760 ivpu_err(vdev, "Failed to register command queue: %d\n", ret);
761 goto err_unlock;
762 }
763
764 ret = ivpu_preemption_job_init(vdev, file_priv, cmdq, job);
765 if (ret) {
766 ivpu_err(vdev, "Failed to initialize preemption buffers for job %d: %d\n",
767 job->job_id, ret);
768 goto err_unlock;
769 }
770
771 job->cmdq_id = cmdq->id;
772
773 is_first_job = xa_empty(&vdev->submitted_jobs_xa);
774 ret = xa_alloc_cyclic(&vdev->submitted_jobs_xa, &job->job_id, job, file_priv->job_limit,
775 &file_priv->job_id_next, GFP_KERNEL);
776 if (ret < 0) {
777 ivpu_dbg(vdev, JOB, "Too many active jobs in ctx %d\n",
778 file_priv->ctx.id);
779 ret = -EBUSY;
780 goto err_unlock;
781 }
782
783 ret = ivpu_cmdq_push_job(cmdq, job);
784 if (ret)
785 goto err_erase_xa;
786
787 ivpu_start_job_timeout_detection(vdev);
788
789 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
790 cmdq->jobq->header.head = cmdq->jobq->header.tail;
791 wmb(); /* Flush WC buffer for jobq header */
792 } else {
793 ivpu_cmdq_ring_db(vdev, cmdq);
794 if (is_first_job)
795 vdev->busy_start_ts = ktime_get();
796 }
797
798 trace_job("submit", job);
799 ivpu_dbg(vdev, JOB, "Job submitted: id %3u ctx %2d cmdq_id %u engine %d prio %d addr 0x%llx next %d\n",
800 job->job_id, file_priv->ctx.id, cmdq->id, job->engine_idx, cmdq->priority,
801 job->cmd_buf_vpu_addr, cmdq->jobq->header.tail);
802
803 mutex_unlock(&file_priv->lock);
804
805 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
806 ivpu_job_signal_and_destroy(vdev, job->job_id, VPU_JSM_STATUS_SUCCESS);
807 }
808
809 mutex_unlock(&vdev->submitted_jobs_lock);
810
811 return 0;
812
813 err_erase_xa:
814 xa_erase(&vdev->submitted_jobs_xa, job->job_id);
815 err_unlock:
816 mutex_unlock(&file_priv->lock);
817 mutex_unlock(&vdev->submitted_jobs_lock);
818 ivpu_rpm_put(vdev);
819 return ret;
820 }
821
822 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)823 ivpu_job_prepare_bos_for_submit(struct drm_file *file, struct ivpu_job *job, u32 *buf_handles,
824 u32 buf_count, u32 commands_offset, u32 preempt_buffer_index)
825 {
826 struct ivpu_file_priv *file_priv = job->file_priv;
827 struct ivpu_device *vdev = file_priv->vdev;
828 struct ww_acquire_ctx acquire_ctx;
829 enum dma_resv_usage usage;
830 struct ivpu_bo *bo;
831 int ret;
832 u32 i;
833
834 for (i = 0; i < buf_count; i++) {
835 struct drm_gem_object *obj = drm_gem_object_lookup(file, buf_handles[i]);
836
837 if (!obj) {
838 ivpu_dbg(vdev, IOCTL, "Failed to lookup GEM object with handle %u\n",
839 buf_handles[i]);
840 return -ENOENT;
841 }
842
843 job->bos[i] = to_ivpu_bo(obj);
844
845 ret = ivpu_bo_bind(job->bos[i]);
846 if (ret)
847 return ret;
848 }
849
850 bo = job->bos[CMD_BUF_IDX];
851 if (!dma_resv_test_signaled(bo->base.base.resv, DMA_RESV_USAGE_READ)) {
852 ivpu_dbg(vdev, IOCTL, "Buffer is already in use by another job\n");
853 return -EBUSY;
854 }
855
856 if (commands_offset >= ivpu_bo_size(bo)) {
857 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u for buffer size %zu\n",
858 commands_offset, ivpu_bo_size(bo));
859 return -EINVAL;
860 }
861
862 job->cmd_buf_vpu_addr = bo->vpu_addr + commands_offset;
863
864 if (preempt_buffer_index) {
865 struct ivpu_bo *preempt_bo = job->bos[preempt_buffer_index];
866
867 if (ivpu_bo_size(preempt_bo) < ivpu_fw_preempt_buf_size(vdev)) {
868 ivpu_dbg(vdev, IOCTL, "Preemption buffer is too small\n");
869 return -EINVAL;
870 }
871 if (ivpu_bo_is_mappable(preempt_bo)) {
872 ivpu_dbg(vdev, IOCTL, "Preemption buffer cannot be mappable\n");
873 return -EINVAL;
874 }
875 job->primary_preempt_buf = preempt_bo;
876 }
877
878 ret = drm_gem_lock_reservations((struct drm_gem_object **)job->bos, buf_count,
879 &acquire_ctx);
880 if (ret) {
881 ivpu_warn_ratelimited(vdev, "Failed to lock reservations: %d\n", ret);
882 return ret;
883 }
884
885 for (i = 0; i < buf_count; i++) {
886 ret = dma_resv_reserve_fences(job->bos[i]->base.base.resv, 1);
887 if (ret) {
888 ivpu_warn_ratelimited(vdev, "Failed to reserve fences: %d\n", ret);
889 goto unlock_reservations;
890 }
891 }
892
893 for (i = 0; i < buf_count; i++) {
894 usage = (i == CMD_BUF_IDX) ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_BOOKKEEP;
895 dma_resv_add_fence(job->bos[i]->base.base.resv, job->done_fence, usage);
896 }
897
898 unlock_reservations:
899 drm_gem_unlock_reservations((struct drm_gem_object **)job->bos, buf_count, &acquire_ctx);
900
901 wmb(); /* Flush write combining buffers */
902
903 return ret;
904 }
905
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)906 static int ivpu_submit(struct drm_file *file, struct ivpu_file_priv *file_priv, u32 cmdq_id,
907 u32 buffer_count, u32 engine, void __user *buffers_ptr, u32 cmds_offset,
908 u32 preempt_buffer_index, u8 priority)
909 {
910 struct ivpu_device *vdev = file_priv->vdev;
911 struct ivpu_job *job;
912 u32 *buf_handles;
913 int idx, ret;
914
915 buf_handles = kcalloc(buffer_count, sizeof(u32), GFP_KERNEL);
916 if (!buf_handles)
917 return -ENOMEM;
918
919 ret = copy_from_user(buf_handles, buffers_ptr, buffer_count * sizeof(u32));
920 if (ret) {
921 ret = -EFAULT;
922 goto err_free_handles;
923 }
924
925 if (!drm_dev_enter(&vdev->drm, &idx)) {
926 ret = -ENODEV;
927 goto err_free_handles;
928 }
929
930 ivpu_dbg(vdev, JOB, "Submit ioctl: ctx %u cmdq_id %u buf_count %u\n",
931 file_priv->ctx.id, cmdq_id, buffer_count);
932
933 job = ivpu_job_create(file_priv, engine, buffer_count);
934 if (!job) {
935 ret = -ENOMEM;
936 goto err_exit_dev;
937 }
938
939 ret = ivpu_job_prepare_bos_for_submit(file, job, buf_handles, buffer_count, cmds_offset,
940 preempt_buffer_index);
941 if (ret)
942 goto err_destroy_job;
943
944 down_read(&vdev->pm->reset_lock);
945 ret = ivpu_job_submit(job, priority, cmdq_id);
946 up_read(&vdev->pm->reset_lock);
947 if (ret)
948 goto err_signal_fence;
949
950 drm_dev_exit(idx);
951 kfree(buf_handles);
952 return ret;
953
954 err_signal_fence:
955 dma_fence_signal(job->done_fence);
956 err_destroy_job:
957 ivpu_job_destroy(job);
958 err_exit_dev:
959 drm_dev_exit(idx);
960 err_free_handles:
961 kfree(buf_handles);
962 return ret;
963 }
964
ivpu_submit_ioctl(struct drm_device * dev,void * data,struct drm_file * file)965 int ivpu_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
966 {
967 struct ivpu_file_priv *file_priv = file->driver_priv;
968 struct ivpu_device *vdev = file_priv->vdev;
969 struct drm_ivpu_submit *args = data;
970 u8 priority;
971
972 if (args->engine != DRM_IVPU_ENGINE_COMPUTE) {
973 ivpu_dbg(vdev, IOCTL, "Invalid engine %d\n", args->engine);
974 return -EINVAL;
975 }
976
977 if (args->priority > DRM_IVPU_JOB_PRIORITY_REALTIME) {
978 ivpu_dbg(vdev, IOCTL, "Invalid priority %d\n", args->priority);
979 return -EINVAL;
980 }
981
982 if (args->buffer_count == 0 || args->buffer_count > JOB_MAX_BUFFER_COUNT) {
983 ivpu_dbg(vdev, IOCTL, "Invalid buffer count %u\n", args->buffer_count);
984 return -EINVAL;
985 }
986
987 if (!IS_ALIGNED(args->commands_offset, 8)) {
988 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u\n", args->commands_offset);
989 return -EINVAL;
990 }
991
992 if (!file_priv->ctx.id) {
993 ivpu_dbg(vdev, IOCTL, "Context not initialized\n");
994 return -EINVAL;
995 }
996
997 if (file_priv->has_mmu_faults) {
998 ivpu_dbg(vdev, IOCTL, "Context %u has MMU faults\n", file_priv->ctx.id);
999 return -EBADFD;
1000 }
1001
1002 priority = ivpu_job_to_jsm_priority(args->priority);
1003
1004 return ivpu_submit(file, file_priv, 0, args->buffer_count, args->engine,
1005 (void __user *)args->buffers_ptr, args->commands_offset, 0, priority);
1006 }
1007
ivpu_cmdq_submit_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1008 int ivpu_cmdq_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1009 {
1010 struct ivpu_file_priv *file_priv = file->driver_priv;
1011 struct ivpu_device *vdev = file_priv->vdev;
1012 struct drm_ivpu_cmdq_submit *args = data;
1013
1014 if (!ivpu_is_capable(file_priv->vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1015 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1016 return -ENODEV;
1017 }
1018
1019 if (args->cmdq_id < IVPU_CMDQ_MIN_ID || args->cmdq_id > IVPU_CMDQ_MAX_ID) {
1020 ivpu_dbg(vdev, IOCTL, "Invalid command queue ID %u\n", args->cmdq_id);
1021 return -EINVAL;
1022 }
1023
1024 if (args->buffer_count == 0 || args->buffer_count > JOB_MAX_BUFFER_COUNT) {
1025 ivpu_dbg(vdev, IOCTL, "Invalid buffer count %u\n", args->buffer_count);
1026 return -EINVAL;
1027 }
1028
1029 if (args->preempt_buffer_index >= args->buffer_count) {
1030 ivpu_dbg(vdev, IOCTL, "Invalid preemption buffer index %u\n",
1031 args->preempt_buffer_index);
1032 return -EINVAL;
1033 }
1034
1035 if (!IS_ALIGNED(args->commands_offset, 8)) {
1036 ivpu_dbg(vdev, IOCTL, "Invalid commands offset %u\n", args->commands_offset);
1037 return -EINVAL;
1038 }
1039
1040 if (!file_priv->ctx.id) {
1041 ivpu_dbg(vdev, IOCTL, "Context not initialized\n");
1042 return -EINVAL;
1043 }
1044
1045 if (file_priv->has_mmu_faults) {
1046 ivpu_dbg(vdev, IOCTL, "Context %u has MMU faults\n", file_priv->ctx.id);
1047 return -EBADFD;
1048 }
1049
1050 return ivpu_submit(file, file_priv, args->cmdq_id, args->buffer_count, VPU_ENGINE_COMPUTE,
1051 (void __user *)args->buffers_ptr, args->commands_offset,
1052 args->preempt_buffer_index, 0);
1053 }
1054
ivpu_cmdq_create_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1055 int ivpu_cmdq_create_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1056 {
1057 struct ivpu_file_priv *file_priv = file->driver_priv;
1058 struct ivpu_device *vdev = file_priv->vdev;
1059 struct drm_ivpu_cmdq_create *args = data;
1060 struct ivpu_cmdq *cmdq;
1061 int ret;
1062
1063 if (!ivpu_is_capable(vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1064 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1065 return -ENODEV;
1066 }
1067
1068 if (args->priority > DRM_IVPU_JOB_PRIORITY_REALTIME) {
1069 ivpu_dbg(vdev, IOCTL, "Invalid priority %d\n", args->priority);
1070 return -EINVAL;
1071 }
1072
1073 ret = ivpu_rpm_get(vdev);
1074 if (ret < 0)
1075 return ret;
1076
1077 mutex_lock(&file_priv->lock);
1078
1079 cmdq = ivpu_cmdq_create(file_priv, ivpu_job_to_jsm_priority(args->priority), args->flags);
1080 if (cmdq)
1081 args->cmdq_id = cmdq->id;
1082
1083 mutex_unlock(&file_priv->lock);
1084
1085 ivpu_rpm_put(vdev);
1086
1087 return cmdq ? 0 : -ENOMEM;
1088 }
1089
ivpu_cmdq_destroy_ioctl(struct drm_device * dev,void * data,struct drm_file * file)1090 int ivpu_cmdq_destroy_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
1091 {
1092 struct ivpu_file_priv *file_priv = file->driver_priv;
1093 struct ivpu_device *vdev = file_priv->vdev;
1094 struct drm_ivpu_cmdq_destroy *args = data;
1095 struct ivpu_cmdq *cmdq;
1096 u32 cmdq_id = 0;
1097 int ret;
1098
1099 if (!ivpu_is_capable(vdev, DRM_IVPU_CAP_MANAGE_CMDQ)) {
1100 ivpu_dbg(vdev, IOCTL, "Command queue management not supported\n");
1101 return -ENODEV;
1102 }
1103
1104 ret = ivpu_rpm_get(vdev);
1105 if (ret < 0)
1106 return ret;
1107
1108 mutex_lock(&file_priv->lock);
1109
1110 cmdq = xa_load(&file_priv->cmdq_xa, args->cmdq_id);
1111 if (!cmdq || cmdq->is_legacy) {
1112 ret = -ENOENT;
1113 } else {
1114 cmdq_id = cmdq->id;
1115 ivpu_cmdq_destroy(file_priv, cmdq);
1116 ret = 0;
1117 }
1118
1119 mutex_unlock(&file_priv->lock);
1120
1121 /* Abort any pending jobs only if cmdq was destroyed */
1122 if (!ret)
1123 ivpu_cmdq_abort_all_jobs(vdev, file_priv->ctx.id, cmdq_id);
1124
1125 ivpu_rpm_put(vdev);
1126
1127 return ret;
1128 }
1129
1130 static void
ivpu_job_done_callback(struct ivpu_device * vdev,struct ivpu_ipc_hdr * ipc_hdr,struct vpu_jsm_msg * jsm_msg)1131 ivpu_job_done_callback(struct ivpu_device *vdev, struct ivpu_ipc_hdr *ipc_hdr,
1132 struct vpu_jsm_msg *jsm_msg)
1133 {
1134 struct vpu_ipc_msg_payload_job_done *payload;
1135
1136 if (!jsm_msg) {
1137 ivpu_err(vdev, "IPC message has no JSM payload\n");
1138 return;
1139 }
1140
1141 if (jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
1142 ivpu_err(vdev, "Invalid JSM message result: %d\n", jsm_msg->result);
1143 return;
1144 }
1145
1146 payload = (struct vpu_ipc_msg_payload_job_done *)&jsm_msg->payload;
1147
1148 mutex_lock(&vdev->submitted_jobs_lock);
1149 if (!ivpu_job_handle_engine_error(vdev, payload->job_id, payload->job_status))
1150 /* No engine error, complete the job normally */
1151 ivpu_job_signal_and_defer_destroy(vdev, payload->job_id, payload->job_status);
1152 mutex_unlock(&vdev->submitted_jobs_lock);
1153 }
1154
ivpu_job_done_consumer_init(struct ivpu_device * vdev)1155 void ivpu_job_done_consumer_init(struct ivpu_device *vdev)
1156 {
1157 ivpu_ipc_consumer_add(vdev, &vdev->job_done_consumer,
1158 VPU_IPC_CHAN_JOB_RET, ivpu_job_done_callback);
1159 }
1160
ivpu_job_done_consumer_fini(struct ivpu_device * vdev)1161 void ivpu_job_done_consumer_fini(struct ivpu_device *vdev)
1162 {
1163 ivpu_ipc_consumer_del(vdev, &vdev->job_done_consumer);
1164 }
1165
reset_engine_and_mark_faulty_contexts(struct ivpu_device * vdev)1166 static int reset_engine_and_mark_faulty_contexts(struct ivpu_device *vdev)
1167 {
1168 u32 num_impacted_contexts;
1169 struct vpu_jsm_msg resp;
1170 int ret;
1171 u32 i;
1172
1173 ret = ivpu_jsm_reset_engine(vdev, 0, &resp);
1174 if (ret)
1175 return ret;
1176
1177 /*
1178 * If faults are detected, ignore guilty contexts from engine reset as NPU may not be stuck
1179 * and could return currently running good context and faulty contexts are already marked
1180 */
1181 if (atomic_cmpxchg(&vdev->faults_detected, 1, 0) == 1)
1182 return 0;
1183
1184 num_impacted_contexts = resp.payload.engine_reset_done.num_impacted_contexts;
1185
1186 ivpu_warn_ratelimited(vdev, "Engine reset performed, impacted contexts: %u\n",
1187 num_impacted_contexts);
1188
1189 if (!in_range(num_impacted_contexts, 1, VPU_MAX_ENGINE_RESET_IMPACTED_CONTEXTS - 1)) {
1190 ivpu_pm_trigger_recovery(vdev, "Cannot determine guilty contexts");
1191 return -EIO;
1192 }
1193
1194 /* No faults detected, NPU likely got stuck. Mark returned contexts as guilty */
1195 guard(mutex)(&vdev->context_list_lock);
1196
1197 for (i = 0; i < num_impacted_contexts; i++) {
1198 u32 ssid = resp.payload.engine_reset_done.impacted_contexts[i].host_ssid;
1199 struct ivpu_file_priv *file_priv = xa_load(&vdev->context_xa, ssid);
1200
1201 if (file_priv) {
1202 mutex_lock(&file_priv->lock);
1203 file_priv->has_mmu_faults = true;
1204 mutex_unlock(&file_priv->lock);
1205 }
1206 }
1207
1208 return 0;
1209 }
1210
ivpu_context_abort_work_fn(struct work_struct * work)1211 void ivpu_context_abort_work_fn(struct work_struct *work)
1212 {
1213 struct ivpu_device *vdev = container_of(work, struct ivpu_device, context_abort_work);
1214 struct ivpu_file_priv *file_priv;
1215 struct ivpu_job *job;
1216 unsigned long ctx_id;
1217 unsigned long id;
1218
1219 if (drm_WARN_ON(&vdev->drm, pm_runtime_get_if_active(vdev->drm.dev) <= 0))
1220 return;
1221
1222 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
1223 if (reset_engine_and_mark_faulty_contexts(vdev))
1224 goto runtime_put;
1225
1226 mutex_lock(&vdev->context_list_lock);
1227 xa_for_each(&vdev->context_xa, ctx_id, file_priv) {
1228 if (!file_priv->has_mmu_faults || file_priv->aborted)
1229 continue;
1230
1231 mutex_lock(&file_priv->lock);
1232 ivpu_context_abort_locked(file_priv);
1233 mutex_unlock(&file_priv->lock);
1234 }
1235 mutex_unlock(&vdev->context_list_lock);
1236
1237 /*
1238 * We will not receive new MMU event interrupts until existing events are discarded
1239 * however, we want to discard these events only after aborting the faulty context
1240 * to avoid generating new faults from that context
1241 */
1242 ivpu_mmu_discard_events(vdev);
1243
1244 if (vdev->fw->sched_mode != VPU_SCHEDULING_MODE_HW)
1245 goto runtime_put;
1246
1247 if (ivpu_jsm_hws_resume_engine(vdev, 0))
1248 goto runtime_put;
1249 /*
1250 * In hardware scheduling mode NPU already has stopped processing jobs
1251 * and won't send us any further notifications, thus we have to free job related resources
1252 * and notify userspace
1253 */
1254 mutex_lock(&vdev->submitted_jobs_lock);
1255 xa_for_each(&vdev->submitted_jobs_xa, id, job)
1256 if (job->file_priv->aborted)
1257 ivpu_job_signal_and_destroy(vdev, job->job_id, DRM_IVPU_JOB_STATUS_ABORTED);
1258 mutex_unlock(&vdev->submitted_jobs_lock);
1259
1260 runtime_put:
1261 pm_runtime_put_autosuspend(vdev->drm.dev);
1262 }
1263