3 * User-level interface to DRM device
5 * \author Rickard E. (Rik) Faith <faith@valinux.com>
6 * \author Kevin E. Martin <martin@valinux.com>
10 * Copyright 1999 Precision Insight, Inc., Cedar Park, Texas.
11 * Copyright 2000 VA Linux Systems, Inc., Sunnyvale, California.
12 * All Rights Reserved.
14 * Permission is hereby granted, free of charge, to any person obtaining a
15 * copy of this software and associated documentation files (the "Software"),
16 * to deal in the Software without restriction, including without limitation
17 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
18 * and/or sell copies of the Software, and to permit persons to whom the
19 * Software is furnished to do so, subject to the following conditions:
21 * The above copyright notice and this permission notice (including the next
22 * paragraph) shall be included in all copies or substantial portions of the
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
28 * PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
29 * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
30 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
31 * DEALINGS IN THE SOFTWARE.
45 #include <sys/types.h>
47 #define stat_t struct stat
48 #include <sys/ioctl.h>
53 /* Not all systems have MAP_FAILED defined */
55 #define MAP_FAILED ((void *)-1)
60 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
73 #define DRM_MAJOR 226 /* Linux */
77 #define DRM_MAX_MINOR 16
81 * This definition needs to be changed on some systems if dev_t is a structure.
82 * If there is a header file we can get it from, there would be best.
85 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
88 #define DRM_MSG_VERBOSITY 3
90 static drmServerInfoPtr drm_server_info;
92 void drmSetServerInfo(drmServerInfoPtr info)
94 drm_server_info = info;
98 * Output a message to stderr.
100 * \param format printf() like format string.
103 * This function is a wrapper around vfprintf().
106 static int drmDebugPrint(const char *format, va_list ap)
108 return vfprintf(stderr, format, ap);
111 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
114 drmMsg(const char *format, ...)
118 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
120 va_start(ap, format);
121 if (drm_server_info) {
122 drm_server_info->debug_print(format,ap);
124 drm_debug_print(format, ap);
131 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
133 drm_debug_print = debug_msg_ptr;
136 static void *drmHashTable = NULL; /* Context switch callbacks */
138 void *drmGetHashTable(void)
143 void *drmMalloc(int size)
146 if ((pt = malloc(size)))
151 void drmFree(void *pt)
157 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
158 static char *drmStrdup(const char *s)
165 retval = malloc(strlen(s)+1);
175 static unsigned long drmGetKeyFromFd(int fd)
184 drmHashEntry *drmGetEntry(int fd)
186 unsigned long key = drmGetKeyFromFd(fd);
191 drmHashTable = drmHashCreate();
193 if (drmHashLookup(drmHashTable, key, &value)) {
194 entry = drmMalloc(sizeof(*entry));
197 entry->tagTable = drmHashCreate();
198 drmHashInsert(drmHashTable, key, entry);
206 * Compare two busid strings
211 * \return 1 if matched.
214 * This function compares two bus ID strings. It understands the older
215 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
216 * domain, b is bus, d is device, f is function.
218 static int drmMatchBusID(const char *id1, const char *id2)
220 /* First, check if the IDs are exactly the same */
221 if (strcasecmp(id1, id2) == 0)
224 /* Try to match old/new-style PCI bus IDs. */
225 if (strncasecmp(id1, "pci", 3) == 0) {
230 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
233 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
238 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
241 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
246 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
255 * Open the DRM device, creating it if necessary.
257 * \param dev major and minor numbers of the device.
258 * \param minor minor number of the device.
260 * \return a file descriptor on success, or a negative value on error.
263 * Assembles the device name from \p minor and opens it, creating the device
264 * special file node with the major and minor numbers specified by \p dev and
265 * parent directory if necessary and was called by root.
267 static int drmOpenDevice(long dev, int minor)
272 mode_t devmode = DRM_DEV_MODE, serv_mode;
273 int isroot = !geteuid();
274 uid_t user = DRM_DEV_UID;
275 gid_t group = DRM_DEV_GID, serv_group;
277 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
278 drmMsg("drmOpenDevice: node name is %s\n", buf);
280 if (drm_server_info) {
281 drm_server_info->get_perms(&serv_group, &serv_mode);
282 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
283 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
284 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
287 if (stat(DRM_DIR_NAME, &st)) {
289 return DRM_ERR_NOT_ROOT;
290 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
291 chown(DRM_DIR_NAME, 0, 0); /* root:root */
292 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
295 /* Check if the device node exists and create it if necessary. */
296 if (stat(buf, &st)) {
298 return DRM_ERR_NOT_ROOT;
300 mknod(buf, S_IFCHR | devmode, dev);
303 if (drm_server_info) {
304 chown(buf, user, group);
308 fd = open(buf, O_RDWR, 0);
309 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
310 fd, fd < 0 ? strerror(errno) : "OK");
314 /* Check if the device node is not what we expect it to be, and recreate it
315 * and try again if so.
317 if (st.st_rdev != dev) {
319 return DRM_ERR_NOT_ROOT;
321 mknod(buf, S_IFCHR | devmode, dev);
322 if (drm_server_info) {
323 chown(buf, user, group);
327 fd = open(buf, O_RDWR, 0);
328 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
329 fd, fd < 0 ? strerror(errno) : "OK");
333 drmMsg("drmOpenDevice: Open failed\n");
340 * Open the DRM device
342 * \param minor device minor number.
343 * \param create allow to create the device if set.
345 * \return a file descriptor on success, or a negative value on error.
348 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
349 * name from \p minor and opens it.
351 static int drmOpenMinor(int minor, int create)
357 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
359 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
360 if ((fd = open(buf, O_RDWR, 0)) >= 0)
367 * Determine whether the DRM kernel driver has been loaded.
369 * \return 1 if the DRM driver is loaded, 0 otherwise.
372 * Determine the presence of the kernel driver by attempting to open the 0
373 * minor and get version information. For backward compatibility with older
374 * Linux implementations, /proc/dri is also checked.
376 int drmAvailable(void)
378 drmVersionPtr version;
382 if ((fd = drmOpenMinor(0, 1)) < 0) {
384 /* Try proc for backward Linux compatibility */
385 if (!access("/proc/dri/0", R_OK))
391 if ((version = drmGetVersion(fd))) {
393 drmFreeVersion(version);
402 * Open the device by bus ID.
404 * \param busid bus ID.
406 * \return a file descriptor on success, or a negative value on error.
409 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
410 * comparing the device bus ID with the one supplied.
412 * \sa drmOpenMinor() and drmGetBusid().
414 static int drmOpenByBusid(const char *busid)
421 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
422 for (i = 0; i < DRM_MAX_MINOR; i++) {
423 fd = drmOpenMinor(i, 1);
424 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
428 sv.drm_dd_major = -1; /* Don't care */
429 sv.drm_dd_minor = -1; /* Don't care */
430 drmSetInterfaceVersion(fd, &sv);
431 buf = drmGetBusid(fd);
432 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
433 if (buf && drmMatchBusID(buf, busid)) {
447 * Open the device by name.
449 * \param name driver name.
451 * \return a file descriptor on success, or a negative value on error.
454 * This function opens the first minor number that matches the driver name and
455 * isn't already in use. If it's in use it then it will already have a bus ID
458 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
460 static int drmOpenByName(const char *name)
464 drmVersionPtr version;
467 if (!drmAvailable()) {
468 if (!drm_server_info) {
472 /* try to load the kernel module now */
473 if (!drm_server_info->load_module(name)) {
474 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
481 * Open the first minor number that matches the driver name and isn't
482 * already in use. If it's in use it will have a busid assigned already.
484 for (i = 0; i < DRM_MAX_MINOR; i++) {
485 if ((fd = drmOpenMinor(i, 1)) >= 0) {
486 if ((version = drmGetVersion(fd))) {
487 if (!strcmp(version->name, name)) {
488 drmFreeVersion(version);
489 id = drmGetBusid(fd);
490 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
499 drmFreeVersion(version);
507 /* Backward-compatibility /proc support */
508 for (i = 0; i < 8; i++) {
509 char proc_name[64], buf[512];
510 char *driver, *pt, *devstring;
513 sprintf(proc_name, "/proc/dri/%d/name", i);
514 if ((fd = open(proc_name, 0, 0)) >= 0) {
515 retcode = read(fd, buf, sizeof(buf)-1);
518 buf[retcode-1] = '\0';
519 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
521 if (*pt) { /* Device is next */
523 if (!strcmp(driver, name)) { /* Match */
524 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
526 if (*pt) { /* Found busid */
527 return drmOpenByBusid(++pt);
528 } else { /* No busid */
529 return drmOpenDevice(strtol(devstring, NULL, 0),i);
543 * Open the DRM device.
545 * Looks up the specified name and bus ID, and opens the device found. The
546 * entry in /dev/dri is created if necessary and if called by root.
548 * \param name driver name. Not referenced if bus ID is supplied.
549 * \param busid bus ID. Zero if not known.
551 * \return a file descriptor on success, or a negative value on error.
554 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
557 int drmOpen(const char *name, const char *busid)
559 if (!drmAvailable() && name != NULL && drm_server_info) {
560 /* try to load the kernel */
561 if (!drm_server_info->load_module(name)) {
562 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
568 int fd = drmOpenByBusid(busid);
574 return drmOpenByName(name);
581 * Free the version information returned by drmGetVersion().
583 * \param v pointer to the version information.
586 * It frees the memory pointed by \p %v as well as all the non-null strings
589 void drmFreeVersion(drmVersionPtr v)
601 * Free the non-public version information returned by the kernel.
603 * \param v pointer to the version information.
606 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
607 * the non-null strings pointers in it.
609 static void drmFreeKernelVersion(drm_version_t *v)
621 * Copy version information.
623 * \param d destination pointer.
624 * \param s source pointer.
627 * Used by drmGetVersion() to translate the information returned by the ioctl
628 * interface in a private structure into the public structure counterpart.
630 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
632 d->version_major = s->version_major;
633 d->version_minor = s->version_minor;
634 d->version_patchlevel = s->version_patchlevel;
635 d->name_len = s->name_len;
636 d->name = drmStrdup(s->name);
637 d->date_len = s->date_len;
638 d->date = drmStrdup(s->date);
639 d->desc_len = s->desc_len;
640 d->desc = drmStrdup(s->desc);
645 * Query the driver version information.
647 * \param fd file descriptor.
649 * \return pointer to a drmVersion structure which should be freed with
652 * \note Similar information is available via /proc/dri.
655 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
656 * first with zeros to get the string lengths, and then the actually strings.
657 * It also null-terminates them since they might not be already.
659 drmVersionPtr drmGetVersion(int fd)
661 drmVersionPtr retval;
662 drm_version_t *version = drmMalloc(sizeof(*version));
664 version->name_len = 0;
665 version->name = NULL;
666 version->date_len = 0;
667 version->date = NULL;
668 version->desc_len = 0;
669 version->desc = NULL;
671 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
672 drmFreeKernelVersion(version);
676 if (version->name_len)
677 version->name = drmMalloc(version->name_len + 1);
678 if (version->date_len)
679 version->date = drmMalloc(version->date_len + 1);
680 if (version->desc_len)
681 version->desc = drmMalloc(version->desc_len + 1);
683 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
684 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
685 drmFreeKernelVersion(version);
689 /* The results might not be null-terminated strings, so terminate them. */
690 if (version->name_len) version->name[version->name_len] = '\0';
691 if (version->date_len) version->date[version->date_len] = '\0';
692 if (version->desc_len) version->desc[version->desc_len] = '\0';
694 retval = drmMalloc(sizeof(*retval));
695 drmCopyVersion(retval, version);
696 drmFreeKernelVersion(version);
702 * Get version information for the DRM user space library.
704 * This version number is driver independent.
706 * \param fd file descriptor.
708 * \return version information.
711 * This function allocates and fills a drm_version structure with a hard coded
714 drmVersionPtr drmGetLibVersion(int fd)
716 drm_version_t *version = drmMalloc(sizeof(*version));
719 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
720 * revision 1.0.x = original DRM interface with no drmGetLibVersion
721 * entry point and many drm<Device> extensions
722 * revision 1.1.x = added drmCommand entry points for device extensions
723 * added drmGetLibVersion to identify libdrm.a version
724 * revision 1.2.x = added drmSetInterfaceVersion
725 * modified drmOpen to handle both busid and name
726 * revision 1.3.x = added server + memory manager
728 version->version_major = 1;
729 version->version_minor = 3;
730 version->version_patchlevel = 0;
732 return (drmVersionPtr)version;
737 * Free the bus ID information.
739 * \param busid bus ID information string as given by drmGetBusid().
742 * This function is just frees the memory pointed by \p busid.
744 void drmFreeBusid(const char *busid)
746 drmFree((void *)busid);
751 * Get the bus ID of the device.
753 * \param fd file descriptor.
755 * \return bus ID string.
758 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
759 * get the string length and data, passing the arguments in a drm_unique
762 char *drmGetBusid(int fd)
769 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
771 u.unique = drmMalloc(u.unique_len + 1);
772 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
774 u.unique[u.unique_len] = '\0';
781 * Set the bus ID of the device.
783 * \param fd file descriptor.
784 * \param busid bus ID string.
786 * \return zero on success, negative on failure.
789 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
790 * the arguments in a drm_unique structure.
792 int drmSetBusid(int fd, const char *busid)
796 u.unique = (char *)busid;
797 u.unique_len = strlen(busid);
799 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
805 int drmGetMagic(int fd, drm_magic_t * magic)
810 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
816 int drmAuthMagic(int fd, drm_magic_t magic)
821 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
827 * Specifies a range of memory that is available for mapping by a
830 * \param fd file descriptor.
831 * \param offset usually the physical address. The actual meaning depends of
832 * the \p type parameter. See below.
833 * \param size of the memory in bytes.
834 * \param type type of the memory to be mapped.
835 * \param flags combination of several flags to modify the function actions.
836 * \param handle will be set to a value that may be used as the offset
837 * parameter for mmap().
839 * \return zero on success or a negative value on error.
841 * \par Mapping the frame buffer
842 * For the frame buffer
843 * - \p offset will be the physical address of the start of the frame buffer,
844 * - \p size will be the size of the frame buffer in bytes, and
845 * - \p type will be DRM_FRAME_BUFFER.
848 * The area mapped will be uncached. If MTRR support is available in the
849 * kernel, the frame buffer area will be set to write combining.
851 * \par Mapping the MMIO register area
852 * For the MMIO register area,
853 * - \p offset will be the physical address of the start of the register area,
854 * - \p size will be the size of the register area bytes, and
855 * - \p type will be DRM_REGISTERS.
857 * The area mapped will be uncached.
859 * \par Mapping the SAREA
861 * - \p offset will be ignored and should be set to zero,
862 * - \p size will be the desired size of the SAREA in bytes,
863 * - \p type will be DRM_SHM.
866 * A shared memory area of the requested size will be created and locked in
867 * kernel memory. This area may be mapped into client-space by using the handle
870 * \note May only be called by root.
873 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
874 * the arguments in a drm_map structure.
876 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
877 drmMapFlags flags, drm_handle_t *handle)
886 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map))
889 *handle = (drm_handle_t)map.handle;
893 int drmRmMap(int fd, drm_handle_t handle)
897 map.handle = (void *)handle;
899 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map))
905 * Make buffers available for DMA transfers.
907 * \param fd file descriptor.
908 * \param count number of buffers.
909 * \param size size of each buffer.
910 * \param flags buffer allocation flags.
911 * \param agp_offset offset in the AGP aperture
913 * \return number of buffers allocated, negative on error.
916 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
920 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
923 drm_buf_desc_t request;
925 request.count = count;
927 request.low_mark = 0;
928 request.high_mark = 0;
929 request.flags = flags;
930 request.agp_start = agp_offset;
932 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request))
934 return request.count;
937 int drmMarkBufs(int fd, double low, double high)
945 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
951 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
954 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
960 for (i = 0; i < info.count; i++) {
961 info.list[i].low_mark = low * info.list[i].count;
962 info.list[i].high_mark = high * info.list[i].count;
963 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
977 * \param fd file descriptor.
978 * \param count number of buffers to free.
979 * \param list list of buffers to be freed.
981 * \return zero on success, or a negative value on failure.
983 * \note This function is primarily used for debugging.
986 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
987 * the arguments in a drm_buf_free structure.
989 int drmFreeBufs(int fd, int count, int *list)
991 drm_buf_free_t request;
993 request.count = count;
995 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1004 * \param fd file descriptor.
1007 * This function closes the file descriptor.
1009 int drmClose(int fd)
1011 unsigned long key = drmGetKeyFromFd(fd);
1012 drmHashEntry *entry = drmGetEntry(fd);
1014 drmHashDestroy(entry->tagTable);
1017 entry->tagTable = NULL;
1019 drmHashDelete(drmHashTable, key);
1027 * Map a region of memory.
1029 * \param fd file descriptor.
1030 * \param handle handle returned by drmAddMap().
1031 * \param size size in bytes. Must match the size used by drmAddMap().
1032 * \param address will contain the user-space virtual address where the mapping
1035 * \return zero on success, or a negative value on failure.
1038 * This function is a wrapper for mmap().
1040 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1042 static unsigned long pagesize_mask = 0;
1048 pagesize_mask = getpagesize() - 1;
1050 size = (size + pagesize_mask) & ~pagesize_mask;
1052 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1053 if (*address == MAP_FAILED)
1060 * Unmap mappings obtained with drmMap().
1062 * \param address address as given by drmMap().
1063 * \param size size in bytes. Must match the size used by drmMap().
1065 * \return zero on success, or a negative value on failure.
1068 * This function is a wrapper for munmap().
1070 int drmUnmap(drmAddress address, drmSize size)
1072 return munmap(address, size);
1075 drmBufInfoPtr drmGetBufInfo(int fd)
1077 drm_buf_info_t info;
1078 drmBufInfoPtr retval;
1084 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1088 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1091 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1096 retval = drmMalloc(sizeof(*retval));
1097 retval->count = info.count;
1098 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1099 for (i = 0; i < info.count; i++) {
1100 retval->list[i].count = info.list[i].count;
1101 retval->list[i].size = info.list[i].size;
1102 retval->list[i].low_mark = info.list[i].low_mark;
1103 retval->list[i].high_mark = info.list[i].high_mark;
1112 * Map all DMA buffers into client-virtual space.
1114 * \param fd file descriptor.
1116 * \return a pointer to a ::drmBufMap structure.
1118 * \note The client may not use these buffers until obtaining buffer indices
1122 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1123 * information about the buffers in a drm_buf_map structure into the
1124 * client-visible data structures.
1126 drmBufMapPtr drmMapBufs(int fd)
1129 drmBufMapPtr retval;
1134 bufs.virtual = NULL;
1135 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1141 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1144 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1149 retval = drmMalloc(sizeof(*retval));
1150 retval->count = bufs.count;
1151 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1152 for (i = 0; i < bufs.count; i++) {
1153 retval->list[i].idx = bufs.list[i].idx;
1154 retval->list[i].total = bufs.list[i].total;
1155 retval->list[i].used = 0;
1156 retval->list[i].address = bufs.list[i].address;
1166 * Unmap buffers allocated with drmMapBufs().
1168 * \return zero on success, or negative value on failure.
1171 * Calls munmap() for every buffer stored in \p bufs and frees the
1172 * memory allocated by drmMapBufs().
1174 int drmUnmapBufs(drmBufMapPtr bufs)
1178 for (i = 0; i < bufs->count; i++) {
1179 munmap(bufs->list[i].address, bufs->list[i].total);
1182 drmFree(bufs->list);
1189 #define DRM_DMA_RETRY 16
1192 * Reserve DMA buffers.
1194 * \param fd file descriptor.
1197 * \return zero on success, or a negative value on failure.
1200 * Assemble the arguments into a drm_dma structure and keeps issuing the
1201 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1203 int drmDMA(int fd, drmDMAReqPtr request)
1208 dma.context = request->context;
1209 dma.send_count = request->send_count;
1210 dma.send_indices = request->send_list;
1211 dma.send_sizes = request->send_sizes;
1212 dma.flags = request->flags;
1213 dma.request_count = request->request_count;
1214 dma.request_size = request->request_size;
1215 dma.request_indices = request->request_list;
1216 dma.request_sizes = request->request_sizes;
1217 dma.granted_count = 0;
1220 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1221 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1224 request->granted_count = dma.granted_count;
1233 * Obtain heavyweight hardware lock.
1235 * \param fd file descriptor.
1236 * \param context context.
1237 * \param flags flags that determine the sate of the hardware when the function
1240 * \return always zero.
1243 * This function translates the arguments into a drm_lock structure and issue
1244 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1246 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1250 lock.context = context;
1252 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1253 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1254 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1255 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1256 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1257 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1259 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1265 * Release the hardware lock.
1267 * \param fd file descriptor.
1268 * \param context context.
1270 * \return zero on success, or a negative value on failure.
1273 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1274 * argument in a drm_lock structure.
1276 int drmUnlock(int fd, drm_context_t context)
1280 lock.context = context;
1282 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1285 drm_context_t *drmGetReservedContextList(int fd, int *count)
1289 drm_context_t * retval;
1293 res.contexts = NULL;
1294 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1300 if (!(list = drmMalloc(res.count * sizeof(*list))))
1302 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1307 res.contexts = list;
1308 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1311 for (i = 0; i < res.count; i++)
1312 retval[i] = list[i].handle;
1319 void drmFreeReservedContextList(drm_context_t *pt)
1327 * Used by the X server during GLXContext initialization. This causes
1328 * per-context kernel-level resources to be allocated.
1330 * \param fd file descriptor.
1331 * \param handle is set on success. To be used by the client when requesting DMA
1332 * dispatch with drmDMA().
1334 * \return zero on success, or a negative value on failure.
1336 * \note May only be called by root.
1339 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1340 * argument in a drm_ctx structure.
1342 int drmCreateContext(int fd, drm_context_t *handle)
1346 ctx.flags = 0; /* Modified with functions below */
1347 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1349 *handle = ctx.handle;
1353 int drmSwitchToContext(int fd, drm_context_t context)
1357 ctx.handle = context;
1358 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1363 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1368 * Context preserving means that no context switches are done between DMA
1369 * buffers from one context and the next. This is suitable for use in the
1370 * X server (which promises to maintain hardware context), or in the
1371 * client-side library when buffers are swapped on behalf of two threads.
1373 ctx.handle = context;
1375 if (flags & DRM_CONTEXT_PRESERVED)
1376 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1377 if (flags & DRM_CONTEXT_2DONLY)
1378 ctx.flags |= _DRM_CONTEXT_2DONLY;
1379 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1384 int drmGetContextFlags(int fd, drm_context_t context,
1385 drm_context_tFlagsPtr flags)
1389 ctx.handle = context;
1390 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1393 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1394 *flags |= DRM_CONTEXT_PRESERVED;
1395 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1396 *flags |= DRM_CONTEXT_2DONLY;
1403 * Free any kernel-level resources allocated with drmCreateContext() associated
1406 * \param fd file descriptor.
1407 * \param handle handle given by drmCreateContext().
1409 * \return zero on success, or a negative value on failure.
1411 * \note May only be called by root.
1414 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1415 * argument in a drm_ctx structure.
1417 int drmDestroyContext(int fd, drm_context_t handle)
1420 ctx.handle = handle;
1421 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1426 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1429 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1431 *handle = draw.handle;
1435 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1438 draw.handle = handle;
1439 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1444 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1445 drm_drawable_info_type_t type, unsigned int num,
1448 drm_update_draw_t update;
1450 update.handle = handle;
1453 update.data = (unsigned long long)(unsigned long)data;
1455 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1462 * Acquire the AGP device.
1464 * Must be called before any of the other AGP related calls.
1466 * \param fd file descriptor.
1468 * \return zero on success, or a negative value on failure.
1471 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1473 int drmAgpAcquire(int fd)
1475 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1482 * Release the AGP device.
1484 * \param fd file descriptor.
1486 * \return zero on success, or a negative value on failure.
1489 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1491 int drmAgpRelease(int fd)
1493 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1502 * \param fd file descriptor.
1503 * \param mode AGP mode.
1505 * \return zero on success, or a negative value on failure.
1508 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1509 * argument in a drm_agp_mode structure.
1511 int drmAgpEnable(int fd, unsigned long mode)
1516 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1523 * Allocate a chunk of AGP memory.
1525 * \param fd file descriptor.
1526 * \param size requested memory size in bytes. Will be rounded to page boundary.
1527 * \param type type of memory to allocate.
1528 * \param address if not zero, will be set to the physical address of the
1530 * \param handle on success will be set to a handle of the allocated memory.
1532 * \return zero on success, or a negative value on failure.
1535 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1536 * arguments in a drm_agp_buffer structure.
1538 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1539 unsigned long *address, drm_handle_t *handle)
1543 *handle = DRM_AGP_NO_HANDLE;
1547 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1550 *address = b.physical;
1557 * Free a chunk of AGP memory.
1559 * \param fd file descriptor.
1560 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1562 * \return zero on success, or a negative value on failure.
1565 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1566 * argument in a drm_agp_buffer structure.
1568 int drmAgpFree(int fd, drm_handle_t handle)
1574 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b))
1581 * Bind a chunk of AGP memory.
1583 * \param fd file descriptor.
1584 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1585 * \param offset offset in bytes. It will round to page boundary.
1587 * \return zero on success, or a negative value on failure.
1590 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1591 * argument in a drm_agp_binding structure.
1593 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1595 drm_agp_binding_t b;
1599 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b))
1606 * Unbind a chunk of AGP memory.
1608 * \param fd file descriptor.
1609 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1611 * \return zero on success, or a negative value on failure.
1614 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1615 * the argument in a drm_agp_binding structure.
1617 int drmAgpUnbind(int fd, drm_handle_t handle)
1619 drm_agp_binding_t b;
1623 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1630 * Get AGP driver major version number.
1632 * \param fd file descriptor.
1634 * \return major version number on success, or a negative value on failure..
1637 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1638 * necessary information in a drm_agp_info structure.
1640 int drmAgpVersionMajor(int fd)
1644 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1646 return i.agp_version_major;
1651 * Get AGP driver minor version number.
1653 * \param fd file descriptor.
1655 * \return minor version number on success, or a negative value on failure.
1658 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1659 * necessary information in a drm_agp_info structure.
1661 int drmAgpVersionMinor(int fd)
1665 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1667 return i.agp_version_minor;
1674 * \param fd file descriptor.
1676 * \return mode on success, or zero on failure.
1679 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1680 * necessary information in a drm_agp_info structure.
1682 unsigned long drmAgpGetMode(int fd)
1686 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1693 * Get AGP aperture base.
1695 * \param fd file descriptor.
1697 * \return aperture base on success, zero on failure.
1700 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1701 * necessary information in a drm_agp_info structure.
1703 unsigned long drmAgpBase(int fd)
1707 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1709 return i.aperture_base;
1714 * Get AGP aperture size.
1716 * \param fd file descriptor.
1718 * \return aperture size on success, zero on failure.
1721 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1722 * necessary information in a drm_agp_info structure.
1724 unsigned long drmAgpSize(int fd)
1728 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1730 return i.aperture_size;
1735 * Get used AGP memory.
1737 * \param fd file descriptor.
1739 * \return memory used on success, or zero on failure.
1742 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1743 * necessary information in a drm_agp_info structure.
1745 unsigned long drmAgpMemoryUsed(int fd)
1749 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1751 return i.memory_used;
1756 * Get available AGP memory.
1758 * \param fd file descriptor.
1760 * \return memory available on success, or zero on failure.
1763 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1764 * necessary information in a drm_agp_info structure.
1766 unsigned long drmAgpMemoryAvail(int fd)
1770 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1772 return i.memory_allowed;
1777 * Get hardware vendor ID.
1779 * \param fd file descriptor.
1781 * \return vendor ID on success, or zero on failure.
1784 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1785 * necessary information in a drm_agp_info structure.
1787 unsigned int drmAgpVendorId(int fd)
1791 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1798 * Get hardware device ID.
1800 * \param fd file descriptor.
1802 * \return zero on success, or zero on failure.
1805 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1806 * necessary information in a drm_agp_info structure.
1808 unsigned int drmAgpDeviceId(int fd)
1812 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1817 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1819 drm_scatter_gather_t sg;
1824 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1826 *handle = sg.handle;
1830 int drmScatterGatherFree(int fd, drm_handle_t handle)
1832 drm_scatter_gather_t sg;
1836 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg))
1844 * \param fd file descriptor.
1845 * \param vbl pointer to a drmVBlank structure.
1847 * \return zero on success, or a negative value on failure.
1850 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1852 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1857 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1858 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1859 } while (ret && errno == EINTR);
1864 int drmError(int err, const char *label)
1867 case DRM_ERR_NO_DEVICE:
1868 fprintf(stderr, "%s: no device\n", label);
1870 case DRM_ERR_NO_ACCESS:
1871 fprintf(stderr, "%s: no access\n", label);
1873 case DRM_ERR_NOT_ROOT:
1874 fprintf(stderr, "%s: not root\n", label);
1876 case DRM_ERR_INVALID:
1877 fprintf(stderr, "%s: invalid args\n", label);
1882 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1890 * Install IRQ handler.
1892 * \param fd file descriptor.
1893 * \param irq IRQ number.
1895 * \return zero on success, or a negative value on failure.
1898 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1899 * argument in a drm_control structure.
1901 int drmCtlInstHandler(int fd, int irq)
1905 ctl.func = DRM_INST_HANDLER;
1907 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1914 * Uninstall IRQ handler.
1916 * \param fd file descriptor.
1918 * \return zero on success, or a negative value on failure.
1921 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1922 * argument in a drm_control structure.
1924 int drmCtlUninstHandler(int fd)
1928 ctl.func = DRM_UNINST_HANDLER;
1930 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1935 int drmFinish(int fd, int context, drmLockFlags flags)
1939 lock.context = context;
1941 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1942 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1943 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1944 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1945 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1946 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1947 if (ioctl(fd, DRM_IOCTL_FINISH, &lock))
1953 * Get IRQ from bus ID.
1955 * \param fd file descriptor.
1956 * \param busnum bus number.
1957 * \param devnum device number.
1958 * \param funcnum function number.
1960 * \return IRQ number on success, or a negative value on failure.
1963 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1964 * arguments in a drm_irq_busid structure.
1966 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1972 p.funcnum = funcnum;
1973 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1978 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1980 drmHashEntry *entry = drmGetEntry(fd);
1982 if (drmHashInsert(entry->tagTable, context, tag)) {
1983 drmHashDelete(entry->tagTable, context);
1984 drmHashInsert(entry->tagTable, context, tag);
1989 int drmDelContextTag(int fd, drm_context_t context)
1991 drmHashEntry *entry = drmGetEntry(fd);
1993 return drmHashDelete(entry->tagTable, context);
1996 void *drmGetContextTag(int fd, drm_context_t context)
1998 drmHashEntry *entry = drmGetEntry(fd);
2001 if (drmHashLookup(entry->tagTable, context, &value))
2007 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2008 drm_handle_t handle)
2010 drm_ctx_priv_map_t map;
2012 map.ctx_id = ctx_id;
2013 map.handle = (void *)handle;
2015 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2020 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2021 drm_handle_t *handle)
2023 drm_ctx_priv_map_t map;
2025 map.ctx_id = ctx_id;
2027 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2030 *handle = (drm_handle_t)map.handle;
2035 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2036 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2042 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map))
2044 *offset = map.offset;
2048 *handle = (unsigned long)map.handle;
2053 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2054 unsigned long *magic, unsigned long *iocs)
2056 drm_client_t client;
2059 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2061 *auth = client.auth;
2064 *magic = client.magic;
2065 *iocs = client.iocs;
2069 int drmGetStats(int fd, drmStatsT *stats)
2074 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s))
2078 memset(stats, 0, sizeof(*stats));
2079 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2083 stats->data[i].long_format = "%-20.20s"; \
2084 stats->data[i].rate_format = "%8.8s"; \
2085 stats->data[i].isvalue = 1; \
2086 stats->data[i].verbose = 0
2089 stats->data[i].long_format = "%-20.20s"; \
2090 stats->data[i].rate_format = "%5.5s"; \
2091 stats->data[i].isvalue = 0; \
2092 stats->data[i].mult_names = "kgm"; \
2093 stats->data[i].mult = 1000; \
2094 stats->data[i].verbose = 0
2097 stats->data[i].long_format = "%-20.20s"; \
2098 stats->data[i].rate_format = "%5.5s"; \
2099 stats->data[i].isvalue = 0; \
2100 stats->data[i].mult_names = "KGM"; \
2101 stats->data[i].mult = 1024; \
2102 stats->data[i].verbose = 0
2105 stats->count = s.count;
2106 for (i = 0; i < s.count; i++) {
2107 stats->data[i].value = s.data[i].value;
2108 switch (s.data[i].type) {
2109 case _DRM_STAT_LOCK:
2110 stats->data[i].long_name = "Lock";
2111 stats->data[i].rate_name = "Lock";
2114 case _DRM_STAT_OPENS:
2115 stats->data[i].long_name = "Opens";
2116 stats->data[i].rate_name = "O";
2118 stats->data[i].verbose = 1;
2120 case _DRM_STAT_CLOSES:
2121 stats->data[i].long_name = "Closes";
2122 stats->data[i].rate_name = "Lock";
2124 stats->data[i].verbose = 1;
2126 case _DRM_STAT_IOCTLS:
2127 stats->data[i].long_name = "Ioctls";
2128 stats->data[i].rate_name = "Ioc/s";
2131 case _DRM_STAT_LOCKS:
2132 stats->data[i].long_name = "Locks";
2133 stats->data[i].rate_name = "Lck/s";
2136 case _DRM_STAT_UNLOCKS:
2137 stats->data[i].long_name = "Unlocks";
2138 stats->data[i].rate_name = "Unl/s";
2142 stats->data[i].long_name = "IRQs";
2143 stats->data[i].rate_name = "IRQ/s";
2146 case _DRM_STAT_PRIMARY:
2147 stats->data[i].long_name = "Primary Bytes";
2148 stats->data[i].rate_name = "PB/s";
2151 case _DRM_STAT_SECONDARY:
2152 stats->data[i].long_name = "Secondary Bytes";
2153 stats->data[i].rate_name = "SB/s";
2157 stats->data[i].long_name = "DMA";
2158 stats->data[i].rate_name = "DMA/s";
2161 case _DRM_STAT_SPECIAL:
2162 stats->data[i].long_name = "Special DMA";
2163 stats->data[i].rate_name = "dma/s";
2166 case _DRM_STAT_MISSED:
2167 stats->data[i].long_name = "Miss";
2168 stats->data[i].rate_name = "Ms/s";
2171 case _DRM_STAT_VALUE:
2172 stats->data[i].long_name = "Value";
2173 stats->data[i].rate_name = "Value";
2176 case _DRM_STAT_BYTE:
2177 stats->data[i].long_name = "Bytes";
2178 stats->data[i].rate_name = "B/s";
2181 case _DRM_STAT_COUNT:
2183 stats->data[i].long_name = "Count";
2184 stats->data[i].rate_name = "Cnt/s";
2193 * Issue a set-version ioctl.
2195 * \param fd file descriptor.
2196 * \param drmCommandIndex command index
2197 * \param data source pointer of the data to be read and written.
2198 * \param size size of the data to be read and written.
2200 * \return zero on success, or a negative value on failure.
2203 * It issues a read-write ioctl given by
2204 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2206 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2209 drm_set_version_t sv;
2211 sv.drm_di_major = version->drm_di_major;
2212 sv.drm_di_minor = version->drm_di_minor;
2213 sv.drm_dd_major = version->drm_dd_major;
2214 sv.drm_dd_minor = version->drm_dd_minor;
2216 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2220 version->drm_di_major = sv.drm_di_major;
2221 version->drm_di_minor = sv.drm_di_minor;
2222 version->drm_dd_major = sv.drm_dd_major;
2223 version->drm_dd_minor = sv.drm_dd_minor;
2229 * Send a device-specific command.
2231 * \param fd file descriptor.
2232 * \param drmCommandIndex command index
2234 * \return zero on success, or a negative value on failure.
2237 * It issues a ioctl given by
2238 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2240 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2242 void *data = NULL; /* dummy */
2243 unsigned long request;
2245 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2247 if (ioctl(fd, request, data)) {
2255 * Send a device-specific read command.
2257 * \param fd file descriptor.
2258 * \param drmCommandIndex command index
2259 * \param data destination pointer of the data to be read.
2260 * \param size size of the data to be read.
2262 * \return zero on success, or a negative value on failure.
2265 * It issues a read ioctl given by
2266 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2268 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2271 unsigned long request;
2273 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2274 DRM_COMMAND_BASE + drmCommandIndex, size);
2276 if (ioctl(fd, request, data)) {
2284 * Send a device-specific write command.
2286 * \param fd file descriptor.
2287 * \param drmCommandIndex command index
2288 * \param data source pointer of the data to be written.
2289 * \param size size of the data to be written.
2291 * \return zero on success, or a negative value on failure.
2294 * It issues a write ioctl given by
2295 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2297 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2300 unsigned long request;
2302 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2303 DRM_COMMAND_BASE + drmCommandIndex, size);
2305 if (ioctl(fd, request, data)) {
2313 * Send a device-specific read-write command.
2315 * \param fd file descriptor.
2316 * \param drmCommandIndex command index
2317 * \param data source pointer of the data to be read and written.
2318 * \param size size of the data to be read and written.
2320 * \return zero on success, or a negative value on failure.
2323 * It issues a read-write ioctl given by
2324 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2326 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2329 unsigned long request;
2331 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2332 DRM_COMMAND_BASE + drmCommandIndex, size);
2334 if (ioctl(fd, request, data)) {
2343 * DRM_FENCE_FLAG_EMIT
2344 * DRM_FENCE_FLAG_SHAREABLE
2345 * DRM_FENCE_MASK_DRIVER
2348 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2351 drm_fence_arg_t arg;
2353 memset(&arg, 0, sizeof(arg));
2356 arg.fence_class = fence_class;
2358 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2360 fence->handle = arg.handle;
2361 fence->fence_class = arg.fence_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, uint32_t fence_class, drmFence *fence)
2376 drm_fence_arg_t arg;
2378 memset(&arg, 0, sizeof(arg));
2380 arg.fence_class = fence_class;
2382 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2384 fence->handle = arg.handle;
2385 fence->fence_class = arg.fence_class;
2386 fence->type = arg.type;
2387 fence->flags = arg.flags;
2388 fence->sequence = arg.sequence;
2389 fence->signaled = 0;
2393 int drmFenceDestroy(int fd, const drmFence *fence)
2395 drm_fence_arg_t arg;
2397 memset(&arg, 0, sizeof(arg));
2398 arg.handle = fence->handle;
2400 if (ioctl(fd, DRM_IOCTL_FENCE_DESTROY, &arg))
2405 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2407 drm_fence_arg_t arg;
2409 memset(&arg, 0, sizeof(arg));
2410 arg.handle = handle;
2412 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2414 fence->handle = arg.handle;
2415 fence->fence_class = arg.fence_class;
2416 fence->type = arg.type;
2417 fence->flags = arg.flags;
2418 fence->signaled = arg.signaled;
2422 int drmFenceUnreference(int fd, const drmFence *fence)
2424 drm_fence_arg_t arg;
2426 memset(&arg, 0, sizeof(arg));
2427 arg.handle = fence->handle;
2429 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2434 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2436 drm_fence_arg_t arg;
2438 memset(&arg, 0, sizeof(arg));
2439 arg.handle = fence->handle;
2440 arg.type = flush_type;
2442 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2444 fence->fence_class = arg.fence_class;
2445 fence->type = arg.type;
2446 fence->signaled = arg.signaled;
2450 int drmFenceUpdate(int fd, drmFence *fence)
2452 drm_fence_arg_t arg;
2454 memset(&arg, 0, sizeof(arg));
2455 arg.handle = fence->handle;
2457 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2459 fence->fence_class = arg.fence_class;
2460 fence->type = arg.type;
2461 fence->signaled = arg.signaled;
2465 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2468 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2469 ((fenceType & fence->signaled) != fenceType)) {
2470 int ret = drmFenceFlush(fd, fence, fenceType);
2475 *signaled = ((fenceType & fence->signaled) == fenceType);
2482 * DRM_FENCE_FLAG_SHAREABLE
2483 * DRM_FENCE_MASK_DRIVER
2487 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2489 drm_fence_arg_t arg;
2491 memset(&arg, 0, sizeof(arg));
2492 arg.fence_class = fence->fence_class;
2494 arg.handle = fence->handle;
2495 arg.type = emit_type;
2497 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2499 fence->fence_class = arg.fence_class;
2500 fence->type = arg.type;
2501 fence->signaled = arg.signaled;
2507 * DRM_FENCE_FLAG_WAIT_LAZY
2508 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2511 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2513 drm_fence_arg_t arg;
2516 if (flush_type == 0) {
2517 flush_type = fence->type;
2520 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2521 if ((flush_type & fence->signaled) == flush_type) {
2526 memset(&arg, 0, sizeof(arg));
2527 arg.handle = fence->handle;
2528 arg.type = flush_type;
2532 ret = ioctl(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2533 } while (ret != 0 && errno == EAGAIN);
2538 fence->fence_class = arg.fence_class;
2539 fence->type = arg.type;
2540 fence->signaled = arg.signaled;
2544 static int drmAdjustListNodes(drmBOList *list)
2550 while(list->numCurrent < list->numTarget) {
2551 node = (drmBONode *) malloc(sizeof(*node));
2557 DRMLISTADD(&node->head, &list->free);
2560 while(list->numCurrent > list->numTarget) {
2561 l = list->free.next;
2562 if (l == &list->free)
2565 node = DRMLISTENTRY(drmBONode, l, head);
2572 void drmBOFreeList(drmBOList *list)
2577 l = list->list.next;
2578 while(l != &list->list) {
2580 node = DRMLISTENTRY(drmBONode, l, head);
2582 l = list->list.next;
2587 l = list->free.next;
2588 while(l != &list->free) {
2590 node = DRMLISTENTRY(drmBONode, l, head);
2592 l = list->free.next;
2597 int drmBOResetList(drmBOList *list)
2602 ret = drmAdjustListNodes(list);
2606 l = list->list.next;
2607 while (l != &list->list) {
2609 DRMLISTADD(l, &list->free);
2611 l = list->list.next;
2613 return drmAdjustListNodes(list);
2616 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2623 l = list->free.next;
2624 if (l == &list->free) {
2625 node = (drmBONode *) malloc(sizeof(*node));
2633 node = DRMLISTENTRY(drmBONode, l, head);
2638 DRMLISTADD(&node->head, &list->list);
2643 void *drmBOListIterator(drmBOList *list)
2645 void *ret = list->list.next;
2647 if (ret == &list->list)
2652 void *drmBOListNext(drmBOList *list, void *iterator)
2656 drmMMListHead *l = (drmMMListHead *) iterator;
2658 if (ret == &list->list)
2663 drmBO *drmBOListBuf(void *iterator)
2666 drmMMListHead *l = (drmMMListHead *) iterator;
2667 node = DRMLISTENTRY(drmBONode, l, head);
2672 int drmBOCreateList(int numTarget, drmBOList *list)
2674 DRMINITLISTHEAD(&list->list);
2675 DRMINITLISTHEAD(&list->free);
2676 list->numTarget = numTarget;
2677 list->numCurrent = 0;
2678 list->numOnList = 0;
2679 return drmAdjustListNodes(list);
2682 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2684 buf->handle = rep->handle;
2685 buf->flags = rep->flags;
2686 buf->size = rep->size;
2687 buf->offset = rep->offset;
2688 buf->mapHandle = rep->arg_handle;
2689 buf->mask = rep->mask;
2690 buf->start = rep->buffer_start;
2691 buf->fenceFlags = rep->fence_flags;
2692 buf->replyFlags = rep->rep_flags;
2693 buf->pageAlignment = rep->page_alignment;
2694 buf->tileInfo = rep->tile_info;
2695 buf->hwTileStride = rep->hw_tile_stride;
2696 buf->desiredTileStride = rep->desired_tile_stride;
2701 int drmBOCreate(int fd, unsigned long size,
2702 unsigned pageAlignment, void *user_buffer,
2704 unsigned hint, drmBO *buf)
2706 struct drm_bo_create_arg arg;
2707 struct drm_bo_create_req *req = &arg.d.req;
2708 struct drm_bo_info_rep *rep = &arg.d.rep;
2711 memset(buf, 0, sizeof(*buf));
2712 memset(&arg, 0, sizeof(arg));
2716 req->page_alignment = pageAlignment;
2717 req->buffer_start = (unsigned long) user_buffer;
2719 buf->virtual = NULL;
2722 ret = ioctl(fd, DRM_IOCTL_BO_CREATE, &arg);
2723 } while (ret != 0 && errno == EAGAIN);
2728 drmBOCopyReply(rep, buf);
2729 buf->mapVirtual = NULL;
2735 int drmBODestroy(int fd, drmBO *buf)
2737 struct drm_bo_handle_arg arg;
2739 if (buf->mapVirtual) {
2740 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2741 buf->mapVirtual = NULL;
2742 buf->virtual = NULL;
2745 memset(&arg, 0, sizeof(arg));
2746 arg.handle = buf->handle;
2748 if (ioctl(fd, DRM_IOCTL_BO_DESTROY, &arg))
2755 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2757 struct drm_bo_reference_info_arg arg;
2758 struct drm_bo_handle_arg *req = &arg.d.req;
2759 struct drm_bo_info_rep *rep = &arg.d.rep;
2761 memset(&arg, 0, sizeof(arg));
2762 req->handle = handle;
2764 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2767 drmBOCopyReply(rep, buf);
2768 buf->mapVirtual = NULL;
2770 buf->virtual = NULL;
2775 int drmBOUnReference(int fd, drmBO *buf)
2777 struct drm_bo_handle_arg arg;
2779 if (buf->mapVirtual) {
2780 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2781 buf->mapVirtual = NULL;
2782 buf->virtual = NULL;
2785 memset(&arg, 0, sizeof(arg));
2786 arg.handle = buf->handle;
2788 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2797 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2798 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2799 * call return an -EBUSY if it can' immediately honor the mapping request.
2802 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2805 struct drm_bo_map_wait_idle_arg arg;
2806 struct drm_bo_info_req *req = &arg.d.req;
2807 struct drm_bo_info_rep *rep = &arg.d.rep;
2811 * Make sure we have a virtual address of the buffer.
2814 if (!buf->virtual) {
2816 virtual = mmap(0, buf->size + buf->start,
2817 PROT_READ | PROT_WRITE, MAP_SHARED,
2818 fd, buf->mapHandle);
2819 if (virtual == MAP_FAILED) {
2824 buf->mapVirtual = virtual;
2825 buf->virtual = ((char *) virtual) + buf->start;
2828 memset(&arg, 0, sizeof(arg));
2829 req->handle = buf->handle;
2830 req->mask = mapFlags;
2831 req->hint = mapHint;
2834 * May hang if the buffer object is busy.
2835 * This IOCTL synchronizes the buffer.
2839 ret = ioctl(fd, DRM_IOCTL_BO_MAP, &arg);
2840 } while (ret != 0 && errno == EAGAIN);
2845 drmBOCopyReply(rep, buf);
2846 buf->mapFlags = mapFlags;
2848 *address = buf->virtual;
2854 int drmBOUnmap(int fd, drmBO *buf)
2856 struct drm_bo_handle_arg arg;
2858 memset(&arg, 0, sizeof(arg));
2859 arg.handle = buf->handle;
2861 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2868 int drmBOValidate(int fd, drmBO *buf, uint32_t fence_class,
2869 uint64_t flags, uint64_t mask,
2872 struct drm_bo_op_arg arg;
2873 struct drm_bo_op_req *req = &arg.d.req;
2874 struct drm_bo_arg_rep *rep = &arg.d.rep;
2877 memset(&arg, 0, sizeof(arg));
2878 req->bo_req.handle = buf->handle;
2879 req->bo_req.flags = flags;
2880 req->bo_req.mask = mask;
2881 req->bo_req.hint = hint;
2882 req->bo_req.fence_class = fence_class;
2883 req->op = drm_bo_validate;
2886 ret = ioctl(fd, DRM_IOCTL_BO_OP, &arg);
2887 } while (ret && errno == EAGAIN);
2896 drmBOCopyReply(&rep->bo_info, buf);
2901 int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle)
2903 struct drm_bo_op_arg arg;
2904 struct drm_bo_op_req *req = &arg.d.req;
2905 struct drm_bo_arg_rep *rep = &arg.d.rep;
2908 memset(&arg, 0, sizeof(arg));
2909 req->bo_req.handle = buf->handle;
2910 req->bo_req.flags = flags;
2911 req->arg_handle = fenceHandle;
2912 req->op = drm_bo_fence;
2914 ret = ioctl(fd, DRM_IOCTL_BO_OP, &arg);
2924 int drmBOInfo(int fd, drmBO *buf)
2926 struct drm_bo_reference_info_arg arg;
2927 struct drm_bo_handle_arg *req = &arg.d.req;
2928 struct drm_bo_info_rep *rep = &arg.d.rep;
2931 memset(&arg, 0, sizeof(arg));
2932 req->handle = buf->handle;
2934 ret = ioctl(fd, DRM_IOCTL_BO_INFO, &arg);
2938 drmBOCopyReply(rep, buf);
2942 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2944 struct drm_bo_map_wait_idle_arg arg;
2945 struct drm_bo_info_req *req = &arg.d.req;
2946 struct drm_bo_info_rep *rep = &arg.d.rep;
2949 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2950 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2951 memset(&arg, 0, sizeof(arg));
2952 req->handle = buf->handle;
2956 ret = ioctl(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2957 } while (ret && errno == EAGAIN);
2962 drmBOCopyReply(rep, buf);
2967 int drmBOSetPin(int fd, drmBO *buf, int pin)
2969 struct drm_bo_set_pin_arg arg;
2970 struct drm_bo_set_pin_req *req = &arg.d.req;
2971 struct drm_bo_info_rep *rep = &arg.d.rep;
2974 memset(&arg, 0, sizeof(arg));
2975 req->handle = buf->handle;
2979 ret = ioctl(fd, DRM_IOCTL_BO_SET_PIN, &arg);
2980 } while (ret && errno == EAGAIN);
2985 drmBOCopyReply(rep, buf);
2990 int drmBOBusy(int fd, drmBO *buf, int *busy)
2992 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2993 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2998 int ret = drmBOInfo(fd, buf);
3001 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
3006 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
3010 drmBONode *node, *cur;
3016 for (l = list->list.next; l != &list->list; l = l->next) {
3017 node = DRMLISTENTRY(drmBONode, l, head);
3018 if (node->buf == buf) {
3024 cur = drmAddListItem(list, buf, flags, mask);
3026 drmMsg("Out of memory creating validate list node.\n");
3034 unsigned memMask = (cur->arg1 | mask) & DRM_BO_MASK_MEM;
3035 unsigned memFlags = cur->arg0 & flags & memMask;
3038 drmMsg("Incompatible memory location requests "
3039 "on validate list.\n");
3040 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3041 cur->arg0, cur->arg1);
3042 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3046 if (mask & cur->arg1 & ~DRM_BO_MASK_MEM & (cur->arg0 ^ flags)) {
3047 drmMsg("Incompatible buffer flag requests "
3048 "on validate list.\n");
3049 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3050 cur->arg0, cur->arg1);
3051 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3056 cur->arg0 = memFlags | ((cur->arg0 | flags) &
3057 cur->arg1 & ~DRM_BO_MASK_MEM);
3063 int drmBOValidateList(int fd, drmBOList *list)
3067 struct drm_bo_op_arg *arg, *first;
3068 struct drm_bo_op_req *req;
3069 struct drm_bo_arg_rep *rep;
3070 uint64_t *prevNext = NULL;
3076 for (l = list->list.next; l != &list->list; l = l->next) {
3077 node = DRMLISTENTRY(drmBONode, l, head);
3079 arg = &node->bo_arg;
3086 *prevNext = (unsigned long) arg;
3088 memset(arg, 0, sizeof(*arg));
3089 prevNext = &arg->next;
3090 req->bo_req.handle = node->buf->handle;
3091 req->op = drm_bo_validate;
3092 req->bo_req.flags = node->arg0;
3093 req->bo_req.hint = 0;
3094 req->bo_req.mask = node->arg1;
3095 req->bo_req.fence_class = 0; /* Backwards compat. */
3102 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3103 } while (ret && errno == EAGAIN);
3108 for (l = list->list.next; l != &list->list; l = l->next) {
3109 node = DRMLISTENTRY(drmBONode, l, head);
3110 arg = &node->bo_arg;
3113 if (!arg->handled) {
3114 drmMsg("Unhandled request\n");
3121 drmBOCopyReply(&rep->bo_info, buf);
3127 int drmBOFenceList(int fd, drmBOList *list, unsigned fenceHandle)
3131 struct drm_bo_op_arg *arg, *first;
3132 struct drm_bo_op_req *req;
3133 struct drm_bo_arg_rep *rep;
3134 uint64_t *prevNext = NULL;
3136 unsigned fence_flags;
3141 for (l = list->list.next; l != &list->list; l = l->next) {
3142 node = DRMLISTENTRY(drmBONode, l, head);
3144 arg = &node->bo_arg;
3151 *prevNext = (unsigned long) arg;
3153 memset(arg, 0, sizeof(*arg));
3154 prevNext = &arg->next;
3155 req->bo_req.handle = node->buf->handle;
3156 req->op = drm_bo_fence;
3157 req->bo_req.mask = node->arg0;
3158 req->arg_handle = fenceHandle;
3164 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3169 for (l = list->list.next; l != &list->list; l = l->next) {
3170 node = DRMLISTENTRY(drmBONode, l, head);
3172 arg = &node->bo_arg;
3179 drmBOCopyReply(&rep->bo_info, node->buf);
3185 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
3188 struct drm_mm_init_arg arg;
3190 memset(&arg, 0, sizeof(arg));
3192 arg.magic = DRM_BO_INIT_MAGIC;
3193 arg.major = DRM_BO_INIT_MAJOR;
3194 arg.minor = DRM_BO_INIT_MINOR;
3195 arg.p_offset = pOffset;
3197 arg.mem_type = memType;
3199 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3204 int drmMMTakedown(int fd, unsigned memType)
3206 struct drm_mm_type_arg arg;
3208 memset(&arg, 0, sizeof(arg));
3209 arg.mem_type = memType;
3211 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
3216 int drmMMLock(int fd, unsigned memType)
3218 struct drm_mm_type_arg arg;
3221 memset(&arg, 0, sizeof(arg));
3222 arg.mem_type = memType;
3225 ret = ioctl(fd, DRM_IOCTL_MM_LOCK, &arg);
3226 } while (ret && errno == EAGAIN);
3228 return (ret) ? -errno : 0;
3231 int drmMMUnlock(int fd, unsigned memType)
3233 struct drm_mm_type_arg arg;
3236 memset(&arg, 0, sizeof(arg));
3238 arg.mem_type = memType;
3241 ret = ioctl(fd, DRM_IOCTL_MM_UNLOCK, &arg);
3242 } while (ret && errno == EAGAIN);
3244 return (ret) ? -errno : 0;
3247 #define DRM_MAX_FDS 16
3252 } connection[DRM_MAX_FDS];
3254 static int nr_fds = 0;
3256 int drmOpenOnce(void *unused,
3263 for (i = 0; i < nr_fds; i++)
3264 if (strcmp(BusID, connection[i].BusID) == 0) {
3265 connection[i].refcount++;
3267 return connection[i].fd;
3270 fd = drmOpen(unused, BusID);
3271 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
3274 connection[nr_fds].BusID = strdup(BusID);
3275 connection[nr_fds].fd = fd;
3276 connection[nr_fds].refcount = 1;
3280 fprintf(stderr, "saved connection %d for %s %d\n",
3281 nr_fds, connection[nr_fds].BusID,
3282 strcmp(BusID, connection[nr_fds].BusID));
3289 void drmCloseOnce(int fd)
3293 for (i = 0; i < nr_fds; i++) {
3294 if (fd == connection[i].fd) {
3295 if (--connection[i].refcount == 0) {
3296 drmClose(connection[i].fd);
3297 free(connection[i].BusID);
3300 connection[i] = connection[nr_fds];