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;