pm.c — Parameter manager

Parameter Manager: free-list and list loads, PM context bookkeeping, the cross-context free-list synchronisation, the out-of-memory handling (SPM) and the free-list reconstruction after a hardware recovery.

Sources:

  • docsClean kick-geometry-ta.md phase B (answers to Q16b-Q16m): free-list banks, live stack pointers, OOM and partial-render register changes;

  • docsClean partial-render-spm.md: SPM decisions, states, fidelity notes;

  • E035 and E037: the free-list and list stack-pointer load commands;

  • mainline pvr_free_list.c: the host side of grow and reconstruction.

static const uint16_t alist_sp_load[2]

ALIST stack-pointer load registers per PM context (kick-geometry-ta.md B step 5); the live values are at 0x0270 / 0x0280.

static const uint16_t mlist_sp_load[2]

MLIST stack-pointer load registers per PM context (live at 0x02C0 / 0x02D0).

struct fl_regs

The registers of one free-list bank of a PM context.

uint16_t base

Base-address load register.

uint16_t top

Stack-top load register (64-bit).

uint16_t cnt

Page-count load registers (pages, then MMU pages at +4).

uint16_t cmd

Load command: write 1, wait until bit 0 clears.

uint16_t live_sp

Live stack pointer.

uint16_t live_pb

Live allocated page count.

uint16_t live_mmu

Live allocated MMU page count.

uint8_t top_shift

Position of the stack top in the 64-bit top register.

static const struct fl_regs flr[2][2]

Free-list bank registers, indexed [PM context][local or global] (E035, answers to Q16b-Q16k).

static const uint16_t mlist_base[2]

MLIST base register per PM context.

static const uint16_t cat_base[2][3]

VCE, TE and ALIST catalogue-base registers per PM context.

struct pm_slot pm_slots[2]

What each PM context holds (see pm_slot).

static bool pm_cmd(uint32_t reg)

Run a PM load command: write 1 and wait (10 ms at most) until bit 0 clears.

Parameters:
  • reg – The command register.

Returns:

true when the command completed.

void pm_select(uint32_t ctx)

Select the PM context that the following loads go to (Q16h, E037).

Parameters:
  • ctx – PM_CTX_GEOM or PM_CTX_FRAG.

bool pm_load_freelist(uint32_t ctx, uint32_t which, const struct pm_freelist *fl)

Load a free list into a PM context.

Writes the base, stack top and page counts, then runs the bank’s load command.

Parameters:
  • ctx – The PM context.

  • which – PM_FL_LOCAL or PM_FL_GLOBAL.

  • fl – The free list.

Returns:

true when the load command completed.

void pm_store_freelist(uint32_t ctx, uint32_t which, struct pm_freelist *fl)

Copy a PM context’s live free-list state back into the free list.

Parameters:
  • ctx – The PM context.

  • which – PM_FL_LOCAL or PM_FL_GLOBAL.

  • fl – The free list that receives the stack top and page counts.

void pm_load_bases(uint32_t ctx, uint64_t mlist, uint64_t vce, uint64_t te, uint64_t alist)

Load the MLIST base and the catalogues of a PM context, without the stack-pointer loads (a first geometry kick).

Parameters:
  • ctx – The PM context.

  • mlist – MLIST device address.

  • vce – VCE catalogue base.

  • te – TE catalogue base.

  • alist – ALIST catalogue base.

void pm_load_lists(uint32_t ctx, uint64_t mlist, uint64_t vce, uint64_t te, uint64_t alist, uint64_t alist_sp, uint32_t mlist_sp)

Load the MLIST base, the catalogues and the ALIST/MLIST stack pointers of a PM context (every load except a first geometry kick).

Parameters:
  • ctx – The PM context.

  • mlist – MLIST device address.

  • vce – VCE catalogue base.

  • te – TE catalogue base.

  • alist – ALIST catalogue base.

  • alist_sp – ALIST stack pointer.

  • mlist_sp – MLIST stack pointer.

void pm_mmu_inval(void)

Invalidate the PM’s MMU and TLB.

