1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020-2023 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_hw.h" 16 #include "ivpu_ipc.h" 17 #include "ivpu_job.h" 18 #include "ivpu_jsm_msg.h" 19 #include "ivpu_pm.h" 20 21 #define CMD_BUF_IDX 0 22 #define JOB_ID_JOB_MASK GENMASK(7, 0) 23 #define JOB_ID_CONTEXT_MASK GENMASK(31, 8) 24 #define JOB_MAX_BUFFER_COUNT 65535 25 26 static void ivpu_cmdq_ring_db(struct ivpu_device *vdev, struct ivpu_cmdq *cmdq) 27 { 28 ivpu_hw_reg_db_set(vdev, cmdq->db_id); 29 } 30 31 static struct ivpu_cmdq *ivpu_cmdq_alloc(struct ivpu_file_priv *file_priv, u16 engine) 32 { 33 struct ivpu_device *vdev = file_priv->vdev; 34 struct vpu_job_queue_header *jobq_header; 35 struct ivpu_cmdq *cmdq; 36 37 cmdq = kzalloc(sizeof(*cmdq), GFP_KERNEL); 38 if (!cmdq) 39 return NULL; 40 41 cmdq->mem = ivpu_bo_alloc_internal(vdev, 0, SZ_4K, DRM_IVPU_BO_WC); 42 if (!cmdq->mem) 43 goto cmdq_free; 44 45 cmdq->db_id = file_priv->ctx.id + engine * ivpu_get_context_count(vdev); 46 cmdq->entry_count = (u32)((ivpu_bo_size(cmdq->mem) - sizeof(struct vpu_job_queue_header)) / 47 sizeof(struct vpu_job_queue_entry)); 48 49 cmdq->jobq = (struct vpu_job_queue *)ivpu_bo_vaddr(cmdq->mem); 50 jobq_header = &cmdq->jobq->header; 51 jobq_header->engine_idx = engine; 52 jobq_header->head = 0; 53 jobq_header->tail = 0; 54 wmb(); /* Flush WC buffer for jobq->header */ 55 56 return cmdq; 57 58 cmdq_free: 59 kfree(cmdq); 60 return NULL; 61 } 62 63 static void ivpu_cmdq_free(struct ivpu_file_priv *file_priv, struct ivpu_cmdq *cmdq) 64 { 65 if (!cmdq) 66 return; 67 68 ivpu_bo_free_internal(cmdq->mem); 69 kfree(cmdq); 70 } 71 72 static struct ivpu_cmdq *ivpu_cmdq_acquire(struct ivpu_file_priv *file_priv, u16 engine) 73 { 74 struct ivpu_device *vdev = file_priv->vdev; 75 struct ivpu_cmdq *cmdq = file_priv->cmdq[engine]; 76 int ret; 77 78 lockdep_assert_held(&file_priv->lock); 79 80 if (!cmdq) { 81 cmdq = ivpu_cmdq_alloc(file_priv, engine); 82 if (!cmdq) 83 return NULL; 84 file_priv->cmdq[engine] = cmdq; 85 } 86 87 if (cmdq->db_registered) 88 return cmdq; 89 90 ret = ivpu_jsm_register_db(vdev, file_priv->ctx.id, cmdq->db_id, 91 cmdq->mem->vpu_addr, ivpu_bo_size(cmdq->mem)); 92 if (ret) 93 return NULL; 94 95 cmdq->db_registered = true; 96 97 return cmdq; 98 } 99 100 static void ivpu_cmdq_release_locked(struct ivpu_file_priv *file_priv, u16 engine) 101 { 102 struct ivpu_cmdq *cmdq = file_priv->cmdq[engine]; 103 104 lockdep_assert_held(&file_priv->lock); 105 106 if (cmdq) { 107 file_priv->cmdq[engine] = NULL; 108 if (cmdq->db_registered) 109 ivpu_jsm_unregister_db(file_priv->vdev, cmdq->db_id); 110 111 ivpu_cmdq_free(file_priv, cmdq); 112 } 113 } 114 115 void ivpu_cmdq_release_all(struct ivpu_file_priv *file_priv) 116 { 117 int i; 118 119 mutex_lock(&file_priv->lock); 120 121 for (i = 0; i < IVPU_NUM_ENGINES; i++) 122 ivpu_cmdq_release_locked(file_priv, i); 123 124 mutex_unlock(&file_priv->lock); 125 } 126 127 /* 128 * Mark the doorbell as unregistered and reset job queue pointers. 129 * This function needs to be called when the VPU hardware is restarted 130 * and FW looses job queue state. The next time job queue is used it 131 * will be registered again. 132 */ 133 static void ivpu_cmdq_reset_locked(struct ivpu_file_priv *file_priv, u16 engine) 134 { 135 struct ivpu_cmdq *cmdq = file_priv->cmdq[engine]; 136 137 lockdep_assert_held(&file_priv->lock); 138 139 if (cmdq) { 140 cmdq->db_registered = false; 141 cmdq->jobq->header.head = 0; 142 cmdq->jobq->header.tail = 0; 143 wmb(); /* Flush WC buffer for jobq header */ 144 } 145 } 146 147 static void ivpu_cmdq_reset_all(struct ivpu_file_priv *file_priv) 148 { 149 int i; 150 151 mutex_lock(&file_priv->lock); 152 153 for (i = 0; i < IVPU_NUM_ENGINES; i++) 154 ivpu_cmdq_reset_locked(file_priv, i); 155 156 mutex_unlock(&file_priv->lock); 157 } 158 159 void ivpu_cmdq_reset_all_contexts(struct ivpu_device *vdev) 160 { 161 struct ivpu_file_priv *file_priv; 162 unsigned long ctx_id; 163 164 xa_for_each(&vdev->context_xa, ctx_id, file_priv) { 165 file_priv = ivpu_file_priv_get_by_ctx_id(vdev, ctx_id); 166 if (!file_priv) 167 continue; 168 169 ivpu_cmdq_reset_all(file_priv); 170 171 ivpu_file_priv_put(&file_priv); 172 } 173 } 174 175 static int ivpu_cmdq_push_job(struct ivpu_cmdq *cmdq, struct ivpu_job *job) 176 { 177 struct ivpu_device *vdev = job->vdev; 178 struct vpu_job_queue_header *header = &cmdq->jobq->header; 179 struct vpu_job_queue_entry *entry; 180 u32 tail = READ_ONCE(header->tail); 181 u32 next_entry = (tail + 1) % cmdq->entry_count; 182 183 /* Check if there is space left in job queue */ 184 if (next_entry == header->head) { 185 ivpu_dbg(vdev, JOB, "Job queue full: ctx %d engine %d db %d head %d tail %d\n", 186 job->file_priv->ctx.id, job->engine_idx, cmdq->db_id, header->head, tail); 187 return -EBUSY; 188 } 189 190 entry = &cmdq->jobq->job[tail]; 191 entry->batch_buf_addr = job->cmd_buf_vpu_addr; 192 entry->job_id = job->job_id; 193 entry->flags = 0; 194 if (unlikely(ivpu_test_mode & IVPU_TEST_MODE_NULL_SUBMISSION)) 195 entry->flags = VPU_JOB_FLAGS_NULL_SUBMISSION_MASK; 196 wmb(); /* Ensure that tail is updated after filling entry */ 197 header->tail = next_entry; 198 wmb(); /* Flush WC buffer for jobq header */ 199 200 return 0; 201 } 202 203 struct ivpu_fence { 204 struct dma_fence base; 205 spinlock_t lock; /* protects base */ 206 struct ivpu_device *vdev; 207 }; 208 209 static inline struct ivpu_fence *to_vpu_fence(struct dma_fence *fence) 210 { 211 return container_of(fence, struct ivpu_fence, base); 212 } 213 214 static const char *ivpu_fence_get_driver_name(struct dma_fence *fence) 215 { 216 return DRIVER_NAME; 217 } 218 219 static const char *ivpu_fence_get_timeline_name(struct dma_fence *fence) 220 { 221 struct ivpu_fence *ivpu_fence = to_vpu_fence(fence); 222 223 return dev_name(ivpu_fence->vdev->drm.dev); 224 } 225 226 static const struct dma_fence_ops ivpu_fence_ops = { 227 .get_driver_name = ivpu_fence_get_driver_name, 228 .get_timeline_name = ivpu_fence_get_timeline_name, 229 }; 230 231 static struct dma_fence *ivpu_fence_create(struct ivpu_device *vdev) 232 { 233 struct ivpu_fence *fence; 234 235 fence = kzalloc(sizeof(*fence), GFP_KERNEL); 236 if (!fence) 237 return NULL; 238 239 fence->vdev = vdev; 240 spin_lock_init(&fence->lock); 241 dma_fence_init(&fence->base, &ivpu_fence_ops, &fence->lock, dma_fence_context_alloc(1), 1); 242 243 return &fence->base; 244 } 245 246 static void job_get(struct ivpu_job *job, struct ivpu_job **link) 247 { 248 struct ivpu_device *vdev = job->vdev; 249 250 kref_get(&job->ref); 251 *link = job; 252 253 ivpu_dbg(vdev, KREF, "Job get: id %u refcount %u\n", job->job_id, kref_read(&job->ref)); 254 } 255 256 static void job_release(struct kref *ref) 257 { 258 struct ivpu_job *job = container_of(ref, struct ivpu_job, ref); 259 struct ivpu_device *vdev = job->vdev; 260 u32 i; 261 262 for (i = 0; i < job->bo_count; i++) 263 if (job->bos[i]) 264 drm_gem_object_put(&job->bos[i]->base.base); 265 266 dma_fence_put(job->done_fence); 267 ivpu_file_priv_put(&job->file_priv); 268 269 ivpu_dbg(vdev, KREF, "Job released: id %u\n", job->job_id); 270 kfree(job); 271 272 /* Allow the VPU to get suspended, must be called after ivpu_file_priv_put() */ 273 ivpu_rpm_put(vdev); 274 } 275 276 static void job_put(struct ivpu_job *job) 277 { 278 struct ivpu_device *vdev = job->vdev; 279 280 ivpu_dbg(vdev, KREF, "Job put: id %u refcount %u\n", job->job_id, kref_read(&job->ref)); 281 kref_put(&job->ref, job_release); 282 } 283 284 static struct ivpu_job * 285 ivpu_create_job(struct ivpu_file_priv *file_priv, u32 engine_idx, u32 bo_count) 286 { 287 struct ivpu_device *vdev = file_priv->vdev; 288 struct ivpu_job *job; 289 int ret; 290 291 ret = ivpu_rpm_get(vdev); 292 if (ret < 0) 293 return NULL; 294 295 job = kzalloc(struct_size(job, bos, bo_count), GFP_KERNEL); 296 if (!job) 297 goto err_rpm_put; 298 299 kref_init(&job->ref); 300 301 job->vdev = vdev; 302 job->engine_idx = engine_idx; 303 job->bo_count = bo_count; 304 job->done_fence = ivpu_fence_create(vdev); 305 if (!job->done_fence) { 306 ivpu_warn_ratelimited(vdev, "Failed to create a fence\n"); 307 goto err_free_job; 308 } 309 310 job->file_priv = ivpu_file_priv_get(file_priv); 311 312 ivpu_dbg(vdev, JOB, "Job created: ctx %2d engine %d", file_priv->ctx.id, job->engine_idx); 313 314 return job; 315 316 err_free_job: 317 kfree(job); 318 err_rpm_put: 319 ivpu_rpm_put(vdev); 320 return NULL; 321 } 322 323 static int ivpu_job_done(struct ivpu_device *vdev, u32 job_id, u32 job_status) 324 { 325 struct ivpu_job *job; 326 327 job = xa_erase(&vdev->submitted_jobs_xa, job_id); 328 if (!job) 329 return -ENOENT; 330 331 if (job->file_priv->has_mmu_faults) 332 job_status = VPU_JSM_STATUS_ABORTED; 333 334 job->bos[CMD_BUF_IDX]->job_status = job_status; 335 dma_fence_signal(job->done_fence); 336 337 ivpu_dbg(vdev, JOB, "Job complete: id %3u ctx %2d engine %d status 0x%x\n", 338 job->job_id, job->file_priv->ctx.id, job->engine_idx, job_status); 339 340 ivpu_stop_job_timeout_detection(vdev); 341 342 job_put(job); 343 return 0; 344 } 345 346 void ivpu_jobs_abort_all(struct ivpu_device *vdev) 347 { 348 struct ivpu_job *job; 349 unsigned long id; 350 351 xa_for_each(&vdev->submitted_jobs_xa, id, job) 352 ivpu_job_done(vdev, id, VPU_JSM_STATUS_ABORTED); 353 } 354 355 static int ivpu_direct_job_submission(struct ivpu_job *job) 356 { 357 struct ivpu_file_priv *file_priv = job->file_priv; 358 struct ivpu_device *vdev = job->vdev; 359 struct xa_limit job_id_range; 360 struct ivpu_cmdq *cmdq; 361 int ret; 362 363 mutex_lock(&file_priv->lock); 364 365 cmdq = ivpu_cmdq_acquire(job->file_priv, job->engine_idx); 366 if (!cmdq) { 367 ivpu_warn(vdev, "Failed get job queue, ctx %d engine %d\n", 368 file_priv->ctx.id, job->engine_idx); 369 ret = -EINVAL; 370 goto err_unlock; 371 } 372 373 job_id_range.min = FIELD_PREP(JOB_ID_CONTEXT_MASK, (file_priv->ctx.id - 1)); 374 job_id_range.max = job_id_range.min | JOB_ID_JOB_MASK; 375 376 job_get(job, &job); 377 ret = xa_alloc(&vdev->submitted_jobs_xa, &job->job_id, job, job_id_range, GFP_KERNEL); 378 if (ret) { 379 ivpu_warn_ratelimited(vdev, "Failed to allocate job id: %d\n", ret); 380 goto err_job_put; 381 } 382 383 ret = ivpu_cmdq_push_job(cmdq, job); 384 if (ret) 385 goto err_xa_erase; 386 387 ivpu_start_job_timeout_detection(vdev); 388 389 ivpu_dbg(vdev, JOB, "Job submitted: id %3u addr 0x%llx ctx %2d engine %d next %d\n", 390 job->job_id, job->cmd_buf_vpu_addr, file_priv->ctx.id, 391 job->engine_idx, cmdq->jobq->header.tail); 392 393 if (ivpu_test_mode & IVPU_TEST_MODE_NULL_HW) { 394 ivpu_job_done(vdev, job->job_id, VPU_JSM_STATUS_SUCCESS); 395 cmdq->jobq->header.head = cmdq->jobq->header.tail; 396 wmb(); /* Flush WC buffer for jobq header */ 397 } else { 398 ivpu_cmdq_ring_db(vdev, cmdq); 399 } 400 401 mutex_unlock(&file_priv->lock); 402 return 0; 403 404 err_xa_erase: 405 xa_erase(&vdev->submitted_jobs_xa, job->job_id); 406 err_job_put: 407 job_put(job); 408 err_unlock: 409 mutex_unlock(&file_priv->lock); 410 return ret; 411 } 412 413 static int 414 ivpu_job_prepare_bos_for_submit(struct drm_file *file, struct ivpu_job *job, u32 *buf_handles, 415 u32 buf_count, u32 commands_offset) 416 { 417 struct ivpu_file_priv *file_priv = file->driver_priv; 418 struct ivpu_device *vdev = file_priv->vdev; 419 struct ww_acquire_ctx acquire_ctx; 420 enum dma_resv_usage usage; 421 struct ivpu_bo *bo; 422 int ret; 423 u32 i; 424 425 for (i = 0; i < buf_count; i++) { 426 struct drm_gem_object *obj = drm_gem_object_lookup(file, buf_handles[i]); 427 428 if (!obj) 429 return -ENOENT; 430 431 job->bos[i] = to_ivpu_bo(obj); 432 433 ret = ivpu_bo_pin(job->bos[i]); 434 if (ret) 435 return ret; 436 } 437 438 bo = job->bos[CMD_BUF_IDX]; 439 if (!dma_resv_test_signaled(bo->base.base.resv, DMA_RESV_USAGE_READ)) { 440 ivpu_warn(vdev, "Buffer is already in use\n"); 441 return -EBUSY; 442 } 443 444 if (commands_offset >= ivpu_bo_size(bo)) { 445 ivpu_warn(vdev, "Invalid command buffer offset %u\n", commands_offset); 446 return -EINVAL; 447 } 448 449 job->cmd_buf_vpu_addr = bo->vpu_addr + commands_offset; 450 451 ret = drm_gem_lock_reservations((struct drm_gem_object **)job->bos, buf_count, 452 &acquire_ctx); 453 if (ret) { 454 ivpu_warn(vdev, "Failed to lock reservations: %d\n", ret); 455 return ret; 456 } 457 458 for (i = 0; i < buf_count; i++) { 459 ret = dma_resv_reserve_fences(job->bos[i]->base.base.resv, 1); 460 if (ret) { 461 ivpu_warn(vdev, "Failed to reserve fences: %d\n", ret); 462 goto unlock_reservations; 463 } 464 } 465 466 for (i = 0; i < buf_count; i++) { 467 usage = (i == CMD_BUF_IDX) ? DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_BOOKKEEP; 468 dma_resv_add_fence(job->bos[i]->base.base.resv, job->done_fence, usage); 469 } 470 471 unlock_reservations: 472 drm_gem_unlock_reservations((struct drm_gem_object **)job->bos, buf_count, &acquire_ctx); 473 474 wmb(); /* Flush write combining buffers */ 475 476 return ret; 477 } 478 479 int ivpu_submit_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 480 { 481 struct ivpu_file_priv *file_priv = file->driver_priv; 482 struct ivpu_device *vdev = file_priv->vdev; 483 struct drm_ivpu_submit *params = data; 484 struct ivpu_job *job; 485 u32 *buf_handles; 486 int idx, ret; 487 488 if (params->engine > DRM_IVPU_ENGINE_COPY) 489 return -EINVAL; 490 491 if (params->buffer_count == 0 || params->buffer_count > JOB_MAX_BUFFER_COUNT) 492 return -EINVAL; 493 494 if (!IS_ALIGNED(params->commands_offset, 8)) 495 return -EINVAL; 496 497 if (!file_priv->ctx.id) 498 return -EINVAL; 499 500 if (file_priv->has_mmu_faults) 501 return -EBADFD; 502 503 buf_handles = kcalloc(params->buffer_count, sizeof(u32), GFP_KERNEL); 504 if (!buf_handles) 505 return -ENOMEM; 506 507 ret = copy_from_user(buf_handles, 508 (void __user *)params->buffers_ptr, 509 params->buffer_count * sizeof(u32)); 510 if (ret) { 511 ret = -EFAULT; 512 goto free_handles; 513 } 514 515 if (!drm_dev_enter(&vdev->drm, &idx)) { 516 ret = -ENODEV; 517 goto free_handles; 518 } 519 520 ivpu_dbg(vdev, JOB, "Submit ioctl: ctx %u buf_count %u\n", 521 file_priv->ctx.id, params->buffer_count); 522 523 job = ivpu_create_job(file_priv, params->engine, params->buffer_count); 524 if (!job) { 525 ivpu_err(vdev, "Failed to create job\n"); 526 ret = -ENOMEM; 527 goto dev_exit; 528 } 529 530 ret = ivpu_job_prepare_bos_for_submit(file, job, buf_handles, params->buffer_count, 531 params->commands_offset); 532 if (ret) { 533 ivpu_err(vdev, "Failed to prepare job, ret %d\n", ret); 534 goto job_put; 535 } 536 537 ret = ivpu_direct_job_submission(job); 538 if (ret) { 539 dma_fence_signal(job->done_fence); 540 ivpu_err(vdev, "Failed to submit job to the HW, ret %d\n", ret); 541 } 542 543 job_put: 544 job_put(job); 545 dev_exit: 546 drm_dev_exit(idx); 547 free_handles: 548 kfree(buf_handles); 549 550 return ret; 551 } 552 553 static void 554 ivpu_job_done_callback(struct ivpu_device *vdev, struct ivpu_ipc_hdr *ipc_hdr, 555 struct vpu_jsm_msg *jsm_msg) 556 { 557 struct vpu_ipc_msg_payload_job_done *payload; 558 int ret; 559 560 if (!jsm_msg) { 561 ivpu_err(vdev, "IPC message has no JSM payload\n"); 562 return; 563 } 564 565 if (jsm_msg->result != VPU_JSM_STATUS_SUCCESS) { 566 ivpu_err(vdev, "Invalid JSM message result: %d\n", jsm_msg->result); 567 return; 568 } 569 570 payload = (struct vpu_ipc_msg_payload_job_done *)&jsm_msg->payload; 571 ret = ivpu_job_done(vdev, payload->job_id, payload->job_status); 572 if (!ret && !xa_empty(&vdev->submitted_jobs_xa)) 573 ivpu_start_job_timeout_detection(vdev); 574 } 575 576 void ivpu_job_done_consumer_init(struct ivpu_device *vdev) 577 { 578 ivpu_ipc_consumer_add(vdev, &vdev->job_done_consumer, 579 VPU_IPC_CHAN_JOB_RET, ivpu_job_done_callback); 580 } 581 582 void ivpu_job_done_consumer_fini(struct ivpu_device *vdev) 583 { 584 ivpu_ipc_consumer_del(vdev, &vdev->job_done_consumer); 585 } 586