1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2020-2024 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/module.h>
12 #include <uapi/drm/ivpu_accel.h>
13
14 #include "ivpu_drv.h"
15 #include "ivpu_fw.h"
16 #include "ivpu_hw.h"
17 #include "ivpu_ipc.h"
18 #include "ivpu_job.h"
19 #include "ivpu_jsm_msg.h"
20 #include "ivpu_pm.h"
21 #include "vpu_boot_api.h"
22
23 #define CMD_BUF_IDX 0
24 #define JOB_MAX_BUFFER_COUNT 65535
25
ivpu_cmdq_ring_db(struct ivpu_device * vdev,struct ivpu_cmdq * cmdq)26 static void ivpu_cmdq_ring_db(struct ivpu_device *vdev, struct ivpu_cmdq *cmdq)
27 {
28 ivpu_hw_db_set(vdev, cmdq->db_id);
29 }
30
ivpu_preemption_buffers_create(struct ivpu_device * vdev,struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)31 static int ivpu_preemption_buffers_create(struct ivpu_device *vdev,
32 struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
33 {
34 u64 primary_size = ALIGN(vdev->fw->primary_preempt_buf_size, PAGE_SIZE);
35 u64 secondary_size = ALIGN(vdev->fw->secondary_preempt_buf_size, PAGE_SIZE);
36 struct ivpu_addr_range range;
37
38 if (vdev->fw->sched_mode != VPU_SCHEDULING_MODE_HW)
39 return 0;
40
41 range.start = vdev->hw->ranges.user.end - (primary_size * IVPU_NUM_CMDQS_PER_CTX);
42 range.end = vdev->hw->ranges.user.end;
43 cmdq->primary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &range, primary_size,
44 DRM_IVPU_BO_WC);
45 if (!cmdq->primary_preempt_buf) {
46 ivpu_err(vdev, "Failed to create primary preemption buffer\n");
47 return -ENOMEM;
48 }
49
50 range.start = vdev->hw->ranges.shave.end - (secondary_size * IVPU_NUM_CMDQS_PER_CTX);
51 range.end = vdev->hw->ranges.shave.end;
52 cmdq->secondary_preempt_buf = ivpu_bo_create(vdev, &file_priv->ctx, &range, secondary_size,
53 DRM_IVPU_BO_WC);
54 if (!cmdq->secondary_preempt_buf) {
55 ivpu_err(vdev, "Failed to create secondary preemption buffer\n");
56 goto err_free_primary;
57 }
58
59 return 0;
60
61 err_free_primary:
62 ivpu_bo_free(cmdq->primary_preempt_buf);
63 cmdq->primary_preempt_buf = NULL;
64 return -ENOMEM;
65 }
66
ivpu_preemption_buffers_free(struct ivpu_device * vdev,struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)67 static void ivpu_preemption_buffers_free(struct ivpu_device *vdev,
68 struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
69 {
70 if (vdev->fw->sched_mode != VPU_SCHEDULING_MODE_HW)
71 return;
72
73 if (cmdq->primary_preempt_buf)
74 ivpu_bo_free(cmdq->primary_preempt_buf);
75 if (cmdq->secondary_preempt_buf)
76 ivpu_bo_free(cmdq->secondary_preempt_buf);
77 }
78
ivpu_cmdq_alloc(struct ivpu_file_priv * file_priv)79 static struct ivpu_cmdq *ivpu_cmdq_alloc(struct ivpu_file_priv *file_priv)
80 {
81 struct ivpu_device *vdev = file_priv->vdev;
82 struct ivpu_cmdq *cmdq;
83 int ret;
84
85 cmdq = kzalloc(sizeof(*cmdq), GFP_KERNEL);
86 if (!cmdq)
87 return NULL;
88
89 cmdq->mem = ivpu_bo_create_global(vdev, SZ_4K, DRM_IVPU_BO_WC | DRM_IVPU_BO_MAPPABLE);
90 if (!cmdq->mem)
91 goto err_free_cmdq;
92
93 ret = ivpu_preemption_buffers_create(vdev, file_priv, cmdq);
94 if (ret)
95 ivpu_warn(vdev, "Failed to allocate preemption buffers, preemption limited\n");
96
97 return cmdq;
98
99 err_free_cmdq:
100 kfree(cmdq);
101 return NULL;
102 }
103
ivpu_cmdq_free(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)104 static void ivpu_cmdq_free(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
105 {
106 if (!cmdq)
107 return;
108
109 ivpu_preemption_buffers_free(file_priv->vdev, file_priv, cmdq);
110 ivpu_bo_free(cmdq->mem);
111 xa_erase(&file_priv->vdev->db_xa, cmdq->db_id);
112 kfree(cmdq);
113 }
114
ivpu_hws_cmdq_init(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq,u16 engine,u8 priority)115 static int ivpu_hws_cmdq_init(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq, u16 engine,
116 u8 priority)
117 {
118 struct ivpu_device *vdev = file_priv->vdev;
119 int ret;
120
121 ret = ivpu_jsm_hws_create_cmdq(vdev, file_priv->ctx.id, file_priv->ctx.id, cmdq->id,
122 task_pid_nr(current), engine,
123 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
124 if (ret)
125 return ret;
126
127 ret = ivpu_jsm_hws_set_context_sched_properties(vdev, file_priv->ctx.id, cmdq->id,
128 priority);
129 if (ret)
130 return ret;
131
132 return 0;
133 }
134
ivpu_register_db(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)135 static int ivpu_register_db(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
136 {
137 struct ivpu_device *vdev = file_priv->vdev;
138 int ret;
139
140 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
141 ret = ivpu_jsm_hws_register_db(vdev, file_priv->ctx.id, cmdq->id, cmdq->db_id,
142 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
143 else
144 ret = ivpu_jsm_register_db(vdev, file_priv->ctx.id, cmdq->db_id,
145 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem));
146
147 if (!ret)
148 ivpu_dbg(vdev, JOB, "DB %d registered to cmdq %d ctx %d\n",
149 cmdq->db_id, cmdq->id, file_priv->ctx.id);
150
151 return ret;
152 }
153
154 static int
ivpu_cmdq_init(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq,u8 priority)155 ivpu_cmdq_init(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq, u8 priority)
156 {
157 struct ivpu_device *vdev = file_priv->vdev;
158 struct vpu_job_queue_header *jobq_header;
159 int ret;
160
161 lockdep_assert_held(&file_priv->lock);
162
163 if (cmdq->db_registered)
164 return 0;
165
166 cmdq->entry_count = (u32)((ivpu_bo_size(cmdq->mem) - sizeof(struct vpu_job_queue_header)) /
167 sizeof(struct vpu_job_queue_entry));
168
169 cmdq->jobq = (struct vpu_job_queue *)ivpu_bo_vaddr(cmdq->mem);
170 jobq_header = &cmdq->jobq->header;
171 jobq_header->engine_idx = VPU_ENGINE_COMPUTE;
172 jobq_header->head = 0;
173 jobq_header->tail = 0;
174 wmb(); /* Flush WC buffer for jobq->header */
175
176 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) {
177 ret = ivpu_hws_cmdq_init(file_priv, cmdq, VPU_ENGINE_COMPUTE, priority);
178 if (ret)
179 return ret;
180 }
181
182 ret = ivpu_register_db(file_priv, cmdq);
183 if (ret)
184 return ret;
185
186 cmdq->db_registered = true;
187
188 return 0;
189 }
190
ivpu_cmdq_fini(struct ivpu_file_priv * file_priv,struct ivpu_cmdq * cmdq)191 static int ivpu_cmdq_fini(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq)
192 {
193 struct ivpu_device *vdev = file_priv->vdev;
194 int ret;
195
196 lockdep_assert_held(&file_priv->lock);
197
198 if (!cmdq->db_registered)
199 return 0;
200
201 cmdq->db_registered = false;
202
203 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) {
204 ret = ivpu_jsm_hws_destroy_cmdq(vdev, file_priv->ctx.id, cmdq->id);
205 if (!ret)
206 ivpu_dbg(vdev, JOB, "Command queue %d destroyed\n", cmdq->id);
207 }
208
209 ret = ivpu_jsm_unregister_db(vdev, cmdq->db_id);
210 if (!ret)
211 ivpu_dbg(vdev, JOB, "DB %d unregistered\n", cmdq->db_id);
212
213 return 0;
214 }
215
ivpu_db_id_alloc(struct ivpu_device * vdev,u32 * db_id)216 static int ivpu_db_id_alloc(struct ivpu_device *vdev, u32 *db_id)
217 {
218 int ret;
219 u32 id;
220
221 ret = xa_alloc_cyclic(&vdev->db_xa, &id, NULL, vdev->db_limit, &vdev->db_next, GFP_KERNEL);
222 if (ret < 0)
223 return ret;
224
225 *db_id = id;
226 return 0;
227 }
228
ivpu_cmdq_id_alloc(struct ivpu_file_priv * file_priv,u32 * cmdq_id)229 static int ivpu_cmdq_id_alloc(struct ivpu_file_priv *file_priv, u32 *cmdq_id)
230 {
231 int ret;
232 u32 id;
233
234 ret = xa_alloc_cyclic(&file_priv->cmdq_xa, &id, NULL, file_priv->cmdq_limit,
235 &file_priv->cmdq_id_next, GFP_KERNEL);
236 if (ret < 0)
237 return ret;
238
239 *cmdq_id = id;
240 return 0;
241 }
242
ivpu_cmdq_acquire(struct ivpu_file_priv * file_priv,u8 priority)243 static struct ivpu_cmdq *ivpu_cmdq_acquire(struct ivpu_file_priv *file_priv, u8 priority)
244 {
245 struct ivpu_device *vdev = file_priv->vdev;
246 struct ivpu_cmdq *cmdq;
247 unsigned long id;
248 int ret;
249
250 lockdep_assert_held(&file_priv->lock);
251
252 xa_for_each(&file_priv->cmdq_xa, id, cmdq)
253 if (cmdq->priority == priority)
254 break;
255
256 if (!cmdq) {
257 cmdq = ivpu_cmdq_alloc(file_priv);
258 if (!cmdq) {
259 ivpu_err(vdev, "Failed to allocate command queue\n");
260 return NULL;
261 }
262
263 ret = ivpu_db_id_alloc(vdev, &cmdq->db_id);
264 if (ret) {
265 ivpu_err(file_priv->vdev, "Failed to allocate doorbell ID: %d\n", ret);
266 goto err_free_cmdq;
267 }
268
269 ret = ivpu_cmdq_id_alloc(file_priv, &cmdq->id);
270 if (ret) {
271 ivpu_err(vdev, "Failed to allocate command queue ID: %d\n", ret);
272 goto err_erase_db_id;
273 }
274
275 cmdq->priority = priority;
276 ret = xa_err(xa_store(&file_priv->cmdq_xa, cmdq->id, cmdq, GFP_KERNEL));
277 if (ret) {
278 ivpu_err(vdev, "Failed to store command queue in cmdq_xa: %d\n", ret);
279 goto err_erase_cmdq_id;
280 }
281 }
282
283 ret = ivpu_cmdq_init(file_priv, cmdq, priority);
284 if (ret) {
285 ivpu_err(vdev, "Failed to initialize command queue: %d\n", ret);
286 goto err_free_cmdq;
287 }
288
289 return cmdq;
290
291 err_erase_cmdq_id:
292 xa_erase(&file_priv->cmdq_xa, cmdq->id);
293 err_erase_db_id:
294 xa_erase(&vdev->db_xa, cmdq->db_id);
295 err_free_cmdq:
296 ivpu_cmdq_free(file_priv, cmdq);
297 return NULL;
298 }
299
ivpu_cmdq_release_all_locked(struct ivpu_file_priv * file_priv)300 void ivpu_cmdq_release_all_locked(struct ivpu_file_priv *file_priv)
301 {
302 struct ivpu_cmdq *cmdq;
303 unsigned long cmdq_id;
304
305 lockdep_assert_held(&file_priv->lock);
306
307 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq) {
308 xa_erase(&file_priv->cmdq_xa, cmdq_id);
309 ivpu_cmdq_fini(file_priv, cmdq);
310 ivpu_cmdq_free(file_priv, cmdq);
311 }
312 }
313
314 /*
315 * Mark the doorbell as unregistered
316 * This function needs to be called when the VPU hardware is restarted
317 * and FW loses job queue state. The next time job queue is used it
318 * will be registered again.
319 */
ivpu_cmdq_reset(struct ivpu_file_priv * file_priv)320 static void ivpu_cmdq_reset(struct ivpu_file_priv *file_priv)
321 {
322 struct ivpu_cmdq *cmdq;
323 unsigned long cmdq_id;
324
325 mutex_lock(&file_priv->lock);
326
327 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq)
328 cmdq->db_registered = false;
329
330 mutex_unlock(&file_priv->lock);
331 }
332
ivpu_cmdq_reset_all_contexts(struct ivpu_device * vdev)333 void ivpu_cmdq_reset_all_contexts(struct ivpu_device *vdev)
334 {
335 struct ivpu_file_priv *file_priv;
336 unsigned long ctx_id;
337
338 mutex_lock(&vdev->context_list_lock);
339
340 xa_for_each(&vdev->context_xa, ctx_id, file_priv)
341 ivpu_cmdq_reset(file_priv);
342
343 mutex_unlock(&vdev->context_list_lock);
344 }
345
ivpu_cmdq_fini_all(struct ivpu_file_priv * file_priv)346 static void ivpu_cmdq_fini_all(struct ivpu_file_priv *file_priv)
347 {
348 struct ivpu_cmdq *cmdq;
349 unsigned long cmdq_id;
350
351 xa_for_each(&file_priv->cmdq_xa, cmdq_id, cmdq)
352 ivpu_cmdq_fini(file_priv, cmdq);
353 }
354
ivpu_context_abort_locked(struct ivpu_file_priv * file_priv)355 void ivpu_context_abort_locked(struct ivpu_file_priv *file_priv)
356 {
357 struct ivpu_device *vdev = file_priv->vdev;
358
359 lockdep_assert_held(&file_priv->lock);
360
361 ivpu_cmdq_fini_all(file_priv);
362
363 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_OS)
364 ivpu_jsm_context_release(vdev, file_priv->ctx.id);
365
366 file_priv->aborted = true;
367 }
368
ivpu_cmdq_push_job(struct ivpu_cmdq * cmdq,struct ivpu_job * job)369 static int ivpu_cmdq_push_job(struct ivpu_cmdq *cmdq, struct ivpu_job *job)
370 {
371 struct ivpu_device *vdev = job->vdev;
372 struct vpu_job_queue_header *header = &cmdq->jobq->header;
373 struct vpu_job_queue_entry *entry;
374 u32 tail = READ_ONCE(header->tail);
375 u32 next_entry = (tail + 1) % cmdq->entry_count;
376
377 /* Check if there is space left in job queue */
378 if (next_entry == header->head) {
379 ivpu_dbg(vdev, JOB, "Job queue full: ctx %d cmdq %d db %d head %d tail %d\n",
380 job->file_priv->ctx.id, cmdq->id, cmdq->db_id, header->head, tail);
381 return -EBUSY;
382 }
383
384 entry = &cmdq->jobq->slot[tail].job;
385 entry->batch_buf_addr = job->cmd_buf_vpu_addr;
386 entry->job_id = job->job_id;
387 entry->flags = 0;
388 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_SUBMISSION))
389 entry->flags = VPU_JOB_FLAGS_NULL_SUBMISSION_MASK;
390
391 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW &&
392 (unlikely(!(ivpu_test_mode & IVPU_TEST_MODE_PREEMPTION_DISABLE)))) {
393 if (cmdq->primary_preempt_buf) {
394 entry->primary_preempt_buf_addr = cmdq->primary_preempt_buf->vpu_addr;
395 entry->primary_preempt_buf_size = ivpu_bo_size(cmdq->primary_preempt_buf);
396 }
397
398 if (cmdq->secondary_preempt_buf) {
399 entry->secondary_preempt_buf_addr = cmdq->secondary_preempt_buf->vpu_addr;
400 entry->secondary_preempt_buf_size =
401 ivpu_bo_size(cmdq->secondary_preempt_buf);
402 }
403 }
404
405 wmb(); /* Ensure that tail is updated after filling entry */
406 header->tail = next_entry;
407 wmb(); /* Flush WC buffer for jobq header */
408
409 return 0;
410 }
411
412 struct ivpu_fence {
413 struct dma_fence base;
414 spinlock_t lock; /* protects base */
415 struct ivpu_device *vdev;
416 };
417
to_vpu_fence(struct dma_fence * fence)418 static inline struct ivpu_fence *to_vpu_fence(struct dma_fence *fence)
419 {
420 return container_of(fence, struct ivpu_fence, base);
421 }
422
ivpu_fence_get_driver_name(struct dma_fence * fence)423 static const char *ivpu_fence_get_driver_name(struct dma_fence *fence)
424 {
425 return DRIVER_NAME;
426 }
427
ivpu_fence_get_timeline_name(struct dma_fence * fence)428 static const char *ivpu_fence_get_timeline_name(struct dma_fence *fence)
429 {
430 struct ivpu_fence *ivpu_fence = to_vpu_fence(fence);
431
432 return dev_name(ivpu_fence->vdev->drm.dev);
433 }
434
435 static const struct dma_fence_ops ivpu_fence_ops = {
436 .get_driver_name = ivpu_fence_get_driver_name,
437 .get_timeline_name = ivpu_fence_get_timeline_name,
438 };
439
ivpu_fence_create(struct ivpu_device * vdev)440 static struct dma_fence *ivpu_fence_create(struct ivpu_device *vdev)
441 {
442 struct ivpu_fence *fence;
443
444 fence = kzalloc(sizeof(*fence), GFP_KERNEL);
445 if (!fence)
446 return NULL;
447
448 fence->vdev = vdev;
449 spin_lock_init(&fence->lock);
450 dma_fence_init(&fence->base, &ivpu_fence_ops, &fence->lock, dma_fence_context_alloc(1), 1);
451
452 return &fence->base;
453 }
454
ivpu_job_destroy(struct ivpu_job * job)455 static void ivpu_job_destroy(struct ivpu_job *job)
456 {
457 struct ivpu_device *vdev = job->vdev;
458 u32 i;
459
460 ivpu_dbg(vdev, JOB, "Job destroyed: id %3u ctx %2d engine %d",
461 job->job_id, job->file_priv->ctx.id, job->engine_idx);
462
463 for (i = 0; i < job->bo_count; i++)
464 if (job->bos[i])
465 drm_gem_object_put(&job->bos[i]->base.base);
466
467 dma_fence_put(job->done_fence);
468 ivpu_file_priv_put(&job->file_priv);
469 kfree(job);
470 }
471
472 static struct ivpu_job *
ivpu_job_create(struct ivpu_file_priv * file_priv,u32 engine_idx,u32 bo_count)473 ivpu_job_create(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count)
474 {
475 struct ivpu_device *vdev = file_priv->vdev;
476 struct ivpu_job *job;
477
478 job = kzalloc(struct_size(job, bos, bo_count), GFP_KERNEL);
479 if (!job)
480 return NULL;
481
482 job->vdev = vdev;
483 job->engine_idx = engine_idx;
484 job->bo_count = bo_count;
485 job->done_fence = ivpu_fence_create(vdev);
486 if (!job->done_fence) {
487 ivpu_warn_ratelimited(vdev, "Failed to create a fence\n");
488 goto err_free_job;
489 }
490
491 job->file_priv = ivpu_file_priv_get(file_priv);
492
493 ivpu_dbg(vdev, JOB, "Job created: ctx %2d engine %d", file_priv->ctx.id, job->engine_idx);
494 return job;
495
496 err_free_job:
497 kfree(job);
498 return NULL;
499 }
500
ivpu_job_remove_from_submitted_jobs(struct ivpu_device * vdev,u32 job_id)501 static struct ivpu_job *ivpu_job_remove_from_submitted_jobs(struct ivpu_device *vdev, u32 job_id)
502 {
503 struct ivpu_job *job;
504
505 lockdep_assert_held(&vdev->submitted_jobs_lock);
506
507 job = xa_erase(&vdev->submitted_jobs_xa, job_id);
508 if (xa_empty(&vdev->submitted_jobs_xa) && job) {
509 vdev->busy_time = ktime_add(ktime_sub(ktime_get(), vdev->busy_start_ts),
510 vdev->busy_time);
511 }
512
513 return job;
514 }
515
ivpu_job_signal_and_destroy(struct ivpu_device * vdev,u32 job_id,u32 job_status)516 static int ivpu_job_signal_and_destroy(struct ivpu_device *vdev, u32 job_id, u32 job_status)
517 {
518 struct ivpu_job *job;
519
520 lockdep_assert_held(&vdev->submitted_jobs_lock);
521
522 job = xa_load(&vdev->submitted_jobs_xa, job_id);
523 if (!job)
524 return -ENOENT;
525
526 if (job_status == VPU_JSM_STATUS_MVNCI_CONTEXT_VIOLATION_HW) {
527 guard(mutex)(&job->file_priv->lock);
528
529 if (job->file_priv->has_mmu_faults)
530 return 0;
531
532 /*
533 * Mark context as faulty and defer destruction of the job to jobs abort thread
534 * handler to synchronize between both faults and jobs returning context violation
535 * status and ensure both are handled in the same way
536 */
537 job->file_priv->has_mmu_faults = true;
538 queue_work(system_wq, &vdev->context_abort_work);
539 return 0;
540 }
541
542 job = ivpu_job_remove_from_submitted_jobs(vdev, job_id);
543 if (!job)
544 return -ENOENT;
545
546 if (job->file_priv->has_mmu_faults)
547 job_status = DRM_IVPU_JOB_STATUS_ABORTED;
548
549 job->bos[CMD_BUF_IDX]->job_status = job_status;
550 dma_fence_signal(job->done_fence);
551
552 ivpu_dbg(vdev, JOB, "Job complete: id %3u ctx %2d engine %d status 0x%x\n",
553 job->job_id, job->file_priv->ctx.id, job->engine_idx, job_status);
554
555 ivpu_job_destroy(job);
556 ivpu_stop_job_timeout_detection(vdev);
557
558 ivpu_rpm_put(vdev);
559
560 if (!xa_empty(&vdev->submitted_jobs_xa))
561 ivpu_start_job_timeout_detection(vdev);
562
563 return 0;
564 }
565
ivpu_jobs_abort_all(struct ivpu_device * vdev)566 void ivpu_jobs_abort_all(struct ivpu_device *vdev)
567 {
568 struct ivpu_job *job;
569 unsigned long id;
570
571 mutex_lock(&vdev->submitted_jobs_lock);
572
573 xa_for_each(&vdev->submitted_jobs_xa, id, job)
574 ivpu_job_signal_and_destroy(vdev, id, DRM_IVPU_JOB_STATUS_ABORTED);
575
576 mutex_unlock(&vdev->submitted_jobs_lock);
577 }
578
ivpu_job_submit(struct ivpu_job * job,u8 priority)579 static int ivpu_job_submit(struct ivpu_job *job, u8 priority)
580 {
581 struct ivpu_file_priv *file_priv = job->file_priv;
582 struct ivpu_device *vdev = job->vdev;
583 struct ivpu_cmdq *cmdq;
584 bool is_first_job;
585 int ret;
586
587 ret = ivpu_rpm_get(vdev);
588 if (ret < 0)
589 return ret;
590
591 mutex_lock(&vdev->submitted_jobs_lock);
592 mutex_lock(&file_priv->lock);
593
594 cmdq = ivpu_cmdq_acquire(file_priv, priority);
595 if (!cmdq) {
596 ivpu_warn_ratelimited(vdev, "Failed to get job queue, ctx %d engine %d prio %d\n",
597 file_priv->ctx.id, job->engine_idx, priority);
598 ret = -EINVAL;
599 goto err_unlock;
600 }
601
602 is_first_job = xa_empty(&vdev->submitted_jobs_xa);
603 ret = xa_alloc_cyclic(&vdev->submitted_jobs_xa, &job->job_id, job, file_priv->job_limit,
604 &file_priv->job_id_next, GFP_KERNEL);
605 if (ret < 0) {
606 ivpu_dbg(vdev, JOB, "Too many active jobs in ctx %d\n",
607 file_priv->ctx.id);
608 ret = -EBUSY;
609 goto err_unlock;
610 }
611
612 ret = ivpu_cmdq_push_job(cmdq, job);
613 if (ret)
614 goto err_erase_xa;
615
616 ivpu_start_job_timeout_detection(vdev);
617
618 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
619 cmdq->jobq->header.head = cmdq->jobq->header.tail;
620 wmb(); /* Flush WC buffer for jobq header */
621 } else {
622 ivpu_cmdq_ring_db(vdev, cmdq);
623 if (is_first_job)
624 vdev->busy_start_ts = ktime_get();
625 }
626
627 ivpu_dbg(vdev, JOB, "Job submitted: id %3u ctx %2d engine %d prio %d addr 0x%llx next %d\n",
628 job->job_id, file_priv->ctx.id, job->engine_idx, priority,
629 job->cmd_buf_vpu_addr, cmdq->jobq->header.tail);
630
631 mutex_unlock(&file_priv->lock);
632
633 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_HW)) {
634 ivpu_job_signal_and_destroy(vdev, job->job_id, VPU_JSM_STATUS_SUCCESS);
635 }
636
637 mutex_unlock(&vdev->submitted_jobs_lock);
638
639 return 0;
640
641 err_erase_xa:
642 xa_erase(&vdev->submitted_jobs_xa, job->job_id);
643 err_unlock:
644 mutex_unlock(&vdev->submitted_jobs_lock);
645 mutex_unlock(&file_priv->lock);
646 ivpu_rpm_put(vdev);
647 return ret;
648 }
649
650 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)651 ivpu_job_prepare_bos_for_submit(struct drm_file *file, struct ivpu_job *job, u32 *buf_handles,
652 u32 buf_count, u32 commands_offset)
653 {
654 struct ivpu_file_priv *file_priv = file->driver_priv;
655 struct ivpu_device *vdev = file_priv->vdev;
656 struct ww_acquire_ctx acquire_ctx;
657 enum dma_resv_usage usage;
658 struct ivpu_bo *bo;
659 int ret;
660 u32 i;
661
662 for (i = 0; i < buf_count; i++) {
663 struct drm_gem_object *obj = drm_gem_object_lookup(file, buf_handles[i]);
664
665 if (!obj)
666 return -ENOENT;
667
668 job->bos[i] = to_ivpu_bo(obj);
669
670 ret = ivpu_bo_pin(job->bos[i]);
671 if (ret)
672 return ret;
673 }
674
675 bo = job->bos[CMD_BUF_IDX];
676 if (!dma_resv_test_signaled(bo->base.base.resv, DMA_RESV_USAGE_READ)) {
677 ivpu_warn(vdev, "Buffer is already in use\n");
678 return -EBUSY;
679 }
680
681 if (commands_offset >= ivpu_bo_size(bo)) {
682 ivpu_warn(vdev, "Invalid command buffer offset %u\n", commands_offset);
683 return -EINVAL;
684 }
685
686 job->cmd_buf_vpu_addr = bo->vpu_addr + commands_offset;
687
688 ret = drm_gem_lock_reservations((struct drm_gem_object **)job->bos, buf_count,
689 &acquire_ctx);
690 if (ret) {
691 ivpu_warn(vdev, "Failed to lock reservations: %d\n", ret);
692 return ret;
693 }
694
695 for (i = 0; i < buf_count; i++) {
696 ret = dma_resv_reserve_fences(job->bos[i]->base.base.resv, 1);
697 if (ret) {
698 ivpu_warn(vdev, "Failed to reserve fences: %d\n", ret);
699 goto unlock_reservations;
700 }
701 }
702
703 for (i = 0; i < buf_count; i++) {
704 usage = (i == CMD_BUF_IDX) ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_BOOKKEEP;
705 dma_resv_add_fence(job->bos[i]->base.base.resv, job->done_fence, usage);
706 }
707
708 unlock_reservations:
709 drm_gem_unlock_reservations((struct drm_gem_object **)job->bos, buf_count, &acquire_ctx);
710
711 wmb(); /* Flush write combining buffers */
712
713 return ret;
714 }
715
ivpu_job_to_hws_priority(struct ivpu_file_priv * file_priv,u8 priority)716 static inline u8 ivpu_job_to_hws_priority(struct ivpu_file_priv *file_priv, u8 priority)
717 {
718 if (priority == DRM_IVPU_JOB_PRIORITY_DEFAULT)
719 return DRM_IVPU_JOB_PRIORITY_NORMAL;
720
721 return priority - 1;
722 }
723
ivpu_submit_ioctl(struct drm_device * dev,void * data,struct drm_file * file)724 int ivpu_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
725 {
726 struct ivpu_file_priv *file_priv = file->driver_priv;
727 struct ivpu_device *vdev = file_priv->vdev;
728 struct drm_ivpu_submit *params = data;
729 struct ivpu_job *job;
730 u32 *buf_handles;
731 int idx, ret;
732 u8 priority;
733
734 if (params->engine != DRM_IVPU_ENGINE_COMPUTE)
735 return -EINVAL;
736
737 if (params->priority > DRM_IVPU_JOB_PRIORITY_REALTIME)
738 return -EINVAL;
739
740 if (params->buffer_count == 0 || params->buffer_count > JOB_MAX_BUFFER_COUNT)
741 return -EINVAL;
742
743 if (!IS_ALIGNED(params->commands_offset, 8))
744 return -EINVAL;
745
746 if (!file_priv->ctx.id)
747 return -EINVAL;
748
749 if (file_priv->has_mmu_faults)
750 return -EBADFD;
751
752 buf_handles = kcalloc(params->buffer_count, sizeof(u32), GFP_KERNEL);
753 if (!buf_handles)
754 return -ENOMEM;
755
756 ret = copy_from_user(buf_handles,
757 (void __user *)params->buffers_ptr,
758 params->buffer_count * sizeof(u32));
759 if (ret) {
760 ret = -EFAULT;
761 goto err_free_handles;
762 }
763
764 if (!drm_dev_enter(&vdev->drm, &idx)) {
765 ret = -ENODEV;
766 goto err_free_handles;
767 }
768
769 ivpu_dbg(vdev, JOB, "Submit ioctl: ctx %u buf_count %u\n",
770 file_priv->ctx.id, params->buffer_count);
771
772 job = ivpu_job_create(file_priv, params->engine, params->buffer_count);
773 if (!job) {
774 ivpu_err(vdev, "Failed to create job\n");
775 ret = -ENOMEM;
776 goto err_exit_dev;
777 }
778
779 ret = ivpu_job_prepare_bos_for_submit(file, job, buf_handles, params->buffer_count,
780 params->commands_offset);
781 if (ret) {
782 ivpu_err(vdev, "Failed to prepare job: %d\n", ret);
783 goto err_destroy_job;
784 }
785
786 priority = ivpu_job_to_hws_priority(file_priv, params->priority);
787
788 down_read(&vdev->pm->reset_lock);
789 ret = ivpu_job_submit(job, priority);
790 up_read(&vdev->pm->reset_lock);
791 if (ret)
792 goto err_signal_fence;
793
794 drm_dev_exit(idx);
795 kfree(buf_handles);
796 return ret;
797
798 err_signal_fence:
799 dma_fence_signal(job->done_fence);
800 err_destroy_job:
801 ivpu_job_destroy(job);
802 err_exit_dev:
803 drm_dev_exit(idx);
804 err_free_handles:
805 kfree(buf_handles);
806 return ret;
807 }
808
809 static void
ivpu_job_done_callback(struct ivpu_device * vdev,struct ivpu_ipc_hdr * ipc_hdr,struct vpu_jsm_msg * jsm_msg)810 ivpu_job_done_callback(struct ivpu_device *vdev, struct ivpu_ipc_hdr *ipc_hdr,
811 struct vpu_jsm_msg *jsm_msg)
812 {
813 struct vpu_ipc_msg_payload_job_done *payload;
814
815 if (!jsm_msg) {
816 ivpu_err(vdev, "IPC message has no JSM payload\n");
817 return;
818 }
819
820 if (jsm_msg->result != VPU_JSM_STATUS_SUCCESS) {
821 ivpu_err(vdev, "Invalid JSM message result: %d\n", jsm_msg->result);
822 return;
823 }
824
825 payload = (struct vpu_ipc_msg_payload_job_done *)&jsm_msg->payload;
826
827 mutex_lock(&vdev->submitted_jobs_lock);
828 ivpu_job_signal_and_destroy(vdev, payload->job_id, payload->job_status);
829 mutex_unlock(&vdev->submitted_jobs_lock);
830 }
831
ivpu_job_done_consumer_init(struct ivpu_device * vdev)832 void ivpu_job_done_consumer_init(struct ivpu_device *vdev)
833 {
834 ivpu_ipc_consumer_add(vdev, &vdev->job_done_consumer,
835 VPU_IPC_CHAN_JOB_RET, ivpu_job_done_callback);
836 }
837
ivpu_job_done_consumer_fini(struct ivpu_device * vdev)838 void ivpu_job_done_consumer_fini(struct ivpu_device *vdev)
839 {
840 ivpu_ipc_consumer_del(vdev, &vdev->job_done_consumer);
841 }
842
ivpu_context_abort_thread_handler(struct work_struct * work)843 void ivpu_context_abort_thread_handler(struct work_struct *work)
844 {
845 struct ivpu_device *vdev = container_of(work, struct ivpu_device, context_abort_work);
846 struct ivpu_file_priv *file_priv;
847 unsigned long ctx_id;
848 struct ivpu_job *job;
849 unsigned long id;
850
851 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW)
852 if (ivpu_jsm_reset_engine(vdev, 0))
853 return;
854
855 mutex_lock(&vdev->context_list_lock);
856 xa_for_each(&vdev->context_xa, ctx_id, file_priv) {
857 if (!file_priv->has_mmu_faults || file_priv->aborted)
858 continue;
859
860 mutex_lock(&file_priv->lock);
861 ivpu_context_abort_locked(file_priv);
862 mutex_unlock(&file_priv->lock);
863 }
864 mutex_unlock(&vdev->context_list_lock);
865
866 if (vdev->fw->sched_mode != VPU_SCHEDULING_MODE_HW)
867 return;
868
869 if (ivpu_jsm_hws_resume_engine(vdev, 0))
870 return;
871 /*
872 * In hardware scheduling mode NPU already has stopped processing jobs
873 * and won't send us any further notifications, thus we have to free job related resources
874 * and notify userspace
875 */
876 mutex_lock(&vdev->submitted_jobs_lock);
877 xa_for_each(&vdev->submitted_jobs_xa, id, job)
878 if (job->file_priv->aborted)
879 ivpu_job_signal_and_destroy(vdev, job->job_id, DRM_IVPU_JOB_STATUS_ABORTED);
880 mutex_unlock(&vdev->submitted_jobs_lock);
881 }
882