Needed at every first geometry kick, every HWRTData switch in a PM context and every 3D finished (kick-geometry-ta.md B step 3). Without it the PM keeps catalogue and TLB entries of the previous HWRTData, and the second HWRTData of a dataset stalls or faults (E041, E042). Invalidates the PM lines of the SLC, the BIF page-catalogue cache and the PM TLB.

void pm_slot_evict(uint32_t ctx, fwaddr_t next_hwrt)

Store the free-list state a PM context holds before another HWRTData is loaded into it (“Store Freelist … for PMDM<n>”).

Without it, two renders that interleave (geometry A, geometry B, fragment A) leave A’s free lists stale, and the PM can hand out pages that are still in use (E041). Not called at present: pm_sync_load_freelists() does this as part of each load.

Parameters:
  • ctx – The PM context.

  • next_hwrt – Firmware address of the HWRTData about to be loaded.

void pm_slot_stored(uint32_t ctx)

Record that a PM context’s free-list state was stored.

Happens at the fragment load for context 0 and at the end of the fragment for context 1. The free-list structures are then the newest copy, and a later eviction must not overwrite them with the stale registers. Without this, the second render of an HWRTData pair hung the TA (E041).

Parameters:
  • ctx – The PM context.

void pm_slot_set(uint32_t ctx, fwaddr_t hwrt)

Record that a PM context was loaded from an HWRTData.

Its free lists are remembered and its live state is marked newer than the structures.

Parameters:
  • ctx – The PM context.

  • hwrt – Firmware address of the HWRTData; 0 empties the slot.

void pm_invalidate(void)

Forget what both PM contexts hold (power-off, hardware recovery).

CR_PM_CONTEXT_PB_BASE

Which free lists the two PM contexts share (Q38): bit 1 set when the local lists differ, bit 0 when the global lists differ.

PM_PAUSE_TA_ALLOC

CR_PM_PAGE_MANAGEOP bit that pauses PM context 0 (TA allocation).

PM_PAUSE_3D_DEALLOC

CR_PM_PAGE_MANAGEOP bit that pauses PM context 1 (3D deallocation).

static bool pm_ctx_running(uint32_t ctx)

Check whether a PM context’s data master is doing PM work right now.

Parameters:
  • ctx – The PM context.

Returns:

true while the geometry runs (context 0), or while a 3D render (not a transfer) runs (context 1).

static void pm_pause_ctx(uint32_t ctx, bool on, uint32_t *paused)

Pause or resume a PM context through CR_PM_PAGE_MANAGEOP.

A context is only paused when its data master runs, and the change is waited for in CR_PM_PAGE_MANAGEOP_ACK.

Parameters:
  • ctx – The PM context.

  • on – Pause (true) or resume.

  • paused – The pause bits this caller set; updated.

bool pm_sync_load_freelists(uint32_t c, fwaddr_t hwrt_fw, const void *hwrt)

Load an HWRTData’s free lists into a PM context, synchronised with the other PM context.

The other context may be running on the other data master for another render context (scheduler.md “Multi-context DM 2 / DM 3 concurrency and PM freelist sync”, Q38). For each list:

  • the list this context held is stored first, unless the other context owns it now (then the other context is paused instead);

  • a list the other context holds is paused and stored from there first, so that this context gets its newest stack pointer.

The lists are then loaded, CONTEXT_PB_BASE tells the PM which lists the two contexts share, and the other context is resumed. The caller loads the catalogues and stack pointers afterwards.

Parameters:
  • c – The PM context to load.

  • hwrt_fw – Firmware address of the HWRTData.

  • hwrt – The HWRTData.

Returns:

false if a free-list load command did not complete.

static void pm_pause(bool on)

Pause or resume the TA’s page allocation (PM context 0).

Parameters:
  • on – Pause (true) or resume.

static void fl_rebase(struct pm_freelist *fl)

Recompute a free list’s current base after its page count changed.

The host fills the page array from its end (pvr_free_list.c), so the base is max_pages - current_pages entries into the array, aligned down to 16 bytes.

Parameters:
  • fl – The free list.

static void fl_add_pages(fwaddr_t addr, struct pm_freelist *fl, uint32_t added, uint32_t old_total)

Apply pages added to a free list.

