frag.c — Fragment (3D) kicks

Fragment (3D) kick of a render and of a partial render (FRAG_PR), the layer loop of render-target arrays, and the render’s completion.

Sources: docsClean kick-compute-and-3d.md (“Fragment kick”, “Observed during the render”, “Input-attachment renders”, “Render-target arrays” for the layer loop, Q27), kick-geometry-ta.md phase B step 3 and the OOM/PR register changes (Q16m), partial-render-spm.md, the public rogue_fwif_frag_regs layout; experiments E035-E040.

PM_3D_MEM_FREE_TIMEOUT_US

How long frag_done() waits for PM_3D_MEM_FREE, in microseconds (the experiment knob EXP_MEM_FREE_US until E066).

This value fixes a bug. It used to be 3 ms (the specification saw the event 0.4 to 2.5 ms after the render). A partial render at the free list maximum takes about 9 ms to return its pages, so the firmware gave up waiting (trace 0xE0410D01) and let the geometry restart and load the same free list while the PM was still writing it back. The free list was left inconsistent, with no error at that moment.

Why 20 ms (E066):

  • Measured waits: 0 for most renders (the event comes with the interrupt), up to 60 us for small ones, about 2.6 ms for bigpb, and 8.5 to 9.0 ms for the partial render at the free list maximum, the largest page return.

  • 10 ms would leave only about 11 % over that maximum. The count is also a little below wall time (it adds 10 us per udelay(10) and leaves out the register reads), and only one free list maximum (65536 pages), one board and the current clocks were measured.

  • The two errors cost differently: too short brings this bug back silently; too long only costs time when the event is late or never comes (one layered render of bigpb-layers per run never raises it and waits the full bound).

So the bound is about twice the longest wait measured.

static bool frag_is_pr

The fragment data master runs a partial render.

struct [anonymous]

The layer loop of a render-target-array render (kick-compute-and-3d.md, Q27): the layer count, and the region-header, depth/stencil and PBE bases with their per-layer strides.

struct [anonymous]

The command’s fields that depend on has_pr, kept for every layer (partial-render-spm.md “Multi-PR kick state”, Q30; layered FRAG_PR, Q39). They are written at the kick and again on every layer of a render-target-array render by frag_layer_state().

struct [anonymous]

The 3D context switch in progress: a store was requested, and the stored context yields the 3D to yield_to until that context has kicked.

void frag_hwr_reset(void)

Forget a running partial render, layer loop and 3D context switch (after a hardware recovery).

static void frag_layer_state(uint32_t layer)

Write the state that depends on the layer’s partial renders.

has_pr is true when an earlier partial render of this frame stored the layer (HWRT_FLAGS_PRIOR_PR and a non-zero per-layer counter):

  • every FRAG_PR forces the depth/stencil store, and 0x06D0 becomes 0x3F with depth or stencil;

  • with has_pr the depth/stencil load is forced, the partial-render background program pds_pr_bgnd is used, isp_bgobjvals gets bit 9, and ISP_CTL bit 17 is cleared unless the flags keep it;

  • a first FRAG_PR whose background differs sets ISP_CTL bit 17;

  • flag 0x8000 makes 0x06D0 0x7F.

Writes ZLS control, the background object, ISP_CTL and 0x06D0. A FRAG_PR then counts against the layer (Q39).

Parameters:
  • layer – The render target (layer) index.

static void rta_program(uint32_t layer)

Program the per-layer registers of a render-target-array render (steps 1-4).

The region-header base, RENDER_TGT_ID (read by the end-of-tile program), the z-slice of each 3D-twiddled PBE target, and the depth and stencil load/store bases.

Parameters:
  • layer – The render target (layer) index.

static uint32_t rta_setup(const uint8_t *cmd, const uint8_t *r, uint8_t *hwrt, const uint8_t *common)

Set up a render-target-array render, normal or partial.

With one layer (or no array) only RENDER_TGT_ID is written (current_render_target) and the PM pages are freed at the end. With several layers the bases and strides are kept in rta, the first layer of valid_render_targets[] is programmed, and the PM pages are kept until the last layer. rta.on is set when more layers follow.

Parameters:
  • cmd – The fragment command.

  • r – The command’s rogue_fwif_frag_regs.

  • hwrt – The HWRTData.

  • common – The HWRTData’s common part.

Returns:

The layer programmed.

bool frag_rta_next(struct dm_state *s)

Move a render-target-array render to its next layer (step 6, Q39).

Called at PIXELBE_END_RENDER, before the cache flush and the retire (and, for a partial render, before it is reported executed). Programs the next layer of valid_render_targets[], writes its partial-render state again (Q36, Q39) and launches the pixel pipe. After the last layer the loop ends; active_render_targets stays until the next first kick.

Parameters:
  • s – The fragment data master’s state.

Returns:

true if a layer was kicked, false when the render is complete.

static void frag_pm_load(fwaddr_t hwrt_fw, const uint8_t *hwrt)

Phase B step 3 of a render: give the HWRTData to PM context 1.

If PM context 0 still holds this HWRTData, its free lists are stored and it stops being tracked there (the same-HWRTData handoff, Q38). The free lists are then loaded into context 1, synchronised with a geometry of another context, followed by the geometry’s saved catalogues and stack pointers.

Parameters:
  • hwrt_fw – Firmware address of the HWRTData.

  • hwrt – The HWRTData.

static void frag_pm_load_pr(fwaddr_t hwrt_fw, const uint8_t *hwrt)

Load PM context 1 for a partial render (Q16m answer).

