1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 HiSilicon Limited. */
6 #include <linux/bitfield.h>
7 #include <linux/iopoll.h>
8 #include <linux/module.h>
11 #define QM_QNUM_V1 4096
12 #define QM_QNUM_V2 1024
13 #define QM_MAX_VFS_NUM_V2 63
16 #define QM_ARUSER_M_CFG_1 0x100088
17 #define AXUSER_SNOOP_ENABLE BIT(30)
18 #define AXUSER_CMD_TYPE GENMASK(14, 12)
19 #define AXUSER_CMD_SMMU_NORMAL 1
20 #define AXUSER_NS BIT(6)
21 #define AXUSER_NO BIT(5)
22 #define AXUSER_FP BIT(4)
23 #define AXUSER_SSV BIT(0)
24 #define AXUSER_BASE (AXUSER_SNOOP_ENABLE | \
25 FIELD_PREP(AXUSER_CMD_TYPE, \
26 AXUSER_CMD_SMMU_NORMAL) | \
27 AXUSER_NS | AXUSER_NO | AXUSER_FP)
28 #define QM_ARUSER_M_CFG_ENABLE 0x100090
29 #define ARUSER_M_CFG_ENABLE 0xfffffffe
30 #define QM_AWUSER_M_CFG_1 0x100098
31 #define QM_AWUSER_M_CFG_ENABLE 0x1000a0
32 #define AWUSER_M_CFG_ENABLE 0xfffffffe
33 #define QM_WUSER_M_CFG_ENABLE 0x1000a8
34 #define WUSER_M_CFG_ENABLE 0xffffffff
37 #define QM_CACHE_CTL 0x100050
38 #define SQC_CACHE_ENABLE BIT(0)
39 #define CQC_CACHE_ENABLE BIT(1)
40 #define SQC_CACHE_WB_ENABLE BIT(4)
41 #define SQC_CACHE_WB_THRD GENMASK(10, 5)
42 #define CQC_CACHE_WB_ENABLE BIT(11)
43 #define CQC_CACHE_WB_THRD GENMASK(17, 12)
44 #define QM_AXI_M_CFG 0x1000ac
45 #define AXI_M_CFG 0xffff
46 #define QM_AXI_M_CFG_ENABLE 0x1000b0
47 #define AM_CFG_SINGLE_PORT_MAX_TRANS 0x300014
48 #define AXI_M_CFG_ENABLE 0xffffffff
49 #define QM_PEH_AXUSER_CFG 0x1000cc
50 #define QM_PEH_AXUSER_CFG_ENABLE 0x1000d0
51 #define PEH_AXUSER_CFG 0x401001
52 #define PEH_AXUSER_CFG_ENABLE 0xffffffff
54 #define QM_AXI_RRESP BIT(0)
55 #define QM_AXI_BRESP BIT(1)
56 #define QM_ECC_MBIT BIT(2)
57 #define QM_ECC_1BIT BIT(3)
58 #define QM_ACC_GET_TASK_TIMEOUT BIT(4)
59 #define QM_ACC_DO_TASK_TIMEOUT BIT(5)
60 #define QM_ACC_WB_NOT_READY_TIMEOUT BIT(6)
61 #define QM_SQ_CQ_VF_INVALID BIT(7)
62 #define QM_CQ_VF_INVALID BIT(8)
63 #define QM_SQ_VF_INVALID BIT(9)
64 #define QM_DB_TIMEOUT BIT(10)
65 #define QM_OF_FIFO_OF BIT(11)
66 #define QM_DB_RANDOM_INVALID BIT(12)
67 #define QM_MAILBOX_TIMEOUT BIT(13)
68 #define QM_FLR_TIMEOUT BIT(14)
70 #define QM_BASE_NFE (QM_AXI_RRESP | QM_AXI_BRESP | QM_ECC_MBIT | \
71 QM_ACC_GET_TASK_TIMEOUT | QM_DB_TIMEOUT | \
72 QM_OF_FIFO_OF | QM_DB_RANDOM_INVALID | \
73 QM_MAILBOX_TIMEOUT | QM_FLR_TIMEOUT)
74 #define QM_BASE_CE QM_ECC_1BIT
76 #define QM_Q_DEPTH 1024
78 #define HISI_ACC_SGL_SGE_NR_MAX 255
79 #define QM_SHAPER_CFG 0x100164
80 #define QM_SHAPER_ENABLE BIT(30)
81 #define QM_SHAPER_TYPE1_OFFSET 10
83 /* page number for queue file region */
84 #define QM_DOORBELL_PAGE_NR 1
86 /* uacce mode of the driver */
87 #define UACCE_MODE_NOUACCE 0 /* don't use uacce */
88 #define UACCE_MODE_SVA 1 /* use uacce sva mode */
89 #define UACCE_MODE_DESC "0(default) means only register to crypto, 1 means both register to crypto and uacce"
131 atomic64_t err_irq_cnt;
132 atomic64_t aeq_irq_cnt;
133 atomic64_t abnormal_irq_cnt;
134 atomic64_t create_qp_err_cnt;
135 atomic64_t mb_err_cnt;
138 struct debugfs_file {
139 enum qm_debug_file index;
141 struct qm_debug *debug;
149 struct dentry *debug_root;
151 struct debugfs_file files[DEBUG_FILE_NUM];
154 struct qm_shaper_factor {
168 struct hisi_qm_status {
179 struct hisi_qm_err_info {
189 struct hisi_qm_err_status {
194 struct hisi_qm_err_ini {
195 int (*hw_init)(struct hisi_qm *qm);
196 void (*hw_err_enable)(struct hisi_qm *qm);
197 void (*hw_err_disable)(struct hisi_qm *qm);
198 u32 (*get_dev_hw_err_status)(struct hisi_qm *qm);
199 void (*clear_dev_hw_err_status)(struct hisi_qm *qm, u32 err_sts);
200 void (*open_axi_master_ooo)(struct hisi_qm *qm);
201 void (*close_axi_master_ooo)(struct hisi_qm *qm);
202 void (*open_sva_prefetch)(struct hisi_qm *qm);
203 void (*close_sva_prefetch)(struct hisi_qm *qm);
204 void (*log_dev_hw_err)(struct hisi_qm *qm, u32 err_sts);
205 void (*err_info_init)(struct hisi_qm *qm);
208 struct hisi_qm_list {
210 struct list_head list;
211 int (*register_to_crypto)(struct hisi_qm *qm);
212 void (*unregister_from_crypto)(struct hisi_qm *qm);
217 enum qm_fun_type fun_type;
218 const char *dev_name;
219 struct pci_dev *pdev;
220 void __iomem *io_base;
221 void __iomem *db_io_base;
230 struct list_head list;
231 struct hisi_qm_list *qm_list;
237 struct qm_aeqe *aeqe;
243 struct hisi_qm_status status;
244 const struct hisi_qm_err_ini *err_ini;
245 struct hisi_qm_err_info err_info;
246 struct hisi_qm_err_status err_status;
247 unsigned long misc_ctl; /* driver removing and reset sched */
249 struct rw_semaphore qps_lock;
251 struct hisi_qp *qp_array;
253 struct mutex mailbox_lock;
255 const struct hisi_qm_hw_ops *ops;
257 struct qm_debug debug;
261 struct workqueue_struct *wq;
262 struct work_struct work;
263 struct work_struct rst_work;
264 struct work_struct cmd_process;
270 /* doorbell isolation enable */
271 bool use_db_isolation;
272 resource_size_t phys_base;
273 resource_size_t db_phys_base;
274 struct uacce_device *uacce;
276 struct qm_shaper_factor *factor;
281 struct hisi_qp_status {
290 int (*fill_sqe)(void *sqe, void *q_parm, void *d_parm);
304 struct hisi_qp_status qp_status;
305 struct hisi_qp_ops *hw_ops;
307 void (*req_cb)(struct hisi_qp *qp, void *data);
308 void (*event_cb)(struct hisi_qp *qp);
314 struct uacce_queue *uacce_q;
317 static inline int q_num_set(const char *val, const struct kernel_param *kp,
320 struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
329 q_num = min_t(u32, QM_QNUM_V1, QM_QNUM_V2);
330 pr_info("No device found currently, suppose queue number is %u\n",
333 if (pdev->revision == QM_HW_V1)
339 ret = kstrtou32(val, 10, &n);
340 if (ret || n < QM_MIN_QNUM || n > q_num)
343 return param_set_int(val, kp);
346 static inline int vfs_num_set(const char *val, const struct kernel_param *kp)
354 ret = kstrtou32(val, 10, &n);
358 if (n > QM_MAX_VFS_NUM_V2)
361 return param_set_int(val, kp);
364 static inline int mode_set(const char *val, const struct kernel_param *kp)
372 ret = kstrtou32(val, 10, &n);
373 if (ret != 0 || (n != UACCE_MODE_SVA &&
374 n != UACCE_MODE_NOUACCE))
377 return param_set_int(val, kp);
380 static inline int uacce_mode_set(const char *val, const struct kernel_param *kp)
382 return mode_set(val, kp);
385 static inline void hisi_qm_init_list(struct hisi_qm_list *qm_list)
387 INIT_LIST_HEAD(&qm_list->list);
388 mutex_init(&qm_list->lock);
391 int hisi_qm_init(struct hisi_qm *qm);
392 void hisi_qm_uninit(struct hisi_qm *qm);
393 int hisi_qm_start(struct hisi_qm *qm);
394 int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r);
395 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type);
396 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg);
397 int hisi_qm_stop_qp(struct hisi_qp *qp);
398 void hisi_qm_release_qp(struct hisi_qp *qp);
399 int hisi_qp_send(struct hisi_qp *qp, const void *msg);
400 int hisi_qm_get_free_qp_num(struct hisi_qm *qm);
401 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number);
402 void hisi_qm_debug_init(struct hisi_qm *qm);
403 enum qm_hw_ver hisi_qm_get_hw_version(struct pci_dev *pdev);
404 void hisi_qm_debug_regs_clear(struct hisi_qm *qm);
405 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs);
406 int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen);
407 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs);
408 void hisi_qm_dev_err_init(struct hisi_qm *qm);
409 void hisi_qm_dev_err_uninit(struct hisi_qm *qm);
410 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
411 pci_channel_state_t state);
412 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev);
413 void hisi_qm_reset_prepare(struct pci_dev *pdev);
414 void hisi_qm_reset_done(struct pci_dev *pdev);
416 struct hisi_acc_sgl_pool;
417 struct hisi_acc_hw_sgl *hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
418 struct scatterlist *sgl, struct hisi_acc_sgl_pool *pool,
419 u32 index, dma_addr_t *hw_sgl_dma);
420 void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
421 struct hisi_acc_hw_sgl *hw_sgl);
422 struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
423 u32 count, u32 sge_nr);
424 void hisi_acc_free_sgl_pool(struct device *dev,
425 struct hisi_acc_sgl_pool *pool);
426 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
427 u8 alg_type, int node, struct hisi_qp **qps);
428 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num);
429 void hisi_qm_dev_shutdown(struct pci_dev *pdev);
430 void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
431 int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
432 void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list);