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 static bool drmNodeIsDRM(int maj, int min);
122 drm_public void drmSetServerInfo(drmServerInfoPtr info)
124 drm_server_info = info;
128 * Output a message to stderr.
130 * \param format printf() like format string.
133 * This function is a wrapper around vfprintf().
136 static int DRM_PRINTFLIKE(1, 0)
137 drmDebugPrint(const char *format, va_list ap)
139 return vfprintf(stderr, format, ap);
143 drmMsg(const char *format, ...)
147 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
148 (drm_server_info && drm_server_info->debug_print))
150 va_start(ap, format);
151 if (drm_server_info) {
152 drm_server_info->debug_print(format,ap);
154 drmDebugPrint(format, ap);
160 static void *drmHashTable = NULL; /* Context switch callbacks */
162 drm_public void *drmGetHashTable(void)
167 drm_public void *drmMalloc(int size)
169 return calloc(1, size);
172 drm_public void drmFree(void *pt)
178 * Call ioctl, restarting if it is interrupted
181 drmIoctl(int fd, unsigned long request, void *arg)
186 ret = ioctl(fd, request, arg);
187 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
191 static unsigned long drmGetKeyFromFd(int fd)
200 drm_public drmHashEntry *drmGetEntry(int fd)
202 unsigned long key = drmGetKeyFromFd(fd);
207 drmHashTable = drmHashCreate();
209 if (drmHashLookup(drmHashTable, key, &value)) {
210 entry = drmMalloc(sizeof(*entry));
213 entry->tagTable = drmHashCreate();
214 drmHashInsert(drmHashTable, key, entry);
222 * Compare two busid strings
227 * \return 1 if matched.
230 * This function compares two bus ID strings. It understands the older
231 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
232 * domain, b is bus, d is device, f is function.
234 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
236 /* First, check if the IDs are exactly the same */
237 if (strcasecmp(id1, id2) == 0)
240 /* Try to match old/new-style PCI bus IDs. */
241 if (strncasecmp(id1, "pci", 3) == 0) {
242 unsigned int o1, b1, d1, f1;
243 unsigned int o2, b2, d2, f2;
246 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
249 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
254 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
257 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
262 /* If domains aren't properly supported by the kernel interface,
263 * just ignore them, which sucks less than picking a totally random
264 * card with "open by name"
269 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
278 * Handles error checking for chown call.
280 * \param path to file.
281 * \param id of the new owner.
282 * \param id of the new group.
284 * \return zero if success or -1 if failure.
287 * Checks for failure. If failure was caused by signal call chown again.
288 * If any other failure happened then it will output error message using
292 static int chown_check_return(const char *path, uid_t owner, gid_t group)
297 rv = chown(path, owner, group);
298 } while (rv != 0 && errno == EINTR);
303 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
304 path, errno, strerror(errno));
309 static const char *drmGetDeviceName(int type)
312 case DRM_NODE_PRIMARY:
314 case DRM_NODE_CONTROL:
315 return DRM_CONTROL_DEV_NAME;
316 case DRM_NODE_RENDER:
317 return DRM_RENDER_DEV_NAME;
323 * Open the DRM device, creating it if necessary.
325 * \param dev major and minor numbers of the device.
326 * \param minor minor number of the device.
328 * \return a file descriptor on success, or a negative value on error.
331 * Assembles the device name from \p minor and opens it, creating the device
332 * special file node with the major and minor numbers specified by \p dev and
333 * parent directory if necessary and was called by root.
335 static int drmOpenDevice(dev_t dev, int minor, int type)
338 const char *dev_name = drmGetDeviceName(type);
339 char buf[DRM_NODE_NAME_MAX];
341 mode_t devmode = DRM_DEV_MODE, serv_mode;
344 int isroot = !geteuid();
345 uid_t user = DRM_DEV_UID;
346 gid_t group = DRM_DEV_GID;
352 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
353 drmMsg("drmOpenDevice: node name is %s\n", buf);
355 if (drm_server_info && drm_server_info->get_perms) {
356 drm_server_info->get_perms(&serv_group, &serv_mode);
357 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
358 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
362 if (stat(DRM_DIR_NAME, &st)) {
364 return DRM_ERR_NOT_ROOT;
365 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
366 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
367 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
370 /* Check if the device node exists and create it if necessary. */
371 if (stat(buf, &st)) {
373 return DRM_ERR_NOT_ROOT;
375 mknod(buf, S_IFCHR | devmode, dev);
378 if (drm_server_info && drm_server_info->get_perms) {
379 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
380 chown_check_return(buf, user, group);
384 /* if we modprobed then wait for udev */
388 if (stat(DRM_DIR_NAME, &st)) {
392 if (udev_count == 50)
397 if (stat(buf, &st)) {
401 if (udev_count == 50)
408 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
409 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
410 fd, fd < 0 ? strerror(errno) : "OK");
415 /* Check if the device node is not what we expect it to be, and recreate it
416 * and try again if so.
418 if (st.st_rdev != dev) {
420 return DRM_ERR_NOT_ROOT;
422 mknod(buf, S_IFCHR | devmode, dev);
423 if (drm_server_info && drm_server_info->get_perms) {
424 chown_check_return(buf, user, group);
428 fd = open(buf, O_RDWR | O_CLOEXEC, 0);
429 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
430 fd, fd < 0 ? strerror(errno) : "OK");
434 drmMsg("drmOpenDevice: Open failed\n");
442 * Open the DRM device
444 * \param minor device minor number.
445 * \param create allow to create the device if set.
447 * \return a file descriptor on success, or a negative value on error.
450 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
451 * name from \p minor and opens it.
453 static int drmOpenMinor(int minor, int create, int type)
456 char buf[DRM_NODE_NAME_MAX];
457 const char *dev_name = drmGetDeviceName(type);
460 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
465 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
466 if ((fd = open(buf, O_RDWR | O_CLOEXEC, 0)) >= 0)
473 * Determine whether the DRM kernel driver has been loaded.
475 * \return 1 if the DRM driver is loaded, 0 otherwise.
478 * Determine the presence of the kernel driver by attempting to open the 0
479 * minor and get version information. For backward compatibility with older
480 * Linux implementations, /proc/dri is also checked.
482 drm_public int drmAvailable(void)
484 drmVersionPtr version;
488 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
490 /* Try proc for backward Linux compatibility */
491 if (!access("/proc/dri/0", R_OK))
497 if ((version = drmGetVersion(fd))) {
499 drmFreeVersion(version);
506 static int drmGetMinorBase(int type)
509 case DRM_NODE_PRIMARY:
511 case DRM_NODE_CONTROL:
513 case DRM_NODE_RENDER:
520 static int drmGetMinorType(int major, int minor)
523 char name[SPECNAMELEN];
526 if (!devname_r(makedev(major, minor), S_IFCHR, name, sizeof(name)))
529 if (sscanf(name, "drm/%d", &id) != 1) {
530 // If not in /dev/drm/ we have the type in the name
531 if (sscanf(name, "dri/card%d\n", &id) >= 1)
532 return DRM_NODE_PRIMARY;
533 else if (sscanf(name, "dri/control%d\n", &id) >= 1)
534 return DRM_NODE_CONTROL;
535 else if (sscanf(name, "dri/renderD%d\n", &id) >= 1)
536 return DRM_NODE_RENDER;
542 int type = minor >> 6;
548 case DRM_NODE_PRIMARY:
549 case DRM_NODE_CONTROL:
550 case DRM_NODE_RENDER:
557 static const char *drmGetMinorName(int type)
560 case DRM_NODE_PRIMARY:
561 return DRM_PRIMARY_MINOR_NAME;
562 case DRM_NODE_CONTROL:
563 return DRM_CONTROL_MINOR_NAME;
564 case DRM_NODE_RENDER:
565 return DRM_RENDER_MINOR_NAME;
572 * Open the device by bus ID.
574 * \param busid bus ID.
575 * \param type device node type.
577 * \return a file descriptor on success, or a negative value on error.
580 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
581 * comparing the device bus ID with the one supplied.
583 * \sa drmOpenMinor() and drmGetBusid().
585 static int drmOpenByBusid(const char *busid, int type)
587 int i, pci_domain_ok = 1;
591 int base = drmGetMinorBase(type);
596 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
597 for (i = base; i < base + DRM_MAX_MINOR; i++) {
598 fd = drmOpenMinor(i, 1, type);
599 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
601 /* We need to try for 1.4 first for proper PCI domain support
602 * and if that fails, we know the kernel is busted
606 sv.drm_dd_major = -1; /* Don't care */
607 sv.drm_dd_minor = -1; /* Don't care */
608 if (drmSetInterfaceVersion(fd, &sv)) {
614 sv.drm_dd_major = -1; /* Don't care */
615 sv.drm_dd_minor = -1; /* Don't care */
616 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
617 drmSetInterfaceVersion(fd, &sv);
619 buf = drmGetBusid(fd);
620 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
621 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
635 * Open the device by name.
637 * \param name driver name.
638 * \param type the device node type.
640 * \return a file descriptor on success, or a negative value on error.
643 * This function opens the first minor number that matches the driver name and
644 * isn't already in use. If it's in use it then it will already have a bus ID
647 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
649 static int drmOpenByName(const char *name, int type)
653 drmVersionPtr version;
655 int base = drmGetMinorBase(type);
661 * Open the first minor number that matches the driver name and isn't
662 * already in use. If it's in use it will have a busid assigned already.
664 for (i = base; i < base + DRM_MAX_MINOR; i++) {
665 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
666 if ((version = drmGetVersion(fd))) {
667 if (!strcmp(version->name, name)) {
668 drmFreeVersion(version);
669 id = drmGetBusid(fd);
670 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
679 drmFreeVersion(version);
687 /* Backward-compatibility /proc support */
688 for (i = 0; i < 8; i++) {
689 char proc_name[64], buf[512];
690 char *driver, *pt, *devstring;
693 sprintf(proc_name, "/proc/dri/%d/name", i);
694 if ((fd = open(proc_name, 0, 0)) >= 0) {
695 retcode = read(fd, buf, sizeof(buf)-1);
698 buf[retcode-1] = '\0';
699 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
701 if (*pt) { /* Device is next */
703 if (!strcmp(driver, name)) { /* Match */
704 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
706 if (*pt) { /* Found busid */
707 return drmOpenByBusid(++pt, type);
708 } else { /* No busid */
709 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
723 * Open the DRM device.
725 * Looks up the specified name and bus ID, and opens the device found. The
726 * entry in /dev/dri is created if necessary and if called by root.
728 * \param name driver name. Not referenced if bus ID is supplied.
729 * \param busid bus ID. Zero if not known.
731 * \return a file descriptor on success, or a negative value on error.
734 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
737 drm_public int drmOpen(const char *name, const char *busid)
739 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
743 * Open the DRM device with specified type.
745 * Looks up the specified name and bus ID, and opens the device found. The
746 * entry in /dev/dri is created if necessary and if called by root.
748 * \param name driver name. Not referenced if bus ID is supplied.
749 * \param busid bus ID. Zero if not known.
750 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
752 * \return a file descriptor on success, or a negative value on error.
755 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
758 drm_public int drmOpenWithType(const char *name, const char *busid, int type)
760 if (name != NULL && drm_server_info &&
761 drm_server_info->load_module && !drmAvailable()) {
762 /* try to load the kernel module */
763 if (!drm_server_info->load_module(name)) {
764 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
770 int fd = drmOpenByBusid(busid, type);
776 return drmOpenByName(name, type);
781 drm_public int drmOpenControl(int minor)
783 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
786 drm_public int drmOpenRender(int minor)
788 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
792 * Free the version information returned by drmGetVersion().
794 * \param v pointer to the version information.
797 * It frees the memory pointed by \p %v as well as all the non-null strings
800 drm_public void drmFreeVersion(drmVersionPtr v)
812 * Free the non-public version information returned by the kernel.
814 * \param v pointer to the version information.
817 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
818 * the non-null strings pointers in it.
820 static void drmFreeKernelVersion(drm_version_t *v)
832 * Copy version information.
834 * \param d destination pointer.
835 * \param s source pointer.
838 * Used by drmGetVersion() to translate the information returned by the ioctl
839 * interface in a private structure into the public structure counterpart.
841 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
843 d->version_major = s->version_major;
844 d->version_minor = s->version_minor;
845 d->version_patchlevel = s->version_patchlevel;
846 d->name_len = s->name_len;
847 d->name = strdup(s->name);
848 d->date_len = s->date_len;
849 d->date = strdup(s->date);
850 d->desc_len = s->desc_len;
851 d->desc = strdup(s->desc);
856 * Query the driver version information.
858 * \param fd file descriptor.
860 * \return pointer to a drmVersion structure which should be freed with
863 * \note Similar information is available via /proc/dri.
866 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
867 * first with zeros to get the string lengths, and then the actually strings.
868 * It also null-terminates them since they might not be already.
870 drm_public drmVersionPtr drmGetVersion(int fd)
872 drmVersionPtr retval;
873 drm_version_t *version = drmMalloc(sizeof(*version));
875 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
876 drmFreeKernelVersion(version);
880 if (version->name_len)
881 version->name = drmMalloc(version->name_len + 1);
882 if (version->date_len)
883 version->date = drmMalloc(version->date_len + 1);
884 if (version->desc_len)
885 version->desc = drmMalloc(version->desc_len + 1);
887 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
888 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
889 drmFreeKernelVersion(version);
893 /* The results might not be null-terminated strings, so terminate them. */
894 if (version->name_len) version->name[version->name_len] = '\0';
895 if (version->date_len) version->date[version->date_len] = '\0';
896 if (version->desc_len) version->desc[version->desc_len] = '\0';
898 retval = drmMalloc(sizeof(*retval));
899 drmCopyVersion(retval, version);
900 drmFreeKernelVersion(version);
906 * Get version information for the DRM user space library.
908 * This version number is driver independent.
910 * \param fd file descriptor.
912 * \return version information.
915 * This function allocates and fills a drm_version structure with a hard coded
918 drm_public drmVersionPtr drmGetLibVersion(int fd)
920 drm_version_t *version = drmMalloc(sizeof(*version));
923 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
924 * revision 1.0.x = original DRM interface with no drmGetLibVersion
925 * entry point and many drm<Device> extensions
926 * revision 1.1.x = added drmCommand entry points for device extensions
927 * added drmGetLibVersion to identify libdrm.a version
928 * revision 1.2.x = added drmSetInterfaceVersion
929 * modified drmOpen to handle both busid and name
930 * revision 1.3.x = added server + memory manager
932 version->version_major = 1;
933 version->version_minor = 3;
934 version->version_patchlevel = 0;
936 return (drmVersionPtr)version;
939 drm_public int drmGetCap(int fd, uint64_t capability, uint64_t *value)
941 struct drm_get_cap cap;
945 cap.capability = capability;
947 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
955 drm_public int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
957 struct drm_set_client_cap cap;
960 cap.capability = capability;
963 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
967 * Free the bus ID information.
969 * \param busid bus ID information string as given by drmGetBusid().
972 * This function is just frees the memory pointed by \p busid.
974 drm_public void drmFreeBusid(const char *busid)
976 drmFree((void *)busid);
981 * Get the bus ID of the device.
983 * \param fd file descriptor.
985 * \return bus ID string.
988 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
989 * get the string length and data, passing the arguments in a drm_unique
992 drm_public char *drmGetBusid(int fd)
998 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
1000 u.unique = drmMalloc(u.unique_len + 1);
1001 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) {
1005 u.unique[u.unique_len] = '\0';
1012 * Set the bus ID of the device.
1014 * \param fd file descriptor.
1015 * \param busid bus ID string.
1017 * \return zero on success, negative on failure.
1020 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
1021 * the arguments in a drm_unique structure.
1023 drm_public int drmSetBusid(int fd, const char *busid)
1028 u.unique = (char *)busid;
1029 u.unique_len = strlen(busid);
1031 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1037 drm_public int drmGetMagic(int fd, drm_magic_t * magic)
1044 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1046 *magic = auth.magic;
1050 drm_public int drmAuthMagic(int fd, drm_magic_t magic)
1056 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1062 * Specifies a range of memory that is available for mapping by a
1065 * \param fd file descriptor.
1066 * \param offset usually the physical address. The actual meaning depends of
1067 * the \p type parameter. See below.
1068 * \param size of the memory in bytes.
1069 * \param type type of the memory to be mapped.
1070 * \param flags combination of several flags to modify the function actions.
1071 * \param handle will be set to a value that may be used as the offset
1072 * parameter for mmap().
1074 * \return zero on success or a negative value on error.
1076 * \par Mapping the frame buffer
1077 * For the frame buffer
1078 * - \p offset will be the physical address of the start of the frame buffer,
1079 * - \p size will be the size of the frame buffer in bytes, and
1080 * - \p type will be DRM_FRAME_BUFFER.
1083 * The area mapped will be uncached. If MTRR support is available in the
1084 * kernel, the frame buffer area will be set to write combining.
1086 * \par Mapping the MMIO register area
1087 * For the MMIO register area,
1088 * - \p offset will be the physical address of the start of the register area,
1089 * - \p size will be the size of the register area bytes, and
1090 * - \p type will be DRM_REGISTERS.
1092 * The area mapped will be uncached.
1094 * \par Mapping the SAREA
1096 * - \p offset will be ignored and should be set to zero,
1097 * - \p size will be the desired size of the SAREA in bytes,
1098 * - \p type will be DRM_SHM.
1101 * A shared memory area of the requested size will be created and locked in
1102 * kernel memory. This area may be mapped into client-space by using the handle
1105 * \note May only be called by root.
1108 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1109 * the arguments in a drm_map structure.
1111 drm_public int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1112 drmMapFlags flags, drm_handle_t *handle)
1117 map.offset = offset;
1121 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1124 *handle = (drm_handle_t)(uintptr_t)map.handle;
1128 drm_public int drmRmMap(int fd, drm_handle_t handle)
1133 map.handle = (void *)(uintptr_t)handle;
1135 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1141 * Make buffers available for DMA transfers.
1143 * \param fd file descriptor.
1144 * \param count number of buffers.
1145 * \param size size of each buffer.
1146 * \param flags buffer allocation flags.
1147 * \param agp_offset offset in the AGP aperture
1149 * \return number of buffers allocated, negative on error.
1152 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1156 drm_public int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1159 drm_buf_desc_t request;
1162 request.count = count;
1163 request.size = size;
1164 request.flags = flags;
1165 request.agp_start = agp_offset;
1167 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1169 return request.count;
1172 drm_public int drmMarkBufs(int fd, double low, double high)
1174 drm_buf_info_t info;
1179 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1185 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1188 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1189 int retval = -errno;
1194 for (i = 0; i < info.count; i++) {
1195 info.list[i].low_mark = low * info.list[i].count;
1196 info.list[i].high_mark = high * info.list[i].count;
1197 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1198 int retval = -errno;
1211 * \param fd file descriptor.
1212 * \param count number of buffers to free.
1213 * \param list list of buffers to be freed.
1215 * \return zero on success, or a negative value on failure.
1217 * \note This function is primarily used for debugging.
1220 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1221 * the arguments in a drm_buf_free structure.
1223 drm_public int drmFreeBufs(int fd, int count, int *list)
1225 drm_buf_free_t request;
1228 request.count = count;
1229 request.list = list;
1230 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1239 * \param fd file descriptor.
1242 * This function closes the file descriptor.
1244 drm_public int drmClose(int fd)
1246 unsigned long key = drmGetKeyFromFd(fd);
1247 drmHashEntry *entry = drmGetEntry(fd);
1249 drmHashDestroy(entry->tagTable);
1252 entry->tagTable = NULL;
1254 drmHashDelete(drmHashTable, key);
1262 * Map a region of memory.
1264 * \param fd file descriptor.
1265 * \param handle handle returned by drmAddMap().
1266 * \param size size in bytes. Must match the size used by drmAddMap().
1267 * \param address will contain the user-space virtual address where the mapping
1270 * \return zero on success, or a negative value on failure.
1273 * This function is a wrapper for mmap().
1275 drm_public int drmMap(int fd, drm_handle_t handle, drmSize size,
1276 drmAddressPtr address)
1278 static unsigned long pagesize_mask = 0;
1284 pagesize_mask = getpagesize() - 1;
1286 size = (size + pagesize_mask) & ~pagesize_mask;
1288 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1289 if (*address == MAP_FAILED)
1296 * Unmap mappings obtained with drmMap().
1298 * \param address address as given by drmMap().
1299 * \param size size in bytes. Must match the size used by drmMap().
1301 * \return zero on success, or a negative value on failure.
1304 * This function is a wrapper for munmap().
1306 drm_public int drmUnmap(drmAddress address, drmSize size)
1308 return drm_munmap(address, size);
1311 drm_public drmBufInfoPtr drmGetBufInfo(int fd)
1313 drm_buf_info_t info;
1314 drmBufInfoPtr retval;
1319 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1323 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1326 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1331 retval = drmMalloc(sizeof(*retval));
1332 retval->count = info.count;
1333 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1334 for (i = 0; i < info.count; i++) {
1335 retval->list[i].count = info.list[i].count;
1336 retval->list[i].size = info.list[i].size;
1337 retval->list[i].low_mark = info.list[i].low_mark;
1338 retval->list[i].high_mark = info.list[i].high_mark;
1347 * Map all DMA buffers into client-virtual space.
1349 * \param fd file descriptor.
1351 * \return a pointer to a ::drmBufMap structure.
1353 * \note The client may not use these buffers until obtaining buffer indices
1357 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1358 * information about the buffers in a drm_buf_map structure into the
1359 * client-visible data structures.
1361 drm_public drmBufMapPtr drmMapBufs(int fd)
1364 drmBufMapPtr retval;
1368 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1374 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1377 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1382 retval = drmMalloc(sizeof(*retval));
1383 retval->count = bufs.count;
1384 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1385 for (i = 0; i < bufs.count; i++) {
1386 retval->list[i].idx = bufs.list[i].idx;
1387 retval->list[i].total = bufs.list[i].total;
1388 retval->list[i].used = 0;
1389 retval->list[i].address = bufs.list[i].address;
1398 * Unmap buffers allocated with drmMapBufs().
1400 * \return zero on success, or negative value on failure.
1403 * Calls munmap() for every buffer stored in \p bufs and frees the
1404 * memory allocated by drmMapBufs().
1406 drm_public int drmUnmapBufs(drmBufMapPtr bufs)
1410 for (i = 0; i < bufs->count; i++) {
1411 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1414 drmFree(bufs->list);
1420 #define DRM_DMA_RETRY 16
1423 * Reserve DMA buffers.
1425 * \param fd file descriptor.
1428 * \return zero on success, or a negative value on failure.
1431 * Assemble the arguments into a drm_dma structure and keeps issuing the
1432 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1434 drm_public int drmDMA(int fd, drmDMAReqPtr request)
1439 dma.context = request->context;
1440 dma.send_count = request->send_count;
1441 dma.send_indices = request->send_list;
1442 dma.send_sizes = request->send_sizes;
1443 dma.flags = request->flags;
1444 dma.request_count = request->request_count;
1445 dma.request_size = request->request_size;
1446 dma.request_indices = request->request_list;
1447 dma.request_sizes = request->request_sizes;
1448 dma.granted_count = 0;
1451 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1452 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1455 request->granted_count = dma.granted_count;
1464 * Obtain heavyweight hardware lock.
1466 * \param fd file descriptor.
1467 * \param context context.
1468 * \param flags flags that determine the state of the hardware when the function
1471 * \return always zero.
1474 * This function translates the arguments into a drm_lock structure and issue
1475 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1477 drm_public int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1482 lock.context = context;
1484 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1485 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1486 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1487 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1488 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1489 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1491 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1497 * Release the hardware lock.
1499 * \param fd file descriptor.
1500 * \param context context.
1502 * \return zero on success, or a negative value on failure.
1505 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1506 * argument in a drm_lock structure.
1508 drm_public int drmUnlock(int fd, drm_context_t context)
1513 lock.context = context;
1514 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1517 drm_public drm_context_t *drmGetReservedContextList(int fd, int *count)
1521 drm_context_t * retval;
1525 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1531 if (!(list = drmMalloc(res.count * sizeof(*list))))
1533 if (!(retval = drmMalloc(res.count * sizeof(*retval))))
1536 res.contexts = list;
1537 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1538 goto err_free_context;
1540 for (i = 0; i < res.count; i++)
1541 retval[i] = list[i].handle;
1554 drm_public void drmFreeReservedContextList(drm_context_t *pt)
1562 * Used by the X server during GLXContext initialization. This causes
1563 * per-context kernel-level resources to be allocated.
1565 * \param fd file descriptor.
1566 * \param handle is set on success. To be used by the client when requesting DMA
1567 * dispatch with drmDMA().
1569 * \return zero on success, or a negative value on failure.
1571 * \note May only be called by root.
1574 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1575 * argument in a drm_ctx structure.
1577 drm_public int drmCreateContext(int fd, drm_context_t *handle)
1582 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1584 *handle = ctx.handle;
1588 drm_public int drmSwitchToContext(int fd, drm_context_t context)
1593 ctx.handle = context;
1594 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1599 drm_public int drmSetContextFlags(int fd, drm_context_t context,
1600 drm_context_tFlags flags)
1605 * Context preserving means that no context switches are done between DMA
1606 * buffers from one context and the next. This is suitable for use in the
1607 * X server (which promises to maintain hardware context), or in the
1608 * client-side library when buffers are swapped on behalf of two threads.
1611 ctx.handle = context;
1612 if (flags & DRM_CONTEXT_PRESERVED)
1613 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1614 if (flags & DRM_CONTEXT_2DONLY)
1615 ctx.flags |= _DRM_CONTEXT_2DONLY;
1616 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1621 drm_public int drmGetContextFlags(int fd, drm_context_t context,
1622 drm_context_tFlagsPtr flags)
1627 ctx.handle = context;
1628 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1631 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1632 *flags |= DRM_CONTEXT_PRESERVED;
1633 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1634 *flags |= DRM_CONTEXT_2DONLY;
1641 * Free any kernel-level resources allocated with drmCreateContext() associated
1644 * \param fd file descriptor.
1645 * \param handle handle given by drmCreateContext().
1647 * \return zero on success, or a negative value on failure.
1649 * \note May only be called by root.
1652 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1653 * argument in a drm_ctx structure.
1655 drm_public int drmDestroyContext(int fd, drm_context_t handle)
1660 ctx.handle = handle;
1661 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1666 drm_public int drmCreateDrawable(int fd, drm_drawable_t *handle)
1671 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1673 *handle = draw.handle;
1677 drm_public int drmDestroyDrawable(int fd, drm_drawable_t handle)
1682 draw.handle = handle;
1683 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1688 drm_public int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1689 drm_drawable_info_type_t type,
1690 unsigned int num, void *data)
1692 drm_update_draw_t update;
1695 update.handle = handle;
1698 update.data = (unsigned long long)(unsigned long)data;
1700 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1706 drm_public int drmCrtcGetSequence(int fd, uint32_t crtcId, uint64_t *sequence,
1709 struct drm_crtc_get_sequence get_seq;
1713 get_seq.crtc_id = crtcId;
1714 ret = drmIoctl(fd, DRM_IOCTL_CRTC_GET_SEQUENCE, &get_seq);
1719 *sequence = get_seq.sequence;
1721 *ns = get_seq.sequence_ns;
1725 drm_public int drmCrtcQueueSequence(int fd, uint32_t crtcId, uint32_t flags,
1727 uint64_t *sequence_queued,
1730 struct drm_crtc_queue_sequence queue_seq;
1733 memclear(queue_seq);
1734 queue_seq.crtc_id = crtcId;
1735 queue_seq.flags = flags;
1736 queue_seq.sequence = sequence;
1737 queue_seq.user_data = user_data;
1739 ret = drmIoctl(fd, DRM_IOCTL_CRTC_QUEUE_SEQUENCE, &queue_seq);
1740 if (ret == 0 && sequence_queued)
1741 *sequence_queued = queue_seq.sequence;
1747 * Acquire the AGP device.
1749 * Must be called before any of the other AGP related calls.
1751 * \param fd file descriptor.
1753 * \return zero on success, or a negative value on failure.
1756 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1758 drm_public int drmAgpAcquire(int fd)
1760 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1767 * Release the AGP device.
1769 * \param fd file descriptor.
1771 * \return zero on success, or a negative value on failure.
1774 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1776 drm_public int drmAgpRelease(int fd)
1778 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1787 * \param fd file descriptor.
1788 * \param mode AGP mode.
1790 * \return zero on success, or a negative value on failure.
1793 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1794 * argument in a drm_agp_mode structure.
1796 drm_public int drmAgpEnable(int fd, unsigned long mode)
1802 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1809 * Allocate a chunk of AGP memory.
1811 * \param fd file descriptor.
1812 * \param size requested memory size in bytes. Will be rounded to page boundary.
1813 * \param type type of memory to allocate.
1814 * \param address if not zero, will be set to the physical address of the
1816 * \param handle on success will be set to a handle of the allocated memory.
1818 * \return zero on success, or a negative value on failure.
1821 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1822 * arguments in a drm_agp_buffer structure.
1824 drm_public int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1825 unsigned long *address, drm_handle_t *handle)
1830 *handle = DRM_AGP_NO_HANDLE;
1833 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1836 *address = b.physical;
1843 * Free a chunk of AGP memory.
1845 * \param fd file descriptor.
1846 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1848 * \return zero on success, or a negative value on failure.
1851 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1852 * argument in a drm_agp_buffer structure.
1854 drm_public int drmAgpFree(int fd, drm_handle_t handle)
1860 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1867 * Bind a chunk of AGP memory.
1869 * \param fd file descriptor.
1870 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1871 * \param offset offset in bytes. It will round to page boundary.
1873 * \return zero on success, or a negative value on failure.
1876 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1877 * argument in a drm_agp_binding structure.
1879 drm_public int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1881 drm_agp_binding_t b;
1886 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1893 * Unbind a chunk of AGP memory.
1895 * \param fd file descriptor.
1896 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1898 * \return zero on success, or a negative value on failure.
1901 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1902 * the argument in a drm_agp_binding structure.
1904 drm_public int drmAgpUnbind(int fd, drm_handle_t handle)
1906 drm_agp_binding_t b;
1910 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1917 * Get AGP driver major version number.
1919 * \param fd file descriptor.
1921 * \return major version number on success, or a negative value on failure..
1924 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1925 * necessary information in a drm_agp_info structure.
1927 drm_public int drmAgpVersionMajor(int fd)
1933 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1935 return i.agp_version_major;
1940 * Get AGP driver minor version number.
1942 * \param fd file descriptor.
1944 * \return minor version number on success, or a negative value on failure.
1947 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1948 * necessary information in a drm_agp_info structure.
1950 drm_public int drmAgpVersionMinor(int fd)
1956 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1958 return i.agp_version_minor;
1965 * \param fd file descriptor.
1967 * \return mode on success, or zero on failure.
1970 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1971 * necessary information in a drm_agp_info structure.
1973 drm_public unsigned long drmAgpGetMode(int fd)
1979 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1986 * Get AGP aperture base.
1988 * \param fd file descriptor.
1990 * \return aperture base on success, zero on failure.
1993 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1994 * necessary information in a drm_agp_info structure.
1996 drm_public unsigned long drmAgpBase(int fd)
2002 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2004 return i.aperture_base;
2009 * Get AGP aperture size.
2011 * \param fd file descriptor.
2013 * \return aperture size on success, zero on failure.
2016 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2017 * necessary information in a drm_agp_info structure.
2019 drm_public unsigned long drmAgpSize(int fd)
2025 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2027 return i.aperture_size;
2032 * Get used AGP memory.
2034 * \param fd file descriptor.
2036 * \return memory used on success, or zero on failure.
2039 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2040 * necessary information in a drm_agp_info structure.
2042 drm_public unsigned long drmAgpMemoryUsed(int fd)
2048 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2050 return i.memory_used;
2055 * Get available AGP memory.
2057 * \param fd file descriptor.
2059 * \return memory available on success, or zero on failure.
2062 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2063 * necessary information in a drm_agp_info structure.
2065 drm_public unsigned long drmAgpMemoryAvail(int fd)
2071 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2073 return i.memory_allowed;
2078 * Get hardware vendor ID.
2080 * \param fd file descriptor.
2082 * \return vendor ID on success, or zero on failure.
2085 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2086 * necessary information in a drm_agp_info structure.
2088 drm_public unsigned int drmAgpVendorId(int fd)
2094 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2101 * Get hardware device ID.
2103 * \param fd file descriptor.
2105 * \return zero on success, or zero on failure.
2108 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2109 * necessary information in a drm_agp_info structure.
2111 drm_public unsigned int drmAgpDeviceId(int fd)
2117 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2122 drm_public int drmScatterGatherAlloc(int fd, unsigned long size,
2123 drm_handle_t *handle)
2125 drm_scatter_gather_t sg;
2131 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2133 *handle = sg.handle;
2137 drm_public int drmScatterGatherFree(int fd, drm_handle_t handle)
2139 drm_scatter_gather_t sg;
2143 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2151 * \param fd file descriptor.
2152 * \param vbl pointer to a drmVBlank structure.
2154 * \return zero on success, or a negative value on failure.
2157 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2159 drm_public int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2161 struct timespec timeout, cur;
2164 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2166 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2172 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2173 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2174 if (ret && errno == EINTR) {
2175 clock_gettime(CLOCK_MONOTONIC, &cur);
2176 /* Timeout after 1s */
2177 if (cur.tv_sec > timeout.tv_sec + 1 ||
2178 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2185 } while (ret && errno == EINTR);
2191 drm_public int drmError(int err, const char *label)
2194 case DRM_ERR_NO_DEVICE:
2195 fprintf(stderr, "%s: no device\n", label);
2197 case DRM_ERR_NO_ACCESS:
2198 fprintf(stderr, "%s: no access\n", label);
2200 case DRM_ERR_NOT_ROOT:
2201 fprintf(stderr, "%s: not root\n", label);
2203 case DRM_ERR_INVALID:
2204 fprintf(stderr, "%s: invalid args\n", label);
2209 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2217 * Install IRQ handler.
2219 * \param fd file descriptor.
2220 * \param irq IRQ number.
2222 * \return zero on success, or a negative value on failure.
2225 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2226 * argument in a drm_control structure.
2228 drm_public int drmCtlInstHandler(int fd, int irq)
2233 ctl.func = DRM_INST_HANDLER;
2235 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2242 * Uninstall IRQ handler.
2244 * \param fd file descriptor.
2246 * \return zero on success, or a negative value on failure.
2249 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2250 * argument in a drm_control structure.
2252 drm_public int drmCtlUninstHandler(int fd)
2257 ctl.func = DRM_UNINST_HANDLER;
2259 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2264 drm_public int drmFinish(int fd, int context, drmLockFlags flags)
2269 lock.context = context;
2270 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2271 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2272 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2273 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2274 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2275 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2276 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2282 * Get IRQ from bus ID.
2284 * \param fd file descriptor.
2285 * \param busnum bus number.
2286 * \param devnum device number.
2287 * \param funcnum function number.
2289 * \return IRQ number on success, or a negative value on failure.
2292 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2293 * arguments in a drm_irq_busid structure.
2295 drm_public int drmGetInterruptFromBusID(int fd, int busnum, int devnum,
2303 p.funcnum = funcnum;
2304 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2309 drm_public int drmAddContextTag(int fd, drm_context_t context, void *tag)
2311 drmHashEntry *entry = drmGetEntry(fd);
2313 if (drmHashInsert(entry->tagTable, context, tag)) {
2314 drmHashDelete(entry->tagTable, context);
2315 drmHashInsert(entry->tagTable, context, tag);
2320 drm_public int drmDelContextTag(int fd, drm_context_t context)
2322 drmHashEntry *entry = drmGetEntry(fd);
2324 return drmHashDelete(entry->tagTable, context);
2327 drm_public void *drmGetContextTag(int fd, drm_context_t context)
2329 drmHashEntry *entry = drmGetEntry(fd);
2332 if (drmHashLookup(entry->tagTable, context, &value))
2338 drm_public int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2339 drm_handle_t handle)
2341 drm_ctx_priv_map_t map;
2344 map.ctx_id = ctx_id;
2345 map.handle = (void *)(uintptr_t)handle;
2347 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2352 drm_public int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2353 drm_handle_t *handle)
2355 drm_ctx_priv_map_t map;
2358 map.ctx_id = ctx_id;
2360 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2363 *handle = (drm_handle_t)(uintptr_t)map.handle;
2368 drm_public int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2369 drmMapType *type, drmMapFlags *flags,
2370 drm_handle_t *handle, int *mtrr)
2376 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2378 *offset = map.offset;
2382 *handle = (unsigned long)map.handle;
2387 drm_public int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2388 unsigned long *magic, unsigned long *iocs)
2390 drm_client_t client;
2394 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2396 *auth = client.auth;
2399 *magic = client.magic;
2400 *iocs = client.iocs;
2404 drm_public int drmGetStats(int fd, drmStatsT *stats)
2410 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2414 memset(stats, 0, sizeof(*stats));
2415 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2419 stats->data[i].long_format = "%-20.20s"; \
2420 stats->data[i].rate_format = "%8.8s"; \
2421 stats->data[i].isvalue = 1; \
2422 stats->data[i].verbose = 0
2425 stats->data[i].long_format = "%-20.20s"; \
2426 stats->data[i].rate_format = "%5.5s"; \
2427 stats->data[i].isvalue = 0; \
2428 stats->data[i].mult_names = "kgm"; \
2429 stats->data[i].mult = 1000; \
2430 stats->data[i].verbose = 0
2433 stats->data[i].long_format = "%-20.20s"; \
2434 stats->data[i].rate_format = "%5.5s"; \
2435 stats->data[i].isvalue = 0; \
2436 stats->data[i].mult_names = "KGM"; \
2437 stats->data[i].mult = 1024; \
2438 stats->data[i].verbose = 0
2441 stats->count = s.count;
2442 for (i = 0; i < s.count; i++) {
2443 stats->data[i].value = s.data[i].value;
2444 switch (s.data[i].type) {
2445 case _DRM_STAT_LOCK:
2446 stats->data[i].long_name = "Lock";
2447 stats->data[i].rate_name = "Lock";
2450 case _DRM_STAT_OPENS:
2451 stats->data[i].long_name = "Opens";
2452 stats->data[i].rate_name = "O";
2454 stats->data[i].verbose = 1;
2456 case _DRM_STAT_CLOSES:
2457 stats->data[i].long_name = "Closes";
2458 stats->data[i].rate_name = "Lock";
2460 stats->data[i].verbose = 1;
2462 case _DRM_STAT_IOCTLS:
2463 stats->data[i].long_name = "Ioctls";
2464 stats->data[i].rate_name = "Ioc/s";
2467 case _DRM_STAT_LOCKS:
2468 stats->data[i].long_name = "Locks";
2469 stats->data[i].rate_name = "Lck/s";
2472 case _DRM_STAT_UNLOCKS:
2473 stats->data[i].long_name = "Unlocks";
2474 stats->data[i].rate_name = "Unl/s";
2478 stats->data[i].long_name = "IRQs";
2479 stats->data[i].rate_name = "IRQ/s";
2482 case _DRM_STAT_PRIMARY:
2483 stats->data[i].long_name = "Primary Bytes";
2484 stats->data[i].rate_name = "PB/s";
2487 case _DRM_STAT_SECONDARY:
2488 stats->data[i].long_name = "Secondary Bytes";
2489 stats->data[i].rate_name = "SB/s";
2493 stats->data[i].long_name = "DMA";
2494 stats->data[i].rate_name = "DMA/s";
2497 case _DRM_STAT_SPECIAL:
2498 stats->data[i].long_name = "Special DMA";
2499 stats->data[i].rate_name = "dma/s";
2502 case _DRM_STAT_MISSED:
2503 stats->data[i].long_name = "Miss";
2504 stats->data[i].rate_name = "Ms/s";
2507 case _DRM_STAT_VALUE:
2508 stats->data[i].long_name = "Value";
2509 stats->data[i].rate_name = "Value";
2512 case _DRM_STAT_BYTE:
2513 stats->data[i].long_name = "Bytes";
2514 stats->data[i].rate_name = "B/s";
2517 case _DRM_STAT_COUNT:
2519 stats->data[i].long_name = "Count";
2520 stats->data[i].rate_name = "Cnt/s";
2529 * Issue a set-version ioctl.
2531 * \param fd file descriptor.
2532 * \param drmCommandIndex command index
2533 * \param data source pointer of the data to be read and written.
2534 * \param size size of the data to be read and written.
2536 * \return zero on success, or a negative value on failure.
2539 * It issues a read-write ioctl given by
2540 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2542 drm_public int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2545 drm_set_version_t sv;
2548 sv.drm_di_major = version->drm_di_major;
2549 sv.drm_di_minor = version->drm_di_minor;
2550 sv.drm_dd_major = version->drm_dd_major;
2551 sv.drm_dd_minor = version->drm_dd_minor;
2553 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2557 version->drm_di_major = sv.drm_di_major;
2558 version->drm_di_minor = sv.drm_di_minor;
2559 version->drm_dd_major = sv.drm_dd_major;
2560 version->drm_dd_minor = sv.drm_dd_minor;
2566 * Send a device-specific command.
2568 * \param fd file descriptor.
2569 * \param drmCommandIndex command index
2571 * \return zero on success, or a negative value on failure.
2574 * It issues a ioctl given by
2575 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2577 drm_public int drmCommandNone(int fd, unsigned long drmCommandIndex)
2579 unsigned long request;
2581 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2583 if (drmIoctl(fd, request, NULL)) {
2591 * Send a device-specific read command.
2593 * \param fd file descriptor.
2594 * \param drmCommandIndex command index
2595 * \param data destination pointer of the data to be read.
2596 * \param size size of the data to be read.
2598 * \return zero on success, or a negative value on failure.
2601 * It issues a read ioctl given by
2602 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2604 drm_public int drmCommandRead(int fd, unsigned long drmCommandIndex,
2605 void *data, unsigned long size)
2607 unsigned long request;
2609 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2610 DRM_COMMAND_BASE + drmCommandIndex, size);
2612 if (drmIoctl(fd, request, data)) {
2620 * Send a device-specific write command.
2622 * \param fd file descriptor.
2623 * \param drmCommandIndex command index
2624 * \param data source pointer of the data to be written.
2625 * \param size size of the data to be written.
2627 * \return zero on success, or a negative value on failure.
2630 * It issues a write ioctl given by
2631 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2633 drm_public int drmCommandWrite(int fd, unsigned long drmCommandIndex,
2634 void *data, unsigned long size)
2636 unsigned long request;
2638 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2639 DRM_COMMAND_BASE + drmCommandIndex, size);
2641 if (drmIoctl(fd, request, data)) {
2649 * Send a device-specific read-write command.
2651 * \param fd file descriptor.
2652 * \param drmCommandIndex command index
2653 * \param data source pointer of the data to be read and written.
2654 * \param size size of the data to be read and written.
2656 * \return zero on success, or a negative value on failure.
2659 * It issues a read-write ioctl given by
2660 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2662 drm_public int drmCommandWriteRead(int fd, unsigned long drmCommandIndex,
2663 void *data, unsigned long size)
2665 unsigned long request;
2667 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2668 DRM_COMMAND_BASE + drmCommandIndex, size);
2670 if (drmIoctl(fd, request, data))
2675 #define DRM_MAX_FDS 16
2681 } connection[DRM_MAX_FDS];
2683 static int nr_fds = 0;
2685 drm_public int drmOpenOnce(void *unused, const char *BusID, int *newlyopened)
2687 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2690 drm_public int drmOpenOnceWithType(const char *BusID, int *newlyopened,
2696 for (i = 0; i < nr_fds; i++)
2697 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2698 (connection[i].type == type)) {
2699 connection[i].refcount++;
2701 return connection[i].fd;
2704 fd = drmOpenWithType(NULL, BusID, type);
2705 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2708 connection[nr_fds].BusID = strdup(BusID);
2709 connection[nr_fds].fd = fd;
2710 connection[nr_fds].refcount = 1;
2711 connection[nr_fds].type = type;
2715 fprintf(stderr, "saved connection %d for %s %d\n",
2716 nr_fds, connection[nr_fds].BusID,
2717 strcmp(BusID, connection[nr_fds].BusID));
2724 drm_public void drmCloseOnce(int fd)
2728 for (i = 0; i < nr_fds; i++) {
2729 if (fd == connection[i].fd) {
2730 if (--connection[i].refcount == 0) {
2731 drmClose(connection[i].fd);
2732 free(connection[i].BusID);
2735 connection[i] = connection[nr_fds];
2743 drm_public int drmSetMaster(int fd)
2745 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2748 drm_public int drmDropMaster(int fd)
2750 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2753 drm_public int drmIsMaster(int fd)
2755 /* Detect master by attempting something that requires master.
2757 * Authenticating magic tokens requires master and 0 is an
2758 * internal kernel detail which we could use. Attempting this on
2759 * a master fd would fail therefore fail with EINVAL because 0
2762 * A non-master fd will fail with EACCES, as the kernel checks
2763 * for master before attempting to do anything else.
2765 * Since we don't want to leak implementation details, use
2768 return drmAuthMagic(fd, 0) != -EACCES;
2771 drm_public char *drmGetDeviceNameFromFd(int fd)
2775 char dname[SPECNAMELEN];
2776 char name[SPECNAMELEN];
2779 if (fstat(fd, &sbuf))
2782 maj = major(sbuf.st_rdev);
2783 min = minor(sbuf.st_rdev);
2785 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2788 if (!devname_r(sbuf.st_rdev, S_IFCHR, dname, sizeof(dname)))
2791 snprintf(name, sizeof(name), "/dev/%s", dname);
2792 return strdup(name);
2799 /* The whole drmOpen thing is a fiasco and we need to find a way
2800 * back to just using open(2). For now, however, lets just make
2801 * things worse with even more ad hoc directory walking code to
2802 * discover the device file name. */
2807 for (i = 0; i < DRM_MAX_MINOR; i++) {
2808 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2809 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2812 if (i == DRM_MAX_MINOR)
2815 return strdup(name);
2819 static bool drmNodeIsDRM(int maj, int min)
2825 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device/drm",
2827 return stat(path, &sbuf) == 0;
2829 char name[SPECNAMELEN];
2831 if (!devname_r(makedev(maj, min), S_IFCHR, name, sizeof(name)))
2833 /* Handle drm/ and dri/ as both are present in different FreeBSD version
2834 * FreeBSD on amd64/i386/powerpc external kernel modules create node in
2835 * in /dev/drm/ and links in /dev/dri while a WIP in kernel driver creates
2836 * only device nodes in /dev/dri/ */
2837 return (!strncmp(name, "drm/", 4) || !strncmp(name, "dri/", 4));
2839 return maj == DRM_MAJOR;
2843 drm_public int drmGetNodeTypeFromFd(int fd)
2848 if (fstat(fd, &sbuf))
2851 maj = major(sbuf.st_rdev);
2852 min = minor(sbuf.st_rdev);
2854 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode)) {
2859 type = drmGetMinorType(maj, min);
2865 drm_public int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags,
2868 struct drm_prime_handle args;
2873 args.handle = handle;
2875 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2879 *prime_fd = args.fd;
2883 drm_public int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2885 struct drm_prime_handle args;
2890 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2894 *handle = args.handle;
2898 static char *drmGetMinorNameForFD(int fd, int type)
2904 const char *name = drmGetMinorName(type);
2906 char dev_name[64], buf[64];
2914 if (fstat(fd, &sbuf))
2917 maj = major(sbuf.st_rdev);
2918 min = minor(sbuf.st_rdev);
2920 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2923 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2925 sysdir = opendir(buf);
2929 while ((ent = readdir(sysdir))) {
2930 if (strncmp(ent->d_name, name, len) == 0) {
2931 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2935 return strdup(dev_name);
2943 char buf[PATH_MAX + 1];
2944 const char *dev_name = drmGetDeviceName(type);
2945 unsigned int maj, min;
2948 if (fstat(fd, &sbuf))
2951 maj = major(sbuf.st_rdev);
2952 min = minor(sbuf.st_rdev);
2954 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
2960 n = snprintf(buf, sizeof(buf), dev_name, DRM_DIR_NAME, min);
2961 if (n == -1 || n >= sizeof(buf))
2968 drm_public char *drmGetPrimaryDeviceNameFromFd(int fd)
2970 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2973 drm_public char *drmGetRenderDeviceNameFromFd(int fd)
2975 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2979 static char * DRM_PRINTFLIKE(2, 3)
2980 sysfs_uevent_get(const char *path, const char *fmt, ...)
2982 char filename[PATH_MAX + 1], *key, *line = NULL, *value = NULL;
2983 size_t size = 0, len;
2989 num = vasprintf(&key, fmt, ap);
2993 snprintf(filename, sizeof(filename), "%s/uevent", path);
2995 fp = fopen(filename, "r");
3001 while ((num = getline(&line, &size, fp)) >= 0) {
3002 if ((strncmp(line, key, len) == 0) && (line[len] == '=')) {
3003 char *start = line + len + 1, *end = line + num - 1;
3008 value = strndup(start, end - start);
3022 /* Little white lie to avoid major rework of the existing code */
3023 #define DRM_BUS_VIRTIO 0x10
3026 static int get_subsystem_type(const char *device_path)
3028 char path[PATH_MAX + 1] = "";
3029 char link[PATH_MAX + 1] = "";
3035 { "/pci", DRM_BUS_PCI },
3036 { "/usb", DRM_BUS_USB },
3037 { "/platform", DRM_BUS_PLATFORM },
3038 { "/spi", DRM_BUS_PLATFORM },
3039 { "/host1x", DRM_BUS_HOST1X },
3040 { "/virtio", DRM_BUS_VIRTIO },
3043 strncpy(path, device_path, PATH_MAX);
3044 strncat(path, "/subsystem", PATH_MAX);
3046 if (readlink(path, link, PATH_MAX) < 0)
3049 name = strrchr(link, '/');
3053 for (unsigned i = 0; i < ARRAY_SIZE(bus_types); i++) {
3054 if (strncmp(name, bus_types[i].name, strlen(bus_types[i].name)) == 0)
3055 return bus_types[i].bus_type;
3062 static int drmParseSubsystemType(int maj, int min)
3065 char path[PATH_MAX + 1] = "";
3066 char real_path[PATH_MAX + 1] = "";
3069 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3070 if (!realpath(path, real_path))
3072 snprintf(path, sizeof(path), "%s", real_path);
3074 subsystem_type = get_subsystem_type(path);
3075 if (subsystem_type == DRM_BUS_VIRTIO) {
3076 strncat(path, "/..", PATH_MAX);
3077 subsystem_type = get_subsystem_type(path);
3079 return subsystem_type;
3080 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3083 #warning "Missing implementation of drmParseSubsystemType"
3089 get_pci_path(int maj, int min, char *pci_path)
3091 char path[PATH_MAX + 1], *term;
3093 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3094 if (!realpath(path, pci_path)) {
3095 strcpy(pci_path, path);
3099 term = strrchr(pci_path, '/');
3100 if (term && strncmp(term, "/virtio", 7) == 0)
3104 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
3107 unsigned int domain, bus, dev, func;
3108 char pci_path[PATH_MAX + 1], *value;
3111 get_pci_path(maj, min, pci_path);
3113 value = sysfs_uevent_get(pci_path, "PCI_SLOT_NAME");
3117 num = sscanf(value, "%04x:%02x:%02x.%1u", &domain, &bus, &dev, &func);
3123 info->domain = domain;
3129 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3130 struct drm_pciinfo pinfo;
3133 type = drmGetMinorType(maj, min);
3137 fd = drmOpenMinor(min, 0, type);
3141 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3147 info->domain = pinfo.domain;
3148 info->bus = pinfo.bus;
3149 info->dev = pinfo.dev;
3150 info->func = pinfo.func;
3154 #warning "Missing implementation of drmParsePciBusInfo"
3159 drm_public int drmDevicesEqual(drmDevicePtr a, drmDevicePtr b)
3161 if (a == NULL || b == NULL)
3164 if (a->bustype != b->bustype)
3167 switch (a->bustype) {
3169 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo)) == 0;
3172 return memcmp(a->businfo.usb, b->businfo.usb, sizeof(drmUsbBusInfo)) == 0;
3174 case DRM_BUS_PLATFORM:
3175 return memcmp(a->businfo.platform, b->businfo.platform, sizeof(drmPlatformBusInfo)) == 0;
3177 case DRM_BUS_HOST1X:
3178 return memcmp(a->businfo.host1x, b->businfo.host1x, sizeof(drmHost1xBusInfo)) == 0;
3187 static int drmGetNodeType(const char *name)
3189 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
3190 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
3191 return DRM_NODE_CONTROL;
3193 if (strncmp(name, DRM_RENDER_MINOR_NAME,
3194 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
3195 return DRM_NODE_RENDER;
3197 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
3198 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
3199 return DRM_NODE_PRIMARY;
3204 static int drmGetMaxNodeName(void)
3206 return sizeof(DRM_DIR_NAME) +
3207 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
3208 sizeof(DRM_CONTROL_MINOR_NAME),
3209 sizeof(DRM_RENDER_MINOR_NAME)) +
3210 3 /* length of the node number */;
3214 static int parse_separate_sysfs_files(int maj, int min,
3215 drmPciDeviceInfoPtr device,
3216 bool ignore_revision)
3218 static const char *attrs[] = {
3219 "revision", /* Older kernels are missing the file, so check for it first */
3225 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3226 unsigned int data[ARRAY_SIZE(attrs)];
3230 get_pci_path(maj, min, pci_path);
3232 for (unsigned i = ignore_revision ? 1 : 0; i < ARRAY_SIZE(attrs); i++) {
3233 snprintf(path, PATH_MAX, "%s/%s", pci_path, attrs[i]);
3234 fp = fopen(path, "r");
3238 ret = fscanf(fp, "%x", &data[i]);
3245 device->revision_id = ignore_revision ? 0xff : data[0] & 0xff;
3246 device->vendor_id = data[1] & 0xffff;
3247 device->device_id = data[2] & 0xffff;
3248 device->subvendor_id = data[3] & 0xffff;
3249 device->subdevice_id = data[4] & 0xffff;
3254 static int parse_config_sysfs_file(int maj, int min,
3255 drmPciDeviceInfoPtr device)
3257 char path[PATH_MAX + 1], pci_path[PATH_MAX + 1];
3258 unsigned char config[64];
3261 get_pci_path(maj, min, pci_path);
3263 snprintf(path, PATH_MAX, "%s/config", pci_path);
3264 fd = open(path, O_RDONLY);
3268 ret = read(fd, config, sizeof(config));
3273 device->vendor_id = config[0] | (config[1] << 8);
3274 device->device_id = config[2] | (config[3] << 8);
3275 device->revision_id = config[8];
3276 device->subvendor_id = config[44] | (config[45] << 8);
3277 device->subdevice_id = config[46] | (config[47] << 8);
3283 static int drmParsePciDeviceInfo(int maj, int min,
3284 drmPciDeviceInfoPtr device,
3288 if (!(flags & DRM_DEVICE_GET_PCI_REVISION))
3289 return parse_separate_sysfs_files(maj, min, device, true);
3291 if (parse_separate_sysfs_files(maj, min, device, false))
3292 return parse_config_sysfs_file(maj, min, device);
3295 #elif defined(__OpenBSD__) || defined(__DragonFly__)
3296 struct drm_pciinfo pinfo;
3299 type = drmGetMinorType(maj, min);
3303 fd = drmOpenMinor(min, 0, type);
3307 if (drmIoctl(fd, DRM_IOCTL_GET_PCIINFO, &pinfo)) {
3313 device->vendor_id = pinfo.vendor_id;
3314 device->device_id = pinfo.device_id;
3315 device->revision_id = pinfo.revision_id;
3316 device->subvendor_id = pinfo.subvendor_id;
3317 device->subdevice_id = pinfo.subdevice_id;
3321 #warning "Missing implementation of drmParsePciDeviceInfo"
3326 static void drmFreePlatformDevice(drmDevicePtr device)
3328 if (device->deviceinfo.platform) {
3329 if (device->deviceinfo.platform->compatible) {
3330 char **compatible = device->deviceinfo.platform->compatible;
3332 while (*compatible) {
3337 free(device->deviceinfo.platform->compatible);
3342 static void drmFreeHost1xDevice(drmDevicePtr device)
3344 if (device->deviceinfo.host1x) {
3345 if (device->deviceinfo.host1x->compatible) {
3346 char **compatible = device->deviceinfo.host1x->compatible;
3348 while (*compatible) {
3353 free(device->deviceinfo.host1x->compatible);
3358 drm_public void drmFreeDevice(drmDevicePtr *device)
3364 switch ((*device)->bustype) {
3365 case DRM_BUS_PLATFORM:
3366 drmFreePlatformDevice(*device);
3369 case DRM_BUS_HOST1X:
3370 drmFreeHost1xDevice(*device);
3379 drm_public void drmFreeDevices(drmDevicePtr devices[], int count)
3383 if (devices == NULL)
3386 for (i = 0; i < count; i++)
3388 drmFreeDevice(&devices[i]);
3391 static drmDevicePtr drmDeviceAlloc(unsigned int type, const char *node,
3392 size_t bus_size, size_t device_size,
3395 size_t max_node_length, extra, size;
3396 drmDevicePtr device;
3400 max_node_length = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3401 extra = DRM_NODE_MAX * (sizeof(void *) + max_node_length);
3403 size = sizeof(*device) + extra + bus_size + device_size;
3405 device = calloc(1, size);
3409 device->available_nodes = 1 << type;
3411 ptr = (char *)device + sizeof(*device);
3412 device->nodes = (char **)ptr;
3414 ptr += DRM_NODE_MAX * sizeof(void *);
3416 for (i = 0; i < DRM_NODE_MAX; i++) {
3417 device->nodes[i] = ptr;
3418 ptr += max_node_length;
3421 memcpy(device->nodes[type], node, max_node_length);
3428 static int drmProcessPciDevice(drmDevicePtr *device,
3429 const char *node, int node_type,
3430 int maj, int min, bool fetch_deviceinfo,
3437 dev = drmDeviceAlloc(node_type, node, sizeof(drmPciBusInfo),
3438 sizeof(drmPciDeviceInfo), &addr);
3442 dev->bustype = DRM_BUS_PCI;
3444 dev->businfo.pci = (drmPciBusInfoPtr)addr;
3446 ret = drmParsePciBusInfo(maj, min, dev->businfo.pci);
3450 // Fetch the device info if the user has requested it
3451 if (fetch_deviceinfo) {
3452 addr += sizeof(drmPciBusInfo);
3453 dev->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3455 ret = drmParsePciDeviceInfo(maj, min, dev->deviceinfo.pci, flags);
3469 static int drmParseUsbBusInfo(int maj, int min, drmUsbBusInfoPtr info)
3472 char path[PATH_MAX + 1], *value;
3473 unsigned int bus, dev;
3476 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3478 value = sysfs_uevent_get(path, "BUSNUM");
3482 ret = sscanf(value, "%03u", &bus);
3488 value = sysfs_uevent_get(path, "DEVNUM");
3492 ret = sscanf(value, "%03u", &dev);
3503 #warning "Missing implementation of drmParseUsbBusInfo"
3508 static int drmParseUsbDeviceInfo(int maj, int min, drmUsbDeviceInfoPtr info)
3511 char path[PATH_MAX + 1], *value;
3512 unsigned int vendor, product;
3515 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3517 value = sysfs_uevent_get(path, "PRODUCT");
3521 ret = sscanf(value, "%x/%x", &vendor, &product);
3527 info->vendor = vendor;
3528 info->product = product;
3532 #warning "Missing implementation of drmParseUsbDeviceInfo"
3537 static int drmProcessUsbDevice(drmDevicePtr *device, const char *node,
3538 int node_type, int maj, int min,
3539 bool fetch_deviceinfo, uint32_t flags)
3545 dev = drmDeviceAlloc(node_type, node, sizeof(drmUsbBusInfo),
3546 sizeof(drmUsbDeviceInfo), &ptr);
3550 dev->bustype = DRM_BUS_USB;
3552 dev->businfo.usb = (drmUsbBusInfoPtr)ptr;
3554 ret = drmParseUsbBusInfo(maj, min, dev->businfo.usb);
3558 if (fetch_deviceinfo) {
3559 ptr += sizeof(drmUsbBusInfo);
3560 dev->deviceinfo.usb = (drmUsbDeviceInfoPtr)ptr;
3562 ret = drmParseUsbDeviceInfo(maj, min, dev->deviceinfo.usb);
3576 static int drmParseOFBusInfo(int maj, int min, char *fullname)
3579 char path[PATH_MAX + 1], *name, *tmp_name;
3581 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3583 name = sysfs_uevent_get(path, "OF_FULLNAME");
3586 /* If the device lacks OF data, pick the MODALIAS info */
3587 name = sysfs_uevent_get(path, "MODALIAS");
3591 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3592 tmp_name = strrchr(name, ':');
3600 strncpy(fullname, tmp_name, DRM_PLATFORM_DEVICE_NAME_LEN);
3601 fullname[DRM_PLATFORM_DEVICE_NAME_LEN - 1] = '\0';
3606 #warning "Missing implementation of drmParseOFBusInfo"
3611 static int drmParseOFDeviceInfo(int maj, int min, char ***compatible)
3614 char path[PATH_MAX + 1], *value, *tmp_name;
3615 unsigned int count, i;
3618 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d/device", maj, min);
3620 value = sysfs_uevent_get(path, "OF_COMPATIBLE_N");
3622 sscanf(value, "%u", &count);
3625 /* Assume one entry if the device lack OF data */
3629 *compatible = calloc(count + 1, sizeof(char *));
3633 for (i = 0; i < count; i++) {
3634 value = sysfs_uevent_get(path, "OF_COMPATIBLE_%u", i);
3637 /* If the device lacks OF data, pick the MODALIAS info */
3638 value = sysfs_uevent_get(path, "MODALIAS");
3644 /* .. and strip the MODALIAS=[platform,usb...]: part. */
3645 tmp_name = strrchr(value, ':');
3650 tmp_name = strdup(tmp_name + 1);
3654 (*compatible)[i] = tmp_name;
3661 free((*compatible)[i]);
3666 #warning "Missing implementation of drmParseOFDeviceInfo"
3671 static int drmProcessPlatformDevice(drmDevicePtr *device,
3672 const char *node, int node_type,
3673 int maj, int min, bool fetch_deviceinfo,
3680 dev = drmDeviceAlloc(node_type, node, sizeof(drmPlatformBusInfo),
3681 sizeof(drmPlatformDeviceInfo), &ptr);
3685 dev->bustype = DRM_BUS_PLATFORM;
3687 dev->businfo.platform = (drmPlatformBusInfoPtr)ptr;
3689 ret = drmParseOFBusInfo(maj, min, dev->businfo.platform->fullname);
3693 if (fetch_deviceinfo) {
3694 ptr += sizeof(drmPlatformBusInfo);
3695 dev->deviceinfo.platform = (drmPlatformDeviceInfoPtr)ptr;
3697 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.platform->compatible);
3711 static int drmProcessHost1xDevice(drmDevicePtr *device,
3712 const char *node, int node_type,
3713 int maj, int min, bool fetch_deviceinfo,
3720 dev = drmDeviceAlloc(node_type, node, sizeof(drmHost1xBusInfo),
3721 sizeof(drmHost1xDeviceInfo), &ptr);
3725 dev->bustype = DRM_BUS_HOST1X;
3727 dev->businfo.host1x = (drmHost1xBusInfoPtr)ptr;
3729 ret = drmParseOFBusInfo(maj, min, dev->businfo.host1x->fullname);
3733 if (fetch_deviceinfo) {
3734 ptr += sizeof(drmHost1xBusInfo);
3735 dev->deviceinfo.host1x = (drmHost1xDeviceInfoPtr)ptr;
3737 ret = drmParseOFDeviceInfo(maj, min, &dev->deviceinfo.host1x->compatible);
3752 process_device(drmDevicePtr *device, const char *d_name,
3753 int req_subsystem_type,
3754 bool fetch_deviceinfo, uint32_t flags)
3757 char node[PATH_MAX + 1];
3758 int node_type, subsystem_type;
3759 unsigned int maj, min;
3761 node_type = drmGetNodeType(d_name);
3765 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, d_name);
3766 if (stat(node, &sbuf))
3769 maj = major(sbuf.st_rdev);
3770 min = minor(sbuf.st_rdev);
3772 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3775 subsystem_type = drmParseSubsystemType(maj, min);
3776 if (req_subsystem_type != -1 && req_subsystem_type != subsystem_type)
3779 switch (subsystem_type) {
3781 return drmProcessPciDevice(device, node, node_type, maj, min,
3782 fetch_deviceinfo, flags);
3784 return drmProcessUsbDevice(device, node, node_type, maj, min,
3785 fetch_deviceinfo, flags);
3786 case DRM_BUS_PLATFORM:
3787 return drmProcessPlatformDevice(device, node, node_type, maj, min,
3788 fetch_deviceinfo, flags);
3789 case DRM_BUS_HOST1X:
3790 return drmProcessHost1xDevice(device, node, node_type, maj, min,
3791 fetch_deviceinfo, flags);
3797 /* Consider devices located on the same bus as duplicate and fold the respective
3798 * entries into a single one.
3800 * Note: this leaves "gaps" in the array, while preserving the length.
3802 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3804 int node_type, i, j;
3806 for (i = 0; i < count; i++) {
3807 for (j = i + 1; j < count; j++) {
3808 if (drmDevicesEqual(local_devices[i], local_devices[j])) {
3809 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3810 node_type = log2(local_devices[j]->available_nodes);
3811 memcpy(local_devices[i]->nodes[node_type],
3812 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3813 drmFreeDevice(&local_devices[j]);
3819 /* Check that the given flags are valid returning 0 on success */
3821 drm_device_validate_flags(uint32_t flags)
3823 return (flags & ~DRM_DEVICE_GET_PCI_REVISION);
3827 drm_device_has_rdev(drmDevicePtr device, dev_t find_rdev)
3831 for (int i = 0; i < DRM_NODE_MAX; i++) {
3832 if (device->available_nodes & 1 << i) {
3833 if (stat(device->nodes[i], &sbuf) == 0 &&
3834 sbuf.st_rdev == find_rdev)
3842 * The kernel drm core has a number of places that assume maximum of
3843 * 3x64 devices nodes. That's 64 for each of primary, control and
3844 * render nodes. Rounded it up to 256 for simplicity.
3846 #define MAX_DRM_NODES 256
3849 * Get information about the opened drm device
3851 * \param fd file descriptor of the drm device
3852 * \param flags feature/behaviour bitmask
3853 * \param device the address of a drmDevicePtr where the information
3854 * will be allocated in stored
3856 * \return zero on success, negative error code otherwise.
3858 * \note Unlike drmGetDevice it does not retrieve the pci device revision field
3859 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
3861 drm_public int drmGetDevice2(int fd, uint32_t flags, drmDevicePtr *device)
3865 * DRI device nodes on OpenBSD are not in their own directory, they reside
3866 * in /dev along with a large number of statically generated /dev nodes.
3867 * Avoid stat'ing all of /dev needlessly by implementing this custom path.
3871 char node[PATH_MAX + 1];
3872 const char *dev_name;
3873 int node_type, subsystem_type;
3874 int maj, min, n, ret;
3876 if (fd == -1 || device == NULL)
3879 if (fstat(fd, &sbuf))
3882 maj = major(sbuf.st_rdev);
3883 min = minor(sbuf.st_rdev);
3885 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3888 node_type = drmGetMinorType(maj, min);
3889 if (node_type == -1)
3892 dev_name = drmGetDeviceName(node_type);
3896 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
3897 if (n == -1 || n >= PATH_MAX)
3899 if (stat(node, &sbuf))
3902 subsystem_type = drmParseSubsystemType(maj, min);
3903 if (subsystem_type != DRM_BUS_PCI)
3906 ret = drmProcessPciDevice(&d, node, node_type, maj, min, true, flags);
3914 drmDevicePtr local_devices[MAX_DRM_NODES];
3917 struct dirent *dent;
3921 int ret, i, node_count;
3924 if (drm_device_validate_flags(flags))
3927 if (fd == -1 || device == NULL)
3930 if (fstat(fd, &sbuf))
3933 find_rdev = sbuf.st_rdev;
3934 maj = major(sbuf.st_rdev);
3935 min = minor(sbuf.st_rdev);
3937 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
3940 subsystem_type = drmParseSubsystemType(maj, min);
3941 if (subsystem_type < 0)
3942 return subsystem_type;
3944 sysdir = opendir(DRM_DIR_NAME);
3949 while ((dent = readdir(sysdir))) {
3950 ret = process_device(&d, dent->d_name, subsystem_type, true, flags);
3954 if (i >= MAX_DRM_NODES) {
3955 fprintf(stderr, "More than %d drm nodes detected. "
3956 "Please report a bug - that should not happen.\n"
3957 "Skipping extra nodes\n", MAX_DRM_NODES);
3960 local_devices[i] = d;
3965 drmFoldDuplicatedDevices(local_devices, node_count);
3969 for (i = 0; i < node_count; i++) {
3970 if (!local_devices[i])
3973 if (drm_device_has_rdev(local_devices[i], find_rdev))
3974 *device = local_devices[i];
3976 drmFreeDevice(&local_devices[i]);
3980 if (*device == NULL)
3987 * Get information about the opened drm device
3989 * \param fd file descriptor of the drm device
3990 * \param device the address of a drmDevicePtr where the information
3991 * will be allocated in stored
3993 * \return zero on success, negative error code otherwise.
3995 drm_public int drmGetDevice(int fd, drmDevicePtr *device)
3997 return drmGetDevice2(fd, DRM_DEVICE_GET_PCI_REVISION, device);
4001 * Get drm devices on the system
4003 * \param flags feature/behaviour bitmask
4004 * \param devices the array of devices with drmDevicePtr elements
4005 * can be NULL to get the device number first
4006 * \param max_devices the maximum number of devices for the array
4008 * \return on error - negative error code,
4009 * if devices is NULL - total number of devices available on the system,
4010 * alternatively the number of devices stored in devices[], which is
4011 * capped by the max_devices.
4013 * \note Unlike drmGetDevices it does not retrieve the pci device revision field
4014 * unless the DRM_DEVICE_GET_PCI_REVISION \p flag is set.
4016 drm_public int drmGetDevices2(uint32_t flags, drmDevicePtr devices[],
4019 drmDevicePtr local_devices[MAX_DRM_NODES];
4020 drmDevicePtr device;
4022 struct dirent *dent;
4023 int ret, i, node_count, device_count;
4025 if (drm_device_validate_flags(flags))
4028 sysdir = opendir(DRM_DIR_NAME);
4033 while ((dent = readdir(sysdir))) {
4034 ret = process_device(&device, dent->d_name, -1, devices != NULL, flags);
4038 if (i >= MAX_DRM_NODES) {
4039 fprintf(stderr, "More than %d drm nodes detected. "
4040 "Please report a bug - that should not happen.\n"
4041 "Skipping extra nodes\n", MAX_DRM_NODES);
4044 local_devices[i] = device;
4049 drmFoldDuplicatedDevices(local_devices, node_count);
4052 for (i = 0; i < node_count; i++) {
4053 if (!local_devices[i])
4056 if ((devices != NULL) && (device_count < max_devices))
4057 devices[device_count] = local_devices[i];
4059 drmFreeDevice(&local_devices[i]);
4065 return device_count;
4069 * Get drm devices on the system
4071 * \param devices the array of devices with drmDevicePtr elements
4072 * can be NULL to get the device number first
4073 * \param max_devices the maximum number of devices for the array
4075 * \return on error - negative error code,
4076 * if devices is NULL - total number of devices available on the system,
4077 * alternatively the number of devices stored in devices[], which is
4078 * capped by the max_devices.
4080 drm_public int drmGetDevices(drmDevicePtr devices[], int max_devices)
4082 return drmGetDevices2(DRM_DEVICE_GET_PCI_REVISION, devices, max_devices);
4085 drm_public char *drmGetDeviceNameFromFd2(int fd)
4089 char path[PATH_MAX + 1], *value;
4090 unsigned int maj, min;
4092 if (fstat(fd, &sbuf))
4095 maj = major(sbuf.st_rdev);
4096 min = minor(sbuf.st_rdev);
4098 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4101 snprintf(path, sizeof(path), "/sys/dev/char/%d:%d", maj, min);
4103 value = sysfs_uevent_get(path, "DEVNAME");
4107 snprintf(path, sizeof(path), "/dev/%s", value);
4110 return strdup(path);
4113 char node[PATH_MAX + 1];
4114 const char *dev_name;
4118 if (fstat(fd, &sbuf))
4121 maj = major(sbuf.st_rdev);
4122 min = minor(sbuf.st_rdev);
4124 if (!drmNodeIsDRM(maj, min) || !S_ISCHR(sbuf.st_mode))
4127 node_type = drmGetMinorType(maj, min);
4128 if (node_type == -1)
4131 dev_name = drmGetDeviceName(node_type);
4135 n = snprintf(node, PATH_MAX, dev_name, DRM_DIR_NAME, min);
4136 if (n == -1 || n >= PATH_MAX)
4139 return strdup(node);
4143 drm_public int drmSyncobjCreate(int fd, uint32_t flags, uint32_t *handle)
4145 struct drm_syncobj_create args;
4151 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_CREATE, &args);
4154 *handle = args.handle;
4158 drm_public int drmSyncobjDestroy(int fd, uint32_t handle)
4160 struct drm_syncobj_destroy args;
4163 args.handle = handle;
4164 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_DESTROY, &args);
4167 drm_public int drmSyncobjHandleToFD(int fd, uint32_t handle, int *obj_fd)
4169 struct drm_syncobj_handle args;
4174 args.handle = handle;
4175 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4182 drm_public int drmSyncobjFDToHandle(int fd, int obj_fd, uint32_t *handle)
4184 struct drm_syncobj_handle args;
4190 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4193 *handle = args.handle;
4197 drm_public int drmSyncobjImportSyncFile(int fd, uint32_t handle,
4200 struct drm_syncobj_handle args;
4203 args.fd = sync_file_fd;
4204 args.handle = handle;
4205 args.flags = DRM_SYNCOBJ_FD_TO_HANDLE_FLAGS_IMPORT_SYNC_FILE;
4206 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_FD_TO_HANDLE, &args);
4209 drm_public int drmSyncobjExportSyncFile(int fd, uint32_t handle,
4212 struct drm_syncobj_handle args;
4217 args.handle = handle;
4218 args.flags = DRM_SYNCOBJ_HANDLE_TO_FD_FLAGS_EXPORT_SYNC_FILE;
4219 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_HANDLE_TO_FD, &args);
4222 *sync_file_fd = args.fd;
4226 drm_public int drmSyncobjWait(int fd, uint32_t *handles, unsigned num_handles,
4227 int64_t timeout_nsec, unsigned flags,
4228 uint32_t *first_signaled)
4230 struct drm_syncobj_wait args;
4234 args.handles = (uintptr_t)handles;
4235 args.timeout_nsec = timeout_nsec;
4236 args.count_handles = num_handles;
4239 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_WAIT, &args);
4244 *first_signaled = args.first_signaled;
4248 drm_public int drmSyncobjReset(int fd, const uint32_t *handles,
4249 uint32_t handle_count)
4251 struct drm_syncobj_array args;
4255 args.handles = (uintptr_t)handles;
4256 args.count_handles = handle_count;
4258 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_RESET, &args);
4262 drm_public int drmSyncobjSignal(int fd, const uint32_t *handles,
4263 uint32_t handle_count)
4265 struct drm_syncobj_array args;
4269 args.handles = (uintptr_t)handles;
4270 args.count_handles = handle_count;
4272 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_SIGNAL, &args);
4276 drm_public int drmSyncobjTimelineSignal(int fd, const uint32_t *handles,
4277 uint64_t *points, uint32_t handle_count)
4279 struct drm_syncobj_timeline_array args;
4283 args.handles = (uintptr_t)handles;
4284 args.points = (uintptr_t)points;
4285 args.count_handles = handle_count;
4287 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_SIGNAL, &args);
4291 drm_public int drmSyncobjTimelineWait(int fd, uint32_t *handles, uint64_t *points,
4292 unsigned num_handles,
4293 int64_t timeout_nsec, unsigned flags,
4294 uint32_t *first_signaled)
4296 struct drm_syncobj_timeline_wait args;
4300 args.handles = (uintptr_t)handles;
4301 args.points = (uintptr_t)points;
4302 args.timeout_nsec = timeout_nsec;
4303 args.count_handles = num_handles;
4306 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TIMELINE_WAIT, &args);
4311 *first_signaled = args.first_signaled;
4316 drm_public int drmSyncobjQuery(int fd, uint32_t *handles, uint64_t *points,
4317 uint32_t handle_count)
4319 struct drm_syncobj_timeline_array args;
4323 args.handles = (uintptr_t)handles;
4324 args.points = (uintptr_t)points;
4325 args.count_handles = handle_count;
4327 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4333 drm_public int drmSyncobjQuery2(int fd, uint32_t *handles, uint64_t *points,
4334 uint32_t handle_count, uint32_t flags)
4336 struct drm_syncobj_timeline_array args;
4339 args.handles = (uintptr_t)handles;
4340 args.points = (uintptr_t)points;
4341 args.count_handles = handle_count;
4344 return drmIoctl(fd, DRM_IOCTL_SYNCOBJ_QUERY, &args);
4348 drm_public int drmSyncobjTransfer(int fd,
4349 uint32_t dst_handle, uint64_t dst_point,
4350 uint32_t src_handle, uint64_t src_point,
4353 struct drm_syncobj_transfer args;
4357 args.src_handle = src_handle;
4358 args.dst_handle = dst_handle;
4359 args.src_point = src_point;
4360 args.dst_point = dst_point;
4363 ret = drmIoctl(fd, DRM_IOCTL_SYNCOBJ_TRANSFER, &args);