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>
54 /* Not all systems have MAP_FAILED defined */
56 #define MAP_FAILED ((void *)-1)
61 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
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 static drmServerInfoPtr drm_server_info;
93 void drmSetServerInfo(drmServerInfoPtr info)
95 drm_server_info = info;
99 * Output a message to stderr.
101 * \param format printf() like format string.
104 * This function is a wrapper around vfprintf().
107 static int drmDebugPrint(const char *format, va_list ap)
109 return vfprintf(stderr, format, ap);
112 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
115 drmMsg(const char *format, ...)
119 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
121 va_start(ap, format);
122 if (drm_server_info) {
123 drm_server_info->debug_print(format,ap);
125 drm_debug_print(format, ap);
132 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
134 drm_debug_print = debug_msg_ptr;
137 static void *drmHashTable = NULL; /* Context switch callbacks */
139 void *drmGetHashTable(void)
144 void *drmMalloc(int size)
147 if ((pt = malloc(size)))
152 void drmFree(void *pt)
158 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
159 static char *drmStrdup(const char *s)
166 retval = malloc(strlen(s)+1);
176 static unsigned long drmGetKeyFromFd(int fd)
185 drmHashEntry *drmGetEntry(int fd)
187 unsigned long key = drmGetKeyFromFd(fd);
192 drmHashTable = drmHashCreate();
194 if (drmHashLookup(drmHashTable, key, &value)) {
195 entry = drmMalloc(sizeof(*entry));
198 entry->tagTable = drmHashCreate();
199 drmHashInsert(drmHashTable, key, entry);
207 * Compare two busid strings
212 * \return 1 if matched.
215 * This function compares two bus ID strings. It understands the older
216 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
217 * domain, b is bus, d is device, f is function.
219 static int drmMatchBusID(const char *id1, const char *id2)
221 /* First, check if the IDs are exactly the same */
222 if (strcasecmp(id1, id2) == 0)
225 /* Try to match old/new-style PCI bus IDs. */
226 if (strncasecmp(id1, "pci", 3) == 0) {
231 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
234 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
239 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
242 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
247 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
256 * Open the DRM device, creating it if necessary.
258 * \param dev major and minor numbers of the device.
259 * \param minor minor number of the device.
261 * \return a file descriptor on success, or a negative value on error.
264 * Assembles the device name from \p minor and opens it, creating the device
265 * special file node with the major and minor numbers specified by \p dev and
266 * parent directory if necessary and was called by root.
268 static int drmOpenDevice(long dev, int minor)
273 mode_t devmode = DRM_DEV_MODE, serv_mode;
274 int isroot = !geteuid();
275 uid_t user = DRM_DEV_UID;
276 gid_t group = DRM_DEV_GID, serv_group;
278 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
279 drmMsg("drmOpenDevice: node name is %s\n", buf);
281 if (drm_server_info) {
282 drm_server_info->get_perms(&serv_group, &serv_mode);
283 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
284 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
285 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
288 if (stat(DRM_DIR_NAME, &st)) {
290 return DRM_ERR_NOT_ROOT;
291 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
292 chown(DRM_DIR_NAME, 0, 0); /* root:root */
293 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
296 /* Check if the device node exists and create it if necessary. */
297 if (stat(buf, &st)) {
299 return DRM_ERR_NOT_ROOT;
301 mknod(buf, S_IFCHR | devmode, dev);
304 if (drm_server_info) {
305 chown(buf, user, group);
309 fd = open(buf, O_RDWR, 0);
310 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
311 fd, fd < 0 ? strerror(errno) : "OK");
315 /* Check if the device node is not what we expect it to be, and recreate it
316 * and try again if so.
318 if (st.st_rdev != dev) {
320 return DRM_ERR_NOT_ROOT;
322 mknod(buf, S_IFCHR | devmode, dev);
323 if (drm_server_info) {
324 chown(buf, user, group);
328 fd = open(buf, O_RDWR, 0);
329 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
330 fd, fd < 0 ? strerror(errno) : "OK");
334 drmMsg("drmOpenDevice: Open failed\n");
341 * Open the DRM device
343 * \param minor device minor number.
344 * \param create allow to create the device if set.
346 * \return a file descriptor on success, or a negative value on error.
349 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
350 * name from \p minor and opens it.
352 static int drmOpenMinor(int minor, int create)
358 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
360 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
361 if ((fd = open(buf, O_RDWR, 0)) >= 0)
368 * Determine whether the DRM kernel driver has been loaded.
370 * \return 1 if the DRM driver is loaded, 0 otherwise.
373 * Determine the presence of the kernel driver by attempting to open the 0
374 * minor and get version information. For backward compatibility with older
375 * Linux implementations, /proc/dri is also checked.
377 int drmAvailable(void)
379 drmVersionPtr version;
383 if ((fd = drmOpenMinor(0, 1)) < 0) {
385 /* Try proc for backward Linux compatibility */
386 if (!access("/proc/dri/0", R_OK))
392 if ((version = drmGetVersion(fd))) {
394 drmFreeVersion(version);
403 * Open the device by bus ID.
405 * \param busid bus ID.
407 * \return a file descriptor on success, or a negative value on error.
410 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
411 * comparing the device bus ID with the one supplied.
413 * \sa drmOpenMinor() and drmGetBusid().
415 static int drmOpenByBusid(const char *busid)
422 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
423 for (i = 0; i < DRM_MAX_MINOR; i++) {
424 fd = drmOpenMinor(i, 1);
425 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
429 sv.drm_dd_major = -1; /* Don't care */
430 sv.drm_dd_minor = -1; /* Don't care */
431 drmSetInterfaceVersion(fd, &sv);
432 buf = drmGetBusid(fd);
433 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
434 if (buf && drmMatchBusID(buf, busid)) {
448 * Open the device by name.
450 * \param name driver name.
452 * \return a file descriptor on success, or a negative value on error.
455 * This function opens the first minor number that matches the driver name and
456 * isn't already in use. If it's in use it then it will already have a bus ID
459 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
461 static int drmOpenByName(const char *name)
465 drmVersionPtr version;
468 if (!drmAvailable()) {
469 if (!drm_server_info) {
473 /* try to load the kernel module now */
474 if (!drm_server_info->load_module(name)) {
475 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
482 * Open the first minor number that matches the driver name and isn't
483 * already in use. If it's in use it will have a busid assigned already.
485 for (i = 0; i < DRM_MAX_MINOR; i++) {
486 if ((fd = drmOpenMinor(i, 1)) >= 0) {
487 if ((version = drmGetVersion(fd))) {
488 if (!strcmp(version->name, name)) {
489 drmFreeVersion(version);
490 id = drmGetBusid(fd);
491 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
500 drmFreeVersion(version);
508 /* Backward-compatibility /proc support */
509 for (i = 0; i < 8; i++) {
510 char proc_name[64], buf[512];
511 char *driver, *pt, *devstring;
514 sprintf(proc_name, "/proc/dri/%d/name", i);
515 if ((fd = open(proc_name, 0, 0)) >= 0) {
516 retcode = read(fd, buf, sizeof(buf)-1);
519 buf[retcode-1] = '\0';
520 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
522 if (*pt) { /* Device is next */
524 if (!strcmp(driver, name)) { /* Match */
525 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
527 if (*pt) { /* Found busid */
528 return drmOpenByBusid(++pt);
529 } else { /* No busid */
530 return drmOpenDevice(strtol(devstring, NULL, 0),i);
544 * Open the DRM device.
546 * Looks up the specified name and bus ID, and opens the device found. The
547 * entry in /dev/dri is created if necessary and if called by root.
549 * \param name driver name. Not referenced if bus ID is supplied.
550 * \param busid bus ID. Zero if not known.
552 * \return a file descriptor on success, or a negative value on error.
555 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
558 int drmOpen(const char *name, const char *busid)
560 if (!drmAvailable() && name != NULL && drm_server_info) {
561 /* try to load the kernel */
562 if (!drm_server_info->load_module(name)) {
563 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
569 int fd = drmOpenByBusid(busid);
575 return drmOpenByName(name);
582 * Free the version information returned by drmGetVersion().
584 * \param v pointer to the version information.
587 * It frees the memory pointed by \p %v as well as all the non-null strings
590 void drmFreeVersion(drmVersionPtr v)
602 * Free the non-public version information returned by the kernel.
604 * \param v pointer to the version information.
607 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
608 * the non-null strings pointers in it.
610 static void drmFreeKernelVersion(drm_version_t *v)
622 * Copy version information.
624 * \param d destination pointer.
625 * \param s source pointer.
628 * Used by drmGetVersion() to translate the information returned by the ioctl
629 * interface in a private structure into the public structure counterpart.
631 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
633 d->version_major = s->version_major;
634 d->version_minor = s->version_minor;
635 d->version_patchlevel = s->version_patchlevel;
636 d->name_len = s->name_len;
637 d->name = drmStrdup(s->name);
638 d->date_len = s->date_len;
639 d->date = drmStrdup(s->date);
640 d->desc_len = s->desc_len;
641 d->desc = drmStrdup(s->desc);
646 * Query the driver version information.
648 * \param fd file descriptor.
650 * \return pointer to a drmVersion structure which should be freed with
653 * \note Similar information is available via /proc/dri.
656 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
657 * first with zeros to get the string lengths, and then the actually strings.
658 * It also null-terminates them since they might not be already.
660 drmVersionPtr drmGetVersion(int fd)
662 drmVersionPtr retval;
663 drm_version_t *version = drmMalloc(sizeof(*version));
665 version->name_len = 0;
666 version->name = NULL;
667 version->date_len = 0;
668 version->date = NULL;
669 version->desc_len = 0;
670 version->desc = NULL;
672 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
673 drmFreeKernelVersion(version);
677 if (version->name_len)
678 version->name = drmMalloc(version->name_len + 1);
679 if (version->date_len)
680 version->date = drmMalloc(version->date_len + 1);
681 if (version->desc_len)
682 version->desc = drmMalloc(version->desc_len + 1);
684 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
685 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
686 drmFreeKernelVersion(version);
690 /* The results might not be null-terminated strings, so terminate them. */
691 if (version->name_len) version->name[version->name_len] = '\0';
692 if (version->date_len) version->date[version->date_len] = '\0';
693 if (version->desc_len) version->desc[version->desc_len] = '\0';
695 retval = drmMalloc(sizeof(*retval));
696 drmCopyVersion(retval, version);
697 drmFreeKernelVersion(version);
703 * Get version information for the DRM user space library.
705 * This version number is driver independent.
707 * \param fd file descriptor.
709 * \return version information.
712 * This function allocates and fills a drm_version structure with a hard coded
715 drmVersionPtr drmGetLibVersion(int fd)
717 drm_version_t *version = drmMalloc(sizeof(*version));
720 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
721 * revision 1.0.x = original DRM interface with no drmGetLibVersion
722 * entry point and many drm<Device> extensions
723 * revision 1.1.x = added drmCommand entry points for device extensions
724 * added drmGetLibVersion to identify libdrm.a version
725 * revision 1.2.x = added drmSetInterfaceVersion
726 * modified drmOpen to handle both busid and name
727 * revision 1.3.x = added server + memory manager
729 version->version_major = 1;
730 version->version_minor = 3;
731 version->version_patchlevel = 0;
733 return (drmVersionPtr)version;
738 * Free the bus ID information.
740 * \param busid bus ID information string as given by drmGetBusid().
743 * This function is just frees the memory pointed by \p busid.
745 void drmFreeBusid(const char *busid)
747 drmFree((void *)busid);
752 * Get the bus ID of the device.
754 * \param fd file descriptor.
756 * \return bus ID string.
759 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
760 * get the string length and data, passing the arguments in a drm_unique
763 char *drmGetBusid(int fd)
770 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
772 u.unique = drmMalloc(u.unique_len + 1);
773 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
775 u.unique[u.unique_len] = '\0';
782 * Set the bus ID of the device.
784 * \param fd file descriptor.
785 * \param busid bus ID string.
787 * \return zero on success, negative on failure.
790 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
791 * the arguments in a drm_unique structure.
793 int drmSetBusid(int fd, const char *busid)
797 u.unique = (char *)busid;
798 u.unique_len = strlen(busid);
800 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
806 int drmGetMagic(int fd, drm_magic_t * magic)
811 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
817 int drmAuthMagic(int fd, drm_magic_t magic)
822 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
828 * Specifies a range of memory that is available for mapping by a
831 * \param fd file descriptor.
832 * \param offset usually the physical address. The actual meaning depends of
833 * the \p type parameter. See below.
834 * \param size of the memory in bytes.
835 * \param type type of the memory to be mapped.
836 * \param flags combination of several flags to modify the function actions.
837 * \param handle will be set to a value that may be used as the offset
838 * parameter for mmap().
840 * \return zero on success or a negative value on error.
842 * \par Mapping the frame buffer
843 * For the frame buffer
844 * - \p offset will be the physical address of the start of the frame buffer,
845 * - \p size will be the size of the frame buffer in bytes, and
846 * - \p type will be DRM_FRAME_BUFFER.
849 * The area mapped will be uncached. If MTRR support is available in the
850 * kernel, the frame buffer area will be set to write combining.
852 * \par Mapping the MMIO register area
853 * For the MMIO register area,
854 * - \p offset will be the physical address of the start of the register area,
855 * - \p size will be the size of the register area bytes, and
856 * - \p type will be DRM_REGISTERS.
858 * The area mapped will be uncached.
860 * \par Mapping the SAREA
862 * - \p offset will be ignored and should be set to zero,
863 * - \p size will be the desired size of the SAREA in bytes,
864 * - \p type will be DRM_SHM.
867 * A shared memory area of the requested size will be created and locked in
868 * kernel memory. This area may be mapped into client-space by using the handle
871 * \note May only be called by root.
874 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
875 * the arguments in a drm_map structure.
877 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
878 drmMapFlags flags, drm_handle_t *handle)
887 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map))
890 *handle = (drm_handle_t)map.handle;
894 int drmRmMap(int fd, drm_handle_t handle)
898 map.handle = (void *)handle;
900 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map))
906 * Make buffers available for DMA transfers.
908 * \param fd file descriptor.
909 * \param count number of buffers.
910 * \param size size of each buffer.
911 * \param flags buffer allocation flags.
912 * \param agp_offset offset in the AGP aperture
914 * \return number of buffers allocated, negative on error.
917 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
921 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
924 drm_buf_desc_t request;
926 request.count = count;
928 request.low_mark = 0;
929 request.high_mark = 0;
930 request.flags = flags;
931 request.agp_start = agp_offset;
933 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request))
935 return request.count;
938 int drmMarkBufs(int fd, double low, double high)
946 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
952 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
955 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
961 for (i = 0; i < info.count; i++) {
962 info.list[i].low_mark = low * info.list[i].count;
963 info.list[i].high_mark = high * info.list[i].count;
964 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
978 * \param fd file descriptor.
979 * \param count number of buffers to free.
980 * \param list list of buffers to be freed.
982 * \return zero on success, or a negative value on failure.
984 * \note This function is primarily used for debugging.
987 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
988 * the arguments in a drm_buf_free structure.
990 int drmFreeBufs(int fd, int count, int *list)
992 drm_buf_free_t request;
994 request.count = count;
996 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1005 * \param fd file descriptor.
1008 * This function closes the file descriptor.
1010 int drmClose(int fd)
1012 unsigned long key = drmGetKeyFromFd(fd);
1013 drmHashEntry *entry = drmGetEntry(fd);
1015 drmHashDestroy(entry->tagTable);
1018 entry->tagTable = NULL;
1020 drmHashDelete(drmHashTable, key);
1028 * Map a region of memory.
1030 * \param fd file descriptor.
1031 * \param handle handle returned by drmAddMap().
1032 * \param size size in bytes. Must match the size used by drmAddMap().
1033 * \param address will contain the user-space virtual address where the mapping
1036 * \return zero on success, or a negative value on failure.
1039 * This function is a wrapper for mmap().
1041 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1043 static unsigned long pagesize_mask = 0;
1049 pagesize_mask = getpagesize() - 1;
1051 size = (size + pagesize_mask) & ~pagesize_mask;
1053 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1054 if (*address == MAP_FAILED)
1061 * Unmap mappings obtained with drmMap().
1063 * \param address address as given by drmMap().
1064 * \param size size in bytes. Must match the size used by drmMap().
1066 * \return zero on success, or a negative value on failure.
1069 * This function is a wrapper for munmap().
1071 int drmUnmap(drmAddress address, drmSize size)
1073 return munmap(address, size);
1076 drmBufInfoPtr drmGetBufInfo(int fd)
1078 drm_buf_info_t info;
1079 drmBufInfoPtr retval;
1085 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1089 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1092 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1097 retval = drmMalloc(sizeof(*retval));
1098 retval->count = info.count;
1099 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1100 for (i = 0; i < info.count; i++) {
1101 retval->list[i].count = info.list[i].count;
1102 retval->list[i].size = info.list[i].size;
1103 retval->list[i].low_mark = info.list[i].low_mark;
1104 retval->list[i].high_mark = info.list[i].high_mark;
1113 * Map all DMA buffers into client-virtual space.
1115 * \param fd file descriptor.
1117 * \return a pointer to a ::drmBufMap structure.
1119 * \note The client may not use these buffers until obtaining buffer indices
1123 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1124 * information about the buffers in a drm_buf_map structure into the
1125 * client-visible data structures.
1127 drmBufMapPtr drmMapBufs(int fd)
1130 drmBufMapPtr retval;
1135 bufs.virtual = NULL;
1136 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1142 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1145 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1150 retval = drmMalloc(sizeof(*retval));
1151 retval->count = bufs.count;
1152 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1153 for (i = 0; i < bufs.count; i++) {
1154 retval->list[i].idx = bufs.list[i].idx;
1155 retval->list[i].total = bufs.list[i].total;
1156 retval->list[i].used = 0;
1157 retval->list[i].address = bufs.list[i].address;
1167 * Unmap buffers allocated with drmMapBufs().
1169 * \return zero on success, or negative value on failure.
1172 * Calls munmap() for every buffer stored in \p bufs and frees the
1173 * memory allocated by drmMapBufs().
1175 int drmUnmapBufs(drmBufMapPtr bufs)
1179 for (i = 0; i < bufs->count; i++) {
1180 munmap(bufs->list[i].address, bufs->list[i].total);
1183 drmFree(bufs->list);
1190 #define DRM_DMA_RETRY 16
1193 * Reserve DMA buffers.
1195 * \param fd file descriptor.
1198 * \return zero on success, or a negative value on failure.
1201 * Assemble the arguments into a drm_dma structure and keeps issuing the
1202 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1204 int drmDMA(int fd, drmDMAReqPtr request)
1209 dma.context = request->context;
1210 dma.send_count = request->send_count;
1211 dma.send_indices = request->send_list;
1212 dma.send_sizes = request->send_sizes;
1213 dma.flags = request->flags;
1214 dma.request_count = request->request_count;
1215 dma.request_size = request->request_size;
1216 dma.request_indices = request->request_list;
1217 dma.request_sizes = request->request_sizes;
1218 dma.granted_count = 0;
1221 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1222 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1225 request->granted_count = dma.granted_count;
1234 * Obtain heavyweight hardware lock.
1236 * \param fd file descriptor.
1237 * \param context context.
1238 * \param flags flags that determine the sate of the hardware when the function
1241 * \return always zero.
1244 * This function translates the arguments into a drm_lock structure and issue
1245 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1247 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1251 lock.context = context;
1253 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1254 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1255 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1256 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1257 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1258 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1260 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1266 * Release the hardware lock.
1268 * \param fd file descriptor.
1269 * \param context context.
1271 * \return zero on success, or a negative value on failure.
1274 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1275 * argument in a drm_lock structure.
1277 int drmUnlock(int fd, drm_context_t context)
1281 lock.context = context;
1283 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1286 drm_context_t *drmGetReservedContextList(int fd, int *count)
1290 drm_context_t * retval;
1294 res.contexts = NULL;
1295 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1301 if (!(list = drmMalloc(res.count * sizeof(*list))))
1303 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1308 res.contexts = list;
1309 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1312 for (i = 0; i < res.count; i++)
1313 retval[i] = list[i].handle;
1320 void drmFreeReservedContextList(drm_context_t *pt)
1328 * Used by the X server during GLXContext initialization. This causes
1329 * per-context kernel-level resources to be allocated.
1331 * \param fd file descriptor.
1332 * \param handle is set on success. To be used by the client when requesting DMA
1333 * dispatch with drmDMA().
1335 * \return zero on success, or a negative value on failure.
1337 * \note May only be called by root.
1340 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1341 * argument in a drm_ctx structure.
1343 int drmCreateContext(int fd, drm_context_t *handle)
1347 ctx.flags = 0; /* Modified with functions below */
1348 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1350 *handle = ctx.handle;
1354 int drmSwitchToContext(int fd, drm_context_t context)
1358 ctx.handle = context;
1359 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1364 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1369 * Context preserving means that no context switches are done between DMA
1370 * buffers from one context and the next. This is suitable for use in the
1371 * X server (which promises to maintain hardware context), or in the
1372 * client-side library when buffers are swapped on behalf of two threads.
1374 ctx.handle = context;
1376 if (flags & DRM_CONTEXT_PRESERVED)
1377 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1378 if (flags & DRM_CONTEXT_2DONLY)
1379 ctx.flags |= _DRM_CONTEXT_2DONLY;
1380 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1385 int drmGetContextFlags(int fd, drm_context_t context,
1386 drm_context_tFlagsPtr flags)
1390 ctx.handle = context;
1391 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1394 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1395 *flags |= DRM_CONTEXT_PRESERVED;
1396 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1397 *flags |= DRM_CONTEXT_2DONLY;
1404 * Free any kernel-level resources allocated with drmCreateContext() associated
1407 * \param fd file descriptor.
1408 * \param handle handle given by drmCreateContext().
1410 * \return zero on success, or a negative value on failure.
1412 * \note May only be called by root.
1415 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1416 * argument in a drm_ctx structure.
1418 int drmDestroyContext(int fd, drm_context_t handle)
1421 ctx.handle = handle;
1422 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1427 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1430 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1432 *handle = draw.handle;
1436 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1439 draw.handle = handle;
1440 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1445 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1446 drm_drawable_info_type_t type, unsigned int num,
1449 drm_update_draw_t update;
1451 update.handle = handle;
1454 update.data = (unsigned long long)(unsigned long)data;
1456 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1463 * Acquire the AGP device.
1465 * Must be called before any of the other AGP related calls.
1467 * \param fd file descriptor.
1469 * \return zero on success, or a negative value on failure.
1472 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1474 int drmAgpAcquire(int fd)
1476 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1483 * Release the AGP device.
1485 * \param fd file descriptor.
1487 * \return zero on success, or a negative value on failure.
1490 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1492 int drmAgpRelease(int fd)
1494 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1503 * \param fd file descriptor.
1504 * \param mode AGP mode.
1506 * \return zero on success, or a negative value on failure.
1509 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1510 * argument in a drm_agp_mode structure.
1512 int drmAgpEnable(int fd, unsigned long mode)
1517 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1524 * Allocate a chunk of AGP memory.
1526 * \param fd file descriptor.
1527 * \param size requested memory size in bytes. Will be rounded to page boundary.
1528 * \param type type of memory to allocate.
1529 * \param address if not zero, will be set to the physical address of the
1531 * \param handle on success will be set to a handle of the allocated memory.
1533 * \return zero on success, or a negative value on failure.
1536 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1537 * arguments in a drm_agp_buffer structure.
1539 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1540 unsigned long *address, drm_handle_t *handle)
1544 *handle = DRM_AGP_NO_HANDLE;
1548 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1551 *address = b.physical;
1558 * Free a chunk of AGP memory.
1560 * \param fd file descriptor.
1561 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1563 * \return zero on success, or a negative value on failure.
1566 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1567 * argument in a drm_agp_buffer structure.
1569 int drmAgpFree(int fd, drm_handle_t handle)
1575 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b))
1582 * Bind a chunk of AGP memory.
1584 * \param fd file descriptor.
1585 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1586 * \param offset offset in bytes. It will round to page boundary.
1588 * \return zero on success, or a negative value on failure.
1591 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1592 * argument in a drm_agp_binding structure.
1594 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1596 drm_agp_binding_t b;
1600 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b))
1607 * Unbind a chunk of AGP memory.
1609 * \param fd file descriptor.
1610 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1612 * \return zero on success, or a negative value on failure.
1615 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1616 * the argument in a drm_agp_binding structure.
1618 int drmAgpUnbind(int fd, drm_handle_t handle)
1620 drm_agp_binding_t b;
1624 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1631 * Get AGP driver major version number.
1633 * \param fd file descriptor.
1635 * \return major version number on success, or a negative value on failure..
1638 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1639 * necessary information in a drm_agp_info structure.
1641 int drmAgpVersionMajor(int fd)
1645 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1647 return i.agp_version_major;
1652 * Get AGP driver minor version number.
1654 * \param fd file descriptor.
1656 * \return minor version number on success, or a negative value on failure.
1659 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1660 * necessary information in a drm_agp_info structure.
1662 int drmAgpVersionMinor(int fd)
1666 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1668 return i.agp_version_minor;
1675 * \param fd file descriptor.
1677 * \return mode on success, or zero on failure.
1680 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1681 * necessary information in a drm_agp_info structure.
1683 unsigned long drmAgpGetMode(int fd)
1687 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1694 * Get AGP aperture base.
1696 * \param fd file descriptor.
1698 * \return aperture base on success, zero on failure.
1701 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1702 * necessary information in a drm_agp_info structure.
1704 unsigned long drmAgpBase(int fd)
1708 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1710 return i.aperture_base;
1715 * Get AGP aperture size.
1717 * \param fd file descriptor.
1719 * \return aperture size on success, zero on failure.
1722 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1723 * necessary information in a drm_agp_info structure.
1725 unsigned long drmAgpSize(int fd)
1729 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1731 return i.aperture_size;
1736 * Get used AGP memory.
1738 * \param fd file descriptor.
1740 * \return memory used on success, or zero on failure.
1743 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1744 * necessary information in a drm_agp_info structure.
1746 unsigned long drmAgpMemoryUsed(int fd)
1750 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1752 return i.memory_used;
1757 * Get available AGP memory.
1759 * \param fd file descriptor.
1761 * \return memory available on success, or zero on failure.
1764 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1765 * necessary information in a drm_agp_info structure.
1767 unsigned long drmAgpMemoryAvail(int fd)
1771 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1773 return i.memory_allowed;
1778 * Get hardware vendor ID.
1780 * \param fd file descriptor.
1782 * \return vendor ID on success, or zero on failure.
1785 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1786 * necessary information in a drm_agp_info structure.
1788 unsigned int drmAgpVendorId(int fd)
1792 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1799 * Get hardware device ID.
1801 * \param fd file descriptor.
1803 * \return zero on success, or zero on failure.
1806 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1807 * necessary information in a drm_agp_info structure.
1809 unsigned int drmAgpDeviceId(int fd)
1813 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1818 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1820 drm_scatter_gather_t sg;
1825 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1827 *handle = sg.handle;
1831 int drmScatterGatherFree(int fd, drm_handle_t handle)
1833 drm_scatter_gather_t sg;
1837 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg))
1845 * \param fd file descriptor.
1846 * \param vbl pointer to a drmVBlank structure.
1848 * \return zero on success, or a negative value on failure.
1851 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1853 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1858 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1859 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1860 } while (ret && errno == EINTR);
1865 int drmError(int err, const char *label)
1868 case DRM_ERR_NO_DEVICE:
1869 fprintf(stderr, "%s: no device\n", label);
1871 case DRM_ERR_NO_ACCESS:
1872 fprintf(stderr, "%s: no access\n", label);
1874 case DRM_ERR_NOT_ROOT:
1875 fprintf(stderr, "%s: not root\n", label);
1877 case DRM_ERR_INVALID:
1878 fprintf(stderr, "%s: invalid args\n", label);
1883 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1891 * Install IRQ handler.
1893 * \param fd file descriptor.
1894 * \param irq IRQ number.
1896 * \return zero on success, or a negative value on failure.
1899 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1900 * argument in a drm_control structure.
1902 int drmCtlInstHandler(int fd, int irq)
1906 ctl.func = DRM_INST_HANDLER;
1908 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1915 * Uninstall IRQ handler.
1917 * \param fd file descriptor.
1919 * \return zero on success, or a negative value on failure.
1922 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1923 * argument in a drm_control structure.
1925 int drmCtlUninstHandler(int fd)
1929 ctl.func = DRM_UNINST_HANDLER;
1931 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1936 int drmFinish(int fd, int context, drmLockFlags flags)
1940 lock.context = context;
1942 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1943 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1944 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1945 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1946 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1947 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1948 if (ioctl(fd, DRM_IOCTL_FINISH, &lock))
1954 * Get IRQ from bus ID.
1956 * \param fd file descriptor.
1957 * \param busnum bus number.
1958 * \param devnum device number.
1959 * \param funcnum function number.
1961 * \return IRQ number on success, or a negative value on failure.
1964 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1965 * arguments in a drm_irq_busid structure.
1967 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1973 p.funcnum = funcnum;
1974 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1979 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1981 drmHashEntry *entry = drmGetEntry(fd);
1983 if (drmHashInsert(entry->tagTable, context, tag)) {
1984 drmHashDelete(entry->tagTable, context);
1985 drmHashInsert(entry->tagTable, context, tag);
1990 int drmDelContextTag(int fd, drm_context_t context)
1992 drmHashEntry *entry = drmGetEntry(fd);
1994 return drmHashDelete(entry->tagTable, context);
1997 void *drmGetContextTag(int fd, drm_context_t context)
1999 drmHashEntry *entry = drmGetEntry(fd);
2002 if (drmHashLookup(entry->tagTable, context, &value))
2008 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2009 drm_handle_t handle)
2011 drm_ctx_priv_map_t map;
2013 map.ctx_id = ctx_id;
2014 map.handle = (void *)handle;
2016 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2021 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2022 drm_handle_t *handle)
2024 drm_ctx_priv_map_t map;
2026 map.ctx_id = ctx_id;
2028 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2031 *handle = (drm_handle_t)map.handle;
2036 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2037 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2043 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map))
2045 *offset = map.offset;
2049 *handle = (unsigned long)map.handle;
2054 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2055 unsigned long *magic, unsigned long *iocs)
2057 drm_client_t client;
2060 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2062 *auth = client.auth;
2065 *magic = client.magic;
2066 *iocs = client.iocs;
2070 int drmGetStats(int fd, drmStatsT *stats)
2075 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s))
2079 memset(stats, 0, sizeof(*stats));
2080 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2084 stats->data[i].long_format = "%-20.20s"; \
2085 stats->data[i].rate_format = "%8.8s"; \
2086 stats->data[i].isvalue = 1; \
2087 stats->data[i].verbose = 0
2090 stats->data[i].long_format = "%-20.20s"; \
2091 stats->data[i].rate_format = "%5.5s"; \
2092 stats->data[i].isvalue = 0; \
2093 stats->data[i].mult_names = "kgm"; \
2094 stats->data[i].mult = 1000; \
2095 stats->data[i].verbose = 0
2098 stats->data[i].long_format = "%-20.20s"; \
2099 stats->data[i].rate_format = "%5.5s"; \
2100 stats->data[i].isvalue = 0; \
2101 stats->data[i].mult_names = "KGM"; \
2102 stats->data[i].mult = 1024; \
2103 stats->data[i].verbose = 0
2106 stats->count = s.count;
2107 for (i = 0; i < s.count; i++) {
2108 stats->data[i].value = s.data[i].value;
2109 switch (s.data[i].type) {
2110 case _DRM_STAT_LOCK:
2111 stats->data[i].long_name = "Lock";
2112 stats->data[i].rate_name = "Lock";
2115 case _DRM_STAT_OPENS:
2116 stats->data[i].long_name = "Opens";
2117 stats->data[i].rate_name = "O";
2119 stats->data[i].verbose = 1;
2121 case _DRM_STAT_CLOSES:
2122 stats->data[i].long_name = "Closes";
2123 stats->data[i].rate_name = "Lock";
2125 stats->data[i].verbose = 1;
2127 case _DRM_STAT_IOCTLS:
2128 stats->data[i].long_name = "Ioctls";
2129 stats->data[i].rate_name = "Ioc/s";
2132 case _DRM_STAT_LOCKS:
2133 stats->data[i].long_name = "Locks";
2134 stats->data[i].rate_name = "Lck/s";
2137 case _DRM_STAT_UNLOCKS:
2138 stats->data[i].long_name = "Unlocks";
2139 stats->data[i].rate_name = "Unl/s";
2143 stats->data[i].long_name = "IRQs";
2144 stats->data[i].rate_name = "IRQ/s";
2147 case _DRM_STAT_PRIMARY:
2148 stats->data[i].long_name = "Primary Bytes";
2149 stats->data[i].rate_name = "PB/s";
2152 case _DRM_STAT_SECONDARY:
2153 stats->data[i].long_name = "Secondary Bytes";
2154 stats->data[i].rate_name = "SB/s";
2158 stats->data[i].long_name = "DMA";
2159 stats->data[i].rate_name = "DMA/s";
2162 case _DRM_STAT_SPECIAL:
2163 stats->data[i].long_name = "Special DMA";
2164 stats->data[i].rate_name = "dma/s";
2167 case _DRM_STAT_MISSED:
2168 stats->data[i].long_name = "Miss";
2169 stats->data[i].rate_name = "Ms/s";
2172 case _DRM_STAT_VALUE:
2173 stats->data[i].long_name = "Value";
2174 stats->data[i].rate_name = "Value";
2177 case _DRM_STAT_BYTE:
2178 stats->data[i].long_name = "Bytes";
2179 stats->data[i].rate_name = "B/s";
2182 case _DRM_STAT_COUNT:
2184 stats->data[i].long_name = "Count";
2185 stats->data[i].rate_name = "Cnt/s";
2194 * Issue a set-version ioctl.
2196 * \param fd file descriptor.
2197 * \param drmCommandIndex command index
2198 * \param data source pointer of the data to be read and written.
2199 * \param size size of the data to be read and written.
2201 * \return zero on success, or a negative value on failure.
2204 * It issues a read-write ioctl given by
2205 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2207 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2210 drm_set_version_t sv;
2212 sv.drm_di_major = version->drm_di_major;
2213 sv.drm_di_minor = version->drm_di_minor;
2214 sv.drm_dd_major = version->drm_dd_major;
2215 sv.drm_dd_minor = version->drm_dd_minor;
2217 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2221 version->drm_di_major = sv.drm_di_major;
2222 version->drm_di_minor = sv.drm_di_minor;
2223 version->drm_dd_major = sv.drm_dd_major;
2224 version->drm_dd_minor = sv.drm_dd_minor;
2230 * Send a device-specific command.
2232 * \param fd file descriptor.
2233 * \param drmCommandIndex command index
2235 * \return zero on success, or a negative value on failure.
2238 * It issues a ioctl given by
2239 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2241 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2243 void *data = NULL; /* dummy */
2244 unsigned long request;
2246 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2248 if (ioctl(fd, request, data)) {
2256 * Send a device-specific read command.
2258 * \param fd file descriptor.
2259 * \param drmCommandIndex command index
2260 * \param data destination pointer of the data to be read.
2261 * \param size size of the data to be read.
2263 * \return zero on success, or a negative value on failure.
2266 * It issues a read ioctl given by
2267 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2269 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2272 unsigned long request;
2274 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2275 DRM_COMMAND_BASE + drmCommandIndex, size);
2277 if (ioctl(fd, request, data)) {
2285 * Send a device-specific write command.
2287 * \param fd file descriptor.
2288 * \param drmCommandIndex command index
2289 * \param data source pointer of the data to be written.
2290 * \param size size of the data to be written.
2292 * \return zero on success, or a negative value on failure.
2295 * It issues a write ioctl given by
2296 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2298 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2301 unsigned long request;
2303 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2304 DRM_COMMAND_BASE + drmCommandIndex, size);
2306 if (ioctl(fd, request, data)) {
2314 * Send a device-specific read-write command.
2316 * \param fd file descriptor.
2317 * \param drmCommandIndex command index
2318 * \param data source pointer of the data to be read and written.
2319 * \param size size of the data to be read and written.
2321 * \return zero on success, or a negative value on failure.
2324 * It issues a read-write ioctl given by
2325 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2327 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2330 unsigned long request;
2332 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2333 DRM_COMMAND_BASE + drmCommandIndex, size);
2335 if (ioctl(fd, request, data)) {
2344 * DRM_FENCE_FLAG_EMIT
2345 * DRM_FENCE_FLAG_SHAREABLE
2346 * DRM_FENCE_MASK_DRIVER
2349 int drmFenceCreate(int fd, unsigned flags, int class, unsigned type,
2352 drm_fence_arg_t arg;
2354 memset(&arg, 0, sizeof(arg));
2358 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2360 fence->handle = arg.handle;
2361 fence->class = arg.class;
2362 fence->type = arg.type;
2363 fence->flags = arg.flags;
2364 fence->signaled = 0;
2370 * DRM_FENCE_FLAG_SHAREABLE
2371 * DRM_FENCE_MASK_DRIVER
2374 int drmFenceBuffers(int fd, unsigned flags, drmFence *fence)
2376 drm_fence_arg_t arg;
2378 memset(&arg, 0, sizeof(arg));
2381 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2383 fence->handle = arg.handle;
2384 fence->class = arg.class;
2385 fence->type = arg.type;
2386 fence->flags = arg.flags;
2387 fence->signaled = 0;
2391 int drmFenceDestroy(int fd, const drmFence *fence)
2393 drm_fence_arg_t arg;
2395 memset(&arg, 0, sizeof(arg));
2396 arg.handle = fence->handle;
2398 if (ioctl(fd, DRM_IOCTL_FENCE_DESTROY, &arg))
2403 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2405 drm_fence_arg_t arg;
2407 memset(&arg, 0, sizeof(arg));
2408 arg.handle = handle;
2410 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2412 fence->handle = arg.handle;
2413 fence->class = arg.class;
2414 fence->type = arg.type;
2415 fence->flags = arg.flags;
2416 fence->signaled = arg.signaled;
2420 int drmFenceUnreference(int fd, const drmFence *fence)
2422 drm_fence_arg_t arg;
2424 memset(&arg, 0, sizeof(arg));
2425 arg.handle = fence->handle;
2427 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2432 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2434 drm_fence_arg_t arg;
2436 memset(&arg, 0, sizeof(arg));
2437 arg.handle = fence->handle;
2438 arg.type = flush_type;
2440 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2442 fence->class = arg.class;
2443 fence->type = arg.type;
2444 fence->signaled = arg.signaled;
2448 int drmFenceUpdate(int fd, drmFence *fence)
2450 drm_fence_arg_t arg;
2452 memset(&arg, 0, sizeof(arg));
2453 arg.handle = fence->handle;
2455 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2457 fence->class = arg.class;
2458 fence->type = arg.type;
2459 fence->signaled = arg.signaled;
2463 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2466 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2467 ((fenceType & fence->signaled) != fenceType)) {
2468 int ret = drmFenceFlush(fd, fence, fenceType);
2473 *signaled = ((fenceType & fence->signaled) == fenceType);
2480 * DRM_FENCE_FLAG_SHAREABLE
2481 * DRM_FENCE_MASK_DRIVER
2485 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2487 drm_fence_arg_t arg;
2489 memset(&arg, 0, sizeof(arg));
2490 arg.class = fence->class;
2492 arg.handle = fence->handle;
2493 arg.type = emit_type;
2495 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2497 fence->class = arg.class;
2498 fence->type = arg.type;
2499 fence->signaled = arg.signaled;
2505 * DRM_FENCE_FLAG_WAIT_LAZY
2506 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2509 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2511 drm_fence_arg_t arg;
2514 if (flush_type == 0) {
2515 flush_type = fence->type;
2518 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2519 if ((flush_type & fence->signaled) == flush_type) {
2524 memset(&arg, 0, sizeof(arg));
2525 arg.handle = fence->handle;
2526 arg.type = flush_type;
2530 ret = ioctl(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2531 } while (ret != 0 && errno == EAGAIN);
2536 fence->class = arg.class;
2537 fence->type = arg.type;
2538 fence->signaled = arg.signaled;
2542 static int drmAdjustListNodes(drmBOList *list)
2548 while(list->numCurrent < list->numTarget) {
2549 node = (drmBONode *) malloc(sizeof(*node));
2555 DRMLISTADD(&node->head, &list->free);
2558 while(list->numCurrent > list->numTarget) {
2559 l = list->free.next;
2560 if (l == &list->free)
2563 node = DRMLISTENTRY(drmBONode, l, head);
2570 void drmBOFreeList(drmBOList *list)
2575 l = list->list.next;
2576 while(l != &list->list) {
2578 node = DRMLISTENTRY(drmBONode, l, head);
2580 l = list->list.next;
2585 l = list->free.next;
2586 while(l != &list->free) {
2588 node = DRMLISTENTRY(drmBONode, l, head);
2590 l = list->free.next;
2595 int drmBOResetList(drmBOList *list)
2600 ret = drmAdjustListNodes(list);
2604 l = list->list.next;
2605 while (l != &list->list) {
2607 DRMLISTADD(l, &list->free);
2609 l = list->list.next;
2611 return drmAdjustListNodes(list);
2614 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2621 l = list->free.next;
2622 if (l == &list->free) {
2623 node = (drmBONode *) malloc(sizeof(*node));
2631 node = DRMLISTENTRY(drmBONode, l, head);
2636 DRMLISTADD(&node->head, &list->list);
2641 void *drmBOListIterator(drmBOList *list)
2643 void *ret = list->list.next;
2645 if (ret == &list->list)
2650 void *drmBOListNext(drmBOList *list, void *iterator)
2654 drmMMListHead *l = (drmMMListHead *) iterator;
2656 if (ret == &list->list)
2661 drmBO *drmBOListBuf(void *iterator)
2664 drmMMListHead *l = (drmMMListHead *) iterator;
2665 node = DRMLISTENTRY(drmBONode, l, head);
2670 int drmBOCreateList(int numTarget, drmBOList *list)
2672 DRMINITLISTHEAD(&list->list);
2673 DRMINITLISTHEAD(&list->free);
2674 list->numTarget = numTarget;
2675 list->numCurrent = 0;
2676 list->numOnList = 0;
2677 return drmAdjustListNodes(list);
2680 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2682 buf->handle = rep->handle;
2683 buf->flags = rep->flags;
2684 buf->size = rep->size;
2685 buf->offset = rep->offset;
2686 buf->mapHandle = rep->arg_handle;
2687 buf->mask = rep->mask;
2688 buf->start = rep->buffer_start;
2689 buf->fenceFlags = rep->fence_flags;
2690 buf->replyFlags = rep->rep_flags;
2691 buf->pageAlignment = rep->page_alignment;
2692 buf->tileInfo = rep->tile_info;
2693 buf->hwTileStride = rep->hw_tile_stride;
2694 buf->desiredTileStride = rep->desired_tile_stride;
2699 int drmBOCreate(int fd, unsigned long start, unsigned long size,
2700 unsigned pageAlignment, void *user_buffer, drm_bo_type_t type,
2702 unsigned hint, drmBO *buf)
2704 struct drm_bo_create_arg arg;
2705 struct drm_bo_create_req *req = &arg.d.req;
2706 struct drm_bo_info_rep *rep = &arg.d.rep;
2709 memset(buf, 0, sizeof(*buf));
2710 memset(&arg, 0, sizeof(arg));
2715 req->page_alignment = pageAlignment;
2717 buf->virtual = NULL;
2720 case drm_bo_type_dc:
2721 req->buffer_start = start;
2723 case drm_bo_type_user:
2724 req->buffer_start = (unsigned long) user_buffer;
2725 buf->virtual = user_buffer;
2727 case drm_bo_type_fake:
2728 req->buffer_start = start;
2735 ret = ioctl(fd, DRM_IOCTL_BO_CREATE, &arg);
2736 } while (ret != 0 && errno == EAGAIN);
2741 drmBOCopyReply(rep, buf);
2742 buf->mapVirtual = NULL;
2748 int drmBODestroy(int fd, drmBO *buf)
2750 struct drm_bo_handle_arg arg;
2752 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2753 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2754 buf->mapVirtual = NULL;
2755 buf->virtual = NULL;
2758 memset(&arg, 0, sizeof(arg));
2759 arg.handle = buf->handle;
2761 if (ioctl(fd, DRM_IOCTL_BO_DESTROY, &arg))
2768 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2770 struct drm_bo_reference_info_arg arg;
2771 struct drm_bo_handle_arg *req = &arg.d.req;
2772 struct drm_bo_info_rep *rep = &arg.d.rep;
2774 memset(&arg, 0, sizeof(arg));
2775 req->handle = handle;
2777 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2780 drmBOCopyReply(rep, buf);
2781 buf->type = drm_bo_type_dc;
2782 buf->mapVirtual = NULL;
2784 buf->virtual = NULL;
2789 int drmBOUnReference(int fd, drmBO *buf)
2791 struct drm_bo_handle_arg arg;
2793 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2794 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2795 buf->mapVirtual = NULL;
2796 buf->virtual = NULL;
2799 memset(&arg, 0, sizeof(arg));
2800 arg.handle = buf->handle;
2802 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2811 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2812 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2813 * call return an -EBUSY if it can' immediately honor the mapping request.
2816 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2819 struct drm_bo_map_wait_idle_arg arg;
2820 struct drm_bo_info_req *req = &arg.d.req;
2821 struct drm_bo_info_rep *rep = &arg.d.rep;
2825 * Make sure we have a virtual address of the buffer.
2828 if (!buf->virtual && buf->type != drm_bo_type_fake) {
2830 virtual = mmap(0, buf->size + buf->start,
2831 PROT_READ | PROT_WRITE, MAP_SHARED,
2832 fd, buf->mapHandle);
2833 if (virtual == MAP_FAILED) {
2838 buf->mapVirtual = virtual;
2839 buf->virtual = ((char *) virtual) + buf->start;
2842 memset(&arg, 0, sizeof(arg));
2843 req->handle = buf->handle;
2844 req->mask = mapFlags;
2845 req->hint = mapHint;
2848 * May hang if the buffer object is busy.
2849 * This IOCTL synchronizes the buffer.
2853 ret = ioctl(fd, DRM_IOCTL_BO_MAP, &arg);
2854 } while (ret != 0 && errno == EAGAIN);
2859 drmBOCopyReply(rep, buf);
2860 buf->mapFlags = mapFlags;
2862 *address = buf->virtual;
2868 int drmBOUnmap(int fd, drmBO *buf)
2870 struct drm_bo_handle_arg arg;
2872 memset(&arg, 0, sizeof(arg));
2873 arg.handle = buf->handle;
2875 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2881 int drmBOValidate(int fd, drmBO *buf,
2882 drm_u64_t flags, drm_u64_t mask,
2885 struct drm_bo_op_arg arg;
2886 struct drm_bo_op_req *req = &arg.d.req;
2887 struct drm_bo_arg_rep *rep = &arg.d.rep;
2890 memset(&arg, 0, sizeof(arg));
2891 req->bo_req.handle = buf->handle;
2892 req->bo_req.flags = flags;
2893 req->bo_req.mask = mask;
2894 req->bo_req.hint = hint;
2895 req->bo_req.fence_class = 0; /* Backwards compatibility. */
2896 req->op = drm_bo_validate;
2899 ret = ioctl(fd, DRM_IOCTL_BO_OP, &arg);
2900 } while (ret && errno == EAGAIN);
2909 drmBOCopyReply(&rep->bo_info, buf);
2914 int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle)
2916 struct drm_bo_op_arg arg;
2917 struct drm_bo_op_req *req = &arg.d.req;
2918 struct drm_bo_arg_rep *rep = &arg.d.rep;
2921 memset(&arg, 0, sizeof(arg));
2922 req->bo_req.handle = buf->handle;
2923 req->bo_req.flags = flags;
2924 req->arg_handle = fenceHandle;
2925 req->op = drm_bo_fence;
2927 ret = ioctl(fd, DRM_IOCTL_BO_OP, &arg);
2937 int drmBOInfo(int fd, drmBO *buf)
2939 struct drm_bo_reference_info_arg arg;
2940 struct drm_bo_handle_arg *req = &arg.d.req;
2941 struct drm_bo_info_rep *rep = &arg.d.rep;
2944 memset(&arg, 0, sizeof(arg));
2945 req->handle = buf->handle;
2947 ret = ioctl(fd, DRM_IOCTL_BO_INFO, &arg);
2951 drmBOCopyReply(rep, buf);
2955 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2957 struct drm_bo_map_wait_idle_arg arg;
2958 struct drm_bo_info_req *req = &arg.d.req;
2959 struct drm_bo_info_rep *rep = &arg.d.rep;
2962 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2963 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2964 memset(&arg, 0, sizeof(arg));
2965 req->handle = buf->handle;
2969 ret = ioctl(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2970 } while (ret && errno == EAGAIN);
2975 drmBOCopyReply(rep, buf);
2980 int drmBOBusy(int fd, drmBO *buf, int *busy)
2982 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2983 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2988 int ret = drmBOInfo(fd, buf);
2991 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2996 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
3000 drmBONode *node, *cur;
3006 for (l = list->list.next; l != &list->list; l = l->next) {
3007 node = DRMLISTENTRY(drmBONode, l, head);
3008 if (node->buf == buf) {
3014 cur = drmAddListItem(list, buf, flags, mask);
3016 drmMsg("Out of memory creating validate list node.\n");
3024 unsigned memMask = (cur->arg1 | mask) & DRM_BO_MASK_MEM;
3025 unsigned memFlags = cur->arg0 & flags & memMask;
3028 drmMsg("Incompatible memory location requests "
3029 "on validate list.\n");
3030 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3031 cur->arg0, cur->arg1);
3032 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3036 if (mask & cur->arg1 & ~DRM_BO_MASK_MEM & (cur->arg0 ^ flags)) {
3037 drmMsg("Incompatible buffer flag requests "
3038 "on validate list.\n");
3039 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3040 cur->arg0, cur->arg1);
3041 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3046 cur->arg0 = memFlags | ((cur->arg0 | flags) &
3047 cur->arg1 & ~DRM_BO_MASK_MEM);
3053 int drmBOValidateList(int fd, drmBOList *list)
3057 struct drm_bo_op_arg *arg, *first;
3058 struct drm_bo_op_req *req;
3059 struct drm_bo_arg_rep *rep;
3060 drm_u64_t *prevNext = NULL;
3066 for (l = list->list.next; l != &list->list; l = l->next) {
3067 node = DRMLISTENTRY(drmBONode, l, head);
3069 arg = &node->bo_arg;
3076 *prevNext = (unsigned long) arg;
3078 memset(arg, 0, sizeof(*arg));
3079 prevNext = &arg->next;
3080 req->bo_req.handle = node->buf->handle;
3081 req->op = drm_bo_validate;
3082 req->bo_req.flags = node->arg0;
3083 req->bo_req.hint = 0;
3084 req->bo_req.mask = node->arg1;
3085 req->bo_req.fence_class = 0; /* Backwards compat. */
3092 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3093 } while (ret && errno == EAGAIN);
3098 for (l = list->list.next; l != &list->list; l = l->next) {
3099 node = DRMLISTENTRY(drmBONode, l, head);
3100 arg = &node->bo_arg;
3103 if (!arg->handled) {
3104 drmMsg("Unhandled request\n");
3111 drmBOCopyReply(&rep->bo_info, buf);
3117 int drmBOFenceList(int fd, drmBOList *list, unsigned fenceHandle)
3121 struct drm_bo_op_arg *arg, *first;
3122 struct drm_bo_op_req *req;
3123 struct drm_bo_arg_rep *rep;
3124 drm_u64_t *prevNext = NULL;
3126 unsigned fence_flags;
3131 for (l = list->list.next; l != &list->list; l = l->next) {
3132 node = DRMLISTENTRY(drmBONode, l, head);
3134 arg = &node->bo_arg;
3141 *prevNext = (unsigned long) arg;
3143 memset(arg, 0, sizeof(*arg));
3144 prevNext = &arg->next;
3145 req->bo_req.handle = node->buf->handle;
3146 req->op = drm_bo_fence;
3147 req->bo_req.mask = node->arg0;
3148 req->arg_handle = fenceHandle;
3154 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3159 for (l = list->list.next; l != &list->list; l = l->next) {
3160 node = DRMLISTENTRY(drmBONode, l, head);
3162 arg = &node->bo_arg;
3169 drmBOCopyReply(&rep->bo_info, node->buf);
3175 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
3178 struct drm_mm_init_arg arg;
3180 memset(&arg, 0, sizeof(arg));
3182 arg.magic = DRM_BO_INIT_MAGIC;
3183 arg.major = DRM_BO_INIT_MAJOR;
3184 arg.minor = DRM_BO_INIT_MINOR;
3185 arg.p_offset = pOffset;
3187 arg.mem_type = memType;
3189 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3194 int drmMMTakedown(int fd, unsigned memType)
3196 struct drm_mm_type_arg arg;
3198 memset(&arg, 0, sizeof(arg));
3199 arg.mem_type = memType;
3201 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
3206 int drmMMLock(int fd, unsigned memType)
3208 struct drm_mm_type_arg arg;
3211 memset(&arg, 0, sizeof(arg));
3212 arg.mem_type = memType;
3215 ret = ioctl(fd, DRM_IOCTL_MM_LOCK, &arg);
3216 } while (ret && errno == EAGAIN);
3218 return (ret) ? -errno : 0;
3221 int drmMMUnlock(int fd, unsigned memType)
3223 struct drm_mm_type_arg arg;
3226 memset(&arg, 0, sizeof(arg));
3228 arg.mem_type = memType;
3231 ret = ioctl(fd, DRM_IOCTL_MM_UNLOCK, &arg);
3232 } while (ret && errno == EAGAIN);
3234 return (ret) ? -errno : 0;
3237 #define DRM_MAX_FDS 16
3242 } connection[DRM_MAX_FDS];
3244 static int nr_fds = 0;
3246 int drmOpenOnce(void *unused,
3253 for (i = 0; i < nr_fds; i++)
3254 if (strcmp(BusID, connection[i].BusID) == 0) {
3255 connection[i].refcount++;
3257 return connection[i].fd;
3260 fd = drmOpen(unused, BusID);
3261 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
3264 connection[nr_fds].BusID = strdup(BusID);
3265 connection[nr_fds].fd = fd;
3266 connection[nr_fds].refcount = 1;
3270 fprintf(stderr, "saved connection %d for %s %d\n",
3271 nr_fds, connection[nr_fds].BusID,
3272 strcmp(BusID, connection[nr_fds].BusID));
3279 void drmCloseOnce(int fd)
3283 for (i = 0; i < nr_fds; i++) {
3284 if (fd == connection[i].fd) {
3285 if (--connection[i].refcount == 0) {
3286 drmClose(connection[i].fd);
3287 free(connection[i].BusID);
3290 connection[i] = connection[nr_fds];