From fdb94f8e9062abe00a1db117cccac020564df164 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 11:56:41 +0100 Subject: [PATCH 1/3] drm/imagination: apple: Account S8003 firmware phase time at record reuse MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Expose the firmware's retired geometry and fragment phase times, in 24 MHz ticks, alongside the existing host in-flight counters. The two W records alternate by sequence parity. Both submit paths wait for last_seq - 1 before writing seq = last_seq + 1, so the prior job in that slot has retired. dma_rmb() orders the same coherent CPU mapping used by the done stamp. Preserve W+0x550/0x8ac before fill_ta/fill_3d memsets W, then count it in s8003_job_write before the result is zeroed. A rejected fill cannot double-count an old result. No firmware record, submission ordering or clock policy changes. Affects: Apple A9 (S8003); measurement only Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 46 ++++++++++++++++--- 1 file changed, 40 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index 0b3547adf1449f..519e6b65431785 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -36,6 +36,8 @@ * readings of Apple's kext and firmware. */ +#include + #include "pvr_apple.h" #include "pvr_apple_chip.h" #include "pvr_context.h" @@ -511,6 +513,7 @@ struct pvr_apple_s8003 { u32 set_frag[S8003_PM_SETS]; unsigned long wb_sets; struct s8003_vm *pm_last, *pm_prev; /* last two scenes for cache-line reads */ + u64 gpu_ticks[2], gpu_jobs[2], gpu_zero[2]; /* retired TA/3D phase times */ struct pvr_vm_context *park_vm; /* maps what every VM maps, nothing else */ /* Bus addresses the firmware is told: its registers' and ours */ @@ -2416,6 +2419,19 @@ static struct s8003_job_slot s8003_slot_of(u32 seq, bool frag) }; } +/* + * Submissions wait for last_seq - 1 before reusing either of the two records: + * the prior occupant of s8003_slot_of(last_seq + 1) is last_seq - 1. Preserve + * its result before fill_ta/fill_3d clears W. Failed fills do not count it. + */ +static u64 s8003_job_ticks(u8 *p, u32 seq, bool frag) +{ + struct s8003_job_slot at = s8003_slot_of(seq, frag); + + dma_rmb(); + return get_unaligned_le64(p + at.w + (frag ? 0x8ac : 0x550)); +} + /* What the 3D engine is waited for between a fragment job's start and finish */ static const u32 s8003_3d_wait[] = { 0x201, 0x138204, 0x10, 0x8388204, 0, 0x104000a, 0x1f, @@ -2444,8 +2460,9 @@ static u8 *useq_put(u8 *out, const u32 *words, unsigned int n) * rebuilt only on a generation change (0xfffffff006cb1940). A fragment job * waits at @wait_stamp for @wait_seq, its geometry job's stamp. */ -static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 slot, bool frag, - u32 seq, u32 wait_stamp, u32 wait_seq, bool pm_record) +static void s8003_job_write(struct pvr_apple_s8003 *s, u8 *p, u32 addr, u32 global, u32 pm, + u32 pm_id, u32 slot, bool frag, u32 seq, u32 wait_stamp, u32 wait_seq, + bool pm_record, u64 gpu_ticks) { struct s8003_job_slot at = s8003_slot_of(seq, frag); u32 w = addr + at.w, record = pm + S8003_PM_RECORD; @@ -2458,6 +2475,13 @@ static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 const u32 time_end[] = { 0x18, result, time, time + 4, seq }; const u32 wait_ta = 0x801, end = 0x40000017; + if (gpu_ticks) { + s->gpu_ticks[frag] += gpu_ticks; + s->gpu_jobs[frag]++; + } else { + s->gpu_zero[frag]++; + } + put32(work + (frag ? 0x8ac : 0x550), 0); put32(work + (frag ? 0x8b0 : 0x554), 0); put32(work + (frag ? 0x8b4 : 0x558), times); @@ -2705,6 +2729,11 @@ static void s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) if (!s) return; + seq_printf(m, "gpu_ns geometry %llu fragment %llu jobs %llu %llu zero %llu %llu\n", + mul_u64_u32_div(READ_ONCE(s->gpu_ticks[0]), 125, 3), + mul_u64_u32_div(READ_ONCE(s->gpu_ticks[1]), 125, 3), + READ_ONCE(s->gpu_jobs[0]), READ_ONCE(s->gpu_jobs[1]), + READ_ONCE(s->gpu_zero[0]), READ_ONCE(s->gpu_zero[1])); mutex_lock(&apple->submit_lock); seq_printf(m, "a9 pm pools bytes %lld shared %d (pools %u x %u blocks) guard_waits %lld\n", atomic64_read(&s->pm_pool_bytes), READ_ONCE(s8003_pm_shared_param), @@ -2922,6 +2951,7 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc u8 *p = s8003_channel(s, APPLE_ENGINE_GEOM); struct s8003_pm *pm = NULL; struct s8003_rt rt; + u64 gpu_ticks; u32 seq, blocks; bool pm_record; unsigned int i; @@ -3001,12 +3031,13 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc if ((s32)(done - pm->rec_seq) < 0) pm_record = true; } + gpu_ticks = s8003_job_ticks(p, seq, false); s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); - s8003_job_write(p, s8003_channel_addr(s, APPLE_ENGINE_GEOM), + s8003_job_write(s, p, s8003_channel_addr(s, APPLE_ENGINE_GEOM), s8003_object_addr(&s->layout, S8003_CHANNEL), s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[0], false, seq, s8003_channel_addr(s, APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, - pm_record ? 0 : pm->rec_seq, pm_record); + pm_record ? 0 : pm->rec_seq, pm_record, gpu_ticks); if (pm_record) pm->rec_seq = seq; pm->hwrt = job->hwrt; @@ -3035,6 +3066,7 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc struct s8003_pm *pm = NULL; struct s8003_rt rt; unsigned int i; + u64 gpu_ticks; u32 seq; int err; @@ -3056,14 +3088,16 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc "record slot wait"); seq = apple->frag_seq + 1; + gpu_ticks = s8003_job_ticks(p, seq, true); err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); if (err) return s8003_submit_error(apple, vm, "fragment", err, S8003_REFUSED_FILL, "3D record fill"); - s8003_job_write(p, s8003_channel_addr(s, APPLE_ENGINE_FRAG), + s8003_job_write(s, p, s8003_channel_addr(s, APPLE_ENGINE_FRAG), s8003_object_addr(&s->layout, S8003_CHANNEL + 1), s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[1], true, seq, - s8003_channel_addr(s, APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq, true); + s8003_channel_addr(s, APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq, true, + gpu_ticks); pm->td_seq = seq; WRITE_ONCE(s->set_frag[pm->set], seq); apple->frag_seq = seq; From cf958f305e0194bd85b531519af439fefd50891f Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 4 Dec 2026 15:23:36 +0800 Subject: [PATCH 2/3] drm/imagination: apple: drop the apple_pm_shared switch The S8003 PM pools are now always shared between VMs, so drop the apple_pm_shared module parameter that kept per-set pools one boot parameter away. Debug switches stay out of the tree. Say next to the guard on a shared pool what it relies on: a scene's geometry and fragment jobs are armed in one submission, so a previous user whose fragment job is not yet submitted has none to come. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index b351e02450308c..8ab209fc35764e 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -2752,11 +2752,6 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, return true; } -static bool s8003_pm_shared_param = true; -module_param_named(apple_pm_shared, s8003_pm_shared_param, bool, 0644); -MODULE_PARM_DESC(apple_pm_shared, - "Apple A9: one PM pool per set index for all VMs (default 1); read when a set first gets a pool"); - static void s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) { struct pvr_apple_s8003 *s = apple->chip_data; @@ -2773,9 +2768,9 @@ static void s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) READ_ONCE(s->gpu_jobs[0]), READ_ONCE(s->gpu_jobs[1]), READ_ONCE(s->gpu_zero[0]), READ_ONCE(s->gpu_zero[1])); mutex_lock(&apple->submit_lock); - seq_printf(m, "a9 pm pools bytes %lld shared %d (pools %u x %u blocks) guard_waits %lld\n", - atomic64_read(&s->pm_pool_bytes), READ_ONCE(s8003_pm_shared_param), - READ_ONCE(s->pm_shared_count), S8003_PM_BLOCKS_MAX, + seq_printf(m, "a9 pm pools bytes %lld (shared pools %u x %u blocks) guard_waits %lld\n", + atomic64_read(&s->pm_pool_bytes), READ_ONCE(s->pm_shared_count), + S8003_PM_BLOCKS_MAX, atomic64_read(&s->pm_guard_waits)); xa_for_each(&s->vms, index, vm) { for (i = 0; i < ARRAY_SIZE(vm->pm); i++) { @@ -2908,7 +2903,7 @@ static int s8003_prepare_job(struct pvr_apple *apple, struct pvr_job *job) mutex_unlock(&apple->submit_lock); } mutex_lock(&apple->submit_lock); - if (!pm->pool && !pm->next_pool && READ_ONCE(s8003_pm_shared_param)) { + if (!pm->pool && !pm->next_pool) { mutex_unlock(&apple->submit_lock); err = s8003_pm_shared_get(apple, i); if (err) @@ -3016,6 +3011,13 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc return s8003_submit_error(apple, vm, "geometry", -EBUSY, S8003_REFUSED_PM, "no PM set"); + /* + * The VMs' sets of one index share a pool: wait until the scene of the + * pool's previous user is done. This relies on a scene's geometry and + * fragment jobs being armed in one submission: the job dependencies then + * order this geometry job after the previous VM's fragment job, so a + * scene with td_seq still 0 has no fragment job that could come later. + */ if (pm->shared) { unsigned int idx = pm - vm->pm; const struct s8003_pm *user = s->pm_shared[idx].user; From aec4c179d497aab2ab11e386725f875bf043cdf6 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Fri, 4 Dec 2026 15:26:24 +0800 Subject: [PATCH 3/3] drm/imagination: apple: drop the A9 job record layout switches The A9 firmware now always gets its job channels and PM sets through META's uncached window, as Apple's kext gives them. Drop the module parameters that kept the earlier cached layout one boot parameter away (apple_ch_uncached, apple_pm_uncached) and the runtime switch for s8003_touch() (apple_touch); s8003_touch() still runs on every variant that needs it. Debug switches stay out of the tree. The comment on where the firmware reaches the records moves to where the layout is chosen. Assisted-by: LLM Signed-off-by: Henrik Klee --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 44 ++++++------------- 1 file changed, 14 insertions(+), 30 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index 8ab209fc35764e..bd4e677844b63a 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -120,7 +120,7 @@ #define S8003_IMAGE_SIZE 0x30000 /* * The root memory: its objects, then, in the Apple layout, the job channels - * (s8003_ch_uncached), at the same offsets from one another as in the shared + * (pvr_apple_s8003_init()), at the same offsets from one another as in the shared * memory. */ #define S8003_ROOT_SIZE 0x14000 @@ -145,7 +145,7 @@ #define S8003_PM_SETS 12 #define S8003_PM_SET_SIZE (3 * PVR_DEVICE_PAGE_SIZE) #define S8003_PM_SETS_VA (S8003_SHARED_VA + S8003_SHARED_SIZE) -/* The PM sets in the Apple layout (s8003_pm_uncached): in META's uncached window */ +/* The PM sets in the Apple layout (pvr_apple_s8003_init()): in META's uncached window */ #define S8003_PM_SETS_ROOT_VA (S8003_ROOT_VA + SZ_1M) #define S8003_PM_RECORD 0x80 #define S8003_PM_BLOCK_SIZE SZ_128K /* eight pages of the pool */ @@ -1509,30 +1509,6 @@ static void s8003_channels_init(struct pvr_apple_s8003 *s) /* How often s8003_touch() runs while the firmware works for the host */ #define S8003_TOUCH_US 1000 -/* - * Where the firmware reaches the A9's job records and PM sets. Apple's kext - * gives the firmware every per-job object through META's uncached window: on - * the iPad 5 under iOS, the channel states are at 0x43200000, a TA's W at - * 0x43230e80, its PM record at 0x43469880, the stamps at 0x43438000. The A8 - * and A7 keep their channels there too. Through the cached window (both 0, - * the earlier A9 layout) a job's finish that comes within ~300 us of a - * doorbell waits for the firmware ~200 us longer than iOS's does, and the - * firmware's writes need s8003_touch() to be seen. Read once, when the GPU is - * probed. - */ -static bool s8003_ch_uncached = true; -module_param_named(apple_ch_uncached, s8003_ch_uncached, bool, 0444); -MODULE_PARM_DESC(apple_ch_uncached, - "A9: job channels (records, microsequences, rings, stamps) in the firmware's uncached window, as Apple's (default 1)"); - -static bool s8003_pm_uncached = true; -module_param_named(apple_pm_uncached, s8003_pm_uncached, bool, 0444); -MODULE_PARM_DESC(apple_pm_uncached, "A9: PM sets in the firmware's uncached window, as Apple's (default 1)"); - -static bool s8003_touch_on = true; -module_param_named(apple_touch, s8003_touch_on, bool, 0644); -MODULE_PARM_DESC(apple_touch, "A9: read the firmware's memory every millisecond while it works (default 1)"); - static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine); /* @@ -1608,7 +1584,7 @@ static void pvr_apple_s8003_events(struct pvr_apple *apple) if (!READ_ONCE(apple->booted) || !s->shared.map) return; /* An eighth early, so that a check every S8003_TOUCH_US always touches */ - if (s->v->touch && READ_ONCE(s8003_touch_on) && + if (s->v->touch && ktime_us_delta(now, READ_ONCE(s->touched)) + S8003_TOUCH_US / 8 >= S8003_TOUCH_US) { WRITE_ONCE(s->touched, now); s8003_touch(s); @@ -3424,9 +3400,17 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) apple->chip_data = s; s->apple = apple; s->v = v; - s->ch_root = v->channels_root || (v->root_channel[0] && s8003_ch_uncached); - s->pm_sets_va = v->root_channel[0] && s8003_pm_uncached ? S8003_PM_SETS_ROOT_VA : - S8003_PM_SETS_VA; + /* + * Where the firmware reaches the A9's job records and PM sets. Apple's + * kext gives the firmware every per-job object through META's uncached + * window: on the iPad 5 under iOS, the channel states are at 0x43200000, + * a TA's W at 0x43230e80, its PM record at 0x43469880, the stamps at + * 0x43438000. The A8 and A7 keep their channels there too. Through the + * cached window a job's finish that comes within ~300 us of a doorbell + * waits for the firmware ~200 us longer than iOS's does. + */ + s->ch_root = v->channels_root || v->root_channel[0]; + s->pm_sets_va = v->root_channel[0] ? S8003_PM_SETS_ROOT_VA : S8003_PM_SETS_VA; INIT_DELAYED_WORK(&s->drain_work, s8003_drain_work); spin_lock_init(&s->tag_lock); spin_lock_init(&s->set_lock);