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.
45 #include <sys/types.h>
47 #define stat_t struct stat
48 #include <sys/ioctl.h>
53 /* Not all systems have MAP_FAILED defined */
55 #define MAP_FAILED ((void *)-1)
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
73 #define DRM_MAJOR 226 /* Linux */
77 #define DRM_MAX_MINOR 16
81 * This definition needs to be changed on some systems if dev_t is a structure.
82 * If there is a header file we can get it from, there would be best.
85 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #define DRM_MSG_VERBOSITY 3
90 #define DRM_NODE_CONTROL 0
91 #define DRM_NODE_RENDER 1
93 static drmServerInfoPtr drm_server_info;
95 void drmSetServerInfo(drmServerInfoPtr info)
97 drm_server_info = info;
101 * Output a message to stderr.
103 * \param format printf() like format string.
106 * This function is a wrapper around vfprintf().
109 static int drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
117 drmMsg(const char *format, ...)
121 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
123 va_start(ap, format);
124 if (drm_server_info) {
125 drm_server_info->debug_print(format,ap);
127 drm_debug_print(format, ap);
134 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
136 drm_debug_print = debug_msg_ptr;
139 static void *drmHashTable = NULL; /* Context switch callbacks */
141 void *drmGetHashTable(void)
146 void *drmMalloc(int size)
149 if ((pt = malloc(size)))
154 void drmFree(void *pt)
160 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
161 static char *drmStrdup(const char *s)
168 retval = malloc(strlen(s)+1);
178 * Call ioctl, restarting if it is interupted
181 drmIoctl(int fd, unsigned long request, void *arg)
186 ret = ioctl(fd, request, arg);
187 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
191 static unsigned long drmGetKeyFromFd(int fd)
200 drmHashEntry *drmGetEntry(int fd)
202 unsigned long key = drmGetKeyFromFd(fd);
207 drmHashTable = drmHashCreate();
209 if (drmHashLookup(drmHashTable, key, &value)) {
210 entry = drmMalloc(sizeof(*entry));
213 entry->tagTable = drmHashCreate();
214 drmHashInsert(drmHashTable, key, entry);
222 * Compare two busid strings
227 * \return 1 if matched.
230 * This function compares two bus ID strings. It understands the older
231 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
232 * domain, b is bus, d is device, f is function.
234 static int drmMatchBusID(const char *id1, const char *id2)
236 /* First, check if the IDs are exactly the same */
237 if (strcasecmp(id1, id2) == 0)
240 /* Try to match old/new-style PCI bus IDs. */
241 if (strncasecmp(id1, "pci", 3) == 0) {
246 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
249 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
254 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
257 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
262 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
271 * Open the DRM device, creating it if necessary.
273 * \param dev major and minor numbers of the device.
274 * \param minor minor number of the device.
276 * \return a file descriptor on success, or a negative value on error.
279 * Assembles the device name from \p minor and opens it, creating the device
280 * special file node with the major and minor numbers specified by \p dev and
281 * parent directory if necessary and was called by root.
283 static int drmOpenDevice(long dev, int minor, int type)
288 mode_t devmode = DRM_DEV_MODE, serv_mode;
289 int isroot = !geteuid();
290 uid_t user = DRM_DEV_UID;
291 gid_t group = DRM_DEV_GID, serv_group;
293 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
294 drmMsg("drmOpenDevice: node name is %s\n", buf);
296 if (drm_server_info) {
297 drm_server_info->get_perms(&serv_group, &serv_mode);
298 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
299 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
300 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
304 if (stat(DRM_DIR_NAME, &st)) {
306 return DRM_ERR_NOT_ROOT;
307 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
308 chown(DRM_DIR_NAME, 0, 0); /* root:root */
309 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
312 /* Check if the device node exists and create it if necessary. */
313 if (stat(buf, &st)) {
315 return DRM_ERR_NOT_ROOT;
317 mknod(buf, S_IFCHR | devmode, dev);
320 if (drm_server_info) {
321 chown(buf, user, group);
325 /* if we modprobed then wait for udev */
329 if (stat(DRM_DIR_NAME, &st)) {
333 if (udev_count == 50)
338 if (stat(buf, &st)) {
342 if (udev_count == 50)
349 fd = open(buf, O_RDWR, 0);
350 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
351 fd, fd < 0 ? strerror(errno) : "OK");
355 /* Check if the device node is not what we expect it to be, and recreate it
356 * and try again if so.
358 if (st.st_rdev != dev) {
360 return DRM_ERR_NOT_ROOT;
362 mknod(buf, S_IFCHR | devmode, dev);
363 if (drm_server_info) {
364 chown(buf, user, group);
368 fd = open(buf, O_RDWR, 0);
369 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
370 fd, fd < 0 ? strerror(errno) : "OK");
374 drmMsg("drmOpenDevice: Open failed\n");
381 * Open the DRM device
383 * \param minor device minor number.
384 * \param create allow to create the device if set.
386 * \return a file descriptor on success, or a negative value on error.
389 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
390 * name from \p minor and opens it.
392 static int drmOpenMinor(int minor, int create, int type)
398 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
400 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
401 if ((fd = open(buf, O_RDWR, 0)) >= 0)
408 * Determine whether the DRM kernel driver has been loaded.
410 * \return 1 if the DRM driver is loaded, 0 otherwise.
413 * Determine the presence of the kernel driver by attempting to open the 0
414 * minor and get version information. For backward compatibility with older
415 * Linux implementations, /proc/dri is also checked.
417 int drmAvailable(void)
419 drmVersionPtr version;
423 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
425 /* Try proc for backward Linux compatibility */
426 if (!access("/proc/dri/0", R_OK))
432 if ((version = drmGetVersion(fd))) {
434 drmFreeVersion(version);
443 * Open the device by bus ID.
445 * \param busid bus ID.
447 * \return a file descriptor on success, or a negative value on error.
450 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
451 * comparing the device bus ID with the one supplied.
453 * \sa drmOpenMinor() and drmGetBusid().
455 static int drmOpenByBusid(const char *busid)
462 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
463 for (i = 0; i < DRM_MAX_MINOR; i++) {
464 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
465 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
469 sv.drm_dd_major = -1; /* Don't care */
470 sv.drm_dd_minor = -1; /* Don't care */
471 drmSetInterfaceVersion(fd, &sv);
472 buf = drmGetBusid(fd);
473 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
474 if (buf && drmMatchBusID(buf, busid)) {
488 * Open the device by name.
490 * \param name driver name.
492 * \return a file descriptor on success, or a negative value on error.
495 * This function opens the first minor number that matches the driver name and
496 * isn't already in use. If it's in use it then it will already have a bus ID
499 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
501 static int drmOpenByName(const char *name)
505 drmVersionPtr version;
508 if (!drmAvailable()) {
509 if (!drm_server_info) {
513 /* try to load the kernel module now */
514 if (!drm_server_info->load_module(name)) {
515 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
522 * Open the first minor number that matches the driver name and isn't
523 * already in use. If it's in use it will have a busid assigned already.
525 for (i = 0; i < DRM_MAX_MINOR; i++) {
526 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
527 if ((version = drmGetVersion(fd))) {
528 if (!strcmp(version->name, name)) {
529 drmFreeVersion(version);
530 id = drmGetBusid(fd);
531 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
540 drmFreeVersion(version);
548 /* Backward-compatibility /proc support */
549 for (i = 0; i < 8; i++) {
550 char proc_name[64], buf[512];
551 char *driver, *pt, *devstring;
554 sprintf(proc_name, "/proc/dri/%d/name", i);
555 if ((fd = open(proc_name, 0, 0)) >= 0) {
556 retcode = read(fd, buf, sizeof(buf)-1);
559 buf[retcode-1] = '\0';
560 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
562 if (*pt) { /* Device is next */
564 if (!strcmp(driver, name)) { /* Match */
565 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
567 if (*pt) { /* Found busid */
568 return drmOpenByBusid(++pt);
569 } else { /* No busid */
570 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
584 * Open the DRM device.
586 * Looks up the specified name and bus ID, and opens the device found. The
587 * entry in /dev/dri is created if necessary and if called by root.
589 * \param name driver name. Not referenced if bus ID is supplied.
590 * \param busid bus ID. Zero if not known.
592 * \return a file descriptor on success, or a negative value on error.
595 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
598 int drmOpen(const char *name, const char *busid)
600 if (!drmAvailable() && name != NULL && drm_server_info) {
601 /* try to load the kernel */
602 if (!drm_server_info->load_module(name)) {
603 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
609 int fd = drmOpenByBusid(busid);
615 return drmOpenByName(name);
620 int drmOpenControl(int minor)
622 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
626 * Free the version information returned by drmGetVersion().
628 * \param v pointer to the version information.
631 * It frees the memory pointed by \p %v as well as all the non-null strings
634 void drmFreeVersion(drmVersionPtr v)
646 * Free the non-public version information returned by the kernel.
648 * \param v pointer to the version information.
651 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
652 * the non-null strings pointers in it.
654 static void drmFreeKernelVersion(drm_version_t *v)
666 * Copy version information.
668 * \param d destination pointer.
669 * \param s source pointer.
672 * Used by drmGetVersion() to translate the information returned by the ioctl
673 * interface in a private structure into the public structure counterpart.
675 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
677 d->version_major = s->version_major;
678 d->version_minor = s->version_minor;
679 d->version_patchlevel = s->version_patchlevel;
680 d->name_len = s->name_len;
681 d->name = drmStrdup(s->name);
682 d->date_len = s->date_len;
683 d->date = drmStrdup(s->date);
684 d->desc_len = s->desc_len;
685 d->desc = drmStrdup(s->desc);
690 * Query the driver version information.
692 * \param fd file descriptor.
694 * \return pointer to a drmVersion structure which should be freed with
697 * \note Similar information is available via /proc/dri.
700 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
701 * first with zeros to get the string lengths, and then the actually strings.
702 * It also null-terminates them since they might not be already.
704 drmVersionPtr drmGetVersion(int fd)
706 drmVersionPtr retval;
707 drm_version_t *version = drmMalloc(sizeof(*version));
709 version->name_len = 0;
710 version->name = NULL;
711 version->date_len = 0;
712 version->date = NULL;
713 version->desc_len = 0;
714 version->desc = NULL;
716 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
717 drmFreeKernelVersion(version);
721 if (version->name_len)
722 version->name = drmMalloc(version->name_len + 1);
723 if (version->date_len)
724 version->date = drmMalloc(version->date_len + 1);
725 if (version->desc_len)
726 version->desc = drmMalloc(version->desc_len + 1);
728 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
729 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
730 drmFreeKernelVersion(version);
734 /* The results might not be null-terminated strings, so terminate them. */
735 if (version->name_len) version->name[version->name_len] = '\0';
736 if (version->date_len) version->date[version->date_len] = '\0';
737 if (version->desc_len) version->desc[version->desc_len] = '\0';
739 retval = drmMalloc(sizeof(*retval));
740 drmCopyVersion(retval, version);
741 drmFreeKernelVersion(version);
747 * Get version information for the DRM user space library.
749 * This version number is driver independent.
751 * \param fd file descriptor.
753 * \return version information.
756 * This function allocates and fills a drm_version structure with a hard coded
759 drmVersionPtr drmGetLibVersion(int fd)
761 drm_version_t *version = drmMalloc(sizeof(*version));
764 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
765 * revision 1.0.x = original DRM interface with no drmGetLibVersion
766 * entry point and many drm<Device> extensions
767 * revision 1.1.x = added drmCommand entry points for device extensions
768 * added drmGetLibVersion to identify libdrm.a version
769 * revision 1.2.x = added drmSetInterfaceVersion
770 * modified drmOpen to handle both busid and name
771 * revision 1.3.x = added server + memory manager
773 version->version_major = 1;
774 version->version_minor = 3;
775 version->version_patchlevel = 0;
777 return (drmVersionPtr)version;
782 * Free the bus ID information.
784 * \param busid bus ID information string as given by drmGetBusid().
787 * This function is just frees the memory pointed by \p busid.
789 void drmFreeBusid(const char *busid)
791 drmFree((void *)busid);
796 * Get the bus ID of the device.
798 * \param fd file descriptor.
800 * \return bus ID string.
803 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
804 * get the string length and data, passing the arguments in a drm_unique
807 char *drmGetBusid(int fd)
814 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
816 u.unique = drmMalloc(u.unique_len + 1);
817 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
819 u.unique[u.unique_len] = '\0';
826 * Set the bus ID of the device.
828 * \param fd file descriptor.
829 * \param busid bus ID string.
831 * \return zero on success, negative on failure.
834 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
835 * the arguments in a drm_unique structure.
837 int drmSetBusid(int fd, const char *busid)
841 u.unique = (char *)busid;
842 u.unique_len = strlen(busid);
844 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
850 int drmGetMagic(int fd, drm_magic_t * magic)
855 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
861 int drmAuthMagic(int fd, drm_magic_t magic)
866 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
872 * Specifies a range of memory that is available for mapping by a
875 * \param fd file descriptor.
876 * \param offset usually the physical address. The actual meaning depends of
877 * the \p type parameter. See below.
878 * \param size of the memory in bytes.
879 * \param type type of the memory to be mapped.
880 * \param flags combination of several flags to modify the function actions.
881 * \param handle will be set to a value that may be used as the offset
882 * parameter for mmap().
884 * \return zero on success or a negative value on error.
886 * \par Mapping the frame buffer
887 * For the frame buffer
888 * - \p offset will be the physical address of the start of the frame buffer,
889 * - \p size will be the size of the frame buffer in bytes, and
890 * - \p type will be DRM_FRAME_BUFFER.
893 * The area mapped will be uncached. If MTRR support is available in the
894 * kernel, the frame buffer area will be set to write combining.
896 * \par Mapping the MMIO register area
897 * For the MMIO register area,
898 * - \p offset will be the physical address of the start of the register area,
899 * - \p size will be the size of the register area bytes, and
900 * - \p type will be DRM_REGISTERS.
902 * The area mapped will be uncached.
904 * \par Mapping the SAREA
906 * - \p offset will be ignored and should be set to zero,
907 * - \p size will be the desired size of the SAREA in bytes,
908 * - \p type will be DRM_SHM.
911 * A shared memory area of the requested size will be created and locked in
912 * kernel memory. This area may be mapped into client-space by using the handle
915 * \note May only be called by root.
918 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
919 * the arguments in a drm_map structure.
921 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
922 drmMapFlags flags, drm_handle_t *handle)
931 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
934 *handle = (drm_handle_t)map.handle;
938 int drmRmMap(int fd, drm_handle_t handle)
942 map.handle = (void *)handle;
944 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
950 * Make buffers available for DMA transfers.
952 * \param fd file descriptor.
953 * \param count number of buffers.
954 * \param size size of each buffer.
955 * \param flags buffer allocation flags.
956 * \param agp_offset offset in the AGP aperture
958 * \return number of buffers allocated, negative on error.
961 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
965 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
968 drm_buf_desc_t request;
970 request.count = count;
972 request.low_mark = 0;
973 request.high_mark = 0;
974 request.flags = flags;
975 request.agp_start = agp_offset;
977 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
979 return request.count;
982 int drmMarkBufs(int fd, double low, double high)
990 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
996 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
999 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1000 int retval = -errno;
1005 for (i = 0; i < info.count; i++) {
1006 info.list[i].low_mark = low * info.list[i].count;
1007 info.list[i].high_mark = high * info.list[i].count;
1008 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1009 int retval = -errno;
1022 * \param fd file descriptor.
1023 * \param count number of buffers to free.
1024 * \param list list of buffers to be freed.
1026 * \return zero on success, or a negative value on failure.
1028 * \note This function is primarily used for debugging.
1031 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1032 * the arguments in a drm_buf_free structure.
1034 int drmFreeBufs(int fd, int count, int *list)
1036 drm_buf_free_t request;
1038 request.count = count;
1039 request.list = list;
1040 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1049 * \param fd file descriptor.
1052 * This function closes the file descriptor.
1054 int drmClose(int fd)
1056 unsigned long key = drmGetKeyFromFd(fd);
1057 drmHashEntry *entry = drmGetEntry(fd);
1059 drmHashDestroy(entry->tagTable);
1062 entry->tagTable = NULL;
1064 drmHashDelete(drmHashTable, key);
1072 * Map a region of memory.
1074 * \param fd file descriptor.
1075 * \param handle handle returned by drmAddMap().
1076 * \param size size in bytes. Must match the size used by drmAddMap().
1077 * \param address will contain the user-space virtual address where the mapping
1080 * \return zero on success, or a negative value on failure.
1083 * This function is a wrapper for mmap().
1085 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1087 static unsigned long pagesize_mask = 0;
1093 pagesize_mask = getpagesize() - 1;
1095 size = (size + pagesize_mask) & ~pagesize_mask;
1097 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1098 if (*address == MAP_FAILED)
1105 * Unmap mappings obtained with drmMap().
1107 * \param address address as given by drmMap().
1108 * \param size size in bytes. Must match the size used by drmMap().
1110 * \return zero on success, or a negative value on failure.
1113 * This function is a wrapper for munmap().
1115 int drmUnmap(drmAddress address, drmSize size)
1117 return munmap(address, size);
1120 drmBufInfoPtr drmGetBufInfo(int fd)
1122 drm_buf_info_t info;
1123 drmBufInfoPtr retval;
1129 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1133 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1136 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1141 retval = drmMalloc(sizeof(*retval));
1142 retval->count = info.count;
1143 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1144 for (i = 0; i < info.count; i++) {
1145 retval->list[i].count = info.list[i].count;
1146 retval->list[i].size = info.list[i].size;
1147 retval->list[i].low_mark = info.list[i].low_mark;
1148 retval->list[i].high_mark = info.list[i].high_mark;
1157 * Map all DMA buffers into client-virtual space.
1159 * \param fd file descriptor.
1161 * \return a pointer to a ::drmBufMap structure.
1163 * \note The client may not use these buffers until obtaining buffer indices
1167 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1168 * information about the buffers in a drm_buf_map structure into the
1169 * client-visible data structures.
1171 drmBufMapPtr drmMapBufs(int fd)
1174 drmBufMapPtr retval;
1179 bufs.virtual = NULL;
1180 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1186 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1189 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1194 retval = drmMalloc(sizeof(*retval));
1195 retval->count = bufs.count;
1196 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1197 for (i = 0; i < bufs.count; i++) {
1198 retval->list[i].idx = bufs.list[i].idx;
1199 retval->list[i].total = bufs.list[i].total;
1200 retval->list[i].used = 0;
1201 retval->list[i].address = bufs.list[i].address;
1211 * Unmap buffers allocated with drmMapBufs().
1213 * \return zero on success, or negative value on failure.
1216 * Calls munmap() for every buffer stored in \p bufs and frees the
1217 * memory allocated by drmMapBufs().
1219 int drmUnmapBufs(drmBufMapPtr bufs)
1223 for (i = 0; i < bufs->count; i++) {
1224 munmap(bufs->list[i].address, bufs->list[i].total);
1227 drmFree(bufs->list);
1234 #define DRM_DMA_RETRY 16
1237 * Reserve DMA buffers.
1239 * \param fd file descriptor.
1242 * \return zero on success, or a negative value on failure.
1245 * Assemble the arguments into a drm_dma structure and keeps issuing the
1246 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1248 int drmDMA(int fd, drmDMAReqPtr request)
1253 dma.context = request->context;
1254 dma.send_count = request->send_count;
1255 dma.send_indices = request->send_list;
1256 dma.send_sizes = request->send_sizes;
1257 dma.flags = request->flags;
1258 dma.request_count = request->request_count;
1259 dma.request_size = request->request_size;
1260 dma.request_indices = request->request_list;
1261 dma.request_sizes = request->request_sizes;
1262 dma.granted_count = 0;
1265 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1266 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1269 request->granted_count = dma.granted_count;
1278 * Obtain heavyweight hardware lock.
1280 * \param fd file descriptor.
1281 * \param context context.
1282 * \param flags flags that determine the sate of the hardware when the function
1285 * \return always zero.
1288 * This function translates the arguments into a drm_lock structure and issue
1289 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1291 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1295 lock.context = context;
1297 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1298 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1299 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1300 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1301 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1302 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1304 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1310 * Release the hardware lock.
1312 * \param fd file descriptor.
1313 * \param context context.
1315 * \return zero on success, or a negative value on failure.
1318 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1319 * argument in a drm_lock structure.
1321 int drmUnlock(int fd, drm_context_t context)
1325 lock.context = context;
1327 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1330 drm_context_t *drmGetReservedContextList(int fd, int *count)
1334 drm_context_t * retval;
1338 res.contexts = NULL;
1339 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1345 if (!(list = drmMalloc(res.count * sizeof(*list))))
1347 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1352 res.contexts = list;
1353 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1356 for (i = 0; i < res.count; i++)
1357 retval[i] = list[i].handle;
1364 void drmFreeReservedContextList(drm_context_t *pt)
1372 * Used by the X server during GLXContext initialization. This causes
1373 * per-context kernel-level resources to be allocated.
1375 * \param fd file descriptor.
1376 * \param handle is set on success. To be used by the client when requesting DMA
1377 * dispatch with drmDMA().
1379 * \return zero on success, or a negative value on failure.
1381 * \note May only be called by root.
1384 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1385 * argument in a drm_ctx structure.
1387 int drmCreateContext(int fd, drm_context_t *handle)
1391 ctx.flags = 0; /* Modified with functions below */
1392 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1394 *handle = ctx.handle;
1398 int drmSwitchToContext(int fd, drm_context_t context)
1402 ctx.handle = context;
1403 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1408 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1413 * Context preserving means that no context switches are done between DMA
1414 * buffers from one context and the next. This is suitable for use in the
1415 * X server (which promises to maintain hardware context), or in the
1416 * client-side library when buffers are swapped on behalf of two threads.
1418 ctx.handle = context;
1420 if (flags & DRM_CONTEXT_PRESERVED)
1421 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1422 if (flags & DRM_CONTEXT_2DONLY)
1423 ctx.flags |= _DRM_CONTEXT_2DONLY;
1424 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1429 int drmGetContextFlags(int fd, drm_context_t context,
1430 drm_context_tFlagsPtr flags)
1434 ctx.handle = context;
1435 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1438 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1439 *flags |= DRM_CONTEXT_PRESERVED;
1440 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1441 *flags |= DRM_CONTEXT_2DONLY;
1448 * Free any kernel-level resources allocated with drmCreateContext() associated
1451 * \param fd file descriptor.
1452 * \param handle handle given by drmCreateContext().
1454 * \return zero on success, or a negative value on failure.
1456 * \note May only be called by root.
1459 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1460 * argument in a drm_ctx structure.
1462 int drmDestroyContext(int fd, drm_context_t handle)
1465 ctx.handle = handle;
1466 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1471 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1474 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1476 *handle = draw.handle;
1480 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1483 draw.handle = handle;
1484 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1489 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1490 drm_drawable_info_type_t type, unsigned int num,
1493 drm_update_draw_t update;
1495 update.handle = handle;
1498 update.data = (unsigned long long)(unsigned long)data;
1500 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1507 * Acquire the AGP device.
1509 * Must be called before any of the other AGP related calls.
1511 * \param fd file descriptor.
1513 * \return zero on success, or a negative value on failure.
1516 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1518 int drmAgpAcquire(int fd)
1520 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1527 * Release the AGP device.
1529 * \param fd file descriptor.
1531 * \return zero on success, or a negative value on failure.
1534 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1536 int drmAgpRelease(int fd)
1538 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1547 * \param fd file descriptor.
1548 * \param mode AGP mode.
1550 * \return zero on success, or a negative value on failure.
1553 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1554 * argument in a drm_agp_mode structure.
1556 int drmAgpEnable(int fd, unsigned long mode)
1561 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1568 * Allocate a chunk of AGP memory.
1570 * \param fd file descriptor.
1571 * \param size requested memory size in bytes. Will be rounded to page boundary.
1572 * \param type type of memory to allocate.
1573 * \param address if not zero, will be set to the physical address of the
1575 * \param handle on success will be set to a handle of the allocated memory.
1577 * \return zero on success, or a negative value on failure.
1580 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1581 * arguments in a drm_agp_buffer structure.
1583 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1584 unsigned long *address, drm_handle_t *handle)
1588 *handle = DRM_AGP_NO_HANDLE;
1592 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1595 *address = b.physical;
1602 * Free a chunk of AGP memory.
1604 * \param fd file descriptor.
1605 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1607 * \return zero on success, or a negative value on failure.
1610 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1611 * argument in a drm_agp_buffer structure.
1613 int drmAgpFree(int fd, drm_handle_t handle)
1619 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1626 * Bind a chunk of AGP memory.
1628 * \param fd file descriptor.
1629 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1630 * \param offset offset in bytes. It will round to page boundary.
1632 * \return zero on success, or a negative value on failure.
1635 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1636 * argument in a drm_agp_binding structure.
1638 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1640 drm_agp_binding_t b;
1644 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1651 * Unbind a chunk of AGP memory.
1653 * \param fd file descriptor.
1654 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1656 * \return zero on success, or a negative value on failure.
1659 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1660 * the argument in a drm_agp_binding structure.
1662 int drmAgpUnbind(int fd, drm_handle_t handle)
1664 drm_agp_binding_t b;
1668 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1675 * Get AGP driver major version number.
1677 * \param fd file descriptor.
1679 * \return major version number on success, or a negative value on failure..
1682 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1683 * necessary information in a drm_agp_info structure.
1685 int drmAgpVersionMajor(int fd)
1689 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1691 return i.agp_version_major;
1696 * Get AGP driver minor version number.
1698 * \param fd file descriptor.
1700 * \return minor version number on success, or a negative value on failure.
1703 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1704 * necessary information in a drm_agp_info structure.
1706 int drmAgpVersionMinor(int fd)
1710 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1712 return i.agp_version_minor;
1719 * \param fd file descriptor.
1721 * \return mode on success, or zero on failure.
1724 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1725 * necessary information in a drm_agp_info structure.
1727 unsigned long drmAgpGetMode(int fd)
1731 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1738 * Get AGP aperture base.
1740 * \param fd file descriptor.
1742 * \return aperture base on success, zero on failure.
1745 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1746 * necessary information in a drm_agp_info structure.
1748 unsigned long drmAgpBase(int fd)
1752 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1754 return i.aperture_base;
1759 * Get AGP aperture size.
1761 * \param fd file descriptor.
1763 * \return aperture size on success, zero on failure.
1766 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1767 * necessary information in a drm_agp_info structure.
1769 unsigned long drmAgpSize(int fd)
1773 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1775 return i.aperture_size;
1780 * Get used AGP memory.
1782 * \param fd file descriptor.
1784 * \return memory used on success, or zero on failure.
1787 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1788 * necessary information in a drm_agp_info structure.
1790 unsigned long drmAgpMemoryUsed(int fd)
1794 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1796 return i.memory_used;
1801 * Get available AGP memory.
1803 * \param fd file descriptor.
1805 * \return memory available on success, or zero on failure.
1808 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1809 * necessary information in a drm_agp_info structure.
1811 unsigned long drmAgpMemoryAvail(int fd)
1815 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1817 return i.memory_allowed;
1822 * Get hardware vendor ID.
1824 * \param fd file descriptor.
1826 * \return vendor ID on success, or zero on failure.
1829 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1830 * necessary information in a drm_agp_info structure.
1832 unsigned int drmAgpVendorId(int fd)
1836 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1843 * Get hardware device ID.
1845 * \param fd file descriptor.
1847 * \return zero on success, or zero on failure.
1850 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1851 * necessary information in a drm_agp_info structure.
1853 unsigned int drmAgpDeviceId(int fd)
1857 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1862 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1864 drm_scatter_gather_t sg;
1869 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1871 *handle = sg.handle;
1875 int drmScatterGatherFree(int fd, drm_handle_t handle)
1877 drm_scatter_gather_t sg;
1881 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1889 * \param fd file descriptor.
1890 * \param vbl pointer to a drmVBlank structure.
1892 * \return zero on success, or a negative value on failure.
1895 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1897 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1902 ret = drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1903 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1904 } while (ret && errno == EINTR);
1909 int drmError(int err, const char *label)
1912 case DRM_ERR_NO_DEVICE:
1913 fprintf(stderr, "%s: no device\n", label);
1915 case DRM_ERR_NO_ACCESS:
1916 fprintf(stderr, "%s: no access\n", label);
1918 case DRM_ERR_NOT_ROOT:
1919 fprintf(stderr, "%s: not root\n", label);
1921 case DRM_ERR_INVALID:
1922 fprintf(stderr, "%s: invalid args\n", label);
1927 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1935 * Install IRQ handler.
1937 * \param fd file descriptor.
1938 * \param irq IRQ number.
1940 * \return zero on success, or a negative value on failure.
1943 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1944 * argument in a drm_control structure.
1946 int drmCtlInstHandler(int fd, int irq)
1950 ctl.func = DRM_INST_HANDLER;
1952 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1959 * Uninstall IRQ handler.
1961 * \param fd file descriptor.
1963 * \return zero on success, or a negative value on failure.
1966 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1967 * argument in a drm_control structure.
1969 int drmCtlUninstHandler(int fd)
1973 ctl.func = DRM_UNINST_HANDLER;
1975 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1980 int drmFinish(int fd, int context, drmLockFlags flags)
1984 lock.context = context;
1986 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1987 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1988 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1989 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1990 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1991 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1992 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
1998 * Get IRQ from bus ID.
2000 * \param fd file descriptor.
2001 * \param busnum bus number.
2002 * \param devnum device number.
2003 * \param funcnum function number.
2005 * \return IRQ number on success, or a negative value on failure.
2008 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2009 * arguments in a drm_irq_busid structure.
2011 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2017 p.funcnum = funcnum;
2018 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2023 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2025 drmHashEntry *entry = drmGetEntry(fd);
2027 if (drmHashInsert(entry->tagTable, context, tag)) {
2028 drmHashDelete(entry->tagTable, context);
2029 drmHashInsert(entry->tagTable, context, tag);
2034 int drmDelContextTag(int fd, drm_context_t context)
2036 drmHashEntry *entry = drmGetEntry(fd);
2038 return drmHashDelete(entry->tagTable, context);
2041 void *drmGetContextTag(int fd, drm_context_t context)
2043 drmHashEntry *entry = drmGetEntry(fd);
2046 if (drmHashLookup(entry->tagTable, context, &value))
2052 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2053 drm_handle_t handle)
2055 drm_ctx_priv_map_t map;
2057 map.ctx_id = ctx_id;
2058 map.handle = (void *)handle;
2060 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2065 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2066 drm_handle_t *handle)
2068 drm_ctx_priv_map_t map;
2070 map.ctx_id = ctx_id;
2072 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2075 *handle = (drm_handle_t)map.handle;
2080 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2081 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2087 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2089 *offset = map.offset;
2093 *handle = (unsigned long)map.handle;
2098 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2099 unsigned long *magic, unsigned long *iocs)
2101 drm_client_t client;
2104 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2106 *auth = client.auth;
2109 *magic = client.magic;
2110 *iocs = client.iocs;
2114 int drmGetStats(int fd, drmStatsT *stats)
2119 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2123 memset(stats, 0, sizeof(*stats));
2124 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2128 stats->data[i].long_format = "%-20.20s"; \
2129 stats->data[i].rate_format = "%8.8s"; \
2130 stats->data[i].isvalue = 1; \
2131 stats->data[i].verbose = 0
2134 stats->data[i].long_format = "%-20.20s"; \
2135 stats->data[i].rate_format = "%5.5s"; \
2136 stats->data[i].isvalue = 0; \
2137 stats->data[i].mult_names = "kgm"; \
2138 stats->data[i].mult = 1000; \
2139 stats->data[i].verbose = 0
2142 stats->data[i].long_format = "%-20.20s"; \
2143 stats->data[i].rate_format = "%5.5s"; \
2144 stats->data[i].isvalue = 0; \
2145 stats->data[i].mult_names = "KGM"; \
2146 stats->data[i].mult = 1024; \
2147 stats->data[i].verbose = 0
2150 stats->count = s.count;
2151 for (i = 0; i < s.count; i++) {
2152 stats->data[i].value = s.data[i].value;
2153 switch (s.data[i].type) {
2154 case _DRM_STAT_LOCK:
2155 stats->data[i].long_name = "Lock";
2156 stats->data[i].rate_name = "Lock";
2159 case _DRM_STAT_OPENS:
2160 stats->data[i].long_name = "Opens";
2161 stats->data[i].rate_name = "O";
2163 stats->data[i].verbose = 1;
2165 case _DRM_STAT_CLOSES:
2166 stats->data[i].long_name = "Closes";
2167 stats->data[i].rate_name = "Lock";
2169 stats->data[i].verbose = 1;
2171 case _DRM_STAT_IOCTLS:
2172 stats->data[i].long_name = "Ioctls";
2173 stats->data[i].rate_name = "Ioc/s";
2176 case _DRM_STAT_LOCKS:
2177 stats->data[i].long_name = "Locks";
2178 stats->data[i].rate_name = "Lck/s";
2181 case _DRM_STAT_UNLOCKS:
2182 stats->data[i].long_name = "Unlocks";
2183 stats->data[i].rate_name = "Unl/s";
2187 stats->data[i].long_name = "IRQs";
2188 stats->data[i].rate_name = "IRQ/s";
2191 case _DRM_STAT_PRIMARY:
2192 stats->data[i].long_name = "Primary Bytes";
2193 stats->data[i].rate_name = "PB/s";
2196 case _DRM_STAT_SECONDARY:
2197 stats->data[i].long_name = "Secondary Bytes";
2198 stats->data[i].rate_name = "SB/s";
2202 stats->data[i].long_name = "DMA";
2203 stats->data[i].rate_name = "DMA/s";
2206 case _DRM_STAT_SPECIAL:
2207 stats->data[i].long_name = "Special DMA";
2208 stats->data[i].rate_name = "dma/s";
2211 case _DRM_STAT_MISSED:
2212 stats->data[i].long_name = "Miss";
2213 stats->data[i].rate_name = "Ms/s";
2216 case _DRM_STAT_VALUE:
2217 stats->data[i].long_name = "Value";
2218 stats->data[i].rate_name = "Value";
2221 case _DRM_STAT_BYTE:
2222 stats->data[i].long_name = "Bytes";
2223 stats->data[i].rate_name = "B/s";
2226 case _DRM_STAT_COUNT:
2228 stats->data[i].long_name = "Count";
2229 stats->data[i].rate_name = "Cnt/s";
2238 * Issue a set-version ioctl.
2240 * \param fd file descriptor.
2241 * \param drmCommandIndex command index
2242 * \param data source pointer of the data to be read and written.
2243 * \param size size of the data to be read and written.
2245 * \return zero on success, or a negative value on failure.
2248 * It issues a read-write ioctl given by
2249 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2251 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2254 drm_set_version_t sv;
2256 sv.drm_di_major = version->drm_di_major;
2257 sv.drm_di_minor = version->drm_di_minor;
2258 sv.drm_dd_major = version->drm_dd_major;
2259 sv.drm_dd_minor = version->drm_dd_minor;
2261 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2265 version->drm_di_major = sv.drm_di_major;
2266 version->drm_di_minor = sv.drm_di_minor;
2267 version->drm_dd_major = sv.drm_dd_major;
2268 version->drm_dd_minor = sv.drm_dd_minor;
2274 * Send a device-specific command.
2276 * \param fd file descriptor.
2277 * \param drmCommandIndex command index
2279 * \return zero on success, or a negative value on failure.
2282 * It issues a ioctl given by
2283 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2285 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2287 void *data = NULL; /* dummy */
2288 unsigned long request;
2290 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2292 if (drmIoctl(fd, request, data)) {
2300 * Send a device-specific read command.
2302 * \param fd file descriptor.
2303 * \param drmCommandIndex command index
2304 * \param data destination pointer of the data to be read.
2305 * \param size size of the data to be read.
2307 * \return zero on success, or a negative value on failure.
2310 * It issues a read ioctl given by
2311 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2313 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2316 unsigned long request;
2318 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2319 DRM_COMMAND_BASE + drmCommandIndex, size);
2321 if (drmIoctl(fd, request, data)) {
2329 * Send a device-specific write command.
2331 * \param fd file descriptor.
2332 * \param drmCommandIndex command index
2333 * \param data source pointer of the data to be written.
2334 * \param size size of the data to be written.
2336 * \return zero on success, or a negative value on failure.
2339 * It issues a write ioctl given by
2340 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2342 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2345 unsigned long request;
2347 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2348 DRM_COMMAND_BASE + drmCommandIndex, size);
2350 if (drmIoctl(fd, request, data)) {
2358 * Send a device-specific read-write command.
2360 * \param fd file descriptor.
2361 * \param drmCommandIndex command index
2362 * \param data source pointer of the data to be read and written.
2363 * \param size size of the data to be read and written.
2365 * \return zero on success, or a negative value on failure.
2368 * It issues a read-write ioctl given by
2369 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2371 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2374 unsigned long request;
2376 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2377 DRM_COMMAND_BASE + drmCommandIndex, size);
2379 if (drmIoctl(fd, request, data))
2384 #define DRM_MAX_FDS 16
2389 } connection[DRM_MAX_FDS];
2391 static int nr_fds = 0;
2393 int drmOpenOnce(void *unused,
2400 for (i = 0; i < nr_fds; i++)
2401 if (strcmp(BusID, connection[i].BusID) == 0) {
2402 connection[i].refcount++;
2404 return connection[i].fd;
2407 fd = drmOpen(unused, BusID);
2408 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2411 connection[nr_fds].BusID = strdup(BusID);
2412 connection[nr_fds].fd = fd;
2413 connection[nr_fds].refcount = 1;
2417 fprintf(stderr, "saved connection %d for %s %d\n",
2418 nr_fds, connection[nr_fds].BusID,
2419 strcmp(BusID, connection[nr_fds].BusID));
2426 void drmCloseOnce(int fd)
2430 for (i = 0; i < nr_fds; i++) {
2431 if (fd == connection[i].fd) {
2432 if (--connection[i].refcount == 0) {
2433 drmClose(connection[i].fd);
2434 free(connection[i].BusID);
2437 connection[i] = connection[nr_fds];
2445 int drmSetMaster(int fd)
2449 fprintf(stderr,"Setting master \n");
2450 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2454 int drmDropMaster(int fd)
2457 fprintf(stderr,"Dropping master \n");
2458 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);