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 / core / console.c

240 lines · 6652 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/* console.c -- ck_puts/ck_printf. Before ExitBootServices the boot stub
18 * attaches UEFI ConOut; afterwards output goes to the UART the ACPI SPCR
19 * names (PL011/SBSA, or 16550 in MMIO). With neither, output is dropped and
20 * the panic path still resets. '\n' is sent as CR LF.
21 * After ExitBootServices every byte the UART gets is also drawn on the GOP
22 * framebuffer once the kernel turned it on (ck_console_set_fb, core/fbcon.c);
23 * a fault while drawing turns the screen off for good, the UART goes on. */
24#include "ck_internal.h"
25#include "fbcon.h"
26#include "arch.h"
27#include "uart_rx.h"
28#ifdef CK_KLOG
29#include "klog.h"
30#endif
31
32#ifndef CK_FB_HOLD_S
33#define CK_FB_HOLD_S 0
34#endif
35
36static void (*efi_write)(const char *s, size_t n);
37static volatile uint8_t *uart;
38static int uart_kind; /* 0 none, 1 pl011, 2 16550 */
39static unsigned uart_stride;
40static uint64_t uart_phys;
41static struct ck_fbcon fb;
42static int fb_on, fb_busy;
43
44void ck_console_set_efi(void (*write)(const char *s, size_t n))
45{
46 efi_write = write;
47}
48
49int ck_console_set_uart(const struct ck_spcr *s)
50{
51 int kind;
52 unsigned stride;
53 if (!s || s->space != 0 || !s->base)
54 return -1;
55 if (ck_uart_from_spcr(s->interface_type, s->access_size, &kind, &stride) != 0)
56 return -1;
57 uart_kind = kind;
58 uart_stride = stride;
59 uart_phys = s->base;
60 uart = (volatile uint8_t *)(uintptr_t)s->base;
61 return 0;
62}
63
64const char *ck_console_uart_name(void)
65{
66 return uart_kind == 1 ? "pl011" : uart_kind == 2 ? "16550" : "none";
67}
68
69uint64_t ck_console_uart_base(void)
70{
71 return uart_phys;
72}
73
74static void uart_putc(char c)
75{
76 if (uart_kind == 1) {
77 volatile uint32_t *fr = (volatile uint32_t *)(uart + 0x18);
78 for (unsigned i = 0; i < 1000000 && (*fr & (1u << 5)); i++)
79 ;
80 *(volatile uint32_t *)(uart + 0x00) = (uint8_t)c;
81 } else if (uart_kind == 2) {
82 /* Access width from the SPCR GAS (ck_16550_width): the Spark's 16550
83 * is dword-spaced and Linux drives it with 32-bit accesses only. */
84 unsigned w = ck_16550_width(uart_stride);
85 for (unsigned i = 0; i < 1000000 && !(ck_uart_rd(uart, CK_16550_LSR * uart_stride, w) & 0x20); i++)
86 ;
87 if (w == 4)
88 *(volatile uint32_t *)uart = (uint8_t)c;
89 else
90 *uart = (uint8_t)c;
91 }
92}
93
94static void write_n(const char *s, size_t n)
95{
96#ifdef CK_KLOG
97 ck_klog_put(s, n); /* RAM copy for the card (Pi 5 build: core/klog_slot.c) */
98#endif
99 if (efi_write) {
100 efi_write(s, n);
101 return;
102 }
103 if (fb_on) {
104 if (fb_busy) {
105 fb_on = 0; /* re-entered from a fault inside the drawing: screen off */
106 } else {
107 fb_busy = 1;
108 ck_fbcon_write(&fb, s, n);
109 fb_busy = 0;
110 }
111 }
112 if (!uart_kind)
113 return;
114 for (size_t i = 0; i < n; i++) {
115 if (s[i] == '\n')
116 uart_putc('\r');
117 uart_putc(s[i]);
118 }
119}
120
121/* The boot sequence owns the screen while it plays (core/boot_screen.c): the
122 * console stops drawing on it, then starts again on a cleared screen. */
123static int fb_paused;
124void ck_console_fb_pause(void)
125{
126 if (fb_on)
127 fb_paused = 1, fb_on = 0;
128}
129void ck_console_fb_resume(void)
130{
131 if (!fb_paused)
132 return;
133 fb_paused = 0;
134 ck_fbcon_clear(&fb);
135 fb_on = 1;
136}
137
138int ck_console_set_fb(volatile void *base, uint32_t width, uint32_t height, uint32_t pitch,
139 uint32_t format)
140{
141 if (ck_fbcon_setup(&fb, base, width, height, pitch, format) != 0)
142 return -1;
143 fb_on = 1;
144 return 0;
145}
146
147int ck_console_fb_info(uint32_t *scale, uint32_t *cols, uint32_t *rows)
148{
149 if (!fb_on)
150 return -1;
151 *scale = fb.scale;
152 *cols = fb.cols;
153 *rows = fb.rows;
154 return 0;
155}
156
157/* Before a reset: keep the screen readable for CK_FB_HOLD_S seconds (0 in
158 * every QEMU build; the hardware staging build sets it so an operator can
159 * read or photograph the final report before the machine resets). */
160void ck_console_fb_hold(void)
161{
162 if (!fb_on || CK_FB_HOLD_S <= 0)
163 return;
164 uint64_t f = ck_rd(cntfrq_el0);
165 if (!f)
166 return;
167 ck_printf("screen: holding %u s before reset\n", (unsigned)CK_FB_HOLD_S);
168 uint64_t t0 = ck_rd(cntpct_el0);
169 while (ck_rd(cntpct_el0) - t0 < (uint64_t)CK_FB_HOLD_S * f)
170 __asm__ volatile("yield");
171}
172
173void ck_puts(const char *s)
174{
175 write_n(s, strlen(s));
176}
177
178struct pbuf {
179 char b[128];
180 size_t n;
181};
182
183static void pbuf_emit(void *ctx, char c)
184{
185 struct pbuf *p = ctx;
186 p->b[p->n++] = c;
187 if (p->n == sizeof p->b) {
188 write_n(p->b, p->n);
189 p->n = 0;
190 }
191}
192
193void ck_vprintf(const char *fmt, va_list ap)
194{
195 struct pbuf p;
196 p.n = 0;
197 ck_vformat(pbuf_emit, &p, fmt, ap);
198 if (p.n)
199 write_n(p.b, p.n);
200}
201
202void ck_printf(const char *fmt, ...)
203{
204 va_list ap;
205 va_start(ap, fmt);
206 ck_vprintf(fmt, ap);
207 va_end(ap);
208}
209
210/* ---- console receive, POLLED (operator input; see include/ck.h) ----
211 * The receive side uses the UART the transmit side already chose from the
212 * ACPI SPCR (ck_console_set_uart): PL011/SBSA (QEMU virt) or 16550 (the DGX
213 * Spark, SPCR type 0x01 at 0x16A00000). Register decode, readiness and the
214 * error rules live in core/uart_rx.h, shared with the host test. No
215 * interrupt is used: the lifecycle polls between its steps. The kernel never
216 * writes a UART control register (baud, FIFO, enables stay as the firmware
217 * left them). */
218static struct ck_uart_rx_count rx_count;
219
220int ck_console_rx_ready(uint32_t *cr)
221{
222 return ck_uart_rx_ready(uart_kind, uart, uart_stride, cr);
223}
224
225int ck_console_rx_poll(void)
226{
227 if (uart_kind != CK_UART_PL011 && uart_kind != CK_UART_16550)
228 return CK_UART_RX_EMPTY;
229 return ck_uart_rx_take(uart_kind, uart, uart_stride, &rx_count);
230}
231
232unsigned ck_console_rx_errors(void)
233{
234 return rx_count.errors;
235}
236
237unsigned ck_console_rx_overruns(void)
238{
239 return rx_count.overruns;
240}