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 static drmServerInfoPtr drm_server_info;
92 void drmSetServerInfo(drmServerInfoPtr info)
94 drm_server_info = info;
98 * Output a message to stderr.
100 * \param format printf() like format string.
103 * This function is a wrapper around vfprintf().
106 static int drmDebugPrint(const char *format, va_list ap)
108 return vfprintf(stderr, format, ap);
111 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
114 drmMsg(const char *format, ...)
118 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
120 va_start(ap, format);
121 if (drm_server_info) {
122 drm_server_info->debug_print(format,ap);
124 drm_debug_print(format, ap);
131 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
133 drm_debug_print = debug_msg_ptr;
136 static void *drmHashTable = NULL; /* Context switch callbacks */
138 void *drmGetHashTable(void)
143 void *drmMalloc(int size)
146 if ((pt = malloc(size)))
151 void drmFree(void *pt)
157 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
158 static char *drmStrdup(const char *s)
165 retval = malloc(strlen(s)+1);
175 * Call ioctl, restarting if it is interupted
178 drmIoctl(int fd, unsigned long request, void *arg)
183 ret = ioctl(fd, request, arg);
184 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
188 static unsigned long drmGetKeyFromFd(int fd)
197 drmHashEntry *drmGetEntry(int fd)
199 unsigned long key = drmGetKeyFromFd(fd);
204 drmHashTable = drmHashCreate();
206 if (drmHashLookup(drmHashTable, key, &value)) {
207 entry = drmMalloc(sizeof(*entry));
210 entry->tagTable = drmHashCreate();
211 drmHashInsert(drmHashTable, key, entry);
219 * Compare two busid strings
224 * \return 1 if matched.
227 * This function compares two bus ID strings. It understands the older
228 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
229 * domain, b is bus, d is device, f is function.
231 static int drmMatchBusID(const char *id1, const char *id2)
233 /* First, check if the IDs are exactly the same */
234 if (strcasecmp(id1, id2) == 0)
237 /* Try to match old/new-style PCI bus IDs. */
238 if (strncasecmp(id1, "pci", 3) == 0) {
243 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
246 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
251 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
254 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
259 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
268 * Open the DRM device, creating it if necessary.
270 * \param dev major and minor numbers of the device.
271 * \param minor minor number of the device.
273 * \return a file descriptor on success, or a negative value on error.
276 * Assembles the device name from \p minor and opens it, creating the device
277 * special file node with the major and minor numbers specified by \p dev and
278 * parent directory if necessary and was called by root.
280 static int drmOpenDevice(long dev, int minor)
285 mode_t devmode = DRM_DEV_MODE, serv_mode;
286 int isroot = !geteuid();
287 uid_t user = DRM_DEV_UID;
288 gid_t group = DRM_DEV_GID, serv_group;
290 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
291 drmMsg("drmOpenDevice: node name is %s\n", buf);
293 if (drm_server_info) {
294 drm_server_info->get_perms(&serv_group, &serv_mode);
295 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
296 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
297 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
301 if (stat(DRM_DIR_NAME, &st)) {
303 return DRM_ERR_NOT_ROOT;
304 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
305 chown(DRM_DIR_NAME, 0, 0); /* root:root */
306 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
309 /* Check if the device node exists and create it if necessary. */
310 if (stat(buf, &st)) {
312 return DRM_ERR_NOT_ROOT;
314 mknod(buf, S_IFCHR | devmode, dev);
317 if (drm_server_info) {
318 chown(buf, user, group);
322 /* if we modprobed then wait for udev */
326 if (stat(DRM_DIR_NAME, &st)) {
330 if (udev_count == 50)
335 if (stat(buf, &st)) {
339 if (udev_count == 50)
346 fd = open(buf, O_RDWR, 0);
347 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
348 fd, fd < 0 ? strerror(errno) : "OK");
352 /* Check if the device node is not what we expect it to be, and recreate it
353 * and try again if so.
355 if (st.st_rdev != dev) {
357 return DRM_ERR_NOT_ROOT;
359 mknod(buf, S_IFCHR | devmode, dev);
360 if (drm_server_info) {
361 chown(buf, user, group);
365 fd = open(buf, O_RDWR, 0);
366 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
367 fd, fd < 0 ? strerror(errno) : "OK");
371 drmMsg("drmOpenDevice: Open failed\n");
378 * Open the DRM device
380 * \param minor device minor number.
381 * \param create allow to create the device if set.
383 * \return a file descriptor on success, or a negative value on error.
386 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
387 * name from \p minor and opens it.
389 static int drmOpenMinor(int minor, int create)
395 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
397 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
398 if ((fd = open(buf, O_RDWR, 0)) >= 0)
405 * Determine whether the DRM kernel driver has been loaded.
407 * \return 1 if the DRM driver is loaded, 0 otherwise.
410 * Determine the presence of the kernel driver by attempting to open the 0
411 * minor and get version information. For backward compatibility with older
412 * Linux implementations, /proc/dri is also checked.
414 int drmAvailable(void)
416 drmVersionPtr version;
420 if ((fd = drmOpenMinor(0, 1)) < 0) {
422 /* Try proc for backward Linux compatibility */
423 if (!access("/proc/dri/0", R_OK))
429 if ((version = drmGetVersion(fd))) {
431 drmFreeVersion(version);
440 * Open the device by bus ID.
442 * \param busid bus ID.
444 * \return a file descriptor on success, or a negative value on error.
447 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
448 * comparing the device bus ID with the one supplied.
450 * \sa drmOpenMinor() and drmGetBusid().
452 static int drmOpenByBusid(const char *busid)
459 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
460 for (i = 0; i < DRM_MAX_MINOR; i++) {
461 fd = drmOpenMinor(i, 1);
462 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
466 sv.drm_dd_major = -1; /* Don't care */
467 sv.drm_dd_minor = -1; /* Don't care */
468 drmSetInterfaceVersion(fd, &sv);
469 buf = drmGetBusid(fd);
470 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
471 if (buf && drmMatchBusID(buf, busid)) {
485 * Open the device by name.
487 * \param name driver name.
489 * \return a file descriptor on success, or a negative value on error.
492 * This function opens the first minor number that matches the driver name and
493 * isn't already in use. If it's in use it then it will already have a bus ID
496 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
498 static int drmOpenByName(const char *name)
502 drmVersionPtr version;
505 if (!drmAvailable()) {
506 if (!drm_server_info) {
510 /* try to load the kernel module now */
511 if (!drm_server_info->load_module(name)) {
512 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
519 * Open the first minor number that matches the driver name and isn't
520 * already in use. If it's in use it will have a busid assigned already.
522 for (i = 0; i < DRM_MAX_MINOR; i++) {
523 if ((fd = drmOpenMinor(i, 1)) >= 0) {
524 if ((version = drmGetVersion(fd))) {
525 if (!strcmp(version->name, name)) {
526 drmFreeVersion(version);
527 id = drmGetBusid(fd);
528 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
537 drmFreeVersion(version);
545 /* Backward-compatibility /proc support */
546 for (i = 0; i < 8; i++) {
547 char proc_name[64], buf[512];
548 char *driver, *pt, *devstring;
551 sprintf(proc_name, "/proc/dri/%d/name", i);
552 if ((fd = open(proc_name, 0, 0)) >= 0) {
553 retcode = read(fd, buf, sizeof(buf)-1);
556 buf[retcode-1] = '\0';
557 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
559 if (*pt) { /* Device is next */
561 if (!strcmp(driver, name)) { /* Match */
562 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
564 if (*pt) { /* Found busid */
565 return drmOpenByBusid(++pt);
566 } else { /* No busid */
567 return drmOpenDevice(strtol(devstring, NULL, 0),i);
581 * Open the DRM device.
583 * Looks up the specified name and bus ID, and opens the device found. The
584 * entry in /dev/dri is created if necessary and if called by root.
586 * \param name driver name. Not referenced if bus ID is supplied.
587 * \param busid bus ID. Zero if not known.
589 * \return a file descriptor on success, or a negative value on error.
592 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
595 int drmOpen(const char *name, const char *busid)
597 if (!drmAvailable() && name != NULL && drm_server_info) {
598 /* try to load the kernel */
599 if (!drm_server_info->load_module(name)) {
600 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
606 int fd = drmOpenByBusid(busid);
612 return drmOpenByName(name);
619 * Free the version information returned by drmGetVersion().
621 * \param v pointer to the version information.
624 * It frees the memory pointed by \p %v as well as all the non-null strings
627 void drmFreeVersion(drmVersionPtr v)
639 * Free the non-public version information returned by the kernel.
641 * \param v pointer to the version information.
644 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
645 * the non-null strings pointers in it.
647 static void drmFreeKernelVersion(drm_version_t *v)
659 * Copy version information.
661 * \param d destination pointer.
662 * \param s source pointer.
665 * Used by drmGetVersion() to translate the information returned by the ioctl
666 * interface in a private structure into the public structure counterpart.
668 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
670 d->version_major = s->version_major;
671 d->version_minor = s->version_minor;
672 d->version_patchlevel = s->version_patchlevel;
673 d->name_len = s->name_len;
674 d->name = drmStrdup(s->name);
675 d->date_len = s->date_len;
676 d->date = drmStrdup(s->date);
677 d->desc_len = s->desc_len;
678 d->desc = drmStrdup(s->desc);
683 * Query the driver version information.
685 * \param fd file descriptor.
687 * \return pointer to a drmVersion structure which should be freed with
690 * \note Similar information is available via /proc/dri.
693 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
694 * first with zeros to get the string lengths, and then the actually strings.
695 * It also null-terminates them since they might not be already.
697 drmVersionPtr drmGetVersion(int fd)
699 drmVersionPtr retval;
700 drm_version_t *version = drmMalloc(sizeof(*version));
702 version->name_len = 0;
703 version->name = NULL;
704 version->date_len = 0;
705 version->date = NULL;
706 version->desc_len = 0;
707 version->desc = NULL;
709 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
710 drmFreeKernelVersion(version);
714 if (version->name_len)
715 version->name = drmMalloc(version->name_len + 1);
716 if (version->date_len)
717 version->date = drmMalloc(version->date_len + 1);
718 if (version->desc_len)
719 version->desc = drmMalloc(version->desc_len + 1);
721 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
722 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
723 drmFreeKernelVersion(version);
727 /* The results might not be null-terminated strings, so terminate them. */
728 if (version->name_len) version->name[version->name_len] = '\0';
729 if (version->date_len) version->date[version->date_len] = '\0';
730 if (version->desc_len) version->desc[version->desc_len] = '\0';
732 retval = drmMalloc(sizeof(*retval));
733 drmCopyVersion(retval, version);
734 drmFreeKernelVersion(version);
740 * Get version information for the DRM user space library.
742 * This version number is driver independent.
744 * \param fd file descriptor.
746 * \return version information.
749 * This function allocates and fills a drm_version structure with a hard coded
752 drmVersionPtr drmGetLibVersion(int fd)
754 drm_version_t *version = drmMalloc(sizeof(*version));
757 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
758 * revision 1.0.x = original DRM interface with no drmGetLibVersion
759 * entry point and many drm<Device> extensions
760 * revision 1.1.x = added drmCommand entry points for device extensions
761 * added drmGetLibVersion to identify libdrm.a version
762 * revision 1.2.x = added drmSetInterfaceVersion
763 * modified drmOpen to handle both busid and name
764 * revision 1.3.x = added server + memory manager
766 version->version_major = 1;
767 version->version_minor = 3;
768 version->version_patchlevel = 0;
770 return (drmVersionPtr)version;
775 * Free the bus ID information.
777 * \param busid bus ID information string as given by drmGetBusid().
780 * This function is just frees the memory pointed by \p busid.
782 void drmFreeBusid(const char *busid)
784 drmFree((void *)busid);
789 * Get the bus ID of the device.
791 * \param fd file descriptor.
793 * \return bus ID string.
796 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
797 * get the string length and data, passing the arguments in a drm_unique
800 char *drmGetBusid(int fd)
807 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
809 u.unique = drmMalloc(u.unique_len + 1);
810 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
812 u.unique[u.unique_len] = '\0';
819 * Set the bus ID of the device.
821 * \param fd file descriptor.
822 * \param busid bus ID string.
824 * \return zero on success, negative on failure.
827 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
828 * the arguments in a drm_unique structure.
830 int drmSetBusid(int fd, const char *busid)
834 u.unique = (char *)busid;
835 u.unique_len = strlen(busid);
837 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
843 int drmGetMagic(int fd, drm_magic_t * magic)
848 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
854 int drmAuthMagic(int fd, drm_magic_t magic)
859 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
865 * Specifies a range of memory that is available for mapping by a
868 * \param fd file descriptor.
869 * \param offset usually the physical address. The actual meaning depends of
870 * the \p type parameter. See below.
871 * \param size of the memory in bytes.
872 * \param type type of the memory to be mapped.
873 * \param flags combination of several flags to modify the function actions.
874 * \param handle will be set to a value that may be used as the offset
875 * parameter for mmap().
877 * \return zero on success or a negative value on error.
879 * \par Mapping the frame buffer
880 * For the frame buffer
881 * - \p offset will be the physical address of the start of the frame buffer,
882 * - \p size will be the size of the frame buffer in bytes, and
883 * - \p type will be DRM_FRAME_BUFFER.
886 * The area mapped will be uncached. If MTRR support is available in the
887 * kernel, the frame buffer area will be set to write combining.
889 * \par Mapping the MMIO register area
890 * For the MMIO register area,
891 * - \p offset will be the physical address of the start of the register area,
892 * - \p size will be the size of the register area bytes, and
893 * - \p type will be DRM_REGISTERS.
895 * The area mapped will be uncached.
897 * \par Mapping the SAREA
899 * - \p offset will be ignored and should be set to zero,
900 * - \p size will be the desired size of the SAREA in bytes,
901 * - \p type will be DRM_SHM.
904 * A shared memory area of the requested size will be created and locked in
905 * kernel memory. This area may be mapped into client-space by using the handle
908 * \note May only be called by root.
911 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
912 * the arguments in a drm_map structure.
914 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
915 drmMapFlags flags, drm_handle_t *handle)
924 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
927 *handle = (drm_handle_t)map.handle;
931 int drmRmMap(int fd, drm_handle_t handle)
935 map.handle = (void *)handle;
937 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
943 * Make buffers available for DMA transfers.
945 * \param fd file descriptor.
946 * \param count number of buffers.
947 * \param size size of each buffer.
948 * \param flags buffer allocation flags.
949 * \param agp_offset offset in the AGP aperture
951 * \return number of buffers allocated, negative on error.
954 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
958 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
961 drm_buf_desc_t request;
963 request.count = count;
965 request.low_mark = 0;
966 request.high_mark = 0;
967 request.flags = flags;
968 request.agp_start = agp_offset;
970 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
972 return request.count;
975 int drmMarkBufs(int fd, double low, double high)
983 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
989 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
992 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
998 for (i = 0; i < info.count; i++) {
999 info.list[i].low_mark = low * info.list[i].count;
1000 info.list[i].high_mark = high * info.list[i].count;
1001 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1002 int retval = -errno;
1015 * \param fd file descriptor.
1016 * \param count number of buffers to free.
1017 * \param list list of buffers to be freed.
1019 * \return zero on success, or a negative value on failure.
1021 * \note This function is primarily used for debugging.
1024 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1025 * the arguments in a drm_buf_free structure.
1027 int drmFreeBufs(int fd, int count, int *list)
1029 drm_buf_free_t request;
1031 request.count = count;
1032 request.list = list;
1033 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1042 * \param fd file descriptor.
1045 * This function closes the file descriptor.
1047 int drmClose(int fd)
1049 unsigned long key = drmGetKeyFromFd(fd);
1050 drmHashEntry *entry = drmGetEntry(fd);
1052 drmHashDestroy(entry->tagTable);
1055 entry->tagTable = NULL;
1057 drmHashDelete(drmHashTable, key);
1065 * Map a region of memory.
1067 * \param fd file descriptor.
1068 * \param handle handle returned by drmAddMap().
1069 * \param size size in bytes. Must match the size used by drmAddMap().
1070 * \param address will contain the user-space virtual address where the mapping
1073 * \return zero on success, or a negative value on failure.
1076 * This function is a wrapper for mmap().
1078 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1080 static unsigned long pagesize_mask = 0;
1086 pagesize_mask = getpagesize() - 1;
1088 size = (size + pagesize_mask) & ~pagesize_mask;
1090 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1091 if (*address == MAP_FAILED)
1098 * Unmap mappings obtained with drmMap().
1100 * \param address address as given by drmMap().
1101 * \param size size in bytes. Must match the size used by drmMap().
1103 * \return zero on success, or a negative value on failure.
1106 * This function is a wrapper for munmap().
1108 int drmUnmap(drmAddress address, drmSize size)
1110 return munmap(address, size);
1113 drmBufInfoPtr drmGetBufInfo(int fd)
1115 drm_buf_info_t info;
1116 drmBufInfoPtr retval;
1122 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1126 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1129 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1134 retval = drmMalloc(sizeof(*retval));
1135 retval->count = info.count;
1136 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1137 for (i = 0; i < info.count; i++) {
1138 retval->list[i].count = info.list[i].count;
1139 retval->list[i].size = info.list[i].size;
1140 retval->list[i].low_mark = info.list[i].low_mark;
1141 retval->list[i].high_mark = info.list[i].high_mark;
1150 * Map all DMA buffers into client-virtual space.
1152 * \param fd file descriptor.
1154 * \return a pointer to a ::drmBufMap structure.
1156 * \note The client may not use these buffers until obtaining buffer indices
1160 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1161 * information about the buffers in a drm_buf_map structure into the
1162 * client-visible data structures.
1164 drmBufMapPtr drmMapBufs(int fd)
1167 drmBufMapPtr retval;
1172 bufs.virtual = NULL;
1173 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1179 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1182 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1187 retval = drmMalloc(sizeof(*retval));
1188 retval->count = bufs.count;
1189 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1190 for (i = 0; i < bufs.count; i++) {
1191 retval->list[i].idx = bufs.list[i].idx;
1192 retval->list[i].total = bufs.list[i].total;
1193 retval->list[i].used = 0;
1194 retval->list[i].address = bufs.list[i].address;
1204 * Unmap buffers allocated with drmMapBufs().
1206 * \return zero on success, or negative value on failure.
1209 * Calls munmap() for every buffer stored in \p bufs and frees the
1210 * memory allocated by drmMapBufs().
1212 int drmUnmapBufs(drmBufMapPtr bufs)
1216 for (i = 0; i < bufs->count; i++) {
1217 munmap(bufs->list[i].address, bufs->list[i].total);
1220 drmFree(bufs->list);
1227 #define DRM_DMA_RETRY 16
1230 * Reserve DMA buffers.
1232 * \param fd file descriptor.
1235 * \return zero on success, or a negative value on failure.
1238 * Assemble the arguments into a drm_dma structure and keeps issuing the
1239 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1241 int drmDMA(int fd, drmDMAReqPtr request)
1246 dma.context = request->context;
1247 dma.send_count = request->send_count;
1248 dma.send_indices = request->send_list;
1249 dma.send_sizes = request->send_sizes;
1250 dma.flags = request->flags;
1251 dma.request_count = request->request_count;
1252 dma.request_size = request->request_size;
1253 dma.request_indices = request->request_list;
1254 dma.request_sizes = request->request_sizes;
1255 dma.granted_count = 0;
1258 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1259 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1262 request->granted_count = dma.granted_count;
1271 * Obtain heavyweight hardware lock.
1273 * \param fd file descriptor.
1274 * \param context context.
1275 * \param flags flags that determine the sate of the hardware when the function
1278 * \return always zero.
1281 * This function translates the arguments into a drm_lock structure and issue
1282 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1284 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1288 lock.context = context;
1290 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1291 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1292 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1293 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1294 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1295 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1297 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1303 * Release the hardware lock.
1305 * \param fd file descriptor.
1306 * \param context context.
1308 * \return zero on success, or a negative value on failure.
1311 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1312 * argument in a drm_lock structure.
1314 int drmUnlock(int fd, drm_context_t context)
1318 lock.context = context;
1320 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1323 drm_context_t *drmGetReservedContextList(int fd, int *count)
1327 drm_context_t * retval;
1331 res.contexts = NULL;
1332 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1338 if (!(list = drmMalloc(res.count * sizeof(*list))))
1340 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1345 res.contexts = list;
1346 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1349 for (i = 0; i < res.count; i++)
1350 retval[i] = list[i].handle;
1357 void drmFreeReservedContextList(drm_context_t *pt)
1365 * Used by the X server during GLXContext initialization. This causes
1366 * per-context kernel-level resources to be allocated.
1368 * \param fd file descriptor.
1369 * \param handle is set on success. To be used by the client when requesting DMA
1370 * dispatch with drmDMA().
1372 * \return zero on success, or a negative value on failure.
1374 * \note May only be called by root.
1377 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1378 * argument in a drm_ctx structure.
1380 int drmCreateContext(int fd, drm_context_t *handle)
1384 ctx.flags = 0; /* Modified with functions below */
1385 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1387 *handle = ctx.handle;
1391 int drmSwitchToContext(int fd, drm_context_t context)
1395 ctx.handle = context;
1396 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1401 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1406 * Context preserving means that no context switches are done between DMA
1407 * buffers from one context and the next. This is suitable for use in the
1408 * X server (which promises to maintain hardware context), or in the
1409 * client-side library when buffers are swapped on behalf of two threads.
1411 ctx.handle = context;
1413 if (flags & DRM_CONTEXT_PRESERVED)
1414 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1415 if (flags & DRM_CONTEXT_2DONLY)
1416 ctx.flags |= _DRM_CONTEXT_2DONLY;
1417 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1422 int drmGetContextFlags(int fd, drm_context_t context,
1423 drm_context_tFlagsPtr flags)
1427 ctx.handle = context;
1428 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1431 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1432 *flags |= DRM_CONTEXT_PRESERVED;
1433 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1434 *flags |= DRM_CONTEXT_2DONLY;
1441 * Free any kernel-level resources allocated with drmCreateContext() associated
1444 * \param fd file descriptor.
1445 * \param handle handle given by drmCreateContext().
1447 * \return zero on success, or a negative value on failure.
1449 * \note May only be called by root.
1452 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1453 * argument in a drm_ctx structure.
1455 int drmDestroyContext(int fd, drm_context_t handle)
1458 ctx.handle = handle;
1459 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1464 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1467 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1469 *handle = draw.handle;
1473 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1476 draw.handle = handle;
1477 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1482 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1483 drm_drawable_info_type_t type, unsigned int num,
1486 drm_update_draw_t update;
1488 update.handle = handle;
1491 update.data = (unsigned long long)(unsigned long)data;
1493 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1500 * Acquire the AGP device.
1502 * Must be called before any of the other AGP related calls.
1504 * \param fd file descriptor.
1506 * \return zero on success, or a negative value on failure.
1509 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1511 int drmAgpAcquire(int fd)
1513 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1520 * Release the AGP device.
1522 * \param fd file descriptor.
1524 * \return zero on success, or a negative value on failure.
1527 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1529 int drmAgpRelease(int fd)
1531 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1540 * \param fd file descriptor.
1541 * \param mode AGP mode.
1543 * \return zero on success, or a negative value on failure.
1546 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1547 * argument in a drm_agp_mode structure.
1549 int drmAgpEnable(int fd, unsigned long mode)
1554 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1561 * Allocate a chunk of AGP memory.
1563 * \param fd file descriptor.
1564 * \param size requested memory size in bytes. Will be rounded to page boundary.
1565 * \param type type of memory to allocate.
1566 * \param address if not zero, will be set to the physical address of the
1568 * \param handle on success will be set to a handle of the allocated memory.
1570 * \return zero on success, or a negative value on failure.
1573 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1574 * arguments in a drm_agp_buffer structure.
1576 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1577 unsigned long *address, drm_handle_t *handle)
1581 *handle = DRM_AGP_NO_HANDLE;
1585 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1588 *address = b.physical;
1595 * Free a chunk of AGP memory.
1597 * \param fd file descriptor.
1598 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1600 * \return zero on success, or a negative value on failure.
1603 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1604 * argument in a drm_agp_buffer structure.
1606 int drmAgpFree(int fd, drm_handle_t handle)
1612 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1619 * Bind a chunk of AGP memory.
1621 * \param fd file descriptor.
1622 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1623 * \param offset offset in bytes. It will round to page boundary.
1625 * \return zero on success, or a negative value on failure.
1628 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1629 * argument in a drm_agp_binding structure.
1631 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1633 drm_agp_binding_t b;
1637 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1644 * Unbind a chunk of AGP memory.
1646 * \param fd file descriptor.
1647 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1649 * \return zero on success, or a negative value on failure.
1652 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1653 * the argument in a drm_agp_binding structure.
1655 int drmAgpUnbind(int fd, drm_handle_t handle)
1657 drm_agp_binding_t b;
1661 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1668 * Get AGP driver major version number.
1670 * \param fd file descriptor.
1672 * \return major version number on success, or a negative value on failure..
1675 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1676 * necessary information in a drm_agp_info structure.
1678 int drmAgpVersionMajor(int fd)
1682 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1684 return i.agp_version_major;
1689 * Get AGP driver minor version number.
1691 * \param fd file descriptor.
1693 * \return minor version number on success, or a negative value on failure.
1696 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1697 * necessary information in a drm_agp_info structure.
1699 int drmAgpVersionMinor(int fd)
1703 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1705 return i.agp_version_minor;
1712 * \param fd file descriptor.
1714 * \return mode on success, or zero on failure.
1717 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1718 * necessary information in a drm_agp_info structure.
1720 unsigned long drmAgpGetMode(int fd)
1724 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1731 * Get AGP aperture base.
1733 * \param fd file descriptor.
1735 * \return aperture base on success, zero on failure.
1738 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1739 * necessary information in a drm_agp_info structure.
1741 unsigned long drmAgpBase(int fd)
1745 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1747 return i.aperture_base;
1752 * Get AGP aperture size.
1754 * \param fd file descriptor.
1756 * \return aperture size on success, zero on failure.
1759 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1760 * necessary information in a drm_agp_info structure.
1762 unsigned long drmAgpSize(int fd)
1766 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1768 return i.aperture_size;
1773 * Get used AGP memory.
1775 * \param fd file descriptor.
1777 * \return memory used on success, or zero on failure.
1780 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1781 * necessary information in a drm_agp_info structure.
1783 unsigned long drmAgpMemoryUsed(int fd)
1787 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1789 return i.memory_used;
1794 * Get available AGP memory.
1796 * \param fd file descriptor.
1798 * \return memory available on success, or zero on failure.
1801 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1802 * necessary information in a drm_agp_info structure.
1804 unsigned long drmAgpMemoryAvail(int fd)
1808 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1810 return i.memory_allowed;
1815 * Get hardware vendor ID.
1817 * \param fd file descriptor.
1819 * \return vendor ID on success, or zero on failure.
1822 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1823 * necessary information in a drm_agp_info structure.
1825 unsigned int drmAgpVendorId(int fd)
1829 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1836 * Get hardware device ID.
1838 * \param fd file descriptor.
1840 * \return zero on success, or zero on failure.
1843 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1844 * necessary information in a drm_agp_info structure.
1846 unsigned int drmAgpDeviceId(int fd)
1850 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1855 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1857 drm_scatter_gather_t sg;
1862 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1864 *handle = sg.handle;
1868 int drmScatterGatherFree(int fd, drm_handle_t handle)
1870 drm_scatter_gather_t sg;
1874 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1882 * \param fd file descriptor.
1883 * \param vbl pointer to a drmVBlank structure.
1885 * \return zero on success, or a negative value on failure.
1888 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1890 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1895 ret = drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1896 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1897 } while (ret && errno == EINTR);
1902 int drmError(int err, const char *label)
1905 case DRM_ERR_NO_DEVICE:
1906 fprintf(stderr, "%s: no device\n", label);
1908 case DRM_ERR_NO_ACCESS:
1909 fprintf(stderr, "%s: no access\n", label);
1911 case DRM_ERR_NOT_ROOT:
1912 fprintf(stderr, "%s: not root\n", label);
1914 case DRM_ERR_INVALID:
1915 fprintf(stderr, "%s: invalid args\n", label);
1920 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1928 * Install IRQ handler.
1930 * \param fd file descriptor.
1931 * \param irq IRQ number.
1933 * \return zero on success, or a negative value on failure.
1936 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1937 * argument in a drm_control structure.
1939 int drmCtlInstHandler(int fd, int irq)
1943 ctl.func = DRM_INST_HANDLER;
1945 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1952 * Uninstall IRQ handler.
1954 * \param fd file descriptor.
1956 * \return zero on success, or a negative value on failure.
1959 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1960 * argument in a drm_control structure.
1962 int drmCtlUninstHandler(int fd)
1966 ctl.func = DRM_UNINST_HANDLER;
1968 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1973 int drmFinish(int fd, int context, drmLockFlags flags)
1977 lock.context = context;
1979 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1980 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1981 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1982 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1983 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1984 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1985 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
1991 * Get IRQ from bus ID.
1993 * \param fd file descriptor.
1994 * \param busnum bus number.
1995 * \param devnum device number.
1996 * \param funcnum function number.
1998 * \return IRQ number on success, or a negative value on failure.
2001 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2002 * arguments in a drm_irq_busid structure.
2004 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2010 p.funcnum = funcnum;
2011 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2016 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2018 drmHashEntry *entry = drmGetEntry(fd);
2020 if (drmHashInsert(entry->tagTable, context, tag)) {
2021 drmHashDelete(entry->tagTable, context);
2022 drmHashInsert(entry->tagTable, context, tag);
2027 int drmDelContextTag(int fd, drm_context_t context)
2029 drmHashEntry *entry = drmGetEntry(fd);
2031 return drmHashDelete(entry->tagTable, context);
2034 void *drmGetContextTag(int fd, drm_context_t context)
2036 drmHashEntry *entry = drmGetEntry(fd);
2039 if (drmHashLookup(entry->tagTable, context, &value))
2045 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2046 drm_handle_t handle)
2048 drm_ctx_priv_map_t map;
2050 map.ctx_id = ctx_id;
2051 map.handle = (void *)handle;
2053 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2058 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2059 drm_handle_t *handle)
2061 drm_ctx_priv_map_t map;
2063 map.ctx_id = ctx_id;
2065 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2068 *handle = (drm_handle_t)map.handle;
2073 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2074 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2080 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2082 *offset = map.offset;
2086 *handle = (unsigned long)map.handle;
2091 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2092 unsigned long *magic, unsigned long *iocs)
2094 drm_client_t client;
2097 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2099 *auth = client.auth;
2102 *magic = client.magic;
2103 *iocs = client.iocs;
2107 int drmGetStats(int fd, drmStatsT *stats)
2112 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2116 memset(stats, 0, sizeof(*stats));
2117 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2121 stats->data[i].long_format = "%-20.20s"; \
2122 stats->data[i].rate_format = "%8.8s"; \
2123 stats->data[i].isvalue = 1; \
2124 stats->data[i].verbose = 0
2127 stats->data[i].long_format = "%-20.20s"; \
2128 stats->data[i].rate_format = "%5.5s"; \
2129 stats->data[i].isvalue = 0; \
2130 stats->data[i].mult_names = "kgm"; \
2131 stats->data[i].mult = 1000; \
2132 stats->data[i].verbose = 0
2135 stats->data[i].long_format = "%-20.20s"; \
2136 stats->data[i].rate_format = "%5.5s"; \
2137 stats->data[i].isvalue = 0; \
2138 stats->data[i].mult_names = "KGM"; \
2139 stats->data[i].mult = 1024; \
2140 stats->data[i].verbose = 0
2143 stats->count = s.count;
2144 for (i = 0; i < s.count; i++) {
2145 stats->data[i].value = s.data[i].value;
2146 switch (s.data[i].type) {
2147 case _DRM_STAT_LOCK:
2148 stats->data[i].long_name = "Lock";
2149 stats->data[i].rate_name = "Lock";
2152 case _DRM_STAT_OPENS:
2153 stats->data[i].long_name = "Opens";
2154 stats->data[i].rate_name = "O";
2156 stats->data[i].verbose = 1;
2158 case _DRM_STAT_CLOSES:
2159 stats->data[i].long_name = "Closes";
2160 stats->data[i].rate_name = "Lock";
2162 stats->data[i].verbose = 1;
2164 case _DRM_STAT_IOCTLS:
2165 stats->data[i].long_name = "Ioctls";
2166 stats->data[i].rate_name = "Ioc/s";
2169 case _DRM_STAT_LOCKS:
2170 stats->data[i].long_name = "Locks";
2171 stats->data[i].rate_name = "Lck/s";
2174 case _DRM_STAT_UNLOCKS:
2175 stats->data[i].long_name = "Unlocks";
2176 stats->data[i].rate_name = "Unl/s";
2180 stats->data[i].long_name = "IRQs";
2181 stats->data[i].rate_name = "IRQ/s";
2184 case _DRM_STAT_PRIMARY:
2185 stats->data[i].long_name = "Primary Bytes";
2186 stats->data[i].rate_name = "PB/s";
2189 case _DRM_STAT_SECONDARY:
2190 stats->data[i].long_name = "Secondary Bytes";
2191 stats->data[i].rate_name = "SB/s";
2195 stats->data[i].long_name = "DMA";
2196 stats->data[i].rate_name = "DMA/s";
2199 case _DRM_STAT_SPECIAL:
2200 stats->data[i].long_name = "Special DMA";
2201 stats->data[i].rate_name = "dma/s";
2204 case _DRM_STAT_MISSED:
2205 stats->data[i].long_name = "Miss";
2206 stats->data[i].rate_name = "Ms/s";
2209 case _DRM_STAT_VALUE:
2210 stats->data[i].long_name = "Value";
2211 stats->data[i].rate_name = "Value";
2214 case _DRM_STAT_BYTE:
2215 stats->data[i].long_name = "Bytes";
2216 stats->data[i].rate_name = "B/s";
2219 case _DRM_STAT_COUNT:
2221 stats->data[i].long_name = "Count";
2222 stats->data[i].rate_name = "Cnt/s";
2231 * Issue a set-version ioctl.
2233 * \param fd file descriptor.
2234 * \param drmCommandIndex command index
2235 * \param data source pointer of the data to be read and written.
2236 * \param size size of the data to be read and written.
2238 * \return zero on success, or a negative value on failure.
2241 * It issues a read-write ioctl given by
2242 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2244 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2247 drm_set_version_t sv;
2249 sv.drm_di_major = version->drm_di_major;
2250 sv.drm_di_minor = version->drm_di_minor;
2251 sv.drm_dd_major = version->drm_dd_major;
2252 sv.drm_dd_minor = version->drm_dd_minor;
2254 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2258 version->drm_di_major = sv.drm_di_major;
2259 version->drm_di_minor = sv.drm_di_minor;
2260 version->drm_dd_major = sv.drm_dd_major;
2261 version->drm_dd_minor = sv.drm_dd_minor;
2267 * Send a device-specific command.
2269 * \param fd file descriptor.
2270 * \param drmCommandIndex command index
2272 * \return zero on success, or a negative value on failure.
2275 * It issues a ioctl given by
2276 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2278 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2280 void *data = NULL; /* dummy */
2281 unsigned long request;
2283 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2285 if (drmIoctl(fd, request, data)) {
2293 * Send a device-specific read command.
2295 * \param fd file descriptor.
2296 * \param drmCommandIndex command index
2297 * \param data destination pointer of the data to be read.
2298 * \param size size of the data to be read.
2300 * \return zero on success, or a negative value on failure.
2303 * It issues a read ioctl given by
2304 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2306 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2309 unsigned long request;
2311 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2312 DRM_COMMAND_BASE + drmCommandIndex, size);
2314 if (drmIoctl(fd, request, data)) {
2322 * Send a device-specific write command.
2324 * \param fd file descriptor.
2325 * \param drmCommandIndex command index
2326 * \param data source pointer of the data to be written.
2327 * \param size size of the data to be written.
2329 * \return zero on success, or a negative value on failure.
2332 * It issues a write ioctl given by
2333 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2335 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2338 unsigned long request;
2340 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2341 DRM_COMMAND_BASE + drmCommandIndex, size);
2343 if (drmIoctl(fd, request, data)) {
2351 * Send a device-specific read-write command.
2353 * \param fd file descriptor.
2354 * \param drmCommandIndex command index
2355 * \param data source pointer of the data to be read and written.
2356 * \param size size of the data to be read and written.
2358 * \return zero on success, or a negative value on failure.
2361 * It issues a read-write ioctl given by
2362 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2364 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2367 unsigned long request;
2369 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2370 DRM_COMMAND_BASE + drmCommandIndex, size);
2372 if (drmIoctl(fd, request, data))
2377 #define DRM_MAX_FDS 16
2382 } connection[DRM_MAX_FDS];
2384 static int nr_fds = 0;
2386 int drmOpenOnce(void *unused,
2393 for (i = 0; i < nr_fds; i++)
2394 if (strcmp(BusID, connection[i].BusID) == 0) {
2395 connection[i].refcount++;
2397 return connection[i].fd;
2400 fd = drmOpen(unused, BusID);
2401 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2404 connection[nr_fds].BusID = strdup(BusID);
2405 connection[nr_fds].fd = fd;
2406 connection[nr_fds].refcount = 1;
2410 fprintf(stderr, "saved connection %d for %s %d\n",
2411 nr_fds, connection[nr_fds].BusID,
2412 strcmp(BusID, connection[nr_fds].BusID));
2419 void drmCloseOnce(int fd)
2423 for (i = 0; i < nr_fds; i++) {
2424 if (fd == connection[i].fd) {
2425 if (--connection[i].refcount == 0) {
2426 drmClose(connection[i].fd);
2427 free(connection[i].BusID);
2430 connection[i] = connection[nr_fds];