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 /* Not all systems have MAP_FAILED defined */
64 #define MAP_FAILED ((void *)-1)
68 #include "libdrm_macros.h"
70 #include "util_math.h"
77 #define DRM_PRIMARY_MINOR_NAME "drm"
78 #define DRM_CONTROL_MINOR_NAME "drmC"
79 #define DRM_RENDER_MINOR_NAME "drmR"
81 #define DRM_PRIMARY_MINOR_NAME "card"
82 #define DRM_CONTROL_MINOR_NAME "controlD"
83 #define DRM_RENDER_MINOR_NAME "renderD"
86 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
100 #endif /* __OpenBSD__ */
103 #define DRM_MAJOR 226 /* Linux */
114 uint16_t subvendor_id;
115 uint16_t subdevice_id;
119 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
122 #define DRM_MSG_VERBOSITY 3
124 #define memclear(s) memset(&s, 0, sizeof(s))
126 static drmServerInfoPtr drm_server_info;
128 drm_public void drmSetServerInfo(drmServerInfoPtr info)
130 drm_server_info = info;
134 * Output a message to stderr.
136 * \param format printf() like format string.
139 * This function is a wrapper around vfprintf().
142 static int DRM_PRINTFLIKE(1, 0)
143 drmDebugPrint(const char *format, va_list ap)
146 return __android_log_vprint(ANDROID_LOG_DEBUG, "libdrm", format, ap);
148 return vfprintf(stderr, format, ap);
153 drmMsg(const char *format, ...)
158 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
159 (drm_server_info && drm_server_info->debug_print))
162 va_start(ap, format);
163 if (drm_server_info) {
164 drm_server_info->debug_print(format,ap);
166 drmDebugPrint(format, ap);
172 static void *drmHashTable = NULL; /* Context switch callbacks */
174 drm_public void *drmGetHashTable(void)
179 drm_public void *drmMalloc(int size)
181 return calloc(1, size);
184 drm_public void drmFree(void *pt)
190 * Call ioctl, restarting if it is interupted
193 drmIoctl(int fd, unsigned long request, void *arg)
198 ret = ioctl(fd, request, arg);
199 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
203 static unsigned long drmGetKeyFromFd(int fd)
212 drm_public drmHashEntry *drmGetEntry(int fd)
214 unsigned long key = drmGetKeyFromFd(fd);
219 drmHashTable = drmHashCreate();
221 if (drmHashLookup(drmHashTable, key, &value)) {
222 entry = drmMalloc(sizeof(*entry));
225 entry->tagTable = drmHashCreate();
226 drmHashInsert(drmHashTable, key, entry);
234 * Compare two busid strings
239 * \return 1 if matched.
242 * This function compares two bus ID strings. It understands the older
243 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
244 * domain, b is bus, d is device, f is function.
246 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
248 /* First, check if the IDs are exactly the same */
249 if (strcasecmp(id1, id2) == 0)
252 /* Try to match old/new-style PCI bus IDs. */
253 if (strncasecmp(id1, "pci", 3) == 0) {
254 unsigned int o1, b1, d1, f1;
255 unsigned int o2, b2, d2, f2;
258 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
261 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
266 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
269 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
274 /* If domains aren't properly supported by the kernel interface,
275 * just ignore them, which sucks less than picking a totally random
276 * card with "open by name"
281 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
290 * Handles error checking for chown call.
292 * \param path to file.
293 * \param id of the new owner.
294 * \param id of the new group.
296 * \return zero if success or -1 if failure.
299 * Checks for failure. If failure was caused by signal call chown again.
300 * If any other failure happened then it will output error mesage using
304 static int chown_check_return(const char *path, uid_t owner, gid_t group)
309 rv = chown(path, owner, group);
310 } while (rv != 0 && errno == EINTR);
315 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
316 path, errno, strerror(errno));
322 * Open the DRM device, creating it if necessary.
324 * \param dev major and minor numbers of the device.
325 * \param minor minor number of the device.
327 * \return a file descriptor on success, or a negative value on error.
330 * Assembles the device name from \p minor and opens it, creating the device
331 * special file node with the major and minor numbers specified by \p dev and
332 * parent directory if necessary and was called by root.
334 static int drmOpenDevice(dev_t dev, int minor, int type)
337 const char *dev_name;
340 mode_t devmode = DRM_DEV_MODE, serv_mode;
343 int isroot = !geteuid();
344 uid_t user = DRM_DEV_UID;
345 gid_t group = DRM_DEV_GID;
349 case DRM_NODE_PRIMARY:
350 dev_name = DRM_DEV_NAME;
352 case DRM_NODE_CONTROL:
353 dev_name = DRM_CONTROL_DEV_NAME;
355 case DRM_NODE_RENDER:
356 dev_name = DRM_RENDER_DEV_NAME;
362 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
363 drmMsg("drmOpenDevice: node name is %s\n", buf);
365 if (drm_server_info && drm_server_info->get_perms) {
366 drm_server_info->get_perms(&serv_group, &serv_mode);
367 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
368 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
372 if (stat(DRM_DIR_NAME, &st)) {
374 return DRM_ERR_NOT_ROOT;
375 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
376 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
377 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
380 /* Check if the device node exists and create it if necessary. */
381 if (stat(buf, &st)) {
383 return DRM_ERR_NOT_ROOT;
385 mknod(buf, S_IFCHR | devmode, dev);
388 if (drm_server_info && drm_server_info->get_perms) {
389 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
390 chown_check_return(buf, user, group);
394 /* if we modprobed then wait for udev */
398 if (stat(DRM_DIR_NAME, &st)) {
402 if (udev_count == 50)
407 if (stat(buf, &st)) {
411 if (udev_count == 50)
418 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
419 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
420 fd, fd < 0 ? strerror(errno) : "OK");
425 /* Check if the device node is not what we expect it to be, and recreate it
426 * and try again if so.
428 if (st.st_rdev != dev) {
430 return DRM_ERR_NOT_ROOT;
432 mknod(buf, S_IFCHR | devmode, dev);
433 if (drm_server_info && drm_server_info->get_perms) {
434 chown_check_return(buf, user, group);
438 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
439 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
440 fd, fd < 0 ? strerror(errno) : "OK");
444 drmMsg("drmOpenDevice: Open failed\n");
452 * Open the DRM device
454 * \param minor device minor number.
455 * \param create allow to create the device if set.
457 * \return a file descriptor on success, or a negative value on error.
460 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
461 * name from \p minor and opens it.
463 static int drmOpenMinor(int minor, int create, int type)
467 const char *dev_name;
470 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
473 case DRM_NODE_PRIMARY:
474 dev_name = DRM_DEV_NAME;
476 case DRM_NODE_CONTROL:
477 dev_name = DRM_CONTROL_DEV_NAME;
479 case DRM_NODE_RENDER:
480 dev_name = DRM_RENDER_DEV_NAME;
486 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
487 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
494 * Determine whether the DRM kernel driver has been loaded.
496 * \return 1 if the DRM driver is loaded, 0 otherwise.
499 * Determine the presence of the kernel driver by attempting to open the 0
500 * minor and get version information. For backward compatibility with older
501 * Linux implementations, /proc/dri is also checked.
503 drm_public int drmAvailable(void)
505 drmVersionPtr version;
509 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
511 /* Try proc for backward Linux compatibility */
512 if (!access("/proc/dri/0", R_OK))
518 if ((version = drmGetVersion(fd))) {
520 drmFreeVersion(version);
527 static int drmGetMinorBase(int type)
530 case DRM_NODE_PRIMARY:
532 case DRM_NODE_CONTROL:
534 case DRM_NODE_RENDER:
541 static int drmGetMinorType(int minor)
543 int type = minor >> 6;
549 case DRM_NODE_PRIMARY:
550 case DRM_NODE_CONTROL:
551 case DRM_NODE_RENDER:
558 static const char *drmGetMinorName(int type)
561 case DRM_NODE_PRIMARY:
562 return DRM_PRIMARY_MINOR_NAME;
563 case DRM_NODE_CONTROL:
564 return DRM_CONTROL_MINOR_NAME;
565 case DRM_NODE_RENDER:
566 return DRM_RENDER_MINOR_NAME;
573 * Open the device by bus ID.
575 * \param busid bus ID.
576 * \param type device node type.
578 * \return a file descriptor on success, or a negative value on error.
581 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
582 * comparing the device bus ID with the one supplied.
584 * \sa drmOpenMinor() and drmGetBusid().
586 static int drmOpenByBusid(const char *busid, int type)
588 int i, pci_domain_ok = 1;
592 int base = drmGetMinorBase(type);
597 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
598 for (i = base; i < base + DRM_MAX_MINOR; i++) {
599 fd = drmOpenMinor(i, 1, type);
600 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
602 /* We need to try for 1.4 first for proper PCI domain support
603 * and if that fails, we know the kernel is busted
607 sv.drm_dd_major = -1; /* Don't care */
608 sv.drm_dd_minor = -1; /* Don't care */
609 if (drmSetInterfaceVersion(fd, &sv)) {
615 sv.drm_dd_major = -1; /* Don't care */
616 sv.drm_dd_minor = -1; /* Don't care */
617 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
618 drmSetInterfaceVersion(fd, &sv);
620 buf = drmGetBusid(fd);
621 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
622 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
636 * Open the device by name.
638 * \param name driver name.
639 * \param type the device node type.
641 * \return a file descriptor on success, or a negative value on error.
644 * This function opens the first minor number that matches the driver name and
645 * isn't already in use. If it's in use it then it will already have a bus ID
648 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
650 static int drmOpenByName(const char *name, int type)
654 drmVersionPtr version;
656 int base = drmGetMinorBase(type);
662 * Open the first minor number that matches the driver name and isn't
663 * already in use. If it's in use it will have a busid assigned already.
665 for (i = base; i < base + DRM_MAX_MINOR; i++) {
666 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
667 if ((version = drmGetVersion(fd))) {
668 if (!strcmp(version->name, name)) {
669 drmFreeVersion(version);
670 id = drmGetBusid(fd);
671 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
680 drmFreeVersion(version);
688 /* Backward-compatibility /proc support */
689 for (i = 0; i < 8; i++) {
690 char proc_name[64], buf[512];
691 char *driver, *pt, *devstring;
694 sprintf(proc_name, "/proc/dri/%d/name", i);
695 if ((fd = open(proc_name, 0, 0)) >= 0) {
696 retcode = read(fd, buf, sizeof(buf)-1);
699 buf[retcode-1] = '\0';
700 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
702 if (*pt) { /* Device is next */
704 if (!strcmp(driver, name)) { /* Match */
705 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
707 if (*pt) { /* Found busid */
708 return drmOpenByBusid(++pt, type);
709 } else { /* No busid */
710 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
724 * Open the DRM device.
726 * Looks up the specified name and bus ID, and opens the device found. The
727 * entry in /dev/dri is created if necessary and if called by root.
729 * \param name driver name. Not referenced if bus ID is supplied.
730 * \param busid bus ID. Zero if not known.
732 * \return a file descriptor on success, or a negative value on error.
735 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
738 drm_public int drmOpen(const char *name, const char *busid)
740 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
744 * Open the DRM device with specified type.
746 * Looks up the specified name and bus ID, and opens the device found. The
747 * entry in /dev/dri is created if necessary and if called by root.
749 * \param name driver name. Not referenced if bus ID is supplied.
750 * \param busid bus ID. Zero if not known.
751 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
753 * \return a file descriptor on success, or a negative value on error.
756 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
759 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
761 if (!drmAvailable() && name != NULL && drm_server_info &&
762 drm_server_info->load_module) {
763 /* try to load the kernel module */
764 if (!drm_server_info->load_module(name)) {
765 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
771 int fd = drmOpenByBusid(busid, type);
777 return drmOpenByName(name, type);
782 drm_public int drmOpenControl(int minor)
784 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
787 drm_public int drmOpenRender(int minor)
789 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
793 * Open the DRM device with specified type of specified framebuffer.
795 * Looks up the associated DRM device with specified type of the
796 * specified framebuffer and opens it.
798 * \param fb the index of framebuffer.
799 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
801 * \return a file descriptor on success, or a negative value on error.
804 drm_public int drmOpenByFB(int fb, int type)
810 const char *name = drmGetMinorName(type);
811 int fd = -1, len = strlen(name);
813 snprintf(buf, sizeof(buf), "/sys/class/graphics/fb%d/device/drm", fb);
814 sysdir = opendir(buf);
818 while ((ent = readdir(sysdir))) {
819 if (!strncmp(ent->d_name, name, len)) {
820 snprintf(buf, sizeof(buf), "%s/%s", DRM_DIR_NAME, ent->d_name);
821 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
829 #warning "Missing implementation of drmOpenByFB"
835 * Free the version information returned by drmGetVersion().
837 * \param v pointer to the version information.
840 * It frees the memory pointed by \p %v as well as all the non-null strings
843 drm_public void drmFreeVersion(drmVersionPtr v)
855 * Free the non-public version information returned by the kernel.
857 * \param v pointer to the version information.
860 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
861 * the non-null strings pointers in it.
863 static void drmFreeKernelVersion(drm_version_t *v)
875 * Copy version information.
877 * \param d destination pointer.
878 * \param s source pointer.
881 * Used by drmGetVersion() to translate the information returned by the ioctl
882 * interface in a private structure into the public structure counterpart.
884 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
886 d->version_major = s->version_major;
887 d->version_minor = s->version_minor;
888 d->version_patchlevel = s->version_patchlevel;
889 d->name_len = s->name_len;
890 d->name = strdup(s->name);
891 d->date_len = s->date_len;
892 d->date = strdup(s->date);
893 d->desc_len = s->desc_len;
894 d->desc = strdup(s->desc);
899 * Query the driver version information.
901 * \param fd file descriptor.
903 * \return pointer to a drmVersion structure which should be freed with
906 * \note Similar information is available via /proc/dri.
909 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
910 * first with zeros to get the string lengths, and then the actually strings.
911 * It also null-terminates them since they might not be already.
913 drm_public drmVersionPtr drmGetVersion(int fd)
915 drmVersionPtr retval;
916 drm_version_t *version = drmMalloc(sizeof(*version));
918 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
919 drmFreeKernelVersion(version);
923 if (version->name_len)
924 version->name = drmMalloc(version->name_len + 1);
925 if (version->date_len)
926 version->date = drmMalloc(version->date_len + 1);
927 if (version->desc_len)
928 version->desc = drmMalloc(version->desc_len + 1);
930 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
931 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
932 drmFreeKernelVersion(version);
936 /* The results might not be null-terminated strings, so terminate them. */
937 if (version->name_len) version->name[version->name_len] = '\0';
938 if (version->date_len) version->date[version->date_len] = '\0';
939 if (version->desc_len) version->desc[version->desc_len] = '\0';
941 retval = drmMalloc(sizeof(*retval));
942 drmCopyVersion(retval, version);
943 drmFreeKernelVersion(version);
949 * Get version information for the DRM user space library.
951 * This version number is driver independent.
953 * \param fd file descriptor.
955 * \return version information.
958 * This function allocates and fills a drm_version structure with a hard coded
961 drm_public drmVersionPtr drmGetLibVersion(int fd)
963 drm_version_t *version = drmMalloc(sizeof(*version));
966 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
967 * revision 1.0.x = original DRM interface with no drmGetLibVersion
968 * entry point and many drm<Device> extensions
969 * revision 1.1.x = added drmCommand entry points for device extensions
970 * added drmGetLibVersion to identify libdrm.a version
971 * revision 1.2.x = added drmSetInterfaceVersion
972 * modified drmOpen to handle both busid and name
973 * revision 1.3.x = added server + memory manager
975 version->version_major = 1;
976 version->version_minor = 3;
977 version->version_patchlevel = 0;
979 return (drmVersionPtr)version;
982 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
984 struct drm_get_cap cap;
988 cap.capability = capability;
990 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
998 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
1000 struct drm_set_client_cap cap;
1003 cap.capability = capability;
1006 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
1010 * Free the bus ID information.
1012 * \param busid bus ID information string as given by drmGetBusid().
1015 * This function is just frees the memory pointed by \p busid.
1017 drm_public void drmFreeBusid(const char *busid)
1019 drmFree((void *)busid);
1024 * Get the bus ID of the device.
1026 * \param fd file descriptor.
1028 * \return bus ID string.
1031 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
1032 * get the string length and data, passing the arguments in a drm_unique
1035 drm_public char *drmGetBusid(int fd)
1041 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
1043 u.unique = drmMalloc(u.unique_len + 1);
1044 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1048 u.unique[u.unique_len] = '\0';
1055 * Set the bus ID of the device.
1057 * \param fd file descriptor.
1058 * \param busid bus ID string.
1060 * \return zero on success, negative on failure.
1063 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1064 * the arguments in a drm_unique structure.
1066 drm_public int drmSetBusid(int fd, const char *busid)
1071 u.unique = (char *)busid;
1072 u.unique_len = strlen(busid);
1074 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1080 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1087 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1089 *magic = auth.magic;
1093 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1099 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1105 * Specifies a range of memory that is available for mapping by a
1108 * \param fd file descriptor.
1109 * \param offset usually the physical address. The actual meaning depends of
1110 * the \p type parameter. See below.
1111 * \param size of the memory in bytes.
1112 * \param type type of the memory to be mapped.
1113 * \param flags combination of several flags to modify the function actions.
1114 * \param handle will be set to a value that may be used as the offset
1115 * parameter for mmap().
1117 * \return zero on success or a negative value on error.
1119 * \par Mapping the frame buffer
1120 * For the frame buffer
1121 * - \p offset will be the physical address of the start of the frame buffer,
1122 * - \p size will be the size of the frame buffer in bytes, and
1123 * - \p type will be DRM_FRAME_BUFFER.
1126 * The area mapped will be uncached. If MTRR support is available in the
1127 * kernel, the frame buffer area will be set to write combining.
1129 * \par Mapping the MMIO register area
1130 * For the MMIO register area,
1131 * - \p offset will be the physical address of the start of the register area,
1132 * - \p size will be the size of the register area bytes, and
1133 * - \p type will be DRM_REGISTERS.
1135 * The area mapped will be uncached.
1137 * \par Mapping the SAREA
1139 * - \p offset will be ignored and should be set to zero,
1140 * - \p size will be the desired size of the SAREA in bytes,
1141 * - \p type will be DRM_SHM.
1144 * A shared memory area of the requested size will be created and locked in
1145 * kernel memory. This area may be mapped into client-space by using the handle
1148 * \note May only be called by root.
1151 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1152 * the arguments in a drm_map structure.
1154 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1155 drmMapFlags flags, drm_handle_t *handle)
1160 map.offset = offset;
1164 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1167 *handle = (drm_handle_t)(uintptr_t)map.handle;
1171 drm_public int drmRmMap(int fd, drm_handle_t handle)
1176 map.handle = (void *)(uintptr_t)handle;
1178 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1184 * Make buffers available for DMA transfers.
1186 * \param fd file descriptor.
1187 * \param count number of buffers.
1188 * \param size size of each buffer.
1189 * \param flags buffer allocation flags.
1190 * \param agp_offset offset in the AGP aperture
1192 * \return number of buffers allocated, negative on error.
1195 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1199 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1202 drm_buf_desc_t request;
1205 request.count = count;
1206 request.size = size;
1207 request.flags = flags;
1208 request.agp_start = agp_offset;
1210 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1212 return request.count;
1215 drm_public int drmMarkBufs(int fd, double low, double high)
1217 drm_buf_info_t info;
1222 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1228 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1231 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1232 int retval = -errno;
1237 for (i = 0; i < info.count; i++) {
1238 info.list[i].low_mark = low * info.list[i].count;
1239 info.list[i].high_mark = high * info.list[i].count;
1240 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1241 int retval = -errno;
1254 * \param fd file descriptor.
1255 * \param count number of buffers to free.
1256 * \param list list of buffers to be freed.
1258 * \return zero on success, or a negative value on failure.
1260 * \note This function is primarily used for debugging.
1263 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1264 * the arguments in a drm_buf_free structure.
1266 drm_public int drmFreeBufs(int fd, int count, int *list)
1268 drm_buf_free_t request;
1271 request.count = count;
1272 request.list = list;
1273 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1282 * \param fd file descriptor.
1285 * This function closes the file descriptor.
1287 drm_public int drmClose(int fd)
1289 unsigned long key = drmGetKeyFromFd(fd);
1290 drmHashEntry *entry = drmGetEntry(fd);
1292 drmHashDestroy(entry->tagTable);
1295 entry->tagTable = NULL;
1297 drmHashDelete(drmHashTable, key);
1305 * Map a region of memory.
1307 * \param fd file descriptor.
1308 * \param handle handle returned by drmAddMap().
1309 * \param size size in bytes. Must match the size used by drmAddMap().
1310 * \param address will contain the user-space virtual address where the mapping
1313 * \return zero on success, or a negative value on failure.
1316 * This function is a wrapper for mmap().
1318 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1319 drmAddressPtr address)
1321 static unsigned long pagesize_mask = 0;
1327 pagesize_mask = getpagesize() - 1;
1329 size = (size + pagesize_mask) & ~pagesize_mask;
1331 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1332 if (*address == MAP_FAILED)
1339 * Unmap mappings obtained with drmMap().
1341 * \param address address as given by drmMap().
1342 * \param size size in bytes. Must match the size used by drmMap().
1344 * \return zero on success, or a negative value on failure.
1347 * This function is a wrapper for munmap().
1349 drm_public int drmUnmap(drmAddress address, drmSize size)
1351 return drm_munmap(address, size);
1354 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1356 drm_buf_info_t info;
1357 drmBufInfoPtr retval;
1362 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1366 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1369 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1374 retval = drmMalloc(sizeof(*retval));
1375 retval->count = info.count;
1376 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1377 for (i = 0; i < info.count; i++) {
1378 retval->list[i].count = info.list[i].count;
1379 retval->list[i].size = info.list[i].size;
1380 retval->list[i].low_mark = info.list[i].low_mark;
1381 retval->list[i].high_mark = info.list[i].high_mark;
1390 * Map all DMA buffers into client-virtual space.
1392 * \param fd file descriptor.
1394 * \return a pointer to a ::drmBufMap structure.
1396 * \note The client may not use these buffers until obtaining buffer indices
1400 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1401 * information about the buffers in a drm_buf_map structure into the
1402 * client-visible data structures.
1404 drm_public drmBufMapPtr drmMapBufs(int fd)
1407 drmBufMapPtr retval;
1411 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1417 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1420 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1425 retval = drmMalloc(sizeof(*retval));
1426 retval->count = bufs.count;
1427 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1428 for (i = 0; i < bufs.count; i++) {
1429 retval->list[i].idx = bufs.list[i].idx;
1430 retval->list[i].total = bufs.list[i].total;
1431 retval->list[i].used = 0;
1432 retval->list[i].address = bufs.list[i].address;
1441 * Unmap buffers allocated with drmMapBufs().
1443 * \return zero on success, or negative value on failure.
1446 * Calls munmap() for every buffer stored in \p bufs and frees the
1447 * memory allocated by drmMapBufs().
1449 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1453 for (i = 0; i < bufs->count; i++) {
1454 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1457 drmFree(bufs->list);
1463 #define DRM_DMA_RETRY 16
1466 * Reserve DMA buffers.
1468 * \param fd file descriptor.
1471 * \return zero on success, or a negative value on failure.
1474 * Assemble the arguments into a drm_dma structure and keeps issuing the
1475 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1477 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1482 dma.context = request->context;
1483 dma.send_count = request->send_count;
1484 dma.send_indices = request->send_list;
1485 dma.send_sizes = request->send_sizes;
1486 dma.flags = request->flags;
1487 dma.request_count = request->request_count;
1488 dma.request_size = request->request_size;
1489 dma.request_indices = request->request_list;
1490 dma.request_sizes = request->request_sizes;
1491 dma.granted_count = 0;
1494 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1495 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1498 request->granted_count = dma.granted_count;
1507 * Obtain heavyweight hardware lock.
1509 * \param fd file descriptor.
1510 * \param context context.
1511 * \param flags flags that determine the sate of the hardware when the function
1514 * \return always zero.
1517 * This function translates the arguments into a drm_lock structure and issue
1518 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1520 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1525 lock.context = context;
1527 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1528 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1529 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1530 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1531 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1532 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1534 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1540 * Release the hardware lock.
1542 * \param fd file descriptor.
1543 * \param context context.
1545 * \return zero on success, or a negative value on failure.
1548 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1549 * argument in a drm_lock structure.
1551 drm_public int drmUnlock(int fd, drm_context_t context)
1556 lock.context = context;
1557 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1560 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1564 drm_context_t * retval;
1568 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1574 if (!(list = drmMalloc(res.count * sizeof(*list))))
1576 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1579 res.contexts = list;
1580 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1581 goto err_free_context;
1583 for (i = 0; i < res.count; i++)
1584 retval[i] = list[i].handle;
1597 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1605 * Used by the X server during GLXContext initialization. This causes
1606 * per-context kernel-level resources to be allocated.
1608 * \param fd file descriptor.
1609 * \param handle is set on success. To be used by the client when requesting DMA
1610 * dispatch with drmDMA().
1612 * \return zero on success, or a negative value on failure.
1614 * \note May only be called by root.
1617 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1618 * argument in a drm_ctx structure.
1620 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1625 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1627 *handle = ctx.handle;
1631 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1636 ctx.handle = context;
1637 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1642 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1643 drm_context_tFlags flags)
1648 * Context preserving means that no context switches are done between DMA
1649 * buffers from one context and the next. This is suitable for use in the
1650 * X server (which promises to maintain hardware context), or in the
1651 * client-side library when buffers are swapped on behalf of two threads.
1654 ctx.handle = context;
1655 if (flags & DRM_CONTEXT_PRESERVED)
1656 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1657 if (flags & DRM_CONTEXT_2DONLY)
1658 ctx.flags |= _DRM_CONTEXT_2DONLY;
1659 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1664 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1665 drm_context_tFlagsPtr flags)
1670 ctx.handle = context;
1671 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1674 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1675 *flags |= DRM_CONTEXT_PRESERVED;
1676 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1677 *flags |= DRM_CONTEXT_2DONLY;
1684 * Free any kernel-level resources allocated with drmCreateContext() associated
1687 * \param fd file descriptor.
1688 * \param handle handle given by drmCreateContext().
1690 * \return zero on success, or a negative value on failure.
1692 * \note May only be called by root.
1695 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1696 * argument in a drm_ctx structure.
1698 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1703 ctx.handle = handle;
1704 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1709 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1714 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1716 *handle = draw.handle;
1720 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1725 draw.handle = handle;
1726 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1731 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1732 drm_drawable_info_type_t type,
1733 unsigned int num, void *data)
1735 drm_update_draw_t update;
1738 update.handle = handle;
1741 update.data = (unsigned long long)(unsigned long)data;
1743 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1749 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1752 struct drm_crtc_get_sequence get_seq;
1756 get_seq.crtc_id = crtcId;
1757 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1762 *sequence = get_seq.sequence;
1764 *ns = get_seq.sequence_ns;
1768 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1770 uint64_t *sequence_queued,
1773 struct drm_crtc_queue_sequence queue_seq;
1776 memclear(queue_seq);
1777 queue_seq.crtc_id = crtcId;
1778 queue_seq.flags = flags;
1779 queue_seq.sequence = sequence;
1780 queue_seq.user_data = user_data;
1782 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1783 if (ret == 0 && sequence_queued)
1784 *sequence_queued = queue_seq.sequence;
1790 * Acquire the AGP device.
1792 * Must be called before any of the other AGP related calls.
1794 * \param fd file descriptor.
1796 * \return zero on success, or a negative value on failure.
1799 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1801 drm_public int drmAgpAcquire(int fd)
1803 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1810 * Release the AGP device.
1812 * \param fd file descriptor.
1814 * \return zero on success, or a negative value on failure.
1817 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1819 drm_public int drmAgpRelease(int fd)
1821 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1830 * \param fd file descriptor.
1831 * \param mode AGP mode.
1833 * \return zero on success, or a negative value on failure.
1836 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1837 * argument in a drm_agp_mode structure.
1839 drm_public int drmAgpEnable(int fd, unsigned long mode)
1845 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1852 * Allocate a chunk of AGP memory.
1854 * \param fd file descriptor.
1855 * \param size requested memory size in bytes. Will be rounded to page boundary.
1856 * \param type type of memory to allocate.
1857 * \param address if not zero, will be set to the physical address of the
1859 * \param handle on success will be set to a handle of the allocated memory.
1861 * \return zero on success, or a negative value on failure.
1864 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1865 * arguments in a drm_agp_buffer structure.
1867 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1868 unsigned long *address, drm_handle_t *handle)
1873 *handle = DRM_AGP_NO_HANDLE;
1876 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1879 *address = b.physical;
1886 * Free a chunk of AGP memory.
1888 * \param fd file descriptor.
1889 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1891 * \return zero on success, or a negative value on failure.
1894 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1895 * argument in a drm_agp_buffer structure.
1897 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1903 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1910 * Bind a chunk of AGP memory.
1912 * \param fd file descriptor.
1913 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1914 * \param offset offset in bytes. It will round to page boundary.
1916 * \return zero on success, or a negative value on failure.
1919 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1920 * argument in a drm_agp_binding structure.
1922 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1924 drm_agp_binding_t b;
1929 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1936 * Unbind a chunk of AGP memory.
1938 * \param fd file descriptor.
1939 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1941 * \return zero on success, or a negative value on failure.
1944 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1945 * the argument in a drm_agp_binding structure.
1947 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1949 drm_agp_binding_t b;
1953 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1960 * Get AGP driver major version number.
1962 * \param fd file descriptor.
1964 * \return major version number on success, or a negative value on failure..
1967 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1968 * necessary information in a drm_agp_info structure.
1970 drm_public int drmAgpVersionMajor(int fd)
1976 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1978 return i.agp_version_major;
1983 * Get AGP driver minor version number.
1985 * \param fd file descriptor.
1987 * \return minor version number on success, or a negative value on failure.
1990 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1991 * necessary information in a drm_agp_info structure.
1993 drm_public int drmAgpVersionMinor(int fd)
1999 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2001 return i.agp_version_minor;
2008 * \param fd file descriptor.
2010 * \return mode on success, or zero on failure.
2013 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2014 * necessary information in a drm_agp_info structure.
2016 drm_public unsigned long drmAgpGetMode(int fd)
2022 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2029 * Get AGP aperture base.
2031 * \param fd file descriptor.
2033 * \return aperture base on success, zero on failure.
2036 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2037 * necessary information in a drm_agp_info structure.
2039 drm_public unsigned long drmAgpBase(int fd)
2045 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2047 return i.aperture_base;
2052 * Get AGP aperture size.
2054 * \param fd file descriptor.
2056 * \return aperture size on success, zero on failure.
2059 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2060 * necessary information in a drm_agp_info structure.
2062 drm_public unsigned long drmAgpSize(int fd)
2068 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2070 return i.aperture_size;
2075 * Get used AGP memory.
2077 * \param fd file descriptor.
2079 * \return memory used on success, or zero on failure.
2082 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2083 * necessary information in a drm_agp_info structure.
2085 drm_public unsigned long drmAgpMemoryUsed(int fd)
2091 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2093 return i.memory_used;
2098 * Get available AGP memory.
2100 * \param fd file descriptor.
2102 * \return memory available on success, or zero on failure.
2105 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2106 * necessary information in a drm_agp_info structure.
2108 drm_public unsigned long drmAgpMemoryAvail(int fd)
2114 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2116 return i.memory_allowed;
2121 * Get hardware vendor ID.
2123 * \param fd file descriptor.
2125 * \return vendor ID on success, or zero on failure.
2128 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2129 * necessary information in a drm_agp_info structure.
2131 drm_public unsigned int drmAgpVendorId(int fd)
2137 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2144 * Get hardware device ID.
2146 * \param fd file descriptor.
2148 * \return zero on success, or zero on failure.
2151 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2152 * necessary information in a drm_agp_info structure.
2154 drm_public unsigned int drmAgpDeviceId(int fd)
2160 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2165 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2166 drm_handle_t *handle)
2168 drm_scatter_gather_t sg;
2174 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2176 *handle = sg.handle;
2180 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2182 drm_scatter_gather_t sg;
2186 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2194 * \param fd file descriptor.
2195 * \param vbl pointer to a drmVBlank structure.
2197 * \return zero on success, or a negative value on failure.
2200 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2202 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2204 struct timespec timeout, cur;
2207 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2209 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2215 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2216 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2217 if (ret && errno == EINTR) {
2218 clock_gettime(CLOCK_MONOTONIC, &cur);
2219 /* Timeout after 1s */
2220 if (cur.tv_sec > timeout.tv_sec + 1 ||
2221 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2228 } while (ret && errno == EINTR);
2234 drm_public int drmError(int err, const char *label)
2237 case DRM_ERR_NO_DEVICE:
2238 fprintf(stderr, "%s: no device\n", label);
2240 case DRM_ERR_NO_ACCESS:
2241 fprintf(stderr, "%s: no access\n", label);
2243 case DRM_ERR_NOT_ROOT:
2244 fprintf(stderr, "%s: not root\n", label);
2246 case DRM_ERR_INVALID:
2247 fprintf(stderr, "%s: invalid args\n", label);
2252 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2260 * Install IRQ handler.
2262 * \param fd file descriptor.
2263 * \param irq IRQ number.
2265 * \return zero on success, or a negative value on failure.
2268 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2269 * argument in a drm_control structure.
2271 drm_public int drmCtlInstHandler(int fd, int irq)
2276 ctl.func = DRM_INST_HANDLER;
2278 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2285 * Uninstall IRQ handler.
2287 * \param fd file descriptor.
2289 * \return zero on success, or a negative value on failure.
2292 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2293 * argument in a drm_control structure.
2295 drm_public int drmCtlUninstHandler(int fd)
2300 ctl.func = DRM_UNINST_HANDLER;
2302 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2307 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2312 lock.context = context;
2313 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2314 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2315 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2316 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2317 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2318 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2319 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2325 * Get IRQ from bus ID.
2327 * \param fd file descriptor.
2328 * \param busnum bus number.
2329 * \param devnum device number.
2330 * \param funcnum function number.
2332 * \return IRQ number on success, or a negative value on failure.
2335 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2336 * arguments in a drm_irq_busid structure.
2338 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2346 p.funcnum = funcnum;
2347 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2352 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2354 drmHashEntry *entry = drmGetEntry(fd);
2356 if (drmHashInsert(entry->tagTable, context, tag)) {
2357 drmHashDelete(entry->tagTable, context);
2358 drmHashInsert(entry->tagTable, context, tag);
2363 drm_public int drmDelContextTag(int fd, drm_context_t context)
2365 drmHashEntry *entry = drmGetEntry(fd);
2367 return drmHashDelete(entry->tagTable, context);
2370 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2372 drmHashEntry *entry = drmGetEntry(fd);
2375 if (drmHashLookup(entry->tagTable, context, &value))
2381 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2382 drm_handle_t handle)
2384 drm_ctx_priv_map_t map;
2387 map.ctx_id = ctx_id;
2388 map.handle = (void *)(uintptr_t)handle;
2390 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2395 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2396 drm_handle_t *handle)
2398 drm_ctx_priv_map_t map;
2401 map.ctx_id = ctx_id;
2403 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2406 *handle = (drm_handle_t)(uintptr_t)map.handle;
2411 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2412 drmMapType *type, drmMapFlags *flags,
2413 drm_handle_t *handle, int *mtrr)
2419 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2421 *offset = map.offset;
2425 *handle = (unsigned long)map.handle;
2430 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2431 unsigned long *magic, unsigned long *iocs)
2433 drm_client_t client;
2437 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2439 *auth = client.auth;
2442 *magic = client.magic;
2443 *iocs = client.iocs;
2447 drm_public int drmGetStats(int fd, drmStatsT *stats)
2453 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2457 memset(stats, 0, sizeof(*stats));
2458 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2462 stats->data[i].long_format = "%-20.20s"; \
2463 stats->data[i].rate_format = "%8.8s"; \
2464 stats->data[i].isvalue = 1; \
2465 stats->data[i].verbose = 0
2468 stats->data[i].long_format = "%-20.20s"; \
2469 stats->data[i].rate_format = "%5.5s"; \
2470 stats->data[i].isvalue = 0; \
2471 stats->data[i].mult_names = "kgm"; \
2472 stats->data[i].mult = 1000; \
2473 stats->data[i].verbose = 0
2476 stats->data[i].long_format = "%-20.20s"; \
2477 stats->data[i].rate_format = "%5.5s"; \
2478 stats->data[i].isvalue = 0; \
2479 stats->data[i].mult_names = "KGM"; \
2480 stats->data[i].mult = 1024; \
2481 stats->data[i].verbose = 0
2484 stats->count = s.count;
2485 for (i = 0; i < s.count; i++) {
2486 stats->data[i].value = s.data[i].value;
2487 switch (s.data[i].type) {
2488 case _DRM_STAT_LOCK:
2489 stats->data[i].long_name = "Lock";
2490 stats->data[i].rate_name = "Lock";
2493 case _DRM_STAT_OPENS:
2494 stats->data[i].long_name = "Opens";
2495 stats->data[i].rate_name = "O";
2497 stats->data[i].verbose = 1;
2499 case _DRM_STAT_CLOSES:
2500 stats->data[i].long_name = "Closes";
2501 stats->data[i].rate_name = "Lock";
2503 stats->data[i].verbose = 1;
2505 case _DRM_STAT_IOCTLS:
2506 stats->data[i].long_name = "Ioctls";
2507 stats->data[i].rate_name = "Ioc/s";
2510 case _DRM_STAT_LOCKS:
2511 stats->data[i].long_name = "Locks";
2512 stats->data[i].rate_name = "Lck/s";
2515 case _DRM_STAT_UNLOCKS:
2516 stats->data[i].long_name = "Unlocks";
2517 stats->data[i].rate_name = "Unl/s";
2521 stats->data[i].long_name = "IRQs";
2522 stats->data[i].rate_name = "IRQ/s";
2525 case _DRM_STAT_PRIMARY:
2526 stats->data[i].long_name = "Primary Bytes";
2527 stats->data[i].rate_name = "PB/s";
2530 case _DRM_STAT_SECONDARY:
2531 stats->data[i].long_name = "Secondary Bytes";
2532 stats->data[i].rate_name = "SB/s";
2536 stats->data[i].long_name = "DMA";
2537 stats->data[i].rate_name = "DMA/s";
2540 case _DRM_STAT_SPECIAL:
2541 stats->data[i].long_name = "Special DMA";
2542 stats->data[i].rate_name = "dma/s";
2545 case _DRM_STAT_MISSED:
2546 stats->data[i].long_name = "Miss";
2547 stats->data[i].rate_name = "Ms/s";
2550 case _DRM_STAT_VALUE:
2551 stats->data[i].long_name = "Value";
2552 stats->data[i].rate_name = "Value";
2555 case _DRM_STAT_BYTE:
2556 stats->data[i].long_name = "Bytes";
2557 stats->data[i].rate_name = "B/s";
2560 case _DRM_STAT_COUNT:
2562 stats->data[i].long_name = "Count";
2563 stats->data[i].rate_name = "Cnt/s";
2572 * Issue a set-version ioctl.
2574 * \param fd file descriptor.
2575 * \param drmCommandIndex command index
2576 * \param data source pointer of the data to be read and written.
2577 * \param size size of the data to be read and written.
2579 * \return zero on success, or a negative value on failure.
2582 * It issues a read-write ioctl given by
2583 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2585 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2588 drm_set_version_t sv;
2591 sv.drm_di_major = version->drm_di_major;
2592 sv.drm_di_minor = version->drm_di_minor;
2593 sv.drm_dd_major = version->drm_dd_major;
2594 sv.drm_dd_minor = version->drm_dd_minor;
2596 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2600 version->drm_di_major = sv.drm_di_major;
2601 version->drm_di_minor = sv.drm_di_minor;
2602 version->drm_dd_major = sv.drm_dd_major;
2603 version->drm_dd_minor = sv.drm_dd_minor;
2609 * Send a device-specific command.
2611 * \param fd file descriptor.
2612 * \param drmCommandIndex command index
2614 * \return zero on success, or a negative value on failure.
2617 * It issues a ioctl given by
2618 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2620 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2622 unsigned long request;
2624 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2626 if (drmIoctl(fd, request, NULL)) {
2634 * Send a device-specific read command.
2636 * \param fd file descriptor.
2637 * \param drmCommandIndex command index
2638 * \param data destination pointer of the data to be read.
2639 * \param size size of the data to be read.
2641 * \return zero on success, or a negative value on failure.
2644 * It issues a read ioctl given by
2645 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2647 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2648 void *data, unsigned long size)
2650 unsigned long request;
2652 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2653 DRM_COMMAND_BASE + drmCommandIndex, size);
2655 if (drmIoctl(fd, request, data)) {
2663 * Send a device-specific write command.
2665 * \param fd file descriptor.
2666 * \param drmCommandIndex command index
2667 * \param data source pointer of the data to be written.
2668 * \param size size of the data to be written.
2670 * \return zero on success, or a negative value on failure.
2673 * It issues a write ioctl given by
2674 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2676 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2677 void *data, unsigned long size)
2679 unsigned long request;
2681 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2682 DRM_COMMAND_BASE + drmCommandIndex, size);
2684 if (drmIoctl(fd, request, data)) {
2692 * Send a device-specific read-write command.
2694 * \param fd file descriptor.
2695 * \param drmCommandIndex command index
2696 * \param data source pointer of the data to be read and written.
2697 * \param size size of the data to be read and written.
2699 * \return zero on success, or a negative value on failure.
2702 * It issues a read-write ioctl given by
2703 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2705 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2706 void *data, unsigned long size)
2708 unsigned long request;
2710 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2711 DRM_COMMAND_BASE + drmCommandIndex, size);
2713 if (drmIoctl(fd, request, data))
2718 #define DRM_MAX_FDS 16
2724 } connection[DRM_MAX_FDS];
2726 static int nr_fds = 0;
2728 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2730 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2733 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2739 for (i = 0; i < nr_fds; i++)
2740 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2741 (connection[i].type == type)) {
2742 connection[i].refcount++;
2744 return connection[i].fd;
2747 fd = drmOpenWithType(NULL, BusID, type);
2748 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2751 connection[nr_fds].BusID = strdup(BusID);
2752 connection[nr_fds].fd = fd;
2753 connection[nr_fds].refcount = 1;
2754 connection[nr_fds].type = type;
2758 fprintf(stderr, "saved connection %d for %s %d\n",
2759 nr_fds, connection[nr_fds].BusID,
2760 strcmp(BusID, connection[nr_fds].BusID));
2767 drm_public void drmCloseOnce(int fd)
2771 for (i = 0; i < nr_fds; i++) {
2772 if (fd == connection[i].fd) {
2773 if (--connection[i].refcount == 0) {
2774 drmClose(connection[i].fd);
2775 free(connection[i].BusID);
2778 connection[i] = connection[nr_fds];
2786 drm_public int drmSetMaster(int fd)
2788 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2791 drm_public int drmDropMaster(int fd)
2793 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2796 drm_public char *drmGetDeviceNameFromFd(int fd)
2803 /* The whole drmOpen thing is a fiasco and we need to find a way
2804 * back to just using open(2). For now, however, lets just make
2805 * things worse with even more ad hoc directory walking code to
2806 * discover the device file name. */
2811 for (i = 0; i < DRM_MAX_MINOR; i++) {
2812 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2813 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2816 if (i == DRM_MAX_MINOR)
2819 return strdup(name);
2822 static bool drmNodeIsDRM(int maj, int min)
2828 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2830 return stat(path, &sbuf) == 0;
2832 return maj == DRM_MAJOR;
2836 drm_public int drmGetNodeTypeFromFd(int fd)
2841 if (fstat(fd, &sbuf))
2844 maj = major(sbuf.st_rdev);
2845 min = minor(sbuf.st_rdev);
2847 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2852 type = drmGetMinorType(min);
2858 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2861 struct drm_prime_handle args;
2866 args.handle = handle;
2868 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2872 *prime_fd = args.fd;
2876 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2878 struct drm_prime_handle args;
2883 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2887 *handle = args.handle;
2891 static char *drmGetMinorNameForFD(int fd, int type)
2897 const char *name = drmGetMinorName(type);
2899 char dev_name[64], buf[64];
2907 if (fstat(fd, &sbuf))
2910 maj = major(sbuf.st_rdev);
2911 min = minor(sbuf.st_rdev);
2913 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2916 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2918 sysdir = opendir(buf);
2922 while ((ent = readdir(sysdir))) {
2923 if (strncmp(ent->d_name, name, len) == 0) {
2924 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2928 return strdup(dev_name);
2936 char buf[PATH_MAX + 1];
2937 const char *dev_name;
2938 unsigned int maj, min;
2941 if (fstat(fd, &sbuf))
2944 maj = major(sbuf.st_rdev);
2945 min = minor(sbuf.st_rdev);
2947 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2951 case DRM_NODE_PRIMARY:
2952 dev_name = DRM_DEV_NAME;
2954 case DRM_NODE_CONTROL:
2955 dev_name = DRM_CONTROL_DEV_NAME;
2957 case DRM_NODE_RENDER:
2958 dev_name = DRM_RENDER_DEV_NAME;
2964 base = drmGetMinorBase(type);
2968 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min - base);
2969 if (n == -1 || n >= sizeof(buf))
2976 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2978 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2981 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2983 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2987 static char * DRM_PRINTFLIKE(2, 3)
2988 sysfs_uevent_get(const char *path, const char *fmt, ...)
2990 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2991 size_t size = 0, len;
2997 num = vasprintf(&key, fmt, ap);
3001 snprintf(filename, sizeof(filename), "%s/uevent", path);
3003 fp = fopen(filename, "r");
3009 while ((num = getline(&line, &size, fp)) >= 0) {
3010 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3011 char *start = line + len + 1, *end = line + num - 1;
3016 value = strndup(start, end - start);
3030 /* Little white lie to avoid major rework of the existing code */
3031 #define DRM_BUS_VIRTIO 0x10
3033 static int drmParseSubsystemType(int maj, int min)
3036 char path[PATH_MAX + 1];
3037 char link[PATH_MAX + 1] = "";
3040 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
3043 if (readlink(path, link, PATH_MAX) < 0)
3046 name = strrchr(link, '/');
3050 if (strncmp(name, "/pci", 4) == 0)
3053 if (strncmp(name, "/usb", 4) == 0)
3056 if (strncmp(name, "/platform", 9) == 0)
3057 return DRM_BUS_PLATFORM;
3059 if (strncmp(name, "/host1x", 7) == 0)
3060 return DRM_BUS_HOST1X;
3062 if (strncmp(name, "/virtio", 7) == 0)
3063 return DRM_BUS_VIRTIO;
3066 #elif defined(__OpenBSD__)
3069 #warning "Missing implementation of drmParseSubsystemType"
3075 get_pci_path(int maj, int min, char *pci_path)
3077 char path[PATH_MAX + 1], *term;
3079 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3080 if (!realpath(path, pci_path)) {
3081 strcpy(pci_path, path);
3085 term = strrchr(pci_path, '/');
3086 if (term && strncmp(term, "/virtio", 7) == 0)
3090 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3093 unsigned int domain, bus, dev, func;
3094 char pci_path[PATH_MAX + 1], *value;
3097 get_pci_path(maj, min, pci_path);
3099 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3103 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3109 info->domain = domain;
3115 #elif defined(__OpenBSD__)
3116 struct drm_pciinfo pinfo;
3119 type = drmGetMinorType(min);
3123 fd = drmOpenMinor(min, 0, type);
3127 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3133 info->domain = pinfo.domain;
3134 info->bus = pinfo.bus;
3135 info->dev = pinfo.dev;
3136 info->func = pinfo.func;
3140 #warning "Missing implementation of drmParsePciBusInfo"
3145 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3147 if (a == NULL || b == NULL)
3150 if (a->bustype != b->bustype)
3153 switch (a->bustype) {
3155 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3158 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3160 case DRM_BUS_PLATFORM:
3161 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3163 case DRM_BUS_HOST1X:
3164 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3173 static int drmGetNodeType(const char *name)
3175 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3176 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3177 return DRM_NODE_PRIMARY;
3179 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3180 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3181 return DRM_NODE_CONTROL;
3183 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3184 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3185 return DRM_NODE_RENDER;
3190 static int drmGetMaxNodeName(void)
3192 return sizeof(DRM_DIR_NAME) +
3193 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3194 sizeof(DRM_CONTROL_MINOR_NAME),
3195 sizeof(DRM_RENDER_MINOR_NAME)) +
3196 3 /* length of the node number */;
3200 static int parse_separate_sysfs_files(int maj, int min,
3201 drmPciDeviceInfoPtr device,
3202 bool ignore_revision)
3204 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
3205 static const char *attrs[] = {
3206 "revision", /* Older kernels are missing the file, so check for it first */
3212 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3213 unsigned int data[ARRAY_SIZE(attrs)];
3217 get_pci_path(maj, min, pci_path);
3219 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3220 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3221 fp = fopen(path, "r");
3225 ret = fscanf(fp, "%x", &data[i]);
3232 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3233 device->vendor_id = data[1] & 0xffff;
3234 device->device_id = data[2] & 0xffff;
3235 device->subvendor_id = data[3] & 0xffff;
3236 device->subdevice_id = data[4] & 0xffff;
3241 static int parse_config_sysfs_file(int maj, int min,
3242 drmPciDeviceInfoPtr device)
3244 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3245 unsigned char config[64];
3248 get_pci_path(maj, min, pci_path);
3250 snprintf(path, PATH_MAX, "%s/config", pci_path);
3251 fd = open(path, O_RDONLY);
3255 ret = read(fd, config, sizeof(config));
3260 device->vendor_id = config[0] | (config[1] << 8);
3261 device->device_id = config[2] | (config[3] << 8);
3262 device->revision_id = config[8];
3263 device->subvendor_id = config[44] | (config[45] << 8);
3264 device->subdevice_id = config[46] | (config[47] << 8);
3270 static int drmParsePciDeviceInfo(int maj, int min,
3271 drmPciDeviceInfoPtr device,
3275 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3276 return parse_separate_sysfs_files(maj, min, device, true);
3278 if (parse_separate_sysfs_files(maj, min, device, false))
3279 return parse_config_sysfs_file(maj, min, device);
3282 #elif defined(__OpenBSD__)
3283 struct drm_pciinfo pinfo;
3286 type = drmGetMinorType(min);
3290 fd = drmOpenMinor(min, 0, type);
3294 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3300 device->vendor_id = pinfo.vendor_id;
3301 device->device_id = pinfo.device_id;
3302 device->revision_id = pinfo.revision_id;
3303 device->subvendor_id = pinfo.subvendor_id;
3304 device->subdevice_id = pinfo.subdevice_id;
3308 #warning "Missing implementation of drmParsePciDeviceInfo"
3313 static void drmFreePlatformDevice(drmDevicePtr device)
3315 if (device->deviceinfo.platform) {
3316 if (device->deviceinfo.platform->compatible) {
3317 char **compatible = device->deviceinfo.platform->compatible;
3319 while (*compatible) {
3324 free(device->deviceinfo.platform->compatible);
3329 static void drmFreeHost1xDevice(drmDevicePtr device)
3331 if (device->deviceinfo.host1x) {
3332 if (device->deviceinfo.host1x->compatible) {
3333 char **compatible = device->deviceinfo.host1x->compatible;
3335 while (*compatible) {
3340 free(device->deviceinfo.host1x->compatible);
3345 drm_public void drmFreeDevice(drmDevicePtr *device)
3351 switch ((*device)->bustype) {
3352 case DRM_BUS_PLATFORM:
3353 drmFreePlatformDevice(*device);
3356 case DRM_BUS_HOST1X:
3357 drmFreeHost1xDevice(*device);
3366 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3370 if (devices == NULL)
3373 for (i = 0; i < count; i++)
3375 drmFreeDevice(&devices[i]);
3378 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3379 size_t bus_size, size_t device_size,
3382 size_t max_node_length, extra, size;
3383 drmDevicePtr device;
3387 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3388 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3390 size = sizeof(*device) + extra + bus_size + device_size;
3392 device = calloc(1, size);
3396 device->available_nodes = 1 << type;
3398 ptr = (char *)device + sizeof(*device);
3399 device->nodes = (char **)ptr;
3401 ptr += DRM_NODE_MAX * sizeof(void *);
3403 for (i = 0; i < DRM_NODE_MAX; i++) {
3404 device->nodes[i] = ptr;
3405 ptr += max_node_length;
3408 memcpy(device->nodes[type], node, max_node_length);
3415 static int drmProcessPciDevice(drmDevicePtr *device,
3416 const char *node, int node_type,
3417 int maj, int min, bool fetch_deviceinfo,
3424 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3425 sizeof(drmPciDeviceInfo), &addr);
3429 dev->bustype = DRM_BUS_PCI;
3431 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3433 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3437 // Fetch the device info if the user has requested it
3438 if (fetch_deviceinfo) {
3439 addr += sizeof(drmPciBusInfo);
3440 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3442 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3456 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3459 char path[PATH_MAX + 1], *value;
3460 unsigned int bus, dev;
3463 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3465 value = sysfs_uevent_get(path, "BUSNUM");
3469 ret = sscanf(value, "%03u", &bus);
3475 value = sysfs_uevent_get(path, "DEVNUM");
3479 ret = sscanf(value, "%03u", &dev);
3490 #warning "Missing implementation of drmParseUsbBusInfo"
3495 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3498 char path[PATH_MAX + 1], *value;
3499 unsigned int vendor, product;
3502 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3504 value = sysfs_uevent_get(path, "PRODUCT");
3508 ret = sscanf(value, "%x/%x", &vendor, &product);
3514 info->vendor = vendor;
3515 info->product = product;
3519 #warning "Missing implementation of drmParseUsbDeviceInfo"
3524 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3525 int node_type, int maj, int min,
3526 bool fetch_deviceinfo, uint32_t flags)
3532 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3533 sizeof(drmUsbDeviceInfo), &ptr);
3537 dev->bustype = DRM_BUS_USB;
3539 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3541 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3545 if (fetch_deviceinfo) {
3546 ptr += sizeof(drmUsbBusInfo);
3547 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3549 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3563 static int drmParsePlatformBusInfo(int maj, int min, drmPlatformBusInfoPtr info)
3566 char path[PATH_MAX + 1], *name;
3568 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3570 name = sysfs_uevent_get(path, "OF_FULLNAME");
3574 strncpy(info->fullname, name, DRM_PLATFORM_DEVICE_NAME_LEN);
3575 info->fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3580 #warning "Missing implementation of drmParsePlatformBusInfo"
3585 static int drmParsePlatformDeviceInfo(int maj, int min,
3586 drmPlatformDeviceInfoPtr info)
3589 char path[PATH_MAX + 1], *value;
3590 unsigned int count, i;
3593 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3595 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3599 sscanf(value, "%u", &count);
3602 info->compatible = calloc(count + 1, sizeof(*info->compatible));
3603 if (!info->compatible)
3606 for (i = 0; i < count; i++) {
3607 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3613 info->compatible[i] = value;
3620 free(info->compatible[i]);
3622 free(info->compatible);
3625 #warning "Missing implementation of drmParsePlatformDeviceInfo"
3630 static int drmProcessPlatformDevice(drmDevicePtr *device,
3631 const char *node, int node_type,
3632 int maj, int min, bool fetch_deviceinfo,
3639 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3640 sizeof(drmPlatformDeviceInfo), &ptr);
3644 dev->bustype = DRM_BUS_PLATFORM;
3646 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3648 ret = drmParsePlatformBusInfo(maj, min, dev->businfo.platform);
3652 if (fetch_deviceinfo) {
3653 ptr += sizeof(drmPlatformBusInfo);
3654 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3656 ret = drmParsePlatformDeviceInfo(maj, min, dev->deviceinfo.platform);
3670 static int drmParseHost1xBusInfo(int maj, int min, drmHost1xBusInfoPtr info)
3673 char path[PATH_MAX + 1], *name;
3675 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3677 name = sysfs_uevent_get(path, "OF_FULLNAME");
3681 strncpy(info->fullname, name, DRM_HOST1X_DEVICE_NAME_LEN);
3682 info->fullname[DRM_HOST1X_DEVICE_NAME_LEN - 1] = '\0';
3687 #warning "Missing implementation of drmParseHost1xBusInfo"
3692 static int drmParseHost1xDeviceInfo(int maj, int min,
3693 drmHost1xDeviceInfoPtr info)
3696 char path[PATH_MAX + 1], *value;
3697 unsigned int count, i;
3700 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3702 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3706 sscanf(value, "%u", &count);
3709 info->compatible = calloc(count + 1, sizeof(*info->compatible));
3710 if (!info->compatible)
3713 for (i = 0; i < count; i++) {
3714 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3720 info->compatible[i] = value;
3727 free(info->compatible[i]);
3729 free(info->compatible);
3732 #warning "Missing implementation of drmParseHost1xDeviceInfo"
3737 static int drmProcessHost1xDevice(drmDevicePtr *device,
3738 const char *node, int node_type,
3739 int maj, int min, bool fetch_deviceinfo,
3746 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3747 sizeof(drmHost1xDeviceInfo), &ptr);
3751 dev->bustype = DRM_BUS_HOST1X;
3753 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3755 ret = drmParseHost1xBusInfo(maj, min, dev->businfo.host1x);
3759 if (fetch_deviceinfo) {
3760 ptr += sizeof(drmHost1xBusInfo);
3761 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3763 ret = drmParseHost1xDeviceInfo(maj, min, dev->deviceinfo.host1x);
3778 process_device(drmDevicePtr *device, const char *d_name,
3779 int req_subsystem_type,
3780 bool fetch_deviceinfo, uint32_t flags)
3783 char node[PATH_MAX + 1];
3784 int node_type, subsystem_type;
3785 unsigned int maj, min;
3787 node_type = drmGetNodeType(d_name);
3791 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3792 if (stat(node, &sbuf))
3795 maj = major(sbuf.st_rdev);
3796 min = minor(sbuf.st_rdev);
3798 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3801 subsystem_type = drmParseSubsystemType(maj, min);
3802 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3805 switch (subsystem_type) {
3807 case DRM_BUS_VIRTIO:
3808 return drmProcessPciDevice(device, node, node_type, maj, min,
3809 fetch_deviceinfo, flags);
3811 return drmProcessUsbDevice(device, node, node_type, maj, min,
3812 fetch_deviceinfo, flags);
3813 case DRM_BUS_PLATFORM:
3814 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3815 fetch_deviceinfo, flags);
3816 case DRM_BUS_HOST1X:
3817 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3818 fetch_deviceinfo, flags);
3824 /* Consider devices located on the same bus as duplicate and fold the respective
3825 * entries into a single one.
3827 * Note: this leaves "gaps" in the array, while preserving the length.
3829 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3831 int node_type, i, j;
3833 for (i = 0; i < count; i++) {
3834 for (j = i + 1; j < count; j++) {
3835 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3836 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3837 node_type = log2(local_devices[j]->available_nodes);
3838 memcpy(local_devices[i]->nodes[node_type],
3839 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3840 drmFreeDevice(&local_devices[j]);
3846 /* Check that the given flags are valid returning 0 on success */
3848 drm_device_validate_flags(uint32_t flags)
3850 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3854 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3858 for (int i = 0; i < DRM_NODE_MAX; i++) {
3859 if (device->available_nodes & 1 << i) {
3860 if (stat(device->nodes[i], &sbuf) == 0 &&
3861 sbuf.st_rdev == find_rdev)
3869 * The kernel drm core has a number of places that assume maximum of
3870 * 3x64 devices nodes. That's 64 for each of primary, control and
3871 * render nodes. Rounded it up to 256 for simplicity.
3873 #define MAX_DRM_NODES 256
3876 * Get information about the opened drm device
3878 * \param fd file descriptor of the drm device
3879 * \param flags feature/behaviour bitmask
3880 * \param device the address of a drmDevicePtr where the information
3881 * will be allocated in stored
3883 * \return zero on success, negative error code otherwise.
3885 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3886 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3888 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3892 * DRI device nodes on OpenBSD are not in their own directory, they reside
3893 * in /dev along with a large number of statically generated /dev nodes.
3894 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3898 char node[PATH_MAX + 1];
3899 const char *dev_name;
3900 int node_type, subsystem_type;
3901 int maj, min, n, ret, base;
3903 if (fd == -1 || device == NULL)
3906 if (fstat(fd, &sbuf))
3909 maj = major(sbuf.st_rdev);
3910 min = minor(sbuf.st_rdev);
3912 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3915 node_type = drmGetMinorType(min);
3916 if (node_type == -1)
3919 switch (node_type) {
3920 case DRM_NODE_PRIMARY:
3921 dev_name = DRM_DEV_NAME;
3923 case DRM_NODE_CONTROL:
3924 dev_name = DRM_CONTROL_DEV_NAME;
3926 case DRM_NODE_RENDER:
3927 dev_name = DRM_RENDER_DEV_NAME;
3933 base = drmGetMinorBase(node_type);
3937 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
3938 if (n == -1 || n >= PATH_MAX)
3940 if (stat(node, &sbuf))
3943 subsystem_type = drmParseSubsystemType(maj, min);
3944 if (subsystem_type != DRM_BUS_PCI)
3947 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3955 drmDevicePtr local_devices[MAX_DRM_NODES];
3958 struct dirent *dent;
3962 int ret, i, node_count;
3965 if (drm_device_validate_flags(flags))
3968 if (fd == -1 || device == NULL)
3971 if (fstat(fd, &sbuf))
3974 find_rdev = sbuf.st_rdev;
3975 maj = major(sbuf.st_rdev);
3976 min = minor(sbuf.st_rdev);
3978 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3981 subsystem_type = drmParseSubsystemType(maj, min);
3982 if (subsystem_type < 0)
3983 return subsystem_type;
3985 sysdir = opendir(DRM_DIR_NAME);
3990 while ((dent = readdir(sysdir))) {
3991 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3995 if (i >= MAX_DRM_NODES) {
3996 fprintf(stderr, "More than %d drm nodes detected. "
3997 "Please report a bug - that should not happen.\n"
3998 "Skipping extra nodes\n", MAX_DRM_NODES);
4001 local_devices[i] = d;
4006 drmFoldDuplicatedDevices(local_devices, node_count);
4010 for (i = 0; i < node_count; i++) {
4011 if (!local_devices[i])
4014 if (drm_device_has_rdev(local_devices[i], find_rdev))
4015 *device = local_devices[i];
4017 drmFreeDevice(&local_devices[i]);
4021 if (*device == NULL)
4028 * Get information about the opened drm device
4030 * \param fd file descriptor of the drm device
4031 * \param device the address of a drmDevicePtr where the information
4032 * will be allocated in stored
4034 * \return zero on success, negative error code otherwise.
4036 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
4038 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
4042 * Get drm devices on the system
4044 * \param flags feature/behaviour bitmask
4045 * \param devices the array of devices with drmDevicePtr elements
4046 * can be NULL to get the device number first
4047 * \param max_devices the maximum number of devices for the array
4049 * \return on error - negative error code,
4050 * if devices is NULL - total number of devices available on the system,
4051 * alternatively the number of devices stored in devices[], which is
4052 * capped by the max_devices.
4054 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4055 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4057 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4060 drmDevicePtr local_devices[MAX_DRM_NODES];
4061 drmDevicePtr device;
4063 struct dirent *dent;
4064 int ret, i, node_count, device_count;
4066 if (drm_device_validate_flags(flags))
4069 sysdir = opendir(DRM_DIR_NAME);
4074 while ((dent = readdir(sysdir))) {
4075 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4079 if (i >= MAX_DRM_NODES) {
4080 fprintf(stderr, "More than %d drm nodes detected. "
4081 "Please report a bug - that should not happen.\n"
4082 "Skipping extra nodes\n", MAX_DRM_NODES);
4085 local_devices[i] = device;
4090 drmFoldDuplicatedDevices(local_devices, node_count);
4093 for (i = 0; i < node_count; i++) {
4094 if (!local_devices[i])
4097 if ((devices != NULL) && (device_count < max_devices))
4098 devices[device_count] = local_devices[i];
4100 drmFreeDevice(&local_devices[i]);
4106 return device_count;
4110 * Get drm devices on the system
4112 * \param devices the array of devices with drmDevicePtr elements
4113 * can be NULL to get the device number first
4114 * \param max_devices the maximum number of devices for the array
4116 * \return on error - negative error code,
4117 * if devices is NULL - total number of devices available on the system,
4118 * alternatively the number of devices stored in devices[], which is
4119 * capped by the max_devices.
4121 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4123 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4126 drm_public char *drmGetDeviceNameFromFd2(int fd)
4130 char path[PATH_MAX + 1], *value;
4131 unsigned int maj, min;
4133 if (fstat(fd, &sbuf))
4136 maj = major(sbuf.st_rdev);
4137 min = minor(sbuf.st_rdev);
4139 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4142 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4144 value = sysfs_uevent_get(path, "DEVNAME");
4148 snprintf(path, sizeof(path), "/dev/%s", value);
4151 return strdup(path);
4154 char node[PATH_MAX + 1];
4155 const char *dev_name;
4157 int maj, min, n, base;
4159 if (fstat(fd, &sbuf))
4162 maj = major(sbuf.st_rdev);
4163 min = minor(sbuf.st_rdev);
4165 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4168 node_type = drmGetMinorType(min);
4169 if (node_type == -1)
4172 switch (node_type) {
4173 case DRM_NODE_PRIMARY:
4174 dev_name = DRM_DEV_NAME;
4176 case DRM_NODE_CONTROL:
4177 dev_name = DRM_CONTROL_DEV_NAME;
4179 case DRM_NODE_RENDER:
4180 dev_name = DRM_RENDER_DEV_NAME;
4186 base = drmGetMinorBase(node_type);
4190 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min - base);
4191 if (n == -1 || n >= PATH_MAX)
4194 return strdup(node);
4198 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4200 struct drm_syncobj_create args;
4206 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4209 *handle = args.handle;
4213 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4215 struct drm_syncobj_destroy args;
4218 args.handle = handle;
4219 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4222 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4224 struct drm_syncobj_handle args;
4229 args.handle = handle;
4230 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4237 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4239 struct drm_syncobj_handle args;
4245 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4248 *handle = args.handle;
4252 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4255 struct drm_syncobj_handle args;
4258 args.fd = sync_file_fd;
4259 args.handle = handle;
4260 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4261 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4264 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4267 struct drm_syncobj_handle args;
4272 args.handle = handle;
4273 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4274 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4277 *sync_file_fd = args.fd;
4281 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4282 int64_t timeout_nsec, unsigned flags,
4283 uint32_t *first_signaled)
4285 struct drm_syncobj_wait args;
4289 args.handles = (uintptr_t)handles;
4290 args.timeout_nsec = timeout_nsec;
4291 args.count_handles = num_handles;
4294 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4299 *first_signaled = args.first_signaled;
4303 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4304 uint32_t handle_count)
4306 struct drm_syncobj_array args;
4310 args.handles = (uintptr_t)handles;
4311 args.count_handles = handle_count;
4313 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4317 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4318 uint32_t handle_count)
4320 struct drm_syncobj_array args;
4324 args.handles = (uintptr_t)handles;
4325 args.count_handles = handle_count;
4327 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);