2017-02-27 12:21:43 +00:00
|
|
|
/*
|
2018-02-12 12:36:17 +00:00
|
|
|
* Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
|
2017-02-27 12:21:43 +00:00
|
|
|
*
|
2017-05-03 09:38:09 +01:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
2017-02-27 12:21:43 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <assert.h>
|
2020-10-08 00:43:50 +01:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2017-02-27 12:21:43 +00:00
|
|
|
#include <string.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
|
|
|
#include <lib/mmio.h>
|
|
|
|
#include <lib/utils_def.h>
|
2019-01-15 14:19:50 +00:00
|
|
|
#include <platform_def.h>
|
2018-12-14 00:18:21 +00:00
|
|
|
|
2018-02-12 12:36:17 +00:00
|
|
|
#include "juno_decl.h"
|
2017-02-27 12:21:43 +00:00
|
|
|
|
|
|
|
#define NSAMPLE_CLOCKS 1 /* min 1 cycle, max 231 cycles */
|
|
|
|
#define NRETRIES 5
|
|
|
|
|
2020-10-08 00:43:50 +01:00
|
|
|
/* initialised to false */
|
|
|
|
static bool juno_trng_initialized;
|
|
|
|
|
|
|
|
static bool output_valid(void)
|
2017-02-27 12:21:43 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < NRETRIES; i++) {
|
|
|
|
uint32_t val;
|
|
|
|
|
|
|
|
val = mmio_read_32(TRNG_BASE + TRNG_STATUS);
|
|
|
|
if (val & 1U)
|
2020-10-08 00:43:50 +01:00
|
|
|
return true;
|
2017-02-27 12:21:43 +00:00
|
|
|
}
|
2020-10-08 00:43:50 +01:00
|
|
|
return false; /* No output data available. */
|
2017-02-27 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2020-10-08 00:43:50 +01:00
|
|
|
* This function fills `buf` with 8 bytes of entropy.
|
2017-02-27 12:21:43 +00:00
|
|
|
* It uses the Trusted Entropy Source peripheral on Juno.
|
2020-10-08 00:43:50 +01:00
|
|
|
* Returns 'true' when the buffer has been filled with entropy
|
|
|
|
* successfully, or 'false' otherwise.
|
2017-02-27 12:21:43 +00:00
|
|
|
*/
|
2020-10-08 00:43:50 +01:00
|
|
|
bool juno_getentropy(uint64_t *buf)
|
2017-02-27 12:21:43 +00:00
|
|
|
{
|
2020-10-08 00:43:50 +01:00
|
|
|
uint64_t ret;
|
2017-02-27 12:21:43 +00:00
|
|
|
|
|
|
|
assert(buf);
|
2020-10-08 00:43:50 +01:00
|
|
|
assert(!check_uptr_overflow((uintptr_t)buf, sizeof(*buf)));
|
|
|
|
|
|
|
|
if (!juno_trng_initialized) {
|
|
|
|
/* Disable interrupt mode. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0);
|
|
|
|
/* Program TRNG to sample for `NSAMPLE_CLOCKS`. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS);
|
|
|
|
/* Abort any potentially pending sampling. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_CONTROL, 2);
|
|
|
|
/* Reset TRNG outputs. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
|
2017-02-27 12:21:43 +00:00
|
|
|
|
2020-10-08 00:43:50 +01:00
|
|
|
juno_trng_initialized = true;
|
|
|
|
}
|
2017-02-27 12:21:43 +00:00
|
|
|
|
2020-10-08 00:43:50 +01:00
|
|
|
if (!output_valid()) {
|
2017-02-27 12:21:43 +00:00
|
|
|
/* Start TRNG. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
|
|
|
|
|
|
|
|
if (!output_valid())
|
2020-10-08 00:43:50 +01:00
|
|
|
return false;
|
2017-02-27 12:21:43 +00:00
|
|
|
}
|
|
|
|
|
2020-10-08 00:43:50 +01:00
|
|
|
/* XOR each two 32-bit registers together, combine the pairs */
|
|
|
|
ret = mmio_read_32(TRNG_BASE + 0);
|
|
|
|
ret ^= mmio_read_32(TRNG_BASE + 4);
|
|
|
|
ret <<= 32;
|
|
|
|
|
|
|
|
ret |= mmio_read_32(TRNG_BASE + 8);
|
|
|
|
ret ^= mmio_read_32(TRNG_BASE + 12);
|
|
|
|
*buf = ret;
|
|
|
|
|
|
|
|
/* Acknowledge current cycle, clear output registers. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
|
|
|
|
/* Trigger next TRNG cycle. */
|
|
|
|
mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
|
|
|
|
|
|
|
|
return true;
|
2017-02-27 12:21:43 +00:00
|
|
|
}
|