The pages are added below the current base: the base moves down by 4 bytes per page and the stack pointer up by the page count (the specification saw 0x20C8 go from 7 to 0x47 for 64 pages). When a PM context holds the list with the newest state, the live state is read, updated and reloaded inside a PM_PAGE_MANAGEOP pause; otherwise only the structure changes.

Parameters:
  • addr – Firmware address of the free list.

  • fl – The free list, with current_pages already raised.

  • added – Number of pages added.

  • old_total – current_pages before the change (only traced).

MAX_PENDING_GROW

Free lists whose grown pages can wait for the next out-of-memory event.

struct [anonymous]

Pages a grow update added while the geometry was not waiting for them.

The reference firmware hands them out at the next out-of-memory event, not mid-run (partial-render-spm.md, observed sequences); loading them into a running PM stalled the TA in our first test.

static uint32_t *grown_slot(fwaddr_t fl, bool create)

Find a free list’s pending grown pages.

Parameters:
  • fl – Firmware address of the free list.

  • create – Take a free entry when the list has none.

Returns:

Pointer to the page count, or NULL.

PRB_ZS

ROGUE_FWIF_PRBUFFER_ZSBUFFER: index of the ZS buffer in pr_buffer_fw_addr[].

PRB_MSAA

ROGUE_FWIF_PRBUFFER_MSAABUFFER.

PRB_MAX

ROGUE_FWIF_PRBUFFER_MAXSUPPORTED.

enum [anonymous]

rogue_fwif_prbuffer_state.

struct [anonymous]

The SPM state of the running geometry: its state, HWRTData, render context, the free list it waits for and its partial-render fence.

fwaddr_t prb[PRB_MAX]

The geometry command’s pr_buffer_fw_addr[]: ZS and MSAA buffers.

static void spm_set(enum spm_state st)

Change the SPM state, and trace it.

Parameters:
  • st – The new state.

void spm_reset(void)

Reset the SPM and forget the pending grown pages. A partial render that an HWR cut short counts as executed for its buffers, so that their cleanup does not wait for it forever.

void pm_resource_freed(fwaddr_t addr)

Forget a resource the host has just freed (KCCB CLEANUP of an HWRTData or a free list, answered “not busy”).

A PM context that was loaded from it is no longer tracked: its free lists are not stored back, not synchronised and not collected for a reconstruction after an HWR, since the memory is gone (FW 0001: an HWR read a destroyed free list, ID 0xDEADBEE0, and its reconstruction never completed). The SPM forgets an HWRTData it was tracking.

Parameters:
  • addr – Firmware address of the HWRTData or free list.

void spm_geom_kick(fwaddr_t hwrt, fwaddr_t cmd_fw, const void *pr_fence, const fwaddr_t pr_buffers[PRB_MAX])

Arm the SPM for a geometry kick.

A later kick of a render whose out-of-memory handling is still in progress keeps the current state.

Parameters:
  • hwrt – Firmware address of the HWRTData.

  • cmd_fw – Firmware address of the render context.

  • pr_fence – The command’s partial_render_geom_frag_fence UFO.

  • pr_buffers – The command’s pr_buffer_fw_addr[] (ZS, MSAA; 0 when the host backed the buffer itself).

static void prb_request(uint32_t id, bool backing)

Send a ZSBUFFER_BACKING or ZSBUFFER_UNBACKING request to the host.

Parameters:
  • id – The buffer’s buffer_id.

  • backing – Back (true) or unback the buffer.

static void prb_request_all(void)

Request the backing of the partial render’s buffers, at out of memory (Q41 items 3 and 4).

Both non-NULL buffers of pr_buffer_fw_addr[] are considered, whatever the fragment command’s flags. A buffer that is not on demand or already backed needs nothing; one whose backing is pending is left alone; an unbacked one, or one whose unbacking is still pending, is requested at once and becomes BACKING_PENDING.

static bool prb_ready(void)

Whether the partial render may run: it waits only while a backing is pending (Q41 item 2). An unbacked buffer (its backing failed) does not block it; the FRAG_PR then runs without that buffer.

Returns:

true when no buffer of the partial render is BACKING_PENDING.

