You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mynewt.apache.org by GitBox <gi...@apache.org> on 2021/08/18 19:23:00 UTC
[GitHub] [mynewt-core] kasjer commented on a change in pull request #2664: Updates to the kinetis TRNG driver
kasjer commented on a change in pull request #2664:
URL: https://github.com/apache/mynewt-core/pull/2664#discussion_r691535404
##########
File path: hw/drivers/trng/trng_kinetis/src/trng_kinetis.c
##########
@@ -35,88 +35,138 @@
do { \
(base)->MCTL |= (TRNG_MCTL_PRGM_MASK | TRNG_MCTL_ERR_MASK); \
} while (0)
+#define TRNG_CLEAR_AND_ENABLE_INTS(base) \
+ do { \
+ (base)->INT_CTRL &= ~(TRNG_INT_CTRL_HW_ERR_MASK | \
+ TRNG_INT_CTRL_ENT_VAL_MASK); \
+ (base)->INT_MASK |= TRNG_INT_MASK_HW_ERR_MASK | \
+ TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
+#define TRNG_CLEAR_AND_ENABLE_ENTROPY_INT(base) \
+ do { \
+ (base)->INT_CTRL &= ~TRNG_INT_CTRL_ENT_VAL_MASK; \
+ (base)->INT_MASK |= TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
+#define TRNG_DISABLE_ENTROPY_INT(base) \
+ do { \
+ (base)->INT_MASK &= ~TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
#else
#error "Unsupported TRNG interface"
#endif
#include "trng/trng.h"
#include "trng_kinetis/trng_kinetis.h"
-static uint8_t rng_cache[ MYNEWT_VAL(KINETIS_TRNG_CACHE_LEN) ];
-static uint16_t rng_cache_out;
-static uint16_t rng_cache_in;
-static struct os_mutex rng_cache_mu;
-static os_stack_t *pstack;
+#define TRNG_CACHE_LEN MYNEWT_VAL(KINETIS_TRNG_CACHE_LEN)
+static struct {
+ uint8_t cache[TRNG_CACHE_LEN];
+ uint16_t out;
+ uint16_t in;
+ uint16_t used;
+ struct os_mutex mu;
Review comment:
This order of fields with cache being first is prone to leave some padding before **out** and before **mu**
##########
File path: hw/drivers/trng/trng_kinetis/src/trng_kinetis.c
##########
@@ -35,88 +35,138 @@
do { \
(base)->MCTL |= (TRNG_MCTL_PRGM_MASK | TRNG_MCTL_ERR_MASK); \
} while (0)
+#define TRNG_CLEAR_AND_ENABLE_INTS(base) \
+ do { \
+ (base)->INT_CTRL &= ~(TRNG_INT_CTRL_HW_ERR_MASK | \
+ TRNG_INT_CTRL_ENT_VAL_MASK); \
+ (base)->INT_MASK |= TRNG_INT_MASK_HW_ERR_MASK | \
+ TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
+#define TRNG_CLEAR_AND_ENABLE_ENTROPY_INT(base) \
+ do { \
+ (base)->INT_CTRL &= ~TRNG_INT_CTRL_ENT_VAL_MASK; \
+ (base)->INT_MASK |= TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
+#define TRNG_DISABLE_ENTROPY_INT(base) \
+ do { \
+ (base)->INT_MASK &= ~TRNG_INT_MASK_ENT_VAL_MASK; \
+ } while (0)
#else
#error "Unsupported TRNG interface"
#endif
#include "trng/trng.h"
#include "trng_kinetis/trng_kinetis.h"
-static uint8_t rng_cache[ MYNEWT_VAL(KINETIS_TRNG_CACHE_LEN) ];
-static uint16_t rng_cache_out;
-static uint16_t rng_cache_in;
-static struct os_mutex rng_cache_mu;
-static os_stack_t *pstack;
+#define TRNG_CACHE_LEN MYNEWT_VAL(KINETIS_TRNG_CACHE_LEN)
+static struct {
+ uint8_t cache[TRNG_CACHE_LEN];
+ uint16_t out;
+ uint16_t in;
+ uint16_t used;
+ struct os_mutex mu;
+} rng_state;
+static_assert(sizeof(rng_state.cache) == TRNG_CACHE_LEN,
+ "Must fix TRNG_CACHE_LEN usage");
+#define CACHE_OUT(x) \
+ do { \
+ (x) = rng_state.cache[rng_state.out]; \
+ rng_state.out = (rng_state.out + 1) % TRNG_CACHE_LEN; \
+ rng_state.used--; \
+ } while (0)
+#define CACHE_IN(x) \
+ do { \
+ rng_state.cache[rng_state.in] = (x); \
+ rng_state.in = (rng_state.in + 1) % TRNG_CACHE_LEN; \
+ rng_state.used++; \
+ } while (0)
+#define IS_CACHE_FULL() (rng_state.used == TRNG_CACHE_LEN)
+#define IS_CACHE_EMPTY() (rng_state.used == 0)
+#define CACHE_INIT() \
+ do { \
+ rng_state.out = 0; \
+ rng_state.in = 0; \
+ rng_state.used = 0; \
+ } while (0)
+#define CACHE_LOCK() os_mutex_pend(&rng_state.mu, OS_TIMEOUT_NEVER)
+#define CACHE_UNLOCK() os_mutex_release(&rng_state.mu)
+#define CACHE_INIT_LOCK() os_mutex_init(&rng_state.mu)
+
+#if USE_RNGA
static bool running;
+#endif
static struct os_eventq rng_evtq;
+static struct os_event evt = {0};
#define TRNG_POLLER_PRIO (8)
#define TRNG_POLLER_STACK_SIZE OS_STACK_ALIGN(64)
-static struct os_task poller_task;
+static struct os_task trng_poller_task;
+OS_TASK_STACK_DEFINE(trng_poller_stack, TRNG_POLLER_STACK_SIZE);
-static inline void
-kinetis_trng_start(void)
+#if USE_TRNG
+static void
+trng_irq_handler(void)
{
- struct os_event evt;
+ if (TRNG0->MCTL & TRNG_MCTL_ERR_MASK) {
+ TRNG0->MCTL |= TRNG_MCTL_ERR_MASK;
+ }
+ if (TRNG0->INT_CTRL & TRNG_INT_CTRL_HW_ERR_MASK) {
+ TRNG0->INT_CTRL &= ~TRNG_INT_CTRL_HW_ERR_MASK;
+ }
+
+ if (TRNG0->INT_CTRL & TRNG_INT_CTRL_ENT_VAL_MASK) {
+ TRNG_DISABLE_ENTROPY_INT(TRNG0);
+ (void)os_eventq_put(&rng_evtq, &evt);
+ }
+}
+#endif
+
+static void
+kinetis_trng_start(void)
+{
#if USE_RNGA
RNGA_SetMode(RNG, kRNGA_ModeNormal);
+ running = true;
+ (void)os_eventq_put(&rng_evtq, &evt);
#elif USE_TRNG
TRNG_START(TRNG0);
#endif
- running = true;
-
- evt.ev_queued = 0;
- evt.ev_arg = NULL;
- (void)os_eventq_put(&rng_evtq, &evt);
}
static inline void
kinetis_trng_stop(void)
{
#if USE_RNGA
RNGA_SetMode(RNG, kRNGA_ModeSleep);
+ running = false;
#elif USE_TRNG
TRNG_STOP(TRNG0);
#endif
-
- running = false;
}
static size_t
kinetis_trng_read(struct trng_dev *trng, void *ptr, size_t size)
{
size_t num_read;
+ uint8_t *u8p;
+
+ CACHE_LOCK();
Review comment:
CACHE_LOCK could be after next two initialization lines
##########
File path: hw/drivers/trng/trng_kinetis/syscfg.yml
##########
@@ -30,3 +30,10 @@ syscfg.defs:
value: 0
restrictions:
- '!KINETIS_TRNG_USE_RNGA'
+ KINETIS_TRNG_ENTROPY_DELAY:
+ description: >
+ The entropy delay can be used to improve the speed of number
+ generation with the compromise of worse entropy. 3200 is the
+ default value, but it can be reduced for faster value
+ generation.
Review comment:
I have not read documentation of TRNG block. Maybe description could specify what the units are.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@mynewt.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org