diff --git a/drivers/gpu/drm/apple/parser-test.c b/drivers/gpu/drm/apple/parser-test.c index e79da0fb0ad08..2c277e32b33cd 100644 --- a/drivers/gpu/drm/apple/parser-test.c +++ b/drivers/gpu/drm/apple/parser-test.c @@ -2072,6 +2072,268 @@ static void parser_internal_v27_only_120hz(struct kunit *test) dcp_modes_release(dcp); } +struct parser_unsafe_timing { + s64 id, score; + s64 hactive, htotal, hporch, hsync; + s64 vactive, vtotal, vporch, vsync, refresh; + bool preferred, unsafe, dsc, second; +}; + +static void blob_color_ids(struct parser_blob *blob, const char *key, bool listed) +{ + blob_key(blob, key); + blob_tag(blob, 2, listed); + if (listed) { + blob_tag(blob, 4, 0); + put_unaligned_le64(80, blob->data + blob->size); + blob->size += 8; + } +} + +/* + * A timing as a desktop dcpext lists it for a monitor behind a DP to HDMI + * converter: one RGB element (ID 80), which UnsafeColorElementIDs and + * DSCRequiredColorElementIDs may list, and optionally a second (ID 81) that + * neither does. + */ +static void blob_unsafe_timing(struct parser_blob *blob, + const struct parser_unsafe_timing *t) +{ + blob_tag(blob, 1, 10); + blob_bool(blob, "IsVirtual", false); + blob_int(blob, "ID", t->id); + blob_int(blob, "Score", t->score); + blob_timing_dimension(blob, "HorizontalAttributes", t->hactive, t->htotal, + t->hporch, t->hsync, + ((u64)t->vtotal * t->refresh << 16) / 1000); + blob_timing_dimension(blob, "VerticalAttributes", t->vactive, t->vtotal, + t->vporch, t->vsync, t->refresh << 16); + blob_color_ids(blob, "UnsafeColorElementIDs", t->unsafe); + blob_color_ids(blob, "DSCRequiredColorElementIDs", t->dsc); + blob_key(blob, "ColorModes"); + blob_tag(blob, 2, 1 + t->second); + blob_color(blob, 80, 242999, 0, DCP_COLOR_FORMAT_RGB); + if (t->second) + blob_color(blob, 81, 242998, 0, DCP_COLOR_FORMAT_RGB); + blob_bool(blob, "IsPreferred", t->preferred); + blob_bool(blob, "IsInterlaced", false); +} + +static struct dcp_display_mode * +parser_unsafe_modes(struct kunit *test, struct parser_blob *blob, + const struct parser_unsafe_timing *timings, unsigned int n, + bool external_sdr, unsigned int *count) +{ + struct dcp_parse_ctx ctx; + unsigned int i; + + put_unaligned_le32(0xd3, blob->data); + blob->size = 4; + blob_tag(blob, 2, n); + for (i = 0; i < n; i++) + blob_unsafe_timing(blob, &timings[i]); + if (parse(blob->data, blob->size, &ctx)) + return ERR_PTR(-EINVAL); + ctx.dcp = &parser_dcp; + return enumerate_modes(&ctx, count, 600, 340, 0, false, external_sdr); +} + +static const struct dcp_display_mode * +parser_default_mode(const struct dcp_display_mode *modes, unsigned int count) +{ + const struct dcp_display_mode *found = NULL; + unsigned int i; + + for (i = 0; i < count; i++) + if (modes[i].mode.type & DRM_MODE_TYPE_PREFERRED) { + if (found) + return NULL; + found = &modes[i]; + } + return found; +} + +/* + * A Dell S2725QS behind a USB-C to HDMI cable: firmware marks 3840x2160 at + * 60 Hz IsPreferred with the highest score, but lists its only element as + * unsafe (594 MHz is above the 340 MHz of the EDID's HDMI 1.4 block), and + * also prefers 1920x1080 at 120 Hz. macOS drives the 60 Hz timing; so must + * the default. An unsafe timing the firmware does not prefer never is. + */ +static void parser_unsafe_preferred_default(struct kunit *test) +{ + struct parser_unsafe_timing timings[] = { + { 72, 16349, 1920, 2200, 88, 44, 1080, 1125, 4, 5, 120, true, false }, + { 50, 16598, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 60, true, true }, + { 48, 15379, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 30, false, false }, + { 77, 6442, 2560, 2720, 48, 32, 1440, 1525, 3, 5, 120, false, true }, + }; + struct parser_blob *blob = kunit_kzalloc(test, sizeof(*blob), GFP_KERNEL); + const struct dcp_display_mode *def; + struct dcp_display_mode *modes; + unsigned int count, i; + + KUNIT_ASSERT_NOT_NULL(test, blob); + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + false, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 4U); + for (i = 0; i < count; i++) { + KUNIT_EXPECT_EQ(test, modes[i].color_mode_id, 80); + KUNIT_EXPECT_EQ(test, modes[i].sdr_usable, !timings[i].unsafe); + KUNIT_EXPECT_EQ(test, modes[i].sdr_unsafe, timings[i].unsafe); + } + def = parser_default_mode(modes, count); + KUNIT_ASSERT_NOT_NULL(test, def); + KUNIT_EXPECT_EQ(test, def->timing_mode_id, 50); + KUNIT_EXPECT_EQ(test, def->mode.clock, 594000); + kfree(modes); + + /* Not preferred, the unsafe 60 Hz timing leaves 1920x1080 the default. */ + timings[1].preferred = false; + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + false, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 4U); + def = parser_default_mode(modes, count); + KUNIT_ASSERT_NOT_NULL(test, def); + KUNIT_EXPECT_EQ(test, def->timing_mode_id, 72); + kfree(modes); +} + +/* + * A timing only DSC can carry is not an unsafe one: preferred or not, and + * whether or not its element is listed as unsafe too, it is never a default + * candidate. + */ +static void parser_unsafe_preferred_dsc(struct kunit *test) +{ + struct parser_unsafe_timing timings[] = { + { 72, 16349, 1920, 2200, 88, 44, 1080, 1125, 4, 5, 120, true, false }, + { 50, 16598, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 60, true, false, true }, + }; + struct parser_blob *blob = kunit_kzalloc(test, sizeof(*blob), GFP_KERNEL); + const struct dcp_display_mode *def; + struct dcp_display_mode *modes; + unsigned int count, pass; + + KUNIT_ASSERT_NOT_NULL(test, blob); + for (pass = 0; pass < 2; pass++) { + timings[1].unsafe = pass; + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + false, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 2U); + KUNIT_EXPECT_EQ(test, modes[1].timing_mode_id, 50); + KUNIT_EXPECT_TRUE(test, modes[1].firmware_preferred); + KUNIT_EXPECT_FALSE(test, modes[1].sdr_usable); + KUNIT_EXPECT_FALSE(test, modes[1].sdr_unsafe); + KUNIT_EXPECT_FALSE(test, dcp_mode_default_candidate(&modes[1])); + KUNIT_EXPECT_TRUE(test, dcp_mode_default_candidate(&modes[0])); + def = parser_default_mode(modes, count); + KUNIT_ASSERT_NOT_NULL(test, def); + KUNIT_EXPECT_EQ(test, def->timing_mode_id, 72); + kfree(modes); + } +} + +/* + * The external SDR path (T8140) never records an unsafe element: it drops a + * timing whose only element is unsafe, and one with a safe element besides + * is plainly usable. The same catalog elsewhere records both. + */ +static void parser_unsafe_external_sdr(struct kunit *test) +{ + struct parser_unsafe_timing timings[] = { + { 72, 16349, 1920, 2200, 88, 44, 1080, 1125, 4, 5, 120, true, false }, + { 50, 16598, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 60, true, true }, + { 48, 15379, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 30, true, true, + false, true }, + }; + struct parser_blob *blob = kunit_kzalloc(test, sizeof(*blob), GFP_KERNEL); + const struct dcp_display_mode *def; + struct dcp_display_mode *modes; + unsigned int count, i; + + KUNIT_ASSERT_NOT_NULL(test, blob); + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + true, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 2U); + KUNIT_EXPECT_EQ(test, modes[0].timing_mode_id, 72); + KUNIT_EXPECT_EQ(test, modes[1].timing_mode_id, 48); + KUNIT_EXPECT_EQ(test, modes[1].color_mode_id, 81); + for (i = 0; i < count; i++) { + KUNIT_EXPECT_TRUE(test, modes[i].firmware_preferred); + KUNIT_EXPECT_TRUE(test, modes[i].sdr_usable); + KUNIT_EXPECT_FALSE(test, modes[i].sdr_unsafe); + } + def = parser_default_mode(modes, count); + KUNIT_ASSERT_NOT_NULL(test, def); + KUNIT_EXPECT_EQ(test, def->timing_mode_id, 48); + kfree(modes); + + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + false, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 3U); + KUNIT_EXPECT_FALSE(test, modes[1].sdr_usable); + KUNIT_EXPECT_TRUE(test, modes[1].sdr_unsafe); + KUNIT_EXPECT_TRUE(test, modes[2].sdr_usable); + KUNIT_EXPECT_TRUE(test, modes[2].sdr_unsafe); + kfree(modes); +} + +/* + * Two timings of the native resolution, both IsPreferred: the one with a + * usable SDR element is the default, whatever the refresh rate of the unsafe + * one, since nothing falls back to it when the unsafe timing fails. The + * unsafe timing is then out of the ranking altogether, as it was before it + * could be a candidate. + */ +static void parser_unsafe_preferred_usable_wins(struct kunit *test) +{ + struct parser_unsafe_timing timings[] = { + { 72, 16349, 1920, 2200, 88, 44, 1080, 1125, 4, 5, 120, true, false }, + { 50, 16598, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 60, true, true }, + { 48, 15379, 3840, 4400, 176, 88, 2160, 2250, 8, 10, 30, true, false }, + { 46, 15000, 3840, 5500, 1276, 88, 2160, 2250, 8, 10, 24, false, false }, + }; + static const struct { + bool preferred_30, preferred_24; + s64 expect; + } cases[] = { + /* Usable and preferred at 30 Hz beats unsafe and preferred at 60 Hz. */ + { true, false, 48 }, + /* No usable timing is preferred: the Dell S2725QS case, 60 Hz. */ + { false, false, 50 }, + /* + * Only the 24 Hz one is: the unsafe timing is out, and the + * usable ones rank as ever, the higher refresh rate first. + */ + { false, true, 48 }, + }; + struct parser_blob *blob = kunit_kzalloc(test, sizeof(*blob), GFP_KERNEL); + const struct dcp_display_mode *def; + struct dcp_display_mode *modes; + unsigned int count, i; + + KUNIT_ASSERT_NOT_NULL(test, blob); + for (i = 0; i < ARRAY_SIZE(cases); i++) { + timings[2].preferred = cases[i].preferred_30; + timings[3].preferred = cases[i].preferred_24; + modes = parser_unsafe_modes(test, blob, timings, ARRAY_SIZE(timings), + false, &count); + KUNIT_ASSERT_NOT_ERR_OR_NULL(test, modes); + KUNIT_ASSERT_EQ(test, count, 4U); + KUNIT_EXPECT_TRUE(test, dcp_mode_default_candidate(&modes[1])); + def = parser_default_mode(modes, count); + KUNIT_ASSERT_NOT_NULL(test, def); + KUNIT_EXPECT_EQ(test, def->timing_mode_id, cases[i].expect); + kfree(modes); + } +} + static struct kunit_case parser_cases[] = { KUNIT_CASE(parser_internal_v27_only_120hz), KUNIT_CASE(parser_panel_of_dimensions), @@ -2116,6 +2378,10 @@ static struct kunit_case parser_cases[] = { KUNIT_CASE(parser_catalog_release), KUNIT_CASE(parser_property_bounds), KUNIT_CASE(parser_desktop_metadata_timing), + KUNIT_CASE(parser_unsafe_preferred_default), + KUNIT_CASE(parser_unsafe_preferred_dsc), + KUNIT_CASE(parser_unsafe_external_sdr), + KUNIT_CASE(parser_unsafe_preferred_usable_wins), {} }; diff --git a/drivers/gpu/drm/apple/parser.c b/drivers/gpu/drm/apple/parser.c index 28c429e48ecc7..de2cbc5a605aa 100644 --- a/drivers/gpu/drm/apple/parser.c +++ b/drivers/gpu/drm/apple/parser.c @@ -639,6 +639,7 @@ static int parse_color_modes(struct dcp_parse_ctx *handle, out->best.score = -1; out->pq.score = -1; out->sdr_usable = false; + out->sdr_unsafe = false; out->has_bootstrap = false; ret = iterator_begin(handle, &outer_it, false); @@ -730,6 +731,9 @@ static int parse_color_modes(struct dcp_parse_ctx *handle, (!timing_color_contains(&limits->unsafe, cmode.id) && !timing_color_contains(&limits->dsc_required, cmode.id)))) out->sdr_usable = true; + else if (!external_sdr && cmode.eotf == DCP_EOTF_SDR_GAMMA && + !timing_color_contains(&limits->dsc_required, cmode.id)) + out->sdr_unsafe = true; if (external_sdr) { unsigned int rank; @@ -1186,9 +1190,9 @@ static bool dcp_180hz_ids_current(struct dcp_parse_ctx handle, /* * An external display defaults to its native resolution at the highest * refresh rate it can take, as other systems pick it, rather than to the - * timing its EDID lists first. Among the timings of that resolution with a - * usable SDR element, the higher refresh rate ranks first, then IsPreferred, - * then the firmware score. + * timing its EDID lists first. Among the default candidates of that + * resolution, the higher refresh rate ranks first, then IsPreferred, then the + * firmware score. */ VISIBLE_IF_KUNIT bool dcp_mode_default_above(const struct dcp_display_mode *mode, s64 score, @@ -1206,21 +1210,54 @@ VISIBLE_IF_KUNIT bool dcp_mode_default_above(const struct dcp_display_mode *mode return score > best_score; } +/* + * A timing the default may be. Firmware lists a color element as unsafe + * when the timing needs a faster link than it can vouch for: behind a USB-C + * to HDMI cable (a Parade PS176) a Dell S2725QS gets every 3840x2160 60 Hz + * element listed, the timings above the 340 MHz of its EDID's HDMI 1.4 block + * being unsafe though its HDMI 2.x block takes 600 MHz. The firmware still + * marks that timing IsPreferred and scores it highest, and macOS drives it + * at 60 Hz (DPTimingModeId 50, an HBR2 x4 link) from the same list. So a + * timing the firmware prefers stays a candidate when its SDR elements are + * unsafe; one only DSC can carry does not, and neither does an unsafe timing + * the firmware does not prefer. dcp_external_default() still passes it over + * when the firmware also prefers a usable timing of the same resolution. + */ +VISIBLE_IF_KUNIT bool dcp_mode_default_candidate(const struct dcp_display_mode *m) +{ + if (m->automatic_rgb8_quirk) + return false; + return m->sdr_usable || (m->sdr_unsafe && m->firmware_preferred); +} + +static bool dcp_mode_same_size(const struct dcp_display_mode *a, + const struct dcp_display_mode *b) +{ + return a->mode.hdisplay == b->mode.hdisplay && + a->mode.vdisplay == b->mode.vdisplay; +} + /* * The native resolution: the largest the firmware marks IsPreferred, or the * largest of all when it marks none. + * + * Nothing falls back to another timing when the default fails to light up, + * so a timing with only unsafe SDR elements is the default only when the + * firmware prefers no usable timing of the native resolution. With one, the + * unsafe timings are left out and the usable ones rank among themselves. */ static struct dcp_display_mode *dcp_external_default(struct dcp_display_mode *modes, unsigned int count) { struct dcp_display_mode *native = NULL, *best = NULL; + bool usable_preferred = false; unsigned int i; for (i = 0; i < count; i++) { const struct dcp_display_mode *m = &modes[i]; u64 area = (u64)m->mode.hdisplay * m->mode.vdisplay; - if (m->automatic_rgb8_quirk || !m->sdr_usable) + if (!dcp_mode_default_candidate(m)) continue; if (!native || (m->firmware_preferred && !native->firmware_preferred) || @@ -1230,12 +1267,18 @@ static struct dcp_display_mode *dcp_external_default(struct dcp_display_mode *mo } if (!native) return NULL; + for (i = 0; i < count; i++) { + const struct dcp_display_mode *m = &modes[i]; + + if (dcp_mode_default_candidate(m) && dcp_mode_same_size(m, native) && + m->sdr_usable && m->firmware_preferred) + usable_preferred = true; + } for (i = 0; i < count; i++) { struct dcp_display_mode *m = &modes[i]; - if (m->automatic_rgb8_quirk || !m->sdr_usable || - m->mode.hdisplay != native->mode.hdisplay || - m->mode.vdisplay != native->mode.vdisplay) + if (!dcp_mode_default_candidate(m) || !dcp_mode_same_size(m, native) || + (usable_preferred && !m->sdr_usable)) continue; if (!best || dcp_mode_default_above(m, m->score, best, best->score)) best = m; diff --git a/drivers/gpu/drm/apple/parser.h b/drivers/gpu/drm/apple/parser.h index f39ec76b9bb0b..607aa029adca7 100644 --- a/drivers/gpu/drm/apple/parser.h +++ b/drivers/gpu/drm/apple/parser.h @@ -163,6 +163,11 @@ struct dcp_display_mode { bool firmware_preferred; /* An SDR color element that is neither unsafe nor DSC only */ bool sdr_usable; + /* + * An SDR color element the firmware lists as unsafe, but not DSC + * only, outside the external SDR path + */ + bool sdr_unsafe; /* The firmware score of the timing */ s64 score; /* The sync polarity flags of the timing, if it reports them */ @@ -202,6 +207,7 @@ bool dcp_property_complete(const void *blob, size_t received, size_t advertised) #if IS_ENABLED(CONFIG_KUNIT) bool dcp_mode_default_above(const struct dcp_display_mode *mode, s64 score, const struct dcp_display_mode *best, s64 best_score); +bool dcp_mode_default_candidate(const struct dcp_display_mode *m); bool dcp_mode_ranks_above(bool preferred, s64 score, bool best_preferred, s64 best_score); #endif