From 773fdb6bd60382abc0bd7b7bb5002cc00bc83e7d Mon Sep 17 00:00:00 2001 From: Ryan Murray Date: Fri, 27 Nov 2026 15:37:42 +1100 Subject: [PATCH] drm/apple: keep SDR content at the user's brightness when a panel shows PQ With HDR on in GNOME, an EDR panel jumps to a blinding level: on a K114s at 835 nits, SDR content goes towards the 2000-nit peak. The firmware shows PQ 100 cd/m^2 at SDR white, the level a swap's brightness sets, and the driver hands it the user's brightness as SDR white. Compositors put SDR content at the PQ reference white of ITU-R BT.2408, 203 cd/m^2: Mutter in BT.2100 mode stops writing the sysfs backlight and scales that reference white instead, and KWin does the same by default. SDR content then shows at 2.03 times the user's brightness, up to the peak. While PQ is shown, give the firmware SDR white x 100 / 203, so PQ 203 lands on the user's brightness and only brighter PQ levels use the headroom, as macOS shows SDR content in EDR. Report 203 in PQ_REFERENCE_LUMINANCE, which said 100. The headroom target follows the lower SDR white, so the peak is still reachable. Nothing changes outside PQ, and the step applies to every panel with EDR headroom. Signed-off-by: Ryan Murray --- drivers/gpu/drm/apple/apple_drv.c | 14 +++++++------- drivers/gpu/drm/apple/dcp-edr.h | 21 +++++++++++++++++++++ drivers/gpu/drm/apple/dcp_backlight_test.c | 19 +++++++++++++++++++ drivers/gpu/drm/apple/iomfb_template.c | 6 ++++++ 4 files changed, 53 insertions(+), 7 deletions(-) diff --git a/drivers/gpu/drm/apple/apple_drv.c b/drivers/gpu/drm/apple/apple_drv.c index 06e36e3a4a00d..328744cfb0b07 100644 --- a/drivers/gpu/drm/apple/apple_drv.c +++ b/drivers/gpu/drm/apple/apple_drv.c @@ -55,6 +55,7 @@ #include "apple_resume.h" #include "dcp-hdr-metadata.h" #include "dcp.h" +#include "dcp-edr.h" #include "plane.h" #include "iomfb_m4.h" #include "t8132_ext_drm.h" @@ -1204,14 +1205,13 @@ static const struct drm_connector_helper_funcs apple_connector_helper_funcs = { }; /* - * A desktop 26.x panel shows PQ as EDR (see dcp_edr_prepare()): PQ 100 cd/m² - * lands on the white the backlight sets, and brighter levels use the - * headroom above it, up to the panel's peak. macOS uses the panel the same - * way. PQ_REFERENCE_LUMINANCE tells a compositor where that white is in PQ, - * so it can put its reference white there and keep using the backlight as - * the brightness control in PQ. + * A panel shows PQ as EDR (see dcp_edr_prepare() and dcp_edr_pq_sdr()): PQ + * DCP_EDR_PQ_REFERENCE_NITS lands on the white the backlight sets, and + * brighter levels use the headroom above it, up to the panel's peak. macOS + * uses the panel the same way. PQ_REFERENCE_LUMINANCE tells a compositor + * where that white is in PQ, so it can put its reference white there and + * keep using the backlight as the brightness control in PQ. */ -#define DCP_EDR_PQ_REFERENCE_NITS 100 static int apple_connector_attach_pq_reference(struct drm_connector *connector) { diff --git a/drivers/gpu/drm/apple/dcp-edr.h b/drivers/gpu/drm/apple/dcp-edr.h index fc5dc03e6f53f..d513cc54e565f 100644 --- a/drivers/gpu/drm/apple/dcp-edr.h +++ b/drivers/gpu/drm/apple/dcp-edr.h @@ -45,6 +45,27 @@ static inline u32 dcp_edr_ceiling(u32 fixed) return hm > DCP_EDR_UNITY && hm <= U32_MAX ? hm : 0; } +/* + * PQ levels in EDR. The firmware shows PQ 100 cd/m^2 at SDR white, the level + * a swap's brightness sets, and brighter PQ levels in the headroom above it. + * Compositors put SDR content at the PQ reference white of ITU-R BT.2408, + * 203 cd/m^2 (Mutter and KWin by default), and turn the backlight over to + * that reference white in HDR. Taken as is, SDR content then shows at 2.03 + * times the user's brightness, up to the panel's peak. So while PQ is shown + * the firmware is given SDR white x 100 / 203: PQ 203 lands on the user's + * brightness, and only brighter levels use the headroom, as macOS shows SDR + * content in EDR. PQ_REFERENCE_LUMINANCE tells compositors the same. + */ +#define DCP_EDR_FW_UNITY_NITS 100U +#define DCP_EDR_PQ_REFERENCE_NITS 203U + +/* The SDR white to hand the firmware for @sdr nits while PQ is shown */ +static inline u32 dcp_edr_pq_sdr(u32 sdr) +{ + return sdr ? max_t(u32, 1, DIV_ROUND_CLOSEST_ULL((u64)sdr * DCP_EDR_FW_UNITY_NITS, + DCP_EDR_PQ_REFERENCE_NITS)) : 0; +} + /* The headroom the panel has above @sdr nits: peak / sdr, up to @ceiling. */ static inline u32 dcp_edr_target(u32 peak, u32 sdr, u32 ceiling) { diff --git a/drivers/gpu/drm/apple/dcp_backlight_test.c b/drivers/gpu/drm/apple/dcp_backlight_test.c index e31fa370b3b4f..780b093f932ab 100644 --- a/drivers/gpu/drm/apple/dcp_backlight_test.c +++ b/drivers/gpu/drm/apple/dcp_backlight_test.c @@ -649,6 +649,24 @@ static void edr_ceiling_test(struct kunit *test) KUNIT_EXPECT_EQ(test, dcp_edr_ceiling(U32_MAX), 65535999U); } +/* PQ 203 cd/m^2, where compositors put SDR content, lands on the user's brightness */ +static void edr_pq_sdr_test(struct kunit *test) +{ + u32 sdr; + + KUNIT_EXPECT_EQ(test, dcp_edr_pq_sdr(0), 0U); + KUNIT_EXPECT_EQ(test, dcp_edr_pq_sdr(1), 1U); + KUNIT_EXPECT_EQ(test, dcp_edr_pq_sdr(203), 100U); + KUNIT_EXPECT_EQ(test, dcp_edr_pq_sdr(1000), 493U); + /* SDR content (PQ 203) shows at the user's level, give or take rounding */ + for (sdr = 10; sdr <= 1600; sdr += 7) + KUNIT_EXPECT_LE(test, abs((s64)dcp_edr_pq_sdr(sdr) * DCP_EDR_PQ_REFERENCE_NITS / + DCP_EDR_FW_UNITY_NITS - sdr), 2); + /* A lower SDR white leaves more headroom to the same peak */ + KUNIT_EXPECT_EQ(test, dcp_edr_target(2000, dcp_edr_pq_sdr(500), 16000), + div_u64(2000ULL * DCP_EDR_UNITY, dcp_edr_pq_sdr(500))); +} + static void edr_target_test(struct kunit *test) { u32 ceiling = dcp_edr_ceiling(EDR_TEST_CEILING); @@ -874,6 +892,7 @@ static struct kunit_case backlight_cases[] = { KUNIT_CASE(backlight_fade_blank_test), KUNIT_CASE(edr_ceiling_test), KUNIT_CASE(edr_target_test), + KUNIT_CASE(edr_pq_sdr_test), KUNIT_CASE(edr_low_end_steady_test), KUNIT_CASE(edr_fade_monotonic_test), KUNIT_CASE(edr_start_ramp_test), diff --git a/drivers/gpu/drm/apple/iomfb_template.c b/drivers/gpu/drm/apple/iomfb_template.c index 050522f5a6fda..5dc9de755cde3 100644 --- a/drivers/gpu/drm/apple/iomfb_template.c +++ b/drivers/gpu/drm/apple/iomfb_template.c @@ -170,6 +170,9 @@ static void dcp_edr_prepare(struct apple_dcp *dcp, struct DCP_FW_NAME(dcp_swap) return; /* no EDR, and none to wind down */ if (!sdr) return; + /* PQ's reference white on the user's brightness (dcp_edr_pq_sdr()) */ + if (dcp->output_edr) + sdr = dcp_edr_pq_sdr(sdr); if (dcp->output_edr) target = dcp_edr_target(peak, sdr, dcp->panel.edr_ceiling); if (restart && dcp->output_edr) { @@ -373,6 +376,9 @@ static void dcp_edr_prepare_v27(struct apple_dcp *dcp, struct dcp_swap_v27 *swap WRITE_ONCE(dcp->edr_ramping, false); if ((!dcp->output_edr && !dcp->edr_hm) || !sdr) return; + /* PQ's reference white on the user's brightness (dcp_edr_pq_sdr()) */ + if (dcp->output_edr) + sdr = dcp_edr_pq_sdr(sdr); cap = dcp_edr_target(READ_ONCE(dcp->panel.peak_nits), sdr, dcp->panel.edr_ceiling); if (dcp->output_edr) target = cap;