qm.h 9.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/* SPDX-License-Identifier: GPL-2.0 */
/* Copyright (c) 2019 HiSilicon Limited. */
#ifndef HISI_ACC_QM_H
#define HISI_ACC_QM_H

#include <linux/bitfield.h>
#include <linux/iopoll.h>
#include <linux/module.h>
#include <linux/pci.h>

11 12
#define QM_QNUM_V1			4096
#define QM_QNUM_V2			1024
13 14
#define QM_MAX_VFS_NUM_V2		63

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
/* qm user domain */
#define QM_ARUSER_M_CFG_1		0x100088
#define AXUSER_SNOOP_ENABLE		BIT(30)
#define AXUSER_CMD_TYPE			GENMASK(14, 12)
#define AXUSER_CMD_SMMU_NORMAL		1
#define AXUSER_NS			BIT(6)
#define AXUSER_NO			BIT(5)
#define AXUSER_FP			BIT(4)
#define AXUSER_SSV			BIT(0)
#define AXUSER_BASE			(AXUSER_SNOOP_ENABLE |		\
					FIELD_PREP(AXUSER_CMD_TYPE,	\
					AXUSER_CMD_SMMU_NORMAL) |	\
					AXUSER_NS | AXUSER_NO | AXUSER_FP)
#define QM_ARUSER_M_CFG_ENABLE		0x100090
#define ARUSER_M_CFG_ENABLE		0xfffffffe
#define QM_AWUSER_M_CFG_1		0x100098
#define QM_AWUSER_M_CFG_ENABLE		0x1000a0
#define AWUSER_M_CFG_ENABLE		0xfffffffe
#define QM_WUSER_M_CFG_ENABLE		0x1000a8
#define WUSER_M_CFG_ENABLE		0xffffffff

/* qm cache */
#define QM_CACHE_CTL			0x100050
#define SQC_CACHE_ENABLE		BIT(0)
#define CQC_CACHE_ENABLE		BIT(1)
#define SQC_CACHE_WB_ENABLE		BIT(4)
#define SQC_CACHE_WB_THRD		GENMASK(10, 5)
#define CQC_CACHE_WB_ENABLE		BIT(11)
#define CQC_CACHE_WB_THRD		GENMASK(17, 12)
#define QM_AXI_M_CFG			0x1000ac
#define AXI_M_CFG			0xffff
#define QM_AXI_M_CFG_ENABLE		0x1000b0
47
#define AM_CFG_SINGLE_PORT_MAX_TRANS	0x300014
48 49 50 51 52 53
#define AXI_M_CFG_ENABLE		0xffffffff
#define QM_PEH_AXUSER_CFG		0x1000cc
#define QM_PEH_AXUSER_CFG_ENABLE	0x1000d0
#define PEH_AXUSER_CFG			0x401001
#define PEH_AXUSER_CFG_ENABLE		0xffffffff

54 55 56 57 58 59 60
#define QM_DFX_MB_CNT_VF		0x104010
#define QM_DFX_DB_CNT_VF		0x104020
#define QM_DFX_SQE_CNT_VF_SQN		0x104030
#define QM_DFX_CQE_CNT_VF_CQN		0x104040
#define QM_DFX_QN_SHIFT			16
#define CURRENT_FUN_MASK		GENMASK(5, 0)
#define CURRENT_Q_MASK			GENMASK(31, 16)
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

#define QM_AXI_RRESP			BIT(0)
#define QM_AXI_BRESP			BIT(1)
#define QM_ECC_MBIT			BIT(2)
#define QM_ECC_1BIT			BIT(3)
#define QM_ACC_GET_TASK_TIMEOUT		BIT(4)
#define QM_ACC_DO_TASK_TIMEOUT		BIT(5)
#define QM_ACC_WB_NOT_READY_TIMEOUT	BIT(6)
#define QM_SQ_CQ_VF_INVALID		BIT(7)
#define QM_CQ_VF_INVALID		BIT(8)
#define QM_SQ_VF_INVALID		BIT(9)
#define QM_DB_TIMEOUT			BIT(10)
#define QM_OF_FIFO_OF			BIT(11)
#define QM_DB_RANDOM_INVALID		BIT(12)

