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);
118 drmMsg(const char *format, ...)
122 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
124 va_start(ap, format);
125 if (drm_server_info) {
126 drm_server_info->debug_print(format,ap);
128 drmDebugPrint(format, ap);
134 static void *drmHashTable = NULL; /* Context switch callbacks */
136 void *drmGetHashTable(void)
141 void *drmMalloc(int size)
144 if ((pt = malloc(size)))
149 void drmFree(void *pt)
156 * Call ioctl, restarting if it is interupted
159 drmIoctl(int fd, unsigned long request, void *arg)
164 ret = ioctl(fd, request, arg);
165 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
169 static unsigned long drmGetKeyFromFd(int fd)
178 drmHashEntry *drmGetEntry(int fd)
180 unsigned long key = drmGetKeyFromFd(fd);
185 drmHashTable = drmHashCreate();
187 if (drmHashLookup(drmHashTable, key, &value)) {
188 entry = drmMalloc(sizeof(*entry));
191 entry->tagTable = drmHashCreate();
192 drmHashInsert(drmHashTable, key, entry);
200 * Compare two busid strings
205 * \return 1 if matched.
208 * This function compares two bus ID strings. It understands the older
209 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
210 * domain, b is bus, d is device, f is function.
212 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
214 /* First, check if the IDs are exactly the same */
215 if (strcasecmp(id1, id2) == 0)
218 /* Try to match old/new-style PCI bus IDs. */
219 if (strncasecmp(id1, "pci", 3) == 0) {
220 unsigned int o1, b1, d1, f1;
221 unsigned int o2, b2, d2, f2;
224 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
227 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
232 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
235 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
240 /* If domains aren't properly supported by the kernel interface,
241 * just ignore them, which sucks less than picking a totally random
242 * card with "open by name"
247 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
256 * Handles error checking for chown call.
258 * \param path to file.
259 * \param id of the new owner.
260 * \param id of the new group.
262 * \return zero if success or -1 if failure.
265 * Checks for failure. If failure was caused by signal call chown again.
266 * If any other failure happened then it will output error mesage using
270 static int chown_check_return(const char *path, uid_t owner, gid_t group)
275 rv = chown(path, owner, group);
276 } while (rv != 0 && errno == EINTR);
281 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
282 path, errno, strerror(errno));
288 * Open the DRM device, creating it if necessary.
290 * \param dev major and minor numbers of the device.
291 * \param minor minor number of the device.
293 * \return a file descriptor on success, or a negative value on error.
296 * Assembles the device name from \p minor and opens it, creating the device
297 * special file node with the major and minor numbers specified by \p dev and
298 * parent directory if necessary and was called by root.
300 static int drmOpenDevice(dev_t dev, int minor, int type)
303 const char *dev_name;
306 mode_t devmode = DRM_DEV_MODE, serv_mode;
309 int isroot = !geteuid();
310 uid_t user = DRM_DEV_UID;
311 gid_t group = DRM_DEV_GID;
315 case DRM_NODE_PRIMARY:
316 dev_name = DRM_DEV_NAME;
318 case DRM_NODE_CONTROL:
319 dev_name = DRM_CONTROL_DEV_NAME;
321 case DRM_NODE_RENDER:
322 dev_name = DRM_RENDER_DEV_NAME;
328 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
329 drmMsg("drmOpenDevice: node name is %s\n", buf);
331 if (drm_server_info) {
332 drm_server_info->get_perms(&serv_group, &serv_mode);
333 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
334 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
338 if (stat(DRM_DIR_NAME, &st)) {
340 return DRM_ERR_NOT_ROOT;
341 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
342 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
343 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
346 /* Check if the device node exists and create it if necessary. */
347 if (stat(buf, &st)) {
349 return DRM_ERR_NOT_ROOT;
351 mknod(buf, S_IFCHR | devmode, dev);
354 if (drm_server_info) {
355 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
356 chown_check_return(buf, user, group);
360 /* if we modprobed then wait for udev */
364 if (stat(DRM_DIR_NAME, &st)) {
368 if (udev_count == 50)
373 if (stat(buf, &st)) {
377 if (udev_count == 50)
384 fd = open(buf, O_RDWR, 0);
385 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
386 fd, fd < 0 ? strerror(errno) : "OK");
391 /* Check if the device node is not what we expect it to be, and recreate it
392 * and try again if so.
394 if (st.st_rdev != dev) {
396 return DRM_ERR_NOT_ROOT;
398 mknod(buf, S_IFCHR | devmode, dev);
399 if (drm_server_info) {
400 chown_check_return(buf, user, group);
404 fd = open(buf, O_RDWR, 0);
405 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
406 fd, fd < 0 ? strerror(errno) : "OK");
410 drmMsg("drmOpenDevice: Open failed\n");
418 * Open the DRM device
420 * \param minor device minor number.
421 * \param create allow to create the device if set.
423 * \return a file descriptor on success, or a negative value on error.
426 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
427 * name from \p minor and opens it.
429 static int drmOpenMinor(int minor, int create, int type)
433 const char *dev_name;
436 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
439 case DRM_NODE_PRIMARY:
440 dev_name = DRM_DEV_NAME;
442 case DRM_NODE_CONTROL:
443 dev_name = DRM_CONTROL_DEV_NAME;
445 case DRM_NODE_RENDER:
446 dev_name = DRM_RENDER_DEV_NAME;
452 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
453 if ((fd = open(buf, O_RDWR, 0)) >= 0)
460 * Determine whether the DRM kernel driver has been loaded.
462 * \return 1 if the DRM driver is loaded, 0 otherwise.
465 * Determine the presence of the kernel driver by attempting to open the 0
466 * minor and get version information. For backward compatibility with older
467 * Linux implementations, /proc/dri is also checked.
469 int drmAvailable(void)
471 drmVersionPtr version;
475 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
477 /* Try proc for backward Linux compatibility */
478 if (!access("/proc/dri/0", R_OK))
484 if ((version = drmGetVersion(fd))) {
486 drmFreeVersion(version);
493 static int drmGetMinorBase(int type)
496 case DRM_NODE_PRIMARY:
498 case DRM_NODE_CONTROL:
500 case DRM_NODE_RENDER:
507 static int drmGetMinorType(int minor)
509 int type = minor >> 6;
515 case DRM_NODE_PRIMARY:
516 case DRM_NODE_CONTROL:
517 case DRM_NODE_RENDER:
524 static const char *drmGetMinorName(int type)
527 case DRM_NODE_PRIMARY:
529 case DRM_NODE_CONTROL:
531 case DRM_NODE_RENDER:
539 * Open the device by bus ID.
541 * \param busid bus ID.
542 * \param type device node type.
544 * \return a file descriptor on success, or a negative value on error.
547 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
548 * comparing the device bus ID with the one supplied.
550 * \sa drmOpenMinor() and drmGetBusid().
552 static int drmOpenByBusid(const char *busid, int type)
554 int i, pci_domain_ok = 1;
558 int base = drmGetMinorBase(type);
563 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
564 for (i = base; i < base + DRM_MAX_MINOR; i++) {
565 fd = drmOpenMinor(i, 1, type);
566 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
568 /* We need to try for 1.4 first for proper PCI domain support
569 * and if that fails, we know the kernel is busted
573 sv.drm_dd_major = -1; /* Don't care */
574 sv.drm_dd_minor = -1; /* Don't care */
575 if (drmSetInterfaceVersion(fd, &sv)) {
581 sv.drm_dd_major = -1; /* Don't care */
582 sv.drm_dd_minor = -1; /* Don't care */
583 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
584 drmSetInterfaceVersion(fd, &sv);
586 buf = drmGetBusid(fd);
587 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
588 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
602 * Open the device by name.
604 * \param name driver name.
605 * \param type the device node type.
607 * \return a file descriptor on success, or a negative value on error.
610 * This function opens the first minor number that matches the driver name and
611 * isn't already in use. If it's in use it then it will already have a bus ID
614 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
616 static int drmOpenByName(const char *name, int type)
620 drmVersionPtr version;
622 int base = drmGetMinorBase(type);
628 * Open the first minor number that matches the driver name and isn't
629 * already in use. If it's in use it will have a busid assigned already.
631 for (i = base; i < base + DRM_MAX_MINOR; i++) {
632 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
633 if ((version = drmGetVersion(fd))) {
634 if (!strcmp(version->name, name)) {
635 drmFreeVersion(version);
636 id = drmGetBusid(fd);
637 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
646 drmFreeVersion(version);
654 /* Backward-compatibility /proc support */
655 for (i = 0; i < 8; i++) {
656 char proc_name[64], buf[512];
657 char *driver, *pt, *devstring;
660 sprintf(proc_name, "/proc/dri/%d/name", i);
661 if ((fd = open(proc_name, 0, 0)) >= 0) {
662 retcode = read(fd, buf, sizeof(buf)-1);
665 buf[retcode-1] = '\0';
666 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
668 if (*pt) { /* Device is next */
670 if (!strcmp(driver, name)) { /* Match */
671 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
673 if (*pt) { /* Found busid */
674 return drmOpenByBusid(++pt, type);
675 } else { /* No busid */
676 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
690 * Open the DRM device.
692 * Looks up the specified name and bus ID, and opens the device found. The
693 * entry in /dev/dri is created if necessary and if called by root.
695 * \param name driver name. Not referenced if bus ID is supplied.
696 * \param busid bus ID. Zero if not known.
698 * \return a file descriptor on success, or a negative value on error.
701 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
704 int drmOpen(const char *name, const char *busid)
706 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
710 * Open the DRM device with specified type.
712 * Looks up the specified name and bus ID, and opens the device found. The
713 * entry in /dev/dri is created if necessary and if called by root.
715 * \param name driver name. Not referenced if bus ID is supplied.
716 * \param busid bus ID. Zero if not known.
717 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
719 * \return a file descriptor on success, or a negative value on error.
722 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
725 int drmOpenWithType(const char *name, const char *busid, int type)
727 if (!drmAvailable() && name != NULL && drm_server_info) {
728 /* try to load the kernel module */
729 if (!drm_server_info->load_module(name)) {
730 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
736 int fd = drmOpenByBusid(busid, type);
742 return drmOpenByName(name, type);
747 int drmOpenControl(int minor)
749 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
752 int drmOpenRender(int minor)
754 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
758 * Free the version information returned by drmGetVersion().
760 * \param v pointer to the version information.
763 * It frees the memory pointed by \p %v as well as all the non-null strings
766 void drmFreeVersion(drmVersionPtr v)
778 * Free the non-public version information returned by the kernel.
780 * \param v pointer to the version information.
783 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
784 * the non-null strings pointers in it.
786 static void drmFreeKernelVersion(drm_version_t *v)
798 * Copy version information.
800 * \param d destination pointer.
801 * \param s source pointer.
804 * Used by drmGetVersion() to translate the information returned by the ioctl
805 * interface in a private structure into the public structure counterpart.
807 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
809 d->version_major = s->version_major;
810 d->version_minor = s->version_minor;
811 d->version_patchlevel = s->version_patchlevel;
812 d->name_len = s->name_len;
813 d->name = strdup(s->name);
814 d->date_len = s->date_len;
815 d->date = strdup(s->date);
816 d->desc_len = s->desc_len;
817 d->desc = strdup(s->desc);
822 * Query the driver version information.
824 * \param fd file descriptor.
826 * \return pointer to a drmVersion structure which should be freed with
829 * \note Similar information is available via /proc/dri.
832 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
833 * first with zeros to get the string lengths, and then the actually strings.
834 * It also null-terminates them since they might not be already.
836 drmVersionPtr drmGetVersion(int fd)
838 drmVersionPtr retval;
839 drm_version_t *version = drmMalloc(sizeof(*version));
843 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
844 drmFreeKernelVersion(version);
848 if (version->name_len)
849 version->name = drmMalloc(version->name_len + 1);
850 if (version->date_len)
851 version->date = drmMalloc(version->date_len + 1);
852 if (version->desc_len)
853 version->desc = drmMalloc(version->desc_len + 1);
855 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
856 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
857 drmFreeKernelVersion(version);
861 /* The results might not be null-terminated strings, so terminate them. */
862 if (version->name_len) version->name[version->name_len] = '\0';
863 if (version->date_len) version->date[version->date_len] = '\0';
864 if (version->desc_len) version->desc[version->desc_len] = '\0';
866 retval = drmMalloc(sizeof(*retval));
867 drmCopyVersion(retval, version);
868 drmFreeKernelVersion(version);
874 * Get version information for the DRM user space library.
876 * This version number is driver independent.
878 * \param fd file descriptor.
880 * \return version information.
883 * This function allocates and fills a drm_version structure with a hard coded
886 drmVersionPtr drmGetLibVersion(int fd)
888 drm_version_t *version = drmMalloc(sizeof(*version));
891 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
892 * revision 1.0.x = original DRM interface with no drmGetLibVersion
893 * entry point and many drm<Device> extensions
894 * revision 1.1.x = added drmCommand entry points for device extensions
895 * added drmGetLibVersion to identify libdrm.a version
896 * revision 1.2.x = added drmSetInterfaceVersion
897 * modified drmOpen to handle both busid and name
898 * revision 1.3.x = added server + memory manager
900 version->version_major = 1;
901 version->version_minor = 3;
902 version->version_patchlevel = 0;
904 return (drmVersionPtr)version;
907 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
909 struct drm_get_cap cap;
913 cap.capability = capability;
915 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
923 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
925 struct drm_set_client_cap cap;
928 cap.capability = capability;
931 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
935 * Free the bus ID information.
937 * \param busid bus ID information string as given by drmGetBusid().
940 * This function is just frees the memory pointed by \p busid.
942 void drmFreeBusid(const char *busid)
944 drmFree((void *)busid);
949 * Get the bus ID of the device.
951 * \param fd file descriptor.
953 * \return bus ID string.
956 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
957 * get the string length and data, passing the arguments in a drm_unique
960 char *drmGetBusid(int fd)
966 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
968 u.unique = drmMalloc(u.unique_len + 1);
969 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
971 u.unique[u.unique_len] = '\0';
978 * Set the bus ID of the device.
980 * \param fd file descriptor.
981 * \param busid bus ID string.
983 * \return zero on success, negative on failure.
986 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
987 * the arguments in a drm_unique structure.
989 int drmSetBusid(int fd, const char *busid)
994 u.unique = (char *)busid;
995 u.unique_len = strlen(busid);
997 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
1003 int drmGetMagic(int fd, drm_magic_t * magic)
1010 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
1012 *magic = auth.magic;
1016 int drmAuthMagic(int fd, drm_magic_t magic)
1022 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
1028 * Specifies a range of memory that is available for mapping by a
1031 * \param fd file descriptor.
1032 * \param offset usually the physical address. The actual meaning depends of
1033 * the \p type parameter. See below.
1034 * \param size of the memory in bytes.
1035 * \param type type of the memory to be mapped.
1036 * \param flags combination of several flags to modify the function actions.
1037 * \param handle will be set to a value that may be used as the offset
1038 * parameter for mmap().
1040 * \return zero on success or a negative value on error.
1042 * \par Mapping the frame buffer
1043 * For the frame buffer
1044 * - \p offset will be the physical address of the start of the frame buffer,
1045 * - \p size will be the size of the frame buffer in bytes, and
1046 * - \p type will be DRM_FRAME_BUFFER.
1049 * The area mapped will be uncached. If MTRR support is available in the
1050 * kernel, the frame buffer area will be set to write combining.
1052 * \par Mapping the MMIO register area
1053 * For the MMIO register area,
1054 * - \p offset will be the physical address of the start of the register area,
1055 * - \p size will be the size of the register area bytes, and
1056 * - \p type will be DRM_REGISTERS.
1058 * The area mapped will be uncached.
1060 * \par Mapping the SAREA
1062 * - \p offset will be ignored and should be set to zero,
1063 * - \p size will be the desired size of the SAREA in bytes,
1064 * - \p type will be DRM_SHM.
1067 * A shared memory area of the requested size will be created and locked in
1068 * kernel memory. This area may be mapped into client-space by using the handle
1071 * \note May only be called by root.
1074 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1075 * the arguments in a drm_map structure.
1077 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1078 drmMapFlags flags, drm_handle_t *handle)
1083 map.offset = offset;
1087 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1090 *handle = (drm_handle_t)(uintptr_t)map.handle;
1094 int drmRmMap(int fd, drm_handle_t handle)
1099 map.handle = (void *)(uintptr_t)handle;
1101 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1107 * Make buffers available for DMA transfers.
1109 * \param fd file descriptor.
1110 * \param count number of buffers.
1111 * \param size size of each buffer.
1112 * \param flags buffer allocation flags.
1113 * \param agp_offset offset in the AGP aperture
1115 * \return number of buffers allocated, negative on error.
1118 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1122 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1125 drm_buf_desc_t request;
1128 request.count = count;
1129 request.size = size;
1130 request.flags = flags;
1131 request.agp_start = agp_offset;
1133 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1135 return request.count;
1138 int drmMarkBufs(int fd, double low, double high)
1140 drm_buf_info_t info;
1145 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1151 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1154 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1155 int retval = -errno;
1160 for (i = 0; i < info.count; i++) {
1161 info.list[i].low_mark = low * info.list[i].count;
1162 info.list[i].high_mark = high * info.list[i].count;
1163 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1164 int retval = -errno;
1177 * \param fd file descriptor.
1178 * \param count number of buffers to free.
1179 * \param list list of buffers to be freed.
1181 * \return zero on success, or a negative value on failure.
1183 * \note This function is primarily used for debugging.
1186 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1187 * the arguments in a drm_buf_free structure.
1189 int drmFreeBufs(int fd, int count, int *list)
1191 drm_buf_free_t request;
1194 request.count = count;
1195 request.list = list;
1196 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1205 * \param fd file descriptor.
1208 * This function closes the file descriptor.
1210 int drmClose(int fd)
1212 unsigned long key = drmGetKeyFromFd(fd);
1213 drmHashEntry *entry = drmGetEntry(fd);
1215 drmHashDestroy(entry->tagTable);
1218 entry->tagTable = NULL;
1220 drmHashDelete(drmHashTable, key);
1228 * Map a region of memory.
1230 * \param fd file descriptor.
1231 * \param handle handle returned by drmAddMap().
1232 * \param size size in bytes. Must match the size used by drmAddMap().
1233 * \param address will contain the user-space virtual address where the mapping
1236 * \return zero on success, or a negative value on failure.
1239 * This function is a wrapper for mmap().
1241 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1243 static unsigned long pagesize_mask = 0;
1249 pagesize_mask = getpagesize() - 1;
1251 size = (size + pagesize_mask) & ~pagesize_mask;
1253 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1254 if (*address == MAP_FAILED)
1261 * Unmap mappings obtained with drmMap().
1263 * \param address address as given by drmMap().
1264 * \param size size in bytes. Must match the size used by drmMap().
1266 * \return zero on success, or a negative value on failure.
1269 * This function is a wrapper for munmap().
1271 int drmUnmap(drmAddress address, drmSize size)
1273 return drm_munmap(address, size);
1276 drmBufInfoPtr drmGetBufInfo(int fd)
1278 drm_buf_info_t info;
1279 drmBufInfoPtr retval;
1284 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1288 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1291 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1296 retval = drmMalloc(sizeof(*retval));
1297 retval->count = info.count;
1298 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1299 for (i = 0; i < info.count; i++) {
1300 retval->list[i].count = info.list[i].count;
1301 retval->list[i].size = info.list[i].size;
1302 retval->list[i].low_mark = info.list[i].low_mark;
1303 retval->list[i].high_mark = info.list[i].high_mark;
1312 * Map all DMA buffers into client-virtual space.
1314 * \param fd file descriptor.
1316 * \return a pointer to a ::drmBufMap structure.
1318 * \note The client may not use these buffers until obtaining buffer indices
1322 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1323 * information about the buffers in a drm_buf_map structure into the
1324 * client-visible data structures.
1326 drmBufMapPtr drmMapBufs(int fd)
1329 drmBufMapPtr retval;
1333 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1339 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1342 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1347 retval = drmMalloc(sizeof(*retval));
1348 retval->count = bufs.count;
1349 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1350 for (i = 0; i < bufs.count; i++) {
1351 retval->list[i].idx = bufs.list[i].idx;
1352 retval->list[i].total = bufs.list[i].total;
1353 retval->list[i].used = 0;
1354 retval->list[i].address = bufs.list[i].address;
1364 * Unmap buffers allocated with drmMapBufs().
1366 * \return zero on success, or negative value on failure.
1369 * Calls munmap() for every buffer stored in \p bufs and frees the
1370 * memory allocated by drmMapBufs().
1372 int drmUnmapBufs(drmBufMapPtr bufs)
1376 for (i = 0; i < bufs->count; i++) {
1377 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1380 drmFree(bufs->list);
1387 #define DRM_DMA_RETRY 16
1390 * Reserve DMA buffers.
1392 * \param fd file descriptor.
1395 * \return zero on success, or a negative value on failure.
1398 * Assemble the arguments into a drm_dma structure and keeps issuing the
1399 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1401 int drmDMA(int fd, drmDMAReqPtr request)
1406 dma.context = request->context;
1407 dma.send_count = request->send_count;
1408 dma.send_indices = request->send_list;
1409 dma.send_sizes = request->send_sizes;
1410 dma.flags = request->flags;
1411 dma.request_count = request->request_count;
1412 dma.request_size = request->request_size;
1413 dma.request_indices = request->request_list;
1414 dma.request_sizes = request->request_sizes;
1415 dma.granted_count = 0;
1418 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1419 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1422 request->granted_count = dma.granted_count;
1431 * Obtain heavyweight hardware lock.
1433 * \param fd file descriptor.
1434 * \param context context.
1435 * \param flags flags that determine the sate of the hardware when the function
1438 * \return always zero.
1441 * This function translates the arguments into a drm_lock structure and issue
1442 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1444 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1449 lock.context = context;
1451 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1452 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1453 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1454 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1455 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1456 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1458 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1464 * Release the hardware lock.
1466 * \param fd file descriptor.
1467 * \param context context.
1469 * \return zero on success, or a negative value on failure.
1472 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1473 * argument in a drm_lock structure.
1475 int drmUnlock(int fd, drm_context_t context)
1480 lock.context = context;
1481 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1484 drm_context_t *drmGetReservedContextList(int fd, int *count)
1488 drm_context_t * retval;
1492 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1498 if (!(list = drmMalloc(res.count * sizeof(*list))))
1500 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1505 res.contexts = list;
1506 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1509 for (i = 0; i < res.count; i++)
1510 retval[i] = list[i].handle;
1517 void drmFreeReservedContextList(drm_context_t *pt)
1525 * Used by the X server during GLXContext initialization. This causes
1526 * per-context kernel-level resources to be allocated.
1528 * \param fd file descriptor.
1529 * \param handle is set on success. To be used by the client when requesting DMA
1530 * dispatch with drmDMA().
1532 * \return zero on success, or a negative value on failure.
1534 * \note May only be called by root.
1537 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1538 * argument in a drm_ctx structure.
1540 int drmCreateContext(int fd, drm_context_t *handle)
1545 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1547 *handle = ctx.handle;
1551 int drmSwitchToContext(int fd, drm_context_t context)
1556 ctx.handle = context;
1557 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1562 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1567 * Context preserving means that no context switches are done between DMA
1568 * buffers from one context and the next. This is suitable for use in the
1569 * X server (which promises to maintain hardware context), or in the
1570 * client-side library when buffers are swapped on behalf of two threads.
1573 ctx.handle = context;
1574 if (flags & DRM_CONTEXT_PRESERVED)
1575 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1576 if (flags & DRM_CONTEXT_2DONLY)
1577 ctx.flags |= _DRM_CONTEXT_2DONLY;
1578 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1583 int drmGetContextFlags(int fd, drm_context_t context,
1584 drm_context_tFlagsPtr flags)
1589 ctx.handle = context;
1590 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1593 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1594 *flags |= DRM_CONTEXT_PRESERVED;
1595 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1596 *flags |= DRM_CONTEXT_2DONLY;
1603 * Free any kernel-level resources allocated with drmCreateContext() associated
1606 * \param fd file descriptor.
1607 * \param handle handle given by drmCreateContext().
1609 * \return zero on success, or a negative value on failure.
1611 * \note May only be called by root.
1614 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1615 * argument in a drm_ctx structure.
1617 int drmDestroyContext(int fd, drm_context_t handle)
1622 ctx.handle = handle;
1623 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1628 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1633 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1635 *handle = draw.handle;
1639 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1644 draw.handle = handle;
1645 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1650 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1651 drm_drawable_info_type_t type, unsigned int num,
1654 drm_update_draw_t update;
1657 update.handle = handle;
1660 update.data = (unsigned long long)(unsigned long)data;
1662 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1669 * Acquire the AGP device.
1671 * Must be called before any of the other AGP related calls.
1673 * \param fd file descriptor.
1675 * \return zero on success, or a negative value on failure.
1678 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1680 int drmAgpAcquire(int fd)
1682 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1689 * Release the AGP device.
1691 * \param fd file descriptor.
1693 * \return zero on success, or a negative value on failure.
1696 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1698 int drmAgpRelease(int fd)
1700 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1709 * \param fd file descriptor.
1710 * \param mode AGP mode.
1712 * \return zero on success, or a negative value on failure.
1715 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1716 * argument in a drm_agp_mode structure.
1718 int drmAgpEnable(int fd, unsigned long mode)
1724 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1731 * Allocate a chunk of AGP memory.
1733 * \param fd file descriptor.
1734 * \param size requested memory size in bytes. Will be rounded to page boundary.
1735 * \param type type of memory to allocate.
1736 * \param address if not zero, will be set to the physical address of the
1738 * \param handle on success will be set to a handle of the allocated memory.
1740 * \return zero on success, or a negative value on failure.
1743 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1744 * arguments in a drm_agp_buffer structure.
1746 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1747 unsigned long *address, drm_handle_t *handle)
1752 *handle = DRM_AGP_NO_HANDLE;
1755 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1758 *address = b.physical;
1765 * Free a chunk of AGP memory.
1767 * \param fd file descriptor.
1768 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1770 * \return zero on success, or a negative value on failure.
1773 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1774 * argument in a drm_agp_buffer structure.
1776 int drmAgpFree(int fd, drm_handle_t handle)
1782 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1789 * Bind a chunk of AGP memory.
1791 * \param fd file descriptor.
1792 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1793 * \param offset offset in bytes. It will round to page boundary.
1795 * \return zero on success, or a negative value on failure.
1798 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1799 * argument in a drm_agp_binding structure.
1801 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1803 drm_agp_binding_t b;
1808 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1815 * Unbind a chunk of AGP memory.
1817 * \param fd file descriptor.
1818 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1820 * \return zero on success, or a negative value on failure.
1823 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1824 * the argument in a drm_agp_binding structure.
1826 int drmAgpUnbind(int fd, drm_handle_t handle)
1828 drm_agp_binding_t b;
1832 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1839 * Get AGP driver major version number.
1841 * \param fd file descriptor.
1843 * \return major version number on success, or a negative value on failure..
1846 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1847 * necessary information in a drm_agp_info structure.
1849 int drmAgpVersionMajor(int fd)
1855 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1857 return i.agp_version_major;
1862 * Get AGP driver minor version number.
1864 * \param fd file descriptor.
1866 * \return minor version number on success, or a negative value on failure.
1869 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1870 * necessary information in a drm_agp_info structure.
1872 int drmAgpVersionMinor(int fd)
1878 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1880 return i.agp_version_minor;
1887 * \param fd file descriptor.
1889 * \return mode on success, or zero on failure.
1892 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1893 * necessary information in a drm_agp_info structure.
1895 unsigned long drmAgpGetMode(int fd)
1901 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1908 * Get AGP aperture base.
1910 * \param fd file descriptor.
1912 * \return aperture base on success, zero on failure.
1915 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1916 * necessary information in a drm_agp_info structure.
1918 unsigned long drmAgpBase(int fd)
1924 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1926 return i.aperture_base;
1931 * Get AGP aperture size.
1933 * \param fd file descriptor.
1935 * \return aperture size on success, zero on failure.
1938 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1939 * necessary information in a drm_agp_info structure.
1941 unsigned long drmAgpSize(int fd)
1947 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1949 return i.aperture_size;
1954 * Get used AGP memory.
1956 * \param fd file descriptor.
1958 * \return memory used on success, or zero on failure.
1961 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1962 * necessary information in a drm_agp_info structure.
1964 unsigned long drmAgpMemoryUsed(int fd)
1970 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1972 return i.memory_used;
1977 * Get available AGP memory.
1979 * \param fd file descriptor.
1981 * \return memory available on success, or zero on failure.
1984 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1985 * necessary information in a drm_agp_info structure.
1987 unsigned long drmAgpMemoryAvail(int fd)
1993 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1995 return i.memory_allowed;
2000 * Get hardware vendor ID.
2002 * \param fd file descriptor.
2004 * \return vendor ID on success, or zero on failure.
2007 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2008 * necessary information in a drm_agp_info structure.
2010 unsigned int drmAgpVendorId(int fd)
2016 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2023 * Get hardware device ID.
2025 * \param fd file descriptor.
2027 * \return zero on success, or zero on failure.
2030 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2031 * necessary information in a drm_agp_info structure.
2033 unsigned int drmAgpDeviceId(int fd)
2039 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2044 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2046 drm_scatter_gather_t sg;
2052 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2054 *handle = sg.handle;
2058 int drmScatterGatherFree(int fd, drm_handle_t handle)
2060 drm_scatter_gather_t sg;
2064 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2072 * \param fd file descriptor.
2073 * \param vbl pointer to a drmVBlank structure.
2075 * \return zero on success, or a negative value on failure.
2078 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2080 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2082 struct timespec timeout, cur;
2085 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2087 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2093 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2094 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2095 if (ret && errno == EINTR) {
2096 clock_gettime(CLOCK_MONOTONIC, &cur);
2097 /* Timeout after 1s */
2098 if (cur.tv_sec > timeout.tv_sec + 1 ||
2099 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2106 } while (ret && errno == EINTR);
2112 int drmError(int err, const char *label)
2115 case DRM_ERR_NO_DEVICE:
2116 fprintf(stderr, "%s: no device\n", label);
2118 case DRM_ERR_NO_ACCESS:
2119 fprintf(stderr, "%s: no access\n", label);
2121 case DRM_ERR_NOT_ROOT:
2122 fprintf(stderr, "%s: not root\n", label);
2124 case DRM_ERR_INVALID:
2125 fprintf(stderr, "%s: invalid args\n", label);
2130 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2138 * Install IRQ handler.
2140 * \param fd file descriptor.
2141 * \param irq IRQ number.
2143 * \return zero on success, or a negative value on failure.
2146 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2147 * argument in a drm_control structure.
2149 int drmCtlInstHandler(int fd, int irq)
2154 ctl.func = DRM_INST_HANDLER;
2156 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2163 * Uninstall IRQ handler.
2165 * \param fd file descriptor.
2167 * \return zero on success, or a negative value on failure.
2170 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2171 * argument in a drm_control structure.
2173 int drmCtlUninstHandler(int fd)
2178 ctl.func = DRM_UNINST_HANDLER;
2180 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2185 int drmFinish(int fd, int context, drmLockFlags flags)
2190 lock.context = context;
2191 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2192 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2193 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2194 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2195 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2196 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2197 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2203 * Get IRQ from bus ID.
2205 * \param fd file descriptor.
2206 * \param busnum bus number.
2207 * \param devnum device number.
2208 * \param funcnum function number.
2210 * \return IRQ number on success, or a negative value on failure.
2213 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2214 * arguments in a drm_irq_busid structure.
2216 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2223 p.funcnum = funcnum;
2224 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2229 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2231 drmHashEntry *entry = drmGetEntry(fd);
2233 if (drmHashInsert(entry->tagTable, context, tag)) {
2234 drmHashDelete(entry->tagTable, context);
2235 drmHashInsert(entry->tagTable, context, tag);
2240 int drmDelContextTag(int fd, drm_context_t context)
2242 drmHashEntry *entry = drmGetEntry(fd);
2244 return drmHashDelete(entry->tagTable, context);
2247 void *drmGetContextTag(int fd, drm_context_t context)
2249 drmHashEntry *entry = drmGetEntry(fd);
2252 if (drmHashLookup(entry->tagTable, context, &value))
2258 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2259 drm_handle_t handle)
2261 drm_ctx_priv_map_t map;
2264 map.ctx_id = ctx_id;
2265 map.handle = (void *)(uintptr_t)handle;
2267 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2272 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2273 drm_handle_t *handle)
2275 drm_ctx_priv_map_t map;
2278 map.ctx_id = ctx_id;
2280 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2283 *handle = (drm_handle_t)(uintptr_t)map.handle;
2288 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2289 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2296 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2298 *offset = map.offset;
2302 *handle = (unsigned long)map.handle;
2307 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2308 unsigned long *magic, unsigned long *iocs)
2310 drm_client_t client;
2314 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2316 *auth = client.auth;
2319 *magic = client.magic;
2320 *iocs = client.iocs;
2324 int drmGetStats(int fd, drmStatsT *stats)
2330 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2334 memset(stats, 0, sizeof(*stats));
2335 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2339 stats->data[i].long_format = "%-20.20s"; \
2340 stats->data[i].rate_format = "%8.8s"; \
2341 stats->data[i].isvalue = 1; \
2342 stats->data[i].verbose = 0
2345 stats->data[i].long_format = "%-20.20s"; \
2346 stats->data[i].rate_format = "%5.5s"; \
2347 stats->data[i].isvalue = 0; \
2348 stats->data[i].mult_names = "kgm"; \
2349 stats->data[i].mult = 1000; \
2350 stats->data[i].verbose = 0
2353 stats->data[i].long_format = "%-20.20s"; \
2354 stats->data[i].rate_format = "%5.5s"; \
2355 stats->data[i].isvalue = 0; \
2356 stats->data[i].mult_names = "KGM"; \
2357 stats->data[i].mult = 1024; \
2358 stats->data[i].verbose = 0
2361 stats->count = s.count;
2362 for (i = 0; i < s.count; i++) {
2363 stats->data[i].value = s.data[i].value;
2364 switch (s.data[i].type) {
2365 case _DRM_STAT_LOCK:
2366 stats->data[i].long_name = "Lock";
2367 stats->data[i].rate_name = "Lock";
2370 case _DRM_STAT_OPENS:
2371 stats->data[i].long_name = "Opens";
2372 stats->data[i].rate_name = "O";
2374 stats->data[i].verbose = 1;
2376 case _DRM_STAT_CLOSES:
2377 stats->data[i].long_name = "Closes";
2378 stats->data[i].rate_name = "Lock";
2380 stats->data[i].verbose = 1;
2382 case _DRM_STAT_IOCTLS:
2383 stats->data[i].long_name = "Ioctls";
2384 stats->data[i].rate_name = "Ioc/s";
2387 case _DRM_STAT_LOCKS:
2388 stats->data[i].long_name = "Locks";
2389 stats->data[i].rate_name = "Lck/s";
2392 case _DRM_STAT_UNLOCKS:
2393 stats->data[i].long_name = "Unlocks";
2394 stats->data[i].rate_name = "Unl/s";
2398 stats->data[i].long_name = "IRQs";
2399 stats->data[i].rate_name = "IRQ/s";
2402 case _DRM_STAT_PRIMARY:
2403 stats->data[i].long_name = "Primary Bytes";
2404 stats->data[i].rate_name = "PB/s";
2407 case _DRM_STAT_SECONDARY:
2408 stats->data[i].long_name = "Secondary Bytes";
2409 stats->data[i].rate_name = "SB/s";
2413 stats->data[i].long_name = "DMA";
2414 stats->data[i].rate_name = "DMA/s";
2417 case _DRM_STAT_SPECIAL:
2418 stats->data[i].long_name = "Special DMA";
2419 stats->data[i].rate_name = "dma/s";
2422 case _DRM_STAT_MISSED:
2423 stats->data[i].long_name = "Miss";
2424 stats->data[i].rate_name = "Ms/s";
2427 case _DRM_STAT_VALUE:
2428 stats->data[i].long_name = "Value";
2429 stats->data[i].rate_name = "Value";
2432 case _DRM_STAT_BYTE:
2433 stats->data[i].long_name = "Bytes";
2434 stats->data[i].rate_name = "B/s";
2437 case _DRM_STAT_COUNT:
2439 stats->data[i].long_name = "Count";
2440 stats->data[i].rate_name = "Cnt/s";
2449 * Issue a set-version ioctl.
2451 * \param fd file descriptor.
2452 * \param drmCommandIndex command index
2453 * \param data source pointer of the data to be read and written.
2454 * \param size size of the data to be read and written.
2456 * \return zero on success, or a negative value on failure.
2459 * It issues a read-write ioctl given by
2460 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2462 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2465 drm_set_version_t sv;
2468 sv.drm_di_major = version->drm_di_major;
2469 sv.drm_di_minor = version->drm_di_minor;
2470 sv.drm_dd_major = version->drm_dd_major;
2471 sv.drm_dd_minor = version->drm_dd_minor;
2473 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2477 version->drm_di_major = sv.drm_di_major;
2478 version->drm_di_minor = sv.drm_di_minor;
2479 version->drm_dd_major = sv.drm_dd_major;
2480 version->drm_dd_minor = sv.drm_dd_minor;
2486 * Send a device-specific command.
2488 * \param fd file descriptor.
2489 * \param drmCommandIndex command index
2491 * \return zero on success, or a negative value on failure.
2494 * It issues a ioctl given by
2495 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2497 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2499 unsigned long request;
2501 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2503 if (drmIoctl(fd, request, NULL)) {
2511 * Send a device-specific read command.
2513 * \param fd file descriptor.
2514 * \param drmCommandIndex command index
2515 * \param data destination pointer of the data to be read.
2516 * \param size size of the data to be read.
2518 * \return zero on success, or a negative value on failure.
2521 * It issues a read ioctl given by
2522 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2524 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2527 unsigned long request;
2529 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2530 DRM_COMMAND_BASE + drmCommandIndex, size);
2532 if (drmIoctl(fd, request, data)) {
2540 * Send a device-specific write command.
2542 * \param fd file descriptor.
2543 * \param drmCommandIndex command index
2544 * \param data source pointer of the data to be written.
2545 * \param size size of the data to be written.
2547 * \return zero on success, or a negative value on failure.
2550 * It issues a write ioctl given by
2551 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2553 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2556 unsigned long request;
2558 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2559 DRM_COMMAND_BASE + drmCommandIndex, size);
2561 if (drmIoctl(fd, request, data)) {
2569 * Send a device-specific read-write command.
2571 * \param fd file descriptor.
2572 * \param drmCommandIndex command index
2573 * \param data source pointer of the data to be read and written.
2574 * \param size size of the data to be read and written.
2576 * \return zero on success, or a negative value on failure.
2579 * It issues a read-write ioctl given by
2580 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2582 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2585 unsigned long request;
2587 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2588 DRM_COMMAND_BASE + drmCommandIndex, size);
2590 if (drmIoctl(fd, request, data))
2595 #define DRM_MAX_FDS 16
2601 } connection[DRM_MAX_FDS];
2603 static int nr_fds = 0;
2605 int drmOpenOnce(void *unused,
2609 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2612 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2617 for (i = 0; i < nr_fds; i++)
2618 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2619 (connection[i].type == type)) {
2620 connection[i].refcount++;
2622 return connection[i].fd;
2625 fd = drmOpenWithType(NULL, BusID, type);
2626 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2629 connection[nr_fds].BusID = strdup(BusID);
2630 connection[nr_fds].fd = fd;
2631 connection[nr_fds].refcount = 1;
2632 connection[nr_fds].type = type;
2636 fprintf(stderr, "saved connection %d for %s %d\n",
2637 nr_fds, connection[nr_fds].BusID,
2638 strcmp(BusID, connection[nr_fds].BusID));
2645 void drmCloseOnce(int fd)
2649 for (i = 0; i < nr_fds; i++) {
2650 if (fd == connection[i].fd) {
2651 if (--connection[i].refcount == 0) {
2652 drmClose(connection[i].fd);
2653 free(connection[i].BusID);
2656 connection[i] = connection[nr_fds];
2664 int drmSetMaster(int fd)
2666 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2669 int drmDropMaster(int fd)
2671 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2674 char *drmGetDeviceNameFromFd(int fd)
2681 /* The whole drmOpen thing is a fiasco and we need to find a way
2682 * back to just using open(2). For now, however, lets just make
2683 * things worse with even more ad hoc directory walking code to
2684 * discover the device file name. */
2689 for (i = 0; i < DRM_MAX_MINOR; i++) {
2690 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2691 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2694 if (i == DRM_MAX_MINOR)
2697 return strdup(name);
2700 int drmGetNodeTypeFromFd(int fd)
2705 if (fstat(fd, &sbuf))
2708 maj = major(sbuf.st_rdev);
2709 min = minor(sbuf.st_rdev);
2711 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode)) {
2716 type = drmGetMinorType(min);
2722 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2724 struct drm_prime_handle args;
2727 args.handle = handle;
2729 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2733 *prime_fd = args.fd;
2737 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2739 struct drm_prime_handle args;
2744 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2748 *handle = args.handle;
2752 static char *drmGetMinorNameForFD(int fd, int type)
2756 struct dirent *pent, *ent;
2758 const char *name = drmGetMinorName(type);
2760 char dev_name[64], buf[64];
2769 if (fstat(fd, &sbuf))
2772 maj = major(sbuf.st_rdev);
2773 min = minor(sbuf.st_rdev);
2775 if (maj != DRM_MAJOR || !S_ISCHR(sbuf.st_mode))
2778 snprintf(buf, sizeof(buf), "/sys/dev/char/%d:%d/device/drm", maj, min);
2780 sysdir = opendir(buf);
2784 name_max = fpathconf(dirfd(sysdir), _PC_NAME_MAX);
2788 pent = malloc(offsetof(struct dirent, d_name) + name_max + 1);
2792 while (readdir_r(sysdir, pent, &ent) == 0 && ent != NULL) {
2793 if (strncmp(ent->d_name, name, len) == 0) {
2797 snprintf(dev_name, sizeof(dev_name), DRM_DIR_NAME "/%s",
2799 return strdup(dev_name);
2811 char *drmGetPrimaryDeviceNameFromFd(int fd)
2813 return drmGetMinorNameForFD(fd, DRM_NODE_PRIMARY);
2816 char *drmGetRenderDeviceNameFromFd(int fd)
2818 return drmGetMinorNameForFD(fd, DRM_NODE_RENDER);