Merge tag 'pstore-v5.8-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees...
[linux-2.6-microblaze.git] / drivers / crypto / hisilicon / qm.h
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /* Copyright (c) 2019 HiSilicon Limited. */
3 #ifndef HISI_ACC_QM_H
4 #define HISI_ACC_QM_H
5
6 #include <linux/bitfield.h>
7 #include <linux/iopoll.h>
8 #include <linux/module.h>
9 #include <linux/pci.h>
10
11 #define QM_QNUM_V1                      4096
12 #define QM_QNUM_V2                      1024
13 #define QM_MAX_VFS_NUM_V2               63
14
15 /* qm user domain */
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
35
36 /* qm cache */
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 AXI_M_CFG_ENABLE                0xffffffff
48 #define QM_PEH_AXUSER_CFG               0x1000cc
49 #define QM_PEH_AXUSER_CFG_ENABLE        0x1000d0
50 #define PEH_AXUSER_CFG                  0x401001
51 #define PEH_AXUSER_CFG_ENABLE           0xffffffff
52
53 #define QM_DFX_MB_CNT_VF                0x104010
54 #define QM_DFX_DB_CNT_VF                0x104020
55 #define QM_DFX_SQE_CNT_VF_SQN           0x104030
56 #define QM_DFX_CQE_CNT_VF_CQN           0x104040
57 #define QM_DFX_QN_SHIFT                 16
58 #define CURRENT_FUN_MASK                GENMASK(5, 0)
59 #define CURRENT_Q_MASK                  GENMASK(31, 16)
60
61 #define QM_AXI_RRESP                    BIT(0)
62 #define QM_AXI_BRESP                    BIT(1)
63 #define QM_ECC_MBIT                     BIT(2)
64 #define QM_ECC_1BIT                     BIT(3)
65 #define QM_ACC_GET_TASK_TIMEOUT         BIT(4)
66 #define QM_ACC_DO_TASK_TIMEOUT          BIT(5)
67 #define QM_ACC_WB_NOT_READY_TIMEOUT     BIT(6)
68 #define QM_SQ_CQ_VF_INVALID             BIT(7)
69 #define QM_CQ_VF_INVALID                BIT(8)
70 #define QM_SQ_VF_INVALID                BIT(9)
71 #define QM_DB_TIMEOUT                   BIT(10)
72 #define QM_OF_FIFO_OF                   BIT(11)
73 #define QM_DB_RANDOM_INVALID            BIT(12)
74
75 #define QM_BASE_NFE     (QM_AXI_RRESP | QM_AXI_BRESP | QM_ECC_MBIT | \
76                          QM_ACC_GET_TASK_TIMEOUT | QM_DB_TIMEOUT | \
77                          QM_OF_FIFO_OF | QM_DB_RANDOM_INVALID)
78 #define QM_BASE_CE                      QM_ECC_1BIT
79
80 #define QM_Q_DEPTH                      1024
81
82 #define HISI_ACC_SGL_SGE_NR_MAX         255
83
84 /* page number for queue file region */
85 #define QM_DOORBELL_PAGE_NR             1
86
87 enum qm_stop_reason {
88         QM_NORMAL,
89         QM_SOFT_RESET,
90         QM_FLR,
91 };
92
93 enum qm_state {
94         QM_INIT = 0,
95         QM_START,
96         QM_CLOSE,
97         QM_STOP,
98 };
99
100 enum qp_state {
101         QP_INIT = 1,
102         QP_START,
103         QP_STOP,
104         QP_CLOSE,
105 };
106
107 enum qm_hw_ver {
108         QM_HW_UNKNOWN = -1,
109         QM_HW_V1 = 0x20,
110         QM_HW_V2 = 0x21,
111         QM_HW_V3 = 0x30,
112 };
113
114 enum qm_fun_type {
115         QM_HW_PF,
116         QM_HW_VF,
117 };
118
119 enum qm_debug_file {
120         CURRENT_Q,
121         CLEAR_ENABLE,
122         DEBUG_FILE_NUM,
123 };
124
125 struct qm_dfx {
126         atomic64_t err_irq_cnt;
127         atomic64_t aeq_irq_cnt;
128         atomic64_t abnormal_irq_cnt;
129         atomic64_t create_qp_err_cnt;
130         atomic64_t mb_err_cnt;
131 };
132
133 struct debugfs_file {
134         enum qm_debug_file index;
135         struct mutex lock;
136         struct qm_debug *debug;
137 };
138
139 struct qm_debug {
140         u32 curr_qm_qp_num;
141         u32 sqe_mask_offset;
142         u32 sqe_mask_len;
143         struct qm_dfx dfx;
144         struct dentry *debug_root;
145         struct dentry *qm_d;
146         struct debugfs_file files[DEBUG_FILE_NUM];
147 };
148
149 struct qm_dma {
150         void *va;
151         dma_addr_t dma;
152         size_t size;
153 };
154
155 struct hisi_qm_status {
156         u32 eq_head;
157         bool eqc_phase;
158         u32 aeq_head;
159         bool aeqc_phase;
160         atomic_t flags;
161         int stop_reason;
162 };
163
164 struct hisi_qm;
165
166 struct hisi_qm_err_info {
167         char *acpi_rst;
168         u32 msi_wr_port;
169         u32 ecc_2bits_mask;
170         u32 ce;
171         u32 nfe;
172         u32 fe;
173 };
174
175 struct hisi_qm_err_status {
176         u32 is_qm_ecc_mbit;
177         u32 is_dev_ecc_mbit;
178 };
179
180 struct hisi_qm_err_ini {
181         int (*hw_init)(struct hisi_qm *qm);
182         void (*hw_err_enable)(struct hisi_qm *qm);
183         void (*hw_err_disable)(struct hisi_qm *qm);
184         u32 (*get_dev_hw_err_status)(struct hisi_qm *qm);
185         void (*clear_dev_hw_err_status)(struct hisi_qm *qm, u32 err_sts);
186         void (*open_axi_master_ooo)(struct hisi_qm *qm);
187         void (*close_axi_master_ooo)(struct hisi_qm *qm);
188         void (*log_dev_hw_err)(struct hisi_qm *qm, u32 err_sts);
189         struct hisi_qm_err_info err_info;
190 };
191
192 struct hisi_qm_list {
193         struct mutex lock;
194         struct list_head list;
195 };
196
197 struct hisi_qm {
198         enum qm_hw_ver ver;
199         enum qm_fun_type fun_type;
200         const char *dev_name;
201         struct pci_dev *pdev;
202         void __iomem *io_base;
203         u32 sqe_size;
204         u32 qp_base;
205         u32 qp_num;
206         u32 qp_in_used;
207         u32 ctrl_qp_num;
208         u32 vfs_num;
209         struct list_head list;
210         struct hisi_qm_list *qm_list;
211
212         struct qm_dma qdma;
213         struct qm_sqc *sqc;
214         struct qm_cqc *cqc;
215         struct qm_eqe *eqe;
216         struct qm_aeqe *aeqe;
217         dma_addr_t sqc_dma;
218         dma_addr_t cqc_dma;
219         dma_addr_t eqe_dma;
220         dma_addr_t aeqe_dma;
221
222         struct hisi_qm_status status;
223         const struct hisi_qm_err_ini *err_ini;
224         struct hisi_qm_err_status err_status;
225         unsigned long reset_flag;
226
227         struct rw_semaphore qps_lock;
228         struct idr qp_idr;
229         struct hisi_qp *qp_array;
230
231         struct mutex mailbox_lock;
232
233         const struct hisi_qm_hw_ops *ops;
234
235         struct qm_debug debug;
236
237         u32 error_mask;
238
239         struct workqueue_struct *wq;
240         struct work_struct work;
241         struct work_struct rst_work;
242
243         const char *algs;
244         bool use_sva;
245         resource_size_t phys_base;
246         resource_size_t phys_size;
247         struct uacce_device *uacce;
248 };
249
250 struct hisi_qp_status {
251         atomic_t used;
252         u16 sq_tail;
253         u16 cq_head;
254         bool cqc_phase;
255         atomic_t flags;
256 };
257
258 struct hisi_qp_ops {
259         int (*fill_sqe)(void *sqe, void *q_parm, void *d_parm);
260 };
261
262 struct hisi_qp {
263         u32 qp_id;
264         u8 alg_type;
265         u8 req_type;
266
267         struct qm_dma qdma;
268         void *sqe;
269         struct qm_cqe *cqe;
270         dma_addr_t sqe_dma;
271         dma_addr_t cqe_dma;
272
273         struct hisi_qp_status qp_status;
274         struct hisi_qp_ops *hw_ops;
275         void *qp_ctx;
276         void (*req_cb)(struct hisi_qp *qp, void *data);
277         void (*event_cb)(struct hisi_qp *qp);
278
279         struct hisi_qm *qm;
280         bool is_resetting;
281         u16 pasid;
282         struct uacce_queue *uacce_q;
283 };
284
285 static inline int q_num_set(const char *val, const struct kernel_param *kp,
286                             unsigned int device)
287 {
288         struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
289                                               device, NULL);
290         u32 n, q_num;
291         int ret;
292
293         if (!val)
294                 return -EINVAL;
295
296         if (!pdev) {
297                 q_num = min_t(u32, QM_QNUM_V1, QM_QNUM_V2);
298                 pr_info("No device found currently, suppose queue number is %d\n",
299                         q_num);
300         } else {
301                 if (pdev->revision == QM_HW_V1)
302                         q_num = QM_QNUM_V1;
303                 else
304                         q_num = QM_QNUM_V2;
305         }
306
307         ret = kstrtou32(val, 10, &n);
308         if (ret || !n || n > q_num)
309                 return -EINVAL;
310
311         return param_set_int(val, kp);
312 }
313
314 static inline int vfs_num_set(const char *val, const struct kernel_param *kp)
315 {
316         u32 n;
317         int ret;
318
319         if (!val)
320                 return -EINVAL;
321
322         ret = kstrtou32(val, 10, &n);
323         if (ret < 0)
324                 return ret;
325
326         if (n > QM_MAX_VFS_NUM_V2)
327                 return -EINVAL;
328
329         return param_set_int(val, kp);
330 }
331
332 static inline void hisi_qm_init_list(struct hisi_qm_list *qm_list)
333 {
334         INIT_LIST_HEAD(&qm_list->list);
335         mutex_init(&qm_list->lock);
336 }
337
338 static inline void hisi_qm_add_to_list(struct hisi_qm *qm,
339                                        struct hisi_qm_list *qm_list)
340 {
341         mutex_lock(&qm_list->lock);
342         list_add_tail(&qm->list, &qm_list->list);
343         mutex_unlock(&qm_list->lock);
344 }
345
346 static inline void hisi_qm_del_from_list(struct hisi_qm *qm,
347                                          struct hisi_qm_list *qm_list)
348 {
349         mutex_lock(&qm_list->lock);
350         list_del(&qm->list);
351         mutex_unlock(&qm_list->lock);
352 }
353
354 int hisi_qm_init(struct hisi_qm *qm);
355 void hisi_qm_uninit(struct hisi_qm *qm);
356 int hisi_qm_start(struct hisi_qm *qm);
357 int hisi_qm_stop(struct hisi_qm *qm);
358 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type);
359 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg);
360 int hisi_qm_stop_qp(struct hisi_qp *qp);
361 void hisi_qm_release_qp(struct hisi_qp *qp);
362 int hisi_qp_send(struct hisi_qp *qp, const void *msg);
363 int hisi_qm_get_free_qp_num(struct hisi_qm *qm);
364 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number);
365 int hisi_qm_debug_init(struct hisi_qm *qm);
366 enum qm_hw_ver hisi_qm_get_hw_version(struct pci_dev *pdev);
367 void hisi_qm_debug_regs_clear(struct hisi_qm *qm);
368 int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs);
369 int hisi_qm_sriov_disable(struct pci_dev *pdev);
370 int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs);
371 void hisi_qm_dev_err_init(struct hisi_qm *qm);
372 void hisi_qm_dev_err_uninit(struct hisi_qm *qm);
373 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
374                                           pci_channel_state_t state);
375 pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev);
376 void hisi_qm_reset_prepare(struct pci_dev *pdev);
377 void hisi_qm_reset_done(struct pci_dev *pdev);
378
379 struct hisi_acc_sgl_pool;
380 struct hisi_acc_hw_sgl *hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
381         struct scatterlist *sgl, struct hisi_acc_sgl_pool *pool,
382         u32 index, dma_addr_t *hw_sgl_dma);
383 void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
384                            struct hisi_acc_hw_sgl *hw_sgl);
385 struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
386                                                    u32 count, u32 sge_nr);
387 void hisi_acc_free_sgl_pool(struct device *dev,
388                             struct hisi_acc_sgl_pool *pool);
389 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
390                            u8 alg_type, int node, struct hisi_qp **qps);
391 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num);
392 #endif