The two PM contexts must share the geometry’s free lists during the partial render (kick-geometry-ta.md Q16m item 3: “the two contexts share the global freelist here”), so the pages it frees go back to the list the geometry allocates from. Context 1 may still hold another HWRTData’s lists (CONTEXT_PB_BASE = 3 at the geometry load): the geometry’s live lists are then stored and loaded into context 1, and CONTEXT_PB_BASE becomes 0, as for a fragment load. Without it context 1 runs on a free-list bank that was never loaded and faults at address 0.

Context 1 then takes the geometry’s current catalogues and list stack pointers from the live registers, and PM_PARTIAL_RENDER_ENABLE is set.

Parameters:
  • hwrt_fw – Firmware address of the HWRTData.

  • hwrt – The HWRTData.

bool kick_frag(fwaddr_t ctxaddr, struct fwcommoncontext *ctx, const struct ccb_cmd_header *hdr, const void *payload, uint32_t cmd_off, bool partial)

Kick a fragment command: a render or a partial render.

The phases of kick-compute-and-3d.md “Fragment kick”:

  • A. Single-core control, BIF slot, the PM load (frag_pm_load() or frag_pm_load_pr()).

  • B. ISP configuration: USC output and clears, view index, region headers, screen and macrotile layout.

  • C. The command’s partial-render state, scissor and depth-bias bases, anti-aliasing and multisample control, depth/stencil bases (the command’s, in a partial render too: Q30), the occlusion query base (Q32), border colours, tiles in flight.

  • D. Pipe reset, end-of-render PDS program, PBE words, the partial-render control 0x01C0, the first layer (rta_setup()) and its partial-render state.

  • E. HWRTData state (or the SPM’s partial-render start) and trace.

  • F. Launch the pixel pipe (the last write).

Parameters:
  • ctxaddr – Firmware address of the render context.

  • ctx – The render context.

  • hdr – The command header.

  • payload – The rogue_fwif_cmd_frag command.

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

  • partial – This is a FRAG_PR armed by the SPM.

Returns:

true when the render runs; false when the command has no HWRTData, common data or local free list (it is retired without running).

static bool csw_enabled(void)

Whether the 3D may be preempted: the host enables it with ROGUE_FWIF_INICFG_OS_CTXSWITCH_FRAG_EN in osdata.fw_os_config_flags (the mainline host leaves it at 0). EXP_CSW_FORCE enables it for tests.

void frag_csw_request(fwaddr_t new_ctx)

Ask the running 3D to store its context, for a higher-priority 3D.

Called by the scheduler when a context’s task (a FRAG, FRAG_PR or TQ_3D) waits for the busy 3D (scheduler.md “Preemption”). The store is requested with BCAST[0x1000] = 1, only while (ISP_RENDER & 3) != 3 and not during a partial render (answer to Q40). A render-target-array render with layers left is not stored: the resume of a layer loop is not specified.

Parameters:
  • new_ctx – Firmware address of the waiting context.

bool frag_csw_end(struct dm_state *s, uint32_t ev)

Finish a 3D whose store was requested, at PIXELBE_END_RENDER.

Answer to Q40, items 1, 2 and 4. The render was stored when CR_EVENT_STATUS bit 3 is set and CR[0x1038] bit 0 is clear; with CR[0x1038] bit 0 set it finished normally, the saved deallocation state is zeroed and the normal completion follows.

A stored render saves the ISP position and, once the PM 3D store is idle (CR[0x03F8] & 0xC2), the PM deallocation state into the context’s rogue_fwif_frag_ctx_state; the context needs resume (CSW_7). The HWRTData becomes 3DCONTEXTSTORED, the PM MMU is invalidated, and the command stays at the head of its queue (keep). The caller flushes the 3D caches and releases the data master; PM_3D_MEM_FREE, the layer loop and the retire are skipped.

Parameters:
  • s – The fragment data master’s state.

  • ev – The CR_EVENT_STATUS bits that ended the render (the caller has already cleared them in the register).

Returns:

true when the render was stored.

fwaddr_t frag_csw_yield_to(fwaddr_t ctxaddr)

The context a stored 3D context must let run first.

Parameters:
  • ctxaddr – Firmware address of a context about to kick its 3D.

Returns:

The higher-priority context it yields to, or 0.

void frag_csw_yield_done(void)

Stop yielding: the higher-priority 3D ran, or no longer waits.

static bool frag_csw_resume(fwaddr_t ctxaddr, const struct fwcommoncontext *ctx)

Resume writes of a 3D kick (answer to Q40, item 3), after the layer state and before the pixel launch.

A normal kick zeroes 0x0298 and 0x01C8. A kick of a stored context gives the ISP its stored position (CR[0x1020]), sets ISP_RENDER bit 4 and restores the PM deallocation state.

Parameters:
  • ctxaddr – Firmware address of the context.

  • ctx – The context.

Returns:

true when the kick resumes a stored render.

void frag_done(struct dm_state *s)

Finish a render or a partial render.

The PM returns the pages by itself and raises PM_3D_MEM_FREE after PIXELBE_END_RENDER (Q16k/Q16l); in a small render both arrive together, so the interrupt task may already have seen it (E038). It is awaited for at most PM_3D_MEM_FREE_TIMEOUT_US, then the PM MMU is invalidated.

  • A partial render sets HWRT_FLAGS_PRIOR_PR (the next partial renders and the final render load what it stored), tells the SPM, and keeps the FRAG_PR queued for the next one.

  • A render stores the free lists from PM context 1 and sets the FRAG_FINISHED state.

Parameters:
  • s – The fragment data master’s state.