Igt-dev Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Gil Dekel <gildekel@google.com>
To: igt-dev@lists.freedesktop.org
Cc: Gil Dekel <gildekel@google.com>,
	Swati Sharma <swati2.sharma@intel.com>,
	 Karthik B S <karthik.b.s@intel.com>,
	Mark Yacoub <markyacoub@google.com>
Subject: [PATCH i-g-t v2 1/3] lib/igt_hdr: Add a testable igt_is_edid_hdr()
Date: Sat, 14 Nov 2026 19:19:02 -0500	[thread overview]
Message-ID: <20261115001917.2933254-2-gildekel@google.com> (raw)
In-Reply-To: <20261115001917.2933254-1-gildekel@google.com>

igt_is_panel_hdr() reads the EDID blob from the connector and parses
it in one go, so the parser can only be exercised on real hardware. It
also trusts the EDID for its bounds: it walks extensions_len blocks
whatever the blob size is, and it walks CTA data blocks up to
dtd_start bytes from the start of the data block collection, which
begins 4 bytes into the extension. On a malformed EDID it reads past
the blob.

Move the parsing into igt_is_edid_hdr(), which takes the raw EDID and
its size. Walk at most the extensions the buffer holds, and only the
CTA data block collection, bytes 4 to dtd_start - 1, without letting a
data block run past it. Return as soon as an extension advertises HDR.
Before, a later CTA extension without HDR could overwrite an earlier
positive result. igt_is_panel_hdr() now just fetches the blob.

Add lib/tests/igt_hdr with CTA extension cases for HDR, SDR only, and
an EDID whose extension is cut off.

Cc: Swati Sharma <swati2.sharma@intel.com>
Cc: Karthik B S <karthik.b.s@intel.com>
Cc: Mark Yacoub <markyacoub@google.com>
Assisted-by: Claude:claude-opus-5.5
Signed-off-by: Gil Dekel <gildekel@google.com>
---

Notes:
    v2: No changes.

 lib/igt_hdr.c         | 110 +++++++++++++++++++++++++++++-------------
 lib/igt_hdr.h         |   1 +
 lib/tests/igt_hdr.c   |  54 +++++++++++++++++++++
 lib/tests/meson.build |   1 +
 4 files changed, 133 insertions(+), 33 deletions(-)
 create mode 100644 lib/tests/igt_hdr.c

diff --git a/lib/igt_hdr.c b/lib/igt_hdr.c
index c0ed9e843..8c36a035e 100644
--- a/lib/igt_hdr.c
+++ b/lib/igt_hdr.c
@@ -24,6 +24,7 @@
 
 /* HDR EDID parsing. */
 #define CTA_EXTENSION_VERSION		0x03
+#define CTA_DATA_BLOCK_OFFSET		4
 #define HDR_STATIC_METADATA_BLOCK	0x06
 #define USE_EXTENDED_TAG		0x07
 
@@ -49,62 +50,105 @@ static bool cta_block(const char *edid_ext)
 	return false;
 }
 
