interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / native / kernel / dev / pi5_rng.h

88 lines · 3486 bytes Raw Blame History
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.h -- Raspberry Pi 5 (BCM2712) RNG200 hardware random source, polled.
18 * Plugs into core/entropy.c as a struct ck_rng_ops. No fallback: when the
19 * block does not answer, is locked out, or stays empty, read() returns 0 and
20 * the strict rules in entropy.c refuse the source.
21 *
22 * Register map and sequences: Linux drivers/char/hw_random/iproc-rng200.c
23 * (raspberrypi/linux rpi-6.12.y): offsets lines 21-48, bcm2711_rng200_init
24 * lines 206-225, bcm2711_rng200_read lines 169-204 (warm-up wait on the total
25 * bit count, FIFO count in the low 8 bits), fail check and one restart per
26 * read lines 105-122, restart sequence lines 70-97.
27 * Address: Pi firmware ACPI DSDT \_SB.RNG0 (HID RPI1014) 0x107d208000 len 0x28.
28 * The block's design is not public: output quality is trust-the-vendor, the
29 * same standing as Arm RNDR. */
30#ifndef AIENOS_CK_PI5_RNG_H
31#define AIENOS_CK_PI5_RNG_H
32#include <stdint.h>
33#include "entropy.h"
34
35#define CK_PI5_RNG_PHYS 0x107d208000ull
36#define CK_PI5_RNG_BYTES 0x28u
37
38#define CK_RNG200_CTRL 0x00u
39#define CK_RNG200_SOFT_RESET 0x04u
40#define CK_RNG200_RBG_SOFT_RESET 0x08u
41#define CK_RNG200_BITCOUNT 0x0cu
42#define CK_RNG200_BITCOUNT_THRESH 0x10u
43#define CK_RNG200_INT_STATUS 0x18u
44#define CK_RNG200_FIFO_DATA 0x20u
45#define CK_RNG200_FIFO_COUNT 0x24u
46
47#define CK_RNG200_CTRL_ENABLE_MASK 0x00001fffu
48#define CK_RNG200_CTRL_ENABLE 0x1u
49#define CK_RNG200_CTRL_DIV_SHIFT 13
50#define CK_RNG200_INT_LOCKOUT 0x80000000u
51#define CK_RNG200_INT_NIST_FAIL 0x00000020u
52#define CK_RNG200_FIFO_COUNT_MASK 0xffu
53#define CK_RNG200_FIFO_THRESH_SHIFT 8
54#define CK_RNG200_BITCOUNT_WARM 16u /* Linux waits for count > 16 */
55#define CK_RNG200_THRESHOLD_INIT 0x40000u
56
57/* Upper bound on register polls for one wait (warm-up or FIFO fill). */
58#ifndef CK_RNG200_SPINS
59#define CK_RNG200_SPINS 2000000u
60#endif
61
62/* Register access, so a host test can stand a mock block behind it. */
63struct ck_rng200_io {
64 uint32_t (*rd)(void *ctx, uint32_t off);
65 void (*wr)(void *ctx, uint32_t off, uint32_t v);
66 void *ctx;
67};
68
69struct ck_rng200 {
70 struct ck_rng200_io io;
71 int up; /* init found the block enabled */
72 int refused; /* latched: lockout/NIST fail twice, or no data */
73 int resets; /* soft resets done (at most one per read) */
74};
75
76/* Linux init: leave an already-enabled block alone, else threshold, FIFO
77 * threshold, enable at 1 MHz. up = block enabled afterwards. */
78void ck_rng200_init(struct ck_rng200 *r, const struct ck_rng200_io *io);
79int ck_rng200_present(void *ctx);
80/* One 64-bit word from two FIFO words. 1 = valid; 0 = failure (never fabricated). */
81int ck_rng200_read(void *ctx, uint64_t *out);
82
83#ifdef CK_PI5
84/* Maps the block, runs init; returns the ops for ck_rng_probe (present()
85 * reports 0 when the map failed). */
86const struct ck_rng_ops *ck_pi5_rng_ops(void);
87#endif
88#endif