ivpu_drv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2020-2025 Intel Corporation
  4. */
  5. #include <linux/firmware.h>
  6. #include <linux/module.h>
  7. #include <linux/pci.h>
  8. #include <linux/pm_runtime.h>
  9. #include <linux/workqueue.h>
  10. #include <generated/utsrelease.h>
  11. #include <drm/drm_accel.h>
  12. #include <drm/drm_file.h>
  13. #include <drm/drm_gem.h>
  14. #include <drm/drm_ioctl.h>
  15. #include <drm/drm_prime.h>
  16. #include "ivpu_coredump.h"
  17. #include "ivpu_debugfs.h"
  18. #include "ivpu_drv.h"
  19. #include "ivpu_fw.h"
  20. #include "ivpu_fw_log.h"
  21. #include "ivpu_gem.h"
  22. #include "ivpu_hw.h"
  23. #include "ivpu_ipc.h"
  24. #include "ivpu_job.h"
  25. #include "ivpu_jsm_msg.h"
  26. #include "ivpu_mmu.h"
  27. #include "ivpu_mmu_context.h"
  28. #include "ivpu_ms.h"
  29. #include "ivpu_pm.h"
  30. #include "ivpu_sysfs.h"
  31. #include "vpu_boot_api.h"
  32. #ifndef DRIVER_VERSION_STR
  33. #define DRIVER_VERSION_STR "1.0.0 " UTS_RELEASE
  34. #endif
  35. int ivpu_dbg_mask;
  36. module_param_named(dbg_mask, ivpu_dbg_mask, int, 0644);
  37. MODULE_PARM_DESC(dbg_mask, "Driver debug mask. See IVPU_DBG_* macros.");
  38. int ivpu_test_mode;
  39. #if IS_ENABLED(CONFIG_DRM_ACCEL_IVPU_DEBUG)
  40. module_param_named_unsafe(test_mode, ivpu_test_mode, int, 0644);
  41. MODULE_PARM_DESC(test_mode, "Test mode mask. See IVPU_TEST_MODE_* macros.");
  42. #endif
  43. u8 ivpu_pll_min_ratio;
  44. module_param_named(pll_min_ratio, ivpu_pll_min_ratio, byte, 0644);
  45. MODULE_PARM_DESC(pll_min_ratio, "Minimum PLL ratio used to set NPU frequency");
  46. u8 ivpu_pll_max_ratio = U8_MAX;
  47. module_param_named(pll_max_ratio, ivpu_pll_max_ratio, byte, 0644);
  48. MODULE_PARM_DESC(pll_max_ratio, "Maximum PLL ratio used to set NPU frequency");
  49. int ivpu_sched_mode = IVPU_SCHED_MODE_AUTO;
  50. module_param_named(sched_mode, ivpu_sched_mode, int, 0444);
  51. MODULE_PARM_DESC(sched_mode, "Scheduler mode: -1 - Use default scheduler, 0 - Use OS scheduler (supported on 27XX - 50XX), 1 - Use HW scheduler");
  52. bool ivpu_disable_mmu_cont_pages;
  53. module_param_named(disable_mmu_cont_pages, ivpu_disable_mmu_cont_pages, bool, 0444);
  54. MODULE_PARM_DESC(disable_mmu_cont_pages, "Disable MMU contiguous pages optimization");
  55. bool ivpu_force_snoop;
  56. module_param_named(force_snoop, ivpu_force_snoop, bool, 0444);
  57. MODULE_PARM_DESC(force_snoop, "Force snooping for NPU host memory access");
  58. struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv)
  59. {
  60. struct ivpu_device *vdev = file_priv->vdev;
  61. kref_get(&file_priv->ref);
  62. ivpu_dbg(vdev, KREF, "file_priv get: ctx %u refcount %u\n",
  63. file_priv->ctx.id, kref_read(&file_priv->ref));
  64. return file_priv;
  65. }
  66. static void file_priv_unbind(struct ivpu_device *vdev, struct ivpu_file_priv *file_priv)
  67. {
  68. mutex_lock(&file_priv->lock);
  69. if (file_priv->bound) {
  70. ivpu_dbg(vdev, FILE, "file_priv unbind: ctx %u\n", file_priv->ctx.id);
  71. ivpu_cmdq_release_all_locked(file_priv);
  72. ivpu_bo_unbind_all_bos_from_context(vdev, &file_priv->ctx);
  73. ivpu_mmu_context_fini(vdev, &file_priv->ctx);
  74. file_priv->bound = false;
  75. drm_WARN_ON(&vdev->drm, !xa_erase_irq(&vdev->context_xa, file_priv->ctx.id));
  76. }
  77. mutex_unlock(&file_priv->lock);
  78. }
  79. static void file_priv_release(struct kref *ref)
  80. {
  81. struct ivpu_file_priv *file_priv = container_of(ref, struct ivpu_file_priv, ref);
  82. struct ivpu_device *vdev = file_priv->vdev;
  83. ivpu_dbg(vdev, FILE, "file_priv release: ctx %u bound %d\n",
  84. file_priv->ctx.id, (bool)file_priv->bound);
  85. pm_runtime_get_sync(vdev->drm.dev);
  86. mutex_lock(&vdev->context_list_lock);
  87. file_priv_unbind(vdev, file_priv);
  88. drm_WARN_ON(&vdev->drm, !xa_empty(&file_priv->cmdq_xa));
  89. xa_destroy(&file_priv->cmdq_xa);
  90. mutex_unlock(&vdev->context_list_lock);
  91. pm_runtime_put_autosuspend(vdev->drm.dev);
  92. mutex_destroy(&file_priv->ms_lock);
  93. mutex_destroy(&file_priv->lock);
  94. kfree(file_priv);
  95. }
  96. void ivpu_file_priv_put(struct ivpu_file_priv **link)
  97. {
  98. struct ivpu_file_priv *file_priv = *link;
  99. struct ivpu_device *vdev = file_priv->vdev;
  100. ivpu_dbg(vdev, KREF, "file_priv put: ctx %u refcount %u\n",
  101. file_priv->ctx.id, kref_read(&file_priv->ref));
  102. *link = NULL;
  103. kref_put(&file_priv->ref, file_priv_release);
  104. }
  105. bool ivpu_is_capable(struct ivpu_device *vdev, u32 capability)
  106. {
  107. switch (capability) {
  108. case DRM_IVPU_CAP_METRIC_STREAMER:
  109. return true;
  110. case DRM_IVPU_CAP_DMA_MEMORY_RANGE:
  111. return true;
  112. case DRM_IVPU_CAP_BO_CREATE_FROM_USERPTR:
  113. return true;
  114. case DRM_IVPU_CAP_MANAGE_CMDQ:
  115. return vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW;
  116. default:
  117. return false;
  118. }
  119. }
  120. static int ivpu_get_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  121. {
  122. struct ivpu_file_priv *file_priv = file->driver_priv;
  123. struct ivpu_device *vdev = file_priv->vdev;
  124. struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  125. struct drm_ivpu_param *args = data;
  126. int ret = 0;
  127. int idx;
  128. if (!drm_dev_enter(dev, &idx))
  129. return -ENODEV;
  130. switch (args->param) {
  131. case DRM_IVPU_PARAM_DEVICE_ID:
  132. args->value = pdev->device;
  133. break;
  134. case DRM_IVPU_PARAM_DEVICE_REVISION:
  135. args->value = pdev->revision;
  136. break;
  137. case DRM_IVPU_PARAM_PLATFORM_TYPE:
  138. args->value = vdev->platform;
  139. break;
  140. case DRM_IVPU_PARAM_CORE_CLOCK_RATE:
  141. args->value = ivpu_hw_dpu_max_freq_get(vdev);
  142. break;
  143. case DRM_IVPU_PARAM_NUM_CONTEXTS:
  144. args->value = ivpu_get_context_count(vdev);
  145. break;
  146. case DRM_IVPU_PARAM_CONTEXT_BASE_ADDRESS:
  147. args->value = vdev->hw->ranges.user.start;
  148. break;
  149. case DRM_IVPU_PARAM_CONTEXT_ID:
  150. args->value = file_priv->ctx.id;
  151. break;
  152. case DRM_IVPU_PARAM_FW_API_VERSION:
  153. if (args->index < VPU_FW_API_VER_NUM) {
  154. struct vpu_firmware_header *fw_hdr;
  155. fw_hdr = (struct vpu_firmware_header *)vdev->fw->file->data;
  156. args->value = fw_hdr->api_version[args->index];
  157. } else {
  158. ret = -EINVAL;
  159. }
  160. break;
  161. case DRM_IVPU_PARAM_ENGINE_HEARTBEAT:
  162. ret = ivpu_jsm_get_heartbeat(vdev, args->index, &args->value);
  163. break;
  164. case DRM_IVPU_PARAM_UNIQUE_INFERENCE_ID:
  165. args->value = (u64)atomic64_inc_return(&vdev->unique_id_counter);
  166. break;
  167. case DRM_IVPU_PARAM_TILE_CONFIG:
  168. args->value = vdev->hw->tile_fuse;
  169. break;
  170. case DRM_IVPU_PARAM_SKU:
  171. args->value = vdev->hw->sku;
  172. break;
  173. case DRM_IVPU_PARAM_CAPABILITIES:
  174. args->value = ivpu_is_capable(vdev, args->index);
  175. break;
  176. case DRM_IVPU_PARAM_PREEMPT_BUFFER_SIZE:
  177. args->value = ivpu_fw_preempt_buf_size(vdev);
  178. break;
  179. default:
  180. ret = -EINVAL;
  181. break;
  182. }
  183. drm_dev_exit(idx);
  184. return ret;
  185. }
  186. static int ivpu_set_param_ioctl(struct drm_device *dev, void *data, struct drm_file *file)
  187. {
  188. struct drm_ivpu_param *args = data;
  189. int ret = 0;
  190. switch (args->param) {
  191. default:
  192. ret = -EINVAL;
  193. }
  194. return ret;
  195. }
  196. static int ivpu_open(struct drm_device *dev, struct drm_file *file)
  197. {
  198. struct ivpu_device *vdev = to_ivpu_device(dev);
  199. struct ivpu_file_priv *file_priv;
  200. u32 ctx_id;
  201. int idx, ret;
  202. if (!drm_dev_enter(dev, &idx))
  203. return -ENODEV;
  204. file_priv = kzalloc_obj(*file_priv);
  205. if (!file_priv) {
  206. ret = -ENOMEM;
  207. goto err_dev_exit;
  208. }
  209. INIT_LIST_HEAD(&file_priv->ms_instance_list);
  210. file_priv->vdev = vdev;
  211. file_priv->bound = true;
  212. kref_init(&file_priv->ref);
  213. mutex_init(&file_priv->lock);
  214. mutex_init(&file_priv->ms_lock);
  215. mutex_lock(&vdev->context_list_lock);
  216. ret = xa_alloc_irq(&vdev->context_xa, &ctx_id, file_priv,
  217. vdev->context_xa_limit, GFP_KERNEL);
  218. if (ret) {
  219. ivpu_err(vdev, "Failed to allocate context id: %d\n", ret);
  220. goto err_unlock;
  221. }
  222. ivpu_mmu_context_init(vdev, &file_priv->ctx, ctx_id);
  223. file_priv->job_limit.min = FIELD_PREP(IVPU_JOB_ID_CONTEXT_MASK, (file_priv->ctx.id - 1));
  224. file_priv->job_limit.max = file_priv->job_limit.min | IVPU_JOB_ID_JOB_MASK;
  225. xa_init_flags(&file_priv->cmdq_xa, XA_FLAGS_ALLOC1);
  226. file_priv->cmdq_limit.min = IVPU_CMDQ_MIN_ID;
  227. file_priv->cmdq_limit.max = IVPU_CMDQ_MAX_ID;
  228. mutex_unlock(&vdev->context_list_lock);
  229. drm_dev_exit(idx);
  230. file->driver_priv = file_priv;
  231. ivpu_dbg(vdev, FILE, "file_priv create: ctx %u process %s pid %d\n",
  232. ctx_id, current->comm, task_pid_nr(current));
  233. return 0;
  234. err_unlock:
  235. mutex_unlock(&vdev->context_list_lock);
  236. mutex_destroy(&file_priv->ms_lock);
  237. mutex_destroy(&file_priv->lock);
  238. kfree(file_priv);
  239. err_dev_exit:
  240. drm_dev_exit(idx);
  241. return ret;
  242. }
  243. static void ivpu_postclose(struct drm_device *dev, struct drm_file *file)
  244. {
  245. struct ivpu_file_priv *file_priv = file->driver_priv;
  246. struct ivpu_device *vdev = to_ivpu_device(dev);
  247. ivpu_dbg(vdev, FILE, "file_priv close: ctx %u process %s pid %d\n",
  248. file_priv->ctx.id, current->comm, task_pid_nr(current));
  249. ivpu_ms_cleanup(file_priv);
  250. ivpu_file_priv_put(&file_priv);
  251. }
  252. static const struct drm_ioctl_desc ivpu_drm_ioctls[] = {
  253. DRM_IOCTL_DEF_DRV(IVPU_GET_PARAM, ivpu_get_param_ioctl, 0),
  254. DRM_IOCTL_DEF_DRV(IVPU_SET_PARAM, ivpu_set_param_ioctl, 0),
  255. DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE, ivpu_bo_create_ioctl, 0),
  256. DRM_IOCTL_DEF_DRV(IVPU_BO_INFO, ivpu_bo_info_ioctl, 0),
  257. DRM_IOCTL_DEF_DRV(IVPU_SUBMIT, ivpu_submit_ioctl, 0),
  258. DRM_IOCTL_DEF_DRV(IVPU_BO_WAIT, ivpu_bo_wait_ioctl, 0),
  259. DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_START, ivpu_ms_start_ioctl, 0),
  260. DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_GET_DATA, ivpu_ms_get_data_ioctl, 0),
  261. DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_STOP, ivpu_ms_stop_ioctl, 0),
  262. DRM_IOCTL_DEF_DRV(IVPU_METRIC_STREAMER_GET_INFO, ivpu_ms_get_info_ioctl, 0),
  263. DRM_IOCTL_DEF_DRV(IVPU_CMDQ_CREATE, ivpu_cmdq_create_ioctl, 0),
  264. DRM_IOCTL_DEF_DRV(IVPU_CMDQ_DESTROY, ivpu_cmdq_destroy_ioctl, 0),
  265. DRM_IOCTL_DEF_DRV(IVPU_CMDQ_SUBMIT, ivpu_cmdq_submit_ioctl, 0),
  266. DRM_IOCTL_DEF_DRV(IVPU_BO_CREATE_FROM_USERPTR, ivpu_bo_create_from_userptr_ioctl, 0),
  267. };
  268. static int ivpu_wait_for_ready(struct ivpu_device *vdev)
  269. {
  270. struct ivpu_ipc_consumer cons;
  271. struct ivpu_ipc_hdr ipc_hdr;
  272. unsigned long timeout;
  273. int ret;
  274. if (ivpu_test_mode & IVPU_TEST_MODE_FW_TEST)
  275. return 0;
  276. ivpu_ipc_consumer_add(vdev, &cons, IVPU_IPC_CHAN_BOOT_MSG, NULL);
  277. timeout = jiffies + msecs_to_jiffies(vdev->timeout.boot);
  278. while (1) {
  279. ivpu_ipc_irq_handler(vdev);
  280. ret = ivpu_ipc_receive(vdev, &cons, &ipc_hdr, NULL, 0);
  281. if (ret != -ETIMEDOUT || time_after_eq(jiffies, timeout))
  282. break;
  283. cond_resched();
  284. }
  285. ivpu_ipc_consumer_del(vdev, &cons);
  286. if (!ret && ipc_hdr.data_addr != IVPU_IPC_BOOT_MSG_DATA_ADDR) {
  287. ivpu_err(vdev, "Invalid NPU ready message: 0x%x\n",
  288. ipc_hdr.data_addr);
  289. return -EIO;
  290. }
  291. if (!ret)
  292. ivpu_dbg(vdev, PM, "NPU ready message received successfully\n");
  293. return ret;
  294. }
  295. static int ivpu_hw_sched_init(struct ivpu_device *vdev)
  296. {
  297. int ret = 0;
  298. if (vdev->fw->sched_mode == VPU_SCHEDULING_MODE_HW) {
  299. ret = ivpu_jsm_hws_setup_priority_bands(vdev);
  300. if (ret) {
  301. ivpu_err(vdev, "Failed to enable hw scheduler: %d", ret);
  302. return ret;
  303. }
  304. }
  305. return ret;
  306. }
  307. /**
  308. * ivpu_boot() - Start VPU firmware
  309. * @vdev: VPU device
  310. *
  311. * This function is paired with ivpu_shutdown() but it doesn't power up the
  312. * VPU because power up has to be called very early in ivpu_probe().
  313. */
  314. int ivpu_boot(struct ivpu_device *vdev)
  315. {
  316. int ret;
  317. drm_WARN_ON(&vdev->drm, atomic_read(&vdev->job_timeout_counter));
  318. drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
  319. ivpu_fw_boot_params_setup(vdev, ivpu_bo_vaddr(vdev->fw->mem_bp));
  320. vdev->fw->last_boot_mode = vdev->fw->next_boot_mode;
  321. ret = ivpu_hw_boot_fw(vdev);
  322. if (ret) {
  323. ivpu_err(vdev, "Failed to start the firmware: %d\n", ret);
  324. return ret;
  325. }
  326. ret = ivpu_wait_for_ready(vdev);
  327. if (ret) {
  328. ivpu_err(vdev, "Failed to boot the firmware: %d\n", ret);
  329. goto err_diagnose_failure;
  330. }
  331. ivpu_hw_irq_clear(vdev);
  332. enable_irq(vdev->irq);
  333. ivpu_hw_irq_enable(vdev);
  334. ivpu_ipc_enable(vdev);
  335. if (!ivpu_fw_is_warm_boot(vdev)) {
  336. ret = ivpu_pm_dct_init(vdev);
  337. if (ret)
  338. goto err_disable_ipc;
  339. ret = ivpu_hw_sched_init(vdev);
  340. if (ret)
  341. goto err_disable_ipc;
  342. }
  343. return 0;
  344. err_disable_ipc:
  345. ivpu_ipc_disable(vdev);
  346. ivpu_hw_irq_disable(vdev);
  347. disable_irq(vdev->irq);
  348. err_diagnose_failure:
  349. ivpu_hw_diagnose_failure(vdev);
  350. ivpu_mmu_evtq_dump(vdev);
  351. ivpu_dev_coredump(vdev);
  352. return ret;
  353. }
  354. void ivpu_prepare_for_reset(struct ivpu_device *vdev)
  355. {
  356. ivpu_hw_irq_disable(vdev);
  357. disable_irq(vdev->irq);
  358. flush_work(&vdev->irq_ipc_work);
  359. flush_work(&vdev->irq_dct_work);
  360. flush_work(&vdev->context_abort_work);
  361. ivpu_ipc_disable(vdev);
  362. ivpu_mmu_disable(vdev);
  363. }
  364. int ivpu_shutdown(struct ivpu_device *vdev)
  365. {
  366. int ret;
  367. /* Save PCI state before powering down as it sometimes gets corrupted if NPU hangs */
  368. pci_save_state(to_pci_dev(vdev->drm.dev));
  369. ret = ivpu_hw_power_down(vdev);
  370. if (ret)
  371. ivpu_warn(vdev, "Failed to power down HW: %d\n", ret);
  372. pci_set_power_state(to_pci_dev(vdev->drm.dev), PCI_D3hot);
  373. return ret;
  374. }
  375. static const struct file_operations ivpu_fops = {
  376. .owner = THIS_MODULE,
  377. DRM_ACCEL_FOPS,
  378. #ifdef CONFIG_PROC_FS
  379. .show_fdinfo = drm_show_fdinfo,
  380. #endif
  381. };
  382. static const struct drm_driver driver = {
  383. .driver_features = DRIVER_GEM | DRIVER_COMPUTE_ACCEL,
  384. .open = ivpu_open,
  385. .postclose = ivpu_postclose,
  386. .gem_create_object = ivpu_gem_create_object,
  387. .gem_prime_import = ivpu_gem_prime_import,
  388. .ioctls = ivpu_drm_ioctls,
  389. .num_ioctls = ARRAY_SIZE(ivpu_drm_ioctls),
  390. .fops = &ivpu_fops,
  391. #ifdef CONFIG_PROC_FS
  392. .show_fdinfo = drm_show_memory_stats,
  393. #endif
  394. .name = DRIVER_NAME,
  395. .desc = DRIVER_DESC,
  396. .major = 1,
  397. };
  398. static int ivpu_irq_init(struct ivpu_device *vdev)
  399. {
  400. struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  401. int ret;
  402. ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI | PCI_IRQ_MSIX);
  403. if (ret < 0) {
  404. ivpu_err(vdev, "Failed to allocate a MSI IRQ: %d\n", ret);
  405. return ret;
  406. }
  407. INIT_WORK(&vdev->irq_ipc_work, ivpu_ipc_irq_work_fn);
  408. INIT_WORK(&vdev->irq_dct_work, ivpu_pm_irq_dct_work_fn);
  409. INIT_WORK(&vdev->context_abort_work, ivpu_context_abort_work_fn);
  410. ivpu_irq_handlers_init(vdev);
  411. vdev->irq = pci_irq_vector(pdev, 0);
  412. ret = devm_request_irq(vdev->drm.dev, vdev->irq, ivpu_hw_irq_handler,
  413. IRQF_NO_AUTOEN, DRIVER_NAME, vdev);
  414. if (ret)
  415. ivpu_err(vdev, "Failed to request an IRQ %d\n", ret);
  416. return ret;
  417. }
  418. static int ivpu_pci_init(struct ivpu_device *vdev)
  419. {
  420. struct pci_dev *pdev = to_pci_dev(vdev->drm.dev);
  421. struct resource *bar0 = &pdev->resource[0];
  422. struct resource *bar4 = &pdev->resource[4];
  423. int ret;
  424. ivpu_dbg(vdev, MISC, "Mapping BAR0 (RegV) %pR\n", bar0);
  425. vdev->regv = devm_ioremap_resource(vdev->drm.dev, bar0);
  426. if (IS_ERR(vdev->regv)) {
  427. ivpu_err(vdev, "Failed to map bar 0: %pe\n", vdev->regv);
  428. return PTR_ERR(vdev->regv);
  429. }
  430. ivpu_dbg(vdev, MISC, "Mapping BAR4 (RegB) %pR\n", bar4);
  431. vdev->regb = devm_ioremap_resource(vdev->drm.dev, bar4);
  432. if (IS_ERR(vdev->regb)) {
  433. ivpu_err(vdev, "Failed to map bar 4: %pe\n", vdev->regb);
  434. return PTR_ERR(vdev->regb);
  435. }
  436. ret = dma_set_mask_and_coherent(vdev->drm.dev, DMA_BIT_MASK(vdev->hw->dma_bits));
  437. if (ret) {
  438. ivpu_err(vdev, "Failed to set DMA mask: %d\n", ret);
  439. return ret;
  440. }
  441. dma_set_max_seg_size(vdev->drm.dev, UINT_MAX);
  442. /* Clear any pending errors */
  443. pcie_capability_clear_word(pdev, PCI_EXP_DEVSTA, 0x3f);
  444. /* NPU does not require 10m D3hot delay */
  445. pdev->d3hot_delay = 0;
  446. ret = pcim_enable_device(pdev);
  447. if (ret) {
  448. ivpu_err(vdev, "Failed to enable PCI device: %d\n", ret);
  449. return ret;
  450. }
  451. pci_set_master(pdev);
  452. return 0;
  453. }
  454. static int ivpu_dev_init(struct ivpu_device *vdev)
  455. {
  456. int ret;
  457. vdev->hw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->hw), GFP_KERNEL);
  458. if (!vdev->hw)
  459. return -ENOMEM;
  460. vdev->mmu = drmm_kzalloc(&vdev->drm, sizeof(*vdev->mmu), GFP_KERNEL);
  461. if (!vdev->mmu)
  462. return -ENOMEM;
  463. vdev->fw = drmm_kzalloc(&vdev->drm, sizeof(*vdev->fw), GFP_KERNEL);
  464. if (!vdev->fw)
  465. return -ENOMEM;
  466. vdev->ipc = drmm_kzalloc(&vdev->drm, sizeof(*vdev->ipc), GFP_KERNEL);
  467. if (!vdev->ipc)
  468. return -ENOMEM;
  469. vdev->pm = drmm_kzalloc(&vdev->drm, sizeof(*vdev->pm), GFP_KERNEL);
  470. if (!vdev->pm)
  471. return -ENOMEM;
  472. if (ivpu_hw_ip_gen(vdev) >= IVPU_HW_IP_40XX)
  473. vdev->hw->dma_bits = 48;
  474. else
  475. vdev->hw->dma_bits = 38;
  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. atomic_set(&vdev->job_timeout_counter, 0);
  481. xa_init_flags(&vdev->context_xa, XA_FLAGS_ALLOC | XA_FLAGS_LOCK_IRQ);
  482. xa_init_flags(&vdev->submitted_jobs_xa, XA_FLAGS_ALLOC1);
  483. xa_init_flags(&vdev->db_xa, XA_FLAGS_ALLOC1);
  484. INIT_LIST_HEAD(&vdev->bo_list);
  485. vdev->db_limit.min = IVPU_MIN_DB;
  486. vdev->db_limit.max = IVPU_MAX_DB;
  487. ret = drmm_mutex_init(&vdev->drm, &vdev->context_list_lock);
  488. if (ret)
  489. goto err_xa_destroy;
  490. ret = drmm_mutex_init(&vdev->drm, &vdev->submitted_jobs_lock);
  491. if (ret)
  492. goto err_xa_destroy;
  493. ret = drmm_mutex_init(&vdev->drm, &vdev->bo_list_lock);
  494. if (ret)
  495. goto err_xa_destroy;
  496. ret = ivpu_pci_init(vdev);
  497. if (ret)
  498. goto err_xa_destroy;
  499. ret = ivpu_irq_init(vdev);
  500. if (ret)
  501. goto err_xa_destroy;
  502. /* Init basic HW info based on buttress registers which are accessible before power up */
  503. ret = ivpu_hw_init(vdev);
  504. if (ret)
  505. goto err_xa_destroy;
  506. /* Power up early so the rest of init code can access VPU registers */
  507. ret = ivpu_hw_power_up(vdev);
  508. if (ret)
  509. goto err_shutdown;
  510. ivpu_mmu_global_context_init(vdev);
  511. ret = ivpu_mmu_init(vdev);
  512. if (ret)
  513. goto err_mmu_gctx_fini;
  514. ret = ivpu_mmu_reserved_context_init(vdev);
  515. if (ret)
  516. goto err_mmu_gctx_fini;
  517. ret = ivpu_fw_init(vdev);
  518. if (ret)
  519. goto err_mmu_rctx_fini;
  520. ret = ivpu_ipc_init(vdev);
  521. if (ret)
  522. goto err_fw_fini;
  523. ivpu_pm_init(vdev);
  524. ret = ivpu_boot(vdev);
  525. if (ret)
  526. goto err_ipc_fini;
  527. ivpu_job_done_consumer_init(vdev);
  528. ivpu_pm_enable(vdev);
  529. return 0;
  530. err_ipc_fini:
  531. ivpu_ipc_fini(vdev);
  532. err_fw_fini:
  533. ivpu_fw_fini(vdev);
  534. err_mmu_rctx_fini:
  535. ivpu_mmu_reserved_context_fini(vdev);
  536. err_mmu_gctx_fini:
  537. ivpu_mmu_global_context_fini(vdev);
  538. err_shutdown:
  539. ivpu_shutdown(vdev);
  540. err_xa_destroy:
  541. xa_destroy(&vdev->db_xa);
  542. xa_destroy(&vdev->submitted_jobs_xa);
  543. xa_destroy(&vdev->context_xa);
  544. return ret;
  545. }
  546. static void ivpu_bo_unbind_all_user_contexts(struct ivpu_device *vdev)
  547. {
  548. struct ivpu_file_priv *file_priv;
  549. unsigned long ctx_id;
  550. mutex_lock(&vdev->context_list_lock);
  551. xa_for_each(&vdev->context_xa, ctx_id, file_priv)
  552. file_priv_unbind(vdev, file_priv);
  553. mutex_unlock(&vdev->context_list_lock);
  554. }
  555. static void ivpu_dev_fini(struct ivpu_device *vdev)
  556. {
  557. ivpu_jobs_abort_all(vdev);
  558. ivpu_pm_disable_recovery(vdev);
  559. ivpu_pm_disable(vdev);
  560. ivpu_prepare_for_reset(vdev);
  561. ivpu_shutdown(vdev);
  562. ivpu_ms_cleanup_all(vdev);
  563. ivpu_job_done_consumer_fini(vdev);
  564. ivpu_bo_unbind_all_user_contexts(vdev);
  565. ivpu_ipc_fini(vdev);
  566. ivpu_fw_fini(vdev);
  567. ivpu_mmu_reserved_context_fini(vdev);
  568. ivpu_mmu_global_context_fini(vdev);
  569. drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->db_xa));
  570. xa_destroy(&vdev->db_xa);
  571. drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->submitted_jobs_xa));
  572. xa_destroy(&vdev->submitted_jobs_xa);
  573. drm_WARN_ON(&vdev->drm, !xa_empty(&vdev->context_xa));
  574. xa_destroy(&vdev->context_xa);
  575. }
  576. static struct pci_device_id ivpu_pci_ids[] = {
  577. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_MTL) },
  578. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_ARL) },
  579. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_LNL) },
  580. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_PTL_P) },
  581. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_WCL) },
  582. { PCI_DEVICE(PCI_VENDOR_ID_INTEL, PCI_DEVICE_ID_NVL) },
  583. { }
  584. };
  585. MODULE_DEVICE_TABLE(pci, ivpu_pci_ids);
  586. static int ivpu_probe(struct pci_dev *pdev, const struct pci_device_id *id)
  587. {
  588. struct ivpu_device *vdev;
  589. int ret;
  590. vdev = devm_drm_dev_alloc(&pdev->dev, &driver, struct ivpu_device, drm);
  591. if (IS_ERR(vdev))
  592. return PTR_ERR(vdev);
  593. pci_set_drvdata(pdev, vdev);
  594. ret = ivpu_dev_init(vdev);
  595. if (ret)
  596. return ret;
  597. ivpu_debugfs_init(vdev);
  598. ivpu_sysfs_init(vdev);
  599. ret = drm_dev_register(&vdev->drm, 0);
  600. if (ret) {
  601. dev_err(&pdev->dev, "Failed to register DRM device: %d\n", ret);
  602. ivpu_dev_fini(vdev);
  603. }
  604. return ret;
  605. }
  606. static void ivpu_remove(struct pci_dev *pdev)
  607. {
  608. struct ivpu_device *vdev = pci_get_drvdata(pdev);
  609. drm_dev_unplug(&vdev->drm);
  610. ivpu_dev_fini(vdev);
  611. }
  612. static const struct dev_pm_ops ivpu_drv_pci_pm = {
  613. SET_SYSTEM_SLEEP_PM_OPS(ivpu_pm_suspend_cb, ivpu_pm_resume_cb)
  614. SET_RUNTIME_PM_OPS(ivpu_pm_runtime_suspend_cb, ivpu_pm_runtime_resume_cb, NULL)
  615. };
  616. static const struct pci_error_handlers ivpu_drv_pci_err = {
  617. .reset_prepare = ivpu_pm_reset_prepare_cb,
  618. .reset_done = ivpu_pm_reset_done_cb,
  619. };
  620. static struct pci_driver ivpu_pci_driver = {
  621. .name = KBUILD_MODNAME,
  622. .id_table = ivpu_pci_ids,
  623. .probe = ivpu_probe,
  624. .remove = ivpu_remove,
  625. .driver = {
  626. .pm = &ivpu_drv_pci_pm,
  627. },
  628. .err_handler = &ivpu_drv_pci_err,
  629. };
  630. module_pci_driver(ivpu_pci_driver);
  631. MODULE_AUTHOR("Intel Corporation");
  632. MODULE_DESCRIPTION(DRIVER_DESC);
  633. MODULE_LICENSE("GPL and additional rights");
  634. MODULE_VERSION(DRIVER_VERSION_STR);