Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 1 | /* |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 2 | * Copyright (c) 2021-2022, Arm Limited. All rights reserved. |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 3 | * |
| 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 Peng | b288c52 | 2021-09-26 16:18:23 +0800 | [diff] [blame] | 11 | #include "tfm_hal_platform.h" |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 12 | #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 | |
| 24 | typedef psa_status_t (*service_fn_t)(psa_msg_t *msg); |
| 25 | typedef psa_status_t (*sfn_init_fn_t)(void); |
| 26 | |
| 27 | /* Declare the global component list */ |
| 28 | struct partition_head_t partition_listhead; |
| 29 | |
| 30 | /* TODO: To be checked with the RPC design. */ |
| 31 | static uintptr_t spm_stack_limit; |
| 32 | static uintptr_t spm_stack_base; |
| 33 | |
| 34 | /* |
| 35 | * Send message and wake up the SP who is waiting on message queue, block the |
| 36 | * current component state and activate the next component. |
| 37 | */ |
| 38 | static psa_status_t sfn_messaging(struct service_t *service, |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 39 | struct conn_handle_t *hdl) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 40 | { |
| 41 | struct partition_t *p_target; |
| 42 | psa_status_t status; |
| 43 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 44 | if (!hdl || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 45 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 46 | } |
| 47 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 48 | hdl->sfn_magic = TFM_MSG_MAGIC_SFN; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 49 | p_target = service->partition; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 50 | p_target->p_handles = hdl; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 51 | |
| 52 | SET_CURRENT_COMPONENT(p_target); |
| 53 | |
| 54 | if (p_target->state == SFN_PARTITION_STATE_NOT_INITED) { |
| 55 | if (p_target->p_ldinf->entry != 0) { |
| 56 | status = ((sfn_init_fn_t)p_target->p_ldinf->entry)(); |
| 57 | /* Negative value indicates errors. */ |
| 58 | if (status < PSA_SUCCESS) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 59 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | p_target->state = SFN_PARTITION_STATE_INITED; |
| 63 | } |
| 64 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 65 | status = ((service_fn_t)service->p_ldinf->sfn)(&hdl->msg); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 66 | |
| 67 | return status; |
| 68 | } |
| 69 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 70 | static int32_t sfn_replying(struct conn_handle_t *hdl, int32_t status) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 71 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 72 | SET_CURRENT_COMPONENT(hdl->p_client); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 73 | |
| 74 | /* |
| 75 | * Returning a value here is necessary, because 'psa_reply' is absent |
| 76 | * for SFN clients, the 'reply' method is performed by SPM internally |
| 77 | * when SFN case, to forward the 'status' to the caller. |
| 78 | * |
| 79 | * For example: |
| 80 | * 'status' MAY contain a 'psa_handle_t' returned by SPM 'connect' and |
| 81 | * SPM needs to 'reply' it back to the caller. Treat 'psa_handle_t' value |
| 82 | * as SPM specific return value and represnent it as 'psa_status_t'. |
| 83 | */ |
| 84 | return status; |
| 85 | } |
| 86 | |
| 87 | /* Parameters are treated as assuredly */ |
| 88 | void sfn_comp_init_assuredly(struct partition_t *p_pt, uint32_t service_set) |
| 89 | { |
| 90 | const struct partition_load_info_t *p_pldi = p_pt->p_ldinf; |
| 91 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame^] | 92 | p_pt->p_handles = NULL; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 93 | p_pt->state = SFN_PARTITION_STATE_NOT_INITED; |
| 94 | |
| 95 | THRD_SYNC_INIT(&p_pt->waitobj); |
| 96 | |
| 97 | /* |
| 98 | * Built-in partitions still have thread instances: NS Agent (TZ) and |
| 99 | * IDLE partition, and NS Agent (TZ) needs to be specific cared here. |
| 100 | */ |
Kevin Peng | 56c571e | 2022-01-10 14:06:05 +0800 | [diff] [blame] | 101 | if (IS_PARTITION_IPC_MODEL(p_pldi)) { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 102 | THRD_INIT(&p_pt->thrd, &p_pt->ctx_ctrl, |
| 103 | TO_THREAD_PRIORITY(PARTITION_PRIORITY(p_pldi->flags))); |
| 104 | |
| 105 | thrd_start(&p_pt->thrd, |
| 106 | POSITION_TO_ENTRY(p_pldi->entry, thrd_fn_t), |
Summer Qin | 9544482 | 2022-01-27 11:22:00 +0800 | [diff] [blame] | 107 | NULL, |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 108 | LOAD_ALLOCED_STACK_ADDR(p_pldi), |
| 109 | LOAD_ALLOCED_STACK_ADDR(p_pldi) + p_pldi->stack_size); |
| 110 | |
| 111 | /* SPM reuses the ns agent stack, use once only at initialization. */ |
Ken Liu | 897e8f1 | 2022-02-10 03:21:17 +0100 | [diff] [blame] | 112 | if (p_pldi->pid == TFM_SP_NON_SECURE_ID) { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 113 | spm_stack_limit = p_pt->ctx_ctrl.sp_limit; |
| 114 | spm_stack_base = p_pt->ctx_ctrl.sp; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | (void)service_set; |
| 119 | } |
| 120 | |
| 121 | static void spm_thread_fn(void *p) |
| 122 | { |
| 123 | struct partition_t *p_part, *p_curr; |
| 124 | |
| 125 | p_curr = GET_CURRENT_COMPONENT(); |
| 126 | /* Call partition initialization routine one by one. */ |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 127 | UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) { |
Kevin Peng | 56c571e | 2022-01-10 14:06:05 +0800 | [diff] [blame] | 128 | if (IS_PARTITION_IPC_MODEL(p_part->p_ldinf)) { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 129 | continue; |
| 130 | } |
| 131 | |
| 132 | if (p_part->state == SFN_PARTITION_STATE_INITED) { |
| 133 | continue; |
| 134 | } |
| 135 | |
| 136 | SET_CURRENT_COMPONENT(p_part); |
| 137 | |
| 138 | if (p_part->p_ldinf->entry != 0) { |
| 139 | if (((sfn_init_fn_t)p_part->p_ldinf->entry)() < PSA_SUCCESS) { |
| 140 | tfm_core_panic(); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | p_part->state = SFN_PARTITION_STATE_INITED; |
| 145 | } |
| 146 | |
| 147 | SET_CURRENT_COMPONENT(p_curr); |
| 148 | |
| 149 | thrd_set_state(CURRENT_THREAD, THRD_STATE_DETACH); |
| 150 | |
| 151 | tfm_arch_trigger_pendsv(); |
| 152 | } |
| 153 | |
| 154 | uint32_t sfn_system_run(void) |
| 155 | { |
| 156 | struct thread_t *p_thrd = (struct thread_t *)spm_stack_limit; |
| 157 | struct context_ctrl_t *p_ctxctrl = (struct context_ctrl_t *)(p_thrd + 1); |
| 158 | uintptr_t sp_limit = (((uintptr_t)(p_ctxctrl + 1)) + 7) & ~0x7; |
| 159 | uintptr_t sp_base = spm_stack_base; |
| 160 | |
| 161 | THRD_INIT(p_thrd, p_ctxctrl, THRD_PRIOR_HIGHEST); |
| 162 | thrd_start(p_thrd, spm_thread_fn, NULL, sp_limit, sp_base); |
| 163 | |
| 164 | return thrd_start_scheduler(&CURRENT_THREAD); |
| 165 | } |
| 166 | |
| 167 | const struct backend_ops_t backend_instance = { |
| 168 | .comp_init_assuredly = sfn_comp_init_assuredly, |
| 169 | .system_run = sfn_system_run, |
| 170 | .messaging = sfn_messaging, |
| 171 | .replying = sfn_replying |
| 172 | }; |