blob: f7d23d2bc44f5663efcce832cb63e6bd8f071bff [file] [log] [blame]
David Hu69e590e2020-05-12 17:19:21 +08001/*
2 * Copyright (c) 2020-2021, Arm Limited. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
8/*
9 * This file is a reference implementation of Non-secure mailbox RTOS API.
10 * This reference implementation is based on TF-M ROTS wrapper API.
11 * It can be replaced by RTOS specific implementation.
12 */
13
14#include "os_wrapper/semaphore.h"
15#include "os_wrapper/thread.h"
16
17#include "tfm_ns_mailbox.h"
18
19#define MAX_SEMAPHORE_COUNT NUM_MAILBOX_QUEUE_SLOT
20
David Hu6730af22020-05-11 19:50:08 +080021/*
22 * Thread flag to manage wait/wake mechanism in mailbox.、
23 * Thread flag can be RTOS specific.
24 * The following example definition also covers the rule of CMSIS-RTOS2, which
25 * requires the MSB of thread flags must be 0b0.
26 */
27#define MAILBOX_THREAD_FLAG 0x5FCA0000
28
David Hu69e590e2020-05-12 17:19:21 +080029static void *ns_lock_handle = NULL;
30
31#ifdef TFM_MULTI_CORE_MULTI_CLIENT_CALL
32const void *tfm_ns_mailbox_os_get_task_handle(void)
33{
34 return os_wrapper_thread_get_handle();
35}
36
David Hu6730af22020-05-11 19:50:08 +080037void tfm_ns_mailbox_os_wait_reply(void)
David Hu69e590e2020-05-12 17:19:21 +080038{
David Hu6730af22020-05-11 19:50:08 +080039 os_wrapper_thread_wait_flag(MAILBOX_THREAD_FLAG, OS_WRAPPER_WAIT_FOREVER);
David Hu69e590e2020-05-12 17:19:21 +080040}
41
David Hu6730af22020-05-11 19:50:08 +080042void tfm_ns_mailbox_os_wake_task_isr(const void *task_handle)
David Hu69e590e2020-05-12 17:19:21 +080043{
David Hu6730af22020-05-11 19:50:08 +080044 os_wrapper_thread_set_flag_isr((void *)task_handle, MAILBOX_THREAD_FLAG);
David Hu69e590e2020-05-12 17:19:21 +080045}
46#endif /* TFM_MULTI_CORE_MULTI_CLIENT_CALL */
47
48int32_t tfm_ns_mailbox_os_lock_init(void)
49{
50 ns_lock_handle = os_wrapper_semaphore_create(MAX_SEMAPHORE_COUNT,
51 MAX_SEMAPHORE_COUNT,
52 NULL);
53 if (!ns_lock_handle) {
54 return MAILBOX_GENERIC_ERROR;
55 }
56
57 return MAILBOX_SUCCESS;
58}
59
60int32_t tfm_ns_mailbox_os_lock_acquire(void)
61{
62 return os_wrapper_semaphore_acquire(ns_lock_handle,
63 OS_WRAPPER_WAIT_FOREVER);
64}
65
66int32_t tfm_ns_mailbox_os_lock_release(void)
67{
68 return os_wrapper_semaphore_release(ns_lock_handle);
69}