1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 HiSilicon Limited. */ 3 #include <asm/page.h> 4 #include <linux/bitmap.h> 5 #include <linux/debugfs.h> 6 #include <linux/dma-mapping.h> 7 #include <linux/io.h> 8 #include <linux/irqreturn.h> 9 #include <linux/log2.h> 10 #include <linux/seq_file.h> 11 #include <linux/slab.h> 12 #include <linux/uacce.h> 13 #include <linux/uaccess.h> 14 #include <uapi/misc/uacce/hisi_qm.h> 15 #include "qm.h" 16 17 /* eq/aeq irq enable */ 18 #define QM_VF_AEQ_INT_SOURCE 0x0 19 #define QM_VF_AEQ_INT_MASK 0x4 20 #define QM_VF_EQ_INT_SOURCE 0x8 21 #define QM_VF_EQ_INT_MASK 0xc 22 #define QM_IRQ_NUM_V1 1 23 #define QM_IRQ_NUM_PF_V2 4 24 #define QM_IRQ_NUM_VF_V2 2 25 26 #define QM_EQ_EVENT_IRQ_VECTOR 0 27 #define QM_AEQ_EVENT_IRQ_VECTOR 1 28 #define QM_ABNORMAL_EVENT_IRQ_VECTOR 3 29 30 /* mailbox */ 31 #define QM_MB_CMD_SQC 0x0 32 #define QM_MB_CMD_CQC 0x1 33 #define QM_MB_CMD_EQC 0x2 34 #define QM_MB_CMD_AEQC 0x3 35 #define QM_MB_CMD_SQC_BT 0x4 36 #define QM_MB_CMD_CQC_BT 0x5 37 #define QM_MB_CMD_SQC_VFT_V2 0x6 38 39 #define QM_MB_CMD_SEND_BASE 0x300 40 #define QM_MB_EVENT_SHIFT 8 41 #define QM_MB_BUSY_SHIFT 13 42 #define QM_MB_OP_SHIFT 14 43 #define QM_MB_CMD_DATA_ADDR_L 0x304 44 #define QM_MB_CMD_DATA_ADDR_H 0x308 45 46 /* sqc shift */ 47 #define QM_SQ_HOP_NUM_SHIFT 0 48 #define QM_SQ_PAGE_SIZE_SHIFT 4 49 #define QM_SQ_BUF_SIZE_SHIFT 8 50 #define QM_SQ_SQE_SIZE_SHIFT 12 51 #define QM_SQ_PRIORITY_SHIFT 0 52 #define QM_SQ_ORDERS_SHIFT 4 53 #define QM_SQ_TYPE_SHIFT 8 54 55 #define QM_SQ_TYPE_MASK GENMASK(3, 0) 56 57 /* cqc shift */ 58 #define QM_CQ_HOP_NUM_SHIFT 0 59 #define QM_CQ_PAGE_SIZE_SHIFT 4 60 #define QM_CQ_BUF_SIZE_SHIFT 8 61 #define QM_CQ_CQE_SIZE_SHIFT 12 62 #define QM_CQ_PHASE_SHIFT 0 63 #define QM_CQ_FLAG_SHIFT 1 64 65 #define QM_CQE_PHASE(cqe) (le16_to_cpu((cqe)->w7) & 0x1) 66 #define QM_QC_CQE_SIZE 4 67 68 /* eqc shift */ 69 #define QM_EQE_AEQE_SIZE (2UL << 12) 70 #define QM_EQC_PHASE_SHIFT 16 71 72 #define QM_EQE_PHASE(eqe) ((le32_to_cpu((eqe)->dw0) >> 16) & 0x1) 73 #define QM_EQE_CQN_MASK GENMASK(15, 0) 74 75 #define QM_AEQE_PHASE(aeqe) ((le32_to_cpu((aeqe)->dw0) >> 16) & 0x1) 76 #define QM_AEQE_TYPE_SHIFT 17 77 78 #define QM_DOORBELL_CMD_SQ 0 79 #define QM_DOORBELL_CMD_CQ 1 80 #define QM_DOORBELL_CMD_EQ 2 81 #define QM_DOORBELL_CMD_AEQ 3 82 83 #define QM_DOORBELL_BASE_V1 0x340 84 #define QM_DB_CMD_SHIFT_V1 16 85 #define QM_DB_INDEX_SHIFT_V1 32 86 #define QM_DB_PRIORITY_SHIFT_V1 48 87 #define QM_DOORBELL_SQ_CQ_BASE_V2 0x1000 88 #define QM_DOORBELL_EQ_AEQ_BASE_V2 0x2000 89 #define QM_DB_CMD_SHIFT_V2 12 90 #define QM_DB_RAND_SHIFT_V2 16 91 #define QM_DB_INDEX_SHIFT_V2 32 92 #define QM_DB_PRIORITY_SHIFT_V2 48 93 94 #define QM_MEM_START_INIT 0x100040 95 #define QM_MEM_INIT_DONE 0x100044 96 #define QM_VFT_CFG_RDY 0x10006c 97 #define QM_VFT_CFG_OP_WR 0x100058 98 #define QM_VFT_CFG_TYPE 0x10005c 99 #define QM_SQC_VFT 0x0 100 #define QM_CQC_VFT 0x1 101 #define QM_VFT_CFG 0x100060 102 #define QM_VFT_CFG_OP_ENABLE 0x100054 103 104 #define QM_VFT_CFG_DATA_L 0x100064 105 #define QM_VFT_CFG_DATA_H 0x100068 106 #define QM_SQC_VFT_BUF_SIZE (7ULL << 8) 107 #define QM_SQC_VFT_SQC_SIZE (5ULL << 12) 108 #define QM_SQC_VFT_INDEX_NUMBER (1ULL << 16) 109 #define QM_SQC_VFT_START_SQN_SHIFT 28 110 #define QM_SQC_VFT_VALID (1ULL << 44) 111 #define QM_SQC_VFT_SQN_SHIFT 45 112 #define QM_CQC_VFT_BUF_SIZE (7ULL << 8) 113 #define QM_CQC_VFT_SQC_SIZE (5ULL << 12) 114 #define QM_CQC_VFT_INDEX_NUMBER (1ULL << 16) 115 #define QM_CQC_VFT_VALID (1ULL << 28) 116 117 #define QM_SQC_VFT_BASE_SHIFT_V2 28 118 #define QM_SQC_VFT_BASE_MASK_V2 GENMASK(5, 0) 119 #define QM_SQC_VFT_NUM_SHIFT_V2 45 120 #define QM_SQC_VFT_NUM_MASK_v2 GENMASK(9, 0) 121 122 #define QM_DFX_CNT_CLR_CE 0x100118 123 124 #define QM_ABNORMAL_INT_SOURCE 0x100000 125 #define QM_ABNORMAL_INT_MASK 0x100004 126 #define QM_ABNORMAL_INT_MASK_VALUE 0x1fff 127 #define QM_ABNORMAL_INT_STATUS 0x100008 128 #define QM_ABNORMAL_INF00 0x100010 129 #define QM_FIFO_OVERFLOW_TYPE 0xc0 130 #define QM_FIFO_OVERFLOW_TYPE_SHIFT 6 131 #define QM_FIFO_OVERFLOW_VF 0x3f 132 #define QM_ABNORMAL_INF01 0x100014 133 #define QM_DB_TIMEOUT_TYPE 0xc0 134 #define QM_DB_TIMEOUT_TYPE_SHIFT 6 135 #define QM_DB_TIMEOUT_VF 0x3f 136 #define QM_RAS_CE_ENABLE 0x1000ec 137 #define QM_RAS_FE_ENABLE 0x1000f0 138 #define QM_RAS_NFE_ENABLE 0x1000f4 139 #define QM_RAS_CE_THRESHOLD 0x1000f8 140 #define QM_RAS_CE_TIMES_PER_IRQ 1 141 #define QM_RAS_MSI_INT_SEL 0x1040f4 142 143 #define QM_CACHE_WB_START 0x204 144 #define QM_CACHE_WB_DONE 0x208 145 146 #define PCI_BAR_2 2 147 #define QM_SQE_DATA_ALIGN_MASK GENMASK(6, 0) 148 #define QMC_ALIGN(sz) ALIGN(sz, 32) 149 150 #define QM_DBG_TMP_BUF_LEN 22 151 152 #define QM_MK_CQC_DW3_V1(hop_num, pg_sz, buf_sz, cqe_sz) \ 153 (((hop_num) << QM_CQ_HOP_NUM_SHIFT) | \ 154 ((pg_sz) << QM_CQ_PAGE_SIZE_SHIFT) | \ 155 ((buf_sz) << QM_CQ_BUF_SIZE_SHIFT) | \ 156 ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 157 158 #define QM_MK_CQC_DW3_V2(cqe_sz) \ 159 ((QM_Q_DEPTH - 1) | ((cqe_sz) << QM_CQ_CQE_SIZE_SHIFT)) 160 161 #define QM_MK_SQC_W13(priority, orders, alg_type) \ 162 (((priority) << QM_SQ_PRIORITY_SHIFT) | \ 163 ((orders) << QM_SQ_ORDERS_SHIFT) | \ 164 (((alg_type) & QM_SQ_TYPE_MASK) << QM_SQ_TYPE_SHIFT)) 165 166 #define QM_MK_SQC_DW3_V1(hop_num, pg_sz, buf_sz, sqe_sz) \ 167 (((hop_num) << QM_SQ_HOP_NUM_SHIFT) | \ 168 ((pg_sz) << QM_SQ_PAGE_SIZE_SHIFT) | \ 169 ((buf_sz) << QM_SQ_BUF_SIZE_SHIFT) | \ 170 ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 171 172 #define QM_MK_SQC_DW3_V2(sqe_sz) \ 173 ((QM_Q_DEPTH - 1) | ((u32)ilog2(sqe_sz) << QM_SQ_SQE_SIZE_SHIFT)) 174 175 #define INIT_QC_COMMON(qc, base, pasid) do { \ 176 (qc)->head = 0; \ 177 (qc)->tail = 0; \ 178 (qc)->base_l = cpu_to_le32(lower_32_bits(base)); \ 179 (qc)->base_h = cpu_to_le32(upper_32_bits(base)); \ 180 (qc)->dw3 = 0; \ 181 (qc)->w8 = 0; \ 182 (qc)->rsvd0 = 0; \ 183 (qc)->pasid = cpu_to_le16(pasid); \ 184 (qc)->w11 = 0; \ 185 (qc)->rsvd1 = 0; \ 186 } while (0) 187 188 enum vft_type { 189 SQC_VFT = 0, 190 CQC_VFT, 191 }; 192 193 struct qm_cqe { 194 __le32 rsvd0; 195 __le16 cmd_id; 196 __le16 rsvd1; 197 __le16 sq_head; 198 __le16 sq_num; 199 __le16 rsvd2; 200 __le16 w7; 201 }; 202 203 struct qm_eqe { 204 __le32 dw0; 205 }; 206 207 struct qm_aeqe { 208 __le32 dw0; 209 }; 210 211 struct qm_sqc { 212 __le16 head; 213 __le16 tail; 214 __le32 base_l; 215 __le32 base_h; 216 __le32 dw3; 217 __le16 w8; 218 __le16 rsvd0; 219 __le16 pasid; 220 __le16 w11; 221 __le16 cq_num; 222 __le16 w13; 223 __le32 rsvd1; 224 }; 225 226 struct qm_cqc { 227 __le16 head; 228 __le16 tail; 229 __le32 base_l; 230 __le32 base_h; 231 __le32 dw3; 232 __le16 w8; 233 __le16 rsvd0; 234 __le16 pasid; 235 __le16 w11; 236 __le32 dw6; 237 __le32 rsvd1; 238 }; 239 240 struct qm_eqc { 241 __le16 head; 242 __le16 tail; 243 __le32 base_l; 244 __le32 base_h; 245 __le32 dw3; 246 __le32 rsvd[2]; 247 __le32 dw6; 248 }; 249 250 struct qm_aeqc { 251 __le16 head; 252 __le16 tail; 253 __le32 base_l; 254 __le32 base_h; 255 __le32 dw3; 256 __le32 rsvd[2]; 257 __le32 dw6; 258 }; 259 260 struct qm_mailbox { 261 __le16 w0; 262 __le16 queue_num; 263 __le32 base_l; 264 __le32 base_h; 265 __le32 rsvd; 266 }; 267 268 struct qm_doorbell { 269 __le16 queue_num; 270 __le16 cmd; 271 __le16 index; 272 __le16 priority; 273 }; 274 275 struct hisi_qm_resource { 276 struct hisi_qm *qm; 277 int distance; 278 struct list_head list; 279 }; 280 281 struct hisi_qm_hw_ops { 282 int (*get_vft)(struct hisi_qm *qm, u32 *base, u32 *number); 283 void (*qm_db)(struct hisi_qm *qm, u16 qn, 284 u8 cmd, u16 index, u8 priority); 285 u32 (*get_irq_num)(struct hisi_qm *qm); 286 int (*debug_init)(struct hisi_qm *qm); 287 void (*hw_error_init)(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe, 288 u32 msi); 289 void (*hw_error_uninit)(struct hisi_qm *qm); 290 pci_ers_result_t (*hw_error_handle)(struct hisi_qm *qm); 291 }; 292 293 static const char * const qm_debug_file_name[] = { 294 [CURRENT_Q] = "current_q", 295 [CLEAR_ENABLE] = "clear_enable", 296 }; 297 298 struct hisi_qm_hw_error { 299 u32 int_msk; 300 const char *msg; 301 }; 302 303 static const struct hisi_qm_hw_error qm_hw_error[] = { 304 { .int_msk = BIT(0), .msg = "qm_axi_rresp" }, 305 { .int_msk = BIT(1), .msg = "qm_axi_bresp" }, 306 { .int_msk = BIT(2), .msg = "qm_ecc_mbit" }, 307 { .int_msk = BIT(3), .msg = "qm_ecc_1bit" }, 308 { .int_msk = BIT(4), .msg = "qm_acc_get_task_timeout" }, 309 { .int_msk = BIT(5), .msg = "qm_acc_do_task_timeout" }, 310 { .int_msk = BIT(6), .msg = "qm_acc_wb_not_ready_timeout" }, 311 { .int_msk = BIT(7), .msg = "qm_sq_cq_vf_invalid" }, 312 { .int_msk = BIT(8), .msg = "qm_cq_vf_invalid" }, 313 { .int_msk = BIT(9), .msg = "qm_sq_vf_invalid" }, 314 { .int_msk = BIT(10), .msg = "qm_db_timeout" }, 315 { .int_msk = BIT(11), .msg = "qm_of_fifo_of" }, 316 { .int_msk = BIT(12), .msg = "qm_db_random_invalid" }, 317 { /* sentinel */ } 318 }; 319 320 static const char * const qm_db_timeout[] = { 321 "sq", "cq", "eq", "aeq", 322 }; 323 324 static const char * const qm_fifo_overflow[] = { 325 "cq", "eq", "aeq", 326 }; 327 328 /* return 0 mailbox ready, -ETIMEDOUT hardware timeout */ 329 static int qm_wait_mb_ready(struct hisi_qm *qm) 330 { 331 u32 val; 332 333 return readl_relaxed_poll_timeout(qm->io_base + QM_MB_CMD_SEND_BASE, 334 val, !((val >> QM_MB_BUSY_SHIFT) & 335 0x1), 10, 1000); 336 } 337 338 /* 128 bit should be written to hardware at one time to trigger a mailbox */ 339 static void qm_mb_write(struct hisi_qm *qm, const void *src) 340 { 341 void __iomem *fun_base = qm->io_base + QM_MB_CMD_SEND_BASE; 342 unsigned long tmp0 = 0, tmp1 = 0; 343 344 if (!IS_ENABLED(CONFIG_ARM64)) { 345 memcpy_toio(fun_base, src, 16); 346 wmb(); 347 return; 348 } 349 350 asm volatile("ldp %0, %1, %3\n" 351 "stp %0, %1, %2\n" 352 "dsb sy\n" 353 : "=&r" (tmp0), 354 "=&r" (tmp1), 355 "+Q" (*((char __iomem *)fun_base)) 356 : "Q" (*((char *)src)) 357 : "memory"); 358 } 359 360 static int qm_mb(struct hisi_qm *qm, u8 cmd, dma_addr_t dma_addr, u16 queue, 361 bool op) 362 { 363 struct qm_mailbox mailbox; 364 int ret = 0; 365 366 dev_dbg(&qm->pdev->dev, "QM mailbox request to q%u: %u-%llx\n", 367 queue, cmd, (unsigned long long)dma_addr); 368 369 mailbox.w0 = cpu_to_le16(cmd | 370 (op ? 0x1 << QM_MB_OP_SHIFT : 0) | 371 (0x1 << QM_MB_BUSY_SHIFT)); 372 mailbox.queue_num = cpu_to_le16(queue); 373 mailbox.base_l = cpu_to_le32(lower_32_bits(dma_addr)); 374 mailbox.base_h = cpu_to_le32(upper_32_bits(dma_addr)); 375 mailbox.rsvd = 0; 376 377 mutex_lock(&qm->mailbox_lock); 378 379 if (unlikely(qm_wait_mb_ready(qm))) { 380 ret = -EBUSY; 381 dev_err(&qm->pdev->dev, "QM mailbox is busy to start!\n"); 382 goto busy_unlock; 383 } 384 385 qm_mb_write(qm, &mailbox); 386 387 if (unlikely(qm_wait_mb_ready(qm))) { 388 ret = -EBUSY; 389 dev_err(&qm->pdev->dev, "QM mailbox operation timeout!\n"); 390 goto busy_unlock; 391 } 392 393 busy_unlock: 394 mutex_unlock(&qm->mailbox_lock); 395 396 return ret; 397 } 398 399 static void qm_db_v1(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 400 { 401 u64 doorbell; 402 403 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V1) | 404 ((u64)index << QM_DB_INDEX_SHIFT_V1) | 405 ((u64)priority << QM_DB_PRIORITY_SHIFT_V1); 406 407 writeq(doorbell, qm->io_base + QM_DOORBELL_BASE_V1); 408 } 409 410 static void qm_db_v2(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 411 { 412 u64 doorbell; 413 u64 dbase; 414 u16 randata = 0; 415 416 if (cmd == QM_DOORBELL_CMD_SQ || cmd == QM_DOORBELL_CMD_CQ) 417 dbase = QM_DOORBELL_SQ_CQ_BASE_V2; 418 else 419 dbase = QM_DOORBELL_EQ_AEQ_BASE_V2; 420 421 doorbell = qn | ((u64)cmd << QM_DB_CMD_SHIFT_V2) | 422 ((u64)randata << QM_DB_RAND_SHIFT_V2) | 423 ((u64)index << QM_DB_INDEX_SHIFT_V2) | 424 ((u64)priority << QM_DB_PRIORITY_SHIFT_V2); 425 426 writeq(doorbell, qm->io_base + dbase); 427 } 428 429 static void qm_db(struct hisi_qm *qm, u16 qn, u8 cmd, u16 index, u8 priority) 430 { 431 dev_dbg(&qm->pdev->dev, "QM doorbell request: qn=%u, cmd=%u, index=%u\n", 432 qn, cmd, index); 433 434 qm->ops->qm_db(qm, qn, cmd, index, priority); 435 } 436 437 static int qm_dev_mem_reset(struct hisi_qm *qm) 438 { 439 u32 val; 440 441 writel(0x1, qm->io_base + QM_MEM_START_INIT); 442 return readl_relaxed_poll_timeout(qm->io_base + QM_MEM_INIT_DONE, val, 443 val & BIT(0), 10, 1000); 444 } 445 446 static u32 qm_get_irq_num_v1(struct hisi_qm *qm) 447 { 448 return QM_IRQ_NUM_V1; 449 } 450 451 static u32 qm_get_irq_num_v2(struct hisi_qm *qm) 452 { 453 if (qm->fun_type == QM_HW_PF) 454 return QM_IRQ_NUM_PF_V2; 455 else 456 return QM_IRQ_NUM_VF_V2; 457 } 458 459 static struct hisi_qp *qm_to_hisi_qp(struct hisi_qm *qm, struct qm_eqe *eqe) 460 { 461 u16 cqn = le32_to_cpu(eqe->dw0) & QM_EQE_CQN_MASK; 462 463 return qm->qp_array[cqn]; 464 } 465 466 static void qm_cq_head_update(struct hisi_qp *qp) 467 { 468 if (qp->qp_status.cq_head == QM_Q_DEPTH - 1) { 469 qp->qp_status.cqc_phase = !qp->qp_status.cqc_phase; 470 qp->qp_status.cq_head = 0; 471 } else { 472 qp->qp_status.cq_head++; 473 } 474 } 475 476 static void qm_poll_qp(struct hisi_qp *qp, struct hisi_qm *qm) 477 { 478 if (qp->event_cb) { 479 qp->event_cb(qp); 480 return; 481 } 482 483 if (qp->req_cb) { 484 struct qm_cqe *cqe = qp->cqe + qp->qp_status.cq_head; 485 486 while (QM_CQE_PHASE(cqe) == qp->qp_status.cqc_phase) { 487 dma_rmb(); 488 qp->req_cb(qp, qp->sqe + qm->sqe_size * 489 le16_to_cpu(cqe->sq_head)); 490 qm_cq_head_update(qp); 491 cqe = qp->cqe + qp->qp_status.cq_head; 492 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, 493 qp->qp_status.cq_head, 0); 494 atomic_dec(&qp->qp_status.used); 495 } 496 497 /* set c_flag */ 498 qm_db(qm, qp->qp_id, QM_DOORBELL_CMD_CQ, 499 qp->qp_status.cq_head, 1); 500 } 501 } 502 503 static void qm_work_process(struct work_struct *work) 504 { 505 struct hisi_qm *qm = container_of(work, struct hisi_qm, work); 506 struct qm_eqe *eqe = qm->eqe + qm->status.eq_head; 507 struct hisi_qp *qp; 508 int eqe_num = 0; 509 510 while (QM_EQE_PHASE(eqe) == qm->status.eqc_phase) { 511 eqe_num++; 512 qp = qm_to_hisi_qp(qm, eqe); 513 if (qp) 514 qm_poll_qp(qp, qm); 515 516 if (qm->status.eq_head == QM_Q_DEPTH - 1) { 517 qm->status.eqc_phase = !qm->status.eqc_phase; 518 eqe = qm->eqe; 519 qm->status.eq_head = 0; 520 } else { 521 eqe++; 522 qm->status.eq_head++; 523 } 524 525 if (eqe_num == QM_Q_DEPTH / 2 - 1) { 526 eqe_num = 0; 527 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 528 } 529 } 530 531 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 532 } 533 534 static irqreturn_t do_qm_irq(int irq, void *data) 535 { 536 struct hisi_qm *qm = (struct hisi_qm *)data; 537 538 /* the workqueue created by device driver of QM */ 539 if (qm->wq) 540 queue_work(qm->wq, &qm->work); 541 else 542 schedule_work(&qm->work); 543 544 return IRQ_HANDLED; 545 } 546 547 static irqreturn_t qm_irq(int irq, void *data) 548 { 549 struct hisi_qm *qm = data; 550 551 if (readl(qm->io_base + QM_VF_EQ_INT_SOURCE)) 552 return do_qm_irq(irq, data); 553 554 dev_err(&qm->pdev->dev, "invalid int source\n"); 555 qm_db(qm, 0, QM_DOORBELL_CMD_EQ, qm->status.eq_head, 0); 556 557 return IRQ_NONE; 558 } 559 560 static irqreturn_t qm_aeq_irq(int irq, void *data) 561 { 562 struct hisi_qm *qm = data; 563 struct qm_aeqe *aeqe = qm->aeqe + qm->status.aeq_head; 564 u32 type; 565 566 if (!readl(qm->io_base + QM_VF_AEQ_INT_SOURCE)) 567 return IRQ_NONE; 568 569 while (QM_AEQE_PHASE(aeqe) == qm->status.aeqc_phase) { 570 type = le32_to_cpu(aeqe->dw0) >> QM_AEQE_TYPE_SHIFT; 571 if (type < ARRAY_SIZE(qm_fifo_overflow)) 572 dev_err(&qm->pdev->dev, "%s overflow\n", 573 qm_fifo_overflow[type]); 574 else 575 dev_err(&qm->pdev->dev, "unknown error type %d\n", 576 type); 577 578 if (qm->status.aeq_head == QM_Q_DEPTH - 1) { 579 qm->status.aeqc_phase = !qm->status.aeqc_phase; 580 aeqe = qm->aeqe; 581 qm->status.aeq_head = 0; 582 } else { 583 aeqe++; 584 qm->status.aeq_head++; 585 } 586 587 qm_db(qm, 0, QM_DOORBELL_CMD_AEQ, qm->status.aeq_head, 0); 588 } 589 590 return IRQ_HANDLED; 591 } 592 593 static irqreturn_t qm_abnormal_irq(int irq, void *data) 594 { 595 const struct hisi_qm_hw_error *err = qm_hw_error; 596 struct hisi_qm *qm = data; 597 struct device *dev = &qm->pdev->dev; 598 u32 error_status, tmp; 599 600 /* read err sts */ 601 tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 602 error_status = qm->msi_mask & tmp; 603 604 while (err->msg) { 605 if (err->int_msk & error_status) 606 dev_err(dev, "%s [error status=0x%x] found\n", 607 err->msg, err->int_msk); 608 609 err++; 610 } 611 612 /* clear err sts */ 613 writel(error_status, qm->io_base + QM_ABNORMAL_INT_SOURCE); 614 615 return IRQ_HANDLED; 616 } 617 618 static int qm_irq_register(struct hisi_qm *qm) 619 { 620 struct pci_dev *pdev = qm->pdev; 621 int ret; 622 623 ret = request_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), 624 qm_irq, IRQF_SHARED, qm->dev_name, qm); 625 if (ret) 626 return ret; 627 628 if (qm->ver == QM_HW_V2) { 629 ret = request_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), 630 qm_aeq_irq, IRQF_SHARED, qm->dev_name, qm); 631 if (ret) 632 goto err_aeq_irq; 633 634 if (qm->fun_type == QM_HW_PF) { 635 ret = request_irq(pci_irq_vector(pdev, 636 QM_ABNORMAL_EVENT_IRQ_VECTOR), 637 qm_abnormal_irq, IRQF_SHARED, 638 qm->dev_name, qm); 639 if (ret) 640 goto err_abonormal_irq; 641 } 642 } 643 644 return 0; 645 646 err_abonormal_irq: 647 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm); 648 err_aeq_irq: 649 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm); 650 return ret; 651 } 652 653 static void qm_irq_unregister(struct hisi_qm *qm) 654 { 655 struct pci_dev *pdev = qm->pdev; 656 657 free_irq(pci_irq_vector(pdev, QM_EQ_EVENT_IRQ_VECTOR), qm); 658 659 if (qm->ver == QM_HW_V2) { 660 free_irq(pci_irq_vector(pdev, QM_AEQ_EVENT_IRQ_VECTOR), qm); 661 662 if (qm->fun_type == QM_HW_PF) 663 free_irq(pci_irq_vector(pdev, 664 QM_ABNORMAL_EVENT_IRQ_VECTOR), qm); 665 } 666 } 667 668 static void qm_init_qp_status(struct hisi_qp *qp) 669 { 670 struct hisi_qp_status *qp_status = &qp->qp_status; 671 672 qp_status->sq_tail = 0; 673 qp_status->cq_head = 0; 674 qp_status->cqc_phase = true; 675 qp_status->flags = 0; 676 } 677 678 static void qm_vft_data_cfg(struct hisi_qm *qm, enum vft_type type, u32 base, 679 u32 number) 680 { 681 u64 tmp = 0; 682 683 if (number > 0) { 684 switch (type) { 685 case SQC_VFT: 686 switch (qm->ver) { 687 case QM_HW_V1: 688 tmp = QM_SQC_VFT_BUF_SIZE | 689 QM_SQC_VFT_SQC_SIZE | 690 QM_SQC_VFT_INDEX_NUMBER | 691 QM_SQC_VFT_VALID | 692 (u64)base << QM_SQC_VFT_START_SQN_SHIFT; 693 break; 694 case QM_HW_V2: 695 tmp = (u64)base << QM_SQC_VFT_START_SQN_SHIFT | 696 QM_SQC_VFT_VALID | 697 (u64)(number - 1) << QM_SQC_VFT_SQN_SHIFT; 698 break; 699 case QM_HW_UNKNOWN: 700 break; 701 } 702 break; 703 case CQC_VFT: 704 switch (qm->ver) { 705 case QM_HW_V1: 706 tmp = QM_CQC_VFT_BUF_SIZE | 707 QM_CQC_VFT_SQC_SIZE | 708 QM_CQC_VFT_INDEX_NUMBER | 709 QM_CQC_VFT_VALID; 710 break; 711 case QM_HW_V2: 712 tmp = QM_CQC_VFT_VALID; 713 break; 714 case QM_HW_UNKNOWN: 715 break; 716 } 717 break; 718 } 719 } 720 721 writel(lower_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_L); 722 writel(upper_32_bits(tmp), qm->io_base + QM_VFT_CFG_DATA_H); 723 } 724 725 static int qm_set_vft_common(struct hisi_qm *qm, enum vft_type type, 726 u32 fun_num, u32 base, u32 number) 727 { 728 unsigned int val; 729 int ret; 730 731 ret = readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 732 val & BIT(0), 10, 1000); 733 if (ret) 734 return ret; 735 736 writel(0x0, qm->io_base + QM_VFT_CFG_OP_WR); 737 writel(type, qm->io_base + QM_VFT_CFG_TYPE); 738 writel(fun_num, qm->io_base + QM_VFT_CFG); 739 740 qm_vft_data_cfg(qm, type, base, number); 741 742 writel(0x0, qm->io_base + QM_VFT_CFG_RDY); 743 writel(0x1, qm->io_base + QM_VFT_CFG_OP_ENABLE); 744 745 return readl_relaxed_poll_timeout(qm->io_base + QM_VFT_CFG_RDY, val, 746 val & BIT(0), 10, 1000); 747 } 748 749 /* The config should be conducted after qm_dev_mem_reset() */ 750 static int qm_set_sqc_cqc_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 751 u32 number) 752 { 753 int ret, i; 754 755 for (i = SQC_VFT; i <= CQC_VFT; i++) { 756 ret = qm_set_vft_common(qm, i, fun_num, base, number); 757 if (ret) 758 return ret; 759 } 760 761 return 0; 762 } 763 764 static int qm_get_vft_v2(struct hisi_qm *qm, u32 *base, u32 *number) 765 { 766 u64 sqc_vft; 767 int ret; 768 769 ret = qm_mb(qm, QM_MB_CMD_SQC_VFT_V2, 0, 0, 1); 770 if (ret) 771 return ret; 772 773 sqc_vft = readl(qm->io_base + QM_MB_CMD_DATA_ADDR_L) | 774 ((u64)readl(qm->io_base + QM_MB_CMD_DATA_ADDR_H) << 32); 775 *base = QM_SQC_VFT_BASE_MASK_V2 & (sqc_vft >> QM_SQC_VFT_BASE_SHIFT_V2); 776 *number = (QM_SQC_VFT_NUM_MASK_v2 & 777 (sqc_vft >> QM_SQC_VFT_NUM_SHIFT_V2)) + 1; 778 779 return 0; 780 } 781 782 static struct hisi_qm *file_to_qm(struct debugfs_file *file) 783 { 784 struct qm_debug *debug = file->debug; 785 786 return container_of(debug, struct hisi_qm, debug); 787 } 788 789 static u32 current_q_read(struct debugfs_file *file) 790 { 791 struct hisi_qm *qm = file_to_qm(file); 792 793 return readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) >> QM_DFX_QN_SHIFT; 794 } 795 796 static int current_q_write(struct debugfs_file *file, u32 val) 797 { 798 struct hisi_qm *qm = file_to_qm(file); 799 u32 tmp; 800 801 if (val >= qm->debug.curr_qm_qp_num) 802 return -EINVAL; 803 804 tmp = val << QM_DFX_QN_SHIFT | 805 (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_FUN_MASK); 806 writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 807 808 tmp = val << QM_DFX_QN_SHIFT | 809 (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_FUN_MASK); 810 writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 811 812 return 0; 813 } 814 815 static u32 clear_enable_read(struct debugfs_file *file) 816 { 817 struct hisi_qm *qm = file_to_qm(file); 818 819 return readl(qm->io_base + QM_DFX_CNT_CLR_CE); 820 } 821 822 /* rd_clr_ctrl 1 enable read clear, otherwise 0 disable it */ 823 static int clear_enable_write(struct debugfs_file *file, u32 rd_clr_ctrl) 824 { 825 struct hisi_qm *qm = file_to_qm(file); 826 827 if (rd_clr_ctrl > 1) 828 return -EINVAL; 829 830 writel(rd_clr_ctrl, qm->io_base + QM_DFX_CNT_CLR_CE); 831 832 return 0; 833 } 834 835 static ssize_t qm_debug_read(struct file *filp, char __user *buf, 836 size_t count, loff_t *pos) 837 { 838 struct debugfs_file *file = filp->private_data; 839 enum qm_debug_file index = file->index; 840 char tbuf[QM_DBG_TMP_BUF_LEN]; 841 u32 val; 842 int ret; 843 844 mutex_lock(&file->lock); 845 switch (index) { 846 case CURRENT_Q: 847 val = current_q_read(file); 848 break; 849 case CLEAR_ENABLE: 850 val = clear_enable_read(file); 851 break; 852 default: 853 mutex_unlock(&file->lock); 854 return -EINVAL; 855 } 856 mutex_unlock(&file->lock); 857 ret = sprintf(tbuf, "%u\n", val); 858 return simple_read_from_buffer(buf, count, pos, tbuf, ret); 859 } 860 861 static ssize_t qm_debug_write(struct file *filp, const char __user *buf, 862 size_t count, loff_t *pos) 863 { 864 struct debugfs_file *file = filp->private_data; 865 enum qm_debug_file index = file->index; 866 unsigned long val; 867 char tbuf[QM_DBG_TMP_BUF_LEN]; 868 int len, ret; 869 870 if (*pos != 0) 871 return 0; 872 873 if (count >= QM_DBG_TMP_BUF_LEN) 874 return -ENOSPC; 875 876 len = simple_write_to_buffer(tbuf, QM_DBG_TMP_BUF_LEN - 1, pos, buf, 877 count); 878 if (len < 0) 879 return len; 880 881 tbuf[len] = '\0'; 882 if (kstrtoul(tbuf, 0, &val)) 883 return -EFAULT; 884 885 mutex_lock(&file->lock); 886 switch (index) { 887 case CURRENT_Q: 888 ret = current_q_write(file, val); 889 if (ret) 890 goto err_input; 891 break; 892 case CLEAR_ENABLE: 893 ret = clear_enable_write(file, val); 894 if (ret) 895 goto err_input; 896 break; 897 default: 898 ret = -EINVAL; 899 goto err_input; 900 } 901 mutex_unlock(&file->lock); 902 903 return count; 904 905 err_input: 906 mutex_unlock(&file->lock); 907 return ret; 908 } 909 910 static const struct file_operations qm_debug_fops = { 911 .owner = THIS_MODULE, 912 .open = simple_open, 913 .read = qm_debug_read, 914 .write = qm_debug_write, 915 }; 916 917 struct qm_dfx_registers { 918 char *reg_name; 919 u64 reg_offset; 920 }; 921 922 #define CNT_CYC_REGS_NUM 10 923 static struct qm_dfx_registers qm_dfx_regs[] = { 924 /* XXX_CNT are reading clear register */ 925 {"QM_ECC_1BIT_CNT ", 0x104000ull}, 926 {"QM_ECC_MBIT_CNT ", 0x104008ull}, 927 {"QM_DFX_MB_CNT ", 0x104018ull}, 928 {"QM_DFX_DB_CNT ", 0x104028ull}, 929 {"QM_DFX_SQE_CNT ", 0x104038ull}, 930 {"QM_DFX_CQE_CNT ", 0x104048ull}, 931 {"QM_DFX_SEND_SQE_TO_ACC_CNT ", 0x104050ull}, 932 {"QM_DFX_WB_SQE_FROM_ACC_CNT ", 0x104058ull}, 933 {"QM_DFX_ACC_FINISH_CNT ", 0x104060ull}, 934 {"QM_DFX_CQE_ERR_CNT ", 0x1040b4ull}, 935 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull}, 936 {"QM_ECC_1BIT_INF ", 0x104004ull}, 937 {"QM_ECC_MBIT_INF ", 0x10400cull}, 938 {"QM_DFX_ACC_RDY_VLD0 ", 0x1040a0ull}, 939 {"QM_DFX_ACC_RDY_VLD1 ", 0x1040a4ull}, 940 {"QM_DFX_AXI_RDY_VLD ", 0x1040a8ull}, 941 {"QM_DFX_FF_ST0 ", 0x1040c8ull}, 942 {"QM_DFX_FF_ST1 ", 0x1040ccull}, 943 {"QM_DFX_FF_ST2 ", 0x1040d0ull}, 944 {"QM_DFX_FF_ST3 ", 0x1040d4ull}, 945 {"QM_DFX_FF_ST4 ", 0x1040d8ull}, 946 {"QM_DFX_FF_ST5 ", 0x1040dcull}, 947 {"QM_DFX_FF_ST6 ", 0x1040e0ull}, 948 {"QM_IN_IDLE_ST ", 0x1040e4ull}, 949 { NULL, 0} 950 }; 951 952 static struct qm_dfx_registers qm_vf_dfx_regs[] = { 953 {"QM_DFX_FUNS_ACTIVE_ST ", 0x200ull}, 954 { NULL, 0} 955 }; 956 957 static int qm_regs_show(struct seq_file *s, void *unused) 958 { 959 struct hisi_qm *qm = s->private; 960 struct qm_dfx_registers *regs; 961 u32 val; 962 963 if (qm->fun_type == QM_HW_PF) 964 regs = qm_dfx_regs; 965 else 966 regs = qm_vf_dfx_regs; 967 968 while (regs->reg_name) { 969 val = readl(qm->io_base + regs->reg_offset); 970 seq_printf(s, "%s= 0x%08x\n", regs->reg_name, val); 971 regs++; 972 } 973 974 return 0; 975 } 976 977 static int qm_regs_open(struct inode *inode, struct file *file) 978 { 979 return single_open(file, qm_regs_show, inode->i_private); 980 } 981 982 static const struct file_operations qm_regs_fops = { 983 .owner = THIS_MODULE, 984 .open = qm_regs_open, 985 .read = seq_read, 986 .release = single_release, 987 }; 988 989 static int qm_create_debugfs_file(struct hisi_qm *qm, enum qm_debug_file index) 990 { 991 struct dentry *qm_d = qm->debug.qm_d; 992 struct debugfs_file *file = qm->debug.files + index; 993 994 debugfs_create_file(qm_debug_file_name[index], 0600, qm_d, file, 995 &qm_debug_fops); 996 997 file->index = index; 998 mutex_init(&file->lock); 999 file->debug = &qm->debug; 1000 1001 return 0; 1002 } 1003 1004 static void qm_hw_error_init_v1(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe, 1005 u32 msi) 1006 { 1007 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK); 1008 } 1009 1010 static void qm_hw_error_init_v2(struct hisi_qm *qm, u32 ce, u32 nfe, u32 fe, 1011 u32 msi) 1012 { 1013 u32 irq_enable = ce | nfe | fe | msi; 1014 u32 irq_unmask = ~irq_enable; 1015 1016 qm->error_mask = ce | nfe | fe; 1017 qm->msi_mask = msi; 1018 1019 /* configure error type */ 1020 writel(ce, qm->io_base + QM_RAS_CE_ENABLE); 1021 writel(QM_RAS_CE_TIMES_PER_IRQ, qm->io_base + QM_RAS_CE_THRESHOLD); 1022 writel(nfe, qm->io_base + QM_RAS_NFE_ENABLE); 1023 writel(fe, qm->io_base + QM_RAS_FE_ENABLE); 1024 1025 /* use RAS irq default, so only set QM_RAS_MSI_INT_SEL for MSI */ 1026 writel(msi, qm->io_base + QM_RAS_MSI_INT_SEL); 1027 1028 irq_unmask &= readl(qm->io_base + QM_ABNORMAL_INT_MASK); 1029 writel(irq_unmask, qm->io_base + QM_ABNORMAL_INT_MASK); 1030 } 1031 1032 static void qm_hw_error_uninit_v2(struct hisi_qm *qm) 1033 { 1034 writel(QM_ABNORMAL_INT_MASK_VALUE, qm->io_base + QM_ABNORMAL_INT_MASK); 1035 } 1036 1037 static void qm_log_hw_error(struct hisi_qm *qm, u32 error_status) 1038 { 1039 const struct hisi_qm_hw_error *err; 1040 struct device *dev = &qm->pdev->dev; 1041 u32 reg_val, type, vf_num; 1042 int i; 1043 1044 for (i = 0; i < ARRAY_SIZE(qm_hw_error); i++) { 1045 err = &qm_hw_error[i]; 1046 if (!(err->int_msk & error_status)) 1047 continue; 1048 1049 dev_err(dev, "%s [error status=0x%x] found\n", 1050 err->msg, err->int_msk); 1051 1052 if (err->int_msk & QM_DB_TIMEOUT) { 1053 reg_val = readl(qm->io_base + QM_ABNORMAL_INF01); 1054 type = (reg_val & QM_DB_TIMEOUT_TYPE) >> 1055 QM_DB_TIMEOUT_TYPE_SHIFT; 1056 vf_num = reg_val & QM_DB_TIMEOUT_VF; 1057 dev_err(dev, "qm %s doorbell timeout in function %u\n", 1058 qm_db_timeout[type], vf_num); 1059 } else if (err->int_msk & QM_OF_FIFO_OF) { 1060 reg_val = readl(qm->io_base + QM_ABNORMAL_INF00); 1061 type = (reg_val & QM_FIFO_OVERFLOW_TYPE) >> 1062 QM_FIFO_OVERFLOW_TYPE_SHIFT; 1063 vf_num = reg_val & QM_FIFO_OVERFLOW_VF; 1064 1065 if (type < ARRAY_SIZE(qm_fifo_overflow)) 1066 dev_err(dev, "qm %s fifo overflow in function %u\n", 1067 qm_fifo_overflow[type], vf_num); 1068 else 1069 dev_err(dev, "unknown error type\n"); 1070 } 1071 } 1072 } 1073 1074 static pci_ers_result_t qm_hw_error_handle_v2(struct hisi_qm *qm) 1075 { 1076 u32 error_status, tmp; 1077 1078 /* read err sts */ 1079 tmp = readl(qm->io_base + QM_ABNORMAL_INT_STATUS); 1080 error_status = qm->error_mask & tmp; 1081 1082 if (error_status) { 1083 qm_log_hw_error(qm, error_status); 1084 1085 /* clear err sts */ 1086 writel(error_status, qm->io_base + QM_ABNORMAL_INT_SOURCE); 1087 1088 return PCI_ERS_RESULT_NEED_RESET; 1089 } 1090 1091 return PCI_ERS_RESULT_RECOVERED; 1092 } 1093 1094 static const struct hisi_qm_hw_ops qm_hw_ops_v1 = { 1095 .qm_db = qm_db_v1, 1096 .get_irq_num = qm_get_irq_num_v1, 1097 .hw_error_init = qm_hw_error_init_v1, 1098 }; 1099 1100 static const struct hisi_qm_hw_ops qm_hw_ops_v2 = { 1101 .get_vft = qm_get_vft_v2, 1102 .qm_db = qm_db_v2, 1103 .get_irq_num = qm_get_irq_num_v2, 1104 .hw_error_init = qm_hw_error_init_v2, 1105 .hw_error_uninit = qm_hw_error_uninit_v2, 1106 .hw_error_handle = qm_hw_error_handle_v2, 1107 }; 1108 1109 static void *qm_get_avail_sqe(struct hisi_qp *qp) 1110 { 1111 struct hisi_qp_status *qp_status = &qp->qp_status; 1112 u16 sq_tail = qp_status->sq_tail; 1113 1114 if (unlikely(atomic_read(&qp->qp_status.used) == QM_Q_DEPTH)) 1115 return NULL; 1116 1117 return qp->sqe + sq_tail * qp->qm->sqe_size; 1118 } 1119 1120 /** 1121 * hisi_qm_create_qp() - Create a queue pair from qm. 1122 * @qm: The qm we create a qp from. 1123 * @alg_type: Accelerator specific algorithm type in sqc. 1124 * 1125 * return created qp, -EBUSY if all qps in qm allocated, -ENOMEM if allocating 1126 * qp memory fails. 1127 */ 1128 struct hisi_qp *hisi_qm_create_qp(struct hisi_qm *qm, u8 alg_type) 1129 { 1130 struct device *dev = &qm->pdev->dev; 1131 struct hisi_qp *qp; 1132 int qp_id, ret; 1133 1134 qp = kzalloc(sizeof(*qp), GFP_KERNEL); 1135 if (!qp) 1136 return ERR_PTR(-ENOMEM); 1137 1138 write_lock(&qm->qps_lock); 1139 1140 qp_id = find_first_zero_bit(qm->qp_bitmap, qm->qp_num); 1141 if (qp_id >= qm->qp_num) { 1142 write_unlock(&qm->qps_lock); 1143 dev_info(&qm->pdev->dev, "QM all queues are busy!\n"); 1144 ret = -EBUSY; 1145 goto err_free_qp; 1146 } 1147 set_bit(qp_id, qm->qp_bitmap); 1148 qm->qp_array[qp_id] = qp; 1149 qm->qp_in_used++; 1150 1151 write_unlock(&qm->qps_lock); 1152 1153 qp->qm = qm; 1154 1155 if (qm->use_dma_api) { 1156 qp->qdma.size = qm->sqe_size * QM_Q_DEPTH + 1157 sizeof(struct qm_cqe) * QM_Q_DEPTH; 1158 qp->qdma.va = dma_alloc_coherent(dev, qp->qdma.size, 1159 &qp->qdma.dma, GFP_KERNEL); 1160 if (!qp->qdma.va) { 1161 ret = -ENOMEM; 1162 goto err_clear_bit; 1163 } 1164 1165 dev_dbg(dev, "allocate qp dma buf(va=%pK, dma=%pad, size=%zx)\n", 1166 qp->qdma.va, &qp->qdma.dma, qp->qdma.size); 1167 } 1168 1169 qp->qp_id = qp_id; 1170 qp->alg_type = alg_type; 1171 1172 return qp; 1173 1174 err_clear_bit: 1175 write_lock(&qm->qps_lock); 1176 qm->qp_array[qp_id] = NULL; 1177 clear_bit(qp_id, qm->qp_bitmap); 1178 write_unlock(&qm->qps_lock); 1179 err_free_qp: 1180 kfree(qp); 1181 return ERR_PTR(ret); 1182 } 1183 EXPORT_SYMBOL_GPL(hisi_qm_create_qp); 1184 1185 /** 1186 * hisi_qm_release_qp() - Release a qp back to its qm. 1187 * @qp: The qp we want to release. 1188 * 1189 * This function releases the resource of a qp. 1190 */ 1191 void hisi_qm_release_qp(struct hisi_qp *qp) 1192 { 1193 struct hisi_qm *qm = qp->qm; 1194 struct qm_dma *qdma = &qp->qdma; 1195 struct device *dev = &qm->pdev->dev; 1196 1197 if (qm->use_dma_api && qdma->va) 1198 dma_free_coherent(dev, qdma->size, qdma->va, qdma->dma); 1199 1200 write_lock(&qm->qps_lock); 1201 qm->qp_array[qp->qp_id] = NULL; 1202 clear_bit(qp->qp_id, qm->qp_bitmap); 1203 qm->qp_in_used--; 1204 write_unlock(&qm->qps_lock); 1205 1206 kfree(qp); 1207 } 1208 EXPORT_SYMBOL_GPL(hisi_qm_release_qp); 1209 1210 static int qm_qp_ctx_cfg(struct hisi_qp *qp, int qp_id, int pasid) 1211 { 1212 struct hisi_qm *qm = qp->qm; 1213 struct device *dev = &qm->pdev->dev; 1214 enum qm_hw_ver ver = qm->ver; 1215 struct qm_sqc *sqc; 1216 struct qm_cqc *cqc; 1217 dma_addr_t sqc_dma; 1218 dma_addr_t cqc_dma; 1219 int ret; 1220 1221 qm_init_qp_status(qp); 1222 1223 sqc = kzalloc(sizeof(struct qm_sqc), GFP_KERNEL); 1224 if (!sqc) 1225 return -ENOMEM; 1226 sqc_dma = dma_map_single(dev, sqc, sizeof(struct qm_sqc), 1227 DMA_TO_DEVICE); 1228 if (dma_mapping_error(dev, sqc_dma)) { 1229 kfree(sqc); 1230 return -ENOMEM; 1231 } 1232 1233 INIT_QC_COMMON(sqc, qp->sqe_dma, pasid); 1234 if (ver == QM_HW_V1) { 1235 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V1(0, 0, 0, qm->sqe_size)); 1236 sqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1); 1237 } else if (ver == QM_HW_V2) { 1238 sqc->dw3 = cpu_to_le32(QM_MK_SQC_DW3_V2(qm->sqe_size)); 1239 sqc->w8 = 0; /* rand_qc */ 1240 } 1241 sqc->cq_num = cpu_to_le16(qp_id); 1242 sqc->w13 = cpu_to_le16(QM_MK_SQC_W13(0, 1, qp->alg_type)); 1243 1244 ret = qm_mb(qm, QM_MB_CMD_SQC, sqc_dma, qp_id, 0); 1245 dma_unmap_single(dev, sqc_dma, sizeof(struct qm_sqc), DMA_TO_DEVICE); 1246 kfree(sqc); 1247 if (ret) 1248 return ret; 1249 1250 cqc = kzalloc(sizeof(struct qm_cqc), GFP_KERNEL); 1251 if (!cqc) 1252 return -ENOMEM; 1253 cqc_dma = dma_map_single(dev, cqc, sizeof(struct qm_cqc), 1254 DMA_TO_DEVICE); 1255 if (dma_mapping_error(dev, cqc_dma)) { 1256 kfree(cqc); 1257 return -ENOMEM; 1258 } 1259 1260 INIT_QC_COMMON(cqc, qp->cqe_dma, pasid); 1261 if (ver == QM_HW_V1) { 1262 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V1(0, 0, 0, 4)); 1263 cqc->w8 = cpu_to_le16(QM_Q_DEPTH - 1); 1264 } else if (ver == QM_HW_V2) { 1265 cqc->dw3 = cpu_to_le32(QM_MK_CQC_DW3_V2(4)); 1266 cqc->w8 = 0; 1267 } 1268 cqc->dw6 = cpu_to_le32(1 << QM_CQ_PHASE_SHIFT | 1 << QM_CQ_FLAG_SHIFT); 1269 1270 ret = qm_mb(qm, QM_MB_CMD_CQC, cqc_dma, qp_id, 0); 1271 dma_unmap_single(dev, cqc_dma, sizeof(struct qm_cqc), DMA_TO_DEVICE); 1272 kfree(cqc); 1273 1274 return ret; 1275 } 1276 1277 /** 1278 * hisi_qm_start_qp() - Start a qp into running. 1279 * @qp: The qp we want to start to run. 1280 * @arg: Accelerator specific argument. 1281 * 1282 * After this function, qp can receive request from user. Return 0 if 1283 * successful, Return -EBUSY if failed. 1284 */ 1285 int hisi_qm_start_qp(struct hisi_qp *qp, unsigned long arg) 1286 { 1287 struct hisi_qm *qm = qp->qm; 1288 struct device *dev = &qm->pdev->dev; 1289 enum qm_hw_ver ver = qm->ver; 1290 int qp_id = qp->qp_id; 1291 int pasid = arg; 1292 size_t off = 0; 1293 int ret; 1294 1295 #define QP_INIT_BUF(qp, type, size) do { \ 1296 (qp)->type = ((qp)->qdma.va + (off)); \ 1297 (qp)->type##_dma = (qp)->qdma.dma + (off); \ 1298 off += (size); \ 1299 } while (0) 1300 1301 if (!qp->qdma.dma) { 1302 dev_err(dev, "cannot get qm dma buffer\n"); 1303 return -EINVAL; 1304 } 1305 1306 /* sq need 128 bytes alignment */ 1307 if (qp->qdma.dma & QM_SQE_DATA_ALIGN_MASK) { 1308 dev_err(dev, "qm sq is not aligned to 128 byte\n"); 1309 return -EINVAL; 1310 } 1311 1312 QP_INIT_BUF(qp, sqe, qm->sqe_size * QM_Q_DEPTH); 1313 QP_INIT_BUF(qp, cqe, sizeof(struct qm_cqe) * QM_Q_DEPTH); 1314 1315 dev_dbg(dev, "init qp buffer(v%d):\n" 1316 " sqe (%pK, %lx)\n" 1317 " cqe (%pK, %lx)\n", 1318 ver, qp->sqe, (unsigned long)qp->sqe_dma, 1319 qp->cqe, (unsigned long)qp->cqe_dma); 1320 1321 ret = qm_qp_ctx_cfg(qp, qp_id, pasid); 1322 if (ret) 1323 return ret; 1324 1325 dev_dbg(dev, "queue %d started\n", qp_id); 1326 1327 return 0; 1328 } 1329 EXPORT_SYMBOL_GPL(hisi_qm_start_qp); 1330 1331 /** 1332 * hisi_qm_stop_qp() - Stop a qp in qm. 1333 * @qp: The qp we want to stop. 1334 * 1335 * This function is reverse of hisi_qm_start_qp. Return 0 if successful. 1336 */ 1337 int hisi_qm_stop_qp(struct hisi_qp *qp) 1338 { 1339 struct device *dev = &qp->qm->pdev->dev; 1340 int i = 0; 1341 1342 /* it is stopped */ 1343 if (test_bit(QP_STOP, &qp->qp_status.flags)) 1344 return 0; 1345 1346 while (atomic_read(&qp->qp_status.used)) { 1347 i++; 1348 msleep(20); 1349 if (i == 10) { 1350 dev_err(dev, "Cannot drain out data for stopping, Force to stop!\n"); 1351 return 0; 1352 } 1353 } 1354 1355 set_bit(QP_STOP, &qp->qp_status.flags); 1356 1357 dev_dbg(dev, "stop queue %u!", qp->qp_id); 1358 1359 return 0; 1360 } 1361 EXPORT_SYMBOL_GPL(hisi_qm_stop_qp); 1362 1363 /** 1364 * hisi_qp_send() - Queue up a task in the hardware queue. 1365 * @qp: The qp in which to put the message. 1366 * @msg: The message. 1367 * 1368 * This function will return -EBUSY if qp is currently full, and -EAGAIN 1369 * if qp related qm is resetting. 1370 */ 1371 int hisi_qp_send(struct hisi_qp *qp, const void *msg) 1372 { 1373 struct hisi_qp_status *qp_status = &qp->qp_status; 1374 u16 sq_tail = qp_status->sq_tail; 1375 u16 sq_tail_next = (sq_tail + 1) % QM_Q_DEPTH; 1376 void *sqe = qm_get_avail_sqe(qp); 1377 1378 if (unlikely(test_bit(QP_STOP, &qp->qp_status.flags))) { 1379 dev_info(&qp->qm->pdev->dev, "QP is stopped or resetting\n"); 1380 return -EAGAIN; 1381 } 1382 1383 if (!sqe) 1384 return -EBUSY; 1385 1386 memcpy(sqe, msg, qp->qm->sqe_size); 1387 1388 qm_db(qp->qm, qp->qp_id, QM_DOORBELL_CMD_SQ, sq_tail_next, 0); 1389 atomic_inc(&qp->qp_status.used); 1390 qp_status->sq_tail = sq_tail_next; 1391 1392 return 0; 1393 } 1394 EXPORT_SYMBOL_GPL(hisi_qp_send); 1395 1396 static void hisi_qm_cache_wb(struct hisi_qm *qm) 1397 { 1398 unsigned int val; 1399 1400 if (qm->ver == QM_HW_V2) { 1401 writel(0x1, qm->io_base + QM_CACHE_WB_START); 1402 if (readl_relaxed_poll_timeout(qm->io_base + QM_CACHE_WB_DONE, 1403 val, val & BIT(0), 10, 1000)) 1404 dev_err(&qm->pdev->dev, "QM writeback sqc cache fail!\n"); 1405 } 1406 } 1407 1408 static void qm_qp_event_notifier(struct hisi_qp *qp) 1409 { 1410 wake_up_interruptible(&qp->uacce_q->wait); 1411 } 1412 1413 static int hisi_qm_get_available_instances(struct uacce_device *uacce) 1414 { 1415 int i, ret; 1416 struct hisi_qm *qm = uacce->priv; 1417 1418 read_lock(&qm->qps_lock); 1419 for (i = 0, ret = 0; i < qm->qp_num; i++) 1420 if (!qm->qp_array[i]) 1421 ret++; 1422 read_unlock(&qm->qps_lock); 1423 1424 return ret; 1425 } 1426 1427 static int hisi_qm_uacce_get_queue(struct uacce_device *uacce, 1428 unsigned long arg, 1429 struct uacce_queue *q) 1430 { 1431 struct hisi_qm *qm = uacce->priv; 1432 struct hisi_qp *qp; 1433 u8 alg_type = 0; 1434 1435 qp = hisi_qm_create_qp(qm, alg_type); 1436 if (IS_ERR(qp)) 1437 return PTR_ERR(qp); 1438 1439 q->priv = qp; 1440 q->uacce = uacce; 1441 qp->uacce_q = q; 1442 qp->event_cb = qm_qp_event_notifier; 1443 qp->pasid = arg; 1444 1445 return 0; 1446 } 1447 1448 static void hisi_qm_uacce_put_queue(struct uacce_queue *q) 1449 { 1450 struct hisi_qp *qp = q->priv; 1451 1452 hisi_qm_cache_wb(qp->qm); 1453 hisi_qm_release_qp(qp); 1454 } 1455 1456 /* map sq/cq/doorbell to user space */ 1457 static int hisi_qm_uacce_mmap(struct uacce_queue *q, 1458 struct vm_area_struct *vma, 1459 struct uacce_qfile_region *qfr) 1460 { 1461 struct hisi_qp *qp = q->priv; 1462 struct hisi_qm *qm = qp->qm; 1463 size_t sz = vma->vm_end - vma->vm_start; 1464 struct pci_dev *pdev = qm->pdev; 1465 struct device *dev = &pdev->dev; 1466 unsigned long vm_pgoff; 1467 int ret; 1468 1469 switch (qfr->type) { 1470 case UACCE_QFRT_MMIO: 1471 if (qm->ver == QM_HW_V2) { 1472 if (sz > PAGE_SIZE * (QM_DOORBELL_PAGE_NR + 1473 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE)) 1474 return -EINVAL; 1475 } else { 1476 if (sz > PAGE_SIZE * QM_DOORBELL_PAGE_NR) 1477 return -EINVAL; 1478 } 1479 1480 vma->vm_flags |= VM_IO; 1481 1482 return remap_pfn_range(vma, vma->vm_start, 1483 qm->phys_base >> PAGE_SHIFT, 1484 sz, pgprot_noncached(vma->vm_page_prot)); 1485 case UACCE_QFRT_DUS: 1486 if (sz != qp->qdma.size) 1487 return -EINVAL; 1488 1489 /* 1490 * dma_mmap_coherent() requires vm_pgoff as 0 1491 * restore vm_pfoff to initial value for mmap() 1492 */ 1493 vm_pgoff = vma->vm_pgoff; 1494 vma->vm_pgoff = 0; 1495 ret = dma_mmap_coherent(dev, vma, qp->qdma.va, 1496 qp->qdma.dma, sz); 1497 vma->vm_pgoff = vm_pgoff; 1498 return ret; 1499 1500 default: 1501 return -EINVAL; 1502 } 1503 } 1504 1505 static int hisi_qm_uacce_start_queue(struct uacce_queue *q) 1506 { 1507 struct hisi_qp *qp = q->priv; 1508 1509 return hisi_qm_start_qp(qp, qp->pasid); 1510 } 1511 1512 static void hisi_qm_uacce_stop_queue(struct uacce_queue *q) 1513 { 1514 hisi_qm_stop_qp(q->priv); 1515 } 1516 1517 static int qm_set_sqctype(struct uacce_queue *q, u16 type) 1518 { 1519 struct hisi_qm *qm = q->uacce->priv; 1520 struct hisi_qp *qp = q->priv; 1521 1522 write_lock(&qm->qps_lock); 1523 qp->alg_type = type; 1524 write_unlock(&qm->qps_lock); 1525 1526 return 0; 1527 } 1528 1529 static long hisi_qm_uacce_ioctl(struct uacce_queue *q, unsigned int cmd, 1530 unsigned long arg) 1531 { 1532 struct hisi_qp *qp = q->priv; 1533 struct hisi_qp_ctx qp_ctx; 1534 1535 if (cmd == UACCE_CMD_QM_SET_QP_CTX) { 1536 if (copy_from_user(&qp_ctx, (void __user *)arg, 1537 sizeof(struct hisi_qp_ctx))) 1538 return -EFAULT; 1539 1540 if (qp_ctx.qc_type != 0 && qp_ctx.qc_type != 1) 1541 return -EINVAL; 1542 1543 qm_set_sqctype(q, qp_ctx.qc_type); 1544 qp_ctx.id = qp->qp_id; 1545 1546 if (copy_to_user((void __user *)arg, &qp_ctx, 1547 sizeof(struct hisi_qp_ctx))) 1548 return -EFAULT; 1549 } else { 1550 return -EINVAL; 1551 } 1552 1553 return 0; 1554 } 1555 1556 static const struct uacce_ops uacce_qm_ops = { 1557 .get_available_instances = hisi_qm_get_available_instances, 1558 .get_queue = hisi_qm_uacce_get_queue, 1559 .put_queue = hisi_qm_uacce_put_queue, 1560 .start_queue = hisi_qm_uacce_start_queue, 1561 .stop_queue = hisi_qm_uacce_stop_queue, 1562 .mmap = hisi_qm_uacce_mmap, 1563 .ioctl = hisi_qm_uacce_ioctl, 1564 }; 1565 1566 static int qm_alloc_uacce(struct hisi_qm *qm) 1567 { 1568 struct pci_dev *pdev = qm->pdev; 1569 struct uacce_device *uacce; 1570 unsigned long mmio_page_nr; 1571 unsigned long dus_page_nr; 1572 struct uacce_interface interface = { 1573 .flags = UACCE_DEV_SVA, 1574 .ops = &uacce_qm_ops, 1575 }; 1576 1577 strncpy(interface.name, pdev->driver->name, sizeof(interface.name)); 1578 1579 uacce = uacce_alloc(&pdev->dev, &interface); 1580 if (IS_ERR(uacce)) 1581 return PTR_ERR(uacce); 1582 1583 if (uacce->flags & UACCE_DEV_SVA) { 1584 qm->use_sva = true; 1585 } else { 1586 /* only consider sva case */ 1587 uacce_remove(uacce); 1588 qm->uacce = NULL; 1589 return -EINVAL; 1590 } 1591 1592 uacce->is_vf = pdev->is_virtfn; 1593 uacce->priv = qm; 1594 uacce->algs = qm->algs; 1595 1596 if (qm->ver == QM_HW_V1) { 1597 mmio_page_nr = QM_DOORBELL_PAGE_NR; 1598 uacce->api_ver = HISI_QM_API_VER_BASE; 1599 } else { 1600 mmio_page_nr = QM_DOORBELL_PAGE_NR + 1601 QM_DOORBELL_SQ_CQ_BASE_V2 / PAGE_SIZE; 1602 uacce->api_ver = HISI_QM_API_VER2_BASE; 1603 } 1604 1605 dus_page_nr = (PAGE_SIZE - 1 + qm->sqe_size * QM_Q_DEPTH + 1606 sizeof(struct qm_cqe) * QM_Q_DEPTH) >> PAGE_SHIFT; 1607 1608 uacce->qf_pg_num[UACCE_QFRT_MMIO] = mmio_page_nr; 1609 uacce->qf_pg_num[UACCE_QFRT_DUS] = dus_page_nr; 1610 1611 qm->uacce = uacce; 1612 1613 return 0; 1614 } 1615 1616 /** 1617 * hisi_qm_get_free_qp_num() - Get free number of qp in qm. 1618 * @qm: The qm which want to get free qp. 1619 * 1620 * This function return free number of qp in qm. 1621 */ 1622 int hisi_qm_get_free_qp_num(struct hisi_qm *qm) 1623 { 1624 int ret; 1625 1626 read_lock(&qm->qps_lock); 1627 ret = qm->qp_num - qm->qp_in_used; 1628 read_unlock(&qm->qps_lock); 1629 1630 return ret; 1631 } 1632 EXPORT_SYMBOL_GPL(hisi_qm_get_free_qp_num); 1633 1634 /** 1635 * hisi_qm_init() - Initialize configures about qm. 1636 * @qm: The qm needing init. 1637 * 1638 * This function init qm, then we can call hisi_qm_start to put qm into work. 1639 */ 1640 int hisi_qm_init(struct hisi_qm *qm) 1641 { 1642 struct pci_dev *pdev = qm->pdev; 1643 struct device *dev = &pdev->dev; 1644 unsigned int num_vec; 1645 int ret; 1646 1647 switch (qm->ver) { 1648 case QM_HW_V1: 1649 qm->ops = &qm_hw_ops_v1; 1650 break; 1651 case QM_HW_V2: 1652 qm->ops = &qm_hw_ops_v2; 1653 break; 1654 default: 1655 return -EINVAL; 1656 } 1657 1658 ret = qm_alloc_uacce(qm); 1659 if (ret < 0) 1660 dev_warn(&pdev->dev, "fail to alloc uacce (%d)\n", ret); 1661 1662 ret = pci_enable_device_mem(pdev); 1663 if (ret < 0) { 1664 dev_err(&pdev->dev, "Failed to enable device mem!\n"); 1665 goto err_remove_uacce; 1666 } 1667 1668 ret = pci_request_mem_regions(pdev, qm->dev_name); 1669 if (ret < 0) { 1670 dev_err(&pdev->dev, "Failed to request mem regions!\n"); 1671 goto err_disable_pcidev; 1672 } 1673 1674 qm->phys_base = pci_resource_start(pdev, PCI_BAR_2); 1675 qm->phys_size = pci_resource_len(qm->pdev, PCI_BAR_2); 1676 qm->io_base = ioremap(qm->phys_base, qm->phys_size); 1677 if (!qm->io_base) { 1678 ret = -EIO; 1679 goto err_release_mem_regions; 1680 } 1681 1682 ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(64)); 1683 if (ret < 0) 1684 goto err_iounmap; 1685 pci_set_master(pdev); 1686 1687 if (!qm->ops->get_irq_num) { 1688 ret = -EOPNOTSUPP; 1689 goto err_iounmap; 1690 } 1691 num_vec = qm->ops->get_irq_num(qm); 1692 ret = pci_alloc_irq_vectors(pdev, num_vec, num_vec, PCI_IRQ_MSI); 1693 if (ret < 0) { 1694 dev_err(dev, "Failed to enable MSI vectors!\n"); 1695 goto err_iounmap; 1696 } 1697 1698 ret = qm_irq_register(qm); 1699 if (ret) 1700 goto err_free_irq_vectors; 1701 1702 qm->qp_in_used = 0; 1703 mutex_init(&qm->mailbox_lock); 1704 rwlock_init(&qm->qps_lock); 1705 INIT_WORK(&qm->work, qm_work_process); 1706 1707 dev_dbg(dev, "init qm %s with %s\n", pdev->is_physfn ? "pf" : "vf", 1708 qm->use_dma_api ? "dma api" : "iommu api"); 1709 1710 return 0; 1711 1712 err_free_irq_vectors: 1713 pci_free_irq_vectors(pdev); 1714 err_iounmap: 1715 iounmap(qm->io_base); 1716 err_release_mem_regions: 1717 pci_release_mem_regions(pdev); 1718 err_disable_pcidev: 1719 pci_disable_device(pdev); 1720 err_remove_uacce: 1721 uacce_remove(qm->uacce); 1722 qm->uacce = NULL; 1723 1724 return ret; 1725 } 1726 EXPORT_SYMBOL_GPL(hisi_qm_init); 1727 1728 /** 1729 * hisi_qm_uninit() - Uninitialize qm. 1730 * @qm: The qm needed uninit. 1731 * 1732 * This function uninits qm related device resources. 1733 */ 1734 void hisi_qm_uninit(struct hisi_qm *qm) 1735 { 1736 struct pci_dev *pdev = qm->pdev; 1737 struct device *dev = &pdev->dev; 1738 1739 uacce_remove(qm->uacce); 1740 qm->uacce = NULL; 1741 1742 if (qm->use_dma_api && qm->qdma.va) { 1743 hisi_qm_cache_wb(qm); 1744 dma_free_coherent(dev, qm->qdma.size, 1745 qm->qdma.va, qm->qdma.dma); 1746 memset(&qm->qdma, 0, sizeof(qm->qdma)); 1747 } 1748 1749 qm_irq_unregister(qm); 1750 pci_free_irq_vectors(pdev); 1751 iounmap(qm->io_base); 1752 pci_release_mem_regions(pdev); 1753 pci_disable_device(pdev); 1754 } 1755 EXPORT_SYMBOL_GPL(hisi_qm_uninit); 1756 1757 /** 1758 * hisi_qm_get_vft() - Get vft from a qm. 1759 * @qm: The qm we want to get its vft. 1760 * @base: The base number of queue in vft. 1761 * @number: The number of queues in vft. 1762 * 1763 * We can allocate multiple queues to a qm by configuring virtual function 1764 * table. We get related configures by this function. Normally, we call this 1765 * function in VF driver to get the queue information. 1766 * 1767 * qm hw v1 does not support this interface. 1768 */ 1769 int hisi_qm_get_vft(struct hisi_qm *qm, u32 *base, u32 *number) 1770 { 1771 if (!base || !number) 1772 return -EINVAL; 1773 1774 if (!qm->ops->get_vft) { 1775 dev_err(&qm->pdev->dev, "Don't support vft read!\n"); 1776 return -EINVAL; 1777 } 1778 1779 return qm->ops->get_vft(qm, base, number); 1780 } 1781 EXPORT_SYMBOL_GPL(hisi_qm_get_vft); 1782 1783 /** 1784 * hisi_qm_set_vft() - Set "virtual function table" for a qm. 1785 * @fun_num: Number of operated function. 1786 * @qm: The qm in which to set vft, alway in a PF. 1787 * @base: The base number of queue in vft. 1788 * @number: The number of queues in vft. 0 means invalid vft. 1789 * 1790 * This function is alway called in PF driver, it is used to assign queues 1791 * among PF and VFs. 1792 * 1793 * Assign queues A~B to PF: hisi_qm_set_vft(qm, 0, A, B - A + 1) 1794 * Assign queues A~B to VF: hisi_qm_set_vft(qm, 2, A, B - A + 1) 1795 * (VF function number 0x2) 1796 */ 1797 int hisi_qm_set_vft(struct hisi_qm *qm, u32 fun_num, u32 base, 1798 u32 number) 1799 { 1800 u32 max_q_num = qm->ctrl_qp_num; 1801 1802 if (base >= max_q_num || number > max_q_num || 1803 (base + number) > max_q_num) 1804 return -EINVAL; 1805 1806 return qm_set_sqc_cqc_vft(qm, fun_num, base, number); 1807 } 1808 EXPORT_SYMBOL_GPL(hisi_qm_set_vft); 1809 1810 static void qm_init_eq_aeq_status(struct hisi_qm *qm) 1811 { 1812 struct hisi_qm_status *status = &qm->status; 1813 1814 status->eq_head = 0; 1815 status->aeq_head = 0; 1816 status->eqc_phase = true; 1817 status->aeqc_phase = true; 1818 } 1819 1820 static int qm_eq_ctx_cfg(struct hisi_qm *qm) 1821 { 1822 struct device *dev = &qm->pdev->dev; 1823 struct qm_eqc *eqc; 1824 struct qm_aeqc *aeqc; 1825 dma_addr_t eqc_dma; 1826 dma_addr_t aeqc_dma; 1827 int ret; 1828 1829 qm_init_eq_aeq_status(qm); 1830 1831 eqc = kzalloc(sizeof(struct qm_eqc), GFP_KERNEL); 1832 if (!eqc) 1833 return -ENOMEM; 1834 eqc_dma = dma_map_single(dev, eqc, sizeof(struct qm_eqc), 1835 DMA_TO_DEVICE); 1836 if (dma_mapping_error(dev, eqc_dma)) { 1837 kfree(eqc); 1838 return -ENOMEM; 1839 } 1840 1841 eqc->base_l = cpu_to_le32(lower_32_bits(qm->eqe_dma)); 1842 eqc->base_h = cpu_to_le32(upper_32_bits(qm->eqe_dma)); 1843 if (qm->ver == QM_HW_V1) 1844 eqc->dw3 = cpu_to_le32(QM_EQE_AEQE_SIZE); 1845 eqc->dw6 = cpu_to_le32((QM_Q_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT)); 1846 ret = qm_mb(qm, QM_MB_CMD_EQC, eqc_dma, 0, 0); 1847 dma_unmap_single(dev, eqc_dma, sizeof(struct qm_eqc), DMA_TO_DEVICE); 1848 kfree(eqc); 1849 if (ret) 1850 return ret; 1851 1852 aeqc = kzalloc(sizeof(struct qm_aeqc), GFP_KERNEL); 1853 if (!aeqc) 1854 return -ENOMEM; 1855 aeqc_dma = dma_map_single(dev, aeqc, sizeof(struct qm_aeqc), 1856 DMA_TO_DEVICE); 1857 if (dma_mapping_error(dev, aeqc_dma)) { 1858 kfree(aeqc); 1859 return -ENOMEM; 1860 } 1861 1862 aeqc->base_l = cpu_to_le32(lower_32_bits(qm->aeqe_dma)); 1863 aeqc->base_h = cpu_to_le32(upper_32_bits(qm->aeqe_dma)); 1864 aeqc->dw6 = cpu_to_le32((QM_Q_DEPTH - 1) | (1 << QM_EQC_PHASE_SHIFT)); 1865 1866 ret = qm_mb(qm, QM_MB_CMD_AEQC, aeqc_dma, 0, 0); 1867 dma_unmap_single(dev, aeqc_dma, sizeof(struct qm_aeqc), DMA_TO_DEVICE); 1868 kfree(aeqc); 1869 1870 return ret; 1871 } 1872 1873 static int __hisi_qm_start(struct hisi_qm *qm) 1874 { 1875 struct pci_dev *pdev = qm->pdev; 1876 struct device *dev = &pdev->dev; 1877 size_t off = 0; 1878 int ret; 1879 1880 #define QM_INIT_BUF(qm, type, num) do { \ 1881 (qm)->type = ((qm)->qdma.va + (off)); \ 1882 (qm)->type##_dma = (qm)->qdma.dma + (off); \ 1883 off += QMC_ALIGN(sizeof(struct qm_##type) * (num)); \ 1884 } while (0) 1885 1886 WARN_ON(!qm->qdma.dma); 1887 1888 if (qm->qp_num == 0) 1889 return -EINVAL; 1890 1891 if (qm->fun_type == QM_HW_PF) { 1892 ret = qm_dev_mem_reset(qm); 1893 if (ret) 1894 return ret; 1895 1896 ret = hisi_qm_set_vft(qm, 0, qm->qp_base, qm->qp_num); 1897 if (ret) 1898 return ret; 1899 } 1900 1901 QM_INIT_BUF(qm, eqe, QM_Q_DEPTH); 1902 QM_INIT_BUF(qm, aeqe, QM_Q_DEPTH); 1903 QM_INIT_BUF(qm, sqc, qm->qp_num); 1904 QM_INIT_BUF(qm, cqc, qm->qp_num); 1905 1906 dev_dbg(dev, "init qm buffer:\n" 1907 " eqe (%pK, %lx)\n" 1908 " aeqe (%pK, %lx)\n" 1909 " sqc (%pK, %lx)\n" 1910 " cqc (%pK, %lx)\n", 1911 qm->eqe, (unsigned long)qm->eqe_dma, 1912 qm->aeqe, (unsigned long)qm->aeqe_dma, 1913 qm->sqc, (unsigned long)qm->sqc_dma, 1914 qm->cqc, (unsigned long)qm->cqc_dma); 1915 1916 ret = qm_eq_ctx_cfg(qm); 1917 if (ret) 1918 return ret; 1919 1920 ret = qm_mb(qm, QM_MB_CMD_SQC_BT, qm->sqc_dma, 0, 0); 1921 if (ret) 1922 return ret; 1923 1924 ret = qm_mb(qm, QM_MB_CMD_CQC_BT, qm->cqc_dma, 0, 0); 1925 if (ret) 1926 return ret; 1927 1928 writel(0x0, qm->io_base + QM_VF_EQ_INT_MASK); 1929 writel(0x0, qm->io_base + QM_VF_AEQ_INT_MASK); 1930 1931 return 0; 1932 } 1933 1934 /** 1935 * hisi_qm_start() - start qm 1936 * @qm: The qm to be started. 1937 * 1938 * This function starts a qm, then we can allocate qp from this qm. 1939 */ 1940 int hisi_qm_start(struct hisi_qm *qm) 1941 { 1942 struct device *dev = &qm->pdev->dev; 1943 1944 dev_dbg(dev, "qm start with %d queue pairs\n", qm->qp_num); 1945 1946 if (!qm->qp_num) { 1947 dev_err(dev, "qp_num should not be 0\n"); 1948 return -EINVAL; 1949 } 1950 1951 if (!qm->qp_bitmap) { 1952 qm->qp_bitmap = devm_kcalloc(dev, BITS_TO_LONGS(qm->qp_num), 1953 sizeof(long), GFP_KERNEL); 1954 qm->qp_array = devm_kcalloc(dev, qm->qp_num, 1955 sizeof(struct hisi_qp *), 1956 GFP_KERNEL); 1957 if (!qm->qp_bitmap || !qm->qp_array) 1958 return -ENOMEM; 1959 } 1960 1961 if (!qm->use_dma_api) { 1962 dev_dbg(&qm->pdev->dev, "qm delay start\n"); 1963 return 0; 1964 } else if (!qm->qdma.va) { 1965 qm->qdma.size = QMC_ALIGN(sizeof(struct qm_eqe) * QM_Q_DEPTH) + 1966 QMC_ALIGN(sizeof(struct qm_aeqe) * QM_Q_DEPTH) + 1967 QMC_ALIGN(sizeof(struct qm_sqc) * qm->qp_num) + 1968 QMC_ALIGN(sizeof(struct qm_cqc) * qm->qp_num); 1969 qm->qdma.va = dma_alloc_coherent(dev, qm->qdma.size, 1970 &qm->qdma.dma, GFP_KERNEL); 1971 dev_dbg(dev, "allocate qm dma buf(va=%pK, dma=%pad, size=%zx)\n", 1972 qm->qdma.va, &qm->qdma.dma, qm->qdma.size); 1973 if (!qm->qdma.va) 1974 return -ENOMEM; 1975 } 1976 1977 return __hisi_qm_start(qm); 1978 } 1979 EXPORT_SYMBOL_GPL(hisi_qm_start); 1980 1981 /** 1982 * hisi_qm_stop() - Stop a qm. 1983 * @qm: The qm which will be stopped. 1984 * 1985 * This function stops qm and its qps, then qm can not accept request. 1986 * Related resources are not released at this state, we can use hisi_qm_start 1987 * to let qm start again. 1988 */ 1989 int hisi_qm_stop(struct hisi_qm *qm) 1990 { 1991 struct device *dev; 1992 struct hisi_qp *qp; 1993 int ret = 0, i; 1994 1995 if (!qm || !qm->pdev) { 1996 WARN_ON(1); 1997 return -EINVAL; 1998 } 1999 2000 dev = &qm->pdev->dev; 2001 2002 /* Mask eq and aeq irq */ 2003 writel(0x1, qm->io_base + QM_VF_EQ_INT_MASK); 2004 writel(0x1, qm->io_base + QM_VF_AEQ_INT_MASK); 2005 2006 /* Stop all qps belong to this qm */ 2007 for (i = 0; i < qm->qp_num; i++) { 2008 qp = qm->qp_array[i]; 2009 if (qp) { 2010 ret = hisi_qm_stop_qp(qp); 2011 if (ret < 0) { 2012 dev_err(dev, "Failed to stop qp%d!\n", i); 2013 return -EBUSY; 2014 } 2015 } 2016 } 2017 2018 if (qm->fun_type == QM_HW_PF) { 2019 ret = hisi_qm_set_vft(qm, 0, 0, 0); 2020 if (ret < 0) 2021 dev_err(dev, "Failed to set vft!\n"); 2022 } 2023 2024 return ret; 2025 } 2026 EXPORT_SYMBOL_GPL(hisi_qm_stop); 2027 2028 /** 2029 * hisi_qm_debug_init() - Initialize qm related debugfs files. 2030 * @qm: The qm for which we want to add debugfs files. 2031 * 2032 * Create qm related debugfs files. 2033 */ 2034 int hisi_qm_debug_init(struct hisi_qm *qm) 2035 { 2036 struct dentry *qm_d; 2037 int i, ret; 2038 2039 qm_d = debugfs_create_dir("qm", qm->debug.debug_root); 2040 qm->debug.qm_d = qm_d; 2041 2042 /* only show this in PF */ 2043 if (qm->fun_type == QM_HW_PF) 2044 for (i = CURRENT_Q; i < DEBUG_FILE_NUM; i++) 2045 if (qm_create_debugfs_file(qm, i)) { 2046 ret = -ENOENT; 2047 goto failed_to_create; 2048 } 2049 2050 debugfs_create_file("qm_regs", 0444, qm->debug.qm_d, qm, &qm_regs_fops); 2051 2052 return 0; 2053 2054 failed_to_create: 2055 debugfs_remove_recursive(qm_d); 2056 return ret; 2057 } 2058 EXPORT_SYMBOL_GPL(hisi_qm_debug_init); 2059 2060 /** 2061 * hisi_qm_debug_regs_clear() - clear qm debug related registers. 2062 * @qm: The qm for which we want to clear its debug registers. 2063 */ 2064 void hisi_qm_debug_regs_clear(struct hisi_qm *qm) 2065 { 2066 struct qm_dfx_registers *regs; 2067 int i; 2068 2069 /* clear current_q */ 2070 writel(0x0, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 2071 writel(0x0, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 2072 2073 /* 2074 * these registers are reading and clearing, so clear them after 2075 * reading them. 2076 */ 2077 writel(0x1, qm->io_base + QM_DFX_CNT_CLR_CE); 2078 2079 regs = qm_dfx_regs; 2080 for (i = 0; i < CNT_CYC_REGS_NUM; i++) { 2081 readl(qm->io_base + regs->reg_offset); 2082 regs++; 2083 } 2084 2085 writel(0x0, qm->io_base + QM_DFX_CNT_CLR_CE); 2086 } 2087 EXPORT_SYMBOL_GPL(hisi_qm_debug_regs_clear); 2088 2089 static void qm_hw_error_init(struct hisi_qm *qm) 2090 { 2091 const struct hisi_qm_err_info *err_info = &qm->err_ini->err_info; 2092 2093 if (!qm->ops->hw_error_init) { 2094 dev_err(&qm->pdev->dev, "QM doesn't support hw error handling!\n"); 2095 return; 2096 } 2097 2098 qm->ops->hw_error_init(qm, err_info->ce, err_info->nfe, 2099 err_info->fe, err_info->msi); 2100 } 2101 2102 static void qm_hw_error_uninit(struct hisi_qm *qm) 2103 { 2104 if (!qm->ops->hw_error_uninit) { 2105 dev_err(&qm->pdev->dev, "Unexpected QM hw error uninit!\n"); 2106 return; 2107 } 2108 2109 qm->ops->hw_error_uninit(qm); 2110 } 2111 2112 static pci_ers_result_t qm_hw_error_handle(struct hisi_qm *qm) 2113 { 2114 if (!qm->ops->hw_error_handle) { 2115 dev_err(&qm->pdev->dev, "QM doesn't support hw error report!\n"); 2116 return PCI_ERS_RESULT_NONE; 2117 } 2118 2119 return qm->ops->hw_error_handle(qm); 2120 } 2121 2122 /** 2123 * hisi_qm_get_hw_version() - Get hardware version of a qm. 2124 * @pdev: The device which hardware version we want to get. 2125 * 2126 * This function gets the hardware version of a qm. Return QM_HW_UNKNOWN 2127 * if the hardware version is not supported. 2128 */ 2129 enum qm_hw_ver hisi_qm_get_hw_version(struct pci_dev *pdev) 2130 { 2131 switch (pdev->revision) { 2132 case QM_HW_V1: 2133 case QM_HW_V2: 2134 return pdev->revision; 2135 default: 2136 return QM_HW_UNKNOWN; 2137 } 2138 } 2139 EXPORT_SYMBOL_GPL(hisi_qm_get_hw_version); 2140 2141 /** 2142 * hisi_qm_dev_err_init() - Initialize device error configuration. 2143 * @qm: The qm for which we want to do error initialization. 2144 * 2145 * Initialize QM and device error related configuration. 2146 */ 2147 void hisi_qm_dev_err_init(struct hisi_qm *qm) 2148 { 2149 if (qm->fun_type == QM_HW_VF) 2150 return; 2151 2152 qm_hw_error_init(qm); 2153 2154 if (!qm->err_ini->hw_err_enable) { 2155 dev_err(&qm->pdev->dev, "Device doesn't support hw error init!\n"); 2156 return; 2157 } 2158 qm->err_ini->hw_err_enable(qm); 2159 } 2160 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_init); 2161 2162 /** 2163 * hisi_qm_dev_err_uninit() - Uninitialize device error configuration. 2164 * @qm: The qm for which we want to do error uninitialization. 2165 * 2166 * Uninitialize QM and device error related configuration. 2167 */ 2168 void hisi_qm_dev_err_uninit(struct hisi_qm *qm) 2169 { 2170 if (qm->fun_type == QM_HW_VF) 2171 return; 2172 2173 qm_hw_error_uninit(qm); 2174 2175 if (!qm->err_ini->hw_err_disable) { 2176 dev_err(&qm->pdev->dev, "Unexpected device hw error uninit!\n"); 2177 return; 2178 } 2179 qm->err_ini->hw_err_disable(qm); 2180 } 2181 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_uninit); 2182 2183 /** 2184 * hisi_qm_free_qps() - free multiple queue pairs. 2185 * @qps: The queue pairs need to be freed. 2186 * @qp_num: The num of queue pairs. 2187 */ 2188 void hisi_qm_free_qps(struct hisi_qp **qps, int qp_num) 2189 { 2190 int i; 2191 2192 if (!qps || qp_num <= 0) 2193 return; 2194 2195 for (i = qp_num - 1; i >= 0; i--) 2196 hisi_qm_release_qp(qps[i]); 2197 } 2198 EXPORT_SYMBOL_GPL(hisi_qm_free_qps); 2199 2200 static void free_list(struct list_head *head) 2201 { 2202 struct hisi_qm_resource *res, *tmp; 2203 2204 list_for_each_entry_safe(res, tmp, head, list) { 2205 list_del(&res->list); 2206 kfree(res); 2207 } 2208 } 2209 2210 static int hisi_qm_sort_devices(int node, struct list_head *head, 2211 struct hisi_qm_list *qm_list) 2212 { 2213 struct hisi_qm_resource *res, *tmp; 2214 struct hisi_qm *qm; 2215 struct list_head *n; 2216 struct device *dev; 2217 int dev_node = 0; 2218 2219 list_for_each_entry(qm, &qm_list->list, list) { 2220 dev = &qm->pdev->dev; 2221 2222 if (IS_ENABLED(CONFIG_NUMA)) { 2223 dev_node = dev_to_node(dev); 2224 if (dev_node < 0) 2225 dev_node = 0; 2226 } 2227 2228 res = kzalloc(sizeof(*res), GFP_KERNEL); 2229 if (!res) 2230 return -ENOMEM; 2231 2232 res->qm = qm; 2233 res->distance = node_distance(dev_node, node); 2234 n = head; 2235 list_for_each_entry(tmp, head, list) { 2236 if (res->distance < tmp->distance) { 2237 n = &tmp->list; 2238 break; 2239 } 2240 } 2241 list_add_tail(&res->list, n); 2242 } 2243 2244 return 0; 2245 } 2246 2247 /** 2248 * hisi_qm_alloc_qps_node() - Create multiple queue pairs. 2249 * @qm_list: The list of all available devices. 2250 * @qp_num: The number of queue pairs need created. 2251 * @alg_type: The algorithm type. 2252 * @node: The numa node. 2253 * @qps: The queue pairs need created. 2254 * 2255 * This function will sort all available device according to numa distance. 2256 * Then try to create all queue pairs from one device, if all devices do 2257 * not meet the requirements will return error. 2258 */ 2259 int hisi_qm_alloc_qps_node(struct hisi_qm_list *qm_list, int qp_num, 2260 u8 alg_type, int node, struct hisi_qp **qps) 2261 { 2262 struct hisi_qm_resource *tmp; 2263 int ret = -ENODEV; 2264 LIST_HEAD(head); 2265 int i; 2266 2267 if (!qps || !qm_list || qp_num <= 0) 2268 return -EINVAL; 2269 2270 mutex_lock(&qm_list->lock); 2271 if (hisi_qm_sort_devices(node, &head, qm_list)) { 2272 mutex_unlock(&qm_list->lock); 2273 goto err; 2274 } 2275 2276 list_for_each_entry(tmp, &head, list) { 2277 for (i = 0; i < qp_num; i++) { 2278 qps[i] = hisi_qm_create_qp(tmp->qm, alg_type); 2279 if (IS_ERR(qps[i])) { 2280 hisi_qm_free_qps(qps, i); 2281 break; 2282 } 2283 } 2284 2285 if (i == qp_num) { 2286 ret = 0; 2287 break; 2288 } 2289 } 2290 2291 mutex_unlock(&qm_list->lock); 2292 if (ret) 2293 pr_info("Failed to create qps, node[%d], alg[%d], qp[%d]!\n", 2294 node, alg_type, qp_num); 2295 2296 err: 2297 free_list(&head); 2298 return ret; 2299 } 2300 EXPORT_SYMBOL_GPL(hisi_qm_alloc_qps_node); 2301 2302 static pci_ers_result_t qm_dev_err_handle(struct hisi_qm *qm) 2303 { 2304 u32 err_sts; 2305 2306 if (!qm->err_ini->get_dev_hw_err_status) { 2307 dev_err(&qm->pdev->dev, "Device doesn't support get hw error status!\n"); 2308 return PCI_ERS_RESULT_NONE; 2309 } 2310 2311 /* get device hardware error status */ 2312 err_sts = qm->err_ini->get_dev_hw_err_status(qm); 2313 if (err_sts) { 2314 if (!qm->err_ini->log_dev_hw_err) { 2315 dev_err(&qm->pdev->dev, "Device doesn't support log hw error!\n"); 2316 return PCI_ERS_RESULT_NEED_RESET; 2317 } 2318 2319 qm->err_ini->log_dev_hw_err(qm, err_sts); 2320 return PCI_ERS_RESULT_NEED_RESET; 2321 } 2322 2323 return PCI_ERS_RESULT_RECOVERED; 2324 } 2325 2326 static pci_ers_result_t qm_process_dev_error(struct pci_dev *pdev) 2327 { 2328 struct hisi_qm *qm = pci_get_drvdata(pdev); 2329 pci_ers_result_t qm_ret, dev_ret; 2330 2331 /* log qm error */ 2332 qm_ret = qm_hw_error_handle(qm); 2333 2334 /* log device error */ 2335 dev_ret = qm_dev_err_handle(qm); 2336 2337 return (qm_ret == PCI_ERS_RESULT_NEED_RESET || 2338 dev_ret == PCI_ERS_RESULT_NEED_RESET) ? 2339 PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED; 2340 } 2341 2342 /** 2343 * hisi_qm_dev_err_detected() - Get device and qm error status then log it. 2344 * @pdev: The PCI device which need report error. 2345 * @state: The connectivity between CPU and device. 2346 * 2347 * We register this function into PCIe AER handlers, It will report device or 2348 * qm hardware error status when error occur. 2349 */ 2350 pci_ers_result_t hisi_qm_dev_err_detected(struct pci_dev *pdev, 2351 pci_channel_state_t state) 2352 { 2353 if (pdev->is_virtfn) 2354 return PCI_ERS_RESULT_NONE; 2355 2356 pci_info(pdev, "PCI error detected, state(=%d)!!\n", state); 2357 if (state == pci_channel_io_perm_failure) 2358 return PCI_ERS_RESULT_DISCONNECT; 2359 2360 return qm_process_dev_error(pdev); 2361 } 2362 EXPORT_SYMBOL_GPL(hisi_qm_dev_err_detected); 2363 2364 MODULE_LICENSE("GPL v2"); 2365 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 2366 MODULE_DESCRIPTION("HiSilicon Accelerator queue manager driver"); 2367