From 8c6c9192c258f833fa4dd3e21ecb439148245665 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Wed, 2 Dec 2026 18:02:40 +0100 Subject: [PATCH 1/3] drm/imagination: apple: report S8003 PM pools in apple_jobs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Account for live PM pool objects, including replacements prepared for a later geometry job. Expose that total and each reserved set's capacity, last firmware feedback and control words through apple_jobs. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index d0865a88005a11..c1d49611dde05e 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -372,6 +372,8 @@ struct s8003_pm { struct pvr_gem_object *next_pool; void *next_pool_map; u32 next_blocks; + u32 fb_high, fb_margin, fb_request; /* last firmware feedback, in pages */ + bool shared; bool ready; /* published under submit_lock and set_lock */ const struct pvr_hwrt_data *hwrt; /* the scene's target */ u32 scene; /* the firmware start the scene belongs to; 0: none */ @@ -489,6 +491,7 @@ struct pvr_apple_s8003 { spinlock_t set_lock; /* @sets, @next_set, &s8003_pm.set */ DECLARE_BITMAP(sets, S8003_PM_SETS); u32 next_set; + atomic64_t pm_pool_bytes; /* bytes in live PM pool objects */ /* * Which sets the firmware may be writing, for s8003_touch(): per set * the last geometry and fragment job on it (0: none this start), and @@ -1930,10 +1933,12 @@ static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) __clear_bit(pm->set, s->sets); spin_unlock_irqrestore(&s->set_lock, flags); if (pm->pool) { + atomic64_sub(pvr_gem_object_size(pm->pool), &s->pm_pool_bytes); pvr_gem_object_vunmap(pm->pool); pvr_gem_object_put(pm->pool); } if (pm->next_pool) { + atomic64_sub(pvr_gem_object_size(pm->next_pool), &s->pm_pool_bytes); pvr_gem_object_vunmap(pm->next_pool); pvr_gem_object_put(pm->next_pool); } @@ -2032,6 +2037,7 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s return -ENOMEM; /* This set is idle. Its previous pool can now be retired. */ if (pm->pool) { + atomic64_sub(pvr_gem_object_size(pm->pool), &s->pm_pool_bytes); pvr_gem_object_vunmap(pm->pool); pvr_gem_object_put(pm->pool); } @@ -2608,6 +2614,9 @@ static void s8003_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) high = get32(list + S8003_PM_FEEDBACK); margin = get32(list + S8003_PM_FEEDBACK + 4); request = min(get32(list + S8003_PM_FEEDBACK + 12), (u32)S8003_PM_GROW_MAX); + WRITE_ONCE(pm->fb_high, high); + WRITE_ONCE(pm->fb_margin, margin); + WRITE_ONCE(pm->fb_request, request); put32(list + S8003_PM_FEEDBACK_TAKEN, 1); wmb(); vm->fb_at = now; @@ -2647,6 +2656,43 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, return true; } +static void s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) +{ + struct pvr_apple_s8003 *s = apple->chip_data; + struct s8003_vm *vm; + unsigned long index; + unsigned int i; + + if (!s) + return; + + mutex_lock(&apple->submit_lock); + seq_printf(m, "a9 pm pools bytes %lld shared 0 (pools 0 x 128 blocks) guard_waits 0\n", + atomic64_read(&s->pm_pool_bytes)); + xa_for_each(&s->vms, index, vm) { + for (i = 0; i < ARRAY_SIZE(vm->pm); i++) { + struct s8003_pm *pm = &vm->pm[i]; + const u8 *control; + const char *pool; + + if (pm->set < 0) + continue; + control = s->pmsets.map + pm->set * S8003_PM_SET_SIZE; + pool = pm->shared ? "shared" : (pm->pool ? "own" : "none"); + seq_printf(m, "a9 pm vm %p set %d blocks %u %s ready %d idle %d " + "fb_high %u margin %u request %u fb_blocks %u " + "ctl20 %u ctl24 %u ctl3c %#x ctl40 %#x ctl54 %u ctl58 %u\n", + vm->vm_ctx, pm->set, pm->blocks, pool, pm->ready, + s8003_pm_idle(apple, pm), READ_ONCE(pm->fb_high), + READ_ONCE(pm->fb_margin), READ_ONCE(pm->fb_request), + vm->fb_blocks, get32(control + 0x20), get32(control + 0x24), + get32(control + 0x3c), get32(control + 0x40), + get32(control + 0x54), get32(control + 0x58)); + } + } + mutex_unlock(&apple->submit_lock); +} + /* Publish capacity only once its pages, CPU maps and aux VA are ready. * Worker readers hold submit_lock; the atomic arming callback holds set_lock. */ @@ -2733,14 +2779,17 @@ static int s8003_prepare_job(struct pvr_apple *apple, struct pvr_job *job) err = PTR_ERR(obj); goto out; } + atomic64_add(pvr_gem_object_size(obj), &s->pm_pool_bytes); map = pvr_gem_object_vmap(obj); if (IS_ERR(map)) { err = PTR_ERR(map); + atomic64_sub(pvr_gem_object_size(obj), &s->pm_pool_bytes); pvr_gem_object_put(obj); goto out; } mutex_lock(&apple->submit_lock); if (pm->next_pool) { + atomic64_sub(pvr_gem_object_size(pm->next_pool), &s->pm_pool_bytes); pvr_gem_object_vunmap(pm->next_pool); pvr_gem_object_put(pm->next_pool); } @@ -6610,6 +6659,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { .vm_fini = pvr_apple_s8003_vm_fini, .vm_scenes = pvr_apple_s8003_vm_scenes, .vm_evictable = pvr_apple_s8003_vm_evictable, + .debugfs_show = s8003_debugfs_show, .mmu_flush = pvr_apple_s8003_mmu_flush, .irq_ack = pvr_apple_s8003_irq_ack, }; From b66b5f6357ca8bc676437d022144e5b986bc3908 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Wed, 2 Dec 2026 18:03:49 +0100 Subject: [PATCH 2/3] drm/imagination: apple: share S8003 PM pools between VMs MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The A9 firmware runs one scene at a time per VM. The job dependency code orders a VM's geometry after the previous VM's fragment work, so up to two scenes can be live on the two sets of one VM. Each set's free list names the PM pages directly. Share one pool per set index between VMs, as the A10 shares free-list pages. This keeps 128 blocks per scene and limits pinned PM pool memory to 32 MiB, down from 16 MiB per allocated set. Count any wait for a previous user of a shared pool and retain per-set allocation behind apple_pm_shared=0. Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 135 ++++++++++++++++-- 1 file changed, 127 insertions(+), 8 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index c1d49611dde05e..0b3547adf1449f 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -492,6 +492,15 @@ struct pvr_apple_s8003 { DECLARE_BITMAP(sets, S8003_PM_SETS); u32 next_set; atomic64_t pm_pool_bytes; /* bytes in live PM pool objects */ + struct mutex pm_shared_lock; /* creating pm_shared[] */ + struct { + struct pvr_gem_object *obj; + void *map; + u32 zeroed; /* the firmware start it was last zeroed for */ + const struct s8003_pm *user; /* last scene's set; submit_lock */ + } pm_shared[2]; + u32 pm_shared_count; + atomic64_t pm_guard_waits; /* * Which sets the firmware may be writing, for s8003_touch(): per set * the last geometry and fragment job on it (0: none this start), and @@ -1001,6 +1010,7 @@ static void s8003_unmap(struct pvr_apple *apple) static void pvr_apple_s8003_fini(struct pvr_apple *apple) { struct pvr_apple_s8003 *s = apple->chip_data; + unsigned int i; /* A firmware left running keeps its memory (t7000_stuck()) */ if (s->stuck) @@ -1011,6 +1021,17 @@ static void pvr_apple_s8003_fini(struct pvr_apple *apple) s->park_vm = NULL; } s8003_mem_fini(apple, &s->zero); + for (i = 0; i < ARRAY_SIZE(s->pm_shared); i++) { + struct pvr_gem_object *obj = s->pm_shared[i].obj; + + if (!obj) + continue; + atomic64_sub(pvr_gem_object_size(obj), &s->pm_pool_bytes); + pvr_gem_object_vunmap(obj); + pvr_gem_object_put(obj); + s->pm_shared[i].obj = NULL; + } + WRITE_ONCE(s->pm_shared_count, 0); s8003_mem_fini(apple, &s->pmsets); s8003_mem_fini(apple, &s->shared); s8003_mem_fini(apple, &s->root); @@ -1924,7 +1945,7 @@ static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, return max(n, 1U); } -static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) +static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm, unsigned int idx) { unsigned long flags; @@ -1933,8 +1954,13 @@ static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) __clear_bit(pm->set, s->sets); spin_unlock_irqrestore(&s->set_lock, flags); if (pm->pool) { - atomic64_sub(pvr_gem_object_size(pm->pool), &s->pm_pool_bytes); - pvr_gem_object_vunmap(pm->pool); + if (pm->shared) { + if (s->pm_shared[idx].user == pm) + s->pm_shared[idx].user = NULL; + } else { + atomic64_sub(pvr_gem_object_size(pm->pool), &s->pm_pool_bytes); + pvr_gem_object_vunmap(pm->pool); + } pvr_gem_object_put(pm->pool); } if (pm->next_pool) { @@ -1997,7 +2023,7 @@ static void s8003_vm_put(struct pvr_apple *apple, struct s8003_vm *vm) unsigned int e; for (e = 0; e < 2; e++) - s8003_pm_put(apple->chip_data, &vm->pm[e]); + s8003_pm_put(apple->chip_data, &vm->pm[e], e); } static u64 s8003_pm_set_va(const struct s8003_pm *pm, u32 page) @@ -2023,6 +2049,7 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s u32 blocks) { struct pvr_apple_s8003 *s = apple->chip_data; + unsigned int idx = pm - vm->pm; u8 *control, *list, *free, *record; u32 previous = 0, i, j, k; u64 list_va, aux_va; @@ -2033,6 +2060,8 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s if (pm->built == s->gen && pm->blocks >= blocks) return 0; if (pm->blocks < blocks) { + if (WARN_ON_ONCE(pm->shared)) + return -EINVAL; if (!pm->next_pool || pm->next_blocks < blocks) return -ENOMEM; /* This set is idle. Its previous pool can now be retired. */ @@ -2050,9 +2079,14 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s } pm->built = 0; pm->rec_seq = 0; - memset(pm->pool_map, 0, pvr_gem_object_size(pm->pool)); + if (!pm->shared || s->pm_shared[idx].zeroed != s->gen) { + memset(pm->pool_map, 0, pvr_gem_object_size(pm->pool)); + dma_sync_sgtable_for_device(apple_dev(apple), pm->pool->base.sgt, + DMA_BIDIRECTIONAL); + if (pm->shared) + s->pm_shared[idx].zeroed = s->gen; + } memset(pm->aux_map, 0, S8003_PM_AUX_SIZE); - dma_sync_sgtable_for_device(apple_dev(apple), pm->pool->base.sgt, DMA_BIDIRECTIONAL); dma_sync_sgtable_for_device(apple_dev(apple), pm->aux->base.sgt, DMA_BIDIRECTIONAL); control = s->pmsets.map + pm->set * S8003_PM_SET_SIZE; @@ -2656,6 +2690,11 @@ 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; @@ -2667,8 +2706,10 @@ static void s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) return; mutex_lock(&apple->submit_lock); - seq_printf(m, "a9 pm pools bytes %lld shared 0 (pools 0 x 128 blocks) guard_waits 0\n", - atomic64_read(&s->pm_pool_bytes)); + 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, + atomic64_read(&s->pm_guard_waits)); xa_for_each(&s->vms, index, vm) { for (i = 0; i < ARRAY_SIZE(vm->pm); i++) { struct s8003_pm *pm = &vm->pm[i]; @@ -2706,6 +2747,39 @@ static void s8003_pm_mark_ready(struct pvr_apple_s8003 *s, struct s8003_pm *pm) spin_unlock_irqrestore(&s->set_lock, flags); } +static int s8003_pm_shared_get(struct pvr_apple *apple, unsigned int i) +{ + struct pvr_apple_s8003 *s = apple->chip_data; + struct pvr_gem_object *obj; + void *map; + int err = 0; + + mutex_lock(&s->pm_shared_lock); + if (s->pm_shared[i].obj) + goto out; + obj = pvr_gem_object_create(apple->pvr_dev, + S8003_PM_BLOCKS_MAX * S8003_PM_BLOCK_SIZE, + PVR_BO_FW_FLAGS_DEVICE_CACHED); + if (IS_ERR(obj)) { + err = PTR_ERR(obj); + goto out; + } + map = pvr_gem_object_vmap(obj); + if (IS_ERR(map)) { + err = PTR_ERR(map); + pvr_gem_object_put(obj); + goto out; + } + s->pm_shared[i].obj = obj; + s->pm_shared[i].map = map; + s->pm_shared[i].zeroed = 0; + WRITE_ONCE(s->pm_shared_count, s->pm_shared_count + 1); + atomic64_add(pvr_gem_object_size(obj), &s->pm_pool_bytes); +out: + mutex_unlock(&s->pm_shared_lock); + return err; +} + static int s8003_prepare_job(struct pvr_apple *apple, struct pvr_job *job) { struct pvr_apple_s8003 *s = apple->chip_data; @@ -2767,6 +2841,29 @@ 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)) { + mutex_unlock(&apple->submit_lock); + err = s8003_pm_shared_get(apple, i); + if (err) + goto out; + mutex_lock(&apple->submit_lock); + if (!pm->pool) { + pvr_gem_object_get(s->pm_shared[i].obj); + pm->pool = s->pm_shared[i].obj; + pm->pool_map = s->pm_shared[i].map; + pm->blocks = S8003_PM_BLOCKS_MAX; + pm->shared = true; + pm->built = 0; + } + s8003_pm_mark_ready(s, pm); + mutex_unlock(&apple->submit_lock); + continue; + } + if (pm->shared) { + s8003_pm_mark_ready(s, pm); + mutex_unlock(&apple->submit_lock); + continue; + } need_pool = max(pm->blocks, pm->next_blocks) < blocks; if (!need_pool) s8003_pm_mark_ready(s, pm); @@ -2851,6 +2948,27 @@ 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"); + if (pm->shared) { + unsigned int idx = pm - vm->pm; + const struct s8003_pm *user = s->pm_shared[idx].user; + + if (user && user != pm && user->scene == s->gen) { + enum pvr_apple_engine engine = user->td_seq ? APPLE_ENGINE_FRAG : + APPLE_ENGINE_GEOM; + u32 last_seq = user->td_seq ? user->td_seq : user->ta_seq; + + if ((s32)(pvr_apple_s8003_engine_done(apple, engine) - last_seq) < 0) { + atomic64_inc(&s->pm_guard_waits); + err = s8003_wait_done(apple, engine, last_seq); + if (err) + return s8003_submit_error(apple, vm, "geometry", err, + S8003_REFUSED_PM, + "shared PM pool busy"); + } + } + s->pm_shared[idx].user = pm; + } + /* * Take the firmware's feedback. The worker does not allocate: a larger * pool is prepared for a later job (s8003_prepare_job()) and switched to @@ -3235,6 +3353,7 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) INIT_DELAYED_WORK(&s->drain_work, s8003_drain_work); spin_lock_init(&s->tag_lock); spin_lock_init(&s->set_lock); + mutex_init(&s->pm_shared_lock); xa_init(&s->vms); s->next_slot = v->slot_first; err = devm_mutex_init(dev, &s->event_lock); From 3642d7758836fb58be6abc21ac591def66e434fb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Thu, 3 Dec 2026 17:52:35 +0100 Subject: [PATCH 3/3] drm/imagination: apple: let any VM take a second PM scene while sets remain MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A VM's second PM set gives it a second scene: its next geometry job no longer waits for its last fragment job to finish. Apple keeps many scenes per work queue (one PM pool per queue, a 256-entry TA ring). The A9 path gave a VM its second set only while fewer than half of the 12 sets were taken, so with a desktop and a browser open a new VM got one. With the shared PM pools (apple_pm_shared) a set costs no pool memory. Reserve a VM's second set on demand, when its first scene is still in flight at the next geometry job, while at least S8003_PM_SPARE_SETS (2) sets stay free for new VMs' first scene. The apple_pm_scene2 module parameter (default 1) selects this; 0 keeps the half rule, which also stays in force when the pools are private (up to 16 MiB per set). On an iPad 5, a WebGPU game in Firefox goes from one set to two, 75 % of its geometry jobs start before the previous fragment job is done, and its render rate rises by 5 % (244.5 to 257.3 per second). Assisted-by: LLM Signed-off-by: Berkay Kösebay --- drivers/gpu/drm/imagination/pvr_apple_s8003.c | 45 ++++++++++++++++--- 1 file changed, 38 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c index 0b3547adf1449f..f5bf3a19e2ebd7 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -1887,9 +1887,30 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex return err; } +/* + * A VM's second PM set gives it a second scene: its next geometry job no + * longer waits for its last fragment job to finish. Apple keeps many + * scenes per work queue (one PM pool per queue, a 256-entry TA ring). With + * the shared pools (apple_pm_shared) a set costs no pool memory, so a VM + * takes its second set when it has a scene in flight at the next geometry + * job, while S8003_PM_SPARE_SETS stay free for new VMs' first scene. + */ +#define S8003_PM_SPARE_SETS 2 +static unsigned int s8003_pm_scene2 = 1; +module_param_named(apple_pm_scene2, s8003_pm_scene2, uint, 0644); +MODULE_PARM_DESC(apple_pm_scene2, + "Apple A9: a VM's second PM set: 0 = while fewer than half are taken, 1 = on demand while 2 stay free (default 1)"); + +static bool s8003_pm_idle(struct pvr_apple *apple, const struct s8003_pm *pm); + +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"); + /* * How many scenes of a VM the firmware may hold: as many as the VM has PM - * sets. Reserve the first here, and the second while more than half remain. + * sets. Reserve the first here, and the second as apple_pm_scene2 says. * IOCTL context: arming counts only sets whose resources are fully ready. */ static unsigned int s8003_vm_reserve_sets(struct pvr_apple *apple, @@ -1910,8 +1931,22 @@ static unsigned int s8003_vm_reserve_sets(struct pvr_apple *apple, n++; continue; } - if (i && bitmap_weight(s->sets, S8003_PM_SETS) >= S8003_PM_SETS / 2) - break; + if (i) { + unsigned int taken = bitmap_weight(s->sets, S8003_PM_SETS); + + /* A private pool per set costs up to 16 MiB: keep the half rule then */ + if (!READ_ONCE(s8003_pm_scene2) || !READ_ONCE(s8003_pm_shared_param)) { + if (taken >= S8003_PM_SETS / 2) + break; + } else { + if (taken + S8003_PM_SPARE_SETS >= S8003_PM_SETS) + break; + /* On demand: only while the first scene is still in flight */ + if (vm->pm[0].set < 0 || !vm->pm[0].ready || + s8003_pm_idle(apple, &vm->pm[0])) + break; + } + } /* In turn: the PM may still write a set a VM gave back */ set = find_next_zero_bit(s->sets, S8003_PM_SETS, s->next_set); if (set >= S8003_PM_SETS) @@ -2690,10 +2725,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) {