void spm_zsbuffer_update(fwaddr_t zsb, bool done, bool backing)

Handle a KCCB ZSBUFFER_BACKING_UPDATE or ZSBUFFER_UNBACKING_UPDATE.

A backing update makes the buffer BACKED, or UNBACKED when the host failed (SPM_42); an unbacking update makes it UNBACKED. A partial render waiting for its buffers is armed once none is pending any more.

Parameters:
  • zsb – Firmware address of the buffer.

  • done – The host succeeded.

  • backing – The update answers a backing request (else an unbacking).

bool spm_pr_zs_unbacked(void)

Whether the FRAG_PR must leave out the forced depth/stencil store and load: its ZS buffer exists but is not backed (Q41 item 2).

Returns:

true when the partial render has a ZS buffer that is not BACKED.

static void prb_count(uint32_t off)

Count a partial render against its buffers (submitted at the kick, executed when it finished), so that cleanup waits for it.

Parameters:
  • off – PRB_SUBMITTED or PRB_EXECUTED.

bool spm_zsbuffer_cleanup(fwaddr_t zsb)

Clean up a ZS/MSAA buffer (KCCB CLEANUP_ZSBUFFER, Q41 items 1 and 5).

  • A partial render that uses it has not finished: busy.

  • It is on demand and not unbacked: an unbacking is requested (unless one is already pending) and the cleanup is busy until the host’s ZSBUFFER_UNBACKING_UPDATE; the host then retries.

  • Otherwise it can be freed.

Parameters:
  • zsb – Firmware address of the buffer.

Returns:

true when the cleanup must be answered busy.

static void spm_resume_ta(void)

Resume the PM and the TA after a free-list update that gave ready pages.

static void spm_pause_ta(void)

Stop the TA until a grow update or a partial render.

Asks the TA to stop on the out-of-memory event, waits for TA_TERMINATE, then pauses the TE/TA state.

static void spm_unpause_ta(void)

Restart the geometry paused by spm_pause_ta() after a grow update.

static void request_grow(struct pm_freelist *fl)

Ask the host to grow a free list (FWCCB_FREELIST_GROW), once.

Parameters:
  • fl – The free list.

static bool can_grow(const struct pm_freelist *fl)

Check whether the host can still grow a free list.

Parameters:
  • fl – The free list.

Returns:

true when it has a grow size and is below its maximum.

static void start_partial_render(void)

Arm a partial render.

Without a partial-render fence nothing is armed (the watchdog decides). Otherwise the touched layers are listed (geom_rta_oom(), Q39) and the state becomes SPM_WAIT_HW: the scheduler then runs the fragment context’s FRAG_PR as a partial render, looking past its FENCE_PR without consuming it.

bool spm_pr_fence_ok(const struct ufo *u)

The SPM failing-UFO check (partial-render-spm.md fidelity notes).

While a partial render is armed, a FENCE_PR UFO that only waits for this geometry (its queue timeline: the address of partial_render_geom_frag_fence) does not block the partial render.

Parameters:
  • u – The unsatisfied UFO.

Returns:

true when the UFO may be passed.

void spm_oom(void)

Handle PM_OUT_OF_MEMORY while the geometry runs.

The firmware does not ask the hardware which list ran out: it always extends the HWRTData’s global free list (freelists[1]; [2] for a secondary geometry pipe, flags bit 8).

  1. Pages from a grow update that arrived while the geometry ran, or else the ready pages, are added at once and the TA resumes; more is requested from the host in the background.

  2. With nothing ready the TA is stopped; the host is asked to grow the list if it can (state SPM_WAIT_GROW), otherwise a partial render is armed.

void spm_grow_update(fwaddr_t addr, uint32_t delta, uint32_t new_pages, uint32_t ready)

Handle a KCCB FREELIST_GROW_UPDATE: the host grew a free list.

When the geometry is not waiting for this list, the new pages are held until its next out-of-memory event (or applied at once when no entry is free). When it waits, the pages (or the ready pages) are added and the TA restarts; if the host could not add any, a partial render is armed.

