1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2017-2018, Intel Corporation
4 * Copyright (C) 2025, Altera Corporation
5 */
6
7 #include <linux/atomic.h>
8 #include <linux/completion.h>
9 #include <linux/delay.h>
10 #include <linux/genalloc.h>
11 #include <linux/hashtable.h>
12 #include <linux/idr.h>
13 #include <linux/io.h>
14 #include <linux/kfifo.h>
15 #include <linux/kthread.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/of.h>
19 #include <linux/of_platform.h>
20 #include <linux/platform_device.h>
21 #include <linux/slab.h>
22 #include <linux/spinlock.h>
23 #include <linux/firmware/intel/stratix10-smc.h>
24 #include <linux/firmware/intel/stratix10-svc-client.h>
25 #include <linux/types.h>
26
27 /**
28 * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO
29 *
30 * SVC_NUM_CHANNEL - number of channel supported by service layer driver
31 *
32 * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s)
33 * from the secure world for FPGA manager to reuse, or to free the buffer(s)
34 * when all bit-stream data had be send.
35 *
36 * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status,
37 * service layer will return error to FPGA manager when timeout occurs,
38 * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC.
39 */
40 #define SVC_NUM_DATA_IN_FIFO 8
41 #define SVC_NUM_CHANNEL 4
42 #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 2000
43 #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30
44 #define BYTE_TO_WORD_SIZE 4
45
46 /* stratix10 service layer clients */
47 #define STRATIX10_RSU "stratix10-rsu"
48
49 /* Maximum number of SDM client IDs. */
50 #define MAX_SDM_CLIENT_IDS 16
51 /* Client ID for SIP Service Version 1. */
52 #define SIP_SVC_V1_CLIENT_ID 0x1
53 /* Maximum number of SDM job IDs. */
54 #define MAX_SDM_JOB_IDS 16
55 /* Number of bits used for asynchronous transaction hashing. */
56 #define ASYNC_TRX_HASH_BITS 3
57 /*
58 * Total number of transaction IDs, which is a combination of
59 * client ID and job ID.
60 */
61 #define TOTAL_TRANSACTION_IDS \
62 (MAX_SDM_CLIENT_IDS * MAX_SDM_JOB_IDS)
63
64 /* Minimum major version of the ATF for Asynchronous transactions. */
65 #define ASYNC_ATF_MINIMUM_MAJOR_VERSION 0x3
66 /* Minimum minor version of the ATF for Asynchronous transactions.*/
67 #define ASYNC_ATF_MINIMUM_MINOR_VERSION 0x0
68
69 /* Job ID field in the transaction ID */
70 #define STRATIX10_JOB_FIELD GENMASK(3, 0)
71 /* Client ID field in the transaction ID */
72 #define STRATIX10_CLIENT_FIELD GENMASK(7, 4)
73 /* Transaction ID mask for Stratix10 service layer */
74 #define STRATIX10_TRANS_ID_FIELD GENMASK(7, 0)
75
76 /* Macro to extract the job ID from a transaction ID. */
77 #define STRATIX10_GET_JOBID(transaction_id) \
78 (FIELD_GET(STRATIX10_JOB_FIELD, transaction_id))
79 /* Macro to set the job ID in a transaction ID. */
80 #define STRATIX10_SET_JOBID(jobid) \
81 (FIELD_PREP(STRATIX10_JOB_FIELD, jobid))
82 /* Macro to set the client ID in a transaction ID. */
83 #define STRATIX10_SET_CLIENTID(clientid) \
84 (FIELD_PREP(STRATIX10_CLIENT_FIELD, clientid))
85 /* Macro to set a transaction ID using a client ID and a job ID. */
86 #define STRATIX10_SET_TRANSACTIONID(clientid, jobid) \
87 (STRATIX10_SET_CLIENTID(clientid) | STRATIX10_SET_JOBID(jobid))
88 /* Macro to set a transaction ID for SIP SMC Async transactions */
89 #define STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(transaction_id) \
90 (FIELD_PREP(STRATIX10_TRANS_ID_FIELD, transaction_id))
91
92 /* 10-bit mask for extracting the SDM status code */
93 #define STRATIX10_SDM_STATUS_MASK GENMASK(9, 0)
94 /* Macro to get the SDM mailbox error status */
95 #define STRATIX10_GET_SDM_STATUS_CODE(status) \
96 (FIELD_GET(STRATIX10_SDM_STATUS_MASK, status))
97
98 typedef void (svc_invoke_fn)(unsigned long, unsigned long, unsigned long,
99 unsigned long, unsigned long, unsigned long,
100 unsigned long, unsigned long,
101 struct arm_smccc_res *);
102 struct stratix10_svc_chan;
103
104 /**
105 * struct stratix10_svc - svc private data
106 * @stratix10_svc_rsu: pointer to stratix10 RSU device
107 */
108 struct stratix10_svc {
109 struct platform_device *stratix10_svc_rsu;
110 };
111
112 /**
113 * struct stratix10_svc_sh_memory - service shared memory structure
114 * @sync_complete: state for a completion
115 * @addr: physical address of shared memory block
116 * @size: size of shared memory block
117 * @invoke_fn: service clients to handle secure monitor or hypervisor calls
118 *
119 * This struct is used to save physical address and size of shared memory
120 * block. The shared memory blocked is allocated by secure monitor software
121 * at secure world.
122 *
123 * Service layer driver uses the physical address and size to create a memory
124 * pool, then allocates data buffer from that memory pool for service client.
125 */
126 struct stratix10_svc_sh_memory {
127 struct completion sync_complete;
128 unsigned long addr;
129 unsigned long size;
130 svc_invoke_fn *invoke_fn;
131 };
132
133 /**
134 * struct stratix10_svc_data_mem - service memory structure
135 * @vaddr: virtual address
136 * @paddr: physical address
137 * @size: size of memory
138 * @node: link list head node
139 *
140 * This struct is used in a list that keeps track of buffers which have
141 * been allocated or freed from the memory pool. Service layer driver also
142 * uses this struct to transfer physical address to virtual address.
143 */
144 struct stratix10_svc_data_mem {
145 void *vaddr;
146 phys_addr_t paddr;
147 size_t size;
148 struct list_head node;
149 };
150
151 /**
152 * struct stratix10_svc_data - service data structure
153 * @chan: service channel
154 * @paddr: physical address of to be processed payload
155 * @size: to be processed playload size
156 * @paddr_output: physical address of processed payload
157 * @size_output: processed payload size
158 * @command: service command requested by client
159 * @flag: configuration type (full or partial)
160 * @arg: args to be passed via registers and not physically mapped buffers
161 *
162 * This struct is used in service FIFO for inter-process communication.
163 */
164 struct stratix10_svc_data {
165 struct stratix10_svc_chan *chan;
166 phys_addr_t paddr;
167 size_t size;
168 phys_addr_t paddr_output;
169 size_t size_output;
170 u32 command;
171 u32 flag;
172 u64 arg[3];
173 };
174
175 /**
176 * struct stratix10_svc_async_handler - Asynchronous handler for Stratix10
177 * service layer
178 * @transaction_id: Unique identifier for the transaction
179 * @achan: Pointer to the asynchronous channel structure
180 * @cb_arg: Argument to be passed to the callback function
181 * @cb: Callback function to be called upon completion
182 * @msg: Pointer to the client message structure
183 * @next: Node in the hash list
184 * @res: Response structure to store result from the secure firmware
185 *
186 * This structure is used to handle asynchronous transactions in the
187 * Stratix10 service layer. It maintains the necessary information
188 * for processing and completing asynchronous requests.
189 */
190
191 struct stratix10_svc_async_handler {
192 u8 transaction_id;
193 struct stratix10_async_chan *achan;
194 void *cb_arg;
195 async_callback_t cb;
196 struct stratix10_svc_client_msg *msg;
197 struct hlist_node next;
198 struct arm_smccc_1_2_regs res;
199 };
200
201 /**
202 * struct stratix10_async_chan - Structure representing an asynchronous channel
203 * @async_client_id: Unique client identifier for the asynchronous operation
204 * @job_id_pool: Pointer to the job ID pool associated with this channel
205 */
206
207 struct stratix10_async_chan {
208 unsigned long async_client_id;
209 struct ida job_id_pool;
210 };
211
212 /**
213 * struct stratix10_async_ctrl - Control structure for Stratix10
214 * asynchronous operations
215 * @initialized: Flag indicating whether the control structure has
216 * been initialized
217 * @invoke_fn: Function pointer for invoking Stratix10 service calls
218 * to EL3 secure firmware
219 * @async_id_pool: Pointer to the ID pool used for asynchronous
220 * operations
221 * @common_achan_refcount: Atomic reference count for the common
222 * asynchronous channel usage
223 * @common_async_chan: Pointer to the common asynchronous channel
224 * structure
225 * @trx_list_lock: Spinlock for protecting the transaction list
226 * operations
227 * @trx_list: Hash table for managing asynchronous transactions
228 */
229
230 struct stratix10_async_ctrl {
231 bool initialized;
232 void (*invoke_fn)(struct stratix10_async_ctrl *actrl,
233 const struct arm_smccc_1_2_regs *args,
234 struct arm_smccc_1_2_regs *res);
235 struct ida async_id_pool;
236 atomic_t common_achan_refcount;
237 struct stratix10_async_chan *common_async_chan;
238 /* spinlock to protect trx_list hash table */
239 spinlock_t trx_list_lock;
240 DECLARE_HASHTABLE(trx_list, ASYNC_TRX_HASH_BITS);
241 };
242
243 /**
244 * struct stratix10_svc_controller - service controller
245 * @dev: device
246 * @chans: array of service channels
247 * @num_chans: number of channels in 'chans' array
248 * @num_active_client: number of active service client
249 * @node: list management
250 * @genpool: memory pool pointing to the memory region
251 * @complete_status: state for completion
252 * @invoke_fn: function to issue secure monitor call or hypervisor call
253 * @svc: manages the list of client svc drivers
254 * @sdm_lock: only allows a single command single response to SDM
255 * @actrl: async control structure
256 *
257 * This struct is used to create communication channels for service clients, to
258 * handle secure monitor or hypervisor call.
259 */
260 struct stratix10_svc_controller {
261 struct device *dev;
262 struct stratix10_svc_chan *chans;
263 int num_chans;
264 int num_active_client;
265 struct list_head node;
266 struct gen_pool *genpool;
267 struct completion complete_status;
268 svc_invoke_fn *invoke_fn;
269 struct stratix10_svc *svc;
270 struct mutex sdm_lock;
271 struct stratix10_async_ctrl actrl;
272 };
273
274 /**
275 * struct stratix10_svc_chan - service communication channel
276 * @ctrl: pointer to service controller which is the provider of this channel
277 * @scl: pointer to service client which owns the channel
278 * @name: service client name associated with the channel
279 * @task: pointer to the thread task which handles SMC or HVC call
280 * @svc_fifo: a queue for storing service message data (separate fifo for every channel)
281 * @svc_fifo_lock: protect access to service message data queue (locking pending fifo)
282 * @lock: protect access to the channel
283 * @async_chan: reference to asynchronous channel object for this channel
284 *
285 * This struct is used by service client to communicate with service layer.
286 * Each service client has its own channel created by service controller.
287 */
288 struct stratix10_svc_chan {
289 struct stratix10_svc_controller *ctrl;
290 struct stratix10_svc_client *scl;
291 char *name;
292 struct task_struct *task;
293 struct kfifo svc_fifo;
294 spinlock_t svc_fifo_lock;
295 spinlock_t lock;
296 struct stratix10_async_chan *async_chan;
297 };
298
299 static LIST_HEAD(svc_ctrl);
300 static LIST_HEAD(svc_data_mem);
301
302 /*
303 * svc_mem_lock protects access to the svc_data_mem list for
304 * concurrent multi-client operations
305 */
306 static DEFINE_MUTEX(svc_mem_lock);
307
308 /**
309 * svc_pa_to_va() - translate physical address to virtual address
310 * @addr: to be translated physical address
311 *
312 * Return: valid virtual address or NULL if the provided physical
313 * address doesn't exist.
314 */
svc_pa_to_va(unsigned long addr)315 static void *svc_pa_to_va(unsigned long addr)
316 {
317 struct stratix10_svc_data_mem *pmem;
318
319 pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr);
320 guard(mutex)(&svc_mem_lock);
321 list_for_each_entry(pmem, &svc_data_mem, node)
322 if (pmem->paddr == addr)
323 return pmem->vaddr;
324
325 /* physical address is not found */
326 return NULL;
327 }
328
329 /**
330 * svc_thread_cmd_data_claim() - claim back buffer from the secure world
331 * @ctrl: pointer to service layer controller
332 * @p_data: pointer to service data structure
333 * @cb_data: pointer to callback data structure to service client
334 *
335 * Claim back the submitted buffers from the secure world and pass buffer
336 * back to service client (FPGA manager, etc) for reuse.
337 */
svc_thread_cmd_data_claim(struct stratix10_svc_controller * ctrl,struct stratix10_svc_data * p_data,struct stratix10_svc_cb_data * cb_data)338 static void svc_thread_cmd_data_claim(struct stratix10_svc_controller *ctrl,
339 struct stratix10_svc_data *p_data,
340 struct stratix10_svc_cb_data *cb_data)
341 {
342 struct arm_smccc_res res;
343 unsigned long timeout;
344
345 reinit_completion(&ctrl->complete_status);
346 timeout = msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS);
347
348 pr_debug("%s: claim back the submitted buffer\n", __func__);
349 do {
350 ctrl->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE,
351 0, 0, 0, 0, 0, 0, 0, &res);
352
353 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
354 if (!res.a1) {
355 complete(&ctrl->complete_status);
356 break;
357 }
358 cb_data->status = BIT(SVC_STATUS_BUFFER_DONE);
359 cb_data->kaddr1 = svc_pa_to_va(res.a1);
360 cb_data->kaddr2 = (res.a2) ?
361 svc_pa_to_va(res.a2) : NULL;
362 cb_data->kaddr3 = (res.a3) ?
363 svc_pa_to_va(res.a3) : NULL;
364 p_data->chan->scl->receive_cb(p_data->chan->scl,
365 cb_data);
366 } else {
367 pr_debug("%s: secure world busy, polling again\n",
368 __func__);
369 }
370 } while (res.a0 == INTEL_SIP_SMC_STATUS_OK ||
371 res.a0 == INTEL_SIP_SMC_STATUS_BUSY ||
372 wait_for_completion_timeout(&ctrl->complete_status, timeout));
373 }
374
375 /**
376 * svc_thread_cmd_config_status() - check configuration status
377 * @ctrl: pointer to service layer controller
378 * @p_data: pointer to service data structure
379 * @cb_data: pointer to callback data structure to service client
380 *
381 * Check whether the secure firmware at secure world has finished the FPGA
382 * configuration, and then inform FPGA manager the configuration status.
383 */
svc_thread_cmd_config_status(struct stratix10_svc_controller * ctrl,struct stratix10_svc_data * p_data,struct stratix10_svc_cb_data * cb_data)384 static void svc_thread_cmd_config_status(struct stratix10_svc_controller *ctrl,
385 struct stratix10_svc_data *p_data,
386 struct stratix10_svc_cb_data *cb_data)
387 {
388 struct arm_smccc_res res;
389 int count_in_sec;
390 unsigned long a0, a1, a2;
391
392 cb_data->kaddr1 = NULL;
393 cb_data->kaddr2 = NULL;
394 cb_data->kaddr3 = NULL;
395 cb_data->status = BIT(SVC_STATUS_ERROR);
396
397 pr_debug("%s: polling config status\n", __func__);
398
399 a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
400 a1 = (unsigned long)p_data->paddr;
401 a2 = (unsigned long)p_data->size;
402
403 if (p_data->command == COMMAND_POLL_SERVICE_STATUS)
404 a0 = INTEL_SIP_SMC_SERVICE_COMPLETED;
405
406 count_in_sec = FPGA_CONFIG_STATUS_TIMEOUT_SEC;
407 while (count_in_sec) {
408 ctrl->invoke_fn(a0, a1, a2, 0, 0, 0, 0, 0, &res);
409 if ((res.a0 == INTEL_SIP_SMC_STATUS_OK) ||
410 (res.a0 == INTEL_SIP_SMC_STATUS_ERROR) ||
411 (res.a0 == INTEL_SIP_SMC_STATUS_REJECTED))
412 break;
413
414 /*
415 * request is still in progress, wait one second then
416 * poll again
417 */
418 msleep(1000);
419 count_in_sec--;
420 }
421
422 if (!count_in_sec) {
423 pr_err("%s: poll status timeout\n", __func__);
424 cb_data->status = BIT(SVC_STATUS_BUSY);
425 } else if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
426 cb_data->status = BIT(SVC_STATUS_COMPLETED);
427 cb_data->kaddr2 = (res.a2) ?
428 svc_pa_to_va(res.a2) : NULL;
429 cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL;
430 } else {
431 pr_err("%s: poll status error\n", __func__);
432 cb_data->kaddr1 = &res.a1;
433 cb_data->kaddr2 = (res.a2) ?
434 svc_pa_to_va(res.a2) : NULL;
435 cb_data->kaddr3 = (res.a3) ? &res.a3 : NULL;
436 cb_data->status = BIT(SVC_STATUS_ERROR);
437 }
438
439 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
440 }
441
442 /**
443 * svc_thread_recv_status_ok() - handle the successful status
444 * @p_data: pointer to service data structure
445 * @cb_data: pointer to callback data structure to service client
446 * @res: result from SMC or HVC call
447 *
448 * Send back the correspond status to the service clients.
449 */
svc_thread_recv_status_ok(struct stratix10_svc_data * p_data,struct stratix10_svc_cb_data * cb_data,struct arm_smccc_res res)450 static void svc_thread_recv_status_ok(struct stratix10_svc_data *p_data,
451 struct stratix10_svc_cb_data *cb_data,
452 struct arm_smccc_res res)
453 {
454 cb_data->kaddr1 = NULL;
455 cb_data->kaddr2 = NULL;
456 cb_data->kaddr3 = NULL;
457
458 switch (p_data->command) {
459 case COMMAND_RECONFIG:
460 case COMMAND_RSU_UPDATE:
461 case COMMAND_RSU_NOTIFY:
462 case COMMAND_FCS_REQUEST_SERVICE:
463 case COMMAND_FCS_SEND_CERTIFICATE:
464 case COMMAND_FCS_DATA_ENCRYPTION:
465 case COMMAND_FCS_DATA_DECRYPTION:
466 cb_data->status = BIT(SVC_STATUS_OK);
467 break;
468 case COMMAND_RECONFIG_DATA_SUBMIT:
469 cb_data->status = BIT(SVC_STATUS_BUFFER_SUBMITTED);
470 break;
471 case COMMAND_RECONFIG_STATUS:
472 cb_data->status = BIT(SVC_STATUS_COMPLETED);
473 break;
474 case COMMAND_RSU_RETRY:
475 case COMMAND_RSU_MAX_RETRY:
476 case COMMAND_RSU_DCMF_STATUS:
477 case COMMAND_FIRMWARE_VERSION:
478 case COMMAND_HWMON_READTEMP:
479 case COMMAND_HWMON_READVOLT:
480 cb_data->status = BIT(SVC_STATUS_OK);
481 cb_data->kaddr1 = &res.a1;
482 break;
483 case COMMAND_SMC_SVC_VERSION:
484 cb_data->status = BIT(SVC_STATUS_OK);
485 cb_data->kaddr1 = &res.a1;
486 cb_data->kaddr2 = &res.a2;
487 break;
488 case COMMAND_RSU_DCMF_VERSION:
489 cb_data->status = BIT(SVC_STATUS_OK);
490 cb_data->kaddr1 = &res.a1;
491 cb_data->kaddr2 = &res.a2;
492 break;
493 case COMMAND_FCS_RANDOM_NUMBER_GEN:
494 case COMMAND_FCS_GET_PROVISION_DATA:
495 case COMMAND_POLL_SERVICE_STATUS:
496 cb_data->status = BIT(SVC_STATUS_OK);
497 cb_data->kaddr1 = &res.a1;
498 cb_data->kaddr2 = svc_pa_to_va(res.a2);
499 cb_data->kaddr3 = &res.a3;
500 break;
501 case COMMAND_MBOX_SEND_CMD:
502 cb_data->status = BIT(SVC_STATUS_OK);
503 cb_data->kaddr1 = &res.a1;
504 /* SDM return size in u8. Convert size to u32 word */
505 res.a2 = res.a2 * BYTE_TO_WORD_SIZE;
506 cb_data->kaddr2 = &res.a2;
507 break;
508 default:
509 pr_warn("it shouldn't happen\n");
510 break;
511 }
512
513 pr_debug("%s: call receive_cb\n", __func__);
514 p_data->chan->scl->receive_cb(p_data->chan->scl, cb_data);
515 }
516
517 /**
518 * svc_normal_to_secure_thread() - the function to run in the kthread
519 * @data: data pointer for kthread function
520 *
521 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
522 * node 0, its function stratix10_svc_secure_call_thread is used to handle
523 * SMC or HVC calls between kernel driver and secure monitor software.
524 *
525 * Return: 0 for success or -ENOMEM on error.
526 */
svc_normal_to_secure_thread(void * data)527 static int svc_normal_to_secure_thread(void *data)
528 {
529 struct stratix10_svc_chan *chan = (struct stratix10_svc_chan *)data;
530 struct stratix10_svc_controller *ctrl = chan->ctrl;
531 struct stratix10_svc_data *pdata = NULL;
532 struct stratix10_svc_cb_data *cbdata = NULL;
533 struct arm_smccc_res res;
534 unsigned long a0, a1, a2, a3, a4, a5, a6, a7;
535 int ret_fifo = 0;
536
537 pdata = kmalloc_obj(*pdata);
538 if (!pdata)
539 return -ENOMEM;
540
541 cbdata = kmalloc_obj(*cbdata);
542 if (!cbdata) {
543 kfree(pdata);
544 return -ENOMEM;
545 }
546
547 /* default set, to remove build warning */
548 a0 = INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK;
549 a1 = 0;
550 a2 = 0;
551 a3 = 0;
552 a4 = 0;
553 a5 = 0;
554 a6 = 0;
555 a7 = 0;
556
557 pr_debug("%s: %s: Thread is running!\n", __func__, chan->name);
558
559 while (!kthread_should_stop()) {
560 ret_fifo = kfifo_out_spinlocked(&chan->svc_fifo,
561 pdata, sizeof(*pdata),
562 &chan->svc_fifo_lock);
563
564 if (!ret_fifo)
565 continue;
566
567 pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
568 (unsigned int)pdata->paddr, pdata->command,
569 (unsigned int)pdata->size);
570
571 /* SDM can only process one command at a time */
572 pr_debug("%s: %s: Thread is waiting for mutex!\n",
573 __func__, chan->name);
574 if (mutex_lock_interruptible(&ctrl->sdm_lock)) {
575 /* item already dequeued; notify client to unblock it */
576 cbdata->status = BIT(SVC_STATUS_ERROR);
577 cbdata->kaddr1 = NULL;
578 cbdata->kaddr2 = NULL;
579 cbdata->kaddr3 = NULL;
580 if (pdata->chan->scl)
581 pdata->chan->scl->receive_cb(pdata->chan->scl,
582 cbdata);
583 break;
584 }
585
586 switch (pdata->command) {
587 case COMMAND_RECONFIG_DATA_CLAIM:
588 svc_thread_cmd_data_claim(ctrl, pdata, cbdata);
589 mutex_unlock(&ctrl->sdm_lock);
590 continue;
591 case COMMAND_RECONFIG:
592 a0 = INTEL_SIP_SMC_FPGA_CONFIG_START;
593 pr_debug("conf_type=%u\n", (unsigned int)pdata->flag);
594 a1 = pdata->flag;
595 a2 = 0;
596 break;
597 case COMMAND_RECONFIG_DATA_SUBMIT:
598 a0 = INTEL_SIP_SMC_FPGA_CONFIG_WRITE;
599 a1 = (unsigned long)pdata->paddr;
600 a2 = (unsigned long)pdata->size;
601 break;
602 case COMMAND_RECONFIG_STATUS:
603 a0 = INTEL_SIP_SMC_FPGA_CONFIG_ISDONE;
604 a1 = 0;
605 a2 = 0;
606 break;
607 case COMMAND_RSU_STATUS:
608 a0 = INTEL_SIP_SMC_RSU_STATUS;
609 a1 = 0;
610 a2 = 0;
611 break;
612 case COMMAND_RSU_UPDATE:
613 a0 = INTEL_SIP_SMC_RSU_UPDATE;
614 a1 = pdata->arg[0];
615 a2 = 0;
616 break;
617 case COMMAND_RSU_NOTIFY:
618 a0 = INTEL_SIP_SMC_RSU_NOTIFY;
619 a1 = pdata->arg[0];
620 a2 = 0;
621 break;
622 case COMMAND_RSU_RETRY:
623 a0 = INTEL_SIP_SMC_RSU_RETRY_COUNTER;
624 a1 = 0;
625 a2 = 0;
626 break;
627 case COMMAND_RSU_MAX_RETRY:
628 a0 = INTEL_SIP_SMC_RSU_MAX_RETRY;
629 a1 = 0;
630 a2 = 0;
631 break;
632 case COMMAND_RSU_DCMF_VERSION:
633 a0 = INTEL_SIP_SMC_RSU_DCMF_VERSION;
634 a1 = 0;
635 a2 = 0;
636 break;
637 case COMMAND_FIRMWARE_VERSION:
638 a0 = INTEL_SIP_SMC_FIRMWARE_VERSION;
639 a1 = 0;
640 a2 = 0;
641 break;
642
643 /* for FCS */
644 case COMMAND_FCS_DATA_ENCRYPTION:
645 a0 = INTEL_SIP_SMC_FCS_CRYPTION;
646 a1 = 1;
647 a2 = (unsigned long)pdata->paddr;
648 a3 = (unsigned long)pdata->size;
649 a4 = (unsigned long)pdata->paddr_output;
650 a5 = (unsigned long)pdata->size_output;
651 break;
652 case COMMAND_FCS_DATA_DECRYPTION:
653 a0 = INTEL_SIP_SMC_FCS_CRYPTION;
654 a1 = 0;
655 a2 = (unsigned long)pdata->paddr;
656 a3 = (unsigned long)pdata->size;
657 a4 = (unsigned long)pdata->paddr_output;
658 a5 = (unsigned long)pdata->size_output;
659 break;
660 case COMMAND_FCS_RANDOM_NUMBER_GEN:
661 a0 = INTEL_SIP_SMC_FCS_RANDOM_NUMBER;
662 a1 = (unsigned long)pdata->paddr;
663 a2 = 0;
664 break;
665 case COMMAND_FCS_REQUEST_SERVICE:
666 a0 = INTEL_SIP_SMC_FCS_SERVICE_REQUEST;
667 a1 = (unsigned long)pdata->paddr;
668 a2 = (unsigned long)pdata->size;
669 break;
670 case COMMAND_FCS_SEND_CERTIFICATE:
671 a0 = INTEL_SIP_SMC_FCS_SEND_CERTIFICATE;
672 a1 = (unsigned long)pdata->paddr;
673 a2 = (unsigned long)pdata->size;
674 break;
675 case COMMAND_FCS_GET_PROVISION_DATA:
676 a0 = INTEL_SIP_SMC_FCS_GET_PROVISION_DATA;
677 a1 = (unsigned long)pdata->paddr;
678 a2 = 0;
679 break;
680 /* for HWMON */
681 case COMMAND_HWMON_READTEMP:
682 a0 = INTEL_SIP_SMC_HWMON_READTEMP;
683 a1 = pdata->arg[0];
684 a2 = 0;
685 break;
686 case COMMAND_HWMON_READVOLT:
687 a0 = INTEL_SIP_SMC_HWMON_READVOLT;
688 a1 = pdata->arg[0];
689 a2 = 0;
690 break;
691 /* for polling */
692 case COMMAND_POLL_SERVICE_STATUS:
693 a0 = INTEL_SIP_SMC_SERVICE_COMPLETED;
694 a1 = (unsigned long)pdata->paddr;
695 a2 = (unsigned long)pdata->size;
696 break;
697 case COMMAND_RSU_DCMF_STATUS:
698 a0 = INTEL_SIP_SMC_RSU_DCMF_STATUS;
699 a1 = 0;
700 a2 = 0;
701 break;
702 case COMMAND_SMC_SVC_VERSION:
703 a0 = INTEL_SIP_SMC_SVC_VERSION;
704 a1 = 0;
705 a2 = 0;
706 break;
707 case COMMAND_MBOX_SEND_CMD:
708 a0 = INTEL_SIP_SMC_MBOX_SEND_CMD;
709 a1 = pdata->arg[0];
710 a2 = (unsigned long)pdata->paddr;
711 a3 = (unsigned long)pdata->size / BYTE_TO_WORD_SIZE;
712 a4 = pdata->arg[1];
713 a5 = (unsigned long)pdata->paddr_output;
714 a6 = (unsigned long)pdata->size_output / BYTE_TO_WORD_SIZE;
715 break;
716 default:
717 pr_warn("it shouldn't happen\n");
718 mutex_unlock(&ctrl->sdm_lock);
719 continue;
720 }
721 pr_debug("%s: %s: before SMC call -- a0=0x%016x a1=0x%016x",
722 __func__, chan->name,
723 (unsigned int)a0,
724 (unsigned int)a1);
725 pr_debug(" a2=0x%016x\n", (unsigned int)a2);
726 pr_debug(" a3=0x%016x\n", (unsigned int)a3);
727 pr_debug(" a4=0x%016x\n", (unsigned int)a4);
728 pr_debug(" a5=0x%016x\n", (unsigned int)a5);
729 ctrl->invoke_fn(a0, a1, a2, a3, a4, a5, a6, a7, &res);
730
731 pr_debug("%s: %s: after SMC call -- res.a0=0x%016x",
732 __func__, chan->name, (unsigned int)res.a0);
733 pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
734 (unsigned int)res.a1, (unsigned int)res.a2);
735 pr_debug(" res.a3=0x%016x\n", (unsigned int)res.a3);
736
737 if (pdata->command == COMMAND_RSU_STATUS) {
738 if (res.a0 == INTEL_SIP_SMC_RSU_ERROR)
739 cbdata->status = BIT(SVC_STATUS_ERROR);
740 else
741 cbdata->status = BIT(SVC_STATUS_OK);
742
743 cbdata->kaddr1 = &res;
744 cbdata->kaddr2 = NULL;
745 cbdata->kaddr3 = NULL;
746 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
747 mutex_unlock(&ctrl->sdm_lock);
748 continue;
749 }
750
751 switch (res.a0) {
752 case INTEL_SIP_SMC_STATUS_OK:
753 svc_thread_recv_status_ok(pdata, cbdata, res);
754 break;
755 case INTEL_SIP_SMC_STATUS_BUSY:
756 switch (pdata->command) {
757 case COMMAND_RECONFIG_DATA_SUBMIT:
758 svc_thread_cmd_data_claim(ctrl,
759 pdata, cbdata);
760 break;
761 case COMMAND_RECONFIG_STATUS:
762 case COMMAND_POLL_SERVICE_STATUS:
763 svc_thread_cmd_config_status(ctrl,
764 pdata, cbdata);
765 break;
766 default:
767 pr_warn("it shouldn't happen\n");
768 break;
769 }
770 break;
771 case INTEL_SIP_SMC_STATUS_REJECTED:
772 pr_debug("%s: STATUS_REJECTED\n", __func__);
773 /* for FCS */
774 switch (pdata->command) {
775 case COMMAND_FCS_REQUEST_SERVICE:
776 case COMMAND_FCS_SEND_CERTIFICATE:
777 case COMMAND_FCS_GET_PROVISION_DATA:
778 case COMMAND_FCS_DATA_ENCRYPTION:
779 case COMMAND_FCS_DATA_DECRYPTION:
780 case COMMAND_FCS_RANDOM_NUMBER_GEN:
781 case COMMAND_MBOX_SEND_CMD:
782 cbdata->status = BIT(SVC_STATUS_INVALID_PARAM);
783 cbdata->kaddr1 = NULL;
784 cbdata->kaddr2 = NULL;
785 cbdata->kaddr3 = NULL;
786 pdata->chan->scl->receive_cb(pdata->chan->scl,
787 cbdata);
788 break;
789 }
790 break;
791 case INTEL_SIP_SMC_STATUS_ERROR:
792 case INTEL_SIP_SMC_RSU_ERROR:
793 pr_err("%s: STATUS_ERROR\n", __func__);
794 cbdata->status = BIT(SVC_STATUS_ERROR);
795 cbdata->kaddr1 = &res.a1;
796 cbdata->kaddr2 = (res.a2) ?
797 svc_pa_to_va(res.a2) : NULL;
798 cbdata->kaddr3 = (res.a3) ? &res.a3 : NULL;
799 pdata->chan->scl->receive_cb(pdata->chan->scl, cbdata);
800 break;
801 default:
802 pr_warn("Secure firmware doesn't support...\n");
803
804 /*
805 * be compatible with older version firmware which
806 * doesn't support newer RSU commands
807 */
808 if ((pdata->command != COMMAND_RSU_UPDATE) &&
809 (pdata->command != COMMAND_RSU_STATUS)) {
810 cbdata->status =
811 BIT(SVC_STATUS_NO_SUPPORT);
812 cbdata->kaddr1 = NULL;
813 cbdata->kaddr2 = NULL;
814 cbdata->kaddr3 = NULL;
815 pdata->chan->scl->receive_cb(
816 pdata->chan->scl, cbdata);
817 }
818 break;
819
820 }
821
822 mutex_unlock(&ctrl->sdm_lock);
823 }
824
825 kfree(cbdata);
826 kfree(pdata);
827
828 return 0;
829 }
830
831 /**
832 * svc_normal_to_secure_shm_thread() - the function to run in the kthread
833 * @data: data pointer for kthread function
834 *
835 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
836 * node 0, its function stratix10_svc_secure_shm_thread is used to query the
837 * physical address of memory block reserved by secure monitor software at
838 * secure world.
839 *
840 * svc_normal_to_secure_shm_thread() terminates directly since it is a
841 * standlone thread for which no one will call kthread_stop() or return when
842 * 'kthread_should_stop()' is true.
843 */
svc_normal_to_secure_shm_thread(void * data)844 static int svc_normal_to_secure_shm_thread(void *data)
845 {
846 struct stratix10_svc_sh_memory
847 *sh_mem = (struct stratix10_svc_sh_memory *)data;
848 struct arm_smccc_res res;
849
850 /* SMC or HVC call to get shared memory info from secure world */
851 sh_mem->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM,
852 0, 0, 0, 0, 0, 0, 0, &res);
853 if (res.a0 == INTEL_SIP_SMC_STATUS_OK) {
854 sh_mem->addr = res.a1;
855 sh_mem->size = res.a2;
856 } else {
857 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__,
858 (unsigned int)res.a0);
859 sh_mem->addr = 0;
860 sh_mem->size = 0;
861 }
862
863 complete(&sh_mem->sync_complete);
864 return 0;
865 }
866
867 /**
868 * svc_get_sh_memory() - get memory block reserved by secure monitor SW
869 * @pdev: pointer to service layer device
870 * @sh_memory: pointer to service shared memory structure
871 *
872 * Return: zero for successfully getting the physical address of memory block
873 * reserved by secure monitor software, or negative value on error.
874 */
svc_get_sh_memory(struct platform_device * pdev,struct stratix10_svc_sh_memory * sh_memory)875 static int svc_get_sh_memory(struct platform_device *pdev,
876 struct stratix10_svc_sh_memory *sh_memory)
877 {
878 struct device *dev = &pdev->dev;
879 struct task_struct *sh_memory_task;
880 unsigned int cpu = 0;
881
882 init_completion(&sh_memory->sync_complete);
883
884 /* smc or hvc call happens on cpu 0 bound kthread */
885 sh_memory_task = kthread_create_on_node(svc_normal_to_secure_shm_thread,
886 (void *)sh_memory,
887 cpu_to_node(cpu),
888 "svc_smc_hvc_shm_thread");
889 if (IS_ERR(sh_memory_task)) {
890 dev_err(dev, "fail to create stratix10_svc_smc_shm_thread\n");
891 return -EINVAL;
892 }
893
894 wake_up_process(sh_memory_task);
895
896 if (!wait_for_completion_timeout(&sh_memory->sync_complete, 10 * HZ)) {
897 dev_err(dev,
898 "timeout to get sh-memory paras from secure world\n");
899 return -ETIMEDOUT;
900 }
901
902 if (!sh_memory->addr || !sh_memory->size) {
903 dev_err(dev,
904 "failed to get shared memory info from secure world\n");
905 return -ENOMEM;
906 }
907
908 dev_dbg(dev, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
909 (unsigned int)sh_memory->addr,
910 (unsigned int)sh_memory->size);
911
912 return 0;
913 }
914
915 /**
916 * svc_create_memory_pool() - create a memory pool from reserved memory block
917 * @pdev: pointer to service layer device
918 * @sh_memory: pointer to service shared memory structure
919 *
920 * Return: pool allocated from reserved memory block or ERR_PTR() on error.
921 */
922 static struct gen_pool *
svc_create_memory_pool(struct platform_device * pdev,struct stratix10_svc_sh_memory * sh_memory)923 svc_create_memory_pool(struct platform_device *pdev,
924 struct stratix10_svc_sh_memory *sh_memory)
925 {
926 struct device *dev = &pdev->dev;
927 struct gen_pool *genpool;
928 unsigned long vaddr;
929 phys_addr_t paddr;
930 size_t size;
931 phys_addr_t begin;
932 phys_addr_t end;
933 void *va;
934 size_t page_mask = PAGE_SIZE - 1;
935 int min_alloc_order = 3;
936 int ret;
937
938 begin = roundup(sh_memory->addr, PAGE_SIZE);
939 end = rounddown(sh_memory->addr + sh_memory->size, PAGE_SIZE);
940 paddr = begin;
941 size = end - begin;
942 va = devm_memremap(dev, paddr, size, MEMREMAP_WC);
943 if (IS_ERR(va)) {
944 dev_err(dev, "fail to remap shared memory\n");
945 return ERR_PTR(-EINVAL);
946 }
947 vaddr = (unsigned long)va;
948 dev_dbg(dev,
949 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
950 va, (unsigned int)paddr, (unsigned int)size);
951 if ((vaddr & page_mask) || (paddr & page_mask) ||
952 (size & page_mask)) {
953 dev_err(dev, "page is not aligned\n");
954 return ERR_PTR(-EINVAL);
955 }
956 genpool = gen_pool_create(min_alloc_order, -1);
957 if (!genpool) {
958 dev_err(dev, "fail to create genpool\n");
959 return ERR_PTR(-ENOMEM);
960 }
961 gen_pool_set_algo(genpool, gen_pool_best_fit, NULL);
962 ret = gen_pool_add_virt(genpool, vaddr, paddr, size, -1);
963 if (ret) {
964 dev_err(dev, "fail to add memory chunk to the pool\n");
965 gen_pool_destroy(genpool);
966 return ERR_PTR(ret);
967 }
968
969 return genpool;
970 }
971
972 /**
973 * svc_smccc_smc() - secure monitor call between normal and secure world
974 * @a0: argument passed in registers 0
975 * @a1: argument passed in registers 1
976 * @a2: argument passed in registers 2
977 * @a3: argument passed in registers 3
978 * @a4: argument passed in registers 4
979 * @a5: argument passed in registers 5
980 * @a6: argument passed in registers 6
981 * @a7: argument passed in registers 7
982 * @res: result values from register 0 to 3
983 */
svc_smccc_smc(unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,unsigned long a6,unsigned long a7,struct arm_smccc_res * res)984 static void svc_smccc_smc(unsigned long a0, unsigned long a1,
985 unsigned long a2, unsigned long a3,
986 unsigned long a4, unsigned long a5,
987 unsigned long a6, unsigned long a7,
988 struct arm_smccc_res *res)
989 {
990 arm_smccc_smc(a0, a1, a2, a3, a4, a5, a6, a7, res);
991 }
992
993 /**
994 * svc_smccc_hvc() - hypervisor call between normal and secure world
995 * @a0: argument passed in registers 0
996 * @a1: argument passed in registers 1
997 * @a2: argument passed in registers 2
998 * @a3: argument passed in registers 3
999 * @a4: argument passed in registers 4
1000 * @a5: argument passed in registers 5
1001 * @a6: argument passed in registers 6
1002 * @a7: argument passed in registers 7
1003 * @res: result values from register 0 to 3
1004 */
svc_smccc_hvc(unsigned long a0,unsigned long a1,unsigned long a2,unsigned long a3,unsigned long a4,unsigned long a5,unsigned long a6,unsigned long a7,struct arm_smccc_res * res)1005 static void svc_smccc_hvc(unsigned long a0, unsigned long a1,
1006 unsigned long a2, unsigned long a3,
1007 unsigned long a4, unsigned long a5,
1008 unsigned long a6, unsigned long a7,
1009 struct arm_smccc_res *res)
1010 {
1011 arm_smccc_hvc(a0, a1, a2, a3, a4, a5, a6, a7, res);
1012 }
1013
1014 /**
1015 * get_invoke_func() - invoke SMC or HVC call
1016 * @dev: pointer to device
1017 *
1018 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
1019 */
get_invoke_func(struct device * dev)1020 static svc_invoke_fn *get_invoke_func(struct device *dev)
1021 {
1022 const char *method;
1023
1024 if (of_property_read_string(dev->of_node, "method", &method)) {
1025 dev_warn(dev, "missing \"method\" property\n");
1026 return ERR_PTR(-ENXIO);
1027 }
1028
1029 if (!strcmp(method, "smc"))
1030 return svc_smccc_smc;
1031 if (!strcmp(method, "hvc"))
1032 return svc_smccc_hvc;
1033
1034 dev_warn(dev, "invalid \"method\" property: %s\n", method);
1035
1036 return ERR_PTR(-EINVAL);
1037 }
1038
1039 /**
1040 * stratix10_svc_request_channel_byname() - request a service channel
1041 * @client: pointer to service client
1042 * @name: service client name
1043 *
1044 * This function is used by service client to request a service channel.
1045 *
1046 * Return: a pointer to channel assigned to the client on success,
1047 * or ERR_PTR() on error.
1048 */
stratix10_svc_request_channel_byname(struct stratix10_svc_client * client,const char * name)1049 struct stratix10_svc_chan *stratix10_svc_request_channel_byname(
1050 struct stratix10_svc_client *client, const char *name)
1051 {
1052 struct device *dev = client->dev;
1053 struct stratix10_svc_controller *controller;
1054 struct stratix10_svc_chan *chan = NULL;
1055 unsigned long flag;
1056 int i;
1057
1058 /* if probe was called after client's, or error on probe */
1059 if (list_empty(&svc_ctrl))
1060 return ERR_PTR(-EPROBE_DEFER);
1061
1062 controller = list_first_entry(&svc_ctrl,
1063 struct stratix10_svc_controller, node);
1064 for (i = 0; i < SVC_NUM_CHANNEL; i++) {
1065 if (!strcmp(controller->chans[i].name, name)) {
1066 chan = &controller->chans[i];
1067 break;
1068 }
1069 }
1070
1071 /* if there was no channel match */
1072 if (i == SVC_NUM_CHANNEL) {
1073 dev_err(dev, "%s: channel not allocated\n", __func__);
1074 return ERR_PTR(-EINVAL);
1075 }
1076
1077 if (chan->scl || !try_module_get(controller->dev->driver->owner)) {
1078 dev_dbg(dev, "%s: svc not free\n", __func__);
1079 return ERR_PTR(-EBUSY);
1080 }
1081
1082 spin_lock_irqsave(&chan->lock, flag);
1083 chan->scl = client;
1084 chan->ctrl->num_active_client++;
1085 spin_unlock_irqrestore(&chan->lock, flag);
1086
1087 return chan;
1088 }
1089 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname);
1090
1091 /**
1092 * stratix10_svc_add_async_client - Add an asynchronous client to the
1093 * Stratix10 service channel.
1094 * @chan: Pointer to the Stratix10 service channel structure.
1095 * @use_unique_clientid: Boolean flag indicating whether to use a
1096 * unique client ID.
1097 *
1098 * This function adds an asynchronous client to the specified
1099 * Stratix10 service channel. If the `use_unique_clientid` flag is
1100 * set to true, a unique client ID is allocated for the asynchronous
1101 * channel. Otherwise, a common asynchronous channel is used.
1102 *
1103 * Return: 0 on success, or a negative error code on failure:
1104 * -EINVAL if the channel is NULL or the async controller is
1105 * not initialized.
1106 * -EALREADY if the async channel is already allocated.
1107 * -ENOMEM if memory allocation fails.
1108 * Other negative values if ID allocation fails.
1109 */
stratix10_svc_add_async_client(struct stratix10_svc_chan * chan,bool use_unique_clientid)1110 int stratix10_svc_add_async_client(struct stratix10_svc_chan *chan,
1111 bool use_unique_clientid)
1112 {
1113 struct stratix10_svc_controller *ctrl;
1114 struct stratix10_async_ctrl *actrl;
1115 struct stratix10_async_chan *achan;
1116 int ret = 0;
1117
1118 if (!chan)
1119 return -EINVAL;
1120
1121 ctrl = chan->ctrl;
1122 actrl = &ctrl->actrl;
1123
1124 if (!actrl->initialized) {
1125 dev_err(ctrl->dev, "Async controller not initialized\n");
1126 return -EINVAL;
1127 }
1128
1129 if (chan->async_chan) {
1130 dev_err(ctrl->dev, "async channel already allocated\n");
1131 return -EALREADY;
1132 }
1133
1134 if (use_unique_clientid &&
1135 atomic_read(&actrl->common_achan_refcount) > 0) {
1136 chan->async_chan = actrl->common_async_chan;
1137 atomic_inc(&actrl->common_achan_refcount);
1138 return 0;
1139 }
1140
1141 achan = kzalloc_obj(*achan);
1142 if (!achan)
1143 return -ENOMEM;
1144
1145 ida_init(&achan->job_id_pool);
1146
1147 ret = ida_alloc_max(&actrl->async_id_pool, MAX_SDM_CLIENT_IDS,
1148 GFP_KERNEL);
1149 if (ret < 0) {
1150 dev_err(ctrl->dev,
1151 "Failed to allocate async client id\n");
1152 ida_destroy(&achan->job_id_pool);
1153 kfree(achan);
1154 return ret;
1155 }
1156
1157 achan->async_client_id = ret;
1158 chan->async_chan = achan;
1159
1160 if (use_unique_clientid &&
1161 atomic_read(&actrl->common_achan_refcount) == 0) {
1162 actrl->common_async_chan = achan;
1163 atomic_inc(&actrl->common_achan_refcount);
1164 }
1165
1166 return 0;
1167 }
1168 EXPORT_SYMBOL_GPL(stratix10_svc_add_async_client);
1169
1170 /**
1171 * stratix10_svc_remove_async_client - Remove an asynchronous client
1172 * from the Stratix10 service
1173 * channel.
1174 * @chan: Pointer to the Stratix10 service channel structure.
1175 *
1176 * This function removes an asynchronous client associated with the
1177 * given service channel. It checks if the channel and the
1178 * asynchronous channel are valid, and then proceeds to decrement
1179 * the reference count for the common asynchronous channel if
1180 * applicable. If the reference count reaches zero, it destroys the
1181 * job ID pool and deallocates the asynchronous client ID. For
1182 * non-common asynchronous channels, it directly destroys the job ID
1183 * pool, deallocates the asynchronous client ID, and frees the
1184 * memory allocated for the asynchronous channel.
1185 *
1186 * Return: 0 on success, -EINVAL if the channel or asynchronous
1187 * channel is invalid.
1188 */
stratix10_svc_remove_async_client(struct stratix10_svc_chan * chan)1189 int stratix10_svc_remove_async_client(struct stratix10_svc_chan *chan)
1190 {
1191 struct stratix10_svc_controller *ctrl;
1192 struct stratix10_async_ctrl *actrl;
1193 struct stratix10_async_chan *achan;
1194
1195 if (!chan)
1196 return -EINVAL;
1197
1198 ctrl = chan->ctrl;
1199 actrl = &ctrl->actrl;
1200 achan = chan->async_chan;
1201
1202 if (!achan) {
1203 dev_err(ctrl->dev, "async channel not allocated\n");
1204 return -EINVAL;
1205 }
1206
1207 if (achan == actrl->common_async_chan) {
1208 atomic_dec(&actrl->common_achan_refcount);
1209 if (atomic_read(&actrl->common_achan_refcount) == 0) {
1210 ida_destroy(&achan->job_id_pool);
1211 ida_free(&actrl->async_id_pool,
1212 achan->async_client_id);
1213 kfree(achan);
1214 actrl->common_async_chan = NULL;
1215 }
1216 } else {
1217 ida_destroy(&achan->job_id_pool);
1218 ida_free(&actrl->async_id_pool, achan->async_client_id);
1219 kfree(achan);
1220 }
1221 chan->async_chan = NULL;
1222
1223 return 0;
1224 }
1225 EXPORT_SYMBOL_GPL(stratix10_svc_remove_async_client);
1226
1227 /**
1228 * stratix10_svc_async_send - Send an asynchronous message to the
1229 * Stratix10 service
1230 * @chan: Pointer to the service channel structure
1231 * @msg: Pointer to the message to be sent
1232 * @handler: Pointer to the handler for the asynchronous message
1233 * used by caller for later reference.
1234 * @cb: Callback function to be called upon completion
1235 * @cb_arg: Argument to be passed to the callback function
1236 *
1237 * This function sends an asynchronous message to the SDM mailbox in
1238 * EL3 secure firmware. It performs various checks and setups,
1239 * including allocating a job ID, setting up the transaction ID and
1240 * packaging it to El3 firmware. The function handles different
1241 * commands by setting up the appropriate arguments for the SMC call.
1242 * If the SMC call is successful, the handler is set up and the
1243 * function returns 0. If the SMC call fails, appropriate error
1244 * handling is performed along with cleanup of resources.
1245 *
1246 * Return: 0 on success, -EINVAL for invalid argument, -ENOMEM if
1247 * memory is not available, -EAGAIN if EL3 firmware is busy, -EBADF
1248 * if the message is rejected by EL3 firmware and -EIO on other
1249 * errors from EL3 firmware.
1250 */
stratix10_svc_async_send(struct stratix10_svc_chan * chan,void * msg,void ** handler,async_callback_t cb,void * cb_arg)1251 int stratix10_svc_async_send(struct stratix10_svc_chan *chan, void *msg,
1252 void **handler, async_callback_t cb, void *cb_arg)
1253 {
1254 struct arm_smccc_1_2_regs args = { 0 }, res = { 0 };
1255 struct stratix10_svc_async_handler *handle = NULL;
1256 struct stratix10_svc_client_msg *p_msg =
1257 (struct stratix10_svc_client_msg *)msg;
1258 struct stratix10_svc_controller *ctrl;
1259 struct stratix10_async_ctrl *actrl;
1260 struct stratix10_async_chan *achan;
1261 int ret = 0;
1262
1263 if (!chan || !msg || !handler)
1264 return -EINVAL;
1265
1266 achan = chan->async_chan;
1267 ctrl = chan->ctrl;
1268 actrl = &ctrl->actrl;
1269
1270 if (!actrl->initialized) {
1271 dev_err(ctrl->dev, "Async controller not initialized\n");
1272 return -EINVAL;
1273 }
1274
1275 if (!achan) {
1276 dev_err(ctrl->dev, "Async channel not allocated\n");
1277 return -EINVAL;
1278 }
1279
1280 handle = kzalloc(sizeof(*handle), GFP_KERNEL);
1281 if (!handle)
1282 return -ENOMEM;
1283
1284 ret = ida_alloc_max(&achan->job_id_pool, MAX_SDM_JOB_IDS,
1285 GFP_KERNEL);
1286 if (ret < 0) {
1287 dev_err(ctrl->dev, "Failed to allocate job id\n");
1288 kfree(handle);
1289 return -ENOMEM;
1290 }
1291
1292 handle->transaction_id =
1293 STRATIX10_SET_TRANSACTIONID(achan->async_client_id, ret);
1294 handle->cb = cb;
1295 handle->msg = p_msg;
1296 handle->cb_arg = cb_arg;
1297 handle->achan = achan;
1298
1299 /*set the transaction jobid in args.a1*/
1300 args.a1 =
1301 STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id);
1302
1303 switch (p_msg->command) {
1304 case COMMAND_RSU_GET_SPT_TABLE:
1305 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_SPT;
1306 break;
1307 case COMMAND_RSU_STATUS:
1308 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_GET_ERROR_STATUS;
1309 break;
1310 case COMMAND_RSU_NOTIFY:
1311 args.a0 = INTEL_SIP_SMC_ASYNC_RSU_NOTIFY;
1312 args.a2 = p_msg->arg[0];
1313 break;
1314 default:
1315 dev_err(ctrl->dev, "Invalid command ,%d\n", p_msg->command);
1316 ret = -EINVAL;
1317 goto deallocate_id;
1318 }
1319
1320 /**
1321 * There is a chance that during the execution of async_send()
1322 * in one core, an interrupt might be received in another core;
1323 * to mitigate this we are adding the handle to the DB and then
1324 * send the smc call. If the smc call is rejected or busy then
1325 * we will deallocate the handle for the client to retry again.
1326 */
1327 scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
1328 hash_add(actrl->trx_list, &handle->next,
1329 handle->transaction_id);
1330 }
1331
1332 actrl->invoke_fn(actrl, &args, &res);
1333
1334 switch (res.a0) {
1335 case INTEL_SIP_SMC_STATUS_OK:
1336 dev_dbg(ctrl->dev,
1337 "Async message sent with transaction_id 0x%02x\n",
1338 handle->transaction_id);
1339 *handler = handle;
1340 return 0;
1341 case INTEL_SIP_SMC_STATUS_BUSY:
1342 dev_warn(ctrl->dev, "Mailbox is busy, try after some time\n");
1343 ret = -EAGAIN;
1344 break;
1345 case INTEL_SIP_SMC_STATUS_REJECTED:
1346 dev_err(ctrl->dev, "Async message rejected\n");
1347 ret = -EBADF;
1348 break;
1349 default:
1350 dev_err(ctrl->dev,
1351 "Failed to send async message ,got status as %ld\n",
1352 res.a0);
1353 ret = -EIO;
1354 }
1355
1356 scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
1357 hash_del(&handle->next);
1358 }
1359
1360 deallocate_id:
1361 ida_free(&achan->job_id_pool,
1362 STRATIX10_GET_JOBID(handle->transaction_id));
1363 kfree(handle);
1364 return ret;
1365 }
1366 EXPORT_SYMBOL_GPL(stratix10_svc_async_send);
1367
1368 /**
1369 * stratix10_svc_async_prepare_response - Prepare the response data for
1370 * an asynchronous transaction.
1371 * @chan: Pointer to the service channel structure.
1372 * @handle: Pointer to the asynchronous handler structure.
1373 * @data: Pointer to the callback data structure.
1374 *
1375 * This function prepares the response data for an asynchronous transaction. It
1376 * extracts the response data from the SMC response structure and stores it in
1377 * the callback data structure. The function also logs the completion of the
1378 * asynchronous transaction.
1379 *
1380 * Return: 0 on success, -ENOENT if the command is invalid
1381 */
stratix10_svc_async_prepare_response(struct stratix10_svc_chan * chan,struct stratix10_svc_async_handler * handle,struct stratix10_svc_cb_data * data)1382 static int stratix10_svc_async_prepare_response(struct stratix10_svc_chan *chan,
1383 struct stratix10_svc_async_handler *handle,
1384 struct stratix10_svc_cb_data *data)
1385 {
1386 struct stratix10_svc_client_msg *p_msg =
1387 (struct stratix10_svc_client_msg *)handle->msg;
1388 struct stratix10_svc_controller *ctrl = chan->ctrl;
1389
1390 data->status = STRATIX10_GET_SDM_STATUS_CODE(handle->res.a1);
1391
1392 switch (p_msg->command) {
1393 case COMMAND_RSU_NOTIFY:
1394 break;
1395 case COMMAND_RSU_GET_SPT_TABLE:
1396 data->kaddr1 = (void *)&handle->res.a2;
1397 data->kaddr2 = (void *)&handle->res.a3;
1398 break;
1399 case COMMAND_RSU_STATUS:
1400 /* COMMAND_RSU_STATUS has more elements than the cb_data
1401 * can acomodate, so passing the response structure to the
1402 * response function to be handled before done command is
1403 * executed by the client.
1404 */
1405 data->kaddr1 = (void *)&handle->res;
1406 break;
1407
1408 default:
1409 dev_alert(ctrl->dev, "Invalid command\n ,%d", p_msg->command);
1410 return -ENOENT;
1411 }
1412 dev_dbg(ctrl->dev, "Async message completed transaction_id 0x%02x\n",
1413 handle->transaction_id);
1414 return 0;
1415 }
1416
1417 /**
1418 * stratix10_svc_async_poll - Polls the status of an asynchronous
1419 * transaction.
1420 * @chan: Pointer to the service channel structure.
1421 * @tx_handle: Handle to the transaction being polled.
1422 * @data: Pointer to the callback data structure.
1423 *
1424 * This function polls the status of an asynchronous transaction
1425 * identified by the given transaction handle. It ensures that the
1426 * necessary structures are initialized and valid before proceeding
1427 * with the poll operation. The function sets up the necessary
1428 * arguments for the SMC call, invokes the call, and prepares the
1429 * response data if the call is successful. If the call fails, the
1430 * function returns the error mapped to the SVC status error.
1431 *
1432 * Return: 0 on success, -EINVAL if any input parameter is invalid,
1433 * -EAGAIN if the transaction is still in progress,
1434 * -EPERM if the command is invalid, or other negative
1435 * error codes on failure.
1436 */
stratix10_svc_async_poll(struct stratix10_svc_chan * chan,void * tx_handle,struct stratix10_svc_cb_data * data)1437 int stratix10_svc_async_poll(struct stratix10_svc_chan *chan,
1438 void *tx_handle,
1439 struct stratix10_svc_cb_data *data)
1440 {
1441 struct stratix10_svc_async_handler *handle;
1442 struct arm_smccc_1_2_regs args = { 0 };
1443 struct stratix10_svc_controller *ctrl;
1444 struct stratix10_async_ctrl *actrl;
1445 struct stratix10_async_chan *achan;
1446 int ret;
1447
1448 if (!chan || !tx_handle || !data)
1449 return -EINVAL;
1450
1451 ctrl = chan->ctrl;
1452 actrl = &ctrl->actrl;
1453 achan = chan->async_chan;
1454
1455 if (!achan) {
1456 dev_err(ctrl->dev, "Async channel not allocated\n");
1457 return -EINVAL;
1458 }
1459
1460 handle = (struct stratix10_svc_async_handler *)tx_handle;
1461 scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
1462 if (!hash_hashed(&handle->next)) {
1463 dev_err(ctrl->dev, "Invalid transaction handler");
1464 return -EINVAL;
1465 }
1466 }
1467
1468 args.a0 = INTEL_SIP_SMC_ASYNC_POLL;
1469 args.a1 =
1470 STRATIX10_SIP_SMC_SET_TRANSACTIONID_X1(handle->transaction_id);
1471
1472 actrl->invoke_fn(actrl, &args, &handle->res);
1473
1474 /*clear data for response*/
1475 memset(data, 0, sizeof(*data));
1476
1477 if (handle->res.a0 == INTEL_SIP_SMC_STATUS_OK) {
1478 ret = stratix10_svc_async_prepare_response(chan, handle, data);
1479 if (ret) {
1480 dev_err(ctrl->dev, "Error in preparation of response,%d\n", ret);
1481 WARN_ON_ONCE(1);
1482 }
1483 return 0;
1484 } else if (handle->res.a0 == INTEL_SIP_SMC_STATUS_BUSY) {
1485 dev_dbg(ctrl->dev, "async message is still in progress\n");
1486 return -EAGAIN;
1487 }
1488
1489 dev_err(ctrl->dev,
1490 "Failed to poll async message ,got status as %ld\n",
1491 handle->res.a0);
1492 return -EINVAL;
1493 }
1494 EXPORT_SYMBOL_GPL(stratix10_svc_async_poll);
1495
1496 /**
1497 * stratix10_svc_async_done - Completes an asynchronous transaction.
1498 * @chan: Pointer to the service channel structure.
1499 * @tx_handle: Handle to the transaction being completed.
1500 *
1501 * This function completes an asynchronous transaction identified by
1502 * the given transaction handle. It ensures that the necessary
1503 * structures are initialized and valid before proceeding with the
1504 * completion operation. The function deallocates the transaction ID,
1505 * frees the memory allocated for the handler, and removes the handler
1506 * from the transaction list.
1507 *
1508 * Return: 0 on success, -EINVAL if any input parameter is invalid,
1509 * or other negative error codes on failure.
1510 */
stratix10_svc_async_done(struct stratix10_svc_chan * chan,void * tx_handle)1511 int stratix10_svc_async_done(struct stratix10_svc_chan *chan, void *tx_handle)
1512 {
1513 struct stratix10_svc_async_handler *handle;
1514 struct stratix10_svc_controller *ctrl;
1515 struct stratix10_async_chan *achan;
1516 struct stratix10_async_ctrl *actrl;
1517
1518 if (!chan || !tx_handle)
1519 return -EINVAL;
1520
1521 ctrl = chan->ctrl;
1522 achan = chan->async_chan;
1523 actrl = &ctrl->actrl;
1524
1525 if (!achan) {
1526 dev_err(ctrl->dev, "async channel not allocated\n");
1527 return -EINVAL;
1528 }
1529
1530 handle = (struct stratix10_svc_async_handler *)tx_handle;
1531 scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
1532 if (!hash_hashed(&handle->next)) {
1533 dev_err(ctrl->dev, "Invalid transaction handle");
1534 return -EINVAL;
1535 }
1536 hash_del(&handle->next);
1537 }
1538 ida_free(&achan->job_id_pool,
1539 STRATIX10_GET_JOBID(handle->transaction_id));
1540 kfree(handle);
1541 return 0;
1542 }
1543 EXPORT_SYMBOL_GPL(stratix10_svc_async_done);
1544
stratix10_smc_1_2(struct stratix10_async_ctrl * actrl,const struct arm_smccc_1_2_regs * args,struct arm_smccc_1_2_regs * res)1545 static inline void stratix10_smc_1_2(struct stratix10_async_ctrl *actrl,
1546 const struct arm_smccc_1_2_regs *args,
1547 struct arm_smccc_1_2_regs *res)
1548 {
1549 arm_smccc_1_2_smc(args, res);
1550 }
1551
1552 /**
1553 * stratix10_svc_async_init - Initialize the Stratix10 service
1554 * controller for asynchronous operations.
1555 * @controller: Pointer to the Stratix10 service controller structure.
1556 *
1557 * This function initializes the asynchronous service controller by
1558 * setting up the necessary data structures and initializing the
1559 * transaction list.
1560 *
1561 * Return: 0 on success, -EINVAL if the controller is NULL or already
1562 * initialized, -ENOMEM if memory allocation fails,
1563 * -EADDRINUSE if the client ID is already reserved, or other
1564 * negative error codes on failure.
1565 */
stratix10_svc_async_init(struct stratix10_svc_controller * controller)1566 static int stratix10_svc_async_init(struct stratix10_svc_controller *controller)
1567 {
1568 struct stratix10_async_ctrl *actrl;
1569 struct arm_smccc_res res;
1570 struct device *dev;
1571 int ret;
1572
1573 if (!controller)
1574 return -EINVAL;
1575
1576 actrl = &controller->actrl;
1577
1578 if (actrl->initialized)
1579 return -EINVAL;
1580
1581 dev = controller->dev;
1582
1583 controller->invoke_fn(INTEL_SIP_SMC_SVC_VERSION, 0, 0, 0, 0, 0, 0, 0, &res);
1584 if (res.a0 != INTEL_SIP_SMC_STATUS_OK ||
1585 !(res.a1 > ASYNC_ATF_MINIMUM_MAJOR_VERSION ||
1586 (res.a1 == ASYNC_ATF_MINIMUM_MAJOR_VERSION &&
1587 res.a2 >= ASYNC_ATF_MINIMUM_MINOR_VERSION))) {
1588 dev_err(dev,
1589 "Intel Service Layer Driver: ATF version is not compatible for async operation\n");
1590 return -EINVAL;
1591 }
1592
1593 actrl->invoke_fn = stratix10_smc_1_2;
1594
1595 ida_init(&actrl->async_id_pool);
1596
1597 /**
1598 * SIP_SVC_V1_CLIENT_ID is used by V1/stratix10_svc_send() clients
1599 * for communicating with SDM synchronously. We need to restrict
1600 * this in V3/stratix10_svc_async_send() usage to distinguish
1601 * between V1 and V3 messages in El3 firmware.
1602 */
1603 ret = ida_alloc_range(&actrl->async_id_pool, SIP_SVC_V1_CLIENT_ID,
1604 SIP_SVC_V1_CLIENT_ID, GFP_KERNEL);
1605 if (ret < 0) {
1606 dev_err(dev,
1607 "Intel Service Layer Driver: Error on reserving SIP_SVC_V1_CLIENT_ID\n");
1608 ida_destroy(&actrl->async_id_pool);
1609 actrl->invoke_fn = NULL;
1610 return -EADDRINUSE;
1611 }
1612
1613 spin_lock_init(&actrl->trx_list_lock);
1614 hash_init(actrl->trx_list);
1615 atomic_set(&actrl->common_achan_refcount, 0);
1616
1617 actrl->initialized = true;
1618 return 0;
1619 }
1620
1621 /**
1622 * stratix10_svc_async_exit - Clean up and exit the asynchronous
1623 * service controller
1624 * @ctrl: Pointer to the stratix10_svc_controller structure
1625 *
1626 * This function performs the necessary cleanup for the asynchronous
1627 * service controller. It checks if the controller is valid and if it
1628 * has been initialized. It then locks the transaction list and safely
1629 * removes and deallocates each handler in the list. The function also
1630 * removes any asynchronous clients associated with the controller's
1631 * channels and destroys the asynchronous ID pool. Finally, it resets
1632 * the asynchronous ID pool and invoke function pointers to NULL.
1633 *
1634 * Return: 0 on success, -EINVAL if the controller is invalid or not
1635 * initialized.
1636 */
stratix10_svc_async_exit(struct stratix10_svc_controller * ctrl)1637 static int stratix10_svc_async_exit(struct stratix10_svc_controller *ctrl)
1638 {
1639 struct stratix10_svc_async_handler *handler;
1640 struct stratix10_async_ctrl *actrl;
1641 struct hlist_node *tmp;
1642 int i;
1643
1644 if (!ctrl)
1645 return -EINVAL;
1646
1647 actrl = &ctrl->actrl;
1648
1649 if (!actrl->initialized)
1650 return -EINVAL;
1651
1652 actrl->initialized = false;
1653
1654 scoped_guard(spinlock_bh, &actrl->trx_list_lock) {
1655 hash_for_each_safe(actrl->trx_list, i, tmp, handler, next) {
1656 ida_free(&handler->achan->job_id_pool,
1657 STRATIX10_GET_JOBID(handler->transaction_id));
1658 hash_del(&handler->next);
1659 kfree(handler);
1660 }
1661 }
1662
1663 for (i = 0; i < SVC_NUM_CHANNEL; i++) {
1664 if (ctrl->chans[i].async_chan) {
1665 stratix10_svc_remove_async_client(&ctrl->chans[i]);
1666 ctrl->chans[i].async_chan = NULL;
1667 }
1668 }
1669
1670 ida_destroy(&actrl->async_id_pool);
1671 actrl->invoke_fn = NULL;
1672
1673 return 0;
1674 }
1675
1676 /**
1677 * stratix10_svc_free_channel() - free service channel
1678 * @chan: service channel to be freed
1679 *
1680 * This function is used by service client to free a service channel.
1681 */
stratix10_svc_free_channel(struct stratix10_svc_chan * chan)1682 void stratix10_svc_free_channel(struct stratix10_svc_chan *chan)
1683 {
1684 unsigned long flag;
1685
1686 spin_lock_irqsave(&chan->lock, flag);
1687 chan->scl = NULL;
1688 chan->ctrl->num_active_client--;
1689 module_put(chan->ctrl->dev->driver->owner);
1690 spin_unlock_irqrestore(&chan->lock, flag);
1691 }
1692 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel);
1693
1694 /**
1695 * stratix10_svc_send() - send a message data to the remote
1696 * @chan: service channel assigned to the client
1697 * @msg: message data to be sent, in the format of
1698 * "struct stratix10_svc_client_msg"
1699 *
1700 * This function is used by service client to add a message to the service
1701 * layer driver's queue for being sent to the secure world.
1702 *
1703 * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
1704 */
stratix10_svc_send(struct stratix10_svc_chan * chan,void * msg)1705 int stratix10_svc_send(struct stratix10_svc_chan *chan, void *msg)
1706 {
1707 struct stratix10_svc_client_msg
1708 *p_msg = (struct stratix10_svc_client_msg *)msg;
1709 struct stratix10_svc_data_mem *p_mem;
1710 struct stratix10_svc_data *p_data;
1711 int ret = 0;
1712 unsigned int cpu = 0;
1713
1714 p_data = kzalloc_obj(*p_data);
1715 if (!p_data)
1716 return -ENOMEM;
1717
1718 /* first caller creates the per-channel kthread */
1719 if (!chan->task) {
1720 struct task_struct *task;
1721
1722 task = kthread_run_on_cpu(svc_normal_to_secure_thread,
1723 (void *)chan,
1724 cpu, "svc_smc_hvc_thread");
1725 if (IS_ERR(task)) {
1726 dev_err(chan->ctrl->dev,
1727 "failed to create svc_smc_hvc_thread\n");
1728 kfree(p_data);
1729 return -EINVAL;
1730 }
1731
1732 spin_lock(&chan->lock);
1733 if (chan->task) {
1734 /* another caller won the race; discard our thread */
1735 spin_unlock(&chan->lock);
1736 kthread_stop(task);
1737 } else {
1738 chan->task = task;
1739 spin_unlock(&chan->lock);
1740 }
1741 }
1742
1743 pr_debug("%s: %s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__,
1744 chan->name, p_msg->payload, p_msg->command,
1745 (unsigned int)p_msg->payload_length);
1746
1747 if (list_empty(&svc_data_mem)) {
1748 if (p_msg->command == COMMAND_RECONFIG) {
1749 struct stratix10_svc_command_config_type *ct =
1750 (struct stratix10_svc_command_config_type *)
1751 p_msg->payload;
1752 p_data->flag = ct->flags;
1753 }
1754 } else {
1755 guard(mutex)(&svc_mem_lock);
1756 list_for_each_entry(p_mem, &svc_data_mem, node)
1757 if (p_mem->vaddr == p_msg->payload) {
1758 p_data->paddr = p_mem->paddr;
1759 p_data->size = p_msg->payload_length;
1760 break;
1761 }
1762 if (p_msg->payload_output) {
1763 list_for_each_entry(p_mem, &svc_data_mem, node)
1764 if (p_mem->vaddr == p_msg->payload_output) {
1765 p_data->paddr_output =
1766 p_mem->paddr;
1767 p_data->size_output =
1768 p_msg->payload_length_output;
1769 break;
1770 }
1771 }
1772 }
1773
1774 p_data->command = p_msg->command;
1775 p_data->arg[0] = p_msg->arg[0];
1776 p_data->arg[1] = p_msg->arg[1];
1777 p_data->arg[2] = p_msg->arg[2];
1778 p_data->size = p_msg->payload_length;
1779 p_data->chan = chan;
1780 pr_debug("%s: %s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n",
1781 __func__,
1782 chan->name,
1783 (unsigned int)p_data->paddr,
1784 p_data->command,
1785 (unsigned int)p_data->size);
1786
1787 ret = kfifo_in_spinlocked(&chan->svc_fifo, p_data,
1788 sizeof(*p_data),
1789 &chan->svc_fifo_lock);
1790
1791 kfree(p_data);
1792
1793 if (!ret)
1794 return -ENOBUFS;
1795
1796 return 0;
1797 }
1798 EXPORT_SYMBOL_GPL(stratix10_svc_send);
1799
1800 /**
1801 * stratix10_svc_done() - complete service request transactions
1802 * @chan: service channel assigned to the client
1803 *
1804 * This function should be called when client has finished its request
1805 * or there is an error in the request process. It allows the service layer
1806 * to stop the running thread to have maximize savings in kernel resources.
1807 */
stratix10_svc_done(struct stratix10_svc_chan * chan)1808 void stratix10_svc_done(struct stratix10_svc_chan *chan)
1809 {
1810 /* stop thread when thread is running */
1811 if (chan->task) {
1812 pr_debug("%s: %s: svc_smc_hvc_shm_thread is stopping\n",
1813 __func__, chan->name);
1814 kthread_stop(chan->task);
1815 chan->task = NULL;
1816 }
1817 }
1818 EXPORT_SYMBOL_GPL(stratix10_svc_done);
1819
1820 /**
1821 * stratix10_svc_allocate_memory() - allocate memory
1822 * @chan: service channel assigned to the client
1823 * @size: memory size requested by a specific service client
1824 *
1825 * Service layer allocates the requested number of bytes buffer from the
1826 * memory pool, service client uses this function to get allocated buffers.
1827 *
1828 * Return: address of allocated memory on success, or ERR_PTR() on error.
1829 */
stratix10_svc_allocate_memory(struct stratix10_svc_chan * chan,size_t size)1830 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan *chan,
1831 size_t size)
1832 {
1833 struct stratix10_svc_data_mem *pmem;
1834 unsigned long va;
1835 phys_addr_t pa;
1836 struct gen_pool *genpool = chan->ctrl->genpool;
1837 size_t s = roundup(size, 1 << genpool->min_alloc_order);
1838
1839 pmem = devm_kzalloc(chan->ctrl->dev, sizeof(*pmem), GFP_KERNEL);
1840 if (!pmem)
1841 return ERR_PTR(-ENOMEM);
1842
1843 guard(mutex)(&svc_mem_lock);
1844 va = gen_pool_alloc(genpool, s);
1845 if (!va)
1846 return ERR_PTR(-ENOMEM);
1847
1848 memset((void *)va, 0, s);
1849 pa = gen_pool_virt_to_phys(genpool, va);
1850
1851 pmem->vaddr = (void *)va;
1852 pmem->paddr = pa;
1853 pmem->size = s;
1854 list_add_tail(&pmem->node, &svc_data_mem);
1855 pr_debug("%s: %s: va=%p, pa=0x%016x\n", __func__,
1856 chan->name, pmem->vaddr, (unsigned int)pmem->paddr);
1857
1858 return (void *)va;
1859 }
1860 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory);
1861
1862 /**
1863 * stratix10_svc_free_memory() - free allocated memory
1864 * @chan: service channel assigned to the client
1865 * @kaddr: memory to be freed
1866 *
1867 * This function is used by service client to free allocated buffers.
1868 */
stratix10_svc_free_memory(struct stratix10_svc_chan * chan,void * kaddr)1869 void stratix10_svc_free_memory(struct stratix10_svc_chan *chan, void *kaddr)
1870 {
1871 struct stratix10_svc_data_mem *pmem;
1872 guard(mutex)(&svc_mem_lock);
1873
1874 list_for_each_entry(pmem, &svc_data_mem, node)
1875 if (pmem->vaddr == kaddr) {
1876 gen_pool_free(chan->ctrl->genpool,
1877 (unsigned long)kaddr, pmem->size);
1878 pmem->vaddr = NULL;
1879 list_del(&pmem->node);
1880 return;
1881 }
1882
1883 list_del(&svc_data_mem);
1884 }
1885 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory);
1886
1887 static const struct of_device_id stratix10_svc_drv_match[] = {
1888 {.compatible = "intel,stratix10-svc"},
1889 {.compatible = "intel,agilex-svc"},
1890 {},
1891 };
1892
1893 static const char * const chan_names[SVC_NUM_CHANNEL] = {
1894 SVC_CLIENT_FPGA,
1895 SVC_CLIENT_RSU,
1896 SVC_CLIENT_FCS,
1897 SVC_CLIENT_HWMON
1898 };
1899
stratix10_svc_drv_probe(struct platform_device * pdev)1900 static int stratix10_svc_drv_probe(struct platform_device *pdev)
1901 {
1902 struct device *dev = &pdev->dev;
1903 struct stratix10_svc_controller *controller;
1904 struct stratix10_svc_chan *chans;
1905 struct gen_pool *genpool;
1906 struct stratix10_svc_sh_memory *sh_memory;
1907 struct stratix10_svc *svc = NULL;
1908
1909 svc_invoke_fn *invoke_fn;
1910 size_t fifo_size;
1911 int ret, i = 0;
1912
1913 /* get SMC or HVC function */
1914 invoke_fn = get_invoke_func(dev);
1915 if (IS_ERR(invoke_fn))
1916 return -EINVAL;
1917
1918 sh_memory = devm_kzalloc(dev, sizeof(*sh_memory), GFP_KERNEL);
1919 if (!sh_memory)
1920 return -ENOMEM;
1921
1922 sh_memory->invoke_fn = invoke_fn;
1923 ret = svc_get_sh_memory(pdev, sh_memory);
1924 if (ret)
1925 return ret;
1926
1927 genpool = svc_create_memory_pool(pdev, sh_memory);
1928 if (IS_ERR(genpool))
1929 return PTR_ERR(genpool);
1930
1931 /* allocate service controller and supporting channel */
1932 controller = devm_kzalloc(dev, sizeof(*controller), GFP_KERNEL);
1933 if (!controller) {
1934 ret = -ENOMEM;
1935 goto err_destroy_pool;
1936 }
1937
1938 chans = devm_kmalloc_array(dev, SVC_NUM_CHANNEL,
1939 sizeof(*chans), GFP_KERNEL | __GFP_ZERO);
1940 if (!chans) {
1941 ret = -ENOMEM;
1942 goto err_destroy_pool;
1943 }
1944
1945 controller->dev = dev;
1946 controller->num_chans = SVC_NUM_CHANNEL;
1947 controller->num_active_client = 0;
1948 controller->chans = chans;
1949 controller->genpool = genpool;
1950 controller->invoke_fn = invoke_fn;
1951 INIT_LIST_HEAD(&controller->node);
1952 init_completion(&controller->complete_status);
1953
1954 ret = stratix10_svc_async_init(controller);
1955 if (ret) {
1956 dev_dbg(dev, "Intel Service Layer Driver: Error on stratix10_svc_async_init %d\n",
1957 ret);
1958 goto err_destroy_pool;
1959 }
1960
1961 fifo_size = sizeof(struct stratix10_svc_data) * SVC_NUM_DATA_IN_FIFO;
1962 mutex_init(&controller->sdm_lock);
1963
1964 for (i = 0; i < SVC_NUM_CHANNEL; i++) {
1965 chans[i].scl = NULL;
1966 chans[i].ctrl = controller;
1967 chans[i].name = (char *)chan_names[i];
1968 spin_lock_init(&chans[i].lock);
1969 ret = kfifo_alloc(&chans[i].svc_fifo, fifo_size, GFP_KERNEL);
1970 if (ret) {
1971 dev_err(dev, "failed to allocate FIFO %d\n", i);
1972 goto err_free_fifos;
1973 }
1974 spin_lock_init(&chans[i].svc_fifo_lock);
1975 }
1976
1977 list_add_tail(&controller->node, &svc_ctrl);
1978 platform_set_drvdata(pdev, controller);
1979
1980 /* add svc client device(s) */
1981 svc = devm_kzalloc(dev, sizeof(*svc), GFP_KERNEL);
1982 if (!svc) {
1983 ret = -ENOMEM;
1984 goto err_free_fifos;
1985 }
1986 controller->svc = svc;
1987
1988 svc->stratix10_svc_rsu = platform_device_alloc(STRATIX10_RSU, 0);
1989 if (!svc->stratix10_svc_rsu) {
1990 dev_err(dev, "failed to allocate %s device\n", STRATIX10_RSU);
1991 ret = -ENOMEM;
1992 goto err_free_fifos;
1993 }
1994
1995 ret = platform_device_add(svc->stratix10_svc_rsu);
1996 if (ret)
1997 goto err_put_device;
1998
1999 ret = of_platform_default_populate(dev_of_node(dev), NULL, dev);
2000 if (ret)
2001 goto err_unregister_rsu_dev;
2002
2003 pr_info("Intel Service Layer Driver Initialized\n");
2004
2005 return 0;
2006
2007 err_unregister_rsu_dev:
2008 platform_device_unregister(svc->stratix10_svc_rsu);
2009 goto err_free_fifos;
2010 err_put_device:
2011 platform_device_put(svc->stratix10_svc_rsu);
2012 err_free_fifos:
2013 /* only remove from list if list_add_tail() was reached */
2014 if (!list_empty(&controller->node))
2015 list_del(&controller->node);
2016 /* free only the FIFOs that were successfully allocated */
2017 while (i--)
2018 kfifo_free(&chans[i].svc_fifo);
2019 stratix10_svc_async_exit(controller);
2020 err_destroy_pool:
2021 gen_pool_destroy(genpool);
2022
2023 return ret;
2024 }
2025
stratix10_svc_drv_remove(struct platform_device * pdev)2026 static void stratix10_svc_drv_remove(struct platform_device *pdev)
2027 {
2028 int i;
2029 struct stratix10_svc_controller *ctrl = platform_get_drvdata(pdev);
2030 struct stratix10_svc *svc = ctrl->svc;
2031
2032 stratix10_svc_async_exit(ctrl);
2033
2034 of_platform_depopulate(ctrl->dev);
2035
2036 platform_device_unregister(svc->stratix10_svc_rsu);
2037
2038 for (i = 0; i < SVC_NUM_CHANNEL; i++) {
2039 if (ctrl->chans[i].task) {
2040 kthread_stop(ctrl->chans[i].task);
2041 ctrl->chans[i].task = NULL;
2042 }
2043 kfifo_free(&ctrl->chans[i].svc_fifo);
2044 }
2045
2046 if (ctrl->genpool)
2047 gen_pool_destroy(ctrl->genpool);
2048 list_del(&ctrl->node);
2049 }
2050
2051 static struct platform_driver stratix10_svc_driver = {
2052 .probe = stratix10_svc_drv_probe,
2053 .remove = stratix10_svc_drv_remove,
2054 .driver = {
2055 .name = "stratix10-svc",
2056 .of_match_table = stratix10_svc_drv_match,
2057 },
2058 };
2059
stratix10_svc_init(void)2060 static int __init stratix10_svc_init(void)
2061 {
2062 struct device_node *fw_np;
2063 struct device_node *np;
2064 int ret;
2065
2066 fw_np = of_find_node_by_name(NULL, "firmware");
2067 if (!fw_np)
2068 return -ENODEV;
2069
2070 np = of_find_matching_node(fw_np, stratix10_svc_drv_match);
2071 if (!np)
2072 return -ENODEV;
2073
2074 of_node_put(np);
2075 ret = of_platform_populate(fw_np, stratix10_svc_drv_match, NULL, NULL);
2076 if (ret)
2077 return ret;
2078
2079 return platform_driver_register(&stratix10_svc_driver);
2080 }
2081
stratix10_svc_exit(void)2082 static void __exit stratix10_svc_exit(void)
2083 {
2084 return platform_driver_unregister(&stratix10_svc_driver);
2085 }
2086
2087 subsys_initcall(stratix10_svc_init);
2088 module_exit(stratix10_svc_exit);
2089
2090 MODULE_LICENSE("GPL v2");
2091 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
2092 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
2093 MODULE_ALIAS("platform:stratix10-svc");
2094