1 // SPDX-License-Identifier: GPL-2.0 2 /* Copyright (c) 2019 HiSilicon Limited. */ 3 #include <linux/acpi.h> 4 #include <linux/aer.h> 5 #include <linux/bitops.h> 6 #include <linux/debugfs.h> 7 #include <linux/init.h> 8 #include <linux/io.h> 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/pci.h> 12 #include <linux/pm_runtime.h> 13 #include <linux/seq_file.h> 14 #include <linux/topology.h> 15 #include <linux/uacce.h> 16 #include "zip.h" 17 18 #define PCI_DEVICE_ID_ZIP_PF 0xa250 19 #define PCI_DEVICE_ID_ZIP_VF 0xa251 20 21 #define HZIP_QUEUE_NUM_V1 4096 22 23 #define HZIP_CLOCK_GATE_CTRL 0x301004 24 #define COMP0_ENABLE BIT(0) 25 #define COMP1_ENABLE BIT(1) 26 #define DECOMP0_ENABLE BIT(2) 27 #define DECOMP1_ENABLE BIT(3) 28 #define DECOMP2_ENABLE BIT(4) 29 #define DECOMP3_ENABLE BIT(5) 30 #define DECOMP4_ENABLE BIT(6) 31 #define DECOMP5_ENABLE BIT(7) 32 #define HZIP_ALL_COMP_DECOMP_EN (COMP0_ENABLE | COMP1_ENABLE | \ 33 DECOMP0_ENABLE | DECOMP1_ENABLE | \ 34 DECOMP2_ENABLE | DECOMP3_ENABLE | \ 35 DECOMP4_ENABLE | DECOMP5_ENABLE) 36 #define HZIP_DECOMP_CHECK_ENABLE BIT(16) 37 #define HZIP_FSM_MAX_CNT 0x301008 38 39 #define HZIP_PORT_ARCA_CHE_0 0x301040 40 #define HZIP_PORT_ARCA_CHE_1 0x301044 41 #define HZIP_PORT_AWCA_CHE_0 0x301060 42 #define HZIP_PORT_AWCA_CHE_1 0x301064 43 #define HZIP_CACHE_ALL_EN 0xffffffff 44 45 #define HZIP_BD_RUSER_32_63 0x301110 46 #define HZIP_SGL_RUSER_32_63 0x30111c 47 #define HZIP_DATA_RUSER_32_63 0x301128 48 #define HZIP_DATA_WUSER_32_63 0x301134 49 #define HZIP_BD_WUSER_32_63 0x301140 50 51 #define HZIP_QM_IDEL_STATUS 0x3040e4 52 53 #define HZIP_CORE_DEBUG_COMP_0 0x302000 54 #define HZIP_CORE_DEBUG_COMP_1 0x303000 55 #define HZIP_CORE_DEBUG_DECOMP_0 0x304000 56 #define HZIP_CORE_DEBUG_DECOMP_1 0x305000 57 #define HZIP_CORE_DEBUG_DECOMP_2 0x306000 58 #define HZIP_CORE_DEBUG_DECOMP_3 0x307000 59 #define HZIP_CORE_DEBUG_DECOMP_4 0x308000 60 #define HZIP_CORE_DEBUG_DECOMP_5 0x309000 61 62 #define HZIP_CORE_INT_SOURCE 0x3010A0 63 #define HZIP_CORE_INT_MASK_REG 0x3010A4 64 #define HZIP_CORE_INT_SET 0x3010A8 65 #define HZIP_CORE_INT_STATUS 0x3010AC 66 #define HZIP_CORE_INT_STATUS_M_ECC BIT(1) 67 #define HZIP_CORE_SRAM_ECC_ERR_INFO 0x301148 68 #define HZIP_CORE_INT_RAS_CE_ENB 0x301160 69 #define HZIP_CORE_INT_RAS_CE_ENABLE 0x1 70 #define HZIP_CORE_INT_RAS_NFE_ENB 0x301164 71 #define HZIP_CORE_INT_RAS_FE_ENB 0x301168 72 #define HZIP_OOO_SHUTDOWN_SEL 0x30120C 73 #define HZIP_CORE_INT_RAS_NFE_ENABLE 0x1FFE 74 #define HZIP_SRAM_ECC_ERR_NUM_SHIFT 16 75 #define HZIP_SRAM_ECC_ERR_ADDR_SHIFT 24 76 #define HZIP_CORE_INT_MASK_ALL GENMASK(12, 0) 77 #define HZIP_COMP_CORE_NUM 2 78 #define HZIP_DECOMP_CORE_NUM 6 79 #define HZIP_CORE_NUM (HZIP_COMP_CORE_NUM + \ 80 HZIP_DECOMP_CORE_NUM) 81 #define HZIP_SQE_SIZE 128 82 #define HZIP_SQ_SIZE (HZIP_SQE_SIZE * QM_Q_DEPTH) 83 #define HZIP_PF_DEF_Q_NUM 64 84 #define HZIP_PF_DEF_Q_BASE 0 85 86 #define HZIP_SOFT_CTRL_CNT_CLR_CE 0x301000 87 #define HZIP_SOFT_CTRL_CNT_CLR_CE_BIT BIT(0) 88 #define HZIP_SOFT_CTRL_ZIP_CONTROL 0x30100C 89 #define HZIP_AXI_SHUTDOWN_ENABLE BIT(14) 90 #define HZIP_WR_PORT BIT(11) 91 92 #define HZIP_BUF_SIZE 22 93 #define HZIP_SQE_MASK_OFFSET 64 94 #define HZIP_SQE_MASK_LEN 48 95 96 #define HZIP_CNT_CLR_CE_EN BIT(0) 97 #define HZIP_RO_CNT_CLR_CE_EN BIT(2) 98 #define HZIP_RD_CNT_CLR_CE_EN (HZIP_CNT_CLR_CE_EN | \ 99 HZIP_RO_CNT_CLR_CE_EN) 100 101 #define HZIP_PREFETCH_CFG 0x3011B0 102 #define HZIP_SVA_TRANS 0x3011C4 103 #define HZIP_PREFETCH_ENABLE (~(BIT(26) | BIT(17) | BIT(0))) 104 #define HZIP_SVA_PREFETCH_DISABLE BIT(26) 105 #define HZIP_SVA_DISABLE_READY (BIT(26) | BIT(30)) 106 #define HZIP_SHAPER_RATE_COMPRESS 750 107 #define HZIP_SHAPER_RATE_DECOMPRESS 140 108 #define HZIP_DELAY_1_US 1 109 #define HZIP_POLL_TIMEOUT_US 1000 110 111 /* clock gating */ 112 #define HZIP_PEH_CFG_AUTO_GATE 0x3011A8 113 #define HZIP_PEH_CFG_AUTO_GATE_EN BIT(0) 114 #define HZIP_CORE_GATED_EN GENMASK(15, 8) 115 #define HZIP_CORE_GATED_OOO_EN BIT(29) 116 #define HZIP_CLOCK_GATED_EN (HZIP_CORE_GATED_EN | \ 117 HZIP_CORE_GATED_OOO_EN) 118 119 static const char hisi_zip_name[] = "hisi_zip"; 120 static struct dentry *hzip_debugfs_root; 121 122 struct hisi_zip_hw_error { 123 u32 int_msk; 124 const char *msg; 125 }; 126 127 struct zip_dfx_item { 128 const char *name; 129 u32 offset; 130 }; 131 132 static struct hisi_qm_list zip_devices = { 133 .register_to_crypto = hisi_zip_register_to_crypto, 134 .unregister_from_crypto = hisi_zip_unregister_from_crypto, 135 }; 136 137 static struct zip_dfx_item zip_dfx_files[] = { 138 {"send_cnt", offsetof(struct hisi_zip_dfx, send_cnt)}, 139 {"recv_cnt", offsetof(struct hisi_zip_dfx, recv_cnt)}, 140 {"send_busy_cnt", offsetof(struct hisi_zip_dfx, send_busy_cnt)}, 141 {"err_bd_cnt", offsetof(struct hisi_zip_dfx, err_bd_cnt)}, 142 }; 143 144 static const struct hisi_zip_hw_error zip_hw_error[] = { 145 { .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" }, 146 { .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" }, 147 { .int_msk = BIT(2), .msg = "zip_axi_rresp_err" }, 148 { .int_msk = BIT(3), .msg = "zip_axi_bresp_err" }, 149 { .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" }, 150 { .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" }, 151 { .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" }, 152 { .int_msk = BIT(7), .msg = "zip_pre_in_data_err" }, 153 { .int_msk = BIT(8), .msg = "zip_com_inf_err" }, 154 { .int_msk = BIT(9), .msg = "zip_enc_inf_err" }, 155 { .int_msk = BIT(10), .msg = "zip_pre_out_err" }, 156 { .int_msk = BIT(11), .msg = "zip_axi_poison_err" }, 157 { .int_msk = BIT(12), .msg = "zip_sva_err" }, 158 { /* sentinel */ } 159 }; 160 161 enum ctrl_debug_file_index { 162 HZIP_CLEAR_ENABLE, 163 HZIP_DEBUG_FILE_NUM, 164 }; 165 166 static const char * const ctrl_debug_file_name[] = { 167 [HZIP_CLEAR_ENABLE] = "clear_enable", 168 }; 169 170 struct ctrl_debug_file { 171 enum ctrl_debug_file_index index; 172 spinlock_t lock; 173 struct hisi_zip_ctrl *ctrl; 174 }; 175 176 /* 177 * One ZIP controller has one PF and multiple VFs, some global configurations 178 * which PF has need this structure. 179 * 180 * Just relevant for PF. 181 */ 182 struct hisi_zip_ctrl { 183 struct hisi_zip *hisi_zip; 184 struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM]; 185 }; 186 187 enum { 188 HZIP_COMP_CORE0, 189 HZIP_COMP_CORE1, 190 HZIP_DECOMP_CORE0, 191 HZIP_DECOMP_CORE1, 192 HZIP_DECOMP_CORE2, 193 HZIP_DECOMP_CORE3, 194 HZIP_DECOMP_CORE4, 195 HZIP_DECOMP_CORE5, 196 }; 197 198 static const u64 core_offsets[] = { 199 [HZIP_COMP_CORE0] = 0x302000, 200 [HZIP_COMP_CORE1] = 0x303000, 201 [HZIP_DECOMP_CORE0] = 0x304000, 202 [HZIP_DECOMP_CORE1] = 0x305000, 203 [HZIP_DECOMP_CORE2] = 0x306000, 204 [HZIP_DECOMP_CORE3] = 0x307000, 205 [HZIP_DECOMP_CORE4] = 0x308000, 206 [HZIP_DECOMP_CORE5] = 0x309000, 207 }; 208 209 static const struct debugfs_reg32 hzip_dfx_regs[] = { 210 {"HZIP_GET_BD_NUM ", 0x00ull}, 211 {"HZIP_GET_RIGHT_BD ", 0x04ull}, 212 {"HZIP_GET_ERROR_BD ", 0x08ull}, 213 {"HZIP_DONE_BD_NUM ", 0x0cull}, 214 {"HZIP_WORK_CYCLE ", 0x10ull}, 215 {"HZIP_IDLE_CYCLE ", 0x18ull}, 216 {"HZIP_MAX_DELAY ", 0x20ull}, 217 {"HZIP_MIN_DELAY ", 0x24ull}, 218 {"HZIP_AVG_DELAY ", 0x28ull}, 219 {"HZIP_MEM_VISIBLE_DATA ", 0x30ull}, 220 {"HZIP_MEM_VISIBLE_ADDR ", 0x34ull}, 221 {"HZIP_CONSUMED_BYTE ", 0x38ull}, 222 {"HZIP_PRODUCED_BYTE ", 0x40ull}, 223 {"HZIP_COMP_INF ", 0x70ull}, 224 {"HZIP_PRE_OUT ", 0x78ull}, 225 {"HZIP_BD_RD ", 0x7cull}, 226 {"HZIP_BD_WR ", 0x80ull}, 227 {"HZIP_GET_BD_AXI_ERR_NUM ", 0x84ull}, 228 {"HZIP_GET_BD_PARSE_ERR_NUM ", 0x88ull}, 229 {"HZIP_ADD_BD_AXI_ERR_NUM ", 0x8cull}, 230 {"HZIP_DECOMP_STF_RELOAD_CURR_ST ", 0x94ull}, 231 {"HZIP_DECOMP_LZ77_CURR_ST ", 0x9cull}, 232 }; 233 234 static const struct kernel_param_ops zip_uacce_mode_ops = { 235 .set = uacce_mode_set, 236 .get = param_get_int, 237 }; 238 239 /* 240 * uacce_mode = 0 means zip only register to crypto, 241 * uacce_mode = 1 means zip both register to crypto and uacce. 242 */ 243 static u32 uacce_mode = UACCE_MODE_NOUACCE; 244 module_param_cb(uacce_mode, &zip_uacce_mode_ops, &uacce_mode, 0444); 245 MODULE_PARM_DESC(uacce_mode, UACCE_MODE_DESC); 246 247 static int pf_q_num_set(const char *val, const struct kernel_param *kp) 248 { 249 return q_num_set(val, kp, PCI_DEVICE_ID_ZIP_PF); 250 } 251 252 static const struct kernel_param_ops pf_q_num_ops = { 253 .set = pf_q_num_set, 254 .get = param_get_int, 255 }; 256 257 static u32 pf_q_num = HZIP_PF_DEF_Q_NUM; 258 module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444); 259 MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 2-4096, v2 2-1024)"); 260 261 static const struct kernel_param_ops vfs_num_ops = { 262 .set = vfs_num_set, 263 .get = param_get_int, 264 }; 265 266 static u32 vfs_num; 267 module_param_cb(vfs_num, &vfs_num_ops, &vfs_num, 0444); 268 MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63), 0(default)"); 269 270 static const struct pci_device_id hisi_zip_dev_ids[] = { 271 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) }, 272 { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) }, 273 { 0, } 274 }; 275 MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids); 276 277 int zip_create_qps(struct hisi_qp **qps, int qp_num, int node) 278 { 279 if (node == NUMA_NO_NODE) 280 node = cpu_to_node(smp_processor_id()); 281 282 return hisi_qm_alloc_qps_node(&zip_devices, qp_num, 0, node, qps); 283 } 284 285 static void hisi_zip_open_sva_prefetch(struct hisi_qm *qm) 286 { 287 u32 val; 288 int ret; 289 290 if (qm->ver < QM_HW_V3) 291 return; 292 293 /* Enable prefetch */ 294 val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG); 295 val &= HZIP_PREFETCH_ENABLE; 296 writel(val, qm->io_base + HZIP_PREFETCH_CFG); 297 298 ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_PREFETCH_CFG, 299 val, !(val & HZIP_SVA_PREFETCH_DISABLE), 300 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US); 301 if (ret) 302 pci_err(qm->pdev, "failed to open sva prefetch\n"); 303 } 304 305 static void hisi_zip_close_sva_prefetch(struct hisi_qm *qm) 306 { 307 u32 val; 308 int ret; 309 310 if (qm->ver < QM_HW_V3) 311 return; 312 313 val = readl_relaxed(qm->io_base + HZIP_PREFETCH_CFG); 314 val |= HZIP_SVA_PREFETCH_DISABLE; 315 writel(val, qm->io_base + HZIP_PREFETCH_CFG); 316 317 ret = readl_relaxed_poll_timeout(qm->io_base + HZIP_SVA_TRANS, 318 val, !(val & HZIP_SVA_DISABLE_READY), 319 HZIP_DELAY_1_US, HZIP_POLL_TIMEOUT_US); 320 if (ret) 321 pci_err(qm->pdev, "failed to close sva prefetch\n"); 322 } 323 324 static void hisi_zip_enable_clock_gate(struct hisi_qm *qm) 325 { 326 u32 val; 327 328 if (qm->ver < QM_HW_V3) 329 return; 330 331 val = readl(qm->io_base + HZIP_CLOCK_GATE_CTRL); 332 val |= HZIP_CLOCK_GATED_EN; 333 writel(val, qm->io_base + HZIP_CLOCK_GATE_CTRL); 334 335 val = readl(qm->io_base + HZIP_PEH_CFG_AUTO_GATE); 336 val |= HZIP_PEH_CFG_AUTO_GATE_EN; 337 writel(val, qm->io_base + HZIP_PEH_CFG_AUTO_GATE); 338 } 339 340 static int hisi_zip_set_user_domain_and_cache(struct hisi_qm *qm) 341 { 342 void __iomem *base = qm->io_base; 343 344 /* qm user domain */ 345 writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1); 346 writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE); 347 writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1); 348 writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE); 349 writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE); 350 351 /* qm cache */ 352 writel(AXI_M_CFG, base + QM_AXI_M_CFG); 353 writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE); 354 355 /* disable FLR triggered by BME(bus master enable) */ 356 writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG); 357 writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE); 358 359 /* cache */ 360 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0); 361 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1); 362 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0); 363 writel(HZIP_CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1); 364 365 /* user domain configurations */ 366 writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63); 367 writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63); 368 369 if (qm->use_sva && qm->ver == QM_HW_V2) { 370 writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_RUSER_32_63); 371 writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_DATA_WUSER_32_63); 372 writel(AXUSER_BASE | AXUSER_SSV, base + HZIP_SGL_RUSER_32_63); 373 } else { 374 writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63); 375 writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63); 376 writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63); 377 } 378 379 /* let's open all compression/decompression cores */ 380 writel(HZIP_DECOMP_CHECK_ENABLE | HZIP_ALL_COMP_DECOMP_EN, 381 base + HZIP_CLOCK_GATE_CTRL); 382 383 /* enable sqc,cqc writeback */ 384 writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE | 385 CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) | 386 FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL); 387 388 hisi_zip_enable_clock_gate(qm); 389 390 return 0; 391 } 392 393 static void hisi_zip_master_ooo_ctrl(struct hisi_qm *qm, bool enable) 394 { 395 u32 val1, val2; 396 397 val1 = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL); 398 if (enable) { 399 val1 |= HZIP_AXI_SHUTDOWN_ENABLE; 400 val2 = HZIP_CORE_INT_RAS_NFE_ENABLE; 401 } else { 402 val1 &= ~HZIP_AXI_SHUTDOWN_ENABLE; 403 val2 = 0x0; 404 } 405 406 if (qm->ver > QM_HW_V2) 407 writel(val2, qm->io_base + HZIP_OOO_SHUTDOWN_SEL); 408 409 writel(val1, qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL); 410 } 411 412 static void hisi_zip_hw_error_enable(struct hisi_qm *qm) 413 { 414 if (qm->ver == QM_HW_V1) { 415 writel(HZIP_CORE_INT_MASK_ALL, 416 qm->io_base + HZIP_CORE_INT_MASK_REG); 417 dev_info(&qm->pdev->dev, "Does not support hw error handle\n"); 418 return; 419 } 420 421 /* clear ZIP hw error source if having */ 422 writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_SOURCE); 423 424 /* configure error type */ 425 writel(HZIP_CORE_INT_RAS_CE_ENABLE, 426 qm->io_base + HZIP_CORE_INT_RAS_CE_ENB); 427 writel(0x0, qm->io_base + HZIP_CORE_INT_RAS_FE_ENB); 428 writel(HZIP_CORE_INT_RAS_NFE_ENABLE, 429 qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB); 430 431 /* enable ZIP block master OOO when nfe occurs on Kunpeng930 */ 432 hisi_zip_master_ooo_ctrl(qm, true); 433 434 /* enable ZIP hw error interrupts */ 435 writel(0, qm->io_base + HZIP_CORE_INT_MASK_REG); 436 } 437 438 static void hisi_zip_hw_error_disable(struct hisi_qm *qm) 439 { 440 /* disable ZIP hw error interrupts */ 441 writel(HZIP_CORE_INT_MASK_ALL, qm->io_base + HZIP_CORE_INT_MASK_REG); 442 443 /* disable ZIP block master OOO when nfe occurs on Kunpeng930 */ 444 hisi_zip_master_ooo_ctrl(qm, false); 445 } 446 447 static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file) 448 { 449 struct hisi_zip *hisi_zip = file->ctrl->hisi_zip; 450 451 return &hisi_zip->qm; 452 } 453 454 static u32 clear_enable_read(struct hisi_qm *qm) 455 { 456 return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 457 HZIP_SOFT_CTRL_CNT_CLR_CE_BIT; 458 } 459 460 static int clear_enable_write(struct hisi_qm *qm, u32 val) 461 { 462 u32 tmp; 463 464 if (val != 1 && val != 0) 465 return -EINVAL; 466 467 tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 468 ~HZIP_SOFT_CTRL_CNT_CLR_CE_BIT) | val; 469 writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 470 471 return 0; 472 } 473 474 static ssize_t hisi_zip_ctrl_debug_read(struct file *filp, char __user *buf, 475 size_t count, loff_t *pos) 476 { 477 struct ctrl_debug_file *file = filp->private_data; 478 struct hisi_qm *qm = file_to_qm(file); 479 char tbuf[HZIP_BUF_SIZE]; 480 u32 val; 481 int ret; 482 483 ret = hisi_qm_get_dfx_access(qm); 484 if (ret) 485 return ret; 486 487 spin_lock_irq(&file->lock); 488 switch (file->index) { 489 case HZIP_CLEAR_ENABLE: 490 val = clear_enable_read(qm); 491 break; 492 default: 493 goto err_input; 494 } 495 spin_unlock_irq(&file->lock); 496 497 hisi_qm_put_dfx_access(qm); 498 ret = scnprintf(tbuf, sizeof(tbuf), "%u\n", val); 499 return simple_read_from_buffer(buf, count, pos, tbuf, ret); 500 501 err_input: 502 spin_unlock_irq(&file->lock); 503 hisi_qm_put_dfx_access(qm); 504 return -EINVAL; 505 } 506 507 static ssize_t hisi_zip_ctrl_debug_write(struct file *filp, 508 const char __user *buf, 509 size_t count, loff_t *pos) 510 { 511 struct ctrl_debug_file *file = filp->private_data; 512 struct hisi_qm *qm = file_to_qm(file); 513 char tbuf[HZIP_BUF_SIZE]; 514 unsigned long val; 515 int len, ret; 516 517 if (*pos != 0) 518 return 0; 519 520 if (count >= HZIP_BUF_SIZE) 521 return -ENOSPC; 522 523 len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count); 524 if (len < 0) 525 return len; 526 527 tbuf[len] = '\0'; 528 if (kstrtoul(tbuf, 0, &val)) 529 return -EFAULT; 530 531 ret = hisi_qm_get_dfx_access(qm); 532 if (ret) 533 return ret; 534 535 spin_lock_irq(&file->lock); 536 switch (file->index) { 537 case HZIP_CLEAR_ENABLE: 538 ret = clear_enable_write(qm, val); 539 if (ret) 540 goto err_input; 541 break; 542 default: 543 ret = -EINVAL; 544 goto err_input; 545 } 546 547 ret = count; 548 549 err_input: 550 spin_unlock_irq(&file->lock); 551 hisi_qm_put_dfx_access(qm); 552 return ret; 553 } 554 555 static const struct file_operations ctrl_debug_fops = { 556 .owner = THIS_MODULE, 557 .open = simple_open, 558 .read = hisi_zip_ctrl_debug_read, 559 .write = hisi_zip_ctrl_debug_write, 560 }; 561 562 static int zip_debugfs_atomic64_set(void *data, u64 val) 563 { 564 if (val) 565 return -EINVAL; 566 567 atomic64_set((atomic64_t *)data, 0); 568 569 return 0; 570 } 571 572 static int zip_debugfs_atomic64_get(void *data, u64 *val) 573 { 574 *val = atomic64_read((atomic64_t *)data); 575 576 return 0; 577 } 578 579 DEFINE_DEBUGFS_ATTRIBUTE(zip_atomic64_ops, zip_debugfs_atomic64_get, 580 zip_debugfs_atomic64_set, "%llu\n"); 581 582 static int hisi_zip_regs_show(struct seq_file *s, void *unused) 583 { 584 hisi_qm_regs_dump(s, s->private); 585 586 return 0; 587 } 588 589 DEFINE_SHOW_ATTRIBUTE(hisi_zip_regs); 590 591 static int hisi_zip_core_debug_init(struct hisi_qm *qm) 592 { 593 struct device *dev = &qm->pdev->dev; 594 struct debugfs_regset32 *regset; 595 struct dentry *tmp_d; 596 char buf[HZIP_BUF_SIZE]; 597 int i; 598 599 for (i = 0; i < HZIP_CORE_NUM; i++) { 600 if (i < HZIP_COMP_CORE_NUM) 601 scnprintf(buf, sizeof(buf), "comp_core%d", i); 602 else 603 scnprintf(buf, sizeof(buf), "decomp_core%d", 604 i - HZIP_COMP_CORE_NUM); 605 606 regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL); 607 if (!regset) 608 return -ENOENT; 609 610 regset->regs = hzip_dfx_regs; 611 regset->nregs = ARRAY_SIZE(hzip_dfx_regs); 612 regset->base = qm->io_base + core_offsets[i]; 613 regset->dev = dev; 614 615 tmp_d = debugfs_create_dir(buf, qm->debug.debug_root); 616 debugfs_create_file("regs", 0444, tmp_d, regset, 617 &hisi_zip_regs_fops); 618 } 619 620 return 0; 621 } 622 623 static void hisi_zip_dfx_debug_init(struct hisi_qm *qm) 624 { 625 struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm); 626 struct hisi_zip_dfx *dfx = &zip->dfx; 627 struct dentry *tmp_dir; 628 void *data; 629 int i; 630 631 tmp_dir = debugfs_create_dir("zip_dfx", qm->debug.debug_root); 632 for (i = 0; i < ARRAY_SIZE(zip_dfx_files); i++) { 633 data = (atomic64_t *)((uintptr_t)dfx + zip_dfx_files[i].offset); 634 debugfs_create_file(zip_dfx_files[i].name, 635 0644, tmp_dir, data, 636 &zip_atomic64_ops); 637 } 638 } 639 640 static int hisi_zip_ctrl_debug_init(struct hisi_qm *qm) 641 { 642 struct hisi_zip *zip = container_of(qm, struct hisi_zip, qm); 643 int i; 644 645 for (i = HZIP_CLEAR_ENABLE; i < HZIP_DEBUG_FILE_NUM; i++) { 646 spin_lock_init(&zip->ctrl->files[i].lock); 647 zip->ctrl->files[i].ctrl = zip->ctrl; 648 zip->ctrl->files[i].index = i; 649 650 debugfs_create_file(ctrl_debug_file_name[i], 0600, 651 qm->debug.debug_root, 652 zip->ctrl->files + i, 653 &ctrl_debug_fops); 654 } 655 656 return hisi_zip_core_debug_init(qm); 657 } 658 659 static int hisi_zip_debugfs_init(struct hisi_qm *qm) 660 { 661 struct device *dev = &qm->pdev->dev; 662 struct dentry *dev_d; 663 int ret; 664 665 dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root); 666 667 qm->debug.sqe_mask_offset = HZIP_SQE_MASK_OFFSET; 668 qm->debug.sqe_mask_len = HZIP_SQE_MASK_LEN; 669 qm->debug.debug_root = dev_d; 670 hisi_qm_debug_init(qm); 671 672 if (qm->fun_type == QM_HW_PF) { 673 ret = hisi_zip_ctrl_debug_init(qm); 674 if (ret) 675 goto failed_to_create; 676 } 677 678 hisi_zip_dfx_debug_init(qm); 679 680 return 0; 681 682 failed_to_create: 683 debugfs_remove_recursive(hzip_debugfs_root); 684 return ret; 685 } 686 687 /* hisi_zip_debug_regs_clear() - clear the zip debug regs */ 688 static void hisi_zip_debug_regs_clear(struct hisi_qm *qm) 689 { 690 int i, j; 691 692 /* enable register read_clear bit */ 693 writel(HZIP_RD_CNT_CLR_CE_EN, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 694 for (i = 0; i < ARRAY_SIZE(core_offsets); i++) 695 for (j = 0; j < ARRAY_SIZE(hzip_dfx_regs); j++) 696 readl(qm->io_base + core_offsets[i] + 697 hzip_dfx_regs[j].offset); 698 699 /* disable register read_clear bit */ 700 writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 701 702 hisi_qm_debug_regs_clear(qm); 703 } 704 705 static void hisi_zip_debugfs_exit(struct hisi_qm *qm) 706 { 707 debugfs_remove_recursive(qm->debug.debug_root); 708 709 if (qm->fun_type == QM_HW_PF) { 710 hisi_zip_debug_regs_clear(qm); 711 qm->debug.curr_qm_qp_num = 0; 712 } 713 } 714 715 static void hisi_zip_log_hw_error(struct hisi_qm *qm, u32 err_sts) 716 { 717 const struct hisi_zip_hw_error *err = zip_hw_error; 718 struct device *dev = &qm->pdev->dev; 719 u32 err_val; 720 721 while (err->msg) { 722 if (err->int_msk & err_sts) { 723 dev_err(dev, "%s [error status=0x%x] found\n", 724 err->msg, err->int_msk); 725 726 if (err->int_msk & HZIP_CORE_INT_STATUS_M_ECC) { 727 err_val = readl(qm->io_base + 728 HZIP_CORE_SRAM_ECC_ERR_INFO); 729 dev_err(dev, "hisi-zip multi ecc sram num=0x%x\n", 730 ((err_val >> 731 HZIP_SRAM_ECC_ERR_NUM_SHIFT) & 0xFF)); 732 } 733 } 734 err++; 735 } 736 } 737 738 static u32 hisi_zip_get_hw_err_status(struct hisi_qm *qm) 739 { 740 return readl(qm->io_base + HZIP_CORE_INT_STATUS); 741 } 742 743 static void hisi_zip_clear_hw_err_status(struct hisi_qm *qm, u32 err_sts) 744 { 745 writel(err_sts, qm->io_base + HZIP_CORE_INT_SOURCE); 746 } 747 748 static void hisi_zip_open_axi_master_ooo(struct hisi_qm *qm) 749 { 750 u32 val; 751 752 val = readl(qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL); 753 754 writel(val & ~HZIP_AXI_SHUTDOWN_ENABLE, 755 qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL); 756 757 writel(val | HZIP_AXI_SHUTDOWN_ENABLE, 758 qm->io_base + HZIP_SOFT_CTRL_ZIP_CONTROL); 759 } 760 761 static void hisi_zip_close_axi_master_ooo(struct hisi_qm *qm) 762 { 763 u32 nfe_enb; 764 765 /* Disable ECC Mbit error report. */ 766 nfe_enb = readl(qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB); 767 writel(nfe_enb & ~HZIP_CORE_INT_STATUS_M_ECC, 768 qm->io_base + HZIP_CORE_INT_RAS_NFE_ENB); 769 770 /* Inject zip ECC Mbit error to block master ooo. */ 771 writel(HZIP_CORE_INT_STATUS_M_ECC, 772 qm->io_base + HZIP_CORE_INT_SET); 773 } 774 775 static void hisi_zip_err_info_init(struct hisi_qm *qm) 776 { 777 struct hisi_qm_err_info *err_info = &qm->err_info; 778 779 err_info->ce = QM_BASE_CE; 780 err_info->fe = 0; 781 err_info->ecc_2bits_mask = HZIP_CORE_INT_STATUS_M_ECC; 782 err_info->dev_ce_mask = HZIP_CORE_INT_RAS_CE_ENABLE; 783 err_info->msi_wr_port = HZIP_WR_PORT; 784 err_info->acpi_rst = "ZRST"; 785 err_info->nfe = QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT; 786 787 if (qm->ver >= QM_HW_V3) 788 err_info->nfe |= QM_ACC_DO_TASK_TIMEOUT; 789 } 790 791 static const struct hisi_qm_err_ini hisi_zip_err_ini = { 792 .hw_init = hisi_zip_set_user_domain_and_cache, 793 .hw_err_enable = hisi_zip_hw_error_enable, 794 .hw_err_disable = hisi_zip_hw_error_disable, 795 .get_dev_hw_err_status = hisi_zip_get_hw_err_status, 796 .clear_dev_hw_err_status = hisi_zip_clear_hw_err_status, 797 .log_dev_hw_err = hisi_zip_log_hw_error, 798 .open_axi_master_ooo = hisi_zip_open_axi_master_ooo, 799 .close_axi_master_ooo = hisi_zip_close_axi_master_ooo, 800 .open_sva_prefetch = hisi_zip_open_sva_prefetch, 801 .close_sva_prefetch = hisi_zip_close_sva_prefetch, 802 .err_info_init = hisi_zip_err_info_init, 803 }; 804 805 static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip) 806 { 807 struct hisi_qm *qm = &hisi_zip->qm; 808 struct hisi_zip_ctrl *ctrl; 809 810 ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL); 811 if (!ctrl) 812 return -ENOMEM; 813 814 hisi_zip->ctrl = ctrl; 815 ctrl->hisi_zip = hisi_zip; 816 qm->err_ini = &hisi_zip_err_ini; 817 qm->err_ini->err_info_init(qm); 818 819 hisi_zip_set_user_domain_and_cache(qm); 820 hisi_zip_open_sva_prefetch(qm); 821 hisi_qm_dev_err_init(qm); 822 hisi_zip_debug_regs_clear(qm); 823 824 return 0; 825 } 826 827 static int hisi_zip_qm_init(struct hisi_qm *qm, struct pci_dev *pdev) 828 { 829 int ret; 830 831 qm->pdev = pdev; 832 qm->ver = pdev->revision; 833 if (pdev->revision >= QM_HW_V3) 834 qm->algs = "zlib\ngzip\ndeflate\nlz77_zstd"; 835 else 836 qm->algs = "zlib\ngzip"; 837 qm->mode = uacce_mode; 838 qm->sqe_size = HZIP_SQE_SIZE; 839 qm->dev_name = hisi_zip_name; 840 841 qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? 842 QM_HW_PF : QM_HW_VF; 843 if (qm->fun_type == QM_HW_PF) { 844 qm->qp_base = HZIP_PF_DEF_Q_BASE; 845 qm->qp_num = pf_q_num; 846 qm->debug.curr_qm_qp_num = pf_q_num; 847 qm->qm_list = &zip_devices; 848 } else if (qm->fun_type == QM_HW_VF && qm->ver == QM_HW_V1) { 849 /* 850 * have no way to get qm configure in VM in v1 hardware, 851 * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force 852 * to trigger only one VF in v1 hardware. 853 * 854 * v2 hardware has no such problem. 855 */ 856 qm->qp_base = HZIP_PF_DEF_Q_NUM; 857 qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM; 858 } 859 860 qm->wq = alloc_workqueue("%s", WQ_HIGHPRI | WQ_MEM_RECLAIM | 861 WQ_UNBOUND, num_online_cpus(), 862 pci_name(qm->pdev)); 863 if (!qm->wq) { 864 pci_err(qm->pdev, "fail to alloc workqueue\n"); 865 return -ENOMEM; 866 } 867 868 ret = hisi_qm_init(qm); 869 if (ret) 870 destroy_workqueue(qm->wq); 871 872 return ret; 873 } 874 875 static void hisi_zip_qm_uninit(struct hisi_qm *qm) 876 { 877 hisi_qm_uninit(qm); 878 destroy_workqueue(qm->wq); 879 } 880 881 static int hisi_zip_probe_init(struct hisi_zip *hisi_zip) 882 { 883 u32 type_rate = HZIP_SHAPER_RATE_COMPRESS; 884 struct hisi_qm *qm = &hisi_zip->qm; 885 int ret; 886 887 if (qm->fun_type == QM_HW_PF) { 888 ret = hisi_zip_pf_probe_init(hisi_zip); 889 if (ret) 890 return ret; 891 /* enable shaper type 0 */ 892 if (qm->ver >= QM_HW_V3) { 893 type_rate |= QM_SHAPER_ENABLE; 894 895 /* ZIP need to enable shaper type 1 */ 896 type_rate |= HZIP_SHAPER_RATE_DECOMPRESS << QM_SHAPER_TYPE1_OFFSET; 897 qm->type_rate = type_rate; 898 } 899 } 900 901 return 0; 902 } 903 904 static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id) 905 { 906 struct hisi_zip *hisi_zip; 907 struct hisi_qm *qm; 908 int ret; 909 910 hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL); 911 if (!hisi_zip) 912 return -ENOMEM; 913 914 qm = &hisi_zip->qm; 915 916 ret = hisi_zip_qm_init(qm, pdev); 917 if (ret) { 918 pci_err(pdev, "Failed to init ZIP QM (%d)!\n", ret); 919 return ret; 920 } 921 922 ret = hisi_zip_probe_init(hisi_zip); 923 if (ret) { 924 pci_err(pdev, "Failed to probe (%d)!\n", ret); 925 goto err_qm_uninit; 926 } 927 928 ret = hisi_qm_start(qm); 929 if (ret) 930 goto err_dev_err_uninit; 931 932 ret = hisi_zip_debugfs_init(qm); 933 if (ret) 934 pci_err(pdev, "failed to init debugfs (%d)!\n", ret); 935 936 ret = hisi_qm_alg_register(qm, &zip_devices); 937 if (ret < 0) { 938 pci_err(pdev, "failed to register driver to crypto!\n"); 939 goto err_qm_stop; 940 } 941 942 if (qm->uacce) { 943 ret = uacce_register(qm->uacce); 944 if (ret) { 945 pci_err(pdev, "failed to register uacce (%d)!\n", ret); 946 goto err_qm_alg_unregister; 947 } 948 } 949 950 if (qm->fun_type == QM_HW_PF && vfs_num > 0) { 951 ret = hisi_qm_sriov_enable(pdev, vfs_num); 952 if (ret < 0) 953 goto err_qm_alg_unregister; 954 } 955 956 hisi_qm_pm_init(qm); 957 958 return 0; 959 960 err_qm_alg_unregister: 961 hisi_qm_alg_unregister(qm, &zip_devices); 962 963 err_qm_stop: 964 hisi_zip_debugfs_exit(qm); 965 hisi_qm_stop(qm, QM_NORMAL); 966 967 err_dev_err_uninit: 968 hisi_qm_dev_err_uninit(qm); 969 970 err_qm_uninit: 971 hisi_zip_qm_uninit(qm); 972 973 return ret; 974 } 975 976 static void hisi_zip_remove(struct pci_dev *pdev) 977 { 978 struct hisi_qm *qm = pci_get_drvdata(pdev); 979 980 hisi_qm_pm_uninit(qm); 981 hisi_qm_wait_task_finish(qm, &zip_devices); 982 hisi_qm_alg_unregister(qm, &zip_devices); 983 984 if (qm->fun_type == QM_HW_PF && qm->vfs_num) 985 hisi_qm_sriov_disable(pdev, true); 986 987 hisi_zip_debugfs_exit(qm); 988 hisi_qm_stop(qm, QM_NORMAL); 989 hisi_qm_dev_err_uninit(qm); 990 hisi_zip_qm_uninit(qm); 991 } 992 993 static const struct dev_pm_ops hisi_zip_pm_ops = { 994 SET_RUNTIME_PM_OPS(hisi_qm_suspend, hisi_qm_resume, NULL) 995 }; 996 997 static const struct pci_error_handlers hisi_zip_err_handler = { 998 .error_detected = hisi_qm_dev_err_detected, 999 .slot_reset = hisi_qm_dev_slot_reset, 1000 .reset_prepare = hisi_qm_reset_prepare, 1001 .reset_done = hisi_qm_reset_done, 1002 }; 1003 1004 static struct pci_driver hisi_zip_pci_driver = { 1005 .name = "hisi_zip", 1006 .id_table = hisi_zip_dev_ids, 1007 .probe = hisi_zip_probe, 1008 .remove = hisi_zip_remove, 1009 .sriov_configure = IS_ENABLED(CONFIG_PCI_IOV) ? 1010 hisi_qm_sriov_configure : NULL, 1011 .err_handler = &hisi_zip_err_handler, 1012 .shutdown = hisi_qm_dev_shutdown, 1013 .driver.pm = &hisi_zip_pm_ops, 1014 }; 1015 1016 static void hisi_zip_register_debugfs(void) 1017 { 1018 if (!debugfs_initialized()) 1019 return; 1020 1021 hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL); 1022 } 1023 1024 static void hisi_zip_unregister_debugfs(void) 1025 { 1026 debugfs_remove_recursive(hzip_debugfs_root); 1027 } 1028 1029 static int __init hisi_zip_init(void) 1030 { 1031 int ret; 1032 1033 hisi_qm_init_list(&zip_devices); 1034 hisi_zip_register_debugfs(); 1035 1036 ret = pci_register_driver(&hisi_zip_pci_driver); 1037 if (ret < 0) { 1038 hisi_zip_unregister_debugfs(); 1039 pr_err("Failed to register pci driver.\n"); 1040 } 1041 1042 return ret; 1043 } 1044 1045 static void __exit hisi_zip_exit(void) 1046 { 1047 pci_unregister_driver(&hisi_zip_pci_driver); 1048 hisi_zip_unregister_debugfs(); 1049 } 1050 1051 module_init(hisi_zip_init); 1052 module_exit(hisi_zip_exit); 1053 1054 MODULE_LICENSE("GPL v2"); 1055 MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 1056 MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator"); 1057