#define QM_BASE_NFE	(QM_AXI_RRESP | QM_AXI_BRESP | QM_ECC_MBIT | \
			 QM_ACC_GET_TASK_TIMEOUT | QM_DB_TIMEOUT | \
78
			 QM_OF_FIFO_OF | QM_DB_RANDOM_INVALID)
79 80 81
#define QM_BASE_CE			QM_ECC_1BIT

#define QM_Q_DEPTH			1024
82
#define QM_MIN_QNUM                     2
83 84
#define HISI_ACC_SGL_SGE_NR_MAX		255

85 86 87
/* page number for queue file region */
#define QM_DOORBELL_PAGE_NR		1

88 89 90 91 92
/* uacce mode of the driver */
#define UACCE_MODE_NOUACCE		0 /* don't use uacce */
#define UACCE_MODE_SVA			1 /* use uacce sva mode */
#define UACCE_MODE_DESC	"0(default) means only register to crypto, 1 means both register to crypto and uacce"

93 94 95 96 97 98 99 100 101 102 103 104 105
enum qm_stop_reason {
	QM_NORMAL,
	QM_SOFT_RESET,
	QM_FLR,
};

enum qm_state {
	QM_INIT = 0,
	QM_START,
	QM_CLOSE,
	QM_STOP,
};

106
enum qp_state {
107 108
	QP_INIT = 1,
	QP_START,
109
	QP_STOP,
110
	QP_CLOSE,
111 112 113 114 115 116
};

enum qm_hw_ver {
	QM_HW_UNKNOWN = -1,
	QM_HW_V1 = 0x20,
	QM_HW_V2 = 0x21,
117
	QM_HW_V3 = 0x30,
118 119 120 121
};

enum qm_fun_type {
	QM_HW_PF,
122
	QM_HW_VF,
123 124
};

125 126 127 128 129 130
enum qm_debug_file {
	CURRENT_Q,
	CLEAR_ENABLE,
	DEBUG_FILE_NUM,
};

131 132 133 134 135 136 137 138
struct qm_dfx {
	atomic64_t err_irq_cnt;
	atomic64_t aeq_irq_cnt;
	atomic64_t abnormal_irq_cnt;
	atomic64_t create_qp_err_cnt;
	atomic64_t mb_err_cnt;
};

139 140 141 142 143 144 145 146
struct debugfs_file {
	enum qm_debug_file index;
	struct mutex lock;
	struct qm_debug *debug;
};

struct qm_debug {
	u32 curr_qm_qp_num;
147 148
	u32 sqe_mask_offset;
	u32 sqe_mask_len;
149
	struct qm_dfx dfx;
150 151 152 153 154
	struct dentry *debug_root;
	struct dentry *qm_d;
	struct debugfs_file files[DEBUG_FILE_NUM];
};

155 156 157 158 159 160 161 162 163 164 165
struct qm_dma {
	void *va;
	dma_addr_t dma;
	size_t size;
};

struct hisi_qm_status {
	u32 eq_head;
	bool eqc_phase;
	u32 aeq_head;
	bool aeqc_phase;
166 167
	atomic_t flags;
	int stop_reason;
168 169
};

170 171 172
struct hisi_qm;

struct hisi_qm_err_info {
173 174 175
	char *acpi_rst;
	u32 msi_wr_port;
	u32 ecc_2bits_mask;
176 177 178 179 180
	u32 ce;
	u32 nfe;
	u32 fe;
};

181 182 183 184 185
struct hisi_qm_err_status {
	u32 is_qm_ecc_mbit;
	u32 is_dev_ecc_mbit;
};

186
struct hisi_qm_err_ini {
187
	int (*hw_init)(struct hisi_qm *qm);
188 189
	void (*hw_err_enable)(struct hisi_qm *qm);
	void (*hw_err_disable)(struct hisi_qm *qm);
190
	u32 (*get_dev_hw_err_status)(struct hisi_qm *qm);
191 192 193
	void (*clear_dev_hw_err_status)(struct hisi_qm *qm, u32 err_sts);
	void (*open_axi_master_ooo)(struct hisi_qm *qm);
	void (*close_axi_master_ooo)(struct hisi_qm *qm);
194
	void (*log_dev_hw_err)(struct hisi_qm *qm, u32 err_sts);
195 196 197
	struct hisi_qm_err_info err_info;
};

