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/49] 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/49] 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/49] 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/49] 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/49] 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/49] 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/49] 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/49] 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/49] 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/49] 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 34a5e439b2d341c7358a25cf81793cc604bff05d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 11/49] drm/imagination: Keep a job's fence on its VM A VM_BIND client supplies no per-job buffer list. Its VM reservation must therefore retain every queued job before idle mappings can be reclaimed. Prepare the VM reservation in the existing drm_exec pass, reserving space for the whole submission even when several jobs share it. Add each armed finished fence with BOOKKEEP usage before queueing. Track the latest finished-fence retirement timestamp under the VM lock. Reclaim skips a signalled fence until its timestamp is published, and requires a full quiet period after the latest retirement. No job lifetime is extended into a fence-signalling callback. Affects: every PowerVR device Assisted-by: LLM --- ...ination-Keep-a-job-s-fence-on-its-VM.patch | 116 ++++++++++++++++++ 1 file changed, 116 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch diff --git a/kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch b/kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch new file mode 100644 index 00000000..17b43d36 --- /dev/null +++ b/kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch @@ -0,0 +1,116 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:35:12 +0100 +Subject: [PATCH 33/38] drm/imagination: Keep a job's fence on its VM + +A VM_BIND client supplies no per-job buffer list. Its VM reservation must +therefore retain every queued job before idle mappings can be reclaimed. +Prepare the VM reservation in the existing drm_exec pass, reserving space +for the whole submission even when several jobs share it. Add each armed +finished fence with BOOKKEEP usage before queueing. + +Track the latest finished-fence retirement timestamp under the VM lock. +Reclaim skips a signalled fence until its timestamp is published, and +requires a full quiet period after the latest retirement. No job lifetime +is extended into a fence-signalling callback. + +Affects: every PowerVR device + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_job.c | 10 ++++++++++ + drivers/gpu/drm/imagination/pvr_job.h | 1 + + drivers/gpu/drm/imagination/pvr_vm.c | 9 +++++++++ + drivers/gpu/drm/imagination/pvr_vm.h | 1 + + 4 files changed, 21 insertions(+) + +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index 48a1cbc..bd0dc2d 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -16,6 +16,7 @@ + #include "pvr_stream_defs.h" + #include "pvr_sync.h" + #include "pvr_trace.h" ++#include "pvr_vm.h" + + #include + #include +@@ -577,6 +578,10 @@ jobs_lock_all_objs(struct drm_exec *exec, struct pvr_job_data *job_data, + return err; + } + ++ err = drm_exec_prepare_obj(exec, pvr_vm_resv_obj(job->ctx->vm_ctx), job_count); ++ if (err) ++ return err; ++ + if (job->hwrt) { + err = drm_exec_prepare_obj(exec, pvr_hwrt_data_resv_obj(job->hwrt), 1); + if (err) +@@ -605,6 +610,11 @@ prepare_job_resvs_for_each(struct drm_exec *exec, struct pvr_job_data *job_data, + static void + update_job_resvs(struct pvr_job *job) + { ++ struct dma_fence *fence = &job->base.s_fence->finished; ++ ++ dma_resv_add_fence(pvr_vm_get_dma_resv(job->ctx->vm_ctx), fence, ++ DMA_RESV_USAGE_BOOKKEEP); ++ + if (job->hwrt) { + enum dma_resv_usage usage = job->type == DRM_PVR_JOB_TYPE_GEOMETRY ? + DMA_RESV_USAGE_WRITE : DMA_RESV_USAGE_READ; +diff --git a/drivers/gpu/drm/imagination/pvr_job.h b/drivers/gpu/drm/imagination/pvr_job.h +index 0ca003c..44ac047 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.h ++++ b/drivers/gpu/drm/imagination/pvr_job.h +@@ -31,6 +31,7 @@ struct pvr_job { + /** @base: drm_sched_job object. */ + struct drm_sched_job base; + ++ + /** @ref_count: Refcount for job. */ + struct kref ref_count; + +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index bad2742..4988a62 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -55,6 +55,9 @@ struct pvr_vm_context { + /** @lock: Global lock on this VM. */ + struct mutex lock; + ++ /* Latest published job retirement, updated under lock when checking idle. */ ++ ktime_t last_retired; ++ + /** + * @pins: Ranges that map and unmap requests must leave alone, as + * &struct pvr_vm_pin. Protected by @lock. +@@ -608,6 +611,7 @@ pvr_vm_create_context(struct pvr_device *pvr_dev, bool is_userspace_context) + 0, 1ULL << device_addr_bits, 0, 0, &pvr_vm_gpuva_ops); + + mutex_init(&vm_ctx->lock); ++ vm_ctx->last_retired = ktime_get(); + INIT_LIST_HEAD(&vm_ctx->pins); + kref_init(&vm_ctx->ref_count); + +@@ -1515,3 +1519,8 @@ pvr_vm_get_fw_mem_context(struct pvr_vm_context *vm_ctx) + { + return vm_ctx->fw_mem_ctx_obj; + } ++ ++struct drm_gem_object *pvr_vm_resv_obj(struct pvr_vm_context *vm_ctx) ++{ ++ return drm_gpuvm_resv_obj(&vm_ctx->gpuvm_mgr); ++} +diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h +index f7a4f1c..be5c99c 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.h ++++ b/drivers/gpu/drm/imagination/pvr_vm.h +@@ -65,6 +65,7 @@ void pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx); + + dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx); + struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx); ++struct drm_gem_object *pvr_vm_resv_obj(struct pvr_vm_context *vm_ctx); + + int pvr_static_data_areas_get(const struct pvr_device *pvr_dev, + struct drm_pvr_ioctl_dev_query_args *args); 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 12/49] 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 d9819505a40766f12a583bf829aeac2ebf234d58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 13/49] drm/imagination: Take userspace BO pages on first use A large storage buffer consumes unswappable memory at CREATE_BO even if it is never used. Leave userspace shmem unbacked until VM_MAP, vmap or a CPU fault. Shmem supplies zero-filled pages. Keep the internal create entry point eager and explicitly mark all its objects kernel-pinned. Audit of sgt readers: VM map acquires residency; GEM vmap now does too; vmap/vunmap cache sync already tolerates NULL; get_dma_addr now returns -EINVAL when unbacked. Its Apple boot, PM pool and VM reserved-page callers use eager kernel BOs. The free-list page writer obtains an sgt for its eager internal BO. Firmware, HWRT sync objects and both Apple backends retain the internal create entry point. The shmem locked page-in helper is private. Transfer a temporary explicit pin's page reference to our DMA-mapped sgt under the reservation instead. Affects: every PowerVR device Assisted-by: LLM --- ...Take-userspace-BO-pages-on-first-use.patch | 198 ++++++++++++++++++ 1 file changed, 198 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch new file mode 100644 index 00000000..d47784a3 --- /dev/null +++ b/kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch @@ -0,0 +1,198 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:35:12 +0100 +Subject: [PATCH 34/38] drm/imagination: Take userspace BO pages on first use + +A large storage buffer consumes unswappable memory at CREATE_BO even if +it is never used. Leave userspace shmem unbacked until VM_MAP, vmap or a +CPU fault. Shmem supplies zero-filled pages. Keep the internal create +entry point eager and explicitly mark all its objects kernel-pinned. + +Audit of sgt readers: VM map acquires residency; GEM vmap now does too; +vmap/vunmap cache sync already tolerates NULL; get_dma_addr now returns +-EINVAL when unbacked. Its Apple boot, PM pool and VM reserved-page +callers use eager kernel BOs. The free-list page writer obtains an sgt +for its eager internal BO. Firmware, HWRT sync objects and both Apple +backends retain the internal create entry point. + +The shmem locked page-in helper is private. Transfer a temporary explicit +pin's page reference to our DMA-mapped sgt under the reservation instead. + +Affects: every PowerVR device + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_drv.c | 2 +- + drivers/gpu/drm/imagination/pvr_gem.c | 79 +++++++++++++++++++++++++-- + drivers/gpu/drm/imagination/pvr_gem.h | 16 +++--- + 3 files changed, 85 insertions(+), 12 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c +index 206ee1f..697b705 100644 +--- a/drivers/gpu/drm/imagination/pvr_drv.c ++++ b/drivers/gpu/drm/imagination/pvr_drv.c +@@ -115,7 +115,7 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, + * Create a buffer object and transfer ownership to a userspace- + * accessible handle. + */ +- pvr_obj = pvr_gem_object_create(pvr_dev, sanitized_size, args->flags); ++ pvr_obj = pvr_gem_object_create_user(pvr_dev, sanitized_size, args->flags); + if (IS_ERR(pvr_obj)) { + err = PTR_ERR(pvr_obj); + goto err_drm_dev_exit; +diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c +index 817b47d..0fffab7 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.c ++++ b/drivers/gpu/drm/imagination/pvr_gem.c +@@ -227,6 +227,10 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) + + dma_resv_lock(obj->resv, NULL); + ++ err = PTR_ERR_OR_ZERO(pvr_gem_get_pages_sgt_locked(pvr_obj)); ++ if (err) ++ goto err_unlock; ++ + err = drm_gem_shmem_vmap_locked(shmem_obj, &map); + if (err) + goto err_unlock; +@@ -357,8 +361,8 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t + * * -%ENOMEM if sufficient physical memory cannot be allocated, or + * * Any other error returned by drm_gem_create_mmap_offset(). + */ +-struct pvr_gem_object * +-pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) ++static struct pvr_gem_object * ++pvr_gem_object_create_common(struct pvr_device *pvr_dev, size_t size, u64 flags) + { + struct drm_device *drm_dev = from_pvr_device(pvr_dev); + struct drm_gem_shmem_object *shmem_obj; +@@ -381,7 +385,10 @@ 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; + +- sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); ++ if (!(flags & PVR_BO_KERNEL_PINNED)) ++ return pvr_obj; ++ ++ sgt = pvr_gem_object_get_pages_sgt(pvr_obj); + if (IS_ERR(sgt)) { + err = PTR_ERR(sgt); + goto err_shmem_object_free; +@@ -423,7 +430,8 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, + struct scatterlist *sgl; + unsigned int sgt_idx; + +- WARN_ON(!shmem_obj->sgt); ++ if (!shmem_obj->sgt || offset >= pvr_gem_object_size(pvr_obj)) ++ return -EINVAL; + for_each_sgtable_dma_sg(shmem_obj->sgt, sgl, sgt_idx) { + u32 new_offset = accumulated_offset + sg_dma_len(sgl); + +@@ -438,3 +446,66 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, + + return -EINVAL; + } ++ ++struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, ++ size_t size, u64 flags) ++{ ++ return pvr_gem_object_create_common(pvr_dev, size, flags | PVR_BO_KERNEL_PINNED); ++} ++ ++struct pvr_gem_object *pvr_gem_object_create_user(struct pvr_device *pvr_dev, ++ size_t size, u64 flags) ++{ ++ return pvr_gem_object_create_common(pvr_dev, size, flags); ++} ++ ++/* The shmem helper's locked page-in entry points are private to that helper. */ ++struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) ++{ ++ struct drm_gem_shmem_object *shmem = &pvr_obj->base; ++ struct drm_gem_object *obj = &shmem->base; ++ struct sg_table *sgt; ++ int err; ++ ++ dma_resv_assert_held(obj->resv); ++ if (shmem->sgt) ++ return shmem->sgt; ++ if (drm_gem_is_imported(obj)) ++ return ERR_PTR(-EINVAL); ++ ++ /* pin_locked gets pages without recursively acquiring the reservation. */ ++ err = drm_gem_shmem_pin_locked(shmem); ++ if (err) ++ return ERR_PTR(err); ++ sgt = drm_gem_shmem_get_sg_table(shmem); ++ if (IS_ERR(sgt)) { ++ err = PTR_ERR(sgt); ++ goto unpin; ++ } ++ err = dma_map_sgtable(obj->dev->dev, sgt, DMA_BIDIRECTIONAL, 0); ++ if (err) { ++ sg_free_table(sgt); ++ kfree(sgt); ++ goto unpin; ++ } ++ /* Transfer the temporary pin's page reference to the sgt. */ ++ refcount_inc(&shmem->pages_use_count); ++ shmem->sgt = sgt; ++unpin: ++ drm_gem_shmem_unpin_locked(shmem); ++ return err ? ERR_PTR(err) : sgt; ++} ++ ++struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) ++{ ++ struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); ++ struct sg_table *sgt; ++ int err; ++ ++ err = dma_resv_lock_interruptible(obj->resv, NULL); ++ if (err) ++ return ERR_PTR(err); ++ sgt = pvr_gem_get_pages_sgt_locked(pvr_obj); ++ dma_resv_unlock(obj->resv); ++ return sgt; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h +index efcd270..07bd602 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.h ++++ b/drivers/gpu/drm/imagination/pvr_gem.h +@@ -63,10 +63,12 @@ struct pvr_file; + + #define PVR_BO_DEVICE_CACHE_COHERENT BIT_ULL(61) + ++#define PVR_BO_KERNEL_PINNED BIT_ULL(60) ++ + #define PVR_BO_KERNEL_FLAGS_MASK (PVR_BO_CPU_CACHED | PVR_BO_FW_NO_CLEAR_ON_RESET | \ +- PVR_BO_DEVICE_CACHE_COHERENT) ++ PVR_BO_DEVICE_CACHE_COHERENT | PVR_BO_KERNEL_PINNED) + +-/* Bits 60..3 are undefined. */ ++/* Bits 59..3 are undefined. */ + /* Bits 2..0 are defined in the UAPI. */ + + /* Other utilities. */ +@@ -129,16 +131,16 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t + struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, + size_t size, u64 flags); + ++struct pvr_gem_object *pvr_gem_object_create_user(struct pvr_device *pvr_dev, ++ size_t size, u64 flags); ++struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj); ++ + int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, + struct pvr_file *pvr_file, u32 *handle); + struct pvr_gem_object *pvr_gem_object_from_handle(struct pvr_file *pvr_file, + u32 handle); + +-static __always_inline struct sg_table * +-pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) +-{ +- return drm_gem_shmem_get_pages_sgt(shmem_gem_from_pvr_gem(pvr_obj)); +-} ++struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj); + + void *pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj); + void pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj); 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 14/49] 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 828ebd869da6209c6c81f7337265fc88aff452c4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 15/49] drm/imagination: Don't pin BO pages for CPU mappings Mesa maps its buffers on the CPU for their whole lifetime. The shmem mmap helper holds page references for every such VMA, defeating reclaim. Keep only GEM references in native VMAs and obtain DMA-mapped pages in the fault handler under the BO reservation. Use PFN mappings with the existing write-combining policy. A later eviction can zap those PFNs under the same reservation before dropping pages. Mark pages dirty to preserve CPU stores across swap. Imported mappings stay with exporters. Affects: every PowerVR device Assisted-by: LLM --- ...-Don-t-pin-BO-pages-for-CPU-mappings.patch | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch b/kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch new file mode 100644 index 00000000..66bfb5f7 --- /dev/null +++ b/kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch @@ -0,0 +1,87 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:35:12 +0100 +Subject: [PATCH 35/38] drm/imagination: Don't pin BO pages for CPU mappings + +Mesa maps its buffers on the CPU for their whole lifetime. The shmem +mmap helper holds page references for every such VMA, defeating reclaim. +Keep only GEM references in native VMAs and obtain DMA-mapped pages in +the fault handler under the BO reservation. Use PFN mappings with the +existing write-combining policy. A later eviction can zap those PFNs +under the same reservation before dropping pages. Mark pages dirty to +preserve CPU stores across swap. Imported mappings stay with exporters. + +Affects: every PowerVR device + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_gem.c | 42 +++++++++++++++++++++++++-- + 1 file changed, 40 insertions(+), 2 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c +index 0fffab7..0129c8d 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.c ++++ b/drivers/gpu/drm/imagination/pvr_gem.c +@@ -49,7 +49,16 @@ static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *v + if (!(pvr_obj->flags & DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS)) + return -EINVAL; + +- return drm_gem_shmem_mmap(shmem_obj, vma); ++ if (drm_gem_is_imported(gem_obj)) ++ return drm_gem_shmem_mmap(shmem_obj, vma); ++ if (vma_is_cow_mapping(vma)) ++ return -EINVAL; ++ ++ vm_flags_set(vma, VM_PFNMAP | VM_DONTEXPAND | VM_DONTDUMP); ++ vma->vm_page_prot = vma_get_page_prot(vma); ++ if (shmem_obj->map_wc) ++ vma->vm_page_prot = pgprot_writecombine(vma->vm_page_prot); ++ return 0; + } + + static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *obj) +@@ -62,6 +71,35 @@ static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *o + return 0; + } + ++static vm_fault_t pvr_gem_fault(struct vm_fault *vmf) ++{ ++ struct drm_gem_object *obj = vmf->vma->vm_private_data; ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ pgoff_t offset = vmf->pgoff - vmf->vma->vm_pgoff; ++ vm_fault_t ret; ++ struct sg_table *sgt; ++ ++ if (offset >= obj->size >> PAGE_SHIFT) ++ return VM_FAULT_SIGBUS; ++ dma_resv_lock(obj->resv, NULL); ++ sgt = pvr_gem_get_pages_sgt_locked(bo); ++ if (IS_ERR(sgt)) { ++ ret = PTR_ERR(sgt) == -ENOMEM ? VM_FAULT_OOM : VM_FAULT_SIGBUS; ++ } else { ++ bo->base.pages_mark_dirty_on_put = true; ++ ret = vmf_insert_pfn(vmf->vma, vmf->address, ++ page_to_pfn(bo->base.pages[offset])); ++ } ++ dma_resv_unlock(obj->resv); ++ return ret; ++} ++ ++static const struct vm_operations_struct pvr_gem_vm_ops = { ++ .fault = pvr_gem_fault, ++ .open = drm_gem_vm_open, ++ .close = drm_gem_vm_close, ++}; ++ + static const struct drm_gem_object_funcs pvr_gem_object_funcs = { + .free = pvr_gem_object_free, + .status = pvr_gem_object_status, +@@ -73,7 +111,7 @@ static const struct drm_gem_object_funcs pvr_gem_object_funcs = { + .vmap = drm_gem_shmem_object_vmap, + .vunmap = drm_gem_shmem_object_vunmap, + .mmap = pvr_gem_mmap, +- .vm_ops = &drm_gem_shmem_vm_ops, ++ .vm_ops = &pvr_gem_vm_ops, + }; + + /** 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 16/49] 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 7f76d810c6c5602d31af2df45280bfda5c59739a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 17/49] drm/imagination: Evict idle BOs under memory pressure Idle clients keep every mapped BO unswappable on small-memory systems. Use the GEM LRUs and shrinker to release native shmem backing while preserving GPU virtual addresses. Require all mapping VMs to be idle, with published retirement timestamps, for a one-second quiet period. Trylock every VM under the BO reservation before editing any PTE. Mark each cleared VA and vm_bo evicted, publish the MMU invalidation, then zap CPU mappings, DMA-unmap and release the sole sgt page reference. Keep pages on any partial unmap or flush failure for submit-time repair. Never reclaim imports, exports (including export publication), kernel BOs, PM/FW-protected BOs, vmaps, explicit pins or additional page users. The Apple veto trylocks submit_lock and S8003 checks pm_last/pm_prev, attached versus firmware gen, fault/booted, and each pm[].scene/td_seq through s8003_pm_idle(). Track the last two geometry VM targets; no new register access is introduced. T8010 keeps its generation invalidation. Use base.gem_lru_mutex, required by drm_gem_lru, rather than a private LRU lock. Reclaim edits use GFP_NOWAIT cursors. IMG KCCB flushing can acquire power/CCB locks and reset firmware: schedule its scan in ordinary work context rather than invoke it from direct reclaim. Eviction is enabled on every device. Apple scans publish RAM-only tags directly in reclaim. Expose controls and resident/evicted bytes, counters and skip reasons in gem_evict; writing a MiB limit requests one pass. No swap, no eviction. Affects: every PowerVR device Assisted-by: LLM --- ...Evict-idle-BOs-under-memory-pressure.patch | 850 ++++++++++++++++++ 1 file changed, 850 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch new file mode 100644 index 00000000..c06404de --- /dev/null +++ b/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch @@ -0,0 +1,850 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:39:24 +0100 +Subject: [PATCH 36/38] drm/imagination: Evict idle BOs under memory pressure + +Idle clients keep every mapped BO unswappable on small-memory systems. +Use the GEM LRUs and shrinker to release native shmem backing while +preserving GPU virtual addresses. Require all mapping VMs to be idle, +with published retirement timestamps, for a one-second quiet period. + +Trylock every VM under the BO reservation before editing any PTE. Mark +each cleared VA and vm_bo evicted, publish the MMU invalidation, then zap +CPU mappings, DMA-unmap and release the sole sgt page reference. Keep +pages on any partial unmap or flush failure for submit-time repair. + +Never reclaim imports, exports (including export publication), kernel +BOs, PM/FW-protected BOs, vmaps, explicit pins or additional page users. +The Apple veto trylocks submit_lock and S8003 checks pm_last/pm_prev, +attached versus firmware gen, fault/booted, and each pm[].scene/td_seq +through s8003_pm_idle(). Track the last two geometry VM targets; no new +register access is introduced. T8010 keeps its generation invalidation. + +Use base.gem_lru_mutex, required by drm_gem_lru, rather than a private LRU +lock. Reclaim edits use GFP_NOWAIT cursors. IMG KCCB flushing can acquire +power/CCB locks and reset firmware: schedule its scan in ordinary work +context rather than invoke it from direct reclaim. Eviction is enabled +on every device. Apple scans publish RAM-only tags directly in reclaim. + +Expose controls and resident/evicted bytes, counters and skip reasons in +gem_evict; writing a MiB limit requests one pass. No swap, no eviction. + +Affects: every PowerVR device + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/Makefile | 1 + + drivers/gpu/drm/imagination/pvr_apple.c | 13 + + drivers/gpu/drm/imagination/pvr_apple.h | 7 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 30 +++ + drivers/gpu/drm/imagination/pvr_debugfs.c | 3 + + drivers/gpu/drm/imagination/pvr_device.c | 7 + + drivers/gpu/drm/imagination/pvr_device.h | 17 ++ + drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++- + drivers/gpu/drm/imagination/pvr_gem.h | 11 + + .../gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ + drivers/gpu/drm/imagination/pvr_mmu.h | 2 + + drivers/gpu/drm/imagination/pvr_vm.c | 100 ++++++++ + drivers/gpu/drm/imagination/pvr_vm.h | 2 + + 15 files changed, 510 insertions(+), 6 deletions(-) + create mode 100644 drivers/gpu/drm/imagination/pvr_gem_shrinker.c + +diff --git a/drivers/gpu/drm/imagination/Makefile b/drivers/gpu/drm/imagination/Makefile +index 08181ce..2e250a4 100644 +--- a/drivers/gpu/drm/imagination/Makefile ++++ b/drivers/gpu/drm/imagination/Makefile +@@ -18,6 +18,7 @@ powervr-y := \ + pvr_fw_trace.o \ + pvr_fw_util.o \ + pvr_gem.o \ ++ pvr_gem_shrinker.o \ + pvr_hwrt.o \ + pvr_job.o \ + pvr_mmu.o \ +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 5a43e3f..d062461 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1666,3 +1666,16 @@ u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type t + *ext_type = 0; + return 0; + } ++ ++/* Reclaim cannot wait behind a chip submission or firmware restart. */ ++bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple *apple = to_apple(pvr_dev); ++ bool allowed; ++ ++ if (!mutex_trylock(&apple->submit_lock)) ++ return false; ++ allowed = !apple->chip->vm_evictable || apple->chip->vm_evictable(apple, vm_ctx); ++ mutex_unlock(&apple->submit_lock); ++ return allowed; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index d1de0ad..fe3c7a5 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -179,6 +179,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); + void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev); + int pvr_apple_job_add_deps(struct pvr_job *job); + void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); ++bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); + 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); +@@ -209,6 +210,12 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, + *ext_type = 0; + return 0; + } ++static inline bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, ++ struct pvr_vm_context *vm_ctx) ++{ ++ return true; ++} ++ + 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 d524415..1cec40e 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -271,6 +271,7 @@ struct pvr_apple_chip { + 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); ++ bool (*vm_evictable)(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); + }; +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index bc6f300..2389178 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -261,6 +261,7 @@ struct s8003_vm { + + struct pvr_apple_s8003 { + struct pvr_apple *apple; ++ struct s8003_vm *pm_last, *pm_prev; + struct delayed_work drain_work; /* reads the rings while the firmware runs */ + struct mutex event_lock; /* the rings the firmware posts to */ + ktime_t touched; /* s8003_touch() */ +@@ -1521,6 +1522,10 @@ 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; + for (e = 0; e < 2; e++) { + if (vm->slot[e]) + __clear_bit(vm->slot[e], s->slots); +@@ -2101,6 +2106,26 @@ static bool s8003_pm_idle(struct pvr_apple *apple, const struct s8003_pm *pm) + return false; + } + ++static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, ++ struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct s8003_vm *vm = xa_load(&s->vms, (unsigned long)vm_ctx); ++ unsigned int i; ++ ++ lockdep_assert_held(&apple->submit_lock); ++ if (!vm || READ_ONCE(s->fault)) ++ return false; ++ if (vm->attached != s->gen) ++ return true; ++ if (!apple->booted || s->pm_last == vm || s->pm_prev == vm) ++ return false; ++ for (i = 0; i < ARRAY_SIZE(vm->pm); i++) ++ if (!s8003_pm_idle(apple, &vm->pm[i])) ++ return false; ++ return true; ++} ++ + static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, + const char *engine, int err, const char *reason) + { +@@ -2158,6 +2183,10 @@ 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; ++ } + pm->scene = s->gen; + pm->ta_seq = seq; + pm->td_seq = 0; +@@ -2589,5 +2618,6 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .vm_init = pvr_apple_s8003_vm_init, + .vm_fini = pvr_apple_s8003_vm_fini, + .vm_scenes = pvr_apple_s8003_vm_scenes, ++ .vm_evictable = pvr_apple_s8003_vm_evictable, + .mmu_flush = pvr_apple_s8003_mmu_flush, + }; +diff --git a/drivers/gpu/drm/imagination/pvr_debugfs.c b/drivers/gpu/drm/imagination/pvr_debugfs.c +index ebdb05d..cc96631 100644 +--- a/drivers/gpu/drm/imagination/pvr_debugfs.c ++++ b/drivers/gpu/drm/imagination/pvr_debugfs.c +@@ -4,6 +4,7 @@ + #include "pvr_debugfs.h" + + #include "pvr_device.h" ++#include "pvr_gem.h" + #include "pvr_fw_trace.h" + + #include +@@ -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_gem_shrinker_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; +diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c +index 2c06a4a..8c1bb33 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.c ++++ b/drivers/gpu/drm/imagination/pvr_device.c +@@ -3,6 +3,7 @@ + + #include "pvr_apple.h" + #include "pvr_device.h" ++#include "pvr_gem.h" + #include "pvr_device_info.h" + + #include "pvr_fw.h" +@@ -807,6 +808,10 @@ pvr_device_init(struct pvr_device *pvr_dev) + /* Get the platform-specific data based on the compatible string. */ + pvr_dev->device_data = of_device_get_match_data(dev); + ++ err = pvr_gem_shrinker_init(pvr_dev); ++ if (err) ++ return err; ++ + /* Enable and initialize clocks required for the device to operate. */ + err = pvr_device_clk_init(pvr_dev); + if (err) +@@ -858,6 +863,8 @@ err_pm_runtime_put: + void + pvr_device_fini(struct pvr_device *pvr_dev) + { ++ pvr_gem_shrinker_fini(pvr_dev); ++ + /* + * Deinitialization stages are performed in reverse order compared to + * the initialization stages in pvr_device_init(). +diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h +index 26b589e..0157190 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.h ++++ b/drivers/gpu/drm/imagination/pvr_device.h +@@ -11,6 +11,8 @@ + #include "pvr_stream.h" + + #include ++#include ++#include + #include + #include + +@@ -92,6 +94,21 @@ struct pvr_device { + */ + struct drm_device base; + ++ /** @gem_reclaim: GEM residency, using base.gem_lru_mutex for all LRUs. */ ++ struct { ++ struct drm_gem_lru evictable; ++ struct drm_gem_lru dontneed; ++ struct drm_gem_lru pinned; ++ struct mutex scan_lock; ++ struct shrinker *shrinker; ++ bool stopped; ++ struct work_struct work; ++ atomic_long_t requested; ++ atomic64_t evicted_bytes; ++ atomic64_t scans, evicts, purges, restores, restore_fail; ++ atomic64_t skip[12]; ++ } gem_reclaim; ++ + /** @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 0129c8d..cf061a5 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.c ++++ b/drivers/gpu/drm/imagination/pvr_gem.c +@@ -18,6 +18,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -27,6 +28,11 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) + { + struct drm_gem_shmem_object *shmem_obj = to_drm_gem_shmem_obj(obj); + ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ ++ drm_gem_lru_remove(obj); ++ if (bo->pages_evicted) ++ atomic64_sub(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); + shmem_obj->pages_mark_dirty_on_put = true; + drm_gem_shmem_free(shmem_obj); + } +@@ -38,6 +44,11 @@ static struct dma_buf *pvr_gem_export(struct drm_gem_object *obj, int flags) + if (pvr_obj->flags & DRM_PVR_BO_PM_FW_PROTECT) + return ERR_PTR(-EPERM); + ++ /* Serialize the transition, including the gap before core sets dma_buf. */ ++ dma_resv_lock(obj->resv, NULL); ++ pvr_obj->exported = true; ++ pvr_gem_lru_update_locked(pvr_obj); ++ dma_resv_unlock(obj->resv); + return drm_gem_prime_export(obj, flags); + } + +@@ -100,16 +111,54 @@ static const struct vm_operations_struct pvr_gem_vm_ops = { + .close = drm_gem_vm_close, + }; + ++static int pvr_gem_pin(struct drm_gem_object *obj) ++{ ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ int err = PTR_ERR_OR_ZERO(pvr_gem_get_pages_sgt_locked(bo)); ++ ++ if (!err) ++ err = drm_gem_shmem_pin_locked(&bo->base); ++ pvr_gem_lru_update_locked(bo); ++ return err; ++} ++ ++static void pvr_gem_unpin(struct drm_gem_object *obj) ++{ ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ ++ drm_gem_shmem_unpin_locked(&bo->base); ++ pvr_gem_lru_update_locked(bo); ++} ++ ++static int pvr_gem_vmap(struct drm_gem_object *obj, struct iosys_map *map) ++{ ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ int err = PTR_ERR_OR_ZERO(pvr_gem_get_pages_sgt_locked(bo)); ++ ++ if (!err) ++ err = drm_gem_shmem_vmap_locked(&bo->base, map); ++ pvr_gem_lru_update_locked(bo); ++ return err; ++} ++ ++static void pvr_gem_vunmap(struct drm_gem_object *obj, struct iosys_map *map) ++{ ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ ++ drm_gem_shmem_vunmap_locked(&bo->base, map); ++ pvr_gem_lru_update_locked(bo); ++} ++ + static const struct drm_gem_object_funcs pvr_gem_object_funcs = { + .free = pvr_gem_object_free, + .status = pvr_gem_object_status, + .print_info = drm_gem_shmem_object_print_info, + .export = pvr_gem_export, +- .pin = drm_gem_shmem_object_pin, +- .unpin = drm_gem_shmem_object_unpin, ++ .pin = pvr_gem_pin, ++ .unpin = pvr_gem_unpin, + .get_sg_table = drm_gem_shmem_object_get_sg_table, +- .vmap = drm_gem_shmem_object_vmap, +- .vunmap = drm_gem_shmem_object_vunmap, ++ .vmap = pvr_gem_vmap, ++ .vunmap = pvr_gem_vunmap, + .mmap = pvr_gem_mmap, + .vm_ops = &pvr_gem_vm_ops, + }; +@@ -269,7 +318,7 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) + if (err) + goto err_unlock; + +- err = drm_gem_shmem_vmap_locked(shmem_obj, &map); ++ err = pvr_gem_vmap(obj, &map); + if (err) + goto err_unlock; + +@@ -323,7 +372,7 @@ pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) + dma_sync_sgtable_for_device(dev, shmem_obj->sgt, DMA_BIDIRECTIONAL); + } + +- drm_gem_shmem_vunmap_locked(shmem_obj, &map); ++ pvr_gem_vunmap(obj, &map); + + dma_resv_unlock(obj->resv); + } +@@ -529,8 +578,14 @@ struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) + /* Transfer the temporary pin's page reference to the sgt. */ + refcount_inc(&shmem->pages_use_count); + shmem->sgt = sgt; ++ if (pvr_obj->pages_evicted) { ++ pvr_obj->pages_evicted = false; ++ atomic64_sub(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); ++ } + unpin: + drm_gem_shmem_unpin_locked(shmem); ++ if (!err) ++ pvr_gem_lru_update_locked(pvr_obj); + return err ? ERR_PTR(err) : sgt; + } + +@@ -547,3 +602,17 @@ struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) + dma_resv_unlock(obj->resv); + return sgt; + } ++ ++void pvr_gem_release_pages_locked(struct pvr_gem_object *bo) ++{ ++ struct drm_gem_object *obj = gem_from_pvr_gem(bo); ++ ++ dma_resv_assert_held(obj->resv); ++ drm_vma_node_unmap(&obj->vma_node, obj->dev->anon_inode->i_mapping); ++ bo->base.pages_mark_dirty_on_put = true; ++ __drm_gem_shmem_free_sgt_locked(&bo->base); ++ drm_gem_shmem_put_pages_locked(&bo->base); ++ bo->pages_evicted = true; ++ atomic64_add(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); ++ drm_gem_lru_remove(obj); ++} +diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h +index 07bd602..c21b8e7 100644 +--- a/drivers/gpu/drm/imagination/pvr_gem.h ++++ b/drivers/gpu/drm/imagination/pvr_gem.h +@@ -111,6 +111,11 @@ struct pvr_gem_object { + */ + u64 flags; + ++ /** @exported: Latched before publishing a dma-buf, under the reservation. */ ++ bool exported; ++ /** @pages_evicted: Backing pages released; protected by the reservation. */ ++ bool pages_evicted; ++ + }; + + static_assert(offsetof(struct pvr_gem_object, base) == 0, +@@ -133,6 +138,12 @@ struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, + + struct pvr_gem_object *pvr_gem_object_create_user(struct pvr_device *pvr_dev, + size_t size, u64 flags); ++void pvr_gem_lru_update_locked(struct pvr_gem_object *bo); ++void pvr_gem_shrinker_debugfs_init(struct pvr_device *pvr_dev, struct dentry *root); ++int pvr_gem_shrinker_init(struct pvr_device *pvr_dev); ++void pvr_gem_shrinker_fini(struct pvr_device *pvr_dev); ++void pvr_gem_release_pages_locked(struct pvr_gem_object *bo); ++ + struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj); + + int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, +diff --git a/drivers/gpu/drm/imagination/pvr_gem_shrinker.c b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c +new file mode 100644 +index 0000000..c43b558 +--- /dev/null ++++ b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c +@@ -0,0 +1,223 @@ ++// SPDX-License-Identifier: GPL-2.0-only OR MIT ++/* Idle VM residency for faultless PowerVR devices. */ ++#include "pvr_device.h" ++#include "pvr_gem.h" ++#include "pvr_vm.h" ++ ++#include ++#include ++#include ++#include ++#include ++#include ++#include ++ ++static bool evict = true; ++module_param(evict, bool, 0644); ++MODULE_PARM_DESC(evict, "Evict idle native BOs (default on)"); ++static unsigned int evict_idle_ms = 1000; ++module_param(evict_idle_ms, uint, 0644); ++MODULE_PARM_DESC(evict_idle_ms, "Quiet time after VM retirement in milliseconds"); ++static bool evict_log; ++module_param(evict_log, bool, 0644); ++MODULE_PARM_DESC(evict_log, "Log BO eviction"); ++ ++void pvr_gem_lru_update_locked(struct pvr_gem_object *bo) ++{ ++ struct drm_gem_object *obj = gem_from_pvr_gem(bo); ++ struct pvr_device *dev = to_pvr_device(obj->dev); ++ struct drm_gem_lru *lru = &dev->gem_reclaim.evictable; ++ ++ dma_resv_assert_held(obj->resv); ++ if (!bo->base.sgt || drm_gem_is_imported(obj)) ++ return; ++ if ((bo->flags & (PVR_BO_KERNEL_PINNED | DRM_PVR_BO_PM_FW_PROTECT)) || ++ bo->exported || obj->dma_buf || refcount_read(&bo->base.vmap_use_count) || ++ refcount_read(&bo->base.pages_pin_count)) ++ lru = &dev->gem_reclaim.pinned; ++ drm_gem_lru_move_tail(lru, obj); ++} ++ ++static bool pvr_gem_evict(struct drm_gem_object *obj, struct ww_acquire_ctx *unused) ++{ ++ struct pvr_device *dev = to_pvr_device(obj->dev); ++ struct pvr_gem_object *bo = gem_to_pvr_gem(obj); ++ struct drm_gem_shmem_object *shmem = &bo->base; ++ unsigned int reason; ++ ++ if (drm_gem_is_imported(obj)) ++ reason = 0; ++ else if (bo->exported || obj->dma_buf) ++ reason = 1; ++ else if (bo->flags & (PVR_BO_KERNEL_PINNED | DRM_PVR_BO_PM_FW_PROTECT)) ++ reason = 2; ++ else if (refcount_read(&shmem->vmap_use_count) || refcount_read(&shmem->pages_pin_count)) ++ reason = 3; ++ else if (!shmem->sgt || refcount_read(&shmem->pages_use_count) != 1) ++ reason = 4; ++ else if (!dma_resv_test_signaled(obj->resv, DMA_RESV_USAGE_BOOKKEEP)) ++ reason = 5; ++ else if (pvr_vm_evict_bo(obj, READ_ONCE(evict_idle_ms), &reason)) { ++ pvr_gem_release_pages_locked(bo); ++ atomic64_inc(&dev->gem_reclaim.evicts); ++ if (READ_ONCE(evict_log)) ++ drm_info(&dev->base, "evicted BO size=%zu\n", obj->size); ++ return true; ++ } ++ atomic64_inc(&dev->gem_reclaim.skip[reason]); ++ return false; ++} ++ ++static unsigned long pvr_gem_reclaim(struct pvr_device *dev, unsigned long pages) ++{ ++ unsigned long remaining = 0, freed = 0; ++ ++ if (!READ_ONCE(evict) || !mutex_trylock(&dev->gem_reclaim.scan_lock)) ++ return SHRINK_STOP; ++ if (dev->gem_reclaim.stopped) { ++ mutex_unlock(&dev->gem_reclaim.scan_lock); ++ return SHRINK_STOP; ++ } ++ atomic64_inc(&dev->gem_reclaim.scans); ++ if (get_nr_swap_pages() > 0) ++ freed = drm_gem_lru_scan(&dev->base, &dev->gem_reclaim.evictable, ++ min(pages, (unsigned long)UINT_MAX), &remaining, ++ pvr_gem_evict, NULL); ++ mutex_unlock(&dev->gem_reclaim.scan_lock); ++ return freed ? freed : SHRINK_STOP; ++} ++ ++/* IMG's KCCB invalidation takes power/CCB locks and can reset firmware. ++ * Run that path outside direct reclaim; Apple publishes a RAM-only tag. ++ */ ++static void pvr_gem_reclaim_work(struct work_struct *work) ++{ ++ struct pvr_device *dev = container_of(work, struct pvr_device, gem_reclaim.work); ++ unsigned long pages = atomic_long_xchg(&dev->gem_reclaim.requested, 0); ++ ++ if (pages) ++ pvr_gem_reclaim(dev, pages); ++} ++ ++static unsigned long pvr_gem_count(struct shrinker *shrinker, struct shrink_control *sc) ++{ ++ struct pvr_device *dev = shrinker->private_data; ++ ++ if (!READ_ONCE(evict) || get_nr_swap_pages() <= 0) ++ return 0; ++ return READ_ONCE(dev->gem_reclaim.evictable.count) + ++ READ_ONCE(dev->gem_reclaim.dontneed.count); ++} ++ ++static unsigned long pvr_gem_scan(struct shrinker *shrinker, struct shrink_control *sc) ++{ ++ struct pvr_device *dev = shrinker->private_data; ++ ++ if (!current_is_kswapd() && ++ (sc->gfp_mask & (__GFP_FS | __GFP_IO)) != (__GFP_FS | __GFP_IO)) ++ return SHRINK_STOP; ++ if (!pvr_device_is_apple(dev)) { ++ atomic_long_set(&dev->gem_reclaim.requested, sc->nr_to_scan); ++ schedule_work(&dev->gem_reclaim.work); ++ return SHRINK_STOP; ++ } ++ return pvr_gem_reclaim(dev, sc->nr_to_scan); ++} ++ ++static int pvr_gem_evict_show(struct seq_file *m, void *unused) ++{ ++ struct pvr_device *dev = m->private; ++ static const char *const reasons[] = { ++ "imported", "exported", "protected", "pinned", "pages", "bo-busy", ++ "vm-trylock", "vm-busy", "young", "chip-veto", "op-alloc", "mmu-flush", ++ }; ++ unsigned long resident, dontneed; ++ unsigned int i; ++ ++ mutex_lock(&dev->base.gem_lru_mutex); ++ dontneed = dev->gem_reclaim.dontneed.count << PAGE_SHIFT; ++ resident = (dev->gem_reclaim.evictable.count + dev->gem_reclaim.pinned.count + ++ dev->gem_reclaim.dontneed.count) << PAGE_SHIFT; ++ mutex_unlock(&dev->base.gem_lru_mutex); ++ seq_printf(m, "resident=%lu evicted=%lld dontneed=%lu\n", resident, ++ atomic64_read(&dev->gem_reclaim.evicted_bytes), dontneed); ++ seq_printf(m, "scan=%lld evict=%lld purge=%lld restore=%lld restore-fail=%lld\n", ++ atomic64_read(&dev->gem_reclaim.scans), atomic64_read(&dev->gem_reclaim.evicts), ++ atomic64_read(&dev->gem_reclaim.purges), atomic64_read(&dev->gem_reclaim.restores), ++ atomic64_read(&dev->gem_reclaim.restore_fail)); ++ for (i = 0; i < ARRAY_SIZE(reasons); i++) ++ seq_printf(m, "skip-%s=%lld\n", reasons[i], atomic64_read(&dev->gem_reclaim.skip[i])); ++ return 0; ++} ++ ++static int pvr_gem_evict_open(struct inode *inode, struct file *file) ++{ ++ return single_open(file, pvr_gem_evict_show, inode->i_private); ++} ++ ++static ssize_t pvr_gem_evict_write(struct file *file, const char __user *buf, ++ size_t len, loff_t *pos) ++{ ++ struct seq_file *m = file->private_data; ++ struct pvr_device *dev = m->private; ++ unsigned long mib; ++ int err = kstrtoul_from_user(buf, len, 0, &mib); ++ ++ if (err) ++ return err; ++ if (mib > (UINT_MAX >> (20 - PAGE_SHIFT))) ++ return -ERANGE; ++ pvr_gem_reclaim(dev, mib << (20 - PAGE_SHIFT)); ++ return len; ++} ++ ++static const struct file_operations pvr_gem_evict_fops = { ++ .owner = THIS_MODULE, ++ .open = pvr_gem_evict_open, ++ .read = seq_read, ++ .write = pvr_gem_evict_write, ++ .llseek = seq_lseek, ++ .release = single_release, ++}; ++ ++void pvr_gem_shrinker_debugfs_init(struct pvr_device *dev, struct dentry *root) ++{ ++ debugfs_create_file("gem_evict", 0600, root, dev, &pvr_gem_evict_fops); ++} ++ ++void pvr_gem_shrinker_fini(struct pvr_device *dev) ++{ ++ if (!dev->gem_reclaim.shrinker) ++ return; ++ mutex_lock(&dev->gem_reclaim.scan_lock); ++ dev->gem_reclaim.stopped = true; ++ mutex_unlock(&dev->gem_reclaim.scan_lock); ++ shrinker_free(dev->gem_reclaim.shrinker); ++ dev->gem_reclaim.shrinker = NULL; ++ cancel_work_sync(&dev->gem_reclaim.work); ++} ++ ++static void pvr_gem_shrinker_release(struct drm_device *drm, void *data) ++{ ++ pvr_gem_shrinker_fini(data); ++} ++ ++int pvr_gem_shrinker_init(struct pvr_device *dev) ++{ ++ struct shrinker *shrinker; ++ ++ drm_gem_lru_init(&dev->gem_reclaim.evictable); ++ drm_gem_lru_init(&dev->gem_reclaim.dontneed); ++ drm_gem_lru_init(&dev->gem_reclaim.pinned); ++ mutex_init(&dev->gem_reclaim.scan_lock); ++ INIT_WORK(&dev->gem_reclaim.work, pvr_gem_reclaim_work); ++ shrinker = shrinker_alloc(0, "drm-powervr"); ++ if (!shrinker) ++ return -ENOMEM; ++ dev->gem_reclaim.shrinker = shrinker; ++ shrinker->private_data = dev; ++ shrinker->count_objects = pvr_gem_count; ++ shrinker->scan_objects = pvr_gem_scan; ++ shrinker_register(shrinker); ++ return drmm_add_action_or_reset(&dev->base, pvr_gem_shrinker_release, dev); ++} +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c +index a4996b9..7f522fd 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.c ++++ b/drivers/gpu/drm/imagination/pvr_mmu.c +@@ -2654,3 +2654,21 @@ int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, + + return err; + } ++ ++/* Reclaim only needs an unmap cursor, never preallocated page tables. */ ++struct pvr_mmu_op_context *pvr_mmu_unmap_context_create(struct pvr_mmu_context *ctx) ++{ ++ struct pvr_mmu_op_context *op_ctx = kzalloc(sizeof(*op_ctx), GFP_NOWAIT); ++ ++ if (!op_ctx) ++ return ERR_PTR(-ENOMEM); ++ op_ctx->mmu_ctx = ctx; ++ return op_ctx; ++} ++ ++int pvr_mmu_unmap_flush(struct pvr_mmu_op_context *op_ctx) ++{ ++ pvr_mmu_op_context_sync(op_ctx); ++ pvr_mmu_flush_request_all(op_ctx->mmu_ctx->pvr_dev); ++ return pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true); ++} +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.h b/drivers/gpu/drm/imagination/pvr_mmu.h +index a8ecd46..a203336 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.h ++++ b/drivers/gpu/drm/imagination/pvr_mmu.h +@@ -103,6 +103,8 @@ pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, + + int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, + u64 device_addr); ++struct pvr_mmu_op_context *pvr_mmu_unmap_context_create(struct pvr_mmu_context *ctx); ++int pvr_mmu_unmap_flush(struct pvr_mmu_op_context *op_ctx); + int pvr_mmu_unmap(struct pvr_mmu_op_context *op_ctx, u64 device_addr, u64 size); + + #endif /* PVR_MMU_H */ +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index 4988a62..b398d9e 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -131,6 +131,9 @@ struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx) + struct pvr_vm_gpuva { + /** @base: The wrapped drm_gpuva object. */ + struct drm_gpuva base; ++ ++ /** @evicted: PTEs absent, even if another VA of this BO is resident. */ ++ bool evicted; + }; + + #define to_pvr_vm_gpuva(va) container_of_const(va, struct pvr_vm_gpuva, base) +@@ -1524,3 +1527,100 @@ struct drm_gem_object *pvr_vm_resv_obj(struct pvr_vm_context *vm_ctx) + { + return drm_gpuvm_resv_obj(&vm_ctx->gpuvm_mgr); + } ++ ++static bool pvr_vm_quiet(struct pvr_vm_context *vm, unsigned int idle_ms) ++{ ++ struct dma_resv_iter cursor; ++ struct dma_fence *fence; ++ bool idle = true; ++ ++ lockdep_assert_held(&vm->lock); ++ dma_resv_iter_begin(&cursor, pvr_vm_get_dma_resv(vm), DMA_RESV_USAGE_BOOKKEEP); ++ dma_resv_for_each_fence_unlocked(&cursor, fence) { ++ /* The signalled bit can be set before the timestamp is published. ++ * Do not spin or touch a job's lifetime from reclaim. ++ */ ++ if (!dma_fence_is_signaled(fence) || ++ !test_bit(DMA_FENCE_FLAG_TIMESTAMP_BIT, &fence->flags)) { ++ idle = false; ++ break; ++ } ++ vm->last_retired = max(vm->last_retired, dma_fence_timestamp(fence)); ++ } ++ dma_resv_iter_end(&cursor); ++ return idle && ktime_ms_delta(ktime_get(), vm->last_retired) >= idle_ms; ++} ++ ++/* BO resv stabilizes its vm_bo list; all VM locks are trylocked before edits. */ ++bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, ++ unsigned int *reason) ++{ ++ struct drm_gpuvm_bo *vm_bo, *last = NULL; ++ bool success = false; ++ ++ drm_gem_for_each_gpuvm_bo(vm_bo, obj) { ++ struct pvr_vm_context *vm = to_pvr_vm_context(vm_bo->vm); ++ ++ if (!mutex_trylock(&vm->lock)) { ++ *reason = 6; ++ goto unlock; ++ } ++ last = vm_bo; ++ if (!vm->is_userspace_context || ++ !dma_resv_test_signaled(pvr_vm_get_dma_resv(vm), DMA_RESV_USAGE_BOOKKEEP)) { ++ *reason = 7; ++ goto unlock; ++ } ++ if (!pvr_vm_quiet(vm, idle_ms)) { ++ *reason = 8; ++ goto unlock; ++ } ++ if (pvr_device_is_apple(vm->pvr_dev) && ++ !pvr_apple_vm_evictable(vm->pvr_dev, vm)) { ++ *reason = 9; ++ goto unlock; ++ } ++ } ++ ++ drm_gem_for_each_gpuvm_bo(vm_bo, obj) { ++ struct pvr_vm_context *vm = to_pvr_vm_context(vm_bo->vm); ++ struct pvr_mmu_op_context *op; ++ struct drm_gpuva *va; ++ int err = 0, flush_err; ++ ++ op = pvr_mmu_unmap_context_create(vm->mmu_ctx); ++ if (IS_ERR(op)) { ++ *reason = 10; ++ goto unlock; ++ } ++ /* On any partial failure keep pages and validate all cleared VAs. */ ++ drm_gpuvm_bo_evict(vm_bo, true); ++ drm_gpuvm_bo_for_each_va(va, vm_bo) { ++ struct pvr_vm_gpuva *pva = to_pvr_vm_gpuva(va); ++ ++ if (pva->evicted) ++ continue; ++ /* Failed unmaps may have cleared a prefix. Restore the full VA. */ ++ pva->evicted = true; ++ err = pvr_mmu_unmap(op, va->va.addr, va->va.range); ++ if (err) ++ break; ++ } ++ flush_err = pvr_mmu_unmap_flush(op); ++ pvr_mmu_op_context_destroy(op); ++ if (err || flush_err) { ++ *reason = 11; ++ goto unlock; ++ } ++ } ++ success = true; ++unlock: ++ if (last) { ++ drm_gem_for_each_gpuvm_bo(vm_bo, obj) { ++ mutex_unlock(&to_pvr_vm_context(vm_bo->vm)->lock); ++ if (vm_bo == last) ++ break; ++ } ++ } ++ return success; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h +index be5c99c..3270743 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.h ++++ b/drivers/gpu/drm/imagination/pvr_vm.h +@@ -66,6 +66,8 @@ void pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx); + dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx); + struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx); + struct drm_gem_object *pvr_vm_resv_obj(struct pvr_vm_context *vm_ctx); ++bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, ++ unsigned int *reason); + + int pvr_static_data_areas_get(const struct pvr_device *pvr_dev, + struct drm_pvr_ioctl_dev_query_args *args); 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 18/49] 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 3d357204a675506a44d6b8dbe1e35c5d5977b0ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 19/49] drm/imagination: Restore evicted VM mappings before arming jobs A faultless GPU must never start work with a hole left by reclaim. The first render on the iPad 5 hung before firmware boot when the HWRT dependency walk reacquired a reservation already owned by drm_exec. Assert ownership there instead of taking a NULL-ticket recursive lock. Lock each distinct submission VM before taking any reservation, validate the GPUVM evict list, and retain VM locks through arming, VM fence publication and queueing. Serialize submissions with the existing order mutex, now common, as the nesting anchor for multiple VM locks. Map and unmap only take one VM lock, then reservations; reclaim only trylocks. PowerVR uses GPUVM flags zero and does not populate its extobj list. Keep the internally spinlocked evict list, add vm_bo_validate, and use drm_gpuvm_prepare_range over the actual VA tree in the drm_exec retry pass. Restore every marked VA from its new DMA-mapped sgt, publish the flush, clear eviction and refresh LRU recency. Keep prior restores on a failure and return -ENOMEM before prepare_job_syncs can arm anything. Track eviction per VA for mixed resident/evicted mappings and splits. Unmap skips cleared PTEs. Repair a partial eviction by clearing its full range before remapping. Move VM_MAP's page acquisition and MMU cursor creation under the VM and BO locks: a bare sgt obtained before either lock could otherwise be freed by reclaim before MAP uses it. Native validation defers context and HWRT reservations until its MMU flush completes: the flush error path resets and vmaps firmware objects. Reinitialize exec for the full reservation pass before dependencies; VM mutexes keep residency protected across both passes. Apple uses its RAM-only invalidation with the complete reservation set held. Affects: every PowerVR device Assisted-by: LLM --- ...Restore-evicted-VM-mappings-before-a.patch | 517 ++++++++++++++++++ 1 file changed, 517 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch b/kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch new file mode 100644 index 00000000..c427af4d --- /dev/null +++ b/kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch @@ -0,0 +1,517 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:40:55 +0100 +Subject: [PATCH 37/38] drm/imagination: Restore evicted VM mappings before + arming jobs + +A faultless GPU must never start work with a hole left by reclaim. +The first render on the iPad 5 hung before firmware boot when the HWRT +dependency walk reacquired a reservation already owned by drm_exec. +Assert ownership there instead of taking a NULL-ticket recursive lock. +Lock each distinct submission VM before taking any reservation, validate +the GPUVM evict list, and retain VM locks through arming, VM fence +publication and queueing. Serialize submissions with the existing order +mutex, now common, as the nesting anchor for multiple VM locks. Map and +unmap only take one VM lock, then reservations; reclaim only trylocks. + +PowerVR uses GPUVM flags zero and does not populate its extobj list. +Keep the internally spinlocked evict list, add vm_bo_validate, and use +drm_gpuvm_prepare_range over the actual VA tree in the drm_exec retry +pass. Restore every marked VA from its new DMA-mapped sgt, publish the +flush, clear eviction and refresh LRU recency. Keep prior restores on a +failure and return -ENOMEM before prepare_job_syncs can arm anything. + +Track eviction per VA for mixed resident/evicted mappings and splits. +Unmap skips cleared PTEs. Repair a partial eviction by clearing its full +range before remapping. Move VM_MAP's page acquisition and MMU cursor +creation under the VM and BO locks: a bare sgt obtained before either +lock could otherwise be freed by reclaim before MAP uses it. + +Native validation defers context and HWRT reservations until its MMU +flush completes: the flush error path resets and vmaps firmware objects. +Reinitialize exec for the full reservation pass before dependencies; VM +mutexes keep residency protected across both passes. Apple uses its +RAM-only invalidation with the complete reservation set held. + +Affects: every PowerVR device + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_job.c | 93 ++++++++++---- + drivers/gpu/drm/imagination/pvr_vm.c | 171 ++++++++++++++++++++++---- + drivers/gpu/drm/imagination/pvr_vm.h | 5 + + 3 files changed, 219 insertions(+), 50 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index bd0dc2d..b4c26ee 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -367,10 +367,10 @@ prepare_job_syncs(struct pvr_file *pvr_file, + dma_resv_usage_rw(job_data->job->type == + DRM_PVR_JOB_TYPE_GEOMETRY); + +- dma_resv_lock(obj->resv, NULL); ++ /* prepare_job_resvs_for_each() already locked this with drm_exec. */ ++ dma_resv_assert_held(obj->resv); + err = drm_sched_job_add_resv_dependencies(&job_data->job->base, + obj->resv, usage); +- dma_resv_unlock(obj->resv); + if (err) + return err; + } +@@ -561,7 +561,7 @@ push_jobs(struct pvr_job_data *job_data, u32 job_count) + + static int + jobs_lock_all_objs(struct drm_exec *exec, struct pvr_job_data *job_data, +- u32 job_count) ++ u32 job_count, bool vm_only) + { + for (u32 i = 0; i < job_count; i++) { + struct pvr_job *job = job_data[i].job; +@@ -572,17 +572,20 @@ jobs_lock_all_objs(struct drm_exec *exec, struct pvr_job_data *job_data, + * concurrent submission to the same queue. Apple contexts have + * no firmware object; their queue's scheduler entity orders jobs. + */ +- if (job->ctx->fw_obj) { ++ if (!vm_only && job->ctx->fw_obj) { + err = drm_exec_lock_obj(exec, gem_from_pvr_gem(job->ctx->fw_obj->gem)); + if (err) + return err; + } + + err = drm_exec_prepare_obj(exec, pvr_vm_resv_obj(job->ctx->vm_ctx), job_count); ++ if (err) ++ return err; ++ err = pvr_vm_prepare_objects(job->ctx->vm_ctx, exec); + if (err) + return err; + +- if (job->hwrt) { ++ if (!vm_only && job->hwrt) { + err = drm_exec_prepare_obj(exec, pvr_hwrt_data_resv_obj(job->hwrt), 1); + if (err) + return err; +@@ -594,10 +597,10 @@ jobs_lock_all_objs(struct drm_exec *exec, struct pvr_job_data *job_data, + + static int + prepare_job_resvs_for_each(struct drm_exec *exec, struct pvr_job_data *job_data, +- u32 job_count) ++ u32 job_count, bool vm_only) + { + drm_exec_until_all_locked(exec) { +- int err = jobs_lock_all_objs(exec, job_data, job_count); ++ int err = jobs_lock_all_objs(exec, job_data, job_count, vm_only); + + drm_exec_retry_on_contention(exec); + if (err) +@@ -737,8 +740,8 @@ pvr_jobs_link_geom_frag(struct pvr_job_data *job_data, u32 *job_count) + * * -%EINVAL on invalid arguments, or + * * Any other error. + */ +-/* Apple GPUs: one submission arms its jobs at a time (pvr_submit_jobs()) */ +-static DEFINE_MUTEX(apple_submit_order); ++/* Nest all submission VM locks outside reservation locking and job arming. */ ++static DEFINE_MUTEX(pvr_submit_order); + + int + pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, +@@ -749,6 +752,8 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + struct xarray signal_array; + u32 jobs_alloced = 0; + struct drm_exec exec; ++ struct pvr_vm_context **vms = NULL; ++ u32 vm_count = 0; + int err; + + if (!args->jobs.count) +@@ -772,14 +777,6 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + + jobs_alloced = args->jobs.count; + +- /* +- * Flush MMU if needed - this has been deferred until now to avoid +- * overuse of this expensive operation. +- */ +- err = pvr_mmu_flush_exec(pvr_dev, false); +- if (err) +- goto out_job_data_cleanup; +- + drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES, 0); + + xa_init_flags(&signal_array, XA_FLAGS_ALLOC); +@@ -792,18 +789,59 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + * its fragment job, which then rendered the other scene and hung the GPU. + * Submissions are made whole under one lock. + */ +- if (pvr_device_is_apple(pvr_dev)) { +- err = mutex_lock_interruptible(&apple_submit_order); ++ vms = kvmalloc_array(jobs_alloced, sizeof(*vms), GFP_KERNEL); ++ if (!vms) { ++ err = -ENOMEM; ++ goto out_exec_fini_unlocked; ++ } ++ err = mutex_lock_interruptible(&pvr_submit_order); ++ if (err) ++ goto out_exec_fini_unlocked; ++ for (u32 i = 0; i < jobs_alloced; i++) { ++ struct pvr_vm_context *vm = job_data[i].job->ctx->vm_ctx; ++ u32 j; ++ ++ for (j = 0; j < vm_count && vms[j] != vm; j++) ++ ; ++ if (j != vm_count) ++ continue; ++ pvr_vm_submit_lock(vm, &pvr_submit_order); ++ vms[vm_count++] = vm; ++ } ++ ++ err = prepare_job_resvs_for_each(&exec, job_data, args->jobs.count, ++ !pvr_device_is_apple(pvr_dev)); ++ if (err) ++ goto out_exec_fini; ++ for (u32 i = 0; i < vm_count; i++) { ++ err = pvr_vm_validate(vms[i], &exec); + if (err) +- goto out_exec_fini_unlocked; ++ goto out_exec_fini; + } + +- err = prepare_job_syncs_for_each(pvr_file, job_data, &args->jobs.count, +- &signal_array); ++ /* ++ * Flush MMU if needed - this has been deferred until now to avoid ++ * overuse of this expensive operation. ++ */ ++ err = pvr_mmu_flush_exec(pvr_dev, false); + if (err) + goto out_exec_fini; + +- err = prepare_job_resvs_for_each(&exec, job_data, args->jobs.count); ++ if (!pvr_device_is_apple(pvr_dev)) { ++ /* A failed IMG flush resets and vmaps firmware objects. Do not ++ * hold context/HWRT reservations until that path has completed. ++ * VM mutexes span both exec passes, so backing cannot be evicted. ++ */ ++ drm_exec_fini(&exec); ++ drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES, 0); ++ err = prepare_job_resvs_for_each(&exec, job_data, args->jobs.count, false); ++ if (err) ++ goto out_exec_fini; ++ } ++ ++ /* prepare_job_syncs arms jobs: residency failures must precede it. */ ++ err = prepare_job_syncs_for_each(pvr_file, job_data, &args->jobs.count, ++ &signal_array); + if (err) + goto out_exec_fini; + +@@ -820,10 +858,15 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + err = 0; + + out_exec_fini: +- if (pvr_device_is_apple(pvr_dev)) +- mutex_unlock(&apple_submit_order); ++ drm_exec_fini(&exec); ++ while (vm_count) ++ pvr_vm_submit_unlock(vms[--vm_count]); ++ mutex_unlock(&pvr_submit_order); ++ goto out_sync_cleanup; + out_exec_fini_unlocked: + drm_exec_fini(&exec); ++out_sync_cleanup: ++ kvfree(vms); + pvr_sync_signal_array_cleanup(&signal_array); + + out_job_data_cleanup: +diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c +index b398d9e..4f6d67f 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.c ++++ b/drivers/gpu/drm/imagination/pvr_vm.c +@@ -234,7 +234,16 @@ static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op) + + static void pvr_vm_bind_op_fini(struct pvr_vm_bind_op *bind_op) + { +- drm_gpuvm_bo_put(bind_op->gpuvm_bo); ++ if (bind_op->gpuvm_bo) { ++ struct drm_gem_object *obj = bind_op->gpuvm_bo->obj; ++ ++ /* Last put removes vm_bo from the BO's GPUVA list. */ ++ drm_gem_object_get(obj); ++ dma_resv_lock(obj->resv, NULL); ++ drm_gpuvm_bo_put(bind_op->gpuvm_bo); ++ dma_resv_unlock(obj->resv); ++ drm_gem_object_put(obj); ++ } + + kfree(bind_op->new_va); + kfree(bind_op->prev_va); +@@ -256,7 +265,6 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, + struct drm_gem_object *obj = gem_from_pvr_gem(pvr_obj); + const bool is_user = vm_ctx->is_userspace_context; + const u64 pvr_obj_size = pvr_gem_object_size(pvr_obj); +- struct sg_table *sgt; + u64 offset_plus_size; + int err; + +@@ -289,20 +297,6 @@ pvr_vm_bind_op_map_init(struct pvr_vm_bind_op *bind_op, + goto err_bind_op_fini; + } + +- /* Pin pages so they're ready for use. */ +- sgt = pvr_gem_object_get_pages_sgt(pvr_obj); +- err = PTR_ERR_OR_ZERO(sgt); +- if (err) +- goto err_bind_op_fini; +- +- bind_op->mmu_op_ctx = +- pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, offset, size); +- err = PTR_ERR_OR_ZERO(bind_op->mmu_op_ctx); +- if (err) { +- bind_op->mmu_op_ctx = NULL; +- goto err_bind_op_fini; +- } +- + bind_op->pvr_obj = pvr_obj; + bind_op->vm_ctx = vm_ctx; + bind_op->device_addr = device_addr; +@@ -358,6 +352,16 @@ err_bind_op_fini: + return err; + } + ++static void pvr_vm_gpuva_unlink(struct drm_gpuva *va) ++{ ++ struct drm_gpuvm_bo *vm_bo = va->vm_bo; ++ ++ /* prepare_range() only locks objects with a live VA. */ ++ if (vm_bo->evicted && list_is_singular(&vm_bo->list.gpuva)) ++ drm_gpuvm_bo_evict(vm_bo, false); ++ drm_gpuva_unlink(va); ++} ++ + /** + * pvr_vm_gpuva_map() - Insert a mapping into a memory context. + * @op: gpuva op containing the remap details. +@@ -409,14 +413,17 @@ pvr_vm_gpuva_unmap(struct drm_gpuva_op *op, void *op_ctx) + { + struct pvr_vm_bind_op *ctx = op_ctx; + +- int err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr, +- op->unmap.va->va.range); ++ int err = 0; ++ ++ if (!to_pvr_vm_gpuva(op->unmap.va)->evicted) ++ err = pvr_mmu_unmap(ctx->mmu_op_ctx, op->unmap.va->va.addr, ++ op->unmap.va->va.range); + + if (err) + return err; + + drm_gpuva_unmap(&op->unmap); +- drm_gpuva_unlink(op->unmap.va); ++ pvr_vm_gpuva_unlink(op->unmap.va); + kfree(to_pvr_vm_gpuva(op->unmap.va)); + + return 0; +@@ -443,9 +450,13 @@ pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx) + int err; + + drm_gpuva_op_remap_to_unmap_range(&op->remap, &va_start, &va_range); +- err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range); +- if (err) +- return err; ++ if (!to_pvr_vm_gpuva(op->remap.unmap->va)->evicted) { ++ err = pvr_mmu_unmap(ctx->mmu_op_ctx, va_start, va_range); ++ if (err) ++ return err; ++ } ++ ctx->prev_va->evicted = to_pvr_vm_gpuva(op->remap.unmap->va)->evicted; ++ ctx->next_va->evicted = ctx->prev_va->evicted; + + /* No actual remap required: the page table tree depth is fixed to 3, + * and we use 4k page table entries only for now. +@@ -464,7 +475,7 @@ pvr_vm_gpuva_remap(struct drm_gpuva_op *op, void *op_ctx) + ctx->next_va = NULL; + } + +- drm_gpuva_unlink(op->remap.unmap->va); ++ pvr_vm_gpuva_unlink(op->remap.unmap->va); + kfree(to_pvr_vm_gpuva(op->remap.unmap->va)); + + return 0; +@@ -529,7 +540,10 @@ static void pvr_gpuvm_free(struct drm_gpuvm *gpuvm) + kfree(to_pvr_vm_context(gpuvm)); + } + ++static int pvr_vm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec); ++ + static const struct drm_gpuvm_ops pvr_vm_gpuva_ops = { ++ .vm_bo_validate = pvr_vm_bo_validate, + .vm_free = pvr_gpuvm_free, + .sm_step_map = pvr_vm_gpuva_map, + .sm_step_remap = pvr_vm_gpuva_remap, +@@ -832,8 +846,26 @@ __pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, + if (err) + goto err_cleanup; + +- err = pvr_vm_bind_op_exec(&bind_op); ++ { ++ struct sg_table *sgt = pvr_gem_get_pages_sgt_locked(pvr_obj); + ++ err = PTR_ERR_OR_ZERO(sgt); ++ if (!err) { ++ bind_op.mmu_op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, ++ pvr_obj_offset, size); ++ err = PTR_ERR_OR_ZERO(bind_op.mmu_op_ctx); ++ if (err) ++ bind_op.mmu_op_ctx = NULL; ++ } ++ if (!err) ++ err = pvr_vm_bind_op_exec(&bind_op); ++ } ++ ++ /* Publish table edits while both VM and BO reservations still exclude reclaim. */ ++ if (bind_op.mmu_op_ctx) { ++ pvr_mmu_op_context_destroy(bind_op.mmu_op_ctx); ++ bind_op.mmu_op_ctx = NULL; ++ } + drm_gpuvm_exec_unlock(&vm_exec); + + err_cleanup: +@@ -915,6 +947,11 @@ pvr_vm_unmap_obj_locked(struct pvr_vm_context *vm_ctx, + + err = pvr_vm_bind_op_exec(&bind_op); + ++ /* Publish table edits while both VM and BO reservations still exclude reclaim. */ ++ if (bind_op.mmu_op_ctx) { ++ pvr_mmu_op_context_destroy(bind_op.mmu_op_ctx); ++ bind_op.mmu_op_ctx = NULL; ++ } + drm_gpuvm_exec_unlock(&vm_exec); + + err_cleanup: +@@ -1588,13 +1625,16 @@ bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, + struct drm_gpuva *va; + int err = 0, flush_err; + ++ if (list_empty(&vm_bo->list.gpuva)) ++ continue; + op = pvr_mmu_unmap_context_create(vm->mmu_ctx); + if (IS_ERR(op)) { + *reason = 10; + goto unlock; + } + /* On any partial failure keep pages and validate all cleared VAs. */ +- drm_gpuvm_bo_evict(vm_bo, true); ++ if (!vm_bo->evicted) ++ drm_gpuvm_bo_evict(vm_bo, true); + drm_gpuvm_bo_for_each_va(va, vm_bo) { + struct pvr_vm_gpuva *pva = to_pvr_vm_gpuva(va); + +@@ -1624,3 +1664,84 @@ unlock: + } + return success; + } ++ ++int pvr_vm_submit_lock(struct pvr_vm_context *vm, struct mutex *nest) ++{ ++ mutex_lock_nest_lock(&vm->lock, nest); ++ return 0; ++} ++ ++void pvr_vm_submit_unlock(struct pvr_vm_context *vm) ++{ ++ /* Fence reservation may prune completed fences even on a failed submit. ++ * Keep a conservative quiet-period floor before releasing the VM lock. ++ */ ++ vm->last_retired = max(vm->last_retired, ktime_get()); ++ mutex_unlock(&vm->lock); ++} ++ ++int pvr_vm_prepare_objects(struct pvr_vm_context *vm, struct drm_exec *exec) ++{ ++ lockdep_assert_held(&vm->lock); ++ /* PowerVR does not register extobjs. Prepare its actual VA tree instead. */ ++ return drm_gpuvm_prepare_range(&vm->gpuvm_mgr, exec, 0, ++ vm->gpuvm_mgr.mm_range, 0); ++} ++ ++static int pvr_vm_bo_validate(struct drm_gpuvm_bo *vm_bo, struct drm_exec *exec) ++{ ++ struct pvr_vm_context *vm = to_pvr_vm_context(vm_bo->vm); ++ struct pvr_gem_object *bo = gem_to_pvr_gem(vm_bo->obj); ++ struct drm_gpuva *va; ++ struct sg_table *sgt; ++ ++ lockdep_assert_held(&vm->lock); ++ dma_resv_assert_held(vm_bo->obj->resv); ++ sgt = pvr_gem_get_pages_sgt_locked(bo); ++ if (IS_ERR(sgt)) ++ return -ENOMEM; ++ drm_gpuvm_bo_for_each_va(va, vm_bo) { ++ struct pvr_vm_gpuva *pva = to_pvr_vm_gpuva(va); ++ struct pvr_mmu_op_context *op; ++ int err; ++ ++ if (!pva->evicted) ++ continue; ++ op = pvr_mmu_op_context_create(vm->mmu_ctx, sgt, va->gem.offset, va->va.range); ++ if (IS_ERR(op)) ++ return -ENOMEM; ++ /* Also repair an eviction that stopped after clearing only a prefix. */ ++ err = pvr_mmu_unmap(op, va->va.addr, va->va.range); ++ if (!err) ++ err = pvr_mmu_map(op, va->va.range, bo->flags, va->va.addr); ++ pvr_mmu_op_context_destroy(op); ++ if (err) ++ return -ENOMEM; ++ pva->evicted = false; ++ } ++ /* A failed flush leaves vm_bo evicted, so a retry must flush again. */ ++ pvr_mmu_flush_request_all(vm->pvr_dev); ++ if (pvr_mmu_flush_exec(vm->pvr_dev, false)) ++ return -ENOMEM; ++ drm_gpuvm_bo_evict(vm_bo, false); ++ pvr_gem_lru_update_locked(bo); ++ atomic64_inc(&vm->pvr_dev->gem_reclaim.restores); ++ return 0; ++} ++ ++int pvr_vm_validate(struct pvr_vm_context *vm, struct drm_exec *exec) ++{ ++ struct drm_gpuva *va; ++ int err; ++ ++ lockdep_assert_held(&vm->lock); ++ err = drm_gpuvm_validate(&vm->gpuvm_mgr, exec); ++ if (err) { ++ atomic64_inc(&vm->pvr_dev->gem_reclaim.restore_fail); ++ return -ENOMEM; ++ } ++ drm_gpuvm_for_each_va(va, &vm->gpuvm_mgr) ++ if (va->gem.obj) ++ pvr_gem_lru_update_locked(gem_to_pvr_gem(va->gem.obj)); ++ return 0; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h +index 3270743..15ee067 100644 +--- a/drivers/gpu/drm/imagination/pvr_vm.h ++++ b/drivers/gpu/drm/imagination/pvr_vm.h +@@ -26,6 +26,7 @@ struct drm_pvr_ioctl_get_heap_info_args; + + /* Forward declaration from */ + struct drm_exec; ++struct mutex; + + /** + * struct pvr_vm_pin - Device-virtual range whose mappings must not change +@@ -66,6 +67,10 @@ void pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx); + dma_addr_t pvr_vm_get_page_table_root_addr(struct pvr_vm_context *vm_ctx); + struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx); + struct drm_gem_object *pvr_vm_resv_obj(struct pvr_vm_context *vm_ctx); ++int pvr_vm_submit_lock(struct pvr_vm_context *vm_ctx, struct mutex *nest); ++void pvr_vm_submit_unlock(struct pvr_vm_context *vm_ctx); ++int pvr_vm_prepare_objects(struct pvr_vm_context *vm_ctx, struct drm_exec *exec); ++int pvr_vm_validate(struct pvr_vm_context *vm_ctx, struct drm_exec *exec); + bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, + unsigned int *reason); + 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 20/49] 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 9d003a7c9cb3333340c9394187ba206a63ec99f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 01:28:57 +0100 Subject: [PATCH 21/49] drm/imagination: apple: Keep reclaim out of the job worker Memory pressure can stall a scheduler worker while its job fence is needed to make memory reclaimable. Prepare S8003 PM resources in submit ioctl context, before VM locks and arming. Take scene sets there, allocate and persistently vmap aux buffers and pending pool growth outside the worker's submit_lock, then publish pointers under that lock. Keep an active set's old pool until the worker selects the idle set. The worker only installs pre-reserved capacity and writes the existing records. Missing capacity fails cleanly instead of allocating in the worker. Allocation audit of run_job through chip submission: - Shared submit_compute_job: fence allocation becomes GFP_NOWAIT. - Shared submit_render_job (T8010): fence and translated command buffer become GFP_NOWAIT, retaining existing -ENOMEM cleanup. - Shared submit_job (S8003): fence allocation becomes GFP_NOWAIT. - S8003 pm_build: aux GEM creation, reserved VM mapping, pool creation and pool/aux vmaps move to prepare_job. Persistent maps make zeroing allocation-free. Replaced pools are released only when their set idles. - S8003 job_write/fill and T8010 write_compute/write_geom/write_frag allocate nothing. Firmware restart reuses the initialized image/pages; the T8010 coherent window/heap and S8003 image are init-time allocations. Publish PM set readiness only after aux mappings and pool resources exist. Arming counts ready sets under set_lock and the worker selects them under submit_lock; both locks protect readiness publication. Only ioctl preparers take prepare_lock. No worker lock is held while allocating the new resources. PM record format and sizing are unchanged. Affects: Apple A9 and A10 Assisted-by: LLM --- ...apple-Keep-reclaim-out-of-the-job-wo.patch | 462 ++++++++++++++++++ 1 file changed, 462 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch diff --git a/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch b/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch new file mode 100644 index 00000000..3601d519 --- /dev/null +++ b/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch @@ -0,0 +1,462 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Sun, 15 Nov 2026 23:43:11 +0100 +Subject: [PATCH 38/38] drm/imagination: apple: Keep reclaim out of the job + worker + +Memory pressure can stall a scheduler worker while its job fence is +needed to make memory reclaimable. Prepare S8003 PM resources in submit +ioctl context, before VM locks and arming. Take scene sets there, allocate +and persistently vmap aux buffers and pending pool growth outside the +worker's submit_lock, then publish pointers under that lock. Keep an +active set's old pool until the worker selects the idle set. The worker +only installs pre-reserved capacity and writes the existing records. +Missing capacity fails cleanly instead of allocating in the worker. + +Allocation audit of run_job through chip submission: +- Shared submit_compute_job: fence allocation becomes GFP_NOWAIT. +- Shared submit_render_job (T8010): fence and translated command buffer + become GFP_NOWAIT, retaining existing -ENOMEM cleanup. +- Shared submit_job (S8003): fence allocation becomes GFP_NOWAIT. +- S8003 pm_build: aux GEM creation, reserved VM mapping, pool creation + and pool/aux vmaps move to prepare_job. Persistent maps make zeroing + allocation-free. Replaced pools are released only when their set idles. +- S8003 job_write/fill and T8010 write_compute/write_geom/write_frag + allocate nothing. Firmware restart reuses the initialized image/pages; + the T8010 coherent window/heap and S8003 image are init-time allocations. + +Publish PM set readiness only after aux mappings and pool resources exist. +Arming counts ready sets under set_lock and the worker selects them under +submit_lock; both locks protect readiness publication. + +Only ioctl preparers take prepare_lock. No worker lock is held while +allocating the new resources. PM record format and sizing are unchanged. + +Affects: Apple A9 and A10 + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 16 +- + drivers/gpu/drm/imagination/pvr_apple.h | 6 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 215 ++++++++++++++---- + drivers/gpu/drm/imagination/pvr_job.c | 7 + + 5 files changed, 195 insertions(+), 50 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index d062461..b26ac79 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -917,7 +917,7 @@ struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, + struct pvr_apple *apple = to_apple(pvr_dev); + struct pvr_apple_fence *fence; + +- fence = kzalloc(sizeof(*fence), GFP_KERNEL); ++ fence = kzalloc(sizeof(*fence), GFP_NOWAIT); + if (!fence) + return ERR_PTR(-ENOMEM); + return pvr_apple_job_fence(apple, fence, APPLE_ENGINE_COMPUTE, +@@ -1165,8 +1165,8 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) + if (apple->chip->submit) + return pvr_apple_submit_job(job); + +- fence = kzalloc(sizeof(*fence), GFP_KERNEL); +- hw = kmalloc(apple->chip->render_job_size, GFP_KERNEL); ++ fence = kzalloc(sizeof(*fence), GFP_NOWAIT); ++ hw = kmalloc(apple->chip->render_job_size, GFP_NOWAIT); + if (!fence || !hw) { + kfree(fence); + kfree(hw); +@@ -1200,7 +1200,7 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) + APPLE_ENGINE_COMPUTE; + struct pvr_apple_fence *fence; + +- fence = kzalloc(sizeof(*fence), GFP_KERNEL); ++ fence = kzalloc(sizeof(*fence), GFP_NOWAIT); + if (!fence) + return ERR_PTR(-ENOMEM); + return pvr_apple_job_fence(apple, fence, engine, apple->chip->submit(apple, job)); +@@ -1679,3 +1679,11 @@ bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *v + mutex_unlock(&apple->submit_lock); + return allowed; + } ++ ++/* IOCTL context, before VM locks, reservation locking or scheduler arming. */ ++int pvr_apple_job_prepare_resources(struct pvr_job *job) ++{ ++ struct pvr_apple *apple = to_apple(job->pvr_dev); ++ ++ return apple->chip->prepare_job ? apple->chip->prepare_job(apple, job) : 0; ++} +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index fe3c7a5..f6b0fbe 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.h ++++ b/drivers/gpu/drm/imagination/pvr_apple.h +@@ -179,6 +179,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); + void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev); + int pvr_apple_job_add_deps(struct pvr_job *job); + void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); ++int pvr_apple_job_prepare_resources(struct pvr_job *job); + bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); + 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); +@@ -210,6 +211,11 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, + *ext_type = 0; + return 0; + } ++static inline int pvr_apple_job_prepare_resources(struct pvr_job *job) ++{ ++ return 0; ++} ++ + static inline bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, + struct pvr_vm_context *vm_ctx) + { +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index 1cec40e..2b3e560 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -266,6 +266,7 @@ struct pvr_apple_chip { + void (*write_frag)(struct pvr_apple *apple, u32 seq, const void *hw); + void (*report_hang)(struct pvr_apple *apple); + ++ int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); + int (*submit)(struct pvr_apple *apple, struct pvr_job *job); + u32 (*engine_done)(struct pvr_apple *apple, enum pvr_apple_engine engine); + void (*events)(struct pvr_apple *apple); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 2389178..c70b999 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -54,6 +54,7 @@ + #include + #include + #include ++#include + #include + #include + #include +@@ -246,6 +247,11 @@ struct s8003_pm { + u32 built; /* the firmware start it was built for */ + struct pvr_gem_object *pool; + struct pvr_gem_object *aux; /* the PM's tables, mapped in the VM */ ++ void *pool_map, *aux_map; ++ struct pvr_gem_object *next_pool; ++ void *next_pool_map; ++ u32 next_blocks; ++ bool ready; /* published under submit_lock and set_lock */ + const struct pvr_hwrt_data *hwrt; /* the scene's target */ + u32 scene; /* the firmware start the scene belongs to; 0: none */ + u32 ta_seq; /* its geometry job */ +@@ -254,6 +260,8 @@ struct s8003_pm { + + struct s8003_vm { + struct pvr_vm_context *vm_ctx; ++ /* Only ioctl preparers take this lock; the job worker never waits on it. */ ++ struct mutex prepare_lock; + u32 slot[2]; /* geometry and compute, fragment; 0: none */ + u32 attached; /* the firmware start the slots were loaded for */ + struct s8003_pm pm[2]; +@@ -1433,6 +1441,7 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex + if (!vm) + return -ENOMEM; + vm->vm_ctx = vm_ctx; ++ mutex_init(&vm->prepare_lock); + vm->pm[0].set = -1; + vm->pm[1].set = -1; + /* The PM reaches its sets through the job's slot, at the firmware's addresses */ +@@ -1446,10 +1455,10 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex + + /* + * How many scenes of a VM the firmware may hold: as many as the VM has PM +- * sets. It takes its first here, and its second while the firmware has more +- * than half of them left. Called when a job is ordered, in atomic context. ++ * sets. Reserve the first here, and the second while more than half remain. ++ * IOCTL context: arming counts only sets whose resources are fully ready. + */ +-static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, ++static unsigned int s8003_vm_reserve_sets(struct pvr_apple *apple, + struct pvr_vm_context *vm_ctx) + { + struct pvr_apple_s8003 *s = apple->chip_data; +@@ -1484,6 +1493,24 @@ static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, + return max(n, 1u); + } + ++static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, ++ struct pvr_vm_context *vm_ctx) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct s8003_vm *vm = xa_load(&s->vms, (unsigned long)vm_ctx); ++ unsigned long flags; ++ unsigned int n = 0, i; ++ ++ if (!vm) ++ return 1; ++ spin_lock_irqsave(&s->set_lock, flags); ++ for (i = 0; i < ARRAY_SIZE(vm->pm); i++) ++ if (vm->pm[i].set >= 0 && vm->pm[i].ready) ++ n++; ++ spin_unlock_irqrestore(&s->set_lock, flags); ++ return max(n, 1U); ++} ++ + static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) + { + unsigned long flags; +@@ -1492,10 +1519,18 @@ static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) + if (pm->set >= 0) + __clear_bit(pm->set, s->sets); + spin_unlock_irqrestore(&s->set_lock, flags); +- if (pm->pool) ++ if (pm->pool) { ++ pvr_gem_object_vunmap(pm->pool); + pvr_gem_object_put(pm->pool); +- if (pm->aux) ++ } ++ if (pm->next_pool) { ++ pvr_gem_object_vunmap(pm->next_pool); ++ pvr_gem_object_put(pm->next_pool); ++ } ++ if (pm->aux) { ++ pvr_gem_object_vunmap(pm->aux); + pvr_gem_object_put(pm->aux); ++ } + } + + /* +@@ -1535,17 +1570,6 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte + kfree(vm); + } + +-static int s8003_obj_zero(struct pvr_gem_object *obj) +-{ +- void *map = pvr_gem_object_vmap(obj); +- +- if (IS_ERR(map)) +- return PTR_ERR(map); +- memset(map, 0, pvr_gem_object_size(obj)); +- pvr_gem_object_vunmap(obj); +- return 0; +-} +- + static u64 s8003_pm_set_va(const struct s8003_pm *pm, u32 page) + { + return S8003_PM_SETS_VA + pm->set * S8003_PM_SET_SIZE + page * PVR_DEVICE_PAGE_SIZE; +@@ -1574,40 +1598,30 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s + u64 list_va, aux_va; + int err; + +- if (!pm->aux) { +- struct pvr_gem_object *aux; +- +- aux = pvr_gem_object_create(apple->pvr_dev, S8003_PM_AUX_SIZE, +- DRM_PVR_BO_PM_FW_PROTECT); +- if (IS_ERR(aux)) +- return PTR_ERR(aux); +- err = pvr_vm_map_reserved(vm->vm_ctx, aux, s8003_pm_aux_va(pm), S8003_PM_AUX_SIZE); +- if (err) { +- pvr_gem_object_put(aux); +- return err; +- } +- pm->aux = aux; +- } ++ if (!pm->aux || !pm->aux_map) ++ return -ENOMEM; + if (pm->built == s->gen && pm->blocks >= blocks) + return 0; + if (pm->blocks < blocks) { +- struct pvr_gem_object *pool; +- +- pool = pvr_gem_object_create(apple->pvr_dev, (size_t)blocks * S8003_PM_BLOCK_SIZE, +- PVR_BO_FW_FLAGS_DEVICE_CACHED); +- if (IS_ERR(pool)) +- return PTR_ERR(pool); +- if (pm->pool) ++ if (!pm->next_pool || pm->next_blocks < blocks) ++ return -ENOMEM; ++ /* This set is idle. Its previous pool can now be retired. */ ++ if (pm->pool) { ++ pvr_gem_object_vunmap(pm->pool); + pvr_gem_object_put(pm->pool); +- pm->pool = pool; +- pm->blocks = blocks; ++ } ++ pm->pool = pm->next_pool; ++ pm->pool_map = pm->next_pool_map; ++ pm->blocks = pm->next_blocks; ++ pm->next_pool = NULL; ++ pm->next_pool_map = NULL; ++ pm->next_blocks = 0; + } + pm->built = 0; +- err = s8003_obj_zero(pm->pool); +- if (!err) +- err = s8003_obj_zero(pm->aux); +- if (err) +- return err; ++ memset(pm->pool_map, 0, pvr_gem_object_size(pm->pool)); ++ memset(pm->aux_map, 0, S8003_PM_AUX_SIZE); ++ dma_sync_sgtable_for_device(apple_dev(apple), pm->pool->base.sgt, DMA_BIDIRECTIONAL); ++ dma_sync_sgtable_for_device(apple_dev(apple), pm->aux->base.sgt, DMA_BIDIRECTIONAL); + + control = s->pmsets.map + pm->set * S8003_PM_SET_SIZE; + list = control + PVR_DEVICE_PAGE_SIZE; +@@ -2126,6 +2140,113 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, + return true; + } + ++/* Publish capacity only once its pages, CPU maps and aux VA are ready. ++ * Worker readers hold submit_lock; the atomic arming callback holds set_lock. ++ */ ++static void s8003_pm_mark_ready(struct pvr_apple_s8003 *s, struct s8003_pm *pm) ++{ ++ unsigned long flags; ++ ++ lockdep_assert_held(&s->apple->submit_lock); ++ spin_lock_irqsave(&s->set_lock, flags); ++ pm->ready = true; ++ spin_unlock_irqrestore(&s->set_lock, flags); ++} ++ ++static int s8003_prepare_job(struct pvr_apple *apple, struct pvr_job *job) ++{ ++ struct pvr_apple_s8003 *s = apple->chip_data; ++ struct s8003_vm *vm = xa_load(&s->vms, (unsigned long)job->ctx->vm_ctx); ++ struct s8003_rt rt; ++ u32 blocks; ++ unsigned int i; ++ int err; ++ ++ if (job->type != DRM_PVR_JOB_TYPE_GEOMETRY) ++ return 0; ++ if (!vm) ++ return -EINVAL; ++ err = s8003_rt_derive(job->hwrt->hwrt_dataset, &rt); ++ if (err) ++ return err; ++ blocks = min_t(u32, max(DIV_ROUND_UP(job->hwrt->hwrt_dataset->free_lists[0]->initial_num_pages, 32), ++ DIV_ROUND_UP(50 + rt.tiles, 32)), S8003_PM_BLOCKS_MAX); ++ mutex_lock(&vm->prepare_lock); ++ mutex_lock(&apple->submit_lock); ++ s8003_vm_reserve_sets(apple, vm->vm_ctx); ++ mutex_unlock(&apple->submit_lock); ++ if (vm->pm[0].set < 0) { ++ err = -ENOMEM; ++ goto out; ++ } ++ for (i = 0; i < ARRAY_SIZE(vm->pm); i++) { ++ struct s8003_pm *pm = &vm->pm[i]; ++ struct pvr_gem_object *obj; ++ void *map; ++ bool need_pool; ++ ++ if (pm->set < 0) ++ continue; ++ if (!pm->aux) { ++ obj = pvr_gem_object_create(apple->pvr_dev, S8003_PM_AUX_SIZE, ++ DRM_PVR_BO_PM_FW_PROTECT); ++ if (IS_ERR(obj)) { ++ err = PTR_ERR(obj); ++ goto out; ++ } ++ map = pvr_gem_object_vmap(obj); ++ if (IS_ERR(map)) { ++ err = PTR_ERR(map); ++ pvr_gem_object_put(obj); ++ goto out; ++ } ++ err = pvr_vm_map_reserved(vm->vm_ctx, obj, s8003_pm_aux_va(pm), S8003_PM_AUX_SIZE); ++ if (err) { ++ pvr_gem_object_vunmap(obj); ++ pvr_gem_object_put(obj); ++ goto out; ++ } ++ mutex_lock(&apple->submit_lock); ++ pm->aux = obj; ++ pm->aux_map = map; ++ mutex_unlock(&apple->submit_lock); ++ } ++ mutex_lock(&apple->submit_lock); ++ need_pool = max(pm->blocks, pm->next_blocks) < blocks; ++ if (!need_pool) ++ s8003_pm_mark_ready(s, pm); ++ mutex_unlock(&apple->submit_lock); ++ if (!need_pool) ++ continue; ++ obj = pvr_gem_object_create(apple->pvr_dev, (size_t)blocks * S8003_PM_BLOCK_SIZE, ++ PVR_BO_FW_FLAGS_DEVICE_CACHED); ++ if (IS_ERR(obj)) { ++ err = PTR_ERR(obj); ++ goto out; ++ } ++ map = pvr_gem_object_vmap(obj); ++ if (IS_ERR(map)) { ++ err = PTR_ERR(map); ++ pvr_gem_object_put(obj); ++ goto out; ++ } ++ mutex_lock(&apple->submit_lock); ++ if (pm->next_pool) { ++ pvr_gem_object_vunmap(pm->next_pool); ++ pvr_gem_object_put(pm->next_pool); ++ } ++ pm->next_pool = obj; ++ pm->next_pool_map = map; ++ pm->next_blocks = blocks; ++ s8003_pm_mark_ready(s, pm); ++ mutex_unlock(&apple->submit_lock); ++ } ++ err = 0; ++out: ++ mutex_unlock(&vm->prepare_lock); ++ return err; ++} ++ + static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, + const char *engine, int err, const char *reason) + { +@@ -2151,14 +2272,15 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + 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])) ++ if (vm->pm[i].set >= 0 && vm->pm[i].ready && ++ s8003_pm_idle(apple, &vm->pm[i])) + pm = &vm->pm[i]; + /* + * Each holds a scene still: one whose fragment job never came (a + * geometry job alone) is given up once its geometry job is done. + */ + for (i = 0; i < 2 && !pm; i++) +- if (vm->pm[i].set >= 0 && !vm->pm[i].td_seq && ++ if (vm->pm[i].set >= 0 && vm->pm[i].ready && !vm->pm[i].td_seq && + !s8003_wait_done(apple, APPLE_ENGINE_GEOM, vm->pm[i].ta_seq)) + pm = &vm->pm[i]; + if (!pm) +@@ -2612,6 +2734,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .fini = pvr_apple_s8003_fini, + .restore_image = pvr_apple_s8003_restore_image, + .report_hang = pvr_apple_s8003_report_hang, ++ .prepare_job = s8003_prepare_job, + .submit = pvr_apple_s8003_submit, + .engine_done = pvr_apple_s8003_engine_done, + .events = pvr_apple_s8003_events, +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index b4c26ee..0d9b9aa 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -776,6 +776,13 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + goto out_free; + + jobs_alloced = args->jobs.count; ++ if (pvr_device_is_apple(pvr_dev)) { ++ for (u32 i = 0; i < jobs_alloced; i++) { ++ err = pvr_apple_job_prepare_resources(job_data[i].job); ++ if (err) ++ goto out_job_data_cleanup; ++ } ++ } + + drm_exec_init(&exec, DRM_EXEC_INTERRUPTIBLE_WAIT | DRM_EXEC_IGNORE_DUPLICATES, 0); + 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 22/49] 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 2e80e37123955a539389b38e3e4743037c66078c Mon Sep 17 00:00:00 2001 From: brkosebay Date: Mon, 16 Nov 2026 05:15:20 +0100 Subject: [PATCH 23/49] kernel: Require Apple chips to opt in to GPU BO eviction Keep S8003 eviction and require a chip hook before reclaim on other Apple GPUs. T8010 remains excluded until its pressure test is validated; non-Apple PowerVR retains the common default. Affects: Apple A9 (S8003); other Apple chips opt in per chip Assisted-by: LLM --- ...Evict-idle-BOs-under-memory-pressure.patch | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch index c06404de..9b62d6ae 100644 --- a/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch +++ b/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch @@ -29,30 +29,33 @@ on every device. Apple scans publish RAM-only tags directly in reclaim. Expose controls and resident/evicted bytes, counters and skip reasons in gem_evict; writing a MiB limit requests one pass. No swap, no eviction. -Affects: every PowerVR device +Apple eviction requires a per-chip vm_evictable hook. Keep the S8003 +hook; chips without one, including T8010, cannot evict until their owner +validates the A10 pressure test. Non-Apple PowerVR keeps its common default. + +Affects: Apple A9 (S8003); other Apple chips opt in per chip Assisted-by: LLM --- - drivers/gpu/drm/imagination/Makefile | 1 + - drivers/gpu/drm/imagination/pvr_apple.c | 13 + - drivers/gpu/drm/imagination/pvr_apple.h | 7 + - drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 30 +++ - drivers/gpu/drm/imagination/pvr_debugfs.c | 3 + - drivers/gpu/drm/imagination/pvr_device.c | 7 + - drivers/gpu/drm/imagination/pvr_device.h | 17 ++ - drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++- - drivers/gpu/drm/imagination/pvr_gem.h | 11 + - .../gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++ - drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ - drivers/gpu/drm/imagination/pvr_mmu.h | 2 + - drivers/gpu/drm/imagination/pvr_vm.c | 100 ++++++++ - drivers/gpu/drm/imagination/pvr_vm.h | 2 + + drivers/gpu/drm/imagination/Makefile | 1 + drivers/gpu/drm/imagination/pvr_apple.c | 13 + + drivers/gpu/drm/imagination/pvr_apple.h | 7 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 30 +++ + drivers/gpu/drm/imagination/pvr_debugfs.c | 3 + drivers/gpu/drm/imagination/pvr_device.c | 7 + + drivers/gpu/drm/imagination/pvr_device.h | 17 ++ + drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++++- + drivers/gpu/drm/imagination/pvr_gem.h | 11 + + drivers/gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ + drivers/gpu/drm/imagination/pvr_mmu.h | 2 + drivers/gpu/drm/imagination/pvr_vm.c | 100 +++++++++++ + drivers/gpu/drm/imagination/pvr_vm.h | 2 15 files changed, 510 insertions(+), 6 deletions(-) - create mode 100644 drivers/gpu/drm/imagination/pvr_gem_shrinker.c diff --git a/drivers/gpu/drm/imagination/Makefile b/drivers/gpu/drm/imagination/Makefile -index 08181ce..2e250a4 100644 +index 08181ce8df32cbd22de6e83253afd72268c51772..2e250a4cc7cc8fa6f0ffd7bb5c133b8e860d1aae 100644 --- a/drivers/gpu/drm/imagination/Makefile +++ b/drivers/gpu/drm/imagination/Makefile @@ -18,6 +18,7 @@ powervr-y := \ @@ -64,7 +67,7 @@ index 08181ce..2e250a4 100644 pvr_job.o \ pvr_mmu.o \ diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 5a43e3f..d062461 100644 +index 5a43e3fde89dff351c9427a19884aefb2de8c926..d0e0c13029168d468df08d7293cd25d72c0d3dce 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.c +++ b/drivers/gpu/drm/imagination/pvr_apple.c @@ -1666,3 +1666,16 @@ u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type t @@ -80,12 +83,12 @@ index 5a43e3f..d062461 100644 + + if (!mutex_trylock(&apple->submit_lock)) + return false; -+ allowed = !apple->chip->vm_evictable || apple->chip->vm_evictable(apple, vm_ctx); ++ allowed = apple->chip->vm_evictable && apple->chip->vm_evictable(apple, vm_ctx); + mutex_unlock(&apple->submit_lock); + return allowed; +} diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h -index d1de0ad..fe3c7a5 100644 +index d1de0ad773238914ab4e50e6881ec5141e81389c..fe3c7a5556a61e5d54e41a4ad36bb644fcdd06c4 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.h +++ b/drivers/gpu/drm/imagination/pvr_apple.h @@ -179,6 +179,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); @@ -110,7 +113,7 @@ index d1de0ad..fe3c7a5 100644 { return -ENODEV; diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index d524415..1cec40e 100644 +index d524415bd771047a2f06398dc51ef849666234e1..1cec40e1daf4040d38924c6c7465570861153946 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_chip.h +++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h @@ -271,6 +271,7 @@ struct pvr_apple_chip { @@ -122,7 +125,7 @@ index d524415..1cec40e 100644 void (*mmu_flush)(struct pvr_apple *apple); }; diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index bc6f300..2389178 100644 +index bc6f30096419b0196eb1930450b9f6942ba252a7..238917843acf8f04e86d511efc739c0888bb8fa3 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c @@ -261,6 +261,7 @@ struct s8003_vm { @@ -190,7 +193,7 @@ index bc6f300..2389178 100644 .mmu_flush = pvr_apple_s8003_mmu_flush, }; diff --git a/drivers/gpu/drm/imagination/pvr_debugfs.c b/drivers/gpu/drm/imagination/pvr_debugfs.c -index ebdb05d..cc96631 100644 +index ebdb05de4072856ee95e68d6349725ee0e8cacb6..cc966315682bc8695227c31710189f773fe9e38b 100644 --- a/drivers/gpu/drm/imagination/pvr_debugfs.c +++ b/drivers/gpu/drm/imagination/pvr_debugfs.c @@ -4,6 +4,7 @@ @@ -211,7 +214,7 @@ index ebdb05d..cc96631 100644 const struct pvr_debugfs_entry *entry = &pvr_debugfs_entries[i]; struct dentry *dir; diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c -index 2c06a4a..8c1bb33 100644 +index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..8c1bb336d3bc64273c02fee796ef785a422f6792 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -3,6 +3,7 @@ @@ -243,7 +246,7 @@ index 2c06a4a..8c1bb33 100644 * Deinitialization stages are performed in reverse order compared to * the initialization stages in pvr_device_init(). diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h -index 26b589e..0157190 100644 +index 26b589ee72b9c261fbb10803fd3ddd9999e95f8c..015719016a56934d59feecb22605a85ec7ac4ba8 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -11,6 +11,8 @@ @@ -278,7 +281,7 @@ index 26b589e..0157190 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 0129c8d..cf061a5 100644 +index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76df817a005 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c @@ -18,6 +18,7 @@ @@ -424,7 +427,7 @@ index 0129c8d..cf061a5 100644 + drm_gem_lru_remove(obj); +} diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index 07bd602..c21b8e7 100644 +index 07bd602e9307e80df80b350ff1b9061757ec3ccd..c21b8e77ee01dee3dd7eae19398b49959979f66c 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h @@ -111,6 +111,11 @@ struct pvr_gem_object { @@ -454,7 +457,7 @@ index 07bd602..c21b8e7 100644 int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, diff --git a/drivers/gpu/drm/imagination/pvr_gem_shrinker.c b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c new file mode 100644 -index 0000000..c43b558 +index 0000000000000000000000000000000000000000..c43b5586573819d30b3ce450c371acac00472f44 --- /dev/null +++ b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c @@ -0,0 +1,223 @@ @@ -682,7 +685,7 @@ index 0000000..c43b558 + return drmm_add_action_or_reset(&dev->base, pvr_gem_shrinker_release, dev); +} diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c -index a4996b9..7f522fd 100644 +index a4996b99784a630ec1d8b3db3ab5a46e044cb6ea..7f522fd3755328c8f71ff31c7d0b5654814d7ff1 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.c +++ b/drivers/gpu/drm/imagination/pvr_mmu.c @@ -2654,3 +2654,21 @@ int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, @@ -708,7 +711,7 @@ index a4996b9..7f522fd 100644 + return pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true); +} diff --git a/drivers/gpu/drm/imagination/pvr_mmu.h b/drivers/gpu/drm/imagination/pvr_mmu.h -index a8ecd46..a203336 100644 +index a8ecd460168dcd417f4369548f0757999e411a79..a203336a7b14e631fa418277373f39a6276264f4 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.h +++ b/drivers/gpu/drm/imagination/pvr_mmu.h @@ -103,6 +103,8 @@ pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, @@ -721,7 +724,7 @@ index a8ecd46..a203336 100644 #endif /* PVR_MMU_H */ diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c -index 4988a62..b398d9e 100644 +index 4988a62d0c7a27c5b053c565fc3317fd673278cb..b398d9e658b52c1a640fb91d7d7ffb37cabf2f21 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -131,6 +131,9 @@ struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx) @@ -836,7 +839,7 @@ index 4988a62..b398d9e 100644 + return success; +} diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h -index be5c99c..3270743 100644 +index be5c99cc72e398416b3f434ed6eacd94179f572b..3270743f609ffdb68a495cb27a1674193fe8b06e 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.h +++ b/drivers/gpu/drm/imagination/pvr_vm.h @@ -66,6 +66,8 @@ void pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx); From de66cfafb704ee6f60d70276a6d1820c8adfa76a Mon Sep 17 00:00:00 2001 From: brkosebay Date: Mon, 16 Nov 2026 05:15:20 +0100 Subject: [PATCH 24/49] 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 96d29e5bbeef06a00d5f4668adaa2963e3493f96 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 25/49] kernel: Renumber eviction patches after the S8003 slot load fix Keep the branch based on the updated A9 render series. Assisted-by: LLM --- ...ch => 0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch} | 0 ...35-drm-imagination-Take-userspace-BO-pages-on-first-use.patch} | 0 ...036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch} | 0 ...37-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch} | 0 ...38-drm-imagination-Restore-evicted-VM-mappings-before-a.patch} | 0 ...39-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch} | 0 6 files changed, 0 insertions(+), 0 deletions(-) rename kernel/patches/drivers/gpu/{0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch => 0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch} (100%) rename kernel/patches/drivers/gpu/{0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch => 0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch} (100%) rename kernel/patches/drivers/gpu/{0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch => 0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch} (100%) rename kernel/patches/drivers/gpu/{0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch => 0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch} (100%) rename kernel/patches/drivers/gpu/{0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch => 0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch} (100%) rename kernel/patches/drivers/gpu/{0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch => 0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch} (100%) diff --git a/kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch similarity index 100% rename from kernel/patches/drivers/gpu/0033-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch rename to kernel/patches/drivers/gpu/0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch b/kernel/patches/drivers/gpu/0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch similarity index 100% rename from kernel/patches/drivers/gpu/0034-drm-imagination-Take-userspace-BO-pages-on-first-use.patch rename to kernel/patches/drivers/gpu/0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch b/kernel/patches/drivers/gpu/0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch similarity index 100% rename from kernel/patches/drivers/gpu/0035-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch rename to kernel/patches/drivers/gpu/0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch similarity index 100% rename from kernel/patches/drivers/gpu/0036-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch rename to kernel/patches/drivers/gpu/0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch b/kernel/patches/drivers/gpu/0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch similarity index 100% rename from kernel/patches/drivers/gpu/0037-drm-imagination-Restore-evicted-VM-mappings-before-a.patch rename to kernel/patches/drivers/gpu/0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch diff --git a/kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch b/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch similarity index 100% rename from kernel/patches/drivers/gpu/0038-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch rename to kernel/patches/drivers/gpu/0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch 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 26/49] 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 27/49] 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 441dd2360b9a67e1c95df555966b1d333dcdb560 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:45:47 +0100 Subject: [PATCH 28/49] drm/imagination: apple: Signal completed jobs before touching S8003 PM memory The S8003 event callback reads firmware and PM cache lines before completed job fences are signalled. That host work delays the next scheduled job even though completion stamps are already visible through coherent mappings. Signal the fences and retire their runtime-PM references first, then run the chip event callback. Keep the A10 event acknowledgement before its retirement-watermark check, as before. No submission dependency changes. Affects: Apple A9 (S8003). The A10 acknowledgement order is unchanged. Assisted-by: LLM --- ...jobs-before-touching-S8003-PM-memory.patch | 48 +++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch new file mode 100644 index 00000000..3b92d31e --- /dev/null +++ b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch @@ -0,0 +1,48 @@ +From a6c0ea12f1712e9c17a642867025f7612fcef9cd Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:45:06 +0100 +Subject: [PATCH] drm/imagination: apple: Signal completed jobs before touching + S8003 PM memory + +The S8003 event callback reads firmware and PM cache lines before completed +job fences are signalled. That host work delays the next scheduled job even +though completion stamps are already visible through coherent mappings. + +Signal the fences and retire their runtime-PM references first, then run +the chip event callback. Keep the A10 event acknowledgement before its +retirement-watermark check, as before. No submission dependency changes. + +Affects: Apple A9 (S8003). The A10 acknowledgement order is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 9 ++++++--- + 1 file changed, 6 insertions(+), 3 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 5a43e3f..6c72c88 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -759,9 +759,7 @@ static void pvr_apple_poll_work(struct work_struct *work) + bool pending = false; + int engine; + +- if (apple->chip->events) +- apple->chip->events(apple); +- else ++ if (!apple->chip->events) + pvr_apple_ack_events(apple); + spin_lock_irq(&apple->fence_lock); + for (engine = 0; engine < APPLE_ENGINES; engine++) { +@@ -795,6 +793,11 @@ static void pvr_apple_poll_work(struct work_struct *work) + } + spin_unlock_irq(&apple->fence_lock); + pvr_apple_jobs_retired(apple, retired); ++ /* Completion stamps need no touch. Signal first, then let S8003 ++ * touch and drain its rings. A10 acknowledgement above is unchanged. ++ */ ++ if (apple->chip->events) ++ apple->chip->events(apple); + + /* The firmware sends no completion message in this setup: poll. */ + if (pending) 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 29/49] 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 30/49] 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 31/49] 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 32/49] 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 33/49] 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 37fbf0c05e526c53ac93e18bf0c5f1bc8dc80786 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:46:27 +0100 Subject: [PATCH 34/49] drm/imagination: apple: Report firmware-measured S8003 GPU time in debugfs Host in-flight durations include queueing and firmware overhead, so they cannot show how long the GPU geometry and fragment phases actually ran. Read the retired phase-time u64 at W+0x550 or W+0x8ac before the record is cleared. Both submission paths wait for last_seq - 1 before writing the two-slot ring's next record, so its previous occupant has retired. Order that shared-memory read with dma_rmb(), preserve it across the fill, and account only successful fills before the result word is cleared. Expose accumulated geometry and fragment time, jobs and zero samples in apple_jobs, converting 24 MHz ticks by ticks * 125 / 3. Add only the small chip debugfs callback needed by that line; no timing parameters, phase-gap attribution or memory census. Submission ordering and power policy stay unchanged. Affects: Apple A9 (S8003), measurement only. The A10 is unchanged. Assisted-by: LLM --- ...e-measured-S8003-GPU-time-in-debugfs.patch | 275 ++++++++++++++++++ 1 file changed, 275 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch new file mode 100644 index 00000000..af6bbd39 --- /dev/null +++ b/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch @@ -0,0 +1,275 @@ +From 3a19f9211c0ef2ae6af305c792cfc555e573f58d Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:46:27 +0100 +Subject: [PATCH] drm/imagination: apple: Report firmware-measured S8003 GPU + time in debugfs + +Host in-flight durations include queueing and firmware overhead, so they +cannot show how long the GPU geometry and fragment phases actually ran. + +Read the retired phase-time u64 at W+0x550 or W+0x8ac before the record is +cleared. Both submission paths wait for last_seq - 1 before writing the +two-slot ring's next record, so its previous occupant has retired. Order +that shared-memory read with dma_rmb(), preserve it across the fill, and +account only successful fills before the result word is cleared. + +Expose accumulated geometry and fragment time, jobs and zero samples in +apple_jobs, converting 24 MHz ticks by ticks * 125 / 3. Add only the small +chip debugfs callback needed by that line; no timing parameters, phase-gap +attribution or memory census. Submission ordering and power policy stay +unchanged. + +Affects: Apple A9 (S8003), measurement only. The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 19 +++++++ + drivers/gpu/drm/imagination/pvr_apple.h | 3 ++ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 3 ++ + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 53 ++++++++++++++++--- + drivers/gpu/drm/imagination/pvr_debugfs.c | 3 ++ + 5 files changed, 75 insertions(+), 6 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 5a43e3f..964549a 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -31,6 +31,8 @@ + + #include + #include ++#include ++#include + #include + #include + #include +@@ -579,6 +581,23 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine + return heap_read32(apple, apple->layout.stamp[engine]); + } + ++static int pvr_apple_jobs_show(struct seq_file *s, void *unused) ++{ ++ struct pvr_apple *apple = s->private; ++ ++ 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) ++{ ++ struct pvr_apple *apple = to_apple(pvr_dev); ++ ++ if (apple && apple->chip->debugfs_show) ++ debugfs_create_file("apple_jobs", 0400, root, apple, &pvr_apple_jobs_fops); ++} ++ + /* 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) + { +diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h +index d1de0ad..24e3ec2 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); +@@ -191,6 +193,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 d524415..1527248 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -28,6 +28,7 @@ + #include + + struct devfreq; ++struct seq_file; + struct pvr_gem_object; + struct pvr_stream_cmd_defs; + +@@ -273,6 +274,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); ++ /* Chip-specific measurements in apple_jobs; no MMIO. */ ++ void (*debugfs_show)(struct pvr_apple *apple, struct seq_file *m); + }; + + /* The heap is coherent memory shared with the firmware, not MMIO. */ +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 40ca888..0f69c5a 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -34,6 +34,9 @@ + * readings of Apple's kext and firmware. + */ + ++#include ++#include ++ + #include "pvr_apple.h" + #include "pvr_apple_chip.h" + #include "pvr_context.h" +@@ -307,6 +310,8 @@ struct pvr_apple_s8003 { + struct pvr_gem_object *spare; + bool pio_mapped; + ++ u64 gpu_ticks[2], gpu_jobs[2], gpu_zero[2]; /* retired geometry/fragment phase time */ ++ + struct s8003_idle idle; + }; + +@@ -1958,6 +1963,19 @@ static struct s8003_job_slot s8003_slot_of(u32 seq, bool frag) + }; + } + ++/* ++ * Submissions wait for last_seq - 1 before reusing either of the two records: ++ * the prior occupant of s8003_slot_of(last_seq + 1) is last_seq - 1. Preserve ++ * its result before fill_ta/fill_3d clears W. Failed fills do not count it. ++ */ ++static u64 s8003_job_ticks(u8 *p, u32 seq, bool frag) ++{ ++ struct s8003_job_slot at = s8003_slot_of(seq, frag); ++ ++ dma_rmb(); ++ return get_unaligned_le64(p + at.w + (frag ? 0x8ac : 0x550)); ++} ++ + /* What the 3D engine is waited for between a fragment job's start and finish */ + static const u32 s8003_3d_wait[] = { + 0x201, 0x138204, 0x10, 0x8388204, 0, 0x104000a, 0x1f, +@@ -1983,8 +2001,8 @@ static u8 *useq_put(u8 *out, const u32 *words, unsigned int n) + * the PM 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) ++static void s8003_job_write(struct pvr_apple_s8003 *s, u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 slot, bool frag, ++ u32 seq, u32 wait_stamp, u32 wait_seq, u64 gpu_ticks) + { + struct s8003_job_slot at = s8003_slot_of(seq, frag); + u32 w = addr + at.w, record = pm + S8003_PM_RECORD; +@@ -1997,6 +2015,13 @@ static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 + const u32 time_end[] = { 0x18, result, time, time + 4, seq }; + const u32 wait_ta = 0x801, end = 0x40000017; + ++ if (gpu_ticks) { ++ s->gpu_ticks[frag] += gpu_ticks; ++ s->gpu_jobs[frag]++; ++ } else { ++ s->gpu_zero[frag]++; ++ } ++ + put32(work + (frag ? 0x8ac : 0x550), 0); + put32(work + (frag ? 0x8b0 : 0x554), 0); + put32(work + (frag ? 0x8b4 : 0x558), times); +@@ -2145,6 +2170,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; ++ u64 gpu_ticks; + unsigned int i; + int err; + +@@ -2179,11 +2205,12 @@ 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; ++ gpu_ticks = s8003_job_ticks(p, seq, false); + s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); +- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_GEOM), ++ s8003_job_write(s, 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, gpu_ticks); + pm->hwrt = job->hwrt; + pm->scene = s->gen; + pm->ta_seq = seq; +@@ -2203,6 +2230,7 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + struct s8003_rt rt; + unsigned int i; + u32 seq; ++ u64 gpu_ticks; + int err; + + /* The scene of this job's geometry job, which was submitted before it */ +@@ -2221,13 +2249,14 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc + return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); + + seq = apple->frag_seq + 1; ++ gpu_ticks = s8003_job_ticks(p, seq, true); + err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); + if (err) + return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); +- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_FRAG), ++ s8003_job_write(s, 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, gpu_ticks); + pm->td_seq = seq; + apple->frag_seq = seq; + err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); +@@ -2589,7 +2618,19 @@ static void pvr_apple_s8003_restore_image(struct pvr_apple *apple) + /* start() copies the image and builds the init data anew each time */ + } + ++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, "gpu_ns geometry %llu fragment %llu jobs %llu %llu zero %llu %llu\n", ++ mul_u64_u32_div(READ_ONCE(s->gpu_ticks[0]), 125, 3), ++ mul_u64_u32_div(READ_ONCE(s->gpu_ticks[1]), 125, 3), ++ READ_ONCE(s->gpu_jobs[0]), READ_ONCE(s->gpu_jobs[1]), ++ READ_ONCE(s->gpu_zero[0]), READ_ONCE(s->gpu_zero[1])); ++} ++ + const struct pvr_apple_chip pvr_apple_chip_s8003 = { ++ .debugfs_show = pvr_apple_s8003_debugfs_show, + .fw_name = "apple/gfx-s8003.bin", + .perf_state_max = S8003_PERF_STATE_MAX, + .geom_stream = &pvr_apple_g5p_cmd_geom_stream, +diff --git a/drivers/gpu/drm/imagination/pvr_debugfs.c b/drivers/gpu/drm/imagination/pvr_debugfs.c +index ebdb05d..fce6cdf 100644 +--- a/drivers/gpu/drm/imagination/pvr_debugfs.c ++++ b/drivers/gpu/drm/imagination/pvr_debugfs.c +@@ -2,6 +2,7 @@ + /* Copyright (c) 2023 Imagination Technologies Ltd. */ + + #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; From 41bb9af6fa060417996b92a999bdb682843814e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:45:47 +0100 Subject: [PATCH 35/49] drm/imagination: apple: Allow opt-in per-VM S8003 scene overlap Composited S8003 workloads serialize one client's geometry behind another client's fragment, even though their PM sets are private to their VMs. Keep each VM's last two fragment fences and add apple_vm_overlap, default off. When enabled on a chip with per-VM scenes, geometry waits on its own VM's previous fragment (or last fragment for one scene). Keep the global scene dependencies with the option off and for chips without per-VM sets. Reference and release the per-VM history with the existing VM lifetime. Affects: Apple A9 (S8003). T8010 has no per-VM scene hook and keeps its global dependencies regardless of the parameter. Assisted-by: LLM --- ...ow-opt-in-per-VM-S8003-scene-overlap.patch | 194 ++++++++++++++++++ 1 file changed, 194 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch b/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch new file mode 100644 index 00000000..ebfe1d2d --- /dev/null +++ b/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch @@ -0,0 +1,194 @@ +From bbd8003115717b35d347c9f40a8807ba71e9e8ba Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:45:47 +0100 +Subject: [PATCH] drm/imagination: apple: Allow opt-in per-VM S8003 scene + overlap + +Composited S8003 workloads serialize one client's geometry behind another +client's fragment, even though their PM sets are private to their VMs. + +Keep each VM's last two fragment fences and add apple_vm_overlap, default +off. When enabled on a chip with per-VM scenes, geometry waits on its own +VM's previous fragment (or last fragment for one scene). Keep the global +scene dependencies with the option off and for chips without per-VM sets. +Reference and release the per-VM history with the existing VM lifetime. + +Affects: Apple A9 (S8003). T8010 has no per-VM scene hook and keeps its +global dependencies regardless of the parameter. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 61 +++++++++++++++++++- + drivers/gpu/drm/imagination/pvr_apple_chip.h | 10 +++- + 2 files changed, 67 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 6c72c88..bcfc2e4 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -64,6 +64,10 @@ MODULE_PARM_DESC(apple_poll_us, "Apple A10: microseconds between job completion + /* Idle time before runtime PM powers the GPU off; tunable in sysfs. */ + #define PVR_APPLE_AUTOSUSPEND_MS 1000 + ++static bool pvr_apple_vm_overlap; ++module_param_named(apple_vm_overlap, pvr_apple_vm_overlap, bool, 0644); ++MODULE_PARM_DESC(apple_vm_overlap, "Apple per-VM scene overlap (default 0)"); ++ + static unsigned int pvr_apple_perf_state; + module_param_named(apple_perf_state, pvr_apple_perf_state, uint, 0444); + MODULE_PARM_DESC(apple_perf_state, +@@ -325,6 +329,7 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) + spin_lock_init(&apple->event_lock); + for (i = 0; i < APPLE_ENGINES; i++) + INIT_LIST_HEAD(&apple->pending[i]); ++ xa_init(&apple->vm_frags); + INIT_DELAYED_WORK(&apple->poll_work, pvr_apple_poll_work); + hrtimer_setup(&apple->poll_timer, pvr_apple_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + apple->fence_context = dma_fence_context_alloc(APPLE_ENGINES); +@@ -850,12 +855,23 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) + /* No job is ordered after the scenes submitted so far any more */ + static void pvr_apple_forget_scenes(struct pvr_apple *apple) + { ++ struct pvr_apple_vm_frags *frags; ++ unsigned long index; ++ ++ spin_lock_irq(&apple->fence_lock); ++ xa_for_each(&apple->vm_frags, index, frags) { ++ dma_fence_put(frags->last); ++ dma_fence_put(frags->prev); ++ frags->last = NULL; ++ frags->prev = NULL; ++ } + dma_fence_put(apple->last_frag); + apple->last_frag = NULL; + dma_fence_put(apple->prev_frag); + apple->prev_frag = NULL; + dma_fence_put(apple->last_geom); + apple->last_geom = NULL; ++ spin_unlock_irq(&apple->fence_lock); + } + + static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) +@@ -1038,6 +1054,18 @@ void pvr_apple_vm_fini(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx + + if (vm_ctx == pvr_dev->kernel_vm_ctx) + return; ++ { ++ struct pvr_apple_vm_frags *frags; ++ ++ spin_lock_irq(&apple->fence_lock); ++ frags = xa_erase(&apple->vm_frags, (unsigned long)vm_ctx); ++ spin_unlock_irq(&apple->fence_lock); ++ if (frags) { ++ dma_fence_put(frags->last); ++ dma_fence_put(frags->prev); ++ kfree(frags); ++ } ++ } + if (apple->chip->vm_fini) + apple->chip->vm_fini(apple, vm_ctx); + if (!apple->booted) +@@ -1224,7 +1252,9 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) + * &pvr_apple_chip.vm_scenes): a geometry job then waits for the fragment job + * before the last one if the last one is its own VM's, and a fragment job + * for its geometry job to be submitted rather than finished +- * (pvr_apple_job_armed()). ++ * (pvr_apple_job_armed()). With apple_vm_overlap on, a chip with per-VM ++ * scenes takes only this VM's previous fragment (or last for one scene), ++ * as each VM owns its PM sets; the global timelines remain for A10/knob 0. + * + * Return: 0 or a negative error. + */ +@@ -1240,9 +1270,14 @@ int pvr_apple_job_add_deps(struct pvr_job *job) + /* 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) ++ 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 && ++ } else if (apple->chip->vm_scenes && READ_ONCE(pvr_apple_vm_overlap)) { ++ struct pvr_apple_vm_frags *frags = xa_load(&apple->vm_frags, ++ (unsigned long)job->ctx->vm_ctx); ++ ++ last = frags ? (scenes > 1 ? frags->prev : frags->last) : NULL; ++ } else if (apple->chip->scenes > 1 && apple->last_frag_vm == job->ctx->vm_ctx && + scenes > 1) + last = apple->prev_frag; + else +@@ -1276,6 +1311,14 @@ void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished) + old = *last; + *last = dma_fence_get(finished); + if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) { ++ struct pvr_apple_vm_frags *frags = xa_load(&apple->vm_frags, ++ (unsigned long)job->ctx->vm_ctx); ++ ++ if (frags) { ++ dma_fence_put(frags->prev); ++ frags->prev = frags->last; ++ frags->last = dma_fence_get(finished); ++ } + apple->last_frag_vm = job->ctx->vm_ctx; + if (joint) + swap(old, apple->prev_frag); +@@ -1300,6 +1343,18 @@ int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) + struct pvr_apple *apple = to_apple(pvr_dev); + int err; + ++ if (apple->chip->vm_scenes) { ++ struct pvr_apple_vm_frags *frags = kzalloc(sizeof(*frags), GFP_KERNEL); ++ ++ if (!frags) ++ return -ENOMEM; ++ err = xa_err(xa_store(&apple->vm_frags, (unsigned long)vm_ctx, ++ frags, GFP_KERNEL)); ++ if (err) { ++ kfree(frags); ++ return err; ++ } ++ } + err = pvr_vm_map_reserved(vm_ctx, apple->data.obj, PVR_APPLE_GPU_DATA, + PVR_DEVICE_PAGE_SIZE); + if (!err) +diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index d524415..2ef5208 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -26,6 +26,7 @@ + #include + #include + #include ++#include + + struct devfreq; + struct pvr_gem_object; +@@ -74,6 +75,10 @@ struct pvr_apple_layout { + u32 event_size; + }; + ++struct pvr_apple_vm_frags { ++ struct dma_fence *last, *prev; ++}; ++ + struct pvr_apple { + struct pvr_device *pvr_dev; + const struct pvr_apple_chip *chip; +@@ -120,6 +125,7 @@ struct pvr_apple { + * Chips with &pvr_apple_chip.scenes of 2: the fragment job before + * @last_frag, and the VM @last_frag runs in. + */ ++ struct xarray vm_frags; /* per-VM last/previous fragment fences, fence_lock */ + struct dma_fence *prev_frag; + const struct pvr_vm_context *last_frag_vm; + +@@ -208,7 +214,9 @@ struct pvr_apple { + * @scenes: Scenes (a geometry job and its fragment job) one VM may have in the + * firmware at a time; 0 is 1. Above 1 a fragment job is submitted as soon + * as its geometry job was, not when that finished: the firmware orders +- * the two. Scenes of different VMs never overlap. ++ * the two. With @vm_scenes, scenes of different VMs may overlap: each ++ * VM owns its PM sets. AGXWorkQueue_initAndAttachPM has one PM per queue; ++ * AGXTACommandDescriptor_preparePMAndWork waits only on that PM's stamps. + * @own_pm_pages: The chip allocates the parameter manager's pages itself; a + * free list has none (pvr_apple_free_list_has_pages()). + * @gate_off_ms: How long the GPU's power domain stays off before the firmware 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 36/49] 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; From 71e26e902e872b5e4c21f66a84fd2574e2c7acf6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:50:15 +0100 Subject: [PATCH 37/49] drm/imagination: apple: Wait only on the last S8003 PM rebuild in steady state The clean-set path waits on the immediately preceding geometry job. When that job is still running it falls back to a full PM record, rebuilding the free list even though this set has not changed. Follow AGXTACommandDescriptor_preparePMAndWork: remember the geometry job that last carried each set's PM record and wait only on that stamp for a clean set. A newer ordinary geometry job does not force a rebuild. Reset the remembered stamp when backing is rebuilt or firmware restarts, and fall back to a record if the rebuild stamp has not been reached. Make this the single behavior. No always-rebuild or legacy-wait mode and no module parameter are added. Keep the existing clean-set bookkeeping in the prerequisite series; report clean queued and unreached stamps. Affects: Apple A9 (S8003). The A10 is unchanged. Assisted-by: LLM --- ...ast-S8003-PM-rebuild-in-steady-state.patch | 167 ++++++++++++++++++ 1 file changed, 167 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0049-drm-imagination-apple-Wait-only-on-the-last-S8003-PM-rebuild-in-steady-state.patch diff --git a/kernel/patches/drivers/gpu/0049-drm-imagination-apple-Wait-only-on-the-last-S8003-PM-rebuild-in-steady-state.patch b/kernel/patches/drivers/gpu/0049-drm-imagination-apple-Wait-only-on-the-last-S8003-PM-rebuild-in-steady-state.patch new file mode 100644 index 00000000..536a502a --- /dev/null +++ b/kernel/patches/drivers/gpu/0049-drm-imagination-apple-Wait-only-on-the-last-S8003-PM-rebuild-in-steady-state.patch @@ -0,0 +1,167 @@ +From 1201f3d62e089541a4ede68c5460b2b463ca4d0b Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 17:50:15 +0100 +Subject: [PATCH] drm/imagination: apple: Wait only on the last S8003 PM + rebuild in steady state + +The clean-set path waits on the immediately preceding geometry job. When +that job is still running it falls back to a full PM record, rebuilding +the free list even though this set has not changed. + +Follow AGXTACommandDescriptor_preparePMAndWork: remember the geometry job +that last carried each set's PM record and wait only on that stamp for a +clean set. A newer ordinary geometry job does not force a rebuild. Reset +the remembered stamp when backing is rebuilt or firmware restarts, and +fall back to a record if the rebuild stamp has not been reached. + +Make this the single behavior. No always-rebuild or legacy-wait mode and +no module parameter are added. Keep the existing clean-set bookkeeping +in the prerequisite series; report clean queued and unreached stamps. + +Affects: Apple A9 (S8003). The A10 is unchanged. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 59 +++++++++++-------- + 1 file changed, 36 insertions(+), 23 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 87b82ca..ff208b4 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -247,6 +247,7 @@ struct s8003_pm { + int set; /* of the firmware's sets; -1: none */ + u32 blocks; /* of the pool */ + u32 built; /* the firmware start it was built for */ ++ u32 rec_seq; /* last geometry job carrying its PM record */ + struct pvr_gem_object *pool; + struct pvr_gem_object *aux; /* the PM's tables, mapped in the VM */ + const struct pvr_hwrt_data *hwrt; /* the scene's target */ +@@ -308,7 +309,7 @@ struct pvr_apple_s8003 { + 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 pm_full, pm_clean, pm_clean_queued, pm_rec_unreached; + u64 fb_reads, fb_grows; + u32 fb_max_blocks; + struct pvr_vm_context *park_vm; /* maps what every VM maps, nothing else */ +@@ -1747,6 +1748,7 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s + pm->blocks = blocks; + } + pm->built = 0; ++ pm->rec_seq = 0; + err = s8003_obj_zero(pm->pool); + if (!err) + err = s8003_obj_zero(pm->aux); +@@ -2101,11 +2103,12 @@ static u8 *useq_put(u8 *out, const u32 *words, unsigned int n) + * image at the job's slot of the channel page @p: the record's links (slot, + * 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, 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. ++ * set's META address, @pm_id its number. Without @pm_record a geometry job ++ * waits for its set's last record-carrying TA, already complete. This is ++ * AGXTACommandDescriptor_preparePMAndWork (0xfffffff006cb9afc): a steady TA ++ * takes the PM stamp set at +0x40; a rebuild takes the full set. Backing is ++ * rebuilt only on a generation change (0xfffffff006cb1940). A fragment job ++ * waits at @wait_stamp for @wait_seq, its geometry job's stamp. + */ + static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 slot, bool frag, + u32 seq, u32 wait_stamp, u32 wait_seq, bool pm_record) +@@ -2192,9 +2195,9 @@ static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 + 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(cmd + 4, wait_stamp); ++ put32(cmd + 8, wait_seq); ++ put32(cmd + 12, wait_seq & 0x7fffffff); + } + put32(p + S8003_CH_RING + ring * 4, addr + at.cmd); + put32(p + S8003_CH_RING + ((ring + 1) % S8003_CH_ENTRIES) * 4, w); +@@ -2268,7 +2271,7 @@ static bool s8003_pm_clean(struct pvr_apple *apple, const struct s8003_pm *pm, u + { + struct pvr_apple_s8003 *s = apple->chip_data; + +- return pm->clean && pm->built == s->gen && pm->blocks >= blocks && ++ return pm->rec_seq && 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; + } +@@ -2399,15 +2402,16 @@ 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; +- s->pm_clean_busy++; ++ if (!pm_record) { ++ u32 done = pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_GEOM); ++ ++ /* The last rebuild must be visible; a newer ordinary TA need not be. */ ++ if ((s32)(done - pm->rec_seq) < 0) { ++ pm_record = true; ++ s->pm_rec_unreached++; ++ } else if ((s32)(done - apple->ta_seq) < 0) { ++ s->pm_clean_queued++; ++ } + } + if (pm_record) + s->pm_full++; +@@ -2417,7 +2421,10 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + 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, pm_record); ++ seq, s8003_channel_addr(APPLE_ENGINE_GEOM) + S8003_CH_STAMP + 4, ++ pm_record ? 0 : pm->rec_seq, pm_record); ++ if (pm_record) ++ pm->rec_seq = seq; + pm->hwrt = job->hwrt; + if (s->pm_last != vm) { + s->pm_prev = s->pm_last; +@@ -2624,7 +2631,9 @@ static int s8003_first_command(struct pvr_apple *apple) + /* A start of the firmware: it knows no VM, and its channels are empty */ + static void s8003_jobs_reset(struct pvr_apple_s8003 *s) + { +- unsigned long flags; ++ struct s8003_vm *vm; ++ unsigned long index, flags; ++ unsigned int i; + + spin_lock_irqsave(&s->tag_lock, flags); + memset(s->tag, 0, sizeof(s->tag)); +@@ -2632,6 +2641,9 @@ static void s8003_jobs_reset(struct pvr_apple_s8003 *s) + bitmap_zero(s->parked, S8003_SLOTS); + if (!++s->gen) + s->gen = 1; ++ xa_for_each(&s->vms, index, vm) ++ for (i = 0; i < ARRAY_SIZE(vm->pm); i++) ++ vm->pm[i].rec_seq = 0; + WRITE_ONCE(s->failed, false); + WRITE_ONCE(s->fault, false); + memset(s->set_geom, 0, sizeof(s->set_geom)); +@@ -2888,8 +2900,9 @@ static void pvr_apple_s8003_debugfs_show(struct pvr_apple *apple, struct seq_fil + + 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_record full %llu clean %llu clean_queued %llu rec_unreached %llu\n", ++ READ_ONCE(s->pm_full), READ_ONCE(s->pm_clean), ++ READ_ONCE(s->pm_clean_queued), READ_ONCE(s->pm_rec_unreached)); + 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), From 1e3d754aada777ee22e0e260805d98b30aa98cd5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 18:46:22 +0100 Subject: [PATCH 38/49] iommu/apple-dart: Defer attachment of masters left running by firmware The boot loader leaves display scanout fetching its framebuffer through a bypassed DART. Attaching an empty DMA domain under those reads can blank or corrupt the display before its driver has mapped the live addresses. Let a DMA master request apple,dart-deferred-attach and export the existing iommu_deferred_attach() helper. The display driver can populate its default domain before enabling translation. Other masters keep their attach policy. Affects: Apple display DMA masters left running by the boot loader. Assisted-by: LLM --- ...-Defer-attachment-of-running-masters.patch | 66 +++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 kernel/patches/drivers/iommu/0011-iommu-apple-dart-Defer-attachment-of-running-masters.patch diff --git a/kernel/patches/drivers/iommu/0011-iommu-apple-dart-Defer-attachment-of-running-masters.patch b/kernel/patches/drivers/iommu/0011-iommu-apple-dart-Defer-attachment-of-running-masters.patch new file mode 100644 index 00000000..5e819866 --- /dev/null +++ b/kernel/patches/drivers/iommu/0011-iommu-apple-dart-Defer-attachment-of-running-masters.patch @@ -0,0 +1,66 @@ +From 938004227cbf513cfc3802c52d3561bec0fa6b22 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 18:46:21 +0100 +Subject: [PATCH] iommu/apple-dart: Defer attachment of masters left running by + firmware + +The boot loader leaves display scanout fetching its framebuffer through a +bypassed DART. Attaching an empty DMA domain under those reads can blank +or corrupt the display before its driver has mapped the live addresses. + +Let a DMA master request apple,dart-deferred-attach and export the existing +iommu_deferred_attach() helper. The display driver can populate its default +domain before enabling translation. Other masters keep their attach policy. + +Affects: Apple display DMA masters left running by the boot loader. + +Assisted-by: LLM +--- + drivers/iommu/apple-dart.c | 14 ++++++++++++++ + drivers/iommu/iommu.c | 1 + + 2 files changed, 15 insertions(+) + +diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c +index 1ee6add..4b3ec7c 100644 +--- a/drivers/iommu/apple-dart.c ++++ b/drivers/iommu/apple-dart.c +@@ -1457,6 +1457,19 @@ out: + return res; + } + ++/* ++ * A master that the boot loader left reading memory through this DART in ++ * bypass, such as a display pipe scanning out its boot framebuffer, keeps ++ * bypass until its driver has mapped those live addresses into the default ++ * domain and calls iommu_deferred_attach(). Translation then never starts ++ * under a fetch of an unmapped address. ++ */ ++static bool apple_dart_is_attach_deferred(struct device *dev) ++{ ++ return of_property_read_bool(dev->of_node, ++ "apple,dart-deferred-attach"); ++} ++ + static int apple_dart_def_domain_type(struct device *dev) + { + struct apple_dart_master_cfg *cfg = dev_iommu_priv_get(dev); +@@ -1503,6 +1516,7 @@ static const struct iommu_ops apple_dart_iommu_ops = { + .device_group = apple_dart_device_group, + .of_xlate = apple_dart_of_xlate, + .def_domain_type = apple_dart_def_domain_type, ++ .is_attach_deferred = apple_dart_is_attach_deferred, + .get_resv_regions = apple_dart_get_resv_regions, + .owner = THIS_MODULE, + .default_domain_ops = &(const struct iommu_domain_ops) { +diff --git a/drivers/iommu/iommu.c b/drivers/iommu/iommu.c +index cd1bca7..8ba534a 100644 +--- a/drivers/iommu/iommu.c ++++ b/drivers/iommu/iommu.c +@@ -2243,6 +2243,7 @@ int iommu_deferred_attach(struct device *dev, struct iommu_domain *domain) + return -EBUSY; + return __iommu_attach_device(domain, dev, NULL); + } ++EXPORT_SYMBOL_GPL(iommu_deferred_attach); + + void iommu_detach_device(struct iommu_domain *domain, struct device *dev) + { From 61702dbe9fa5d880cf1a1785308a378bc4140a10 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 18:46:22 +0100 Subject: [PATCH 39/49] dt-bindings: display: Describe the Apple scanout DART and SMMU The display fetch path has a DART and a second SMMU front end. Describe both named register windows and fault interrupts for the S5L8960X layout, and the display pipe's optional deferred-attach scanout DMA master. Existing single-window IOMMUs and display pipes remain valid. Affects: Apple S5L8960X display DART and scanout device trees. Assisted-by: LLM --- ...Describe-Apple-scanout-DART-and-SMMU.patch | 140 ++++++++++++++++++ 1 file changed, 140 insertions(+) create mode 100644 kernel/patches/drivers/iommu/0012-dt-bindings-display-Describe-Apple-scanout-DART-and-SMMU.patch diff --git a/kernel/patches/drivers/iommu/0012-dt-bindings-display-Describe-Apple-scanout-DART-and-SMMU.patch b/kernel/patches/drivers/iommu/0012-dt-bindings-display-Describe-Apple-scanout-DART-and-SMMU.patch new file mode 100644 index 00000000..23d4c19b --- /dev/null +++ b/kernel/patches/drivers/iommu/0012-dt-bindings-display-Describe-Apple-scanout-DART-and-SMMU.patch @@ -0,0 +1,140 @@ +From d7f21f46aa3e225f902a1a8b18211976327ba14e Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 18:46:21 +0100 +Subject: [PATCH] dt-bindings: display: Describe the Apple scanout DART and + SMMU + +The display fetch path has a DART and a second SMMU front end. Describe +both named register windows and fault interrupts for the S5L8960X layout, +and the display pipe's optional deferred-attach scanout DMA master. +Existing single-window IOMMUs and display pipes remain valid. + +Affects: Apple S5L8960X display DART and scanout device trees. + +Assisted-by: LLM +--- + .../display/apple,s8000-display-pipe.yaml | 31 +++++++++-- + .../devicetree/bindings/iommu/apple,dart.yaml | 52 ++++++++++++++++++- + 2 files changed, 78 insertions(+), 5 deletions(-) + +diff --git a/Documentation/devicetree/bindings/display/apple,s8000-display-pipe.yaml b/Documentation/devicetree/bindings/display/apple,s8000-display-pipe.yaml +index 4df755c..103344b 100644 +--- a/Documentation/devicetree/bindings/display/apple,s8000-display-pipe.yaml ++++ b/Documentation/devicetree/bindings/display/apple,s8000-display-pipe.yaml +@@ -18,9 +18,11 @@ description: | + bootloader leaves the pipe, the eDP link and the panel running; the pipe + reads memory through the display DART (dart-disp0), which the bootloader + leaves in bypass (on the A10, the scanout's streams have no page tables). +- The DART has no node of its own: it must not be reset under the running +- scanout, so its two register windows are listed here and only read, to +- check that the scanout bypasses translation. ++ The pipe's two dart-disp0 register windows, the DART and the SMMU in ++ front of it, are listed here and only read, to check that the scanout ++ bypasses translation. For translated scanout the DART may also be ++ described as a disabled iommu node that the display driver enables on ++ request, referenced from a "scanout" child node. + + properties: + compatible: +@@ -46,6 +48,29 @@ properties: + minItems: 2 + maxItems: 3 + ++ scanout: ++ type: object ++ additionalProperties: false ++ description: ++ The pipe's memory reads as a DMA master behind the display DART, for ++ translated scanout. Every stream of the DART is listed. The ++ bootloader leaves the DART in bypass under a live scanout, so the ++ attach is deferred until the driver has mapped that scanout. ++ ++ properties: ++ iommus: ++ minItems: 1 ++ maxItems: 4 ++ ++ apple,dart-deferred-attach: ++ type: boolean ++ description: ++ Keep the DART in bypass for this master until its driver attaches ++ the default domain itself. ++ ++ required: ++ - iommus ++ + allOf: + - if: + properties: +diff --git a/Documentation/devicetree/bindings/iommu/apple,dart.yaml b/Documentation/devicetree/bindings/iommu/apple,dart.yaml +index df72722..9bbc29f 100644 +--- a/Documentation/devicetree/bindings/iommu/apple,dart.yaml ++++ b/Documentation/devicetree/bindings/iommu/apple,dart.yaml +@@ -41,10 +41,27 @@ properties: + - const: apple,s5l8960x-dart + + reg: +- maxItems: 1 ++ minItems: 1 ++ maxItems: 2 ++ ++ reg-names: ++ minItems: 1 ++ items: ++ - const: dart ++ - const: smmu ++ description: ++ The SMMU window of display DARTs; translation must be enabled per ++ stream in both units. Only S5L8960X display DARTs have this window. + + interrupts: +- maxItems: 1 ++ minItems: 1 ++ maxItems: 2 ++ ++ interrupt-names: ++ minItems: 1 ++ items: ++ - const: dart ++ - const: smmu + + clocks: + description: +@@ -99,6 +116,37 @@ allOf: + apple,error-reflector: false + apple,fetch-config: false + apple,diag-config: false ++ reg: ++ maxItems: 1 ++ reg-names: false ++ interrupts: ++ maxItems: 1 ++ interrupt-names: false ++ ++ - if: ++ properties: ++ reg: ++ minItems: 2 ++ then: ++ required: ++ - reg-names ++ - interrupt-names ++ properties: ++ reg-names: ++ minItems: 2 ++ interrupts: ++ minItems: 2 ++ interrupt-names: ++ minItems: 2 ++ ++ - if: ++ properties: ++ interrupts: ++ minItems: 2 ++ then: ++ properties: ++ reg: ++ minItems: 2 + + additionalProperties: false + From a8afe8a943cebde0b7a794b89db008a4e072d6c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 18:46:22 +0100 Subject: [PATCH 40/49] iommu/apple-dart: Drive the S5L8960X display SMMU window Translated display fetches can read unrelated DRAM without a DART fault when the second front end remains in bypass. Mirror stream translation in DART then SMMU order and program the second error reflector and diagnostic word, following AppleS5L8960XDART enableStream and hardware initialization. Only the primary DART owns page tables and invalidation commands. Handle both fault interrupts, expose their combined count for the display self-check, and mask each faulting line after 16 faults until translation or bypass setup rearms it. This path requires an explicitly described second window; existing single-window DARTs retain their setup. Affects: S5L8960X display DARTs with an SMMU window. Assisted-by: LLM --- ...apple-dart-Drive-display-SMMU-window.patch | 416 ++++++++++++++++++ 1 file changed, 416 insertions(+) create mode 100644 kernel/patches/drivers/iommu/0013-iommu-apple-dart-Drive-display-SMMU-window.patch diff --git a/kernel/patches/drivers/iommu/0013-iommu-apple-dart-Drive-display-SMMU-window.patch b/kernel/patches/drivers/iommu/0013-iommu-apple-dart-Drive-display-SMMU-window.patch new file mode 100644 index 00000000..a2a0b12a --- /dev/null +++ b/kernel/patches/drivers/iommu/0013-iommu-apple-dart-Drive-display-SMMU-window.patch @@ -0,0 +1,416 @@ +From fa62e7c369dc7de82c54fd27015c37980e887dcf Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 18:46:21 +0100 +Subject: [PATCH] iommu/apple-dart: Drive the S5L8960X display SMMU window + +Translated display fetches can read unrelated DRAM without a DART fault +when the second front end remains in bypass. Mirror stream translation in +DART then SMMU order and program the second error reflector and diagnostic +word, following AppleS5L8960XDART enableStream and hardware initialization. +Only the primary DART owns page tables and invalidation commands. + +Handle both fault interrupts, expose their combined count for the display +self-check, and mask each faulting line after 16 faults until translation +or bypass setup rearms it. This path requires an explicitly described +second window; existing single-window DARTs retain their setup. + +Affects: S5L8960X display DARTs with an SMMU window. + +Assisted-by: LLM +--- + drivers/iommu/apple-dart.c | 191 +++++++++++++++++++++++++++++++++++-- + include/linux/apple-dart.h | 6 ++ + 2 files changed, 191 insertions(+), 6 deletions(-) + +diff --git a/drivers/iommu/apple-dart.c b/drivers/iommu/apple-dart.c +index 4b3ec7c..368ac4c 100644 +--- a/drivers/iommu/apple-dart.c ++++ b/drivers/iommu/apple-dart.c +@@ -84,6 +84,15 @@ + #define DART_S5L8960X_REMAP 0x2c + #define DART_S5L8960X_FETCH_CONFIG 0x30 + ++/* The display SMMU shares DART's walker, but has its own stream enable. */ ++#define DART_S5L8960X_SMMU_TCR 0x0c ++#define DART_S5L8960X_SMMU_ERROR 0x10 ++#define DART_S5L8960X_SMMU_ERROR_ADDR 0x1c ++#define DART_S5L8960X_SMMU_DIAG 0x20 ++#define DART_S5L8960X_SMMU_REFLECTOR 0x24 ++#define DART_S5L8960X_SMMU_BYPASS 0x2c ++#define DART_S5L8960X_FAULT_LIMIT 16 ++ + #define DART_S5L8960X_TTBR 0x40 + + /* T8020/T6000 registers */ +@@ -220,6 +229,13 @@ struct apple_dart { + const struct apple_dart_hw *hw; + + void __iomem *regs; ++ void __iomem *smmu; ++ int smmu_irq; ++ bool smmu_irq_requested; ++ atomic_t fault_count; ++ spinlock_t fault_lock; ++ u32 fault_base; ++ unsigned long fault_irq_masked; /* bit 0 DART, bit 1 SMMU */ + + int irq; + struct clk_bulk_data *clks; +@@ -252,6 +268,75 @@ struct apple_dart { + }; + + static DEFINE_MUTEX(apple_dart_groups_lock); ++static struct platform_driver apple_dart_driver; ++ ++/* Only a display DART with a second window uses the new IRQ storm policy. */ ++static void apple_dart_s5l8960x_rearm_faults(struct apple_dart *dart) ++{ ++ unsigned long flags, masked; ++ ++ if (!dart->smmu) ++ return; ++ spin_lock_irqsave(&dart->fault_lock, flags); ++ dart->fault_base = atomic_read(&dart->fault_count); ++ masked = dart->fault_irq_masked; ++ dart->fault_irq_masked = 0; ++ spin_unlock_irqrestore(&dart->fault_lock, flags); ++ if (masked & BIT(0)) ++ enable_irq(dart->irq); ++ if (masked & BIT(1)) ++ enable_irq(dart->smmu_irq); ++} ++ ++static void apple_dart_s5l8960x_note_fault(struct apple_dart *dart, int irq, ++ void __iomem *reg, u32 status) ++{ ++ unsigned long flags; ++ unsigned int bit = irq == dart->irq ? 0 : 1; ++ u32 count; ++ ++ if (!dart->smmu) { ++ writel(status, reg); ++ atomic_inc(&dart->fault_count); ++ return; ++ } ++ /* Keep the W1C and count indivisible to a verdict's status snapshot. */ ++ spin_lock_irqsave(&dart->fault_lock, flags); ++ writel(status, reg); ++ count = atomic_inc_return(&dart->fault_count); ++ if (count - dart->fault_base >= DART_S5L8960X_FAULT_LIMIT && ++ !test_and_set_bit(bit, &dart->fault_irq_masked)) { ++ dev_err(dart->dev, "%s fault storm, masking IRQ %d after %u faults\n", ++ bit ? "SMMU" : "DART", irq, count - dart->fault_base); ++ disable_irq_nosync(irq); ++ } ++ spin_unlock_irqrestore(&dart->fault_lock, flags); ++} ++ ++int apple_dart_fault_count(struct device *dev) ++{ ++ struct apple_dart *dart; ++ unsigned long flags; ++ u32 pending; ++ int count; ++ ++ if (!dev || dev->driver != &apple_dart_driver.driver || ++ !of_device_is_compatible(dev->of_node, "apple,s5l8960x-dart")) ++ return -ENODEV; ++ dart = dev_get_drvdata(dev); ++ if (!dart) ++ return -ENODEV; ++ spin_lock_irqsave(&dart->fault_lock, flags); ++ pending = readl(dart->regs + DART_S5L8960X_ERROR); ++ if (dart->smmu) ++ pending |= readl(dart->smmu + DART_S5L8960X_SMMU_ERROR); ++ count = atomic_read(&dart->fault_count) + !!(pending & DART_S5L8960X_ERROR_FLAG); ++ spin_unlock_irqrestore(&dart->fault_lock, flags); ++ return count; ++} ++ ++EXPORT_SYMBOL_GPL(apple_dart_fault_count); ++ + + struct apple_dart_hw { + enum dart_type type; +@@ -405,6 +490,16 @@ static u32 apple_dart_s5l8960x_read_tcr(struct apple_dart *dart, u32 sid) + return (tcr >> (sid * 8)) & 0xff; + } + ++static bool apple_dart_s5l8960x_smmu_tcr_matches(struct apple_dart *dart, u32 sid) ++{ ++ u32 smmu; ++ ++ if (!dart->smmu) ++ return true; ++ smmu = readl(dart->smmu + DART_S5L8960X_SMMU_TCR) >> (sid * 8); ++ return !((smmu ^ apple_dart_s5l8960x_read_tcr(dart, sid)) & BIT(7)); ++} ++ + static void apple_dart_s5l8960x_write_tcr(struct apple_dart *dart, u32 sid, u32 val) + { + u32 shift = sid * DART_S5L8960X_TCR_BITS_PER_STREAM; +@@ -424,6 +519,14 @@ static void apple_dart_s5l8960x_write_tcr(struct apple_dart *dart, u32 sid, u32 + tcr &= ~(0xff << shift); + tcr |= (val & 0xff) << shift; + writel(tcr, dart->regs + dart->hw->tcr); ++ if (dart->smmu) { ++ /* iOS enableStream: DART first, SMMU second, under the same lock. */ ++ readl(dart->regs + dart->hw->tcr); ++ tcr = readl(dart->smmu + DART_S5L8960X_SMMU_TCR); ++ tcr &= ~(0x80U << shift); ++ tcr |= (val & 0x80U) << shift; ++ writel(tcr, dart->smmu + DART_S5L8960X_SMMU_TCR); ++ } + spin_unlock_irqrestore(&dart->tcr_lock, flags); + } + +@@ -628,6 +731,9 @@ static void apple_dart_s5l8960x_set_reflector(struct apple_dart *dart) + if (dart->has_error_reflector) + writel(dart->error_reflector, + dart->regs + DART_S5L8960X_ERROR_REFLECTOR); ++ if (dart->smmu && dart->has_error_reflector) ++ writel(dart->error_reflector, ++ dart->smmu + DART_S5L8960X_SMMU_REFLECTOR); + } + + /* The configuration iOS writes after the TCR: fetch, then diag */ +@@ -639,6 +745,8 @@ static void apple_dart_s5l8960x_set_configs(struct apple_dart *dart) + if (dart->has_diag_config) + writel(dart->diag_config, + dart->regs + DART_S5L8960X_DIAG_CONFIG); ++ if (dart->smmu) ++ writel(0, dart->smmu + DART_S5L8960X_SMMU_DIAG); + } + + static int apple_dart_hw_reset(struct apple_dart *dart) +@@ -670,6 +778,9 @@ static int apple_dart_hw_reset(struct apple_dart *dart) + + /* clear any pending errors before the interrupt is unmasked */ + writel(readl(dart->regs + dart->hw->error), dart->regs + dart->hw->error); ++ if (dart->smmu) ++ writel(readl(dart->smmu + DART_S5L8960X_SMMU_ERROR), ++ dart->smmu + DART_S5L8960X_SMMU_ERROR); + + if (dart->hw->type == DART_T8110) + writel(0, dart->regs + DART_T8110_ERROR_MASK); +@@ -701,7 +812,7 @@ apple_dart_s5l8960x_reload_configuration(struct apple_dart *dart, u32 sid) + u32 fetch_config; + u32 command = DART_S5L8960X_STREAM_COMMAND_INVALIDATE; + u32 remap; +- u32 tcr; ++ u32 tcr, smmu_tcr = 0; + int ret; + + spin_lock_irqsave(&dart->lock, flags); +@@ -709,6 +820,8 @@ apple_dart_s5l8960x_reload_configuration(struct apple_dart *dart, u32 sid) + fetch_config = readl(dart->regs + DART_S5L8960X_FETCH_CONFIG); + remap = readl(dart->regs + DART_S5L8960X_REMAP); + tcr = readl(dart->regs + DART_S5L8960X_TCR); ++ if (dart->smmu) ++ smmu_tcr = readl(dart->smmu + DART_S5L8960X_SMMU_TCR); + + for (stream = 0; stream < dart->num_streams; stream++) + for (idx = 0; idx < dart->hw->ttbr_count; idx++) +@@ -731,6 +844,8 @@ apple_dart_s5l8960x_reload_configuration(struct apple_dart *dart, u32 sid) + */ + tcr &= ~(0xff << shift); + tcr |= (dart->hw->tcr_enabled & 0xff) << shift; ++ smmu_tcr = (smmu_tcr & ~(0x80U << shift)) | ++ ((dart->hw->tcr_enabled & 0x80U) << shift); + apple_dart_s5l8960x_set_reflector(dart); + apple_dart_s5l8960x_write_tcr(dart, sid, dart->hw->tcr_enabled); + writel(remap, dart->regs + DART_S5L8960X_REMAP); +@@ -743,6 +858,8 @@ apple_dart_s5l8960x_reload_configuration(struct apple_dart *dart, u32 sid) + ret = apple_dart_s5l8960x_command_locked(&stream_map, command); + + writel(tcr, dart->regs + DART_S5L8960X_TCR); ++ if (dart->smmu) ++ writel(smmu_tcr, dart->smmu + DART_S5L8960X_SMMU_TCR); + writel(fetch_config, dart->regs + DART_S5L8960X_FETCH_CONFIG); + writel(diag_config, dart->regs + DART_S5L8960X_DIAG_CONFIG); + spin_unlock_irqrestore(&dart->lock, flags); +@@ -901,7 +1018,8 @@ static bool apple_dart_streams_tcr_is(struct apple_dart_stream_map *stream_map, + + /* Only the streams of this map: other bytes belong to other masters */ + for_each_set_bit(sid, stream_map->sidmap, dart->num_streams) +- if (dart->hw->read_tcr(dart, sid) != tcr) ++ if (dart->hw->read_tcr(dart, sid) != tcr || ++ !apple_dart_s5l8960x_smmu_tcr_matches(dart, sid)) + return false; + + return true; +@@ -944,6 +1062,7 @@ apple_dart_s5l8960x_setup_translation(struct apple_dart_domain *domain, + struct apple_dart *dart = stream_map->dart; + int i, ret; + ++ apple_dart_s5l8960x_rearm_faults(dart); + apple_dart_hw_disable_dma(stream_map); + if (!apple_dart_streams_tcr_is(stream_map, dart->hw->tcr_disabled)) { + ret = -EIO; +@@ -1177,8 +1296,10 @@ static int apple_dart_attach_dev_blocked(struct iommu_domain *domain, + struct apple_dart_stream_map *stream_map; + int i; + +- for_each_stream_map(i, cfg, stream_map) ++ for_each_stream_map(i, cfg, stream_map) { + apple_dart_hw_disable_dma(stream_map); ++ apple_dart_s5l8960x_rearm_faults(stream_map->dart); ++ } + return 0; + } + +@@ -1531,6 +1652,24 @@ static const struct iommu_ops apple_dart_iommu_ops = { + } + }; + ++static irqreturn_t apple_dart_s5l8960x_smmu_irq(int irq, void *dev) ++{ ++ struct apple_dart *dart = dev; ++ u32 status, addr; ++ ++ if (!dart->smmu) ++ return IRQ_NONE; ++ status = readl(dart->smmu + DART_S5L8960X_SMMU_ERROR); ++ if (!(status & DART_S5L8960X_ERROR_FLAG)) ++ return IRQ_NONE; ++ addr = readl(dart->smmu + DART_S5L8960X_SMMU_ERROR_ADDR); ++ dev_err_ratelimited(dart->dev, "SMMU fault: status %#x stream %u at %#x\n", ++ status, (u32)FIELD_GET(DART_S5L8960X_ERROR_STREAM, status), addr); ++ apple_dart_s5l8960x_note_fault(dart, irq, ++ dart->smmu + DART_S5L8960X_SMMU_ERROR, status); ++ return IRQ_HANDLED; ++} ++ + static irqreturn_t apple_dart_s5l8960x_irq(int irq, void *dev) + { + struct apple_dart *dart = dev; +@@ -1541,7 +1680,7 @@ static irqreturn_t apple_dart_s5l8960x_irq(int irq, void *dev) + u8 stream_idx = FIELD_GET(DART_S5L8960X_ERROR_STREAM, error); + + if (!(error & DART_S5L8960X_ERROR_FLAG)) +- return IRQ_NONE; ++ return apple_dart_s5l8960x_smmu_irq(irq, dev); + + /* there should only be a single bit set but let's use == to be sure */ + if (error_code == DART_T8020_ERROR_READ_FAULT) +@@ -1562,7 +1701,8 @@ static irqreturn_t apple_dart_s5l8960x_irq(int irq, void *dev) + "translation fault: status:0x%x stream:%d code:0x%x (%s) at 0x%x", + error, stream_idx, error_code, fault_name, addr); + +- writel(error, dart->regs + DART_S5L8960X_ERROR); ++ apple_dart_s5l8960x_note_fault(dart, irq, ++ dart->regs + DART_S5L8960X_ERROR, error); + return IRQ_HANDLED; + } + +@@ -1684,7 +1824,7 @@ static int apple_dart_probe(struct platform_device *pdev) + { + int ret; + u32 dart_params[4]; +- struct resource *res; ++ struct resource *res, *smmu_res = NULL; + struct apple_dart *dart; + struct device *dev = &pdev->dev; + +@@ -1696,6 +1836,9 @@ static int apple_dart_probe(struct platform_device *pdev) + dart->hw = of_device_get_match_data(dev); + spin_lock_init(&dart->lock); + spin_lock_init(&dart->tcr_lock); ++ spin_lock_init(&dart->fault_lock); ++ atomic_set(&dart->fault_count, 0); ++ dart->smmu_irq = -1; + + dart->regs = devm_platform_get_and_ioremap_resource(pdev, 0, &res); + if (IS_ERR(dart->regs)) +@@ -1706,6 +1849,22 @@ static int apple_dart_probe(struct platform_device *pdev) + return -EINVAL; + } + ++ if (dart->hw->type == DART_S5L8960X) { ++ smmu_res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "smmu"); ++ if (smmu_res) { ++ dart->smmu = devm_ioremap_resource(dev, smmu_res); ++ if (IS_ERR(dart->smmu)) ++ return PTR_ERR(dart->smmu); ++ dart->smmu_irq = platform_get_irq_byname_optional(pdev, "smmu"); ++ if (dart->smmu_irq == -EPROBE_DEFER) ++ return -EPROBE_DEFER; ++ if (dart->smmu_irq < 0 && platform_irq_count(pdev) > 1) ++ dart->smmu_irq = platform_get_irq(pdev, 1); ++ if (dart->smmu_irq < 0) ++ return dart->smmu_irq; ++ } ++ } ++ + dart->irq = platform_get_irq(pdev, 0); + if (dart->irq < 0) + return -ENODEV; +@@ -1720,6 +1879,12 @@ static int apple_dart_probe(struct platform_device *pdev) + return ret; + + if (dart->hw->type == DART_S5L8960X) { ++ if (dart->smmu) ++ dev_info(dev, "SMMU at %pR: tcr %08x error %08x reflector %08x bypass %08x\n", ++ smmu_res, readl(dart->smmu + DART_S5L8960X_SMMU_TCR), ++ readl(dart->smmu + DART_S5L8960X_SMMU_ERROR), ++ readl(dart->smmu + DART_S5L8960X_SMMU_REFLECTOR), ++ readl(dart->smmu + DART_S5L8960X_SMMU_BYPASS)); + dart->pgsize = SZ_4K; + /* + * These DARTs only support bypass with offset due to address +@@ -1776,6 +1941,14 @@ static int apple_dart_probe(struct platform_device *pdev) + if (ret) + goto err_clk_disable; + ++ if (dart->smmu) { ++ ret = request_irq(dart->smmu_irq, apple_dart_s5l8960x_smmu_irq, ++ IRQF_SHARED, "apple-dart SMMU fault handler", dart); ++ if (ret) ++ goto err_free_irq; ++ dart->smmu_irq_requested = true; ++ } ++ + platform_set_drvdata(pdev, dart); + + ret = iommu_device_sysfs_add(&dart->iommu, dev, NULL, "apple-dart.%s", +@@ -1797,6 +1970,9 @@ static int apple_dart_probe(struct platform_device *pdev) + err_sysfs_remove: + iommu_device_sysfs_remove(&dart->iommu); + err_free_irq: ++ apple_dart_s5l8960x_rearm_faults(dart); ++ if (dart->smmu_irq_requested) ++ free_irq(dart->smmu_irq, dart); + free_irq(dart->irq, dart); + err_clk_disable: + clk_bulk_disable_unprepare(dart->num_clks, dart->clks); +@@ -1809,6 +1985,9 @@ static void apple_dart_remove(struct platform_device *pdev) + struct apple_dart *dart = platform_get_drvdata(pdev); + + apple_dart_hw_reset(dart); ++ apple_dart_s5l8960x_rearm_faults(dart); ++ if (dart->smmu_irq_requested) ++ free_irq(dart->smmu_irq, dart); + free_irq(dart->irq, dart); + + iommu_device_unregister(&dart->iommu); +diff --git a/include/linux/apple-dart.h b/include/linux/apple-dart.h +index 1bf0558..dfbdcef 100644 +--- a/include/linux/apple-dart.h ++++ b/include/linux/apple-dart.h +@@ -7,10 +7,16 @@ + #include + + struct device_node; ++struct device; + + #if IS_REACHABLE(CONFIG_APPLE_DART) ++int apple_dart_fault_count(struct device *dart_dev); + int apple_dart_reload_configuration(struct device_node *np, u32 sid); + #else ++static inline int apple_dart_fault_count(struct device *dart_dev) ++{ ++ return -ENODEV; ++} + static inline int apple_dart_reload_configuration(struct device_node *np, + u32 sid) + { From 79311622702f09efe6dbdd67eee6f1046fec0a6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 19:25:09 +0100 Subject: [PATCH 41/49] drm/imagination: Don't walk into an absent level 1 page table An unmap walk does not create page tables. When it loads a level 1 table that is absent, pvr_mmu_op_context_load_tables() leaves curr_page.l1_table NULL and returns success. Removing the last level 0 table of a level 1 table also frees that level 1 table and clears curr_page.l1_table. In both cases, the next page that crosses only a level 0 boundary asks for a level 0 reload, and pvr_page_table_l0_get_or_insert() reads the level 1 entry through the NULL table: Unable to handle kernel NULL pointer dereference at virtual address 0000000000001008 pc : pvr_mmu_op_context_load_tables+0xd8/0x1dc [powervr] lr : pvr_mmu_op_context_next_page+0xbc/0xd4 [powervr] pvr_mmu_op_context_unmap_curr_page+0x48/0x68 [powervr] pvr_mmu_unmap+0x2c/0x38 [powervr] 0x1008 is the backing page's host_ptr behind the 512-entry mirror array. Unmapping a range only after it was mapped never reaches this, but an unmap of a range that is already partly or fully unmapped does, for example when restoring an evicted mapping first clears whatever an interrupted eviction left behind. Without a level 1 table there is no level 0 table either. Report -ENXIO like any other absent table when not inserting, so the walk treats the range as unmapped. A walk that inserts tables always loads the level 1 table first, so warn if it ever gets here. Assisted-by: LLM --- ...lk-into-an-absent-level-1-page-table.patch | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch diff --git a/kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch b/kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch new file mode 100644 index 00000000..7252e59a --- /dev/null +++ b/kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch @@ -0,0 +1,73 @@ +From 86b0db6a8e6a28a4c33341cc69586327dd7d3ad6 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 19:22:29 +0100 +Subject: [PATCH 1/2] drm/imagination: Don't walk into an absent level 1 page + table + +An unmap walk does not create page tables. When it loads a level 1 +table that is absent, pvr_mmu_op_context_load_tables() leaves +curr_page.l1_table NULL and returns success. Removing the last level 0 +table of a level 1 table also frees that level 1 table and clears +curr_page.l1_table. In both cases, the next page that crosses only a +level 0 boundary asks for a level 0 reload, and +pvr_page_table_l0_get_or_insert() reads the level 1 entry through the +NULL table: + + Unable to handle kernel NULL pointer dereference at virtual address 0000000000001008 + pc : pvr_mmu_op_context_load_tables+0xd8/0x1dc [powervr] + lr : pvr_mmu_op_context_next_page+0xbc/0xd4 [powervr] + pvr_mmu_op_context_unmap_curr_page+0x48/0x68 [powervr] + pvr_mmu_unmap+0x2c/0x38 [powervr] + +0x1008 is the backing page's host_ptr behind the 512-entry mirror +array. Unmapping a range only after it was mapped never reaches this, +but an unmap of a range that is already partly or fully unmapped does, +for example when restoring an evicted mapping first clears whatever an +interrupted eviction left behind. + +Without a level 1 table there is no level 0 table either. Report +-ENXIO like any other absent table when not inserting, so the walk +treats the range as unmapped. A walk that inserts tables always loads +the level 1 table first, so warn if it ever gets here. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_mmu.c | 16 +++++++++++++++- + 1 file changed, 15 insertions(+), 1 deletion(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c +index 7f522fd..e9e6ccb 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.c ++++ b/drivers/gpu/drm/imagination/pvr_mmu.c +@@ -1787,9 +1787,11 @@ pvr_page_table_l1_get_or_insert(struct pvr_mmu_op_context *op_ctx, + * * 0 on success, + * + * If @should_insert is %false: +- * * -%ENXIO if a level 0 page table would have been inserted. ++ * * -%ENXIO if a level 0 page table would have been inserted, or if ++ * there is no level 1 page table to look in. + * + * If @should_insert is %true: ++ * * -%EINVAL if there is no level 1 page table to insert into, or + * * Any error encountered while inserting the level 0 page table. + */ + static int +@@ -1798,6 +1800,18 @@ pvr_page_table_l0_get_or_insert(struct pvr_mmu_op_context *op_ctx, + { + struct pvr_page_table_l0 *table; + ++ /* ++ * A walk that does not create tables can reach a level 0 boundary with ++ * no level 1 table: the level 1 table was absent when it was loaded, ++ * or removing its last entry earlier in the same walk freed it. There ++ * is then no level 0 table either. ++ */ ++ if (!op_ctx->curr_page.l1_table) { ++ if (WARN_ON_ONCE(should_insert)) ++ return -EINVAL; ++ return -ENXIO; ++ } ++ + if (pvr_page_table_l1_entry_is_valid(op_ctx->curr_page.l1_table, + op_ctx->curr_page.l1_idx)) { + op_ctx->curr_page.l0_table = From e7b2c363c63475ee66f4f3b9c082050983f492a7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 19:25:09 +0100 Subject: [PATCH 42/49] drm/imagination: Skip absent page tables when unmapping pvr_mmu_op_context_unmap_curr_page() advances one device page at a time, even through a range whose page tables are absent. Unmapping a range that is already unmapped, such as clearing what an interrupted eviction may have left before restoring a mapping, then walks every page of it: 16384 steps for 256 MiB with 16 KiB pages, 65536 with 4 KiB pages. Do what the FIXME suggested. When the current page has no level 0 table, no page up to the end of that table's span can be mapped; with no level 1 table, the same holds up to the end of the level 1 span. Move the cursor to the last page of that span, so the next advance crosses the boundary and loads the following tables through the usual path, which also syncs the tables being left. Never move past the last page of the range. The check runs after each page is destroyed, so a level 0 or level 1 table freed by removing its last entry also ends the walk through its span. The -ENXIO this loop skipped on never reached it: pvr_mmu_op_context_load_tables() reports an absent table as success with a NULL table pointer, which is what the loop now checks. Assisted-by: LLM --- ...ip-absent-page-tables-when-unmapping.patch | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch diff --git a/kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch b/kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch new file mode 100644 index 00000000..a4e73a76 --- /dev/null +++ b/kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch @@ -0,0 +1,111 @@ +From 8ca9202dc7360ec2296d7917698e9457e22b74dd Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 19:23:50 +0100 +Subject: [PATCH 2/2] drm/imagination: Skip absent page tables when unmapping + +pvr_mmu_op_context_unmap_curr_page() advances one device page at a +time, even through a range whose page tables are absent. Unmapping a +range that is already unmapped, such as clearing what an interrupted +eviction may have left before restoring a mapping, then walks every +page of it: 16384 steps for 256 MiB with 16 KiB pages, 65536 with +4 KiB pages. + +Do what the FIXME suggested. When the current page has no level 0 +table, no page up to the end of that table's span can be mapped; with +no level 1 table, the same holds up to the end of the level 1 span. +Move the cursor to the last page of that span, so the next advance +crosses the boundary and loads the following tables through the usual +path, which also syncs the tables being left. Never move past the last +page of the range. + +The check runs after each page is destroyed, so a level 0 or level 1 +table freed by removing its last entry also ends the walk through its +span. + +The -ENXIO this loop skipped on never reached it: +pvr_mmu_op_context_load_tables() reports an absent table as success +with a NULL table pointer, which is what the loop now checks. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_mmu.c | 59 +++++++++++++++++---------- + 1 file changed, 37 insertions(+), 22 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c +index e9e6ccb..f6263da 100644 +--- a/drivers/gpu/drm/imagination/pvr_mmu.c ++++ b/drivers/gpu/drm/imagination/pvr_mmu.c +@@ -2455,36 +2455,51 @@ static int + pvr_mmu_op_context_unmap_curr_page(struct pvr_mmu_op_context *op_ctx, + u64 nr_pages) + { ++ u64 page = 0; + int err; + +- if (nr_pages == 0) +- return 0; ++ while (page < nr_pages) { ++ u64 skip = 0; + +- /* +- * Destroy first page outside loop, as it doesn't require a page +- * advance beforehand. If the L0 page table reference in +- * @op_ctx.curr_page is %NULL, there cannot be a mapped page at +- * @op_ctx.curr_page (so skip ahead). +- */ +- if (op_ctx->curr_page.l0_table) ++ /* Does nothing if there is no page, or no L0 table, here. */ + pvr_page_destroy(op_ctx); + +- for (u64 page = 1; page < nr_pages; ++page) { +- err = pvr_mmu_op_context_next_page(op_ctx, false); + /* +- * If the page table tree structure at @op_ctx.curr_page is +- * incomplete, skip ahead. We don't care about unmapping pages +- * that cannot exist. +- * +- * FIXME: This could be made more efficient by jumping ahead +- * using pvr_mmu_op_context_set_curr_page(). ++ * Without an L0 page table here, which may have just been ++ * removed with its last page, no page up to the end of the ++ * span of that table can be mapped. Without an L1 page table, ++ * the same holds up to the end of the span of the L1 table. ++ * Move to the last page of that span, so that the next advance ++ * crosses its boundary and loads the tables after it. + */ +- if (err == -ENXIO) +- continue; +- else if (err) +- return err; ++ if (!op_ctx->curr_page.l1_table) { ++ skip = (u64)(ROGUE_MMUCTRL_ENTRIES_PD_VALUE - 1 - ++ op_ctx->curr_page.l1_idx) * ++ ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X + ++ (ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X - 1 - ++ op_ctx->curr_page.l0_idx); ++ } else if (!op_ctx->curr_page.l0_table) { ++ skip = ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X - 1 - ++ op_ctx->curr_page.l0_idx; ++ } + +- pvr_page_destroy(op_ctx); ++ /* Stop at the last page; never advance past the range. */ ++ if (skip >= nr_pages - page - 1) ++ return 0; ++ ++ if (skip) { ++ if (!op_ctx->curr_page.l1_table) ++ op_ctx->curr_page.l1_idx = ++ ROGUE_MMUCTRL_ENTRIES_PD_VALUE - 1; ++ op_ctx->curr_page.l0_idx = ++ ROGUE_MMUCTRL_ENTRIES_PT_VALUE_X - 1; ++ page += skip; ++ } ++ ++ err = pvr_mmu_op_context_next_page(op_ctx, false); ++ if (err && err != -ENXIO) ++ return err; ++ ++page; + } + + return 0; From c8c94b2af8637f3a341f5a609e6ea00ae37a3a1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 18:46:22 +0100 Subject: [PATCH 43/49] drm/adbe: Verify translated display scanout before enabling planes Fullscreen client buffers cannot scan out directly while the display fetches physically contiguous memory in bypass, adding a compositor pass. Map scanout buffers through the display DART so imported dma-bufs form a single device-address range. Preserve the boot framebuffer mapping before deferred attachment and hold each displayed buffer through its last fetch. Require a live pipe-CRC self-check before using translated planes. Scan out an owned asymmetric pattern in bypass, through a nonidentity mapping, and through an alias with two 16 KiB pages swapped. Enable blend CRC directly at ADP +0x1074, restore its previous value, and require stable nonzero reference CRCs, matching translated CRCs, a different permuted CRC and no DART/SMMU faults. Report per-step pipe errors; output underrun alone is diagnostic. Always restore the previous framebuffer after the check. On failure or an inconclusive result, restore bypass and verify both translation controls. Retain live mappings if the previous framebuffer or rollback cannot be confirmed. Shutdown parks on the boot framebuffer before disabling both front ends. There is no option to bypass verification; the existing read-only dart_scanout parameter remains a boot-time disable switch. Only a display pipe with a scanout DMA child takes the new path. Both SoCs use the same mapping-before-attach and mandatory CRC check. If the CRC enable does not read back, restore its control without flipping or attaching. A zero or unstable reference, or an ineffective page permutation, also returns to bypass. Restore the prior image before returning from any displayed test. Affects: Apple A9 and A10 display scanout. Assisted-by: LLM --- ...be-Verify-translated-display-scanout.patch | 1079 +++++++++++++++++ 1 file changed, 1079 insertions(+) create mode 100644 kernel/patches/drivers/drm/0008-drm-adbe-Verify-translated-display-scanout.patch diff --git a/kernel/patches/drivers/drm/0008-drm-adbe-Verify-translated-display-scanout.patch b/kernel/patches/drivers/drm/0008-drm-adbe-Verify-translated-display-scanout.patch new file mode 100644 index 00000000..835df50b --- /dev/null +++ b/kernel/patches/drivers/drm/0008-drm-adbe-Verify-translated-display-scanout.patch @@ -0,0 +1,1079 @@ +From f61977a8cc02c5f34aa0b280ceee1306bf043750 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 18:46:21 +0100 +Subject: [PATCH] drm/adbe: Verify translated display scanout before enabling + planes + +Fullscreen client buffers cannot scan out directly while the display +fetches physically contiguous memory in bypass, adding a compositor pass. +Map scanout buffers through the display DART so imported dma-bufs form a +single device-address range. Preserve the boot framebuffer mapping before +deferred attachment and hold each displayed buffer through its last fetch. + +Require a live pipe-CRC self-check before using translated planes. Scan out +an owned asymmetric pattern in bypass, through a nonidentity mapping, and +through an alias with two 16 KiB pages swapped. Enable blend CRC directly at +ADP +0x1074, restore its previous value, and require stable nonzero reference +CRCs, matching translated CRCs, a different permuted CRC and no DART/SMMU +faults. Report per-step pipe errors; output underrun alone is diagnostic. + +Always restore the previous framebuffer after the check. On failure or an +inconclusive result, restore bypass and verify both translation controls. +Retain live mappings if the previous framebuffer or rollback cannot be +confirmed. Shutdown parks on the boot framebuffer before disabling both +front ends. There is no option to bypass verification; the existing +read-only dart_scanout parameter remains a boot-time disable switch. + +Only a display pipe with a scanout DMA child takes the new path. Both SoCs +use the same mapping-before-attach and mandatory CRC check. If the CRC enable +does not read back, restore its control without flipping or attaching. A +zero or unstable reference, or an ineffective page permutation, also returns +to bypass. Restore the prior image before returning from any displayed test. + +Affects: Apple A9 and A10 display scanout. + +Assisted-by: LLM +--- + drivers/gpu/drm/adbe/adbe_drv.c | 892 +++++++++++++++++++++++++++++++- + 1 file changed, 888 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/adbe/adbe_drv.c b/drivers/gpu/drm/adbe/adbe_drv.c +index 2508e65..c1af6ca 100644 +--- a/drivers/gpu/drm/adbe/adbe_drv.c ++++ b/drivers/gpu/drm/adbe/adbe_drv.c +@@ -1,6 +1,6 @@ + // SPDX-License-Identifier: GPL-2.0-only + /* +- * DRM/KMS driver for the Apple A9 (S8000/S8003) display pipe, disp0. ++ * DRM/KMS driver for the Apple A9/A10 display pipe, disp0. + * + * iBoot leaves the eDP link, the panel and the timing generator running. + * This driver adopts that mode and never programs timings, clocks or the +@@ -26,26 +26,42 @@ + * never writing it, and leaves the display to simpledrm otherwise. + * Scanout buffers are ordinary DMA allocations that must fall inside the + * same 4 GiB window. ++ * ++ * With dart_scanout=1 the pipe reads through dart-disp0 instead, as on iOS, ++ * where IOMobileFramebuffer maps every swap surface through the display's ++ * IOMapper as one DART segment. Any buffer, including another device's ++ * scattered dma-buf, can then be scanned out. Probe maps the live boot ++ * framebuffer at its bypass bus address before translation starts, keeps ++ * every buffer it handed to the pipe mapped until the pipe is seen reading a ++ * newer one, and returns the DART to bypass, on the boot framebuffer, at ++ * shutdown. + */ + + #include ++#include ++#include + #include + #include + #include + #include + #include + #include ++#include + #include + #include + #include + #include ++#include + #include + #include + #include + #include + #include + #include ++#include ++#include + #include ++#include + + #include + #include +@@ -71,6 +87,10 @@ + #define ADP_PARMST_STATE GENMASK(7, 4) + #define ADP_INTEN 0x1028 + #define ADP_STATUS 0x102c /* W1C; reads are unmasked */ ++#define ADP_CRC 0x2030 /* blend CRC, read only */ ++#define ADP_CRC_CTL 0x1074 /* iOS: 1 enables blend CRC, 0 disables */ ++#define ADP_CRC_CTL_EN BIT(0) ++#define ADP_BLEND_BYPASS 0x2020 /* logged only, never written here */ + #define ADP_FRAME 0x1034 /* [15:0] frame counter */ + #define ADP_VALID 0x1038 /* bit 0: pipe state usable */ + #define ADP_FIFO_PTR 0x1040 +@@ -138,6 +158,20 @@ + #define ADBE_FB_SIZE (ADBE_PITCH * ADBE_HEIGHT) + #define ADBE_KICK_WAIT_US (20 * USEC_PER_MSEC) /* iOS allows 1 s */ + #define ADBE_PARK_WAIT_US (100 * USEC_PER_MSEC) ++/* Translated scanout: buffers held for the pipe, and when they are released. */ ++#define ADBE_HOLD_MAX 8 ++#define ADBE_HOLD_FRAMES 1 /* boundaries after a newer entry is seen live */ ++#define ADBE_HOLD_POLL_MS 8 ++#define ADBE_FRAME_MS 17 /* one 60 Hz frame, rounded up */ ++/* Smallest IOVA window left below the boot framebuffer for allocations. */ ++#define ADBE_DART_MIN_WINDOW SZ_256M ++#define ADBE_VERIFY_PAGE SZ_16K ++#define ADBE_VERIFY_IOVA_START SZ_16M ++ ++static bool dart_scanout = true; ++module_param(dart_scanout, bool, 0444); ++MODULE_PARM_DESC(dart_scanout, ++ "Scan out through the display DART, so any dma-buf can be shown (reads back the active mode)"); + #define ADBE_FLIP_VBLANKS 3 /* complete on VBI if bit 10 never comes */ + #define ADBE_FLIP_TIMEOUT_MS 100 /* complete anyway without an interrupt */ + #define ADBE_INTEN_VBLANK (ADP_ST_VBI | ADP_ST_APPLIED) +@@ -171,6 +205,29 @@ struct adbe { + bool event_applied; + u64 kick_seq; + struct timer_list flip_timer; ++ ++ /* Translated scanout only (dart_scanout); scanout is NULL otherwise. */ ++ struct device_node *scanout_np; ++ struct platform_device *scanout; ++ struct platform_device *dart; ++ void __iomem *dart_regs[DART_WINDOWS]; ++ struct iommu_domain *domain; ++ u32 boot_iova_len; ++ /* ++ * Framebuffers handed to the pipe, oldest first. One is released only ++ * after the pipe has shown a newer one and ADBE_HOLD_FRAMES frame ++ * boundaries have passed, so no buffer is unmapped while it is still ++ * fetched. ++ */ ++ spinlock_t hold_lock; ++ struct { ++ struct drm_framebuffer *fb; ++ u32 base; ++ bool seen; /* live base has shown this entry */ ++ u16 seen_frame; /* ADP_FRAME when it was first seen */ ++ } hold[ADBE_HOLD_MAX]; ++ unsigned int nhold; ++ struct delayed_work hold_work; + }; + + static struct adbe *to_adbe(struct drm_device *drm) +@@ -388,7 +445,7 @@ static int adbe_kick(struct adbe *adbe, u32 base, u32 stride, + * applied in time, store the base directly and push an empty packet, as the + * H7 pipe driver does. + */ +-static void adbe_park(struct adbe *adbe) ++static void adbe_park_pipe(struct adbe *adbe) + { + unsigned long flags; + u32 base; +@@ -419,6 +476,131 @@ static void adbe_park(struct adbe *adbe) + adbe->cur_base = adbe->boot_base; + } + ++/* ++ * Translated scanout: drop the held framebuffers that the pipe can no ++ * longer read. The parameter FIFO applies packets in order, one at a frame ++ * boundary, and a new packet is only pushed once the previous one is out of ++ * the FIFO. Once the live base has shown an entry and ADBE_HOLD_FRAMES ++ * further boundaries have passed, every older entry is therefore finished, ++ * including ones whose update was skipped. A park is recorded as an entry ++ * without a framebuffer, so it orders against later updates like any ++ * buffer. A base that matches nothing releases nothing. @all drops every ++ * entry; callers use it only once the DART is back in bypass. ++ */ ++static void adbe_hold_release(struct adbe *adbe, bool all) ++{ ++ struct drm_framebuffer *drop[ADBE_HOLD_MAX]; ++ unsigned int ndrop = 0, keep, i; ++ unsigned long flags; ++ u16 frame; ++ u32 hw; ++ ++ hw = readl(adbe->regs + ADP_BASE) & ~ADBE_ADDR_TAG; ++ frame = readl(adbe->regs + ADP_FRAME); ++ ++ spin_lock_irqsave(&adbe->hold_lock, flags); ++ if (all) { ++ ndrop = adbe->nhold; ++ } else { ++ /* The newest entry with the live base is the one on screen. */ ++ for (i = adbe->nhold; i-- > 0;) { ++ if (adbe->hold[i].base == hw) { ++ if (!adbe->hold[i].seen) { ++ adbe->hold[i].seen = true; ++ adbe->hold[i].seen_frame = frame; ++ } ++ break; ++ } ++ } ++ /* Everything older than the newest entry seen long enough. */ ++ for (i = adbe->nhold; i-- > 0;) { ++ if (adbe->hold[i].seen && ++ (u16)(frame - adbe->hold[i].seen_frame) >= ++ ADBE_HOLD_FRAMES) { ++ ndrop = i; ++ break; ++ } ++ } ++ } ++ keep = adbe->nhold - ndrop; ++ for (i = 0; i < ndrop; i++) ++ drop[i] = adbe->hold[i].fb; ++ for (i = 0; i < keep; i++) ++ adbe->hold[i] = adbe->hold[ndrop + i]; ++ adbe->nhold = keep; ++ spin_unlock_irqrestore(&adbe->hold_lock, flags); ++ ++ for (i = 0; i < ndrop; i++) ++ if (drop[i]) ++ drm_framebuffer_put(drop[i]); ++} ++ ++static void adbe_hold_work(struct work_struct *work) ++{ ++ struct adbe *adbe = container_of(to_delayed_work(work), struct adbe, ++ hold_work); ++ ++ adbe_hold_release(adbe, false); ++ /* The newest entry is the one on screen: it stays until replaced. */ ++ if (READ_ONCE(adbe->nhold) > 1) ++ schedule_delayed_work(&adbe->hold_work, ++ msecs_to_jiffies(ADBE_HOLD_POLL_MS)); ++} ++ ++/* ++ * Hold @fb, at bus address @bus, until the pipe has moved past it. A NULL ++ * @fb records a park on the boot framebuffer. ++ */ ++static void adbe_hold_push(struct adbe *adbe, struct drm_framebuffer *fb, ++ u32 bus) ++{ ++ struct drm_framebuffer *old = NULL; ++ unsigned long flags; ++ int tries; ++ ++ /* Full only if the pipe ignores updates; give it a few frames. */ ++ for (tries = 0; tries < 8 && READ_ONCE(adbe->nhold) == ADBE_HOLD_MAX; ++ tries++) { ++ msleep(ADBE_HOLD_POLL_MS); ++ adbe_hold_release(adbe, false); ++ } ++ ++ if (fb) ++ drm_framebuffer_get(fb); ++ spin_lock_irqsave(&adbe->hold_lock, flags); ++ if (!fb && adbe->nhold && !adbe->hold[adbe->nhold - 1].fb) { ++ /* Already parked. */ ++ spin_unlock_irqrestore(&adbe->hold_lock, flags); ++ return; ++ } ++ if (adbe->nhold == ADBE_HOLD_MAX) { ++ old = adbe->hold[0].fb; ++ memmove(&adbe->hold[0], &adbe->hold[1], ++ sizeof(adbe->hold[0]) * (ADBE_HOLD_MAX - 1)); ++ adbe->nhold--; ++ } ++ adbe->hold[adbe->nhold].fb = fb; ++ adbe->hold[adbe->nhold].base = bus; ++ adbe->hold[adbe->nhold].seen = false; ++ adbe->nhold++; ++ spin_unlock_irqrestore(&adbe->hold_lock, flags); ++ ++ if (old) { ++ dev_warn_ratelimited(adbe->drm.dev, ++ "pipe not following updates, releasing the oldest held buffer\n"); ++ drm_framebuffer_put(old); ++ } ++ mod_delayed_work(system_wq, &adbe->hold_work, ++ msecs_to_jiffies(ADBE_HOLD_POLL_MS)); ++} ++ ++static void adbe_park(struct adbe *adbe) ++{ ++ adbe_park_pipe(adbe); ++ if (adbe->scanout) ++ adbe_hold_push(adbe, NULL, adbe->boot_base & ~ADBE_ADDR_TAG); ++} ++ + static int adbe_plane_atomic_check(struct drm_plane *plane, + struct drm_atomic_commit *state) + { +@@ -477,6 +659,11 @@ static void adbe_plane_atomic_update(struct drm_plane *plane, + adbe->next_base = adbe_tag_word(bus, adbe->boot_base); + adbe->next_stride = adbe_tag_word(fb->pitches[0], adbe->boot_stride); + adbe->next_dirty = true; ++ /* DRM drops its reference at flip completion, which may precede the ++ * boundary the pipe applies the base at; keep it mapped until then. ++ */ ++ if (adbe->scanout) ++ adbe_hold_push(adbe, fb, bus); + } + + static const struct drm_plane_helper_funcs adbe_plane_helper_funcs = { +@@ -799,8 +986,12 @@ static int adbe_check_dart_bypass(struct adbe *adbe, u32 bus, phys_addr_t pa) + struct platform_device *pdev = to_platform_device(adbe->drm.dev); + static const char * const names[DART_WINDOWS] = { "dart0", "dart1" }; + struct device *dev = adbe->drm.dev; ++ struct device_node *scanout_np = of_get_child_by_name(dev->of_node, "scanout"); ++ bool has_scanout = !!scanout_np; + unsigned int win, sid; + ++ of_node_put(scanout_np); ++ + for (win = 0; win < DART_WINDOWS; win++) { + struct resource *res; + void __iomem *dart; +@@ -825,6 +1016,12 @@ static int adbe_check_dart_bypass(struct adbe *adbe, u32 bus, phys_addr_t pa) + for (sid = 0; sid < DART_STREAMS && !ret; sid++) { + if (!((tcr >> (8 * sid)) & DART_TCR_TRANSLATE)) + continue; ++ /* A scanout DMA child identifies dart1 as an SMMU. */ ++ if (has_scanout && win == 1) { ++ dev_err(dev, "display SMMU stream %u translates: unknown scanout state\n", sid); ++ ret = -ENODEV; ++ break; ++ } + ret = adbe_dart_maps_scanout(dart, sid, bus, pa); + if (ret) + dev_err(dev, "display DART %s stream %u %s the scanout\n", +@@ -839,6 +1036,657 @@ static int adbe_check_dart_bypass(struct adbe *adbe, u32 bus, phys_addr_t pa) + return 0; + } + ++#ifdef CONFIG_IOMMU_API ++/* Non-requesting views. Only an owned, blocked rollback may force-clear ++ * residual translate bits, after the IOMMU driver has attempted the detach. ++ */ ++static void adbe_dart_iounmap(void *map) ++{ ++ iounmap(map); ++} ++ ++static int adbe_dart_map_windows(struct adbe *adbe) ++{ ++ struct platform_device *pdev = to_platform_device(adbe->drm.dev); ++ static const char * const names[] = { "dart0", "dart1" }; ++ unsigned int win; ++ ++ for (win = 0; win < ARRAY_SIZE(names); win++) { ++ struct resource *res = platform_get_resource_byname(pdev, IORESOURCE_MEM, names[win]); ++ int ret; ++ ++ if (!res) ++ return -ENODEV; ++ if (res->flags & IORESOURCE_MEM_NONPOSTED) ++ adbe->dart_regs[win] = ioremap_np(res->start, resource_size(res)); ++ else ++ adbe->dart_regs[win] = ioremap(res->start, resource_size(res)); ++ if (!adbe->dart_regs[win]) ++ return -ENOMEM; ++ ret = devm_add_action_or_reset(adbe->drm.dev, adbe_dart_iounmap, adbe->dart_regs[win]); ++ if (ret) ++ return ret; ++ } ++ return 0; ++} ++ ++static void adbe_dart_capture(struct adbe *adbe, const char *phase) ++{ ++ void __iomem *d = adbe->dart_regs[0], *s = adbe->dart_regs[1]; ++ ++ dev_info(adbe->drm.dev, ++ "display DART %s: 0c=%08x 10=%08x 1c=%08x 20=%08x 24=%08x 2c=%08x 30=%08x\n", ++ phase, readl(d + 0x0c), readl(d + 0x10), readl(d + 0x1c), ++ readl(d + 0x20), readl(d + 0x24), readl(d + 0x2c), readl(d + 0x30)); ++ dev_info(adbe->drm.dev, ++ "display SMMU %s: 0c=%08x 10=%08x 14=%08x 1c=%08x 20=%08x 24=%08x 2c=%08x 1000=%08x\n", ++ phase, readl(s + 0x0c), readl(s + 0x10), readl(s + 0x14), ++ readl(s + 0x1c), readl(s + 0x20), readl(s + 0x24), readl(s + 0x2c), readl(s + 0x1000)); ++} ++ ++/* Only the primary DART has TTBRs; both front ends must translate. */ ++static int adbe_check_dart_translates(struct adbe *adbe, u32 bus, phys_addr_t pa) ++{ ++ void __iomem *dart = adbe->dart_regs[0], *smmu = adbe->dart_regs[1]; ++ u32 tcr = readl(dart + DART_TCR), smmu_tcr = readl(smmu + DART_TCR); ++ unsigned int sid; ++ int ret; ++ ++ for (sid = 0; sid < DART_STREAMS; sid++) { ++ if (!((tcr >> (8 * sid)) & DART_TCR_TRANSLATE) || ++ !((smmu_tcr >> (8 * sid)) & DART_TCR_TRANSLATE)) ++ ret = -EIO; ++ else ++ ret = adbe_dart_maps_scanout(dart, sid, bus, pa); ++ if (ret != 1) { ++ dev_err(adbe->drm.dev, "display DART/SMMU stream %u does not map %#x to %pa\n", ++ sid, bus, &pa); ++ return ret < 0 ? ret : -EIO; ++ } ++ } ++ return 0; ++} ++ ++static bool adbe_dart_is_bypass(struct adbe *adbe) ++{ ++ u32 d = readl(adbe->dart_regs[0] + DART_TCR); ++ u32 s = readl(adbe->dart_regs[1] + DART_TCR); ++ ++ if (d || s) { ++ dev_err(adbe->drm.dev, "display DART/SMMU still translating (%08x/%08x)\n", d, s); ++ return false; ++ } ++ return true; ++} ++ ++/* Claiming the blocking domain must precede any fallback TCR write. No ++ * scanout allocations exist yet, and the pipe is back on its identity image. ++ */ ++static int adbe_dart_rollback(struct adbe *adbe, struct device *sdev) ++{ ++ u32 mask = 0x80808080, d, s; ++ int ret; ++ ++ ret = iommu_device_claim_dma_owner(sdev, adbe); ++ d = readl(adbe->dart_regs[0] + DART_TCR); ++ s = readl(adbe->dart_regs[1] + DART_TCR); ++ if (ret) { ++ dev_err(adbe->drm.dev, "cannot return the display DART to bypass (%d)\n", ret); ++ } else if ((d | s) & mask) { ++ dev_err(adbe->drm.dev, ++ "display DART rollback left translate bits set (%08x/%08x); clearing DART then SMMU\n", ++ d, s); ++ writel(d & ~mask, adbe->dart_regs[0] + DART_TCR); ++ readl(adbe->dart_regs[0] + DART_TCR); ++ writel(s & ~mask, adbe->dart_regs[1] + DART_TCR); ++ readl(adbe->dart_regs[1] + DART_TCR); ++ d = readl(adbe->dart_regs[0] + DART_TCR); ++ s = readl(adbe->dart_regs[1] + DART_TCR); ++ } ++ dev_info(adbe->drm.dev, "display DART after-rollback dart0c=%08x smmu0c=%08x\n", d, s); ++ return ret || !adbe_dart_is_bypass(adbe) ? -EIO : 0; ++} ++ ++static u32 adbe_verify_errors(struct adbe *adbe) ++{ ++ u32 errors = readl(adbe->regs + ADP_STATUS) & ADP_ST_ERRORS; ++ ++ if (errors) ++ writel(errors, adbe->regs + ADP_STATUS); ++ return errors; ++} ++ ++/* No IRQ is requested yet. Require this packet's applied bit, two further ++ * VBIs, then two samples on successive VBIs. Preserve errors for each step. ++ */ ++static int adbe_verify_show(struct adbe *adbe, u32 base, u32 stride, ++ u32 *crc, u32 *errors) ++{ ++ u32 status, frame, previous, samples[2]; ++ unsigned int i; ++ int ret; ++ ++ ret = adbe_kick(adbe, base, stride, NULL); ++ if (ret) ++ goto out; ++ ret = readl_poll_timeout(adbe->regs + ADP_STATUS, status, ++ status & ADP_ST_APPLIED, 1000, ADBE_PARK_WAIT_US); ++ if (ret) ++ goto out; ++ writel(status & ADP_ST_DONE, adbe->regs + ADP_STATUS); ++ for (i = 0; i < 3; i++) { ++ previous = (u16)readl(adbe->regs + ADP_FRAME); ++ ret = readl_poll_timeout(adbe->regs + ADP_FRAME, frame, ++ (u16)frame != previous, 1000, ++ 3 * ADBE_FRAME_MS * USEC_PER_MSEC); ++ if (ret) ++ goto out; ++ if (i) ++ samples[i - 1] = readl(adbe->regs + ADP_CRC); ++ } ++ *crc = samples[1]; ++ if (readl(adbe->regs + ADP_BASE) != base || ++ readl(adbe->regs + ADP_STRIDE) != stride) ++ ret = -EIO; ++ else if (samples[0] != samples[1]) ++ ret = -EAGAIN; ++out: ++ *errors |= adbe_verify_errors(adbe); ++ return ret; ++} ++ ++static bool adbe_verify_overlap(u64 a, size_t len, u64 b, size_t blen) ++{ ++ return a < b + blen && b < a + len; ++} ++ ++/* All manual mappings are made before the DMA allocator sees this domain. ++ * Exclude both the boot mapping and scratch's bypass/identity mapping. ++ */ ++static int adbe_verify_aliases(u32 boot, u32 scratch, size_t len, u32 alias[2]) ++{ ++ u64 at; ++ unsigned int n = 0; ++ ++ for (at = ADBE_VERIFY_IOVA_START; at + len <= boot; at += ALIGN(len, SZ_16M)) { ++ if (adbe_verify_overlap(at, len, boot, PAGE_ALIGN(ADBE_FB_SIZE)) || ++ adbe_verify_overlap(at, len, scratch, len)) ++ continue; ++ alias[n++] = at; ++ if (n == 2) ++ return 0; ++ } ++ return -ENOSPC; ++} ++ ++/* Same allocator as adbe_dumb_create(), before drm_dev_set_dma_dev(): the ++ * parent is still in direct-DMA mode. Verify physical contiguity from its ++ * exported sg table instead of treating an opaque DMA handle as a PA. ++ */ ++static struct drm_gem_dma_object *adbe_verify_pattern(struct adbe *adbe, ++ size_t len, phys_addr_t *pa, u32 *bus) ++{ ++ struct drm_gem_dma_object *obj; ++ struct sg_table *sgt; ++ struct scatterlist *sg; ++ phys_addr_t next; ++ size_t bytes = 0, off; ++ unsigned int i; ++ __le32 *pixels; ++ int ret = -ERANGE; ++ ++ obj = drm_gem_dma_create(&adbe->drm, len); ++ if (IS_ERR(obj)) ++ return obj; ++ sgt = drm_gem_dma_get_sg_table(obj); ++ if (IS_ERR(sgt)) { ++ ret = PTR_ERR(sgt); ++ goto put; ++ } ++ *pa = sg_phys(sgt->sgl); ++ next = *pa; ++ for_each_sg(sgt->sgl, sg, sgt->orig_nents, i) { ++ if (sg_phys(sg) != next) ++ break; ++ next += sg->length; ++ bytes += sg->length; ++ } ++ sg_free_table(sgt); ++ kfree(sgt); ++ if (bytes < len || !IS_ALIGNED(*pa, ADBE_VERIFY_PAGE) || ++ !adbe_cpu_to_bus(adbe, *pa, len, bus)) ++ goto put; ++ pixels = obj->vaddr; ++ for (off = 0; off < len; off += sizeof(*pixels)) { ++ u32 x = (off % ADBE_PITCH) / sizeof(*pixels); ++ u32 y = off / ADBE_PITCH; ++ u32 value = (x * 0x9e3779b1U) ^ (y * 0x85ebca77U); ++ ++ /* A distinct opaque marker at every 16 KiB page's first pixel ++ * guarantees that no two test pages contain the same bytes. ++ */ ++ if (!(off % ADBE_VERIFY_PAGE)) ++ value = off / ADBE_VERIFY_PAGE + 1; ++ pixels[off / sizeof(*pixels)] = cpu_to_le32(0xff000000 | (value & 0xffffff)); ++ } ++ wmb(); /* publish the allocator's write-combined stores */ ++ dma_sync_single_for_device(drm_dev_dma_dev(&adbe->drm), obj->dma_addr, ++ len, DMA_TO_DEVICE); ++ return obj; ++put: ++ drm_gem_object_put(&obj->base); ++ return ERR_PTR(ret); ++} ++ ++/* A is a real bypass reference, before deferred attach; B/C cannot validate ++ * each other if both fetch unrelated RAM. Every exit restores the old frame ++ * before releasing scratch pages or IOVAs, and verdict dumps follow rollback. ++ */ ++static int adbe_dart_verify(struct adbe *adbe, struct device *sdev, ++ struct iommu_domain *domain, u32 iova, size_t len, int f0) ++{ ++ struct drm_gem_dma_object *pattern = NULL; ++ const char *result = "INCONCLUSIVE", *reason = "fault count unavailable"; ++ u32 crc[3] = {}, errors[4] = {}, alias[2], bus = 0, ignored_crc; ++ u32 crcctl[4] = {}; /* before write, after write, after A, after restore */ ++ u32 blend_bypass = 0; ++ bool crcctl_written = false; ++ u32 previous_base = readl(adbe->regs + ADP_BASE); ++ u32 previous_stride = readl(adbe->regs + ADP_STRIDE); ++ u32 stride = adbe_tag_word(ADBE_PITCH, previous_stride); ++ size_t mapped_p = 0; ++ phys_addr_t pa; ++ bool mapped_id = false, mapped_a = false, restore_failed = false; ++ int f1, delta = 0, ret; ++ ++ if (f0 < 0) ++ goto verdict; ++ if (!adbe_dart_is_bypass(adbe)) { ++ result = "FAIL"; ++ reason = "reference is not bypass"; ++ goto verdict; ++ } ++ reason = "pattern allocation failed"; ++ pattern = adbe_verify_pattern(adbe, len, &pa, &bus); ++ if (IS_ERR(pattern)) { ++ pattern = NULL; ++ goto verdict; ++ } ++ reason = "no alias IOVA window"; ++ if (adbe_verify_overlap(bus, len, iova, len) || ++ adbe_verify_aliases(iova, bus, len, alias)) ++ goto cleanup; ++ reason = "alias mapping failed"; ++ ret = iommu_map(domain, bus, pa, len, IOMMU_READ, GFP_KERNEL); ++ if (ret) ++ goto cleanup; ++ mapped_id = true; ++ ret = iommu_map(domain, alias[0], pa, len, IOMMU_READ, GFP_KERNEL); ++ if (ret) ++ goto cleanup; ++ mapped_a = true; ++ /* Swap exactly the first two 16 KiB pages, not the image halves. */ ++ ret = iommu_map(domain, alias[1], pa + ADBE_VERIFY_PAGE, ++ ADBE_VERIFY_PAGE, IOMMU_READ, GFP_KERNEL); ++ if (ret) ++ goto cleanup; ++ mapped_p = ADBE_VERIFY_PAGE; ++ ret = iommu_map(domain, alias[1] + ADBE_VERIFY_PAGE, pa, ++ ADBE_VERIFY_PAGE, IOMMU_READ, GFP_KERNEL); ++ if (ret) ++ goto cleanup; ++ mapped_p += ADBE_VERIFY_PAGE; ++ ret = iommu_map(domain, alias[1] + mapped_p, pa + mapped_p, ++ len - mapped_p, IOMMU_READ, GFP_KERNEL); ++ if (ret) ++ goto cleanup; ++ mapped_p = len; ++ ++ console_lock(); ++ /* Discard inherited errors, especially a warm boot's output underrun. */ ++ adbe_verify_errors(adbe); ++ /* iOS setBlendCRCEnable (ExportCRCAtVBI path): direct write of 1. */ ++ crcctl[0] = readl(adbe->regs + ADP_CRC_CTL); ++ blend_bypass = readl(adbe->regs + ADP_BLEND_BYPASS); ++ writel(ADP_CRC_CTL_EN, adbe->regs + ADP_CRC_CTL); ++ crcctl_written = true; ++ crcctl[1] = readl(adbe->regs + ADP_CRC_CTL); ++ if (!(crcctl[1] & ADP_CRC_CTL_EN)) { ++ reason = "pipe CRC enable not live"; ++ goto restore_crc; ++ } ++ ret = adbe_verify_show(adbe, adbe_tag_word(bus, previous_base), stride, ++ &crc[0], &errors[0]); ++ crcctl[2] = readl(adbe->regs + ADP_CRC_CTL); ++ if (ret || !crc[0]) { ++ reason = "pipe CRC not live"; ++ goto restore; ++ } ++ ret = iommu_deferred_attach(sdev, domain); ++ if (ret) { ++ result = "FAIL"; ++ reason = "translation attach failed"; ++ goto restore; ++ } ++ if (adbe_check_dart_translates(adbe, bus, pa) || ++ adbe_check_dart_translates(adbe, alias[0], pa) || ++ adbe_check_dart_translates(adbe, alias[1], pa + ADBE_VERIFY_PAGE)) { ++ result = "FAIL"; ++ reason = "translation read-back changed"; ++ goto restore; ++ } ++ ret = adbe_verify_show(adbe, adbe_tag_word(alias[0], previous_base), stride, ++ &crc[1], &errors[1]); ++ if (ret == -EAGAIN) { ++ reason = "translated pattern CRC unstable"; ++ goto restore; ++ } ++ if (ret || crc[1] != crc[0]) { ++ result = "FAIL"; ++ reason = ret ? "pipe update failed" : "wrong pixels through translation"; ++ goto restore; ++ } ++ ret = adbe_verify_show(adbe, adbe_tag_word(alias[1], previous_base), stride, ++ &crc[2], &errors[2]); ++ if (ret) { ++ reason = ret == -EAGAIN ? "permuted pattern CRC unstable" : "pipe update failed"; ++ if (ret != -EAGAIN) ++ result = "FAIL"; ++ } else if (crc[2] == crc[0]) { ++ reason = "pipe CRC not live"; ++ } else { ++ reason = NULL; ++ } ++restore: ++ /* R: previous image, including its exact base/stride tag bits. Its CRC ++ * need not be live: applied/base/stride and the frame waits qualify it. ++ */ ++ ret = adbe_verify_show(adbe, previous_base, previous_stride, ++ &ignored_crc, &errors[3]); ++ restore_failed = ret && ret != -EAGAIN; ++restore_crc: ++ if (crcctl_written) { ++ writel(crcctl[0], adbe->regs + ADP_CRC_CTL); ++ crcctl[3] = readl(adbe->regs + ADP_CRC_CTL); ++ } ++ console_unlock(); ++ if (restore_failed) { ++ result = "FAIL"; ++ reason = "previous framebuffer restore failed"; ++ dev_err(adbe->drm.dev, ++ "display DART cannot restore previous scanout; retaining scratch and IOVAs\n"); ++ } ++cleanup: ++ /* Never free or unmap anything the pipe might still be fetching. */ ++ if (!restore_failed) { ++ bool failed = false; ++ ++ if (mapped_p && iommu_unmap(domain, alias[1], mapped_p) != mapped_p) ++ failed = true; ++ if (mapped_a && iommu_unmap(domain, alias[0], len) != len) ++ failed = true; ++ if (mapped_id && iommu_unmap(domain, bus, len) != len) ++ failed = true; ++ if (failed) { ++ result = "FAIL"; ++ reason = "alias unmap failed"; ++ } ++ } ++verdict: ++ /* Both IRQ sources and their sticky/masked errors feed this counter. */ ++ f1 = apple_dart_fault_count(&adbe->dart->dev); ++ if (f0 >= 0 && f1 >= f0) ++ delta = f1 - f0; ++ if (delta) { ++ result = "FAIL"; ++ reason = "DART/SMMU fault"; ++ } else if (f0 < 0 || f1 < f0) { ++ if (result[0] != 'F') { ++ result = "INCONCLUSIVE"; ++ reason = "fault count unavailable"; ++ } ++ } ++ /* Bit 1 is OutUnder (reported only); bits 6/9/11 are PFAlign/PFOver/RdErr. */ ++ if ((errors[1] | errors[2]) & (ADP_ST_ERRORS & ~BIT(1))) { ++ result = "FAIL"; ++ reason = "translated pipe error"; ++ } ++ if (!reason && ++ (adbe_check_dart_translates(adbe, iova, adbe->boot.start) || ++ adbe_check_dart_translates(adbe, iova + len - SZ_4K, ++ adbe->boot.start + len - SZ_4K))) { ++ result = "FAIL"; ++ reason = "translation read-back changed"; ++ } ++ ret = 0; ++ if (reason) { ++ if (restore_failed || adbe_dart_rollback(adbe, sdev)) ++ ret = -EIO; ++ else ++ ret = -ENODEV; ++ } ++ if (pattern && !restore_failed && ret != -EIO) ++ drm_gem_object_put(&pattern->base); ++ /* This capture is AFTER rollback and any forced clear, never before. */ ++ adbe_dart_capture(adbe, "after-verdict"); ++ if (!reason) { ++ dev_info(adbe->drm.dev, ++ "display DART self-check passed: pattern crc %08x translated %08x permuted %08x faults +%d step=A errbits=%#x step=B errbits=%#x step=C errbits=%#x step=R errbits=%#x crcctl %08x>%08x>%08x restored %08x blendbypass %08x\n", ++ crc[0], crc[1], crc[2], delta, errors[0], errors[1], errors[2], errors[3], ++ crcctl[0], crcctl[1], crcctl[2], crcctl[3], blend_bypass); ++ } else { ++ dev_info(adbe->drm.dev, ++ "display DART self-check %s (%s: pattern crc %08x translated %08x permuted %08x faults +%d step=A errbits=%#x step=B errbits=%#x step=C errbits=%#x step=R errbits=%#x crcctl %08x>%08x>%08x restored %08x blendbypass %08x); %s\n", ++ result, reason, crc[0], crc[1], crc[2], delta, ++ errors[0], errors[1], errors[2], errors[3], ++ crcctl[0], crcctl[1], crcctl[2], crcctl[3], blend_bypass, ++ ret == -EIO ? "rollback unconfirmed; leaving scanout unmanaged" : "back to bypass"); ++ } ++ return ret; ++} ++ ++/* ++ * Enable the display DART node, bind its driver, and order this device ++ * after it for system sleep: the DART's resume resets and restores its ++ * streams, which must happen before the pipe is given an IOVA again. ++ */ ++static int adbe_dart_bind(struct adbe *adbe) ++{ ++ struct device *dev = adbe->drm.dev; ++ struct platform_device *pdev = NULL; ++ struct of_phandle_args args; ++ int ret = 0; ++ ++ if (!IS_ENABLED(CONFIG_OF_DYNAMIC)) ++ return -EOPNOTSUPP; ++ ++ if (of_parse_phandle_with_args(adbe->scanout_np, "iommus", ++ "#iommu-cells", 0, &args)) ++ return -ENODEV; ++ ++#ifdef CONFIG_OF_DYNAMIC ++ if (!of_device_is_available(args.np)) { ++ struct of_changeset cs; ++ ++ of_changeset_init(&cs); ++ ret = of_changeset_update_prop_string(&cs, args.np, "status", ++ "okay") ?: ++ of_changeset_apply(&cs); ++ of_changeset_destroy(&cs); ++ } ++#endif ++ ++ if (!ret) { ++ pdev = of_find_device_by_node(args.np); ++ if (!pdev) { ++ pdev = of_platform_device_create(args.np, NULL, NULL); ++ if (pdev) ++ get_device(&pdev->dev); ++ } ++ if (!pdev) { ++ ret = -ENODEV; ++ } else if (!pdev->dev.driver) { ++ dev_err(dev, "%pOF: no DART driver bound\n", args.np); ++ ret = -ENODEV; ++ } else if (!device_link_add(dev, &pdev->dev, ++ DL_FLAG_STATELESS)) { ++ ret = -EINVAL; ++ } ++ if (ret && pdev) { ++ put_device(&pdev->dev); ++ pdev = NULL; ++ } ++ } ++ adbe->dart = pdev; ++ of_node_put(args.np); ++ ++ return ret; ++} ++ ++/* ++ * Switch the pipe from bypass to translated scanout. The boot framebuffer ++ * stays where the pipe reads it: its bypass bus address is mapped to its ++ * physical address in the scanout device's default domain while that ++ * domain is not yet attached (apple,dart-deferred-attach), and only then ++ * is it attached. New allocations stay below the boot framebuffer. ++ * ++ * Returns 0 in translated mode, -EIO if the DART state is unknown (the ++ * caller must leave the display alone), or another error with the pipe ++ * still reading through bypass. ++ */ ++static int adbe_dart_init(struct adbe *adbe) ++{ ++ struct device *dev = adbe->drm.dev; ++ u32 iova = adbe->boot_base & ~ADBE_ADDR_TAG; ++ size_t len = PAGE_ALIGN(ADBE_FB_SIZE); ++ struct platform_device *spdev; ++ struct iommu_domain *domain; ++ struct device *sdev; ++ int ret, f0; ++ ++ if (iova < ADBE_DART_MIN_WINDOW || (u64)iova + len > ADBE_BUS_LIMIT || ++ !IS_ALIGNED(adbe->boot.start, PAGE_SIZE) || ++ !IS_ALIGNED(iova, PAGE_SIZE)) { ++ dev_warn(dev, "boot framebuffer at bus %#x leaves no IOVA window\n", ++ iova); ++ return -ERANGE; ++ } ++ ++ adbe->scanout_np = of_get_child_by_name(dev->of_node, "scanout"); ++ if (!adbe->scanout_np) ++ return -ENODEV; ++ ++ ret = adbe_dart_map_windows(adbe); ++ if (ret) ++ return ret; ++ adbe_dart_capture(adbe, "before-attach"); ++ ++ ret = adbe_dart_bind(adbe); ++ if (ret) ++ return ret; ++ ++ spdev = of_platform_device_create(adbe->scanout_np, NULL, dev); ++ if (!spdev) ++ return -ENODEV; ++ sdev = &spdev->dev; ++ if (!device_iommu_mapped(sdev)) ++ of_dma_configure(sdev, adbe->scanout_np, true); ++ if (!device_iommu_mapped(sdev)) { ++ dev_err(dev, "scanout device has no display DART\n"); ++ return -ENODEV; ++ } ++ if (!sdev->iommu->attach_deferred) ++ dev_err(dev, "display DART attached before the boot framebuffer was mapped\n"); ++ ++ ret = dma_set_mask_and_coherent(sdev, DMA_BIT_MASK(32)); ++ if (ret) ++ goto bypass; ++ /* Allocations come from below the boot framebuffer. */ ++ sdev->bus_dma_limit = iova - 1; ++ ++ domain = iommu_get_domain_for_dev(sdev); ++ if (!domain || (domain->type != IOMMU_DOMAIN_DMA && ++ domain->type != IOMMU_DOMAIN_DMA_FQ)) { ++ ret = -ENODEV; ++ goto bypass; ++ } ++ ret = iommu_map(domain, iova, adbe->boot.start, len, IOMMU_READ, ++ GFP_KERNEL); ++ if (ret) ++ goto bypass; ++ if (iommu_iova_to_phys(domain, iova) != adbe->boot.start || ++ iommu_iova_to_phys(domain, iova + len - 1) != ++ adbe->boot.start + len - 1) { ++ ret = -EIO; ++ goto bypass; ++ } ++ ++ f0 = apple_dart_fault_count(&adbe->dart->dev); ++ ret = adbe_dart_verify(adbe, sdev, domain, iova, len, f0); ++ if (ret) ++ return ret; ++ ++ adbe->scanout = spdev; ++ adbe->domain = domain; ++ adbe->boot_iova_len = len; ++ adbe->bus_offset = 0; ++ drm_dev_set_dma_dev(&adbe->drm, sdev); ++ dev_info(dev, "scanout through the display DART, boot framebuffer at %#x\n", ++ iova); ++ ++ return 0; ++ ++bypass: ++ /* An unconfirmed boot base must retain its alias mappings and domain. */ ++ if ((readl(adbe->regs + ADP_BASE) & ~ADBE_ADDR_TAG) != iova) { ++ dev_err(dev, "display DART cannot restore boot scanout; leaving mappings alive\n"); ++ return -EIO; ++ } ++ /* The blocking domain of this DART is bypass, as iBoot left it. */ ++ if (adbe_dart_rollback(adbe, sdev)) ++ return -EIO; ++ adbe_dart_capture(adbe, "after-verdict"); ++ return ret == -EIO ? -ENODEV : ret; ++} ++ ++/* ++ * Return the display DART to bypass with the pipe on the boot framebuffer, ++ * the state iBoot leaves and the next kernel's probe checks for. ++ */ ++static void adbe_dart_exit(struct adbe *adbe) ++{ ++ u32 base; ++ ++ if (!adbe->scanout) ++ return; ++ ++ cancel_delayed_work_sync(&adbe->hold_work); ++ base = readl(adbe->regs + ADP_BASE); ++ if (base != adbe->boot_base) { ++ dev_err(adbe->drm.dev, ++ "pipe reads %#x, not the boot framebuffer: display DART left translating\n", ++ base); ++ return; ++ } ++ /* Let the frame in flight finish on the boot framebuffer. */ ++ msleep(2 * ADBE_FRAME_MS); ++ if (adbe_dart_rollback(adbe, &adbe->scanout->dev)) ++ return; ++ adbe_hold_release(adbe, true); ++ adbe->scanout = NULL; ++ dev_info(adbe->drm.dev, "display DART back in bypass\n"); ++} ++#else ++static int adbe_dart_init(struct adbe *adbe) ++{ ++ return -EOPNOTSUPP; ++} ++ ++static void adbe_dart_exit(struct adbe *adbe) ++{ ++} ++#endif ++ + /* Check that the pipe runs as iBoot leaves it; reads only. */ + static int adbe_adopt(struct adbe *adbe) + { +@@ -983,6 +1831,8 @@ static int adbe_probe(struct platform_device *pdev) + return PTR_ERR(adbe); + platform_set_drvdata(pdev, adbe); + timer_setup(&adbe->flip_timer, adbe_flip_timeout, 0); ++ spin_lock_init(&adbe->hold_lock); ++ INIT_DELAYED_WORK(&adbe->hold_work, adbe_hold_work); + + adbe->regs = devm_platform_ioremap_resource(pdev, 0); + if (IS_ERR(adbe->regs)) +@@ -1015,6 +1865,25 @@ static int adbe_probe(struct platform_device *pdev) + goto put_fb; + } + ++ /* Before any allocation: buffers then come from the DART's domain. */ ++ if (dart_scanout) { ++ struct device_node *scanout; ++ ++ scanout = of_get_child_by_name(dev->of_node, "scanout"); ++ if (!scanout) { ++ dart_scanout = false; ++ } else { ++ of_node_put(scanout); ++ ret = adbe_dart_init(adbe); ++ if (ret == -EIO) ++ goto put_fb; /* unknown DART state: leave it to simpledrm */ ++ if (ret) ++ dev_warn(dev, "translated scanout unavailable (%d), using bypass\n", ++ ret); ++ dart_scanout = !ret; ++ } ++ } ++ + ret = adbe_modeset_init(adbe); + if (ret) + goto put_fb; +@@ -1033,19 +1902,25 @@ static int adbe_probe(struct platform_device *pdev) + /* From here on simpledrm is gone; the pipe still shows its buffer. */ + ret = adbe_take_boot_fb(adbe, fb_np); + of_node_put(fb_np); +- if (ret) ++ if (ret) { ++ adbe_dart_exit(adbe); + return dev_err_probe(dev, ret, + "cannot take over the boot framebuffer\n"); ++ } + + ret = drm_dev_register(&adbe->drm, 0); +- if (ret) ++ if (ret) { ++ adbe_dart_exit(adbe); + return ret; ++ } + + drm_client_setup(&adbe->drm, NULL); + + return 0; + + put_fb: ++ /* Nothing was flipped yet: the pipe is still on the boot framebuffer. */ ++ adbe_dart_exit(adbe); + of_node_put(fb_np); + return ret; + } +@@ -1057,6 +1932,9 @@ static void adbe_remove(struct platform_device *pdev) + drm_dev_unplug(&adbe->drm); + drm_atomic_helper_shutdown(&adbe->drm); + timer_delete_sync(&adbe->flip_timer); ++ if (adbe->scanout) ++ adbe_park(adbe); ++ adbe_dart_exit(adbe); + writel(adbe->saved_inten, adbe->regs + ADP_INTEN); + } + +@@ -1065,6 +1943,12 @@ static void adbe_shutdown(struct platform_device *pdev) + struct adbe *adbe = platform_get_drvdata(pdev); + + drm_atomic_helper_shutdown(&adbe->drm); ++ if (adbe->scanout) { ++ timer_delete_sync(&adbe->flip_timer); ++ adbe_park(adbe); ++ } ++ /* A kexec'd kernel then finds the pipe as iBoot leaves it. */ ++ adbe_dart_exit(adbe); + writel(adbe->saved_inten, adbe->regs + ADP_INTEN); + } + From 31d50bc568a9d1f351bc4057926a22cd5ca2ac1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 18:46:22 +0100 Subject: [PATCH 44/49] arm64: dts: apple: s8003: Describe the display DART and SMMU The display pipe fetches through two translation front ends. Describe the DART and SMMU windows, both fault interrupts, and the deferred-attach scanout master so the boot framebuffer can be mapped before attachment. Keep the IOMMU disabled until the display driver prepares the live mapping. Only S8003 gets this node; T8010 scanout is unchanged. Record the supplied J71t display proof in the hardware catalog and regenerate its support matrix; other boards retain their recorded status. Affects: Apple A9 S8003 display DART. Assisted-by: LLM --- chips/s8003.toml | 1 + devices/j71t/device.toml | 3 +- docs/compatibility.md | 2 +- ...s8003-Describe-display-DART-and-SMMU.patch | 69 +++++++++++++++++++ 4 files changed, 73 insertions(+), 2 deletions(-) create mode 100644 kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch diff --git a/chips/s8003.toml b/chips/s8003.toml index 59e14302..59a92cdb 100644 --- a/chips/s8003.toml +++ b/chips/s8003.toml @@ -15,3 +15,4 @@ stages = ["iBSS", "iBEC"] page_size = "xnu-6153.141.1 osfmk/arm/proc_reg.h" platform = "J71t on hardware; USB serial and networking verified" stages = "remote_boot two-stage CPID list" +display = "J71t: display DART/SMMU pattern CRC passed; desktop and imported-plane scanout verified on the full development kernel" diff --git a/devices/j71t/device.toml b/devices/j71t/device.toml index bedf92b4..b8007d26 100644 --- a/devices/j71t/device.toml +++ b/devices/j71t/device.toml @@ -29,7 +29,8 @@ status = "experimental" # States: working, untested, missing, none (features.toml). [features] -display = "untested" +# Display DART/SMMU pattern CRC and imported-plane scanout verified on J71t. +display = "working" screen_off = "untested" backlight = "untested" touch = "untested" diff --git a/docs/compatibility.md b/docs/compatibility.md index bd041890..0bd21dd2 100644 --- a/docs/compatibility.md +++ b/docs/compatibility.md @@ -161,7 +161,7 @@ Boards Orchard builds for. States: working, untested, missing, none (no such har | `n66` | untested | missing | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `n69u` | untested | missing | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `n71` | untested | missing | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | -| `j71t` | untested | untested | untested | untested | untested | working | working | working | missing | untested | untested | untested | untested | untested | none | untested | untested | untested | untested | untested | working | working | working | working | untested | missing | working | +| `j71t` | working | untested | untested | untested | untested | working | working | working | missing | untested | untested | untested | untested | untested | none | untested | untested | untested | untested | untested | working | working | working | working | untested | missing | working | | `j72t` | untested | untested | untested | untested | untested | untested | untested | untested | missing | untested | untested | untested | untested | untested | missing | untested | untested | untested | untested | untested | untested | untested | untested | untested | untested | missing | untested | | `n66m` | untested | missing | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | | `n69` | untested | missing | untested | missing | untested | untested | untested | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | missing | untested | untested | untested | untested | missing | missing | missing | missing | missing | diff --git a/kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch b/kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch new file mode 100644 index 00000000..54fbc684 --- /dev/null +++ b/kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch @@ -0,0 +1,69 @@ +From 003db12ba455457f107026f6d80d61e63e46befb Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 18:46:22 +0100 +Subject: [PATCH] arm64: dts: apple: s8003: Describe the display DART and SMMU + +The display pipe fetches through two translation front ends. Describe +the DART and SMMU windows, both fault interrupts, and the deferred-attach +scanout master so the boot framebuffer can be mapped before attachment. +Keep the IOMMU disabled until the display driver prepares the live mapping. +Only S8003 gets this node; T8010 scanout is unchanged. +Record the supplied J71t display proof in the hardware catalog and +regenerate its support matrix; other boards retain their recorded status. + +Affects: Apple A9 S8003 display DART. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/s8003.dtsi | 38 ++++++++++++++++++++++++++++ + 1 file changed, 38 insertions(+) + +diff --git a/arch/arm64/boot/dts/apple/s8003.dtsi b/arch/arm64/boot/dts/apple/s8003.dtsi +index 1ed521d..f7c6c6f 100644 +--- a/arch/arm64/boot/dts/apple/s8003.dtsi ++++ b/arch/arm64/boot/dts/apple/s8003.dtsi +@@ -94,6 +94,44 @@ + ; + power-domains = <&ps_disp0>, <&ps_dp>; + status = "disabled"; ++ ++ /* ++ * The pipe's reads as a DMA master behind dart-disp0: every ++ * stream of its DART. Used for verified translated ++ * scanout; it then enables the DART below and ++ * creates this device itself, after mapping the live boot ++ * framebuffer, so translation never starts under an unmapped ++ * fetch. ++ */ ++ scanout { ++ iommus = <&disp0_dart 0>, <&disp0_dart 1>, ++ <&disp0_dart 2>, <&disp0_dart 3>; ++ apple,dart-deferred-attach; ++ }; ++ }; ++ ++ /* ++ * ADT dart-disp0: the DART and its SMMU front end. iOS enables ++ * each stream in both windows; only the DART owns tables and TLB ++ * commands. The pipe maps dart1 without requesting it, so this ++ * node owns the SMMU resource. Disabled until the display driver prepares ++ * its live mapping and verifies translated scanout. ++ */ ++ disp0_dart: iommu@206304000 { ++ compatible = "apple,s8000-dart", "apple,s5l8960x-dart"; ++ reg = <0x2 0x06304000 0x0 0x4000>, ++ <0x2 0x06300000 0x0 0x4000>; ++ reg-names = "dart", "smmu"; ++ #iommu-cells = <1>; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ ; ++ interrupt-names = "dart", "smmu"; ++ power-domains = <&ps_disp0>; ++ apple,error-reflector = /bits/ 64 <0x20fffc000>; ++ apple,fetch-config = <0x000e0303>; ++ apple,diag-config = <0x00000100>; ++ status = "disabled"; + }; + + /* ADT tempsensor,s8000 (AppleT700XTempSensor): SOC and TCC blocks */ From e89c469fbfac593dde9579af2e1502dd239e247a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 19:29:13 +0100 Subject: [PATCH 45/49] arm64: dts: apple: t8010: Describe verified display DART scanout Fullscreen imported buffers otherwise require a compositor pass on the 1536x2048 iPad 6 panel. Describe the display DART and SMMU with their ADT windows, reflector, configuration and fault interrupts, and give disp0 a deferred-attach scanout master. Reuse the same map-before-attach and mandatory pipe-CRC gate as S8003; no unchecked T8010 translation path is introduced. Use the display back-end domain, which keeps its front end powered, because there is no separate DISP0_DART domain in the current PMGR description. The ADT's smmu-options=2 and dart-options=5 are noted for hardware testers; no speculative properties or register writes are added for them. disp0 is enabled on the iPad 6 and, through the shared iPad 6 include, on the iPad 7. On the iPad 7 the display driver rejects the boot framebuffer size at probe, before any DART code runs, so it stays on simpledrm and the scanout master is never attached there. Translated scanout is untested on A10 hardware. Affects: Apple A10 iPad 6 display scanout; the iPad 7 stays on simpledrm. Assisted-by: LLM --- chips/t8010.toml | 1 + devices/j71b/device.toml | 1 + devices/j72b/device.toml | 1 + ...scribe-verified-display-DART-scanout.patch | 77 +++++++++++++++++++ 4 files changed, 80 insertions(+) create mode 100644 kernel/patches/devices/t8010/0034-arm64-dts-apple-t8010-Describe-verified-display-DART-scanout.patch diff --git a/chips/t8010.toml b/chips/t8010.toml index f0982ec6..53cfb130 100644 --- a/chips/t8010.toml +++ b/chips/t8010.toml @@ -15,3 +15,4 @@ stages = ["iBSS"] page_size = "xnu-6153.141.1 osfmk/arm/proc_reg.h" platform = "J71b on hardware" stages = "J71b single-stage on hardware; iBSS and iBEC same size" +display_dart = "J71b/J171 ADT display DART/SMMU; iPad 6 translation guarded by pipe CRC, untested on hardware" diff --git a/devices/j71b/device.toml b/devices/j71b/device.toml index d984a3d8..8fccc52e 100644 --- a/devices/j71b/device.toml +++ b/devices/j71b/device.toml @@ -27,6 +27,7 @@ status = "supported" # States: working, untested, missing, none (features.toml). [features] +# Display DART/SMMU scanout is enabled behind the pipe-CRC gate; untested on A10. display = "working" screen_off = "working" backlight = "working" diff --git a/devices/j72b/device.toml b/devices/j72b/device.toml index cfbedf94..5635b0cf 100644 --- a/devices/j72b/device.toml +++ b/devices/j72b/device.toml @@ -30,6 +30,7 @@ status = "experimental" # States: working, untested, missing, none (features.toml). [features] +# Display DART/SMMU scanout is enabled behind the pipe-CRC gate; untested on A10. display = "untested" screen_off = "untested" backlight = "untested" diff --git a/kernel/patches/devices/t8010/0034-arm64-dts-apple-t8010-Describe-verified-display-DART-scanout.patch b/kernel/patches/devices/t8010/0034-arm64-dts-apple-t8010-Describe-verified-display-DART-scanout.patch new file mode 100644 index 00000000..985b1764 --- /dev/null +++ b/kernel/patches/devices/t8010/0034-arm64-dts-apple-t8010-Describe-verified-display-DART-scanout.patch @@ -0,0 +1,77 @@ +From 6b012d7280861afb8365c708e5992aa1644fe644 Mon Sep 17 00:00:00 2001 +From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= +Date: Mon, 16 Nov 2026 19:29:13 +0100 +Subject: [PATCH] arm64: dts: apple: t8010: Describe verified display DART + scanout + +Fullscreen imported buffers otherwise require a compositor pass on the +1536x2048 iPad 6 panel. Describe the display DART and SMMU with their ADT +windows, reflector, configuration and fault interrupts, and give disp0 a +deferred-attach scanout master. Reuse the same map-before-attach and mandatory +pipe-CRC gate as S8003; no unchecked T8010 translation path is introduced. + +Use the display back-end domain, which keeps its front end powered, because +there is no separate DISP0_DART domain in the current PMGR description. +The ADT's smmu-options=2 and dart-options=5 are noted for hardware testers; +no speculative properties or register writes are added for them. +disp0 is enabled on the iPad 6 and, through the shared iPad 6 include, on +the iPad 7. On the iPad 7 the display driver rejects the boot framebuffer +size at probe, before any DART code runs, so it stays on simpledrm and the +scanout master is never attached there. Translated scanout is untested on +A10 hardware. + +Affects: Apple A10 iPad 6 display scanout; the iPad 7 stays on simpledrm. + +Assisted-by: LLM +--- + arch/arm64/boot/dts/apple/t8010.dtsi | 30 ++++++++++++++++++++++++++-- + 1 file changed, 28 insertions(+), 2 deletions(-) + +diff --git a/arch/arm64/boot/dts/apple/t8010.dtsi b/arch/arm64/boot/dts/apple/t8010.dtsi +index 73262df..d4be88a 100644 +--- a/arch/arm64/boot/dts/apple/t8010.dtsi ++++ b/arch/arm64/boot/dts/apple/t8010.dtsi +@@ -613,8 +613,8 @@ + + /* + * iBoot leaves the eDP link, the panel and the pipe running; the +- * driver adopts that mode. The display DART (dart-disp0) is only +- * read, to check that the scanout bypasses translation. ++ * driver adopts that mode. The display DART (dart-disp0) stays ++ * in bypass until the driver maps and verifies translated scanout. + */ + disp0: display-controller@206200000 { + compatible = "apple,t8010-display-pipe", "apple,s8000-display-pipe"; +@@ -631,6 +631,32 @@ + ; + power-domains = <&ps_disp0_fe>, <&ps_disp0_be>, <&ps_dp>; + status = "disabled"; ++ ++ /* Map the live boot framebuffer before enabling translation. */ ++ scanout { ++ iommus = <&disp0_dart 0>, <&disp0_dart 1>, ++ <&disp0_dart 2>, <&disp0_dart 3>; ++ apple,dart-deferred-attach; ++ }; ++ }; ++ ++ /* Disabled until the display driver maps and verifies scanout. */ ++ disp0_dart: iommu@206304000 { ++ compatible = "apple,t8010-dart", "apple,s5l8960x-dart"; ++ reg = <0x2 0x06304000 0x0 0x4000>, ++ <0x2 0x06300000 0x0 0x4000>; ++ reg-names = "dart", "smmu"; ++ #iommu-cells = <1>; ++ interrupt-parent = <&aic>; ++ interrupts = , ++ ; ++ interrupt-names = "dart", "smmu"; ++ /* No separate DISP0_DART domain; BE keeps FE powered too. */ ++ power-domains = <&ps_disp0_be>; ++ apple,error-reflector = /bits/ 64 <0x20fffc000>; ++ apple,fetch-config = <0x000e0303>; ++ apple,diag-config = <0x00000100>; ++ status = "disabled"; + }; + + pmgr: power-management@20e000000 { From ea05dfc7349503ad3225383ea988a9a30f64c6e2 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Tue, 17 Nov 2026 17:39:24 +0800 Subject: [PATCH 46/49] kernel: Put the A9 GPU follow-up series in one order #151, #152, #156 and #157 each numbered their patches after #150 as if they were the only follow-up. Number them in merge order: stability (0034-0042), firmware interrupt and PM (0043-0049), eviction (0050-0057), GPU time (0058), scene overlap (0059-0060). Assisted-by: LLM --- ...ch => 0050-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch} | 0 ...51-drm-imagination-Take-userspace-BO-pages-on-first-use.patch} | 0 ...052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch} | 0 ...53-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch} | 0 ...54-drm-imagination-Restore-evicted-VM-mappings-before-a.patch} | 0 ...55-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch} | 0 ...magination-Don-t-walk-into-an-absent-level-1-page-table.patch} | 0 ...-drm-imagination-Skip-absent-page-tables-when-unmapping.patch} | 0 ...pple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch} | 0 ...e-Signal-completed-jobs-before-touching-S8003-PM-memory.patch} | 0 ...agination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch} | 0 11 files changed, 0 insertions(+), 0 deletions(-) rename kernel/patches/drivers/gpu/{0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch => 0050-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch} (100%) rename kernel/patches/drivers/gpu/{0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch => 0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch} (100%) rename kernel/patches/drivers/gpu/{0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch => 0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch} (100%) rename kernel/patches/drivers/gpu/{0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch => 0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch} (100%) rename kernel/patches/drivers/gpu/{0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch => 0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch} (100%) rename kernel/patches/drivers/gpu/{0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch => 0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch} (100%) rename kernel/patches/drivers/gpu/{0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch => 0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch} (100%) rename kernel/patches/drivers/gpu/{0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch => 0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch} (100%) rename kernel/patches/drivers/gpu/{0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch => 0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch} (100%) rename kernel/patches/drivers/gpu/{0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch => 0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch} (100%) rename kernel/patches/drivers/gpu/{0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch => 0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch} (100%) diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch b/kernel/patches/drivers/gpu/0050-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch similarity index 100% rename from kernel/patches/drivers/gpu/0034-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch rename to kernel/patches/drivers/gpu/0050-drm-imagination-Keep-a-job-s-fence-on-its-VM.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch b/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch similarity index 100% rename from kernel/patches/drivers/gpu/0035-drm-imagination-Take-userspace-BO-pages-on-first-use.patch rename to kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch diff --git a/kernel/patches/drivers/gpu/0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch b/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch similarity index 100% rename from kernel/patches/drivers/gpu/0036-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch rename to kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch diff --git a/kernel/patches/drivers/gpu/0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch similarity index 100% rename from kernel/patches/drivers/gpu/0037-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch rename to kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch diff --git a/kernel/patches/drivers/gpu/0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch b/kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch similarity index 100% rename from kernel/patches/drivers/gpu/0038-drm-imagination-Restore-evicted-VM-mappings-before-a.patch rename to kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch diff --git a/kernel/patches/drivers/gpu/0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch b/kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch similarity index 100% rename from kernel/patches/drivers/gpu/0039-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch rename to kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch diff --git a/kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch b/kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch similarity index 100% rename from kernel/patches/drivers/gpu/0040-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch rename to kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch diff --git a/kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch b/kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch similarity index 100% rename from kernel/patches/drivers/gpu/0041-drm-imagination-Skip-absent-page-tables-when-unmapping.patch rename to kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch b/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch similarity index 100% rename from kernel/patches/drivers/gpu/0034-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch rename to kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch diff --git a/kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch b/kernel/patches/drivers/gpu/0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch similarity index 100% rename from kernel/patches/drivers/gpu/0034-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch rename to kernel/patches/drivers/gpu/0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch diff --git a/kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch b/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch similarity index 100% rename from kernel/patches/drivers/gpu/0035-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch rename to kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch From 2a03369fe352d29d48b2baf5493ff497f3a20c39 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Tue, 17 Nov 2026 17:48:56 +0800 Subject: [PATCH 47/49] kernel: Combine the A9 GPU follow-up series into one stack #151, #152, #153 and #157 were each written on #150 alone. Combined: - gpu/0051: the BO memory cap charges a native BO at creation, also when its pages come later. - gpu/0053: one record of the last two PM scenes, shared by the cache-line touch mask and the eviction veto. - gpu/0054: the restore path and VM_MAP size their page tables from the device address, as gpu/0042 made VM_MAP do. - gpu/0055: pools are prepared in the submit ioctl and sized for the firmware's feedback too; the job worker only switches to a prepared pool, between scenes, and never allocates. - gpu/0058: completions are signalled before the S8003 rings are touched, in the poll worker of gpu/0041 and gpu/0043. Two fixes on top: one submission order lock per device instead of one for every PowerVR device (gpu/0059), and the job worker allocates as before on chips that cannot evict (gpu/0060), so the A10 is unchanged. Left out: GPU time in debugfs (#156; belongs in per-client fdinfo accounting) and the default-off scene overlap switch (#157's second patch). The s8003 display DART patch becomes devices/s8000/0017; main took 0016 meanwhile. Assisted-by: LLM --- ...8003-Describe-display-DART-and-SMMU.patch} | 0 ...Take-userspace-BO-pages-on-first-use.patch | 37 ++- ...-Don-t-pin-BO-pages-for-CPU-mappings.patch | 10 +- ...Evict-idle-BOs-under-memory-pressure.patch | 175 +++++------ ...Restore-evicted-VM-mappings-before-a.patch | 36 +-- ...apple-Keep-reclaim-out-of-the-job-wo.patch | 82 ++++-- ...on-t-walk-into-an-absent-level-1-pa.patch} | 7 +- ...kip-absent-page-tables-when-unmappi.patch} | 8 +- ...e-measured-S8003-GPU-time-in-debugfs.patch | 275 ------------------ ...pple-Signal-completed-jobs-before-t.patch} | 22 +- ...ination-Order-submissions-per-device.patch | 97 ++++++ ...apple-Allocate-in-the-job-worker-as-.patch | 66 +++++ ...ow-opt-in-per-VM-S8003-scene-overlap.patch | 194 ------------ 13 files changed, 351 insertions(+), 658 deletions(-) rename kernel/patches/devices/s8000/{0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch => 0017-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch} (100%) rename kernel/patches/drivers/gpu/{0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch => 0056-drm-imagination-Don-t-walk-into-an-absent-level-1-pa.patch} (94%) rename kernel/patches/drivers/gpu/{0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch => 0057-drm-imagination-Skip-absent-page-tables-when-unmappi.patch} (94%) delete mode 100644 kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch rename kernel/patches/drivers/gpu/{0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch => 0058-drm-imagination-apple-Signal-completed-jobs-before-t.patch} (75%) create mode 100644 kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch create mode 100644 kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allocate-in-the-job-worker-as-.patch delete mode 100644 kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch diff --git a/kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch b/kernel/patches/devices/s8000/0017-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch similarity index 100% rename from kernel/patches/devices/s8000/0016-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch rename to kernel/patches/devices/s8000/0017-arm64-dts-apple-s8003-Describe-display-DART-and-SMMU.patch diff --git a/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch b/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch index d47784a3..3719743d 100644 --- a/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch +++ b/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 23:35:12 +0100 -Subject: [PATCH 34/38] drm/imagination: Take userspace BO pages on first use +Subject: [PATCH] drm/imagination: Take userspace BO pages on first use A large storage buffer consumes unswappable memory at CREATE_BO even if it is never used. Leave userspace shmem unbacked until VM_MAP, vmap or a @@ -23,12 +23,12 @@ Affects: every PowerVR device Assisted-by: LLM --- drivers/gpu/drm/imagination/pvr_drv.c | 2 +- - drivers/gpu/drm/imagination/pvr_gem.c | 79 +++++++++++++++++++++++++-- - drivers/gpu/drm/imagination/pvr_gem.h | 16 +++--- - 3 files changed, 85 insertions(+), 12 deletions(-) + drivers/gpu/drm/imagination/pvr_gem.c | 84 +++++++++++++++++++++++++-- + drivers/gpu/drm/imagination/pvr_gem.h | 16 ++--- + 3 files changed, 89 insertions(+), 13 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c -index 206ee1f..697b705 100644 +index 7ad0b71f81..f27d9b3151 100644 --- a/drivers/gpu/drm/imagination/pvr_drv.c +++ b/drivers/gpu/drm/imagination/pvr_drv.c @@ -115,7 +115,7 @@ pvr_ioctl_create_bo(struct drm_device *drm_dev, void *raw_args, @@ -41,10 +41,10 @@ index 206ee1f..697b705 100644 err = PTR_ERR(pvr_obj); goto err_drm_dev_exit; diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c -index 817b47d..0fffab7 100644 +index 44626d14f9..cca2f00ba0 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -227,6 +227,10 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) +@@ -357,6 +357,10 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) dma_resv_lock(obj->resv, NULL); @@ -55,7 +55,7 @@ index 817b47d..0fffab7 100644 err = drm_gem_shmem_vmap_locked(shmem_obj, &map); if (err) goto err_unlock; -@@ -357,8 +361,8 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t +@@ -487,8 +491,8 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t * * -%ENOMEM if sufficient physical memory cannot be allocated, or * * Any other error returned by drm_gem_create_mmap_offset(). */ @@ -66,10 +66,19 @@ index 817b47d..0fffab7 100644 { struct drm_device *drm_dev = from_pvr_device(pvr_dev); struct drm_gem_shmem_object *shmem_obj; -@@ -381,7 +385,10 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) +@@ -511,12 +515,18 @@ 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. */ ++ /* ++ * Imports also use gem_create_object; only native BOs are charged, by ++ * size, here. A userspace BO is charged before it has any backing. ++ */ + err = pvr_gem_pin_charge(pvr_obj); + if (err) + goto err_shmem_object_free; + - sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); + if (!(flags & PVR_BO_KERNEL_PINNED)) + return pvr_obj; @@ -78,7 +87,7 @@ index 817b47d..0fffab7 100644 if (IS_ERR(sgt)) { err = PTR_ERR(sgt); goto err_shmem_object_free; -@@ -423,7 +430,8 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, +@@ -558,7 +568,8 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, struct scatterlist *sgl; unsigned int sgt_idx; @@ -88,7 +97,7 @@ index 817b47d..0fffab7 100644 for_each_sgtable_dma_sg(shmem_obj->sgt, sgl, sgt_idx) { u32 new_offset = accumulated_offset + sg_dma_len(sgl); -@@ -438,3 +446,66 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, +@@ -573,3 +584,66 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, return -EINVAL; } @@ -156,10 +165,10 @@ index 817b47d..0fffab7 100644 + return sgt; +} diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index efcd270..07bd602 100644 +index 2e7cf2c9a6..ac9a38301a 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h -@@ -63,10 +63,12 @@ struct pvr_file; +@@ -64,10 +64,12 @@ struct pvr_gem_account; #define PVR_BO_DEVICE_CACHE_COHERENT BIT_ULL(61) @@ -174,7 +183,7 @@ index efcd270..07bd602 100644 /* Bits 2..0 are defined in the UAPI. */ /* Other utilities. */ -@@ -129,16 +131,16 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t +@@ -133,16 +135,16 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags); diff --git a/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch b/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch index 66bfb5f7..48ff05f9 100644 --- a/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch +++ b/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 23:35:12 +0100 -Subject: [PATCH 35/38] drm/imagination: Don't pin BO pages for CPU mappings +Subject: [PATCH] drm/imagination: Don't pin BO pages for CPU mappings Mesa maps its buffers on the CPU for their whole lifetime. The shmem mmap helper holds page references for every such VMA, defeating reclaim. @@ -19,10 +19,10 @@ Assisted-by: LLM 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c -index 0fffab7..0129c8d 100644 +index cca2f00ba0..ff912e039b 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -49,7 +49,16 @@ static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *v +@@ -179,7 +179,16 @@ static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *v if (!(pvr_obj->flags & DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS)) return -EINVAL; @@ -40,7 +40,7 @@ index 0fffab7..0129c8d 100644 } static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *obj) -@@ -62,6 +71,35 @@ static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *o +@@ -192,6 +201,35 @@ static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *o return 0; } @@ -76,7 +76,7 @@ index 0fffab7..0129c8d 100644 static const struct drm_gem_object_funcs pvr_gem_object_funcs = { .free = pvr_gem_object_free, .status = pvr_gem_object_status, -@@ -73,7 +111,7 @@ static const struct drm_gem_object_funcs pvr_gem_object_funcs = { +@@ -203,7 +241,7 @@ static const struct drm_gem_object_funcs pvr_gem_object_funcs = { .vmap = drm_gem_shmem_object_vmap, .vunmap = drm_gem_shmem_object_vunmap, .mmap = pvr_gem_mmap, diff --git a/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch index 9b62d6ae..770fdd53 100644 --- a/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch +++ b/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch @@ -1,7 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 23:39:24 +0100 -Subject: [PATCH 36/38] drm/imagination: Evict idle BOs under memory pressure +Subject: [PATCH] drm/imagination: Evict idle BOs under memory pressure Idle clients keep every mapped BO unswappable on small-memory systems. Use the GEM LRUs and shrinker to release native shmem backing while @@ -37,25 +37,26 @@ Affects: Apple A9 (S8003); other Apple chips opt in per chip Assisted-by: LLM --- - drivers/gpu/drm/imagination/Makefile | 1 - drivers/gpu/drm/imagination/pvr_apple.c | 13 + - drivers/gpu/drm/imagination/pvr_apple.h | 7 + - drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 30 +++ - drivers/gpu/drm/imagination/pvr_debugfs.c | 3 - drivers/gpu/drm/imagination/pvr_device.c | 7 + - drivers/gpu/drm/imagination/pvr_device.h | 17 ++ - drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++++- - drivers/gpu/drm/imagination/pvr_gem.h | 11 + - drivers/gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++++++++ - drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ - drivers/gpu/drm/imagination/pvr_mmu.h | 2 - drivers/gpu/drm/imagination/pvr_vm.c | 100 +++++++++++ - drivers/gpu/drm/imagination/pvr_vm.h | 2 - 15 files changed, 510 insertions(+), 6 deletions(-) + drivers/gpu/drm/imagination/Makefile | 1 + + drivers/gpu/drm/imagination/pvr_apple.c | 13 + + drivers/gpu/drm/imagination/pvr_apple.h | 7 + + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 21 ++ + drivers/gpu/drm/imagination/pvr_debugfs.c | 2 + + drivers/gpu/drm/imagination/pvr_device.c | 7 + + drivers/gpu/drm/imagination/pvr_device.h | 16 ++ + drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++- + drivers/gpu/drm/imagination/pvr_gem.h | 10 + + .../gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++ + drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ + drivers/gpu/drm/imagination/pvr_mmu.h | 2 + + drivers/gpu/drm/imagination/pvr_vm.c | 100 ++++++++ + drivers/gpu/drm/imagination/pvr_vm.h | 2 + + 15 files changed, 498 insertions(+), 6 deletions(-) + create mode 100644 drivers/gpu/drm/imagination/pvr_gem_shrinker.c diff --git a/drivers/gpu/drm/imagination/Makefile b/drivers/gpu/drm/imagination/Makefile -index 08181ce8df32cbd22de6e83253afd72268c51772..2e250a4cc7cc8fa6f0ffd7bb5c133b8e860d1aae 100644 +index 08181ce8df..2e250a4cc7 100644 --- a/drivers/gpu/drm/imagination/Makefile +++ b/drivers/gpu/drm/imagination/Makefile @@ -18,6 +18,7 @@ powervr-y := \ @@ -67,10 +68,10 @@ index 08181ce8df32cbd22de6e83253afd72268c51772..2e250a4cc7cc8fa6f0ffd7bb5c133b8e pvr_job.o \ pvr_mmu.o \ diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 5a43e3fde89dff351c9427a19884aefb2de8c926..d0e0c13029168d468df08d7293cd25d72c0d3dce 100644 +index 1108abd27e..29fbe0d350 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.c +++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -1666,3 +1666,16 @@ u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type t +@@ -2244,3 +2244,16 @@ u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, enum pvr_stream_type t *ext_type = 0; return 0; } @@ -88,10 +89,10 @@ index 5a43e3fde89dff351c9427a19884aefb2de8c926..d0e0c13029168d468df08d7293cd25d7 + return allowed; +} diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h -index d1de0ad773238914ab4e50e6881ec5141e81389c..fe3c7a5556a61e5d54e41a4ad36bb644fcdd06c4 100644 +index e0ccb1343c..4dea520059 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.h +++ b/drivers/gpu/drm/imagination/pvr_apple.h -@@ -179,6 +179,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); +@@ -181,6 +181,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev); int pvr_apple_job_add_deps(struct pvr_job *job); void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); @@ -99,7 +100,7 @@ index d1de0ad773238914ab4e50e6881ec5141e81389c..fe3c7a5556a61e5d54e41a4ad36bb644 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); -@@ -209,6 +210,12 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, +@@ -214,6 +215,12 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, *ext_type = 0; return 0; } @@ -113,42 +114,23 @@ index d1de0ad773238914ab4e50e6881ec5141e81389c..fe3c7a5556a61e5d54e41a4ad36bb644 { return -ENODEV; diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index d524415bd771047a2f06398dc51ef849666234e1..1cec40e1daf4040d38924c6c7465570861153946 100644 +index 992d0e8a23..18ede48864 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_chip.h +++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h -@@ -271,6 +271,7 @@ struct pvr_apple_chip { +@@ -320,6 +320,7 @@ struct pvr_apple_chip { 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); + bool (*vm_evictable)(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 */ diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index bc6f30096419b0196eb1930450b9f6942ba252a7..238917843acf8f04e86d511efc739c0888bb8fa3 100644 +index ff208b4baa..6cbf50ac04 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -@@ -261,6 +261,7 @@ struct s8003_vm { - - struct pvr_apple_s8003 { - struct pvr_apple *apple; -+ struct s8003_vm *pm_last, *pm_prev; - struct delayed_work drain_work; /* reads the rings while the firmware runs */ - struct mutex event_lock; /* the rings the firmware posts to */ - ktime_t touched; /* s8003_touch() */ -@@ -1521,6 +1522,10 @@ 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); +@@ -2336,6 +2336,26 @@ static void s8003_pm_feedback(struct pvr_apple *apple, struct s8003_vm *vm) } -+ if (s->pm_last == vm) -+ s->pm_last = NULL; -+ if (s->pm_prev == vm) -+ s->pm_prev = NULL; - for (e = 0; e < 2; e++) { - if (vm->slot[e]) - __clear_bit(vm->slot[e], s->slots); -@@ -2101,6 +2106,26 @@ static bool s8003_pm_idle(struct pvr_apple *apple, const struct s8003_pm *pm) - return false; } +static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, @@ -174,47 +156,36 @@ index bc6f30096419b0196eb1930450b9f6942ba252a7..238917843acf8f04e86d511efc739c08 static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, const char *engine, int err, const char *reason) { -@@ -2158,6 +2183,10 @@ 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; -+ } - pm->scene = s->gen; - pm->ta_seq = seq; - pm->td_seq = 0; -@@ -2589,5 +2618,6 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { +@@ -2940,6 +2960,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { .vm_init = pvr_apple_s8003_vm_init, .vm_fini = pvr_apple_s8003_vm_fini, .vm_scenes = pvr_apple_s8003_vm_scenes, + .vm_evictable = pvr_apple_s8003_vm_evictable, .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 ebdb05de4072856ee95e68d6349725ee0e8cacb6..cc966315682bc8695227c31710189f773fe9e38b 100644 +index 21c5f0de4d..8151d81228 100644 --- a/drivers/gpu/drm/imagination/pvr_debugfs.c +++ b/drivers/gpu/drm/imagination/pvr_debugfs.c -@@ -4,6 +4,7 @@ - #include "pvr_debugfs.h" +@@ -5,6 +5,7 @@ + #include "pvr_apple.h" #include "pvr_device.h" +#include "pvr_gem.h" #include "pvr_fw_trace.h" #include -@@ -27,6 +28,8 @@ pvr_debugfs_init(struct drm_minor *minor) - struct pvr_device *pvr_dev = to_pvr_device(drm_dev); +@@ -29,6 +30,7 @@ pvr_debugfs_init(struct drm_minor *minor) struct dentry *root = minor->debugfs_root; + pvr_apple_debugfs_init(pvr_dev, root); + pvr_gem_shrinker_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; diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c -index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..8c1bb336d3bc64273c02fee796ef785a422f6792 100644 +index c13a1b6489..c160763a3a 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -3,6 +3,7 @@ @@ -225,7 +196,7 @@ index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..8c1bb336d3bc64273c02fee796ef785a #include "pvr_device_info.h" #include "pvr_fw.h" -@@ -807,6 +808,10 @@ pvr_device_init(struct pvr_device *pvr_dev) +@@ -810,6 +811,10 @@ pvr_device_init(struct pvr_device *pvr_dev) /* Get the platform-specific data based on the compatible string. */ pvr_dev->device_data = of_device_get_match_data(dev); @@ -236,7 +207,7 @@ index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..8c1bb336d3bc64273c02fee796ef785a /* Enable and initialize clocks required for the device to operate. */ err = pvr_device_clk_init(pvr_dev); if (err) -@@ -858,6 +863,8 @@ err_pm_runtime_put: +@@ -861,6 +866,8 @@ pvr_device_init(struct pvr_device *pvr_dev) void pvr_device_fini(struct pvr_device *pvr_dev) { @@ -246,7 +217,7 @@ index 2c06a4ac117be0b954ecd7eb5a471efd142beb42..8c1bb336d3bc64273c02fee796ef785a * Deinitialization stages are performed in reverse order compared to * the initialization stages in pvr_device_init(). diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h -index 26b589ee72b9c261fbb10803fd3ddd9999e95f8c..015719016a56934d59feecb22605a85ec7ac4ba8 100644 +index da999d480b..24b4dd03d5 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -11,6 +11,8 @@ @@ -258,10 +229,10 @@ index 26b589ee72b9c261fbb10803fd3ddd9999e95f8c..015719016a56934d59feecb22605a85e #include #include -@@ -92,6 +94,21 @@ struct pvr_device { - */ - struct drm_device base; - +@@ -98,6 +100,20 @@ struct pvr_device { + struct list_head gem_pin_accounts; + /** @gem_pin_bytes: Native BO bytes reserved before backing pages exist. */ + u64 gem_pin_bytes; + /** @gem_reclaim: GEM residency, using base.gem_lru_mutex for all LRUs. */ + struct { + struct drm_gem_lru evictable; @@ -276,23 +247,22 @@ index 26b589ee72b9c261fbb10803fd3ddd9999e95f8c..015719016a56934d59feecb22605a85e + atomic64_t scans, evicts, purges, restores, restore_fail; + atomic64_t skip[12]; + } gem_reclaim; -+ + /** @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 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76df817a005 100644 +index ff912e039b..f21a91a716 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -18,6 +18,7 @@ - #include - #include - #include +@@ -24,6 +24,7 @@ + #include + #include + #include +#include #include #include #include -@@ -27,6 +28,11 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) +@@ -156,6 +157,11 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) { struct drm_gem_shmem_object *shmem_obj = to_drm_gem_shmem_obj(obj); @@ -302,9 +272,9 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d + if (bo->pages_evicted) + atomic64_sub(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); shmem_obj->pages_mark_dirty_on_put = true; + pvr_gem_pin_uncharge(gem_to_pvr_gem(obj)); drm_gem_shmem_free(shmem_obj); - } -@@ -38,6 +44,11 @@ static struct dma_buf *pvr_gem_export(struct drm_gem_object *obj, int flags) +@@ -168,6 +174,11 @@ static struct dma_buf *pvr_gem_export(struct drm_gem_object *obj, int flags) if (pvr_obj->flags & DRM_PVR_BO_PM_FW_PROTECT) return ERR_PTR(-EPERM); @@ -316,7 +286,7 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d return drm_gem_prime_export(obj, flags); } -@@ -100,16 +111,54 @@ static const struct vm_operations_struct pvr_gem_vm_ops = { +@@ -230,16 +241,54 @@ static const struct vm_operations_struct pvr_gem_vm_ops = { .close = drm_gem_vm_close, }; @@ -375,7 +345,7 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d .mmap = pvr_gem_mmap, .vm_ops = &pvr_gem_vm_ops, }; -@@ -269,7 +318,7 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) +@@ -399,7 +448,7 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) if (err) goto err_unlock; @@ -384,7 +354,7 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d if (err) goto err_unlock; -@@ -323,7 +372,7 @@ pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) +@@ -453,7 +502,7 @@ pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) dma_sync_sgtable_for_device(dev, shmem_obj->sgt, DMA_BIDIRECTIONAL); } @@ -393,7 +363,7 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d dma_resv_unlock(obj->resv); } -@@ -529,8 +578,14 @@ struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) +@@ -667,8 +716,14 @@ struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) /* Transfer the temporary pin's page reference to the sgt. */ refcount_inc(&shmem->pages_use_count); shmem->sgt = sgt; @@ -408,7 +378,7 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d return err ? ERR_PTR(err) : sgt; } -@@ -547,3 +602,17 @@ struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) +@@ -685,3 +740,17 @@ struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) dma_resv_unlock(obj->resv); return sgt; } @@ -427,22 +397,21 @@ index 0129c8d9e21c593392e206e44f39c2e57d140986..cf061a5d5e6434b2ee36416399b4e76d + drm_gem_lru_remove(obj); +} diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index 07bd602e9307e80df80b350ff1b9061757ec3ccd..c21b8e77ee01dee3dd7eae19398b49959979f66c 100644 +index ac9a38301a..efb40d6b0b 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h -@@ -111,6 +111,11 @@ struct pvr_gem_object { - */ - u64 flags; +@@ -114,6 +114,10 @@ struct pvr_gem_object { + /** @pin_account: Creating process's budget, retained until the BO is freed. */ + struct pvr_gem_account *pin_account; + /** @exported: Latched before publishing a dma-buf, under the reservation. */ + bool exported; + /** @pages_evicted: Backing pages released; protected by the reservation. */ + bool pages_evicted; -+ + }; - static_assert(offsetof(struct pvr_gem_object, base) == 0, -@@ -133,6 +138,12 @@ struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, +@@ -137,6 +141,12 @@ struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, struct pvr_gem_object *pvr_gem_object_create_user(struct pvr_device *pvr_dev, size_t size, u64 flags); @@ -457,7 +426,7 @@ index 07bd602e9307e80df80b350ff1b9061757ec3ccd..c21b8e77ee01dee3dd7eae19398b4995 int pvr_gem_object_into_handle(struct pvr_gem_object *pvr_obj, diff --git a/drivers/gpu/drm/imagination/pvr_gem_shrinker.c b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c new file mode 100644 -index 0000000000000000000000000000000000000000..c43b5586573819d30b3ce450c371acac00472f44 +index 0000000000..c43b558657 --- /dev/null +++ b/drivers/gpu/drm/imagination/pvr_gem_shrinker.c @@ -0,0 +1,223 @@ @@ -685,10 +654,10 @@ index 0000000000000000000000000000000000000000..c43b5586573819d30b3ce450c371acac + return drmm_add_action_or_reset(&dev->base, pvr_gem_shrinker_release, dev); +} diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c -index a4996b99784a630ec1d8b3db3ab5a46e044cb6ea..7f522fd3755328c8f71ff31c7d0b5654814d7ff1 100644 +index e06d4b61ef..c9e89e1940 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.c +++ b/drivers/gpu/drm/imagination/pvr_mmu.c -@@ -2654,3 +2654,21 @@ int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, +@@ -2656,3 +2656,21 @@ int pvr_mmu_map(struct pvr_mmu_op_context *op_ctx, u64 size, u64 flags, return err; } @@ -711,7 +680,7 @@ index a4996b99784a630ec1d8b3db3ab5a46e044cb6ea..7f522fd3755328c8f71ff31c7d0b5654 + return pvr_mmu_flush_exec(op_ctx->mmu_ctx->pvr_dev, true); +} diff --git a/drivers/gpu/drm/imagination/pvr_mmu.h b/drivers/gpu/drm/imagination/pvr_mmu.h -index a8ecd460168dcd417f4369548f0757999e411a79..a203336a7b14e631fa418277373f39a6276264f4 100644 +index 2c02d61ba0..287ba4059f 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.h +++ b/drivers/gpu/drm/imagination/pvr_mmu.h @@ -103,6 +103,8 @@ pvr_mmu_op_context_create(struct pvr_mmu_context *ctx, @@ -724,7 +693,7 @@ index a8ecd460168dcd417f4369548f0757999e411a79..a203336a7b14e631fa418277373f39a6 #endif /* PVR_MMU_H */ diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c -index 4988a62d0c7a27c5b053c565fc3317fd673278cb..b398d9e658b52c1a640fb91d7d7ffb37cabf2f21 100644 +index 4116fe5c12..2eb04a3a4e 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -131,6 +131,9 @@ struct dma_resv *pvr_vm_get_dma_resv(struct pvr_vm_context *vm_ctx) @@ -839,7 +808,7 @@ index 4988a62d0c7a27c5b053c565fc3317fd673278cb..b398d9e658b52c1a640fb91d7d7ffb37 + return success; +} diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h -index be5c99cc72e398416b3f434ed6eacd94179f572b..3270743f609ffdb68a495cb27a1674193fe8b06e 100644 +index be5c99cc72..3270743f60 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.h +++ b/drivers/gpu/drm/imagination/pvr_vm.h @@ -66,6 +66,8 @@ void pvr_vm_unmap_all(struct pvr_vm_context *vm_ctx); diff --git a/kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch b/kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch index c427af4d..8f5571e3 100644 --- a/kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch +++ b/kernel/patches/drivers/gpu/0054-drm-imagination-Restore-evicted-VM-mappings-before-a.patch @@ -1,8 +1,8 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 23:40:55 +0100 -Subject: [PATCH 37/38] drm/imagination: Restore evicted VM mappings before - arming jobs +Subject: [PATCH] drm/imagination: Restore evicted VM mappings before arming + jobs A faultless GPU must never start work with a hole left by reclaim. The first render on the iPad 5 hung before firmware boot when the HWRT @@ -38,12 +38,12 @@ Affects: every PowerVR device Assisted-by: LLM --- drivers/gpu/drm/imagination/pvr_job.c | 93 ++++++++++---- - drivers/gpu/drm/imagination/pvr_vm.c | 171 ++++++++++++++++++++++---- + drivers/gpu/drm/imagination/pvr_vm.c | 173 ++++++++++++++++++++++---- drivers/gpu/drm/imagination/pvr_vm.h | 5 + - 3 files changed, 219 insertions(+), 50 deletions(-) + 3 files changed, 221 insertions(+), 50 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c -index bd0dc2d..b4c26ee 100644 +index bd0dc2d6d6..b4c26ee5f0 100644 --- a/drivers/gpu/drm/imagination/pvr_job.c +++ b/drivers/gpu/drm/imagination/pvr_job.c @@ -367,10 +367,10 @@ prepare_job_syncs(struct pvr_file *pvr_file, @@ -224,7 +224,7 @@ index bd0dc2d..b4c26ee 100644 out_job_data_cleanup: diff --git a/drivers/gpu/drm/imagination/pvr_vm.c b/drivers/gpu/drm/imagination/pvr_vm.c -index b398d9e..4f6d67f 100644 +index 2eb04a3a4e..37cf116ce0 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.c +++ b/drivers/gpu/drm/imagination/pvr_vm.c @@ -234,7 +234,16 @@ static int pvr_vm_bind_op_exec(struct pvr_vm_bind_op *bind_op) @@ -264,7 +264,7 @@ index b398d9e..4f6d67f 100644 - 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; @@ -274,7 +274,7 @@ index b398d9e..4f6d67f 100644 bind_op->pvr_obj = pvr_obj; bind_op->vm_ctx = vm_ctx; bind_op->device_addr = device_addr; -@@ -358,6 +352,16 @@ err_bind_op_fini: +@@ -358,6 +352,16 @@ pvr_vm_bind_op_unmap_init(struct pvr_vm_bind_op *bind_op, return err; } @@ -349,18 +349,19 @@ index b398d9e..4f6d67f 100644 .vm_free = pvr_gpuvm_free, .sm_step_map = pvr_vm_gpuva_map, .sm_step_remap = pvr_vm_gpuva_remap, -@@ -832,8 +846,26 @@ __pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, +@@ -832,8 +846,27 @@ __pvr_vm_map(struct pvr_vm_context *vm_ctx, struct pvr_gem_object *pvr_obj, if (err) goto err_cleanup; - err = pvr_vm_bind_op_exec(&bind_op); + { + struct sg_table *sgt = pvr_gem_get_pages_sgt_locked(pvr_obj); - ++ + err = PTR_ERR_OR_ZERO(sgt); + if (!err) { + bind_op.mmu_op_ctx = pvr_mmu_op_context_create(vm_ctx->mmu_ctx, sgt, -+ pvr_obj_offset, size); ++ device_addr, pvr_obj_offset, ++ size); + err = PTR_ERR_OR_ZERO(bind_op.mmu_op_ctx); + if (err) + bind_op.mmu_op_ctx = NULL; @@ -368,7 +369,7 @@ index b398d9e..4f6d67f 100644 + if (!err) + err = pvr_vm_bind_op_exec(&bind_op); + } -+ + + /* Publish table edits while both VM and BO reservations still exclude reclaim. */ + if (bind_op.mmu_op_ctx) { + pvr_mmu_op_context_destroy(bind_op.mmu_op_ctx); @@ -377,7 +378,7 @@ index b398d9e..4f6d67f 100644 drm_gpuvm_exec_unlock(&vm_exec); err_cleanup: -@@ -915,6 +947,11 @@ pvr_vm_unmap_obj_locked(struct pvr_vm_context *vm_ctx, +@@ -915,6 +948,11 @@ pvr_vm_unmap_obj_locked(struct pvr_vm_context *vm_ctx, err = pvr_vm_bind_op_exec(&bind_op); @@ -389,7 +390,7 @@ index b398d9e..4f6d67f 100644 drm_gpuvm_exec_unlock(&vm_exec); err_cleanup: -@@ -1588,13 +1625,16 @@ bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, +@@ -1588,13 +1626,16 @@ bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, struct drm_gpuva *va; int err = 0, flush_err; @@ -407,7 +408,7 @@ index b398d9e..4f6d67f 100644 drm_gpuvm_bo_for_each_va(va, vm_bo) { struct pvr_vm_gpuva *pva = to_pvr_vm_gpuva(va); -@@ -1624,3 +1664,84 @@ unlock: +@@ -1624,3 +1665,85 @@ bool pvr_vm_evict_bo(struct drm_gem_object *obj, unsigned int idle_ms, } return success; } @@ -454,7 +455,8 @@ index b398d9e..4f6d67f 100644 + + if (!pva->evicted) + continue; -+ op = pvr_mmu_op_context_create(vm->mmu_ctx, sgt, va->gem.offset, va->va.range); ++ op = pvr_mmu_op_context_create(vm->mmu_ctx, sgt, va->va.addr, ++ va->gem.offset, va->va.range); + if (IS_ERR(op)) + return -ENOMEM; + /* Also repair an eviction that stopped after clearing only a prefix. */ @@ -493,7 +495,7 @@ index b398d9e..4f6d67f 100644 + return 0; +} diff --git a/drivers/gpu/drm/imagination/pvr_vm.h b/drivers/gpu/drm/imagination/pvr_vm.h -index 3270743..15ee067 100644 +index 3270743f60..15ee067cca 100644 --- a/drivers/gpu/drm/imagination/pvr_vm.h +++ b/drivers/gpu/drm/imagination/pvr_vm.h @@ -26,6 +26,7 @@ struct drm_pvr_ioctl_get_heap_info_args; diff --git a/kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch b/kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch index 3601d519..8ccfdc92 100644 --- a/kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch +++ b/kernel/patches/drivers/gpu/0055-drm-imagination-apple-Keep-reclaim-out-of-the-job-wo.patch @@ -1,8 +1,7 @@ From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Sun, 15 Nov 2026 23:43:11 +0100 -Subject: [PATCH 38/38] drm/imagination: apple: Keep reclaim out of the job - worker +Subject: [PATCH] drm/imagination: apple: Keep reclaim out of the job worker Memory pressure can stall a scheduler worker while its job fence is needed to make memory reclaimable. Prepare S8003 PM resources in submit @@ -39,15 +38,15 @@ Assisted-by: LLM drivers/gpu/drm/imagination/pvr_apple.c | 16 +- drivers/gpu/drm/imagination/pvr_apple.h | 6 + drivers/gpu/drm/imagination/pvr_apple_chip.h | 1 + - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 215 ++++++++++++++---- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 226 ++++++++++++++---- drivers/gpu/drm/imagination/pvr_job.c | 7 + - 5 files changed, 195 insertions(+), 50 deletions(-) + 5 files changed, 203 insertions(+), 53 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index d062461..b26ac79 100644 +index 29fbe0d350..5badeebcbe 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.c +++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -917,7 +917,7 @@ struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, +@@ -1441,7 +1441,7 @@ struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, struct pvr_apple *apple = to_apple(pvr_dev); struct pvr_apple_fence *fence; @@ -56,7 +55,7 @@ index d062461..b26ac79 100644 if (!fence) return ERR_PTR(-ENOMEM); return pvr_apple_job_fence(apple, fence, APPLE_ENGINE_COMPUTE, -@@ -1165,8 +1165,8 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) +@@ -1689,8 +1689,8 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) if (apple->chip->submit) return pvr_apple_submit_job(job); @@ -67,7 +66,7 @@ index d062461..b26ac79 100644 if (!fence || !hw) { kfree(fence); kfree(hw); -@@ -1200,7 +1200,7 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) +@@ -1724,7 +1724,7 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) APPLE_ENGINE_COMPUTE; struct pvr_apple_fence *fence; @@ -76,7 +75,7 @@ index d062461..b26ac79 100644 if (!fence) return ERR_PTR(-ENOMEM); return pvr_apple_job_fence(apple, fence, engine, apple->chip->submit(apple, job)); -@@ -1679,3 +1679,11 @@ bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *v +@@ -2257,3 +2257,11 @@ bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *v mutex_unlock(&apple->submit_lock); return allowed; } @@ -89,10 +88,10 @@ index d062461..b26ac79 100644 + return apple->chip->prepare_job ? apple->chip->prepare_job(apple, job) : 0; +} diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h -index fe3c7a5..f6b0fbe 100644 +index 4dea520059..09a21e5d93 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.h +++ b/drivers/gpu/drm/imagination/pvr_apple.h -@@ -179,6 +179,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); +@@ -181,6 +181,7 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence); void pvr_apple_wait_file_jobs(struct pvr_device *pvr_dev); int pvr_apple_job_add_deps(struct pvr_job *job); void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished); @@ -100,7 +99,7 @@ index fe3c7a5..f6b0fbe 100644 bool pvr_apple_vm_evictable(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx); 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); -@@ -210,6 +211,11 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, +@@ -215,6 +216,11 @@ static inline u32 pvr_apple_stream_musthave(struct pvr_device *pvr_dev, *ext_type = 0; return 0; } @@ -113,22 +112,22 @@ index fe3c7a5..f6b0fbe 100644 struct pvr_vm_context *vm_ctx) { diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index 1cec40e..2b3e560 100644 +index 18ede48864..687887d8f3 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_chip.h +++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h -@@ -266,6 +266,7 @@ struct pvr_apple_chip { +@@ -313,6 +313,7 @@ struct pvr_apple_chip { void (*write_frag)(struct pvr_apple *apple, u32 seq, const void *hw); void (*report_hang)(struct pvr_apple *apple); + int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); int (*submit)(struct pvr_apple *apple, struct pvr_job *job); u32 (*engine_done)(struct pvr_apple *apple, enum pvr_apple_engine engine); - void (*events)(struct pvr_apple *apple); + bool (*fault)(struct pvr_apple *apple); diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index 2389178..c70b999 100644 +index 6cbf50ac04..0bbaea56f6 100644 --- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c +++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -@@ -54,6 +54,7 @@ +@@ -56,6 +56,7 @@ #include #include #include @@ -136,8 +135,8 @@ index 2389178..c70b999 100644 #include #include #include -@@ -246,6 +247,11 @@ struct s8003_pm { - u32 built; /* the firmware start it was built for */ +@@ -250,6 +251,11 @@ struct s8003_pm { + u32 rec_seq; /* last geometry job carrying its PM record */ struct pvr_gem_object *pool; struct pvr_gem_object *aux; /* the PM's tables, mapped in the VM */ + void *pool_map, *aux_map; @@ -148,7 +147,7 @@ index 2389178..c70b999 100644 const struct pvr_hwrt_data *hwrt; /* the scene's target */ u32 scene; /* the firmware start the scene belongs to; 0: none */ u32 ta_seq; /* its geometry job */ -@@ -254,6 +260,8 @@ struct s8003_pm { +@@ -259,6 +265,8 @@ struct s8003_pm { struct s8003_vm { struct pvr_vm_context *vm_ctx; @@ -157,7 +156,7 @@ index 2389178..c70b999 100644 u32 slot[2]; /* geometry and compute, fragment; 0: none */ u32 attached; /* the firmware start the slots were loaded for */ struct s8003_pm pm[2]; -@@ -1433,6 +1441,7 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex +@@ -1577,6 +1585,7 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex if (!vm) return -ENOMEM; vm->vm_ctx = vm_ctx; @@ -165,7 +164,7 @@ index 2389178..c70b999 100644 vm->pm[0].set = -1; vm->pm[1].set = -1; /* The PM reaches its sets through the job's slot, at the firmware's addresses */ -@@ -1446,10 +1455,10 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex +@@ -1590,10 +1599,10 @@ static int pvr_apple_s8003_vm_init(struct pvr_apple *apple, struct pvr_vm_contex /* * How many scenes of a VM the firmware may hold: as many as the VM has PM @@ -179,7 +178,7 @@ index 2389178..c70b999 100644 struct pvr_vm_context *vm_ctx) { struct pvr_apple_s8003 *s = apple->chip_data; -@@ -1484,6 +1493,24 @@ static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, +@@ -1628,6 +1637,24 @@ static unsigned int pvr_apple_s8003_vm_scenes(struct pvr_apple *apple, return max(n, 1u); } @@ -204,7 +203,7 @@ index 2389178..c70b999 100644 static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) { unsigned long flags; -@@ -1492,10 +1519,18 @@ static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) +@@ -1636,10 +1663,18 @@ static void s8003_pm_put(struct pvr_apple_s8003 *s, struct s8003_pm *pm) if (pm->set >= 0) __clear_bit(pm->set, s->sets); spin_unlock_irqrestore(&s->set_lock, flags); @@ -225,7 +224,7 @@ index 2389178..c70b999 100644 } /* -@@ -1535,17 +1570,6 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte +@@ -1680,17 +1715,6 @@ static void pvr_apple_s8003_vm_fini(struct pvr_apple *apple, struct pvr_vm_conte kfree(vm); } @@ -243,7 +242,7 @@ index 2389178..c70b999 100644 static u64 s8003_pm_set_va(const struct s8003_pm *pm, u32 page) { return S8003_PM_SETS_VA + pm->set * S8003_PM_SET_SIZE + page * PVR_DEVICE_PAGE_SIZE; -@@ -1574,40 +1598,30 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s +@@ -1719,41 +1743,31 @@ static int s8003_pm_build(struct pvr_apple *apple, struct s8003_vm *vm, struct s u64 list_va, aux_va; int err; @@ -290,6 +289,7 @@ index 2389178..c70b999 100644 + pm->next_blocks = 0; } pm->built = 0; + pm->rec_seq = 0; - err = s8003_obj_zero(pm->pool); - if (!err) - err = s8003_obj_zero(pm->aux); @@ -302,7 +302,7 @@ index 2389178..c70b999 100644 control = s->pmsets.map + pm->set * S8003_PM_SET_SIZE; list = control + PVR_DEVICE_PAGE_SIZE; -@@ -2126,6 +2140,113 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, +@@ -2356,6 +2370,115 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, return true; } @@ -340,6 +340,8 @@ index 2389178..c70b999 100644 + mutex_lock(&vm->prepare_lock); + mutex_lock(&apple->submit_lock); + s8003_vm_reserve_sets(apple, vm->vm_ctx); ++ /* The firmware's feedback (s8003_pm_feedback()) is a floor for the next pool. */ ++ blocks = max(blocks, min(vm->fb_blocks, S8003_PM_BLOCKS_MAX)); + mutex_unlock(&apple->submit_lock); + if (vm->pm[0].set < 0) { + err = -ENOMEM; @@ -416,7 +418,7 @@ index 2389178..c70b999 100644 static int s8003_submit_error(struct pvr_apple *apple, struct s8003_vm *vm, const char *engine, int err, const char *reason) { -@@ -2151,14 +2272,15 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc +@@ -2382,26 +2505,30 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc 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++) @@ -434,16 +436,34 @@ index 2389178..c70b999 100644 !s8003_wait_done(apple, APPLE_ENGINE_GEOM, vm->pm[i].ta_seq)) pm = &vm->pm[i]; if (!pm) -@@ -2612,6 +2734,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + return s8003_submit_error(apple, vm, "geometry", -EBUSY, "no PM set"); + +- /* Process context, submit_lock held: grow through the existing pool allocator. */ ++ /* ++ * Take the firmware's feedback. The worker does not allocate: a larger ++ * pool is prepared for a later job (s8003_prepare_job()) and switched to ++ * here, between scenes, once it is ready. ++ */ + 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); ++ blocks = max(blocks, min(vm->fb_blocks, max(pm->blocks, pm->next_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; +@@ -2952,6 +3079,7 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { .fini = pvr_apple_s8003_fini, .restore_image = pvr_apple_s8003_restore_image, .report_hang = pvr_apple_s8003_report_hang, + .prepare_job = s8003_prepare_job, .submit = pvr_apple_s8003_submit, .engine_done = pvr_apple_s8003_engine_done, - .events = pvr_apple_s8003_events, + .fault = pvr_apple_s8003_fault, diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c -index b4c26ee..0d9b9aa 100644 +index b4c26ee5f0..0d9b9aaf29 100644 --- a/drivers/gpu/drm/imagination/pvr_job.c +++ b/drivers/gpu/drm/imagination/pvr_job.c @@ -776,6 +776,13 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, diff --git a/kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch b/kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-pa.patch similarity index 94% rename from kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch rename to kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-pa.patch index 7252e59a..417c399e 100644 --- a/kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-page-table.patch +++ b/kernel/patches/drivers/gpu/0056-drm-imagination-Don-t-walk-into-an-absent-level-1-pa.patch @@ -1,8 +1,7 @@ -From 86b0db6a8e6a28a4c33341cc69586327dd7d3ad6 Mon Sep 17 00:00:00 2001 +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 19:22:29 +0100 -Subject: [PATCH 1/2] drm/imagination: Don't walk into an absent level 1 page - table +Subject: [PATCH] drm/imagination: Don't walk into an absent level 1 page table An unmap walk does not create page tables. When it loads a level 1 table that is absent, pvr_mmu_op_context_load_tables() leaves @@ -36,7 +35,7 @@ Assisted-by: LLM 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c -index 7f522fd..e9e6ccb 100644 +index c9e89e1940..80d93cb1ad 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.c +++ b/drivers/gpu/drm/imagination/pvr_mmu.c @@ -1787,9 +1787,11 @@ pvr_page_table_l1_get_or_insert(struct pvr_mmu_op_context *op_ctx, diff --git a/kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch b/kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmappi.patch similarity index 94% rename from kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch rename to kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmappi.patch index a4e73a76..2d148b01 100644 --- a/kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmapping.patch +++ b/kernel/patches/drivers/gpu/0057-drm-imagination-Skip-absent-page-tables-when-unmappi.patch @@ -1,7 +1,7 @@ -From 8ca9202dc7360ec2296d7917698e9457e22b74dd Mon Sep 17 00:00:00 2001 +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 19:23:50 +0100 -Subject: [PATCH 2/2] drm/imagination: Skip absent page tables when unmapping +Subject: [PATCH] drm/imagination: Skip absent page tables when unmapping pvr_mmu_op_context_unmap_curr_page() advances one device page at a time, even through a range whose page tables are absent. Unmapping a @@ -32,10 +32,10 @@ Assisted-by: LLM 1 file changed, 37 insertions(+), 22 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_mmu.c b/drivers/gpu/drm/imagination/pvr_mmu.c -index e9e6ccb..f6263da 100644 +index 80d93cb1ad..0d40c8a262 100644 --- a/drivers/gpu/drm/imagination/pvr_mmu.c +++ b/drivers/gpu/drm/imagination/pvr_mmu.c -@@ -2455,36 +2455,51 @@ static int +@@ -2457,36 +2457,51 @@ static int pvr_mmu_op_context_unmap_curr_page(struct pvr_mmu_op_context *op_ctx, u64 nr_pages) { diff --git a/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch b/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch deleted file mode 100644 index af6bbd39..00000000 --- a/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Report-firmware-measured-S8003-GPU-time-in-debugfs.patch +++ /dev/null @@ -1,275 +0,0 @@ -From 3a19f9211c0ef2ae6af305c792cfc555e573f58d Mon Sep 17 00:00:00 2001 -From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= -Date: Mon, 16 Nov 2026 17:46:27 +0100 -Subject: [PATCH] drm/imagination: apple: Report firmware-measured S8003 GPU - time in debugfs - -Host in-flight durations include queueing and firmware overhead, so they -cannot show how long the GPU geometry and fragment phases actually ran. - -Read the retired phase-time u64 at W+0x550 or W+0x8ac before the record is -cleared. Both submission paths wait for last_seq - 1 before writing the -two-slot ring's next record, so its previous occupant has retired. Order -that shared-memory read with dma_rmb(), preserve it across the fill, and -account only successful fills before the result word is cleared. - -Expose accumulated geometry and fragment time, jobs and zero samples in -apple_jobs, converting 24 MHz ticks by ticks * 125 / 3. Add only the small -chip debugfs callback needed by that line; no timing parameters, phase-gap -attribution or memory census. Submission ordering and power policy stay -unchanged. - -Affects: Apple A9 (S8003), measurement only. The A10 is unchanged. - -Assisted-by: LLM ---- - drivers/gpu/drm/imagination/pvr_apple.c | 19 +++++++ - drivers/gpu/drm/imagination/pvr_apple.h | 3 ++ - drivers/gpu/drm/imagination/pvr_apple_chip.h | 3 ++ - drivers/gpu/drm/imagination/pvr_apple_s8003.c | 53 ++++++++++++++++--- - drivers/gpu/drm/imagination/pvr_debugfs.c | 3 ++ - 5 files changed, 75 insertions(+), 6 deletions(-) - -diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 5a43e3f..964549a 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple.c -+++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -31,6 +31,8 @@ - - #include - #include -+#include -+#include - #include - #include - #include -@@ -579,6 +581,23 @@ static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine - return heap_read32(apple, apple->layout.stamp[engine]); - } - -+static int pvr_apple_jobs_show(struct seq_file *s, void *unused) -+{ -+ struct pvr_apple *apple = s->private; -+ -+ 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) -+{ -+ struct pvr_apple *apple = to_apple(pvr_dev); -+ -+ if (apple && apple->chip->debugfs_show) -+ debugfs_create_file("apple_jobs", 0400, root, apple, &pvr_apple_jobs_fops); -+} -+ - /* 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) - { -diff --git a/drivers/gpu/drm/imagination/pvr_apple.h b/drivers/gpu/drm/imagination/pvr_apple.h -index d1de0ad..24e3ec2 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); -@@ -191,6 +193,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 d524415..1527248 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple_chip.h -+++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h -@@ -28,6 +28,7 @@ - #include - - struct devfreq; -+struct seq_file; - struct pvr_gem_object; - struct pvr_stream_cmd_defs; - -@@ -273,6 +274,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); -+ /* Chip-specific measurements in apple_jobs; no MMIO. */ -+ void (*debugfs_show)(struct pvr_apple *apple, struct seq_file *m); - }; - - /* The heap is coherent memory shared with the firmware, not MMIO. */ -diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -index 40ca888..0f69c5a 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c -+++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c -@@ -34,6 +34,9 @@ - * readings of Apple's kext and firmware. - */ - -+#include -+#include -+ - #include "pvr_apple.h" - #include "pvr_apple_chip.h" - #include "pvr_context.h" -@@ -307,6 +310,8 @@ struct pvr_apple_s8003 { - struct pvr_gem_object *spare; - bool pio_mapped; - -+ u64 gpu_ticks[2], gpu_jobs[2], gpu_zero[2]; /* retired geometry/fragment phase time */ -+ - struct s8003_idle idle; - }; - -@@ -1958,6 +1963,19 @@ static struct s8003_job_slot s8003_slot_of(u32 seq, bool frag) - }; - } - -+/* -+ * Submissions wait for last_seq - 1 before reusing either of the two records: -+ * the prior occupant of s8003_slot_of(last_seq + 1) is last_seq - 1. Preserve -+ * its result before fill_ta/fill_3d clears W. Failed fills do not count it. -+ */ -+static u64 s8003_job_ticks(u8 *p, u32 seq, bool frag) -+{ -+ struct s8003_job_slot at = s8003_slot_of(seq, frag); -+ -+ dma_rmb(); -+ return get_unaligned_le64(p + at.w + (frag ? 0x8ac : 0x550)); -+} -+ - /* What the 3D engine is waited for between a fragment job's start and finish */ - static const u32 s8003_3d_wait[] = { - 0x201, 0x138204, 0x10, 0x8388204, 0, 0x104000a, 0x1f, -@@ -1983,8 +2001,8 @@ static u8 *useq_put(u8 *out, const u32 *words, unsigned int n) - * the PM 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) -+static void s8003_job_write(struct pvr_apple_s8003 *s, u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 slot, bool frag, -+ u32 seq, u32 wait_stamp, u32 wait_seq, u64 gpu_ticks) - { - struct s8003_job_slot at = s8003_slot_of(seq, frag); - u32 w = addr + at.w, record = pm + S8003_PM_RECORD; -@@ -1997,6 +2015,13 @@ static void s8003_job_write(u8 *p, u32 addr, u32 global, u32 pm, u32 pm_id, u32 - const u32 time_end[] = { 0x18, result, time, time + 4, seq }; - const u32 wait_ta = 0x801, end = 0x40000017; - -+ if (gpu_ticks) { -+ s->gpu_ticks[frag] += gpu_ticks; -+ s->gpu_jobs[frag]++; -+ } else { -+ s->gpu_zero[frag]++; -+ } -+ - put32(work + (frag ? 0x8ac : 0x550), 0); - put32(work + (frag ? 0x8b0 : 0x554), 0); - put32(work + (frag ? 0x8b4 : 0x558), times); -@@ -2145,6 +2170,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; -+ u64 gpu_ticks; - unsigned int i; - int err; - -@@ -2179,11 +2205,12 @@ 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; -+ gpu_ticks = s8003_job_ticks(p, seq, false); - s8003_fill_ta(p + s8003_slot_of(seq, false).w, job->cmd, job->hwrt, &rt); -- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_GEOM), -+ s8003_job_write(s, 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, gpu_ticks); - pm->hwrt = job->hwrt; - pm->scene = s->gen; - pm->ta_seq = seq; -@@ -2203,6 +2230,7 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc - struct s8003_rt rt; - unsigned int i; - u32 seq; -+ u64 gpu_ticks; - int err; - - /* The scene of this job's geometry job, which was submitted before it */ -@@ -2221,13 +2249,14 @@ static int s8003_submit_frag(struct pvr_apple *apple, struct s8003_vm *vm, struc - return s8003_submit_error(apple, vm, "fragment", err, "record slot wait"); - - seq = apple->frag_seq + 1; -+ gpu_ticks = s8003_job_ticks(p, seq, true); - err = s8003_fill_3d(p + s8003_slot_of(seq, true).w, job->cmd, job->hwrt, &rt); - if (err) - return s8003_submit_error(apple, vm, "fragment", err, "3D record fill"); -- s8003_job_write(p, s8003_channel_addr(APPLE_ENGINE_FRAG), -+ s8003_job_write(s, 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, gpu_ticks); - pm->td_seq = seq; - apple->frag_seq = seq; - err = s8003_publish(apple, APPLE_ENGINE_FRAG, seq, (2 * seq) % S8003_CH_ENTRIES); -@@ -2589,7 +2618,19 @@ static void pvr_apple_s8003_restore_image(struct pvr_apple *apple) - /* start() copies the image and builds the init data anew each time */ - } - -+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, "gpu_ns geometry %llu fragment %llu jobs %llu %llu zero %llu %llu\n", -+ mul_u64_u32_div(READ_ONCE(s->gpu_ticks[0]), 125, 3), -+ mul_u64_u32_div(READ_ONCE(s->gpu_ticks[1]), 125, 3), -+ READ_ONCE(s->gpu_jobs[0]), READ_ONCE(s->gpu_jobs[1]), -+ READ_ONCE(s->gpu_zero[0]), READ_ONCE(s->gpu_zero[1])); -+} -+ - const struct pvr_apple_chip pvr_apple_chip_s8003 = { -+ .debugfs_show = pvr_apple_s8003_debugfs_show, - .fw_name = "apple/gfx-s8003.bin", - .perf_state_max = S8003_PERF_STATE_MAX, - .geom_stream = &pvr_apple_g5p_cmd_geom_stream, -diff --git a/drivers/gpu/drm/imagination/pvr_debugfs.c b/drivers/gpu/drm/imagination/pvr_debugfs.c -index ebdb05d..fce6cdf 100644 ---- a/drivers/gpu/drm/imagination/pvr_debugfs.c -+++ b/drivers/gpu/drm/imagination/pvr_debugfs.c -@@ -2,6 +2,7 @@ - /* Copyright (c) 2023 Imagination Technologies Ltd. */ - - #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; diff --git a/kernel/patches/drivers/gpu/0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch b/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Signal-completed-jobs-before-t.patch similarity index 75% rename from kernel/patches/drivers/gpu/0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch rename to kernel/patches/drivers/gpu/0058-drm-imagination-apple-Signal-completed-jobs-before-t.patch index 3b92d31e..8adc7f05 100644 --- a/kernel/patches/drivers/gpu/0059-drm-imagination-apple-Signal-completed-jobs-before-touching-S8003-PM-memory.patch +++ b/kernel/patches/drivers/gpu/0058-drm-imagination-apple-Signal-completed-jobs-before-t.patch @@ -1,4 +1,4 @@ -From a6c0ea12f1712e9c17a642867025f7612fcef9cd Mon Sep 17 00:00:00 2001 +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= Date: Mon, 16 Nov 2026 17:45:06 +0100 Subject: [PATCH] drm/imagination: apple: Signal completed jobs before touching @@ -20,22 +20,22 @@ Assisted-by: LLM 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 5a43e3f..6c72c88 100644 +index 5badeebcbe..1556ba5a07 100644 --- a/drivers/gpu/drm/imagination/pvr_apple.c +++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -759,9 +759,7 @@ static void pvr_apple_poll_work(struct work_struct *work) - bool pending = false; - int engine; +@@ -1256,9 +1256,7 @@ static void pvr_apple_poll_work(struct work_struct *work) + if (READ_ONCE(apple->stopping)) + return; - if (apple->chip->events) - apple->chip->events(apple); - else + if (!apple->chip->events) pvr_apple_ack_events(apple); - spin_lock_irq(&apple->fence_lock); - for (engine = 0; engine < APPLE_ENGINES; engine++) { -@@ -795,6 +793,11 @@ static void pvr_apple_poll_work(struct work_struct *work) - } + if (atomic_xchg(&apple->fault_pending, 0)) + pvr_apple_fault_pending(apple); +@@ -1302,6 +1300,11 @@ static void pvr_apple_poll_work(struct work_struct *work) + irq_on = apple->fw_irq_on; spin_unlock_irq(&apple->fence_lock); pvr_apple_jobs_retired(apple, retired); + /* Completion stamps need no touch. Signal first, then let S8003 @@ -44,5 +44,5 @@ index 5a43e3f..6c72c88 100644 + if (apple->chip->events) + apple->chip->events(apple); - /* The firmware sends no completion message in this setup: poll. */ - if (pending) + if (!pending || READ_ONCE(apple->stopping)) + return; diff --git a/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch b/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch new file mode 100644 index 00000000..334c9e57 --- /dev/null +++ b/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch @@ -0,0 +1,97 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Henrik Klee +Date: Tue, 17 Nov 2026 17:47:46 +0800 +Subject: [PATCH] drm/imagination: Order submissions per device + +The submission order lock became one static mutex for every PowerVR +device in the system, so every submission of every process and device +waited for every other. Its job is to make one device's submission whole +and nest its VM locks: make it a member of the device. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_device.c | 4 ++++ + drivers/gpu/drm/imagination/pvr_device.h | 6 ++++++ + drivers/gpu/drm/imagination/pvr_job.c | 9 +++------ + 3 files changed, 13 insertions(+), 6 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c +index c160763a3a..81c0b4f095 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.c ++++ b/drivers/gpu/drm/imagination/pvr_device.c +@@ -13,6 +13,7 @@ + #include "pvr_stream.h" + #include "pvr_vm.h" + ++#include + #include + + #include +@@ -805,6 +806,9 @@ pvr_device_init(struct pvr_device *pvr_dev) + struct device *dev = drm_dev->dev; + int err; + ++ err = drmm_mutex_init(drm_dev, &pvr_dev->submit_order); ++ if (err) ++ return err; + spin_lock_init(&pvr_dev->gem_pin_lock); + INIT_LIST_HEAD(&pvr_dev->gem_pin_accounts); + +diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h +index 24b4dd03d5..e5414e8ca4 100644 +--- a/drivers/gpu/drm/imagination/pvr_device.h ++++ b/drivers/gpu/drm/imagination/pvr_device.h +@@ -94,6 +94,12 @@ struct pvr_device { + */ + struct drm_device base; + ++ /** ++ * @submit_order: Makes a submission whole and nests its VM locks ++ * outside reservation locking and job arming (pvr_submit_jobs()). ++ */ ++ struct mutex submit_order; ++ + /** @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. */ +diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c +index 0d9b9aaf29..d4e7e8869c 100644 +--- a/drivers/gpu/drm/imagination/pvr_job.c ++++ b/drivers/gpu/drm/imagination/pvr_job.c +@@ -740,9 +740,6 @@ pvr_jobs_link_geom_frag(struct pvr_job_data *job_data, u32 *job_count) + * * -%EINVAL on invalid arguments, or + * * Any other error. + */ +-/* Nest all submission VM locks outside reservation locking and job arming. */ +-static DEFINE_MUTEX(pvr_submit_order); +- + int + pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + struct drm_pvr_ioctl_submit_jobs_args *args) +@@ -801,7 +798,7 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + err = -ENOMEM; + goto out_exec_fini_unlocked; + } +- err = mutex_lock_interruptible(&pvr_submit_order); ++ err = mutex_lock_interruptible(&pvr_dev->submit_order); + if (err) + goto out_exec_fini_unlocked; + for (u32 i = 0; i < jobs_alloced; i++) { +@@ -812,7 +809,7 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + ; + if (j != vm_count) + continue; +- pvr_vm_submit_lock(vm, &pvr_submit_order); ++ pvr_vm_submit_lock(vm, &pvr_dev->submit_order); + vms[vm_count++] = vm; + } + +@@ -868,7 +865,7 @@ pvr_submit_jobs(struct pvr_device *pvr_dev, struct pvr_file *pvr_file, + drm_exec_fini(&exec); + while (vm_count) + pvr_vm_submit_unlock(vms[--vm_count]); +- mutex_unlock(&pvr_submit_order); ++ mutex_unlock(&pvr_dev->submit_order); + goto out_sync_cleanup; + out_exec_fini_unlocked: + drm_exec_fini(&exec); diff --git a/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allocate-in-the-job-worker-as-.patch b/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allocate-in-the-job-worker-as-.patch new file mode 100644 index 00000000..10cfa398 --- /dev/null +++ b/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allocate-in-the-job-worker-as-.patch @@ -0,0 +1,66 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Henrik Klee +Date: Tue, 17 Nov 2026 17:47:55 +0800 +Subject: [PATCH] drm/imagination: apple: Allocate in the job worker as before + without eviction + +Keeping reclaim out of the job worker is needed only where the shrinker +can evict, and so wait for this worker's jobs: a chip with a vm_evictable +hook. On the others (the A10) it only made job submission fail under +memory pressure. Allocate there as before. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 18 ++++++++++++++---- + 1 file changed, 14 insertions(+), 4 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 1556ba5a07..425105fb08 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -1437,6 +1437,16 @@ static struct dma_fence *pvr_apple_job_fence(struct pvr_apple *apple, struct pvr + return &fence->base; + } + ++/* ++ * The job worker may allocate only without reclaim on a chip whose BOs can ++ * be evicted: the shrinker can then wait for the jobs this worker runs. ++ * Other chips allocate there as before. ++ */ ++static gfp_t pvr_apple_job_gfp(struct pvr_apple *apple) ++{ ++ return apple->chip->vm_evictable ? GFP_NOWAIT : GFP_KERNEL; ++} ++ + struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, + struct pvr_vm_context *vm_ctx, u64 stream, + u64 ctx_state) +@@ -1444,7 +1454,7 @@ struct dma_fence *pvr_apple_submit_compute_job(struct pvr_device *pvr_dev, + struct pvr_apple *apple = to_apple(pvr_dev); + struct pvr_apple_fence *fence; + +- fence = kzalloc(sizeof(*fence), GFP_NOWAIT); ++ fence = kzalloc(sizeof(*fence), pvr_apple_job_gfp(apple)); + if (!fence) + return ERR_PTR(-ENOMEM); + return pvr_apple_job_fence(apple, fence, APPLE_ENGINE_COMPUTE, +@@ -1692,8 +1702,8 @@ struct dma_fence *pvr_apple_submit_render_job(struct pvr_job *job) + if (apple->chip->submit) + return pvr_apple_submit_job(job); + +- fence = kzalloc(sizeof(*fence), GFP_NOWAIT); +- hw = kmalloc(apple->chip->render_job_size, GFP_NOWAIT); ++ fence = kzalloc(sizeof(*fence), pvr_apple_job_gfp(apple)); ++ hw = kmalloc(apple->chip->render_job_size, pvr_apple_job_gfp(apple)); + if (!fence || !hw) { + kfree(fence); + kfree(hw); +@@ -1727,7 +1737,7 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) + APPLE_ENGINE_COMPUTE; + struct pvr_apple_fence *fence; + +- fence = kzalloc(sizeof(*fence), GFP_NOWAIT); ++ fence = kzalloc(sizeof(*fence), pvr_apple_job_gfp(apple)); + if (!fence) + return ERR_PTR(-ENOMEM); + return pvr_apple_job_fence(apple, fence, engine, apple->chip->submit(apple, job)); diff --git a/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch b/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch deleted file mode 100644 index ebfe1d2d..00000000 --- a/kernel/patches/drivers/gpu/0060-drm-imagination-apple-Allow-opt-in-per-VM-S8003-scene-overlap.patch +++ /dev/null @@ -1,194 +0,0 @@ -From bbd8003115717b35d347c9f40a8807ba71e9e8ba Mon Sep 17 00:00:00 2001 -From: =?UTF-8?q?Berkay=20K=C3=B6sebay?= -Date: Mon, 16 Nov 2026 17:45:47 +0100 -Subject: [PATCH] drm/imagination: apple: Allow opt-in per-VM S8003 scene - overlap - -Composited S8003 workloads serialize one client's geometry behind another -client's fragment, even though their PM sets are private to their VMs. - -Keep each VM's last two fragment fences and add apple_vm_overlap, default -off. When enabled on a chip with per-VM scenes, geometry waits on its own -VM's previous fragment (or last fragment for one scene). Keep the global -scene dependencies with the option off and for chips without per-VM sets. -Reference and release the per-VM history with the existing VM lifetime. - -Affects: Apple A9 (S8003). T8010 has no per-VM scene hook and keeps its -global dependencies regardless of the parameter. - -Assisted-by: LLM ---- - drivers/gpu/drm/imagination/pvr_apple.c | 61 +++++++++++++++++++- - drivers/gpu/drm/imagination/pvr_apple_chip.h | 10 +++- - 2 files changed, 67 insertions(+), 4 deletions(-) - -diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c -index 6c72c88..bcfc2e4 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple.c -+++ b/drivers/gpu/drm/imagination/pvr_apple.c -@@ -64,6 +64,10 @@ MODULE_PARM_DESC(apple_poll_us, "Apple A10: microseconds between job completion - /* Idle time before runtime PM powers the GPU off; tunable in sysfs. */ - #define PVR_APPLE_AUTOSUSPEND_MS 1000 - -+static bool pvr_apple_vm_overlap; -+module_param_named(apple_vm_overlap, pvr_apple_vm_overlap, bool, 0644); -+MODULE_PARM_DESC(apple_vm_overlap, "Apple per-VM scene overlap (default 0)"); -+ - static unsigned int pvr_apple_perf_state; - module_param_named(apple_perf_state, pvr_apple_perf_state, uint, 0444); - MODULE_PARM_DESC(apple_perf_state, -@@ -325,6 +329,7 @@ static int pvr_apple_power_init(struct pvr_device *pvr_dev) - spin_lock_init(&apple->event_lock); - for (i = 0; i < APPLE_ENGINES; i++) - INIT_LIST_HEAD(&apple->pending[i]); -+ xa_init(&apple->vm_frags); - INIT_DELAYED_WORK(&apple->poll_work, pvr_apple_poll_work); - hrtimer_setup(&apple->poll_timer, pvr_apple_poll_timer, CLOCK_MONOTONIC, HRTIMER_MODE_REL); - apple->fence_context = dma_fence_context_alloc(APPLE_ENGINES); -@@ -850,12 +855,23 @@ void pvr_apple_report_hang(struct pvr_device *pvr_dev, struct dma_fence *fence) - /* No job is ordered after the scenes submitted so far any more */ - static void pvr_apple_forget_scenes(struct pvr_apple *apple) - { -+ struct pvr_apple_vm_frags *frags; -+ unsigned long index; -+ -+ spin_lock_irq(&apple->fence_lock); -+ xa_for_each(&apple->vm_frags, index, frags) { -+ dma_fence_put(frags->last); -+ dma_fence_put(frags->prev); -+ frags->last = NULL; -+ frags->prev = NULL; -+ } - dma_fence_put(apple->last_frag); - apple->last_frag = NULL; - dma_fence_put(apple->prev_frag); - apple->prev_frag = NULL; - dma_fence_put(apple->last_geom); - apple->last_geom = NULL; -+ spin_unlock_irq(&apple->fence_lock); - } - - static unsigned int pvr_apple_cancel_fences(struct pvr_apple *apple, int error) -@@ -1038,6 +1054,18 @@ void pvr_apple_vm_fini(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx - - if (vm_ctx == pvr_dev->kernel_vm_ctx) - return; -+ { -+ struct pvr_apple_vm_frags *frags; -+ -+ spin_lock_irq(&apple->fence_lock); -+ frags = xa_erase(&apple->vm_frags, (unsigned long)vm_ctx); -+ spin_unlock_irq(&apple->fence_lock); -+ if (frags) { -+ dma_fence_put(frags->last); -+ dma_fence_put(frags->prev); -+ kfree(frags); -+ } -+ } - if (apple->chip->vm_fini) - apple->chip->vm_fini(apple, vm_ctx); - if (!apple->booted) -@@ -1224,7 +1252,9 @@ struct dma_fence *pvr_apple_submit_job(struct pvr_job *job) - * &pvr_apple_chip.vm_scenes): a geometry job then waits for the fragment job - * before the last one if the last one is its own VM's, and a fragment job - * for its geometry job to be submitted rather than finished -- * (pvr_apple_job_armed()). -+ * (pvr_apple_job_armed()). With apple_vm_overlap on, a chip with per-VM -+ * scenes takes only this VM's previous fragment (or last for one scene), -+ * as each VM owns its PM sets; the global timelines remain for A10/knob 0. - * - * Return: 0 or a negative error. - */ -@@ -1240,9 +1270,14 @@ int pvr_apple_job_add_deps(struct pvr_job *job) - /* 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) -+ 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 && -+ } else if (apple->chip->vm_scenes && READ_ONCE(pvr_apple_vm_overlap)) { -+ struct pvr_apple_vm_frags *frags = xa_load(&apple->vm_frags, -+ (unsigned long)job->ctx->vm_ctx); -+ -+ last = frags ? (scenes > 1 ? frags->prev : frags->last) : NULL; -+ } else if (apple->chip->scenes > 1 && apple->last_frag_vm == job->ctx->vm_ctx && - scenes > 1) - last = apple->prev_frag; - else -@@ -1276,6 +1311,14 @@ void pvr_apple_job_armed(struct pvr_job *job, struct dma_fence *finished) - old = *last; - *last = dma_fence_get(finished); - if (job->type == DRM_PVR_JOB_TYPE_FRAGMENT) { -+ struct pvr_apple_vm_frags *frags = xa_load(&apple->vm_frags, -+ (unsigned long)job->ctx->vm_ctx); -+ -+ if (frags) { -+ dma_fence_put(frags->prev); -+ frags->prev = frags->last; -+ frags->last = dma_fence_get(finished); -+ } - apple->last_frag_vm = job->ctx->vm_ctx; - if (joint) - swap(old, apple->prev_frag); -@@ -1300,6 +1343,18 @@ int pvr_apple_vm_init(struct pvr_device *pvr_dev, struct pvr_vm_context *vm_ctx) - struct pvr_apple *apple = to_apple(pvr_dev); - int err; - -+ if (apple->chip->vm_scenes) { -+ struct pvr_apple_vm_frags *frags = kzalloc(sizeof(*frags), GFP_KERNEL); -+ -+ if (!frags) -+ return -ENOMEM; -+ err = xa_err(xa_store(&apple->vm_frags, (unsigned long)vm_ctx, -+ frags, GFP_KERNEL)); -+ if (err) { -+ kfree(frags); -+ return err; -+ } -+ } - err = pvr_vm_map_reserved(vm_ctx, apple->data.obj, PVR_APPLE_GPU_DATA, - PVR_DEVICE_PAGE_SIZE); - if (!err) -diff --git a/drivers/gpu/drm/imagination/pvr_apple_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h -index d524415..2ef5208 100644 ---- a/drivers/gpu/drm/imagination/pvr_apple_chip.h -+++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h -@@ -26,6 +26,7 @@ - #include - #include - #include -+#include - - struct devfreq; - struct pvr_gem_object; -@@ -74,6 +75,10 @@ struct pvr_apple_layout { - u32 event_size; - }; - -+struct pvr_apple_vm_frags { -+ struct dma_fence *last, *prev; -+}; -+ - struct pvr_apple { - struct pvr_device *pvr_dev; - const struct pvr_apple_chip *chip; -@@ -120,6 +125,7 @@ struct pvr_apple { - * Chips with &pvr_apple_chip.scenes of 2: the fragment job before - * @last_frag, and the VM @last_frag runs in. - */ -+ struct xarray vm_frags; /* per-VM last/previous fragment fences, fence_lock */ - struct dma_fence *prev_frag; - const struct pvr_vm_context *last_frag_vm; - -@@ -208,7 +214,9 @@ struct pvr_apple { - * @scenes: Scenes (a geometry job and its fragment job) one VM may have in the - * firmware at a time; 0 is 1. Above 1 a fragment job is submitted as soon - * as its geometry job was, not when that finished: the firmware orders -- * the two. Scenes of different VMs never overlap. -+ * the two. With @vm_scenes, scenes of different VMs may overlap: each -+ * VM owns its PM sets. AGXWorkQueue_initAndAttachPM has one PM per queue; -+ * AGXTACommandDescriptor_preparePMAndWork waits only on that PM's stamps. - * @own_pm_pages: The chip allocates the parameter manager's pages itself; a - * free list has none (pvr_apple_free_list_has_pages()). - * @gate_off_ms: How long the GPU's power domain stays off before the firmware From a9a9f2167f8971dcdfcbb742835464a2b42ffc9f Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Tue, 17 Nov 2026 17:59:18 +0800 Subject: [PATCH 48/49] kernel: A10 crash recovery, no BO cap, trimmed GPU diagnostics - gpu/0061: the T8010's RTKit crash callback latches the same shared fault as the S8003's fault event: waits end at once and the GPU resets within milliseconds instead of after the 5 s job timeout. - gpu/0038 (BO memory cap) goes. It charged a BO's full size at creation, which no longer measures memory now that userspace BOs are backed on first use and idle ones are evicted to swap (zram on every install). It refused legitimate large buffers (GravityMark) and put a chip check into common code. - gpu/0062: apple_jobs keeps the hang state; runtime PM timings, poll, interrupt, cache-touch and PM record counters and the per-VM pool log line go. Assisted-by: LLM --- ...Cap-native-BO-memory-by-device-and-c.patch | 255 ------------ ...Take-userspace-BO-pages-on-first-use.patch | 33 +- ...-Don-t-pin-BO-pages-for-CPU-mappings.patch | 8 +- ...Evict-idle-BOs-under-memory-pressure.patch | 64 +-- ...ination-Order-submissions-per-device.patch | 21 +- ...apple-Recover-from-A10-firmware-cras.patch | 244 +++++++++++ ...apple-Keep-only-the-hang-state-in-de.patch | 390 ++++++++++++++++++ 7 files changed, 694 insertions(+), 321 deletions(-) delete mode 100644 kernel/patches/drivers/gpu/0038-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch create mode 100644 kernel/patches/drivers/gpu/0061-drm-imagination-apple-Recover-from-A10-firmware-cras.patch create mode 100644 kernel/patches/drivers/gpu/0062-drm-imagination-apple-Keep-only-the-hang-state-in-de.patch diff --git a/kernel/patches/drivers/gpu/0038-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 deleted file mode 100644 index 55ad4a6a..00000000 --- a/kernel/patches/drivers/gpu/0038-drm-imagination-Cap-native-BO-memory-by-device-and-c.patch +++ /dev/null @@ -1,255 +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 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. 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 | 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 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) - 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 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 { - */ - 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 817b47df763fd8dc0e3603360f8fabbe1b672bb5..44626d14f92e85b145f9447b952202eb068fd402 100644 ---- a/drivers/gpu/drm/imagination/pvr_gem.c -+++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -17,17 +17,147 @@ - #include - #include - #include -+#include -+#include - #include -+#include -+#include -+#include -+#include - #include - #include - #include - #include - -+/* Creation-time reservation; resident-page accounting can replace this later. */ -+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 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 { -+ struct list_head link; -+ struct pid *tgid; -+ u64 bytes; -+ 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); -+ 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; -+ 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_pct && (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 +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; - -+ /* 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 +533,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 efcd270ffa5a75e035d81a9dd87325d9bc7d3eb7..2e7cf2c9a6a435f3a1a0b06d6c3927b6296c8782 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, diff --git a/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch b/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch index 3719743d..da3ce3e8 100644 --- a/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch +++ b/kernel/patches/drivers/gpu/0051-drm-imagination-Take-userspace-BO-pages-on-first-use.patch @@ -23,9 +23,9 @@ Affects: every PowerVR device Assisted-by: LLM --- drivers/gpu/drm/imagination/pvr_drv.c | 2 +- - drivers/gpu/drm/imagination/pvr_gem.c | 84 +++++++++++++++++++++++++-- - drivers/gpu/drm/imagination/pvr_gem.h | 16 ++--- - 3 files changed, 89 insertions(+), 13 deletions(-) + drivers/gpu/drm/imagination/pvr_gem.c | 79 +++++++++++++++++++++++++-- + drivers/gpu/drm/imagination/pvr_gem.h | 16 +++--- + 3 files changed, 85 insertions(+), 12 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_drv.c b/drivers/gpu/drm/imagination/pvr_drv.c index 7ad0b71f81..f27d9b3151 100644 @@ -41,10 +41,10 @@ index 7ad0b71f81..f27d9b3151 100644 err = PTR_ERR(pvr_obj); goto err_drm_dev_exit; diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c -index 44626d14f9..cca2f00ba0 100644 +index 817b47df76..0fffab7190 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -357,6 +357,10 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) +@@ -227,6 +227,10 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) dma_resv_lock(obj->resv, NULL); @@ -55,7 +55,7 @@ index 44626d14f9..cca2f00ba0 100644 err = drm_gem_shmem_vmap_locked(shmem_obj, &map); if (err) goto err_unlock; -@@ -487,8 +491,8 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t +@@ -357,8 +361,8 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t * * -%ENOMEM if sufficient physical memory cannot be allocated, or * * Any other error returned by drm_gem_create_mmap_offset(). */ @@ -66,19 +66,10 @@ index 44626d14f9..cca2f00ba0 100644 { struct drm_device *drm_dev = from_pvr_device(pvr_dev); struct drm_gem_shmem_object *shmem_obj; -@@ -511,12 +515,18 @@ pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags) +@@ -381,7 +385,10 @@ 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. */ -+ /* -+ * Imports also use gem_create_object; only native BOs are charged, by -+ * size, here. A userspace BO is charged before it has any backing. -+ */ - err = pvr_gem_pin_charge(pvr_obj); - if (err) - goto err_shmem_object_free; - - sgt = drm_gem_shmem_get_pages_sgt(shmem_obj); + if (!(flags & PVR_BO_KERNEL_PINNED)) + return pvr_obj; @@ -87,7 +78,7 @@ index 44626d14f9..cca2f00ba0 100644 if (IS_ERR(sgt)) { err = PTR_ERR(sgt); goto err_shmem_object_free; -@@ -558,7 +568,8 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, +@@ -423,7 +430,8 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, struct scatterlist *sgl; unsigned int sgt_idx; @@ -97,7 +88,7 @@ index 44626d14f9..cca2f00ba0 100644 for_each_sgtable_dma_sg(shmem_obj->sgt, sgl, sgt_idx) { u32 new_offset = accumulated_offset + sg_dma_len(sgl); -@@ -573,3 +584,66 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, +@@ -438,3 +446,66 @@ pvr_gem_get_dma_addr(struct pvr_gem_object *pvr_obj, u32 offset, return -EINVAL; } @@ -165,10 +156,10 @@ index 44626d14f9..cca2f00ba0 100644 + return sgt; +} diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index 2e7cf2c9a6..ac9a38301a 100644 +index efcd270ffa..07bd602e93 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h -@@ -64,10 +64,12 @@ struct pvr_gem_account; +@@ -63,10 +63,12 @@ struct pvr_file; #define PVR_BO_DEVICE_CACHE_COHERENT BIT_ULL(61) @@ -183,7 +174,7 @@ index 2e7cf2c9a6..ac9a38301a 100644 /* Bits 2..0 are defined in the UAPI. */ /* Other utilities. */ -@@ -133,16 +135,16 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t +@@ -129,16 +131,16 @@ struct drm_gem_object *pvr_gem_create_object(struct drm_device *drm_dev, size_t struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, size_t size, u64 flags); diff --git a/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch b/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch index 48ff05f9..f19bcf7f 100644 --- a/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch +++ b/kernel/patches/drivers/gpu/0052-drm-imagination-Don-t-pin-BO-pages-for-CPU-mappings.patch @@ -19,10 +19,10 @@ Assisted-by: LLM 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_gem.c b/drivers/gpu/drm/imagination/pvr_gem.c -index cca2f00ba0..ff912e039b 100644 +index 0fffab7190..0129c8d9e2 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -179,7 +179,16 @@ static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *v +@@ -49,7 +49,16 @@ static int pvr_gem_mmap(struct drm_gem_object *gem_obj, struct vm_area_struct *v if (!(pvr_obj->flags & DRM_PVR_BO_ALLOW_CPU_USERSPACE_ACCESS)) return -EINVAL; @@ -40,7 +40,7 @@ index cca2f00ba0..ff912e039b 100644 } static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *obj) -@@ -192,6 +201,35 @@ static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *o +@@ -62,6 +71,35 @@ static enum drm_gem_object_status pvr_gem_object_status(struct drm_gem_object *o return 0; } @@ -76,7 +76,7 @@ index cca2f00ba0..ff912e039b 100644 static const struct drm_gem_object_funcs pvr_gem_object_funcs = { .free = pvr_gem_object_free, .status = pvr_gem_object_status, -@@ -203,7 +241,7 @@ static const struct drm_gem_object_funcs pvr_gem_object_funcs = { +@@ -73,7 +111,7 @@ static const struct drm_gem_object_funcs pvr_gem_object_funcs = { .vmap = drm_gem_shmem_object_vmap, .vunmap = drm_gem_shmem_object_vunmap, .mmap = pvr_gem_mmap, diff --git a/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch b/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch index 770fdd53..ac88e343 100644 --- a/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch +++ b/kernel/patches/drivers/gpu/0053-drm-imagination-Evict-idle-BOs-under-memory-pressure.patch @@ -44,15 +44,15 @@ Assisted-by: LLM drivers/gpu/drm/imagination/pvr_apple_s8003.c | 21 ++ drivers/gpu/drm/imagination/pvr_debugfs.c | 2 + drivers/gpu/drm/imagination/pvr_device.c | 7 + - drivers/gpu/drm/imagination/pvr_device.h | 16 ++ + drivers/gpu/drm/imagination/pvr_device.h | 17 ++ drivers/gpu/drm/imagination/pvr_gem.c | 81 ++++++- - drivers/gpu/drm/imagination/pvr_gem.h | 10 + + drivers/gpu/drm/imagination/pvr_gem.h | 11 + .../gpu/drm/imagination/pvr_gem_shrinker.c | 223 ++++++++++++++++++ drivers/gpu/drm/imagination/pvr_mmu.c | 18 ++ drivers/gpu/drm/imagination/pvr_mmu.h | 2 + drivers/gpu/drm/imagination/pvr_vm.c | 100 ++++++++ drivers/gpu/drm/imagination/pvr_vm.h | 2 + - 15 files changed, 498 insertions(+), 6 deletions(-) + 15 files changed, 500 insertions(+), 6 deletions(-) create mode 100644 drivers/gpu/drm/imagination/pvr_gem_shrinker.c diff --git a/drivers/gpu/drm/imagination/Makefile b/drivers/gpu/drm/imagination/Makefile @@ -185,7 +185,7 @@ index 21c5f0de4d..8151d81228 100644 for (size_t i = 0; i < ARRAY_SIZE(pvr_debugfs_entries); ++i) { const struct pvr_debugfs_entry *entry = &pvr_debugfs_entries[i]; diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c -index c13a1b6489..c160763a3a 100644 +index 2c06a4ac11..8c1bb336d3 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -3,6 +3,7 @@ @@ -196,7 +196,7 @@ index c13a1b6489..c160763a3a 100644 #include "pvr_device_info.h" #include "pvr_fw.h" -@@ -810,6 +811,10 @@ pvr_device_init(struct pvr_device *pvr_dev) +@@ -807,6 +808,10 @@ pvr_device_init(struct pvr_device *pvr_dev) /* Get the platform-specific data based on the compatible string. */ pvr_dev->device_data = of_device_get_match_data(dev); @@ -207,7 +207,7 @@ index c13a1b6489..c160763a3a 100644 /* Enable and initialize clocks required for the device to operate. */ err = pvr_device_clk_init(pvr_dev); if (err) -@@ -861,6 +866,8 @@ pvr_device_init(struct pvr_device *pvr_dev) +@@ -858,6 +863,8 @@ pvr_device_init(struct pvr_device *pvr_dev) void pvr_device_fini(struct pvr_device *pvr_dev) { @@ -217,7 +217,7 @@ index c13a1b6489..c160763a3a 100644 * Deinitialization stages are performed in reverse order compared to * the initialization stages in pvr_device_init(). diff --git a/drivers/gpu/drm/imagination/pvr_device.h b/drivers/gpu/drm/imagination/pvr_device.h -index da999d480b..24b4dd03d5 100644 +index 26b589ee72..015719016a 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -11,6 +11,8 @@ @@ -229,10 +229,10 @@ index da999d480b..24b4dd03d5 100644 #include #include -@@ -98,6 +100,20 @@ struct pvr_device { - struct list_head gem_pin_accounts; - /** @gem_pin_bytes: Native BO bytes reserved before backing pages exist. */ - u64 gem_pin_bytes; +@@ -92,6 +94,21 @@ struct pvr_device { + */ + struct drm_device base; + + /** @gem_reclaim: GEM residency, using base.gem_lru_mutex for all LRUs. */ + struct { + struct drm_gem_lru evictable; @@ -247,22 +247,23 @@ index da999d480b..24b4dd03d5 100644 + atomic64_t scans, evicts, purges, restores, restore_fail; + atomic64_t skip[12]; + } gem_reclaim; - ++ /** @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 ff912e039b..f21a91a716 100644 +index 0129c8d9e2..cf061a5d5e 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.c +++ b/drivers/gpu/drm/imagination/pvr_gem.c -@@ -24,6 +24,7 @@ - #include - #include - #include +@@ -18,6 +18,7 @@ + #include + #include + #include +#include #include #include #include -@@ -156,6 +157,11 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) +@@ -27,6 +28,11 @@ static void pvr_gem_object_free(struct drm_gem_object *obj) { struct drm_gem_shmem_object *shmem_obj = to_drm_gem_shmem_obj(obj); @@ -272,9 +273,9 @@ index ff912e039b..f21a91a716 100644 + if (bo->pages_evicted) + atomic64_sub(obj->size, &to_pvr_device(obj->dev)->gem_reclaim.evicted_bytes); shmem_obj->pages_mark_dirty_on_put = true; - pvr_gem_pin_uncharge(gem_to_pvr_gem(obj)); drm_gem_shmem_free(shmem_obj); -@@ -168,6 +174,11 @@ static struct dma_buf *pvr_gem_export(struct drm_gem_object *obj, int flags) + } +@@ -38,6 +44,11 @@ static struct dma_buf *pvr_gem_export(struct drm_gem_object *obj, int flags) if (pvr_obj->flags & DRM_PVR_BO_PM_FW_PROTECT) return ERR_PTR(-EPERM); @@ -286,7 +287,7 @@ index ff912e039b..f21a91a716 100644 return drm_gem_prime_export(obj, flags); } -@@ -230,16 +241,54 @@ static const struct vm_operations_struct pvr_gem_vm_ops = { +@@ -100,16 +111,54 @@ static const struct vm_operations_struct pvr_gem_vm_ops = { .close = drm_gem_vm_close, }; @@ -345,7 +346,7 @@ index ff912e039b..f21a91a716 100644 .mmap = pvr_gem_mmap, .vm_ops = &pvr_gem_vm_ops, }; -@@ -399,7 +448,7 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) +@@ -269,7 +318,7 @@ pvr_gem_object_vmap(struct pvr_gem_object *pvr_obj) if (err) goto err_unlock; @@ -354,7 +355,7 @@ index ff912e039b..f21a91a716 100644 if (err) goto err_unlock; -@@ -453,7 +502,7 @@ pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) +@@ -323,7 +372,7 @@ pvr_gem_object_vunmap(struct pvr_gem_object *pvr_obj) dma_sync_sgtable_for_device(dev, shmem_obj->sgt, DMA_BIDIRECTIONAL); } @@ -363,7 +364,7 @@ index ff912e039b..f21a91a716 100644 dma_resv_unlock(obj->resv); } -@@ -667,8 +716,14 @@ struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) +@@ -529,8 +578,14 @@ struct sg_table *pvr_gem_get_pages_sgt_locked(struct pvr_gem_object *pvr_obj) /* Transfer the temporary pin's page reference to the sgt. */ refcount_inc(&shmem->pages_use_count); shmem->sgt = sgt; @@ -378,7 +379,7 @@ index ff912e039b..f21a91a716 100644 return err ? ERR_PTR(err) : sgt; } -@@ -685,3 +740,17 @@ struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) +@@ -547,3 +602,17 @@ struct sg_table *pvr_gem_object_get_pages_sgt(struct pvr_gem_object *pvr_obj) dma_resv_unlock(obj->resv); return sgt; } @@ -397,21 +398,22 @@ index ff912e039b..f21a91a716 100644 + drm_gem_lru_remove(obj); +} diff --git a/drivers/gpu/drm/imagination/pvr_gem.h b/drivers/gpu/drm/imagination/pvr_gem.h -index ac9a38301a..efb40d6b0b 100644 +index 07bd602e93..c21b8e77ee 100644 --- a/drivers/gpu/drm/imagination/pvr_gem.h +++ b/drivers/gpu/drm/imagination/pvr_gem.h -@@ -114,6 +114,10 @@ struct pvr_gem_object { +@@ -111,6 +111,11 @@ struct pvr_gem_object { + */ + u64 flags; - /** @pin_account: Creating process's budget, retained until the BO is freed. */ - struct pvr_gem_account *pin_account; + /** @exported: Latched before publishing a dma-buf, under the reservation. */ + bool exported; + /** @pages_evicted: Backing pages released; protected by the reservation. */ + bool pages_evicted; - ++ }; -@@ -137,6 +141,12 @@ struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, + static_assert(offsetof(struct pvr_gem_object, base) == 0, +@@ -133,6 +138,12 @@ struct pvr_gem_object *pvr_gem_object_create(struct pvr_device *pvr_dev, struct pvr_gem_object *pvr_gem_object_create_user(struct pvr_device *pvr_dev, size_t size, u64 flags); diff --git a/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch b/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch index 334c9e57..954bfd8f 100644 --- a/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch +++ b/kernel/patches/drivers/gpu/0059-drm-imagination-Order-submissions-per-device.patch @@ -10,13 +10,13 @@ and nest its VM locks: make it a member of the device. Assisted-by: LLM --- - drivers/gpu/drm/imagination/pvr_device.c | 4 ++++ + drivers/gpu/drm/imagination/pvr_device.c | 5 +++++ drivers/gpu/drm/imagination/pvr_device.h | 6 ++++++ drivers/gpu/drm/imagination/pvr_job.c | 9 +++------ - 3 files changed, 13 insertions(+), 6 deletions(-) + 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/drivers/gpu/drm/imagination/pvr_device.c b/drivers/gpu/drm/imagination/pvr_device.c -index c160763a3a..81c0b4f095 100644 +index 8c1bb336d3..e5065f8042 100644 --- a/drivers/gpu/drm/imagination/pvr_device.c +++ b/drivers/gpu/drm/imagination/pvr_device.c @@ -13,6 +13,7 @@ @@ -27,18 +27,19 @@ index c160763a3a..81c0b4f095 100644 #include #include -@@ -805,6 +806,9 @@ pvr_device_init(struct pvr_device *pvr_dev) +@@ -805,6 +806,10 @@ pvr_device_init(struct pvr_device *pvr_dev) struct device *dev = drm_dev->dev; int err; + err = drmm_mutex_init(drm_dev, &pvr_dev->submit_order); + if (err) + return 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 24b4dd03d5..e5414e8ca4 100644 +index 015719016a..98be4c27b5 100644 --- a/drivers/gpu/drm/imagination/pvr_device.h +++ b/drivers/gpu/drm/imagination/pvr_device.h @@ -94,6 +94,12 @@ struct pvr_device { @@ -51,9 +52,9 @@ index 24b4dd03d5..e5414e8ca4 100644 + */ + struct mutex submit_order; + - /** @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. */ + /** @gem_reclaim: GEM residency, using base.gem_lru_mutex for all LRUs. */ + struct { + struct drm_gem_lru evictable; diff --git a/drivers/gpu/drm/imagination/pvr_job.c b/drivers/gpu/drm/imagination/pvr_job.c index 0d9b9aaf29..d4e7e8869c 100644 --- a/drivers/gpu/drm/imagination/pvr_job.c diff --git a/kernel/patches/drivers/gpu/0061-drm-imagination-apple-Recover-from-A10-firmware-cras.patch b/kernel/patches/drivers/gpu/0061-drm-imagination-apple-Recover-from-A10-firmware-cras.patch new file mode 100644 index 00000000..b97bff05 --- /dev/null +++ b/kernel/patches/drivers/gpu/0061-drm-imagination-apple-Recover-from-A10-firmware-cras.patch @@ -0,0 +1,244 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Henrik Klee +Date: Tue, 17 Nov 2026 17:54:54 +0800 +Subject: [PATCH] drm/imagination: apple: Recover from A10 firmware crashes as + from A9 faults + +The S8003 latched a firmware fault in its own data: its waits ended at +once and the oldest bound job's scheduler reset the GPU within +milliseconds. The T8010 reports the same condition through RTKit's +crash callback, but only logged it, and every waiter ran into its +timeout (5 s) before the GPU was reset. + +Move the latch into the shared state. pvr_apple_firmware_fault() sets it +once per firmware boot, captures the MMU state and faults the oldest +bound scheduler; the S8003 fault event and the T8010 crash callback both +call it. The shared waits end with -EIO once it is set, like the S8003's +own. The chip fault getter goes; debugfs reads the shared flag. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 32 +++++++++++++------ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 3 +- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 26 ++++----------- + drivers/gpu/drm/imagination/pvr_apple_t8010.c | 1 + + 4 files changed, 31 insertions(+), 31 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 425105fb08..1eeaeb322b 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -667,9 +667,12 @@ static u32 pvr_apple_completed_seq(struct pvr_apple *apple) + static int pvr_apple_wait(struct pvr_apple *apple, u32 seq, unsigned int timeout_us) + { + u32 done; ++ int err; + +- return read_poll_timeout(pvr_apple_completed_seq, done, (s32)(done - seq) >= 0, 1000, +- timeout_us, false, apple); ++ err = read_poll_timeout(pvr_apple_completed_seq, done, ++ (s32)(done - seq) >= 0 || READ_ONCE(apple->fault), 1000, ++ timeout_us, false, apple); ++ return !err && (s32)(done - seq) < 0 ? -EIO : err; + } + + static u32 pvr_apple_engine_done(struct pvr_apple *apple, enum pvr_apple_engine engine) +@@ -734,8 +737,7 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + 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); ++ fault = READ_ONCE(apple->fault); + spin_lock_irqsave(&apple->fence_lock, flags); + for (engine = 0; engine < APPLE_ENGINES; engine++) + list_for_each_entry(fence, &apple->pending[engine], node) +@@ -774,8 +776,7 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + 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, "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)); +@@ -812,8 +813,12 @@ static int pvr_apple_wait_engine(struct pvr_apple *apple, enum pvr_apple_engine + * switch of GPU VM, for every engine; 1 ms steps cost a scene-heavy + * WebGL page several milliseconds per frame + */ +- return read_poll_timeout(pvr_apple_engine_poll, done, (s32)(done - seq) >= 0, 20, +- timeout_us, false, apple, engine); ++ int err; ++ ++ err = read_poll_timeout(pvr_apple_engine_poll, done, ++ (s32)(done - seq) >= 0 || READ_ONCE(apple->fault), 20, ++ timeout_us, false, apple, engine); ++ return !err && (s32)(done - seq) < 0 ? -EIO : err; + } + + /* Wait until every engine retired its submitted jobs; submit_lock held */ +@@ -1053,9 +1058,17 @@ void pvr_apple_report_mmu_fault(struct pvr_apple *apple) + } + } + +-/* Called once when a chip latches a fault, after capturing its MMU state. */ ++/* ++ * A chip saw its firmware stop on a fault (an S8003 fault event, a T8010 ++ * RTKit crash). Latch it once per boot: waits for the firmware end at once, ++ * the MMU state is captured, and the oldest bound job's scheduler resets ++ * the GPU instead of waiting for its timeout. ++ */ + void pvr_apple_firmware_fault(struct pvr_apple *apple) + { ++ if (xchg(&apple->fault, true)) ++ return; ++ pvr_apple_report_mmu_fault(apple); + atomic_set(&apple->fault_pending, 1); + pvr_apple_firmware_event(apple); + } +@@ -1857,6 +1870,7 @@ static int pvr_apple_fw_start(struct pvr_apple *apple) + apple->frag_events = 0; + apple->geom_pending = false; + apple->fault_reported = false; ++ WRITE_ONCE(apple->fault, 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 687887d8f3..a95933618f 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -99,6 +99,7 @@ struct pvr_apple { + bool stopping; /* terminal shutdown forbids firmware restart */ + bool booted; + bool fault_reported; /* once per firmware boot */ ++ bool fault; /* the firmware stopped on a fault in this boot */ + bool fw_ready; /* init data handed over once: runtime PM restarts it */ + ktime_t gate_off; /* last successful runtime power-off */ + +@@ -262,7 +263,6 @@ 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. +@@ -316,7 +316,6 @@ struct pvr_apple_chip { + int (*prepare_job)(struct pvr_apple *apple, struct pvr_job *job); + 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); +diff --git a/drivers/gpu/drm/imagination/pvr_apple_s8003.c b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +index 0bbaea56f6..9584e03a5a 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -281,7 +281,6 @@ struct pvr_apple_s8003 { + struct mutex event_lock; /* the rings the firmware posts to */ + 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 */ + + /* +@@ -1017,11 +1016,7 @@ 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); +- if (!READ_ONCE(s->fault)) { +- WRITE_ONCE(s->fault, true); +- pvr_apple_report_mmu_fault(apple); +- pvr_apple_firmware_fault(apple); +- } ++ pvr_apple_firmware_fault(apple); + } + consumer = (consumer + 1) % APPLE_RING_ENTRIES; + ring_consume(ctl, consumer); +@@ -1354,12 +1349,11 @@ 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; + + err = read_poll_timeout(s8003_done_poll, done, +- (s32)(done - seq) >= 0 || READ_ONCE(s->fault), 20, ++ (s32)(done - seq) >= 0 || READ_ONCE(apple->fault), 20, + S8003_JOB_TIMEOUT_US, false, apple, engine); + if (!err && (s32)(done - seq) < 0) { + pvr_apple_report_mmu_fault(apple); +@@ -2286,7 +2280,7 @@ static bool s8003_pm_clean(struct pvr_apple *apple, const struct s8003_pm *pm, u + struct pvr_apple_s8003 *s = apple->chip_data; + + return pm->rec_seq && pm->clean && pm->built == s->gen && pm->blocks >= blocks && +- pm->scene == s->gen && pm->td_seq && !READ_ONCE(s->fault) && ++ pm->scene == s->gen && pm->td_seq && !READ_ONCE(apple->fault) && + (s32)(pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_FRAG) - pm->td_seq) >= 0; + } + +@@ -2358,7 +2352,7 @@ static bool pvr_apple_s8003_vm_evictable(struct pvr_apple *apple, + unsigned int i; + + lockdep_assert_held(&apple->submit_lock); +- if (!vm || READ_ONCE(s->fault)) ++ if (!vm || READ_ONCE(apple->fault)) + return false; + if (vm->attached != s->gen) + return true; +@@ -2751,7 +2745,7 @@ static void pvr_apple_s8003_report_hang(struct pvr_apple *apple) + } + dev_err(apple_dev(apple), + "hang: firmware %s, device control %u/%u, Fender state %#x\n", +- s->failed ? "rings unreadable" : s->fault ? "posted a fault" : "posted no fault", ++ s->failed ? "rings unreadable" : READ_ONCE(apple->fault) ? "posted a fault" : "posted no fault", + get32(dc + S8003_RING_CONSUMER), + get32(dc + S8003_RING_PRODUCER), readl(apple->fender + FENDER_POWER_STATE)); + } +@@ -2792,7 +2786,7 @@ static void s8003_jobs_reset(struct pvr_apple_s8003 *s) + for (i = 0; i < ARRAY_SIZE(vm->pm); i++) + vm->pm[i].rec_seq = 0; + WRITE_ONCE(s->failed, false); +- WRITE_ONCE(s->fault, false); ++ WRITE_ONCE(s->apple->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); +@@ -3034,13 +3028,6 @@ 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; +@@ -3082,7 +3069,6 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .prepare_job = s8003_prepare_job, + .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, +diff --git a/drivers/gpu/drm/imagination/pvr_apple_t8010.c b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +index cc841c3067..c6a05d998b 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_t8010.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_t8010.c +@@ -124,6 +124,7 @@ static void pvr_apple_crashed(void *cookie, const void *crashlog, size_t size) + if (crashlog) + print_hex_dump(KERN_ERR, "crashlog: ", DUMP_PREFIX_OFFSET, 16, 4, crashlog, size, + false); ++ pvr_apple_firmware_fault(apple); + } + + static const struct apple_rtkit_ops pvr_apple_rtkit_ops = { diff --git a/kernel/patches/drivers/gpu/0062-drm-imagination-apple-Keep-only-the-hang-state-in-de.patch b/kernel/patches/drivers/gpu/0062-drm-imagination-apple-Keep-only-the-hang-state-in-de.patch new file mode 100644 index 00000000..db3ac636 --- /dev/null +++ b/kernel/patches/drivers/gpu/0062-drm-imagination-apple-Keep-only-the-hang-state-in-de.patch @@ -0,0 +1,390 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Henrik Klee +Date: Tue, 17 Nov 2026 17:58:30 +0800 +Subject: [PATCH] drm/imagination: apple: Keep only the hang state in debugfs + +apple_jobs reported runtime PM timings, poll and interrupt counts, S8003 +cache-line touch timings, PM record and pool feedback counters, and a +log line for every new VM's first pool. They served the investigations +that led to the PM and interrupt changes and added atomics to the job +paths. Runtime PM already reports its own state in sysfs. + +Keep what a hang report needs: whether the firmware runs or faulted, +each engine's submitted, done and pending jobs, busy time, runtime PM +state and the completion interrupt's state. A pool grown from firmware +feedback logs at debug level. + +Assisted-by: LLM +--- + drivers/gpu/drm/imagination/pvr_apple.c | 80 +------------------ + drivers/gpu/drm/imagination/pvr_apple_chip.h | 21 ----- + drivers/gpu/drm/imagination/pvr_apple_s8003.c | 61 ++------------ + 3 files changed, 7 insertions(+), 155 deletions(-) + +diff --git a/drivers/gpu/drm/imagination/pvr_apple.c b/drivers/gpu/drm/imagination/pvr_apple.c +index 1eeaeb322b..2f3105c6a3 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple.c ++++ b/drivers/gpu/drm/imagination/pvr_apple.c +@@ -422,30 +422,17 @@ 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(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. +@@ -459,31 +446,12 @@ 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)) +@@ -494,8 +462,6 @@ 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. */ +@@ -525,10 +491,6 @@ 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; + } + +@@ -685,7 +647,6 @@ 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); +@@ -703,13 +664,6 @@ 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; +@@ -719,10 +673,8 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + [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; +@@ -744,15 +696,6 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + 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); +@@ -778,19 +721,7 @@ static int pvr_apple_jobs_show(struct seq_file *s, void *unused) + seq_printf(s, "retired_total %llu\nbusy_ns %llu\n", retired_total, busy_ns); + 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); ++ seq_printf(s, "fw_irq %s\n", !apple->fw_irq ? "none" : irq_on ? "on" : "off"); + + return 0; + } +@@ -1213,7 +1144,6 @@ 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); +@@ -1299,12 +1229,6 @@ 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_chip.h b/drivers/gpu/drm/imagination/pvr_apple_chip.h +index a95933618f..7ee08eae97 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_chip.h ++++ b/drivers/gpu/drm/imagination/pvr_apple_chip.h +@@ -79,13 +79,6 @@ struct pvr_apple_layout { + + 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; +@@ -165,19 +158,6 @@ struct pvr_apple { + 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 */ +@@ -326,7 +306,6 @@ struct pvr_apple_chip { + /* 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 9584e03a5a..e396067cc2 100644 +--- a/drivers/gpu/drm/imagination/pvr_apple_s8003.c ++++ b/drivers/gpu/drm/imagination/pvr_apple_s8003.c +@@ -270,7 +270,6 @@ 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 */ + u32 fb_blocks; /* pool size the firmware's feedback asked for */ + ktime_t fb_at; /* when that feedback was last taken */ + }; +@@ -311,14 +310,6 @@ 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_queued, pm_rec_unreached; +- 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 */ +@@ -1286,9 +1277,7 @@ 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); +- 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); ++ s8003_touch(s); + } + if (!READ_ONCE(s->failed) && s8003_drain(apple, &alive)) { + WRITE_ONCE(s->failed, true); +@@ -1340,9 +1329,6 @@ 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); + } +@@ -1383,9 +1369,6 @@ 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); + } +@@ -2325,7 +2308,6 @@ 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; +@@ -2336,11 +2318,9 @@ 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); ++ dev_dbg(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); + } + } + +@@ -2527,13 +2507,6 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + 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), +- "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 */ +@@ -2547,17 +2520,9 @@ static int s8003_submit_geom(struct pvr_apple *apple, struct s8003_vm *vm, struc + u32 done = pvr_apple_s8003_engine_done(apple, APPLE_ENGINE_GEOM); + + /* The last rebuild must be visible; a newer ordinary TA need not be. */ +- if ((s32)(done - pm->rec_seq) < 0) { ++ if ((s32)(done - pm->rec_seq) < 0) + pm_record = true; +- s->pm_rec_unreached++; +- } else if ((s32)(done - apple->ta_seq) < 0) { +- s->pm_clean_queued++; +- } + } +- 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), +@@ -3028,21 +2993,6 @@ static u32 pvr_apple_s8003_max_power_state(struct pvr_apple *apple) + return get32(s8003_object(s, S8003_PLATFORM) + 0x710); + } + +-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_queued %llu rec_unreached %llu\n", +- READ_ONCE(s->pm_full), READ_ONCE(s->pm_clean), +- READ_ONCE(s->pm_clean_queued), READ_ONCE(s->pm_rec_unreached)); +- 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, +@@ -3077,5 +3027,4 @@ const struct pvr_apple_chip pvr_apple_chip_s8003 = { + .vm_evictable = pvr_apple_s8003_vm_evictable, + .mmu_flush = pvr_apple_s8003_mmu_flush, + .irq_ack = pvr_apple_s8003_irq_ack, +- .debugfs_show = pvr_apple_s8003_debugfs_show, + }; From acb24606b88e017d8a0c35442db2aae9e1626b34 Mon Sep 17 00:00:00 2001 From: Henrik Klee Date: Tue, 17 Nov 2026 17:48:01 +0800 Subject: [PATCH 49/49] ci: compile the kernels a pull request changes Pull request checks never compiled a kernel, so a green check said nothing about kernel patches, configs or build scripts. On a pull request the changed job now compares each platform's kernel between the merge commit and the branch it merges into (ci/changed_kernels.py): its source key, computed by each side's own code, and the build scripts ci/reuse_kernel.py guards. The kernel job runs for the platforms that differ and leaves the others out of its matrix. A change to a shared patch layer (hoolock, pauli1go, drivers, devices) changes both apple-16k and apple-4k, which compile the same patches with different page sizes and configs, so both are built; a config change builds only its platform; apple-armv7 builds when its own layers change; documentation builds nothing. A pull request reads the compiler cache of the last main build but saves none, and archives and uploads nothing; assets, draft and nightly still never run for it. Assisted-by: LLM --- .github/workflows/build.yml | 35 +++++++++++++++-- ci/changed_kernels.py | 67 ++++++++++++++++++++++++++++++++ tests/test_ci_changed_kernels.py | 59 ++++++++++++++++++++++++++++ tests/test_ci_efficiency.py | 29 +++++++++++++- 4 files changed, 184 insertions(+), 6 deletions(-) create mode 100755 ci/changed_kernels.py create mode 100644 tests/test_ci_changed_kernels.py diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index c85aca4e..789abc77 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -58,10 +58,20 @@ jobs: - run: python -m unittest discover -s tests -v changed: # A scheduled run builds only when main moved since the last nightly. + # A pull request compiles the kernel of every platform whose sources or + # build scripts it changes (ci/changed_kernels.py), and publishes nothing. runs-on: ubuntu-24.04 outputs: nightly: ${{ steps.check.outputs.nightly }} + kernels: ${{ steps.check.outputs.kernels }} + kernel_exclude: ${{ steps.check.outputs.kernel_exclude }} steps: + # The merge commit and its first parent, the branch it merges into. + - uses: actions/checkout@v4 + if: github.event_name == 'pull_request' + with: + persist-credentials: false + fetch-depth: 2 - id: check env: GH_TOKEN: ${{ github.token }} @@ -77,6 +87,12 @@ jobs: else echo nightly=false >> "$GITHUB_OUTPUT" fi + if [[ "$EVENT" == pull_request ]]; then + git worktree add --detach "$RUNNER_TEMP/base" HEAD^1 + python3 ci/changed_kernels.py "$RUNNER_TEMP/base" apple-16k apple-4k apple-armv7 | tee -a "$GITHUB_OUTPUT" + else + printf 'kernels=[]\nkernel_exclude=[]\n' >> "$GITHUB_OUTPUT" + fi native-smoke: runs-on: ubuntu-24.04-arm env: @@ -101,7 +117,7 @@ jobs: # The compile jobs start beside the tests, not after them: a failed test costs # some compiling, and `assets` (so every draft and nightly) still waits for both. kernel: - if: github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' || needs.changed.outputs.nightly == 'true' + if: github.ref_type == 'tag' || github.event_name == 'workflow_dispatch' || needs.changed.outputs.nightly == 'true' || needs.changed.outputs.kernels != '[]' needs: changed runs-on: ubuntu-24.04-arm container: debian:bookworm @@ -110,6 +126,8 @@ jobs: matrix: # Every platform with a kernel series; distribution releases cover all of them. platform: [apple-16k, apple-4k, apple-armv7] + # A pull request leaves out the platforms whose kernel it doesn't change. + exclude: ${{ fromJSON(needs.changed.outputs.kernel_exclude) }} defaults: run: shell: bash @@ -138,9 +156,16 @@ jobs: run: bash kernel/install-toolchain # The compiler cache of this platform's last build: a changed patch then # compiles only what it changes. One cache per kernel state; a new state - # starts from the newest older one. + # starts from the newest older one. A pull request only reads it, so its + # states don't push main's out of the repository's cache space. - uses: actions/cache@v4 - if: steps.reuse.outputs.reused == '' + if: steps.reuse.outputs.reused == '' && github.event_name != 'pull_request' + with: + path: out/cache/ccache/kernel + key: kernel-ccache-v1-${{ matrix.platform }}-${{ steps.reuse.outputs.key }} + restore-keys: kernel-ccache-v1-${{ matrix.platform }}- + - uses: actions/cache/restore@v4 + if: steps.reuse.outputs.reused == '' && github.event_name == 'pull_request' with: path: out/cache/ccache/kernel key: kernel-ccache-v1-${{ matrix.platform }}-${{ steps.reuse.outputs.key }} @@ -157,12 +182,14 @@ jobs: # Keep only what this build used, so the saved cache stays one build large. ccache --evict-older-than "$(( $(date +%s) - started + 60 ))s" >/dev/null { echo "Compiler cache, $PLATFORM:"; echo '```'; ccache --show-stats; echo '```'; } >> "$GITHUB_STEP_SUMMARY" + # A pull request's kernel only has to compile; nothing assembles it. - name: Archive components and corresponding kernel source - if: steps.reuse.outputs.reused == '' + if: steps.reuse.outputs.reused == '' && github.event_name != 'pull_request' run: | tar -C out -czf "out/kernel-components-$PLATFORM.tar.gz" components tar -C out/kernel-work/source --exclude=.git --use-compress-program=pigz -cf "out/kernel-source-$PLATFORM.tar.gz" . - uses: actions/upload-artifact@v4 + if: github.event_name != 'pull_request' with: name: orchard-kernel-${{ matrix.platform }} path: | diff --git a/ci/changed_kernels.py b/ci/changed_kernels.py new file mode 100755 index 00000000..7e86183e --- /dev/null +++ b/ci/changed_kernels.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 +"""The platforms whose kernel a pull request changes, so its checks compile them. + + ci/changed_kernels.py BASE PLATFORM... + +BASE is a checkout of the commit the pull request merges into. A platform +counts as changed when its source key (orchard.published: upstream pin, +series, patches, config, toolchain recipes, producer) or one of the build +scripts the key leaves out (ci/reuse_kernel.py GUARDED) differs between BASE +and this checkout: exactly what would stop the nightly's kernel from being +reused. Each side's key comes from that side's own code, so a changed key +recipe counts too; a platform BASE cannot describe (a new one) counts as +changed. + +Prints two GitHub output lines: `kernels`, the JSON list of changed +platforms, and `kernel_exclude`, the matrix entries of the unchanged ones. +""" +import importlib.util +import json +import os +from pathlib import Path +import subprocess +import sys +import tempfile + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('reuse_kernel', ROOT / 'ci/reuse_kernel.py') +reuse_kernel = importlib.util.module_from_spec(spec) +spec.loader.exec_module(reuse_kernel) + + +def source_key(root, platform): + """PLATFORM's source key in the checkout ROOT by ROOT's own code, or None when it has none.""" + with tempfile.TemporaryDirectory(prefix='changed-kernels-') as temporary: + output = Path(temporary) / 'keys.json' + env = {**os.environ, 'PYTHONPATH': str(Path(root) / 'src')} + done = subprocess.run([sys.executable, '-m', 'orchard.published', 'keys', '--output', str(output), platform], + cwd=root, env=env, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL) + if done.returncode != 0: + return None + return json.loads(output.read_text())['platforms'][platform]['key'] + + +def changed(base, platforms, *, root=ROOT): + """The PLATFORMS whose kernel differs between the checkouts BASE and ROOT.""" + base, root = Path(base), Path(root) + if reuse_kernel.checkout_scripts(base) != reuse_kernel.checkout_scripts(root): + return list(platforms) + build = [] + for platform in platforms: + before = source_key(base, platform) + if before is None or before != source_key(root, platform): + build.append(platform) + return build + + +def main(): + if len(sys.argv) < 3: + sys.exit(__doc__.split('\n\n')[1]) + platforms = sys.argv[2:] + build = changed(sys.argv[1], platforms) + print('kernels=' + json.dumps(build)) + print('kernel_exclude=' + json.dumps([{'platform': name} for name in platforms if name not in build])) + + +if __name__ == '__main__': + main() diff --git a/tests/test_ci_changed_kernels.py b/tests/test_ci_changed_kernels.py new file mode 100644 index 00000000..91d10432 --- /dev/null +++ b/tests/test_ci_changed_kernels.py @@ -0,0 +1,59 @@ +"""ci/changed_kernels.py: a pull request compiles the kernel of exactly the platforms it changes.""" +import importlib.util +from pathlib import Path +import shutil +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location('changed_kernels', ROOT / 'ci/changed_kernels.py') +changed_kernels = importlib.util.module_from_spec(spec) +spec.loader.exec_module(changed_kernels) + +PLATFORMS = ['apple-16k', 'apple-4k', 'apple-armv7'] + + +class ChangedKernels(unittest.TestCase): + def setUp(self): + temporary = tempfile.TemporaryDirectory() + self.addCleanup(temporary.cleanup) + # A copy of what decides a kernel; the checkout is the pull request's side. + self.base = Path(temporary.name) / 'base' + for name in ('kernel', 'src', 'platforms', 'devices', 'chips', 'features.toml', 'methods.toml'): + source = ROOT / name + if source.is_dir(): + shutil.copytree(source, self.base / name, ignore=shutil.ignore_patterns('__pycache__', '.work')) + elif source.exists(): + shutil.copy2(source, self.base / name) + + def changed(self): + return changed_kernels.changed(self.base, PLATFORMS) + + def test_the_same_sources_change_no_kernel(self): + self.assertEqual(self.changed(), []) + + def test_a_config_changes_only_its_platform(self): + with (self.base / 'kernel/configs/apple-4k/config').open('a') as config: + config.write('# CONFIG_EXAMPLE is not set\n') + self.assertEqual(self.changed(), ['apple-4k']) + + def test_a_shared_patch_layer_changes_both_arm64_platforms(self): + patch = sorted((self.base / 'kernel/patches/drivers').rglob('*.patch'))[0] + patch.write_text(patch.read_text() + '\n') + self.assertEqual(self.changed(), ['apple-16k', 'apple-4k']) + + def test_documentation_changes_no_kernel(self): + (self.base / 'kernel/README.md').write_text('older notes\n') + self.assertEqual(self.changed(), []) + + def test_a_build_script_changes_every_kernel(self): + (self.base / 'kernel/build').write_text('#!/bin/bash\n') + self.assertEqual(self.changed(), PLATFORMS) + + def test_a_platform_the_base_lacks_is_changed(self): + (self.base / 'kernel/series/apple-armv7').unlink() + self.assertEqual(self.changed(), ['apple-armv7']) + + +if __name__ == '__main__': + unittest.main() diff --git a/tests/test_ci_efficiency.py b/tests/test_ci_efficiency.py index b7584ff9..6299c548 100644 --- a/tests/test_ci_efficiency.py +++ b/tests/test_ci_efficiency.py @@ -52,14 +52,39 @@ def test_compilers_start_beside_the_tests_and_only_assembly_waits_for_them(self) def test_kernel_job_reuses_the_nightly_or_compiles_with_the_kept_compiler_cache(self): body = job((ROOT / '.github/workflows/build.yml').read_text(), 'kernel') self.assertIn('python3 ci/reuse_kernel.py "$PLATFORM" --output out', body) - # Everything after the question compiles only when the nightly had no answer. - self.assertEqual(body.count("if: steps.reuse.outputs.reused == ''"), 4) + # Everything after the question compiles only when the nightly had no answer + # (toolchain, saved or read-only compiler cache, build, archive). + self.assertEqual(body.count("if: steps.reuse.outputs.reused == ''"), 5) self.assertIn('path: out/cache/ccache/kernel', body) self.assertIn('key: kernel-ccache-v1-${{ matrix.platform }}-${{ steps.reuse.outputs.key }}', body) self.assertIn('restore-keys: kernel-ccache-v1-${{ matrix.platform }}-\n', body) self.assertIn('ORCHARD_CACHE_DIR="$PWD/out/cache"', body) self.assertLess(body.index('bash kernel/install-toolchain'), body.index('uses: actions/cache@v4')) + def test_pull_requests_compile_the_kernels_they_change_and_publish_nothing(self): + workflow = (ROOT / '.github/workflows/build.yml').read_text() + platforms = re.search(r'platform: \[([^\]]*)\]', job(workflow, 'kernel')).group(1).replace(',', '') + changed = job(workflow, 'changed') + self.assertIn('fetch-depth: 2', changed) + self.assertIn('python3 ci/changed_kernels.py "$RUNNER_TEMP/base" ' + platforms + ' |', changed) + self.assertIn("printf 'kernels=[]\\nkernel_exclude=[]\\n'", changed) + body = job(workflow, 'kernel') + self.assertIn("|| needs.changed.outputs.kernels != '[]'\n", body) + self.assertIn('exclude: ${{ fromJSON(needs.changed.outputs.kernel_exclude) }}', body) + # A pull request reads the compiler cache but never saves one. + self.assertIn("if: steps.reuse.outputs.reused == '' && github.event_name != 'pull_request'\n" + " with:\n path: out/cache/ccache/kernel", body) + self.assertIn("uses: actions/cache/restore@v4\n" + " if: steps.reuse.outputs.reused == '' && github.event_name == 'pull_request'", body) + # Nor does it archive or upload a kernel; assets (and so publishing) needs tools and pongo, + # which a pull request never runs. + self.assertIn("Archive components and corresponding kernel source\n" + " if: steps.reuse.outputs.reused == '' && github.event_name != 'pull_request'", body) + self.assertIn("uses: actions/upload-artifact@v4\n if: github.event_name != 'pull_request'", body) + for name in ('tools', 'pongo-check', 'pongo'): + self.assertNotIn('pull_request', job(workflow, name)) + self.assertNotIn('kernels', job(workflow, name)) + def test_apple_build_has_exact_input_cache_and_one_compiler_invocation(self): body = job((ROOT / '.github/workflows/build.yml').read_text(), 'pongo') self.assertEqual(body.count('python boot/quiet/build-pongo.py'), 1)