ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
cpu1_main.c
Go to the documentation of this file.
1
49
50#include <stdint.h>
51#include <string.h>
52
53#include "book.h"
54#include "compile_on_m33.h"
55#include "epub.h"
56#include "ra8_attributes.h"
57#include "ra8_err.h"
58#include "ra8_ipc.h"
59#include "ra8_rabook_pipeline.h"
60#include "rabook_compile.h"
61
63extern uint32_t g_ra8_ls_cpu1_stack_top;
65extern uint32_t g_ra8_ls_cpu1_data_start;
67extern uint32_t g_ra8_ls_cpu1_data_end;
69extern uint32_t g_ra8_ls_cpu1_data_load;
71extern uint32_t g_ra8_ls_cpu1_bss_start;
73extern uint32_t g_ra8_ls_cpu1_bss_end;
74
75[[noreturn]] void cpu1_reset_handler(void);
76
88typedef enum : uint32_t {
94 k_arena_string = 8U * 1024U,
95 k_arena_imgpool = 64U * 1024U,
96 k_arena_xhtml = 16U * 1024U,
97 k_arena_imgraw = 16U * 1024U,
98 k_arena_css = 16U * 1024U,
100
101/* The full compile working set lives in external SDRAM (.sdram_bss, NOLOAD):
102 * epub_book_t alone is ~65 KiB and the builder/scratch arenas push well past
103 * the 64 KiB SRAM_CPU1. The M85 brings up the SDRAM controller before releasing
104 * the M33. NOLOAD needs no startup zeroing -- the builder appends into its
105 * arenas before reading, the EPUB scratch is filled before each read, and
106 * s_book is memset in compile_fixture before epub_open. The finalized blob
107 * goes to the shared SRAM2 output buffer (com33_blob), not here. */
108[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static epub_book_t s_book;
109[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static book_chapter_t s_chapters[k_arena_chapters];
110[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static book_node_t s_nodes[k_arena_nodes];
111[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static book_attr_t s_attrs[k_arena_attrs];
112[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static book_stylesheet_t s_styles[k_arena_styles];
113[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static book_image_t s_images[k_arena_images];
114[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static char s_string_pool[k_arena_string];
115[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static uint8_t s_image_pool[k_arena_imgpool];
116[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static uint8_t s_xhtml[k_arena_xhtml];
117[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static ra8_rabook_xml_workspace_t s_xml_workspace;
118[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static uint8_t s_image_raw[k_arena_imgraw];
119[[gnu::section(".sdram_bss"), gnu::aligned(8)]] static char s_css[k_arena_css];
120
142{
143 if (buf == nullptr) {
144 return false;
145 }
146 if (scr == nullptr) {
147 return false;
148 }
149 uint8_t* out = com33_blob();
150 if (out == nullptr) {
151 return false;
152 }
153 *buf = (ra8_rabook_buffers_t){
154 .chapters = s_chapters,
155 .nodes = s_nodes,
156 .attrs = s_attrs,
157 .stylesheets = s_styles,
158 .images = s_images,
159 .string_pool = s_string_pool,
160 .image_pool = s_image_pool,
161 .out = out,
162 .chapter_cap = (uint32_t)k_arena_chapters,
163 .node_cap = (uint32_t)k_arena_nodes,
164 .attr_cap = (uint32_t)k_arena_attrs,
165 .stylesheet_cap = (uint32_t)k_arena_styles,
166 .image_cap = (uint32_t)k_arena_images,
167 .string_cap = (uint32_t)k_arena_string,
168 .image_pool_cap = (uint32_t)k_arena_imgpool,
169 .out_cap = (uint32_t)k_com33_blob_cap,
170 };
172 .xhtml = s_xhtml,
173 .xhtml_cap = sizeof(s_xhtml),
174 .image_raw = s_image_raw,
175 .image_cap = sizeof(s_image_raw),
176 .img_arena = nullptr,
177 .gray = nullptr,
178 .gray_cap = 0U,
179 .css = s_css,
180 .css_cap = sizeof(s_css),
181 .xml_workspace = &s_xml_workspace,
182 };
183 return true;
184}
185
213static bool compile_fixture(const void** out_blob, uint32_t* out_len)
214{
215 if (out_blob == nullptr) {
216 return false;
217 }
218 if (out_len == nullptr) {
219 return false;
220 }
221 /* The M85 staged the source .epub into shared SRAM and posted its base/len in
222 * the request; the M33 compiles whatever was staged (not a baked-in fixture),
223 * so the same image serves any book the M85 dispatches. */
224 const volatile com33_mailbox_t* mb = com33_mailbox();
225 memset(&s_book, 0, sizeof(s_book));
226 const epub_mem_media_t media = {
227 .data = (const uint8_t*)(uintptr_t)mb->epub_base,
228 .size = (size_t)mb->epub_len,
229 };
230 if (epub_open(&media, "rabook.epub", &s_book) != k_ra8_ok) {
231 return false;
232 }
233
234 ra8_rabook_buffers_t buf = {};
236 if (!bind_compile(&buf, &scr)) {
237 (void)epub_close(&s_book);
238 return false;
239 }
240
241 const ra8_err_t rc = rabook_compile_from_epub_to_buffer(&s_book, &buf, &scr, out_blob, out_len);
242 (void)epub_close(&s_book);
243 return rc == k_ra8_ok;
244}
245
270static void publish_result(volatile com33_mailbox_t* mb, const void* blob, uint32_t len, bool ok)
271{
272 if (mb == nullptr) {
273 return;
274 }
275 bool good = ok;
276 if (blob == nullptr) {
277 good = false;
278 }
279 if (len == 0U) {
280 good = false;
281 }
282 if (good) {
283 const book_header_t* hdr = book_header(blob);
284 mb->blob_base = (uint32_t)(uintptr_t)blob;
285 mb->blob_len = len;
286 mb->blob_crc = hdr->crc32_val;
287 mb->chapter_count = hdr->chapter_count;
288 __asm volatile("dsb" ::: "memory");
289 mb->status = (uint32_t)k_com33_status_ok;
290 } else {
291 mb->status = (uint32_t)k_com33_status_build_fail;
292 }
293 __asm volatile("dsb" ::: "memory");
294 mb->done = 1U;
295}
296
311[[noreturn]] static void cpu1_park(void)
312{
313 while (1) {
314 __asm volatile("nop");
315 }
316}
317
326typedef enum : uint8_t {
328} cpu1_ipc_t;
329
350static void notify_m85(void)
351{
353}
354
373[[noreturn]] static void cpu1_run_compile(void)
374{
375 volatile com33_mailbox_t* mb = com33_mailbox();
376 mb->m33_sig = (uint32_t)k_com33_m33_sig;
377 __asm volatile("dsb" ::: "memory");
378
379 /* The M85 stages the .epub + buffers and posts req_magic BEFORE releasing this
380 * core, so the job is ready on the first read; the dmb orders that read after
381 * observing the boot handshake. A missing request is reported, not compiled. */
382 __asm volatile("dmb" ::: "memory");
383 if (mb->req_magic != (uint32_t)k_com33_req_magic) {
384 mb->status = (uint32_t)k_com33_status_no_request;
385 __asm volatile("dsb" ::: "memory");
386 mb->done = 1U;
387 __asm volatile("dsb" ::: "memory");
388 notify_m85();
389 cpu1_park();
390 }
391
392 const void* blob = nullptr;
393 uint32_t len = 0U;
394 const bool ok = compile_fixture(&blob, &len);
395
396 publish_result(mb, blob, len, ok);
397 notify_m85();
398 cpu1_park();
399}
400
419[[noreturn]] void cpu1_reset_handler(void)
420{
421 uint32_t* dst = &g_ra8_ls_cpu1_data_start;
422 uint32_t* src = &g_ra8_ls_cpu1_data_load;
424 while (dst < &g_ra8_ls_cpu1_data_end) {
425 *dst = *src;
426 dst++;
427 src++;
428 }
429
430 uint32_t* bss = &g_ra8_ls_cpu1_bss_start;
432 while (bss < &g_ra8_ls_cpu1_bss_end) {
433 *bss = 0U;
434 bss++;
435 }
436
438}
439
463[[noreturn]] static void cpu1_fault_handler(void)
464{
465 volatile com33_mailbox_t* mb = com33_mailbox();
466 mb->status = (uint32_t)k_com33_status_fault;
467 __asm volatile("dsb" ::: "memory");
468 mb->done = 1U;
469 __asm volatile("dsb" ::: "memory");
470 notify_m85();
471 while (1) {
472 __asm volatile("nop");
473 }
474}
475
486#ifndef RA8_OFF_TARGET
487/* The vector table is only meaningful in the cross-compiled M33 image. The host
488 * unit-test build compile-checks this TU but never links it as an executable, so
489 * dropping the table there costs no coverage. */
490[[gnu::used, gnu::section(".cpu1_vectors")]] const uintptr_t g_cpu1_vector_table[] = {
491 (uintptr_t)&g_ra8_ls_cpu1_stack_top,
492 (uintptr_t)&cpu1_reset_handler,
493 (uintptr_t)&cpu1_fault_handler,
494 (uintptr_t)&cpu1_fault_handler,
495 (uintptr_t)&cpu1_fault_handler,
496 (uintptr_t)&cpu1_fault_handler,
497 (uintptr_t)&cpu1_fault_handler,
498 (uintptr_t)&cpu1_fault_handler,
499};
500#endif
Flat, execute-in-place container for a build-time "compiled" e-book.
static const book_header_t * book_header(const void *base)
View the blob base as its header.
Definition book.h:382
Shared-SRAM contract for the "RABOOK1 emitter on the M33" demo (#149b).
static volatile com33_mailbox_t * com33_mailbox(void)
Typed pointer to the fixed-address shared mailbox.
static uint8_t * com33_blob(void)
Typed pointer to the fixed-address shared output-blob buffer.
@ k_com33_status_no_request
status: req_magic absent (no staged job).
@ k_com33_status_fault
status: M33 took a hardware fault mid-run.
@ k_com33_req_magic
"REQ0" – M85 stamps it once the job (the
@ k_com33_status_ok
status: compile finalized a valid blob.
@ k_com33_m33_sig
"M33 CODE" boot sentinel written by M33.
@ k_com33_status_build_fail
status: a compile stage overflowed/failed.
@ k_com33_blob_cap
Shared output-blob buffer capacity in bytes.
EPUB (.epub) reader and chapter iterator for ra8-firmware.
ra8_err_t epub_close(epub_book_t *book)
Close a previously opened EPUB book.
Definition epub_open.c:507
ra8_err_t epub_open(const void *media, const char *path, epub_book_t *out_book)
Open an EPUB book from an opaque media handle.
Definition epub_open.c:405
static epub_book_t s_book
Opened book (large – file-scope, not on the stack).
Definition main.c:132
void cpu1_reset_handler(void)
CPU1 reset handler.
Definition cpu1_main.c:266
uint32_t g_ra8_ls_cpu1_data_end
uint32_t g_ra8_ls_cpu1_bss_start
uint32_t g_ra8_ls_cpu1_data_load
uint32_t g_ra8_ls_cpu1_bss_end
const uintptr_t g_cpu1_vector_table[]
Minimal Armv8-M vector table for CPU1.
Definition cpu1_main.c:337
uint32_t g_ra8_ls_cpu1_stack_top
uint32_t g_ra8_ls_cpu1_data_start
static void publish_result(volatile com33_mailbox_t *mb, const void *blob, uint32_t len, bool ok)
Publish the emitter result into the shared mailbox.
Definition cpu1_main.c:270
static uint8_t s_image_pool[k_arena_imgpool]
Definition cpu1_main.c:115
static bool compile_fixture(const void **out_blob, uint32_t *out_len)
Compile the baked parity fixture EPUB into the shared output blob.
Definition cpu1_main.c:213
m33_arena_cap_t
Capacities of the M33 text/CSS/SVG compile's caller-owned arenas.
Definition cpu1_main.c:88
@ k_arena_imgpool
Image-pool capacity (bytes).
Definition cpu1_main.c:95
@ k_arena_xhtml
Chapter XHTML parse scratch (bytes).
Definition cpu1_main.c:96
@ k_arena_images
Image-table entries.
Definition cpu1_main.c:93
@ k_arena_attrs
Attribute-table entries.
Definition cpu1_main.c:91
@ k_arena_string
String-pool capacity (bytes).
Definition cpu1_main.c:94
@ k_arena_styles
Stylesheet-table entries.
Definition cpu1_main.c:92
@ k_arena_chapters
Chapter-table entries.
Definition cpu1_main.c:89
@ k_arena_css
Stylesheet load scratch (bytes).
Definition cpu1_main.c:98
@ k_arena_imgraw
SVG/resource load scratch (bytes).
Definition cpu1_main.c:97
@ k_arena_nodes
Node-table entries.
Definition cpu1_main.c:90
static book_node_t s_nodes[k_arena_nodes]
Definition cpu1_main.c:110
static void notify_m85(void)
Poke the M85's IPC0 receive line to wake it from WFI.
Definition cpu1_main.c:350
cpu1_ipc_t
IPC channel the M33 pokes to wake the parked M85.
Definition cpu1_main.c:326
@ k_cpu1_ipc_wake_channel
IPC0 channel 0 (CPU1 -> CPU0).
Definition cpu1_main.c:327
static void cpu1_park(void)
Park the M33 forever once the emitter has reported its result.
Definition cpu1_main.c:311
static uint8_t s_image_raw[k_arena_imgraw]
Definition cpu1_main.c:118
static char s_css[k_arena_css]
Definition cpu1_main.c:119
static book_stylesheet_t s_styles[k_arena_styles]
Definition cpu1_main.c:112
static book_image_t s_images[k_arena_images]
Definition cpu1_main.c:113
static void cpu1_run_compile(void)
CPU1 emitter entry: build the book, finalize the blob, report.
Definition cpu1_main.c:373
static char s_string_pool[k_arena_string]
Definition cpu1_main.c:114
static bool bind_compile(ra8_rabook_buffers_t *buf, ra8_rabook_pipeline_scratch_t *scr)
Point a builder + pipeline-scratch view at the SDRAM arenas + output.
Definition cpu1_main.c:141
static ra8_rabook_xml_workspace_t s_xml_workspace
Definition cpu1_main.c:117
static book_chapter_t s_chapters[k_arena_chapters]
Definition cpu1_main.c:109
static book_attr_t s_attrs[k_arena_attrs]
Definition cpu1_main.c:111
Annotation-attribute framework macros for ra8-firmware.
#define RA8_LOOP_BOUND_RUNTIME(ceiling_ref)
NASA Power-of-10 Rule 2: bind ONE loop to a runtime / linker ceiling.
Error Code Definitions for ra8-firmware.
@ k_ra8_ok
Success – operation completed with all postconditions satisfied.
Definition ra8_err.h:119
ra8_err_codes_t ra8_err_t
Canonical error-return type used by every ra8-firmware API.
Definition ra8_err.h:546
void * memset(void *dst, int value, size_t n)
Fill memory with a constant byte value.
Inter-Processor Communication (IPC) HAL driver – public API.
@ k_ra8_ipc_irq_event_0
RA8 ipc IRQ event 0.
ra8_err_t ra8_ipc_send_event(uint8_t channel, ra8_ipc_irq_event_id_t event_id)
Generate a maskable IRQ event on the peer core.
Definition ra8_ipc.c:355
End-to-end EPUB -> RABOOK1 compile pipeline (#149).
ra8_err_t rabook_compile_from_epub_to_buffer(epub_book_t *epub, const ra8_rabook_buffers_t *bufs, const ra8_rabook_pipeline_scratch_t *scr, const void **out_blob, uint32_t *out_len)
Compile an open EPUB into a RABOOK1 blob left in bufs->out, with no filesystem write.
Zero-heap builder that emits a RABOOK1 blob (the #149 compiler back-end).
static uint8_t s_xhtml[k_sh_xhtml_cap]
Definition sh_book.c:55
One name="value" attribute on an element.
Definition book.h:317
One spine document (a renderable chapter) plus its TOC label.
Definition book.h:280
Fixed 100-byte prologue describing every table and pool in the blob.
Definition book.h:246
uint32_t crc32_val
CRC-32/ISO-HDLC of the body (all bytes after this header).
Definition book.h:270
uint32_t chapter_count
Number of spine chapters.
Definition book.h:256
Descriptor for one transcoded image in the image pool.
Definition book.h:354
One DOM node.
Definition book.h:299
A preserved CSS stylesheet and the chapter it scopes to.
Definition book.h:332
Cross-core handoff block backed by a fixed shared-SRAM address.
volatile uint32_t blob_base
Address of the finalized blob (= out_base).
volatile uint32_t chapter_count
Chapters the M33 emitted into the blob.
volatile uint32_t blob_len
Finalized RABOOK1 blob length, bytes.
volatile uint32_t m33_sig
M33 stamps k_com33_m33_sig on boot.
volatile uint32_t req_magic
M85 stamps k_com33_req_magic when the job is.
volatile uint32_t blob_crc
Blob header body CRC-32, echoed for cross-check.
volatile uint32_t epub_len
M85: length of the staged .epub, bytes.
volatile uint32_t done
Set to 1 by the M33 once the blob is published.
volatile uint32_t epub_base
M85: address of the staged source .epub bytes.
volatile uint32_t status
Compile outcome (com33_const_t status codes).
Opened EPUB book.
Definition epub.h:286
In-memory EPUB media descriptor.
Definition epub.h:179
Caller-owned, fixed-capacity arenas the builder appends into (no heap).
Caller-owned temporary buffers the pipeline stage needs.
Caller-owned storage for one parser invocation.