162c455caSZhou Wang // SPDX-License-Identifier: GPL-2.0 262c455caSZhou Wang /* Copyright (c) 2019 HiSilicon Limited. */ 362c455caSZhou Wang #include <linux/acpi.h> 462c455caSZhou Wang #include <linux/aer.h> 562c455caSZhou Wang #include <linux/bitops.h> 672c7a68dSZhou Wang #include <linux/debugfs.h> 762c455caSZhou Wang #include <linux/init.h> 862c455caSZhou Wang #include <linux/io.h> 962c455caSZhou Wang #include <linux/kernel.h> 1062c455caSZhou Wang #include <linux/module.h> 1162c455caSZhou Wang #include <linux/pci.h> 1272c7a68dSZhou Wang #include <linux/seq_file.h> 1362c455caSZhou Wang #include <linux/topology.h> 1462c455caSZhou Wang #include "zip.h" 1562c455caSZhou Wang 1662c455caSZhou Wang #define PCI_DEVICE_ID_ZIP_PF 0xa250 1779e09f30SZhou Wang #define PCI_DEVICE_ID_ZIP_VF 0xa251 1862c455caSZhou Wang 1962c455caSZhou Wang #define HZIP_VF_NUM 63 2062c455caSZhou Wang #define HZIP_QUEUE_NUM_V1 4096 2162c455caSZhou Wang #define HZIP_QUEUE_NUM_V2 1024 2262c455caSZhou Wang 2362c455caSZhou Wang #define HZIP_CLOCK_GATE_CTRL 0x301004 2462c455caSZhou Wang #define COMP0_ENABLE BIT(0) 2562c455caSZhou Wang #define COMP1_ENABLE BIT(1) 2662c455caSZhou Wang #define DECOMP0_ENABLE BIT(2) 2762c455caSZhou Wang #define DECOMP1_ENABLE BIT(3) 2862c455caSZhou Wang #define DECOMP2_ENABLE BIT(4) 2962c455caSZhou Wang #define DECOMP3_ENABLE BIT(5) 3062c455caSZhou Wang #define DECOMP4_ENABLE BIT(6) 3162c455caSZhou Wang #define DECOMP5_ENABLE BIT(7) 3262c455caSZhou Wang #define ALL_COMP_DECOMP_EN (COMP0_ENABLE | COMP1_ENABLE | \ 3362c455caSZhou Wang DECOMP0_ENABLE | DECOMP1_ENABLE | \ 3462c455caSZhou Wang DECOMP2_ENABLE | DECOMP3_ENABLE | \ 3562c455caSZhou Wang DECOMP4_ENABLE | DECOMP5_ENABLE) 3662c455caSZhou Wang #define DECOMP_CHECK_ENABLE BIT(16) 3772c7a68dSZhou Wang #define HZIP_FSM_MAX_CNT 0x301008 3862c455caSZhou Wang 3962c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_0 0x301040 4062c455caSZhou Wang #define HZIP_PORT_ARCA_CHE_1 0x301044 4162c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_0 0x301060 4262c455caSZhou Wang #define HZIP_PORT_AWCA_CHE_1 0x301064 4362c455caSZhou Wang #define CACHE_ALL_EN 0xffffffff 4462c455caSZhou Wang 4562c455caSZhou Wang #define HZIP_BD_RUSER_32_63 0x301110 4662c455caSZhou Wang #define HZIP_SGL_RUSER_32_63 0x30111c 4762c455caSZhou Wang #define HZIP_DATA_RUSER_32_63 0x301128 4862c455caSZhou Wang #define HZIP_DATA_WUSER_32_63 0x301134 4962c455caSZhou Wang #define HZIP_BD_WUSER_32_63 0x301140 5062c455caSZhou Wang 5172c7a68dSZhou Wang #define HZIP_QM_IDEL_STATUS 0x3040e4 5262c455caSZhou Wang 5372c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_0 0x302000 5472c7a68dSZhou Wang #define HZIP_CORE_DEBUG_COMP_1 0x303000 5572c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_0 0x304000 5672c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_1 0x305000 5772c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_2 0x306000 5872c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_3 0x307000 5972c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_4 0x308000 6072c7a68dSZhou Wang #define HZIP_CORE_DEBUG_DECOMP_5 0x309000 6162c455caSZhou Wang 6262c455caSZhou Wang #define HZIP_CORE_INT_SOURCE 0x3010A0 6362c455caSZhou Wang #define HZIP_CORE_INT_MASK 0x3010A4 6462c455caSZhou Wang #define HZIP_CORE_INT_STATUS 0x3010AC 6562c455caSZhou Wang #define HZIP_CORE_INT_STATUS_M_ECC BIT(1) 6662c455caSZhou Wang #define HZIP_CORE_SRAM_ECC_ERR_INFO 0x301148 6762c455caSZhou Wang #define SRAM_ECC_ERR_NUM_SHIFT 16 6862c455caSZhou Wang #define SRAM_ECC_ERR_ADDR_SHIFT 24 6962c455caSZhou Wang #define HZIP_CORE_INT_DISABLE 0x000007FF 7072c7a68dSZhou Wang #define HZIP_COMP_CORE_NUM 2 7172c7a68dSZhou Wang #define HZIP_DECOMP_CORE_NUM 6 7272c7a68dSZhou Wang #define HZIP_CORE_NUM (HZIP_COMP_CORE_NUM + \ 7372c7a68dSZhou Wang HZIP_DECOMP_CORE_NUM) 7462c455caSZhou Wang #define HZIP_SQE_SIZE 128 7572c7a68dSZhou Wang #define HZIP_SQ_SIZE (HZIP_SQE_SIZE * QM_Q_DEPTH) 7662c455caSZhou Wang #define HZIP_PF_DEF_Q_NUM 64 7762c455caSZhou Wang #define HZIP_PF_DEF_Q_BASE 0 7862c455caSZhou Wang 7972c7a68dSZhou Wang #define HZIP_SOFT_CTRL_CNT_CLR_CE 0x301000 8072c7a68dSZhou Wang #define SOFT_CTRL_CNT_CLR_CE_BIT BIT(0) 8162c455caSZhou Wang 8262c455caSZhou Wang #define HZIP_NUMA_DISTANCE 100 8372c7a68dSZhou Wang #define HZIP_BUF_SIZE 22 8462c455caSZhou Wang 8562c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip"; 8672c7a68dSZhou Wang static struct dentry *hzip_debugfs_root; 87*719181f3SShukun Tan static LIST_HEAD(hisi_zip_list); 88*719181f3SShukun Tan static DEFINE_MUTEX(hisi_zip_list_lock); 8962c455caSZhou Wang 9062c455caSZhou Wang #ifdef CONFIG_NUMA 9162c455caSZhou Wang static struct hisi_zip *find_zip_device_numa(int node) 9262c455caSZhou Wang { 9362c455caSZhou Wang struct hisi_zip *zip = NULL; 9462c455caSZhou Wang struct hisi_zip *hisi_zip; 9562c455caSZhou Wang int min_distance = HZIP_NUMA_DISTANCE; 9662c455caSZhou Wang struct device *dev; 9762c455caSZhou Wang 9862c455caSZhou Wang list_for_each_entry(hisi_zip, &hisi_zip_list, list) { 9962c455caSZhou Wang dev = &hisi_zip->qm.pdev->dev; 10062c455caSZhou Wang if (node_distance(dev->numa_node, node) < min_distance) { 10162c455caSZhou Wang zip = hisi_zip; 10262c455caSZhou Wang min_distance = node_distance(dev->numa_node, node); 10362c455caSZhou Wang } 10462c455caSZhou Wang } 10562c455caSZhou Wang 10662c455caSZhou Wang return zip; 10762c455caSZhou Wang } 10862c455caSZhou Wang #endif 10962c455caSZhou Wang 11062c455caSZhou Wang struct hisi_zip *find_zip_device(int node) 11162c455caSZhou Wang { 11262c455caSZhou Wang struct hisi_zip *zip = NULL; 11362c455caSZhou Wang 11462c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 11562c455caSZhou Wang #ifdef CONFIG_NUMA 11662c455caSZhou Wang zip = find_zip_device_numa(node); 11762c455caSZhou Wang #else 11862c455caSZhou Wang zip = list_first_entry(&hisi_zip_list, struct hisi_zip, list); 11962c455caSZhou Wang #endif 12062c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 12162c455caSZhou Wang 12262c455caSZhou Wang return zip; 12362c455caSZhou Wang } 12462c455caSZhou Wang 12562c455caSZhou Wang struct hisi_zip_hw_error { 12662c455caSZhou Wang u32 int_msk; 12762c455caSZhou Wang const char *msg; 12862c455caSZhou Wang }; 12962c455caSZhou Wang 13062c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = { 13162c455caSZhou Wang { .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" }, 13262c455caSZhou Wang { .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" }, 13362c455caSZhou Wang { .int_msk = BIT(2), .msg = "zip_axi_rresp_err" }, 13462c455caSZhou Wang { .int_msk = BIT(3), .msg = "zip_axi_bresp_err" }, 13562c455caSZhou Wang { .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" }, 13662c455caSZhou Wang { .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" }, 13762c455caSZhou Wang { .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" }, 13862c455caSZhou Wang { .int_msk = BIT(7), .msg = "zip_pre_in_data_err" }, 13962c455caSZhou Wang { .int_msk = BIT(8), .msg = "zip_com_inf_err" }, 14062c455caSZhou Wang { .int_msk = BIT(9), .msg = "zip_enc_inf_err" }, 14162c455caSZhou Wang { .int_msk = BIT(10), .msg = "zip_pre_out_err" }, 14262c455caSZhou Wang { /* sentinel */ } 14362c455caSZhou Wang }; 14462c455caSZhou Wang 14572c7a68dSZhou Wang enum ctrl_debug_file_index { 14672c7a68dSZhou Wang HZIP_CURRENT_QM, 14772c7a68dSZhou Wang HZIP_CLEAR_ENABLE, 14872c7a68dSZhou Wang HZIP_DEBUG_FILE_NUM, 14972c7a68dSZhou Wang }; 15072c7a68dSZhou Wang 15172c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = { 15272c7a68dSZhou Wang [HZIP_CURRENT_QM] = "current_qm", 15372c7a68dSZhou Wang [HZIP_CLEAR_ENABLE] = "clear_enable", 15472c7a68dSZhou Wang }; 15572c7a68dSZhou Wang 15672c7a68dSZhou Wang struct ctrl_debug_file { 15772c7a68dSZhou Wang enum ctrl_debug_file_index index; 15872c7a68dSZhou Wang spinlock_t lock; 15972c7a68dSZhou Wang struct hisi_zip_ctrl *ctrl; 16072c7a68dSZhou Wang }; 16172c7a68dSZhou Wang 16262c455caSZhou Wang /* 16362c455caSZhou Wang * One ZIP controller has one PF and multiple VFs, some global configurations 16462c455caSZhou Wang * which PF has need this structure. 16562c455caSZhou Wang * 16662c455caSZhou Wang * Just relevant for PF. 16762c455caSZhou Wang */ 16862c455caSZhou Wang struct hisi_zip_ctrl { 16979e09f30SZhou Wang u32 num_vfs; 17062c455caSZhou Wang struct hisi_zip *hisi_zip; 17172c7a68dSZhou Wang struct dentry *debug_root; 17272c7a68dSZhou Wang struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM]; 17372c7a68dSZhou Wang }; 17472c7a68dSZhou Wang 17572c7a68dSZhou Wang enum { 17672c7a68dSZhou Wang HZIP_COMP_CORE0, 17772c7a68dSZhou Wang HZIP_COMP_CORE1, 17872c7a68dSZhou Wang HZIP_DECOMP_CORE0, 17972c7a68dSZhou Wang HZIP_DECOMP_CORE1, 18072c7a68dSZhou Wang HZIP_DECOMP_CORE2, 18172c7a68dSZhou Wang HZIP_DECOMP_CORE3, 18272c7a68dSZhou Wang HZIP_DECOMP_CORE4, 18372c7a68dSZhou Wang HZIP_DECOMP_CORE5, 18472c7a68dSZhou Wang }; 18572c7a68dSZhou Wang 18672c7a68dSZhou Wang static const u64 core_offsets[] = { 18772c7a68dSZhou Wang [HZIP_COMP_CORE0] = 0x302000, 18872c7a68dSZhou Wang [HZIP_COMP_CORE1] = 0x303000, 18972c7a68dSZhou Wang [HZIP_DECOMP_CORE0] = 0x304000, 19072c7a68dSZhou Wang [HZIP_DECOMP_CORE1] = 0x305000, 19172c7a68dSZhou Wang [HZIP_DECOMP_CORE2] = 0x306000, 19272c7a68dSZhou Wang [HZIP_DECOMP_CORE3] = 0x307000, 19372c7a68dSZhou Wang [HZIP_DECOMP_CORE4] = 0x308000, 19472c7a68dSZhou Wang [HZIP_DECOMP_CORE5] = 0x309000, 19572c7a68dSZhou Wang }; 19672c7a68dSZhou Wang 19772c7a68dSZhou Wang static struct debugfs_reg32 hzip_dfx_regs[] = { 19872c7a68dSZhou Wang {"HZIP_GET_BD_NUM ", 0x00ull}, 19972c7a68dSZhou Wang {"HZIP_GET_RIGHT_BD ", 0x04ull}, 20072c7a68dSZhou Wang {"HZIP_GET_ERROR_BD ", 0x08ull}, 20172c7a68dSZhou Wang {"HZIP_DONE_BD_NUM ", 0x0cull}, 20272c7a68dSZhou Wang {"HZIP_WORK_CYCLE ", 0x10ull}, 20372c7a68dSZhou Wang {"HZIP_IDLE_CYCLE ", 0x18ull}, 20472c7a68dSZhou Wang {"HZIP_MAX_DELAY ", 0x20ull}, 20572c7a68dSZhou Wang {"HZIP_MIN_DELAY ", 0x24ull}, 20672c7a68dSZhou Wang {"HZIP_AVG_DELAY ", 0x28ull}, 20772c7a68dSZhou Wang {"HZIP_MEM_VISIBLE_DATA ", 0x30ull}, 20872c7a68dSZhou Wang {"HZIP_MEM_VISIBLE_ADDR ", 0x34ull}, 20972c7a68dSZhou Wang {"HZIP_COMSUMED_BYTE ", 0x38ull}, 21072c7a68dSZhou Wang {"HZIP_PRODUCED_BYTE ", 0x40ull}, 21172c7a68dSZhou Wang {"HZIP_COMP_INF ", 0x70ull}, 21272c7a68dSZhou Wang {"HZIP_PRE_OUT ", 0x78ull}, 21372c7a68dSZhou Wang {"HZIP_BD_RD ", 0x7cull}, 21472c7a68dSZhou Wang {"HZIP_BD_WR ", 0x80ull}, 21572c7a68dSZhou Wang {"HZIP_GET_BD_AXI_ERR_NUM ", 0x84ull}, 21672c7a68dSZhou Wang {"HZIP_GET_BD_PARSE_ERR_NUM ", 0x88ull}, 21772c7a68dSZhou Wang {"HZIP_ADD_BD_AXI_ERR_NUM ", 0x8cull}, 21872c7a68dSZhou Wang {"HZIP_DECOMP_STF_RELOAD_CURR_ST ", 0x94ull}, 21972c7a68dSZhou Wang {"HZIP_DECOMP_LZ77_CURR_ST ", 0x9cull}, 22062c455caSZhou Wang }; 22162c455caSZhou Wang 22262c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp) 22362c455caSZhou Wang { 22462c455caSZhou Wang struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI, 22562c455caSZhou Wang PCI_DEVICE_ID_ZIP_PF, NULL); 22662c455caSZhou Wang u32 n, q_num; 22762c455caSZhou Wang u8 rev_id; 22862c455caSZhou Wang int ret; 22962c455caSZhou Wang 23062c455caSZhou Wang if (!val) 23162c455caSZhou Wang return -EINVAL; 23262c455caSZhou Wang 23362c455caSZhou Wang if (!pdev) { 23462c455caSZhou Wang q_num = min_t(u32, HZIP_QUEUE_NUM_V1, HZIP_QUEUE_NUM_V2); 23562c455caSZhou Wang pr_info("No device found currently, suppose queue number is %d\n", 23662c455caSZhou Wang q_num); 23762c455caSZhou Wang } else { 23862c455caSZhou Wang rev_id = pdev->revision; 23962c455caSZhou Wang switch (rev_id) { 24062c455caSZhou Wang case QM_HW_V1: 24162c455caSZhou Wang q_num = HZIP_QUEUE_NUM_V1; 24262c455caSZhou Wang break; 24362c455caSZhou Wang case QM_HW_V2: 24462c455caSZhou Wang q_num = HZIP_QUEUE_NUM_V2; 24562c455caSZhou Wang break; 24662c455caSZhou Wang default: 24762c455caSZhou Wang return -EINVAL; 24862c455caSZhou Wang } 24962c455caSZhou Wang } 25062c455caSZhou Wang 25162c455caSZhou Wang ret = kstrtou32(val, 10, &n); 25262c455caSZhou Wang if (ret != 0 || n > q_num || n == 0) 25362c455caSZhou Wang return -EINVAL; 25462c455caSZhou Wang 25562c455caSZhou Wang return param_set_int(val, kp); 25662c455caSZhou Wang } 25762c455caSZhou Wang 25862c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = { 25962c455caSZhou Wang .set = pf_q_num_set, 26062c455caSZhou Wang .get = param_get_int, 26162c455caSZhou Wang }; 26262c455caSZhou Wang 26362c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM; 26462c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444); 26562c455caSZhou Wang MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 1-4096, v2 1-1024)"); 26662c455caSZhou Wang 26762c455caSZhou Wang static int uacce_mode; 26862c455caSZhou Wang module_param(uacce_mode, int, 0); 26962c455caSZhou Wang 27062c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = { 27162c455caSZhou Wang { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) }, 27279e09f30SZhou Wang { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) }, 27362c455caSZhou Wang { 0, } 27462c455caSZhou Wang }; 27562c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids); 27662c455caSZhou Wang 27762c455caSZhou Wang static inline void hisi_zip_add_to_list(struct hisi_zip *hisi_zip) 27862c455caSZhou Wang { 27962c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 28062c455caSZhou Wang list_add_tail(&hisi_zip->list, &hisi_zip_list); 28162c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 28262c455caSZhou Wang } 28362c455caSZhou Wang 28462c455caSZhou Wang static inline void hisi_zip_remove_from_list(struct hisi_zip *hisi_zip) 28562c455caSZhou Wang { 28662c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 28762c455caSZhou Wang list_del(&hisi_zip->list); 28862c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 28962c455caSZhou Wang } 29062c455caSZhou Wang 29162c455caSZhou Wang static void hisi_zip_set_user_domain_and_cache(struct hisi_zip *hisi_zip) 29262c455caSZhou Wang { 29362c455caSZhou Wang void __iomem *base = hisi_zip->qm.io_base; 29462c455caSZhou Wang 29562c455caSZhou Wang /* qm user domain */ 29662c455caSZhou Wang writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1); 29762c455caSZhou Wang writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE); 29862c455caSZhou Wang writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1); 29962c455caSZhou Wang writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE); 30062c455caSZhou Wang writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE); 30162c455caSZhou Wang 30262c455caSZhou Wang /* qm cache */ 30362c455caSZhou Wang writel(AXI_M_CFG, base + QM_AXI_M_CFG); 30462c455caSZhou Wang writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE); 30562c455caSZhou Wang /* disable FLR triggered by BME(bus master enable) */ 30662c455caSZhou Wang writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG); 30762c455caSZhou Wang writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE); 30862c455caSZhou Wang 30962c455caSZhou Wang /* cache */ 31062c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0); 31162c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1); 31262c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0); 31362c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1); 31462c455caSZhou Wang 31562c455caSZhou Wang /* user domain configurations */ 31662c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63); 31762c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63); 31862c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63); 31962c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63); 32062c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63); 32162c455caSZhou Wang 32262c455caSZhou Wang /* let's open all compression/decompression cores */ 32362c455caSZhou Wang writel(DECOMP_CHECK_ENABLE | ALL_COMP_DECOMP_EN, 32462c455caSZhou Wang base + HZIP_CLOCK_GATE_CTRL); 32562c455caSZhou Wang 32662c455caSZhou Wang /* enable sqc writeback */ 32762c455caSZhou Wang writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE | 32862c455caSZhou Wang CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) | 32962c455caSZhou Wang FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL); 33062c455caSZhou Wang } 33162c455caSZhou Wang 33262c455caSZhou Wang static void hisi_zip_hw_error_set_state(struct hisi_zip *hisi_zip, bool state) 33362c455caSZhou Wang { 33462c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 33562c455caSZhou Wang 33662c455caSZhou Wang if (qm->ver == QM_HW_V1) { 33762c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, qm->io_base + HZIP_CORE_INT_MASK); 338ee1788c6SZhou Wang dev_info(&qm->pdev->dev, "Does not support hw error handle\n"); 33962c455caSZhou Wang return; 34062c455caSZhou Wang } 34162c455caSZhou Wang 34262c455caSZhou Wang if (state) { 34362c455caSZhou Wang /* clear ZIP hw error source if having */ 34462c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, hisi_zip->qm.io_base + 34562c455caSZhou Wang HZIP_CORE_INT_SOURCE); 34662c455caSZhou Wang /* enable ZIP hw error interrupts */ 34762c455caSZhou Wang writel(0, hisi_zip->qm.io_base + HZIP_CORE_INT_MASK); 34862c455caSZhou Wang } else { 34962c455caSZhou Wang /* disable ZIP hw error interrupts */ 35062c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, 35162c455caSZhou Wang hisi_zip->qm.io_base + HZIP_CORE_INT_MASK); 35262c455caSZhou Wang } 35362c455caSZhou Wang } 35462c455caSZhou Wang 35572c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file) 35672c7a68dSZhou Wang { 35772c7a68dSZhou Wang struct hisi_zip *hisi_zip = file->ctrl->hisi_zip; 35872c7a68dSZhou Wang 35972c7a68dSZhou Wang return &hisi_zip->qm; 36072c7a68dSZhou Wang } 36172c7a68dSZhou Wang 36272c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file) 36372c7a68dSZhou Wang { 36472c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 36572c7a68dSZhou Wang 36672c7a68dSZhou Wang return readl(qm->io_base + QM_DFX_MB_CNT_VF); 36772c7a68dSZhou Wang } 36872c7a68dSZhou Wang 36972c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val) 37072c7a68dSZhou Wang { 37172c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 37272c7a68dSZhou Wang struct hisi_zip_ctrl *ctrl = file->ctrl; 37372c7a68dSZhou Wang u32 vfq_num; 37472c7a68dSZhou Wang u32 tmp; 37572c7a68dSZhou Wang 37672c7a68dSZhou Wang if (val > ctrl->num_vfs) 37772c7a68dSZhou Wang return -EINVAL; 37872c7a68dSZhou Wang 37972c7a68dSZhou Wang /* Calculate curr_qm_qp_num and store */ 38072c7a68dSZhou Wang if (val == 0) { 38172c7a68dSZhou Wang qm->debug.curr_qm_qp_num = qm->qp_num; 38272c7a68dSZhou Wang } else { 38372c7a68dSZhou Wang vfq_num = (qm->ctrl_qp_num - qm->qp_num) / ctrl->num_vfs; 38472c7a68dSZhou Wang if (val == ctrl->num_vfs) 38572c7a68dSZhou Wang qm->debug.curr_qm_qp_num = qm->ctrl_qp_num - 38672c7a68dSZhou Wang qm->qp_num - (ctrl->num_vfs - 1) * vfq_num; 38772c7a68dSZhou Wang else 38872c7a68dSZhou Wang qm->debug.curr_qm_qp_num = vfq_num; 38972c7a68dSZhou Wang } 39072c7a68dSZhou Wang 39172c7a68dSZhou Wang writel(val, qm->io_base + QM_DFX_MB_CNT_VF); 39272c7a68dSZhou Wang writel(val, qm->io_base + QM_DFX_DB_CNT_VF); 39372c7a68dSZhou Wang 39472c7a68dSZhou Wang tmp = val | 39572c7a68dSZhou Wang (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK); 39672c7a68dSZhou Wang writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 39772c7a68dSZhou Wang 39872c7a68dSZhou Wang tmp = val | 39972c7a68dSZhou Wang (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK); 40072c7a68dSZhou Wang writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 40172c7a68dSZhou Wang 40272c7a68dSZhou Wang return 0; 40372c7a68dSZhou Wang } 40472c7a68dSZhou Wang 40572c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file) 40672c7a68dSZhou Wang { 40772c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 40872c7a68dSZhou Wang 40972c7a68dSZhou Wang return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 41072c7a68dSZhou Wang SOFT_CTRL_CNT_CLR_CE_BIT; 41172c7a68dSZhou Wang } 41272c7a68dSZhou Wang 41372c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val) 41472c7a68dSZhou Wang { 41572c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 41672c7a68dSZhou Wang u32 tmp; 41772c7a68dSZhou Wang 41872c7a68dSZhou Wang if (val != 1 && val != 0) 41972c7a68dSZhou Wang return -EINVAL; 42072c7a68dSZhou Wang 42172c7a68dSZhou Wang tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 42272c7a68dSZhou Wang ~SOFT_CTRL_CNT_CLR_CE_BIT) | val; 42372c7a68dSZhou Wang writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 42472c7a68dSZhou Wang 42572c7a68dSZhou Wang return 0; 42672c7a68dSZhou Wang } 42772c7a68dSZhou Wang 42872c7a68dSZhou Wang static ssize_t ctrl_debug_read(struct file *filp, char __user *buf, 42972c7a68dSZhou Wang size_t count, loff_t *pos) 43072c7a68dSZhou Wang { 43172c7a68dSZhou Wang struct ctrl_debug_file *file = filp->private_data; 43272c7a68dSZhou Wang char tbuf[HZIP_BUF_SIZE]; 43372c7a68dSZhou Wang u32 val; 43472c7a68dSZhou Wang int ret; 43572c7a68dSZhou Wang 43672c7a68dSZhou Wang spin_lock_irq(&file->lock); 43772c7a68dSZhou Wang switch (file->index) { 43872c7a68dSZhou Wang case HZIP_CURRENT_QM: 43972c7a68dSZhou Wang val = current_qm_read(file); 44072c7a68dSZhou Wang break; 44172c7a68dSZhou Wang case HZIP_CLEAR_ENABLE: 44272c7a68dSZhou Wang val = clear_enable_read(file); 44372c7a68dSZhou Wang break; 44472c7a68dSZhou Wang default: 44572c7a68dSZhou Wang spin_unlock_irq(&file->lock); 44672c7a68dSZhou Wang return -EINVAL; 44772c7a68dSZhou Wang } 44872c7a68dSZhou Wang spin_unlock_irq(&file->lock); 44972c7a68dSZhou Wang ret = sprintf(tbuf, "%u\n", val); 45072c7a68dSZhou Wang return simple_read_from_buffer(buf, count, pos, tbuf, ret); 45172c7a68dSZhou Wang } 45272c7a68dSZhou Wang 45372c7a68dSZhou Wang static ssize_t ctrl_debug_write(struct file *filp, const char __user *buf, 45472c7a68dSZhou Wang size_t count, loff_t *pos) 45572c7a68dSZhou Wang { 45672c7a68dSZhou Wang struct ctrl_debug_file *file = filp->private_data; 45772c7a68dSZhou Wang char tbuf[HZIP_BUF_SIZE]; 45872c7a68dSZhou Wang unsigned long val; 45972c7a68dSZhou Wang int len, ret; 46072c7a68dSZhou Wang 46172c7a68dSZhou Wang if (*pos != 0) 46272c7a68dSZhou Wang return 0; 46372c7a68dSZhou Wang 46472c7a68dSZhou Wang if (count >= HZIP_BUF_SIZE) 46572c7a68dSZhou Wang return -ENOSPC; 46672c7a68dSZhou Wang 46772c7a68dSZhou Wang len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count); 46872c7a68dSZhou Wang if (len < 0) 46972c7a68dSZhou Wang return len; 47072c7a68dSZhou Wang 47172c7a68dSZhou Wang tbuf[len] = '\0'; 47272c7a68dSZhou Wang if (kstrtoul(tbuf, 0, &val)) 47372c7a68dSZhou Wang return -EFAULT; 47472c7a68dSZhou Wang 47572c7a68dSZhou Wang spin_lock_irq(&file->lock); 47672c7a68dSZhou Wang switch (file->index) { 47772c7a68dSZhou Wang case HZIP_CURRENT_QM: 47872c7a68dSZhou Wang ret = current_qm_write(file, val); 47972c7a68dSZhou Wang if (ret) 48072c7a68dSZhou Wang goto err_input; 48172c7a68dSZhou Wang break; 48272c7a68dSZhou Wang case HZIP_CLEAR_ENABLE: 48372c7a68dSZhou Wang ret = clear_enable_write(file, val); 48472c7a68dSZhou Wang if (ret) 48572c7a68dSZhou Wang goto err_input; 48672c7a68dSZhou Wang break; 48772c7a68dSZhou Wang default: 48872c7a68dSZhou Wang ret = -EINVAL; 48972c7a68dSZhou Wang goto err_input; 49072c7a68dSZhou Wang } 49172c7a68dSZhou Wang spin_unlock_irq(&file->lock); 49272c7a68dSZhou Wang 49372c7a68dSZhou Wang return count; 49472c7a68dSZhou Wang 49572c7a68dSZhou Wang err_input: 49672c7a68dSZhou Wang spin_unlock_irq(&file->lock); 49772c7a68dSZhou Wang return ret; 49872c7a68dSZhou Wang } 49972c7a68dSZhou Wang 50072c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = { 50172c7a68dSZhou Wang .owner = THIS_MODULE, 50272c7a68dSZhou Wang .open = simple_open, 50372c7a68dSZhou Wang .read = ctrl_debug_read, 50472c7a68dSZhou Wang .write = ctrl_debug_write, 50572c7a68dSZhou Wang }; 50672c7a68dSZhou Wang 50772c7a68dSZhou Wang static int hisi_zip_core_debug_init(struct hisi_zip_ctrl *ctrl) 50872c7a68dSZhou Wang { 50972c7a68dSZhou Wang struct hisi_zip *hisi_zip = ctrl->hisi_zip; 51072c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 51172c7a68dSZhou Wang struct device *dev = &qm->pdev->dev; 51272c7a68dSZhou Wang struct debugfs_regset32 *regset; 51372c7a68dSZhou Wang struct dentry *tmp_d, *tmp; 51472c7a68dSZhou Wang char buf[HZIP_BUF_SIZE]; 51572c7a68dSZhou Wang int i; 51672c7a68dSZhou Wang 51772c7a68dSZhou Wang for (i = 0; i < HZIP_CORE_NUM; i++) { 51872c7a68dSZhou Wang if (i < HZIP_COMP_CORE_NUM) 51972c7a68dSZhou Wang sprintf(buf, "comp_core%d", i); 52072c7a68dSZhou Wang else 52172c7a68dSZhou Wang sprintf(buf, "decomp_core%d", i - HZIP_COMP_CORE_NUM); 52272c7a68dSZhou Wang 52372c7a68dSZhou Wang tmp_d = debugfs_create_dir(buf, ctrl->debug_root); 52472c7a68dSZhou Wang if (!tmp_d) 52572c7a68dSZhou Wang return -ENOENT; 52672c7a68dSZhou Wang 52772c7a68dSZhou Wang regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL); 52872c7a68dSZhou Wang if (!regset) 52972c7a68dSZhou Wang return -ENOENT; 53072c7a68dSZhou Wang 53172c7a68dSZhou Wang regset->regs = hzip_dfx_regs; 53272c7a68dSZhou Wang regset->nregs = ARRAY_SIZE(hzip_dfx_regs); 53372c7a68dSZhou Wang regset->base = qm->io_base + core_offsets[i]; 53472c7a68dSZhou Wang 53572c7a68dSZhou Wang tmp = debugfs_create_regset32("regs", 0444, tmp_d, regset); 53672c7a68dSZhou Wang if (!tmp) 53772c7a68dSZhou Wang return -ENOENT; 53872c7a68dSZhou Wang } 53972c7a68dSZhou Wang 54072c7a68dSZhou Wang return 0; 54172c7a68dSZhou Wang } 54272c7a68dSZhou Wang 54372c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl) 54472c7a68dSZhou Wang { 54572c7a68dSZhou Wang struct dentry *tmp; 54672c7a68dSZhou Wang int i; 54772c7a68dSZhou Wang 54872c7a68dSZhou Wang for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) { 54972c7a68dSZhou Wang spin_lock_init(&ctrl->files[i].lock); 55072c7a68dSZhou Wang ctrl->files[i].ctrl = ctrl; 55172c7a68dSZhou Wang ctrl->files[i].index = i; 55272c7a68dSZhou Wang 55372c7a68dSZhou Wang tmp = debugfs_create_file(ctrl_debug_file_name[i], 0600, 55472c7a68dSZhou Wang ctrl->debug_root, ctrl->files + i, 55572c7a68dSZhou Wang &ctrl_debug_fops); 55672c7a68dSZhou Wang if (!tmp) 55772c7a68dSZhou Wang return -ENOENT; 55872c7a68dSZhou Wang } 55972c7a68dSZhou Wang 56072c7a68dSZhou Wang return hisi_zip_core_debug_init(ctrl); 56172c7a68dSZhou Wang } 56272c7a68dSZhou Wang 56372c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip) 56472c7a68dSZhou Wang { 56572c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 56672c7a68dSZhou Wang struct device *dev = &qm->pdev->dev; 56772c7a68dSZhou Wang struct dentry *dev_d; 56872c7a68dSZhou Wang int ret; 56972c7a68dSZhou Wang 57072c7a68dSZhou Wang dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root); 57172c7a68dSZhou Wang if (!dev_d) 57272c7a68dSZhou Wang return -ENOENT; 57372c7a68dSZhou Wang 57472c7a68dSZhou Wang qm->debug.debug_root = dev_d; 57572c7a68dSZhou Wang ret = hisi_qm_debug_init(qm); 57672c7a68dSZhou Wang if (ret) 57772c7a68dSZhou Wang goto failed_to_create; 57872c7a68dSZhou Wang 57972c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) { 58072c7a68dSZhou Wang hisi_zip->ctrl->debug_root = dev_d; 58172c7a68dSZhou Wang ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl); 58272c7a68dSZhou Wang if (ret) 58372c7a68dSZhou Wang goto failed_to_create; 58472c7a68dSZhou Wang } 58572c7a68dSZhou Wang 58672c7a68dSZhou Wang return 0; 58772c7a68dSZhou Wang 58872c7a68dSZhou Wang failed_to_create: 58972c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 59072c7a68dSZhou Wang return ret; 59172c7a68dSZhou Wang } 59272c7a68dSZhou Wang 59372c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip) 59472c7a68dSZhou Wang { 59572c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 59672c7a68dSZhou Wang 59772c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF); 59872c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF); 59972c7a68dSZhou Wang writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 60072c7a68dSZhou Wang 60172c7a68dSZhou Wang hisi_qm_debug_regs_clear(qm); 60272c7a68dSZhou Wang } 60372c7a68dSZhou Wang 60472c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip) 60572c7a68dSZhou Wang { 60672c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 60772c7a68dSZhou Wang 60872c7a68dSZhou Wang debugfs_remove_recursive(qm->debug.debug_root); 60972c7a68dSZhou Wang 61072c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) 61172c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 61272c7a68dSZhou Wang } 61372c7a68dSZhou Wang 61462c455caSZhou Wang static void hisi_zip_hw_error_init(struct hisi_zip *hisi_zip) 61562c455caSZhou Wang { 61662c455caSZhou Wang hisi_qm_hw_error_init(&hisi_zip->qm, QM_BASE_CE, 61762c455caSZhou Wang QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT, 0, 61862c455caSZhou Wang QM_DB_RANDOM_INVALID); 61962c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, true); 62062c455caSZhou Wang } 62162c455caSZhou Wang 62262c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip) 62362c455caSZhou Wang { 62462c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 62562c455caSZhou Wang struct hisi_zip_ctrl *ctrl; 62662c455caSZhou Wang 62762c455caSZhou Wang ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL); 62862c455caSZhou Wang if (!ctrl) 62962c455caSZhou Wang return -ENOMEM; 63062c455caSZhou Wang 63162c455caSZhou Wang hisi_zip->ctrl = ctrl; 63262c455caSZhou Wang ctrl->hisi_zip = hisi_zip; 63362c455caSZhou Wang 63462c455caSZhou Wang switch (qm->ver) { 63562c455caSZhou Wang case QM_HW_V1: 63662c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1; 63762c455caSZhou Wang break; 63862c455caSZhou Wang 63962c455caSZhou Wang case QM_HW_V2: 64062c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2; 64162c455caSZhou Wang break; 64262c455caSZhou Wang 64362c455caSZhou Wang default: 64462c455caSZhou Wang return -EINVAL; 64562c455caSZhou Wang } 64662c455caSZhou Wang 64762c455caSZhou Wang hisi_zip_set_user_domain_and_cache(hisi_zip); 64862c455caSZhou Wang hisi_zip_hw_error_init(hisi_zip); 64972c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 65062c455caSZhou Wang 65162c455caSZhou Wang return 0; 65262c455caSZhou Wang } 65362c455caSZhou Wang 65462c455caSZhou Wang static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id) 65562c455caSZhou Wang { 65662c455caSZhou Wang struct hisi_zip *hisi_zip; 65762c455caSZhou Wang enum qm_hw_ver rev_id; 65862c455caSZhou Wang struct hisi_qm *qm; 65962c455caSZhou Wang int ret; 66062c455caSZhou Wang 66162c455caSZhou Wang rev_id = hisi_qm_get_hw_version(pdev); 66262c455caSZhou Wang if (rev_id == QM_HW_UNKNOWN) 66362c455caSZhou Wang return -EINVAL; 66462c455caSZhou Wang 66562c455caSZhou Wang hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL); 66662c455caSZhou Wang if (!hisi_zip) 66762c455caSZhou Wang return -ENOMEM; 66862c455caSZhou Wang pci_set_drvdata(pdev, hisi_zip); 66962c455caSZhou Wang 67062c455caSZhou Wang qm = &hisi_zip->qm; 67162c455caSZhou Wang qm->pdev = pdev; 67262c455caSZhou Wang qm->ver = rev_id; 67362c455caSZhou Wang 67462c455caSZhou Wang qm->sqe_size = HZIP_SQE_SIZE; 67562c455caSZhou Wang qm->dev_name = hisi_zip_name; 67679e09f30SZhou Wang qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? QM_HW_PF : 67779e09f30SZhou Wang QM_HW_VF; 67862c455caSZhou Wang switch (uacce_mode) { 67962c455caSZhou Wang case 0: 68062c455caSZhou Wang qm->use_dma_api = true; 68162c455caSZhou Wang break; 68262c455caSZhou Wang case 1: 68362c455caSZhou Wang qm->use_dma_api = false; 68462c455caSZhou Wang break; 68562c455caSZhou Wang case 2: 68662c455caSZhou Wang qm->use_dma_api = true; 68762c455caSZhou Wang break; 68862c455caSZhou Wang default: 68962c455caSZhou Wang return -EINVAL; 69062c455caSZhou Wang } 69162c455caSZhou Wang 69262c455caSZhou Wang ret = hisi_qm_init(qm); 69362c455caSZhou Wang if (ret) { 69462c455caSZhou Wang dev_err(&pdev->dev, "Failed to init qm!\n"); 69562c455caSZhou Wang return ret; 69662c455caSZhou Wang } 69762c455caSZhou Wang 69879e09f30SZhou Wang if (qm->fun_type == QM_HW_PF) { 69962c455caSZhou Wang ret = hisi_zip_pf_probe_init(hisi_zip); 70062c455caSZhou Wang if (ret) 70179e09f30SZhou Wang return ret; 70262c455caSZhou Wang 70362c455caSZhou Wang qm->qp_base = HZIP_PF_DEF_Q_BASE; 70462c455caSZhou Wang qm->qp_num = pf_q_num; 70579e09f30SZhou Wang } else if (qm->fun_type == QM_HW_VF) { 70679e09f30SZhou Wang /* 70779e09f30SZhou Wang * have no way to get qm configure in VM in v1 hardware, 70879e09f30SZhou Wang * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force 70979e09f30SZhou Wang * to trigger only one VF in v1 hardware. 71079e09f30SZhou Wang * 71179e09f30SZhou Wang * v2 hardware has no such problem. 71279e09f30SZhou Wang */ 71379e09f30SZhou Wang if (qm->ver == QM_HW_V1) { 71479e09f30SZhou Wang qm->qp_base = HZIP_PF_DEF_Q_NUM; 71579e09f30SZhou Wang qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM; 71679e09f30SZhou Wang } else if (qm->ver == QM_HW_V2) 71779e09f30SZhou Wang /* v2 starts to support get vft by mailbox */ 71879e09f30SZhou Wang hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num); 71979e09f30SZhou Wang } 72062c455caSZhou Wang 72162c455caSZhou Wang ret = hisi_qm_start(qm); 72262c455caSZhou Wang if (ret) 72362c455caSZhou Wang goto err_qm_uninit; 72462c455caSZhou Wang 72572c7a68dSZhou Wang ret = hisi_zip_debugfs_init(hisi_zip); 72672c7a68dSZhou Wang if (ret) 72772c7a68dSZhou Wang dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret); 72872c7a68dSZhou Wang 72962c455caSZhou Wang hisi_zip_add_to_list(hisi_zip); 73062c455caSZhou Wang 73162c455caSZhou Wang return 0; 73262c455caSZhou Wang 73362c455caSZhou Wang err_qm_uninit: 73462c455caSZhou Wang hisi_qm_uninit(qm); 73562c455caSZhou Wang return ret; 73662c455caSZhou Wang } 73762c455caSZhou Wang 73879e09f30SZhou Wang /* Currently we only support equal assignment */ 73979e09f30SZhou Wang static int hisi_zip_vf_q_assign(struct hisi_zip *hisi_zip, int num_vfs) 74079e09f30SZhou Wang { 74179e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 74279e09f30SZhou Wang u32 qp_num = qm->qp_num; 74379e09f30SZhou Wang u32 q_base = qp_num; 74479e09f30SZhou Wang u32 q_num, remain_q_num, i; 74579e09f30SZhou Wang int ret; 74679e09f30SZhou Wang 74779e09f30SZhou Wang if (!num_vfs) 74879e09f30SZhou Wang return -EINVAL; 74979e09f30SZhou Wang 75079e09f30SZhou Wang remain_q_num = qm->ctrl_qp_num - qp_num; 75179e09f30SZhou Wang if (remain_q_num < num_vfs) 75279e09f30SZhou Wang return -EINVAL; 75379e09f30SZhou Wang 75479e09f30SZhou Wang q_num = remain_q_num / num_vfs; 75579e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 75679e09f30SZhou Wang if (i == num_vfs) 75779e09f30SZhou Wang q_num += remain_q_num % num_vfs; 75879e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, q_base, q_num); 75979e09f30SZhou Wang if (ret) 76079e09f30SZhou Wang return ret; 76179e09f30SZhou Wang q_base += q_num; 76279e09f30SZhou Wang } 76379e09f30SZhou Wang 76479e09f30SZhou Wang return 0; 76579e09f30SZhou Wang } 76679e09f30SZhou Wang 76779e09f30SZhou Wang static int hisi_zip_clear_vft_config(struct hisi_zip *hisi_zip) 76879e09f30SZhou Wang { 76979e09f30SZhou Wang struct hisi_zip_ctrl *ctrl = hisi_zip->ctrl; 77079e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 77179e09f30SZhou Wang u32 i, num_vfs = ctrl->num_vfs; 77279e09f30SZhou Wang int ret; 77379e09f30SZhou Wang 77479e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 77579e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, 0, 0); 77679e09f30SZhou Wang if (ret) 77779e09f30SZhou Wang return ret; 77879e09f30SZhou Wang } 77979e09f30SZhou Wang 78079e09f30SZhou Wang ctrl->num_vfs = 0; 78179e09f30SZhou Wang 78279e09f30SZhou Wang return 0; 78379e09f30SZhou Wang } 78479e09f30SZhou Wang 78579e09f30SZhou Wang static int hisi_zip_sriov_enable(struct pci_dev *pdev, int max_vfs) 78679e09f30SZhou Wang { 78779e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 78879e09f30SZhou Wang int pre_existing_vfs, num_vfs, ret; 78979e09f30SZhou Wang 79079e09f30SZhou Wang pre_existing_vfs = pci_num_vf(pdev); 79179e09f30SZhou Wang 79279e09f30SZhou Wang if (pre_existing_vfs) { 79379e09f30SZhou Wang dev_err(&pdev->dev, 79479e09f30SZhou Wang "Can't enable VF. Please disable pre-enabled VFs!\n"); 79579e09f30SZhou Wang return 0; 79679e09f30SZhou Wang } 79779e09f30SZhou Wang 79879e09f30SZhou Wang num_vfs = min_t(int, max_vfs, HZIP_VF_NUM); 79979e09f30SZhou Wang 80079e09f30SZhou Wang ret = hisi_zip_vf_q_assign(hisi_zip, num_vfs); 80179e09f30SZhou Wang if (ret) { 80279e09f30SZhou Wang dev_err(&pdev->dev, "Can't assign queues for VF!\n"); 80379e09f30SZhou Wang return ret; 80479e09f30SZhou Wang } 80579e09f30SZhou Wang 80679e09f30SZhou Wang hisi_zip->ctrl->num_vfs = num_vfs; 80779e09f30SZhou Wang 80879e09f30SZhou Wang ret = pci_enable_sriov(pdev, num_vfs); 80979e09f30SZhou Wang if (ret) { 81079e09f30SZhou Wang dev_err(&pdev->dev, "Can't enable VF!\n"); 81179e09f30SZhou Wang hisi_zip_clear_vft_config(hisi_zip); 81279e09f30SZhou Wang return ret; 81379e09f30SZhou Wang } 81479e09f30SZhou Wang 81579e09f30SZhou Wang return num_vfs; 81679e09f30SZhou Wang } 81779e09f30SZhou Wang 81879e09f30SZhou Wang static int hisi_zip_sriov_disable(struct pci_dev *pdev) 81979e09f30SZhou Wang { 82079e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 82179e09f30SZhou Wang 82279e09f30SZhou Wang if (pci_vfs_assigned(pdev)) { 82379e09f30SZhou Wang dev_err(&pdev->dev, 82479e09f30SZhou Wang "Can't disable VFs while VFs are assigned!\n"); 82579e09f30SZhou Wang return -EPERM; 82679e09f30SZhou Wang } 82779e09f30SZhou Wang 82879e09f30SZhou Wang /* remove in hisi_zip_pci_driver will be called to free VF resources */ 82979e09f30SZhou Wang pci_disable_sriov(pdev); 83079e09f30SZhou Wang 83179e09f30SZhou Wang return hisi_zip_clear_vft_config(hisi_zip); 83279e09f30SZhou Wang } 83379e09f30SZhou Wang 83479e09f30SZhou Wang static int hisi_zip_sriov_configure(struct pci_dev *pdev, int num_vfs) 83579e09f30SZhou Wang { 83679e09f30SZhou Wang if (num_vfs == 0) 83779e09f30SZhou Wang return hisi_zip_sriov_disable(pdev); 83879e09f30SZhou Wang else 83979e09f30SZhou Wang return hisi_zip_sriov_enable(pdev, num_vfs); 84079e09f30SZhou Wang } 84179e09f30SZhou Wang 84262c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev) 84362c455caSZhou Wang { 84462c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 84562c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 84662c455caSZhou Wang 84779e09f30SZhou Wang if (qm->fun_type == QM_HW_PF && hisi_zip->ctrl->num_vfs != 0) 84879e09f30SZhou Wang hisi_zip_sriov_disable(pdev); 84979e09f30SZhou Wang 85072c7a68dSZhou Wang hisi_zip_debugfs_exit(hisi_zip); 85162c455caSZhou Wang hisi_qm_stop(qm); 85279e09f30SZhou Wang 85379e09f30SZhou Wang if (qm->fun_type == QM_HW_PF) 85462c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, false); 85579e09f30SZhou Wang 85662c455caSZhou Wang hisi_qm_uninit(qm); 85762c455caSZhou Wang hisi_zip_remove_from_list(hisi_zip); 85862c455caSZhou Wang } 85962c455caSZhou Wang 86062c455caSZhou Wang static void hisi_zip_log_hw_error(struct hisi_zip *hisi_zip, u32 err_sts) 86162c455caSZhou Wang { 86262c455caSZhou Wang const struct hisi_zip_hw_error *err = zip_hw_error; 86362c455caSZhou Wang struct device *dev = &hisi_zip->qm.pdev->dev; 86462c455caSZhou Wang u32 err_val; 86562c455caSZhou Wang 86662c455caSZhou Wang while (err->msg) { 86762c455caSZhou Wang if (err->int_msk & err_sts) { 86862c455caSZhou Wang dev_warn(dev, "%s [error status=0x%x] found\n", 86962c455caSZhou Wang err->msg, err->int_msk); 87062c455caSZhou Wang 87162c455caSZhou Wang if (HZIP_CORE_INT_STATUS_M_ECC & err->int_msk) { 87262c455caSZhou Wang err_val = readl(hisi_zip->qm.io_base + 87362c455caSZhou Wang HZIP_CORE_SRAM_ECC_ERR_INFO); 87462c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram num=0x%x\n", 87562c455caSZhou Wang ((err_val >> SRAM_ECC_ERR_NUM_SHIFT) & 87662c455caSZhou Wang 0xFF)); 87762c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram addr=0x%x\n", 87862c455caSZhou Wang (err_val >> SRAM_ECC_ERR_ADDR_SHIFT)); 87962c455caSZhou Wang } 88062c455caSZhou Wang } 88162c455caSZhou Wang err++; 88262c455caSZhou Wang } 88362c455caSZhou Wang } 88462c455caSZhou Wang 88562c455caSZhou Wang static pci_ers_result_t hisi_zip_hw_error_handle(struct hisi_zip *hisi_zip) 88662c455caSZhou Wang { 88762c455caSZhou Wang u32 err_sts; 88862c455caSZhou Wang 88962c455caSZhou Wang /* read err sts */ 89062c455caSZhou Wang err_sts = readl(hisi_zip->qm.io_base + HZIP_CORE_INT_STATUS); 89162c455caSZhou Wang 89262c455caSZhou Wang if (err_sts) { 89362c455caSZhou Wang hisi_zip_log_hw_error(hisi_zip, err_sts); 89462c455caSZhou Wang /* clear error interrupts */ 89562c455caSZhou Wang writel(err_sts, hisi_zip->qm.io_base + HZIP_CORE_INT_SOURCE); 89662c455caSZhou Wang 89762c455caSZhou Wang return PCI_ERS_RESULT_NEED_RESET; 89862c455caSZhou Wang } 89962c455caSZhou Wang 90062c455caSZhou Wang return PCI_ERS_RESULT_RECOVERED; 90162c455caSZhou Wang } 90262c455caSZhou Wang 90362c455caSZhou Wang static pci_ers_result_t hisi_zip_process_hw_error(struct pci_dev *pdev) 90462c455caSZhou Wang { 90562c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 90662c455caSZhou Wang struct device *dev = &pdev->dev; 90762c455caSZhou Wang pci_ers_result_t qm_ret, zip_ret; 90862c455caSZhou Wang 90962c455caSZhou Wang if (!hisi_zip) { 91062c455caSZhou Wang dev_err(dev, 91162c455caSZhou Wang "Can't recover ZIP-error occurred during device init\n"); 91262c455caSZhou Wang return PCI_ERS_RESULT_NONE; 91362c455caSZhou Wang } 91462c455caSZhou Wang 91562c455caSZhou Wang qm_ret = hisi_qm_hw_error_handle(&hisi_zip->qm); 91662c455caSZhou Wang 91762c455caSZhou Wang zip_ret = hisi_zip_hw_error_handle(hisi_zip); 91862c455caSZhou Wang 91962c455caSZhou Wang return (qm_ret == PCI_ERS_RESULT_NEED_RESET || 92062c455caSZhou Wang zip_ret == PCI_ERS_RESULT_NEED_RESET) ? 92162c455caSZhou Wang PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED; 92262c455caSZhou Wang } 92362c455caSZhou Wang 92462c455caSZhou Wang static pci_ers_result_t hisi_zip_error_detected(struct pci_dev *pdev, 92562c455caSZhou Wang pci_channel_state_t state) 92662c455caSZhou Wang { 92762c455caSZhou Wang if (pdev->is_virtfn) 92862c455caSZhou Wang return PCI_ERS_RESULT_NONE; 92962c455caSZhou Wang 93062c455caSZhou Wang dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state); 93162c455caSZhou Wang if (state == pci_channel_io_perm_failure) 93262c455caSZhou Wang return PCI_ERS_RESULT_DISCONNECT; 93362c455caSZhou Wang 93462c455caSZhou Wang return hisi_zip_process_hw_error(pdev); 93562c455caSZhou Wang } 93662c455caSZhou Wang 93762c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = { 93862c455caSZhou Wang .error_detected = hisi_zip_error_detected, 93962c455caSZhou Wang }; 94062c455caSZhou Wang 94162c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = { 94262c455caSZhou Wang .name = "hisi_zip", 94362c455caSZhou Wang .id_table = hisi_zip_dev_ids, 94462c455caSZhou Wang .probe = hisi_zip_probe, 94562c455caSZhou Wang .remove = hisi_zip_remove, 946bf6a7a5aSArnd Bergmann .sriov_configure = IS_ENABLED(CONFIG_PCI_IOV) ? 947*719181f3SShukun Tan hisi_zip_sriov_configure : NULL, 94862c455caSZhou Wang .err_handler = &hisi_zip_err_handler, 94962c455caSZhou Wang }; 95062c455caSZhou Wang 95172c7a68dSZhou Wang static void hisi_zip_register_debugfs(void) 95272c7a68dSZhou Wang { 95372c7a68dSZhou Wang if (!debugfs_initialized()) 95472c7a68dSZhou Wang return; 95572c7a68dSZhou Wang 95672c7a68dSZhou Wang hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL); 95772c7a68dSZhou Wang if (IS_ERR_OR_NULL(hzip_debugfs_root)) 95872c7a68dSZhou Wang hzip_debugfs_root = NULL; 95972c7a68dSZhou Wang } 96072c7a68dSZhou Wang 96172c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void) 96272c7a68dSZhou Wang { 96372c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 96472c7a68dSZhou Wang } 96572c7a68dSZhou Wang 96662c455caSZhou Wang static int __init hisi_zip_init(void) 96762c455caSZhou Wang { 96862c455caSZhou Wang int ret; 96962c455caSZhou Wang 97072c7a68dSZhou Wang hisi_zip_register_debugfs(); 97172c7a68dSZhou Wang 97262c455caSZhou Wang ret = pci_register_driver(&hisi_zip_pci_driver); 97362c455caSZhou Wang if (ret < 0) { 97462c455caSZhou Wang pr_err("Failed to register pci driver.\n"); 97572c7a68dSZhou Wang goto err_pci; 97662c455caSZhou Wang } 97762c455caSZhou Wang 97862c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) { 97962c455caSZhou Wang ret = hisi_zip_register_to_crypto(); 98062c455caSZhou Wang if (ret < 0) { 98162c455caSZhou Wang pr_err("Failed to register driver to crypto.\n"); 98262c455caSZhou Wang goto err_crypto; 98362c455caSZhou Wang } 98462c455caSZhou Wang } 98562c455caSZhou Wang 98662c455caSZhou Wang return 0; 98762c455caSZhou Wang 98862c455caSZhou Wang err_crypto: 98962c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 99072c7a68dSZhou Wang err_pci: 99172c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 99272c7a68dSZhou Wang 99362c455caSZhou Wang return ret; 99462c455caSZhou Wang } 99562c455caSZhou Wang 99662c455caSZhou Wang static void __exit hisi_zip_exit(void) 99762c455caSZhou Wang { 99862c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) 99962c455caSZhou Wang hisi_zip_unregister_from_crypto(); 100062c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 100172c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 100262c455caSZhou Wang } 100362c455caSZhou Wang 100462c455caSZhou Wang module_init(hisi_zip_init); 100562c455caSZhou Wang module_exit(hisi_zip_exit); 100662c455caSZhou Wang 100762c455caSZhou Wang MODULE_LICENSE("GPL v2"); 100862c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 100962c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator"); 1010