198 199 200
struct hisi_qm_list {
	struct mutex lock;
	struct list_head list;
201 202
	int (*register_to_crypto)(void);
	void (*unregister_from_crypto)(void);
203 204
};

205 206
struct hisi_qm {
	enum qm_hw_ver ver;
207
	enum qm_fun_type fun_type;
208 209 210 211 212 213
	const char *dev_name;
	struct pci_dev *pdev;
	void __iomem *io_base;
	u32 sqe_size;
	u32 qp_base;
	u32 qp_num;
214
	u32 qp_in_used;
215
	u32 ctrl_qp_num;
216
	u32 vfs_num;
217
	struct list_head list;
218
	struct hisi_qm_list *qm_list;
219 220 221 222 223 224 225 226 227 228 229 230

	struct qm_dma qdma;
	struct qm_sqc *sqc;
	struct qm_cqc *cqc;
	struct qm_eqe *eqe;
	struct qm_aeqe *aeqe;
	dma_addr_t sqc_dma;
	dma_addr_t cqc_dma;
	dma_addr_t eqe_dma;
	dma_addr_t aeqe_dma;

	struct hisi_qm_status status;
231
	const struct hisi_qm_err_ini *err_ini;
232 233
	struct hisi_qm_err_status err_status;
	unsigned long reset_flag;
234

235
	struct rw_semaphore qps_lock;
236 237
	struct idr qp_idr;
	struct hisi_qp *qp_array;
238 239 240 241 242

	struct mutex mailbox_lock;

	const struct hisi_qm_hw_ops *ops;

243 244
	struct qm_debug debug;

245 246
	u32 error_mask;

247 248
	struct workqueue_struct *wq;
	struct work_struct work;
249
	struct work_struct rst_work;
250

251 252
	const char *algs;
	bool use_sva;
253
	bool is_frozen;
254 255 256
	resource_size_t phys_base;
	resource_size_t phys_size;
	struct uacce_device *uacce;
257
	int mode;
258 259 260 261 262 263 264
};

struct hisi_qp_status {
	atomic_t used;
	u16 sq_tail;
	u16 cq_head;
	bool cqc_phase;
265
	atomic_t flags;
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
};

struct hisi_qp_ops {
	int (*fill_sqe)(void *sqe, void *q_parm, void *d_parm);
};

struct hisi_qp {
	u32 qp_id;
	u8 alg_type;
	u8 req_type;

	struct qm_dma qdma;
	void *sqe;
	struct qm_cqe *cqe;
	dma_addr_t sqe_dma;
	dma_addr_t cqe_dma;

	struct hisi_qp_status qp_status;
	struct hisi_qp_ops *hw_ops;
	void *qp_ctx;
	void (*req_cb)(struct hisi_qp *qp, void *data);
287
	void (*event_cb)(struct hisi_qp *qp);
288 289

	struct hisi_qm *qm;
290
	bool is_resetting;
291 292
	u16 pasid;
	struct uacce_queue *uacce_q;
293 294
};

295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
static inline int q_num_set(const char *val, const struct kernel_param *kp,
			    unsigned int device)
{
	struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI,
					      device, NULL);
	u32 n, q_num;
	int ret;

	if (!val)
		return -EINVAL;

	if (!pdev) {
		q_num = min_t(u32, QM_QNUM_V1, QM_QNUM_V2);
		pr_info("No device found currently, suppose queue number is %d\n",
			q_num);
	} else {
311
		if (pdev->revision == QM_HW_V1)
312
			q_num = QM_QNUM_V1;
313
		else
314 315 316 317
			q_num = QM_QNUM_V2;
	}

	ret = kstrtou32(val, 10, &n);
318
	if (ret || n < QM_MIN_QNUM || n > q_num)