+/*
+ * Walks a CTA-861 data block collection of @len bytes and looks for an
+ * HDR Static Metadata Block.
+ */
+static bool cta_data_blocks_have_hdr(const uint8_t *db, int len)
+{
+	int i = 0;
+
+	while (i < len) {
+		int db_len = db[i] & 0x1f;
+
+		if (i + 1 + db_len > len)
+			break;
+
+		/* cta_block() reads the tag, extended tag and EOTF bytes. */
+		if (db_len >= 2 && cta_block((const char *)&db[i]))
+			return true;
+
+		i += 1 + db_len;
+	}
+
+	return false;
+}
+
+static bool cta_ext_has_hdr(const struct edid_cea *cea)
+{
+	/* Data blocks sit between the 4-byte header and the first DTD. */
+	int len = min_t(int, cea->dtd_start - CTA_DATA_BLOCK_OFFSET,
+			sizeof(cea->data));
+
+	/* HDR not defined in CTA Extension Version < 3. */
+	if (cea->revision != CTA_EXTENSION_VERSION)
+		return false;
+
+	return len > 0 &&
+	       cta_data_blocks_have_hdr((const uint8_t *)cea->data, len);
+}
+
+/**
+ * igt_is_edid_hdr:
+ * @data: raw EDID, the base block followed by its extension blocks
+ * @size: size of @data in bytes
+ *
+ * Looks for an HDR Static Metadata Block in the CTA extensions of an
+ * EDID. A sink is considered HDR-capable when it advertises support for
+ * Traditional Gamma HDR or SMPTE ST 2084 EOTFs. Never reads past @size.
+ *
+ * Returns: true if the EDID advertises HDR support, false otherwise.
+ */
+bool igt_is_edid_hdr(const void *data, size_t size)
+{
+	const struct edid *edid = data;
+	int num_ext;
+
+	if (size < EDID_BLOCK_SIZE)
+		return false;
+
+	/* Don't trust extensions_len beyond what @data holds. */
+	num_ext = min_t(int, edid->extensions_len,
+			(int)(size / EDID_BLOCK_SIZE) - 1);
+
+	for (int i = 0; i < num_ext; i++) {
+		const struct edid_ext *ext = &edid->extensions[i];
+
+		if (ext->tag == EDID_EXT_CEA && cta_ext_has_hdr(&ext->data.cea))
+			return true;
+	}
+
+	return false;
+}
+
 /**
  * igt_is_panel_hdr:
  * @fd: DRM file descriptor
  * @output: output to check
  *
- * Parses the EDID of the given output looking for an HDR Static Metadata
- * Block in the CTA extension. A panel is considered HDR-capable when it
- * advertises support for Traditional Gamma HDR or SMPTE ST 2084 EOTFs.
+ * Checks the EDID of the given output with igt_is_edid_hdr().
  *
  * Returns: true if the panel supports HDR, false otherwise.
  */
 bool igt_is_panel_hdr(int fd, igt_output_t *output)
 {
 	bool ok;
-	int i, j, offset;
 	uint64_t edid_blob_id;
 	drmModePropertyBlobRes *edid_blob;
-	const struct edid_ext *edid_ext;
-	const struct edid *edid;
-	const struct edid_cea *edid_cea;
-	const char *cea_data;
-	bool ret = false;
+	bool ret;
 
 	ok = kmstest_get_property(fd, output->id,
 			DRM_MODE_OBJECT_CONNECTOR, "EDID",
 			NULL, &edid_blob_id, NULL);
 
 	if (!ok || !edid_blob_id)
-		return ret;
+		return false;
 
 	edid_blob = drmModeGetPropertyBlob(fd, edid_blob_id);
 	igt_assert(edid_blob);
+	igt_assert(edid_blob->data);
 
-	edid = (const struct edid *) edid_blob->data;
-	igt_assert(edid);
-
-	for (i = 0; i < edid->extensions_len; i++) {
-		edid_ext = &edid->extensions[i];
-		edid_cea = &edid_ext->data.cea;
-
-		/* HDR not defined in CTA Extension Version < 3. */
-		if ((edid_ext->tag != EDID_EXT_CEA) ||
-		    (edid_cea->revision != CTA_EXTENSION_VERSION))
-				continue;
-		else {
-			offset = edid_cea->dtd_start;
-			cea_data = edid_cea->data;
-
-			for (j = 0; j < offset; j += (cea_data[j] & 0x1f) + 1) {
-				ret = cta_block(cea_data + j);
-
-				if (ret)
-					break;
-			}
-		}
-	}
+	ret = igt_is_edid_hdr(edid_blob->data, edid_blob->length);
 
 	drmModeFreePropertyBlob(edid_blob);
 
diff --git a/lib/igt_hdr.h b/lib/igt_hdr.h
index 15ce5f117..f8988b36c 100644
--- a/lib/igt_hdr.h
+++ b/lib/igt_hdr.h
@@ -15,6 +15,7 @@ enum hdmi_metadata_type {
 	HDMI_STATIC_METADATA_TYPE1 = 0,
 };
 
+bool igt_is_edid_hdr(const void *data, size_t size);
 bool igt_is_panel_hdr(int fd, igt_output_t *output);
 
 void igt_hdr_fill_st2084(struct hdr_output_metadata *meta);
diff --git a/lib/tests/igt_hdr.c b/lib/tests/igt_hdr.c
new file mode 100644
index 000000000..628d4b2e6
--- /dev/null
+++ b/lib/tests/igt_hdr.c
@@ -0,0 +1,54 @@
+// SPDX-License-Identifier: MIT
+/*
+ * Copyright © 2026 Google
+ */
+
+#include <string.h>
+
+#include "igt_core.h"
+#include "igt_hdr.h"
+
+static const uint8_t edid_header[] = {
+	0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
+};
+
+/*
+ * Base block plus one CTA-861 extension whose only data block is an HDR
+ * Static Metadata Data Block advertising @eotf.
+ */
+static void make_cta_edid(uint8_t edid[2 * EDID_BLOCK_SIZE], uint8_t eotf)
+{
+	uint8_t *cta = edid + EDID_BLOCK_SIZE;
+
+	memset(edid, 0, 2 * EDID_BLOCK_SIZE);
+	memcpy(edid, edid_header, sizeof(edid_header));
+	edid[126] = 1;	/* extension count */
+
+	cta[0] = 0x02;	/* CTA-861 extension tag */
+	cta[1] = 0x03;	/* revision 3 */
+	cta[2] = 0x08;	/* DTDs start after one 4-byte data block */
+	cta[4] = 0xe3;	/* extended tag data block, 3 payload bytes */
+	cta[5] = 0x06;	/* HDR Static Metadata Data Block */
+	cta[6] = eotf;
+	cta[7] = 0x01;	/* static metadata descriptor type 1 */
+}
+
+int igt_main()
+{
+	uint8_t edid[2 * EDID_BLOCK_SIZE];
+
+	igt_subtest("cta-hdr") {
+		make_cta_edid(edid, 0x05);	/* SDR gamma + SMPTE ST 2084 */
+		igt_assert(igt_is_edid_hdr(edid, sizeof(edid)));
+	}
+
+	igt_subtest("cta-sdr-only") {
+		make_cta_edid(edid, 0x01);	/* SDR gamma */
+		igt_assert(!igt_is_edid_hdr(edid, sizeof(edid)));
+	}
+
+	igt_subtest("cta-truncated") {
+		make_cta_edid(edid, 0x05);
+		igt_assert(!igt_is_edid_hdr(edid, EDID_BLOCK_SIZE));
+	}
+}
diff --git a/lib/tests/meson.build b/lib/tests/meson.build
index 18967e592..5e7e2d049 100644
--- a/lib/tests/meson.build
+++ b/lib/tests/meson.build
@@ -11,6 +11,7 @@ lib_tests = [
 	'igt_facts',
 	'igt_fork',
 	'igt_fork_helper',
+	'igt_hdr',
 	'igt_hook',
 	'igt_hook_integration',
 	'igt_kms_array_diff',
-- 
Gil Dekel, Software Engineer, Google / ChromeOS Display and Graphics


  reply	other threads:[~2026-11-16  5:28 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-14 22:29 [PATCH i-g-t 0/3] lib/igt_hdr: Find HDR in DisplayID EDIDs Gil Dekel
2026-11-14 22:29 ` [PATCH i-g-t 1/3] lib/igt_hdr: Add a testable igt_is_edid_hdr() Gil Dekel
2026-11-14 22:29 ` [PATCH i-g-t 2/3] lib/igt_hdr: Parse DisplayID CTA data blocks Gil Dekel
2026-11-14 22:29 ` [PATCH i-g-t 3/3] tests/kms_hdr: Use /dev/kmsg in invalid-hdr Gil Dekel
2026-11-15  0:19 ` [PATCH i-g-t v2 0/3] lib/igt_hdr: Find HDR in DisplayID EDIDs Gil Dekel
2026-11-15  0:19   ` Gil Dekel [this message]
2026-11-15  0:19   ` [PATCH i-g-t v2 2/3] lib/igt_hdr: Parse DisplayID CTA data blocks Gil Dekel
2026-11-15  0:19   ` [PATCH i-g-t v2 3/3] tests/kms_hdr: Use /dev/kmsg in invalid-hdr Gil Dekel
2026-11-16  7:51 ` ✓ Xe.CI.BAT: success for lib/igt_hdr: Find HDR in DisplayID EDIDs Patchwork
2026-11-16  8:07 ` ✓ i915.CI.BAT: " Patchwork
2026-11-16  9:07 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-11-16  9:40 ` [PATCH i-g-t 0/3] " Jani Nikula
2026-11-17  3:13   ` Gil Dekel
2026-11-16 11:26 ` ✗ i915.CI.Full: failure for " Patchwork

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261115001917.2933254-2-gildekel@google.com \
    --to=gildekel@google.com \
    --cc=igt-dev@lists.freedesktop.org \
    --cc=karthik.b.s@intel.com \
    --cc=markyacoub@google.com \
    --cc=swati2.sharma@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox