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__) || defined(__DragonFly__)
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);
582 int drmOpenControl(int minor)
584 return drmOpenMinor(minor, 0, DRM_NODE_CONTROL);
588 * Free the version information returned by drmGetVersion().
590 * \param v pointer to the version information.
593 * It frees the memory pointed by \p %v as well as all the non-null strings
596 void drmFreeVersion(drmVersionPtr v)
608 * Free the non-public version information returned by the kernel.
610 * \param v pointer to the version information.
613 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
614 * the non-null strings pointers in it.
616 static void drmFreeKernelVersion(drm_version_t *v)
628 * Copy version information.
630 * \param d destination pointer.
631 * \param s source pointer.
634 * Used by drmGetVersion() to translate the information returned by the ioctl
635 * interface in a private structure into the public structure counterpart.
637 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
639 d->version_major = s->version_major;
640 d->version_minor = s->version_minor;
641 d->version_patchlevel = s->version_patchlevel;
642 d->name_len = s->name_len;
643 d->name = drmStrdup(s->name);
644 d->date_len = s->date_len;
645 d->date = drmStrdup(s->date);
646 d->desc_len = s->desc_len;
647 d->desc = drmStrdup(s->desc);
652 * Query the driver version information.
654 * \param fd file descriptor.
656 * \return pointer to a drmVersion structure which should be freed with
659 * \note Similar information is available via /proc/dri.
662 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
663 * first with zeros to get the string lengths, and then the actually strings.
664 * It also null-terminates them since they might not be already.
666 drmVersionPtr drmGetVersion(int fd)
668 drmVersionPtr retval;
669 drm_version_t *version = drmMalloc(sizeof(*version));
671 version->name_len = 0;
672 version->name = NULL;
673 version->date_len = 0;
674 version->date = NULL;
675 version->desc_len = 0;
676 version->desc = NULL;
678 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
679 drmFreeKernelVersion(version);
683 if (version->name_len)
684 version->name = drmMalloc(version->name_len + 1);
685 if (version->date_len)
686 version->date = drmMalloc(version->date_len + 1);
687 if (version->desc_len)
688 version->desc = drmMalloc(version->desc_len + 1);
690 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
691 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
692 drmFreeKernelVersion(version);
696 /* The results might not be null-terminated strings, so terminate them. */
697 if (version->name_len) version->name[version->name_len] = '\0';
698 if (version->date_len) version->date[version->date_len] = '\0';
699 if (version->desc_len) version->desc[version->desc_len] = '\0';
701 retval = drmMalloc(sizeof(*retval));
702 drmCopyVersion(retval, version);
703 drmFreeKernelVersion(version);
709 * Get version information for the DRM user space library.
711 * This version number is driver independent.
713 * \param fd file descriptor.
715 * \return version information.
718 * This function allocates and fills a drm_version structure with a hard coded
721 drmVersionPtr drmGetLibVersion(int fd)
723 drm_version_t *version = drmMalloc(sizeof(*version));
726 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
727 * revision 1.0.x = original DRM interface with no drmGetLibVersion
728 * entry point and many drm<Device> extensions
729 * revision 1.1.x = added drmCommand entry points for device extensions
730 * added drmGetLibVersion to identify libdrm.a version
731 * revision 1.2.x = added drmSetInterfaceVersion
732 * modified drmOpen to handle both busid and name
733 * revision 1.3.x = added server + memory manager
735 version->version_major = 1;
736 version->version_minor = 3;
737 version->version_patchlevel = 0;
739 return (drmVersionPtr)version;
744 * Free the bus ID information.
746 * \param busid bus ID information string as given by drmGetBusid().
749 * This function is just frees the memory pointed by \p busid.
751 void drmFreeBusid(const char *busid)
753 drmFree((void *)busid);
758 * Get the bus ID of the device.
760 * \param fd file descriptor.
762 * \return bus ID string.
765 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
766 * get the string length and data, passing the arguments in a drm_unique
769 char *drmGetBusid(int fd)
776 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
778 u.unique = drmMalloc(u.unique_len + 1);
779 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
781 u.unique[u.unique_len] = '\0';
788 * Set the bus ID of the device.
790 * \param fd file descriptor.
791 * \param busid bus ID string.
793 * \return zero on success, negative on failure.
796 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
797 * the arguments in a drm_unique structure.
799 int drmSetBusid(int fd, const char *busid)
803 u.unique = (char *)busid;
804 u.unique_len = strlen(busid);
806 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
812 int drmGetMagic(int fd, drm_magic_t * magic)
817 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
823 int drmAuthMagic(int fd, drm_magic_t magic)
828 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
834 * Specifies a range of memory that is available for mapping by a
837 * \param fd file descriptor.
838 * \param offset usually the physical address. The actual meaning depends of
839 * the \p type parameter. See below.
840 * \param size of the memory in bytes.
841 * \param type type of the memory to be mapped.
842 * \param flags combination of several flags to modify the function actions.
843 * \param handle will be set to a value that may be used as the offset
844 * parameter for mmap().
846 * \return zero on success or a negative value on error.
848 * \par Mapping the frame buffer
849 * For the frame buffer
850 * - \p offset will be the physical address of the start of the frame buffer,
851 * - \p size will be the size of the frame buffer in bytes, and
852 * - \p type will be DRM_FRAME_BUFFER.
855 * The area mapped will be uncached. If MTRR support is available in the
856 * kernel, the frame buffer area will be set to write combining.
858 * \par Mapping the MMIO register area
859 * For the MMIO register area,
860 * - \p offset will be the physical address of the start of the register area,
861 * - \p size will be the size of the register area bytes, and
862 * - \p type will be DRM_REGISTERS.
864 * The area mapped will be uncached.
866 * \par Mapping the SAREA
868 * - \p offset will be ignored and should be set to zero,
869 * - \p size will be the desired size of the SAREA in bytes,
870 * - \p type will be DRM_SHM.
873 * A shared memory area of the requested size will be created and locked in
874 * kernel memory. This area may be mapped into client-space by using the handle
877 * \note May only be called by root.
880 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
881 * the arguments in a drm_map structure.
883 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
884 drmMapFlags flags, drm_handle_t *handle)
893 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map))
896 *handle = (drm_handle_t)map.handle;
900 int drmRmMap(int fd, drm_handle_t handle)
904 map.handle = (void *)handle;
906 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map))
912 * Make buffers available for DMA transfers.
914 * \param fd file descriptor.
915 * \param count number of buffers.
916 * \param size size of each buffer.
917 * \param flags buffer allocation flags.
918 * \param agp_offset offset in the AGP aperture
920 * \return number of buffers allocated, negative on error.
923 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
927 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
930 drm_buf_desc_t request;
932 request.count = count;
934 request.low_mark = 0;
935 request.high_mark = 0;
936 request.flags = flags;
937 request.agp_start = agp_offset;
939 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request))
941 return request.count;
944 int drmMarkBufs(int fd, double low, double high)
952 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
958 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
961 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
967 for (i = 0; i < info.count; i++) {
968 info.list[i].low_mark = low * info.list[i].count;
969 info.list[i].high_mark = high * info.list[i].count;
970 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
984 * \param fd file descriptor.
985 * \param count number of buffers to free.
986 * \param list list of buffers to be freed.
988 * \return zero on success, or a negative value on failure.
990 * \note This function is primarily used for debugging.
993 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
994 * the arguments in a drm_buf_free structure.
996 int drmFreeBufs(int fd, int count, int *list)
998 drm_buf_free_t request;
1000 request.count = count;
1001 request.list = list;
1002 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1011 * \param fd file descriptor.
1014 * This function closes the file descriptor.
1016 int drmClose(int fd)
1018 unsigned long key = drmGetKeyFromFd(fd);
1019 drmHashEntry *entry = drmGetEntry(fd);
1021 drmHashDestroy(entry->tagTable);
1024 entry->tagTable = NULL;
1026 drmHashDelete(drmHashTable, key);
1034 * Map a region of memory.
1036 * \param fd file descriptor.
1037 * \param handle handle returned by drmAddMap().
1038 * \param size size in bytes. Must match the size used by drmAddMap().
1039 * \param address will contain the user-space virtual address where the mapping
1042 * \return zero on success, or a negative value on failure.
1045 * This function is a wrapper for mmap().
1047 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1049 static unsigned long pagesize_mask = 0;
1055 pagesize_mask = getpagesize() - 1;
1057 size = (size + pagesize_mask) & ~pagesize_mask;
1059 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1060 if (*address == MAP_FAILED)
1067 * Unmap mappings obtained with drmMap().
1069 * \param address address as given by drmMap().
1070 * \param size size in bytes. Must match the size used by drmMap().
1072 * \return zero on success, or a negative value on failure.
1075 * This function is a wrapper for munmap().
1077 int drmUnmap(drmAddress address, drmSize size)
1079 return munmap(address, size);
1082 drmBufInfoPtr drmGetBufInfo(int fd)
1084 drm_buf_info_t info;
1085 drmBufInfoPtr retval;
1091 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1095 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1098 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1103 retval = drmMalloc(sizeof(*retval));
1104 retval->count = info.count;
1105 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1106 for (i = 0; i < info.count; i++) {
1107 retval->list[i].count = info.list[i].count;
1108 retval->list[i].size = info.list[i].size;
1109 retval->list[i].low_mark = info.list[i].low_mark;
1110 retval->list[i].high_mark = info.list[i].high_mark;
1119 * Map all DMA buffers into client-virtual space.
1121 * \param fd file descriptor.
1123 * \return a pointer to a ::drmBufMap structure.
1125 * \note The client may not use these buffers until obtaining buffer indices
1129 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1130 * information about the buffers in a drm_buf_map structure into the
1131 * client-visible data structures.
1133 drmBufMapPtr drmMapBufs(int fd)
1136 drmBufMapPtr retval;
1141 bufs.virtual = NULL;
1142 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1148 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1151 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1156 retval = drmMalloc(sizeof(*retval));
1157 retval->count = bufs.count;
1158 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1159 for (i = 0; i < bufs.count; i++) {
1160 retval->list[i].idx = bufs.list[i].idx;
1161 retval->list[i].total = bufs.list[i].total;
1162 retval->list[i].used = 0;
1163 retval->list[i].address = bufs.list[i].address;
1173 * Unmap buffers allocated with drmMapBufs().
1175 * \return zero on success, or negative value on failure.
1178 * Calls munmap() for every buffer stored in \p bufs and frees the
1179 * memory allocated by drmMapBufs().
1181 int drmUnmapBufs(drmBufMapPtr bufs)
1185 for (i = 0; i < bufs->count; i++) {
1186 munmap(bufs->list[i].address, bufs->list[i].total);
1189 drmFree(bufs->list);
1196 #define DRM_DMA_RETRY 16
1199 * Reserve DMA buffers.
1201 * \param fd file descriptor.
1204 * \return zero on success, or a negative value on failure.
1207 * Assemble the arguments into a drm_dma structure and keeps issuing the
1208 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1210 int drmDMA(int fd, drmDMAReqPtr request)
1215 dma.context = request->context;
1216 dma.send_count = request->send_count;
1217 dma.send_indices = request->send_list;
1218 dma.send_sizes = request->send_sizes;
1219 dma.flags = request->flags;
1220 dma.request_count = request->request_count;
1221 dma.request_size = request->request_size;
1222 dma.request_indices = request->request_list;
1223 dma.request_sizes = request->request_sizes;
1224 dma.granted_count = 0;
1227 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1228 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1231 request->granted_count = dma.granted_count;
1240 * Obtain heavyweight hardware lock.
1242 * \param fd file descriptor.
1243 * \param context context.
1244 * \param flags flags that determine the sate of the hardware when the function
1247 * \return always zero.
1250 * This function translates the arguments into a drm_lock structure and issue
1251 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1253 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1257 lock.context = context;
1259 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1260 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1261 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1262 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1263 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1264 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1266 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1272 * Release the hardware lock.
1274 * \param fd file descriptor.
1275 * \param context context.
1277 * \return zero on success, or a negative value on failure.
1280 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1281 * argument in a drm_lock structure.
1283 int drmUnlock(int fd, drm_context_t context)
1287 lock.context = context;
1289 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1292 drm_context_t *drmGetReservedContextList(int fd, int *count)
1296 drm_context_t * retval;
1300 res.contexts = NULL;
1301 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1307 if (!(list = drmMalloc(res.count * sizeof(*list))))
1309 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1314 res.contexts = list;
1315 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res))
1318 for (i = 0; i < res.count; i++)
1319 retval[i] = list[i].handle;
1326 void drmFreeReservedContextList(drm_context_t *pt)
1334 * Used by the X server during GLXContext initialization. This causes
1335 * per-context kernel-level resources to be allocated.
1337 * \param fd file descriptor.
1338 * \param handle is set on success. To be used by the client when requesting DMA
1339 * dispatch with drmDMA().
1341 * \return zero on success, or a negative value on failure.
1343 * \note May only be called by root.
1346 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1347 * argument in a drm_ctx structure.
1349 int drmCreateContext(int fd, drm_context_t *handle)
1353 ctx.flags = 0; /* Modified with functions below */
1354 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1356 *handle = ctx.handle;
1360 int drmSwitchToContext(int fd, drm_context_t context)
1364 ctx.handle = context;
1365 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1370 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1375 * Context preserving means that no context switches are done between DMA
1376 * buffers from one context and the next. This is suitable for use in the
1377 * X server (which promises to maintain hardware context), or in the
1378 * client-side library when buffers are swapped on behalf of two threads.
1380 ctx.handle = context;
1382 if (flags & DRM_CONTEXT_PRESERVED)
1383 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1384 if (flags & DRM_CONTEXT_2DONLY)
1385 ctx.flags |= _DRM_CONTEXT_2DONLY;
1386 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1391 int drmGetContextFlags(int fd, drm_context_t context,
1392 drm_context_tFlagsPtr flags)
1396 ctx.handle = context;
1397 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1400 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1401 *flags |= DRM_CONTEXT_PRESERVED;
1402 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1403 *flags |= DRM_CONTEXT_2DONLY;
1410 * Free any kernel-level resources allocated with drmCreateContext() associated
1413 * \param fd file descriptor.
1414 * \param handle handle given by drmCreateContext().
1416 * \return zero on success, or a negative value on failure.
1418 * \note May only be called by root.
1421 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1422 * argument in a drm_ctx structure.
1424 int drmDestroyContext(int fd, drm_context_t handle)
1427 ctx.handle = handle;
1428 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1433 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1436 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1438 *handle = draw.handle;
1442 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1445 draw.handle = handle;
1446 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1451 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1452 drm_drawable_info_type_t type, unsigned int num,
1455 drm_update_draw_t update;
1457 update.handle = handle;
1460 update.data = (unsigned long long)(unsigned long)data;
1462 if (ioctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1469 * Acquire the AGP device.
1471 * Must be called before any of the other AGP related calls.
1473 * \param fd file descriptor.
1475 * \return zero on success, or a negative value on failure.
1478 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1480 int drmAgpAcquire(int fd)
1482 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1489 * Release the AGP device.
1491 * \param fd file descriptor.
1493 * \return zero on success, or a negative value on failure.
1496 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1498 int drmAgpRelease(int fd)
1500 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1509 * \param fd file descriptor.
1510 * \param mode AGP mode.
1512 * \return zero on success, or a negative value on failure.
1515 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1516 * argument in a drm_agp_mode structure.
1518 int drmAgpEnable(int fd, unsigned long mode)
1523 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1530 * Allocate a chunk of AGP memory.
1532 * \param fd file descriptor.
1533 * \param size requested memory size in bytes. Will be rounded to page boundary.
1534 * \param type type of memory to allocate.
1535 * \param address if not zero, will be set to the physical address of the
1537 * \param handle on success will be set to a handle of the allocated memory.
1539 * \return zero on success, or a negative value on failure.
1542 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1543 * arguments in a drm_agp_buffer structure.
1545 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1546 unsigned long *address, drm_handle_t *handle)
1550 *handle = DRM_AGP_NO_HANDLE;
1554 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1557 *address = b.physical;
1564 * Free a chunk of AGP memory.
1566 * \param fd file descriptor.
1567 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1569 * \return zero on success, or a negative value on failure.
1572 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1573 * argument in a drm_agp_buffer structure.
1575 int drmAgpFree(int fd, drm_handle_t handle)
1581 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b))
1588 * Bind a chunk of AGP memory.
1590 * \param fd file descriptor.
1591 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1592 * \param offset offset in bytes. It will round to page boundary.
1594 * \return zero on success, or a negative value on failure.
1597 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1598 * argument in a drm_agp_binding structure.
1600 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1602 drm_agp_binding_t b;
1606 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b))
1613 * Unbind a chunk of AGP memory.
1615 * \param fd file descriptor.
1616 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1618 * \return zero on success, or a negative value on failure.
1621 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1622 * the argument in a drm_agp_binding structure.
1624 int drmAgpUnbind(int fd, drm_handle_t handle)
1626 drm_agp_binding_t b;
1630 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1637 * Get AGP driver major version number.
1639 * \param fd file descriptor.
1641 * \return major version number on success, or a negative value on failure..
1644 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1645 * necessary information in a drm_agp_info structure.
1647 int drmAgpVersionMajor(int fd)
1651 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1653 return i.agp_version_major;
1658 * Get AGP driver minor version number.
1660 * \param fd file descriptor.
1662 * \return minor version number on success, or a negative value on failure.
1665 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1666 * necessary information in a drm_agp_info structure.
1668 int drmAgpVersionMinor(int fd)
1672 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1674 return i.agp_version_minor;
1681 * \param fd file descriptor.
1683 * \return mode on success, or zero on failure.
1686 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1687 * necessary information in a drm_agp_info structure.
1689 unsigned long drmAgpGetMode(int fd)
1693 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1700 * Get AGP aperture base.
1702 * \param fd file descriptor.
1704 * \return aperture base on success, zero on failure.
1707 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1708 * necessary information in a drm_agp_info structure.
1710 unsigned long drmAgpBase(int fd)
1714 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1716 return i.aperture_base;
1721 * Get AGP aperture size.
1723 * \param fd file descriptor.
1725 * \return aperture size on success, zero on failure.
1728 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1729 * necessary information in a drm_agp_info structure.
1731 unsigned long drmAgpSize(int fd)
1735 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1737 return i.aperture_size;
1742 * Get used AGP memory.
1744 * \param fd file descriptor.
1746 * \return memory used on success, or zero on failure.
1749 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1750 * necessary information in a drm_agp_info structure.
1752 unsigned long drmAgpMemoryUsed(int fd)
1756 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1758 return i.memory_used;
1763 * Get available AGP memory.
1765 * \param fd file descriptor.
1767 * \return memory available on success, or zero on failure.
1770 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1771 * necessary information in a drm_agp_info structure.
1773 unsigned long drmAgpMemoryAvail(int fd)
1777 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1779 return i.memory_allowed;
1784 * Get hardware vendor ID.
1786 * \param fd file descriptor.
1788 * \return vendor ID on success, or zero on failure.
1791 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1792 * necessary information in a drm_agp_info structure.
1794 unsigned int drmAgpVendorId(int fd)
1798 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1805 * Get hardware device ID.
1807 * \param fd file descriptor.
1809 * \return zero on success, or zero on failure.
1812 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1813 * necessary information in a drm_agp_info structure.
1815 unsigned int drmAgpDeviceId(int fd)
1819 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i))
1824 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1826 drm_scatter_gather_t sg;
1831 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1833 *handle = sg.handle;
1837 int drmScatterGatherFree(int fd, drm_handle_t handle)
1839 drm_scatter_gather_t sg;
1843 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg))
1851 * \param fd file descriptor.
1852 * \param vbl pointer to a drmVBlank structure.
1854 * \return zero on success, or a negative value on failure.
1857 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1859 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1864 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1865 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1866 } while (ret && errno == EINTR);
1871 int drmError(int err, const char *label)
1874 case DRM_ERR_NO_DEVICE:
1875 fprintf(stderr, "%s: no device\n", label);
1877 case DRM_ERR_NO_ACCESS:
1878 fprintf(stderr, "%s: no access\n", label);
1880 case DRM_ERR_NOT_ROOT:
1881 fprintf(stderr, "%s: not root\n", label);
1883 case DRM_ERR_INVALID:
1884 fprintf(stderr, "%s: invalid args\n", label);
1889 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1897 * Install IRQ handler.
1899 * \param fd file descriptor.
1900 * \param irq IRQ number.
1902 * \return zero on success, or a negative value on failure.
1905 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1906 * argument in a drm_control structure.
1908 int drmCtlInstHandler(int fd, int irq)
1912 ctl.func = DRM_INST_HANDLER;
1914 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1921 * Uninstall IRQ handler.
1923 * \param fd file descriptor.
1925 * \return zero on success, or a negative value on failure.
1928 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1929 * argument in a drm_control structure.
1931 int drmCtlUninstHandler(int fd)
1935 ctl.func = DRM_UNINST_HANDLER;
1937 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl))
1942 int drmFinish(int fd, int context, drmLockFlags flags)
1946 lock.context = context;
1948 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1949 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1950 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1951 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1952 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1953 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1954 if (ioctl(fd, DRM_IOCTL_FINISH, &lock))
1960 * Get IRQ from bus ID.
1962 * \param fd file descriptor.
1963 * \param busnum bus number.
1964 * \param devnum device number.
1965 * \param funcnum function number.
1967 * \return IRQ number on success, or a negative value on failure.
1970 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1971 * arguments in a drm_irq_busid structure.
1973 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1979 p.funcnum = funcnum;
1980 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1985 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1987 drmHashEntry *entry = drmGetEntry(fd);
1989 if (drmHashInsert(entry->tagTable, context, tag)) {
1990 drmHashDelete(entry->tagTable, context);
1991 drmHashInsert(entry->tagTable, context, tag);
1996 int drmDelContextTag(int fd, drm_context_t context)
1998 drmHashEntry *entry = drmGetEntry(fd);
2000 return drmHashDelete(entry->tagTable, context);
2003 void *drmGetContextTag(int fd, drm_context_t context)
2005 drmHashEntry *entry = drmGetEntry(fd);
2008 if (drmHashLookup(entry->tagTable, context, &value))
2014 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2015 drm_handle_t handle)
2017 drm_ctx_priv_map_t map;
2019 map.ctx_id = ctx_id;
2020 map.handle = (void *)handle;
2022 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2027 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2028 drm_handle_t *handle)
2030 drm_ctx_priv_map_t map;
2032 map.ctx_id = ctx_id;
2034 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2037 *handle = (drm_handle_t)map.handle;
2042 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2043 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2049 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map))
2051 *offset = map.offset;
2055 *handle = (unsigned long)map.handle;
2060 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2061 unsigned long *magic, unsigned long *iocs)
2063 drm_client_t client;
2066 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2068 *auth = client.auth;
2071 *magic = client.magic;
2072 *iocs = client.iocs;
2076 int drmGetStats(int fd, drmStatsT *stats)
2081 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s))
2085 memset(stats, 0, sizeof(*stats));
2086 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2090 stats->data[i].long_format = "%-20.20s"; \
2091 stats->data[i].rate_format = "%8.8s"; \
2092 stats->data[i].isvalue = 1; \
2093 stats->data[i].verbose = 0
2096 stats->data[i].long_format = "%-20.20s"; \
2097 stats->data[i].rate_format = "%5.5s"; \
2098 stats->data[i].isvalue = 0; \
2099 stats->data[i].mult_names = "kgm"; \
2100 stats->data[i].mult = 1000; \
2101 stats->data[i].verbose = 0
2104 stats->data[i].long_format = "%-20.20s"; \
2105 stats->data[i].rate_format = "%5.5s"; \
2106 stats->data[i].isvalue = 0; \
2107 stats->data[i].mult_names = "KGM"; \
2108 stats->data[i].mult = 1024; \
2109 stats->data[i].verbose = 0
2112 stats->count = s.count;
2113 for (i = 0; i < s.count; i++) {
2114 stats->data[i].value = s.data[i].value;
2115 switch (s.data[i].type) {
2116 case _DRM_STAT_LOCK:
2117 stats->data[i].long_name = "Lock";
2118 stats->data[i].rate_name = "Lock";
2121 case _DRM_STAT_OPENS:
2122 stats->data[i].long_name = "Opens";
2123 stats->data[i].rate_name = "O";
2125 stats->data[i].verbose = 1;
2127 case _DRM_STAT_CLOSES:
2128 stats->data[i].long_name = "Closes";
2129 stats->data[i].rate_name = "Lock";
2131 stats->data[i].verbose = 1;
2133 case _DRM_STAT_IOCTLS:
2134 stats->data[i].long_name = "Ioctls";
2135 stats->data[i].rate_name = "Ioc/s";
2138 case _DRM_STAT_LOCKS:
2139 stats->data[i].long_name = "Locks";
2140 stats->data[i].rate_name = "Lck/s";
2143 case _DRM_STAT_UNLOCKS:
2144 stats->data[i].long_name = "Unlocks";
2145 stats->data[i].rate_name = "Unl/s";
2149 stats->data[i].long_name = "IRQs";
2150 stats->data[i].rate_name = "IRQ/s";
2153 case _DRM_STAT_PRIMARY:
2154 stats->data[i].long_name = "Primary Bytes";
2155 stats->data[i].rate_name = "PB/s";
2158 case _DRM_STAT_SECONDARY:
2159 stats->data[i].long_name = "Secondary Bytes";
2160 stats->data[i].rate_name = "SB/s";
2164 stats->data[i].long_name = "DMA";
2165 stats->data[i].rate_name = "DMA/s";
2168 case _DRM_STAT_SPECIAL:
2169 stats->data[i].long_name = "Special DMA";
2170 stats->data[i].rate_name = "dma/s";
2173 case _DRM_STAT_MISSED:
2174 stats->data[i].long_name = "Miss";
2175 stats->data[i].rate_name = "Ms/s";
2178 case _DRM_STAT_VALUE:
2179 stats->data[i].long_name = "Value";
2180 stats->data[i].rate_name = "Value";
2183 case _DRM_STAT_BYTE:
2184 stats->data[i].long_name = "Bytes";
2185 stats->data[i].rate_name = "B/s";
2188 case _DRM_STAT_COUNT:
2190 stats->data[i].long_name = "Count";
2191 stats->data[i].rate_name = "Cnt/s";
2200 * Issue a set-version ioctl.
2202 * \param fd file descriptor.
2203 * \param drmCommandIndex command index
2204 * \param data source pointer of the data to be read and written.
2205 * \param size size of the data to be read and written.
2207 * \return zero on success, or a negative value on failure.
2210 * It issues a read-write ioctl given by
2211 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2213 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2216 drm_set_version_t sv;
2218 sv.drm_di_major = version->drm_di_major;
2219 sv.drm_di_minor = version->drm_di_minor;
2220 sv.drm_dd_major = version->drm_dd_major;
2221 sv.drm_dd_minor = version->drm_dd_minor;
2223 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2227 version->drm_di_major = sv.drm_di_major;
2228 version->drm_di_minor = sv.drm_di_minor;
2229 version->drm_dd_major = sv.drm_dd_major;
2230 version->drm_dd_minor = sv.drm_dd_minor;
2236 * Send a device-specific command.
2238 * \param fd file descriptor.
2239 * \param drmCommandIndex command index
2241 * \return zero on success, or a negative value on failure.
2244 * It issues a ioctl given by
2245 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2247 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2249 void *data = NULL; /* dummy */
2250 unsigned long request;
2252 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2254 if (ioctl(fd, request, data)) {
2262 * Send a device-specific read command.
2264 * \param fd file descriptor.
2265 * \param drmCommandIndex command index
2266 * \param data destination pointer of the data to be read.
2267 * \param size size of the data to be read.
2269 * \return zero on success, or a negative value on failure.
2272 * It issues a read ioctl given by
2273 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2275 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2278 unsigned long request;
2280 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2281 DRM_COMMAND_BASE + drmCommandIndex, size);
2283 if (ioctl(fd, request, data)) {
2291 * Send a device-specific write command.
2293 * \param fd file descriptor.
2294 * \param drmCommandIndex command index
2295 * \param data source pointer of the data to be written.
2296 * \param size size of the data to be written.
2298 * \return zero on success, or a negative value on failure.
2301 * It issues a write ioctl given by
2302 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2304 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2307 unsigned long request;
2309 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2310 DRM_COMMAND_BASE + drmCommandIndex, size);
2312 if (ioctl(fd, request, data)) {
2320 * Send a device-specific read-write command.
2322 * \param fd file descriptor.
2323 * \param drmCommandIndex command index
2324 * \param data source pointer of the data to be read and written.
2325 * \param size size of the data to be read and written.
2327 * \return zero on success, or a negative value on failure.
2330 * It issues a read-write ioctl given by
2331 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2333 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2336 unsigned long request;
2338 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2339 DRM_COMMAND_BASE + drmCommandIndex, size);
2341 if (ioctl(fd, request, data)) {
2350 * DRM_FENCE_FLAG_EMIT
2351 * DRM_FENCE_FLAG_SHAREABLE
2352 * DRM_FENCE_MASK_DRIVER
2355 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2358 drm_fence_arg_t arg;
2360 memset(&arg, 0, sizeof(arg));
2363 arg.fence_class = fence_class;
2365 if (ioctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2367 fence->handle = arg.handle;
2368 fence->fence_class = arg.fence_class;
2369 fence->type = arg.type;
2370 fence->flags = arg.flags;
2371 fence->signaled = 0;
2377 * DRM_FENCE_FLAG_SHAREABLE
2378 * DRM_FENCE_MASK_DRIVER
2381 int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence)
2383 drm_fence_arg_t arg;
2385 memset(&arg, 0, sizeof(arg));
2387 arg.fence_class = fence_class;
2389 if (ioctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2391 fence->handle = arg.handle;
2392 fence->fence_class = arg.fence_class;
2393 fence->type = arg.type;
2394 fence->flags = arg.flags;
2395 fence->sequence = arg.sequence;
2396 fence->signaled = 0;
2400 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2402 drm_fence_arg_t arg;
2404 memset(&arg, 0, sizeof(arg));
2405 arg.handle = handle;
2407 if (ioctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2409 fence->handle = arg.handle;
2410 fence->fence_class = arg.fence_class;
2411 fence->type = arg.type;
2412 fence->flags = arg.flags;
2413 fence->signaled = arg.signaled;
2417 int drmFenceUnreference(int fd, const drmFence *fence)
2419 drm_fence_arg_t arg;
2421 memset(&arg, 0, sizeof(arg));
2422 arg.handle = fence->handle;
2424 if (ioctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2429 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2431 drm_fence_arg_t arg;
2433 memset(&arg, 0, sizeof(arg));
2434 arg.handle = fence->handle;
2435 arg.type = flush_type;
2437 if (ioctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2439 fence->fence_class = arg.fence_class;
2440 fence->type = arg.type;
2441 fence->signaled = arg.signaled;
2445 int drmFenceUpdate(int fd, drmFence *fence)
2447 drm_fence_arg_t arg;
2449 memset(&arg, 0, sizeof(arg));
2450 arg.handle = fence->handle;
2452 if (ioctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2454 fence->fence_class = arg.fence_class;
2455 fence->type = arg.type;
2456 fence->signaled = arg.signaled;
2460 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2463 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2464 ((fenceType & fence->signaled) != fenceType)) {
2465 int ret = drmFenceFlush(fd, fence, fenceType);
2470 *signaled = ((fenceType & fence->signaled) == fenceType);
2477 * DRM_FENCE_FLAG_SHAREABLE
2478 * DRM_FENCE_MASK_DRIVER
2482 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2484 drm_fence_arg_t arg;
2486 memset(&arg, 0, sizeof(arg));
2487 arg.fence_class = fence->fence_class;
2489 arg.handle = fence->handle;
2490 arg.type = emit_type;
2492 if (ioctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2494 fence->fence_class = arg.fence_class;
2495 fence->type = arg.type;
2496 fence->signaled = arg.signaled;
2497 fence->sequence = arg.sequence;
2503 * DRM_FENCE_FLAG_WAIT_LAZY
2504 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2507 #define DRM_IOCTL_TIMEOUT_USEC 3000000UL
2509 static unsigned long
2510 drmTimeDiff(struct timeval *now, struct timeval *then)
2514 val = now->tv_sec - then->tv_sec;
2516 val += now->tv_usec;
2517 val -= then->tv_usec;
2519 return (unsigned long) val;
2523 drmIoctlTimeout(int fd, unsigned long request, void *argp)
2526 struct timeval then, now;
2530 ret = ioctl(fd, request, argp);
2531 if (ret != 0 && errno == EAGAIN) {
2533 gettimeofday(&then, NULL);
2536 gettimeofday(&now, NULL);
2538 } while (ret != 0 && errno == EAGAIN &&
2539 drmTimeDiff(&now, &then) < DRM_IOCTL_TIMEOUT_USEC);
2542 return ((errno == EAGAIN) ? -EBUSY : -errno);
2550 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2552 drm_fence_arg_t arg;
2555 if (flush_type == 0) {
2556 flush_type = fence->type;
2559 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2560 if ((flush_type & fence->signaled) == flush_type) {
2565 memset(&arg, 0, sizeof(arg));
2566 arg.handle = fence->handle;
2567 arg.type = flush_type;
2571 ret = drmIoctlTimeout(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2575 fence->fence_class = arg.fence_class;
2576 fence->type = arg.type;
2577 fence->signaled = arg.signaled;
2581 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2583 buf->handle = rep->handle;
2584 buf->flags = rep->flags;
2585 buf->size = rep->size;
2586 buf->offset = rep->offset;
2587 buf->mapHandle = rep->arg_handle;
2588 buf->proposedFlags = rep->proposed_flags;
2589 buf->start = rep->buffer_start;
2590 buf->fenceFlags = rep->fence_flags;
2591 buf->replyFlags = rep->rep_flags;
2592 buf->pageAlignment = rep->page_alignment;
2593 buf->tileInfo = rep->tile_info;
2594 buf->hwTileStride = rep->hw_tile_stride;
2595 buf->desiredTileStride = rep->desired_tile_stride;
2600 int drmBOCreate(int fd, unsigned long size,
2601 unsigned pageAlignment, void *user_buffer,
2603 unsigned hint, drmBO *buf)
2605 struct drm_bo_create_arg arg;
2606 struct drm_bo_create_req *req = &arg.d.req;
2607 struct drm_bo_info_rep *rep = &arg.d.rep;
2610 memset(buf, 0, sizeof(*buf));
2611 memset(&arg, 0, sizeof(arg));
2615 req->page_alignment = pageAlignment;
2616 req->buffer_start = (unsigned long) user_buffer;
2618 buf->virtual = NULL;
2620 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_CREATE, &arg);
2624 drmBOCopyReply(rep, buf);
2625 buf->virtual = user_buffer;
2631 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2633 struct drm_bo_reference_info_arg arg;
2634 struct drm_bo_handle_arg *req = &arg.d.req;
2635 struct drm_bo_info_rep *rep = &arg.d.rep;
2637 memset(&arg, 0, sizeof(arg));
2638 req->handle = handle;
2640 if (ioctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2643 drmBOCopyReply(rep, buf);
2644 buf->mapVirtual = NULL;
2646 buf->virtual = NULL;
2651 int drmBOUnreference(int fd, drmBO *buf)
2653 struct drm_bo_handle_arg arg;
2655 if (buf->mapVirtual && buf->mapHandle) {
2656 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2657 buf->mapVirtual = NULL;
2658 buf->virtual = NULL;
2661 memset(&arg, 0, sizeof(arg));
2662 arg.handle = buf->handle;
2664 if (ioctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2673 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2674 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2675 * call return an -EBUSY if it can' immediately honor the mapping request.
2678 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2681 struct drm_bo_map_wait_idle_arg arg;
2682 struct drm_bo_info_req *req = &arg.d.req;
2683 struct drm_bo_info_rep *rep = &arg.d.rep;
2687 * Make sure we have a virtual address of the buffer.
2690 if (!buf->virtual) {
2692 virtual = mmap(0, buf->size + buf->start,
2693 PROT_READ | PROT_WRITE, MAP_SHARED,
2694 fd, buf->mapHandle);
2695 if (virtual == MAP_FAILED) {
2700 buf->mapVirtual = virtual;
2701 buf->virtual = ((char *) virtual) + buf->start;
2704 memset(&arg, 0, sizeof(arg));
2705 req->handle = buf->handle;
2706 req->mask = mapFlags;
2707 req->hint = mapHint;
2710 * May hang if the buffer object is busy.
2711 * This IOCTL synchronizes the buffer.
2714 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_MAP, &arg);
2718 drmBOCopyReply(rep, buf);
2719 buf->mapFlags = mapFlags;
2721 *address = buf->virtual;
2727 int drmBOUnmap(int fd, drmBO *buf)
2729 struct drm_bo_handle_arg arg;
2731 memset(&arg, 0, sizeof(arg));
2732 arg.handle = buf->handle;
2734 if (ioctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2741 int drmBOSetStatus(int fd, drmBO *buf,
2742 uint64_t flags, uint64_t mask,
2744 unsigned int desired_tile_stride,
2745 unsigned int tile_info)
2748 struct drm_bo_map_wait_idle_arg arg;
2749 struct drm_bo_info_req *req = &arg.d.req;
2750 struct drm_bo_info_rep *rep = &arg.d.rep;
2753 memset(&arg, 0, sizeof(arg));
2756 req->handle = buf->handle;
2758 req->desired_tile_stride = desired_tile_stride;
2759 req->tile_info = tile_info;
2761 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_SETSTATUS, &arg);
2765 drmBOCopyReply(rep, buf);
2770 int drmBOInfo(int fd, drmBO *buf)
2772 struct drm_bo_reference_info_arg arg;
2773 struct drm_bo_handle_arg *req = &arg.d.req;
2774 struct drm_bo_info_rep *rep = &arg.d.rep;
2777 memset(&arg, 0, sizeof(arg));
2778 req->handle = buf->handle;
2780 ret = ioctl(fd, DRM_IOCTL_BO_INFO, &arg);
2784 drmBOCopyReply(rep, buf);
2788 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2790 struct drm_bo_map_wait_idle_arg arg;
2791 struct drm_bo_info_req *req = &arg.d.req;
2792 struct drm_bo_info_rep *rep = &arg.d.rep;
2795 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2796 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2797 memset(&arg, 0, sizeof(arg));
2798 req->handle = buf->handle;
2801 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2805 drmBOCopyReply(rep, buf);
2810 int drmBOBusy(int fd, drmBO *buf, int *busy)
2812 int ret = drmBOInfo(fd, buf);
2817 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2821 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
2824 struct drm_mm_init_arg arg;
2826 memset(&arg, 0, sizeof(arg));
2828 arg.magic = DRM_BO_INIT_MAGIC;
2829 arg.major = DRM_BO_INIT_MAJOR;
2830 arg.minor = DRM_BO_INIT_MINOR;
2831 arg.p_offset = pOffset;
2833 arg.mem_type = memType;
2835 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
2840 int drmMMTakedown(int fd, unsigned memType)
2842 struct drm_mm_type_arg arg;
2844 memset(&arg, 0, sizeof(arg));
2845 arg.mem_type = memType;
2847 if (ioctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
2853 * If this function returns an error, and lockBM was set to 1,
2854 * the buffer manager is NOT locked.
2857 int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict)
2859 struct drm_mm_type_arg arg;
2861 memset(&arg, 0, sizeof(arg));
2862 arg.mem_type = memType;
2863 arg.lock_flags |= (lockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2864 arg.lock_flags |= (ignoreNoEvict) ? DRM_BO_LOCK_IGNORE_NO_EVICT : 0;
2866 return drmIoctlTimeout(fd, DRM_IOCTL_MM_LOCK, &arg);
2869 int drmMMUnlock(int fd, unsigned memType, int unlockBM)
2871 struct drm_mm_type_arg arg;
2873 memset(&arg, 0, sizeof(arg));
2875 arg.mem_type = memType;
2876 arg.lock_flags |= (unlockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2878 return drmIoctlTimeout(fd, DRM_IOCTL_MM_UNLOCK, &arg);
2881 int drmMMInfo(int fd, unsigned memType, uint64_t *size)
2883 struct drm_mm_info_arg arg;
2885 memset(&arg, 0, sizeof(arg));
2887 arg.mem_type = memType;
2889 if (ioctl(fd, DRM_IOCTL_MM_INFO, &arg))
2896 int drmBOVersion(int fd, unsigned int *major,
2897 unsigned int *minor,
2898 unsigned int *patchlevel)
2900 struct drm_bo_version_arg arg;
2903 memset(&arg, 0, sizeof(arg));
2904 ret = ioctl(fd, DRM_IOCTL_BO_VERSION, &arg);
2913 *patchlevel = arg.patchlevel;
2920 #define DRM_MAX_FDS 16
2925 } connection[DRM_MAX_FDS];
2927 static int nr_fds = 0;
2929 int drmOpenOnce(void *unused,
2936 for (i = 0; i < nr_fds; i++)
2937 if (strcmp(BusID, connection[i].BusID) == 0) {
2938 connection[i].refcount++;
2940 return connection[i].fd;
2943 fd = drmOpen(unused, BusID);
2944 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2947 connection[nr_fds].BusID = strdup(BusID);
2948 connection[nr_fds].fd = fd;
2949 connection[nr_fds].refcount = 1;
2953 fprintf(stderr, "saved connection %d for %s %d\n",
2954 nr_fds, connection[nr_fds].BusID,
2955 strcmp(BusID, connection[nr_fds].BusID));
2962 void drmCloseOnce(int fd)
2966 for (i = 0; i < nr_fds; i++) {
2967 if (fd == connection[i].fd) {
2968 if (--connection[i].refcount == 0) {
2969 drmClose(connection[i].fd);
2970 free(connection[i].BusID);
2973 connection[i] = connection[nr_fds];
2981 int drmSetMaster(int fd)
2985 fprintf(stderr,"Setting master \n");
2986 ret = ioctl(fd, DRM_IOCTL_SET_MASTER, 0);
2990 int drmDropMaster(int fd)
2993 fprintf(stderr,"Dropping master \n");
2994 ret = ioctl(fd, DRM_IOCTL_DROP_MASTER, 0);