blob: 09552a608e71330990b214ae1010091184e5a7f9 [file] [log] [blame]
dp-arm8f59e152017-02-27 12:21:43 +00001/*
Roberto Vargas2b36b152018-02-12 12:36:17 +00002 * Copyright (c) 2017-2018, ARM Limited and Contributors. All rights reserved.
dp-arm8f59e152017-02-27 12:21:43 +00003 *
dp-armfa3cf0b2017-05-03 09:38:09 +01004 * SPDX-License-Identifier: BSD-3-Clause
dp-arm8f59e152017-02-27 12:21:43 +00005 */
6
Andre Przywarac7d10e32020-10-16 12:06:57 +01007#include <arm_acle.h>
dp-arm8f59e152017-02-27 12:21:43 +00008#include <assert.h>
Andre Przywara927b3992020-10-08 00:43:50 +01009#include <stdbool.h>
10#include <stdint.h>
dp-arm8f59e152017-02-27 12:21:43 +000011#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000012
13#include <lib/mmio.h>
14#include <lib/utils_def.h>
Antonio Nino Diaza320ecd2019-01-15 14:19:50 +000015#include <platform_def.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +000016
Andre Przywara31ed4702020-10-08 00:45:22 +010017#include <lib/smccc.h>
18#include <services/trng_svc.h>
19#include <smccc_helpers.h>
20
21#include <plat/common/platform.h>
dp-arm8f59e152017-02-27 12:21:43 +000022
23#define NSAMPLE_CLOCKS 1 /* min 1 cycle, max 231 cycles */
24#define NRETRIES 5
25
Andre Przywara927b3992020-10-08 00:43:50 +010026/* initialised to false */
27static bool juno_trng_initialized;
28
29static bool output_valid(void)
dp-arm8f59e152017-02-27 12:21:43 +000030{
31 int i;
32
33 for (i = 0; i < NRETRIES; i++) {
34 uint32_t val;
35
36 val = mmio_read_32(TRNG_BASE + TRNG_STATUS);
37 if (val & 1U)
Andre Przywara927b3992020-10-08 00:43:50 +010038 return true;
dp-arm8f59e152017-02-27 12:21:43 +000039 }
Andre Przywara927b3992020-10-08 00:43:50 +010040 return false; /* No output data available. */
dp-arm8f59e152017-02-27 12:21:43 +000041}
42
Andre Przywara31ed4702020-10-08 00:45:22 +010043DEFINE_SVC_UUID2(_plat_trng_uuid,
44 0x23523c58, 0x7448, 0x4083, 0x9d, 0x16,
45 0xe3, 0xfa, 0xb9, 0xf1, 0x73, 0xbc
46);
47uuid_t plat_trng_uuid;
48
Andre Przywarac7d10e32020-10-16 12:06:57 +010049static uint32_t crc_value = ~0U;
50
dp-arm8f59e152017-02-27 12:21:43 +000051/*
Andre Przywara31ed4702020-10-08 00:45:22 +010052 * Uses the Trusted Entropy Source peripheral on Juno to return 8 bytes of
53 * entropy. Returns 'true' when done successfully, 'false' otherwise.
dp-arm8f59e152017-02-27 12:21:43 +000054 */
Andre Przywara31ed4702020-10-08 00:45:22 +010055bool plat_get_entropy(uint64_t *out)
dp-arm8f59e152017-02-27 12:21:43 +000056{
Andre Przywara927b3992020-10-08 00:43:50 +010057 uint64_t ret;
dp-arm8f59e152017-02-27 12:21:43 +000058
Andre Przywara31ed4702020-10-08 00:45:22 +010059 assert(out);
60 assert(!check_uptr_overflow((uintptr_t)out, sizeof(*out)));
dp-arm8f59e152017-02-27 12:21:43 +000061
Andre Przywara927b3992020-10-08 00:43:50 +010062 if (!juno_trng_initialized) {
63 /* Disable interrupt mode. */
64 mmio_write_32(TRNG_BASE + TRNG_INTMASK, 0);
65 /* Program TRNG to sample for `NSAMPLE_CLOCKS`. */
66 mmio_write_32(TRNG_BASE + TRNG_CONFIG, NSAMPLE_CLOCKS);
67 /* Abort any potentially pending sampling. */
68 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 2);
69 /* Reset TRNG outputs. */
70 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
dp-arm8f59e152017-02-27 12:21:43 +000071
Andre Przywara927b3992020-10-08 00:43:50 +010072 juno_trng_initialized = true;
73 }
dp-arm8f59e152017-02-27 12:21:43 +000074
Andre Przywara927b3992020-10-08 00:43:50 +010075 if (!output_valid()) {
dp-arm8f59e152017-02-27 12:21:43 +000076 /* Start TRNG. */
77 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
78
dp-arm8f59e152017-02-27 12:21:43 +000079 if (!output_valid())
Andre Przywara927b3992020-10-08 00:43:50 +010080 return false;
81 }
dp-arm8f59e152017-02-27 12:21:43 +000082
Andre Przywarac7d10e32020-10-16 12:06:57 +010083 /* CRC each two 32-bit registers together, combine the pairs */
84 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 0));
85 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 4));
86 ret = (uint64_t)crc_value << 32;
dp-arm8f59e152017-02-27 12:21:43 +000087
Andre Przywarac7d10e32020-10-16 12:06:57 +010088 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 8));
89 crc_value = __crc32w(crc_value, mmio_read_32(TRNG_BASE + 12));
Andre Przywara31ed4702020-10-08 00:45:22 +010090 *out = ret | crc_value;
dp-arm8f59e152017-02-27 12:21:43 +000091
Andre Przywara927b3992020-10-08 00:43:50 +010092 /* Acknowledge current cycle, clear output registers. */
93 mmio_write_32(TRNG_BASE + TRNG_STATUS, 1);
94 /* Trigger next TRNG cycle. */
95 mmio_write_32(TRNG_BASE + TRNG_CONTROL, 1);
dp-arm8f59e152017-02-27 12:21:43 +000096
Andre Przywara927b3992020-10-08 00:43:50 +010097 return true;
dp-arm8f59e152017-02-27 12:21:43 +000098}
Andre Przywara31ed4702020-10-08 00:45:22 +010099
100void plat_entropy_setup(void)
101{
102 uint64_t dummy;
103
104 plat_trng_uuid = _plat_trng_uuid;
105
106 /* Initialise the entropy source and trigger RNG generation */
107 plat_get_entropy(&dummy);
108}