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 static drmServerInfoPtr drm_server_info;
92 void drmSetServerInfo(drmServerInfoPtr info)
94 drm_server_info = info;
98 * Output a message to stderr.
100 * \param format printf() like format string.
103 * This function is a wrapper around vfprintf().
106 static int drmDebugPrint(const char *format, va_list ap)
108 return vfprintf(stderr, format, ap);
111 static int (*drm_debug_print)(const char *format, va_list ap) = drmDebugPrint;
114 drmMsg(const char *format, ...)
118 if (((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose")) || drm_server_info)
120 va_start(ap, format);
121 if (drm_server_info) {
122 drm_server_info->debug_print(format,ap);
124 drm_debug_print(format, ap);
131 drmSetDebugMsgFunction(int (*debug_msg_ptr)(const char *format, va_list ap))
133 drm_debug_print = debug_msg_ptr;
136 static void *drmHashTable = NULL; /* Context switch callbacks */
138 void *drmGetHashTable(void)
143 void *drmMalloc(int size)
146 if ((pt = malloc(size)))
151 void drmFree(void *pt)
157 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
158 static char *drmStrdup(const char *s)
165 retval = malloc(strlen(s)+1);
175 * Call ioctl, restarting if it is interupted
178 drmIoctl(int fd, int request, void *arg)
183 ret = ioctl(fd, request, arg);
184 } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
188 static unsigned long drmGetKeyFromFd(int fd)
197 drmHashEntry *drmGetEntry(int fd)
199 unsigned long key = drmGetKeyFromFd(fd);
204 drmHashTable = drmHashCreate();
206 if (drmHashLookup(drmHashTable, key, &value)) {
207 entry = drmMalloc(sizeof(*entry));
210 entry->tagTable = drmHashCreate();
211 drmHashInsert(drmHashTable, key, entry);
219 * Compare two busid strings
224 * \return 1 if matched.
227 * This function compares two bus ID strings. It understands the older
228 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
229 * domain, b is bus, d is device, f is function.
231 static int drmMatchBusID(const char *id1, const char *id2)
233 /* First, check if the IDs are exactly the same */
234 if (strcasecmp(id1, id2) == 0)
237 /* Try to match old/new-style PCI bus IDs. */
238 if (strncasecmp(id1, "pci", 3) == 0) {
243 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
246 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
251 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
254 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
259 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
268 * Open the DRM device, creating it if necessary.
270 * \param dev major and minor numbers of the device.
271 * \param minor minor number of the device.
273 * \return a file descriptor on success, or a negative value on error.
276 * Assembles the device name from \p minor and opens it, creating the device
277 * special file node with the major and minor numbers specified by \p dev and
278 * parent directory if necessary and was called by root.
280 static int drmOpenDevice(long dev, int minor)
285 mode_t devmode = DRM_DEV_MODE, serv_mode;
286 int isroot = !geteuid();
287 uid_t user = DRM_DEV_UID;
288 gid_t group = DRM_DEV_GID, serv_group;
290 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
291 drmMsg("drmOpenDevice: node name is %s\n", buf);
293 if (drm_server_info) {
294 drm_server_info->get_perms(&serv_group, &serv_mode);
295 devmode = serv_mode ? serv_mode : DRM_DEV_MODE;
296 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
297 group = (serv_group >= 0) ? serv_group : DRM_DEV_GID;
300 if (stat(DRM_DIR_NAME, &st)) {
302 return DRM_ERR_NOT_ROOT;
303 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
304 chown(DRM_DIR_NAME, 0, 0); /* root:root */
305 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
308 /* Check if the device node exists and create it if necessary. */
309 if (stat(buf, &st)) {
311 return DRM_ERR_NOT_ROOT;
313 mknod(buf, S_IFCHR | devmode, dev);
316 if (drm_server_info) {
317 chown(buf, user, group);
321 fd = open(buf, O_RDWR, 0);
322 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
323 fd, fd < 0 ? strerror(errno) : "OK");
327 /* Check if the device node is not what we expect it to be, and recreate it
328 * and try again if so.
330 if (st.st_rdev != dev) {
332 return DRM_ERR_NOT_ROOT;
334 mknod(buf, S_IFCHR | devmode, dev);
335 if (drm_server_info) {
336 chown(buf, user, group);
340 fd = open(buf, O_RDWR, 0);
341 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
342 fd, fd < 0 ? strerror(errno) : "OK");
346 drmMsg("drmOpenDevice: Open failed\n");
353 * Open the DRM device
355 * \param minor device minor number.
356 * \param create allow to create the device if set.
358 * \return a file descriptor on success, or a negative value on error.
361 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
362 * name from \p minor and opens it.
364 static int drmOpenMinor(int minor, int create)
370 return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
372 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
373 if ((fd = open(buf, O_RDWR, 0)) >= 0)
380 * Determine whether the DRM kernel driver has been loaded.
382 * \return 1 if the DRM driver is loaded, 0 otherwise.
385 * Determine the presence of the kernel driver by attempting to open the 0
386 * minor and get version information. For backward compatibility with older
387 * Linux implementations, /proc/dri is also checked.
389 int drmAvailable(void)
391 drmVersionPtr version;
395 if ((fd = drmOpenMinor(0, 1)) < 0) {
397 /* Try proc for backward Linux compatibility */
398 if (!access("/proc/dri/0", R_OK))
404 if ((version = drmGetVersion(fd))) {
406 drmFreeVersion(version);
415 * Open the device by bus ID.
417 * \param busid bus ID.
419 * \return a file descriptor on success, or a negative value on error.
422 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
423 * comparing the device bus ID with the one supplied.
425 * \sa drmOpenMinor() and drmGetBusid().
427 static int drmOpenByBusid(const char *busid)
434 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
435 for (i = 0; i < DRM_MAX_MINOR; i++) {
436 fd = drmOpenMinor(i, 1);
437 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
441 sv.drm_dd_major = -1; /* Don't care */
442 sv.drm_dd_minor = -1; /* Don't care */
443 drmSetInterfaceVersion(fd, &sv);
444 buf = drmGetBusid(fd);
445 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
446 if (buf && drmMatchBusID(buf, busid)) {
460 * Open the device by name.
462 * \param name driver name.
464 * \return a file descriptor on success, or a negative value on error.
467 * This function opens the first minor number that matches the driver name and
468 * isn't already in use. If it's in use it then it will already have a bus ID
471 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
473 static int drmOpenByName(const char *name)
477 drmVersionPtr version;
480 if (!drmAvailable()) {
481 if (!drm_server_info) {
485 /* try to load the kernel module now */
486 if (!drm_server_info->load_module(name)) {
487 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
494 * Open the first minor number that matches the driver name and isn't
495 * already in use. If it's in use it will have a busid assigned already.
497 for (i = 0; i < DRM_MAX_MINOR; i++) {
498 if ((fd = drmOpenMinor(i, 1)) >= 0) {
499 if ((version = drmGetVersion(fd))) {
500 if (!strcmp(version->name, name)) {
501 drmFreeVersion(version);
502 id = drmGetBusid(fd);
503 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
512 drmFreeVersion(version);
520 /* Backward-compatibility /proc support */
521 for (i = 0; i < 8; i++) {
522 char proc_name[64], buf[512];
523 char *driver, *pt, *devstring;
526 sprintf(proc_name, "/proc/dri/%d/name", i);
527 if ((fd = open(proc_name, 0, 0)) >= 0) {
528 retcode = read(fd, buf, sizeof(buf)-1);
531 buf[retcode-1] = '\0';
532 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
534 if (*pt) { /* Device is next */
536 if (!strcmp(driver, name)) { /* Match */
537 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
539 if (*pt) { /* Found busid */
540 return drmOpenByBusid(++pt);
541 } else { /* No busid */
542 return drmOpenDevice(strtol(devstring, NULL, 0),i);
556 * Open the DRM device.
558 * Looks up the specified name and bus ID, and opens the device found. The
559 * entry in /dev/dri is created if necessary and if called by root.
561 * \param name driver name. Not referenced if bus ID is supplied.
562 * \param busid bus ID. Zero if not known.
564 * \return a file descriptor on success, or a negative value on error.
567 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
570 int drmOpen(const char *name, const char *busid)
572 if (!drmAvailable() && name != NULL && drm_server_info) {
573 /* try to load the kernel */
574 if (!drm_server_info->load_module(name)) {
575 drmMsg("[drm] failed to load kernel module \"%s\"\n", name);
581 int fd = drmOpenByBusid(busid);
587 return drmOpenByName(name);
594 * Free the version information returned by drmGetVersion().
596 * \param v pointer to the version information.
599 * It frees the memory pointed by \p %v as well as all the non-null strings
602 void drmFreeVersion(drmVersionPtr v)
614 * Free the non-public version information returned by the kernel.
616 * \param v pointer to the version information.
619 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
620 * the non-null strings pointers in it.
622 static void drmFreeKernelVersion(drm_version_t *v)
634 * Copy version information.
636 * \param d destination pointer.
637 * \param s source pointer.
640 * Used by drmGetVersion() to translate the information returned by the ioctl
641 * interface in a private structure into the public structure counterpart.
643 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
645 d->version_major = s->version_major;
646 d->version_minor = s->version_minor;
647 d->version_patchlevel = s->version_patchlevel;
648 d->name_len = s->name_len;
649 d->name = drmStrdup(s->name);
650 d->date_len = s->date_len;
651 d->date = drmStrdup(s->date);
652 d->desc_len = s->desc_len;
653 d->desc = drmStrdup(s->desc);
658 * Query the driver version information.
660 * \param fd file descriptor.
662 * \return pointer to a drmVersion structure which should be freed with
665 * \note Similar information is available via /proc/dri.
668 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
669 * first with zeros to get the string lengths, and then the actually strings.
670 * It also null-terminates them since they might not be already.
672 drmVersionPtr drmGetVersion(int fd)
674 drmVersionPtr retval;
675 drm_version_t *version = drmMalloc(sizeof(*version));
677 version->name_len = 0;
678 version->name = NULL;
679 version->date_len = 0;
680 version->date = NULL;
681 version->desc_len = 0;
682 version->desc = NULL;
684 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
685 drmFreeKernelVersion(version);
689 if (version->name_len)
690 version->name = drmMalloc(version->name_len + 1);
691 if (version->date_len)
692 version->date = drmMalloc(version->date_len + 1);
693 if (version->desc_len)
694 version->desc = drmMalloc(version->desc_len + 1);
696 if (drmIoctl(fd, DRM_IOCTL_VERSION, version)) {
697 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
698 drmFreeKernelVersion(version);
702 /* The results might not be null-terminated strings, so terminate them. */
703 if (version->name_len) version->name[version->name_len] = '\0';
704 if (version->date_len) version->date[version->date_len] = '\0';
705 if (version->desc_len) version->desc[version->desc_len] = '\0';
707 retval = drmMalloc(sizeof(*retval));
708 drmCopyVersion(retval, version);
709 drmFreeKernelVersion(version);
715 * Get version information for the DRM user space library.
717 * This version number is driver independent.
719 * \param fd file descriptor.
721 * \return version information.
724 * This function allocates and fills a drm_version structure with a hard coded
727 drmVersionPtr drmGetLibVersion(int fd)
729 drm_version_t *version = drmMalloc(sizeof(*version));
732 * NOTE THIS MUST NOT GO ABOVE VERSION 1.X due to drivers needing it
733 * revision 1.0.x = original DRM interface with no drmGetLibVersion
734 * entry point and many drm<Device> extensions
735 * revision 1.1.x = added drmCommand entry points for device extensions
736 * added drmGetLibVersion to identify libdrm.a version
737 * revision 1.2.x = added drmSetInterfaceVersion
738 * modified drmOpen to handle both busid and name
739 * revision 1.3.x = added server + memory manager
741 version->version_major = 1;
742 version->version_minor = 3;
743 version->version_patchlevel = 0;
745 return (drmVersionPtr)version;
750 * Free the bus ID information.
752 * \param busid bus ID information string as given by drmGetBusid().
755 * This function is just frees the memory pointed by \p busid.
757 void drmFreeBusid(const char *busid)
759 drmFree((void *)busid);
764 * Get the bus ID of the device.
766 * \param fd file descriptor.
768 * \return bus ID string.
771 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
772 * get the string length and data, passing the arguments in a drm_unique
775 char *drmGetBusid(int fd)
782 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
784 u.unique = drmMalloc(u.unique_len + 1);
785 if (drmIoctl(fd, DRM_IOCTL_GET_UNIQUE, &u))
787 u.unique[u.unique_len] = '\0';
794 * Set the bus ID of the device.
796 * \param fd file descriptor.
797 * \param busid bus ID string.
799 * \return zero on success, negative on failure.
802 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
803 * the arguments in a drm_unique structure.
805 int drmSetBusid(int fd, const char *busid)
809 u.unique = (char *)busid;
810 u.unique_len = strlen(busid);
812 if (drmIoctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
818 int drmGetMagic(int fd, drm_magic_t * magic)
823 if (drmIoctl(fd, DRM_IOCTL_GET_MAGIC, &auth))
829 int drmAuthMagic(int fd, drm_magic_t magic)
834 if (drmIoctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth))
840 * Specifies a range of memory that is available for mapping by a
843 * \param fd file descriptor.
844 * \param offset usually the physical address. The actual meaning depends of
845 * the \p type parameter. See below.
846 * \param size of the memory in bytes.
847 * \param type type of the memory to be mapped.
848 * \param flags combination of several flags to modify the function actions.
849 * \param handle will be set to a value that may be used as the offset
850 * parameter for mmap().
852 * \return zero on success or a negative value on error.
854 * \par Mapping the frame buffer
855 * For the frame buffer
856 * - \p offset will be the physical address of the start of the frame buffer,
857 * - \p size will be the size of the frame buffer in bytes, and
858 * - \p type will be DRM_FRAME_BUFFER.
861 * The area mapped will be uncached. If MTRR support is available in the
862 * kernel, the frame buffer area will be set to write combining.
864 * \par Mapping the MMIO register area
865 * For the MMIO register area,
866 * - \p offset will be the physical address of the start of the register area,
867 * - \p size will be the size of the register area bytes, and
868 * - \p type will be DRM_REGISTERS.
870 * The area mapped will be uncached.
872 * \par Mapping the SAREA
874 * - \p offset will be ignored and should be set to zero,
875 * - \p size will be the desired size of the SAREA in bytes,
876 * - \p type will be DRM_SHM.
879 * A shared memory area of the requested size will be created and locked in
880 * kernel memory. This area may be mapped into client-space by using the handle
883 * \note May only be called by root.
886 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
887 * the arguments in a drm_map structure.
889 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
890 drmMapFlags flags, drm_handle_t *handle)
899 if (drmIoctl(fd, DRM_IOCTL_ADD_MAP, &map))
902 *handle = (drm_handle_t)map.handle;
906 int drmRmMap(int fd, drm_handle_t handle)
910 map.handle = (void *)handle;
912 if(drmIoctl(fd, DRM_IOCTL_RM_MAP, &map))
918 * Make buffers available for DMA transfers.
920 * \param fd file descriptor.
921 * \param count number of buffers.
922 * \param size size of each buffer.
923 * \param flags buffer allocation flags.
924 * \param agp_offset offset in the AGP aperture
926 * \return number of buffers allocated, negative on error.
929 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
933 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
936 drm_buf_desc_t request;
938 request.count = count;
940 request.low_mark = 0;
941 request.high_mark = 0;
942 request.flags = flags;
943 request.agp_start = agp_offset;
945 if (drmIoctl(fd, DRM_IOCTL_ADD_BUFS, &request))
947 return request.count;
950 int drmMarkBufs(int fd, double low, double high)
958 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
964 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
967 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
973 for (i = 0; i < info.count; i++) {
974 info.list[i].low_mark = low * info.list[i].count;
975 info.list[i].high_mark = high * info.list[i].count;
976 if (drmIoctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
990 * \param fd file descriptor.
991 * \param count number of buffers to free.
992 * \param list list of buffers to be freed.
994 * \return zero on success, or a negative value on failure.
996 * \note This function is primarily used for debugging.
999 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
1000 * the arguments in a drm_buf_free structure.
1002 int drmFreeBufs(int fd, int count, int *list)
1004 drm_buf_free_t request;
1006 request.count = count;
1007 request.list = list;
1008 if (drmIoctl(fd, DRM_IOCTL_FREE_BUFS, &request))
1017 * \param fd file descriptor.
1020 * This function closes the file descriptor.
1022 int drmClose(int fd)
1024 unsigned long key = drmGetKeyFromFd(fd);
1025 drmHashEntry *entry = drmGetEntry(fd);
1027 drmHashDestroy(entry->tagTable);
1030 entry->tagTable = NULL;
1032 drmHashDelete(drmHashTable, key);
1040 * Map a region of memory.
1042 * \param fd file descriptor.
1043 * \param handle handle returned by drmAddMap().
1044 * \param size size in bytes. Must match the size used by drmAddMap().
1045 * \param address will contain the user-space virtual address where the mapping
1048 * \return zero on success, or a negative value on failure.
1051 * This function is a wrapper for mmap().
1053 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1055 static unsigned long pagesize_mask = 0;
1061 pagesize_mask = getpagesize() - 1;
1063 size = (size + pagesize_mask) & ~pagesize_mask;
1065 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1066 if (*address == MAP_FAILED)
1073 * Unmap mappings obtained with drmMap().
1075 * \param address address as given by drmMap().
1076 * \param size size in bytes. Must match the size used by drmMap().
1078 * \return zero on success, or a negative value on failure.
1081 * This function is a wrapper for munmap().
1083 int drmUnmap(drmAddress address, drmSize size)
1085 return munmap(address, size);
1088 drmBufInfoPtr drmGetBufInfo(int fd)
1090 drm_buf_info_t info;
1091 drmBufInfoPtr retval;
1097 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info))
1101 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1104 if (drmIoctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1109 retval = drmMalloc(sizeof(*retval));
1110 retval->count = info.count;
1111 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1112 for (i = 0; i < info.count; i++) {
1113 retval->list[i].count = info.list[i].count;
1114 retval->list[i].size = info.list[i].size;
1115 retval->list[i].low_mark = info.list[i].low_mark;
1116 retval->list[i].high_mark = info.list[i].high_mark;
1125 * Map all DMA buffers into client-virtual space.
1127 * \param fd file descriptor.
1129 * \return a pointer to a ::drmBufMap structure.
1131 * \note The client may not use these buffers until obtaining buffer indices
1135 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1136 * information about the buffers in a drm_buf_map structure into the
1137 * client-visible data structures.
1139 drmBufMapPtr drmMapBufs(int fd)
1142 drmBufMapPtr retval;
1147 bufs.virtual = NULL;
1148 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs))
1154 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1157 if (drmIoctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1162 retval = drmMalloc(sizeof(*retval));
1163 retval->count = bufs.count;
1164 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1165 for (i = 0; i < bufs.count; i++) {
1166 retval->list[i].idx = bufs.list[i].idx;
1167 retval->list[i].total = bufs.list[i].total;
1168 retval->list[i].used = 0;
1169 retval->list[i].address = bufs.list[i].address;
1179 * Unmap buffers allocated with drmMapBufs().
1181 * \return zero on success, or negative value on failure.
1184 * Calls munmap() for every buffer stored in \p bufs and frees the
1185 * memory allocated by drmMapBufs().
1187 int drmUnmapBufs(drmBufMapPtr bufs)
1191 for (i = 0; i < bufs->count; i++) {
1192 munmap(bufs->list[i].address, bufs->list[i].total);
1195 drmFree(bufs->list);
1202 #define DRM_DMA_RETRY 16
1205 * Reserve DMA buffers.
1207 * \param fd file descriptor.
1210 * \return zero on success, or a negative value on failure.
1213 * Assemble the arguments into a drm_dma structure and keeps issuing the
1214 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1216 int drmDMA(int fd, drmDMAReqPtr request)
1221 dma.context = request->context;
1222 dma.send_count = request->send_count;
1223 dma.send_indices = request->send_list;
1224 dma.send_sizes = request->send_sizes;
1225 dma.flags = request->flags;
1226 dma.request_count = request->request_count;
1227 dma.request_size = request->request_size;
1228 dma.request_indices = request->request_list;
1229 dma.request_sizes = request->request_sizes;
1230 dma.granted_count = 0;
1233 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1234 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1237 request->granted_count = dma.granted_count;
1246 * Obtain heavyweight hardware lock.
1248 * \param fd file descriptor.
1249 * \param context context.
1250 * \param flags flags that determine the sate of the hardware when the function
1253 * \return always zero.
1256 * This function translates the arguments into a drm_lock structure and issue
1257 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1259 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1263 lock.context = context;
1265 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1266 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1267 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1268 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1269 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1270 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1272 while (drmIoctl(fd, DRM_IOCTL_LOCK, &lock))
1278 * Release the hardware lock.
1280 * \param fd file descriptor.
1281 * \param context context.
1283 * \return zero on success, or a negative value on failure.
1286 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1287 * argument in a drm_lock structure.
1289 int drmUnlock(int fd, drm_context_t context)
1293 lock.context = context;
1295 return drmIoctl(fd, DRM_IOCTL_UNLOCK, &lock);
1298 drm_context_t *drmGetReservedContextList(int fd, int *count)
1302 drm_context_t * retval;
1306 res.contexts = NULL;
1307 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1313 if (!(list = drmMalloc(res.count * sizeof(*list))))
1315 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1320 res.contexts = list;
1321 if (drmIoctl(fd, DRM_IOCTL_RES_CTX, &res))
1324 for (i = 0; i < res.count; i++)
1325 retval[i] = list[i].handle;
1332 void drmFreeReservedContextList(drm_context_t *pt)
1340 * Used by the X server during GLXContext initialization. This causes
1341 * per-context kernel-level resources to be allocated.
1343 * \param fd file descriptor.
1344 * \param handle is set on success. To be used by the client when requesting DMA
1345 * dispatch with drmDMA().
1347 * \return zero on success, or a negative value on failure.
1349 * \note May only be called by root.
1352 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1353 * argument in a drm_ctx structure.
1355 int drmCreateContext(int fd, drm_context_t *handle)
1359 ctx.flags = 0; /* Modified with functions below */
1360 if (drmIoctl(fd, DRM_IOCTL_ADD_CTX, &ctx))
1362 *handle = ctx.handle;
1366 int drmSwitchToContext(int fd, drm_context_t context)
1370 ctx.handle = context;
1371 if (drmIoctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx))
1376 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1381 * Context preserving means that no context switches are done between DMA
1382 * buffers from one context and the next. This is suitable for use in the
1383 * X server (which promises to maintain hardware context), or in the
1384 * client-side library when buffers are swapped on behalf of two threads.
1386 ctx.handle = context;
1388 if (flags & DRM_CONTEXT_PRESERVED)
1389 ctx.flags |= _DRM_CONTEXT_PRESERVED;
1390 if (flags & DRM_CONTEXT_2DONLY)
1391 ctx.flags |= _DRM_CONTEXT_2DONLY;
1392 if (drmIoctl(fd, DRM_IOCTL_MOD_CTX, &ctx))
1397 int drmGetContextFlags(int fd, drm_context_t context,
1398 drm_context_tFlagsPtr flags)
1402 ctx.handle = context;
1403 if (drmIoctl(fd, DRM_IOCTL_GET_CTX, &ctx))
1406 if (ctx.flags & _DRM_CONTEXT_PRESERVED)
1407 *flags |= DRM_CONTEXT_PRESERVED;
1408 if (ctx.flags & _DRM_CONTEXT_2DONLY)
1409 *flags |= DRM_CONTEXT_2DONLY;
1416 * Free any kernel-level resources allocated with drmCreateContext() associated
1419 * \param fd file descriptor.
1420 * \param handle handle given by drmCreateContext().
1422 * \return zero on success, or a negative value on failure.
1424 * \note May only be called by root.
1427 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1428 * argument in a drm_ctx structure.
1430 int drmDestroyContext(int fd, drm_context_t handle)
1433 ctx.handle = handle;
1434 if (drmIoctl(fd, DRM_IOCTL_RM_CTX, &ctx))
1439 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1442 if (drmIoctl(fd, DRM_IOCTL_ADD_DRAW, &draw))
1444 *handle = draw.handle;
1448 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1451 draw.handle = handle;
1452 if (drmIoctl(fd, DRM_IOCTL_RM_DRAW, &draw))
1457 int drmUpdateDrawableInfo(int fd, drm_drawable_t handle,
1458 drm_drawable_info_type_t type, unsigned int num,
1461 drm_update_draw_t update;
1463 update.handle = handle;
1466 update.data = (unsigned long long)(unsigned long)data;
1468 if (drmIoctl(fd, DRM_IOCTL_UPDATE_DRAW, &update))
1475 * Acquire the AGP device.
1477 * Must be called before any of the other AGP related calls.
1479 * \param fd file descriptor.
1481 * \return zero on success, or a negative value on failure.
1484 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1486 int drmAgpAcquire(int fd)
1488 if (drmIoctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL))
1495 * Release the AGP device.
1497 * \param fd file descriptor.
1499 * \return zero on success, or a negative value on failure.
1502 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1504 int drmAgpRelease(int fd)
1506 if (drmIoctl(fd, DRM_IOCTL_AGP_RELEASE, NULL))
1515 * \param fd file descriptor.
1516 * \param mode AGP mode.
1518 * \return zero on success, or a negative value on failure.
1521 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1522 * argument in a drm_agp_mode structure.
1524 int drmAgpEnable(int fd, unsigned long mode)
1529 if (drmIoctl(fd, DRM_IOCTL_AGP_ENABLE, &m))
1536 * Allocate a chunk of AGP memory.
1538 * \param fd file descriptor.
1539 * \param size requested memory size in bytes. Will be rounded to page boundary.
1540 * \param type type of memory to allocate.
1541 * \param address if not zero, will be set to the physical address of the
1543 * \param handle on success will be set to a handle of the allocated memory.
1545 * \return zero on success, or a negative value on failure.
1548 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1549 * arguments in a drm_agp_buffer structure.
1551 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1552 unsigned long *address, drm_handle_t *handle)
1556 *handle = DRM_AGP_NO_HANDLE;
1560 if (drmIoctl(fd, DRM_IOCTL_AGP_ALLOC, &b))
1563 *address = b.physical;
1570 * Free a chunk of AGP memory.
1572 * \param fd file descriptor.
1573 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1575 * \return zero on success, or a negative value on failure.
1578 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1579 * argument in a drm_agp_buffer structure.
1581 int drmAgpFree(int fd, drm_handle_t handle)
1587 if (drmIoctl(fd, DRM_IOCTL_AGP_FREE, &b))
1594 * Bind a chunk of AGP memory.
1596 * \param fd file descriptor.
1597 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1598 * \param offset offset in bytes. It will round to page boundary.
1600 * \return zero on success, or a negative value on failure.
1603 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1604 * argument in a drm_agp_binding structure.
1606 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1608 drm_agp_binding_t b;
1612 if (drmIoctl(fd, DRM_IOCTL_AGP_BIND, &b))
1619 * Unbind a chunk of AGP memory.
1621 * \param fd file descriptor.
1622 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1624 * \return zero on success, or a negative value on failure.
1627 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1628 * the argument in a drm_agp_binding structure.
1630 int drmAgpUnbind(int fd, drm_handle_t handle)
1632 drm_agp_binding_t b;
1636 if (drmIoctl(fd, DRM_IOCTL_AGP_UNBIND, &b))
1643 * Get AGP driver major version number.
1645 * \param fd file descriptor.
1647 * \return major version number on success, or a negative value on failure..
1650 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1651 * necessary information in a drm_agp_info structure.
1653 int drmAgpVersionMajor(int fd)
1657 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1659 return i.agp_version_major;
1664 * Get AGP driver minor version number.
1666 * \param fd file descriptor.
1668 * \return minor version number on success, or a negative value on failure.
1671 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1672 * necessary information in a drm_agp_info structure.
1674 int drmAgpVersionMinor(int fd)
1678 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1680 return i.agp_version_minor;
1687 * \param fd file descriptor.
1689 * \return mode on success, or zero on failure.
1692 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1693 * necessary information in a drm_agp_info structure.
1695 unsigned long drmAgpGetMode(int fd)
1699 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1706 * Get AGP aperture base.
1708 * \param fd file descriptor.
1710 * \return aperture base on success, zero on failure.
1713 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1714 * necessary information in a drm_agp_info structure.
1716 unsigned long drmAgpBase(int fd)
1720 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1722 return i.aperture_base;
1727 * Get AGP aperture size.
1729 * \param fd file descriptor.
1731 * \return aperture size on success, zero on failure.
1734 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1735 * necessary information in a drm_agp_info structure.
1737 unsigned long drmAgpSize(int fd)
1741 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1743 return i.aperture_size;
1748 * Get used AGP memory.
1750 * \param fd file descriptor.
1752 * \return memory used on success, or zero on failure.
1755 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1756 * necessary information in a drm_agp_info structure.
1758 unsigned long drmAgpMemoryUsed(int fd)
1762 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1764 return i.memory_used;
1769 * Get available AGP memory.
1771 * \param fd file descriptor.
1773 * \return memory available on success, or zero on failure.
1776 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1777 * necessary information in a drm_agp_info structure.
1779 unsigned long drmAgpMemoryAvail(int fd)
1783 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1785 return i.memory_allowed;
1790 * Get hardware vendor ID.
1792 * \param fd file descriptor.
1794 * \return vendor ID on success, or zero on failure.
1797 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1798 * necessary information in a drm_agp_info structure.
1800 unsigned int drmAgpVendorId(int fd)
1804 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1811 * Get hardware device ID.
1813 * \param fd file descriptor.
1815 * \return zero on success, or zero on failure.
1818 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1819 * necessary information in a drm_agp_info structure.
1821 unsigned int drmAgpDeviceId(int fd)
1825 if (drmIoctl(fd, DRM_IOCTL_AGP_INFO, &i))
1830 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1832 drm_scatter_gather_t sg;
1837 if (drmIoctl(fd, DRM_IOCTL_SG_ALLOC, &sg))
1839 *handle = sg.handle;
1843 int drmScatterGatherFree(int fd, drm_handle_t handle)
1845 drm_scatter_gather_t sg;
1849 if (drmIoctl(fd, DRM_IOCTL_SG_FREE, &sg))
1857 * \param fd file descriptor.
1858 * \param vbl pointer to a drmVBlank structure.
1860 * \return zero on success, or a negative value on failure.
1863 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1865 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1870 ret = drmIoctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1871 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1872 } while (ret && errno == EINTR);
1877 int drmError(int err, const char *label)
1880 case DRM_ERR_NO_DEVICE:
1881 fprintf(stderr, "%s: no device\n", label);
1883 case DRM_ERR_NO_ACCESS:
1884 fprintf(stderr, "%s: no access\n", label);
1886 case DRM_ERR_NOT_ROOT:
1887 fprintf(stderr, "%s: not root\n", label);
1889 case DRM_ERR_INVALID:
1890 fprintf(stderr, "%s: invalid args\n", label);
1895 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1903 * Install IRQ handler.
1905 * \param fd file descriptor.
1906 * \param irq IRQ number.
1908 * \return zero on success, or a negative value on failure.
1911 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1912 * argument in a drm_control structure.
1914 int drmCtlInstHandler(int fd, int irq)
1918 ctl.func = DRM_INST_HANDLER;
1920 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1927 * Uninstall IRQ handler.
1929 * \param fd file descriptor.
1931 * \return zero on success, or a negative value on failure.
1934 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1935 * argument in a drm_control structure.
1937 int drmCtlUninstHandler(int fd)
1941 ctl.func = DRM_UNINST_HANDLER;
1943 if (drmIoctl(fd, DRM_IOCTL_CONTROL, &ctl))
1948 int drmFinish(int fd, int context, drmLockFlags flags)
1952 lock.context = context;
1954 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1955 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1956 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1957 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1958 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1959 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1960 if (drmIoctl(fd, DRM_IOCTL_FINISH, &lock))
1966 * Get IRQ from bus ID.
1968 * \param fd file descriptor.
1969 * \param busnum bus number.
1970 * \param devnum device number.
1971 * \param funcnum function number.
1973 * \return IRQ number on success, or a negative value on failure.
1976 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1977 * arguments in a drm_irq_busid structure.
1979 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1985 p.funcnum = funcnum;
1986 if (drmIoctl(fd, DRM_IOCTL_IRQ_BUSID, &p))
1991 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1993 drmHashEntry *entry = drmGetEntry(fd);
1995 if (drmHashInsert(entry->tagTable, context, tag)) {
1996 drmHashDelete(entry->tagTable, context);
1997 drmHashInsert(entry->tagTable, context, tag);
2002 int drmDelContextTag(int fd, drm_context_t context)
2004 drmHashEntry *entry = drmGetEntry(fd);
2006 return drmHashDelete(entry->tagTable, context);
2009 void *drmGetContextTag(int fd, drm_context_t context)
2011 drmHashEntry *entry = drmGetEntry(fd);
2014 if (drmHashLookup(entry->tagTable, context, &value))
2020 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
2021 drm_handle_t handle)
2023 drm_ctx_priv_map_t map;
2025 map.ctx_id = ctx_id;
2026 map.handle = (void *)handle;
2028 if (drmIoctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map))
2033 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
2034 drm_handle_t *handle)
2036 drm_ctx_priv_map_t map;
2038 map.ctx_id = ctx_id;
2040 if (drmIoctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map))
2043 *handle = (drm_handle_t)map.handle;
2048 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
2049 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
2055 if (drmIoctl(fd, DRM_IOCTL_GET_MAP, &map))
2057 *offset = map.offset;
2061 *handle = (unsigned long)map.handle;
2066 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
2067 unsigned long *magic, unsigned long *iocs)
2069 drm_client_t client;
2072 if (drmIoctl(fd, DRM_IOCTL_GET_CLIENT, &client))
2074 *auth = client.auth;
2077 *magic = client.magic;
2078 *iocs = client.iocs;
2082 int drmGetStats(int fd, drmStatsT *stats)
2087 if (drmIoctl(fd, DRM_IOCTL_GET_STATS, &s))
2091 memset(stats, 0, sizeof(*stats));
2092 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
2096 stats->data[i].long_format = "%-20.20s"; \
2097 stats->data[i].rate_format = "%8.8s"; \
2098 stats->data[i].isvalue = 1; \
2099 stats->data[i].verbose = 0
2102 stats->data[i].long_format = "%-20.20s"; \
2103 stats->data[i].rate_format = "%5.5s"; \
2104 stats->data[i].isvalue = 0; \
2105 stats->data[i].mult_names = "kgm"; \
2106 stats->data[i].mult = 1000; \
2107 stats->data[i].verbose = 0
2110 stats->data[i].long_format = "%-20.20s"; \
2111 stats->data[i].rate_format = "%5.5s"; \
2112 stats->data[i].isvalue = 0; \
2113 stats->data[i].mult_names = "KGM"; \
2114 stats->data[i].mult = 1024; \
2115 stats->data[i].verbose = 0
2118 stats->count = s.count;
2119 for (i = 0; i < s.count; i++) {
2120 stats->data[i].value = s.data[i].value;
2121 switch (s.data[i].type) {
2122 case _DRM_STAT_LOCK:
2123 stats->data[i].long_name = "Lock";
2124 stats->data[i].rate_name = "Lock";
2127 case _DRM_STAT_OPENS:
2128 stats->data[i].long_name = "Opens";
2129 stats->data[i].rate_name = "O";
2131 stats->data[i].verbose = 1;
2133 case _DRM_STAT_CLOSES:
2134 stats->data[i].long_name = "Closes";
2135 stats->data[i].rate_name = "Lock";
2137 stats->data[i].verbose = 1;
2139 case _DRM_STAT_IOCTLS:
2140 stats->data[i].long_name = "Ioctls";
2141 stats->data[i].rate_name = "Ioc/s";
2144 case _DRM_STAT_LOCKS:
2145 stats->data[i].long_name = "Locks";
2146 stats->data[i].rate_name = "Lck/s";
2149 case _DRM_STAT_UNLOCKS:
2150 stats->data[i].long_name = "Unlocks";
2151 stats->data[i].rate_name = "Unl/s";
2155 stats->data[i].long_name = "IRQs";
2156 stats->data[i].rate_name = "IRQ/s";
2159 case _DRM_STAT_PRIMARY:
2160 stats->data[i].long_name = "Primary Bytes";
2161 stats->data[i].rate_name = "PB/s";
2164 case _DRM_STAT_SECONDARY:
2165 stats->data[i].long_name = "Secondary Bytes";
2166 stats->data[i].rate_name = "SB/s";
2170 stats->data[i].long_name = "DMA";
2171 stats->data[i].rate_name = "DMA/s";
2174 case _DRM_STAT_SPECIAL:
2175 stats->data[i].long_name = "Special DMA";
2176 stats->data[i].rate_name = "dma/s";
2179 case _DRM_STAT_MISSED:
2180 stats->data[i].long_name = "Miss";
2181 stats->data[i].rate_name = "Ms/s";
2184 case _DRM_STAT_VALUE:
2185 stats->data[i].long_name = "Value";
2186 stats->data[i].rate_name = "Value";
2189 case _DRM_STAT_BYTE:
2190 stats->data[i].long_name = "Bytes";
2191 stats->data[i].rate_name = "B/s";
2194 case _DRM_STAT_COUNT:
2196 stats->data[i].long_name = "Count";
2197 stats->data[i].rate_name = "Cnt/s";
2206 * Issue a set-version ioctl.
2208 * \param fd file descriptor.
2209 * \param drmCommandIndex command index
2210 * \param data source pointer of the data to be read and written.
2211 * \param size size of the data to be read and written.
2213 * \return zero on success, or a negative value on failure.
2216 * It issues a read-write ioctl given by
2217 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2219 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2222 drm_set_version_t sv;
2224 sv.drm_di_major = version->drm_di_major;
2225 sv.drm_di_minor = version->drm_di_minor;
2226 sv.drm_dd_major = version->drm_dd_major;
2227 sv.drm_dd_minor = version->drm_dd_minor;
2229 if (drmIoctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2233 version->drm_di_major = sv.drm_di_major;
2234 version->drm_di_minor = sv.drm_di_minor;
2235 version->drm_dd_major = sv.drm_dd_major;
2236 version->drm_dd_minor = sv.drm_dd_minor;
2242 * Send a device-specific command.
2244 * \param fd file descriptor.
2245 * \param drmCommandIndex command index
2247 * \return zero on success, or a negative value on failure.
2250 * It issues a ioctl given by
2251 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2253 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2255 void *data = NULL; /* dummy */
2256 unsigned long request;
2258 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2260 if (drmIoctl(fd, request, data)) {
2268 * Send a device-specific read command.
2270 * \param fd file descriptor.
2271 * \param drmCommandIndex command index
2272 * \param data destination pointer of the data to be read.
2273 * \param size size of the data to be read.
2275 * \return zero on success, or a negative value on failure.
2278 * It issues a read ioctl given by
2279 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2281 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2284 unsigned long request;
2286 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2287 DRM_COMMAND_BASE + drmCommandIndex, size);
2289 if (drmIoctl(fd, request, data)) {
2297 * Send a device-specific write command.
2299 * \param fd file descriptor.
2300 * \param drmCommandIndex command index
2301 * \param data source pointer of the data to be written.
2302 * \param size size of the data to be written.
2304 * \return zero on success, or a negative value on failure.
2307 * It issues a write ioctl given by
2308 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2310 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2313 unsigned long request;
2315 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2316 DRM_COMMAND_BASE + drmCommandIndex, size);
2318 if (drmIoctl(fd, request, data)) {
2326 * Send a device-specific read-write command.
2328 * \param fd file descriptor.
2329 * \param drmCommandIndex command index
2330 * \param data source pointer of the data to be read and written.
2331 * \param size size of the data to be read and written.
2333 * \return zero on success, or a negative value on failure.
2336 * It issues a read-write ioctl given by
2337 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2339 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2342 unsigned long request;
2344 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2345 DRM_COMMAND_BASE + drmCommandIndex, size);
2347 if (drmIoctl(fd, request, data))
2355 * DRM_FENCE_FLAG_EMIT
2356 * DRM_FENCE_FLAG_SHAREABLE
2357 * DRM_FENCE_MASK_DRIVER
2360 int drmFenceCreate(int fd, unsigned flags, int fence_class, unsigned type,
2363 drm_fence_arg_t arg;
2365 memset(&arg, 0, sizeof(arg));
2368 arg.fence_class = fence_class;
2370 if (drmIoctl(fd, DRM_IOCTL_FENCE_CREATE, &arg))
2372 fence->handle = arg.handle;
2373 fence->fence_class = arg.fence_class;
2374 fence->type = arg.type;
2375 fence->flags = arg.flags;
2376 fence->signaled = 0;
2382 * DRM_FENCE_FLAG_SHAREABLE
2383 * DRM_FENCE_MASK_DRIVER
2386 int drmFenceBuffers(int fd, unsigned flags, uint32_t fence_class, drmFence *fence)
2388 drm_fence_arg_t arg;
2390 memset(&arg, 0, sizeof(arg));
2392 arg.fence_class = fence_class;
2394 if (drmIoctl(fd, DRM_IOCTL_FENCE_BUFFERS, &arg))
2396 fence->handle = arg.handle;
2397 fence->fence_class = arg.fence_class;
2398 fence->type = arg.type;
2399 fence->flags = arg.flags;
2400 fence->sequence = arg.sequence;
2401 fence->signaled = 0;
2405 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2407 drm_fence_arg_t arg;
2409 memset(&arg, 0, sizeof(arg));
2410 arg.handle = handle;
2412 if (drmIoctl(fd, DRM_IOCTL_FENCE_REFERENCE, &arg))
2414 fence->handle = arg.handle;
2415 fence->fence_class = arg.fence_class;
2416 fence->type = arg.type;
2417 fence->flags = arg.flags;
2418 fence->signaled = arg.signaled;
2422 int drmFenceUnreference(int fd, const drmFence *fence)
2424 drm_fence_arg_t arg;
2426 memset(&arg, 0, sizeof(arg));
2427 arg.handle = fence->handle;
2429 if (drmIoctl(fd, DRM_IOCTL_FENCE_UNREFERENCE, &arg))
2434 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2436 drm_fence_arg_t arg;
2438 memset(&arg, 0, sizeof(arg));
2439 arg.handle = fence->handle;
2440 arg.type = flush_type;
2442 if (drmIoctl(fd, DRM_IOCTL_FENCE_FLUSH, &arg))
2444 fence->fence_class = arg.fence_class;
2445 fence->type = arg.type;
2446 fence->signaled = arg.signaled;
2450 int drmFenceUpdate(int fd, drmFence *fence)
2452 drm_fence_arg_t arg;
2454 memset(&arg, 0, sizeof(arg));
2455 arg.handle = fence->handle;
2457 if (drmIoctl(fd, DRM_IOCTL_FENCE_SIGNALED, &arg))
2459 fence->fence_class = arg.fence_class;
2460 fence->type = arg.type;
2461 fence->signaled = arg.signaled;
2465 int drmFenceSignaled(int fd, drmFence *fence, unsigned fenceType,
2468 if ((fence->flags & DRM_FENCE_FLAG_SHAREABLE) ||
2469 ((fenceType & fence->signaled) != fenceType)) {
2470 int ret = drmFenceFlush(fd, fence, fenceType);
2475 *signaled = ((fenceType & fence->signaled) == fenceType);
2482 * DRM_FENCE_FLAG_SHAREABLE
2483 * DRM_FENCE_MASK_DRIVER
2487 int drmFenceEmit(int fd, unsigned flags, drmFence *fence, unsigned emit_type)
2489 drm_fence_arg_t arg;
2491 memset(&arg, 0, sizeof(arg));
2492 arg.fence_class = fence->fence_class;
2494 arg.handle = fence->handle;
2495 arg.type = emit_type;
2497 if (drmIoctl(fd, DRM_IOCTL_FENCE_EMIT, &arg))
2499 fence->fence_class = arg.fence_class;
2500 fence->type = arg.type;
2501 fence->signaled = arg.signaled;
2502 fence->sequence = arg.sequence;
2508 * DRM_FENCE_FLAG_WAIT_LAZY
2509 * DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS
2512 #define DRM_IOCTL_TIMEOUT_USEC 3000000UL
2514 static unsigned long
2515 drmTimeDiff(struct timeval *now, struct timeval *then)
2519 val = now->tv_sec - then->tv_sec;
2521 val += now->tv_usec;
2522 val -= then->tv_usec;
2524 return (unsigned long) val;
2528 drmIoctlTimeout(int fd, unsigned long request, void *argp)
2531 struct timeval then, now;
2535 ret = drmIoctl(fd, request, argp);
2536 if (ret != 0 && errno == EAGAIN) {
2538 gettimeofday(&then, NULL);
2541 gettimeofday(&now, NULL);
2543 } while (ret != 0 && errno == EAGAIN &&
2544 drmTimeDiff(&now, &then) < DRM_IOCTL_TIMEOUT_USEC);
2547 return ((errno == EAGAIN) ? -EBUSY : -errno);
2555 int drmFenceWait(int fd, unsigned flags, drmFence *fence, unsigned flush_type)
2557 drm_fence_arg_t arg;
2560 if (flush_type == 0) {
2561 flush_type = fence->type;
2564 if (!(fence->flags & DRM_FENCE_FLAG_SHAREABLE)) {
2565 if ((flush_type & fence->signaled) == flush_type) {
2570 memset(&arg, 0, sizeof(arg));
2571 arg.handle = fence->handle;
2572 arg.type = flush_type;
2576 ret = drmIoctlTimeout(fd, DRM_IOCTL_FENCE_WAIT, &arg);
2580 fence->fence_class = arg.fence_class;
2581 fence->type = arg.type;
2582 fence->signaled = arg.signaled;
2586 static void drmBOCopyReply(const struct drm_bo_info_rep *rep, drmBO *buf)
2588 buf->handle = rep->handle;
2589 buf->flags = rep->flags;
2590 buf->size = rep->size;
2591 buf->offset = rep->offset;
2592 buf->mapHandle = rep->arg_handle;
2593 buf->proposedFlags = rep->proposed_flags;
2594 buf->start = rep->buffer_start;
2595 buf->fenceFlags = rep->fence_flags;
2596 buf->replyFlags = rep->rep_flags;
2597 buf->pageAlignment = rep->page_alignment;
2598 buf->tileInfo = rep->tile_info;
2599 buf->hwTileStride = rep->hw_tile_stride;
2600 buf->desiredTileStride = rep->desired_tile_stride;
2605 int drmBOCreate(int fd, unsigned long size,
2606 unsigned pageAlignment, void *user_buffer,
2608 unsigned hint, drmBO *buf)
2610 struct drm_bo_create_arg arg;
2611 struct drm_bo_create_req *req = &arg.d.req;
2612 struct drm_bo_info_rep *rep = &arg.d.rep;
2615 memset(buf, 0, sizeof(*buf));
2616 memset(&arg, 0, sizeof(arg));
2620 req->page_alignment = pageAlignment;
2621 req->buffer_start = (unsigned long) user_buffer;
2623 buf->virtual = NULL;
2625 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_CREATE, &arg);
2629 drmBOCopyReply(rep, buf);
2630 buf->virtual = user_buffer;
2636 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2638 struct drm_bo_reference_info_arg arg;
2639 struct drm_bo_handle_arg *req = &arg.d.req;
2640 struct drm_bo_info_rep *rep = &arg.d.rep;
2642 memset(&arg, 0, sizeof(arg));
2643 req->handle = handle;
2645 if (drmIoctl(fd, DRM_IOCTL_BO_REFERENCE, &arg))
2648 drmBOCopyReply(rep, buf);
2649 buf->mapVirtual = NULL;
2651 buf->virtual = NULL;
2656 int drmBOUnreference(int fd, drmBO *buf)
2658 struct drm_bo_handle_arg arg;
2660 if (buf->mapVirtual && buf->mapHandle) {
2661 (void) munmap(buf->mapVirtual, buf->start + buf->size);
2662 buf->mapVirtual = NULL;
2663 buf->virtual = NULL;
2666 memset(&arg, 0, sizeof(arg));
2667 arg.handle = buf->handle;
2669 if (drmIoctl(fd, DRM_IOCTL_BO_UNREFERENCE, &arg))
2678 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2679 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2680 * call return an -EBUSY if it can' immediately honor the mapping request.
2683 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2686 struct drm_bo_map_wait_idle_arg arg;
2687 struct drm_bo_info_req *req = &arg.d.req;
2688 struct drm_bo_info_rep *rep = &arg.d.rep;
2692 * Make sure we have a virtual address of the buffer.
2695 if (!buf->virtual) {
2697 virtual = mmap(0, buf->size + buf->start,
2698 PROT_READ | PROT_WRITE, MAP_SHARED,
2699 fd, buf->mapHandle);
2700 if (virtual == MAP_FAILED) {
2705 buf->mapVirtual = virtual;
2706 buf->virtual = ((char *) virtual) + buf->start;
2709 memset(&arg, 0, sizeof(arg));
2710 req->handle = buf->handle;
2711 req->mask = mapFlags;
2712 req->hint = mapHint;
2715 * May hang if the buffer object is busy.
2716 * This IOCTL synchronizes the buffer.
2719 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_MAP, &arg);
2723 drmBOCopyReply(rep, buf);
2724 buf->mapFlags = mapFlags;
2726 *address = buf->virtual;
2732 int drmBOUnmap(int fd, drmBO *buf)
2734 struct drm_bo_handle_arg arg;
2736 memset(&arg, 0, sizeof(arg));
2737 arg.handle = buf->handle;
2739 if (drmIoctl(fd, DRM_IOCTL_BO_UNMAP, &arg)) {
2746 int drmBOSetStatus(int fd, drmBO *buf,
2747 uint64_t flags, uint64_t mask,
2749 unsigned int desired_tile_stride,
2750 unsigned int tile_info)
2753 struct drm_bo_map_wait_idle_arg arg;
2754 struct drm_bo_info_req *req = &arg.d.req;
2755 struct drm_bo_info_rep *rep = &arg.d.rep;
2758 memset(&arg, 0, sizeof(arg));
2761 req->handle = buf->handle;
2763 req->desired_tile_stride = desired_tile_stride;
2764 req->tile_info = tile_info;
2766 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_SETSTATUS, &arg);
2770 drmBOCopyReply(rep, buf);
2775 int drmBOInfo(int fd, drmBO *buf)
2777 struct drm_bo_reference_info_arg arg;
2778 struct drm_bo_handle_arg *req = &arg.d.req;
2779 struct drm_bo_info_rep *rep = &arg.d.rep;
2782 memset(&arg, 0, sizeof(arg));
2783 req->handle = buf->handle;
2785 ret = drmIoctl(fd, DRM_IOCTL_BO_INFO, &arg);
2789 drmBOCopyReply(rep, buf);
2793 int drmBOWaitIdle(int fd, drmBO *buf, unsigned hint)
2795 struct drm_bo_map_wait_idle_arg arg;
2796 struct drm_bo_info_req *req = &arg.d.req;
2797 struct drm_bo_info_rep *rep = &arg.d.rep;
2800 if ((buf->flags & DRM_BO_FLAG_SHAREABLE) ||
2801 (buf->replyFlags & DRM_BO_REP_BUSY)) {
2802 memset(&arg, 0, sizeof(arg));
2803 req->handle = buf->handle;
2806 ret = drmIoctlTimeout(fd, DRM_IOCTL_BO_WAIT_IDLE, &arg);
2810 drmBOCopyReply(rep, buf);
2815 int drmBOBusy(int fd, drmBO *buf, int *busy)
2817 int ret = drmBOInfo(fd, buf);
2822 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2826 int drmMMInit(int fd, unsigned long pOffset, unsigned long pSize,
2829 struct drm_mm_init_arg arg;
2831 memset(&arg, 0, sizeof(arg));
2833 arg.magic = DRM_BO_INIT_MAGIC;
2834 arg.major = DRM_BO_INIT_MAJOR;
2835 arg.minor = DRM_BO_INIT_MINOR;
2836 arg.p_offset = pOffset;
2838 arg.mem_type = memType;
2840 if (drmIoctl(fd, DRM_IOCTL_MM_INIT, &arg))
2845 int drmMMTakedown(int fd, unsigned memType)
2847 struct drm_mm_type_arg arg;
2849 memset(&arg, 0, sizeof(arg));
2850 arg.mem_type = memType;
2852 if (drmIoctl(fd, DRM_IOCTL_MM_TAKEDOWN, &arg))
2858 * If this function returns an error, and lockBM was set to 1,
2859 * the buffer manager is NOT locked.
2862 int drmMMLock(int fd, unsigned memType, int lockBM, int ignoreNoEvict)
2864 struct drm_mm_type_arg arg;
2866 memset(&arg, 0, sizeof(arg));
2867 arg.mem_type = memType;
2868 arg.lock_flags |= (lockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2869 arg.lock_flags |= (ignoreNoEvict) ? DRM_BO_LOCK_IGNORE_NO_EVICT : 0;
2871 return drmIoctlTimeout(fd, DRM_IOCTL_MM_LOCK, &arg);
2874 int drmMMUnlock(int fd, unsigned memType, int unlockBM)
2876 struct drm_mm_type_arg arg;
2878 memset(&arg, 0, sizeof(arg));
2880 arg.mem_type = memType;
2881 arg.lock_flags |= (unlockBM) ? DRM_BO_LOCK_UNLOCK_BM : 0;
2883 return drmIoctlTimeout(fd, DRM_IOCTL_MM_UNLOCK, &arg);
2886 int drmMMInfo(int fd, unsigned memType, uint64_t *size)
2888 struct drm_mm_info_arg arg;
2890 memset(&arg, 0, sizeof(arg));
2892 arg.mem_type = memType;
2894 if (drmIoctl(fd, DRM_IOCTL_MM_INFO, &arg))
2901 int drmBOVersion(int fd, unsigned int *major,
2902 unsigned int *minor,
2903 unsigned int *patchlevel)
2905 struct drm_bo_version_arg arg;
2908 memset(&arg, 0, sizeof(arg));
2909 ret = drmIoctl(fd, DRM_IOCTL_BO_VERSION, &arg);
2918 *patchlevel = arg.patchlevel;
2925 #define DRM_MAX_FDS 16
2930 } connection[DRM_MAX_FDS];
2932 static int nr_fds = 0;
2934 int drmOpenOnce(void *unused,
2941 for (i = 0; i < nr_fds; i++)
2942 if (strcmp(BusID, connection[i].BusID) == 0) {
2943 connection[i].refcount++;
2945 return connection[i].fd;
2948 fd = drmOpen(unused, BusID);
2949 if (fd <= 0 || nr_fds == DRM_MAX_FDS)
2952 connection[nr_fds].BusID = strdup(BusID);
2953 connection[nr_fds].fd = fd;
2954 connection[nr_fds].refcount = 1;
2958 fprintf(stderr, "saved connection %d for %s %d\n",
2959 nr_fds, connection[nr_fds].BusID,
2960 strcmp(BusID, connection[nr_fds].BusID));
2967 void drmCloseOnce(int fd)
2971 for (i = 0; i < nr_fds; i++) {
2972 if (fd == connection[i].fd) {
2973 if (--connection[i].refcount == 0) {
2974 drmClose(connection[i].fd);
2975 free(connection[i].BusID);
2978 connection[i] = connection[nr_fds];