All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kunal Joshi <kunal1.joshi@intel.com>
To: igt-dev@lists.freedesktop.org
Cc: Kunal Joshi <kunal1.joshi@intel.com>
Subject: [PATCH i-g-t 11/13] lib/igt_dp: Add DPCD read helpers
Date: Wed, 16 Sep 2026 10:17:59 +0530	[thread overview]
Message-ID: <20260916044801.1279102-12-kunal1.joshi@intel.com> (raw)
In-Reply-To: <20260916044801.1279102-1-kunal1.joshi@intel.com>

Reading the DPCD of a connector means finding the drm_dp_aux child of
its sysfs directory and reading from the matching character device.
Four tests already carry their own copy of that: kms_dp_aux_dev,
amd_assr, amd_replay and amd_multidisplay_modeset.

Add the AUX channel lookup and the DPCD read to the library so that new
tests do not have to add a fifth. The helpers are read only, since
writing DPCD from userspace races with the driver doing the same.

Use pread() rather than lseek() plus read(), which is both shorter and
free of the failure the amd_assr copy has, where the lseek() return
value is asserted to be non-zero and a read at DPCD offset 0 would
therefore fail.

Assisted-by: GitHub_Copilot:claude-opus-5
Signed-off-by: Kunal Joshi <kunal1.joshi@intel.com>
---
 lib/igt_dp.c | 108 +++++++++++++++++++++++++++++++++++++++++++++++++++
 lib/igt_dp.h |   9 +++++
 2 files changed, 117 insertions(+)

diff --git a/lib/igt_dp.c b/lib/igt_dp.c
index 9f71c9e72..0b1fe73de 100644
--- a/lib/igt_dp.c
+++ b/lib/igt_dp.c
@@ -6,7 +6,13 @@
  *   Louis Chauvet <louis.chauvet@bootlin.com>
  */
 
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
 #include <stdint.h>
+#include <string.h>
+#include <unistd.h>
 
 #include "drmtest.h"
 #include "i915/i915_dp.h"
@@ -173,3 +179,105 @@ void igt_dp_wait_pending_retrain(int drm_fd, igt_output_t *output)
 	}
 	igt_assert_f(false, "Timeout waiting for pending retrain to complete\n");
 }
+
+/**
+ * igt_dp_aux_open: Open the AUX channel device of a display port
+ * @drm_fd: DRM file descriptor
+ * @output: igt_output_t object representing the display port
+ *
+ * The AUX channel of a connector is exposed as a character device when the
+ * kernel is built with CONFIG_DRM_DISPLAY_DP_AUX_CHARDEV. The device is
+ * parented to the connector, so it is the connector's own sysfs directory
+ * which is scanned here and not /dev, and there is at most one entry in it to
+ * match. /dev is only where the matched entry is then opened from.
+ *
+ * Returns:
+ * A file descriptor for the AUX channel device, or a negative error code on
+ * failure.
+ */
+int igt_dp_aux_open(int drm_fd, igt_output_t *output)
+{
+	struct dirent *entry;
+	int aux_fd = -ENOENT;
+	int dir_fd;
+	DIR *dir;
+
+	dir_fd = igt_connector_sysfs_open(drm_fd, output->config.connector);
+	if (dir_fd < 0)
+		return -ENOENT;
+
+	dir = fdopendir(dir_fd);
+	if (!dir) {
+		/* Save the error before close() gets a chance to overwrite it. */
+		int err = -errno;
+
+		close(dir_fd);
+		return err;
+	}
+
+	errno = 0;
+
+	while ((entry = readdir(dir))) {
+		char path[NAME_MAX + sizeof("/dev/")];
+
+		if (strncmp(entry->d_name, "drm_dp_aux", strlen("drm_dp_aux")))
+			continue;
+
+		snprintf(path, sizeof(path), "/dev/%s", entry->d_name);
+
+		aux_fd = open(path, O_RDONLY);
+		if (aux_fd < 0)
+			aux_fd = -errno;
+
+		break;
+	}
+
+	/* readdir() reports both the end of the directory and an error as NULL. */
+	if (!entry && errno)
+		aux_fd = -errno;
+
+	closedir(dir);
+
+	return aux_fd;
+}
+
+/**
+ * igt_dp_dpcd_read: Read from the DPCD of a display port
+ * @aux_fd:	AUX channel device file descriptor from igt_dp_aux_open()
+ * @offset:	DPCD offset to read from
+ * @buf:	Buffer to read into
+ * @size:	Number of bytes to read
+ *
+ * Returns:
+ * The number of bytes read, or a negative error code on failure.
+ */
+int igt_dp_dpcd_read(int aux_fd, unsigned int offset, void *buf, size_t size)
+{
+	ssize_t ret;
+
+	ret = pread(aux_fd, buf, size, offset);
+	if (ret < 0)
+		return -errno;
+
+	return ret;
+}
+
+/**
+ * igt_dp_dpcd_read_byte: Read a single byte from the DPCD of a display port
+ * @aux_fd:	AUX channel device file descriptor from igt_dp_aux_open()
+ * @offset:	DPCD offset to read from
+ * @val:	Location to store the value read
+ *
+ * Returns:
+ * 0 on success, or a negative error code on failure.
+ */
+int igt_dp_dpcd_read_byte(int aux_fd, unsigned int offset, uint8_t *val)
+{
+	int ret;
+
+	ret = igt_dp_dpcd_read(aux_fd, offset, val, sizeof(*val));
+	if (ret < 0)
+		return ret;
+
+	return ret == 1 ? 0 : -EIO;
+}
diff --git a/lib/igt_dp.h b/lib/igt_dp.h
index 0c56ad2ec..44f3b11a1 100644
--- a/lib/igt_dp.h
+++ b/lib/igt_dp.h
@@ -9,6 +9,9 @@
 #ifndef _IGT_DP_H_
 #define _IGT_DP_H_
 
