1 // SPDX-License-Identifier: GPL-2.0
3 * FPGA Manager Driver for Intel Stratix10 SoC
5 * Copyright (C) 2018 Intel Corporation
7 #include <linux/completion.h>
8 #include <linux/fpga/fpga-mgr.h>
9 #include <linux/firmware/intel/stratix10-svc-client.h>
10 #include <linux/module.h>
12 #include <linux/of_platform.h>
13 #include <linux/platform_device.h>
16 * FPGA programming requires a higher level of privilege (EL3), per the SoC
19 #define NUM_SVC_BUFS 4
20 #define SVC_BUF_SIZE SZ_512K
22 /* Indicates buffer is in use if set */
23 #define SVC_BUF_LOCK 0
25 #define S10_BUFFER_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_BUFFER_TIMEOUT_MS))
26 #define S10_RECONFIG_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_REQUEST_TIMEOUT_MS))
30 * buf: virtual address of buf provided by service layer
31 * lock: locked if buffer is in use
39 struct stratix10_svc_chan *chan;
40 struct stratix10_svc_client client;
41 struct completion status_return_completion;
42 struct s10_svc_buf svc_bufs[NUM_SVC_BUFS];
46 static int s10_svc_send_msg(struct s10_priv *priv,
47 enum stratix10_svc_command_code command,
48 void *payload, u32 payload_length)
50 struct stratix10_svc_chan *chan = priv->chan;
51 struct device *dev = priv->client.dev;
52 struct stratix10_svc_client_msg msg;
55 dev_dbg(dev, "%s cmd=%d payload=%p length=%d\n",
56 __func__, command, payload, payload_length);
58 msg.command = command;
59 msg.payload = payload;
60 msg.payload_length = payload_length;
62 ret = stratix10_svc_send(chan, &msg);
63 dev_dbg(dev, "stratix10_svc_send returned status %d\n", ret);
69 * Free buffers allocated from the service layer's pool that are not in use.
70 * Return true when all buffers are freed.
72 static bool s10_free_buffers(struct fpga_manager *mgr)
74 struct s10_priv *priv = mgr->priv;
78 for (i = 0; i < NUM_SVC_BUFS; i++) {
79 if (!priv->svc_bufs[i].buf) {
84 if (!test_and_set_bit_lock(SVC_BUF_LOCK,
85 &priv->svc_bufs[i].lock)) {
86 stratix10_svc_free_memory(priv->chan,
87 priv->svc_bufs[i].buf);
88 priv->svc_bufs[i].buf = NULL;
93 return num_free == NUM_SVC_BUFS;
97 * Returns count of how many buffers are not in use.
99 static uint s10_free_buffer_count(struct fpga_manager *mgr)
101 struct s10_priv *priv = mgr->priv;
105 for (i = 0; i < NUM_SVC_BUFS; i++)
106 if (!priv->svc_bufs[i].buf)
114 * Given the returned buffer address, match that address to our buffer struct
115 * and unlock that buffer. This marks it as available to be refilled and sent
118 * kaddr: kernel address of buffer that was returned from service layer
120 static void s10_unlock_bufs(struct s10_priv *priv, void *kaddr)
127 for (i = 0; i < NUM_SVC_BUFS; i++)
128 if (priv->svc_bufs[i].buf == kaddr) {
129 clear_bit_unlock(SVC_BUF_LOCK,
130 &priv->svc_bufs[i].lock);
134 WARN(1, "Unknown buffer returned from service layer %p\n", kaddr);
138 * s10_receive_callback - callback for service layer to use to provide client
139 * (this driver) messages received through the mailbox.
140 * client: service layer client struct
141 * data: message from service layer
143 static void s10_receive_callback(struct stratix10_svc_client *client,
144 struct stratix10_svc_cb_data *data)
146 struct s10_priv *priv = client->priv;
150 WARN_ONCE(!data, "%s: stratix10_svc_rc_data = NULL", __func__);
152 status = data->status;
155 * Here we set status bits as we receive them. Elsewhere, we always use
156 * test_and_clear_bit() to check status in priv->status
158 for (i = 0; i <= SVC_STATUS_ERROR; i++)
159 if (status & (1 << i))
160 set_bit(i, &priv->status);
162 if (status & BIT(SVC_STATUS_BUFFER_DONE)) {
163 s10_unlock_bufs(priv, data->kaddr1);
164 s10_unlock_bufs(priv, data->kaddr2);
165 s10_unlock_bufs(priv, data->kaddr3);
168 complete(&priv->status_return_completion);
172 * s10_ops_write_init - prepare for FPGA reconfiguration by requesting
173 * partial reconfig and allocating buffers from the service layer.
175 static int s10_ops_write_init(struct fpga_manager *mgr,
176 struct fpga_image_info *info,
177 const char *buf, size_t count)
179 struct s10_priv *priv = mgr->priv;
180 struct device *dev = priv->client.dev;
181 struct stratix10_svc_command_config_type ctype;
187 if (info->flags & FPGA_MGR_PARTIAL_RECONFIG) {
188 dev_dbg(dev, "Requesting partial reconfiguration.\n");
189 ctype.flags |= BIT(COMMAND_RECONFIG_FLAG_PARTIAL);
191 dev_dbg(dev, "Requesting full reconfiguration.\n");
194 reinit_completion(&priv->status_return_completion);
195 ret = s10_svc_send_msg(priv, COMMAND_RECONFIG,
196 &ctype, sizeof(ctype));
200 ret = wait_for_completion_timeout(
201 &priv->status_return_completion, S10_RECONFIG_TIMEOUT);
203 dev_err(dev, "timeout waiting for RECONFIG_REQUEST\n");
209 if (!test_and_clear_bit(SVC_STATUS_OK, &priv->status)) {
214 /* Allocate buffers from the service layer's pool. */
215 for (i = 0; i < NUM_SVC_BUFS; i++) {
216 kbuf = stratix10_svc_allocate_memory(priv->chan, SVC_BUF_SIZE);
218 s10_free_buffers(mgr);
223 priv->svc_bufs[i].buf = kbuf;
224 priv->svc_bufs[i].lock = 0;
228 stratix10_svc_done(priv->chan);
233 * s10_send_buf - send a buffer to the service layer queue
234 * mgr: fpga manager struct
235 * buf: fpga image buffer
236 * count: size of buf in bytes
237 * Returns # of bytes transferred or -ENOBUFS if the all the buffers are in use
238 * or if the service queue is full. Never returns 0.
240 static int s10_send_buf(struct fpga_manager *mgr, const char *buf, size_t count)
242 struct s10_priv *priv = mgr->priv;
243 struct device *dev = priv->client.dev;
249 /* get/lock a buffer that that's not being used */
250 for (i = 0; i < NUM_SVC_BUFS; i++)
251 if (!test_and_set_bit_lock(SVC_BUF_LOCK,
252 &priv->svc_bufs[i].lock))
255 if (i == NUM_SVC_BUFS)
258 xfer_sz = count < SVC_BUF_SIZE ? count : SVC_BUF_SIZE;
260 svc_buf = priv->svc_bufs[i].buf;
261 memcpy(svc_buf, buf, xfer_sz);
262 ret = s10_svc_send_msg(priv, COMMAND_RECONFIG_DATA_SUBMIT,
266 "Error while sending data to service layer (%d)", ret);
267 clear_bit_unlock(SVC_BUF_LOCK, &priv->svc_bufs[i].lock);
275 * Send an FPGA image to privileged layers to write to the FPGA. When done
276 * sending, free all service layer buffers we allocated in write_init.
278 static int s10_ops_write(struct fpga_manager *mgr, const char *buf,
281 struct s10_priv *priv = mgr->priv;
282 struct device *dev = priv->client.dev;
288 * Loop waiting for buffers to be returned. When a buffer is returned,
289 * reuse it to send more data or free if if all data has been sent.
291 while (count > 0 || s10_free_buffer_count(mgr) != NUM_SVC_BUFS) {
292 reinit_completion(&priv->status_return_completion);
295 sent = s10_send_buf(mgr, buf, count);
302 if (s10_free_buffers(mgr))
305 ret = s10_svc_send_msg(
306 priv, COMMAND_RECONFIG_DATA_CLAIM,
313 * If callback hasn't already happened, wait for buffers to be
314 * returned from service layer
316 wait_status = 1; /* not timed out */
318 wait_status = wait_for_completion_timeout(
319 &priv->status_return_completion,
322 if (test_and_clear_bit(SVC_STATUS_BUFFER_DONE, &priv->status) ||
323 test_and_clear_bit(SVC_STATUS_BUFFER_SUBMITTED,
329 if (test_and_clear_bit(SVC_STATUS_ERROR, &priv->status)) {
330 dev_err(dev, "ERROR - giving up - SVC_STATUS_ERROR\n");
336 dev_err(dev, "timeout waiting for svc layer buffers\n");
342 if (!s10_free_buffers(mgr))
343 dev_err(dev, "%s not all buffers were freed\n", __func__);
348 static int s10_ops_write_complete(struct fpga_manager *mgr,
349 struct fpga_image_info *info)
351 struct s10_priv *priv = mgr->priv;
352 struct device *dev = priv->client.dev;
353 unsigned long timeout;
356 timeout = usecs_to_jiffies(info->config_complete_timeout_us);
359 reinit_completion(&priv->status_return_completion);
361 ret = s10_svc_send_msg(priv, COMMAND_RECONFIG_STATUS, NULL, 0);
365 ret = wait_for_completion_timeout(
366 &priv->status_return_completion, timeout);
369 "timeout waiting for RECONFIG_COMPLETED\n");
373 /* Not error or timeout, so ret is # of jiffies until timeout */
377 if (test_and_clear_bit(SVC_STATUS_COMPLETED, &priv->status))
380 if (test_and_clear_bit(SVC_STATUS_ERROR, &priv->status)) {
381 dev_err(dev, "ERROR - giving up - SVC_STATUS_ERROR\n");
387 stratix10_svc_done(priv->chan);
392 static const struct fpga_manager_ops s10_ops = {
393 .write_init = s10_ops_write_init,
394 .write = s10_ops_write,
395 .write_complete = s10_ops_write_complete,
398 static int s10_probe(struct platform_device *pdev)
400 struct device *dev = &pdev->dev;
401 struct s10_priv *priv;
402 struct fpga_manager *mgr;
405 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
409 priv->client.dev = dev;
410 priv->client.receive_cb = s10_receive_callback;
411 priv->client.priv = priv;
413 priv->chan = stratix10_svc_request_channel_byname(&priv->client,
415 if (IS_ERR(priv->chan)) {
416 dev_err(dev, "couldn't get service channel (%s)\n",
418 return PTR_ERR(priv->chan);
421 init_completion(&priv->status_return_completion);
423 mgr = fpga_mgr_register(dev, "Stratix10 SOC FPGA Manager",
426 dev_err(dev, "unable to register FPGA manager\n");
431 platform_set_drvdata(pdev, mgr);
435 stratix10_svc_free_channel(priv->chan);
439 static int s10_remove(struct platform_device *pdev)
441 struct fpga_manager *mgr = platform_get_drvdata(pdev);
442 struct s10_priv *priv = mgr->priv;
444 fpga_mgr_unregister(mgr);
445 stratix10_svc_free_channel(priv->chan);
450 static const struct of_device_id s10_of_match[] = {
451 {.compatible = "intel,stratix10-soc-fpga-mgr"},
452 {.compatible = "intel,agilex-soc-fpga-mgr"},
456 MODULE_DEVICE_TABLE(of, s10_of_match);
458 static struct platform_driver s10_driver = {
460 .remove = s10_remove,
462 .name = "Stratix10 SoC FPGA manager",
463 .of_match_table = of_match_ptr(s10_of_match),
467 static int __init s10_init(void)
469 struct device_node *fw_np;
470 struct device_node *np;
473 fw_np = of_find_node_by_name(NULL, "svc");
478 np = of_find_matching_node(fw_np, s10_of_match);
485 ret = of_platform_populate(fw_np, s10_of_match, NULL, NULL);
490 return platform_driver_register(&s10_driver);
493 static void __exit s10_exit(void)
495 return platform_driver_unregister(&s10_driver);
498 module_init(s10_init);
499 module_exit(s10_exit);
502 MODULE_DESCRIPTION("Intel Stratix 10 SOC FPGA Manager");
503 MODULE_LICENSE("GPL v2");