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/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/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/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/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 */ 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 { 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); + } + 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; + } + 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; + } + 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)) { + 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); + } + + 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; + 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; ++ } + } + } + 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) + 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(); 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) + { 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 + 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) + {