From 1f7f70e4c1779f417fa32a0795398854ab8cde25 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 01/25] drm/imagination: apple: Size an S8003 dataset's free list by its initial pages On the iPad 5, vkCreateDevice() fails with VK_ERROR_INITIALIZATION_FAILED. CREATE_HWRT_DATASET returns -EINVAL without a log line when Mesa creates the dummy render target used during device creation. The A9 allocates the parameter manager's pages itself. Its free list is not grown by pvr_free_list_create(), so current_pages stays zero, but hwrt_init_kernel_structure() still checks it against the minimum. For chips with own_pm_pages, check initial_num_pages instead. This is the limit used to size the S8003 parameter manager's render blocks. Affects the Apple A9 (S8003). The A10 is unchanged. Assisted-by: LLM --- ...apple-Size-an-S8003-dataset-s-free-l.patch | 64 +++++++++++++++++++ 1 file changed, 64 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0026-drm-imagination-apple-Size-an-S8003-dataset-s-free-l.patch diff --git a/kernel/patches/drivers/gpu/0026-drm-imagination-apple-Size-an-S8003-dataset-s-free-l.patch b/kernel/patches/drivers/gpu/0026-drm-imagination-apple-Size-an-S8003-dataset-s-free-l.patch new file mode 100644 index 00000000..3f9cc12a --- /dev/null +++ b/kernel/patches/drivers/gpu/0026-drm-imagination-apple-Size-an-S8003-dataset-s-free-l.patch @@ -0,0 +1,64 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 12:45:47 +0100 +Subject: [PATCH] drm/imagination: apple: Size an S8003 dataset's free list by + its initial pages + +On the iPad 5, vkCreateDevice() fails with VK_ERROR_INITIALIZATION_FAILED. +CREATE_HWRT_DATASET returns -EINVAL without a log line when Mesa creates +the dummy render target used during device creation. + +The A9 allocates the parameter manager's pages itself. Its free list is +not grown by pvr_free_list_create(), so current_pages stays zero, but +hwrt_init_kernel_structure() still checks it against the minimum. + +For chips with own_pm_pages, check initial_num_pages instead. This is +the limit used to size the S8003 parameter manager's render blocks. + +Affects the Apple A9 (S8003). The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_hwrt.c | 14 ++++++++++++-- + 1 file changed, 12 insertions(+), 2 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_hwrt.c b/drivers/gpu/drm/imagination/pvr_hwrt.c +index ec50237..dd91ed9 100644 +--- a/drivers/gpu/drm/imagination/pvr_hwrt.c ++++ b/drivers/gpu/drm/imagination/pvr_hwrt.c +@@ -1,6 +1,7 @@ + // SPDX-License-Identifier: GPL-2.0-only OR MIT + /* Copyright (c) 2023 Imagination Technologies Ltd. */ + ++#include "pvr_apple.h" + #include "pvr_free_list.h" + #include "pvr_hwrt.h" + #include "pvr_gem.h" +@@ -45,6 +46,7 @@ hwrt_init_kernel_structure(struct pvr_file *pvr_file, + struct pvr_hwrt_dataset *hwrt) + { + struct pvr_device *pvr_dev = pvr_file->pvr_dev; ++ u32 local_pages; + int err; + + hwrt->pvr_dev = pvr_dev; +@@ -62,8 +64,16 @@ hwrt_init_kernel_structure(struct pvr_file *pvr_file, + } + } + +- if (hwrt->free_lists[ROGUE_FW_LOCAL_FREELIST]->current_pages < +- pvr_get_free_list_min_pages(pvr_dev)) { ++ /* ++ * On a chip that allocates the parameter manager's pages itself, a ++ * free list never holds pages (pvr_apple_free_list_has_pages()); its ++ * initial size is how many a render may take. ++ */ ++ local_pages = hwrt->free_lists[ROGUE_FW_LOCAL_FREELIST]->current_pages; ++ if (pvr_device_is_apple(pvr_dev) && !pvr_apple_free_list_has_pages(pvr_dev)) ++ local_pages = hwrt->free_lists[ROGUE_FW_LOCAL_FREELIST]->initial_num_pages; ++ ++ if (local_pages < pvr_get_free_list_min_pages(pvr_dev)) { + err = -EINVAL; + goto err_put_free_lists; + } + From 81b5e548f9a30fc69500a19325729fe6840a3990 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 02/25] drm/imagination: Don't free a job's command twice when its stream is rejected After a rejected command stream on the iPad 5, the next exec in another process oopses in free_bprm with a bad rss-counter. slub_debug reports "Object already free" on kmalloc-512: the object was allocated and freed in pvr_fw_cmd_init(), then freed again from pvr_job_put(). When stream parsing fails, pvr_job_process_stream() frees the command. The caller then releases the job, whose release path frees it again. Clear the command pointer and length after freeing the rejected command. Upstream has the same code. Affects every PowerVR device. Fixes: eaf01ee5ba28 ("drm/imagination: Implement job submission and scheduling") Assisted-by: LLM --- ...Don-t-free-a-job-s-command-twice-whe.patch | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0027-drm-imagination-Don-t-free-a-job-s-command-twice-whe.patch diff --git a/kernel/patches/drivers/gpu/0027-drm-imagination-Don-t-free-a-job-s-command-twice-whe.patch b/kernel/patches/drivers/gpu/0027-drm-imagination-Don-t-free-a-job-s-command-twice-whe.patch new file mode 100644 index 00000000..a69f3590 --- /dev/null +++ b/kernel/patches/drivers/gpu/0027-drm-imagination-Don-t-free-a-job-s-command-twice-whe.patch @@ -0,0 +1,42 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 18:28:34 +0100 +Subject: [PATCH] drm/imagination: Don't free a job's command twice when its + stream is rejected + +After a rejected command stream on the iPad 5, the next exec in another +process oopses in free_bprm with a bad rss-counter. slub_debug reports +"Object already free" on kmalloc-512: the object was allocated and freed +in pvr_fw_cmd_init(), then freed again from pvr_job_put(). + +When stream parsing fails, pvr_job_process_stream() frees the command. +The caller then releases the job, whose release path frees it again. +Clear the command pointer and length after freeing the rejected command. + +Upstream has the same code. Affects every PowerVR device. + +Fixes: eaf01ee5ba28 ("drm/imagination: Implement job submission and scheduling") + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_job.c | 5 ++++- + 1 file changed, 4 insertions(+), 1 deletion(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index 95c5d2e..48a1cbc 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -79,8 +79,11 @@ pvr_job_process_stream(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_d + job->cmd_len = cmd_defs->dest_size; + + err = pvr_stream_process(pvr_dev, cmd_defs, stream, stream_size, job->cmd); +- if (err) ++ if (err) { + kfree(job->cmd); ++ job->cmd = NULL; ++ job->cmd_len = 0; ++ } + + return err; + } + From 3002362ee59b257841bc40095194b4b99543a762 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 03/25] drm/imagination: apple: Require each chip's own register extension Every geometry and fragment job on the iPad 5 is refused with -EINVAL, without a log line. The stream parser requires the A10's type-1 register extension on every Apple chip, while the A9 supplies its registers in extension type 2. Put each chip's required extension type and mask beside its stream definitions and obtain them through an Apple accessor. Keep the A10's values and leave compute without an Apple must-have entry. Affects the Apple A9 (S8003). The A10's validation values are unchanged. Assisted-by: LLM --- ...apple-Require-each-chip-s-own-regist.patch | 181 ++++++++++++++++++ 1 file changed, 181 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0028-drm-imagination-apple-Require-each-chip-s-own-regist.patch diff --git a/kernel/patches/drivers/gpu/0028-drm-imagination-apple-Require-each-chip-s-own-regist.patch b/kernel/patches/drivers/gpu/0028-drm-imagination-apple-Require-each-chip-s-own-regist.patch new file mode 100644 index 00000000..360aad59 --- /dev/null +++ b/kernel/patches/drivers/gpu/0028-drm-imagination-apple-Require-each-chip-s-own-regist.patch @@ -0,0 +1,181 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 18:28:34 +0100 +Subject: [PATCH] drm/imagination: apple: Require each chip's own register + extension + +Every geometry and fragment job on the iPad 5 is refused with -EINVAL, +without a log line. The stream parser requires the A10's type-1 register +extension on every Apple chip, while the A9 supplies its registers in +extension type 2. + +Put each chip's required extension type and mask beside its stream +definitions and obtain them through an Apple accessor. Keep the A10's +values and leave compute without an Apple must-have entry. + +Affects the Apple A9 (S8003). The A10's validation values are unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 26 +++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_apple.h | 9 +++++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 8 ++++++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 4 +++ + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 4 +++ + drivers/gpu/drm/imagination/pvr_stream.c | 11 +++++--- + 6 files changed, 58 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index e147626..b0505a2 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1636,3 +1636,29 @@ const struct pvr_stream_cmd_defs *pvr_apple_cmd_stream(struct pvr_device *pvr_de + return chip->compute_stream ?: &pvr_cmd_compute_stream; + return type == DRM_PVR_JOB_TYPE_GEOMETRY ? chip->geom_stream : chip->frag_stream; + } ++ ++/** ++ * pvr_apple_stream_musthave() - Required register extension for a chip's stream ++ * @pvr_dev: Target device. ++ * @type: Geometry or fragment stream type. ++ * @ext_type: Extension header type to which the returned mask applies. ++ * ++ * Return: the chip's required register bits, or zero for other stream types. ++ */ ++u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type type, ++ u32 *ext_type) ++{ ++ const struct pvr_apple_chip *chip = pvr_dev->device_data->apple_chip; ++ ++ if (type == PVR_STREAM_TYPE_GEOM) { ++ *ext_type = chip->geom_ext_type; ++ return chip->geom_ext_mask; ++ } ++ if (type == PVR_STREAM_TYPE_FRAG) { ++ *ext_type = chip->frag_ext_type; ++ return chip->frag_ext_mask; ++ } ++ ++ *ext_type = 0; ++ return 0; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index cdc8e96..d1de0ad 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -10,6 +10,7 @@ + #include + + #include "pvr_rogue_fwif_client.h" ++#include "pvr_stream.h" + + struct dma_fence; + struct pvr_apple_chip; +@@ -167,6 +168,8 @@ int pvr_apple_resume(struct pvr_device *pvr_dev); + void pvr_apple_device_info_init(struct pvr_device *pvr_dev); + bool pvr_apple_takes_jobs(struct pvr_device *pvr_dev); + bool pvr_apple_free_list_has_pages(struct pvr_device *pvr_dev); ++u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type type, ++ u32 *ext_type); + struct dma_fence *pvr_apple_submit_job(struct pvr_job *job); + struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, + struct pvr_vm_context *vm_ctx, u64 stream, +@@ -200,6 +203,12 @@ static inline int pvr_apple_resume(struct pvr_device *pvr_dev) { return 0; } + static inline void pvr_apple_device_info_init(struct pvr_device *pvr_dev) {} + static inline bool pvr_apple_takes_jobs(struct pvr_device *pvr_dev) { return false; } + static inline bool pvr_apple_free_list_has_pages(struct pvr_device *pvr_dev) { return true; } ++static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, ++ enum pvr_stream_type type, u32 *ext_type) ++{ ++ *ext_type = 0; ++ return 0; ++} + static inline int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) + { + return -ENODEV; +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 201d72a..d524415 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -164,6 +164,10 @@ struct pvr_apple { + * @render_job_size: Bytes @prepare_geom and @prepare_frag fill. + * @geom_stream: How a geometry job's command stream from userspace is decoded. + * @frag_stream: The same for a fragment job. ++ * @geom_ext_type: Required geometry register extension header type. ++ * @geom_ext_mask: Required bits within that geometry extension header. ++ * @frag_ext_type: Required fragment register extension header type. ++ * @frag_ext_mask: Required bits within that fragment extension header. + * @init: At probe, with Fender mapped: map and allocate what the chip needs, + * load the firmware (pvr_apple_fw_load()), set &pvr_apple.heap, + * &pvr_apple.layout and &pvr_apple.chip_data. Device-managed. +@@ -238,6 +242,10 @@ struct pvr_apple_chip { + size_t render_job_size; + const struct pvr_stream_cmd_defs *geom_stream; + const struct pvr_stream_cmd_defs *frag_stream; ++ u32 geom_ext_type; ++ u32 geom_ext_mask; ++ u32 frag_ext_type; ++ u32 frag_ext_mask; + const struct pvr_stream_cmd_defs *compute_stream; + unsigned int scenes; + bool own_pm_pages; +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 468dd71..8115176 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -2550,6 +2550,10 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .perf_state_max = S8003_PERF_STATE_MAX, + .geom_stream = &pvr_apple_g5p_cmd_geom_stream, + .frag_stream = &pvr_apple_g5p_cmd_frag_stream, ++ .geom_ext_type = PVR_STREAM_EXTHDR_TYPE_GEOM_G5P, ++ .geom_ext_mask = PVR_STREAM_EXTHDR_GEOM_G5P_REGS, ++ .frag_ext_type = PVR_STREAM_EXTHDR_TYPE_FRAG_G5P, ++ .frag_ext_mask = PVR_STREAM_EXTHDR_FRAG_G5P_REGS, + .compute_stream = &pvr_apple_g5p_cmd_compute_stream, + .scenes = 2, + .own_pm_pages = true, +diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +index f3ee833..5c62c13 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_t8010.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +@@ -461,6 +461,10 @@ const struct pvr_apple_chip pvr_apple_chip_t8010 = { + .render_job_size = sizeof(union pvr_apple_t8010_render_job), + .geom_stream = &pvr_apple_cmd_geom_stream, + .frag_stream = &pvr_apple_cmd_frag_stream, ++ .geom_ext_type = PVR_STREAM_EXTHDR_TYPE_GEOM_APPLE, ++ .geom_ext_mask = PVR_STREAM_EXTHDR_GEOM_APPLE_REGS, ++ .frag_ext_type = PVR_STREAM_EXTHDR_TYPE_FRAG_APPLE, ++ .frag_ext_mask = PVR_STREAM_EXTHDR_FRAG_APPLE_REGS, + .init = pvr_apple_t8010_init, + .boot = pvr_apple_t8010_boot, + .start = pvr_apple_t8010_start, +diff --git a/drivers/gpu/drm/imagination/pvr_stream.c b/drivers/gpu/drm/imagination/pvr_stream.c +index 7db6eb1..d83b7db 100644 +--- a/drivers/gpu/drm/imagination/pvr_stream.c ++++ b/drivers/gpu/drm/imagination/pvr_stream.c +@@ -1,6 +1,7 @@ + // SPDX-License-Identifier: GPL-2.0-only OR MIT + /* Copyright (c) 2023 Imagination Technologies Ltd. */ + ++#include "pvr_apple.h" + #include "pvr_device.h" + #include "pvr_rogue_fwif_stream.h" + #include "pvr_stream.h" +@@ -272,10 +273,12 @@ pvr_stream_create_musthave_masks(struct pvr_device *pvr_dev) + PVR_STREAM_EXTHDR_FRAG0_BRN47217; + + if (pvr_device_is_apple(pvr_dev)) { +- pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_GEOM][PVR_STREAM_EXTHDR_TYPE_GEOM_APPLE] |= +- PVR_STREAM_EXTHDR_GEOM_APPLE_REGS; +- pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_FRAG][PVR_STREAM_EXTHDR_TYPE_FRAG_APPLE] |= +- PVR_STREAM_EXTHDR_FRAG_APPLE_REGS; ++ u32 ext_type, mask; ++ ++ mask = pvr_apple_stream_musthave(pvr_dev, PVR_STREAM_TYPE_GEOM, &ext_type); ++ pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_GEOM][ext_type] |= mask; ++ mask = pvr_apple_stream_musthave(pvr_dev, PVR_STREAM_TYPE_FRAG, &ext_type); ++ pvr_dev->stream_musthave_quirks[PVR_STREAM_TYPE_FRAG][ext_type] |= mask; + } + + if (pvr_device_has_uapi_quirk(pvr_dev, 49927)) { + From 14ff1676cfaf10dfb0ec3d71ade2400dc47fb26b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 04/25] drm/imagination: apple: Drop a fragment job's reference on its geometry job Once rendering works on the iPad 5, every fragment job warns at WARN_ON(job->paired_job) in pvr_job.c, thousands of times a minute. Geometry jobs are never freed, and clients hang when destroying a render target: glmark2 and vkcube run until killed. Apple queues receive jobs linked by pvr_jobs_link_geom_frag(), but their free path does not drop the fragment job's paired geometry reference. That reference also keeps the geometry job's HWRT and context alive. Follow pvr_queue_free_job(): drop the paired geometry reference and clear paired_job before dropping the scheduler's reference. Affects the shared Apple queue used by the A9 (S8003) and A10 (T8010). The A10 has been build-tested only. Assisted-by: LLM --- ...apple-Drop-a-fragment-job-s-referenc.patch | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0029-drm-imagination-apple-Drop-a-fragment-job-s-referenc.patch diff --git a/kernel/patches/drivers/gpu/0029-drm-imagination-apple-Drop-a-fragment-job-s-referenc.patch b/kernel/patches/drivers/gpu/0029-drm-imagination-apple-Drop-a-fragment-job-s-referenc.patch new file mode 100644 index 00000000..51b02b97 --- /dev/null +++ b/kernel/patches/drivers/gpu/0029-drm-imagination-apple-Drop-a-fragment-job-s-referenc.patch @@ -0,0 +1,43 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 19:08:29 +0100 +Subject: [PATCH] drm/imagination: apple: Drop a fragment job's reference on + its geometry job + +Once rendering works on the iPad 5, every fragment job warns at +WARN_ON(job->paired_job) in pvr_job.c, thousands of times a minute. +Geometry jobs are never freed, and clients hang when destroying a +render target: glmark2 and vkcube run until killed. + +Apple queues receive jobs linked by pvr_jobs_link_geom_frag(), but their +free path does not drop the fragment job's paired geometry reference. +That reference also keeps the geometry job's HWRT and context alive. + +Follow pvr_queue_free_job(): drop the paired geometry reference and +clear paired_job before dropping the scheduler's reference. + +Affects the shared Apple queue used by the A9 (S8003) and A10 (T8010). +The A10 has been build-tested only. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_queue.c | 5 +++++ + 1 file changed, 5 insertions(+) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_queue.c b/drivers/gpu/drm/imagination/pvr_apple_queue.c +index 80bc17b..d176fe6 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_queue.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_queue.c +@@ -89,6 +89,11 @@ static void pvr_apple_free_job(struct drm_sched_job *sched_job) + struct pvr_job *job = container_of(sched_job, struct pvr_job, base); + + drm_sched_job_cleanup(sched_job); ++ ++ if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT && job->paired_job) ++ pvr_job_put(job->paired_job); ++ ++ job->paired_job = NULL; + pvr_job_put(job); + } + + From 631f82d52507fab7749048014f911ac9576fa12b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 05/25] drm/imagination: apple: Let every geometry job take its VM's scene first A client's first render on the iPad 5 fails without a log line when another client rendered last. Its geometry job returns -EBUSY, then its fragment job is refused with "fragment job without a geometry job". The call that takes a VM's PM sets is inside the dependency choice for consecutive jobs of the same VM. A new VM can therefore reach submission without a PM set when the previous fragment belonged to another VM. Call vm_scenes for every geometry job before selecting its dependency. Keep the existing prev_frag/last_frag choice and locking context. Affects the Apple A9 (S8003). The A10 has no vm_scenes callback and is unchanged. Assisted-by: LLM --- ...apple-Let-every-geometry-job-take-it.patch | 50 +++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0030-drm-imagination-apple-Let-every-geometry-job-take-it.patch diff --git a/kernel/patches/drivers/gpu/0030-drm-imagination-apple-Let-every-geometry-job-take-it.patch b/kernel/patches/drivers/gpu/0030-drm-imagination-apple-Let-every-geometry-job-take-it.patch new file mode 100644 index 00000000..2f432c80 --- /dev/null +++ b/kernel/patches/drivers/gpu/0030-drm-imagination-apple-Let-every-geometry-job-take-it.patch @@ -0,0 +1,50 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 19:32:10 +0100 +Subject: [PATCH] drm/imagination: apple: Let every geometry job take its VM's + scene first + +A client's first render on the iPad 5 fails without a log line when +another client rendered last. Its geometry job returns -EBUSY, then +its fragment job is refused with "fragment job without a geometry job". + +The call that takes a VM's PM sets is inside the dependency choice for +consecutive jobs of the same VM. A new VM can therefore reach submission +without a PM set when the previous fragment belonged to another VM. + +Call vm_scenes for every geometry job before selecting its dependency. +Keep the existing prev_frag/last_frag choice and locking context. + +Affects the Apple A9 (S8003). The A10 has no vm_scenes callback and is +unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 6 +++++- + 1 file changed, 5 insertions(+), 1 deletion(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index b0505a2..5a43e3f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1229,14 +1229,18 @@ int pvr_apple_job_add_deps(struct pvr_job *job) + { + struct pvr_apple *apple = to_apple(job->pvr_dev); + struct dma_fence *last; ++ unsigned int scenes = 1; + + if (job->type != DRM_PVR_JOB_TYPE_GEOMETRY && job->type != DRM_PVR_JOB_TYPE_FRAGMENT) + return 0; + spin_lock_irq(&apple->fence_lock); ++ /* Take this VM's first scene even when another VM rendered last. */ ++ if (job->type == DRM_PVR_JOB_TYPE_GEOMETRY && apple->chip->vm_scenes) ++ scenes = apple->chip->vm_scenes(apple, job->ctx->vm_ctx); + if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) + last = apple->last_geom; + else if (apple->chip->scenes > 1 && apple->last_frag_vm == job->ctx->vm_ctx && +- apple->chip->vm_scenes(apple, job->ctx->vm_ctx) > 1) ++ scenes > 1) + last = apple->prev_frag; + else + last = apple->last_frag; + From dd9efa632f1578b90d8944060fc8e1ade934c049 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 06/25] drm/imagination: Say why a command stream is rejected The iPad 5's rejected geometry and fragment streams return -EINVAL without explaining which check failed. Applications fail to render, and the kernel gives no values with which to diagnose the rejection. Report malformed headers, invalid destination entries, short input, unsupported extensions and missing must-have bits at the rejecting check. Include the command type, entry or extension, and checked values. Use one rate-limited warning per rejected stream; callers log short reads so that propagated errors do not produce duplicate messages. Keep successful parsing and error returns unchanged. Affects every PowerVR device. Assisted-by: LLM --- ...Say-why-a-command-stream-is-rejected.patch | 251 ++++++++++++++++++ 1 file changed, 251 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0031-drm-imagination-Say-why-a-command-stream-is-rejected.patch diff --git a/kernel/patches/drivers/gpu/0031-drm-imagination-Say-why-a-command-stream-is-rejected.patch b/kernel/patches/drivers/gpu/0031-drm-imagination-Say-why-a-command-stream-is-rejected.patch new file mode 100644 index 00000000..ed8c8522 --- /dev/null +++ b/kernel/patches/drivers/gpu/0031-drm-imagination-Say-why-a-command-stream-is-rejected.patch @@ -0,0 +1,251 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 18:29:37 +0100 +Subject: [PATCH] drm/imagination: Say why a command stream is rejected + +The iPad 5's rejected geometry and fragment streams return -EINVAL +without explaining which check failed. Applications fail to render, +and the kernel gives no values with which to diagnose the rejection. + +Report malformed headers, invalid destination entries, short input, +unsupported extensions and missing must-have bits at the rejecting +check. Include the command type, entry or extension, and checked values. +Use one rate-limited warning per rejected stream; callers log short +reads so that propagated errors do not produce duplicate messages. + +Keep successful parsing and error returns unchanged. Affects every +PowerVR device. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_stream.c | 101 +++++++++++++++++------ + 1 file changed, 77 insertions(+), 24 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_stream.c b/drivers/gpu/drm/imagination/pvr_stream.c +index d83b7db..d0c5a1e 100644 +--- a/drivers/gpu/drm/imagination/pvr_stream.c ++++ b/drivers/gpu/drm/imagination/pvr_stream.c +@@ -7,6 +7,7 @@ + #include "pvr_stream.h" + + #include ++#include + #include + #include + #include +@@ -49,6 +50,8 @@ pvr_stream_get_data(u8 *stream, u32 *stream_offset, u32 stream_size, u32 data_si + /** + * pvr_stream_process_1() - Process a single stream and fill destination structure + * @pvr_dev: Device pointer. ++ * @type: Command stream type. ++ * @ext_type: Extension header type, or -1 for the main stream. + * @stream_def: Stream definition. + * @nr_entries: Number of entries in &stream_def. + * @stream: Pointer to stream. +@@ -63,14 +66,20 @@ pvr_stream_get_data(u8 *stream, u32 *stream_offset, u32 stream_size, u32 data_si + * * -%EINVAL on malformed stream. + */ + static int +-pvr_stream_process_1(struct pvr_device *pvr_dev, const struct pvr_stream_def *stream_def, +- u32 nr_entries, u8 *stream, u32 stream_offset, u32 stream_size, ++pvr_stream_process_1(struct pvr_device *pvr_dev, enum pvr_stream_type type, int ext_type, ++ const struct pvr_stream_def *stream_def, u32 nr_entries, u8 *stream, ++ u32 stream_offset, u32 stream_size, + u8 *dest, u32 dest_size, u32 *stream_offset_out) + { + int err = 0; + + for (u32 i = 0; i < nr_entries; i++) { ++ u32 data_size = 0; ++ + if (stream_def[i].offset >= dest_size) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: destination type=%u ext_type=%d entry=%u offset=%u dest_size=%u\n", ++ type, ext_type, i, stream_def[i].offset, dest_size); + err = -EINVAL; + break; + } +@@ -80,41 +89,42 @@ pvr_stream_process_1(struct pvr_device *pvr_dev, const struct pvr_stream_def *st + + switch (stream_def[i].size) { + case PVR_STREAM_SIZE_8: ++ data_size = sizeof(u8); + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u8), + sizeof(u8), dest + stream_def[i].offset); +- if (err) +- return err; + break; + + case PVR_STREAM_SIZE_16: ++ data_size = sizeof(u16); + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u16), + sizeof(u16), dest + stream_def[i].offset); +- if (err) +- return err; + break; + + case PVR_STREAM_SIZE_32: ++ data_size = sizeof(u32); + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32), + sizeof(u32), dest + stream_def[i].offset); +- if (err) +- return err; + break; + + case PVR_STREAM_SIZE_64: ++ data_size = sizeof(u64); + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u64), + sizeof(u64), dest + stream_def[i].offset); +- if (err) +- return err; + break; + + case PVR_STREAM_SIZE_ARRAY: ++ data_size = stream_def[i].array_size; + err = pvr_stream_get_data(stream, &stream_offset, stream_size, + stream_def[i].array_size, sizeof(u64), + dest + stream_def[i].offset); +- if (err) +- return err; + break; + } ++ if (err) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: short data type=%u ext_type=%d entry=%u offset=%u size=%u stream_size=%u\n", ++ type, ext_type, i, stream_offset, data_size, stream_size); ++ return err; ++ } + } + + if (stream_offset_out) +@@ -143,18 +153,30 @@ pvr_stream_process_ext_stream(struct pvr_device *pvr_dev, + + err = pvr_stream_get_data(ext_stream, &stream_offset, ext_stream_size, sizeof(u32), + sizeof(ext_header), &ext_header); +- if (err) ++ if (err) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: short extension header type=%u ext_type=-1 entry=-1 offset=%u size=%zu stream_size=%u\n", ++ cmd_defs->type, stream_offset, sizeof(ext_header), ext_stream_size); + return err; ++ } + + type = (ext_header & PVR_STREAM_EXTHDR_TYPE_MASK) >> PVR_STREAM_EXTHDR_TYPE_SHIFT; + data = ext_header & PVR_STREAM_EXTHDR_DATA_MASK; + +- if (type >= cmd_defs->ext_nr_headers) ++ if (type >= cmd_defs->ext_nr_headers) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: extension type=%u ext_type=%u ext_nr_headers=%u\n", ++ cmd_defs->type, type, cmd_defs->ext_nr_headers); + return -EINVAL; ++ } + + header = &cmd_defs->ext_headers[type]; +- if (data & ~header->valid_mask) ++ if (data & ~header->valid_mask) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: extension bits type=%u ext_type=%u data=0x%08x valid_mask=0x%08x\n", ++ cmd_defs->type, type, data, header->valid_mask); + return -EINVAL; ++ } + + musthave_masks[type] &= ~data; + +@@ -165,10 +187,15 @@ pvr_stream_process_ext_stream(struct pvr_device *pvr_dev, + continue; + + /* Quirk 0: the extension is not tied to a hardware quirk. */ +- if (ext_def->quirk && !pvr_device_has_uapi_quirk(pvr_dev, ext_def->quirk)) ++ if (ext_def->quirk && !pvr_device_has_uapi_quirk(pvr_dev, ext_def->quirk)) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: unsupported quirk type=%u ext_type=%u entry=%u quirk=%u data=0x%08x mask=0x%08x\n", ++ cmd_defs->type, type, i, ext_def->quirk, data, ext_def->header_mask); + return -EINVAL; ++ } + +- err = pvr_stream_process_1(pvr_dev, ext_def->stream, ext_def->stream_len, ++ err = pvr_stream_process_1(pvr_dev, cmd_defs->type, type, ++ ext_def->stream, ext_def->stream_len, + ext_stream, stream_offset, + ext_stream_size, dest, + cmd_defs->dest_size, &stream_offset); +@@ -182,8 +209,12 @@ pvr_stream_process_ext_stream(struct pvr_device *pvr_dev, + * for this command was not present. + */ + for (u32 i = 0; i < cmd_defs->ext_nr_headers; i++) { +- if (musthave_masks[i]) ++ if (musthave_masks[i]) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: missing must-have type=%u ext_type=%u leftover_mask=0x%08x\n", ++ cmd_defs->type, i, musthave_masks[i]); + return -EINVAL; ++ } + } + + return 0; +@@ -213,13 +244,21 @@ pvr_stream_process(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs + u32 padding; + int err; + +- if (!stream || !stream_size) ++ if (!stream || !stream_size) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: empty input type=%u stream_present=%u stream_size=%u\n", ++ cmd_defs->type, !!stream, stream_size); + return -EINVAL; ++ } + + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32), + sizeof(u32), &main_stream_len); +- if (err) ++ if (err) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: short main_stream_len type=%u ext_type=-1 entry=-1 offset=%u size=%zu stream_size=%u\n", ++ cmd_defs->type, stream_offset, sizeof(u32), stream_size); + return err; ++ } + + /* + * u32 after stream length is padding to ensure u64 alignment, but may be used for expansion +@@ -227,13 +266,22 @@ pvr_stream_process(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs + */ + err = pvr_stream_get_data(stream, &stream_offset, stream_size, sizeof(u32), + sizeof(u32), &padding); +- if (err) ++ if (err) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: short padding type=%u ext_type=-1 entry=-1 offset=%u size=%zu stream_size=%u\n", ++ cmd_defs->type, stream_offset, sizeof(u32), stream_size); + return err; ++ } + +- if (main_stream_len < stream_offset || main_stream_len > stream_size || padding) ++ if (main_stream_len < stream_offset || main_stream_len > stream_size || padding) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: main header type=%u main_stream_len=%u stream_size=%u padding=0x%08x\n", ++ cmd_defs->type, main_stream_len, stream_size, padding); + return -EINVAL; ++ } + +- err = pvr_stream_process_1(pvr_dev, cmd_defs->main_stream, cmd_defs->main_stream_len, ++ err = pvr_stream_process_1(pvr_dev, cmd_defs->type, -1, ++ cmd_defs->main_stream, cmd_defs->main_stream_len, + stream, stream_offset, main_stream_len, dest_out, + cmd_defs->dest_size, &stream_offset); + if (err) +@@ -250,8 +298,13 @@ pvr_stream_process(struct pvr_device *pvr_dev, const struct pvr_stream_cmd_defs + * quirks for this command. + */ + for (u32 i = 0; i < cmd_defs->ext_nr_headers; i++) { +- if (pvr_dev->stream_musthave_quirks[cmd_defs->type][i]) ++ if (pvr_dev->stream_musthave_quirks[cmd_defs->type][i]) { ++ dev_warn_ratelimited(from_pvr_device(pvr_dev)->dev, ++ "stream rejected: no extension must-have type=%u ext_type=%u leftover_mask=0x%08x\n", ++ cmd_defs->type, i, ++ pvr_dev->stream_musthave_quirks[cmd_defs->type][i]); + return -EINVAL; ++ } + } + } + From 637a573d06ea01556d5a4a4a3ed283032c969928 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 21:27:08 +0100 Subject: [PATCH 07/25] drm/imagination: apple: Say why an S8003 job was not submitted Render failures on the iPad 5 can leave a fragment job without its geometry job. S8003 submission errors otherwise return silently, so the kernel does not say which engine or stage failed. Add one rate-limited warning at each failing submission stage with the engine, error, VM and reason. Separate the PM pool build and record slot wait checks so that their errors can be distinguished. Keep submission and recovery behaviour unchanged. A fragment without its geometry scene is still refused. Affects the Apple A9 (S8003). The A10 is unchanged. Assisted-by: LLM --- ...apple-Say-why-an-S8003-job-was-not-s.patch | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0032-drm-imagination-apple-Say-why-an-S8003-job-was-not-s.patch diff --git a/kernel/patches/drivers/gpu/0032-drm-imagination-apple-Say-why-an-S8003-job-was-not-s.patch b/kernel/patches/drivers/gpu/0032-drm-imagination-apple-Say-why-an-S8003-job-was-not-s.patch new file mode 100644 index 00000000..82d05d7b --- /dev/null +++ b/kernel/patches/drivers/gpu/0032-drm-imagination-apple-Say-why-an-S8003-job-was-not-s.patch @@ -0,0 +1,149 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 19:32:10 +0100 +Subject: [PATCH] drm/imagination: apple: Say why an S8003 job was not + submitted + +Render failures on the iPad 5 can leave a fragment job without its +geometry job. S8003 submission errors otherwise return silently, so +the kernel does not say which engine or stage failed. + +Add one rate-limited warning at each failing submission stage with the +engine, error, VM and reason. Separate the PM pool build and record +slot wait checks so that their errors can be distinguished. + +Keep submission and recovery behaviour unchanged. A fragment without +its geometry scene is still refused. + +Affects the Apple A9 (S8003). The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 49 +++++++++++++------ + 1 file changed, 33 insertions(+), 16 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index e4868b6..b31ec3e 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -2262,6 +2262,15 @@ static bool s8003_pm_idle(struct pvr_apple *apple, const struct s8003_pm *pm) + return false; + } + ++static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, ++ const char *engine, int err, const char *reason) ++{ ++ dev_warn_ratelimited(apple_dev(apple), ++ "%s job not submitted: error=%d VM=%p: %s\n", ++ engine, err, vm->vm_ctx, reason); ++ return err; ++} ++ + static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) + { + const struct pvr_hwrt_dataset *hwrt = job->hwrt->hwrt_dataset; +@@ -2275,7 +2284,7 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + + err = s8003_rt_derive(hwrt, &rt); + if (err) +- return err; ++ return s8003_submit_error(apple, vm, "geometry", err, "rt derive"); + /* The VM's sets were taken when the job was ordered (pvr_apple_s8003_vm_scenes()) */ + for (i = 0; i < 2 && !pm; i++) + if (vm->pm[i].set >= 0 && s8003_pm_idle(apple, &vm->pm[i])) +@@ -2289,7 +2298,7 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + !s8003_wait_done(apple, APPLE_ENGINE_GEOM, vm->pm[i].ta_seq)) + pm = &vm->pm[i]; + if (!pm) +- return -EBUSY; ++ return s8003_submit_error(apple, vm, "geometry", -EBUSY, "no PM set"); + + /* As many 128 KiB blocks as the free list has 4 KiB pages, and enough for the target */ + blocks = max(DIV_ROUND_UP(hwrt->free_lists[0]->initial_num_pages, 32), +@@ -2136,9 +2145,11 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + err = s8003_pm_build(apple, vm, pm, min(blocks, S8003_PM_BLOCKS_MAX)); ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "PM pool build failed"); + /* The record slot of the job before the last one is free again */ +- if (!err && apple->ta_seq) ++ if (apple->ta_seq) + err = s8003_wait_done(apple, APPLE_ENGINE_GEOM, apple->ta_seq - 1); + if (err) +- return err; ++ return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); + + seq = apple->ta_seq + 1; + s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); +@@ -2320,7 +2331,9 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + pm->td_seq = 0; + apple->ta_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_GEOM, seq, (2 * seq) % S8003_CH_ENTRIES); +- return err ?: seq; ++ if (err) ++ return s8003_submit_error(apple, vm, "geometry", err, "publish"); ++ return seq; + } + + static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struct pvr_job *job) +@@ -2337,21 +2350,21 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + for (i = 0; i < 2; i++) + if (vm->pm[i].scene == s->gen && vm->pm[i].hwrt == job->hwrt && !vm->pm[i].td_seq) + pm = &vm->pm[i]; +- if (!pm) { +- dev_warn_ratelimited(apple_dev(apple), +- "fragment job without a geometry job: not run\n"); +- return -ECANCELED; +- } ++ if (!pm) ++ return s8003_submit_error(apple, vm, "fragment", -ECANCELED, ++ "its geometry job failed or was not submitted"); + err = s8003_rt_derive(job->hwrt->hwrt_dataset, &rt); +- if (!err && apple->frag_seq) ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "rt derive"); ++ if (apple->frag_seq) + err = s8003_wait_done(apple, APPLE_ENGINE_FRAG, apple->frag_seq - 1); + if (err) +- return err; ++ return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); + + seq = apple->frag_seq + 1; + err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); + if (err) +- return err; ++ return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); + s8003_job_write(p, s8003_channel_addr(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, +@@ -2359,7 +2372,9 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + pm->td_seq = seq; + apple->frag_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); +- return err ?: seq; ++ if (err) ++ return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ return seq; + } + + /* +@@ -2382,7 +2397,7 @@ static int s8003_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + + err = s8003_wait_done(apple, APPLE_ENGINE_COMPUTE, apple->last_seq); + if (err) +- return err; ++ return s8003_submit_error(apple, vm, "compute", err, "record slot wait"); + seq = apple->last_seq + 1; + { + const u32 useq[] = { +@@ -2428,7 +2443,9 @@ static int s8003_submit_compute(struct pvr_apple *apple, struct s8003_vm *vm, st + put32(p + S8003_CH_RING + ((seq - 1) % S8003_CH_ENTRIES) * 4, w); + apple->last_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_COMPUTE, seq, seq % S8003_CH_ENTRIES); +- return err ?: seq; ++ if (err) ++ return s8003_submit_error(apple, vm, "compute", err, "publish"); ++ return seq; + } + + static int pvr_apple_s8003_submit(struct pvr_apple *apple, struct pvr_job *job) + From 3bec95f46f804148d9a9c2f7e2a2bb166040e92c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 11:03:58 +0100 Subject: [PATCH 08/25] drm/imagination: apple: Keep VM tag bumps off an S8003 slot pair while it loads The iPad 5 loses Firefox's WebGL context under multiple GPU clients. Firmware slot loads return -EIO, geometry jobs fail, and their fragment jobs are refused. Single-client runs do not hit the failure. VM map and unmap flushes take tag_lock but not submit_lock. They can bump a slot's host tag while DC_SLOT loads its root. With two or more bumps, firmware can copy an intermediate tag that matches neither the loaded tag nor the current host tag, falsely failing the load check. Mark each slot as loading under tag_lock while publishing its root and tag. Defer flushes on those slots, verify the exact loaded tag, then clear loading and apply any pending bump through the shared helper on both success and failure. submit_lock keeps the VM's job behind the pending bump, so firmware still invalidates stale translations before using the slot. Report the slot, loaded, firmware and current tags on a mismatch. Affects: Apple A9 (S8003). The A10 is unchanged. Assisted-by: LLM --- ...ff-an-S8003-slot-pair-while-it-loads.patch | 129 ++++++++++++++++++ 1 file changed, 129 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0033-drm-imagination-apple-Keep-VM-tag-bumps-off-an-S8003-slot-pair-while-it-loads.patch diff --git a/kernel/patches/drivers/gpu/0033-drm-imagination-apple-Keep-VM-tag-bumps-off-an-S8003-slot-pair-while-it-loads.patch b/kernel/patches/drivers/gpu/0033-drm-imagination-apple-Keep-VM-tag-bumps-off-an-S8003-slot-pair-while-it-loads.patch new file mode 100644 index 00000000..e05d3253 --- /dev/null +++ b/kernel/patches/drivers/gpu/0033-drm-imagination-apple-Keep-VM-tag-bumps-off-an-S8003-slot-pair-while-it-loads.patch @@ -0,0 +1,129 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 11:03:58 +0100 +Subject: [PATCH] drm/imagination: apple: Keep VM tag bumps off an S8003 slot + pair while it loads + +The iPad 5 loses Firefox's WebGL context under multiple GPU clients. +Firmware slot loads return -EIO, geometry jobs fail, and their fragment +jobs are refused. Single-client runs do not hit the failure. + +VM map and unmap flushes take tag_lock but not submit_lock. They can +bump a slot's host tag while DC_SLOT loads its root. With two or more +bumps, firmware can copy an intermediate tag that matches neither the +loaded tag nor the current host tag, falsely failing the load check. + +Mark each slot as loading under tag_lock while publishing its root and +tag. Defer flushes on those slots, verify the exact loaded tag, then +clear loading and apply any pending bump through the shared helper on +both success and failure. submit_lock keeps the VM's job behind the +pending bump, so firmware still invalidates stale translations before +using the slot. Report the slot, loaded, firmware and current tags on +a mismatch. + +Affects: Apple A9 (S8003). The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 55 ++++++++++++++----- + 1 file changed, 41 insertions(+), 14 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index bc6f300..40ca888 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -276,6 +276,8 @@ struct pvr_apple_s8003 { + u32 gen; + spinlock_t tag_lock; /* @tag and the host's tags in the shared memory */ + u32 tag[S8003_SLOTS]; /* 0: slot not loaded */ ++ DECLARE_BITMAP(loading, S8003_SLOTS); /* tag held until DC_SLOT completes */ ++ DECLARE_BITMAP(bump_pending, S8003_SLOTS); /* flush deferred while loading */ + DECLARE_BITMAP(slots, S8003_SLOTS); + DECLARE_BITMAP(parked, S8003_SLOTS); /* left on @park_vm's tables */ + u32 next_slot; +@@ -1315,6 +1317,21 @@ static int s8003_device_control(struct pvr_apple *apple, const u32 (*cmd)[2], un + S8003_DC_TIMEOUT_US, false, apple, ctl); + } + ++/* Bump a loaded slot's tag with tag_lock held; leave empty slots at zero. */ ++static void s8003_slot_tag_bump(struct pvr_apple_s8003 *s, u32 slot) ++{ ++ u32 tag = s->tag[slot]; ++ ++ lockdep_assert_held(&s->tag_lock); ++ if (!tag) ++ return; ++ tag += S8003_TAG_CATALOGUE; ++ if (!tag) ++ tag = S8003_TAG_CATALOGUE; ++ s->tag[slot] = tag; ++ put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, tag); ++} ++ + /* Point a pair of slots at a page-table root (0: at none) under @tag */ + static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, u32 tag) + { +@@ -1326,6 +1343,7 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + + spin_lock_irqsave(&s->tag_lock, flags); + for (e = 0; e < 2; e++) { ++ __set_bit(slot[e], s->loading); + put64(s8003_object(s, S8003_MMU_ROOT) + 8 * slot[e], root); + put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot[e], tag); + s->tag[slot[e]] = tag; +@@ -1333,16 +1351,30 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + spin_unlock_irqrestore(&s->tag_lock, flags); + + err = s8003_device_control(apple, cmd, 2); +- /* The firmware copies the tag it loaded the root under */ ++ if (err) ++ dev_err(apple_dev(apple), "GFX firmware slot load failed: %d\n", err); ++ /* The firmware must copy the tag we held stable while loading the root. */ + for (e = 0; e < 2 && !err; e++) { + u32 seen = get32(s8003_object(s, S8003_TAG_SEEN) + 4 * slot[e]); + +- if (seen != tag && seen != READ_ONCE(s->tag[slot[e]])) ++ if (seen != tag) { ++ dev_err(apple_dev(apple), ++ "GFX firmware did not load slot %u: loaded tag %#x, firmware saw %#x, now %#x\n", ++ slot[e], tag, seen, READ_ONCE(s->tag[slot[e]])); + err = -EIO; ++ } + } +- if (err) +- dev_err(apple_dev(apple), "GFX firmware did not load slots %u and %u: %d\n", +- slot[0], slot[1], err); ++ /* The attach holds submit_lock: deferred flushes precede the VM's job. */ ++ spin_lock_irqsave(&s->tag_lock, flags); ++ for (e = 0; e < 2; e++) { ++ __clear_bit(slot[e], s->loading); ++ if (test_bit(slot[e], s->bump_pending)) { ++ s8003_slot_tag_bump(s, slot[e]); ++ __clear_bit(slot[e], s->bump_pending); ++ } ++ } ++ spin_unlock_irqrestore(&s->tag_lock, flags); ++ wmb(); + return err; + } + +@@ -1406,15 +1438,10 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + return; + spin_lock_irqsave(&s->tag_lock, flags); + for (slot = S8003_SLOT_FIRST; slot < S8003_SLOTS; slot++) { +- u32 tag = s->tag[slot]; +- +- if (!tag) +- continue; +- tag += S8003_TAG_CATALOGUE; +- if (!tag) +- tag = S8003_TAG_CATALOGUE; +- s->tag[slot] = tag; +- put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, tag); ++ if (test_bit(slot, s->loading)) ++ __set_bit(slot, s->bump_pending); ++ else ++ s8003_slot_tag_bump(s, slot); + } + spin_unlock_irqrestore(&s->tag_lock, flags); + wmb(); From e316c2146250465c74f67ce5c88ca8b343a57f48 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:19 +0100 Subject: [PATCH 09/25] drm/imagination: apple: End S8003 waits when firmware reports a fault Firmware stops processing jobs after a fault, but internal record and PM waits spent five seconds polling an unreachable stamp. End waits on the existing fault latch and return -EIO unless the requested stamp actually arrived. Keep the latch until a new boot. Affects: Apple A9 (S8003); A10 unchanged Assisted-by: LLM --- ...apple-End-S8003-waits-when-firmware-.patch | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch diff --git a/kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch b/kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch new file mode 100644 index 00000000..0fedfdec --- /dev/null +++ b/kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch @@ -0,0 +1,59 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:19 +0100 +Subject: [PATCH 33/40] drm/imagination: apple: End S8003 waits when firmware + reports a fault + +Firmware stops processing jobs after a fault, but internal record and +PM waits spent five seconds polling an unreachable stamp. End waits on +the existing fault latch and return -EIO unless the requested stamp +actually arrived. Keep the latch until a new boot. + +Affects: Apple A9 (S8003); A10 unchanged +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 15 +++++++++++---- + 1 file changed, 11 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index bc6f300..e5be557 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -941,7 +941,7 @@ static int s8003_ring_discard(u8 *ctl) + * Read what the firmware posted, in the order Apple's kext does: the log + * lanes, the events, statistics, trace. A full ring stalls the firmware. + * Events other than "alive" and a job's stamp are a fault or a recovery +- * request: they are logged, and a job the firmware gave up times out. An ++ * request: they are logged, and end a wait for a job the firmware gave up. An + * error is a ring whose indices are not the host's and the firmware's any + * more. + */ +@@ -973,7 +973,8 @@ static int s8003_drain(struct pvr_apple *apple, bool *alive) + if (type == S8003_EVENT_ALIVE) { + *alive = true; + } else if (type != S8003_EVENT_STAMP) { +- dev_warn_ratelimited(apple_dev(apple), "firmware event %u: %*ph\n", type, ++ dev_warn_ratelimited(apple_dev(apple), "firmware fault event type=%u slot=%u: %*ph\n", ++ type, get_unaligned_le32(e + 0x0c), + S8003_EVENT_SIZE - 4, e + 4); + WRITE_ONCE(s->fault, true); + } +@@ -1249,10 +1250,16 @@ static u32 s8003_done_poll(struct pvr_apple *apple, enum pvr_apple_engine engine + + static int s8003_wait_done(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 seq) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; + u32 done; ++ int err; + +- return read_poll_timeout(s8003_done_poll, done, (s32)(done - seq) >= 0, 20, +- S8003_JOB_TIMEOUT_US, false, apple, engine); ++ err = read_poll_timeout(s8003_done_poll, done, ++ (s32)(done - seq) >= 0 || READ_ONCE(s->fault), 20, ++ S8003_JOB_TIMEOUT_US, false, apple, engine); ++ if (!err && (s32)(done - seq) < 0) ++ return -EIO; ++ return err; + } + + /* From 71fdc452af973cc2bd45794163c9c818aa810c1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 10/25] drm/imagination: apple: Capture licensed MMU faults before reset A texture read at VA 0xffffffc0 stopped rendering on the iPad 5. Share the decoder and once-per-boot report so the VA, access, requester and context are captured before reset. Read only e150/e158 after checking Fender state, transition bits and an optional chip-supplied highest state. Refuse capture without a licence. Both fault edges and internal wait failures use this path. Affects: Apple A9 (S8003); chips without a licence callback do not read RGX Assisted-by: LLM --- ...apple-Capture-licensed-MMU-faults-be.patch | 273 ++++++++++++++++++ 1 file changed, 273 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch new file mode 100644 index 00000000..5376d44d --- /dev/null +++ b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch @@ -0,0 +1,273 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:19 +0100 +Subject: [PATCH 34/40] drm/imagination: apple: Capture licensed MMU faults + before reset + +A texture read at VA 0xffffffc0 stopped rendering on the iPad 5. Share +the decoder and once-per-boot report so the VA, access, requester and +context are captured before reset. Read only e150/e158 after checking +Fender state, transition bits and an optional chip-supplied highest +state. Refuse capture without a licence. Both fault edges and internal +wait failures use this path. + +Affects: Apple A9 (S8003); chips without a licence callback do not read RGX +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 144 ++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 5 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 13 +- + 3 files changed, 160 insertions(+), 2 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 5a43e3f..96097fc 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -680,6 +680,149 @@ static const struct dma_fence_ops pvr_apple_fence_ops = { + .get_timeline_name = pvr_apple_fence_timeline_name, + }; + ++/* MMU status fields and requestor names from the G5P fault decoder. */ ++struct pvr_apple_mmu_fault { ++ bool valid, read; ++ u8 type, level, requester, sideband, context; ++ u64 va; ++}; ++ ++static struct pvr_apple_mmu_fault pvr_apple_mmu_fault_decode(u64 s) ++{ ++ struct pvr_apple_mmu_fault f = { ++ .valid = s & 1, ++ .type = (s >> 1) & 3, ++ .read = (s >> 3) & 1, ++ .level = (s >> 4) & 3, ++ .requester = (s >> 6) & 0x3f, ++ .sideband = (s >> 12) & 0xff, ++ .context = (s >> 20) & 0xff, ++ .va = (s >> 24) & 0xfffffffff0ULL, ++ }; ++ ++ return f; ++} ++ ++static const char *const pvr_apple_mmu_fault_types[4] = { ++ "page-invalid", ++ "type1", ++ "read-only", ++ "pm-meta-protected", ++}; ++ ++/* Requester names, iOS kext table 6ff5ed8 (16-byte {name, class} entries; read from the j71t ++ * 16.7.10 kernelcache). A larger index prints its number only. ++ */ ++static const char *const pvr_apple_mmu_requesters[] = { ++ "META", ++ "TLA", ++ "HOST", ++ "VDMM", ++ "CDM", ++ "IPP", ++ "PM", ++ "TILING", ++ "MCU_TEXAS0", ++ "PDS_TEXAS0", ++ "PBE0_TEXAS0", ++ "PBE1_TEXAS0", ++ "VDMS_BLACKPEARL0", ++ "IPF_BLACKPEARL0", ++ "ISP_BLACKPEARL0", ++ "TPF_BLACKPEARL0", ++ "USCS_BLACKPEARL0", ++ "PPP_BLACKPEARL0", ++ "VCE_BLACKPEARL0", ++ "FBCDC_BLACKPEARL0", ++ "MCU_TEXAS1", ++ "PDS_TEXAS1", ++ "PBE0_TEXAS1", ++ "PBE1_TEXAS1", ++ "MCU_TEXAS2", ++ "PDS_TEXAS2", ++ "PBE0_TEXAS2", ++ "PBE1_TEXAS2", ++ "VDMS_BLACKPEARL1", ++ "IPF_BLACKPEARL1", ++ "ISP_BLACKPEARL1", ++ "TPF_BLACKPEARL1", ++ "USCS_BLACKPEARL1", ++ "PPP_BLACKPEARL1", ++ "VCE_BLACKPEARL1", ++ "FBCDC_BLACKPEARL1", ++ "MCU_TEXAS3", ++ "PDS_TEXAS3", ++ "PBE0_TEXAS3", ++ "PBE1_TEXAS3", ++ "MCU_TEXAS4", ++ "PDS_TEXAS4", ++ "PBE0_TEXAS4", ++ "PBE1_TEXAS4", ++ "VDMS_BLACKPEARL2", ++ "IPF_BLACKPEARL2", ++ "ISP_BLACKPEARL2", ++ "TPF_BLACKPEARL2", ++ "USCS_BLACKPEARL2", ++ "PPP_BLACKPEARL2", ++ "VCE_BLACKPEARL2", ++ "FBCDC_BLACKPEARL2", ++ "MCU_TEXAS5", ++ "PDS_TEXAS5", ++ "PBE0_TEXAS5", ++ "PBE1_TEXAS5", ++}; ++ ++static const char *pvr_apple_mmu_requester(u32 requester) ++{ ++ if (requester >= ARRAY_SIZE(pvr_apple_mmu_requesters)) ++ return "?"; ++ return pvr_apple_mmu_requesters[requester]; ++} ++ ++/* The firmware stopped on a fault: iOS reads these banks before resetting it. */ ++void pvr_apple_report_mmu_fault(struct pvr_apple *apple) ++{ ++ u32 ctrl, power, state, highest; ++ const char *reason; ++ unsigned int bank; ++ ++ if (xchg(&apple->fault_reported, true)) ++ return; ++ if (!apple->chip->max_power_state) { ++ dev_warn(apple_dev(apple), "GPU MMU fault licence refused: no licence for this chip\n"); ++ return; ++ } ++ ctrl = readl(apple->fender + FENDER_CTRL); ++ power = readl(apple->fender + FENDER_POWER_STATE); ++ state = power & 0xf; ++ highest = apple->chip->max_power_state(apple); ++ if (ctrl & BIT(29)) ++ reason = "power-down in progress"; ++ else if (power & FENDER_POWER_REQUEST) ++ reason = "power request in flight"; ++ else if (!state || state > highest) ++ reason = "state outside epoch range"; ++ else ++ reason = NULL; ++ if (reason) { ++ dev_warn(apple_dev(apple), ++ "GPU MMU fault licence refused: %s (ctrl=%#x power=%#x max=%u)\n", ++ reason, ctrl, power, highest); ++ return; ++ } ++ ++ for (bank = 0; bank < 2; bank++) { ++ u64 status = pvr_cr_read64(apple->pvr_dev, 0xe150 + bank * 8); ++ struct pvr_apple_mmu_fault f = pvr_apple_mmu_fault_decode(status); ++ ++ dev_warn(apple_dev(apple), ++ "GPU MMU fault bank=%u va=0x%llx %s type=%s level=%u requestor=%u/%s context=%u valid=%u sideband=%u\n", ++ bank, f.va, f.read ? "read" : "write", pvr_apple_mmu_fault_types[f.type], ++ f.level, f.requester, pvr_apple_mmu_requester(f.requester), f.context, ++ f.valid, f.sideband); ++ } ++} ++ + /* + * Consume the firmware's events. Completion is taken from the stamps, so the + * events are only acknowledged: a full event ring stalls the firmware. +@@ -1319,6 +1462,7 @@ static int pvr_apple_fw_start(struct pvr_apple *apple) + apple->frag_seq = 0; + apple->frag_events = 0; + apple->geom_pending = false; ++ apple->fault_reported = false; + return apple->chip->start(apple); + } + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index d524415..663e409 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -39,6 +39,7 @@ struct pvr_stream_cmd_defs; + * the GPU's OPP table, the state number as opp-level; without one the GPU + * runs in the chip's perf_state_max. + */ ++#define FENDER_CTRL 0x0000 + #define FENDER_POWER_STATE 0x1000 + #define FENDER_POWER_STATE_MASK 0x1f + #define FENDER_POWER_REQUEST 0x10 /* | target state */ +@@ -85,6 +86,7 @@ struct pvr_apple { + void *heap; /* memory shared with the firmware: rings, stamps, commands */ + struct pvr_apple_layout layout; + bool booted; ++ bool fault_reported; /* once per firmware boot */ + bool fw_ready; /* init data handed over once: runtime PM restarts it */ + + struct mutex submit_lock; /* job slots, rings, producer index, GPU VM */ +@@ -218,6 +220,7 @@ struct pvr_apple { + * reaches when it is finished, or a negative error. + * @engine_done: The sequence number of the last finished job of @engine. No + * lock held; may be called with interrupts off. ++ * @max_power_state: Highest state licensed for fault capture; optional. + * @events: Read what the firmware posted. Before @engine_done wherever a job + * is waited for; may sleep. + * @vm_init: A userspace VM was created: map what the firmware needs in it. +@@ -268,6 +271,7 @@ struct pvr_apple_chip { + + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); + u32 (*engine_done)(struct pvr_apple *apple, enum pvr_apple_engine engine); ++ u32 (*max_power_state)(struct pvr_apple *apple); + void (*events)(struct pvr_apple *apple); + int (*vm_init)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + void (*vm_fini)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); +@@ -286,6 +290,7 @@ static inline struct device *apple_dev(struct pvr_apple *apple) + return from_pvr_device(apple->pvr_dev)->dev; + } + ++void pvr_apple_report_mmu_fault(struct pvr_apple *apple); + int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size); + int pvr_apple_wait_idle(struct pvr_apple *apple, unsigned int timeout_us); + int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index e5be557..23483f8 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -71,7 +71,6 @@ + * Fender beyond the power state. The boot stream is a list of Rogue register + * writes, two words each; Fender runs it when the core's power state leaves 0. + */ +-#define FENDER_CTRL 0x0000 + #define FENDER_CTRL_BOOT 0x40000000 /* as iOS sets it before a boot */ + #define FENDER_STREAM_STATE 0x0010 /* cleared before the stream is stored */ + #define FENDER_WAKE 0x0014 +@@ -1257,8 +1256,10 @@ static int s8003_wait_done(struct pvr_apple *apple, enum pvr_apple_engine engine + err = read_poll_timeout(s8003_done_poll, done, + (s32)(done - seq) >= 0 || READ_ONCE(s->fault), 20, + S8003_JOB_TIMEOUT_US, false, apple, engine); +- if (!err && (s32)(done - seq) < 0) ++ if (!err && (s32)(done - seq) < 0) { ++ pvr_apple_report_mmu_fault(apple); + return -EIO; ++ } + return err; + } + +@@ -2569,6 +2570,13 @@ static void pvr_apple_s8003_restore_image(struct pvr_apple *apple) + /* start() copies the image and builds the init data anew each time */ + } + ++static u32 pvr_apple_s8003_max_power_state(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return get32(s8003_object(s, S8003_PLATFORM) + 0x710); ++} ++ + const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .fw_name = "apple/gfx-s8003.bin", + .perf_state_max = S8003_PERF_STATE_MAX, +@@ -2592,6 +2600,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .report_hang = pvr_apple_s8003_report_hang, + .submit = pvr_apple_s8003_submit, + .engine_done = pvr_apple_s8003_engine_done, ++ .max_power_state = pvr_apple_s8003_max_power_state, + .events = pvr_apple_s8003_events, + .vm_init = pvr_apple_s8003_vm_init, + .vm_fini = pvr_apple_s8003_vm_fini, From 4eb6a5563f5620e3a434bb64339cc46fa0d5438a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 11/25] drm/imagination: apple: Honour the minimum runtime gate-off interval A quick idle suspend and resume can restart firmware before the chip's minimum gate-off interval expires. Record successful power-off time and wait only its remaining interval before powering up. Retain reset's existing delay. Chips with no minimum are unaffected. Affects: Apple A9 (S8003); A10 has no gate-off minimum Assisted-by: LLM --- ...apple-Honour-the-minimum-runtime-gat.patch | 57 +++++++++++++++++++ 1 file changed, 57 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch b/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch new file mode 100644 index 00000000..9ab8d12b --- /dev/null +++ b/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch @@ -0,0 +1,57 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:19 +0100 +Subject: [PATCH 35/40] drm/imagination: apple: Honour the minimum runtime + gate-off interval + +A quick idle suspend and resume can restart firmware before the chip's +minimum gate-off interval expires. Record successful power-off time and +wait only its remaining interval before powering up. Retain reset's +existing delay. Chips with no minimum are unaffected. + +Affects: Apple A9 (S8003); A10 has no gate-off minimum +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 9 +++++++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + + 2 files changed, 10 insertions(+) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 96097fc..ea1150d 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -387,6 +387,13 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) + struct pvr_apple *apple = to_apple(pvr_dev); + int err; + ++ if (apple->chip->gate_off_ms && apple->gate_off) { ++ s64 remaining = (s64)apple->chip->gate_off_ms * USEC_PER_MSEC - ++ ktime_us_delta(ktime_get(), apple->gate_off); ++ ++ if (remaining > 0) ++ msleep(DIV_ROUND_UP(remaining, USEC_PER_MSEC)); ++ } + if (!apple->fw_ready) + return pvr_apple_boot(apple); + +@@ -429,6 +436,8 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + /* the pollers run only while fences are pending; none are */ + hrtimer_cancel(&apple->poll_timer); + cancel_delayed_work_sync(&apple->poll_work); ++ if (apple->chip->gate_off_ms) ++ apple->gate_off = ktime_get(); + return 0; + } + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 663e409..e223cf8 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -88,6 +88,7 @@ struct pvr_apple { + bool booted; + bool fault_reported; /* once per firmware boot */ + bool fw_ready; /* init data handed over once: runtime PM restarts it */ ++ ktime_t gate_off; /* last successful runtime power-off */ + + struct mutex submit_lock; /* job slots, rings, producer index, GPU VM */ + u32 last_seq; /* last submitted compute job */ From b58b01e7d9b922c1e6a64cfbb2eb9bec42a7cd43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 12/25] drm/imagination: apple: Keep VM tags distinct from firmware's cache A slot tag can wrap onto firmware's cached value, making a page-table change invisible and retaining stale translations. Share the helper that skips zero and the cached tag, retaining unsigned wrap and the steps-past counter. Leave A10's generation bump unchanged until its cached-generation offset is known. Affects: Apple A9 (S8003); A10 unchanged Assisted-by: LLM --- ...apple-Keep-VM-tags-distinct-from-fir.patch | 86 +++++++++++++++++++ 1 file changed, 86 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch new file mode 100644 index 00000000..ffe871ff --- /dev/null +++ b/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch @@ -0,0 +1,86 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:20 +0100 +Subject: [PATCH 36/40] drm/imagination: apple: Keep VM tags distinct from + firmware's cache + +A slot tag can wrap onto firmware's cached value, making a page-table +change invisible and retaining stale translations. Share the helper +that skips zero and the cached tag, retaining unsigned wrap and the +steps-past counter. Leave A10's generation bump unchanged until its +cached-generation offset is known. + +Affects: Apple A9 (S8003); A10 unchanged +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 9 +++++++++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 16 ++++++++++++---- + 2 files changed, 21 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index e223cf8..7c49546 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -280,6 +280,15 @@ struct pvr_apple_chip { + void (*mmu_flush)(struct pvr_apple *apple); + }; + ++/* A new host tag must be nonzero and differ from the firmware's cached tag. */ ++static inline u32 pvr_apple_tag_bump(u32 tag, u32 step, u32 cached) ++{ ++ tag += step; ++ while (!tag || tag == cached) ++ tag += step; ++ return tag; ++} ++ + /* The heap is coherent memory shared with the firmware, not MMIO. */ + static inline u32 heap_read32(struct pvr_apple *apple, u32 offset) + { +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 23483f8..6a4affa 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -275,6 +275,7 @@ struct pvr_apple_s8003 { + u32 gen; + spinlock_t tag_lock; /* @tag and the host's tags in the shared memory */ + u32 tag[S8003_SLOTS]; /* 0: slot not loaded */ ++ u64 tag_steps_past; /* cached or zero tags skipped, under tag_lock */ + DECLARE_BITMAP(slots, S8003_SLOTS); + DECLARE_BITMAP(parked, S8003_SLOTS); /* left on @park_vm's tables */ + u32 next_slot; +@@ -1409,23 +1410,30 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + struct pvr_apple_s8003 *s = apple->chip_data; + unsigned long flags; + u32 slot; ++ u64 steps = 0, total; + + if (!s->shared.map) + return; + spin_lock_irqsave(&s->tag_lock, flags); + for (slot = S8003_SLOT_FIRST; slot < S8003_SLOTS; slot++) { +- u32 tag = s->tag[slot]; ++ u32 tag = s->tag[slot], cached, next; + + if (!tag) + continue; +- tag += S8003_TAG_CATALOGUE; +- if (!tag) +- tag = S8003_TAG_CATALOGUE; ++ cached = get32(s8003_object(s, S8003_TAG_SEEN) + 4 * slot); ++ next = pvr_apple_tag_bump(tag, S8003_TAG_CATALOGUE, cached); ++ steps += (u32)(next - (tag + S8003_TAG_CATALOGUE)) / S8003_TAG_CATALOGUE; ++ tag = next; + s->tag[slot] = tag; + put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, tag); + } ++ s->tag_steps_past += steps; ++ total = s->tag_steps_past; + spin_unlock_irqrestore(&s->tag_lock, flags); + wmb(); ++ if (steps) ++ dev_dbg_ratelimited(apple_dev(apple), ++ "MMU tag steps-past=%llu total=%llu\n", steps, total); + } + + static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) From 2496c8f873e25d2a382b75abd79d12bad0a1c244 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 13/25] drm/imagination: Cap native BO memory by device and creator Native creation pins all backing pages, so a client can exhaust memory needed to recover. Charge before taking pages and refuse above device and per-TGID budgets with -ENOMEM and a ratelimited diagnostic. Share accounting across the creator's DRM files, uncharge at free and exclude imports. Defaults are 50 percent of RAM and 75 percent of that device budget per client. Create-time charging counts real backing here; it can move to residency when eviction becomes available. Affects: every PowerVR device Assisted-by: LLM --- ...Cap-native-BO-memory-by-device-and-c.patch | 233 ++++++++++++++++++ 1 file changed, 233 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch b/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch new file mode 100644 index 00000000..6ec86676 --- /dev/null +++ b/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch @@ -0,0 +1,233 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:20 +0100 +Subject: [PATCH 37/40] drm/imagination: Cap native BO memory by device and + creator + +Native creation pins all backing pages, so a client can exhaust memory +needed to recover. Charge before taking pages and refuse above device +and per-TGID budgets with -ENOMEM and a ratelimited diagnostic. Share +accounting across the creator's DRM files, uncharge at free and exclude +imports. Defaults are 50 percent of RAM and 75 percent of that device +budget per client. Create-time charging counts real backing here; it +can move to residency when eviction becomes available. + +Affects: every PowerVR device +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_device.c | 3 + + drivers/gpu/drm/imagination/pvr_device.h | 7 ++ + drivers/gpu/drm/imagination/pvr_gem.c | 120 ++++++++++++++++++++++- + drivers/gpu/drm/imagination/pvr_gem.h | 4 + + 4 files changed, 133 insertions(+), 1 deletion(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c +index 2c06a4a..c13a1b6 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.c ++++ b/drivers/gpu/drm/imagination/pvr_device.c +@@ -804,6 +804,9 @@ pvr_device_init(struct pvr_device *pvr_dev) + struct device *dev = drm_dev->dev; + int err; + ++ spin_lock_init(&pvr_dev->gem_pin_lock); ++ INIT_LIST_HEAD(&pvr_dev->gem_pin_accounts); ++ + /* Get the platform-specific data based on the compatible string. */ + pvr_dev->device_data = of_device_get_match_data(dev); + +diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h +index 26b589e..da999d4 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.h ++++ b/drivers/gpu/drm/imagination/pvr_device.h +@@ -92,6 +92,13 @@ struct pvr_device { + */ + struct drm_device base; + ++ /** @gem_pin_lock: Protects the GEM pin budget and per-process accounts. */ ++ spinlock_t gem_pin_lock; ++ /** @gem_pin_accounts: Live BO owners, keyed by their TGID's struct pid. */ ++ struct list_head gem_pin_accounts; ++ /** @gem_pin_bytes: Native BO bytes reserved before backing pages exist. */ ++ u64 gem_pin_bytes; ++ + /** @gpu_id: GPU ID detected at runtime. */ + struct pvr_gpu_id gpu_id; + +diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c +index 817b47d..7604c59 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.c ++++ b/drivers/gpu/drm/imagination/pvr_gem.c +@@ -17,17 +17,130 @@ + #include + #include + #include ++#include + #include ++#include ++#include ++#include ++#include + #include + #include + #include + #include + ++/* Creation-time reservation; resident-page accounting can replace this later. */ ++static unsigned int pinned_device_pct = 50; ++module_param(pinned_device_pct, uint, 0644); ++MODULE_PARM_DESC(pinned_device_pct, "Maximum native BO memory as a percentage of RAM (default 50)"); ++ ++static unsigned int pinned_client_pct = 75; ++module_param(pinned_client_pct, uint, 0644); ++MODULE_PARM_DESC(pinned_client_pct, "Maximum per-process share of the device BO cap (default 75)"); ++ ++/* A held struct pid prevents PID reuse from merging unrelated clients' charges. */ ++struct pvr_gem_account { ++ struct list_head link; ++ struct pid *tgid; ++ u64 bytes; ++ unsigned int objects; ++}; ++ ++static int pvr_gem_pin_charge(struct pvr_gem_object *pvr_obj) ++{ ++ struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); ++ struct pvr_device *pvr_dev = to_pvr_device(obj->dev); ++ struct pvr_gem_account *account, *found, *fresh = NULL; ++ struct pid *tgid = task_tgid(current); ++ u64 cap = div_u64(((u64)totalram_pages() << PAGE_SHIFT) * ++ min(READ_ONCE(pinned_device_pct), 100U), 100); ++ u64 client_cap = div_u64(cap * min(READ_ONCE(pinned_client_pct), 100U), 100); ++ u64 device_bytes, client_bytes; ++ unsigned long flags; ++ int err = 0; ++ ++ for (;;) { ++ spin_lock_irqsave(&pvr_dev->gem_pin_lock, flags); ++ found = NULL; ++ list_for_each_entry(account, &pvr_dev->gem_pin_accounts, link) { ++ if (account->tgid == tgid) { ++ found = account; ++ break; ++ } ++ } ++ device_bytes = pvr_dev->gem_pin_bytes; ++ client_bytes = found ? found->bytes : 0; ++ if (device_bytes > cap || obj->size > cap - device_bytes || ++ client_bytes > client_cap || obj->size > client_cap - client_bytes) { ++ spin_unlock_irqrestore(&pvr_dev->gem_pin_lock, flags); ++ err = -ENOMEM; ++ break; ++ } ++ if (!found && !fresh) { ++ spin_unlock_irqrestore(&pvr_dev->gem_pin_lock, flags); ++ fresh = kzalloc_obj(*fresh); ++ if (!fresh) ++ return -ENOMEM; ++ fresh->tgid = get_pid(tgid); ++ continue; ++ } ++ if (!found) { ++ found = fresh; ++ fresh = NULL; ++ list_add_tail(&found->link, &pvr_dev->gem_pin_accounts); ++ } ++ pvr_dev->gem_pin_bytes += obj->size; ++ found->bytes += obj->size; ++ found->objects++; ++ pvr_obj->pin_account = found; ++ spin_unlock_irqrestore(&pvr_dev->gem_pin_lock, flags); ++ break; ++ } ++ if (fresh) { ++ put_pid(fresh->tgid); ++ kfree(fresh); ++ } ++ if (err) { ++ char comm[TASK_COMM_LEN]; ++ ++ get_task_comm(comm, current->group_leader); ++ dev_warn_ratelimited(obj->dev->dev, ++ "pin cap refused BO=%zuK client=%s[%d] device=%lluK/%lluK client=%lluK/%lluK\n", ++ obj->size >> 10, comm, task_tgid_nr(current), device_bytes >> 10, ++ cap >> 10, client_bytes >> 10, client_cap >> 10); ++ } ++ return err; ++} ++ ++static void pvr_gem_pin_uncharge(struct pvr_gem_object *pvr_obj) ++{ ++ struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); ++ struct pvr_device *pvr_dev = to_pvr_device(obj->dev); ++ struct pvr_gem_account *account = pvr_obj->pin_account; ++ unsigned long flags; ++ bool last; ++ ++ if (!account) ++ return; ++ spin_lock_irqsave(&pvr_dev->gem_pin_lock, flags); ++ pvr_dev->gem_pin_bytes -= obj->size; ++ account->bytes -= obj->size; ++ last = !--account->objects; ++ if (last) ++ list_del(&account->link); ++ pvr_obj->pin_account = NULL; ++ spin_unlock_irqrestore(&pvr_dev->gem_pin_lock, flags); ++ if (last) { ++ put_pid(account->tgid); ++ kfree(account); ++ } ++} ++ + static void pvr_gem_object_free(struct drm_gem_object *obj) + { + struct drm_gem_shmem_object *shmem_obj = to_drm_gem_shmem_obj(obj); + + shmem_obj->pages_mark_dirty_on_put = true; ++ pvr_gem_pin_uncharge(gem_to_pvr_gem(obj)); + drm_gem_shmem_free(shmem_obj); + } + +@@ -381,6 +494,11 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) + pvr_obj = shmem_gem_to_pvr_gem(shmem_obj); + pvr_obj->flags = flags; + ++ /* Imports also use gem_create_object; only native BOs reserve pages here. */ ++ err = pvr_gem_pin_charge(pvr_obj); ++ if (err) ++ goto err_shmem_object_free; ++ + sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); + if (IS_ERR(sgt)) { + err = PTR_ERR(sgt); +@@ -398,7 +516,7 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) + return pvr_obj; + + err_shmem_object_free: +- drm_gem_shmem_free(shmem_obj); ++ pvr_gem_object_put(pvr_obj); + + return ERR_PTR(err); + } +diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h +index efcd270..2e7cf2c 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.h ++++ b/drivers/gpu/drm/imagination/pvr_gem.h +@@ -27,6 +27,7 @@ + /* Forward declaration from "pvr_device.h". */ + struct pvr_device; + struct pvr_file; ++struct pvr_gem_account; + + /** + * DOC: Flags for DRM_IOCTL_PVR_CREATE_BO (kernel-only) +@@ -109,6 +110,9 @@ struct pvr_gem_object { + */ + u64 flags; + ++ /** @pin_account: Creating process's budget, retained until the BO is freed. */ ++ struct pvr_gem_account *pin_account; ++ + }; + + static_assert(offsetof(struct pvr_gem_object, base) == 0, From e8f2130a313c80f22b796ee1259f7d9968f489d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 14/25] drm/imagination: apple: Report each VM's initial PM pool size Pool-limit failures cannot be attributed without the sizes real scenes ask the PM to hold. Log VM, blocks, clamp, tiles and initial free-list pages once per VM when its first pool is built. Reuse existing values and add no register access. Affects: Apple A9 (S8003); A10 unchanged Assisted-by: LLM --- ...apple-Report-each-VM-s-initial-PM-po.patch | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch diff --git a/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch b/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch new file mode 100644 index 00000000..19048873 --- /dev/null +++ b/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch @@ -0,0 +1,43 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 03:01:44 +0100 +Subject: [PATCH 38/40] drm/imagination: apple: Report each VM's initial PM + pool size + +Pool-limit failures cannot be attributed without the sizes real scenes +ask the PM to hold. Log VM, blocks, clamp, tiles and initial free-list +pages once per VM when its first pool is built. Reuse existing values +and add no register access. + +Affects: Apple A9 (S8003); A10 unchanged +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 8 ++++++++ + 1 file changed, 8 insertions(+) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 6a4affa..829487f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -256,6 +256,7 @@ struct s8003_vm { + u32 slot[2]; /* geometry and compute, fragment; 0: none */ + u32 attached; /* the firmware start the slots were loaded for */ + struct s8003_pm pm[2]; ++ bool pm_logged; /* first pool sizing report for this VM */ + }; + + struct pvr_apple_s8003 { +@@ -2159,6 +2160,13 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + blocks = max(DIV_ROUND_UP(hwrt->free_lists[0]->initial_num_pages, 32), + DIV_ROUND_UP(50 + rt.tiles, 32)); + err = s8003_pm_build(apple, vm, pm, min(blocks, S8003_PM_BLOCKS_MAX)); ++ if (!err && !vm->pm_logged) { ++ dev_info(apple_dev(apple), ++ "PM pool VM=%p blocks=%u clamped=%u tiles=%u initial_num_pages=%u\n", ++ vm->vm_ctx, pm->blocks, blocks > S8003_PM_BLOCKS_MAX, rt.tiles, ++ hwrt->free_lists[0]->initial_num_pages); ++ vm->pm_logged = true; ++ } + if (err) + return s8003_submit_error(apple, vm, "geometry", err, "PM pool build failed"); + /* The record slot of the job before the last one is free again */ From d0059d55407324fb74330eaf72117a1841c01c75 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 15/25] drm/imagination: apple: Reset promptly after a firmware fault A firmware fault stopped the iPad 5 GPU, but submitted jobs waited for the five-second scheduler timeout. Fault the one scheduler owning the oldest bound hardware fence; its reset cancels all pending engines. Bind scheduler metadata under the fence lock and retain normal timeout coverage for unbound fences. Mark the selected fence so the timeout handler says the job was lost to a firmware fault. Keep context failure, stop, reset and restart unchanged. A10 can opt in once its fault event types are confirmed. Affects: Apple A9 (S8003); A10 event policy unchanged Assisted-by: LLM --- ...apple-Reset-promptly-after-a-firmwar.patch | 222 ++++++++++++++++++ 1 file changed, 222 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch diff --git a/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch b/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch new file mode 100644 index 00000000..aa3e7f85 --- /dev/null +++ b/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch @@ -0,0 +1,222 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 03:02:23 +0100 +Subject: [PATCH 39/40] drm/imagination: apple: Reset promptly after a firmware + fault + +A firmware fault stopped the iPad 5 GPU, but submitted jobs waited for +the five-second scheduler timeout. Fault the one scheduler owning the +oldest bound hardware fence; its reset cancels all pending engines. +Bind scheduler metadata under the fence lock and retain normal timeout +coverage for unbound fences. Mark the selected fence so the timeout +handler says the job was lost to a firmware fault. Keep context failure, +stop, reset and restart unchanged. A10 can opt in once its fault event +types are confirmed. + +Affects: Apple A9 (S8003); A10 event policy unchanged +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 78 +++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 7 ++ + drivers/gpu/drm/imagination/pvr_apple_queue.c | 7 +- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 6 +- + 4 files changed, 96 insertions(+), 2 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index ea1150d..990ad40 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -50,6 +50,9 @@ + struct pvr_apple_fence { + struct dma_fence base; + struct list_head node; ++ struct drm_gpu_scheduler *sched; ++ ktime_t submitted; ++ bool firmware_fault; + }; + + /* +@@ -321,6 +324,7 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + err = devm_mutex_init(dev, &apple->pm_lock); + if (err) + return err; ++ atomic_set(&apple->fault_pending, 0); + spin_lock_init(&apple->fence_lock); + spin_lock_init(&apple->event_lock); + for (i = 0; i < APPLE_ENGINES; i++) +@@ -689,6 +693,33 @@ static const struct dma_fence_ops pvr_apple_fence_ops = { + .get_timeline_name = pvr_apple_fence_timeline_name, + }; + ++bool pvr_apple_fence_faulted(struct dma_fence *base) ++{ ++ struct pvr_apple_fence *fence; ++ ++ if (!base || base->ops != &pvr_apple_fence_ops) ++ return false; ++ fence = container_of(base, struct pvr_apple_fence, base); ++ return READ_ONCE(fence->firmware_fault); ++} ++ ++/* Bind only hardware fences returned by a successful Apple submission. */ ++void pvr_apple_fence_bind(struct pvr_device *pvr_dev, struct dma_fence *base, ++ struct drm_gpu_scheduler *sched) ++{ ++ struct pvr_apple *apple = to_apple(pvr_dev); ++ struct pvr_apple_fence *fence; ++ unsigned long flags; ++ ++ if (!base || base->ops != &pvr_apple_fence_ops) ++ return; ++ fence = container_of(base, struct pvr_apple_fence, base); ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ fence->sched = sched; ++ fence->submitted = ktime_get(); ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++} ++ + /* MMU status fields and requestor names from the G5P fault decoder. */ + struct pvr_apple_mmu_fault { + bool valid, read; +@@ -832,6 +863,51 @@ void pvr_apple_report_mmu_fault(struct pvr_apple *apple) + } + } + ++/* Called once when a chip latches a fault, after capturing its MMU state. */ ++void pvr_apple_firmware_fault(struct pvr_apple *apple) ++{ ++ atomic_set(&apple->fault_pending, 1); ++ mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++} ++ ++/* A reset cancels every engine: fault only the oldest bound scheduler. */ ++static void pvr_apple_fault_pending(struct pvr_apple *apple) ++{ ++ static const char *const names[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = "geometry", ++ [APPLE_ENGINE_FRAG] = "fragment", ++ [APPLE_ENGINE_COMPUTE] = "compute", ++ }; ++ struct pvr_apple_fence *fence, *oldest = NULL; ++ struct drm_gpu_scheduler *sched = NULL; ++ unsigned long flags; ++ u64 seqno = 0; ++ int engine, selected = 0; ++ ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ fence = list_first_entry_or_null(&apple->pending[engine], ++ struct pvr_apple_fence, node); ++ if (!fence || !fence->sched) ++ continue; ++ if (!oldest || ktime_before(fence->submitted, oldest->submitted)) { ++ oldest = fence; ++ selected = engine; ++ } ++ } ++ if (oldest) { ++ WRITE_ONCE(oldest->firmware_fault, true); ++ sched = oldest->sched; ++ seqno = oldest->base.seqno; ++ } ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++ if (sched) { ++ dev_warn(apple_dev(apple), "firmware fault: resetting via %s job seqno=%llu\n", ++ names[selected], seqno); ++ drm_sched_fault(sched); ++ } ++} ++ + /* + * Consume the firmware's events. Completion is taken from the stamps, so the + * events are only acknowledged: a full event ring stalls the firmware. +@@ -915,6 +991,8 @@ static void pvr_apple_poll_work(struct work_struct *work) + apple->chip->events(apple); + else + pvr_apple_ack_events(apple); ++ if (atomic_xchg(&apple->fault_pending, 0)) ++ pvr_apple_fault_pending(apple); + spin_lock_irq(&apple->fence_lock); + for (engine = 0; engine < APPLE_ENGINES; engine++) { + u32 done = pvr_apple_engine_done(apple, engine); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 7c49546..09ecce9 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -17,6 +17,7 @@ + #include "pvr_apple.h" + #include "pvr_device.h" + ++#include + #include + #include + #include +@@ -28,6 +29,7 @@ + #include + + struct devfreq; ++struct drm_gpu_scheduler; + struct pvr_gem_object; + struct pvr_stream_cmd_defs; + +@@ -110,6 +112,7 @@ struct pvr_apple { + u64 fence_context; /* first of APPLE_ENGINES */ + spinlock_t fence_lock; + struct list_head pending[3]; /* struct pvr_apple_fence, by seq */ ++ atomic_t fault_pending; /* a chip latched a new firmware fault */ + struct delayed_work poll_work; + struct hrtimer poll_timer; /* rearms poll_work while jobs are pending */ + /* +@@ -301,6 +304,10 @@ static inline struct device *apple_dev(struct pvr_apple *apple) + } + + void pvr_apple_report_mmu_fault(struct pvr_apple *apple); ++void pvr_apple_firmware_fault(struct pvr_apple *apple); ++bool pvr_apple_fence_faulted(struct dma_fence *fence); ++void pvr_apple_fence_bind(struct pvr_device *pvr_dev, struct dma_fence *fence, ++ struct drm_gpu_scheduler *sched); + int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size); + int pvr_apple_wait_idle(struct pvr_apple *apple, unsigned int timeout_us); + int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_queue.c b/drivers/gpu/drm/imagination/pvr_apple_queue.c +index d176fe6..065dad1 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_queue.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_queue.c +@@ -57,6 +57,8 @@ static struct dma_fence *pvr_apple_run_job(struct drm_sched_job *sched_job) + fence = pvr_apple_submit(job); + if (IS_ERR(fence)) + pvr_power_put(job->pvr_dev); ++ else ++ pvr_apple_fence_bind(job->pvr_dev, fence, sched_job->sched); + return fence; + } + +@@ -75,7 +77,10 @@ static enum drm_gpu_sched_stat pvr_apple_timedout_job(struct drm_sched_job *sche + * puts it back, and drm_sched_start() completes it and the others + * from their cancelled fences, so that free_job() runs for each. + */ +- drm_err(from_pvr_device(job->pvr_dev), "Apple GPU job timed out\n"); ++ if (pvr_apple_fence_faulted(sched_job->s_fence->parent)) ++ drm_err(from_pvr_device(job->pvr_dev), "Apple GPU job lost to firmware fault\n"); ++ else ++ drm_err(from_pvr_device(job->pvr_dev), "Apple GPU job timed out\n"); + pvr_apple_report_hang(job->pvr_dev, sched_job->s_fence->parent); + atomic_set(&job->ctx->faulty, 1); + drm_sched_stop(sched, sched_job); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 829487f..2be9500 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -977,7 +977,11 @@ static int s8003_drain(struct pvr_apple *apple, bool *alive) + dev_warn_ratelimited(apple_dev(apple), "firmware fault event type=%u slot=%u: %*ph\n", + type, get_unaligned_le32(e + 0x0c), + S8003_EVENT_SIZE - 4, e + 4); +- WRITE_ONCE(s->fault, true); ++ if (!READ_ONCE(s->fault)) { ++ WRITE_ONCE(s->fault, true); ++ pvr_apple_report_mmu_fault(apple); ++ pvr_apple_firmware_fault(apple); ++ } + } + consumer = (consumer + 1) % APPLE_RING_ENTRIES; + ring_consume(ctl, consumer); From 11b87a1404b20aab83f75e156acca078fc5514ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 03:21:20 +0100 Subject: [PATCH 16/25] drm/imagination: apple: Stop firmware before releasing its pages Driver removal freed firmware storage while the DMA writer still ran. Stop firmware and pollers before releasing pages, and defer an idle firmware restart until a job needs it. A blocked submit ioctl also held reboot in DRM unplug SRCU for minutes. Quiesce hardware in shutdown without unplug or scheduler flush. Bound submit locking and engine waits, cancel fences with -ESHUTDOWN, and use terminal-only non-joining work, mailbox and runtime-PM operations. Preserve the existing power requests and force-stop writes. Retain callback storage until CPUs stop; ordinary teardown still joins workers. Affects: Apple A9 and A10 firmware teardown and reboot Assisted-by: LLM --- ...apple-Stop-firmware-before-releasing.patch | 503 ++++++++++++++++++ 1 file changed, 503 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch diff --git a/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch b/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch new file mode 100644 index 00000000..ae38f211 --- /dev/null +++ b/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch @@ -0,0 +1,503 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 03:20:25 +0100 +Subject: [PATCH 40/40] drm/imagination: apple: Stop firmware before releasing + its pages + +Driver removal freed firmware storage while the DMA writer still ran. +Stop firmware and pollers before releasing pages, and defer an idle +firmware restart until a job needs it. A blocked submit ioctl also held +reboot in DRM unplug SRCU for minutes. Quiesce hardware in shutdown +without unplug or scheduler flush. Bound submit locking and engine waits, +cancel fences with -ESHUTDOWN, and use terminal-only non-joining work, +mailbox and runtime-PM operations. Preserve the existing power requests +and force-stop writes. Retain callback storage until CPUs stop; ordinary +teardown still joins workers. + +Affects: Apple A9 and A10 firmware teardown and reboot +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 127 ++++++++++++++---- + drivers/gpu/drm/imagination/pvr_apple.h | 3 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + + drivers/gpu/drm/imagination/pvr_apple_queue.c | 3 +- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 10 +- + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 9 +- + drivers/gpu/drm/imagination/pvr_drv.c | 12 ++ + drivers/soc/apple/mailbox.c | 11 ++ + drivers/soc/apple/mailbox.h | 1 + + drivers/soc/apple/rtkit.c | 38 ++++++ + include/linux/soc/apple/rtkit.h | 3 + + 11 files changed, 189 insertions(+), 29 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 990ad40..77c89d0 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -389,7 +389,6 @@ static void pvr_apple_fw_stop(struct pvr_apple *apple); + static int pvr_apple_power_on(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); +- int err; + + if (apple->chip->gate_off_ms && apple->gate_off) { + s64 remaining = (s64)apple->chip->gate_off_ms * USEC_PER_MSEC - +@@ -398,15 +397,29 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) + if (remaining > 0) + msleep(DIV_ROUND_UP(remaining, USEC_PER_MSEC)); + } +- if (!apple->fw_ready) +- return pvr_apple_boot(apple); ++ /* The driver core resumes a device before .remove(). Powering the ++ * domain must not boot an idle firmware just to tear it down again. ++ * The first job after resume starts it in pvr_apple_prepare_job(). ++ */ ++ if (READ_ONCE(apple->stopping)) ++ return -ENODEV; ++ return apple->fw_ready ? 0 : pvr_apple_boot(apple); ++} ++ ++int pvr_apple_prepare_job(struct pvr_device *pvr_dev) ++{ ++ struct pvr_apple *apple = to_apple(pvr_dev); ++ bool resumed; ++ int err; + + mutex_lock(&apple->submit_lock); +- err = apple->booted ? 0 : pvr_apple_fw_restart(apple); ++ resumed = !apple->booted; ++ err = READ_ONCE(apple->stopping) ? -ENODEV : ++ apple->booted ? 0 : pvr_apple_fw_restart(apple); + mutex_unlock(&apple->submit_lock); + if (err) + dev_err(apple_dev(apple), "GPU did not start: %d\n", err); +- else if (apple->devfreq) ++ else if (resumed && apple->devfreq) + devfreq_resume_device(apple->devfreq); + return err; + } +@@ -416,6 +429,9 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + struct pvr_apple *apple = to_apple(pvr_dev); + int err; + ++ /* Terminal shutdown already stopped firmware; never wait on submitters. */ ++ if (READ_ONCE(apple->stopping)) ++ return 0; + if (!apple->fw_ready) + return 0; + +@@ -595,7 +611,7 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine + /* For a wait: what the firmware posted meanwhile is read first */ + static u32 pvr_apple_engine_poll(struct pvr_apple *apple, enum pvr_apple_engine engine) + { +- if (apple->chip->events) ++ if (!READ_ONCE(apple->stopping) && apple->chip->events) + apple->chip->events(apple); + return pvr_apple_engine_done(apple, engine); + } +@@ -987,6 +1003,8 @@ static void pvr_apple_poll_work(struct work_struct *work) + bool pending = false; + int engine; + ++ if (READ_ONCE(apple->stopping)) ++ return; + if (apple->chip->events) + apple->chip->events(apple); + else +@@ -1027,7 +1045,7 @@ static void pvr_apple_poll_work(struct work_struct *work) + pvr_apple_jobs_retired(apple, retired); + + /* The firmware sends no completion message in this setup: poll. */ +- if (pending) ++ if (pending && !READ_ONCE(apple->stopping)) + hrtimer_start(&apple->poll_timer, us_to_ktime(max(pvr_apple_poll_us, 10U)), + HRTIMER_MODE_REL); + } +@@ -1036,7 +1054,8 @@ static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer) + { + struct pvr_apple *apple = container_of(timer, struct pvr_apple, poll_timer); + +- mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++ if (!READ_ONCE(apple->stopping)) ++ mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); + return HRTIMER_NORESTART; + } + +@@ -1091,10 +1110,16 @@ static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) + unsigned int cancelled = 0; + int engine; + +- /* each rearms the other: the timer, the work, then a timer it started */ +- hrtimer_cancel(&apple->poll_timer); +- cancel_delayed_work_sync(&apple->poll_work); +- hrtimer_cancel(&apple->poll_timer); ++ if (READ_ONCE(apple->stopping)) { ++ /* Terminal: retain storage and never wait for blocked worker activity. */ ++ hrtimer_try_to_cancel(&apple->poll_timer); ++ cancel_delayed_work(&apple->poll_work); ++ } else { ++ /* Each rearms the other. Normal teardown joins both pollers. */ ++ hrtimer_cancel(&apple->poll_timer); ++ cancel_delayed_work_sync(&apple->poll_work); ++ hrtimer_cancel(&apple->poll_timer); ++ } + spin_lock_irq(&apple->fence_lock); + for (engine = 0; engine < APPLE_ENGINES; engine++) { + list_for_each_entry_safe(fence, tmp, &apple->pending[engine], node) { +@@ -1192,13 +1217,13 @@ void pvr_apple_quiesce(struct pvr_device *pvr_dev) + int err; + + mutex_lock(&apple->submit_lock); +- err = pvr_apple_wait_idle(apple, 5000000); ++ err = apple->booted ? pvr_apple_wait_idle(apple, 5000000) : 0; + mutex_unlock(&apple->submit_lock); + + if (err) { + dev_err(apple_dev(apple), "GPU did not retire its jobs before removal\n"); + pvr_apple_cancel_fences(apple, -ETIMEDOUT); +- } else { ++ } else if (apple->booted) { + mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); + flush_delayed_work(&apple->poll_work); + } +@@ -1572,6 +1597,8 @@ static int pvr_apple_fw_restart(struct pvr_apple *apple) + int err; + + lockdep_assert_held(&apple->submit_lock); ++ if (READ_ONCE(apple->stopping)) ++ return -ENODEV; + apple->chip->restore_image(apple); + err = pvr_apple_boot(apple); + if (!err) +@@ -1641,7 +1668,8 @@ int pvr_apple_reset(struct pvr_device *pvr_dev) + } + mutex_lock(&apple->submit_lock); + } +- err = apple->booted ? 0 : pvr_apple_fw_restart(apple); ++ err = READ_ONCE(apple->stopping) ? -ENODEV : ++ apple->booted ? 0 : pvr_apple_fw_restart(apple); + mutex_unlock(&apple->submit_lock); + if (err) + dev_err(dev, "GPU reset failed: %d\n", err); +@@ -1777,14 +1805,72 @@ struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t siz + return obj; + } + ++/* ++ * Shutdown is terminal: wait at most one second for a submitter and at most ++ * one second total for all engines. Chip stop has bounded hardware/RTKit ++ * polls (A9: 200 ms; A10: 100 ms plus two 1 s ACKs). Never flush the job ++ * scheduler here: it may be waiting on a job that the GPU cannot finish. ++ */ ++void pvr_apple_shutdown(struct pvr_device *pvr_dev) ++{ ++ struct pvr_apple *apple = to_apple(pvr_dev); ++ struct device *dev = apple_dev(apple); ++ unsigned long deadline = jiffies + msecs_to_jiffies(1000); ++ int engine, err = 0; ++ ++ WRITE_ONCE(apple->stopping, true); ++ while (!mutex_trylock(&apple->submit_lock)) { ++ if (time_after_eq(jiffies, deadline)) { ++ dev_err(dev, "shutdown: submitter did not stop within 1 s; continuing\n"); ++ pvr_apple_cancel_fences(apple, -ESHUTDOWN); ++ return; ++ } ++ usleep_range(1000, 2000); ++ } ++ deadline = jiffies + msecs_to_jiffies(1000); ++ if (apple->booted) { ++ for (engine = 0; engine < APPLE_ENGINES && !err; engine++) { ++ unsigned int remaining; ++ ++ if (time_after_eq(jiffies, deadline)) { ++ err = -ETIMEDOUT; ++ break; ++ } ++ remaining = jiffies_to_usecs(deadline - jiffies); ++ err = pvr_apple_wait_engine(apple, engine, ++ pvr_apple_engine_last(apple, engine), remaining); ++ } ++ apple->chip->stop(apple, false); ++ apple->booted = false; ++ apple->chip->idle(apple); ++ } ++ mutex_unlock(&apple->submit_lock); ++ pvr_apple_cancel_fences(apple, -ESHUTDOWN); ++ if (err) ++ dev_warn(dev, "shutdown: GPU jobs timed out; firmware forced off\n"); ++ /* No resume, including for a firmware that has never run. */ ++ /* device_shutdown() pins runtime PM before invoking this callback. */ ++ pm_runtime_put_noidle(dev); ++ err = __pm_runtime_suspend(dev, RPM_ASYNC | RPM_NOWAIT); ++ pm_runtime_get_noresume(dev); ++ if (err < 0) ++ dev_warn(dev, "shutdown: runtime suspend returned %d; continuing\n", err); ++ else ++ dev_info(dev, "shutdown: Apple GPU firmware stopped, runtime suspend requested\n"); ++} ++ + void pvr_apple_fw_fini(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); + ++ /* Stop the DMA writer while every firmware page is still owned. */ ++ mutex_lock(&apple->submit_lock); ++ pvr_apple_fw_stop(apple); + apple->fw_ready = false; ++ mutex_unlock(&apple->submit_lock); ++ hrtimer_cancel(&apple->poll_timer); ++ cancel_delayed_work_sync(&apple->poll_work); + pvr_apple_forget_scenes(apple); +- if (apple->booted) +- pvr_apple_switch_to_kernel_vm(apple, NULL); + pvr_apple_cancel_fences(apple, -ENODEV); + pvr_apple_gpu_pages_fini(apple); + if (apple->pm_pages) { +@@ -1792,13 +1878,6 @@ void pvr_apple_fw_fini(struct pvr_device *pvr_dev) + pvr_gem_object_put(apple->pm_pages); + apple->pm_pages = NULL; + } +- if (apple->booted) { +- apple->chip->stop(apple, false); +- /* the firmware's messages until its stop queued completion checks */ +- cancel_delayed_work_sync(&apple->poll_work); +- hrtimer_cancel(&apple->poll_timer); +- apple->booted = false; +- } + if (apple->chip->fini) + apple->chip->fini(apple); + } +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index d1de0ad..211e61d 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -182,7 +182,9 @@ void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); + int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); + void pvr_apple_mmu_flush(struct pvr_device *pvr_dev); + void pvr_apple_vm_fini(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); ++int pvr_apple_prepare_job(struct pvr_device *pvr_dev); + void pvr_apple_quiesce(struct pvr_device *pvr_dev); ++void pvr_apple_shutdown(struct pvr_device *pvr_dev); + const struct pvr_stream_cmd_defs *pvr_apple_cmd_stream(struct pvr_device *pvr_dev, + enum drm_pvr_job_type type); + +@@ -228,6 +230,7 @@ static inline int pvr_apple_job_add_deps(struct pvr_job *job) { return 0; } + static inline void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished) {} + static inline void pvr_apple_mmu_flush(struct pvr_device *pvr_dev) {} + static inline void pvr_apple_quiesce(struct pvr_device *pvr_dev) {} ++static inline void pvr_apple_shutdown(struct pvr_device *pvr_dev) {} + static inline void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev) {} + static inline void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) {} + static inline void pvr_apple_vm_fini(struct pvr_device *pvr_dev, +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 09ecce9..fe66ea5 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -87,6 +87,7 @@ struct pvr_apple { + size_t fw_size; + void *heap; /* memory shared with the firmware: rings, stamps, commands */ + struct pvr_apple_layout layout; ++ bool stopping; /* terminal shutdown forbids firmware restart */ + bool booted; + bool fault_reported; /* once per firmware boot */ + bool fw_ready; /* init data handed over once: runtime PM restarts it */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_queue.c b/drivers/gpu/drm/imagination/pvr_apple_queue.c +index 065dad1..8e511cc 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_queue.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_queue.c +@@ -54,7 +54,8 @@ static struct dma_fence *pvr_apple_run_job(struct drm_sched_job *sched_job) + err = pvr_power_get(job->pvr_dev); + if (err) + return ERR_PTR(err); +- fence = pvr_apple_submit(job); ++ err = pvr_apple_prepare_job(job->pvr_dev); ++ fence = err ? ERR_PTR(err) : pvr_apple_submit(job); + if (IS_ERR(fence)) + pvr_power_put(job->pvr_dev); + else +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 2be9500..1152a22 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -1001,12 +1001,15 @@ static void s8003_drain_work(struct work_struct *work) + bool alive = false; + int err; + ++ if (READ_ONCE(s->apple->stopping)) ++ return; + err = s8003_drain(s->apple, &alive); + if (err) { + dev_err(apple_dev(s->apple), "GFX firmware's rings are no longer read: %d\n", err); + return; + } +- schedule_delayed_work(&s->drain_work, msecs_to_jiffies(S8003_DRAIN_MS)); ++ if (!READ_ONCE(s->apple->stopping)) ++ schedule_delayed_work(&s->drain_work, msecs_to_jiffies(S8003_DRAIN_MS)); + } + + static int s8003_wait_alive(struct pvr_apple *apple) +@@ -2574,7 +2577,10 @@ static void pvr_apple_s8003_stop(struct pvr_apple *apple, bool recover) + { + struct pvr_apple_s8003 *s = apple->chip_data; + +- cancel_delayed_work_sync(&s->drain_work); ++ if (READ_ONCE(apple->stopping)) ++ cancel_delayed_work(&s->drain_work); ++ else ++ cancel_delayed_work_sync(&s->drain_work); + if (pm_runtime_status_suspended(apple_dev(apple))) + return; + s8003_core_off(apple); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +index 5c62c13..19be745 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_t8010.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +@@ -292,7 +292,9 @@ static void pvr_apple_t8010_stop(struct pvr_apple *apple, bool recover) + struct pvr_apple_t8010 *t = apple->chip_data; + + pvr_apple_request_power_state(apple, 0); +- if (apple_rtkit_shutdown(t->rtk) && recover) ++ if (READ_ONCE(apple->stopping)) ++ apple_rtkit_shutdown_terminal(t->rtk); ++ else if (apple_rtkit_shutdown(t->rtk) && recover) + apple_rtkit_reinit(t->rtk); + writel(readl(apple->fender + FENDER_RUN) & ~FENDER_RUN_ENABLE, + apple->fender + FENDER_RUN); +@@ -303,7 +305,10 @@ static void pvr_apple_t8010_idle(struct pvr_apple *apple) + struct pvr_apple_t8010 *t = apple->chip_data; + + /* its mailbox would keep the GPU's power domain on */ +- t->rtk_idle = !apple_rtkit_mbox_stop(t->rtk); ++ if (READ_ONCE(apple->stopping)) ++ t->rtk_idle = true; ++ else ++ t->rtk_idle = !apple_rtkit_mbox_stop(t->rtk); + } + + static void pvr_apple_t8010_restore_image(struct pvr_apple *apple) +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index 206ee1f..7ad0b71 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -1500,6 +1500,17 @@ static void pvr_remove(struct platform_device *plat_dev) + pvr_power_domains_fini(pvr_dev); + } + ++static void pvr_shutdown(struct platform_device *plat_dev) ++{ ++ struct drm_device *drm_dev = platform_get_drvdata(plat_dev); ++ struct pvr_device *pvr_dev = to_pvr_device(drm_dev); ++ ++ if (!pvr_device_is_apple(pvr_dev)) ++ return; ++ /* Unplug waits for ioctl SRCU readers, including wedged submitters. */ ++ pvr_apple_shutdown(pvr_dev); ++} ++ + static const struct pvr_device_data pvr_device_data_manual = { + .pwr_ops = &pvr_power_sequence_ops_manual, + }; +@@ -1588,6 +1599,7 @@ static const struct dev_pm_ops pvr_pm_ops = { + static struct platform_driver pvr_driver = { + .probe = pvr_probe, + .remove = pvr_remove, ++ .shutdown = pvr_shutdown, + .driver = { + .name = PVR_DRIVER_NAME, + .pm = &pvr_pm_ops, +diff --git a/drivers/soc/apple/mailbox.c b/drivers/soc/apple/mailbox.c +index 1417dba..cd27fb6 100644 +--- a/drivers/soc/apple/mailbox.c ++++ b/drivers/soc/apple/mailbox.c +@@ -358,6 +358,17 @@ void apple_mbox_stop(struct apple_mbox *mbox) + } + EXPORT_SYMBOL(apple_mbox_stop); + ++/* Terminal shutdown retains all callback storage until the CPUs stop. */ ++void apple_mbox_stop_nowait(struct apple_mbox *mbox) ++{ ++ if (!mbox->active) ++ return; ++ mbox->active = false; ++ disable_irq_nosync(mbox->irq_recv_not_empty); ++ pm_runtime_put_noidle(mbox->dev); ++} ++EXPORT_SYMBOL_GPL(apple_mbox_stop_nowait); ++ + struct apple_mbox *apple_mbox_get(struct device *dev, int index) + { + struct of_phandle_args args; +diff --git a/drivers/soc/apple/mailbox.h b/drivers/soc/apple/mailbox.h +index f73a891..a65a45f 100644 +--- a/drivers/soc/apple/mailbox.h ++++ b/drivers/soc/apple/mailbox.h +@@ -41,6 +41,7 @@ struct apple_mbox *apple_mbox_get_byname(struct device *dev, const char *name); + + int apple_mbox_start(struct apple_mbox *mbox); + void apple_mbox_stop(struct apple_mbox *mbox); ++void apple_mbox_stop_nowait(struct apple_mbox *mbox); + int apple_mbox_poll(struct apple_mbox *mbox); + int apple_mbox_send(struct apple_mbox *mbox, struct apple_mbox_msg msg, + bool atomic); +diff --git a/drivers/soc/apple/rtkit.c b/drivers/soc/apple/rtkit.c +index 725ab3f..387e693 100644 +--- a/drivers/soc/apple/rtkit.c ++++ b/drivers/soc/apple/rtkit.c +@@ -885,6 +885,44 @@ int apple_rtkit_shutdown(struct apple_rtkit *rtk) + } + EXPORT_SYMBOL_GPL(apple_rtkit_shutdown); + ++/** ++ * apple_rtkit_shutdown_terminal() - Stop firmware without joining callbacks ++ * @rtk: RTKit reference, retained until the CPUs stop ++ * ++ * Each send polls for at most 500 ms, each ACK waits at most one second. ++ * The caller must halt the co-processor even if a request fails. Do not ++ * reinitialize or free any storage: a callback may still be running. ++ * ++ * Return: zero on acknowledgement, or the first request error. ++ */ ++int apple_rtkit_shutdown_terminal(struct apple_rtkit *rtk) ++{ ++ u64 msg; ++ int ret; ++ ++ reinit_completion(&rtk->ap_pwr_ack_completion); ++ msg = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_AP_PWR_STATE) | ++ FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_QUIESCED); ++ ret = apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_MGMT, msg, NULL, true); ++ if (!ret) ++ ret = apple_rtkit_wait_for_completion(&rtk->ap_pwr_ack_completion); ++ if (!ret && rtk->ap_power_state != APPLE_RTKIT_PWR_STATE_QUIESCED) ++ ret = -EINVAL; ++ if (!ret) { ++ reinit_completion(&rtk->iop_pwr_ack_completion); ++ msg = FIELD_PREP(APPLE_RTKIT_MGMT_TYPE, APPLE_RTKIT_MGMT_SET_IOP_PWR_STATE) | ++ FIELD_PREP(APPLE_RTKIT_MGMT_PWR_STATE, APPLE_RTKIT_PWR_STATE_SLEEP); ++ ret = apple_rtkit_send_message(rtk, APPLE_RTKIT_EP_MGMT, msg, NULL, true); ++ if (!ret) ++ ret = apple_rtkit_wait_for_completion(&rtk->iop_pwr_ack_completion); ++ if (!ret && rtk->iop_power_state != APPLE_RTKIT_PWR_STATE_SLEEP) ++ ret = -EINVAL; ++ } ++ apple_mbox_stop_nowait(rtk->mbox); ++ return ret; ++} ++EXPORT_SYMBOL_GPL(apple_rtkit_shutdown_terminal); ++ + int apple_rtkit_poweroff(struct apple_rtkit *rtk) + { + int ret; +diff --git a/include/linux/soc/apple/rtkit.h b/include/linux/soc/apple/rtkit.h +index 1f9ebb6..743e89c 100644 +--- a/include/linux/soc/apple/rtkit.h ++++ b/include/linux/soc/apple/rtkit.h +@@ -195,4 +195,7 @@ int apple_rtkit_poll(struct apple_rtkit *rtk); + */ + u8 apple_rtkit_app_ep_to_ep(struct apple_rtkit *rtk, u8 app_ep); + ++/* Terminal only: bounded requests, no callback join or buffer release. */ ++int apple_rtkit_shutdown_terminal(struct apple_rtkit *rtk); ++ + #endif /* _LINUX_APPLE_RTKIT_H_ */ From de66cfafb704ee6f60d70276a6d1820c8adfa76a Mon Sep 17 00:00:00 2001 From: brkosebay Date: Mon, 16 Nov 2026 05:15:20 +0100 Subject: [PATCH 17/25] kernel: Preserve A10 cap defaults and balance Apple GPU devfreq Keep the native BO cap at S8003 50/75 defaults and off on other chips unless explicitly configured. Track one devfreq suspend reference across lazy runtime resumes, failed starts and system sleep on both Apple chips. Assisted-by: LLM --- ...Cap-native-BO-memory-by-device-and-c.patch | 70 +++++--- ...apple-Stop-firmware-before-releasing.patch | 163 ++++++++++++++---- 2 files changed, 173 insertions(+), 60 deletions(-) diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch b/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch index 6ec86676..55ad4a6a 100644 --- a/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch +++ b/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch @@ -8,21 +8,26 @@ Native creation pins all backing pages, so a client can exhaust memory needed to recover. Charge before taking pages and refuse above device and per-TGID budgets with -ENOMEM and a ratelimited diagnostic. Share accounting across the creator's DRM files, uncharge at free and exclude -imports. Defaults are 50 percent of RAM and 75 percent of that device -budget per client. Create-time charging counts real backing here; it +imports. S8003 defaults are 50 percent of RAM and 75 percent of that device +budget per client. Other devices default to no cap. Create-time charging counts real backing here; it can move to residency when eviction becomes available. +Select automatic defaults per device: S8003 retains 50/75; all other +chips have no cap. Explicit percentages override the defaults and zero +disables a limit. -1 selects automatic defaults. With the device cap off, +skip accounting allocation as well as refusals. + Affects: every PowerVR device Assisted-by: LLM --- - drivers/gpu/drm/imagination/pvr_device.c | 3 + - drivers/gpu/drm/imagination/pvr_device.h | 7 ++ - drivers/gpu/drm/imagination/pvr_gem.c | 120 ++++++++++++++++++++++- - drivers/gpu/drm/imagination/pvr_gem.h | 4 + - 4 files changed, 133 insertions(+), 1 deletion(-) + drivers/gpu/drm/imagination/pvr_device.c | 3 + + drivers/gpu/drm/imagination/pvr_device.h | 7 ++ + drivers/gpu/drm/imagination/pvr_gem.c | 137 ++++++++++++++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_gem.h | 4 + + 4 files changed, 150 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c -index 2c06a4a..c13a1b6 100644 +index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..c13a1b648929ee7d2030f17ef2093a81725984e4 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -804,6 +804,9 @@ pvr_device_init(struct pvr_device *pvr_dev) @@ -36,7 +41,7 @@ index 2c06a4a..c13a1b6 100644 pvr_dev->device_data = of_device_get_match_data(dev); diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h -index 26b589e..da999d4 100644 +index 26b589ee72b9c261fbb10803fd3ddd9999e95f8c..da999d480bce7c34aef8d34ddedf3517d4c4bb09 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -92,6 +92,13 @@ struct pvr_device { @@ -54,14 +59,15 @@ index 26b589e..da999d4 100644 struct pvr_gpu_id gpu_id; diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c -index 817b47d..7604c59 100644 +index 817b47df763fd8dc0e3603360f8fabbe1b672bb5..44626d14f92e85b145f9447b952202eb068fd402 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -17,17 +17,130 @@ +@@ -17,17 +17,147 @@ #include #include #include +#include ++#include #include +#include +#include @@ -73,13 +79,13 @@ index 817b47d..7604c59 100644 #include +/* Creation-time reservation; resident-page accounting can replace this later. */ -+static unsigned int pinned_device_pct = 50; -+module_param(pinned_device_pct, uint, 0644); -+MODULE_PARM_DESC(pinned_device_pct, "Maximum native BO memory as a percentage of RAM (default 50)"); ++static int pinned_device_pct = -1; ++module_param(pinned_device_pct, int, 0644); ++MODULE_PARM_DESC(pinned_device_pct, "Native BO RAM percentage (0 off, -1 auto: S8003 50, other chips 0)"); + -+static unsigned int pinned_client_pct = 75; -+module_param(pinned_client_pct, uint, 0644); -+MODULE_PARM_DESC(pinned_client_pct, "Maximum per-process share of the device BO cap (default 75)"); ++static int pinned_client_pct = -1; ++module_param(pinned_client_pct, int, 0644); ++MODULE_PARM_DESC(pinned_client_pct, "Per-process share of the BO cap (0 off, -1 auto: S8003 75, other chips 0)"); + +/* A held struct pid prevents PID reuse from merging unrelated clients' charges. */ +struct pvr_gem_account { @@ -89,19 +95,34 @@ index 817b47d..7604c59 100644 + unsigned int objects; +}; + ++/* Defaults are per device; explicit module parameters override either limit. */ ++static unsigned int pvr_gem_pin_limit(struct pvr_device *pvr_dev, int pct, ++ unsigned int s8003_default) ++{ ++ if (pct < 0) ++ return of_device_is_compatible(from_pvr_device(pvr_dev)->dev->of_node, ++ "apple,s8003-gpu") ? s8003_default : 0; ++ return min((unsigned int)pct, 100U); ++} ++ +static int pvr_gem_pin_charge(struct pvr_gem_object *pvr_obj) +{ + struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); + struct pvr_device *pvr_dev = to_pvr_device(obj->dev); + struct pvr_gem_account *account, *found, *fresh = NULL; + struct pid *tgid = task_tgid(current); -+ u64 cap = div_u64(((u64)totalram_pages() << PAGE_SHIFT) * -+ min(READ_ONCE(pinned_device_pct), 100U), 100); -+ u64 client_cap = div_u64(cap * min(READ_ONCE(pinned_client_pct), 100U), 100); ++ unsigned int device_pct = pvr_gem_pin_limit(pvr_dev, READ_ONCE(pinned_device_pct), 50); ++ unsigned int client_pct = pvr_gem_pin_limit(pvr_dev, READ_ONCE(pinned_client_pct), 75); ++ u64 cap = div_u64(((u64)totalram_pages() << PAGE_SHIFT) * device_pct, 100); ++ u64 client_cap = div_u64(cap * client_pct, 100); + u64 device_bytes, client_bytes; + unsigned long flags; + int err = 0; + ++ /* No accounting allocation or refusal on chips whose cap is off. */ ++ if (!device_pct) ++ return 0; ++ + for (;;) { + spin_lock_irqsave(&pvr_dev->gem_pin_lock, flags); + found = NULL; @@ -114,7 +135,8 @@ index 817b47d..7604c59 100644 + device_bytes = pvr_dev->gem_pin_bytes; + client_bytes = found ? found->bytes : 0; + if (device_bytes > cap || obj->size > cap - device_bytes || -+ client_bytes > client_cap || obj->size > client_cap - client_bytes) { ++ (client_pct && (client_bytes > client_cap || ++ obj->size > client_cap - client_bytes))) { + spin_unlock_irqrestore(&pvr_dev->gem_pin_lock, flags); + err = -ENOMEM; + break; @@ -188,7 +210,7 @@ index 817b47d..7604c59 100644 drm_gem_shmem_free(shmem_obj); } -@@ -381,6 +494,11 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) +@@ -381,6 +511,11 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) pvr_obj = shmem_gem_to_pvr_gem(shmem_obj); pvr_obj->flags = flags; @@ -200,7 +222,7 @@ index 817b47d..7604c59 100644 sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); if (IS_ERR(sgt)) { err = PTR_ERR(sgt); -@@ -398,7 +516,7 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) +@@ -398,7 +533,7 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) return pvr_obj; err_shmem_object_free: @@ -210,7 +232,7 @@ index 817b47d..7604c59 100644 return ERR_PTR(err); } diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index efcd270..2e7cf2c 100644 +index efcd270ffa5a75e035d81a9dd87325d9bc7d3eb7..2e7cf2c9a6a435f3a1a0b06d6c3927b6296c8782 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h @@ -27,6 +27,7 @@ diff --git a/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch b/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch index ae38f211..8e4d511d 100644 --- a/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch +++ b/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch @@ -14,27 +14,72 @@ mailbox and runtime-PM operations. Preserve the existing power requests and force-stop writes. Retain callback storage until CPUs stop; ordinary teardown still joins workers. +Track the driver's devfreq suspend reference and change it only on +suspend/resume edges, serialized outside submit_lock. A jobless runtime +resume or failed firmware start cannot accumulate extra suspend counts. +Apply the same guard to system sleep and error unwinds on both chips. + Affects: Apple A9 and A10 firmware teardown and reboot Assisted-by: LLM --- - drivers/gpu/drm/imagination/pvr_apple.c | 127 ++++++++++++++---- - drivers/gpu/drm/imagination/pvr_apple.h | 3 + - drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + - drivers/gpu/drm/imagination/pvr_apple_queue.c | 3 +- - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 10 +- - drivers/gpu/drm/imagination/pvr_apple_t8010.c | 9 +- - drivers/gpu/drm/imagination/pvr_drv.c | 12 ++ - drivers/soc/apple/mailbox.c | 11 ++ - drivers/soc/apple/mailbox.h | 1 + - drivers/soc/apple/rtkit.c | 38 ++++++ - include/linux/soc/apple/rtkit.h | 3 + - 11 files changed, 189 insertions(+), 29 deletions(-) + drivers/gpu/drm/imagination/pvr_apple.c | 166 ++++++++++++++++++++----- + drivers/gpu/drm/imagination/pvr_apple.h | 3 + drivers/gpu/drm/imagination/pvr_apple_chip.h | 3 + drivers/gpu/drm/imagination/pvr_apple_queue.c | 3 + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 10 +- + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 9 + + drivers/gpu/drm/imagination/pvr_drv.c | 12 ++ + drivers/soc/apple/mailbox.c | 11 ++ + drivers/soc/apple/mailbox.h | 1 + drivers/soc/apple/rtkit.c | 38 ++++++ + include/linux/soc/apple/rtkit.h | 3 + 11 files changed, 221 insertions(+), 38 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 990ad40..77c89d0 100644 +index 990ad4097890d70ba76579cc7b157a09f79ddfe4..b7790604b9117a3767c1366c098ac1e4d5ba6ba0 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.c +++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -389,7 +389,6 @@ static void pvr_apple_fw_stop(struct pvr_apple *apple); +@@ -303,6 +303,30 @@ int pvr_apple_fw_load(struct pvr_apple *apple, size_t max_size) + return 0; + } + ++/* Track our one suspend reference, including jobless resumes and failed starts. ++ * devfreq changes its suspend count even when its governor callback fails. ++ * Do not take submit_lock here: devfreq callbacks have their own lock order. ++ */ ++static void pvr_apple_devfreq_suspend(struct pvr_apple *apple) ++{ ++ mutex_lock(&apple->devfreq_lock); ++ if (apple->devfreq && !apple->devfreq_suspended) { ++ devfreq_suspend_device(apple->devfreq); ++ WRITE_ONCE(apple->devfreq_suspended, true); ++ } ++ mutex_unlock(&apple->devfreq_lock); ++} ++ ++static void pvr_apple_devfreq_resume(struct pvr_apple *apple) ++{ ++ mutex_lock(&apple->devfreq_lock); ++ if (apple->devfreq && apple->devfreq_suspended) { ++ devfreq_resume_device(apple->devfreq); ++ WRITE_ONCE(apple->devfreq_suspended, false); ++ } ++ mutex_unlock(&apple->devfreq_lock); ++} ++ + static int pvr_apple_power_init(struct pvr_device *pvr_dev) + { + struct device *dev = from_pvr_device(pvr_dev)->dev; +@@ -320,6 +344,8 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + err = devm_mutex_init(dev, &apple->submit_lock); + if (!err) + err = devm_mutex_init(dev, &apple->perf_lock); ++ if (!err) ++ err = devm_mutex_init(dev, &apple->devfreq_lock); + if (!err) + err = devm_mutex_init(dev, &apple->pm_lock); + if (err) +@@ -389,7 +415,6 @@ static void pvr_apple_fw_stop(struct pvr_apple *apple); static int pvr_apple_power_on(struct pvr_device *pvr_dev) { struct pvr_apple *apple = to_apple(pvr_dev); @@ -42,7 +87,7 @@ index 990ad40..77c89d0 100644 if (apple->chip->gate_off_ms && apple->gate_off) { s64 remaining = (s64)apple->chip->gate_off_ms * USEC_PER_MSEC - -@@ -398,15 +397,29 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) +@@ -398,16 +423,28 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) if (remaining > 0) msleep(DIV_ROUND_UP(remaining, USEC_PER_MSEC)); } @@ -60,23 +105,23 @@ index 990ad40..77c89d0 100644 +int pvr_apple_prepare_job(struct pvr_device *pvr_dev) +{ + struct pvr_apple *apple = to_apple(pvr_dev); -+ bool resumed; + int err; mutex_lock(&apple->submit_lock); - err = apple->booted ? 0 : pvr_apple_fw_restart(apple); -+ resumed = !apple->booted; + err = READ_ONCE(apple->stopping) ? -ENODEV : + apple->booted ? 0 : pvr_apple_fw_restart(apple); mutex_unlock(&apple->submit_lock); if (err) dev_err(apple_dev(apple), "GPU did not start: %d\n", err); - else if (apple->devfreq) -+ else if (resumed && apple->devfreq) - devfreq_resume_device(apple->devfreq); +- devfreq_resume_device(apple->devfreq); ++ else if (READ_ONCE(apple->devfreq_suspended)) ++ pvr_apple_devfreq_resume(apple); return err; } -@@ -416,6 +429,9 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + +@@ -416,11 +453,13 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) struct pvr_apple *apple = to_apple(pvr_dev); int err; @@ -86,7 +131,23 @@ index 990ad40..77c89d0 100644 if (!apple->fw_ready) return 0; -@@ -595,7 +611,7 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine +- if (apple->devfreq) +- devfreq_suspend_device(apple->devfreq); ++ pvr_apple_devfreq_suspend(apple); + mutex_lock(&apple->submit_lock); + /* + * Nothing holds a job reference, so this does not wait in practice. +@@ -433,8 +472,7 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + } + mutex_unlock(&apple->submit_lock); + if (err) { +- if (apple->devfreq) +- devfreq_resume_device(apple->devfreq); ++ pvr_apple_devfreq_resume(apple); + return -EBUSY; + } + /* the pollers run only while fences are pending; none are */ +@@ -595,7 +633,7 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine /* For a wait: what the firmware posted meanwhile is read first */ static u32 pvr_apple_engine_poll(struct pvr_apple *apple, enum pvr_apple_engine engine) { @@ -95,7 +156,7 @@ index 990ad40..77c89d0 100644 apple->chip->events(apple); return pvr_apple_engine_done(apple, engine); } -@@ -987,6 +1003,8 @@ static void pvr_apple_poll_work(struct work_struct *work) +@@ -987,6 +1025,8 @@ static void pvr_apple_poll_work(struct work_struct *work) bool pending = false; int engine; @@ -104,7 +165,7 @@ index 990ad40..77c89d0 100644 if (apple->chip->events) apple->chip->events(apple); else -@@ -1027,7 +1045,7 @@ static void pvr_apple_poll_work(struct work_struct *work) +@@ -1027,7 +1067,7 @@ static void pvr_apple_poll_work(struct work_struct *work) pvr_apple_jobs_retired(apple, retired); /* The firmware sends no completion message in this setup: poll. */ @@ -113,7 +174,7 @@ index 990ad40..77c89d0 100644 hrtimer_start(&apple->poll_timer, us_to_ktime(max(pvr_apple_poll_us, 10U)), HRTIMER_MODE_REL); } -@@ -1036,7 +1054,8 @@ static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer) +@@ -1036,7 +1076,8 @@ static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer) { struct pvr_apple *apple = container_of(timer, struct pvr_apple, poll_timer); @@ -123,7 +184,7 @@ index 990ad40..77c89d0 100644 return HRTIMER_NORESTART; } -@@ -1091,10 +1110,16 @@ static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) +@@ -1091,10 +1132,16 @@ static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) unsigned int cancelled = 0; int engine; @@ -144,7 +205,7 @@ index 990ad40..77c89d0 100644 spin_lock_irq(&apple->fence_lock); for (engine = 0; engine < APPLE_ENGINES; engine++) { list_for_each_entry_safe(fence, tmp, &apple->pending[engine], node) { -@@ -1192,13 +1217,13 @@ void pvr_apple_quiesce(struct pvr_device *pvr_dev) +@@ -1192,13 +1239,13 @@ void pvr_apple_quiesce(struct pvr_device *pvr_dev) int err; mutex_lock(&apple->submit_lock); @@ -160,7 +221,7 @@ index 990ad40..77c89d0 100644 mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); flush_delayed_work(&apple->poll_work); } -@@ -1572,6 +1597,8 @@ static int pvr_apple_fw_restart(struct pvr_apple *apple) +@@ -1572,6 +1619,8 @@ static int pvr_apple_fw_restart(struct pvr_apple *apple) int err; lockdep_assert_held(&apple->submit_lock); @@ -169,7 +230,7 @@ index 990ad40..77c89d0 100644 apple->chip->restore_image(apple); err = pvr_apple_boot(apple); if (!err) -@@ -1641,7 +1668,8 @@ int pvr_apple_reset(struct pvr_device *pvr_dev) +@@ -1641,7 +1690,8 @@ int pvr_apple_reset(struct pvr_device *pvr_dev) } mutex_lock(&apple->submit_lock); } @@ -179,7 +240,28 @@ index 990ad40..77c89d0 100644 mutex_unlock(&apple->submit_lock); if (err) dev_err(dev, "GPU reset failed: %d\n", err); -@@ -1777,14 +1805,72 @@ struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t siz +@@ -1677,8 +1727,7 @@ int pvr_apple_suspend(struct pvr_device *pvr_dev) + if (pm_runtime_status_suspended(dev)) + return 0; + +- if (apple->devfreq) +- devfreq_suspend_device(apple->devfreq); ++ pvr_apple_devfreq_suspend(apple); + + mutex_lock(&apple->submit_lock); + err = pvr_apple_wait_idle(apple, 5000000); +@@ -1730,8 +1779,8 @@ int pvr_apple_resume(struct pvr_device *pvr_dev) + mutex_unlock(&apple->submit_lock); + if (err) + dev_err(apple_dev(apple), "GPU did not start after sleep: %d\n", err); +- else if (apple->devfreq) +- devfreq_resume_device(apple->devfreq); ++ else ++ pvr_apple_devfreq_resume(apple); + return err; + } + +@@ -1777,14 +1826,72 @@ struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t siz return obj; } @@ -254,7 +336,7 @@ index 990ad40..77c89d0 100644 pvr_apple_cancel_fences(apple, -ENODEV); pvr_apple_gpu_pages_fini(apple); if (apple->pm_pages) { -@@ -1792,13 +1878,6 @@ void pvr_apple_fw_fini(struct pvr_device *pvr_dev) +@@ -1792,13 +1899,6 @@ void pvr_apple_fw_fini(struct pvr_device *pvr_dev) pvr_gem_object_put(apple->pm_pages); apple->pm_pages = NULL; } @@ -269,7 +351,7 @@ index 990ad40..77c89d0 100644 apple->chip->fini(apple); } diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h -index d1de0ad..211e61d 100644 +index d1de0ad773238914ab4e50e6881ec5141e81389c..211e61df8cca8e530774f2052ab4626f298ad436 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.h +++ b/drivers/gpu/drm/imagination/pvr_apple.h @@ -182,7 +182,9 @@ void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); @@ -291,7 +373,7 @@ index d1de0ad..211e61d 100644 static inline void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) {} static inline void pvr_apple_vm_fini(struct pvr_device *pvr_dev, diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index 09ecce9..fe66ea5 100644 +index 09ecce9d3cb032d9e98ec424a7d4404033d5ad9e..09571e68bd2b7ee62af15aeed4e68b603469f8bc 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_chip.h +++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h @@ -87,6 +87,7 @@ struct pvr_apple { @@ -302,8 +384,17 @@ index 09ecce9..fe66ea5 100644 bool booted; bool fault_reported; /* once per firmware boot */ bool fw_ready; /* init data handed over once: runtime PM restarts it */ +@@ -138,6 +139,8 @@ struct pvr_apple { + u32 perf_state; + u32 perf_state_max; + struct devfreq *devfreq; ++ struct mutex devfreq_lock; /* serializes our devfreq suspend reference */ ++ bool devfreq_suspended; + ktime_t busy_since; + u64 busy_ns; + ktime_t stat_since; diff --git a/drivers/gpu/drm/imagination/pvr_apple_queue.c b/drivers/gpu/drm/imagination/pvr_apple_queue.c -index 065dad1..8e511cc 100644 +index 065dad18dcb131b434e9f371f1e5e0a7c1844b8d..8e511cc270ae2eaddb4bb3a661c5946e4c66514e 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_queue.c +++ b/drivers/gpu/drm/imagination/pvr_apple_queue.c @@ -54,7 +54,8 @@ static struct dma_fence *pvr_apple_run_job(struct drm_sched_job *sched_job) @@ -317,7 +408,7 @@ index 065dad1..8e511cc 100644 pvr_power_put(job->pvr_dev); else diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index 2be9500..1152a22 100644 +index 2be9500a6c54abc2abfb551c2bedcce9a285b9b5..1152a221c6ebc25ce01dda0335aa00c7d1f84087 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -1001,12 +1001,15 @@ static void s8003_drain_work(struct work_struct *work) @@ -350,7 +441,7 @@ index 2be9500..1152a22 100644 return; s8003_core_off(apple); diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c -index 5c62c13..19be745 100644 +index 5c62c13c85d7b67b50b273bc3d822acc62e76d2f..19be745c5e809fc5b57e0740e925ef256d5c3cde 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_t8010.c +++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c @@ -292,7 +292,9 @@ static void pvr_apple_t8010_stop(struct pvr_apple *apple, bool recover) @@ -377,7 +468,7 @@ index 5c62c13..19be745 100644 static void pvr_apple_t8010_restore_image(struct pvr_apple *apple) diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c -index 206ee1f..7ad0b71 100644 +index 206ee1ff656c9e9821e8f28d1ff633cf2d90a601..7ad0b71f81a3ef8be7dbbeb18a98e31ddcbdfa8d 100644 --- a/drivers/gpu/drm/imagination/pvr_drv.c +++ b/drivers/gpu/drm/imagination/pvr_drv.c @@ -1500,6 +1500,17 @@ static void pvr_remove(struct platform_device *plat_dev) From da32d2d334f3ce93c5be18371a3d8f46bf7673e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 11:05:29 +0100 Subject: [PATCH 18/25] kernel: Merge S8003 deferred flushes with cached VM tags Keep the branch based on the updated A9 render series. Assisted-by: LLM --- ...pple-End-S8003-waits-when-firmware-.patch} | 0 ...pple-Capture-licensed-MMU-faults-be.patch} | 0 ...pple-Honour-the-minimum-runtime-gat.patch} | 0 ...apple-Keep-VM-tags-distinct-from-fir.patch | 86 ----------- ...apple-Keep-VM-tags-distinct-from-fir.patch | 133 ++++++++++++++++++ ...ap-native-BO-memory-by-device-and-c.patch} | 0 ...pple-Report-each-VM-s-initial-PM-po.patch} | 0 ...pple-Reset-promptly-after-a-firmwar.patch} | 0 ...pple-Stop-firmware-before-releasing.patch} | 0 9 files changed, 133 insertions(+), 86 deletions(-) rename kernel/patches/drivers/gpu/{0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch => 0034-drm-imagination-apple-End-S8003-waits-when-firmware-.patch} (100%) rename kernel/patches/drivers/gpu/{0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch => 0035-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch} (100%) rename kernel/patches/drivers/gpu/{0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch => 0036-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch} (100%) delete mode 100644 kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch create mode 100644 kernel/patches/drivers/gpu/0037-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch rename kernel/patches/drivers/gpu/{0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch => 0038-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch} (100%) rename kernel/patches/drivers/gpu/{0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch => 0039-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch} (100%) rename kernel/patches/drivers/gpu/{0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch => 0040-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch} (100%) rename kernel/patches/drivers/gpu/{0040-drm-imagination-apple-Stop-firmware-before-releasing.patch => 0041-drm-imagination-apple-Stop-firmware-before-releasing.patch} (100%) diff --git a/kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-End-S8003-waits-when-firmware-.patch similarity index 100% rename from kernel/patches/drivers/gpu/0033-drm-imagination-apple-End-S8003-waits-when-firmware-.patch rename to kernel/patches/drivers/gpu/0034-drm-imagination-apple-End-S8003-waits-when-firmware-.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch b/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch similarity index 100% rename from kernel/patches/drivers/gpu/0034-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch rename to kernel/patches/drivers/gpu/0035-drm-imagination-apple-Capture-licensed-MMU-faults-be.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch similarity index 100% rename from kernel/patches/drivers/gpu/0035-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch rename to kernel/patches/drivers/gpu/0036-drm-imagination-apple-Honour-the-minimum-runtime-gat.patch diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch deleted file mode 100644 index ffe871ff..00000000 --- a/kernel/patches/drivers/gpu/0036-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch +++ /dev/null @@ -1,86 +0,0 @@ -From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 -From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= -Date: Mon, 16 Nov 2026 02:57:20 +0100 -Subject: [PATCH 36/40] drm/imagination: apple: Keep VM tags distinct from - firmware's cache - -A slot tag can wrap onto firmware's cached value, making a page-table -change invisible and retaining stale translations. Share the helper -that skips zero and the cached tag, retaining unsigned wrap and the -steps-past counter. Leave A10's generation bump unchanged until its -cached-generation offset is known. - -Affects: Apple A9 (S8003); A10 unchanged -Assisted-by: LLM ---- - drivers/gpu/drm/imagination/pvr_apple_chip.h | 9 +++++++++ - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 16 ++++++++++++---- - 2 files changed, 21 insertions(+), 4 deletions(-) - -diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index e223cf8..7c49546 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple_chip.h -+++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h -@@ -280,6 +280,15 @@ struct pvr_apple_chip { - void (*mmu_flush)(struct pvr_apple *apple); - }; - -+/* A new host tag must be nonzero and differ from the firmware's cached tag. */ -+static inline u32 pvr_apple_tag_bump(u32 tag, u32 step, u32 cached) -+{ -+ tag += step; -+ while (!tag || tag == cached) -+ tag += step; -+ return tag; -+} -+ - /* The heap is coherent memory shared with the firmware, not MMIO. */ - static inline u32 heap_read32(struct pvr_apple *apple, u32 offset) - { -diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index 23483f8..6a4affa 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c -+++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -@@ -275,6 +275,7 @@ struct pvr_apple_s8003 { - u32 gen; - spinlock_t tag_lock; /* @tag and the host's tags in the shared memory */ - u32 tag[S8003_SLOTS]; /* 0: slot not loaded */ -+ u64 tag_steps_past; /* cached or zero tags skipped, under tag_lock */ - DECLARE_BITMAP(slots, S8003_SLOTS); - DECLARE_BITMAP(parked, S8003_SLOTS); /* left on @park_vm's tables */ - u32 next_slot; -@@ -1409,23 +1410,30 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) - struct pvr_apple_s8003 *s = apple->chip_data; - unsigned long flags; - u32 slot; -+ u64 steps = 0, total; - - if (!s->shared.map) - return; - spin_lock_irqsave(&s->tag_lock, flags); - for (slot = S8003_SLOT_FIRST; slot < S8003_SLOTS; slot++) { -- u32 tag = s->tag[slot]; -+ u32 tag = s->tag[slot], cached, next; - - if (!tag) - continue; -- tag += S8003_TAG_CATALOGUE; -- if (!tag) -- tag = S8003_TAG_CATALOGUE; -+ cached = get32(s8003_object(s, S8003_TAG_SEEN) + 4 * slot); -+ next = pvr_apple_tag_bump(tag, S8003_TAG_CATALOGUE, cached); -+ steps += (u32)(next - (tag + S8003_TAG_CATALOGUE)) / S8003_TAG_CATALOGUE; -+ tag = next; - s->tag[slot] = tag; - put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, tag); - } -+ s->tag_steps_past += steps; -+ total = s->tag_steps_past; - spin_unlock_irqrestore(&s->tag_lock, flags); - wmb(); -+ if (steps) -+ dev_dbg_ratelimited(apple_dev(apple), -+ "MMU tag steps-past=%llu total=%llu\n", steps, total); - } - - static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch b/kernel/patches/drivers/gpu/0037-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch new file mode 100644 index 00000000..248f81e0 --- /dev/null +++ b/kernel/patches/drivers/gpu/0037-drm-imagination-apple-Keep-VM-tags-distinct-from-fir.patch @@ -0,0 +1,133 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 02:57:20 +0100 +Subject: [PATCH] drm/imagination: apple: Keep VM tags distinct from + firmware's cache + +A slot tag can wrap onto firmware's cached value, making a page-table +change invisible and retaining stale translations. Share the helper +that skips zero and the cached tag, retaining unsigned wrap and the +steps-past counter. Leave A10's generation bump unchanged until its +cached-generation offset is known. + +Use the slot bump helper for both immediate and deferred flushes, so a +loading slot keeps its tag stable and its pending bump skips cached tags. + +Affects: Apple A9 (S8003); A10 unchanged +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 9 +++++++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 33 +++++++++++++++++-------- + 2 files changed, 32 insertions(+), 10 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index e223cf8..7c49546 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -280,6 +280,15 @@ struct pvr_apple_chip { + void (*mmu_flush)(struct pvr_apple *apple); + }; + ++/* A new host tag must be nonzero and differ from the firmware's cached tag. */ ++static inline u32 pvr_apple_tag_bump(u32 tag, u32 step, u32 cached) ++{ ++ tag += step; ++ while (!tag || tag == cached) ++ tag += step; ++ return tag; ++} ++ + /* The heap is coherent memory shared with the firmware, not MMIO. */ + static inline u32 heap_read32(struct pvr_apple *apple, u32 offset) + { +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index e5523d7..b1e3498 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -275,6 +275,7 @@ struct pvr_apple_s8003 { + u32 gen; + spinlock_t tag_lock; /* @tag and the host's tags in the shared memory */ + u32 tag[S8003_SLOTS]; /* 0: slot not loaded */ ++ u64 tag_steps_past; /* cached or zero tags skipped, under tag_lock */ + DECLARE_BITMAP(loading, S8003_SLOTS); /* tag held until DC_SLOT completes */ + DECLARE_BITMAP(bump_pending, S8003_SLOTS); /* flush deferred while loading */ + DECLARE_BITMAP(slots, S8003_SLOTS); +@@ -1326,18 +1327,18 @@ static int s8003_device_control(struct pvr_apple *apple, const u32 (*cmd)[2], un + } + + /* Bump a loaded slot's tag with tag_lock held; leave empty slots at zero. */ +-static void s8003_slot_tag_bump(struct pvr_apple_s8003 *s, u32 slot) ++static u64 s8003_slot_tag_bump(struct pvr_apple_s8003 *s, u32 slot) + { +- u32 tag = s->tag[slot]; ++ u32 tag = s->tag[slot], cached, next; + + lockdep_assert_held(&s->tag_lock); + if (!tag) +- return; +- tag += S8003_TAG_CATALOGUE; +- if (!tag) +- tag = S8003_TAG_CATALOGUE; +- s->tag[slot] = tag; +- put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, tag); ++ return 0; ++ cached = get32(s8003_object(s, S8003_TAG_SEEN) + 4 * slot); ++ next = pvr_apple_tag_bump(tag, S8003_TAG_CATALOGUE, cached); ++ s->tag[slot] = next; ++ put32(s8003_object(s, S8003_TAG_HOST) + 4 * slot, next); ++ return (u32)(next - (tag + S8003_TAG_CATALOGUE)) / S8003_TAG_CATALOGUE; + } + + /* Point a pair of slots at a page-table root (0: at none) under @tag */ +@@ -1346,6 +1347,7 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + const u32 cmd[2][2] = { { S8003_DC_SLOT, slot[1] }, { S8003_DC_SLOT, slot[0] } }; + struct pvr_apple_s8003 *s = apple->chip_data; + unsigned long flags; ++ u64 steps = 0, total; + unsigned int e; + int err; + +@@ -1377,12 +1379,17 @@ static int s8003_slots_load(struct pvr_apple *apple, const u32 *slot, u64 root, + for (e = 0; e < 2; e++) { + __clear_bit(slot[e], s->loading); + if (test_bit(slot[e], s->bump_pending)) { +- s8003_slot_tag_bump(s, slot[e]); ++ steps += s8003_slot_tag_bump(s, slot[e]); + __clear_bit(slot[e], s->bump_pending); + } + } ++ s->tag_steps_past += steps; ++ total = s->tag_steps_past; + spin_unlock_irqrestore(&s->tag_lock, flags); + wmb(); ++ if (steps) ++ dev_dbg_ratelimited(apple_dev(apple), ++ "MMU tag steps-past=%llu total=%llu\n", steps, total); + return err; + } + +@@ -1440,6 +1447,7 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + { + struct pvr_apple_s8003 *s = apple->chip_data; + unsigned long flags; ++ u64 steps = 0, total; + u32 slot; + + if (!s->shared.map) +@@ -1449,10 +1457,15 @@ static void pvr_apple_s8003_mmu_flush(struct pvr_apple *apple) + if (test_bit(slot, s->loading)) + __set_bit(slot, s->bump_pending); + else +- s8003_slot_tag_bump(s, slot); ++ steps += s8003_slot_tag_bump(s, slot); + } ++ s->tag_steps_past += steps; ++ total = s->tag_steps_past; + spin_unlock_irqrestore(&s->tag_lock, flags); + wmb(); ++ if (steps) ++ dev_dbg_ratelimited(apple_dev(apple), ++ "MMU tag steps-past=%llu total=%llu\n", steps, total); + } + + static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx) diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch b/kernel/patches/drivers/gpu/0038-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch similarity index 100% rename from kernel/patches/drivers/gpu/0037-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch rename to kernel/patches/drivers/gpu/0038-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch diff --git a/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch b/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch similarity index 100% rename from kernel/patches/drivers/gpu/0038-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch rename to kernel/patches/drivers/gpu/0039-drm-imagination-apple-Report-each-VM-s-initial-PM-po.patch diff --git a/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch b/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch similarity index 100% rename from kernel/patches/drivers/gpu/0039-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch rename to kernel/patches/drivers/gpu/0040-drm-imagination-apple-Reset-promptly-after-a-firmwar.patch diff --git a/kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch b/kernel/patches/drivers/gpu/0041-drm-imagination-apple-Stop-firmware-before-releasing.patch similarity index 100% rename from kernel/patches/drivers/gpu/0040-drm-imagination-apple-Stop-firmware-before-releasing.patch rename to kernel/patches/drivers/gpu/0041-drm-imagination-apple-Stop-firmware-before-releasing.patch From 854e64f38b0805239035947b1ab7ab01684a2415 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 14:02:14 +0100 Subject: [PATCH 19/25] drm/imagination: Size MMU page-table preallocation by device address A 1 MiB buffer at BO offset zero can cross a 2 MiB device-VA boundary. Sizing the table reserve from the BO offset leaves it one table short, so mapping fails with ENOMEM on every retry despite available memory. Masked indices can also wrap across a parent boundary and underflow the allocation count. Count absolute regions through the inclusive last VA; retain the BO offset only for the scatter-gather cursor. Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") Affects: every PowerVR device Assisted-by: LLM --- ...able-preallocation-by-device-address.patch | 132 ++++++++++++++++++ 1 file changed, 132 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0042-drm-imagination-Size-MMU-page-table-preallocation-by-device-address.patch diff --git a/kernel/patches/drivers/gpu/0042-drm-imagination-Size-MMU-page-table-preallocation-by-device-address.patch b/kernel/patches/drivers/gpu/0042-drm-imagination-Size-MMU-page-table-preallocation-by-device-address.patch new file mode 100644 index 00000000..fdb34b71 --- /dev/null +++ b/kernel/patches/drivers/gpu/0042-drm-imagination-Size-MMU-page-table-preallocation-by-device-address.patch @@ -0,0 +1,132 @@ +From 9c707f186bc87d1d0dfc37fff8cf3bb07263776d Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 14:02:14 +0100 +Subject: [PATCH] drm/imagination: Size MMU page-table preallocation by device + address + +A 1 MiB buffer at BO offset zero can cross a 2 MiB device-VA boundary. +Sizing the table reserve from the BO offset leaves it one table short, +so mapping fails with ENOMEM on every retry despite available memory. +Masked indices can also wrap across a parent boundary and underflow the +allocation count. Count absolute regions through the inclusive last VA; +retain the BO offset only for the scatter-gather cursor. + +Fixes: ff5f643de0bf ("drm/imagination: Add GEM and VM related code") +Affects: every PowerVR device +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_mmu.c | 28 ++++++++++++++------------- + drivers/gpu/drm/imagination/pvr_mmu.h | 2 +- + drivers/gpu/drm/imagination/pvr_vm.c | 8 ++++---- + 3 files changed, 20 insertions(+), 18 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c +index a4996b9..e06d4b6 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.c ++++ b/drivers/gpu/drm/imagination/pvr_mmu.c +@@ -2348,7 +2348,8 @@ void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx) + * pvr_mmu_op_context_create() - Create an MMU op context. + * @ctx: MMU context associated with owning VM context. + * @sgt: Scatter gather table containing pages pinned for use by this context. +- * @sgt_offset: Start offset of the requested device-virtual memory mapping. ++ * @device_addr: Virtual device address at the start of the requested mapping. ++ * @sgt_offset: Offset into @sgt of the start of the requested mapping. + * @size: Size in bytes of the requested device-virtual memory mapping. For an + * unmapping, this should be zero so that no page tables are allocated. + * +@@ -2359,7 +2360,7 @@ void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx) + */ + struct pvr_mmu_op_context * + pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, struct sg_table *sgt, +- u64 sgt_offset, u64 size) ++ u64 device_addr, u64 sgt_offset, u64 size) + { + int err; + +@@ -2375,18 +2376,19 @@ pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, struct sg_table *sgt, + + if (size) { + /* +- * The number of page table objects we need to prealloc is +- * indicated by the mapping size, start offset and the sizes +- * of the areas mapped per PT or PD. The range calculation is +- * identical to that for the index into a table for a device +- * address, so we reuse those functions here. ++ * Count absolute regions touched by the inclusive device-VA range. ++ * BO offsets only locate the backing pages, and masked table indices ++ * wrap when a mapping crosses a parent table boundary. ++ * Existing tables do not consume preallocations, so this is an upper bound. + */ +- const u32 l1_start_idx = pvr_page_table_l2_idx(sgt_offset); +- const u32 l1_end_idx = pvr_page_table_l2_idx(sgt_offset + size); +- const u32 l1_count = l1_end_idx - l1_start_idx + 1; +- const u32 l0_start_idx = pvr_page_table_l1_idx(sgt_offset); +- const u32 l0_end_idx = pvr_page_table_l1_idx(sgt_offset + size); +- const u32 l0_count = l0_end_idx - l0_start_idx + 1; ++ const u64 first = device_addr; ++ const u64 last = device_addr + size - 1; ++ const u64 l1_count = ++ (last >> ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT) - ++ (first >> ROGUE_MMUCTRL_VADDR_PC_INDEX_SHIFT) + 1; ++ const u64 l0_count = ++ (last >> ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT) - ++ (first >> ROGUE_MMUCTRL_VADDR_PD_INDEX_SHIFT) + 1; + + /* + * Alloc and push page table entries until we have enough of +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.h b/drivers/gpu/drm/imagination/pvr_mmu.h +index a8ecd46..2c02d61 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.h ++++ b/drivers/gpu/drm/imagination/pvr_mmu.h +@@ -99,7 +99,7 @@ dma_addr_t pvr_mmu_get_root_table_dma_addr(struct pvr_mmu_context *ctx); + void pvr_mmu_op_context_destroy(struct pvr_mmu_op_context *op_ctx); + struct pvr_mmu_op_context * + pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, +- struct sg_table *sgt, u64 sgt_offset, u64 size); ++ struct sg_table *sgt, u64 device_addr, u64 sgt_offset, u64 size); + + int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, + u64 device_addr); +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index bad2742..3ba5fe3 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -290,7 +290,7 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, + goto err_bind_op_fini; + + bind_op->mmu_op_ctx = +- pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size); ++ pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, device_addr, offset, size); + err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); + if (err) { + bind_op->mmu_op_ctx = NULL; +@@ -332,7 +332,7 @@ pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op, + } + + bind_op->mmu_op_ctx = +- pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0); ++ pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, device_addr, 0, 0); + err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); + if (err) { + bind_op->mmu_op_ctx = NULL; +@@ -978,7 +978,7 @@ pvr_vm_map_io(struct pvr_vm_context *vm_ctx, phys_addr_t io_addr, u64 device_add + sg_dma_address(sgt.sgl) = io_addr; + sg_dma_len(sgt.sgl) = size; + +- op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, &sgt, 0, size); ++ op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, &sgt, device_addr, 0, size); + if (IS_ERR(op_ctx)) { + err = PTR_ERR(op_ctx); + goto err_free_table; +@@ -1009,7 +1009,7 @@ pvr_vm_unmap_io(struct pvr_vm_context *vm_ctx, u64 device_addr, u64 size) + { + struct pvr_mmu_op_context *op_ctx; + +- op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, 0, 0); ++ op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, NULL, device_addr, 0, 0); + if (WARN_ON(IS_ERR(op_ctx))) + return; + +-- +2.49.0 + From 6de4bcf2b9f2d7a909c34724c07f98a04253615a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:04 +0100 Subject: [PATCH 20/25] drm/imagination: apple: Let a chip's firmware interrupt drive job completion The poll work re-arms a 100 us timer for as long as any job is pending: about 10,000 wakeups a second whenever the GPU is busy, and a finished job is seen up to 100 us late. Let a chip hand the backend its firmware's interrupt. The hard IRQ handler acknowledges it at the source through the new irq_ack() op and kicks the poll work, which drains the firmware's rings and signals fences as before. The interrupt is requested with IRQF_NO_AUTOEN and enabled only while fences are pending, because firmware that is up but idle raises it as well. While it is enabled the poll work re-checks every irq_poll_us, except for PVR_APPLE_FAST_US after a chip reported a finished job (pvr_apple_firmware_progress()). The stamps and ring watermarks that make a job done do not all reach memory at the same moment, so those re-checks keep apple_poll_us. pvr_apple_firmware_event() is the one place that kicks the poll work for the firmware. The A10's RTKit receive callback now calls it as well, with the same behaviour as before. The A10 takes no interrupt here and keeps polling, because none of its firmware messages is known to mean completion. Affects: Apple A9 (S8003) and A10 (T8010); the A10 keeps polling, no behaviour change Assisted-by: LLM --- ...mware-interrupt-drive-job-completion.patch | 268 ++++++++++++++++++ 1 file changed, 268 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0043-drm-imagination-apple-Let-a-chips-firmware-interrupt-drive-job-completion.patch diff --git a/kernel/patches/drivers/gpu/0043-drm-imagination-apple-Let-a-chips-firmware-interrupt-drive-job-completion.patch b/kernel/patches/drivers/gpu/0043-drm-imagination-apple-Let-a-chips-firmware-interrupt-drive-job-completion.patch new file mode 100644 index 00000000..ce05c7ec --- /dev/null +++ b/kernel/patches/drivers/gpu/0043-drm-imagination-apple-Let-a-chips-firmware-interrupt-drive-job-completion.patch @@ -0,0 +1,268 @@ +From dbb68f39764e51b6cea88ae046c05c0d170de0aa Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:47:09 +0100 +Subject: [PATCH] drm/imagination: apple: Let a chip's firmware interrupt drive + job completion + +The poll work re-arms a 100 us timer for as long as any job is pending: +about 10,000 wakeups a second whenever the GPU is busy, and a finished +job is seen up to 100 us late. + +Let a chip hand the backend its firmware's interrupt. The hard IRQ handler +acknowledges it at the source through the new irq_ack() op and kicks the +poll work, which drains the firmware's rings and signals fences as +before. The interrupt is requested with IRQF_NO_AUTOEN and enabled only +while fences are pending, because firmware that is up but idle raises it +as well. While it is enabled the poll work re-checks every irq_poll_us, +except for PVR_APPLE_FAST_US after a chip reported a finished job +(pvr_apple_firmware_progress()). The stamps and ring watermarks that +make a job done do not all reach memory at the same moment, so those +re-checks keep apple_poll_us. + +pvr_apple_firmware_event() is the one place that kicks the poll work for +the firmware. The A10's RTKit receive callback now calls it as well, +with the same behaviour as before. The A10 takes no interrupt here and +keeps polling, because none of its firmware messages is known to mean +completion. + +Affects: Apple A9 (S8003) and A10 (T8010); the A10 keeps polling, no behaviour change + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 110 ++++++++++++++++-- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 17 +++ + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 2 +- + 3 files changed, 119 insertions(+), 10 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index b779060..0fa3264 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -37,6 +37,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -64,6 +65,13 @@ static unsigned int pvr_apple_poll_us = 100; + module_param_named(apple_poll_us, pvr_apple_poll_us, uint, 0644); + MODULE_PARM_DESC(apple_poll_us, "Apple A10: microseconds between job completion checks (default 100)"); + ++/* ++ * With the firmware's interrupt taken, checks stay apple_poll_us apart for ++ * this long after the firmware announced a completion: the stamps and ring ++ * watermarks that make a job done are not all in memory at once. ++ */ ++#define PVR_APPLE_FAST_US 1000 ++ + /* Idle time before runtime PM powers the GPU off; tunable in sysfs. */ + #define PVR_APPLE_AUTOSUSPEND_MS 1000 + +@@ -905,7 +913,7 @@ void pvr_apple_report_mmu_fault(struct pvr_apple *apple) + void pvr_apple_firmware_fault(struct pvr_apple *apple) + { + atomic_set(&apple->fault_pending, 1); +- mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++ pvr_apple_firmware_event(apple); + } + + /* A reset cancels every engine: fault only the oldest bound scheduler. */ +@@ -1016,13 +1024,85 @@ static void pvr_apple_jobs_retired(struct pvr_apple *apple, unsigned int n) + pm_runtime_put_autosuspend(dev); + } + +-/* Signal the fences of retired jobs; poll again while any are pending. */ ++/** ++ * pvr_apple_firmware_event() - The firmware posted something: look now ++ * @apple: Apple firmware state. ++ * ++ * Any context, hard IRQ included. ++ */ ++void pvr_apple_firmware_event(struct pvr_apple *apple) ++{ ++ /* After a terminal shutdown nothing may run the poll work again */ ++ if (!READ_ONCE(apple->stopping)) ++ mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++} ++ ++/** ++ * pvr_apple_firmware_progress() - The firmware announced a finished job ++ * @apple: Apple firmware state. ++ * ++ * For a chip's events(): check again soon, until the job is done in memory. ++ */ ++void pvr_apple_firmware_progress(struct pvr_apple *apple) ++{ ++ WRITE_ONCE(apple->fast_until, ktime_add_us(ktime_get(), PVR_APPLE_FAST_US)); ++} ++ ++static irqreturn_t pvr_apple_firmware_irq(int irq, void *data) ++{ ++ struct pvr_apple *apple = data; ++ ++ /* At its source first, as Apple's kext does before it reads the rings */ ++ apple->chip->irq_ack(apple); ++ pvr_apple_firmware_event(apple); ++ return IRQ_HANDLED; ++} ++ ++/** ++ * pvr_apple_firmware_irq_init() - Take the firmware's interrupt ++ * @apple: Apple firmware state. ++ * @irq: Its Linux interrupt number. ++ * ++ * For a chip's init(), with &pvr_apple_chip.irq_ack set. The interrupt is ++ * enabled only while jobs are pending: an idle firmware raises it too. ++ * ++ * Return: 0 or a negative error; on error the jobs are polled for. ++ */ ++int pvr_apple_firmware_irq_init(struct pvr_apple *apple, int irq) ++{ ++ struct device *dev = apple_dev(apple); ++ int err; ++ ++ if (!apple->chip->irq_ack || !apple->chip->irq_poll_us) ++ return -EINVAL; ++ err = devm_request_irq(dev, irq, pvr_apple_firmware_irq, IRQF_NO_AUTOEN, ++ "apple-gfx-fw", apple); ++ if (err) ++ return err; ++ apple->fw_irq = irq; ++ return 0; ++} ++ ++/* Enable the firmware's interrupt while jobs are pending; fence_lock held */ ++static void pvr_apple_fw_irq_set_locked(struct pvr_apple *apple, bool on) ++{ ++ lockdep_assert_held(&apple->fence_lock); ++ if (!apple->fw_irq || apple->fw_irq_on == on) ++ return; ++ apple->fw_irq_on = on; ++ if (on) ++ enable_irq(apple->fw_irq); ++ else ++ disable_irq_nosync(apple->fw_irq); ++} ++ ++/* Signal the fences of retired jobs; check again while any are pending. */ + static void pvr_apple_poll_work(struct work_struct *work) + { + struct pvr_apple *apple = container_of(work, struct pvr_apple, poll_work.work); + struct pvr_apple_fence *fence, *tmp; +- unsigned int retired = 0; +- bool pending = false; ++ unsigned int retired = 0, us; ++ bool pending = false, irq_on; + int engine; + + if (READ_ONCE(apple->stopping)) +@@ -1063,13 +1143,23 @@ static void pvr_apple_poll_work(struct work_struct *work) + apple->busy_ns += ktime_to_ns(ktime_sub(ktime_get(), apple->busy_since)); + apple->busy_since = 0; + } ++ pvr_apple_fw_irq_set_locked(apple, pending); ++ irq_on = apple->fw_irq_on; + spin_unlock_irq(&apple->fence_lock); + pvr_apple_jobs_retired(apple, retired); + +- /* The firmware sends no completion message in this setup: poll. */ +- if (pending && !READ_ONCE(apple->stopping)) +- hrtimer_start(&apple->poll_timer, us_to_ktime(max(pvr_apple_poll_us, 10U)), +- HRTIMER_MODE_REL); ++ if (!pending || READ_ONCE(apple->stopping)) ++ return; ++ /* ++ * Without the firmware's interrupt (the A10's firmware sends no ++ * completion message in this setup) poll. With it, check again ++ * soon only after an announced completion; otherwise the slower ++ * period backs the interrupt up. ++ */ ++ us = READ_ONCE(pvr_apple_poll_us); ++ if (irq_on && !ktime_before(ktime_get(), READ_ONCE(apple->fast_until))) ++ us = apple->chip->irq_poll_us; ++ hrtimer_start(&apple->poll_timer, us_to_ktime(max(us, 10U)), HRTIMER_MODE_REL); + } + + static enum hrtimer_restart pvr_apple_poll_timer(struct hrtimer *timer) +@@ -1152,6 +1242,7 @@ static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) + cancelled++; + } + } ++ pvr_apple_fw_irq_set_locked(apple, false); + spin_unlock_irq(&apple->fence_lock); + pvr_apple_jobs_retired(apple, cancelled); + return cancelled; +@@ -1182,8 +1273,9 @@ static struct dma_fence *pvr_apple_job_fence(struct pvr_apple *apple, struct pvr + if (!apple->busy_since) + apple->busy_since = ktime_get(); + list_add_tail(&fence->node, &apple->pending[engine]); ++ pvr_apple_fw_irq_set_locked(apple, true); + spin_unlock_irq(&apple->fence_lock); +- mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++ pvr_apple_firmware_event(apple); + return &fence->base; + } + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 09571e6..3b954d3 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -116,6 +116,16 @@ struct pvr_apple { + atomic_t fault_pending; /* a chip latched a new firmware fault */ + struct delayed_work poll_work; + struct hrtimer poll_timer; /* rearms poll_work while jobs are pending */ ++ /* ++ * The firmware's interrupt, if the chip has one the host can take ++ * (0: none). It is enabled while fences are pending (@fw_irq_on, ++ * under fence_lock) and kicks poll_work; poll_work then checks again ++ * every &pvr_apple_chip.irq_poll_us, and every apple_poll_us until ++ * @fast_until, which a completion the firmware announced sets. ++ */ ++ int fw_irq; ++ bool fw_irq_on; ++ ktime_t fast_until; + /* + * The last fragment job pushed: the next geometry job waits for it, as + * the firmware keeps one parameter-manager scene at a time. +@@ -260,6 +270,8 @@ struct pvr_apple_chip { + unsigned int scenes; + bool own_pm_pages; + unsigned int gate_off_ms; ++ /* With the firmware's interrupt taken: microseconds between checks while jobs are pending */ ++ unsigned int irq_poll_us; + + int (*init)(struct pvr_apple *apple); + int (*boot)(struct pvr_apple *apple); +@@ -284,6 +296,8 @@ struct pvr_apple_chip { + void (*vm_fini)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + unsigned int (*vm_scenes)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); + void (*mmu_flush)(struct pvr_apple *apple); ++ /* Acknowledge the firmware's interrupt at its source; hard IRQ context */ ++ void (*irq_ack)(struct pvr_apple *apple); + }; + + /* A new host tag must be nonzero and differ from the firmware's cached tag. */ +@@ -316,5 +330,8 @@ int pvr_apple_wait_idle(struct pvr_apple *apple, unsigned int timeout_us); + int pvr_apple_request_power_state(struct pvr_apple *apple, u32 state); + u32 pvr_apple_start_power_state(struct pvr_apple *apple); + int pvr_apple_fender_power_on(struct pvr_apple *apple); ++int pvr_apple_firmware_irq_init(struct pvr_apple *apple, int irq); ++void pvr_apple_firmware_event(struct pvr_apple *apple); ++void pvr_apple_firmware_progress(struct pvr_apple *apple); + + #endif /* PVR_APPLE_CHIP_H */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +index 19be745..cc841c3 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_t8010.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +@@ -113,7 +113,7 @@ static void pvr_apple_recv_message(void *cookie, u8 endpoint, u64 message) + + dev_dbg(apple_dev(apple), "firmware ep %#x: %#016llx\n", endpoint, message); + /* none is known to mean completion, but a check costs little */ +- mod_delayed_work(system_highpri_wq, &apple->poll_work, 0); ++ pvr_apple_firmware_event(apple); + } + + static void pvr_apple_crashed(void *cookie, const void *crashlog, size_t size) From 9db1af122254b0072efc68b9a4ae7de86a3bc504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:04 +0100 Subject: [PATCH 21/25] drm/imagination: apple: Take the S8003 firmware's interrupt The A9's firmware raises AIC interrupt 174, the node's fifth, by writing its bit to the AIC's software-set register (AIC+0x4014, bit 14). The apple-aic driver never clears a software-set interrupt, so taking it without help would fire again as soon as it is unmasked. That is why the backend polled for finished jobs every 100 us. Apple's kext clears the bit itself at the software-clear register (AIC+0x4094 for interrupt 174) before it reads the rings, and so does this. irq_ack() writes the bit to that word, which init() maps from the AIC's address in the device tree, and the shared code then takes the interrupt while jobs are pending. A stamp event read from the event ring marks progress, so completion is checked again at 100 us steps for a millisecond while the job's watermarks reach memory. Otherwise the backstop period is 1 ms, the period of s8003_touch() that the firmware needs to make progress at all. If the interrupt or its register cannot be had, the backend logs a warning and polls as before. Affects: Apple A9 (S8003) Assisted-by: LLM --- ...e-Take-the-S8003-firmwares-interrupt.patch | 155 ++++++++++++++++++ 1 file changed, 155 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0044-drm-imagination-apple-Take-the-S8003-firmwares-interrupt.patch diff --git a/kernel/patches/drivers/gpu/0044-drm-imagination-apple-Take-the-S8003-firmwares-interrupt.patch b/kernel/patches/drivers/gpu/0044-drm-imagination-apple-Take-the-S8003-firmwares-interrupt.patch new file mode 100644 index 00000000..d87fdff6 --- /dev/null +++ b/kernel/patches/drivers/gpu/0044-drm-imagination-apple-Take-the-S8003-firmwares-interrupt.patch @@ -0,0 +1,155 @@ +From 5b1057243a7d438ab937a5f603abed8257995d23 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:47:09 +0100 +Subject: [PATCH] drm/imagination: apple: Take the S8003 firmware's interrupt + +The A9's firmware raises AIC interrupt 174, the node's fifth, by writing +its bit to the AIC's software-set register (AIC+0x4014, bit 14). The +apple-aic driver never clears a software-set interrupt, so taking it +without help would fire again as soon as it is unmasked. That is why the +backend polled for finished jobs every 100 us. + +Apple's kext clears the bit itself at the software-clear register +(AIC+0x4094 for interrupt 174) before it reads the rings, and so does +this. irq_ack() writes the bit to that word, which init() maps from the +AIC's address in the device tree, and the shared code then takes the +interrupt while jobs are pending. A stamp event read from the event ring +marks progress, so completion is checked again at 100 us steps for a +millisecond while the job's watermarks reach memory. Otherwise the +backstop period is 1 ms, the period of s8003_touch() that the firmware +needs to make progress at all. + +If the interrupt or its register cannot be had, the backend logs a +warning and polls as before. + +Affects: Apple A9 (S8003) + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 52 +++++++++++++++++-- + 1 file changed, 48 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index b813f71..46c3602 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -24,9 +24,11 @@ + * mapped for it. + * + * Jobs are written as this firmware takes them, which is not the A10's way: +- * see "Jobs" below. The firmware's interrupt to the host is not taken (the +- * interrupt controller's driver cannot acknowledge it): its rings are read +- * while a job is waited for, and on a timer otherwise. ++ * see "Jobs" below. The firmware raises its interrupt to the host through ++ * the interrupt controller's software-set register, which that controller's ++ * driver never clears: the host clears it itself before it reads the rings, ++ * as Apple's kext does. It is taken while jobs are pending; otherwise the ++ * rings are read on a timer. + * + * The boot sequence, the layout of the shared memory, the job records and + * every value in them are from Berkay Kösebay's G5P driver, which runs the +@@ -96,6 +98,7 @@ + /* The interrupt controller's registers the firmware uses, from its base */ + #define AIC_TIME 0x1020 + #define AIC_SW_SET 0x4000 ++#define AIC_SW_CLR 0x4080 /* the host clears the firmware's interrupt here */ + + /* + * The firmware's GPU addresses: all in the GiB powervr's kernel VM keeps for +@@ -266,6 +269,7 @@ struct pvr_apple_s8003 { + ktime_t touched; /* s8003_touch() */ + bool failed; /* its rings' indices are not ours any more */ + bool fault; /* it posted an event that is neither "alive" nor a stamp */ ++ bool stamped; /* it posted a job's stamp since events() last looked */ + + /* + * Per VM (struct s8003_vm, by its VM context) and per start of the +@@ -292,6 +296,7 @@ struct pvr_apple_s8003 { + phys_addr_t rgx_phys; + phys_addr_t aic_phys; + u32 irq; /* the firmware's interrupt to the host, as the AIC numbers it */ ++ void __iomem *aic_sw_clr; /* its word of the AIC's software-clear registers */ + + struct s8003_layout layout; + u8 *image; /* the unpacked image, as the bootloader finds it */ +@@ -975,7 +980,9 @@ static int s8003_drain(struct pvr_apple *apple, bool *alive) + + if (type == S8003_EVENT_ALIVE) { + *alive = true; +- } else if (type != S8003_EVENT_STAMP) { ++ } else if (type == S8003_EVENT_STAMP) { ++ WRITE_ONCE(s->stamped, true); ++ } else { + dev_warn_ratelimited(apple_dev(apple), "firmware fault event type=%u slot=%u: %*ph\n", + type, get_unaligned_le32(e + 0x0c), + S8003_EVENT_SIZE - 4, e + 4); +@@ -1211,6 +1218,9 @@ static void pvr_apple_s8003_events(struct pvr_apple *apple) + WRITE_ONCE(s->failed, true); + dev_err(apple_dev(apple), "GFX firmware's rings are no longer read\n"); + } ++ /* A job's stamp: its watermarks follow; look again soon */ ++ if (xchg(&s->stamped, false)) ++ pvr_apple_firmware_progress(apple); + } + + static u32 s8003_submitted(struct pvr_apple *apple, enum pvr_apple_engine engine) +@@ -2477,6 +2487,36 @@ static void s8003_free_image(void *data) + kvfree(data); + } + ++/* Hard IRQ context: clear the software-set source, before the rings are read */ ++static void pvr_apple_s8003_irq_ack(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ writel(BIT(s->irq % 32), s->aic_sw_clr); ++} ++ ++/* ++ * The firmware's interrupt, the node's fifth. Without it (or its register) ++ * the jobs are polled for, as before. ++ */ ++static void s8003_irq_init(struct pvr_apple *apple) ++{ ++ struct device *dev = apple_dev(apple); ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ int irq, err; ++ ++ s->aic_sw_clr = devm_ioremap(dev, s->aic_phys + AIC_SW_CLR + 4 * (s->irq / 32), 4); ++ irq = platform_get_irq_optional(to_platform_device(dev), 4); ++ if (!s->aic_sw_clr) ++ err = -ENOMEM; ++ else if (irq < 0) ++ err = irq; ++ else ++ err = pvr_apple_firmware_irq_init(apple, irq); ++ if (err) ++ dev_warn(dev, "firmware interrupt not taken (%d): jobs are polled for\n", err); ++} ++ + static int pvr_apple_s8003_init(struct pvr_apple *apple) + { + struct device *dev = apple_dev(apple); +@@ -2506,6 +2546,7 @@ static int pvr_apple_s8003_init(struct pvr_apple *apple) + err = s8003_parse_dt(apple); + if (err) + return err; ++ s8003_irq_init(apple); + err = s8003_layout_plan(&s->layout); + if (err) + return err; +@@ -2648,6 +2689,8 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .scenes = 2, + .own_pm_pages = true, + .gate_off_ms = 10, ++ /* As often as s8003_touch() runs: the firmware needs those reads to go on */ ++ .irq_poll_us = 1000, + .init = pvr_apple_s8003_init, + .boot = pvr_apple_s8003_boot, + .start = pvr_apple_s8003_start, +@@ -2664,4 +2707,5 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .vm_fini = pvr_apple_s8003_vm_fini, + .vm_scenes = pvr_apple_s8003_vm_scenes, + .mmu_flush = pvr_apple_s8003_mmu_flush, ++ .irq_ack = pvr_apple_s8003_irq_ack, + }; From afa6f7e658975bffa3428a910d06015cd14ea59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:05 +0100 Subject: [PATCH 22/25] drm/imagination: apple: Touch only the S8003 PM sets the firmware may write While a job runs, s8003_touch() reads every cache line of the firmware's root and shared memory and of every PM set that some VM has taken, at most once a millisecond: about 9,700 non-cached loads, or 0.2 ms of CPU each time, with two clients. The firmware needs those reads to see its own progress. A set it is not writing gains nothing from them. Read only the sets the firmware may be writing: those with a geometry or fragment job that is not done yet, and those of the VMs that submitted the last two scenes, whose free lists the PM writes back when a later job runs (the reason for the park VM). Each set's last geometry and fragment job is kept for this, and the last two VMs' sets as a mask. Both are written under submit_lock and read without it. A set read for nothing costs time only. The root and shared memory are still read whole. On the iPad 5, restricting these reads was safe: the reads took 25 to 33 % less time, with no slower phase, timeout or fault. Also allow the 1 ms period an eighth of slack, so that a check made exactly every millisecond does not skip every other read. Affects: Apple A9 (S8003) Assisted-by: LLM --- ...S8003-PM-sets-the-firmware-may-write.patch | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0045-drm-imagination-apple-Touch-only-the-S8003-PM-sets-the-firmware-may-write.patch diff --git a/kernel/patches/drivers/gpu/0045-drm-imagination-apple-Touch-only-the-S8003-PM-sets-the-firmware-may-write.patch b/kernel/patches/drivers/gpu/0045-drm-imagination-apple-Touch-only-the-S8003-PM-sets-the-firmware-may-write.patch new file mode 100644 index 00000000..dc62ec44 --- /dev/null +++ b/kernel/patches/drivers/gpu/0045-drm-imagination-apple-Touch-only-the-S8003-PM-sets-the-firmware-may-write.patch @@ -0,0 +1,188 @@ +From 1d6b27d6b5d14daacedab40cb1a74ba12a70ec10 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:47:51 +0100 +Subject: [PATCH] drm/imagination: apple: Touch only the S8003 PM sets the + firmware may write + +While a job runs, s8003_touch() reads every cache line of the +firmware's root and shared memory and of every PM set that some VM has +taken, at most once a millisecond: about 9,700 non-cached loads, or +0.2 ms of CPU each time, with two clients. The firmware needs those +reads to see its own progress. A set it is not writing gains nothing +from them. + +Read only the sets the firmware may be writing: those with a geometry +or fragment job that is not done yet, and those of the VMs that +submitted the last two scenes, whose free lists the PM writes back when +a later job runs (the reason for the park VM). Each set's last geometry +and fragment job is kept for this, and the last two VMs' sets as a mask. +Both are written under submit_lock and read without it. A set read for +nothing costs time only. The root and shared memory are still read +whole. + +On the iPad 5, restricting these reads was safe: the reads +took 25 to 33 % less time, with no slower phase, timeout or fault. Also +allow the 1 ms period an eighth of slack, so that a check made exactly +every millisecond does not skip every other read. + +Affects: Apple A9 (S8003) + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 80 ++++++++++++++++++- + 1 file changed, 76 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 46c3602..dd2340e 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -289,6 +289,16 @@ struct pvr_apple_s8003 { + spinlock_t set_lock; /* @sets, @next_set, &s8003_pm.set */ + DECLARE_BITMAP(sets, S8003_PM_SETS); + u32 next_set; ++ /* ++ * 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 ++ * the sets of the VMs that submitted the last two scenes. Written ++ * under submit_lock, read without it. ++ */ ++ u32 set_geom[S8003_PM_SETS]; ++ u32 set_frag[S8003_PM_SETS]; ++ unsigned long wb_sets; ++ struct s8003_vm *pm_last, *pm_prev; /* last two scenes for cache-line reads */ + struct pvr_vm_context *park_vm; /* maps what every VM maps, nothing else */ + + /* Bus addresses the firmware is told: its registers' and ours */ +@@ -1180,27 +1190,73 @@ static void s8003_channels_init(struct pvr_apple_s8003 *s) + s8003_channel_addr(APPLE_ENGINE_COMPUTE), S8003_CU_TQ, S8003_CU_DEP); + } + ++/* How often s8003_touch() runs while the firmware works for the host */ ++#define S8003_TOUCH_US 1000 ++ ++static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine); ++ ++/* ++ * The PM sets the firmware may be writing: those with a geometry or fragment ++ * job not done yet, and those of the VMs whose scenes it handled last and ++ * the one before, whose free lists the PM writes back when a later job runs. ++ */ ++static unsigned long s8003_live_sets(struct pvr_apple_s8003 *s) ++{ ++ u32 geom = pvr_apple_s8003_engine_done(s->apple, APPLE_ENGINE_GEOM); ++ u32 frag = pvr_apple_s8003_engine_done(s->apple, APPLE_ENGINE_FRAG); ++ unsigned long live = READ_ONCE(s->wb_sets); ++ unsigned int set; ++ ++ BUILD_BUG_ON(S8003_PM_SETS > BITS_PER_LONG); ++ for (set = 0; set < S8003_PM_SETS; set++) { ++ u32 g = READ_ONCE(s->set_geom[set]), f = READ_ONCE(s->set_frag[set]); ++ ++ if ((g && (s32)(geom - g) < 0) || (f && (s32)(frag - f) < 0)) ++ live |= BIT(set); ++ } ++ return live & READ_ONCE(s->sets[0]); ++} ++ + /* + * While a job runs the firmware does not see its own progress in the root and + * shared memory until the host read those cache lines: without the reads a + * phase waited for the firmware's next tick, seconds in all (measured on the + * iPad 5: 7.9 s from input to frame, 0.27 s with them every 1 to 2 ms). One +- * load per 64-byte line, the PM sets in use included. ++ * load per 64-byte line, the PM sets the firmware may be writing included; ++ * reading only those cut the time of the reads by a quarter to a third on ++ * the G5P driver, with no slower phase. + */ + static void s8003_touch(struct pvr_apple_s8003 *s) + { +- unsigned long set; ++ unsigned long set, live = s8003_live_sets(s); + u32 at; + + for (at = 0; at < S8003_ROOT_SIZE; at += 64) + (void)READ_ONCE(*(const u32 *)(s->root.map + at)); + for (at = 0; at < S8003_SHARED_SIZE; at += 64) + (void)READ_ONCE(*(const u32 *)(s->shared.map + at)); +- for_each_set_bit(set, s->sets, S8003_PM_SETS) ++ for_each_set_bit(set, &live, S8003_PM_SETS) + for (at = 0; at < S8003_PM_SET_SIZE; at += 64) + (void)READ_ONCE(*(const u32 *)(s->pmsets.map + set * S8003_PM_SET_SIZE + at)); + } + ++static unsigned long s8003_vm_sets(const struct s8003_vm *vm) ++{ ++ unsigned long mask = 0; ++ unsigned int i; ++ ++ for (i = 0; vm && i < ARRAY_SIZE(vm->pm); i++) ++ if (vm->pm[i].set >= 0) ++ mask |= BIT(vm->pm[i].set); ++ return mask; ++} ++ ++/* After @pm_last or @pm_prev changed; submit_lock held */ ++static void s8003_wb_sets_update(struct pvr_apple_s8003 *s) ++{ ++ WRITE_ONCE(s->wb_sets, s8003_vm_sets(s->pm_last) | s8003_vm_sets(s->pm_prev)); ++} ++ + /* What the firmware posted since the last look; wherever a job is waited for */ + static void pvr_apple_s8003_events(struct pvr_apple *apple) + { +@@ -1210,7 +1266,8 @@ static void pvr_apple_s8003_events(struct pvr_apple *apple) + + if (!READ_ONCE(apple->booted) || !s->shared.map) + return; +- if (ktime_us_delta(now, READ_ONCE(s->touched)) >= USEC_PER_MSEC) { ++ /* An eighth early, so that a check every S8003_TOUCH_US always touches */ ++ if (ktime_us_delta(now, READ_ONCE(s->touched)) + S8003_TOUCH_US / 8 >= S8003_TOUCH_US) { + WRITE_ONCE(s->touched, now); + s8003_touch(s); + } +@@ -1587,6 +1644,11 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + __set_bit(vm->slot[0], s->parked); + __set_bit(vm->slot[1], s->parked); + } ++ if (s->pm_last == vm) ++ s->pm_last = NULL; ++ if (s->pm_prev == vm) ++ s->pm_prev = NULL; ++ s8003_wb_sets_update(s); + for (e = 0; e < 2; e++) { + if (vm->slot[e]) + __clear_bit(vm->slot[e], s->slots); +@@ -2231,9 +2293,16 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[0], false, + seq, 0, 0); + pm->hwrt = job->hwrt; ++ if (s->pm_last != vm) { ++ s->pm_prev = s->pm_last; ++ s->pm_last = vm; ++ } ++ s8003_wb_sets_update(s); + pm->scene = s->gen; + pm->ta_seq = seq; + pm->td_seq = 0; ++ WRITE_ONCE(s->set_frag[pm->set], 0); ++ WRITE_ONCE(s->set_geom[pm->set], seq); + apple->ta_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_GEOM, seq, (2 * seq) % S8003_CH_ENTRIES); + if (err) +@@ -2275,6 +2344,7 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + s8003_meta_addr(s8003_pm_set_va(pm, 0)), pm->set + 1, vm->slot[1], true, seq, + s8003_channel_addr(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq); + pm->td_seq = seq; ++ WRITE_ONCE(s->set_frag[pm->set], seq); + apple->frag_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); + if (err) +@@ -2437,6 +2507,8 @@ static void s8003_jobs_reset(struct pvr_apple_s8003 *s) + s->gen = 1; + WRITE_ONCE(s->failed, false); + WRITE_ONCE(s->fault, false); ++ memset(s->set_geom, 0, sizeof(s->set_geom)); ++ memset(s->set_frag, 0, sizeof(s->set_frag)); + memset(s->pmsets.map, 0, s->pmsets.size); + s8003_channels_init(s); + } From 1877376088380b943fb7444e04041e9dcb727ae5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:05 +0100 Subject: [PATCH 23/25] drm/imagination: apple: Send the S8003 PM record only for a set that is not clean Every geometry job starts with the PM record command {6, slot, PM id, set}. It has the firmware take the PM state from the set and rebuild the set's free list from its block list, which is per-job firmware time. Earlier hardware measurements sent it only when the set was not known to be clean, and otherwise {4, the geometry channel's stamp, seq - 1}, a wait that is already satisfied. On the iPad 5 that cut a small render pass by a third and raised glmark2 by 6 to 35 % per scene, with no fault, timeout or free-list drift. It let the next geometry job run under the previous fragment job, as on iOS. Do the same. A set is clean for a geometry job when: - it is not built anew for the job (same firmware start, pool large enough); - its last scene ran to its end in this firmware start: geometry and fragment job both handed over, the fragment job done, no fault latched. A scene without a fragment job gave no pages back and leaves the set dirty. So does a job that failed before it reached the firmware. A firmware start (every runtime resume, every reset) builds the sets anew. Unlike in earlier hardware measurements, a geometry job here can be written while the one before it still runs. The {4} form would then make the firmware wait on its own stamp, which it sees late without the host's reads. So the record is skipped only when job seq - 1 is already done. Affects: Apple A9 (S8003) Assisted-by: LLM --- ...ord-only-for-a-set-that-is-not-clean.patch | 164 ++++++++++++++++++ 1 file changed, 164 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0046-drm-imagination-apple-Send-the-S8003-PM-record-only-for-a-set-that-is-not-clean.patch diff --git a/kernel/patches/drivers/gpu/0046-drm-imagination-apple-Send-the-S8003-PM-record-only-for-a-set-that-is-not-clean.patch b/kernel/patches/drivers/gpu/0046-drm-imagination-apple-Send-the-S8003-PM-record-only-for-a-set-that-is-not-clean.patch new file mode 100644 index 00000000..ee944618 --- /dev/null +++ b/kernel/patches/drivers/gpu/0046-drm-imagination-apple-Send-the-S8003-PM-record-only-for-a-set-that-is-not-clean.patch @@ -0,0 +1,164 @@ +From afae9981d9f7324e30089e24c02d1cdca4e6fb24 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:47:51 +0100 +Subject: [PATCH] drm/imagination: apple: Send the S8003 PM record only for a + set that is not clean + +Every geometry job starts with the PM record command {6, slot, PM id, +set}. It has the firmware take the PM state from the set and rebuild +the set's free list from its block list, which is per-job firmware +time. Earlier hardware measurements sent it only when the set was not known to be +clean, and otherwise {4, the geometry channel's stamp, seq - 1}, a wait +that is already satisfied. On the iPad 5 that cut a small render pass +by a third and raised glmark2 by 6 to 35 % per scene, with no fault, +timeout or free-list drift. It let the next geometry job run under the +previous fragment job, as on iOS. + +Do the same. A set is clean for a geometry job when: +- it is not built anew for the job (same firmware start, pool large + enough); +- its last scene ran to its end in this firmware start: geometry and + fragment job both handed over, the fragment job done, no fault + latched. + +A scene without a fragment job gave no pages back and leaves the set +dirty. So does a job that failed before it reached the firmware. A +firmware start (every runtime resume, every reset) builds the sets anew. + +Unlike in earlier hardware measurements, a geometry job here can be written while the +one before it still runs. The {4} form would then make the firmware +wait on its own stamp, which it sees late without the host's reads. So +the record is skipped only when job seq - 1 is already done. + +Affects: Apple A9 (S8003) + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 49 ++++++++++++++++--- + 1 file changed, 43 insertions(+), 6 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index dd2340e..7f57479 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -252,6 +252,7 @@ struct s8003_pm { + u32 scene; /* the firmware start the scene belongs to; 0: none */ + u32 ta_seq; /* its geometry job */ + u32 td_seq; /* its fragment job; 0: not submitted */ ++ bool clean; /* both of its jobs were handed to the firmware */ + }; + + struct s8003_vm { +@@ -2081,11 +2082,13 @@ static u8 *useq_put(u8 *out, const u32 *words, unsigned int n) + * dependency object, PM set and its scene record, microsequence, stamps), the + * microsequence, the command and both pointer-ring entries. @pm is the PM + * set's META address, @pm_id its number. A geometry job's command hands over +- * the PM set; a fragment job's waits for the stamp at @wait_stamp to reach +- * @wait_seq, its geometry job's. ++ * the PM set, unless @pm_record is false: then it only names the previous ++ * geometry job's stamp, which that job already reached, as the G5P driver ++ * did for a clean set. A fragment job's waits for the stamp at @wait_stamp to ++ * reach @wait_seq, its geometry job's. + */ + 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) ++ u32 seq, u32 wait_stamp, u32 wait_seq, bool pm_record) + { + struct s8003_job_slot at = s8003_slot_of(seq, frag); + u32 w = addr + at.w, record = pm + S8003_PM_RECORD; +@@ -2162,11 +2165,16 @@ static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 + put32(cmd + 4, wait_stamp); + put32(cmd + 8, wait_seq); + put32(cmd + 12, wait_seq & 0x7fffffff); +- } else { ++ } else if (pm_record) { + put32(cmd, S8003_CMD_PM); + put32(cmd + 4, slot); + put32(cmd + 8, pm_id); + put32(cmd + 12, pm); ++ } else { ++ put32(cmd, S8003_CMD_WAIT); ++ put32(cmd + 4, addr + S8003_CH_STAMP + 4); ++ put32(cmd + 8, seq - 1); ++ put32(cmd + 12, (seq - 1) & 0x7fffffff); + } + put32(p + S8003_CH_RING + ring * 4, addr + at.cmd); + put32(p + S8003_CH_RING + ((ring + 1) % S8003_CH_ENTRIES) * 4, w); +@@ -2229,6 +2237,22 @@ static bool s8003_pm_idle(struct pvr_apple *apple, const struct s8003_pm *pm) + return false; + } + ++/* ++ * Whether a geometry job on @pm, which needs @blocks, may go without the PM ++ * record, which has the firmware rebuild the set's free list from its block ++ * list: the set is not built anew for the job, and its last scene ran to its ++ * end in this start of the firmware (both jobs handed over, the fragment job ++ * done, no fault). A scene without a fragment job gave no pages back. ++ */ ++static bool s8003_pm_clean(struct pvr_apple *apple, const struct s8003_pm *pm, u32 blocks) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return pm->clean && pm->built == s->gen && pm->blocks >= blocks && ++ pm->scene == s->gen && pm->td_seq && !READ_ONCE(s->fault) && ++ (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_FRAG) - pm->td_seq) >= 0; ++} ++ + static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, + const char *engine, int err, const char *reason) + { +@@ -2246,6 +2270,7 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + struct s8003_pm *pm = NULL; + struct s8003_rt rt; + u32 seq, blocks; ++ bool pm_record; + unsigned int i; + int err; + +@@ -2270,6 +2295,9 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + /* As many 128 KiB blocks as the free list has 4 KiB pages, and enough for the target */ + blocks = max(DIV_ROUND_UP(hwrt->free_lists[0]->initial_num_pages, 32), + DIV_ROUND_UP(50 + rt.tiles, 32)); ++ /* Before the build, which may make the set anew */ ++ pm_record = !s8003_pm_clean(apple, pm, min(blocks, S8003_PM_BLOCKS_MAX)); ++ pm->clean = false; + err = s8003_pm_build(apple, vm, pm, min(blocks, S8003_PM_BLOCKS_MAX)); + if (!err && !vm->pm_logged) { + dev_info(apple_dev(apple), +@@ -2287,11 +2315,19 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + return s8003_submit_error(apple, vm, "geometry", err, "record slot wait"); + + seq = apple->ta_seq + 1; ++ /* ++ * Without the record the job waits for the previous geometry job's ++ * stamp; only if it is there already, so that the firmware never ++ * waits on its own stamp for it. ++ */ ++ if (!pm_record && ++ (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_GEOM) - apple->ta_seq) < 0) ++ pm_record = true; + s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); + s8003_job_write(p, s8003_channel_addr(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, 0, 0); ++ seq, 0, 0, pm_record); + pm->hwrt = job->hwrt; + if (s->pm_last != vm) { + s->pm_prev = s->pm_last; +@@ -2342,13 +2378,14 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + s8003_job_write(p, s8003_channel_addr(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(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq); ++ s8003_channel_addr(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, pm->ta_seq, true); + pm->td_seq = seq; + WRITE_ONCE(s->set_frag[pm->set], seq); + apple->frag_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); + if (err) + return s8003_submit_error(apple, vm, "fragment", err, "publish"); ++ pm->clean = true; + return seq; + } + From 9de39b98ae2c1e0bb47b91fbb0692b4f717902b8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:05 +0100 Subject: [PATCH 24/25] drm/imagination: apple: Grow an S8003 PM pool from firmware feedback An S8003 scene that exhausts its PM pool stops the firmware. Dataset sizing alone does not incorporate what previous scenes actually used. Read the firmware high-water mark, margin and bounded growth request from the completed set's list page, following its taken-flag protocol. At most every 167 ms per VM, grow to three times high-water or one block plus the margin, otherwise by the request (at most eight blocks). Ignore impossible values and retain the existing pool cap. Take feedback in the geometry submission's process context, under the existing submit lock, just before the existing idle-set pool allocator. No reclaim or prepared-pool infrastructure is needed by this change. Affects: Apple A9 (S8003). The A10 is unchanged. Assisted-by: LLM --- ...S8003-PM-pool-from-firmware-feedback.patch | 117 ++++++++++++++++++ 1 file changed, 117 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0047-drm-imagination-apple-Grow-an-S8003-PM-pool-from-firmware-feedback.patch diff --git a/kernel/patches/drivers/gpu/0047-drm-imagination-apple-Grow-an-S8003-PM-pool-from-firmware-feedback.patch b/kernel/patches/drivers/gpu/0047-drm-imagination-apple-Grow-an-S8003-PM-pool-from-firmware-feedback.patch new file mode 100644 index 00000000..1e8da7cc --- /dev/null +++ b/kernel/patches/drivers/gpu/0047-drm-imagination-apple-Grow-an-S8003-PM-pool-from-firmware-feedback.patch @@ -0,0 +1,117 @@ +From 6e9fa37f11a035f3159d387c618a85836a340a7b Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:48:14 +0100 +Subject: [PATCH] drm/imagination: apple: Grow an S8003 PM pool from firmware + feedback + +An S8003 scene that exhausts its PM pool stops the firmware. Dataset +sizing alone does not incorporate what previous scenes actually used. + +Read the firmware high-water mark, margin and bounded growth request from +the completed set's list page, following its taken-flag protocol. At most +every 167 ms per VM, grow to three times high-water or one block plus the +margin, otherwise by the request (at most eight blocks). Ignore impossible +values and retain the existing pool cap. + +Take feedback in the geometry submission's process context, under the +existing submit lock, just before the existing idle-set pool allocator. +No reclaim or prepared-pool infrastructure is needed by this change. + +Affects: Apple A9 (S8003). The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 63 +++++++++++++++++++ + 1 file changed, 63 insertions(+) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 7f57479..566b706 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -261,6 +261,8 @@ struct s8003_vm { + u32 attached; /* the firmware start the slots were loaded for */ + struct s8003_pm pm[2]; + bool pm_logged; /* first pool sizing report for this VM */ ++ u32 fb_blocks; /* pool size the firmware's feedback asked for */ ++ ktime_t fb_at; /* when that feedback was last taken */ + }; + + struct pvr_apple_s8003 { +@@ -2253,6 +2255,63 @@ static bool s8003_pm_clean(struct pvr_apple *apple, const struct s8003_pm *pm, u + (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_FRAG) - pm->td_seq) >= 0; + } + ++/* ++ * The firmware reports, at 0x40 of a set's list page, the most pages of the ++ * pool a scene used and a margin (4 KiB pages) and a request for more ++ * blocks; 0x60 is 1 once the host took them. Apple's kext grows the pool ++ * from them between scenes (AGXParameterManagementMeta:: ++ * growPoolFromFirmwareFeedback): to three times the high-water mark or the ++ * margin plus a block, else by the request, at most every 0x3d0901 ticks ++ * of 24 MHz. ++ */ ++#define S8003_PM_FEEDBACK 0x40 ++#define S8003_PM_FEEDBACK_TAKEN 0x60 ++#define S8003_PM_FEEDBACK_MS 167 ++#define S8003_PM_GROW_MAX 8 /* blocks per request */ ++ ++/* Take a VM's idle sets' feedback; submit_lock held */ ++static void s8003_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ktime_t now = ktime_get(); ++ unsigned int i; ++ ++ lockdep_assert_held(&apple->submit_lock); ++ if (!apple->booted || (vm->fb_at && ktime_ms_delta(now, vm->fb_at) < S8003_PM_FEEDBACK_MS)) ++ return; ++ for (i = 0; i < ARRAY_SIZE(vm->pm); i++) { ++ struct s8003_pm *pm = &vm->pm[i]; ++ u32 high, margin, request, target; ++ u8 *list; ++ ++ if (pm->set < 0 || pm->built != s->gen || !s8003_pm_idle(apple, pm)) ++ continue; ++ list = s->pmsets.map + pm->set * S8003_PM_SET_SIZE + PVR_DEVICE_PAGE_SIZE; ++ if (get32(list + S8003_PM_FEEDBACK_TAKEN) == 1) ++ continue; ++ rmb(); /* the flag before the words it covers */ ++ 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); ++ put32(list + S8003_PM_FEEDBACK_TAKEN, 1); ++ wmb(); ++ vm->fb_at = now; ++ /* Not from this pool: ignored */ ++ if (high > pm->blocks * 32 || margin > S8003_PM_BLOCKS_MAX * 32) ++ continue; ++ target = DIV_ROUND_UP(max(3 * high, 32 + margin), 32); ++ if (target <= pm->blocks && request) ++ target = pm->blocks + request; ++ target = min(target, S8003_PM_BLOCKS_MAX); ++ if (target <= max(vm->fb_blocks, pm->blocks)) ++ continue; ++ vm->fb_blocks = target; ++ dev_info(apple_dev(apple), ++ "PM pool VM=%p grows to %u blocks from feedback: high-water %u pages, margin %u, request %u\n", ++ vm->vm_ctx, target, high, margin, request); ++ } ++} ++ + static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, + const char *engine, int err, const char *reason) + { +@@ -2292,9 +2351,13 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + if (!pm) + return s8003_submit_error(apple, vm, "geometry", -EBUSY, "no PM set"); + ++ /* Process context, submit_lock held: grow through the existing pool allocator. */ ++ s8003_pm_feedback(apple, vm); + /* As many 128 KiB blocks as the free list has 4 KiB pages, and enough for the target */ + blocks = max(DIV_ROUND_UP(hwrt->free_lists[0]->initial_num_pages, 32), + DIV_ROUND_UP(50 + rt.tiles, 32)); ++ /* The existing build path allocates a larger pool while this set is idle. */ ++ blocks = max(blocks, vm->fb_blocks); + /* Before the build, which may make the set anew */ + pm_record = !s8003_pm_clean(apple, pm, min(blocks, S8003_PM_BLOCKS_MAX)); + pm->clean = false; From d8eaee5abe50508d76ab6e809b0c5880dc97feab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:49:05 +0100 Subject: [PATCH 25/25] drm/imagination: apple: Report runtime PM cycles and job waits in debugfs Runtime suspend stops the firmware and the next job starts it again, but host in-flight durations do not show that cycle's cost or the work spent checking for completion. Expose a read-only apple_jobs file and snapshot job progress without powering up the GPU. Report firmware starts and failures, gate-off waits, boot/resume/first-job latency, suspend duration, IRQs and poll iterations. Let each chip add its own measurements; S8003 reports cache-line read time, PM record use, pool feedback and its wait loops. Keep the existing power policy, submission order and completion criteria unchanged. Affects: Apple A9 (S8003) and A10 (T8010), debugfs measurements only. Assisted-by: LLM --- ...e-PM-cycles-and-job-waits-in-debugfs.patch | 551 ++++++++++++++++++ 1 file changed, 551 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0048-drm-imagination-apple-Report-runtime-PM-cycles-and-job-waits-in-debugfs.patch diff --git a/kernel/patches/drivers/gpu/0048-drm-imagination-apple-Report-runtime-PM-cycles-and-job-waits-in-debugfs.patch b/kernel/patches/drivers/gpu/0048-drm-imagination-apple-Report-runtime-PM-cycles-and-job-waits-in-debugfs.patch new file mode 100644 index 00000000..e3857a5d --- /dev/null +++ b/kernel/patches/drivers/gpu/0048-drm-imagination-apple-Report-runtime-PM-cycles-and-job-waits-in-debugfs.patch @@ -0,0 +1,551 @@ +From 1b06b36e5416d50260b5a4dff2b37de51c51bf3f Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:49:04 +0100 +Subject: [PATCH] drm/imagination: apple: Report runtime PM cycles and job + waits in debugfs + +Runtime suspend stops the firmware and the next job starts it again, but +host in-flight durations do not show that cycle's cost or the work spent +checking for completion. + +Expose a read-only apple_jobs file and snapshot job progress without +powering up the GPU. Report firmware starts and failures, gate-off waits, +boot/resume/first-job latency, suspend duration, IRQs and poll iterations. +Let each chip add its own measurements; S8003 reports cache-line read time, +PM record use, pool feedback and its wait loops. Keep the existing power +policy, submission order and completion criteria unchanged. + +Affects: Apple A9 (S8003) and A10 (T8010), debugfs measurements only. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 159 +++++++++++++++++- + drivers/gpu/drm/imagination/pvr_apple.h | 3 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 28 +++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 56 +++++- + drivers/gpu/drm/imagination/pvr_debugfs.c | 3 + + 5 files changed, 244 insertions(+), 5 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 0fa3264..1108abd 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -31,6 +31,7 @@ + + #include + #include ++#include + #include + #include + #include +@@ -45,6 +46,7 @@ + #include + #include + #include ++#include + #include + #include + +@@ -420,16 +422,30 @@ static void pvr_apple_fw_stop(struct pvr_apple *apple); + * after a reset. At probe, before the init data exists, power on only boots + * the firmware: pvr_apple_fw_init() follows. + */ ++static void pvr_apple_stat_add(struct pvr_apple_stat *stat, s64 us) ++{ ++ stat->last = max(us, 0LL); ++ stat->max = max(stat->max, stat->last); ++ stat->total += stat->last; ++} ++ + static int pvr_apple_power_on(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); ++ ktime_t start = ktime_get(); ++ unsigned long flags; + ++ WRITE_ONCE(apple->power_on_at, start); + if (apple->chip->gate_off_ms && apple->gate_off) { + s64 remaining = (s64)apple->chip->gate_off_ms * USEC_PER_MSEC - +- ktime_us_delta(ktime_get(), apple->gate_off); ++ ktime_us_delta(start, apple->gate_off); + +- if (remaining > 0) ++ if (remaining > 0) { + msleep(DIV_ROUND_UP(remaining, USEC_PER_MSEC)); ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ pvr_apple_stat_add(&apple->rpm_gate_wait, ktime_us_delta(ktime_get(), start)); ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++ } + } + /* The driver core resumes a device before .remove(). Powering the + * domain must not boot an idle firmware just to tear it down again. +@@ -443,12 +459,31 @@ static int pvr_apple_power_on(struct pvr_device *pvr_dev) + int pvr_apple_prepare_job(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); ++ ktime_t start = ktime_get(), on; ++ unsigned long flags; ++ bool resumed; + int err; + + mutex_lock(&apple->submit_lock); ++ resumed = !apple->booted; + err = READ_ONCE(apple->stopping) ? -ENODEV : + apple->booted ? 0 : pvr_apple_fw_restart(apple); + mutex_unlock(&apple->submit_lock); ++ if (resumed) { ++ /* A start after a reset has no power-on of its own */ ++ on = xchg(&apple->power_on_at, 0); ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ apple->rpm_resumes++; ++ if (err) { ++ apple->rpm_resume_failed++; ++ } else { ++ pvr_apple_stat_add(&apple->rpm_boot, ktime_us_delta(ktime_get(), start)); ++ if (on) ++ pvr_apple_stat_add(&apple->rpm_resume, ktime_us_delta(ktime_get(), on)); ++ apple->first_job_from = on ?: start; ++ } ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++ } + if (err) + dev_err(apple_dev(apple), "GPU did not start: %d\n", err); + else if (READ_ONCE(apple->devfreq_suspended)) +@@ -459,6 +494,8 @@ int pvr_apple_prepare_job(struct pvr_device *pvr_dev) + static int pvr_apple_power_off(struct pvr_device *pvr_dev) + { + struct pvr_apple *apple = to_apple(pvr_dev); ++ ktime_t start = ktime_get(); ++ unsigned long flags; + int err; + + /* Terminal shutdown already stopped firmware; never wait on submitters. */ +@@ -488,6 +525,10 @@ static int pvr_apple_power_off(struct pvr_device *pvr_dev) + cancel_delayed_work_sync(&apple->poll_work); + if (apple->chip->gate_off_ms) + apple->gate_off = ktime_get(); ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ apple->rpm_suspends++; ++ pvr_apple_stat_add(&apple->rpm_suspend, ktime_us_delta(ktime_get(), start)); ++ spin_unlock_irqrestore(&apple->fence_lock, flags); + return 0; + } + +@@ -641,6 +682,7 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine + /* For a wait: what the firmware posted meanwhile is read first */ + static u32 pvr_apple_engine_poll(struct pvr_apple *apple, enum pvr_apple_engine engine) + { ++ atomic64_inc(&apple->wait_polls); + if (!READ_ONCE(apple->stopping) && apple->chip->events) + apple->chip->events(apple); + return pvr_apple_engine_done(apple, engine); +@@ -658,6 +700,108 @@ static u32 pvr_apple_engine_last(struct pvr_apple *apple, enum pvr_apple_engine + } + } + ++static void pvr_apple_stat_show(struct seq_file *s, const char *name, ++ const struct pvr_apple_stat *stat) ++{ ++ seq_printf(s, "%s last %llu max %llu total %llu\n", name, stat->last, stat->max, ++ stat->total); ++} ++ ++static int pvr_apple_jobs_show(struct seq_file *s, void *unused) ++{ ++ struct pvr_apple *apple = s->private; ++ struct device *dev = apple_dev(apple); ++ static const char *const names[APPLE_ENGINES] = { ++ [APPLE_ENGINE_GEOM] = "geometry", ++ [APPLE_ENGINE_FRAG] = "fragment", ++ [APPLE_ENGINE_COMPUTE] = "compute", ++ }; ++ struct pvr_apple_stat gate_wait, boot, resume, first_job, suspend; ++ u32 submitted[APPLE_ENGINES], done[APPLE_ENGINES]; ++ unsigned int pending[APPLE_ENGINES] = {}; ++ u64 resumes, resume_failed, suspends, poll_runs; ++ struct pvr_apple_fence *fence; ++ u64 retired_total, busy_ns; ++ bool irq_on; ++ unsigned long flags; ++ bool booted, fault = false; ++ const char *runtime_status; ++ int engine; ++ ++ mutex_lock(&apple->submit_lock); ++ booted = apple->booted; ++ for (engine = 0; engine < APPLE_ENGINES; engine++) { ++ submitted[engine] = pvr_apple_engine_last(apple, engine); ++ done[engine] = pvr_apple_engine_done(apple, engine); ++ } ++ if (apple->chip->fault) ++ fault = apple->chip->fault(apple); ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) ++ list_for_each_entry(fence, &apple->pending[engine], node) ++ pending[engine]++; ++ retired_total = apple->retired_total; ++ busy_ns = apple->busy_ns; ++ gate_wait = apple->rpm_gate_wait; ++ boot = apple->rpm_boot; ++ resume = apple->rpm_resume; ++ first_job = apple->rpm_first_job; ++ suspend = apple->rpm_suspend; ++ resumes = apple->rpm_resumes; ++ resume_failed = apple->rpm_resume_failed; ++ suspends = apple->rpm_suspends; ++ poll_runs = apple->poll_runs; ++ irq_on = apple->fw_irq_on; ++ spin_unlock_irqrestore(&apple->fence_lock, flags); ++ mutex_unlock(&apple->submit_lock); ++ ++ switch (READ_ONCE(dev->power.runtime_status)) { ++ case RPM_ACTIVE: ++ runtime_status = "active"; ++ break; ++ case RPM_RESUMING: ++ runtime_status = "resuming"; ++ break; ++ case RPM_SUSPENDED: ++ runtime_status = "suspended"; ++ break; ++ default: ++ runtime_status = "suspending"; ++ break; ++ } ++ seq_printf(s, "booted %u\n", booted); ++ for (engine = 0; engine < APPLE_ENGINES; engine++) ++ seq_printf(s, "%s submitted %u done %u pending %u\n", names[engine], ++ submitted[engine], done[engine], pending[engine]); ++ seq_printf(s, "retired_total %llu\nbusy_ns %llu\n", retired_total, busy_ns); ++ if (apple->chip->fault) ++ seq_printf(s, "fault %u\n", fault); ++ seq_printf(s, "runtime_pm_status %s\n", runtime_status); ++ seq_printf(s, "fw_irq %s irqs %lld\n", !apple->fw_irq ? "none" : irq_on ? "on" : "off", ++ atomic64_read(&apple->fw_irqs)); ++ seq_printf(s, "poll_runs %llu wait_polls %lld\n", poll_runs, ++ atomic64_read(&apple->wait_polls)); ++ seq_printf(s, "rpm_resumes %llu failed %llu\n", resumes, resume_failed); ++ pvr_apple_stat_show(s, "rpm_gate_wait_us", &gate_wait); ++ pvr_apple_stat_show(s, "rpm_boot_us", &boot); ++ pvr_apple_stat_show(s, "rpm_resume_us", &resume); ++ pvr_apple_stat_show(s, "rpm_first_job_us", &first_job); ++ seq_printf(s, "rpm_suspends %llu\n", suspends); ++ pvr_apple_stat_show(s, "rpm_suspend_us", &suspend); ++ if (apple->chip->debugfs_show) ++ apple->chip->debugfs_show(apple, s); ++ ++ return 0; ++} ++DEFINE_SHOW_ATTRIBUTE(pvr_apple_jobs); ++ ++void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root) ++{ ++ if (pvr_dev->apple) ++ debugfs_create_file("apple_jobs", 0400, root, pvr_dev->apple, ++ &pvr_apple_jobs_fops); ++} ++ + static int pvr_apple_wait_engine(struct pvr_apple *apple, enum pvr_apple_engine engine, u32 seq, + unsigned int timeout_us) + { +@@ -1016,9 +1160,13 @@ static u32 pvr_apple_frag_events(struct pvr_apple *apple) + static void pvr_apple_jobs_retired(struct pvr_apple *apple, unsigned int n) + { + struct device *dev = apple_dev(apple); ++ unsigned long flags; + + if (!n) + return; ++ spin_lock_irqsave(&apple->fence_lock, flags); ++ apple->retired_total += n; ++ spin_unlock_irqrestore(&apple->fence_lock, flags); + pm_runtime_mark_last_busy(dev); + while (n--) + pm_runtime_put_autosuspend(dev); +@@ -1052,6 +1200,7 @@ static irqreturn_t pvr_apple_firmware_irq(int irq, void *data) + { + struct pvr_apple *apple = data; + ++ atomic64_inc(&apple->fw_irqs); + /* At its source first, as Apple's kext does before it reads the rings */ + apple->chip->irq_ack(apple); + pvr_apple_firmware_event(apple); +@@ -1139,6 +1288,12 @@ static void pvr_apple_poll_work(struct work_struct *work) + } + pending |= !list_empty(&apple->pending[engine]); + } ++ apple->poll_runs++; ++ if (retired && apple->first_job_from) { ++ pvr_apple_stat_add(&apple->rpm_first_job, ++ ktime_us_delta(ktime_get(), apple->first_job_from)); ++ apple->first_job_from = 0; ++ } + if (!pending && apple->busy_since) { + apple->busy_ns += ktime_to_ns(ktime_sub(ktime_get(), apple->busy_since)); + apple->busy_since = 0; +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index 211e61d..e0ccb13 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -12,6 +12,7 @@ + #include "pvr_rogue_fwif_client.h" + #include "pvr_stream.h" + ++struct dentry; + struct dma_fence; + struct pvr_apple_chip; + struct pvr_context; +@@ -159,6 +160,7 @@ struct pvr_apple_g5p_cmd_compute { + }; + + #if IS_ENABLED(CONFIG_DRM_POWERVR_APPLE) ++void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root); + int pvr_apple_fw_init(struct pvr_device *pvr_dev); + void pvr_apple_fw_fini(struct pvr_device *pvr_dev); + struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t size); +@@ -193,6 +195,7 @@ void pvr_apple_queue_destroy(struct pvr_queue *queue, bool cleanup_queue_entity) + int pvr_apple_queue_job_init(struct pvr_job *job, u64 drm_client_id); + void pvr_apple_queue_job_cleanup(struct pvr_job *job); + #else ++static inline void pvr_apple_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root) {} + static inline int pvr_apple_fw_init(struct pvr_device *pvr_dev) { return -ENODEV; } + static inline void pvr_apple_fw_fini(struct pvr_device *pvr_dev) {} + static inline struct pvr_gem_object *pvr_apple_pm_pages(struct pvr_device *pvr_dev, size_t size) +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 3b954d3..992d0e8 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -77,6 +77,15 @@ struct pvr_apple_layout { + u32 event_size; + }; + ++struct seq_file; ++ ++/* A duration in microseconds, for debugfs: the last, the longest, the sum */ ++struct pvr_apple_stat { ++ u64 last; ++ u64 max; ++ u64 total; ++}; ++ + struct pvr_apple { + struct pvr_device *pvr_dev; + const struct pvr_apple_chip *chip; +@@ -153,6 +162,21 @@ struct pvr_apple { + bool devfreq_suspended; + ktime_t busy_since; + u64 busy_ns; ++ u64 retired_total; /* retired or cancelled jobs, fence_lock */ ++ ++ /* ++ * For debugfs (apple_jobs): runtime PM cycles and how often the ++ * backend looked for finished jobs; under fence_lock unless atomic. ++ * A resume runs from power-on to the firmware taking commands; the ++ * first job after it retires @first_job_from later. ++ */ ++ struct pvr_apple_stat rpm_gate_wait, rpm_boot, rpm_resume, rpm_first_job, rpm_suspend; ++ u64 rpm_resumes, rpm_resume_failed, rpm_suspends; ++ ktime_t power_on_at; /* the last runtime power-on not yet booted from */ ++ ktime_t first_job_from; /* 0: no start waits for its first job */ ++ u64 poll_runs; ++ atomic64_t fw_irqs; ++ atomic64_t wait_polls; + ktime_t stat_since; + + /* Backs GPU VA 0 in userspace VMs: the end-of-render writes there */ +@@ -238,6 +262,7 @@ struct pvr_apple { + * @engine_done: The sequence number of the last finished job of @engine. No + * lock held; may be called with interrupts off. + * @max_power_state: Highest state licensed for fault capture; optional. ++ * @fault: Read the chip firmware fault flag without waking the GPU. + * @events: Read what the firmware posted. Before @engine_done wherever a job + * is waited for; may sleep. + * @vm_init: A userspace VM was created: map what the firmware needs in it. +@@ -290,6 +315,7 @@ struct pvr_apple_chip { + + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); + u32 (*engine_done)(struct pvr_apple *apple, enum pvr_apple_engine engine); ++ bool (*fault)(struct pvr_apple *apple); + u32 (*max_power_state)(struct pvr_apple *apple); + void (*events)(struct pvr_apple *apple); + int (*vm_init)(struct pvr_apple *apple, struct pvr_vm_context *vm_ctx); +@@ -298,6 +324,8 @@ struct pvr_apple_chip { + void (*mmu_flush)(struct pvr_apple *apple); + /* Acknowledge the firmware's interrupt at its source; hard IRQ context */ + void (*irq_ack)(struct pvr_apple *apple); ++ /* The chip's own lines of debugfs apple_jobs */ ++ void (*debugfs_show)(struct pvr_apple *apple, struct seq_file *m); + }; + + /* A new host tag must be nonzero and differ from the firmware's cached tag. */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 566b706..87b82ca 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -63,6 +63,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -302,6 +303,14 @@ 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 */ ++ ++ /* For debugfs: the reads s8003_touch() made, and the waits for the firmware */ ++ atomic64_t touches, touch_ns, touch_lines; ++ atomic64_t done_polls, ring_polls; ++ /* Under submit_lock: PM record or none per geometry job, pool feedback */ ++ u64 pm_full, pm_clean, pm_clean_busy; ++ u64 fb_reads, fb_grows; ++ u32 fb_max_blocks; + struct pvr_vm_context *park_vm; /* maps what every VM maps, nothing else */ + + /* Bus addresses the firmware is told: its registers' and ours */ +@@ -1229,7 +1238,7 @@ static unsigned long s8003_live_sets(struct pvr_apple_s8003 *s) + * reading only those cut the time of the reads by a quarter to a third on + * the G5P driver, with no slower phase. + */ +-static void s8003_touch(struct pvr_apple_s8003 *s) ++static u32 s8003_touch(struct pvr_apple_s8003 *s) + { + unsigned long set, live = s8003_live_sets(s); + u32 at; +@@ -1241,6 +1250,7 @@ static void s8003_touch(struct pvr_apple_s8003 *s) + for_each_set_bit(set, &live, S8003_PM_SETS) + for (at = 0; at < S8003_PM_SET_SIZE; at += 64) + (void)READ_ONCE(*(const u32 *)(s->pmsets.map + set * S8003_PM_SET_SIZE + at)); ++ return (S8003_ROOT_SIZE + S8003_SHARED_SIZE + hweight_long(live) * S8003_PM_SET_SIZE) / 64; + } + + static unsigned long s8003_vm_sets(const struct s8003_vm *vm) +@@ -1272,7 +1282,9 @@ static void pvr_apple_s8003_events(struct pvr_apple *apple) + /* An eighth early, so that a check every S8003_TOUCH_US always touches */ + if (ktime_us_delta(now, READ_ONCE(s->touched)) + S8003_TOUCH_US / 8 >= S8003_TOUCH_US) { + WRITE_ONCE(s->touched, now); +- s8003_touch(s); ++ atomic64_add(s8003_touch(s), &s->touch_lines); ++ atomic64_add(ktime_to_ns(ktime_sub(ktime_get(), now)), &s->touch_ns); ++ atomic64_inc(&s->touches); + } + if (!READ_ONCE(s->failed) && s8003_drain(apple, &alive)) { + WRITE_ONCE(s->failed, true); +@@ -1324,6 +1336,9 @@ static u32 pvr_apple_s8003_engine_done(struct pvr_apple *apple, enum pvr_apple_e + + static u32 s8003_done_poll(struct pvr_apple *apple, enum pvr_apple_engine engine) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ atomic64_inc(&s->done_polls); + pvr_apple_s8003_events(apple); + return pvr_apple_s8003_engine_done(apple, engine); + } +@@ -1365,6 +1380,9 @@ static int s8003_doorbell(struct pvr_apple *apple, u32 dm) + + static u32 s8003_ring_consumer(struct pvr_apple *apple, u8 *ctl) + { ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ atomic64_inc(&s->ring_polls); + pvr_apple_s8003_events(apple); + return ring_read(ctl, S8003_RING_CONSUMER); + } +@@ -2296,6 +2314,7 @@ static void s8003_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) + put32(list + S8003_PM_FEEDBACK_TAKEN, 1); + wmb(); + vm->fb_at = now; ++ s->fb_reads++; + /* Not from this pool: ignored */ + if (high > pm->blocks * 32 || margin > S8003_PM_BLOCKS_MAX * 32) + continue; +@@ -2306,6 +2325,8 @@ static void s8003_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) + if (target <= max(vm->fb_blocks, pm->blocks)) + continue; + vm->fb_blocks = target; ++ s->fb_grows++; ++ s->fb_max_blocks = max(s->fb_max_blocks, target); + dev_info(apple_dev(apple), + "PM pool VM=%p grows to %u blocks from feedback: high-water %u pages, margin %u, request %u\n", + vm->vm_ctx, target, high, margin, request); +@@ -2384,8 +2405,14 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + * waits on its own stamp for it. + */ + if (!pm_record && +- (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_GEOM) - apple->ta_seq) < 0) ++ (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_GEOM) - apple->ta_seq) < 0) { + pm_record = true; ++ s->pm_clean_busy++; ++ } ++ if (pm_record) ++ s->pm_full++; ++ else ++ s->pm_clean++; + s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); + s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_GEOM), + s8003_object_addr(&s->layout, S8003_CHANNEL), +@@ -2848,6 +2875,27 @@ static u32 pvr_apple_s8003_max_power_state(struct pvr_apple *apple) + return get32(s8003_object(s, S8003_PLATFORM) + 0x710); + } + ++static bool pvr_apple_s8003_fault(struct pvr_apple *apple) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ return READ_ONCE(s->fault); ++} ++ ++static void pvr_apple_s8003_debugfs_show(struct pvr_apple *apple, struct seq_file *m) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ ++ seq_printf(m, "touch count %lld ns %lld lines %lld\n", atomic64_read(&s->touches), ++ atomic64_read(&s->touch_ns), atomic64_read(&s->touch_lines)); ++ seq_printf(m, "pm_record full %llu clean %llu clean_busy %llu\n", READ_ONCE(s->pm_full), ++ READ_ONCE(s->pm_clean), READ_ONCE(s->pm_clean_busy)); ++ seq_printf(m, "pm_pool feedback %llu grows %llu max_blocks %u\n", READ_ONCE(s->fb_reads), ++ READ_ONCE(s->fb_grows), READ_ONCE(s->fb_max_blocks)); ++ seq_printf(m, "s8003_wait_polls done %lld ring %lld\n", atomic64_read(&s->done_polls), ++ atomic64_read(&s->ring_polls)); ++} ++ + const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .fw_name = "apple/gfx-s8003.bin", + .perf_state_max = S8003_PERF_STATE_MAX, +@@ -2873,6 +2921,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .report_hang = pvr_apple_s8003_report_hang, + .submit = pvr_apple_s8003_submit, + .engine_done = pvr_apple_s8003_engine_done, ++ .fault = pvr_apple_s8003_fault, + .max_power_state = pvr_apple_s8003_max_power_state, + .events = pvr_apple_s8003_events, + .vm_init = pvr_apple_s8003_vm_init, +@@ -2880,4 +2929,5 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .vm_scenes = pvr_apple_s8003_vm_scenes, + .mmu_flush = pvr_apple_s8003_mmu_flush, + .irq_ack = pvr_apple_s8003_irq_ack, ++ .debugfs_show = pvr_apple_s8003_debugfs_show, + }; +diff --git a/drivers/gpu/drm/imagination/pvr_debugfs.c b/drivers/gpu/drm/imagination/pvr_debugfs.c +index ebdb05d..21c5f0d 100644 +--- a/drivers/gpu/drm/imagination/pvr_debugfs.c ++++ b/drivers/gpu/drm/imagination/pvr_debugfs.c +@@ -3,6 +3,7 @@ + + #include "pvr_debugfs.h" + ++#include "pvr_apple.h" + #include "pvr_device.h" + #include "pvr_fw_trace.h" + +@@ -27,6 +28,8 @@ pvr_debugfs_init(struct drm_minor *minor) + struct pvr_device *pvr_dev = to_pvr_device(drm_dev); + struct dentry *root = minor->debugfs_root; + ++ pvr_apple_debugfs_init(pvr_dev, root); ++ + for (size_t i = 0; i < ARRAY_SIZE(pvr_debugfs_entries); ++i) { + const struct pvr_debugfs_entry *entry = &pvr_debugfs_entries[i]; + struct dentry *dir;