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 2
38 #define FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS 200
39 #define FPGA_CONFIG_STATUS_TIMEOUT_SEC 30
41 /* stratix10 service layer clients */
42 #define STRATIX10_RSU "stratix10-rsu"
44 typedef void (svc_invoke_fn
)(unsigned long, unsigned long, unsigned long,
45 unsigned long, unsigned long, unsigned long,
46 unsigned long, unsigned long,
47 struct arm_smccc_res
*);
48 struct stratix10_svc_chan
;
51 * struct stratix10_svc - svc private data
52 * @stratix10_svc_rsu: pointer to stratix10 RSU device
54 struct stratix10_svc
{
55 struct platform_device
*stratix10_svc_rsu
;
59 * struct stratix10_svc_sh_memory - service shared memory structure
60 * @sync_complete: state for a completion
61 * @addr: physical address of shared memory block
62 * @size: size of shared memory block
63 * @invoke_fn: function to issue secure monitor or hypervisor call
65 * This struct is used to save physical address and size of shared memory
66 * block. The shared memory blocked is allocated by secure monitor software
69 * Service layer driver uses the physical address and size to create a memory
70 * pool, then allocates data buffer from that memory pool for service client.
72 struct stratix10_svc_sh_memory
{
73 struct completion sync_complete
;
76 svc_invoke_fn
*invoke_fn
;
80 * struct stratix10_svc_data_mem - service memory structure
81 * @vaddr: virtual address
82 * @paddr: physical address
83 * @size: size of memory
84 * @node: link list head node
86 * This struct is used in a list that keeps track of buffers which have
87 * been allocated or freed from the memory pool. Service layer driver also
88 * uses this struct to transfer physical address to virtual address.
90 struct stratix10_svc_data_mem
{
94 struct list_head node
;
98 * struct stratix10_svc_data - service data structure
99 * @chan: service channel
100 * @paddr: playload physical address
101 * @size: playload size
102 * @command: service command requested by client
103 * @flag: configuration type (full or partial)
104 * @arg: args to be passed via registers and not physically mapped buffers
106 * This struct is used in service FIFO for inter-process communication.
108 struct stratix10_svc_data
{
109 struct stratix10_svc_chan
*chan
;
118 * struct stratix10_svc_controller - service controller
120 * @chans: array of service channels
121 * @num_chans: number of channels in 'chans' array
122 * @num_active_client: number of active service client
123 * @node: list management
124 * @genpool: memory pool pointing to the memory region
125 * @task: pointer to the thread task which handles SMC or HVC call
126 * @svc_fifo: a queue for storing service message data
127 * @complete_status: state for completion
128 * @svc_fifo_lock: protect access to service message data queue
129 * @invoke_fn: function to issue secure monitor call or hypervisor call
131 * This struct is used to create communication channels for service clients, to
132 * handle secure monitor or hypervisor call.
134 struct stratix10_svc_controller
{
136 struct stratix10_svc_chan
*chans
;
138 int num_active_client
;
139 struct list_head node
;
140 struct gen_pool
*genpool
;
141 struct task_struct
*task
;
142 struct kfifo svc_fifo
;
143 struct completion complete_status
;
144 spinlock_t svc_fifo_lock
;
145 svc_invoke_fn
*invoke_fn
;
149 * struct stratix10_svc_chan - service communication channel
150 * @ctrl: pointer to service controller which is the provider of this channel
151 * @scl: pointer to service client which owns the channel
152 * @name: service client name associated with the channel
153 * @lock: protect access to the channel
155 * This struct is used by service client to communicate with service layer, each
156 * service client has its own channel created by service controller.
158 struct stratix10_svc_chan
{
159 struct stratix10_svc_controller
*ctrl
;
160 struct stratix10_svc_client
*scl
;
165 static LIST_HEAD(svc_ctrl
);
166 static LIST_HEAD(svc_data_mem
);
169 * svc_pa_to_va() - translate physical address to virtual address
170 * @addr: to be translated physical address
172 * Return: valid virtual address or NULL if the provided physical
173 * address doesn't exist.
175 static void *svc_pa_to_va(unsigned long addr
)
177 struct stratix10_svc_data_mem
*pmem
;
179 pr_debug("claim back P-addr=0x%016x\n", (unsigned int)addr
);
180 list_for_each_entry(pmem
, &svc_data_mem
, node
)
181 if (pmem
->paddr
== addr
)
184 /* physical address is not found */
189 * svc_thread_cmd_data_claim() - claim back buffer from the secure world
190 * @ctrl: pointer to service layer controller
191 * @p_data: pointer to service data structure
192 * @cb_data: pointer to callback data structure to service client
194 * Claim back the submitted buffers from the secure world and pass buffer
195 * back to service client (FPGA manager, etc) for reuse.
197 static void svc_thread_cmd_data_claim(struct stratix10_svc_controller
*ctrl
,
198 struct stratix10_svc_data
*p_data
,
199 struct stratix10_svc_cb_data
*cb_data
)
201 struct arm_smccc_res res
;
202 unsigned long timeout
;
204 reinit_completion(&ctrl
->complete_status
);
205 timeout
= msecs_to_jiffies(FPGA_CONFIG_DATA_CLAIM_TIMEOUT_MS
);
207 pr_debug("%s: claim back the submitted buffer\n", __func__
);
209 ctrl
->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_COMPLETED_WRITE
,
210 0, 0, 0, 0, 0, 0, 0, &res
);
212 if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
) {
214 complete(&ctrl
->complete_status
);
217 cb_data
->status
= BIT(SVC_STATUS_BUFFER_DONE
);
218 cb_data
->kaddr1
= svc_pa_to_va(res
.a1
);
219 cb_data
->kaddr2
= (res
.a2
) ?
220 svc_pa_to_va(res
.a2
) : NULL
;
221 cb_data
->kaddr3
= (res
.a3
) ?
222 svc_pa_to_va(res
.a3
) : NULL
;
223 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
,
226 pr_debug("%s: secure world busy, polling again\n",
229 } while (res
.a0
== INTEL_SIP_SMC_STATUS_OK
||
230 res
.a0
== INTEL_SIP_SMC_STATUS_BUSY
||
231 wait_for_completion_timeout(&ctrl
->complete_status
, timeout
));
235 * svc_thread_cmd_config_status() - check configuration status
236 * @ctrl: pointer to service layer controller
237 * @p_data: pointer to service data structure
238 * @cb_data: pointer to callback data structure to service client
240 * Check whether the secure firmware at secure world has finished the FPGA
241 * configuration, and then inform FPGA manager the configuration status.
243 static void svc_thread_cmd_config_status(struct stratix10_svc_controller
*ctrl
,
244 struct stratix10_svc_data
*p_data
,
245 struct stratix10_svc_cb_data
*cb_data
)
247 struct arm_smccc_res res
;
250 cb_data
->kaddr1
= NULL
;
251 cb_data
->kaddr2
= NULL
;
252 cb_data
->kaddr3
= NULL
;
253 cb_data
->status
= BIT(SVC_STATUS_ERROR
);
255 pr_debug("%s: polling config status\n", __func__
);
257 count_in_sec
= FPGA_CONFIG_STATUS_TIMEOUT_SEC
;
258 while (count_in_sec
) {
259 ctrl
->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_ISDONE
,
260 0, 0, 0, 0, 0, 0, 0, &res
);
261 if ((res
.a0
== INTEL_SIP_SMC_STATUS_OK
) ||
262 (res
.a0
== INTEL_SIP_SMC_STATUS_ERROR
))
266 * configuration is still in progress, wait one second then
273 if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
&& count_in_sec
)
274 cb_data
->status
= BIT(SVC_STATUS_COMPLETED
);
276 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
, cb_data
);
280 * svc_thread_recv_status_ok() - handle the successful status
281 * @p_data: pointer to service data structure
282 * @cb_data: pointer to callback data structure to service client
283 * @res: result from SMC or HVC call
285 * Send back the correspond status to the service clients.
287 static void svc_thread_recv_status_ok(struct stratix10_svc_data
*p_data
,
288 struct stratix10_svc_cb_data
*cb_data
,
289 struct arm_smccc_res res
)
291 cb_data
->kaddr1
= NULL
;
292 cb_data
->kaddr2
= NULL
;
293 cb_data
->kaddr3
= NULL
;
295 switch (p_data
->command
) {
296 case COMMAND_RECONFIG
:
297 case COMMAND_RSU_UPDATE
:
298 case COMMAND_RSU_NOTIFY
:
299 cb_data
->status
= BIT(SVC_STATUS_OK
);
301 case COMMAND_RECONFIG_DATA_SUBMIT
:
302 cb_data
->status
= BIT(SVC_STATUS_BUFFER_SUBMITTED
);
304 case COMMAND_RECONFIG_STATUS
:
305 cb_data
->status
= BIT(SVC_STATUS_COMPLETED
);
307 case COMMAND_RSU_RETRY
:
308 case COMMAND_RSU_MAX_RETRY
:
309 cb_data
->status
= BIT(SVC_STATUS_OK
);
310 cb_data
->kaddr1
= &res
.a1
;
312 case COMMAND_RSU_DCMF_VERSION
:
313 cb_data
->status
= BIT(SVC_STATUS_OK
);
314 cb_data
->kaddr1
= &res
.a1
;
315 cb_data
->kaddr2
= &res
.a2
;
318 pr_warn("it shouldn't happen\n");
322 pr_debug("%s: call receive_cb\n", __func__
);
323 p_data
->chan
->scl
->receive_cb(p_data
->chan
->scl
, cb_data
);
327 * svc_normal_to_secure_thread() - the function to run in the kthread
328 * @data: data pointer for kthread function
330 * Service layer driver creates stratix10_svc_smc_hvc_call kthread on CPU
331 * node 0, its function stratix10_svc_secure_call_thread is used to handle
332 * SMC or HVC calls between kernel driver and secure monitor software.
334 * Return: 0 for success or -ENOMEM on error.
336 static int svc_normal_to_secure_thread(void *data
)
338 struct stratix10_svc_controller
339 *ctrl
= (struct stratix10_svc_controller
*)data
;
340 struct stratix10_svc_data
*pdata
;
341 struct stratix10_svc_cb_data
*cbdata
;
342 struct arm_smccc_res res
;
343 unsigned long a0
, a1
, a2
;
346 pdata
= kmalloc(sizeof(*pdata
), GFP_KERNEL
);
350 cbdata
= kmalloc(sizeof(*cbdata
), GFP_KERNEL
);
356 /* default set, to remove build warning */
357 a0
= INTEL_SIP_SMC_FPGA_CONFIG_LOOPBACK
;
361 pr_debug("smc_hvc_shm_thread is running\n");
363 while (!kthread_should_stop()) {
364 ret_fifo
= kfifo_out_spinlocked(&ctrl
->svc_fifo
,
365 pdata
, sizeof(*pdata
),
366 &ctrl
->svc_fifo_lock
);
371 pr_debug("get from FIFO pa=0x%016x, command=%u, size=%u\n",
372 (unsigned int)pdata
->paddr
, pdata
->command
,
373 (unsigned int)pdata
->size
);
375 switch (pdata
->command
) {
376 case COMMAND_RECONFIG_DATA_CLAIM
:
377 svc_thread_cmd_data_claim(ctrl
, pdata
, cbdata
);
379 case COMMAND_RECONFIG
:
380 a0
= INTEL_SIP_SMC_FPGA_CONFIG_START
;
381 pr_debug("conf_type=%u\n", (unsigned int)pdata
->flag
);
385 case COMMAND_RECONFIG_DATA_SUBMIT
:
386 a0
= INTEL_SIP_SMC_FPGA_CONFIG_WRITE
;
387 a1
= (unsigned long)pdata
->paddr
;
388 a2
= (unsigned long)pdata
->size
;
390 case COMMAND_RECONFIG_STATUS
:
391 a0
= INTEL_SIP_SMC_FPGA_CONFIG_ISDONE
;
395 case COMMAND_RSU_STATUS
:
396 a0
= INTEL_SIP_SMC_RSU_STATUS
;
400 case COMMAND_RSU_UPDATE
:
401 a0
= INTEL_SIP_SMC_RSU_UPDATE
;
405 case COMMAND_RSU_NOTIFY
:
406 a0
= INTEL_SIP_SMC_RSU_NOTIFY
;
410 case COMMAND_RSU_RETRY
:
411 a0
= INTEL_SIP_SMC_RSU_RETRY_COUNTER
;
415 case COMMAND_RSU_MAX_RETRY
:
416 a0
= INTEL_SIP_SMC_RSU_MAX_RETRY
;
420 case COMMAND_RSU_DCMF_VERSION
:
421 a0
= INTEL_SIP_SMC_RSU_DCMF_VERSION
;
426 pr_warn("it shouldn't happen\n");
429 pr_debug("%s: before SMC call -- a0=0x%016x a1=0x%016x",
430 __func__
, (unsigned int)a0
, (unsigned int)a1
);
431 pr_debug(" a2=0x%016x\n", (unsigned int)a2
);
433 ctrl
->invoke_fn(a0
, a1
, a2
, 0, 0, 0, 0, 0, &res
);
435 pr_debug("%s: after SMC call -- res.a0=0x%016x",
436 __func__
, (unsigned int)res
.a0
);
437 pr_debug(" res.a1=0x%016x, res.a2=0x%016x",
438 (unsigned int)res
.a1
, (unsigned int)res
.a2
);
439 pr_debug(" res.a3=0x%016x\n", (unsigned int)res
.a3
);
441 if (pdata
->command
== COMMAND_RSU_STATUS
) {
442 if (res
.a0
== INTEL_SIP_SMC_RSU_ERROR
)
443 cbdata
->status
= BIT(SVC_STATUS_ERROR
);
445 cbdata
->status
= BIT(SVC_STATUS_OK
);
447 cbdata
->kaddr1
= &res
;
448 cbdata
->kaddr2
= NULL
;
449 cbdata
->kaddr3
= NULL
;
450 pdata
->chan
->scl
->receive_cb(pdata
->chan
->scl
, cbdata
);
455 case INTEL_SIP_SMC_STATUS_OK
:
456 svc_thread_recv_status_ok(pdata
, cbdata
, res
);
458 case INTEL_SIP_SMC_STATUS_BUSY
:
459 switch (pdata
->command
) {
460 case COMMAND_RECONFIG_DATA_SUBMIT
:
461 svc_thread_cmd_data_claim(ctrl
,
464 case COMMAND_RECONFIG_STATUS
:
465 svc_thread_cmd_config_status(ctrl
,
469 pr_warn("it shouldn't happen\n");
473 case INTEL_SIP_SMC_STATUS_REJECTED
:
474 pr_debug("%s: STATUS_REJECTED\n", __func__
);
476 case INTEL_SIP_SMC_STATUS_ERROR
:
477 case INTEL_SIP_SMC_RSU_ERROR
:
478 pr_err("%s: STATUS_ERROR\n", __func__
);
479 cbdata
->status
= BIT(SVC_STATUS_ERROR
);
480 cbdata
->kaddr1
= NULL
;
481 cbdata
->kaddr2
= NULL
;
482 cbdata
->kaddr3
= NULL
;
483 pdata
->chan
->scl
->receive_cb(pdata
->chan
->scl
, cbdata
);
486 pr_warn("Secure firmware doesn't support...\n");
489 * be compatible with older version firmware which
490 * doesn't support RSU notify or retry
492 if ((pdata
->command
== COMMAND_RSU_RETRY
) ||
493 (pdata
->command
== COMMAND_RSU_MAX_RETRY
) ||
494 (pdata
->command
== COMMAND_RSU_NOTIFY
)) {
496 BIT(SVC_STATUS_NO_SUPPORT
);
497 cbdata
->kaddr1
= NULL
;
498 cbdata
->kaddr2
= NULL
;
499 cbdata
->kaddr3
= NULL
;
500 pdata
->chan
->scl
->receive_cb(
501 pdata
->chan
->scl
, cbdata
);
515 * svc_normal_to_secure_shm_thread() - the function to run in the kthread
516 * @data: data pointer for kthread function
518 * Service layer driver creates stratix10_svc_smc_hvc_shm kthread on CPU
519 * node 0, its function stratix10_svc_secure_shm_thread is used to query the
520 * physical address of memory block reserved by secure monitor software at
523 * svc_normal_to_secure_shm_thread() calls do_exit() directly since it is a
524 * standlone thread for which no one will call kthread_stop() or return when
525 * 'kthread_should_stop()' is true.
527 static int svc_normal_to_secure_shm_thread(void *data
)
529 struct stratix10_svc_sh_memory
530 *sh_mem
= (struct stratix10_svc_sh_memory
*)data
;
531 struct arm_smccc_res res
;
533 /* SMC or HVC call to get shared memory info from secure world */
534 sh_mem
->invoke_fn(INTEL_SIP_SMC_FPGA_CONFIG_GET_MEM
,
535 0, 0, 0, 0, 0, 0, 0, &res
);
536 if (res
.a0
== INTEL_SIP_SMC_STATUS_OK
) {
537 sh_mem
->addr
= res
.a1
;
538 sh_mem
->size
= res
.a2
;
540 pr_err("%s: after SMC call -- res.a0=0x%016x", __func__
,
541 (unsigned int)res
.a0
);
546 complete(&sh_mem
->sync_complete
);
551 * svc_get_sh_memory() - get memory block reserved by secure monitor SW
552 * @pdev: pointer to service layer device
553 * @sh_memory: pointer to service shared memory structure
555 * Return: zero for successfully getting the physical address of memory block
556 * reserved by secure monitor software, or negative value on error.
558 static int svc_get_sh_memory(struct platform_device
*pdev
,
559 struct stratix10_svc_sh_memory
*sh_memory
)
561 struct device
*dev
= &pdev
->dev
;
562 struct task_struct
*sh_memory_task
;
563 unsigned int cpu
= 0;
565 init_completion(&sh_memory
->sync_complete
);
567 /* smc or hvc call happens on cpu 0 bound kthread */
568 sh_memory_task
= kthread_create_on_node(svc_normal_to_secure_shm_thread
,
571 "svc_smc_hvc_shm_thread");
572 if (IS_ERR(sh_memory_task
)) {
573 dev_err(dev
, "fail to create stratix10_svc_smc_shm_thread\n");
577 wake_up_process(sh_memory_task
);
579 if (!wait_for_completion_timeout(&sh_memory
->sync_complete
, 10 * HZ
)) {
581 "timeout to get sh-memory paras from secure world\n");
585 if (!sh_memory
->addr
|| !sh_memory
->size
) {
587 "failed to get shared memory info from secure world\n");
591 dev_dbg(dev
, "SM software provides paddr: 0x%016x, size: 0x%08x\n",
592 (unsigned int)sh_memory
->addr
,
593 (unsigned int)sh_memory
->size
);
599 * svc_create_memory_pool() - create a memory pool from reserved memory block
600 * @pdev: pointer to service layer device
601 * @sh_memory: pointer to service shared memory structure
603 * Return: pool allocated from reserved memory block or ERR_PTR() on error.
605 static struct gen_pool
*
606 svc_create_memory_pool(struct platform_device
*pdev
,
607 struct stratix10_svc_sh_memory
*sh_memory
)
609 struct device
*dev
= &pdev
->dev
;
610 struct gen_pool
*genpool
;
617 size_t page_mask
= PAGE_SIZE
- 1;
618 int min_alloc_order
= 3;
621 begin
= roundup(sh_memory
->addr
, PAGE_SIZE
);
622 end
= rounddown(sh_memory
->addr
+ sh_memory
->size
, PAGE_SIZE
);
625 va
= memremap(paddr
, size
, MEMREMAP_WC
);
627 dev_err(dev
, "fail to remap shared memory\n");
628 return ERR_PTR(-EINVAL
);
630 vaddr
= (unsigned long)va
;
632 "reserved memory vaddr: %p, paddr: 0x%16x size: 0x%8x\n",
633 va
, (unsigned int)paddr
, (unsigned int)size
);
634 if ((vaddr
& page_mask
) || (paddr
& page_mask
) ||
635 (size
& page_mask
)) {
636 dev_err(dev
, "page is not aligned\n");
637 return ERR_PTR(-EINVAL
);
639 genpool
= gen_pool_create(min_alloc_order
, -1);
641 dev_err(dev
, "fail to create genpool\n");
642 return ERR_PTR(-ENOMEM
);
644 gen_pool_set_algo(genpool
, gen_pool_best_fit
, NULL
);
645 ret
= gen_pool_add_virt(genpool
, vaddr
, paddr
, size
, -1);
647 dev_err(dev
, "fail to add memory chunk to the pool\n");
648 gen_pool_destroy(genpool
);
656 * svc_smccc_smc() - secure monitor call between normal and secure world
657 * @a0: argument passed in registers 0
658 * @a1: argument passed in registers 1
659 * @a2: argument passed in registers 2
660 * @a3: argument passed in registers 3
661 * @a4: argument passed in registers 4
662 * @a5: argument passed in registers 5
663 * @a6: argument passed in registers 6
664 * @a7: argument passed in registers 7
665 * @res: result values from register 0 to 3
667 static void svc_smccc_smc(unsigned long a0
, unsigned long a1
,
668 unsigned long a2
, unsigned long a3
,
669 unsigned long a4
, unsigned long a5
,
670 unsigned long a6
, unsigned long a7
,
671 struct arm_smccc_res
*res
)
673 arm_smccc_smc(a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, res
);
677 * svc_smccc_hvc() - hypervisor call between normal and secure world
678 * @a0: argument passed in registers 0
679 * @a1: argument passed in registers 1
680 * @a2: argument passed in registers 2
681 * @a3: argument passed in registers 3
682 * @a4: argument passed in registers 4
683 * @a5: argument passed in registers 5
684 * @a6: argument passed in registers 6
685 * @a7: argument passed in registers 7
686 * @res: result values from register 0 to 3
688 static void svc_smccc_hvc(unsigned long a0
, unsigned long a1
,
689 unsigned long a2
, unsigned long a3
,
690 unsigned long a4
, unsigned long a5
,
691 unsigned long a6
, unsigned long a7
,
692 struct arm_smccc_res
*res
)
694 arm_smccc_hvc(a0
, a1
, a2
, a3
, a4
, a5
, a6
, a7
, res
);
698 * get_invoke_func() - invoke SMC or HVC call
699 * @dev: pointer to device
701 * Return: function pointer to svc_smccc_smc or svc_smccc_hvc.
703 static svc_invoke_fn
*get_invoke_func(struct device
*dev
)
707 if (of_property_read_string(dev
->of_node
, "method", &method
)) {
708 dev_warn(dev
, "missing \"method\" property\n");
709 return ERR_PTR(-ENXIO
);
712 if (!strcmp(method
, "smc"))
713 return svc_smccc_smc
;
714 if (!strcmp(method
, "hvc"))
715 return svc_smccc_hvc
;
717 dev_warn(dev
, "invalid \"method\" property: %s\n", method
);
719 return ERR_PTR(-EINVAL
);
723 * stratix10_svc_request_channel_byname() - request a service channel
724 * @client: pointer to service client
725 * @name: service client name
727 * This function is used by service client to request a service channel.
729 * Return: a pointer to channel assigned to the client on success,
730 * or ERR_PTR() on error.
732 struct stratix10_svc_chan
*stratix10_svc_request_channel_byname(
733 struct stratix10_svc_client
*client
, const char *name
)
735 struct device
*dev
= client
->dev
;
736 struct stratix10_svc_controller
*controller
;
737 struct stratix10_svc_chan
*chan
= NULL
;
741 /* if probe was called after client's, or error on probe */
742 if (list_empty(&svc_ctrl
))
743 return ERR_PTR(-EPROBE_DEFER
);
745 controller
= list_first_entry(&svc_ctrl
,
746 struct stratix10_svc_controller
, node
);
747 for (i
= 0; i
< SVC_NUM_CHANNEL
; i
++) {
748 if (!strcmp(controller
->chans
[i
].name
, name
)) {
749 chan
= &controller
->chans
[i
];
754 /* if there was no channel match */
755 if (i
== SVC_NUM_CHANNEL
) {
756 dev_err(dev
, "%s: channel not allocated\n", __func__
);
757 return ERR_PTR(-EINVAL
);
760 if (chan
->scl
|| !try_module_get(controller
->dev
->driver
->owner
)) {
761 dev_dbg(dev
, "%s: svc not free\n", __func__
);
762 return ERR_PTR(-EBUSY
);
765 spin_lock_irqsave(&chan
->lock
, flag
);
767 chan
->ctrl
->num_active_client
++;
768 spin_unlock_irqrestore(&chan
->lock
, flag
);
772 EXPORT_SYMBOL_GPL(stratix10_svc_request_channel_byname
);
775 * stratix10_svc_free_channel() - free service channel
776 * @chan: service channel to be freed
778 * This function is used by service client to free a service channel.
780 void stratix10_svc_free_channel(struct stratix10_svc_chan
*chan
)
784 spin_lock_irqsave(&chan
->lock
, flag
);
786 chan
->ctrl
->num_active_client
--;
787 module_put(chan
->ctrl
->dev
->driver
->owner
);
788 spin_unlock_irqrestore(&chan
->lock
, flag
);
790 EXPORT_SYMBOL_GPL(stratix10_svc_free_channel
);
793 * stratix10_svc_send() - send a message data to the remote
794 * @chan: service channel assigned to the client
795 * @msg: message data to be sent, in the format of
796 * "struct stratix10_svc_client_msg"
798 * This function is used by service client to add a message to the service
799 * layer driver's queue for being sent to the secure world.
801 * Return: 0 for success, -ENOMEM or -ENOBUFS on error.
803 int stratix10_svc_send(struct stratix10_svc_chan
*chan
, void *msg
)
805 struct stratix10_svc_client_msg
806 *p_msg
= (struct stratix10_svc_client_msg
*)msg
;
807 struct stratix10_svc_data_mem
*p_mem
;
808 struct stratix10_svc_data
*p_data
;
810 unsigned int cpu
= 0;
812 p_data
= kzalloc(sizeof(*p_data
), GFP_KERNEL
);
816 /* first client will create kernel thread */
817 if (!chan
->ctrl
->task
) {
819 kthread_create_on_node(svc_normal_to_secure_thread
,
822 "svc_smc_hvc_thread");
823 if (IS_ERR(chan
->ctrl
->task
)) {
824 dev_err(chan
->ctrl
->dev
,
825 "failed to create svc_smc_hvc_thread\n");
829 kthread_bind(chan
->ctrl
->task
, cpu
);
830 wake_up_process(chan
->ctrl
->task
);
833 pr_debug("%s: sent P-va=%p, P-com=%x, P-size=%u\n", __func__
,
834 p_msg
->payload
, p_msg
->command
,
835 (unsigned int)p_msg
->payload_length
);
837 if (list_empty(&svc_data_mem
)) {
838 if (p_msg
->command
== COMMAND_RECONFIG
) {
839 struct stratix10_svc_command_config_type
*ct
=
840 (struct stratix10_svc_command_config_type
*)
842 p_data
->flag
= ct
->flags
;
845 list_for_each_entry(p_mem
, &svc_data_mem
, node
)
846 if (p_mem
->vaddr
== p_msg
->payload
) {
847 p_data
->paddr
= p_mem
->paddr
;
852 p_data
->command
= p_msg
->command
;
853 p_data
->arg
[0] = p_msg
->arg
[0];
854 p_data
->arg
[1] = p_msg
->arg
[1];
855 p_data
->arg
[2] = p_msg
->arg
[2];
856 p_data
->size
= p_msg
->payload_length
;
858 pr_debug("%s: put to FIFO pa=0x%016x, cmd=%x, size=%u\n", __func__
,
859 (unsigned int)p_data
->paddr
, p_data
->command
,
860 (unsigned int)p_data
->size
);
861 ret
= kfifo_in_spinlocked(&chan
->ctrl
->svc_fifo
, p_data
,
863 &chan
->ctrl
->svc_fifo_lock
);
872 EXPORT_SYMBOL_GPL(stratix10_svc_send
);
875 * stratix10_svc_done() - complete service request transactions
876 * @chan: service channel assigned to the client
878 * This function should be called when client has finished its request
879 * or there is an error in the request process. It allows the service layer
880 * to stop the running thread to have maximize savings in kernel resources.
882 void stratix10_svc_done(struct stratix10_svc_chan
*chan
)
884 /* stop thread when thread is running AND only one active client */
885 if (chan
->ctrl
->task
&& chan
->ctrl
->num_active_client
<= 1) {
886 pr_debug("svc_smc_hvc_shm_thread is stopped\n");
887 kthread_stop(chan
->ctrl
->task
);
888 chan
->ctrl
->task
= NULL
;
891 EXPORT_SYMBOL_GPL(stratix10_svc_done
);
894 * stratix10_svc_allocate_memory() - allocate memory
895 * @chan: service channel assigned to the client
896 * @size: memory size requested by a specific service client
898 * Service layer allocates the requested number of bytes buffer from the
899 * memory pool, service client uses this function to get allocated buffers.
901 * Return: address of allocated memory on success, or ERR_PTR() on error.
903 void *stratix10_svc_allocate_memory(struct stratix10_svc_chan
*chan
,
906 struct stratix10_svc_data_mem
*pmem
;
909 struct gen_pool
*genpool
= chan
->ctrl
->genpool
;
910 size_t s
= roundup(size
, 1 << genpool
->min_alloc_order
);
912 pmem
= devm_kzalloc(chan
->ctrl
->dev
, sizeof(*pmem
), GFP_KERNEL
);
914 return ERR_PTR(-ENOMEM
);
916 va
= gen_pool_alloc(genpool
, s
);
918 return ERR_PTR(-ENOMEM
);
920 memset((void *)va
, 0, s
);
921 pa
= gen_pool_virt_to_phys(genpool
, va
);
923 pmem
->vaddr
= (void *)va
;
926 list_add_tail(&pmem
->node
, &svc_data_mem
);
927 pr_debug("%s: va=%p, pa=0x%016x\n", __func__
,
928 pmem
->vaddr
, (unsigned int)pmem
->paddr
);
932 EXPORT_SYMBOL_GPL(stratix10_svc_allocate_memory
);
935 * stratix10_svc_free_memory() - free allocated memory
936 * @chan: service channel assigned to the client
937 * @kaddr: memory to be freed
939 * This function is used by service client to free allocated buffers.
941 void stratix10_svc_free_memory(struct stratix10_svc_chan
*chan
, void *kaddr
)
943 struct stratix10_svc_data_mem
*pmem
;
946 list_for_each_entry(pmem
, &svc_data_mem
, node
)
947 if (pmem
->vaddr
== kaddr
) {
952 gen_pool_free(chan
->ctrl
->genpool
, (unsigned long)kaddr
, size
);
954 list_del(&pmem
->node
);
956 EXPORT_SYMBOL_GPL(stratix10_svc_free_memory
);
958 static const struct of_device_id stratix10_svc_drv_match
[] = {
959 {.compatible
= "intel,stratix10-svc"},
960 {.compatible
= "intel,agilex-svc"},
964 static int stratix10_svc_drv_probe(struct platform_device
*pdev
)
966 struct device
*dev
= &pdev
->dev
;
967 struct stratix10_svc_controller
*controller
;
968 struct stratix10_svc_chan
*chans
;
969 struct gen_pool
*genpool
;
970 struct stratix10_svc_sh_memory
*sh_memory
;
971 struct stratix10_svc
*svc
;
973 svc_invoke_fn
*invoke_fn
;
977 /* get SMC or HVC function */
978 invoke_fn
= get_invoke_func(dev
);
979 if (IS_ERR(invoke_fn
))
982 sh_memory
= devm_kzalloc(dev
, sizeof(*sh_memory
), GFP_KERNEL
);
986 sh_memory
->invoke_fn
= invoke_fn
;
987 ret
= svc_get_sh_memory(pdev
, sh_memory
);
991 genpool
= svc_create_memory_pool(pdev
, sh_memory
);
995 /* allocate service controller and supporting channel */
996 controller
= devm_kzalloc(dev
, sizeof(*controller
), GFP_KERNEL
);
1000 chans
= devm_kmalloc_array(dev
, SVC_NUM_CHANNEL
,
1001 sizeof(*chans
), GFP_KERNEL
| __GFP_ZERO
);
1005 controller
->dev
= dev
;
1006 controller
->num_chans
= SVC_NUM_CHANNEL
;
1007 controller
->num_active_client
= 0;
1008 controller
->chans
= chans
;
1009 controller
->genpool
= genpool
;
1010 controller
->task
= NULL
;
1011 controller
->invoke_fn
= invoke_fn
;
1012 init_completion(&controller
->complete_status
);
1014 fifo_size
= sizeof(struct stratix10_svc_data
) * SVC_NUM_DATA_IN_FIFO
;
1015 ret
= kfifo_alloc(&controller
->svc_fifo
, fifo_size
, GFP_KERNEL
);
1017 dev_err(dev
, "failed to allocate FIFO\n");
1020 spin_lock_init(&controller
->svc_fifo_lock
);
1022 chans
[0].scl
= NULL
;
1023 chans
[0].ctrl
= controller
;
1024 chans
[0].name
= SVC_CLIENT_FPGA
;
1025 spin_lock_init(&chans
[0].lock
);
1027 chans
[1].scl
= NULL
;
1028 chans
[1].ctrl
= controller
;
1029 chans
[1].name
= SVC_CLIENT_RSU
;
1030 spin_lock_init(&chans
[1].lock
);
1032 list_add_tail(&controller
->node
, &svc_ctrl
);
1033 platform_set_drvdata(pdev
, controller
);
1035 /* add svc client device(s) */
1036 svc
= devm_kzalloc(dev
, sizeof(*svc
), GFP_KERNEL
);
1040 svc
->stratix10_svc_rsu
= platform_device_alloc(STRATIX10_RSU
, 0);
1041 if (!svc
->stratix10_svc_rsu
) {
1042 dev_err(dev
, "failed to allocate %s device\n", STRATIX10_RSU
);
1046 ret
= platform_device_add(svc
->stratix10_svc_rsu
);
1048 platform_device_put(svc
->stratix10_svc_rsu
);
1051 dev_set_drvdata(dev
, svc
);
1053 pr_info("Intel Service Layer Driver Initialized\n");
1058 static int stratix10_svc_drv_remove(struct platform_device
*pdev
)
1060 struct stratix10_svc
*svc
= dev_get_drvdata(&pdev
->dev
);
1061 struct stratix10_svc_controller
*ctrl
= platform_get_drvdata(pdev
);
1063 platform_device_unregister(svc
->stratix10_svc_rsu
);
1065 kfifo_free(&ctrl
->svc_fifo
);
1067 kthread_stop(ctrl
->task
);
1071 gen_pool_destroy(ctrl
->genpool
);
1072 list_del(&ctrl
->node
);
1077 static struct platform_driver stratix10_svc_driver
= {
1078 .probe
= stratix10_svc_drv_probe
,
1079 .remove
= stratix10_svc_drv_remove
,
1081 .name
= "stratix10-svc",
1082 .of_match_table
= stratix10_svc_drv_match
,
1086 static int __init
stratix10_svc_init(void)
1088 struct device_node
*fw_np
;
1089 struct device_node
*np
;
1092 fw_np
= of_find_node_by_name(NULL
, "firmware");
1096 np
= of_find_matching_node(fw_np
, stratix10_svc_drv_match
);
1101 ret
= of_platform_populate(fw_np
, stratix10_svc_drv_match
, NULL
, NULL
);
1105 return platform_driver_register(&stratix10_svc_driver
);
1108 static void __exit
stratix10_svc_exit(void)
1110 return platform_driver_unregister(&stratix10_svc_driver
);
1113 subsys_initcall(stratix10_svc_init
);
1114 module_exit(stratix10_svc_exit
);
1116 MODULE_LICENSE("GPL v2");
1117 MODULE_DESCRIPTION("Intel Stratix10 Service Layer Driver");
1118 MODULE_AUTHOR("Richard Gong <richard.gong@intel.com>");
1119 MODULE_ALIAS("platform:stratix10-svc");