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 303*39977f4bSHao Fang static u32 vfs_num; 304*39977f4bSHao Fang module_param(vfs_num, uint, 0444); 305*39977f4bSHao Fang MODULE_PARM_DESC(vfs_num, "Number of VFs to enable(1-63)"); 306*39977f4bSHao 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; 55072c7a68dSZhou Wang struct dentry *tmp_d, *tmp; 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 tmp_d = debugfs_create_dir(buf, ctrl->debug_root); 56172c7a68dSZhou Wang if (!tmp_d) 56272c7a68dSZhou Wang return -ENOENT; 56372c7a68dSZhou Wang 56472c7a68dSZhou Wang regset = devm_kzalloc(dev, sizeof(*regset), GFP_KERNEL); 56572c7a68dSZhou Wang if (!regset) 56672c7a68dSZhou Wang return -ENOENT; 56772c7a68dSZhou Wang 56872c7a68dSZhou Wang regset->regs = hzip_dfx_regs; 56972c7a68dSZhou Wang regset->nregs = ARRAY_SIZE(hzip_dfx_regs); 57072c7a68dSZhou Wang regset->base = qm->io_base + core_offsets[i]; 57172c7a68dSZhou Wang 57272c7a68dSZhou Wang tmp = debugfs_create_regset32("regs", 0444, tmp_d, regset); 57372c7a68dSZhou Wang if (!tmp) 57472c7a68dSZhou Wang return -ENOENT; 57572c7a68dSZhou Wang } 57672c7a68dSZhou Wang 57772c7a68dSZhou Wang return 0; 57872c7a68dSZhou Wang } 57972c7a68dSZhou Wang 58072c7a68dSZhou Wang static int hisi_zip_ctrl_debug_init(struct hisi_zip_ctrl *ctrl) 58172c7a68dSZhou Wang { 58272c7a68dSZhou Wang struct dentry *tmp; 58372c7a68dSZhou Wang int i; 58472c7a68dSZhou Wang 58572c7a68dSZhou Wang for (i = HZIP_CURRENT_QM; i < HZIP_DEBUG_FILE_NUM; i++) { 58672c7a68dSZhou Wang spin_lock_init(&ctrl->files[i].lock); 58772c7a68dSZhou Wang ctrl->files[i].ctrl = ctrl; 58872c7a68dSZhou Wang ctrl->files[i].index = i; 58972c7a68dSZhou Wang 59072c7a68dSZhou Wang tmp = debugfs_create_file(ctrl_debug_file_name[i], 0600, 59172c7a68dSZhou Wang ctrl->debug_root, ctrl->files + i, 59272c7a68dSZhou Wang &ctrl_debug_fops); 59372c7a68dSZhou Wang if (!tmp) 59472c7a68dSZhou Wang return -ENOENT; 59572c7a68dSZhou Wang } 59672c7a68dSZhou Wang 59772c7a68dSZhou Wang return hisi_zip_core_debug_init(ctrl); 59872c7a68dSZhou Wang } 59972c7a68dSZhou Wang 60072c7a68dSZhou Wang static int hisi_zip_debugfs_init(struct hisi_zip *hisi_zip) 60172c7a68dSZhou Wang { 60272c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 60372c7a68dSZhou Wang struct device *dev = &qm->pdev->dev; 60472c7a68dSZhou Wang struct dentry *dev_d; 60572c7a68dSZhou Wang int ret; 60672c7a68dSZhou Wang 60772c7a68dSZhou Wang dev_d = debugfs_create_dir(dev_name(dev), hzip_debugfs_root); 60872c7a68dSZhou Wang if (!dev_d) 60972c7a68dSZhou Wang return -ENOENT; 61072c7a68dSZhou Wang 61172c7a68dSZhou Wang qm->debug.debug_root = dev_d; 61272c7a68dSZhou Wang ret = hisi_qm_debug_init(qm); 61372c7a68dSZhou Wang if (ret) 61472c7a68dSZhou Wang goto failed_to_create; 61572c7a68dSZhou Wang 61672c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) { 61772c7a68dSZhou Wang hisi_zip->ctrl->debug_root = dev_d; 61872c7a68dSZhou Wang ret = hisi_zip_ctrl_debug_init(hisi_zip->ctrl); 61972c7a68dSZhou Wang if (ret) 62072c7a68dSZhou Wang goto failed_to_create; 62172c7a68dSZhou Wang } 62272c7a68dSZhou Wang 62372c7a68dSZhou Wang return 0; 62472c7a68dSZhou Wang 62572c7a68dSZhou Wang failed_to_create: 62672c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 62772c7a68dSZhou Wang return ret; 62872c7a68dSZhou Wang } 62972c7a68dSZhou Wang 63072c7a68dSZhou Wang static void hisi_zip_debug_regs_clear(struct hisi_zip *hisi_zip) 63172c7a68dSZhou Wang { 63272c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 63372c7a68dSZhou Wang 63472c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_MB_CNT_VF); 63572c7a68dSZhou Wang writel(0x0, qm->io_base + QM_DFX_DB_CNT_VF); 63672c7a68dSZhou Wang writel(0x0, qm->io_base + HZIP_SOFT_CTRL_CNT_CLR_CE); 63772c7a68dSZhou Wang 63872c7a68dSZhou Wang hisi_qm_debug_regs_clear(qm); 63972c7a68dSZhou Wang } 64072c7a68dSZhou Wang 64172c7a68dSZhou Wang static void hisi_zip_debugfs_exit(struct hisi_zip *hisi_zip) 64272c7a68dSZhou Wang { 64372c7a68dSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 64472c7a68dSZhou Wang 64572c7a68dSZhou Wang debugfs_remove_recursive(qm->debug.debug_root); 64672c7a68dSZhou Wang 64772c7a68dSZhou Wang if (qm->fun_type == QM_HW_PF) 64872c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 64972c7a68dSZhou Wang } 65072c7a68dSZhou Wang 65162c455caSZhou Wang static void hisi_zip_hw_error_init(struct hisi_zip *hisi_zip) 65262c455caSZhou Wang { 65362c455caSZhou Wang hisi_qm_hw_error_init(&hisi_zip->qm, QM_BASE_CE, 65462c455caSZhou Wang QM_BASE_NFE | QM_ACC_WB_NOT_READY_TIMEOUT, 0, 65562c455caSZhou Wang QM_DB_RANDOM_INVALID); 65662c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, true); 65762c455caSZhou Wang } 65862c455caSZhou Wang 65962c455caSZhou Wang static int hisi_zip_pf_probe_init(struct hisi_zip *hisi_zip) 66062c455caSZhou Wang { 66162c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 66262c455caSZhou Wang struct hisi_zip_ctrl *ctrl; 66362c455caSZhou Wang 66462c455caSZhou Wang ctrl = devm_kzalloc(&qm->pdev->dev, sizeof(*ctrl), GFP_KERNEL); 66562c455caSZhou Wang if (!ctrl) 66662c455caSZhou Wang return -ENOMEM; 66762c455caSZhou Wang 66862c455caSZhou Wang hisi_zip->ctrl = ctrl; 66962c455caSZhou Wang ctrl->hisi_zip = hisi_zip; 67062c455caSZhou Wang 67162c455caSZhou Wang switch (qm->ver) { 67262c455caSZhou Wang case QM_HW_V1: 67362c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V1; 67462c455caSZhou Wang break; 67562c455caSZhou Wang 67662c455caSZhou Wang case QM_HW_V2: 67762c455caSZhou Wang qm->ctrl_qp_num = HZIP_QUEUE_NUM_V2; 67862c455caSZhou Wang break; 67962c455caSZhou Wang 68062c455caSZhou Wang default: 68162c455caSZhou Wang return -EINVAL; 68262c455caSZhou Wang } 68362c455caSZhou Wang 68462c455caSZhou Wang hisi_zip_set_user_domain_and_cache(hisi_zip); 68562c455caSZhou Wang hisi_zip_hw_error_init(hisi_zip); 68672c7a68dSZhou Wang hisi_zip_debug_regs_clear(hisi_zip); 68762c455caSZhou Wang 68862c455caSZhou Wang return 0; 68962c455caSZhou Wang } 69062c455caSZhou Wang 69179e09f30SZhou Wang /* Currently we only support equal assignment */ 69279e09f30SZhou Wang static int hisi_zip_vf_q_assign(struct hisi_zip *hisi_zip, int num_vfs) 69379e09f30SZhou Wang { 69479e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 69579e09f30SZhou Wang u32 qp_num = qm->qp_num; 69679e09f30SZhou Wang u32 q_base = qp_num; 69779e09f30SZhou Wang u32 q_num, remain_q_num, i; 69879e09f30SZhou Wang int ret; 69979e09f30SZhou Wang 70079e09f30SZhou Wang if (!num_vfs) 70179e09f30SZhou Wang return -EINVAL; 70279e09f30SZhou Wang 70379e09f30SZhou Wang remain_q_num = qm->ctrl_qp_num - qp_num; 70479e09f30SZhou Wang if (remain_q_num < num_vfs) 70579e09f30SZhou Wang return -EINVAL; 70679e09f30SZhou Wang 70779e09f30SZhou Wang q_num = remain_q_num / num_vfs; 70879e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 70979e09f30SZhou Wang if (i == num_vfs) 71079e09f30SZhou Wang q_num += remain_q_num % num_vfs; 71179e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, q_base, q_num); 71279e09f30SZhou Wang if (ret) 71379e09f30SZhou Wang return ret; 71479e09f30SZhou Wang q_base += q_num; 71579e09f30SZhou Wang } 71679e09f30SZhou Wang 71779e09f30SZhou Wang return 0; 71879e09f30SZhou Wang } 71979e09f30SZhou Wang 72079e09f30SZhou Wang static int hisi_zip_clear_vft_config(struct hisi_zip *hisi_zip) 72179e09f30SZhou Wang { 72279e09f30SZhou Wang struct hisi_zip_ctrl *ctrl = hisi_zip->ctrl; 72379e09f30SZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 72479e09f30SZhou Wang u32 i, num_vfs = ctrl->num_vfs; 72579e09f30SZhou Wang int ret; 72679e09f30SZhou Wang 72779e09f30SZhou Wang for (i = 1; i <= num_vfs; i++) { 72879e09f30SZhou Wang ret = hisi_qm_set_vft(qm, i, 0, 0); 72979e09f30SZhou Wang if (ret) 73079e09f30SZhou Wang return ret; 73179e09f30SZhou Wang } 73279e09f30SZhou Wang 73379e09f30SZhou Wang ctrl->num_vfs = 0; 73479e09f30SZhou Wang 73579e09f30SZhou Wang return 0; 73679e09f30SZhou Wang } 73779e09f30SZhou Wang 73879e09f30SZhou Wang static int hisi_zip_sriov_enable(struct pci_dev *pdev, int max_vfs) 73979e09f30SZhou Wang { 74079e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 74179e09f30SZhou Wang int pre_existing_vfs, num_vfs, ret; 74279e09f30SZhou Wang 74379e09f30SZhou Wang pre_existing_vfs = pci_num_vf(pdev); 74479e09f30SZhou Wang 74579e09f30SZhou Wang if (pre_existing_vfs) { 74679e09f30SZhou Wang dev_err(&pdev->dev, 74779e09f30SZhou Wang "Can't enable VF. Please disable pre-enabled VFs!\n"); 74879e09f30SZhou Wang return 0; 74979e09f30SZhou Wang } 75079e09f30SZhou Wang 75179e09f30SZhou Wang num_vfs = min_t(int, max_vfs, HZIP_VF_NUM); 75279e09f30SZhou Wang 75379e09f30SZhou Wang ret = hisi_zip_vf_q_assign(hisi_zip, num_vfs); 75479e09f30SZhou Wang if (ret) { 75579e09f30SZhou Wang dev_err(&pdev->dev, "Can't assign queues for VF!\n"); 75679e09f30SZhou Wang return ret; 75779e09f30SZhou Wang } 75879e09f30SZhou Wang 75979e09f30SZhou Wang hisi_zip->ctrl->num_vfs = num_vfs; 76079e09f30SZhou Wang 76179e09f30SZhou Wang ret = pci_enable_sriov(pdev, num_vfs); 76279e09f30SZhou Wang if (ret) { 76379e09f30SZhou Wang dev_err(&pdev->dev, "Can't enable VF!\n"); 76479e09f30SZhou Wang hisi_zip_clear_vft_config(hisi_zip); 76579e09f30SZhou Wang return ret; 76679e09f30SZhou Wang } 76779e09f30SZhou Wang 76879e09f30SZhou Wang return num_vfs; 76979e09f30SZhou Wang } 77079e09f30SZhou Wang 77179e09f30SZhou Wang static int hisi_zip_sriov_disable(struct pci_dev *pdev) 77279e09f30SZhou Wang { 77379e09f30SZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 77479e09f30SZhou Wang 77579e09f30SZhou Wang if (pci_vfs_assigned(pdev)) { 77679e09f30SZhou Wang dev_err(&pdev->dev, 77779e09f30SZhou Wang "Can't disable VFs while VFs are assigned!\n"); 77879e09f30SZhou Wang return -EPERM; 77979e09f30SZhou Wang } 78079e09f30SZhou Wang 78179e09f30SZhou Wang /* remove in hisi_zip_pci_driver will be called to free VF resources */ 78279e09f30SZhou Wang pci_disable_sriov(pdev); 78379e09f30SZhou Wang 78479e09f30SZhou Wang return hisi_zip_clear_vft_config(hisi_zip); 78579e09f30SZhou Wang } 78679e09f30SZhou Wang 787*39977f4bSHao Fang static int hisi_zip_probe(struct pci_dev *pdev, const struct pci_device_id *id) 788*39977f4bSHao Fang { 789*39977f4bSHao Fang struct hisi_zip *hisi_zip; 790*39977f4bSHao Fang enum qm_hw_ver rev_id; 791*39977f4bSHao Fang struct hisi_qm *qm; 792*39977f4bSHao Fang int ret; 793*39977f4bSHao Fang 794*39977f4bSHao Fang rev_id = hisi_qm_get_hw_version(pdev); 795*39977f4bSHao Fang if (rev_id == QM_HW_UNKNOWN) 796*39977f4bSHao Fang return -EINVAL; 797*39977f4bSHao Fang 798*39977f4bSHao Fang hisi_zip = devm_kzalloc(&pdev->dev, sizeof(*hisi_zip), GFP_KERNEL); 799*39977f4bSHao Fang if (!hisi_zip) 800*39977f4bSHao Fang return -ENOMEM; 801*39977f4bSHao Fang pci_set_drvdata(pdev, hisi_zip); 802*39977f4bSHao Fang 803*39977f4bSHao Fang qm = &hisi_zip->qm; 804*39977f4bSHao Fang qm->pdev = pdev; 805*39977f4bSHao Fang qm->ver = rev_id; 806*39977f4bSHao Fang 807*39977f4bSHao Fang qm->sqe_size = HZIP_SQE_SIZE; 808*39977f4bSHao Fang qm->dev_name = hisi_zip_name; 809*39977f4bSHao Fang qm->fun_type = (pdev->device == PCI_DEVICE_ID_ZIP_PF) ? QM_HW_PF : 810*39977f4bSHao Fang QM_HW_VF; 811*39977f4bSHao Fang switch (uacce_mode) { 812*39977f4bSHao Fang case 0: 813*39977f4bSHao Fang qm->use_dma_api = true; 814*39977f4bSHao Fang break; 815*39977f4bSHao Fang case 1: 816*39977f4bSHao Fang qm->use_dma_api = false; 817*39977f4bSHao Fang break; 818*39977f4bSHao Fang case 2: 819*39977f4bSHao Fang qm->use_dma_api = true; 820*39977f4bSHao Fang break; 821*39977f4bSHao Fang default: 822*39977f4bSHao Fang return -EINVAL; 823*39977f4bSHao Fang } 824*39977f4bSHao Fang 825*39977f4bSHao Fang ret = hisi_qm_init(qm); 826*39977f4bSHao Fang if (ret) { 827*39977f4bSHao Fang dev_err(&pdev->dev, "Failed to init qm!\n"); 828*39977f4bSHao Fang return ret; 829*39977f4bSHao Fang } 830*39977f4bSHao Fang 831*39977f4bSHao Fang if (qm->fun_type == QM_HW_PF) { 832*39977f4bSHao Fang ret = hisi_zip_pf_probe_init(hisi_zip); 833*39977f4bSHao Fang if (ret) 834*39977f4bSHao Fang return ret; 835*39977f4bSHao Fang 836*39977f4bSHao Fang qm->qp_base = HZIP_PF_DEF_Q_BASE; 837*39977f4bSHao Fang qm->qp_num = pf_q_num; 838*39977f4bSHao Fang } else if (qm->fun_type == QM_HW_VF) { 839*39977f4bSHao Fang /* 840*39977f4bSHao Fang * have no way to get qm configure in VM in v1 hardware, 841*39977f4bSHao Fang * so currently force PF to uses HZIP_PF_DEF_Q_NUM, and force 842*39977f4bSHao Fang * to trigger only one VF in v1 hardware. 843*39977f4bSHao Fang * 844*39977f4bSHao Fang * v2 hardware has no such problem. 845*39977f4bSHao Fang */ 846*39977f4bSHao Fang if (qm->ver == QM_HW_V1) { 847*39977f4bSHao Fang qm->qp_base = HZIP_PF_DEF_Q_NUM; 848*39977f4bSHao Fang qm->qp_num = HZIP_QUEUE_NUM_V1 - HZIP_PF_DEF_Q_NUM; 849*39977f4bSHao Fang } else if (qm->ver == QM_HW_V2) 850*39977f4bSHao Fang /* v2 starts to support get vft by mailbox */ 851*39977f4bSHao Fang hisi_qm_get_vft(qm, &qm->qp_base, &qm->qp_num); 852*39977f4bSHao Fang } 853*39977f4bSHao Fang 854*39977f4bSHao Fang ret = hisi_qm_start(qm); 855*39977f4bSHao Fang if (ret) 856*39977f4bSHao Fang goto err_qm_uninit; 857*39977f4bSHao Fang 858*39977f4bSHao Fang ret = hisi_zip_debugfs_init(hisi_zip); 859*39977f4bSHao Fang if (ret) 860*39977f4bSHao Fang dev_err(&pdev->dev, "Failed to init debugfs (%d)!\n", ret); 861*39977f4bSHao Fang 862*39977f4bSHao Fang hisi_zip_add_to_list(hisi_zip); 863*39977f4bSHao Fang 864*39977f4bSHao Fang if (qm->fun_type == QM_HW_PF && vfs_num > 0) { 865*39977f4bSHao Fang ret = hisi_zip_sriov_enable(pdev, vfs_num); 866*39977f4bSHao Fang if (ret < 0) 867*39977f4bSHao Fang goto err_remove_from_list; 868*39977f4bSHao Fang } 869*39977f4bSHao Fang 870*39977f4bSHao Fang return 0; 871*39977f4bSHao Fang 872*39977f4bSHao Fang err_remove_from_list: 873*39977f4bSHao Fang hisi_zip_remove_from_list(hisi_zip); 874*39977f4bSHao Fang hisi_zip_debugfs_exit(hisi_zip); 875*39977f4bSHao Fang hisi_qm_stop(qm); 876*39977f4bSHao Fang err_qm_uninit: 877*39977f4bSHao Fang hisi_qm_uninit(qm); 878*39977f4bSHao Fang return ret; 879*39977f4bSHao Fang } 880*39977f4bSHao Fang 88179e09f30SZhou Wang static int hisi_zip_sriov_configure(struct pci_dev *pdev, int num_vfs) 88279e09f30SZhou Wang { 88379e09f30SZhou Wang if (num_vfs == 0) 88479e09f30SZhou Wang return hisi_zip_sriov_disable(pdev); 88579e09f30SZhou Wang else 88679e09f30SZhou Wang return hisi_zip_sriov_enable(pdev, num_vfs); 88779e09f30SZhou Wang } 88879e09f30SZhou Wang 88962c455caSZhou Wang static void hisi_zip_remove(struct pci_dev *pdev) 89062c455caSZhou Wang { 89162c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 89262c455caSZhou Wang struct hisi_qm *qm = &hisi_zip->qm; 89362c455caSZhou Wang 89479e09f30SZhou Wang if (qm->fun_type == QM_HW_PF && hisi_zip->ctrl->num_vfs != 0) 89579e09f30SZhou Wang hisi_zip_sriov_disable(pdev); 89679e09f30SZhou Wang 89772c7a68dSZhou Wang hisi_zip_debugfs_exit(hisi_zip); 89862c455caSZhou Wang hisi_qm_stop(qm); 89979e09f30SZhou Wang 90079e09f30SZhou Wang if (qm->fun_type == QM_HW_PF) 90162c455caSZhou Wang hisi_zip_hw_error_set_state(hisi_zip, false); 90279e09f30SZhou Wang 90362c455caSZhou Wang hisi_qm_uninit(qm); 90462c455caSZhou Wang hisi_zip_remove_from_list(hisi_zip); 90562c455caSZhou Wang } 90662c455caSZhou Wang 90762c455caSZhou Wang static void hisi_zip_log_hw_error(struct hisi_zip *hisi_zip, u32 err_sts) 90862c455caSZhou Wang { 90962c455caSZhou Wang const struct hisi_zip_hw_error *err = zip_hw_error; 91062c455caSZhou Wang struct device *dev = &hisi_zip->qm.pdev->dev; 91162c455caSZhou Wang u32 err_val; 91262c455caSZhou Wang 91362c455caSZhou Wang while (err->msg) { 91462c455caSZhou Wang if (err->int_msk & err_sts) { 91562c455caSZhou Wang dev_warn(dev, "%s [error status=0x%x] found\n", 91662c455caSZhou Wang err->msg, err->int_msk); 91762c455caSZhou Wang 91862c455caSZhou Wang if (HZIP_CORE_INT_STATUS_M_ECC & err->int_msk) { 91962c455caSZhou Wang err_val = readl(hisi_zip->qm.io_base + 92062c455caSZhou Wang HZIP_CORE_SRAM_ECC_ERR_INFO); 92162c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram num=0x%x\n", 92262c455caSZhou Wang ((err_val >> SRAM_ECC_ERR_NUM_SHIFT) & 92362c455caSZhou Wang 0xFF)); 92462c455caSZhou Wang dev_warn(dev, "hisi-zip multi ecc sram addr=0x%x\n", 92562c455caSZhou Wang (err_val >> SRAM_ECC_ERR_ADDR_SHIFT)); 92662c455caSZhou Wang } 92762c455caSZhou Wang } 92862c455caSZhou Wang err++; 92962c455caSZhou Wang } 93062c455caSZhou Wang } 93162c455caSZhou Wang 93262c455caSZhou Wang static pci_ers_result_t hisi_zip_hw_error_handle(struct hisi_zip *hisi_zip) 93362c455caSZhou Wang { 93462c455caSZhou Wang u32 err_sts; 93562c455caSZhou Wang 93662c455caSZhou Wang /* read err sts */ 93762c455caSZhou Wang err_sts = readl(hisi_zip->qm.io_base + HZIP_CORE_INT_STATUS); 93862c455caSZhou Wang 93962c455caSZhou Wang if (err_sts) { 94062c455caSZhou Wang hisi_zip_log_hw_error(hisi_zip, err_sts); 94162c455caSZhou Wang /* clear error interrupts */ 94262c455caSZhou Wang writel(err_sts, hisi_zip->qm.io_base + HZIP_CORE_INT_SOURCE); 94362c455caSZhou Wang 94462c455caSZhou Wang return PCI_ERS_RESULT_NEED_RESET; 94562c455caSZhou Wang } 94662c455caSZhou Wang 94762c455caSZhou Wang return PCI_ERS_RESULT_RECOVERED; 94862c455caSZhou Wang } 94962c455caSZhou Wang 95062c455caSZhou Wang static pci_ers_result_t hisi_zip_process_hw_error(struct pci_dev *pdev) 95162c455caSZhou Wang { 95262c455caSZhou Wang struct hisi_zip *hisi_zip = pci_get_drvdata(pdev); 95362c455caSZhou Wang struct device *dev = &pdev->dev; 95462c455caSZhou Wang pci_ers_result_t qm_ret, zip_ret; 95562c455caSZhou Wang 95662c455caSZhou Wang if (!hisi_zip) { 95762c455caSZhou Wang dev_err(dev, 95862c455caSZhou Wang "Can't recover ZIP-error occurred during device init\n"); 95962c455caSZhou Wang return PCI_ERS_RESULT_NONE; 96062c455caSZhou Wang } 96162c455caSZhou Wang 96262c455caSZhou Wang qm_ret = hisi_qm_hw_error_handle(&hisi_zip->qm); 96362c455caSZhou Wang 96462c455caSZhou Wang zip_ret = hisi_zip_hw_error_handle(hisi_zip); 96562c455caSZhou Wang 96662c455caSZhou Wang return (qm_ret == PCI_ERS_RESULT_NEED_RESET || 96762c455caSZhou Wang zip_ret == PCI_ERS_RESULT_NEED_RESET) ? 96862c455caSZhou Wang PCI_ERS_RESULT_NEED_RESET : PCI_ERS_RESULT_RECOVERED; 96962c455caSZhou Wang } 97062c455caSZhou Wang 97162c455caSZhou Wang static pci_ers_result_t hisi_zip_error_detected(struct pci_dev *pdev, 97262c455caSZhou Wang pci_channel_state_t state) 97362c455caSZhou Wang { 97462c455caSZhou Wang if (pdev->is_virtfn) 97562c455caSZhou Wang return PCI_ERS_RESULT_NONE; 97662c455caSZhou Wang 97762c455caSZhou Wang dev_info(&pdev->dev, "PCI error detected, state(=%d)!!\n", state); 97862c455caSZhou Wang if (state == pci_channel_io_perm_failure) 97962c455caSZhou Wang return PCI_ERS_RESULT_DISCONNECT; 98062c455caSZhou Wang 98162c455caSZhou Wang return hisi_zip_process_hw_error(pdev); 98262c455caSZhou Wang } 98362c455caSZhou Wang 98462c455caSZhou Wang static const struct pci_error_handlers hisi_zip_err_handler = { 98562c455caSZhou Wang .error_detected = hisi_zip_error_detected, 98662c455caSZhou Wang }; 98762c455caSZhou Wang 98862c455caSZhou Wang static struct pci_driver hisi_zip_pci_driver = { 98962c455caSZhou Wang .name = "hisi_zip", 99062c455caSZhou Wang .id_table = hisi_zip_dev_ids, 99162c455caSZhou Wang .probe = hisi_zip_probe, 99262c455caSZhou Wang .remove = hisi_zip_remove, 993bf6a7a5aSArnd Bergmann .sriov_configure = IS_ENABLED(CONFIG_PCI_IOV) ? 994719181f3SShukun Tan hisi_zip_sriov_configure : NULL, 99562c455caSZhou Wang .err_handler = &hisi_zip_err_handler, 99662c455caSZhou Wang }; 99762c455caSZhou Wang 99872c7a68dSZhou Wang static void hisi_zip_register_debugfs(void) 99972c7a68dSZhou Wang { 100072c7a68dSZhou Wang if (!debugfs_initialized()) 100172c7a68dSZhou Wang return; 100272c7a68dSZhou Wang 100372c7a68dSZhou Wang hzip_debugfs_root = debugfs_create_dir("hisi_zip", NULL); 100472c7a68dSZhou Wang if (IS_ERR_OR_NULL(hzip_debugfs_root)) 100572c7a68dSZhou Wang hzip_debugfs_root = NULL; 100672c7a68dSZhou Wang } 100772c7a68dSZhou Wang 100872c7a68dSZhou Wang static void hisi_zip_unregister_debugfs(void) 100972c7a68dSZhou Wang { 101072c7a68dSZhou Wang debugfs_remove_recursive(hzip_debugfs_root); 101172c7a68dSZhou Wang } 101272c7a68dSZhou Wang 101362c455caSZhou Wang static int __init hisi_zip_init(void) 101462c455caSZhou Wang { 101562c455caSZhou Wang int ret; 101662c455caSZhou Wang 101772c7a68dSZhou Wang hisi_zip_register_debugfs(); 101872c7a68dSZhou Wang 101962c455caSZhou Wang ret = pci_register_driver(&hisi_zip_pci_driver); 102062c455caSZhou Wang if (ret < 0) { 102162c455caSZhou Wang pr_err("Failed to register pci driver.\n"); 102272c7a68dSZhou Wang goto err_pci; 102362c455caSZhou Wang } 102462c455caSZhou Wang 102562c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) { 102662c455caSZhou Wang ret = hisi_zip_register_to_crypto(); 102762c455caSZhou Wang if (ret < 0) { 102862c455caSZhou Wang pr_err("Failed to register driver to crypto.\n"); 102962c455caSZhou Wang goto err_crypto; 103062c455caSZhou Wang } 103162c455caSZhou Wang } 103262c455caSZhou Wang 103362c455caSZhou Wang return 0; 103462c455caSZhou Wang 103562c455caSZhou Wang err_crypto: 103662c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 103772c7a68dSZhou Wang err_pci: 103872c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 103972c7a68dSZhou Wang 104062c455caSZhou Wang return ret; 104162c455caSZhou Wang } 104262c455caSZhou Wang 104362c455caSZhou Wang static void __exit hisi_zip_exit(void) 104462c455caSZhou Wang { 104562c455caSZhou Wang if (uacce_mode == 0 || uacce_mode == 2) 104662c455caSZhou Wang hisi_zip_unregister_from_crypto(); 104762c455caSZhou Wang pci_unregister_driver(&hisi_zip_pci_driver); 104872c7a68dSZhou Wang hisi_zip_unregister_debugfs(); 104962c455caSZhou Wang } 105062c455caSZhou Wang 105162c455caSZhou Wang module_init(hisi_zip_init); 105262c455caSZhou Wang module_exit(hisi_zip_exit); 105362c455caSZhou Wang 105462c455caSZhou Wang MODULE_LICENSE("GPL v2"); 105562c455caSZhou Wang MODULE_AUTHOR("Zhou Wang <wangzhou1@hisilicon.com>"); 105662c455caSZhou Wang MODULE_DESCRIPTION("Driver for HiSilicon ZIP accelerator"); 1057