*Resend the whole actual series*
This patch implements a simple function for matching DRM device FDs against the desired properties of a device that you are looking for.
The properties that are possible to look for are the elements of DrmVersion and DrmDevice.
The discussion that led to this series can be found here: https://lists.freedesktop.org/archives/mesa-dev/2018-January/183878.html
In the previous discussion we left off having settled on implementing this in mesa/src/loader/loader.c, which I initially did. But the code ended up being so generic that there's no reason for it not to live in libdrm, since it can be used for any compositor and mesa.
The implementer will still have to iterate through all of the devices available on the target, and see if they match. This additional functionality could be moved into libdrm at a later point if it turns out that all of the users do this in the same manner. If there is some variety, for example with selecting fallback devices if nothing matches maybe it is best left up to the user of libdrm.
The biggest problem with the approach as implemented, the way I see it, is how we match against the DrmVersion/DrmDevice of a given FD. Currently we use DrmVersion & DrmDevice as supplied by the caller to define what we are looking for. This is a little bit problematic, especially for DrmDevice, since all of the elements of the struct do not have enough bitspace to signal that we are uninterested in looking for that specific element. DrmDevice uses drmDevicesEqual() to do what amounts to a memcmp of the DrmDevice argument and the one of the FD. So looking for any device on any PCI bus with just the PCI vendor ID supplied isn't possible.
An alternative Daniel Stone suggested is adding enums for different properties and allowing the caller to supply a list of properties that are interesting and their values. In terms of long-term maintainership this might be less pleasant than the approach of the current implementation.
Robert Foss (1): xf86drm: Add drmHandleMatch func
xf86drm.h | 2 ++ xf86drmMode.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)
drmHandleMatch is intended to allow for userspace to filter out devices that it does not want to open.
Opening specific devices using paths alone is not a reliable due to probing order. This function intends to provide a mechanism for filtering out devices that don't fit what you need using the already existing drmVersion and drmDevice structs.
Most fields of drmVersion and drmDevice can be used for comparing between the target device and the actual FD that has been provided.
Signed-off-by: Robert Foss robert.foss@collabora.com --- xf86drm.h | 2 ++ xf86drmMode.c | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+)
diff --git a/xf86drm.h b/xf86drm.h index 7773d71a8030..7ef1e2818301 100644 --- a/xf86drm.h +++ b/xf86drm.h @@ -863,6 +863,8 @@ extern int drmGetDevices2(uint32_t flags, drmDevicePtr devices[], int max_device
extern int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b);
+extern int drmHandleMatch(int fd, drmVersionPtr ver, drmDevicePtr dev); + extern int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle); extern int drmSyncobjDestroy(int fd, uint32_t handle); extern int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd); diff --git a/xf86drmMode.c b/xf86drmMode.c index 9a15b5e78dda..e403515a94b7 100644 --- a/xf86drmMode.c +++ b/xf86drmMode.c @@ -946,6 +946,75 @@ int drmHandleEvent(int fd, drmEventContextPtr evctx) return 0; }
+#define matchCmpInt(ver, ver_fd, element) \ + if (ver->element > 0 && ver->element != ver_fd->element) \ + goto fail; + +#define matchCmpStr(ver, ver_fd, element) \ + if (ver->element != NULL && !strcmp(ver->element, ver_fd->element)) \ + goto fail; + +static int drmMatchVersion(int fd, drmVersionPtr ver) +{ + drmVersionPtr ver_fd = NULL; + + if (!ver) + goto success; + + ver_fd = drmGetVersion(fd); + if (!ver_fd) + goto fail; + + matchCmpInt(ver, ver_fd, version_major); + matchCmpInt(ver, ver_fd, version_minor); + matchCmpInt(ver, ver_fd, version_patchlevel); + + matchCmpStr(ver, ver_fd, name); + matchCmpStr(ver, ver_fd, date); + matchCmpStr(ver, ver_fd, desc); + +success: + drmFreeVersion(ver_fd); + return 1; +fail: + drmFreeVersion(ver_fd); + return 0; +} + +static int drmMatchDevice(int fd, drmDevicePtr dev) +{ + drmDevicePtr dev_fd = NULL; + + if (!dev) + goto success; + + if (drmGetDevice2(fd, 0, &dev_fd) != 0) { + goto fail; + } + + matchCmpInt(dev, dev_fd, available_nodes) + matchCmpInt(dev, dev_fd, bustype) + + drmDevicesEqual(dev, dev_fd); + +success: + drmFreeDevice(&dev_fd); + return 1; +fail: + drmFreeDevice(&dev_fd); + return 0; +} + +int drmHandleMatch(int fd, drmVersionPtr ver, drmDevicePtr dev) +{ + int ret = 1; + + ret &= drmMatchVersion(fd, ver); + ret &= drmMatchDevice(fd, dev); + + return ret; +} + int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id, uint32_t flags, void *user_data) {
Hi Rob,
On Thu, Apr 12, 2018 at 12:56 PM Robert Foss robert.foss@collabora.com wrote:
*Resend the whole actual series*
This patch implements a simple function for matching DRM device FDs
against
the desired properties of a device that you are looking for.
The properties that are possible to look for are the elements of
DrmVersion
and DrmDevice.
The discussion that led to this series can be found here: https://lists.freedesktop.org/archives/mesa-dev/2018-January/183878.html
In the previous discussion we left off having settled on implementing this in mesa/src/loader/loader.c, which I initially did. But the code ended up
being
so generic that there's no reason for it not to live in libdrm, since it
can be
used for any compositor and mesa.
The implementer will still have to iterate through all of the devices
available
on the target, and see if they match. This additional functionality could
be
moved into libdrm at a later point if it turns out that all of the users
do this
in the same manner. If there is some variety, for example with selecting fallback devices if
nothing
matches maybe it is best left up to the user of libdrm.
The biggest problem with the approach as implemented, the way I see it,
is how
we match against the DrmVersion/DrmDevice of a given FD. Currently we use DrmVersion & DrmDevice as supplied by the caller to
define what
we are looking for. This is a little bit problematic, especially for DrmDevice, since all of
the
elements of the struct do not have enough bitspace to signal that we are uninterested in looking for that specific element. DrmDevice uses drmDevicesEqual() to do what amounts to a memcmp of the DrmDevice
argument and
the one of the FD. So looking for any device on any PCI bus with just the PCI vendor ID supplied isn't possible.
An alternative Daniel Stone suggested is adding enums for different
properties
and allowing the caller to supply a list of properties that are
interesting and
their values. In terms of long-term maintainership this might be less
pleasant
than the approach of the current implementation.
I'm personally okay with how patch 1 implements the matching. Thanks for this work. Looking forward to the Mesa probing helper, which uses this! :)
P.S. I normally use my @chromium.org email for mailing lists.
Best regards, Tomasz
dri-devel@lists.freedesktop.org