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
69 /* Not all systems have MAP_FAILED defined */
71 #define MAP_FAILED ((void *)-1)
76 #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
89 #define DRM_MAJOR 226 /* Linux */
93 #define DRM_MAX_MINOR 16
97 * This definition needs to be changed on some systems if dev_t is a structure.
98 * If there is a header file we can get it from, there would be best.
101 #define makedev(x,y) ((dev_t)(((x) << 8) | (y)))
104 #define DRM_MSG_VERBOSITY 3
107 * Output a message to stderr.
109 * \param format printf() like format string.
112 * This function is a wrapper around vfprintf().
115 drmMsg(const char *format, ...)
119 #ifndef XFree86Server
121 if ((env = getenv("LIBGL_DEBUG")) && strstr(env, "verbose"))
124 va_start(ap, format);
126 xf86VDrvMsgVerb(-1, X_NONE, DRM_MSG_VERBOSITY, format, ap);
128 vfprintf(stderr, format, ap);
134 static void *drmHashTable = NULL; /* Context switch callbacks */
136 typedef struct drmHashEntry {
138 void (*f)(int, void *, void *);
142 void *drmMalloc(int size)
145 if ((pt = _DRM_MALLOC(size))) memset(pt, 0, size);
149 void drmFree(void *pt)
151 if (pt) _DRM_FREE(pt);
154 /* drmStrdup can't use strdup(3), since it doesn't call _DRM_MALLOC... */
155 static char *drmStrdup(const char *s)
162 retval = _DRM_MALLOC(strlen(s)+1);
172 static unsigned long drmGetKeyFromFd(int fd)
181 static drmHashEntry *drmGetEntry(int fd)
183 unsigned long key = drmGetKeyFromFd(fd);
187 if (!drmHashTable) drmHashTable = drmHashCreate();
189 if (drmHashLookup(drmHashTable, key, &value)) {
190 entry = drmMalloc(sizeof(*entry));
193 entry->tagTable = drmHashCreate();
194 drmHashInsert(drmHashTable, key, entry);
202 * Compare two busid strings
207 * \return 1 if matched.
210 * This function compares two bus ID strings. It understands the older
211 * PCI:b:d:f format and the newer pci:oooo:bb:dd.f format. In the format, o is
212 * domain, b is bus, d is device, f is function.
214 static int drmMatchBusID(const char *id1, const char *id2)
216 /* First, check if the IDs are exactly the same */
217 if (strcasecmp(id1, id2) == 0)
220 /* Try to match old/new-style PCI bus IDs. */
221 if (strncasecmp(id1, "pci", 3) == 0) {
226 ret = sscanf(id1, "pci:%04x:%02x:%02x.%d", &o1, &b1, &d1, &f1);
229 ret = sscanf(id1, "PCI:%d:%d:%d", &b1, &d1, &f1);
234 ret = sscanf(id2, "pci:%04x:%02x:%02x.%d", &o2, &b2, &d2, &f2);
237 ret = sscanf(id2, "PCI:%d:%d:%d", &b2, &d2, &f2);
242 if ((o1 != o2) || (b1 != b2) || (d1 != d2) || (f1 != f2))
251 * Open the DRM device, creating it if necessary.
253 * \param dev major and minor numbers of the device.
254 * \param minor minor number of the device.
256 * \return a file descriptor on success, or a negative value on error.
259 * Assembles the device name from \p minor and opens it, creating the device
260 * special file node with the major and minor numbers specified by \p dev and
261 * parent directory if necessary and was called by root.
263 static int drmOpenDevice(long dev, int minor)
268 mode_t devmode = DRM_DEV_MODE;
269 int isroot = !geteuid();
270 #if defined(XFree86Server)
271 uid_t user = DRM_DEV_UID;
272 gid_t group = DRM_DEV_GID;
275 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
276 drmMsg("drmOpenDevice: node name is %s\n", buf);
278 #if defined(XFree86Server)
279 devmode = xf86ConfigDRI.mode ? xf86ConfigDRI.mode : DRM_DEV_MODE;
280 devmode &= ~(S_IXUSR|S_IXGRP|S_IXOTH);
281 group = (xf86ConfigDRI.group >= 0) ? xf86ConfigDRI.group : DRM_DEV_GID;
284 if (stat(DRM_DIR_NAME, &st)) {
285 if (!isroot) return DRM_ERR_NOT_ROOT;
286 mkdir(DRM_DIR_NAME, DRM_DEV_DIRMODE);
287 chown(DRM_DIR_NAME, 0, 0); /* root:root */
288 chmod(DRM_DIR_NAME, DRM_DEV_DIRMODE);
291 /* Check if the device node exists and create it if necessary. */
292 if (stat(buf, &st)) {
293 if (!isroot) return DRM_ERR_NOT_ROOT;
295 mknod(buf, S_IFCHR | devmode, dev);
297 #if defined(XFree86Server)
298 chown(buf, user, group);
302 fd = open(buf, O_RDWR, 0);
303 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
304 fd, fd < 0 ? strerror(errno) : "OK");
305 if (fd >= 0) return fd;
307 /* Check if the device node is not what we expect it to be, and recreate it
308 * and try again if so.
310 if (st.st_rdev != dev) {
311 if (!isroot) return DRM_ERR_NOT_ROOT;
313 mknod(buf, S_IFCHR | devmode, dev);
314 #if defined(XFree86Server)
315 chown(buf, user, group);
319 fd = open(buf, O_RDWR, 0);
320 drmMsg("drmOpenDevice: open result is %d, (%s)\n",
321 fd, fd < 0 ? strerror(errno) : "OK");
322 if (fd >= 0) return fd;
324 drmMsg("drmOpenDevice: Open failed\n");
331 * Open the DRM device
333 * \param minor device minor number.
334 * \param create allow to create the device if set.
336 * \return a file descriptor on success, or a negative value on error.
339 * Calls drmOpenDevice() if \p create is set, otherwise assembles the device
340 * name from \p minor and opens it.
342 static int drmOpenMinor(int minor, int create)
347 if (create) return drmOpenDevice(makedev(DRM_MAJOR, minor), minor);
349 sprintf(buf, DRM_DEV_NAME, DRM_DIR_NAME, minor);
350 if ((fd = open(buf, O_RDWR, 0)) >= 0) return fd;
356 * Determine whether the DRM kernel driver has been loaded.
358 * \return 1 if the DRM driver is loaded, 0 otherwise.
361 * Determine the presence of the kernel driver by attempting to open the 0
362 * minor and get version information. For backward compatibility with older
363 * Linux implementations, /proc/dri is also checked.
365 int drmAvailable(void)
367 drmVersionPtr version;
371 if ((fd = drmOpenMinor(0, 1)) < 0) {
373 /* Try proc for backward Linux compatibility */
374 if (!access("/proc/dri/0", R_OK)) return 1;
379 if ((version = drmGetVersion(fd))) {
381 drmFreeVersion(version);
390 * Open the device by bus ID.
392 * \param busid bus ID.
394 * \return a file descriptor on success, or a negative value on error.
397 * This function attempts to open every possible minor (up to DRM_MAX_MINOR),
398 * comparing the device bus ID with the one supplied.
400 * \sa drmOpenMinor() and drmGetBusid().
402 static int drmOpenByBusid(const char *busid)
409 drmMsg("drmOpenByBusid: Searching for BusID %s\n", busid);
410 for (i = 0; i < DRM_MAX_MINOR; i++) {
411 fd = drmOpenMinor(i, 1);
412 drmMsg("drmOpenByBusid: drmOpenMinor returns %d\n", fd);
416 sv.drm_dd_major = -1; /* Don't care */
417 sv.drm_dd_minor = -1; /* Don't care */
418 drmSetInterfaceVersion(fd, &sv);
419 buf = drmGetBusid(fd);
420 drmMsg("drmOpenByBusid: drmGetBusid reports %s\n", buf);
421 if (buf && drmMatchBusID(buf, busid)) {
425 if (buf) drmFreeBusid(buf);
434 * Open the device by name.
436 * \param name driver name.
438 * \return a file descriptor on success, or a negative value on error.
441 * This function opens the first minor number that matches the driver name and
442 * isn't already in use. If it's in use it then it will already have a bus ID
445 * \sa drmOpenMinor(), drmGetVersion() and drmGetBusid().
447 static int drmOpenByName(const char *name)
451 drmVersionPtr version;
454 if (!drmAvailable()) {
455 #if !defined(XFree86Server)
458 /* try to load the kernel module now */
459 if (!xf86LoadKernelModule(name)) {
460 ErrorF("[drm] failed to load kernel module \"%s\"\n",
468 * Open the first minor number that matches the driver name and isn't
469 * already in use. If it's in use it will have a busid assigned already.
471 for (i = 0; i < DRM_MAX_MINOR; i++) {
472 if ((fd = drmOpenMinor(i, 1)) >= 0) {
473 if ((version = drmGetVersion(fd))) {
474 if (!strcmp(version->name, name)) {
475 drmFreeVersion(version);
476 id = drmGetBusid(fd);
477 drmMsg("drmGetBusid returned '%s'\n", id ? id : "NULL");
486 drmFreeVersion(version);
494 /* Backward-compatibility /proc support */
495 for (i = 0; i < 8; i++) {
496 char proc_name[64], buf[512];
497 char *driver, *pt, *devstring;
500 sprintf(proc_name, "/proc/dri/%d/name", i);
501 if ((fd = open(proc_name, 0, 0)) >= 0) {
502 retcode = read(fd, buf, sizeof(buf)-1);
505 buf[retcode-1] = '\0';
506 for (driver = pt = buf; *pt && *pt != ' '; ++pt)
508 if (*pt) { /* Device is next */
510 if (!strcmp(driver, name)) { /* Match */
511 for (devstring = ++pt; *pt && *pt != ' '; ++pt)
513 if (*pt) { /* Found busid */
514 return drmOpenByBusid(++pt);
515 } else { /* No busid */
516 return drmOpenDevice(strtol(devstring, NULL, 0),i);
530 * Open the DRM device.
532 * Looks up the specified name and bus ID, and opens the device found. The
533 * entry in /dev/dri is created if necessary and if called by root.
535 * \param name driver name. Not referenced if bus ID is supplied.
536 * \param busid bus ID. Zero if not known.
538 * \return a file descriptor on success, or a negative value on error.
541 * It calls drmOpenByBusid() if \p busid is specified or drmOpenByName()
544 int drmOpen(const char *name, const char *busid)
547 if (!drmAvailable() && name != NULL) {
548 /* try to load the kernel */
549 if (!xf86LoadKernelModule(name)) {
550 ErrorF("[drm] failed to load kernel module \"%s\"\n",
560 fd = drmOpenByBusid(busid);
566 return drmOpenByName(name);
573 * Free the version information returned by drmGetVersion().
575 * \param v pointer to the version information.
578 * It frees the memory pointed by \p %v as well as all the non-null strings
581 void drmFreeVersion(drmVersionPtr v)
584 if (v->name) drmFree(v->name);
585 if (v->date) drmFree(v->date);
586 if (v->desc) drmFree(v->desc);
592 * Free the non-public version information returned by the kernel.
594 * \param v pointer to the version information.
597 * Used by drmGetVersion() to free the memory pointed by \p %v as well as all
598 * the non-null strings pointers in it.
600 static void drmFreeKernelVersion(drm_version_t *v)
603 if (v->name) drmFree(v->name);
604 if (v->date) drmFree(v->date);
605 if (v->desc) drmFree(v->desc);
611 * Copy version information.
613 * \param d destination pointer.
614 * \param s source pointer.
617 * Used by drmGetVersion() to translate the information returned by the ioctl
618 * interface in a private structure into the public structure counterpart.
620 static void drmCopyVersion(drmVersionPtr d, const drm_version_t *s)
622 d->version_major = s->version_major;
623 d->version_minor = s->version_minor;
624 d->version_patchlevel = s->version_patchlevel;
625 d->name_len = s->name_len;
626 d->name = drmStrdup(s->name);
627 d->date_len = s->date_len;
628 d->date = drmStrdup(s->date);
629 d->desc_len = s->desc_len;
630 d->desc = drmStrdup(s->desc);
635 * Query the driver version information.
637 * \param fd file descriptor.
639 * \return pointer to a drmVersion structure which should be freed with
642 * \note Similar information is available via /proc/dri.
645 * It gets the version information via successive DRM_IOCTL_VERSION ioctls,
646 * first with zeros to get the string lengths, and then the actually strings.
647 * It also null-terminates them since they might not be already.
649 drmVersionPtr drmGetVersion(int fd)
651 drmVersionPtr retval;
652 drm_version_t *version = drmMalloc(sizeof(*version));
654 version->name_len = 0;
655 version->name = NULL;
656 version->date_len = 0;
657 version->date = NULL;
658 version->desc_len = 0;
659 version->desc = NULL;
661 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
662 drmFreeKernelVersion(version);
666 if (version->name_len)
667 version->name = drmMalloc(version->name_len + 1);
668 if (version->date_len)
669 version->date = drmMalloc(version->date_len + 1);
670 if (version->desc_len)
671 version->desc = drmMalloc(version->desc_len + 1);
673 if (ioctl(fd, DRM_IOCTL_VERSION, version)) {
674 drmMsg("DRM_IOCTL_VERSION: %s\n", strerror(errno));
675 drmFreeKernelVersion(version);
679 /* The results might not be null-terminated strings, so terminate them. */
680 if (version->name_len) version->name[version->name_len] = '\0';
681 if (version->date_len) version->date[version->date_len] = '\0';
682 if (version->desc_len) version->desc[version->desc_len] = '\0';
684 retval = drmMalloc(sizeof(*retval));
685 drmCopyVersion(retval, version);
686 drmFreeKernelVersion(version);
692 * Get version information for the DRM user space library.
694 * This version number is driver independent.
696 * \param fd file descriptor.
698 * \return version information.
701 * This function allocates and fills a drm_version structure with a hard coded
704 drmVersionPtr drmGetLibVersion(int fd)
706 drm_version_t *version = drmMalloc(sizeof(*version));
709 * revision 1.0.x = original DRM interface with no drmGetLibVersion
710 * entry point and many drm<Device> extensions
711 * revision 1.1.x = added drmCommand entry points for device extensions
712 * added drmGetLibVersion to identify libdrm.a version
713 * revision 1.2.x = added drmSetInterfaceVersion
714 * modified drmOpen to handle both busid and name
716 version->version_major = 1;
717 version->version_minor = 2;
718 version->version_patchlevel = 0;
720 return (drmVersionPtr)version;
725 * Free the bus ID information.
727 * \param busid bus ID information string as given by drmGetBusid().
730 * This function is just frees the memory pointed by \p busid.
732 void drmFreeBusid(const char *busid)
734 drmFree((void *)busid);
739 * Get the bus ID of the device.
741 * \param fd file descriptor.
743 * \return bus ID string.
746 * This function gets the bus ID via successive DRM_IOCTL_GET_UNIQUE ioctls to
747 * get the string length and data, passing the arguments in a drm_unique
750 char *drmGetBusid(int fd)
757 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
758 u.unique = drmMalloc(u.unique_len + 1);
759 if (ioctl(fd, DRM_IOCTL_GET_UNIQUE, &u)) return NULL;
760 u.unique[u.unique_len] = '\0';
767 * Set the bus ID of the device.
769 * \param fd file descriptor.
770 * \param busid bus ID string.
772 * \return zero on success, negative on failure.
775 * This function is a wrapper around the DRM_IOCTL_SET_UNIQUE ioctl, passing
776 * the arguments in a drm_unique structure.
778 int drmSetBusid(int fd, const char *busid)
782 u.unique = (char *)busid;
783 u.unique_len = strlen(busid);
785 if (ioctl(fd, DRM_IOCTL_SET_UNIQUE, &u)) {
791 int drmGetMagic(int fd, drm_magic_t * magic)
796 if (ioctl(fd, DRM_IOCTL_GET_MAGIC, &auth)) return -errno;
801 int drmAuthMagic(int fd, drm_magic_t magic)
806 if (ioctl(fd, DRM_IOCTL_AUTH_MAGIC, &auth)) return -errno;
811 * Specifies a range of memory that is available for mapping by a
814 * \param fd file descriptor.
815 * \param offset usually the physical address. The actual meaning depends of
816 * the \p type parameter. See below.
817 * \param size of the memory in bytes.
818 * \param type type of the memory to be mapped.
819 * \param flags combination of several flags to modify the function actions.
820 * \param handle will be set to a value that may be used as the offset
821 * parameter for mmap().
823 * \return zero on success or a negative value on error.
825 * \par Mapping the frame buffer
826 * For the frame buffer
827 * - \p offset will be the physical address of the start of the frame buffer,
828 * - \p size will be the size of the frame buffer in bytes, and
829 * - \p type will be DRM_FRAME_BUFFER.
832 * The area mapped will be uncached. If MTRR support is available in the
833 * kernel, the frame buffer area will be set to write combining.
835 * \par Mapping the MMIO register area
836 * For the MMIO register area,
837 * - \p offset will be the physical address of the start of the register area,
838 * - \p size will be the size of the register area bytes, and
839 * - \p type will be DRM_REGISTERS.
841 * The area mapped will be uncached.
843 * \par Mapping the SAREA
845 * - \p offset will be ignored and should be set to zero,
846 * - \p size will be the desired size of the SAREA in bytes,
847 * - \p type will be DRM_SHM.
850 * A shared memory area of the requested size will be created and locked in
851 * kernel memory. This area may be mapped into client-space by using the handle
854 * \note May only be called by root.
857 * This function is a wrapper around the DRM_IOCTL_ADD_MAP ioctl, passing
858 * the arguments in a drm_map structure.
860 int drmAddMap(int fd, drm_handle_t offset, drmSize size, drmMapType type,
861 drmMapFlags flags, drm_handle_t *handle)
870 if (ioctl(fd, DRM_IOCTL_ADD_MAP, &map)) return -errno;
871 if (handle) *handle = (drm_handle_t)map.handle;
875 int drmRmMap(int fd, drm_handle_t handle)
879 map.handle = (void *)handle;
881 if(ioctl(fd, DRM_IOCTL_RM_MAP, &map)) return -errno;
886 * Make buffers available for DMA transfers.
888 * \param fd file descriptor.
889 * \param count number of buffers.
890 * \param size size of each buffer.
891 * \param flags buffer allocation flags.
892 * \param agp_offset offset in the AGP aperture
894 * \return number of buffers allocated, negative on error.
897 * This function is a wrapper around DRM_IOCTL_ADD_BUFS ioctl.
901 int drmAddBufs(int fd, int count, int size, drmBufDescFlags flags,
904 drm_buf_desc_t request;
906 request.count = count;
908 request.low_mark = 0;
909 request.high_mark = 0;
910 request.flags = flags;
911 request.agp_start = agp_offset;
913 if (ioctl(fd, DRM_IOCTL_ADD_BUFS, &request)) return -errno;
914 return request.count;
917 int drmMarkBufs(int fd, double low, double high)
925 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return -EINVAL;
927 if (!info.count) return -EINVAL;
929 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
932 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
938 for (i = 0; i < info.count; i++) {
939 info.list[i].low_mark = low * info.list[i].count;
940 info.list[i].high_mark = high * info.list[i].count;
941 if (ioctl(fd, DRM_IOCTL_MARK_BUFS, &info.list[i])) {
955 * \param fd file descriptor.
956 * \param count number of buffers to free.
957 * \param list list of buffers to be freed.
959 * \return zero on success, or a negative value on failure.
961 * \note This function is primarily used for debugging.
964 * This function is a wrapper around the DRM_IOCTL_FREE_BUFS ioctl, passing
965 * the arguments in a drm_buf_free structure.
967 int drmFreeBufs(int fd, int count, int *list)
969 drm_buf_free_t request;
971 request.count = count;
973 if (ioctl(fd, DRM_IOCTL_FREE_BUFS, &request)) return -errno;
981 * \param fd file descriptor.
984 * This function closes the file descriptor.
988 unsigned long key = drmGetKeyFromFd(fd);
989 drmHashEntry *entry = drmGetEntry(fd);
991 drmHashDestroy(entry->tagTable);
994 entry->tagTable = NULL;
996 drmHashDelete(drmHashTable, key);
1004 * Map a region of memory.
1006 * \param fd file descriptor.
1007 * \param handle handle returned by drmAddMap().
1008 * \param size size in bytes. Must match the size used by drmAddMap().
1009 * \param address will contain the user-space virtual address where the mapping
1012 * \return zero on success, or a negative value on failure.
1015 * This function is a wrapper for mmap().
1017 int drmMap(int fd, drm_handle_t handle, drmSize size, drmAddressPtr address)
1019 static unsigned long pagesize_mask = 0;
1021 if (fd < 0) return -EINVAL;
1024 pagesize_mask = getpagesize() - 1;
1026 size = (size + pagesize_mask) & ~pagesize_mask;
1028 *address = mmap(0, size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, handle);
1029 if (*address == MAP_FAILED) return -errno;
1035 * Unmap mappings obtained with drmMap().
1037 * \param address address as given by drmMap().
1038 * \param size size in bytes. Must match the size used by drmMap().
1040 * \return zero on success, or a negative value on failure.
1043 * This function is a wrapper for munmap().
1045 int drmUnmap(drmAddress address, drmSize size)
1047 return munmap(address, size);
1050 drmBufInfoPtr drmGetBufInfo(int fd)
1052 drm_buf_info_t info;
1053 drmBufInfoPtr retval;
1059 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) return NULL;
1062 if (!(info.list = drmMalloc(info.count * sizeof(*info.list))))
1065 if (ioctl(fd, DRM_IOCTL_INFO_BUFS, &info)) {
1070 retval = drmMalloc(sizeof(*retval));
1071 retval->count = info.count;
1072 retval->list = drmMalloc(info.count * sizeof(*retval->list));
1073 for (i = 0; i < info.count; i++) {
1074 retval->list[i].count = info.list[i].count;
1075 retval->list[i].size = info.list[i].size;
1076 retval->list[i].low_mark = info.list[i].low_mark;
1077 retval->list[i].high_mark = info.list[i].high_mark;
1086 * Map all DMA buffers into client-virtual space.
1088 * \param fd file descriptor.
1090 * \return a pointer to a ::drmBufMap structure.
1092 * \note The client may not use these buffers until obtaining buffer indices
1096 * This function calls the DRM_IOCTL_MAP_BUFS ioctl and copies the returned
1097 * information about the buffers in a drm_buf_map structure into the
1098 * client-visible data structures.
1100 drmBufMapPtr drmMapBufs(int fd)
1103 drmBufMapPtr retval;
1108 bufs.virtual = NULL;
1109 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) return NULL;
1111 if (!bufs.count) return NULL;
1113 if (!(bufs.list = drmMalloc(bufs.count * sizeof(*bufs.list))))
1116 if (ioctl(fd, DRM_IOCTL_MAP_BUFS, &bufs)) {
1121 retval = drmMalloc(sizeof(*retval));
1122 retval->count = bufs.count;
1123 retval->list = drmMalloc(bufs.count * sizeof(*retval->list));
1124 for (i = 0; i < bufs.count; i++) {
1125 retval->list[i].idx = bufs.list[i].idx;
1126 retval->list[i].total = bufs.list[i].total;
1127 retval->list[i].used = 0;
1128 retval->list[i].address = bufs.list[i].address;
1138 * Unmap buffers allocated with drmMapBufs().
1140 * \return zero on success, or negative value on failure.
1143 * Calls munmap() for every buffer stored in \p bufs and frees the
1144 * memory allocated by drmMapBufs().
1146 int drmUnmapBufs(drmBufMapPtr bufs)
1150 for (i = 0; i < bufs->count; i++) {
1151 munmap(bufs->list[i].address, bufs->list[i].total);
1154 drmFree(bufs->list);
1161 #define DRM_DMA_RETRY 16
1164 * Reserve DMA buffers.
1166 * \param fd file descriptor.
1169 * \return zero on success, or a negative value on failure.
1172 * Assemble the arguments into a drm_dma structure and keeps issuing the
1173 * DRM_IOCTL_DMA ioctl until success or until maximum number of retries.
1175 int drmDMA(int fd, drmDMAReqPtr request)
1180 dma.context = request->context;
1181 dma.send_count = request->send_count;
1182 dma.send_indices = request->send_list;
1183 dma.send_sizes = request->send_sizes;
1184 dma.flags = request->flags;
1185 dma.request_count = request->request_count;
1186 dma.request_size = request->request_size;
1187 dma.request_indices = request->request_list;
1188 dma.request_sizes = request->request_sizes;
1189 dma.granted_count = 0;
1192 ret = ioctl( fd, DRM_IOCTL_DMA, &dma );
1193 } while ( ret && errno == EAGAIN && i++ < DRM_DMA_RETRY );
1196 request->granted_count = dma.granted_count;
1205 * Obtain heavyweight hardware lock.
1207 * \param fd file descriptor.
1208 * \param context context.
1209 * \param flags flags that determine the sate of the hardware when the function
1212 * \return always zero.
1215 * This function translates the arguments into a drm_lock structure and issue
1216 * the DRM_IOCTL_LOCK ioctl until the lock is successfully acquired.
1218 int drmGetLock(int fd, drm_context_t context, drmLockFlags flags)
1222 lock.context = context;
1224 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1225 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1226 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1227 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1228 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1229 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1231 while (ioctl(fd, DRM_IOCTL_LOCK, &lock))
1237 * Release the hardware lock.
1239 * \param fd file descriptor.
1240 * \param context context.
1242 * \return zero on success, or a negative value on failure.
1245 * This function is a wrapper around the DRM_IOCTL_UNLOCK ioctl, passing the
1246 * argument in a drm_lock structure.
1248 int drmUnlock(int fd, drm_context_t context)
1252 lock.context = context;
1254 return ioctl(fd, DRM_IOCTL_UNLOCK, &lock);
1257 drm_context_t *drmGetReservedContextList(int fd, int *count)
1261 drm_context_t * retval;
1265 res.contexts = NULL;
1266 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1268 if (!res.count) return NULL;
1270 if (!(list = drmMalloc(res.count * sizeof(*list)))) return NULL;
1271 if (!(retval = drmMalloc(res.count * sizeof(*retval)))) {
1276 res.contexts = list;
1277 if (ioctl(fd, DRM_IOCTL_RES_CTX, &res)) return NULL;
1279 for (i = 0; i < res.count; i++) retval[i] = list[i].handle;
1286 void drmFreeReservedContextList(drm_context_t *pt)
1294 * Used by the X server during GLXContext initialization. This causes
1295 * per-context kernel-level resources to be allocated.
1297 * \param fd file descriptor.
1298 * \param handle is set on success. To be used by the client when requesting DMA
1299 * dispatch with drmDMA().
1301 * \return zero on success, or a negative value on failure.
1303 * \note May only be called by root.
1306 * This function is a wrapper around the DRM_IOCTL_ADD_CTX ioctl, passing the
1307 * argument in a drm_ctx structure.
1309 int drmCreateContext(int fd, drm_context_t *handle)
1313 ctx.flags = 0; /* Modified with functions below */
1314 if (ioctl(fd, DRM_IOCTL_ADD_CTX, &ctx)) return -errno;
1315 *handle = ctx.handle;
1319 int drmSwitchToContext(int fd, drm_context_t context)
1323 ctx.handle = context;
1324 if (ioctl(fd, DRM_IOCTL_SWITCH_CTX, &ctx)) return -errno;
1328 int drmSetContextFlags(int fd, drm_context_t context, drm_context_tFlags flags)
1333 * Context preserving means that no context switches are done between DMA
1334 * buffers from one context and the next. This is suitable for use in the
1335 * X server (which promises to maintain hardware context), or in the
1336 * client-side library when buffers are swapped on behalf of two threads.
1338 ctx.handle = context;
1340 if (flags & DRM_CONTEXT_PRESERVED) ctx.flags |= _DRM_CONTEXT_PRESERVED;
1341 if (flags & DRM_CONTEXT_2DONLY) ctx.flags |= _DRM_CONTEXT_2DONLY;
1342 if (ioctl(fd, DRM_IOCTL_MOD_CTX, &ctx)) return -errno;
1346 int drmGetContextFlags(int fd, drm_context_t context,
1347 drm_context_tFlagsPtr flags)
1351 ctx.handle = context;
1352 if (ioctl(fd, DRM_IOCTL_GET_CTX, &ctx)) return -errno;
1354 if (ctx.flags & _DRM_CONTEXT_PRESERVED) *flags |= DRM_CONTEXT_PRESERVED;
1355 if (ctx.flags & _DRM_CONTEXT_2DONLY) *flags |= DRM_CONTEXT_2DONLY;
1362 * Free any kernel-level resources allocated with drmCreateContext() associated
1365 * \param fd file descriptor.
1366 * \param handle handle given by drmCreateContext().
1368 * \return zero on success, or a negative value on failure.
1370 * \note May only be called by root.
1373 * This function is a wrapper around the DRM_IOCTL_RM_CTX ioctl, passing the
1374 * argument in a drm_ctx structure.
1376 int drmDestroyContext(int fd, drm_context_t handle)
1379 ctx.handle = handle;
1380 if (ioctl(fd, DRM_IOCTL_RM_CTX, &ctx)) return -errno;
1384 int drmCreateDrawable(int fd, drm_drawable_t *handle)
1387 if (ioctl(fd, DRM_IOCTL_ADD_DRAW, &draw)) return -errno;
1388 *handle = draw.handle;
1392 int drmDestroyDrawable(int fd, drm_drawable_t handle)
1395 draw.handle = handle;
1396 if (ioctl(fd, DRM_IOCTL_RM_DRAW, &draw)) return -errno;
1401 * Acquire the AGP device.
1403 * Must be called before any of the other AGP related calls.
1405 * \param fd file descriptor.
1407 * \return zero on success, or a negative value on failure.
1410 * This function is a wrapper around the DRM_IOCTL_AGP_ACQUIRE ioctl.
1412 int drmAgpAcquire(int fd)
1414 if (ioctl(fd, DRM_IOCTL_AGP_ACQUIRE, NULL)) return -errno;
1420 * Release the AGP device.
1422 * \param fd file descriptor.
1424 * \return zero on success, or a negative value on failure.
1427 * This function is a wrapper around the DRM_IOCTL_AGP_RELEASE ioctl.
1429 int drmAgpRelease(int fd)
1431 if (ioctl(fd, DRM_IOCTL_AGP_RELEASE, NULL)) return -errno;
1439 * \param fd file descriptor.
1440 * \param mode AGP mode.
1442 * \return zero on success, or a negative value on failure.
1445 * This function is a wrapper around the DRM_IOCTL_AGP_ENABLE ioctl, passing the
1446 * argument in a drm_agp_mode structure.
1448 int drmAgpEnable(int fd, unsigned long mode)
1453 if (ioctl(fd, DRM_IOCTL_AGP_ENABLE, &m)) return -errno;
1459 * Allocate a chunk of AGP memory.
1461 * \param fd file descriptor.
1462 * \param size requested memory size in bytes. Will be rounded to page boundary.
1463 * \param type type of memory to allocate.
1464 * \param address if not zero, will be set to the physical address of the
1466 * \param handle on success will be set to a handle of the allocated memory.
1468 * \return zero on success, or a negative value on failure.
1471 * This function is a wrapper around the DRM_IOCTL_AGP_ALLOC ioctl, passing the
1472 * arguments in a drm_agp_buffer structure.
1474 int drmAgpAlloc(int fd, unsigned long size, unsigned long type,
1475 unsigned long *address, drm_handle_t *handle)
1479 *handle = DRM_AGP_NO_HANDLE;
1483 if (ioctl(fd, DRM_IOCTL_AGP_ALLOC, &b)) return -errno;
1484 if (address != 0UL) *address = b.physical;
1491 * Free a chunk of AGP memory.
1493 * \param fd file descriptor.
1494 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1496 * \return zero on success, or a negative value on failure.
1499 * This function is a wrapper around the DRM_IOCTL_AGP_FREE ioctl, passing the
1500 * argument in a drm_agp_buffer structure.
1502 int drmAgpFree(int fd, drm_handle_t handle)
1508 if (ioctl(fd, DRM_IOCTL_AGP_FREE, &b)) return -errno;
1514 * Bind a chunk of AGP memory.
1516 * \param fd file descriptor.
1517 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1518 * \param offset offset in bytes. It will round to page boundary.
1520 * \return zero on success, or a negative value on failure.
1523 * This function is a wrapper around the DRM_IOCTL_AGP_BIND ioctl, passing the
1524 * argument in a drm_agp_binding structure.
1526 int drmAgpBind(int fd, drm_handle_t handle, unsigned long offset)
1528 drm_agp_binding_t b;
1532 if (ioctl(fd, DRM_IOCTL_AGP_BIND, &b)) return -errno;
1538 * Unbind a chunk of AGP memory.
1540 * \param fd file descriptor.
1541 * \param handle handle to the allocated memory, as given by drmAgpAllocate().
1543 * \return zero on success, or a negative value on failure.
1546 * This function is a wrapper around the DRM_IOCTL_AGP_UNBIND ioctl, passing
1547 * the argument in a drm_agp_binding structure.
1549 int drmAgpUnbind(int fd, drm_handle_t handle)
1551 drm_agp_binding_t b;
1555 if (ioctl(fd, DRM_IOCTL_AGP_UNBIND, &b)) return -errno;
1561 * Get AGP driver major version number.
1563 * \param fd file descriptor.
1565 * \return major version number on success, or a negative value on failure..
1568 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1569 * necessary information in a drm_agp_info structure.
1571 int drmAgpVersionMajor(int fd)
1575 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1576 return i.agp_version_major;
1581 * Get AGP driver minor version number.
1583 * \param fd file descriptor.
1585 * \return minor version number on success, or a negative value on failure.
1588 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1589 * necessary information in a drm_agp_info structure.
1591 int drmAgpVersionMinor(int fd)
1595 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return -errno;
1596 return i.agp_version_minor;
1603 * \param fd file descriptor.
1605 * \return mode on success, or zero on failure.
1608 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1609 * necessary information in a drm_agp_info structure.
1611 unsigned long drmAgpGetMode(int fd)
1615 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1621 * Get AGP aperture base.
1623 * \param fd file descriptor.
1625 * \return aperture base on success, zero on failure.
1628 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1629 * necessary information in a drm_agp_info structure.
1631 unsigned long drmAgpBase(int fd)
1635 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1636 return i.aperture_base;
1641 * Get AGP aperture size.
1643 * \param fd file descriptor.
1645 * \return aperture size on success, zero on failure.
1648 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1649 * necessary information in a drm_agp_info structure.
1651 unsigned long drmAgpSize(int fd)
1655 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1656 return i.aperture_size;
1661 * Get used AGP memory.
1663 * \param fd file descriptor.
1665 * \return memory used on success, or zero on failure.
1668 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1669 * necessary information in a drm_agp_info structure.
1671 unsigned long drmAgpMemoryUsed(int fd)
1675 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1676 return i.memory_used;
1681 * Get available AGP memory.
1683 * \param fd file descriptor.
1685 * \return memory available on success, or zero on failure.
1688 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1689 * necessary information in a drm_agp_info structure.
1691 unsigned long drmAgpMemoryAvail(int fd)
1695 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1696 return i.memory_allowed;
1701 * Get hardware vendor ID.
1703 * \param fd file descriptor.
1705 * \return vendor ID on success, or zero on failure.
1708 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1709 * necessary information in a drm_agp_info structure.
1711 unsigned int drmAgpVendorId(int fd)
1715 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1721 * Get hardware device ID.
1723 * \param fd file descriptor.
1725 * \return zero on success, or zero on failure.
1728 * This function is a wrapper around the DRM_IOCTL_AGP_INFO ioctl, getting the
1729 * necessary information in a drm_agp_info structure.
1731 unsigned int drmAgpDeviceId(int fd)
1735 if (ioctl(fd, DRM_IOCTL_AGP_INFO, &i)) return 0;
1739 int drmScatterGatherAlloc(int fd, unsigned long size, drm_handle_t *handle)
1741 drm_scatter_gather_t sg;
1746 if (ioctl(fd, DRM_IOCTL_SG_ALLOC, &sg)) return -errno;
1747 *handle = sg.handle;
1751 int drmScatterGatherFree(int fd, drm_handle_t handle)
1753 drm_scatter_gather_t sg;
1757 if (ioctl(fd, DRM_IOCTL_SG_FREE, &sg)) return -errno;
1764 * \param fd file descriptor.
1765 * \param vbl pointer to a drmVBlank structure.
1767 * \return zero on success, or a negative value on failure.
1770 * This function is a wrapper around the DRM_IOCTL_WAIT_VBLANK ioctl.
1772 int drmWaitVBlank(int fd, drmVBlankPtr vbl)
1777 ret = ioctl(fd, DRM_IOCTL_WAIT_VBLANK, vbl);
1778 vbl->request.type &= ~DRM_VBLANK_RELATIVE;
1779 } while (ret && errno == EINTR);
1784 int drmError(int err, const char *label)
1787 case DRM_ERR_NO_DEVICE: fprintf(stderr, "%s: no device\n", label); break;
1788 case DRM_ERR_NO_ACCESS: fprintf(stderr, "%s: no access\n", label); break;
1789 case DRM_ERR_NOT_ROOT: fprintf(stderr, "%s: not root\n", label); break;
1790 case DRM_ERR_INVALID: fprintf(stderr, "%s: invalid args\n", label);break;
1792 if (err < 0) err = -err;
1793 fprintf( stderr, "%s: error %d (%s)\n", label, err, strerror(err) );
1801 * Install IRQ handler.
1803 * \param fd file descriptor.
1804 * \param irq IRQ number.
1806 * \return zero on success, or a negative value on failure.
1809 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1810 * argument in a drm_control structure.
1812 int drmCtlInstHandler(int fd, int irq)
1816 ctl.func = DRM_INST_HANDLER;
1818 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1824 * Uninstall IRQ handler.
1826 * \param fd file descriptor.
1828 * \return zero on success, or a negative value on failure.
1831 * This function is a wrapper around the DRM_IOCTL_CONTROL ioctl, passing the
1832 * argument in a drm_control structure.
1834 int drmCtlUninstHandler(int fd)
1838 ctl.func = DRM_UNINST_HANDLER;
1840 if (ioctl(fd, DRM_IOCTL_CONTROL, &ctl)) return -errno;
1844 int drmFinish(int fd, int context, drmLockFlags flags)
1848 lock.context = context;
1850 if (flags & DRM_LOCK_READY) lock.flags |= _DRM_LOCK_READY;
1851 if (flags & DRM_LOCK_QUIESCENT) lock.flags |= _DRM_LOCK_QUIESCENT;
1852 if (flags & DRM_LOCK_FLUSH) lock.flags |= _DRM_LOCK_FLUSH;
1853 if (flags & DRM_LOCK_FLUSH_ALL) lock.flags |= _DRM_LOCK_FLUSH_ALL;
1854 if (flags & DRM_HALT_ALL_QUEUES) lock.flags |= _DRM_HALT_ALL_QUEUES;
1855 if (flags & DRM_HALT_CUR_QUEUES) lock.flags |= _DRM_HALT_CUR_QUEUES;
1856 if (ioctl(fd, DRM_IOCTL_FINISH, &lock)) return -errno;
1861 * Get IRQ from bus ID.
1863 * \param fd file descriptor.
1864 * \param busnum bus number.
1865 * \param devnum device number.
1866 * \param funcnum function number.
1868 * \return IRQ number on success, or a negative value on failure.
1871 * This function is a wrapper around the DRM_IOCTL_IRQ_BUSID ioctl, passing the
1872 * arguments in a drm_irq_busid structure.
1874 int drmGetInterruptFromBusID(int fd, int busnum, int devnum, int funcnum)
1880 p.funcnum = funcnum;
1881 if (ioctl(fd, DRM_IOCTL_IRQ_BUSID, &p)) return -errno;
1885 int drmAddContextTag(int fd, drm_context_t context, void *tag)
1887 drmHashEntry *entry = drmGetEntry(fd);
1889 if (drmHashInsert(entry->tagTable, context, tag)) {
1890 drmHashDelete(entry->tagTable, context);
1891 drmHashInsert(entry->tagTable, context, tag);
1896 int drmDelContextTag(int fd, drm_context_t context)
1898 drmHashEntry *entry = drmGetEntry(fd);
1900 return drmHashDelete(entry->tagTable, context);
1903 void *drmGetContextTag(int fd, drm_context_t context)
1905 drmHashEntry *entry = drmGetEntry(fd);
1908 if (drmHashLookup(entry->tagTable, context, &value)) return NULL;
1913 int drmAddContextPrivateMapping(int fd, drm_context_t ctx_id,
1914 drm_handle_t handle)
1916 drm_ctx_priv_map_t map;
1918 map.ctx_id = ctx_id;
1919 map.handle = (void *)handle;
1921 if (ioctl(fd, DRM_IOCTL_SET_SAREA_CTX, &map)) return -errno;
1925 int drmGetContextPrivateMapping(int fd, drm_context_t ctx_id,
1926 drm_handle_t *handle)
1928 drm_ctx_priv_map_t map;
1930 map.ctx_id = ctx_id;
1932 if (ioctl(fd, DRM_IOCTL_GET_SAREA_CTX, &map)) return -errno;
1933 if (handle) *handle = (drm_handle_t)map.handle;
1938 int drmGetMap(int fd, int idx, drm_handle_t *offset, drmSize *size,
1939 drmMapType *type, drmMapFlags *flags, drm_handle_t *handle,
1945 if (ioctl(fd, DRM_IOCTL_GET_MAP, &map)) return -errno;
1946 *offset = map.offset;
1950 *handle = (unsigned long)map.handle;
1955 int drmGetClient(int fd, int idx, int *auth, int *pid, int *uid,
1956 unsigned long *magic, unsigned long *iocs)
1958 drm_client_t client;
1961 if (ioctl(fd, DRM_IOCTL_GET_CLIENT, &client)) return -errno;
1962 *auth = client.auth;
1965 *magic = client.magic;
1966 *iocs = client.iocs;
1970 int drmGetStats(int fd, drmStatsT *stats)
1975 if (ioctl(fd, DRM_IOCTL_GET_STATS, &s)) return -errno;
1978 memset(stats, 0, sizeof(*stats));
1979 if (s.count > sizeof(stats->data)/sizeof(stats->data[0]))
1983 stats->data[i].long_format = "%-20.20s"; \
1984 stats->data[i].rate_format = "%8.8s"; \
1985 stats->data[i].isvalue = 1; \
1986 stats->data[i].verbose = 0
1989 stats->data[i].long_format = "%-20.20s"; \
1990 stats->data[i].rate_format = "%5.5s"; \
1991 stats->data[i].isvalue = 0; \
1992 stats->data[i].mult_names = "kgm"; \
1993 stats->data[i].mult = 1000; \
1994 stats->data[i].verbose = 0
1997 stats->data[i].long_format = "%-20.20s"; \
1998 stats->data[i].rate_format = "%5.5s"; \
1999 stats->data[i].isvalue = 0; \
2000 stats->data[i].mult_names = "KGM"; \
2001 stats->data[i].mult = 1024; \
2002 stats->data[i].verbose = 0
2005 stats->count = s.count;
2006 for (i = 0; i < s.count; i++) {
2007 stats->data[i].value = s.data[i].value;
2008 switch (s.data[i].type) {
2009 case _DRM_STAT_LOCK:
2010 stats->data[i].long_name = "Lock";
2011 stats->data[i].rate_name = "Lock";
2014 case _DRM_STAT_OPENS:
2015 stats->data[i].long_name = "Opens";
2016 stats->data[i].rate_name = "O";
2018 stats->data[i].verbose = 1;
2020 case _DRM_STAT_CLOSES:
2021 stats->data[i].long_name = "Closes";
2022 stats->data[i].rate_name = "Lock";
2024 stats->data[i].verbose = 1;
2026 case _DRM_STAT_IOCTLS:
2027 stats->data[i].long_name = "Ioctls";
2028 stats->data[i].rate_name = "Ioc/s";
2031 case _DRM_STAT_LOCKS:
2032 stats->data[i].long_name = "Locks";
2033 stats->data[i].rate_name = "Lck/s";
2036 case _DRM_STAT_UNLOCKS:
2037 stats->data[i].long_name = "Unlocks";
2038 stats->data[i].rate_name = "Unl/s";
2042 stats->data[i].long_name = "IRQs";
2043 stats->data[i].rate_name = "IRQ/s";
2046 case _DRM_STAT_PRIMARY:
2047 stats->data[i].long_name = "Primary Bytes";
2048 stats->data[i].rate_name = "PB/s";
2051 case _DRM_STAT_SECONDARY:
2052 stats->data[i].long_name = "Secondary Bytes";
2053 stats->data[i].rate_name = "SB/s";
2057 stats->data[i].long_name = "DMA";
2058 stats->data[i].rate_name = "DMA/s";
2061 case _DRM_STAT_SPECIAL:
2062 stats->data[i].long_name = "Special DMA";
2063 stats->data[i].rate_name = "dma/s";
2066 case _DRM_STAT_MISSED:
2067 stats->data[i].long_name = "Miss";
2068 stats->data[i].rate_name = "Ms/s";
2071 case _DRM_STAT_VALUE:
2072 stats->data[i].long_name = "Value";
2073 stats->data[i].rate_name = "Value";
2076 case _DRM_STAT_BYTE:
2077 stats->data[i].long_name = "Bytes";
2078 stats->data[i].rate_name = "B/s";
2081 case _DRM_STAT_COUNT:
2083 stats->data[i].long_name = "Count";
2084 stats->data[i].rate_name = "Cnt/s";
2093 * Issue a set-version ioctl.
2095 * \param fd file descriptor.
2096 * \param drmCommandIndex command index
2097 * \param data source pointer of the data to be read and written.
2098 * \param size size of the data to be read and written.
2100 * \return zero on success, or a negative value on failure.
2103 * It issues a read-write ioctl given by
2104 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2106 int drmSetInterfaceVersion(int fd, drmSetVersion *version)
2109 drm_set_version_t sv;
2111 sv.drm_di_major = version->drm_di_major;
2112 sv.drm_di_minor = version->drm_di_minor;
2113 sv.drm_dd_major = version->drm_dd_major;
2114 sv.drm_dd_minor = version->drm_dd_minor;
2116 if (ioctl(fd, DRM_IOCTL_SET_VERSION, &sv)) {
2120 version->drm_di_major = sv.drm_di_major;
2121 version->drm_di_minor = sv.drm_di_minor;
2122 version->drm_dd_major = sv.drm_dd_major;
2123 version->drm_dd_minor = sv.drm_dd_minor;
2129 * Send a device-specific command.
2131 * \param fd file descriptor.
2132 * \param drmCommandIndex command index
2134 * \return zero on success, or a negative value on failure.
2137 * It issues a ioctl given by
2138 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2140 int drmCommandNone(int fd, unsigned long drmCommandIndex)
2142 void *data = NULL; /* dummy */
2143 unsigned long request;
2145 request = DRM_IO( DRM_COMMAND_BASE + drmCommandIndex);
2147 if (ioctl(fd, request, data)) {
2155 * Send a device-specific read command.
2157 * \param fd file descriptor.
2158 * \param drmCommandIndex command index
2159 * \param data destination pointer of the data to be read.
2160 * \param size size of the data to be read.
2162 * \return zero on success, or a negative value on failure.
2165 * It issues a read ioctl given by
2166 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2168 int drmCommandRead(int fd, unsigned long drmCommandIndex, void *data,
2171 unsigned long request;
2173 request = DRM_IOC( DRM_IOC_READ, DRM_IOCTL_BASE,
2174 DRM_COMMAND_BASE + drmCommandIndex, size);
2176 if (ioctl(fd, request, data)) {
2184 * Send a device-specific write command.
2186 * \param fd file descriptor.
2187 * \param drmCommandIndex command index
2188 * \param data source pointer of the data to be written.
2189 * \param size size of the data to be written.
2191 * \return zero on success, or a negative value on failure.
2194 * It issues a write ioctl given by
2195 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2197 int drmCommandWrite(int fd, unsigned long drmCommandIndex, void *data,
2200 unsigned long request;
2202 request = DRM_IOC( DRM_IOC_WRITE, DRM_IOCTL_BASE,
2203 DRM_COMMAND_BASE + drmCommandIndex, size);
2205 if (ioctl(fd, request, data)) {
2213 * Send a device-specific read-write command.
2215 * \param fd file descriptor.
2216 * \param drmCommandIndex command index
2217 * \param data source pointer of the data to be read and written.
2218 * \param size size of the data to be read and written.
2220 * \return zero on success, or a negative value on failure.
2223 * It issues a read-write ioctl given by
2224 * \code DRM_COMMAND_BASE + drmCommandIndex \endcode.
2226 int drmCommandWriteRead(int fd, unsigned long drmCommandIndex, void *data,
2229 unsigned long request;
2231 request = DRM_IOC( DRM_IOC_READ|DRM_IOC_WRITE, DRM_IOCTL_BASE,
2232 DRM_COMMAND_BASE + drmCommandIndex, size);
2234 if (ioctl(fd, request, data)) {
2240 int drmFenceCreate(int fd, int shareable, int class,unsigned type,
2244 drm_fence_arg_t arg;
2248 arg.flags = (shareable) ? DRM_FENCE_FLAG_SHAREABLE : 0;
2249 arg.flags |= (emit) ? DRM_FENCE_FLAG_EMIT : 0;
2250 arg.op = drm_fence_create;
2251 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2253 fence->handle = arg.handle;
2254 fence->class = arg.class;
2255 fence->type = arg.type;
2256 fence->signaled = 0;
2260 int drmFenceDestroy(int fd, const drmFence *fence)
2262 drm_fence_arg_t arg;
2264 arg.handle = fence->handle;
2265 arg.op = drm_fence_destroy;
2266 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2271 int drmFenceReference(int fd, unsigned handle, drmFence *fence)
2273 drm_fence_arg_t arg;
2275 arg.handle = handle;
2276 arg.op = drm_fence_reference;
2277 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2279 fence->handle = arg.handle;
2280 fence->class = arg.class;
2281 fence->type = arg.type;
2282 fence->signaled = arg.signaled;
2286 int drmFenceUnreference(int fd, const drmFence *fence)
2288 drm_fence_arg_t arg;
2290 arg.handle = fence->handle;
2291 arg.op = drm_fence_unreference;
2292 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2297 int drmFenceFlush(int fd, drmFence *fence, unsigned flush_type)
2299 drm_fence_arg_t arg;
2301 arg.handle = fence->handle;
2302 arg.type = flush_type;
2303 arg.op = drm_fence_flush;
2304 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2306 fence->class = arg.class;
2307 fence->type = arg.type;
2308 fence->signaled = arg.signaled;
2312 int drmFenceSignaled(int fd, drmFence *fence)
2314 drm_fence_arg_t arg;
2316 arg.handle = fence->handle;
2317 arg.op = drm_fence_signaled;
2318 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2320 fence->class = arg.class;
2321 fence->type = arg.type;
2322 fence->signaled = arg.signaled;
2326 int drmFenceEmit(int fd, drmFence *fence, unsigned emit_type)
2328 drm_fence_arg_t arg;
2330 arg.handle = fence->handle;
2331 arg.type = emit_type;
2332 arg.op = drm_fence_emit;
2333 if (ioctl(fd, DRM_IOCTL_FENCE, &arg))
2335 fence->class = arg.class;
2336 fence->type = arg.type;
2337 fence->signaled = arg.signaled;
2341 int drmFenceWait(int fd, drmFence *fence, unsigned flush_type,
2342 int lazy, int ignore_signals)
2344 drm_fence_arg_t arg;
2347 arg.handle = fence->handle;
2348 arg.type = flush_type;
2349 arg.flags = (lazy) ? DRM_FENCE_FLAG_WAIT_LAZY : 0;
2350 arg.flags |= (ignore_signals) ? DRM_FENCE_FLAG_WAIT_IGNORE_SIGNALS : 0;
2351 arg.op = drm_fence_wait;
2353 ret = ioctl(fd, DRM_IOCTL_FENCE, &arg);
2354 } while (ret != 0 && errno == EAGAIN);
2359 fence->class = arg.class;
2360 fence->type = arg.type;
2361 fence->signaled = arg.signaled;