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.
47 #include <sys/types.h>
49 #define stat_t struct stat
50 #include <sys/ioctl.h>
54 /* Not all systems have MAP_FAILED defined */
56 #define MAP_FAILED ((void *)-1)
62 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
75 #define DRM_MAJOR 226 /* Linux */
79 * This definition needs to be changed on some systems if dev_t is a structure.
80 * If there is a header file we can get it from, there would be best.
83 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
86 #define DRM_MSG_VERBOSITY 3
88 #define memclear(s) memset(&s, 0, sizeof(s))
90 static drmServerInfoPtr drm_server_info;
92 void drmSetServerInfo(drmServerInfoPtr info)
94 drm_server_info = info;
98 * Output a message to stderr.
100 * \param format printf() like format string.
103 * This function is a wrapper around vfprintf().
106 static int DRM_PRINTFLIKE(1, 0)
107 drmDebugPrint(const char *format, va_list ap)
109 return vfprintf(stderr, format, ap);
112 typedef int DRM_PRINTFLIKE(1, 0) (*debug_msg_func_t)(const char *format,
115 static debug_msg_func_t drm_debug_print = drmDebugPrint;
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 drm_debug_print(format, ap);
135 drmSetDebugMsgFunction(debug_msg_func_t debug_msg_ptr)
137 drm_debug_print = debug_msg_ptr;
140 static void *drmHashTable = NULL; /* Context switch callbacks */
142 void *drmGetHashTable(void)
147 void *drmMalloc(int size)
150 if ((pt = malloc(size)))
155 void drmFree(void *pt)
162 * Call ioctl, restarting if it is interupted
165 drmIoctl(int fd, unsigned long request, void *arg)
170 ret = ioctl(fd, request, arg);
171 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
175 static unsigned long drmGetKeyFromFd(int fd)
184 drmHashEntry *drmGetEntry(int fd)
186 unsigned long key = drmGetKeyFromFd(fd);
191 drmHashTable = drmHashCreate();
193 if (drmHashLookup(drmHashTable, key, &value)) {
194 entry = drmMalloc(sizeof(*entry));
197 entry->tagTable = drmHashCreate();
198 drmHashInsert(drmHashTable, key, entry);
206 * Compare two busid strings
211 * \return 1 if matched.
214 * This function compares two bus ID strings. It understands the older
215 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
216 * domain, b is bus, d is device, f is function.
218 static int drmMatchBusID(const char *id1, const char *id2, int pci_domain_ok)
220 /* First, check if the IDs are exactly the same */
221 if (strcasecmp(id1, id2) == 0)
224 /* Try to match old/new-style PCI bus IDs. */
225 if (strncasecmp(id1, "pci", 3) == 0) {
226 unsigned int o1, b1, d1, f1;
227 unsigned int o2, b2, d2, f2;
230 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
233 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
238 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
241 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
246 /* If domains aren't properly supported by the kernel interface,
247 * just ignore them, which sucks less than picking a totally random
248 * card with "open by name"
253 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
262 * Handles error checking for chown call.
264 * \param path to file.
265 * \param id of the new owner.
266 * \param id of the new group.
268 * \return zero if success or -1 if failure.
271 * Checks for failure. If failure was caused by signal call chown again.
272 * If any other failure happened then it will output error mesage using
275 static int chown_check_return(const char *path, uid_t owner, gid_t group)
280 rv = chown(path, owner, group);
281 } while (rv != 0 && errno == EINTR);
286 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
287 path, errno, strerror(errno));
292 * Open the DRM device, creating it if necessary.
294 * \param dev major and minor numbers of the device.
295 * \param minor minor number of the device.
297 * \return a file descriptor on success, or a negative value on error.
300 * Assembles the device name from \p minor and opens it, creating the device
301 * special file node with the major and minor numbers specified by \p dev and
302 * parent directory if necessary and was called by root.
304 static int drmOpenDevice(dev_t dev, int minor, int type)
307 const char *dev_name;
310 mode_t devmode = DRM_DEV_MODE, serv_mode;
311 int isroot = !geteuid();
312 uid_t user = DRM_DEV_UID;
313 gid_t group = DRM_DEV_GID, serv_group;
316 case DRM_NODE_PRIMARY:
317 dev_name = DRM_DEV_NAME;
319 case DRM_NODE_CONTROL:
320 dev_name = DRM_CONTROL_DEV_NAME;
322 case DRM_NODE_RENDER:
323 dev_name = DRM_RENDER_DEV_NAME;
329 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
330 drmMsg("drmOpenDevice: node name is %s\n", buf);
332 if (drm_server_info) {
333 drm_server_info->get_perms(&serv_group, &serv_mode);
334 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
335 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
336 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
340 if (stat(DRM_DIR_NAME, &st)) {
342 return DRM_ERR_NOT_ROOT;
343 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
344 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
345 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
348 /* Check if the device node exists and create it if necessary. */
349 if (stat(buf, &st)) {
351 return DRM_ERR_NOT_ROOT;
353 mknod(buf, S_IFCHR | devmode, dev);
356 if (drm_server_info) {
357 chown_check_return(buf, user, group);
361 /* if we modprobed then wait for udev */
365 if (stat(DRM_DIR_NAME, &st)) {
369 if (udev_count == 50)
374 if (stat(buf, &st)) {
378 if (udev_count == 50)
385 fd = open(buf, O_RDWR, 0);
386 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
387 fd, fd < 0 ? strerror(errno) : "OK");
392 /* Check if the device node is not what we expect it to be, and recreate it
393 * and try again if so.
395 if (st.st_rdev != dev) {
397 return DRM_ERR_NOT_ROOT;
399 mknod(buf, S_IFCHR | devmode, dev);
400 if (drm_server_info) {
401 chown_check_return(buf, user, group);
405 fd = open(buf, O_RDWR, 0);
406 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
407 fd, fd < 0 ? strerror(errno) : "OK");
411 drmMsg("drmOpenDevice: Open failed\n");
419 * Open the DRM device
421 * \param minor device minor number.
422 * \param create allow to create the device if set.
424 * \return a file descriptor on success, or a negative value on error.
427 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
428 * name from \p minor and opens it.
430 static int drmOpenMinor(int minor, int create, int type)
434 const char *dev_name;
437 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
440 case DRM_NODE_PRIMARY:
441 dev_name = DRM_DEV_NAME;
443 case DRM_NODE_CONTROL:
444 dev_name = DRM_CONTROL_DEV_NAME;
446 case DRM_NODE_RENDER:
447 dev_name = DRM_RENDER_DEV_NAME;
453 sprintf(buf, dev_name, DRM_DIR_NAME, minor);
454 if ((fd = open(buf, O_RDWR, 0)) >= 0)
461 * Determine whether the DRM kernel driver has been loaded.
463 * \return 1 if the DRM driver is loaded, 0 otherwise.
466 * Determine the presence of the kernel driver by attempting to open the 0
467 * minor and get version information. For backward compatibility with older
468 * Linux implementations, /proc/dri is also checked.
470 int drmAvailable(void)
472 drmVersionPtr version;
476 if ((fd = drmOpenMinor(0, 1, DRM_NODE_PRIMARY)) < 0) {
478 /* Try proc for backward Linux compatibility */
479 if (!access("/proc/dri/0", R_OK))
485 if ((version = drmGetVersion(fd))) {
487 drmFreeVersion(version);
494 static int drmGetMinorBase(int type)
497 case DRM_NODE_PRIMARY:
499 case DRM_NODE_CONTROL:
501 case DRM_NODE_RENDER:
509 * Open the device by bus ID.
511 * \param busid bus ID.
512 * \param type device node type.
514 * \return a file descriptor on success, or a negative value on error.
517 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
518 * comparing the device bus ID with the one supplied.
520 * \sa drmOpenMinor() and drmGetBusid().
522 static int drmOpenByBusid(const char *busid, int type)
524 int i, pci_domain_ok = 1;
528 int base = drmGetMinorBase(type);
533 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
534 for (i = base; i < base + DRM_MAX_MINOR; i++) {
535 fd = drmOpenMinor(i, 1, type);
536 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
538 /* We need to try for 1.4 first for proper PCI domain support
539 * and if that fails, we know the kernel is busted
543 sv.drm_dd_major = -1; /* Don't care */
544 sv.drm_dd_minor = -1; /* Don't care */
545 if (drmSetInterfaceVersion(fd, &sv)) {
551 sv.drm_dd_major = -1; /* Don't care */
552 sv.drm_dd_minor = -1; /* Don't care */
553 drmMsg("drmOpenByBusid: Interface 1.4 failed, trying 1.1\n");
554 drmSetInterfaceVersion(fd, &sv);
556 buf = drmGetBusid(fd);
557 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
558 if (buf && drmMatchBusID(buf, busid, pci_domain_ok)) {
572 * Open the device by name.
574 * \param name driver name.
575 * \param type the device node type.
577 * \return a file descriptor on success, or a negative value on error.
580 * This function opens the first minor number that matches the driver name and
581 * isn't already in use. If it's in use it then it will already have a bus ID
584 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
586 static int drmOpenByName(const char *name, int type)
590 drmVersionPtr version;
592 int base = drmGetMinorBase(type);
598 * Open the first minor number that matches the driver name and isn't
599 * already in use. If it's in use it will have a busid assigned already.
601 for (i = base; i < base + DRM_MAX_MINOR; i++) {
602 if ((fd = drmOpenMinor(i, 1, type)) >= 0) {
603 if ((version = drmGetVersion(fd))) {
604 if (!strcmp(version->name, name)) {
605 drmFreeVersion(version);
606 id = drmGetBusid(fd);
607 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
616 drmFreeVersion(version);
624 /* Backward-compatibility /proc support */
625 for (i = 0; i < 8; i++) {
626 char proc_name[64], buf[512];
627 char *driver, *pt, *devstring;
630 sprintf(proc_name, "/proc/dri/%d/name", i);
631 if ((fd = open(proc_name, 0, 0)) >= 0) {
632 retcode = read(fd, buf, sizeof(buf)-1);
635 buf[retcode-1] = '\0';
636 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
638 if (*pt) { /* Device is next */
640 if (!strcmp(driver, name)) { /* Match */
641 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
643 if (*pt) { /* Found busid */
644 return drmOpenByBusid(++pt, type);
645 } else { /* No busid */
646 return drmOpenDevice(strtol(devstring, NULL, 0),i, type);
660 * Open the DRM device.
662 * Looks up the specified name and bus ID, and opens the device found. The
663 * entry in /dev/dri is created if necessary and if called by root.
665 * \param name driver name. Not referenced if bus ID is supplied.
666 * \param busid bus ID. Zero if not known.
668 * \return a file descriptor on success, or a negative value on error.
671 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
674 int drmOpen(const char *name, const char *busid)
676 return drmOpenWithType(name, busid, DRM_NODE_PRIMARY);
680 * Open the DRM device with specified type.
682 * Looks up the specified name and bus ID, and opens the device found. The
683 * entry in /dev/dri is created if necessary and if called by root.
685 * \param name driver name. Not referenced if bus ID is supplied.
686 * \param busid bus ID. Zero if not known.
687 * \param type the device node type to open, PRIMARY, CONTROL or RENDER
689 * \return a file descriptor on success, or a negative value on error.
692 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
695 int drmOpenWithType(const char *name, const char *busid, int type)
697 if (!drmAvailable() && name != NULL && drm_server_info) {
698 /* try to load the kernel module */
699 if (!drm_server_info->load_module(name)) {
700 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
706 int fd = drmOpenByBusid(busid, type);
712 return drmOpenByName(name, type);
717 int drmOpenControl(int minor)
719 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
722 int drmOpenRender(int minor)
724 return drmOpenMinor(minor, 0, DRM_NODE_RENDER);
728 * Free the version information returned by drmGetVersion().
730 * \param v pointer to the version information.
733 * It frees the memory pointed by \p %v as well as all the non-null strings
736 void drmFreeVersion(drmVersionPtr v)
748 * Free the non-public version information returned by the kernel.
750 * \param v pointer to the version information.
753 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
754 * the non-null strings pointers in it.
756 static void drmFreeKernelVersion(drm_version_t *v)
768 * Copy version information.
770 * \param d destination pointer.
771 * \param s source pointer.
774 * Used by drmGetVersion() to translate the information returned by the ioctl
775 * interface in a private structure into the public structure counterpart.
777 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
779 d->version_major = s->version_major;
780 d->version_minor = s->version_minor;
781 d->version_patchlevel = s->version_patchlevel;
782 d->name_len = s->name_len;
783 d->name = strdup(s->name);
784 d->date_len = s->date_len;
785 d->date = strdup(s->date);
786 d->desc_len = s->desc_len;
787 d->desc = strdup(s->desc);
792 * Query the driver version information.
794 * \param fd file descriptor.
796 * \return pointer to a drmVersion structure which should be freed with
799 * \note Similar information is available via /proc/dri.
802 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
803 * first with zeros to get the string lengths, and then the actually strings.
804 * It also null-terminates them since they might not be already.
806 drmVersionPtr drmGetVersion(int fd)
808 drmVersionPtr retval;
809 drm_version_t *version = drmMalloc(sizeof(*version));
813 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
814 drmFreeKernelVersion(version);
818 if (version->name_len)
819 version->name = drmMalloc(version->name_len + 1);
820 if (version->date_len)
821 version->date = drmMalloc(version->date_len + 1);
822 if (version->desc_len)
823 version->desc = drmMalloc(version->desc_len + 1);
825 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
826 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
827 drmFreeKernelVersion(version);
831 /* The results might not be null-terminated strings, so terminate them. */
832 if (version->name_len) version->name[version->name_len] = '\0';
833 if (version->date_len) version->date[version->date_len] = '\0';
834 if (version->desc_len) version->desc[version->desc_len] = '\0';
836 retval = drmMalloc(sizeof(*retval));
837 drmCopyVersion(retval, version);
838 drmFreeKernelVersion(version);
844 * Get version information for the DRM user space library.
846 * This version number is driver independent.
848 * \param fd file descriptor.
850 * \return version information.
853 * This function allocates and fills a drm_version structure with a hard coded
856 drmVersionPtr drmGetLibVersion(int fd)
858 drm_version_t *version = drmMalloc(sizeof(*version));
861 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
862 * revision 1.0.x = original DRM interface with no drmGetLibVersion
863 * entry point and many drm<Device> extensions
864 * revision 1.1.x = added drmCommand entry points for device extensions
865 * added drmGetLibVersion to identify libdrm.a version
866 * revision 1.2.x = added drmSetInterfaceVersion
867 * modified drmOpen to handle both busid and name
868 * revision 1.3.x = added server + memory manager
870 version->version_major = 1;
871 version->version_minor = 3;
872 version->version_patchlevel = 0;
874 return (drmVersionPtr)version;
877 int drmGetCap(int fd, uint64_t capability, uint64_t *value)
879 struct drm_get_cap cap;
883 cap.capability = capability;
885 ret = drmIoctl(fd, DRM_IOCTL_GET_CAP, &cap);
893 int drmSetClientCap(int fd, uint64_t capability, uint64_t value)
895 struct drm_set_client_cap cap;
898 cap.capability = capability;
901 return drmIoctl(fd, DRM_IOCTL_SET_CLIENT_CAP, &cap);
905 * Free the bus ID information.
907 * \param busid bus ID information string as given by drmGetBusid().
910 * This function is just frees the memory pointed by \p busid.
912 void drmFreeBusid(const char *busid)
914 drmFree((void *)busid);
919 * Get the bus ID of the device.
921 * \param fd file descriptor.
923 * \return bus ID string.
926 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
927 * get the string length and data, passing the arguments in a drm_unique
930 char *drmGetBusid(int fd)
936 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
938 u.unique = drmMalloc(u.unique_len + 1);
939 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
941 u.unique[u.unique_len] = '\0';
948 * Set the bus ID of the device.
950 * \param fd file descriptor.
951 * \param busid bus ID string.
953 * \return zero on success, negative on failure.
956 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
957 * the arguments in a drm_unique structure.
959 int drmSetBusid(int fd, const char *busid)
964 u.unique = (char *)busid;
965 u.unique_len = strlen(busid);
967 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
973 int drmGetMagic(int fd, drm_magic_t * magic)
980 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
986 int drmAuthMagic(int fd, drm_magic_t magic)
992 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
998 * Specifies a range of memory that is available for mapping by a
1001 * \param fd file descriptor.
1002 * \param offset usually the physical address. The actual meaning depends of
1003 * the \p type parameter. See below.
1004 * \param size of the memory in bytes.
1005 * \param type type of the memory to be mapped.
1006 * \param flags combination of several flags to modify the function actions.
1007 * \param handle will be set to a value that may be used as the offset
1008 * parameter for mmap().
1010 * \return zero on success or a negative value on error.
1012 * \par Mapping the frame buffer
1013 * For the frame buffer
1014 * - \p offset will be the physical address of the start of the frame buffer,
1015 * - \p size will be the size of the frame buffer in bytes, and
1016 * - \p type will be DRM_FRAME_BUFFER.
1019 * The area mapped will be uncached. If MTRR support is available in the
1020 * kernel, the frame buffer area will be set to write combining.
1022 * \par Mapping the MMIO register area
1023 * For the MMIO register area,
1024 * - \p offset will be the physical address of the start of the register area,
1025 * - \p size will be the size of the register area bytes, and
1026 * - \p type will be DRM_REGISTERS.
1028 * The area mapped will be uncached.
1030 * \par Mapping the SAREA
1032 * - \p offset will be ignored and should be set to zero,
1033 * - \p size will be the desired size of the SAREA in bytes,
1034 * - \p type will be DRM_SHM.
1037 * A shared memory area of the requested size will be created and locked in
1038 * kernel memory. This area may be mapped into client-space by using the handle
1041 * \note May only be called by root.
1044 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
1045 * the arguments in a drm_map structure.
1047 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
1048 drmMapFlags flags, drm_handle_t *handle)
1053 map.offset = offset;
1057 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
1060 *handle = (drm_handle_t)(uintptr_t)map.handle;
1064 int drmRmMap(int fd, drm_handle_t handle)
1069 map.handle = (void *)(uintptr_t)handle;
1071 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
1077 * Make buffers available for DMA transfers.
1079 * \param fd file descriptor.
1080 * \param count number of buffers.
1081 * \param size size of each buffer.
1082 * \param flags buffer allocation flags.
1083 * \param agp_offset offset in the AGP aperture
1085 * \return number of buffers allocated, negative on error.
1088 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
1092 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1095 drm_buf_desc_t request;
1098 request.count = count;
1099 request.size = size;
1100 request.flags = flags;
1101 request.agp_start = agp_offset;
1103 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1105 return request.count;
1108 int drmMarkBufs(int fd, double low, double high)
1110 drm_buf_info_t info;
1115 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1121 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1124 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1125 int retval = -errno;
1130 for (i = 0; i < info.count; i++) {
1131 info.list[i].low_mark = low * info.list[i].count;
1132 info.list[i].high_mark = high * info.list[i].count;
1133 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1134 int retval = -errno;
1147 * \param fd file descriptor.
1148 * \param count number of buffers to free.
1149 * \param list list of buffers to be freed.
1151 * \return zero on success, or a negative value on failure.
1153 * \note This function is primarily used for debugging.
1156 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1157 * the arguments in a drm_buf_free structure.
1159 int drmFreeBufs(int fd, int count, int *list)
1161 drm_buf_free_t request;
1164 request.count = count;
1165 request.list = list;
1166 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1175 * \param fd file descriptor.
1178 * This function closes the file descriptor.
1180 int drmClose(int fd)
1182 unsigned long key = drmGetKeyFromFd(fd);
1183 drmHashEntry *entry = drmGetEntry(fd);
1185 drmHashDestroy(entry->tagTable);
1188 entry->tagTable = NULL;
1190 drmHashDelete(drmHashTable, key);
1198 * Map a region of memory.
1200 * \param fd file descriptor.
1201 * \param handle handle returned by drmAddMap().
1202 * \param size size in bytes. Must match the size used by drmAddMap().
1203 * \param address will contain the user-space virtual address where the mapping
1206 * \return zero on success, or a negative value on failure.
1209 * This function is a wrapper for mmap().
1211 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1213 static unsigned long pagesize_mask = 0;
1219 pagesize_mask = getpagesize() - 1;
1221 size = (size + pagesize_mask) & ~pagesize_mask;
1223 *address = drm_mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1224 if (*address == MAP_FAILED)
1231 * Unmap mappings obtained with drmMap().
1233 * \param address address as given by drmMap().
1234 * \param size size in bytes. Must match the size used by drmMap().
1236 * \return zero on success, or a negative value on failure.
1239 * This function is a wrapper for munmap().
1241 int drmUnmap(drmAddress address, drmSize size)
1243 return drm_munmap(address, size);
1246 drmBufInfoPtr drmGetBufInfo(int fd)
1248 drm_buf_info_t info;
1249 drmBufInfoPtr retval;
1254 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1258 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1261 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1266 retval = drmMalloc(sizeof(*retval));
1267 retval->count = info.count;
1268 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1269 for (i = 0; i < info.count; i++) {
1270 retval->list[i].count = info.list[i].count;
1271 retval->list[i].size = info.list[i].size;
1272 retval->list[i].low_mark = info.list[i].low_mark;
1273 retval->list[i].high_mark = info.list[i].high_mark;
1282 * Map all DMA buffers into client-virtual space.
1284 * \param fd file descriptor.
1286 * \return a pointer to a ::drmBufMap structure.
1288 * \note The client may not use these buffers until obtaining buffer indices
1292 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1293 * information about the buffers in a drm_buf_map structure into the
1294 * client-visible data structures.
1296 drmBufMapPtr drmMapBufs(int fd)
1299 drmBufMapPtr retval;
1303 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1309 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1312 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1317 retval = drmMalloc(sizeof(*retval));
1318 retval->count = bufs.count;
1319 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1320 for (i = 0; i < bufs.count; i++) {
1321 retval->list[i].idx = bufs.list[i].idx;
1322 retval->list[i].total = bufs.list[i].total;
1323 retval->list[i].used = 0;
1324 retval->list[i].address = bufs.list[i].address;
1334 * Unmap buffers allocated with drmMapBufs().
1336 * \return zero on success, or negative value on failure.
1339 * Calls munmap() for every buffer stored in \p bufs and frees the
1340 * memory allocated by drmMapBufs().
1342 int drmUnmapBufs(drmBufMapPtr bufs)
1346 for (i = 0; i < bufs->count; i++) {
1347 drm_munmap(bufs->list[i].address, bufs->list[i].total);
1350 drmFree(bufs->list);
1357 #define DRM_DMA_RETRY 16
1360 * Reserve DMA buffers.
1362 * \param fd file descriptor.
1365 * \return zero on success, or a negative value on failure.
1368 * Assemble the arguments into a drm_dma structure and keeps issuing the
1369 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1371 int drmDMA(int fd, drmDMAReqPtr request)
1376 dma.context = request->context;
1377 dma.send_count = request->send_count;
1378 dma.send_indices = request->send_list;
1379 dma.send_sizes = request->send_sizes;
1380 dma.flags = request->flags;
1381 dma.request_count = request->request_count;
1382 dma.request_size = request->request_size;
1383 dma.request_indices = request->request_list;
1384 dma.request_sizes = request->request_sizes;
1385 dma.granted_count = 0;
1388 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1389 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1392 request->granted_count = dma.granted_count;
1401 * Obtain heavyweight hardware lock.
1403 * \param fd file descriptor.
1404 * \param context context.
1405 * \param flags flags that determine the sate of the hardware when the function
1408 * \return always zero.
1411 * This function translates the arguments into a drm_lock structure and issue
1412 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1414 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1419 lock.context = context;
1421 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1422 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1423 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1424 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1425 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1426 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1428 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1434 * Release the hardware lock.
1436 * \param fd file descriptor.
1437 * \param context context.
1439 * \return zero on success, or a negative value on failure.
1442 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1443 * argument in a drm_lock structure.
1445 int drmUnlock(int fd, drm_context_t context)
1450 lock.context = context;
1451 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1454 drm_context_t *drmGetReservedContextList(int fd, int *count)
1458 drm_context_t * retval;
1462 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1468 if (!(list = drmMalloc(res.count * sizeof(*list))))
1470 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1475 res.contexts = list;
1476 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1479 for (i = 0; i < res.count; i++)
1480 retval[i] = list[i].handle;
1487 void drmFreeReservedContextList(drm_context_t *pt)
1495 * Used by the X server during GLXContext initialization. This causes
1496 * per-context kernel-level resources to be allocated.
1498 * \param fd file descriptor.
1499 * \param handle is set on success. To be used by the client when requesting DMA
1500 * dispatch with drmDMA().
1502 * \return zero on success, or a negative value on failure.
1504 * \note May only be called by root.
1507 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1508 * argument in a drm_ctx structure.
1510 int drmCreateContext(int fd, drm_context_t *handle)
1515 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1517 *handle = ctx.handle;
1521 int drmSwitchToContext(int fd, drm_context_t context)
1526 ctx.handle = context;
1527 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1532 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1537 * Context preserving means that no context switches are done between DMA
1538 * buffers from one context and the next. This is suitable for use in the
1539 * X server (which promises to maintain hardware context), or in the
1540 * client-side library when buffers are swapped on behalf of two threads.
1543 ctx.handle = context;
1544 if (flags & DRM_CONTEXT_PRESERVED)
1545 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1546 if (flags & DRM_CONTEXT_2DONLY)
1547 ctx.flags |= _DRM_CONTEXT_2DONLY;
1548 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1553 int drmGetContextFlags(int fd, drm_context_t context,
1554 drm_context_tFlagsPtr flags)
1559 ctx.handle = context;
1560 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1563 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1564 *flags |= DRM_CONTEXT_PRESERVED;
1565 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1566 *flags |= DRM_CONTEXT_2DONLY;
1573 * Free any kernel-level resources allocated with drmCreateContext() associated
1576 * \param fd file descriptor.
1577 * \param handle handle given by drmCreateContext().
1579 * \return zero on success, or a negative value on failure.
1581 * \note May only be called by root.
1584 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1585 * argument in a drm_ctx structure.
1587 int drmDestroyContext(int fd, drm_context_t handle)
1592 ctx.handle = handle;
1593 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1598 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1603 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1605 *handle = draw.handle;
1609 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1614 draw.handle = handle;
1615 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1620 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1621 drm_drawable_info_type_t type, unsigned int num,
1624 drm_update_draw_t update;
1627 update.handle = handle;
1630 update.data = (unsigned long long)(unsigned long)data;
1632 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1639 * Acquire the AGP device.
1641 * Must be called before any of the other AGP related calls.
1643 * \param fd file descriptor.
1645 * \return zero on success, or a negative value on failure.
1648 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1650 int drmAgpAcquire(int fd)
1652 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1659 * Release the AGP device.
1661 * \param fd file descriptor.
1663 * \return zero on success, or a negative value on failure.
1666 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1668 int drmAgpRelease(int fd)
1670 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1679 * \param fd file descriptor.
1680 * \param mode AGP mode.
1682 * \return zero on success, or a negative value on failure.
1685 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1686 * argument in a drm_agp_mode structure.
1688 int drmAgpEnable(int fd, unsigned long mode)
1694 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1701 * Allocate a chunk of AGP memory.
1703 * \param fd file descriptor.
1704 * \param size requested memory size in bytes. Will be rounded to page boundary.
1705 * \param type type of memory to allocate.
1706 * \param address if not zero, will be set to the physical address of the
1708 * \param handle on success will be set to a handle of the allocated memory.
1710 * \return zero on success, or a negative value on failure.
1713 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1714 * arguments in a drm_agp_buffer structure.
1716 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1717 unsigned long *address, drm_handle_t *handle)
1722 *handle = DRM_AGP_NO_HANDLE;
1725 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1728 *address = b.physical;
1735 * Free a chunk of AGP memory.
1737 * \param fd file descriptor.
1738 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1740 * \return zero on success, or a negative value on failure.
1743 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1744 * argument in a drm_agp_buffer structure.
1746 int drmAgpFree(int fd, drm_handle_t handle)
1752 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1759 * Bind a chunk of AGP memory.
1761 * \param fd file descriptor.
1762 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1763 * \param offset offset in bytes. It will round to page boundary.
1765 * \return zero on success, or a negative value on failure.
1768 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1769 * argument in a drm_agp_binding structure.
1771 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1773 drm_agp_binding_t b;
1778 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1785 * Unbind a chunk of AGP memory.
1787 * \param fd file descriptor.
1788 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1790 * \return zero on success, or a negative value on failure.
1793 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1794 * the argument in a drm_agp_binding structure.
1796 int drmAgpUnbind(int fd, drm_handle_t handle)
1798 drm_agp_binding_t b;
1802 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1809 * Get AGP driver major version number.
1811 * \param fd file descriptor.
1813 * \return major version number on success, or a negative value on failure..
1816 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1817 * necessary information in a drm_agp_info structure.
1819 int drmAgpVersionMajor(int fd)
1825 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1827 return i.agp_version_major;
1832 * Get AGP driver minor version number.
1834 * \param fd file descriptor.
1836 * \return minor version number on success, or a negative value on failure.
1839 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1840 * necessary information in a drm_agp_info structure.
1842 int drmAgpVersionMinor(int fd)
1848 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1850 return i.agp_version_minor;
1857 * \param fd file descriptor.
1859 * \return mode on success, or zero on failure.
1862 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1863 * necessary information in a drm_agp_info structure.
1865 unsigned long drmAgpGetMode(int fd)
1871 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1878 * Get AGP aperture base.
1880 * \param fd file descriptor.
1882 * \return aperture base on success, zero on failure.
1885 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1886 * necessary information in a drm_agp_info structure.
1888 unsigned long drmAgpBase(int fd)
1894 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1896 return i.aperture_base;
1901 * Get AGP aperture size.
1903 * \param fd file descriptor.
1905 * \return aperture size on success, zero on failure.
1908 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1909 * necessary information in a drm_agp_info structure.
1911 unsigned long drmAgpSize(int fd)
1917 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1919 return i.aperture_size;
1924 * Get used AGP memory.
1926 * \param fd file descriptor.
1928 * \return memory used on success, or zero on failure.
1931 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1932 * necessary information in a drm_agp_info structure.
1934 unsigned long drmAgpMemoryUsed(int fd)
1940 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1942 return i.memory_used;
1947 * Get available AGP memory.
1949 * \param fd file descriptor.
1951 * \return memory available on success, or zero on failure.
1954 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1955 * necessary information in a drm_agp_info structure.
1957 unsigned long drmAgpMemoryAvail(int fd)
1963 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1965 return i.memory_allowed;
1970 * Get hardware vendor ID.
1972 * \param fd file descriptor.
1974 * \return vendor ID on success, or zero on failure.
1977 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1978 * necessary information in a drm_agp_info structure.
1980 unsigned int drmAgpVendorId(int fd)
1986 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1993 * Get hardware device ID.
1995 * \param fd file descriptor.
1997 * \return zero on success, or zero on failure.
2000 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
2001 * necessary information in a drm_agp_info structure.
2003 unsigned int drmAgpDeviceId(int fd)
2009 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
2014 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
2016 drm_scatter_gather_t sg;
2022 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
2024 *handle = sg.handle;
2028 int drmScatterGatherFree(int fd, drm_handle_t handle)
2030 drm_scatter_gather_t sg;
2034 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
2042 * \param fd file descriptor.
2043 * \param vbl pointer to a drmVBlank structure.
2045 * \return zero on success, or a negative value on failure.
2048 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
2050 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
2052 struct timespec timeout, cur;
2055 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
2057 fprintf(stderr, "clock_gettime failed: %s\n", strerror(errno));
2063 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
2064 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
2065 if (ret && errno == EINTR) {
2066 clock_gettime(CLOCK_MONOTONIC, &cur);
2067 /* Timeout after 1s */
2068 if (cur.tv_sec > timeout.tv_sec + 1 ||
2069 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
2076 } while (ret && errno == EINTR);
2082 int drmError(int err, const char *label)
2085 case DRM_ERR_NO_DEVICE:
2086 fprintf(stderr, "%s: no device\n", label);
2088 case DRM_ERR_NO_ACCESS:
2089 fprintf(stderr, "%s: no access\n", label);
2091 case DRM_ERR_NOT_ROOT:
2092 fprintf(stderr, "%s: not root\n", label);
2094 case DRM_ERR_INVALID:
2095 fprintf(stderr, "%s: invalid args\n", label);
2100 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
2108 * Install IRQ handler.
2110 * \param fd file descriptor.
2111 * \param irq IRQ number.
2113 * \return zero on success, or a negative value on failure.
2116 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2117 * argument in a drm_control structure.
2119 int drmCtlInstHandler(int fd, int irq)
2124 ctl.func = DRM_INST_HANDLER;
2126 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2133 * Uninstall IRQ handler.
2135 * \param fd file descriptor.
2137 * \return zero on success, or a negative value on failure.
2140 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2141 * argument in a drm_control structure.
2143 int drmCtlUninstHandler(int fd)
2148 ctl.func = DRM_UNINST_HANDLER;
2150 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2155 int drmFinish(int fd, int context, drmLockFlags flags)
2160 lock.context = context;
2161 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2162 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2163 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2164 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2165 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2166 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2167 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2173 * Get IRQ from bus ID.
2175 * \param fd file descriptor.
2176 * \param busnum bus number.
2177 * \param devnum device number.
2178 * \param funcnum function number.
2180 * \return IRQ number on success, or a negative value on failure.
2183 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2184 * arguments in a drm_irq_busid structure.
2186 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2193 p.funcnum = funcnum;
2194 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2199 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2201 drmHashEntry *entry = drmGetEntry(fd);
2203 if (drmHashInsert(entry->tagTable, context, tag)) {
2204 drmHashDelete(entry->tagTable, context);
2205 drmHashInsert(entry->tagTable, context, tag);
2210 int drmDelContextTag(int fd, drm_context_t context)
2212 drmHashEntry *entry = drmGetEntry(fd);
2214 return drmHashDelete(entry->tagTable, context);
2217 void *drmGetContextTag(int fd, drm_context_t context)
2219 drmHashEntry *entry = drmGetEntry(fd);
2222 if (drmHashLookup(entry->tagTable, context, &value))
2228 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2229 drm_handle_t handle)
2231 drm_ctx_priv_map_t map;
2234 map.ctx_id = ctx_id;
2235 map.handle = (void *)(uintptr_t)handle;
2237 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2242 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2243 drm_handle_t *handle)
2245 drm_ctx_priv_map_t map;
2248 map.ctx_id = ctx_id;
2250 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2253 *handle = (drm_handle_t)(uintptr_t)map.handle;
2258 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2259 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2266 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2268 *offset = map.offset;
2272 *handle = (unsigned long)map.handle;
2277 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2278 unsigned long *magic, unsigned long *iocs)
2280 drm_client_t client;
2284 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2286 *auth = client.auth;
2289 *magic = client.magic;
2290 *iocs = client.iocs;
2294 int drmGetStats(int fd, drmStatsT *stats)
2300 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2304 memset(stats, 0, sizeof(*stats));
2305 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2309 stats->data[i].long_format = "%-20.20s"; \
2310 stats->data[i].rate_format = "%8.8s"; \
2311 stats->data[i].isvalue = 1; \
2312 stats->data[i].verbose = 0
2315 stats->data[i].long_format = "%-20.20s"; \
2316 stats->data[i].rate_format = "%5.5s"; \
2317 stats->data[i].isvalue = 0; \
2318 stats->data[i].mult_names = "kgm"; \
2319 stats->data[i].mult = 1000; \
2320 stats->data[i].verbose = 0
2323 stats->data[i].long_format = "%-20.20s"; \
2324 stats->data[i].rate_format = "%5.5s"; \
2325 stats->data[i].isvalue = 0; \
2326 stats->data[i].mult_names = "KGM"; \
2327 stats->data[i].mult = 1024; \
2328 stats->data[i].verbose = 0
2331 stats->count = s.count;
2332 for (i = 0; i < s.count; i++) {
2333 stats->data[i].value = s.data[i].value;
2334 switch (s.data[i].type) {
2335 case _DRM_STAT_LOCK:
2336 stats->data[i].long_name = "Lock";
2337 stats->data[i].rate_name = "Lock";
2340 case _DRM_STAT_OPENS:
2341 stats->data[i].long_name = "Opens";
2342 stats->data[i].rate_name = "O";
2344 stats->data[i].verbose = 1;
2346 case _DRM_STAT_CLOSES:
2347 stats->data[i].long_name = "Closes";
2348 stats->data[i].rate_name = "Lock";
2350 stats->data[i].verbose = 1;
2352 case _DRM_STAT_IOCTLS:
2353 stats->data[i].long_name = "Ioctls";
2354 stats->data[i].rate_name = "Ioc/s";
2357 case _DRM_STAT_LOCKS:
2358 stats->data[i].long_name = "Locks";
2359 stats->data[i].rate_name = "Lck/s";
2362 case _DRM_STAT_UNLOCKS:
2363 stats->data[i].long_name = "Unlocks";
2364 stats->data[i].rate_name = "Unl/s";
2368 stats->data[i].long_name = "IRQs";
2369 stats->data[i].rate_name = "IRQ/s";
2372 case _DRM_STAT_PRIMARY:
2373 stats->data[i].long_name = "Primary Bytes";
2374 stats->data[i].rate_name = "PB/s";
2377 case _DRM_STAT_SECONDARY:
2378 stats->data[i].long_name = "Secondary Bytes";
2379 stats->data[i].rate_name = "SB/s";
2383 stats->data[i].long_name = "DMA";
2384 stats->data[i].rate_name = "DMA/s";
2387 case _DRM_STAT_SPECIAL:
2388 stats->data[i].long_name = "Special DMA";
2389 stats->data[i].rate_name = "dma/s";
2392 case _DRM_STAT_MISSED:
2393 stats->data[i].long_name = "Miss";
2394 stats->data[i].rate_name = "Ms/s";
2397 case _DRM_STAT_VALUE:
2398 stats->data[i].long_name = "Value";
2399 stats->data[i].rate_name = "Value";
2402 case _DRM_STAT_BYTE:
2403 stats->data[i].long_name = "Bytes";
2404 stats->data[i].rate_name = "B/s";
2407 case _DRM_STAT_COUNT:
2409 stats->data[i].long_name = "Count";
2410 stats->data[i].rate_name = "Cnt/s";
2419 * Issue a set-version ioctl.
2421 * \param fd file descriptor.
2422 * \param drmCommandIndex command index
2423 * \param data source pointer of the data to be read and written.
2424 * \param size size of the data to be read and written.
2426 * \return zero on success, or a negative value on failure.
2429 * It issues a read-write ioctl given by
2430 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2432 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2435 drm_set_version_t sv;
2438 sv.drm_di_major = version->drm_di_major;
2439 sv.drm_di_minor = version->drm_di_minor;
2440 sv.drm_dd_major = version->drm_dd_major;
2441 sv.drm_dd_minor = version->drm_dd_minor;
2443 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2447 version->drm_di_major = sv.drm_di_major;
2448 version->drm_di_minor = sv.drm_di_minor;
2449 version->drm_dd_major = sv.drm_dd_major;
2450 version->drm_dd_minor = sv.drm_dd_minor;
2456 * Send a device-specific command.
2458 * \param fd file descriptor.
2459 * \param drmCommandIndex command index
2461 * \return zero on success, or a negative value on failure.
2464 * It issues a ioctl given by
2465 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2467 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2469 unsigned long request;
2471 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2473 if (drmIoctl(fd, request, NULL)) {
2481 * Send a device-specific read command.
2483 * \param fd file descriptor.
2484 * \param drmCommandIndex command index
2485 * \param data destination pointer of the data to be read.
2486 * \param size size of the data to be read.
2488 * \return zero on success, or a negative value on failure.
2491 * It issues a read ioctl given by
2492 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2494 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2497 unsigned long request;
2499 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2500 DRM_COMMAND_BASE + drmCommandIndex, size);
2502 if (drmIoctl(fd, request, data)) {
2510 * Send a device-specific write command.
2512 * \param fd file descriptor.
2513 * \param drmCommandIndex command index
2514 * \param data source pointer of the data to be written.
2515 * \param size size of the data to be written.
2517 * \return zero on success, or a negative value on failure.
2520 * It issues a write ioctl given by
2521 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2523 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2526 unsigned long request;
2528 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2529 DRM_COMMAND_BASE + drmCommandIndex, size);
2531 if (drmIoctl(fd, request, data)) {
2539 * Send a device-specific read-write command.
2541 * \param fd file descriptor.
2542 * \param drmCommandIndex command index
2543 * \param data source pointer of the data to be read and written.
2544 * \param size size of the data to be read and written.
2546 * \return zero on success, or a negative value on failure.
2549 * It issues a read-write ioctl given by
2550 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2552 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2555 unsigned long request;
2557 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2558 DRM_COMMAND_BASE + drmCommandIndex, size);
2560 if (drmIoctl(fd, request, data))
2565 #define DRM_MAX_FDS 16
2571 } connection[DRM_MAX_FDS];
2573 static int nr_fds = 0;
2575 int drmOpenOnce(void *unused,
2579 return drmOpenOnceWithType(BusID, newlyopened, DRM_NODE_PRIMARY);
2582 int drmOpenOnceWithType(const char *BusID, int *newlyopened, int type)
2587 for (i = 0; i < nr_fds; i++)
2588 if ((strcmp(BusID, connection[i].BusID) == 0) &&
2589 (connection[i].type == type)) {
2590 connection[i].refcount++;
2592 return connection[i].fd;
2595 fd = drmOpenWithType(NULL, BusID, type);
2596 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2599 connection[nr_fds].BusID = strdup(BusID);
2600 connection[nr_fds].fd = fd;
2601 connection[nr_fds].refcount = 1;
2602 connection[nr_fds].type = type;
2606 fprintf(stderr, "saved connection %d for %s %d\n",
2607 nr_fds, connection[nr_fds].BusID,
2608 strcmp(BusID, connection[nr_fds].BusID));
2615 void drmCloseOnce(int fd)
2619 for (i = 0; i < nr_fds; i++) {
2620 if (fd == connection[i].fd) {
2621 if (--connection[i].refcount == 0) {
2622 drmClose(connection[i].fd);
2623 free(connection[i].BusID);
2626 connection[i] = connection[nr_fds];
2634 int drmSetMaster(int fd)
2636 return drmIoctl(fd, DRM_IOCTL_SET_MASTER, NULL);
2639 int drmDropMaster(int fd)
2641 return drmIoctl(fd, DRM_IOCTL_DROP_MASTER, NULL);
2644 char *drmGetDeviceNameFromFd(int fd)
2651 /* The whole drmOpen thing is a fiasco and we need to find a way
2652 * back to just using open(2). For now, however, lets just make
2653 * things worse with even more ad hoc directory walking code to
2654 * discover the device file name. */
2659 for (i = 0; i < DRM_MAX_MINOR; i++) {
2660 snprintf(name, sizeof name, DRM_DEV_NAME, DRM_DIR_NAME, i);
2661 if (stat(name, &sbuf) == 0 && sbuf.st_rdev == d)
2664 if (i == DRM_MAX_MINOR)
2667 return strdup(name);
2670 int drmPrimeHandleToFD(int fd, uint32_t handle, uint32_t flags, int *prime_fd)
2672 struct drm_prime_handle args;
2675 args.handle = handle;
2677 ret = drmIoctl(fd, DRM_IOCTL_PRIME_HANDLE_TO_FD, &args);
2681 *prime_fd = args.fd;
2685 int drmPrimeFDToHandle(int fd, int prime_fd, uint32_t *handle)
2687 struct drm_prime_handle args;
2692 ret = drmIoctl(fd, DRM_IOCTL_PRIME_FD_TO_HANDLE, &args);
2696 *handle = args.handle;