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.
34 #ifdef HAVE_XORG_CONFIG_H
35 #include <xorg-config.h>
40 # include "xf86_OSproc.h"
42 # include "xf86_ansic.h"
43 # define _DRM_MALLOC xalloc
44 # define _DRM_FREE xfree
45 # ifndef XFree86LOADER
46 # include <sys/mman.h>
57 # include <sys/types.h>
58 # include <sys/stat.h>
59 # define stat_t struct stat
60 # include <sys/ioctl.h>
61 # include <sys/mman.h>
62 # include <sys/time.h>
64 # define _DRM_MALLOC malloc
65 # define _DRM_FREE free
70 /* Not all systems have MAP_FAILED defined */
72 #define MAP_FAILED ((void *)-1)
77 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
90 #define DRM_MAJOR 226 /* Linux */
94 #define DRM_MAX_MINOR 16
98 * This definition needs to be changed on some systems if dev_t is a structure.
99 * If there is a header file we can get it from, there would be best.
102 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
105 #define DRM_MSG_VERBOSITY 3
108 * Output a message to stderr.
110 * \param format printf() like format string.
113 * This function is a wrapper around vfprintf().
116 drmMsg(const char *format, ...)
120 #ifndef XFree86Server
122 if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose"))
125 va_start(ap, format);
127 xf86VDrvMsgVerb(-1, X_NONE, DRM_MSG_VERBOSITY, format, ap);
129 vfprintf(stderr, format, ap);
135 static void *drmHashTable = NULL; /* Context switch callbacks */
137 typedef struct drmHashEntry {
139 void (*f)(int, void *, void *);
143 void *drmMalloc(int size)
146 if ((pt = _DRM_MALLOC(size))) memset(pt, 0, size);
150 void drmFree(void *pt)
152 if (pt) _DRM_FREE(pt);
155 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
156 static char *drmStrdup(const char *s)
163 retval = _DRM_MALLOC(strlen(s)+1);
173 static unsigned long drmGetKeyFromFd(int fd)
182 static drmHashEntry *drmGetEntry(int fd)
184 unsigned long key = drmGetKeyFromFd(fd);
188 if (!drmHashTable) drmHashTable = drmHashCreate();
190 if (drmHashLookup(drmHashTable, key, &value)) {
191 entry = drmMalloc(sizeof(*entry));
194 entry->tagTable = drmHashCreate();
195 drmHashInsert(drmHashTable, key, entry);
203 * Compare two busid strings
208 * \return 1 if matched.
211 * This function compares two bus ID strings. It understands the older
212 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
213 * domain, b is bus, d is device, f is function.
215 static int drmMatchBusID(const char *id1, const char *id2)
217 /* First, check if the IDs are exactly the same */
218 if (strcasecmp(id1, id2) == 0)
221 /* Try to match old/new-style PCI bus IDs. */
222 if (strncasecmp(id1, "pci", 3) == 0) {
227 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
230 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
235 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
238 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
243 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
252 * Open the DRM device, creating it if necessary.
254 * \param dev major and minor numbers of the device.
255 * \param minor minor number of the device.
257 * \return a file descriptor on success, or a negative value on error.
260 * Assembles the device name from \p minor and opens it, creating the device
261 * special file node with the major and minor numbers specified by \p dev and
262 * parent directory if necessary and was called by root.
264 static int drmOpenDevice(long dev, int minor)
269 mode_t devmode = DRM_DEV_MODE;
270 int isroot = !geteuid();
271 #if defined(XFree86Server)
272 uid_t user = DRM_DEV_UID;
273 gid_t group = DRM_DEV_GID;
276 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
277 drmMsg("drmOpenDevice: node name is %s\n", buf);
279 #if defined(XFree86Server)
280 devmode = xf86ConfigDRI.mode ? xf86ConfigDRI.mode : DRM_DEV_MODE;
281 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
282 group = (xf86ConfigDRI.group >= 0) ? xf86ConfigDRI.group : DRM_DEV_GID;
285 if (stat(DRM_DIR_NAME, &st)) {
286 if (!isroot) return DRM_ERR_NOT_ROOT;
287 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
288 chown(DRM_DIR_NAME, 0, 0); /* root:root */
289 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
292 /* Check if the device node exists and create it if necessary. */
293 if (stat(buf, &st)) {
294 if (!isroot) return DRM_ERR_NOT_ROOT;
296 mknod(buf, S_IFCHR | devmode, dev);
298 #if defined(XFree86Server)
299 chown(buf, user, group);
303 fd = open(buf, O_RDWR, 0);
304 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
305 fd, fd < 0 ? strerror(errno) : "OK");
306 if (fd >= 0) return fd;
308 /* Check if the device node is not what we expect it to be, and recreate it
309 * and try again if so.
311 if (st.st_rdev != dev) {
312 if (!isroot) return DRM_ERR_NOT_ROOT;
314 mknod(buf, S_IFCHR | devmode, dev);
315 #if defined(XFree86Server)
316 chown(buf, user, group);
320 fd = open(buf, O_RDWR, 0);
321 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
322 fd, fd < 0 ? strerror(errno) : "OK");
323 if (fd >= 0) return fd;
325 drmMsg("drmOpenDevice: Open failed\n");
332 * Open the DRM device
334 * \param minor device minor number.
335 * \param create allow to create the device if set.
337 * \return a file descriptor on success, or a negative value on error.
340 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
341 * name from \p minor and opens it.
343 static int drmOpenMinor(int minor, int create)
348 if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
350 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
351 if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
357 * Determine whether the DRM kernel driver has been loaded.
359 * \return 1 if the DRM driver is loaded, 0 otherwise.
362 * Determine the presence of the kernel driver by attempting to open the 0
363 * minor and get version information. For backward compatibility with older
364 * Linux implementations, /proc/dri is also checked.
366 int drmAvailable(void)
368 drmVersionPtr version;
372 if ((fd = drmOpenMinor(0, 1)) < 0) {
374 /* Try proc for backward Linux compatibility */
375 if (!access("/proc/dri/0", R_OK)) return 1;
380 if ((version = drmGetVersion(fd))) {
382 drmFreeVersion(version);
391 * Open the device by bus ID.
393 * \param busid bus ID.
395 * \return a file descriptor on success, or a negative value on error.
398 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
399 * comparing the device bus ID with the one supplied.
401 * \sa drmOpenMinor() and drmGetBusid().
403 static int drmOpenByBusid(const char *busid)
410 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
411 for (i = 0; i < DRM_MAX_MINOR; i++) {
412 fd = drmOpenMinor(i, 1);
413 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
417 sv.drm_dd_major = -1; /* Don't care */
418 sv.drm_dd_minor = -1; /* Don't care */
419 drmSetInterfaceVersion(fd, &sv);
420 buf = drmGetBusid(fd);
421 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
422 if (buf && drmMatchBusID(buf, busid)) {
426 if (buf) drmFreeBusid(buf);
435 * Open the device by name.
437 * \param name driver name.
439 * \return a file descriptor on success, or a negative value on error.
442 * This function opens the first minor number that matches the driver name and
443 * isn't already in use. If it's in use it then it will already have a bus ID
446 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
448 static int drmOpenByName(const char *name)
452 drmVersionPtr version;
455 if (!drmAvailable()) {
456 #if !defined(XFree86Server)
459 /* try to load the kernel module now */
460 if (!xf86LoadKernelModule(name)) {
461 ErrorF("[drm] failed to load kernel module \"%s\"\n",
469 * Open the first minor number that matches the driver name and isn't
470 * already in use. If it's in use it will have a busid assigned already.
472 for (i = 0; i < DRM_MAX_MINOR; i++) {
473 if ((fd = drmOpenMinor(i, 1)) >= 0) {
474 if ((version = drmGetVersion(fd))) {
475 if (!strcmp(version->name, name)) {
476 drmFreeVersion(version);
477 id = drmGetBusid(fd);
478 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
487 drmFreeVersion(version);
495 /* Backward-compatibility /proc support */
496 for (i = 0; i < 8; i++) {
497 char proc_name[64], buf[512];
498 char *driver, *pt, *devstring;
501 sprintf(proc_name, "/proc/dri/%d/name", i);
502 if ((fd = open(proc_name, 0, 0)) >= 0) {
503 retcode = read(fd, buf, sizeof(buf)-1);
506 buf[retcode-1] = '\0';
507 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
509 if (*pt) { /* Device is next */
511 if (!strcmp(driver, name)) { /* Match */
512 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
514 if (*pt) { /* Found busid */
515 return drmOpenByBusid(++pt);
516 } else { /* No busid */
517 return drmOpenDevice(strtol(devstring, NULL, 0),i);
531 * Open the DRM device.
533 * Looks up the specified name and bus ID, and opens the device found. The
534 * entry in /dev/dri is created if necessary and if called by root.
536 * \param name driver name. Not referenced if bus ID is supplied.
537 * \param busid bus ID. Zero if not known.
539 * \return a file descriptor on success, or a negative value on error.
542 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
545 int drmOpen(const char *name, const char *busid)
548 if (!drmAvailable() && name != NULL) {
549 /* try to load the kernel */
550 if (!xf86LoadKernelModule(name)) {
551 ErrorF("[drm] failed to load kernel module \"%s\"\n",
561 fd = drmOpenByBusid(busid);
567 return drmOpenByName(name);
574 * Free the version information returned by drmGetVersion().
576 * \param v pointer to the version information.
579 * It frees the memory pointed by \p %v as well as all the non-null strings
582 void drmFreeVersion(drmVersionPtr v)
585 if (v->name) drmFree(v->name);
586 if (v->date) drmFree(v->date);
587 if (v->desc) drmFree(v->desc);
593 * Free the non-public version information returned by the kernel.
595 * \param v pointer to the version information.
598 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
599 * the non-null strings pointers in it.
601 static void drmFreeKernelVersion(drm_version_t *v)
604 if (v->name) drmFree(v->name);
605 if (v->date) drmFree(v->date);
606 if (v->desc) drmFree(v->desc);
612 * Copy version information.
614 * \param d destination pointer.
615 * \param s source pointer.
618 * Used by drmGetVersion() to translate the information returned by the ioctl
619 * interface in a private structure into the public structure counterpart.
621 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
623 d->version_major = s->version_major;
624 d->version_minor = s->version_minor;
625 d->version_patchlevel = s->version_patchlevel;
626 d->name_len = s->name_len;
627 d->name = drmStrdup(s->name);
628 d->date_len = s->date_len;
629 d->date = drmStrdup(s->date);
630 d->desc_len = s->desc_len;
631 d->desc = drmStrdup(s->desc);
636 * Query the driver version information.
638 * \param fd file descriptor.
640 * \return pointer to a drmVersion structure which should be freed with
643 * \note Similar information is available via /proc/dri.
646 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
647 * first with zeros to get the string lengths, and then the actually strings.
648 * It also null-terminates them since they might not be already.
650 drmVersionPtr drmGetVersion(int fd)
652 drmVersionPtr retval;
653 drm_version_t *version = drmMalloc(sizeof(*version));
655 version->name_len = 0;
656 version->name = NULL;
657 version->date_len = 0;
658 version->date = NULL;
659 version->desc_len = 0;
660 version->desc = NULL;
662 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
663 drmFreeKernelVersion(version);
667 if (version->name_len)
668 version->name = drmMalloc(version->name_len + 1);
669 if (version->date_len)
670 version->date = drmMalloc(version->date_len + 1);
671 if (version->desc_len)
672 version->desc = drmMalloc(version->desc_len + 1);
674 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
675 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
676 drmFreeKernelVersion(version);
680 /* The results might not be null-terminated strings, so terminate them. */
681 if (version->name_len) version->name[version->name_len] = '\0';
682 if (version->date_len) version->date[version->date_len] = '\0';
683 if (version->desc_len) version->desc[version->desc_len] = '\0';
685 retval = drmMalloc(sizeof(*retval));
686 drmCopyVersion(retval, version);
687 drmFreeKernelVersion(version);
693 * Get version information for the DRM user space library.
695 * This version number is driver independent.
697 * \param fd file descriptor.
699 * \return version information.
702 * This function allocates and fills a drm_version structure with a hard coded
705 drmVersionPtr drmGetLibVersion(int fd)
707 drm_version_t *version = drmMalloc(sizeof(*version));
710 * revision 1.0.x = original DRM interface with no drmGetLibVersion
711 * entry point and many drm<Device> extensions
712 * revision 1.1.x = added drmCommand entry points for device extensions
713 * added drmGetLibVersion to identify libdrm.a version
714 * revision 1.2.x = added drmSetInterfaceVersion
715 * modified drmOpen to handle both busid and name
717 version->version_major = 1;
718 version->version_minor = 2;
719 version->version_patchlevel = 0;
721 return (drmVersionPtr)version;
726 * Free the bus ID information.
728 * \param busid bus ID information string as given by drmGetBusid().
731 * This function is just frees the memory pointed by \p busid.
733 void drmFreeBusid(const char *busid)
735 drmFree((void *)busid);
740 * Get the bus ID of the device.
742 * \param fd file descriptor.
744 * \return bus ID string.
747 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
748 * get the string length and data, passing the arguments in a drm_unique
751 char *drmGetBusid(int fd)
758 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
759 u.unique = drmMalloc(u.unique_len + 1);
760 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
761 u.unique[u.unique_len] = '\0';
768 * Set the bus ID of the device.
770 * \param fd file descriptor.
771 * \param busid bus ID string.
773 * \return zero on success, negative on failure.
776 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
777 * the arguments in a drm_unique structure.
779 int drmSetBusid(int fd, const char *busid)
783 u.unique = (char *)busid;
784 u.unique_len = strlen(busid);
786 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
792 int drmGetMagic(int fd, drm_magic_t * magic)
797 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) return -errno;
802 int drmAuthMagic(int fd, drm_magic_t magic)
807 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) return -errno;
812 * Specifies a range of memory that is available for mapping by a
815 * \param fd file descriptor.
816 * \param offset usually the physical address. The actual meaning depends of
817 * the \p type parameter. See below.
818 * \param size of the memory in bytes.
819 * \param type type of the memory to be mapped.
820 * \param flags combination of several flags to modify the function actions.
821 * \param handle will be set to a value that may be used as the offset
822 * parameter for mmap().
824 * \return zero on success or a negative value on error.
826 * \par Mapping the frame buffer
827 * For the frame buffer
828 * - \p offset will be the physical address of the start of the frame buffer,
829 * - \p size will be the size of the frame buffer in bytes, and
830 * - \p type will be DRM_FRAME_BUFFER.
833 * The area mapped will be uncached. If MTRR support is available in the
834 * kernel, the frame buffer area will be set to write combining.
836 * \par Mapping the MMIO register area
837 * For the MMIO register area,
838 * - \p offset will be the physical address of the start of the register area,
839 * - \p size will be the size of the register area bytes, and
840 * - \p type will be DRM_REGISTERS.
842 * The area mapped will be uncached.
844 * \par Mapping the SAREA
846 * - \p offset will be ignored and should be set to zero,
847 * - \p size will be the desired size of the SAREA in bytes,
848 * - \p type will be DRM_SHM.
851 * A shared memory area of the requested size will be created and locked in
852 * kernel memory. This area may be mapped into client-space by using the handle
855 * \note May only be called by root.
858 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
859 * the arguments in a drm_map structure.
861 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
862 drmMapFlags flags, drm_handle_t *handle)
871 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
872 if (handle) *handle = (drm_handle_t)map.handle;
876 int drmRmMap(int fd, drm_handle_t handle)
880 map.handle = (void *)handle;
882 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map)) return -errno;
887 * Make buffers available for DMA transfers.
889 * \param fd file descriptor.
890 * \param count number of buffers.
891 * \param size size of each buffer.
892 * \param flags buffer allocation flags.
893 * \param agp_offset offset in the AGP aperture
895 * \return number of buffers allocated, negative on error.
898 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
902 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
905 drm_buf_desc_t request;
907 request.count = count;
909 request.low_mark = 0;
910 request.high_mark = 0;
911 request.flags = flags;
912 request.agp_start = agp_offset;
914 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
915 return request.count;
918 int drmMarkBufs(int fd, double low, double high)
926 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return -EINVAL;
928 if (!info.count) return -EINVAL;
930 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
933 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
939 for (i = 0; i < info.count; i++) {
940 info.list[i].low_mark = low * info.list[i].count;
941 info.list[i].high_mark = high * info.list[i].count;
942 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
956 * \param fd file descriptor.
957 * \param count number of buffers to free.
958 * \param list list of buffers to be freed.
960 * \return zero on success, or a negative value on failure.
962 * \note This function is primarily used for debugging.
965 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
966 * the arguments in a drm_buf_free structure.
968 int drmFreeBufs(int fd, int count, int *list)
970 drm_buf_free_t request;
972 request.count = count;
974 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
982 * \param fd file descriptor.
985 * This function closes the file descriptor.
989 unsigned long key = drmGetKeyFromFd(fd);
990 drmHashEntry *entry = drmGetEntry(fd);
992 drmHashDestroy(entry->tagTable);
995 entry->tagTable = NULL;
997 drmHashDelete(drmHashTable, key);
1005 * Map a region of memory.
1007 * \param fd file descriptor.
1008 * \param handle handle returned by drmAddMap().
1009 * \param size size in bytes. Must match the size used by drmAddMap().
1010 * \param address will contain the user-space virtual address where the mapping
1013 * \return zero on success, or a negative value on failure.
1016 * This function is a wrapper for mmap().
1018 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1020 static unsigned long pagesize_mask = 0;
1022 if (fd < 0) return -EINVAL;
1025 pagesize_mask = getpagesize() - 1;
1027 size = (size + pagesize_mask) & ~pagesize_mask;
1029 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1030 if (*address == MAP_FAILED) return -errno;
1036 * Unmap mappings obtained with drmMap().
1038 * \param address address as given by drmMap().
1039 * \param size size in bytes. Must match the size used by drmMap().
1041 * \return zero on success, or a negative value on failure.
1044 * This function is a wrapper for munmap().
1046 int drmUnmap(drmAddress address, drmSize size)
1048 return munmap(address, size);
1051 drmBufInfoPtr drmGetBufInfo(int fd)
1053 drm_buf_info_t info;
1054 drmBufInfoPtr retval;
1060 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return NULL;
1063 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1066 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1071 retval = drmMalloc(sizeof(*retval));
1072 retval->count = info.count;
1073 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1074 for (i = 0; i < info.count; i++) {
1075 retval->list[i].count = info.list[i].count;
1076 retval->list[i].size = info.list[i].size;
1077 retval->list[i].low_mark = info.list[i].low_mark;
1078 retval->list[i].high_mark = info.list[i].high_mark;
1087 * Map all DMA buffers into client-virtual space.
1089 * \param fd file descriptor.
1091 * \return a pointer to a ::drmBufMap structure.
1093 * \note The client may not use these buffers until obtaining buffer indices
1097 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1098 * information about the buffers in a drm_buf_map structure into the
1099 * client-visible data structures.
1101 drmBufMapPtr drmMapBufs(int fd)
1104 drmBufMapPtr retval;
1109 bufs.virtual = NULL;
1110 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
1112 if (!bufs.count) return NULL;
1114 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1117 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1122 retval = drmMalloc(sizeof(*retval));
1123 retval->count = bufs.count;
1124 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1125 for (i = 0; i < bufs.count; i++) {
1126 retval->list[i].idx = bufs.list[i].idx;
1127 retval->list[i].total = bufs.list[i].total;
1128 retval->list[i].used = 0;
1129 retval->list[i].address = bufs.list[i].address;
1139 * Unmap buffers allocated with drmMapBufs().
1141 * \return zero on success, or negative value on failure.
1144 * Calls munmap() for every buffer stored in \p bufs and frees the
1145 * memory allocated by drmMapBufs().
1147 int drmUnmapBufs(drmBufMapPtr bufs)
1151 for (i = 0; i < bufs->count; i++) {
1152 munmap(bufs->list[i].address, bufs->list[i].total);
1155 drmFree(bufs->list);
1162 #define DRM_DMA_RETRY 16
1165 * Reserve DMA buffers.
1167 * \param fd file descriptor.
1170 * \return zero on success, or a negative value on failure.
1173 * Assemble the arguments into a drm_dma structure and keeps issuing the
1174 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1176 int drmDMA(int fd, drmDMAReqPtr request)
1181 dma.context = request->context;
1182 dma.send_count = request->send_count;
1183 dma.send_indices = request->send_list;
1184 dma.send_sizes = request->send_sizes;
1185 dma.flags = request->flags;
1186 dma.request_count = request->request_count;
1187 dma.request_size = request->request_size;
1188 dma.request_indices = request->request_list;
1189 dma.request_sizes = request->request_sizes;
1190 dma.granted_count = 0;
1193 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1194 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1197 request->granted_count = dma.granted_count;
1206 * Obtain heavyweight hardware lock.
1208 * \param fd file descriptor.
1209 * \param context context.
1210 * \param flags flags that determine the sate of the hardware when the function
1213 * \return always zero.
1216 * This function translates the arguments into a drm_lock structure and issue
1217 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1219 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1223 lock.context = context;
1225 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1226 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1227 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1228 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1229 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1230 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1232 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1238 * Release the hardware lock.
1240 * \param fd file descriptor.
1241 * \param context context.
1243 * \return zero on success, or a negative value on failure.
1246 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1247 * argument in a drm_lock structure.
1249 int drmUnlock(int fd, drm_context_t context)
1253 lock.context = context;
1255 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1258 drm_context_t *drmGetReservedContextList(int fd, int *count)
1262 drm_context_t * retval;
1266 res.contexts = NULL;
1267 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1269 if (!res.count) return NULL;
1271 if (!(list = drmMalloc(res.count * sizeof(*list)))) return NULL;
1272 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1277 res.contexts = list;
1278 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1280 for (i = 0; i < res.count; i++) retval[i] = list[i].handle;
1287 void drmFreeReservedContextList(drm_context_t *pt)
1295 * Used by the X server during GLXContext initialization. This causes
1296 * per-context kernel-level resources to be allocated.
1298 * \param fd file descriptor.
1299 * \param handle is set on success. To be used by the client when requesting DMA
1300 * dispatch with drmDMA().
1302 * \return zero on success, or a negative value on failure.
1304 * \note May only be called by root.
1307 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1308 * argument in a drm_ctx structure.
1310 int drmCreateContext(int fd, drm_context_t *handle)
1314 ctx.flags = 0; /* Modified with functions below */
1315 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
1316 *handle = ctx.handle;
1320 int drmSwitchToContext(int fd, drm_context_t context)
1324 ctx.handle = context;
1325 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) return -errno;
1329 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1334 * Context preserving means that no context switches are done between DMA
1335 * buffers from one context and the next. This is suitable for use in the
1336 * X server (which promises to maintain hardware context), or in the
1337 * client-side library when buffers are swapped on behalf of two threads.
1339 ctx.handle = context;
1341 if (flags & DRM_CONTEXT_PRESERVED) ctx.flags |= _DRM_CONTEXT_PRESERVED;
1342 if (flags & DRM_CONTEXT_2DONLY) ctx.flags |= _DRM_CONTEXT_2DONLY;
1343 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) return -errno;
1347 int drmGetContextFlags(int fd, drm_context_t context,
1348 drm_context_tFlagsPtr flags)
1352 ctx.handle = context;
1353 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx)) return -errno;
1355 if (ctx.flags & _DRM_CONTEXT_PRESERVED) *flags |= DRM_CONTEXT_PRESERVED;
1356 if (ctx.flags & _DRM_CONTEXT_2DONLY) *flags |= DRM_CONTEXT_2DONLY;
1363 * Free any kernel-level resources allocated with drmCreateContext() associated
1366 * \param fd file descriptor.
1367 * \param handle handle given by drmCreateContext().
1369 * \return zero on success, or a negative value on failure.
1371 * \note May only be called by root.
1374 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1375 * argument in a drm_ctx structure.
1377 int drmDestroyContext(int fd, drm_context_t handle)
1380 ctx.handle = handle;
1381 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
1385 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1388 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) return -errno;
1389 *handle = draw.handle;
1393 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1396 draw.handle = handle;
1397 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw)) return -errno;
1402 * Acquire the AGP device.
1404 * Must be called before any of the other AGP related calls.
1406 * \param fd file descriptor.
1408 * \return zero on success, or a negative value on failure.
1411 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1413 int drmAgpAcquire(int fd)
1415 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
1421 * Release the AGP device.
1423 * \param fd file descriptor.
1425 * \return zero on success, or a negative value on failure.
1428 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1430 int drmAgpRelease(int fd)
1432 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
1440 * \param fd file descriptor.
1441 * \param mode AGP mode.
1443 * \return zero on success, or a negative value on failure.
1446 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1447 * argument in a drm_agp_mode structure.
1449 int drmAgpEnable(int fd, unsigned long mode)
1454 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
1460 * Allocate a chunk of AGP memory.
1462 * \param fd file descriptor.
1463 * \param size requested memory size in bytes. Will be rounded to page boundary.
1464 * \param type type of memory to allocate.
1465 * \param address if not zero, will be set to the physical address of the
1467 * \param handle on success will be set to a handle of the allocated memory.
1469 * \return zero on success, or a negative value on failure.
1472 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1473 * arguments in a drm_agp_buffer structure.
1475 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1476 unsigned long *address, drm_handle_t *handle)
1480 *handle = DRM_AGP_NO_HANDLE;
1484 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
1485 if (address != 0UL) *address = b.physical;
1492 * Free a chunk of AGP memory.
1494 * \param fd file descriptor.
1495 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1497 * \return zero on success, or a negative value on failure.
1500 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1501 * argument in a drm_agp_buffer structure.
1503 int drmAgpFree(int fd, drm_handle_t handle)
1509 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
1515 * Bind a chunk of AGP memory.
1517 * \param fd file descriptor.
1518 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1519 * \param offset offset in bytes. It will round to page boundary.
1521 * \return zero on success, or a negative value on failure.
1524 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1525 * argument in a drm_agp_binding structure.
1527 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1529 drm_agp_binding_t b;
1533 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
1539 * Unbind a chunk of AGP memory.
1541 * \param fd file descriptor.
1542 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1544 * \return zero on success, or a negative value on failure.
1547 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1548 * the argument in a drm_agp_binding structure.
1550 int drmAgpUnbind(int fd, drm_handle_t handle)
1552 drm_agp_binding_t b;
1556 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
1562 * Get AGP driver major version number.
1564 * \param fd file descriptor.
1566 * \return major version number on success, or a negative value on failure..
1569 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1570 * necessary information in a drm_agp_info structure.
1572 int drmAgpVersionMajor(int fd)
1576 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1577 return i.agp_version_major;
1582 * Get AGP driver minor version number.
1584 * \param fd file descriptor.
1586 * \return minor version number on success, or a negative value on failure.
1589 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1590 * necessary information in a drm_agp_info structure.
1592 int drmAgpVersionMinor(int fd)
1596 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1597 return i.agp_version_minor;
1604 * \param fd file descriptor.
1606 * \return mode on success, or zero on failure.
1609 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1610 * necessary information in a drm_agp_info structure.
1612 unsigned long drmAgpGetMode(int fd)
1616 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1622 * Get AGP aperture base.
1624 * \param fd file descriptor.
1626 * \return aperture base on success, zero on failure.
1629 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1630 * necessary information in a drm_agp_info structure.
1632 unsigned long drmAgpBase(int fd)
1636 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1637 return i.aperture_base;
1642 * Get AGP aperture size.
1644 * \param fd file descriptor.
1646 * \return aperture size on success, zero on failure.
1649 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1650 * necessary information in a drm_agp_info structure.
1652 unsigned long drmAgpSize(int fd)
1656 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1657 return i.aperture_size;
1662 * Get used AGP memory.
1664 * \param fd file descriptor.
1666 * \return memory used on success, or zero on failure.
1669 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1670 * necessary information in a drm_agp_info structure.
1672 unsigned long drmAgpMemoryUsed(int fd)
1676 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1677 return i.memory_used;
1682 * Get available AGP memory.
1684 * \param fd file descriptor.
1686 * \return memory available on success, or zero on failure.
1689 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1690 * necessary information in a drm_agp_info structure.
1692 unsigned long drmAgpMemoryAvail(int fd)
1696 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1697 return i.memory_allowed;
1702 * Get hardware vendor ID.
1704 * \param fd file descriptor.
1706 * \return vendor ID on success, or zero on failure.
1709 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1710 * necessary information in a drm_agp_info structure.
1712 unsigned int drmAgpVendorId(int fd)
1716 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1722 * Get hardware device ID.
1724 * \param fd file descriptor.
1726 * \return zero on success, or zero on failure.
1729 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1730 * necessary information in a drm_agp_info structure.
1732 unsigned int drmAgpDeviceId(int fd)
1736 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1740 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1742 drm_scatter_gather_t sg;
1747 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
1748 *handle = sg.handle;
1752 int drmScatterGatherFree(int fd, drm_handle_t handle)
1754 drm_scatter_gather_t sg;
1758 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
1765 * \param fd file descriptor.
1766 * \param vbl pointer to a drmVBlank structure.
1768 * \return zero on success, or a negative value on failure.
1771 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1773 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1778 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1779 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1780 } while (ret && errno == EINTR);
1785 int drmError(int err, const char *label)
1788 case DRM_ERR_NO_DEVICE: fprintf(stderr, "%s: no device\n", label); break;
1789 case DRM_ERR_NO_ACCESS: fprintf(stderr, "%s: no access\n", label); break;
1790 case DRM_ERR_NOT_ROOT: fprintf(stderr, "%s: not root\n", label); break;
1791 case DRM_ERR_INVALID: fprintf(stderr, "%s: invalid args\n", label);break;
1793 if (err < 0) err = -err;
1794 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1802 * Install IRQ handler.
1804 * \param fd file descriptor.
1805 * \param irq IRQ number.
1807 * \return zero on success, or a negative value on failure.
1810 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1811 * argument in a drm_control structure.
1813 int drmCtlInstHandler(int fd, int irq)
1817 ctl.func = DRM_INST_HANDLER;
1819 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1825 * Uninstall IRQ handler.
1827 * \param fd file descriptor.
1829 * \return zero on success, or a negative value on failure.
1832 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1833 * argument in a drm_control structure.
1835 int drmCtlUninstHandler(int fd)
1839 ctl.func = DRM_UNINST_HANDLER;
1841 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1845 int drmFinish(int fd, int context, drmLockFlags flags)
1849 lock.context = context;
1851 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1852 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1853 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1854 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1855 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1856 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1857 if (ioctl(fd, DRM_IOCTL_FINISH, &lock)) return -errno;
1862 * Get IRQ from bus ID.
1864 * \param fd file descriptor.
1865 * \param busnum bus number.
1866 * \param devnum device number.
1867 * \param funcnum function number.
1869 * \return IRQ number on success, or a negative value on failure.
1872 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1873 * arguments in a drm_irq_busid structure.
1875 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1881 p.funcnum = funcnum;
1882 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
1886 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1888 drmHashEntry *entry = drmGetEntry(fd);
1890 if (drmHashInsert(entry->tagTable, context, tag)) {
1891 drmHashDelete(entry->tagTable, context);
1892 drmHashInsert(entry->tagTable, context, tag);
1897 int drmDelContextTag(int fd, drm_context_t context)
1899 drmHashEntry *entry = drmGetEntry(fd);
1901 return drmHashDelete(entry->tagTable, context);
1904 void *drmGetContextTag(int fd, drm_context_t context)
1906 drmHashEntry *entry = drmGetEntry(fd);
1909 if (drmHashLookup(entry->tagTable, context, &value)) return NULL;
1914 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
1915 drm_handle_t handle)
1917 drm_ctx_priv_map_t map;
1919 map.ctx_id = ctx_id;
1920 map.handle = (void *)handle;
1922 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) return -errno;
1926 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
1927 drm_handle_t *handle)
1929 drm_ctx_priv_map_t map;
1931 map.ctx_id = ctx_id;
1933 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) return -errno;
1934 if (handle) *handle = (drm_handle_t)map.handle;
1939 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
1940 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
1946 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map)) return -errno;
1947 *offset = map.offset;
1951 *handle = (unsigned long)map.handle;
1956 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
1957 unsigned long *magic, unsigned long *iocs)
1959 drm_client_t client;
1962 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client)) return -errno;
1963 *auth = client.auth;
1966 *magic = client.magic;
1967 *iocs = client.iocs;
1971 int drmGetStats(int fd, drmStatsT *stats)
1976 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s)) return -errno;
1979 memset(stats, 0, sizeof(*stats));
1980 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
1984 stats->data[i].long_format = "%-20.20s"; \
1985 stats->data[i].rate_format = "%8.8s"; \
1986 stats->data[i].isvalue = 1; \
1987 stats->data[i].verbose = 0
1990 stats->data[i].long_format = "%-20.20s"; \
1991 stats->data[i].rate_format = "%5.5s"; \
1992 stats->data[i].isvalue = 0; \
1993 stats->data[i].mult_names = "kgm"; \
1994 stats->data[i].mult = 1000; \
1995 stats->data[i].verbose = 0
1998 stats->data[i].long_format = "%-20.20s"; \
1999 stats->data[i].rate_format = "%5.5s"; \
2000 stats->data[i].isvalue = 0; \
2001 stats->data[i].mult_names = "KGM"; \
2002 stats->data[i].mult = 1024; \
2003 stats->data[i].verbose = 0
2006 stats->count = s.count;
2007 for (i = 0; i < s.count; i++) {
2008 stats->data[i].value = s.data[i].value;
2009 switch (s.data[i].type) {
2010 case _DRM_STAT_LOCK:
2011 stats->data[i].long_name = "Lock";
2012 stats->data[i].rate_name = "Lock";
2015 case _DRM_STAT_OPENS:
2016 stats->data[i].long_name = "Opens";
2017 stats->data[i].rate_name = "O";
2019 stats->data[i].verbose = 1;
2021 case _DRM_STAT_CLOSES:
2022 stats->data[i].long_name = "Closes";
2023 stats->data[i].rate_name = "Lock";
2025 stats->data[i].verbose = 1;
2027 case _DRM_STAT_IOCTLS:
2028 stats->data[i].long_name = "Ioctls";
2029 stats->data[i].rate_name = "Ioc/s";
2032 case _DRM_STAT_LOCKS:
2033 stats->data[i].long_name = "Locks";
2034 stats->data[i].rate_name = "Lck/s";
2037 case _DRM_STAT_UNLOCKS:
2038 stats->data[i].long_name = "Unlocks";
2039 stats->data[i].rate_name = "Unl/s";
2043 stats->data[i].long_name = "IRQs";
2044 stats->data[i].rate_name = "IRQ/s";
2047 case _DRM_STAT_PRIMARY:
2048 stats->data[i].long_name = "Primary Bytes";
2049 stats->data[i].rate_name = "PB/s";
2052 case _DRM_STAT_SECONDARY:
2053 stats->data[i].long_name = "Secondary Bytes";
2054 stats->data[i].rate_name = "SB/s";
2058 stats->data[i].long_name = "DMA";
2059 stats->data[i].rate_name = "DMA/s";
2062 case _DRM_STAT_SPECIAL:
2063 stats->data[i].long_name = "Special DMA";
2064 stats->data[i].rate_name = "dma/s";
2067 case _DRM_STAT_MISSED:
2068 stats->data[i].long_name = "Miss";
2069 stats->data[i].rate_name = "Ms/s";
2072 case _DRM_STAT_VALUE:
2073 stats->data[i].long_name = "Value";
2074 stats->data[i].rate_name = "Value";
2077 case _DRM_STAT_BYTE:
2078 stats->data[i].long_name = "Bytes";
2079 stats->data[i].rate_name = "B/s";
2082 case _DRM_STAT_COUNT:
2084 stats->data[i].long_name = "Count";
2085 stats->data[i].rate_name = "Cnt/s";
2094 * Issue a set-version ioctl.
2096 * \param fd file descriptor.
2097 * \param drmCommandIndex command index
2098 * \param data source pointer of the data to be read and written.
2099 * \param size size of the data to be read and written.
2101 * \return zero on success, or a negative value on failure.
2104 * It issues a read-write ioctl given by
2105 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2107 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2110 drm_set_version_t sv;
2112 sv.drm_di_major = version->drm_di_major;
2113 sv.drm_di_minor = version->drm_di_minor;
2114 sv.drm_dd_major = version->drm_dd_major;
2115 sv.drm_dd_minor = version->drm_dd_minor;
2117 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2121 version->drm_di_major = sv.drm_di_major;
2122 version->drm_di_minor = sv.drm_di_minor;
2123 version->drm_dd_major = sv.drm_dd_major;
2124 version->drm_dd_minor = sv.drm_dd_minor;
2130 * Send a device-specific command.
2132 * \param fd file descriptor.
2133 * \param drmCommandIndex command index
2135 * \return zero on success, or a negative value on failure.
2138 * It issues a ioctl given by
2139 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2141 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2143 void *data = NULL; /* dummy */
2144 unsigned long request;
2146 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2148 if (ioctl(fd, request, data)) {
2156 * Send a device-specific read command.
2158 * \param fd file descriptor.
2159 * \param drmCommandIndex command index
2160 * \param data destination pointer of the data to be read.
2161 * \param size size of the data to be read.
2163 * \return zero on success, or a negative value on failure.
2166 * It issues a read ioctl given by
2167 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2169 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2172 unsigned long request;
2174 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2175 DRM_COMMAND_BASE + drmCommandIndex, size);
2177 if (ioctl(fd, request, data)) {
2185 * Send a device-specific write command.
2187 * \param fd file descriptor.
2188 * \param drmCommandIndex command index
2189 * \param data source pointer of the data to be written.
2190 * \param size size of the data to be written.
2192 * \return zero on success, or a negative value on failure.
2195 * It issues a write ioctl given by
2196 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2198 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2201 unsigned long request;
2203 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2204 DRM_COMMAND_BASE + drmCommandIndex, size);
2206 if (ioctl(fd, request, data)) {
2214 * Send a device-specific read-write command.
2216 * \param fd file descriptor.
2217 * \param drmCommandIndex command index
2218 * \param data source pointer of the data to be read and written.
2219 * \param size size of the data to be read and written.
2221 * \return zero on success, or a negative value on failure.
2224 * It issues a read-write ioctl given by
2225 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2227 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2230 unsigned long request;
2232 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2233 DRM_COMMAND_BASE + drmCommandIndex, size);
2235 if (ioctl(fd, request, data)) {
2243 int drmFenceCreate(int fd, int shareable, int class,unsigned type,
2247 drm_fence_arg_t arg;
2251 arg.flags = (shareable) ? DRM_FENCE_FLAG_SHAREABLE : 0;
2252 arg.flags |= (emit) ? DRM_FENCE_FLAG_EMIT : 0;
2253 arg.op = drm_fence_create;
2254 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2256 fence->handle = arg.handle;
2257 fence->class = arg.class;
2258 fence->type = arg.type;
2259 fence->signaled = 0;
2263 int drmFenceBuffers(int fd, int shareable, drmFence *fence)
2265 drm_fence_arg_t arg;
2267 arg.flags = (shareable) ? DRM_FENCE_FLAG_SHAREABLE : 0;
2268 arg.op = drm_fence_buffers;
2269 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2271 fence->handle = arg.handle;
2272 fence->class = arg.class;
2273 fence->type = arg.type;
2274 fence->signaled = 0;
2278 int drmFenceDestroy(int fd, const drmFence *fence)
2280 drm_fence_arg_t arg;
2282 arg.handle = fence->handle;
2283 arg.op = drm_fence_destroy;
2284 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2289 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2291 drm_fence_arg_t arg;
2293 arg.handle = handle;
2294 arg.op = drm_fence_reference;
2295 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2297 fence->handle = arg.handle;
2298 fence->class = arg.class;
2299 fence->type = arg.type;
2300 fence->signaled = arg.signaled;
2304 int drmFenceUnreference(int fd, const drmFence *fence)
2306 drm_fence_arg_t arg;
2308 arg.handle = fence->handle;
2309 arg.op = drm_fence_unreference;
2310 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2315 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2317 drm_fence_arg_t arg;
2319 arg.handle = fence->handle;
2320 arg.type = flush_type;
2321 arg.op = drm_fence_flush;
2322 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2324 fence->class = arg.class;
2325 fence->type = arg.type;
2326 fence->signaled = arg.signaled;
2330 int drmFenceSignaled(int fd, drmFence *fence)
2332 drm_fence_arg_t arg;
2334 arg.handle = fence->handle;
2335 arg.op = drm_fence_signaled;
2336 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2338 fence->class = arg.class;
2339 fence->type = arg.type;
2340 fence->signaled = arg.signaled;
2344 int drmFenceEmit(int fd, drmFence *fence, unsigned emit_type)
2346 drm_fence_arg_t arg;
2348 arg.handle = fence->handle;
2349 arg.type = emit_type;
2350 arg.op = drm_fence_emit;
2351 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2353 fence->class = arg.class;
2354 fence->type = arg.type;
2355 fence->signaled = arg.signaled;
2359 int drmFenceWait(int fd, drmFence *fence, unsigned flush_type,
2360 int lazy, int ignore_signals)
2362 drm_fence_arg_t arg;
2365 arg.handle = fence->handle;
2366 arg.type = flush_type;
2367 arg.flags = (lazy) ? DRM_FENCE_FLAG_WAIT_LAZY : 0;
2368 arg.flags |= (ignore_signals) ? DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS : 0;
2369 arg.op = drm_fence_wait;
2371 ret = ioctl(fd, DRM_IOCTL_FENCE, &arg);
2372 } while (ret != 0 && errno == EAGAIN);
2377 fence->class = arg.class;
2378 fence->type = arg.type;
2379 fence->signaled = arg.signaled;
2383 int drmTTMCreate(int fd, drmTTM *ttm, unsigned long size, unsigned flags)
2387 arg.op = drm_ttm_create;
2391 if (ioctl(fd, DRM_IOCTL_TTM, &arg))
2394 ttm->handle = arg.handle;
2395 ttm->user_token = (drm_handle_t) arg.user_token;
2396 ttm->flags = arg.flags;
2397 ttm->size = arg.size;
2398 ttm->virtual = NULL;
2403 int drmTTMDestroy(int fd, const drmTTM *ttm)
2407 arg.op = drm_ttm_destroy;
2408 arg.handle = ttm->handle;
2409 if (ioctl(fd, DRM_IOCTL_TTM, &arg))
2415 int drmTTMReference(int fd, unsigned handle, drmTTM *ttm)
2419 arg.handle = handle;
2420 arg.op = drm_ttm_reference;
2421 if (ioctl(fd, DRM_IOCTL_TTM, &arg))
2423 ttm->handle = arg.handle;
2424 ttm->user_token = (drm_handle_t) arg.user_token;
2425 ttm->flags = arg.flags;
2426 ttm->size = arg.size;
2430 int drmTTMUnreference(int fd, const drmTTM *ttm)
2434 arg.op = drm_ttm_destroy;
2435 arg.handle = ttm->handle;
2436 if (ioctl(fd, DRM_IOCTL_TTM, &arg))
2441 drm_handle_t drmTTMMapHandle(int fd, const drmTTM *ttm)
2444 return ttm->user_token;
2447 static int drmAdjustListNodes(drmBOList *list)
2453 while(list->numCurrent < list->numTarget) {
2454 node = (drmBONode *) malloc(sizeof(*node));
2460 DRMLISTADD(&node->head, &list->free);
2463 while(list->numCurrent > list->numTarget) {
2464 l = list->free.next;
2465 if (l == &list->free)
2468 node = DRMLISTENTRY(drmBONode, l, head);
2475 void drmBOFreeList(drmBOList *list)
2481 l = list->list.next;
2482 while(l != &list->list) {
2484 node = DRMLISTENTRY(drmBONode, l, head);
2486 l = list->free.next;
2491 l = list->free.next;
2492 while(l != &list->free) {
2494 node = DRMLISTENTRY(drmBONode, l, head);
2496 l = list->free.next;
2501 int drmBOResetList(drmBOList *list) {
2506 ret = drmAdjustListNodes(list);
2510 l = list->list.next;
2511 while(l != &list->list) {
2513 DRMLISTADD(l, &list->free);
2516 return drmAdjustListNodes(list);
2519 static drmBONode *drmAddListItem(drmBOList *list, drmBO *item,
2526 l = list->free.next;
2527 if (l == &list->free) {
2528 node = (drmBONode *) malloc(sizeof(*node));
2535 node = DRMLISTENTRY(drmBONode, l, head);
2540 DRMLISTADD(&node->head, &list->list);
2545 void *drmBOListIterator(drmBOList *list)
2547 void *ret = list->list.next;
2549 if (ret == &list->list)
2554 void *drmBOListNext(drmBOList *list, void *iterator)
2558 drmMMListHead *l = (drmMMListHead *) iterator;
2560 if (ret == &list->list)
2565 drmBO *drmBOListBuf(void *iterator)
2568 drmMMListHead *l = (drmMMListHead *) iterator;
2569 node = DRMLISTENTRY(drmBONode, l, head);
2575 int drmBOCreateList(int numTarget, drmBOList *list)
2577 DRMINITLISTHEAD(&list->list);
2578 DRMINITLISTHEAD(&list->free);
2579 list->numTarget = numTarget;
2580 list->numCurrent = 0;
2581 list->numOnList = 0;
2582 return drmAdjustListNodes(list);
2585 static void drmBOCopyReply(const drm_bo_arg_reply_t *rep,
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->mask = rep->mask;
2594 buf->start = rep->buffer_start;
2595 buf->fenceFlags = rep->fence_flags;
2596 buf->replyFlags = rep->rep_flags;
2601 int drmBOCreate(int fd, drmTTM *ttm, unsigned long start, unsigned long size,
2602 void *user_buffer, drm_bo_type_t type, unsigned mask,
2603 unsigned hint, drmBO *buf)
2606 drm_bo_arg_request_t *req = &arg.req;
2607 drm_bo_arg_reply_t *rep = &arg.rep;
2616 buf->virtual = NULL;
2619 case drm_bo_type_ttm:
2620 req->arg_handle = ttm->handle;
2621 req->buffer_start = start;
2624 case drm_bo_type_dc:
2625 req->buffer_start = start;
2627 case drm_bo_type_user:
2628 req->buffer_start = (unsigned long) user_buffer;
2629 buf->virtual = user_buffer;
2634 req->op = drm_bo_create;
2637 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2640 fprintf(stderr, "Not handled\n");
2647 drmBOCopyReply(rep, buf);
2648 buf->mapVirtual = NULL;
2654 int drmBODestroy(int fd, drmBO *buf)
2657 drm_bo_arg_request_t *req = &arg.req;
2658 drm_bo_arg_reply_t *rep = &arg.rep;
2660 if (buf->mapVirtual) {
2661 (void) drmUnmap(buf->mapVirtual, buf->start + buf->size);
2662 buf->mapVirtual = NULL;
2663 buf->virtual = NULL;
2667 req->handle = buf->handle;
2668 req->op = drm_bo_destroy;
2671 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2684 int drmBOReference(int fd, unsigned handle, drmBO *buf)
2688 drm_bo_arg_request_t *req = &arg.req;
2689 drm_bo_arg_reply_t *rep = &arg.rep;
2692 req->handle = handle;
2693 req->op = drm_bo_reference;
2696 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2705 drmBOCopyReply(rep, buf);
2706 buf->type = drm_bo_type_dc;
2707 buf->mapVirtual = NULL;
2709 buf->virtual = NULL;
2714 int drmBOUnReference(int fd, drmBO *buf)
2717 drm_bo_arg_request_t *req = &arg.req;
2718 drm_bo_arg_reply_t *rep = &arg.rep;
2721 req->handle = buf->handle;
2722 req->op = drm_bo_unreference;
2725 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg))
2739 * Flags can be DRM_BO_FLAG_READ, DRM_BO_FLAG_WRITE or'ed together
2740 * Hint currently be DRM_BO_HINT_DONT_BLOCK, which makes the
2741 * call return an -EBUSY if it can' immediately honor the mapping request.
2744 int drmBOMap(int fd, drmBO *buf, unsigned mapFlags, unsigned mapHint,
2749 drm_bo_arg_request_t *req = &arg.req;
2750 drm_bo_arg_reply_t *rep = &arg.rep;
2754 * Make sure we have a virtual address of the buffer.
2757 if (!buf->virtual) {
2759 ret = drmMap(fd, buf->mapHandle, buf->size + buf->start, &virtual);
2762 buf->mapVirtual = virtual;
2763 buf->virtual = ((char *) virtual) + buf->start;
2764 fprintf(stderr,"Mapvirtual, virtual: 0x%08x 0x%08x\n",
2765 buf->mapVirtual, buf->virtual);
2769 req->handle = buf->handle;
2770 req->mask = mapFlags;
2771 req->hint = mapHint;
2772 req->op = drm_bo_map;
2776 * May hang if the buffer object is busy.
2777 * This IOCTL synchronizes the buffer.
2781 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2782 } while (ret != 0 && errno == EAGAIN);
2791 drmBOCopyReply(rep, buf);
2792 buf->mapFlags = mapFlags;
2794 *address = buf->virtual;
2799 int drmBOUnmap(int fd, drmBO *buf)
2802 drm_bo_arg_request_t *req = &arg.req;
2803 drm_bo_arg_reply_t *rep = &arg.rep;
2807 req->handle = buf->handle;
2808 req->op = drm_bo_unmap;
2811 if (ioctl(fd, DRM_IOCTL_BUFOBJ, &arg)) {
2822 int drmBOValidate(int fd, drmBO *buf, unsigned flags, unsigned mask,
2826 drm_bo_arg_request_t *req = &arg.req;
2827 drm_bo_arg_reply_t *rep = &arg.rep;
2831 req->handle = buf->handle;
2834 req->arg_handle = mask; /* Encode mask in the arg_handle field :/ */
2835 req->op = drm_bo_validate;
2840 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2841 } while (ret && errno == -EAGAIN);
2851 drmBOCopyReply(rep, buf);
2856 int drmBOFence(int fd, drmBO *buf, unsigned flags, unsigned fenceHandle)
2859 drm_bo_arg_request_t *req = &arg.req;
2860 drm_bo_arg_reply_t *rep = &arg.rep;
2864 req->handle = buf->handle;
2866 req->arg_handle = fenceHandle;
2867 req->op = drm_bo_validate;
2870 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2881 int drmBOInfo(int fd, drmBO *buf)
2884 drm_bo_arg_request_t *req = &arg.req;
2885 drm_bo_arg_reply_t *rep = &arg.rep;
2889 req->handle = buf->handle;
2890 req->op = drm_bo_info;
2893 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
2901 drmBOCopyReply(rep, buf);
2905 int drmBufBusy(int fd, drmBO *buf, int *busy)
2907 if (!(buf->flags & DRM_BO_FLAG_SHAREABLE) &&
2908 !(buf->replyFlags & DRM_BO_REP_BUSY)) {
2912 int ret = drmBOInfo(fd, buf);
2915 *busy = (buf->replyFlags & DRM_BO_REP_BUSY);
2921 int drmAddValidateItem(drmBOList *list, drmBO *buf, unsigned flags,
2925 drmBONode *node, *cur;
2926 unsigned oldFlags, newFlags;
2932 mask &= ~DRM_BO_MASK_MEM;
2934 for (l = list->list.next; l != &list->list; l = l->next) {
2935 node = DRMLISTENTRY(drmBONode, l, head);
2936 if (node->buf == buf) {
2942 cur = drmAddListItem(list, buf, flags, mask);
2944 drmMsg("Out of memory creating validate list node.\n");
2951 unsigned memFlags = cur->arg0 & DRM_BO_MASK_MEM;
2953 if (!(memFlags & flags)) {
2954 drmMsg("Incompatible memory location requests "
2955 "on validate list.\n");
2958 if ((cur->arg1 | mask) & (cur->arg0 ^ flags)) {
2959 drmMsg("Incompatible buffer flag requests "
2960 " on validate list.\n");
2964 cur->arg0 = (memFlags & flags) | ((cur->arg0 | flags) & cur->arg1);
2969 int drmBOValidateList(int fd, drmBOList *list)
2974 drm_bo_arg_t *arg, *first;
2975 drm_bo_arg_request_t *req;
2976 drm_bo_arg_reply_t *rep;
2977 drm_u64_t *prevNext = NULL;
2983 for (l = list->list.next; l != &list->list; l = l->next) {
2984 node = DRMLISTENTRY(drmBONode, l, head);
2986 arg = &node->bo_arg;
2993 *prevNext = (unsigned long) arg;
2996 prevNext = &req->next;
2998 req->handle = node->buf->handle;
2999 req->op = drm_bo_validate;
3000 req->mask = node->arg0;
3002 req->arg_handle = node->arg1;
3009 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
3010 } while (ret && errno == -EAGAIN);
3016 for (l = list->list.next; l != &list->list; l = l->next) {
3017 node = DRMLISTENTRY(drmBONode, l, head);
3019 arg = &node->bo_arg;
3028 drmBOCopyReply(rep, buf);
3035 int drmBOFenceList(int fd, drmBOList *list, unsigned fenceHandle)
3040 drm_bo_arg_t *arg, *first;
3041 drm_bo_arg_request_t *req;
3042 drm_bo_arg_reply_t *rep;
3043 drm_u64_t *prevNext = NULL;
3045 unsigned fence_flags;
3050 for (l = list->list.next; l != &list->list; l = l->next) {
3051 node = DRMLISTENTRY(drmBONode, l, head);
3053 arg = &node->bo_arg;
3060 *prevNext = (unsigned long) arg;
3063 prevNext = &req->next;
3065 req->handle = node->buf->handle;
3066 req->op = drm_bo_fence;
3067 req->mask = node->arg0;
3068 req->arg_handle = fenceHandle;
3074 ret = ioctl(fd, DRM_IOCTL_BUFOBJ, &arg);
3079 for (l = list->list.next; l != &list->list; l = l->next) {
3080 node = DRMLISTENTRY(drmBONode, l, head);
3082 arg = &node->bo_arg;
3089 drmBOCopyReply(rep, buf);
3095 int drmMMInit(int fd, unsigned long vramPOffset, unsigned long vramPSize,
3096 unsigned long ttPOffset, unsigned long ttPSize)
3098 drm_mm_init_arg_t arg;
3100 arg.req.op = mm_init;
3101 arg.req.vr_p_offset = vramPOffset;
3102 arg.req.vr_p_size = vramPSize;
3103 arg.req.tt_p_offset = ttPOffset;
3104 arg.req.tt_p_size = ttPSize;
3106 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))
3112 int drmMMTakedown(int fd)
3114 drm_mm_init_arg_t arg;
3115 arg.req.op = mm_takedown;
3117 if (ioctl(fd, DRM_IOCTL_MM_INIT, &arg))