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.
49 #include <sys/types.h>
51 #define stat_t struct stat
52 #include <sys/ioctl.h>
55 #ifdef HAVE_SYS_MKDEV_H
56 # include <sys/mkdev.h> /* defines major(), minor(), and makedev() on Solaris */
59 /* Not all systems have MAP_FAILED defined */
61 #define MAP_FAILED ((void *)-1)
67 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
80 #define DRM_MAJOR 226 /* Linux */
84 * This definition needs to be changed on some systems if dev_t is a structure.
85 * If there is a header file we can get it from, there would be best.
88 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
91 #define DRM_MSG_VERBOSITY 3
93 #define memclear(s) memset(&s, 0, sizeof(s))
95 static drmServerInfoPtr drm_server_info;
97 void drmSetServerInfo(drmServerInfoPtr info)
99 drm_server_info = info;
103 * Output a message to stderr.
105 * \param format printf() like format string.
108 * This function is a wrapper around vfprintf().
111 static int DRM_PRINTFLIKE(1, 0)
112 drmDebugPrint(const char *format, va_list ap)
114 return vfprintf(stderr, format, ap);
117 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
120 static debug_msg_func_t drm_debug_print = drmDebugPrint;
123 drmMsg(const char *format, ...)
127 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
129 va_start(ap, format);
130 if (drm_server_info) {
131 drm_server_info->debug_print(format,ap);
133 drm_debug_print(format, ap);
140 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
142 drm_debug_print = debug_msg_ptr;
145 static void *drmHashTable = NULL; /* Context switch callbacks */
147 void *drmGetHashTable(void)
152 void *drmMalloc(int size)
155 if ((pt = malloc(size)))
160 void drmFree(void *pt)
167 * Call ioctl, restarting if it is interupted
170 drmIoctl(int fd, unsigned long request, void *arg)
175 ret = ioctl(fd, request, arg);
176 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
180 static unsigned long drmGetKeyFromFd(int fd)
189 drmHashEntry *drmGetEntry(int fd)
191 unsigned long key = drmGetKeyFromFd(fd);
196 drmHashTable = drmHashCreate();
198 if (drmHashLookup(drmHashTable, key, &value)) {
199 entry = drmMalloc(sizeof(*entry));
202 entry->tagTable = drmHashCreate();
203 drmHashInsert(drmHashTable, key, entry);
211 * Compare two busid strings
216 * \return 1 if matched.
219 * This function compares two bus ID strings. It understands the older
220 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
221 * domain, b is bus, d is device, f is function.
223 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
225 /* First, check if the IDs are exactly the same */
226 if (strcasecmp(id1, id2) == 0)
229 /* Try to match old/new-style PCI bus IDs. */
230 if (strncasecmp(id1, "pci", 3) == 0) {
231 unsigned int o1, b1, d1, f1;
232 unsigned int o2, b2, d2, f2;
235 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
238 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
243 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
246 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
251 /* If domains aren't properly supported by the kernel interface,
252 * just ignore them, which sucks less than picking a totally random
253 * card with "open by name"
258 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
267 * Handles error checking for chown call.
269 * \param path to file.
270 * \param id of the new owner.
271 * \param id of the new group.
273 * \return zero if success or -1 if failure.
276 * Checks for failure. If failure was caused by signal call chown again.
277 * If any other failure happened then it will output error mesage using
280 static int chown_check_return(const char *path, uid_t owner, gid_t group)
285 rv = chown(path, owner, group);
286 } while (rv != 0 && errno == EINTR);
291 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
292 path, errno, strerror(errno));
297 * Open the DRM device, creating it if necessary.
299 * \param dev major and minor numbers of the device.
300 * \param minor minor number of the device.
302 * \return a file descriptor on success, or a negative value on error.
305 * Assembles the device name from \p minor and opens it, creating the device
306 * special file node with the major and minor numbers specified by \p dev and
307 * parent directory if necessary and was called by root.
309 static int drmOpenDevice(dev_t dev, int minor, int type)
312 const char *dev_name;
315 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, serv_group;
321 case DRM_NODE_PRIMARY:
322 dev_name = DRM_DEV_NAME;
324 case DRM_NODE_CONTROL:
325 dev_name = DRM_CONTROL_DEV_NAME;
327 case DRM_NODE_RENDER:
328 dev_name = DRM_RENDER_DEV_NAME;
334 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
335 drmMsg("drmOpenDevice: node name is %s\n", buf);
337 if (drm_server_info) {
338 drm_server_info->get_perms(&serv_group, &serv_mode);
339 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
340 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
341 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
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) {
362 chown_check_return(buf, user, group);
366 /* if we modprobed then wait for udev */
370 if (stat(DRM_DIR_NAME, &st)) {
374 if (udev_count == 50)
379 if (stat(buf, &st)) {
383 if (udev_count == 50)
390 fd = open(buf, O_RDWR, 0);
391 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
392 fd, fd < 0 ? strerror(errno) : "OK");
397 /* Check if the device node is not what we expect it to be, and recreate it
398 * and try again if so.
400 if (st.st_rdev != dev) {
402 return DRM_ERR_NOT_ROOT;
404 mknod(buf, S_IFCHR | devmode, dev);
405 if (drm_server_info) {
406 chown_check_return(buf, user, group);
410 fd = open(buf, O_RDWR, 0);
411 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
412 fd, fd < 0 ? strerror(errno) : "OK");
416 drmMsg("drmOpenDevice: Open failed\n");
424 * Open the DRM device
426 * \param minor device minor number.
427 * \param create allow to create the device if set.
429 * \return a file descriptor on success, or a negative value on error.
432 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
433 * name from \p minor and opens it.
435 static int drmOpenMinor(int minor, int create, int type)
439 const char *dev_name;
442 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
445 case DRM_NODE_PRIMARY:
446 dev_name = DRM_DEV_NAME;
448 case DRM_NODE_CONTROL:
449 dev_name = DRM_CONTROL_DEV_NAME;
451 case DRM_NODE_RENDER:
452 dev_name = DRM_RENDER_DEV_NAME;
458 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
459 if ((fd = open(buf, O_RDWR, 0)) >= 0)
466 * Determine whether the DRM kernel driver has been loaded.
468 * \return 1 if the DRM driver is loaded, 0 otherwise.
471 * Determine the presence of the kernel driver by attempting to open the 0
472 * minor and get version information. For backward compatibility with older
473 * Linux implementations, /proc/dri is also checked.
475 int drmAvailable(void)
477 drmVersionPtr version;
481 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
483 /* Try proc for backward Linux compatibility */
484 if (!access("/proc/dri/0", R_OK))
490 if ((version = drmGetVersion(fd))) {
492 drmFreeVersion(version);
499 static int drmGetMinorBase(int type)
502 case DRM_NODE_PRIMARY:
504 case DRM_NODE_CONTROL:
506 case DRM_NODE_RENDER:
513 static int drmGetMinorType(int minor)
515 int type = minor >> 6;
521 case DRM_NODE_PRIMARY:
522 case DRM_NODE_CONTROL:
523 case DRM_NODE_RENDER:
530 static const char *drmGetMinorName(int type)
533 case DRM_NODE_PRIMARY:
535 case DRM_NODE_CONTROL:
537 case DRM_NODE_RENDER:
545 * Open the device by bus ID.
547 * \param busid bus ID.
548 * \param type device node type.
550 * \return a file descriptor on success, or a negative value on error.
553 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
554 * comparing the device bus ID with the one supplied.
556 * \sa drmOpenMinor() and drmGetBusid().
558 static int drmOpenByBusid(const char *busid, int type)
560 int i, pci_domain_ok = 1;
564 int base = drmGetMinorBase(type);
569 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
570 for (i = base; i < base + DRM_MAX_MINOR; i++) {
571 fd = drmOpenMinor(i, 1, type);
572 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
574 /* We need to try for 1.4 first for proper PCI domain support
575 * and if that fails, we know the kernel is busted
579 sv.drm_dd_major = -1; /* Don't care */
580 sv.drm_dd_minor = -1; /* Don't care */
581 if (drmSetInterfaceVersion(fd, &sv)) {
587 sv.drm_dd_major = -1; /* Don't care */
588 sv.drm_dd_minor = -1; /* Don't care */
589 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
590 drmSetInterfaceVersion(fd, &sv);
592 buf = drmGetBusid(fd);
593 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
594 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
608 * Open the device by name.
610 * \param name driver name.
611 * \param type the device node type.
613 * \return a file descriptor on success, or a negative value on error.
616 * This function opens the first minor number that matches the driver name and
617 * isn't already in use. If it's in use it then it will already have a bus ID
620 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
622 static int drmOpenByName(const char *name, int type)
626 drmVersionPtr version;
628 int base = drmGetMinorBase(type);
634 * Open the first minor number that matches the driver name and isn't
635 * already in use. If it's in use it will have a busid assigned already.
637 for (i = base; i < base + DRM_MAX_MINOR; i++) {
638 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
639 if ((version = drmGetVersion(fd))) {
640 if (!strcmp(version->name, name)) {
641 drmFreeVersion(version);
642 id = drmGetBusid(fd);
643 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
652 drmFreeVersion(version);
660 /* Backward-compatibility /proc support */
661 for (i = 0; i < 8; i++) {
662 char proc_name[64], buf[512];
663 char *driver, *pt, *devstring;
666 sprintf(proc_name, "/proc/dri/%d/name", i);
667 if ((fd = open(proc_name, 0, 0)) >= 0) {
668 retcode = read(fd, buf, sizeof(buf)-1);
671 buf[retcode-1] = '\0';
672 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
674 if (*pt) { /* Device is next */
676 if (!strcmp(driver, name)) { /* Match */
677 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
679 if (*pt) { /* Found busid */
680 return drmOpenByBusid(++pt, type);
681 } else { /* No busid */
682 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
696 * Open the DRM device.
698 * Looks up the specified name and bus ID, and opens the device found. The
699 * entry in /dev/dri is created if necessary and if called by root.
701 * \param name driver name. Not referenced if bus ID is supplied.
702 * \param busid bus ID. Zero if not known.
704 * \return a file descriptor on success, or a negative value on error.
707 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
710 int drmOpen(const char *name, const char *busid)
712 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
716 * Open the DRM device with specified type.
718 * Looks up the specified name and bus ID, and opens the device found. The
719 * entry in /dev/dri is created if necessary and if called by root.
721 * \param name driver name. Not referenced if bus ID is supplied.
722 * \param busid bus ID. Zero if not known.
723 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
725 * \return a file descriptor on success, or a negative value on error.
728 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
731 int drmOpenWithType(const char *name, const char *busid, int type)
733 if (!drmAvailable() && name != NULL && drm_server_info) {
734 /* try to load the kernel module */
735 if (!drm_server_info->load_module(name)) {
736 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
742 int fd = drmOpenByBusid(busid, type);
748 return drmOpenByName(name, type);
753 int drmOpenControl(int minor)
755 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
758 int drmOpenRender(int minor)
760 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
764 * Free the version information returned by drmGetVersion().
766 * \param v pointer to the version information.
769 * It frees the memory pointed by \p %v as well as all the non-null strings
772 void drmFreeVersion(drmVersionPtr v)
784 * Free the non-public version information returned by the kernel.
786 * \param v pointer to the version information.
789 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
790 * the non-null strings pointers in it.
792 static void drmFreeKernelVersion(drm_version_t *v)
804 * Copy version information.
806 * \param d destination pointer.
807 * \param s source pointer.
810 * Used by drmGetVersion() to translate the information returned by the ioctl
811 * interface in a private structure into the public structure counterpart.
813 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
815 d->version_major = s->version_major;
816 d->version_minor = s->version_minor;
817 d->version_patchlevel = s->version_patchlevel;
818 d->name_len = s->name_len;
819 d->name = strdup(s->name);
820 d->date_len = s->date_len;
821 d->date = strdup(s->date);
822 d->desc_len = s->desc_len;
823 d->desc = strdup(s->desc);
828 * Query the driver version information.
830 * \param fd file descriptor.
832 * \return pointer to a drmVersion structure which should be freed with
835 * \note Similar information is available via /proc/dri.
838 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
839 * first with zeros to get the string lengths, and then the actually strings.
840 * It also null-terminates them since they might not be already.
842 drmVersionPtr drmGetVersion(int fd)
844 drmVersionPtr retval;
845 drm_version_t *version = drmMalloc(sizeof(*version));
849 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
850 drmFreeKernelVersion(version);
854 if (version->name_len)
855 version->name = drmMalloc(version->name_len + 1);
856 if (version->date_len)
857 version->date = drmMalloc(version->date_len + 1);
858 if (version->desc_len)
859 version->desc = drmMalloc(version->desc_len + 1);
861 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
862 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
863 drmFreeKernelVersion(version);
867 /* The results might not be null-terminated strings, so terminate them. */
868 if (version->name_len) version->name[version->name_len] = '\0';
869 if (version->date_len) version->date[version->date_len] = '\0';
870 if (version->desc_len) version->desc[version->desc_len] = '\0';
872 retval = drmMalloc(sizeof(*retval));
873 drmCopyVersion(retval, version);
874 drmFreeKernelVersion(version);
880 * Get version information for the DRM user space library.
882 * This version number is driver independent.
884 * \param fd file descriptor.
886 * \return version information.
889 * This function allocates and fills a drm_version structure with a hard coded
892 drmVersionPtr drmGetLibVersion(int fd)
894 drm_version_t *version = drmMalloc(sizeof(*version));
897 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
898 * revision 1.0.x = original DRM interface with no drmGetLibVersion
899 * entry point and many drm<Device> extensions
900 * revision 1.1.x = added drmCommand entry points for device extensions
901 * added drmGetLibVersion to identify libdrm.a version
902 * revision 1.2.x = added drmSetInterfaceVersion
903 * modified drmOpen to handle both busid and name
904 * revision 1.3.x = added server + memory manager
906 version->version_major = 1;
907 version->version_minor = 3;
908 version->version_patchlevel = 0;
910 return (drmVersionPtr)version;
913 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
915 struct drm_get_cap cap;
919 cap.capability = capability;
921 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
929 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
931 struct drm_set_client_cap cap;
934 cap.capability = capability;
937 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
941 * Free the bus ID information.
943 * \param busid bus ID information string as given by drmGetBusid().
946 * This function is just frees the memory pointed by \p busid.
948 void drmFreeBusid(const char *busid)
950 drmFree((void *)busid);
955 * Get the bus ID of the device.
957 * \param fd file descriptor.
959 * \return bus ID string.
962 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
963 * get the string length and data, passing the arguments in a drm_unique
966 char *drmGetBusid(int fd)
972 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
974 u.unique = drmMalloc(u.unique_len + 1);
975 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
977 u.unique[u.unique_len] = '\0';
984 * Set the bus ID of the device.
986 * \param fd file descriptor.
987 * \param busid bus ID string.
989 * \return zero on success, negative on failure.
992 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
993 * the arguments in a drm_unique structure.
995 int drmSetBusid(int fd, const char *busid)
1000 u.unique = (char *)busid;
1001 u.unique_len = strlen(busid);
1003 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1009 int drmGetMagic(int fd, drm_magic_t * magic)
1016 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1018 *magic = auth.magic;
1022 int drmAuthMagic(int fd, drm_magic_t magic)
1028 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1034 * Specifies a range of memory that is available for mapping by a
1037 * \param fd file descriptor.
1038 * \param offset usually the physical address. The actual meaning depends of
1039 * the \p type parameter. See below.
1040 * \param size of the memory in bytes.
1041 * \param type type of the memory to be mapped.
1042 * \param flags combination of several flags to modify the function actions.
1043 * \param handle will be set to a value that may be used as the offset
1044 * parameter for mmap().
1046 * \return zero on success or a negative value on error.
1048 * \par Mapping the frame buffer
1049 * For the frame buffer
1050 * - \p offset will be the physical address of the start of the frame buffer,
1051 * - \p size will be the size of the frame buffer in bytes, and
1052 * - \p type will be DRM_FRAME_BUFFER.
1055 * The area mapped will be uncached. If MTRR support is available in the
1056 * kernel, the frame buffer area will be set to write combining.
1058 * \par Mapping the MMIO register area
1059 * For the MMIO register area,
1060 * - \p offset will be the physical address of the start of the register area,
1061 * - \p size will be the size of the register area bytes, and
1062 * - \p type will be DRM_REGISTERS.
1064 * The area mapped will be uncached.
1066 * \par Mapping the SAREA
1068 * - \p offset will be ignored and should be set to zero,
1069 * - \p size will be the desired size of the SAREA in bytes,
1070 * - \p type will be DRM_SHM.
1073 * A shared memory area of the requested size will be created and locked in
1074 * kernel memory. This area may be mapped into client-space by using the handle
1077 * \note May only be called by root.
1080 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1081 * the arguments in a drm_map structure.
1083 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1084 drmMapFlags flags, drm_handle_t *handle)
1089 map.offset = offset;
1093 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1096 *handle = (drm_handle_t)(uintptr_t)map.handle;
1100 int drmRmMap(int fd, drm_handle_t handle)
1105 map.handle = (void *)(uintptr_t)handle;
1107 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1113 * Make buffers available for DMA transfers.
1115 * \param fd file descriptor.
1116 * \param count number of buffers.
1117 * \param size size of each buffer.
1118 * \param flags buffer allocation flags.
1119 * \param agp_offset offset in the AGP aperture
1121 * \return number of buffers allocated, negative on error.
1124 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1128 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1131 drm_buf_desc_t request;
1134 request.count = count;
1135 request.size = size;
1136 request.flags = flags;
1137 request.agp_start = agp_offset;
1139 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1141 return request.count;
1144 int drmMarkBufs(int fd, double low, double high)
1146 drm_buf_info_t info;
1151 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1157 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1160 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1161 int retval = -errno;
1166 for (i = 0; i < info.count; i++) {
1167 info.list[i].low_mark = low * info.list[i].count;
1168 info.list[i].high_mark = high * info.list[i].count;
1169 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1170 int retval = -errno;
1183 * \param fd file descriptor.
1184 * \param count number of buffers to free.
1185 * \param list list of buffers to be freed.
1187 * \return zero on success, or a negative value on failure.
1189 * \note This function is primarily used for debugging.
1192 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1193 * the arguments in a drm_buf_free structure.
1195 int drmFreeBufs(int fd, int count, int *list)
1197 drm_buf_free_t request;
1200 request.count = count;
1201 request.list = list;
1202 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1211 * \param fd file descriptor.
1214 * This function closes the file descriptor.
1216 int drmClose(int fd)
1218 unsigned long key = drmGetKeyFromFd(fd);
1219 drmHashEntry *entry = drmGetEntry(fd);
1221 drmHashDestroy(entry->tagTable);
1224 entry->tagTable = NULL;
1226 drmHashDelete(drmHashTable, key);
1234 * Map a region of memory.
1236 * \param fd file descriptor.
1237 * \param handle handle returned by drmAddMap().
1238 * \param size size in bytes. Must match the size used by drmAddMap().
1239 * \param address will contain the user-space virtual address where the mapping
1242 * \return zero on success, or a negative value on failure.
1245 * This function is a wrapper for mmap().
1247 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1249 static unsigned long pagesize_mask = 0;
1255 pagesize_mask = getpagesize() - 1;
1257 size = (size + pagesize_mask) & ~pagesize_mask;
1259 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1260 if (*address == MAP_FAILED)
1267 * Unmap mappings obtained with drmMap().
1269 * \param address address as given by drmMap().
1270 * \param size size in bytes. Must match the size used by drmMap().
1272 * \return zero on success, or a negative value on failure.
1275 * This function is a wrapper for munmap().
1277 int drmUnmap(drmAddress address, drmSize size)
1279 return drm_munmap(address, size);
1282 drmBufInfoPtr drmGetBufInfo(int fd)
1284 drm_buf_info_t info;
1285 drmBufInfoPtr retval;
1290 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1294 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1297 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1302 retval = drmMalloc(sizeof(*retval));
1303 retval->count = info.count;
1304 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1305 for (i = 0; i < info.count; i++) {
1306 retval->list[i].count = info.list[i].count;
1307 retval->list[i].size = info.list[i].size;
1308 retval->list[i].low_mark = info.list[i].low_mark;
1309 retval->list[i].high_mark = info.list[i].high_mark;
1318 * Map all DMA buffers into client-virtual space.
1320 * \param fd file descriptor.
1322 * \return a pointer to a ::drmBufMap structure.
1324 * \note The client may not use these buffers until obtaining buffer indices
1328 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1329 * information about the buffers in a drm_buf_map structure into the
1330 * client-visible data structures.
1332 drmBufMapPtr drmMapBufs(int fd)
1335 drmBufMapPtr retval;
1339 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1345 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1348 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1353 retval = drmMalloc(sizeof(*retval));
1354 retval->count = bufs.count;
1355 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1356 for (i = 0; i < bufs.count; i++) {
1357 retval->list[i].idx = bufs.list[i].idx;
1358 retval->list[i].total = bufs.list[i].total;
1359 retval->list[i].used = 0;
1360 retval->list[i].address = bufs.list[i].address;
1370 * Unmap buffers allocated with drmMapBufs().
1372 * \return zero on success, or negative value on failure.
1375 * Calls munmap() for every buffer stored in \p bufs and frees the
1376 * memory allocated by drmMapBufs().
1378 int drmUnmapBufs(drmBufMapPtr bufs)
1382 for (i = 0; i < bufs->count; i++) {
1383 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1386 drmFree(bufs->list);
1393 #define DRM_DMA_RETRY 16
1396 * Reserve DMA buffers.
1398 * \param fd file descriptor.
1401 * \return zero on success, or a negative value on failure.
1404 * Assemble the arguments into a drm_dma structure and keeps issuing the
1405 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1407 int drmDMA(int fd, drmDMAReqPtr request)
1412 dma.context = request->context;
1413 dma.send_count = request->send_count;
1414 dma.send_indices = request->send_list;
1415 dma.send_sizes = request->send_sizes;
1416 dma.flags = request->flags;
1417 dma.request_count = request->request_count;
1418 dma.request_size = request->request_size;
1419 dma.request_indices = request->request_list;
1420 dma.request_sizes = request->request_sizes;
1421 dma.granted_count = 0;
1424 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1425 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1428 request->granted_count = dma.granted_count;
1437 * Obtain heavyweight hardware lock.
1439 * \param fd file descriptor.
1440 * \param context context.
1441 * \param flags flags that determine the sate of the hardware when the function
1444 * \return always zero.
1447 * This function translates the arguments into a drm_lock structure and issue
1448 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1450 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1455 lock.context = context;
1457 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1458 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1459 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1460 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1461 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1462 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1464 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1470 * Release the hardware lock.
1472 * \param fd file descriptor.
1473 * \param context context.
1475 * \return zero on success, or a negative value on failure.
1478 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1479 * argument in a drm_lock structure.
1481 int drmUnlock(int fd, drm_context_t context)
1486 lock.context = context;
1487 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1490 drm_context_t *drmGetReservedContextList(int fd, int *count)
1494 drm_context_t * retval;
1498 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1504 if (!(list = drmMalloc(res.count * sizeof(*list))))
1506 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1511 res.contexts = list;
1512 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1515 for (i = 0; i < res.count; i++)
1516 retval[i] = list[i].handle;
1523 void drmFreeReservedContextList(drm_context_t *pt)
1531 * Used by the X server during GLXContext initialization. This causes
1532 * per-context kernel-level resources to be allocated.
1534 * \param fd file descriptor.
1535 * \param handle is set on success. To be used by the client when requesting DMA
1536 * dispatch with drmDMA().
1538 * \return zero on success, or a negative value on failure.
1540 * \note May only be called by root.
1543 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1544 * argument in a drm_ctx structure.
1546 int drmCreateContext(int fd, drm_context_t *handle)
1551 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1553 *handle = ctx.handle;
1557 int drmSwitchToContext(int fd, drm_context_t context)
1562 ctx.handle = context;
1563 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1568 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1573 * Context preserving means that no context switches are done between DMA
1574 * buffers from one context and the next. This is suitable for use in the
1575 * X server (which promises to maintain hardware context), or in the
1576 * client-side library when buffers are swapped on behalf of two threads.
1579 ctx.handle = context;
1580 if (flags & DRM_CONTEXT_PRESERVED)
1581 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1582 if (flags & DRM_CONTEXT_2DONLY)
1583 ctx.flags |= _DRM_CONTEXT_2DONLY;
1584 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1589 int drmGetContextFlags(int fd, drm_context_t context,
1590 drm_context_tFlagsPtr flags)
1595 ctx.handle = context;
1596 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1599 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1600 *flags |= DRM_CONTEXT_PRESERVED;
1601 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1602 *flags |= DRM_CONTEXT_2DONLY;
1609 * Free any kernel-level resources allocated with drmCreateContext() associated
1612 * \param fd file descriptor.
1613 * \param handle handle given by drmCreateContext().
1615 * \return zero on success, or a negative value on failure.
1617 * \note May only be called by root.
1620 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1621 * argument in a drm_ctx structure.
1623 int drmDestroyContext(int fd, drm_context_t handle)
1628 ctx.handle = handle;
1629 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1634 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1639 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1641 *handle = draw.handle;
1645 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1650 draw.handle = handle;
1651 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1656 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1657 drm_drawable_info_type_t type, unsigned int num,
1660 drm_update_draw_t update;
1663 update.handle = handle;
1666 update.data = (unsigned long long)(unsigned long)data;
1668 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1675 * Acquire the AGP device.
1677 * Must be called before any of the other AGP related calls.
1679 * \param fd file descriptor.
1681 * \return zero on success, or a negative value on failure.
1684 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1686 int drmAgpAcquire(int fd)
1688 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1695 * Release the AGP device.
1697 * \param fd file descriptor.
1699 * \return zero on success, or a negative value on failure.
1702 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1704 int drmAgpRelease(int fd)
1706 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1715 * \param fd file descriptor.
1716 * \param mode AGP mode.
1718 * \return zero on success, or a negative value on failure.
1721 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1722 * argument in a drm_agp_mode structure.
1724 int drmAgpEnable(int fd, unsigned long mode)
1730 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1737 * Allocate a chunk of AGP memory.
1739 * \param fd file descriptor.
1740 * \param size requested memory size in bytes. Will be rounded to page boundary.
1741 * \param type type of memory to allocate.
1742 * \param address if not zero, will be set to the physical address of the
1744 * \param handle on success will be set to a handle of the allocated memory.
1746 * \return zero on success, or a negative value on failure.
1749 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1750 * arguments in a drm_agp_buffer structure.
1752 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1753 unsigned long *address, drm_handle_t *handle)
1758 *handle = DRM_AGP_NO_HANDLE;
1761 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1764 *address = b.physical;
1771 * Free a chunk of AGP memory.
1773 * \param fd file descriptor.
1774 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1776 * \return zero on success, or a negative value on failure.
1779 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1780 * argument in a drm_agp_buffer structure.
1782 int drmAgpFree(int fd, drm_handle_t handle)
1788 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1795 * Bind a chunk of AGP memory.
1797 * \param fd file descriptor.
1798 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1799 * \param offset offset in bytes. It will round to page boundary.
1801 * \return zero on success, or a negative value on failure.
1804 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1805 * argument in a drm_agp_binding structure.
1807 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1809 drm_agp_binding_t b;
1814 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1821 * Unbind a chunk of AGP memory.
1823 * \param fd file descriptor.
1824 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1826 * \return zero on success, or a negative value on failure.
1829 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1830 * the argument in a drm_agp_binding structure.
1832 int drmAgpUnbind(int fd, drm_handle_t handle)
1834 drm_agp_binding_t b;
1838 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1845 * Get AGP driver major version number.
1847 * \param fd file descriptor.
1849 * \return major version number on success, or a negative value on failure..
1852 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1853 * necessary information in a drm_agp_info structure.
1855 int drmAgpVersionMajor(int fd)
1861 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1863 return i.agp_version_major;
1868 * Get AGP driver minor version number.
1870 * \param fd file descriptor.
1872 * \return minor version number on success, or a negative value on failure.
1875 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1876 * necessary information in a drm_agp_info structure.
1878 int drmAgpVersionMinor(int fd)
1884 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1886 return i.agp_version_minor;
1893 * \param fd file descriptor.
1895 * \return mode on success, or zero on failure.
1898 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1899 * necessary information in a drm_agp_info structure.
1901 unsigned long drmAgpGetMode(int fd)
1907 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1914 * Get AGP aperture base.
1916 * \param fd file descriptor.
1918 * \return aperture base on success, zero on failure.
1921 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1922 * necessary information in a drm_agp_info structure.
1924 unsigned long drmAgpBase(int fd)
1930 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1932 return i.aperture_base;
1937 * Get AGP aperture size.
1939 * \param fd file descriptor.
1941 * \return aperture size on success, zero on failure.
1944 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1945 * necessary information in a drm_agp_info structure.
1947 unsigned long drmAgpSize(int fd)
1953 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1955 return i.aperture_size;
1960 * Get used AGP memory.
1962 * \param fd file descriptor.
1964 * \return memory used on success, or zero on failure.
1967 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1968 * necessary information in a drm_agp_info structure.
1970 unsigned long drmAgpMemoryUsed(int fd)
1976 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1978 return i.memory_used;
1983 * Get available AGP memory.
1985 * \param fd file descriptor.
1987 * \return memory available on success, or zero on failure.
1990 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1991 * necessary information in a drm_agp_info structure.
1993 unsigned long drmAgpMemoryAvail(int fd)
1999 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2001 return i.memory_allowed;
2006 * Get hardware vendor ID.
2008 * \param fd file descriptor.
2010 * \return vendor ID on success, or zero on failure.
2013 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2014 * necessary information in a drm_agp_info structure.
2016 unsigned int drmAgpVendorId(int fd)
2022 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2029 * Get hardware device ID.
2031 * \param fd file descriptor.
2033 * \return zero on success, or zero on failure.
2036 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2037 * necessary information in a drm_agp_info structure.
2039 unsigned int drmAgpDeviceId(int fd)
2045 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2050 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2052 drm_scatter_gather_t sg;
2058 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2060 *handle = sg.handle;
2064 int drmScatterGatherFree(int fd, drm_handle_t handle)
2066 drm_scatter_gather_t sg;
2070 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2078 * \param fd file descriptor.
2079 * \param vbl pointer to a drmVBlank structure.
2081 * \return zero on success, or a negative value on failure.
2084 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2086 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2088 struct timespec timeout, cur;
2091 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2093 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2099 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2100 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2101 if (ret && errno == EINTR) {
2102 clock_gettime(CLOCK_MONOTONIC, &cur);
2103 /* Timeout after 1s */
2104 if (cur.tv_sec > timeout.tv_sec + 1 ||
2105 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2112 } while (ret && errno == EINTR);
2118 int drmError(int err, const char *label)
2121 case DRM_ERR_NO_DEVICE:
2122 fprintf(stderr, "%s: no device\n", label);
2124 case DRM_ERR_NO_ACCESS:
2125 fprintf(stderr, "%s: no access\n", label);
2127 case DRM_ERR_NOT_ROOT:
2128 fprintf(stderr, "%s: not root\n", label);
2130 case DRM_ERR_INVALID:
2131 fprintf(stderr, "%s: invalid args\n", label);
2136 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2144 * Install IRQ handler.
2146 * \param fd file descriptor.
2147 * \param irq IRQ number.
2149 * \return zero on success, or a negative value on failure.
2152 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2153 * argument in a drm_control structure.
2155 int drmCtlInstHandler(int fd, int irq)
2160 ctl.func = DRM_INST_HANDLER;
2162 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2169 * Uninstall IRQ handler.
2171 * \param fd file descriptor.
2173 * \return zero on success, or a negative value on failure.
2176 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2177 * argument in a drm_control structure.
2179 int drmCtlUninstHandler(int fd)
2184 ctl.func = DRM_UNINST_HANDLER;
2186 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2191 int drmFinish(int fd, int context, drmLockFlags flags)
2196 lock.context = context;
2197 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2198 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2199 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2200 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2201 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2202 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2203 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2209 * Get IRQ from bus ID.
2211 * \param fd file descriptor.
2212 * \param busnum bus number.
2213 * \param devnum device number.
2214 * \param funcnum function number.
2216 * \return IRQ number on success, or a negative value on failure.
2219 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2220 * arguments in a drm_irq_busid structure.
2222 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2229 p.funcnum = funcnum;
2230 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2235 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2237 drmHashEntry *entry = drmGetEntry(fd);
2239 if (drmHashInsert(entry->tagTable, context, tag)) {
2240 drmHashDelete(entry->tagTable, context);
2241 drmHashInsert(entry->tagTable, context, tag);
2246 int drmDelContextTag(int fd, drm_context_t context)
2248 drmHashEntry *entry = drmGetEntry(fd);
2250 return drmHashDelete(entry->tagTable, context);
2253 void *drmGetContextTag(int fd, drm_context_t context)
2255 drmHashEntry *entry = drmGetEntry(fd);
2258 if (drmHashLookup(entry->tagTable, context, &value))
2264 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2265 drm_handle_t handle)
2267 drm_ctx_priv_map_t map;
2270 map.ctx_id = ctx_id;
2271 map.handle = (void *)(uintptr_t)handle;
2273 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2278 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2279 drm_handle_t *handle)
2281 drm_ctx_priv_map_t map;
2284 map.ctx_id = ctx_id;
2286 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2289 *handle = (drm_handle_t)(uintptr_t)map.handle;
2294 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2295 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2302 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2304 *offset = map.offset;
2308 *handle = (unsigned long)map.handle;
2313 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2314 unsigned long *magic, unsigned long *iocs)
2316 drm_client_t client;
2320 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2322 *auth = client.auth;
2325 *magic = client.magic;
2326 *iocs = client.iocs;
2330 int drmGetStats(int fd, drmStatsT *stats)
2336 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2340 memset(stats, 0, sizeof(*stats));
2341 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2345 stats->data[i].long_format = "%-20.20s"; \
2346 stats->data[i].rate_format = "%8.8s"; \
2347 stats->data[i].isvalue = 1; \
2348 stats->data[i].verbose = 0
2351 stats->data[i].long_format = "%-20.20s"; \
2352 stats->data[i].rate_format = "%5.5s"; \
2353 stats->data[i].isvalue = 0; \
2354 stats->data[i].mult_names = "kgm"; \
2355 stats->data[i].mult = 1000; \
2356 stats->data[i].verbose = 0
2359 stats->data[i].long_format = "%-20.20s"; \
2360 stats->data[i].rate_format = "%5.5s"; \
2361 stats->data[i].isvalue = 0; \
2362 stats->data[i].mult_names = "KGM"; \
2363 stats->data[i].mult = 1024; \
2364 stats->data[i].verbose = 0
2367 stats->count = s.count;
2368 for (i = 0; i < s.count; i++) {
2369 stats->data[i].value = s.data[i].value;
2370 switch (s.data[i].type) {
2371 case _DRM_STAT_LOCK:
2372 stats->data[i].long_name = "Lock";
2373 stats->data[i].rate_name = "Lock";
2376 case _DRM_STAT_OPENS:
2377 stats->data[i].long_name = "Opens";
2378 stats->data[i].rate_name = "O";
2380 stats->data[i].verbose = 1;
2382 case _DRM_STAT_CLOSES:
2383 stats->data[i].long_name = "Closes";
2384 stats->data[i].rate_name = "Lock";
2386 stats->data[i].verbose = 1;
2388 case _DRM_STAT_IOCTLS:
2389 stats->data[i].long_name = "Ioctls";
2390 stats->data[i].rate_name = "Ioc/s";
2393 case _DRM_STAT_LOCKS:
2394 stats->data[i].long_name = "Locks";
2395 stats->data[i].rate_name = "Lck/s";
2398 case _DRM_STAT_UNLOCKS:
2399 stats->data[i].long_name = "Unlocks";
2400 stats->data[i].rate_name = "Unl/s";
2404 stats->data[i].long_name = "IRQs";
2405 stats->data[i].rate_name = "IRQ/s";
2408 case _DRM_STAT_PRIMARY:
2409 stats->data[i].long_name = "Primary Bytes";
2410 stats->data[i].rate_name = "PB/s";
2413 case _DRM_STAT_SECONDARY:
2414 stats->data[i].long_name = "Secondary Bytes";
2415 stats->data[i].rate_name = "SB/s";
2419 stats->data[i].long_name = "DMA";
2420 stats->data[i].rate_name = "DMA/s";
2423 case _DRM_STAT_SPECIAL:
2424 stats->data[i].long_name = "Special DMA";
2425 stats->data[i].rate_name = "dma/s";
2428 case _DRM_STAT_MISSED:
2429 stats->data[i].long_name = "Miss";
2430 stats->data[i].rate_name = "Ms/s";
2433 case _DRM_STAT_VALUE:
2434 stats->data[i].long_name = "Value";
2435 stats->data[i].rate_name = "Value";
2438 case _DRM_STAT_BYTE:
2439 stats->data[i].long_name = "Bytes";
2440 stats->data[i].rate_name = "B/s";
2443 case _DRM_STAT_COUNT:
2445 stats->data[i].long_name = "Count";
2446 stats->data[i].rate_name = "Cnt/s";
2455 * Issue a set-version ioctl.
2457 * \param fd file descriptor.
2458 * \param drmCommandIndex command index
2459 * \param data source pointer of the data to be read and written.
2460 * \param size size of the data to be read and written.
2462 * \return zero on success, or a negative value on failure.
2465 * It issues a read-write ioctl given by
2466 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2468 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2471 drm_set_version_t sv;
2474 sv.drm_di_major = version->drm_di_major;
2475 sv.drm_di_minor = version->drm_di_minor;
2476 sv.drm_dd_major = version->drm_dd_major;
2477 sv.drm_dd_minor = version->drm_dd_minor;
2479 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2483 version->drm_di_major = sv.drm_di_major;
2484 version->drm_di_minor = sv.drm_di_minor;
2485 version->drm_dd_major = sv.drm_dd_major;
2486 version->drm_dd_minor = sv.drm_dd_minor;
2492 * Send a device-specific command.
2494 * \param fd file descriptor.
2495 * \param drmCommandIndex command index
2497 * \return zero on success, or a negative value on failure.
2500 * It issues a ioctl given by
2501 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2503 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2505 unsigned long request;
2507 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2509 if (drmIoctl(fd, request, NULL)) {
2517 * Send a device-specific read command.
2519 * \param fd file descriptor.
2520 * \param drmCommandIndex command index
2521 * \param data destination pointer of the data to be read.
2522 * \param size size of the data to be read.
2524 * \return zero on success, or a negative value on failure.
2527 * It issues a read ioctl given by
2528 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2530 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2533 unsigned long request;
2535 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2536 DRM_COMMAND_BASE + drmCommandIndex, size);
2538 if (drmIoctl(fd, request, data)) {
2546 * Send a device-specific write command.
2548 * \param fd file descriptor.
2549 * \param drmCommandIndex command index
2550 * \param data source pointer of the data to be written.
2551 * \param size size of the data to be written.
2553 * \return zero on success, or a negative value on failure.
2556 * It issues a write ioctl given by
2557 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2559 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2562 unsigned long request;
2564 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2565 DRM_COMMAND_BASE + drmCommandIndex, size);
2567 if (drmIoctl(fd, request, data)) {
2575 * Send a device-specific read-write command.
2577 * \param fd file descriptor.
2578 * \param drmCommandIndex command index
2579 * \param data source pointer of the data to be read and written.
2580 * \param size size of the data to be read and written.
2582 * \return zero on success, or a negative value on failure.
2585 * It issues a read-write ioctl given by
2586 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2588 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2591 unsigned long request;
2593 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2594 DRM_COMMAND_BASE + drmCommandIndex, size);
2596 if (drmIoctl(fd, request, data))
2601 #define DRM_MAX_FDS 16
2607 } connection[DRM_MAX_FDS];
2609 static int nr_fds = 0;
2611 int drmOpenOnce(void *unused,
2615 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2618 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2623 for (i = 0; i < nr_fds; i++)
2624 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2625 (connection[i].type == type)) {
2626 connection[i].refcount++;
2628 return connection[i].fd;
2631 fd = drmOpenWithType(NULL, BusID, type);
2632 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2635 connection[nr_fds].BusID = strdup(BusID);
2636 connection[nr_fds].fd = fd;
2637 connection[nr_fds].refcount = 1;
2638 connection[nr_fds].type = type;
2642 fprintf(stderr, "saved connection %d for %s %d\n",
2643 nr_fds, connection[nr_fds].BusID,
2644 strcmp(BusID, connection[nr_fds].BusID));
2651 void drmCloseOnce(int fd)
2655 for (i = 0; i < nr_fds; i++) {
2656 if (fd == connection[i].fd) {
2657 if (--connection[i].refcount == 0) {
2658 drmClose(connection[i].fd);
2659 free(connection[i].BusID);
2662 connection[i] = connection[nr_fds];
2670 int drmSetMaster(int fd)
2672 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2675 int drmDropMaster(int fd)
2677 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2680 char *drmGetDeviceNameFromFd(int fd)
2687 /* The whole drmOpen thing is a fiasco and we need to find a way
2688 * back to just using open(2). For now, however, lets just make
2689 * things worse with even more ad hoc directory walking code to
2690 * discover the device file name. */
2695 for (i = 0; i < DRM_MAX_MINOR; i++) {
2696 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2697 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2700 if (i == DRM_MAX_MINOR)
2703 return strdup(name);
2706 int drmGetNodeTypeFromFd(int fd)
2711 if (fstat(fd, &sbuf))
2714 maj = major(sbuf.st_rdev);
2715 min = minor(sbuf.st_rdev);
2717 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2722 type = drmGetMinorType(min);
2728 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2730 struct drm_prime_handle args;
2733 args.handle = handle;
2735 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2739 *prime_fd = args.fd;
2743 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2745 struct drm_prime_handle args;
2750 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2754 *handle = args.handle;
2758 static char *drmGetMinorNameForFD(int fd, int type)
2762 struct dirent *pent, *ent;
2764 const char *name = drmGetMinorName(type);
2766 char dev_name[64], buf[64];
2775 if (fstat(fd, &sbuf))
2778 maj = major(sbuf.st_rdev);
2779 min = minor(sbuf.st_rdev);
2781 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2784 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2786 sysdir = opendir(buf);
2790 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2794 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2798 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2799 if (strncmp(ent->d_name, name, len) == 0) {
2803 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2805 return strdup(dev_name);
2817 char *drmGetPrimaryDeviceNameFromFd(int fd)
2819 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2822 char *drmGetRenderDeviceNameFromFd(int fd)
2824 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);