Skip to content

Commit f4c2dc5

Browse files
decsnydleach02
authored andcommitted
drivers: entropy_mcux_caam: Add semaphore
Add a semaphore to the entropy mcux caam driver to make the driver thread safe, since some static variables in the HAL can be the source of some race conditions. Signed-off-by: Declan Snyder <[email protected]>
1 parent 2c98a00 commit f4c2dc5

File tree

1 file changed

+14
-0
lines changed

1 file changed

+14
-0
lines changed

drivers/entropy/entropy_mcux_caam.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include <zephyr/drivers/entropy.h>
1111
#include <zephyr/random/rand32.h>
1212
#include <zephyr/init.h>
13+
#include <zephyr/kernel.h>
1314

1415
#include "fsl_caam.h"
1516

@@ -21,6 +22,10 @@ static caam_job_ring_interface_t jrif0 __attribute__((__section__(".nocache")));
2122
static uint8_t rng_buff_pool[CONFIG_ENTRY_MCUX_CAAM_POOL_SIZE]
2223
__attribute__((__section__(".nocache")));
2324

25+
26+
/* This semaphore is needed to prevent race condition to static variables in the HAL driver */
27+
K_SEM_DEFINE(mcux_caam_sem, 1, 1)
28+
2429
static int entropy_mcux_caam_get_entropy(const struct device *dev,
2530
uint8_t *buffer,
2631
uint16_t length)
@@ -30,6 +35,8 @@ static int entropy_mcux_caam_get_entropy(const struct device *dev,
3035
caam_handle_t handle;
3136
uint16_t read_length = 0;
3237
uint16_t insert_idx = 0;
38+
int ret = 0;
39+
k_timeout_t sem_timeout = K_MSEC(10);
3340

3441
handle.jobRing = kCAAM_JobRing0;
3542

@@ -41,10 +48,17 @@ static int entropy_mcux_caam_get_entropy(const struct device *dev,
4148
while (insert_idx < length) {
4249
read_length = MIN(sizeof(rng_buff_pool), (length - insert_idx));
4350

51+
ret = k_sem_take(&mcux_caam_sem, sem_timeout);
52+
if (ret) {
53+
return ret;
54+
}
55+
4456
status = CAAM_RNG_GetRandomData(
4557
config->base, &handle, kCAAM_RngStateHandle0,
4658
&rng_buff_pool[0], read_length, kCAAM_RngDataAny, NULL);
4759

60+
k_sem_give(&mcux_caam_sem);
61+
4862
memcpy(&buffer[insert_idx], &rng_buff_pool[0], read_length);
4963
insert_idx += read_length;
5064
}

0 commit comments

Comments
 (0)