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