Skip to contentSkip to conversation

libdrm: Add master <> render node helpers

RFC PATCH Superseded Emil Velikov 4 participants 16 messages Feb 2, 2015 – Mar 7, 2015

lore
Share & tools
lore
#0 lore raw

cover libdrm: Add master <> render node helpers

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Note: BSD guys - you'll likely need your own version of these functions.

Cheers,
Emil

Cc: Thomas Klausner <[email protected]>
Cc: Konstantin Belousov <[email protected]>
Cc: Robert Millan <[email protected]>
Cc: Mark Kettenis <[email protected]>
Cc: Jonathan Gray <[email protected]>
Cc: François Tigeot <[email protected]>
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#1 lore raw

PATCH 1/2 drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers

Currently most places assume reliable primary <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and safes all the guesswork for the user.

Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 57 insertions(+)
xf86drm.c +54 -0
index 345325a..6af7ac0 100644
@@ -2607,3 +2607,57 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+char *drmGetDeviceNameFromRenderFD(int fd)
+{
+
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}
+
+char *drmGetRenderNameFromDeviceFD(int fd)
+{
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}
xf86drm.h +3 -0
index bfd0670..bca5887 100644
@@ -740,6 +740,9 @@ extern char *drmGetDeviceNameFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetRenderNameFromDeviceFD(int fd);
+extern char *drmGetDeviceNameFromRenderFD(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
Signature · 6 lines
2.2.2

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw

Re: [PATCH 1/2] drm: add drmGet{Device, Render}NameFrom{Render, Device}Fd helpers

Hi Emil,

Quoted 22 lines · Emil Velikov

Currently most places assume reliable primary <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and safes all the guesswork for the user.

Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 57 insertions(+)
xf86drm.c +0 -0
index 345325a..6af7ac0 100644
@@ -2607,3 +2607,57 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2619

Quoted 10 lines
xf86drm.c +10 -0
+char *drmGetDeviceNameFromRenderFD(int fd)
+{
+
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+

fstat returns 0 on success and -1 otherwise so this should be

if (fstat(fd, &sbuf))

    return NULL;
Quoted 17 lines
xf86drm.c +17 -0
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2646

Quoted 10 lines
xf86drm.c +10 -0
+
+char *drmGetRenderNameFromDeviceFD(int fd)
+{
+	struct stat sbuf;
+	char name[64], buf[64];
+	int maj, min, i;
+
+	if (!fstat(fd, &sbuf))
+		return NULL;
+

Same here.

Quoted 22 lines
xf86drm.c +17 -0
+	if (!S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, buf, i);
+		if (stat(name, &sbuf) == 0) {
+			snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+			return strdup(name);
+		}
+	}
+	return NULL;
+}
xf86drm.h +0 -0
index bfd0670..bca5887 100644

xf86drm.h extern char *drmGetDeviceNameFromFd(int fd); · line 748

Quoted 9 lines
xf86drm.h +3 -0
@@ -740,6 +740,9 @@ extern char *drmGetDeviceNameFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetRenderNameFromDeviceFD(int fd);
+extern char *drmGetDeviceNameFromRenderFD(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#3 lore raw

PATCH v2 1/1 drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions

In reply to Emil Velikov

Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD.

v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.

Cc Frank Binns <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 85 insertions(+)
xf86drm.c +82 -0
index e117bc6..d4a4dc6 100644
@@ -40,6 +40,8 @@
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
     }
 }
 
