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 class, unsigned type,
2351 drm_fence_arg_t arg;
2353 memset(&arg, 0, sizeof(arg));
2357 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2359 fence->handle = arg.handle;
2360 fence->class = arg.class;
2361 fence->type = arg.type;
2362 fence->flags = arg.flags;
2363 fence->signaled = 0;
2369 * DRM_FENCE_FLAG_SHAREABLE
2370 * DRM_FENCE_MASK_DRIVER
2373 int drmFenceBuffers(int fd, unsigned flags, drmFence *fence)
2375 drm_fence_arg_t arg;
2377 memset(&arg, 0, sizeof(arg));
2380 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2382 fence->handle = arg.handle;
2383 fence->class = arg.class;
2384 fence->type = arg.type;
2385 fence->flags = arg.flags;
2386 fence->signaled = 0;
2390 int drmFenceDestroy(int fd, const drmFence *fence)
2392 drm_fence_arg_t arg;
2394 memset(&arg, 0, sizeof(arg));
2395 arg.handle = fence->handle;
2397 if (ioctl(fd, DRM_IOCTL_FENCE_DESTROY, &arg))
2402 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2404 drm_fence_arg_t arg;
2406 memset(&arg, 0, sizeof(arg));
2407 arg.handle = handle;
2409 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2411 fence->handle = arg.handle;
2412 fence->class = arg.class;
2413 fence->type = arg.type;
2414 fence->flags = arg.flags;
2415 fence->signaled = arg.signaled;
2419 int drmFenceUnreference(int fd, const drmFence *fence)
2421 drm_fence_arg_t arg;
2423 memset(&arg, 0, sizeof(arg));
2424 arg.handle = fence->handle;
2426 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2431 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2433 drm_fence_arg_t arg;
2435 memset(&arg, 0, sizeof(arg));
2436 arg.handle = fence->handle;
2437 arg.type = flush_type;
2439 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2441 fence->class = arg.class;
2442 fence->type = arg.type;
2443 fence->signaled = arg.signaled;
2447 int drmFenceUpdate(int fd, drmFence *fence)
2449 drm_fence_arg_t arg;
2451 memset(&arg, 0, sizeof(arg));
2452 arg.handle = fence->handle;
2454 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2456 fence->class = arg.class;
2457 fence->type = arg.type;
2458 fence->signaled = arg.signaled;
2462 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2465 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2466 ((fenceType & fence->signaled) != fenceType)) {
2467 int ret = drmFenceFlush(fd, fence, fenceType);
2472 *signaled = ((fenceType & fence->signaled) == fenceType);
2479 * DRM_FENCE_FLAG_SHAREABLE
2480 * DRM_FENCE_MASK_DRIVER
2484 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2486 drm_fence_arg_t arg;
2488 memset(&arg, 0, sizeof(arg));
2489 arg.class = fence->class;
2491 arg.handle = fence->handle;
2492 arg.type = emit_type;
2494 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2496 fence->class = arg.class;
2497 fence->type = arg.type;
2498 fence->signaled = arg.signaled;
2504 * DRM_FENCE_FLAG_WAIT_LAZY
2505 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2508 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2510 drm_fence_arg_t arg;
2513 if (flush_type == 0) {
2514 flush_type = fence->type;
2517 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2518 if ((flush_type & fence->signaled) == flush_type) {
2523 memset(&arg, 0, sizeof(arg));
2524 arg.handle = fence->handle;
2525 arg.type = flush_type;
2529 ret = ioctl(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2530 } while (ret != 0 && errno == EAGAIN);
2535 fence->class = arg.class;
2536 fence->type = arg.type;
2537 fence->signaled = arg.signaled;
2541 static int drmAdjustListNodes(drmBOList *list)
2547 while(list->numCurrent < list->numTarget) {
2548 node = (drmBONode *) malloc(sizeof(*node));
2554 DRMLISTADD(&node->head, &list->free);
2557 while(list->numCurrent > list->numTarget) {
2558 l = list->free.next;
2559 if (l == &list->free)
2562 node = DRMLISTENTRY(drmBONode, l, head);
2569 void drmBOFreeList(drmBOList *list)
2574 l = list->list.next;
2575 while(l != &list->list) {
2577 node = DRMLISTENTRY(drmBONode, l, head);
2579 l = list->list.next;
2584 l = list->free.next;
2585 while(l != &list->free) {
2587 node = DRMLISTENTRY(drmBONode, l, head);
2589 l = list->free.next;
2594 int drmBOResetList(drmBOList *list)
2599 ret = drmAdjustListNodes(list);
2603 l = list->list.next;
2604 while (l != &list->list) {
2606 DRMLISTADD(l, &list->free);
2608 l = list->list.next;
2610 return drmAdjustListNodes(list);
2613 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2620 l = list->free.next;
2621 if (l == &list->free) {
2622 node = (drmBONode *) malloc(sizeof(*node));
2630 node = DRMLISTENTRY(drmBONode, l, head);
2635 DRMLISTADD(&node->head, &list->list);
2640 void *drmBOListIterator(drmBOList *list)
2642 void *ret = list->list.next;
2644 if (ret == &list->list)
2649 void *drmBOListNext(drmBOList *list, void *iterator)
2653 drmMMListHead *l = (drmMMListHead *) iterator;
2655 if (ret == &list->list)
2660 drmBO *drmBOListBuf(void *iterator)
2663 drmMMListHead *l = (drmMMListHead *) iterator;
2664 node = DRMLISTENTRY(drmBONode, l, head);
2669 int drmBOCreateList(int numTarget, drmBOList *list)
2671 DRMINITLISTHEAD(&list->list);
2672 DRMINITLISTHEAD(&list->free);
2673 list->numTarget = numTarget;
2674 list->numCurrent = 0;
2675 list->numOnList = 0;
2676 return drmAdjustListNodes(list);
2679 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2681 buf->handle = rep->handle;
2682 buf->flags = rep->flags;
2683 buf->size = rep->size;
2684 buf->offset = rep->offset;
2685 buf->mapHandle = rep->arg_handle;
2686 buf->mask = rep->mask;
2687 buf->start = rep->buffer_start;
2688 buf->fenceFlags = rep->fence_flags;
2689 buf->replyFlags = rep->rep_flags;
2690 buf->pageAlignment = rep->page_alignment;
2691 buf->tileInfo = rep->tile_info;
2692 buf->hwTileStride = rep->hw_tile_stride;
2693 buf->desiredTileStride = rep->desired_tile_stride;
2698 int drmBOCreate(int fd, unsigned long start, unsigned long size,
2699 unsigned pageAlignment, void *user_buffer, drm_bo_type_t type,
2701 unsigned hint, drmBO *buf)
2703 struct drm_bo_create_arg arg;
2704 struct drm_bo_create_req *req = &arg.d.req;
2705 struct drm_bo_info_rep *rep = &arg.d.rep;
2708 memset(buf, 0, sizeof(*buf));
2709 memset(&arg, 0, sizeof(arg));
2714 req->page_alignment = pageAlignment;
2716 buf->virtual = NULL;
2719 case drm_bo_type_dc:
2720 req->buffer_start = start;
2722 case drm_bo_type_user:
2723 req->buffer_start = (unsigned long) user_buffer;
2724 buf->virtual = user_buffer;
2726 case drm_bo_type_fake:
2727 req->buffer_start = start;
2734 ret = ioctl(fd, DRM_IOCTL_BO_CREATE, &arg);
2735 } while (ret != 0 && errno == EAGAIN);
2740 drmBOCopyReply(rep, buf);
2741 buf->mapVirtual = NULL;
2747 int drmBODestroy(int fd, drmBO *buf)
2749 struct drm_bo_handle_arg arg;
2751 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2752 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2753 buf->mapVirtual = NULL;
2754 buf->virtual = NULL;
2757 memset(&arg, 0, sizeof(arg));
2758 arg.handle = buf->handle;
2760 if (ioctl(fd, DRM_IOCTL_BO_DESTROY, &arg))
2767 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2769 struct drm_bo_reference_info_arg arg;
2770 struct drm_bo_handle_arg *req = &arg.d.req;
2771 struct drm_bo_info_rep *rep = &arg.d.rep;
2773 memset(&arg, 0, sizeof(arg));
2774 req->handle = handle;
2776 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2779 drmBOCopyReply(rep, buf);
2780 buf->type = drm_bo_type_dc;
2781 buf->mapVirtual = NULL;
2783 buf->virtual = NULL;
2788 int drmBOUnReference(int fd, drmBO *buf)
2790 struct drm_bo_handle_arg arg;
2792 if (buf->mapVirtual && (buf->type != drm_bo_type_fake)) {
2793 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2794 buf->mapVirtual = NULL;
2795 buf->virtual = NULL;
2798 memset(&arg, 0, sizeof(arg));
2799 arg.handle = buf->handle;
2801 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2810 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2811 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2812 * call return an -EBUSY if it can' immediately honor the mapping request.
2815 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2818 struct drm_bo_map_wait_idle_arg arg;
2819 struct drm_bo_info_req *req = &arg.d.req;
2820 struct drm_bo_info_rep *rep = &arg.d.rep;
2824 * Make sure we have a virtual address of the buffer.
2827 if (!buf->virtual && buf->type != drm_bo_type_fake) {
2829 virtual = mmap(0, buf->size + buf->start,
2830 PROT_READ | PROT_WRITE, MAP_SHARED,
2831 fd, buf->mapHandle);
2832 if (virtual == MAP_FAILED) {
2837 buf->mapVirtual = virtual;
2838 buf->virtual = ((char *) virtual) + buf->start;
2841 memset(&arg, 0, sizeof(arg));
2842 req->handle = buf->handle;
2843 req->mask = mapFlags;
2844 req->hint = mapHint;
2847 * May hang if the buffer object is busy.
2848 * This IOCTL synchronizes the buffer.
2852 ret = ioctl(fd, DRM_IOCTL_BO_MAP, &arg);
2853 } while (ret != 0 && errno == EAGAIN);
2858 drmBOCopyReply(rep, buf);
2859 buf->mapFlags = mapFlags;
2861 *address = buf->virtual;
2867 int drmBOUnmap(int fd, drmBO *buf)
2869 struct drm_bo_handle_arg arg;
2871 memset(&arg, 0, sizeof(arg));
2872 arg.handle = buf->handle;
2874 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2881 int drmBOValidate(int fd, drmBO *buf,
2882 uint64_t flags, uint64_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 drmBOSetPin(int fd, drmBO *buf, int pin)
2982 struct drm_bo_set_pin_arg arg;
2983 struct drm_bo_set_pin_req *req = &arg.d.req;
2984 struct drm_bo_info_rep *rep = &arg.d.rep;
2987 memset(&arg, 0, sizeof(arg));
2988 req->handle = buf->handle;
2992 ret = ioctl(fd, DRM_IOCTL_BO_SET_PIN, &arg);
2993 } while (ret && errno == EAGAIN);
2998 drmBOCopyReply(rep, buf);
3003 int drmBOBusy(int fd, drmBO *buf, int *busy)
3005 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
3006 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
3011 int ret = drmBOInfo(fd, buf);
3014 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
3019 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
3023 drmBONode *node, *cur;
3029 for (l = list->list.next; l != &list->list; l = l->next) {
3030 node = DRMLISTENTRY(drmBONode, l, head);
3031 if (node->buf == buf) {
3037 cur = drmAddListItem(list, buf, flags, mask);
3039 drmMsg("Out of memory creating validate list node.\n");
3047 unsigned memMask = (cur->arg1 | mask) & DRM_BO_MASK_MEM;
3048 unsigned memFlags = cur->arg0 & flags & memMask;
3051 drmMsg("Incompatible memory location requests "
3052 "on validate list.\n");
3053 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3054 cur->arg0, cur->arg1);
3055 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3059 if (mask & cur->arg1 & ~DRM_BO_MASK_MEM & (cur->arg0 ^ flags)) {
3060 drmMsg("Incompatible buffer flag requests "
3061 "on validate list.\n");
3062 drmMsg("Previous flag: 0x%08lx, mask: 0x%08lx\n",
3063 cur->arg0, cur->arg1);
3064 drmMsg("Current flag: 0x%08lx, mask: 0x%08lx\n",
3069 cur->arg0 = memFlags | ((cur->arg0 | flags) &
3070 cur->arg1 & ~DRM_BO_MASK_MEM);
3076 int drmBOValidateList(int fd, drmBOList *list)
3080 struct drm_bo_op_arg *arg, *first;
3081 struct drm_bo_op_req *req;
3082 struct drm_bo_arg_rep *rep;
3083 uint64_t *prevNext = NULL;
3089 for (l = list->list.next; l != &list->list; l = l->next) {
3090 node = DRMLISTENTRY(drmBONode, l, head);
3092 arg = &node->bo_arg;
3099 *prevNext = (unsigned long) arg;
3101 memset(arg, 0, sizeof(*arg));
3102 prevNext = &arg->next;
3103 req->bo_req.handle = node->buf->handle;
3104 req->op = drm_bo_validate;
3105 req->bo_req.flags = node->arg0;
3106 req->bo_req.hint = 0;
3107 req->bo_req.mask = node->arg1;
3108 req->bo_req.fence_class = 0; /* Backwards compat. */
3115 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3116 } while (ret && errno == EAGAIN);
3121 for (l = list->list.next; l != &list->list; l = l->next) {
3122 node = DRMLISTENTRY(drmBONode, l, head);
3123 arg = &node->bo_arg;
3126 if (!arg->handled) {
3127 drmMsg("Unhandled request\n");
3134 drmBOCopyReply(&rep->bo_info, buf);
3140 int drmBOFenceList(int fd, drmBOList *list, unsigned fenceHandle)
3144 struct drm_bo_op_arg *arg, *first;
3145 struct drm_bo_op_req *req;
3146 struct drm_bo_arg_rep *rep;
3147 uint64_t *prevNext = NULL;
3149 unsigned fence_flags;
3154 for (l = list->list.next; l != &list->list; l = l->next) {
3155 node = DRMLISTENTRY(drmBONode, l, head);
3157 arg = &node->bo_arg;
3164 *prevNext = (unsigned long) arg;
3166 memset(arg, 0, sizeof(*arg));
3167 prevNext = &arg->next;
3168 req->bo_req.handle = node->buf->handle;
3169 req->op = drm_bo_fence;
3170 req->bo_req.mask = node->arg0;
3171 req->arg_handle = fenceHandle;
3177 ret = ioctl(fd, DRM_IOCTL_BO_OP, first);
3182 for (l = list->list.next; l != &list->list; l = l->next) {
3183 node = DRMLISTENTRY(drmBONode, l, head);
3185 arg = &node->bo_arg;
3192 drmBOCopyReply(&rep->bo_info, node->buf);
3198 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
3201 struct drm_mm_init_arg arg;
3203 memset(&arg, 0, sizeof(arg));
3205 arg.magic = DRM_BO_INIT_MAGIC;
3206 arg.major = DRM_BO_INIT_MAJOR;
3207 arg.minor = DRM_BO_INIT_MINOR;
3208 arg.p_offset = pOffset;
3210 arg.mem_type = memType;
3212 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3217 int drmMMTakedown(int fd, unsigned memType)
3219 struct drm_mm_type_arg arg;
3221 memset(&arg, 0, sizeof(arg));
3222 arg.mem_type = memType;
3224 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
3229 int drmMMLock(int fd, unsigned memType)
3231 struct drm_mm_type_arg arg;
3234 memset(&arg, 0, sizeof(arg));
3235 arg.mem_type = memType;
3238 ret = ioctl(fd, DRM_IOCTL_MM_LOCK, &arg);
3239 } while (ret && errno == EAGAIN);
3241 return (ret) ? -errno : 0;
3244 int drmMMUnlock(int fd, unsigned memType)
3246 struct drm_mm_type_arg arg;
3249 memset(&arg, 0, sizeof(arg));
3251 arg.mem_type = memType;
3254 ret = ioctl(fd, DRM_IOCTL_MM_UNLOCK, &arg);
3255 } while (ret && errno == EAGAIN);
3257 return (ret) ? -errno : 0;
3260 #define DRM_MAX_FDS 16
3265 } connection[DRM_MAX_FDS];
3267 static int nr_fds = 0;
3269 int drmOpenOnce(void *unused,
3276 for (i = 0; i < nr_fds; i++)
3277 if (strcmp(BusID, connection[i].BusID) == 0) {
3278 connection[i].refcount++;
3280 return connection[i].fd;
3283 fd = drmOpen(unused, BusID);
3284 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
3287 connection[nr_fds].BusID = strdup(BusID);
3288 connection[nr_fds].fd = fd;
3289 connection[nr_fds].refcount = 1;
3293 fprintf(stderr, "saved connection %d for %s %d\n",
3294 nr_fds, connection[nr_fds].BusID,
3295 strcmp(BusID, connection[nr_fds].BusID));
3302 void drmCloseOnce(int fd)
3306 for (i = 0; i < nr_fds; i++) {
3307 if (fd == connection[i].fd) {
3308 if (--connection[i].refcount == 0) {
3309 drmClose(connection[i].fd);
3310 free(connection[i].BusID);
3313 connection[i] = connection[nr_fds];