aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / native / kernel / dev / pi5_rng.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* pi5_rng.c -- see pi5_rng.h. The logic is hosted-testable; the MMIO glue at |
| 18 | * the bottom is Pi 5 only (CK_PI5). */ |
| 19 | #include "pi5_rng.h" |
| 20 | |
| 21 | static uint32_t fifo_words(struct ck_rng200 *r) |
| 22 | { |
| 23 | return r->io.rd(r->io.ctx, CK_RNG200_FIFO_COUNT) & CK_RNG200_FIFO_COUNT_MASK; |
| 24 | } |
| 25 | |
| 26 | void ck_rng200_init(struct ck_rng200 *r, const struct ck_rng200_io *io) |
| 27 | { |
| 28 | r->io = *io; |
| 29 | r->refused = 0; |
| 30 | r->resets = 0; |
| 31 | r->up = 0; |
| 32 | if (!(r->io.rd(r->io.ctx, CK_RNG200_CTRL) & CK_RNG200_CTRL_ENABLE_MASK)) { |
| 33 | r->io.wr(r->io.ctx, CK_RNG200_BITCOUNT_THRESH, CK_RNG200_THRESHOLD_INIT); |
| 34 | r->io.wr(r->io.ctx, CK_RNG200_FIFO_COUNT, 2u << CK_RNG200_FIFO_THRESH_SHIFT); |
| 35 | r->io.wr(r->io.ctx, CK_RNG200_CTRL, (3u << CK_RNG200_CTRL_DIV_SHIFT) | CK_RNG200_CTRL_ENABLE); |
| 36 | } |
| 37 | r->up = (r->io.rd(r->io.ctx, CK_RNG200_CTRL) & CK_RNG200_CTRL_ENABLE_MASK) != 0; |
| 38 | } |
| 39 | |
| 40 | int ck_rng200_present(void *ctx) |
| 41 | { |
| 42 | struct ck_rng200 *r = ctx; |
| 43 | return r->up && !r->refused; |
| 44 | } |
| 45 | |
| 46 | /* Linux iproc_rng200_restart: disable, clear status, pulse RBG and RNG reset, enable. */ |
| 47 | static void restart(struct ck_rng200 *r) |
| 48 | { |
| 49 | void *c = r->io.ctx; |
| 50 | uint32_t v = r->io.rd(c, CK_RNG200_CTRL) & ~CK_RNG200_CTRL_ENABLE_MASK; |
| 51 | r->io.wr(c, CK_RNG200_CTRL, v); |
| 52 | r->io.wr(c, CK_RNG200_INT_STATUS, 0xffffffffu); |
| 53 | r->io.wr(c, CK_RNG200_RBG_SOFT_RESET, r->io.rd(c, CK_RNG200_RBG_SOFT_RESET) | 1u); |
| 54 | r->io.wr(c, CK_RNG200_SOFT_RESET, r->io.rd(c, CK_RNG200_SOFT_RESET) | 1u); |
| 55 | r->io.wr(c, CK_RNG200_SOFT_RESET, r->io.rd(c, CK_RNG200_SOFT_RESET) & ~1u); |
| 56 | r->io.wr(c, CK_RNG200_RBG_SOFT_RESET, r->io.rd(c, CK_RNG200_RBG_SOFT_RESET) & ~1u); |
| 57 | r->io.wr(c, CK_RNG200_CTRL, (r->io.rd(c, CK_RNG200_CTRL) & ~CK_RNG200_CTRL_ENABLE_MASK) | CK_RNG200_CTRL_ENABLE); |
| 58 | r->resets++; |
| 59 | } |
| 60 | |
| 61 | /* 1 = healthy; 0 = failed even after the one allowed restart (latch refusal). */ |
| 62 | static int healthy(struct ck_rng200 *r) |
| 63 | { |
| 64 | uint32_t bad = CK_RNG200_INT_LOCKOUT | CK_RNG200_INT_NIST_FAIL; |
| 65 | if (r->io.rd(r->io.ctx, CK_RNG200_INT_STATUS) & bad) { |
| 66 | restart(r); |
| 67 | if (r->io.rd(r->io.ctx, CK_RNG200_INT_STATUS) & bad) { |
| 68 | r->refused = 1; |
| 69 | return 0; |
| 70 | } |
| 71 | } |
| 72 | return 1; |
| 73 | } |
| 74 | |
| 75 | static int word32(struct ck_rng200 *r, uint32_t *out) |
| 76 | { |
| 77 | uint32_t i; |
| 78 | if (!healthy(r)) |
| 79 | return 0; |
| 80 | for (i = 0; i < CK_RNG200_SPINS; i++) |
| 81 | if (r->io.rd(r->io.ctx, CK_RNG200_BITCOUNT) > CK_RNG200_BITCOUNT_WARM) |
| 82 | break; |
| 83 | if (i == CK_RNG200_SPINS) |
| 84 | return 0; |
| 85 | for (i = 0; i < CK_RNG200_SPINS; i++) |
| 86 | if (fifo_words(r)) |
| 87 | break; |
| 88 | if (i == CK_RNG200_SPINS) |
| 89 | return 0; |
| 90 | *out = r->io.rd(r->io.ctx, CK_RNG200_FIFO_DATA); |
| 91 | return 1; |
| 92 | } |
| 93 | |
| 94 | int ck_rng200_read(void *ctx, uint64_t *out) |
| 95 | { |
| 96 | struct ck_rng200 *r = ctx; |
| 97 | uint32_t a, b; |
| 98 | if (!r->up || r->refused) |
| 99 | return 0; |
| 100 | if (!word32(r, &a) || !word32(r, &b)) |
| 101 | return 0; |
| 102 | *out = ((uint64_t)a << 32) | b; |
| 103 | return 1; |
| 104 | } |
| 105 | |
| 106 | #ifdef CK_PI5 |
| 107 | #include "ck.h" |
| 108 | |
| 109 | static volatile uint8_t *regs; |
| 110 | static struct ck_rng200 dev; |
| 111 | |
| 112 | static uint32_t mm_rd(void *c, uint32_t off) { (void)c; return *(volatile uint32_t *)(regs + off); } |
| 113 | static void mm_wr(void *c, uint32_t off, uint32_t v) { (void)c; *(volatile uint32_t *)(regs + off) = v; } |
| 114 | |
| 115 | static int mm_present(void *c) |
| 116 | { |
| 117 | return regs && ck_rng200_present(c); |
| 118 | } |
| 119 | |
| 120 | static const struct ck_rng_ops pi_ops = {mm_present, ck_rng200_read, &dev}; |
| 121 | |
| 122 | const struct ck_rng_ops *ck_pi5_rng_ops(void) |
| 123 | { |
| 124 | static const struct ck_rng200_io io = {mm_rd, mm_wr, 0}; |
| 125 | if (!regs) { |
| 126 | regs = ck_mmio_map(CK_PI5_RNG_PHYS, 0x1000); |
| 127 | if (regs) |
| 128 | ck_rng200_init(&dev, &io); |
| 129 | } |
| 130 | return &pi_ops; |
| 131 | } |
| 132 | #endif |