ivpu_drv.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. /*
  3. * Copyright (C) 2020-2025 Intel Corporation
  4. */
  5. #ifndef __IVPU_DRV_H__
  6. #define __IVPU_DRV_H__
  7. #include <drm/drm_device.h>
  8. #include <drm/drm_drv.h>
  9. #include <drm/drm_managed.h>
  10. #include <drm/drm_mm.h>
  11. #include <drm/drm_print.h>
  12. #include <linux/pci.h>
  13. #include <linux/xarray.h>
  14. #include <uapi/drm/ivpu_accel.h>
  15. #include "ivpu_mmu_context.h"
  16. #include "ivpu_ipc.h"
  17. #define DRIVER_NAME "intel_vpu"
  18. #define DRIVER_DESC "Driver for Intel NPU (Neural Processing Unit)"
  19. #define PCI_DEVICE_ID_MTL 0x7d1d
  20. #define PCI_DEVICE_ID_ARL 0xad1d
  21. #define PCI_DEVICE_ID_LNL 0x643e
  22. #define PCI_DEVICE_ID_PTL_P 0xb03e
  23. #define PCI_DEVICE_ID_WCL 0xfd3e
  24. #define PCI_DEVICE_ID_NVL 0xd71d
  25. #define IVPU_HW_IP_37XX 37
  26. #define IVPU_HW_IP_40XX 40
  27. #define IVPU_HW_IP_50XX 50
  28. #define IVPU_HW_IP_60XX 60
  29. #define IVPU_HW_IP_REV_LNL_B0 4
  30. #define IVPU_HW_IP_REV_NVL_A0 0
  31. #define IVPU_HW_BTRS_MTL 1
  32. #define IVPU_HW_BTRS_LNL 2
  33. #define IVPU_GLOBAL_CONTEXT_MMU_SSID 0
  34. /* SSID 1 is used by the VPU to represent reserved context */
  35. #define IVPU_RESERVED_CONTEXT_MMU_SSID 1
  36. #define IVPU_USER_CONTEXT_MIN_SSID 2
  37. #define IVPU_USER_CONTEXT_MAX_SSID (IVPU_USER_CONTEXT_MIN_SSID + 63)
  38. #define IVPU_MIN_DB 1
  39. #define IVPU_MAX_DB 255
  40. #define IVPU_JOB_ID_JOB_MASK GENMASK(7, 0)
  41. #define IVPU_JOB_ID_CONTEXT_MASK GENMASK(31, 8)
  42. #define IVPU_NUM_PRIORITIES 4
  43. #define IVPU_NUM_CMDQS_PER_CTX (IVPU_NUM_PRIORITIES)
  44. #define IVPU_CMDQ_MIN_ID 1
  45. #define IVPU_CMDQ_MAX_ID 255
  46. #define IVPU_PLATFORM_SILICON 0
  47. #define IVPU_PLATFORM_SIMICS 2
  48. #define IVPU_PLATFORM_FPGA 3
  49. #define IVPU_PLATFORM_HSLE 4
  50. #define IVPU_PLATFORM_INVALID 8
  51. #define IVPU_SCHED_MODE_AUTO -1
  52. #define IVPU_DBG_REG BIT(0)
  53. #define IVPU_DBG_IRQ BIT(1)
  54. #define IVPU_DBG_MMU BIT(2)
  55. #define IVPU_DBG_FILE BIT(3)
  56. #define IVPU_DBG_MISC BIT(4)
  57. #define IVPU_DBG_FW_BOOT BIT(5)
  58. #define IVPU_DBG_PM BIT(6)
  59. #define IVPU_DBG_IPC BIT(7)
  60. #define IVPU_DBG_BO BIT(8)
  61. #define IVPU_DBG_JOB BIT(9)
  62. #define IVPU_DBG_JSM BIT(10)
  63. #define IVPU_DBG_KREF BIT(11)
  64. #define IVPU_DBG_RPM BIT(12)
  65. #define IVPU_DBG_MMU_MAP BIT(13)
  66. #define IVPU_DBG_IOCTL BIT(14)
  67. #define ivpu_err(vdev, fmt, ...) \
  68. drm_err(&(vdev)->drm, "%s(): " fmt, __func__, ##__VA_ARGS__)
  69. #define ivpu_err_ratelimited(vdev, fmt, ...) \
  70. drm_err_ratelimited(&(vdev)->drm, "%s(): " fmt, __func__, ##__VA_ARGS__)
  71. #define ivpu_warn(vdev, fmt, ...) \
  72. drm_warn(&(vdev)->drm, "%s(): " fmt, __func__, ##__VA_ARGS__)
  73. #define ivpu_warn_ratelimited(vdev, fmt, ...) \
  74. drm_err_ratelimited(&(vdev)->drm, "%s(): " fmt, __func__, ##__VA_ARGS__)
  75. #define ivpu_info(vdev, fmt, ...) drm_info(&(vdev)->drm, fmt, ##__VA_ARGS__)
  76. #define ivpu_dbg(vdev, type, fmt, args...) do { \
  77. if (unlikely(IVPU_DBG_##type & ivpu_dbg_mask)) \
  78. dev_dbg((vdev)->drm.dev, "[%s] " fmt, #type, ##args); \
  79. } while (0)
  80. #define IVPU_WA(wa_name) (vdev->wa.wa_name)
  81. #define IVPU_PRINT_WA(wa_name) do { \
  82. if (IVPU_WA(wa_name)) \
  83. ivpu_dbg(vdev, MISC, "Using WA: " #wa_name "\n"); \
  84. } while (0)
  85. struct ivpu_wa_table {
  86. bool punit_disabled;
  87. bool clear_runtime_mem;
  88. bool interrupt_clear_with_0;
  89. bool disable_clock_relinquish;
  90. bool disable_d0i3_msg;
  91. bool wp0_during_power_up;
  92. bool disable_d0i2;
  93. };
  94. struct ivpu_hw_info;
  95. struct ivpu_mmu_info;
  96. struct ivpu_fw_info;
  97. struct ivpu_ipc_info;
  98. struct ivpu_pm_info;
  99. struct ivpu_device {
  100. struct drm_device drm;
  101. void __iomem *regb;
  102. void __iomem *regv;
  103. u32 platform;
  104. u32 irq;
  105. struct ivpu_wa_table wa;
  106. struct ivpu_hw_info *hw;
  107. struct ivpu_mmu_info *mmu;
  108. struct ivpu_fw_info *fw;
  109. struct ivpu_ipc_info *ipc;
  110. struct ivpu_pm_info *pm;
  111. struct ivpu_mmu_context gctx;
  112. struct ivpu_mmu_context rctx;
  113. struct mutex context_list_lock; /* Protects user context addition/removal */
  114. struct xarray context_xa;
  115. struct xa_limit context_xa_limit;
  116. struct xarray db_xa;
  117. struct xa_limit db_limit;
  118. u32 db_next;
  119. struct work_struct irq_ipc_work;
  120. struct work_struct irq_dct_work;
  121. struct work_struct context_abort_work;
  122. struct mutex bo_list_lock; /* Protects bo_list */
  123. struct list_head bo_list;
  124. struct mutex submitted_jobs_lock; /* Protects submitted_jobs */
  125. struct xarray submitted_jobs_xa;
  126. struct ivpu_ipc_consumer job_done_consumer;
  127. atomic_t job_timeout_counter;
  128. atomic64_t unique_id_counter;
  129. ktime_t busy_start_ts;
  130. ktime_t busy_time;
  131. struct {
  132. int boot;
  133. int jsm;
  134. int tdr;
  135. int inference;
  136. int autosuspend;
  137. int d0i3_entry_msg;
  138. int state_dump_msg;
  139. } timeout;
  140. };
  141. /*
  142. * file_priv has its own refcount (ref) that allows user space to close the fd
  143. * without blocking even if VPU is still processing some jobs.
  144. */
  145. struct ivpu_file_priv {
  146. struct kref ref;
  147. struct ivpu_device *vdev;
  148. struct mutex lock; /* Protects cmdq */
  149. struct xarray cmdq_xa;
  150. struct ivpu_mmu_context ctx;
  151. struct mutex ms_lock; /* Protects ms_instance_list, ms_info_bo */
  152. struct list_head ms_instance_list;
  153. struct ivpu_bo *ms_info_bo;
  154. struct xa_limit job_limit;
  155. u32 job_id_next;
  156. struct xa_limit cmdq_limit;
  157. u32 cmdq_id_next;
  158. bool has_mmu_faults;
  159. bool bound;
  160. bool aborted;
  161. };
  162. extern int ivpu_dbg_mask;
  163. extern u8 ivpu_pll_min_ratio;
  164. extern u8 ivpu_pll_max_ratio;
  165. extern int ivpu_sched_mode;
  166. extern bool ivpu_disable_mmu_cont_pages;
  167. extern bool ivpu_force_snoop;
  168. #define IVPU_TEST_MODE_FW_TEST BIT(0)
  169. #define IVPU_TEST_MODE_NULL_HW BIT(1)
  170. #define IVPU_TEST_MODE_NULL_SUBMISSION BIT(2)
  171. #define IVPU_TEST_MODE_D0I3_MSG_DISABLE BIT(4)
  172. #define IVPU_TEST_MODE_D0I3_MSG_ENABLE BIT(5)
  173. #define IVPU_TEST_MODE_MIP_DISABLE BIT(6)
  174. #define IVPU_TEST_MODE_DISABLE_TIMEOUTS BIT(8)
  175. #define IVPU_TEST_MODE_TURBO_ENABLE BIT(9)
  176. #define IVPU_TEST_MODE_TURBO_DISABLE BIT(10)
  177. #define IVPU_TEST_MODE_CLK_RELINQ_DISABLE BIT(11)
  178. #define IVPU_TEST_MODE_CLK_RELINQ_ENABLE BIT(12)
  179. #define IVPU_TEST_MODE_D0I2_DISABLE BIT(13)
  180. extern int ivpu_test_mode;
  181. struct ivpu_file_priv *ivpu_file_priv_get(struct ivpu_file_priv *file_priv);
  182. void ivpu_file_priv_put(struct ivpu_file_priv **link);
  183. int ivpu_boot(struct ivpu_device *vdev);
  184. int ivpu_shutdown(struct ivpu_device *vdev);
  185. void ivpu_prepare_for_reset(struct ivpu_device *vdev);
  186. bool ivpu_is_capable(struct ivpu_device *vdev, u32 capability);
  187. static inline u8 ivpu_revision(struct ivpu_device *vdev)
  188. {
  189. return to_pci_dev(vdev->drm.dev)->revision;
  190. }
  191. static inline u16 ivpu_device_id(struct ivpu_device *vdev)
  192. {
  193. return to_pci_dev(vdev->drm.dev)->device;
  194. }
  195. static inline int ivpu_hw_ip_gen(struct ivpu_device *vdev)
  196. {
  197. switch (ivpu_device_id(vdev)) {
  198. case PCI_DEVICE_ID_MTL:
  199. case PCI_DEVICE_ID_ARL:
  200. return IVPU_HW_IP_37XX;
  201. case PCI_DEVICE_ID_LNL:
  202. return IVPU_HW_IP_40XX;
  203. case PCI_DEVICE_ID_PTL_P:
  204. case PCI_DEVICE_ID_WCL:
  205. return IVPU_HW_IP_50XX;
  206. case PCI_DEVICE_ID_NVL:
  207. return IVPU_HW_IP_60XX;
  208. default:
  209. dump_stack();
  210. ivpu_err(vdev, "Unknown NPU IP generation\n");
  211. return 0;
  212. }
  213. }
  214. static inline int ivpu_hw_btrs_gen(struct ivpu_device *vdev)
  215. {
  216. switch (ivpu_device_id(vdev)) {
  217. case PCI_DEVICE_ID_MTL:
  218. case PCI_DEVICE_ID_ARL:
  219. return IVPU_HW_BTRS_MTL;
  220. case PCI_DEVICE_ID_LNL:
  221. case PCI_DEVICE_ID_PTL_P:
  222. case PCI_DEVICE_ID_WCL:
  223. case PCI_DEVICE_ID_NVL:
  224. return IVPU_HW_BTRS_LNL;
  225. default:
  226. dump_stack();
  227. ivpu_err(vdev, "Unknown buttress generation\n");
  228. return 0;
  229. }
  230. }
  231. static inline struct ivpu_device *to_ivpu_device(struct drm_device *dev)
  232. {
  233. return container_of(dev, struct ivpu_device, drm);
  234. }
  235. static inline u32 ivpu_get_context_count(struct ivpu_device *vdev)
  236. {
  237. struct xa_limit ctx_limit = vdev->context_xa_limit;
  238. return (ctx_limit.max - ctx_limit.min + 1);
  239. }
  240. static inline u32 ivpu_get_platform(struct ivpu_device *vdev)
  241. {
  242. WARN_ON_ONCE(vdev->platform == IVPU_PLATFORM_INVALID);
  243. return vdev->platform;
  244. }
  245. static inline bool ivpu_is_silicon(struct ivpu_device *vdev)
  246. {
  247. return ivpu_get_platform(vdev) == IVPU_PLATFORM_SILICON;
  248. }
  249. static inline bool ivpu_is_simics(struct ivpu_device *vdev)
  250. {
  251. return ivpu_get_platform(vdev) == IVPU_PLATFORM_SIMICS;
  252. }
  253. static inline bool ivpu_is_fpga(struct ivpu_device *vdev)
  254. {
  255. return ivpu_get_platform(vdev) == IVPU_PLATFORM_FPGA ||
  256. ivpu_get_platform(vdev) == IVPU_PLATFORM_HSLE;
  257. }
  258. static inline bool ivpu_is_force_snoop_enabled(struct ivpu_device *vdev)
  259. {
  260. return ivpu_force_snoop;
  261. }
  262. #endif /* __IVPU_DRV_H__ */