1 /*
2  * Copyright (c) 2024, Texas Instruments Incorporated
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/kernel.h>
8 #include <zephyr/sys/__assert.h>
9 #include <kernel/zephyr/dpl/dpl.h>
10 #include <ti/drivers/dpl/MutexP.h>
11 
12 /*
13  * Zephyr kernel object pools:
14  *
15  * This bit of code enables the simplelink host driver, which assumes dynamic
16  * allocation of kernel objects (semaphores, mutexes, hwis), to be
17  * more easily ported to Zephyr (which supports static allocation).
18  *
19  * It leverages the Zephyr memory slab, enabling us to define a mutex object
20  * pool for use by the SimpleLink host driver.
21  */
22 
23 /* Define a Mutex pool: */
24 #define DPL_MAX_MUTEXES 4 /* From simplelink driver code inspection */
25 K_MEM_SLAB_DEFINE(mutex_slab, sizeof(struct k_mutex), DPL_MAX_MUTEXES, MEM_ALIGN);
26 
dpl_mutex_pool_alloc()27 static struct k_mutex *dpl_mutex_pool_alloc()
28 {
29     struct k_mutex *mutex_ptr = NULL;
30 
31     if (k_mem_slab_alloc(&mutex_slab, (void **)&mutex_ptr, K_NO_WAIT) < 0)
32     {
33         /*
34          * We assert, as this is a logic error, due to a change in #
35          * of mutexes needed by the simplelink driver. In that case,
36          * the mutex pool must be increased programmatically to match.
37          */
38         __ASSERT(0, "Increase size of DPL mutex pool");
39     }
40     return mutex_ptr;
41 }
42 
dpl_mutex_pool_free(struct k_mutex * mutex)43 static MutexP_Status dpl_mutex_pool_free(struct k_mutex *mutex)
44 {
45     k_mem_slab_free(&mutex_slab, (void **)&mutex);
46     return MutexP_OK;
47 }
48 
MutexP_create(MutexP_Params * params)49 MutexP_Handle MutexP_create(MutexP_Params *params)
50 {
51     struct k_mutex *mutex;
52 
53     ARG_UNUSED(params);
54 
55     mutex = dpl_mutex_pool_alloc();
56     __ASSERT(mutex, "MutexP_create failed\r\n");
57 
58     if (mutex)
59     {
60         k_mutex_init(mutex);
61     }
62     return ((MutexP_Handle)mutex);
63 }
64 
MutexP_delete(MutexP_Handle handle)65 void MutexP_delete(MutexP_Handle handle)
66 {
67     /* No way in Zephyr to "reset" the lock, so just re-init: */
68     k_mutex_init((struct k_mutex *)handle);
69 
70     dpl_mutex_pool_free((struct k_mutex *)handle);
71 }
72 
MutexP_lock(MutexP_Handle handle)73 uintptr_t MutexP_lock(MutexP_Handle handle)
74 {
75     unsigned int key = 0;
76     int retval;
77 
78     retval = k_mutex_lock((struct k_mutex *)handle, K_FOREVER);
79     __ASSERT(retval == 0, "MutexP_lock: retval: %d\r\n", retval);
80 
81     return ((uintptr_t)key);
82 }
83 
MutexP_Params_init(MutexP_Params * params)84 void MutexP_Params_init(MutexP_Params *params)
85 {
86     params->callback = NULL;
87 }
88 
MutexP_unlock(MutexP_Handle handle,uintptr_t key)89 void MutexP_unlock(MutexP_Handle handle, uintptr_t key)
90 {
91     ARG_UNUSED(key);
92 
93     k_mutex_unlock((struct k_mutex *)handle);
94 }
95