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>
55 /* Not all systems have MAP_FAILED defined */
57 #define MAP_FAILED ((void *)-1)
62 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
75 #define DRM_MAJOR 226 /* Linux */
79 #define DRM_MAX_MINOR 16
83 * This definition needs to be changed on some systems if dev_t is a structure.
84 * If there is a header file we can get it from, there would be best.
87 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
90 #define DRM_MSG_VERBOSITY 3
92 #define DRM_NODE_CONTROL 0
93 #define DRM_NODE_RENDER 1
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 drmDebugPrint(const char *format, va_list ap)
113 return vfprintf(stderr, format, ap);
116 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
119 drmMsg(const char *format, ...)
123 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
125 va_start(ap, format);
126 if (drm_server_info) {
127 drm_server_info->debug_print(format,ap);
129 drm_debug_print(format, ap);
136 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
138 drm_debug_print = debug_msg_ptr;
141 static void *drmHashTable = NULL; /* Context switch callbacks */
143 void *drmGetHashTable(void)
148 void *drmMalloc(int size)
151 if ((pt = malloc(size)))
156 void drmFree(void *pt)
162 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
163 static char *drmStrdup(const char *s)
170 retval = malloc(strlen(s)+1);
180 * Call ioctl, restarting if it is interupted
183 drmIoctl(int fd, unsigned long request, void *arg)
188 ret = ioctl(fd, request, arg);
189 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
193 static unsigned long drmGetKeyFromFd(int fd)
202 drmHashEntry *drmGetEntry(int fd)
204 unsigned long key = drmGetKeyFromFd(fd);
209 drmHashTable = drmHashCreate();
211 if (drmHashLookup(drmHashTable, key, &value)) {
212 entry = drmMalloc(sizeof(*entry));
215 entry->tagTable = drmHashCreate();
216 drmHashInsert(drmHashTable, key, entry);
224 * Compare two busid strings
229 * \return 1 if matched.
232 * This function compares two bus ID strings. It understands the older
233 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
234 * domain, b is bus, d is device, f is function.
236 static int drmMatchBusID(const char *id1, const char *id2)
238 /* First, check if the IDs are exactly the same */
239 if (strcasecmp(id1, id2) == 0)
242 /* Try to match old/new-style PCI bus IDs. */
243 if (strncasecmp(id1, "pci", 3) == 0) {
244 unsigned int o1, b1, d1, f1;
245 unsigned int o2, b2, d2, f2;
248 ret = sscanf(id1, "pci:%04x:%02x:%02x.%u", &o1, &b1, &d1, &f1);
251 ret = sscanf(id1, "PCI:%u:%u:%u", &b1, &d1, &f1);
256 ret = sscanf(id2, "pci:%04x:%02x:%02x.%u", &o2, &b2, &d2, &f2);
259 ret = sscanf(id2, "PCI:%u:%u:%u", &b2, &d2, &f2);
264 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
273 * Handles error checking for chown call.
275 * \param path to file.
276 * \param id of the new owner.
277 * \param id of the new group.
279 * \return zero if success or -1 if failure.
282 * Checks for failure. If failure was caused by signal call chown again.
283 * If any other failure happened then it will output error mesage using
286 static int chown_check_return(const char *path, uid_t owner, gid_t group)
291 rv = chown(path, owner, group);
292 } while (rv != 0 && errno == EINTR);
297 drmMsg("Failed to change owner or group for file %s! %d: %s\n",
298 path, errno, strerror(errno));
303 * Open the DRM device, creating it if necessary.
305 * \param dev major and minor numbers of the device.
306 * \param minor minor number of the device.
308 * \return a file descriptor on success, or a negative value on error.
311 * Assembles the device name from \p minor and opens it, creating the device
312 * special file node with the major and minor numbers specified by \p dev and
313 * parent directory if necessary and was called by root.
315 static int drmOpenDevice(long dev, int minor, int type)
320 mode_t devmode = DRM_DEV_MODE, serv_mode;
321 int isroot = !geteuid();
322 uid_t user = DRM_DEV_UID;
323 gid_t group = DRM_DEV_GID, serv_group;
325 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
326 drmMsg("drmOpenDevice: node name is %s\n", buf);
328 if (drm_server_info) {
329 drm_server_info->get_perms(&serv_group, &serv_mode);
330 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
331 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
332 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
336 if (stat(DRM_DIR_NAME, &st)) {
338 return DRM_ERR_NOT_ROOT;
339 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
340 chown_check_return(DRM_DIR_NAME, 0, 0); /* root:root */
341 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
344 /* Check if the device node exists and create it if necessary. */
345 if (stat(buf, &st)) {
347 return DRM_ERR_NOT_ROOT;
349 mknod(buf, S_IFCHR | devmode, dev);
352 if (drm_server_info) {
353 chown_check_return(buf, user, group);
357 /* if we modprobed then wait for udev */
361 if (stat(DRM_DIR_NAME, &st)) {
365 if (udev_count == 50)
370 if (stat(buf, &st)) {
374 if (udev_count == 50)
381 fd = open(buf, O_RDWR, 0);
382 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
383 fd, fd < 0 ? strerror(errno) : "OK");
387 /* Check if the device node is not what we expect it to be, and recreate it
388 * and try again if so.
390 if (st.st_rdev != dev) {
392 return DRM_ERR_NOT_ROOT;
394 mknod(buf, S_IFCHR | devmode, dev);
395 if (drm_server_info) {
396 chown_check_return(buf, user, group);
400 fd = open(buf, O_RDWR, 0);
401 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
402 fd, fd < 0 ? strerror(errno) : "OK");
406 drmMsg("drmOpenDevice: Open failed\n");
413 * Open the DRM device
415 * \param minor device minor number.
416 * \param create allow to create the device if set.
418 * \return a file descriptor on success, or a negative value on error.
421 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
422 * name from \p minor and opens it.
424 static int drmOpenMinor(int minor, int create, int type)
430 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
432 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
433 if ((fd = open(buf, O_RDWR, 0)) >= 0)
440 * Determine whether the DRM kernel driver has been loaded.
442 * \return 1 if the DRM driver is loaded, 0 otherwise.
445 * Determine the presence of the kernel driver by attempting to open the 0
446 * minor and get version information. For backward compatibility with older
447 * Linux implementations, /proc/dri is also checked.
449 int drmAvailable(void)
451 drmVersionPtr version;
455 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
457 /* Try proc for backward Linux compatibility */
458 if (!access("/proc/dri/0", R_OK))
464 if ((version = drmGetVersion(fd))) {
466 drmFreeVersion(version);
475 * Open the device by bus ID.
477 * \param busid bus ID.
479 * \return a file descriptor on success, or a negative value on error.
482 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
483 * comparing the device bus ID with the one supplied.
485 * \sa drmOpenMinor() and drmGetBusid().
487 static int drmOpenByBusid(const char *busid)
494 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
495 for (i = 0; i < DRM_MAX_MINOR; i++) {
496 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
497 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
501 sv.drm_dd_major = -1; /* Don't care */
502 sv.drm_dd_minor = -1; /* Don't care */
503 drmSetInterfaceVersion(fd, &sv);
504 buf = drmGetBusid(fd);
505 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
506 if (buf && drmMatchBusID(buf, busid)) {
520 * Open the device by name.
522 * \param name driver name.
524 * \return a file descriptor on success, or a negative value on error.
527 * This function opens the first minor number that matches the driver name and
528 * isn't already in use. If it's in use it then it will already have a bus ID
531 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
533 static int drmOpenByName(const char *name)
537 drmVersionPtr version;
540 if (!drmAvailable()) {
541 if (!drm_server_info) {
545 /* try to load the kernel module now */
546 if (!drm_server_info->load_module(name)) {
547 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
554 * Open the first minor number that matches the driver name and isn't
555 * already in use. If it's in use it will have a busid assigned already.
557 for (i = 0; i < DRM_MAX_MINOR; i++) {
558 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
559 if ((version = drmGetVersion(fd))) {
560 if (!strcmp(version->name, name)) {
561 drmFreeVersion(version);
562 id = drmGetBusid(fd);
563 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
572 drmFreeVersion(version);
580 /* Backward-compatibility /proc support */
581 for (i = 0; i < 8; i++) {
582 char proc_name[64], buf[512];
583 char *driver, *pt, *devstring;
586 sprintf(proc_name, "/proc/dri/%d/name", i);
587 if ((fd = open(proc_name, 0, 0)) >= 0) {
588 retcode = read(fd, buf, sizeof(buf)-1);
591 buf[retcode-1] = '\0';
592 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
594 if (*pt) { /* Device is next */
596 if (!strcmp(driver, name)) { /* Match */
597 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
599 if (*pt) { /* Found busid */
600 return drmOpenByBusid(++pt);
601 } else { /* No busid */
602 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
616 * Open the DRM device.
618 * Looks up the specified name and bus ID, and opens the device found. The
619 * entry in /dev/dri is created if necessary and if called by root.
621 * \param name driver name. Not referenced if bus ID is supplied.
622 * \param busid bus ID. Zero if not known.
624 * \return a file descriptor on success, or a negative value on error.
627 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
630 int drmOpen(const char *name, const char *busid)
632 if (!drmAvailable() && name != NULL && drm_server_info) {
633 /* try to load the kernel */
634 if (!drm_server_info->load_module(name)) {
635 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
641 int fd = drmOpenByBusid(busid);
647 return drmOpenByName(name);
652 int drmOpenControl(int minor)
654 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
658 * Free the version information returned by drmGetVersion().
660 * \param v pointer to the version information.
663 * It frees the memory pointed by \p %v as well as all the non-null strings
666 void drmFreeVersion(drmVersionPtr v)
678 * Free the non-public version information returned by the kernel.
680 * \param v pointer to the version information.
683 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
684 * the non-null strings pointers in it.
686 static void drmFreeKernelVersion(drm_version_t *v)
698 * Copy version information.
700 * \param d destination pointer.
701 * \param s source pointer.
704 * Used by drmGetVersion() to translate the information returned by the ioctl
705 * interface in a private structure into the public structure counterpart.
707 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
709 d->version_major = s->version_major;
710 d->version_minor = s->version_minor;
711 d->version_patchlevel = s->version_patchlevel;
712 d->name_len = s->name_len;
713 d->name = drmStrdup(s->name);
714 d->date_len = s->date_len;
715 d->date = drmStrdup(s->date);
716 d->desc_len = s->desc_len;
717 d->desc = drmStrdup(s->desc);
722 * Query the driver version information.
724 * \param fd file descriptor.
726 * \return pointer to a drmVersion structure which should be freed with
729 * \note Similar information is available via /proc/dri.
732 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
733 * first with zeros to get the string lengths, and then the actually strings.
734 * It also null-terminates them since they might not be already.
736 drmVersionPtr drmGetVersion(int fd)
738 drmVersionPtr retval;
739 drm_version_t *version = drmMalloc(sizeof(*version));
741 version->name_len = 0;
742 version->name = NULL;
743 version->date_len = 0;
744 version->date = NULL;
745 version->desc_len = 0;
746 version->desc = NULL;
748 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
749 drmFreeKernelVersion(version);
753 if (version->name_len)
754 version->name = drmMalloc(version->name_len + 1);
755 if (version->date_len)
756 version->date = drmMalloc(version->date_len + 1);
757 if (version->desc_len)
758 version->desc = drmMalloc(version->desc_len + 1);
760 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
761 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
762 drmFreeKernelVersion(version);
766 /* The results might not be null-terminated strings, so terminate them. */
767 if (version->name_len) version->name[version->name_len] = '\0';
768 if (version->date_len) version->date[version->date_len] = '\0';
769 if (version->desc_len) version->desc[version->desc_len] = '\0';
771 retval = drmMalloc(sizeof(*retval));
772 drmCopyVersion(retval, version);
773 drmFreeKernelVersion(version);
779 * Get version information for the DRM user space library.
781 * This version number is driver independent.
783 * \param fd file descriptor.
785 * \return version information.
788 * This function allocates and fills a drm_version structure with a hard coded
791 drmVersionPtr drmGetLibVersion(int fd)
793 drm_version_t *version = drmMalloc(sizeof(*version));
796 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
797 * revision 1.0.x = original DRM interface with no drmGetLibVersion
798 * entry point and many drm<Device> extensions
799 * revision 1.1.x = added drmCommand entry points for device extensions
800 * added drmGetLibVersion to identify libdrm.a version
801 * revision 1.2.x = added drmSetInterfaceVersion
802 * modified drmOpen to handle both busid and name
803 * revision 1.3.x = added server + memory manager
805 version->version_major = 1;
806 version->version_minor = 3;
807 version->version_patchlevel = 0;
809 return (drmVersionPtr)version;
814 * Free the bus ID information.
816 * \param busid bus ID information string as given by drmGetBusid().
819 * This function is just frees the memory pointed by \p busid.
821 void drmFreeBusid(const char *busid)
823 drmFree((void *)busid);
828 * Get the bus ID of the device.
830 * \param fd file descriptor.
832 * \return bus ID string.
835 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
836 * get the string length and data, passing the arguments in a drm_unique
839 char *drmGetBusid(int fd)
846 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
848 u.unique = drmMalloc(u.unique_len + 1);
849 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
851 u.unique[u.unique_len] = '\0';
858 * Set the bus ID of the device.
860 * \param fd file descriptor.
861 * \param busid bus ID string.
863 * \return zero on success, negative on failure.
866 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
867 * the arguments in a drm_unique structure.
869 int drmSetBusid(int fd, const char *busid)
873 u.unique = (char *)busid;
874 u.unique_len = strlen(busid);
876 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
882 int drmGetMagic(int fd, drm_magic_t * magic)
887 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
893 int drmAuthMagic(int fd, drm_magic_t magic)
898 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
904 * Specifies a range of memory that is available for mapping by a
907 * \param fd file descriptor.
908 * \param offset usually the physical address. The actual meaning depends of
909 * the \p type parameter. See below.
910 * \param size of the memory in bytes.
911 * \param type type of the memory to be mapped.
912 * \param flags combination of several flags to modify the function actions.
913 * \param handle will be set to a value that may be used as the offset
914 * parameter for mmap().
916 * \return zero on success or a negative value on error.
918 * \par Mapping the frame buffer
919 * For the frame buffer
920 * - \p offset will be the physical address of the start of the frame buffer,
921 * - \p size will be the size of the frame buffer in bytes, and
922 * - \p type will be DRM_FRAME_BUFFER.
925 * The area mapped will be uncached. If MTRR support is available in the
926 * kernel, the frame buffer area will be set to write combining.
928 * \par Mapping the MMIO register area
929 * For the MMIO register area,
930 * - \p offset will be the physical address of the start of the register area,
931 * - \p size will be the size of the register area bytes, and
932 * - \p type will be DRM_REGISTERS.
934 * The area mapped will be uncached.
936 * \par Mapping the SAREA
938 * - \p offset will be ignored and should be set to zero,
939 * - \p size will be the desired size of the SAREA in bytes,
940 * - \p type will be DRM_SHM.
943 * A shared memory area of the requested size will be created and locked in
944 * kernel memory. This area may be mapped into client-space by using the handle
947 * \note May only be called by root.
950 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
951 * the arguments in a drm_map structure.
953 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
954 drmMapFlags flags, drm_handle_t *handle)
963 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
966 *handle = (drm_handle_t)map.handle;
970 int drmRmMap(int fd, drm_handle_t handle)
974 map.handle = (void *)handle;
976 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
982 * Make buffers available for DMA transfers.
984 * \param fd file descriptor.
985 * \param count number of buffers.
986 * \param size size of each buffer.
987 * \param flags buffer allocation flags.
988 * \param agp_offset offset in the AGP aperture
990 * \return number of buffers allocated, negative on error.
993 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
997 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
1000 drm_buf_desc_t request;
1002 request.count = count;
1003 request.size = size;
1004 request.low_mark = 0;
1005 request.high_mark = 0;
1006 request.flags = flags;
1007 request.agp_start = agp_offset;
1009 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
1011 return request.count;
1014 int drmMarkBufs(int fd, double low, double high)
1016 drm_buf_info_t info;
1022 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1028 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1031 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1032 int retval = -errno;
1037 for (i = 0; i < info.count; i++) {
1038 info.list[i].low_mark = low * info.list[i].count;
1039 info.list[i].high_mark = high * info.list[i].count;
1040 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1041 int retval = -errno;
1054 * \param fd file descriptor.
1055 * \param count number of buffers to free.
1056 * \param list list of buffers to be freed.
1058 * \return zero on success, or a negative value on failure.
1060 * \note This function is primarily used for debugging.
1063 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1064 * the arguments in a drm_buf_free structure.
1066 int drmFreeBufs(int fd, int count, int *list)
1068 drm_buf_free_t request;
1070 request.count = count;
1071 request.list = list;
1072 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1081 * \param fd file descriptor.
1084 * This function closes the file descriptor.
1086 int drmClose(int fd)
1088 unsigned long key = drmGetKeyFromFd(fd);
1089 drmHashEntry *entry = drmGetEntry(fd);
1091 drmHashDestroy(entry->tagTable);
1094 entry->tagTable = NULL;
1096 drmHashDelete(drmHashTable, key);
1104 * Map a region of memory.
1106 * \param fd file descriptor.
1107 * \param handle handle returned by drmAddMap().
1108 * \param size size in bytes. Must match the size used by drmAddMap().
1109 * \param address will contain the user-space virtual address where the mapping
1112 * \return zero on success, or a negative value on failure.
1115 * This function is a wrapper for mmap().
1117 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1119 static unsigned long pagesize_mask = 0;
1125 pagesize_mask = getpagesize() - 1;
1127 size = (size + pagesize_mask) & ~pagesize_mask;
1129 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1130 if (*address == MAP_FAILED)
1137 * Unmap mappings obtained with drmMap().
1139 * \param address address as given by drmMap().
1140 * \param size size in bytes. Must match the size used by drmMap().
1142 * \return zero on success, or a negative value on failure.
1145 * This function is a wrapper for munmap().
1147 int drmUnmap(drmAddress address, drmSize size)
1149 return munmap(address, size);
1152 drmBufInfoPtr drmGetBufInfo(int fd)
1154 drm_buf_info_t info;
1155 drmBufInfoPtr retval;
1161 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1165 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1168 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1173 retval = drmMalloc(sizeof(*retval));
1174 retval->count = info.count;
1175 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1176 for (i = 0; i < info.count; i++) {
1177 retval->list[i].count = info.list[i].count;
1178 retval->list[i].size = info.list[i].size;
1179 retval->list[i].low_mark = info.list[i].low_mark;
1180 retval->list[i].high_mark = info.list[i].high_mark;
1189 * Map all DMA buffers into client-virtual space.
1191 * \param fd file descriptor.
1193 * \return a pointer to a ::drmBufMap structure.
1195 * \note The client may not use these buffers until obtaining buffer indices
1199 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1200 * information about the buffers in a drm_buf_map structure into the
1201 * client-visible data structures.
1203 drmBufMapPtr drmMapBufs(int fd)
1206 drmBufMapPtr retval;
1211 bufs.virtual = NULL;
1212 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1218 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1221 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1226 retval = drmMalloc(sizeof(*retval));
1227 retval->count = bufs.count;
1228 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1229 for (i = 0; i < bufs.count; i++) {
1230 retval->list[i].idx = bufs.list[i].idx;
1231 retval->list[i].total = bufs.list[i].total;
1232 retval->list[i].used = 0;
1233 retval->list[i].address = bufs.list[i].address;
1243 * Unmap buffers allocated with drmMapBufs().
1245 * \return zero on success, or negative value on failure.
1248 * Calls munmap() for every buffer stored in \p bufs and frees the
1249 * memory allocated by drmMapBufs().
1251 int drmUnmapBufs(drmBufMapPtr bufs)
1255 for (i = 0; i < bufs->count; i++) {
1256 munmap(bufs->list[i].address, bufs->list[i].total);
1259 drmFree(bufs->list);
1266 #define DRM_DMA_RETRY 16
1269 * Reserve DMA buffers.
1271 * \param fd file descriptor.
1274 * \return zero on success, or a negative value on failure.
1277 * Assemble the arguments into a drm_dma structure and keeps issuing the
1278 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1280 int drmDMA(int fd, drmDMAReqPtr request)
1285 dma.context = request->context;
1286 dma.send_count = request->send_count;
1287 dma.send_indices = request->send_list;
1288 dma.send_sizes = request->send_sizes;
1289 dma.flags = request->flags;
1290 dma.request_count = request->request_count;
1291 dma.request_size = request->request_size;
1292 dma.request_indices = request->request_list;
1293 dma.request_sizes = request->request_sizes;
1294 dma.granted_count = 0;
1297 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1298 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1301 request->granted_count = dma.granted_count;
1310 * Obtain heavyweight hardware lock.
1312 * \param fd file descriptor.
1313 * \param context context.
1314 * \param flags flags that determine the sate of the hardware when the function
1317 * \return always zero.
1320 * This function translates the arguments into a drm_lock structure and issue
1321 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1323 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1327 lock.context = context;
1329 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1330 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1331 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1332 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1333 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1334 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1336 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1342 * Release the hardware lock.
1344 * \param fd file descriptor.
1345 * \param context context.
1347 * \return zero on success, or a negative value on failure.
1350 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1351 * argument in a drm_lock structure.
1353 int drmUnlock(int fd, drm_context_t context)
1357 lock.context = context;
1359 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1362 drm_context_t *drmGetReservedContextList(int fd, int *count)
1366 drm_context_t * retval;
1370 res.contexts = NULL;
1371 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1377 if (!(list = drmMalloc(res.count * sizeof(*list))))
1379 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1384 res.contexts = list;
1385 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1388 for (i = 0; i < res.count; i++)
1389 retval[i] = list[i].handle;
1396 void drmFreeReservedContextList(drm_context_t *pt)
1404 * Used by the X server during GLXContext initialization. This causes
1405 * per-context kernel-level resources to be allocated.
1407 * \param fd file descriptor.
1408 * \param handle is set on success. To be used by the client when requesting DMA
1409 * dispatch with drmDMA().
1411 * \return zero on success, or a negative value on failure.
1413 * \note May only be called by root.
1416 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1417 * argument in a drm_ctx structure.
1419 int drmCreateContext(int fd, drm_context_t *handle)
1423 ctx.flags = 0; /* Modified with functions below */
1424 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1426 *handle = ctx.handle;
1430 int drmSwitchToContext(int fd, drm_context_t context)
1434 ctx.handle = context;
1435 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1440 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1445 * Context preserving means that no context switches are done between DMA
1446 * buffers from one context and the next. This is suitable for use in the
1447 * X server (which promises to maintain hardware context), or in the
1448 * client-side library when buffers are swapped on behalf of two threads.
1450 ctx.handle = context;
1452 if (flags & DRM_CONTEXT_PRESERVED)
1453 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1454 if (flags & DRM_CONTEXT_2DONLY)
1455 ctx.flags |= _DRM_CONTEXT_2DONLY;
1456 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1461 int drmGetContextFlags(int fd, drm_context_t context,
1462 drm_context_tFlagsPtr flags)
1466 ctx.handle = context;
1467 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1470 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1471 *flags |= DRM_CONTEXT_PRESERVED;
1472 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1473 *flags |= DRM_CONTEXT_2DONLY;
1480 * Free any kernel-level resources allocated with drmCreateContext() associated
1483 * \param fd file descriptor.
1484 * \param handle handle given by drmCreateContext().
1486 * \return zero on success, or a negative value on failure.
1488 * \note May only be called by root.
1491 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1492 * argument in a drm_ctx structure.
1494 int drmDestroyContext(int fd, drm_context_t handle)
1497 ctx.handle = handle;
1498 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1503 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1506 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1508 *handle = draw.handle;
1512 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1515 draw.handle = handle;
1516 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1521 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1522 drm_drawable_info_type_t type, unsigned int num,
1525 drm_update_draw_t update;
1527 update.handle = handle;
1530 update.data = (unsigned long long)(unsigned long)data;
1532 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1539 * Acquire the AGP device.
1541 * Must be called before any of the other AGP related calls.
1543 * \param fd file descriptor.
1545 * \return zero on success, or a negative value on failure.
1548 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1550 int drmAgpAcquire(int fd)
1552 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1559 * Release the AGP device.
1561 * \param fd file descriptor.
1563 * \return zero on success, or a negative value on failure.
1566 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1568 int drmAgpRelease(int fd)
1570 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1579 * \param fd file descriptor.
1580 * \param mode AGP mode.
1582 * \return zero on success, or a negative value on failure.
1585 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1586 * argument in a drm_agp_mode structure.
1588 int drmAgpEnable(int fd, unsigned long mode)
1593 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1600 * Allocate a chunk of AGP memory.
1602 * \param fd file descriptor.
1603 * \param size requested memory size in bytes. Will be rounded to page boundary.
1604 * \param type type of memory to allocate.
1605 * \param address if not zero, will be set to the physical address of the
1607 * \param handle on success will be set to a handle of the allocated memory.
1609 * \return zero on success, or a negative value on failure.
1612 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1613 * arguments in a drm_agp_buffer structure.
1615 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1616 unsigned long *address, drm_handle_t *handle)
1620 *handle = DRM_AGP_NO_HANDLE;
1624 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1627 *address = b.physical;
1634 * Free a chunk of AGP memory.
1636 * \param fd file descriptor.
1637 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1639 * \return zero on success, or a negative value on failure.
1642 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1643 * argument in a drm_agp_buffer structure.
1645 int drmAgpFree(int fd, drm_handle_t handle)
1651 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1658 * Bind a chunk of AGP memory.
1660 * \param fd file descriptor.
1661 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1662 * \param offset offset in bytes. It will round to page boundary.
1664 * \return zero on success, or a negative value on failure.
1667 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1668 * argument in a drm_agp_binding structure.
1670 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1672 drm_agp_binding_t b;
1676 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1683 * Unbind a chunk of AGP memory.
1685 * \param fd file descriptor.
1686 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1688 * \return zero on success, or a negative value on failure.
1691 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1692 * the argument in a drm_agp_binding structure.
1694 int drmAgpUnbind(int fd, drm_handle_t handle)
1696 drm_agp_binding_t b;
1700 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1707 * Get AGP driver major version number.
1709 * \param fd file descriptor.
1711 * \return major version number on success, or a negative value on failure..
1714 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1715 * necessary information in a drm_agp_info structure.
1717 int drmAgpVersionMajor(int fd)
1721 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1723 return i.agp_version_major;
1728 * Get AGP driver minor version number.
1730 * \param fd file descriptor.
1732 * \return minor version number on success, or a negative value on failure.
1735 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1736 * necessary information in a drm_agp_info structure.
1738 int drmAgpVersionMinor(int fd)
1742 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1744 return i.agp_version_minor;
1751 * \param fd file descriptor.
1753 * \return mode on success, or zero on failure.
1756 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1757 * necessary information in a drm_agp_info structure.
1759 unsigned long drmAgpGetMode(int fd)
1763 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1770 * Get AGP aperture base.
1772 * \param fd file descriptor.
1774 * \return aperture base on success, zero on failure.
1777 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1778 * necessary information in a drm_agp_info structure.
1780 unsigned long drmAgpBase(int fd)
1784 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1786 return i.aperture_base;
1791 * Get AGP aperture size.
1793 * \param fd file descriptor.
1795 * \return aperture size on success, zero on failure.
1798 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1799 * necessary information in a drm_agp_info structure.
1801 unsigned long drmAgpSize(int fd)
1805 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1807 return i.aperture_size;
1812 * Get used AGP memory.
1814 * \param fd file descriptor.
1816 * \return memory used on success, or zero on failure.
1819 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1820 * necessary information in a drm_agp_info structure.
1822 unsigned long drmAgpMemoryUsed(int fd)
1826 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1828 return i.memory_used;
1833 * Get available AGP memory.
1835 * \param fd file descriptor.
1837 * \return memory available on success, or zero on failure.
1840 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1841 * necessary information in a drm_agp_info structure.
1843 unsigned long drmAgpMemoryAvail(int fd)
1847 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1849 return i.memory_allowed;
1854 * Get hardware vendor ID.
1856 * \param fd file descriptor.
1858 * \return vendor ID on success, or zero on failure.
1861 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1862 * necessary information in a drm_agp_info structure.
1864 unsigned int drmAgpVendorId(int fd)
1868 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1875 * Get hardware device ID.
1877 * \param fd file descriptor.
1879 * \return zero on success, or zero on failure.
1882 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1883 * necessary information in a drm_agp_info structure.
1885 unsigned int drmAgpDeviceId(int fd)
1889 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1894 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1896 drm_scatter_gather_t sg;
1901 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1903 *handle = sg.handle;
1907 int drmScatterGatherFree(int fd, drm_handle_t handle)
1909 drm_scatter_gather_t sg;
1913 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1921 * \param fd file descriptor.
1922 * \param vbl pointer to a drmVBlank structure.
1924 * \return zero on success, or a negative value on failure.
1927 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1929 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1931 struct timespec timeout, cur;
1934 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1936 fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret));
1942 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1943 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1944 if (ret && errno == EINTR) {
1945 clock_gettime(CLOCK_MONOTONIC, &cur);
1946 /* Timeout after 1s */
1947 if (cur.tv_sec > timeout.tv_sec + 1 ||
1948 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1955 } while (ret && errno == EINTR);
1961 int drmError(int err, const char *label)
1964 case DRM_ERR_NO_DEVICE:
1965 fprintf(stderr, "%s: no device\n", label);
1967 case DRM_ERR_NO_ACCESS:
1968 fprintf(stderr, "%s: no access\n", label);
1970 case DRM_ERR_NOT_ROOT:
1971 fprintf(stderr, "%s: not root\n", label);
1973 case DRM_ERR_INVALID:
1974 fprintf(stderr, "%s: invalid args\n", label);
1979 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1987 * Install IRQ handler.
1989 * \param fd file descriptor.
1990 * \param irq IRQ number.
1992 * \return zero on success, or a negative value on failure.
1995 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1996 * argument in a drm_control structure.
1998 int drmCtlInstHandler(int fd, int irq)
2002 ctl.func = DRM_INST_HANDLER;
2004 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2011 * Uninstall IRQ handler.
2013 * \param fd file descriptor.
2015 * \return zero on success, or a negative value on failure.
2018 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
2019 * argument in a drm_control structure.
2021 int drmCtlUninstHandler(int fd)
2025 ctl.func = DRM_UNINST_HANDLER;
2027 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2032 int drmFinish(int fd, int context, drmLockFlags flags)
2036 lock.context = context;
2038 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2039 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2040 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2041 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2042 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2043 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2044 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2050 * Get IRQ from bus ID.
2052 * \param fd file descriptor.
2053 * \param busnum bus number.
2054 * \param devnum device number.
2055 * \param funcnum function number.
2057 * \return IRQ number on success, or a negative value on failure.
2060 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2061 * arguments in a drm_irq_busid structure.
2063 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2069 p.funcnum = funcnum;
2070 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2075 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2077 drmHashEntry *entry = drmGetEntry(fd);
2079 if (drmHashInsert(entry->tagTable, context, tag)) {
2080 drmHashDelete(entry->tagTable, context);
2081 drmHashInsert(entry->tagTable, context, tag);
2086 int drmDelContextTag(int fd, drm_context_t context)
2088 drmHashEntry *entry = drmGetEntry(fd);
2090 return drmHashDelete(entry->tagTable, context);
2093 void *drmGetContextTag(int fd, drm_context_t context)
2095 drmHashEntry *entry = drmGetEntry(fd);
2098 if (drmHashLookup(entry->tagTable, context, &value))
2104 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2105 drm_handle_t handle)
2107 drm_ctx_priv_map_t map;
2109 map.ctx_id = ctx_id;
2110 map.handle = (void *)handle;
2112 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2117 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2118 drm_handle_t *handle)
2120 drm_ctx_priv_map_t map;
2122 map.ctx_id = ctx_id;
2124 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2127 *handle = (drm_handle_t)map.handle;
2132 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2133 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2139 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2141 *offset = map.offset;
2145 *handle = (unsigned long)map.handle;
2150 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2151 unsigned long *magic, unsigned long *iocs)
2153 drm_client_t client;
2156 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2158 *auth = client.auth;
2161 *magic = client.magic;
2162 *iocs = client.iocs;
2166 int drmGetStats(int fd, drmStatsT *stats)
2171 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2175 memset(stats, 0, sizeof(*stats));
2176 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2180 stats->data[i].long_format = "%-20.20s"; \
2181 stats->data[i].rate_format = "%8.8s"; \
2182 stats->data[i].isvalue = 1; \
2183 stats->data[i].verbose = 0
2186 stats->data[i].long_format = "%-20.20s"; \
2187 stats->data[i].rate_format = "%5.5s"; \
2188 stats->data[i].isvalue = 0; \
2189 stats->data[i].mult_names = "kgm"; \
2190 stats->data[i].mult = 1000; \
2191 stats->data[i].verbose = 0
2194 stats->data[i].long_format = "%-20.20s"; \
2195 stats->data[i].rate_format = "%5.5s"; \
2196 stats->data[i].isvalue = 0; \
2197 stats->data[i].mult_names = "KGM"; \
2198 stats->data[i].mult = 1024; \
2199 stats->data[i].verbose = 0
2202 stats->count = s.count;
2203 for (i = 0; i < s.count; i++) {
2204 stats->data[i].value = s.data[i].value;
2205 switch (s.data[i].type) {
2206 case _DRM_STAT_LOCK:
2207 stats->data[i].long_name = "Lock";
2208 stats->data[i].rate_name = "Lock";
2211 case _DRM_STAT_OPENS:
2212 stats->data[i].long_name = "Opens";
2213 stats->data[i].rate_name = "O";
2215 stats->data[i].verbose = 1;
2217 case _DRM_STAT_CLOSES:
2218 stats->data[i].long_name = "Closes";
2219 stats->data[i].rate_name = "Lock";
2221 stats->data[i].verbose = 1;
2223 case _DRM_STAT_IOCTLS:
2224 stats->data[i].long_name = "Ioctls";
2225 stats->data[i].rate_name = "Ioc/s";
2228 case _DRM_STAT_LOCKS:
2229 stats->data[i].long_name = "Locks";
2230 stats->data[i].rate_name = "Lck/s";
2233 case _DRM_STAT_UNLOCKS:
2234 stats->data[i].long_name = "Unlocks";
2235 stats->data[i].rate_name = "Unl/s";
2239 stats->data[i].long_name = "IRQs";
2240 stats->data[i].rate_name = "IRQ/s";
2243 case _DRM_STAT_PRIMARY:
2244 stats->data[i].long_name = "Primary Bytes";
2245 stats->data[i].rate_name = "PB/s";
2248 case _DRM_STAT_SECONDARY:
2249 stats->data[i].long_name = "Secondary Bytes";
2250 stats->data[i].rate_name = "SB/s";
2254 stats->data[i].long_name = "DMA";
2255 stats->data[i].rate_name = "DMA/s";
2258 case _DRM_STAT_SPECIAL:
2259 stats->data[i].long_name = "Special DMA";
2260 stats->data[i].rate_name = "dma/s";
2263 case _DRM_STAT_MISSED:
2264 stats->data[i].long_name = "Miss";
2265 stats->data[i].rate_name = "Ms/s";
2268 case _DRM_STAT_VALUE:
2269 stats->data[i].long_name = "Value";
2270 stats->data[i].rate_name = "Value";
2273 case _DRM_STAT_BYTE:
2274 stats->data[i].long_name = "Bytes";
2275 stats->data[i].rate_name = "B/s";
2278 case _DRM_STAT_COUNT:
2280 stats->data[i].long_name = "Count";
2281 stats->data[i].rate_name = "Cnt/s";
2290 * Issue a set-version ioctl.
2292 * \param fd file descriptor.
2293 * \param drmCommandIndex command index
2294 * \param data source pointer of the data to be read and written.
2295 * \param size size of the data to be read and written.
2297 * \return zero on success, or a negative value on failure.
2300 * It issues a read-write ioctl given by
2301 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2303 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2306 drm_set_version_t sv;
2308 sv.drm_di_major = version->drm_di_major;
2309 sv.drm_di_minor = version->drm_di_minor;
2310 sv.drm_dd_major = version->drm_dd_major;
2311 sv.drm_dd_minor = version->drm_dd_minor;
2313 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2317 version->drm_di_major = sv.drm_di_major;
2318 version->drm_di_minor = sv.drm_di_minor;
2319 version->drm_dd_major = sv.drm_dd_major;
2320 version->drm_dd_minor = sv.drm_dd_minor;
2326 * Send a device-specific command.
2328 * \param fd file descriptor.
2329 * \param drmCommandIndex command index
2331 * \return zero on success, or a negative value on failure.
2334 * It issues a ioctl given by
2335 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2337 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2339 void *data = NULL; /* dummy */
2340 unsigned long request;
2342 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2344 if (drmIoctl(fd, request, data)) {
2352 * Send a device-specific read command.
2354 * \param fd file descriptor.
2355 * \param drmCommandIndex command index
2356 * \param data destination pointer of the data to be read.
2357 * \param size size of the data to be read.
2359 * \return zero on success, or a negative value on failure.
2362 * It issues a read ioctl given by
2363 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2365 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2368 unsigned long request;
2370 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2371 DRM_COMMAND_BASE + drmCommandIndex, size);
2373 if (drmIoctl(fd, request, data)) {
2381 * Send a device-specific write command.
2383 * \param fd file descriptor.
2384 * \param drmCommandIndex command index
2385 * \param data source pointer of the data to be written.
2386 * \param size size of the data to be written.
2388 * \return zero on success, or a negative value on failure.
2391 * It issues a write ioctl given by
2392 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2394 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2397 unsigned long request;
2399 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2400 DRM_COMMAND_BASE + drmCommandIndex, size);
2402 if (drmIoctl(fd, request, data)) {
2410 * Send a device-specific read-write command.
2412 * \param fd file descriptor.
2413 * \param drmCommandIndex command index
2414 * \param data source pointer of the data to be read and written.
2415 * \param size size of the data to be read and written.
2417 * \return zero on success, or a negative value on failure.
2420 * It issues a read-write ioctl given by
2421 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2423 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2426 unsigned long request;
2428 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2429 DRM_COMMAND_BASE + drmCommandIndex, size);
2431 if (drmIoctl(fd, request, data))
2436 #define DRM_MAX_FDS 16
2441 } connection[DRM_MAX_FDS];
2443 static int nr_fds = 0;
2445 int drmOpenOnce(void *unused,
2452 for (i = 0; i < nr_fds; i++)
2453 if (strcmp(BusID, connection[i].BusID) == 0) {
2454 connection[i].refcount++;
2456 return connection[i].fd;
2459 fd = drmOpen(unused, BusID);
2460 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2463 connection[nr_fds].BusID = strdup(BusID);
2464 connection[nr_fds].fd = fd;
2465 connection[nr_fds].refcount = 1;
2469 fprintf(stderr, "saved connection %d for %s %d\n",
2470 nr_fds, connection[nr_fds].BusID,
2471 strcmp(BusID, connection[nr_fds].BusID));
2478 void drmCloseOnce(int fd)
2482 for (i = 0; i < nr_fds; i++) {
2483 if (fd == connection[i].fd) {
2484 if (--connection[i].refcount == 0) {
2485 drmClose(connection[i].fd);
2486 free(connection[i].BusID);
2489 connection[i] = connection[nr_fds];
2497 int drmSetMaster(int fd)
2501 fprintf(stderr,"Setting master \n");
2502 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2506 int drmDropMaster(int fd)
2509 fprintf(stderr,"Dropping master \n");
2510 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);