main.c — Entry point and interrupt dispatch

Firmware initialisation and interrupt dispatch.

After boot the firmware has no main loop of its own: it sleeps in WAIT and every piece of work runs from one of three interrupt sources (timer, MTS background task, MTS interrupt task). As a safety net the timer tick also polls the Kernel CCB, so a missed MTS wake-up only costs latency.

struct fw_state g

The firmware’s global state (see fw_state).

TICK_MS

Timer period in milliseconds, used as a polling backstop.

static void timer_arm(void)

Program CP0 Compare for the next timer tick, TICK_MS from now.

static void clocks_update(uint32_t hz)

Derive the clock-dependent rates from the core clock.

Sets core_clock_hz, the GPU timer ticks per millisecond (clock / 256) and the CP0 Count ticks per millisecond (clock / 2). Neither rate is ever left at zero.

Parameters:
  • hz – Core clock in Hz; 0 selects 307.2 MHz.

static void shared_init(void)

Find the host-shared structures.

Reads sysinit and osinit at their fixed addresses and takes from them the addresses of sysdata, osdata, the runtime configuration, the trace control and both CCBs. Also stores the remap word for the fault page (used by the TLB refill handler), sets the clocks from the runtime configuration (or the initial clock) and reports the hardware as healthy.

void fw_main(void)

Firmware entry point, called by boot.S once the stack and caches are up.

Initialises the shared structures, the trace and statistics, powers the GPU on, initialises the units, the scheduler and the kick state, enables the host interrupt, reports firmware_started to the host and unmasks the three interrupt lines (HW0 timer, HW1 MTS background task, HW2 MTS interrupt task). It then handles any KCCB command queued before it was ready and sleeps in WAIT forever; all later work runs from irq_dispatch().

Boot-stage words 4, 5 and 6 (and the 0xC0DE000n debug register) mark its progress.

static void mts_task_end(uint32_t v)

Tell the MTS that the current task has finished.

Parameters:
  • v – The task type: 0 for the background task, 2 for the interrupt task.

void irq_dispatch(uint32_t source)

Handle one interrupt, called by the exception vector in vectors.S.

  • Timer: re-arms the tick, then (unless powered off) processes the KCCB, polls the scheduler events, runs the HWR watchdog and the scheduler.

  • MTS background task: processes the KCCB, polls the events and runs the scheduler, then ends the task.

  • MTS interrupt task: reads and clears the GPU event status, passes the events to sched_irq() and ends the task.

Each source also updates its debug counters.

Parameters:
  • source – 0 timer, 1 MTS background task, 2 MTS interrupt task; anything else counts as spurious.

void fatal_exception(void)

Report an unexpected exception, called by vectors.S.

The vector has saved Cause, EPC and BadVAddr in the boot data page. Sets HWR_FW_FAULT and counts the fault in sysdata, writes a trace entry (0xDEAD00xx with the exception code) and interrupts the host.