12 #include "sw/device/lib/base/multibits.h"
15 #include "entropy_src_regs.h"
18 if (entropy_src == NULL) {
22 mmio_region_write32(entropy_src->
base_addr,
23 ENTROPY_SRC_MODULE_ENABLE_REG_OFFSET,
24 ENTROPY_SRC_MODULE_ENABLE_REG_RESVAL);
27 if (!mmio_region_read32(entropy_src->
base_addr,
28 ENTROPY_SRC_REGWEN_REG_OFFSET)) {
33 mmio_region_write32(entropy_src->
base_addr,
34 ENTROPY_SRC_ENTROPY_CONTROL_REG_OFFSET,
35 ENTROPY_SRC_ENTROPY_CONTROL_REG_RESVAL);
37 mmio_region_write32(entropy_src->
base_addr, ENTROPY_SRC_CONF_REG_OFFSET,
38 ENTROPY_SRC_CONF_REG_RESVAL);
40 mmio_region_write32(entropy_src->
base_addr,
41 ENTROPY_SRC_HEALTH_TEST_WINDOWS_REG_OFFSET,
42 ENTROPY_SRC_HEALTH_TEST_WINDOWS_REG_RESVAL);
44 mmio_region_write32(entropy_src->
base_addr,
45 ENTROPY_SRC_ALERT_THRESHOLD_REG_OFFSET,
46 ENTROPY_SRC_ALERT_THRESHOLD_REG_RESVAL);
54 if (entropy_src == NULL ||
60 if (!mmio_region_read32(entropy_src->
base_addr,
61 ENTROPY_SRC_REGWEN_REG_OFFSET)) {
67 0, ENTROPY_SRC_ENTROPY_CONTROL_ES_ROUTE_FIELD,
70 entropy_ctrl_reg, ENTROPY_SRC_ENTROPY_CONTROL_ES_TYPE_FIELD,
72 mmio_region_write32(entropy_src->
base_addr,
73 ENTROPY_SRC_ENTROPY_CONTROL_REG_OFFSET, entropy_ctrl_reg);
80 0, ENTROPY_SRC_CONF_FIPS_ENABLE_FIELD,
81 config.
fips_enable ? kMultiBitBool4True : kMultiBitBool4False);
85 entropy_conf_reg, ENTROPY_SRC_CONF_FIPS_FLAG_FIELD,
86 config.
fips_flag ? kMultiBitBool4True : kMultiBitBool4False);
90 entropy_conf_reg, ENTROPY_SRC_CONF_RNG_FIPS_FIELD,
91 config.
rng_fips ? kMultiBitBool4True : kMultiBitBool4False);
95 entropy_conf_reg, ENTROPY_SRC_CONF_ENTROPY_DATA_REG_ENABLE_FIELD,
100 entropy_conf_reg, ENTROPY_SRC_CONF_THRESHOLD_SCOPE_FIELD,
102 : kMultiBitBool4False);
105 uint32_t rng_bit_en =
107 ? kMultiBitBool4False
108 : kMultiBitBool4True;
110 entropy_conf_reg, ENTROPY_SRC_CONF_RNG_BIT_ENABLE_FIELD, rng_bit_en);
111 uint32_t rng_bit_sel =
114 entropy_conf_reg, ENTROPY_SRC_CONF_RNG_BIT_SEL_FIELD, rng_bit_sel);
119 entropy_conf_reg, ENTROPY_SRC_CONF_ENTROPY_DATA_REG_ENABLE_FIELD,
122 mmio_region_write32(entropy_src->
base_addr, ENTROPY_SRC_CONF_REG_OFFSET,
129 uint32_t health_test_window_sizes =
131 ENTROPY_SRC_HEALTH_TEST_WINDOWS_FIPS_WINDOW_FIELD,
133 mmio_region_write32(entropy_src->
base_addr,
134 ENTROPY_SRC_HEALTH_TEST_WINDOWS_REG_OFFSET,
135 health_test_window_sizes);
139 0, ENTROPY_SRC_ALERT_THRESHOLD_ALERT_THRESHOLD_FIELD,
142 alert_threshold, ENTROPY_SRC_ALERT_THRESHOLD_ALERT_THRESHOLD_INV_FIELD,
144 mmio_region_write32(entropy_src->
base_addr,
145 ENTROPY_SRC_ALERT_THRESHOLD_REG_OFFSET, alert_threshold);
148 mmio_region_write32(entropy_src->
base_addr,
149 ENTROPY_SRC_MODULE_ENABLE_REG_OFFSET,
158 if (entropy_src == NULL ||
160 ENTROPY_SRC_OBSERVE_FIFO_THRESH_OBSERVE_FIFO_THRESH_MASK ||
165 if (!mmio_region_read32(entropy_src->
base_addr,
166 ENTROPY_SRC_REGWEN_REG_OFFSET)) {
170 mmio_region_write32(entropy_src->
base_addr,
171 ENTROPY_SRC_OBSERVE_FIFO_THRESH_REG_OFFSET,
178 reg, ENTROPY_SRC_FW_OV_CONTROL_FW_OV_ENTROPY_INSERT_FIELD,
180 mmio_region_write32(entropy_src->
base_addr,
181 ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET, reg);
188 if (entropy_src == NULL) {
193 0, ENTROPY_SRC_FW_OV_SHA3_START_FW_OV_INSERT_START_FIELD,
195 mmio_region_write32(entropy_src->
base_addr,
196 ENTROPY_SRC_FW_OV_SHA3_START_REG_OFFSET, reg);
204 if (entropy_src == NULL) {
208 if (!mmio_region_read32(entropy_src->
base_addr,
209 ENTROPY_SRC_REGWEN_REG_OFFSET)) {
213 ptrdiff_t high_thresholds_reg_offset = -1;
214 ptrdiff_t low_thresholds_reg_offset = -1;
217 high_thresholds_reg_offset = ENTROPY_SRC_REPCNT_THRESHOLDS_REG_OFFSET;
224 high_thresholds_reg_offset = ENTROPY_SRC_REPCNTS_THRESHOLDS_REG_OFFSET;
231 high_thresholds_reg_offset = ENTROPY_SRC_ADAPTP_HI_THRESHOLDS_REG_OFFSET;
232 low_thresholds_reg_offset = ENTROPY_SRC_ADAPTP_LO_THRESHOLDS_REG_OFFSET;
235 high_thresholds_reg_offset = ENTROPY_SRC_BUCKET_THRESHOLDS_REG_OFFSET;
242 high_thresholds_reg_offset = ENTROPY_SRC_MARKOV_HI_THRESHOLDS_REG_OFFSET;
243 low_thresholds_reg_offset = ENTROPY_SRC_MARKOV_LO_THRESHOLDS_REG_OFFSET;
246 high_thresholds_reg_offset = ENTROPY_SRC_EXTHT_HI_THRESHOLDS_REG_OFFSET;
247 low_thresholds_reg_offset = ENTROPY_SRC_EXTHT_LO_THRESHOLDS_REG_OFFSET;
253 mmio_region_write32(entropy_src->
base_addr, high_thresholds_reg_offset,
255 if (low_thresholds_reg_offset != -1) {
256 mmio_region_write32(entropy_src->
base_addr, low_thresholds_reg_offset,
269 if (!mmio_region_read32(entropy_src->
base_addr,
270 ENTROPY_SRC_ME_REGWEN_REG_OFFSET)) {
274 mmio_region_write32(entropy_src->
base_addr,
275 ENTROPY_SRC_MODULE_ENABLE_REG_OFFSET,
282 if (entropy_src == NULL) {
286 mmio_region_write32(entropy_src->
base_addr, ENTROPY_SRC_ME_REGWEN_REG_OFFSET,
288 mmio_region_write32(entropy_src->
base_addr, ENTROPY_SRC_SW_REGUPD_REG_OFFSET,
296 if (entropy_src == NULL || is_locked == NULL) {
300 uint32_t module_enable_regwen = mmio_region_read32(
301 entropy_src->
base_addr, ENTROPY_SRC_ME_REGWEN_REG_OFFSET);
302 uint32_t sw_regupd = mmio_region_read32(entropy_src->
base_addr,
303 ENTROPY_SRC_SW_REGUPD_REG_OFFSET);
304 if (module_enable_regwen == sw_regupd) {
305 *is_locked = sw_regupd == 0;
319 if (entropy_src == NULL || stats == NULL) {
323 ptrdiff_t high_watermarks_reg_offset = -1;
324 ptrdiff_t low_watermarks_reg_offset = -1;
325 ptrdiff_t high_fails_reg_offset = -1;
326 ptrdiff_t low_fails_reg_offset = -1;
327 for (uint32_t i = 0; i < kDifEntropySrcTestNumVariants; ++i) {
330 high_watermarks_reg_offset =
331 ENTROPY_SRC_REPCNT_HI_WATERMARKS_REG_OFFSET;
332 low_watermarks_reg_offset = -1;
333 high_fails_reg_offset = ENTROPY_SRC_REPCNT_TOTAL_FAILS_REG_OFFSET;
334 low_fails_reg_offset = -1;
337 high_watermarks_reg_offset =
338 ENTROPY_SRC_REPCNTS_HI_WATERMARKS_REG_OFFSET;
339 low_watermarks_reg_offset = -1;
340 high_fails_reg_offset = ENTROPY_SRC_REPCNTS_TOTAL_FAILS_REG_OFFSET;
341 low_fails_reg_offset = -1;
344 high_watermarks_reg_offset =
345 ENTROPY_SRC_ADAPTP_HI_WATERMARKS_REG_OFFSET;
346 low_watermarks_reg_offset = ENTROPY_SRC_ADAPTP_LO_WATERMARKS_REG_OFFSET;
347 high_fails_reg_offset = ENTROPY_SRC_ADAPTP_HI_TOTAL_FAILS_REG_OFFSET;
348 low_fails_reg_offset = ENTROPY_SRC_ADAPTP_LO_TOTAL_FAILS_REG_OFFSET;
351 high_watermarks_reg_offset =
352 ENTROPY_SRC_BUCKET_HI_WATERMARKS_REG_OFFSET;
353 low_watermarks_reg_offset = -1;
354 high_fails_reg_offset = ENTROPY_SRC_BUCKET_TOTAL_FAILS_REG_OFFSET;
355 low_fails_reg_offset = -1;
358 high_watermarks_reg_offset =
359 ENTROPY_SRC_MARKOV_HI_WATERMARKS_REG_OFFSET;
360 low_watermarks_reg_offset = ENTROPY_SRC_MARKOV_LO_WATERMARKS_REG_OFFSET;
361 high_fails_reg_offset = ENTROPY_SRC_MARKOV_HI_TOTAL_FAILS_REG_OFFSET;
362 low_fails_reg_offset = ENTROPY_SRC_MARKOV_LO_TOTAL_FAILS_REG_OFFSET;
365 high_watermarks_reg_offset = ENTROPY_SRC_EXTHT_HI_WATERMARKS_REG_OFFSET;
366 low_watermarks_reg_offset = ENTROPY_SRC_EXTHT_LO_WATERMARKS_REG_OFFSET;
367 high_fails_reg_offset = ENTROPY_SRC_EXTHT_HI_TOTAL_FAILS_REG_OFFSET;
368 low_fails_reg_offset = ENTROPY_SRC_EXTHT_LO_TOTAL_FAILS_REG_OFFSET;
375 entropy_src->
base_addr, high_watermarks_reg_offset);
377 low_watermarks_reg_offset == -1
379 : (uint16_t)mmio_region_read32(entropy_src->
base_addr,
380 low_watermarks_reg_offset);
383 mmio_region_read32(entropy_src->
base_addr, high_fails_reg_offset);
385 low_fails_reg_offset == -1
387 : mmio_region_read32(entropy_src->
base_addr, low_fails_reg_offset);
396 if (entropy_src == NULL || counts == NULL) {
400 counts->
total_fails = (uint16_t)mmio_region_read32(
401 entropy_src->
base_addr, ENTROPY_SRC_ALERT_SUMMARY_FAIL_COUNTS_REG_OFFSET);
403 uint32_t alert_fail_counts = mmio_region_read32(
404 entropy_src->
base_addr, ENTROPY_SRC_ALERT_FAIL_COUNTS_REG_OFFSET);
405 uint32_t extht_alert_fail_counts = mmio_region_read32(
406 entropy_src->
base_addr, ENTROPY_SRC_EXTHT_FAIL_COUNTS_REG_OFFSET);
412 ENTROPY_SRC_ALERT_FAIL_COUNTS_REPCNT_FAIL_COUNT_FIELD);
416 ENTROPY_SRC_ALERT_FAIL_COUNTS_REPCNTS_FAIL_COUNT_FIELD);
420 ENTROPY_SRC_ALERT_FAIL_COUNTS_ADAPTP_HI_FAIL_COUNT_FIELD);
422 alert_fail_counts, ENTROPY_SRC_ALERT_FAIL_COUNTS_BUCKET_FAIL_COUNT_FIELD);
425 ENTROPY_SRC_ALERT_FAIL_COUNTS_MARKOV_HI_FAIL_COUNT_FIELD);
428 extht_alert_fail_counts,
429 ENTROPY_SRC_EXTHT_FAIL_COUNTS_EXTHT_HI_FAIL_COUNT_FIELD);
437 ENTROPY_SRC_ALERT_FAIL_COUNTS_ADAPTP_LO_FAIL_COUNT_FIELD);
441 ENTROPY_SRC_ALERT_FAIL_COUNTS_MARKOV_LO_FAIL_COUNT_FIELD);
443 extht_alert_fail_counts,
444 ENTROPY_SRC_EXTHT_FAIL_COUNTS_EXTHT_LO_FAIL_COUNT_FIELD);
451 ENTROPY_SRC_INTR_STATE_REG_OFFSET,
452 ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT);
457 if (entropy_src == NULL) {
466 if (entropy_src == NULL || word == NULL) {
471 if (!is_entropy_available(entropy_src)) {
475 *word = mmio_region_read32(entropy_src->
base_addr,
476 ENTROPY_SRC_ENTROPY_DATA_REG_OFFSET);
481 ENTROPY_SRC_INTR_STATE_REG_OFFSET,
482 ENTROPY_SRC_INTR_STATE_ES_ENTROPY_VALID_BIT);
489 if (entropy_src == NULL) {
495 uint32_t reg = mmio_region_read32(entropy_src->
base_addr,
496 ENTROPY_SRC_OBSERVE_FIFO_THRESH_REG_OFFSET);
503 reg = mmio_region_read32(entropy_src->
base_addr,
504 ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET);
506 kMultiBitBool4True) {
512 reg = mmio_region_read32(entropy_src->
base_addr,
513 ENTROPY_SRC_INTR_STATE_REG_OFFSET);
515 reg, ENTROPY_SRC_INTR_STATE_ES_OBSERVE_FIFO_READY_BIT));
518 for (
size_t i = 0; i < len; ++i) {
519 reg = mmio_region_read32(entropy_src->
base_addr,
520 ENTROPY_SRC_FW_OV_RD_DATA_REG_OFFSET);
528 0, ENTROPY_SRC_INTR_STATE_ES_OBSERVE_FIFO_READY_BIT,
true);
529 mmio_region_write32(entropy_src->
base_addr, ENTROPY_SRC_INTR_STATE_REG_OFFSET,
537 if (entropy_src == NULL || len == NULL) {
543 uint32_t reg = mmio_region_read32(entropy_src->
base_addr,
544 ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET);
546 kMultiBitBool4True) {
551 size_t read_count = 0;
552 while (read_count < *len &&
553 mmio_region_read32(entropy_src->
base_addr,
554 ENTROPY_SRC_OBSERVE_FIFO_DEPTH_REG_OFFSET) > 0) {
555 uint32_t reg = mmio_region_read32(entropy_src->
base_addr,
556 ENTROPY_SRC_FW_OV_RD_DATA_REG_OFFSET);
558 buf[read_count++] = reg;
570 if (entropy_src == NULL || buf == NULL) {
576 uint32_t reg = mmio_region_read32(entropy_src->
base_addr,
577 ENTROPY_SRC_FW_OV_CONTROL_REG_OFFSET);
579 kMultiBitBool4True ||
581 reg, ENTROPY_SRC_FW_OV_CONTROL_FW_OV_ENTROPY_INSERT_FIELD) !=
582 kMultiBitBool4True) {
587 for (
size_t i = 0; i < len; ++i) {
588 if (mmio_region_read32(entropy_src->
base_addr,
589 ENTROPY_SRC_FW_OV_WR_FIFO_FULL_REG_OFFSET)) {
596 mmio_region_write32(entropy_src->
base_addr,
597 ENTROPY_SRC_FW_OV_WR_DATA_REG_OFFSET, buf[i]);
608 if (entropy_src == NULL) {
613 uint32_t current_val = mmio_region_read32(
614 entropy_src->
base_addr, ENTROPY_SRC_FW_OV_SHA3_START_REG_OFFSET);
615 if (current_val == kMultiBitBool4True) {
619 mmio_region_write32(entropy_src->
base_addr,
620 ENTROPY_SRC_FW_OV_SHA3_START_REG_OFFSET,
628 if (entropy_src == NULL) {
634 if (mmio_region_read32(entropy_src->
base_addr,
635 ENTROPY_SRC_FW_OV_WR_FIFO_FULL_REG_OFFSET)) {
639 mmio_region_write32(entropy_src->
base_addr,
640 ENTROPY_SRC_FW_OV_SHA3_START_REG_OFFSET,
641 kMultiBitBool4False);
647 if (entropy_src == NULL || is_full == NULL) {
651 *is_full = mmio_region_read32(entropy_src->
base_addr,
652 ENTROPY_SRC_FW_OV_WR_FIFO_FULL_REG_OFFSET);
659 if (entropy_src == NULL || has_overflowed == NULL) {
663 *has_overflowed = mmio_region_read32(
664 entropy_src->
base_addr, ENTROPY_SRC_FW_OV_RD_FIFO_OVERFLOW_REG_OFFSET);
671 if (entropy_src == NULL || fifo_depth == NULL) {
675 *fifo_depth = mmio_region_read32(entropy_src->
base_addr,
676 ENTROPY_SRC_OBSERVE_FIFO_DEPTH_REG_OFFSET);
684 if (entropy_src == NULL || debug_state == NULL) {
688 uint32_t debug_state_reg = mmio_region_read32(
689 entropy_src->
base_addr, ENTROPY_SRC_DEBUG_STATUS_REG_OFFSET);
691 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_ENTROPY_FIFO_DEPTH_FIELD);
693 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_SHA3_FSM_FIELD);
695 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_SHA3_BLOCK_PR_BIT);
697 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_SHA3_SQUEEZING_BIT);
699 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_SHA3_ABSORBED_BIT);
701 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_SHA3_ERR_BIT);
703 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_MAIN_SM_IDLE_BIT);
705 debug_state_reg, ENTROPY_SRC_DEBUG_STATUS_MAIN_SM_BOOT_DONE_BIT);
712 if (entropy_src == NULL || alerts == NULL) {
716 *alerts = mmio_region_read32(entropy_src->
base_addr,
717 ENTROPY_SRC_RECOV_ALERT_STS_REG_OFFSET);
728 uint32_t active_alerts = mmio_region_read32(
729 entropy_src->
base_addr, ENTROPY_SRC_RECOV_ALERT_STS_REG_OFFSET);
730 active_alerts &= ~alerts;
731 mmio_region_write32(entropy_src->
base_addr,
732 ENTROPY_SRC_RECOV_ALERT_STS_REG_OFFSET, active_alerts);
739 if (entropy_src == NULL || errors == NULL) {
743 uint32_t err_code_reg = mmio_region_read32(entropy_src->
base_addr,
744 ENTROPY_SRC_ERR_CODE_REG_OFFSET);
750 ENTROPY_SRC_ERR_CODE_SFIFO_ESRNG_ERR_BIT)) {
752 ENTROPY_SRC_ERR_CODE_FIFO_WRITE_ERR_BIT)) {
756 ENTROPY_SRC_ERR_CODE_FIFO_READ_ERR_BIT)) {
760 ENTROPY_SRC_ERR_CODE_FIFO_STATE_ERR_BIT)) {
767 ENTROPY_SRC_ERR_CODE_SFIFO_OBSERVE_ERR_BIT)) {
769 ENTROPY_SRC_ERR_CODE_FIFO_WRITE_ERR_BIT)) {
773 ENTROPY_SRC_ERR_CODE_FIFO_READ_ERR_BIT)) {
777 ENTROPY_SRC_ERR_CODE_FIFO_STATE_ERR_BIT)) {
784 ENTROPY_SRC_ERR_CODE_SFIFO_ESFINAL_ERR_BIT)) {
786 ENTROPY_SRC_ERR_CODE_FIFO_WRITE_ERR_BIT)) {
790 ENTROPY_SRC_ERR_CODE_FIFO_READ_ERR_BIT)) {
794 ENTROPY_SRC_ERR_CODE_FIFO_STATE_ERR_BIT)) {
801 ENTROPY_SRC_ERR_CODE_ES_ACK_SM_ERR_BIT)) {
805 ENTROPY_SRC_ERR_CODE_ES_MAIN_SM_ERR_BIT)) {
817 if (entropy_src == NULL) {
821 uint32_t err_code_reg = 0;
827 err_code_reg = ENTROPY_SRC_ERR_CODE_SFIFO_ESRNG_ERR_BIT;
832 err_code_reg = ENTROPY_SRC_ERR_CODE_SFIFO_OBSERVE_ERR_BIT;
837 err_code_reg = ENTROPY_SRC_ERR_CODE_SFIFO_ESFINAL_ERR_BIT;
840 err_code_reg = ENTROPY_SRC_ERR_CODE_ES_ACK_SM_ERR_BIT;
843 err_code_reg = ENTROPY_SRC_ERR_CODE_ES_MAIN_SM_ERR_BIT;
846 err_code_reg = ENTROPY_SRC_ERR_CODE_ES_CNTR_ERR_BIT;
852 mmio_region_write32(entropy_src->
base_addr,
853 ENTROPY_SRC_ERR_CODE_TEST_REG_OFFSET, err_code_reg);
860 if (entropy_src == NULL || state == NULL) {
864 *state = mmio_region_read32(entropy_src->
base_addr,
865 ENTROPY_SRC_MAIN_SM_STATE_REG_OFFSET);