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 | |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 30 | /* |
| 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 | */ |
| 34 | static psa_status_t sfn_messaging(struct service_t *service, |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 35 | struct conn_handle_t *hdl) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 36 | { |
| 37 | struct partition_t *p_target; |
| 38 | psa_status_t status; |
| 39 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 40 | if (!hdl || !service || !service->p_ldinf || !service->partition) { |
Mingyang Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 41 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 42 | } |
| 43 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 44 | hdl->sfn_magic = TFM_MSG_MAGIC_SFN; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 45 | p_target = service->partition; |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 46 | p_target->p_handles = hdl; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 47 | |
| 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 Sun | bb4a42a | 2021-12-14 15:18:52 +0800 | [diff] [blame] | 55 | return PSA_ERROR_PROGRAMMER_ERROR; |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 56 | } |
| 57 | } |
| 58 | p_target->state = SFN_PARTITION_STATE_INITED; |
| 59 | } |
| 60 | |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 61 | status = ((service_fn_t)service->p_ldinf->sfn)(&hdl->msg); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 62 | |
| 63 | return status; |
| 64 | } |
| 65 | |
Ken Liu | f8c7e53 | 2022-02-10 15:03:04 +0800 | [diff] [blame] | 66 | static psa_status_t sfn_replying(struct conn_handle_t *hdl, int32_t status) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 67 | { |
Ken Liu | 0bed7e0 | 2022-02-10 12:38:07 +0800 | [diff] [blame] | 68 | SET_CURRENT_COMPONENT(hdl->p_client); |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 69 | |
| 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 Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 83 | static void spm_thread_fn(void) |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 84 | { |
| 85 | struct partition_t *p_part, *p_curr; |
| 86 | |
| 87 | p_curr = GET_CURRENT_COMPONENT(); |
| 88 | /* Call partition initialization routine one by one. */ |
Ken Liu | 5a28da3 | 2022-01-19 14:37:05 +0800 | [diff] [blame] | 89 | UNI_LIST_FOREACH(p_part, PARTITION_LIST_ADDR, next) { |
Kevin Peng | 56c571e | 2022-01-10 14:06:05 +0800 | [diff] [blame] | 90 | if (IS_PARTITION_IPC_MODEL(p_part->p_ldinf)) { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 91 | 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 Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 110 | } |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 111 | |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 112 | /* Parameters are treated as assuredly */ |
| 113 | void 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 Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 116 | |
Ken Liu | ef229a2 | 2022-02-11 11:15:43 +0800 | [diff] [blame] | 117 | 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 Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | uint32_t sfn_system_run(void) |
| 143 | { |
Ken Liu | f39d8eb | 2021-10-07 12:55:33 +0800 | [diff] [blame] | 144 | return thrd_start_scheduler(&CURRENT_THREAD); |
| 145 | } |
| 146 | |
| 147 | const 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 | }; |