blob: d52d0f50fea04a7b5b0e864cca6cdf616415d880 [file] [log] [blame]
Ken Liuf39d8eb2021-10-07 12:55:33 +08001/*
Mingyang Sunbb4a42a2021-12-14 15:18:52 +08002 * Copyright (c) 2021-2022, Arm Limited. All rights reserved.
Ken Liuf39d8eb2021-10-07 12:55:33 +08003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8#include <stdint.h>
9#include "compiler_ext_defs.h"
10#include "current.h"
Kevin Pengb288c522021-09-26 16:18:23 +080011#include "tfm_hal_platform.h"
Ken Liuf39d8eb2021-10-07 12:55:33 +080012#include "ffm/backend.h"
13#include "load/partition_defs.h"
14#include "load/service_defs.h"
15#include "load/spm_load_api.h"
16#include "psa/error.h"
17#include "psa/service.h"
18#include "spm_ipc.h"
19
20/* SFN Partition state */
21#define SFN_PARTITION_STATE_NOT_INITED 0
22#define SFN_PARTITION_STATE_INITED 1
23
24typedef psa_status_t (*service_fn_t)(psa_msg_t *msg);
25typedef psa_status_t (*sfn_init_fn_t)(void);
26
27/* Declare the global component list */
28struct partition_head_t partition_listhead;
29
Ken Liuf39d8eb2021-10-07 12:55:33 +080030/*
31 * Send message and wake up the SP who is waiting on message queue, block the
32 * current component state and activate the next component.
33 */
34static psa_status_t sfn_messaging(struct service_t *service,
Ken Liu0bed7e02022-02-10 12:38:07 +080035 struct conn_handle_t *hdl)
Ken Liuf39d8eb2021-10-07 12:55:33 +080036{
37 struct partition_t *p_target;
38 psa_status_t status;
39
Ken Liu0bed7e02022-02-10 12:38:07 +080040 if (!hdl || !service || !service->p_ldinf || !service->partition) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +080041 return PSA_ERROR_PROGRAMMER_ERROR;
Ken Liuf39d8eb2021-10-07 12:55:33 +080042 }
43
Ken Liu0bed7e02022-02-10 12:38:07 +080044 hdl->sfn_magic = TFM_MSG_MAGIC_SFN;
Ken Liuf39d8eb2021-10-07 12:55:33 +080045 p_target = service->partition;
Ken Liu0bed7e02022-02-10 12:38:07 +080046 p_target->p_handles = hdl;
Ken Liuf39d8eb2021-10-07 12:55:33 +080047
48 SET_CURRENT_COMPONENT(p_target);
49
50 if (p_target->state == SFN_PARTITION_STATE_NOT_INITED) {
51 if (p_target->p_ldinf->entry != 0) {
52 status = ((sfn_init_fn_t)p_target->p_ldinf->entry)();
53 /* Negative value indicates errors. */
54 if (status < PSA_SUCCESS) {
Mingyang Sunbb4a42a2021-12-14 15:18:52 +080055 return PSA_ERROR_PROGRAMMER_ERROR;
Ken Liuf39d8eb2021-10-07 12:55:33 +080056 }
57 }
58 p_target->state = SFN_PARTITION_STATE_INITED;
59 }
60
Ken Liu0bed7e02022-02-10 12:38:07 +080061 status = ((service_fn_t)service->p_ldinf->sfn)(&hdl->msg);
Ken Liuf39d8eb2021-10-07 12:55:33 +080062
63 return status;
64}
65
Ken Liuf8c7e532022-02-10 15:03:04 +080066static psa_status_t sfn_replying(struct conn_handle_t *hdl, int32_t status)
Ken Liuf39d8eb2021-10-07 12:55:33 +080067{
Ken Liu0bed7e02022-02-10 12:38:07 +080068 SET_CURRENT_COMPONENT(hdl->p_client);
Ken Liuf39d8eb2021-10-07 12:55:33 +080069
70 /*
71 * Returning a value here is necessary, because 'psa_reply' is absent
72 * for SFN clients, the 'reply' method is performed by SPM internally
73 * when SFN case, to forward the 'status' to the caller.
74 *
75 * For example:
76 * 'status' MAY contain a 'psa_handle_t' returned by SPM 'connect' and
77 * SPM needs to 'reply' it back to the caller. Treat 'psa_handle_t' value
78 * as SPM specific return value and represnent it as 'psa_status_t'.
79 */
80 return status;
81}
82
Ken Liuef229a22022-02-11 11:15:43 +080083static void spm_thread_fn(void)
Ken Liuf39d8eb2021-10-07 12:55:33 +080084{
85 struct partition_t *p_part, *p_curr;
86
87 p_curr = GET_CURRENT_COMPONENT();
88 /* Call partition initialization routine one by one. */
Ken Liu5a28da32022-01-19 14:37:05 +080089 UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) {
Kevin Peng56c571e2022-01-10 14:06:05 +080090 if (IS_PARTITION_IPC_MODEL(p_part->p_ldinf)) {
Ken Liuf39d8eb2021-10-07 12:55:33 +080091 continue;
92 }
93
94 if (p_part->state == SFN_PARTITION_STATE_INITED) {
95 continue;
96 }
97
98 SET_CURRENT_COMPONENT(p_part);
99
100 if (p_part->p_ldinf->entry != 0) {
101 if (((sfn_init_fn_t)p_part->p_ldinf->entry)() < PSA_SUCCESS) {
102 tfm_core_panic();
103 }
104 }
105
106 p_part->state = SFN_PARTITION_STATE_INITED;
107 }
108
109 SET_CURRENT_COMPONENT(p_curr);
Ken Liuef229a22022-02-11 11:15:43 +0800110}
Ken Liuf39d8eb2021-10-07 12:55:33 +0800111
Ken Liuef229a22022-02-11 11:15:43 +0800112/* Parameters are treated as assuredly */
113void sfn_comp_init_assuredly(struct partition_t *p_pt, uint32_t service_set)
114{
115 const struct partition_load_info_t *p_pldi = p_pt->p_ldinf;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800116
Ken Liuef229a22022-02-11 11:15:43 +0800117 p_pt->p_handles = NULL;
118 p_pt->state = SFN_PARTITION_STATE_NOT_INITED;
119
120 THRD_SYNC_INIT(&p_pt->waitobj);
121
122 ARCH_CTXCTRL_INIT(&p_pt->ctx_ctrl,
123 LOAD_ALLOCED_STACK_ADDR(p_pldi),
124 p_pldi->stack_size);
125
126 /*
127 * Built-in partitions still have thread instances: NS Agent (TZ) and
128 * IDLE partition, and NS Agent (TZ) needs to be specific cared here.
129 */
130 if (p_pldi->pid == TFM_SP_NON_SECURE_ID) {
131 THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl,
132 TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags)));
133
134 thrd_start(&p_pt->thrd,
135 POSITION_TO_ENTRY(spm_thread_fn, thrd_fn_t),
136 POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t));
137 }
138
139 (void)service_set;
Ken Liuf39d8eb2021-10-07 12:55:33 +0800140}
141
142uint32_t sfn_system_run(void)
143{
Ken Liuf39d8eb2021-10-07 12:55:33 +0800144 return thrd_start_scheduler(&CURRENT_THREAD);
145}
146
147const struct backend_ops_t backend_instance = {
148 .comp_init_assuredly = sfn_comp_init_assuredly,
149 .system_run = sfn_system_run,
150 .messaging = sfn_messaging,
151 .replying = sfn_replying
152};