+static const char *drmGetMinorName(int type)
+{
+    switch (type) {
+    case DRM_NODE_PRIMARY:
+        return "card";
+    case DRM_NODE_CONTROL:
+        return "controlD";
+    case DRM_NODE_RENDER:
+        return "renderD";
+    default:
+        return NULL;
+    }
+}
+
 /**
  * Open the device by bus ID.
  *
@@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+	DIR *sysdir;
+	struct dirent *pent, *ent;
+	struct stat sbuf;
+	const char *name = drmGetMinorName(type);
+	const int len = strlen(name);
+	char dev_name[64], buf[64];
+	long name_max;
+	int maj, min;
+
+	if (!name)
+		return NULL;
+
+	if (fstat(fd, &sbuf))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	sysdir = opendir(buf);
+	if (!sysdir)
+		return NULL;
+
+	name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+	if (name_max == -1)
+		goto out_close_dir;
+
+	pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+	if (pent == NULL)
+		 goto out_close_dir;
+
+	while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+		if (strncmp(ent->d_name, name, len) == 0) {
+			free(pent);
+			closedir(sysdir);
+
+			snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+				 ent->d_name);
+			return strdup(dev_name);
+		}
+	}
+
+	free(pent);
+
+out_close_dir:
+	closedir(sysdir);
+#endif
+	return NULL;
+}
+
+char *drmGetMasterNameFromRenderFD(int fd)
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderNameFromMasterFD(int fd)
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
xf86drm.h +3 -0
index afd38a1..5fdf27b 100644
@@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetRenderNameFromMasterFD(int fd);
+extern char *drmGetMasterNameFromRenderFD(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
Signature · 6 lines
2.3.0

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
Reviewed-by on v2 1/1
#4 lore raw

Re: [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions

Hi Emil,

Quoted 57 lines · Emil Velikov

Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD.

v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.

Cc Frank Binns <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 85 insertions(+)
xf86drm.c +16 -0
index e117bc6..d4a4dc6 100644
@@ -40,6 +40,8 @@
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
     }
 }
 
+static const char *drmGetMinorName(int type)
+{
+    switch (type) {
+    case DRM_NODE_PRIMARY:
+        return "card";
+    case DRM_NODE_CONTROL:
+        return "controlD";
+    case DRM_NODE_RENDER:
+        return "renderD";
+    default:
+        return NULL;
+    }
+}
+
 /**
  * Open the device by bus ID.
  *
@@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2762

Quoted 10 lines
xf86drm.c +8 -0
 }
 
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+	DIR *sysdir;
+	struct dirent *pent, *ent;
+	struct stat sbuf;
+	const char *name = drmGetMinorName(type);
+	const int len = strlen(name);

This will cause a segfault if 'name' is NULL.

Quoted 40 lines
xf86drm.c +40 -0
+	char dev_name[64], buf[64];
+	long name_max;
+	int maj, min;
+
+	if (!name)
+		return NULL;
+
+	if (fstat(fd, &sbuf))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	sysdir = opendir(buf);
+	if (!sysdir)
+		return NULL;
+
+	name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+	if (name_max == -1)
+		goto out_close_dir;
+
+	pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+	if (pent == NULL)
+		 goto out_close_dir;
+
+	while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+		if (strncmp(ent->d_name, name, len) == 0) {
+			free(pent);
+			closedir(sysdir);
+
+			snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+				 ent->d_name);
+			return strdup(dev_name);
+		}
+	}

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2812

Quoted 10 lines
xf86drm.c +10 -0
+
+	free(pent);
+
+out_close_dir:
+	closedir(sysdir);
+#endif
+	return NULL;
+}
+
+char *drmGetMasterNameFromRenderFD(int fd)

I think drmGetPrimaryDeviceNameFromFd would be more appropriate given the node type is 'primary', the type of the fd doesn't matter afaics and for consistency with other drmGet* functions. However, given that's a bit of a mouthful I guess the 'Device' part could be dropped.

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2817

Quoted 5 lines
xf86drm.c +5 -0
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderNameFromMasterFD(int fd)

As above, maybe drmGetRenderDeviceNameFromFd?

With those things changed/fixed you can have a:

Thanks
Frank

Quoted 8 lines
xf86drm.c +3 -0
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
xf86drm.h +0 -0
index afd38a1..5fdf27b 100644

xf86drm.h extern int drmGetNodeTypeFromFd(int fd); · line 757

Quoted 9 lines
xf86drm.h +3 -0
@@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetRenderNameFromMasterFD(int fd);
+extern char *drmGetMasterNameFromRenderFD(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#5 lore raw

Re: [PATCH libdrm v2] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions

Quoted 116 lines · Frank Binns

Hi Emil,

Quoted 57 lines · Emil Velikov

Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD.

v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.

Cc Frank Binns <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 82 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 85 insertions(+)
xf86drm.c +16 -0
index e117bc6..d4a4dc6 100644
@@ -40,6 +40,8 @@
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
     }
 }
 
+static const char *drmGetMinorName(int type)
+{
+    switch (type) {
+    case DRM_NODE_PRIMARY:
+        return "card";
+    case DRM_NODE_CONTROL:
+        return "controlD";
+    case DRM_NODE_RENDER:
+        return "renderD";
+    default:
+        return NULL;
+    }
+}
+
 /**
  * Open the device by bus ID.
  *
@@ -2736,3 +2752,69 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
      return 0;

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2762

Quoted 10 lines
xf86drm.c +8 -0
 }
 
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+     DIR *sysdir;
+     struct dirent *pent, *ent;
+     struct stat sbuf;
+     const char *name = drmGetMinorName(type);
+     const int len = strlen(name);

This will cause a segfault if 'name' is NULL.

Quoted 44 lines
xf86drm.c +44 -0
+     char dev_name[64], buf[64];
+     long name_max;
+     int maj, min;
+
+     if (!name)
+             return NULL;
+
+     if (fstat(fd, &sbuf))
+             return NULL;
+
+     maj = major(sbuf.st_rdev);
+     min = minor(sbuf.st_rdev);
+
+     if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+             return NULL;
+
+     snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+     sysdir = opendir(buf);
+     if (!sysdir)
+             return NULL;
+
+     name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+     if (name_max == -1)
+             goto out_close_dir;
+
+     pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+     if (pent == NULL)
+              goto out_close_dir;
+
+     while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+             if (strncmp(ent->d_name, name, len) == 0) {
+                     free(pent);
+                     closedir(sysdir);
+
+                     snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+                              ent->d_name);
+                     return strdup(dev_name);
+             }
+     }
+
+     free(pent);
+
+out_close_dir:
Quoted 8 lines

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2812

Quoted 6 lines
xf86drm.c +6 -0
+     closedir(sysdir);
+#endif
+     return NULL;
+}
+
+char *drmGetMasterNameFromRenderFD(int fd)

I think drmGetPrimaryDeviceNameFromFd would be more appropriate given the node type is 'primary',

Most places that I've seen call "/dev/dri/cardX" master node, although with the introduction of DRM_NODE_PRIMARY I believe your suggestion may be better.

Quoted 4 lines

the type of the fd doesn't matter afaics and
for consistency with other drmGet* functions. However, given that's a bit of a mouthful I guess the 'Device' part could be dropped.

I was thinking about this initially then decided to keep the Render/MasterFD explicit. Mainly because I'm don't know how cumbersome the *BSD implementation will be.

But with that said it none of the BSD guys objects within 1-2 weeks I'll go with your suggestion.

Thanks
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw

PATCH v3 1/1 drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions

In reply to Emil Velikov

Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD.

v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.

v3:
- Don't segfault if name is NULL.
- Update function names, as suggested by Frank Binns.

Cc Frank Binns <[email protected]>
Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>

---

Hi all,

I'm planning to push this Monday afternoon. If you have any comments or suggestions please go ahead.

Cheers,
Emil

 xf86drm.c | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h |  3 +++
 2 files changed, 87 insertions(+)
xf86drm.c +84 -0
index e117bc6..194cd35 100644
@@ -40,6 +40,8 @@
 #include <string.h>
 #include <strings.h>
 #include <ctype.h>
+#include <dirent.h>
+#include <stddef.h>
 #include <fcntl.h>
 #include <errno.h>
 #include <signal.h>
@@ -522,6 +524,20 @@ static int drmGetMinorType(int minor)
     }
 }
 
+static const char *drmGetMinorName(int type)
+{
+    switch (type) {
+    case DRM_NODE_PRIMARY:
+        return "card";
+    case DRM_NODE_CONTROL:
+        return "controlD";
+    case DRM_NODE_RENDER:
+        return "renderD";
+    default:
+        return NULL;
+    }
+}
+
 /**
  * Open the device by bus ID.
  *
@@ -2736,3 +2752,71 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+static char *drmGetMinorNameForFD(int fd, int type)
+{
+#ifdef __linux__
+	DIR *sysdir;
+	struct dirent *pent, *ent;
+	struct stat sbuf;
+	const char *name = drmGetMinorName(type);
+	int len;
+	char dev_name[64], buf[64];
+	long name_max;
+	int maj, min;
+
+	if (!name)
+		return NULL;
+
+	len = strlen(name);
+
+	if (fstat(fd, &sbuf))
+		return NULL;
+
+	maj = major(sbuf.st_rdev);
+	min = minor(sbuf.st_rdev);
+
+	if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
+		return NULL;
+
+	snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
+
+	sysdir = opendir(buf);
+	if (!sysdir)
+		return NULL;
+
+	name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
+	if (name_max == -1)
+		goto out_close_dir;
+
+	pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
+	if (pent == NULL)
+		 goto out_close_dir;
+
+	while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
+		if (strncmp(ent->d_name, name, len) == 0) {
+			free(pent);
+			closedir(sysdir);
+
+			snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
+				 ent->d_name);
+			return strdup(dev_name);
+		}
+	}
+
+	free(pent);
+
+out_close_dir:
+	closedir(sysdir);
+#endif
+	return NULL;
+}
+
+char *drmGetPrimaryDeviceNameFromFd(int fd)
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
+}
+
+char *drmGetRenderDeviceNameFromFd(int fd)
+{
+	return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
+}
xf86drm.h +3 -0
index afd38a1..40c55c9 100644
@@ -749,6 +749,9 @@ extern int drmGetNodeTypeFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+extern char *drmGetPrimaryDeviceNameFromFd(int fd);
+extern char *drmGetRenderDeviceNameFromFd(int fd);
+
 #if defined(__cplusplus) || defined(c_plusplus)
 }
 #endif
Signature · 6 lines
2.3.1

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#7 lore raw

Re: [PATCH libdrm v3] drm: add drmGet(Master|Render)NameFrom(Render|Master)FD functions

Quoted 12 lines · Emil Velikov

Currently most places assume reliable master <> render node mapping. Although this may work in some cases, it is not correct.

Add a couple of helpers that hide the details and provide the name of the master/render device name, given an render/master FD.

v2:
- Rename Device and Primary to Master (aka the /dev/dri/cardX device).
- Check for the file via readdir_r() rather than stat().
- Wrap the check into a single function.
- Return NULL for non-linux platforms.

Quoted 3 lines

v3:
- Don't segfault if name is NULL.
- Update function names, as suggested by Frank Binns.

And seemingly I've forgot to update the patch summary to reflect the change. I'll do so before pushing

-Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw

PATCH 2/2 libdrm: add drmGetNodeType() helper

In reply to Emil Velikov

The add a simple helper which returns the node type of the opened fd. Likely to be used in conjunction with the previous two helpers.

Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 43 +++++++++++++++++++++++++++++++++++++++----
 xf86drm.h |  7 +++++++
 2 files changed, 46 insertions(+), 4 deletions(-)
xf86drm.c +39 -4
index 6af7ac0..a70c0dd 100644
@@ -85,10 +85,6 @@
 
 #define DRM_MSG_VERBOSITY 3
 
-#define DRM_NODE_CONTROL 0
-#define DRM_NODE_PRIMARY 1
-#define DRM_NODE_RENDER 2
-
 static drmServerInfoPtr drm_server_info;
 
 void drmSetServerInfo(drmServerInfoPtr info)
@@ -2607,6 +2603,45 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+int drmGetNodeType(int fd, drmNodeType *type)
+{
+	struct stat sbuf;
+	char name[64];
+	dev_t d;
+	int i;
+
+	if (!fstat(fd, &sbuf))
+		return -errno;
+
+	d = sbuf.st_rdev;
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_PRIMARY;
+			return 0;
+		}
+	}
+
+	for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_CONTROL;
+			return 0;
+		}
+	}
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_RENDER;
+			return 0;
+		}
+	}
+
+	return -EINVAL;
+}
+
 char *drmGetDeviceNameFromRenderFD(int fd)
 {
 
xf86drm.h +7 -0
index bca5887..7d67df9 100644
@@ -740,6 +740,13 @@ extern char *drmGetDeviceNameFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 
+typedef enum _drmNodeType {
+    DRM_NODE_CONTROL = 0,
+    DRM_NODE_PRIMARY = 1,
+    DRM_NODE_RENDER  = 2
+} drmNodeType, *drmNodeTypePtr;
+
+extern int drmGetNodeType(int fd, drmNodeType *type);
 extern char *drmGetRenderNameFromDeviceFD(int fd);
 extern char *drmGetDeviceNameFromRenderFD(int fd);
 
Signature · 6 lines
2.2.2

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#9 lore raw

Re: [PATCH 2/2] libdrm: add drmGetNodeType() helper

Hi Emil,

Quoted 55 lines · Emil Velikov

The add a simple helper which returns the node type of the opened fd. Likely to be used in conjunction with the previous two helpers.

Cc Daniel Vetter <[email protected]>
Cc David Herrmann <[email protected]>
---
 xf86drm.c | 43 +++++++++++++++++++++++++++++++++++++++----
 xf86drm.h |  7 +++++++
 2 files changed, 46 insertions(+), 4 deletions(-)
xf86drm.c +25 -4
index 6af7ac0..a70c0dd 100644
@@ -85,10 +85,6 @@
 
 #define DRM_MSG_VERBOSITY 3
 
-#define DRM_NODE_CONTROL 0
-#define DRM_NODE_PRIMARY 1
-#define DRM_NODE_RENDER 2
-
 static drmServerInfoPtr drm_server_info;
 
 void drmSetServerInfo(drmServerInfoPtr info)
@@ -2607,6 +2603,45 @@ int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
 	return 0;
 }
 
+int drmGetNodeType(int fd, drmNodeType *type)
+{
+	struct stat sbuf;
+	char name[64];
+	dev_t d;
+	int i;
+
+	if (!fstat(fd, &sbuf))
+		return -errno;
+
+	d = sbuf.st_rdev;
+
+	for (i = 0; i < DRM_MAX_MINOR; i++) {
+		snprintf(name, sizeof(name), DRM_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_PRIMARY;
+			return 0;
+		}
+	}
+
+	for (i = 64; i < (64 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_CONTROL;
+			return 0;

xf86drm.c int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle) · line 2640

Quoted 10 lines
xf86drm.c +10 -0
+		}
+	}
+
+	for (i = 128; i < (128 + DRM_MAX_MINOR); i++) {
+		snprintf(name, sizeof(name), DRM_RENDER_DEV_NAME, DRM_DIR_NAME, i);
+		if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d) {
+			*type = DRM_NODE_RENDER;
+			return 0;
+		}
+	}

It would seem easier (and faster) to use minor(sbuf.st_rdev) to determine the type of node.

I'm not sure what the etiquette is here but I've prepared a patch that does it this way instead, which will follow this email. Please feel free to add your own signed-off-by, suggested-by, etc as I don't want to steal all your credit :)

Thanks
Frank

Quoted 15 lines
xf86drm.c +4 -0
+
+	return -EINVAL;
+}
+
 char *drmGetDeviceNameFromRenderFD(int fd)
 {
 
xf86drm.h +0 -0
index bca5887..7d67df9 100644
@@ -740,6 +740,13 @@ extern char *drmGetDeviceNameFromFd(int fd);
 extern int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd);
 extern int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle);
 

xf86drm.h extern char *drmGetDeviceNameFromFd(int fd); · line 752

Quoted 10 lines
xf86drm.h +7 -0
+typedef enum _drmNodeType {
+    DRM_NODE_CONTROL = 0,
+    DRM_NODE_PRIMARY = 1,
+    DRM_NODE_RENDER  = 2
+} drmNodeType, *drmNodeTypePtr;
+
+extern int drmGetNodeType(int fd, drmNodeType *type);
 extern char *drmGetRenderNameFromDeviceFD(int fd);
 extern char *drmGetDeviceNameFromRenderFD(int fd);
 

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw
In reply to Emil Velikov
Quoted 15 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

Quoted 9 lines

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Note: BSD guys - you'll likely need your own version of these functions.

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

*BSD guys,

Please take a look and/or forward this to the relevant people. I fear that my *BSD knowledge is virtually none :\

Cheers,
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#11 lore raw
Quoted 30 lines · Emil Velikov
Quoted 15 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

Quoted 9 lines

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Note: BSD guys - you'll likely need your own version of these functions.

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

Quoted 7 lines

*BSD guys,

Please take a look and/or forward this to the relevant people. I fear that my *BSD knowledge is virtually none :\

Cheers,
Emil

I can't speak for other projects but OpenBSD drm is currently based on Linux 3.8 with no render nodes or prime.

There is no sysfs/udev etc so when/if render nodes work it will likely involve either a fixed mapping or ioctls.
_______________________________________________
dri-devel mailing list [email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw
In reply to Emil Velikov

Hi

Quoted 23 lines · Emil Velikov
Quoted 22 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Quoted 6 lines
Quoted 2 lines

Note: BSD guys - you'll likely need your own version of these functions.

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

The patches look ok. I really dislike the direct file-probing, though. I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not the one to ask about coding-style for libdrm, so fine with me.

Thanks
David
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#13 lore raw
Quoted 28 lines · David Herrmann

Hi

Quoted 25 lines · Emil Velikov
Quoted 24 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Note: BSD guys - you'll likely need your own version of these functions.

Quoted 9 lines
Quoted 4 lines

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

The patches look ok. I really dislike the direct file-probing, though. I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not the one to ask about coding-style for libdrm, so fine with me.

Thanks for having a look. libudev is no used in libdrm so I will stay away for now. I would prefer to use readdir on /dev/dri/ but AFAICT there is no way to get the mapping from those alone. Am I missing something ?

I'm also thinking that changing the prototypes, as below, might be useful - i.e. return 0 on success, -ENODEV when the corresponding device lacks render/primary device, and other negative value on error ?

char *drmGetRenderNameFromDeviceFD(int fd);
char *drmGetDeviceNameFromRenderFD(int fd);

int drmGetRenderNameFromDeviceFD(int fd, char **render); int drmGetDeviceNameFromRenderFD(int fd, char **device);

Cheers,
Emil

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#14 lore raw

Hi Emil,

Quoted 33 lines · Emil Velikov
Quoted 32 lines · David Herrmann

Hi

Quoted 29 lines · Emil Velikov
Quoted 15 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

Quoted 9 lines

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Note: BSD guys - you'll likely need your own version of these functions.

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

Quoted 8 lines
Quoted 4 lines

The patches look ok. I really dislike the direct file-probing, though. I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not the one to ask about coding-style for libdrm, so fine with me.

Thanks for having a look. libudev is no used in libdrm so I will stay away for now. I would prefer to use readdir on /dev/dri/ but AFAICT there is no way to get the mapping from those alone. Am I missing something ?

Can you you not just use use readdir on /dev/dri and stat on each entry's d_name comparing the result to an fstat on the fd? Once you find a match you just
strdup the d_name.

Quoted 7 lines

I'm also thinking that changing the prototypes, as below, might be useful - i.e. return 0 on success, -ENODEV when the corresponding device lacks render/primary device, and other negative value on error ?

char *drmGetRenderNameFromDeviceFD(int fd);
char *drmGetDeviceNameFromRenderFD(int fd);

I personally prefer this as it fits in better with what we already have.

Quoted 5 lines

int drmGetRenderNameFromDeviceFD(int fd, char **render); int drmGetDeviceNameFromRenderFD(int fd, char **device);

Quoted 7 lines

Cheers,
Emil

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#15 lore raw
Quoted 40 lines · Frank Binns

Hi Emil,

Quoted 37 lines · Emil Velikov
Quoted 36 lines · David Herrmann

Hi

Quoted 23 lines · Emil Velikov
Quoted 22 lines · Emil Velikov

Hi all,

As mentioned a couple of days ago at #dri-devel some(most) users of render nodes tend to rely on strict mapping between the primary and render node. I.e. something along the lines of

  fstat(render_fd, &sbuf);
  sprintf(primary_node, "/dev/dri/card%d",
                        ((sbuf.st_rdev & 0x3f) | 0x80));

Currently the following are (ab)using the above code:
- xf86-video-nouveau
- xf86-video-intel
- libva (vaapi)

As reminded by David Herrman, this is not the correct solution - thus I've added a couple of helpers which walk through sysfs of the respecitive device and return the correct device name.

I'm not 100% happy with the function names, so suggestions are greatly appreciated. Any other comments are also welcome :)

Quoted 6 lines
Quoted 2 lines

Note: BSD guys - you'll likely need your own version of these functions.

David, Daniel

Can you please take a look at these two patches. Would be great if we can minimize the above assumptions before they get too wide spread.

The patches look ok. I really dislike the direct file-probing, though. I'd recommend using libudev or readdir_r() on /dev/dri/, but I'm not the one to ask about coding-style for libdrm, so fine with me.

Quoted 6 lines
Quoted 4 lines

Thanks for having a look. libudev is no used in libdrm so I will stay away for now. I would prefer to use readdir on /dev/dri/ but AFAICT there is no way to get the mapping from those alone. Am I missing something ?

Can you you not just use use readdir on /dev/dri and stat on each entry's d_name comparing the result to an fstat on the fd?

Unless I've misunderstood something, a primary device will have 226:{0-63}, while render device 226:{128-191}.

One cannot just mask out the 7th bit, as this is the (wrong) solution what I've mentioned above. Or maybe I'm missing something ? Afaict the information/"link" between the two is present at the drm module level - thus the ugly sysfs dance.

Quoted 11 lines

Once you find a match you just strdup the d_name.

Quoted 7 lines

I'm also thinking that changing the prototypes, as below, might be useful - i.e. return 0 on success, -ENODEV when the corresponding device lacks render/primary device, and other negative value on error ?

char *drmGetRenderNameFromDeviceFD(int fd);
char *drmGetDeviceNameFromRenderFD(int fd);

I personally prefer this as it fits in better with what we already have.

The recent inspiration came from the prime helpers. I don't feel strongly either way.

Cheers,
Emil
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel