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 #if defined(__FreeBSD__)
63 #include <sys/param.h>
66 #define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
68 /* Not all systems have MAP_FAILED defined */
70 #define MAP_FAILED ((void *)-1)
74 #include "libdrm_macros.h"
76 #include "util_math.h"
92 #endif /* __OpenBSD__ */
95 #define DRM_MAJOR 226 /* Linux */
98 #if defined(__OpenBSD__) || defined(__DragonFly__)
106 uint16_t subvendor_id;
107 uint16_t subdevice_id;
111 #define DRM_IOCTL_GET_PCIINFO DRM_IOR(0x15, struct drm_pciinfo)
114 #define DRM_MSG_VERBOSITY 3
116 #define memclear(s) memset(&s, 0, sizeof(s))
118 static drmServerInfoPtr drm_server_info;
120 drm_public void drmSetServerInfo(drmServerInfoPtr info)
122 drm_server_info = info;
126 * Output a message to stderr.
128 * \param format printf() like format string.
131 * This function is a wrapper around vfprintf().
134 static int DRM_PRINTFLIKE(1, 0)
135 drmDebugPrint(const char *format, va_list ap)
137 return vfprintf(stderr, format, ap);
141 drmMsg(const char *format, ...)
145 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
146 (drm_server_info && drm_server_info->debug_print))
148 va_start(ap, format);
149 if (drm_server_info) {
150 drm_server_info->debug_print(format,ap);
152 drmDebugPrint(format, ap);
158 static void *drmHashTable = NULL; /* Context switch callbacks */
160 drm_public void *drmGetHashTable(void)
165 drm_public void *drmMalloc(int size)
167 return calloc(1, size);
170 drm_public void drmFree(void *pt)
176 * Call ioctl, restarting if it is interrupted
179 drmIoctl(int fd, unsigned long request, void *arg)
184 ret = ioctl(fd, request, arg);
185 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
189 static unsigned long drmGetKeyFromFd(int fd)
198 drm_public drmHashEntry *drmGetEntry(int fd)
200 unsigned long key = drmGetKeyFromFd(fd);
205 drmHashTable = drmHashCreate();
207 if (drmHashLookup(drmHashTable, key, &value)) {
208 entry = drmMalloc(sizeof(*entry));
211 entry->tagTable = drmHashCreate();
212 drmHashInsert(drmHashTable, key, entry);
220 * Compare two busid strings
225 * \return 1 if matched.
228 * This function compares two bus ID strings. It understands the older
229 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
230 * domain, b is bus, d is device, f is function.
232 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
234 /* First, check if the IDs are exactly the same */
235 if (strcasecmp(id1, id2) == 0)
238 /* Try to match old/new-style PCI bus IDs. */
239 if (strncasecmp(id1, "pci", 3) == 0) {
240 unsigned int o1, b1, d1, f1;
241 unsigned int o2, b2, d2, f2;
244 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
247 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
252 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
255 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
260 /* If domains aren't properly supported by the kernel interface,
261 * just ignore them, which sucks less than picking a totally random
262 * card with "open by name"
267 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
276 * Handles error checking for chown call.
278 * \param path to file.
279 * \param id of the new owner.
280 * \param id of the new group.
282 * \return zero if success or -1 if failure.
285 * Checks for failure. If failure was caused by signal call chown again.
286 * If any other failure happened then it will output error message using
290 static int chown_check_return(const char *path, uid_t owner, gid_t group)
295 rv = chown(path, owner, group);
296 } while (rv != 0 && errno == EINTR);
301 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
302 path, errno, strerror(errno));
307 static const char *drmGetDeviceName(int type)
310 case DRM_NODE_PRIMARY:
312 case DRM_NODE_CONTROL:
313 return DRM_CONTROL_DEV_NAME;
314 case DRM_NODE_RENDER:
315 return DRM_RENDER_DEV_NAME;
321 * Open the DRM device, creating it if necessary.
323 * \param dev major and minor numbers of the device.
324 * \param minor minor number of the device.
326 * \return a file descriptor on success, or a negative value on error.
329 * Assembles the device name from \p minor and opens it, creating the device
330 * special file node with the major and minor numbers specified by \p dev and
331 * parent directory if necessary and was called by root.
333 static int drmOpenDevice(dev_t dev, int minor, int type)
336 const char *dev_name = drmGetDeviceName(type);
337 char buf[DRM_NODE_NAME_MAX];
339 mode_t devmode = DRM_DEV_MODE, serv_mode;
342 int isroot = !geteuid();
343 uid_t user = DRM_DEV_UID;
344 gid_t group = DRM_DEV_GID;
350 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
351 drmMsg("drmOpenDevice: node name is %s\n", buf);
353 if (drm_server_info && drm_server_info->get_perms) {
354 drm_server_info->get_perms(&serv_group, &serv_mode);
355 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
356 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
360 if (stat(DRM_DIR_NAME, &st)) {
362 return DRM_ERR_NOT_ROOT;
363 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
364 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
365 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
368 /* Check if the device node exists and create it if necessary. */
369 if (stat(buf, &st)) {
371 return DRM_ERR_NOT_ROOT;
373 mknod(buf, S_IFCHR | devmode, dev);
376 if (drm_server_info && drm_server_info->get_perms) {
377 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
378 chown_check_return(buf, user, group);
382 /* if we modprobed then wait for udev */
386 if (stat(DRM_DIR_NAME, &st)) {
390 if (udev_count == 50)
395 if (stat(buf, &st)) {
399 if (udev_count == 50)
406 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
407 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
408 fd, fd < 0 ? strerror(errno) : "OK");
413 /* Check if the device node is not what we expect it to be, and recreate it
414 * and try again if so.
416 if (st.st_rdev != dev) {
418 return DRM_ERR_NOT_ROOT;
420 mknod(buf, S_IFCHR | devmode, dev);
421 if (drm_server_info && drm_server_info->get_perms) {
422 chown_check_return(buf, user, group);
426 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
427 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
428 fd, fd < 0 ? strerror(errno) : "OK");
432 drmMsg("drmOpenDevice: Open failed\n");
440 * Open the DRM device
442 * \param minor device minor number.
443 * \param create allow to create the device if set.
445 * \return a file descriptor on success, or a negative value on error.
448 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
449 * name from \p minor and opens it.
451 static int drmOpenMinor(int minor, int create, int type)
454 char buf[DRM_NODE_NAME_MAX];
455 const char *dev_name = drmGetDeviceName(type);
458 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
463 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
464 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
471 * Determine whether the DRM kernel driver has been loaded.
473 * \return 1 if the DRM driver is loaded, 0 otherwise.
476 * Determine the presence of the kernel driver by attempting to open the 0
477 * minor and get version information. For backward compatibility with older
478 * Linux implementations, /proc/dri is also checked.
480 drm_public int drmAvailable(void)
482 drmVersionPtr version;
486 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
488 /* Try proc for backward Linux compatibility */
489 if (!access("/proc/dri/0", R_OK))
495 if ((version = drmGetVersion(fd))) {
497 drmFreeVersion(version);
504 static int drmGetMinorBase(int type)
507 case DRM_NODE_PRIMARY:
509 case DRM_NODE_CONTROL:
511 case DRM_NODE_RENDER:
518 static int drmGetMinorType(int minor)
520 int type = minor >> 6;
526 case DRM_NODE_PRIMARY:
527 case DRM_NODE_CONTROL:
528 case DRM_NODE_RENDER:
535 static const char *drmGetMinorName(int type)
538 case DRM_NODE_PRIMARY:
539 return DRM_PRIMARY_MINOR_NAME;
540 case DRM_NODE_CONTROL:
541 return DRM_CONTROL_MINOR_NAME;
542 case DRM_NODE_RENDER:
543 return DRM_RENDER_MINOR_NAME;
550 * Open the device by bus ID.
552 * \param busid bus ID.
553 * \param type device node type.
555 * \return a file descriptor on success, or a negative value on error.
558 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
559 * comparing the device bus ID with the one supplied.
561 * \sa drmOpenMinor() and drmGetBusid().
563 static int drmOpenByBusid(const char *busid, int type)
565 int i, pci_domain_ok = 1;
569 int base = drmGetMinorBase(type);
574 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
575 for (i = base; i < base + DRM_MAX_MINOR; i++) {
576 fd = drmOpenMinor(i, 1, type);
577 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
579 /* We need to try for 1.4 first for proper PCI domain support
580 * and if that fails, we know the kernel is busted
584 sv.drm_dd_major = -1; /* Don't care */
585 sv.drm_dd_minor = -1; /* Don't care */
586 if (drmSetInterfaceVersion(fd, &sv)) {
592 sv.drm_dd_major = -1; /* Don't care */
593 sv.drm_dd_minor = -1; /* Don't care */
594 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
595 drmSetInterfaceVersion(fd, &sv);
597 buf = drmGetBusid(fd);
598 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
599 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
613 * Open the device by name.
615 * \param name driver name.
616 * \param type the device node type.
618 * \return a file descriptor on success, or a negative value on error.
621 * This function opens the first minor number that matches the driver name and
622 * isn't already in use. If it's in use it then it will already have a bus ID
625 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
627 static int drmOpenByName(const char *name, int type)
631 drmVersionPtr version;
633 int base = drmGetMinorBase(type);
639 * Open the first minor number that matches the driver name and isn't
640 * already in use. If it's in use it will have a busid assigned already.
642 for (i = base; i < base + DRM_MAX_MINOR; i++) {
643 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
644 if ((version = drmGetVersion(fd))) {
645 if (!strcmp(version->name, name)) {
646 drmFreeVersion(version);
647 id = drmGetBusid(fd);
648 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
657 drmFreeVersion(version);
665 /* Backward-compatibility /proc support */
666 for (i = 0; i < 8; i++) {
667 char proc_name[64], buf[512];
668 char *driver, *pt, *devstring;
671 sprintf(proc_name, "/proc/dri/%d/name", i);
672 if ((fd = open(proc_name, 0, 0)) >= 0) {
673 retcode = read(fd, buf, sizeof(buf)-1);
676 buf[retcode-1] = '\0';
677 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
679 if (*pt) { /* Device is next */
681 if (!strcmp(driver, name)) { /* Match */
682 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
684 if (*pt) { /* Found busid */
685 return drmOpenByBusid(++pt, type);
686 } else { /* No busid */
687 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
701 * Open the DRM device.
703 * Looks up the specified name and bus ID, and opens the device found. The
704 * entry in /dev/dri is created if necessary and if called by root.
706 * \param name driver name. Not referenced if bus ID is supplied.
707 * \param busid bus ID. Zero if not known.
709 * \return a file descriptor on success, or a negative value on error.
712 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
715 drm_public int drmOpen(const char *name, const char *busid)
717 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
721 * Open the DRM device with specified type.
723 * Looks up the specified name and bus ID, and opens the device found. The
724 * entry in /dev/dri is created if necessary and if called by root.
726 * \param name driver name. Not referenced if bus ID is supplied.
727 * \param busid bus ID. Zero if not known.
728 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
730 * \return a file descriptor on success, or a negative value on error.
733 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
736 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
738 if (name != NULL && drm_server_info &&
739 drm_server_info->load_module && !drmAvailable()) {
740 /* try to load the kernel module */
741 if (!drm_server_info->load_module(name)) {
742 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
748 int fd = drmOpenByBusid(busid, type);
754 return drmOpenByName(name, type);
759 drm_public int drmOpenControl(int minor)
761 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
764 drm_public int drmOpenRender(int minor)
766 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
770 * Free the version information returned by drmGetVersion().
772 * \param v pointer to the version information.
775 * It frees the memory pointed by \p %v as well as all the non-null strings
778 drm_public void drmFreeVersion(drmVersionPtr v)
790 * Free the non-public version information returned by the kernel.
792 * \param v pointer to the version information.
795 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
796 * the non-null strings pointers in it.
798 static void drmFreeKernelVersion(drm_version_t *v)
810 * Copy version information.
812 * \param d destination pointer.
813 * \param s source pointer.
816 * Used by drmGetVersion() to translate the information returned by the ioctl
817 * interface in a private structure into the public structure counterpart.
819 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
821 d->version_major = s->version_major;
822 d->version_minor = s->version_minor;
823 d->version_patchlevel = s->version_patchlevel;
824 d->name_len = s->name_len;
825 d->name = strdup(s->name);
826 d->date_len = s->date_len;
827 d->date = strdup(s->date);
828 d->desc_len = s->desc_len;
829 d->desc = strdup(s->desc);
834 * Query the driver version information.
836 * \param fd file descriptor.
838 * \return pointer to a drmVersion structure which should be freed with
841 * \note Similar information is available via /proc/dri.
844 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
845 * first with zeros to get the string lengths, and then the actually strings.
846 * It also null-terminates them since they might not be already.
848 drm_public drmVersionPtr drmGetVersion(int fd)
850 drmVersionPtr retval;
851 drm_version_t *version = drmMalloc(sizeof(*version));
853 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
854 drmFreeKernelVersion(version);
858 if (version->name_len)
859 version->name = drmMalloc(version->name_len + 1);
860 if (version->date_len)
861 version->date = drmMalloc(version->date_len + 1);
862 if (version->desc_len)
863 version->desc = drmMalloc(version->desc_len + 1);
865 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
866 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
867 drmFreeKernelVersion(version);
871 /* The results might not be null-terminated strings, so terminate them. */
872 if (version->name_len) version->name[version->name_len] = '\0';
873 if (version->date_len) version->date[version->date_len] = '\0';
874 if (version->desc_len) version->desc[version->desc_len] = '\0';
876 retval = drmMalloc(sizeof(*retval));
877 drmCopyVersion(retval, version);
878 drmFreeKernelVersion(version);
884 * Get version information for the DRM user space library.
886 * This version number is driver independent.
888 * \param fd file descriptor.
890 * \return version information.
893 * This function allocates and fills a drm_version structure with a hard coded
896 drm_public drmVersionPtr drmGetLibVersion(int fd)
898 drm_version_t *version = drmMalloc(sizeof(*version));
901 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
902 * revision 1.0.x = original DRM interface with no drmGetLibVersion
903 * entry point and many drm<Device> extensions
904 * revision 1.1.x = added drmCommand entry points for device extensions
905 * added drmGetLibVersion to identify libdrm.a version
906 * revision 1.2.x = added drmSetInterfaceVersion
907 * modified drmOpen to handle both busid and name
908 * revision 1.3.x = added server + memory manager
910 version->version_major = 1;
911 version->version_minor = 3;
912 version->version_patchlevel = 0;
914 return (drmVersionPtr)version;
917 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
919 struct drm_get_cap cap;
923 cap.capability = capability;
925 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
933 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
935 struct drm_set_client_cap cap;
938 cap.capability = capability;
941 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
945 * Free the bus ID information.
947 * \param busid bus ID information string as given by drmGetBusid().
950 * This function is just frees the memory pointed by \p busid.
952 drm_public void drmFreeBusid(const char *busid)
954 drmFree((void *)busid);
959 * Get the bus ID of the device.
961 * \param fd file descriptor.
963 * \return bus ID string.
966 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
967 * get the string length and data, passing the arguments in a drm_unique
970 drm_public char *drmGetBusid(int fd)
976 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
978 u.unique = drmMalloc(u.unique_len + 1);
979 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
983 u.unique[u.unique_len] = '\0';
990 * Set the bus ID of the device.
992 * \param fd file descriptor.
993 * \param busid bus ID string.
995 * \return zero on success, negative on failure.
998 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
999 * the arguments in a drm_unique structure.
1001 drm_public int drmSetBusid(int fd, const char *busid)
1006 u.unique = (char *)busid;
1007 u.unique_len = strlen(busid);
1009 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1015 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1022 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1024 *magic = auth.magic;
1028 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1034 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1040 * Specifies a range of memory that is available for mapping by a
1043 * \param fd file descriptor.
1044 * \param offset usually the physical address. The actual meaning depends of
1045 * the \p type parameter. See below.
1046 * \param size of the memory in bytes.
1047 * \param type type of the memory to be mapped.
1048 * \param flags combination of several flags to modify the function actions.
1049 * \param handle will be set to a value that may be used as the offset
1050 * parameter for mmap().
1052 * \return zero on success or a negative value on error.
1054 * \par Mapping the frame buffer
1055 * For the frame buffer
1056 * - \p offset will be the physical address of the start of the frame buffer,
1057 * - \p size will be the size of the frame buffer in bytes, and
1058 * - \p type will be DRM_FRAME_BUFFER.
1061 * The area mapped will be uncached. If MTRR support is available in the
1062 * kernel, the frame buffer area will be set to write combining.
1064 * \par Mapping the MMIO register area
1065 * For the MMIO register area,
1066 * - \p offset will be the physical address of the start of the register area,
1067 * - \p size will be the size of the register area bytes, and
1068 * - \p type will be DRM_REGISTERS.
1070 * The area mapped will be uncached.
1072 * \par Mapping the SAREA
1074 * - \p offset will be ignored and should be set to zero,
1075 * - \p size will be the desired size of the SAREA in bytes,
1076 * - \p type will be DRM_SHM.
1079 * A shared memory area of the requested size will be created and locked in
1080 * kernel memory. This area may be mapped into client-space by using the handle
1083 * \note May only be called by root.
1086 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1087 * the arguments in a drm_map structure.
1089 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1090 drmMapFlags flags, drm_handle_t *handle)
1095 map.offset = offset;
1099 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1102 *handle = (drm_handle_t)(uintptr_t)map.handle;
1106 drm_public int drmRmMap(int fd, drm_handle_t handle)
1111 map.handle = (void *)(uintptr_t)handle;
1113 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1119 * Make buffers available for DMA transfers.
1121 * \param fd file descriptor.
1122 * \param count number of buffers.
1123 * \param size size of each buffer.
1124 * \param flags buffer allocation flags.
1125 * \param agp_offset offset in the AGP aperture
1127 * \return number of buffers allocated, negative on error.
1130 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1134 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1137 drm_buf_desc_t request;
1140 request.count = count;
1141 request.size = size;
1142 request.flags = flags;
1143 request.agp_start = agp_offset;
1145 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1147 return request.count;
1150 drm_public int drmMarkBufs(int fd, double low, double high)
1152 drm_buf_info_t info;
1157 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1163 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1166 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1167 int retval = -errno;
1172 for (i = 0; i < info.count; i++) {
1173 info.list[i].low_mark = low * info.list[i].count;
1174 info.list[i].high_mark = high * info.list[i].count;
1175 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1176 int retval = -errno;
1189 * \param fd file descriptor.
1190 * \param count number of buffers to free.
1191 * \param list list of buffers to be freed.
1193 * \return zero on success, or a negative value on failure.
1195 * \note This function is primarily used for debugging.
1198 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1199 * the arguments in a drm_buf_free structure.
1201 drm_public int drmFreeBufs(int fd, int count, int *list)
1203 drm_buf_free_t request;
1206 request.count = count;
1207 request.list = list;
1208 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1217 * \param fd file descriptor.
1220 * This function closes the file descriptor.
1222 drm_public int drmClose(int fd)
1224 unsigned long key = drmGetKeyFromFd(fd);
1225 drmHashEntry *entry = drmGetEntry(fd);
1227 drmHashDestroy(entry->tagTable);
1230 entry->tagTable = NULL;
1232 drmHashDelete(drmHashTable, key);
1240 * Map a region of memory.
1242 * \param fd file descriptor.
1243 * \param handle handle returned by drmAddMap().
1244 * \param size size in bytes. Must match the size used by drmAddMap().
1245 * \param address will contain the user-space virtual address where the mapping
1248 * \return zero on success, or a negative value on failure.
1251 * This function is a wrapper for mmap().
1253 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1254 drmAddressPtr address)
1256 static unsigned long pagesize_mask = 0;
1262 pagesize_mask = getpagesize() - 1;
1264 size = (size + pagesize_mask) & ~pagesize_mask;
1266 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1267 if (*address == MAP_FAILED)
1274 * Unmap mappings obtained with drmMap().
1276 * \param address address as given by drmMap().
1277 * \param size size in bytes. Must match the size used by drmMap().
1279 * \return zero on success, or a negative value on failure.
1282 * This function is a wrapper for munmap().
1284 drm_public int drmUnmap(drmAddress address, drmSize size)
1286 return drm_munmap(address, size);
1289 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1291 drm_buf_info_t info;
1292 drmBufInfoPtr retval;
1297 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1301 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1304 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1309 retval = drmMalloc(sizeof(*retval));
1310 retval->count = info.count;
1311 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1312 for (i = 0; i < info.count; i++) {
1313 retval->list[i].count = info.list[i].count;
1314 retval->list[i].size = info.list[i].size;
1315 retval->list[i].low_mark = info.list[i].low_mark;
1316 retval->list[i].high_mark = info.list[i].high_mark;
1325 * Map all DMA buffers into client-virtual space.
1327 * \param fd file descriptor.
1329 * \return a pointer to a ::drmBufMap structure.
1331 * \note The client may not use these buffers until obtaining buffer indices
1335 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1336 * information about the buffers in a drm_buf_map structure into the
1337 * client-visible data structures.
1339 drm_public drmBufMapPtr drmMapBufs(int fd)
1342 drmBufMapPtr retval;
1346 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1352 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1355 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1360 retval = drmMalloc(sizeof(*retval));
1361 retval->count = bufs.count;
1362 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1363 for (i = 0; i < bufs.count; i++) {
1364 retval->list[i].idx = bufs.list[i].idx;
1365 retval->list[i].total = bufs.list[i].total;
1366 retval->list[i].used = 0;
1367 retval->list[i].address = bufs.list[i].address;
1376 * Unmap buffers allocated with drmMapBufs().
1378 * \return zero on success, or negative value on failure.
1381 * Calls munmap() for every buffer stored in \p bufs and frees the
1382 * memory allocated by drmMapBufs().
1384 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1388 for (i = 0; i < bufs->count; i++) {
1389 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1392 drmFree(bufs->list);
1398 #define DRM_DMA_RETRY 16
1401 * Reserve DMA buffers.
1403 * \param fd file descriptor.
1406 * \return zero on success, or a negative value on failure.
1409 * Assemble the arguments into a drm_dma structure and keeps issuing the
1410 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1412 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1417 dma.context = request->context;
1418 dma.send_count = request->send_count;
1419 dma.send_indices = request->send_list;
1420 dma.send_sizes = request->send_sizes;
1421 dma.flags = request->flags;
1422 dma.request_count = request->request_count;
1423 dma.request_size = request->request_size;
1424 dma.request_indices = request->request_list;
1425 dma.request_sizes = request->request_sizes;
1426 dma.granted_count = 0;
1429 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1430 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1433 request->granted_count = dma.granted_count;
1442 * Obtain heavyweight hardware lock.
1444 * \param fd file descriptor.
1445 * \param context context.
1446 * \param flags flags that determine the state of the hardware when the function
1449 * \return always zero.
1452 * This function translates the arguments into a drm_lock structure and issue
1453 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1455 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1460 lock.context = context;
1462 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1463 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1464 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1465 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1466 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1467 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1469 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1475 * Release the hardware lock.
1477 * \param fd file descriptor.
1478 * \param context context.
1480 * \return zero on success, or a negative value on failure.
1483 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1484 * argument in a drm_lock structure.
1486 drm_public int drmUnlock(int fd, drm_context_t context)
1491 lock.context = context;
1492 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1495 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1499 drm_context_t * retval;
1503 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1509 if (!(list = drmMalloc(res.count * sizeof(*list))))
1511 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1514 res.contexts = list;
1515 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1516 goto err_free_context;
1518 for (i = 0; i < res.count; i++)
1519 retval[i] = list[i].handle;
1532 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1540 * Used by the X server during GLXContext initialization. This causes
1541 * per-context kernel-level resources to be allocated.
1543 * \param fd file descriptor.
1544 * \param handle is set on success. To be used by the client when requesting DMA
1545 * dispatch with drmDMA().
1547 * \return zero on success, or a negative value on failure.
1549 * \note May only be called by root.
1552 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1553 * argument in a drm_ctx structure.
1555 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1560 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1562 *handle = ctx.handle;
1566 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1571 ctx.handle = context;
1572 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1577 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1578 drm_context_tFlags flags)
1583 * Context preserving means that no context switches are done between DMA
1584 * buffers from one context and the next. This is suitable for use in the
1585 * X server (which promises to maintain hardware context), or in the
1586 * client-side library when buffers are swapped on behalf of two threads.
1589 ctx.handle = context;
1590 if (flags & DRM_CONTEXT_PRESERVED)
1591 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1592 if (flags & DRM_CONTEXT_2DONLY)
1593 ctx.flags |= _DRM_CONTEXT_2DONLY;
1594 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1599 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1600 drm_context_tFlagsPtr flags)
1605 ctx.handle = context;
1606 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1609 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1610 *flags |= DRM_CONTEXT_PRESERVED;
1611 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1612 *flags |= DRM_CONTEXT_2DONLY;
1619 * Free any kernel-level resources allocated with drmCreateContext() associated
1622 * \param fd file descriptor.
1623 * \param handle handle given by drmCreateContext().
1625 * \return zero on success, or a negative value on failure.
1627 * \note May only be called by root.
1630 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1631 * argument in a drm_ctx structure.
1633 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1638 ctx.handle = handle;
1639 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1644 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1649 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1651 *handle = draw.handle;
1655 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1660 draw.handle = handle;
1661 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1666 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1667 drm_drawable_info_type_t type,
1668 unsigned int num, void *data)
1670 drm_update_draw_t update;
1673 update.handle = handle;
1676 update.data = (unsigned long long)(unsigned long)data;
1678 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1684 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1687 struct drm_crtc_get_sequence get_seq;
1691 get_seq.crtc_id = crtcId;
1692 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1697 *sequence = get_seq.sequence;
1699 *ns = get_seq.sequence_ns;
1703 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1705 uint64_t *sequence_queued,
1708 struct drm_crtc_queue_sequence queue_seq;
1711 memclear(queue_seq);
1712 queue_seq.crtc_id = crtcId;
1713 queue_seq.flags = flags;
1714 queue_seq.sequence = sequence;
1715 queue_seq.user_data = user_data;
1717 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1718 if (ret == 0 && sequence_queued)
1719 *sequence_queued = queue_seq.sequence;
1725 * Acquire the AGP device.
1727 * Must be called before any of the other AGP related calls.
1729 * \param fd file descriptor.
1731 * \return zero on success, or a negative value on failure.
1734 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1736 drm_public int drmAgpAcquire(int fd)
1738 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1745 * Release the AGP device.
1747 * \param fd file descriptor.
1749 * \return zero on success, or a negative value on failure.
1752 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1754 drm_public int drmAgpRelease(int fd)
1756 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1765 * \param fd file descriptor.
1766 * \param mode AGP mode.
1768 * \return zero on success, or a negative value on failure.
1771 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1772 * argument in a drm_agp_mode structure.
1774 drm_public int drmAgpEnable(int fd, unsigned long mode)
1780 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1787 * Allocate a chunk of AGP memory.
1789 * \param fd file descriptor.
1790 * \param size requested memory size in bytes. Will be rounded to page boundary.
1791 * \param type type of memory to allocate.
1792 * \param address if not zero, will be set to the physical address of the
1794 * \param handle on success will be set to a handle of the allocated memory.
1796 * \return zero on success, or a negative value on failure.
1799 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1800 * arguments in a drm_agp_buffer structure.
1802 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1803 unsigned long *address, drm_handle_t *handle)
1808 *handle = DRM_AGP_NO_HANDLE;
1811 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1814 *address = b.physical;
1821 * Free a chunk of AGP memory.
1823 * \param fd file descriptor.
1824 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1826 * \return zero on success, or a negative value on failure.
1829 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1830 * argument in a drm_agp_buffer structure.
1832 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1838 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1845 * Bind a chunk of AGP memory.
1847 * \param fd file descriptor.
1848 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1849 * \param offset offset in bytes. It will round to page boundary.
1851 * \return zero on success, or a negative value on failure.
1854 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1855 * argument in a drm_agp_binding structure.
1857 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1859 drm_agp_binding_t b;
1864 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1871 * Unbind a chunk of AGP memory.
1873 * \param fd file descriptor.
1874 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1876 * \return zero on success, or a negative value on failure.
1879 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1880 * the argument in a drm_agp_binding structure.
1882 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1884 drm_agp_binding_t b;
1888 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1895 * Get AGP driver major version number.
1897 * \param fd file descriptor.
1899 * \return major version number on success, or a negative value on failure..
1902 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1903 * necessary information in a drm_agp_info structure.
1905 drm_public int drmAgpVersionMajor(int fd)
1911 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1913 return i.agp_version_major;
1918 * Get AGP driver minor version number.
1920 * \param fd file descriptor.
1922 * \return minor version number on success, or a negative value on failure.
1925 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1926 * necessary information in a drm_agp_info structure.
1928 drm_public int drmAgpVersionMinor(int fd)
1934 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1936 return i.agp_version_minor;
1943 * \param fd file descriptor.
1945 * \return mode on success, or zero on failure.
1948 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1949 * necessary information in a drm_agp_info structure.
1951 drm_public unsigned long drmAgpGetMode(int fd)
1957 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1964 * Get AGP aperture base.
1966 * \param fd file descriptor.
1968 * \return aperture base on success, zero on failure.
1971 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1972 * necessary information in a drm_agp_info structure.
1974 drm_public unsigned long drmAgpBase(int fd)
1980 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1982 return i.aperture_base;
1987 * Get AGP aperture size.
1989 * \param fd file descriptor.
1991 * \return aperture size on success, zero on failure.
1994 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1995 * necessary information in a drm_agp_info structure.
1997 drm_public unsigned long drmAgpSize(int fd)
2003 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2005 return i.aperture_size;
2010 * Get used AGP memory.
2012 * \param fd file descriptor.
2014 * \return memory used on success, or zero on failure.
2017 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2018 * necessary information in a drm_agp_info structure.
2020 drm_public unsigned long drmAgpMemoryUsed(int fd)
2026 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2028 return i.memory_used;
2033 * Get available AGP memory.
2035 * \param fd file descriptor.
2037 * \return memory available on success, or zero on failure.
2040 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2041 * necessary information in a drm_agp_info structure.
2043 drm_public unsigned long drmAgpMemoryAvail(int fd)
2049 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2051 return i.memory_allowed;
2056 * Get hardware vendor ID.
2058 * \param fd file descriptor.
2060 * \return vendor ID on success, or zero on failure.
2063 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2064 * necessary information in a drm_agp_info structure.
2066 drm_public unsigned int drmAgpVendorId(int fd)
2072 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2079 * Get hardware device ID.
2081 * \param fd file descriptor.
2083 * \return zero on success, or zero on failure.
2086 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2087 * necessary information in a drm_agp_info structure.
2089 drm_public unsigned int drmAgpDeviceId(int fd)
2095 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2100 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2101 drm_handle_t *handle)
2103 drm_scatter_gather_t sg;
2109 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2111 *handle = sg.handle;
2115 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2117 drm_scatter_gather_t sg;
2121 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2129 * \param fd file descriptor.
2130 * \param vbl pointer to a drmVBlank structure.
2132 * \return zero on success, or a negative value on failure.
2135 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2137 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2139 struct timespec timeout, cur;
2142 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2144 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2150 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2151 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2152 if (ret && errno == EINTR) {
2153 clock_gettime(CLOCK_MONOTONIC, &cur);
2154 /* Timeout after 1s */
2155 if (cur.tv_sec > timeout.tv_sec + 1 ||
2156 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2163 } while (ret && errno == EINTR);
2169 drm_public int drmError(int err, const char *label)
2172 case DRM_ERR_NO_DEVICE:
2173 fprintf(stderr, "%s: no device\n", label);
2175 case DRM_ERR_NO_ACCESS:
2176 fprintf(stderr, "%s: no access\n", label);
2178 case DRM_ERR_NOT_ROOT:
2179 fprintf(stderr, "%s: not root\n", label);
2181 case DRM_ERR_INVALID:
2182 fprintf(stderr, "%s: invalid args\n", label);
2187 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2195 * Install IRQ handler.
2197 * \param fd file descriptor.
2198 * \param irq IRQ number.
2200 * \return zero on success, or a negative value on failure.
2203 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2204 * argument in a drm_control structure.
2206 drm_public int drmCtlInstHandler(int fd, int irq)
2211 ctl.func = DRM_INST_HANDLER;
2213 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2220 * Uninstall IRQ handler.
2222 * \param fd file descriptor.
2224 * \return zero on success, or a negative value on failure.
2227 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2228 * argument in a drm_control structure.
2230 drm_public int drmCtlUninstHandler(int fd)
2235 ctl.func = DRM_UNINST_HANDLER;
2237 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2242 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2247 lock.context = context;
2248 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2249 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2250 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2251 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2252 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2253 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2254 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2260 * Get IRQ from bus ID.
2262 * \param fd file descriptor.
2263 * \param busnum bus number.
2264 * \param devnum device number.
2265 * \param funcnum function number.
2267 * \return IRQ number on success, or a negative value on failure.
2270 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2271 * arguments in a drm_irq_busid structure.
2273 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2281 p.funcnum = funcnum;
2282 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2287 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2289 drmHashEntry *entry = drmGetEntry(fd);
2291 if (drmHashInsert(entry->tagTable, context, tag)) {
2292 drmHashDelete(entry->tagTable, context);
2293 drmHashInsert(entry->tagTable, context, tag);
2298 drm_public int drmDelContextTag(int fd, drm_context_t context)
2300 drmHashEntry *entry = drmGetEntry(fd);
2302 return drmHashDelete(entry->tagTable, context);
2305 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2307 drmHashEntry *entry = drmGetEntry(fd);
2310 if (drmHashLookup(entry->tagTable, context, &value))
2316 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2317 drm_handle_t handle)
2319 drm_ctx_priv_map_t map;
2322 map.ctx_id = ctx_id;
2323 map.handle = (void *)(uintptr_t)handle;
2325 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2330 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2331 drm_handle_t *handle)
2333 drm_ctx_priv_map_t map;
2336 map.ctx_id = ctx_id;
2338 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2341 *handle = (drm_handle_t)(uintptr_t)map.handle;
2346 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2347 drmMapType *type, drmMapFlags *flags,
2348 drm_handle_t *handle, int *mtrr)
2354 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2356 *offset = map.offset;
2360 *handle = (unsigned long)map.handle;
2365 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2366 unsigned long *magic, unsigned long *iocs)
2368 drm_client_t client;
2372 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2374 *auth = client.auth;
2377 *magic = client.magic;
2378 *iocs = client.iocs;
2382 drm_public int drmGetStats(int fd, drmStatsT *stats)
2388 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2392 memset(stats, 0, sizeof(*stats));
2393 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2397 stats->data[i].long_format = "%-20.20s"; \
2398 stats->data[i].rate_format = "%8.8s"; \
2399 stats->data[i].isvalue = 1; \
2400 stats->data[i].verbose = 0
2403 stats->data[i].long_format = "%-20.20s"; \
2404 stats->data[i].rate_format = "%5.5s"; \
2405 stats->data[i].isvalue = 0; \
2406 stats->data[i].mult_names = "kgm"; \
2407 stats->data[i].mult = 1000; \
2408 stats->data[i].verbose = 0
2411 stats->data[i].long_format = "%-20.20s"; \
2412 stats->data[i].rate_format = "%5.5s"; \
2413 stats->data[i].isvalue = 0; \
2414 stats->data[i].mult_names = "KGM"; \
2415 stats->data[i].mult = 1024; \
2416 stats->data[i].verbose = 0
2419 stats->count = s.count;
2420 for (i = 0; i < s.count; i++) {
2421 stats->data[i].value = s.data[i].value;
2422 switch (s.data[i].type) {
2423 case _DRM_STAT_LOCK:
2424 stats->data[i].long_name = "Lock";
2425 stats->data[i].rate_name = "Lock";
2428 case _DRM_STAT_OPENS:
2429 stats->data[i].long_name = "Opens";
2430 stats->data[i].rate_name = "O";
2432 stats->data[i].verbose = 1;
2434 case _DRM_STAT_CLOSES:
2435 stats->data[i].long_name = "Closes";
2436 stats->data[i].rate_name = "Lock";
2438 stats->data[i].verbose = 1;
2440 case _DRM_STAT_IOCTLS:
2441 stats->data[i].long_name = "Ioctls";
2442 stats->data[i].rate_name = "Ioc/s";
2445 case _DRM_STAT_LOCKS:
2446 stats->data[i].long_name = "Locks";
2447 stats->data[i].rate_name = "Lck/s";
2450 case _DRM_STAT_UNLOCKS:
2451 stats->data[i].long_name = "Unlocks";
2452 stats->data[i].rate_name = "Unl/s";
2456 stats->data[i].long_name = "IRQs";
2457 stats->data[i].rate_name = "IRQ/s";
2460 case _DRM_STAT_PRIMARY:
2461 stats->data[i].long_name = "Primary Bytes";
2462 stats->data[i].rate_name = "PB/s";
2465 case _DRM_STAT_SECONDARY:
2466 stats->data[i].long_name = "Secondary Bytes";
2467 stats->data[i].rate_name = "SB/s";
2471 stats->data[i].long_name = "DMA";
2472 stats->data[i].rate_name = "DMA/s";
2475 case _DRM_STAT_SPECIAL:
2476 stats->data[i].long_name = "Special DMA";
2477 stats->data[i].rate_name = "dma/s";
2480 case _DRM_STAT_MISSED:
2481 stats->data[i].long_name = "Miss";
2482 stats->data[i].rate_name = "Ms/s";
2485 case _DRM_STAT_VALUE:
2486 stats->data[i].long_name = "Value";
2487 stats->data[i].rate_name = "Value";
2490 case _DRM_STAT_BYTE:
2491 stats->data[i].long_name = "Bytes";
2492 stats->data[i].rate_name = "B/s";
2495 case _DRM_STAT_COUNT:
2497 stats->data[i].long_name = "Count";
2498 stats->data[i].rate_name = "Cnt/s";
2507 * Issue a set-version ioctl.
2509 * \param fd file descriptor.
2510 * \param drmCommandIndex command index
2511 * \param data source pointer of the data to be read and written.
2512 * \param size size of the data to be read and written.
2514 * \return zero on success, or a negative value on failure.
2517 * It issues a read-write ioctl given by
2518 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2520 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2523 drm_set_version_t sv;
2526 sv.drm_di_major = version->drm_di_major;
2527 sv.drm_di_minor = version->drm_di_minor;
2528 sv.drm_dd_major = version->drm_dd_major;
2529 sv.drm_dd_minor = version->drm_dd_minor;
2531 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2535 version->drm_di_major = sv.drm_di_major;
2536 version->drm_di_minor = sv.drm_di_minor;
2537 version->drm_dd_major = sv.drm_dd_major;
2538 version->drm_dd_minor = sv.drm_dd_minor;
2544 * Send a device-specific command.
2546 * \param fd file descriptor.
2547 * \param drmCommandIndex command index
2549 * \return zero on success, or a negative value on failure.
2552 * It issues a ioctl given by
2553 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2555 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2557 unsigned long request;
2559 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2561 if (drmIoctl(fd, request, NULL)) {
2569 * Send a device-specific read command.
2571 * \param fd file descriptor.
2572 * \param drmCommandIndex command index
2573 * \param data destination pointer of the data to be read.
2574 * \param size size of the data to be read.
2576 * \return zero on success, or a negative value on failure.
2579 * It issues a read ioctl given by
2580 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2582 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2583 void *data, unsigned long size)
2585 unsigned long request;
2587 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2588 DRM_COMMAND_BASE + drmCommandIndex, size);
2590 if (drmIoctl(fd, request, data)) {
2598 * Send a device-specific write command.
2600 * \param fd file descriptor.
2601 * \param drmCommandIndex command index
2602 * \param data source pointer of the data to be written.
2603 * \param size size of the data to be written.
2605 * \return zero on success, or a negative value on failure.
2608 * It issues a write ioctl given by
2609 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2611 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2612 void *data, unsigned long size)
2614 unsigned long request;
2616 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2617 DRM_COMMAND_BASE + drmCommandIndex, size);
2619 if (drmIoctl(fd, request, data)) {
2627 * Send a device-specific read-write command.
2629 * \param fd file descriptor.
2630 * \param drmCommandIndex command index
2631 * \param data source pointer of the data to be read and written.
2632 * \param size size of the data to be read and written.
2634 * \return zero on success, or a negative value on failure.
2637 * It issues a read-write ioctl given by
2638 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2640 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2641 void *data, unsigned long size)
2643 unsigned long request;
2645 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2646 DRM_COMMAND_BASE + drmCommandIndex, size);
2648 if (drmIoctl(fd, request, data))
2653 #define DRM_MAX_FDS 16
2659 } connection[DRM_MAX_FDS];
2661 static int nr_fds = 0;
2663 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2665 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2668 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2674 for (i = 0; i < nr_fds; i++)
2675 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2676 (connection[i].type == type)) {
2677 connection[i].refcount++;
2679 return connection[i].fd;
2682 fd = drmOpenWithType(NULL, BusID, type);
2683 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2686 connection[nr_fds].BusID = strdup(BusID);
2687 connection[nr_fds].fd = fd;
2688 connection[nr_fds].refcount = 1;
2689 connection[nr_fds].type = type;
2693 fprintf(stderr, "saved connection %d for %s %d\n",
2694 nr_fds, connection[nr_fds].BusID,
2695 strcmp(BusID, connection[nr_fds].BusID));
2702 drm_public void drmCloseOnce(int fd)
2706 for (i = 0; i < nr_fds; i++) {
2707 if (fd == connection[i].fd) {
2708 if (--connection[i].refcount == 0) {
2709 drmClose(connection[i].fd);
2710 free(connection[i].BusID);
2713 connection[i] = connection[nr_fds];
2721 drm_public int drmSetMaster(int fd)
2723 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2726 drm_public int drmDropMaster(int fd)
2728 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2731 drm_public int drmIsMaster(int fd)
2733 /* Detect master by attempting something that requires master.
2735 * Authenticating magic tokens requires master and 0 is an
2736 * internal kernel detail which we could use. Attempting this on
2737 * a master fd would fail therefore fail with EINVAL because 0
2740 * A non-master fd will fail with EACCES, as the kernel checks
2741 * for master before attempting to do anything else.
2743 * Since we don't want to leak implementation details, use
2746 return drmAuthMagic(fd, 0) != -EACCES;
2749 drm_public char *drmGetDeviceNameFromFd(int fd)
2756 /* The whole drmOpen thing is a fiasco and we need to find a way
2757 * back to just using open(2). For now, however, lets just make
2758 * things worse with even more ad hoc directory walking code to
2759 * discover the device file name. */
2764 for (i = 0; i < DRM_MAX_MINOR; i++) {
2765 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2766 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2769 if (i == DRM_MAX_MINOR)
2772 return strdup(name);
2775 static bool drmNodeIsDRM(int maj, int min)
2781 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2783 return stat(path, &sbuf) == 0;
2785 char name[SPECNAMELEN];
2787 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
2789 /* Handle drm/ and dri/ as both are present in different FreeBSD version
2790 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2791 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2792 * only device nodes in /dev/dri/ */
2793 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
2795 return maj == DRM_MAJOR;
2799 drm_public int drmGetNodeTypeFromFd(int fd)
2804 if (fstat(fd, &sbuf))
2807 maj = major(sbuf.st_rdev);
2808 min = minor(sbuf.st_rdev);
2810 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2815 type = drmGetMinorType(min);
2821 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2824 struct drm_prime_handle args;
2829 args.handle = handle;
2831 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2835 *prime_fd = args.fd;
2839 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2841 struct drm_prime_handle args;
2846 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2850 *handle = args.handle;
2854 static char *drmGetMinorNameForFD(int fd, int type)
2860 const char *name = drmGetMinorName(type);
2862 char dev_name[64], buf[64];
2870 if (fstat(fd, &sbuf))
2873 maj = major(sbuf.st_rdev);
2874 min = minor(sbuf.st_rdev);
2876 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2879 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2881 sysdir = opendir(buf);
2885 while ((ent = readdir(sysdir))) {
2886 if (strncmp(ent->d_name, name, len) == 0) {
2887 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2891 return strdup(dev_name);
2899 char buf[PATH_MAX + 1];
2900 const char *dev_name = drmGetDeviceName(type);
2901 unsigned int maj, min;
2904 if (fstat(fd, &sbuf))
2907 maj = major(sbuf.st_rdev);
2908 min = minor(sbuf.st_rdev);
2910 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2916 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
2917 if (n == -1 || n >= sizeof(buf))
2924 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2926 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2929 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2931 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2935 static char * DRM_PRINTFLIKE(2, 3)
2936 sysfs_uevent_get(const char *path, const char *fmt, ...)
2938 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2939 size_t size = 0, len;
2945 num = vasprintf(&key, fmt, ap);
2949 snprintf(filename, sizeof(filename), "%s/uevent", path);
2951 fp = fopen(filename, "r");
2957 while ((num = getline(&line, &size, fp)) >= 0) {
2958 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
2959 char *start = line + len + 1, *end = line + num - 1;
2964 value = strndup(start, end - start);
2978 /* Little white lie to avoid major rework of the existing code */
2979 #define DRM_BUS_VIRTIO 0x10
2982 static int get_subsystem_type(const char *device_path)
2984 char path[PATH_MAX + 1] = "";
2985 char link[PATH_MAX + 1] = "";
2991 { "/pci", DRM_BUS_PCI },
2992 { "/usb", DRM_BUS_USB },
2993 { "/platform", DRM_BUS_PLATFORM },
2994 { "/spi", DRM_BUS_PLATFORM },
2995 { "/host1x", DRM_BUS_HOST1X },
2996 { "/virtio", DRM_BUS_VIRTIO },
2999 strncpy(path, device_path, PATH_MAX);
3000 strncat(path, "/subsystem", PATH_MAX);
3002 if (readlink(path, link, PATH_MAX) < 0)
3005 name = strrchr(link, '/');
3009 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3010 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3011 return bus_types[i].bus_type;
3018 static int drmParseSubsystemType(int maj, int min)
3021 char path[PATH_MAX + 1] = "";
3022 char real_path[PATH_MAX + 1] = "";
3025 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3026 if (!realpath(path, real_path))
3028 snprintf(path, sizeof(path), "%s", real_path);
3030 subsystem_type = get_subsystem_type(path);
3031 if (subsystem_type == DRM_BUS_VIRTIO) {
3032 strncat(path, "/..", PATH_MAX);
3033 subsystem_type = get_subsystem_type(path);
3035 return subsystem_type;
3036 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3039 #warning "Missing implementation of drmParseSubsystemType"
3045 get_pci_path(int maj, int min, char *pci_path)
3047 char path[PATH_MAX + 1], *term;
3049 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3050 if (!realpath(path, pci_path)) {
3051 strcpy(pci_path, path);
3055 term = strrchr(pci_path, '/');
3056 if (term && strncmp(term, "/virtio", 7) == 0)
3060 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3063 unsigned int domain, bus, dev, func;
3064 char pci_path[PATH_MAX + 1], *value;
3067 get_pci_path(maj, min, pci_path);
3069 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3073 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3079 info->domain = domain;
3085 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3086 struct drm_pciinfo pinfo;
3089 type = drmGetMinorType(min);
3093 fd = drmOpenMinor(min, 0, type);
3097 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3103 info->domain = pinfo.domain;
3104 info->bus = pinfo.bus;
3105 info->dev = pinfo.dev;
3106 info->func = pinfo.func;
3110 #warning "Missing implementation of drmParsePciBusInfo"
3115 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3117 if (a == NULL || b == NULL)
3120 if (a->bustype != b->bustype)
3123 switch (a->bustype) {
3125 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3128 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3130 case DRM_BUS_PLATFORM:
3131 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3133 case DRM_BUS_HOST1X:
3134 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3143 static int drmGetNodeType(const char *name)
3145 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3146 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3147 return DRM_NODE_CONTROL;
3149 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3150 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3151 return DRM_NODE_RENDER;
3153 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3154 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3155 return DRM_NODE_PRIMARY;
3160 static int drmGetMaxNodeName(void)
3162 return sizeof(DRM_DIR_NAME) +
3163 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3164 sizeof(DRM_CONTROL_MINOR_NAME),
3165 sizeof(DRM_RENDER_MINOR_NAME)) +
3166 3 /* length of the node number */;
3170 static int parse_separate_sysfs_files(int maj, int min,
3171 drmPciDeviceInfoPtr device,
3172 bool ignore_revision)
3174 static const char *attrs[] = {
3175 "revision", /* Older kernels are missing the file, so check for it first */
3181 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3182 unsigned int data[ARRAY_SIZE(attrs)];
3186 get_pci_path(maj, min, pci_path);
3188 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3189 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3190 fp = fopen(path, "r");
3194 ret = fscanf(fp, "%x", &data[i]);
3201 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3202 device->vendor_id = data[1] & 0xffff;
3203 device->device_id = data[2] & 0xffff;
3204 device->subvendor_id = data[3] & 0xffff;
3205 device->subdevice_id = data[4] & 0xffff;
3210 static int parse_config_sysfs_file(int maj, int min,
3211 drmPciDeviceInfoPtr device)
3213 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3214 unsigned char config[64];
3217 get_pci_path(maj, min, pci_path);
3219 snprintf(path, PATH_MAX, "%s/config", pci_path);
3220 fd = open(path, O_RDONLY);
3224 ret = read(fd, config, sizeof(config));
3229 device->vendor_id = config[0] | (config[1] << 8);
3230 device->device_id = config[2] | (config[3] << 8);
3231 device->revision_id = config[8];
3232 device->subvendor_id = config[44] | (config[45] << 8);
3233 device->subdevice_id = config[46] | (config[47] << 8);
3239 static int drmParsePciDeviceInfo(int maj, int min,
3240 drmPciDeviceInfoPtr device,
3244 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3245 return parse_separate_sysfs_files(maj, min, device, true);
3247 if (parse_separate_sysfs_files(maj, min, device, false))
3248 return parse_config_sysfs_file(maj, min, device);
3251 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3252 struct drm_pciinfo pinfo;
3255 type = drmGetMinorType(min);
3259 fd = drmOpenMinor(min, 0, type);
3263 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3269 device->vendor_id = pinfo.vendor_id;
3270 device->device_id = pinfo.device_id;
3271 device->revision_id = pinfo.revision_id;
3272 device->subvendor_id = pinfo.subvendor_id;
3273 device->subdevice_id = pinfo.subdevice_id;
3277 #warning "Missing implementation of drmParsePciDeviceInfo"
3282 static void drmFreePlatformDevice(drmDevicePtr device)
3284 if (device->deviceinfo.platform) {
3285 if (device->deviceinfo.platform->compatible) {
3286 char **compatible = device->deviceinfo.platform->compatible;
3288 while (*compatible) {
3293 free(device->deviceinfo.platform->compatible);
3298 static void drmFreeHost1xDevice(drmDevicePtr device)
3300 if (device->deviceinfo.host1x) {
3301 if (device->deviceinfo.host1x->compatible) {
3302 char **compatible = device->deviceinfo.host1x->compatible;
3304 while (*compatible) {
3309 free(device->deviceinfo.host1x->compatible);
3314 drm_public void drmFreeDevice(drmDevicePtr *device)
3320 switch ((*device)->bustype) {
3321 case DRM_BUS_PLATFORM:
3322 drmFreePlatformDevice(*device);
3325 case DRM_BUS_HOST1X:
3326 drmFreeHost1xDevice(*device);
3335 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3339 if (devices == NULL)
3342 for (i = 0; i < count; i++)
3344 drmFreeDevice(&devices[i]);
3347 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3348 size_t bus_size, size_t device_size,
3351 size_t max_node_length, extra, size;
3352 drmDevicePtr device;
3356 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3357 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3359 size = sizeof(*device) + extra + bus_size + device_size;
3361 device = calloc(1, size);
3365 device->available_nodes = 1 << type;
3367 ptr = (char *)device + sizeof(*device);
3368 device->nodes = (char **)ptr;
3370 ptr += DRM_NODE_MAX * sizeof(void *);
3372 for (i = 0; i < DRM_NODE_MAX; i++) {
3373 device->nodes[i] = ptr;
3374 ptr += max_node_length;
3377 memcpy(device->nodes[type], node, max_node_length);
3384 static int drmProcessPciDevice(drmDevicePtr *device,
3385 const char *node, int node_type,
3386 int maj, int min, bool fetch_deviceinfo,
3393 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3394 sizeof(drmPciDeviceInfo), &addr);
3398 dev->bustype = DRM_BUS_PCI;
3400 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3402 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3406 // Fetch the device info if the user has requested it
3407 if (fetch_deviceinfo) {
3408 addr += sizeof(drmPciBusInfo);
3409 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3411 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3425 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3428 char path[PATH_MAX + 1], *value;
3429 unsigned int bus, dev;
3432 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3434 value = sysfs_uevent_get(path, "BUSNUM");
3438 ret = sscanf(value, "%03u", &bus);
3444 value = sysfs_uevent_get(path, "DEVNUM");
3448 ret = sscanf(value, "%03u", &dev);
3459 #warning "Missing implementation of drmParseUsbBusInfo"
3464 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3467 char path[PATH_MAX + 1], *value;
3468 unsigned int vendor, product;
3471 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3473 value = sysfs_uevent_get(path, "PRODUCT");
3477 ret = sscanf(value, "%x/%x", &vendor, &product);
3483 info->vendor = vendor;
3484 info->product = product;
3488 #warning "Missing implementation of drmParseUsbDeviceInfo"
3493 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3494 int node_type, int maj, int min,
3495 bool fetch_deviceinfo, uint32_t flags)
3501 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3502 sizeof(drmUsbDeviceInfo), &ptr);
3506 dev->bustype = DRM_BUS_USB;
3508 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3510 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3514 if (fetch_deviceinfo) {
3515 ptr += sizeof(drmUsbBusInfo);
3516 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3518 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3532 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3535 char path[PATH_MAX + 1], *name, *tmp_name;
3537 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3539 name = sysfs_uevent_get(path, "OF_FULLNAME");
3542 /* If the device lacks OF data, pick the MODALIAS info */
3543 name = sysfs_uevent_get(path, "MODALIAS");
3547 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3548 tmp_name = strrchr(name, ':');
3556 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3557 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3562 #warning "Missing implementation of drmParseOFBusInfo"
3567 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3570 char path[PATH_MAX + 1], *value, *tmp_name;
3571 unsigned int count, i;
3574 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3576 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3578 sscanf(value, "%u", &count);
3581 /* Assume one entry if the device lack OF data */
3585 *compatible = calloc(count + 1, sizeof(char *));
3589 for (i = 0; i < count; i++) {
3590 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3593 /* If the device lacks OF data, pick the MODALIAS info */
3594 value = sysfs_uevent_get(path, "MODALIAS");
3600 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3601 tmp_name = strrchr(value, ':');
3606 tmp_name = strdup(tmp_name + 1);
3610 (*compatible)[i] = tmp_name;
3617 free((*compatible)[i]);
3622 #warning "Missing implementation of drmParseOFDeviceInfo"
3627 static int drmProcessPlatformDevice(drmDevicePtr *device,
3628 const char *node, int node_type,
3629 int maj, int min, bool fetch_deviceinfo,
3636 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3637 sizeof(drmPlatformDeviceInfo), &ptr);
3641 dev->bustype = DRM_BUS_PLATFORM;
3643 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3645 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3649 if (fetch_deviceinfo) {
3650 ptr += sizeof(drmPlatformBusInfo);
3651 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3653 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3667 static int drmProcessHost1xDevice(drmDevicePtr *device,
3668 const char *node, int node_type,
3669 int maj, int min, bool fetch_deviceinfo,
3676 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3677 sizeof(drmHost1xDeviceInfo), &ptr);
3681 dev->bustype = DRM_BUS_HOST1X;
3683 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3685 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3689 if (fetch_deviceinfo) {
3690 ptr += sizeof(drmHost1xBusInfo);
3691 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3693 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3708 process_device(drmDevicePtr *device, const char *d_name,
3709 int req_subsystem_type,
3710 bool fetch_deviceinfo, uint32_t flags)
3713 char node[PATH_MAX + 1];
3714 int node_type, subsystem_type;
3715 unsigned int maj, min;
3717 node_type = drmGetNodeType(d_name);
3721 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3722 if (stat(node, &sbuf))
3725 maj = major(sbuf.st_rdev);
3726 min = minor(sbuf.st_rdev);
3728 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3731 subsystem_type = drmParseSubsystemType(maj, min);
3732 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3735 switch (subsystem_type) {
3737 return drmProcessPciDevice(device, node, node_type, maj, min,
3738 fetch_deviceinfo, flags);
3740 return drmProcessUsbDevice(device, node, node_type, maj, min,
3741 fetch_deviceinfo, flags);
3742 case DRM_BUS_PLATFORM:
3743 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3744 fetch_deviceinfo, flags);
3745 case DRM_BUS_HOST1X:
3746 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3747 fetch_deviceinfo, flags);
3753 /* Consider devices located on the same bus as duplicate and fold the respective
3754 * entries into a single one.
3756 * Note: this leaves "gaps" in the array, while preserving the length.
3758 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3760 int node_type, i, j;
3762 for (i = 0; i < count; i++) {
3763 for (j = i + 1; j < count; j++) {
3764 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3765 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3766 node_type = log2(local_devices[j]->available_nodes);
3767 memcpy(local_devices[i]->nodes[node_type],
3768 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3769 drmFreeDevice(&local_devices[j]);
3775 /* Check that the given flags are valid returning 0 on success */
3777 drm_device_validate_flags(uint32_t flags)
3779 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3783 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3787 for (int i = 0; i < DRM_NODE_MAX; i++) {
3788 if (device->available_nodes & 1 << i) {
3789 if (stat(device->nodes[i], &sbuf) == 0 &&
3790 sbuf.st_rdev == find_rdev)
3798 * The kernel drm core has a number of places that assume maximum of
3799 * 3x64 devices nodes. That's 64 for each of primary, control and
3800 * render nodes. Rounded it up to 256 for simplicity.
3802 #define MAX_DRM_NODES 256
3805 * Get information about the opened drm device
3807 * \param fd file descriptor of the drm device
3808 * \param flags feature/behaviour bitmask
3809 * \param device the address of a drmDevicePtr where the information
3810 * will be allocated in stored
3812 * \return zero on success, negative error code otherwise.
3814 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3815 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3817 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3821 * DRI device nodes on OpenBSD are not in their own directory, they reside
3822 * in /dev along with a large number of statically generated /dev nodes.
3823 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3827 char node[PATH_MAX + 1];
3828 const char *dev_name;
3829 int node_type, subsystem_type;
3830 int maj, min, n, ret;
3832 if (fd == -1 || device == NULL)
3835 if (fstat(fd, &sbuf))
3838 maj = major(sbuf.st_rdev);
3839 min = minor(sbuf.st_rdev);
3841 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3844 node_type = drmGetMinorType(min);
3845 if (node_type == -1)
3848 dev_name = drmGetDeviceName(node_type);
3852 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
3853 if (n == -1 || n >= PATH_MAX)
3855 if (stat(node, &sbuf))
3858 subsystem_type = drmParseSubsystemType(maj, min);
3859 if (subsystem_type != DRM_BUS_PCI)
3862 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3870 drmDevicePtr local_devices[MAX_DRM_NODES];
3873 struct dirent *dent;
3877 int ret, i, node_count;
3880 if (drm_device_validate_flags(flags))
3883 if (fd == -1 || device == NULL)
3886 if (fstat(fd, &sbuf))
3889 find_rdev = sbuf.st_rdev;
3890 maj = major(sbuf.st_rdev);
3891 min = minor(sbuf.st_rdev);
3893 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3896 subsystem_type = drmParseSubsystemType(maj, min);
3897 if (subsystem_type < 0)
3898 return subsystem_type;
3900 sysdir = opendir(DRM_DIR_NAME);
3905 while ((dent = readdir(sysdir))) {
3906 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3910 if (i >= MAX_DRM_NODES) {
3911 fprintf(stderr, "More than %d drm nodes detected. "
3912 "Please report a bug - that should not happen.\n"
3913 "Skipping extra nodes\n", MAX_DRM_NODES);
3916 local_devices[i] = d;
3921 drmFoldDuplicatedDevices(local_devices, node_count);
3925 for (i = 0; i < node_count; i++) {
3926 if (!local_devices[i])
3929 if (drm_device_has_rdev(local_devices[i], find_rdev))
3930 *device = local_devices[i];
3932 drmFreeDevice(&local_devices[i]);
3936 if (*device == NULL)
3943 * Get information about the opened drm device
3945 * \param fd file descriptor of the drm device
3946 * \param device the address of a drmDevicePtr where the information
3947 * will be allocated in stored
3949 * \return zero on success, negative error code otherwise.
3951 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3953 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
3957 * Get drm devices on the system
3959 * \param flags feature/behaviour bitmask
3960 * \param devices the array of devices with drmDevicePtr elements
3961 * can be NULL to get the device number first
3962 * \param max_devices the maximum number of devices for the array
3964 * \return on error - negative error code,
3965 * if devices is NULL - total number of devices available on the system,
3966 * alternatively the number of devices stored in devices[], which is
3967 * capped by the max_devices.
3969 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
3970 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3972 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
3975 drmDevicePtr local_devices[MAX_DRM_NODES];
3976 drmDevicePtr device;
3978 struct dirent *dent;
3979 int ret, i, node_count, device_count;
3981 if (drm_device_validate_flags(flags))
3984 sysdir = opendir(DRM_DIR_NAME);
3989 while ((dent = readdir(sysdir))) {
3990 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
3994 if (i >= MAX_DRM_NODES) {
3995 fprintf(stderr, "More than %d drm nodes detected. "
3996 "Please report a bug - that should not happen.\n"
3997 "Skipping extra nodes\n", MAX_DRM_NODES);
4000 local_devices[i] = device;
4005 drmFoldDuplicatedDevices(local_devices, node_count);
4008 for (i = 0; i < node_count; i++) {
4009 if (!local_devices[i])
4012 if ((devices != NULL) && (device_count < max_devices))
4013 devices[device_count] = local_devices[i];
4015 drmFreeDevice(&local_devices[i]);
4021 return device_count;
4025 * Get drm devices on the system
4027 * \param devices the array of devices with drmDevicePtr elements
4028 * can be NULL to get the device number first
4029 * \param max_devices the maximum number of devices for the array
4031 * \return on error - negative error code,
4032 * if devices is NULL - total number of devices available on the system,
4033 * alternatively the number of devices stored in devices[], which is
4034 * capped by the max_devices.
4036 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4038 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4041 drm_public char *drmGetDeviceNameFromFd2(int fd)
4045 char path[PATH_MAX + 1], *value;
4046 unsigned int maj, min;
4048 if (fstat(fd, &sbuf))
4051 maj = major(sbuf.st_rdev);
4052 min = minor(sbuf.st_rdev);
4054 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4057 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4059 value = sysfs_uevent_get(path, "DEVNAME");
4063 snprintf(path, sizeof(path), "/dev/%s", value);
4066 return strdup(path);
4069 char node[PATH_MAX + 1];
4070 const char *dev_name;
4074 if (fstat(fd, &sbuf))
4077 maj = major(sbuf.st_rdev);
4078 min = minor(sbuf.st_rdev);
4080 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4083 node_type = drmGetMinorType(min);
4084 if (node_type == -1)
4087 dev_name = drmGetDeviceName(node_type);
4091 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4092 if (n == -1 || n >= PATH_MAX)
4095 return strdup(node);
4099 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4101 struct drm_syncobj_create args;
4107 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4110 *handle = args.handle;
4114 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4116 struct drm_syncobj_destroy args;
4119 args.handle = handle;
4120 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4123 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4125 struct drm_syncobj_handle args;
4130 args.handle = handle;
4131 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4138 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4140 struct drm_syncobj_handle args;
4146 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4149 *handle = args.handle;
4153 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4156 struct drm_syncobj_handle args;
4159 args.fd = sync_file_fd;
4160 args.handle = handle;
4161 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4162 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4165 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4168 struct drm_syncobj_handle args;
4173 args.handle = handle;
4174 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4175 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4178 *sync_file_fd = args.fd;
4182 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4183 int64_t timeout_nsec, unsigned flags,
4184 uint32_t *first_signaled)
4186 struct drm_syncobj_wait args;
4190 args.handles = (uintptr_t)handles;
4191 args.timeout_nsec = timeout_nsec;
4192 args.count_handles = num_handles;
4195 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4200 *first_signaled = args.first_signaled;
4204 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4205 uint32_t handle_count)
4207 struct drm_syncobj_array args;
4211 args.handles = (uintptr_t)handles;
4212 args.count_handles = handle_count;
4214 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4218 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4219 uint32_t handle_count)
4221 struct drm_syncobj_array args;
4225 args.handles = (uintptr_t)handles;
4226 args.count_handles = handle_count;
4228 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4232 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4233 uint64_t *points, uint32_t handle_count)
4235 struct drm_syncobj_timeline_array args;
4239 args.handles = (uintptr_t)handles;
4240 args.points = (uintptr_t)points;
4241 args.count_handles = handle_count;
4243 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4247 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4248 unsigned num_handles,
4249 int64_t timeout_nsec, unsigned flags,
4250 uint32_t *first_signaled)
4252 struct drm_syncobj_timeline_wait args;
4256 args.handles = (uintptr_t)handles;
4257 args.points = (uintptr_t)points;
4258 args.timeout_nsec = timeout_nsec;
4259 args.count_handles = num_handles;
4262 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4267 *first_signaled = args.first_signaled;
4272 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4273 uint32_t handle_count)
4275 struct drm_syncobj_timeline_array args;
4279 args.handles = (uintptr_t)handles;
4280 args.points = (uintptr_t)points;
4281 args.count_handles = handle_count;
4283 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4289 drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4290 uint32_t handle_count, uint32_t flags)
4292 struct drm_syncobj_timeline_array args;
4295 args.handles = (uintptr_t)handles;
4296 args.points = (uintptr_t)points;
4297 args.count_handles = handle_count;
4300 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4304 drm_public int drmSyncobjTransfer(int fd,
4305 uint32_t dst_handle, uint64_t dst_point,
4306 uint32_t src_handle, uint64_t src_point,
4309 struct drm_syncobj_transfer args;
4313 args.src_handle = src_handle;
4314 args.dst_handle = dst_handle;
4315 args.src_point = src_point;
4316 args.dst_point = dst_point;
4319 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);