mmu.c — MMU, caches and GPU memory access

GPU MMU cache maintenance, and firmware access to client GPU memory.

Client memory is reached by walking the render context’s GPU page tables to a bus address and mapping that page through a scratch entry of the firmware’s MIPS page table.

MMUCACHE_PT

MMUCACHE flag: invalidate the page-table cache.

MMUCACHE_PD

MMUCACHE flag: invalidate the page-directory cache.

MMUCACHE_PC

MMUCACHE flag: invalidate the page-catalogue cache.

MMUCACHE_TLB

MMUCACHE flag: invalidate the MMU TLB.

MMUCACHE_PMTLB

MMUCACHE flag: invalidate the parameter manager’s TLB.

MMUCACHE_INTERRUPT

MMUCACHE flag: the host wants an interrupt (the sync word is always written).

PM TLB invalidate register (no public name; from the specification).

void bif_inval(uint32_t bits)

Invalidate GPU MMU caches and wait until it is done.

CR_BIF_CTRL_INVAL is asynchronous: the requested bits read back as 1 until the invalidation completes (kick-geometry-ta.md, partial-render-spm.md: write it broadcast, then poll the bits to 0). A kick that follows before then may still use stale translations: with several memory contexts mapping the same virtual addresses, a write then lands in another context’s page (E063).

Parameters:
  • bits – CR_BIF_CTRL_INVAL bits (PT 0x1, PD 0x2, PC 0x4, TLB 0x8).

uint32_t mmu_cache_op(const struct kccb_cmd *cmd)

Handle a KCCB MMUCACHE command.

The host has rewritten page tables in DRAM, so the SLC is flushed and invalidated first (otherwise the MMU walks stale lines, E015). Then the requested MMU caches (CR_BIF_CTRL_INVAL) and the PM TLB are invalidated, the dynamic MIPS TLB entries are flushed in case firmware mappings changed too, and the sync value is written to the host’s sync word.

Parameters:
  • cmd – The command; cmd->data.mmucache holds the flags and the sync word.

SCRATCH_VA

Firmware virtual address of the scratch page used to reach client memory.

It sits in the host’s reserved 1 MiB of the heap window, past the firmware data object, where the host allocates nothing. It is the even page of a TLB pair; the odd one stays empty.

PTE_UNCACHED_VG

MIPS PTE flags for a read-only mapping: uncached, valid, global.

PTE_UNCACHED_DVG

MIPS PTE flags for a writable mapping: uncached, dirty, valid, global.

static volatile uint32_t *scratch_pte(void)

Find the scratch page’s entry in the firmware’s MIPS page table.

Returns:

Pointer to the PTE that maps SCRATCH_VA.

static volatile uint8_t *phys_map(uint64_t pa, bool write)

Map a bus page at the scratch address.

The entry is only taken when it is empty, so a host mapping is never overwritten. The dynamic TLB is flushed so that the new entry is used.

Parameters:
  • pa – Any bus address inside the 4 KiB page to map.

  • write – Map writable (dirty bit set) instead of read-only.

Returns:

The firmware address of pa, or NULL when the scratch entry is in use.

static void phys_unmap(void)

Remove the scratch mapping made by phys_map().

static bool phys_rd(uint64_t pa, uint64_t *v, bool wide)

Read a GPU page-table entry at a bus address.

The entry is naturally aligned, so it never crosses the mapped page.

Parameters:
  • pa – Bus address of the entry.

  • v – Where to store the entry.

  • wide – Read 64 bits (page directory and page table) instead of 32 (page catalogue).

Returns:

true when read, false when the scratch entry was busy.

static uint64_t gpu_va_to_pa(fwaddr_t memctx, uint64_t va)

Translate a GPU virtual address through a memory context’s page tables.

Walks the page catalogue (1 GiB per entry), the page directory (2 MiB per entry, which also gives the page size, 4 KiB to 2 MiB) and the page table, using the public entry formats of pvr_rogue_mmu_defs.h.

Parameters:
  • memctx – Firmware address of the context’s fwmemcontext.

  • va – GPU virtual address.

Returns:

The bus address, or 0 when any level is not valid.

static bool gpu_access(fwaddr_t memctx, uint64_t va, uint8_t *buf, uint32_t len, bool write)

Read or zero client GPU memory, one page at a time.

Parameters:
  • memctx – Firmware address of the memory context.

  • va – GPU virtual address of the first byte.

  • buf – Destination of a read; NULL when zeroing.

  • len – Number of bytes.

  • write – Zero the memory instead of reading it.

Returns:

true when every byte was accessed, false when a page is not mapped or the scratch entry is busy.

bool gpu_read(fwaddr_t memctx, uint64_t va, void *buf, uint32_t len)

Copy client GPU memory into the firmware.

Parameters:
  • memctx – Firmware address of the memory context.

  • va – GPU virtual address.

  • buf – Destination.

  • len – Number of bytes.

Returns:

true on success, false when part of the range is not mapped.

bool gpu_zero(fwaddr_t memctx, uint64_t va, uint32_t len)

Zero client GPU memory.

Parameters:
  • memctx – Firmware address of the memory context.

  • va – GPU virtual address.

  • len – Number of bytes.

Returns:

true on success, false when part of the range is not mapped.