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 8272c7a68dSZhou Wang #define HZIP_BUF_SIZE 22 8362c455caSZhou Wang 8462c455caSZhou Wang static const char hisi_zip_name[] = "hisi_zip"; 8572c7a68dSZhou Wang static struct dentry *hzip_debugfs_root; 86719181f3SShukun Tan static LIST_HEAD(hisi_zip_list); 87719181f3SShukun Tan static DEFINE_MUTEX(hisi_zip_list_lock); 8862c455caSZhou Wang 89700f7d0dSZhou Wang struct hisi_zip_resource { 90700f7d0dSZhou Wang struct hisi_zip *hzip; 91700f7d0dSZhou Wang int distance; 92700f7d0dSZhou Wang struct list_head list; 93700f7d0dSZhou Wang }; 94700f7d0dSZhou Wang 95700f7d0dSZhou Wang static void free_list(struct list_head *head) 9662c455caSZhou Wang { 97700f7d0dSZhou Wang struct hisi_zip_resource *res, *tmp; 9862c455caSZhou Wang 99700f7d0dSZhou Wang list_for_each_entry_safe(res, tmp, head, list) { 100700f7d0dSZhou Wang list_del(&res->list); 101700f7d0dSZhou Wang kfree(res); 10262c455caSZhou Wang } 10362c455caSZhou Wang } 10462c455caSZhou Wang 10562c455caSZhou Wang struct hisi_zip *find_zip_device(int node) 10662c455caSZhou Wang { 107700f7d0dSZhou Wang struct hisi_zip_resource *res, *tmp; 1081e67ee93SZhou Wang struct hisi_zip *ret = NULL; 109700f7d0dSZhou Wang struct hisi_zip *hisi_zip; 110700f7d0dSZhou Wang struct list_head *n; 111700f7d0dSZhou Wang struct device *dev; 112700f7d0dSZhou Wang LIST_HEAD(head); 11362c455caSZhou Wang 11462c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 115700f7d0dSZhou Wang 1161e67ee93SZhou Wang if (IS_ENABLED(CONFIG_NUMA)) { 117700f7d0dSZhou Wang list_for_each_entry(hisi_zip, &hisi_zip_list, list) { 118700f7d0dSZhou Wang res = kzalloc(sizeof(*res), GFP_KERNEL); 119700f7d0dSZhou Wang if (!res) 120700f7d0dSZhou Wang goto err; 121700f7d0dSZhou Wang 122700f7d0dSZhou Wang dev = &hisi_zip->qm.pdev->dev; 123700f7d0dSZhou Wang res->hzip = hisi_zip; 1241e67ee93SZhou Wang res->distance = node_distance(dev_to_node(dev), node); 125700f7d0dSZhou Wang 126700f7d0dSZhou Wang n = &head; 127700f7d0dSZhou Wang list_for_each_entry(tmp, &head, list) { 128700f7d0dSZhou Wang if (res->distance < tmp->distance) { 129700f7d0dSZhou Wang n = &tmp->list; 130700f7d0dSZhou Wang break; 131700f7d0dSZhou Wang } 132700f7d0dSZhou Wang } 133700f7d0dSZhou Wang list_add_tail(&res->list, n); 134700f7d0dSZhou Wang } 135700f7d0dSZhou Wang 136700f7d0dSZhou Wang list_for_each_entry(tmp, &head, list) { 137700f7d0dSZhou Wang if (hisi_qm_get_free_qp_num(&tmp->hzip->qm)) { 138700f7d0dSZhou Wang ret = tmp->hzip; 139700f7d0dSZhou Wang break; 140700f7d0dSZhou Wang } 141700f7d0dSZhou Wang } 142700f7d0dSZhou Wang 143700f7d0dSZhou Wang free_list(&head); 1441e67ee93SZhou Wang } else { 145700f7d0dSZhou Wang ret = list_first_entry(&hisi_zip_list, struct hisi_zip, list); 1461e67ee93SZhou Wang } 1471e67ee93SZhou Wang 14862c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 14962c455caSZhou Wang 150700f7d0dSZhou Wang return ret; 151700f7d0dSZhou Wang 152700f7d0dSZhou Wang err: 153700f7d0dSZhou Wang free_list(&head); 154700f7d0dSZhou Wang mutex_unlock(&hisi_zip_list_lock); 155700f7d0dSZhou Wang return NULL; 15662c455caSZhou Wang } 15762c455caSZhou Wang 15862c455caSZhou Wang struct hisi_zip_hw_error { 15962c455caSZhou Wang u32 int_msk; 16062c455caSZhou Wang const char *msg; 16162c455caSZhou Wang }; 16262c455caSZhou Wang 16362c455caSZhou Wang static const struct hisi_zip_hw_error zip_hw_error[] = { 16462c455caSZhou Wang { .int_msk = BIT(0), .msg = "zip_ecc_1bitt_err" }, 16562c455caSZhou Wang { .int_msk = BIT(1), .msg = "zip_ecc_2bit_err" }, 16662c455caSZhou Wang { .int_msk = BIT(2), .msg = "zip_axi_rresp_err" }, 16762c455caSZhou Wang { .int_msk = BIT(3), .msg = "zip_axi_bresp_err" }, 16862c455caSZhou Wang { .int_msk = BIT(4), .msg = "zip_src_addr_parse_err" }, 16962c455caSZhou Wang { .int_msk = BIT(5), .msg = "zip_dst_addr_parse_err" }, 17062c455caSZhou Wang { .int_msk = BIT(6), .msg = "zip_pre_in_addr_err" }, 17162c455caSZhou Wang { .int_msk = BIT(7), .msg = "zip_pre_in_data_err" }, 17262c455caSZhou Wang { .int_msk = BIT(8), .msg = "zip_com_inf_err" }, 17362c455caSZhou Wang { .int_msk = BIT(9), .msg = "zip_enc_inf_err" }, 17462c455caSZhou Wang { .int_msk = BIT(10), .msg = "zip_pre_out_err" }, 17562c455caSZhou Wang { /* sentinel */ } 17662c455caSZhou Wang }; 17762c455caSZhou Wang 17872c7a68dSZhou Wang enum ctrl_debug_file_index { 17972c7a68dSZhou Wang HZIP_CURRENT_QM, 18072c7a68dSZhou Wang HZIP_CLEAR_ENABLE, 18172c7a68dSZhou Wang HZIP_DEBUG_FILE_NUM, 18272c7a68dSZhou Wang }; 18372c7a68dSZhou Wang 18472c7a68dSZhou Wang static const char * const ctrl_debug_file_name[] = { 18572c7a68dSZhou Wang [HZIP_CURRENT_QM] = "current_qm", 18672c7a68dSZhou Wang [HZIP_CLEAR_ENABLE] = "clear_enable", 18772c7a68dSZhou Wang }; 18872c7a68dSZhou Wang 18972c7a68dSZhou Wang struct ctrl_debug_file { 19072c7a68dSZhou Wang enum ctrl_debug_file_index index; 19172c7a68dSZhou Wang spinlock_t lock; 19272c7a68dSZhou Wang struct hisi_zip_ctrl *ctrl; 19372c7a68dSZhou Wang }; 19472c7a68dSZhou Wang 19562c455caSZhou Wang /* 19662c455caSZhou Wang * One ZIP controller has one PF and multiple VFs, some global configurations 19762c455caSZhou Wang * which PF has need this structure. 19862c455caSZhou Wang * 19962c455caSZhou Wang * Just relevant for PF. 20062c455caSZhou Wang */ 20162c455caSZhou Wang struct hisi_zip_ctrl { 20279e09f30SZhou Wang u32 num_vfs; 20362c455caSZhou Wang struct hisi_zip *hisi_zip; 20472c7a68dSZhou Wang struct dentry *debug_root; 20572c7a68dSZhou Wang struct ctrl_debug_file files[HZIP_DEBUG_FILE_NUM]; 20672c7a68dSZhou Wang }; 20772c7a68dSZhou Wang 20872c7a68dSZhou Wang enum { 20972c7a68dSZhou Wang HZIP_COMP_CORE0, 21072c7a68dSZhou Wang HZIP_COMP_CORE1, 21172c7a68dSZhou Wang HZIP_DECOMP_CORE0, 21272c7a68dSZhou Wang HZIP_DECOMP_CORE1, 21372c7a68dSZhou Wang HZIP_DECOMP_CORE2, 21472c7a68dSZhou Wang HZIP_DECOMP_CORE3, 21572c7a68dSZhou Wang HZIP_DECOMP_CORE4, 21672c7a68dSZhou Wang HZIP_DECOMP_CORE5, 21772c7a68dSZhou Wang }; 21872c7a68dSZhou Wang 21972c7a68dSZhou Wang static const u64 core_offsets[] = { 22072c7a68dSZhou Wang [HZIP_COMP_CORE0] = 0x302000, 22172c7a68dSZhou Wang [HZIP_COMP_CORE1] = 0x303000, 22272c7a68dSZhou Wang [HZIP_DECOMP_CORE0] = 0x304000, 22372c7a68dSZhou Wang [HZIP_DECOMP_CORE1] = 0x305000, 22472c7a68dSZhou Wang [HZIP_DECOMP_CORE2] = 0x306000, 22572c7a68dSZhou Wang [HZIP_DECOMP_CORE3] = 0x307000, 22672c7a68dSZhou Wang [HZIP_DECOMP_CORE4] = 0x308000, 22772c7a68dSZhou Wang [HZIP_DECOMP_CORE5] = 0x309000, 22872c7a68dSZhou Wang }; 22972c7a68dSZhou Wang 23072c7a68dSZhou Wang static struct debugfs_reg32 hzip_dfx_regs[] = { 23172c7a68dSZhou Wang {"HZIP_GET_BD_NUM ", 0x00ull}, 23272c7a68dSZhou Wang {"HZIP_GET_RIGHT_BD ", 0x04ull}, 23372c7a68dSZhou Wang {"HZIP_GET_ERROR_BD ", 0x08ull}, 23472c7a68dSZhou Wang {"HZIP_DONE_BD_NUM ", 0x0cull}, 23572c7a68dSZhou Wang {"HZIP_WORK_CYCLE ", 0x10ull}, 23672c7a68dSZhou Wang {"HZIP_IDLE_CYCLE ", 0x18ull}, 23772c7a68dSZhou Wang {"HZIP_MAX_DELAY ", 0x20ull}, 23872c7a68dSZhou Wang {"HZIP_MIN_DELAY ", 0x24ull}, 23972c7a68dSZhou Wang {"HZIP_AVG_DELAY ", 0x28ull}, 24072c7a68dSZhou Wang {"HZIP_MEM_VISIBLE_DATA ", 0x30ull}, 24172c7a68dSZhou Wang {"HZIP_MEM_VISIBLE_ADDR ", 0x34ull}, 24272c7a68dSZhou Wang {"HZIP_COMSUMED_BYTE ", 0x38ull}, 24372c7a68dSZhou Wang {"HZIP_PRODUCED_BYTE ", 0x40ull}, 24472c7a68dSZhou Wang {"HZIP_COMP_INF ", 0x70ull}, 24572c7a68dSZhou Wang {"HZIP_PRE_OUT ", 0x78ull}, 24672c7a68dSZhou Wang {"HZIP_BD_RD ", 0x7cull}, 24772c7a68dSZhou Wang {"HZIP_BD_WR ", 0x80ull}, 24872c7a68dSZhou Wang {"HZIP_GET_BD_AXI_ERR_NUM ", 0x84ull}, 24972c7a68dSZhou Wang {"HZIP_GET_BD_PARSE_ERR_NUM ", 0x88ull}, 25072c7a68dSZhou Wang {"HZIP_ADD_BD_AXI_ERR_NUM ", 0x8cull}, 25172c7a68dSZhou Wang {"HZIP_DECOMP_STF_RELOAD_CURR_ST ", 0x94ull}, 25272c7a68dSZhou Wang {"HZIP_DECOMP_LZ77_CURR_ST ", 0x9cull}, 25362c455caSZhou Wang }; 25462c455caSZhou Wang 25562c455caSZhou Wang static int pf_q_num_set(const char *val, const struct kernel_param *kp) 25662c455caSZhou Wang { 25762c455caSZhou Wang struct pci_dev *pdev = pci_get_device(PCI_VENDOR_ID_HUAWEI, 25862c455caSZhou Wang PCI_DEVICE_ID_ZIP_PF, NULL); 25962c455caSZhou Wang u32 n, q_num; 26062c455caSZhou Wang u8 rev_id; 26162c455caSZhou Wang int ret; 26262c455caSZhou Wang 26362c455caSZhou Wang if (!val) 26462c455caSZhou Wang return -EINVAL; 26562c455caSZhou Wang 26662c455caSZhou Wang if (!pdev) { 26762c455caSZhou Wang q_num = min_t(u32, HZIP_QUEUE_NUM_V1, HZIP_QUEUE_NUM_V2); 26862c455caSZhou Wang pr_info("No device found currently, suppose queue number is %d\n", 26962c455caSZhou Wang q_num); 27062c455caSZhou Wang } else { 27162c455caSZhou Wang rev_id = pdev->revision; 27262c455caSZhou Wang switch (rev_id) { 27362c455caSZhou Wang case QM_HW_V1: 27462c455caSZhou Wang q_num = HZIP_QUEUE_NUM_V1; 27562c455caSZhou Wang break; 27662c455caSZhou Wang case QM_HW_V2: 27762c455caSZhou Wang q_num = HZIP_QUEUE_NUM_V2; 27862c455caSZhou Wang break; 27962c455caSZhou Wang default: 28062c455caSZhou Wang return -EINVAL; 28162c455caSZhou Wang } 28262c455caSZhou Wang } 28362c455caSZhou Wang 28462c455caSZhou Wang ret = kstrtou32(val, 10, &n); 28562c455caSZhou Wang if (ret != 0 || n > q_num || n == 0) 28662c455caSZhou Wang return -EINVAL; 28762c455caSZhou Wang 28862c455caSZhou Wang return param_set_int(val, kp); 28962c455caSZhou Wang } 29062c455caSZhou Wang 29162c455caSZhou Wang static const struct kernel_param_ops pf_q_num_ops = { 29262c455caSZhou Wang .set = pf_q_num_set, 29362c455caSZhou Wang .get = param_get_int, 29462c455caSZhou Wang }; 29562c455caSZhou Wang 29662c455caSZhou Wang static u32 pf_q_num = HZIP_PF_DEF_Q_NUM; 29762c455caSZhou Wang module_param_cb(pf_q_num, &pf_q_num_ops, &pf_q_num, 0444); 29862c455caSZhou Wang MODULE_PARM_DESC(pf_q_num, "Number of queues in PF(v1 1-4096, v2 1-1024)"); 29962c455caSZhou Wang 30062c455caSZhou Wang static int uacce_mode; 30162c455caSZhou Wang module_param(uacce_mode, int, 0); 30262c455caSZhou Wang 30339977f4bSHao Fang static u32 vfs_num; 30439977f4bSHao Fang module_param(vfs_num, uint, 0444); 30539977f4bSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63)"); 30639977f4bSHao Fang 30762c455caSZhou Wang static const struct pci_device_id hisi_zip_dev_ids[] = { 30862c455caSZhou Wang { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_PF) }, 30979e09f30SZhou Wang { PCI_DEVICE(PCI_VENDOR_ID_HUAWEI, PCI_DEVICE_ID_ZIP_VF) }, 31062c455caSZhou Wang { 0, } 31162c455caSZhou Wang }; 31262c455caSZhou Wang MODULE_DEVICE_TABLE(pci, hisi_zip_dev_ids); 31362c455caSZhou Wang 31462c455caSZhou Wang static inline void hisi_zip_add_to_list(struct hisi_zip *hisi_zip) 31562c455caSZhou Wang { 31662c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 31762c455caSZhou Wang list_add_tail(&hisi_zip->list, &hisi_zip_list); 31862c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 31962c455caSZhou Wang } 32062c455caSZhou Wang 32162c455caSZhou Wang static inline void hisi_zip_remove_from_list(struct hisi_zip *hisi_zip) 32262c455caSZhou Wang { 32362c455caSZhou Wang mutex_lock(&hisi_zip_list_lock); 32462c455caSZhou Wang list_del(&hisi_zip->list); 32562c455caSZhou Wang mutex_unlock(&hisi_zip_list_lock); 32662c455caSZhou Wang } 32762c455caSZhou Wang 32862c455caSZhou Wang static void hisi_zip_set_user_domain_and_cache(struct hisi_zip *hisi_zip) 32962c455caSZhou Wang { 33062c455caSZhou Wang void __iomem *base = hisi_zip->qm.io_base; 33162c455caSZhou Wang 33262c455caSZhou Wang /* qm user domain */ 33362c455caSZhou Wang writel(AXUSER_BASE, base + QM_ARUSER_M_CFG_1); 33462c455caSZhou Wang writel(ARUSER_M_CFG_ENABLE, base + QM_ARUSER_M_CFG_ENABLE); 33562c455caSZhou Wang writel(AXUSER_BASE, base + QM_AWUSER_M_CFG_1); 33662c455caSZhou Wang writel(AWUSER_M_CFG_ENABLE, base + QM_AWUSER_M_CFG_ENABLE); 33762c455caSZhou Wang writel(WUSER_M_CFG_ENABLE, base + QM_WUSER_M_CFG_ENABLE); 33862c455caSZhou Wang 33962c455caSZhou Wang /* qm cache */ 34062c455caSZhou Wang writel(AXI_M_CFG, base + QM_AXI_M_CFG); 34162c455caSZhou Wang writel(AXI_M_CFG_ENABLE, base + QM_AXI_M_CFG_ENABLE); 34262c455caSZhou Wang /* disable FLR triggered by BME(bus master enable) */ 34362c455caSZhou Wang writel(PEH_AXUSER_CFG, base + QM_PEH_AXUSER_CFG); 34462c455caSZhou Wang writel(PEH_AXUSER_CFG_ENABLE, base + QM_PEH_AXUSER_CFG_ENABLE); 34562c455caSZhou Wang 34662c455caSZhou Wang /* cache */ 34762c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_0); 34862c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_ARCA_CHE_1); 34962c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_0); 35062c455caSZhou Wang writel(CACHE_ALL_EN, base + HZIP_PORT_AWCA_CHE_1); 35162c455caSZhou Wang 35262c455caSZhou Wang /* user domain configurations */ 35362c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_BD_RUSER_32_63); 35462c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_SGL_RUSER_32_63); 35562c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_BD_WUSER_32_63); 35662c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_DATA_RUSER_32_63); 35762c455caSZhou Wang writel(AXUSER_BASE, base + HZIP_DATA_WUSER_32_63); 35862c455caSZhou Wang 35962c455caSZhou Wang /* let's open all compression/decompression cores */ 36062c455caSZhou Wang writel(DECOMP_CHECK_ENABLE | ALL_COMP_DECOMP_EN, 36162c455caSZhou Wang base + HZIP_CLOCK_GATE_CTRL); 36262c455caSZhou Wang 36362c455caSZhou Wang /* enable sqc writeback */ 36462c455caSZhou Wang writel(SQC_CACHE_ENABLE | CQC_CACHE_ENABLE | SQC_CACHE_WB_ENABLE | 36562c455caSZhou Wang CQC_CACHE_WB_ENABLE | FIELD_PREP(SQC_CACHE_WB_THRD, 1) | 36662c455caSZhou Wang FIELD_PREP(CQC_CACHE_WB_THRD, 1), base + QM_CACHE_CTL); 36762c455caSZhou Wang } 36862c455caSZhou Wang 36962c455caSZhou Wang static void hisi_zip_hw_error_set_state(struct hisi_zip *hisi_zip, bool state) 37062c455caSZhou Wang { 37162c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 37262c455caSZhou Wang 37362c455caSZhou Wang if (qm->ver == QM_HW_V1) { 37462c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, qm->io_base + HZIP_CORE_INT_MASK); 375ee1788c6SZhou Wang dev_info(&qm->pdev->dev, "Does not support hw error handle\n"); 37662c455caSZhou Wang return; 37762c455caSZhou Wang } 37862c455caSZhou Wang 37962c455caSZhou Wang if (state) { 38062c455caSZhou Wang /* clear ZIP hw error source if having */ 38162c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, hisi_zip->qm.io_base + 38262c455caSZhou Wang HZIP_CORE_INT_SOURCE); 38362c455caSZhou Wang /* enable ZIP hw error interrupts */ 38462c455caSZhou Wang writel(0, hisi_zip->qm.io_base + HZIP_CORE_INT_MASK); 38562c455caSZhou Wang } else { 38662c455caSZhou Wang /* disable ZIP hw error interrupts */ 38762c455caSZhou Wang writel(HZIP_CORE_INT_DISABLE, 38862c455caSZhou Wang hisi_zip->qm.io_base + HZIP_CORE_INT_MASK); 38962c455caSZhou Wang } 39062c455caSZhou Wang } 39162c455caSZhou Wang 39272c7a68dSZhou Wang static inline struct hisi_qm *file_to_qm(struct ctrl_debug_file *file) 39372c7a68dSZhou Wang { 39472c7a68dSZhou Wang struct hisi_zip *hisi_zip = file->ctrl->hisi_zip; 39572c7a68dSZhou Wang 39672c7a68dSZhou Wang return &hisi_zip->qm; 39772c7a68dSZhou Wang } 39872c7a68dSZhou Wang 39972c7a68dSZhou Wang static u32 current_qm_read(struct ctrl_debug_file *file) 40072c7a68dSZhou Wang { 40172c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 40272c7a68dSZhou Wang 40372c7a68dSZhou Wang return readl(qm->io_base + QM_DFX_MB_CNT_VF); 40472c7a68dSZhou Wang } 40572c7a68dSZhou Wang 40672c7a68dSZhou Wang static int current_qm_write(struct ctrl_debug_file *file, u32 val) 40772c7a68dSZhou Wang { 40872c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 40972c7a68dSZhou Wang struct hisi_zip_ctrl *ctrl = file->ctrl; 41072c7a68dSZhou Wang u32 vfq_num; 41172c7a68dSZhou Wang u32 tmp; 41272c7a68dSZhou Wang 41372c7a68dSZhou Wang if (val > ctrl->num_vfs) 41472c7a68dSZhou Wang return -EINVAL; 41572c7a68dSZhou Wang 41672c7a68dSZhou Wang /* Calculate curr_qm_qp_num and store */ 41772c7a68dSZhou Wang if (val == 0) { 41872c7a68dSZhou Wang qm->debug.curr_qm_qp_num = qm->qp_num; 41972c7a68dSZhou Wang } else { 42072c7a68dSZhou Wang vfq_num = (qm->ctrl_qp_num - qm->qp_num) / ctrl->num_vfs; 42172c7a68dSZhou Wang if (val == ctrl->num_vfs) 42272c7a68dSZhou Wang qm->debug.curr_qm_qp_num = qm->ctrl_qp_num - 42372c7a68dSZhou Wang qm->qp_num - (ctrl->num_vfs - 1) * vfq_num; 42472c7a68dSZhou Wang else 42572c7a68dSZhou Wang qm->debug.curr_qm_qp_num = vfq_num; 42672c7a68dSZhou Wang } 42772c7a68dSZhou Wang 42872c7a68dSZhou Wang writel(val, qm->io_base + QM_DFX_MB_CNT_VF); 42972c7a68dSZhou Wang writel(val, qm->io_base + QM_DFX_DB_CNT_VF); 43072c7a68dSZhou Wang 43172c7a68dSZhou Wang tmp = val | 43272c7a68dSZhou Wang (readl(qm->io_base + QM_DFX_SQE_CNT_VF_SQN) & CURRENT_Q_MASK); 43372c7a68dSZhou Wang writel(tmp, qm->io_base + QM_DFX_SQE_CNT_VF_SQN); 43472c7a68dSZhou Wang 43572c7a68dSZhou Wang tmp = val | 43672c7a68dSZhou Wang (readl(qm->io_base + QM_DFX_CQE_CNT_VF_CQN) & CURRENT_Q_MASK); 43772c7a68dSZhou Wang writel(tmp, qm->io_base + QM_DFX_CQE_CNT_VF_CQN); 43872c7a68dSZhou Wang 43972c7a68dSZhou Wang return 0; 44072c7a68dSZhou Wang } 44172c7a68dSZhou Wang 44272c7a68dSZhou Wang static u32 clear_enable_read(struct ctrl_debug_file *file) 44372c7a68dSZhou Wang { 44472c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 44572c7a68dSZhou Wang 44672c7a68dSZhou Wang return readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 44772c7a68dSZhou Wang SOFT_CTRL_CNT_CLR_CE_BIT; 44872c7a68dSZhou Wang } 44972c7a68dSZhou Wang 45072c7a68dSZhou Wang static int clear_enable_write(struct ctrl_debug_file *file, u32 val) 45172c7a68dSZhou Wang { 45272c7a68dSZhou Wang struct hisi_qm *qm = file_to_qm(file); 45372c7a68dSZhou Wang u32 tmp; 45472c7a68dSZhou Wang 45572c7a68dSZhou Wang if (val != 1 && val != 0) 45672c7a68dSZhou Wang return -EINVAL; 45772c7a68dSZhou Wang 45872c7a68dSZhou Wang tmp = (readl(qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE) & 45972c7a68dSZhou Wang ~SOFT_CTRL_CNT_CLR_CE_BIT) | val; 46072c7a68dSZhou Wang writel(tmp, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 46172c7a68dSZhou Wang 46272c7a68dSZhou Wang return 0; 46372c7a68dSZhou Wang } 46472c7a68dSZhou Wang 46572c7a68dSZhou Wang static ssize_t ctrl_debug_read(struct file *filp, char __user *buf, 46672c7a68dSZhou Wang size_t count, loff_t *pos) 46772c7a68dSZhou Wang { 46872c7a68dSZhou Wang struct ctrl_debug_file *file = filp->private_data; 46972c7a68dSZhou Wang char tbuf[HZIP_BUF_SIZE]; 47072c7a68dSZhou Wang u32 val; 47172c7a68dSZhou Wang int ret; 47272c7a68dSZhou Wang 47372c7a68dSZhou Wang spin_lock_irq(&file->lock); 47472c7a68dSZhou Wang switch (file->index) { 47572c7a68dSZhou Wang case HZIP_CURRENT_QM: 47672c7a68dSZhou Wang val = current_qm_read(file); 47772c7a68dSZhou Wang break; 47872c7a68dSZhou Wang case HZIP_CLEAR_ENABLE: 47972c7a68dSZhou Wang val = clear_enable_read(file); 48072c7a68dSZhou Wang break; 48172c7a68dSZhou Wang default: 48272c7a68dSZhou Wang spin_unlock_irq(&file->lock); 48372c7a68dSZhou Wang return -EINVAL; 48472c7a68dSZhou Wang } 48572c7a68dSZhou Wang spin_unlock_irq(&file->lock); 48672c7a68dSZhou Wang ret = sprintf(tbuf, "%u\n", val); 48772c7a68dSZhou Wang return simple_read_from_buffer(buf, count, pos, tbuf, ret); 48872c7a68dSZhou Wang } 48972c7a68dSZhou Wang 49072c7a68dSZhou Wang static ssize_t ctrl_debug_write(struct file *filp, const char __user *buf, 49172c7a68dSZhou Wang size_t count, loff_t *pos) 49272c7a68dSZhou Wang { 49372c7a68dSZhou Wang struct ctrl_debug_file *file = filp->private_data; 49472c7a68dSZhou Wang char tbuf[HZIP_BUF_SIZE]; 49572c7a68dSZhou Wang unsigned long val; 49672c7a68dSZhou Wang int len, ret; 49772c7a68dSZhou Wang 49872c7a68dSZhou Wang if (*pos != 0) 49972c7a68dSZhou Wang return 0; 50072c7a68dSZhou Wang 50172c7a68dSZhou Wang if (count >= HZIP_BUF_SIZE) 50272c7a68dSZhou Wang return -ENOSPC; 50372c7a68dSZhou Wang 50472c7a68dSZhou Wang len = simple_write_to_buffer(tbuf, HZIP_BUF_SIZE - 1, pos, buf, count); 50572c7a68dSZhou Wang if (len < 0) 50672c7a68dSZhou Wang return len; 50772c7a68dSZhou Wang 50872c7a68dSZhou Wang tbuf[len] = '\0'; 50972c7a68dSZhou Wang if (kstrtoul(tbuf, 0, &val)) 51072c7a68dSZhou Wang return -EFAULT; 51172c7a68dSZhou Wang 51272c7a68dSZhou Wang spin_lock_irq(&file->lock); 51372c7a68dSZhou Wang switch (file->index) { 51472c7a68dSZhou Wang case HZIP_CURRENT_QM: 51572c7a68dSZhou Wang ret = current_qm_write(file, val); 51672c7a68dSZhou Wang if (ret) 51772c7a68dSZhou Wang goto err_input; 51872c7a68dSZhou Wang break; 51972c7a68dSZhou Wang case HZIP_CLEAR_ENABLE: 52072c7a68dSZhou Wang ret = clear_enable_write(file, val); 52172c7a68dSZhou Wang if (ret) 52272c7a68dSZhou Wang goto err_input; 52372c7a68dSZhou Wang break; 52472c7a68dSZhou Wang default: 52572c7a68dSZhou Wang ret = -EINVAL; 52672c7a68dSZhou Wang goto err_input; 52772c7a68dSZhou Wang } 52872c7a68dSZhou Wang spin_unlock_irq(&file->lock); 52972c7a68dSZhou Wang 53072c7a68dSZhou Wang return count; 53172c7a68dSZhou Wang 53272c7a68dSZhou Wang err_input: 53372c7a68dSZhou Wang spin_unlock_irq(&file->lock); 53472c7a68dSZhou Wang return ret; 53572c7a68dSZhou Wang } 53672c7a68dSZhou Wang 53772c7a68dSZhou Wang static const struct file_operations ctrl_debug_fops = { 53872c7a68dSZhou Wang .owner = THIS_MODULE, 53972c7a68dSZhou Wang .open = simple_open, 54072c7a68dSZhou Wang .read = ctrl_debug_read, 54172c7a68dSZhou Wang .write = ctrl_debug_write, 54272c7a68dSZhou Wang }; 54372c7a68dSZhou Wang 54472c7a68dSZhou Wang static int hisi_zip_core_debug_init(struct hisi_zip_ctrl *ctrl) 54572c7a68dSZhou Wang { 54672c7a68dSZhou Wang struct hisi_zip *hisi_zip = ctrl->hisi_zip; 54772c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 54872c7a68dSZhou Wang struct device *dev = &qm->pdev->dev; 54972c7a68dSZhou Wang struct debugfs_regset32 *regset; 550*4a97bfc7SGreg Kroah-Hartman struct dentry *tmp_d; 55172c7a68dSZhou Wang char buf[HZIP_BUF_SIZE]; 55272c7a68dSZhou Wang int i; 55372c7a68dSZhou Wang 55472c7a68dSZhou Wang for (i = 0; i < HZIP_CORE_NUM; i++) { 55572c7a68dSZhou Wang if (i < HZIP_COMP_CORE_NUM) 55672c7a68dSZhou Wang sprintf(buf, "comp_core%d", i); 55772c7a68dSZhou Wang else 55872c7a68dSZhou Wang sprintf(buf, "decomp_core%d", i - HZIP_COMP_CORE_NUM); 55972c7a68dSZhou Wang 56072c7a68dSZhou Wang regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL); 56172c7a68dSZhou Wang if (!regset) 56272c7a68dSZhou Wang return -ENOENT; 56372c7a68dSZhou Wang 56472c7a68dSZhou Wang regset->regs = hzip_dfx_regs; 56572c7a68dSZhou Wang regset->nregs = ARRAY_SIZE(hzip_dfx_regs); 56672c7a68dSZhou Wang regset->base = qm->io_base + core_offsets[i]; 56772c7a68dSZhou Wang 568*4a97bfc7SGreg Kroah-Hartman tmp_d = debugfs_create_dir(buf, ctrl->debug_root); 569*4a97bfc7SGreg Kroah-Hartman debugfs_create_regset32("regs", 0444, tmp_d, regset); 57072c7a68dSZhou Wang } 57172c7a68dSZhou Wang 57272c7a68dSZhou Wang return 0; 57372c7a68dSZhou Wang } 57472c7a68dSZhou Wang 57572c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl) 57672c7a68dSZhou Wang { 57772c7a68dSZhou Wang int i; 57872c7a68dSZhou Wang 57972c7a68dSZhou Wang for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) { 58072c7a68dSZhou Wang spin_lock_init(&ctrl->files[i].lock); 58172c7a68dSZhou Wang ctrl->files[i].ctrl = ctrl; 58272c7a68dSZhou Wang ctrl->files[i].index = i; 58372c7a68dSZhou Wang 584*4a97bfc7SGreg Kroah-Hartman debugfs_create_file(ctrl_debug_file_name[i], 0600, 58572c7a68dSZhou Wang ctrl->debug_root, ctrl->files + i, 58672c7a68dSZhou Wang &ctrl_debug_fops); 58772c7a68dSZhou Wang } 58872c7a68dSZhou Wang 58972c7a68dSZhou Wang return hisi_zip_core_debug_init(ctrl); 59072c7a68dSZhou Wang } 59172c7a68dSZhou Wang 59272c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip) 59372c7a68dSZhou Wang { 59472c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 59572c7a68dSZhou Wang struct device *dev = &qm->pdev->dev; 59672c7a68dSZhou Wang struct dentry *dev_d; 59772c7a68dSZhou Wang int ret; 59872c7a68dSZhou Wang 59972c7a68dSZhou Wang dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root); 60072c7a68dSZhou Wang 60172c7a68dSZhou Wang qm->debug.debug_root = dev_d; 60272c7a68dSZhou Wang ret = hisi_qm_debug_init(qm); 60372c7a68dSZhou Wang if (ret) 60472c7a68dSZhou Wang goto failed_to_create; 60572c7a68dSZhou Wang 60672c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) { 60772c7a68dSZhou Wang hisi_zip->ctrl->debug_root = dev_d; 60872c7a68dSZhou Wang ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl); 60972c7a68dSZhou Wang if (ret) 61072c7a68dSZhou Wang goto failed_to_create; 61172c7a68dSZhou Wang } 61272c7a68dSZhou Wang 61372c7a68dSZhou Wang return 0; 61472c7a68dSZhou Wang 61572c7a68dSZhou Wang failed_to_create: 61672c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 61772c7a68dSZhou Wang return ret; 61872c7a68dSZhou Wang } 61972c7a68dSZhou Wang 62072c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip) 62172c7a68dSZhou Wang { 62272c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 62372c7a68dSZhou Wang 62472c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF); 62572c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF); 62672c7a68dSZhou Wang writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 62772c7a68dSZhou Wang 62872c7a68dSZhou Wang hisi_qm_debug_regs_clear(qm); 62972c7a68dSZhou Wang } 63072c7a68dSZhou Wang 63172c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip) 63272c7a68dSZhou Wang { 63372c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 63472c7a68dSZhou Wang 63572c7a68dSZhou Wang debugfs_remove_recursive(qm->debug.debug_root); 63672c7a68dSZhou Wang 63772c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) 63872c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 63972c7a68dSZhou Wang } 64072c7a68dSZhou Wang 64162c455caSZhou Wang static void hisi_zip_hw_error_init(struct hisi_zip *hisi_zip) 64262c455caSZhou Wang { 64362c455caSZhou Wang hisi_qm_hw_error_init(&hisi_zip->qm, QM_BASE_CE, 64462c455caSZhou Wang QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT, 0, 64562c455caSZhou Wang QM_DB_RANDOM_INVALID); 64662c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, true); 64762c455caSZhou Wang } 64862c455caSZhou Wang 64962c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip) 65062c455caSZhou Wang { 65162c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 65262c455caSZhou Wang struct hisi_zip_ctrl *ctrl; 65362c455caSZhou Wang 65462c455caSZhou Wang ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL); 65562c455caSZhou Wang if (!ctrl) 65662c455caSZhou Wang return -ENOMEM; 65762c455caSZhou Wang 65862c455caSZhou Wang hisi_zip->ctrl = ctrl; 65962c455caSZhou Wang ctrl->hisi_zip = hisi_zip; 66062c455caSZhou Wang 66162c455caSZhou Wang switch (qm->ver) { 66262c455caSZhou Wang case QM_HW_V1: 66362c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1; 66462c455caSZhou Wang break; 66562c455caSZhou Wang 66662c455caSZhou Wang case QM_HW_V2: 66762c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2; 66862c455caSZhou Wang break; 66962c455caSZhou Wang 67062c455caSZhou Wang default: 67162c455caSZhou Wang return -EINVAL; 67262c455caSZhou Wang } 67362c455caSZhou Wang 67462c455caSZhou Wang hisi_zip_set_user_domain_and_cache(hisi_zip); 67562c455caSZhou Wang hisi_zip_hw_error_init(hisi_zip); 67672c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 67762c455caSZhou Wang 67862c455caSZhou Wang return 0; 67962c455caSZhou Wang } 68062c455caSZhou Wang 68179e09f30SZhou Wang /* Currently we only support equal assignment */ 68279e09f30SZhou Wang static int hisi_zip_vf_q_assign(struct hisi_zip *hisi_zip, int num_vfs) 68379e09f30SZhou Wang { 68479e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 68579e09f30SZhou Wang u32 qp_num = qm->qp_num; 68679e09f30SZhou Wang u32 q_base = qp_num; 68779e09f30SZhou Wang u32 q_num, remain_q_num, i; 68879e09f30SZhou Wang int ret; 68979e09f30SZhou Wang 69079e09f30SZhou Wang if (!num_vfs) 69179e09f30SZhou Wang return -EINVAL; 69279e09f30SZhou Wang 69379e09f30SZhou Wang remain_q_num = qm->ctrl_qp_num - qp_num; 69479e09f30SZhou Wang if (remain_q_num < num_vfs) 69579e09f30SZhou Wang return -EINVAL; 69679e09f30SZhou Wang 69779e09f30SZhou Wang q_num = remain_q_num / num_vfs; 69879e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 69979e09f30SZhou Wang if (i == num_vfs) 70079e09f30SZhou Wang q_num += remain_q_num % num_vfs; 70179e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, q_base, q_num); 70279e09f30SZhou Wang if (ret) 70379e09f30SZhou Wang return ret; 70479e09f30SZhou Wang q_base += q_num; 70579e09f30SZhou Wang } 70679e09f30SZhou Wang 70779e09f30SZhou Wang return 0; 70879e09f30SZhou Wang } 70979e09f30SZhou Wang 71079e09f30SZhou Wang static int hisi_zip_clear_vft_config(struct hisi_zip *hisi_zip) 71179e09f30SZhou Wang { 71279e09f30SZhou Wang struct hisi_zip_ctrl *ctrl = hisi_zip->ctrl; 71379e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 71479e09f30SZhou Wang u32 i, num_vfs = ctrl->num_vfs; 71579e09f30SZhou Wang int ret; 71679e09f30SZhou Wang 71779e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 71879e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, 0, 0); 71979e09f30SZhou Wang if (ret) 72079e09f30SZhou Wang return ret; 72179e09f30SZhou Wang } 72279e09f30SZhou Wang 72379e09f30SZhou Wang ctrl->num_vfs = 0; 72479e09f30SZhou Wang 72579e09f30SZhou Wang return 0; 72679e09f30SZhou Wang } 72779e09f30SZhou Wang 72879e09f30SZhou Wang static int hisi_zip_sriov_enable(struct pci_dev *pdev, int max_vfs) 72979e09f30SZhou Wang { 73079e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 73179e09f30SZhou Wang int pre_existing_vfs, num_vfs, ret; 73279e09f30SZhou Wang 73379e09f30SZhou Wang pre_existing_vfs = pci_num_vf(pdev); 73479e09f30SZhou Wang 73579e09f30SZhou Wang if (pre_existing_vfs) { 73679e09f30SZhou Wang dev_err(&pdev->dev, 73779e09f30SZhou Wang "Can't enable VF. Please disable pre-enabled VFs!\n"); 73879e09f30SZhou Wang return 0; 73979e09f30SZhou Wang } 74079e09f30SZhou Wang 74179e09f30SZhou Wang num_vfs = min_t(int, max_vfs, HZIP_VF_NUM); 74279e09f30SZhou Wang 74379e09f30SZhou Wang ret = hisi_zip_vf_q_assign(hisi_zip, num_vfs); 74479e09f30SZhou Wang if (ret) { 74579e09f30SZhou Wang dev_err(&pdev->dev, "Can't assign queues for VF!\n"); 74679e09f30SZhou Wang return ret; 74779e09f30SZhou Wang } 74879e09f30SZhou Wang 74979e09f30SZhou Wang hisi_zip->ctrl->num_vfs = num_vfs; 75079e09f30SZhou Wang 75179e09f30SZhou Wang ret = pci_enable_sriov(pdev, num_vfs); 75279e09f30SZhou Wang if (ret) { 75379e09f30SZhou Wang dev_err(&pdev->dev, "Can't enable VF!\n"); 75479e09f30SZhou Wang hisi_zip_clear_vft_config(hisi_zip); 75579e09f30SZhou Wang return ret; 75679e09f30SZhou Wang } 75779e09f30SZhou Wang 75879e09f30SZhou Wang return num_vfs; 75979e09f30SZhou Wang } 76079e09f30SZhou Wang 76179e09f30SZhou Wang static int hisi_zip_sriov_disable(struct pci_dev *pdev) 76279e09f30SZhou Wang { 76379e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 76479e09f30SZhou Wang 76579e09f30SZhou Wang if (pci_vfs_assigned(pdev)) { 76679e09f30SZhou Wang dev_err(&pdev->dev, 76779e09f30SZhou Wang "Can't disable VFs while VFs are assigned!\n"); 76879e09f30SZhou Wang return -EPERM; 76979e09f30SZhou Wang } 77079e09f30SZhou Wang 77179e09f30SZhou Wang /* remove in hisi_zip_pci_driver will be called to free VF resources */ 77279e09f30SZhou Wang pci_disable_sriov(pdev); 77379e09f30SZhou Wang 77479e09f30SZhou Wang return hisi_zip_clear_vft_config(hisi_zip); 77579e09f30SZhou Wang } 77679e09f30SZhou Wang 77739977f4bSHao Fang static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id) 77839977f4bSHao Fang { 77939977f4bSHao Fang struct hisi_zip *hisi_zip; 78039977f4bSHao Fang enum qm_hw_ver rev_id; 78139977f4bSHao Fang struct hisi_qm *qm; 78239977f4bSHao Fang int ret; 78339977f4bSHao Fang 78439977f4bSHao Fang rev_id = hisi_qm_get_hw_version(pdev); 78539977f4bSHao Fang if (rev_id == QM_HW_UNKNOWN) 78639977f4bSHao Fang return -EINVAL; 78739977f4bSHao Fang 78839977f4bSHao Fang hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL); 78939977f4bSHao Fang if (!hisi_zip) 79039977f4bSHao Fang return -ENOMEM; 79139977f4bSHao Fang pci_set_drvdata(pdev, hisi_zip); 79239977f4bSHao Fang 79339977f4bSHao Fang qm = &hisi_zip->qm; 79439977f4bSHao Fang qm->pdev = pdev; 79539977f4bSHao Fang qm->ver = rev_id; 79639977f4bSHao Fang 79739977f4bSHao Fang qm->sqe_size = HZIP_SQE_SIZE; 79839977f4bSHao Fang qm->dev_name = hisi_zip_name; 79939977f4bSHao Fang qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? QM_HW_PF : 80039977f4bSHao Fang QM_HW_VF; 80139977f4bSHao Fang switch (uacce_mode) { 80239977f4bSHao Fang case 0: 80339977f4bSHao Fang qm->use_dma_api = true; 80439977f4bSHao Fang break; 80539977f4bSHao Fang case 1: 80639977f4bSHao Fang qm->use_dma_api = false; 80739977f4bSHao Fang break; 80839977f4bSHao Fang case 2: 80939977f4bSHao Fang qm->use_dma_api = true; 81039977f4bSHao Fang break; 81139977f4bSHao Fang default: 81239977f4bSHao Fang return -EINVAL; 81339977f4bSHao Fang } 81439977f4bSHao Fang 81539977f4bSHao Fang ret = hisi_qm_init(qm); 81639977f4bSHao Fang if (ret) { 81739977f4bSHao Fang dev_err(&pdev->dev, "Failed to init qm!\n"); 81839977f4bSHao Fang return ret; 81939977f4bSHao Fang } 82039977f4bSHao Fang 82139977f4bSHao Fang if (qm->fun_type == QM_HW_PF) { 82239977f4bSHao Fang ret = hisi_zip_pf_probe_init(hisi_zip); 82339977f4bSHao Fang if (ret) 82439977f4bSHao Fang return ret; 82539977f4bSHao Fang 82639977f4bSHao Fang qm->qp_base = HZIP_PF_DEF_Q_BASE; 82739977f4bSHao Fang qm->qp_num = pf_q_num; 82839977f4bSHao Fang } else if (qm->fun_type == QM_HW_VF) { 82939977f4bSHao Fang /* 83039977f4bSHao Fang * have no way to get qm configure in VM in v1 hardware, 83139977f4bSHao Fang * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force 83239977f4bSHao Fang * to trigger only one VF in v1 hardware. 83339977f4bSHao Fang * 83439977f4bSHao Fang * v2 hardware has no such problem. 83539977f4bSHao Fang */ 83639977f4bSHao Fang if (qm->ver == QM_HW_V1) { 83739977f4bSHao Fang qm->qp_base = HZIP_PF_DEF_Q_NUM; 83839977f4bSHao Fang qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM; 83939977f4bSHao Fang } else if (qm->ver == QM_HW_V2) 84039977f4bSHao Fang /* v2 starts to support get vft by mailbox */ 84139977f4bSHao Fang hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num); 84239977f4bSHao Fang } 84339977f4bSHao Fang 84439977f4bSHao Fang ret = hisi_qm_start(qm); 84539977f4bSHao Fang if (ret) 84639977f4bSHao Fang goto err_qm_uninit; 84739977f4bSHao Fang 84839977f4bSHao Fang ret = hisi_zip_debugfs_init(hisi_zip); 84939977f4bSHao Fang if (ret) 85039977f4bSHao Fang dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret); 85139977f4bSHao Fang 85239977f4bSHao Fang hisi_zip_add_to_list(hisi_zip); 85339977f4bSHao Fang 85439977f4bSHao Fang if (qm->fun_type == QM_HW_PF && vfs_num > 0) { 85539977f4bSHao Fang ret = hisi_zip_sriov_enable(pdev, vfs_num); 85639977f4bSHao Fang if (ret < 0) 85739977f4bSHao Fang goto err_remove_from_list; 85839977f4bSHao Fang } 85939977f4bSHao Fang 86039977f4bSHao Fang return 0; 86139977f4bSHao Fang 86239977f4bSHao Fang err_remove_from_list: 86339977f4bSHao Fang hisi_zip_remove_from_list(hisi_zip); 86439977f4bSHao Fang hisi_zip_debugfs_exit(hisi_zip); 86539977f4bSHao Fang hisi_qm_stop(qm); 86639977f4bSHao Fang err_qm_uninit: 86739977f4bSHao Fang hisi_qm_uninit(qm); 86839977f4bSHao Fang return ret; 86939977f4bSHao Fang } 87039977f4bSHao Fang 87179e09f30SZhou Wang static int hisi_zip_sriov_configure(struct pci_dev *pdev, int num_vfs) 87279e09f30SZhou Wang { 87379e09f30SZhou Wang if (num_vfs == 0) 87479e09f30SZhou Wang return hisi_zip_sriov_disable(pdev); 87579e09f30SZhou Wang else 87679e09f30SZhou Wang return hisi_zip_sriov_enable(pdev, num_vfs); 87779e09f30SZhou Wang } 87879e09f30SZhou Wang 87962c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev) 88062c455caSZhou Wang { 88162c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 88262c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 88362c455caSZhou Wang 88479e09f30SZhou Wang if (qm->fun_type == QM_HW_PF && hisi_zip->ctrl->num_vfs != 0) 88579e09f30SZhou Wang hisi_zip_sriov_disable(pdev); 88679e09f30SZhou Wang 88772c7a68dSZhou Wang hisi_zip_debugfs_exit(hisi_zip); 88862c455caSZhou Wang hisi_qm_stop(qm); 88979e09f30SZhou Wang 89079e09f30SZhou Wang if (qm->fun_type == QM_HW_PF) 89162c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, false); 89279e09f30SZhou Wang 89362c455caSZhou Wang hisi_qm_uninit(qm); 89462c455caSZhou Wang hisi_zip_remove_from_list(hisi_zip); 89562c455caSZhou Wang } 89662c455caSZhou Wang 89762c455caSZhou Wang static void hisi_zip_log_hw_error(struct hisi_zip *hisi_zip, u32 err_sts) 89862c455caSZhou Wang { 89962c455caSZhou Wang const struct hisi_zip_hw_error *err = zip_hw_error; 90062c455caSZhou Wang struct device *dev = &hisi_zip->qm.pdev->dev; 90162c455caSZhou Wang u32 err_val; 90262c455caSZhou Wang 90362c455caSZhou Wang while (err->msg) { 90462c455caSZhou Wang if (err->int_msk & err_sts) { 90562c455caSZhou Wang dev_warn(dev, "%s [error status=0x%x] found\n", 90662c455caSZhou Wang err->msg, err->int_msk); 90762c455caSZhou Wang 90862c455caSZhou Wang if (HZIP_CORE_INT_STATUS_M_ECC & err->int_msk) { 90962c455caSZhou Wang err_val = readl(hisi_zip->qm.io_base + 91062c455caSZhou Wang HZIP_CORE_SRAM_ECC_ERR_INFO); 91162c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram num=0x%x\n", 91262c455caSZhou Wang ((err_val >> SRAM_ECC_ERR_NUM_SHIFT) & 91362c455caSZhou Wang 0xFF)); 91462c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram addr=0x%x\n", 91562c455caSZhou Wang (err_val >> SRAM_ECC_ERR_ADDR_SHIFT)); 91662c455caSZhou Wang } 91762c455caSZhou Wang } 91862c455caSZhou Wang err++; 91962c455caSZhou Wang } 92062c455caSZhou Wang } 92162c455caSZhou Wang 92262c455caSZhou Wang static pci_ers_result_t hisi_zip_hw_error_handle(struct hisi_zip *hisi_zip) 92362c455caSZhou Wang { 92462c455caSZhou Wang u32 err_sts; 92562c455caSZhou Wang 92662c455caSZhou Wang /* read err sts */ 92762c455caSZhou Wang err_sts = readl(hisi_zip->qm.io_base + HZIP_CORE_INT_STATUS); 92862c455caSZhou Wang 92962c455caSZhou Wang if (err_sts) { 93062c455caSZhou Wang hisi_zip_log_hw_error(hisi_zip, err_sts); 93162c455caSZhou Wang /* clear error interrupts */ 93262c455caSZhou Wang writel(err_sts, hisi_zip->qm.io_base + HZIP_CORE_INT_SOURCE); 93362c455caSZhou Wang 93462c455caSZhou Wang return PCI_ERS_RESULT_NEED_RESET; 93562c455caSZhou Wang } 93662c455caSZhou Wang 93762c455caSZhou Wang return PCI_ERS_RESULT_RECOVERED; 93862c455caSZhou Wang } 93962c455caSZhou Wang 94062c455caSZhou Wang static pci_ers_result_t hisi_zip_process_hw_error(struct pci_dev *pdev) 94162c455caSZhou Wang { 94262c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 94362c455caSZhou Wang struct device *dev = &pdev->dev; 94462c455caSZhou Wang pci_ers_result_t qm_ret, zip_ret; 94562c455caSZhou Wang 94662c455caSZhou Wang if (!hisi_zip) { 94762c455caSZhou Wang dev_err(dev, 94862c455caSZhou Wang "Can't recover ZIP-error occurred during device init\n"); 94962c455caSZhou Wang return PCI_ERS_RESULT_NONE; 95062c455caSZhou Wang } 95162c455caSZhou Wang 95262c455caSZhou Wang qm_ret = hisi_qm_hw_error_handle(&hisi_zip->qm); 95362c455caSZhou Wang 95462c455caSZhou Wang zip_ret = hisi_zip_hw_error_handle(hisi_zip); 95562c455caSZhou Wang 95662c455caSZhou Wang return (qm_ret == PCI_ERS_RESULT_NEED_RESET || 95762c455caSZhou Wang zip_ret == PCI_ERS_RESULT_NEED_RESET) ? 95862c455caSZhou Wang PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED; 95962c455caSZhou Wang } 96062c455caSZhou Wang 96162c455caSZhou Wang static pci_ers_result_t hisi_zip_error_detected(struct pci_dev *pdev, 96262c455caSZhou Wang pci_channel_state_t state) 96362c455caSZhou Wang { 96462c455caSZhou Wang if (pdev->is_virtfn) 96562c455caSZhou Wang return PCI_ERS_RESULT_NONE; 96662c455caSZhou Wang 96762c455caSZhou Wang dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state); 96862c455caSZhou Wang if (state == pci_channel_io_perm_failure) 96962c455caSZhou Wang return PCI_ERS_RESULT_DISCONNECT; 97062c455caSZhou Wang 97162c455caSZhou Wang return hisi_zip_process_hw_error(pdev); 97262c455caSZhou Wang } 97362c455caSZhou Wang 97462c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = { 97562c455caSZhou Wang .error_detected = hisi_zip_error_detected, 97662c455caSZhou Wang }; 97762c455caSZhou Wang 97862c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = { 97962c455caSZhou Wang .name = "hisi_zip", 98062c455caSZhou Wang .id_table = hisi_zip_dev_ids, 98162c455caSZhou Wang .probe = hisi_zip_probe, 98262c455caSZhou Wang .remove = hisi_zip_remove, 983bf6a7a5aSArnd Bergmann .sriov_configure = IS_ENABLED(CONFIG_PCI_IOV) ? 984719181f3SShukun Tan hisi_zip_sriov_configure : NULL, 98562c455caSZhou Wang .err_handler = &hisi_zip_err_handler, 98662c455caSZhou Wang }; 98762c455caSZhou Wang 98872c7a68dSZhou Wang static void hisi_zip_register_debugfs(void) 98972c7a68dSZhou Wang { 99072c7a68dSZhou Wang if (!debugfs_initialized()) 99172c7a68dSZhou Wang return; 99272c7a68dSZhou Wang 99372c7a68dSZhou Wang hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL); 99472c7a68dSZhou Wang } 99572c7a68dSZhou Wang 99672c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void) 99772c7a68dSZhou Wang { 99872c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 99972c7a68dSZhou Wang } 100072c7a68dSZhou Wang 100162c455caSZhou Wang static int __init hisi_zip_init(void) 100262c455caSZhou Wang { 100362c455caSZhou Wang int ret; 100462c455caSZhou Wang 100572c7a68dSZhou Wang hisi_zip_register_debugfs(); 100672c7a68dSZhou Wang 100762c455caSZhou Wang ret = pci_register_driver(&hisi_zip_pci_driver); 100862c455caSZhou Wang if (ret < 0) { 100962c455caSZhou Wang pr_err("Failed to register pci driver.\n"); 101072c7a68dSZhou Wang goto err_pci; 101162c455caSZhou Wang } 101262c455caSZhou Wang 101362c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) { 101462c455caSZhou Wang ret = hisi_zip_register_to_crypto(); 101562c455caSZhou Wang if (ret < 0) { 101662c455caSZhou Wang pr_err("Failed to register driver to crypto.\n"); 101762c455caSZhou Wang goto err_crypto; 101862c455caSZhou Wang } 101962c455caSZhou Wang } 102062c455caSZhou Wang 102162c455caSZhou Wang return 0; 102262c455caSZhou Wang 102362c455caSZhou Wang err_crypto: 102462c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 102572c7a68dSZhou Wang err_pci: 102672c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 102772c7a68dSZhou Wang 102862c455caSZhou Wang return ret; 102962c455caSZhou Wang } 103062c455caSZhou Wang 103162c455caSZhou Wang static void __exit hisi_zip_exit(void) 103262c455caSZhou Wang { 103362c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) 103462c455caSZhou Wang hisi_zip_unregister_from_crypto(); 103562c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 103672c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 103762c455caSZhou Wang } 103862c455caSZhou Wang 103962c455caSZhou Wang module_init(hisi_zip_init); 104062c455caSZhou Wang module_exit(hisi_zip_exit); 104162c455caSZhou Wang 104262c455caSZhou Wang MODULE_LICENSE("GPL v2"); 104362c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 104462c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator"); 1045