dri-devel Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] xf86drm: add drmOpenByFB
@ 2020-05-23 15:44 Mauro Rossi
  2020-05-23 21:36 ` Simon Ser
  2020-05-24 18:53 ` Daniel Vetter
  0 siblings, 2 replies; 4+ messages in thread
From: Mauro Rossi @ 2020-05-23 15:44 UTC (permalink / raw)
  To: dri-devel; +Cc: robert.foss, Mauro Rossi, emil.l.velikov, cwhuang

OpenByFB is introduced to overcome GPU driver loading order issue
on a device with multiple GPUs, e.g. Intel iGPU and Nvidia dGPU
where the first drmfb kernel module loaded will become device file
/dev/dri/card0 and the second will become /dev/dri/card1

The use case is to prefer Intel iGPU over dGPU, or viceversa,
in a deterministic and reliable manner.

OpenByFB function opens the DRM device with specified fb and node type,
thus enabling gralloc to open the correct device node of the primary fb,
regardless of the GPU driver loading order.

Signed-off-by: Chih-Wei Huang <cwhuang@linux.org.tw>
---
 core-symbols.txt |  1 +
 xf86drm.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
 xf86drm.h        |  1 +
 3 files changed, 44 insertions(+)

diff --git a/core-symbols.txt b/core-symbols.txt
index 1ff4ecaa..6bf8c70d 100644
--- a/core-symbols.txt
+++ b/core-symbols.txt
@@ -146,6 +146,7 @@ drmModeSetCursor2
 drmModeSetPlane
 drmMsg
 drmOpen
+drmOpenByFB
 drmOpenControl
 drmOpenOnce
 drmOpenOnceWithType
diff --git a/xf86drm.c b/xf86drm.c
index b49d42f7..229a54bf 100644
--- a/xf86drm.c
+++ b/xf86drm.c
@@ -793,6 +793,48 @@ drm_public int drmOpenRender(int minor)
     return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
 }
 
