Skip to content

armgit / PS5LM

Public Imported from GitHub
0

llama.cpp on a jailbroken PS5: run any GGUF model natively on the console, starting with Qwen 3.8

App: driver and ggml switches from /data/PS5LM/env.txt; RADV patches

Mert Cobanov51eaac07869c
diff --git a/patches/mesa/0007-radv-ps5-a-destroyed-buffer-s-memory-waits-for-the-G.patch b/patches/mesa/0007-radv-ps5-a-destroyed-buffer-s-memory-waits-for-the-G.patch
new file mode 100644
index 0000000..52da88f
--- /dev/null
+++ b/patches/mesa/0007-radv-ps5-a-destroyed-buffer-s-memory-waits-for-the-G.patch
@@ -0,0 +1,101 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: ps5cemu <ps5cemu@localhost>
+Date: Sat, 3 Oct 2026 16:30:49 +0000
+Subject: [PATCH] radv/ps5: a destroyed buffer's memory waits for the GPU's
+ work
+
+amdgpu keeps a buffer that submitted work names alive in the kernel until
+that work has run, and RADV and applications rely on it. The PS5 winsys
+unmapped and released a buffer's direct memory as soon as it was destroyed,
+so work already submitted could reach unmapped memory, and the PS5's GPU
+stops on such a fault: the whole console hangs (Breath of the Wild's runes
+and scope under Cemu).
+
+A destroyed buffer's memory now waits, with the newest submission's
+sequence number, until the queue has run that far; what the GPU is done
+with is freed at each later destroy.
+---
+ src/amd/vulkan/winsys/ps5/radv_ps5_bo.c | 61 +++++++++++++++++++++++--
+ 1 file changed, 58 insertions(+), 3 deletions(-)
+
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_bo.c b/src/amd/vulkan/winsys/ps5/radv_ps5_bo.c
+index 084c8bb..90a92ae 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_bo.c
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_bo.c
+@@ -12,7 +12,51 @@
+ #include <string.h>
+
+ #include "util/os_time.h"
++#include "util/simple_mtx.h"
+ #include "util/u_atomic.h"
++#include "util/u_dynarray.h"
++
++/* The memory of a destroyed buffer, kept until the GPU has run every submission made before the
++ * destroy. amdgpu keeps a buffer that submitted work names alive in the kernel until that work is
++ * done, and RADV and applications rely on it; freed at once here, a later read or write of the
++ * GPU's reached unmapped memory, and the PS5's GPU stops on such a fault (the console hangs). */
++struct radv_ps5_zombie {
++ struct radv_ps5_memory memory;
++ void *placed;
++ uint64_t seq;
++};
++
++static simple_mtx_t radv_ps5_zombie_lock = SIMPLE_MTX_INITIALIZER;
++static struct util_dynarray radv_ps5_zombies; /* of struct radv_ps5_zombie, under the lock */
++
++static void
++radv_ps5_zombie_free(struct radv_ps5_zombie *zombie)
++{
++ if (zombie->placed)
++ radv_ps5_memory_unmap_at(zombie->placed, zombie->memory.bytes, false);
++ radv_ps5_memory_free(&zombie->memory);
++}
++
++/* Frees what the GPU is done with: everything when there is no queue. */
++static void
++radv_ps5_zombies_reap(struct radv_ps5_winsys *ws)
++{
++ const uint64_t done = ws->queue ? radv_ps5_queue_poll(ws->queue) : UINT64_MAX;
++ simple_mtx_lock(&radv_ps5_zombie_lock);
++ unsigned count = util_dynarray_num_elements(&radv_ps5_zombies, struct radv_ps5_zombie);
++ for (unsigned i = 0; i < count;) {
++ struct radv_ps5_zombie *zombie = util_dynarray_element(&radv_ps5_zombies, struct radv_ps5_zombie, i);
++ if (zombie->seq > done) {
++ i++;
++ continue;
++ }
++ radv_ps5_zombie_free(zombie);
++ *zombie = *util_dynarray_top_ptr(&radv_ps5_zombies, struct radv_ps5_zombie);
++ (void)util_dynarray_pop(&radv_ps5_zombies, struct radv_ps5_zombie);
++ count--;
++ }
++ simple_mtx_unlock(&radv_ps5_zombie_lock);
++}
+
+ /* A buffer is one direct-memory allocation, mapped for the CPU and the GPU at
+ * the same address for as long as it lives, so it is always resident, its GPU
+@@ -124,10 +168,21 @@ radv_ps5_buffer_destroy(struct radeon_winsys *rws, struct radeon_winsys_bo *base
+ p_atomic_add(&ws->allocated_vram, -(int64_t)bo->memory.bytes);
+ else
+ p_atomic_add(&ws->allocated_gtt, -(int64_t)bo->memory.bytes);
+- if (bo->placed)
+- radv_ps5_memory_unmap_at(bo->placed, bo->memory.bytes, false);
+- radv_ps5_memory_free(&bo->memory);
++ struct radv_ps5_zombie zombie = {.memory = bo->memory, .placed = bo->placed};
+ free(bo);
++ /* Work submitted before now may still use it: kept until the GPU has run it. */
++ if (ws->queue) {
++ zombie.seq = p_atomic_read(&ws->queue->submitted_seq);
++ if (radv_ps5_queue_poll(ws->queue) < zombie.seq) {
++ simple_mtx_lock(&radv_ps5_zombie_lock);
++ util_dynarray_append_typed(&radv_ps5_zombies, struct radv_ps5_zombie, zombie);
++ simple_mtx_unlock(&radv_ps5_zombie_lock);
++ radv_ps5_zombies_reap(ws);
++ return;
++ }
++ }
++ radv_ps5_zombie_free(&zombie);
++ radv_ps5_zombies_reap(ws);
+ }
+
+ static void *
diff --git a/patches/mesa/0008-radv-ps5-submissions-handed-to-a-thread-of-their-own.patch b/patches/mesa/0008-radv-ps5-submissions-handed-to-a-thread-of-their-own.patch
new file mode 100644
index 0000000..a711d51
--- /dev/null
+++ b/patches/mesa/0008-radv-ps5-submissions-handed-to-a-thread-of-their-own.patch
@@ -0,0 +1,322 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: ps5cemu <ps5cemu@localhost>
+Date: Tue, 13 Oct 2026 18:51:18 +0000
+Subject: [PATCH] radv/ps5: submissions handed to a thread of their own
+ (RADV_PS5_SUBMIT_THREAD)
+
+sceAgcDriverSubmitDcb keeps the submitting thread in the console's kernel
+for a while, and the submitting thread is Cemu's GPU thread, which records
+Breath of the Wild's 3,000 to 4,000 draws a frame and submits every 300.
+At 4K (2026-10-13) the statistics line gave agc_ms 1,300 to 1,800 in each
+10 s: 13 to 18% of the GPU thread's time, at 100 to 250 us a submission.
+The CPU-time profile of the same run shows almost none of it, so the
+thread was blocked in the call, not running. A PC's amdgpu takes a few
+microseconds a submission.
+
+With RADV_PS5_SUBMIT_THREAD=1 a submission is planned, numbered, given ring
+space and copied into the ring where it is made, as before; a thread of its
+own then flushes the words and hands them to AGC, in the order they were
+made, and the kick thread makes the suspend point after each, as before.
+Nothing else changes: a sync object is signalled by the sequence number,
+which the GPU reaches only once the thread has submitted it, and ring space
+comes back as the marker passes it. What AGC is told outside a submission
+(the clock read, the tessellation rings) waits for the thread to empty
+first, so it stays in order. A submission AGC refuses loses the device, as
+before; the thread says which in the log.
+
+The thread looks for the next submission for RADV_PS5_SUBMIT_SPIN_US
+(2000 by default) before it sleeps: while a game draws, the next comes
+within a millisecond or two, and waking a sleeping thread costs the
+submitter a system call and the GPU the wake-up's delay. Off (the
+default), submissions are made where they were.
+---
+ src/amd/vulkan/winsys/ps5/radv_ps5_cs.c | 188 +++++++++++++++++-
+ src/amd/vulkan/winsys/ps5/radv_ps5_platform.c | 6 +-
+ src/amd/vulkan/winsys/ps5/radv_ps5_platform.h | 6 +-
+ 3 files changed, 194 insertions(+), 6 deletions(-)
+
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_cs.c b/src/amd/vulkan/winsys/ps5/radv_ps5_cs.c
+index 3251c09c664..e4d6b184e30 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_cs.c
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_cs.c
+@@ -13,7 +13,9 @@
+
+ #include "util/os_time.h"
+ #include "util/u_atomic.h"
++#include "util/u_debug.h"
+ #include "util/u_math.h"
++#include "util/u_thread.h"
+ #include "sid.h"
+
+ /* Command streams. The console faults a PM4 INDIRECT_BUFFER into title memory
+@@ -312,6 +314,175 @@ radv_ps5_cs_annotate(struct ac_cmdbuf *cs, const char *marker)
+
+ /* ------------------------------------------------------------- submission */
+
++/* Submissions handed to a thread of their own (RADV_PS5_SUBMIT_THREAD=1).
++ *
++ * sceAgcDriverSubmitDcb keeps the submitting thread in the console's kernel
++ * for a while: Cemu's GPU thread, which records Breath of the Wild's 3,000 to
++ * 4,000 draws a frame and submits every 300, spent 14% of its time in it at
++ * 4K (agc_ms, 2026-10-13), most of it blocked rather than running. With the
++ * variable set, a submission is planned, numbered, given ring space and copied
++ * into the ring where it is made, as before; the thread then flushes its words
++ * and hands them to AGC, in the order they were made. Nothing else changes: a
++ * sync object is signalled by the sequence number, which the GPU reaches only
++ * once the thread has submitted it, and ring space comes back as the marker
++ * passes it, as before. RADV_PS5_SUBMIT_SPIN_US (2000 by default) is how long
++ * the thread looks for the next submission before it sleeps: while a game
++ * draws, the next comes within a millisecond or two, and a sleeping thread
++ * costs the submitter a system call to wake and the GPU the wake-up's delay. */
++#define RADV_PS5_SUBMITTER_JOBS 256u
++
++struct radv_ps5_submit_job {
++ uint32_t *words;
++ uint32_t count;
++ volatile uint32_t *marker;
++ uint32_t marker_value;
++};
++
++static struct {
++ bool enabled;
++ /* PAUSEs the thread spins for before it sleeps: about 20 ns each on the
++ * console's CPU. */
++ uint32_t spins;
++ struct radv_ps5_submit_job jobs[RADV_PS5_SUBMITTER_JOBS];
++ /* The next job the thread submits (only the thread writes it), and the
++ * next free slot (written under the queue's submit lock). */
++ uint32_t head;
++ uint32_t tail;
++ /* The thread sleeps, or is about to, on wanted. */
++ uint32_t sleeping;
++ /* Submitters waiting on done: for room, or for the thread to catch up. */
++ uint32_t waiting;
++ /* The first result AGC refused a submission with; the device is lost from
++ * then on. */
++ int32_t error;
++ mtx_t lock;
++ cnd_t wanted;
++ cnd_t done;
++} radv_ps5_submitter;
++static once_flag radv_ps5_submitter_once = ONCE_FLAG_INIT;
++
++static int
++radv_ps5_submitter_body(void *unused)
++{
++ (void)unused;
++ u_thread_setname("radv_submit");
++ uint32_t head = 0;
++ for (;;) {
++ for (uint32_t spin = radv_ps5_submitter.spins; spin && head == p_atomic_read(&radv_ps5_submitter.tail); spin--)
++ __builtin_ia32_pause();
++ if (head == p_atomic_read(&radv_ps5_submitter.tail)) {
++ mtx_lock(&radv_ps5_submitter.lock);
++ p_atomic_set(&radv_ps5_submitter.sleeping, 1);
++ /* The flag before the look at the tail: a submitter stores the tail,
++ * then looks at the flag, so one of the two sees the other's store. */
++ __sync_synchronize();
++ while (head == p_atomic_read(&radv_ps5_submitter.tail))
++ cnd_wait(&radv_ps5_submitter.wanted, &radv_ps5_submitter.lock);
++ p_atomic_set(&radv_ps5_submitter.sleeping, 0);
++ mtx_unlock(&radv_ps5_submitter.lock);
++ }
++ const struct radv_ps5_submit_job job = radv_ps5_submitter.jobs[head % RADV_PS5_SUBMITTER_JOBS];
++ if (!p_atomic_read(&radv_ps5_submitter.error)) {
++ const int result = radv_ps5_submit(job.words, job.count, job.marker, job.marker_value);
++ if (result != 0) {
++ fprintf(stderr, "radv/ps5: sceAgcDriverSubmitDcb failed: 0x%08x (on the submit thread)\n",
++ (unsigned)result);
++ p_atomic_set(&radv_ps5_submitter.error, result);
++ }
++ }
++ head++;
++ p_atomic_set(&radv_ps5_submitter.head, head);
++ /* The head before the look at the waiters, as above. */
++ __sync_synchronize();
++ if (p_atomic_read(&radv_ps5_submitter.waiting)) {
++ mtx_lock(&radv_ps5_submitter.lock);
++ cnd_broadcast(&radv_ps5_submitter.done);
++ mtx_unlock(&radv_ps5_submitter.lock);
++ }
++ }
++ return 0;
++}
++
++static void
++radv_ps5_submitter_start(void)
++{
++ if (!debug_get_bool_option("RADV_PS5_SUBMIT_THREAD", false))
++ return;
++ const int64_t spin_us = debug_get_num_option("RADV_PS5_SUBMIT_SPIN_US", 2000);
++ radv_ps5_submitter.spins = (uint32_t)CLAMP(spin_us, 0, 100000) * 50;
++ if (mtx_init(&radv_ps5_submitter.lock, mtx_plain) != thrd_success ||
++ cnd_init(&radv_ps5_submitter.wanted) != thrd_success || cnd_init(&radv_ps5_submitter.done) != thrd_success) {
++ fprintf(stderr, "radv/ps5: submissions stay on the submitting thread: the submit thread's lock was refused\n");
++ return;
++ }
++ thrd_t thread;
++ if (thrd_create(&thread, radv_ps5_submitter_body, NULL) != thrd_success) {
++ fprintf(stderr, "radv/ps5: submissions stay on the submitting thread: the submit thread was refused\n");
++ return;
++ }
++ thrd_detach(thread);
++ radv_ps5_submitter.enabled = true;
++ fprintf(stderr, "radv/ps5: submissions on a thread of their own (RADV_PS5_SUBMIT_THREAD), which looks for the next "
++ "one for %" PRId64 " us before it sleeps\n",
++ (int64_t)CLAMP(spin_us, 0, 100000));
++}
++
++/* Waits until the submit thread has submitted every job a submitter waits
++ * for: up to (not including) until. */
++static void
++radv_ps5_submitter_wait(uint32_t until)
++{
++ if ((int32_t)(until - p_atomic_read(&radv_ps5_submitter.head)) <= 0)
++ return;
++ mtx_lock(&radv_ps5_submitter.lock);
++ p_atomic_inc(&radv_ps5_submitter.waiting);
++ /* The count before the look at the head; the thread stores the head, then
++ * looks at the count. */
++ __sync_synchronize();
++ while ((int32_t)(until - p_atomic_read(&radv_ps5_submitter.head)) > 0)
++ cnd_wait(&radv_ps5_submitter.done, &radv_ps5_submitter.lock);
++ p_atomic_dec(&radv_ps5_submitter.waiting);
++ mtx_unlock(&radv_ps5_submitter.lock);
++}
++
++/* Every job submitted to AGC: before anything else reaches AGC, or an AGC
++ * setting changes for the next submissions. Called with the submit lock held,
++ * so no job is added meanwhile. */
++static void
++radv_ps5_submitter_drain(void)
++{
++ if (radv_ps5_submitter.enabled)
++ radv_ps5_submitter_wait(radv_ps5_submitter.tail);
++}
++
++/* Hands count words to the submit thread, after the jobs before them. Called
++ * with the submit lock held. Returns what AGC refused an earlier one with, or
++ * 0. */
++static int
++radv_ps5_submitter_push(uint32_t *words, uint32_t count, volatile uint32_t *marker, uint32_t marker_value)
++{
++ const uint32_t tail = radv_ps5_submitter.tail;
++ /* All the slots in use: the thread is RADV_PS5_SUBMITTER_JOBS submissions
++ * behind, so it is running, not asleep. */
++ radv_ps5_submitter_wait(tail - RADV_PS5_SUBMITTER_JOBS + 1);
++ radv_ps5_submitter.jobs[tail % RADV_PS5_SUBMITTER_JOBS] = (struct radv_ps5_submit_job){
++ .words = words,
++ .count = count,
++ .marker = marker,
++ .marker_value = marker_value,
++ };
++ /* The job before the tail (a release store), and the tail before the look
++ * at the thread's flag. */
++ p_atomic_set(&radv_ps5_submitter.tail, tail + 1);
++ __sync_synchronize();
++ if (p_atomic_read(&radv_ps5_submitter.sleeping)) {
++ mtx_lock(&radv_ps5_submitter.lock);
++ cnd_signal(&radv_ps5_submitter.wanted);
++ mtx_unlock(&radv_ps5_submitter.lock);
++ }
++ return p_atomic_read(&radv_ps5_submitter.error);
++}
++
+ /* The completion packet: one RELEASE_MEM of CACHE_FLUSH_AND_INV_TS_EVENT
+ * (event 20, index 5) that flushes the colour and depth caches, writes back and
+ * invalidates L2 and the vector caches (the GCR actions 0x30c), and only then
+@@ -358,6 +529,8 @@ radv_ps5_read_gpu_clock(void)
+ const uint64_t va = (uint64_t)(uintptr_t)value;
+
+ simple_mtx_lock(&queue->submit_lock);
++ /* Submitted here, not on the submit thread: after what it still holds. */
++ radv_ps5_submitter_drain();
+ *value = UINT64_MAX;
+ radv_ps5_cpu_flush((const void *)value, sizeof(*value));
+ words[0] = PKT3(PKT3_RELEASE_MEM, 6, 0);
+@@ -543,6 +716,9 @@ radv_ps5_cs_submit(struct radeon_winsys_ctx *rctx, const struct radv_winsys_subm
+ if (radv_ps5_cs(submit->cs_array[i])->status != VK_SUCCESS)
+ return radv_ps5_cs(submit->cs_array[i])->status;
+ }
++ call_once(&radv_ps5_submitter_once, radv_ps5_submitter_start);
++ if (p_atomic_read(&radv_ps5_submitter.error))
++ return VK_ERROR_DEVICE_LOST;
+
+ /* Waits need only their signals submitted: this queue runs in order. */
+ uint64_t depends = 0;
+@@ -555,6 +731,7 @@ radv_ps5_cs_submit(struct radeon_winsys_ctx *rctx, const struct radv_winsys_subm
+ * it, after the GPU has finished with the one it held. */
+ if (ctx->tess_factor_ring_va && (ctx->tess_factor_ring_va != queue->tess_factor_ring_va ||
+ ctx->tess_factor_ring_size != queue->tess_factor_ring_size)) {
++ radv_ps5_submitter_drain();
+ radv_ps5_queue_wait_seq(queue, queue->submitted_seq, OS_TIMEOUT_INFINITE);
+ const int set = radv_ps5_set_tess_factor_ring(ctx->tess_factor_ring_va, ctx->tess_factor_ring_size);
+ if (set != 0)
+@@ -566,6 +743,8 @@ radv_ps5_cs_submit(struct radeon_winsys_ctx *rctx, const struct radv_winsys_subm
+ /* The off-chip ring RADV allocated holds as many workgroups as its
+ * OFFCHIP_BUFFERING names; AGC starts with its own. */
+ if (ctx->tess_factor_ring_va && !queue->hs_offchip_param_set) {
++ /* For the next submissions: none made before it may reach AGC after it. */
++ radv_ps5_submitter_drain();
+ const uint32_t param = ctx->ws->info.hs_offchip_param;
+ const int set = radv_ps5_set_hs_offchip_param(G_03093C_OFFCHIP_GRANULARITY_GFX103(param),
+ G_03093C_OFFCHIP_BUFFERING_GFX103(param));
+@@ -667,13 +846,18 @@ radv_ps5_cs_submit(struct radeon_winsys_ctx *rctx, const struct radv_winsys_subm
+ for (unsigned i = 0; i < parts && submitted == 0; i++) {
+ const uint32_t start = *util_dynarray_element(&starts, uint32_t, i);
+ const uint32_t end = i + 1 < parts ? *util_dynarray_element(&starts, uint32_t, i + 1) : count;
+- submitted = radv_ps5_submit(words + start, end - start, queue->marker, (uint32_t)seq);
++ if (radv_ps5_submitter.enabled)
++ submitted = radv_ps5_submitter_push(words + start, end - start, queue->marker, (uint32_t)seq);
++ else
++ submitted = radv_ps5_submit(words + start, end - start, queue->marker, (uint32_t)seq);
+ }
+ util_dynarray_fini(&starts);
+ radv_ps5_note_submission(count, copy_start - claim_start, copy_end - copy_start);
+ simple_mtx_unlock(&queue->submit_lock);
+ if (submitted != 0) {
+- fprintf(stderr, "radv/ps5: sceAgcDriverSubmitDcb failed: 0x%08x\n", (unsigned)submitted);
++ /* The submit thread said which one AGC refused when it did. */
++ if (!radv_ps5_submitter.enabled)
++ fprintf(stderr, "radv/ps5: sceAgcDriverSubmitDcb failed: 0x%08x\n", (unsigned)submitted);
+ return VK_ERROR_DEVICE_LOST;
+ }
+ ctx->queue_used[submit->ip_type][submit->queue_index] = true;
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_platform.c b/src/amd/vulkan/winsys/ps5/radv_ps5_platform.c
+index 98783e37036..67b304f6dd7 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_platform.c
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_platform.c
+@@ -706,8 +706,10 @@ radv_ps5_submit(uint32_t *words, uint32_t count, volatile uint32_t *marker, uint
+ };
+ int32_t result = sceAgcDriverSubmitDcb(&description);
+ const uint64_t submitted = os_time_get_nano();
+- radv_ps5_submit_ns[0] += flushed - started;
+- radv_ps5_submit_ns[1] += submitted - flushed;
++ /* atomic: the submit thread (RADV_PS5_SUBMIT_THREAD) adds while a
++ * submitter takes the totals */
++ p_atomic_add(&radv_ps5_submit_ns[0], flushed - started);
++ p_atomic_add(&radv_ps5_submit_ns[1], submitted - flushed);
+ if (result != 0)
+ return result;
+ call_once(&radv_ps5_kick_once, radv_ps5_kick_start);
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_platform.h b/src/amd/vulkan/winsys/ps5/radv_ps5_platform.h
+index 68bdeb35a7c..f203fad25e0 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_platform.h
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_platform.h
+@@ -100,12 +100,14 @@ void radv_ps5_cpu_flush(const void *address, size_t bytes);
+
+ /* Submits count PM4 words at words (GPU-visible memory whose CPU lines have
+ * been flushed). The words end with a packet that writes marker_value to
+- * marker once they have run; the host model writes it at once. */
++ * marker once they have run; the host model writes it at once. Called by one
++ * thread at a time: the submitter, with the submit lock held, or the submit
++ * thread (RADV_PS5_SUBMIT_THREAD, radv_ps5_cs.c). */
+ int radv_ps5_submit(uint32_t *words, uint32_t count, volatile uint32_t *marker, uint32_t marker_value);
+
+ /* The nanoseconds radv_ps5_submit spent since the last call, in the cache
+ * flush, in sceAgcDriverSubmitDcb and in sceAgcSuspendPoint (called with the
+- * submit lock held, as radv_ps5_submit is). */
++ * submit lock held). */
+ void radv_ps5_submit_times(uint64_t times[3]);
+ /* The tessellation factor ring the GPU uses from the next submission on (AGC
+ * owns its registers); 0 or the system software's error. */
diff --git a/patches/mesa/0009-radv-ps5-the-GPU-waits-spin-and-sleep-chosen-at-run-.patch b/patches/mesa/0009-radv-ps5-the-GPU-waits-spin-and-sleep-chosen-at-run-.patch
new file mode 100644
index 0000000..3713a36
--- /dev/null
+++ b/patches/mesa/0009-radv-ps5-the-GPU-waits-spin-and-sleep-chosen-at-run-.patch
@@ -0,0 +1,103 @@
+From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
+From: ps5cemu <ps5cemu@localhost>
+Date: Tue, 13 Oct 2026 18:52:14 +0000
+Subject: [PATCH] radv/ps5: the GPU waits' spin and sleep chosen at run time,
+ and fence status without the clock
+
+A wait for the GPU checks the marker in a loop for 1.5 ms, then sleeps a
+millisecond between looks, so it wakes up to a millisecond after the GPU
+got there. Cemu waits so for the previous frame at every swap, and the
+flip thread for each frame before its flip. RADV_PS5_WAIT_SPIN_US and
+RADV_PS5_WAIT_SLEEP_US now choose the two (1500 and 1000 by default, as
+before), so an A/B on a console can try shorter sleeps without a new
+driver. This replaces patch 0003's fixed 200 us.
+
+vkGetFenceStatus waits with an absolute timeout of 0, and every one that
+found its fence not yet signalled read the monotonic clock to learn so, a
+system call on the console. Cemu asks after every submission. A zero
+timeout now answers VK_TIMEOUT without reading it, as 0003 did; the
+answer is the same.
+---
+ src/amd/vulkan/winsys/ps5/radv_ps5_sync.c | 7 ++++++
+ src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c | 28 ++++++++++++++++++---
+ 2 files changed, 31 insertions(+), 4 deletions(-)
+
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_sync.c b/src/amd/vulkan/winsys/ps5/radv_ps5_sync.c
+index a01b3bcc055..0b2276c902d 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_sync.c
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_sync.c
+@@ -140,6 +140,13 @@ radv_ps5_sync_wait_many(struct vk_device *device, uint32_t wait_count, const str
+ mtx_unlock(&queue->sync_lock);
+ return VK_SUCCESS;
+ }
++ /* A status query (vkGetFenceStatus, which Cemu makes after every
++ * submission) asks with no time at all: the answer needs no clock,
++ * which can be a system call on the console. */
++ if (abs_timeout_ns == 0) {
++ mtx_unlock(&queue->sync_lock);
++ return VK_TIMEOUT;
++ }
+ const uint64_t now = os_time_get_nano();
+ if (now >= abs_timeout_ns) {
+ mtx_unlock(&queue->sync_lock);
+diff --git a/src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c b/src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c
+index 6319a3398d8..08a9b123fa2 100644
+--- a/src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c
++++ b/src/amd/vulkan/winsys/ps5/radv_ps5_winsys.c
+@@ -16,6 +16,7 @@
+ #include "tools/radv_debug.h"
+ #include "util/os_time.h"
+ #include "util/u_atomic.h"
++#include "util/u_debug.h"
+ #include "util/u_math.h"
+ #include "ac_linux_drm.h"
+ #include "sid.h"
+@@ -85,8 +86,26 @@ radv_ps5_queue_poll(struct radv_ps5_queue *queue)
+ * a conformance run's heaviest cases need more). */
+ #define RADV_PS5_STALL_NS (UINT64_C(10) * 1000000000)
+ /* Before sleeping, the marker is checked in a loop for this long: most waits
+- * end well inside a millisecond (PS5_Vulkan R36). */
+-#define RADV_PS5_SPIN_NS UINT64_C(1500000)
++ * end well inside a millisecond (PS5_Vulkan R36). After that it is checked
++ * between sleeps, and a wait wakes up to a sleep late after the GPU got there:
++ * the flip thread waits so for each frame before its flip, and Cemu for the
++ * previous frame at every swap. RADV_PS5_WAIT_SPIN_US and
++ * RADV_PS5_WAIT_SLEEP_US change them (1500 and 1000 by default). */
++static uint64_t radv_ps5_wait_spin_ns = UINT64_C(1500000);
++static unsigned radv_ps5_wait_sleep_us = 1000;
++static once_flag radv_ps5_wait_once = ONCE_FLAG_INIT;
++
++static void
++radv_ps5_wait_configure(void)
++{
++ const int64_t spin_us = debug_get_num_option("RADV_PS5_WAIT_SPIN_US", 1500);
++ const int64_t sleep_us = debug_get_num_option("RADV_PS5_WAIT_SLEEP_US", 1000);
++ radv_ps5_wait_spin_ns = (uint64_t)CLAMP(spin_us, 0, 100000) * 1000;
++ radv_ps5_wait_sleep_us = (unsigned)CLAMP(sleep_us, 50, 10000);
++ if (spin_us != 1500 || sleep_us != 1000)
++ fprintf(stderr, "radv/ps5: a wait for the GPU spins %" PRIu64 " us, then sleeps %u us between looks\n",
++ radv_ps5_wait_spin_ns / 1000, radv_ps5_wait_sleep_us);
++}
+
+ bool
+ radv_ps5_queue_wait_seq(struct radv_ps5_queue *queue, uint64_t seq, uint64_t abs_timeout_ns)
+@@ -94,8 +113,9 @@ radv_ps5_queue_wait_seq(struct radv_ps5_queue *queue, uint64_t seq, uint64_t abs
+ uint64_t completed = radv_ps5_queue_poll(queue);
+ if (completed >= seq)
+ return true;
++ call_once(&radv_ps5_wait_once, radv_ps5_wait_configure);
+ uint64_t now = os_time_get_nano();
+- const uint64_t spin_until = now + RADV_PS5_SPIN_NS;
++ const uint64_t spin_until = now + radv_ps5_wait_spin_ns;
+ uint64_t progress_at = now;
+ uint64_t last = completed;
+ for (;;) {
+@@ -116,7 +136,7 @@ radv_ps5_queue_wait_seq(struct radv_ps5_queue *queue, uint64_t seq, uint64_t abs
+ if (now < spin_until)
+ __builtin_ia32_pause();
+ else
+- radv_ps5_sleep_us(1000);
++ radv_ps5_sleep_us(radv_ps5_wait_sleep_us);
+ }
+ }
+
diff --git a/patches/mesa/README.md b/patches/mesa/README.md
new file mode 100644
index 0000000..a8df81a
--- /dev/null
+++ b/patches/mesa/README.md
@@ -0,0 +1,19 @@
+# Driver patches
+
+Three patches to PS5_Mesa's RADV, from [PS5CEMU-HAR](https://github.com/premohq/PS5CEMU-HAR)
+(`patches/mesa`, GPL-3.0), applied on top of the Mesa revision PS5_Vulkan pins (`1ba9874`):
+
+- `0007`: a destroyed buffer's memory waits until the GPU is done with it, so freed memory
+ still in use cannot fault the GPU and stop the console. Always on.
+- `0008`: submissions handed to a thread of the driver's own (`RADV_PS5_SUBMIT_THREAD=1`).
+- `0009`: how long a wait for the GPU spins and sleeps (`RADV_PS5_WAIT_SPIN_US`,
+ `RADV_PS5_WAIT_SLEEP_US`).
+
+0008 and 0009 change nothing unless asked. On the console neither made generation faster
+(2026-10-16, Qwen3.5 0.8B and 9B), so the app does not ask. Put the switches in
+`/data/PS5LM/env.txt` to try them. To build RADV with the patches:
+
+```sh
+cd .deps/src/PS5_Mesa && git am ../../../patches/mesa/*.patch
+(cd ../PS5_Vulkan && PS5_MESA_REVISION=$(git -C ../PS5_Mesa rev-parse HEAD) tools/build-radv.sh release)
+```
diff --git a/ps5/app/main.cpp b/ps5/app/main.cpp
index f4a5b38..bedad6d 100644
--- a/ps5/app/main.cpp
+++ b/ps5/app/main.cpp
@@ -101,6 +101,7 @@ using namespace ps5lm;
constexpr const char * kLog = "/data/PS5LM/app.log";
constexpr const char * kArgs = "/data/PS5LM/app-args.txt";
constexpr const char * kChoice = "/data/PS5LM/model.txt";
+constexpr const char * kEnv = "/data/PS5LM/env.txt"; // driver and ggml switches, for measuring
constexpr const char * kTextOnly = "/data/PS5LM/text-only"; // the last load's projector failed: load without it once
constexpr const char * kQuit = "/data/PS5LM/quit";
constexpr const char * kSettings = "/data/PS5LM/settings.json";
@@ -565,6 +566,20 @@ int main(int, char **) {
setenv("radv_enable_unified_heap_on_apu", "true", 1);
setenv("HOME", "/data/PS5LM", 0);
setenv("LLAMA_CACHE", "/data/PS5LM/cache", 0);
+ // Driver and backend switches to measure with, one NAME=value a line in
+ // env.txt, set before the GPU opens; only the driver's, ggml's and llama's.
+ for (const auto & line : read_lines(kEnv)) {
+ const size_t eq = line.find('=');
+ const std::string name = line.substr(0, eq == std::string::npos ? 0 : eq);
+ bool known = false;
+ for (const char * prefix : { "RADV_", "MESA_", "ACO_", "GGML_", "LLAMA_" }) {
+ known = known || name.rfind(prefix, 0) == 0;
+ }
+ if (known) {
+ setenv(name.c_str(), line.c_str() + eq + 1, 1);
+ }
+ std::printf("ps5lm-app: env %s%s\n", line.c_str(), known ? "" : " (ignored)");
+ }
mkdir(kModels, 0777);
std::printf("ps5lm-app: start\n");