xref: /linux/drivers/fpga/dfl-fme-mgr.c (revision 1e463430a9e4a4c6b457a9c49b07be9df299cd8b)
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
33e150e3f4SWu Hao #define FME_PR_INTFC_ID_L	0xA8
34e150e3f4SWu 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 u64 fme_mgr_status(struct fpga_manager *mgr)
256af275ec6SWu Hao {
257af275ec6SWu Hao 	struct fme_mgr_priv *priv = mgr->priv;
258af275ec6SWu Hao 
259af275ec6SWu Hao 	return pr_error_to_mgr_status(priv->pr_error);
260af275ec6SWu Hao }
261af275ec6SWu Hao 
262af275ec6SWu Hao static const struct fpga_manager_ops fme_mgr_ops = {
263af275ec6SWu Hao 	.write_init = fme_mgr_write_init,
264af275ec6SWu Hao 	.write = fme_mgr_write,
265af275ec6SWu Hao 	.write_complete = fme_mgr_write_complete,
266af275ec6SWu Hao 	.status = fme_mgr_status,
267af275ec6SWu Hao };
268af275ec6SWu Hao 
2695ebae801SWu Hao static void fme_mgr_get_compat_id(void __iomem *fme_pr,
2705ebae801SWu Hao 				  struct fpga_compat_id *id)
2715ebae801SWu Hao {
2725ebae801SWu Hao 	id->id_l = readq(fme_pr + FME_PR_INTFC_ID_L);
2735ebae801SWu Hao 	id->id_h = readq(fme_pr + FME_PR_INTFC_ID_H);
2745ebae801SWu Hao }
2755ebae801SWu Hao 
276af275ec6SWu Hao static int fme_mgr_probe(struct platform_device *pdev)
277af275ec6SWu Hao {
278af275ec6SWu Hao 	struct dfl_fme_mgr_pdata *pdata = dev_get_platdata(&pdev->dev);
2794ba0b2c2SRuss Weight 	struct fpga_manager_info info = { 0 };
280af275ec6SWu Hao 	struct device *dev = &pdev->dev;
281af275ec6SWu Hao 	struct fme_mgr_priv *priv;
282af275ec6SWu Hao 	struct fpga_manager *mgr;
283af275ec6SWu Hao 
284af275ec6SWu Hao 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
285af275ec6SWu Hao 	if (!priv)
286af275ec6SWu Hao 		return -ENOMEM;
287af275ec6SWu Hao 
288af275ec6SWu Hao 	if (pdata->ioaddr)
289af275ec6SWu Hao 		priv->ioaddr = pdata->ioaddr;
290af275ec6SWu Hao 
291af275ec6SWu Hao 	if (!priv->ioaddr) {
292*1e463430SYangtao Li 		priv->ioaddr = devm_platform_ioremap_resource(pdev, 0);
293af275ec6SWu Hao 		if (IS_ERR(priv->ioaddr))
294af275ec6SWu Hao 			return PTR_ERR(priv->ioaddr);
295af275ec6SWu Hao 	}
296af275ec6SWu Hao 
2974ba0b2c2SRuss Weight 	info.name = "DFL FME FPGA Manager";
2984ba0b2c2SRuss Weight 	info.mops = &fme_mgr_ops;
2994ba0b2c2SRuss Weight 	info.priv = priv;
3004ba0b2c2SRuss Weight 	info.compat_id = devm_kzalloc(dev, sizeof(*info.compat_id), GFP_KERNEL);
3014ba0b2c2SRuss Weight 	if (!info.compat_id)
3025ebae801SWu Hao 		return -ENOMEM;
3035ebae801SWu Hao 
3044ba0b2c2SRuss Weight 	fme_mgr_get_compat_id(priv->ioaddr, info.compat_id);
3054ba0b2c2SRuss Weight 	mgr = devm_fpga_mgr_register_full(dev, &info);
3064ba0b2c2SRuss Weight 	return PTR_ERR_OR_ZERO(mgr);
307af275ec6SWu Hao }
308af275ec6SWu Hao 
309af275ec6SWu Hao static struct platform_driver fme_mgr_driver = {
310af275ec6SWu Hao 	.driver	= {
311af275ec6SWu Hao 		.name    = DFL_FPGA_FME_MGR,
312af275ec6SWu Hao 	},
313af275ec6SWu Hao 	.probe   = fme_mgr_probe,
314af275ec6SWu Hao };
315af275ec6SWu Hao 
316af275ec6SWu Hao module_platform_driver(fme_mgr_driver);
317af275ec6SWu Hao 
318af275ec6SWu Hao MODULE_DESCRIPTION("FPGA Manager for DFL FPGA Management Engine");
319af275ec6SWu Hao MODULE_AUTHOR("Intel Corporation");
320af275ec6SWu Hao MODULE_LICENSE("GPL v2");
321af275ec6SWu Hao MODULE_ALIAS("platform:dfl-fme-mgr");
322