From c97b9dbff5b225cb4a378ada90d464550def8454 Mon Sep 17 00:00:00 2001 From: Marcelo Alcantara Date: Wed, 2 Dec 2026 14:24:04 +1000 Subject: [PATCH] drm/apple: fall back to safe SDR when auto SDR BT2020_RGB has no match Compositors such as Hyprland 0.57 (default cm=edid) set Colorspace=BT2020_RGB in SDR mode whenever the sink EDID advertises BT.2020 and the connector exposes the Colorspace property. On bandwidth-limited HDMI 2.0 timings (such as 3840x2160@60 on an LG 4K UHD TV behind T6040 external DCP), the firmware ColorModes catalogue exposes no BT2020_RGB entries and marks all 10/12-bit BT2020_YCC entries as unsafe, leaving only 8-bit YCbCr 4:2:0 sRGB/BT.601 safe. dcp_crtc_check_hdr() -> dcp_select_color_mode() rejected every atomic modeset with -EINVAL, trapping aquamarine/Hyprland in an unpaced retry loop (~1,000 commits/s, ~2,800 GPU mailbox IRQs/s, ~64 W idle power vs ~7.8 W baseline) with the external HDMI display never completing modeset. Fix both gaps without weakening explicit colour requests or HDR checks: 1. In dcp_color_space_matches(), allow DRM_MODE_COLORIMETRY_BT2020_RGB with DRM_CONNECTOR_COLOR_FORMAT_AUTO to match safe DCP_COLORIMETRY_BT2020_YCC entries for SDR as well as HDR (RGB still outranks YCbCr when both exist). 2. Extract dcp_scan_color_modes() and, in dcp_select_color_mode(), if an automatic SDR BT2020_RGB request (color_format == AUTO and broadcast_rgb == AUTO) finds no matching BT.2020 entry, fall back directly through dcp_scan_color_modes() with DRM_MODE_COLORIMETRY_DEFAULT rather than dcp_default_sdr_color(). This preserves unsafe/sdr_usable filtering, EOTF checks, max_bpc, and works even when cached default slots are unpopulated. 3. In dcp_crtc_check_hdr(), accept DRM_MODE_COLORIMETRY_BT2020_RGB alongside DRM_MODE_COLORIMETRY_DEFAULT for fixed 8-bit SDR pipes (!iomfb_template_pq || dcp_is_ext26) under the same AUTO/AUTO and max_bpc >= 8 guards. Explicit format, range, non-BT2020_RGB colorspace, low max_bpc, and HDR/PQ requests continue to return -EINVAL when unsupported. Add unit test coverage in the apple-dcp display.py selftest. Signed-off-by: Marcelo Alcantara --- drivers/gpu/drm/apple/dcp-color.h | 76 ++++++++++----- drivers/gpu/drm/apple/iomfb.c | 5 +- .../drivers/gpu/apple-dcp/display.py | 94 +++++++++++++++++++ 3 files changed, 149 insertions(+), 26 deletions(-) diff --git a/drivers/gpu/drm/apple/dcp-color.h b/drivers/gpu/drm/apple/dcp-color.h index e9a683dcde9bc..88307f86063f6 100644 --- a/drivers/gpu/drm/apple/dcp-color.h +++ b/drivers/gpu/drm/apple/dcp-color.h @@ -125,11 +125,10 @@ static inline bool dcp_color_space_matches(u8 colorimetry, if (colorimetry == DCP_COLORIMETRY_BT2020_RGB) return true; /* - * Compositors (e.g. Mutter) request BT2020_RGB with AUTO format - * for HDR even on timings where only YCbCr BT.2020 is safe. + * Compositors (e.g. Mutter, Hyprland) request BT2020_RGB with + * AUTO format even on timings where only YCbCr BT.2020 is safe. */ - return hdr && - req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO && + return req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO && colorimetry == DCP_COLORIMETRY_BT2020_YCC; case DRM_MODE_COLORIMETRY_BT2020_YCC: return colorimetry == DCP_COLORIMETRY_BT2020_YCC; @@ -283,9 +282,9 @@ static inline int dcp_default_sdr_color(const struct dcp_display_mode *mode, return -EINVAL; } -static inline int dcp_select_color_mode(const struct dcp_display_mode *mode, - const struct dcp_color_request *req, - struct dcp_color_mode *out) +static inline const struct dcp_color_entry * +dcp_scan_color_modes(const struct dcp_display_mode *mode, + const struct dcp_color_request *req) { u8 max_bpc = req->max_bpc ? req->max_bpc : 12; bool hdr = req->eotf != HDMI_EOTF_TRADITIONAL_GAMMA_SDR; @@ -293,23 +292,6 @@ static inline int dcp_select_color_mode(const struct dcp_display_mode *mode, const struct dcp_color_entry *best = NULL; u32 best_rank = 0; - if (dcp_color_request_is_default(req)) { - if (req->eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR && - req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT) - return dcp_default_sdr_color(mode, out); - if (req->eotf == HDMI_EOTF_SMPTE_ST2084 && - (req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT || - req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB)) { - if (!req->allow_hdr || mode->pq.score < 0) - return -EINVAL; - *out = mode->pq; - return 0; - } - } - - if (hdr && !req->allow_hdr) - return -EINVAL; - max_pass = (req->allow_unsafe_sdr && !mode->sdr_usable && mode->sdr_unsafe) ? 2 : 1; for (pass = 0; pass < max_pass && !best; pass++) { bool allow_unsafe = pass > 0; @@ -346,6 +328,52 @@ static inline int dcp_select_color_mode(const struct dcp_display_mode *mode, } } + return best; +} + +static inline int dcp_select_color_mode(const struct dcp_display_mode *mode, + const struct dcp_color_request *req, + struct dcp_color_mode *out) +{ + bool hdr = req->eotf != HDMI_EOTF_TRADITIONAL_GAMMA_SDR; + const struct dcp_color_entry *best; + + if (dcp_color_request_is_default(req)) { + if (req->eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR && + req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT) + return dcp_default_sdr_color(mode, out); + if (req->eotf == HDMI_EOTF_SMPTE_ST2084 && + (req->colorspace == DRM_MODE_COLORIMETRY_DEFAULT || + req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB)) { + if (!req->allow_hdr || mode->pq.score < 0) + return -EINVAL; + *out = mode->pq; + return 0; + } + } + + if (hdr && !req->allow_hdr) + return -EINVAL; + + best = dcp_scan_color_modes(mode, req); + if (!best && !hdr && + req->colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB && + req->color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO && + req->broadcast_rgb == DRM_HDMI_BROADCAST_RGB_AUTO) { + /* + * Compositors (e.g. Hyprland's default cm=edid) set + * Colorspace=BT2020_RGB in SDR when the sink EDID advertises + * BT.2020, even on bandwidth-limited timings (such as 4K60 + * HDMI 2.0) where only 8-bit sRGB/BT.601 YCbCr 4:2:0 is safe. + * Rejecting that commit with -EINVAL traps compositors in an + * unpaced modeset retry loop; fall back through the filtered + * catalogue so unsafe, EOTF, and max_bpc rules still apply. + */ + struct dcp_color_request fallback = *req; + + fallback.colorspace = DRM_MODE_COLORIMETRY_DEFAULT; + best = dcp_scan_color_modes(mode, &fallback); + } if (!best) return -EINVAL; dcp_color_mode_from_entry(out, best); diff --git a/drivers/gpu/drm/apple/iomfb.c b/drivers/gpu/drm/apple/iomfb.c index 1f1c36a19c219..b507be69895a7 100644 --- a/drivers/gpu/drm/apple/iomfb.c +++ b/drivers/gpu/drm/apple/iomfb.c @@ -3778,9 +3778,10 @@ int dcp_crtc_check_hdr(struct apple_dcp *dcp, struct drm_crtc *crtc, req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT) return 0; if (!READ_ONCE(dcp->iomfb_template_pq) || dcp_is_ext26(dcp)) { - /* Fixed 8-bit SDR pipes satisfy any default SDR request with max_bpc >= 8. */ + /* Fixed 8-bit SDR pipes satisfy default or auto BT.2020 SDR with max_bpc >= 8. */ if (req.eotf == HDMI_EOTF_TRADITIONAL_GAMMA_SDR && - req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT && + (req.colorspace == DRM_MODE_COLORIMETRY_DEFAULT || + req.colorspace == DRM_MODE_COLORIMETRY_BT2020_RGB) && req.color_format == DRM_CONNECTOR_COLOR_FORMAT_AUTO && req.broadcast_rgb == DRM_HDMI_BROADCAST_RGB_AUTO && (!req.max_bpc || req.max_bpc >= 8)) diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/display.py b/tools/testing/selftests/drivers/gpu/apple-dcp/display.py index a4de9d6f8e76c..d875963e5480c 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/display.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/display.py @@ -1356,6 +1356,100 @@ def color_selection(root): assert(!dcp_select_color_mode(&k4, &req, &out) && out.id == 119 && out.format == DCP_COLOR_FORMAT_YCBCR420); assert(k4.colors[1].format == DCP_COLOR_FORMAT_YCBCR444); + /* SDR BT2020_RGB + AUTO matches safe SDR BT2020_YCC when no BT2020_RGB exists */ + struct dcp_display_mode sdr_ycc = { + .color_mode_id = 123, .sdr_usable = true, + .sdr = { .score = 100, .id = 123, .eotf = DCP_EOTF_SDR_GAMMA, + .format = DCP_COLOR_FORMAT_YCBCR444, .colorimetry = DCP_COLORIMETRY_SRGB, + .range = DCP_COLOR_YCBCR_RANGE_LIMITED, .depth = 8 }, + }; + add_entry(&sdr_ycc, 123, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420); + add_entry(&sdr_ycc, 137, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_DOWNSTREAM_420); + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .max_bpc = 10 }; + assert(!dcp_select_color_mode(&sdr_ycc, &req, &out) && out.id == 137 && out.format == DCP_COLOR_FORMAT_YCBCR420); + /* Explicit wire format (even matching YCBCR420) disables the BT2020_RGB -> BT2020_YCC alias */ + req.color_format = DRM_CONNECTOR_COLOR_FORMAT_YCBCR420; + assert(dcp_select_color_mode(&sdr_ycc, &req, &out) == -EINVAL); + req.color_format = DRM_CONNECTOR_COLOR_FORMAT_RGB444; + assert(dcp_select_color_mode(&sdr_ycc, &req, &out) == -EINVAL); + + /* 4K60 HDMI 2.0 timing (LG UHD shape): only 8-bit 4:2:0 sRGB/BT.601 is safe; + * BT.2020 entries are unsafe. Auto SDR BT2020_RGB (Hyprland cm=edid) falls back + * through the filtered catalogue while preserving safety, EOTF, max_bpc, and + * strict rejection of explicit overrides and HDR. + */ + struct dcp_display_mode lg4k60 = { + .color_mode_id = 80, .sdr_usable = true, .sdr_unsafe = true, + /* Cached default points at an unsafe RGB mode (rate.py 4:2:0 parser shape): + * fallback MUST NOT bypass filtering via dcp_default_sdr_color(). */ + .sdr_rgb = { .score = 300000, .id = 80, .eotf = DCP_EOTF_SDR_GAMMA, + .format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_SRGB, + .range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 8 }, + .sdr_444 = { .score = -1 }, .sdr = { .score = -1 }, .best = { .score = -1 }, + .pq = { .score = -1 }, + }; + add_entry(&lg4k60, 80, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_FULL, 8, DCP_COLOR_FLAG_UNSAFE); + add_entry(&lg4k60, 125, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT601, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420); + add_entry(&lg4k60, 123, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_LIMITED, 8, DCP_COLOR_FLAG_DOWNSTREAM_420); + add_entry(&lg4k60, 137, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_UNSAFE | DCP_COLOR_FLAG_DOWNSTREAM_420); + add_entry(&lg4k60, 141, DCP_EOTF_ST_2084, DCP_COLOR_FORMAT_YCBCR444, DCP_COLORIMETRY_BT2020_YCC, DCP_COLOR_YCBCR_RANGE_LIMITED, 10, DCP_COLOR_FLAG_UNSAFE | DCP_COLOR_FLAG_DOWNSTREAM_420); + /* Auto SDR BT2020_RGB with uncapped, 12-bpc, or 8-bpc picks safe 4:2:0 SDR (125), never unsafe 80 */ + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = true, .allow_unsafe_sdr = true }; + assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420); + req.max_bpc = 12; + assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420); + req.max_bpc = 8; + assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420); + /* Fallback also succeeds when all cached default slots are unpopulated */ + lg4k60.sdr_rgb.score = -1; + req.max_bpc = 0; + assert(!dcp_select_color_mode(&lg4k60, &req, &out) && out.id == 125 && out.format == DCP_COLOR_FORMAT_YCBCR420); + /* max_bpc too low even for safe 8-bit SDR still rejects */ + req.max_bpc = 6; + assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL); + /* Explicit format, range, colorspace, or HDR still reject */ + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, + .color_format = DRM_CONNECTOR_COLOR_FORMAT_RGB444, .allow_unsafe_sdr = true }; + assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL); + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, + .broadcast_rgb = DRM_HDMI_BROADCAST_RGB_LIMITED, .allow_unsafe_sdr = true }; + assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL); + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_YCC, .allow_unsafe_sdr = true }; + assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL); + req = (struct dcp_color_request){ .eotf = HDMI_EOTF_SMPTE_ST2084, + .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = true, .allow_unsafe_sdr = true }; + assert(dcp_select_color_mode(&lg4k60, &req, &out) == -EINVAL); + + /* Uncapped unsafe-only timing: fallback respects allow_unsafe_sdr and never picks PQ for SDR */ + struct dcp_display_mode unsafe_only = { + .color_mode_id = 80, .sdr_usable = false, .sdr_unsafe = true, + .sdr_rgb = { .score = 100, .id = 80, .eotf = DCP_EOTF_SDR_GAMMA, + .format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_SRGB, + .range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 8 }, + .sdr_444 = { .score = -1 }, .sdr = { .score = -1 }, .best = { .score = -1 }, .pq = { .score = -1 }, + }; + add_entry(&unsafe_only, 80, DCP_EOTF_SDR_GAMMA, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_SRGB, DCP_COLOR_YCBCR_RANGE_FULL, 8, DCP_COLOR_FLAG_UNSAFE); + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_unsafe_sdr = false }; + assert(dcp_select_color_mode(&unsafe_only, &req, &out) == -EINVAL); + req.allow_unsafe_sdr = true; + assert(!dcp_select_color_mode(&unsafe_only, &req, &out) && out.id == 80); + + struct dcp_display_mode pq_only = { + .color_mode_id = 141, .sdr_usable = false, .sdr_unsafe = false, + .sdr_rgb = { .score = -1 }, .sdr_444 = { .score = -1 }, .sdr = { .score = -1 }, + .best = { .score = 100, .id = 141, .eotf = DCP_EOTF_ST_2084, + .format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_BT2020_RGB, + .range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 10 }, + .pq = { .score = 100, .id = 141, .eotf = DCP_EOTF_ST_2084, + .format = DCP_COLOR_FORMAT_RGB, .colorimetry = DCP_COLORIMETRY_BT2020_RGB, + .range = DCP_COLOR_YCBCR_RANGE_FULL, .depth = 10 }, + }; + add_entry(&pq_only, 141, DCP_EOTF_ST_2084, DCP_COLOR_FORMAT_RGB, DCP_COLORIMETRY_BT2020_RGB, DCP_COLOR_YCBCR_RANGE_FULL, 10, 0); + req = (struct dcp_color_request){ .colorspace = DRM_MODE_COLORIMETRY_BT2020_RGB, .allow_hdr = false }; + assert(dcp_select_color_mode(&pq_only, &req, &out) == -EINVAL); + req.allow_hdr = true; + assert(dcp_select_color_mode(&pq_only, &req, &out) == -EINVAL); + puts("PASS: colour mode selection across RGB/YCbCr 444/422/420, 8/10/12 bpc, Full/Limited, wide gamut, PQ/HLG, and -EINVAL rejection"); return 0; }