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/platform_device.h>
14 #include <linux/firmware/intel/stratix10-svc-client.h>
15 #include <linux/string.h>
16 #include <linux/sysfs.h>
18 #define RSU_STATE_MASK GENMASK_ULL(31, 0)
19 #define RSU_VERSION_MASK GENMASK_ULL(63, 32)
20 #define RSU_ERROR_LOCATION_MASK GENMASK_ULL(31, 0)
21 #define RSU_ERROR_DETAIL_MASK GENMASK_ULL(63, 32)
22 #define RSU_DCMF0_MASK GENMASK_ULL(31, 0)
23 #define RSU_DCMF1_MASK GENMASK_ULL(63, 32)
24 #define RSU_DCMF2_MASK GENMASK_ULL(31, 0)
25 #define RSU_DCMF3_MASK GENMASK_ULL(63, 32)
26 #define RSU_DCMF0_STATUS_MASK GENMASK_ULL(15, 0)
27 #define RSU_DCMF1_STATUS_MASK GENMASK_ULL(31, 16)
28 #define RSU_DCMF2_STATUS_MASK GENMASK_ULL(47, 32)
29 #define RSU_DCMF3_STATUS_MASK GENMASK_ULL(63, 48)
31 #define RSU_TIMEOUT (msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
33 #define INVALID_RETRY_COUNTER 0xFF
34 #define INVALID_DCMF_VERSION 0xFF
35 #define INVALID_DCMF_STATUS 0xFFFFFFFF
37 typedef void (*rsu_callback)(struct stratix10_svc_client *client,
38 struct stratix10_svc_cb_data *data);
40 * struct stratix10_rsu_priv - rsu data structure
41 * @chan: pointer to the allocated service channel
42 * @client: active service client
43 * @completion: state for callback completion
44 * @lock: a mutex to protect callback completion state
45 * @status.current_image: address of image currently running in flash
46 * @status.fail_image: address of failed image in flash
47 * @status.version: the interface version number of RSU firmware
48 * @status.state: the state of RSU system
49 * @status.error_details: error code
50 * @status.error_location: the error offset inside the image that failed
51 * @dcmf_version.dcmf0: Quartus dcmf0 version
52 * @dcmf_version.dcmf1: Quartus dcmf1 version
53 * @dcmf_version.dcmf2: Quartus dcmf2 version
54 * @dcmf_version.dcmf3: Quartus dcmf3 version
55 * @dcmf_status.dcmf0: dcmf0 status
56 * @dcmf_status.dcmf1: dcmf1 status
57 * @dcmf_status.dcmf2: dcmf2 status
58 * @dcmf_status.dcmf3: dcmf3 status
59 * @retry_counter: the current image's retry counter
60 * @max_retry: the preset max retry value
62 struct stratix10_rsu_priv {
63 struct stratix10_svc_chan *chan;
64 struct stratix10_svc_client client;
65 struct completion completion;
68 unsigned long current_image;
69 unsigned long fail_image;
72 unsigned int error_details;
73 unsigned int error_location;
90 unsigned int retry_counter;
91 unsigned int max_retry;
95 * rsu_status_callback() - Status callback from Intel Service Layer
96 * @client: pointer to service client
97 * @data: pointer to callback data structure
99 * Callback from Intel service layer for RSU status request. Status is
100 * only updated after a system reboot, so a get updated status call is
101 * made during driver probe.
103 static void rsu_status_callback(struct stratix10_svc_client *client,
104 struct stratix10_svc_cb_data *data)
106 struct stratix10_rsu_priv *priv = client->priv;
107 struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;
109 if (data->status == BIT(SVC_STATUS_OK)) {
110 priv->status.version = FIELD_GET(RSU_VERSION_MASK,
112 priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
113 priv->status.fail_image = res->a1;
114 priv->status.current_image = res->a0;
115 priv->status.error_location =
116 FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
117 priv->status.error_details =
118 FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
120 dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
122 priv->status.version = 0;
123 priv->status.state = 0;
124 priv->status.fail_image = 0;
125 priv->status.current_image = 0;
126 priv->status.error_location = 0;
127 priv->status.error_details = 0;
130 complete(&priv->completion);
134 * rsu_command_callback() - Update callback from Intel Service Layer
135 * @client: pointer to client
136 * @data: pointer to callback data structure
138 * Callback from Intel service layer for RSU commands.
140 static void rsu_command_callback(struct stratix10_svc_client *client,
141 struct stratix10_svc_cb_data *data)
143 struct stratix10_rsu_priv *priv = client->priv;
145 if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
146 dev_warn(client->dev, "Secure FW doesn't support notify\n");
147 else if (data->status == BIT(SVC_STATUS_ERROR))
148 dev_err(client->dev, "Failure, returned status is %lu\n",
151 complete(&priv->completion);
155 * rsu_retry_callback() - Callback from Intel service layer for getting
156 * the current image's retry counter from the firmware
157 * @client: pointer to client
158 * @data: pointer to callback data structure
160 * Callback from Intel service layer for retry counter, which is used by
161 * user to know how many times the images is still allowed to reload
162 * itself before giving up and starting RSU fail-over flow.
164 static void rsu_retry_callback(struct stratix10_svc_client *client,
165 struct stratix10_svc_cb_data *data)
167 struct stratix10_rsu_priv *priv = client->priv;
168 unsigned int *counter = (unsigned int *)data->kaddr1;
170 if (data->status == BIT(SVC_STATUS_OK))
171 priv->retry_counter = *counter;
172 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
173 dev_warn(client->dev, "Secure FW doesn't support retry\n");
175 dev_err(client->dev, "Failed to get retry counter %lu\n",
178 complete(&priv->completion);
182 * rsu_max_retry_callback() - Callback from Intel service layer for getting
183 * the max retry value from the firmware
184 * @client: pointer to client
185 * @data: pointer to callback data structure
187 * Callback from Intel service layer for max retry.
189 static void rsu_max_retry_callback(struct stratix10_svc_client *client,
190 struct stratix10_svc_cb_data *data)
192 struct stratix10_rsu_priv *priv = client->priv;
193 unsigned int *max_retry = (unsigned int *)data->kaddr1;
195 if (data->status == BIT(SVC_STATUS_OK))
196 priv->max_retry = *max_retry;
197 else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
198 dev_warn(client->dev, "Secure FW doesn't support max retry\n");
200 dev_err(client->dev, "Failed to get max retry %lu\n",
203 complete(&priv->completion);
207 * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
209 * @client: pointer to client
210 * @data: pointer to callback data structure
212 * Callback from Intel service layer for DCMF version number
214 static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
215 struct stratix10_svc_cb_data *data)
217 struct stratix10_rsu_priv *priv = client->priv;
218 unsigned long long *value1 = (unsigned long long *)data->kaddr1;
219 unsigned long long *value2 = (unsigned long long *)data->kaddr2;
221 if (data->status == BIT(SVC_STATUS_OK)) {
222 priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
223 priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
224 priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
225 priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
227 dev_err(client->dev, "failed to get DCMF version\n");
229 complete(&priv->completion);
233 * rsu_dcmf_status_callback() - Callback from Intel service layer for getting
235 * @client: pointer to client
236 * @data: pointer to callback data structure
238 * Callback from Intel service layer for DCMF status
240 static void rsu_dcmf_status_callback(struct stratix10_svc_client *client,
241 struct stratix10_svc_cb_data *data)
243 struct stratix10_rsu_priv *priv = client->priv;
244 unsigned long long *value = (unsigned long long *)data->kaddr1;
246 if (data->status == BIT(SVC_STATUS_OK)) {
247 priv->dcmf_status.dcmf0 = FIELD_GET(RSU_DCMF0_STATUS_MASK,
249 priv->dcmf_status.dcmf1 = FIELD_GET(RSU_DCMF1_STATUS_MASK,
251 priv->dcmf_status.dcmf2 = FIELD_GET(RSU_DCMF2_STATUS_MASK,
253 priv->dcmf_status.dcmf3 = FIELD_GET(RSU_DCMF3_STATUS_MASK,
256 dev_err(client->dev, "failed to get DCMF status\n");
258 complete(&priv->completion);
262 * rsu_send_msg() - send a message to Intel service layer
263 * @priv: pointer to rsu private data
264 * @command: RSU status or update command
265 * @arg: the request argument, the bitstream address or notify status
266 * @callback: function pointer for the callback (status or update)
268 * Start an Intel service layer transaction to perform the SMC call that
269 * is necessary to get RSU boot log or set the address of bitstream to
272 * Returns 0 on success or -ETIMEDOUT on error.
274 static int rsu_send_msg(struct stratix10_rsu_priv *priv,
275 enum stratix10_svc_command_code command,
277 rsu_callback callback)
279 struct stratix10_svc_client_msg msg;
282 mutex_lock(&priv->lock);
283 reinit_completion(&priv->completion);
284 priv->client.receive_cb = callback;
286 msg.command = command;
290 ret = stratix10_svc_send(priv->chan, &msg);
294 ret = wait_for_completion_interruptible_timeout(&priv->completion,
297 dev_err(priv->client.dev,
298 "timeout waiting for SMC call\n");
301 } else if (ret < 0) {
302 dev_err(priv->client.dev,
303 "error %d waiting for SMC call\n", ret);
310 stratix10_svc_done(priv->chan);
311 mutex_unlock(&priv->lock);
316 * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
317 * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
318 * related. They allow user space software to query the configuration system
319 * status and to request optional reboot behavior specific to Intel FPGAs.
322 static ssize_t current_image_show(struct device *dev,
323 struct device_attribute *attr, char *buf)
325 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
330 return sprintf(buf, "0x%08lx\n", priv->status.current_image);
333 static ssize_t fail_image_show(struct device *dev,
334 struct device_attribute *attr, char *buf)
336 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
341 return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
344 static ssize_t version_show(struct device *dev, struct device_attribute *attr,
347 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
352 return sprintf(buf, "0x%08x\n", priv->status.version);
355 static ssize_t state_show(struct device *dev, struct device_attribute *attr,
358 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
363 return sprintf(buf, "0x%08x\n", priv->status.state);
366 static ssize_t error_location_show(struct device *dev,
367 struct device_attribute *attr, char *buf)
369 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
374 return sprintf(buf, "0x%08x\n", priv->status.error_location);
377 static ssize_t error_details_show(struct device *dev,
378 struct device_attribute *attr, char *buf)
380 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
385 return sprintf(buf, "0x%08x\n", priv->status.error_details);
388 static ssize_t retry_counter_show(struct device *dev,
389 struct device_attribute *attr, char *buf)
391 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
396 return sprintf(buf, "0x%08x\n", priv->retry_counter);
399 static ssize_t max_retry_show(struct device *dev,
400 struct device_attribute *attr, char *buf)
402 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
407 return scnprintf(buf, sizeof(priv->max_retry),
408 "0x%08x\n", priv->max_retry);
411 static ssize_t dcmf0_show(struct device *dev,
412 struct device_attribute *attr, char *buf)
414 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
419 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
422 static ssize_t dcmf1_show(struct device *dev,
423 struct device_attribute *attr, char *buf)
425 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
430 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
433 static ssize_t dcmf2_show(struct device *dev,
434 struct device_attribute *attr, char *buf)
436 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
441 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
444 static ssize_t dcmf3_show(struct device *dev,
445 struct device_attribute *attr, char *buf)
447 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
452 return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
455 static ssize_t dcmf0_status_show(struct device *dev,
456 struct device_attribute *attr, char *buf)
458 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
463 if (priv->dcmf_status.dcmf0 == INVALID_DCMF_STATUS)
466 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf0);
469 static ssize_t dcmf1_status_show(struct device *dev,
470 struct device_attribute *attr, char *buf)
472 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
477 if (priv->dcmf_status.dcmf1 == INVALID_DCMF_STATUS)
480 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf1);
483 static ssize_t dcmf2_status_show(struct device *dev,
484 struct device_attribute *attr, char *buf)
486 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
491 if (priv->dcmf_status.dcmf2 == INVALID_DCMF_STATUS)
494 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf2);
497 static ssize_t dcmf3_status_show(struct device *dev,
498 struct device_attribute *attr, char *buf)
500 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
505 if (priv->dcmf_status.dcmf3 == INVALID_DCMF_STATUS)
508 return sprintf(buf, "0x%08x\n", priv->dcmf_status.dcmf3);
510 static ssize_t reboot_image_store(struct device *dev,
511 struct device_attribute *attr,
512 const char *buf, size_t count)
514 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
515 unsigned long address;
521 ret = kstrtoul(buf, 0, &address);
525 ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
526 address, rsu_command_callback);
528 dev_err(dev, "Error, RSU update returned %i\n", ret);
535 static ssize_t notify_store(struct device *dev,
536 struct device_attribute *attr,
537 const char *buf, size_t count)
539 struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
540 unsigned long status;
546 ret = kstrtoul(buf, 0, &status);
550 ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY,
551 status, rsu_command_callback);
553 dev_err(dev, "Error, RSU notify returned %i\n", ret);
557 /* to get the updated state */
558 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
559 0, rsu_status_callback);
561 dev_err(dev, "Error, getting RSU status %i\n", ret);
565 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
567 dev_err(dev, "Error, getting RSU retry %i\n", ret);
574 static DEVICE_ATTR_RO(current_image);
575 static DEVICE_ATTR_RO(fail_image);
576 static DEVICE_ATTR_RO(state);
577 static DEVICE_ATTR_RO(version);
578 static DEVICE_ATTR_RO(error_location);
579 static DEVICE_ATTR_RO(error_details);
580 static DEVICE_ATTR_RO(retry_counter);
581 static DEVICE_ATTR_RO(max_retry);
582 static DEVICE_ATTR_RO(dcmf0);
583 static DEVICE_ATTR_RO(dcmf1);
584 static DEVICE_ATTR_RO(dcmf2);
585 static DEVICE_ATTR_RO(dcmf3);
586 static DEVICE_ATTR_RO(dcmf0_status);
587 static DEVICE_ATTR_RO(dcmf1_status);
588 static DEVICE_ATTR_RO(dcmf2_status);
589 static DEVICE_ATTR_RO(dcmf3_status);
590 static DEVICE_ATTR_WO(reboot_image);
591 static DEVICE_ATTR_WO(notify);
593 static struct attribute *rsu_attrs[] = {
594 &dev_attr_current_image.attr,
595 &dev_attr_fail_image.attr,
596 &dev_attr_state.attr,
597 &dev_attr_version.attr,
598 &dev_attr_error_location.attr,
599 &dev_attr_error_details.attr,
600 &dev_attr_retry_counter.attr,
601 &dev_attr_max_retry.attr,
602 &dev_attr_dcmf0.attr,
603 &dev_attr_dcmf1.attr,
604 &dev_attr_dcmf2.attr,
605 &dev_attr_dcmf3.attr,
606 &dev_attr_dcmf0_status.attr,
607 &dev_attr_dcmf1_status.attr,
608 &dev_attr_dcmf2_status.attr,
609 &dev_attr_dcmf3_status.attr,
610 &dev_attr_reboot_image.attr,
611 &dev_attr_notify.attr,
615 ATTRIBUTE_GROUPS(rsu);
617 static int stratix10_rsu_probe(struct platform_device *pdev)
619 struct device *dev = &pdev->dev;
620 struct stratix10_rsu_priv *priv;
623 priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
627 priv->client.dev = dev;
628 priv->client.receive_cb = NULL;
629 priv->client.priv = priv;
630 priv->status.current_image = 0;
631 priv->status.fail_image = 0;
632 priv->status.error_location = 0;
633 priv->status.error_details = 0;
634 priv->status.version = 0;
635 priv->status.state = 0;
636 priv->retry_counter = INVALID_RETRY_COUNTER;
637 priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
638 priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
639 priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
640 priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
641 priv->max_retry = INVALID_RETRY_COUNTER;
642 priv->dcmf_status.dcmf0 = INVALID_DCMF_STATUS;
643 priv->dcmf_status.dcmf1 = INVALID_DCMF_STATUS;
644 priv->dcmf_status.dcmf2 = INVALID_DCMF_STATUS;
645 priv->dcmf_status.dcmf3 = INVALID_DCMF_STATUS;
647 mutex_init(&priv->lock);
648 priv->chan = stratix10_svc_request_channel_byname(&priv->client,
650 if (IS_ERR(priv->chan)) {
651 dev_err(dev, "couldn't get service channel %s\n",
653 return PTR_ERR(priv->chan);
656 init_completion(&priv->completion);
657 platform_set_drvdata(pdev, priv);
659 /* get the initial state from firmware */
660 ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
661 0, rsu_status_callback);
663 dev_err(dev, "Error, getting RSU status %i\n", ret);
664 stratix10_svc_free_channel(priv->chan);
667 /* get DCMF version from firmware */
668 ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
669 0, rsu_dcmf_version_callback);
671 dev_err(dev, "Error, getting DCMF version %i\n", ret);
672 stratix10_svc_free_channel(priv->chan);
675 ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_STATUS,
676 0, rsu_dcmf_status_callback);
678 dev_err(dev, "Error, getting DCMF status %i\n", ret);
679 stratix10_svc_free_channel(priv->chan);
682 ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
684 dev_err(dev, "Error, getting RSU retry %i\n", ret);
685 stratix10_svc_free_channel(priv->chan);
688 ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
689 rsu_max_retry_callback);
691 dev_err(dev, "Error, getting RSU max retry %i\n", ret);
692 stratix10_svc_free_channel(priv->chan);
698 static int stratix10_rsu_remove(struct platform_device *pdev)
700 struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
702 stratix10_svc_free_channel(priv->chan);
706 static struct platform_driver stratix10_rsu_driver = {
707 .probe = stratix10_rsu_probe,
708 .remove = stratix10_rsu_remove,
710 .name = "stratix10-rsu",
711 .dev_groups = rsu_groups,
715 module_platform_driver(stratix10_rsu_driver);
717 MODULE_LICENSE("GPL v2");
718 MODULE_DESCRIPTION("Intel Remote System Update Driver");
719 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");