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.
46 #include <sys/types.h>
48 #define stat_t struct stat
49 #include <sys/ioctl.h>
54 /* Not all systems have MAP_FAILED defined */
56 #define MAP_FAILED ((void *)-1)
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__DragonFly__)
74 #define DRM_MAJOR 226 /* Linux */
78 #define DRM_MAX_MINOR 16
82 * This definition needs to be changed on some systems if dev_t is a structure.
83 * If there is a header file we can get it from, there would be best.
86 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
89 #define DRM_MSG_VERBOSITY 3
91 #define DRM_NODE_CONTROL 0
92 #define DRM_NODE_RENDER 1
94 static drmServerInfoPtr drm_server_info;
96 void drmSetServerInfo(drmServerInfoPtr info)
98 drm_server_info = info;
102 * Output a message to stderr.
104 * \param format printf() like format string.
107 * This function is a wrapper around vfprintf().
110 static int drmDebugPrint(const char *format, va_list ap)
112 return vfprintf(stderr, format, ap);
115 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
118 drmMsg(const char *format, ...)
122 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
124 va_start(ap, format);
125 if (drm_server_info) {
126 drm_server_info->debug_print(format,ap);
128 drm_debug_print(format, ap);
135 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
137 drm_debug_print = debug_msg_ptr;
140 static void *drmHashTable = NULL; /* Context switch callbacks */
142 void *drmGetHashTable(void)
147 void *drmMalloc(int size)
150 if ((pt = malloc(size)))
155 void drmFree(void *pt)
161 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
162 static char *drmStrdup(const char *s)
169 retval = malloc(strlen(s)+1);
179 * Call ioctl, restarting if it is interupted
182 drmIoctl(int fd, unsigned long request, void *arg)
187 ret = ioctl(fd, request, arg);
188 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
192 static unsigned long drmGetKeyFromFd(int fd)
201 drmHashEntry *drmGetEntry(int fd)
203 unsigned long key = drmGetKeyFromFd(fd);
208 drmHashTable = drmHashCreate();
210 if (drmHashLookup(drmHashTable, key, &value)) {
211 entry = drmMalloc(sizeof(*entry));
214 entry->tagTable = drmHashCreate();
215 drmHashInsert(drmHashTable, key, entry);
223 * Compare two busid strings
228 * \return 1 if matched.
231 * This function compares two bus ID strings. It understands the older
232 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
233 * domain, b is bus, d is device, f is function.
235 static int drmMatchBusID(const char *id1, const char *id2)
237 /* First, check if the IDs are exactly the same */
238 if (strcasecmp(id1, id2) == 0)
241 /* Try to match old/new-style PCI bus IDs. */
242 if (strncasecmp(id1, "pci", 3) == 0) {
247 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
250 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
255 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
258 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
263 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
272 * Open the DRM device, creating it if necessary.
274 * \param dev major and minor numbers of the device.
275 * \param minor minor number of the device.
277 * \return a file descriptor on success, or a negative value on error.
280 * Assembles the device name from \p minor and opens it, creating the device
281 * special file node with the major and minor numbers specified by \p dev and
282 * parent directory if necessary and was called by root.
284 static int drmOpenDevice(long dev, int minor, int type)
289 mode_t devmode = DRM_DEV_MODE, serv_mode;
290 int isroot = !geteuid();
291 uid_t user = DRM_DEV_UID;
292 gid_t group = DRM_DEV_GID, serv_group;
294 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
295 drmMsg("drmOpenDevice: node name is %s\n", buf);
297 if (drm_server_info) {
298 drm_server_info->get_perms(&serv_group, &serv_mode);
299 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
300 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
301 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
305 if (stat(DRM_DIR_NAME, &st)) {
307 return DRM_ERR_NOT_ROOT;
308 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
309 chown(DRM_DIR_NAME, 0, 0); /* root:root */
310 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
313 /* Check if the device node exists and create it if necessary. */
314 if (stat(buf, &st)) {
316 return DRM_ERR_NOT_ROOT;
318 mknod(buf, S_IFCHR | devmode, dev);
321 if (drm_server_info) {
322 chown(buf, user, group);
326 /* if we modprobed then wait for udev */
330 if (stat(DRM_DIR_NAME, &st)) {
334 if (udev_count == 50)
339 if (stat(buf, &st)) {
343 if (udev_count == 50)
350 fd = open(buf, O_RDWR, 0);
351 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
352 fd, fd < 0 ? strerror(errno) : "OK");
356 /* Check if the device node is not what we expect it to be, and recreate it
357 * and try again if so.
359 if (st.st_rdev != dev) {
361 return DRM_ERR_NOT_ROOT;
363 mknod(buf, S_IFCHR | devmode, dev);
364 if (drm_server_info) {
365 chown(buf, user, group);
369 fd = open(buf, O_RDWR, 0);
370 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
371 fd, fd < 0 ? strerror(errno) : "OK");
375 drmMsg("drmOpenDevice: Open failed\n");
382 * Open the DRM device
384 * \param minor device minor number.
385 * \param create allow to create the device if set.
387 * \return a file descriptor on success, or a negative value on error.
390 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
391 * name from \p minor and opens it.
393 static int drmOpenMinor(int minor, int create, int type)
399 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
401 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
402 if ((fd = open(buf, O_RDWR, 0)) >= 0)
409 * Determine whether the DRM kernel driver has been loaded.
411 * \return 1 if the DRM driver is loaded, 0 otherwise.
414 * Determine the presence of the kernel driver by attempting to open the 0
415 * minor and get version information. For backward compatibility with older
416 * Linux implementations, /proc/dri is also checked.
418 int drmAvailable(void)
420 drmVersionPtr version;
424 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
426 /* Try proc for backward Linux compatibility */
427 if (!access("/proc/dri/0", R_OK))
433 if ((version = drmGetVersion(fd))) {
435 drmFreeVersion(version);
444 * Open the device by bus ID.
446 * \param busid bus ID.
448 * \return a file descriptor on success, or a negative value on error.
451 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
452 * comparing the device bus ID with the one supplied.
454 * \sa drmOpenMinor() and drmGetBusid().
456 static int drmOpenByBusid(const char *busid)
463 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
464 for (i = 0; i < DRM_MAX_MINOR; i++) {
465 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
466 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
470 sv.drm_dd_major = -1; /* Don't care */
471 sv.drm_dd_minor = -1; /* Don't care */
472 drmSetInterfaceVersion(fd, &sv);
473 buf = drmGetBusid(fd);
474 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
475 if (buf && drmMatchBusID(buf, busid)) {
489 * Open the device by name.
491 * \param name driver name.
493 * \return a file descriptor on success, or a negative value on error.
496 * This function opens the first minor number that matches the driver name and
497 * isn't already in use. If it's in use it then it will already have a bus ID
500 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
502 static int drmOpenByName(const char *name)
506 drmVersionPtr version;
509 if (!drmAvailable()) {
510 if (!drm_server_info) {
514 /* try to load the kernel module now */
515 if (!drm_server_info->load_module(name)) {
516 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
523 * Open the first minor number that matches the driver name and isn't
524 * already in use. If it's in use it will have a busid assigned already.
526 for (i = 0; i < DRM_MAX_MINOR; i++) {
527 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
528 if ((version = drmGetVersion(fd))) {
529 if (!strcmp(version->name, name)) {
530 drmFreeVersion(version);
531 id = drmGetBusid(fd);
532 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
541 drmFreeVersion(version);
549 /* Backward-compatibility /proc support */
550 for (i = 0; i < 8; i++) {
551 char proc_name[64], buf[512];
552 char *driver, *pt, *devstring;
555 sprintf(proc_name, "/proc/dri/%d/name", i);
556 if ((fd = open(proc_name, 0, 0)) >= 0) {
557 retcode = read(fd, buf, sizeof(buf)-1);
560 buf[retcode-1] = '\0';
561 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
563 if (*pt) { /* Device is next */
565 if (!strcmp(driver, name)) { /* Match */
566 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
568 if (*pt) { /* Found busid */
569 return drmOpenByBusid(++pt);
570 } else { /* No busid */
571 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
585 * Open the DRM device.
587 * Looks up the specified name and bus ID, and opens the device found. The
588 * entry in /dev/dri is created if necessary and if called by root.
590 * \param name driver name. Not referenced if bus ID is supplied.
591 * \param busid bus ID. Zero if not known.
593 * \return a file descriptor on success, or a negative value on error.
596 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
599 int drmOpen(const char *name, const char *busid)
601 if (!drmAvailable() && name != NULL && drm_server_info) {
602 /* try to load the kernel */
603 if (!drm_server_info->load_module(name)) {
604 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
610 int fd = drmOpenByBusid(busid);
616 return drmOpenByName(name);
621 int drmOpenControl(int minor)
623 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
627 * Free the version information returned by drmGetVersion().
629 * \param v pointer to the version information.
632 * It frees the memory pointed by \p %v as well as all the non-null strings
635 void drmFreeVersion(drmVersionPtr v)
647 * Free the non-public version information returned by the kernel.
649 * \param v pointer to the version information.
652 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
653 * the non-null strings pointers in it.
655 static void drmFreeKernelVersion(drm_version_t *v)
667 * Copy version information.
669 * \param d destination pointer.
670 * \param s source pointer.
673 * Used by drmGetVersion() to translate the information returned by the ioctl
674 * interface in a private structure into the public structure counterpart.
676 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
678 d->version_major = s->version_major;
679 d->version_minor = s->version_minor;
680 d->version_patchlevel = s->version_patchlevel;
681 d->name_len = s->name_len;
682 d->name = drmStrdup(s->name);
683 d->date_len = s->date_len;
684 d->date = drmStrdup(s->date);
685 d->desc_len = s->desc_len;
686 d->desc = drmStrdup(s->desc);
691 * Query the driver version information.
693 * \param fd file descriptor.
695 * \return pointer to a drmVersion structure which should be freed with
698 * \note Similar information is available via /proc/dri.
701 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
702 * first with zeros to get the string lengths, and then the actually strings.
703 * It also null-terminates them since they might not be already.
705 drmVersionPtr drmGetVersion(int fd)
707 drmVersionPtr retval;
708 drm_version_t *version = drmMalloc(sizeof(*version));
710 version->name_len = 0;
711 version->name = NULL;
712 version->date_len = 0;
713 version->date = NULL;
714 version->desc_len = 0;
715 version->desc = NULL;
717 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
718 drmFreeKernelVersion(version);
722 if (version->name_len)
723 version->name = drmMalloc(version->name_len + 1);
724 if (version->date_len)
725 version->date = drmMalloc(version->date_len + 1);
726 if (version->desc_len)
727 version->desc = drmMalloc(version->desc_len + 1);
729 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
730 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
731 drmFreeKernelVersion(version);
735 /* The results might not be null-terminated strings, so terminate them. */
736 if (version->name_len) version->name[version->name_len] = '\0';
737 if (version->date_len) version->date[version->date_len] = '\0';
738 if (version->desc_len) version->desc[version->desc_len] = '\0';
740 retval = drmMalloc(sizeof(*retval));
741 drmCopyVersion(retval, version);
742 drmFreeKernelVersion(version);
748 * Get version information for the DRM user space library.
750 * This version number is driver independent.
752 * \param fd file descriptor.
754 * \return version information.
757 * This function allocates and fills a drm_version structure with a hard coded
760 drmVersionPtr drmGetLibVersion(int fd)
762 drm_version_t *version = drmMalloc(sizeof(*version));
765 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
766 * revision 1.0.x = original DRM interface with no drmGetLibVersion
767 * entry point and many drm<Device> extensions
768 * revision 1.1.x = added drmCommand entry points for device extensions
769 * added drmGetLibVersion to identify libdrm.a version
770 * revision 1.2.x = added drmSetInterfaceVersion
771 * modified drmOpen to handle both busid and name
772 * revision 1.3.x = added server + memory manager
774 version->version_major = 1;
775 version->version_minor = 3;
776 version->version_patchlevel = 0;
778 return (drmVersionPtr)version;
783 * Free the bus ID information.
785 * \param busid bus ID information string as given by drmGetBusid().
788 * This function is just frees the memory pointed by \p busid.
790 void drmFreeBusid(const char *busid)
792 drmFree((void *)busid);
797 * Get the bus ID of the device.
799 * \param fd file descriptor.
801 * \return bus ID string.
804 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
805 * get the string length and data, passing the arguments in a drm_unique
808 char *drmGetBusid(int fd)
815 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
817 u.unique = drmMalloc(u.unique_len + 1);
818 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
820 u.unique[u.unique_len] = '\0';
827 * Set the bus ID of the device.
829 * \param fd file descriptor.
830 * \param busid bus ID string.
832 * \return zero on success, negative on failure.
835 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
836 * the arguments in a drm_unique structure.
838 int drmSetBusid(int fd, const char *busid)
842 u.unique = (char *)busid;
843 u.unique_len = strlen(busid);
845 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
851 int drmGetMagic(int fd, drm_magic_t * magic)
856 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
862 int drmAuthMagic(int fd, drm_magic_t magic)
867 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
873 * Specifies a range of memory that is available for mapping by a
876 * \param fd file descriptor.
877 * \param offset usually the physical address. The actual meaning depends of
878 * the \p type parameter. See below.
879 * \param size of the memory in bytes.
880 * \param type type of the memory to be mapped.
881 * \param flags combination of several flags to modify the function actions.
882 * \param handle will be set to a value that may be used as the offset
883 * parameter for mmap().
885 * \return zero on success or a negative value on error.
887 * \par Mapping the frame buffer
888 * For the frame buffer
889 * - \p offset will be the physical address of the start of the frame buffer,
890 * - \p size will be the size of the frame buffer in bytes, and
891 * - \p type will be DRM_FRAME_BUFFER.
894 * The area mapped will be uncached. If MTRR support is available in the
895 * kernel, the frame buffer area will be set to write combining.
897 * \par Mapping the MMIO register area
898 * For the MMIO register area,
899 * - \p offset will be the physical address of the start of the register area,
900 * - \p size will be the size of the register area bytes, and
901 * - \p type will be DRM_REGISTERS.
903 * The area mapped will be uncached.
905 * \par Mapping the SAREA
907 * - \p offset will be ignored and should be set to zero,
908 * - \p size will be the desired size of the SAREA in bytes,
909 * - \p type will be DRM_SHM.
912 * A shared memory area of the requested size will be created and locked in
913 * kernel memory. This area may be mapped into client-space by using the handle
916 * \note May only be called by root.
919 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
920 * the arguments in a drm_map structure.
922 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
923 drmMapFlags flags, drm_handle_t *handle)
932 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
935 *handle = (drm_handle_t)map.handle;
939 int drmRmMap(int fd, drm_handle_t handle)
943 map.handle = (void *)handle;
945 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
951 * Make buffers available for DMA transfers.
953 * \param fd file descriptor.
954 * \param count number of buffers.
955 * \param size size of each buffer.
956 * \param flags buffer allocation flags.
957 * \param agp_offset offset in the AGP aperture
959 * \return number of buffers allocated, negative on error.
962 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
966 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
969 drm_buf_desc_t request;
971 request.count = count;
973 request.low_mark = 0;
974 request.high_mark = 0;
975 request.flags = flags;
976 request.agp_start = agp_offset;
978 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
980 return request.count;
983 int drmMarkBufs(int fd, double low, double high)
991 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
997 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1000 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1001 int retval = -errno;
1006 for (i = 0; i < info.count; i++) {
1007 info.list[i].low_mark = low * info.list[i].count;
1008 info.list[i].high_mark = high * info.list[i].count;
1009 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
1010 int retval = -errno;
1023 * \param fd file descriptor.
1024 * \param count number of buffers to free.
1025 * \param list list of buffers to be freed.
1027 * \return zero on success, or a negative value on failure.
1029 * \note This function is primarily used for debugging.
1032 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1033 * the arguments in a drm_buf_free structure.
1035 int drmFreeBufs(int fd, int count, int *list)
1037 drm_buf_free_t request;
1039 request.count = count;
1040 request.list = list;
1041 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1050 * \param fd file descriptor.
1053 * This function closes the file descriptor.
1055 int drmClose(int fd)
1057 unsigned long key = drmGetKeyFromFd(fd);
1058 drmHashEntry *entry = drmGetEntry(fd);
1060 drmHashDestroy(entry->tagTable);
1063 entry->tagTable = NULL;
1065 drmHashDelete(drmHashTable, key);
1073 * Map a region of memory.
1075 * \param fd file descriptor.
1076 * \param handle handle returned by drmAddMap().
1077 * \param size size in bytes. Must match the size used by drmAddMap().
1078 * \param address will contain the user-space virtual address where the mapping
1081 * \return zero on success, or a negative value on failure.
1084 * This function is a wrapper for mmap().
1086 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1088 static unsigned long pagesize_mask = 0;
1094 pagesize_mask = getpagesize() - 1;
1096 size = (size + pagesize_mask) & ~pagesize_mask;
1098 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1099 if (*address == MAP_FAILED)
1106 * Unmap mappings obtained with drmMap().
1108 * \param address address as given by drmMap().
1109 * \param size size in bytes. Must match the size used by drmMap().
1111 * \return zero on success, or a negative value on failure.
1114 * This function is a wrapper for munmap().
1116 int drmUnmap(drmAddress address, drmSize size)
1118 return munmap(address, size);
1121 drmBufInfoPtr drmGetBufInfo(int fd)
1123 drm_buf_info_t info;
1124 drmBufInfoPtr retval;
1130 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1134 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1137 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1142 retval = drmMalloc(sizeof(*retval));
1143 retval->count = info.count;
1144 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1145 for (i = 0; i < info.count; i++) {
1146 retval->list[i].count = info.list[i].count;
1147 retval->list[i].size = info.list[i].size;
1148 retval->list[i].low_mark = info.list[i].low_mark;
1149 retval->list[i].high_mark = info.list[i].high_mark;
1158 * Map all DMA buffers into client-virtual space.
1160 * \param fd file descriptor.
1162 * \return a pointer to a ::drmBufMap structure.
1164 * \note The client may not use these buffers until obtaining buffer indices
1168 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1169 * information about the buffers in a drm_buf_map structure into the
1170 * client-visible data structures.
1172 drmBufMapPtr drmMapBufs(int fd)
1175 drmBufMapPtr retval;
1180 bufs.virtual = NULL;
1181 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1187 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1190 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1195 retval = drmMalloc(sizeof(*retval));
1196 retval->count = bufs.count;
1197 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1198 for (i = 0; i < bufs.count; i++) {
1199 retval->list[i].idx = bufs.list[i].idx;
1200 retval->list[i].total = bufs.list[i].total;
1201 retval->list[i].used = 0;
1202 retval->list[i].address = bufs.list[i].address;
1212 * Unmap buffers allocated with drmMapBufs().
1214 * \return zero on success, or negative value on failure.
1217 * Calls munmap() for every buffer stored in \p bufs and frees the
1218 * memory allocated by drmMapBufs().
1220 int drmUnmapBufs(drmBufMapPtr bufs)
1224 for (i = 0; i < bufs->count; i++) {
1225 munmap(bufs->list[i].address, bufs->list[i].total);
1228 drmFree(bufs->list);
1235 #define DRM_DMA_RETRY 16
1238 * Reserve DMA buffers.
1240 * \param fd file descriptor.
1243 * \return zero on success, or a negative value on failure.
1246 * Assemble the arguments into a drm_dma structure and keeps issuing the
1247 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1249 int drmDMA(int fd, drmDMAReqPtr request)
1254 dma.context = request->context;
1255 dma.send_count = request->send_count;
1256 dma.send_indices = request->send_list;
1257 dma.send_sizes = request->send_sizes;
1258 dma.flags = request->flags;
1259 dma.request_count = request->request_count;
1260 dma.request_size = request->request_size;
1261 dma.request_indices = request->request_list;
1262 dma.request_sizes = request->request_sizes;
1263 dma.granted_count = 0;
1266 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1267 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1270 request->granted_count = dma.granted_count;
1279 * Obtain heavyweight hardware lock.
1281 * \param fd file descriptor.
1282 * \param context context.
1283 * \param flags flags that determine the sate of the hardware when the function
1286 * \return always zero.
1289 * This function translates the arguments into a drm_lock structure and issue
1290 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1292 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1296 lock.context = context;
1298 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1299 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1300 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1301 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1302 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1303 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1305 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1311 * Release the hardware lock.
1313 * \param fd file descriptor.
1314 * \param context context.
1316 * \return zero on success, or a negative value on failure.
1319 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1320 * argument in a drm_lock structure.
1322 int drmUnlock(int fd, drm_context_t context)
1326 lock.context = context;
1328 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1331 drm_context_t *drmGetReservedContextList(int fd, int *count)
1335 drm_context_t * retval;
1339 res.contexts = NULL;
1340 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1346 if (!(list = drmMalloc(res.count * sizeof(*list))))
1348 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1353 res.contexts = list;
1354 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1357 for (i = 0; i < res.count; i++)
1358 retval[i] = list[i].handle;
1365 void drmFreeReservedContextList(drm_context_t *pt)
1373 * Used by the X server during GLXContext initialization. This causes
1374 * per-context kernel-level resources to be allocated.
1376 * \param fd file descriptor.
1377 * \param handle is set on success. To be used by the client when requesting DMA
1378 * dispatch with drmDMA().
1380 * \return zero on success, or a negative value on failure.
1382 * \note May only be called by root.
1385 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1386 * argument in a drm_ctx structure.
1388 int drmCreateContext(int fd, drm_context_t *handle)
1392 ctx.flags = 0; /* Modified with functions below */
1393 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1395 *handle = ctx.handle;
1399 int drmSwitchToContext(int fd, drm_context_t context)
1403 ctx.handle = context;
1404 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1409 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1414 * Context preserving means that no context switches are done between DMA
1415 * buffers from one context and the next. This is suitable for use in the
1416 * X server (which promises to maintain hardware context), or in the
1417 * client-side library when buffers are swapped on behalf of two threads.
1419 ctx.handle = context;
1421 if (flags & DRM_CONTEXT_PRESERVED)
1422 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1423 if (flags & DRM_CONTEXT_2DONLY)
1424 ctx.flags |= _DRM_CONTEXT_2DONLY;
1425 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1430 int drmGetContextFlags(int fd, drm_context_t context,
1431 drm_context_tFlagsPtr flags)
1435 ctx.handle = context;
1436 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1439 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1440 *flags |= DRM_CONTEXT_PRESERVED;
1441 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1442 *flags |= DRM_CONTEXT_2DONLY;
1449 * Free any kernel-level resources allocated with drmCreateContext() associated
1452 * \param fd file descriptor.
1453 * \param handle handle given by drmCreateContext().
1455 * \return zero on success, or a negative value on failure.
1457 * \note May only be called by root.
1460 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1461 * argument in a drm_ctx structure.
1463 int drmDestroyContext(int fd, drm_context_t handle)
1466 ctx.handle = handle;
1467 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1472 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1475 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1477 *handle = draw.handle;
1481 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1484 draw.handle = handle;
1485 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1490 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1491 drm_drawable_info_type_t type, unsigned int num,
1494 drm_update_draw_t update;
1496 update.handle = handle;
1499 update.data = (unsigned long long)(unsigned long)data;
1501 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1508 * Acquire the AGP device.
1510 * Must be called before any of the other AGP related calls.
1512 * \param fd file descriptor.
1514 * \return zero on success, or a negative value on failure.
1517 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1519 int drmAgpAcquire(int fd)
1521 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1528 * Release the AGP device.
1530 * \param fd file descriptor.
1532 * \return zero on success, or a negative value on failure.
1535 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1537 int drmAgpRelease(int fd)
1539 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1548 * \param fd file descriptor.
1549 * \param mode AGP mode.
1551 * \return zero on success, or a negative value on failure.
1554 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1555 * argument in a drm_agp_mode structure.
1557 int drmAgpEnable(int fd, unsigned long mode)
1562 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1569 * Allocate a chunk of AGP memory.
1571 * \param fd file descriptor.
1572 * \param size requested memory size in bytes. Will be rounded to page boundary.
1573 * \param type type of memory to allocate.
1574 * \param address if not zero, will be set to the physical address of the
1576 * \param handle on success will be set to a handle of the allocated memory.
1578 * \return zero on success, or a negative value on failure.
1581 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1582 * arguments in a drm_agp_buffer structure.
1584 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1585 unsigned long *address, drm_handle_t *handle)
1589 *handle = DRM_AGP_NO_HANDLE;
1593 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1596 *address = b.physical;
1603 * Free a chunk of AGP memory.
1605 * \param fd file descriptor.
1606 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1608 * \return zero on success, or a negative value on failure.
1611 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1612 * argument in a drm_agp_buffer structure.
1614 int drmAgpFree(int fd, drm_handle_t handle)
1620 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1627 * Bind a chunk of AGP memory.
1629 * \param fd file descriptor.
1630 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1631 * \param offset offset in bytes. It will round to page boundary.
1633 * \return zero on success, or a negative value on failure.
1636 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1637 * argument in a drm_agp_binding structure.
1639 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1641 drm_agp_binding_t b;
1645 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1652 * Unbind a chunk of AGP memory.
1654 * \param fd file descriptor.
1655 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1657 * \return zero on success, or a negative value on failure.
1660 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1661 * the argument in a drm_agp_binding structure.
1663 int drmAgpUnbind(int fd, drm_handle_t handle)
1665 drm_agp_binding_t b;
1669 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1676 * Get AGP driver major version number.
1678 * \param fd file descriptor.
1680 * \return major version number on success, or a negative value on failure..
1683 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1684 * necessary information in a drm_agp_info structure.
1686 int drmAgpVersionMajor(int fd)
1690 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1692 return i.agp_version_major;
1697 * Get AGP driver minor version number.
1699 * \param fd file descriptor.
1701 * \return minor version number on success, or a negative value on failure.
1704 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1705 * necessary information in a drm_agp_info structure.
1707 int drmAgpVersionMinor(int fd)
1711 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1713 return i.agp_version_minor;
1720 * \param fd file descriptor.
1722 * \return mode on success, or zero on failure.
1725 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1726 * necessary information in a drm_agp_info structure.
1728 unsigned long drmAgpGetMode(int fd)
1732 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1739 * Get AGP aperture base.
1741 * \param fd file descriptor.
1743 * \return aperture base on success, zero on failure.
1746 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1747 * necessary information in a drm_agp_info structure.
1749 unsigned long drmAgpBase(int fd)
1753 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1755 return i.aperture_base;
1760 * Get AGP aperture size.
1762 * \param fd file descriptor.
1764 * \return aperture size on success, zero on failure.
1767 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1768 * necessary information in a drm_agp_info structure.
1770 unsigned long drmAgpSize(int fd)
1774 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1776 return i.aperture_size;
1781 * Get used AGP memory.
1783 * \param fd file descriptor.
1785 * \return memory used on success, or zero on failure.
1788 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1789 * necessary information in a drm_agp_info structure.
1791 unsigned long drmAgpMemoryUsed(int fd)
1795 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1797 return i.memory_used;
1802 * Get available AGP memory.
1804 * \param fd file descriptor.
1806 * \return memory available on success, or zero on failure.
1809 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1810 * necessary information in a drm_agp_info structure.
1812 unsigned long drmAgpMemoryAvail(int fd)
1816 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1818 return i.memory_allowed;
1823 * Get hardware vendor ID.
1825 * \param fd file descriptor.
1827 * \return vendor ID on success, or zero on failure.
1830 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1831 * necessary information in a drm_agp_info structure.
1833 unsigned int drmAgpVendorId(int fd)
1837 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1844 * Get hardware device ID.
1846 * \param fd file descriptor.
1848 * \return zero on success, or zero on failure.
1851 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1852 * necessary information in a drm_agp_info structure.
1854 unsigned int drmAgpDeviceId(int fd)
1858 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1863 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1865 drm_scatter_gather_t sg;
1870 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1872 *handle = sg.handle;
1876 int drmScatterGatherFree(int fd, drm_handle_t handle)
1878 drm_scatter_gather_t sg;
1882 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1890 * \param fd file descriptor.
1891 * \param vbl pointer to a drmVBlank structure.
1893 * \return zero on success, or a negative value on failure.
1896 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1898 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1900 struct timespec timeout, cur;
1903 ret = clock_gettime(CLOCK_MONOTONIC, &timeout);
1905 fprintf(stderr, "clock_gettime failed: %s\n", strerror(ret));
1911 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1912 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1913 if (ret && errno == EINTR) {
1914 clock_gettime(CLOCK_MONOTONIC, &cur);
1915 /* Timeout after 1s */
1916 if (cur.tv_sec > timeout.tv_sec + 1 ||
1917 (cur.tv_sec == timeout.tv_sec && cur.tv_nsec >=
1924 } while (ret && errno == EINTR);
1930 int drmError(int err, const char *label)
1933 case DRM_ERR_NO_DEVICE:
1934 fprintf(stderr, "%s: no device\n", label);
1936 case DRM_ERR_NO_ACCESS:
1937 fprintf(stderr, "%s: no access\n", label);
1939 case DRM_ERR_NOT_ROOT:
1940 fprintf(stderr, "%s: not root\n", label);
1942 case DRM_ERR_INVALID:
1943 fprintf(stderr, "%s: invalid args\n", label);
1948 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1956 * Install IRQ handler.
1958 * \param fd file descriptor.
1959 * \param irq IRQ number.
1961 * \return zero on success, or a negative value on failure.
1964 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1965 * argument in a drm_control structure.
1967 int drmCtlInstHandler(int fd, int irq)
1971 ctl.func = DRM_INST_HANDLER;
1973 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1980 * Uninstall IRQ handler.
1982 * \param fd file descriptor.
1984 * \return zero on success, or a negative value on failure.
1987 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1988 * argument in a drm_control structure.
1990 int drmCtlUninstHandler(int fd)
1994 ctl.func = DRM_UNINST_HANDLER;
1996 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
2001 int drmFinish(int fd, int context, drmLockFlags flags)
2005 lock.context = context;
2007 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
2008 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
2009 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
2010 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
2011 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
2012 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
2013 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
2019 * Get IRQ from bus ID.
2021 * \param fd file descriptor.
2022 * \param busnum bus number.
2023 * \param devnum device number.
2024 * \param funcnum function number.
2026 * \return IRQ number on success, or a negative value on failure.
2029 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
2030 * arguments in a drm_irq_busid structure.
2032 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
2038 p.funcnum = funcnum;
2039 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
2044 int drmAddContextTag(int fd, drm_context_t context, void *tag)
2046 drmHashEntry *entry = drmGetEntry(fd);
2048 if (drmHashInsert(entry->tagTable, context, tag)) {
2049 drmHashDelete(entry->tagTable, context);
2050 drmHashInsert(entry->tagTable, context, tag);
2055 int drmDelContextTag(int fd, drm_context_t context)
2057 drmHashEntry *entry = drmGetEntry(fd);
2059 return drmHashDelete(entry->tagTable, context);
2062 void *drmGetContextTag(int fd, drm_context_t context)
2064 drmHashEntry *entry = drmGetEntry(fd);
2067 if (drmHashLookup(entry->tagTable, context, &value))
2073 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2074 drm_handle_t handle)
2076 drm_ctx_priv_map_t map;
2078 map.ctx_id = ctx_id;
2079 map.handle = (void *)handle;
2081 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2086 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2087 drm_handle_t *handle)
2089 drm_ctx_priv_map_t map;
2091 map.ctx_id = ctx_id;
2093 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2096 *handle = (drm_handle_t)map.handle;
2101 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2102 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2108 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2110 *offset = map.offset;
2114 *handle = (unsigned long)map.handle;
2119 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2120 unsigned long *magic, unsigned long *iocs)
2122 drm_client_t client;
2125 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2127 *auth = client.auth;
2130 *magic = client.magic;
2131 *iocs = client.iocs;
2135 int drmGetStats(int fd, drmStatsT *stats)
2140 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2144 memset(stats, 0, sizeof(*stats));
2145 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2149 stats->data[i].long_format = "%-20.20s"; \
2150 stats->data[i].rate_format = "%8.8s"; \
2151 stats->data[i].isvalue = 1; \
2152 stats->data[i].verbose = 0
2155 stats->data[i].long_format = "%-20.20s"; \
2156 stats->data[i].rate_format = "%5.5s"; \
2157 stats->data[i].isvalue = 0; \
2158 stats->data[i].mult_names = "kgm"; \
2159 stats->data[i].mult = 1000; \
2160 stats->data[i].verbose = 0
2163 stats->data[i].long_format = "%-20.20s"; \
2164 stats->data[i].rate_format = "%5.5s"; \
2165 stats->data[i].isvalue = 0; \
2166 stats->data[i].mult_names = "KGM"; \
2167 stats->data[i].mult = 1024; \
2168 stats->data[i].verbose = 0
2171 stats->count = s.count;
2172 for (i = 0; i < s.count; i++) {
2173 stats->data[i].value = s.data[i].value;
2174 switch (s.data[i].type) {
2175 case _DRM_STAT_LOCK:
2176 stats->data[i].long_name = "Lock";
2177 stats->data[i].rate_name = "Lock";
2180 case _DRM_STAT_OPENS:
2181 stats->data[i].long_name = "Opens";
2182 stats->data[i].rate_name = "O";
2184 stats->data[i].verbose = 1;
2186 case _DRM_STAT_CLOSES:
2187 stats->data[i].long_name = "Closes";
2188 stats->data[i].rate_name = "Lock";
2190 stats->data[i].verbose = 1;
2192 case _DRM_STAT_IOCTLS:
2193 stats->data[i].long_name = "Ioctls";
2194 stats->data[i].rate_name = "Ioc/s";
2197 case _DRM_STAT_LOCKS:
2198 stats->data[i].long_name = "Locks";
2199 stats->data[i].rate_name = "Lck/s";
2202 case _DRM_STAT_UNLOCKS:
2203 stats->data[i].long_name = "Unlocks";
2204 stats->data[i].rate_name = "Unl/s";
2208 stats->data[i].long_name = "IRQs";
2209 stats->data[i].rate_name = "IRQ/s";
2212 case _DRM_STAT_PRIMARY:
2213 stats->data[i].long_name = "Primary Bytes";
2214 stats->data[i].rate_name = "PB/s";
2217 case _DRM_STAT_SECONDARY:
2218 stats->data[i].long_name = "Secondary Bytes";
2219 stats->data[i].rate_name = "SB/s";
2223 stats->data[i].long_name = "DMA";
2224 stats->data[i].rate_name = "DMA/s";
2227 case _DRM_STAT_SPECIAL:
2228 stats->data[i].long_name = "Special DMA";
2229 stats->data[i].rate_name = "dma/s";
2232 case _DRM_STAT_MISSED:
2233 stats->data[i].long_name = "Miss";
2234 stats->data[i].rate_name = "Ms/s";
2237 case _DRM_STAT_VALUE:
2238 stats->data[i].long_name = "Value";
2239 stats->data[i].rate_name = "Value";
2242 case _DRM_STAT_BYTE:
2243 stats->data[i].long_name = "Bytes";
2244 stats->data[i].rate_name = "B/s";
2247 case _DRM_STAT_COUNT:
2249 stats->data[i].long_name = "Count";
2250 stats->data[i].rate_name = "Cnt/s";
2259 * Issue a set-version ioctl.
2261 * \param fd file descriptor.
2262 * \param drmCommandIndex command index
2263 * \param data source pointer of the data to be read and written.
2264 * \param size size of the data to be read and written.
2266 * \return zero on success, or a negative value on failure.
2269 * It issues a read-write ioctl given by
2270 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2272 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2275 drm_set_version_t sv;
2277 sv.drm_di_major = version->drm_di_major;
2278 sv.drm_di_minor = version->drm_di_minor;
2279 sv.drm_dd_major = version->drm_dd_major;
2280 sv.drm_dd_minor = version->drm_dd_minor;
2282 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2286 version->drm_di_major = sv.drm_di_major;
2287 version->drm_di_minor = sv.drm_di_minor;
2288 version->drm_dd_major = sv.drm_dd_major;
2289 version->drm_dd_minor = sv.drm_dd_minor;
2295 * Send a device-specific command.
2297 * \param fd file descriptor.
2298 * \param drmCommandIndex command index
2300 * \return zero on success, or a negative value on failure.
2303 * It issues a ioctl given by
2304 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2306 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2308 void *data = NULL; /* dummy */
2309 unsigned long request;
2311 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2313 if (drmIoctl(fd, request, data)) {
2321 * Send a device-specific read command.
2323 * \param fd file descriptor.
2324 * \param drmCommandIndex command index
2325 * \param data destination pointer of the data to be read.
2326 * \param size size of the data to be read.
2328 * \return zero on success, or a negative value on failure.
2331 * It issues a read ioctl given by
2332 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2334 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2337 unsigned long request;
2339 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2340 DRM_COMMAND_BASE + drmCommandIndex, size);
2342 if (drmIoctl(fd, request, data)) {
2350 * Send a device-specific write command.
2352 * \param fd file descriptor.
2353 * \param drmCommandIndex command index
2354 * \param data source pointer of the data to be written.
2355 * \param size size of the data to be written.
2357 * \return zero on success, or a negative value on failure.
2360 * It issues a write ioctl given by
2361 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2363 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2366 unsigned long request;
2368 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2369 DRM_COMMAND_BASE + drmCommandIndex, size);
2371 if (drmIoctl(fd, request, data)) {
2379 * Send a device-specific read-write command.
2381 * \param fd file descriptor.
2382 * \param drmCommandIndex command index
2383 * \param data source pointer of the data to be read and written.
2384 * \param size size of the data to be read and written.
2386 * \return zero on success, or a negative value on failure.
2389 * It issues a read-write ioctl given by
2390 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2392 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2395 unsigned long request;
2397 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2398 DRM_COMMAND_BASE + drmCommandIndex, size);
2400 if (drmIoctl(fd, request, data))
2405 #define DRM_MAX_FDS 16
2410 } connection[DRM_MAX_FDS];
2412 static int nr_fds = 0;
2414 int drmOpenOnce(void *unused,
2421 for (i = 0; i < nr_fds; i++)
2422 if (strcmp(BusID, connection[i].BusID) == 0) {
2423 connection[i].refcount++;
2425 return connection[i].fd;
2428 fd = drmOpen(unused, BusID);
2429 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2432 connection[nr_fds].BusID = strdup(BusID);
2433 connection[nr_fds].fd = fd;
2434 connection[nr_fds].refcount = 1;
2438 fprintf(stderr, "saved connection %d for %s %d\n",
2439 nr_fds, connection[nr_fds].BusID,
2440 strcmp(BusID, connection[nr_fds].BusID));
2447 void drmCloseOnce(int fd)
2451 for (i = 0; i < nr_fds; i++) {
2452 if (fd == connection[i].fd) {
2453 if (--connection[i].refcount == 0) {
2454 drmClose(connection[i].fd);
2455 free(connection[i].BusID);
2458 connection[i] = connection[nr_fds];
2466 int drmSetMaster(int fd)
2470 fprintf(stderr,"Setting master \n");
2471 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2475 int drmDropMaster(int fd)
2478 fprintf(stderr,"Dropping master \n");
2479 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);