1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018-2019, Intel Corporation
6 #include <linux/arm-smccc.h>
7 #include <linux/bitfield.h>
8 #include <linux/completion.h>
9 #include <linux/kobject.h>
10 #include <linux/module.h>
11 #include <linux/mutex.h>
13 #include <linux/of_platform.h>
14 #include <linux/platform_device.h>
15 #include <linux/firmware/intel/stratix10-svc-client.h>
16 #include <linux/string.h>
17 #include <linux/sysfs.h>
19 #define RSU_STATE_MASK GENMASK_ULL(31, 0)
20 #define RSU_VERSION_MASK GENMASK_ULL(63, 32)
21 #define RSU_ERROR_LOCATION_MASK GENMASK_ULL(31, 0)
22 #define RSU_ERROR_DETAIL_MASK GENMASK_ULL(63, 32)
24 #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
26 #define INVALID_RETRY_COUNTER 0xFFFFFFFF
28 typedef void (*rsu_callback
)(struct stratix10_svc_client
*client
,
29 struct stratix10_svc_cb_data
*data
);
31 * struct stratix10_rsu_priv - rsu data structure
32 * @chan: pointer to the allocated service channel
33 * @client: active service client
34 * @completion: state for callback completion
35 * @lock: a mutex to protect callback completion state
36 * @status.current_image: address of image currently running in flash
37 * @status.fail_image: address of failed image in flash
38 * @status.version: the version number of RSU firmware
39 * @status.state: the state of RSU system
40 * @status.error_details: error code
41 * @status.error_location: the error offset inside the image that failed
42 * @retry_counter: the current image's retry counter
44 struct stratix10_rsu_priv
{
45 struct stratix10_svc_chan
*chan
;
46 struct stratix10_svc_client client
;
47 struct completion completion
;
50 unsigned long current_image
;
51 unsigned long fail_image
;
54 unsigned int error_details
;
55 unsigned int error_location
;
57 unsigned int retry_counter
;
61 * rsu_status_callback() - Status callback from Intel Service Layer
62 * @client: pointer to service client
63 * @data: pointer to callback data structure
65 * Callback from Intel service layer for RSU status request. Status is
66 * only updated after a system reboot, so a get updated status call is
67 * made during driver probe.
69 static void rsu_status_callback(struct stratix10_svc_client
*client
,
70 struct stratix10_svc_cb_data
*data
)
72 struct stratix10_rsu_priv
*priv
= client
->priv
;
73 struct arm_smccc_res
*res
= (struct arm_smccc_res
*)data
->kaddr1
;
75 if (data
->status
== BIT(SVC_STATUS_RSU_OK
)) {
76 priv
->status
.version
= FIELD_GET(RSU_VERSION_MASK
,
78 priv
->status
.state
= FIELD_GET(RSU_STATE_MASK
, res
->a2
);
79 priv
->status
.fail_image
= res
->a1
;
80 priv
->status
.current_image
= res
->a0
;
81 priv
->status
.error_location
=
82 FIELD_GET(RSU_ERROR_LOCATION_MASK
, res
->a3
);
83 priv
->status
.error_details
=
84 FIELD_GET(RSU_ERROR_DETAIL_MASK
, res
->a3
);
86 dev_err(client
->dev
, "COMMAND_RSU_STATUS returned 0x%lX\n",
88 priv
->status
.version
= 0;
89 priv
->status
.state
= 0;
90 priv
->status
.fail_image
= 0;
91 priv
->status
.current_image
= 0;
92 priv
->status
.error_location
= 0;
93 priv
->status
.error_details
= 0;
96 complete(&priv
->completion
);
100 * rsu_command_callback() - Update callback from Intel Service Layer
101 * @client: pointer to client
102 * @data: pointer to callback data structure
104 * Callback from Intel service layer for RSU commands.
106 static void rsu_command_callback(struct stratix10_svc_client
*client
,
107 struct stratix10_svc_cb_data
*data
)
109 struct stratix10_rsu_priv
*priv
= client
->priv
;
111 if (data
->status
== BIT(SVC_STATUS_RSU_NO_SUPPORT
))
112 dev_warn(client
->dev
, "Secure FW doesn't support notify\n");
113 else if (data
->status
== BIT(SVC_STATUS_RSU_ERROR
))
114 dev_err(client
->dev
, "Failure, returned status is %lu\n",
117 complete(&priv
->completion
);
121 * rsu_retry_callback() - Callback from Intel service layer for getting
122 * the current image's retry counter from firmware
123 * @client: pointer to client
124 * @data: pointer to callback data structure
126 * Callback from Intel service layer for retry counter, which is used by
127 * user to know how many times the images is still allowed to reload
128 * itself before giving up and starting RSU fail-over flow.
130 static void rsu_retry_callback(struct stratix10_svc_client
*client
,
131 struct stratix10_svc_cb_data
*data
)
133 struct stratix10_rsu_priv
*priv
= client
->priv
;
134 unsigned int *counter
= (unsigned int *)data
->kaddr1
;
136 if (data
->status
== BIT(SVC_STATUS_RSU_OK
))
137 priv
->retry_counter
= *counter
;
138 else if (data
->status
== BIT(SVC_STATUS_RSU_NO_SUPPORT
))
139 dev_warn(client
->dev
, "Secure FW doesn't support retry\n");
141 dev_err(client
->dev
, "Failed to get retry counter %lu\n",
144 complete(&priv
->completion
);
148 * rsu_send_msg() - send a message to Intel service layer
149 * @priv: pointer to rsu private data
150 * @command: RSU status or update command
151 * @arg: the request argument, the bitstream address or notify status
152 * @callback: function pointer for the callback (status or update)
154 * Start an Intel service layer transaction to perform the SMC call that
155 * is necessary to get RSU boot log or set the address of bitstream to
158 * Returns 0 on success or -ETIMEDOUT on error.
160 static int rsu_send_msg(struct stratix10_rsu_priv
*priv
,
161 enum stratix10_svc_command_code command
,
163 rsu_callback callback
)
165 struct stratix10_svc_client_msg msg
;
168 mutex_lock(&priv
->lock
);
169 reinit_completion(&priv
->completion
);
170 priv
->client
.receive_cb
= callback
;
172 msg
.command
= command
;
176 ret
= stratix10_svc_send(priv
->chan
, &msg
);
180 ret
= wait_for_completion_interruptible_timeout(&priv
->completion
,
183 dev_err(priv
->client
.dev
,
184 "timeout waiting for SMC call\n");
187 } else if (ret
< 0) {
188 dev_err(priv
->client
.dev
,
189 "error %d waiting for SMC call\n", ret
);
196 stratix10_svc_done(priv
->chan
);
197 mutex_unlock(&priv
->lock
);
202 * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
203 * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
204 * related. They allow user space software to query the configuration system
205 * status and to request optional reboot behavior specific to Intel FPGAs.
208 static ssize_t
current_image_show(struct device
*dev
,
209 struct device_attribute
*attr
, char *buf
)
211 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
216 return sprintf(buf
, "0x%08lx\n", priv
->status
.current_image
);
219 static ssize_t
fail_image_show(struct device
*dev
,
220 struct device_attribute
*attr
, char *buf
)
222 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
227 return sprintf(buf
, "0x%08lx\n", priv
->status
.fail_image
);
230 static ssize_t
version_show(struct device
*dev
, struct device_attribute
*attr
,
233 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
238 return sprintf(buf
, "0x%08x\n", priv
->status
.version
);
241 static ssize_t
state_show(struct device
*dev
, struct device_attribute
*attr
,
244 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
249 return sprintf(buf
, "0x%08x\n", priv
->status
.state
);
252 static ssize_t
error_location_show(struct device
*dev
,
253 struct device_attribute
*attr
, char *buf
)
255 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
260 return sprintf(buf
, "0x%08x\n", priv
->status
.error_location
);
263 static ssize_t
error_details_show(struct device
*dev
,
264 struct device_attribute
*attr
, char *buf
)
266 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
271 return sprintf(buf
, "0x%08x\n", priv
->status
.error_details
);
274 static ssize_t
retry_counter_show(struct device
*dev
,
275 struct device_attribute
*attr
, char *buf
)
277 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
282 return sprintf(buf
, "0x%08x\n", priv
->retry_counter
);
285 static ssize_t
reboot_image_store(struct device
*dev
,
286 struct device_attribute
*attr
,
287 const char *buf
, size_t count
)
289 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
290 unsigned long address
;
296 ret
= kstrtoul(buf
, 0, &address
);
300 ret
= rsu_send_msg(priv
, COMMAND_RSU_UPDATE
,
301 address
, rsu_command_callback
);
303 dev_err(dev
, "Error, RSU update returned %i\n", ret
);
310 static ssize_t
notify_store(struct device
*dev
,
311 struct device_attribute
*attr
,
312 const char *buf
, size_t count
)
314 struct stratix10_rsu_priv
*priv
= dev_get_drvdata(dev
);
315 unsigned long status
;
321 ret
= kstrtoul(buf
, 0, &status
);
325 ret
= rsu_send_msg(priv
, COMMAND_RSU_NOTIFY
,
326 status
, rsu_command_callback
);
328 dev_err(dev
, "Error, RSU notify returned %i\n", ret
);
332 /* to get the updated state */
333 ret
= rsu_send_msg(priv
, COMMAND_RSU_STATUS
,
334 0, rsu_status_callback
);
336 dev_err(dev
, "Error, getting RSU status %i\n", ret
);
340 ret
= rsu_send_msg(priv
, COMMAND_RSU_RETRY
, 0, rsu_retry_callback
);
342 dev_err(dev
, "Error, getting RSU retry %i\n", ret
);
349 static DEVICE_ATTR_RO(current_image
);
350 static DEVICE_ATTR_RO(fail_image
);
351 static DEVICE_ATTR_RO(state
);
352 static DEVICE_ATTR_RO(version
);
353 static DEVICE_ATTR_RO(error_location
);
354 static DEVICE_ATTR_RO(error_details
);
355 static DEVICE_ATTR_RO(retry_counter
);
356 static DEVICE_ATTR_WO(reboot_image
);
357 static DEVICE_ATTR_WO(notify
);
359 static struct attribute
*rsu_attrs
[] = {
360 &dev_attr_current_image
.attr
,
361 &dev_attr_fail_image
.attr
,
362 &dev_attr_state
.attr
,
363 &dev_attr_version
.attr
,
364 &dev_attr_error_location
.attr
,
365 &dev_attr_error_details
.attr
,
366 &dev_attr_retry_counter
.attr
,
367 &dev_attr_reboot_image
.attr
,
368 &dev_attr_notify
.attr
,
372 ATTRIBUTE_GROUPS(rsu
);
374 static int stratix10_rsu_probe(struct platform_device
*pdev
)
376 struct device
*dev
= &pdev
->dev
;
377 struct stratix10_rsu_priv
*priv
;
380 priv
= devm_kzalloc(dev
, sizeof(*priv
), GFP_KERNEL
);
384 priv
->client
.dev
= dev
;
385 priv
->client
.receive_cb
= NULL
;
386 priv
->client
.priv
= priv
;
387 priv
->status
.current_image
= 0;
388 priv
->status
.fail_image
= 0;
389 priv
->status
.error_location
= 0;
390 priv
->status
.error_details
= 0;
391 priv
->status
.version
= 0;
392 priv
->status
.state
= 0;
393 priv
->retry_counter
= INVALID_RETRY_COUNTER
;
395 mutex_init(&priv
->lock
);
396 priv
->chan
= stratix10_svc_request_channel_byname(&priv
->client
,
398 if (IS_ERR(priv
->chan
)) {
399 dev_err(dev
, "couldn't get service channel %s\n",
401 return PTR_ERR(priv
->chan
);
404 init_completion(&priv
->completion
);
405 platform_set_drvdata(pdev
, priv
);
407 /* get the initial state from firmware */
408 ret
= rsu_send_msg(priv
, COMMAND_RSU_STATUS
,
409 0, rsu_status_callback
);
411 dev_err(dev
, "Error, getting RSU status %i\n", ret
);
412 stratix10_svc_free_channel(priv
->chan
);
415 ret
= rsu_send_msg(priv
, COMMAND_RSU_RETRY
, 0, rsu_retry_callback
);
417 dev_err(dev
, "Error, getting RSU retry %i\n", ret
);
418 stratix10_svc_free_channel(priv
->chan
);
424 static int stratix10_rsu_remove(struct platform_device
*pdev
)
426 struct stratix10_rsu_priv
*priv
= platform_get_drvdata(pdev
);
428 stratix10_svc_free_channel(priv
->chan
);
432 static struct platform_driver stratix10_rsu_driver
= {
433 .probe
= stratix10_rsu_probe
,
434 .remove
= stratix10_rsu_remove
,
436 .name
= "stratix10-rsu",
437 .dev_groups
= rsu_groups
,
441 module_platform_driver(stratix10_rsu_driver
);
443 MODULE_LICENSE("GPL v2");
444 MODULE_DESCRIPTION("Intel Remote System Update Driver");
445 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");