sched.c — Client CCB scheduler

Client CCB processing and data-master scheduling.

Every context the host kicks is remembered in a small table. A context is “pending” while its client CCB has unread commands. The scheduler walks the pending contexts round-robin and, for each one, consumes commands from read_offset:

  • auxiliary commands (fences, updates, priority, padding…) are handled in place;

  • a fence that is not satisfied blocks the context (it is re-evaluated on the next scheduling pass: every background task, interrupt task and timer tick);

  • a task is dispatched to its data master if the DM is idle, and stays at read_offset until the DM signals completion; then it is retired and the following updates are processed.

MAX_CTX

Size of the context table.

CLEANUP_Q

Kicks per context whose cleanup control blocks can wait for retirement.

MAX_CMDS_PASS

Commands one context may consume in a single pass.

struct ctx_ent

One context known to the scheduler.

fwaddr_t addr

Firmware address of the context’s fwcommoncontext.

bool used

The entry is in use.

bool pending

The client CCB may have commands to consume.

bool dead

The client CCB is unusable (bad command header or missing buffers).

bool freed

The host cleaned the context up while one of its tasks ran: the entry is freed when that task ends, without touching the context’s memory (FW 0002).

uint8_t dm

The context’s data master, from fwcommoncontext.dm.

fwaddr_t cleanup[CLEANUP_Q][3]

Cleanup control blocks of the kicks not yet retired (FIFO).

uint8_t cleanup_n[CLEANUP_Q]

Number of cleanup control blocks in each FIFO slot.

uint8_t cq_head

FIFO head index.

uint8_t cq_count

FIFO length.

static struct ctx_ent ctxs[MAX_CTX]

The context table.

CTXS_MAGIC

ctxs holds a table from an earlier boot (E064).

struct dm_state dms[DM_MAX]

Per data master: the task it runs, if any (see dm_state).

static uint32_t rr_next

Where the next round-robin pass starts.

static bool host_notify_needed

A UFO was written or a command retired: interrupt the host after the pass.

static inline struct fwcommoncontext *ctx_ptr(const struct ctx_ent *e)

Get a context’s fwcommoncontext.

Parameters:
  • e – The table entry.

Returns:

Pointer to the host’s context structure.

static struct ctx_ent *ctx_find(fwaddr_t addr, bool create)

Look a context up in the table.

Parameters:
  • addr – Firmware address of the context.

  • create – Add the context when it is not in the table.

Returns:

The entry, or NULL when it is not known (and not created, or the table is full).

static bool any_busy(void)

Check whether the GPU has work.

Also traces (DBG_6, 0xE044xxxx) what keeps the GPU busy whenever that changes (E043).

Returns:

true when a data master runs a task or a live context has commands pending.

static void update_pow_state(void)

Update the utilisation state and the power state after a pass.

The utilisation is ACTIVE while work runs or waits, IDLE otherwise, and BLOCKED while forced idle holds work back. The power state follows (ON or IDLE) unless the firmware is forced idle or powered off.

void sched_init(void)

Reset the scheduler: forget every context and data-master task, reset the kick state and report the GPU idle.

static bool ufo_satisfied(const struct ufo *u, bool signed_ge)

Check whether a fence UFO is satisfied.

A sync checkpoint is satisfied once it is signalled or errored. A plain UFO must equal its value, or, for partial-render fences, be at least its value (signed comparison, so that the counter may wrap).

Parameters:
  • u – The UFO.

  • signed_ge – Compare with >= instead of ==.

Returns:

true when satisfied (or when its address is null).

static void ufo_update(const struct ufo *u, bool rmw)

Perform an update UFO.

A sync checkpoint is marked signalled; a plain UFO is set to its value, or incremented by it.

Parameters:
  • u – The UFO.

  • rmw – Add the value instead of writing it.

void sched_ufo_signal(const struct ufo *u)

Signal a UFO from the firmware.

Used by the SPM code to release the partial-render fence. The host is interrupted after the scheduling pass.

Parameters:
  • u – The UFO to write.

static bool pr_fence_blocked(const struct ufo *u, uint32_t cnt)

Check whether a FENCE_PR passed only through the SPM check.

Parameters:
  • u – The fence’s UFOs.

  • cnt – Number of UFOs.

Returns:

true when one of the UFOs is not satisfied by its own value, so the fence only passed because a partial render is armed.

static bool run_partial_render(struct ctx_ent *e, struct fwcommoncontext *ctx, uint8_t *ccb, uint32_t off)

Kick the FRAG_PR that follows a partial-render fence.

The fence is not consumed: the partial render runs while the queue stays where it is.

Parameters:
  • e – The context’s table entry.

  • ctx – The context.

  • ccb – The client CCB.

  • off – Offset of the command after the fence.

Returns:

true when the partial render was kicked; false when the command is not a FRAG_PR or the fragment DM is busy.

static uint32_t task_dm(uint32_t id, uint32_t ctx_dm)

Choose the data master for a task command.

Parameters:
  • id – The command type (CCB_CMD_*, without the flag bits).

  • ctx_dm – The context’s data master, used for any other command.

Returns:

A DM_* index.

static void cleanup_ctl_done(struct ctx_ent *e)

Retire the oldest kick’s cleanup control blocks.

Increments executed_commands in each of them, which tells the host that the resources the kick used may be freed.

Parameters:
  • e – The context’s table entry.

static bool ctx_process(struct ctx_ent *e)

Consume the commands of one context.

At most MAX_CMDS_PASS commands are taken per call. Stops at an unsatisfied fence, at a task whose data master is busy, while one of the context’s own tasks runs, or when the CCB is empty. A command without the magic word marks the context dead.

Parameters:
  • e – The context’s table entry.

Returns:

true if the context made progress (anything retired or dispatched).

void sched_run(void)

Run the scheduler.

Walks the pending contexts round-robin, repeating while any of them makes progress (at most 8 passes), then updates the power state and interrupts the host if anything changed for it.

void sched_dm_done(uint32_t dm)

Retire a data master’s task, called by the kick code when it finishes.

Moves the context’s read offset past the task command and retires its cleanup control blocks. After a partial render (keep set) the FRAG_PR stays at the head of the queue for the next one.

Parameters:
  • dm – The data master (DM_*).

void sched_irq(uint32_t events)

Handle GPU events from the MTS interrupt task.

Parameters:
  • events – The CR_EVENT_STATUS bits that were set.

uint32_t sched_kick(const struct kick_data *kick)

Handle a KCCB kick: the host wrote new commands to a client CCB.

Adds the context to the table if needed, queues the kick’s cleanup control blocks (counting them as submitted), takes the new write offset and wrap mask, and runs the scheduler.

Parameters:
  • kick – The kick command.

Returns:

Extra return-slot flags; always 0.

void sched_force_update(fwaddr_t ctxaddr, uint32_t fence_offset)

Handle a KCCB FORCE_UPDATE: unblock a context stuck on a fence.

Sync checkpoints of the fence at fence_offset that are still active are marked errored, then the context is scheduled again.

Parameters:
  • ctxaddr – Firmware address of the context.

  • fence_offset – Offset of the fence in the client CCB.

uint32_t sched_cleanup(uint32_t type, fwaddr_t addr)

Handle a KCCB CLEANUP request.

A context may be freed when none of its tasks runs and its CCB is empty (or dead); it is then removed from the table. HWRTData, free lists and ZS buffers are checked by kick_resource_busy().

Parameters:
  • type – CLEANUP_* resource type.

  • addr – Firmware address of the resource.

Returns:

KCCB_RTN_CLEANUP_BUSY when the resource is still in use, else 0.