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>
61 #include <sys/sysctl.h>
65 #if defined(__FreeBSD__)
66 #include <sys/param.h>
69 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
71 /* Not all systems have MAP_FAILED defined */
73 #define MAP_FAILED ((void *)-1)
77 #include "libdrm_macros.h"
79 #include "util_math.h"
95 #endif /* __OpenBSD__ */
98 #define DRM_MAJOR 226 /* Linux */
101 #if defined(__OpenBSD__) || defined(__DragonFly__)
109 uint16_t subvendor_id;
110 uint16_t subdevice_id;
114 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
117 #define DRM_MSG_VERBOSITY 3
119 #define memclear(s) memset(&s, 0, sizeof(s))
121 static drmServerInfoPtr drm_server_info;
123 static bool drmNodeIsDRM(int maj, int min);
125 drm_public void drmSetServerInfo(drmServerInfoPtr info)
127 drm_server_info = info;
131 * Output a message to stderr.
133 * \param format printf() like format string.
136 * This function is a wrapper around vfprintf().
139 static int DRM_PRINTFLIKE(1, 0)
140 drmDebugPrint(const char *format, va_list ap)
142 return vfprintf(stderr, format, ap);
146 drmMsg(const char *format, ...)
150 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
151 (drm_server_info && drm_server_info->debug_print))
153 va_start(ap, format);
154 if (drm_server_info) {
155 drm_server_info->debug_print(format,ap);
157 drmDebugPrint(format, ap);
163 static void *drmHashTable = NULL; /* Context switch callbacks */
165 drm_public void *drmGetHashTable(void)
170 drm_public void *drmMalloc(int size)
172 return calloc(1, size);
175 drm_public void drmFree(void *pt)
181 * Call ioctl, restarting if it is interrupted
184 drmIoctl(int fd, unsigned long request, void *arg)
189 ret = ioctl(fd, request, arg);
190 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
194 static unsigned long drmGetKeyFromFd(int fd)
203 drm_public drmHashEntry *drmGetEntry(int fd)
205 unsigned long key = drmGetKeyFromFd(fd);
210 drmHashTable = drmHashCreate();
212 if (drmHashLookup(drmHashTable, key, &value)) {
213 entry = drmMalloc(sizeof(*entry));
216 entry->tagTable = drmHashCreate();
217 drmHashInsert(drmHashTable, key, entry);
225 * Compare two busid strings
230 * \return 1 if matched.
233 * This function compares two bus ID strings. It understands the older
234 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
235 * domain, b is bus, d is device, f is function.
237 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
239 /* First, check if the IDs are exactly the same */
240 if (strcasecmp(id1, id2) == 0)
243 /* Try to match old/new-style PCI bus IDs. */
244 if (strncasecmp(id1, "pci", 3) == 0) {
245 unsigned int o1, b1, d1, f1;
246 unsigned int o2, b2, d2, f2;
249 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
252 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
257 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
260 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
265 /* If domains aren't properly supported by the kernel interface,
266 * just ignore them, which sucks less than picking a totally random
267 * card with "open by name"
272 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
281 * Handles error checking for chown call.
283 * \param path to file.
284 * \param id of the new owner.
285 * \param id of the new group.
287 * \return zero if success or -1 if failure.
290 * Checks for failure. If failure was caused by signal call chown again.
291 * If any other failure happened then it will output error message using
295 static int chown_check_return(const char *path, uid_t owner, gid_t group)
300 rv = chown(path, owner, group);
301 } while (rv != 0 && errno == EINTR);
306 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
307 path, errno, strerror(errno));
312 static const char *drmGetDeviceName(int type)
315 case DRM_NODE_PRIMARY:
317 case DRM_NODE_CONTROL:
318 return DRM_CONTROL_DEV_NAME;
319 case DRM_NODE_RENDER:
320 return DRM_RENDER_DEV_NAME;
326 * Open the DRM device, creating it if necessary.
328 * \param dev major and minor numbers of the device.
329 * \param minor minor number of the device.
331 * \return a file descriptor on success, or a negative value on error.
334 * Assembles the device name from \p minor and opens it, creating the device
335 * special file node with the major and minor numbers specified by \p dev and
336 * parent directory if necessary and was called by root.
338 static int drmOpenDevice(dev_t dev, int minor, int type)
341 const char *dev_name = drmGetDeviceName(type);
342 char buf[DRM_NODE_NAME_MAX];
344 mode_t devmode = DRM_DEV_MODE, serv_mode;
347 int isroot = !geteuid();
348 uid_t user = DRM_DEV_UID;
349 gid_t group = DRM_DEV_GID;
355 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
356 drmMsg("drmOpenDevice: node name is %s\n", buf);
358 if (drm_server_info && drm_server_info->get_perms) {
359 drm_server_info->get_perms(&serv_group, &serv_mode);
360 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
361 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
365 if (stat(DRM_DIR_NAME, &st)) {
367 return DRM_ERR_NOT_ROOT;
368 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
369 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
370 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
373 /* Check if the device node exists and create it if necessary. */
374 if (stat(buf, &st)) {
376 return DRM_ERR_NOT_ROOT;
378 mknod(buf, S_IFCHR | devmode, dev);
381 if (drm_server_info && drm_server_info->get_perms) {
382 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
383 chown_check_return(buf, user, group);
387 /* if we modprobed then wait for udev */
391 if (stat(DRM_DIR_NAME, &st)) {
395 if (udev_count == 50)
400 if (stat(buf, &st)) {
404 if (udev_count == 50)
411 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
412 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
413 fd, fd < 0 ? strerror(errno) : "OK");
418 /* Check if the device node is not what we expect it to be, and recreate it
419 * and try again if so.
421 if (st.st_rdev != dev) {
423 return DRM_ERR_NOT_ROOT;
425 mknod(buf, S_IFCHR | devmode, dev);
426 if (drm_server_info && drm_server_info->get_perms) {
427 chown_check_return(buf, user, group);
431 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
432 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
433 fd, fd < 0 ? strerror(errno) : "OK");
437 drmMsg("drmOpenDevice: Open failed\n");
445 * Open the DRM device
447 * \param minor device minor number.
448 * \param create allow to create the device if set.
450 * \return a file descriptor on success, or a negative value on error.
453 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
454 * name from \p minor and opens it.
456 static int drmOpenMinor(int minor, int create, int type)
459 char buf[DRM_NODE_NAME_MAX];
460 const char *dev_name = drmGetDeviceName(type);
463 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
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 major, int minor)
526 char name[SPECNAMELEN];
529 if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name)))
532 if (sscanf(name, "drm/%d", &id) != 1) {
533 // If not in /dev/drm/ we have the type in the name
534 if (sscanf(name, "dri/card%d\n", &id) >= 1)
535 return DRM_NODE_PRIMARY;
536 else if (sscanf(name, "dri/control%d\n", &id) >= 1)
537 return DRM_NODE_CONTROL;
538 else if (sscanf(name, "dri/renderD%d\n", &id) >= 1)
539 return DRM_NODE_RENDER;
545 int type = minor >> 6;
551 case DRM_NODE_PRIMARY:
552 case DRM_NODE_CONTROL:
553 case DRM_NODE_RENDER:
560 static const char *drmGetMinorName(int type)
563 case DRM_NODE_PRIMARY:
564 return DRM_PRIMARY_MINOR_NAME;
565 case DRM_NODE_CONTROL:
566 return DRM_CONTROL_MINOR_NAME;
567 case DRM_NODE_RENDER:
568 return DRM_RENDER_MINOR_NAME;
575 * Open the device by bus ID.
577 * \param busid bus ID.
578 * \param type device node type.
580 * \return a file descriptor on success, or a negative value on error.
583 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
584 * comparing the device bus ID with the one supplied.
586 * \sa drmOpenMinor() and drmGetBusid().
588 static int drmOpenByBusid(const char *busid, int type)
590 int i, pci_domain_ok = 1;
594 int base = drmGetMinorBase(type);
599 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
600 for (i = base; i < base + DRM_MAX_MINOR; i++) {
601 fd = drmOpenMinor(i, 1, type);
602 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
604 /* We need to try for 1.4 first for proper PCI domain support
605 * and if that fails, we know the kernel is busted
609 sv.drm_dd_major = -1; /* Don't care */
610 sv.drm_dd_minor = -1; /* Don't care */
611 if (drmSetInterfaceVersion(fd, &sv)) {
617 sv.drm_dd_major = -1; /* Don't care */
618 sv.drm_dd_minor = -1; /* Don't care */
619 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
620 drmSetInterfaceVersion(fd, &sv);
622 buf = drmGetBusid(fd);
623 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
624 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
638 * Open the device by name.
640 * \param name driver name.
641 * \param type the device node type.
643 * \return a file descriptor on success, or a negative value on error.
646 * This function opens the first minor number that matches the driver name and
647 * isn't already in use. If it's in use it then it will already have a bus ID
650 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
652 static int drmOpenByName(const char *name, int type)
656 drmVersionPtr version;
658 int base = drmGetMinorBase(type);
664 * Open the first minor number that matches the driver name and isn't
665 * already in use. If it's in use it will have a busid assigned already.
667 for (i = base; i < base + DRM_MAX_MINOR; i++) {
668 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
669 if ((version = drmGetVersion(fd))) {
670 if (!strcmp(version->name, name)) {
671 drmFreeVersion(version);
672 id = drmGetBusid(fd);
673 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
682 drmFreeVersion(version);
690 /* Backward-compatibility /proc support */
691 for (i = 0; i < 8; i++) {
692 char proc_name[64], buf[512];
693 char *driver, *pt, *devstring;
696 sprintf(proc_name, "/proc/dri/%d/name", i);
697 if ((fd = open(proc_name, 0, 0)) >= 0) {
698 retcode = read(fd, buf, sizeof(buf)-1);
701 buf[retcode-1] = '\0';
702 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
704 if (*pt) { /* Device is next */
706 if (!strcmp(driver, name)) { /* Match */
707 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
709 if (*pt) { /* Found busid */
710 return drmOpenByBusid(++pt, type);
711 } else { /* No busid */
712 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
726 * Open the DRM device.
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.
734 * \return a file descriptor on success, or a negative value on error.
737 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
740 drm_public int drmOpen(const char *name, const char *busid)
742 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
746 * Open the DRM device with specified type.
748 * Looks up the specified name and bus ID, and opens the device found. The
749 * entry in /dev/dri is created if necessary and if called by root.
751 * \param name driver name. Not referenced if bus ID is supplied.
752 * \param busid bus ID. Zero if not known.
753 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
755 * \return a file descriptor on success, or a negative value on error.
758 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
761 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
763 if (name != NULL && drm_server_info &&
764 drm_server_info->load_module && !drmAvailable()) {
765 /* try to load the kernel module */
766 if (!drm_server_info->load_module(name)) {
767 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
773 int fd = drmOpenByBusid(busid, type);
779 return drmOpenByName(name, type);
784 drm_public int drmOpenControl(int minor)
786 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
789 drm_public int drmOpenRender(int minor)
791 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
795 * Free the version information returned by drmGetVersion().
797 * \param v pointer to the version information.
800 * It frees the memory pointed by \p %v as well as all the non-null strings
803 drm_public void drmFreeVersion(drmVersionPtr v)
815 * Free the non-public version information returned by the kernel.
817 * \param v pointer to the version information.
820 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
821 * the non-null strings pointers in it.
823 static void drmFreeKernelVersion(drm_version_t *v)
835 * Copy version information.
837 * \param d destination pointer.
838 * \param s source pointer.
841 * Used by drmGetVersion() to translate the information returned by the ioctl
842 * interface in a private structure into the public structure counterpart.
844 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
846 d->version_major = s->version_major;
847 d->version_minor = s->version_minor;
848 d->version_patchlevel = s->version_patchlevel;
849 d->name_len = s->name_len;
850 d->name = strdup(s->name);
851 d->date_len = s->date_len;
852 d->date = strdup(s->date);
853 d->desc_len = s->desc_len;
854 d->desc = strdup(s->desc);
859 * Query the driver version information.
861 * \param fd file descriptor.
863 * \return pointer to a drmVersion structure which should be freed with
866 * \note Similar information is available via /proc/dri.
869 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
870 * first with zeros to get the string lengths, and then the actually strings.
871 * It also null-terminates them since they might not be already.
873 drm_public drmVersionPtr drmGetVersion(int fd)
875 drmVersionPtr retval;
876 drm_version_t *version = drmMalloc(sizeof(*version));
878 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
879 drmFreeKernelVersion(version);
883 if (version->name_len)
884 version->name = drmMalloc(version->name_len + 1);
885 if (version->date_len)
886 version->date = drmMalloc(version->date_len + 1);
887 if (version->desc_len)
888 version->desc = drmMalloc(version->desc_len + 1);
890 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
891 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
892 drmFreeKernelVersion(version);
896 /* The results might not be null-terminated strings, so terminate them. */
897 if (version->name_len) version->name[version->name_len] = '\0';
898 if (version->date_len) version->date[version->date_len] = '\0';
899 if (version->desc_len) version->desc[version->desc_len] = '\0';
901 retval = drmMalloc(sizeof(*retval));
902 drmCopyVersion(retval, version);
903 drmFreeKernelVersion(version);
909 * Get version information for the DRM user space library.
911 * This version number is driver independent.
913 * \param fd file descriptor.
915 * \return version information.
918 * This function allocates and fills a drm_version structure with a hard coded
921 drm_public drmVersionPtr drmGetLibVersion(int fd)
923 drm_version_t *version = drmMalloc(sizeof(*version));
926 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
927 * revision 1.0.x = original DRM interface with no drmGetLibVersion
928 * entry point and many drm<Device> extensions
929 * revision 1.1.x = added drmCommand entry points for device extensions
930 * added drmGetLibVersion to identify libdrm.a version
931 * revision 1.2.x = added drmSetInterfaceVersion
932 * modified drmOpen to handle both busid and name
933 * revision 1.3.x = added server + memory manager
935 version->version_major = 1;
936 version->version_minor = 3;
937 version->version_patchlevel = 0;
939 return (drmVersionPtr)version;
942 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
944 struct drm_get_cap cap;
948 cap.capability = capability;
950 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
958 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
960 struct drm_set_client_cap cap;
963 cap.capability = capability;
966 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
970 * Free the bus ID information.
972 * \param busid bus ID information string as given by drmGetBusid().
975 * This function is just frees the memory pointed by \p busid.
977 drm_public void drmFreeBusid(const char *busid)
979 drmFree((void *)busid);
984 * Get the bus ID of the device.
986 * \param fd file descriptor.
988 * \return bus ID string.
991 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
992 * get the string length and data, passing the arguments in a drm_unique
995 drm_public char *drmGetBusid(int fd)
1001 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
1003 u.unique = drmMalloc(u.unique_len + 1);
1004 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1008 u.unique[u.unique_len] = '\0';
1015 * Set the bus ID of the device.
1017 * \param fd file descriptor.
1018 * \param busid bus ID string.
1020 * \return zero on success, negative on failure.
1023 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1024 * the arguments in a drm_unique structure.
1026 drm_public int drmSetBusid(int fd, const char *busid)
1031 u.unique = (char *)busid;
1032 u.unique_len = strlen(busid);
1034 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1040 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1047 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1049 *magic = auth.magic;
1053 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1059 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1065 * Specifies a range of memory that is available for mapping by a
1068 * \param fd file descriptor.
1069 * \param offset usually the physical address. The actual meaning depends of
1070 * the \p type parameter. See below.
1071 * \param size of the memory in bytes.
1072 * \param type type of the memory to be mapped.
1073 * \param flags combination of several flags to modify the function actions.
1074 * \param handle will be set to a value that may be used as the offset
1075 * parameter for mmap().
1077 * \return zero on success or a negative value on error.
1079 * \par Mapping the frame buffer
1080 * For the frame buffer
1081 * - \p offset will be the physical address of the start of the frame buffer,
1082 * - \p size will be the size of the frame buffer in bytes, and
1083 * - \p type will be DRM_FRAME_BUFFER.
1086 * The area mapped will be uncached. If MTRR support is available in the
1087 * kernel, the frame buffer area will be set to write combining.
1089 * \par Mapping the MMIO register area
1090 * For the MMIO register area,
1091 * - \p offset will be the physical address of the start of the register area,
1092 * - \p size will be the size of the register area bytes, and
1093 * - \p type will be DRM_REGISTERS.
1095 * The area mapped will be uncached.
1097 * \par Mapping the SAREA
1099 * - \p offset will be ignored and should be set to zero,
1100 * - \p size will be the desired size of the SAREA in bytes,
1101 * - \p type will be DRM_SHM.
1104 * A shared memory area of the requested size will be created and locked in
1105 * kernel memory. This area may be mapped into client-space by using the handle
1108 * \note May only be called by root.
1111 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1112 * the arguments in a drm_map structure.
1114 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1115 drmMapFlags flags, drm_handle_t *handle)
1120 map.offset = offset;
1124 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1127 *handle = (drm_handle_t)(uintptr_t)map.handle;
1131 drm_public int drmRmMap(int fd, drm_handle_t handle)
1136 map.handle = (void *)(uintptr_t)handle;
1138 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1144 * Make buffers available for DMA transfers.
1146 * \param fd file descriptor.
1147 * \param count number of buffers.
1148 * \param size size of each buffer.
1149 * \param flags buffer allocation flags.
1150 * \param agp_offset offset in the AGP aperture
1152 * \return number of buffers allocated, negative on error.
1155 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1159 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1162 drm_buf_desc_t request;
1165 request.count = count;
1166 request.size = size;
1167 request.flags = flags;
1168 request.agp_start = agp_offset;
1170 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1172 return request.count;
1175 drm_public int drmMarkBufs(int fd, double low, double high)
1177 drm_buf_info_t info;
1182 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1188 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1191 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1192 int retval = -errno;
1197 for (i = 0; i < info.count; i++) {
1198 info.list[i].low_mark = low * info.list[i].count;
1199 info.list[i].high_mark = high * info.list[i].count;
1200 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1201 int retval = -errno;
1214 * \param fd file descriptor.
1215 * \param count number of buffers to free.
1216 * \param list list of buffers to be freed.
1218 * \return zero on success, or a negative value on failure.
1220 * \note This function is primarily used for debugging.
1223 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1224 * the arguments in a drm_buf_free structure.
1226 drm_public int drmFreeBufs(int fd, int count, int *list)
1228 drm_buf_free_t request;
1231 request.count = count;
1232 request.list = list;
1233 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1242 * \param fd file descriptor.
1245 * This function closes the file descriptor.
1247 drm_public int drmClose(int fd)
1249 unsigned long key = drmGetKeyFromFd(fd);
1250 drmHashEntry *entry = drmGetEntry(fd);
1252 drmHashDestroy(entry->tagTable);
1255 entry->tagTable = NULL;
1257 drmHashDelete(drmHashTable, key);
1265 * Map a region of memory.
1267 * \param fd file descriptor.
1268 * \param handle handle returned by drmAddMap().
1269 * \param size size in bytes. Must match the size used by drmAddMap().
1270 * \param address will contain the user-space virtual address where the mapping
1273 * \return zero on success, or a negative value on failure.
1276 * This function is a wrapper for mmap().
1278 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1279 drmAddressPtr address)
1281 static unsigned long pagesize_mask = 0;
1287 pagesize_mask = getpagesize() - 1;
1289 size = (size + pagesize_mask) & ~pagesize_mask;
1291 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1292 if (*address == MAP_FAILED)
1299 * Unmap mappings obtained with drmMap().
1301 * \param address address as given by drmMap().
1302 * \param size size in bytes. Must match the size used by drmMap().
1304 * \return zero on success, or a negative value on failure.
1307 * This function is a wrapper for munmap().
1309 drm_public int drmUnmap(drmAddress address, drmSize size)
1311 return drm_munmap(address, size);
1314 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1316 drm_buf_info_t info;
1317 drmBufInfoPtr retval;
1322 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1326 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1329 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1334 retval = drmMalloc(sizeof(*retval));
1335 retval->count = info.count;
1336 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1337 for (i = 0; i < info.count; i++) {
1338 retval->list[i].count = info.list[i].count;
1339 retval->list[i].size = info.list[i].size;
1340 retval->list[i].low_mark = info.list[i].low_mark;
1341 retval->list[i].high_mark = info.list[i].high_mark;
1350 * Map all DMA buffers into client-virtual space.
1352 * \param fd file descriptor.
1354 * \return a pointer to a ::drmBufMap structure.
1356 * \note The client may not use these buffers until obtaining buffer indices
1360 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1361 * information about the buffers in a drm_buf_map structure into the
1362 * client-visible data structures.
1364 drm_public drmBufMapPtr drmMapBufs(int fd)
1367 drmBufMapPtr retval;
1371 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1377 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1380 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1385 retval = drmMalloc(sizeof(*retval));
1386 retval->count = bufs.count;
1387 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1388 for (i = 0; i < bufs.count; i++) {
1389 retval->list[i].idx = bufs.list[i].idx;
1390 retval->list[i].total = bufs.list[i].total;
1391 retval->list[i].used = 0;
1392 retval->list[i].address = bufs.list[i].address;
1401 * Unmap buffers allocated with drmMapBufs().
1403 * \return zero on success, or negative value on failure.
1406 * Calls munmap() for every buffer stored in \p bufs and frees the
1407 * memory allocated by drmMapBufs().
1409 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1413 for (i = 0; i < bufs->count; i++) {
1414 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1417 drmFree(bufs->list);
1423 #define DRM_DMA_RETRY 16
1426 * Reserve DMA buffers.
1428 * \param fd file descriptor.
1431 * \return zero on success, or a negative value on failure.
1434 * Assemble the arguments into a drm_dma structure and keeps issuing the
1435 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1437 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1442 dma.context = request->context;
1443 dma.send_count = request->send_count;
1444 dma.send_indices = request->send_list;
1445 dma.send_sizes = request->send_sizes;
1446 dma.flags = request->flags;
1447 dma.request_count = request->request_count;
1448 dma.request_size = request->request_size;
1449 dma.request_indices = request->request_list;
1450 dma.request_sizes = request->request_sizes;
1451 dma.granted_count = 0;
1454 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1455 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1458 request->granted_count = dma.granted_count;
1467 * Obtain heavyweight hardware lock.
1469 * \param fd file descriptor.
1470 * \param context context.
1471 * \param flags flags that determine the state of the hardware when the function
1474 * \return always zero.
1477 * This function translates the arguments into a drm_lock structure and issue
1478 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1480 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1485 lock.context = context;
1487 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1488 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1489 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1490 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1491 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1492 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1494 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1500 * Release the hardware lock.
1502 * \param fd file descriptor.
1503 * \param context context.
1505 * \return zero on success, or a negative value on failure.
1508 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1509 * argument in a drm_lock structure.
1511 drm_public int drmUnlock(int fd, drm_context_t context)
1516 lock.context = context;
1517 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1520 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1524 drm_context_t * retval;
1528 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1534 if (!(list = drmMalloc(res.count * sizeof(*list))))
1536 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1539 res.contexts = list;
1540 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1541 goto err_free_context;
1543 for (i = 0; i < res.count; i++)
1544 retval[i] = list[i].handle;
1557 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1565 * Used by the X server during GLXContext initialization. This causes
1566 * per-context kernel-level resources to be allocated.
1568 * \param fd file descriptor.
1569 * \param handle is set on success. To be used by the client when requesting DMA
1570 * dispatch with drmDMA().
1572 * \return zero on success, or a negative value on failure.
1574 * \note May only be called by root.
1577 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1578 * argument in a drm_ctx structure.
1580 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1585 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1587 *handle = ctx.handle;
1591 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1596 ctx.handle = context;
1597 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1602 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1603 drm_context_tFlags flags)
1608 * Context preserving means that no context switches are done between DMA
1609 * buffers from one context and the next. This is suitable for use in the
1610 * X server (which promises to maintain hardware context), or in the
1611 * client-side library when buffers are swapped on behalf of two threads.
1614 ctx.handle = context;
1615 if (flags & DRM_CONTEXT_PRESERVED)
1616 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1617 if (flags & DRM_CONTEXT_2DONLY)
1618 ctx.flags |= _DRM_CONTEXT_2DONLY;
1619 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1624 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1625 drm_context_tFlagsPtr flags)
1630 ctx.handle = context;
1631 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1634 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1635 *flags |= DRM_CONTEXT_PRESERVED;
1636 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1637 *flags |= DRM_CONTEXT_2DONLY;
1644 * Free any kernel-level resources allocated with drmCreateContext() associated
1647 * \param fd file descriptor.
1648 * \param handle handle given by drmCreateContext().
1650 * \return zero on success, or a negative value on failure.
1652 * \note May only be called by root.
1655 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1656 * argument in a drm_ctx structure.
1658 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1663 ctx.handle = handle;
1664 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1669 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1674 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1676 *handle = draw.handle;
1680 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1685 draw.handle = handle;
1686 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1691 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1692 drm_drawable_info_type_t type,
1693 unsigned int num, void *data)
1695 drm_update_draw_t update;
1698 update.handle = handle;
1701 update.data = (unsigned long long)(unsigned long)data;
1703 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1709 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1712 struct drm_crtc_get_sequence get_seq;
1716 get_seq.crtc_id = crtcId;
1717 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1722 *sequence = get_seq.sequence;
1724 *ns = get_seq.sequence_ns;
1728 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1730 uint64_t *sequence_queued,
1733 struct drm_crtc_queue_sequence queue_seq;
1736 memclear(queue_seq);
1737 queue_seq.crtc_id = crtcId;
1738 queue_seq.flags = flags;
1739 queue_seq.sequence = sequence;
1740 queue_seq.user_data = user_data;
1742 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1743 if (ret == 0 && sequence_queued)
1744 *sequence_queued = queue_seq.sequence;
1750 * Acquire the AGP device.
1752 * Must be called before any of the other AGP related calls.
1754 * \param fd file descriptor.
1756 * \return zero on success, or a negative value on failure.
1759 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1761 drm_public int drmAgpAcquire(int fd)
1763 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1770 * Release the AGP device.
1772 * \param fd file descriptor.
1774 * \return zero on success, or a negative value on failure.
1777 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1779 drm_public int drmAgpRelease(int fd)
1781 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1790 * \param fd file descriptor.
1791 * \param mode AGP mode.
1793 * \return zero on success, or a negative value on failure.
1796 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1797 * argument in a drm_agp_mode structure.
1799 drm_public int drmAgpEnable(int fd, unsigned long mode)
1805 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1812 * Allocate a chunk of AGP memory.
1814 * \param fd file descriptor.
1815 * \param size requested memory size in bytes. Will be rounded to page boundary.
1816 * \param type type of memory to allocate.
1817 * \param address if not zero, will be set to the physical address of the
1819 * \param handle on success will be set to a handle of the allocated memory.
1821 * \return zero on success, or a negative value on failure.
1824 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1825 * arguments in a drm_agp_buffer structure.
1827 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1828 unsigned long *address, drm_handle_t *handle)
1833 *handle = DRM_AGP_NO_HANDLE;
1836 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1839 *address = b.physical;
1846 * Free a chunk of AGP memory.
1848 * \param fd file descriptor.
1849 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1851 * \return zero on success, or a negative value on failure.
1854 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1855 * argument in a drm_agp_buffer structure.
1857 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1863 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1870 * Bind a chunk of AGP memory.
1872 * \param fd file descriptor.
1873 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1874 * \param offset offset in bytes. It will round to page boundary.
1876 * \return zero on success, or a negative value on failure.
1879 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1880 * argument in a drm_agp_binding structure.
1882 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1884 drm_agp_binding_t b;
1889 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1896 * Unbind a chunk of AGP memory.
1898 * \param fd file descriptor.
1899 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1901 * \return zero on success, or a negative value on failure.
1904 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1905 * the argument in a drm_agp_binding structure.
1907 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1909 drm_agp_binding_t b;
1913 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1920 * Get AGP driver major version number.
1922 * \param fd file descriptor.
1924 * \return major version number on success, or a negative value on failure..
1927 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1928 * necessary information in a drm_agp_info structure.
1930 drm_public int drmAgpVersionMajor(int fd)
1936 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1938 return i.agp_version_major;
1943 * Get AGP driver minor version number.
1945 * \param fd file descriptor.
1947 * \return minor version number on success, or a negative value on failure.
1950 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1951 * necessary information in a drm_agp_info structure.
1953 drm_public int drmAgpVersionMinor(int fd)
1959 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1961 return i.agp_version_minor;
1968 * \param fd file descriptor.
1970 * \return mode on success, or zero on failure.
1973 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1974 * necessary information in a drm_agp_info structure.
1976 drm_public unsigned long drmAgpGetMode(int fd)
1982 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1989 * Get AGP aperture base.
1991 * \param fd file descriptor.
1993 * \return aperture base on success, zero on failure.
1996 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1997 * necessary information in a drm_agp_info structure.
1999 drm_public unsigned long drmAgpBase(int fd)
2005 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2007 return i.aperture_base;
2012 * Get AGP aperture size.
2014 * \param fd file descriptor.
2016 * \return aperture size on success, zero on failure.
2019 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2020 * necessary information in a drm_agp_info structure.
2022 drm_public unsigned long drmAgpSize(int fd)
2028 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2030 return i.aperture_size;
2035 * Get used AGP memory.
2037 * \param fd file descriptor.
2039 * \return memory used on success, or zero on failure.
2042 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2043 * necessary information in a drm_agp_info structure.
2045 drm_public unsigned long drmAgpMemoryUsed(int fd)
2051 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2053 return i.memory_used;
2058 * Get available AGP memory.
2060 * \param fd file descriptor.
2062 * \return memory available on success, or zero on failure.
2065 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2066 * necessary information in a drm_agp_info structure.
2068 drm_public unsigned long drmAgpMemoryAvail(int fd)
2074 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2076 return i.memory_allowed;
2081 * Get hardware vendor ID.
2083 * \param fd file descriptor.
2085 * \return vendor ID on success, or zero on failure.
2088 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2089 * necessary information in a drm_agp_info structure.
2091 drm_public unsigned int drmAgpVendorId(int fd)
2097 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2104 * Get hardware device ID.
2106 * \param fd file descriptor.
2108 * \return zero on success, or zero on failure.
2111 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2112 * necessary information in a drm_agp_info structure.
2114 drm_public unsigned int drmAgpDeviceId(int fd)
2120 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2125 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2126 drm_handle_t *handle)
2128 drm_scatter_gather_t sg;
2134 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2136 *handle = sg.handle;
2140 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2142 drm_scatter_gather_t sg;
2146 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2154 * \param fd file descriptor.
2155 * \param vbl pointer to a drmVBlank structure.
2157 * \return zero on success, or a negative value on failure.
2160 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2162 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2164 struct timespec timeout, cur;
2167 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2169 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2175 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2176 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2177 if (ret && errno == EINTR) {
2178 clock_gettime(CLOCK_MONOTONIC, &cur);
2179 /* Timeout after 1s */
2180 if (cur.tv_sec > timeout.tv_sec + 1 ||
2181 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2188 } while (ret && errno == EINTR);
2194 drm_public int drmError(int err, const char *label)
2197 case DRM_ERR_NO_DEVICE:
2198 fprintf(stderr, "%s: no device\n", label);
2200 case DRM_ERR_NO_ACCESS:
2201 fprintf(stderr, "%s: no access\n", label);
2203 case DRM_ERR_NOT_ROOT:
2204 fprintf(stderr, "%s: not root\n", label);
2206 case DRM_ERR_INVALID:
2207 fprintf(stderr, "%s: invalid args\n", label);
2212 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2220 * Install IRQ handler.
2222 * \param fd file descriptor.
2223 * \param irq IRQ number.
2225 * \return zero on success, or a negative value on failure.
2228 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2229 * argument in a drm_control structure.
2231 drm_public int drmCtlInstHandler(int fd, int irq)
2236 ctl.func = DRM_INST_HANDLER;
2238 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2245 * Uninstall IRQ handler.
2247 * \param fd file descriptor.
2249 * \return zero on success, or a negative value on failure.
2252 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2253 * argument in a drm_control structure.
2255 drm_public int drmCtlUninstHandler(int fd)
2260 ctl.func = DRM_UNINST_HANDLER;
2262 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2267 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2272 lock.context = context;
2273 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2274 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2275 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2276 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2277 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2278 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2279 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2285 * Get IRQ from bus ID.
2287 * \param fd file descriptor.
2288 * \param busnum bus number.
2289 * \param devnum device number.
2290 * \param funcnum function number.
2292 * \return IRQ number on success, or a negative value on failure.
2295 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2296 * arguments in a drm_irq_busid structure.
2298 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2306 p.funcnum = funcnum;
2307 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2312 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2314 drmHashEntry *entry = drmGetEntry(fd);
2316 if (drmHashInsert(entry->tagTable, context, tag)) {
2317 drmHashDelete(entry->tagTable, context);
2318 drmHashInsert(entry->tagTable, context, tag);
2323 drm_public int drmDelContextTag(int fd, drm_context_t context)
2325 drmHashEntry *entry = drmGetEntry(fd);
2327 return drmHashDelete(entry->tagTable, context);
2330 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2332 drmHashEntry *entry = drmGetEntry(fd);
2335 if (drmHashLookup(entry->tagTable, context, &value))
2341 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2342 drm_handle_t handle)
2344 drm_ctx_priv_map_t map;
2347 map.ctx_id = ctx_id;
2348 map.handle = (void *)(uintptr_t)handle;
2350 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2355 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2356 drm_handle_t *handle)
2358 drm_ctx_priv_map_t map;
2361 map.ctx_id = ctx_id;
2363 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2366 *handle = (drm_handle_t)(uintptr_t)map.handle;
2371 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2372 drmMapType *type, drmMapFlags *flags,
2373 drm_handle_t *handle, int *mtrr)
2379 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2381 *offset = map.offset;
2385 *handle = (unsigned long)map.handle;
2390 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2391 unsigned long *magic, unsigned long *iocs)
2393 drm_client_t client;
2397 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2399 *auth = client.auth;
2402 *magic = client.magic;
2403 *iocs = client.iocs;
2407 drm_public int drmGetStats(int fd, drmStatsT *stats)
2413 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2417 memset(stats, 0, sizeof(*stats));
2418 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2422 stats->data[i].long_format = "%-20.20s"; \
2423 stats->data[i].rate_format = "%8.8s"; \
2424 stats->data[i].isvalue = 1; \
2425 stats->data[i].verbose = 0
2428 stats->data[i].long_format = "%-20.20s"; \
2429 stats->data[i].rate_format = "%5.5s"; \
2430 stats->data[i].isvalue = 0; \
2431 stats->data[i].mult_names = "kgm"; \
2432 stats->data[i].mult = 1000; \
2433 stats->data[i].verbose = 0
2436 stats->data[i].long_format = "%-20.20s"; \
2437 stats->data[i].rate_format = "%5.5s"; \
2438 stats->data[i].isvalue = 0; \
2439 stats->data[i].mult_names = "KGM"; \
2440 stats->data[i].mult = 1024; \
2441 stats->data[i].verbose = 0
2444 stats->count = s.count;
2445 for (i = 0; i < s.count; i++) {
2446 stats->data[i].value = s.data[i].value;
2447 switch (s.data[i].type) {
2448 case _DRM_STAT_LOCK:
2449 stats->data[i].long_name = "Lock";
2450 stats->data[i].rate_name = "Lock";
2453 case _DRM_STAT_OPENS:
2454 stats->data[i].long_name = "Opens";
2455 stats->data[i].rate_name = "O";
2457 stats->data[i].verbose = 1;
2459 case _DRM_STAT_CLOSES:
2460 stats->data[i].long_name = "Closes";
2461 stats->data[i].rate_name = "Lock";
2463 stats->data[i].verbose = 1;
2465 case _DRM_STAT_IOCTLS:
2466 stats->data[i].long_name = "Ioctls";
2467 stats->data[i].rate_name = "Ioc/s";
2470 case _DRM_STAT_LOCKS:
2471 stats->data[i].long_name = "Locks";
2472 stats->data[i].rate_name = "Lck/s";
2475 case _DRM_STAT_UNLOCKS:
2476 stats->data[i].long_name = "Unlocks";
2477 stats->data[i].rate_name = "Unl/s";
2481 stats->data[i].long_name = "IRQs";
2482 stats->data[i].rate_name = "IRQ/s";
2485 case _DRM_STAT_PRIMARY:
2486 stats->data[i].long_name = "Primary Bytes";
2487 stats->data[i].rate_name = "PB/s";
2490 case _DRM_STAT_SECONDARY:
2491 stats->data[i].long_name = "Secondary Bytes";
2492 stats->data[i].rate_name = "SB/s";
2496 stats->data[i].long_name = "DMA";
2497 stats->data[i].rate_name = "DMA/s";
2500 case _DRM_STAT_SPECIAL:
2501 stats->data[i].long_name = "Special DMA";
2502 stats->data[i].rate_name = "dma/s";
2505 case _DRM_STAT_MISSED:
2506 stats->data[i].long_name = "Miss";
2507 stats->data[i].rate_name = "Ms/s";
2510 case _DRM_STAT_VALUE:
2511 stats->data[i].long_name = "Value";
2512 stats->data[i].rate_name = "Value";
2515 case _DRM_STAT_BYTE:
2516 stats->data[i].long_name = "Bytes";
2517 stats->data[i].rate_name = "B/s";
2520 case _DRM_STAT_COUNT:
2522 stats->data[i].long_name = "Count";
2523 stats->data[i].rate_name = "Cnt/s";
2532 * Issue a set-version ioctl.
2534 * \param fd file descriptor.
2535 * \param drmCommandIndex command index
2536 * \param data source pointer of the data to be read and written.
2537 * \param size size of the data to be read and written.
2539 * \return zero on success, or a negative value on failure.
2542 * It issues a read-write ioctl given by
2543 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2545 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2548 drm_set_version_t sv;
2551 sv.drm_di_major = version->drm_di_major;
2552 sv.drm_di_minor = version->drm_di_minor;
2553 sv.drm_dd_major = version->drm_dd_major;
2554 sv.drm_dd_minor = version->drm_dd_minor;
2556 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2560 version->drm_di_major = sv.drm_di_major;
2561 version->drm_di_minor = sv.drm_di_minor;
2562 version->drm_dd_major = sv.drm_dd_major;
2563 version->drm_dd_minor = sv.drm_dd_minor;
2569 * Send a device-specific command.
2571 * \param fd file descriptor.
2572 * \param drmCommandIndex command index
2574 * \return zero on success, or a negative value on failure.
2577 * It issues a ioctl given by
2578 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2580 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2582 unsigned long request;
2584 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2586 if (drmIoctl(fd, request, NULL)) {
2594 * Send a device-specific read command.
2596 * \param fd file descriptor.
2597 * \param drmCommandIndex command index
2598 * \param data destination pointer of the data to be read.
2599 * \param size size of the data to be read.
2601 * \return zero on success, or a negative value on failure.
2604 * It issues a read ioctl given by
2605 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2607 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2608 void *data, unsigned long size)
2610 unsigned long request;
2612 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2613 DRM_COMMAND_BASE + drmCommandIndex, size);
2615 if (drmIoctl(fd, request, data)) {
2623 * Send a device-specific write command.
2625 * \param fd file descriptor.
2626 * \param drmCommandIndex command index
2627 * \param data source pointer of the data to be written.
2628 * \param size size of the data to be written.
2630 * \return zero on success, or a negative value on failure.
2633 * It issues a write ioctl given by
2634 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2636 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2637 void *data, unsigned long size)
2639 unsigned long request;
2641 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2642 DRM_COMMAND_BASE + drmCommandIndex, size);
2644 if (drmIoctl(fd, request, data)) {
2652 * Send a device-specific read-write command.
2654 * \param fd file descriptor.
2655 * \param drmCommandIndex command index
2656 * \param data source pointer of the data to be read and written.
2657 * \param size size of the data to be read and written.
2659 * \return zero on success, or a negative value on failure.
2662 * It issues a read-write ioctl given by
2663 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2665 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2666 void *data, unsigned long size)
2668 unsigned long request;
2670 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2671 DRM_COMMAND_BASE + drmCommandIndex, size);
2673 if (drmIoctl(fd, request, data))
2678 #define DRM_MAX_FDS 16
2684 } connection[DRM_MAX_FDS];
2686 static int nr_fds = 0;
2688 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2690 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2693 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2699 for (i = 0; i < nr_fds; i++)
2700 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2701 (connection[i].type == type)) {
2702 connection[i].refcount++;
2704 return connection[i].fd;
2707 fd = drmOpenWithType(NULL, BusID, type);
2708 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2711 connection[nr_fds].BusID = strdup(BusID);
2712 connection[nr_fds].fd = fd;
2713 connection[nr_fds].refcount = 1;
2714 connection[nr_fds].type = type;
2718 fprintf(stderr, "saved connection %d for %s %d\n",
2719 nr_fds, connection[nr_fds].BusID,
2720 strcmp(BusID, connection[nr_fds].BusID));
2727 drm_public void drmCloseOnce(int fd)
2731 for (i = 0; i < nr_fds; i++) {
2732 if (fd == connection[i].fd) {
2733 if (--connection[i].refcount == 0) {
2734 drmClose(connection[i].fd);
2735 free(connection[i].BusID);
2738 connection[i] = connection[nr_fds];
2746 drm_public int drmSetMaster(int fd)
2748 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2751 drm_public int drmDropMaster(int fd)
2753 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2756 drm_public int drmIsMaster(int fd)
2758 /* Detect master by attempting something that requires master.
2760 * Authenticating magic tokens requires master and 0 is an
2761 * internal kernel detail which we could use. Attempting this on
2762 * a master fd would fail therefore fail with EINVAL because 0
2765 * A non-master fd will fail with EACCES, as the kernel checks
2766 * for master before attempting to do anything else.
2768 * Since we don't want to leak implementation details, use
2771 return drmAuthMagic(fd, 0) != -EACCES;
2774 drm_public char *drmGetDeviceNameFromFd(int fd)
2778 char dname[SPECNAMELEN];
2779 char name[SPECNAMELEN];
2782 if (fstat(fd, &sbuf))
2785 maj = major(sbuf.st_rdev);
2786 min = minor(sbuf.st_rdev);
2788 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2791 if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
2794 snprintf(name, sizeof(name), "/dev/%s", dname);
2795 return strdup(name);
2802 /* The whole drmOpen thing is a fiasco and we need to find a way
2803 * back to just using open(2). For now, however, lets just make
2804 * things worse with even more ad hoc directory walking code to
2805 * discover the device file name. */
2810 for (i = 0; i < DRM_MAX_MINOR; i++) {
2811 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2812 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2815 if (i == DRM_MAX_MINOR)
2818 return strdup(name);
2822 static bool drmNodeIsDRM(int maj, int min)
2828 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2830 return stat(path, &sbuf) == 0;
2832 char name[SPECNAMELEN];
2834 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
2836 /* Handle drm/ and dri/ as both are present in different FreeBSD version
2837 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2838 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2839 * only device nodes in /dev/dri/ */
2840 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
2842 return maj == DRM_MAJOR;
2846 drm_public int drmGetNodeTypeFromFd(int fd)
2851 if (fstat(fd, &sbuf))
2854 maj = major(sbuf.st_rdev);
2855 min = minor(sbuf.st_rdev);
2857 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2862 type = drmGetMinorType(maj, min);
2868 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2871 struct drm_prime_handle args;
2876 args.handle = handle;
2878 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2882 *prime_fd = args.fd;
2886 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2888 struct drm_prime_handle args;
2893 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2897 *handle = args.handle;
2901 static char *drmGetMinorNameForFD(int fd, int type)
2907 const char *name = drmGetMinorName(type);
2909 char dev_name[64], buf[64];
2917 if (fstat(fd, &sbuf))
2920 maj = major(sbuf.st_rdev);
2921 min = minor(sbuf.st_rdev);
2923 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2926 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2928 sysdir = opendir(buf);
2932 while ((ent = readdir(sysdir))) {
2933 if (strncmp(ent->d_name, name, len) == 0) {
2934 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2938 return strdup(dev_name);
2946 char dname[SPECNAMELEN];
2948 char name[SPECNAMELEN];
2951 if (fstat(fd, &sbuf))
2954 maj = major(sbuf.st_rdev);
2955 min = minor(sbuf.st_rdev);
2957 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2960 if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
2963 /* Handle both /dev/drm and /dev/dri
2964 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2965 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2966 * only device nodes in /dev/dri/ */
2967 mname = drmGetMinorName(type);
2968 if (sscanf(dname, "drm/%d", &id) != 1) {
2969 snprintf(name, sizeof(name), "dri/%s", mname);
2970 if (strncmp(name, dname, strlen(name)) != 0)
2972 snprintf(name, sizeof(name), "/dev/%s", dname);
2974 snprintf(name, sizeof(name), DRM_DIR_NAME "/%s%d", mname, id);
2975 return strdup(name);
2978 char buf[PATH_MAX + 1];
2979 const char *dev_name = drmGetDeviceName(type);
2980 unsigned int maj, min;
2983 if (fstat(fd, &sbuf))
2986 maj = major(sbuf.st_rdev);
2987 min = minor(sbuf.st_rdev);
2989 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2995 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
2996 if (n == -1 || n >= sizeof(buf))
3003 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
3005 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
3008 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
3010 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
3014 static char * DRM_PRINTFLIKE(2, 3)
3015 sysfs_uevent_get(const char *path, const char *fmt, ...)
3017 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
3018 size_t size = 0, len;
3024 num = vasprintf(&key, fmt, ap);
3028 snprintf(filename, sizeof(filename), "%s/uevent", path);
3030 fp = fopen(filename, "r");
3036 while ((num = getline(&line, &size, fp)) >= 0) {
3037 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3038 char *start = line + len + 1, *end = line + num - 1;
3043 value = strndup(start, end - start);
3057 /* Little white lie to avoid major rework of the existing code */
3058 #define DRM_BUS_VIRTIO 0x10
3061 static int get_subsystem_type(const char *device_path)
3063 char path[PATH_MAX + 1] = "";
3064 char link[PATH_MAX + 1] = "";
3070 { "/pci", DRM_BUS_PCI },
3071 { "/usb", DRM_BUS_USB },
3072 { "/platform", DRM_BUS_PLATFORM },
3073 { "/spi", DRM_BUS_PLATFORM },
3074 { "/host1x", DRM_BUS_HOST1X },
3075 { "/virtio", DRM_BUS_VIRTIO },
3078 strncpy(path, device_path, PATH_MAX);
3079 strncat(path, "/subsystem", PATH_MAX);
3081 if (readlink(path, link, PATH_MAX) < 0)
3084 name = strrchr(link, '/');
3088 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3089 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3090 return bus_types[i].bus_type;
3097 static int drmParseSubsystemType(int maj, int min)
3100 char path[PATH_MAX + 1] = "";
3101 char real_path[PATH_MAX + 1] = "";
3104 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3105 if (!realpath(path, real_path))
3107 snprintf(path, sizeof(path), "%s", real_path);
3109 subsystem_type = get_subsystem_type(path);
3110 if (subsystem_type == DRM_BUS_VIRTIO) {
3111 strncat(path, "/..", PATH_MAX);
3112 subsystem_type = get_subsystem_type(path);
3114 return subsystem_type;
3115 #elif defined(__OpenBSD__) || defined(__DragonFly__) || defined(__FreeBSD__)
3118 #warning "Missing implementation of drmParseSubsystemType"
3125 get_pci_path(int maj, int min, char *pci_path)
3127 char path[PATH_MAX + 1], *term;
3129 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3130 if (!realpath(path, pci_path)) {
3131 strcpy(pci_path, path);
3135 term = strrchr(pci_path, '/');
3136 if (term && strncmp(term, "/virtio", 7) == 0)
3142 static int get_sysctl_pci_bus_info(int maj, int min, drmPciBusInfoPtr info)
3144 char dname[SPECNAMELEN];
3145 char sysctl_name[16];
3146 char sysctl_val[256];
3148 int id, type, nelem;
3149 unsigned int rdev, majmin, domain, bus, dev, func;
3151 rdev = makedev(maj, min);
3152 if (!devname_r(rdev, S_IFCHR, dname, sizeof(dname)))
3155 if (sscanf(dname, "drm/%d\n", &id) != 1)
3157 type = drmGetMinorType(maj, min);
3161 /* BUG: This above section is iffy, since it mandates that a driver will
3162 * create both card and render node.
3163 * If it does not, the next DRM device will create card#X and
3164 * renderD#(128+X)-1.
3165 * This is a possibility in FreeBSD but for now there is no good way for
3166 * obtaining the info.
3169 case DRM_NODE_PRIMARY:
3171 case DRM_NODE_CONTROL:
3174 case DRM_NODE_RENDER:
3181 if (snprintf(sysctl_name, sizeof(sysctl_name), "hw.dri.%d.busid", id) <= 0)
3183 sysctl_len = sizeof(sysctl_val);
3184 if (sysctlbyname(sysctl_name, sysctl_val, &sysctl_len, NULL, 0))
3187 #define bus_fmt "pci:%04x:%02x:%02x.%u"
3189 nelem = sscanf(sysctl_val, bus_fmt, &domain, &bus, &dev, &func);
3192 info->domain = domain;
3201 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3204 unsigned int domain, bus, dev, func;
3205 char pci_path[PATH_MAX + 1], *value;
3208 get_pci_path(maj, min, pci_path);
3210 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3214 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3220 info->domain = domain;
3226 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3227 struct drm_pciinfo pinfo;
3230 type = drmGetMinorType(maj, min);
3234 fd = drmOpenMinor(min, 0, type);
3238 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3244 info->domain = pinfo.domain;
3245 info->bus = pinfo.bus;
3246 info->dev = pinfo.dev;
3247 info->func = pinfo.func;
3251 return get_sysctl_pci_bus_info(maj, min, info);
3253 #warning "Missing implementation of drmParsePciBusInfo"
3258 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3260 if (a == NULL || b == NULL)
3263 if (a->bustype != b->bustype)
3266 switch (a->bustype) {
3268 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3271 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3273 case DRM_BUS_PLATFORM:
3274 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3276 case DRM_BUS_HOST1X:
3277 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3286 static int drmGetNodeType(const char *name)
3288 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3289 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3290 return DRM_NODE_CONTROL;
3292 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3293 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3294 return DRM_NODE_RENDER;
3296 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3297 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3298 return DRM_NODE_PRIMARY;
3303 static int drmGetMaxNodeName(void)
3305 return sizeof(DRM_DIR_NAME) +
3306 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3307 sizeof(DRM_CONTROL_MINOR_NAME),
3308 sizeof(DRM_RENDER_MINOR_NAME)) +
3309 3 /* length of the node number */;
3313 static int parse_separate_sysfs_files(int maj, int min,
3314 drmPciDeviceInfoPtr device,
3315 bool ignore_revision)
3317 static const char *attrs[] = {
3318 "revision", /* Older kernels are missing the file, so check for it first */
3324 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3325 unsigned int data[ARRAY_SIZE(attrs)];
3329 get_pci_path(maj, min, pci_path);
3331 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3332 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3333 fp = fopen(path, "r");
3337 ret = fscanf(fp, "%x", &data[i]);
3344 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3345 device->vendor_id = data[1] & 0xffff;
3346 device->device_id = data[2] & 0xffff;
3347 device->subvendor_id = data[3] & 0xffff;
3348 device->subdevice_id = data[4] & 0xffff;
3353 static int parse_config_sysfs_file(int maj, int min,
3354 drmPciDeviceInfoPtr device)
3356 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3357 unsigned char config[64];
3360 get_pci_path(maj, min, pci_path);
3362 snprintf(path, PATH_MAX, "%s/config", pci_path);
3363 fd = open(path, O_RDONLY);
3367 ret = read(fd, config, sizeof(config));
3372 device->vendor_id = config[0] | (config[1] << 8);
3373 device->device_id = config[2] | (config[3] << 8);
3374 device->revision_id = config[8];
3375 device->subvendor_id = config[44] | (config[45] << 8);
3376 device->subdevice_id = config[46] | (config[47] << 8);
3382 static int drmParsePciDeviceInfo(int maj, int min,
3383 drmPciDeviceInfoPtr device,
3387 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3388 return parse_separate_sysfs_files(maj, min, device, true);
3390 if (parse_separate_sysfs_files(maj, min, device, false))
3391 return parse_config_sysfs_file(maj, min, device);
3394 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3395 struct drm_pciinfo pinfo;
3398 type = drmGetMinorType(maj, min);
3402 fd = drmOpenMinor(min, 0, type);
3406 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3412 device->vendor_id = pinfo.vendor_id;
3413 device->device_id = pinfo.device_id;
3414 device->revision_id = pinfo.revision_id;
3415 device->subvendor_id = pinfo.subvendor_id;
3416 device->subdevice_id = pinfo.subdevice_id;
3420 #warning "Missing implementation of drmParsePciDeviceInfo"
3425 static void drmFreePlatformDevice(drmDevicePtr device)
3427 if (device->deviceinfo.platform) {
3428 if (device->deviceinfo.platform->compatible) {
3429 char **compatible = device->deviceinfo.platform->compatible;
3431 while (*compatible) {
3436 free(device->deviceinfo.platform->compatible);
3441 static void drmFreeHost1xDevice(drmDevicePtr device)
3443 if (device->deviceinfo.host1x) {
3444 if (device->deviceinfo.host1x->compatible) {
3445 char **compatible = device->deviceinfo.host1x->compatible;
3447 while (*compatible) {
3452 free(device->deviceinfo.host1x->compatible);
3457 drm_public void drmFreeDevice(drmDevicePtr *device)
3463 switch ((*device)->bustype) {
3464 case DRM_BUS_PLATFORM:
3465 drmFreePlatformDevice(*device);
3468 case DRM_BUS_HOST1X:
3469 drmFreeHost1xDevice(*device);
3478 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3482 if (devices == NULL)
3485 for (i = 0; i < count; i++)
3487 drmFreeDevice(&devices[i]);
3490 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3491 size_t bus_size, size_t device_size,
3494 size_t max_node_length, extra, size;
3495 drmDevicePtr device;
3499 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3500 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3502 size = sizeof(*device) + extra + bus_size + device_size;
3504 device = calloc(1, size);
3508 device->available_nodes = 1 << type;
3510 ptr = (char *)device + sizeof(*device);
3511 device->nodes = (char **)ptr;
3513 ptr += DRM_NODE_MAX * sizeof(void *);
3515 for (i = 0; i < DRM_NODE_MAX; i++) {
3516 device->nodes[i] = ptr;
3517 ptr += max_node_length;
3520 memcpy(device->nodes[type], node, max_node_length);
3527 static int drmProcessPciDevice(drmDevicePtr *device,
3528 const char *node, int node_type,
3529 int maj, int min, bool fetch_deviceinfo,
3536 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3537 sizeof(drmPciDeviceInfo), &addr);
3541 dev->bustype = DRM_BUS_PCI;
3543 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3545 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3549 // Fetch the device info if the user has requested it
3550 if (fetch_deviceinfo) {
3551 addr += sizeof(drmPciBusInfo);
3552 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3554 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3568 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3571 char path[PATH_MAX + 1], *value;
3572 unsigned int bus, dev;
3575 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3577 value = sysfs_uevent_get(path, "BUSNUM");
3581 ret = sscanf(value, "%03u", &bus);
3587 value = sysfs_uevent_get(path, "DEVNUM");
3591 ret = sscanf(value, "%03u", &dev);
3602 #warning "Missing implementation of drmParseUsbBusInfo"
3607 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3610 char path[PATH_MAX + 1], *value;
3611 unsigned int vendor, product;
3614 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3616 value = sysfs_uevent_get(path, "PRODUCT");
3620 ret = sscanf(value, "%x/%x", &vendor, &product);
3626 info->vendor = vendor;
3627 info->product = product;
3631 #warning "Missing implementation of drmParseUsbDeviceInfo"
3636 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3637 int node_type, int maj, int min,
3638 bool fetch_deviceinfo, uint32_t flags)
3644 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3645 sizeof(drmUsbDeviceInfo), &ptr);
3649 dev->bustype = DRM_BUS_USB;
3651 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3653 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3657 if (fetch_deviceinfo) {
3658 ptr += sizeof(drmUsbBusInfo);
3659 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3661 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3675 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3678 char path[PATH_MAX + 1], *name, *tmp_name;
3680 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3682 name = sysfs_uevent_get(path, "OF_FULLNAME");
3685 /* If the device lacks OF data, pick the MODALIAS info */
3686 name = sysfs_uevent_get(path, "MODALIAS");
3690 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3691 tmp_name = strrchr(name, ':');
3699 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3700 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3705 #warning "Missing implementation of drmParseOFBusInfo"
3710 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3713 char path[PATH_MAX + 1], *value, *tmp_name;
3714 unsigned int count, i;
3717 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3719 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3721 sscanf(value, "%u", &count);
3724 /* Assume one entry if the device lack OF data */
3728 *compatible = calloc(count + 1, sizeof(char *));
3732 for (i = 0; i < count; i++) {
3733 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3736 /* If the device lacks OF data, pick the MODALIAS info */
3737 value = sysfs_uevent_get(path, "MODALIAS");
3743 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3744 tmp_name = strrchr(value, ':');
3749 tmp_name = strdup(tmp_name + 1);
3753 (*compatible)[i] = tmp_name;
3760 free((*compatible)[i]);
3765 #warning "Missing implementation of drmParseOFDeviceInfo"
3770 static int drmProcessPlatformDevice(drmDevicePtr *device,
3771 const char *node, int node_type,
3772 int maj, int min, bool fetch_deviceinfo,
3779 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3780 sizeof(drmPlatformDeviceInfo), &ptr);
3784 dev->bustype = DRM_BUS_PLATFORM;
3786 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3788 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3792 if (fetch_deviceinfo) {
3793 ptr += sizeof(drmPlatformBusInfo);
3794 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3796 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3810 static int drmProcessHost1xDevice(drmDevicePtr *device,
3811 const char *node, int node_type,
3812 int maj, int min, bool fetch_deviceinfo,
3819 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3820 sizeof(drmHost1xDeviceInfo), &ptr);
3824 dev->bustype = DRM_BUS_HOST1X;
3826 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3828 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3832 if (fetch_deviceinfo) {
3833 ptr += sizeof(drmHost1xBusInfo);
3834 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3836 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3851 process_device(drmDevicePtr *device, const char *d_name,
3852 int req_subsystem_type,
3853 bool fetch_deviceinfo, uint32_t flags)
3856 char node[PATH_MAX + 1];
3857 int node_type, subsystem_type;
3858 unsigned int maj, min;
3860 node_type = drmGetNodeType(d_name);
3864 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3865 if (stat(node, &sbuf))
3868 maj = major(sbuf.st_rdev);
3869 min = minor(sbuf.st_rdev);
3871 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3874 subsystem_type = drmParseSubsystemType(maj, min);
3875 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3878 switch (subsystem_type) {
3880 return drmProcessPciDevice(device, node, node_type, maj, min,
3881 fetch_deviceinfo, flags);
3883 return drmProcessUsbDevice(device, node, node_type, maj, min,
3884 fetch_deviceinfo, flags);
3885 case DRM_BUS_PLATFORM:
3886 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3887 fetch_deviceinfo, flags);
3888 case DRM_BUS_HOST1X:
3889 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3890 fetch_deviceinfo, flags);
3896 /* Consider devices located on the same bus as duplicate and fold the respective
3897 * entries into a single one.
3899 * Note: this leaves "gaps" in the array, while preserving the length.
3901 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3903 int node_type, i, j;
3905 for (i = 0; i < count; i++) {
3906 for (j = i + 1; j < count; j++) {
3907 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3908 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3909 node_type = log2(local_devices[j]->available_nodes);
3910 memcpy(local_devices[i]->nodes[node_type],
3911 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3912 drmFreeDevice(&local_devices[j]);
3918 /* Check that the given flags are valid returning 0 on success */
3920 drm_device_validate_flags(uint32_t flags)
3922 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3926 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3930 for (int i = 0; i < DRM_NODE_MAX; i++) {
3931 if (device->available_nodes & 1 << i) {
3932 if (stat(device->nodes[i], &sbuf) == 0 &&
3933 sbuf.st_rdev == find_rdev)
3941 * The kernel drm core has a number of places that assume maximum of
3942 * 3x64 devices nodes. That's 64 for each of primary, control and
3943 * render nodes. Rounded it up to 256 for simplicity.
3945 #define MAX_DRM_NODES 256
3948 * Get information about the opened drm device
3950 * \param fd file descriptor of the drm device
3951 * \param flags feature/behaviour bitmask
3952 * \param device the address of a drmDevicePtr where the information
3953 * will be allocated in stored
3955 * \return zero on success, negative error code otherwise.
3957 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3958 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3960 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3964 * DRI device nodes on OpenBSD are not in their own directory, they reside
3965 * in /dev along with a large number of statically generated /dev nodes.
3966 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3970 char node[PATH_MAX + 1];
3971 const char *dev_name;
3972 int node_type, subsystem_type;
3973 int maj, min, n, ret;
3975 if (fd == -1 || device == NULL)
3978 if (fstat(fd, &sbuf))
3981 maj = major(sbuf.st_rdev);
3982 min = minor(sbuf.st_rdev);
3984 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3987 node_type = drmGetMinorType(maj, min);
3988 if (node_type == -1)
3991 dev_name = drmGetDeviceName(node_type);
3995 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
3996 if (n == -1 || n >= PATH_MAX)
3998 if (stat(node, &sbuf))
4001 subsystem_type = drmParseSubsystemType(maj, min);
4002 if (subsystem_type != DRM_BUS_PCI)
4005 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
4013 drmDevicePtr local_devices[MAX_DRM_NODES];
4016 struct dirent *dent;
4020 int ret, i, node_count;
4023 if (drm_device_validate_flags(flags))
4026 if (fd == -1 || device == NULL)
4029 if (fstat(fd, &sbuf))
4032 find_rdev = sbuf.st_rdev;
4033 maj = major(sbuf.st_rdev);
4034 min = minor(sbuf.st_rdev);
4036 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4039 subsystem_type = drmParseSubsystemType(maj, min);
4040 if (subsystem_type < 0)
4041 return subsystem_type;
4043 sysdir = opendir(DRM_DIR_NAME);
4048 while ((dent = readdir(sysdir))) {
4049 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
4053 if (i >= MAX_DRM_NODES) {
4054 fprintf(stderr, "More than %d drm nodes detected. "
4055 "Please report a bug - that should not happen.\n"
4056 "Skipping extra nodes\n", MAX_DRM_NODES);
4059 local_devices[i] = d;
4064 drmFoldDuplicatedDevices(local_devices, node_count);
4068 for (i = 0; i < node_count; i++) {
4069 if (!local_devices[i])
4072 if (drm_device_has_rdev(local_devices[i], find_rdev))
4073 *device = local_devices[i];
4075 drmFreeDevice(&local_devices[i]);
4079 if (*device == NULL)
4086 * Get information about the opened drm device
4088 * \param fd file descriptor of the drm device
4089 * \param device the address of a drmDevicePtr where the information
4090 * will be allocated in stored
4092 * \return zero on success, negative error code otherwise.
4094 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
4096 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
4100 * Get drm devices on the system
4102 * \param flags feature/behaviour bitmask
4103 * \param devices the array of devices with drmDevicePtr elements
4104 * can be NULL to get the device number first
4105 * \param max_devices the maximum number of devices for the array
4107 * \return on error - negative error code,
4108 * if devices is NULL - total number of devices available on the system,
4109 * alternatively the number of devices stored in devices[], which is
4110 * capped by the max_devices.
4112 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4113 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4115 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4118 drmDevicePtr local_devices[MAX_DRM_NODES];
4119 drmDevicePtr device;
4121 struct dirent *dent;
4122 int ret, i, node_count, device_count;
4124 if (drm_device_validate_flags(flags))
4127 sysdir = opendir(DRM_DIR_NAME);
4132 while ((dent = readdir(sysdir))) {
4133 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4137 if (i >= MAX_DRM_NODES) {
4138 fprintf(stderr, "More than %d drm nodes detected. "
4139 "Please report a bug - that should not happen.\n"
4140 "Skipping extra nodes\n", MAX_DRM_NODES);
4143 local_devices[i] = device;
4148 drmFoldDuplicatedDevices(local_devices, node_count);
4151 for (i = 0; i < node_count; i++) {
4152 if (!local_devices[i])
4155 if ((devices != NULL) && (device_count < max_devices))
4156 devices[device_count] = local_devices[i];
4158 drmFreeDevice(&local_devices[i]);
4164 return device_count;
4168 * Get drm devices on the system
4170 * \param devices the array of devices with drmDevicePtr elements
4171 * can be NULL to get the device number first
4172 * \param max_devices the maximum number of devices for the array
4174 * \return on error - negative error code,
4175 * if devices is NULL - total number of devices available on the system,
4176 * alternatively the number of devices stored in devices[], which is
4177 * capped by the max_devices.
4179 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4181 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4184 drm_public char *drmGetDeviceNameFromFd2(int fd)
4188 char path[PATH_MAX + 1], *value;
4189 unsigned int maj, min;
4191 if (fstat(fd, &sbuf))
4194 maj = major(sbuf.st_rdev);
4195 min = minor(sbuf.st_rdev);
4197 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4200 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4202 value = sysfs_uevent_get(path, "DEVNAME");
4206 snprintf(path, sizeof(path), "/dev/%s", value);
4209 return strdup(path);
4211 return drmGetDeviceNameFromFd(fd);
4214 char node[PATH_MAX + 1];
4215 const char *dev_name;
4219 if (fstat(fd, &sbuf))
4222 maj = major(sbuf.st_rdev);
4223 min = minor(sbuf.st_rdev);
4225 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4228 node_type = drmGetMinorType(maj, min);
4229 if (node_type == -1)
4232 dev_name = drmGetDeviceName(node_type);
4236 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4237 if (n == -1 || n >= PATH_MAX)
4240 return strdup(node);
4244 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4246 struct drm_syncobj_create args;
4252 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4255 *handle = args.handle;
4259 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4261 struct drm_syncobj_destroy args;
4264 args.handle = handle;
4265 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4268 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4270 struct drm_syncobj_handle args;
4275 args.handle = handle;
4276 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4283 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4285 struct drm_syncobj_handle args;
4291 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4294 *handle = args.handle;
4298 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4301 struct drm_syncobj_handle args;
4304 args.fd = sync_file_fd;
4305 args.handle = handle;
4306 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4307 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4310 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4313 struct drm_syncobj_handle args;
4318 args.handle = handle;
4319 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4320 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4323 *sync_file_fd = args.fd;
4327 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4328 int64_t timeout_nsec, unsigned flags,
4329 uint32_t *first_signaled)
4331 struct drm_syncobj_wait args;
4335 args.handles = (uintptr_t)handles;
4336 args.timeout_nsec = timeout_nsec;
4337 args.count_handles = num_handles;
4340 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4345 *first_signaled = args.first_signaled;
4349 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4350 uint32_t handle_count)
4352 struct drm_syncobj_array args;
4356 args.handles = (uintptr_t)handles;
4357 args.count_handles = handle_count;
4359 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4363 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4364 uint32_t handle_count)
4366 struct drm_syncobj_array args;
4370 args.handles = (uintptr_t)handles;
4371 args.count_handles = handle_count;
4373 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4377 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4378 uint64_t *points, uint32_t handle_count)
4380 struct drm_syncobj_timeline_array args;
4384 args.handles = (uintptr_t)handles;
4385 args.points = (uintptr_t)points;
4386 args.count_handles = handle_count;
4388 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4392 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4393 unsigned num_handles,
4394 int64_t timeout_nsec, unsigned flags,
4395 uint32_t *first_signaled)
4397 struct drm_syncobj_timeline_wait args;
4401 args.handles = (uintptr_t)handles;
4402 args.points = (uintptr_t)points;
4403 args.timeout_nsec = timeout_nsec;
4404 args.count_handles = num_handles;
4407 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4412 *first_signaled = args.first_signaled;
4417 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4418 uint32_t handle_count)
4420 struct drm_syncobj_timeline_array args;
4424 args.handles = (uintptr_t)handles;
4425 args.points = (uintptr_t)points;
4426 args.count_handles = handle_count;
4428 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4434 drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4435 uint32_t handle_count, uint32_t flags)
4437 struct drm_syncobj_timeline_array args;
4440 args.handles = (uintptr_t)handles;
4441 args.points = (uintptr_t)points;
4442 args.count_handles = handle_count;
4445 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4449 drm_public int drmSyncobjTransfer(int fd,
4450 uint32_t dst_handle, uint64_t dst_point,
4451 uint32_t src_handle, uint64_t src_point,
4454 struct drm_syncobj_transfer args;
4458 args.src_handle = src_handle;
4459 args.dst_handle = dst_handle;
4460 args.src_point = src_point;
4461 args.dst_point = dst_point;
4464 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);