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.
51 #include <sys/types.h>
53 #define stat_t struct stat
54 #include <sys/ioctl.h>
57 #ifdef HAVE_SYS_MKDEV_H
58 # include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
62 /* Not all systems have MAP_FAILED defined */
64 #define MAP_FAILED ((void *)-1)
68 #include "libdrm_macros.h"
70 #include "util_math.h"
73 #define DRM_PRIMARY_MINOR_NAME "drm"
74 #define DRM_CONTROL_MINOR_NAME "drmC"
75 #define DRM_RENDER_MINOR_NAME "drmR"
77 #define DRM_PRIMARY_MINOR_NAME "card"
78 #define DRM_CONTROL_MINOR_NAME "controlD"
79 #define DRM_RENDER_MINOR_NAME "renderD"
82 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
96 #endif /* __OpenBSD__ */
99 #define DRM_MAJOR 226 /* Linux */
102 #define DRM_MSG_VERBOSITY 3
104 #define memclear(s) memset(&s, 0, sizeof(s))
106 static drmServerInfoPtr drm_server_info;
108 void drmSetServerInfo(drmServerInfoPtr info)
110 drm_server_info = info;
114 * Output a message to stderr.
116 * \param format printf() like format string.
119 * This function is a wrapper around vfprintf().
122 static int DRM_PRINTFLIKE(1, 0)
123 drmDebugPrint(const char *format, va_list ap)
125 return vfprintf(stderr, format, ap);
129 drmMsg(const char *format, ...)
133 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) ||
134 (drm_server_info && drm_server_info->debug_print))
136 va_start(ap, format);
137 if (drm_server_info) {
138 drm_server_info->debug_print(format,ap);
140 drmDebugPrint(format, ap);
146 static void *drmHashTable = NULL; /* Context switch callbacks */
148 void *drmGetHashTable(void)
153 void *drmMalloc(int size)
155 return calloc(1, size);
158 void drmFree(void *pt)
164 * Call ioctl, restarting if it is interupted
167 drmIoctl(int fd, unsigned long request, void *arg)
172 ret = ioctl(fd, request, arg);
173 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
177 static unsigned long drmGetKeyFromFd(int fd)
186 drmHashEntry *drmGetEntry(int fd)
188 unsigned long key = drmGetKeyFromFd(fd);
193 drmHashTable = drmHashCreate();
195 if (drmHashLookup(drmHashTable, key, &value)) {
196 entry = drmMalloc(sizeof(*entry));
199 entry->tagTable = drmHashCreate();
200 drmHashInsert(drmHashTable, key, entry);
208 * Compare two busid strings
213 * \return 1 if matched.
216 * This function compares two bus ID strings. It understands the older
217 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
218 * domain, b is bus, d is device, f is function.
220 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
222 /* First, check if the IDs are exactly the same */
223 if (strcasecmp(id1, id2) == 0)
226 /* Try to match old/new-style PCI bus IDs. */
227 if (strncasecmp(id1, "pci", 3) == 0) {
228 unsigned int o1, b1, d1, f1;
229 unsigned int o2, b2, d2, f2;
232 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
235 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
240 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
243 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
248 /* If domains aren't properly supported by the kernel interface,
249 * just ignore them, which sucks less than picking a totally random
250 * card with "open by name"
255 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
264 * Handles error checking for chown call.
266 * \param path to file.
267 * \param id of the new owner.
268 * \param id of the new group.
270 * \return zero if success or -1 if failure.
273 * Checks for failure. If failure was caused by signal call chown again.
274 * If any other failure happened then it will output error mesage using
278 static int chown_check_return(const char *path, uid_t owner, gid_t group)
283 rv = chown(path, owner, group);
284 } while (rv != 0 && errno == EINTR);
289 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
290 path, errno, strerror(errno));
296 * Open the DRM device, creating it if necessary.
298 * \param dev major and minor numbers of the device.
299 * \param minor minor number of the device.
301 * \return a file descriptor on success, or a negative value on error.
304 * Assembles the device name from \p minor and opens it, creating the device
305 * special file node with the major and minor numbers specified by \p dev and
306 * parent directory if necessary and was called by root.
308 static int drmOpenDevice(dev_t dev, int minor, int type)
311 const char *dev_name;
314 mode_t devmode = DRM_DEV_MODE, serv_mode;
317 int isroot = !geteuid();
318 uid_t user = DRM_DEV_UID;
319 gid_t group = DRM_DEV_GID;
323 case DRM_NODE_PRIMARY:
324 dev_name = DRM_DEV_NAME;
326 case DRM_NODE_CONTROL:
327 dev_name = DRM_CONTROL_DEV_NAME;
329 case DRM_NODE_RENDER:
330 dev_name = DRM_RENDER_DEV_NAME;
336 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
337 drmMsg("drmOpenDevice: node name is %s\n", buf);
339 if (drm_server_info && drm_server_info->get_perms) {
340 drm_server_info->get_perms(&serv_group, &serv_mode);
341 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
342 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
346 if (stat(DRM_DIR_NAME, &st)) {
348 return DRM_ERR_NOT_ROOT;
349 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
350 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
351 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
354 /* Check if the device node exists and create it if necessary. */
355 if (stat(buf, &st)) {
357 return DRM_ERR_NOT_ROOT;
359 mknod(buf, S_IFCHR | devmode, dev);
362 if (drm_server_info && drm_server_info->get_perms) {
363 group = ((int)serv_group >= 0) ? serv_group : DRM_DEV_GID;
364 chown_check_return(buf, user, group);
368 /* if we modprobed then wait for udev */
372 if (stat(DRM_DIR_NAME, &st)) {
376 if (udev_count == 50)
381 if (stat(buf, &st)) {
385 if (udev_count == 50)
392 fd = open(buf, O_RDWR, 0);
393 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
394 fd, fd < 0 ? strerror(errno) : "OK");
399 /* Check if the device node is not what we expect it to be, and recreate it
400 * and try again if so.
402 if (st.st_rdev != dev) {
404 return DRM_ERR_NOT_ROOT;
406 mknod(buf, S_IFCHR | devmode, dev);
407 if (drm_server_info && drm_server_info->get_perms) {
408 chown_check_return(buf, user, group);
412 fd = open(buf, O_RDWR, 0);
413 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
414 fd, fd < 0 ? strerror(errno) : "OK");
418 drmMsg("drmOpenDevice: Open failed\n");
426 * Open the DRM device
428 * \param minor device minor number.
429 * \param create allow to create the device if set.
431 * \return a file descriptor on success, or a negative value on error.
434 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
435 * name from \p minor and opens it.
437 static int drmOpenMinor(int minor, int create, int type)
441 const char *dev_name;
444 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
447 case DRM_NODE_PRIMARY:
448 dev_name = DRM_DEV_NAME;
450 case DRM_NODE_CONTROL:
451 dev_name = DRM_CONTROL_DEV_NAME;
453 case DRM_NODE_RENDER:
454 dev_name = DRM_RENDER_DEV_NAME;
460 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
461 if ((fd = open(buf, O_RDWR, 0)) >= 0)
468 * Determine whether the DRM kernel driver has been loaded.
470 * \return 1 if the DRM driver is loaded, 0 otherwise.
473 * Determine the presence of the kernel driver by attempting to open the 0
474 * minor and get version information. For backward compatibility with older
475 * Linux implementations, /proc/dri is also checked.
477 int drmAvailable(void)
479 drmVersionPtr version;
483 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
485 /* Try proc for backward Linux compatibility */
486 if (!access("/proc/dri/0", R_OK))
492 if ((version = drmGetVersion(fd))) {
494 drmFreeVersion(version);
501 static int drmGetMinorBase(int type)
504 case DRM_NODE_PRIMARY:
506 case DRM_NODE_CONTROL:
508 case DRM_NODE_RENDER:
515 static int drmGetMinorType(int minor)
517 int type = minor >> 6;
523 case DRM_NODE_PRIMARY:
524 case DRM_NODE_CONTROL:
525 case DRM_NODE_RENDER:
532 static const char *drmGetMinorName(int type)
535 case DRM_NODE_PRIMARY:
536 return DRM_PRIMARY_MINOR_NAME;
537 case DRM_NODE_CONTROL:
538 return DRM_CONTROL_MINOR_NAME;
539 case DRM_NODE_RENDER:
540 return DRM_RENDER_MINOR_NAME;
547 * Open the device by bus ID.
549 * \param busid bus ID.
550 * \param type device node type.
552 * \return a file descriptor on success, or a negative value on error.
555 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
556 * comparing the device bus ID with the one supplied.
558 * \sa drmOpenMinor() and drmGetBusid().
560 static int drmOpenByBusid(const char *busid, int type)
562 int i, pci_domain_ok = 1;
566 int base = drmGetMinorBase(type);
571 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
572 for (i = base; i < base + DRM_MAX_MINOR; i++) {
573 fd = drmOpenMinor(i, 1, type);
574 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
576 /* We need to try for 1.4 first for proper PCI domain support
577 * and if that fails, we know the kernel is busted
581 sv.drm_dd_major = -1; /* Don't care */
582 sv.drm_dd_minor = -1; /* Don't care */
583 if (drmSetInterfaceVersion(fd, &sv)) {
589 sv.drm_dd_major = -1; /* Don't care */
590 sv.drm_dd_minor = -1; /* Don't care */
591 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
592 drmSetInterfaceVersion(fd, &sv);
594 buf = drmGetBusid(fd);
595 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
596 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
610 * Open the device by name.
612 * \param name driver name.
613 * \param type the device node type.
615 * \return a file descriptor on success, or a negative value on error.
618 * This function opens the first minor number that matches the driver name and
619 * isn't already in use. If it's in use it then it will already have a bus ID
622 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
624 static int drmOpenByName(const char *name, int type)
628 drmVersionPtr version;
630 int base = drmGetMinorBase(type);
636 * Open the first minor number that matches the driver name and isn't
637 * already in use. If it's in use it will have a busid assigned already.
639 for (i = base; i < base + DRM_MAX_MINOR; i++) {
640 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
641 if ((version = drmGetVersion(fd))) {
642 if (!strcmp(version->name, name)) {
643 drmFreeVersion(version);
644 id = drmGetBusid(fd);
645 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
654 drmFreeVersion(version);
662 /* Backward-compatibility /proc support */
663 for (i = 0; i < 8; i++) {
664 char proc_name[64], buf[512];
665 char *driver, *pt, *devstring;
668 sprintf(proc_name, "/proc/dri/%d/name", i);
669 if ((fd = open(proc_name, 0, 0)) >= 0) {
670 retcode = read(fd, buf, sizeof(buf)-1);
673 buf[retcode-1] = '\0';
674 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
676 if (*pt) { /* Device is next */
678 if (!strcmp(driver, name)) { /* Match */
679 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
681 if (*pt) { /* Found busid */
682 return drmOpenByBusid(++pt, type);
683 } else { /* No busid */
684 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
698 * Open the DRM device.
700 * Looks up the specified name and bus ID, and opens the device found. The
701 * entry in /dev/dri is created if necessary and if called by root.
703 * \param name driver name. Not referenced if bus ID is supplied.
704 * \param busid bus ID. Zero if not known.
706 * \return a file descriptor on success, or a negative value on error.
709 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
712 int drmOpen(const char *name, const char *busid)
714 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
718 * Open the DRM device with specified type.
720 * Looks up the specified name and bus ID, and opens the device found. The
721 * entry in /dev/dri is created if necessary and if called by root.
723 * \param name driver name. Not referenced if bus ID is supplied.
724 * \param busid bus ID. Zero if not known.
725 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
727 * \return a file descriptor on success, or a negative value on error.
730 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
733 int drmOpenWithType(const char *name, const char *busid, int type)
735 if (!drmAvailable() && name != NULL && drm_server_info &&
736 drm_server_info->load_module) {
737 /* try to load the kernel module */
738 if (!drm_server_info->load_module(name)) {
739 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
745 int fd = drmOpenByBusid(busid, type);
751 return drmOpenByName(name, type);
756 int drmOpenControl(int minor)
758 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
761 int drmOpenRender(int minor)
763 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
767 * Free the version information returned by drmGetVersion().
769 * \param v pointer to the version information.
772 * It frees the memory pointed by \p %v as well as all the non-null strings
775 void drmFreeVersion(drmVersionPtr v)
787 * Free the non-public version information returned by the kernel.
789 * \param v pointer to the version information.
792 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
793 * the non-null strings pointers in it.
795 static void drmFreeKernelVersion(drm_version_t *v)
807 * Copy version information.
809 * \param d destination pointer.
810 * \param s source pointer.
813 * Used by drmGetVersion() to translate the information returned by the ioctl
814 * interface in a private structure into the public structure counterpart.
816 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
818 d->version_major = s->version_major;
819 d->version_minor = s->version_minor;
820 d->version_patchlevel = s->version_patchlevel;
821 d->name_len = s->name_len;
822 d->name = strdup(s->name);
823 d->date_len = s->date_len;
824 d->date = strdup(s->date);
825 d->desc_len = s->desc_len;
826 d->desc = strdup(s->desc);
831 * Query the driver version information.
833 * \param fd file descriptor.
835 * \return pointer to a drmVersion structure which should be freed with
838 * \note Similar information is available via /proc/dri.
841 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
842 * first with zeros to get the string lengths, and then the actually strings.
843 * It also null-terminates them since they might not be already.
845 drmVersionPtr drmGetVersion(int fd)
847 drmVersionPtr retval;
848 drm_version_t *version = drmMalloc(sizeof(*version));
852 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
853 drmFreeKernelVersion(version);
857 if (version->name_len)
858 version->name = drmMalloc(version->name_len + 1);
859 if (version->date_len)
860 version->date = drmMalloc(version->date_len + 1);
861 if (version->desc_len)
862 version->desc = drmMalloc(version->desc_len + 1);
864 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
865 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
866 drmFreeKernelVersion(version);
870 /* The results might not be null-terminated strings, so terminate them. */
871 if (version->name_len) version->name[version->name_len] = '\0';
872 if (version->date_len) version->date[version->date_len] = '\0';
873 if (version->desc_len) version->desc[version->desc_len] = '\0';
875 retval = drmMalloc(sizeof(*retval));
876 drmCopyVersion(retval, version);
877 drmFreeKernelVersion(version);
883 * Get version information for the DRM user space library.
885 * This version number is driver independent.
887 * \param fd file descriptor.
889 * \return version information.
892 * This function allocates and fills a drm_version structure with a hard coded
895 drmVersionPtr drmGetLibVersion(int fd)
897 drm_version_t *version = drmMalloc(sizeof(*version));
900 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
901 * revision 1.0.x = original DRM interface with no drmGetLibVersion
902 * entry point and many drm<Device> extensions
903 * revision 1.1.x = added drmCommand entry points for device extensions
904 * added drmGetLibVersion to identify libdrm.a version
905 * revision 1.2.x = added drmSetInterfaceVersion
906 * modified drmOpen to handle both busid and name
907 * revision 1.3.x = added server + memory manager
909 version->version_major = 1;
910 version->version_minor = 3;
911 version->version_patchlevel = 0;
913 return (drmVersionPtr)version;
916 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
918 struct drm_get_cap cap;
922 cap.capability = capability;
924 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
932 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
934 struct drm_set_client_cap cap;
937 cap.capability = capability;
940 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
944 * Free the bus ID information.
946 * \param busid bus ID information string as given by drmGetBusid().
949 * This function is just frees the memory pointed by \p busid.
951 void drmFreeBusid(const char *busid)
953 drmFree((void *)busid);
958 * Get the bus ID of the device.
960 * \param fd file descriptor.
962 * \return bus ID string.
965 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
966 * get the string length and data, passing the arguments in a drm_unique
969 char *drmGetBusid(int fd)
975 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
977 u.unique = drmMalloc(u.unique_len + 1);
978 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
980 u.unique[u.unique_len] = '\0';
987 * Set the bus ID of the device.
989 * \param fd file descriptor.
990 * \param busid bus ID string.
992 * \return zero on success, negative on failure.
995 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
996 * the arguments in a drm_unique structure.
998 int drmSetBusid(int fd, const char *busid)
1003 u.unique = (char *)busid;
1004 u.unique_len = strlen(busid);
1006 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1012 int drmGetMagic(int fd, drm_magic_t * magic)
1019 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1021 *magic = auth.magic;
1025 int drmAuthMagic(int fd, drm_magic_t magic)
1031 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1037 * Specifies a range of memory that is available for mapping by a
1040 * \param fd file descriptor.
1041 * \param offset usually the physical address. The actual meaning depends of
1042 * the \p type parameter. See below.
1043 * \param size of the memory in bytes.
1044 * \param type type of the memory to be mapped.
1045 * \param flags combination of several flags to modify the function actions.
1046 * \param handle will be set to a value that may be used as the offset
1047 * parameter for mmap().
1049 * \return zero on success or a negative value on error.
1051 * \par Mapping the frame buffer
1052 * For the frame buffer
1053 * - \p offset will be the physical address of the start of the frame buffer,
1054 * - \p size will be the size of the frame buffer in bytes, and
1055 * - \p type will be DRM_FRAME_BUFFER.
1058 * The area mapped will be uncached. If MTRR support is available in the
1059 * kernel, the frame buffer area will be set to write combining.
1061 * \par Mapping the MMIO register area
1062 * For the MMIO register area,
1063 * - \p offset will be the physical address of the start of the register area,
1064 * - \p size will be the size of the register area bytes, and
1065 * - \p type will be DRM_REGISTERS.
1067 * The area mapped will be uncached.
1069 * \par Mapping the SAREA
1071 * - \p offset will be ignored and should be set to zero,
1072 * - \p size will be the desired size of the SAREA in bytes,
1073 * - \p type will be DRM_SHM.
1076 * A shared memory area of the requested size will be created and locked in
1077 * kernel memory. This area may be mapped into client-space by using the handle
1080 * \note May only be called by root.
1083 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1084 * the arguments in a drm_map structure.
1086 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1087 drmMapFlags flags, drm_handle_t *handle)
1092 map.offset = offset;
1096 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1099 *handle = (drm_handle_t)(uintptr_t)map.handle;
1103 int drmRmMap(int fd, drm_handle_t handle)
1108 map.handle = (void *)(uintptr_t)handle;
1110 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1116 * Make buffers available for DMA transfers.
1118 * \param fd file descriptor.
1119 * \param count number of buffers.
1120 * \param size size of each buffer.
1121 * \param flags buffer allocation flags.
1122 * \param agp_offset offset in the AGP aperture
1124 * \return number of buffers allocated, negative on error.
1127 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1131 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1134 drm_buf_desc_t request;
1137 request.count = count;
1138 request.size = size;
1139 request.flags = flags;
1140 request.agp_start = agp_offset;
1142 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1144 return request.count;
1147 int drmMarkBufs(int fd, double low, double high)
1149 drm_buf_info_t info;
1154 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1160 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1163 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1164 int retval = -errno;
1169 for (i = 0; i < info.count; i++) {
1170 info.list[i].low_mark = low * info.list[i].count;
1171 info.list[i].high_mark = high * info.list[i].count;
1172 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1173 int retval = -errno;
1186 * \param fd file descriptor.
1187 * \param count number of buffers to free.
1188 * \param list list of buffers to be freed.
1190 * \return zero on success, or a negative value on failure.
1192 * \note This function is primarily used for debugging.
1195 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1196 * the arguments in a drm_buf_free structure.
1198 int drmFreeBufs(int fd, int count, int *list)
1200 drm_buf_free_t request;
1203 request.count = count;
1204 request.list = list;
1205 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1214 * \param fd file descriptor.
1217 * This function closes the file descriptor.
1219 int drmClose(int fd)
1221 unsigned long key = drmGetKeyFromFd(fd);
1222 drmHashEntry *entry = drmGetEntry(fd);
1224 drmHashDestroy(entry->tagTable);
1227 entry->tagTable = NULL;
1229 drmHashDelete(drmHashTable, key);
1237 * Map a region of memory.
1239 * \param fd file descriptor.
1240 * \param handle handle returned by drmAddMap().
1241 * \param size size in bytes. Must match the size used by drmAddMap().
1242 * \param address will contain the user-space virtual address where the mapping
1245 * \return zero on success, or a negative value on failure.
1248 * This function is a wrapper for mmap().
1250 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1252 static unsigned long pagesize_mask = 0;
1258 pagesize_mask = getpagesize() - 1;
1260 size = (size + pagesize_mask) & ~pagesize_mask;
1262 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1263 if (*address == MAP_FAILED)
1270 * Unmap mappings obtained with drmMap().
1272 * \param address address as given by drmMap().
1273 * \param size size in bytes. Must match the size used by drmMap().
1275 * \return zero on success, or a negative value on failure.
1278 * This function is a wrapper for munmap().
1280 int drmUnmap(drmAddress address, drmSize size)
1282 return drm_munmap(address, size);
1285 drmBufInfoPtr drmGetBufInfo(int fd)
1287 drm_buf_info_t info;
1288 drmBufInfoPtr retval;
1293 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1297 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1300 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1305 retval = drmMalloc(sizeof(*retval));
1306 retval->count = info.count;
1307 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1308 for (i = 0; i < info.count; i++) {
1309 retval->list[i].count = info.list[i].count;
1310 retval->list[i].size = info.list[i].size;
1311 retval->list[i].low_mark = info.list[i].low_mark;
1312 retval->list[i].high_mark = info.list[i].high_mark;
1321 * Map all DMA buffers into client-virtual space.
1323 * \param fd file descriptor.
1325 * \return a pointer to a ::drmBufMap structure.
1327 * \note The client may not use these buffers until obtaining buffer indices
1331 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1332 * information about the buffers in a drm_buf_map structure into the
1333 * client-visible data structures.
1335 drmBufMapPtr drmMapBufs(int fd)
1338 drmBufMapPtr retval;
1342 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1348 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1351 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1356 retval = drmMalloc(sizeof(*retval));
1357 retval->count = bufs.count;
1358 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1359 for (i = 0; i < bufs.count; i++) {
1360 retval->list[i].idx = bufs.list[i].idx;
1361 retval->list[i].total = bufs.list[i].total;
1362 retval->list[i].used = 0;
1363 retval->list[i].address = bufs.list[i].address;
1372 * Unmap buffers allocated with drmMapBufs().
1374 * \return zero on success, or negative value on failure.
1377 * Calls munmap() for every buffer stored in \p bufs and frees the
1378 * memory allocated by drmMapBufs().
1380 int drmUnmapBufs(drmBufMapPtr bufs)
1384 for (i = 0; i < bufs->count; i++) {
1385 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1388 drmFree(bufs->list);
1394 #define DRM_DMA_RETRY 16
1397 * Reserve DMA buffers.
1399 * \param fd file descriptor.
1402 * \return zero on success, or a negative value on failure.
1405 * Assemble the arguments into a drm_dma structure and keeps issuing the
1406 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1408 int drmDMA(int fd, drmDMAReqPtr request)
1413 dma.context = request->context;
1414 dma.send_count = request->send_count;
1415 dma.send_indices = request->send_list;
1416 dma.send_sizes = request->send_sizes;
1417 dma.flags = request->flags;
1418 dma.request_count = request->request_count;
1419 dma.request_size = request->request_size;
1420 dma.request_indices = request->request_list;
1421 dma.request_sizes = request->request_sizes;
1422 dma.granted_count = 0;
1425 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1426 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1429 request->granted_count = dma.granted_count;
1438 * Obtain heavyweight hardware lock.
1440 * \param fd file descriptor.
1441 * \param context context.
1442 * \param flags flags that determine the sate of the hardware when the function
1445 * \return always zero.
1448 * This function translates the arguments into a drm_lock structure and issue
1449 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1451 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1456 lock.context = context;
1458 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1459 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1460 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1461 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1462 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1463 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1465 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1471 * Release the hardware lock.
1473 * \param fd file descriptor.
1474 * \param context context.
1476 * \return zero on success, or a negative value on failure.
1479 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1480 * argument in a drm_lock structure.
1482 int drmUnlock(int fd, drm_context_t context)
1487 lock.context = context;
1488 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1491 drm_context_t *drmGetReservedContextList(int fd, int *count)
1495 drm_context_t * retval;
1499 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1505 if (!(list = drmMalloc(res.count * sizeof(*list))))
1507 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1512 res.contexts = list;
1513 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1516 for (i = 0; i < res.count; i++)
1517 retval[i] = list[i].handle;
1524 void drmFreeReservedContextList(drm_context_t *pt)
1532 * Used by the X server during GLXContext initialization. This causes
1533 * per-context kernel-level resources to be allocated.
1535 * \param fd file descriptor.
1536 * \param handle is set on success. To be used by the client when requesting DMA
1537 * dispatch with drmDMA().
1539 * \return zero on success, or a negative value on failure.
1541 * \note May only be called by root.
1544 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1545 * argument in a drm_ctx structure.
1547 int drmCreateContext(int fd, drm_context_t *handle)
1552 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1554 *handle = ctx.handle;
1558 int drmSwitchToContext(int fd, drm_context_t context)
1563 ctx.handle = context;
1564 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1569 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1574 * Context preserving means that no context switches are done between DMA
1575 * buffers from one context and the next. This is suitable for use in the
1576 * X server (which promises to maintain hardware context), or in the
1577 * client-side library when buffers are swapped on behalf of two threads.
1580 ctx.handle = context;
1581 if (flags & DRM_CONTEXT_PRESERVED)
1582 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1583 if (flags & DRM_CONTEXT_2DONLY)
1584 ctx.flags |= _DRM_CONTEXT_2DONLY;
1585 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1590 int drmGetContextFlags(int fd, drm_context_t context,
1591 drm_context_tFlagsPtr flags)
1596 ctx.handle = context;
1597 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1600 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1601 *flags |= DRM_CONTEXT_PRESERVED;
1602 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1603 *flags |= DRM_CONTEXT_2DONLY;
1610 * Free any kernel-level resources allocated with drmCreateContext() associated
1613 * \param fd file descriptor.
1614 * \param handle handle given by drmCreateContext().
1616 * \return zero on success, or a negative value on failure.
1618 * \note May only be called by root.
1621 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1622 * argument in a drm_ctx structure.
1624 int drmDestroyContext(int fd, drm_context_t handle)
1629 ctx.handle = handle;
1630 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1635 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1640 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1642 *handle = draw.handle;
1646 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1651 draw.handle = handle;
1652 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1657 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1658 drm_drawable_info_type_t type, unsigned int num,
1661 drm_update_draw_t update;
1664 update.handle = handle;
1667 update.data = (unsigned long long)(unsigned long)data;
1669 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1676 * Acquire the AGP device.
1678 * Must be called before any of the other AGP related calls.
1680 * \param fd file descriptor.
1682 * \return zero on success, or a negative value on failure.
1685 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1687 int drmAgpAcquire(int fd)
1689 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1696 * Release the AGP device.
1698 * \param fd file descriptor.
1700 * \return zero on success, or a negative value on failure.
1703 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1705 int drmAgpRelease(int fd)
1707 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1716 * \param fd file descriptor.
1717 * \param mode AGP mode.
1719 * \return zero on success, or a negative value on failure.
1722 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1723 * argument in a drm_agp_mode structure.
1725 int drmAgpEnable(int fd, unsigned long mode)
1731 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1738 * Allocate a chunk of AGP memory.
1740 * \param fd file descriptor.
1741 * \param size requested memory size in bytes. Will be rounded to page boundary.
1742 * \param type type of memory to allocate.
1743 * \param address if not zero, will be set to the physical address of the
1745 * \param handle on success will be set to a handle of the allocated memory.
1747 * \return zero on success, or a negative value on failure.
1750 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1751 * arguments in a drm_agp_buffer structure.
1753 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1754 unsigned long *address, drm_handle_t *handle)
1759 *handle = DRM_AGP_NO_HANDLE;
1762 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1765 *address = b.physical;
1772 * Free a chunk of AGP memory.
1774 * \param fd file descriptor.
1775 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1777 * \return zero on success, or a negative value on failure.
1780 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1781 * argument in a drm_agp_buffer structure.
1783 int drmAgpFree(int fd, drm_handle_t handle)
1789 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1796 * Bind a chunk of AGP memory.
1798 * \param fd file descriptor.
1799 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1800 * \param offset offset in bytes. It will round to page boundary.
1802 * \return zero on success, or a negative value on failure.
1805 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1806 * argument in a drm_agp_binding structure.
1808 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1810 drm_agp_binding_t b;
1815 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1822 * Unbind a chunk of AGP memory.
1824 * \param fd file descriptor.
1825 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1827 * \return zero on success, or a negative value on failure.
1830 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1831 * the argument in a drm_agp_binding structure.
1833 int drmAgpUnbind(int fd, drm_handle_t handle)
1835 drm_agp_binding_t b;
1839 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1846 * Get AGP driver major version number.
1848 * \param fd file descriptor.
1850 * \return major version number on success, or a negative value on failure..
1853 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1854 * necessary information in a drm_agp_info structure.
1856 int drmAgpVersionMajor(int fd)
1862 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1864 return i.agp_version_major;
1869 * Get AGP driver minor version number.
1871 * \param fd file descriptor.
1873 * \return minor version number on success, or a negative value on failure.
1876 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1877 * necessary information in a drm_agp_info structure.
1879 int drmAgpVersionMinor(int fd)
1885 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1887 return i.agp_version_minor;
1894 * \param fd file descriptor.
1896 * \return mode on success, or zero on failure.
1899 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1900 * necessary information in a drm_agp_info structure.
1902 unsigned long drmAgpGetMode(int fd)
1908 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1915 * Get AGP aperture base.
1917 * \param fd file descriptor.
1919 * \return aperture base on success, zero on failure.
1922 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1923 * necessary information in a drm_agp_info structure.
1925 unsigned long drmAgpBase(int fd)
1931 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1933 return i.aperture_base;
1938 * Get AGP aperture size.
1940 * \param fd file descriptor.
1942 * \return aperture size on success, zero on failure.
1945 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1946 * necessary information in a drm_agp_info structure.
1948 unsigned long drmAgpSize(int fd)
1954 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1956 return i.aperture_size;
1961 * Get used AGP memory.
1963 * \param fd file descriptor.
1965 * \return memory used on success, or zero on failure.
1968 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1969 * necessary information in a drm_agp_info structure.
1971 unsigned long drmAgpMemoryUsed(int fd)
1977 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1979 return i.memory_used;
1984 * Get available AGP memory.
1986 * \param fd file descriptor.
1988 * \return memory available on success, or zero on failure.
1991 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1992 * necessary information in a drm_agp_info structure.
1994 unsigned long drmAgpMemoryAvail(int fd)
2000 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2002 return i.memory_allowed;
2007 * Get hardware vendor ID.
2009 * \param fd file descriptor.
2011 * \return vendor ID on success, or zero on failure.
2014 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2015 * necessary information in a drm_agp_info structure.
2017 unsigned int drmAgpVendorId(int fd)
2023 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2030 * Get hardware device ID.
2032 * \param fd file descriptor.
2034 * \return zero on success, or zero on failure.
2037 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2038 * necessary information in a drm_agp_info structure.
2040 unsigned int drmAgpDeviceId(int fd)
2046 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2051 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2053 drm_scatter_gather_t sg;
2059 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2061 *handle = sg.handle;
2065 int drmScatterGatherFree(int fd, drm_handle_t handle)
2067 drm_scatter_gather_t sg;
2071 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2079 * \param fd file descriptor.
2080 * \param vbl pointer to a drmVBlank structure.
2082 * \return zero on success, or a negative value on failure.
2085 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2087 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2089 struct timespec timeout, cur;
2092 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2094 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2100 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2101 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2102 if (ret && errno == EINTR) {
2103 clock_gettime(CLOCK_MONOTONIC, &cur);
2104 /* Timeout after 1s */
2105 if (cur.tv_sec > timeout.tv_sec + 1 ||
2106 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2113 } while (ret && errno == EINTR);
2119 int drmError(int err, const char *label)
2122 case DRM_ERR_NO_DEVICE:
2123 fprintf(stderr, "%s: no device\n", label);
2125 case DRM_ERR_NO_ACCESS:
2126 fprintf(stderr, "%s: no access\n", label);
2128 case DRM_ERR_NOT_ROOT:
2129 fprintf(stderr, "%s: not root\n", label);
2131 case DRM_ERR_INVALID:
2132 fprintf(stderr, "%s: invalid args\n", label);
2137 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2145 * Install IRQ handler.
2147 * \param fd file descriptor.
2148 * \param irq IRQ number.
2150 * \return zero on success, or a negative value on failure.
2153 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2154 * argument in a drm_control structure.
2156 int drmCtlInstHandler(int fd, int irq)
2161 ctl.func = DRM_INST_HANDLER;
2163 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2170 * Uninstall IRQ handler.
2172 * \param fd file descriptor.
2174 * \return zero on success, or a negative value on failure.
2177 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2178 * argument in a drm_control structure.
2180 int drmCtlUninstHandler(int fd)
2185 ctl.func = DRM_UNINST_HANDLER;
2187 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2192 int drmFinish(int fd, int context, drmLockFlags flags)
2197 lock.context = context;
2198 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2199 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2200 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2201 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2202 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2203 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2204 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2210 * Get IRQ from bus ID.
2212 * \param fd file descriptor.
2213 * \param busnum bus number.
2214 * \param devnum device number.
2215 * \param funcnum function number.
2217 * \return IRQ number on success, or a negative value on failure.
2220 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2221 * arguments in a drm_irq_busid structure.
2223 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2230 p.funcnum = funcnum;
2231 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2236 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2238 drmHashEntry *entry = drmGetEntry(fd);
2240 if (drmHashInsert(entry->tagTable, context, tag)) {
2241 drmHashDelete(entry->tagTable, context);
2242 drmHashInsert(entry->tagTable, context, tag);
2247 int drmDelContextTag(int fd, drm_context_t context)
2249 drmHashEntry *entry = drmGetEntry(fd);
2251 return drmHashDelete(entry->tagTable, context);
2254 void *drmGetContextTag(int fd, drm_context_t context)
2256 drmHashEntry *entry = drmGetEntry(fd);
2259 if (drmHashLookup(entry->tagTable, context, &value))
2265 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2266 drm_handle_t handle)
2268 drm_ctx_priv_map_t map;
2271 map.ctx_id = ctx_id;
2272 map.handle = (void *)(uintptr_t)handle;
2274 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2279 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2280 drm_handle_t *handle)
2282 drm_ctx_priv_map_t map;
2285 map.ctx_id = ctx_id;
2287 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2290 *handle = (drm_handle_t)(uintptr_t)map.handle;
2295 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2296 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2303 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2305 *offset = map.offset;
2309 *handle = (unsigned long)map.handle;
2314 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2315 unsigned long *magic, unsigned long *iocs)
2317 drm_client_t client;
2321 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2323 *auth = client.auth;
2326 *magic = client.magic;
2327 *iocs = client.iocs;
2331 int drmGetStats(int fd, drmStatsT *stats)
2337 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2341 memset(stats, 0, sizeof(*stats));
2342 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2346 stats->data[i].long_format = "%-20.20s"; \
2347 stats->data[i].rate_format = "%8.8s"; \
2348 stats->data[i].isvalue = 1; \
2349 stats->data[i].verbose = 0
2352 stats->data[i].long_format = "%-20.20s"; \
2353 stats->data[i].rate_format = "%5.5s"; \
2354 stats->data[i].isvalue = 0; \
2355 stats->data[i].mult_names = "kgm"; \
2356 stats->data[i].mult = 1000; \
2357 stats->data[i].verbose = 0
2360 stats->data[i].long_format = "%-20.20s"; \
2361 stats->data[i].rate_format = "%5.5s"; \
2362 stats->data[i].isvalue = 0; \
2363 stats->data[i].mult_names = "KGM"; \
2364 stats->data[i].mult = 1024; \
2365 stats->data[i].verbose = 0
2368 stats->count = s.count;
2369 for (i = 0; i < s.count; i++) {
2370 stats->data[i].value = s.data[i].value;
2371 switch (s.data[i].type) {
2372 case _DRM_STAT_LOCK:
2373 stats->data[i].long_name = "Lock";
2374 stats->data[i].rate_name = "Lock";
2377 case _DRM_STAT_OPENS:
2378 stats->data[i].long_name = "Opens";
2379 stats->data[i].rate_name = "O";
2381 stats->data[i].verbose = 1;
2383 case _DRM_STAT_CLOSES:
2384 stats->data[i].long_name = "Closes";
2385 stats->data[i].rate_name = "Lock";
2387 stats->data[i].verbose = 1;
2389 case _DRM_STAT_IOCTLS:
2390 stats->data[i].long_name = "Ioctls";
2391 stats->data[i].rate_name = "Ioc/s";
2394 case _DRM_STAT_LOCKS:
2395 stats->data[i].long_name = "Locks";
2396 stats->data[i].rate_name = "Lck/s";
2399 case _DRM_STAT_UNLOCKS:
2400 stats->data[i].long_name = "Unlocks";
2401 stats->data[i].rate_name = "Unl/s";
2405 stats->data[i].long_name = "IRQs";
2406 stats->data[i].rate_name = "IRQ/s";
2409 case _DRM_STAT_PRIMARY:
2410 stats->data[i].long_name = "Primary Bytes";
2411 stats->data[i].rate_name = "PB/s";
2414 case _DRM_STAT_SECONDARY:
2415 stats->data[i].long_name = "Secondary Bytes";
2416 stats->data[i].rate_name = "SB/s";
2420 stats->data[i].long_name = "DMA";
2421 stats->data[i].rate_name = "DMA/s";
2424 case _DRM_STAT_SPECIAL:
2425 stats->data[i].long_name = "Special DMA";
2426 stats->data[i].rate_name = "dma/s";
2429 case _DRM_STAT_MISSED:
2430 stats->data[i].long_name = "Miss";
2431 stats->data[i].rate_name = "Ms/s";
2434 case _DRM_STAT_VALUE:
2435 stats->data[i].long_name = "Value";
2436 stats->data[i].rate_name = "Value";
2439 case _DRM_STAT_BYTE:
2440 stats->data[i].long_name = "Bytes";
2441 stats->data[i].rate_name = "B/s";
2444 case _DRM_STAT_COUNT:
2446 stats->data[i].long_name = "Count";
2447 stats->data[i].rate_name = "Cnt/s";
2456 * Issue a set-version ioctl.
2458 * \param fd file descriptor.
2459 * \param drmCommandIndex command index
2460 * \param data source pointer of the data to be read and written.
2461 * \param size size of the data to be read and written.
2463 * \return zero on success, or a negative value on failure.
2466 * It issues a read-write ioctl given by
2467 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2469 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2472 drm_set_version_t sv;
2475 sv.drm_di_major = version->drm_di_major;
2476 sv.drm_di_minor = version->drm_di_minor;
2477 sv.drm_dd_major = version->drm_dd_major;
2478 sv.drm_dd_minor = version->drm_dd_minor;
2480 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2484 version->drm_di_major = sv.drm_di_major;
2485 version->drm_di_minor = sv.drm_di_minor;
2486 version->drm_dd_major = sv.drm_dd_major;
2487 version->drm_dd_minor = sv.drm_dd_minor;
2493 * Send a device-specific command.
2495 * \param fd file descriptor.
2496 * \param drmCommandIndex command index
2498 * \return zero on success, or a negative value on failure.
2501 * It issues a ioctl given by
2502 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2504 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2506 unsigned long request;
2508 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2510 if (drmIoctl(fd, request, NULL)) {
2518 * Send a device-specific read command.
2520 * \param fd file descriptor.
2521 * \param drmCommandIndex command index
2522 * \param data destination pointer of the data to be read.
2523 * \param size size of the data to be read.
2525 * \return zero on success, or a negative value on failure.
2528 * It issues a read ioctl given by
2529 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2531 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2534 unsigned long request;
2536 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2537 DRM_COMMAND_BASE + drmCommandIndex, size);
2539 if (drmIoctl(fd, request, data)) {
2547 * Send a device-specific write command.
2549 * \param fd file descriptor.
2550 * \param drmCommandIndex command index
2551 * \param data source pointer of the data to be written.
2552 * \param size size of the data to be written.
2554 * \return zero on success, or a negative value on failure.
2557 * It issues a write ioctl given by
2558 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2560 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2563 unsigned long request;
2565 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2566 DRM_COMMAND_BASE + drmCommandIndex, size);
2568 if (drmIoctl(fd, request, data)) {
2576 * Send a device-specific read-write command.
2578 * \param fd file descriptor.
2579 * \param drmCommandIndex command index
2580 * \param data source pointer of the data to be read and written.
2581 * \param size size of the data to be read and written.
2583 * \return zero on success, or a negative value on failure.
2586 * It issues a read-write ioctl given by
2587 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2589 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2592 unsigned long request;
2594 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2595 DRM_COMMAND_BASE + drmCommandIndex, size);
2597 if (drmIoctl(fd, request, data))
2602 #define DRM_MAX_FDS 16
2608 } connection[DRM_MAX_FDS];
2610 static int nr_fds = 0;
2612 int drmOpenOnce(void *unused,
2616 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2619 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2624 for (i = 0; i < nr_fds; i++)
2625 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2626 (connection[i].type == type)) {
2627 connection[i].refcount++;
2629 return connection[i].fd;
2632 fd = drmOpenWithType(NULL, BusID, type);
2633 if (fd < 0 || nr_fds == DRM_MAX_FDS)
2636 connection[nr_fds].BusID = strdup(BusID);
2637 connection[nr_fds].fd = fd;
2638 connection[nr_fds].refcount = 1;
2639 connection[nr_fds].type = type;
2643 fprintf(stderr, "saved connection %d for %s %d\n",
2644 nr_fds, connection[nr_fds].BusID,
2645 strcmp(BusID, connection[nr_fds].BusID));
2652 void drmCloseOnce(int fd)
2656 for (i = 0; i < nr_fds; i++) {
2657 if (fd == connection[i].fd) {
2658 if (--connection[i].refcount == 0) {
2659 drmClose(connection[i].fd);
2660 free(connection[i].BusID);
2663 connection[i] = connection[nr_fds];
2671 int drmSetMaster(int fd)
2673 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2676 int drmDropMaster(int fd)
2678 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2681 char *drmGetDeviceNameFromFd(int fd)
2688 /* The whole drmOpen thing is a fiasco and we need to find a way
2689 * back to just using open(2). For now, however, lets just make
2690 * things worse with even more ad hoc directory walking code to
2691 * discover the device file name. */
2696 for (i = 0; i < DRM_MAX_MINOR; i++) {
2697 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2698 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2701 if (i == DRM_MAX_MINOR)
2704 return strdup(name);
2707 int drmGetNodeTypeFromFd(int fd)
2712 if (fstat(fd, &sbuf))
2715 maj = major(sbuf.st_rdev);
2716 min = minor(sbuf.st_rdev);
2718 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2723 type = drmGetMinorType(min);
2729 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2731 struct drm_prime_handle args;
2736 args.handle = handle;
2738 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2742 *prime_fd = args.fd;
2746 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2748 struct drm_prime_handle args;
2753 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2757 *handle = args.handle;
2761 static char *drmGetMinorNameForFD(int fd, int type)
2765 struct dirent *pent, *ent;
2767 const char *name = drmGetMinorName(type);
2769 char dev_name[64], buf[64];
2778 if (fstat(fd, &sbuf))
2781 maj = major(sbuf.st_rdev);
2782 min = minor(sbuf.st_rdev);
2784 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2787 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2789 sysdir = opendir(buf);
2793 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2797 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2801 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2802 if (strncmp(ent->d_name, name, len) == 0) {
2803 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2809 return strdup(dev_name);
2818 #warning "Missing implementation of drmGetMinorNameForFD"
2823 char *drmGetPrimaryDeviceNameFromFd(int fd)
2825 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2828 char *drmGetRenderDeviceNameFromFd(int fd)
2830 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);
2833 static int drmParseSubsystemType(int maj, int min)
2836 char path[PATH_MAX + 1];
2837 char link[PATH_MAX + 1] = "";
2840 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/subsystem",
2843 if (readlink(path, link, PATH_MAX) < 0)
2846 name = strrchr(link, '/');
2850 if (strncmp(name, "/pci", 4) == 0)
2855 #warning "Missing implementation of drmParseSubsystemType"
2860 static int drmParsePciBusInfo(int maj, int min, drmPciBusInfoPtr info)
2863 char path[PATH_MAX + 1];
2866 int domain, bus, dev, func;
2869 snprintf(path, PATH_MAX, "/sys/dev/char/%d:%d/device/uevent", maj, min);
2870 fd = open(path, O_RDONLY);
2874 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 /* length 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 = ALIGN(drmGetMaxNodeName(), sizeof(void *));
3005 *device = calloc(1, sizeof(drmDevice) +
3006 (DRM_NODE_MAX * (sizeof(void *) + max_node_str)) +
3007 sizeof(drmPciBusInfo) +
3008 sizeof(drmPciDeviceInfo));
3012 addr = (char*)*device;
3014 (*device)->bustype = DRM_BUS_PCI;
3015 (*device)->available_nodes = 1 << node_type;
3017 addr += sizeof(drmDevice);
3018 (*device)->nodes = (char**)addr;
3020 addr += DRM_NODE_MAX * sizeof(void *);
3021 for (i = 0; i < DRM_NODE_MAX; i++) {
3022 (*device)->nodes[i] = addr;
3023 addr += max_node_str;
3025 memcpy((*device)->nodes[node_type], node, max_node_str);
3027 (*device)->businfo.pci = (drmPciBusInfoPtr)addr;
3029 ret = drmParsePciBusInfo(maj, min, (*device)->businfo.pci);
3033 // Fetch the device info if the user has requested it
3034 if (fetch_deviceinfo) {
3035 addr += sizeof(drmPciBusInfo);
3036 (*device)->deviceinfo.pci = (drmPciDeviceInfoPtr)addr;
3038 ret = drmParsePciDeviceInfo(d_name, (*device)->deviceinfo.pci);
3050 static void drmFoldDuplicatedDevices(drmDevicePtr local_devices[], int count)
3052 int node_type, i, j;
3054 for (i = 0; i < count; i++) {
3055 for (j = i + 1; j < count; j++) {
3056 if (drmCompareBusInfo(local_devices[i], local_devices[j]) == 0) {
3057 local_devices[i]->available_nodes |= local_devices[j]->available_nodes;
3058 node_type = log2(local_devices[j]->available_nodes);
3059 memcpy(local_devices[i]->nodes[node_type],
3060 local_devices[j]->nodes[node_type], drmGetMaxNodeName());
3061 drmFreeDevice(&local_devices[j]);
3068 * Get information about the opened drm device
3070 * \param fd file descriptor of the drm device
3071 * \param device the address of a drmDevicePtr where the information
3072 * will be allocated in stored
3074 * \return zero on success, negative error code otherwise.
3076 int drmGetDevice(int fd, drmDevicePtr *device)
3078 drmDevicePtr *local_devices;
3081 struct dirent *dent;
3083 char node[PATH_MAX + 1];
3084 int node_type, subsystem_type;
3086 int ret, i, node_count;
3089 if (fd == -1 || device == NULL)
3092 if (fstat(fd, &sbuf))
3095 maj = major(sbuf.st_rdev);
3096 min = minor(sbuf.st_rdev);
3098 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3101 subsystem_type = drmParseSubsystemType(maj, min);
3103 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3104 if (local_devices == NULL)
3107 sysdir = opendir(DRM_DIR_NAME);
3114 while ((dent = readdir(sysdir))) {
3115 node_type = drmGetNodeType(dent->d_name);
3119 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3120 if (stat(node, &sbuf))
3123 maj = major(sbuf.st_rdev);
3124 min = minor(sbuf.st_rdev);
3126 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3129 if (drmParseSubsystemType(maj, min) != subsystem_type)
3132 switch (subsystem_type) {
3134 ret = drmProcessPciDevice(&d, dent->d_name, node, node_type,
3141 fprintf(stderr, "The subsystem type is not supported yet\n");
3145 if (i >= max_count) {
3149 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3152 local_devices = temp;
3155 local_devices[i] = d;
3160 /* Fold nodes into a single device if they share the same bus info */
3161 drmFoldDuplicatedDevices(local_devices, node_count);
3163 *device = local_devices[0];
3164 for (i = 1; i < node_count && local_devices[i]; i++)
3165 drmFreeDevice(&local_devices[i]);
3168 free(local_devices);
3172 drmFreeDevices(local_devices, i);
3176 free(local_devices);
3181 * Get drm devices on the system
3183 * \param devices the array of devices with drmDevicePtr elements
3184 * can be NULL to get the device number first
3185 * \param max_devices the maximum number of devices for the array
3187 * \return on error - negative error code,
3188 * if devices is NULL - total number of devices available on the system,
3189 * alternatively the number of devices stored in devices[], which is
3190 * capped by the max_devices.
3192 int drmGetDevices(drmDevicePtr devices[], int max_devices)
3194 drmDevicePtr *local_devices;
3195 drmDevicePtr device;
3197 struct dirent *dent;
3199 char node[PATH_MAX + 1];
3200 int node_type, subsystem_type;
3202 int ret, i, node_count, device_count;
3205 local_devices = calloc(max_count, sizeof(drmDevicePtr));
3206 if (local_devices == NULL)
3209 sysdir = opendir(DRM_DIR_NAME);
3216 while ((dent = readdir(sysdir))) {
3217 node_type = drmGetNodeType(dent->d_name);
3221 snprintf(node, PATH_MAX, "%s/%s", DRM_DIR_NAME, dent->d_name);
3222 if (stat(node, &sbuf))
3225 maj = major(sbuf.st_rdev);
3226 min = minor(sbuf.st_rdev);
3228 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
3231 subsystem_type = drmParseSubsystemType(maj, min);
3233 if (subsystem_type < 0)
3236 switch (subsystem_type) {
3238 ret = drmProcessPciDevice(&device, dent->d_name, node, node_type,
3239 maj, min, devices != NULL);
3245 fprintf(stderr, "The subsystem type is not supported yet\n");
3249 if (i >= max_count) {
3253 temp = realloc(local_devices, max_count * sizeof(drmDevicePtr));
3256 local_devices = temp;
3259 local_devices[i] = device;
3264 /* Fold nodes into a single device if they share the same bus info */
3265 drmFoldDuplicatedDevices(local_devices, node_count);
3268 for (i = 0; i < node_count; i++) {
3269 if (!local_devices[i])
3272 if ((devices != NULL) && (device_count < max_devices))
3273 devices[device_count] = local_devices[i];
3275 drmFreeDevice(&local_devices[i]);
3281 free(local_devices);
3282 return device_count;
3285 drmFreeDevices(local_devices, i);
3289 free(local_devices);