xref: /linux/include/linux/firmware/intel/stratix10-svc-client.h (revision 71e6a19fe2669faf7dcb96ba8a4fde927485e60e)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /*
3  * Copyright (C) 2017-2018, Intel Corporation
4  * Copyright (C) 2025, Altera Corporation
5  */
6 
7 #ifndef __STRATIX10_SVC_CLIENT_H
8 #define __STRATIX10_SVC_CLIENT_H
9 
10 /*
11  * Service layer driver supports client names
12  *
13  * fpga: for FPGA configuration
14  * rsu: for remote status update
15  * hwmon: for hardware monitoring (voltage and temperature)
16  */
17 #define SVC_CLIENT_FPGA			"fpga"
18 #define SVC_CLIENT_RSU			"rsu"
19 #define SVC_CLIENT_FCS			"fcs"
20 #define SVC_CLIENT_HWMON		"hwmon"
21 
22 /*
23  * Status of the sent command, in bit number
24  *
25  * SVC_STATUS_OK:
26  * Secure firmware accepts the request issued by one of service clients.
27  *
28  * SVC_STATUS_BUFFER_SUBMITTED:
29  * Service client successfully submits data buffer to secure firmware.
30  *
31  * SVC_STATUS_BUFFER_DONE:
32  * Secure firmware completes data process, ready to accept the
33  * next WRITE transaction.
34  *
35  * SVC_STATUS_COMPLETED:
36  * Secure firmware completes service request successfully. In case of
37  * FPGA configuration, FPGA should be in user mode.
38  *
39  * SVC_COMMAND_STATUS_BUSY:
40  * Service request is still in process.
41  *
42  * SVC_COMMAND_STATUS_ERROR:
43  * Error encountered during the process of the service request.
44  *
45  * SVC_STATUS_NO_SUPPORT:
46  * Secure firmware doesn't support requested features such as RSU retry
47  * or RSU notify.
48  */
49 #define SVC_STATUS_OK			0
50 #define SVC_STATUS_BUFFER_SUBMITTED	1
51 #define SVC_STATUS_BUFFER_DONE		2
52 #define SVC_STATUS_COMPLETED		3
53 #define SVC_STATUS_BUSY			4
54 #define SVC_STATUS_ERROR		5
55 #define SVC_STATUS_NO_SUPPORT		6
56 #define SVC_STATUS_INVALID_PARAM	7
57 
58 /*
59  * Flag bit for COMMAND_RECONFIG
60  *
61  * COMMAND_RECONFIG_FLAG_PARTIAL:
62  * Set to FPGA configuration type (full or partial).
63  */
64 #define COMMAND_RECONFIG_FLAG_PARTIAL	0
65 
66 /*
67  * Timeout settings for service clients:
68  * timeout value used in Stratix10 FPGA manager driver.
69  * timeout value used in RSU driver
70  */
71 #define SVC_RECONFIG_REQUEST_TIMEOUT_MS         5000
72 #define SVC_RECONFIG_BUFFER_TIMEOUT_MS          5000
73 #define SVC_RSU_REQUEST_TIMEOUT_MS              2000
74 #define SVC_FCS_REQUEST_TIMEOUT_MS		2000
75 #define SVC_COMPLETED_TIMEOUT_MS		30000
76 #define SVC_HWMON_REQUEST_TIMEOUT_MS		2000
77 
78 struct stratix10_svc_chan;
79 
80 /**
81  * enum stratix10_svc_command_code - supported service commands
82  *
83  * @COMMAND_NOOP: do 'dummy' request for integration/debug/trouble-shooting
84  *
85  * @COMMAND_RECONFIG: ask for FPGA configuration preparation, return status
86  * is SVC_STATUS_OK
87  *
88  * @COMMAND_RECONFIG_DATA_SUBMIT: submit buffer(s) of bit-stream data for the
89  * FPGA configuration, return status is SVC_STATUS_SUBMITTED or SVC_STATUS_ERROR
90  *
91  * @COMMAND_RECONFIG_DATA_CLAIM: check the status of the configuration, return
92  * status is SVC_STATUS_COMPLETED, or SVC_STATUS_BUSY, or SVC_STATUS_ERROR
93  *
94  * @COMMAND_RECONFIG_STATUS: check the status of the configuration, return
95  * status is SVC_STATUS_COMPLETED, or SVC_STATUS_BUSY, or SVC_STATUS_ERROR
96  *
97  * @COMMAND_RSU_STATUS: request remote system update boot log, return status
98  * is log data or SVC_STATUS_RSU_ERROR
99  *
100  * @COMMAND_RSU_UPDATE: set the offset of the bitstream to boot after reboot,
101  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
102  *
103  * @COMMAND_RSU_NOTIFY: report the status of hard processor system
104  * software to firmware, return status is SVC_STATUS_OK or
105  * SVC_STATUS_ERROR
106  *
107  * @COMMAND_RSU_RETRY: query firmware for the current image's retry counter,
108  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
109  *
110  * @COMMAND_RSU_MAX_RETRY: query firmware for the max retry value,
111  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
112  *
113  * @COMMAND_RSU_DCMF_VERSION: query firmware for the DCMF version, return status
114  * is SVC_STATUS_OK or SVC_STATUS_ERROR
115  *
116  * @COMMAND_POLL_SERVICE_STATUS: poll if the service request is complete,
117  * return statis is SVC_STATUS_OK, SVC_STATUS_ERROR or SVC_STATUS_BUSY
118  *
119  * @COMMAND_FIRMWARE_VERSION: query running firmware version, return status
120  * is SVC_STATUS_OK or SVC_STATUS_ERROR
121  *
122  * @COMMAND_SMC_SVC_VERSION: Non-mailbox SMC SVC API Version,
123  * return status is SVC_STATUS_OK
124  *
125  * @COMMAND_MBOX_SEND_CMD: send generic mailbox command, return status is
126  * SVC_STATUS_OK or SVC_STATUS_ERROR
127  *
128  * @COMMAND_RSU_DCMF_STATUS: query firmware for the DCMF status
129  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
130  *
131  * @COMMAND_RSU_GET_SPT_TABLE: query firmware for SPT table
132  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
133  *
134  * @COMMAND_FCS_REQUEST_SERVICE: request validation of image from firmware,
135  * return status is SVC_STATUS_OK, SVC_STATUS_INVALID_PARAM
136  *
137  * @COMMAND_FCS_SEND_CERTIFICATE: send a certificate, return status is
138  * SVC_STATUS_OK, SVC_STATUS_INVALID_PARAM, SVC_STATUS_ERROR
139  *
140  * @COMMAND_FCS_GET_PROVISION_DATA: read the provisioning data, return status is
141  * SVC_STATUS_OK, SVC_STATUS_INVALID_PARAM, SVC_STATUS_ERROR
142  *
143  * @COMMAND_FCS_DATA_ENCRYPTION: encrypt the data, return status is
144  * SVC_STATUS_OK, SVC_STATUS_INVALID_PARAM, SVC_STATUS_ERROR
145  *
146  * @COMMAND_FCS_DATA_DECRYPTION: decrypt the data, return status is
147  * SVC_STATUS_OK, SVC_STATUS_INVALID_PARAM, SVC_STATUS_ERROR
148  *
149  * @COMMAND_FCS_RANDOM_NUMBER_GEN: generate a random number, return status
150  * is SVC_STATUS_OK, SVC_STATUS_ERROR
151  *
152  * @COMMAND_HWMON_READTEMP: query the temperature from the hardware monitor,
153  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
154  *
155  * @COMMAND_HWMON_READVOLT: query the voltage from the hardware monitor,
156  * return status is SVC_STATUS_OK or SVC_STATUS_ERROR
157  *
158  * @COMMAND_SMC_ATF_BUILD_VER: Non-mailbox SMC ATF Build Version,
159  * return status is SVC_STATUS_OK
160  *
161  */
162 enum stratix10_svc_command_code {
163 	/* for FPGA */
164 	COMMAND_NOOP = 0,
165 	COMMAND_RECONFIG,
166 	COMMAND_RECONFIG_DATA_SUBMIT,
167 	COMMAND_RECONFIG_DATA_CLAIM,
168 	COMMAND_RECONFIG_STATUS,
169 	/* for RSU */
170 	COMMAND_RSU_STATUS = 10,
171 	COMMAND_RSU_UPDATE,
172 	COMMAND_RSU_NOTIFY,
173 	COMMAND_RSU_RETRY,
174 	COMMAND_RSU_MAX_RETRY,
175 	COMMAND_RSU_DCMF_VERSION,
176 	COMMAND_RSU_DCMF_STATUS,
177 	COMMAND_FIRMWARE_VERSION,
178 	COMMAND_RSU_GET_SPT_TABLE,
179 	/* for FCS */
180 	COMMAND_FCS_REQUEST_SERVICE = 20,
181 	COMMAND_FCS_SEND_CERTIFICATE,
182 	COMMAND_FCS_GET_PROVISION_DATA,
183 	COMMAND_FCS_DATA_ENCRYPTION,
184 	COMMAND_FCS_DATA_DECRYPTION,
185 	COMMAND_FCS_RANDOM_NUMBER_GEN,
186 	/* for general status poll */
187 	COMMAND_POLL_SERVICE_STATUS = 40,
188 	/* for generic mailbox send command */
189 	COMMAND_MBOX_SEND_CMD = 100,
190 	/* Non-mailbox SMC Call */
191 	COMMAND_SMC_SVC_VERSION = 200,
192 	/* for HWMON */
193 	COMMAND_HWMON_READTEMP,
194 	COMMAND_HWMON_READVOLT,
195 	COMMAND_SMC_ATF_BUILD_VER
196 };
197 
198 /**
199  * struct stratix10_svc_client_msg - message sent by client to service
200  * @payload: starting address of data need be processed
201  * @payload_length: to be processed data size in bytes
202  * @payload_output: starting address of processed data
203  * @payload_length_output: processed data size in bytes
204  * @command: service command
205  * @arg: args to be passed via registers and not physically mapped buffers
206  */
207 struct stratix10_svc_client_msg {
208 	void *payload;
209 	size_t payload_length;
210 	void *payload_output;
211 	size_t payload_length_output;
212 	enum stratix10_svc_command_code command;
213 	u64 arg[3];
214 };
215 
216 /**
217  * struct stratix10_svc_command_config_type - config type
218  * @flags: flag bit for the type of FPGA configuration
219  */
220 struct stratix10_svc_command_config_type {
221 	u32 flags;
222 };
223 
224 /**
225  * struct stratix10_svc_cb_data - callback data structure from service layer
226  * @status: the status of sent command
227  * @kaddr1: address of 1st completed data block
228  * @kaddr2: address of 2nd completed data block
229  * @kaddr3: address of 3rd completed data block
230  */
231 struct stratix10_svc_cb_data {
232 	u32 status;
233 	void *kaddr1;
234 	void *kaddr2;
235 	void *kaddr3;
236 };
237 
238 /**
239  * struct stratix10_svc_client - service client structure
240  * @dev: the client device
241  * @receive_cb: callback to provide service client the received data
242  * @priv: client private data
243  */
244 struct stratix10_svc_client {
245 	struct device *dev;
246 	void (*receive_cb)(struct stratix10_svc_client *client,
247 			   struct stratix10_svc_cb_data *cb_data);
248 	void *priv;
249 };
250 
251 /**
252  * stratix10_svc_request_channel_byname() - request service channel
253  * @client: identity of the client requesting the channel
254  * @name: supporting client name defined above
255  *
256  * Return: a pointer to channel assigned to the client on success,
257  * or ERR_PTR() on error.
258  */
259 struct stratix10_svc_chan
260 *stratix10_svc_request_channel_byname(struct stratix10_svc_client *client,
261 	const char *name);
262 
263 /**
264  * stratix10_svc_free_channel() - free service channel.
265  * @chan: service channel to be freed
266  */
267 void stratix10_svc_free_channel(struct stratix10_svc_chan *chan);
268 
269 /**
270  * stratix10_svc_allocate_memory() - allocate the momory
271  * @chan: service channel assigned to the client
272  * @size: number of bytes client requests
273  *
274  * Service layer allocates the requested number of bytes from the memory
275  * pool for the client.
276  *
277  * Return: the starting address of allocated memory on success, or
278  * ERR_PTR() on error.
279  */
280 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
281 				    size_t size);
282 
283 /**
284  * stratix10_svc_free_memory() - free allocated memory
285  * @chan: service channel assigned to the client
286  * @kaddr: starting address of memory to be free back to pool
287  */
288 void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr);
289 
290 /**
291  * stratix10_svc_send() - send a message to the remote
292  * @chan: service channel assigned to the client
293  * @msg: message data to be sent, in the format of
294  * struct stratix10_svc_client_msg
295  *
296  * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
297  */
298 int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg);
299 
300 /**
301  * stratix10_svc_done() - complete service request
302  * @chan: service channel assigned to the client
303  *
304  * This function is used by service client to inform service layer that
305  * client's service requests are completed, or there is an error in the
306  * request process.
307  */
308 void stratix10_svc_done(struct stratix10_svc_chan *chan);
309 
310 /**
311  * typedef async_callback_t - A type definition for an asynchronous callback function.
312  *
313  * This type defines a function pointer for an asynchronous callback.
314  * The callback function takes a single argument, which is a pointer to
315  * user-defined data.
316  *
317  * @cb_arg: Argument to be passed to the callback function.
318  */
319 typedef void (*async_callback_t)(void *cb_arg);
320 
321 /**
322  * stratix10_svc_add_async_client - Add an asynchronous client to a Stratix 10
323  *                                  service channel.
324  * @chan: Pointer to the Stratix 10 service channel structure.
325  * @use_unique_clientid: Boolean flag indicating whether to use a unique client ID.
326  *
327  * This function registers an asynchronous client with the specified Stratix 10
328  * service channel. If the use_unique_clientid flag is set to true, a unique client
329  * ID will be assigned to the client.
330  *
331  * Return: 0 on success, or a negative error code on failure:
332  *         -EINVAL if the channel is NULL or the async controller is not initialized.
333  *         -EALREADY if the async channel is already allocated.
334  *         -ENOMEM if memory allocation fails.
335  *         Other negative values if ID allocation fails
336  */
337 int stratix10_svc_add_async_client(struct stratix10_svc_chan *chan, bool use_unique_clientid);
338 
339 /**
340  * stratix10_svc_remove_async_client - Remove an asynchronous client from the Stratix 10
341  *                                     service channel.
342  * @chan: Pointer to the Stratix 10 service channel structure.
343  *
344  * This function removes an asynchronous client from the specified Stratix 10 service channel.
345  * It is typically used to clean up and release resources associated with the client.
346  *
347  * Return: 0 on success, -EINVAL if the channel or asynchronous channel is invalid.
348  */
349 int stratix10_svc_remove_async_client(struct stratix10_svc_chan *chan);
350 
351 /**
352  * stratix10_svc_async_send - Send an asynchronous message to the SDM mailbox
353  *                            in EL3 secure firmware.
354  * @chan: Pointer to the service channel structure.
355  * @msg: Pointer to the message to be sent.
356  * @handler: Pointer to the handler object used by caller to track the transaction.
357  * @cb: Callback function to be called upon completion.
358  * @cb_arg: Argument to be passed to the callback function.
359  *
360  * This function sends a message asynchronously to the SDM mailbox in EL3 secure firmware.
361  * and registers a callback function to be invoked when the operation completes.
362  *
363  * Return: 0 on success,and negative error codes on failure.
364  */
365 int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg, void **handler,
366 			     async_callback_t cb, void *cb_arg);
367 
368 /**
369  * stratix10_svc_async_poll - Polls the status of an asynchronous service request.
370  * @chan: Pointer to the service channel structure.
371  * @tx_handle: Handle to the transaction being polled.
372  * @data: Pointer to the callback data structure to be filled with the result.
373  *
374  * This function checks the status of an asynchronous service request
375  * and fills the provided callback data structure with the result.
376  *
377  * Return: 0 on success, -EINVAL if any input parameter is invalid or if the
378  *         async controller is not initialized, -EAGAIN if the transaction is
379  *         still in progress, or other negative error codes on failure.
380  */
381 int stratix10_svc_async_poll(struct stratix10_svc_chan *chan, void *tx_handle,
382 			     struct stratix10_svc_cb_data *data);
383 
384 /**
385  * stratix10_svc_async_done - Complete an asynchronous transaction
386  * @chan: Pointer to the service channel structure
387  * @tx_handle: Pointer to the transaction handle
388  *
389  * This function completes an asynchronous transaction by removing the
390  * transaction from the hash table and deallocating the associated resources.
391  *
392  * Return: 0 on success, -EINVAL on invalid input or errors.
393  */
394 int stratix10_svc_async_done(struct stratix10_svc_chan *chan, void *tx_handle);
395 
396 #endif
397 
398