From eeebeaec3a919a7c1cefe76f9153043dd6ede3a3 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 19 Nov 2026 07:42:18 -0500 Subject: [PATCH 1/3] iommu: apple-dart: room for more external display host windows An external display DART's SID0 arena holds the two retained copy surfaces (four slices) and the host windows of directly scanned-out framebuffers, one 32 MiB slice each. Ten slices leave six windows. A compositor that also puts its cursor on a plane of its own (the next commit) uses three windows for its frames and up to three for its cursor buffers, besides the console's, and the windows ran out: the frame fell back to the copy path and the cursor was not shown. Take sixteen slices, which leaves twelve windows. The arena is fixed at admission from the slices nothing else uses; generic DMA keeps the rest of the 2048. Signed-off-by: Diljit Singh --- drivers/iommu/apple-dart-dcp.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/drivers/iommu/apple-dart-dcp.h b/drivers/iommu/apple-dart-dcp.h index 49ed48c2f3de6..51f3f68331a20 100644 --- a/drivers/iommu/apple-dart-dcp.h +++ b/drivers/iommu/apple-dart-dcp.h @@ -192,8 +192,12 @@ struct apple_dart_dcp_allocation { /* SID0 slices for the retained copy surfaces; generic DMA gets the other free ones. */ #define DCP_DART_SCANOUT_SLOTS 4 -/* An external display DART's SID0 arena: the surfaces and host windows. */ -#define DCP_DART_DISPEXT_SLOTS 10 +/* + * An external display DART's SID0 arena: the surfaces (DCP_DART_SCANOUT_SLOTS) + * and the host windows, a slice each. Under KWin the frame takes three + * windows and the cursor up to three more, besides a console's. + */ +#define DCP_DART_DISPEXT_SLOTS 16 static bool apple_dart_has_borrowed(struct apple_dart *dart) { From be354addc3c5a4fe2fd277768e7b5197d9718007 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 19 Nov 2026 06:05:59 -0500 Subject: [PATCH 2/3] drm: apple: t8132: a cursor plane on external outputs The T8132 external outputs (the J713's USB-C displays, and the J773g Mac mini's HDMI with the main DCP) offered one plane, the primary. KWin 6.8 then puts its cursor on that plane and every modeset fails with EINVAL ("Plane must cover entire CRTC", a 64x64 buffer as a 32x32 destination), so the desktop never lights the output unless KWIN_FORCE_SW_CURSOR=1 is set. Add a cursor plane to each output and present it as the swap's upper surface 1, the way the swap record already describes upper surfaces: - ARGB8888, linear, pre-multiplied (is_premultiplied set), zpos 1, under surface ID 2; - the check takes what the DCP takes as an upper surface: unscaled, 32x32 to 256x256 on screen, from the buffer's first byte, the pitch a multiple of 64 bytes. A cursor partly off the right or bottom edge is shown as its on-screen part; one off the top or left edge, or with less than 32 pixels left on screen, is refused, so that the compositor draws it itself; - every swap carries the cursor with the frame, the copied frames included: a commit of the cursor alone presents the frame on screen again with it; - the cursor's framebuffer is mapped in a SID0 window of its own, tracked as the frame's is (the swap going out, the swaps in flight, given-up swaps, what a confirmed swap put on screen), so that a window is never unmapped while a swap may still show it. Twelve windows instead of eight. Tested on a J773g (Mac mini M4) with a G81SF monitor on HDMI at 1920x1080, 60 and 120 Hz (kernel 7.3.0 with #1192): - a raw atomic client: a 64x64 cursor with the modeset, then mid screen, in the bottom-right corner, 40 px visible at the right and bottom edges and a 32x32 crop are accepted and committed; 20 px visible at the right, off the left or top edge, and a 16x16 crop are refused; 240 cursor-only commits in a row, none failed (16.6 ms each at 60 Hz); - KWin 6.8 drives the output with the cursor on the cursor plane, which follows a uinput pointer; no swap errors. Signed-off-by: Diljit Singh --- drivers/gpu/drm/apple/dcp-m4-frame.h | 5 +- drivers/gpu/drm/apple/iomfb_m4.c | 151 ++++++++++++++++++++++---- drivers/gpu/drm/apple/iomfb_m4.h | 21 +++- drivers/gpu/drm/apple/t8132_ext_drm.c | 142 +++++++++++++++++++++--- 4 files changed, 274 insertions(+), 45 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-m4-frame.h b/drivers/gpu/drm/apple/dcp-m4-frame.h index e51fe0a239bcb..6cf24f0ef4b2a 100644 --- a/drivers/gpu/drm/apple/dcp-m4-frame.h +++ b/drivers/gpu/drm/apple/dcp-m4-frame.h @@ -29,6 +29,8 @@ struct dcp_m4_frame { u32 height; u32 stride; enum dcp_m4_frame_format format; + /* ARGB8888 blended pre-multiplied rather than by coverage */ + bool premultiplied; }; static inline int dcp_m4_frame_check(const struct dcp_m4_frame *frame) @@ -97,7 +99,8 @@ static inline void dcp_m4_surface_encode(u8 *surface, const struct dcp_m4_frame u32 size = frame->stride * frame->height; memset(surface, 0, DCP_M4_SURFACE_SIZE); - surface[2] = frame->format == DCP_M4_XRGB8888; + /* is_premultiplied: an opaque XRGB8888 frame is blended as one too. */ + surface[2] = frame->format == DCP_M4_XRGB8888 || frame->premultiplied; put_unaligned_le32(1, surface + 3); put_unaligned_le32(1, surface + 7); put_unaligned_le32(0x42475241, surface + 0x0b); diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index 3ae666b093ea7..c1c43360d5a24 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -805,7 +805,7 @@ static int dcp_m4_create_piodma_ext(struct iomfb_m4 *dcp) * confirmed swap shows it (given-up swaps that may still show it are in * ext_lost_swap); pinned: its unmap failed, kept until reboot. */ -#define DCP_M4_EXT_WINDOWS 6 +#define DCP_M4_EXT_WINDOWS 12 struct dcp_m4_ext_window { u64 dva; void *key; @@ -823,6 +823,8 @@ struct dcp_m4_ext_window { struct dcp_m4_ext_lost { u32 id; int win; + /* The cursor's window it may still put on screen (-1: none), as @win */ + int cwin; bool superseded; }; @@ -835,6 +837,8 @@ struct dcp_m4_ext_lost { struct dcp_m4_ext_inflight { struct dcp_m4_swap_state swap; int win; + /* Its cursor's window (-1: none) */ + int cwin; int gen; unsigned long deadline; bool flip_held; @@ -956,6 +960,8 @@ struct dcp_m4_runtime { struct dcp_m4_ext_inflight ext_q[DCP_M4_EXT_INFLIGHT]; unsigned int ext_qn; int ext_swap_win; + /* The window of the cursor of the swap going out (-1: none) */ + int ext_swap_cwin; unsigned int ext_swap_async; bool ext_swap_now; struct dcp_m4_ext_done ext_done; @@ -1002,7 +1008,9 @@ struct dcp_m4_runtime { }; static void dcp_m4_ext_async_done(struct dcp_m4_runtime *r, int i, enum dcp_m4_ext_end end); -static void dcp_m4_ext_shown(struct dcp_m4_runtime *r, u32 id, int win, int older, +static int dcp_m4_ext_cursor(struct dcp_m4_runtime *r, const struct iomfb_m4_ext_cursor *c, + struct dcp_m4_frame *f, struct dcp_m4_upper *u); +static void dcp_m4_ext_shown(struct dcp_m4_runtime *r, u32 id, int win, int cwin, int older, enum dcp_m4_ext_end end); static void dcp_m4_runtime_wake(struct iomfb_m4 *dcp) @@ -1223,7 +1231,8 @@ static int dcp_m4_ext_lost_slot(struct dcp_m4_runtime *r, u32 id) static bool dcp_m4_ext_win_lost(struct dcp_m4_runtime *r, int i) { for (unsigned int j = 0; j < ARRAY_SIZE(r->ext_lost_swap); j++) - if (r->ext_lost_swap[j].id && r->ext_lost_swap[j].win == i) + if (r->ext_lost_swap[j].id && + (r->ext_lost_swap[j].win == i || r->ext_lost_swap[j].cwin == i)) return true; return false; } @@ -1246,7 +1255,7 @@ static bool dcp_m4_ext_late_d590(struct dcp_m4_runtime *r, u32 id, bool shown) if (slot < 0) return false; lost = r->ext_lost_swap[slot]; - r->ext_lost_swap[slot] = (struct dcp_m4_ext_lost){ .win = -1 }; + r->ext_lost_swap[slot] = (struct dcp_m4_ext_lost){ .win = -1, .cwin = -1 }; for (int i = 0; i < 2; i++) { if (r->ext_buf_lost[i] != id) continue; @@ -1271,6 +1280,9 @@ static bool dcp_m4_ext_late_d590(struct dcp_m4_runtime *r, u32 id, bool shown) r->ext_win[lost.win].on_screen = true; front = true; } + /* So is the window of the cursor it showed. */ + if (shown && lost.cwin >= 0) + r->ext_win[lost.cwin].on_screen = true; if (held) { r->ext_hold_id = 0; r->ext_hold_probe = false; @@ -1355,7 +1367,7 @@ static int dcp_m4_runtime_swap_done(void *cookie, u32 id) if (!ret) { r->ext_swap_shown = done.shown; if (r->ext_swap_async) - dcp_m4_ext_shown(r, id, r->ext_swap_win, r->ext_qn, + dcp_m4_ext_shown(r, id, r->ext_swap_win, r->ext_swap_cwin, r->ext_qn, done.shown ? DCP_M4_EXT_END_SHOWN : DCP_M4_EXT_END_UNSHOWN); } @@ -2092,7 +2104,7 @@ static int dcp_m4_ext_swap_wait(struct dcp_m4_runtime *r, int link_gen, u64 *wor * failure as before. */ static bool dcp_m4_ext_swap_give_up(struct dcp_m4_runtime *r, struct dcp_m4_swap_state *swap, - int win, bool superseded, int error) + int win, int cwin, bool superseded, int error) { struct iomfb_m4 *dcp = r->dcp; struct dcp_m4_transaction *t = &dcp->transaction; @@ -2115,7 +2127,7 @@ static bool dcp_m4_ext_swap_give_up(struct dcp_m4_runtime *r, struct dcp_m4_swap return false; } r->ext_lost_swap[slot] = (struct dcp_m4_ext_lost){ - .id = swap->id, .win = win, .superseded = superseded, + .id = swap->id, .win = win, .cwin = cwin, .superseded = superseded, }; swap->emitted = true; if (r->ext_swap_surface >= 0) @@ -2259,6 +2271,7 @@ static int dcp_m4_runtime_swap_full(struct dcp_m4_runtime *r, bool primary, *e = (struct dcp_m4_ext_inflight){ .swap = handed, .win = r->ext_swap_win, + .cwin = r->ext_swap_cwin, .gen = link_gen, .deadline = jiffies + msecs_to_jiffies(5000), .flip_held = r->ext_qn || r->ext_swap_async < 2, @@ -2294,7 +2307,8 @@ static int dcp_m4_runtime_swap_full(struct dcp_m4_runtime *r, bool primary, /* Only the copied host input is freed; the firmware-visible RPC stays pinned. */ kfree(input); /* Only its D590 missing: given up, the session kept. */ - if (ret && d590_missing && dcp_m4_ext_swap_give_up(r, &r->swap, -1, false, ret)) + if (ret && d590_missing && + dcp_m4_ext_swap_give_up(r, &r->swap, -1, r->ext_swap_cwin, false, ret)) return ret; if (ret) { dcp_m4_swap_fail(&r->swap, ret); @@ -2396,8 +2410,9 @@ static int dcp_m4_startup(struct iomfb_m4 *dcp) /* No surface on screen or in flight yet. */ r->ext_swap_surface = -1; r->ext_front = -1; - /* No direct swap going out. */ + /* No direct swap going out, no cursor with one. */ r->ext_swap_win = -1; + r->ext_swap_cwin = -1; INIT_DELAYED_WORK(&r->ext_async_work, dcp_m4_ext_async_work); init_completion(&r->received); INIT_WORK(&r->idle, dcp_m4_runtime_idle); @@ -4323,8 +4338,10 @@ static void dcp_m4_ext_retired(struct dcp_m4_runtime *r, int shown) /* No window is on screen, nor may a given-up swap show one. */ for (int i = 0; i < DCP_M4_EXT_WINDOWS; i++) r->ext_win[i].on_screen = false; - for (unsigned int i = 0; i < ARRAY_SIZE(r->ext_lost_swap); i++) + for (unsigned int i = 0; i < ARRAY_SIZE(r->ext_lost_swap); i++) { r->ext_lost_swap[i].win = -1; + r->ext_lost_swap[i].cwin = -1; + } } /* Rows of @rect from @src (@pitch) into @dst (@stride); its pixels. */ @@ -4355,15 +4372,16 @@ static u64 dcp_m4_ext_copy(void *dst, u32 stride, const struct iosys_map *src, u * swapped without a mode, a sink or the display DART (its last D572). */ int iomfb_m4_ext_present(struct apple_dcp *owner, const struct iosys_map *src, u32 pitch, - const struct drm_rect *damage) + const struct drm_rect *damage, const struct iomfb_m4_ext_cursor *cursor) { struct iomfb_m4 *dcp = owner->v26_6; struct dcp_m4_runtime *r; struct drm_rect full, rect, cur; - struct dcp_m4_frame *f; + struct dcp_m4_frame *f, cf; + struct dcp_m4_upper cu; unsigned int back; void *cpu; - int ret = -ENOLINK; + int ret = -ENOLINK, cw = -1; if (!dcp || !dcp->external) return -ENODEV; @@ -4468,9 +4486,22 @@ int iomfb_m4_ext_present(struct apple_dcp *owner, const struct iosys_map *src, u } /* Only through the Normal-NC view; nothing else maps these pages. */ dma_wmb(); + /* + * The cursor with it, its window had now: the DART is checked again, + * as callbacks may have been handled since (a surfaceless swap above). + */ + ret = cursor && cursor->fb.key ? dcp_m4_ext_scanout_gate(dcp) : 0; + if (!ret) + ret = dcp_m4_ext_cursor(r, cursor, &cf, &cu); + if (ret) { + r->ext_buf_stale[back] = true; + goto out; + } + cw = r->ext_swap_cwin; r->ext_swap_surface = back; - ret = dcp_m4_runtime_swap_full(r, true, f, NULL, 0, false, 0, 0, 0); + ret = dcp_m4_runtime_swap_full(r, true, f, cu.frame ? &cu : NULL, 0, false, 0, 0, 0); r->ext_swap_surface = -1; + r->ext_swap_cwin = -1; if (ret) { r->ext_buf_stale[back] = true; goto out; @@ -4486,6 +4517,9 @@ int iomfb_m4_ext_present(struct apple_dcp *owner, const struct iosys_map *src, u goto out; } dcp_m4_ext_retired(r, back); + /* Its cursor is on screen with it. */ + if (cw >= 0) + r->ext_win[cw].on_screen = true; /* A frame shown, for the replay after a sleep. */ if (r->ext_restore_pending) WRITE_ONCE(r->ext_restore_frame, true); @@ -4545,10 +4579,10 @@ static bool dcp_m4_ext_win_idle(struct dcp_m4_runtime *r, int i) const struct dcp_m4_ext_window *w = &r->ext_win[i]; if (!w->key || w->on_screen || w->pinned || r->ext_swap_win == i || - dcp_m4_ext_win_lost(r, i)) + r->ext_swap_cwin == i || dcp_m4_ext_win_lost(r, i)) return false; for (unsigned int j = 0; j < r->ext_qn; j++) - if (r->ext_q[j].win == i) + if (r->ext_q[j].win == i || r->ext_q[j].cwin == i) return false; return true; } @@ -4731,7 +4765,7 @@ static void dcp_m4_ext_flip_after(struct dcp_m4_runtime *r, const struct dcp_m4_ * else is retired. UNSHOWN: what was on screen stays, and its window is free. * The copy surfaces go stale and a hold ends either way. Session lock held. */ -static void dcp_m4_ext_shown(struct dcp_m4_runtime *r, u32 id, int win, int older, +static void dcp_m4_ext_shown(struct dcp_m4_runtime *r, u32 id, int win, int cwin, int older, enum dcp_m4_ext_end end) { if (end == DCP_M4_EXT_END_SHOWN) { @@ -4751,6 +4785,9 @@ static void dcp_m4_ext_shown(struct dcp_m4_runtime *r, u32 id, int win, int olde if (end != DCP_M4_EXT_END_UNSHOWN) { if (win >= 0) r->ext_win[win].on_screen = true; + /* Its cursor (@cwin, -1: none) is on screen with it. */ + if (cwin >= 0) + r->ext_win[cwin].on_screen = true; /* A frame shown, for the replay after a sleep. */ if (r->ext_restore_pending) WRITE_ONCE(r->ext_restore_frame, true); @@ -4780,7 +4817,7 @@ static void dcp_m4_ext_async_done(struct dcp_m4_runtime *r, int i, enum dcp_m4_e e.swap.id, r->ext_q[0].swap.id); dcp_m4_ext_q_remove(r, i); if (end != DCP_M4_EXT_END_TAKEN) - dcp_m4_ext_shown(r, e.swap.id, e.win, i, end); + dcp_m4_ext_shown(r, e.swap.id, e.win, e.cwin, i, end); dcp_m4_ext_flip_after(r, &e); } @@ -4809,7 +4846,7 @@ static void dcp_m4_ext_async_expire(struct dcp_m4_runtime *r, bool went) r->ext_buf_stale[0] = true; r->ext_buf_stale[1] = true; if (!READ_ONCE(dcp->error) && - !dcp_m4_ext_swap_give_up(r, &e.swap, e.win, e.superseded, ret)) { + !dcp_m4_ext_swap_give_up(r, &e.swap, e.win, e.cwin, e.superseded, ret)) { dcp_m4_swap_fail(&r->swap, ret); dcp_m4_transaction_fail(&dcp->transaction, ret); dcp_m4_failed(dcp, ret); @@ -4852,6 +4889,65 @@ static void dcp_m4_ext_async_work(struct work_struct *work) mutex_unlock(&dcp->lock); } +/* The cursor's surface in a swap: upper surface 1, under its own ID. */ +#define DCP_M4_EXT_CURSOR_SURFACE 1 +#define DCP_M4_EXT_CURSOR_ID 2 + +/* + * The cursor of the swap going out, @c (NULL or no key: none), as an upper + * surface in @u (its frame in @f), its framebuffer mapped in a window of its + * own (dcp_m4_ext_win_get(), whose caller's conditions hold), which + * r->ext_swap_cwin names until the swap is out (-1: none). Without a window + * for it (-ENOSPC, or pages it cannot map), the frame goes without the + * cursor, said once in a while. -EAGAIN while the DART is not confirmed since + * a sleep, another error for a failed mapping (a hardware error): no frame. + */ +static int dcp_m4_ext_cursor(struct dcp_m4_runtime *r, const struct iomfb_m4_ext_cursor *c, + struct dcp_m4_frame *f, struct dcp_m4_upper *u) +{ + struct iomfb_m4 *dcp = r->dcp; + int w; + + *u = (struct dcp_m4_upper){ + .index = DCP_M4_EXT_CURSOR_SURFACE, .id = DCP_M4_EXT_CURSOR_ID, + }; + r->ext_swap_cwin = -1; + if (!c || !c->fb.key) + return 0; + *f = (struct dcp_m4_frame){ + .width = c->fb.width, .height = c->fb.height, .stride = c->fb.pitch, + .format = DCP_M4_ARGB8888, .premultiplied = true, + }; + u->x = c->x; + u->y = c->y; + /* Checked before any window is taken for it: the DRM check let it through. */ + u->frame = f; + f->dva = DCP_M4_SCANOUT_BASE; + f->backing_size = (u64)c->fb.pitch * c->fb.height; + if (dcp_m4_upper_check_on(u, r->ext_width, r->ext_height)) { + dev_warn_ratelimited(dcp->dev, "cursor %ux%u (pitch %u) at %u,%u: not on the %ux%u output, not shown\n", + c->fb.width, c->fb.height, c->fb.pitch, c->x, c->y, + r->ext_width, r->ext_height); + u->frame = NULL; + return 0; + } + w = dcp_m4_ext_win_get(r, &c->fb, PAGE_ALIGN((size_t)c->fb.pitch * c->fb.height)); + if (w == -ENOSPC || w == -EOPNOTSUPP) { + dev_warn_ratelimited(dcp->dev, "cursor not shown: no window for its framebuffer (%d)\n", + w); + u->frame = NULL; + return 0; + } + if (w < 0) { + u->frame = NULL; + return w; + } + f->dva = r->ext_win[w].dva; + f->backing_size = (u64)r->ext_win[w].count * SZ_16K; + r->ext_swap_cwin = w; + return 0; +} + /* * One DRM frame scanned out directly * from its framebuffer, mapped into a SID0 host window (kept per @@ -4864,11 +4960,13 @@ static void dcp_m4_ext_async_work(struct work_struct *work) * -EOPNOTSUPP for a framebuffer that does not fit the mode or the window: the * caller copies it instead. */ -int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_ext_fb *fb) +int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_ext_fb *fb, + const struct iomfb_m4_ext_cursor *cursor) { struct iomfb_m4 *dcp = owner->v26_6; struct dcp_m4_runtime *r; - struct dcp_m4_frame f; + struct dcp_m4_frame f, cf; + struct dcp_m4_upper cu; int ret = -ENOLINK, w; unsigned int depth, free; size_t need; @@ -4935,14 +5033,21 @@ int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_e .width = fb->width, .height = fb->height, .stride = fb->pitch, .format = DCP_M4_XRGB8888, }; + /* The frame's window is taken: the cursor's may not reuse it. */ + r->ext_swap_win = w; + ret = dcp_m4_ext_cursor(r, cursor, &cf, &cu); + if (ret) { + r->ext_swap_win = -1; + goto out; + } r->ext_hold_probe = false; r->ext_swap_surface = -1; - r->ext_swap_win = w; r->ext_swap_async = depth; r->ext_swap_now = false; - ret = dcp_m4_runtime_swap_full(r, true, &f, NULL, 0, false, 0, 0, 0); + ret = dcp_m4_runtime_swap_full(r, true, &f, cu.frame ? &cu : NULL, 0, false, 0, 0, 0); r->ext_swap_async = 0; r->ext_swap_win = -1; + r->ext_swap_cwin = -1; if (!ret && r->ext_swap_now) ret = 1; out: diff --git a/drivers/gpu/drm/apple/iomfb_m4.h b/drivers/gpu/drm/apple/iomfb_m4.h index c45d737b1d726..dea45d5e3a745 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.h +++ b/drivers/gpu/drm/apple/iomfb_m4.h @@ -36,6 +36,16 @@ struct iomfb_m4_ext_fb { void (*put)(void *key); }; +/* + * A cursor over the frame (the DRM cursor plane): its framebuffer as for + * direct scanout, ARGB8888 with pre-multiplied alpha, @fb.width x @fb.height + * from its first byte, at @x,@y wholly on the output. @fb.key NULL: none. + */ +struct iomfb_m4_ext_cursor { + struct iomfb_m4_ext_fb fb; + u32 x, y; +}; + #if IS_ENABLED(CONFIG_DRM_APPLE_DCP_T8132) /* Probe is inert; start adopts firmware after the common component is pinned. */ @@ -57,10 +67,11 @@ int iomfb_m4_ext_mode_ids(struct apple_dcp *dcp, const struct drm_display_mode * int iomfb_m4_ext_enable(struct apple_dcp *dcp, const struct iomfb_m4_ext_ids *ids, u32 width, u32 height); int iomfb_m4_ext_present(struct apple_dcp *dcp, const struct iosys_map *src, u32 pitch, - const struct drm_rect *damage); + const struct drm_rect *damage, const struct iomfb_m4_ext_cursor *cursor); int iomfb_m4_ext_blank(struct apple_dcp *dcp); /* 0 once submitted, its flip later (t8132_ext_drm_flip_done()); 1: its flip now. */ -int iomfb_m4_ext_present_direct(struct apple_dcp *dcp, const struct iomfb_m4_ext_fb *fb); +int iomfb_m4_ext_present_direct(struct apple_dcp *dcp, const struct iomfb_m4_ext_fb *fb, + const struct iomfb_m4_ext_cursor *cursor); /* Why an external DCP's display refuses system sleep, or NULL. */ const char *iomfb_m4_ext_sleep_veto(struct apple_dcp *dcp); /* An external output through a system sleep. */ @@ -117,14 +128,16 @@ static inline int iomfb_m4_ext_enable(struct apple_dcp *dcp, const struct iomfb_ } static inline int iomfb_m4_ext_present(struct apple_dcp *dcp, const struct iosys_map *src, - u32 pitch, const struct drm_rect *damage) + u32 pitch, const struct drm_rect *damage, + const struct iomfb_m4_ext_cursor *cursor) { return -ENODEV; } static inline int iomfb_m4_ext_blank(struct apple_dcp *dcp) { return -ENODEV; } static inline int iomfb_m4_ext_present_direct(struct apple_dcp *dcp, - const struct iomfb_m4_ext_fb *fb) + const struct iomfb_m4_ext_fb *fb, + const struct iomfb_m4_ext_cursor *cursor) { return -ENODEV; } diff --git a/drivers/gpu/drm/apple/t8132_ext_drm.c b/drivers/gpu/drm/apple/t8132_ext_drm.c index 911f011ae400b..6ff07f1ead78d 100644 --- a/drivers/gpu/drm/apple/t8132_ext_drm.c +++ b/drivers/gpu/drm/apple/t8132_ext_drm.c @@ -69,6 +69,8 @@ static struct t8132_ext_crtc_state *to_t8132_ext_crtc_state(struct drm_crtc_stat struct t8132_ext_out { struct drm_plane plane; + /* The cursor, an upper surface of the frame's swap */ + struct drm_plane cursor; struct drm_crtc crtc; struct drm_encoder encoder; struct drm_connector connector; @@ -124,6 +126,11 @@ static const u32 t8132_ext_formats[] = { DRM_FORMAT_ARGB8888, }; +/* The DCP blends an upper surface by its alpha, pre-multiplied. */ +static const u32 t8132_ext_cursor_formats[] = { + DRM_FORMAT_ARGB8888, +}; + static const u64 t8132_ext_modifiers[] = { DRM_FORMAT_MOD_LINEAR, DRM_FORMAT_MOD_INVALID, @@ -152,6 +159,40 @@ static int t8132_ext_plane_check(struct drm_plane *plane, struct drm_atomic_comm return ret; } +/* + * The cursor: what the DCP takes as an upper surface (dcp_m4_upper_check_on()), + * from its buffer's first byte, unscaled, from 32x32 to 256x256 on screen (a + * smaller surface crashes the firmware), a linear ARGB8888 buffer whose pitch + * is a multiple of 64 bytes. Its right and bottom edges may go off the output + * (only the part on it is shown), not its top or left one. Anything else is + * refused, so that the compositor draws the cursor itself. + */ +static int t8132_ext_cursor_check(struct drm_plane *plane, struct drm_atomic_commit *state) +{ + struct drm_plane_state *ps = drm_atomic_get_new_plane_state(state, plane); + struct drm_crtc_state *cs = NULL; + struct drm_framebuffer *fb = ps->fb; + int w, h, ret; + + if (ps->crtc) + cs = drm_atomic_get_new_crtc_state(state, ps->crtc); + ret = drm_atomic_helper_check_plane_state(ps, cs, DRM_PLANE_NO_SCALING, + DRM_PLANE_NO_SCALING, true, true); + if (ret || !ps->visible) + return ret; + w = drm_rect_width(&ps->dst); + h = drm_rect_height(&ps->dst); + if (ps->src.x1 || ps->src.y1 || ps->dst.x1 != ps->crtc_x || ps->dst.y1 != ps->crtc_y || + w < 32 || h < 32 || w > 256 || h > 256 || fb->modifier != DRM_FORMAT_MOD_LINEAR || + fb->offsets[0] || fb->pitches[0] % 64 || fb->pitches[0] < w * 4) { + drm_dbg_atomic(plane->dev, "[PLANE:%d:%s] cursor %dx%d+%d+%d (fb %ux%u, pitch %u) refused\n", + plane->base.id, plane->name, w, h, ps->dst.x1, ps->dst.y1, + fb->width, fb->height, fb->pitches[0]); + return -EINVAL; + } + return 0; +} + /* * Nothing to program here: the CRTC's flush presents the frame. * drm_atomic_helper_commit_planes() calls this for every plane it updates @@ -166,6 +207,11 @@ static const struct drm_plane_helper_funcs t8132_ext_plane_helpers = { .atomic_update = t8132_ext_plane_update, }; +static const struct drm_plane_helper_funcs t8132_ext_cursor_helpers = { + .atomic_check = t8132_ext_cursor_check, + .atomic_update = t8132_ext_plane_update, +}; + static const struct drm_plane_funcs t8132_ext_plane_funcs = { .update_plane = drm_atomic_helper_update_plane, .disable_plane = drm_atomic_helper_disable_plane, @@ -363,7 +409,8 @@ static void t8132_ext_direct_refused(struct t8132_ext_out *out, struct drm_frame * the commit tail, blocking or not). */ static int t8132_ext_present_direct(struct t8132_ext_out *out, struct apple_dcp *owner, - struct drm_framebuffer *fb, struct drm_gem_object *obj) + struct drm_framebuffer *fb, struct drm_gem_object *obj, + const struct iomfb_m4_ext_cursor *cursor) { const struct drm_display_mode *mode = &out->crtc.state->mode; struct iomfb_m4_ext_fb d = { @@ -408,25 +455,64 @@ static int t8132_ext_present_direct(struct t8132_ext_out *out, struct apple_dcp } dma_sync_sgtable_for_device(obj->import_attach->dev, dma->sgt, DMA_TO_DEVICE); } - ret = iomfb_m4_ext_present_direct(owner, &d); + ret = iomfb_m4_ext_present_direct(owner, &d, cursor); if (ret == -EOPNOTSUPP) t8132_ext_direct_refused(out, fb, t8132_ext_pages_refused); return ret < 0 ? ret : ret ? 3 : 2; } /* - * Present the primary plane's new frame: scanned out directly where it can - * be, else copied into the DCP and swapped in; 2 or 3 once a direct frame's - * swap is submitted (see t8132_ext_present_direct()), 1 if the commit has no - * new frame (no plane state), so nothing was tried. A copied framebuffer is - * read through its kernel mapping (dma-buf vmap for an imported one) inside - * CPU access. + * The cursor plane's framebuffer as the cursor of the frame going out, into + * @c: none if the plane shows nothing, or if its buffer is not one the DCP + * can scan out (16 KiB pages; said once per framebuffer). An imported buffer + * is cleaned out of the CPU caches first, as a directly scanned-out frame. + */ +static void t8132_ext_cursor_get(struct t8132_ext_out *out, struct iomfb_m4_ext_cursor *c) +{ + struct drm_plane_state *ps = out->cursor.state; + struct drm_framebuffer *fb = ps ? ps->fb : NULL; + struct drm_gem_object *obj; + + *c = (struct iomfb_m4_ext_cursor){}; + if (!fb || !ps->visible || ps->crtc != &out->crtc) + return; + obj = drm_gem_fb_get_obj(fb, 0); + if (!obj) + return; + if (drm_gem_is_imported(obj)) { + struct drm_gem_dma_object *dma = to_drm_gem_dma_obj(obj); + + if (!dma->sgt || !obj->import_attach || + !t8132_ext_sgt_pages_ok(dma->sgt, obj->size)) { + t8132_ext_direct_refused(out, fb, "cursor not shown: its pages are not 16 KiB pages each"); + return; + } + dma_sync_sgtable_for_device(obj->import_attach->dev, dma->sgt, DMA_TO_DEVICE); + } + c->fb = (struct iomfb_m4_ext_fb){ + .key = fb, .width = drm_rect_width(&ps->dst), .height = drm_rect_height(&ps->dst), + .pitch = fb->pitches[0], + .pages = t8132_ext_fb_pages, .get = t8132_ext_fb_get, .put = t8132_ext_fb_put, + }; + c->x = ps->dst.x1; + c->y = ps->dst.y1; +} + +/* + * Present the primary plane's frame with the cursor over it: scanned out + * directly where it can be, else copied into the DCP and swapped in; 2 or 3 + * once a direct frame's swap is submitted (see t8132_ext_present_direct()), + * 1 if the commit changes neither plane (no plane state), so nothing was + * tried. A commit of the cursor alone presents the frame on screen again + * with it (no damage). A copied framebuffer is read through its kernel + * mapping (dma-buf vmap for an imported one) inside CPU access. */ static int t8132_ext_present(struct t8132_ext_out *out, struct drm_atomic_commit *state) { struct drm_plane_state *new = drm_atomic_get_new_plane_state(state, &out->plane); struct drm_plane_state *old = drm_atomic_get_old_plane_state(state, &out->plane); struct apple_dcp *owner = t8132_ext_owner(out); + struct iomfb_m4_ext_cursor cursor; struct drm_framebuffer *fb; struct drm_gem_object *obj; struct iosys_map map, data; @@ -434,20 +520,26 @@ static int t8132_ext_present(struct t8132_ext_out *out, struct drm_atomic_commit struct dma_buf *buf; int ret; - if (!new || !old) - return 1; - fb = new->fb; + if (!new || !old) { + if (!drm_atomic_get_new_plane_state(state, &out->cursor)) + return 1; + /* The cursor alone: the frame on screen again, nothing of it damaged. */ + new = out->plane.state; + old = NULL; + } + fb = new ? new->fb : NULL; if (!owner || !fb || !new->visible) return -ENOLINK; if (out->frame_lost) damage = DRM_RECT_INIT(0, 0, fb->width, fb->height); - else if (!drm_atomic_helper_damage_merged(old, new, &damage)) + else if (!old || !drm_atomic_helper_damage_merged(old, new, &damage)) damage = DRM_RECT_INIT(0, 0, 0, 0); obj = drm_gem_fb_get_obj(fb, 0); if (!obj) return -EINVAL; + t8132_ext_cursor_get(out, &cursor); /* Scanned out directly where it can be, else copied below. */ - ret = t8132_ext_present_direct(out, owner, fb, obj); + ret = t8132_ext_present_direct(out, owner, fb, obj, &cursor); if (ret != -ENOSPC && ret != -EOPNOTSUPP) return ret; if (!drm_gem_is_imported(obj)) { @@ -456,7 +548,7 @@ static int t8132_ext_present(struct t8132_ext_out *out, struct drm_atomic_commit if (!dma->vaddr) return -EINVAL; iosys_map_set_vaddr(&data, dma->vaddr + fb->offsets[0]); - return iomfb_m4_ext_present(owner, &data, fb->pitches[0], &damage); + return iomfb_m4_ext_present(owner, &data, fb->pitches[0], &damage, &cursor); } buf = obj->import_attach->dmabuf; ret = dma_buf_vmap_unlocked(buf, &map); @@ -466,7 +558,7 @@ static int t8132_ext_present(struct t8132_ext_out *out, struct drm_atomic_commit if (!ret) { data = map; iosys_map_incr(&data, fb->offsets[0]); - ret = iomfb_m4_ext_present(owner, &data, fb->pitches[0], &damage); + ret = iomfb_m4_ext_present(owner, &data, fb->pitches[0], &damage, &cursor); dma_buf_end_cpu_access(buf, DMA_FROM_DEVICE); } dma_buf_vunmap_unlocked(buf, &map); @@ -1034,10 +1126,24 @@ static int t8132_ext_out_init(struct drm_device *drm, struct platform_device *pd BIT(DRM_MODE_BLEND_PIXEL_NONE)); if (ret) goto err_plane; - ret = drm_crtc_init_with_planes(drm, &out->crtc, &out->plane, NULL, - &t8132_ext_crtc_funcs, "dcpext-%s", dev_name(&pdev->dev)); + /* The cursor, over the frame (the same plane funcs: t8132_ext_plane()). */ + ret = drm_universal_plane_init(drm, &out->cursor, BIT(drm->mode_config.num_crtc), + &t8132_ext_plane_funcs, t8132_ext_cursor_formats, + ARRAY_SIZE(t8132_ext_cursor_formats), t8132_ext_modifiers, + DRM_PLANE_TYPE_CURSOR, NULL); if (ret) goto err_plane; + drm_plane_helper_add(&out->cursor, &t8132_ext_cursor_helpers); + ret = drm_plane_create_zpos_immutable_property(&out->cursor, 1); + if (!ret) + ret = drm_plane_create_blend_mode_property(&out->cursor, + BIT(DRM_MODE_BLEND_PREMULTI)); + if (ret) + goto err_cursor; + ret = drm_crtc_init_with_planes(drm, &out->crtc, &out->plane, &out->cursor, + &t8132_ext_crtc_funcs, "dcpext-%s", dev_name(&pdev->dev)); + if (ret) + goto err_cursor; drm_crtc_helper_add(&out->crtc, &t8132_ext_crtc_helpers); ret = drm_simple_encoder_init(drm, &out->encoder, DRM_MODE_ENCODER_TMDS); if (ret) @@ -1068,6 +1174,8 @@ static int t8132_ext_out_init(struct drm_device *drm, struct platform_device *pd drm_encoder_cleanup(&out->encoder); err_crtc: drm_crtc_cleanup(&out->crtc); +err_cursor: + drm_plane_cleanup(&out->cursor); err_plane: drm_plane_cleanup(&out->plane); err_free: From 7fcae884f4226e3ec926d1b9caaaedcfb04b1b86 Mon Sep 17 00:00:00 2001 From: Diljit Singh Date: Thu, 19 Nov 2026 06:05:59 -0500 Subject: [PATCH 3/3] drm: apple: t8132: let a frame wait for the swaps in flight With both slots for direct swaps in flight, iomfb_m4_ext_present_direct() returned -EBUSY and the frame was dropped; and a frame that fell back to the copy path while a direct swap was in flight was refused with -EBUSY too. On the J773g Mac mini this showed as "frame not shown: -16", about ten in a row after every 120 Hz modeset (fbcon, then KWin) while the display took its first frames, and more whenever the host windows ran out. A dropped frame's flip event is still sent, so a compositor that then goes idle leaves the screen on the frame before. Let the frame wait instead, with the session lock dropped: a direct one for the oldest swap in flight to complete or be given up, a copied one for all of them, 1 s at most each, then go out (-EBUSY only after that). Tested on a J773g with KWin at 1920x1080@120 over HDMI: no "frame not shown" in the boot or across three suspend-to-idle cycles (with the previous commits). Signed-off-by: Diljit Singh --- drivers/gpu/drm/apple/iomfb_m4.c | 64 ++++++++++++++++++++++++++++++-- 1 file changed, 60 insertions(+), 4 deletions(-) diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index c1c43360d5a24..daa04470c09f9 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -138,6 +138,8 @@ struct iomfb_m4_ext_mode { * compositor may draw into a buffer that is still on screen. */ #define DCP_M4_EXT_INFLIGHT 2 +/* How long a direct frame waits for a free slot before it is dropped */ +#define DCP_M4_EXT_SLOT_WAIT_MS 1000 static unsigned int t8132_ext_swap_depth = DCP_M4_EXT_INFLIGHT; static int t8132_ext_swap_depth_set(const char *val, const struct kernel_param *kp) @@ -4161,6 +4163,28 @@ static bool dcp_m4_ext_ids_current(struct iomfb_m4 *dcp, const struct iomfb_m4_e return found; } +/* + * A copied frame: the direct swaps in flight complete (D590) or are given up + * first, 1 s at most (-EBUSY then), the session lock dropped meanwhile. + */ +static int dcp_m4_ext_drain_direct(struct iomfb_m4 *dcp, struct dcp_m4_runtime *r) +{ + unsigned long end = jiffies + msecs_to_jiffies(DCP_M4_EXT_SLOT_WAIT_MS); + + lockdep_assert_held(&dcp->lock); + while (r->ext_qn) { + long left = (long)(end - jiffies); + + if (left <= 0 || READ_ONCE(dcp->error)) + return -EBUSY; + mutex_unlock(&dcp->lock); + wait_event_timeout(dcp->ext_settle_wq, + !READ_ONCE(r->ext_qn) || READ_ONCE(dcp->error), left); + mutex_lock(&dcp->lock); + } + return 0; +} + /* * DRM: before an enable or disable, which must not be refused: the direct * swaps in flight complete (D590) or are given up (5 s, or at once when the @@ -4415,9 +4439,23 @@ int iomfb_m4_ext_present(struct apple_dcp *owner, const struct iosys_map *src, u if (r->ext_mode_stale) goto out; ret = -EBUSY; - if ((r->ext_hold_id && !r->ext_hold_probe) || r->ext_orphan_start || - r->ext_qn || r->ext_sleeping) + if ((r->ext_hold_id && !r->ext_hold_probe) || r->ext_orphan_start || r->ext_sleeping) goto out; + /* + * Direct swaps in flight complete (or are given up) first, 1 s at most, + * rather than the frame being dropped; the DART is checked again after. + */ + if (r->ext_qn) { + ret = dcp_m4_ext_drain_direct(dcp, r); + if (!ret) + ret = dcp_m4_ext_scanout_gate(dcp); + if (ret) + goto out; + ret = -EBUSY; + if ((r->ext_hold_id && !r->ext_hold_probe) || r->ext_orphan_start || + r->ext_sleeping || READ_ONCE(dcp->error)) + goto out; + } r->ext_hold_probe = false; /* * Never the surface on screen, nor one a given-up swap may still @@ -4964,6 +5002,7 @@ int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_e const struct iomfb_m4_ext_cursor *cursor) { struct iomfb_m4 *dcp = owner->v26_6; + unsigned long until = jiffies + msecs_to_jiffies(DCP_M4_EXT_SLOT_WAIT_MS); struct dcp_m4_runtime *r; struct dcp_m4_frame f, cf; struct dcp_m4_upper cu; @@ -4974,6 +5013,7 @@ int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_e if (!dcp || !dcp->external) return -ENODEV; mutex_lock(&dcp->lock); +again: r = dcp->runtime; /* Pairs with the release store of r->done in startup. */ if (!r || !smp_load_acquire(&r->done) || READ_ONCE(dcp->error) || !r->ext_connected || @@ -4985,9 +5025,25 @@ int iomfb_m4_ext_present_direct(struct apple_dcp *owner, const struct iomfb_m4_e goto out; ret = -EBUSY; depth = clamp_val(READ_ONCE(t8132_ext_swap_depth), 1, DCP_M4_EXT_INFLIGHT); - if ((r->ext_hold_id && !r->ext_hold_probe) || r->ext_orphan_start || - r->ext_qn >= depth || r->ext_sleeping) + if ((r->ext_hold_id && !r->ext_hold_probe) || r->ext_orphan_start || r->ext_sleeping) goto out; + /* + * Every slot taken: the frame waits for the oldest swap in flight to + * complete (or be given up), rather than being dropped while the + * display is slow to take the first frames of a mode. The session lock + * is dropped meanwhile; whatever changed is checked again. + */ + if (r->ext_qn >= depth) { + long left = (long)(until - jiffies); + + if (left <= 0) + goto out; + mutex_unlock(&dcp->lock); + wait_event_timeout(dcp->ext_settle_wq, + READ_ONCE(r->ext_qn) < depth || READ_ONCE(dcp->error), left); + mutex_lock(&dcp->lock); + goto again; + } /* * A slot for each swap in flight, this one included, that a later D590 * may complete (dcp_m4_runtime_swap_done()); else frames are copied.