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 #define DRM_NODE_CONTROL 0
91 #define DRM_NODE_RENDER 1
93 static drmServerInfoPtr drm_server_info;
95 void drmSetServerInfo(drmServerInfoPtr info)
97 drm_server_info = info;
101 * Output a message to stderr.
103 * \param format printf() like format string.
106 * This function is a wrapper around vfprintf().
109 static int drmDebugPrint(const char *format, va_list ap)
111 return vfprintf(stderr, format, ap);
114 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
117 drmMsg(const char *format, ...)
121 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
123 va_start(ap, format);
124 if (drm_server_info) {
125 drm_server_info->debug_print(format,ap);
127 drm_debug_print(format, ap);
134 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
136 drm_debug_print = debug_msg_ptr;
139 static void *drmHashTable = NULL; /* Context switch callbacks */
141 void *drmGetHashTable(void)
146 void *drmMalloc(int size)
149 if ((pt = malloc(size)))
154 void drmFree(void *pt)
160 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
161 static char *drmStrdup(const char *s)
168 retval = malloc(strlen(s)+1);
178 static unsigned long drmGetKeyFromFd(int fd)
187 drmHashEntry *drmGetEntry(int fd)
189 unsigned long key = drmGetKeyFromFd(fd);
194 drmHashTable = drmHashCreate();
196 if (drmHashLookup(drmHashTable, key, &value)) {
197 entry = drmMalloc(sizeof(*entry));
200 entry->tagTable = drmHashCreate();
201 drmHashInsert(drmHashTable, key, entry);
209 * Compare two busid strings
214 * \return 1 if matched.
217 * This function compares two bus ID strings. It understands the older
218 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
219 * domain, b is bus, d is device, f is function.
221 static int drmMatchBusID(const char *id1, const char *id2)
223 /* First, check if the IDs are exactly the same */
224 if (strcasecmp(id1, id2) == 0)
227 /* Try to match old/new-style PCI bus IDs. */
228 if (strncasecmp(id1, "pci", 3) == 0) {
233 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
236 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
241 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
244 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
249 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
258 * Open the DRM device, creating it if necessary.
260 * \param dev major and minor numbers of the device.
261 * \param minor minor number of the device.
263 * \return a file descriptor on success, or a negative value on error.
266 * Assembles the device name from \p minor and opens it, creating the device
267 * special file node with the major and minor numbers specified by \p dev and
268 * parent directory if necessary and was called by root.
270 static int drmOpenDevice(long dev, int minor, int type)
275 mode_t devmode = DRM_DEV_MODE, serv_mode;
276 int isroot = !geteuid();
277 uid_t user = DRM_DEV_UID;
278 gid_t group = DRM_DEV_GID, serv_group;
280 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
281 drmMsg("drmOpenDevice: node name is %s\n", buf);
283 if (drm_server_info) {
284 drm_server_info->get_perms(&serv_group, &serv_mode);
285 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
286 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
287 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
290 if (stat(DRM_DIR_NAME, &st)) {
292 return DRM_ERR_NOT_ROOT;
293 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
294 chown(DRM_DIR_NAME, 0, 0); /* root:root */
295 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
298 /* Check if the device node exists and create it if necessary. */
299 if (stat(buf, &st)) {
301 return DRM_ERR_NOT_ROOT;
303 mknod(buf, S_IFCHR | devmode, dev);
306 if (drm_server_info) {
307 chown(buf, user, group);
311 fd = open(buf, O_RDWR, 0);
312 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
313 fd, fd < 0 ? strerror(errno) : "OK");
317 /* Check if the device node is not what we expect it to be, and recreate it
318 * and try again if so.
320 if (st.st_rdev != dev) {
322 return DRM_ERR_NOT_ROOT;
324 mknod(buf, S_IFCHR | devmode, dev);
325 if (drm_server_info) {
326 chown(buf, user, group);
330 fd = open(buf, O_RDWR, 0);
331 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
332 fd, fd < 0 ? strerror(errno) : "OK");
336 drmMsg("drmOpenDevice: Open failed\n");
343 * Open the DRM device
345 * \param minor device minor number.
346 * \param create allow to create the device if set.
348 * \return a file descriptor on success, or a negative value on error.
351 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
352 * name from \p minor and opens it.
354 static int drmOpenMinor(int minor, int create, int type)
360 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor, type);
362 sprintf(buf, type ? DRM_DEV_NAME : DRM_CONTROL_DEV_NAME, DRM_DIR_NAME, minor);
363 if ((fd = open(buf, O_RDWR, 0)) >= 0)
370 * Determine whether the DRM kernel driver has been loaded.
372 * \return 1 if the DRM driver is loaded, 0 otherwise.
375 * Determine the presence of the kernel driver by attempting to open the 0
376 * minor and get version information. For backward compatibility with older
377 * Linux implementations, /proc/dri is also checked.
379 int drmAvailable(void)
381 drmVersionPtr version;
385 if ((fd = drmOpenMinor(0, 1, DRM_NODE_RENDER)) < 0) {
387 /* Try proc for backward Linux compatibility */
388 if (!access("/proc/dri/0", R_OK))
394 if ((version = drmGetVersion(fd))) {
396 drmFreeVersion(version);
405 * Open the device by bus ID.
407 * \param busid bus ID.
409 * \return a file descriptor on success, or a negative value on error.
412 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
413 * comparing the device bus ID with the one supplied.
415 * \sa drmOpenMinor() and drmGetBusid().
417 static int drmOpenByBusid(const char *busid)
424 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
425 for (i = 0; i < DRM_MAX_MINOR; i++) {
426 fd = drmOpenMinor(i, 1, DRM_NODE_RENDER);
427 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
431 sv.drm_dd_major = -1; /* Don't care */
432 sv.drm_dd_minor = -1; /* Don't care */
433 drmSetInterfaceVersion(fd, &sv);
434 buf = drmGetBusid(fd);
435 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
436 if (buf && drmMatchBusID(buf, busid)) {
450 * Open the device by name.
452 * \param name driver name.
454 * \return a file descriptor on success, or a negative value on error.
457 * This function opens the first minor number that matches the driver name and
458 * isn't already in use. If it's in use it then it will already have a bus ID
461 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
463 static int drmOpenByName(const char *name)
467 drmVersionPtr version;
470 if (!drmAvailable()) {
471 if (!drm_server_info) {
475 /* try to load the kernel module now */
476 if (!drm_server_info->load_module(name)) {
477 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
484 * Open the first minor number that matches the driver name and isn't
485 * already in use. If it's in use it will have a busid assigned already.
487 for (i = 0; i < DRM_MAX_MINOR; i++) {
488 if ((fd = drmOpenMinor(i, 1, DRM_NODE_RENDER)) >= 0) {
489 if ((version = drmGetVersion(fd))) {
490 if (!strcmp(version->name, name)) {
491 drmFreeVersion(version);
492 id = drmGetBusid(fd);
493 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
502 drmFreeVersion(version);
510 /* Backward-compatibility /proc support */
511 for (i = 0; i < 8; i++) {
512 char proc_name[64], buf[512];
513 char *driver, *pt, *devstring;
516 sprintf(proc_name, "/proc/dri/%d/name", i);
517 if ((fd = open(proc_name, 0, 0)) >= 0) {
518 retcode = read(fd, buf, sizeof(buf)-1);
521 buf[retcode-1] = '\0';
522 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
524 if (*pt) { /* Device is next */
526 if (!strcmp(driver, name)) { /* Match */
527 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
529 if (*pt) { /* Found busid */
530 return drmOpenByBusid(++pt);
531 } else { /* No busid */
532 return drmOpenDevice(strtol(devstring, NULL, 0),i, DRM_NODE_RENDER);
546 * Open the DRM device.
548 * Looks up the specified name and bus ID, and opens the device found. The
549 * entry in /dev/dri is created if necessary and if called by root.
551 * \param name driver name. Not referenced if bus ID is supplied.
552 * \param busid bus ID. Zero if not known.
554 * \return a file descriptor on success, or a negative value on error.
557 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
560 int drmOpen(const char *name, const char *busid)
562 if (!drmAvailable() && name != NULL && drm_server_info) {
563 /* try to load the kernel */
564 if (!drm_server_info->load_module(name)) {
565 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
571 int fd = drmOpenByBusid(busid);
577 return drmOpenByName(name);
584 * Free the version information returned by drmGetVersion().
586 * \param v pointer to the version information.
589 * It frees the memory pointed by \p %v as well as all the non-null strings
592 void drmFreeVersion(drmVersionPtr v)
604 * Free the non-public version information returned by the kernel.
606 * \param v pointer to the version information.
609 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
610 * the non-null strings pointers in it.
612 static void drmFreeKernelVersion(drm_version_t *v)
624 * Copy version information.
626 * \param d destination pointer.
627 * \param s source pointer.
630 * Used by drmGetVersion() to translate the information returned by the ioctl
631 * interface in a private structure into the public structure counterpart.
633 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
635 d->version_major = s->version_major;
636 d->version_minor = s->version_minor;
637 d->version_patchlevel = s->version_patchlevel;
638 d->name_len = s->name_len;
639 d->name = drmStrdup(s->name);
640 d->date_len = s->date_len;
641 d->date = drmStrdup(s->date);
642 d->desc_len = s->desc_len;
643 d->desc = drmStrdup(s->desc);
648 * Query the driver version information.
650 * \param fd file descriptor.
652 * \return pointer to a drmVersion structure which should be freed with
655 * \note Similar information is available via /proc/dri.
658 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
659 * first with zeros to get the string lengths, and then the actually strings.
660 * It also null-terminates them since they might not be already.
662 drmVersionPtr drmGetVersion(int fd)
664 drmVersionPtr retval;
665 drm_version_t *version = drmMalloc(sizeof(*version));
667 version->name_len = 0;
668 version->name = NULL;
669 version->date_len = 0;
670 version->date = NULL;
671 version->desc_len = 0;
672 version->desc = NULL;
674 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
675 drmFreeKernelVersion(version);
679 if (version->name_len)
680 version->name = drmMalloc(version->name_len + 1);
681 if (version->date_len)
682 version->date = drmMalloc(version->date_len + 1);
683 if (version->desc_len)
684 version->desc = drmMalloc(version->desc_len + 1);
686 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
687 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
688 drmFreeKernelVersion(version);
692 /* The results might not be null-terminated strings, so terminate them. */
693 if (version->name_len) version->name[version->name_len] = '\0';
694 if (version->date_len) version->date[version->date_len] = '\0';
695 if (version->desc_len) version->desc[version->desc_len] = '\0';
697 retval = drmMalloc(sizeof(*retval));
698 drmCopyVersion(retval, version);
699 drmFreeKernelVersion(version);
705 * Get version information for the DRM user space library.
707 * This version number is driver independent.
709 * \param fd file descriptor.
711 * \return version information.
714 * This function allocates and fills a drm_version structure with a hard coded
717 drmVersionPtr drmGetLibVersion(int fd)
719 drm_version_t *version = drmMalloc(sizeof(*version));
722 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
723 * revision 1.0.x = original DRM interface with no drmGetLibVersion
724 * entry point and many drm<Device> extensions
725 * revision 1.1.x = added drmCommand entry points for device extensions
726 * added drmGetLibVersion to identify libdrm.a version
727 * revision 1.2.x = added drmSetInterfaceVersion
728 * modified drmOpen to handle both busid and name
729 * revision 1.3.x = added server + memory manager
731 version->version_major = 1;
732 version->version_minor = 3;
733 version->version_patchlevel = 0;
735 return (drmVersionPtr)version;
740 * Free the bus ID information.
742 * \param busid bus ID information string as given by drmGetBusid().
745 * This function is just frees the memory pointed by \p busid.
747 void drmFreeBusid(const char *busid)
749 drmFree((void *)busid);
754 * Get the bus ID of the device.
756 * \param fd file descriptor.
758 * \return bus ID string.
761 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
762 * get the string length and data, passing the arguments in a drm_unique
765 char *drmGetBusid(int fd)
772 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
774 u.unique = drmMalloc(u.unique_len + 1);
775 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
777 u.unique[u.unique_len] = '\0';
784 * Set the bus ID of the device.
786 * \param fd file descriptor.
787 * \param busid bus ID string.
789 * \return zero on success, negative on failure.
792 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
793 * the arguments in a drm_unique structure.
795 int drmSetBusid(int fd, const char *busid)
799 u.unique = (char *)busid;
800 u.unique_len = strlen(busid);
802 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
808 int drmGetMagic(int fd, drm_magic_t * magic)
813 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
819 int drmAuthMagic(int fd, drm_magic_t magic)
824 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
830 * Specifies a range of memory that is available for mapping by a
833 * \param fd file descriptor.
834 * \param offset usually the physical address. The actual meaning depends of
835 * the \p type parameter. See below.
836 * \param size of the memory in bytes.
837 * \param type type of the memory to be mapped.
838 * \param flags combination of several flags to modify the function actions.
839 * \param handle will be set to a value that may be used as the offset
840 * parameter for mmap().
842 * \return zero on success or a negative value on error.
844 * \par Mapping the frame buffer
845 * For the frame buffer
846 * - \p offset will be the physical address of the start of the frame buffer,
847 * - \p size will be the size of the frame buffer in bytes, and
848 * - \p type will be DRM_FRAME_BUFFER.
851 * The area mapped will be uncached. If MTRR support is available in the
852 * kernel, the frame buffer area will be set to write combining.
854 * \par Mapping the MMIO register area
855 * For the MMIO register area,
856 * - \p offset will be the physical address of the start of the register area,
857 * - \p size will be the size of the register area bytes, and
858 * - \p type will be DRM_REGISTERS.
860 * The area mapped will be uncached.
862 * \par Mapping the SAREA
864 * - \p offset will be ignored and should be set to zero,
865 * - \p size will be the desired size of the SAREA in bytes,
866 * - \p type will be DRM_SHM.
869 * A shared memory area of the requested size will be created and locked in
870 * kernel memory. This area may be mapped into client-space by using the handle
873 * \note May only be called by root.
876 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
877 * the arguments in a drm_map structure.
879 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
880 drmMapFlags flags, drm_handle_t *handle)
889 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map))
892 *handle = (drm_handle_t)map.handle;
896 int drmRmMap(int fd, drm_handle_t handle)
900 map.handle = (void *)handle;
902 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map))
908 * Make buffers available for DMA transfers.
910 * \param fd file descriptor.
911 * \param count number of buffers.
912 * \param size size of each buffer.
913 * \param flags buffer allocation flags.
914 * \param agp_offset offset in the AGP aperture
916 * \return number of buffers allocated, negative on error.
919 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
923 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
926 drm_buf_desc_t request;
928 request.count = count;
930 request.low_mark = 0;
931 request.high_mark = 0;
932 request.flags = flags;
933 request.agp_start = agp_offset;
935 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request))
937 return request.count;
940 int drmMarkBufs(int fd, double low, double high)
948 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
954 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
957 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
963 for (i = 0; i < info.count; i++) {
964 info.list[i].low_mark = low * info.list[i].count;
965 info.list[i].high_mark = high * info.list[i].count;
966 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
980 * \param fd file descriptor.
981 * \param count number of buffers to free.
982 * \param list list of buffers to be freed.
984 * \return zero on success, or a negative value on failure.
986 * \note This function is primarily used for debugging.
989 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
990 * the arguments in a drm_buf_free structure.
992 int drmFreeBufs(int fd, int count, int *list)
994 drm_buf_free_t request;
996 request.count = count;
998 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1007 * \param fd file descriptor.
1010 * This function closes the file descriptor.
1012 int drmClose(int fd)
1014 unsigned long key = drmGetKeyFromFd(fd);
1015 drmHashEntry *entry = drmGetEntry(fd);
1017 drmHashDestroy(entry->tagTable);
1020 entry->tagTable = NULL;
1022 drmHashDelete(drmHashTable, key);
1030 * Map a region of memory.
1032 * \param fd file descriptor.
1033 * \param handle handle returned by drmAddMap().
1034 * \param size size in bytes. Must match the size used by drmAddMap().
1035 * \param address will contain the user-space virtual address where the mapping
1038 * \return zero on success, or a negative value on failure.
1041 * This function is a wrapper for mmap().
1043 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1045 static unsigned long pagesize_mask = 0;
1051 pagesize_mask = getpagesize() - 1;
1053 size = (size + pagesize_mask) & ~pagesize_mask;
1055 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1056 if (*address == MAP_FAILED)
1063 * Unmap mappings obtained with drmMap().
1065 * \param address address as given by drmMap().
1066 * \param size size in bytes. Must match the size used by drmMap().
1068 * \return zero on success, or a negative value on failure.
1071 * This function is a wrapper for munmap().
1073 int drmUnmap(drmAddress address, drmSize size)
1075 return munmap(address, size);
1078 drmBufInfoPtr drmGetBufInfo(int fd)
1080 drm_buf_info_t info;
1081 drmBufInfoPtr retval;
1087 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1091 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1094 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1099 retval = drmMalloc(sizeof(*retval));
1100 retval->count = info.count;
1101 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1102 for (i = 0; i < info.count; i++) {
1103 retval->list[i].count = info.list[i].count;
1104 retval->list[i].size = info.list[i].size;
1105 retval->list[i].low_mark = info.list[i].low_mark;
1106 retval->list[i].high_mark = info.list[i].high_mark;
1115 * Map all DMA buffers into client-virtual space.
1117 * \param fd file descriptor.
1119 * \return a pointer to a ::drmBufMap structure.
1121 * \note The client may not use these buffers until obtaining buffer indices
1125 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1126 * information about the buffers in a drm_buf_map structure into the
1127 * client-visible data structures.
1129 drmBufMapPtr drmMapBufs(int fd)
1132 drmBufMapPtr retval;
1137 bufs.virtual = NULL;
1138 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1144 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1147 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1152 retval = drmMalloc(sizeof(*retval));
1153 retval->count = bufs.count;
1154 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1155 for (i = 0; i < bufs.count; i++) {
1156 retval->list[i].idx = bufs.list[i].idx;
1157 retval->list[i].total = bufs.list[i].total;
1158 retval->list[i].used = 0;
1159 retval->list[i].address = bufs.list[i].address;
1169 * Unmap buffers allocated with drmMapBufs().
1171 * \return zero on success, or negative value on failure.
1174 * Calls munmap() for every buffer stored in \p bufs and frees the
1175 * memory allocated by drmMapBufs().
1177 int drmUnmapBufs(drmBufMapPtr bufs)
1181 for (i = 0; i < bufs->count; i++) {
1182 munmap(bufs->list[i].address, bufs->list[i].total);
1185 drmFree(bufs->list);
1192 #define DRM_DMA_RETRY 16
1195 * Reserve DMA buffers.
1197 * \param fd file descriptor.
1200 * \return zero on success, or a negative value on failure.
1203 * Assemble the arguments into a drm_dma structure and keeps issuing the
1204 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1206 int drmDMA(int fd, drmDMAReqPtr request)
1211 dma.context = request->context;
1212 dma.send_count = request->send_count;
1213 dma.send_indices = request->send_list;
1214 dma.send_sizes = request->send_sizes;
1215 dma.flags = request->flags;
1216 dma.request_count = request->request_count;
1217 dma.request_size = request->request_size;
1218 dma.request_indices = request->request_list;
1219 dma.request_sizes = request->request_sizes;
1220 dma.granted_count = 0;
1223 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1224 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1227 request->granted_count = dma.granted_count;
1236 * Obtain heavyweight hardware lock.
1238 * \param fd file descriptor.
1239 * \param context context.
1240 * \param flags flags that determine the sate of the hardware when the function
1243 * \return always zero.
1246 * This function translates the arguments into a drm_lock structure and issue
1247 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1249 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1253 lock.context = context;
1255 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1256 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1257 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1258 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1259 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1260 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1262 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1268 * Release the hardware lock.
1270 * \param fd file descriptor.
1271 * \param context context.
1273 * \return zero on success, or a negative value on failure.
1276 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1277 * argument in a drm_lock structure.
1279 int drmUnlock(int fd, drm_context_t context)
1283 lock.context = context;
1285 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1288 drm_context_t *drmGetReservedContextList(int fd, int *count)
1292 drm_context_t * retval;
1296 res.contexts = NULL;
1297 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1303 if (!(list = drmMalloc(res.count * sizeof(*list))))
1305 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1310 res.contexts = list;
1311 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1314 for (i = 0; i < res.count; i++)
1315 retval[i] = list[i].handle;
1322 void drmFreeReservedContextList(drm_context_t *pt)
1330 * Used by the X server during GLXContext initialization. This causes
1331 * per-context kernel-level resources to be allocated.
1333 * \param fd file descriptor.
1334 * \param handle is set on success. To be used by the client when requesting DMA
1335 * dispatch with drmDMA().
1337 * \return zero on success, or a negative value on failure.
1339 * \note May only be called by root.
1342 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1343 * argument in a drm_ctx structure.
1345 int drmCreateContext(int fd, drm_context_t *handle)
1349 ctx.flags = 0; /* Modified with functions below */
1350 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1352 *handle = ctx.handle;
1356 int drmSwitchToContext(int fd, drm_context_t context)
1360 ctx.handle = context;
1361 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1366 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1371 * Context preserving means that no context switches are done between DMA
1372 * buffers from one context and the next. This is suitable for use in the
1373 * X server (which promises to maintain hardware context), or in the
1374 * client-side library when buffers are swapped on behalf of two threads.
1376 ctx.handle = context;
1378 if (flags & DRM_CONTEXT_PRESERVED)
1379 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1380 if (flags & DRM_CONTEXT_2DONLY)
1381 ctx.flags |= _DRM_CONTEXT_2DONLY;
1382 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1387 int drmGetContextFlags(int fd, drm_context_t context,
1388 drm_context_tFlagsPtr flags)
1392 ctx.handle = context;
1393 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1396 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1397 *flags |= DRM_CONTEXT_PRESERVED;
1398 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1399 *flags |= DRM_CONTEXT_2DONLY;
1406 * Free any kernel-level resources allocated with drmCreateContext() associated
1409 * \param fd file descriptor.
1410 * \param handle handle given by drmCreateContext().
1412 * \return zero on success, or a negative value on failure.
1414 * \note May only be called by root.
1417 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1418 * argument in a drm_ctx structure.
1420 int drmDestroyContext(int fd, drm_context_t handle)
1423 ctx.handle = handle;
1424 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1429 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1432 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1434 *handle = draw.handle;
1438 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1441 draw.handle = handle;
1442 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1447 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1448 drm_drawable_info_type_t type, unsigned int num,
1451 drm_update_draw_t update;
1453 update.handle = handle;
1456 update.data = (unsigned long long)(unsigned long)data;
1458 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1465 * Acquire the AGP device.
1467 * Must be called before any of the other AGP related calls.
1469 * \param fd file descriptor.
1471 * \return zero on success, or a negative value on failure.
1474 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1476 int drmAgpAcquire(int fd)
1478 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1485 * Release the AGP device.
1487 * \param fd file descriptor.
1489 * \return zero on success, or a negative value on failure.
1492 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1494 int drmAgpRelease(int fd)
1496 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1505 * \param fd file descriptor.
1506 * \param mode AGP mode.
1508 * \return zero on success, or a negative value on failure.
1511 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1512 * argument in a drm_agp_mode structure.
1514 int drmAgpEnable(int fd, unsigned long mode)
1519 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1526 * Allocate a chunk of AGP memory.
1528 * \param fd file descriptor.
1529 * \param size requested memory size in bytes. Will be rounded to page boundary.
1530 * \param type type of memory to allocate.
1531 * \param address if not zero, will be set to the physical address of the
1533 * \param handle on success will be set to a handle of the allocated memory.
1535 * \return zero on success, or a negative value on failure.
1538 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1539 * arguments in a drm_agp_buffer structure.
1541 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1542 unsigned long *address, drm_handle_t *handle)
1546 *handle = DRM_AGP_NO_HANDLE;
1550 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1553 *address = b.physical;
1560 * Free a chunk of AGP memory.
1562 * \param fd file descriptor.
1563 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1565 * \return zero on success, or a negative value on failure.
1568 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1569 * argument in a drm_agp_buffer structure.
1571 int drmAgpFree(int fd, drm_handle_t handle)
1577 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b))
1584 * Bind a chunk of AGP memory.
1586 * \param fd file descriptor.
1587 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1588 * \param offset offset in bytes. It will round to page boundary.
1590 * \return zero on success, or a negative value on failure.
1593 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1594 * argument in a drm_agp_binding structure.
1596 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1598 drm_agp_binding_t b;
1602 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b))
1609 * Unbind a chunk of AGP memory.
1611 * \param fd file descriptor.
1612 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1614 * \return zero on success, or a negative value on failure.
1617 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1618 * the argument in a drm_agp_binding structure.
1620 int drmAgpUnbind(int fd, drm_handle_t handle)
1622 drm_agp_binding_t b;
1626 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1633 * Get AGP driver major version number.
1635 * \param fd file descriptor.
1637 * \return major version number on success, or a negative value on failure..
1640 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1641 * necessary information in a drm_agp_info structure.
1643 int drmAgpVersionMajor(int fd)
1647 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1649 return i.agp_version_major;
1654 * Get AGP driver minor version number.
1656 * \param fd file descriptor.
1658 * \return minor version number on success, or a negative value on failure.
1661 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1662 * necessary information in a drm_agp_info structure.
1664 int drmAgpVersionMinor(int fd)
1668 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1670 return i.agp_version_minor;
1677 * \param fd file descriptor.
1679 * \return mode on success, or zero on failure.
1682 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1683 * necessary information in a drm_agp_info structure.
1685 unsigned long drmAgpGetMode(int fd)
1689 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1696 * Get AGP aperture base.
1698 * \param fd file descriptor.
1700 * \return aperture base on success, zero on failure.
1703 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1704 * necessary information in a drm_agp_info structure.
1706 unsigned long drmAgpBase(int fd)
1710 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1712 return i.aperture_base;
1717 * Get AGP aperture size.
1719 * \param fd file descriptor.
1721 * \return aperture size on success, zero on failure.
1724 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1725 * necessary information in a drm_agp_info structure.
1727 unsigned long drmAgpSize(int fd)
1731 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1733 return i.aperture_size;
1738 * Get used AGP memory.
1740 * \param fd file descriptor.
1742 * \return memory used on success, or zero on failure.
1745 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1746 * necessary information in a drm_agp_info structure.
1748 unsigned long drmAgpMemoryUsed(int fd)
1752 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1754 return i.memory_used;
1759 * Get available AGP memory.
1761 * \param fd file descriptor.
1763 * \return memory available on success, or zero on failure.
1766 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1767 * necessary information in a drm_agp_info structure.
1769 unsigned long drmAgpMemoryAvail(int fd)
1773 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1775 return i.memory_allowed;
1780 * Get hardware vendor ID.
1782 * \param fd file descriptor.
1784 * \return vendor ID on success, or zero on failure.
1787 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1788 * necessary information in a drm_agp_info structure.
1790 unsigned int drmAgpVendorId(int fd)
1794 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1801 * Get hardware device ID.
1803 * \param fd file descriptor.
1805 * \return zero on success, or zero on failure.
1808 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1809 * necessary information in a drm_agp_info structure.
1811 unsigned int drmAgpDeviceId(int fd)
1815 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1820 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1822 drm_scatter_gather_t sg;
1827 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1829 *handle = sg.handle;
1833 int drmScatterGatherFree(int fd, drm_handle_t handle)
1835 drm_scatter_gather_t sg;
1839 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg))
1847 * \param fd file descriptor.
1848 * \param vbl pointer to a drmVBlank structure.
1850 * \return zero on success, or a negative value on failure.
1853 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1855 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1860 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1861 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1862 } while (ret && errno == EINTR);
1867 int drmError(int err, const char *label)
1870 case DRM_ERR_NO_DEVICE:
1871 fprintf(stderr, "%s: no device\n", label);
1873 case DRM_ERR_NO_ACCESS:
1874 fprintf(stderr, "%s: no access\n", label);
1876 case DRM_ERR_NOT_ROOT:
1877 fprintf(stderr, "%s: not root\n", label);
1879 case DRM_ERR_INVALID:
1880 fprintf(stderr, "%s: invalid args\n", label);
1885 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1893 * Install IRQ handler.
1895 * \param fd file descriptor.
1896 * \param irq IRQ number.
1898 * \return zero on success, or a negative value on failure.
1901 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1902 * argument in a drm_control structure.
1904 int drmCtlInstHandler(int fd, int irq)
1908 ctl.func = DRM_INST_HANDLER;
1910 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1917 * Uninstall IRQ handler.
1919 * \param fd file descriptor.
1921 * \return zero on success, or a negative value on failure.
1924 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1925 * argument in a drm_control structure.
1927 int drmCtlUninstHandler(int fd)
1931 ctl.func = DRM_UNINST_HANDLER;
1933 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1938 int drmFinish(int fd, int context, drmLockFlags flags)
1942 lock.context = context;
1944 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1945 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1946 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1947 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1948 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1949 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1950 if (ioctl(fd, DRM_IOCTL_FINISH, &lock))
1956 * Get IRQ from bus ID.
1958 * \param fd file descriptor.
1959 * \param busnum bus number.
1960 * \param devnum device number.
1961 * \param funcnum function number.
1963 * \return IRQ number on success, or a negative value on failure.
1966 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1967 * arguments in a drm_irq_busid structure.
1969 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1975 p.funcnum = funcnum;
1976 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1981 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1983 drmHashEntry *entry = drmGetEntry(fd);
1985 if (drmHashInsert(entry->tagTable, context, tag)) {
1986 drmHashDelete(entry->tagTable, context);
1987 drmHashInsert(entry->tagTable, context, tag);
1992 int drmDelContextTag(int fd, drm_context_t context)
1994 drmHashEntry *entry = drmGetEntry(fd);
1996 return drmHashDelete(entry->tagTable, context);
1999 void *drmGetContextTag(int fd, drm_context_t context)
2001 drmHashEntry *entry = drmGetEntry(fd);
2004 if (drmHashLookup(entry->tagTable, context, &value))
2010 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2011 drm_handle_t handle)
2013 drm_ctx_priv_map_t map;
2015 map.ctx_id = ctx_id;
2016 map.handle = (void *)handle;
2018 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2023 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2024 drm_handle_t *handle)
2026 drm_ctx_priv_map_t map;
2028 map.ctx_id = ctx_id;
2030 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2033 *handle = (drm_handle_t)map.handle;
2038 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2039 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2045 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map))
2047 *offset = map.offset;
2051 *handle = (unsigned long)map.handle;
2056 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2057 unsigned long *magic, unsigned long *iocs)
2059 drm_client_t client;
2062 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2064 *auth = client.auth;
2067 *magic = client.magic;
2068 *iocs = client.iocs;
2072 int drmGetStats(int fd, drmStatsT *stats)
2077 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s))
2081 memset(stats, 0, sizeof(*stats));
2082 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2086 stats->data[i].long_format = "%-20.20s"; \
2087 stats->data[i].rate_format = "%8.8s"; \
2088 stats->data[i].isvalue = 1; \
2089 stats->data[i].verbose = 0
2092 stats->data[i].long_format = "%-20.20s"; \
2093 stats->data[i].rate_format = "%5.5s"; \
2094 stats->data[i].isvalue = 0; \
2095 stats->data[i].mult_names = "kgm"; \
2096 stats->data[i].mult = 1000; \
2097 stats->data[i].verbose = 0
2100 stats->data[i].long_format = "%-20.20s"; \
2101 stats->data[i].rate_format = "%5.5s"; \
2102 stats->data[i].isvalue = 0; \
2103 stats->data[i].mult_names = "KGM"; \
2104 stats->data[i].mult = 1024; \
2105 stats->data[i].verbose = 0
2108 stats->count = s.count;
2109 for (i = 0; i < s.count; i++) {
2110 stats->data[i].value = s.data[i].value;
2111 switch (s.data[i].type) {
2112 case _DRM_STAT_LOCK:
2113 stats->data[i].long_name = "Lock";
2114 stats->data[i].rate_name = "Lock";
2117 case _DRM_STAT_OPENS:
2118 stats->data[i].long_name = "Opens";
2119 stats->data[i].rate_name = "O";
2121 stats->data[i].verbose = 1;
2123 case _DRM_STAT_CLOSES:
2124 stats->data[i].long_name = "Closes";
2125 stats->data[i].rate_name = "Lock";
2127 stats->data[i].verbose = 1;
2129 case _DRM_STAT_IOCTLS:
2130 stats->data[i].long_name = "Ioctls";
2131 stats->data[i].rate_name = "Ioc/s";
2134 case _DRM_STAT_LOCKS:
2135 stats->data[i].long_name = "Locks";
2136 stats->data[i].rate_name = "Lck/s";
2139 case _DRM_STAT_UNLOCKS:
2140 stats->data[i].long_name = "Unlocks";
2141 stats->data[i].rate_name = "Unl/s";
2145 stats->data[i].long_name = "IRQs";
2146 stats->data[i].rate_name = "IRQ/s";
2149 case _DRM_STAT_PRIMARY:
2150 stats->data[i].long_name = "Primary Bytes";
2151 stats->data[i].rate_name = "PB/s";
2154 case _DRM_STAT_SECONDARY:
2155 stats->data[i].long_name = "Secondary Bytes";
2156 stats->data[i].rate_name = "SB/s";
2160 stats->data[i].long_name = "DMA";
2161 stats->data[i].rate_name = "DMA/s";
2164 case _DRM_STAT_SPECIAL:
2165 stats->data[i].long_name = "Special DMA";
2166 stats->data[i].rate_name = "dma/s";
2169 case _DRM_STAT_MISSED:
2170 stats->data[i].long_name = "Miss";
2171 stats->data[i].rate_name = "Ms/s";
2174 case _DRM_STAT_VALUE:
2175 stats->data[i].long_name = "Value";
2176 stats->data[i].rate_name = "Value";
2179 case _DRM_STAT_BYTE:
2180 stats->data[i].long_name = "Bytes";
2181 stats->data[i].rate_name = "B/s";
2184 case _DRM_STAT_COUNT:
2186 stats->data[i].long_name = "Count";
2187 stats->data[i].rate_name = "Cnt/s";
2196 * Issue a set-version ioctl.
2198 * \param fd file descriptor.
2199 * \param drmCommandIndex command index
2200 * \param data source pointer of the data to be read and written.
2201 * \param size size of the data to be read and written.
2203 * \return zero on success, or a negative value on failure.
2206 * It issues a read-write ioctl given by
2207 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2209 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2212 drm_set_version_t sv;
2214 sv.drm_di_major = version->drm_di_major;
2215 sv.drm_di_minor = version->drm_di_minor;
2216 sv.drm_dd_major = version->drm_dd_major;
2217 sv.drm_dd_minor = version->drm_dd_minor;
2219 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2223 version->drm_di_major = sv.drm_di_major;
2224 version->drm_di_minor = sv.drm_di_minor;
2225 version->drm_dd_major = sv.drm_dd_major;
2226 version->drm_dd_minor = sv.drm_dd_minor;
2232 * Send a device-specific command.
2234 * \param fd file descriptor.
2235 * \param drmCommandIndex command index
2237 * \return zero on success, or a negative value on failure.
2240 * It issues a ioctl given by
2241 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2243 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2245 void *data = NULL; /* dummy */
2246 unsigned long request;
2248 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2250 if (ioctl(fd, request, data)) {
2258 * Send a device-specific read command.
2260 * \param fd file descriptor.
2261 * \param drmCommandIndex command index
2262 * \param data destination pointer of the data to be read.
2263 * \param size size of the data to be read.
2265 * \return zero on success, or a negative value on failure.
2268 * It issues a read ioctl given by
2269 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2271 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2274 unsigned long request;
2276 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2277 DRM_COMMAND_BASE + drmCommandIndex, size);
2279 if (ioctl(fd, request, data)) {
2287 * Send a device-specific write command.
2289 * \param fd file descriptor.
2290 * \param drmCommandIndex command index
2291 * \param data source pointer of the data to be written.
2292 * \param size size of the data to be written.
2294 * \return zero on success, or a negative value on failure.
2297 * It issues a write ioctl given by
2298 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2300 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2303 unsigned long request;
2305 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2306 DRM_COMMAND_BASE + drmCommandIndex, size);
2308 if (ioctl(fd, request, data)) {
2316 * Send a device-specific read-write command.
2318 * \param fd file descriptor.
2319 * \param drmCommandIndex command index
2320 * \param data source pointer of the data to be read and written.
2321 * \param size size of the data to be read and written.
2323 * \return zero on success, or a negative value on failure.
2326 * It issues a read-write ioctl given by
2327 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2329 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2332 unsigned long request;
2334 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2335 DRM_COMMAND_BASE + drmCommandIndex, size);
2337 if (ioctl(fd, request, data)) {
2346 * DRM_FENCE_FLAG_EMIT
2347 * DRM_FENCE_FLAG_SHAREABLE
2348 * DRM_FENCE_MASK_DRIVER
2351 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2354 drm_fence_arg_t arg;
2356 memset(&arg, 0, sizeof(arg));
2359 arg.fence_class = fence_class;
2361 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2363 fence->handle = arg.handle;
2364 fence->fence_class = arg.fence_class;
2365 fence->type = arg.type;
2366 fence->flags = arg.flags;
2367 fence->signaled = 0;
2373 * DRM_FENCE_FLAG_SHAREABLE
2374 * DRM_FENCE_MASK_DRIVER
2377 int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence)
2379 drm_fence_arg_t arg;
2381 memset(&arg, 0, sizeof(arg));
2383 arg.fence_class = fence_class;
2385 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2387 fence->handle = arg.handle;
2388 fence->fence_class = arg.fence_class;
2389 fence->type = arg.type;
2390 fence->flags = arg.flags;
2391 fence->sequence = arg.sequence;
2392 fence->signaled = 0;
2396 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2398 drm_fence_arg_t arg;
2400 memset(&arg, 0, sizeof(arg));
2401 arg.handle = handle;
2403 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2405 fence->handle = arg.handle;
2406 fence->fence_class = arg.fence_class;
2407 fence->type = arg.type;
2408 fence->flags = arg.flags;
2409 fence->signaled = arg.signaled;
2413 int drmFenceUnreference(int fd, const drmFence *fence)
2415 drm_fence_arg_t arg;
2417 memset(&arg, 0, sizeof(arg));
2418 arg.handle = fence->handle;
2420 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2425 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2427 drm_fence_arg_t arg;
2429 memset(&arg, 0, sizeof(arg));
2430 arg.handle = fence->handle;
2431 arg.type = flush_type;
2433 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2435 fence->fence_class = arg.fence_class;
2436 fence->type = arg.type;
2437 fence->signaled = arg.signaled;
2441 int drmFenceUpdate(int fd, drmFence *fence)
2443 drm_fence_arg_t arg;
2445 memset(&arg, 0, sizeof(arg));
2446 arg.handle = fence->handle;
2448 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2450 fence->fence_class = arg.fence_class;
2451 fence->type = arg.type;
2452 fence->signaled = arg.signaled;
2456 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2459 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2460 ((fenceType & fence->signaled) != fenceType)) {
2461 int ret = drmFenceFlush(fd, fence, fenceType);
2466 *signaled = ((fenceType & fence->signaled) == fenceType);
2473 * DRM_FENCE_FLAG_SHAREABLE
2474 * DRM_FENCE_MASK_DRIVER
2478 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2480 drm_fence_arg_t arg;
2482 memset(&arg, 0, sizeof(arg));
2483 arg.fence_class = fence->fence_class;
2485 arg.handle = fence->handle;
2486 arg.type = emit_type;
2488 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2490 fence->fence_class = arg.fence_class;
2491 fence->type = arg.type;
2492 fence->signaled = arg.signaled;
2493 fence->sequence = arg.sequence;
2499 * DRM_FENCE_FLAG_WAIT_LAZY
2500 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2503 #define DRM_IOCTL_TIMEOUT_USEC 3000000UL
2505 static unsigned long
2506 drmTimeDiff(struct timeval *now, struct timeval *then)
2510 val = now->tv_sec - then->tv_sec;
2512 val += now->tv_usec;
2513 val -= then->tv_usec;
2515 return (unsigned long) val;
2519 drmIoctlTimeout(int fd, unsigned long request, void *argp)
2522 struct timeval then, now;
2526 ret = ioctl(fd, request, argp);
2527 if (ret != 0 && errno == EAGAIN) {
2529 gettimeofday(&then, NULL);
2532 gettimeofday(&now, NULL);
2534 } while (ret != 0 && errno == EAGAIN &&
2535 drmTimeDiff(&now, &then) < DRM_IOCTL_TIMEOUT_USEC);
2538 return ((errno == EAGAIN) ? -EBUSY : -errno);
2546 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2548 drm_fence_arg_t arg;
2551 if (flush_type == 0) {
2552 flush_type = fence->type;
2555 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2556 if ((flush_type & fence->signaled) == flush_type) {
2561 memset(&arg, 0, sizeof(arg));
2562 arg.handle = fence->handle;
2563 arg.type = flush_type;
2567 ret = drmIoctlTimeout(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2571 fence->fence_class = arg.fence_class;
2572 fence->type = arg.type;
2573 fence->signaled = arg.signaled;
2577 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2579 buf->handle = rep->handle;
2580 buf->flags = rep->flags;
2581 buf->size = rep->size;
2582 buf->offset = rep->offset;
2583 buf->mapHandle = rep->arg_handle;
2584 buf->mask = rep->mask;
2585 buf->start = rep->buffer_start;
2586 buf->fenceFlags = rep->fence_flags;
2587 buf->replyFlags = rep->rep_flags;
2588 buf->pageAlignment = rep->page_alignment;
2589 buf->tileInfo = rep->tile_info;
2590 buf->hwTileStride = rep->hw_tile_stride;
2591 buf->desiredTileStride = rep->desired_tile_stride;
2596 int drmBOCreate(int fd, unsigned long size,
2597 unsigned pageAlignment, void *user_buffer,
2599 unsigned hint, drmBO *buf)
2601 struct drm_bo_create_arg arg;
2602 struct drm_bo_create_req *req = &arg.d.req;
2603 struct drm_bo_info_rep *rep = &arg.d.rep;
2606 memset(buf, 0, sizeof(*buf));
2607 memset(&arg, 0, sizeof(arg));
2611 req->page_alignment = pageAlignment;
2612 req->buffer_start = (unsigned long) user_buffer;
2614 buf->virtual = NULL;
2616 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_CREATE, &arg);
2620 drmBOCopyReply(rep, buf);
2621 buf->virtual = user_buffer;
2627 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2629 struct drm_bo_reference_info_arg arg;
2630 struct drm_bo_handle_arg *req = &arg.d.req;
2631 struct drm_bo_info_rep *rep = &arg.d.rep;
2633 memset(&arg, 0, sizeof(arg));
2634 req->handle = handle;
2636 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2639 drmBOCopyReply(rep, buf);
2640 buf->mapVirtual = NULL;
2642 buf->virtual = NULL;
2647 int drmBOUnreference(int fd, drmBO *buf)
2649 struct drm_bo_handle_arg arg;
2651 if (buf->mapVirtual && buf->mapHandle) {
2652 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2653 buf->mapVirtual = NULL;
2654 buf->virtual = NULL;
2657 memset(&arg, 0, sizeof(arg));
2658 arg.handle = buf->handle;
2660 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2669 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2670 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2671 * call return an -EBUSY if it can' immediately honor the mapping request.
2674 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2677 struct drm_bo_map_wait_idle_arg arg;
2678 struct drm_bo_info_req *req = &arg.d.req;
2679 struct drm_bo_info_rep *rep = &arg.d.rep;
2683 * Make sure we have a virtual address of the buffer.
2686 if (!buf->virtual) {
2688 virtual = mmap(0, buf->size + buf->start,
2689 PROT_READ | PROT_WRITE, MAP_SHARED,
2690 fd, buf->mapHandle);
2691 if (virtual == MAP_FAILED) {
2696 buf->mapVirtual = virtual;
2697 buf->virtual = ((char *) virtual) + buf->start;
2700 memset(&arg, 0, sizeof(arg));
2701 req->handle = buf->handle;
2702 req->mask = mapFlags;
2703 req->hint = mapHint;
2706 * May hang if the buffer object is busy.
2707 * This IOCTL synchronizes the buffer.
2710 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_MAP, &arg);
2714 drmBOCopyReply(rep, buf);
2715 buf->mapFlags = mapFlags;
2717 *address = buf->virtual;
2723 int drmBOUnmap(int fd, drmBO *buf)
2725 struct drm_bo_handle_arg arg;
2727 memset(&arg, 0, sizeof(arg));
2728 arg.handle = buf->handle;
2730 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2737 int drmBOSetStatus(int fd, drmBO *buf,
2738 uint64_t flags, uint64_t mask,
2740 unsigned int desired_tile_stride,
2741 unsigned int tile_info)
2744 struct drm_bo_map_wait_idle_arg arg;
2745 struct drm_bo_info_req *req = &arg.d.req;
2746 struct drm_bo_info_rep *rep = &arg.d.rep;
2749 memset(&arg, 0, sizeof(arg));
2752 req->handle = buf->handle;
2754 req->desired_tile_stride = desired_tile_stride;
2755 req->tile_info = tile_info;
2757 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_SETSTATUS, &arg);
2761 drmBOCopyReply(rep, buf);
2766 int drmBOInfo(int fd, drmBO *buf)
2768 struct drm_bo_reference_info_arg arg;
2769 struct drm_bo_handle_arg *req = &arg.d.req;
2770 struct drm_bo_info_rep *rep = &arg.d.rep;
2773 memset(&arg, 0, sizeof(arg));
2774 req->handle = buf->handle;
2776 ret = ioctl(fd, DRM_IOCTL_BO_INFO, &arg);
2780 drmBOCopyReply(rep, buf);
2784 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2786 struct drm_bo_map_wait_idle_arg arg;
2787 struct drm_bo_info_req *req = &arg.d.req;
2788 struct drm_bo_info_rep *rep = &arg.d.rep;
2791 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2792 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2793 memset(&arg, 0, sizeof(arg));
2794 req->handle = buf->handle;
2797 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2801 drmBOCopyReply(rep, buf);
2806 int drmBOBusy(int fd, drmBO *buf, int *busy)
2808 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2809 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2814 int ret = drmBOInfo(fd, buf);
2817 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2822 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
2825 struct drm_mm_init_arg arg;
2827 memset(&arg, 0, sizeof(arg));
2829 arg.magic = DRM_BO_INIT_MAGIC;
2830 arg.major = DRM_BO_INIT_MAJOR;
2831 arg.minor = DRM_BO_INIT_MINOR;
2832 arg.p_offset = pOffset;
2834 arg.mem_type = memType;
2836 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
2841 int drmMMTakedown(int fd, unsigned memType)
2843 struct drm_mm_type_arg arg;
2845 memset(&arg, 0, sizeof(arg));
2846 arg.mem_type = memType;
2848 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
2854 * If this function returns an error, and lockBM was set to 1,
2855 * the buffer manager is NOT locked.
2858 int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict)
2860 struct drm_mm_type_arg arg;
2862 memset(&arg, 0, sizeof(arg));
2863 arg.mem_type = memType;
2864 arg.lock_flags |= (lockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2865 arg.lock_flags |= (ignoreNoEvict) ? DRM_BO_LOCK_IGNORE_NO_EVICT : 0;
2867 return drmIoctlTimeout(fd, DRM_IOCTL_MM_LOCK, &arg);
2870 int drmMMUnlock(int fd, unsigned memType, int unlockBM)
2872 struct drm_mm_type_arg arg;
2874 memset(&arg, 0, sizeof(arg));
2876 arg.mem_type = memType;
2877 arg.lock_flags |= (unlockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2879 return drmIoctlTimeout(fd, DRM_IOCTL_MM_UNLOCK, &arg);
2882 int drmBOVersion(int fd, unsigned int *major,
2883 unsigned int *minor,
2884 unsigned int *patchlevel)
2886 struct drm_bo_version_arg arg;
2889 memset(&arg, 0, sizeof(arg));
2890 ret = ioctl(fd, DRM_IOCTL_BO_VERSION, &arg);
2899 *patchlevel = arg.patchlevel;
2906 #define DRM_MAX_FDS 16
2911 } connection[DRM_MAX_FDS];
2913 static int nr_fds = 0;
2915 int drmOpenOnce(void *unused,
2922 for (i = 0; i < nr_fds; i++)
2923 if (strcmp(BusID, connection[i].BusID) == 0) {
2924 connection[i].refcount++;
2926 return connection[i].fd;
2929 fd = drmOpen(unused, BusID);
2930 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2933 connection[nr_fds].BusID = strdup(BusID);
2934 connection[nr_fds].fd = fd;
2935 connection[nr_fds].refcount = 1;
2939 fprintf(stderr, "saved connection %d for %s %d\n",
2940 nr_fds, connection[nr_fds].BusID,
2941 strcmp(BusID, connection[nr_fds].BusID));
2948 void drmCloseOnce(int fd)
2952 for (i = 0; i < nr_fds; i++) {
2953 if (fd == connection[i].fd) {
2954 if (--connection[i].refcount == 0) {
2955 drmClose(connection[i].fd);
2956 free(connection[i].BusID);
2959 connection[i] = connection[nr_fds];