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)
23 #define RSU_DCMF0_MASK GENMASK_ULL(31, 0)
24 #define RSU_DCMF1_MASK GENMASK_ULL(63, 32)
25 #define RSU_DCMF2_MASK GENMASK_ULL(31, 0)
26 #define RSU_DCMF3_MASK GENMASK_ULL(63, 32)
28 #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
30 #define INVALID_RETRY_COUNTER 0xFF
31 #define INVALID_DCMF_VERSION 0xFF
34 typedef void (*rsu_callback)(struct stratix10_svc_client *client,
35 struct stratix10_svc_cb_data *data);
37 * struct stratix10_rsu_priv - rsu data structure
38 * @chan: pointer to the allocated service channel
39 * @client: active service client
40 * @completion: state for callback completion
41 * @lock: a mutex to protect callback completion state
42 * @status.current_image: address of image currently running in flash
43 * @status.fail_image: address of failed image in flash
44 * @status.version: the interface version number of RSU firmware
45 * @status.state: the state of RSU system
46 * @status.error_details: error code
47 * @status.error_location: the error offset inside the image that failed
48 * @dcmf_version.dcmf0: Quartus dcmf0 version
49 * @dcmf_version.dcmf1: Quartus dcmf1 version
50 * @dcmf_version.dcmf2: Quartus dcmf2 version
51 * @dcmf_version.dcmf3: Quartus dcmf3 version
52 * @retry_counter: the current image's retry counter
53 * @max_retry: the preset max retry value
55 struct stratix10_rsu_priv {
56 struct stratix10_svc_chan *chan;
57 struct stratix10_svc_client client;
58 struct completion completion;
61 unsigned long current_image;
62 unsigned long fail_image;
65 unsigned int error_details;
66 unsigned int error_location;
76 unsigned int retry_counter;
77 unsigned int max_retry;
81 * rsu_status_callback() - Status callback from Intel Service Layer
82 * @client: pointer to service client
83 * @data: pointer to callback data structure
85 * Callback from Intel service layer for RSU status request. Status is
86 * only updated after a system reboot, so a get updated status call is
87 * made during driver probe.
89 static void rsu_status_callback(struct stratix10_svc_client *client,
90 struct stratix10_svc_cb_data *data)
92 struct stratix10_rsu_priv *priv = client->priv;
93 struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;
95 if (data->status == BIT(SVC_STATUS_OK)) {
96 priv->status.version = FIELD_GET(RSU_VERSION_MASK,
98 priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
99 priv->status.fail_image = res->a1;
100 priv->status.current_image = res->a0;
101 priv->status.error_location =
102 FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
103 priv->status.error_details =
104 FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
106 dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
108 priv->status.version = 0;
109 priv->status.state = 0;
110 priv->status.fail_image = 0;
111 priv->status.current_image = 0;
112 priv->status.error_location = 0;
113 priv->status.error_details = 0;
116 complete(&priv->completion);
120 * rsu_command_callback() - Update callback from Intel Service Layer
121 * @client: pointer to client
122 * @data: pointer to callback data structure
124 * Callback from Intel service layer for RSU commands.
126 static void rsu_command_callback(struct stratix10_svc_client *client,
127 struct stratix10_svc_cb_data *data)
129 struct stratix10_rsu_priv *priv = client->priv;
131 if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
132 dev_warn(client->dev, "FW doesn't support notify\n");
133 else if (data->status == BIT(SVC_STATUS_ERROR))
134 dev_err(client->dev, "Failure, returned status is %lu\n",
137 complete(&priv->completion);
141 * rsu_retry_callback() - Callback from Intel service layer for getting
142 * the current image's retry counter from the firmware
143 * @client: pointer to client
144 * @data: pointer to callback data structure
146 * Callback from Intel service layer for retry counter, which is used by
147 * user to know how many times the images is still allowed to reload
148 * itself before giving up and starting RSU fail-over flow.
150 static void rsu_retry_callback(struct stratix10_svc_client *client,
151 struct stratix10_svc_cb_data *data)
153 struct stratix10_rsu_priv *priv = client->priv;
154 unsigned int *counter = (unsigned int *)data->kaddr1;
156 if (data->status == BIT(SVC_STATUS_OK))
157 priv->retry_counter = *counter;
158 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
159 dev_warn(client->dev, "FW doesn't support retry\n");
161 dev_err(client->dev, "Failed to get retry counter %lu\n",
164 complete(&priv->completion);
168 * rsu_max_retry_callback() - Callback from Intel service layer for getting
169 * the max retry value from the firmware
170 * @client: pointer to client
171 * @data: pointer to callback data structure
173 * Callback from Intel service layer for max retry.
175 static void rsu_max_retry_callback(struct stratix10_svc_client *client,
176 struct stratix10_svc_cb_data *data)
178 struct stratix10_rsu_priv *priv = client->priv;
179 unsigned int *max_retry = (unsigned int *)data->kaddr1;
181 if (data->status == BIT(SVC_STATUS_OK))
182 priv->max_retry = *max_retry;
183 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
184 dev_warn(client->dev, "FW doesn't support max retry\n");
186 dev_err(client->dev, "Failed to get max retry %lu\n",
189 complete(&priv->completion);
193 * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
195 * @client: pointer to client
196 * @data: pointer to callback data structure
198 * Callback from Intel service layer for DCMF version number
200 static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
201 struct stratix10_svc_cb_data *data)
203 struct stratix10_rsu_priv *priv = client->priv;
204 unsigned long long *value1 = (unsigned long long *)data->kaddr1;
205 unsigned long long *value2 = (unsigned long long *)data->kaddr2;
207 if (data->status == BIT(SVC_STATUS_OK)) {
208 priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
209 priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
210 priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
211 priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
213 dev_err(client->dev, "failed to get DCMF version\n");
215 complete(&priv->completion);
219 * rsu_send_msg() - send a message to Intel service layer
220 * @priv: pointer to rsu private data
221 * @command: RSU status or update command
222 * @arg: the request argument, the bitstream address or notify status
223 * @callback: function pointer for the callback (status or update)
225 * Start an Intel service layer transaction to perform the SMC call that
226 * is necessary to get RSU boot log or set the address of bitstream to
229 * Returns 0 on success or -ETIMEDOUT on error.
231 static int rsu_send_msg(struct stratix10_rsu_priv *priv,
232 enum stratix10_svc_command_code command,
234 rsu_callback callback)
236 struct stratix10_svc_client_msg msg;
239 mutex_lock(&priv->lock);
240 reinit_completion(&priv->completion);
241 priv->client.receive_cb = callback;
243 msg.command = command;
247 ret = stratix10_svc_send(priv->chan, &msg);
251 ret = wait_for_completion_interruptible_timeout(&priv->completion,
254 dev_err(priv->client.dev,
255 "timeout waiting for SMC call\n");
258 } else if (ret < 0) {
259 dev_err(priv->client.dev,
260 "error %d waiting for SMC call\n", ret);
267 stratix10_svc_done(priv->chan);
268 mutex_unlock(&priv->lock);
273 * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
274 * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
275 * related. They allow user space software to query the configuration system
276 * status and to request optional reboot behavior specific to Intel FPGAs.
279 static ssize_t current_image_show(struct device *dev,
280 struct device_attribute *attr, char *buf)
282 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
287 return sprintf(buf, "0x%08lx\n", priv->status.current_image);
290 static ssize_t fail_image_show(struct device *dev,
291 struct device_attribute *attr, char *buf)
293 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
298 return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
301 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
304 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
309 return sprintf(buf, "0x%08x\n", priv->status.version);
312 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
315 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
320 return sprintf(buf, "0x%08x\n", priv->status.state);
323 static ssize_t error_location_show(struct device *dev,
324 struct device_attribute *attr, char *buf)
326 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
331 return sprintf(buf, "0x%08x\n", priv->status.error_location);
334 static ssize_t error_details_show(struct device *dev,
335 struct device_attribute *attr, char *buf)
337 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
342 return sprintf(buf, "0x%08x\n", priv->status.error_details);
345 static ssize_t retry_counter_show(struct device *dev,
346 struct device_attribute *attr, char *buf)
348 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
353 return sprintf(buf, "0x%08x\n", priv->retry_counter);
356 static ssize_t max_retry_show(struct device *dev,
357 struct device_attribute *attr, char *buf)
359 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
364 return sprintf(buf, "0x%08x\n", priv->max_retry);
367 static ssize_t dcmf0_show(struct device *dev,
368 struct device_attribute *attr, char *buf)
370 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
375 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
378 static ssize_t dcmf1_show(struct device *dev,
379 struct device_attribute *attr, char *buf)
381 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
386 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
389 static ssize_t dcmf2_show(struct device *dev,
390 struct device_attribute *attr, char *buf)
392 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
397 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
400 static ssize_t dcmf3_show(struct device *dev,
401 struct device_attribute *attr, char *buf)
403 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
408 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
411 static ssize_t reboot_image_store(struct device *dev,
412 struct device_attribute *attr,
413 const char *buf, size_t count)
415 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
416 unsigned long address;
422 ret = kstrtoul(buf, 0, &address);
426 ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
427 address, rsu_command_callback);
429 dev_err(dev, "Error, RSU update returned %i\n", ret);
436 static ssize_t notify_store(struct device *dev,
437 struct device_attribute *attr,
438 const char *buf, size_t count)
440 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
441 unsigned long status;
447 ret = kstrtoul(buf, 0, &status);
451 ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY,
452 status, rsu_command_callback);
454 dev_err(dev, "Error, RSU notify returned %i\n", ret);
458 /* to get the updated state */
459 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
460 0, rsu_status_callback);
462 dev_err(dev, "Error, getting RSU status %i\n", ret);
466 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
468 dev_err(dev, "Error, getting RSU retry %i\n", ret);
475 static DEVICE_ATTR_RO(current_image);
476 static DEVICE_ATTR_RO(fail_image);
477 static DEVICE_ATTR_RO(state);
478 static DEVICE_ATTR_RO(version);
479 static DEVICE_ATTR_RO(error_location);
480 static DEVICE_ATTR_RO(error_details);
481 static DEVICE_ATTR_RO(retry_counter);
482 static DEVICE_ATTR_RO(max_retry);
483 static DEVICE_ATTR_RO(dcmf0);
484 static DEVICE_ATTR_RO(dcmf1);
485 static DEVICE_ATTR_RO(dcmf2);
486 static DEVICE_ATTR_RO(dcmf3);
487 static DEVICE_ATTR_WO(reboot_image);
488 static DEVICE_ATTR_WO(notify);
490 static struct attribute *rsu_attrs[] = {
491 &dev_attr_current_image.attr,
492 &dev_attr_fail_image.attr,
493 &dev_attr_state.attr,
494 &dev_attr_version.attr,
495 &dev_attr_error_location.attr,
496 &dev_attr_error_details.attr,
497 &dev_attr_retry_counter.attr,
498 &dev_attr_max_retry.attr,
499 &dev_attr_dcmf0.attr,
500 &dev_attr_dcmf1.attr,
501 &dev_attr_dcmf2.attr,
502 &dev_attr_dcmf3.attr,
503 &dev_attr_reboot_image.attr,
504 &dev_attr_notify.attr,
508 ATTRIBUTE_GROUPS(rsu);
510 static int stratix10_rsu_probe(struct platform_device *pdev)
512 struct device *dev = &pdev->dev;
513 struct stratix10_rsu_priv *priv;
516 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
520 priv->client.dev = dev;
521 priv->client.receive_cb = NULL;
522 priv->client.priv = priv;
523 priv->status.current_image = 0;
524 priv->status.fail_image = 0;
525 priv->status.error_location = 0;
526 priv->status.error_details = 0;
527 priv->status.version = 0;
528 priv->status.state = 0;
529 priv->retry_counter = INVALID_RETRY_COUNTER;
530 priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
531 priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
532 priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
533 priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
534 priv->max_retry = INVALID_RETRY_COUNTER;
536 mutex_init(&priv->lock);
537 priv->chan = stratix10_svc_request_channel_byname(&priv->client,
539 if (IS_ERR(priv->chan)) {
540 dev_err(dev, "couldn't get service channel %s\n",
542 return PTR_ERR(priv->chan);
545 init_completion(&priv->completion);
546 platform_set_drvdata(pdev, priv);
548 /* get the initial state from firmware */
549 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
550 0, rsu_status_callback);
552 dev_err(dev, "Error, getting RSU status %i\n", ret);
553 stratix10_svc_free_channel(priv->chan);
556 /* get DCMF version from firmware */
557 ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
558 0, rsu_dcmf_version_callback);
560 dev_err(dev, "Error, getting DCMF version %i\n", ret);
561 stratix10_svc_free_channel(priv->chan);
564 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
566 dev_err(dev, "Error, getting RSU retry %i\n", ret);
567 stratix10_svc_free_channel(priv->chan);
570 ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
571 rsu_max_retry_callback);
573 dev_err(dev, "Error, getting RSU max retry %i\n", ret);
574 stratix10_svc_free_channel(priv->chan);
580 static int stratix10_rsu_remove(struct platform_device *pdev)
582 struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
584 stratix10_svc_free_channel(priv->chan);
588 static struct platform_driver stratix10_rsu_driver = {
589 .probe = stratix10_rsu_probe,
590 .remove = stratix10_rsu_remove,
592 .name = "stratix10-rsu",
593 .dev_groups = rsu_groups,
597 module_platform_driver(stratix10_rsu_driver);
599 MODULE_LICENSE("GPL v2");
600 MODULE_DESCRIPTION("Intel Remote System Update Driver");