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