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"
74 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
88 #endif /* __OpenBSD__ */
91 #define DRM_MAJOR 226 /* Linux */
94 #if defined(__OpenBSD__) || defined(__DragonFly__)
102 uint16_t subvendor_id;
103 uint16_t subdevice_id;
107 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
110 #define DRM_MSG_VERBOSITY 3
112 #define memclear(s) memset(&s, 0, sizeof(s))
114 static drmServerInfoPtr drm_server_info;
116 drm_public void drmSetServerInfo(drmServerInfoPtr info)
118 drm_server_info = info;
122 * Output a message to stderr.
124 * \param format printf() like format string.
127 * This function is a wrapper around vfprintf().
130 static int DRM_PRINTFLIKE(1, 0)
131 drmDebugPrint(const char *format, va_list ap)
133 return vfprintf(stderr, format, ap);
137 drmMsg(const char *format, ...)
141 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
142 (drm_server_info && drm_server_info->debug_print))
144 va_start(ap, format);
145 if (drm_server_info) {
146 drm_server_info->debug_print(format,ap);
148 drmDebugPrint(format, ap);
154 static void *drmHashTable = NULL; /* Context switch callbacks */
156 drm_public void *drmGetHashTable(void)
161 drm_public void *drmMalloc(int size)
163 return calloc(1, size);
166 drm_public void drmFree(void *pt)
172 * Call ioctl, restarting if it is interrupted
175 drmIoctl(int fd, unsigned long request, void *arg)
180 ret = ioctl(fd, request, arg);
181 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
185 static unsigned long drmGetKeyFromFd(int fd)
194 drm_public drmHashEntry *drmGetEntry(int fd)
196 unsigned long key = drmGetKeyFromFd(fd);
201 drmHashTable = drmHashCreate();
203 if (drmHashLookup(drmHashTable, key, &value)) {
204 entry = drmMalloc(sizeof(*entry));
207 entry->tagTable = drmHashCreate();
208 drmHashInsert(drmHashTable, key, entry);
216 * Compare two busid strings
221 * \return 1 if matched.
224 * This function compares two bus ID strings. It understands the older
225 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
226 * domain, b is bus, d is device, f is function.
228 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
230 /* First, check if the IDs are exactly the same */
231 if (strcasecmp(id1, id2) == 0)
234 /* Try to match old/new-style PCI bus IDs. */
235 if (strncasecmp(id1, "pci", 3) == 0) {
236 unsigned int o1, b1, d1, f1;
237 unsigned int o2, b2, d2, f2;
240 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
243 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
248 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
251 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
256 /* If domains aren't properly supported by the kernel interface,
257 * just ignore them, which sucks less than picking a totally random
258 * card with "open by name"
263 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
272 * Handles error checking for chown call.
274 * \param path to file.
275 * \param id of the new owner.
276 * \param id of the new group.
278 * \return zero if success or -1 if failure.
281 * Checks for failure. If failure was caused by signal call chown again.
282 * If any other failure happened then it will output error message using
286 static int chown_check_return(const char *path, uid_t owner, gid_t group)
291 rv = chown(path, owner, group);
292 } while (rv != 0 && errno == EINTR);
297 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
298 path, errno, strerror(errno));
304 * Open the DRM device, creating it if necessary.
306 * \param dev major and minor numbers of the device.
307 * \param minor minor number of the device.
309 * \return a file descriptor on success, or a negative value on error.
312 * Assembles the device name from \p minor and opens it, creating the device
313 * special file node with the major and minor numbers specified by \p dev and
314 * parent directory if necessary and was called by root.
316 static int drmOpenDevice(dev_t dev, int minor, int type)
319 const char *dev_name;
320 char buf[DRM_NODE_NAME_MAX];
322 mode_t devmode = DRM_DEV_MODE, serv_mode;
325 int isroot = !geteuid();
326 uid_t user = DRM_DEV_UID;
327 gid_t group = DRM_DEV_GID;
331 case DRM_NODE_PRIMARY:
332 dev_name = DRM_DEV_NAME;
334 case DRM_NODE_CONTROL:
335 dev_name = DRM_CONTROL_DEV_NAME;
337 case DRM_NODE_RENDER:
338 dev_name = DRM_RENDER_DEV_NAME;
344 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
345 drmMsg("drmOpenDevice: node name is %s\n", buf);
347 if (drm_server_info && drm_server_info->get_perms) {
348 drm_server_info->get_perms(&serv_group, &serv_mode);
349 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
350 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
354 if (stat(DRM_DIR_NAME, &st)) {
356 return DRM_ERR_NOT_ROOT;
357 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
358 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
359 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
362 /* Check if the device node exists and create it if necessary. */
363 if (stat(buf, &st)) {
365 return DRM_ERR_NOT_ROOT;
367 mknod(buf, S_IFCHR | devmode, dev);
370 if (drm_server_info && drm_server_info->get_perms) {
371 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
372 chown_check_return(buf, user, group);
376 /* if we modprobed then wait for udev */
380 if (stat(DRM_DIR_NAME, &st)) {
384 if (udev_count == 50)
389 if (stat(buf, &st)) {
393 if (udev_count == 50)
400 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
401 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
402 fd, fd < 0 ? strerror(errno) : "OK");
407 /* Check if the device node is not what we expect it to be, and recreate it
408 * and try again if so.
410 if (st.st_rdev != dev) {
412 return DRM_ERR_NOT_ROOT;
414 mknod(buf, S_IFCHR | devmode, dev);
415 if (drm_server_info && drm_server_info->get_perms) {
416 chown_check_return(buf, user, group);
420 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
421 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
422 fd, fd < 0 ? strerror(errno) : "OK");
426 drmMsg("drmOpenDevice: Open failed\n");
434 * Open the DRM device
436 * \param minor device minor number.
437 * \param create allow to create the device if set.
439 * \return a file descriptor on success, or a negative value on error.
442 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
443 * name from \p minor and opens it.
445 static int drmOpenMinor(int minor, int create, int type)
448 char buf[DRM_NODE_NAME_MAX];
449 const char *dev_name;
452 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
455 case DRM_NODE_PRIMARY:
456 dev_name = DRM_DEV_NAME;
458 case DRM_NODE_CONTROL:
459 dev_name = DRM_CONTROL_DEV_NAME;
461 case DRM_NODE_RENDER:
462 dev_name = DRM_RENDER_DEV_NAME;
468 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
469 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
476 * Determine whether the DRM kernel driver has been loaded.
478 * \return 1 if the DRM driver is loaded, 0 otherwise.
481 * Determine the presence of the kernel driver by attempting to open the 0
482 * minor and get version information. For backward compatibility with older
483 * Linux implementations, /proc/dri is also checked.
485 drm_public int drmAvailable(void)
487 drmVersionPtr version;
491 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
493 /* Try proc for backward Linux compatibility */
494 if (!access("/proc/dri/0", R_OK))
500 if ((version = drmGetVersion(fd))) {
502 drmFreeVersion(version);
509 static int drmGetMinorBase(int type)
512 case DRM_NODE_PRIMARY:
514 case DRM_NODE_CONTROL:
516 case DRM_NODE_RENDER:
523 static int drmGetMinorType(int minor)
525 int type = minor >> 6;
531 case DRM_NODE_PRIMARY:
532 case DRM_NODE_CONTROL:
533 case DRM_NODE_RENDER:
540 static const char *drmGetMinorName(int type)
543 case DRM_NODE_PRIMARY:
544 return DRM_PRIMARY_MINOR_NAME;
545 case DRM_NODE_CONTROL:
546 return DRM_CONTROL_MINOR_NAME;
547 case DRM_NODE_RENDER:
548 return DRM_RENDER_MINOR_NAME;
555 * Open the device by bus ID.
557 * \param busid bus ID.
558 * \param type device node type.
560 * \return a file descriptor on success, or a negative value on error.
563 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
564 * comparing the device bus ID with the one supplied.
566 * \sa drmOpenMinor() and drmGetBusid().
568 static int drmOpenByBusid(const char *busid, int type)
570 int i, pci_domain_ok = 1;
574 int base = drmGetMinorBase(type);
579 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
580 for (i = base; i < base + DRM_MAX_MINOR; i++) {
581 fd = drmOpenMinor(i, 1, type);
582 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
584 /* We need to try for 1.4 first for proper PCI domain support
585 * and if that fails, we know the kernel is busted
589 sv.drm_dd_major = -1; /* Don't care */
590 sv.drm_dd_minor = -1; /* Don't care */
591 if (drmSetInterfaceVersion(fd, &sv)) {
597 sv.drm_dd_major = -1; /* Don't care */
598 sv.drm_dd_minor = -1; /* Don't care */
599 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
600 drmSetInterfaceVersion(fd, &sv);
602 buf = drmGetBusid(fd);
603 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
604 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
618 * Open the device by name.
620 * \param name driver name.
621 * \param type the device node type.
623 * \return a file descriptor on success, or a negative value on error.
626 * This function opens the first minor number that matches the driver name and
627 * isn't already in use. If it's in use it then it will already have a bus ID
630 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
632 static int drmOpenByName(const char *name, int type)
636 drmVersionPtr version;
638 int base = drmGetMinorBase(type);
644 * Open the first minor number that matches the driver name and isn't
645 * already in use. If it's in use it will have a busid assigned already.
647 for (i = base; i < base + DRM_MAX_MINOR; i++) {
648 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
649 if ((version = drmGetVersion(fd))) {
650 if (!strcmp(version->name, name)) {
651 drmFreeVersion(version);
652 id = drmGetBusid(fd);
653 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
662 drmFreeVersion(version);
670 /* Backward-compatibility /proc support */
671 for (i = 0; i < 8; i++) {
672 char proc_name[64], buf[512];
673 char *driver, *pt, *devstring;
676 sprintf(proc_name, "/proc/dri/%d/name", i);
677 if ((fd = open(proc_name, 0, 0)) >= 0) {
678 retcode = read(fd, buf, sizeof(buf)-1);
681 buf[retcode-1] = '\0';
682 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
684 if (*pt) { /* Device is next */
686 if (!strcmp(driver, name)) { /* Match */
687 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
689 if (*pt) { /* Found busid */
690 return drmOpenByBusid(++pt, type);
691 } else { /* No busid */
692 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
706 * Open the DRM device.
708 * Looks up the specified name and bus ID, and opens the device found. The
709 * entry in /dev/dri is created if necessary and if called by root.
711 * \param name driver name. Not referenced if bus ID is supplied.
712 * \param busid bus ID. Zero if not known.
714 * \return a file descriptor on success, or a negative value on error.
717 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
720 drm_public int drmOpen(const char *name, const char *busid)
722 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
726 * Open the DRM device with specified type.
728 * Looks up the specified name and bus ID, and opens the device found. The
729 * entry in /dev/dri is created if necessary and if called by root.
731 * \param name driver name. Not referenced if bus ID is supplied.
732 * \param busid bus ID. Zero if not known.
733 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
735 * \return a file descriptor on success, or a negative value on error.
738 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
741 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
743 if (name != NULL && drm_server_info &&
744 drm_server_info->load_module && !drmAvailable()) {
745 /* try to load the kernel module */
746 if (!drm_server_info->load_module(name)) {
747 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
753 int fd = drmOpenByBusid(busid, type);
759 return drmOpenByName(name, type);
764 drm_public int drmOpenControl(int minor)
766 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
769 drm_public int drmOpenRender(int minor)
771 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
775 * Free the version information returned by drmGetVersion().
777 * \param v pointer to the version information.
780 * It frees the memory pointed by \p %v as well as all the non-null strings
783 drm_public void drmFreeVersion(drmVersionPtr v)
795 * Free the non-public version information returned by the kernel.
797 * \param v pointer to the version information.
800 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
801 * the non-null strings pointers in it.
803 static void drmFreeKernelVersion(drm_version_t *v)
815 * Copy version information.
817 * \param d destination pointer.
818 * \param s source pointer.
821 * Used by drmGetVersion() to translate the information returned by the ioctl
822 * interface in a private structure into the public structure counterpart.
824 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
826 d->version_major = s->version_major;
827 d->version_minor = s->version_minor;
828 d->version_patchlevel = s->version_patchlevel;
829 d->name_len = s->name_len;
830 d->name = strdup(s->name);
831 d->date_len = s->date_len;
832 d->date = strdup(s->date);
833 d->desc_len = s->desc_len;
834 d->desc = strdup(s->desc);
839 * Query the driver version information.
841 * \param fd file descriptor.
843 * \return pointer to a drmVersion structure which should be freed with
846 * \note Similar information is available via /proc/dri.
849 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
850 * first with zeros to get the string lengths, and then the actually strings.
851 * It also null-terminates them since they might not be already.
853 drm_public drmVersionPtr drmGetVersion(int fd)
855 drmVersionPtr retval;
856 drm_version_t *version = drmMalloc(sizeof(*version));
858 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
859 drmFreeKernelVersion(version);
863 if (version->name_len)
864 version->name = drmMalloc(version->name_len + 1);
865 if (version->date_len)
866 version->date = drmMalloc(version->date_len + 1);
867 if (version->desc_len)
868 version->desc = drmMalloc(version->desc_len + 1);
870 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
871 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
872 drmFreeKernelVersion(version);
876 /* The results might not be null-terminated strings, so terminate them. */
877 if (version->name_len) version->name[version->name_len] = '\0';
878 if (version->date_len) version->date[version->date_len] = '\0';
879 if (version->desc_len) version->desc[version->desc_len] = '\0';
881 retval = drmMalloc(sizeof(*retval));
882 drmCopyVersion(retval, version);
883 drmFreeKernelVersion(version);
889 * Get version information for the DRM user space library.
891 * This version number is driver independent.
893 * \param fd file descriptor.
895 * \return version information.
898 * This function allocates and fills a drm_version structure with a hard coded
901 drm_public drmVersionPtr drmGetLibVersion(int fd)
903 drm_version_t *version = drmMalloc(sizeof(*version));
906 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
907 * revision 1.0.x = original DRM interface with no drmGetLibVersion
908 * entry point and many drm<Device> extensions
909 * revision 1.1.x = added drmCommand entry points for device extensions
910 * added drmGetLibVersion to identify libdrm.a version
911 * revision 1.2.x = added drmSetInterfaceVersion
912 * modified drmOpen to handle both busid and name
913 * revision 1.3.x = added server + memory manager
915 version->version_major = 1;
916 version->version_minor = 3;
917 version->version_patchlevel = 0;
919 return (drmVersionPtr)version;
922 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
924 struct drm_get_cap cap;
928 cap.capability = capability;
930 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
938 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
940 struct drm_set_client_cap cap;
943 cap.capability = capability;
946 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
950 * Free the bus ID information.
952 * \param busid bus ID information string as given by drmGetBusid().
955 * This function is just frees the memory pointed by \p busid.
957 drm_public void drmFreeBusid(const char *busid)
959 drmFree((void *)busid);
964 * Get the bus ID of the device.
966 * \param fd file descriptor.
968 * \return bus ID string.
971 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
972 * get the string length and data, passing the arguments in a drm_unique
975 drm_public char *drmGetBusid(int fd)
981 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
983 u.unique = drmMalloc(u.unique_len + 1);
984 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
988 u.unique[u.unique_len] = '\0';
995 * Set the bus ID of the device.
997 * \param fd file descriptor.
998 * \param busid bus ID string.
1000 * \return zero on success, negative on failure.
1003 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1004 * the arguments in a drm_unique structure.
1006 drm_public int drmSetBusid(int fd, const char *busid)
1011 u.unique = (char *)busid;
1012 u.unique_len = strlen(busid);
1014 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1020 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1027 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1029 *magic = auth.magic;
1033 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1039 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1045 * Specifies a range of memory that is available for mapping by a
1048 * \param fd file descriptor.
1049 * \param offset usually the physical address. The actual meaning depends of
1050 * the \p type parameter. See below.
1051 * \param size of the memory in bytes.
1052 * \param type type of the memory to be mapped.
1053 * \param flags combination of several flags to modify the function actions.
1054 * \param handle will be set to a value that may be used as the offset
1055 * parameter for mmap().
1057 * \return zero on success or a negative value on error.
1059 * \par Mapping the frame buffer
1060 * For the frame buffer
1061 * - \p offset will be the physical address of the start of the frame buffer,
1062 * - \p size will be the size of the frame buffer in bytes, and
1063 * - \p type will be DRM_FRAME_BUFFER.
1066 * The area mapped will be uncached. If MTRR support is available in the
1067 * kernel, the frame buffer area will be set to write combining.
1069 * \par Mapping the MMIO register area
1070 * For the MMIO register area,
1071 * - \p offset will be the physical address of the start of the register area,
1072 * - \p size will be the size of the register area bytes, and
1073 * - \p type will be DRM_REGISTERS.
1075 * The area mapped will be uncached.
1077 * \par Mapping the SAREA
1079 * - \p offset will be ignored and should be set to zero,
1080 * - \p size will be the desired size of the SAREA in bytes,
1081 * - \p type will be DRM_SHM.
1084 * A shared memory area of the requested size will be created and locked in
1085 * kernel memory. This area may be mapped into client-space by using the handle
1088 * \note May only be called by root.
1091 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1092 * the arguments in a drm_map structure.
1094 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1095 drmMapFlags flags, drm_handle_t *handle)
1100 map.offset = offset;
1104 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1107 *handle = (drm_handle_t)(uintptr_t)map.handle;
1111 drm_public int drmRmMap(int fd, drm_handle_t handle)
1116 map.handle = (void *)(uintptr_t)handle;
1118 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1124 * Make buffers available for DMA transfers.
1126 * \param fd file descriptor.
1127 * \param count number of buffers.
1128 * \param size size of each buffer.
1129 * \param flags buffer allocation flags.
1130 * \param agp_offset offset in the AGP aperture
1132 * \return number of buffers allocated, negative on error.
1135 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1139 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1142 drm_buf_desc_t request;
1145 request.count = count;
1146 request.size = size;
1147 request.flags = flags;
1148 request.agp_start = agp_offset;
1150 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1152 return request.count;
1155 drm_public int drmMarkBufs(int fd, double low, double high)
1157 drm_buf_info_t info;
1162 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1168 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1171 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1172 int retval = -errno;
1177 for (i = 0; i < info.count; i++) {
1178 info.list[i].low_mark = low * info.list[i].count;
1179 info.list[i].high_mark = high * info.list[i].count;
1180 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1181 int retval = -errno;
1194 * \param fd file descriptor.
1195 * \param count number of buffers to free.
1196 * \param list list of buffers to be freed.
1198 * \return zero on success, or a negative value on failure.
1200 * \note This function is primarily used for debugging.
1203 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1204 * the arguments in a drm_buf_free structure.
1206 drm_public int drmFreeBufs(int fd, int count, int *list)
1208 drm_buf_free_t request;
1211 request.count = count;
1212 request.list = list;
1213 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1222 * \param fd file descriptor.
1225 * This function closes the file descriptor.
1227 drm_public int drmClose(int fd)
1229 unsigned long key = drmGetKeyFromFd(fd);
1230 drmHashEntry *entry = drmGetEntry(fd);
1232 drmHashDestroy(entry->tagTable);
1235 entry->tagTable = NULL;
1237 drmHashDelete(drmHashTable, key);
1245 * Map a region of memory.
1247 * \param fd file descriptor.
1248 * \param handle handle returned by drmAddMap().
1249 * \param size size in bytes. Must match the size used by drmAddMap().
1250 * \param address will contain the user-space virtual address where the mapping
1253 * \return zero on success, or a negative value on failure.
1256 * This function is a wrapper for mmap().
1258 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1259 drmAddressPtr address)
1261 static unsigned long pagesize_mask = 0;
1267 pagesize_mask = getpagesize() - 1;
1269 size = (size + pagesize_mask) & ~pagesize_mask;
1271 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1272 if (*address == MAP_FAILED)
1279 * Unmap mappings obtained with drmMap().
1281 * \param address address as given by drmMap().
1282 * \param size size in bytes. Must match the size used by drmMap().
1284 * \return zero on success, or a negative value on failure.
1287 * This function is a wrapper for munmap().
1289 drm_public int drmUnmap(drmAddress address, drmSize size)
1291 return drm_munmap(address, size);
1294 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1296 drm_buf_info_t info;
1297 drmBufInfoPtr retval;
1302 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1306 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1309 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1314 retval = drmMalloc(sizeof(*retval));
1315 retval->count = info.count;
1316 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1317 for (i = 0; i < info.count; i++) {
1318 retval->list[i].count = info.list[i].count;
1319 retval->list[i].size = info.list[i].size;
1320 retval->list[i].low_mark = info.list[i].low_mark;
1321 retval->list[i].high_mark = info.list[i].high_mark;
1330 * Map all DMA buffers into client-virtual space.
1332 * \param fd file descriptor.
1334 * \return a pointer to a ::drmBufMap structure.
1336 * \note The client may not use these buffers until obtaining buffer indices
1340 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1341 * information about the buffers in a drm_buf_map structure into the
1342 * client-visible data structures.
1344 drm_public drmBufMapPtr drmMapBufs(int fd)
1347 drmBufMapPtr retval;
1351 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1357 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1360 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1365 retval = drmMalloc(sizeof(*retval));
1366 retval->count = bufs.count;
1367 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1368 for (i = 0; i < bufs.count; i++) {
1369 retval->list[i].idx = bufs.list[i].idx;
1370 retval->list[i].total = bufs.list[i].total;
1371 retval->list[i].used = 0;
1372 retval->list[i].address = bufs.list[i].address;
1381 * Unmap buffers allocated with drmMapBufs().
1383 * \return zero on success, or negative value on failure.
1386 * Calls munmap() for every buffer stored in \p bufs and frees the
1387 * memory allocated by drmMapBufs().
1389 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1393 for (i = 0; i < bufs->count; i++) {
1394 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1397 drmFree(bufs->list);
1403 #define DRM_DMA_RETRY 16
1406 * Reserve DMA buffers.
1408 * \param fd file descriptor.
1411 * \return zero on success, or a negative value on failure.
1414 * Assemble the arguments into a drm_dma structure and keeps issuing the
1415 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1417 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1422 dma.context = request->context;
1423 dma.send_count = request->send_count;
1424 dma.send_indices = request->send_list;
1425 dma.send_sizes = request->send_sizes;
1426 dma.flags = request->flags;
1427 dma.request_count = request->request_count;
1428 dma.request_size = request->request_size;
1429 dma.request_indices = request->request_list;
1430 dma.request_sizes = request->request_sizes;
1431 dma.granted_count = 0;
1434 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1435 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1438 request->granted_count = dma.granted_count;
1447 * Obtain heavyweight hardware lock.
1449 * \param fd file descriptor.
1450 * \param context context.
1451 * \param flags flags that determine the state of the hardware when the function
1454 * \return always zero.
1457 * This function translates the arguments into a drm_lock structure and issue
1458 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1460 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1465 lock.context = context;
1467 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1468 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1469 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1470 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1471 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1472 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1474 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1480 * Release the hardware lock.
1482 * \param fd file descriptor.
1483 * \param context context.
1485 * \return zero on success, or a negative value on failure.
1488 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1489 * argument in a drm_lock structure.
1491 drm_public int drmUnlock(int fd, drm_context_t context)
1496 lock.context = context;
1497 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1500 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1504 drm_context_t * retval;
1508 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1514 if (!(list = drmMalloc(res.count * sizeof(*list))))
1516 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1519 res.contexts = list;
1520 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1521 goto err_free_context;
1523 for (i = 0; i < res.count; i++)
1524 retval[i] = list[i].handle;
1537 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1545 * Used by the X server during GLXContext initialization. This causes
1546 * per-context kernel-level resources to be allocated.
1548 * \param fd file descriptor.
1549 * \param handle is set on success. To be used by the client when requesting DMA
1550 * dispatch with drmDMA().
1552 * \return zero on success, or a negative value on failure.
1554 * \note May only be called by root.
1557 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1558 * argument in a drm_ctx structure.
1560 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1565 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1567 *handle = ctx.handle;
1571 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1576 ctx.handle = context;
1577 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1582 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1583 drm_context_tFlags flags)
1588 * Context preserving means that no context switches are done between DMA
1589 * buffers from one context and the next. This is suitable for use in the
1590 * X server (which promises to maintain hardware context), or in the
1591 * client-side library when buffers are swapped on behalf of two threads.
1594 ctx.handle = context;
1595 if (flags & DRM_CONTEXT_PRESERVED)
1596 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1597 if (flags & DRM_CONTEXT_2DONLY)
1598 ctx.flags |= _DRM_CONTEXT_2DONLY;
1599 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1604 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1605 drm_context_tFlagsPtr flags)
1610 ctx.handle = context;
1611 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1614 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1615 *flags |= DRM_CONTEXT_PRESERVED;
1616 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1617 *flags |= DRM_CONTEXT_2DONLY;
1624 * Free any kernel-level resources allocated with drmCreateContext() associated
1627 * \param fd file descriptor.
1628 * \param handle handle given by drmCreateContext().
1630 * \return zero on success, or a negative value on failure.
1632 * \note May only be called by root.
1635 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1636 * argument in a drm_ctx structure.
1638 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1643 ctx.handle = handle;
1644 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1649 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1654 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1656 *handle = draw.handle;
1660 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1665 draw.handle = handle;
1666 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1671 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1672 drm_drawable_info_type_t type,
1673 unsigned int num, void *data)
1675 drm_update_draw_t update;
1678 update.handle = handle;
1681 update.data = (unsigned long long)(unsigned long)data;
1683 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1689 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1692 struct drm_crtc_get_sequence get_seq;
1696 get_seq.crtc_id = crtcId;
1697 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1702 *sequence = get_seq.sequence;
1704 *ns = get_seq.sequence_ns;
1708 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1710 uint64_t *sequence_queued,
1713 struct drm_crtc_queue_sequence queue_seq;
1716 memclear(queue_seq);
1717 queue_seq.crtc_id = crtcId;
1718 queue_seq.flags = flags;
1719 queue_seq.sequence = sequence;
1720 queue_seq.user_data = user_data;
1722 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1723 if (ret == 0 && sequence_queued)
1724 *sequence_queued = queue_seq.sequence;
1730 * Acquire the AGP device.
1732 * Must be called before any of the other AGP related calls.
1734 * \param fd file descriptor.
1736 * \return zero on success, or a negative value on failure.
1739 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1741 drm_public int drmAgpAcquire(int fd)
1743 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1750 * Release the AGP device.
1752 * \param fd file descriptor.
1754 * \return zero on success, or a negative value on failure.
1757 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1759 drm_public int drmAgpRelease(int fd)
1761 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1770 * \param fd file descriptor.
1771 * \param mode AGP mode.
1773 * \return zero on success, or a negative value on failure.
1776 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1777 * argument in a drm_agp_mode structure.
1779 drm_public int drmAgpEnable(int fd, unsigned long mode)
1785 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1792 * Allocate a chunk of AGP memory.
1794 * \param fd file descriptor.
1795 * \param size requested memory size in bytes. Will be rounded to page boundary.
1796 * \param type type of memory to allocate.
1797 * \param address if not zero, will be set to the physical address of the
1799 * \param handle on success will be set to a handle of the allocated memory.
1801 * \return zero on success, or a negative value on failure.
1804 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1805 * arguments in a drm_agp_buffer structure.
1807 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1808 unsigned long *address, drm_handle_t *handle)
1813 *handle = DRM_AGP_NO_HANDLE;
1816 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1819 *address = b.physical;
1826 * Free a chunk of AGP memory.
1828 * \param fd file descriptor.
1829 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1831 * \return zero on success, or a negative value on failure.
1834 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1835 * argument in a drm_agp_buffer structure.
1837 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1843 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1850 * Bind a chunk of AGP memory.
1852 * \param fd file descriptor.
1853 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1854 * \param offset offset in bytes. It will round to page boundary.
1856 * \return zero on success, or a negative value on failure.
1859 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1860 * argument in a drm_agp_binding structure.
1862 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1864 drm_agp_binding_t b;
1869 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1876 * Unbind a chunk of AGP memory.
1878 * \param fd file descriptor.
1879 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1881 * \return zero on success, or a negative value on failure.
1884 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1885 * the argument in a drm_agp_binding structure.
1887 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1889 drm_agp_binding_t b;
1893 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1900 * Get AGP driver major version number.
1902 * \param fd file descriptor.
1904 * \return major version number on success, or a negative value on failure..
1907 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1908 * necessary information in a drm_agp_info structure.
1910 drm_public int drmAgpVersionMajor(int fd)
1916 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1918 return i.agp_version_major;
1923 * Get AGP driver minor version number.
1925 * \param fd file descriptor.
1927 * \return minor version number on success, or a negative value on failure.
1930 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1931 * necessary information in a drm_agp_info structure.
1933 drm_public int drmAgpVersionMinor(int fd)
1939 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1941 return i.agp_version_minor;
1948 * \param fd file descriptor.
1950 * \return mode on success, or zero on failure.
1953 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1954 * necessary information in a drm_agp_info structure.
1956 drm_public unsigned long drmAgpGetMode(int fd)
1962 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1969 * Get AGP aperture base.
1971 * \param fd file descriptor.
1973 * \return aperture base on success, zero on failure.
1976 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1977 * necessary information in a drm_agp_info structure.
1979 drm_public unsigned long drmAgpBase(int fd)
1985 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1987 return i.aperture_base;
1992 * Get AGP aperture size.
1994 * \param fd file descriptor.
1996 * \return aperture size on success, zero on failure.
1999 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2000 * necessary information in a drm_agp_info structure.
2002 drm_public unsigned long drmAgpSize(int fd)
2008 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2010 return i.aperture_size;
2015 * Get used AGP memory.
2017 * \param fd file descriptor.
2019 * \return memory used on success, or zero on failure.
2022 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2023 * necessary information in a drm_agp_info structure.
2025 drm_public unsigned long drmAgpMemoryUsed(int fd)
2031 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2033 return i.memory_used;
2038 * Get available AGP memory.
2040 * \param fd file descriptor.
2042 * \return memory available on success, or zero on failure.
2045 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2046 * necessary information in a drm_agp_info structure.
2048 drm_public unsigned long drmAgpMemoryAvail(int fd)
2054 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2056 return i.memory_allowed;
2061 * Get hardware vendor ID.
2063 * \param fd file descriptor.
2065 * \return vendor ID on success, or zero on failure.
2068 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2069 * necessary information in a drm_agp_info structure.
2071 drm_public unsigned int drmAgpVendorId(int fd)
2077 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2084 * Get hardware device ID.
2086 * \param fd file descriptor.
2088 * \return zero on success, or zero on failure.
2091 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2092 * necessary information in a drm_agp_info structure.
2094 drm_public unsigned int drmAgpDeviceId(int fd)
2100 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2105 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2106 drm_handle_t *handle)
2108 drm_scatter_gather_t sg;
2114 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2116 *handle = sg.handle;
2120 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2122 drm_scatter_gather_t sg;
2126 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2134 * \param fd file descriptor.
2135 * \param vbl pointer to a drmVBlank structure.
2137 * \return zero on success, or a negative value on failure.
2140 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2142 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2144 struct timespec timeout, cur;
2147 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2149 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2155 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2156 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2157 if (ret && errno == EINTR) {
2158 clock_gettime(CLOCK_MONOTONIC, &cur);
2159 /* Timeout after 1s */
2160 if (cur.tv_sec > timeout.tv_sec + 1 ||
2161 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2168 } while (ret && errno == EINTR);
2174 drm_public int drmError(int err, const char *label)
2177 case DRM_ERR_NO_DEVICE:
2178 fprintf(stderr, "%s: no device\n", label);
2180 case DRM_ERR_NO_ACCESS:
2181 fprintf(stderr, "%s: no access\n", label);
2183 case DRM_ERR_NOT_ROOT:
2184 fprintf(stderr, "%s: not root\n", label);
2186 case DRM_ERR_INVALID:
2187 fprintf(stderr, "%s: invalid args\n", label);
2192 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2200 * Install IRQ handler.
2202 * \param fd file descriptor.
2203 * \param irq IRQ number.
2205 * \return zero on success, or a negative value on failure.
2208 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2209 * argument in a drm_control structure.
2211 drm_public int drmCtlInstHandler(int fd, int irq)
2216 ctl.func = DRM_INST_HANDLER;
2218 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2225 * Uninstall IRQ handler.
2227 * \param fd file descriptor.
2229 * \return zero on success, or a negative value on failure.
2232 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2233 * argument in a drm_control structure.
2235 drm_public int drmCtlUninstHandler(int fd)
2240 ctl.func = DRM_UNINST_HANDLER;
2242 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2247 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2252 lock.context = context;
2253 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2254 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2255 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2256 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2257 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2258 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2259 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2265 * Get IRQ from bus ID.
2267 * \param fd file descriptor.
2268 * \param busnum bus number.
2269 * \param devnum device number.
2270 * \param funcnum function number.
2272 * \return IRQ number on success, or a negative value on failure.
2275 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2276 * arguments in a drm_irq_busid structure.
2278 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2286 p.funcnum = funcnum;
2287 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2292 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2294 drmHashEntry *entry = drmGetEntry(fd);
2296 if (drmHashInsert(entry->tagTable, context, tag)) {
2297 drmHashDelete(entry->tagTable, context);
2298 drmHashInsert(entry->tagTable, context, tag);
2303 drm_public int drmDelContextTag(int fd, drm_context_t context)
2305 drmHashEntry *entry = drmGetEntry(fd);
2307 return drmHashDelete(entry->tagTable, context);
2310 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2312 drmHashEntry *entry = drmGetEntry(fd);
2315 if (drmHashLookup(entry->tagTable, context, &value))
2321 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2322 drm_handle_t handle)
2324 drm_ctx_priv_map_t map;
2327 map.ctx_id = ctx_id;
2328 map.handle = (void *)(uintptr_t)handle;
2330 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2335 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2336 drm_handle_t *handle)
2338 drm_ctx_priv_map_t map;
2341 map.ctx_id = ctx_id;
2343 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2346 *handle = (drm_handle_t)(uintptr_t)map.handle;
2351 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2352 drmMapType *type, drmMapFlags *flags,
2353 drm_handle_t *handle, int *mtrr)
2359 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2361 *offset = map.offset;
2365 *handle = (unsigned long)map.handle;
2370 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2371 unsigned long *magic, unsigned long *iocs)
2373 drm_client_t client;
2377 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2379 *auth = client.auth;
2382 *magic = client.magic;
2383 *iocs = client.iocs;
2387 drm_public int drmGetStats(int fd, drmStatsT *stats)
2393 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2397 memset(stats, 0, sizeof(*stats));
2398 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2402 stats->data[i].long_format = "%-20.20s"; \
2403 stats->data[i].rate_format = "%8.8s"; \
2404 stats->data[i].isvalue = 1; \
2405 stats->data[i].verbose = 0
2408 stats->data[i].long_format = "%-20.20s"; \
2409 stats->data[i].rate_format = "%5.5s"; \
2410 stats->data[i].isvalue = 0; \
2411 stats->data[i].mult_names = "kgm"; \
2412 stats->data[i].mult = 1000; \
2413 stats->data[i].verbose = 0
2416 stats->data[i].long_format = "%-20.20s"; \
2417 stats->data[i].rate_format = "%5.5s"; \
2418 stats->data[i].isvalue = 0; \
2419 stats->data[i].mult_names = "KGM"; \
2420 stats->data[i].mult = 1024; \
2421 stats->data[i].verbose = 0
2424 stats->count = s.count;
2425 for (i = 0; i < s.count; i++) {
2426 stats->data[i].value = s.data[i].value;
2427 switch (s.data[i].type) {
2428 case _DRM_STAT_LOCK:
2429 stats->data[i].long_name = "Lock";
2430 stats->data[i].rate_name = "Lock";
2433 case _DRM_STAT_OPENS:
2434 stats->data[i].long_name = "Opens";
2435 stats->data[i].rate_name = "O";
2437 stats->data[i].verbose = 1;
2439 case _DRM_STAT_CLOSES:
2440 stats->data[i].long_name = "Closes";
2441 stats->data[i].rate_name = "Lock";
2443 stats->data[i].verbose = 1;
2445 case _DRM_STAT_IOCTLS:
2446 stats->data[i].long_name = "Ioctls";
2447 stats->data[i].rate_name = "Ioc/s";
2450 case _DRM_STAT_LOCKS:
2451 stats->data[i].long_name = "Locks";
2452 stats->data[i].rate_name = "Lck/s";
2455 case _DRM_STAT_UNLOCKS:
2456 stats->data[i].long_name = "Unlocks";
2457 stats->data[i].rate_name = "Unl/s";
2461 stats->data[i].long_name = "IRQs";
2462 stats->data[i].rate_name = "IRQ/s";
2465 case _DRM_STAT_PRIMARY:
2466 stats->data[i].long_name = "Primary Bytes";
2467 stats->data[i].rate_name = "PB/s";
2470 case _DRM_STAT_SECONDARY:
2471 stats->data[i].long_name = "Secondary Bytes";
2472 stats->data[i].rate_name = "SB/s";
2476 stats->data[i].long_name = "DMA";
2477 stats->data[i].rate_name = "DMA/s";
2480 case _DRM_STAT_SPECIAL:
2481 stats->data[i].long_name = "Special DMA";
2482 stats->data[i].rate_name = "dma/s";
2485 case _DRM_STAT_MISSED:
2486 stats->data[i].long_name = "Miss";
2487 stats->data[i].rate_name = "Ms/s";
2490 case _DRM_STAT_VALUE:
2491 stats->data[i].long_name = "Value";
2492 stats->data[i].rate_name = "Value";
2495 case _DRM_STAT_BYTE:
2496 stats->data[i].long_name = "Bytes";
2497 stats->data[i].rate_name = "B/s";
2500 case _DRM_STAT_COUNT:
2502 stats->data[i].long_name = "Count";
2503 stats->data[i].rate_name = "Cnt/s";
2512 * Issue a set-version ioctl.
2514 * \param fd file descriptor.
2515 * \param drmCommandIndex command index
2516 * \param data source pointer of the data to be read and written.
2517 * \param size size of the data to be read and written.
2519 * \return zero on success, or a negative value on failure.
2522 * It issues a read-write ioctl given by
2523 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2525 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2528 drm_set_version_t sv;
2531 sv.drm_di_major = version->drm_di_major;
2532 sv.drm_di_minor = version->drm_di_minor;
2533 sv.drm_dd_major = version->drm_dd_major;
2534 sv.drm_dd_minor = version->drm_dd_minor;
2536 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2540 version->drm_di_major = sv.drm_di_major;
2541 version->drm_di_minor = sv.drm_di_minor;
2542 version->drm_dd_major = sv.drm_dd_major;
2543 version->drm_dd_minor = sv.drm_dd_minor;
2549 * Send a device-specific command.
2551 * \param fd file descriptor.
2552 * \param drmCommandIndex command index
2554 * \return zero on success, or a negative value on failure.
2557 * It issues a ioctl given by
2558 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2560 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2562 unsigned long request;
2564 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2566 if (drmIoctl(fd, request, NULL)) {
2574 * Send a device-specific read command.
2576 * \param fd file descriptor.
2577 * \param drmCommandIndex command index
2578 * \param data destination pointer of the data to be read.
2579 * \param size size of the data to be read.
2581 * \return zero on success, or a negative value on failure.
2584 * It issues a read ioctl given by
2585 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2587 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2588 void *data, unsigned long size)
2590 unsigned long request;
2592 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2593 DRM_COMMAND_BASE + drmCommandIndex, size);
2595 if (drmIoctl(fd, request, data)) {
2603 * Send a device-specific write command.
2605 * \param fd file descriptor.
2606 * \param drmCommandIndex command index
2607 * \param data source pointer of the data to be written.
2608 * \param size size of the data to be written.
2610 * \return zero on success, or a negative value on failure.
2613 * It issues a write ioctl given by
2614 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2616 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2617 void *data, unsigned long size)
2619 unsigned long request;
2621 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2622 DRM_COMMAND_BASE + drmCommandIndex, size);
2624 if (drmIoctl(fd, request, data)) {
2632 * Send a device-specific read-write command.
2634 * \param fd file descriptor.
2635 * \param drmCommandIndex command index
2636 * \param data source pointer of the data to be read and written.
2637 * \param size size of the data to be read and written.
2639 * \return zero on success, or a negative value on failure.
2642 * It issues a read-write ioctl given by
2643 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2645 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2646 void *data, unsigned long size)
2648 unsigned long request;
2650 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2651 DRM_COMMAND_BASE + drmCommandIndex, size);
2653 if (drmIoctl(fd, request, data))
2658 #define DRM_MAX_FDS 16
2664 } connection[DRM_MAX_FDS];
2666 static int nr_fds = 0;
2668 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2670 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2673 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2679 for (i = 0; i < nr_fds; i++)
2680 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2681 (connection[i].type == type)) {
2682 connection[i].refcount++;
2684 return connection[i].fd;
2687 fd = drmOpenWithType(NULL, BusID, type);
2688 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2691 connection[nr_fds].BusID = strdup(BusID);
2692 connection[nr_fds].fd = fd;
2693 connection[nr_fds].refcount = 1;
2694 connection[nr_fds].type = type;
2698 fprintf(stderr, "saved connection %d for %s %d\n",
2699 nr_fds, connection[nr_fds].BusID,
2700 strcmp(BusID, connection[nr_fds].BusID));
2707 drm_public void drmCloseOnce(int fd)
2711 for (i = 0; i < nr_fds; i++) {
2712 if (fd == connection[i].fd) {
2713 if (--connection[i].refcount == 0) {
2714 drmClose(connection[i].fd);
2715 free(connection[i].BusID);
2718 connection[i] = connection[nr_fds];
2726 drm_public int drmSetMaster(int fd)
2728 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2731 drm_public int drmDropMaster(int fd)
2733 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2736 drm_public int drmIsMaster(int fd)
2738 /* Detect master by attempting something that requires master.
2740 * Authenticating magic tokens requires master and 0 is an
2741 * internal kernel detail which we could use. Attempting this on
2742 * a master fd would fail therefore fail with EINVAL because 0
2745 * A non-master fd will fail with EACCES, as the kernel checks
2746 * for master before attempting to do anything else.
2748 * Since we don't want to leak implementation details, use
2751 return drmAuthMagic(fd, 0) != -EACCES;
2754 drm_public char *drmGetDeviceNameFromFd(int fd)
2761 /* The whole drmOpen thing is a fiasco and we need to find a way
2762 * back to just using open(2). For now, however, lets just make
2763 * things worse with even more ad hoc directory walking code to
2764 * discover the device file name. */
2769 for (i = 0; i < DRM_MAX_MINOR; i++) {
2770 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2771 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2774 if (i == DRM_MAX_MINOR)
2777 return strdup(name);
2780 static bool drmNodeIsDRM(int maj, int min)
2786 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2788 return stat(path, &sbuf) == 0;
2790 return maj == DRM_MAJOR;
2794 drm_public int drmGetNodeTypeFromFd(int fd)
2799 if (fstat(fd, &sbuf))
2802 maj = major(sbuf.st_rdev);
2803 min = minor(sbuf.st_rdev);
2805 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2810 type = drmGetMinorType(min);
2816 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2819 struct drm_prime_handle args;
2824 args.handle = handle;
2826 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2830 *prime_fd = args.fd;
2834 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2836 struct drm_prime_handle args;
2841 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2845 *handle = args.handle;
2849 static char *drmGetMinorNameForFD(int fd, int type)
2855 const char *name = drmGetMinorName(type);
2857 char dev_name[64], buf[64];
2865 if (fstat(fd, &sbuf))
2868 maj = major(sbuf.st_rdev);
2869 min = minor(sbuf.st_rdev);
2871 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2874 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2876 sysdir = opendir(buf);
2880 while ((ent = readdir(sysdir))) {
2881 if (strncmp(ent->d_name, name, len) == 0) {
2882 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2886 return strdup(dev_name);
2894 char buf[PATH_MAX + 1];
2895 const char *dev_name;
2896 unsigned int maj, min;
2899 if (fstat(fd, &sbuf))
2902 maj = major(sbuf.st_rdev);
2903 min = minor(sbuf.st_rdev);
2905 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2909 case DRM_NODE_PRIMARY:
2910 dev_name = DRM_DEV_NAME;
2912 case DRM_NODE_CONTROL:
2913 dev_name = DRM_CONTROL_DEV_NAME;
2915 case DRM_NODE_RENDER:
2916 dev_name = DRM_RENDER_DEV_NAME;
2922 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
2923 if (n == -1 || n >= sizeof(buf))
2930 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2932 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2935 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2937 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2941 static char * DRM_PRINTFLIKE(2, 3)
2942 sysfs_uevent_get(const char *path, const char *fmt, ...)
2944 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2945 size_t size = 0, len;
2951 num = vasprintf(&key, fmt, ap);
2955 snprintf(filename, sizeof(filename), "%s/uevent", path);
2957 fp = fopen(filename, "r");
2963 while ((num = getline(&line, &size, fp)) >= 0) {
2964 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
2965 char *start = line + len + 1, *end = line + num - 1;
2970 value = strndup(start, end - start);
2984 /* Little white lie to avoid major rework of the existing code */
2985 #define DRM_BUS_VIRTIO 0x10
2987 static int drmParseSubsystemType(int maj, int min)
2990 char path[PATH_MAX + 1];
2991 char link[PATH_MAX + 1] = "";
2997 { "/pci", DRM_BUS_PCI },
2998 { "/usb", DRM_BUS_USB },
2999 { "/platform", DRM_BUS_PLATFORM },
3000 { "/spi", DRM_BUS_PLATFORM },
3001 { "/host1x", DRM_BUS_HOST1X },
3002 { "/virtio", DRM_BUS_VIRTIO },
3005 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
3008 if (readlink(path, link, PATH_MAX) < 0)
3011 name = strrchr(link, '/');
3015 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3016 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3017 return bus_types[i].bus_type;
3021 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3024 #warning "Missing implementation of drmParseSubsystemType"
3030 get_pci_path(int maj, int min, char *pci_path)
3032 char path[PATH_MAX + 1], *term;
3034 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3035 if (!realpath(path, pci_path)) {
3036 strcpy(pci_path, path);
3040 term = strrchr(pci_path, '/');
3041 if (term && strncmp(term, "/virtio", 7) == 0)
3045 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3048 unsigned int domain, bus, dev, func;
3049 char pci_path[PATH_MAX + 1], *value;
3052 get_pci_path(maj, min, pci_path);
3054 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3058 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3064 info->domain = domain;
3070 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3071 struct drm_pciinfo pinfo;
3074 type = drmGetMinorType(min);
3078 fd = drmOpenMinor(min, 0, type);
3082 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3088 info->domain = pinfo.domain;
3089 info->bus = pinfo.bus;
3090 info->dev = pinfo.dev;
3091 info->func = pinfo.func;
3095 #warning "Missing implementation of drmParsePciBusInfo"
3100 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3102 if (a == NULL || b == NULL)
3105 if (a->bustype != b->bustype)
3108 switch (a->bustype) {
3110 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3113 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3115 case DRM_BUS_PLATFORM:
3116 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3118 case DRM_BUS_HOST1X:
3119 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3128 static int drmGetNodeType(const char *name)
3130 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3131 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3132 return DRM_NODE_CONTROL;
3134 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3135 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3136 return DRM_NODE_RENDER;
3138 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3139 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3140 return DRM_NODE_PRIMARY;
3145 static int drmGetMaxNodeName(void)
3147 return sizeof(DRM_DIR_NAME) +
3148 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3149 sizeof(DRM_CONTROL_MINOR_NAME),
3150 sizeof(DRM_RENDER_MINOR_NAME)) +
3151 3 /* length of the node number */;
3155 static int parse_separate_sysfs_files(int maj, int min,
3156 drmPciDeviceInfoPtr device,
3157 bool ignore_revision)
3159 static const char *attrs[] = {
3160 "revision", /* Older kernels are missing the file, so check for it first */
3166 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3167 unsigned int data[ARRAY_SIZE(attrs)];
3171 get_pci_path(maj, min, pci_path);
3173 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3174 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3175 fp = fopen(path, "r");
3179 ret = fscanf(fp, "%x", &data[i]);
3186 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3187 device->vendor_id = data[1] & 0xffff;
3188 device->device_id = data[2] & 0xffff;
3189 device->subvendor_id = data[3] & 0xffff;
3190 device->subdevice_id = data[4] & 0xffff;
3195 static int parse_config_sysfs_file(int maj, int min,
3196 drmPciDeviceInfoPtr device)
3198 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3199 unsigned char config[64];
3202 get_pci_path(maj, min, pci_path);
3204 snprintf(path, PATH_MAX, "%s/config", pci_path);
3205 fd = open(path, O_RDONLY);
3209 ret = read(fd, config, sizeof(config));
3214 device->vendor_id = config[0] | (config[1] << 8);
3215 device->device_id = config[2] | (config[3] << 8);
3216 device->revision_id = config[8];
3217 device->subvendor_id = config[44] | (config[45] << 8);
3218 device->subdevice_id = config[46] | (config[47] << 8);
3224 static int drmParsePciDeviceInfo(int maj, int min,
3225 drmPciDeviceInfoPtr device,
3229 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3230 return parse_separate_sysfs_files(maj, min, device, true);
3232 if (parse_separate_sysfs_files(maj, min, device, false))
3233 return parse_config_sysfs_file(maj, min, device);
3236 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3237 struct drm_pciinfo pinfo;
3240 type = drmGetMinorType(min);
3244 fd = drmOpenMinor(min, 0, type);
3248 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3254 device->vendor_id = pinfo.vendor_id;
3255 device->device_id = pinfo.device_id;
3256 device->revision_id = pinfo.revision_id;
3257 device->subvendor_id = pinfo.subvendor_id;
3258 device->subdevice_id = pinfo.subdevice_id;
3262 #warning "Missing implementation of drmParsePciDeviceInfo"
3267 static void drmFreePlatformDevice(drmDevicePtr device)
3269 if (device->deviceinfo.platform) {
3270 if (device->deviceinfo.platform->compatible) {
3271 char **compatible = device->deviceinfo.platform->compatible;
3273 while (*compatible) {
3278 free(device->deviceinfo.platform->compatible);
3283 static void drmFreeHost1xDevice(drmDevicePtr device)
3285 if (device->deviceinfo.host1x) {
3286 if (device->deviceinfo.host1x->compatible) {
3287 char **compatible = device->deviceinfo.host1x->compatible;
3289 while (*compatible) {
3294 free(device->deviceinfo.host1x->compatible);
3299 drm_public void drmFreeDevice(drmDevicePtr *device)
3305 switch ((*device)->bustype) {
3306 case DRM_BUS_PLATFORM:
3307 drmFreePlatformDevice(*device);
3310 case DRM_BUS_HOST1X:
3311 drmFreeHost1xDevice(*device);
3320 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3324 if (devices == NULL)
3327 for (i = 0; i < count; i++)
3329 drmFreeDevice(&devices[i]);
3332 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3333 size_t bus_size, size_t device_size,
3336 size_t max_node_length, extra, size;
3337 drmDevicePtr device;
3341 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3342 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3344 size = sizeof(*device) + extra + bus_size + device_size;
3346 device = calloc(1, size);
3350 device->available_nodes = 1 << type;
3352 ptr = (char *)device + sizeof(*device);
3353 device->nodes = (char **)ptr;
3355 ptr += DRM_NODE_MAX * sizeof(void *);
3357 for (i = 0; i < DRM_NODE_MAX; i++) {
3358 device->nodes[i] = ptr;
3359 ptr += max_node_length;
3362 memcpy(device->nodes[type], node, max_node_length);
3369 static int drmProcessPciDevice(drmDevicePtr *device,
3370 const char *node, int node_type,
3371 int maj, int min, bool fetch_deviceinfo,
3378 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3379 sizeof(drmPciDeviceInfo), &addr);
3383 dev->bustype = DRM_BUS_PCI;
3385 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3387 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3391 // Fetch the device info if the user has requested it
3392 if (fetch_deviceinfo) {
3393 addr += sizeof(drmPciBusInfo);
3394 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3396 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3410 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3413 char path[PATH_MAX + 1], *value;
3414 unsigned int bus, dev;
3417 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3419 value = sysfs_uevent_get(path, "BUSNUM");
3423 ret = sscanf(value, "%03u", &bus);
3429 value = sysfs_uevent_get(path, "DEVNUM");
3433 ret = sscanf(value, "%03u", &dev);
3444 #warning "Missing implementation of drmParseUsbBusInfo"
3449 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3452 char path[PATH_MAX + 1], *value;
3453 unsigned int vendor, product;
3456 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3458 value = sysfs_uevent_get(path, "PRODUCT");
3462 ret = sscanf(value, "%x/%x", &vendor, &product);
3468 info->vendor = vendor;
3469 info->product = product;
3473 #warning "Missing implementation of drmParseUsbDeviceInfo"
3478 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3479 int node_type, int maj, int min,
3480 bool fetch_deviceinfo, uint32_t flags)
3486 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3487 sizeof(drmUsbDeviceInfo), &ptr);
3491 dev->bustype = DRM_BUS_USB;
3493 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3495 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3499 if (fetch_deviceinfo) {
3500 ptr += sizeof(drmUsbBusInfo);
3501 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3503 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3517 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3520 char path[PATH_MAX + 1], *name, *tmp_name;
3522 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3524 name = sysfs_uevent_get(path, "OF_FULLNAME");
3527 /* If the device lacks OF data, pick the MODALIAS info */
3528 name = sysfs_uevent_get(path, "MODALIAS");
3532 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3533 tmp_name = strrchr(name, ':');
3541 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3542 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3547 #warning "Missing implementation of drmParseOFBusInfo"
3552 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3555 char path[PATH_MAX + 1], *value, *tmp_name;
3556 unsigned int count, i;
3559 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3561 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3563 sscanf(value, "%u", &count);
3566 /* Assume one entry if the device lack OF data */
3570 *compatible = calloc(count + 1, sizeof(char *));
3574 for (i = 0; i < count; i++) {
3575 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3578 /* If the device lacks OF data, pick the MODALIAS info */
3579 value = sysfs_uevent_get(path, "MODALIAS");
3585 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3586 tmp_name = strrchr(value, ':');
3591 tmp_name = strdup(tmp_name + 1);
3595 (*compatible)[i] = tmp_name;
3602 free((*compatible)[i]);
3607 #warning "Missing implementation of drmParseOFDeviceInfo"
3612 static int drmProcessPlatformDevice(drmDevicePtr *device,
3613 const char *node, int node_type,
3614 int maj, int min, bool fetch_deviceinfo,
3621 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3622 sizeof(drmPlatformDeviceInfo), &ptr);
3626 dev->bustype = DRM_BUS_PLATFORM;
3628 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3630 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3634 if (fetch_deviceinfo) {
3635 ptr += sizeof(drmPlatformBusInfo);
3636 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3638 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3652 static int drmProcessHost1xDevice(drmDevicePtr *device,
3653 const char *node, int node_type,
3654 int maj, int min, bool fetch_deviceinfo,
3661 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3662 sizeof(drmHost1xDeviceInfo), &ptr);
3666 dev->bustype = DRM_BUS_HOST1X;
3668 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3670 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3674 if (fetch_deviceinfo) {
3675 ptr += sizeof(drmHost1xBusInfo);
3676 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3678 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3693 process_device(drmDevicePtr *device, const char *d_name,
3694 int req_subsystem_type,
3695 bool fetch_deviceinfo, uint32_t flags)
3698 char node[PATH_MAX + 1];
3699 int node_type, subsystem_type;
3700 unsigned int maj, min;
3702 node_type = drmGetNodeType(d_name);
3706 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3707 if (stat(node, &sbuf))
3710 maj = major(sbuf.st_rdev);
3711 min = minor(sbuf.st_rdev);
3713 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3716 subsystem_type = drmParseSubsystemType(maj, min);
3717 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3720 switch (subsystem_type) {
3722 case DRM_BUS_VIRTIO:
3723 return drmProcessPciDevice(device, node, node_type, maj, min,
3724 fetch_deviceinfo, flags);
3726 return drmProcessUsbDevice(device, node, node_type, maj, min,
3727 fetch_deviceinfo, flags);
3728 case DRM_BUS_PLATFORM:
3729 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3730 fetch_deviceinfo, flags);
3731 case DRM_BUS_HOST1X:
3732 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3733 fetch_deviceinfo, flags);
3739 /* Consider devices located on the same bus as duplicate and fold the respective
3740 * entries into a single one.
3742 * Note: this leaves "gaps" in the array, while preserving the length.
3744 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3746 int node_type, i, j;
3748 for (i = 0; i < count; i++) {
3749 for (j = i + 1; j < count; j++) {
3750 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3751 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3752 node_type = log2(local_devices[j]->available_nodes);
3753 memcpy(local_devices[i]->nodes[node_type],
3754 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3755 drmFreeDevice(&local_devices[j]);
3761 /* Check that the given flags are valid returning 0 on success */
3763 drm_device_validate_flags(uint32_t flags)
3765 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3769 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3773 for (int i = 0; i < DRM_NODE_MAX; i++) {
3774 if (device->available_nodes & 1 << i) {
3775 if (stat(device->nodes[i], &sbuf) == 0 &&
3776 sbuf.st_rdev == find_rdev)
3784 * The kernel drm core has a number of places that assume maximum of
3785 * 3x64 devices nodes. That's 64 for each of primary, control and
3786 * render nodes. Rounded it up to 256 for simplicity.
3788 #define MAX_DRM_NODES 256
3791 * Get information about the opened drm device
3793 * \param fd file descriptor of the drm device
3794 * \param flags feature/behaviour bitmask
3795 * \param device the address of a drmDevicePtr where the information
3796 * will be allocated in stored
3798 * \return zero on success, negative error code otherwise.
3800 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3801 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3803 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3807 * DRI device nodes on OpenBSD are not in their own directory, they reside
3808 * in /dev along with a large number of statically generated /dev nodes.
3809 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3813 char node[PATH_MAX + 1];
3814 const char *dev_name;
3815 int node_type, subsystem_type;
3816 int maj, min, n, ret;
3818 if (fd == -1 || device == NULL)
3821 if (fstat(fd, &sbuf))
3824 maj = major(sbuf.st_rdev);
3825 min = minor(sbuf.st_rdev);
3827 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3830 node_type = drmGetMinorType(min);
3831 if (node_type == -1)
3834 switch (node_type) {
3835 case DRM_NODE_PRIMARY:
3836 dev_name = DRM_DEV_NAME;
3838 case DRM_NODE_CONTROL:
3839 dev_name = DRM_CONTROL_DEV_NAME;
3841 case DRM_NODE_RENDER:
3842 dev_name = DRM_RENDER_DEV_NAME;
3848 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
3849 if (n == -1 || n >= PATH_MAX)
3851 if (stat(node, &sbuf))
3854 subsystem_type = drmParseSubsystemType(maj, min);
3855 if (subsystem_type != DRM_BUS_PCI)
3858 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3866 drmDevicePtr local_devices[MAX_DRM_NODES];
3869 struct dirent *dent;
3873 int ret, i, node_count;
3876 if (drm_device_validate_flags(flags))
3879 if (fd == -1 || device == NULL)
3882 if (fstat(fd, &sbuf))
3885 find_rdev = sbuf.st_rdev;
3886 maj = major(sbuf.st_rdev);
3887 min = minor(sbuf.st_rdev);
3889 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3892 subsystem_type = drmParseSubsystemType(maj, min);
3893 if (subsystem_type < 0)
3894 return subsystem_type;
3896 sysdir = opendir(DRM_DIR_NAME);
3901 while ((dent = readdir(sysdir))) {
3902 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3906 if (i >= MAX_DRM_NODES) {
3907 fprintf(stderr, "More than %d drm nodes detected. "
3908 "Please report a bug - that should not happen.\n"
3909 "Skipping extra nodes\n", MAX_DRM_NODES);
3912 local_devices[i] = d;
3917 drmFoldDuplicatedDevices(local_devices, node_count);
3921 for (i = 0; i < node_count; i++) {
3922 if (!local_devices[i])
3925 if (drm_device_has_rdev(local_devices[i], find_rdev))
3926 *device = local_devices[i];
3928 drmFreeDevice(&local_devices[i]);
3932 if (*device == NULL)
3939 * Get information about the opened drm device
3941 * \param fd file descriptor of the drm device
3942 * \param device the address of a drmDevicePtr where the information
3943 * will be allocated in stored
3945 * \return zero on success, negative error code otherwise.
3947 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3949 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3953 * Get drm devices on the system
3955 * \param flags feature/behaviour bitmask
3956 * \param devices the array of devices with drmDevicePtr elements
3957 * can be NULL to get the device number first
3958 * \param max_devices the maximum number of devices for the array
3960 * \return on error - negative error code,
3961 * if devices is NULL - total number of devices available on the system,
3962 * alternatively the number of devices stored in devices[], which is
3963 * capped by the max_devices.
3965 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
3966 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3968 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
3971 drmDevicePtr local_devices[MAX_DRM_NODES];
3972 drmDevicePtr device;
3974 struct dirent *dent;
3975 int ret, i, node_count, device_count;
3977 if (drm_device_validate_flags(flags))
3980 sysdir = opendir(DRM_DIR_NAME);
3985 while ((dent = readdir(sysdir))) {
3986 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
3990 if (i >= MAX_DRM_NODES) {
3991 fprintf(stderr, "More than %d drm nodes detected. "
3992 "Please report a bug - that should not happen.\n"
3993 "Skipping extra nodes\n", MAX_DRM_NODES);
3996 local_devices[i] = device;
4001 drmFoldDuplicatedDevices(local_devices, node_count);
4004 for (i = 0; i < node_count; i++) {
4005 if (!local_devices[i])
4008 if ((devices != NULL) && (device_count < max_devices))
4009 devices[device_count] = local_devices[i];
4011 drmFreeDevice(&local_devices[i]);
4017 return device_count;
4021 * Get drm devices on the system
4023 * \param devices the array of devices with drmDevicePtr elements
4024 * can be NULL to get the device number first
4025 * \param max_devices the maximum number of devices for the array
4027 * \return on error - negative error code,
4028 * if devices is NULL - total number of devices available on the system,
4029 * alternatively the number of devices stored in devices[], which is
4030 * capped by the max_devices.
4032 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4034 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4037 drm_public char *drmGetDeviceNameFromFd2(int fd)
4041 char path[PATH_MAX + 1], *value;
4042 unsigned int maj, min;
4044 if (fstat(fd, &sbuf))
4047 maj = major(sbuf.st_rdev);
4048 min = minor(sbuf.st_rdev);
4050 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4053 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4055 value = sysfs_uevent_get(path, "DEVNAME");
4059 snprintf(path, sizeof(path), "/dev/%s", value);
4062 return strdup(path);
4065 char node[PATH_MAX + 1];
4066 const char *dev_name;
4070 if (fstat(fd, &sbuf))
4073 maj = major(sbuf.st_rdev);
4074 min = minor(sbuf.st_rdev);
4076 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4079 node_type = drmGetMinorType(min);
4080 if (node_type == -1)
4083 switch (node_type) {
4084 case DRM_NODE_PRIMARY:
4085 dev_name = DRM_DEV_NAME;
4087 case DRM_NODE_CONTROL:
4088 dev_name = DRM_CONTROL_DEV_NAME;
4090 case DRM_NODE_RENDER:
4091 dev_name = DRM_RENDER_DEV_NAME;
4097 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4098 if (n == -1 || n >= PATH_MAX)
4101 return strdup(node);
4105 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4107 struct drm_syncobj_create args;
4113 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4116 *handle = args.handle;
4120 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4122 struct drm_syncobj_destroy args;
4125 args.handle = handle;
4126 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4129 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4131 struct drm_syncobj_handle args;
4136 args.handle = handle;
4137 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4144 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4146 struct drm_syncobj_handle args;
4152 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4155 *handle = args.handle;
4159 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4162 struct drm_syncobj_handle args;
4165 args.fd = sync_file_fd;
4166 args.handle = handle;
4167 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4168 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4171 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4174 struct drm_syncobj_handle args;
4179 args.handle = handle;
4180 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4181 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4184 *sync_file_fd = args.fd;
4188 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4189 int64_t timeout_nsec, unsigned flags,
4190 uint32_t *first_signaled)
4192 struct drm_syncobj_wait args;
4196 args.handles = (uintptr_t)handles;
4197 args.timeout_nsec = timeout_nsec;
4198 args.count_handles = num_handles;
4201 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4206 *first_signaled = args.first_signaled;
4210 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4211 uint32_t handle_count)
4213 struct drm_syncobj_array args;
4217 args.handles = (uintptr_t)handles;
4218 args.count_handles = handle_count;
4220 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4224 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4225 uint32_t handle_count)
4227 struct drm_syncobj_array args;
4231 args.handles = (uintptr_t)handles;
4232 args.count_handles = handle_count;
4234 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4238 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4239 uint64_t *points, uint32_t handle_count)
4241 struct drm_syncobj_timeline_array args;
4245 args.handles = (uintptr_t)handles;
4246 args.points = (uintptr_t)points;
4247 args.count_handles = handle_count;
4249 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4253 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4254 unsigned num_handles,
4255 int64_t timeout_nsec, unsigned flags,
4256 uint32_t *first_signaled)
4258 struct drm_syncobj_timeline_wait args;
4262 args.handles = (uintptr_t)handles;
4263 args.points = (uintptr_t)points;
4264 args.timeout_nsec = timeout_nsec;
4265 args.count_handles = num_handles;
4268 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4273 *first_signaled = args.first_signaled;
4278 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4279 uint32_t handle_count)
4281 struct drm_syncobj_timeline_array args;
4285 args.handles = (uintptr_t)handles;
4286 args.points = (uintptr_t)points;
4287 args.count_handles = handle_count;
4289 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4295 drm_public int drmSyncobjTransfer(int fd,
4296 uint32_t dst_handle, uint64_t dst_point,
4297 uint32_t src_handle, uint64_t src_point,
4300 struct drm_syncobj_transfer args;
4304 args.src_handle = src_handle;
4305 args.dst_handle = dst_handle;
4306 args.src_point = src_point;
4307 args.dst_point = dst_point;
4310 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);