Parameters:
  • addr – Firmware address of the free list.

  • delta – Pages added by the host (not used: derived from the totals).

  • new_pages – The list’s new page total, ready pages included.

  • ready – Ready pages held back for the next out-of-memory event.

bool spm_pr_armed(fwaddr_t hwrt)

Check whether a partial render is armed for an HWRTData.

Parameters:
  • hwrt – Firmware address of the HWRTData.

Returns:

true in SPM_WAIT_HW for this HWRTData.

bool spm_active(void)

Whether the SPM is handling an out-of-memory event of the running geometry (the TA must not be stored then).

Returns:

true while the SPM is not idle.

void spm_pr_started(void)

Record that the armed partial render was kicked.

void spm_pr_done(void)

Restart the paused geometry after a partial render.

Follows partial-render-spm.md “Restarting geometry after a partial render”: turns the partial-render mode off, drops the 3D PM context’s state (it is the partial render’s), selects PM context 0, pulses the restart registers, invalidates the PM TLB, then resumes the PM and the TA.

MAX_FL_RECONSTRUCT

At most this many free lists per reconstruction request (ROGUE_FWIF_MAX_FREELISTS_TO_RECONSTRUCT).

FL_RECONSTRUCT_FAILED

Flag set in a free-list ID of the reconstruction update when the host failed.

struct [anonymous]

Free lists whose reconstruction the host has not confirmed yet, and the DMs that the scheduler holds meanwhile (traced once per DM).

static void fl_collect(uint32_t *ids, uint32_t *cnt, fwaddr_t fl_fw)

Add a free list to a reconstruction request, once.

Also records it as pending until the host’s update, clears its grow request and traces the request.

Parameters:
  • ids – IDs of the request.

  • cnt – Number of entries in ids, updated.

  • fl_fw – Firmware address of the free list.

void pm_hwr_reconstruct(const fwaddr_t *hwrts, uint32_t n)

Ask the host to rebuild the free lists after a hardware recovery (hardware-recovery.md step 4).

Collects, without duplicates, every free list loaded in a PM context and every free list of the given HWRTData, resets the SPM and the PM context tracking, marks those HWRTData HWR with the caches to be zeroed, and sends FWCCB_FREELISTS_RECONSTRUCTION with the list IDs and the HWR count. The geometry and 3D DMs are then held until the host confirms the rebuild (pm_reconstruct_update(), E054): the host rewrites the free lists in memory meanwhile.

Parameters:
  • hwrts – Firmware addresses of the HWRTData in use.

  • n – Number of entries in hwrts.

bool hwrt_discard(uint32_t dm, fwaddr_t ctxaddr, struct fwcommoncontext *ctx, fwaddr_t hwrt_fw, uint32_t cmd_off)

Discard a command whose HWRTData an HWR left in the HWR state (Q42).

hardware-recovery.md “Discarded geometry and subsequent 3D commands”: a 3D or a non-first geometry kick on an HWRTData in state HWR (set by the recovery and by the host’s free-list reconstruction) must not touch the GPU. The command is retired (its fences are then updated by the commands that follow it) and the context’s resume flags are cleared. A first geometry kick is never discarded: it moves the state on.

Parameters:
  • dm – The data master of the command.

  • ctxaddr – Firmware address of the context.

  • ctx – The context.

  • hwrt_fw – Firmware address of the HWRTData.

  • cmd_off – Offset of the command in the client CCB.

Returns:

true when the command must be retired without running.

void pm_reconstruct_update(const uint32_t *ids, uint32_t n)

Handle the host’s KCCB_FREELISTS_RECONSTRUCTION_UPDATE.

Each listed free list is no longer pending (HWR_53, or HWR_55 when the host flags a failure; the list is released either way, since the mainline host never fails). Once none is pending, the held DMs may run again (RTD_22); the caller runs the scheduler.

Parameters:
  • ids – The free-list IDs of the update.

  • n – Number of entries in ids.

bool pm_reconstruct_hold(uint32_t dm)

Whether a DM must wait for a free-list reconstruction.

The geometry and 3D DMs use the free lists, so they are held while any reconstruction is pending (HWR_63, traced once per DM and HWR).

Parameters:
  • dm – The data master.

Returns:

true when the DM must not start a task now.