1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 HiSilicon Limited. */
6 #include <linux/bitfield.h>
7 #include <linux/debugfs.h>
8 #include <linux/iopoll.h>
9 #include <linux/module.h>
10 #include <linux/pci.h>
12 #define QM_QNUM_V1 4096
13 #define QM_QNUM_V2 1024
14 #define QM_MAX_VFS_NUM_V2 63
17 #define QM_ARUSER_M_CFG_1 0x100088
18 #define AXUSER_SNOOP_ENABLE BIT(30)
19 #define AXUSER_CMD_TYPE GENMASK(14, 12)
20 #define AXUSER_CMD_SMMU_NORMAL 1
21 #define AXUSER_NS BIT(6)
22 #define AXUSER_NO BIT(5)
23 #define AXUSER_FP BIT(4)
24 #define AXUSER_SSV BIT(0)
25 #define AXUSER_BASE (AXUSER_SNOOP_ENABLE | \
26 FIELD_PREP(AXUSER_CMD_TYPE, \
27 AXUSER_CMD_SMMU_NORMAL) | \
28 AXUSER_NS | AXUSER_NO | AXUSER_FP)
29 #define QM_ARUSER_M_CFG_ENABLE 0x100090
30 #define ARUSER_M_CFG_ENABLE 0xfffffffe
31 #define QM_AWUSER_M_CFG_1 0x100098
32 #define QM_AWUSER_M_CFG_ENABLE 0x1000a0
33 #define AWUSER_M_CFG_ENABLE 0xfffffffe
34 #define QM_WUSER_M_CFG_ENABLE 0x1000a8
35 #define WUSER_M_CFG_ENABLE 0xffffffff
38 #define QM_MB_CMD_SQC 0x0
39 #define QM_MB_CMD_CQC 0x1
40 #define QM_MB_CMD_EQC 0x2
41 #define QM_MB_CMD_AEQC 0x3
42 #define QM_MB_CMD_SQC_BT 0x4
43 #define QM_MB_CMD_CQC_BT 0x5
44 #define QM_MB_CMD_SQC_VFT_V2 0x6
45 #define QM_MB_CMD_STOP_QP 0x8
46 #define QM_MB_CMD_FLUSH_QM 0x9
47 #define QM_MB_CMD_SRC 0xc
48 #define QM_MB_CMD_DST 0xd
50 #define QM_MB_CMD_SEND_BASE 0x300
51 #define QM_MB_EVENT_SHIFT 8
52 #define QM_MB_BUSY_SHIFT 13
53 #define QM_MB_OP_SHIFT 14
54 #define QM_MB_CMD_DATA_ADDR_L 0x304
55 #define QM_MB_CMD_DATA_ADDR_H 0x308
56 #define QM_MB_MAX_WAIT_CNT 6000
59 #define QM_DOORBELL_CMD_SQ 0
60 #define QM_DOORBELL_CMD_CQ 1
61 #define QM_DOORBELL_CMD_EQ 2
62 #define QM_DOORBELL_CMD_AEQ 3
64 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000
65 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000
66 #define QM_QP_MAX_NUM_SHIFT 11
67 #define QM_DB_CMD_SHIFT_V2 12
68 #define QM_DB_RAND_SHIFT_V2 16
69 #define QM_DB_INDEX_SHIFT_V2 32
70 #define QM_DB_PRIORITY_SHIFT_V2 48
71 #define QM_VF_STATE 0x60
74 #define QM_CACHE_CTL 0x100050
75 #define SQC_CACHE_ENABLE BIT(0)
76 #define CQC_CACHE_ENABLE BIT(1)
77 #define SQC_CACHE_WB_ENABLE BIT(4)
78 #define SQC_CACHE_WB_THRD GENMASK(10, 5)
79 #define CQC_CACHE_WB_ENABLE BIT(11)
80 #define CQC_CACHE_WB_THRD GENMASK(17, 12)
81 #define QM_AXI_M_CFG 0x1000ac
82 #define AXI_M_CFG 0xffff
83 #define QM_AXI_M_CFG_ENABLE 0x1000b0
84 #define AM_CFG_SINGLE_PORT_MAX_TRANS 0x300014
85 #define AXI_M_CFG_ENABLE 0xffffffff
86 #define QM_PEH_AXUSER_CFG 0x1000cc
87 #define QM_PEH_AXUSER_CFG_ENABLE 0x1000d0
88 #define PEH_AXUSER_CFG 0x401001
89 #define PEH_AXUSER_CFG_ENABLE 0xffffffff
92 #define HISI_ACC_SGL_SGE_NR_MAX 255
93 #define QM_SHAPER_CFG 0x100164
94 #define QM_SHAPER_ENABLE BIT(30)
95 #define QM_SHAPER_TYPE1_OFFSET 10
97 /* page number for queue file region */
98 #define QM_DOORBELL_PAGE_NR 1
100 /* uacce mode of the driver */
101 #define UACCE_MODE_NOUACCE 0 /* don't use uacce */
102 #define UACCE_MODE_SVA 1 /* use uacce sva mode */
103 #define UACCE_MODE_DESC "0(default) means only register to crypto, 1 means both register to crypto and uacce"
105 enum qm_stop_reason
{
144 enum qm_misc_ctl_bits
{
145 QM_DRIVER_REMOVING
= 0x0,
152 QM_SUPPORT_DB_ISOLATION
= 0x0,
155 QM_SUPPORT_STOP_FUNC
,
156 QM_SUPPORT_MB_COMMAND
,
157 QM_SUPPORT_SVA_PREFETCH
,
171 struct dfx_diff_registers
{
178 atomic64_t err_irq_cnt
;
179 atomic64_t aeq_irq_cnt
;
180 atomic64_t abnormal_irq_cnt
;
181 atomic64_t create_qp_err_cnt
;
182 atomic64_t mb_err_cnt
;
185 struct debugfs_file
{
186 enum qm_debug_file index
;
188 struct qm_debug
*debug
;
196 struct dentry
*debug_root
;
198 struct debugfs_file files
[DEBUG_FILE_NUM
];
199 struct qm_dev_dfx dev_dfx
;
200 unsigned int *qm_last_words
;
201 /* ACC engines recoreding last regs */
202 unsigned int *last_words
;
203 struct dfx_diff_registers
*qm_diff_regs
;
204 struct dfx_diff_registers
*acc_diff_regs
;
207 struct qm_shaper_factor
{
221 struct hisi_qm_status
{
232 enum acc_err_result
{
238 struct hisi_qm_err_info
{
242 u32 qm_shutdown_mask
;
243 u32 dev_shutdown_mask
;
251 struct hisi_qm_err_status
{
256 struct hisi_qm_err_ini
{
257 int (*hw_init
)(struct hisi_qm
*qm
);
258 void (*hw_err_enable
)(struct hisi_qm
*qm
);
259 void (*hw_err_disable
)(struct hisi_qm
*qm
);
260 u32 (*get_dev_hw_err_status
)(struct hisi_qm
*qm
);
261 void (*clear_dev_hw_err_status
)(struct hisi_qm
*qm
, u32 err_sts
);
262 void (*open_axi_master_ooo
)(struct hisi_qm
*qm
);
263 void (*close_axi_master_ooo
)(struct hisi_qm
*qm
);
264 void (*open_sva_prefetch
)(struct hisi_qm
*qm
);
265 void (*close_sva_prefetch
)(struct hisi_qm
*qm
);
266 void (*show_last_dfx_regs
)(struct hisi_qm
*qm
);
267 void (*err_info_init
)(struct hisi_qm
*qm
);
268 enum acc_err_result (*get_err_result
)(struct hisi_qm
*qm
);
271 struct hisi_qm_cap_info
{
273 /* Register offset */
275 /* Bit offset in register */
283 struct hisi_qm_cap_query_info
{
292 struct hisi_qm_cap_record
{
298 struct hisi_qm_cap_tables
{
300 struct hisi_qm_cap_record
*qm_cap_table
;
302 struct hisi_qm_cap_record
*dev_cap_table
;
305 struct hisi_qm_list
{
307 struct list_head list
;
308 int (*register_to_crypto
)(struct hisi_qm
*qm
);
309 void (*unregister_from_crypto
)(struct hisi_qm
*qm
);
312 struct hisi_qm_poll_data
{
314 struct work_struct work
;
320 * struct qm_err_isolate
321 * @isolate_lock: protects device error log
322 * @err_threshold: user config error threshold which triggers isolation
323 * @is_isolate: device isolation state
324 * @uacce_hw_errs: index into qm device error list
326 struct qm_err_isolate
{
327 struct mutex isolate_lock
;
330 struct list_head qm_hw_errs
;
337 struct qm_aeqc
*aeqc
;
347 enum qm_fun_type fun_type
;
348 const char *dev_name
;
349 struct pci_dev
*pdev
;
350 void __iomem
*io_base
;
351 void __iomem
*db_io_base
;
353 /* Capbility version, 0: not supports */
365 struct list_head list
;
366 struct hisi_qm_list
*qm_list
;
372 struct qm_aeqe
*aeqe
;
377 struct qm_rsv_buf xqc_buf
;
379 struct hisi_qm_status status
;
380 const struct hisi_qm_err_ini
*err_ini
;
381 struct hisi_qm_err_info err_info
;
382 struct hisi_qm_err_status err_status
;
383 /* driver removing and reset sched */
384 unsigned long misc_ctl
;
385 /* Device capability bit */
388 struct rw_semaphore qps_lock
;
390 struct hisi_qp
*qp_array
;
391 struct hisi_qm_poll_data
*poll_data
;
393 struct mutex mailbox_lock
;
395 const struct hisi_qm_hw_ops
*ops
;
397 struct qm_debug debug
;
401 struct workqueue_struct
*wq
;
402 struct work_struct rst_work
;
403 struct work_struct cmd_process
;
407 resource_size_t phys_base
;
408 resource_size_t db_phys_base
;
409 struct uacce_device
*uacce
;
411 struct qm_shaper_factor
*factor
;
414 struct qm_err_isolate isolate_data
;
416 struct hisi_qm_cap_tables cap_tables
;
419 struct hisi_qp_status
{
428 int (*fill_sqe
)(void *sqe
, void *q_parm
, void *d_parm
);
444 struct hisi_qp_status qp_status
;
445 struct hisi_qp_ops
*hw_ops
;
447 void (*req_cb
)(struct hisi_qp
*qp
, void *data
);
448 void (*event_cb
)(struct hisi_qp
*qp
);
454 struct uacce_queue
*uacce_q
;
457 static inline int vfs_num_set(const char *val
, const struct kernel_param
*kp
)
465 ret
= kstrtou32(val
, 10, &n
);
469 if (n
> QM_MAX_VFS_NUM_V2
)
472 return param_set_int(val
, kp
);
475 static inline int mode_set(const char *val
, const struct kernel_param
*kp
)
483 ret
= kstrtou32(val
, 10, &n
);
484 if (ret
!= 0 || (n
!= UACCE_MODE_SVA
&&
485 n
!= UACCE_MODE_NOUACCE
))
488 return param_set_int(val
, kp
);
491 static inline int uacce_mode_set(const char *val
, const struct kernel_param
*kp
)
493 return mode_set(val
, kp
);
496 static inline void hisi_qm_init_list(struct hisi_qm_list
*qm_list
)
498 INIT_LIST_HEAD(&qm_list
->list
);
499 mutex_init(&qm_list
->lock
);
502 static inline void hisi_qm_add_list(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
)
504 mutex_lock(&qm_list
->lock
);
505 list_add_tail(&qm
->list
, &qm_list
->list
);
506 mutex_unlock(&qm_list
->lock
);
509 static inline void hisi_qm_del_list(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
)
511 mutex_lock(&qm_list
->lock
);
513 mutex_unlock(&qm_list
->lock
);
516 int hisi_qm_q_num_set(const char *val
, const struct kernel_param
*kp
,
517 unsigned int device
);
518 int hisi_qm_init(struct hisi_qm
*qm
);
519 void hisi_qm_uninit(struct hisi_qm
*qm
);
520 int hisi_qm_start(struct hisi_qm
*qm
);
521 int hisi_qm_stop(struct hisi_qm
*qm
, enum qm_stop_reason r
);
522 int hisi_qm_start_qp(struct hisi_qp
*qp
, unsigned long arg
);
523 void hisi_qm_stop_qp(struct hisi_qp
*qp
);
524 int hisi_qp_send(struct hisi_qp
*qp
, const void *msg
);
525 void hisi_qm_debug_init(struct hisi_qm
*qm
);
526 void hisi_qm_debug_regs_clear(struct hisi_qm
*qm
);
527 int hisi_qm_sriov_enable(struct pci_dev
*pdev
, int max_vfs
);
528 int hisi_qm_sriov_disable(struct pci_dev
*pdev
, bool is_frozen
);
529 int hisi_qm_sriov_configure(struct pci_dev
*pdev
, int num_vfs
);
530 void hisi_qm_dev_err_init(struct hisi_qm
*qm
);
531 void hisi_qm_dev_err_uninit(struct hisi_qm
*qm
);
532 int hisi_qm_regs_debugfs_init(struct hisi_qm
*qm
,
533 struct dfx_diff_registers
*dregs
, u32 reg_len
);
534 void hisi_qm_regs_debugfs_uninit(struct hisi_qm
*qm
, u32 reg_len
);
535 void hisi_qm_acc_diff_regs_dump(struct hisi_qm
*qm
, struct seq_file
*s
,
536 struct dfx_diff_registers
*dregs
, u32 regs_len
);
538 pci_ers_result_t
hisi_qm_dev_err_detected(struct pci_dev
*pdev
,
539 pci_channel_state_t state
);
540 pci_ers_result_t
hisi_qm_dev_slot_reset(struct pci_dev
*pdev
);
541 void hisi_qm_reset_prepare(struct pci_dev
*pdev
);
542 void hisi_qm_reset_done(struct pci_dev
*pdev
);
544 int hisi_qm_wait_mb_ready(struct hisi_qm
*qm
);
545 int hisi_qm_mb(struct hisi_qm
*qm
, u8 cmd
, dma_addr_t dma_addr
, u16 queue
,
548 struct hisi_acc_sgl_pool
;
549 struct hisi_acc_hw_sgl
*hisi_acc_sg_buf_map_to_hw_sgl(struct device
*dev
,
550 struct scatterlist
*sgl
, struct hisi_acc_sgl_pool
*pool
,
551 u32 index
, dma_addr_t
*hw_sgl_dma
);
552 void hisi_acc_sg_buf_unmap(struct device
*dev
, struct scatterlist
*sgl
,
553 struct hisi_acc_hw_sgl
*hw_sgl
);
554 struct hisi_acc_sgl_pool
*hisi_acc_create_sgl_pool(struct device
*dev
,
555 u32 count
, u32 sge_nr
);
556 void hisi_acc_free_sgl_pool(struct device
*dev
,
557 struct hisi_acc_sgl_pool
*pool
);
558 int hisi_qm_alloc_qps_node(struct hisi_qm_list
*qm_list
, int qp_num
,
559 u8 alg_type
, int node
, struct hisi_qp
**qps
);
560 void hisi_qm_free_qps(struct hisi_qp
**qps
, int qp_num
);
561 void hisi_qm_dev_shutdown(struct pci_dev
*pdev
);
562 void hisi_qm_wait_task_finish(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
);
563 int hisi_qm_alg_register(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
, int guard
);
564 void hisi_qm_alg_unregister(struct hisi_qm
*qm
, struct hisi_qm_list
*qm_list
, int guard
);
565 int hisi_qm_resume(struct device
*dev
);
566 int hisi_qm_suspend(struct device
*dev
);
567 void hisi_qm_pm_uninit(struct hisi_qm
*qm
);
568 void hisi_qm_pm_init(struct hisi_qm
*qm
);
569 int hisi_qm_get_dfx_access(struct hisi_qm
*qm
);
570 void hisi_qm_put_dfx_access(struct hisi_qm
*qm
);
571 void hisi_qm_regs_dump(struct seq_file
*s
, struct debugfs_regset32
*regset
);
572 u32
hisi_qm_get_hw_info(struct hisi_qm
*qm
,
573 const struct hisi_qm_cap_info
*info_table
,
574 u32 index
, bool is_read
);
575 u32
hisi_qm_get_cap_value(struct hisi_qm
*qm
,
576 const struct hisi_qm_cap_query_info
*info_table
,
577 u32 index
, bool is_read
);
578 int hisi_qm_set_algs(struct hisi_qm
*qm
, u64 alg_msk
, const struct qm_dev_alg
*dev_algs
,
581 /* Used by VFIO ACC live migration driver */
582 struct pci_driver
*hisi_sec_get_pf_driver(void);
583 struct pci_driver
*hisi_hpre_get_pf_driver(void);
584 struct pci_driver
*hisi_zip_get_pf_driver(void);