+/**
+ * Open the DRM device with specified type of specified framebuffer.
+ *
+ * Looks up the associated DRM device with specified type of the
+ * specified framebuffer and opens it.
+ *
+ * \param fb the index of framebuffer.
+ * \param type the device node type to open, PRIMARY, CONTROL or RENDER
+ *
+ * \return a file descriptor on success, or a negative value on error.
+ *
+ */
+drm_public int drmOpenByFB(int fb, int type)
+{
+#ifdef __linux__
+    DIR *sysdir;
+    struct dirent *ent;
+    char buf[64];
+    const char *name = drmGetMinorName(type);
+    int fd = -1, len = strlen(name);
+
+    snprintf(buf, sizeof(buf), "/sys/class/graphics/fb%d/device/drm", fb);
+    sysdir = opendir(buf);
+    if (!sysdir)
+        return -errno;
+
+    while ((ent = readdir(sysdir))) {
+        if (!strncmp(ent->d_name, name, len)) {
+            snprintf(buf, sizeof(buf), "%s/%s", DRM_DIR_NAME, ent->d_name);
+            fd = open(buf, O_RDWR | O_CLOEXEC, 0);
+            break;
+        }
+    }
+
+    closedir(sysdir);
+    return fd;
+#else
+#warning "Missing implementation of drmOpenByFB"
+    return -EINVAL;
+#endif
+}
+
 /**
  * Free the version information returned by drmGetVersion().
  *
diff --git a/xf86drm.h b/xf86drm.h
index 7b85079a..d45d696f 100644
--- a/xf86drm.h
+++ b/xf86drm.h
@@ -605,6 +605,7 @@ extern int           drmOpenWithType(const char *name, const char *busid,
 
 extern int           drmOpenControl(int minor);
 extern int           drmOpenRender(int minor);
+extern int           drmOpenByFB(int fb, int type);
 extern int           drmClose(int fd);
 extern drmVersionPtr drmGetVersion(int fd);
 extern drmVersionPtr drmGetLibVersion(int fd);
-- 
2.25.1

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xf86drm: add drmOpenByFB
  2020-05-23 15:44 [PATCH] xf86drm: add drmOpenByFB Mauro Rossi
@ 2020-05-23 21:36 ` Simon Ser
  2020-05-24 18:53 ` Daniel Vetter
  1 sibling, 0 replies; 4+ messages in thread
From: Simon Ser @ 2020-05-23 21:36 UTC (permalink / raw)
  To: Mauro Rossi; +Cc: robert.foss, cwhuang, emil.l.velikov, dri-devel

On Saturday, May 23, 2020 5:44 PM, Mauro Rossi <issor.oruam@gmail.com> wrote:

> OpenByFB is introduced to overcome GPU driver loading order issue
> on a device with multiple GPUs, e.g. Intel iGPU and Nvidia dGPU
> where the first drmfb kernel module loaded will become device file
> /dev/dri/card0 and the second will become /dev/dri/card1
>
> The use case is to prefer Intel iGPU over dGPU, or viceversa,
> in a deterministic and reliable manner.
>
> OpenByFB function opens the DRM device with specified fb and node type,
> thus enabling gralloc to open the correct device node of the primary fb,
> regardless of the GPU driver loading order.

"FB" is ambiguous here, because the other FB functions in this file
refer to DRM frame-buffer objects (as opposed to fbdev I believe?).
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xf86drm: add drmOpenByFB
  2020-05-23 15:44 [PATCH] xf86drm: add drmOpenByFB Mauro Rossi
  2020-05-23 21:36 ` Simon Ser
@ 2020-05-24 18:53 ` Daniel Vetter
  2020-05-24 19:25   ` Simon Ser
  1 sibling, 1 reply; 4+ messages in thread
From: Daniel Vetter @ 2020-05-24 18:53 UTC (permalink / raw)
  To: Mauro Rossi; +Cc: Robert Foss, cwhuang, Emil Velikov, dri-devel

On Sat, May 23, 2020 at 5:44 PM Mauro Rossi <issor.oruam@gmail.com> wrote:
>
> OpenByFB is introduced to overcome GPU driver loading order issue
> on a device with multiple GPUs, e.g. Intel iGPU and Nvidia dGPU
> where the first drmfb kernel module loaded will become device file
> /dev/dri/card0 and the second will become /dev/dri/card1
>
> The use case is to prefer Intel iGPU over dGPU, or viceversa,
> in a deterministic and reliable manner.
>
> OpenByFB function opens the DRM device with specified fb and node type,
> thus enabling gralloc to open the correct device node of the primary fb,
> regardless of the GPU driver loading order.

The fbdev load ordering is as inconsistent/random as the dri node
loading. Well more so, because you might have random firmware fbdev
drivers hanging out there. Hence I'm not following how this solves
anything for your problem.

I think usually what userspace does it look at the boot_vga property
of the underlying device in sysfs, and prefer that one. Iirc (but
might be wrong) that's patched to essentially mean "primary display
device, the same the firmware used for boot-up screens". Either way I
think the proper way to solve this is you figure out matching rules
you care about using udev/sysfs, and then use that information to open
the right drm node. Opening by fbdev seems a most convoluted way to
get to a drm device (we're trying to deprecate fbdev outright as much
as possible), and you could even match for any specific device with
sysfs already if that's what you're doing using udev rules.
-Daniel


>
> Signed-off-by: Chih-Wei Huang <cwhuang@linux.org.tw>
> ---
>  core-symbols.txt |  1 +
>  xf86drm.c        | 42 ++++++++++++++++++++++++++++++++++++++++++
>  xf86drm.h        |  1 +
>  3 files changed, 44 insertions(+)
>
> diff --git a/core-symbols.txt b/core-symbols.txt
> index 1ff4ecaa..6bf8c70d 100644
> --- a/core-symbols.txt
> +++ b/core-symbols.txt
> @@ -146,6 +146,7 @@ drmModeSetCursor2
>  drmModeSetPlane
>  drmMsg
>  drmOpen
> +drmOpenByFB
>  drmOpenControl
>  drmOpenOnce
>  drmOpenOnceWithType
> diff --git a/xf86drm.c b/xf86drm.c
> index b49d42f7..229a54bf 100644
> --- a/xf86drm.c
> +++ b/xf86drm.c
> @@ -793,6 +793,48 @@ drm_public int drmOpenRender(int minor)
>      return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
>  }
>
> +/**
> + * Open the DRM device with specified type of specified framebuffer.
> + *
> + * Looks up the associated DRM device with specified type of the
> + * specified framebuffer and opens it.
> + *
> + * \param fb the index of framebuffer.
> + * \param type the device node type to open, PRIMARY, CONTROL or RENDER
> + *
> + * \return a file descriptor on success, or a negative value on error.
> + *
> + */
> +drm_public int drmOpenByFB(int fb, int type)
> +{
> +#ifdef __linux__
> +    DIR *sysdir;
> +    struct dirent *ent;
> +    char buf[64];
> +    const char *name = drmGetMinorName(type);
> +    int fd = -1, len = strlen(name);
> +
> +    snprintf(buf, sizeof(buf), "/sys/class/graphics/fb%d/device/drm", fb);
> +    sysdir = opendir(buf);
> +    if (!sysdir)
> +        return -errno;
> +
> +    while ((ent = readdir(sysdir))) {
> +        if (!strncmp(ent->d_name, name, len)) {
> +            snprintf(buf, sizeof(buf), "%s/%s", DRM_DIR_NAME, ent->d_name);
> +            fd = open(buf, O_RDWR | O_CLOEXEC, 0);
> +            break;
> +        }
> +    }
> +
> +    closedir(sysdir);
> +    return fd;
> +#else
> +#warning "Missing implementation of drmOpenByFB"
> +    return -EINVAL;
> +#endif
> +}
> +
>  /**
>   * Free the version information returned by drmGetVersion().
>   *
> diff --git a/xf86drm.h b/xf86drm.h
> index 7b85079a..d45d696f 100644
> --- a/xf86drm.h
> +++ b/xf86drm.h
> @@ -605,6 +605,7 @@ extern int           drmOpenWithType(const char *name, const char *busid,
>
>  extern int           drmOpenControl(int minor);
>  extern int           drmOpenRender(int minor);
> +extern int           drmOpenByFB(int fb, int type);
>  extern int           drmClose(int fd);
>  extern drmVersionPtr drmGetVersion(int fd);
>  extern drmVersionPtr drmGetLibVersion(int fd);
> --
> 2.25.1
>
> _______________________________________________
> dri-devel mailing list
> dri-devel@lists.freedesktop.org
> https://lists.freedesktop.org/mailman/listinfo/dri-devel



-- 
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] xf86drm: add drmOpenByFB
  2020-05-24 18:53 ` Daniel Vetter
@ 2020-05-24 19:25   ` Simon Ser
  0 siblings, 0 replies; 4+ messages in thread
From: Simon Ser @ 2020-05-24 19:25 UTC (permalink / raw)
  To: Daniel Vetter; +Cc: Robert Foss, Emil Velikov, Mauro Rossi, cwhuang, dri-devel

On Sunday, May 24, 2020 8:53 PM, Daniel Vetter <daniel@ffwll.ch> wrote:

> On Sat, May 23, 2020 at 5:44 PM Mauro Rossi issor.oruam@gmail.com wrote:
>
> > OpenByFB is introduced to overcome GPU driver loading order issue
> > on a device with multiple GPUs, e.g. Intel iGPU and Nvidia dGPU
> > where the first drmfb kernel module loaded will become device file
> > /dev/dri/card0 and the second will become /dev/dri/card1
> > The use case is to prefer Intel iGPU over dGPU, or viceversa,
> > in a deterministic and reliable manner.
> > OpenByFB function opens the DRM device with specified fb and node type,
> > thus enabling gralloc to open the correct device node of the primary fb,
> > regardless of the GPU driver loading order.
>
> The fbdev load ordering is as inconsistent/random as the dri node
> loading. Well more so, because you might have random firmware fbdev
> drivers hanging out there. Hence I'm not following how this solves
> anything for your problem.
>
> I think usually what userspace does it look at the boot_vga property
> of the underlying device in sysfs, and prefer that one.

Yes. See [1] for an example of how to do this.

[1]: https://github.com/swaywm/wlroots/blob/5c942bd5972afee9a68cb15c14aa83b4b0aaf82d/backend/session/session.c#L331
_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-23 15:44 [PATCH] xf86drm: add drmOpenByFB Mauro Rossi
2020-05-23 21:36 ` Simon Ser
2020-05-24 18:53 ` Daniel Vetter
2020-05-24 19:25   ` Simon Ser

dri-devel Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/dri-devel/0 dri-devel/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dri-devel dri-devel/ https://lore.kernel.org/dri-devel \
		dri-devel@lists.freedesktop.org
	public-inbox-index dri-devel

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.freedesktop.lists.dri-devel


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git