3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
48 #include <sys/types.h>
50 #define stat_t struct stat
51 #include <sys/ioctl.h>
55 #include <sys/mkdev.h>
57 #ifdef MAJOR_IN_SYSMACROS
58 #include <sys/sysmacros.h>
62 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
64 /* Not all systems have MAP_FAILED defined */
66 #define MAP_FAILED ((void *)-1)
70 #include "libdrm_macros.h"
72 #include "util_math.h"
75 #define DRM_PRIMARY_MINOR_NAME "drm"
76 #define DRM_CONTROL_MINOR_NAME "drmC"
77 #define DRM_RENDER_MINOR_NAME "drmR"
79 #define DRM_PRIMARY_MINOR_NAME "card"
80 #define DRM_CONTROL_MINOR_NAME "controlD"
81 #define DRM_RENDER_MINOR_NAME "renderD"
84 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
98 #endif /* __OpenBSD__ */
101 #define DRM_MAJOR 226 /* Linux */
104 #if defined(__OpenBSD__) || defined(__DragonFly__)
112 uint16_t subvendor_id;
113 uint16_t subdevice_id;
117 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
120 #define DRM_MSG_VERBOSITY 3
122 #define memclear(s) memset(&s, 0, sizeof(s))
124 static drmServerInfoPtr drm_server_info;
126 drm_public void drmSetServerInfo(drmServerInfoPtr info)
128 drm_server_info = info;
132 * Output a message to stderr.
134 * \param format printf() like format string.
137 * This function is a wrapper around vfprintf().
140 static int DRM_PRINTFLIKE(1, 0)
141 drmDebugPrint(const char *format, va_list ap)
143 return vfprintf(stderr, format, ap);
147 drmMsg(const char *format, ...)
151 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
152 (drm_server_info && drm_server_info->debug_print))
154 va_start(ap, format);
155 if (drm_server_info) {
156 drm_server_info->debug_print(format,ap);
158 drmDebugPrint(format, ap);
164 static void *drmHashTable = NULL; /* Context switch callbacks */
166 drm_public void *drmGetHashTable(void)
171 drm_public void *drmMalloc(int size)
173 return calloc(1, size);
176 drm_public void drmFree(void *pt)
182 * Call ioctl, restarting if it is interrupted
185 drmIoctl(int fd, unsigned long request, void *arg)
190 ret = ioctl(fd, request, arg);
191 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
195 static unsigned long drmGetKeyFromFd(int fd)
204 drm_public drmHashEntry *drmGetEntry(int fd)
206 unsigned long key = drmGetKeyFromFd(fd);
211 drmHashTable = drmHashCreate();
213 if (drmHashLookup(drmHashTable, key, &value)) {
214 entry = drmMalloc(sizeof(*entry));
217 entry->tagTable = drmHashCreate();
218 drmHashInsert(drmHashTable, key, entry);
226 * Compare two busid strings
231 * \return 1 if matched.
234 * This function compares two bus ID strings. It understands the older
235 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
236 * domain, b is bus, d is device, f is function.
238 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
240 /* First, check if the IDs are exactly the same */
241 if (strcasecmp(id1, id2) == 0)
244 /* Try to match old/new-style PCI bus IDs. */
245 if (strncasecmp(id1, "pci", 3) == 0) {
246 unsigned int o1, b1, d1, f1;
247 unsigned int o2, b2, d2, f2;
250 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
253 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
258 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
261 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
266 /* If domains aren't properly supported by the kernel interface,
267 * just ignore them, which sucks less than picking a totally random
268 * card with "open by name"
273 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
282 * Handles error checking for chown call.
284 * \param path to file.
285 * \param id of the new owner.
286 * \param id of the new group.
288 * \return zero if success or -1 if failure.
291 * Checks for failure. If failure was caused by signal call chown again.
292 * If any other failure happened then it will output error message using
296 static int chown_check_return(const char *path, uid_t owner, gid_t group)
301 rv = chown(path, owner, group);
302 } while (rv != 0 && errno == EINTR);
307 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
308 path, errno, strerror(errno));
314 * Open the DRM device, creating it if necessary.
316 * \param dev major and minor numbers of the device.
317 * \param minor minor number of the device.
319 * \return a file descriptor on success, or a negative value on error.
322 * Assembles the device name from \p minor and opens it, creating the device
323 * special file node with the major and minor numbers specified by \p dev and
324 * parent directory if necessary and was called by root.
326 static int drmOpenDevice(dev_t dev, int minor, int type)
329 const char *dev_name;
332 mode_t devmode = DRM_DEV_MODE, serv_mode;
335 int isroot = !geteuid();
336 uid_t user = DRM_DEV_UID;
337 gid_t group = DRM_DEV_GID;
341 case DRM_NODE_PRIMARY:
342 dev_name = DRM_DEV_NAME;
344 case DRM_NODE_CONTROL:
345 dev_name = DRM_CONTROL_DEV_NAME;
347 case DRM_NODE_RENDER:
348 dev_name = DRM_RENDER_DEV_NAME;
354 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
355 drmMsg("drmOpenDevice: node name is %s\n", buf);
357 if (drm_server_info && drm_server_info->get_perms) {
358 drm_server_info->get_perms(&serv_group, &serv_mode);
359 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
360 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
364 if (stat(DRM_DIR_NAME, &st)) {
366 return DRM_ERR_NOT_ROOT;
367 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
368 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
369 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
372 /* Check if the device node exists and create it if necessary. */
373 if (stat(buf, &st)) {
375 return DRM_ERR_NOT_ROOT;
377 mknod(buf, S_IFCHR | devmode, dev);
380 if (drm_server_info && drm_server_info->get_perms) {
381 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
382 chown_check_return(buf, user, group);
386 /* if we modprobed then wait for udev */
390 if (stat(DRM_DIR_NAME, &st)) {
394 if (udev_count == 50)
399 if (stat(buf, &st)) {
403 if (udev_count == 50)
410 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
411 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
412 fd, fd < 0 ? strerror(errno) : "OK");
417 /* Check if the device node is not what we expect it to be, and recreate it
418 * and try again if so.
420 if (st.st_rdev != dev) {
422 return DRM_ERR_NOT_ROOT;
424 mknod(buf, S_IFCHR | devmode, dev);
425 if (drm_server_info && drm_server_info->get_perms) {
426 chown_check_return(buf, user, group);
430 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
431 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
432 fd, fd < 0 ? strerror(errno) : "OK");
436 drmMsg("drmOpenDevice: Open failed\n");
444 * Open the DRM device
446 * \param minor device minor number.
447 * \param create allow to create the device if set.
449 * \return a file descriptor on success, or a negative value on error.
452 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
453 * name from \p minor and opens it.
455 static int drmOpenMinor(int minor, int create, int type)
459 const char *dev_name;
462 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
465 case DRM_NODE_PRIMARY:
466 dev_name = DRM_DEV_NAME;
468 case DRM_NODE_CONTROL:
469 dev_name = DRM_CONTROL_DEV_NAME;
471 case DRM_NODE_RENDER:
472 dev_name = DRM_RENDER_DEV_NAME;
478 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
479 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
486 * Determine whether the DRM kernel driver has been loaded.
488 * \return 1 if the DRM driver is loaded, 0 otherwise.
491 * Determine the presence of the kernel driver by attempting to open the 0
492 * minor and get version information. For backward compatibility with older
493 * Linux implementations, /proc/dri is also checked.
495 drm_public int drmAvailable(void)
497 drmVersionPtr version;
501 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
503 /* Try proc for backward Linux compatibility */
504 if (!access("/proc/dri/0", R_OK))
510 if ((version = drmGetVersion(fd))) {
512 drmFreeVersion(version);
519 static int drmGetMinorBase(int type)
522 case DRM_NODE_PRIMARY:
524 case DRM_NODE_CONTROL:
526 case DRM_NODE_RENDER:
533 static int drmGetMinorType(int minor)
535 int type = minor >> 6;
541 case DRM_NODE_PRIMARY:
542 case DRM_NODE_CONTROL:
543 case DRM_NODE_RENDER:
550 static const char *drmGetMinorName(int type)
553 case DRM_NODE_PRIMARY:
554 return DRM_PRIMARY_MINOR_NAME;
555 case DRM_NODE_CONTROL:
556 return DRM_CONTROL_MINOR_NAME;
557 case DRM_NODE_RENDER:
558 return DRM_RENDER_MINOR_NAME;
565 * Open the device by bus ID.
567 * \param busid bus ID.
568 * \param type device node type.
570 * \return a file descriptor on success, or a negative value on error.
573 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
574 * comparing the device bus ID with the one supplied.
576 * \sa drmOpenMinor() and drmGetBusid().
578 static int drmOpenByBusid(const char *busid, int type)
580 int i, pci_domain_ok = 1;
584 int base = drmGetMinorBase(type);
589 /* We need to try for 1.4 first for proper PCI domain support */
590 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
591 for (i = base; i < base + DRM_MAX_MINOR; i++) {
592 fd = drmOpenMinor(i, 1, type);
593 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
597 sv.drm_dd_major = -1; /* Don't care */
598 sv.drm_dd_minor = -1; /* Don't care */
599 if (!drmSetInterfaceVersion(fd, &sv)) {
600 buf = drmGetBusid(fd);
601 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
602 if (buf && drmMatchBusID(buf, busid, 1)) {
613 for (i = base; i < base + DRM_MAX_MINOR; i++) {
614 fd = drmOpenMinor(i, 1, type);
615 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
617 /* We need to try for 1.4 first for proper PCI domain support
618 * and if that fails, we know the kernel is busted
622 sv.drm_dd_major = -1; /* Don't care */
623 sv.drm_dd_minor = -1; /* Don't care */
624 if (drmSetInterfaceVersion(fd, &sv)) {
630 sv.drm_dd_major = -1; /* Don't care */
631 sv.drm_dd_minor = -1; /* Don't care */
632 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
633 drmSetInterfaceVersion(fd, &sv);
635 buf = drmGetBusid(fd);
636 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
637 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
651 * Open the device by name.
653 * \param name driver name.
654 * \param type the device node type.
656 * \return a file descriptor on success, or a negative value on error.
659 * This function opens the first minor number that matches the driver name and
660 * isn't already in use. If it's in use it then it will already have a bus ID
663 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
665 static int drmOpenByName(const char *name, int type)
669 drmVersionPtr version;
671 int base = drmGetMinorBase(type);
677 * Open the first minor number that matches the driver name and isn't
678 * already in use. If it's in use it will have a busid assigned already.
680 for (i = base; i < base + DRM_MAX_MINOR; i++) {
681 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
682 if ((version = drmGetVersion(fd))) {
683 if (!strcmp(version->name, name)) {
684 drmFreeVersion(version);
685 id = drmGetBusid(fd);
686 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
695 drmFreeVersion(version);
703 /* Backward-compatibility /proc support */
704 for (i = 0; i < 8; i++) {
705 char proc_name[64], buf[512];
706 char *driver, *pt, *devstring;
709 sprintf(proc_name, "/proc/dri/%d/name", i);
710 if ((fd = open(proc_name, 0, 0)) >= 0) {
711 retcode = read(fd, buf, sizeof(buf)-1);
714 buf[retcode-1] = '\0';
715 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
717 if (*pt) { /* Device is next */
719 if (!strcmp(driver, name)) { /* Match */
720 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
722 if (*pt) { /* Found busid */
723 return drmOpenByBusid(++pt, type);
724 } else { /* No busid */
725 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
739 * Open the DRM device.
741 * Looks up the specified name and bus ID, and opens the device found. The
742 * entry in /dev/dri is created if necessary and if called by root.
744 * \param name driver name. Not referenced if bus ID is supplied.
745 * \param busid bus ID. Zero if not known.
747 * \return a file descriptor on success, or a negative value on error.
750 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
753 drm_public int drmOpen(const char *name, const char *busid)
755 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
759 * Open the DRM device with specified type.
761 * Looks up the specified name and bus ID, and opens the device found. The
762 * entry in /dev/dri is created if necessary and if called by root.
764 * \param name driver name. Not referenced if bus ID is supplied.
765 * \param busid bus ID. Zero if not known.
766 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
768 * \return a file descriptor on success, or a negative value on error.
771 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
774 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
776 if (!drmAvailable() && name != NULL && drm_server_info &&
777 drm_server_info->load_module) {
778 /* try to load the kernel module */
779 if (!drm_server_info->load_module(name)) {
780 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
786 int fd = drmOpenByBusid(busid, type);
792 return drmOpenByName(name, type);
797 drm_public int drmOpenControl(int minor)
799 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
802 drm_public int drmOpenRender(int minor)
804 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
808 * Free the version information returned by drmGetVersion().
810 * \param v pointer to the version information.
813 * It frees the memory pointed by \p %v as well as all the non-null strings
816 drm_public void drmFreeVersion(drmVersionPtr v)
828 * Free the non-public version information returned by the kernel.
830 * \param v pointer to the version information.
833 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
834 * the non-null strings pointers in it.
836 static void drmFreeKernelVersion(drm_version_t *v)
848 * Copy version information.
850 * \param d destination pointer.
851 * \param s source pointer.
854 * Used by drmGetVersion() to translate the information returned by the ioctl
855 * interface in a private structure into the public structure counterpart.
857 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
859 d->version_major = s->version_major;
860 d->version_minor = s->version_minor;
861 d->version_patchlevel = s->version_patchlevel;
862 d->name_len = s->name_len;
863 d->name = strdup(s->name);
864 d->date_len = s->date_len;
865 d->date = strdup(s->date);
866 d->desc_len = s->desc_len;
867 d->desc = strdup(s->desc);
872 * Query the driver version information.
874 * \param fd file descriptor.
876 * \return pointer to a drmVersion structure which should be freed with
879 * \note Similar information is available via /proc/dri.
882 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
883 * first with zeros to get the string lengths, and then the actually strings.
884 * It also null-terminates them since they might not be already.
886 drm_public drmVersionPtr drmGetVersion(int fd)
888 drmVersionPtr retval;
889 drm_version_t *version = drmMalloc(sizeof(*version));
891 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
892 drmFreeKernelVersion(version);
896 if (version->name_len)
897 version->name = drmMalloc(version->name_len + 1);
898 if (version->date_len)
899 version->date = drmMalloc(version->date_len + 1);
900 if (version->desc_len)
901 version->desc = drmMalloc(version->desc_len + 1);
903 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
904 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
905 drmFreeKernelVersion(version);
909 /* The results might not be null-terminated strings, so terminate them. */
910 if (version->name_len) version->name[version->name_len] = '\0';
911 if (version->date_len) version->date[version->date_len] = '\0';
912 if (version->desc_len) version->desc[version->desc_len] = '\0';
914 retval = drmMalloc(sizeof(*retval));
915 drmCopyVersion(retval, version);
916 drmFreeKernelVersion(version);
922 * Get version information for the DRM user space library.
924 * This version number is driver independent.
926 * \param fd file descriptor.
928 * \return version information.
931 * This function allocates and fills a drm_version structure with a hard coded
934 drm_public drmVersionPtr drmGetLibVersion(int fd)
936 drm_version_t *version = drmMalloc(sizeof(*version));
939 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
940 * revision 1.0.x = original DRM interface with no drmGetLibVersion
941 * entry point and many drm<Device> extensions
942 * revision 1.1.x = added drmCommand entry points for device extensions
943 * added drmGetLibVersion to identify libdrm.a version
944 * revision 1.2.x = added drmSetInterfaceVersion
945 * modified drmOpen to handle both busid and name
946 * revision 1.3.x = added server + memory manager
948 version->version_major = 1;
949 version->version_minor = 3;
950 version->version_patchlevel = 0;
952 return (drmVersionPtr)version;
955 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
957 struct drm_get_cap cap;
961 cap.capability = capability;
963 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
971 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
973 struct drm_set_client_cap cap;
976 cap.capability = capability;
979 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
983 * Free the bus ID information.
985 * \param busid bus ID information string as given by drmGetBusid().
988 * This function is just frees the memory pointed by \p busid.
990 drm_public void drmFreeBusid(const char *busid)
992 drmFree((void *)busid);
997 * Get the bus ID of the device.
999 * \param fd file descriptor.
1001 * \return bus ID string.
1004 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
1005 * get the string length and data, passing the arguments in a drm_unique
1008 drm_public char *drmGetBusid(int fd)
1014 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
1016 u.unique = drmMalloc(u.unique_len + 1);
1017 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1021 u.unique[u.unique_len] = '\0';
1028 * Set the bus ID of the device.
1030 * \param fd file descriptor.
1031 * \param busid bus ID string.
1033 * \return zero on success, negative on failure.
1036 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1037 * the arguments in a drm_unique structure.
1039 drm_public int drmSetBusid(int fd, const char *busid)
1044 u.unique = (char *)busid;
1045 u.unique_len = strlen(busid);
1047 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1053 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1060 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1062 *magic = auth.magic;
1066 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1072 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1078 * Specifies a range of memory that is available for mapping by a
1081 * \param fd file descriptor.
1082 * \param offset usually the physical address. The actual meaning depends of
1083 * the \p type parameter. See below.
1084 * \param size of the memory in bytes.
1085 * \param type type of the memory to be mapped.
1086 * \param flags combination of several flags to modify the function actions.
1087 * \param handle will be set to a value that may be used as the offset
1088 * parameter for mmap().
1090 * \return zero on success or a negative value on error.
1092 * \par Mapping the frame buffer
1093 * For the frame buffer
1094 * - \p offset will be the physical address of the start of the frame buffer,
1095 * - \p size will be the size of the frame buffer in bytes, and
1096 * - \p type will be DRM_FRAME_BUFFER.
1099 * The area mapped will be uncached. If MTRR support is available in the
1100 * kernel, the frame buffer area will be set to write combining.
1102 * \par Mapping the MMIO register area
1103 * For the MMIO register area,
1104 * - \p offset will be the physical address of the start of the register area,
1105 * - \p size will be the size of the register area bytes, and
1106 * - \p type will be DRM_REGISTERS.
1108 * The area mapped will be uncached.
1110 * \par Mapping the SAREA
1112 * - \p offset will be ignored and should be set to zero,
1113 * - \p size will be the desired size of the SAREA in bytes,
1114 * - \p type will be DRM_SHM.
1117 * A shared memory area of the requested size will be created and locked in
1118 * kernel memory. This area may be mapped into client-space by using the handle
1121 * \note May only be called by root.
1124 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1125 * the arguments in a drm_map structure.
1127 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1128 drmMapFlags flags, drm_handle_t *handle)
1133 map.offset = offset;
1137 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1140 *handle = (drm_handle_t)(uintptr_t)map.handle;
1144 drm_public int drmRmMap(int fd, drm_handle_t handle)
1149 map.handle = (void *)(uintptr_t)handle;
1151 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1157 * Make buffers available for DMA transfers.
1159 * \param fd file descriptor.
1160 * \param count number of buffers.
1161 * \param size size of each buffer.
1162 * \param flags buffer allocation flags.
1163 * \param agp_offset offset in the AGP aperture
1165 * \return number of buffers allocated, negative on error.
1168 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1172 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1175 drm_buf_desc_t request;
1178 request.count = count;
1179 request.size = size;
1180 request.flags = flags;
1181 request.agp_start = agp_offset;
1183 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1185 return request.count;
1188 drm_public int drmMarkBufs(int fd, double low, double high)
1190 drm_buf_info_t info;
1195 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1201 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1204 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1205 int retval = -errno;
1210 for (i = 0; i < info.count; i++) {
1211 info.list[i].low_mark = low * info.list[i].count;
1212 info.list[i].high_mark = high * info.list[i].count;
1213 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1214 int retval = -errno;
1227 * \param fd file descriptor.
1228 * \param count number of buffers to free.
1229 * \param list list of buffers to be freed.
1231 * \return zero on success, or a negative value on failure.
1233 * \note This function is primarily used for debugging.
1236 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1237 * the arguments in a drm_buf_free structure.
1239 drm_public int drmFreeBufs(int fd, int count, int *list)
1241 drm_buf_free_t request;
1244 request.count = count;
1245 request.list = list;
1246 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1255 * \param fd file descriptor.
1258 * This function closes the file descriptor.
1260 drm_public int drmClose(int fd)
1262 unsigned long key = drmGetKeyFromFd(fd);
1263 drmHashEntry *entry = drmGetEntry(fd);
1265 drmHashDestroy(entry->tagTable);
1268 entry->tagTable = NULL;
1270 drmHashDelete(drmHashTable, key);
1278 * Map a region of memory.
1280 * \param fd file descriptor.
1281 * \param handle handle returned by drmAddMap().
1282 * \param size size in bytes. Must match the size used by drmAddMap().
1283 * \param address will contain the user-space virtual address where the mapping
1286 * \return zero on success, or a negative value on failure.
1289 * This function is a wrapper for mmap().
1291 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1292 drmAddressPtr address)
1294 static unsigned long pagesize_mask = 0;
1300 pagesize_mask = getpagesize() - 1;
1302 size = (size + pagesize_mask) & ~pagesize_mask;
1304 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1305 if (*address == MAP_FAILED)
1312 * Unmap mappings obtained with drmMap().
1314 * \param address address as given by drmMap().
1315 * \param size size in bytes. Must match the size used by drmMap().
1317 * \return zero on success, or a negative value on failure.
1320 * This function is a wrapper for munmap().
1322 drm_public int drmUnmap(drmAddress address, drmSize size)
1324 return drm_munmap(address, size);
1327 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1329 drm_buf_info_t info;
1330 drmBufInfoPtr retval;
1335 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1339 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1342 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1347 retval = drmMalloc(sizeof(*retval));
1348 retval->count = info.count;
1349 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1350 for (i = 0; i < info.count; i++) {
1351 retval->list[i].count = info.list[i].count;
1352 retval->list[i].size = info.list[i].size;
1353 retval->list[i].low_mark = info.list[i].low_mark;
1354 retval->list[i].high_mark = info.list[i].high_mark;
1363 * Map all DMA buffers into client-virtual space.
1365 * \param fd file descriptor.
1367 * \return a pointer to a ::drmBufMap structure.
1369 * \note The client may not use these buffers until obtaining buffer indices
1373 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1374 * information about the buffers in a drm_buf_map structure into the
1375 * client-visible data structures.
1377 drm_public drmBufMapPtr drmMapBufs(int fd)
1380 drmBufMapPtr retval;
1384 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1390 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1393 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1398 retval = drmMalloc(sizeof(*retval));
1399 retval->count = bufs.count;
1400 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1401 for (i = 0; i < bufs.count; i++) {
1402 retval->list[i].idx = bufs.list[i].idx;
1403 retval->list[i].total = bufs.list[i].total;
1404 retval->list[i].used = 0;
1405 retval->list[i].address = bufs.list[i].address;
1414 * Unmap buffers allocated with drmMapBufs().
1416 * \return zero on success, or negative value on failure.
1419 * Calls munmap() for every buffer stored in \p bufs and frees the
1420 * memory allocated by drmMapBufs().
1422 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1426 for (i = 0; i < bufs->count; i++) {
1427 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1430 drmFree(bufs->list);
1436 #define DRM_DMA_RETRY 16
1439 * Reserve DMA buffers.
1441 * \param fd file descriptor.
1444 * \return zero on success, or a negative value on failure.
1447 * Assemble the arguments into a drm_dma structure and keeps issuing the
1448 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1450 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1455 dma.context = request->context;
1456 dma.send_count = request->send_count;
1457 dma.send_indices = request->send_list;
1458 dma.send_sizes = request->send_sizes;
1459 dma.flags = request->flags;
1460 dma.request_count = request->request_count;
1461 dma.request_size = request->request_size;
1462 dma.request_indices = request->request_list;
1463 dma.request_sizes = request->request_sizes;
1464 dma.granted_count = 0;
1467 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1468 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1471 request->granted_count = dma.granted_count;
1480 * Obtain heavyweight hardware lock.
1482 * \param fd file descriptor.
1483 * \param context context.
1484 * \param flags flags that determine the state of the hardware when the function
1487 * \return always zero.
1490 * This function translates the arguments into a drm_lock structure and issue
1491 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1493 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1498 lock.context = context;
1500 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1501 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1502 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1503 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1504 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1505 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1507 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1513 * Release the hardware lock.
1515 * \param fd file descriptor.
1516 * \param context context.
1518 * \return zero on success, or a negative value on failure.
1521 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1522 * argument in a drm_lock structure.
1524 drm_public int drmUnlock(int fd, drm_context_t context)
1529 lock.context = context;
1530 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1533 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1537 drm_context_t * retval;
1541 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1547 if (!(list = drmMalloc(res.count * sizeof(*list))))
1549 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1552 res.contexts = list;
1553 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1554 goto err_free_context;
1556 for (i = 0; i < res.count; i++)
1557 retval[i] = list[i].handle;
1570 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1578 * Used by the X server during GLXContext initialization. This causes
1579 * per-context kernel-level resources to be allocated.
1581 * \param fd file descriptor.
1582 * \param handle is set on success. To be used by the client when requesting DMA
1583 * dispatch with drmDMA().
1585 * \return zero on success, or a negative value on failure.
1587 * \note May only be called by root.
1590 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1591 * argument in a drm_ctx structure.
1593 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1598 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1600 *handle = ctx.handle;
1604 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1609 ctx.handle = context;
1610 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1615 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1616 drm_context_tFlags flags)
1621 * Context preserving means that no context switches are done between DMA
1622 * buffers from one context and the next. This is suitable for use in the
1623 * X server (which promises to maintain hardware context), or in the
1624 * client-side library when buffers are swapped on behalf of two threads.
1627 ctx.handle = context;
1628 if (flags & DRM_CONTEXT_PRESERVED)
1629 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1630 if (flags & DRM_CONTEXT_2DONLY)
1631 ctx.flags |= _DRM_CONTEXT_2DONLY;
1632 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1637 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1638 drm_context_tFlagsPtr flags)
1643 ctx.handle = context;
1644 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1647 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1648 *flags |= DRM_CONTEXT_PRESERVED;
1649 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1650 *flags |= DRM_CONTEXT_2DONLY;
1657 * Free any kernel-level resources allocated with drmCreateContext() associated
1660 * \param fd file descriptor.
1661 * \param handle handle given by drmCreateContext().
1663 * \return zero on success, or a negative value on failure.
1665 * \note May only be called by root.
1668 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1669 * argument in a drm_ctx structure.
1671 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1676 ctx.handle = handle;
1677 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1682 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1687 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1689 *handle = draw.handle;
1693 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1698 draw.handle = handle;
1699 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1704 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1705 drm_drawable_info_type_t type,
1706 unsigned int num, void *data)
1708 drm_update_draw_t update;
1711 update.handle = handle;
1714 update.data = (unsigned long long)(unsigned long)data;
1716 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1722 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1725 struct drm_crtc_get_sequence get_seq;
1729 get_seq.crtc_id = crtcId;
1730 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1735 *sequence = get_seq.sequence;
1737 *ns = get_seq.sequence_ns;
1741 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1743 uint64_t *sequence_queued,
1746 struct drm_crtc_queue_sequence queue_seq;
1749 memclear(queue_seq);
1750 queue_seq.crtc_id = crtcId;
1751 queue_seq.flags = flags;
1752 queue_seq.sequence = sequence;
1753 queue_seq.user_data = user_data;
1755 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1756 if (ret == 0 && sequence_queued)
1757 *sequence_queued = queue_seq.sequence;
1763 * Acquire the AGP device.
1765 * Must be called before any of the other AGP related calls.
1767 * \param fd file descriptor.
1769 * \return zero on success, or a negative value on failure.
1772 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1774 drm_public int drmAgpAcquire(int fd)
1776 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1783 * Release the AGP device.
1785 * \param fd file descriptor.
1787 * \return zero on success, or a negative value on failure.
1790 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1792 drm_public int drmAgpRelease(int fd)
1794 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1803 * \param fd file descriptor.
1804 * \param mode AGP mode.
1806 * \return zero on success, or a negative value on failure.
1809 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1810 * argument in a drm_agp_mode structure.
1812 drm_public int drmAgpEnable(int fd, unsigned long mode)
1818 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1825 * Allocate a chunk of AGP memory.
1827 * \param fd file descriptor.
1828 * \param size requested memory size in bytes. Will be rounded to page boundary.
1829 * \param type type of memory to allocate.
1830 * \param address if not zero, will be set to the physical address of the
1832 * \param handle on success will be set to a handle of the allocated memory.
1834 * \return zero on success, or a negative value on failure.
1837 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1838 * arguments in a drm_agp_buffer structure.
1840 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1841 unsigned long *address, drm_handle_t *handle)
1846 *handle = DRM_AGP_NO_HANDLE;
1849 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1852 *address = b.physical;
1859 * Free a chunk of AGP memory.
1861 * \param fd file descriptor.
1862 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1864 * \return zero on success, or a negative value on failure.
1867 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1868 * argument in a drm_agp_buffer structure.
1870 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1876 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1883 * Bind a chunk of AGP memory.
1885 * \param fd file descriptor.
1886 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1887 * \param offset offset in bytes. It will round to page boundary.
1889 * \return zero on success, or a negative value on failure.
1892 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1893 * argument in a drm_agp_binding structure.
1895 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1897 drm_agp_binding_t b;
1902 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1909 * Unbind a chunk of AGP memory.
1911 * \param fd file descriptor.
1912 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1914 * \return zero on success, or a negative value on failure.
1917 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1918 * the argument in a drm_agp_binding structure.
1920 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1922 drm_agp_binding_t b;
1926 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1933 * Get AGP driver major version number.
1935 * \param fd file descriptor.
1937 * \return major version number on success, or a negative value on failure..
1940 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1941 * necessary information in a drm_agp_info structure.
1943 drm_public int drmAgpVersionMajor(int fd)
1949 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1951 return i.agp_version_major;
1956 * Get AGP driver minor version number.
1958 * \param fd file descriptor.
1960 * \return minor version number on success, or a negative value on failure.
1963 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1964 * necessary information in a drm_agp_info structure.
1966 drm_public int drmAgpVersionMinor(int fd)
1972 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1974 return i.agp_version_minor;
1981 * \param fd file descriptor.
1983 * \return mode on success, or zero on failure.
1986 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1987 * necessary information in a drm_agp_info structure.
1989 drm_public unsigned long drmAgpGetMode(int fd)
1995 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2002 * Get AGP aperture base.
2004 * \param fd file descriptor.
2006 * \return aperture base on success, zero on failure.
2009 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2010 * necessary information in a drm_agp_info structure.
2012 drm_public unsigned long drmAgpBase(int fd)
2018 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2020 return i.aperture_base;
2025 * Get AGP aperture size.
2027 * \param fd file descriptor.
2029 * \return aperture size on success, zero on failure.
2032 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2033 * necessary information in a drm_agp_info structure.
2035 drm_public unsigned long drmAgpSize(int fd)
2041 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2043 return i.aperture_size;
2048 * Get used AGP memory.
2050 * \param fd file descriptor.
2052 * \return memory used on success, or zero on failure.
2055 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2056 * necessary information in a drm_agp_info structure.
2058 drm_public unsigned long drmAgpMemoryUsed(int fd)
2064 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2066 return i.memory_used;
2071 * Get available AGP memory.
2073 * \param fd file descriptor.
2075 * \return memory available on success, or zero on failure.
2078 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2079 * necessary information in a drm_agp_info structure.
2081 drm_public unsigned long drmAgpMemoryAvail(int fd)
2087 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2089 return i.memory_allowed;
2094 * Get hardware vendor ID.
2096 * \param fd file descriptor.
2098 * \return vendor ID on success, or zero on failure.
2101 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2102 * necessary information in a drm_agp_info structure.
2104 drm_public unsigned int drmAgpVendorId(int fd)
2110 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2117 * Get hardware device ID.
2119 * \param fd file descriptor.
2121 * \return zero on success, or zero on failure.
2124 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2125 * necessary information in a drm_agp_info structure.
2127 drm_public unsigned int drmAgpDeviceId(int fd)
2133 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2138 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2139 drm_handle_t *handle)
2141 drm_scatter_gather_t sg;
2147 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2149 *handle = sg.handle;
2153 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2155 drm_scatter_gather_t sg;
2159 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2167 * \param fd file descriptor.
2168 * \param vbl pointer to a drmVBlank structure.
2170 * \return zero on success, or a negative value on failure.
2173 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2175 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2177 struct timespec timeout, cur;
2180 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2182 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2188 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2189 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2190 if (ret && errno == EINTR) {
2191 clock_gettime(CLOCK_MONOTONIC, &cur);
2192 /* Timeout after 1s */
2193 if (cur.tv_sec > timeout.tv_sec + 1 ||
2194 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2201 } while (ret && errno == EINTR);
2207 drm_public int drmError(int err, const char *label)
2210 case DRM_ERR_NO_DEVICE:
2211 fprintf(stderr, "%s: no device\n", label);
2213 case DRM_ERR_NO_ACCESS:
2214 fprintf(stderr, "%s: no access\n", label);
2216 case DRM_ERR_NOT_ROOT:
2217 fprintf(stderr, "%s: not root\n", label);
2219 case DRM_ERR_INVALID:
2220 fprintf(stderr, "%s: invalid args\n", label);
2225 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2233 * Install IRQ handler.
2235 * \param fd file descriptor.
2236 * \param irq IRQ number.
2238 * \return zero on success, or a negative value on failure.
2241 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2242 * argument in a drm_control structure.
2244 drm_public int drmCtlInstHandler(int fd, int irq)
2249 ctl.func = DRM_INST_HANDLER;
2251 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2258 * Uninstall IRQ handler.
2260 * \param fd file descriptor.
2262 * \return zero on success, or a negative value on failure.
2265 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2266 * argument in a drm_control structure.
2268 drm_public int drmCtlUninstHandler(int fd)
2273 ctl.func = DRM_UNINST_HANDLER;
2275 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2280 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2285 lock.context = context;
2286 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2287 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2288 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2289 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2290 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2291 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2292 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2298 * Get IRQ from bus ID.
2300 * \param fd file descriptor.
2301 * \param busnum bus number.
2302 * \param devnum device number.
2303 * \param funcnum function number.
2305 * \return IRQ number on success, or a negative value on failure.
2308 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2309 * arguments in a drm_irq_busid structure.
2311 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2319 p.funcnum = funcnum;
2320 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2325 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2327 drmHashEntry *entry = drmGetEntry(fd);
2329 if (drmHashInsert(entry->tagTable, context, tag)) {
2330 drmHashDelete(entry->tagTable, context);
2331 drmHashInsert(entry->tagTable, context, tag);
2336 drm_public int drmDelContextTag(int fd, drm_context_t context)
2338 drmHashEntry *entry = drmGetEntry(fd);
2340 return drmHashDelete(entry->tagTable, context);
2343 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2345 drmHashEntry *entry = drmGetEntry(fd);
2348 if (drmHashLookup(entry->tagTable, context, &value))
2354 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2355 drm_handle_t handle)
2357 drm_ctx_priv_map_t map;
2360 map.ctx_id = ctx_id;
2361 map.handle = (void *)(uintptr_t)handle;
2363 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2368 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2369 drm_handle_t *handle)
2371 drm_ctx_priv_map_t map;
2374 map.ctx_id = ctx_id;
2376 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2379 *handle = (drm_handle_t)(uintptr_t)map.handle;
2384 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2385 drmMapType *type, drmMapFlags *flags,
2386 drm_handle_t *handle, int *mtrr)
2392 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2394 *offset = map.offset;
2398 *handle = (unsigned long)map.handle;
2403 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2404 unsigned long *magic, unsigned long *iocs)
2406 drm_client_t client;
2410 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2412 *auth = client.auth;
2415 *magic = client.magic;
2416 *iocs = client.iocs;
2420 drm_public int drmGetStats(int fd, drmStatsT *stats)
2426 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2430 memset(stats, 0, sizeof(*stats));
2431 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2435 stats->data[i].long_format = "%-20.20s"; \
2436 stats->data[i].rate_format = "%8.8s"; \
2437 stats->data[i].isvalue = 1; \
2438 stats->data[i].verbose = 0
2441 stats->data[i].long_format = "%-20.20s"; \
2442 stats->data[i].rate_format = "%5.5s"; \
2443 stats->data[i].isvalue = 0; \
2444 stats->data[i].mult_names = "kgm"; \
2445 stats->data[i].mult = 1000; \
2446 stats->data[i].verbose = 0
2449 stats->data[i].long_format = "%-20.20s"; \
2450 stats->data[i].rate_format = "%5.5s"; \
2451 stats->data[i].isvalue = 0; \
2452 stats->data[i].mult_names = "KGM"; \
2453 stats->data[i].mult = 1024; \
2454 stats->data[i].verbose = 0
2457 stats->count = s.count;
2458 for (i = 0; i < s.count; i++) {
2459 stats->data[i].value = s.data[i].value;
2460 switch (s.data[i].type) {
2461 case _DRM_STAT_LOCK:
2462 stats->data[i].long_name = "Lock";
2463 stats->data[i].rate_name = "Lock";
2466 case _DRM_STAT_OPENS:
2467 stats->data[i].long_name = "Opens";
2468 stats->data[i].rate_name = "O";
2470 stats->data[i].verbose = 1;
2472 case _DRM_STAT_CLOSES:
2473 stats->data[i].long_name = "Closes";
2474 stats->data[i].rate_name = "Lock";
2476 stats->data[i].verbose = 1;
2478 case _DRM_STAT_IOCTLS:
2479 stats->data[i].long_name = "Ioctls";
2480 stats->data[i].rate_name = "Ioc/s";
2483 case _DRM_STAT_LOCKS:
2484 stats->data[i].long_name = "Locks";
2485 stats->data[i].rate_name = "Lck/s";
2488 case _DRM_STAT_UNLOCKS:
2489 stats->data[i].long_name = "Unlocks";
2490 stats->data[i].rate_name = "Unl/s";
2494 stats->data[i].long_name = "IRQs";
2495 stats->data[i].rate_name = "IRQ/s";
2498 case _DRM_STAT_PRIMARY:
2499 stats->data[i].long_name = "Primary Bytes";
2500 stats->data[i].rate_name = "PB/s";
2503 case _DRM_STAT_SECONDARY:
2504 stats->data[i].long_name = "Secondary Bytes";
2505 stats->data[i].rate_name = "SB/s";
2509 stats->data[i].long_name = "DMA";
2510 stats->data[i].rate_name = "DMA/s";
2513 case _DRM_STAT_SPECIAL:
2514 stats->data[i].long_name = "Special DMA";
2515 stats->data[i].rate_name = "dma/s";
2518 case _DRM_STAT_MISSED:
2519 stats->data[i].long_name = "Miss";
2520 stats->data[i].rate_name = "Ms/s";
2523 case _DRM_STAT_VALUE:
2524 stats->data[i].long_name = "Value";
2525 stats->data[i].rate_name = "Value";
2528 case _DRM_STAT_BYTE:
2529 stats->data[i].long_name = "Bytes";
2530 stats->data[i].rate_name = "B/s";
2533 case _DRM_STAT_COUNT:
2535 stats->data[i].long_name = "Count";
2536 stats->data[i].rate_name = "Cnt/s";
2545 * Issue a set-version ioctl.
2547 * \param fd file descriptor.
2548 * \param drmCommandIndex command index
2549 * \param data source pointer of the data to be read and written.
2550 * \param size size of the data to be read and written.
2552 * \return zero on success, or a negative value on failure.
2555 * It issues a read-write ioctl given by
2556 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2558 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2561 drm_set_version_t sv;
2564 sv.drm_di_major = version->drm_di_major;
2565 sv.drm_di_minor = version->drm_di_minor;
2566 sv.drm_dd_major = version->drm_dd_major;
2567 sv.drm_dd_minor = version->drm_dd_minor;
2569 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2573 version->drm_di_major = sv.drm_di_major;
2574 version->drm_di_minor = sv.drm_di_minor;
2575 version->drm_dd_major = sv.drm_dd_major;
2576 version->drm_dd_minor = sv.drm_dd_minor;
2582 * Send a device-specific command.
2584 * \param fd file descriptor.
2585 * \param drmCommandIndex command index
2587 * \return zero on success, or a negative value on failure.
2590 * It issues a ioctl given by
2591 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2593 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2595 unsigned long request;
2597 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2599 if (drmIoctl(fd, request, NULL)) {
2607 * Send a device-specific read command.
2609 * \param fd file descriptor.
2610 * \param drmCommandIndex command index
2611 * \param data destination pointer of the data to be read.
2612 * \param size size of the data to be read.
2614 * \return zero on success, or a negative value on failure.
2617 * It issues a read ioctl given by
2618 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2620 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2621 void *data, unsigned long size)
2623 unsigned long request;
2625 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2626 DRM_COMMAND_BASE + drmCommandIndex, size);
2628 if (drmIoctl(fd, request, data)) {
2636 * Send a device-specific write command.
2638 * \param fd file descriptor.
2639 * \param drmCommandIndex command index
2640 * \param data source pointer of the data to be written.
2641 * \param size size of the data to be written.
2643 * \return zero on success, or a negative value on failure.
2646 * It issues a write ioctl given by
2647 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2649 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2650 void *data, unsigned long size)
2652 unsigned long request;
2654 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2655 DRM_COMMAND_BASE + drmCommandIndex, size);
2657 if (drmIoctl(fd, request, data)) {
2665 * Send a device-specific read-write command.
2667 * \param fd file descriptor.
2668 * \param drmCommandIndex command index
2669 * \param data source pointer of the data to be read and written.
2670 * \param size size of the data to be read and written.
2672 * \return zero on success, or a negative value on failure.
2675 * It issues a read-write ioctl given by
2676 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2678 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2679 void *data, unsigned long size)
2681 unsigned long request;
2683 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2684 DRM_COMMAND_BASE + drmCommandIndex, size);
2686 if (drmIoctl(fd, request, data))
2691 #define DRM_MAX_FDS 16
2697 } connection[DRM_MAX_FDS];
2699 static int nr_fds = 0;
2701 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2703 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2706 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2712 for (i = 0; i < nr_fds; i++)
2713 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2714 (connection[i].type == type)) {
2715 connection[i].refcount++;
2717 return connection[i].fd;
2720 fd = drmOpenWithType(NULL, BusID, type);
2721 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2724 connection[nr_fds].BusID = strdup(BusID);
2725 connection[nr_fds].fd = fd;
2726 connection[nr_fds].refcount = 1;
2727 connection[nr_fds].type = type;
2731 fprintf(stderr, "saved connection %d for %s %d\n",
2732 nr_fds, connection[nr_fds].BusID,
2733 strcmp(BusID, connection[nr_fds].BusID));
2740 drm_public void drmCloseOnce(int fd)
2744 for (i = 0; i < nr_fds; i++) {
2745 if (fd == connection[i].fd) {
2746 if (--connection[i].refcount == 0) {
2747 drmClose(connection[i].fd);
2748 free(connection[i].BusID);
2751 connection[i] = connection[nr_fds];
2759 drm_public int drmSetMaster(int fd)
2761 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2764 drm_public int drmDropMaster(int fd)
2766 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2769 drm_public int drmIsMaster(int fd)
2771 /* Detect master by attempting something that requires master.
2773 * Authenticating magic tokens requires master and 0 is an
2774 * internal kernel detail which we could use. Attempting this on
2775 * a master fd would fail therefore fail with EINVAL because 0
2778 * A non-master fd will fail with EACCES, as the kernel checks
2779 * for master before attempting to do anything else.
2781 * Since we don't want to leak implementation details, use
2784 return drmAuthMagic(fd, 0) != -EACCES;
2787 drm_public char *drmGetDeviceNameFromFd(int fd)
2794 /* The whole drmOpen thing is a fiasco and we need to find a way
2795 * back to just using open(2). For now, however, lets just make
2796 * things worse with even more ad hoc directory walking code to
2797 * discover the device file name. */
2802 for (i = 0; i < DRM_MAX_MINOR; i++) {
2803 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2804 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2807 if (i == DRM_MAX_MINOR)
2810 return strdup(name);
2813 static bool drmNodeIsDRM(int maj, int min)
2819 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2821 return stat(path, &sbuf) == 0;
2823 return maj == DRM_MAJOR;
2827 drm_public int drmGetNodeTypeFromFd(int fd)
2832 if (fstat(fd, &sbuf))
2835 maj = major(sbuf.st_rdev);
2836 min = minor(sbuf.st_rdev);
2838 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2843 type = drmGetMinorType(min);
2849 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2852 struct drm_prime_handle args;
2857 args.handle = handle;
2859 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2863 *prime_fd = args.fd;
2867 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2869 struct drm_prime_handle args;
2874 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2878 *handle = args.handle;
2882 static char *drmGetMinorNameForFD(int fd, int type)
2888 const char *name = drmGetMinorName(type);
2890 char dev_name[64], buf[64];
2898 if (fstat(fd, &sbuf))
2901 maj = major(sbuf.st_rdev);
2902 min = minor(sbuf.st_rdev);
2904 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2907 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2909 sysdir = opendir(buf);
2913 while ((ent = readdir(sysdir))) {
2914 if (strncmp(ent->d_name, name, len) == 0) {
2915 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2919 return strdup(dev_name);
2927 char buf[PATH_MAX + 1];
2928 const char *dev_name;
2929 unsigned int maj, min;
2932 if (fstat(fd, &sbuf))
2935 maj = major(sbuf.st_rdev);
2936 min = minor(sbuf.st_rdev);
2938 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2942 case DRM_NODE_PRIMARY:
2943 dev_name = DRM_DEV_NAME;
2945 case DRM_NODE_CONTROL:
2946 dev_name = DRM_CONTROL_DEV_NAME;
2948 case DRM_NODE_RENDER:
2949 dev_name = DRM_RENDER_DEV_NAME;
2955 base = drmGetMinorBase(type);
2959 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
2960 if (n == -1 || n >= sizeof(buf))
2967 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2969 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2972 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2974 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2978 static char * DRM_PRINTFLIKE(2, 3)
2979 sysfs_uevent_get(const char *path, const char *fmt, ...)
2981 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2982 size_t size = 0, len;
2988 num = vasprintf(&key, fmt, ap);
2992 snprintf(filename, sizeof(filename), "%s/uevent", path);
2994 fp = fopen(filename, "r");
3000 while ((num = getline(&line, &size, fp)) >= 0) {
3001 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3002 char *start = line + len + 1, *end = line + num - 1;
3007 value = strndup(start, end - start);
3021 /* Little white lie to avoid major rework of the existing code */
3022 #define DRM_BUS_VIRTIO 0x10
3024 static int drmParseSubsystemType(int maj, int min)
3027 char path[PATH_MAX + 1];
3028 char link[PATH_MAX + 1] = "";
3034 { "/pci", DRM_BUS_PCI },
3035 { "/usb", DRM_BUS_USB },
3036 { "/platform", DRM_BUS_PLATFORM },
3037 { "/spi", DRM_BUS_PLATFORM },
3038 { "/host1x", DRM_BUS_HOST1X },
3039 { "/virtio", DRM_BUS_VIRTIO },
3042 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
3045 if (readlink(path, link, PATH_MAX) < 0)
3048 name = strrchr(link, '/');
3052 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3053 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3054 return bus_types[i].bus_type;
3058 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3061 #warning "Missing implementation of drmParseSubsystemType"
3067 get_pci_path(int maj, int min, char *pci_path)
3069 char path[PATH_MAX + 1], *term;
3071 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3072 if (!realpath(path, pci_path)) {
3073 strcpy(pci_path, path);
3077 term = strrchr(pci_path, '/');
3078 if (term && strncmp(term, "/virtio", 7) == 0)
3082 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3085 unsigned int domain, bus, dev, func;
3086 char pci_path[PATH_MAX + 1], *value;
3089 get_pci_path(maj, min, pci_path);
3091 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3095 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3101 info->domain = domain;
3107 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3108 struct drm_pciinfo pinfo;
3111 type = drmGetMinorType(min);
3115 fd = drmOpenMinor(min, 0, type);
3119 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3125 info->domain = pinfo.domain;
3126 info->bus = pinfo.bus;
3127 info->dev = pinfo.dev;
3128 info->func = pinfo.func;
3132 #warning "Missing implementation of drmParsePciBusInfo"
3137 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3139 if (a == NULL || b == NULL)
3142 if (a->bustype != b->bustype)
3145 switch (a->bustype) {
3147 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3150 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3152 case DRM_BUS_PLATFORM:
3153 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3155 case DRM_BUS_HOST1X:
3156 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3165 static int drmGetNodeType(const char *name)
3167 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3168 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3169 return DRM_NODE_PRIMARY;
3171 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3172 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3173 return DRM_NODE_CONTROL;
3175 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3176 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3177 return DRM_NODE_RENDER;
3182 static int drmGetMaxNodeName(void)
3184 return sizeof(DRM_DIR_NAME) +
3185 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3186 sizeof(DRM_CONTROL_MINOR_NAME),
3187 sizeof(DRM_RENDER_MINOR_NAME)) +
3188 3 /* length of the node number */;
3192 static int parse_separate_sysfs_files(int maj, int min,
3193 drmPciDeviceInfoPtr device,
3194 bool ignore_revision)
3196 static const char *attrs[] = {
3197 "revision", /* Older kernels are missing the file, so check for it first */
3203 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3204 unsigned int data[ARRAY_SIZE(attrs)];
3208 get_pci_path(maj, min, pci_path);
3210 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3211 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3212 fp = fopen(path, "r");
3216 ret = fscanf(fp, "%x", &data[i]);
3223 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3224 device->vendor_id = data[1] & 0xffff;
3225 device->device_id = data[2] & 0xffff;
3226 device->subvendor_id = data[3] & 0xffff;
3227 device->subdevice_id = data[4] & 0xffff;
3232 static int parse_config_sysfs_file(int maj, int min,
3233 drmPciDeviceInfoPtr device)
3235 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3236 unsigned char config[64];
3239 get_pci_path(maj, min, pci_path);
3241 snprintf(path, PATH_MAX, "%s/config", pci_path);
3242 fd = open(path, O_RDONLY);
3246 ret = read(fd, config, sizeof(config));
3251 device->vendor_id = config[0] | (config[1] << 8);
3252 device->device_id = config[2] | (config[3] << 8);
3253 device->revision_id = config[8];
3254 device->subvendor_id = config[44] | (config[45] << 8);
3255 device->subdevice_id = config[46] | (config[47] << 8);
3261 static int drmParsePciDeviceInfo(int maj, int min,
3262 drmPciDeviceInfoPtr device,
3266 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3267 return parse_separate_sysfs_files(maj, min, device, true);
3269 if (parse_separate_sysfs_files(maj, min, device, false))
3270 return parse_config_sysfs_file(maj, min, device);
3273 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3274 struct drm_pciinfo pinfo;
3277 type = drmGetMinorType(min);
3281 fd = drmOpenMinor(min, 0, type);
3285 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3291 device->vendor_id = pinfo.vendor_id;
3292 device->device_id = pinfo.device_id;
3293 device->revision_id = pinfo.revision_id;
3294 device->subvendor_id = pinfo.subvendor_id;
3295 device->subdevice_id = pinfo.subdevice_id;
3299 #warning "Missing implementation of drmParsePciDeviceInfo"
3304 static void drmFreePlatformDevice(drmDevicePtr device)
3306 if (device->deviceinfo.platform) {
3307 if (device->deviceinfo.platform->compatible) {
3308 char **compatible = device->deviceinfo.platform->compatible;
3310 while (*compatible) {
3315 free(device->deviceinfo.platform->compatible);
3320 static void drmFreeHost1xDevice(drmDevicePtr device)
3322 if (device->deviceinfo.host1x) {
3323 if (device->deviceinfo.host1x->compatible) {
3324 char **compatible = device->deviceinfo.host1x->compatible;
3326 while (*compatible) {
3331 free(device->deviceinfo.host1x->compatible);
3336 drm_public void drmFreeDevice(drmDevicePtr *device)
3342 switch ((*device)->bustype) {
3343 case DRM_BUS_PLATFORM:
3344 drmFreePlatformDevice(*device);
3347 case DRM_BUS_HOST1X:
3348 drmFreeHost1xDevice(*device);
3357 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3361 if (devices == NULL)
3364 for (i = 0; i < count; i++)
3366 drmFreeDevice(&devices[i]);
3369 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3370 size_t bus_size, size_t device_size,
3373 size_t max_node_length, extra, size;
3374 drmDevicePtr device;
3378 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3379 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3381 size = sizeof(*device) + extra + bus_size + device_size;
3383 device = calloc(1, size);
3387 device->available_nodes = 1 << type;
3389 ptr = (char *)device + sizeof(*device);
3390 device->nodes = (char **)ptr;
3392 ptr += DRM_NODE_MAX * sizeof(void *);
3394 for (i = 0; i < DRM_NODE_MAX; i++) {
3395 device->nodes[i] = ptr;
3396 ptr += max_node_length;
3399 memcpy(device->nodes[type], node, max_node_length);
3406 static int drmProcessPciDevice(drmDevicePtr *device,
3407 const char *node, int node_type,
3408 int maj, int min, bool fetch_deviceinfo,
3415 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3416 sizeof(drmPciDeviceInfo), &addr);
3420 dev->bustype = DRM_BUS_PCI;
3422 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3424 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3428 // Fetch the device info if the user has requested it
3429 if (fetch_deviceinfo) {
3430 addr += sizeof(drmPciBusInfo);
3431 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3433 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3447 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3450 char path[PATH_MAX + 1], *value;
3451 unsigned int bus, dev;
3454 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3456 value = sysfs_uevent_get(path, "BUSNUM");
3460 ret = sscanf(value, "%03u", &bus);
3466 value = sysfs_uevent_get(path, "DEVNUM");
3470 ret = sscanf(value, "%03u", &dev);
3481 #warning "Missing implementation of drmParseUsbBusInfo"
3486 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3489 char path[PATH_MAX + 1], *value;
3490 unsigned int vendor, product;
3493 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3495 value = sysfs_uevent_get(path, "PRODUCT");
3499 ret = sscanf(value, "%x/%x", &vendor, &product);
3505 info->vendor = vendor;
3506 info->product = product;
3510 #warning "Missing implementation of drmParseUsbDeviceInfo"
3515 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3516 int node_type, int maj, int min,
3517 bool fetch_deviceinfo, uint32_t flags)
3523 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3524 sizeof(drmUsbDeviceInfo), &ptr);
3528 dev->bustype = DRM_BUS_USB;
3530 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3532 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3536 if (fetch_deviceinfo) {
3537 ptr += sizeof(drmUsbBusInfo);
3538 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3540 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3554 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3557 char path[PATH_MAX + 1], *name, *tmp_name;
3559 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3561 name = sysfs_uevent_get(path, "OF_FULLNAME");
3564 /* If the device lacks OF data, pick the MODALIAS info */
3565 name = sysfs_uevent_get(path, "MODALIAS");
3569 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3570 tmp_name = strrchr(name, ':');
3578 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3579 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3584 #warning "Missing implementation of drmParseOFBusInfo"
3589 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3592 char path[PATH_MAX + 1], *value, *tmp_name;
3593 unsigned int count, i;
3596 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3598 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3600 sscanf(value, "%u", &count);
3603 /* Assume one entry if the device lack OF data */
3607 *compatible = calloc(count + 1, sizeof(char *));
3611 for (i = 0; i < count; i++) {
3612 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3615 /* If the device lacks OF data, pick the MODALIAS info */
3616 value = sysfs_uevent_get(path, "MODALIAS");
3622 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3623 tmp_name = strrchr(value, ':');
3628 tmp_name = strdup(tmp_name + 1);
3632 (*compatible)[i] = tmp_name;
3639 free((*compatible)[i]);
3644 #warning "Missing implementation of drmParseOFDeviceInfo"
3649 static int drmProcessPlatformDevice(drmDevicePtr *device,
3650 const char *node, int node_type,
3651 int maj, int min, bool fetch_deviceinfo,
3658 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3659 sizeof(drmPlatformDeviceInfo), &ptr);
3663 dev->bustype = DRM_BUS_PLATFORM;
3665 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3667 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3671 if (fetch_deviceinfo) {
3672 ptr += sizeof(drmPlatformBusInfo);
3673 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3675 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3689 static int drmProcessHost1xDevice(drmDevicePtr *device,
3690 const char *node, int node_type,
3691 int maj, int min, bool fetch_deviceinfo,
3698 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3699 sizeof(drmHost1xDeviceInfo), &ptr);
3703 dev->bustype = DRM_BUS_HOST1X;
3705 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3707 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3711 if (fetch_deviceinfo) {
3712 ptr += sizeof(drmHost1xBusInfo);
3713 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3715 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3730 process_device(drmDevicePtr *device, const char *d_name,
3731 int req_subsystem_type,
3732 bool fetch_deviceinfo, uint32_t flags)
3735 char node[PATH_MAX + 1];
3736 int node_type, subsystem_type;
3737 unsigned int maj, min;
3739 node_type = drmGetNodeType(d_name);
3743 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3744 if (stat(node, &sbuf))
3747 maj = major(sbuf.st_rdev);
3748 min = minor(sbuf.st_rdev);
3750 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3753 subsystem_type = drmParseSubsystemType(maj, min);
3754 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3757 switch (subsystem_type) {
3759 case DRM_BUS_VIRTIO:
3760 return drmProcessPciDevice(device, node, node_type, maj, min,
3761 fetch_deviceinfo, flags);
3763 return drmProcessUsbDevice(device, node, node_type, maj, min,
3764 fetch_deviceinfo, flags);
3765 case DRM_BUS_PLATFORM:
3766 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3767 fetch_deviceinfo, flags);
3768 case DRM_BUS_HOST1X:
3769 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3770 fetch_deviceinfo, flags);
3776 /* Consider devices located on the same bus as duplicate and fold the respective
3777 * entries into a single one.
3779 * Note: this leaves "gaps" in the array, while preserving the length.
3781 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3783 int node_type, i, j;
3785 for (i = 0; i < count; i++) {
3786 for (j = i + 1; j < count; j++) {
3787 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3788 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3789 node_type = log2(local_devices[j]->available_nodes);
3790 memcpy(local_devices[i]->nodes[node_type],
3791 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3792 drmFreeDevice(&local_devices[j]);
3798 /* Check that the given flags are valid returning 0 on success */
3800 drm_device_validate_flags(uint32_t flags)
3802 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3806 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3810 for (int i = 0; i < DRM_NODE_MAX; i++) {
3811 if (device->available_nodes & 1 << i) {
3812 if (stat(device->nodes[i], &sbuf) == 0 &&
3813 sbuf.st_rdev == find_rdev)
3821 * The kernel drm core has a number of places that assume maximum of
3822 * 3x64 devices nodes. That's 64 for each of primary, control and
3823 * render nodes. Rounded it up to 256 for simplicity.
3825 #define MAX_DRM_NODES 256
3828 * Get information about the opened drm device
3830 * \param fd file descriptor of the drm device
3831 * \param flags feature/behaviour bitmask
3832 * \param device the address of a drmDevicePtr where the information
3833 * will be allocated in stored
3835 * \return zero on success, negative error code otherwise.
3837 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3838 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3840 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3844 * DRI device nodes on OpenBSD are not in their own directory, they reside
3845 * in /dev along with a large number of statically generated /dev nodes.
3846 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3850 char node[PATH_MAX + 1];
3851 const char *dev_name;
3852 int node_type, subsystem_type;
3853 int maj, min, n, ret, base;
3855 if (fd == -1 || device == NULL)
3858 if (fstat(fd, &sbuf))
3861 maj = major(sbuf.st_rdev);
3862 min = minor(sbuf.st_rdev);
3864 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3867 node_type = drmGetMinorType(min);
3868 if (node_type == -1)
3871 switch (node_type) {
3872 case DRM_NODE_PRIMARY:
3873 dev_name = DRM_DEV_NAME;
3875 case DRM_NODE_CONTROL:
3876 dev_name = DRM_CONTROL_DEV_NAME;
3878 case DRM_NODE_RENDER:
3879 dev_name = DRM_RENDER_DEV_NAME;
3885 base = drmGetMinorBase(node_type);
3889 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
3890 if (n == -1 || n >= PATH_MAX)
3892 if (stat(node, &sbuf))
3895 subsystem_type = drmParseSubsystemType(maj, min);
3896 if (subsystem_type != DRM_BUS_PCI)
3899 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3907 drmDevicePtr local_devices[MAX_DRM_NODES];
3910 struct dirent *dent;
3914 int ret, i, node_count;
3917 if (drm_device_validate_flags(flags))
3920 if (fd == -1 || device == NULL)
3923 if (fstat(fd, &sbuf))
3926 find_rdev = sbuf.st_rdev;
3927 maj = major(sbuf.st_rdev);
3928 min = minor(sbuf.st_rdev);
3930 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3933 subsystem_type = drmParseSubsystemType(maj, min);
3934 if (subsystem_type < 0)
3935 return subsystem_type;
3937 sysdir = opendir(DRM_DIR_NAME);
3942 while ((dent = readdir(sysdir))) {
3943 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3947 if (i >= MAX_DRM_NODES) {
3948 fprintf(stderr, "More than %d drm nodes detected. "
3949 "Please report a bug - that should not happen.\n"
3950 "Skipping extra nodes\n", MAX_DRM_NODES);
3953 local_devices[i] = d;
3958 drmFoldDuplicatedDevices(local_devices, node_count);
3962 for (i = 0; i < node_count; i++) {
3963 if (!local_devices[i])
3966 if (drm_device_has_rdev(local_devices[i], find_rdev))
3967 *device = local_devices[i];
3969 drmFreeDevice(&local_devices[i]);
3973 if (*device == NULL)
3980 * Get information about the opened drm device
3982 * \param fd file descriptor of the drm device
3983 * \param device the address of a drmDevicePtr where the information
3984 * will be allocated in stored
3986 * \return zero on success, negative error code otherwise.
3988 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3990 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3994 * Get drm devices on the system
3996 * \param flags feature/behaviour bitmask
3997 * \param devices the array of devices with drmDevicePtr elements
3998 * can be NULL to get the device number first
3999 * \param max_devices the maximum number of devices for the array
4001 * \return on error - negative error code,
4002 * if devices is NULL - total number of devices available on the system,
4003 * alternatively the number of devices stored in devices[], which is
4004 * capped by the max_devices.
4006 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4007 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4009 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4012 drmDevicePtr local_devices[MAX_DRM_NODES];
4013 drmDevicePtr device;
4015 struct dirent *dent;
4016 int ret, i, node_count, device_count;
4018 if (drm_device_validate_flags(flags))
4021 sysdir = opendir(DRM_DIR_NAME);
4026 while ((dent = readdir(sysdir))) {
4027 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4031 if (i >= MAX_DRM_NODES) {
4032 fprintf(stderr, "More than %d drm nodes detected. "
4033 "Please report a bug - that should not happen.\n"
4034 "Skipping extra nodes\n", MAX_DRM_NODES);
4037 local_devices[i] = device;
4042 drmFoldDuplicatedDevices(local_devices, node_count);
4045 for (i = 0; i < node_count; i++) {
4046 if (!local_devices[i])
4049 if ((devices != NULL) && (device_count < max_devices))
4050 devices[device_count] = local_devices[i];
4052 drmFreeDevice(&local_devices[i]);
4058 return device_count;
4062 * Get drm devices on the system
4064 * \param devices the array of devices with drmDevicePtr elements
4065 * can be NULL to get the device number first
4066 * \param max_devices the maximum number of devices for the array
4068 * \return on error - negative error code,
4069 * if devices is NULL - total number of devices available on the system,
4070 * alternatively the number of devices stored in devices[], which is
4071 * capped by the max_devices.
4073 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4075 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4078 drm_public char *drmGetDeviceNameFromFd2(int fd)
4082 char path[PATH_MAX + 1], *value;
4083 unsigned int maj, min;
4085 if (fstat(fd, &sbuf))
4088 maj = major(sbuf.st_rdev);
4089 min = minor(sbuf.st_rdev);
4091 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4094 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4096 value = sysfs_uevent_get(path, "DEVNAME");
4100 snprintf(path, sizeof(path), "/dev/%s", value);
4103 return strdup(path);
4106 char node[PATH_MAX + 1];
4107 const char *dev_name;
4109 int maj, min, n, base;
4111 if (fstat(fd, &sbuf))
4114 maj = major(sbuf.st_rdev);
4115 min = minor(sbuf.st_rdev);
4117 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4120 node_type = drmGetMinorType(min);
4121 if (node_type == -1)
4124 switch (node_type) {
4125 case DRM_NODE_PRIMARY:
4126 dev_name = DRM_DEV_NAME;
4128 case DRM_NODE_CONTROL:
4129 dev_name = DRM_CONTROL_DEV_NAME;
4131 case DRM_NODE_RENDER:
4132 dev_name = DRM_RENDER_DEV_NAME;
4138 base = drmGetMinorBase(node_type);
4142 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
4143 if (n == -1 || n >= PATH_MAX)
4146 return strdup(node);
4150 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4152 struct drm_syncobj_create args;
4158 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4161 *handle = args.handle;
4165 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4167 struct drm_syncobj_destroy args;
4170 args.handle = handle;
4171 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4174 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4176 struct drm_syncobj_handle args;
4181 args.handle = handle;
4182 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4189 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4191 struct drm_syncobj_handle args;
4197 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4200 *handle = args.handle;
4204 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4207 struct drm_syncobj_handle args;
4210 args.fd = sync_file_fd;
4211 args.handle = handle;
4212 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4213 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4216 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4219 struct drm_syncobj_handle args;
4224 args.handle = handle;
4225 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4226 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4229 *sync_file_fd = args.fd;
4233 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4234 int64_t timeout_nsec, unsigned flags,
4235 uint32_t *first_signaled)
4237 struct drm_syncobj_wait args;
4241 args.handles = (uintptr_t)handles;
4242 args.timeout_nsec = timeout_nsec;
4243 args.count_handles = num_handles;
4246 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4251 *first_signaled = args.first_signaled;
4255 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4256 uint32_t handle_count)
4258 struct drm_syncobj_array args;
4262 args.handles = (uintptr_t)handles;
4263 args.count_handles = handle_count;
4265 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4269 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4270 uint32_t handle_count)
4272 struct drm_syncobj_array args;
4276 args.handles = (uintptr_t)handles;
4277 args.count_handles = handle_count;
4279 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);