319 320 321 322 323
		return -EINVAL;

	return param_set_int(val, kp);
}

324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
static inline int vfs_num_set(const char *val, const struct kernel_param *kp)
{
	u32 n;
	int ret;

	if (!val)
		return -EINVAL;

	ret = kstrtou32(val, 10, &n);
	if (ret < 0)
		return ret;

	if (n > QM_MAX_VFS_NUM_V2)
		return -EINVAL;

	return param_set_int(val, kp);
}

342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362
static inline int mode_set(const char *val, const struct kernel_param *kp)
{
	u32 n;
	int ret;

	if (!val)
		return -EINVAL;

	ret = kstrtou32(val, 10, &n);
	if (ret != 0 || (n != UACCE_MODE_SVA &&
			 n != UACCE_MODE_NOUACCE))
		return -EINVAL;

	return param_set_int(val, kp);
}

static inline int uacce_mode_set(const char *val, const struct kernel_param *kp)
{
	return mode_set(val, kp);
}

363 364 365 366 367 368
static inline void hisi_qm_init_list(struct hisi_qm_list *qm_list)
{
	INIT_LIST_HEAD(&qm_list->list);
	mutex_init(&qm_list->lock);
}

369 370 371
int hisi_qm_init(struct hisi_qm *qm);
void hisi_qm_uninit(struct hisi_qm *qm);
int hisi_qm_start(struct hisi_qm *qm);
372
int hisi_qm_stop(struct hisi_qm *qm, enum qm_stop_reason r);
373 374 375 376 377
struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type);
int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg);
int hisi_qm_stop_qp(struct hisi_qp *qp);
void hisi_qm_release_qp(struct hisi_qp *qp);
int hisi_qp_send(struct hisi_qp *qp, const void *msg);
378
int hisi_qm_get_free_qp_num(struct hisi_qm *qm);
379
int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number);
380
void hisi_qm_debug_init(struct hisi_qm *qm);
381
enum qm_hw_ver hisi_qm_get_hw_version(struct pci_dev *pdev);
382
void hisi_qm_debug_regs_clear(struct hisi_qm *qm);
383
int hisi_qm_sriov_enable(struct pci_dev *pdev, int max_vfs);
384
int hisi_qm_sriov_disable(struct pci_dev *pdev, bool is_frozen);
385
int hisi_qm_sriov_configure(struct pci_dev *pdev, int num_vfs);
386 387
void hisi_qm_dev_err_init(struct hisi_qm *qm);
void hisi_qm_dev_err_uninit(struct hisi_qm *qm);
388 389
pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev,
					  pci_channel_state_t state);
390
pci_ers_result_t hisi_qm_dev_slot_reset(struct pci_dev *pdev);
S
Shukun Tan 已提交
391 392
void hisi_qm_reset_prepare(struct pci_dev *pdev);
void hisi_qm_reset_done(struct pci_dev *pdev);
393 394 395 396 397 398 399 400 401 402 403

struct hisi_acc_sgl_pool;
struct hisi_acc_hw_sgl *hisi_acc_sg_buf_map_to_hw_sgl(struct device *dev,
	struct scatterlist *sgl, struct hisi_acc_sgl_pool *pool,
	u32 index, dma_addr_t *hw_sgl_dma);
void hisi_acc_sg_buf_unmap(struct device *dev, struct scatterlist *sgl,
			   struct hisi_acc_hw_sgl *hw_sgl);
struct hisi_acc_sgl_pool *hisi_acc_create_sgl_pool(struct device *dev,
						   u32 count, u32 sge_nr);
void hisi_acc_free_sgl_pool(struct device *dev,
			    struct hisi_acc_sgl_pool *pool);
404 405 406
int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num,
			   u8 alg_type, int node, struct hisi_qp **qps);
void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num);
407
void hisi_qm_dev_shutdown(struct pci_dev *pdev);
408
void hisi_qm_wait_task_finish(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
409 410
int hisi_qm_alg_register(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
void hisi_qm_alg_unregister(struct hisi_qm *qm, struct hisi_qm_list *qm_list);
411
#endif