1 // SPDX-License-Identifier: GPL-2.0
3 * FPGA Manager Driver for FPGA Management Engine (FME)
5 * Copyright (C) 2017-2018 Intel Corporation, Inc.
8 * Kang Luwei <luwei.kang@intel.com>
9 * Xiao Guangrong <guangrong.xiao@linux.intel.com>
10 * Wu Hao <hao.wu@intel.com>
11 * Joseph Grecco <joe.grecco@intel.com>
12 * Enno Luebbers <enno.luebbers@intel.com>
13 * Tim Whisonant <tim.whisonant@intel.com>
14 * Ananda Ravuri <ananda.ravuri@intel.com>
15 * Christopher Rauer <christopher.rauer@intel.com>
16 * Henry Mitchel <henry.mitchel@intel.com>
19 #include <linux/bitfield.h>
20 #include <linux/module.h>
21 #include <linux/iopoll.h>
22 #include <linux/io-64-nonatomic-lo-hi.h>
23 #include <linux/fpga/fpga-mgr.h>
25 #include "dfl-fme-pr.h"
27 /* FME Partial Reconfiguration Sub Feature Register Set */
28 #define FME_PR_DFH 0x0
29 #define FME_PR_CTRL 0x8
30 #define FME_PR_STS 0x10
31 #define FME_PR_DATA 0x18
32 #define FME_PR_ERR 0x20
33 #define FME_PR_INTFC_ID_L 0xA8
34 #define FME_PR_INTFC_ID_H 0xB0
36 /* FME PR Control Register Bitfield */
37 #define FME_PR_CTRL_PR_RST BIT_ULL(0) /* Reset PR engine */
38 #define FME_PR_CTRL_PR_RSTACK BIT_ULL(4) /* Ack for PR engine reset */
39 #define FME_PR_CTRL_PR_RGN_ID GENMASK_ULL(9, 7) /* PR Region ID */
40 #define FME_PR_CTRL_PR_START BIT_ULL(12) /* Start to request PR service */
41 #define FME_PR_CTRL_PR_COMPLETE BIT_ULL(13) /* PR data push completion */
43 /* FME PR Status Register Bitfield */
44 /* Number of available entries in HW queue inside the PR engine. */
45 #define FME_PR_STS_PR_CREDIT GENMASK_ULL(8, 0)
46 #define FME_PR_STS_PR_STS BIT_ULL(16) /* PR operation status */
47 #define FME_PR_STS_PR_STS_IDLE 0
48 #define FME_PR_STS_PR_CTRLR_STS GENMASK_ULL(22, 20) /* Controller status */
49 #define FME_PR_STS_PR_HOST_STS GENMASK_ULL(27, 24) /* PR host status */
51 /* FME PR Data Register Bitfield */
52 /* PR data from the raw-binary file. */
53 #define FME_PR_DATA_PR_DATA_RAW GENMASK_ULL(32, 0)
55 /* FME PR Error Register */
56 /* PR Operation errors detected. */
57 #define FME_PR_ERR_OPERATION_ERR BIT_ULL(0)
58 /* CRC error detected. */
59 #define FME_PR_ERR_CRC_ERR BIT_ULL(1)
60 /* Incompatible PR bitstream detected. */
61 #define FME_PR_ERR_INCOMPATIBLE_BS BIT_ULL(2)
62 /* PR data push protocol violated. */
63 #define FME_PR_ERR_PROTOCOL_ERR BIT_ULL(3)
64 /* PR data fifo overflow error detected */
65 #define FME_PR_ERR_FIFO_OVERFLOW BIT_ULL(4)
67 #define PR_WAIT_TIMEOUT 8000000
68 #define PR_HOST_STATUS_IDLE 0
75 static u64 pr_error_to_mgr_status(u64 err)
79 if (err & FME_PR_ERR_OPERATION_ERR)
80 status |= FPGA_MGR_STATUS_OPERATION_ERR;
81 if (err & FME_PR_ERR_CRC_ERR)
82 status |= FPGA_MGR_STATUS_CRC_ERR;
83 if (err & FME_PR_ERR_INCOMPATIBLE_BS)
84 status |= FPGA_MGR_STATUS_INCOMPATIBLE_IMAGE_ERR;
85 if (err & FME_PR_ERR_PROTOCOL_ERR)
86 status |= FPGA_MGR_STATUS_IP_PROTOCOL_ERR;
87 if (err & FME_PR_ERR_FIFO_OVERFLOW)
88 status |= FPGA_MGR_STATUS_FIFO_OVERFLOW_ERR;
93 static u64 fme_mgr_pr_error_handle(void __iomem *fme_pr)
95 u64 pr_status, pr_error;
97 pr_status = readq(fme_pr + FME_PR_STS);
98 if (!(pr_status & FME_PR_STS_PR_STS))
101 pr_error = readq(fme_pr + FME_PR_ERR);
102 writeq(pr_error, fme_pr + FME_PR_ERR);
107 static int fme_mgr_write_init(struct fpga_manager *mgr,
108 struct fpga_image_info *info,
109 const char *buf, size_t count)
111 struct device *dev = &mgr->dev;
112 struct fme_mgr_priv *priv = mgr->priv;
113 void __iomem *fme_pr = priv->ioaddr;
114 u64 pr_ctrl, pr_status;
116 if (!(info->flags & FPGA_MGR_PARTIAL_RECONFIG)) {
117 dev_err(dev, "only supports partial reconfiguration.\n");
121 dev_dbg(dev, "resetting PR before initiated PR\n");
123 pr_ctrl = readq(fme_pr + FME_PR_CTRL);
124 pr_ctrl |= FME_PR_CTRL_PR_RST;
125 writeq(pr_ctrl, fme_pr + FME_PR_CTRL);
127 if (readq_poll_timeout(fme_pr + FME_PR_CTRL, pr_ctrl,
128 pr_ctrl & FME_PR_CTRL_PR_RSTACK, 1,
130 dev_err(dev, "PR Reset ACK timeout\n");
134 pr_ctrl = readq(fme_pr + FME_PR_CTRL);
135 pr_ctrl &= ~FME_PR_CTRL_PR_RST;
136 writeq(pr_ctrl, fme_pr + FME_PR_CTRL);
139 "waiting for PR resource in HW to be initialized and ready\n");
141 if (readq_poll_timeout(fme_pr + FME_PR_STS, pr_status,
142 (pr_status & FME_PR_STS_PR_STS) ==
143 FME_PR_STS_PR_STS_IDLE, 1, PR_WAIT_TIMEOUT)) {
144 dev_err(dev, "PR Status timeout\n");
145 priv->pr_error = fme_mgr_pr_error_handle(fme_pr);
149 dev_dbg(dev, "check and clear previous PR error\n");
150 priv->pr_error = fme_mgr_pr_error_handle(fme_pr);
152 dev_dbg(dev, "previous PR error detected %llx\n",
153 (unsigned long long)priv->pr_error);
155 dev_dbg(dev, "set PR port ID\n");
157 pr_ctrl = readq(fme_pr + FME_PR_CTRL);
158 pr_ctrl &= ~FME_PR_CTRL_PR_RGN_ID;
159 pr_ctrl |= FIELD_PREP(FME_PR_CTRL_PR_RGN_ID, info->region_id);
160 writeq(pr_ctrl, fme_pr + FME_PR_CTRL);
165 static int fme_mgr_write(struct fpga_manager *mgr,
166 const char *buf, size_t count)
168 struct device *dev = &mgr->dev;
169 struct fme_mgr_priv *priv = mgr->priv;
170 void __iomem *fme_pr = priv->ioaddr;
171 u64 pr_ctrl, pr_status, pr_data;
172 int delay = 0, pr_credit, i = 0;
174 dev_dbg(dev, "start request\n");
176 pr_ctrl = readq(fme_pr + FME_PR_CTRL);
177 pr_ctrl |= FME_PR_CTRL_PR_START;
178 writeq(pr_ctrl, fme_pr + FME_PR_CTRL);
180 dev_dbg(dev, "pushing data from bitstream to HW\n");
183 * driver can push data to PR hardware using PR_DATA register once HW
184 * has enough pr_credit (> 1), pr_credit reduces one for every 32bit
185 * pr data write to PR_DATA register. If pr_credit <= 1, driver needs
186 * to wait for enough pr_credit from hardware by polling.
188 pr_status = readq(fme_pr + FME_PR_STS);
189 pr_credit = FIELD_GET(FME_PR_STS_PR_CREDIT, pr_status);
192 while (pr_credit <= 1) {
193 if (delay++ > PR_WAIT_TIMEOUT) {
194 dev_err(dev, "PR_CREDIT timeout\n");
199 pr_status = readq(fme_pr + FME_PR_STS);
200 pr_credit = FIELD_GET(FME_PR_STS_PR_CREDIT, pr_status);
204 dev_err(dev, "Invalid PR bitstream size\n");
209 pr_data |= FIELD_PREP(FME_PR_DATA_PR_DATA_RAW,
210 *(((u32 *)buf) + i));
211 writeq(pr_data, fme_pr + FME_PR_DATA);
220 static int fme_mgr_write_complete(struct fpga_manager *mgr,
221 struct fpga_image_info *info)
223 struct device *dev = &mgr->dev;
224 struct fme_mgr_priv *priv = mgr->priv;
225 void __iomem *fme_pr = priv->ioaddr;
228 pr_ctrl = readq(fme_pr + FME_PR_CTRL);
229 pr_ctrl |= FME_PR_CTRL_PR_COMPLETE;
230 writeq(pr_ctrl, fme_pr + FME_PR_CTRL);
232 dev_dbg(dev, "green bitstream push complete\n");
233 dev_dbg(dev, "waiting for HW to release PR resource\n");
235 if (readq_poll_timeout(fme_pr + FME_PR_CTRL, pr_ctrl,
236 !(pr_ctrl & FME_PR_CTRL_PR_START), 1,
238 dev_err(dev, "PR Completion ACK timeout.\n");
242 dev_dbg(dev, "PR operation complete, checking status\n");
243 priv->pr_error = fme_mgr_pr_error_handle(fme_pr);
244 if (priv->pr_error) {
245 dev_dbg(dev, "PR error detected %llx\n",
246 (unsigned long long)priv->pr_error);
250 dev_dbg(dev, "PR done successfully\n");
255 static u64 fme_mgr_status(struct fpga_manager *mgr)
257 struct fme_mgr_priv *priv = mgr->priv;
259 return pr_error_to_mgr_status(priv->pr_error);
262 static const struct fpga_manager_ops fme_mgr_ops = {
263 .write_init = fme_mgr_write_init,
264 .write = fme_mgr_write,
265 .write_complete = fme_mgr_write_complete,
266 .status = fme_mgr_status,
269 static void fme_mgr_get_compat_id(void __iomem *fme_pr,
270 struct fpga_compat_id *id)
272 id->id_l = readq(fme_pr + FME_PR_INTFC_ID_L);
273 id->id_h = readq(fme_pr + FME_PR_INTFC_ID_H);
276 static int fme_mgr_probe(struct platform_device *pdev)
278 struct dfl_fme_mgr_pdata *pdata = dev_get_platdata(&pdev->dev);
279 struct fpga_manager_info info = { 0 };
280 struct device *dev = &pdev->dev;
281 struct fme_mgr_priv *priv;
282 struct fpga_manager *mgr;
283 struct resource *res;
285 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
290 priv->ioaddr = pdata->ioaddr;
293 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
294 priv->ioaddr = devm_ioremap_resource(dev, res);
295 if (IS_ERR(priv->ioaddr))
296 return PTR_ERR(priv->ioaddr);
299 info.name = "DFL FME FPGA Manager";
300 info.mops = &fme_mgr_ops;
302 info.compat_id = devm_kzalloc(dev, sizeof(*info.compat_id), GFP_KERNEL);
306 fme_mgr_get_compat_id(priv->ioaddr, info.compat_id);
307 mgr = devm_fpga_mgr_register_full(dev, &info);
308 return PTR_ERR_OR_ZERO(mgr);
311 static struct platform_driver fme_mgr_driver = {
313 .name = DFL_FPGA_FME_MGR,
315 .probe = fme_mgr_probe,
318 module_platform_driver(fme_mgr_driver);
320 MODULE_DESCRIPTION("FPGA Manager for DFL FPGA Management Engine");
321 MODULE_AUTHOR("Intel Corporation");
322 MODULE_LICENSE("GPL v2");
323 MODULE_ALIAS("platform:dfl-fme-mgr");