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 300
72#define SVC_RECONFIG_BUFFER_TIMEOUT_MS 720
73#define SVC_RSU_REQUEST_TIMEOUT_MS 300
74#define SVC_FCS_REQUEST_TIMEOUT_MS 2000
75#define SVC_COMPLETED_TIMEOUT_MS 30000
76#define SVC_HWMON_REQUEST_TIMEOUT_MS 300
77
78struct 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 */
158enum stratix10_svc_command_code {
159 /* for FPGA */
160 COMMAND_NOOP = 0,
161 COMMAND_RECONFIG,
162 COMMAND_RECONFIG_DATA_SUBMIT,
163 COMMAND_RECONFIG_DATA_CLAIM,
164 COMMAND_RECONFIG_STATUS,
165 /* for RSU */
166 COMMAND_RSU_STATUS = 10,
167 COMMAND_RSU_UPDATE,
168 COMMAND_RSU_NOTIFY,
169 COMMAND_RSU_RETRY,
170 COMMAND_RSU_MAX_RETRY,
171 COMMAND_RSU_DCMF_VERSION,
172 COMMAND_RSU_DCMF_STATUS,
173 COMMAND_FIRMWARE_VERSION,
174 COMMAND_RSU_GET_SPT_TABLE,
175 /* for FCS */
176 COMMAND_FCS_REQUEST_SERVICE = 20,
177 COMMAND_FCS_SEND_CERTIFICATE,
178 COMMAND_FCS_GET_PROVISION_DATA,
179 COMMAND_FCS_DATA_ENCRYPTION,
180 COMMAND_FCS_DATA_DECRYPTION,
181 COMMAND_FCS_RANDOM_NUMBER_GEN,
182 /* for general status poll */
183 COMMAND_POLL_SERVICE_STATUS = 40,
184 /* for generic mailbox send command */
185 COMMAND_MBOX_SEND_CMD = 100,
186 /* Non-mailbox SMC Call */
187 COMMAND_SMC_SVC_VERSION = 200,
188 /* for HWMON */
189 COMMAND_HWMON_READTEMP,
190 COMMAND_HWMON_READVOLT
191};
192
193/**
194 * struct stratix10_svc_client_msg - message sent by client to service
195 * @payload: starting address of data need be processed
196 * @payload_length: to be processed data size in bytes
197 * @payload_output: starting address of processed data
198 * @payload_length_output: processed data size in bytes
199 * @command: service command
200 * @arg: args to be passed via registers and not physically mapped buffers
201 */
202struct stratix10_svc_client_msg {
203 void *payload;
204 size_t payload_length;
205 void *payload_output;
206 size_t payload_length_output;
207 enum stratix10_svc_command_code command;
208 u64 arg[3];
209};
210
211/**
212 * struct stratix10_svc_command_config_type - config type
213 * @flags: flag bit for the type of FPGA configuration
214 */
215struct stratix10_svc_command_config_type {
216 u32 flags;
217};
218
219/**
220 * struct stratix10_svc_cb_data - callback data structure from service layer
221 * @status: the status of sent command
222 * @kaddr1: address of 1st completed data block
223 * @kaddr2: address of 2nd completed data block
224 * @kaddr3: address of 3rd completed data block
225 */
226struct stratix10_svc_cb_data {
227 u32 status;
228 void *kaddr1;
229 void *kaddr2;
230 void *kaddr3;
231};
232
233/**
234 * struct stratix10_svc_client - service client structure
235 * @dev: the client device
236 * @receive_cb: callback to provide service client the received data
237 * @priv: client private data
238 */
239struct stratix10_svc_client {
240 struct device *dev;
241 void (*receive_cb)(struct stratix10_svc_client *client,
242 struct stratix10_svc_cb_data *cb_data);
243 void *priv;
244};
245
246/**
247 * stratix10_svc_request_channel_byname() - request service channel
248 * @client: identity of the client requesting the channel
249 * @name: supporting client name defined above
250 *
251 * Return: a pointer to channel assigned to the client on success,
252 * or ERR_PTR() on error.
253 */
254struct stratix10_svc_chan
255*stratix10_svc_request_channel_byname(struct stratix10_svc_client *client,
256 const char *name);
257
258/**
259 * stratix10_svc_free_channel() - free service channel.
260 * @chan: service channel to be freed
261 */
262void stratix10_svc_free_channel(struct stratix10_svc_chan *chan);
263
264/**
265 * stratix10_svc_allocate_memory() - allocate the momory
266 * @chan: service channel assigned to the client
267 * @size: number of bytes client requests
268 *
269 * Service layer allocates the requested number of bytes from the memory
270 * pool for the client.
271 *
272 * Return: the starting address of allocated memory on success, or
273 * ERR_PTR() on error.
274 */
275void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
276 size_t size);
277
278/**
279 * stratix10_svc_free_memory() - free allocated memory
280 * @chan: service channel assigned to the client
281 * @kaddr: starting address of memory to be free back to pool
282 */
283void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr);
284
285/**
286 * stratix10_svc_send() - send a message to the remote
287 * @chan: service channel assigned to the client
288 * @msg: message data to be sent, in the format of
289 * struct stratix10_svc_client_msg
290 *
291 * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
292 */
293int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg);
294
295/**
296 * stratix10_svc_done() - complete service request
297 * @chan: service channel assigned to the client
298 *
299 * This function is used by service client to inform service layer that
300 * client's service requests are completed, or there is an error in the
301 * request process.
302 */
303void stratix10_svc_done(struct stratix10_svc_chan *chan);
304
305/**
306 * typedef async_callback_t - A type definition for an asynchronous callback function.
307 *
308 * This type defines a function pointer for an asynchronous callback.
309 * The callback function takes a single argument, which is a pointer to
310 * user-defined data.
311 *
312 * @cb_arg: Argument to be passed to the callback function.
313 */
314typedef void (*async_callback_t)(void *cb_arg);
315
316/**
317 * stratix10_svc_add_async_client - Add an asynchronous client to a Stratix 10
318 * service channel.
319 * @chan: Pointer to the Stratix 10 service channel structure.
320 * @use_unique_clientid: Boolean flag indicating whether to use a unique client ID.
321 *
322 * This function registers an asynchronous client with the specified Stratix 10
323 * service channel. If the use_unique_clientid flag is set to true, a unique client
324 * ID will be assigned to the client.
325 *
326 * Return: 0 on success, or a negative error code on failure:
327 * -EINVAL if the channel is NULL or the async controller is not initialized.
328 * -EALREADY if the async channel is already allocated.
329 * -ENOMEM if memory allocation fails.
330 * Other negative values if ID allocation fails
331 */
332int stratix10_svc_add_async_client(struct stratix10_svc_chan *chan, bool use_unique_clientid);
333
334/**
335 * stratix10_svc_remove_async_client - Remove an asynchronous client from the Stratix 10
336 * service channel.
337 * @chan: Pointer to the Stratix 10 service channel structure.
338 *
339 * This function removes an asynchronous client from the specified Stratix 10 service channel.
340 * It is typically used to clean up and release resources associated with the client.
341 *
342 * Return: 0 on success, -EINVAL if the channel or asynchronous channel is invalid.
343 */
344int stratix10_svc_remove_async_client(struct stratix10_svc_chan *chan);
345
346/**
347 * stratix10_svc_async_send - Send an asynchronous message to the SDM mailbox
348 * in EL3 secure firmware.
349 * @chan: Pointer to the service channel structure.
350 * @msg: Pointer to the message to be sent.
351 * @handler: Pointer to the handler object used by caller to track the transaction.
352 * @cb: Callback function to be called upon completion.
353 * @cb_arg: Argument to be passed to the callback function.
354 *
355 * This function sends a message asynchronously to the SDM mailbox in EL3 secure firmware.
356 * and registers a callback function to be invoked when the operation completes.
357 *
358 * Return: 0 on success,and negative error codes on failure.
359 */
360int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg, void **handler,
361 async_callback_t cb, void *cb_arg);
362
363/**
364 * stratix10_svc_async_poll - Polls the status of an asynchronous service request.
365 * @chan: Pointer to the service channel structure.
366 * @tx_handle: Handle to the transaction being polled.
367 * @data: Pointer to the callback data structure to be filled with the result.
368 *
369 * This function checks the status of an asynchronous service request
370 * and fills the provided callback data structure with the result.
371 *
372 * Return: 0 on success, -EINVAL if any input parameter is invalid or if the
373 * async controller is not initialized, -EAGAIN if the transaction is
374 * still in progress, or other negative error codes on failure.
375 */
376int stratix10_svc_async_poll(struct stratix10_svc_chan *chan, void *tx_handle,
377 struct stratix10_svc_cb_data *data);
378
379/**
380 * stratix10_svc_async_done - Complete an asynchronous transaction
381 * @chan: Pointer to the service channel structure
382 * @tx_handle: Pointer to the transaction handle
383 *
384 * This function completes an asynchronous transaction by removing the
385 * transaction from the hash table and deallocating the associated resources.
386 *
387 * Return: 0 on success, -EINVAL on invalid input or errors.
388 */
389int stratix10_svc_async_done(struct stratix10_svc_chan *chan, void *tx_handle);
390
391#endif
392
393

source code of linux/include/linux/firmware/intel/stratix10-svc-client.h