1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * Copyright (C) 2020-2026 Intel Corporation 4 */ 5 6 #include <linux/firmware.h> 7 #include <linux/module.h> 8 #include <linux/pci.h> 9 #include <linux/pm_runtime.h> 10 #include <linux/workqueue.h> 11 #include <generated/utsrelease.h> 12 13 #include <drm/drm_accel.h> 14 #include <drm/drm_file.h> 15 #include <drm/drm_gem.h> 16 #include <drm/drm_ioctl.h> 17 #include <drm/drm_prime.h> 18 19 #include "ivpu_coredump.h" 20 #include "ivpu_debugfs.h" 21 #include "ivpu_drv.h" 22 #include "ivpu_fw.h" 23 #include "ivpu_fw_log.h" 24 #include "ivpu_gem.h" 25 #include "ivpu_hw.h" 26 #include "ivpu_ipc.h" 27 #include "ivpu_job.h" 28 #include "ivpu_jsm_msg.h" 29 #include "ivpu_mmu.h" 30 #include "ivpu_mmu_context.h" 31 #include "ivpu_ms.h" 32 #include "ivpu_pm.h" 33 #include "ivpu_sysfs.h" 34 #include "vpu_boot_api.h" 35 36 #ifndef DRIVER_VERSION_STR 37 #define DRIVER_VERSION_STR "1.0.0 " UTS_RELEASE 38 #endif 39 40 int ivpu_dbg_mask; 41 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644); 42 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros."); 43 44 int ivpu_test_mode; 45 #if IS_ENABLED(CONFIG_DRM_ACCEL_IVPU_DEBUG) 46 module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644); 47 MODULE_PARM_DESC(test_mode, "Test mode mask. See IVPU_TEST_MODE_* macros."); 48 #endif 49 50 u8 ivpu_pll_min_ratio; 51 module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644); 52 MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set NPU frequency"); 53 54 u8 ivpu_pll_max_ratio = U8_MAX; 55 module_param_named(pll_max_ratio, ivpu_pll_max_ratio, byte, 0644); 56 MODULE_PARM_DESC(pll_max_ratio, "Maximum PLL ratio used to set NPU frequency"); 57 58 int ivpu_sched_mode = IVPU_SCHED_MODE_AUTO; 59 module_param_named(sched_mode, ivpu_sched_mode, int, 0444); 60 MODULE_PARM_DESC(sched_mode, "Scheduler mode: -1 - Use default scheduler, 0 - Use OS scheduler (supported on 27XX - 50XX), 1 - Use HW scheduler"); 61 62 bool ivpu_disable_mmu_cont_pages; 63 module_param_named(disable_mmu_cont_pages, ivpu_disable_mmu_cont_pages, bool, 0444); 64 MODULE_PARM_DESC(disable_mmu_cont_pages, "Disable MMU contiguous pages optimization"); 65 66 bool ivpu_force_snoop; 67 module_param_named(force_snoop, ivpu_force_snoop, bool, 0444); 68 MODULE_PARM_DESC(force_snoop, "Force snooping for NPU host memory access"); 69 70 static struct ivpu_user_limits *ivpu_user_limits_alloc(struct ivpu_device *vdev, uid_t uid) 71 { 72 struct ivpu_user_limits *limits; 73 74 limits = kzalloc_obj(*limits); 75 if (!limits) 76 return ERR_PTR(-ENOMEM); 77 78 kref_init(&limits->ref); 79 atomic_set(&limits->db_count, 0); 80 limits->vdev = vdev; 81 limits->uid = uid; 82 83 /* Allow root user to allocate all contexts */ 84 if (uid == 0) { 85 limits->max_ctx_count = ivpu_get_context_count(vdev); 86 limits->max_db_count = ivpu_get_doorbell_count(vdev); 87 } else { 88 limits->max_ctx_count = ivpu_get_context_count(vdev) / 2; 89 limits->max_db_count = ivpu_get_doorbell_count(vdev) / 2; 90 } 91 92 hash_add(vdev->user_limits, &limits->hash_node, uid); 93 94 return limits; 95 } 96 97 static struct ivpu_user_limits *ivpu_user_limits_get(struct ivpu_device *vdev) 98 { 99 struct ivpu_user_limits *limits; 100 uid_t uid = current_uid().val; 101 102 guard(mutex)(&vdev->user_limits_lock); 103 104 hash_for_each_possible(vdev->user_limits, limits, hash_node, uid) { 105 if (limits->uid == uid) { 106 if (kref_read(&limits->ref) >= limits->max_ctx_count) { 107 ivpu_dbg(vdev, IOCTL, "User %u exceeded max ctx count %u\n", uid, 108 limits->max_ctx_count); 109 return ERR_PTR(-EMFILE); 110 } 111 112 kref_get(&limits->ref); 113 return limits; 114 } 115 } 116 117 return ivpu_user_limits_alloc(vdev, uid); 118 } 119 120 static void ivpu_user_limits_release(struct kref *ref) 121 { 122 struct ivpu_user_limits *limits = container_of(ref, struct ivpu_user_limits, ref); 123 struct ivpu_device *vdev = limits->vdev; 124 125 lockdep_assert_held(&vdev->user_limits_lock); 126 drm_WARN_ON(&vdev->drm, atomic_read(&limits->db_count)); 127 hash_del(&limits->hash_node); 128 kfree(limits); 129 } 130 131 static void ivpu_user_limits_put(struct ivpu_device *vdev, struct ivpu_user_limits *limits) 132 { 133 guard(mutex)(&vdev->user_limits_lock); 134 kref_put(&limits->ref, ivpu_user_limits_release); 135 } 136 137 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv) 138 { 139 struct ivpu_device *vdev = file_priv->vdev; 140 141 kref_get(&file_priv->ref); 142 143 ivpu_dbg(vdev, KREF, "file_priv get: ctx %u refcount %u\n", 144 file_priv->ctx.id, kref_read(&file_priv->ref)); 145 146 return file_priv; 147 } 148 149 static void file_priv_unbind(struct ivpu_device *vdev, struct ivpu_file_priv *file_priv) 150 { 151 mutex_lock(&file_priv->lock); 152 if (file_priv->bound) { 153 ivpu_dbg(vdev, FILE, "file_priv unbind: ctx %u\n", file_priv->ctx.id); 154 155 ivpu_cmdq_release_all_locked(file_priv); 156 ivpu_bo_unbind_all_bos_from_context(vdev, &file_priv->ctx); 157 ivpu_mmu_context_fini(vdev, &file_priv->ctx); 158 file_priv->bound = false; 159 drm_WARN_ON(&vdev->drm, !xa_erase_irq(&vdev->context_xa, file_priv->ctx.id)); 160 } 161 mutex_unlock(&file_priv->lock); 162 } 163 164 static void file_priv_release(struct kref *ref) 165 { 166 struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref); 167 struct ivpu_device *vdev = file_priv->vdev; 168 169 ivpu_dbg(vdev, FILE, "file_priv release: ctx %u bound %d\n", 170 file_priv->ctx.id, (bool)file_priv->bound); 171 172 pm_runtime_get_sync(vdev->drm.dev); 173 mutex_lock(&vdev->context_list_lock); 174 file_priv_unbind(vdev, file_priv); 175 drm_WARN_ON(&vdev->drm, !xa_empty(&file_priv->cmdq_xa)); 176 xa_destroy(&file_priv->cmdq_xa); 177 mutex_unlock(&vdev->context_list_lock); 178 pm_runtime_put_autosuspend(vdev->drm.dev); 179 180 ivpu_user_limits_put(vdev, file_priv->user_limits); 181 mutex_destroy(&file_priv->ms_lock); 182 mutex_destroy(&file_priv->lock); 183 kfree(file_priv); 184 } 185 186 void ivpu_file_priv_put(struct ivpu_file_priv **link) 187 { 188 struct ivpu_file_priv *file_priv = *link; 189 struct ivpu_device *vdev = file_priv->vdev; 190 191 ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n", 192 file_priv->ctx.id, kref_read(&file_priv->ref)); 193 194 *link = NULL; 195 kref_put(&file_priv->ref, file_priv_release); 196 } 197 198 bool ivpu_is_capable(struct ivpu_device *vdev, u32 capability) 199 { 200 switch (capability) { 201 case DRM_IVPU_CAP_METRIC_STREAMER: 202 return true; 203 case DRM_IVPU_CAP_DMA_MEMORY_RANGE: 204 return true; 205 case DRM_IVPU_CAP_BO_CREATE_FROM_USERPTR: 206 return true; 207 case DRM_IVPU_CAP_MANAGE_CMDQ: 208 return vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW; 209 default: 210 return false; 211 } 212 } 213 214 static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 215 { 216 struct ivpu_file_priv *file_priv = file->driver_priv; 217 struct ivpu_device *vdev = file_priv->vdev; 218 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev); 219 struct drm_ivpu_param *args = data; 220 int ret = 0; 221 int idx; 222 223 if (!drm_dev_enter(dev, &idx)) 224 return -ENODEV; 225 226 switch (args->param) { 227 case DRM_IVPU_PARAM_DEVICE_ID: 228 args->value = pdev->device; 229 break; 230 case DRM_IVPU_PARAM_DEVICE_REVISION: 231 args->value = pdev->revision; 232 break; 233 case DRM_IVPU_PARAM_PLATFORM_TYPE: 234 args->value = vdev->platform; 235 break; 236 case DRM_IVPU_PARAM_CORE_CLOCK_RATE: 237 args->value = ivpu_hw_btrs_pll_ratio_to_hz(vdev, vdev->hw->pll.max_ratio); 238 break; 239 case DRM_IVPU_PARAM_NUM_CONTEXTS: 240 args->value = file_priv->user_limits->max_ctx_count; 241 break; 242 case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS: 243 args->value = vdev->hw->ranges.user.start; 244 break; 245 case DRM_IVPU_PARAM_CONTEXT_ID: 246 args->value = file_priv->ctx.id; 247 break; 248 case DRM_IVPU_PARAM_FW_API_VERSION: 249 if (args->index < VPU_FW_API_VER_NUM) { 250 struct vpu_firmware_header *fw_hdr; 251 252 fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data; 253 args->value = fw_hdr->api_version[args->index]; 254 } else { 255 ret = -EINVAL; 256 } 257 break; 258 case DRM_IVPU_PARAM_ENGINE_HEARTBEAT: 259 ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value); 260 break; 261 case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID: 262 args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter); 263 break; 264 case DRM_IVPU_PARAM_TILE_CONFIG: 265 args->value = vdev->hw->tile_fuse; 266 break; 267 case DRM_IVPU_PARAM_SKU: 268 args->value = vdev->hw->sku; 269 break; 270 case DRM_IVPU_PARAM_CAPABILITIES: 271 args->value = ivpu_is_capable(vdev, args->index); 272 break; 273 case DRM_IVPU_PARAM_PREEMPT_BUFFER_SIZE: 274 args->value = ivpu_fw_preempt_buf_size(vdev); 275 break; 276 default: 277 ret = -EINVAL; 278 break; 279 } 280 281 drm_dev_exit(idx); 282 return ret; 283 } 284 285 static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file) 286 { 287 struct drm_ivpu_param *args = data; 288 int ret = 0; 289 290 switch (args->param) { 291 default: 292 ret = -EINVAL; 293 } 294 295 return ret; 296 } 297 298 static int ivpu_open(struct drm_device *dev, struct drm_file *file) 299 { 300 struct ivpu_device *vdev = to_ivpu_device(dev); 301 struct ivpu_file_priv *file_priv; 302 struct ivpu_user_limits *limits; 303 u32 ctx_id; 304 int idx, ret; 305 306 if (!drm_dev_enter(dev, &idx)) 307 return -ENODEV; 308 309 limits = ivpu_user_limits_get(vdev); 310 if (IS_ERR(limits)) { 311 ret = PTR_ERR(limits); 312 goto err_dev_exit; 313 } 314 315 file_priv = kzalloc_obj(*file_priv); 316 if (!file_priv) { 317 ret = -ENOMEM; 318 goto err_user_limits_put; 319 } 320 321 INIT_LIST_HEAD(&file_priv->ms_instance_list); 322 323 file_priv->vdev = vdev; 324 file_priv->bound = true; 325 file_priv->user_limits = limits; 326 kref_init(&file_priv->ref); 327 mutex_init(&file_priv->lock); 328 mutex_init(&file_priv->ms_lock); 329 330 mutex_lock(&vdev->context_list_lock); 331 332 ret = xa_alloc_irq(&vdev->context_xa, &ctx_id, file_priv, 333 vdev->context_xa_limit, GFP_KERNEL); 334 if (ret) { 335 ivpu_err(vdev, "Failed to allocate context id: %d\n", ret); 336 goto err_unlock; 337 } 338 339 ivpu_mmu_context_init(vdev, &file_priv->ctx, ctx_id); 340 341 file_priv->job_limit.min = FIELD_PREP(IVPU_JOB_ID_CONTEXT_MASK, (file_priv->ctx.id - 1)); 342 file_priv->job_limit.max = file_priv->job_limit.min | IVPU_JOB_ID_JOB_MASK; 343 344 xa_init_flags(&file_priv->cmdq_xa, XA_FLAGS_ALLOC1); 345 file_priv->cmdq_limit.min = IVPU_CMDQ_MIN_ID; 346 file_priv->cmdq_limit.max = IVPU_CMDQ_MAX_ID; 347 348 mutex_unlock(&vdev->context_list_lock); 349 drm_dev_exit(idx); 350 351 file->driver_priv = file_priv; 352 353 ivpu_dbg(vdev, FILE, "file_priv create: ctx %u process %s pid %d\n", 354 ctx_id, current->comm, task_pid_nr(current)); 355 356 return 0; 357 358 err_unlock: 359 mutex_unlock(&vdev->context_list_lock); 360 mutex_destroy(&file_priv->ms_lock); 361 mutex_destroy(&file_priv->lock); 362 kfree(file_priv); 363 err_user_limits_put: 364 ivpu_user_limits_put(vdev, limits); 365 err_dev_exit: 366 drm_dev_exit(idx); 367 return ret; 368 } 369 370 static void ivpu_postclose(struct drm_device *dev, struct drm_file *file) 371 { 372 struct ivpu_file_priv *file_priv = file->driver_priv; 373 struct ivpu_device *vdev = to_ivpu_device(dev); 374 375 ivpu_dbg(vdev, FILE, "file_priv close: ctx %u process %s pid %d\n", 376 file_priv->ctx.id, current->comm, task_pid_nr(current)); 377 378 ivpu_ms_cleanup(file_priv); 379 ivpu_file_priv_put(&file_priv); 380 } 381 382 static const struct drm_ioctl_desc ivpu_drm_ioctls[] = { 383 DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0), 384 DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0), 385 DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0), 386 DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0), 387 DRM_IOCTL_DEF_DRV(IVPU_SUBMIT, ivpu_submit_ioctl, 0), 388 DRM_IOCTL_DEF_DRV(IVPU_BO_WAIT, ivpu_bo_wait_ioctl, 0), 389 DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_START, ivpu_ms_start_ioctl, 0), 390 DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_GET_DATA, ivpu_ms_get_data_ioctl, 0), 391 DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_STOP, ivpu_ms_stop_ioctl, 0), 392 DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_GET_INFO, ivpu_ms_get_info_ioctl, 0), 393 DRM_IOCTL_DEF_DRV(IVPU_CMDQ_CREATE, ivpu_cmdq_create_ioctl, 0), 394 DRM_IOCTL_DEF_DRV(IVPU_CMDQ_DESTROY, ivpu_cmdq_destroy_ioctl, 0), 395 DRM_IOCTL_DEF_DRV(IVPU_CMDQ_SUBMIT, ivpu_cmdq_submit_ioctl, 0), 396 DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE_FROM_USERPTR, ivpu_bo_create_from_userptr_ioctl, 0), 397 }; 398 399 static int ivpu_wait_for_ready(struct ivpu_device *vdev) 400 { 401 struct ivpu_ipc_consumer cons; 402 struct ivpu_ipc_hdr ipc_hdr; 403 unsigned long timeout; 404 int ret; 405 406 if (ivpu_test_mode & IVPU_TEST_MODE_FW_TEST) 407 return 0; 408 409 ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG, NULL); 410 411 timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot); 412 while (1) { 413 ivpu_ipc_irq_handler(vdev); 414 ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0); 415 if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout)) 416 break; 417 418 cond_resched(); 419 } 420 421 ivpu_ipc_consumer_del(vdev, &cons); 422 423 if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) { 424 ivpu_err(vdev, "Invalid NPU ready message: 0x%x\n", ipc_hdr.data_addr); 425 return -EIO; 426 } 427 428 if (!ret) 429 ivpu_dbg(vdev, PM, "NPU ready message received successfully\n"); 430 431 return ret; 432 } 433 434 static int ivpu_hw_sched_init(struct ivpu_device *vdev) 435 { 436 int ret = 0; 437 438 if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) { 439 ret = ivpu_jsm_hws_setup_priority_bands(vdev); 440 if (ret) { 441 ivpu_err(vdev, "Failed to enable hw scheduler: %d", ret); 442 return ret; 443 } 444 } 445 446 return ret; 447 } 448 449 /** 450 * ivpu_boot() - Start VPU firmware 451 * @vdev: VPU device 452 * 453 * This function is paired with ivpu_shutdown() but it doesn't power up the 454 * VPU because power up has to be called very early in ivpu_probe(). 455 */ 456 int ivpu_boot(struct ivpu_device *vdev) 457 { 458 int ret; 459 460 drm_WARN_ON(&vdev->drm, atomic_read(&vdev->job_timeout_counter)); 461 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa)); 462 463 ivpu_fw_boot_params_setup(vdev, ivpu_bo_vaddr(vdev->fw->mem_bp)); 464 vdev->fw->last_boot_mode = vdev->fw->next_boot_mode; 465 466 ret = ivpu_hw_boot_fw(vdev); 467 if (ret) { 468 ivpu_err(vdev, "Failed to start the firmware: %d\n", ret); 469 return ret; 470 } 471 472 ret = ivpu_wait_for_ready(vdev); 473 if (ret) { 474 ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret); 475 goto err_diagnose_failure; 476 } 477 ivpu_hw_irq_clear(vdev); 478 enable_irq(vdev->irq); 479 ivpu_hw_irq_enable(vdev); 480 ivpu_ipc_enable(vdev); 481 482 if (!ivpu_fw_is_warm_boot(vdev)) { 483 ret = ivpu_pm_dct_init(vdev); 484 if (ret) 485 goto err_disable_ipc; 486 487 ret = ivpu_hw_sched_init(vdev); 488 if (ret) 489 goto err_disable_ipc; 490 491 ret = ivpu_hw_btrs_cfg_freq_init(vdev); 492 if (ret) 493 goto err_disable_ipc; 494 } 495 496 return 0; 497 498 err_disable_ipc: 499 ivpu_ipc_disable(vdev); 500 ivpu_hw_irq_disable(vdev); 501 disable_irq(vdev->irq); 502 err_diagnose_failure: 503 ivpu_hw_diagnose_failure(vdev); 504 ivpu_mmu_evtq_dump(vdev); 505 ivpu_dev_coredump(vdev); 506 return ret; 507 } 508 509 void ivpu_prepare_for_reset(struct ivpu_device *vdev) 510 { 511 ivpu_hw_irq_disable(vdev); 512 disable_irq(vdev->irq); 513 flush_work(&vdev->irq_ipc_work); 514 flush_work(&vdev->irq_dct_work); 515 flush_work(&vdev->context_abort_work); 516 ivpu_ipc_disable(vdev); 517 ivpu_mmu_disable(vdev); 518 } 519 520 int ivpu_shutdown(struct ivpu_device *vdev) 521 { 522 int ret; 523 524 /* Save PCI state before powering down as it sometimes gets corrupted if NPU hangs */ 525 pci_save_state(to_pci_dev(vdev->drm.dev)); 526 527 ret = ivpu_hw_power_down(vdev); 528 if (ret) 529 ivpu_warn(vdev, "Failed to power down HW: %d\n", ret); 530 531 pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot); 532 533 return ret; 534 } 535 536 static const struct file_operations ivpu_fops = { 537 .owner = THIS_MODULE, 538 DRM_ACCEL_FOPS, 539 #ifdef CONFIG_PROC_FS 540 .show_fdinfo = drm_show_fdinfo, 541 #endif 542 }; 543 544 static int ivpu_gem_prime_handle_to_fd(struct drm_device *dev, struct drm_file *file_priv, 545 u32 handle, u32 flags, int *prime_fd) 546 { 547 struct drm_gem_object *obj; 548 549 obj = drm_gem_object_lookup(file_priv, handle); 550 if (!obj) 551 return -ENOENT; 552 553 if (drm_gem_is_imported(obj)) { 554 /* Do not allow re-exporting */ 555 drm_gem_object_put(obj); 556 return -EOPNOTSUPP; 557 } 558 559 drm_gem_object_put(obj); 560 561 return drm_gem_prime_handle_to_fd(dev, file_priv, handle, flags, prime_fd); 562 } 563 564 static const struct drm_driver driver = { 565 .driver_features = DRIVER_GEM | DRIVER_COMPUTE_ACCEL, 566 567 .open = ivpu_open, 568 .postclose = ivpu_postclose, 569 570 .gem_create_object = ivpu_gem_create_object, 571 .gem_prime_import = ivpu_gem_prime_import, 572 .prime_handle_to_fd = ivpu_gem_prime_handle_to_fd, 573 574 .ioctls = ivpu_drm_ioctls, 575 .num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls), 576 .fops = &ivpu_fops, 577 #ifdef CONFIG_PROC_FS 578 .show_fdinfo = drm_show_memory_stats, 579 #endif 580 581 .name = DRIVER_NAME, 582 .desc = DRIVER_DESC, 583 584 .major = 1, 585 }; 586 587 static int ivpu_irq_init(struct ivpu_device *vdev) 588 { 589 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev); 590 int ret; 591 592 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX); 593 if (ret < 0) { 594 ivpu_err(vdev, "Failed to allocate a MSI IRQ: %d\n", ret); 595 return ret; 596 } 597 598 INIT_WORK(&vdev->irq_ipc_work, ivpu_ipc_irq_work_fn); 599 INIT_WORK(&vdev->irq_dct_work, ivpu_pm_irq_dct_work_fn); 600 INIT_WORK(&vdev->context_abort_work, ivpu_context_abort_work_fn); 601 602 ivpu_irq_handlers_init(vdev); 603 604 vdev->irq = pci_irq_vector(pdev, 0); 605 606 ret = devm_request_irq(vdev->drm.dev, vdev->irq, ivpu_hw_irq_handler, 607 IRQF_NO_AUTOEN, DRIVER_NAME, vdev); 608 if (ret) 609 ivpu_err(vdev, "Failed to request an IRQ %d\n", ret); 610 611 return ret; 612 } 613 614 static int ivpu_pci_init(struct ivpu_device *vdev) 615 { 616 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev); 617 struct resource *bar0 = &pdev->resource[0]; 618 struct resource *bar4 = &pdev->resource[4]; 619 int ret; 620 621 ivpu_dbg(vdev, MISC, "Mapping BAR0 (RegV) %pR\n", bar0); 622 vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0); 623 if (IS_ERR(vdev->regv)) { 624 ivpu_err(vdev, "Failed to map bar 0: %pe\n", vdev->regv); 625 return PTR_ERR(vdev->regv); 626 } 627 628 ivpu_dbg(vdev, MISC, "Mapping BAR4 (RegB) %pR\n", bar4); 629 vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4); 630 if (IS_ERR(vdev->regb)) { 631 ivpu_err(vdev, "Failed to map bar 4: %pe\n", vdev->regb); 632 return PTR_ERR(vdev->regb); 633 } 634 635 ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(vdev->hw->dma_bits)); 636 if (ret) { 637 ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret); 638 return ret; 639 } 640 dma_set_max_seg_size(vdev->drm.dev, UINT_MAX); 641 642 /* Clear any pending errors */ 643 pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f); 644 645 /* NPU does not require 10m D3hot delay */ 646 pdev->d3hot_delay = 0; 647 648 ret = pcim_enable_device(pdev); 649 if (ret) { 650 ivpu_err(vdev, "Failed to enable PCI device: %d\n", ret); 651 return ret; 652 } 653 654 pci_set_master(pdev); 655 656 return 0; 657 } 658 659 static int ivpu_dev_init(struct ivpu_device *vdev) 660 { 661 int ret; 662 663 vdev->hw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->hw), GFP_KERNEL); 664 if (!vdev->hw) 665 return -ENOMEM; 666 667 vdev->mmu = drmm_kzalloc(&vdev->drm, sizeof(*vdev->mmu), GFP_KERNEL); 668 if (!vdev->mmu) 669 return -ENOMEM; 670 671 vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL); 672 if (!vdev->fw) 673 return -ENOMEM; 674 675 vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL); 676 if (!vdev->ipc) 677 return -ENOMEM; 678 679 vdev->pm = drmm_kzalloc(&vdev->drm, sizeof(*vdev->pm), GFP_KERNEL); 680 if (!vdev->pm) 681 return -ENOMEM; 682 683 if (ivpu_hw_ip_gen(vdev) >= IVPU_HW_IP_40XX) 684 vdev->hw->dma_bits = 48; 685 else 686 vdev->hw->dma_bits = 38; 687 688 vdev->platform = IVPU_PLATFORM_INVALID; 689 vdev->context_xa_limit.min = IVPU_USER_CONTEXT_MIN_SSID; 690 vdev->context_xa_limit.max = IVPU_USER_CONTEXT_MAX_SSID; 691 atomic64_set(&vdev->unique_id_counter, 0); 692 atomic_set(&vdev->job_timeout_counter, 0); 693 atomic_set(&vdev->faults_detected, 0); 694 xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ); 695 xa_init_flags(&vdev->submitted_jobs_xa, XA_FLAGS_ALLOC1); 696 xa_init_flags(&vdev->db_xa, XA_FLAGS_ALLOC1); 697 INIT_LIST_HEAD(&vdev->bo_list); 698 hash_init(vdev->user_limits); 699 700 vdev->db_limit.min = IVPU_MIN_DB; 701 vdev->db_limit.max = IVPU_MAX_DB; 702 703 ret = drmm_mutex_init(&vdev->drm, &vdev->context_list_lock); 704 if (ret) 705 goto err_xa_destroy; 706 707 ret = drmm_mutex_init(&vdev->drm, &vdev->user_limits_lock); 708 if (ret) 709 goto err_xa_destroy; 710 711 ret = drmm_mutex_init(&vdev->drm, &vdev->submitted_jobs_lock); 712 if (ret) 713 goto err_xa_destroy; 714 715 ret = drmm_mutex_init(&vdev->drm, &vdev->bo_list_lock); 716 if (ret) 717 goto err_xa_destroy; 718 719 ret = ivpu_pci_init(vdev); 720 if (ret) 721 goto err_xa_destroy; 722 723 ret = ivpu_irq_init(vdev); 724 if (ret) 725 goto err_xa_destroy; 726 727 /* Init basic HW info based on buttress registers which are accessible before power up */ 728 ret = ivpu_hw_init(vdev); 729 if (ret) 730 goto err_xa_destroy; 731 732 /* Power up early so the rest of init code can access VPU registers */ 733 ret = ivpu_hw_power_up(vdev); 734 if (ret) 735 goto err_shutdown; 736 737 ivpu_mmu_global_context_init(vdev); 738 739 ret = ivpu_mmu_init(vdev); 740 if (ret) 741 goto err_mmu_gctx_fini; 742 743 ret = ivpu_mmu_reserved_context_init(vdev); 744 if (ret) 745 goto err_mmu_gctx_fini; 746 747 ret = ivpu_fw_init(vdev); 748 if (ret) 749 goto err_mmu_rctx_fini; 750 751 ret = ivpu_ipc_init(vdev); 752 if (ret) 753 goto err_fw_fini; 754 755 ivpu_pm_init(vdev); 756 757 ret = ivpu_boot(vdev); 758 if (ret) 759 goto err_ipc_fini; 760 761 ivpu_job_done_consumer_init(vdev); 762 ivpu_pm_enable(vdev); 763 764 return 0; 765 766 err_ipc_fini: 767 ivpu_ipc_fini(vdev); 768 err_fw_fini: 769 ivpu_fw_fini(vdev); 770 err_mmu_rctx_fini: 771 ivpu_mmu_reserved_context_fini(vdev); 772 err_mmu_gctx_fini: 773 ivpu_mmu_global_context_fini(vdev); 774 err_shutdown: 775 ivpu_shutdown(vdev); 776 err_xa_destroy: 777 xa_destroy(&vdev->db_xa); 778 xa_destroy(&vdev->submitted_jobs_xa); 779 xa_destroy(&vdev->context_xa); 780 return ret; 781 } 782 783 static void ivpu_bo_unbind_all_user_contexts(struct ivpu_device *vdev) 784 { 785 struct ivpu_file_priv *file_priv; 786 unsigned long ctx_id; 787 788 mutex_lock(&vdev->context_list_lock); 789 790 xa_for_each(&vdev->context_xa, ctx_id, file_priv) 791 file_priv_unbind(vdev, file_priv); 792 793 mutex_unlock(&vdev->context_list_lock); 794 } 795 796 static void ivpu_dev_fini(struct ivpu_device *vdev) 797 { 798 ivpu_jobs_abort_all(vdev); 799 ivpu_pm_disable_recovery(vdev); 800 ivpu_pm_disable(vdev); 801 ivpu_prepare_for_reset(vdev); 802 ivpu_shutdown(vdev); 803 804 ivpu_ms_cleanup_all(vdev); 805 ivpu_job_done_consumer_fini(vdev); 806 ivpu_bo_unbind_all_user_contexts(vdev); 807 808 ivpu_ipc_fini(vdev); 809 ivpu_fw_fini(vdev); 810 ivpu_mmu_reserved_context_fini(vdev); 811 ivpu_mmu_global_context_fini(vdev); 812 813 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->db_xa)); 814 xa_destroy(&vdev->db_xa); 815 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa)); 816 xa_destroy(&vdev->submitted_jobs_xa); 817 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa)); 818 xa_destroy(&vdev->context_xa); 819 } 820 821 static struct pci_device_id ivpu_pci_ids[] = { 822 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) }, 823 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_ARL) }, 824 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_LNL) }, 825 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PTL_P) }, 826 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_WCL) }, 827 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_NVL) }, 828 {} 829 }; 830 MODULE_DEVICE_TABLE(pci, ivpu_pci_ids); 831 832 static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id) 833 { 834 struct ivpu_device *vdev; 835 int ret; 836 837 vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm); 838 if (IS_ERR(vdev)) 839 return PTR_ERR(vdev); 840 841 pci_set_drvdata(pdev, vdev); 842 843 ret = ivpu_dev_init(vdev); 844 if (ret) 845 return ret; 846 847 ivpu_debugfs_init(vdev); 848 ivpu_sysfs_init(vdev); 849 850 ret = drm_dev_register(&vdev->drm, 0); 851 if (ret) { 852 dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret); 853 ivpu_dev_fini(vdev); 854 } 855 856 return ret; 857 } 858 859 static void ivpu_remove(struct pci_dev *pdev) 860 { 861 struct ivpu_device *vdev = pci_get_drvdata(pdev); 862 863 drm_dev_unplug(&vdev->drm); 864 ivpu_dev_fini(vdev); 865 } 866 867 static const struct dev_pm_ops ivpu_drv_pci_pm = { 868 SET_SYSTEM_SLEEP_PM_OPS(ivpu_pm_suspend_cb, ivpu_pm_resume_cb) 869 SET_RUNTIME_PM_OPS(ivpu_pm_runtime_suspend_cb, ivpu_pm_runtime_resume_cb, NULL) 870 }; 871 872 static const struct pci_error_handlers ivpu_drv_pci_err = { 873 .reset_prepare = ivpu_pm_reset_prepare_cb, 874 .reset_done = ivpu_pm_reset_done_cb, 875 }; 876 877 static struct pci_driver ivpu_pci_driver = { 878 .name = KBUILD_MODNAME, 879 .id_table = ivpu_pci_ids, 880 .probe = ivpu_probe, 881 .remove = ivpu_remove, 882 .driver = { 883 .pm = &ivpu_drv_pci_pm, 884 }, 885 .err_handler = &ivpu_drv_pci_err, 886 }; 887 888 module_pci_driver(ivpu_pci_driver); 889 890 MODULE_AUTHOR("Intel Corporation"); 891 MODULE_DESCRIPTION(DRIVER_DESC); 892 MODULE_LICENSE("GPL and additional rights"); 893 MODULE_VERSION(DRIVER_VERSION_STR); 894