1 /*
2  * Copyright (c) 2022, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include <stdint.h>
9 
10 #include "runtime_defs.h"
11 #include "sprt_partition_metadata_indicator.h"
12 
13 #include "psa/error.h"
14 #include "psa/service.h"
15 
common_sfn_thread(void)16 void common_sfn_thread(void)
17 {
18     psa_signal_t sig_asserted, signal_mask, sig;
19     psa_msg_t msg;
20     struct runtime_metadata_t *meta;
21     service_fn_t *p_sfn_table;
22     sfn_init_fn_t sfn_init;
23 
24     meta = PART_METADATA();
25     sfn_init = (sfn_init_fn_t)meta->entry;
26     p_sfn_table = (service_fn_t *)meta->sfn_table;
27     signal_mask = (1 << meta->n_sfn) - 1;
28 
29     if (sfn_init && sfn_init() != PSA_SUCCESS) {
30         psa_panic();
31     }
32 
33     while (1) {
34         sig_asserted = psa_wait(signal_mask, PSA_BLOCK);
35         /* Handle signals */
36         for (int i = 0; sig_asserted != 0 && i < meta->n_sfn; i++) {
37             sig = 1 << i;
38             if (sig_asserted & sig) {
39                 /* The i bit signal asserted, index of SFN is i as well */
40                 if (!p_sfn_table[i]) {
41                     /* No corresponding SFN */
42                     psa_panic();
43                 }
44 
45                 psa_get(sig, &msg);
46                 psa_reply(msg.handle, ((service_fn_t)p_sfn_table[i])(&msg));
47                 sig_asserted &= ~sig;
48             }
49         }
50 
51         if (sig_asserted != 0) {
52             /* Wrong signal asserted */
53             psa_panic();
54         }
55     }
56 }
57