1 /* SPDX-License-Identifier: GPL-2.0 */ 2 /* 3 * Copyright (C) 2022-2024, Advanced Micro Devices, Inc. 4 */ 5 6 #ifndef _AMDXDNA_PCI_DRV_H_ 7 #define _AMDXDNA_PCI_DRV_H_ 8 9 #include <linux/workqueue.h> 10 #include <linux/xarray.h> 11 12 #define XDNA_INFO(xdna, fmt, args...) drm_info(&(xdna)->ddev, fmt, ##args) 13 #define XDNA_WARN(xdna, fmt, args...) drm_warn(&(xdna)->ddev, "%s: "fmt, __func__, ##args) 14 #define XDNA_ERR(xdna, fmt, args...) drm_err(&(xdna)->ddev, "%s: "fmt, __func__, ##args) 15 #define XDNA_DBG(xdna, fmt, args...) drm_dbg(&(xdna)->ddev, fmt, ##args) 16 #define XDNA_INFO_ONCE(xdna, fmt, args...) drm_info_once(&(xdna)->ddev, fmt, ##args) 17 18 #define XDNA_MBZ_DBG(xdna, ptr, sz) \ 19 ({ \ 20 int __i; \ 21 int __ret = 0; \ 22 u8 *__ptr = (u8 *)(ptr); \ 23 for (__i = 0; __i < (sz); __i++) { \ 24 if (__ptr[__i]) { \ 25 XDNA_DBG(xdna, "MBZ check failed"); \ 26 __ret = -EINVAL; \ 27 break; \ 28 } \ 29 } \ 30 __ret; \ 31 }) 32 33 #define to_xdna_dev(drm_dev) \ 34 ((struct amdxdna_dev *)container_of(drm_dev, struct amdxdna_dev, ddev)) 35 36 extern const struct drm_driver amdxdna_drm_drv; 37 38 struct amdxdna_client; 39 struct amdxdna_dev; 40 struct amdxdna_drm_get_info; 41 struct amdxdna_drm_set_state; 42 struct amdxdna_gem_obj; 43 struct amdxdna_hwctx; 44 struct amdxdna_sched_job; 45 46 /* 47 * struct amdxdna_dev_ops - Device hardware operation callbacks 48 */ 49 struct amdxdna_dev_ops { 50 int (*init)(struct amdxdna_dev *xdna); 51 void (*fini)(struct amdxdna_dev *xdna); 52 int (*resume)(struct amdxdna_dev *xdna); 53 int (*suspend)(struct amdxdna_dev *xdna); 54 int (*hwctx_init)(struct amdxdna_hwctx *hwctx); 55 void (*hwctx_fini)(struct amdxdna_hwctx *hwctx); 56 int (*hwctx_config)(struct amdxdna_hwctx *hwctx, u32 type, u64 value, void *buf, u32 size); 57 void (*hmm_invalidate)(struct amdxdna_gem_obj *abo, unsigned long cur_seq); 58 int (*cmd_submit)(struct amdxdna_hwctx *hwctx, struct amdxdna_sched_job *job, u64 *seq); 59 int (*get_aie_info)(struct amdxdna_client *client, struct amdxdna_drm_get_info *args); 60 int (*set_aie_state)(struct amdxdna_client *client, struct amdxdna_drm_set_state *args); 61 int (*get_array)(struct amdxdna_client *client, struct amdxdna_drm_get_array *args); 62 }; 63 64 /* 65 * struct amdxdna_dev_info - Device hardware information 66 * Record device static information, like reg, mbox, PSP, SMU bar index 67 */ 68 struct amdxdna_dev_info { 69 int reg_bar; 70 int mbox_bar; 71 int sram_bar; 72 int psp_bar; 73 int smu_bar; 74 int device_type; 75 int first_col; 76 u32 dev_mem_buf_shift; 77 u64 dev_mem_base; 78 size_t dev_mem_size; 79 char *vbnv; 80 const struct amdxdna_dev_priv *dev_priv; 81 const struct amdxdna_dev_ops *ops; 82 }; 83 84 struct amdxdna_fw_ver { 85 u32 major; 86 u32 minor; 87 u32 sub; 88 u32 build; 89 }; 90 91 struct amdxdna_dev { 92 struct drm_device ddev; 93 struct amdxdna_dev_hdl *dev_handle; 94 const struct amdxdna_dev_info *dev_info; 95 void *xrs_hdl; 96 97 struct mutex dev_lock; /* per device lock */ 98 struct list_head client_list; 99 struct amdxdna_fw_ver fw_ver; 100 struct rw_semaphore notifier_lock; /* for mmu notifier*/ 101 struct workqueue_struct *notifier_wq; 102 }; 103 104 /* 105 * struct amdxdna_device_id - PCI device info 106 */ 107 struct amdxdna_device_id { 108 unsigned short device; 109 u8 revision; 110 const struct amdxdna_dev_info *dev_info; 111 }; 112 113 /* 114 * struct amdxdna_client - amdxdna client 115 * A per fd data structure for managing context and other user process stuffs. 116 */ 117 struct amdxdna_client { 118 struct list_head node; 119 pid_t pid; 120 struct srcu_struct hwctx_srcu; 121 struct xarray hwctx_xa; 122 u32 next_hwctxid; 123 struct amdxdna_dev *xdna; 124 struct drm_file *filp; 125 126 struct mutex mm_lock; /* protect memory related */ 127 struct amdxdna_gem_obj *dev_heap; 128 129 struct iommu_sva *sva; 130 int pasid; 131 }; 132 133 #define amdxdna_for_each_hwctx(client, hwctx_id, entry) \ 134 xa_for_each(&(client)->hwctx_xa, hwctx_id, entry) 135 136 /* Add device info below */ 137 extern const struct amdxdna_dev_info dev_npu1_info; 138 extern const struct amdxdna_dev_info dev_npu2_info; 139 extern const struct amdxdna_dev_info dev_npu4_info; 140 extern const struct amdxdna_dev_info dev_npu5_info; 141 extern const struct amdxdna_dev_info dev_npu6_info; 142 143 int amdxdna_sysfs_init(struct amdxdna_dev *xdna); 144 void amdxdna_sysfs_fini(struct amdxdna_dev *xdna); 145 146 #endif /* _AMDXDNA_PCI_DRV_H_ */ 147