util.c — Utilities and trace

Small runtime helpers: memory, time, register polling, the host write barrier and interrupt, and the trace buffer.

The firmware links without libc or libgcc, so this file also provides memset, memcpy and the 64-bit division helpers the compiler calls.

void *memset(void *d, int c, size_t n)

Fill memory with a byte.

Parameters:
  • d – Destination.

  • c – Byte value (only the low 8 bits are used).

  • n – Number of bytes.

Returns:

d.

void *memcpy(void *d, const void *s, size_t n)

Copy memory.

Copies 32-bit words when both pointers and the length are 4-byte aligned (uncached GPU memory and registers must not be read byte by byte when a word access is possible), bytes otherwise. The areas must not overlap.

Parameters:
  • d – Destination.

  • s – Source.

  • n – Number of bytes.

Returns:

d.

static uint64_t udivmod64(uint64_t n, uint64_t d, uint64_t *rem)

Unsigned 64-bit division by shift and subtract.

Parameters:
  • n – Dividend.

  • d – Divisor. Division by zero returns all ones and a zero remainder.

  • rem – Where to store the remainder, or NULL.

Returns:

The quotient.

uint64_t __udivdi3(uint64_t n, uint64_t d)

Compiler helper for unsigned 64-bit division (n / d).

Parameters:
  • n – Dividend.

  • d – Divisor.

Returns:

The quotient.

uint64_t __umoddi3(uint64_t n, uint64_t d)

Compiler helper for unsigned 64-bit remainder (n % d).

Parameters:
  • n – Dividend.

  • d – Divisor.

Returns:

The remainder.

uint64_t gpu_timer(void)

Read the GPU timer.

CR_TIMER is read as high, low, high again until both high halves agree, so that a carry between the two 32-bit reads is never seen.

Returns:

The 48-bit timer value (it counts at the core clock / 256).

void udelay(uint32_t us)

Busy-wait.

Counts CP0 Count ticks (core clock / 2), so the delay follows the current clock in g.

Parameters:
  • us – Delay in microseconds.

bool cr_poll32(uint32_t off, uint32_t mask, uint32_t value, uint32_t timeout_us)

Poll a GPU register until a masked value appears.

On timeout the register, mask and a counter are recorded in sysdata (cr_poll_*, read by the host’s debug dump) and a HWR_13 trace entry is written.

Parameters:
  • off – Register offset.

  • mask – Bits to compare.

  • value – Expected value of reg & mask.

  • timeout_us – Timeout in microseconds.

Returns:

true when (reg & mask) == value was seen, false on timeout.

void write_barrier(void)

Make every previous write visible to the host.

Used before publishing a CCB offset or raising an interrupt: writes the uncached barrier word, runs EHB and SYNC, reads the word back, then reads a register so that the bus is drained.

void notify_host(void)

Raise the firmware-to-host interrupt.

Increments osdata->interrupt_count[0] (the host uses it to recognise a new interrupt) between two write barriers, then sets the MIPS wrapper interrupt status.

void trace_init(void)

Locate the trace buffer.

Takes the address and size of thread 0’s buffer from the trace control structure. Tracing stays off when either is zero; a write pointer outside the buffer is reset to 0.

static uint32_t group_bit(uint32_t gid)

Map an SFID group number to its log-type group bit.

Parameters:
  • gid – Group number, bits 12..15 of the SFID.

Returns:

The ROGUE_FWIF_LOG_TYPE_GROUP_* bit that enables the group.

void trace_emit(uint32_t sfid, const uint32_t *params, uint32_t n)

Append one entry to the trace ring.

Nothing is written unless tracing is on (log_type bit 0) and the SFID’s group is enabled. The entry is the SFID, the 64-bit GPU timer (high word first) and the parameters; it may wrap around the end of the ring. Interrupts are masked while the entry is written. Normally called through the TRACE macro.

Parameters:
  • sfid – String format identifier (SFID_*).

  • params – The parameters.

  • n – Number of parameters; it must match the SFID’s format string.