diff --git a/drivers/gpu/drm/apple/dcp-display-metadata.c b/drivers/gpu/drm/apple/dcp-display-metadata.c index 1e393c2ea9d2b..7915e0e9842d7 100644 --- a/drivers/gpu/drm/apple/dcp-display-metadata.c +++ b/drivers/gpu/drm/apple/dcp-display-metadata.c @@ -198,6 +198,26 @@ const struct dcp_color_space *dcp_panel_gamut_color_space(enum dcp_panel_gamut g } } +/* How far an EDID's measured or rounded coordinates may be from the named ones, in 1/1024. */ +#define DCP_EDID_COLOR_TOLERANCE 5 + +/* Whether the primaries and the white of @edid are those of @space. */ +bool dcp_edid_has_color_space(const struct edid *edid, const struct dcp_color_space *space) +{ + const u8 hi[] = { edid->red_x, edid->red_y, edid->green_x, edid->green_y, + edid->blue_x, edid->blue_y, edid->white_x, edid->white_y }; + + for (unsigned int i = 0; i < ARRAY_SIZE(hi); i++) { + u8 lo = i < 4 ? edid->red_green_lo : edid->blue_white_lo; + int value = hi[i] << 2 | (lo >> (6 - 2 * (i % 4)) & 3); + u32 named = i < 6 ? space->chromaticity[i / 2][i % 2] : space->white[i - 6]; + + if (abs(value - (int)DIV_ROUND_CLOSEST(named, 64)) > DCP_EDID_COLOR_TOLERANCE) + return false; + } + return true; +} + /* * An internal panel's colour target: the standard space its ADT names, or * else Display P3. Every untagged RGB surface on an internal panel is tagged diff --git a/drivers/gpu/drm/apple/dcp-display-metadata.h b/drivers/gpu/drm/apple/dcp-display-metadata.h index 9a12bebaacbf2..287f20ccdeb96 100644 --- a/drivers/gpu/drm/apple/dcp-display-metadata.h +++ b/drivers/gpu/drm/apple/dcp-display-metadata.h @@ -52,6 +52,9 @@ const struct dcp_color_space *dcp_color_space_lookup(const char *name); const struct dcp_color_space *dcp_panel_gamut_color_space(enum dcp_panel_gamut gamut); const struct dcp_color_space *dcp_panel_color_target(enum dcp_panel_gamut gamut); +struct edid; +bool dcp_edid_has_color_space(const struct edid *edid, const struct dcp_color_space *space); + struct drm_edid; struct drm_display_mode; struct dcp_display_mode; diff --git a/drivers/gpu/drm/apple/dcp-m4-frame.h b/drivers/gpu/drm/apple/dcp-m4-frame.h index 35a72fe59e882..883a3dd184913 100644 --- a/drivers/gpu/drm/apple/dcp-m4-frame.h +++ b/drivers/gpu/drm/apple/dcp-m4-frame.h @@ -109,6 +109,9 @@ static inline int dcp_m4_direct_frame(struct dcp_m4_frame *frame, u64 dva, u64 m return dcp_m4_direct_frame_at(frame, dva, mapped, width, height, pitch, alpha, 0); } +/* A surface's primaries: those of the panel, Display P3 with a D65 white. */ +#define DCP_M4_SURFACE_COLORSPACE 0x14 + /* Exact single-layer fixture, not an ABI for tiled or compressed surfaces. */ static inline void dcp_m4_surface_encode(u8 *surface, const struct dcp_m4_frame *frame) { @@ -120,7 +123,7 @@ static inline void dcp_m4_surface_encode(u8 *surface, const struct dcp_m4_frame put_unaligned_le32(1, surface + 7); put_unaligned_le32(0x42475241, surface + 0x0b); surface[0x13] = 13; - surface[0x14] = 12; + surface[DCP_M4_SURFACE_COLORSPACE] = 12; put_unaligned_le32(frame->stride, surface + 0x15); put_unaligned_le16(1, surface + 0x19); surface[0x1b] = 1; @@ -315,6 +318,22 @@ static inline void dcp_m4_swap_set_intent(void *output, u32 intent, u32 surface_ put_unaligned_le32(surface_id, data + 0x9c); } +/* + * Give the surfaces of an encoded swap (surface 0 if it presents @primary, and + * @upper's) other primaries than the panel's. + */ +static inline void dcp_m4_swap_set_colorspace(void *output, bool primary, + const struct dcp_m4_upper *upper, u8 colorspace) +{ + u8 *surfaces = (u8 *)output + dcp_m4_pf->swap_surface; + + if (primary) + surfaces[DCP_M4_SURFACE_COLORSPACE] = colorspace; + if (upper && upper->frame) + surfaces[DCP_M4_SURFACE_SIZE * upper->index + DCP_M4_SURFACE_COLORSPACE] = + colorspace; +} + /* * Set an encoded swap's presentation deadline: IOMobileFramebuffer timestamp * types 1, 2 and 7 (+0x0, +0x8, +0x30), as the firmware 26 desktop path diff --git a/drivers/gpu/drm/apple/dcp-sdr-edid.h b/drivers/gpu/drm/apple/dcp-sdr-edid.h index 5b8c710de8d7e..f5f7aba51e3d0 100644 --- a/drivers/gpu/drm/apple/dcp-sdr-edid.h +++ b/drivers/gpu/drm/apple/dcp-sdr-edid.h @@ -83,4 +83,24 @@ static inline size_t dcp_edid_sdr(u8 *edid, size_t size) return output; } +/* + * dcp_edid_sdr() for an output without an sRGB contract, whose link carries + * SDR RGB in the sink's own gamut: the sink's primaries are what its frames + * are shown with, so they stay. + */ +static inline size_t dcp_edid_sdr_native(u8 *edid, size_t size) +{ + u8 primaries[DCP_EDID_CHROMATICITY_SIZE]; + + if (size < DCP_EDID_BLOCK_SIZE) + return 0; + memcpy(primaries, edid + DCP_EDID_CHROMATICITY, sizeof(primaries)); + size = dcp_edid_sdr(edid, size); + if (size) { + memcpy(edid + DCP_EDID_CHROMATICITY, primaries, sizeof(primaries)); + dcp_edid_checksum(edid); + } + return size; +} + #endif diff --git a/drivers/gpu/drm/apple/iomfb_m4.c b/drivers/gpu/drm/apple/iomfb_m4.c index dd3e573e89044..aa343a6a13750 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.c +++ b/drivers/gpu/drm/apple/iomfb_m4.c @@ -49,6 +49,7 @@ #include "afk.h" #include "dcp.h" +#include "dcp-display-metadata.h" #include "dcp-sdr-edid.h" #include "dptxep.h" #include "epic/dpavservep.h" @@ -63,6 +64,7 @@ #include "dcp-m4-property.h" #include "dcp-m4-frame.h" #include "dcp-refresh.h" +#include "iomfb_plane.h" #include "parser.h" #include "t8132_ext_drm.h" @@ -281,6 +283,8 @@ struct iomfb_m4 { const struct drm_edid *ext_edid; int ext_edid_link_gen; unsigned int ext_edid_avserv_gen; + /* The primaries its frames carry, see iomfb_m4_ext_edid_published() */ + u8 ext_colorspace; /* * The sink generation, which a released route moves on (words received * before carry the old one and publish nothing); whether a @@ -2263,6 +2267,9 @@ static int dcp_m4_runtime_swap_full(struct dcp_m4_runtime *r, bool primary, if (!ret) ret = dcp_m4_swap_encode_on(input, DCP_M4_SWAP_SIZE, id, primary, frame, upper, keep, brightness_change, nits, check, width, height); + if (!ret && ext) + dcp_m4_swap_set_colorspace(input, primary && frame, upper, + READ_ONCE(dcp->ext_colorspace)); if (!ret && intent) dcp_m4_swap_set_intent(input, intent, surface_id); /* @@ -3334,10 +3341,10 @@ int iomfb_m4_ext_get_modes(struct apple_dcp *owner, struct drm_connector *connec } /* - * The EDID the connector publishes, as native26's (dcp_edid_sdr()): this - * scanout is 8-bpc RGB SDR, so the sink's colour capabilities, deep colour - * and 4:2:0 timings go. The last would otherwise make the probe helper prune - * the catalog's mode of a timing the sink lists as 4:2:0 only. + * The EDID the connector publishes (dcp_edid_sdr_native()): this scanout is + * 8-bpc RGB SDR, so the sink's colour capabilities, deep colour and 4:2:0 + * timings go. The last would otherwise make the probe helper prune the + * catalog's mode of a timing the sink lists as 4:2:0 only. Its primaries stay. */ static const struct drm_edid *iomfb_m4_ext_edid_sdr(const struct drm_edid *edid) { @@ -3352,7 +3359,7 @@ static const struct drm_edid *iomfb_m4_ext_edid_sdr(const struct drm_edid *edid) copy = kmemdup(raw, size, GFP_KERNEL); if (!copy) return ERR_PTR(-ENOMEM); - size = dcp_edid_sdr(copy, size); + size = dcp_edid_sdr_native(copy, size); sdr = size ? drm_edid_alloc(copy, size) : ERR_PTR(-EPROTO); kfree(copy); return sdr ?: ERR_PTR(-ENOMEM); @@ -3452,6 +3459,22 @@ const struct drm_edid *iomfb_m4_ext_edid(struct apple_dcp *owner) return edid; } +/* + * The firmware converts a frame from the primaries it is tagged with to the + * sink's, and reads an untagged frame as sRGB. A compositor draws in the gamut + * of the EDID the connector publishes, so a Display P3 one's frames are tagged + * Display P3, as the panel's are, and any other's stay untagged. + */ +void iomfb_m4_ext_edid_published(struct apple_dcp *owner, const struct edid *edid) +{ + struct iomfb_m4 *dcp = owner->v26_6; + bool p3 = edid && dcp_edid_has_color_space(edid, dcp_color_space_lookup("display-p3")); + + if (dcp && dcp->external) + WRITE_ONCE(dcp->ext_colorspace, + p3 ? DCP_COLORSPACE_P3_D65 : DCP_COLORSPACE_UNSPECIFIED); +} + /* * An adopted external DCP whose IOMFB session opened (A401, open, drain * and a healthy runtime) and has not failed since. Lock-free: the runtime is @@ -3966,8 +3989,9 @@ static_assert(sizeof(struct dcp_m4_mode_info_req) == 0x7c); * The colour element of a timing: RGB with the SDR gamma EOTF, not virtual, * not unsafe for the timing, at 8 bpc if the timing lists it as needing DSC; * the lowest depth offered (8 bpc if there is one); at that depth one that - * needs no DSC over one that does, then sRGB/BT.709 colorimetry and full - * range over others, then the higher score. Never PQ or HLG. + * needs no DSC over one that does, then Default RGB colorimetry (the sink's + * native gamut) over sRGB/BT.709 over others, then full range, then the higher + * score. Never PQ or HLG. */ struct dcp_m4_ext_pick { struct dcp_color_element best; @@ -3976,9 +4000,14 @@ struct dcp_m4_ext_pick { static unsigned int dcp_m4_ext_color_rank(const struct dcp_color_element *e) { - return (e->colorimetry == DCP_COLORIMETRY_SRGB || - e->colorimetry == DCP_COLORIMETRY_BT709 ? 2 : 0) + - (e->range == DCP_COLOR_YCBCR_RANGE_FULL ? 1 : 0); + unsigned int space = 0; + + if (e->colorimetry == DCP_COLORIMETRY_RGB) + space = 4; + else if (e->colorimetry == DCP_COLORIMETRY_SRGB || + e->colorimetry == DCP_COLORIMETRY_BT709) + space = 2; + return space + (e->range == DCP_COLOR_YCBCR_RANGE_FULL ? 1 : 0); } static void dcp_m4_ext_pick_color(void *data, const struct dcp_color_element *e) diff --git a/drivers/gpu/drm/apple/iomfb_m4.h b/drivers/gpu/drm/apple/iomfb_m4.h index dce9aeec38448..6c48fae8d9528 100644 --- a/drivers/gpu/drm/apple/iomfb_m4.h +++ b/drivers/gpu/drm/apple/iomfb_m4.h @@ -12,6 +12,7 @@ struct device; struct dentry; struct drm_connector; struct drm_edid; +struct edid; struct drm_display_mode; struct drm_rect; struct iosys_map; @@ -54,6 +55,8 @@ void iomfb_m4_external_link_gone(struct apple_dcp *dcp); int iomfb_m4_ext_detect(struct apple_dcp *dcp); int iomfb_m4_ext_get_modes(struct apple_dcp *dcp, struct drm_connector *connector); const struct drm_edid *iomfb_m4_ext_edid(struct apple_dcp *dcp); +/* The EDID its connector now publishes, NULL for none: the gamut of its frames. */ +void iomfb_m4_ext_edid_published(struct apple_dcp *dcp, const struct edid *edid); /* DRM drives the external output: modeset, copied frames, disable. */ int iomfb_m4_ext_mode_ids(struct apple_dcp *dcp, const struct drm_display_mode *mode, struct iomfb_m4_ext_ids *ids); @@ -122,6 +125,11 @@ static inline const struct drm_edid *iomfb_m4_ext_edid(struct apple_dcp *dcp) return ERR_PTR(-ENODEV); } +static inline void iomfb_m4_ext_edid_published(struct apple_dcp *dcp, + const struct edid *edid) +{ +} + static inline int iomfb_m4_ext_mode_ids(struct apple_dcp *dcp, const struct drm_display_mode *mode, struct iomfb_m4_ext_ids *ids) diff --git a/drivers/gpu/drm/apple/parser-test.c b/drivers/gpu/drm/apple/parser-test.c index 340e1157108d6..28f02f5fde975 100644 --- a/drivers/gpu/drm/apple/parser-test.c +++ b/drivers/gpu/drm/apple/parser-test.c @@ -1555,6 +1555,35 @@ static void parser_panel_color_target(struct kunit *test) KUNIT_EXPECT_EQ(test, raw->gamma, 0xff); } +/* An EDID's own primaries name a colour space, within its rounding. */ +static void parser_edid_color_space(struct kunit *test) +{ + const struct dcp_color_space *p3 = dcp_color_space_lookup("display-p3"); + const struct dcp_color_space *srgb = dcp_color_space_lookup("srgb"); + struct dcp_display_metadata metadata = { + .manufacturer = "APP", .product = 0x616, .name = "Apple Panel", + }; + struct edid edid; + + edid = *panel_color_edid(test, &metadata, p3); + KUNIT_EXPECT_TRUE(test, dcp_edid_has_color_space(&edid, p3)); + KUNIT_EXPECT_FALSE(test, dcp_edid_has_color_space(&edid, srgb)); + /* Red x 701/1024 is within the tolerance of 696, 702 is not. */ + edid.red_x = 701 >> 2; + edid.red_green_lo = (edid.red_green_lo & 0x3f) | (701 & 3) << 6; + KUNIT_EXPECT_TRUE(test, dcp_edid_has_color_space(&edid, p3)); + edid.red_green_lo = (edid.red_green_lo & 0x3f) | (702 & 3) << 6; + KUNIT_EXPECT_FALSE(test, dcp_edid_has_color_space(&edid, p3)); + /* Display P3 primaries with a D50 white are another space. */ + edid = *panel_color_edid(test, &metadata, p3); + edid.white_x = 353 >> 2; + KUNIT_EXPECT_FALSE(test, dcp_edid_has_color_space(&edid, p3)); + /* No primaries at all. */ + edid = (struct edid){}; + KUNIT_EXPECT_FALSE(test, dcp_edid_has_color_space(&edid, p3)); + KUNIT_EXPECT_FALSE(test, dcp_edid_has_color_space(&edid, srgb)); +} + static size_t parser_adt_property(u8 *out, size_t capacity, const char *name, const void *value, u32 size) { @@ -2426,6 +2455,7 @@ static struct kunit_case parser_cases[] = { KUNIT_CASE(parser_panel_identity_stable), KUNIT_CASE(parser_panel_identity), KUNIT_CASE(parser_panel_color_target), + KUNIT_CASE(parser_edid_color_space), KUNIT_CASE(parser_panel_adt_color_target), KUNIT_CASE(parser_tiled_size_keeps_dsc_modes), KUNIT_CASE(parser_score_presence), diff --git a/drivers/gpu/drm/apple/t8132_ext_drm.c b/drivers/gpu/drm/apple/t8132_ext_drm.c index 508547909f287..a2f08e3fd1d2e 100644 --- a/drivers/gpu/drm/apple/t8132_ext_drm.c +++ b/drivers/gpu/drm/apple/t8132_ext_drm.c @@ -640,6 +640,7 @@ static int t8132_ext_get_modes(struct drm_connector *connector) { struct t8132_ext_out *out = container_of(connector, struct t8132_ext_out, connector); struct apple_dcp *owner = t8132_ext_owner(out); + const struct drm_property_blob *blob; const struct drm_edid *edid; int count; @@ -653,6 +654,9 @@ static int t8132_ext_get_modes(struct drm_connector *connector) edid = NULL; } drm_edid_connector_update(connector, edid); + /* A failed update may have left the previous EDID published. */ + blob = connector->edid_blob_ptr; + iomfb_m4_ext_edid_published(owner, blob ? blob->data : NULL); drm_edid_free(edid); return count; } diff --git a/tools/asahi/dcpext26-edid-tests.py b/tools/asahi/dcpext26-edid-tests.py index 0171c1ece0fde..23e040def503a 100644 --- a/tools/asahi/dcpext26-edid-tests.py +++ b/tools/asahi/dcpext26-edid-tests.py @@ -77,12 +77,19 @@ def function(name): memcpy(original,sdr,256);assert(dcp_edid_sdr(sdr,256)==256);assert(!memcmp(sdr,original,256)); for(unsigned n=0;n<128;n++)assert(!dcp_edid_sdr(sdr,n)); assert(!dcp_edid_sdr(sdr,128)); + /* The sink's own primaries kept, the rest as above and the checksum right. */ + u8 native[384];memset(sdr+25,0x55,10);checksum(sdr);memcpy(native,sdr,sizeof(native)); + assert(dcp_edid_sdr(sdr,256)==256);assert(dcp_edid_sdr_native(native,256)==256); + for(unsigned i=0;i<256;i++)assert(native[i]==(i>=25&&i<35?0x55:i==127?native[127]:sdr[i])); + {u8 sum=0;for(unsigned j=0;j<128;j++)sum+=native[j];assert(!sum);} + for(unsigned n=0;n<128;n++)assert(!dcp_edid_sdr_native(native,n)); + assert(!dcp_edid_sdr_native(native,128)); /* A CTA with no data collection can still advertise basic stereo audio. */ memset(sdr,0,sizeof(sdr));memcpy(sdr,header,8);sdr[126]=1; cta=sdr+128;cta[0]=2;cta[1]=3;cta[3]=0x40;checksum(sdr);checksum(cta); assert(dcp_edid_sdr(sdr,256)==256);assert(sdr[126]==1 && cta[3]==0x40); - puts("PASS SDR EDID identity, audio, timing, colour removal, checksums, bounds and idempotence"); + puts("PASS SDR EDID identity, audio, timing, colour removal, native primaries, checksums, bounds and idempotence"); puts("PASS native26 AV identity, stale attachment/owner, bounded EDID length, extension count and checksum failures"); } ''' diff --git a/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py b/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py index 8c4267a2dc8f7..616b443e3a189 100644 --- a/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py +++ b/tools/testing/selftests/drivers/gpu/apple-dcp/rate.py @@ -1072,8 +1072,10 @@ def publication(root): if 'dcp_edid_sdr(' in publication: # Keep extracting the real publication path, including its SDR helper. # Model only kernel allocation/DRM ownership at the host boundary. + # dcp_edid_sdr_native() is the M4 outputs', not this path's. sdr = '\n'.join(line for line in (base / 'dcp-sdr-edid.h').read_text().splitlines() - if not line.startswith('#include')) + if not line.startswith('#include')).replace( + 'static inline ', 'static inline __attribute__((unused)) ') publication = sdr + r""" static void *kmemdup(const void *data,size_t size,int gfp) { (void)gfp;void *copy=malloc(size);if(copy)memcpy(copy,data,size);return copy;