18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: (BSD-3-Clause OR GPL-2.0-only) 28c2ecf20Sopenharmony_ci/* Copyright(c) 2015 - 2020 Intel Corporation */ 38c2ecf20Sopenharmony_ci#include <linux/workqueue.h> 48c2ecf20Sopenharmony_ci#include <linux/pci.h> 58c2ecf20Sopenharmony_ci#include <linux/device.h> 68c2ecf20Sopenharmony_ci#include <linux/iommu.h> 78c2ecf20Sopenharmony_ci#include "adf_common_drv.h" 88c2ecf20Sopenharmony_ci#include "adf_cfg.h" 98c2ecf20Sopenharmony_ci#include "adf_pf2vf_msg.h" 108c2ecf20Sopenharmony_ci 118c2ecf20Sopenharmony_cistatic struct workqueue_struct *pf2vf_resp_wq; 128c2ecf20Sopenharmony_ci 138c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_A_OFFSET (0x3A400 + 0x190) 148c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_A_NUM_REGS 96 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_B_OFFSET (0x3A400 + 0x310) 178c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_B_NUM_REGS 12 188c2ecf20Sopenharmony_ci 198c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_REG_SIZE 4 208c2ecf20Sopenharmony_ci#define ME2FUNCTION_MAP_VALID BIT(7) 218c2ecf20Sopenharmony_ci 228c2ecf20Sopenharmony_ci#define READ_CSR_ME2FUNCTION_MAP_A(pmisc_bar_addr, index) \ 238c2ecf20Sopenharmony_ci ADF_CSR_RD(pmisc_bar_addr, ME2FUNCTION_MAP_A_OFFSET + \ 248c2ecf20Sopenharmony_ci ME2FUNCTION_MAP_REG_SIZE * index) 258c2ecf20Sopenharmony_ci 268c2ecf20Sopenharmony_ci#define WRITE_CSR_ME2FUNCTION_MAP_A(pmisc_bar_addr, index, value) \ 278c2ecf20Sopenharmony_ci ADF_CSR_WR(pmisc_bar_addr, ME2FUNCTION_MAP_A_OFFSET + \ 288c2ecf20Sopenharmony_ci ME2FUNCTION_MAP_REG_SIZE * index, value) 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci#define READ_CSR_ME2FUNCTION_MAP_B(pmisc_bar_addr, index) \ 318c2ecf20Sopenharmony_ci ADF_CSR_RD(pmisc_bar_addr, ME2FUNCTION_MAP_B_OFFSET + \ 328c2ecf20Sopenharmony_ci ME2FUNCTION_MAP_REG_SIZE * index) 338c2ecf20Sopenharmony_ci 348c2ecf20Sopenharmony_ci#define WRITE_CSR_ME2FUNCTION_MAP_B(pmisc_bar_addr, index, value) \ 358c2ecf20Sopenharmony_ci ADF_CSR_WR(pmisc_bar_addr, ME2FUNCTION_MAP_B_OFFSET + \ 368c2ecf20Sopenharmony_ci ME2FUNCTION_MAP_REG_SIZE * index, value) 378c2ecf20Sopenharmony_ci 388c2ecf20Sopenharmony_cistruct adf_pf2vf_resp { 398c2ecf20Sopenharmony_ci struct work_struct pf2vf_resp_work; 408c2ecf20Sopenharmony_ci struct adf_accel_vf_info *vf_info; 418c2ecf20Sopenharmony_ci}; 428c2ecf20Sopenharmony_ci 438c2ecf20Sopenharmony_cistatic void adf_iov_send_resp(struct work_struct *work) 448c2ecf20Sopenharmony_ci{ 458c2ecf20Sopenharmony_ci struct adf_pf2vf_resp *pf2vf_resp = 468c2ecf20Sopenharmony_ci container_of(work, struct adf_pf2vf_resp, pf2vf_resp_work); 478c2ecf20Sopenharmony_ci 488c2ecf20Sopenharmony_ci adf_vf2pf_req_hndl(pf2vf_resp->vf_info); 498c2ecf20Sopenharmony_ci kfree(pf2vf_resp); 508c2ecf20Sopenharmony_ci} 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_cistatic void adf_vf2pf_bh_handler(void *data) 538c2ecf20Sopenharmony_ci{ 548c2ecf20Sopenharmony_ci struct adf_accel_vf_info *vf_info = (struct adf_accel_vf_info *)data; 558c2ecf20Sopenharmony_ci struct adf_pf2vf_resp *pf2vf_resp; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci pf2vf_resp = kzalloc(sizeof(*pf2vf_resp), GFP_ATOMIC); 588c2ecf20Sopenharmony_ci if (!pf2vf_resp) 598c2ecf20Sopenharmony_ci return; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci pf2vf_resp->vf_info = vf_info; 628c2ecf20Sopenharmony_ci INIT_WORK(&pf2vf_resp->pf2vf_resp_work, adf_iov_send_resp); 638c2ecf20Sopenharmony_ci queue_work(pf2vf_resp_wq, &pf2vf_resp->pf2vf_resp_work); 648c2ecf20Sopenharmony_ci} 658c2ecf20Sopenharmony_ci 668c2ecf20Sopenharmony_cistatic int adf_enable_sriov(struct adf_accel_dev *accel_dev) 678c2ecf20Sopenharmony_ci{ 688c2ecf20Sopenharmony_ci struct pci_dev *pdev = accel_to_pci_dev(accel_dev); 698c2ecf20Sopenharmony_ci int totalvfs = pci_sriov_get_totalvfs(pdev); 708c2ecf20Sopenharmony_ci struct adf_hw_device_data *hw_data = accel_dev->hw_device; 718c2ecf20Sopenharmony_ci struct adf_bar *pmisc = 728c2ecf20Sopenharmony_ci &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)]; 738c2ecf20Sopenharmony_ci void __iomem *pmisc_addr = pmisc->virt_addr; 748c2ecf20Sopenharmony_ci struct adf_accel_vf_info *vf_info; 758c2ecf20Sopenharmony_ci int i; 768c2ecf20Sopenharmony_ci u32 reg; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci for (i = 0, vf_info = accel_dev->pf.vf_info; i < totalvfs; 798c2ecf20Sopenharmony_ci i++, vf_info++) { 808c2ecf20Sopenharmony_ci /* This ptr will be populated when VFs will be created */ 818c2ecf20Sopenharmony_ci vf_info->accel_dev = accel_dev; 828c2ecf20Sopenharmony_ci vf_info->vf_nr = i; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci tasklet_init(&vf_info->vf2pf_bh_tasklet, 858c2ecf20Sopenharmony_ci (void *)adf_vf2pf_bh_handler, 868c2ecf20Sopenharmony_ci (unsigned long)vf_info); 878c2ecf20Sopenharmony_ci mutex_init(&vf_info->pf2vf_lock); 888c2ecf20Sopenharmony_ci ratelimit_state_init(&vf_info->vf2pf_ratelimit, 898c2ecf20Sopenharmony_ci DEFAULT_RATELIMIT_INTERVAL, 908c2ecf20Sopenharmony_ci DEFAULT_RATELIMIT_BURST); 918c2ecf20Sopenharmony_ci } 928c2ecf20Sopenharmony_ci 938c2ecf20Sopenharmony_ci /* Set Valid bits in ME Thread to PCIe Function Mapping Group A */ 948c2ecf20Sopenharmony_ci for (i = 0; i < ME2FUNCTION_MAP_A_NUM_REGS; i++) { 958c2ecf20Sopenharmony_ci reg = READ_CSR_ME2FUNCTION_MAP_A(pmisc_addr, i); 968c2ecf20Sopenharmony_ci reg |= ME2FUNCTION_MAP_VALID; 978c2ecf20Sopenharmony_ci WRITE_CSR_ME2FUNCTION_MAP_A(pmisc_addr, i, reg); 988c2ecf20Sopenharmony_ci } 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci /* Set Valid bits in ME Thread to PCIe Function Mapping Group B */ 1018c2ecf20Sopenharmony_ci for (i = 0; i < ME2FUNCTION_MAP_B_NUM_REGS; i++) { 1028c2ecf20Sopenharmony_ci reg = READ_CSR_ME2FUNCTION_MAP_B(pmisc_addr, i); 1038c2ecf20Sopenharmony_ci reg |= ME2FUNCTION_MAP_VALID; 1048c2ecf20Sopenharmony_ci WRITE_CSR_ME2FUNCTION_MAP_B(pmisc_addr, i, reg); 1058c2ecf20Sopenharmony_ci } 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci /* Enable VF to PF interrupts for all VFs */ 1088c2ecf20Sopenharmony_ci adf_enable_vf2pf_interrupts(accel_dev, GENMASK_ULL(totalvfs - 1, 0)); 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci /* 1118c2ecf20Sopenharmony_ci * Due to the hardware design, when SR-IOV and the ring arbiter 1128c2ecf20Sopenharmony_ci * are enabled all the VFs supported in hardware must be enabled in 1138c2ecf20Sopenharmony_ci * order for all the hardware resources (i.e. bundles) to be usable. 1148c2ecf20Sopenharmony_ci * When SR-IOV is enabled, each of the VFs will own one bundle. 1158c2ecf20Sopenharmony_ci */ 1168c2ecf20Sopenharmony_ci return pci_enable_sriov(pdev, totalvfs); 1178c2ecf20Sopenharmony_ci} 1188c2ecf20Sopenharmony_ci 1198c2ecf20Sopenharmony_ci/** 1208c2ecf20Sopenharmony_ci * adf_disable_sriov() - Disable SRIOV for the device 1218c2ecf20Sopenharmony_ci * @accel_dev: Pointer to accel device. 1228c2ecf20Sopenharmony_ci * 1238c2ecf20Sopenharmony_ci * Function disables SRIOV for the accel device. 1248c2ecf20Sopenharmony_ci * 1258c2ecf20Sopenharmony_ci * Return: 0 on success, error code otherwise. 1268c2ecf20Sopenharmony_ci */ 1278c2ecf20Sopenharmony_civoid adf_disable_sriov(struct adf_accel_dev *accel_dev) 1288c2ecf20Sopenharmony_ci{ 1298c2ecf20Sopenharmony_ci struct adf_hw_device_data *hw_data = accel_dev->hw_device; 1308c2ecf20Sopenharmony_ci struct adf_bar *pmisc = 1318c2ecf20Sopenharmony_ci &GET_BARS(accel_dev)[hw_data->get_misc_bar_id(hw_data)]; 1328c2ecf20Sopenharmony_ci void __iomem *pmisc_addr = pmisc->virt_addr; 1338c2ecf20Sopenharmony_ci int totalvfs = pci_sriov_get_totalvfs(accel_to_pci_dev(accel_dev)); 1348c2ecf20Sopenharmony_ci struct adf_accel_vf_info *vf; 1358c2ecf20Sopenharmony_ci u32 reg; 1368c2ecf20Sopenharmony_ci int i; 1378c2ecf20Sopenharmony_ci 1388c2ecf20Sopenharmony_ci if (!accel_dev->pf.vf_info) 1398c2ecf20Sopenharmony_ci return; 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci adf_pf2vf_notify_restarting(accel_dev); 1428c2ecf20Sopenharmony_ci 1438c2ecf20Sopenharmony_ci pci_disable_sriov(accel_to_pci_dev(accel_dev)); 1448c2ecf20Sopenharmony_ci 1458c2ecf20Sopenharmony_ci /* Disable VF to PF interrupts */ 1468c2ecf20Sopenharmony_ci adf_disable_vf2pf_interrupts(accel_dev, 0xFFFFFFFF); 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci /* Clear Valid bits in ME Thread to PCIe Function Mapping Group A */ 1498c2ecf20Sopenharmony_ci for (i = 0; i < ME2FUNCTION_MAP_A_NUM_REGS; i++) { 1508c2ecf20Sopenharmony_ci reg = READ_CSR_ME2FUNCTION_MAP_A(pmisc_addr, i); 1518c2ecf20Sopenharmony_ci reg &= ~ME2FUNCTION_MAP_VALID; 1528c2ecf20Sopenharmony_ci WRITE_CSR_ME2FUNCTION_MAP_A(pmisc_addr, i, reg); 1538c2ecf20Sopenharmony_ci } 1548c2ecf20Sopenharmony_ci 1558c2ecf20Sopenharmony_ci /* Clear Valid bits in ME Thread to PCIe Function Mapping Group B */ 1568c2ecf20Sopenharmony_ci for (i = 0; i < ME2FUNCTION_MAP_B_NUM_REGS; i++) { 1578c2ecf20Sopenharmony_ci reg = READ_CSR_ME2FUNCTION_MAP_B(pmisc_addr, i); 1588c2ecf20Sopenharmony_ci reg &= ~ME2FUNCTION_MAP_VALID; 1598c2ecf20Sopenharmony_ci WRITE_CSR_ME2FUNCTION_MAP_B(pmisc_addr, i, reg); 1608c2ecf20Sopenharmony_ci } 1618c2ecf20Sopenharmony_ci 1628c2ecf20Sopenharmony_ci for (i = 0, vf = accel_dev->pf.vf_info; i < totalvfs; i++, vf++) { 1638c2ecf20Sopenharmony_ci tasklet_disable(&vf->vf2pf_bh_tasklet); 1648c2ecf20Sopenharmony_ci tasklet_kill(&vf->vf2pf_bh_tasklet); 1658c2ecf20Sopenharmony_ci mutex_destroy(&vf->pf2vf_lock); 1668c2ecf20Sopenharmony_ci } 1678c2ecf20Sopenharmony_ci 1688c2ecf20Sopenharmony_ci kfree(accel_dev->pf.vf_info); 1698c2ecf20Sopenharmony_ci accel_dev->pf.vf_info = NULL; 1708c2ecf20Sopenharmony_ci} 1718c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(adf_disable_sriov); 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci/** 1748c2ecf20Sopenharmony_ci * adf_sriov_configure() - Enable SRIOV for the device 1758c2ecf20Sopenharmony_ci * @pdev: Pointer to pci device. 1768c2ecf20Sopenharmony_ci * @numvfs: Number of virtual functions (VFs) to enable. 1778c2ecf20Sopenharmony_ci * 1788c2ecf20Sopenharmony_ci * Note that the @numvfs parameter is ignored and all VFs supported by the 1798c2ecf20Sopenharmony_ci * device are enabled due to the design of the hardware. 1808c2ecf20Sopenharmony_ci * 1818c2ecf20Sopenharmony_ci * Function enables SRIOV for the pci device. 1828c2ecf20Sopenharmony_ci * 1838c2ecf20Sopenharmony_ci * Return: number of VFs enabled on success, error code otherwise. 1848c2ecf20Sopenharmony_ci */ 1858c2ecf20Sopenharmony_ciint adf_sriov_configure(struct pci_dev *pdev, int numvfs) 1868c2ecf20Sopenharmony_ci{ 1878c2ecf20Sopenharmony_ci struct adf_accel_dev *accel_dev = adf_devmgr_pci_to_accel_dev(pdev); 1888c2ecf20Sopenharmony_ci int totalvfs = pci_sriov_get_totalvfs(pdev); 1898c2ecf20Sopenharmony_ci unsigned long val; 1908c2ecf20Sopenharmony_ci int ret; 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci if (!accel_dev) { 1938c2ecf20Sopenharmony_ci dev_err(&pdev->dev, "Failed to find accel_dev\n"); 1948c2ecf20Sopenharmony_ci return -EFAULT; 1958c2ecf20Sopenharmony_ci } 1968c2ecf20Sopenharmony_ci 1978c2ecf20Sopenharmony_ci if (!iommu_present(&pci_bus_type)) 1988c2ecf20Sopenharmony_ci dev_warn(&pdev->dev, "IOMMU should be enabled for SR-IOV to work correctly\n"); 1998c2ecf20Sopenharmony_ci 2008c2ecf20Sopenharmony_ci if (accel_dev->pf.vf_info) { 2018c2ecf20Sopenharmony_ci dev_info(&pdev->dev, "Already enabled for this device\n"); 2028c2ecf20Sopenharmony_ci return -EINVAL; 2038c2ecf20Sopenharmony_ci } 2048c2ecf20Sopenharmony_ci 2058c2ecf20Sopenharmony_ci if (adf_dev_started(accel_dev)) { 2068c2ecf20Sopenharmony_ci if (adf_devmgr_in_reset(accel_dev) || 2078c2ecf20Sopenharmony_ci adf_dev_in_use(accel_dev)) { 2088c2ecf20Sopenharmony_ci dev_err(&GET_DEV(accel_dev), "Device busy\n"); 2098c2ecf20Sopenharmony_ci return -EBUSY; 2108c2ecf20Sopenharmony_ci } 2118c2ecf20Sopenharmony_ci 2128c2ecf20Sopenharmony_ci adf_dev_stop(accel_dev); 2138c2ecf20Sopenharmony_ci adf_dev_shutdown(accel_dev); 2148c2ecf20Sopenharmony_ci } 2158c2ecf20Sopenharmony_ci 2168c2ecf20Sopenharmony_ci if (adf_cfg_section_add(accel_dev, ADF_KERNEL_SEC)) 2178c2ecf20Sopenharmony_ci return -EFAULT; 2188c2ecf20Sopenharmony_ci val = 0; 2198c2ecf20Sopenharmony_ci if (adf_cfg_add_key_value_param(accel_dev, ADF_KERNEL_SEC, 2208c2ecf20Sopenharmony_ci ADF_NUM_CY, (void *)&val, ADF_DEC)) 2218c2ecf20Sopenharmony_ci return -EFAULT; 2228c2ecf20Sopenharmony_ci 2238c2ecf20Sopenharmony_ci set_bit(ADF_STATUS_CONFIGURED, &accel_dev->status); 2248c2ecf20Sopenharmony_ci 2258c2ecf20Sopenharmony_ci /* Allocate memory for VF info structs */ 2268c2ecf20Sopenharmony_ci accel_dev->pf.vf_info = kcalloc(totalvfs, 2278c2ecf20Sopenharmony_ci sizeof(struct adf_accel_vf_info), 2288c2ecf20Sopenharmony_ci GFP_KERNEL); 2298c2ecf20Sopenharmony_ci if (!accel_dev->pf.vf_info) 2308c2ecf20Sopenharmony_ci return -ENOMEM; 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci if (adf_dev_init(accel_dev)) { 2338c2ecf20Sopenharmony_ci dev_err(&GET_DEV(accel_dev), "Failed to init qat_dev%d\n", 2348c2ecf20Sopenharmony_ci accel_dev->accel_id); 2358c2ecf20Sopenharmony_ci return -EFAULT; 2368c2ecf20Sopenharmony_ci } 2378c2ecf20Sopenharmony_ci 2388c2ecf20Sopenharmony_ci if (adf_dev_start(accel_dev)) { 2398c2ecf20Sopenharmony_ci dev_err(&GET_DEV(accel_dev), "Failed to start qat_dev%d\n", 2408c2ecf20Sopenharmony_ci accel_dev->accel_id); 2418c2ecf20Sopenharmony_ci return -EFAULT; 2428c2ecf20Sopenharmony_ci } 2438c2ecf20Sopenharmony_ci 2448c2ecf20Sopenharmony_ci ret = adf_enable_sriov(accel_dev); 2458c2ecf20Sopenharmony_ci if (ret) 2468c2ecf20Sopenharmony_ci return ret; 2478c2ecf20Sopenharmony_ci 2488c2ecf20Sopenharmony_ci return numvfs; 2498c2ecf20Sopenharmony_ci} 2508c2ecf20Sopenharmony_ciEXPORT_SYMBOL_GPL(adf_sriov_configure); 2518c2ecf20Sopenharmony_ci 2528c2ecf20Sopenharmony_ciint __init adf_init_pf_wq(void) 2538c2ecf20Sopenharmony_ci{ 2548c2ecf20Sopenharmony_ci /* Workqueue for PF2VF responses */ 2558c2ecf20Sopenharmony_ci pf2vf_resp_wq = alloc_workqueue("qat_pf2vf_resp_wq", WQ_MEM_RECLAIM, 0); 2568c2ecf20Sopenharmony_ci 2578c2ecf20Sopenharmony_ci return !pf2vf_resp_wq ? -ENOMEM : 0; 2588c2ecf20Sopenharmony_ci} 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_civoid adf_exit_pf_wq(void) 2618c2ecf20Sopenharmony_ci{ 2628c2ecf20Sopenharmony_ci if (pf2vf_resp_wq) { 2638c2ecf20Sopenharmony_ci destroy_workqueue(pf2vf_resp_wq); 2648c2ecf20Sopenharmony_ci pf2vf_resp_wq = NULL; 2658c2ecf20Sopenharmony_ci } 2668c2ecf20Sopenharmony_ci} 267