xref: /OK3568_Linux_fs/kernel/drivers/firmware/stratix10-rsu.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Copyright (C) 2018-2019, Intel Corporation
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun 
6*4882a593Smuzhiyun #include <linux/arm-smccc.h>
7*4882a593Smuzhiyun #include <linux/bitfield.h>
8*4882a593Smuzhiyun #include <linux/completion.h>
9*4882a593Smuzhiyun #include <linux/kobject.h>
10*4882a593Smuzhiyun #include <linux/module.h>
11*4882a593Smuzhiyun #include <linux/mutex.h>
12*4882a593Smuzhiyun #include <linux/of.h>
13*4882a593Smuzhiyun #include <linux/of_platform.h>
14*4882a593Smuzhiyun #include <linux/platform_device.h>
15*4882a593Smuzhiyun #include <linux/firmware/intel/stratix10-svc-client.h>
16*4882a593Smuzhiyun #include <linux/string.h>
17*4882a593Smuzhiyun #include <linux/sysfs.h>
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun #define RSU_STATE_MASK			GENMASK_ULL(31, 0)
20*4882a593Smuzhiyun #define RSU_VERSION_MASK		GENMASK_ULL(63, 32)
21*4882a593Smuzhiyun #define RSU_ERROR_LOCATION_MASK		GENMASK_ULL(31, 0)
22*4882a593Smuzhiyun #define RSU_ERROR_DETAIL_MASK		GENMASK_ULL(63, 32)
23*4882a593Smuzhiyun #define RSU_DCMF0_MASK			GENMASK_ULL(31, 0)
24*4882a593Smuzhiyun #define RSU_DCMF1_MASK			GENMASK_ULL(63, 32)
25*4882a593Smuzhiyun #define RSU_DCMF2_MASK			GENMASK_ULL(31, 0)
26*4882a593Smuzhiyun #define RSU_DCMF3_MASK			GENMASK_ULL(63, 32)
27*4882a593Smuzhiyun 
28*4882a593Smuzhiyun #define RSU_TIMEOUT	(msecs_to_jiffies(SVC_RSU_REQUEST_TIMEOUT_MS))
29*4882a593Smuzhiyun 
30*4882a593Smuzhiyun #define INVALID_RETRY_COUNTER		0xFF
31*4882a593Smuzhiyun #define INVALID_DCMF_VERSION		0xFF
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 
34*4882a593Smuzhiyun typedef void (*rsu_callback)(struct stratix10_svc_client *client,
35*4882a593Smuzhiyun 			     struct stratix10_svc_cb_data *data);
36*4882a593Smuzhiyun /**
37*4882a593Smuzhiyun  * struct stratix10_rsu_priv - rsu data structure
38*4882a593Smuzhiyun  * @chan: pointer to the allocated service channel
39*4882a593Smuzhiyun  * @client: active service client
40*4882a593Smuzhiyun  * @completion: state for callback completion
41*4882a593Smuzhiyun  * @lock: a mutex to protect callback completion state
42*4882a593Smuzhiyun  * @status.current_image: address of image currently running in flash
43*4882a593Smuzhiyun  * @status.fail_image: address of failed image in flash
44*4882a593Smuzhiyun  * @status.version: the interface version number of RSU firmware
45*4882a593Smuzhiyun  * @status.state: the state of RSU system
46*4882a593Smuzhiyun  * @status.error_details: error code
47*4882a593Smuzhiyun  * @status.error_location: the error offset inside the image that failed
48*4882a593Smuzhiyun  * @dcmf_version.dcmf0: Quartus dcmf0 version
49*4882a593Smuzhiyun  * @dcmf_version.dcmf1: Quartus dcmf1 version
50*4882a593Smuzhiyun  * @dcmf_version.dcmf2: Quartus dcmf2 version
51*4882a593Smuzhiyun  * @dcmf_version.dcmf3: Quartus dcmf3 version
52*4882a593Smuzhiyun  * @retry_counter: the current image's retry counter
53*4882a593Smuzhiyun  * @max_retry: the preset max retry value
54*4882a593Smuzhiyun  */
55*4882a593Smuzhiyun struct stratix10_rsu_priv {
56*4882a593Smuzhiyun 	struct stratix10_svc_chan *chan;
57*4882a593Smuzhiyun 	struct stratix10_svc_client client;
58*4882a593Smuzhiyun 	struct completion completion;
59*4882a593Smuzhiyun 	struct mutex lock;
60*4882a593Smuzhiyun 	struct {
61*4882a593Smuzhiyun 		unsigned long current_image;
62*4882a593Smuzhiyun 		unsigned long fail_image;
63*4882a593Smuzhiyun 		unsigned int version;
64*4882a593Smuzhiyun 		unsigned int state;
65*4882a593Smuzhiyun 		unsigned int error_details;
66*4882a593Smuzhiyun 		unsigned int error_location;
67*4882a593Smuzhiyun 	} status;
68*4882a593Smuzhiyun 
69*4882a593Smuzhiyun 	struct {
70*4882a593Smuzhiyun 		unsigned int dcmf0;
71*4882a593Smuzhiyun 		unsigned int dcmf1;
72*4882a593Smuzhiyun 		unsigned int dcmf2;
73*4882a593Smuzhiyun 		unsigned int dcmf3;
74*4882a593Smuzhiyun 	} dcmf_version;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	unsigned int retry_counter;
77*4882a593Smuzhiyun 	unsigned int max_retry;
78*4882a593Smuzhiyun };
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun /**
81*4882a593Smuzhiyun  * rsu_status_callback() - Status callback from Intel Service Layer
82*4882a593Smuzhiyun  * @client: pointer to service client
83*4882a593Smuzhiyun  * @data: pointer to callback data structure
84*4882a593Smuzhiyun  *
85*4882a593Smuzhiyun  * Callback from Intel service layer for RSU status request. Status is
86*4882a593Smuzhiyun  * only updated after a system reboot, so a get updated status call is
87*4882a593Smuzhiyun  * made during driver probe.
88*4882a593Smuzhiyun  */
rsu_status_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)89*4882a593Smuzhiyun static void rsu_status_callback(struct stratix10_svc_client *client,
90*4882a593Smuzhiyun 				struct stratix10_svc_cb_data *data)
91*4882a593Smuzhiyun {
92*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = client->priv;
93*4882a593Smuzhiyun 	struct arm_smccc_res *res = (struct arm_smccc_res *)data->kaddr1;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 	if (data->status == BIT(SVC_STATUS_OK)) {
96*4882a593Smuzhiyun 		priv->status.version = FIELD_GET(RSU_VERSION_MASK,
97*4882a593Smuzhiyun 						 res->a2);
98*4882a593Smuzhiyun 		priv->status.state = FIELD_GET(RSU_STATE_MASK, res->a2);
99*4882a593Smuzhiyun 		priv->status.fail_image = res->a1;
100*4882a593Smuzhiyun 		priv->status.current_image = res->a0;
101*4882a593Smuzhiyun 		priv->status.error_location =
102*4882a593Smuzhiyun 			FIELD_GET(RSU_ERROR_LOCATION_MASK, res->a3);
103*4882a593Smuzhiyun 		priv->status.error_details =
104*4882a593Smuzhiyun 			FIELD_GET(RSU_ERROR_DETAIL_MASK, res->a3);
105*4882a593Smuzhiyun 	} else {
106*4882a593Smuzhiyun 		dev_err(client->dev, "COMMAND_RSU_STATUS returned 0x%lX\n",
107*4882a593Smuzhiyun 			res->a0);
108*4882a593Smuzhiyun 		priv->status.version = 0;
109*4882a593Smuzhiyun 		priv->status.state = 0;
110*4882a593Smuzhiyun 		priv->status.fail_image = 0;
111*4882a593Smuzhiyun 		priv->status.current_image = 0;
112*4882a593Smuzhiyun 		priv->status.error_location = 0;
113*4882a593Smuzhiyun 		priv->status.error_details = 0;
114*4882a593Smuzhiyun 	}
115*4882a593Smuzhiyun 
116*4882a593Smuzhiyun 	complete(&priv->completion);
117*4882a593Smuzhiyun }
118*4882a593Smuzhiyun 
119*4882a593Smuzhiyun /**
120*4882a593Smuzhiyun  * rsu_command_callback() - Update callback from Intel Service Layer
121*4882a593Smuzhiyun  * @client: pointer to client
122*4882a593Smuzhiyun  * @data: pointer to callback data structure
123*4882a593Smuzhiyun  *
124*4882a593Smuzhiyun  * Callback from Intel service layer for RSU commands.
125*4882a593Smuzhiyun  */
rsu_command_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)126*4882a593Smuzhiyun static void rsu_command_callback(struct stratix10_svc_client *client,
127*4882a593Smuzhiyun 				 struct stratix10_svc_cb_data *data)
128*4882a593Smuzhiyun {
129*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = client->priv;
130*4882a593Smuzhiyun 
131*4882a593Smuzhiyun 	if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
132*4882a593Smuzhiyun 		dev_warn(client->dev, "FW doesn't support notify\n");
133*4882a593Smuzhiyun 	else if (data->status == BIT(SVC_STATUS_ERROR))
134*4882a593Smuzhiyun 		dev_err(client->dev, "Failure, returned status is %lu\n",
135*4882a593Smuzhiyun 			BIT(data->status));
136*4882a593Smuzhiyun 
137*4882a593Smuzhiyun 	complete(&priv->completion);
138*4882a593Smuzhiyun }
139*4882a593Smuzhiyun 
140*4882a593Smuzhiyun /**
141*4882a593Smuzhiyun  * rsu_retry_callback() - Callback from Intel service layer for getting
142*4882a593Smuzhiyun  * the current image's retry counter from the firmware
143*4882a593Smuzhiyun  * @client: pointer to client
144*4882a593Smuzhiyun  * @data: pointer to callback data structure
145*4882a593Smuzhiyun  *
146*4882a593Smuzhiyun  * Callback from Intel service layer for retry counter, which is used by
147*4882a593Smuzhiyun  * user to know how many times the images is still allowed to reload
148*4882a593Smuzhiyun  * itself before giving up and starting RSU fail-over flow.
149*4882a593Smuzhiyun  */
rsu_retry_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)150*4882a593Smuzhiyun static void rsu_retry_callback(struct stratix10_svc_client *client,
151*4882a593Smuzhiyun 			       struct stratix10_svc_cb_data *data)
152*4882a593Smuzhiyun {
153*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = client->priv;
154*4882a593Smuzhiyun 	unsigned int *counter = (unsigned int *)data->kaddr1;
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 	if (data->status == BIT(SVC_STATUS_OK))
157*4882a593Smuzhiyun 		priv->retry_counter = *counter;
158*4882a593Smuzhiyun 	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
159*4882a593Smuzhiyun 		dev_warn(client->dev, "FW doesn't support retry\n");
160*4882a593Smuzhiyun 	else
161*4882a593Smuzhiyun 		dev_err(client->dev, "Failed to get retry counter %lu\n",
162*4882a593Smuzhiyun 			BIT(data->status));
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun 	complete(&priv->completion);
165*4882a593Smuzhiyun }
166*4882a593Smuzhiyun 
167*4882a593Smuzhiyun /**
168*4882a593Smuzhiyun  * rsu_max_retry_callback() - Callback from Intel service layer for getting
169*4882a593Smuzhiyun  * the max retry value from the firmware
170*4882a593Smuzhiyun  * @client: pointer to client
171*4882a593Smuzhiyun  * @data: pointer to callback data structure
172*4882a593Smuzhiyun  *
173*4882a593Smuzhiyun  * Callback from Intel service layer for max retry.
174*4882a593Smuzhiyun  */
rsu_max_retry_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)175*4882a593Smuzhiyun static void rsu_max_retry_callback(struct stratix10_svc_client *client,
176*4882a593Smuzhiyun 				   struct stratix10_svc_cb_data *data)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = client->priv;
179*4882a593Smuzhiyun 	unsigned int *max_retry = (unsigned int *)data->kaddr1;
180*4882a593Smuzhiyun 
181*4882a593Smuzhiyun 	if (data->status == BIT(SVC_STATUS_OK))
182*4882a593Smuzhiyun 		priv->max_retry = *max_retry;
183*4882a593Smuzhiyun 	else if (data->status == BIT(SVC_STATUS_NO_SUPPORT))
184*4882a593Smuzhiyun 		dev_warn(client->dev, "FW doesn't support max retry\n");
185*4882a593Smuzhiyun 	else
186*4882a593Smuzhiyun 		dev_err(client->dev, "Failed to get max retry %lu\n",
187*4882a593Smuzhiyun 			BIT(data->status));
188*4882a593Smuzhiyun 
189*4882a593Smuzhiyun 	complete(&priv->completion);
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun /**
193*4882a593Smuzhiyun  * rsu_dcmf_version_callback() - Callback from Intel service layer for getting
194*4882a593Smuzhiyun  * the DCMF version
195*4882a593Smuzhiyun  * @client: pointer to client
196*4882a593Smuzhiyun  * @data: pointer to callback data structure
197*4882a593Smuzhiyun  *
198*4882a593Smuzhiyun  * Callback from Intel service layer for DCMF version number
199*4882a593Smuzhiyun  */
rsu_dcmf_version_callback(struct stratix10_svc_client * client,struct stratix10_svc_cb_data * data)200*4882a593Smuzhiyun static void rsu_dcmf_version_callback(struct stratix10_svc_client *client,
201*4882a593Smuzhiyun 				      struct stratix10_svc_cb_data *data)
202*4882a593Smuzhiyun {
203*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = client->priv;
204*4882a593Smuzhiyun 	unsigned long long *value1 = (unsigned long long *)data->kaddr1;
205*4882a593Smuzhiyun 	unsigned long long *value2 = (unsigned long long *)data->kaddr2;
206*4882a593Smuzhiyun 
207*4882a593Smuzhiyun 	if (data->status == BIT(SVC_STATUS_OK)) {
208*4882a593Smuzhiyun 		priv->dcmf_version.dcmf0 = FIELD_GET(RSU_DCMF0_MASK, *value1);
209*4882a593Smuzhiyun 		priv->dcmf_version.dcmf1 = FIELD_GET(RSU_DCMF1_MASK, *value1);
210*4882a593Smuzhiyun 		priv->dcmf_version.dcmf2 = FIELD_GET(RSU_DCMF2_MASK, *value2);
211*4882a593Smuzhiyun 		priv->dcmf_version.dcmf3 = FIELD_GET(RSU_DCMF3_MASK, *value2);
212*4882a593Smuzhiyun 	} else
213*4882a593Smuzhiyun 		dev_err(client->dev, "failed to get DCMF version\n");
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 	complete(&priv->completion);
216*4882a593Smuzhiyun }
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun /**
219*4882a593Smuzhiyun  * rsu_send_msg() - send a message to Intel service layer
220*4882a593Smuzhiyun  * @priv: pointer to rsu private data
221*4882a593Smuzhiyun  * @command: RSU status or update command
222*4882a593Smuzhiyun  * @arg: the request argument, the bitstream address or notify status
223*4882a593Smuzhiyun  * @callback: function pointer for the callback (status or update)
224*4882a593Smuzhiyun  *
225*4882a593Smuzhiyun  * Start an Intel service layer transaction to perform the SMC call that
226*4882a593Smuzhiyun  * is necessary to get RSU boot log or set the address of bitstream to
227*4882a593Smuzhiyun  * boot after reboot.
228*4882a593Smuzhiyun  *
229*4882a593Smuzhiyun  * Returns 0 on success or -ETIMEDOUT on error.
230*4882a593Smuzhiyun  */
rsu_send_msg(struct stratix10_rsu_priv * priv,enum stratix10_svc_command_code command,unsigned long arg,rsu_callback callback)231*4882a593Smuzhiyun static int rsu_send_msg(struct stratix10_rsu_priv *priv,
232*4882a593Smuzhiyun 			enum stratix10_svc_command_code command,
233*4882a593Smuzhiyun 			unsigned long arg,
234*4882a593Smuzhiyun 			rsu_callback callback)
235*4882a593Smuzhiyun {
236*4882a593Smuzhiyun 	struct stratix10_svc_client_msg msg;
237*4882a593Smuzhiyun 	int ret;
238*4882a593Smuzhiyun 
239*4882a593Smuzhiyun 	mutex_lock(&priv->lock);
240*4882a593Smuzhiyun 	reinit_completion(&priv->completion);
241*4882a593Smuzhiyun 	priv->client.receive_cb = callback;
242*4882a593Smuzhiyun 
243*4882a593Smuzhiyun 	msg.command = command;
244*4882a593Smuzhiyun 	if (arg)
245*4882a593Smuzhiyun 		msg.arg[0] = arg;
246*4882a593Smuzhiyun 
247*4882a593Smuzhiyun 	ret = stratix10_svc_send(priv->chan, &msg);
248*4882a593Smuzhiyun 	if (ret < 0)
249*4882a593Smuzhiyun 		goto status_done;
250*4882a593Smuzhiyun 
251*4882a593Smuzhiyun 	ret = wait_for_completion_interruptible_timeout(&priv->completion,
252*4882a593Smuzhiyun 							RSU_TIMEOUT);
253*4882a593Smuzhiyun 	if (!ret) {
254*4882a593Smuzhiyun 		dev_err(priv->client.dev,
255*4882a593Smuzhiyun 			"timeout waiting for SMC call\n");
256*4882a593Smuzhiyun 		ret = -ETIMEDOUT;
257*4882a593Smuzhiyun 		goto status_done;
258*4882a593Smuzhiyun 	} else if (ret < 0) {
259*4882a593Smuzhiyun 		dev_err(priv->client.dev,
260*4882a593Smuzhiyun 			"error %d waiting for SMC call\n", ret);
261*4882a593Smuzhiyun 		goto status_done;
262*4882a593Smuzhiyun 	} else {
263*4882a593Smuzhiyun 		ret = 0;
264*4882a593Smuzhiyun 	}
265*4882a593Smuzhiyun 
266*4882a593Smuzhiyun status_done:
267*4882a593Smuzhiyun 	stratix10_svc_done(priv->chan);
268*4882a593Smuzhiyun 	mutex_unlock(&priv->lock);
269*4882a593Smuzhiyun 	return ret;
270*4882a593Smuzhiyun }
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun /*
273*4882a593Smuzhiyun  * This driver exposes some optional features of the Intel Stratix 10 SoC FPGA.
274*4882a593Smuzhiyun  * The sysfs interfaces exposed here are FPGA Remote System Update (RSU)
275*4882a593Smuzhiyun  * related. They allow user space software to query the configuration system
276*4882a593Smuzhiyun  * status and to request optional reboot behavior specific to Intel FPGAs.
277*4882a593Smuzhiyun  */
278*4882a593Smuzhiyun 
current_image_show(struct device * dev,struct device_attribute * attr,char * buf)279*4882a593Smuzhiyun static ssize_t current_image_show(struct device *dev,
280*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
281*4882a593Smuzhiyun {
282*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun 	if (!priv)
285*4882a593Smuzhiyun 		return -ENODEV;
286*4882a593Smuzhiyun 
287*4882a593Smuzhiyun 	return sprintf(buf, "0x%08lx\n", priv->status.current_image);
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun 
fail_image_show(struct device * dev,struct device_attribute * attr,char * buf)290*4882a593Smuzhiyun static ssize_t fail_image_show(struct device *dev,
291*4882a593Smuzhiyun 			       struct device_attribute *attr, char *buf)
292*4882a593Smuzhiyun {
293*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
294*4882a593Smuzhiyun 
295*4882a593Smuzhiyun 	if (!priv)
296*4882a593Smuzhiyun 		return -ENODEV;
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	return sprintf(buf, "0x%08lx\n", priv->status.fail_image);
299*4882a593Smuzhiyun }
300*4882a593Smuzhiyun 
version_show(struct device * dev,struct device_attribute * attr,char * buf)301*4882a593Smuzhiyun static ssize_t version_show(struct device *dev, struct device_attribute *attr,
302*4882a593Smuzhiyun 			    char *buf)
303*4882a593Smuzhiyun {
304*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
305*4882a593Smuzhiyun 
306*4882a593Smuzhiyun 	if (!priv)
307*4882a593Smuzhiyun 		return -ENODEV;
308*4882a593Smuzhiyun 
309*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->status.version);
310*4882a593Smuzhiyun }
311*4882a593Smuzhiyun 
state_show(struct device * dev,struct device_attribute * attr,char * buf)312*4882a593Smuzhiyun static ssize_t state_show(struct device *dev, struct device_attribute *attr,
313*4882a593Smuzhiyun 			  char *buf)
314*4882a593Smuzhiyun {
315*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
316*4882a593Smuzhiyun 
317*4882a593Smuzhiyun 	if (!priv)
318*4882a593Smuzhiyun 		return -ENODEV;
319*4882a593Smuzhiyun 
320*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->status.state);
321*4882a593Smuzhiyun }
322*4882a593Smuzhiyun 
error_location_show(struct device * dev,struct device_attribute * attr,char * buf)323*4882a593Smuzhiyun static ssize_t error_location_show(struct device *dev,
324*4882a593Smuzhiyun 				   struct device_attribute *attr, char *buf)
325*4882a593Smuzhiyun {
326*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
327*4882a593Smuzhiyun 
328*4882a593Smuzhiyun 	if (!priv)
329*4882a593Smuzhiyun 		return -ENODEV;
330*4882a593Smuzhiyun 
331*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->status.error_location);
332*4882a593Smuzhiyun }
333*4882a593Smuzhiyun 
error_details_show(struct device * dev,struct device_attribute * attr,char * buf)334*4882a593Smuzhiyun static ssize_t error_details_show(struct device *dev,
335*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
336*4882a593Smuzhiyun {
337*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
338*4882a593Smuzhiyun 
339*4882a593Smuzhiyun 	if (!priv)
340*4882a593Smuzhiyun 		return -ENODEV;
341*4882a593Smuzhiyun 
342*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->status.error_details);
343*4882a593Smuzhiyun }
344*4882a593Smuzhiyun 
retry_counter_show(struct device * dev,struct device_attribute * attr,char * buf)345*4882a593Smuzhiyun static ssize_t retry_counter_show(struct device *dev,
346*4882a593Smuzhiyun 				  struct device_attribute *attr, char *buf)
347*4882a593Smuzhiyun {
348*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
349*4882a593Smuzhiyun 
350*4882a593Smuzhiyun 	if (!priv)
351*4882a593Smuzhiyun 		return -ENODEV;
352*4882a593Smuzhiyun 
353*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->retry_counter);
354*4882a593Smuzhiyun }
355*4882a593Smuzhiyun 
max_retry_show(struct device * dev,struct device_attribute * attr,char * buf)356*4882a593Smuzhiyun static ssize_t max_retry_show(struct device *dev,
357*4882a593Smuzhiyun 			      struct device_attribute *attr, char *buf)
358*4882a593Smuzhiyun {
359*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
360*4882a593Smuzhiyun 
361*4882a593Smuzhiyun 	if (!priv)
362*4882a593Smuzhiyun 		return -ENODEV;
363*4882a593Smuzhiyun 
364*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->max_retry);
365*4882a593Smuzhiyun }
366*4882a593Smuzhiyun 
dcmf0_show(struct device * dev,struct device_attribute * attr,char * buf)367*4882a593Smuzhiyun static ssize_t dcmf0_show(struct device *dev,
368*4882a593Smuzhiyun 			  struct device_attribute *attr, char *buf)
369*4882a593Smuzhiyun {
370*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
371*4882a593Smuzhiyun 
372*4882a593Smuzhiyun 	if (!priv)
373*4882a593Smuzhiyun 		return -ENODEV;
374*4882a593Smuzhiyun 
375*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf0);
376*4882a593Smuzhiyun }
377*4882a593Smuzhiyun 
dcmf1_show(struct device * dev,struct device_attribute * attr,char * buf)378*4882a593Smuzhiyun static ssize_t dcmf1_show(struct device *dev,
379*4882a593Smuzhiyun 			  struct device_attribute *attr, char *buf)
380*4882a593Smuzhiyun {
381*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
382*4882a593Smuzhiyun 
383*4882a593Smuzhiyun 	if (!priv)
384*4882a593Smuzhiyun 		return -ENODEV;
385*4882a593Smuzhiyun 
386*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf1);
387*4882a593Smuzhiyun }
388*4882a593Smuzhiyun 
dcmf2_show(struct device * dev,struct device_attribute * attr,char * buf)389*4882a593Smuzhiyun static ssize_t dcmf2_show(struct device *dev,
390*4882a593Smuzhiyun 			  struct device_attribute *attr, char *buf)
391*4882a593Smuzhiyun {
392*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
393*4882a593Smuzhiyun 
394*4882a593Smuzhiyun 	if (!priv)
395*4882a593Smuzhiyun 		return -ENODEV;
396*4882a593Smuzhiyun 
397*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf2);
398*4882a593Smuzhiyun }
399*4882a593Smuzhiyun 
dcmf3_show(struct device * dev,struct device_attribute * attr,char * buf)400*4882a593Smuzhiyun static ssize_t dcmf3_show(struct device *dev,
401*4882a593Smuzhiyun 			  struct device_attribute *attr, char *buf)
402*4882a593Smuzhiyun {
403*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
404*4882a593Smuzhiyun 
405*4882a593Smuzhiyun 	if (!priv)
406*4882a593Smuzhiyun 		return -ENODEV;
407*4882a593Smuzhiyun 
408*4882a593Smuzhiyun 	return sprintf(buf, "0x%08x\n", priv->dcmf_version.dcmf3);
409*4882a593Smuzhiyun }
410*4882a593Smuzhiyun 
reboot_image_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)411*4882a593Smuzhiyun static ssize_t reboot_image_store(struct device *dev,
412*4882a593Smuzhiyun 				  struct device_attribute *attr,
413*4882a593Smuzhiyun 				  const char *buf, size_t count)
414*4882a593Smuzhiyun {
415*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
416*4882a593Smuzhiyun 	unsigned long address;
417*4882a593Smuzhiyun 	int ret;
418*4882a593Smuzhiyun 
419*4882a593Smuzhiyun 	if (!priv)
420*4882a593Smuzhiyun 		return -ENODEV;
421*4882a593Smuzhiyun 
422*4882a593Smuzhiyun 	ret = kstrtoul(buf, 0, &address);
423*4882a593Smuzhiyun 	if (ret)
424*4882a593Smuzhiyun 		return ret;
425*4882a593Smuzhiyun 
426*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_UPDATE,
427*4882a593Smuzhiyun 			   address, rsu_command_callback);
428*4882a593Smuzhiyun 	if (ret) {
429*4882a593Smuzhiyun 		dev_err(dev, "Error, RSU update returned %i\n", ret);
430*4882a593Smuzhiyun 		return ret;
431*4882a593Smuzhiyun 	}
432*4882a593Smuzhiyun 
433*4882a593Smuzhiyun 	return count;
434*4882a593Smuzhiyun }
435*4882a593Smuzhiyun 
notify_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)436*4882a593Smuzhiyun static ssize_t notify_store(struct device *dev,
437*4882a593Smuzhiyun 			    struct device_attribute *attr,
438*4882a593Smuzhiyun 			    const char *buf, size_t count)
439*4882a593Smuzhiyun {
440*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = dev_get_drvdata(dev);
441*4882a593Smuzhiyun 	unsigned long status;
442*4882a593Smuzhiyun 	int ret;
443*4882a593Smuzhiyun 
444*4882a593Smuzhiyun 	if (!priv)
445*4882a593Smuzhiyun 		return -ENODEV;
446*4882a593Smuzhiyun 
447*4882a593Smuzhiyun 	ret = kstrtoul(buf, 0, &status);
448*4882a593Smuzhiyun 	if (ret)
449*4882a593Smuzhiyun 		return ret;
450*4882a593Smuzhiyun 
451*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_NOTIFY,
452*4882a593Smuzhiyun 			   status, rsu_command_callback);
453*4882a593Smuzhiyun 	if (ret) {
454*4882a593Smuzhiyun 		dev_err(dev, "Error, RSU notify returned %i\n", ret);
455*4882a593Smuzhiyun 		return ret;
456*4882a593Smuzhiyun 	}
457*4882a593Smuzhiyun 
458*4882a593Smuzhiyun 	/* to get the updated state */
459*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
460*4882a593Smuzhiyun 			   0, rsu_status_callback);
461*4882a593Smuzhiyun 	if (ret) {
462*4882a593Smuzhiyun 		dev_err(dev, "Error, getting RSU status %i\n", ret);
463*4882a593Smuzhiyun 		return ret;
464*4882a593Smuzhiyun 	}
465*4882a593Smuzhiyun 
466*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
467*4882a593Smuzhiyun 	if (ret) {
468*4882a593Smuzhiyun 		dev_err(dev, "Error, getting RSU retry %i\n", ret);
469*4882a593Smuzhiyun 		return ret;
470*4882a593Smuzhiyun 	}
471*4882a593Smuzhiyun 
472*4882a593Smuzhiyun 	return count;
473*4882a593Smuzhiyun }
474*4882a593Smuzhiyun 
475*4882a593Smuzhiyun static DEVICE_ATTR_RO(current_image);
476*4882a593Smuzhiyun static DEVICE_ATTR_RO(fail_image);
477*4882a593Smuzhiyun static DEVICE_ATTR_RO(state);
478*4882a593Smuzhiyun static DEVICE_ATTR_RO(version);
479*4882a593Smuzhiyun static DEVICE_ATTR_RO(error_location);
480*4882a593Smuzhiyun static DEVICE_ATTR_RO(error_details);
481*4882a593Smuzhiyun static DEVICE_ATTR_RO(retry_counter);
482*4882a593Smuzhiyun static DEVICE_ATTR_RO(max_retry);
483*4882a593Smuzhiyun static DEVICE_ATTR_RO(dcmf0);
484*4882a593Smuzhiyun static DEVICE_ATTR_RO(dcmf1);
485*4882a593Smuzhiyun static DEVICE_ATTR_RO(dcmf2);
486*4882a593Smuzhiyun static DEVICE_ATTR_RO(dcmf3);
487*4882a593Smuzhiyun static DEVICE_ATTR_WO(reboot_image);
488*4882a593Smuzhiyun static DEVICE_ATTR_WO(notify);
489*4882a593Smuzhiyun 
490*4882a593Smuzhiyun static struct attribute *rsu_attrs[] = {
491*4882a593Smuzhiyun 	&dev_attr_current_image.attr,
492*4882a593Smuzhiyun 	&dev_attr_fail_image.attr,
493*4882a593Smuzhiyun 	&dev_attr_state.attr,
494*4882a593Smuzhiyun 	&dev_attr_version.attr,
495*4882a593Smuzhiyun 	&dev_attr_error_location.attr,
496*4882a593Smuzhiyun 	&dev_attr_error_details.attr,
497*4882a593Smuzhiyun 	&dev_attr_retry_counter.attr,
498*4882a593Smuzhiyun 	&dev_attr_max_retry.attr,
499*4882a593Smuzhiyun 	&dev_attr_dcmf0.attr,
500*4882a593Smuzhiyun 	&dev_attr_dcmf1.attr,
501*4882a593Smuzhiyun 	&dev_attr_dcmf2.attr,
502*4882a593Smuzhiyun 	&dev_attr_dcmf3.attr,
503*4882a593Smuzhiyun 	&dev_attr_reboot_image.attr,
504*4882a593Smuzhiyun 	&dev_attr_notify.attr,
505*4882a593Smuzhiyun 	NULL
506*4882a593Smuzhiyun };
507*4882a593Smuzhiyun 
508*4882a593Smuzhiyun ATTRIBUTE_GROUPS(rsu);
509*4882a593Smuzhiyun 
stratix10_rsu_probe(struct platform_device * pdev)510*4882a593Smuzhiyun static int stratix10_rsu_probe(struct platform_device *pdev)
511*4882a593Smuzhiyun {
512*4882a593Smuzhiyun 	struct device *dev = &pdev->dev;
513*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv;
514*4882a593Smuzhiyun 	int ret;
515*4882a593Smuzhiyun 
516*4882a593Smuzhiyun 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
517*4882a593Smuzhiyun 	if (!priv)
518*4882a593Smuzhiyun 		return -ENOMEM;
519*4882a593Smuzhiyun 
520*4882a593Smuzhiyun 	priv->client.dev = dev;
521*4882a593Smuzhiyun 	priv->client.receive_cb = NULL;
522*4882a593Smuzhiyun 	priv->client.priv = priv;
523*4882a593Smuzhiyun 	priv->status.current_image = 0;
524*4882a593Smuzhiyun 	priv->status.fail_image = 0;
525*4882a593Smuzhiyun 	priv->status.error_location = 0;
526*4882a593Smuzhiyun 	priv->status.error_details = 0;
527*4882a593Smuzhiyun 	priv->status.version = 0;
528*4882a593Smuzhiyun 	priv->status.state = 0;
529*4882a593Smuzhiyun 	priv->retry_counter = INVALID_RETRY_COUNTER;
530*4882a593Smuzhiyun 	priv->dcmf_version.dcmf0 = INVALID_DCMF_VERSION;
531*4882a593Smuzhiyun 	priv->dcmf_version.dcmf1 = INVALID_DCMF_VERSION;
532*4882a593Smuzhiyun 	priv->dcmf_version.dcmf2 = INVALID_DCMF_VERSION;
533*4882a593Smuzhiyun 	priv->dcmf_version.dcmf3 = INVALID_DCMF_VERSION;
534*4882a593Smuzhiyun 	priv->max_retry = INVALID_RETRY_COUNTER;
535*4882a593Smuzhiyun 
536*4882a593Smuzhiyun 	mutex_init(&priv->lock);
537*4882a593Smuzhiyun 	priv->chan = stratix10_svc_request_channel_byname(&priv->client,
538*4882a593Smuzhiyun 							  SVC_CLIENT_RSU);
539*4882a593Smuzhiyun 	if (IS_ERR(priv->chan)) {
540*4882a593Smuzhiyun 		dev_err(dev, "couldn't get service channel %s\n",
541*4882a593Smuzhiyun 			SVC_CLIENT_RSU);
542*4882a593Smuzhiyun 		return PTR_ERR(priv->chan);
543*4882a593Smuzhiyun 	}
544*4882a593Smuzhiyun 
545*4882a593Smuzhiyun 	init_completion(&priv->completion);
546*4882a593Smuzhiyun 	platform_set_drvdata(pdev, priv);
547*4882a593Smuzhiyun 
548*4882a593Smuzhiyun 	/* get the initial state from firmware */
549*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_STATUS,
550*4882a593Smuzhiyun 			   0, rsu_status_callback);
551*4882a593Smuzhiyun 	if (ret) {
552*4882a593Smuzhiyun 		dev_err(dev, "Error, getting RSU status %i\n", ret);
553*4882a593Smuzhiyun 		stratix10_svc_free_channel(priv->chan);
554*4882a593Smuzhiyun 	}
555*4882a593Smuzhiyun 
556*4882a593Smuzhiyun 	/* get DCMF version from firmware */
557*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_DCMF_VERSION,
558*4882a593Smuzhiyun 			   0, rsu_dcmf_version_callback);
559*4882a593Smuzhiyun 	if (ret) {
560*4882a593Smuzhiyun 		dev_err(dev, "Error, getting DCMF version %i\n", ret);
561*4882a593Smuzhiyun 		stratix10_svc_free_channel(priv->chan);
562*4882a593Smuzhiyun 	}
563*4882a593Smuzhiyun 
564*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_RETRY, 0, rsu_retry_callback);
565*4882a593Smuzhiyun 	if (ret) {
566*4882a593Smuzhiyun 		dev_err(dev, "Error, getting RSU retry %i\n", ret);
567*4882a593Smuzhiyun 		stratix10_svc_free_channel(priv->chan);
568*4882a593Smuzhiyun 	}
569*4882a593Smuzhiyun 
570*4882a593Smuzhiyun 	ret = rsu_send_msg(priv, COMMAND_RSU_MAX_RETRY, 0,
571*4882a593Smuzhiyun 			   rsu_max_retry_callback);
572*4882a593Smuzhiyun 	if (ret) {
573*4882a593Smuzhiyun 		dev_err(dev, "Error, getting RSU max retry %i\n", ret);
574*4882a593Smuzhiyun 		stratix10_svc_free_channel(priv->chan);
575*4882a593Smuzhiyun 	}
576*4882a593Smuzhiyun 
577*4882a593Smuzhiyun 	return ret;
578*4882a593Smuzhiyun }
579*4882a593Smuzhiyun 
stratix10_rsu_remove(struct platform_device * pdev)580*4882a593Smuzhiyun static int stratix10_rsu_remove(struct platform_device *pdev)
581*4882a593Smuzhiyun {
582*4882a593Smuzhiyun 	struct stratix10_rsu_priv *priv = platform_get_drvdata(pdev);
583*4882a593Smuzhiyun 
584*4882a593Smuzhiyun 	stratix10_svc_free_channel(priv->chan);
585*4882a593Smuzhiyun 	return 0;
586*4882a593Smuzhiyun }
587*4882a593Smuzhiyun 
588*4882a593Smuzhiyun static struct platform_driver stratix10_rsu_driver = {
589*4882a593Smuzhiyun 	.probe = stratix10_rsu_probe,
590*4882a593Smuzhiyun 	.remove = stratix10_rsu_remove,
591*4882a593Smuzhiyun 	.driver = {
592*4882a593Smuzhiyun 		.name = "stratix10-rsu",
593*4882a593Smuzhiyun 		.dev_groups = rsu_groups,
594*4882a593Smuzhiyun 	},
595*4882a593Smuzhiyun };
596*4882a593Smuzhiyun 
597*4882a593Smuzhiyun module_platform_driver(stratix10_rsu_driver);
598*4882a593Smuzhiyun 
599*4882a593Smuzhiyun MODULE_LICENSE("GPL v2");
600*4882a593Smuzhiyun MODULE_DESCRIPTION("Intel Remote System Update Driver");
601*4882a593Smuzhiyun MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
602