1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2017-2018, Intel Corporation
6 #include <linux/completion.h>
7 #include <linux/delay.h>
8 #include <linux/genalloc.h>
10 #include <linux/kfifo.h>
11 #include <linux/kthread.h>
12 #include <linux/module.h>
13 #include <linux/mutex.h>
15 #include <linux/of_platform.h>
16 #include <linux/platform_device.h>
17 #include <linux/slab.h>
18 #include <linux/spinlock.h>
19 #include <linux/firmware/intel/stratix10-smc.h>
20 #include <linux/firmware/intel/stratix10-svc-client.h>
21 #include <linux/types.h>
24 * SVC_NUM_DATA_IN_FIFO - number of struct stratix10_svc_data in the FIFO
26 * SVC_NUM_CHANNEL - number of channel supported by service layer driver
28 * FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS - claim back the submitted buffer(s)
29 * from the secure world for FPGA manager to reuse, or to free the buffer(s)
30 * when all bit-stream data had be send.
32 * FPGA_CONFIG_STATUS_TIMEOUT_SEC - poll the FPGA configuration status,
33 * service layer will return error to FPGA manager when timeout occurs,
34 * timeout is set to 30 seconds (30 * 1000) at Intel Stratix10 SoC.
36 #define SVC_NUM_DATA_IN_FIFO 32
37 #define SVC_NUM_CHANNEL 3
38 #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 200
39 #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30
40 #define BYTE_TO_WORD_SIZE 4
42 /* stratix10 service layer clients */
43 #define STRATIX10_RSU "stratix10-rsu"
44 #define INTEL_FCS "intel-fcs"
46 typedef void (svc_invoke_fn
)(unsigned long, unsigned long, unsigned long,
47 unsigned long, unsigned long, unsigned long,
48 unsigned long, unsigned long,
49 struct arm_smccc_res
*);
50 struct stratix10_svc_chan
;
53 * struct stratix10_svc - svc private data
54 * @stratix10_svc_rsu: pointer to stratix10 RSU device
56 struct stratix10_svc
{
57 struct platform_device
*stratix10_svc_rsu
;
58 struct platform_device
*intel_svc_fcs
;
62 * struct stratix10_svc_sh_memory - service shared memory structure
63 * @sync_complete: state for a completion
64 * @addr: physical address of shared memory block
65 * @size: size of shared memory block
66 * @invoke_fn: function to issue secure monitor or hypervisor call
68 * This struct is used to save physical address and size of shared memory
69 * block. The shared memory blocked is allocated by secure monitor software
72 * Service layer driver uses the physical address and size to create a memory
73 * pool, then allocates data buffer from that memory pool for service client.
75 struct stratix10_svc_sh_memory
{
76 struct completion sync_complete
;
79 svc_invoke_fn
*invoke_fn
;
83 * struct stratix10_svc_data_mem - service memory structure
84 * @vaddr: virtual address
85 * @paddr: physical address
86 * @size: size of memory
87 * @node: link list head node
89 * This struct is used in a list that keeps track of buffers which have
90 * been allocated or freed from the memory pool. Service layer driver also
91 * uses this struct to transfer physical address to virtual address.
93 struct stratix10_svc_data_mem
{
97 struct list_head node
;
101 * struct stratix10_svc_data - service data structure
102 * @chan: service channel
103 * @paddr: physical address of to be processed payload
104 * @size: to be processed playload size
105 * @paddr_output: physical address of processed payload
106 * @size_output: processed payload size
107 * @command: service command requested by client
108 * @flag: configuration type (full or partial)
109 * @arg: args to be passed via registers and not physically mapped buffers
111 * This struct is used in service FIFO for inter-process communication.
113 struct stratix10_svc_data
{
114 struct stratix10_svc_chan
*chan
;
117 phys_addr_t paddr_output
;
125 * struct stratix10_svc_controller - service controller
127 * @chans: array of service channels
128 * @num_chans: number of channels in 'chans' array
129 * @num_active_client: number of active service client
130 * @node: list management
131 * @genpool: memory pool pointing to the memory region
132 * @task: pointer to the thread task which handles SMC or HVC call
133 * @svc_fifo: a queue for storing service message data
134 * @complete_status: state for completion
135 * @svc_fifo_lock: protect access to service message data queue
136 * @invoke_fn: function to issue secure monitor call or hypervisor call
138 * This struct is used to create communication channels for service clients, to
139 * handle secure monitor or hypervisor call.
141 struct stratix10_svc_controller
{
143 struct stratix10_svc_chan
*chans
;
145 int num_active_client
;
146 struct list_head node
;
147 struct gen_pool
*genpool
;
148 struct task_struct
*task
;
149 struct kfifo svc_fifo
;
150 struct completion complete_status
;
151 spinlock_t svc_fifo_lock
;
152 svc_invoke_fn
*invoke_fn
;
156 * struct stratix10_svc_chan - service communication channel
157 * @ctrl: pointer to service controller which is the provider of this channel
158 * @scl: pointer to service client which owns the channel
159 * @name: service client name associated with the channel
160 * @lock: protect access to the channel
162 * This struct is used by service client to communicate with service layer, each
163 * service client has its own channel created by service controller.
165 struct stratix10_svc_chan
{
166 struct stratix10_svc_controller
*ctrl
;
167 struct stratix10_svc_client
*scl
;
172 static LIST_HEAD(svc_ctrl
);
173 static LIST_HEAD(svc_data_mem
);
176 * svc_pa_to_va() - translate physical address to virtual address
177 * @addr: to be translated physical address
179 * Return: valid virtual address or NULL if the provided physical
180 * address doesn't exist.
182 static void *svc_pa_to_va(unsigned long addr
)
184 struct stratix10_svc_data_mem
*pmem
;
186 pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr
);
187 list_for_each_entry(pmem
, &svc_data_mem
, node
)
188 if (pmem
->paddr
== addr
)
191 /* physical address is not found */
196 * svc_thread_cmd_data_claim() - claim back buffer from the secure world
197 * @ctrl: pointer to service layer controller
198 * @p_data: pointer to service data structure
199 * @cb_data: pointer to callback data structure to service client
201 * Claim back the submitted buffers from the secure world and pass buffer
202 * back to service client (FPGA manager, etc) for reuse.
204 static void svc_thread_cmd_data_claim(struct stratix10_svc_controller
*ctrl
,
205 struct stratix10_svc_data
*p_data
,
206 struct stratix10_svc_cb_data
*cb_data
)
208 struct arm_smccc_res res
;
209 unsigned long timeout
;
211 reinit_completion(&ctrl
->complete_status
);
212 timeout
= msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS
);
214 pr_debug("%s: claim back the submitted buffer\n", __func__
);
216 ctrl
->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE
,
217 0, 0, 0, 0, 0, 0, 0, &res
);
219 if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
) {
221 complete(&ctrl
->complete_status
);
224 cb_data
->status
= BIT(SVC_STATUS_BUFFER_DONE
);
225 cb_data
->kaddr1
= svc_pa_to_va(res
.a1
);
226 cb_data
->kaddr2
= (res
.a2
) ?
227 svc_pa_to_va(res
.a2
) : NULL
;
228 cb_data
->kaddr3
= (res
.a3
) ?
229 svc_pa_to_va(res
.a3
) : NULL
;
230 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
,
233 pr_debug("%s: secure world busy, polling again\n",
236 } while (res
.a0
== INTEL_SIP_SMC_STATUS_OK
||
237 res
.a0
== INTEL_SIP_SMC_STATUS_BUSY
||
238 wait_for_completion_timeout(&ctrl
->complete_status
, timeout
));
242 * svc_thread_cmd_config_status() - check configuration status
243 * @ctrl: pointer to service layer controller
244 * @p_data: pointer to service data structure
245 * @cb_data: pointer to callback data structure to service client
247 * Check whether the secure firmware at secure world has finished the FPGA
248 * configuration, and then inform FPGA manager the configuration status.
250 static void svc_thread_cmd_config_status(struct stratix10_svc_controller
*ctrl
,
251 struct stratix10_svc_data
*p_data
,
252 struct stratix10_svc_cb_data
*cb_data
)
254 struct arm_smccc_res res
;
256 unsigned long a0
, a1
, a2
;
258 cb_data
->kaddr1
= NULL
;
259 cb_data
->kaddr2
= NULL
;
260 cb_data
->kaddr3
= NULL
;
261 cb_data
->status
= BIT(SVC_STATUS_ERROR
);
263 pr_debug("%s: polling config status\n", __func__
);
265 a0
= INTEL_SIP_SMC_FPGA_CONFIG_ISDONE
;
266 a1
= (unsigned long)p_data
->paddr
;
267 a2
= (unsigned long)p_data
->size
;
269 if (p_data
->command
== COMMAND_POLL_SERVICE_STATUS
)
270 a0
= INTEL_SIP_SMC_SERVICE_COMPLETED
;
272 count_in_sec
= FPGA_CONFIG_STATUS_TIMEOUT_SEC
;
273 while (count_in_sec
) {
274 ctrl
->invoke_fn(a0
, a1
, a2
, 0, 0, 0, 0, 0, &res
);
275 if ((res
.a0
== INTEL_SIP_SMC_STATUS_OK
) ||
276 (res
.a0
== INTEL_SIP_SMC_STATUS_ERROR
) ||
277 (res
.a0
== INTEL_SIP_SMC_STATUS_REJECTED
))
281 * request is still in progress, wait one second then
289 pr_err("%s: poll status timeout\n", __func__
);
290 cb_data
->status
= BIT(SVC_STATUS_BUSY
);
291 } else if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
) {
292 cb_data
->status
= BIT(SVC_STATUS_COMPLETED
);
293 cb_data
->kaddr2
= (res
.a2
) ?
294 svc_pa_to_va(res
.a2
) : NULL
;
295 cb_data
->kaddr3
= (res
.a3
) ? &res
.a3
: NULL
;
297 pr_err("%s: poll status error\n", __func__
);
298 cb_data
->kaddr1
= &res
.a1
;
299 cb_data
->kaddr2
= (res
.a2
) ?
300 svc_pa_to_va(res
.a2
) : NULL
;
301 cb_data
->kaddr3
= (res
.a3
) ? &res
.a3
: NULL
;
302 cb_data
->status
= BIT(SVC_STATUS_ERROR
);
305 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
, cb_data
);
309 * svc_thread_recv_status_ok() - handle the successful status
310 * @p_data: pointer to service data structure
311 * @cb_data: pointer to callback data structure to service client
312 * @res: result from SMC or HVC call
314 * Send back the correspond status to the service clients.
316 static void svc_thread_recv_status_ok(struct stratix10_svc_data
*p_data
,
317 struct stratix10_svc_cb_data
*cb_data
,
318 struct arm_smccc_res res
)
320 cb_data
->kaddr1
= NULL
;
321 cb_data
->kaddr2
= NULL
;
322 cb_data
->kaddr3
= NULL
;
324 switch (p_data
->command
) {
325 case COMMAND_RECONFIG
:
326 case COMMAND_RSU_UPDATE
:
327 case COMMAND_RSU_NOTIFY
:
328 case COMMAND_FCS_REQUEST_SERVICE
:
329 case COMMAND_FCS_SEND_CERTIFICATE
:
330 case COMMAND_FCS_DATA_ENCRYPTION
:
331 case COMMAND_FCS_DATA_DECRYPTION
:
332 cb_data
->status
= BIT(SVC_STATUS_OK
);
334 case COMMAND_RECONFIG_DATA_SUBMIT
:
335 cb_data
->status
= BIT(SVC_STATUS_BUFFER_SUBMITTED
);
337 case COMMAND_RECONFIG_STATUS
:
338 cb_data
->status
= BIT(SVC_STATUS_COMPLETED
);
340 case COMMAND_RSU_RETRY
:
341 case COMMAND_RSU_MAX_RETRY
:
342 case COMMAND_RSU_DCMF_STATUS
:
343 case COMMAND_FIRMWARE_VERSION
:
344 cb_data
->status
= BIT(SVC_STATUS_OK
);
345 cb_data
->kaddr1
= &res
.a1
;
347 case COMMAND_SMC_SVC_VERSION
:
348 cb_data
->status
= BIT(SVC_STATUS_OK
);
349 cb_data
->kaddr1
= &res
.a1
;
350 cb_data
->kaddr2
= &res
.a2
;
352 case COMMAND_RSU_DCMF_VERSION
:
353 cb_data
->status
= BIT(SVC_STATUS_OK
);
354 cb_data
->kaddr1
= &res
.a1
;
355 cb_data
->kaddr2
= &res
.a2
;
357 case COMMAND_FCS_RANDOM_NUMBER_GEN
:
358 case COMMAND_FCS_GET_PROVISION_DATA
:
359 case COMMAND_POLL_SERVICE_STATUS
:
360 cb_data
->status
= BIT(SVC_STATUS_OK
);
361 cb_data
->kaddr1
= &res
.a1
;
362 cb_data
->kaddr2
= svc_pa_to_va(res
.a2
);
363 cb_data
->kaddr3
= &res
.a3
;
365 case COMMAND_MBOX_SEND_CMD
:
366 cb_data
->status
= BIT(SVC_STATUS_OK
);
367 cb_data
->kaddr1
= &res
.a1
;
368 /* SDM return size in u8. Convert size to u32 word */
369 res
.a2
= res
.a2
* BYTE_TO_WORD_SIZE
;
370 cb_data
->kaddr2
= &res
.a2
;
373 pr_warn("it shouldn't happen\n");
377 pr_debug("%s: call receive_cb\n", __func__
);
378 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
, cb_data
);
382 * svc_normal_to_secure_thread() - the function to run in the kthread
383 * @data: data pointer for kthread function
385 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
386 * node 0, its function stratix10_svc_secure_call_thread is used to handle
387 * SMC or HVC calls between kernel driver and secure monitor software.
389 * Return: 0 for success or -ENOMEM on error.
391 static int svc_normal_to_secure_thread(void *data
)
393 struct stratix10_svc_controller
394 *ctrl
= (struct stratix10_svc_controller
*)data
;
395 struct stratix10_svc_data
*pdata
;
396 struct stratix10_svc_cb_data
*cbdata
;
397 struct arm_smccc_res res
;
398 unsigned long a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
;
401 pdata
= kmalloc(sizeof(*pdata
), GFP_KERNEL
);
405 cbdata
= kmalloc(sizeof(*cbdata
), GFP_KERNEL
);
411 /* default set, to remove build warning */
412 a0
= INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK
;
421 pr_debug("smc_hvc_shm_thread is running\n");
423 while (!kthread_should_stop()) {
424 ret_fifo
= kfifo_out_spinlocked(&ctrl
->svc_fifo
,
425 pdata
, sizeof(*pdata
),
426 &ctrl
->svc_fifo_lock
);
431 pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
432 (unsigned int)pdata
->paddr
, pdata
->command
,
433 (unsigned int)pdata
->size
);
435 switch (pdata
->command
) {
436 case COMMAND_RECONFIG_DATA_CLAIM
:
437 svc_thread_cmd_data_claim(ctrl
, pdata
, cbdata
);
439 case COMMAND_RECONFIG
:
440 a0
= INTEL_SIP_SMC_FPGA_CONFIG_START
;
441 pr_debug("conf_type=%u\n", (unsigned int)pdata
->flag
);
445 case COMMAND_RECONFIG_DATA_SUBMIT
:
446 a0
= INTEL_SIP_SMC_FPGA_CONFIG_WRITE
;
447 a1
= (unsigned long)pdata
->paddr
;
448 a2
= (unsigned long)pdata
->size
;
450 case COMMAND_RECONFIG_STATUS
:
451 a0
= INTEL_SIP_SMC_FPGA_CONFIG_ISDONE
;
455 case COMMAND_RSU_STATUS
:
456 a0
= INTEL_SIP_SMC_RSU_STATUS
;
460 case COMMAND_RSU_UPDATE
:
461 a0
= INTEL_SIP_SMC_RSU_UPDATE
;
465 case COMMAND_RSU_NOTIFY
:
466 a0
= INTEL_SIP_SMC_RSU_NOTIFY
;
470 case COMMAND_RSU_RETRY
:
471 a0
= INTEL_SIP_SMC_RSU_RETRY_COUNTER
;
475 case COMMAND_RSU_MAX_RETRY
:
476 a0
= INTEL_SIP_SMC_RSU_MAX_RETRY
;
480 case COMMAND_RSU_DCMF_VERSION
:
481 a0
= INTEL_SIP_SMC_RSU_DCMF_VERSION
;
485 case COMMAND_FIRMWARE_VERSION
:
486 a0
= INTEL_SIP_SMC_FIRMWARE_VERSION
;
492 case COMMAND_FCS_DATA_ENCRYPTION
:
493 a0
= INTEL_SIP_SMC_FCS_CRYPTION
;
495 a2
= (unsigned long)pdata
->paddr
;
496 a3
= (unsigned long)pdata
->size
;
497 a4
= (unsigned long)pdata
->paddr_output
;
498 a5
= (unsigned long)pdata
->size_output
;
500 case COMMAND_FCS_DATA_DECRYPTION
:
501 a0
= INTEL_SIP_SMC_FCS_CRYPTION
;
503 a2
= (unsigned long)pdata
->paddr
;
504 a3
= (unsigned long)pdata
->size
;
505 a4
= (unsigned long)pdata
->paddr_output
;
506 a5
= (unsigned long)pdata
->size_output
;
508 case COMMAND_FCS_RANDOM_NUMBER_GEN
:
509 a0
= INTEL_SIP_SMC_FCS_RANDOM_NUMBER
;
510 a1
= (unsigned long)pdata
->paddr
;
513 case COMMAND_FCS_REQUEST_SERVICE
:
514 a0
= INTEL_SIP_SMC_FCS_SERVICE_REQUEST
;
515 a1
= (unsigned long)pdata
->paddr
;
516 a2
= (unsigned long)pdata
->size
;
518 case COMMAND_FCS_SEND_CERTIFICATE
:
519 a0
= INTEL_SIP_SMC_FCS_SEND_CERTIFICATE
;
520 a1
= (unsigned long)pdata
->paddr
;
521 a2
= (unsigned long)pdata
->size
;
523 case COMMAND_FCS_GET_PROVISION_DATA
:
524 a0
= INTEL_SIP_SMC_FCS_GET_PROVISION_DATA
;
525 a1
= (unsigned long)pdata
->paddr
;
530 case COMMAND_POLL_SERVICE_STATUS
:
531 a0
= INTEL_SIP_SMC_SERVICE_COMPLETED
;
532 a1
= (unsigned long)pdata
->paddr
;
533 a2
= (unsigned long)pdata
->size
;
535 case COMMAND_RSU_DCMF_STATUS
:
536 a0
= INTEL_SIP_SMC_RSU_DCMF_STATUS
;
540 case COMMAND_SMC_SVC_VERSION
:
541 a0
= INTEL_SIP_SMC_SVC_VERSION
;
545 case COMMAND_MBOX_SEND_CMD
:
546 a0
= INTEL_SIP_SMC_MBOX_SEND_CMD
;
548 a2
= (unsigned long)pdata
->paddr
;
549 a3
= (unsigned long)pdata
->size
/ BYTE_TO_WORD_SIZE
;
551 a5
= (unsigned long)pdata
->paddr_output
;
552 a6
= (unsigned long)pdata
->size_output
/ BYTE_TO_WORD_SIZE
;
555 pr_warn("it shouldn't happen\n");
558 pr_debug("%s: before SMC call -- a0=0x%016x a1=0x%016x",
562 pr_debug(" a2=0x%016x\n", (unsigned int)a2
);
563 pr_debug(" a3=0x%016x\n", (unsigned int)a3
);
564 pr_debug(" a4=0x%016x\n", (unsigned int)a4
);
565 pr_debug(" a5=0x%016x\n", (unsigned int)a5
);
566 ctrl
->invoke_fn(a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, &res
);
568 pr_debug("%s: after SMC call -- res.a0=0x%016x",
569 __func__
, (unsigned int)res
.a0
);
570 pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
571 (unsigned int)res
.a1
, (unsigned int)res
.a2
);
572 pr_debug(" res.a3=0x%016x\n", (unsigned int)res
.a3
);
574 if (pdata
->command
== COMMAND_RSU_STATUS
) {
575 if (res
.a0
== INTEL_SIP_SMC_RSU_ERROR
)
576 cbdata
->status
= BIT(SVC_STATUS_ERROR
);
578 cbdata
->status
= BIT(SVC_STATUS_OK
);
580 cbdata
->kaddr1
= &res
;
581 cbdata
->kaddr2
= NULL
;
582 cbdata
->kaddr3
= NULL
;
583 pdata
->chan
->scl
->receive_cb(pdata
->chan
->scl
, cbdata
);
588 case INTEL_SIP_SMC_STATUS_OK
:
589 svc_thread_recv_status_ok(pdata
, cbdata
, res
);
591 case INTEL_SIP_SMC_STATUS_BUSY
:
592 switch (pdata
->command
) {
593 case COMMAND_RECONFIG_DATA_SUBMIT
:
594 svc_thread_cmd_data_claim(ctrl
,
597 case COMMAND_RECONFIG_STATUS
:
598 case COMMAND_POLL_SERVICE_STATUS
:
599 svc_thread_cmd_config_status(ctrl
,
603 pr_warn("it shouldn't happen\n");
607 case INTEL_SIP_SMC_STATUS_REJECTED
:
608 pr_debug("%s: STATUS_REJECTED\n", __func__
);
610 switch (pdata
->command
) {
611 case COMMAND_FCS_REQUEST_SERVICE
:
612 case COMMAND_FCS_SEND_CERTIFICATE
:
613 case COMMAND_FCS_GET_PROVISION_DATA
:
614 case COMMAND_FCS_DATA_ENCRYPTION
:
615 case COMMAND_FCS_DATA_DECRYPTION
:
616 case COMMAND_FCS_RANDOM_NUMBER_GEN
:
617 case COMMAND_MBOX_SEND_CMD
:
618 cbdata
->status
= BIT(SVC_STATUS_INVALID_PARAM
);
619 cbdata
->kaddr1
= NULL
;
620 cbdata
->kaddr2
= NULL
;
621 cbdata
->kaddr3
= NULL
;
622 pdata
->chan
->scl
->receive_cb(pdata
->chan
->scl
,
627 case INTEL_SIP_SMC_STATUS_ERROR
:
628 case INTEL_SIP_SMC_RSU_ERROR
:
629 pr_err("%s: STATUS_ERROR\n", __func__
);
630 cbdata
->status
= BIT(SVC_STATUS_ERROR
);
631 cbdata
->kaddr1
= &res
.a1
;
632 cbdata
->kaddr2
= (res
.a2
) ?
633 svc_pa_to_va(res
.a2
) : NULL
;
634 cbdata
->kaddr3
= (res
.a3
) ? &res
.a3
: NULL
;
635 pdata
->chan
->scl
->receive_cb(pdata
->chan
->scl
, cbdata
);
638 pr_warn("Secure firmware doesn't support...\n");
641 * be compatible with older version firmware which
642 * doesn't support newer RSU commands
644 if ((pdata
->command
!= COMMAND_RSU_UPDATE
) &&
645 (pdata
->command
!= COMMAND_RSU_STATUS
)) {
647 BIT(SVC_STATUS_NO_SUPPORT
);
648 cbdata
->kaddr1
= NULL
;
649 cbdata
->kaddr2
= NULL
;
650 cbdata
->kaddr3
= NULL
;
651 pdata
->chan
->scl
->receive_cb(
652 pdata
->chan
->scl
, cbdata
);
666 * svc_normal_to_secure_shm_thread() - the function to run in the kthread
667 * @data: data pointer for kthread function
669 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
670 * node 0, its function stratix10_svc_secure_shm_thread is used to query the
671 * physical address of memory block reserved by secure monitor software at
674 * svc_normal_to_secure_shm_thread() terminates directly since it is a
675 * standlone thread for which no one will call kthread_stop() or return when
676 * 'kthread_should_stop()' is true.
678 static int svc_normal_to_secure_shm_thread(void *data
)
680 struct stratix10_svc_sh_memory
681 *sh_mem
= (struct stratix10_svc_sh_memory
*)data
;
682 struct arm_smccc_res res
;
684 /* SMC or HVC call to get shared memory info from secure world */
685 sh_mem
->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM
,
686 0, 0, 0, 0, 0, 0, 0, &res
);
687 if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
) {
688 sh_mem
->addr
= res
.a1
;
689 sh_mem
->size
= res
.a2
;
691 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__
,
692 (unsigned int)res
.a0
);
697 complete(&sh_mem
->sync_complete
);
702 * svc_get_sh_memory() - get memory block reserved by secure monitor SW
703 * @pdev: pointer to service layer device
704 * @sh_memory: pointer to service shared memory structure
706 * Return: zero for successfully getting the physical address of memory block
707 * reserved by secure monitor software, or negative value on error.
709 static int svc_get_sh_memory(struct platform_device
*pdev
,
710 struct stratix10_svc_sh_memory
*sh_memory
)
712 struct device
*dev
= &pdev
->dev
;
713 struct task_struct
*sh_memory_task
;
714 unsigned int cpu
= 0;
716 init_completion(&sh_memory
->sync_complete
);
718 /* smc or hvc call happens on cpu 0 bound kthread */
719 sh_memory_task
= kthread_create_on_node(svc_normal_to_secure_shm_thread
,
722 "svc_smc_hvc_shm_thread");
723 if (IS_ERR(sh_memory_task
)) {
724 dev_err(dev
, "fail to create stratix10_svc_smc_shm_thread\n");
728 wake_up_process(sh_memory_task
);
730 if (!wait_for_completion_timeout(&sh_memory
->sync_complete
, 10 * HZ
)) {
732 "timeout to get sh-memory paras from secure world\n");
736 if (!sh_memory
->addr
|| !sh_memory
->size
) {
738 "failed to get shared memory info from secure world\n");
742 dev_dbg(dev
, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
743 (unsigned int)sh_memory
->addr
,
744 (unsigned int)sh_memory
->size
);
750 * svc_create_memory_pool() - create a memory pool from reserved memory block
751 * @pdev: pointer to service layer device
752 * @sh_memory: pointer to service shared memory structure
754 * Return: pool allocated from reserved memory block or ERR_PTR() on error.
756 static struct gen_pool
*
757 svc_create_memory_pool(struct platform_device
*pdev
,
758 struct stratix10_svc_sh_memory
*sh_memory
)
760 struct device
*dev
= &pdev
->dev
;
761 struct gen_pool
*genpool
;
768 size_t page_mask
= PAGE_SIZE
- 1;
769 int min_alloc_order
= 3;
772 begin
= roundup(sh_memory
->addr
, PAGE_SIZE
);
773 end
= rounddown(sh_memory
->addr
+ sh_memory
->size
, PAGE_SIZE
);
776 va
= devm_memremap(dev
, paddr
, size
, MEMREMAP_WC
);
778 dev_err(dev
, "fail to remap shared memory\n");
779 return ERR_PTR(-EINVAL
);
781 vaddr
= (unsigned long)va
;
783 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
784 va
, (unsigned int)paddr
, (unsigned int)size
);
785 if ((vaddr
& page_mask
) || (paddr
& page_mask
) ||
786 (size
& page_mask
)) {
787 dev_err(dev
, "page is not aligned\n");
788 return ERR_PTR(-EINVAL
);
790 genpool
= gen_pool_create(min_alloc_order
, -1);
792 dev_err(dev
, "fail to create genpool\n");
793 return ERR_PTR(-ENOMEM
);
795 gen_pool_set_algo(genpool
, gen_pool_best_fit
, NULL
);
796 ret
= gen_pool_add_virt(genpool
, vaddr
, paddr
, size
, -1);
798 dev_err(dev
, "fail to add memory chunk to the pool\n");
799 gen_pool_destroy(genpool
);
807 * svc_smccc_smc() - secure monitor call between normal and secure world
808 * @a0: argument passed in registers 0
809 * @a1: argument passed in registers 1
810 * @a2: argument passed in registers 2
811 * @a3: argument passed in registers 3
812 * @a4: argument passed in registers 4
813 * @a5: argument passed in registers 5
814 * @a6: argument passed in registers 6
815 * @a7: argument passed in registers 7
816 * @res: result values from register 0 to 3
818 static void svc_smccc_smc(unsigned long a0
, unsigned long a1
,
819 unsigned long a2
, unsigned long a3
,
820 unsigned long a4
, unsigned long a5
,
821 unsigned long a6
, unsigned long a7
,
822 struct arm_smccc_res
*res
)
824 arm_smccc_smc(a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, res
);
828 * svc_smccc_hvc() - hypervisor call between normal and secure world
829 * @a0: argument passed in registers 0
830 * @a1: argument passed in registers 1
831 * @a2: argument passed in registers 2
832 * @a3: argument passed in registers 3
833 * @a4: argument passed in registers 4
834 * @a5: argument passed in registers 5
835 * @a6: argument passed in registers 6
836 * @a7: argument passed in registers 7
837 * @res: result values from register 0 to 3
839 static void svc_smccc_hvc(unsigned long a0
, unsigned long a1
,
840 unsigned long a2
, unsigned long a3
,
841 unsigned long a4
, unsigned long a5
,
842 unsigned long a6
, unsigned long a7
,
843 struct arm_smccc_res
*res
)
845 arm_smccc_hvc(a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, res
);
849 * get_invoke_func() - invoke SMC or HVC call
850 * @dev: pointer to device
852 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
854 static svc_invoke_fn
*get_invoke_func(struct device
*dev
)
858 if (of_property_read_string(dev
->of_node
, "method", &method
)) {
859 dev_warn(dev
, "missing \"method\" property\n");
860 return ERR_PTR(-ENXIO
);
863 if (!strcmp(method
, "smc"))
864 return svc_smccc_smc
;
865 if (!strcmp(method
, "hvc"))
866 return svc_smccc_hvc
;
868 dev_warn(dev
, "invalid \"method\" property: %s\n", method
);
870 return ERR_PTR(-EINVAL
);
874 * stratix10_svc_request_channel_byname() - request a service channel
875 * @client: pointer to service client
876 * @name: service client name
878 * This function is used by service client to request a service channel.
880 * Return: a pointer to channel assigned to the client on success,
881 * or ERR_PTR() on error.
883 struct stratix10_svc_chan
*stratix10_svc_request_channel_byname(
884 struct stratix10_svc_client
*client
, const char *name
)
886 struct device
*dev
= client
->dev
;
887 struct stratix10_svc_controller
*controller
;
888 struct stratix10_svc_chan
*chan
= NULL
;
892 /* if probe was called after client's, or error on probe */
893 if (list_empty(&svc_ctrl
))
894 return ERR_PTR(-EPROBE_DEFER
);
896 controller
= list_first_entry(&svc_ctrl
,
897 struct stratix10_svc_controller
, node
);
898 for (i
= 0; i
< SVC_NUM_CHANNEL
; i
++) {
899 if (!strcmp(controller
->chans
[i
].name
, name
)) {
900 chan
= &controller
->chans
[i
];
905 /* if there was no channel match */
906 if (i
== SVC_NUM_CHANNEL
) {
907 dev_err(dev
, "%s: channel not allocated\n", __func__
);
908 return ERR_PTR(-EINVAL
);
911 if (chan
->scl
|| !try_module_get(controller
->dev
->driver
->owner
)) {
912 dev_dbg(dev
, "%s: svc not free\n", __func__
);
913 return ERR_PTR(-EBUSY
);
916 spin_lock_irqsave(&chan
->lock
, flag
);
918 chan
->ctrl
->num_active_client
++;
919 spin_unlock_irqrestore(&chan
->lock
, flag
);
923 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname
);
926 * stratix10_svc_free_channel() - free service channel
927 * @chan: service channel to be freed
929 * This function is used by service client to free a service channel.
931 void stratix10_svc_free_channel(struct stratix10_svc_chan
*chan
)
935 spin_lock_irqsave(&chan
->lock
, flag
);
937 chan
->ctrl
->num_active_client
--;
938 module_put(chan
->ctrl
->dev
->driver
->owner
);
939 spin_unlock_irqrestore(&chan
->lock
, flag
);
941 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel
);
944 * stratix10_svc_send() - send a message data to the remote
945 * @chan: service channel assigned to the client
946 * @msg: message data to be sent, in the format of
947 * "struct stratix10_svc_client_msg"
949 * This function is used by service client to add a message to the service
950 * layer driver's queue for being sent to the secure world.
952 * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
954 int stratix10_svc_send(struct stratix10_svc_chan
*chan
, void *msg
)
956 struct stratix10_svc_client_msg
957 *p_msg
= (struct stratix10_svc_client_msg
*)msg
;
958 struct stratix10_svc_data_mem
*p_mem
;
959 struct stratix10_svc_data
*p_data
;
961 unsigned int cpu
= 0;
963 p_data
= kzalloc(sizeof(*p_data
), GFP_KERNEL
);
967 /* first client will create kernel thread */
968 if (!chan
->ctrl
->task
) {
970 kthread_create_on_node(svc_normal_to_secure_thread
,
973 "svc_smc_hvc_thread");
974 if (IS_ERR(chan
->ctrl
->task
)) {
975 dev_err(chan
->ctrl
->dev
,
976 "failed to create svc_smc_hvc_thread\n");
980 kthread_bind(chan
->ctrl
->task
, cpu
);
981 wake_up_process(chan
->ctrl
->task
);
984 pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__
,
985 p_msg
->payload
, p_msg
->command
,
986 (unsigned int)p_msg
->payload_length
);
988 if (list_empty(&svc_data_mem
)) {
989 if (p_msg
->command
== COMMAND_RECONFIG
) {
990 struct stratix10_svc_command_config_type
*ct
=
991 (struct stratix10_svc_command_config_type
*)
993 p_data
->flag
= ct
->flags
;
996 list_for_each_entry(p_mem
, &svc_data_mem
, node
)
997 if (p_mem
->vaddr
== p_msg
->payload
) {
998 p_data
->paddr
= p_mem
->paddr
;
999 p_data
->size
= p_msg
->payload_length
;
1002 if (p_msg
->payload_output
) {
1003 list_for_each_entry(p_mem
, &svc_data_mem
, node
)
1004 if (p_mem
->vaddr
== p_msg
->payload_output
) {
1005 p_data
->paddr_output
=
1007 p_data
->size_output
=
1008 p_msg
->payload_length_output
;
1014 p_data
->command
= p_msg
->command
;
1015 p_data
->arg
[0] = p_msg
->arg
[0];
1016 p_data
->arg
[1] = p_msg
->arg
[1];
1017 p_data
->arg
[2] = p_msg
->arg
[2];
1018 p_data
->size
= p_msg
->payload_length
;
1019 p_data
->chan
= chan
;
1020 pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__
,
1021 (unsigned int)p_data
->paddr
, p_data
->command
,
1022 (unsigned int)p_data
->size
);
1023 ret
= kfifo_in_spinlocked(&chan
->ctrl
->svc_fifo
, p_data
,
1025 &chan
->ctrl
->svc_fifo_lock
);
1034 EXPORT_SYMBOL_GPL(stratix10_svc_send
);
1037 * stratix10_svc_done() - complete service request transactions
1038 * @chan: service channel assigned to the client
1040 * This function should be called when client has finished its request
1041 * or there is an error in the request process. It allows the service layer
1042 * to stop the running thread to have maximize savings in kernel resources.
1044 void stratix10_svc_done(struct stratix10_svc_chan
*chan
)
1046 /* stop thread when thread is running AND only one active client */
1047 if (chan
->ctrl
->task
&& chan
->ctrl
->num_active_client
<= 1) {
1048 pr_debug("svc_smc_hvc_shm_thread is stopped\n");
1049 kthread_stop(chan
->ctrl
->task
);
1050 chan
->ctrl
->task
= NULL
;
1053 EXPORT_SYMBOL_GPL(stratix10_svc_done
);
1056 * stratix10_svc_allocate_memory() - allocate memory
1057 * @chan: service channel assigned to the client
1058 * @size: memory size requested by a specific service client
1060 * Service layer allocates the requested number of bytes buffer from the
1061 * memory pool, service client uses this function to get allocated buffers.
1063 * Return: address of allocated memory on success, or ERR_PTR() on error.
1065 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan
*chan
,
1068 struct stratix10_svc_data_mem
*pmem
;
1071 struct gen_pool
*genpool
= chan
->ctrl
->genpool
;
1072 size_t s
= roundup(size
, 1 << genpool
->min_alloc_order
);
1074 pmem
= devm_kzalloc(chan
->ctrl
->dev
, sizeof(*pmem
), GFP_KERNEL
);
1076 return ERR_PTR(-ENOMEM
);
1078 va
= gen_pool_alloc(genpool
, s
);
1080 return ERR_PTR(-ENOMEM
);
1082 memset((void *)va
, 0, s
);
1083 pa
= gen_pool_virt_to_phys(genpool
, va
);
1085 pmem
->vaddr
= (void *)va
;
1088 list_add_tail(&pmem
->node
, &svc_data_mem
);
1089 pr_debug("%s: va=%p, pa=0x%016x\n", __func__
,
1090 pmem
->vaddr
, (unsigned int)pmem
->paddr
);
1094 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory
);
1097 * stratix10_svc_free_memory() - free allocated memory
1098 * @chan: service channel assigned to the client
1099 * @kaddr: memory to be freed
1101 * This function is used by service client to free allocated buffers.
1103 void stratix10_svc_free_memory(struct stratix10_svc_chan
*chan
, void *kaddr
)
1105 struct stratix10_svc_data_mem
*pmem
;
1107 list_for_each_entry(pmem
, &svc_data_mem
, node
)
1108 if (pmem
->vaddr
== kaddr
) {
1109 gen_pool_free(chan
->ctrl
->genpool
,
1110 (unsigned long)kaddr
, pmem
->size
);
1112 list_del(&pmem
->node
);
1116 list_del(&svc_data_mem
);
1118 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory
);
1120 static const struct of_device_id stratix10_svc_drv_match
[] = {
1121 {.compatible
= "intel,stratix10-svc"},
1122 {.compatible
= "intel,agilex-svc"},
1126 static int stratix10_svc_drv_probe(struct platform_device
*pdev
)
1128 struct device
*dev
= &pdev
->dev
;
1129 struct stratix10_svc_controller
*controller
;
1130 struct stratix10_svc_chan
*chans
;
1131 struct gen_pool
*genpool
;
1132 struct stratix10_svc_sh_memory
*sh_memory
;
1133 struct stratix10_svc
*svc
;
1135 svc_invoke_fn
*invoke_fn
;
1139 /* get SMC or HVC function */
1140 invoke_fn
= get_invoke_func(dev
);
1141 if (IS_ERR(invoke_fn
))
1144 sh_memory
= devm_kzalloc(dev
, sizeof(*sh_memory
), GFP_KERNEL
);
1148 sh_memory
->invoke_fn
= invoke_fn
;
1149 ret
= svc_get_sh_memory(pdev
, sh_memory
);
1153 genpool
= svc_create_memory_pool(pdev
, sh_memory
);
1154 if (IS_ERR(genpool
))
1155 return PTR_ERR(genpool
);
1157 /* allocate service controller and supporting channel */
1158 controller
= devm_kzalloc(dev
, sizeof(*controller
), GFP_KERNEL
);
1161 goto err_destroy_pool
;
1164 chans
= devm_kmalloc_array(dev
, SVC_NUM_CHANNEL
,
1165 sizeof(*chans
), GFP_KERNEL
| __GFP_ZERO
);
1168 goto err_destroy_pool
;
1171 controller
->dev
= dev
;
1172 controller
->num_chans
= SVC_NUM_CHANNEL
;
1173 controller
->num_active_client
= 0;
1174 controller
->chans
= chans
;
1175 controller
->genpool
= genpool
;
1176 controller
->task
= NULL
;
1177 controller
->invoke_fn
= invoke_fn
;
1178 init_completion(&controller
->complete_status
);
1180 fifo_size
= sizeof(struct stratix10_svc_data
) * SVC_NUM_DATA_IN_FIFO
;
1181 ret
= kfifo_alloc(&controller
->svc_fifo
, fifo_size
, GFP_KERNEL
);
1183 dev_err(dev
, "failed to allocate FIFO\n");
1184 goto err_destroy_pool
;
1186 spin_lock_init(&controller
->svc_fifo_lock
);
1188 chans
[0].scl
= NULL
;
1189 chans
[0].ctrl
= controller
;
1190 chans
[0].name
= SVC_CLIENT_FPGA
;
1191 spin_lock_init(&chans
[0].lock
);
1193 chans
[1].scl
= NULL
;
1194 chans
[1].ctrl
= controller
;
1195 chans
[1].name
= SVC_CLIENT_RSU
;
1196 spin_lock_init(&chans
[1].lock
);
1198 chans
[2].scl
= NULL
;
1199 chans
[2].ctrl
= controller
;
1200 chans
[2].name
= SVC_CLIENT_FCS
;
1201 spin_lock_init(&chans
[2].lock
);
1203 list_add_tail(&controller
->node
, &svc_ctrl
);
1204 platform_set_drvdata(pdev
, controller
);
1206 /* add svc client device(s) */
1207 svc
= devm_kzalloc(dev
, sizeof(*svc
), GFP_KERNEL
);
1210 goto err_free_kfifo
;
1213 svc
->stratix10_svc_rsu
= platform_device_alloc(STRATIX10_RSU
, 0);
1214 if (!svc
->stratix10_svc_rsu
) {
1215 dev_err(dev
, "failed to allocate %s device\n", STRATIX10_RSU
);
1217 goto err_free_kfifo
;
1220 ret
= platform_device_add(svc
->stratix10_svc_rsu
);
1222 platform_device_put(svc
->stratix10_svc_rsu
);
1223 goto err_free_kfifo
;
1226 svc
->intel_svc_fcs
= platform_device_alloc(INTEL_FCS
, 1);
1227 if (!svc
->intel_svc_fcs
) {
1228 dev_err(dev
, "failed to allocate %s device\n", INTEL_FCS
);
1230 goto err_unregister_dev
;
1233 ret
= platform_device_add(svc
->intel_svc_fcs
);
1235 platform_device_put(svc
->intel_svc_fcs
);
1236 goto err_unregister_dev
;
1239 dev_set_drvdata(dev
, svc
);
1241 pr_info("Intel Service Layer Driver Initialized\n");
1246 platform_device_unregister(svc
->stratix10_svc_rsu
);
1248 kfifo_free(&controller
->svc_fifo
);
1250 gen_pool_destroy(genpool
);
1254 static void stratix10_svc_drv_remove(struct platform_device
*pdev
)
1256 struct stratix10_svc
*svc
= dev_get_drvdata(&pdev
->dev
);
1257 struct stratix10_svc_controller
*ctrl
= platform_get_drvdata(pdev
);
1259 platform_device_unregister(svc
->intel_svc_fcs
);
1260 platform_device_unregister(svc
->stratix10_svc_rsu
);
1262 kfifo_free(&ctrl
->svc_fifo
);
1264 kthread_stop(ctrl
->task
);
1268 gen_pool_destroy(ctrl
->genpool
);
1269 list_del(&ctrl
->node
);
1272 static struct platform_driver stratix10_svc_driver
= {
1273 .probe
= stratix10_svc_drv_probe
,
1274 .remove_new
= stratix10_svc_drv_remove
,
1276 .name
= "stratix10-svc",
1277 .of_match_table
= stratix10_svc_drv_match
,
1281 static int __init
stratix10_svc_init(void)
1283 struct device_node
*fw_np
;
1284 struct device_node
*np
;
1287 fw_np
= of_find_node_by_name(NULL
, "firmware");
1291 np
= of_find_matching_node(fw_np
, stratix10_svc_drv_match
);
1296 ret
= of_platform_populate(fw_np
, stratix10_svc_drv_match
, NULL
, NULL
);
1300 return platform_driver_register(&stratix10_svc_driver
);
1303 static void __exit
stratix10_svc_exit(void)
1305 return platform_driver_unregister(&stratix10_svc_driver
);
1308 subsys_initcall(stratix10_svc_init
);
1309 module_exit(stratix10_svc_exit
);
1311 MODULE_LICENSE("GPL v2");
1312 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
1313 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
1314 MODULE_ALIAS("platform:stratix10-svc");