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.
50 #include <sys/types.h>
52 #define stat_t struct stat
53 #include <sys/ioctl.h>
56 #ifdef HAVE_SYS_MKDEV_H
57 # include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
61 /* Not all systems have MAP_FAILED defined */
63 #define MAP_FAILED ((void *)-1)
67 #include "libdrm_macros.h"
69 #include "util_math.h"
72 #define DRM_PRIMARY_MINOR_NAME "drm"
73 #define DRM_CONTROL_MINOR_NAME "drmC"
74 #define DRM_RENDER_MINOR_NAME "drmR"
76 #define DRM_PRIMARY_MINOR_NAME "card"
77 #define DRM_CONTROL_MINOR_NAME "controlD"
78 #define DRM_RENDER_MINOR_NAME "renderD"
81 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
95 #endif /* __OpenBSD__ */
98 #define DRM_MAJOR 226 /* Linux */
101 #define DRM_MSG_VERBOSITY 3
103 #define memclear(s) memset(&s, 0, sizeof(s))
105 static drmServerInfoPtr drm_server_info;
107 void drmSetServerInfo(drmServerInfoPtr info)
109 drm_server_info = info;
113 * Output a message to stderr.
115 * \param format printf() like format string.
118 * This function is a wrapper around vfprintf().
121 static int DRM_PRINTFLIKE(1, 0)
122 drmDebugPrint(const char *format, va_list ap)
124 return vfprintf(stderr, format, ap);
128 drmMsg(const char *format, ...)
132 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
133 (drm_server_info && drm_server_info->debug_print))
135 va_start(ap, format);
136 if (drm_server_info) {
137 drm_server_info->debug_print(format,ap);
139 drmDebugPrint(format, ap);
145 static void *drmHashTable = NULL; /* Context switch callbacks */
147 void *drmGetHashTable(void)
152 void *drmMalloc(int size)
154 return calloc(1, size);
157 void drmFree(void *pt)
163 * Call ioctl, restarting if it is interupted
166 drmIoctl(int fd, unsigned long request, void *arg)
171 ret = ioctl(fd, request, arg);
172 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
176 static unsigned long drmGetKeyFromFd(int fd)
185 drmHashEntry *drmGetEntry(int fd)
187 unsigned long key = drmGetKeyFromFd(fd);
192 drmHashTable = drmHashCreate();
194 if (drmHashLookup(drmHashTable, key, &value)) {
195 entry = drmMalloc(sizeof(*entry));
198 entry->tagTable = drmHashCreate();
199 drmHashInsert(drmHashTable, key, entry);
207 * Compare two busid strings
212 * \return 1 if matched.
215 * This function compares two bus ID strings. It understands the older
216 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
217 * domain, b is bus, d is device, f is function.
219 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
221 /* First, check if the IDs are exactly the same */
222 if (strcasecmp(id1, id2) == 0)
225 /* Try to match old/new-style PCI bus IDs. */
226 if (strncasecmp(id1, "pci", 3) == 0) {
227 unsigned int o1, b1, d1, f1;
228 unsigned int o2, b2, d2, f2;
231 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
234 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
239 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
242 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
247 /* If domains aren't properly supported by the kernel interface,
248 * just ignore them, which sucks less than picking a totally random
249 * card with "open by name"
254 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
263 * Handles error checking for chown call.
265 * \param path to file.
266 * \param id of the new owner.
267 * \param id of the new group.
269 * \return zero if success or -1 if failure.
272 * Checks for failure. If failure was caused by signal call chown again.
273 * If any other failure happened then it will output error mesage using
277 static int chown_check_return(const char *path, uid_t owner, gid_t group)
282 rv = chown(path, owner, group);
283 } while (rv != 0 && errno == EINTR);
288 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
289 path, errno, strerror(errno));
295 * Open the DRM device, creating it if necessary.
297 * \param dev major and minor numbers of the device.
298 * \param minor minor number of the device.
300 * \return a file descriptor on success, or a negative value on error.
303 * Assembles the device name from \p minor and opens it, creating the device
304 * special file node with the major and minor numbers specified by \p dev and
305 * parent directory if necessary and was called by root.
307 static int drmOpenDevice(dev_t dev, int minor, int type)
310 const char *dev_name;
313 mode_t devmode = DRM_DEV_MODE, serv_mode;
316 int isroot = !geteuid();
317 uid_t user = DRM_DEV_UID;
318 gid_t group = DRM_DEV_GID;
322 case DRM_NODE_PRIMARY:
323 dev_name = DRM_DEV_NAME;
325 case DRM_NODE_CONTROL:
326 dev_name = DRM_CONTROL_DEV_NAME;
328 case DRM_NODE_RENDER:
329 dev_name = DRM_RENDER_DEV_NAME;
335 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
336 drmMsg("drmOpenDevice: node name is %s\n", buf);
338 if (drm_server_info && drm_server_info->get_perms) {
339 drm_server_info->get_perms(&serv_group, &serv_mode);
340 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
341 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
345 if (stat(DRM_DIR_NAME, &st)) {
347 return DRM_ERR_NOT_ROOT;
348 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
349 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
350 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
353 /* Check if the device node exists and create it if necessary. */
354 if (stat(buf, &st)) {
356 return DRM_ERR_NOT_ROOT;
358 mknod(buf, S_IFCHR | devmode, dev);
361 if (drm_server_info && drm_server_info->get_perms) {
362 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
363 chown_check_return(buf, user, group);
367 /* if we modprobed then wait for udev */
371 if (stat(DRM_DIR_NAME, &st)) {
375 if (udev_count == 50)
380 if (stat(buf, &st)) {
384 if (udev_count == 50)
391 fd = open(buf, O_RDWR, 0);
392 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
393 fd, fd < 0 ? strerror(errno) : "OK");
398 /* Check if the device node is not what we expect it to be, and recreate it
399 * and try again if so.
401 if (st.st_rdev != dev) {
403 return DRM_ERR_NOT_ROOT;
405 mknod(buf, S_IFCHR | devmode, dev);
406 if (drm_server_info && drm_server_info->get_perms) {
407 chown_check_return(buf, user, group);
411 fd = open(buf, O_RDWR, 0);
412 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
413 fd, fd < 0 ? strerror(errno) : "OK");
417 drmMsg("drmOpenDevice: Open failed\n");
425 * Open the DRM device
427 * \param minor device minor number.
428 * \param create allow to create the device if set.
430 * \return a file descriptor on success, or a negative value on error.
433 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
434 * name from \p minor and opens it.
436 static int drmOpenMinor(int minor, int create, int type)
440 const char *dev_name;
443 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
446 case DRM_NODE_PRIMARY:
447 dev_name = DRM_DEV_NAME;
449 case DRM_NODE_CONTROL:
450 dev_name = DRM_CONTROL_DEV_NAME;
452 case DRM_NODE_RENDER:
453 dev_name = DRM_RENDER_DEV_NAME;
459 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
460 if ((fd = open(buf, O_RDWR, 0)) >= 0)
467 * Determine whether the DRM kernel driver has been loaded.
469 * \return 1 if the DRM driver is loaded, 0 otherwise.
472 * Determine the presence of the kernel driver by attempting to open the 0
473 * minor and get version information. For backward compatibility with older
474 * Linux implementations, /proc/dri is also checked.
476 int drmAvailable(void)
478 drmVersionPtr version;
482 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
484 /* Try proc for backward Linux compatibility */
485 if (!access("/proc/dri/0", R_OK))
491 if ((version = drmGetVersion(fd))) {
493 drmFreeVersion(version);
500 static int drmGetMinorBase(int type)
503 case DRM_NODE_PRIMARY:
505 case DRM_NODE_CONTROL:
507 case DRM_NODE_RENDER:
514 static int drmGetMinorType(int minor)
516 int type = minor >> 6;
522 case DRM_NODE_PRIMARY:
523 case DRM_NODE_CONTROL:
524 case DRM_NODE_RENDER:
531 static const char *drmGetMinorName(int type)
534 case DRM_NODE_PRIMARY:
535 return DRM_PRIMARY_MINOR_NAME;
536 case DRM_NODE_CONTROL:
537 return DRM_CONTROL_MINOR_NAME;
538 case DRM_NODE_RENDER:
539 return DRM_RENDER_MINOR_NAME;
546 * Open the device by bus ID.
548 * \param busid bus ID.
549 * \param type device node type.
551 * \return a file descriptor on success, or a negative value on error.
554 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
555 * comparing the device bus ID with the one supplied.
557 * \sa drmOpenMinor() and drmGetBusid().
559 static int drmOpenByBusid(const char *busid, int type)
561 int i, pci_domain_ok = 1;
565 int base = drmGetMinorBase(type);
570 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
571 for (i = base; i < base + DRM_MAX_MINOR; i++) {
572 fd = drmOpenMinor(i, 1, type);
573 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
575 /* We need to try for 1.4 first for proper PCI domain support
576 * and if that fails, we know the kernel is busted
580 sv.drm_dd_major = -1; /* Don't care */
581 sv.drm_dd_minor = -1; /* Don't care */
582 if (drmSetInterfaceVersion(fd, &sv)) {
588 sv.drm_dd_major = -1; /* Don't care */
589 sv.drm_dd_minor = -1; /* Don't care */
590 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
591 drmSetInterfaceVersion(fd, &sv);
593 buf = drmGetBusid(fd);
594 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
595 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
609 * Open the device by name.
611 * \param name driver name.
612 * \param type the device node type.
614 * \return a file descriptor on success, or a negative value on error.
617 * This function opens the first minor number that matches the driver name and
618 * isn't already in use. If it's in use it then it will already have a bus ID
621 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
623 static int drmOpenByName(const char *name, int type)
627 drmVersionPtr version;
629 int base = drmGetMinorBase(type);
635 * Open the first minor number that matches the driver name and isn't
636 * already in use. If it's in use it will have a busid assigned already.
638 for (i = base; i < base + DRM_MAX_MINOR; i++) {
639 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
640 if ((version = drmGetVersion(fd))) {
641 if (!strcmp(version->name, name)) {
642 drmFreeVersion(version);
643 id = drmGetBusid(fd);
644 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
653 drmFreeVersion(version);
661 /* Backward-compatibility /proc support */
662 for (i = 0; i < 8; i++) {
663 char proc_name[64], buf[512];
664 char *driver, *pt, *devstring;
667 sprintf(proc_name, "/proc/dri/%d/name", i);
668 if ((fd = open(proc_name, 0, 0)) >= 0) {
669 retcode = read(fd, buf, sizeof(buf)-1);
672 buf[retcode-1] = '\0';
673 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
675 if (*pt) { /* Device is next */
677 if (!strcmp(driver, name)) { /* Match */
678 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
680 if (*pt) { /* Found busid */
681 return drmOpenByBusid(++pt, type);
682 } else { /* No busid */
683 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
697 * Open the DRM device.
699 * Looks up the specified name and bus ID, and opens the device found. The
700 * entry in /dev/dri is created if necessary and if called by root.
702 * \param name driver name. Not referenced if bus ID is supplied.
703 * \param busid bus ID. Zero if not known.
705 * \return a file descriptor on success, or a negative value on error.
708 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
711 int drmOpen(const char *name, const char *busid)
713 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
717 * Open the DRM device with specified type.
719 * Looks up the specified name and bus ID, and opens the device found. The
720 * entry in /dev/dri is created if necessary and if called by root.
722 * \param name driver name. Not referenced if bus ID is supplied.
723 * \param busid bus ID. Zero if not known.
724 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
726 * \return a file descriptor on success, or a negative value on error.
729 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
732 int drmOpenWithType(const char *name, const char *busid, int type)
734 if (!drmAvailable() && name != NULL && drm_server_info &&
735 drm_server_info->load_module) {
736 /* try to load the kernel module */
737 if (!drm_server_info->load_module(name)) {
738 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
744 int fd = drmOpenByBusid(busid, type);
750 return drmOpenByName(name, type);
755 int drmOpenControl(int minor)
757 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
760 int drmOpenRender(int minor)
762 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
766 * Free the version information returned by drmGetVersion().
768 * \param v pointer to the version information.
771 * It frees the memory pointed by \p %v as well as all the non-null strings
774 void drmFreeVersion(drmVersionPtr v)
786 * Free the non-public version information returned by the kernel.
788 * \param v pointer to the version information.
791 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
792 * the non-null strings pointers in it.
794 static void drmFreeKernelVersion(drm_version_t *v)
806 * Copy version information.
808 * \param d destination pointer.
809 * \param s source pointer.
812 * Used by drmGetVersion() to translate the information returned by the ioctl
813 * interface in a private structure into the public structure counterpart.
815 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
817 d->version_major = s->version_major;
818 d->version_minor = s->version_minor;
819 d->version_patchlevel = s->version_patchlevel;
820 d->name_len = s->name_len;
821 d->name = strdup(s->name);
822 d->date_len = s->date_len;
823 d->date = strdup(s->date);
824 d->desc_len = s->desc_len;
825 d->desc = strdup(s->desc);
830 * Query the driver version information.
832 * \param fd file descriptor.
834 * \return pointer to a drmVersion structure which should be freed with
837 * \note Similar information is available via /proc/dri.
840 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
841 * first with zeros to get the string lengths, and then the actually strings.
842 * It also null-terminates them since they might not be already.
844 drmVersionPtr drmGetVersion(int fd)
846 drmVersionPtr retval;
847 drm_version_t *version = drmMalloc(sizeof(*version));
851 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
852 drmFreeKernelVersion(version);
856 if (version->name_len)
857 version->name = drmMalloc(version->name_len + 1);
858 if (version->date_len)
859 version->date = drmMalloc(version->date_len + 1);
860 if (version->desc_len)
861 version->desc = drmMalloc(version->desc_len + 1);
863 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
864 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
865 drmFreeKernelVersion(version);
869 /* The results might not be null-terminated strings, so terminate them. */
870 if (version->name_len) version->name[version->name_len] = '\0';
871 if (version->date_len) version->date[version->date_len] = '\0';
872 if (version->desc_len) version->desc[version->desc_len] = '\0';
874 retval = drmMalloc(sizeof(*retval));
875 drmCopyVersion(retval, version);
876 drmFreeKernelVersion(version);
882 * Get version information for the DRM user space library.
884 * This version number is driver independent.
886 * \param fd file descriptor.
888 * \return version information.
891 * This function allocates and fills a drm_version structure with a hard coded
894 drmVersionPtr drmGetLibVersion(int fd)
896 drm_version_t *version = drmMalloc(sizeof(*version));
899 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
900 * revision 1.0.x = original DRM interface with no drmGetLibVersion
901 * entry point and many drm<Device> extensions
902 * revision 1.1.x = added drmCommand entry points for device extensions
903 * added drmGetLibVersion to identify libdrm.a version
904 * revision 1.2.x = added drmSetInterfaceVersion
905 * modified drmOpen to handle both busid and name
906 * revision 1.3.x = added server + memory manager
908 version->version_major = 1;
909 version->version_minor = 3;
910 version->version_patchlevel = 0;
912 return (drmVersionPtr)version;
915 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
917 struct drm_get_cap cap;
921 cap.capability = capability;
923 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
931 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
933 struct drm_set_client_cap cap;
936 cap.capability = capability;
939 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
943 * Free the bus ID information.
945 * \param busid bus ID information string as given by drmGetBusid().
948 * This function is just frees the memory pointed by \p busid.
950 void drmFreeBusid(const char *busid)
952 drmFree((void *)busid);
957 * Get the bus ID of the device.
959 * \param fd file descriptor.
961 * \return bus ID string.
964 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
965 * get the string length and data, passing the arguments in a drm_unique
968 char *drmGetBusid(int fd)
974 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
976 u.unique = drmMalloc(u.unique_len + 1);
977 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
979 u.unique[u.unique_len] = '\0';
986 * Set the bus ID of the device.
988 * \param fd file descriptor.
989 * \param busid bus ID string.
991 * \return zero on success, negative on failure.
994 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
995 * the arguments in a drm_unique structure.
997 int drmSetBusid(int fd, const char *busid)
1002 u.unique = (char *)busid;
1003 u.unique_len = strlen(busid);
1005 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1011 int drmGetMagic(int fd, drm_magic_t * magic)
1018 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1020 *magic = auth.magic;
1024 int drmAuthMagic(int fd, drm_magic_t magic)
1030 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1036 * Specifies a range of memory that is available for mapping by a
1039 * \param fd file descriptor.
1040 * \param offset usually the physical address. The actual meaning depends of
1041 * the \p type parameter. See below.
1042 * \param size of the memory in bytes.
1043 * \param type type of the memory to be mapped.
1044 * \param flags combination of several flags to modify the function actions.
1045 * \param handle will be set to a value that may be used as the offset
1046 * parameter for mmap().
1048 * \return zero on success or a negative value on error.
1050 * \par Mapping the frame buffer
1051 * For the frame buffer
1052 * - \p offset will be the physical address of the start of the frame buffer,
1053 * - \p size will be the size of the frame buffer in bytes, and
1054 * - \p type will be DRM_FRAME_BUFFER.
1057 * The area mapped will be uncached. If MTRR support is available in the
1058 * kernel, the frame buffer area will be set to write combining.
1060 * \par Mapping the MMIO register area
1061 * For the MMIO register area,
1062 * - \p offset will be the physical address of the start of the register area,
1063 * - \p size will be the size of the register area bytes, and
1064 * - \p type will be DRM_REGISTERS.
1066 * The area mapped will be uncached.
1068 * \par Mapping the SAREA
1070 * - \p offset will be ignored and should be set to zero,
1071 * - \p size will be the desired size of the SAREA in bytes,
1072 * - \p type will be DRM_SHM.
1075 * A shared memory area of the requested size will be created and locked in
1076 * kernel memory. This area may be mapped into client-space by using the handle
1079 * \note May only be called by root.
1082 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1083 * the arguments in a drm_map structure.
1085 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1086 drmMapFlags flags, drm_handle_t *handle)
1091 map.offset = offset;
1095 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1098 *handle = (drm_handle_t)(uintptr_t)map.handle;
1102 int drmRmMap(int fd, drm_handle_t handle)
1107 map.handle = (void *)(uintptr_t)handle;
1109 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1115 * Make buffers available for DMA transfers.
1117 * \param fd file descriptor.
1118 * \param count number of buffers.
1119 * \param size size of each buffer.
1120 * \param flags buffer allocation flags.
1121 * \param agp_offset offset in the AGP aperture
1123 * \return number of buffers allocated, negative on error.
1126 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1130 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1133 drm_buf_desc_t request;
1136 request.count = count;
1137 request.size = size;
1138 request.flags = flags;
1139 request.agp_start = agp_offset;
1141 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1143 return request.count;
1146 int drmMarkBufs(int fd, double low, double high)
1148 drm_buf_info_t info;
1153 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1159 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1162 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1163 int retval = -errno;
1168 for (i = 0; i < info.count; i++) {
1169 info.list[i].low_mark = low * info.list[i].count;
1170 info.list[i].high_mark = high * info.list[i].count;
1171 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1172 int retval = -errno;
1185 * \param fd file descriptor.
1186 * \param count number of buffers to free.
1187 * \param list list of buffers to be freed.
1189 * \return zero on success, or a negative value on failure.
1191 * \note This function is primarily used for debugging.
1194 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1195 * the arguments in a drm_buf_free structure.
1197 int drmFreeBufs(int fd, int count, int *list)
1199 drm_buf_free_t request;
1202 request.count = count;
1203 request.list = list;
1204 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1213 * \param fd file descriptor.
1216 * This function closes the file descriptor.
1218 int drmClose(int fd)
1220 unsigned long key = drmGetKeyFromFd(fd);
1221 drmHashEntry *entry = drmGetEntry(fd);
1223 drmHashDestroy(entry->tagTable);
1226 entry->tagTable = NULL;
1228 drmHashDelete(drmHashTable, key);
1236 * Map a region of memory.
1238 * \param fd file descriptor.
1239 * \param handle handle returned by drmAddMap().
1240 * \param size size in bytes. Must match the size used by drmAddMap().
1241 * \param address will contain the user-space virtual address where the mapping
1244 * \return zero on success, or a negative value on failure.
1247 * This function is a wrapper for mmap().
1249 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1251 static unsigned long pagesize_mask = 0;
1257 pagesize_mask = getpagesize() - 1;
1259 size = (size + pagesize_mask) & ~pagesize_mask;
1261 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1262 if (*address == MAP_FAILED)
1269 * Unmap mappings obtained with drmMap().
1271 * \param address address as given by drmMap().
1272 * \param size size in bytes. Must match the size used by drmMap().
1274 * \return zero on success, or a negative value on failure.
1277 * This function is a wrapper for munmap().
1279 int drmUnmap(drmAddress address, drmSize size)
1281 return drm_munmap(address, size);
1284 drmBufInfoPtr drmGetBufInfo(int fd)
1286 drm_buf_info_t info;
1287 drmBufInfoPtr retval;
1292 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1296 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1299 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1304 retval = drmMalloc(sizeof(*retval));
1305 retval->count = info.count;
1306 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1307 for (i = 0; i < info.count; i++) {
1308 retval->list[i].count = info.list[i].count;
1309 retval->list[i].size = info.list[i].size;
1310 retval->list[i].low_mark = info.list[i].low_mark;
1311 retval->list[i].high_mark = info.list[i].high_mark;
1320 * Map all DMA buffers into client-virtual space.
1322 * \param fd file descriptor.
1324 * \return a pointer to a ::drmBufMap structure.
1326 * \note The client may not use these buffers until obtaining buffer indices
1330 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1331 * information about the buffers in a drm_buf_map structure into the
1332 * client-visible data structures.
1334 drmBufMapPtr drmMapBufs(int fd)
1337 drmBufMapPtr retval;
1341 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1347 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1350 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1355 retval = drmMalloc(sizeof(*retval));
1356 retval->count = bufs.count;
1357 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1358 for (i = 0; i < bufs.count; i++) {
1359 retval->list[i].idx = bufs.list[i].idx;
1360 retval->list[i].total = bufs.list[i].total;
1361 retval->list[i].used = 0;
1362 retval->list[i].address = bufs.list[i].address;
1372 * Unmap buffers allocated with drmMapBufs().
1374 * \return zero on success, or negative value on failure.
1377 * Calls munmap() for every buffer stored in \p bufs and frees the
1378 * memory allocated by drmMapBufs().
1380 int drmUnmapBufs(drmBufMapPtr bufs)
1384 for (i = 0; i < bufs->count; i++) {
1385 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1388 drmFree(bufs->list);
1395 #define DRM_DMA_RETRY 16
1398 * Reserve DMA buffers.
1400 * \param fd file descriptor.
1403 * \return zero on success, or a negative value on failure.
1406 * Assemble the arguments into a drm_dma structure and keeps issuing the
1407 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1409 int drmDMA(int fd, drmDMAReqPtr request)
1414 dma.context = request->context;
1415 dma.send_count = request->send_count;
1416 dma.send_indices = request->send_list;
1417 dma.send_sizes = request->send_sizes;
1418 dma.flags = request->flags;
1419 dma.request_count = request->request_count;
1420 dma.request_size = request->request_size;
1421 dma.request_indices = request->request_list;
1422 dma.request_sizes = request->request_sizes;
1423 dma.granted_count = 0;
1426 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1427 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1430 request->granted_count = dma.granted_count;
1439 * Obtain heavyweight hardware lock.
1441 * \param fd file descriptor.
1442 * \param context context.
1443 * \param flags flags that determine the sate of the hardware when the function
1446 * \return always zero.
1449 * This function translates the arguments into a drm_lock structure and issue
1450 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1452 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1457 lock.context = context;
1459 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1460 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1461 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1462 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1463 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1464 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1466 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1472 * Release the hardware lock.
1474 * \param fd file descriptor.
1475 * \param context context.
1477 * \return zero on success, or a negative value on failure.
1480 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1481 * argument in a drm_lock structure.
1483 int drmUnlock(int fd, drm_context_t context)
1488 lock.context = context;
1489 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1492 drm_context_t *drmGetReservedContextList(int fd, int *count)
1496 drm_context_t * retval;
1500 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1506 if (!(list = drmMalloc(res.count * sizeof(*list))))
1508 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1513 res.contexts = list;
1514 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1517 for (i = 0; i < res.count; i++)
1518 retval[i] = list[i].handle;
1525 void drmFreeReservedContextList(drm_context_t *pt)
1533 * Used by the X server during GLXContext initialization. This causes
1534 * per-context kernel-level resources to be allocated.
1536 * \param fd file descriptor.
1537 * \param handle is set on success. To be used by the client when requesting DMA
1538 * dispatch with drmDMA().
1540 * \return zero on success, or a negative value on failure.
1542 * \note May only be called by root.
1545 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1546 * argument in a drm_ctx structure.
1548 int drmCreateContext(int fd, drm_context_t *handle)
1553 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1555 *handle = ctx.handle;
1559 int drmSwitchToContext(int fd, drm_context_t context)
1564 ctx.handle = context;
1565 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1570 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1575 * Context preserving means that no context switches are done between DMA
1576 * buffers from one context and the next. This is suitable for use in the
1577 * X server (which promises to maintain hardware context), or in the
1578 * client-side library when buffers are swapped on behalf of two threads.
1581 ctx.handle = context;
1582 if (flags & DRM_CONTEXT_PRESERVED)
1583 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1584 if (flags & DRM_CONTEXT_2DONLY)
1585 ctx.flags |= _DRM_CONTEXT_2DONLY;
1586 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1591 int drmGetContextFlags(int fd, drm_context_t context,
1592 drm_context_tFlagsPtr flags)
1597 ctx.handle = context;
1598 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1601 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1602 *flags |= DRM_CONTEXT_PRESERVED;
1603 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1604 *flags |= DRM_CONTEXT_2DONLY;
1611 * Free any kernel-level resources allocated with drmCreateContext() associated
1614 * \param fd file descriptor.
1615 * \param handle handle given by drmCreateContext().
1617 * \return zero on success, or a negative value on failure.
1619 * \note May only be called by root.
1622 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1623 * argument in a drm_ctx structure.
1625 int drmDestroyContext(int fd, drm_context_t handle)
1630 ctx.handle = handle;
1631 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1636 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1641 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1643 *handle = draw.handle;
1647 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1652 draw.handle = handle;
1653 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1658 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1659 drm_drawable_info_type_t type, unsigned int num,
1662 drm_update_draw_t update;
1665 update.handle = handle;
1668 update.data = (unsigned long long)(unsigned long)data;
1670 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1677 * Acquire the AGP device.
1679 * Must be called before any of the other AGP related calls.
1681 * \param fd file descriptor.
1683 * \return zero on success, or a negative value on failure.
1686 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1688 int drmAgpAcquire(int fd)
1690 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1697 * Release the AGP device.
1699 * \param fd file descriptor.
1701 * \return zero on success, or a negative value on failure.
1704 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1706 int drmAgpRelease(int fd)
1708 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1717 * \param fd file descriptor.
1718 * \param mode AGP mode.
1720 * \return zero on success, or a negative value on failure.
1723 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1724 * argument in a drm_agp_mode structure.
1726 int drmAgpEnable(int fd, unsigned long mode)
1732 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1739 * Allocate a chunk of AGP memory.
1741 * \param fd file descriptor.
1742 * \param size requested memory size in bytes. Will be rounded to page boundary.
1743 * \param type type of memory to allocate.
1744 * \param address if not zero, will be set to the physical address of the
1746 * \param handle on success will be set to a handle of the allocated memory.
1748 * \return zero on success, or a negative value on failure.
1751 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1752 * arguments in a drm_agp_buffer structure.
1754 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1755 unsigned long *address, drm_handle_t *handle)
1760 *handle = DRM_AGP_NO_HANDLE;
1763 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1766 *address = b.physical;
1773 * Free a chunk of AGP memory.
1775 * \param fd file descriptor.
1776 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1778 * \return zero on success, or a negative value on failure.
1781 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1782 * argument in a drm_agp_buffer structure.
1784 int drmAgpFree(int fd, drm_handle_t handle)
1790 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1797 * Bind a chunk of AGP memory.
1799 * \param fd file descriptor.
1800 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1801 * \param offset offset in bytes. It will round to page boundary.
1803 * \return zero on success, or a negative value on failure.
1806 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1807 * argument in a drm_agp_binding structure.
1809 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1811 drm_agp_binding_t b;
1816 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1823 * Unbind a chunk of AGP memory.
1825 * \param fd file descriptor.
1826 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1828 * \return zero on success, or a negative value on failure.
1831 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1832 * the argument in a drm_agp_binding structure.
1834 int drmAgpUnbind(int fd, drm_handle_t handle)
1836 drm_agp_binding_t b;
1840 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1847 * Get AGP driver major version number.
1849 * \param fd file descriptor.
1851 * \return major version number on success, or a negative value on failure..
1854 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1855 * necessary information in a drm_agp_info structure.
1857 int drmAgpVersionMajor(int fd)
1863 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1865 return i.agp_version_major;
1870 * Get AGP driver minor version number.
1872 * \param fd file descriptor.
1874 * \return minor version number on success, or a negative value on failure.
1877 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1878 * necessary information in a drm_agp_info structure.
1880 int drmAgpVersionMinor(int fd)
1886 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1888 return i.agp_version_minor;
1895 * \param fd file descriptor.
1897 * \return mode on success, or zero on failure.
1900 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1901 * necessary information in a drm_agp_info structure.
1903 unsigned long drmAgpGetMode(int fd)
1909 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1916 * Get AGP aperture base.
1918 * \param fd file descriptor.
1920 * \return aperture base on success, zero on failure.
1923 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1924 * necessary information in a drm_agp_info structure.
1926 unsigned long drmAgpBase(int fd)
1932 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1934 return i.aperture_base;
1939 * Get AGP aperture size.
1941 * \param fd file descriptor.
1943 * \return aperture size on success, zero on failure.
1946 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1947 * necessary information in a drm_agp_info structure.
1949 unsigned long drmAgpSize(int fd)
1955 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1957 return i.aperture_size;
1962 * Get used AGP memory.
1964 * \param fd file descriptor.
1966 * \return memory used on success, or zero on failure.
1969 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1970 * necessary information in a drm_agp_info structure.
1972 unsigned long drmAgpMemoryUsed(int fd)
1978 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1980 return i.memory_used;
1985 * Get available AGP memory.
1987 * \param fd file descriptor.
1989 * \return memory available on success, or zero on failure.
1992 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1993 * necessary information in a drm_agp_info structure.
1995 unsigned long drmAgpMemoryAvail(int fd)
2001 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2003 return i.memory_allowed;
2008 * Get hardware vendor ID.
2010 * \param fd file descriptor.
2012 * \return vendor ID on success, or zero on failure.
2015 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2016 * necessary information in a drm_agp_info structure.
2018 unsigned int drmAgpVendorId(int fd)
2024 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2031 * Get hardware device ID.
2033 * \param fd file descriptor.
2035 * \return zero on success, or zero on failure.
2038 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2039 * necessary information in a drm_agp_info structure.
2041 unsigned int drmAgpDeviceId(int fd)
2047 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2052 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2054 drm_scatter_gather_t sg;
2060 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2062 *handle = sg.handle;
2066 int drmScatterGatherFree(int fd, drm_handle_t handle)
2068 drm_scatter_gather_t sg;
2072 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2080 * \param fd file descriptor.
2081 * \param vbl pointer to a drmVBlank structure.
2083 * \return zero on success, or a negative value on failure.
2086 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2088 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2090 struct timespec timeout, cur;
2093 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2095 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2101 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2102 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2103 if (ret && errno == EINTR) {
2104 clock_gettime(CLOCK_MONOTONIC, &cur);
2105 /* Timeout after 1s */
2106 if (cur.tv_sec > timeout.tv_sec + 1 ||
2107 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2114 } while (ret && errno == EINTR);
2120 int drmError(int err, const char *label)
2123 case DRM_ERR_NO_DEVICE:
2124 fprintf(stderr, "%s: no device\n", label);
2126 case DRM_ERR_NO_ACCESS:
2127 fprintf(stderr, "%s: no access\n", label);
2129 case DRM_ERR_NOT_ROOT:
2130 fprintf(stderr, "%s: not root\n", label);
2132 case DRM_ERR_INVALID:
2133 fprintf(stderr, "%s: invalid args\n", label);
2138 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2146 * Install IRQ handler.
2148 * \param fd file descriptor.
2149 * \param irq IRQ number.
2151 * \return zero on success, or a negative value on failure.
2154 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2155 * argument in a drm_control structure.
2157 int drmCtlInstHandler(int fd, int irq)
2162 ctl.func = DRM_INST_HANDLER;
2164 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2171 * Uninstall IRQ handler.
2173 * \param fd file descriptor.
2175 * \return zero on success, or a negative value on failure.
2178 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2179 * argument in a drm_control structure.
2181 int drmCtlUninstHandler(int fd)
2186 ctl.func = DRM_UNINST_HANDLER;
2188 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2193 int drmFinish(int fd, int context, drmLockFlags flags)
2198 lock.context = context;
2199 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2200 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2201 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2202 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2203 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2204 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2205 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2211 * Get IRQ from bus ID.
2213 * \param fd file descriptor.
2214 * \param busnum bus number.
2215 * \param devnum device number.
2216 * \param funcnum function number.
2218 * \return IRQ number on success, or a negative value on failure.
2221 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2222 * arguments in a drm_irq_busid structure.
2224 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2231 p.funcnum = funcnum;
2232 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2237 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2239 drmHashEntry *entry = drmGetEntry(fd);
2241 if (drmHashInsert(entry->tagTable, context, tag)) {
2242 drmHashDelete(entry->tagTable, context);
2243 drmHashInsert(entry->tagTable, context, tag);
2248 int drmDelContextTag(int fd, drm_context_t context)
2250 drmHashEntry *entry = drmGetEntry(fd);
2252 return drmHashDelete(entry->tagTable, context);
2255 void *drmGetContextTag(int fd, drm_context_t context)
2257 drmHashEntry *entry = drmGetEntry(fd);
2260 if (drmHashLookup(entry->tagTable, context, &value))
2266 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2267 drm_handle_t handle)
2269 drm_ctx_priv_map_t map;
2272 map.ctx_id = ctx_id;
2273 map.handle = (void *)(uintptr_t)handle;
2275 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2280 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2281 drm_handle_t *handle)
2283 drm_ctx_priv_map_t map;
2286 map.ctx_id = ctx_id;
2288 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2291 *handle = (drm_handle_t)(uintptr_t)map.handle;
2296 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2297 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2304 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2306 *offset = map.offset;
2310 *handle = (unsigned long)map.handle;
2315 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2316 unsigned long *magic, unsigned long *iocs)
2318 drm_client_t client;
2322 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2324 *auth = client.auth;
2327 *magic = client.magic;
2328 *iocs = client.iocs;
2332 int drmGetStats(int fd, drmStatsT *stats)
2338 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2342 memset(stats, 0, sizeof(*stats));
2343 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2347 stats->data[i].long_format = "%-20.20s"; \
2348 stats->data[i].rate_format = "%8.8s"; \
2349 stats->data[i].isvalue = 1; \
2350 stats->data[i].verbose = 0
2353 stats->data[i].long_format = "%-20.20s"; \
2354 stats->data[i].rate_format = "%5.5s"; \
2355 stats->data[i].isvalue = 0; \
2356 stats->data[i].mult_names = "kgm"; \
2357 stats->data[i].mult = 1000; \
2358 stats->data[i].verbose = 0
2361 stats->data[i].long_format = "%-20.20s"; \
2362 stats->data[i].rate_format = "%5.5s"; \
2363 stats->data[i].isvalue = 0; \
2364 stats->data[i].mult_names = "KGM"; \
2365 stats->data[i].mult = 1024; \
2366 stats->data[i].verbose = 0
2369 stats->count = s.count;
2370 for (i = 0; i < s.count; i++) {
2371 stats->data[i].value = s.data[i].value;
2372 switch (s.data[i].type) {
2373 case _DRM_STAT_LOCK:
2374 stats->data[i].long_name = "Lock";
2375 stats->data[i].rate_name = "Lock";
2378 case _DRM_STAT_OPENS:
2379 stats->data[i].long_name = "Opens";
2380 stats->data[i].rate_name = "O";
2382 stats->data[i].verbose = 1;
2384 case _DRM_STAT_CLOSES:
2385 stats->data[i].long_name = "Closes";
2386 stats->data[i].rate_name = "Lock";
2388 stats->data[i].verbose = 1;
2390 case _DRM_STAT_IOCTLS:
2391 stats->data[i].long_name = "Ioctls";
2392 stats->data[i].rate_name = "Ioc/s";
2395 case _DRM_STAT_LOCKS:
2396 stats->data[i].long_name = "Locks";
2397 stats->data[i].rate_name = "Lck/s";
2400 case _DRM_STAT_UNLOCKS:
2401 stats->data[i].long_name = "Unlocks";
2402 stats->data[i].rate_name = "Unl/s";
2406 stats->data[i].long_name = "IRQs";
2407 stats->data[i].rate_name = "IRQ/s";
2410 case _DRM_STAT_PRIMARY:
2411 stats->data[i].long_name = "Primary Bytes";
2412 stats->data[i].rate_name = "PB/s";
2415 case _DRM_STAT_SECONDARY:
2416 stats->data[i].long_name = "Secondary Bytes";
2417 stats->data[i].rate_name = "SB/s";
2421 stats->data[i].long_name = "DMA";
2422 stats->data[i].rate_name = "DMA/s";
2425 case _DRM_STAT_SPECIAL:
2426 stats->data[i].long_name = "Special DMA";
2427 stats->data[i].rate_name = "dma/s";
2430 case _DRM_STAT_MISSED:
2431 stats->data[i].long_name = "Miss";
2432 stats->data[i].rate_name = "Ms/s";
2435 case _DRM_STAT_VALUE:
2436 stats->data[i].long_name = "Value";
2437 stats->data[i].rate_name = "Value";
2440 case _DRM_STAT_BYTE:
2441 stats->data[i].long_name = "Bytes";
2442 stats->data[i].rate_name = "B/s";
2445 case _DRM_STAT_COUNT:
2447 stats->data[i].long_name = "Count";
2448 stats->data[i].rate_name = "Cnt/s";
2457 * Issue a set-version ioctl.
2459 * \param fd file descriptor.
2460 * \param drmCommandIndex command index
2461 * \param data source pointer of the data to be read and written.
2462 * \param size size of the data to be read and written.
2464 * \return zero on success, or a negative value on failure.
2467 * It issues a read-write ioctl given by
2468 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2470 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2473 drm_set_version_t sv;
2476 sv.drm_di_major = version->drm_di_major;
2477 sv.drm_di_minor = version->drm_di_minor;
2478 sv.drm_dd_major = version->drm_dd_major;
2479 sv.drm_dd_minor = version->drm_dd_minor;
2481 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2485 version->drm_di_major = sv.drm_di_major;
2486 version->drm_di_minor = sv.drm_di_minor;
2487 version->drm_dd_major = sv.drm_dd_major;
2488 version->drm_dd_minor = sv.drm_dd_minor;
2494 * Send a device-specific command.
2496 * \param fd file descriptor.
2497 * \param drmCommandIndex command index
2499 * \return zero on success, or a negative value on failure.
2502 * It issues a ioctl given by
2503 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2505 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2507 unsigned long request;
2509 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2511 if (drmIoctl(fd, request, NULL)) {
2519 * Send a device-specific read command.
2521 * \param fd file descriptor.
2522 * \param drmCommandIndex command index
2523 * \param data destination pointer of the data to be read.
2524 * \param size size of the data to be read.
2526 * \return zero on success, or a negative value on failure.
2529 * It issues a read ioctl given by
2530 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2532 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2535 unsigned long request;
2537 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2538 DRM_COMMAND_BASE + drmCommandIndex, size);
2540 if (drmIoctl(fd, request, data)) {
2548 * Send a device-specific write command.
2550 * \param fd file descriptor.
2551 * \param drmCommandIndex command index
2552 * \param data source pointer of the data to be written.
2553 * \param size size of the data to be written.
2555 * \return zero on success, or a negative value on failure.
2558 * It issues a write ioctl given by
2559 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2561 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2564 unsigned long request;
2566 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2567 DRM_COMMAND_BASE + drmCommandIndex, size);
2569 if (drmIoctl(fd, request, data)) {
2577 * Send a device-specific read-write command.
2579 * \param fd file descriptor.
2580 * \param drmCommandIndex command index
2581 * \param data source pointer of the data to be read and written.
2582 * \param size size of the data to be read and written.
2584 * \return zero on success, or a negative value on failure.
2587 * It issues a read-write ioctl given by
2588 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2590 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2593 unsigned long request;
2595 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2596 DRM_COMMAND_BASE + drmCommandIndex, size);
2598 if (drmIoctl(fd, request, data))
2603 #define DRM_MAX_FDS 16
2609 } connection[DRM_MAX_FDS];
2611 static int nr_fds = 0;
2613 int drmOpenOnce(void *unused,
2617 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2620 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2625 for (i = 0; i < nr_fds; i++)
2626 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2627 (connection[i].type == type)) {
2628 connection[i].refcount++;
2630 return connection[i].fd;
2633 fd = drmOpenWithType(NULL, BusID, type);
2634 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2637 connection[nr_fds].BusID = strdup(BusID);
2638 connection[nr_fds].fd = fd;
2639 connection[nr_fds].refcount = 1;
2640 connection[nr_fds].type = type;
2644 fprintf(stderr, "saved connection %d for %s %d\n",
2645 nr_fds, connection[nr_fds].BusID,
2646 strcmp(BusID, connection[nr_fds].BusID));
2653 void drmCloseOnce(int fd)
2657 for (i = 0; i < nr_fds; i++) {
2658 if (fd == connection[i].fd) {
2659 if (--connection[i].refcount == 0) {
2660 drmClose(connection[i].fd);
2661 free(connection[i].BusID);
2664 connection[i] = connection[nr_fds];
2672 int drmSetMaster(int fd)
2674 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2677 int drmDropMaster(int fd)
2679 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2682 char *drmGetDeviceNameFromFd(int fd)
2689 /* The whole drmOpen thing is a fiasco and we need to find a way
2690 * back to just using open(2). For now, however, lets just make
2691 * things worse with even more ad hoc directory walking code to
2692 * discover the device file name. */
2697 for (i = 0; i < DRM_MAX_MINOR; i++) {
2698 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2699 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2702 if (i == DRM_MAX_MINOR)
2705 return strdup(name);
2708 int drmGetNodeTypeFromFd(int fd)
2713 if (fstat(fd, &sbuf))
2716 maj = major(sbuf.st_rdev);
2717 min = minor(sbuf.st_rdev);
2719 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2724 type = drmGetMinorType(min);
2730 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2732 struct drm_prime_handle args;
2737 args.handle = handle;
2739 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2743 *prime_fd = args.fd;
2747 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2749 struct drm_prime_handle args;
2754 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2758 *handle = args.handle;
2762 static char *drmGetMinorNameForFD(int fd, int type)
2766 struct dirent *pent, *ent;
2768 const char *name = drmGetMinorName(type);
2770 char dev_name[64], buf[64];
2779 if (fstat(fd, &sbuf))
2782 maj = major(sbuf.st_rdev);
2783 min = minor(sbuf.st_rdev);
2785 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2788 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2790 sysdir = opendir(buf);
2794 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2798 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2802 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2803 if (strncmp(ent->d_name, name, len) == 0) {
2804 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2810 return strdup(dev_name);
2819 #warning "Missing implementation of drmGetMinorNameForFD"
2824 char *drmGetPrimaryDeviceNameFromFd(int fd)
2826 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2829 char *drmGetRenderDeviceNameFromFd(int fd)
2831 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2834 static int drmParseSubsystemType(int maj, int min)
2837 char path[PATH_MAX + 1];
2838 char link[PATH_MAX + 1] = "";
2841 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2844 if (readlink(path, link, PATH_MAX) < 0)
2847 name = strrchr(link, '/');
2851 if (strncmp(name, "/pci", 4) == 0)
2856 #warning "Missing implementation of drmParseSubsystemType"
2861 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
2864 char path[PATH_MAX + 1];
2867 int domain, bus, dev, func;
2870 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2871 fd = open(path, O_RDONLY);
2875 ret = read(fd, data, sizeof(data));
2880 #define TAG "PCI_SLOT_NAME="
2881 str = strstr(data, TAG);
2885 if (sscanf(str, TAG "%04x:%02x:%02x.%1u",
2886 &domain, &bus, &dev, &func) != 4)
2890 info->domain = domain;
2897 #warning "Missing implementation of drmParsePciBusInfo"
2902 static int drmCompareBusInfo(drmDevicePtr a, drmDevicePtr b)
2904 if (a == NULL || b == NULL)
2907 if (a->bustype != b->bustype)
2910 switch (a->bustype) {
2912 return memcmp(a->businfo.pci, b->businfo.pci, sizeof(drmPciBusInfo));
2920 static int drmGetNodeType(const char *name)
2922 if (strncmp(name, DRM_PRIMARY_MINOR_NAME,
2923 sizeof(DRM_PRIMARY_MINOR_NAME) - 1) == 0)
2924 return DRM_NODE_PRIMARY;
2926 if (strncmp(name, DRM_CONTROL_MINOR_NAME,
2927 sizeof(DRM_CONTROL_MINOR_NAME ) - 1) == 0)
2928 return DRM_NODE_CONTROL;
2930 if (strncmp(name, DRM_RENDER_MINOR_NAME,
2931 sizeof(DRM_RENDER_MINOR_NAME) - 1) == 0)
2932 return DRM_NODE_RENDER;
2937 static int drmGetMaxNodeName(void)
2939 return sizeof(DRM_DIR_NAME) +
2940 MAX3(sizeof(DRM_PRIMARY_MINOR_NAME),
2941 sizeof(DRM_CONTROL_MINOR_NAME),
2942 sizeof(DRM_RENDER_MINOR_NAME)) +
2943 3 /* lenght of the node number */;
2946 static int drmParsePciDeviceInfo(const char *d_name,
2947 drmPciDeviceInfoPtr device)
2950 char path[PATH_MAX + 1];
2951 unsigned char config[64];
2954 snprintf(path, PATH_MAX, "/sys/class/drm/%s/device/config", d_name);
2955 fd = open(path, O_RDONLY);
2959 ret = read(fd, config, sizeof(config));
2964 device->vendor_id = config[0] | (config[1] << 8);
2965 device->device_id = config[2] | (config[3] << 8);
2966 device->revision_id = config[8];
2967 device->subvendor_id = config[44] | (config[45] << 8);
2968 device->subdevice_id = config[46] | (config[47] << 8);
2972 #warning "Missing implementation of drmParsePciDeviceInfo"
2977 void drmFreeDevice(drmDevicePtr *device)
2986 void drmFreeDevices(drmDevicePtr devices[], int count)
2990 if (devices == NULL)
2993 for (i = 0; i < count && devices[i] != NULL; i++)
2994 drmFreeDevice(&devices[i]);
2997 static int drmProcessPciDevice(drmDevicePtr *device, const char *d_name,
2998 const char *node, int node_type,
2999 int maj, int min, bool fetch_deviceinfo)
3001 const int max_node_str = drmGetMaxNodeName();
3005 addr = *device = calloc(1, sizeof(drmDevice) +
3007 (sizeof(void *) + max_node_str)) +
3008 sizeof(drmPciBusInfo) +
3009 sizeof(drmPciDeviceInfo));
3013 (*device)->bustype = DRM_BUS_PCI;
3014 (*device)->available_nodes = 1 << node_type;
3016 addr += sizeof(drmDevice);
3017 (*device)->nodes = addr;
3019 addr += DRM_NODE_MAX * sizeof(void *);
3020 for (i = 0; i < DRM_NODE_MAX; i++) {
3021 (*device)->nodes[i] = addr;
3022 addr += max_node_str;
3024 memcpy((*device)->nodes[node_type], node, max_node_str);
3026 (*device)->businfo.pci = addr;
3028 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3032 // Fetch the device info if the user has requested it
3033 if (fetch_deviceinfo) {
3034 addr += sizeof(drmPciBusInfo);
3035 (*device)->deviceinfo.pci = addr;
3037 ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci);
3049 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3051 int node_type, i, j;
3053 for (i = 0; i < count; i++) {
3054 for (j = i + 1; j < count; j++) {
3055 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3056 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3057 node_type = log2(local_devices[j]->available_nodes);
3058 memcpy(local_devices[i]->nodes[node_type],
3059 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3060 drmFreeDevice(&local_devices[j]);
3067 * Get information about the opened drm device
3069 * \param fd file descriptor of the drm device
3070 * \param device the address of a drmDevicePtr where the information
3071 * will be allocated in stored
3073 * \return zero on success, negative error code otherwise.
3075 int drmGetDevice(int fd, drmDevicePtr *device)
3077 drmDevicePtr *local_devices;
3080 struct dirent *dent;
3082 char node[PATH_MAX + 1];
3083 int node_type, subsystem_type;
3085 int ret, i, node_count;
3088 if (fd == -1 || device == NULL)
3091 if (fstat(fd, &sbuf))
3094 maj = major(sbuf.st_rdev);
3095 min = minor(sbuf.st_rdev);
3097 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3100 subsystem_type = drmParseSubsystemType(maj, min);
3102 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3103 if (local_devices == NULL)
3106 sysdir = opendir(DRM_DIR_NAME);
3113 while ((dent = readdir(sysdir))) {
3114 node_type = drmGetNodeType(dent->d_name);
3118 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3119 if (stat(node, &sbuf))
3122 maj = major(sbuf.st_rdev);
3123 min = minor(sbuf.st_rdev);
3125 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3128 if (drmParseSubsystemType(maj, min) != subsystem_type)
3131 switch (subsystem_type) {
3133 ret = drmProcessPciDevice(&d, dent->d_name, node, node_type,
3140 fprintf(stderr, "The subsystem type is not supported yet\n");
3144 if (i >= max_count) {
3148 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3151 local_devices = temp;
3154 local_devices[i] = d;
3159 /* Fold nodes into a single device if they share the same bus info */
3160 drmFoldDuplicatedDevices(local_devices, node_count);
3162 *device = local_devices[0];
3163 for (i = 1; i < node_count && local_devices[i]; i++)
3164 drmFreeDevice(&local_devices[i]);
3166 free(local_devices);
3171 drmFreeDevices(local_devices, i);
3172 free(local_devices);
3180 * Get drm devices on the system
3182 * \param devices the array of devices with drmDevicePtr elements
3183 * can be NULL to get the device number first
3184 * \param max_devices the maximum number of devices for the array
3186 * \return on error - negative error code,
3187 * if devices is NULL - total number of devices available on the system,
3188 * alternatively the number of devices stored in devices[], which is
3189 * capped by the max_devices.
3191 int drmGetDevices(drmDevicePtr devices[], int max_devices)
3193 drmDevicePtr *local_devices;
3194 drmDevicePtr device;
3196 struct dirent *dent;
3198 char node[PATH_MAX + 1];
3199 int node_type, subsystem_type;
3201 int ret, i, node_count, device_count;
3204 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3205 if (local_devices == NULL)
3208 sysdir = opendir(DRM_DIR_NAME);
3215 while ((dent = readdir(sysdir))) {
3216 node_type = drmGetNodeType(dent->d_name);
3220 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3221 if (stat(node, &sbuf))
3224 maj = major(sbuf.st_rdev);
3225 min = minor(sbuf.st_rdev);
3227 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3230 subsystem_type = drmParseSubsystemType(maj, min);
3232 if (subsystem_type < 0)
3235 switch (subsystem_type) {
3237 ret = drmProcessPciDevice(&device, dent->d_name, node, node_type,
3238 maj, min, devices != NULL);
3244 fprintf(stderr, "The subsystem type is not supported yet\n");
3248 if (i >= max_count) {
3252 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3255 local_devices = temp;
3258 local_devices[i] = device;
3263 /* Fold nodes into a single device if they share the same bus info */
3264 drmFoldDuplicatedDevices(local_devices, node_count);
3267 for (i = 0; i < node_count && local_devices[i]; i++) {
3268 if ((devices != NULL) && (device_count < max_devices))
3269 devices[device_count] = local_devices[i];
3271 drmFreeDevice(&local_devices[i]);
3276 free(local_devices);
3278 return device_count;
3281 drmFreeDevices(local_devices, i);
3282 free(local_devices);