blob: 15726740b9fad7d6b70aa1e5737cb92f6dd1fec1 [file] [log] [blame]
Mingyang Sundeae45d2021-09-06 15:31:07 +08001/*
Mingyang Sunbb4a42a2021-12-14 15:18:52 +08002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
Chris Brand3778bc12021-12-15 17:01:05 -08003 * Copyright (c) 2021, Cypress Semiconductor Corporation. All rights reserved.
Mingyang Sundeae45d2021-09-06 15:31:07 +08004 *
5 * SPDX-License-Identifier: BSD-3-Clause
6 *
7 */
8
9#include <stdint.h>
Mingyang Sun620c8562021-11-10 11:44:58 +080010#include "critical_section.h"
Ken Liue07c3b72021-10-14 16:19:13 +080011#include "compiler_ext_defs.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080012#include "spm_ipc.h"
Ken Liu62bae592021-10-19 22:15:43 +080013#include "tfm_hal_isolation.h"
Kevin Pengb288c522021-09-26 16:18:23 +080014#include "tfm_hal_platform.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080015#include "tfm_rpc.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080016#include "ffm/backend.h"
Ken Liu62bae592021-10-19 22:15:43 +080017#include "utilities.h"
Mingyang Sundeae45d2021-09-06 15:31:07 +080018#include "load/partition_defs.h"
19#include "load/service_defs.h"
20#include "load/spm_load_api.h"
21#include "psa/error.h"
22
23/* Declare the global component list */
24struct partition_head_t partition_listhead;
25
Xinyu Zhanga7ba50b2021-12-27 17:32:53 +080026#ifdef CONFIG_TFM_PSA_API_CROSS_CALL
Ken Liue07c3b72021-10-14 16:19:13 +080027
28#ifdef TFM_MULTI_CORE_TOPOLOGY
29/* TODO: To be checked when RPC design updates. */
30static uint8_t spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE] __aligned(8);
31struct context_ctrl_t spm_thread_context = {
32 .sp = (uint32_t)&spm_stack_local[CONFIG_TFM_SPM_THREAD_STACK_SIZE],
33 .sp_limit = (uint32_t)spm_stack_local,
34 .reserved = 0,
35 .exc_ret = 0,
36};
37struct context_ctrl_t *p_spm_thread_context = &spm_thread_context;
38#else
39struct context_ctrl_t *p_spm_thread_context;
40#endif
41
42#endif
43
Mingyang Sundeae45d2021-09-06 15:31:07 +080044/*
45 * Send message and wake up the SP who is waiting on message queue, block the
Ken Liuf39d8eb2021-10-07 12:55:33 +080046 * current thread and trigger scheduler.
Mingyang Sundeae45d2021-09-06 15:31:07 +080047 */
48static psa_status_t ipc_messaging(struct service_t *service,
49 struct tfm_msg_body_t *msg)
50{
51 struct partition_t *p_owner = NULL;
52 psa_signal_t signal = 0;
Mingyang Sun620c8562021-11-10 11:44:58 +080053 struct critical_section_t cs_assert = CRITICAL_SECTION_STATIC_INIT;
Mingyang Sundeae45d2021-09-06 15:31:07 +080054
55 if (!msg || !service || !service->p_ldinf || !service->partition) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +080056 return PSA_ERROR_PROGRAMMER_ERROR;
Mingyang Sundeae45d2021-09-06 15:31:07 +080057 }
58
59 p_owner = service->partition;
60 signal = service->p_ldinf->signal;
61
Mingyang Sun620c8562021-11-10 11:44:58 +080062 CRITICAL_SECTION_ENTER(cs_assert);
Mingyang Sundeae45d2021-09-06 15:31:07 +080063 /* Add message to partition message list tail */
64 BI_LIST_INSERT_BEFORE(&p_owner->msg_list, &msg->msg_node);
65
66 /* Messages put. Update signals */
67 p_owner->signals_asserted |= signal;
68
69 if (p_owner->signals_waiting & signal) {
70 thrd_wake_up(&p_owner->waitobj,
71 (p_owner->signals_asserted & p_owner->signals_waiting));
72 p_owner->signals_waiting &= ~signal;
73 }
Mingyang Sun620c8562021-11-10 11:44:58 +080074 CRITICAL_SECTION_LEAVE(cs_assert);
Mingyang Sundeae45d2021-09-06 15:31:07 +080075
76 /*
77 * If it is a NS request via RPC, it is unnecessary to block current
78 * thread.
79 */
80
81 if (!is_tfm_rpc_msg(msg)) {
82 thrd_wait_on(&msg->ack_evnt, CURRENT_THREAD);
83 }
84
85 return PSA_SUCCESS;
86}
87
Ken Liuf39d8eb2021-10-07 12:55:33 +080088static int32_t ipc_replying(struct tfm_msg_body_t *p_msg, int32_t status)
Ken Liu802a3702021-10-15 12:09:56 +080089{
90 if (is_tfm_rpc_msg(p_msg)) {
91 tfm_rpc_client_call_reply(p_msg, status);
92 } else {
93 thrd_wake_up(&p_msg->ack_evnt, status);
94 }
Ken Liuf39d8eb2021-10-07 12:55:33 +080095
96 /*
97 * 'psa_reply' exists in IPC model only and returns 'void'. Return
98 * 'PSA_SUCCESS' here always since SPM does not forward the status
99 * to the caller.
100 */
101 return PSA_SUCCESS;
Ken Liu802a3702021-10-15 12:09:56 +0800102}
103
Mingyang Sundeae45d2021-09-06 15:31:07 +0800104/* Parameters are treated as assuredly */
105static void ipc_comp_init_assuredly(struct partition_t *p_pt,
106 uint32_t service_setting)
107{
108 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
109 void *p_param = NULL;
110
111 p_pt->signals_allowed |= PSA_DOORBELL | service_setting;
112
113 THRD_SYNC_INIT(&p_pt->waitobj);
114 BI_LIST_INIT_NODE(&p_pt->msg_list);
115
116 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
117 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
118
119 if (p_pldi->pid == TFM_SP_NON_SECURE_ID) {
Kevin Pengb288c522021-09-26 16:18:23 +0800120 p_param = (void *)tfm_hal_get_ns_entry_point();
Ken Liue07c3b72021-10-14 16:19:13 +0800121
Xinyu Zhanga7ba50b2021-12-27 17:32:53 +0800122#ifdef CONFIG_TFM_PSA_API_CROSS_CALL
Chris Brand3778bc12021-12-15 17:01:05 -0800123#ifndef TFM_MULTI_CORE_TOPOLOGY
Ken Liue07c3b72021-10-14 16:19:13 +0800124 SPM_THREAD_CONTEXT = &p_pt->ctx_ctrl;
125#endif
Chris Brand3778bc12021-12-15 17:01:05 -0800126#endif
Ken Liue07c3b72021-10-14 16:19:13 +0800127
Mingyang Sundeae45d2021-09-06 15:31:07 +0800128 }
129
130 thrd_start(&p_pt->thrd,
131 POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), p_param,
132 LOAD_ALLOCED_STACK_ADDR(p_pldi),
133 LOAD_ALLOCED_STACK_ADDR(p_pldi) + p_pldi->stack_size);
134}
135
136static uint32_t ipc_system_run(void)
137{
Ken Liu62bae592021-10-19 22:15:43 +0800138 uint32_t control;
139 struct partition_t *p_cur_pt;
140
Chris Brand3778bc12021-12-15 17:01:05 -0800141#ifdef CONFIG_TFM_PSA_API_THREAD_CALL
142 TFM_CORE_ASSERT(SPM_THREAD_CONTEXT);
143#endif
144
Ken Liu62bae592021-10-19 22:15:43 +0800145 control = thrd_start_scheduler(&CURRENT_THREAD);
146
147 p_cur_pt = TO_CONTAINER(CURRENT_THREAD->p_context_ctrl,
148 struct partition_t, ctx_ctrl);
149
150 if (tfm_hal_update_boundaries(p_cur_pt->p_ldinf, p_cur_pt->p_boundaries)
151 != TFM_HAL_SUCCESS) {
152 tfm_core_panic();
153 }
154
155 return control;
Mingyang Sundeae45d2021-09-06 15:31:07 +0800156}
157
158const struct backend_ops_t backend_instance = {
159 .comp_init_assuredly = ipc_comp_init_assuredly,
160 .system_run = ipc_system_run,
161 .messaging = ipc_messaging,
Ken Liu802a3702021-10-15 12:09:56 +0800162 .replying = ipc_replying,
Mingyang Sundeae45d2021-09-06 15:31:07 +0800163};