+#include <stddef.h>
+#include <stdint.h>
+
 #include "igt_kms.h"
 
 int igt_dp_get_current_link_rate(int drm_fd, igt_output_t *output);
@@ -27,4 +30,10 @@ int igt_dp_get_pending_retrain(int drm_fd, igt_output_t *output);
 
 void igt_dp_wait_pending_retrain(int drm_fd, igt_output_t *output);
 
+int igt_dp_aux_open(int drm_fd, igt_output_t *output);
+
+int igt_dp_dpcd_read(int aux_fd, unsigned int offset, void *buf, size_t size);
+
+int igt_dp_dpcd_read_byte(int aux_fd, unsigned int offset, uint8_t *val);
+
 #endif
-- 
2.25.1


  parent reply	other threads:[~2026-09-16  4:36 UTC|newest]

Thread overview: 32+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-16  4:47 [PATCH i-g-t 00/13] Expand kms_dp_link_training coverage Kunal Joshi
2026-09-16  4:47 ` [PATCH i-g-t 01/13] lib/i915/i915_dp: Add helpers for the allowed link configs debugfs Kunal Joshi
2026-09-21  8:37   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 02/13] tests/intel/kms_dp_link_training: Use the UHBR helpers from lib Kunal Joshi
2026-09-21  8:38   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 03/13] tests/intel/kms_dp_link_training: Extract train_link_config() Kunal Joshi
2026-09-22 11:51   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 04/13] tests/intel/kms_dp_link_training: Check the config survived training Kunal Joshi
2026-09-22  5:25   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 05/13] tests/intel/kms_dp_link_training: Drive the smallest mode the sink offers Kunal Joshi
2026-09-22 11:59   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 06/13] tests/intel/kms_dp_link_training: Train every allowed link config Kunal Joshi
2026-09-22 13:51   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 07/13] lib/i915/i915_dp: Add a Type-C port mode query Kunal Joshi
2026-09-22 14:55   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 08/13] tests/intel/kms_dp_link_training: Log the DP link inventory in the fixture Kunal Joshi
2026-09-23 12:46   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 09/13] tests/intel/kms_dp_link_training: Group the outputs into links Kunal Joshi
2026-09-23 13:10   ` S, Sowmiya
2026-09-16  4:47 ` [PATCH i-g-t 10/13] tests/intel/kms_dp_link_training: Add per connector mode subtests Kunal Joshi
2026-09-23 13:29   ` S, Sowmiya
2026-09-16  4:47 ` Kunal Joshi [this message]
2026-09-23 13:34   ` [PATCH i-g-t 11/13] lib/igt_dp: Add DPCD read helpers S, Sowmiya
2026-09-16  4:48 ` [PATCH i-g-t 12/13] lib/igt_dp: Add link status predicates for both channel codings Kunal Joshi
2026-09-23 13:55   ` S, Sowmiya
2026-09-16  4:48 ` [PATCH i-g-t 13/13] tests/intel/kms_dp_link_training: Verify the trained link from the sink side Kunal Joshi
2026-09-23 14:00   ` S, Sowmiya
2026-09-16  5:08 ` ✓ Xe.CI.BAT: success for Expand kms_dp_link_training coverage Patchwork
2026-09-16  5:25 ` ✓ i915.CI.BAT: " Patchwork
2026-09-16  6:15 ` ✗ Xe.CI.FULL: failure " Patchwork
2026-09-16 11:59 ` ✗ i915.CI.Full: " Patchwork
  -- strict thread matches above, loose matches on Subject: below --
2026-10-01  7:36 [PATCH i-g-t 00/13] " Kunal Joshi
2026-10-01  7:37 ` [PATCH i-g-t 11/13] lib/igt_dp: add DPCD read helpers Kunal Joshi

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=20260916044801.1279102-12-kunal1.joshi@intel.com \
    --to=kunal1.joshi@intel.com \
    --cc=igt-dev@lists.freedesktop.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.