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));
303 static const char *drmGetDeviceName(int type)
306 case DRM_NODE_PRIMARY:
308 case DRM_NODE_CONTROL:
309 return DRM_CONTROL_DEV_NAME;
310 case DRM_NODE_RENDER:
311 return DRM_RENDER_DEV_NAME;
317 * Open the DRM device, creating it if necessary.
319 * \param dev major and minor numbers of the device.
320 * \param minor minor number of the device.
322 * \return a file descriptor on success, or a negative value on error.
325 * Assembles the device name from \p minor and opens it, creating the device
326 * special file node with the major and minor numbers specified by \p dev and
327 * parent directory if necessary and was called by root.
329 static int drmOpenDevice(dev_t dev, int minor, int type)
332 const char *dev_name = drmGetDeviceName(type);
333 char buf[DRM_NODE_NAME_MAX];
335 mode_t devmode = DRM_DEV_MODE, serv_mode;
338 int isroot = !geteuid();
339 uid_t user = DRM_DEV_UID;
340 gid_t group = DRM_DEV_GID;
346 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
347 drmMsg("drmOpenDevice: node name is %s\n", buf);
349 if (drm_server_info && drm_server_info->get_perms) {
350 drm_server_info->get_perms(&serv_group, &serv_mode);
351 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
352 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
356 if (stat(DRM_DIR_NAME, &st)) {
358 return DRM_ERR_NOT_ROOT;
359 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
360 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
361 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
364 /* Check if the device node exists and create it if necessary. */
365 if (stat(buf, &st)) {
367 return DRM_ERR_NOT_ROOT;
369 mknod(buf, S_IFCHR | devmode, dev);
372 if (drm_server_info && drm_server_info->get_perms) {
373 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
374 chown_check_return(buf, user, group);
378 /* if we modprobed then wait for udev */
382 if (stat(DRM_DIR_NAME, &st)) {
386 if (udev_count == 50)
391 if (stat(buf, &st)) {
395 if (udev_count == 50)
402 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
403 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
404 fd, fd < 0 ? strerror(errno) : "OK");
409 /* Check if the device node is not what we expect it to be, and recreate it
410 * and try again if so.
412 if (st.st_rdev != dev) {
414 return DRM_ERR_NOT_ROOT;
416 mknod(buf, S_IFCHR | devmode, dev);
417 if (drm_server_info && drm_server_info->get_perms) {
418 chown_check_return(buf, user, group);
422 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
423 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
424 fd, fd < 0 ? strerror(errno) : "OK");
428 drmMsg("drmOpenDevice: Open failed\n");
436 * Open the DRM device
438 * \param minor device minor number.
439 * \param create allow to create the device if set.
441 * \return a file descriptor on success, or a negative value on error.
444 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
445 * name from \p minor and opens it.
447 static int drmOpenMinor(int minor, int create, int type)
450 char buf[DRM_NODE_NAME_MAX];
451 const char *dev_name = drmGetDeviceName(type);
454 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
459 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
460 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
467 * Determine whether the DRM kernel driver has been loaded.
469 * \return 1 if the DRM driver is loaded, 0 otherwise.
472 * Determine the presence of the kernel driver by attempting to open the 0
473 * minor and get version information. For backward compatibility with older
474 * Linux implementations, /proc/dri is also checked.
476 drm_public int drmAvailable(void)
478 drmVersionPtr version;
482 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
484 /* Try proc for backward Linux compatibility */
485 if (!access("/proc/dri/0", R_OK))
491 if ((version = drmGetVersion(fd))) {
493 drmFreeVersion(version);
500 static int drmGetMinorBase(int type)
503 case DRM_NODE_PRIMARY:
505 case DRM_NODE_CONTROL:
507 case DRM_NODE_RENDER:
514 static int drmGetMinorType(int minor)
516 int type = minor >> 6;
522 case DRM_NODE_PRIMARY:
523 case DRM_NODE_CONTROL:
524 case DRM_NODE_RENDER:
531 static const char *drmGetMinorName(int type)
534 case DRM_NODE_PRIMARY:
535 return DRM_PRIMARY_MINOR_NAME;
536 case DRM_NODE_CONTROL:
537 return DRM_CONTROL_MINOR_NAME;
538 case DRM_NODE_RENDER:
539 return DRM_RENDER_MINOR_NAME;
546 * Open the device by bus ID.
548 * \param busid bus ID.
549 * \param type device node type.
551 * \return a file descriptor on success, or a negative value on error.
554 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
555 * comparing the device bus ID with the one supplied.
557 * \sa drmOpenMinor() and drmGetBusid().
559 static int drmOpenByBusid(const char *busid, int type)
561 int i, pci_domain_ok = 1;
565 int base = drmGetMinorBase(type);
570 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
571 for (i = base; i < base + DRM_MAX_MINOR; i++) {
572 fd = drmOpenMinor(i, 1, type);
573 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
575 /* We need to try for 1.4 first for proper PCI domain support
576 * and if that fails, we know the kernel is busted
580 sv.drm_dd_major = -1; /* Don't care */
581 sv.drm_dd_minor = -1; /* Don't care */
582 if (drmSetInterfaceVersion(fd, &sv)) {
588 sv.drm_dd_major = -1; /* Don't care */
589 sv.drm_dd_minor = -1; /* Don't care */
590 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
591 drmSetInterfaceVersion(fd, &sv);
593 buf = drmGetBusid(fd);
594 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
595 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
609 * Open the device by name.
611 * \param name driver name.
612 * \param type the device node type.
614 * \return a file descriptor on success, or a negative value on error.
617 * This function opens the first minor number that matches the driver name and
618 * isn't already in use. If it's in use it then it will already have a bus ID
621 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
623 static int drmOpenByName(const char *name, int type)
627 drmVersionPtr version;
629 int base = drmGetMinorBase(type);
635 * Open the first minor number that matches the driver name and isn't
636 * already in use. If it's in use it will have a busid assigned already.
638 for (i = base; i < base + DRM_MAX_MINOR; i++) {
639 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
640 if ((version = drmGetVersion(fd))) {
641 if (!strcmp(version->name, name)) {
642 drmFreeVersion(version);
643 id = drmGetBusid(fd);
644 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
653 drmFreeVersion(version);
661 /* Backward-compatibility /proc support */
662 for (i = 0; i < 8; i++) {
663 char proc_name[64], buf[512];
664 char *driver, *pt, *devstring;
667 sprintf(proc_name, "/proc/dri/%d/name", i);
668 if ((fd = open(proc_name, 0, 0)) >= 0) {
669 retcode = read(fd, buf, sizeof(buf)-1);
672 buf[retcode-1] = '\0';
673 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
675 if (*pt) { /* Device is next */
677 if (!strcmp(driver, name)) { /* Match */
678 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
680 if (*pt) { /* Found busid */
681 return drmOpenByBusid(++pt, type);
682 } else { /* No busid */
683 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
697 * Open the DRM device.
699 * Looks up the specified name and bus ID, and opens the device found. The
700 * entry in /dev/dri is created if necessary and if called by root.
702 * \param name driver name. Not referenced if bus ID is supplied.
703 * \param busid bus ID. Zero if not known.
705 * \return a file descriptor on success, or a negative value on error.
708 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
711 drm_public int drmOpen(const char *name, const char *busid)
713 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
717 * Open the DRM device with specified type.
719 * Looks up the specified name and bus ID, and opens the device found. The
720 * entry in /dev/dri is created if necessary and if called by root.
722 * \param name driver name. Not referenced if bus ID is supplied.
723 * \param busid bus ID. Zero if not known.
724 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
726 * \return a file descriptor on success, or a negative value on error.
729 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
732 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
734 if (name != NULL && drm_server_info &&
735 drm_server_info->load_module && !drmAvailable()) {
736 /* try to load the kernel module */
737 if (!drm_server_info->load_module(name)) {
738 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
744 int fd = drmOpenByBusid(busid, type);
750 return drmOpenByName(name, type);
755 drm_public int drmOpenControl(int minor)
757 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
760 drm_public int drmOpenRender(int minor)
762 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
766 * Free the version information returned by drmGetVersion().
768 * \param v pointer to the version information.
771 * It frees the memory pointed by \p %v as well as all the non-null strings
774 drm_public void drmFreeVersion(drmVersionPtr v)
786 * Free the non-public version information returned by the kernel.
788 * \param v pointer to the version information.
791 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
792 * the non-null strings pointers in it.
794 static void drmFreeKernelVersion(drm_version_t *v)
806 * Copy version information.
808 * \param d destination pointer.
809 * \param s source pointer.
812 * Used by drmGetVersion() to translate the information returned by the ioctl
813 * interface in a private structure into the public structure counterpart.
815 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
817 d->version_major = s->version_major;
818 d->version_minor = s->version_minor;
819 d->version_patchlevel = s->version_patchlevel;
820 d->name_len = s->name_len;
821 d->name = strdup(s->name);
822 d->date_len = s->date_len;
823 d->date = strdup(s->date);
824 d->desc_len = s->desc_len;
825 d->desc = strdup(s->desc);
830 * Query the driver version information.
832 * \param fd file descriptor.
834 * \return pointer to a drmVersion structure which should be freed with
837 * \note Similar information is available via /proc/dri.
840 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
841 * first with zeros to get the string lengths, and then the actually strings.
842 * It also null-terminates them since they might not be already.
844 drm_public drmVersionPtr drmGetVersion(int fd)
846 drmVersionPtr retval;
847 drm_version_t *version = drmMalloc(sizeof(*version));
849 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
850 drmFreeKernelVersion(version);
854 if (version->name_len)
855 version->name = drmMalloc(version->name_len + 1);
856 if (version->date_len)
857 version->date = drmMalloc(version->date_len + 1);
858 if (version->desc_len)
859 version->desc = drmMalloc(version->desc_len + 1);
861 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
862 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
863 drmFreeKernelVersion(version);
867 /* The results might not be null-terminated strings, so terminate them. */
868 if (version->name_len) version->name[version->name_len] = '\0';
869 if (version->date_len) version->date[version->date_len] = '\0';
870 if (version->desc_len) version->desc[version->desc_len] = '\0';
872 retval = drmMalloc(sizeof(*retval));
873 drmCopyVersion(retval, version);
874 drmFreeKernelVersion(version);
880 * Get version information for the DRM user space library.
882 * This version number is driver independent.
884 * \param fd file descriptor.
886 * \return version information.
889 * This function allocates and fills a drm_version structure with a hard coded
892 drm_public drmVersionPtr drmGetLibVersion(int fd)
894 drm_version_t *version = drmMalloc(sizeof(*version));
897 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
898 * revision 1.0.x = original DRM interface with no drmGetLibVersion
899 * entry point and many drm<Device> extensions
900 * revision 1.1.x = added drmCommand entry points for device extensions
901 * added drmGetLibVersion to identify libdrm.a version
902 * revision 1.2.x = added drmSetInterfaceVersion
903 * modified drmOpen to handle both busid and name
904 * revision 1.3.x = added server + memory manager
906 version->version_major = 1;
907 version->version_minor = 3;
908 version->version_patchlevel = 0;
910 return (drmVersionPtr)version;
913 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
915 struct drm_get_cap cap;
919 cap.capability = capability;
921 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
929 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
931 struct drm_set_client_cap cap;
934 cap.capability = capability;
937 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
941 * Free the bus ID information.
943 * \param busid bus ID information string as given by drmGetBusid().
946 * This function is just frees the memory pointed by \p busid.
948 drm_public void drmFreeBusid(const char *busid)
950 drmFree((void *)busid);
955 * Get the bus ID of the device.
957 * \param fd file descriptor.
959 * \return bus ID string.
962 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
963 * get the string length and data, passing the arguments in a drm_unique
966 drm_public char *drmGetBusid(int fd)
972 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
974 u.unique = drmMalloc(u.unique_len + 1);
975 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
979 u.unique[u.unique_len] = '\0';
986 * Set the bus ID of the device.
988 * \param fd file descriptor.
989 * \param busid bus ID string.
991 * \return zero on success, negative on failure.
994 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
995 * the arguments in a drm_unique structure.
997 drm_public int drmSetBusid(int fd, const char *busid)
1002 u.unique = (char *)busid;
1003 u.unique_len = strlen(busid);
1005 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1011 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1018 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1020 *magic = auth.magic;
1024 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1030 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1036 * Specifies a range of memory that is available for mapping by a
1039 * \param fd file descriptor.
1040 * \param offset usually the physical address. The actual meaning depends of
1041 * the \p type parameter. See below.
1042 * \param size of the memory in bytes.
1043 * \param type type of the memory to be mapped.
1044 * \param flags combination of several flags to modify the function actions.
1045 * \param handle will be set to a value that may be used as the offset
1046 * parameter for mmap().
1048 * \return zero on success or a negative value on error.
1050 * \par Mapping the frame buffer
1051 * For the frame buffer
1052 * - \p offset will be the physical address of the start of the frame buffer,
1053 * - \p size will be the size of the frame buffer in bytes, and
1054 * - \p type will be DRM_FRAME_BUFFER.
1057 * The area mapped will be uncached. If MTRR support is available in the
1058 * kernel, the frame buffer area will be set to write combining.
1060 * \par Mapping the MMIO register area
1061 * For the MMIO register area,
1062 * - \p offset will be the physical address of the start of the register area,
1063 * - \p size will be the size of the register area bytes, and
1064 * - \p type will be DRM_REGISTERS.
1066 * The area mapped will be uncached.
1068 * \par Mapping the SAREA
1070 * - \p offset will be ignored and should be set to zero,
1071 * - \p size will be the desired size of the SAREA in bytes,
1072 * - \p type will be DRM_SHM.
1075 * A shared memory area of the requested size will be created and locked in
1076 * kernel memory. This area may be mapped into client-space by using the handle
1079 * \note May only be called by root.
1082 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1083 * the arguments in a drm_map structure.
1085 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1086 drmMapFlags flags, drm_handle_t *handle)
1091 map.offset = offset;
1095 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1098 *handle = (drm_handle_t)(uintptr_t)map.handle;
1102 drm_public int drmRmMap(int fd, drm_handle_t handle)
1107 map.handle = (void *)(uintptr_t)handle;
1109 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1115 * Make buffers available for DMA transfers.
1117 * \param fd file descriptor.
1118 * \param count number of buffers.
1119 * \param size size of each buffer.
1120 * \param flags buffer allocation flags.
1121 * \param agp_offset offset in the AGP aperture
1123 * \return number of buffers allocated, negative on error.
1126 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1130 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1133 drm_buf_desc_t request;
1136 request.count = count;
1137 request.size = size;
1138 request.flags = flags;
1139 request.agp_start = agp_offset;
1141 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1143 return request.count;
1146 drm_public int drmMarkBufs(int fd, double low, double high)
1148 drm_buf_info_t info;
1153 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1159 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1162 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1163 int retval = -errno;
1168 for (i = 0; i < info.count; i++) {
1169 info.list[i].low_mark = low * info.list[i].count;
1170 info.list[i].high_mark = high * info.list[i].count;
1171 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1172 int retval = -errno;
1185 * \param fd file descriptor.
1186 * \param count number of buffers to free.
1187 * \param list list of buffers to be freed.
1189 * \return zero on success, or a negative value on failure.
1191 * \note This function is primarily used for debugging.
1194 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1195 * the arguments in a drm_buf_free structure.
1197 drm_public int drmFreeBufs(int fd, int count, int *list)
1199 drm_buf_free_t request;
1202 request.count = count;
1203 request.list = list;
1204 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1213 * \param fd file descriptor.
1216 * This function closes the file descriptor.
1218 drm_public int drmClose(int fd)
1220 unsigned long key = drmGetKeyFromFd(fd);
1221 drmHashEntry *entry = drmGetEntry(fd);
1223 drmHashDestroy(entry->tagTable);
1226 entry->tagTable = NULL;
1228 drmHashDelete(drmHashTable, key);
1236 * Map a region of memory.
1238 * \param fd file descriptor.
1239 * \param handle handle returned by drmAddMap().
1240 * \param size size in bytes. Must match the size used by drmAddMap().
1241 * \param address will contain the user-space virtual address where the mapping
1244 * \return zero on success, or a negative value on failure.
1247 * This function is a wrapper for mmap().
1249 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1250 drmAddressPtr address)
1252 static unsigned long pagesize_mask = 0;
1258 pagesize_mask = getpagesize() - 1;
1260 size = (size + pagesize_mask) & ~pagesize_mask;
1262 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1263 if (*address == MAP_FAILED)
1270 * Unmap mappings obtained with drmMap().
1272 * \param address address as given by drmMap().
1273 * \param size size in bytes. Must match the size used by drmMap().
1275 * \return zero on success, or a negative value on failure.
1278 * This function is a wrapper for munmap().
1280 drm_public int drmUnmap(drmAddress address, drmSize size)
1282 return drm_munmap(address, size);
1285 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1287 drm_buf_info_t info;
1288 drmBufInfoPtr retval;
1293 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1297 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1300 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1305 retval = drmMalloc(sizeof(*retval));
1306 retval->count = info.count;
1307 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1308 for (i = 0; i < info.count; i++) {
1309 retval->list[i].count = info.list[i].count;
1310 retval->list[i].size = info.list[i].size;
1311 retval->list[i].low_mark = info.list[i].low_mark;
1312 retval->list[i].high_mark = info.list[i].high_mark;
1321 * Map all DMA buffers into client-virtual space.
1323 * \param fd file descriptor.
1325 * \return a pointer to a ::drmBufMap structure.
1327 * \note The client may not use these buffers until obtaining buffer indices
1331 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1332 * information about the buffers in a drm_buf_map structure into the
1333 * client-visible data structures.
1335 drm_public drmBufMapPtr drmMapBufs(int fd)
1338 drmBufMapPtr retval;
1342 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1348 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1351 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1356 retval = drmMalloc(sizeof(*retval));
1357 retval->count = bufs.count;
1358 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1359 for (i = 0; i < bufs.count; i++) {
1360 retval->list[i].idx = bufs.list[i].idx;
1361 retval->list[i].total = bufs.list[i].total;
1362 retval->list[i].used = 0;
1363 retval->list[i].address = bufs.list[i].address;
1372 * Unmap buffers allocated with drmMapBufs().
1374 * \return zero on success, or negative value on failure.
1377 * Calls munmap() for every buffer stored in \p bufs and frees the
1378 * memory allocated by drmMapBufs().
1380 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1384 for (i = 0; i < bufs->count; i++) {
1385 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1388 drmFree(bufs->list);
1394 #define DRM_DMA_RETRY 16
1397 * Reserve DMA buffers.
1399 * \param fd file descriptor.
1402 * \return zero on success, or a negative value on failure.
1405 * Assemble the arguments into a drm_dma structure and keeps issuing the
1406 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1408 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1413 dma.context = request->context;
1414 dma.send_count = request->send_count;
1415 dma.send_indices = request->send_list;
1416 dma.send_sizes = request->send_sizes;
1417 dma.flags = request->flags;
1418 dma.request_count = request->request_count;
1419 dma.request_size = request->request_size;
1420 dma.request_indices = request->request_list;
1421 dma.request_sizes = request->request_sizes;
1422 dma.granted_count = 0;
1425 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1426 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1429 request->granted_count = dma.granted_count;
1438 * Obtain heavyweight hardware lock.
1440 * \param fd file descriptor.
1441 * \param context context.
1442 * \param flags flags that determine the state of the hardware when the function
1445 * \return always zero.
1448 * This function translates the arguments into a drm_lock structure and issue
1449 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1451 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1456 lock.context = context;
1458 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1459 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1460 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1461 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1462 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1463 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1465 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1471 * Release the hardware lock.
1473 * \param fd file descriptor.
1474 * \param context context.
1476 * \return zero on success, or a negative value on failure.
1479 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1480 * argument in a drm_lock structure.
1482 drm_public int drmUnlock(int fd, drm_context_t context)
1487 lock.context = context;
1488 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1491 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1495 drm_context_t * retval;
1499 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1505 if (!(list = drmMalloc(res.count * sizeof(*list))))
1507 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1510 res.contexts = list;
1511 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1512 goto err_free_context;
1514 for (i = 0; i < res.count; i++)
1515 retval[i] = list[i].handle;
1528 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1536 * Used by the X server during GLXContext initialization. This causes
1537 * per-context kernel-level resources to be allocated.
1539 * \param fd file descriptor.
1540 * \param handle is set on success. To be used by the client when requesting DMA
1541 * dispatch with drmDMA().
1543 * \return zero on success, or a negative value on failure.
1545 * \note May only be called by root.
1548 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1549 * argument in a drm_ctx structure.
1551 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1556 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1558 *handle = ctx.handle;
1562 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1567 ctx.handle = context;
1568 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1573 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1574 drm_context_tFlags flags)
1579 * Context preserving means that no context switches are done between DMA
1580 * buffers from one context and the next. This is suitable for use in the
1581 * X server (which promises to maintain hardware context), or in the
1582 * client-side library when buffers are swapped on behalf of two threads.
1585 ctx.handle = context;
1586 if (flags & DRM_CONTEXT_PRESERVED)
1587 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1588 if (flags & DRM_CONTEXT_2DONLY)
1589 ctx.flags |= _DRM_CONTEXT_2DONLY;
1590 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1595 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1596 drm_context_tFlagsPtr flags)
1601 ctx.handle = context;
1602 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1605 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1606 *flags |= DRM_CONTEXT_PRESERVED;
1607 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1608 *flags |= DRM_CONTEXT_2DONLY;
1615 * Free any kernel-level resources allocated with drmCreateContext() associated
1618 * \param fd file descriptor.
1619 * \param handle handle given by drmCreateContext().
1621 * \return zero on success, or a negative value on failure.
1623 * \note May only be called by root.
1626 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1627 * argument in a drm_ctx structure.
1629 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1634 ctx.handle = handle;
1635 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1640 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1645 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1647 *handle = draw.handle;
1651 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1656 draw.handle = handle;
1657 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1662 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1663 drm_drawable_info_type_t type,
1664 unsigned int num, void *data)
1666 drm_update_draw_t update;
1669 update.handle = handle;
1672 update.data = (unsigned long long)(unsigned long)data;
1674 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1680 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1683 struct drm_crtc_get_sequence get_seq;
1687 get_seq.crtc_id = crtcId;
1688 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1693 *sequence = get_seq.sequence;
1695 *ns = get_seq.sequence_ns;
1699 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1701 uint64_t *sequence_queued,
1704 struct drm_crtc_queue_sequence queue_seq;
1707 memclear(queue_seq);
1708 queue_seq.crtc_id = crtcId;
1709 queue_seq.flags = flags;
1710 queue_seq.sequence = sequence;
1711 queue_seq.user_data = user_data;
1713 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1714 if (ret == 0 && sequence_queued)
1715 *sequence_queued = queue_seq.sequence;
1721 * Acquire the AGP device.
1723 * Must be called before any of the other AGP related calls.
1725 * \param fd file descriptor.
1727 * \return zero on success, or a negative value on failure.
1730 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1732 drm_public int drmAgpAcquire(int fd)
1734 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1741 * Release the AGP device.
1743 * \param fd file descriptor.
1745 * \return zero on success, or a negative value on failure.
1748 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1750 drm_public int drmAgpRelease(int fd)
1752 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1761 * \param fd file descriptor.
1762 * \param mode AGP mode.
1764 * \return zero on success, or a negative value on failure.
1767 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1768 * argument in a drm_agp_mode structure.
1770 drm_public int drmAgpEnable(int fd, unsigned long mode)
1776 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1783 * Allocate a chunk of AGP memory.
1785 * \param fd file descriptor.
1786 * \param size requested memory size in bytes. Will be rounded to page boundary.
1787 * \param type type of memory to allocate.
1788 * \param address if not zero, will be set to the physical address of the
1790 * \param handle on success will be set to a handle of the allocated memory.
1792 * \return zero on success, or a negative value on failure.
1795 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1796 * arguments in a drm_agp_buffer structure.
1798 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1799 unsigned long *address, drm_handle_t *handle)
1804 *handle = DRM_AGP_NO_HANDLE;
1807 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1810 *address = b.physical;
1817 * Free a chunk of AGP memory.
1819 * \param fd file descriptor.
1820 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1822 * \return zero on success, or a negative value on failure.
1825 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1826 * argument in a drm_agp_buffer structure.
1828 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1834 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1841 * Bind a chunk of AGP memory.
1843 * \param fd file descriptor.
1844 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1845 * \param offset offset in bytes. It will round to page boundary.
1847 * \return zero on success, or a negative value on failure.
1850 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1851 * argument in a drm_agp_binding structure.
1853 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1855 drm_agp_binding_t b;
1860 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1867 * Unbind a chunk of AGP memory.
1869 * \param fd file descriptor.
1870 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1872 * \return zero on success, or a negative value on failure.
1875 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1876 * the argument in a drm_agp_binding structure.
1878 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1880 drm_agp_binding_t b;
1884 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1891 * Get AGP driver major version number.
1893 * \param fd file descriptor.
1895 * \return major version number on success, or a negative value on failure..
1898 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1899 * necessary information in a drm_agp_info structure.
1901 drm_public int drmAgpVersionMajor(int fd)
1907 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1909 return i.agp_version_major;
1914 * Get AGP driver minor version number.
1916 * \param fd file descriptor.
1918 * \return minor version number on success, or a negative value on failure.
1921 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1922 * necessary information in a drm_agp_info structure.
1924 drm_public int drmAgpVersionMinor(int fd)
1930 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1932 return i.agp_version_minor;
1939 * \param fd file descriptor.
1941 * \return mode on success, or zero on failure.
1944 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1945 * necessary information in a drm_agp_info structure.
1947 drm_public unsigned long drmAgpGetMode(int fd)
1953 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1960 * Get AGP aperture base.
1962 * \param fd file descriptor.
1964 * \return aperture base on success, zero 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 unsigned long drmAgpBase(int fd)
1976 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1978 return i.aperture_base;
1983 * Get AGP aperture size.
1985 * \param fd file descriptor.
1987 * \return aperture size on success, zero 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 unsigned long drmAgpSize(int fd)
1999 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2001 return i.aperture_size;
2006 * Get used AGP memory.
2008 * \param fd file descriptor.
2010 * \return memory used 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 drmAgpMemoryUsed(int fd)
2022 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2024 return i.memory_used;
2029 * Get available AGP memory.
2031 * \param fd file descriptor.
2033 * \return memory available on success, or 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 drmAgpMemoryAvail(int fd)
2045 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2047 return i.memory_allowed;
2052 * Get hardware vendor ID.
2054 * \param fd file descriptor.
2056 * \return vendor ID on success, or 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 int drmAgpVendorId(int fd)
2068 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2075 * Get hardware device ID.
2077 * \param fd file descriptor.
2079 * \return zero 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 int drmAgpDeviceId(int fd)
2091 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2096 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2097 drm_handle_t *handle)
2099 drm_scatter_gather_t sg;
2105 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2107 *handle = sg.handle;
2111 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2113 drm_scatter_gather_t sg;
2117 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2125 * \param fd file descriptor.
2126 * \param vbl pointer to a drmVBlank structure.
2128 * \return zero on success, or a negative value on failure.
2131 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2133 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2135 struct timespec timeout, cur;
2138 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2140 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2146 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2147 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2148 if (ret && errno == EINTR) {
2149 clock_gettime(CLOCK_MONOTONIC, &cur);
2150 /* Timeout after 1s */
2151 if (cur.tv_sec > timeout.tv_sec + 1 ||
2152 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2159 } while (ret && errno == EINTR);
2165 drm_public int drmError(int err, const char *label)
2168 case DRM_ERR_NO_DEVICE:
2169 fprintf(stderr, "%s: no device\n", label);
2171 case DRM_ERR_NO_ACCESS:
2172 fprintf(stderr, "%s: no access\n", label);
2174 case DRM_ERR_NOT_ROOT:
2175 fprintf(stderr, "%s: not root\n", label);
2177 case DRM_ERR_INVALID:
2178 fprintf(stderr, "%s: invalid args\n", label);
2183 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2191 * Install IRQ handler.
2193 * \param fd file descriptor.
2194 * \param irq IRQ number.
2196 * \return zero on success, or a negative value on failure.
2199 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2200 * argument in a drm_control structure.
2202 drm_public int drmCtlInstHandler(int fd, int irq)
2207 ctl.func = DRM_INST_HANDLER;
2209 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2216 * Uninstall IRQ handler.
2218 * \param fd file descriptor.
2220 * \return zero on success, or a negative value on failure.
2223 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2224 * argument in a drm_control structure.
2226 drm_public int drmCtlUninstHandler(int fd)
2231 ctl.func = DRM_UNINST_HANDLER;
2233 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2238 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2243 lock.context = context;
2244 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2245 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2246 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2247 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2248 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2249 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2250 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2256 * Get IRQ from bus ID.
2258 * \param fd file descriptor.
2259 * \param busnum bus number.
2260 * \param devnum device number.
2261 * \param funcnum function number.
2263 * \return IRQ number on success, or a negative value on failure.
2266 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2267 * arguments in a drm_irq_busid structure.
2269 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2277 p.funcnum = funcnum;
2278 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2283 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2285 drmHashEntry *entry = drmGetEntry(fd);
2287 if (drmHashInsert(entry->tagTable, context, tag)) {
2288 drmHashDelete(entry->tagTable, context);
2289 drmHashInsert(entry->tagTable, context, tag);
2294 drm_public int drmDelContextTag(int fd, drm_context_t context)
2296 drmHashEntry *entry = drmGetEntry(fd);
2298 return drmHashDelete(entry->tagTable, context);
2301 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2303 drmHashEntry *entry = drmGetEntry(fd);
2306 if (drmHashLookup(entry->tagTable, context, &value))
2312 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2313 drm_handle_t handle)
2315 drm_ctx_priv_map_t map;
2318 map.ctx_id = ctx_id;
2319 map.handle = (void *)(uintptr_t)handle;
2321 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2326 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2327 drm_handle_t *handle)
2329 drm_ctx_priv_map_t map;
2332 map.ctx_id = ctx_id;
2334 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2337 *handle = (drm_handle_t)(uintptr_t)map.handle;
2342 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2343 drmMapType *type, drmMapFlags *flags,
2344 drm_handle_t *handle, int *mtrr)
2350 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2352 *offset = map.offset;
2356 *handle = (unsigned long)map.handle;
2361 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2362 unsigned long *magic, unsigned long *iocs)
2364 drm_client_t client;
2368 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2370 *auth = client.auth;
2373 *magic = client.magic;
2374 *iocs = client.iocs;
2378 drm_public int drmGetStats(int fd, drmStatsT *stats)
2384 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2388 memset(stats, 0, sizeof(*stats));
2389 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2393 stats->data[i].long_format = "%-20.20s"; \
2394 stats->data[i].rate_format = "%8.8s"; \
2395 stats->data[i].isvalue = 1; \
2396 stats->data[i].verbose = 0
2399 stats->data[i].long_format = "%-20.20s"; \
2400 stats->data[i].rate_format = "%5.5s"; \
2401 stats->data[i].isvalue = 0; \
2402 stats->data[i].mult_names = "kgm"; \
2403 stats->data[i].mult = 1000; \
2404 stats->data[i].verbose = 0
2407 stats->data[i].long_format = "%-20.20s"; \
2408 stats->data[i].rate_format = "%5.5s"; \
2409 stats->data[i].isvalue = 0; \
2410 stats->data[i].mult_names = "KGM"; \
2411 stats->data[i].mult = 1024; \
2412 stats->data[i].verbose = 0
2415 stats->count = s.count;
2416 for (i = 0; i < s.count; i++) {
2417 stats->data[i].value = s.data[i].value;
2418 switch (s.data[i].type) {
2419 case _DRM_STAT_LOCK:
2420 stats->data[i].long_name = "Lock";
2421 stats->data[i].rate_name = "Lock";
2424 case _DRM_STAT_OPENS:
2425 stats->data[i].long_name = "Opens";
2426 stats->data[i].rate_name = "O";
2428 stats->data[i].verbose = 1;
2430 case _DRM_STAT_CLOSES:
2431 stats->data[i].long_name = "Closes";
2432 stats->data[i].rate_name = "Lock";
2434 stats->data[i].verbose = 1;
2436 case _DRM_STAT_IOCTLS:
2437 stats->data[i].long_name = "Ioctls";
2438 stats->data[i].rate_name = "Ioc/s";
2441 case _DRM_STAT_LOCKS:
2442 stats->data[i].long_name = "Locks";
2443 stats->data[i].rate_name = "Lck/s";
2446 case _DRM_STAT_UNLOCKS:
2447 stats->data[i].long_name = "Unlocks";
2448 stats->data[i].rate_name = "Unl/s";
2452 stats->data[i].long_name = "IRQs";
2453 stats->data[i].rate_name = "IRQ/s";
2456 case _DRM_STAT_PRIMARY:
2457 stats->data[i].long_name = "Primary Bytes";
2458 stats->data[i].rate_name = "PB/s";
2461 case _DRM_STAT_SECONDARY:
2462 stats->data[i].long_name = "Secondary Bytes";
2463 stats->data[i].rate_name = "SB/s";
2467 stats->data[i].long_name = "DMA";
2468 stats->data[i].rate_name = "DMA/s";
2471 case _DRM_STAT_SPECIAL:
2472 stats->data[i].long_name = "Special DMA";
2473 stats->data[i].rate_name = "dma/s";
2476 case _DRM_STAT_MISSED:
2477 stats->data[i].long_name = "Miss";
2478 stats->data[i].rate_name = "Ms/s";
2481 case _DRM_STAT_VALUE:
2482 stats->data[i].long_name = "Value";
2483 stats->data[i].rate_name = "Value";
2486 case _DRM_STAT_BYTE:
2487 stats->data[i].long_name = "Bytes";
2488 stats->data[i].rate_name = "B/s";
2491 case _DRM_STAT_COUNT:
2493 stats->data[i].long_name = "Count";
2494 stats->data[i].rate_name = "Cnt/s";
2503 * Issue a set-version ioctl.
2505 * \param fd file descriptor.
2506 * \param drmCommandIndex command index
2507 * \param data source pointer of the data to be read and written.
2508 * \param size size of the data to be read and written.
2510 * \return zero on success, or a negative value on failure.
2513 * It issues a read-write ioctl given by
2514 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2516 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2519 drm_set_version_t sv;
2522 sv.drm_di_major = version->drm_di_major;
2523 sv.drm_di_minor = version->drm_di_minor;
2524 sv.drm_dd_major = version->drm_dd_major;
2525 sv.drm_dd_minor = version->drm_dd_minor;
2527 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2531 version->drm_di_major = sv.drm_di_major;
2532 version->drm_di_minor = sv.drm_di_minor;
2533 version->drm_dd_major = sv.drm_dd_major;
2534 version->drm_dd_minor = sv.drm_dd_minor;
2540 * Send a device-specific command.
2542 * \param fd file descriptor.
2543 * \param drmCommandIndex command index
2545 * \return zero on success, or a negative value on failure.
2548 * It issues a ioctl given by
2549 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2551 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2553 unsigned long request;
2555 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2557 if (drmIoctl(fd, request, NULL)) {
2565 * Send a device-specific read command.
2567 * \param fd file descriptor.
2568 * \param drmCommandIndex command index
2569 * \param data destination pointer of the data to be read.
2570 * \param size size of the data to be read.
2572 * \return zero on success, or a negative value on failure.
2575 * It issues a read ioctl given by
2576 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2578 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2579 void *data, unsigned long size)
2581 unsigned long request;
2583 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2584 DRM_COMMAND_BASE + drmCommandIndex, size);
2586 if (drmIoctl(fd, request, data)) {
2594 * Send a device-specific write command.
2596 * \param fd file descriptor.
2597 * \param drmCommandIndex command index
2598 * \param data source pointer of the data to be written.
2599 * \param size size of the data to be written.
2601 * \return zero on success, or a negative value on failure.
2604 * It issues a write ioctl given by
2605 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2607 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2608 void *data, unsigned long size)
2610 unsigned long request;
2612 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2613 DRM_COMMAND_BASE + drmCommandIndex, size);
2615 if (drmIoctl(fd, request, data)) {
2623 * Send a device-specific read-write command.
2625 * \param fd file descriptor.
2626 * \param drmCommandIndex command index
2627 * \param data source pointer of the data to be read and written.
2628 * \param size size of the data to be read and written.
2630 * \return zero on success, or a negative value on failure.
2633 * It issues a read-write ioctl given by
2634 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2636 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2637 void *data, unsigned long size)
2639 unsigned long request;
2641 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2642 DRM_COMMAND_BASE + drmCommandIndex, size);
2644 if (drmIoctl(fd, request, data))
2649 #define DRM_MAX_FDS 16
2655 } connection[DRM_MAX_FDS];
2657 static int nr_fds = 0;
2659 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2661 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2664 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2670 for (i = 0; i < nr_fds; i++)
2671 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2672 (connection[i].type == type)) {
2673 connection[i].refcount++;
2675 return connection[i].fd;
2678 fd = drmOpenWithType(NULL, BusID, type);
2679 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2682 connection[nr_fds].BusID = strdup(BusID);
2683 connection[nr_fds].fd = fd;
2684 connection[nr_fds].refcount = 1;
2685 connection[nr_fds].type = type;
2689 fprintf(stderr, "saved connection %d for %s %d\n",
2690 nr_fds, connection[nr_fds].BusID,
2691 strcmp(BusID, connection[nr_fds].BusID));
2698 drm_public void drmCloseOnce(int fd)
2702 for (i = 0; i < nr_fds; i++) {
2703 if (fd == connection[i].fd) {
2704 if (--connection[i].refcount == 0) {
2705 drmClose(connection[i].fd);
2706 free(connection[i].BusID);
2709 connection[i] = connection[nr_fds];
2717 drm_public int drmSetMaster(int fd)
2719 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2722 drm_public int drmDropMaster(int fd)
2724 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2727 drm_public int drmIsMaster(int fd)
2729 /* Detect master by attempting something that requires master.
2731 * Authenticating magic tokens requires master and 0 is an
2732 * internal kernel detail which we could use. Attempting this on
2733 * a master fd would fail therefore fail with EINVAL because 0
2736 * A non-master fd will fail with EACCES, as the kernel checks
2737 * for master before attempting to do anything else.
2739 * Since we don't want to leak implementation details, use
2742 return drmAuthMagic(fd, 0) != -EACCES;
2745 drm_public char *drmGetDeviceNameFromFd(int fd)
2752 /* The whole drmOpen thing is a fiasco and we need to find a way
2753 * back to just using open(2). For now, however, lets just make
2754 * things worse with even more ad hoc directory walking code to
2755 * discover the device file name. */
2760 for (i = 0; i < DRM_MAX_MINOR; i++) {
2761 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2762 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2765 if (i == DRM_MAX_MINOR)
2768 return strdup(name);
2771 static bool drmNodeIsDRM(int maj, int min)
2777 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2779 return stat(path, &sbuf) == 0;
2781 return maj == DRM_MAJOR;
2785 drm_public int drmGetNodeTypeFromFd(int fd)
2790 if (fstat(fd, &sbuf))
2793 maj = major(sbuf.st_rdev);
2794 min = minor(sbuf.st_rdev);
2796 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2801 type = drmGetMinorType(min);
2807 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2810 struct drm_prime_handle args;
2815 args.handle = handle;
2817 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2821 *prime_fd = args.fd;
2825 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2827 struct drm_prime_handle args;
2832 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2836 *handle = args.handle;
2840 static char *drmGetMinorNameForFD(int fd, int type)
2846 const char *name = drmGetMinorName(type);
2848 char dev_name[64], buf[64];
2856 if (fstat(fd, &sbuf))
2859 maj = major(sbuf.st_rdev);
2860 min = minor(sbuf.st_rdev);
2862 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2865 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2867 sysdir = opendir(buf);
2871 while ((ent = readdir(sysdir))) {
2872 if (strncmp(ent->d_name, name, len) == 0) {
2873 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2877 return strdup(dev_name);
2885 char buf[PATH_MAX + 1];
2886 const char *dev_name = drmGetDeviceName(type);
2887 unsigned int maj, min;
2890 if (fstat(fd, &sbuf))
2893 maj = major(sbuf.st_rdev);
2894 min = minor(sbuf.st_rdev);
2896 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2902 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
2903 if (n == -1 || n >= sizeof(buf))
2910 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2912 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2915 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2917 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2921 static char * DRM_PRINTFLIKE(2, 3)
2922 sysfs_uevent_get(const char *path, const char *fmt, ...)
2924 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2925 size_t size = 0, len;
2931 num = vasprintf(&key, fmt, ap);
2935 snprintf(filename, sizeof(filename), "%s/uevent", path);
2937 fp = fopen(filename, "r");
2943 while ((num = getline(&line, &size, fp)) >= 0) {
2944 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
2945 char *start = line + len + 1, *end = line + num - 1;
2950 value = strndup(start, end - start);
2964 /* Little white lie to avoid major rework of the existing code */
2965 #define DRM_BUS_VIRTIO 0x10
2967 static int drmParseSubsystemType(int maj, int min)
2970 char path[PATH_MAX + 1];
2971 char link[PATH_MAX + 1] = "";
2977 { "/pci", DRM_BUS_PCI },
2978 { "/usb", DRM_BUS_USB },
2979 { "/platform", DRM_BUS_PLATFORM },
2980 { "/spi", DRM_BUS_PLATFORM },
2981 { "/host1x", DRM_BUS_HOST1X },
2982 { "/virtio", DRM_BUS_VIRTIO },
2985 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2988 if (readlink(path, link, PATH_MAX) < 0)
2991 name = strrchr(link, '/');
2995 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
2996 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
2997 return bus_types[i].bus_type;
3001 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3004 #warning "Missing implementation of drmParseSubsystemType"
3010 get_pci_path(int maj, int min, char *pci_path)
3012 char path[PATH_MAX + 1], *term;
3014 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3015 if (!realpath(path, pci_path)) {
3016 strcpy(pci_path, path);
3020 term = strrchr(pci_path, '/');
3021 if (term && strncmp(term, "/virtio", 7) == 0)
3025 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3028 unsigned int domain, bus, dev, func;
3029 char pci_path[PATH_MAX + 1], *value;
3032 get_pci_path(maj, min, pci_path);
3034 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3038 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3044 info->domain = domain;
3050 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3051 struct drm_pciinfo pinfo;
3054 type = drmGetMinorType(min);
3058 fd = drmOpenMinor(min, 0, type);
3062 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3068 info->domain = pinfo.domain;
3069 info->bus = pinfo.bus;
3070 info->dev = pinfo.dev;
3071 info->func = pinfo.func;
3075 #warning "Missing implementation of drmParsePciBusInfo"
3080 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3082 if (a == NULL || b == NULL)
3085 if (a->bustype != b->bustype)
3088 switch (a->bustype) {
3090 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3093 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3095 case DRM_BUS_PLATFORM:
3096 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3098 case DRM_BUS_HOST1X:
3099 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3108 static int drmGetNodeType(const char *name)
3110 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3111 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3112 return DRM_NODE_CONTROL;
3114 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3115 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3116 return DRM_NODE_RENDER;
3118 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3119 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3120 return DRM_NODE_PRIMARY;
3125 static int drmGetMaxNodeName(void)
3127 return sizeof(DRM_DIR_NAME) +
3128 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3129 sizeof(DRM_CONTROL_MINOR_NAME),
3130 sizeof(DRM_RENDER_MINOR_NAME)) +
3131 3 /* length of the node number */;
3135 static int parse_separate_sysfs_files(int maj, int min,
3136 drmPciDeviceInfoPtr device,
3137 bool ignore_revision)
3139 static const char *attrs[] = {
3140 "revision", /* Older kernels are missing the file, so check for it first */
3146 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3147 unsigned int data[ARRAY_SIZE(attrs)];
3151 get_pci_path(maj, min, pci_path);
3153 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3154 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3155 fp = fopen(path, "r");
3159 ret = fscanf(fp, "%x", &data[i]);
3166 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3167 device->vendor_id = data[1] & 0xffff;
3168 device->device_id = data[2] & 0xffff;
3169 device->subvendor_id = data[3] & 0xffff;
3170 device->subdevice_id = data[4] & 0xffff;
3175 static int parse_config_sysfs_file(int maj, int min,
3176 drmPciDeviceInfoPtr device)
3178 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3179 unsigned char config[64];
3182 get_pci_path(maj, min, pci_path);
3184 snprintf(path, PATH_MAX, "%s/config", pci_path);
3185 fd = open(path, O_RDONLY);
3189 ret = read(fd, config, sizeof(config));
3194 device->vendor_id = config[0] | (config[1] << 8);
3195 device->device_id = config[2] | (config[3] << 8);
3196 device->revision_id = config[8];
3197 device->subvendor_id = config[44] | (config[45] << 8);
3198 device->subdevice_id = config[46] | (config[47] << 8);
3204 static int drmParsePciDeviceInfo(int maj, int min,
3205 drmPciDeviceInfoPtr device,
3209 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3210 return parse_separate_sysfs_files(maj, min, device, true);
3212 if (parse_separate_sysfs_files(maj, min, device, false))
3213 return parse_config_sysfs_file(maj, min, device);
3216 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3217 struct drm_pciinfo pinfo;
3220 type = drmGetMinorType(min);
3224 fd = drmOpenMinor(min, 0, type);
3228 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3234 device->vendor_id = pinfo.vendor_id;
3235 device->device_id = pinfo.device_id;
3236 device->revision_id = pinfo.revision_id;
3237 device->subvendor_id = pinfo.subvendor_id;
3238 device->subdevice_id = pinfo.subdevice_id;
3242 #warning "Missing implementation of drmParsePciDeviceInfo"
3247 static void drmFreePlatformDevice(drmDevicePtr device)
3249 if (device->deviceinfo.platform) {
3250 if (device->deviceinfo.platform->compatible) {
3251 char **compatible = device->deviceinfo.platform->compatible;
3253 while (*compatible) {
3258 free(device->deviceinfo.platform->compatible);
3263 static void drmFreeHost1xDevice(drmDevicePtr device)
3265 if (device->deviceinfo.host1x) {
3266 if (device->deviceinfo.host1x->compatible) {
3267 char **compatible = device->deviceinfo.host1x->compatible;
3269 while (*compatible) {
3274 free(device->deviceinfo.host1x->compatible);
3279 drm_public void drmFreeDevice(drmDevicePtr *device)
3285 switch ((*device)->bustype) {
3286 case DRM_BUS_PLATFORM:
3287 drmFreePlatformDevice(*device);
3290 case DRM_BUS_HOST1X:
3291 drmFreeHost1xDevice(*device);
3300 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3304 if (devices == NULL)
3307 for (i = 0; i < count; i++)
3309 drmFreeDevice(&devices[i]);
3312 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3313 size_t bus_size, size_t device_size,
3316 size_t max_node_length, extra, size;
3317 drmDevicePtr device;
3321 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3322 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3324 size = sizeof(*device) + extra + bus_size + device_size;
3326 device = calloc(1, size);
3330 device->available_nodes = 1 << type;
3332 ptr = (char *)device + sizeof(*device);
3333 device->nodes = (char **)ptr;
3335 ptr += DRM_NODE_MAX * sizeof(void *);
3337 for (i = 0; i < DRM_NODE_MAX; i++) {
3338 device->nodes[i] = ptr;
3339 ptr += max_node_length;
3342 memcpy(device->nodes[type], node, max_node_length);
3349 static int drmProcessPciDevice(drmDevicePtr *device,
3350 const char *node, int node_type,
3351 int maj, int min, bool fetch_deviceinfo,
3358 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3359 sizeof(drmPciDeviceInfo), &addr);
3363 dev->bustype = DRM_BUS_PCI;
3365 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3367 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3371 // Fetch the device info if the user has requested it
3372 if (fetch_deviceinfo) {
3373 addr += sizeof(drmPciBusInfo);
3374 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3376 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3390 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3393 char path[PATH_MAX + 1], *value;
3394 unsigned int bus, dev;
3397 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3399 value = sysfs_uevent_get(path, "BUSNUM");
3403 ret = sscanf(value, "%03u", &bus);
3409 value = sysfs_uevent_get(path, "DEVNUM");
3413 ret = sscanf(value, "%03u", &dev);
3424 #warning "Missing implementation of drmParseUsbBusInfo"
3429 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3432 char path[PATH_MAX + 1], *value;
3433 unsigned int vendor, product;
3436 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3438 value = sysfs_uevent_get(path, "PRODUCT");
3442 ret = sscanf(value, "%x/%x", &vendor, &product);
3448 info->vendor = vendor;
3449 info->product = product;
3453 #warning "Missing implementation of drmParseUsbDeviceInfo"
3458 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3459 int node_type, int maj, int min,
3460 bool fetch_deviceinfo, uint32_t flags)
3466 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3467 sizeof(drmUsbDeviceInfo), &ptr);
3471 dev->bustype = DRM_BUS_USB;
3473 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3475 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3479 if (fetch_deviceinfo) {
3480 ptr += sizeof(drmUsbBusInfo);
3481 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3483 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3497 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3500 char path[PATH_MAX + 1], *name, *tmp_name;
3502 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3504 name = sysfs_uevent_get(path, "OF_FULLNAME");
3507 /* If the device lacks OF data, pick the MODALIAS info */
3508 name = sysfs_uevent_get(path, "MODALIAS");
3512 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3513 tmp_name = strrchr(name, ':');
3521 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3522 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3527 #warning "Missing implementation of drmParseOFBusInfo"
3532 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3535 char path[PATH_MAX + 1], *value, *tmp_name;
3536 unsigned int count, i;
3539 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3541 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3543 sscanf(value, "%u", &count);
3546 /* Assume one entry if the device lack OF data */
3550 *compatible = calloc(count + 1, sizeof(char *));
3554 for (i = 0; i < count; i++) {
3555 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3558 /* If the device lacks OF data, pick the MODALIAS info */
3559 value = sysfs_uevent_get(path, "MODALIAS");
3565 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3566 tmp_name = strrchr(value, ':');
3571 tmp_name = strdup(tmp_name + 1);
3575 (*compatible)[i] = tmp_name;
3582 free((*compatible)[i]);
3587 #warning "Missing implementation of drmParseOFDeviceInfo"
3592 static int drmProcessPlatformDevice(drmDevicePtr *device,
3593 const char *node, int node_type,
3594 int maj, int min, bool fetch_deviceinfo,
3601 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3602 sizeof(drmPlatformDeviceInfo), &ptr);
3606 dev->bustype = DRM_BUS_PLATFORM;
3608 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3610 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3614 if (fetch_deviceinfo) {
3615 ptr += sizeof(drmPlatformBusInfo);
3616 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3618 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3632 static int drmProcessHost1xDevice(drmDevicePtr *device,
3633 const char *node, int node_type,
3634 int maj, int min, bool fetch_deviceinfo,
3641 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3642 sizeof(drmHost1xDeviceInfo), &ptr);
3646 dev->bustype = DRM_BUS_HOST1X;
3648 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3650 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3654 if (fetch_deviceinfo) {
3655 ptr += sizeof(drmHost1xBusInfo);
3656 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3658 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3673 process_device(drmDevicePtr *device, const char *d_name,
3674 int req_subsystem_type,
3675 bool fetch_deviceinfo, uint32_t flags)
3678 char node[PATH_MAX + 1];
3679 int node_type, subsystem_type;
3680 unsigned int maj, min;
3682 node_type = drmGetNodeType(d_name);
3686 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3687 if (stat(node, &sbuf))
3690 maj = major(sbuf.st_rdev);
3691 min = minor(sbuf.st_rdev);
3693 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3696 subsystem_type = drmParseSubsystemType(maj, min);
3697 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3700 switch (subsystem_type) {
3702 case DRM_BUS_VIRTIO:
3703 return drmProcessPciDevice(device, node, node_type, maj, min,
3704 fetch_deviceinfo, flags);
3706 return drmProcessUsbDevice(device, node, node_type, maj, min,
3707 fetch_deviceinfo, flags);
3708 case DRM_BUS_PLATFORM:
3709 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3710 fetch_deviceinfo, flags);
3711 case DRM_BUS_HOST1X:
3712 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3713 fetch_deviceinfo, flags);
3719 /* Consider devices located on the same bus as duplicate and fold the respective
3720 * entries into a single one.
3722 * Note: this leaves "gaps" in the array, while preserving the length.
3724 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3726 int node_type, i, j;
3728 for (i = 0; i < count; i++) {
3729 for (j = i + 1; j < count; j++) {
3730 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3731 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3732 node_type = log2(local_devices[j]->available_nodes);
3733 memcpy(local_devices[i]->nodes[node_type],
3734 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3735 drmFreeDevice(&local_devices[j]);
3741 /* Check that the given flags are valid returning 0 on success */
3743 drm_device_validate_flags(uint32_t flags)
3745 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3749 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3753 for (int i = 0; i < DRM_NODE_MAX; i++) {
3754 if (device->available_nodes & 1 << i) {
3755 if (stat(device->nodes[i], &sbuf) == 0 &&
3756 sbuf.st_rdev == find_rdev)
3764 * The kernel drm core has a number of places that assume maximum of
3765 * 3x64 devices nodes. That's 64 for each of primary, control and
3766 * render nodes. Rounded it up to 256 for simplicity.
3768 #define MAX_DRM_NODES 256
3771 * Get information about the opened drm device
3773 * \param fd file descriptor of the drm device
3774 * \param flags feature/behaviour bitmask
3775 * \param device the address of a drmDevicePtr where the information
3776 * will be allocated in stored
3778 * \return zero on success, negative error code otherwise.
3780 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3781 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3783 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3787 * DRI device nodes on OpenBSD are not in their own directory, they reside
3788 * in /dev along with a large number of statically generated /dev nodes.
3789 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3793 char node[PATH_MAX + 1];
3794 const char *dev_name;
3795 int node_type, subsystem_type;
3796 int maj, min, n, ret;
3798 if (fd == -1 || device == NULL)
3801 if (fstat(fd, &sbuf))
3804 maj = major(sbuf.st_rdev);
3805 min = minor(sbuf.st_rdev);
3807 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3810 node_type = drmGetMinorType(min);
3811 if (node_type == -1)
3814 dev_name = drmGetDeviceName(node_type);
3818 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
3819 if (n == -1 || n >= PATH_MAX)
3821 if (stat(node, &sbuf))
3824 subsystem_type = drmParseSubsystemType(maj, min);
3825 if (subsystem_type != DRM_BUS_PCI)
3828 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3836 drmDevicePtr local_devices[MAX_DRM_NODES];
3839 struct dirent *dent;
3843 int ret, i, node_count;
3846 if (drm_device_validate_flags(flags))
3849 if (fd == -1 || device == NULL)
3852 if (fstat(fd, &sbuf))
3855 find_rdev = sbuf.st_rdev;
3856 maj = major(sbuf.st_rdev);
3857 min = minor(sbuf.st_rdev);
3859 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3862 subsystem_type = drmParseSubsystemType(maj, min);
3863 if (subsystem_type < 0)
3864 return subsystem_type;
3866 sysdir = opendir(DRM_DIR_NAME);
3871 while ((dent = readdir(sysdir))) {
3872 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3876 if (i >= MAX_DRM_NODES) {
3877 fprintf(stderr, "More than %d drm nodes detected. "
3878 "Please report a bug - that should not happen.\n"
3879 "Skipping extra nodes\n", MAX_DRM_NODES);
3882 local_devices[i] = d;
3887 drmFoldDuplicatedDevices(local_devices, node_count);
3891 for (i = 0; i < node_count; i++) {
3892 if (!local_devices[i])
3895 if (drm_device_has_rdev(local_devices[i], find_rdev))
3896 *device = local_devices[i];
3898 drmFreeDevice(&local_devices[i]);
3902 if (*device == NULL)
3909 * Get information about the opened drm device
3911 * \param fd file descriptor of the drm device
3912 * \param device the address of a drmDevicePtr where the information
3913 * will be allocated in stored
3915 * \return zero on success, negative error code otherwise.
3917 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3919 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3923 * Get drm devices on the system
3925 * \param flags feature/behaviour bitmask
3926 * \param devices the array of devices with drmDevicePtr elements
3927 * can be NULL to get the device number first
3928 * \param max_devices the maximum number of devices for the array
3930 * \return on error - negative error code,
3931 * if devices is NULL - total number of devices available on the system,
3932 * alternatively the number of devices stored in devices[], which is
3933 * capped by the max_devices.
3935 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
3936 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3938 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
3941 drmDevicePtr local_devices[MAX_DRM_NODES];
3942 drmDevicePtr device;
3944 struct dirent *dent;
3945 int ret, i, node_count, device_count;
3947 if (drm_device_validate_flags(flags))
3950 sysdir = opendir(DRM_DIR_NAME);
3955 while ((dent = readdir(sysdir))) {
3956 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
3960 if (i >= MAX_DRM_NODES) {
3961 fprintf(stderr, "More than %d drm nodes detected. "
3962 "Please report a bug - that should not happen.\n"
3963 "Skipping extra nodes\n", MAX_DRM_NODES);
3966 local_devices[i] = device;
3971 drmFoldDuplicatedDevices(local_devices, node_count);
3974 for (i = 0; i < node_count; i++) {
3975 if (!local_devices[i])
3978 if ((devices != NULL) && (device_count < max_devices))
3979 devices[device_count] = local_devices[i];
3981 drmFreeDevice(&local_devices[i]);
3987 return device_count;
3991 * Get drm devices on the system
3993 * \param devices the array of devices with drmDevicePtr elements
3994 * can be NULL to get the device number first
3995 * \param max_devices the maximum number of devices for the array
3997 * \return on error - negative error code,
3998 * if devices is NULL - total number of devices available on the system,
3999 * alternatively the number of devices stored in devices[], which is
4000 * capped by the max_devices.
4002 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4004 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4007 drm_public char *drmGetDeviceNameFromFd2(int fd)
4011 char path[PATH_MAX + 1], *value;
4012 unsigned int maj, min;
4014 if (fstat(fd, &sbuf))
4017 maj = major(sbuf.st_rdev);
4018 min = minor(sbuf.st_rdev);
4020 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4023 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4025 value = sysfs_uevent_get(path, "DEVNAME");
4029 snprintf(path, sizeof(path), "/dev/%s", value);
4032 return strdup(path);
4035 char node[PATH_MAX + 1];
4036 const char *dev_name;
4040 if (fstat(fd, &sbuf))
4043 maj = major(sbuf.st_rdev);
4044 min = minor(sbuf.st_rdev);
4046 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4049 node_type = drmGetMinorType(min);
4050 if (node_type == -1)
4053 dev_name = drmGetDeviceName(node_type);
4057 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4058 if (n == -1 || n >= PATH_MAX)
4061 return strdup(node);
4065 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4067 struct drm_syncobj_create args;
4073 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4076 *handle = args.handle;
4080 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4082 struct drm_syncobj_destroy args;
4085 args.handle = handle;
4086 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4089 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4091 struct drm_syncobj_handle args;
4096 args.handle = handle;
4097 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4104 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4106 struct drm_syncobj_handle args;
4112 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4115 *handle = args.handle;
4119 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4122 struct drm_syncobj_handle args;
4125 args.fd = sync_file_fd;
4126 args.handle = handle;
4127 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4128 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4131 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4134 struct drm_syncobj_handle args;
4139 args.handle = handle;
4140 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4141 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4144 *sync_file_fd = args.fd;
4148 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4149 int64_t timeout_nsec, unsigned flags,
4150 uint32_t *first_signaled)
4152 struct drm_syncobj_wait args;
4156 args.handles = (uintptr_t)handles;
4157 args.timeout_nsec = timeout_nsec;
4158 args.count_handles = num_handles;
4161 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4166 *first_signaled = args.first_signaled;
4170 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4171 uint32_t handle_count)
4173 struct drm_syncobj_array args;
4177 args.handles = (uintptr_t)handles;
4178 args.count_handles = handle_count;
4180 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4184 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4185 uint32_t handle_count)
4187 struct drm_syncobj_array args;
4191 args.handles = (uintptr_t)handles;
4192 args.count_handles = handle_count;
4194 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4198 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4199 uint64_t *points, uint32_t handle_count)
4201 struct drm_syncobj_timeline_array args;
4205 args.handles = (uintptr_t)handles;
4206 args.points = (uintptr_t)points;
4207 args.count_handles = handle_count;
4209 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4213 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4214 unsigned num_handles,
4215 int64_t timeout_nsec, unsigned flags,
4216 uint32_t *first_signaled)
4218 struct drm_syncobj_timeline_wait args;
4222 args.handles = (uintptr_t)handles;
4223 args.points = (uintptr_t)points;
4224 args.timeout_nsec = timeout_nsec;
4225 args.count_handles = num_handles;
4228 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4233 *first_signaled = args.first_signaled;
4238 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4239 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_QUERY, &args);
4255 drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4256 uint32_t handle_count, uint32_t flags)
4258 struct drm_syncobj_timeline_array args;
4261 args.handles = (uintptr_t)handles;
4262 args.points = (uintptr_t)points;
4263 args.count_handles = handle_count;
4266 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4270 drm_public int drmSyncobjTransfer(int fd,
4271 uint32_t dst_handle, uint64_t dst_point,
4272 uint32_t src_handle, uint64_t src_point,
4275 struct drm_syncobj_transfer args;
4279 args.src_handle = src_handle;
4280 args.dst_handle = dst_handle;
4281 args.src_point = src_point;
4282 args.dst_point = dst_point;
4285 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);