1*af275ec6SWu Hao // SPDX-License-Identifier: GPL-2.0 2*af275ec6SWu Hao /* 3*af275ec6SWu Hao * FPGA Manager Driver for FPGA Management Engine (FME) 4*af275ec6SWu Hao * 5*af275ec6SWu Hao * Copyright (C) 2017-2018 Intel Corporation, Inc. 6*af275ec6SWu Hao * 7*af275ec6SWu Hao * Authors: 8*af275ec6SWu Hao * Kang Luwei <luwei.kang@intel.com> 9*af275ec6SWu Hao * Xiao Guangrong <guangrong.xiao@linux.intel.com> 10*af275ec6SWu Hao * Wu Hao <hao.wu@intel.com> 11*af275ec6SWu Hao * Joseph Grecco <joe.grecco@intel.com> 12*af275ec6SWu Hao * Enno Luebbers <enno.luebbers@intel.com> 13*af275ec6SWu Hao * Tim Whisonant <tim.whisonant@intel.com> 14*af275ec6SWu Hao * Ananda Ravuri <ananda.ravuri@intel.com> 15*af275ec6SWu Hao * Christopher Rauer <christopher.rauer@intel.com> 16*af275ec6SWu Hao * Henry Mitchel <henry.mitchel@intel.com> 17*af275ec6SWu Hao */ 18*af275ec6SWu Hao 19*af275ec6SWu Hao #include <linux/bitfield.h> 20*af275ec6SWu Hao #include <linux/module.h> 21*af275ec6SWu Hao #include <linux/iopoll.h> 22*af275ec6SWu Hao #include <linux/io-64-nonatomic-lo-hi.h> 23*af275ec6SWu Hao #include <linux/fpga/fpga-mgr.h> 24*af275ec6SWu Hao 25*af275ec6SWu Hao #include "dfl-fme-pr.h" 26*af275ec6SWu Hao 27*af275ec6SWu Hao /* FME Partial Reconfiguration Sub Feature Register Set */ 28*af275ec6SWu Hao #define FME_PR_DFH 0x0 29*af275ec6SWu Hao #define FME_PR_CTRL 0x8 30*af275ec6SWu Hao #define FME_PR_STS 0x10 31*af275ec6SWu Hao #define FME_PR_DATA 0x18 32*af275ec6SWu Hao #define FME_PR_ERR 0x20 33*af275ec6SWu Hao #define FME_PR_INTFC_ID_H 0xA8 34*af275ec6SWu Hao #define FME_PR_INTFC_ID_L 0xB0 35*af275ec6SWu Hao 36*af275ec6SWu Hao /* FME PR Control Register Bitfield */ 37*af275ec6SWu Hao #define FME_PR_CTRL_PR_RST BIT_ULL(0) /* Reset PR engine */ 38*af275ec6SWu Hao #define FME_PR_CTRL_PR_RSTACK BIT_ULL(4) /* Ack for PR engine reset */ 39*af275ec6SWu Hao #define FME_PR_CTRL_PR_RGN_ID GENMASK_ULL(9, 7) /* PR Region ID */ 40*af275ec6SWu Hao #define FME_PR_CTRL_PR_START BIT_ULL(12) /* Start to request PR service */ 41*af275ec6SWu Hao #define FME_PR_CTRL_PR_COMPLETE BIT_ULL(13) /* PR data push completion */ 42*af275ec6SWu Hao 43*af275ec6SWu Hao /* FME PR Status Register Bitfield */ 44*af275ec6SWu Hao /* Number of available entries in HW queue inside the PR engine. */ 45*af275ec6SWu Hao #define FME_PR_STS_PR_CREDIT GENMASK_ULL(8, 0) 46*af275ec6SWu Hao #define FME_PR_STS_PR_STS BIT_ULL(16) /* PR operation status */ 47*af275ec6SWu Hao #define FME_PR_STS_PR_STS_IDLE 0 48*af275ec6SWu Hao #define FME_PR_STS_PR_CTRLR_STS GENMASK_ULL(22, 20) /* Controller status */ 49*af275ec6SWu Hao #define FME_PR_STS_PR_HOST_STS GENMASK_ULL(27, 24) /* PR host status */ 50*af275ec6SWu Hao 51*af275ec6SWu Hao /* FME PR Data Register Bitfield */ 52*af275ec6SWu Hao /* PR data from the raw-binary file. */ 53*af275ec6SWu Hao #define FME_PR_DATA_PR_DATA_RAW GENMASK_ULL(32, 0) 54*af275ec6SWu Hao 55*af275ec6SWu Hao /* FME PR Error Register */ 56*af275ec6SWu Hao /* PR Operation errors detected. */ 57*af275ec6SWu Hao #define FME_PR_ERR_OPERATION_ERR BIT_ULL(0) 58*af275ec6SWu Hao /* CRC error detected. */ 59*af275ec6SWu Hao #define FME_PR_ERR_CRC_ERR BIT_ULL(1) 60*af275ec6SWu Hao /* Incompatible PR bitstream detected. */ 61*af275ec6SWu Hao #define FME_PR_ERR_INCOMPATIBLE_BS BIT_ULL(2) 62*af275ec6SWu Hao /* PR data push protocol violated. */ 63*af275ec6SWu Hao #define FME_PR_ERR_PROTOCOL_ERR BIT_ULL(3) 64*af275ec6SWu Hao /* PR data fifo overflow error detected */ 65*af275ec6SWu Hao #define FME_PR_ERR_FIFO_OVERFLOW BIT_ULL(4) 66*af275ec6SWu Hao 67*af275ec6SWu Hao #define PR_WAIT_TIMEOUT 8000000 68*af275ec6SWu Hao #define PR_HOST_STATUS_IDLE 0 69*af275ec6SWu Hao 70*af275ec6SWu Hao struct fme_mgr_priv { 71*af275ec6SWu Hao void __iomem *ioaddr; 72*af275ec6SWu Hao u64 pr_error; 73*af275ec6SWu Hao }; 74*af275ec6SWu Hao 75*af275ec6SWu Hao static u64 pr_error_to_mgr_status(u64 err) 76*af275ec6SWu Hao { 77*af275ec6SWu Hao u64 status = 0; 78*af275ec6SWu Hao 79*af275ec6SWu Hao if (err & FME_PR_ERR_OPERATION_ERR) 80*af275ec6SWu Hao status |= FPGA_MGR_STATUS_OPERATION_ERR; 81*af275ec6SWu Hao if (err & FME_PR_ERR_CRC_ERR) 82*af275ec6SWu Hao status |= FPGA_MGR_STATUS_CRC_ERR; 83*af275ec6SWu Hao if (err & FME_PR_ERR_INCOMPATIBLE_BS) 84*af275ec6SWu Hao status |= FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR; 85*af275ec6SWu Hao if (err & FME_PR_ERR_PROTOCOL_ERR) 86*af275ec6SWu Hao status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR; 87*af275ec6SWu Hao if (err & FME_PR_ERR_FIFO_OVERFLOW) 88*af275ec6SWu Hao status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR; 89*af275ec6SWu Hao 90*af275ec6SWu Hao return status; 91*af275ec6SWu Hao } 92*af275ec6SWu Hao 93*af275ec6SWu Hao static u64 fme_mgr_pr_error_handle(void __iomem *fme_pr) 94*af275ec6SWu Hao { 95*af275ec6SWu Hao u64 pr_status, pr_error; 96*af275ec6SWu Hao 97*af275ec6SWu Hao pr_status = readq(fme_pr + FME_PR_STS); 98*af275ec6SWu Hao if (!(pr_status & FME_PR_STS_PR_STS)) 99*af275ec6SWu Hao return 0; 100*af275ec6SWu Hao 101*af275ec6SWu Hao pr_error = readq(fme_pr + FME_PR_ERR); 102*af275ec6SWu Hao writeq(pr_error, fme_pr + FME_PR_ERR); 103*af275ec6SWu Hao 104*af275ec6SWu Hao return pr_error; 105*af275ec6SWu Hao } 106*af275ec6SWu Hao 107*af275ec6SWu Hao static int fme_mgr_write_init(struct fpga_manager *mgr, 108*af275ec6SWu Hao struct fpga_image_info *info, 109*af275ec6SWu Hao const char *buf, size_t count) 110*af275ec6SWu Hao { 111*af275ec6SWu Hao struct device *dev = &mgr->dev; 112*af275ec6SWu Hao struct fme_mgr_priv *priv = mgr->priv; 113*af275ec6SWu Hao void __iomem *fme_pr = priv->ioaddr; 114*af275ec6SWu Hao u64 pr_ctrl, pr_status; 115*af275ec6SWu Hao 116*af275ec6SWu Hao if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) { 117*af275ec6SWu Hao dev_err(dev, "only supports partial reconfiguration.\n"); 118*af275ec6SWu Hao return -EINVAL; 119*af275ec6SWu Hao } 120*af275ec6SWu Hao 121*af275ec6SWu Hao dev_dbg(dev, "resetting PR before initiated PR\n"); 122*af275ec6SWu Hao 123*af275ec6SWu Hao pr_ctrl = readq(fme_pr + FME_PR_CTRL); 124*af275ec6SWu Hao pr_ctrl |= FME_PR_CTRL_PR_RST; 125*af275ec6SWu Hao writeq(pr_ctrl, fme_pr + FME_PR_CTRL); 126*af275ec6SWu Hao 127*af275ec6SWu Hao if (readq_poll_timeout(fme_pr + FME_PR_CTRL, pr_ctrl, 128*af275ec6SWu Hao pr_ctrl & FME_PR_CTRL_PR_RSTACK, 1, 129*af275ec6SWu Hao PR_WAIT_TIMEOUT)) { 130*af275ec6SWu Hao dev_err(dev, "PR Reset ACK timeout\n"); 131*af275ec6SWu Hao return -ETIMEDOUT; 132*af275ec6SWu Hao } 133*af275ec6SWu Hao 134*af275ec6SWu Hao pr_ctrl = readq(fme_pr + FME_PR_CTRL); 135*af275ec6SWu Hao pr_ctrl &= ~FME_PR_CTRL_PR_RST; 136*af275ec6SWu Hao writeq(pr_ctrl, fme_pr + FME_PR_CTRL); 137*af275ec6SWu Hao 138*af275ec6SWu Hao dev_dbg(dev, 139*af275ec6SWu Hao "waiting for PR resource in HW to be initialized and ready\n"); 140*af275ec6SWu Hao 141*af275ec6SWu Hao if (readq_poll_timeout(fme_pr + FME_PR_STS, pr_status, 142*af275ec6SWu Hao (pr_status & FME_PR_STS_PR_STS) == 143*af275ec6SWu Hao FME_PR_STS_PR_STS_IDLE, 1, PR_WAIT_TIMEOUT)) { 144*af275ec6SWu Hao dev_err(dev, "PR Status timeout\n"); 145*af275ec6SWu Hao priv->pr_error = fme_mgr_pr_error_handle(fme_pr); 146*af275ec6SWu Hao return -ETIMEDOUT; 147*af275ec6SWu Hao } 148*af275ec6SWu Hao 149*af275ec6SWu Hao dev_dbg(dev, "check and clear previous PR error\n"); 150*af275ec6SWu Hao priv->pr_error = fme_mgr_pr_error_handle(fme_pr); 151*af275ec6SWu Hao if (priv->pr_error) 152*af275ec6SWu Hao dev_dbg(dev, "previous PR error detected %llx\n", 153*af275ec6SWu Hao (unsigned long long)priv->pr_error); 154*af275ec6SWu Hao 155*af275ec6SWu Hao dev_dbg(dev, "set PR port ID\n"); 156*af275ec6SWu Hao 157*af275ec6SWu Hao pr_ctrl = readq(fme_pr + FME_PR_CTRL); 158*af275ec6SWu Hao pr_ctrl &= ~FME_PR_CTRL_PR_RGN_ID; 159*af275ec6SWu Hao pr_ctrl |= FIELD_PREP(FME_PR_CTRL_PR_RGN_ID, info->region_id); 160*af275ec6SWu Hao writeq(pr_ctrl, fme_pr + FME_PR_CTRL); 161*af275ec6SWu Hao 162*af275ec6SWu Hao return 0; 163*af275ec6SWu Hao } 164*af275ec6SWu Hao 165*af275ec6SWu Hao static int fme_mgr_write(struct fpga_manager *mgr, 166*af275ec6SWu Hao const char *buf, size_t count) 167*af275ec6SWu Hao { 168*af275ec6SWu Hao struct device *dev = &mgr->dev; 169*af275ec6SWu Hao struct fme_mgr_priv *priv = mgr->priv; 170*af275ec6SWu Hao void __iomem *fme_pr = priv->ioaddr; 171*af275ec6SWu Hao u64 pr_ctrl, pr_status, pr_data; 172*af275ec6SWu Hao int delay = 0, pr_credit, i = 0; 173*af275ec6SWu Hao 174*af275ec6SWu Hao dev_dbg(dev, "start request\n"); 175*af275ec6SWu Hao 176*af275ec6SWu Hao pr_ctrl = readq(fme_pr + FME_PR_CTRL); 177*af275ec6SWu Hao pr_ctrl |= FME_PR_CTRL_PR_START; 178*af275ec6SWu Hao writeq(pr_ctrl, fme_pr + FME_PR_CTRL); 179*af275ec6SWu Hao 180*af275ec6SWu Hao dev_dbg(dev, "pushing data from bitstream to HW\n"); 181*af275ec6SWu Hao 182*af275ec6SWu Hao /* 183*af275ec6SWu Hao * driver can push data to PR hardware using PR_DATA register once HW 184*af275ec6SWu Hao * has enough pr_credit (> 1), pr_credit reduces one for every 32bit 185*af275ec6SWu Hao * pr data write to PR_DATA register. If pr_credit <= 1, driver needs 186*af275ec6SWu Hao * to wait for enough pr_credit from hardware by polling. 187*af275ec6SWu Hao */ 188*af275ec6SWu Hao pr_status = readq(fme_pr + FME_PR_STS); 189*af275ec6SWu Hao pr_credit = FIELD_GET(FME_PR_STS_PR_CREDIT, pr_status); 190*af275ec6SWu Hao 191*af275ec6SWu Hao while (count > 0) { 192*af275ec6SWu Hao while (pr_credit <= 1) { 193*af275ec6SWu Hao if (delay++ > PR_WAIT_TIMEOUT) { 194*af275ec6SWu Hao dev_err(dev, "PR_CREDIT timeout\n"); 195*af275ec6SWu Hao return -ETIMEDOUT; 196*af275ec6SWu Hao } 197*af275ec6SWu Hao udelay(1); 198*af275ec6SWu Hao 199*af275ec6SWu Hao pr_status = readq(fme_pr + FME_PR_STS); 200*af275ec6SWu Hao pr_credit = FIELD_GET(FME_PR_STS_PR_CREDIT, pr_status); 201*af275ec6SWu Hao } 202*af275ec6SWu Hao 203*af275ec6SWu Hao if (count < 4) { 204*af275ec6SWu Hao dev_err(dev, "Invaild PR bitstream size\n"); 205*af275ec6SWu Hao return -EINVAL; 206*af275ec6SWu Hao } 207*af275ec6SWu Hao 208*af275ec6SWu Hao pr_data = 0; 209*af275ec6SWu Hao pr_data |= FIELD_PREP(FME_PR_DATA_PR_DATA_RAW, 210*af275ec6SWu Hao *(((u32 *)buf) + i)); 211*af275ec6SWu Hao writeq(pr_data, fme_pr + FME_PR_DATA); 212*af275ec6SWu Hao count -= 4; 213*af275ec6SWu Hao pr_credit--; 214*af275ec6SWu Hao i++; 215*af275ec6SWu Hao } 216*af275ec6SWu Hao 217*af275ec6SWu Hao return 0; 218*af275ec6SWu Hao } 219*af275ec6SWu Hao 220*af275ec6SWu Hao static int fme_mgr_write_complete(struct fpga_manager *mgr, 221*af275ec6SWu Hao struct fpga_image_info *info) 222*af275ec6SWu Hao { 223*af275ec6SWu Hao struct device *dev = &mgr->dev; 224*af275ec6SWu Hao struct fme_mgr_priv *priv = mgr->priv; 225*af275ec6SWu Hao void __iomem *fme_pr = priv->ioaddr; 226*af275ec6SWu Hao u64 pr_ctrl; 227*af275ec6SWu Hao 228*af275ec6SWu Hao pr_ctrl = readq(fme_pr + FME_PR_CTRL); 229*af275ec6SWu Hao pr_ctrl |= FME_PR_CTRL_PR_COMPLETE; 230*af275ec6SWu Hao writeq(pr_ctrl, fme_pr + FME_PR_CTRL); 231*af275ec6SWu Hao 232*af275ec6SWu Hao dev_dbg(dev, "green bitstream push complete\n"); 233*af275ec6SWu Hao dev_dbg(dev, "waiting for HW to release PR resource\n"); 234*af275ec6SWu Hao 235*af275ec6SWu Hao if (readq_poll_timeout(fme_pr + FME_PR_CTRL, pr_ctrl, 236*af275ec6SWu Hao !(pr_ctrl & FME_PR_CTRL_PR_START), 1, 237*af275ec6SWu Hao PR_WAIT_TIMEOUT)) { 238*af275ec6SWu Hao dev_err(dev, "PR Completion ACK timeout.\n"); 239*af275ec6SWu Hao return -ETIMEDOUT; 240*af275ec6SWu Hao } 241*af275ec6SWu Hao 242*af275ec6SWu Hao dev_dbg(dev, "PR operation complete, checking status\n"); 243*af275ec6SWu Hao priv->pr_error = fme_mgr_pr_error_handle(fme_pr); 244*af275ec6SWu Hao if (priv->pr_error) { 245*af275ec6SWu Hao dev_dbg(dev, "PR error detected %llx\n", 246*af275ec6SWu Hao (unsigned long long)priv->pr_error); 247*af275ec6SWu Hao return -EIO; 248*af275ec6SWu Hao } 249*af275ec6SWu Hao 250*af275ec6SWu Hao dev_dbg(dev, "PR done successfully\n"); 251*af275ec6SWu Hao 252*af275ec6SWu Hao return 0; 253*af275ec6SWu Hao } 254*af275ec6SWu Hao 255*af275ec6SWu Hao static enum fpga_mgr_states fme_mgr_state(struct fpga_manager *mgr) 256*af275ec6SWu Hao { 257*af275ec6SWu Hao return FPGA_MGR_STATE_UNKNOWN; 258*af275ec6SWu Hao } 259*af275ec6SWu Hao 260*af275ec6SWu Hao static u64 fme_mgr_status(struct fpga_manager *mgr) 261*af275ec6SWu Hao { 262*af275ec6SWu Hao struct fme_mgr_priv *priv = mgr->priv; 263*af275ec6SWu Hao 264*af275ec6SWu Hao return pr_error_to_mgr_status(priv->pr_error); 265*af275ec6SWu Hao } 266*af275ec6SWu Hao 267*af275ec6SWu Hao static const struct fpga_manager_ops fme_mgr_ops = { 268*af275ec6SWu Hao .write_init = fme_mgr_write_init, 269*af275ec6SWu Hao .write = fme_mgr_write, 270*af275ec6SWu Hao .write_complete = fme_mgr_write_complete, 271*af275ec6SWu Hao .state = fme_mgr_state, 272*af275ec6SWu Hao .status = fme_mgr_status, 273*af275ec6SWu Hao }; 274*af275ec6SWu Hao 275*af275ec6SWu Hao static int fme_mgr_probe(struct platform_device *pdev) 276*af275ec6SWu Hao { 277*af275ec6SWu Hao struct dfl_fme_mgr_pdata *pdata = dev_get_platdata(&pdev->dev); 278*af275ec6SWu Hao struct device *dev = &pdev->dev; 279*af275ec6SWu Hao struct fme_mgr_priv *priv; 280*af275ec6SWu Hao struct fpga_manager *mgr; 281*af275ec6SWu Hao struct resource *res; 282*af275ec6SWu Hao int ret; 283*af275ec6SWu Hao 284*af275ec6SWu Hao priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL); 285*af275ec6SWu Hao if (!priv) 286*af275ec6SWu Hao return -ENOMEM; 287*af275ec6SWu Hao 288*af275ec6SWu Hao if (pdata->ioaddr) 289*af275ec6SWu Hao priv->ioaddr = pdata->ioaddr; 290*af275ec6SWu Hao 291*af275ec6SWu Hao if (!priv->ioaddr) { 292*af275ec6SWu Hao res = platform_get_resource(pdev, IORESOURCE_MEM, 0); 293*af275ec6SWu Hao priv->ioaddr = devm_ioremap_resource(dev, res); 294*af275ec6SWu Hao if (IS_ERR(priv->ioaddr)) 295*af275ec6SWu Hao return PTR_ERR(priv->ioaddr); 296*af275ec6SWu Hao } 297*af275ec6SWu Hao 298*af275ec6SWu Hao mgr = fpga_mgr_create(dev, "DFL FME FPGA Manager", 299*af275ec6SWu Hao &fme_mgr_ops, priv); 300*af275ec6SWu Hao if (!mgr) 301*af275ec6SWu Hao return -ENOMEM; 302*af275ec6SWu Hao 303*af275ec6SWu Hao platform_set_drvdata(pdev, mgr); 304*af275ec6SWu Hao 305*af275ec6SWu Hao ret = fpga_mgr_register(mgr); 306*af275ec6SWu Hao if (ret) 307*af275ec6SWu Hao fpga_mgr_free(mgr); 308*af275ec6SWu Hao 309*af275ec6SWu Hao return ret; 310*af275ec6SWu Hao } 311*af275ec6SWu Hao 312*af275ec6SWu Hao static int fme_mgr_remove(struct platform_device *pdev) 313*af275ec6SWu Hao { 314*af275ec6SWu Hao struct fpga_manager *mgr = platform_get_drvdata(pdev); 315*af275ec6SWu Hao 316*af275ec6SWu Hao fpga_mgr_unregister(mgr); 317*af275ec6SWu Hao 318*af275ec6SWu Hao return 0; 319*af275ec6SWu Hao } 320*af275ec6SWu Hao 321*af275ec6SWu Hao static struct platform_driver fme_mgr_driver = { 322*af275ec6SWu Hao .driver = { 323*af275ec6SWu Hao .name = DFL_FPGA_FME_MGR, 324*af275ec6SWu Hao }, 325*af275ec6SWu Hao .probe = fme_mgr_probe, 326*af275ec6SWu Hao .remove = fme_mgr_remove, 327*af275ec6SWu Hao }; 328*af275ec6SWu Hao 329*af275ec6SWu Hao module_platform_driver(fme_mgr_driver); 330*af275ec6SWu Hao 331*af275ec6SWu Hao MODULE_DESCRIPTION("FPGA Manager for DFL FPGA Management Engine"); 332*af275ec6SWu Hao MODULE_AUTHOR("Intel Corporation"); 333*af275ec6SWu Hao MODULE_LICENSE("GPL v2"); 334*af275ec6SWu Hao MODULE_ALIAS("platform:dfl-fme-mgr"); 335