Intel-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Ben Widawsky <ben@bwidawsk.net>
To: intel-gfx@lists.freedesktop.org
Cc: Ben Widawsky <ben@bwidawsk.net>
Subject: [PATCH 2/3] drm/i915: extract card getting
Date: Sun, 25 Mar 2012 17:33:31 -0700	[thread overview]
Message-ID: <1332722012-4853-5-git-send-email-ben@bwidawsk.net> (raw)
In-Reply-To: <1332722012-4853-1-git-send-email-ben@bwidawsk.net>

I didn't test this very thoroughly...

Signed-off-by: Ben Widawsky <ben@bwidawsk.net>
---
 lib/drmtest.c |  122 +++++++++++++++++++++++++++++++++++++++------------------
 1 file changed, 83 insertions(+), 39 deletions(-)

diff --git a/lib/drmtest.c b/lib/drmtest.c
index f9b7a6f..063b5c5 100644
--- a/lib/drmtest.c
+++ b/lib/drmtest.c
@@ -26,6 +26,8 @@
  *
  */
 
+#define _GNU_SOURCE
+#include <stdio.h>
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/ioctl.h>
@@ -112,70 +114,112 @@ void gem_quiescent_gpu(int fd)
 	gem_sync(fd, handle);
 }
 
-/** Open the first DRM device we can find, searching up to 16 device nodes */
-int drm_open_any(void)
+static bool is_master(int fd)
 {
-	char name[20];
+	drm_client_t client;
+	int ret;
+
+	/* Check that we're the only opener and authed. */
+	client.idx = 0;
+	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+	assert (ret == 0);
+	if (!client.auth) {
+		return 0;
+	}
+	client.idx = 1;
+	ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
+	if (ret != -1 || errno != EINVAL) {
+		return 0;
+	}
+	return 1;
+}
+
+/**
+ * drm_get_card() - get an intel card number for use in /dev or /sys
+ *
+ * @master: -1 not a master, 0 don't care, 1 is the master
+ *
+ * returns -1 on error
+ */
+int drm_get_card(int master)
+{
+	char *name;
 	int i, fd;
 
 	for (i = 0; i < 16; i++) {
-		sprintf(name, "/dev/dri/card%d", i);
+		int ret;
+
+		ret = asprintf(&name, "/dev/dri/card%u", i);
+		if (ret == -1)
+			return -1;
 		fd = open(name, O_RDWR);
+		free(name);
+
 		if (fd == -1)
 			continue;
 
-		if (is_intel(fd)) {
+		if (is_intel(fd) && master == 0) {
 			gem_quiescent_gpu(fd);
-			return fd;
+			break;
+		}
+
+		if (master == 1 && is_master(fd)) {
+			close(fd);
+			break;
+		}
+
+		if (master == -1 && !is_master(fd)) {
+			close(fd);
+			break;
 		}
 
 		close(fd);
 	}
-	fprintf(stderr, "failed to open any drm device. retry as root?\n");
-	abort();
+
+	return i;
 }
 
+/** Open the first DRM device we can find, searching up to 16 device nodes */
+int drm_open_any(void)
+{
+	char *name;
+	int ret, fd;
+
+	ret = asprintf(&name, "/dev/dri/card%d", drm_get_card(0));
+	if (ret == -1)
+		return -1;
+
+	fd = open(name, O_RDWR);
+	free(name);
+
+	if (fd == -1)
+		fprintf(stderr, "failed to open any drm device. retry as root?\n");
+
+	assert(is_intel(fd));
+
+	return fd;
+}
 
 /**
  * Open the first DRM device we can find where we end up being the master.
  */
 int drm_open_any_master(void)
 {
-	char name[20];
-	int i, fd;
+	char *name;
+	int ret, fd;
 
-	for (i = 0; i < 16; i++) {
-		drm_client_t client;
-		int ret;
+	ret = asprintf(&name, "/dev/dri/card%d", drm_get_card(1));
+	if (ret == -1)
+		return -1;
 
-		sprintf(name, "/dev/dri/card%d", i);
-		fd = open(name, O_RDWR);
-		if (fd == -1)
-			continue;
+	fd = open(name, O_RDWR);
+	free(name);
+	if (fd == -1)
+		fprintf(stderr, "Couldn't find an un-controlled DRM device\n");
 
-		if (!is_intel(fd)) {
-			close(fd);
-			continue;
-		}
+	assert(is_intel(fd));
 
-		/* Check that we're the only opener and authed. */
-		client.idx = 0;
-		ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
-		assert (ret == 0);
-		if (!client.auth) {
-			close(fd);
-			continue;
-		}
-		client.idx = 1;
-		ret = ioctl(fd, DRM_IOCTL_GET_CLIENT, &client);
-		if (ret != -1 || errno != EINVAL) {
-			close(fd);
-			continue;
-		}
-		return fd;
-	}
-	fprintf(stderr, "Couldn't find an un-controlled DRM device\n");
-	abort();
+	return fd;
 }
 
 void gem_set_tiling(int fd, uint32_t handle, int tiling, int stride)
-- 
1.7.9.4

  parent reply	other threads:[~2012-03-26  0:35 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2012-03-26  0:33 [PATCH v2 1/3] drm/i915: add rc6 residency times to debugfs Ben Widawsky
2012-03-26  0:33 ` [PATCH 2/3] drm/i915: extract intel_enable_rc6() Ben Widawsky
2012-03-26  9:32   ` Daniel Vetter
2012-03-26  0:33 ` [PATCH v2 3/3] drm/i915: rc6 in sysfs Ben Widawsky
2012-03-26  0:33 ` [PATCH 1/3] build: make sure we have asprintf Ben Widawsky
2012-03-26  0:33 ` Ben Widawsky [this message]
2012-03-26  9:33   ` [PATCH 2/3] drm/i915: extract card getting Daniel Vetter
2012-03-26  0:33 ` [PATCH v2 3/3] tests: rc6 residency test Ben Widawsky
2012-03-26  9:37   ` Daniel Vetter
2012-03-26  9:40     ` Daniel Vetter
2012-03-26 21:54       ` Ben Widawsky
2012-03-27  7:22         ` Daniel Vetter
2012-03-27  8:34   ` Daniel Vetter
2012-03-26  9:34 ` [PATCH v2 1/3] drm/i915: add rc6 residency times to debugfs Daniel Vetter
2012-03-26 12:33   ` Eugeni Dodonov
2012-03-26 16:49     ` Ben Widawsky
2012-03-26 22:34     ` Ben Widawsky
2012-03-26 23:04       ` Eugeni Dodonov
  -- strict thread matches above, loose matches on Subject: below --
2012-03-28  1:59 [PATCH 1/2] " Ben Widawsky
2012-03-28  1:59 ` [PATCH 2/3] drm/i915: extract card getting Ben Widawsky

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=1332722012-4853-5-git-send-email-ben@bwidawsk.net \
    --to=ben@bwidawsk.net \
    --cc=intel-gfx@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox