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>
15 * FPGA programming requires a higher level of privilege (EL3), per the SoC
18 #define NUM_SVC_BUFS 4
19 #define SVC_BUF_SIZE SZ_512K
21 /* Indicates buffer is in use if set */
22 #define SVC_BUF_LOCK 0
24 #define S10_BUFFER_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_BUFFER_TIMEOUT_MS))
25 #define S10_RECONFIG_TIMEOUT (msecs_to_jiffies(SVC_RECONFIG_REQUEST_TIMEOUT_MS))
29 * buf: virtual address of buf provided by service layer
30 * lock: locked if buffer is in use
38 struct stratix10_svc_chan
*chan
;
39 struct stratix10_svc_client client
;
40 struct completion status_return_completion
;
41 struct s10_svc_buf svc_bufs
[NUM_SVC_BUFS
];
45 static int s10_svc_send_msg(struct s10_priv
*priv
,
46 enum stratix10_svc_command_code command
,
47 void *payload
, u32 payload_length
)
49 struct stratix10_svc_chan
*chan
= priv
->chan
;
50 struct device
*dev
= priv
->client
.dev
;
51 struct stratix10_svc_client_msg msg
;
54 dev_dbg(dev
, "%s cmd=%d payload=%p length=%d\n",
55 __func__
, command
, payload
, payload_length
);
57 msg
.command
= command
;
58 msg
.payload
= payload
;
59 msg
.payload_length
= payload_length
;
61 ret
= stratix10_svc_send(chan
, &msg
);
62 dev_dbg(dev
, "stratix10_svc_send returned status %d\n", ret
);
68 * Free buffers allocated from the service layer's pool that are not in use.
69 * Return true when all buffers are freed.
71 static bool s10_free_buffers(struct fpga_manager
*mgr
)
73 struct s10_priv
*priv
= mgr
->priv
;
77 for (i
= 0; i
< NUM_SVC_BUFS
; i
++) {
78 if (!priv
->svc_bufs
[i
].buf
) {
83 if (!test_and_set_bit_lock(SVC_BUF_LOCK
,
84 &priv
->svc_bufs
[i
].lock
)) {
85 stratix10_svc_free_memory(priv
->chan
,
86 priv
->svc_bufs
[i
].buf
);
87 priv
->svc_bufs
[i
].buf
= NULL
;
92 return num_free
== NUM_SVC_BUFS
;
96 * Returns count of how many buffers are not in use.
98 static uint
s10_free_buffer_count(struct fpga_manager
*mgr
)
100 struct s10_priv
*priv
= mgr
->priv
;
104 for (i
= 0; i
< NUM_SVC_BUFS
; i
++)
105 if (!priv
->svc_bufs
[i
].buf
)
113 * Given the returned buffer address, match that address to our buffer struct
114 * and unlock that buffer. This marks it as available to be refilled and sent
117 * kaddr: kernel address of buffer that was returned from service layer
119 static void s10_unlock_bufs(struct s10_priv
*priv
, void *kaddr
)
126 for (i
= 0; i
< NUM_SVC_BUFS
; i
++)
127 if (priv
->svc_bufs
[i
].buf
== kaddr
) {
128 clear_bit_unlock(SVC_BUF_LOCK
,
129 &priv
->svc_bufs
[i
].lock
);
133 WARN(1, "Unknown buffer returned from service layer %p\n", kaddr
);
137 * s10_receive_callback - callback for service layer to use to provide client
138 * (this driver) messages received through the mailbox.
139 * client: service layer client struct
140 * data: message from service layer
142 static void s10_receive_callback(struct stratix10_svc_client
*client
,
143 struct stratix10_svc_cb_data
*data
)
145 struct s10_priv
*priv
= client
->priv
;
149 WARN_ONCE(!data
, "%s: stratix10_svc_rc_data = NULL", __func__
);
151 status
= data
->status
;
154 * Here we set status bits as we receive them. Elsewhere, we always use
155 * test_and_clear_bit() to check status in priv->status
157 for (i
= 0; i
<= SVC_STATUS_RECONFIG_ERROR
; i
++)
158 if (status
& (1 << i
))
159 set_bit(i
, &priv
->status
);
161 if (status
& BIT(SVC_STATUS_RECONFIG_BUFFER_DONE
)) {
162 s10_unlock_bufs(priv
, data
->kaddr1
);
163 s10_unlock_bufs(priv
, data
->kaddr2
);
164 s10_unlock_bufs(priv
, data
->kaddr3
);
167 complete(&priv
->status_return_completion
);
171 * s10_ops_write_init - prepare for FPGA reconfiguration by requesting
172 * partial reconfig and allocating buffers from the service layer.
174 static int s10_ops_write_init(struct fpga_manager
*mgr
,
175 struct fpga_image_info
*info
,
176 const char *buf
, size_t count
)
178 struct s10_priv
*priv
= mgr
->priv
;
179 struct device
*dev
= priv
->client
.dev
;
180 struct stratix10_svc_command_config_type ctype
;
186 if (info
->flags
& FPGA_MGR_PARTIAL_RECONFIG
) {
187 dev_dbg(dev
, "Requesting partial reconfiguration.\n");
188 ctype
.flags
|= BIT(COMMAND_RECONFIG_FLAG_PARTIAL
);
190 dev_dbg(dev
, "Requesting full reconfiguration.\n");
193 reinit_completion(&priv
->status_return_completion
);
194 ret
= s10_svc_send_msg(priv
, COMMAND_RECONFIG
,
195 &ctype
, sizeof(ctype
));
199 ret
= wait_for_completion_interruptible_timeout(
200 &priv
->status_return_completion
, S10_RECONFIG_TIMEOUT
);
202 dev_err(dev
, "timeout waiting for RECONFIG_REQUEST\n");
207 dev_err(dev
, "error (%d) waiting for RECONFIG_REQUEST\n", ret
);
212 if (!test_and_clear_bit(SVC_STATUS_RECONFIG_REQUEST_OK
,
218 /* Allocate buffers from the service layer's pool. */
219 for (i
= 0; i
< NUM_SVC_BUFS
; i
++) {
220 kbuf
= stratix10_svc_allocate_memory(priv
->chan
, SVC_BUF_SIZE
);
222 s10_free_buffers(mgr
);
227 priv
->svc_bufs
[i
].buf
= kbuf
;
228 priv
->svc_bufs
[i
].lock
= 0;
232 stratix10_svc_done(priv
->chan
);
237 * s10_send_buf - send a buffer to the service layer queue
238 * mgr: fpga manager struct
239 * buf: fpga image buffer
240 * count: size of buf in bytes
241 * Returns # of bytes transferred or -ENOBUFS if the all the buffers are in use
242 * or if the service queue is full. Never returns 0.
244 static int s10_send_buf(struct fpga_manager
*mgr
, const char *buf
, size_t count
)
246 struct s10_priv
*priv
= mgr
->priv
;
247 struct device
*dev
= priv
->client
.dev
;
253 /* get/lock a buffer that that's not being used */
254 for (i
= 0; i
< NUM_SVC_BUFS
; i
++)
255 if (!test_and_set_bit_lock(SVC_BUF_LOCK
,
256 &priv
->svc_bufs
[i
].lock
))
259 if (i
== NUM_SVC_BUFS
)
262 xfer_sz
= count
< SVC_BUF_SIZE
? count
: SVC_BUF_SIZE
;
264 svc_buf
= priv
->svc_bufs
[i
].buf
;
265 memcpy(svc_buf
, buf
, xfer_sz
);
266 ret
= s10_svc_send_msg(priv
, COMMAND_RECONFIG_DATA_SUBMIT
,
270 "Error while sending data to service layer (%d)", ret
);
271 clear_bit_unlock(SVC_BUF_LOCK
, &priv
->svc_bufs
[i
].lock
);
279 * Send a FPGA image to privileged layers to write to the FPGA. When done
280 * sending, free all service layer buffers we allocated in write_init.
282 static int s10_ops_write(struct fpga_manager
*mgr
, const char *buf
,
285 struct s10_priv
*priv
= mgr
->priv
;
286 struct device
*dev
= priv
->client
.dev
;
292 * Loop waiting for buffers to be returned. When a buffer is returned,
293 * reuse it to send more data or free if if all data has been sent.
295 while (count
> 0 || s10_free_buffer_count(mgr
) != NUM_SVC_BUFS
) {
296 reinit_completion(&priv
->status_return_completion
);
299 sent
= s10_send_buf(mgr
, buf
, count
);
306 if (s10_free_buffers(mgr
))
309 ret
= s10_svc_send_msg(
310 priv
, COMMAND_RECONFIG_DATA_CLAIM
,
317 * If callback hasn't already happened, wait for buffers to be
318 * returned from service layer
320 wait_status
= 1; /* not timed out */
322 wait_status
= wait_for_completion_interruptible_timeout(
323 &priv
->status_return_completion
,
326 if (test_and_clear_bit(SVC_STATUS_RECONFIG_BUFFER_DONE
,
328 test_and_clear_bit(SVC_STATUS_RECONFIG_BUFFER_SUBMITTED
,
334 if (test_and_clear_bit(SVC_STATUS_RECONFIG_ERROR
,
336 dev_err(dev
, "ERROR - giving up - SVC_STATUS_RECONFIG_ERROR\n");
342 dev_err(dev
, "timeout waiting for svc layer buffers\n");
346 if (wait_status
< 0) {
349 "error (%d) waiting for svc layer buffers\n",
355 if (!s10_free_buffers(mgr
))
356 dev_err(dev
, "%s not all buffers were freed\n", __func__
);
361 static int s10_ops_write_complete(struct fpga_manager
*mgr
,
362 struct fpga_image_info
*info
)
364 struct s10_priv
*priv
= mgr
->priv
;
365 struct device
*dev
= priv
->client
.dev
;
366 unsigned long timeout
;
369 timeout
= usecs_to_jiffies(info
->config_complete_timeout_us
);
372 reinit_completion(&priv
->status_return_completion
);
374 ret
= s10_svc_send_msg(priv
, COMMAND_RECONFIG_STATUS
, NULL
, 0);
378 ret
= wait_for_completion_interruptible_timeout(
379 &priv
->status_return_completion
, timeout
);
382 "timeout waiting for RECONFIG_COMPLETED\n");
388 "error (%d) waiting for RECONFIG_COMPLETED\n",
392 /* Not error or timeout, so ret is # of jiffies until timeout */
396 if (test_and_clear_bit(SVC_STATUS_RECONFIG_COMPLETED
,
400 if (test_and_clear_bit(SVC_STATUS_RECONFIG_ERROR
,
402 dev_err(dev
, "ERROR - giving up - SVC_STATUS_RECONFIG_ERROR\n");
408 stratix10_svc_done(priv
->chan
);
413 static enum fpga_mgr_states
s10_ops_state(struct fpga_manager
*mgr
)
415 return FPGA_MGR_STATE_UNKNOWN
;
418 static const struct fpga_manager_ops s10_ops
= {
419 .state
= s10_ops_state
,
420 .write_init
= s10_ops_write_init
,
421 .write
= s10_ops_write
,
422 .write_complete
= s10_ops_write_complete
,
425 static int s10_probe(struct platform_device
*pdev
)
427 struct device
*dev
= &pdev
->dev
;
428 struct s10_priv
*priv
;
429 struct fpga_manager
*mgr
;
432 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
436 priv
->client
.dev
= dev
;
437 priv
->client
.receive_cb
= s10_receive_callback
;
438 priv
->client
.priv
= priv
;
440 priv
->chan
= stratix10_svc_request_channel_byname(&priv
->client
,
442 if (IS_ERR(priv
->chan
)) {
443 dev_err(dev
, "couldn't get service channel (%s)\n",
445 return PTR_ERR(priv
->chan
);
448 init_completion(&priv
->status_return_completion
);
450 mgr
= fpga_mgr_create(dev
, "Stratix10 SOC FPGA Manager",
453 dev_err(dev
, "unable to create FPGA manager\n");
458 ret
= fpga_mgr_register(mgr
);
460 dev_err(dev
, "unable to register FPGA manager\n");
465 platform_set_drvdata(pdev
, mgr
);
469 stratix10_svc_free_channel(priv
->chan
);
473 static int s10_remove(struct platform_device
*pdev
)
475 struct fpga_manager
*mgr
= platform_get_drvdata(pdev
);
476 struct s10_priv
*priv
= mgr
->priv
;
478 fpga_mgr_unregister(mgr
);
479 stratix10_svc_free_channel(priv
->chan
);
484 static const struct of_device_id s10_of_match
[] = {
485 { .compatible
= "intel,stratix10-soc-fpga-mgr", },
489 MODULE_DEVICE_TABLE(of
, s10_of_match
);
491 static struct platform_driver s10_driver
= {
493 .remove
= s10_remove
,
495 .name
= "Stratix10 SoC FPGA manager",
496 .of_match_table
= of_match_ptr(s10_of_match
),
500 static int __init
s10_init(void)
502 struct device_node
*fw_np
;
503 struct device_node
*np
;
506 fw_np
= of_find_node_by_name(NULL
, "svc");
511 np
= of_find_matching_node(fw_np
, s10_of_match
);
518 ret
= of_platform_populate(fw_np
, s10_of_match
, NULL
, NULL
);
523 return platform_driver_register(&s10_driver
);
526 static void __exit
s10_exit(void)
528 return platform_driver_unregister(&s10_driver
);
531 module_init(s10_init
);
532 module_exit(s10_exit
);
534 MODULE_AUTHOR("Alan Tull <atull@kernel.org>");
535 MODULE_DESCRIPTION("Intel Stratix 10 SOC FPGA Manager");
536 MODULE_LICENSE("GPL v2");