1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2020-2023 Intel Corporation
6 #include <linux/firmware.h>
7 #include <linux/module.h>
10 #include <drm/drm_accel.h>
11 #include <drm/drm_file.h>
12 #include <drm/drm_gem.h>
13 #include <drm/drm_ioctl.h>
14 #include <drm/drm_prime.h>
16 #include "vpu_boot_api.h"
23 #include "ivpu_jsm_msg.h"
25 #include "ivpu_mmu_context.h"
28 #ifndef DRIVER_VERSION_STR
29 #define DRIVER_VERSION_STR __stringify(DRM_IVPU_DRIVER_MAJOR) "." \
30 __stringify(DRM_IVPU_DRIVER_MINOR) "."
33 static const struct drm_driver driver;
35 static struct lock_class_key submitted_jobs_xa_lock_class_key;
38 module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
39 MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
42 module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644);
43 MODULE_PARM_DESC(test_mode, "Test mode: 0 - normal operation, 1 - fw unit test, 2 - null hw");
45 u8 ivpu_pll_min_ratio;
46 module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644);
47 MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set VPU frequency");
49 u8 ivpu_pll_max_ratio = U8_MAX;
50 module_param_named(pll_max_ratio, ivpu_pll_max_ratio, byte, 0644);
51 MODULE_PARM_DESC(pll_max_ratio, "Maximum PLL ratio used to set VPU frequency");
53 struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
55 struct ivpu_device *vdev = file_priv->vdev;
57 kref_get(&file_priv->ref);
59 ivpu_dbg(vdev, KREF, "file_priv get: ctx %u refcount %u\n",
60 file_priv->ctx.id, kref_read(&file_priv->ref));
65 struct ivpu_file_priv *ivpu_file_priv_get_by_ctx_id(struct ivpu_device *vdev, unsigned long id)
67 struct ivpu_file_priv *file_priv;
69 xa_lock_irq(&vdev->context_xa);
70 file_priv = xa_load(&vdev->context_xa, id);
71 /* file_priv may still be in context_xa during file_priv_release() */
72 if (file_priv && !kref_get_unless_zero(&file_priv->ref))
74 xa_unlock_irq(&vdev->context_xa);
77 ivpu_dbg(vdev, KREF, "file_priv get by id: ctx %u refcount %u\n",
78 file_priv->ctx.id, kref_read(&file_priv->ref));
83 static void file_priv_release(struct kref *ref)
85 struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
86 struct ivpu_device *vdev = file_priv->vdev;
88 ivpu_dbg(vdev, FILE, "file_priv release: ctx %u\n", file_priv->ctx.id);
90 ivpu_cmdq_release_all(file_priv);
91 ivpu_bo_remove_all_bos_from_context(&file_priv->ctx);
92 ivpu_jsm_context_release(vdev, file_priv->ctx.id);
93 ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
94 drm_WARN_ON(&vdev->drm, xa_erase_irq(&vdev->context_xa, file_priv->ctx.id) != file_priv);
95 mutex_destroy(&file_priv->lock);
99 void ivpu_file_priv_put(struct ivpu_file_priv **link)
101 struct ivpu_file_priv *file_priv = *link;
102 struct ivpu_device *vdev = file_priv->vdev;
104 drm_WARN_ON(&vdev->drm, !file_priv);
106 ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n",
107 file_priv->ctx.id, kref_read(&file_priv->ref));
110 kref_put(&file_priv->ref, file_priv_release);
113 static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
115 struct ivpu_file_priv *file_priv = file->driver_priv;
116 struct ivpu_device *vdev = file_priv->vdev;
117 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
118 struct drm_ivpu_param *args = data;
122 if (!drm_dev_enter(dev, &idx))
125 switch (args->param) {
126 case DRM_IVPU_PARAM_DEVICE_ID:
127 args->value = pdev->device;
129 case DRM_IVPU_PARAM_DEVICE_REVISION:
130 args->value = pdev->revision;
132 case DRM_IVPU_PARAM_PLATFORM_TYPE:
133 args->value = vdev->platform;
135 case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
136 args->value = ivpu_hw_reg_pll_freq_get(vdev);
138 case DRM_IVPU_PARAM_NUM_CONTEXTS:
139 args->value = ivpu_get_context_count(vdev);
141 case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
142 args->value = vdev->hw->ranges.user_low.start;
144 case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
145 args->value = file_priv->priority;
147 case DRM_IVPU_PARAM_CONTEXT_ID:
148 args->value = file_priv->ctx.id;
150 case DRM_IVPU_PARAM_FW_API_VERSION:
151 if (args->index < VPU_FW_API_VER_NUM) {
152 struct vpu_firmware_header *fw_hdr;
154 fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data;
155 args->value = fw_hdr->api_version[args->index];
160 case DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
161 ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value);
163 case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
164 args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter);
166 case DRM_IVPU_PARAM_TILE_CONFIG:
167 args->value = vdev->hw->tile_fuse;
169 case DRM_IVPU_PARAM_SKU:
170 args->value = vdev->hw->sku;
181 static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
183 struct ivpu_file_priv *file_priv = file->driver_priv;
184 struct drm_ivpu_param *args = data;
187 switch (args->param) {
188 case DRM_IVPU_PARAM_CONTEXT_PRIORITY:
189 if (args->value <= DRM_IVPU_CONTEXT_PRIORITY_REALTIME)
190 file_priv->priority = args->value;
201 static int ivpu_open(struct drm_device *dev, struct drm_file *file)
203 struct ivpu_device *vdev = to_ivpu_device(dev);
204 struct ivpu_file_priv *file_priv;
209 ret = xa_alloc_irq(&vdev->context_xa, &ctx_id, NULL, vdev->context_xa_limit, GFP_KERNEL);
211 ivpu_err(vdev, "Failed to allocate context id: %d\n", ret);
215 file_priv = kzalloc(sizeof(*file_priv), GFP_KERNEL);
221 file_priv->vdev = vdev;
222 file_priv->priority = DRM_IVPU_CONTEXT_PRIORITY_NORMAL;
223 kref_init(&file_priv->ref);
224 mutex_init(&file_priv->lock);
226 ret = ivpu_mmu_user_context_init(vdev, &file_priv->ctx, ctx_id);
228 goto err_mutex_destroy;
230 old = xa_store_irq(&vdev->context_xa, ctx_id, file_priv, GFP_KERNEL);
231 if (xa_is_err(old)) {
233 ivpu_err(vdev, "Failed to store context %u: %d\n", ctx_id, ret);
237 ivpu_dbg(vdev, FILE, "file_priv create: ctx %u process %s pid %d\n",
238 ctx_id, current->comm, task_pid_nr(current));
240 file->driver_priv = file_priv;
244 ivpu_mmu_user_context_fini(vdev, &file_priv->ctx);
246 mutex_destroy(&file_priv->lock);
249 xa_erase_irq(&vdev->context_xa, ctx_id);
253 static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
255 struct ivpu_file_priv *file_priv = file->driver_priv;
256 struct ivpu_device *vdev = to_ivpu_device(dev);
258 ivpu_dbg(vdev, FILE, "file_priv close: ctx %u process %s pid %d\n",
259 file_priv->ctx.id, current->comm, task_pid_nr(current));
261 ivpu_file_priv_put(&file_priv);
264 static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
265 DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0),
266 DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0),
267 DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
268 DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
269 DRM_IOCTL_DEF_DRV(IVPU_SUBMIT, ivpu_submit_ioctl, 0),
270 DRM_IOCTL_DEF_DRV(IVPU_BO_WAIT, ivpu_bo_wait_ioctl, 0),
273 static int ivpu_wait_for_ready(struct ivpu_device *vdev)
275 struct ivpu_ipc_consumer cons;
276 struct ivpu_ipc_hdr ipc_hdr;
277 unsigned long timeout;
280 if (ivpu_test_mode == IVPU_TEST_MODE_FW_TEST)
283 ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG);
285 timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot);
287 ret = ivpu_ipc_irq_handler(vdev);
290 ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0);
291 if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout))
297 ivpu_ipc_consumer_del(vdev, &cons);
299 if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) {
300 ivpu_err(vdev, "Invalid VPU ready message: 0x%x\n",
306 ivpu_info(vdev, "VPU ready message received successfully\n");
308 ivpu_hw_diagnose_failure(vdev);
314 * ivpu_boot() - Start VPU firmware
317 * This function is paired with ivpu_shutdown() but it doesn't power up the
318 * VPU because power up has to be called very early in ivpu_probe().
320 int ivpu_boot(struct ivpu_device *vdev)
324 /* Update boot params located at first 4KB of FW memory */
325 ivpu_fw_boot_params_setup(vdev, vdev->fw->mem->kvaddr);
327 ret = ivpu_hw_boot_fw(vdev);
329 ivpu_err(vdev, "Failed to start the firmware: %d\n", ret);
333 ret = ivpu_wait_for_ready(vdev);
335 ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
339 ivpu_hw_irq_clear(vdev);
340 enable_irq(vdev->irq);
341 ivpu_hw_irq_enable(vdev);
342 ivpu_ipc_enable(vdev);
346 int ivpu_shutdown(struct ivpu_device *vdev)
350 ivpu_hw_irq_disable(vdev);
351 disable_irq(vdev->irq);
352 ivpu_ipc_disable(vdev);
353 ivpu_mmu_disable(vdev);
355 ret = ivpu_hw_power_down(vdev);
357 ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
362 static const struct file_operations ivpu_fops = {
363 .owner = THIS_MODULE,
367 static const struct drm_driver driver = {
368 .driver_features = DRIVER_GEM | DRIVER_COMPUTE_ACCEL,
371 .postclose = ivpu_postclose,
372 .prime_handle_to_fd = drm_gem_prime_handle_to_fd,
373 .prime_fd_to_handle = drm_gem_prime_fd_to_handle,
374 .gem_prime_import = ivpu_gem_prime_import,
375 .gem_prime_mmap = drm_gem_prime_mmap,
377 .ioctls = ivpu_drm_ioctls,
378 .num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
384 .major = DRM_IVPU_DRIVER_MAJOR,
385 .minor = DRM_IVPU_DRIVER_MINOR,
388 static int ivpu_irq_init(struct ivpu_device *vdev)
390 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
393 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
395 ivpu_err(vdev, "Failed to allocate a MSI IRQ: %d\n", ret);
399 vdev->irq = pci_irq_vector(pdev, 0);
401 ret = devm_request_irq(vdev->drm.dev, vdev->irq, vdev->hw->ops->irq_handler,
402 IRQF_NO_AUTOEN, DRIVER_NAME, vdev);
404 ivpu_err(vdev, "Failed to request an IRQ %d\n", ret);
409 static int ivpu_pci_init(struct ivpu_device *vdev)
411 struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
412 struct resource *bar0 = &pdev->resource[0];
413 struct resource *bar4 = &pdev->resource[4];
416 ivpu_dbg(vdev, MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
417 vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
418 if (IS_ERR(vdev->regv)) {
419 ivpu_err(vdev, "Failed to map bar 0: %pe\n", vdev->regv);
420 return PTR_ERR(vdev->regv);
423 ivpu_dbg(vdev, MISC, "Mapping BAR4 (RegB) %pR\n", bar4);
424 vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4);
425 if (IS_ERR(vdev->regb)) {
426 ivpu_err(vdev, "Failed to map bar 4: %pe\n", vdev->regb);
427 return PTR_ERR(vdev->regb);
430 ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(38));
432 ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret);
435 dma_set_max_seg_size(vdev->drm.dev, UINT_MAX);
437 /* Clear any pending errors */
438 pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f);
440 ret = pcim_enable_device(pdev);
442 ivpu_err(vdev, "Failed to enable PCI device: %d\n", ret);
446 pci_set_master(pdev);
451 static int ivpu_dev_init(struct ivpu_device *vdev)
455 vdev->hw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->hw), GFP_KERNEL);
459 vdev->mmu = drmm_kzalloc(&vdev->drm, sizeof(*vdev->mmu), GFP_KERNEL);
463 vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL);
467 vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
471 vdev->pm = drmm_kzalloc(&vdev->drm, sizeof(*vdev->pm), GFP_KERNEL);
475 vdev->hw->ops = &ivpu_hw_mtl_ops;
476 vdev->platform = IVPU_PLATFORM_INVALID;
477 vdev->context_xa_limit.min = IVPU_USER_CONTEXT_MIN_SSID;
478 vdev->context_xa_limit.max = IVPU_USER_CONTEXT_MAX_SSID;
479 atomic64_set(&vdev->unique_id_counter, 0);
480 xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC);
481 xa_init_flags(&vdev->submitted_jobs_xa, XA_FLAGS_ALLOC1);
482 lockdep_set_class(&vdev->submitted_jobs_xa.xa_lock, &submitted_jobs_xa_lock_class_key);
484 ret = ivpu_pci_init(vdev);
486 ivpu_err(vdev, "Failed to initialize PCI device: %d\n", ret);
490 ret = ivpu_irq_init(vdev);
492 ivpu_err(vdev, "Failed to initialize IRQs: %d\n", ret);
496 /* Init basic HW info based on buttress registers which are accessible before power up */
497 ret = ivpu_hw_info_init(vdev);
499 ivpu_err(vdev, "Failed to initialize HW info: %d\n", ret);
503 /* Power up early so the rest of init code can access VPU registers */
504 ret = ivpu_hw_power_up(vdev);
506 ivpu_err(vdev, "Failed to power up HW: %d\n", ret);
510 ret = ivpu_mmu_global_context_init(vdev);
512 ivpu_err(vdev, "Failed to initialize global MMU context: %d\n", ret);
516 ret = ivpu_mmu_init(vdev);
518 ivpu_err(vdev, "Failed to initialize MMU device: %d\n", ret);
519 goto err_mmu_gctx_fini;
522 ret = ivpu_fw_init(vdev);
524 ivpu_err(vdev, "Failed to initialize firmware: %d\n", ret);
525 goto err_mmu_gctx_fini;
528 ret = ivpu_ipc_init(vdev);
530 ivpu_err(vdev, "Failed to initialize IPC: %d\n", ret);
534 ret = ivpu_pm_init(vdev);
536 ivpu_err(vdev, "Failed to initialize PM: %d\n", ret);
540 ret = ivpu_job_done_thread_init(vdev);
542 ivpu_err(vdev, "Failed to initialize job done thread: %d\n", ret);
546 ret = ivpu_fw_load(vdev);
548 ivpu_err(vdev, "Failed to load firmware: %d\n", ret);
549 goto err_job_done_thread_fini;
552 ret = ivpu_boot(vdev);
554 ivpu_err(vdev, "Failed to boot: %d\n", ret);
555 goto err_job_done_thread_fini;
558 ivpu_pm_enable(vdev);
562 err_job_done_thread_fini:
563 ivpu_job_done_thread_fini(vdev);
569 ivpu_mmu_global_context_fini(vdev);
571 ivpu_hw_power_down(vdev);
572 if (IVPU_WA(d3hot_after_power_off))
573 pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
575 xa_destroy(&vdev->submitted_jobs_xa);
576 xa_destroy(&vdev->context_xa);
580 static void ivpu_dev_fini(struct ivpu_device *vdev)
582 ivpu_pm_disable(vdev);
584 if (IVPU_WA(d3hot_after_power_off))
585 pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
586 ivpu_job_done_thread_fini(vdev);
587 ivpu_pm_cancel_recovery(vdev);
591 ivpu_mmu_global_context_fini(vdev);
593 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
594 xa_destroy(&vdev->submitted_jobs_xa);
595 drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
596 xa_destroy(&vdev->context_xa);
599 static struct pci_device_id ivpu_pci_ids[] = {
600 { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },
603 MODULE_DEVICE_TABLE(pci, ivpu_pci_ids);
605 static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
607 struct ivpu_device *vdev;
610 vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm);
612 return PTR_ERR(vdev);
614 pci_set_drvdata(pdev, vdev);
616 ret = ivpu_dev_init(vdev);
618 dev_err(&pdev->dev, "Failed to initialize VPU device: %d\n", ret);
622 ret = drm_dev_register(&vdev->drm, 0);
624 dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret);
631 static void ivpu_remove(struct pci_dev *pdev)
633 struct ivpu_device *vdev = pci_get_drvdata(pdev);
635 drm_dev_unplug(&vdev->drm);
639 static const struct dev_pm_ops ivpu_drv_pci_pm = {
640 SET_SYSTEM_SLEEP_PM_OPS(ivpu_pm_suspend_cb, ivpu_pm_resume_cb)
641 SET_RUNTIME_PM_OPS(ivpu_pm_runtime_suspend_cb, ivpu_pm_runtime_resume_cb, NULL)
644 static const struct pci_error_handlers ivpu_drv_pci_err = {
645 .reset_prepare = ivpu_pm_reset_prepare_cb,
646 .reset_done = ivpu_pm_reset_done_cb,
649 static struct pci_driver ivpu_pci_driver = {
650 .name = KBUILD_MODNAME,
651 .id_table = ivpu_pci_ids,
653 .remove = ivpu_remove,
655 .pm = &ivpu_drv_pci_pm,
657 .err_handler = &ivpu_drv_pci_err,
660 module_pci_driver(ivpu_pci_driver);
662 MODULE_AUTHOR("Intel Corporation");
663 MODULE_DESCRIPTION(DRIVER_DESC);
664 MODULE_LICENSE("GPL and additional rights");
665 MODULE_VERSION(DRIVER_VERSION_STR);